Line data Source code
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.
4 :
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.
9 :
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.
14 :
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/>. */
17 :
18 : #ifdef HAVE_CONFIG_H
19 : # include <config.h>
20 : #endif
21 :
22 : #include <argp.h>
23 : #include <assert.h>
24 : #include <ctype.h>
25 : #include <dwarf.h>
26 : #include <errno.h>
27 : #include <error.h>
28 : #include <fcntl.h>
29 : #include <gelf.h>
30 : #include <inttypes.h>
31 : #include <langinfo.h>
32 : #include <libdw.h>
33 : #include <libdwfl.h>
34 : #include <libintl.h>
35 : #include <locale.h>
36 : #include <stdarg.h>
37 : #include <stdbool.h>
38 : #include <stdio.h>
39 : #include <stdio_ext.h>
40 : #include <stdlib.h>
41 : #include <string.h>
42 : #include <strings.h>
43 : #include <time.h>
44 : #include <unistd.h>
45 : #include <sys/stat.h>
46 : #include <signal.h>
47 :
48 : #include <libeu.h>
49 : #include <system.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"
58 :
59 : #include "../libdw/known-dwarf.h"
60 :
61 : #ifdef __linux__
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
67 : #else
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. */
74 : #endif
75 :
76 : /* Name and version of program. */
77 : ARGP_PROGRAM_VERSION_HOOK_DEF = print_version;
78 :
79 : /* Bug report address. */
80 : ARGP_PROGRAM_BUG_ADDRESS_DEF = PACKAGE_BUGREPORT;
81 :
82 : /* argp key value for --elf-section, non-ascii. */
83 : #define ELF_INPUT_SECTION 256
84 :
85 : /* argp key value for --dwarf-skeleton, non-ascii. */
86 : #define DWARF_SKELETON 257
87 :
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;
91 :
92 : /* Definitions of arguments for argp functions. */
93 : static const struct argp_option options[] =
94 : {
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 "
98 : "input data"), 0 },
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 },
123 :
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 },
136 :
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 }
147 : };
148 :
149 : /* Short description of program. */
150 : static const char doc[] = N_("\
151 : Print information from ELF file in human-readable form.");
152 :
153 : /* Strings for arguments in help texts. */
154 : static const char args_doc[] = N_("FILE...");
155 :
156 : /* Prototype for option handler. */
157 : static error_t parse_opt (int key, char *arg, struct argp_state *state);
158 :
159 : /* Data structure to communicate with argp functions. */
160 : static struct argp argp =
161 : {
162 : options, parse_opt, args_doc, doc, NULL, NULL, NULL
163 : };
164 :
165 : /* If non-null, the section from which we should read to (compressed) ELF. */
166 : static const char *elf_input_section = NULL;
167 :
168 : /* If non-null, the file that contains the skeleton CUs. */
169 : static const char *dwarf_skeleton = NULL;
170 :
171 : /* Flags set by the option controlling the output. */
172 :
173 : /* True if dynamic segment should be printed. */
174 : static bool print_dynamic_table;
175 :
176 : /* True if the file header should be printed. */
177 : static bool print_file_header;
178 :
179 : /* True if the program headers should be printed. */
180 : static bool print_program_header;
181 :
182 : /* True if relocations should be printed. */
183 : static bool print_relocations;
184 :
185 : /* True if the section headers should be printed. */
186 : static bool print_section_header;
187 :
188 : /* True if the symbol table should be printed. */
189 : static bool print_symbol_table;
190 :
191 : /* A specific section name, or NULL to print all symbol tables. */
192 : static char *symbol_table_section;
193 :
194 : /* True if the version information should be printed. */
195 : static bool print_version_info;
196 :
197 : /* True if section groups should be printed. */
198 : static bool print_section_groups;
199 :
200 : /* True if bucket list length histogram should be printed. */
201 : static bool print_histogram;
202 :
203 : /* True if the architecture specific data should be printed. */
204 : static bool print_arch;
205 :
206 : /* True if note section content should be printed. */
207 : static bool print_notes;
208 :
209 : /* True if SHF_STRINGS section content should be printed. */
210 : static bool print_string_sections;
211 :
212 : /* True if archive index should be printed. */
213 : static bool print_archive_index;
214 :
215 : /* True if any of the control options except print_archive_index is set. */
216 : static bool any_control_option;
217 :
218 : /* True if we should print addresses from DWARF in symbolic form. */
219 : static bool print_address_names = true;
220 :
221 : /* True if we should print raw values instead of relativized addresses. */
222 : static bool print_unresolved_addresses = false;
223 :
224 : /* True if we should print the .debug_aranges section using libdw. */
225 : static bool decodedaranges = false;
226 :
227 : /* True if we should print the .debug_aranges section using libdw. */
228 : static bool decodedline = false;
229 :
230 : /* True if we want to show more information about compressed sections. */
231 : static bool print_decompress = false;
232 :
233 : /* True if we want to show split compile units for debug_info skeletons. */
234 : static bool show_split_units = false;
235 :
236 : /* Select printing of debugging sections. */
237 : static enum section_e
238 : {
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;
260 :
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;
264 :
265 : /* Select string dumping of sections. */
266 : static struct section_argument *string_sections;
267 : static struct section_argument **string_sections_tail = &string_sections;
268 :
269 : struct section_argument
270 : {
271 : struct section_argument *next;
272 : const char *arg;
273 : bool implicit;
274 : };
275 :
276 : /* Numbers of sections and program headers in the file. */
277 : static size_t shnum;
278 : static size_t phnum;
279 :
280 :
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,
291 : GElf_Shdr *shdr);
292 : static void handle_relocs_rela (Ebl *ebl, GElf_Ehdr *ehdr, Elf_Scn *scn,
293 : GElf_Shdr *shdr);
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,
300 : GElf_Shdr *shdr);
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 *);
310 :
311 :
312 : /* Looked up once with gettext in main. */
313 : static char *yes_str;
314 : static char *no_str;
315 :
316 : int
317 211 : main (int argc, char *argv[])
318 : {
319 : /* We use no threads here which can interfere with handling a stream. */
320 211 : (void) __fsetlocking (stdout, FSETLOCKING_BYCALLER);
321 :
322 : /* Set locale. */
323 211 : setlocale (LC_ALL, "");
324 :
325 : /* Initialize the message catalog. */
326 211 : textdomain (PACKAGE_TARNAME);
327 :
328 : /* Look up once. */
329 211 : yes_str = gettext ("yes");
330 211 : no_str = gettext ("no");
331 :
332 : /* Parse and process arguments. */
333 : int remaining;
334 211 : argp_parse (&argp, argc, argv, 0, &remaining, NULL);
335 :
336 : /* Before we start tell the ELF library which version we are using. */
337 211 : elf_version (EV_CURRENT);
338 :
339 : /* Now process all the files given at the command line. */
340 211 : bool only_one = remaining + 1 == argc;
341 : do
342 : {
343 : /* Open the file. */
344 434 : int fd = open (argv[remaining], O_RDONLY);
345 217 : if (fd == -1)
346 : {
347 0 : error (0, errno, gettext ("cannot open input file"));
348 0 : continue;
349 : }
350 :
351 217 : process_file (fd, argv[remaining], only_one);
352 :
353 217 : close (fd);
354 : }
355 217 : while (++remaining < argc);
356 :
357 211 : return error_message_count != 0;
358 : }
359 :
360 :
361 : /* Handle program arguments. */
362 : static error_t
363 1325 : parse_opt (int key, char *arg,
364 : struct argp_state *state __attribute__ ((unused)))
365 : {
366 102 : void add_dump_section (const char *name, bool implicit)
367 : {
368 102 : struct section_argument *a = xmalloc (sizeof *a);
369 102 : a->arg = name;
370 102 : a->next = NULL;
371 102 : a->implicit = implicit;
372 102 : struct section_argument ***tailp
373 102 : = key == 'x' ? &dump_data_sections_tail : &string_sections_tail;
374 102 : **tailp = a;
375 102 : *tailp = &a->next;
376 102 : }
377 :
378 1325 : switch (key)
379 : {
380 32 : case 'a':
381 32 : print_file_header = true;
382 32 : print_program_header = true;
383 32 : print_relocations = true;
384 32 : print_section_header = true;
385 32 : print_symbol_table = true;
386 32 : print_version_info = true;
387 32 : print_dynamic_table = true;
388 32 : print_section_groups = true;
389 32 : print_histogram = true;
390 32 : print_arch = true;
391 32 : print_notes = true;
392 32 : implicit_debug_sections |= section_exception;
393 32 : add_dump_section (".strtab", true);
394 32 : add_dump_section (".dynstr", true);
395 32 : add_dump_section (".comment", true);
396 32 : any_control_option = true;
397 32 : break;
398 4 : case 'A':
399 4 : print_arch = true;
400 4 : any_control_option = true;
401 4 : break;
402 2 : case 'd':
403 2 : print_dynamic_table = true;
404 2 : any_control_option = true;
405 2 : break;
406 0 : case 'e':
407 0 : print_debug_sections |= section_exception;
408 0 : any_control_option = true;
409 0 : break;
410 0 : case 'g':
411 0 : print_section_groups = true;
412 0 : any_control_option = true;
413 0 : break;
414 0 : case 'h':
415 0 : print_file_header = true;
416 0 : any_control_option = true;
417 0 : break;
418 0 : case 'I':
419 0 : print_histogram = true;
420 0 : any_control_option = true;
421 0 : break;
422 0 : case 'l':
423 0 : print_program_header = true;
424 0 : any_control_option = true;
425 0 : break;
426 10 : case 'n':
427 10 : print_notes = true;
428 10 : any_control_option = true;
429 10 : break;
430 1 : case 'r':
431 1 : print_relocations = true;
432 1 : any_control_option = true;
433 1 : break;
434 54 : case 'S':
435 54 : print_section_header = true;
436 54 : any_control_option = true;
437 54 : break;
438 14 : case 's':
439 14 : print_symbol_table = true;
440 14 : any_control_option = true;
441 14 : symbol_table_section = arg;
442 14 : break;
443 0 : case 'V':
444 0 : print_version_info = true;
445 0 : any_control_option = true;
446 0 : break;
447 2 : case 'c':
448 2 : print_archive_index = true;
449 2 : break;
450 101 : case 'w':
451 101 : if (arg == NULL)
452 : {
453 33 : print_debug_sections = section_all;
454 33 : implicit_debug_sections = section_info;
455 33 : show_split_units = true;
456 : }
457 68 : else if (strcmp (arg, "abbrev") == 0)
458 0 : print_debug_sections |= section_abbrev;
459 68 : else if (strcmp (arg, "addr") == 0)
460 : {
461 2 : print_debug_sections |= section_addr;
462 2 : implicit_debug_sections |= section_info;
463 : }
464 66 : else if (strcmp (arg, "aranges") == 0)
465 3 : print_debug_sections |= section_aranges;
466 63 : else if (strcmp (arg, "decodedaranges") == 0)
467 : {
468 1 : print_debug_sections |= section_aranges;
469 1 : decodedaranges = true;
470 : }
471 62 : else if (strcmp (arg, "ranges") == 0)
472 : {
473 12 : print_debug_sections |= section_ranges;
474 12 : implicit_debug_sections |= section_info;
475 : }
476 50 : else if (strcmp (arg, "frame") == 0 || strcmp (arg, "frames") == 0)
477 2 : print_debug_sections |= section_frame;
478 48 : else if (strcmp (arg, "info") == 0)
479 : {
480 13 : print_debug_sections |= section_info;
481 13 : print_debug_sections |= section_types;
482 : }
483 35 : else if (strcmp (arg, "info+") == 0)
484 : {
485 2 : print_debug_sections |= section_info;
486 2 : print_debug_sections |= section_types;
487 2 : show_split_units = true;
488 : }
489 33 : else if (strcmp (arg, "loc") == 0)
490 : {
491 15 : print_debug_sections |= section_loc;
492 15 : implicit_debug_sections |= section_info;
493 : }
494 18 : else if (strcmp (arg, "line") == 0)
495 6 : print_debug_sections |= section_line;
496 12 : else if (strcmp (arg, "decodedline") == 0)
497 : {
498 4 : print_debug_sections |= section_line;
499 4 : decodedline = true;
500 : }
501 8 : else if (strcmp (arg, "pubnames") == 0)
502 0 : print_debug_sections |= section_pubnames;
503 8 : else if (strcmp (arg, "str") == 0)
504 : {
505 3 : print_debug_sections |= section_str;
506 : /* For mapping string offset tables to CUs. */
507 3 : implicit_debug_sections |= section_info;
508 : }
509 5 : else if (strcmp (arg, "macinfo") == 0)
510 0 : print_debug_sections |= section_macinfo;
511 5 : else if (strcmp (arg, "macro") == 0)
512 3 : print_debug_sections |= section_macro;
513 2 : else if (strcmp (arg, "exception") == 0)
514 0 : print_debug_sections |= section_exception;
515 2 : else if (strcmp (arg, "gdb_index") == 0)
516 2 : print_debug_sections |= section_gdb_index;
517 : else
518 : {
519 0 : fprintf (stderr, gettext ("Unknown DWARF debug section `%s'.\n"),
520 : arg);
521 0 : argp_help (&argp, stderr, ARGP_HELP_SEE,
522 : program_invocation_short_name);
523 0 : exit (1);
524 : }
525 101 : any_control_option = true;
526 101 : break;
527 1 : case 'p':
528 1 : any_control_option = true;
529 1 : if (arg == NULL)
530 : {
531 0 : print_string_sections = true;
532 0 : break;
533 : }
534 : FALLTHROUGH;
535 : case 'x':
536 6 : add_dump_section (arg, false);
537 6 : any_control_option = true;
538 6 : break;
539 2 : case 'N':
540 2 : print_address_names = false;
541 2 : break;
542 20 : case 'U':
543 20 : print_unresolved_addresses = true;
544 20 : break;
545 0 : case ARGP_KEY_NO_ARGS:
546 0 : fputs (gettext ("Missing file name.\n"), stderr);
547 0 : goto do_argp_help;
548 211 : case ARGP_KEY_FINI:
549 211 : if (! any_control_option && ! print_archive_index)
550 : {
551 0 : fputs (gettext ("No operation specified.\n"), stderr);
552 0 : do_argp_help:
553 0 : argp_help (&argp, stderr, ARGP_HELP_SEE,
554 : program_invocation_short_name);
555 0 : exit (EXIT_FAILURE);
556 : }
557 : break;
558 : case 'W': /* Ignored. */
559 : break;
560 13 : case 'z':
561 13 : print_decompress = true;
562 13 : break;
563 4 : case ELF_INPUT_SECTION:
564 4 : if (arg == NULL)
565 4 : elf_input_section = ".gnu_debugdata";
566 : else
567 0 : elf_input_section = arg;
568 : break;
569 5 : case DWARF_SKELETON:
570 5 : dwarf_skeleton = arg;
571 5 : break;
572 : default:
573 : return ARGP_ERR_UNKNOWN;
574 : }
575 : return 0;
576 : }
577 :
578 :
579 : /* Create a file descriptor to read the data from the
580 : elf_input_section given a file descriptor to an ELF file. */
581 : static int
582 4 : open_input_section (int fd)
583 : {
584 : size_t shnums;
585 : size_t cnt;
586 : size_t shstrndx;
587 4 : Elf *elf = elf_begin (fd, ELF_C_READ_MMAP, NULL);
588 4 : if (elf == NULL)
589 : {
590 0 : error (0, 0, gettext ("cannot generate Elf descriptor: %s"),
591 : elf_errmsg (-1));
592 0 : return -1;
593 : }
594 :
595 4 : if (elf_getshdrnum (elf, &shnums) < 0)
596 : {
597 0 : error (0, 0, gettext ("cannot determine number of sections: %s"),
598 : elf_errmsg (-1));
599 0 : open_error:
600 0 : elf_end (elf);
601 0 : return -1;
602 : }
603 :
604 4 : if (elf_getshdrstrndx (elf, &shstrndx) < 0)
605 : {
606 0 : error (0, 0, gettext ("cannot get section header string table index"));
607 : goto open_error;
608 : }
609 :
610 178 : for (cnt = 0; cnt < shnums; ++cnt)
611 : {
612 91 : Elf_Scn *scn = elf_getscn (elf, cnt);
613 91 : if (scn == NULL)
614 : {
615 0 : error (0, 0, gettext ("cannot get section: %s"),
616 : elf_errmsg (-1));
617 0 : goto open_error;
618 : }
619 :
620 : GElf_Shdr shdr_mem;
621 91 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
622 91 : if (unlikely (shdr == NULL))
623 : {
624 0 : error (0, 0, gettext ("cannot get section header: %s"),
625 : elf_errmsg (-1));
626 : goto open_error;
627 : }
628 :
629 91 : const char *sname = elf_strptr (elf, shstrndx, shdr->sh_name);
630 91 : if (sname == NULL)
631 : {
632 0 : error (0, 0, gettext ("cannot get section name"));
633 : goto open_error;
634 : }
635 :
636 91 : if (strcmp (sname, elf_input_section) == 0)
637 : {
638 4 : Elf_Data *data = elf_rawdata (scn, NULL);
639 4 : if (data == NULL)
640 : {
641 0 : error (0, 0, gettext ("cannot get %s content: %s"),
642 : sname, elf_errmsg (-1));
643 : goto open_error;
644 : }
645 :
646 : /* Create (and immediately unlink) a temporary file to store
647 : section data in to create a file descriptor for it. */
648 4 : const char *tmpdir = getenv ("TMPDIR") ?: P_tmpdir;
649 : static const char suffix[] = "/readelfXXXXXX";
650 4 : int tmplen = strlen (tmpdir) + sizeof (suffix);
651 4 : char *tempname = alloca (tmplen);
652 4 : sprintf (tempname, "%s%s", tmpdir, suffix);
653 :
654 4 : int sfd = mkstemp (tempname);
655 4 : if (sfd == -1)
656 : {
657 0 : error (0, 0, gettext ("cannot create temp file '%s'"),
658 : tempname);
659 : goto open_error;
660 : }
661 4 : unlink (tempname);
662 :
663 4 : ssize_t size = data->d_size;
664 4 : if (write_retry (sfd, data->d_buf, size) != size)
665 : {
666 0 : error (0, 0, gettext ("cannot write section data"));
667 : goto open_error;
668 : }
669 :
670 4 : if (elf_end (elf) != 0)
671 : {
672 0 : error (0, 0, gettext ("error while closing Elf descriptor: %s"),
673 : elf_errmsg (-1));
674 0 : return -1;
675 : }
676 :
677 4 : if (lseek (sfd, 0, SEEK_SET) == -1)
678 : {
679 0 : error (0, 0, gettext ("error while rewinding file descriptor"));
680 0 : return -1;
681 : }
682 :
683 : return sfd;
684 : }
685 : }
686 :
687 : /* Named section not found. */
688 0 : if (elf_end (elf) != 0)
689 0 : error (0, 0, gettext ("error while closing Elf descriptor: %s"),
690 : elf_errmsg (-1));
691 : return -1;
692 : }
693 :
694 : /* Check if the file is an archive, and if so dump its index. */
695 : static void
696 2 : check_archive_index (int fd, const char *fname, bool only_one)
697 : {
698 : /* Create an `Elf' descriptor. */
699 2 : Elf *elf = elf_begin (fd, ELF_C_READ_MMAP, NULL);
700 2 : if (elf == NULL)
701 0 : error (0, 0, gettext ("cannot generate Elf descriptor: %s"),
702 : elf_errmsg (-1));
703 : else
704 : {
705 2 : if (elf_kind (elf) == ELF_K_AR)
706 : {
707 2 : if (!only_one)
708 : printf ("\n%s:\n\n", fname);
709 2 : dump_archive_index (elf, fname);
710 : }
711 : else
712 : error (0, 0,
713 0 : gettext ("'%s' is not an archive, cannot print archive index"),
714 : fname);
715 :
716 : /* Now we can close the descriptor. */
717 2 : if (elf_end (elf) != 0)
718 0 : error (0, 0, gettext ("error while closing Elf descriptor: %s"),
719 : elf_errmsg (-1));
720 : }
721 2 : }
722 :
723 : /* Trivial callback used for checking if we opened an archive. */
724 : static int
725 203 : count_dwflmod (Dwfl_Module *dwflmod __attribute__ ((unused)),
726 : void **userdata __attribute__ ((unused)),
727 : const char *name __attribute__ ((unused)),
728 : Dwarf_Addr base __attribute__ ((unused)),
729 : void *arg)
730 : {
731 203 : if (*(bool *) arg)
732 : return DWARF_CB_ABORT;
733 203 : *(bool *) arg = true;
734 203 : return DWARF_CB_OK;
735 : }
736 :
737 : struct process_dwflmod_args
738 : {
739 : int fd;
740 : bool only_one;
741 : };
742 :
743 : static int
744 215 : process_dwflmod (Dwfl_Module *dwflmod,
745 : void **userdata __attribute__ ((unused)),
746 : const char *name __attribute__ ((unused)),
747 : Dwarf_Addr base __attribute__ ((unused)),
748 : void *arg)
749 : {
750 215 : const struct process_dwflmod_args *a = arg;
751 :
752 : /* Print the file name. */
753 215 : if (!a->only_one)
754 : {
755 : const char *fname;
756 12 : dwfl_module_info (dwflmod, NULL, NULL, NULL, NULL, NULL, &fname, NULL);
757 :
758 24 : printf ("\n%s:\n\n", fname);
759 : }
760 :
761 215 : process_elf_file (dwflmod, a->fd);
762 :
763 215 : return DWARF_CB_OK;
764 : }
765 :
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. */
769 : static int
770 29 : find_no_debuginfo (Dwfl_Module *mod,
771 : void **userdata,
772 : const char *modname,
773 : Dwarf_Addr base,
774 : const char *file_name,
775 : const char *debuglink_file,
776 : GElf_Word debuglink_crc,
777 : char **debuginfo_file_name)
778 : {
779 : Dwarf_Addr dwbias;
780 29 : dwfl_module_info (mod, NULL, NULL, NULL, &dwbias, NULL, NULL, NULL);
781 :
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 29 : if (dwbias == (Dwarf_Addr) -1)
787 : return -1;
788 :
789 5 : return dwfl_standard_find_debuginfo (mod, userdata, modname, base,
790 : file_name, debuglink_file,
791 : debuglink_crc, debuginfo_file_name);
792 : }
793 :
794 : static Dwfl *
795 224 : create_dwfl (int fd, const char *fname)
796 : {
797 : /* Duplicate an fd for dwfl_report_offline to swallow. */
798 224 : int dwfl_fd = dup (fd);
799 224 : if (unlikely (dwfl_fd < 0))
800 0 : error (EXIT_FAILURE, errno, "dup");
801 :
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 =
805 : {
806 : .section_address = dwfl_offline_section_address,
807 : .find_debuginfo = find_no_debuginfo
808 : };
809 224 : Dwfl *dwfl = dwfl_begin (&callbacks);
810 224 : if (likely (dwfl != NULL))
811 : /* Let 0 be the logical address of the file (or first in archive). */
812 224 : dwfl->offline_next_address = 0;
813 224 : if (dwfl_report_offline (dwfl, fname, fname, dwfl_fd) == NULL)
814 : {
815 : struct stat st;
816 0 : if (fstat (dwfl_fd, &st) != 0)
817 0 : error (0, errno, gettext ("cannot stat input file"));
818 0 : else if (unlikely (st.st_size == 0))
819 0 : error (0, 0, gettext ("input file is empty"));
820 : else
821 0 : error (0, 0, gettext ("failed reading '%s': %s"),
822 : fname, dwfl_errmsg (-1));
823 0 : close (dwfl_fd); /* Consumed on success, not on failure. */
824 0 : dwfl = NULL;
825 : }
826 : else
827 224 : dwfl_report_end (dwfl, NULL, NULL);
828 :
829 224 : return dwfl;
830 : }
831 :
832 : /* Process one input file. */
833 : static void
834 217 : process_file (int fd, const char *fname, bool only_one)
835 : {
836 217 : if (print_archive_index)
837 2 : check_archive_index (fd, fname, only_one);
838 :
839 217 : if (!any_control_option)
840 : return;
841 :
842 215 : if (elf_input_section != NULL)
843 : {
844 : /* Replace fname and fd with section content. */
845 4 : char *fnname = alloca (strlen (fname) + strlen (elf_input_section) + 2);
846 8 : sprintf (fnname, "%s:%s", fname, elf_input_section);
847 4 : fd = open_input_section (fd);
848 4 : if (fd == -1)
849 : {
850 0 : error (0, 0, gettext ("No such section '%s' in '%s'"),
851 : elf_input_section, fname);
852 : return;
853 : }
854 : fname = fnname;
855 : }
856 :
857 215 : Dwfl *dwfl = create_dwfl (fd, fname);
858 215 : if (dwfl != NULL)
859 : {
860 215 : if (only_one)
861 : {
862 : /* Clear ONLY_ONE if we have multiple modules, from an archive. */
863 203 : bool seen = false;
864 203 : only_one = dwfl_getmodules (dwfl, &count_dwflmod, &seen, 0) == 0;
865 : }
866 :
867 : /* Process the one or more modules gleaned from this file. */
868 215 : struct process_dwflmod_args a = { .fd = fd, .only_one = only_one };
869 215 : dwfl_getmodules (dwfl, &process_dwflmod, &a, 0);
870 : }
871 : /* Terrible hack for hooking unrelated skeleton/split compile units,
872 : see __libdw_link_skel_split in print_debug. */
873 215 : if (! do_not_close_dwfl)
874 215 : dwfl_end (dwfl);
875 :
876 : /* Need to close the replaced fd if we created it. Caller takes
877 : care of original. */
878 215 : if (elf_input_section != NULL)
879 4 : close (fd);
880 : }
881 :
882 : /* Check whether there are any compressed sections in the ELF file. */
883 : static bool
884 73 : elf_contains_chdrs (Elf *elf)
885 : {
886 73 : Elf_Scn *scn = NULL;
887 1749 : while ((scn = elf_nextscn (elf, scn)) != NULL)
888 : {
889 : GElf_Shdr shdr_mem;
890 1609 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
891 1609 : if (shdr != NULL && (shdr->sh_flags & SHF_COMPRESSED) != 0)
892 6 : return true;
893 : }
894 : return false;
895 : }
896 :
897 : /* Process one ELF file. */
898 : static void
899 215 : process_elf_file (Dwfl_Module *dwflmod, int fd)
900 : {
901 : GElf_Addr dwflbias;
902 215 : Elf *elf = dwfl_module_getelf (dwflmod, &dwflbias);
903 :
904 : GElf_Ehdr ehdr_mem;
905 215 : GElf_Ehdr *ehdr = gelf_getehdr (elf, &ehdr_mem);
906 :
907 215 : if (ehdr == NULL)
908 : {
909 0 : elf_error:
910 0 : error (0, 0, gettext ("cannot read ELF header: %s"), elf_errmsg (-1));
911 0 : return;
912 : }
913 :
914 215 : Ebl *ebl = ebl_openbackend (elf);
915 215 : if (unlikely (ebl == NULL))
916 : {
917 0 : ebl_error:
918 0 : error (0, errno, gettext ("cannot create EBL handle"));
919 : return;
920 : }
921 :
922 : /* Determine the number of sections. */
923 215 : if (unlikely (elf_getshdrnum (ebl->elf, &shnum) < 0))
924 0 : error (EXIT_FAILURE, 0,
925 0 : gettext ("cannot determine number of sections: %s"),
926 : elf_errmsg (-1));
927 :
928 : /* Determine the number of phdrs. */
929 215 : if (unlikely (elf_getphdrnum (ebl->elf, &phnum) < 0))
930 0 : error (EXIT_FAILURE, 0,
931 0 : gettext ("cannot determine number of program headers: %s"),
932 : elf_errmsg (-1));
933 :
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 430 : bool print_unchanged = ((print_section_header
940 129 : || print_relocations
941 128 : || dump_data_sections != NULL
942 123 : || print_notes)
943 317 : && (ehdr->e_type == ET_REL
944 73 : || elf_contains_chdrs (ebl->elf)));
945 :
946 215 : Elf *pure_elf = NULL;
947 215 : Ebl *pure_ebl = ebl;
948 215 : if (print_unchanged)
949 : {
950 : /* Read the file afresh. */
951 35 : off_t aroff = elf_getaroff (elf);
952 35 : pure_elf = elf_begin (fd, ELF_C_READ_MMAP, NULL);
953 35 : if (aroff > 0)
954 : {
955 : /* Archive member. */
956 0 : (void) elf_rand (pure_elf, aroff);
957 0 : Elf *armem = elf_begin (-1, ELF_C_READ_MMAP, pure_elf);
958 0 : elf_end (pure_elf);
959 0 : pure_elf = armem;
960 : }
961 35 : if (pure_elf == NULL)
962 : goto elf_error;
963 35 : pure_ebl = ebl_openbackend (pure_elf);
964 35 : if (pure_ebl == NULL)
965 : goto ebl_error;
966 : }
967 :
968 215 : if (print_file_header)
969 32 : print_ehdr (ebl, ehdr);
970 215 : if (print_section_header)
971 86 : print_shdr (pure_ebl, ehdr);
972 215 : if (print_program_header)
973 32 : print_phdr (ebl, ehdr);
974 215 : if (print_section_groups)
975 32 : print_scngrp (ebl);
976 215 : if (print_dynamic_table)
977 34 : print_dynamic (ebl);
978 215 : if (print_relocations)
979 33 : print_relocs (pure_ebl, ehdr);
980 215 : if (print_histogram)
981 32 : handle_hash (ebl);
982 215 : if (print_symbol_table)
983 46 : print_symtab (ebl, SHT_DYNSYM);
984 215 : if (print_version_info)
985 32 : print_verinfo (ebl);
986 215 : if (print_symbol_table)
987 46 : print_symtab (ebl, SHT_SYMTAB);
988 215 : if (print_arch)
989 36 : print_liblist (ebl);
990 215 : if (print_arch)
991 36 : print_attributes (ebl, ehdr);
992 215 : if (dump_data_sections != NULL)
993 5 : dump_data (pure_ebl);
994 215 : if (string_sections != NULL)
995 33 : dump_strings (ebl);
996 215 : if ((print_debug_sections | implicit_debug_sections) != 0)
997 124 : print_debug (dwflmod, ebl, ehdr);
998 215 : if (print_notes)
999 42 : handle_notes (pure_ebl, ehdr);
1000 215 : if (print_string_sections)
1001 0 : print_strings (ebl);
1002 :
1003 215 : ebl_closebackend (ebl);
1004 :
1005 215 : if (pure_ebl != ebl)
1006 : {
1007 35 : ebl_closebackend (pure_ebl);
1008 35 : elf_end (pure_elf);
1009 : }
1010 : }
1011 :
1012 :
1013 : /* Print file type. */
1014 : static void
1015 32 : print_file_type (unsigned short int e_type)
1016 : {
1017 32 : if (likely (e_type <= ET_CORE))
1018 : {
1019 : static const char *const knowntypes[] =
1020 : {
1021 : N_("NONE (None)"),
1022 : N_("REL (Relocatable file)"),
1023 : N_("EXEC (Executable file)"),
1024 : N_("DYN (Shared object file)"),
1025 : N_("CORE (Core file)")
1026 : };
1027 32 : puts (gettext (knowntypes[e_type]));
1028 : }
1029 0 : else if (e_type >= ET_LOOS && e_type <= ET_HIOS)
1030 0 : printf (gettext ("OS Specific: (%x)\n"), e_type);
1031 0 : else if (e_type >= ET_LOPROC /* && e_type <= ET_HIPROC always true */)
1032 0 : printf (gettext ("Processor Specific: (%x)\n"), e_type);
1033 : else
1034 0 : puts ("???");
1035 32 : }
1036 :
1037 :
1038 : /* Print ELF header. */
1039 : static void
1040 32 : print_ehdr (Ebl *ebl, GElf_Ehdr *ehdr)
1041 : {
1042 32 : fputs_unlocked (gettext ("ELF Header:\n Magic: "), stdout);
1043 544 : for (size_t cnt = 0; cnt < EI_NIDENT; ++cnt)
1044 1024 : printf (" %02hhx", ehdr->e_ident[cnt]);
1045 :
1046 96 : printf (gettext ("\n Class: %s\n"),
1047 32 : ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? "ELF32"
1048 : : ehdr->e_ident[EI_CLASS] == ELFCLASS64 ? "ELF64"
1049 28 : : "\?\?\?");
1050 :
1051 96 : printf (gettext (" Data: %s\n"),
1052 32 : ehdr->e_ident[EI_DATA] == ELFDATA2LSB
1053 : ? "2's complement, little endian"
1054 : : ehdr->e_ident[EI_DATA] == ELFDATA2MSB
1055 6 : ? "2's complement, big endian" : "\?\?\?");
1056 :
1057 96 : printf (gettext (" Ident Version: %hhd %s\n"),
1058 32 : ehdr->e_ident[EI_VERSION],
1059 32 : ehdr->e_ident[EI_VERSION] == EV_CURRENT ? gettext ("(current)")
1060 : : "(\?\?\?)");
1061 :
1062 : char buf[512];
1063 64 : printf (gettext (" OS/ABI: %s\n"),
1064 32 : ebl_osabi_name (ebl, ehdr->e_ident[EI_OSABI], buf, sizeof (buf)));
1065 :
1066 64 : printf (gettext (" ABI Version: %hhd\n"),
1067 32 : ehdr->e_ident[EI_ABIVERSION]);
1068 :
1069 32 : fputs_unlocked (gettext (" Type: "), stdout);
1070 32 : print_file_type (ehdr->e_type);
1071 :
1072 64 : printf (gettext (" Machine: %s\n"), ebl->name);
1073 :
1074 64 : printf (gettext (" Version: %d %s\n"),
1075 : ehdr->e_version,
1076 32 : ehdr->e_version == EV_CURRENT ? gettext ("(current)") : "(\?\?\?)");
1077 :
1078 64 : printf (gettext (" Entry point address: %#" PRIx64 "\n"),
1079 : ehdr->e_entry);
1080 :
1081 64 : printf (gettext (" Start of program headers: %" PRId64 " %s\n"),
1082 : ehdr->e_phoff, gettext ("(bytes into file)"));
1083 :
1084 64 : printf (gettext (" Start of section headers: %" PRId64 " %s\n"),
1085 : ehdr->e_shoff, gettext ("(bytes into file)"));
1086 :
1087 64 : printf (gettext (" Flags: %s\n"),
1088 : ebl_machine_flag_name (ebl, ehdr->e_flags, buf, sizeof (buf)));
1089 :
1090 96 : printf (gettext (" Size of this header: %" PRId16 " %s\n"),
1091 32 : ehdr->e_ehsize, gettext ("(bytes)"));
1092 :
1093 96 : printf (gettext (" Size of program header entries: %" PRId16 " %s\n"),
1094 32 : ehdr->e_phentsize, gettext ("(bytes)"));
1095 :
1096 64 : printf (gettext (" Number of program headers entries: %" PRId16),
1097 32 : ehdr->e_phnum);
1098 32 : if (ehdr->e_phnum == PN_XNUM)
1099 : {
1100 : GElf_Shdr shdr_mem;
1101 0 : GElf_Shdr *shdr = gelf_getshdr (elf_getscn (ebl->elf, 0), &shdr_mem);
1102 0 : if (shdr != NULL)
1103 0 : printf (gettext (" (%" PRIu32 " in [0].sh_info)"),
1104 0 : (uint32_t) shdr->sh_info);
1105 : else
1106 0 : fputs_unlocked (gettext (" ([0] not available)"), stdout);
1107 : }
1108 64 : fputc_unlocked ('\n', stdout);
1109 :
1110 96 : printf (gettext (" Size of section header entries: %" PRId16 " %s\n"),
1111 32 : ehdr->e_shentsize, gettext ("(bytes)"));
1112 :
1113 64 : printf (gettext (" Number of section headers entries: %" PRId16),
1114 32 : ehdr->e_shnum);
1115 32 : if (ehdr->e_shnum == 0)
1116 : {
1117 : GElf_Shdr shdr_mem;
1118 0 : GElf_Shdr *shdr = gelf_getshdr (elf_getscn (ebl->elf, 0), &shdr_mem);
1119 0 : if (shdr != NULL)
1120 0 : printf (gettext (" (%" PRIu32 " in [0].sh_size)"),
1121 0 : (uint32_t) shdr->sh_size);
1122 : else
1123 0 : fputs_unlocked (gettext (" ([0] not available)"), stdout);
1124 : }
1125 64 : fputc_unlocked ('\n', stdout);
1126 :
1127 32 : if (unlikely (ehdr->e_shstrndx == SHN_XINDEX))
1128 : {
1129 : GElf_Shdr shdr_mem;
1130 0 : GElf_Shdr *shdr = gelf_getshdr (elf_getscn (ebl->elf, 0), &shdr_mem);
1131 0 : if (shdr != NULL)
1132 : /* We managed to get the zeroth section. */
1133 0 : snprintf (buf, sizeof (buf), gettext (" (%" PRIu32 " in [0].sh_link)"),
1134 0 : (uint32_t) shdr->sh_link);
1135 : else
1136 : {
1137 0 : strncpy (buf, gettext (" ([0] not available)"), sizeof (buf));
1138 0 : buf[sizeof (buf) - 1] = '\0';
1139 : }
1140 :
1141 0 : printf (gettext (" Section header string table index: XINDEX%s\n\n"),
1142 : buf);
1143 : }
1144 : else
1145 32 : printf (gettext (" Section header string table index: %" PRId16 "\n\n"),
1146 : ehdr->e_shstrndx);
1147 32 : }
1148 :
1149 :
1150 : static const char *
1151 : get_visibility_type (int value)
1152 : {
1153 14421 : switch (value)
1154 : {
1155 : case STV_DEFAULT:
1156 : return "DEFAULT";
1157 0 : case STV_INTERNAL:
1158 : return "INTERNAL";
1159 104 : case STV_HIDDEN:
1160 : return "HIDDEN";
1161 0 : case STV_PROTECTED:
1162 : return "PROTECTED";
1163 0 : default:
1164 : return "???";
1165 : }
1166 : }
1167 :
1168 : static const char *
1169 : elf_ch_type_name (unsigned int code)
1170 : {
1171 12 : if (code == 0)
1172 : return "NONE";
1173 :
1174 12 : if (code == ELFCOMPRESS_ZLIB)
1175 : return "ZLIB";
1176 :
1177 : return "UNKNOWN";
1178 : }
1179 :
1180 : /* Print the section headers. */
1181 : static void
1182 86 : print_shdr (Ebl *ebl, GElf_Ehdr *ehdr)
1183 : {
1184 : size_t cnt;
1185 : size_t shstrndx;
1186 :
1187 86 : if (! print_file_header)
1188 108 : printf (gettext ("\
1189 : There are %d section headers, starting at offset %#" PRIx64 ":\n\
1190 : \n"),
1191 54 : ehdr->e_shnum, ehdr->e_shoff);
1192 :
1193 : /* Get the section header string table index. */
1194 86 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
1195 : error (EXIT_FAILURE, 0,
1196 0 : gettext ("cannot get section header string table index"));
1197 :
1198 86 : puts (gettext ("Section Headers:"));
1199 :
1200 86 : if (ehdr->e_ident[EI_CLASS] == ELFCLASS32)
1201 34 : puts (gettext ("[Nr] Name Type Addr Off Size ES Flags Lk Inf Al"));
1202 : else
1203 52 : puts (gettext ("[Nr] Name Type Addr Off Size ES Flags Lk Inf Al"));
1204 :
1205 86 : if (print_decompress)
1206 : {
1207 8 : if (ehdr->e_ident[EI_CLASS] == ELFCLASS32)
1208 4 : puts (gettext (" [Compression Size Al]"));
1209 : else
1210 4 : puts (gettext (" [Compression Size Al]"));
1211 : }
1212 :
1213 2246 : for (cnt = 0; cnt < shnum; ++cnt)
1214 : {
1215 2246 : Elf_Scn *scn = elf_getscn (ebl->elf, cnt);
1216 :
1217 2246 : if (unlikely (scn == NULL))
1218 0 : error (EXIT_FAILURE, 0, gettext ("cannot get section: %s"),
1219 : elf_errmsg (-1));
1220 :
1221 : /* Get the section header. */
1222 : GElf_Shdr shdr_mem;
1223 2246 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
1224 2246 : if (unlikely (shdr == NULL))
1225 0 : error (EXIT_FAILURE, 0, gettext ("cannot get section header: %s"),
1226 : elf_errmsg (-1));
1227 :
1228 : char flagbuf[20];
1229 2246 : char *cp = flagbuf;
1230 2246 : if (shdr->sh_flags & SHF_WRITE)
1231 518 : *cp++ = 'W';
1232 2246 : if (shdr->sh_flags & SHF_ALLOC)
1233 1469 : *cp++ = 'A';
1234 2246 : if (shdr->sh_flags & SHF_EXECINSTR)
1235 275 : *cp++ = 'X';
1236 2246 : if (shdr->sh_flags & SHF_MERGE)
1237 104 : *cp++ = 'M';
1238 2246 : if (shdr->sh_flags & SHF_STRINGS)
1239 102 : *cp++ = 'S';
1240 2246 : if (shdr->sh_flags & SHF_INFO_LINK)
1241 130 : *cp++ = 'I';
1242 2246 : if (shdr->sh_flags & SHF_LINK_ORDER)
1243 2 : *cp++ = 'L';
1244 2246 : if (shdr->sh_flags & SHF_OS_NONCONFORMING)
1245 0 : *cp++ = 'N';
1246 2246 : if (shdr->sh_flags & SHF_GROUP)
1247 0 : *cp++ = 'G';
1248 2246 : if (shdr->sh_flags & SHF_TLS)
1249 8 : *cp++ = 'T';
1250 2246 : if (shdr->sh_flags & SHF_COMPRESSED)
1251 12 : *cp++ = 'C';
1252 2246 : if (shdr->sh_flags & SHF_ORDERED)
1253 0 : *cp++ = 'O';
1254 2246 : if (shdr->sh_flags & SHF_EXCLUDE)
1255 0 : *cp++ = 'E';
1256 2246 : *cp = '\0';
1257 :
1258 : const char *sname;
1259 : char buf[128];
1260 2246 : sname = elf_strptr (ebl->elf, shstrndx, shdr->sh_name) ?: "<corrupt>";
1261 8984 : printf ("[%2zu] %-20s %-12s %0*" PRIx64 " %0*" PRIx64 " %0*" PRIx64
1262 : " %2" PRId64 " %-5s %2" PRId32 " %3" PRId32
1263 : " %2" PRId64 "\n",
1264 : cnt, sname,
1265 2246 : 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 2246 : 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);
1271 :
1272 2246 : if (print_decompress)
1273 : {
1274 76 : if ((shdr->sh_flags & SHF_COMPRESSED) != 0)
1275 : {
1276 : GElf_Chdr chdr;
1277 12 : if (gelf_getchdr (scn, &chdr) != NULL)
1278 36 : printf (" [ELF %s (%" PRId32 ") %0*" PRIx64
1279 : " %2" PRId64 "]\n",
1280 : elf_ch_type_name (chdr.ch_type),
1281 : chdr.ch_type,
1282 12 : ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 6 : 8,
1283 : chdr.ch_size, chdr.ch_addralign);
1284 : else
1285 0 : error (0, 0,
1286 0 : gettext ("bad compression header for section %zd: %s"),
1287 : elf_ndxscn (scn), elf_errmsg (-1));
1288 : }
1289 64 : else if (strncmp(".zdebug", sname, strlen (".zdebug")) == 0)
1290 : {
1291 : ssize_t size;
1292 12 : if ((size = dwelf_scn_gnu_compressed_size (scn)) >= 0)
1293 12 : printf (" [GNU ZLIB %0*zx ]\n",
1294 12 : ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 6 : 8, size);
1295 : else
1296 0 : error (0, 0,
1297 0 : gettext ("bad gnu compressed size for section %zd: %s"),
1298 : elf_ndxscn (scn), elf_errmsg (-1));
1299 : }
1300 : }
1301 : }
1302 :
1303 172 : fputc_unlocked ('\n', stdout);
1304 86 : }
1305 :
1306 :
1307 : /* Print the program header. */
1308 : static void
1309 32 : print_phdr (Ebl *ebl, GElf_Ehdr *ehdr)
1310 : {
1311 32 : if (phnum == 0)
1312 : /* No program header, this is OK in relocatable objects. */
1313 22 : return;
1314 :
1315 10 : puts (gettext ("Program Headers:"));
1316 10 : if (ehdr->e_ident[EI_CLASS] == ELFCLASS32)
1317 0 : puts (gettext ("\
1318 : Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align"));
1319 : else
1320 10 : puts (gettext ("\
1321 : Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align"));
1322 :
1323 : /* Process all program headers. */
1324 : bool has_relro = false;
1325 : GElf_Addr relro_from = 0;
1326 : GElf_Addr relro_to = 0;
1327 90 : for (size_t cnt = 0; cnt < phnum; ++cnt)
1328 : {
1329 : char buf[128];
1330 : GElf_Phdr mem;
1331 90 : GElf_Phdr *phdr = gelf_getphdr (ebl->elf, cnt, &mem);
1332 :
1333 : /* If for some reason the header cannot be returned show this. */
1334 90 : if (unlikely (phdr == NULL))
1335 : {
1336 0 : puts (" ???");
1337 0 : continue;
1338 : }
1339 :
1340 630 : 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 90 : ebl_segment_type_name (ebl, phdr->p_type, buf, sizeof (buf)),
1343 : phdr->p_offset,
1344 : ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 8 : 16, phdr->p_vaddr,
1345 90 : ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 8 : 16, phdr->p_paddr,
1346 : phdr->p_filesz,
1347 : phdr->p_memsz,
1348 90 : phdr->p_flags & PF_R ? 'R' : ' ',
1349 90 : phdr->p_flags & PF_W ? 'W' : ' ',
1350 90 : phdr->p_flags & PF_X ? 'E' : ' ',
1351 : phdr->p_align);
1352 :
1353 90 : if (phdr->p_type == PT_INTERP)
1354 : {
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. */
1364 :
1365 6 : Elf_Scn *scn = gelf_offscn (ebl->elf, phdr->p_offset);
1366 : GElf_Shdr shdr_mem;
1367 6 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
1368 :
1369 : size_t maxsize;
1370 6 : char *filedata = elf_rawfile (ebl->elf, &maxsize);
1371 :
1372 6 : if (shdr != NULL && shdr->sh_type == SHT_PROGBITS
1373 6 : && filedata != NULL && phdr->p_offset < maxsize
1374 6 : && phdr->p_filesz <= maxsize - phdr->p_offset
1375 6 : && memchr (filedata + phdr->p_offset, '\0',
1376 : phdr->p_filesz) != NULL)
1377 6 : printf (gettext ("\t[Requesting program interpreter: %s]\n"),
1378 : filedata + phdr->p_offset);
1379 : }
1380 84 : else if (phdr->p_type == PT_GNU_RELRO)
1381 : {
1382 10 : has_relro = true;
1383 10 : relro_from = phdr->p_vaddr;
1384 10 : relro_to = relro_from + phdr->p_memsz;
1385 : }
1386 : }
1387 :
1388 10 : if (ehdr->e_shnum == 0)
1389 : /* No sections in the file. Punt. */
1390 : return;
1391 :
1392 : /* Get the section header string table index. */
1393 : size_t shstrndx;
1394 10 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
1395 : error (EXIT_FAILURE, 0,
1396 0 : gettext ("cannot get section header string table index"));
1397 :
1398 10 : puts (gettext ("\n Section to Segment mapping:\n Segment Sections..."));
1399 :
1400 100 : for (size_t cnt = 0; cnt < phnum; ++cnt)
1401 : {
1402 : /* Print the segment number. */
1403 90 : printf (" %2.2zu ", cnt);
1404 :
1405 : GElf_Phdr phdr_mem;
1406 90 : GElf_Phdr *phdr = gelf_getphdr (ebl->elf, cnt, &phdr_mem);
1407 : /* This must not happen. */
1408 90 : if (unlikely (phdr == NULL))
1409 0 : error (EXIT_FAILURE, 0, gettext ("cannot get program header: %s"),
1410 : elf_errmsg (-1));
1411 :
1412 : /* Iterate over the sections. */
1413 : bool in_relro = false;
1414 : bool in_ro = false;
1415 3216 : for (size_t inner = 1; inner < shnum; ++inner)
1416 : {
1417 3216 : Elf_Scn *scn = elf_getscn (ebl->elf, inner);
1418 : /* This should not happen. */
1419 3216 : if (unlikely (scn == NULL))
1420 0 : error (EXIT_FAILURE, 0, gettext ("cannot get section: %s"),
1421 : elf_errmsg (-1));
1422 :
1423 : /* Get the section header. */
1424 : GElf_Shdr shdr_mem;
1425 3216 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
1426 3216 : if (unlikely (shdr == NULL))
1427 0 : error (EXIT_FAILURE, 0,
1428 0 : gettext ("cannot get section header: %s"),
1429 : elf_errmsg (-1));
1430 :
1431 3216 : if (shdr->sh_size > 0
1432 : /* Compare allocated sections by VMA, unallocated
1433 : sections by file offset. */
1434 6432 : && (shdr->sh_flags & SHF_ALLOC
1435 2386 : ? (shdr->sh_addr >= phdr->p_vaddr
1436 3902 : && (shdr->sh_addr + shdr->sh_size
1437 1516 : <= phdr->p_vaddr + phdr->p_memsz))
1438 830 : : (shdr->sh_offset >= phdr->p_offset
1439 1660 : && (shdr->sh_offset + shdr->sh_size
1440 830 : <= phdr->p_offset + phdr->p_filesz))))
1441 : {
1442 372 : if (has_relro && !in_relro
1443 266 : && shdr->sh_addr >= relro_from
1444 58 : && shdr->sh_addr + shdr->sh_size <= relro_to)
1445 : {
1446 38 : fputs_unlocked (" [RELRO:", stdout);
1447 38 : in_relro = true;
1448 : }
1449 334 : else if (has_relro && in_relro && shdr->sh_addr >= relro_to)
1450 : {
1451 10 : fputs_unlocked ("]", stdout);
1452 10 : in_relro = false;
1453 : }
1454 324 : else if (has_relro && in_relro
1455 96 : && shdr->sh_addr + shdr->sh_size > relro_to)
1456 0 : fputs_unlocked ("] <RELRO:", stdout);
1457 324 : else if (phdr->p_type == PT_LOAD && (phdr->p_flags & PF_W) == 0)
1458 : {
1459 176 : if (!in_ro)
1460 : {
1461 10 : fputs_unlocked (" [RO:", stdout);
1462 10 : in_ro = true;
1463 : }
1464 : }
1465 : else
1466 : {
1467 : /* Determine the segment this section is part of. */
1468 : size_t cnt2;
1469 : GElf_Phdr phdr2_mem;
1470 : GElf_Phdr *phdr2 = NULL;
1471 308 : for (cnt2 = 0; cnt2 < phnum; ++cnt2)
1472 : {
1473 456 : phdr2 = gelf_getphdr (ebl->elf, cnt2, &phdr2_mem);
1474 :
1475 456 : if (phdr2 != NULL && phdr2->p_type == PT_LOAD
1476 264 : && shdr->sh_addr >= phdr2->p_vaddr
1477 528 : && (shdr->sh_addr + shdr->sh_size
1478 264 : <= phdr2->p_vaddr + phdr2->p_memsz))
1479 : break;
1480 : }
1481 :
1482 148 : if (cnt2 < phnum)
1483 : {
1484 148 : if ((phdr2->p_flags & PF_W) == 0 && !in_ro)
1485 : {
1486 26 : fputs_unlocked (" [RO:", stdout);
1487 26 : in_ro = true;
1488 : }
1489 122 : else if ((phdr2->p_flags & PF_W) != 0 && in_ro)
1490 : {
1491 0 : fputs_unlocked ("]", stdout);
1492 0 : in_ro = false;
1493 : }
1494 : }
1495 : }
1496 :
1497 744 : printf (" %s",
1498 372 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name));
1499 :
1500 : /* Signal that this sectin is only partially covered. */
1501 372 : if (has_relro && in_relro
1502 134 : && shdr->sh_addr + shdr->sh_size > relro_to)
1503 : {
1504 0 : fputs_unlocked (">", stdout);
1505 0 : in_relro = false;
1506 : }
1507 : }
1508 : }
1509 90 : if (in_relro || in_ro)
1510 64 : fputs_unlocked ("]", stdout);
1511 :
1512 : /* Finish the line. */
1513 180 : fputc_unlocked ('\n', stdout);
1514 : }
1515 : }
1516 :
1517 :
1518 : static const char *
1519 : section_name (Ebl *ebl, GElf_Ehdr *ehdr, GElf_Shdr *shdr)
1520 : {
1521 324 : return elf_strptr (ebl->elf, ehdr->e_shstrndx, shdr->sh_name) ?: "???";
1522 : }
1523 :
1524 :
1525 : static void
1526 0 : handle_scngrp (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr)
1527 : {
1528 : /* Get the data of the section. */
1529 0 : Elf_Data *data = elf_getdata (scn, NULL);
1530 :
1531 0 : Elf_Scn *symscn = elf_getscn (ebl->elf, shdr->sh_link);
1532 : GElf_Shdr symshdr_mem;
1533 0 : GElf_Shdr *symshdr = gelf_getshdr (symscn, &symshdr_mem);
1534 0 : Elf_Data *symdata = elf_getdata (symscn, NULL);
1535 :
1536 0 : if (data == NULL || data->d_size < sizeof (Elf32_Word) || symshdr == NULL
1537 0 : || symdata == NULL)
1538 0 : return;
1539 :
1540 : /* Get the section header string table index. */
1541 : size_t shstrndx;
1542 0 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
1543 : error (EXIT_FAILURE, 0,
1544 0 : gettext ("cannot get section header string table index"));
1545 :
1546 0 : Elf32_Word *grpref = (Elf32_Word *) data->d_buf;
1547 :
1548 : GElf_Sym sym_mem;
1549 0 : GElf_Sym *sym = gelf_getsym (symdata, shdr->sh_info, &sym_mem);
1550 :
1551 0 : printf ((grpref[0] & GRP_COMDAT)
1552 0 : ? ngettext ("\
1553 : \nCOMDAT section group [%2zu] '%s' with signature '%s' contains %zu entry:\n",
1554 : "\
1555 : \nCOMDAT section group [%2zu] '%s' with signature '%s' contains %zu entries:\n",
1556 : data->d_size / sizeof (Elf32_Word) - 1)
1557 0 : : ngettext ("\
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),
1561 : elf_ndxscn (scn),
1562 0 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
1563 : (sym == NULL ? NULL
1564 0 : : elf_strptr (ebl->elf, symshdr->sh_link, sym->st_name))
1565 : ?: gettext ("<INVALID SYMBOL>"),
1566 0 : data->d_size / sizeof (Elf32_Word) - 1);
1567 :
1568 0 : for (size_t cnt = 1; cnt < data->d_size / sizeof (Elf32_Word); ++cnt)
1569 : {
1570 : GElf_Shdr grpshdr_mem;
1571 0 : GElf_Shdr *grpshdr = gelf_getshdr (elf_getscn (ebl->elf, grpref[cnt]),
1572 : &grpshdr_mem);
1573 :
1574 : const char *str;
1575 0 : printf (" [%2u] %s\n",
1576 : grpref[cnt],
1577 : grpshdr != NULL
1578 0 : && (str = elf_strptr (ebl->elf, shstrndx, grpshdr->sh_name))
1579 : ? str : gettext ("<INVALID SECTION>"));
1580 : }
1581 : }
1582 :
1583 :
1584 : static void
1585 32 : print_scngrp (Ebl *ebl)
1586 : {
1587 : /* Find all relocation sections and handle them. */
1588 32 : Elf_Scn *scn = NULL;
1589 :
1590 951 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
1591 : {
1592 : /* Handle the section if it is a symbol table. */
1593 : GElf_Shdr shdr_mem;
1594 887 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
1595 :
1596 887 : if (shdr != NULL && shdr->sh_type == SHT_GROUP)
1597 : {
1598 0 : if ((shdr->sh_flags & SHF_COMPRESSED) != 0)
1599 : {
1600 0 : if (elf_compress (scn, 0, 0) < 0)
1601 0 : printf ("WARNING: %s [%zd]\n",
1602 : gettext ("Couldn't uncompress section"),
1603 : elf_ndxscn (scn));
1604 0 : shdr = gelf_getshdr (scn, &shdr_mem);
1605 0 : if (unlikely (shdr == NULL))
1606 0 : error (EXIT_FAILURE, 0,
1607 0 : gettext ("cannot get section [%zd] header: %s"),
1608 : elf_ndxscn (scn),
1609 : elf_errmsg (-1));
1610 : }
1611 0 : handle_scngrp (ebl, scn, shdr);
1612 : }
1613 : }
1614 32 : }
1615 :
1616 :
1617 : static const struct flags
1618 : {
1619 : int mask;
1620 : const char *str;
1621 : } dt_flags[] =
1622 : {
1623 : { DF_ORIGIN, "ORIGIN" },
1624 : { DF_SYMBOLIC, "SYMBOLIC" },
1625 : { DF_TEXTREL, "TEXTREL" },
1626 : { DF_BIND_NOW, "BIND_NOW" },
1627 : { DF_STATIC_TLS, "STATIC_TLS" }
1628 : };
1629 : static const int ndt_flags = sizeof (dt_flags) / sizeof (dt_flags[0]);
1630 :
1631 : static const struct flags dt_flags_1[] =
1632 : {
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" },
1650 : };
1651 : static const int ndt_flags_1 = sizeof (dt_flags_1) / sizeof (dt_flags_1[0]);
1652 :
1653 : static const struct flags dt_feature_1[] =
1654 : {
1655 : { DTF_1_PARINIT, "PARINIT" },
1656 : { DTF_1_CONFEXP, "CONFEXP" }
1657 : };
1658 : static const int ndt_feature_1 = (sizeof (dt_feature_1)
1659 : / sizeof (dt_feature_1[0]));
1660 :
1661 : static const struct flags dt_posflag_1[] =
1662 : {
1663 : { DF_P1_LAZYLOAD, "LAZYLOAD" },
1664 : { DF_P1_GROUPPERM, "GROUPPERM" }
1665 : };
1666 : static const int ndt_posflag_1 = (sizeof (dt_posflag_1)
1667 : / sizeof (dt_posflag_1[0]));
1668 :
1669 :
1670 : static void
1671 10 : print_flags (int class, GElf_Xword d_val, const struct flags *flags,
1672 : int nflags)
1673 : {
1674 10 : bool first = true;
1675 : int cnt;
1676 :
1677 138 : for (cnt = 0; cnt < nflags; ++cnt)
1678 128 : if (d_val & flags[cnt].mask)
1679 : {
1680 22 : if (!first)
1681 : putchar_unlocked (' ');
1682 22 : fputs_unlocked (flags[cnt].str, stdout);
1683 22 : d_val &= ~flags[cnt].mask;
1684 22 : first = false;
1685 : }
1686 :
1687 10 : if (d_val != 0)
1688 : {
1689 10 : if (!first)
1690 : putchar_unlocked (' ');
1691 10 : printf ("%#0*" PRIx64, class == ELFCLASS32 ? 10 : 18, d_val);
1692 : }
1693 :
1694 10 : putchar_unlocked ('\n');
1695 10 : }
1696 :
1697 :
1698 : static void
1699 : print_dt_flags (int class, GElf_Xword d_val)
1700 : {
1701 1 : print_flags (class, d_val, dt_flags, ndt_flags);
1702 : }
1703 :
1704 :
1705 : static void
1706 : print_dt_flags_1 (int class, GElf_Xword d_val)
1707 : {
1708 7 : print_flags (class, d_val, dt_flags_1, ndt_flags_1);
1709 : }
1710 :
1711 :
1712 : static void
1713 : print_dt_feature_1 (int class, GElf_Xword d_val)
1714 : {
1715 1 : print_flags (class, d_val, dt_feature_1, ndt_feature_1);
1716 : }
1717 :
1718 :
1719 : static void
1720 : print_dt_posflag_1 (int class, GElf_Xword d_val)
1721 : {
1722 1 : print_flags (class, d_val, dt_posflag_1, ndt_posflag_1);
1723 : }
1724 :
1725 :
1726 : static void
1727 12 : handle_dynamic (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr)
1728 : {
1729 12 : int class = gelf_getclass (ebl->elf);
1730 : GElf_Shdr glink_mem;
1731 : GElf_Shdr *glink;
1732 : Elf_Data *data;
1733 : size_t cnt;
1734 : size_t shstrndx;
1735 : size_t sh_entsize;
1736 :
1737 : /* Get the data of the section. */
1738 12 : data = elf_getdata (scn, NULL);
1739 12 : if (data == NULL)
1740 0 : return;
1741 :
1742 : /* Get the section header string table index. */
1743 12 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
1744 : error (EXIT_FAILURE, 0,
1745 0 : gettext ("cannot get section header string table index"));
1746 :
1747 12 : sh_entsize = gelf_fsize (ebl->elf, ELF_T_DYN, 1, EV_CURRENT);
1748 :
1749 12 : glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link), &glink_mem);
1750 12 : if (glink == NULL)
1751 0 : error (EXIT_FAILURE, 0, gettext ("invalid sh_link value in section %zu"),
1752 : elf_ndxscn (scn));
1753 :
1754 48 : printf (ngettext ("\
1755 : \nDynamic segment contains %lu entry:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'\n",
1756 : "\
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 12 : (unsigned long int) (shdr->sh_size / sh_entsize),
1760 : class == ELFCLASS32 ? 10 : 18, shdr->sh_addr,
1761 : shdr->sh_offset,
1762 12 : (int) shdr->sh_link,
1763 12 : elf_strptr (ebl->elf, shstrndx, glink->sh_name));
1764 12 : fputs_unlocked (gettext (" Type Value\n"), stdout);
1765 :
1766 448 : for (cnt = 0; cnt < shdr->sh_size / sh_entsize; ++cnt)
1767 : {
1768 : GElf_Dyn dynmem;
1769 436 : GElf_Dyn *dyn = gelf_getdyn (data, cnt, &dynmem);
1770 436 : if (dyn == NULL)
1771 : break;
1772 :
1773 : char buf[64];
1774 872 : printf (" %-17s ",
1775 : ebl_dynamic_tag_name (ebl, dyn->d_tag, buf, sizeof (buf)));
1776 :
1777 436 : switch (dyn->d_tag)
1778 : {
1779 66 : case DT_NULL:
1780 : case DT_DEBUG:
1781 : case DT_BIND_NOW:
1782 : case DT_TEXTREL:
1783 : /* No further output. */
1784 66 : fputc_unlocked ('\n', stdout);
1785 : break;
1786 :
1787 52 : case DT_NEEDED:
1788 104 : printf (gettext ("Shared library: [%s]\n"),
1789 52 : elf_strptr (ebl->elf, shdr->sh_link, dyn->d_un.d_val));
1790 : break;
1791 :
1792 3 : case DT_SONAME:
1793 6 : printf (gettext ("Library soname: [%s]\n"),
1794 3 : elf_strptr (ebl->elf, shdr->sh_link, dyn->d_un.d_val));
1795 : break;
1796 :
1797 1 : case DT_RPATH:
1798 2 : printf (gettext ("Library rpath: [%s]\n"),
1799 1 : elf_strptr (ebl->elf, shdr->sh_link, dyn->d_un.d_val));
1800 : break;
1801 :
1802 2 : case DT_RUNPATH:
1803 4 : printf (gettext ("Library runpath: [%s]\n"),
1804 2 : elf_strptr (ebl->elf, shdr->sh_link, dyn->d_un.d_val));
1805 : break;
1806 :
1807 91 : case DT_PLTRELSZ:
1808 : case DT_RELASZ:
1809 : case DT_STRSZ:
1810 : case DT_RELSZ:
1811 : case DT_RELAENT:
1812 : case DT_SYMENT:
1813 : case DT_RELENT:
1814 : case DT_PLTPADSZ:
1815 : case DT_MOVEENT:
1816 : case DT_MOVESZ:
1817 : case DT_INIT_ARRAYSZ:
1818 : case DT_FINI_ARRAYSZ:
1819 : case DT_SYMINSZ:
1820 : case DT_SYMINENT:
1821 : case DT_GNU_CONFLICTSZ:
1822 : case DT_GNU_LIBLISTSZ:
1823 91 : printf (gettext ("%" PRId64 " (bytes)\n"), dyn->d_un.d_val);
1824 : break;
1825 :
1826 31 : case DT_VERDEFNUM:
1827 : case DT_VERNEEDNUM:
1828 : case DT_RELACOUNT:
1829 : case DT_RELCOUNT:
1830 31 : printf ("%" PRId64 "\n", dyn->d_un.d_val);
1831 : break;
1832 :
1833 12 : case DT_PLTREL:;
1834 12 : const char *tagname = ebl_dynamic_tag_name (ebl, dyn->d_un.d_val,
1835 : NULL, 0);
1836 12 : puts (tagname ?: "???");
1837 12 : break;
1838 :
1839 1 : case DT_FLAGS:
1840 1 : print_dt_flags (class, dyn->d_un.d_val);
1841 : break;
1842 :
1843 7 : case DT_FLAGS_1:
1844 7 : print_dt_flags_1 (class, dyn->d_un.d_val);
1845 : break;
1846 :
1847 1 : case DT_FEATURE_1:
1848 1 : print_dt_feature_1 (class, dyn->d_un.d_val);
1849 : break;
1850 :
1851 1 : case DT_POSFLAG_1:
1852 1 : print_dt_posflag_1 (class, dyn->d_un.d_val);
1853 : break;
1854 :
1855 168 : default:
1856 168 : printf ("%#0*" PRIx64 "\n",
1857 : class == ELFCLASS32 ? 10 : 18, dyn->d_un.d_val);
1858 : break;
1859 : }
1860 : }
1861 : }
1862 :
1863 :
1864 : /* Print the dynamic segment. */
1865 : static void
1866 34 : print_dynamic (Ebl *ebl)
1867 : {
1868 70 : for (size_t i = 0; i < phnum; ++i)
1869 : {
1870 : GElf_Phdr phdr_mem;
1871 48 : GElf_Phdr *phdr = gelf_getphdr (ebl->elf, i, &phdr_mem);
1872 :
1873 48 : if (phdr != NULL && phdr->p_type == PT_DYNAMIC)
1874 : {
1875 12 : Elf_Scn *scn = gelf_offscn (ebl->elf, phdr->p_offset);
1876 : GElf_Shdr shdr_mem;
1877 12 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
1878 12 : if (shdr != NULL && shdr->sh_type == SHT_DYNAMIC)
1879 12 : handle_dynamic (ebl, scn, shdr);
1880 : break;
1881 : }
1882 : }
1883 34 : }
1884 :
1885 :
1886 : /* Print relocations. */
1887 : static void
1888 33 : print_relocs (Ebl *ebl, GElf_Ehdr *ehdr)
1889 : {
1890 : /* Find all relocation sections and handle them. */
1891 33 : Elf_Scn *scn = NULL;
1892 :
1893 987 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
1894 : {
1895 : /* Handle the section if it is a symbol table. */
1896 : GElf_Shdr shdr_mem;
1897 921 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
1898 :
1899 921 : if (likely (shdr != NULL))
1900 : {
1901 921 : if (shdr->sh_type == SHT_REL)
1902 8 : handle_relocs_rel (ebl, ehdr, scn, shdr);
1903 913 : else if (shdr->sh_type == SHT_RELA)
1904 162 : handle_relocs_rela (ebl, ehdr, scn, shdr);
1905 : }
1906 : }
1907 33 : }
1908 :
1909 :
1910 : /* Handle a relocation section. */
1911 : static void
1912 8 : handle_relocs_rel (Ebl *ebl, GElf_Ehdr *ehdr, Elf_Scn *scn, GElf_Shdr *shdr)
1913 : {
1914 8 : int class = gelf_getclass (ebl->elf);
1915 8 : size_t sh_entsize = gelf_fsize (ebl->elf, ELF_T_REL, 1, EV_CURRENT);
1916 8 : int nentries = shdr->sh_size / sh_entsize;
1917 :
1918 : /* Get the data of the section. */
1919 8 : Elf_Data *data = elf_getdata (scn, NULL);
1920 8 : if (data == NULL)
1921 0 : return;
1922 :
1923 : /* Get the symbol table information. */
1924 8 : Elf_Scn *symscn = elf_getscn (ebl->elf, shdr->sh_link);
1925 : GElf_Shdr symshdr_mem;
1926 8 : GElf_Shdr *symshdr = gelf_getshdr (symscn, &symshdr_mem);
1927 8 : Elf_Data *symdata = elf_getdata (symscn, NULL);
1928 :
1929 : /* Get the section header of the section the relocations are for. */
1930 : GElf_Shdr destshdr_mem;
1931 8 : GElf_Shdr *destshdr = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_info),
1932 : &destshdr_mem);
1933 :
1934 8 : if (unlikely (symshdr == NULL || symdata == NULL || destshdr == NULL))
1935 : {
1936 0 : printf (gettext ("\nInvalid symbol table at offset %#0" PRIx64 "\n"),
1937 : shdr->sh_offset);
1938 : return;
1939 : }
1940 :
1941 : /* Search for the optional extended section index table. */
1942 8 : Elf_Data *xndxdata = NULL;
1943 8 : int xndxscnidx = elf_scnshndx (scn);
1944 8 : if (unlikely (xndxscnidx > 0))
1945 0 : xndxdata = elf_getdata (elf_getscn (ebl->elf, xndxscnidx), NULL);
1946 :
1947 : /* Get the section header string table index. */
1948 : size_t shstrndx;
1949 8 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
1950 : error (EXIT_FAILURE, 0,
1951 0 : gettext ("cannot get section header string table index"));
1952 :
1953 8 : if (shdr->sh_info != 0)
1954 24 : printf (ngettext ("\
1955 : \nRelocation section [%2zu] '%s' for section [%2u] '%s' at offset %#0" PRIx64 " contains %d entry:\n",
1956 : "\
1957 : \nRelocation section [%2zu] '%s' for section [%2u] '%s' at offset %#0" PRIx64 " contains %d entries:\n",
1958 : nentries),
1959 : elf_ndxscn (scn),
1960 8 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
1961 8 : (unsigned int) shdr->sh_info,
1962 8 : elf_strptr (ebl->elf, shstrndx, destshdr->sh_name),
1963 : shdr->sh_offset,
1964 : nentries);
1965 : else
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
1968 : name. */
1969 0 : printf (ngettext ("\
1970 : \nRelocation section [%2u] '%s' at offset %#0" PRIx64 " contains %d entry:\n",
1971 : "\
1972 : \nRelocation section [%2u] '%s' at offset %#0" PRIx64 " contains %d entries:\n",
1973 : nentries),
1974 0 : (unsigned int) elf_ndxscn (scn),
1975 0 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
1976 : shdr->sh_offset,
1977 : nentries);
1978 8 : fputs_unlocked (class == ELFCLASS32
1979 : ? gettext ("\
1980 : Offset Type Value Name\n")
1981 : : gettext ("\
1982 : Offset Type Value Name\n"),
1983 : stdout);
1984 :
1985 8 : int is_statically_linked = 0;
1986 33 : for (int cnt = 0; cnt < nentries; ++cnt)
1987 : {
1988 : GElf_Rel relmem;
1989 25 : GElf_Rel *rel = gelf_getrel (data, cnt, &relmem);
1990 25 : if (likely (rel != NULL))
1991 : {
1992 : char buf[128];
1993 : GElf_Sym symmem;
1994 : Elf32_Word xndx;
1995 25 : GElf_Sym *sym = gelf_getsymshndx (symdata, xndxdata,
1996 25 : GELF_R_SYM (rel->r_info),
1997 : &symmem, &xndx);
1998 25 : if (unlikely (sym == NULL))
1999 : {
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 0 : if (is_statically_linked == 0)
2004 : {
2005 : /* Find the program header and look for a PT_INTERP entry. */
2006 0 : is_statically_linked = -1;
2007 0 : if (ehdr->e_type == ET_EXEC)
2008 : {
2009 : is_statically_linked = 1;
2010 :
2011 0 : for (size_t inner = 0; inner < phnum; ++inner)
2012 : {
2013 : GElf_Phdr phdr_mem;
2014 0 : GElf_Phdr *phdr = gelf_getphdr (ebl->elf, inner,
2015 : &phdr_mem);
2016 0 : if (phdr != NULL && phdr->p_type == PT_INTERP)
2017 : {
2018 0 : is_statically_linked = -1;
2019 0 : break;
2020 : }
2021 : }
2022 : }
2023 : }
2024 :
2025 0 : if (is_statically_linked > 0 && shdr->sh_link == 0)
2026 0 : printf ("\
2027 : %#0*" PRIx64 " %-20s %*s %s\n",
2028 : class == ELFCLASS32 ? 10 : 18, rel->r_offset,
2029 0 : ebl_reloc_type_check (ebl, GELF_R_TYPE (rel->r_info))
2030 : /* Avoid the leading R_ which isn't carrying any
2031 : information. */
2032 0 : ? 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 0 : elf_strptr (ebl->elf, shstrndx, destshdr->sh_name));
2037 : else
2038 0 : printf (" %#0*" PRIx64 " %-20s <%s %ld>\n",
2039 : class == ELFCLASS32 ? 10 : 18, rel->r_offset,
2040 0 : ebl_reloc_type_check (ebl, GELF_R_TYPE (rel->r_info))
2041 : /* Avoid the leading R_ which isn't carrying any
2042 : information. */
2043 0 : ? 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 0 : (long int) GELF_R_SYM (rel->r_info));
2048 : }
2049 25 : else if (GELF_ST_TYPE (sym->st_info) != STT_SECTION)
2050 68 : printf (" %#0*" PRIx64 " %-20s %#0*" PRIx64 " %s\n",
2051 : class == ELFCLASS32 ? 10 : 18, rel->r_offset,
2052 17 : likely (ebl_reloc_type_check (ebl,
2053 : GELF_R_TYPE (rel->r_info)))
2054 : /* Avoid the leading R_ which isn't carrying any
2055 : information. */
2056 17 : ? 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 34 : elf_strptr (ebl->elf, symshdr->sh_link, sym->st_name));
2061 : else
2062 : {
2063 : /* This is a relocation against a STT_SECTION symbol. */
2064 : GElf_Shdr secshdr_mem;
2065 : GElf_Shdr *secshdr;
2066 8 : secshdr = gelf_getshdr (elf_getscn (ebl->elf,
2067 8 : sym->st_shndx == SHN_XINDEX
2068 0 : ? xndx : sym->st_shndx),
2069 : &secshdr_mem);
2070 :
2071 8 : if (unlikely (secshdr == NULL))
2072 0 : printf (" %#0*" PRIx64 " %-20s <%s %ld>\n",
2073 : class == ELFCLASS32 ? 10 : 18, rel->r_offset,
2074 0 : ebl_reloc_type_check (ebl, GELF_R_TYPE (rel->r_info))
2075 : /* Avoid the leading R_ which isn't carrying any
2076 : information. */
2077 0 : ? 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 0 : (long int) (sym->st_shndx == SHN_XINDEX
2082 0 : ? xndx : sym->st_shndx));
2083 : else
2084 24 : printf (" %#0*" PRIx64 " %-20s %#0*" PRIx64 " %s\n",
2085 : class == ELFCLASS32 ? 10 : 18, rel->r_offset,
2086 8 : ebl_reloc_type_check (ebl, GELF_R_TYPE (rel->r_info))
2087 : /* Avoid the leading R_ which isn't carrying any
2088 : information. */
2089 8 : ? 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 8 : elf_strptr (ebl->elf, shstrndx, secshdr->sh_name));
2094 : }
2095 : }
2096 : }
2097 : }
2098 :
2099 :
2100 : /* Handle a relocation section. */
2101 : static void
2102 162 : handle_relocs_rela (Ebl *ebl, GElf_Ehdr *ehdr, Elf_Scn *scn, GElf_Shdr *shdr)
2103 : {
2104 162 : int class = gelf_getclass (ebl->elf);
2105 162 : size_t sh_entsize = gelf_fsize (ebl->elf, ELF_T_RELA, 1, EV_CURRENT);
2106 162 : int nentries = shdr->sh_size / sh_entsize;
2107 :
2108 : /* Get the data of the section. */
2109 162 : Elf_Data *data = elf_getdata (scn, NULL);
2110 162 : if (data == NULL)
2111 0 : return;
2112 :
2113 : /* Get the symbol table information. */
2114 162 : Elf_Scn *symscn = elf_getscn (ebl->elf, shdr->sh_link);
2115 : GElf_Shdr symshdr_mem;
2116 162 : GElf_Shdr *symshdr = gelf_getshdr (symscn, &symshdr_mem);
2117 162 : Elf_Data *symdata = elf_getdata (symscn, NULL);
2118 :
2119 : /* Get the section header of the section the relocations are for. */
2120 : GElf_Shdr destshdr_mem;
2121 162 : GElf_Shdr *destshdr = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_info),
2122 : &destshdr_mem);
2123 :
2124 162 : if (unlikely (symshdr == NULL || symdata == NULL || destshdr == NULL))
2125 : {
2126 0 : printf (gettext ("\nInvalid symbol table at offset %#0" PRIx64 "\n"),
2127 : shdr->sh_offset);
2128 : return;
2129 : }
2130 :
2131 : /* Search for the optional extended section index table. */
2132 162 : Elf_Data *xndxdata = NULL;
2133 162 : int xndxscnidx = elf_scnshndx (scn);
2134 162 : if (unlikely (xndxscnidx > 0))
2135 0 : xndxdata = elf_getdata (elf_getscn (ebl->elf, xndxscnidx), NULL);
2136 :
2137 : /* Get the section header string table index. */
2138 : size_t shstrndx;
2139 162 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
2140 : error (EXIT_FAILURE, 0,
2141 0 : gettext ("cannot get section header string table index"));
2142 :
2143 162 : if (shdr->sh_info != 0)
2144 456 : printf (ngettext ("\
2145 : \nRelocation section [%2zu] '%s' for section [%2u] '%s' at offset %#0" PRIx64 " contains %d entry:\n",
2146 : "\
2147 : \nRelocation section [%2zu] '%s' for section [%2u] '%s' at offset %#0" PRIx64 " contains %d entries:\n",
2148 : nentries),
2149 : elf_ndxscn (scn),
2150 152 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
2151 152 : (unsigned int) shdr->sh_info,
2152 152 : elf_strptr (ebl->elf, shstrndx, destshdr->sh_name),
2153 : shdr->sh_offset,
2154 : nentries);
2155 : else
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
2158 : name. */
2159 30 : printf (ngettext ("\
2160 : \nRelocation section [%2u] '%s' at offset %#0" PRIx64 " contains %d entry:\n",
2161 : "\
2162 : \nRelocation section [%2u] '%s' at offset %#0" PRIx64 " contains %d entries:\n",
2163 : nentries),
2164 10 : (unsigned int) elf_ndxscn (scn),
2165 10 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
2166 : shdr->sh_offset,
2167 : nentries);
2168 162 : fputs_unlocked (class == ELFCLASS32
2169 : ? gettext ("\
2170 : Offset Type Value Addend Name\n")
2171 : : gettext ("\
2172 : Offset Type Value Addend Name\n"),
2173 : stdout);
2174 :
2175 162 : int is_statically_linked = 0;
2176 34642 : for (int cnt = 0; cnt < nentries; ++cnt)
2177 : {
2178 : GElf_Rela relmem;
2179 34480 : GElf_Rela *rel = gelf_getrela (data, cnt, &relmem);
2180 34480 : if (likely (rel != NULL))
2181 : {
2182 : char buf[64];
2183 : GElf_Sym symmem;
2184 : Elf32_Word xndx;
2185 34480 : GElf_Sym *sym = gelf_getsymshndx (symdata, xndxdata,
2186 34480 : GELF_R_SYM (rel->r_info),
2187 : &symmem, &xndx);
2188 :
2189 34480 : if (unlikely (sym == NULL))
2190 : {
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 0 : if (is_statically_linked == 0)
2195 : {
2196 : /* Find the program header and look for a PT_INTERP entry. */
2197 0 : is_statically_linked = -1;
2198 0 : if (ehdr->e_type == ET_EXEC)
2199 : {
2200 : is_statically_linked = 1;
2201 :
2202 0 : for (size_t inner = 0; inner < phnum; ++inner)
2203 : {
2204 : GElf_Phdr phdr_mem;
2205 0 : GElf_Phdr *phdr = gelf_getphdr (ebl->elf, inner,
2206 : &phdr_mem);
2207 0 : if (phdr != NULL && phdr->p_type == PT_INTERP)
2208 : {
2209 0 : is_statically_linked = -1;
2210 0 : break;
2211 : }
2212 : }
2213 : }
2214 : }
2215 :
2216 0 : if (is_statically_linked > 0 && shdr->sh_link == 0)
2217 0 : printf ("\
2218 : %#0*" PRIx64 " %-15s %*s %#6" PRIx64 " %s\n",
2219 : class == ELFCLASS32 ? 10 : 18, rel->r_offset,
2220 0 : ebl_reloc_type_check (ebl, GELF_R_TYPE (rel->r_info))
2221 : /* Avoid the leading R_ which isn't carrying any
2222 : information. */
2223 0 : ? 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, "",
2227 : rel->r_addend,
2228 0 : elf_strptr (ebl->elf, shstrndx, destshdr->sh_name));
2229 : else
2230 0 : printf (" %#0*" PRIx64 " %-15s <%s %ld>\n",
2231 : class == ELFCLASS32 ? 10 : 18, rel->r_offset,
2232 0 : ebl_reloc_type_check (ebl, GELF_R_TYPE (rel->r_info))
2233 : /* Avoid the leading R_ which isn't carrying any
2234 : information. */
2235 0 : ? 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 0 : (long int) GELF_R_SYM (rel->r_info));
2240 : }
2241 34480 : else if (GELF_ST_TYPE (sym->st_info) != STT_SECTION)
2242 79212 : printf ("\
2243 : %#0*" PRIx64 " %-15s %#0*" PRIx64 " %+6" PRId64 " %s\n",
2244 : class == ELFCLASS32 ? 10 : 18, rel->r_offset,
2245 19803 : likely (ebl_reloc_type_check (ebl,
2246 : GELF_R_TYPE (rel->r_info)))
2247 : /* Avoid the leading R_ which isn't carrying any
2248 : information. */
2249 19803 : ? 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,
2253 : rel->r_addend,
2254 39606 : elf_strptr (ebl->elf, symshdr->sh_link, sym->st_name));
2255 : else
2256 : {
2257 : /* This is a relocation against a STT_SECTION symbol. */
2258 : GElf_Shdr secshdr_mem;
2259 : GElf_Shdr *secshdr;
2260 14677 : secshdr = gelf_getshdr (elf_getscn (ebl->elf,
2261 14677 : sym->st_shndx == SHN_XINDEX
2262 0 : ? xndx : sym->st_shndx),
2263 : &secshdr_mem);
2264 :
2265 14677 : if (unlikely (secshdr == NULL))
2266 0 : printf (" %#0*" PRIx64 " %-15s <%s %ld>\n",
2267 : class == ELFCLASS32 ? 10 : 18, rel->r_offset,
2268 0 : ebl_reloc_type_check (ebl, GELF_R_TYPE (rel->r_info))
2269 : /* Avoid the leading R_ which isn't carrying any
2270 : information. */
2271 0 : ? 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 0 : (long int) (sym->st_shndx == SHN_XINDEX
2276 0 : ? xndx : sym->st_shndx));
2277 : else
2278 44031 : printf ("\
2279 : %#0*" PRIx64 " %-15s %#0*" PRIx64 " %+6" PRId64 " %s\n",
2280 : class == ELFCLASS32 ? 10 : 18, rel->r_offset,
2281 14677 : ebl_reloc_type_check (ebl, GELF_R_TYPE (rel->r_info))
2282 : /* Avoid the leading R_ which isn't carrying any
2283 : information. */
2284 14677 : ? 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,
2288 : rel->r_addend,
2289 14677 : elf_strptr (ebl->elf, shstrndx, secshdr->sh_name));
2290 : }
2291 : }
2292 : }
2293 : }
2294 :
2295 :
2296 : /* Print the program header. */
2297 : static void
2298 92 : print_symtab (Ebl *ebl, int type)
2299 : {
2300 : /* Find the symbol table(s). For this we have to search through the
2301 : section table. */
2302 92 : Elf_Scn *scn = NULL;
2303 :
2304 2716 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
2305 : {
2306 : /* Handle the section if it is a symbol table. */
2307 : GElf_Shdr shdr_mem;
2308 2532 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
2309 :
2310 2532 : if (shdr != NULL && shdr->sh_type == (GElf_Word) type)
2311 : {
2312 56 : if (symbol_table_section != NULL)
2313 : {
2314 : /* Get the section header string table index. */
2315 : size_t shstrndx;
2316 : const char *sname;
2317 4 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
2318 : error (EXIT_FAILURE, 0,
2319 0 : gettext ("cannot get section header string table index"));
2320 4 : sname = elf_strptr (ebl->elf, shstrndx, shdr->sh_name);
2321 4 : if (sname == NULL || strcmp (sname, symbol_table_section) != 0)
2322 2 : continue;
2323 : }
2324 :
2325 54 : if ((shdr->sh_flags & SHF_COMPRESSED) != 0)
2326 : {
2327 0 : if (elf_compress (scn, 0, 0) < 0)
2328 0 : printf ("WARNING: %s [%zd]\n",
2329 : gettext ("Couldn't uncompress section"),
2330 : elf_ndxscn (scn));
2331 0 : shdr = gelf_getshdr (scn, &shdr_mem);
2332 0 : if (unlikely (shdr == NULL))
2333 0 : error (EXIT_FAILURE, 0,
2334 0 : gettext ("cannot get section [%zd] header: %s"),
2335 : elf_ndxscn (scn), elf_errmsg (-1));
2336 : }
2337 54 : handle_symtab (ebl, scn, shdr);
2338 : }
2339 : }
2340 92 : }
2341 :
2342 :
2343 : static void
2344 54 : handle_symtab (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr)
2345 : {
2346 54 : Elf_Data *versym_data = NULL;
2347 54 : Elf_Data *verneed_data = NULL;
2348 54 : Elf_Data *verdef_data = NULL;
2349 54 : Elf_Data *xndx_data = NULL;
2350 54 : int class = gelf_getclass (ebl->elf);
2351 54 : Elf32_Word verneed_stridx = 0;
2352 54 : Elf32_Word verdef_stridx = 0;
2353 :
2354 : /* Get the data of the section. */
2355 54 : Elf_Data *data = elf_getdata (scn, NULL);
2356 54 : if (data == NULL)
2357 0 : return;
2358 :
2359 : /* Find out whether we have other sections we might need. */
2360 : Elf_Scn *runscn = NULL;
2361 1646 : while ((runscn = elf_nextscn (ebl->elf, runscn)) != NULL)
2362 : {
2363 : GElf_Shdr runshdr_mem;
2364 1592 : GElf_Shdr *runshdr = gelf_getshdr (runscn, &runshdr_mem);
2365 :
2366 1592 : if (likely (runshdr != NULL))
2367 : {
2368 1592 : if (runshdr->sh_type == SHT_GNU_versym
2369 27 : && runshdr->sh_link == elf_ndxscn (scn))
2370 : /* Bingo, found the version information. Now get the data. */
2371 17 : versym_data = elf_getdata (runscn, NULL);
2372 1575 : else if (runshdr->sh_type == SHT_GNU_verneed)
2373 : {
2374 : /* This is the information about the needed versions. */
2375 27 : verneed_data = elf_getdata (runscn, NULL);
2376 27 : verneed_stridx = runshdr->sh_link;
2377 : }
2378 1548 : else if (runshdr->sh_type == SHT_GNU_verdef)
2379 : {
2380 : /* This is the information about the defined versions. */
2381 8 : verdef_data = elf_getdata (runscn, NULL);
2382 8 : verdef_stridx = runshdr->sh_link;
2383 : }
2384 1540 : else if (runshdr->sh_type == SHT_SYMTAB_SHNDX
2385 0 : && runshdr->sh_link == elf_ndxscn (scn))
2386 : /* Extended section index. */
2387 0 : xndx_data = elf_getdata (runscn, NULL);
2388 : }
2389 : }
2390 :
2391 : /* Get the section header string table index. */
2392 : size_t shstrndx;
2393 54 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
2394 : error (EXIT_FAILURE, 0,
2395 0 : gettext ("cannot get section header string table index"));
2396 :
2397 : GElf_Shdr glink_mem;
2398 54 : GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
2399 : &glink_mem);
2400 54 : if (glink == NULL)
2401 0 : error (EXIT_FAILURE, 0, gettext ("invalid sh_link value in section %zu"),
2402 : elf_ndxscn (scn));
2403 :
2404 : /* Now we can compute the number of entries in the section. */
2405 108 : unsigned int nsyms = data->d_size / (class == ELFCLASS32
2406 : ? sizeof (Elf32_Sym)
2407 54 : : sizeof (Elf64_Sym));
2408 :
2409 162 : printf (ngettext ("\nSymbol table [%2u] '%s' contains %u entry:\n",
2410 : "\nSymbol table [%2u] '%s' contains %u entries:\n",
2411 : nsyms),
2412 54 : (unsigned int) elf_ndxscn (scn),
2413 54 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name), nsyms);
2414 162 : printf (ngettext (" %lu local symbol String table: [%2u] '%s'\n",
2415 : " %lu local symbols String table: [%2u] '%s'\n",
2416 : shdr->sh_info),
2417 54 : (unsigned long int) shdr->sh_info,
2418 54 : (unsigned int) shdr->sh_link,
2419 54 : elf_strptr (ebl->elf, shstrndx, glink->sh_name));
2420 :
2421 54 : fputs_unlocked (class == ELFCLASS32
2422 : ? gettext ("\
2423 : Num: Value Size Type Bind Vis Ndx Name\n")
2424 : : gettext ("\
2425 : Num: Value Size Type Bind Vis Ndx Name\n"),
2426 : stdout);
2427 :
2428 14475 : for (unsigned int cnt = 0; cnt < nsyms; ++cnt)
2429 : {
2430 : char typebuf[64];
2431 : char bindbuf[64];
2432 : char scnbuf[64];
2433 : Elf32_Word xndx;
2434 : GElf_Sym sym_mem;
2435 14421 : GElf_Sym *sym = gelf_getsymshndx (data, xndx_data, cnt, &sym_mem, &xndx);
2436 :
2437 14421 : if (unlikely (sym == NULL))
2438 0 : continue;
2439 :
2440 : /* Determine the real section index. */
2441 14421 : if (likely (sym->st_shndx != SHN_XINDEX))
2442 14421 : xndx = sym->st_shndx;
2443 :
2444 115368 : printf (gettext ("\
2445 : %5u: %0*" PRIx64 " %6" PRId64 " %-7s %-6s %-9s %6s %s"),
2446 : cnt,
2447 : class == ELFCLASS32 ? 8 : 16,
2448 : sym->st_value,
2449 : sym->st_size,
2450 14421 : ebl_symbol_type_name (ebl, GELF_ST_TYPE (sym->st_info),
2451 : typebuf, sizeof (typebuf)),
2452 14421 : ebl_symbol_binding_name (ebl, GELF_ST_BIND (sym->st_info),
2453 : bindbuf, sizeof (bindbuf)),
2454 14421 : get_visibility_type (GELF_ST_VISIBILITY (sym->st_other)),
2455 14421 : ebl_section_name (ebl, sym->st_shndx, xndx, scnbuf,
2456 : sizeof (scnbuf), NULL, shnum),
2457 28842 : elf_strptr (ebl->elf, shdr->sh_link, sym->st_name));
2458 :
2459 14421 : if (versym_data != NULL)
2460 : {
2461 : /* Get the version information. */
2462 : GElf_Versym versym_mem;
2463 1394 : GElf_Versym *versym = gelf_getversym (versym_data, cnt, &versym_mem);
2464 :
2465 1394 : if (versym != NULL && ((*versym & 0x8000) != 0 || *versym > 1))
2466 : {
2467 1176 : bool is_nobits = false;
2468 1176 : bool check_def = xndx != SHN_UNDEF;
2469 :
2470 1176 : if (xndx < SHN_LORESERVE || sym->st_shndx == SHN_XINDEX)
2471 : {
2472 : GElf_Shdr symshdr_mem;
2473 1142 : GElf_Shdr *symshdr =
2474 1142 : gelf_getshdr (elf_getscn (ebl->elf, xndx), &symshdr_mem);
2475 :
2476 1142 : is_nobits = (symshdr != NULL
2477 1142 : && symshdr->sh_type == SHT_NOBITS);
2478 : }
2479 :
2480 1176 : if (is_nobits || ! check_def)
2481 : {
2482 : /* We must test both. */
2483 : GElf_Vernaux vernaux_mem;
2484 804 : GElf_Vernaux *vernaux = NULL;
2485 804 : size_t vn_offset = 0;
2486 :
2487 : GElf_Verneed verneed_mem;
2488 804 : GElf_Verneed *verneed = gelf_getverneed (verneed_data, 0,
2489 : &verneed_mem);
2490 4012 : while (verneed != NULL)
2491 : {
2492 3208 : size_t vna_offset = vn_offset;
2493 :
2494 3208 : vernaux = gelf_getvernaux (verneed_data,
2495 3208 : vna_offset += verneed->vn_aux,
2496 : &vernaux_mem);
2497 9729 : while (vernaux != NULL
2498 6521 : && vernaux->vna_other != *versym
2499 5717 : && vernaux->vna_next != 0)
2500 : {
2501 : /* Update the offset. */
2502 3313 : vna_offset += vernaux->vna_next;
2503 :
2504 3313 : vernaux = (vernaux->vna_next == 0
2505 : ? NULL
2506 3313 : : gelf_getvernaux (verneed_data,
2507 : vna_offset,
2508 : &vernaux_mem));
2509 : }
2510 :
2511 : /* Check whether we found the version. */
2512 3208 : if (vernaux != NULL && vernaux->vna_other == *versym)
2513 : /* Found it. */
2514 : break;
2515 :
2516 2404 : vn_offset += verneed->vn_next;
2517 : verneed = (verneed->vn_next == 0
2518 : ? NULL
2519 2404 : : gelf_getverneed (verneed_data, vn_offset,
2520 : &verneed_mem));
2521 : }
2522 :
2523 804 : if (vernaux != NULL && vernaux->vna_other == *versym)
2524 : {
2525 2412 : printf ("@%s (%u)",
2526 : elf_strptr (ebl->elf, verneed_stridx,
2527 804 : vernaux->vna_name),
2528 : (unsigned int) vernaux->vna_other);
2529 804 : check_def = 0;
2530 : }
2531 0 : else if (unlikely (! is_nobits))
2532 0 : error (0, 0, gettext ("bad dynamic symbol"));
2533 : else
2534 : check_def = 1;
2535 : }
2536 :
2537 372 : if (check_def && *versym != 0x8001)
2538 : {
2539 : /* We must test both. */
2540 372 : size_t vd_offset = 0;
2541 :
2542 : GElf_Verdef verdef_mem;
2543 372 : GElf_Verdef *verdef = gelf_getverdef (verdef_data, 0,
2544 : &verdef_mem);
2545 2742 : while (verdef != NULL)
2546 : {
2547 2370 : if (verdef->vd_ndx == (*versym & 0x7fff))
2548 : /* Found the definition. */
2549 : break;
2550 :
2551 1998 : vd_offset += verdef->vd_next;
2552 : verdef = (verdef->vd_next == 0
2553 : ? NULL
2554 1998 : : gelf_getverdef (verdef_data, vd_offset,
2555 : &verdef_mem));
2556 : }
2557 :
2558 372 : if (verdef != NULL)
2559 : {
2560 : GElf_Verdaux verdaux_mem;
2561 372 : GElf_Verdaux *verdaux
2562 372 : = gelf_getverdaux (verdef_data,
2563 372 : vd_offset + verdef->vd_aux,
2564 : &verdaux_mem);
2565 :
2566 372 : if (verdaux != NULL)
2567 372 : printf ((*versym & 0x8000) ? "@%s" : "@@%s",
2568 : elf_strptr (ebl->elf, verdef_stridx,
2569 372 : verdaux->vda_name));
2570 : }
2571 : }
2572 : }
2573 : }
2574 :
2575 14421 : putchar_unlocked ('\n');
2576 : }
2577 : }
2578 :
2579 :
2580 : /* Print version information. */
2581 : static void
2582 32 : print_verinfo (Ebl *ebl)
2583 : {
2584 : /* Find the version information sections. For this we have to
2585 : search through the section table. */
2586 32 : Elf_Scn *scn = NULL;
2587 :
2588 951 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
2589 : {
2590 : /* Handle the section if it is part of the versioning handling. */
2591 : GElf_Shdr shdr_mem;
2592 887 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
2593 :
2594 887 : if (likely (shdr != NULL))
2595 : {
2596 887 : if (shdr->sh_type == SHT_GNU_verneed)
2597 10 : handle_verneed (ebl, scn, shdr);
2598 877 : else if (shdr->sh_type == SHT_GNU_verdef)
2599 4 : handle_verdef (ebl, scn, shdr);
2600 873 : else if (shdr->sh_type == SHT_GNU_versym)
2601 10 : handle_versym (ebl, scn, shdr);
2602 : }
2603 : }
2604 32 : }
2605 :
2606 :
2607 : static const char *
2608 120 : get_ver_flags (unsigned int flags)
2609 : {
2610 : static char buf[32];
2611 : char *endp;
2612 :
2613 120 : if (flags == 0)
2614 115 : return gettext ("none");
2615 :
2616 5 : if (flags & VER_FLG_BASE)
2617 4 : endp = stpcpy (buf, "BASE ");
2618 : else
2619 : endp = buf;
2620 :
2621 5 : if (flags & VER_FLG_WEAK)
2622 : {
2623 1 : if (endp != buf)
2624 0 : endp = stpcpy (endp, "| ");
2625 :
2626 1 : endp = stpcpy (endp, "WEAK ");
2627 : }
2628 :
2629 5 : if (unlikely (flags & ~(VER_FLG_BASE | VER_FLG_WEAK)))
2630 : {
2631 0 : strncpy (endp, gettext ("| <unknown>"), buf + sizeof (buf) - endp);
2632 0 : buf[sizeof (buf) - 1] = '\0';
2633 : }
2634 :
2635 : return buf;
2636 : }
2637 :
2638 :
2639 : static void
2640 10 : handle_verneed (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr)
2641 : {
2642 10 : int class = gelf_getclass (ebl->elf);
2643 :
2644 : /* Get the data of the section. */
2645 10 : Elf_Data *data = elf_getdata (scn, NULL);
2646 10 : if (data == NULL)
2647 0 : return;
2648 :
2649 : /* Get the section header string table index. */
2650 : size_t shstrndx;
2651 10 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
2652 : error (EXIT_FAILURE, 0,
2653 0 : gettext ("cannot get section header string table index"));
2654 :
2655 : GElf_Shdr glink_mem;
2656 10 : GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
2657 : &glink_mem);
2658 10 : if (glink == NULL)
2659 0 : error (EXIT_FAILURE, 0, gettext ("invalid sh_link value in section %zu"),
2660 : elf_ndxscn (scn));
2661 :
2662 50 : printf (ngettext ("\
2663 : \nVersion needs section [%2u] '%s' contains %d entry:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'\n",
2664 : "\
2665 : \nVersion needs section [%2u] '%s' contains %d entries:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'\n",
2666 : shdr->sh_info),
2667 10 : (unsigned int) elf_ndxscn (scn),
2668 10 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name), shdr->sh_info,
2669 : class == ELFCLASS32 ? 10 : 18, shdr->sh_addr,
2670 : shdr->sh_offset,
2671 10 : (unsigned int) shdr->sh_link,
2672 10 : elf_strptr (ebl->elf, shstrndx, glink->sh_name));
2673 :
2674 10 : unsigned int offset = 0;
2675 47 : for (int cnt = shdr->sh_info; --cnt >= 0; )
2676 : {
2677 : /* Get the data at the next offset. */
2678 : GElf_Verneed needmem;
2679 37 : GElf_Verneed *need = gelf_getverneed (data, offset, &needmem);
2680 37 : if (unlikely (need == NULL))
2681 : break;
2682 :
2683 185 : printf (gettext (" %#06x: Version: %hu File: %s Cnt: %hu\n"),
2684 37 : offset, (unsigned short int) need->vn_version,
2685 74 : elf_strptr (ebl->elf, shdr->sh_link, need->vn_file),
2686 37 : (unsigned short int) need->vn_cnt);
2687 :
2688 37 : unsigned int auxoffset = offset + need->vn_aux;
2689 119 : for (int cnt2 = need->vn_cnt; --cnt2 >= 0; )
2690 : {
2691 : GElf_Vernaux auxmem;
2692 82 : GElf_Vernaux *aux = gelf_getvernaux (data, auxoffset, &auxmem);
2693 82 : if (unlikely (aux == NULL))
2694 : break;
2695 :
2696 410 : printf (gettext (" %#06x: Name: %s Flags: %s Version: %hu\n"),
2697 : auxoffset,
2698 164 : elf_strptr (ebl->elf, shdr->sh_link, aux->vna_name),
2699 82 : get_ver_flags (aux->vna_flags),
2700 82 : (unsigned short int) aux->vna_other);
2701 :
2702 82 : if (aux->vna_next == 0)
2703 : break;
2704 :
2705 45 : auxoffset += aux->vna_next;
2706 : }
2707 :
2708 : /* Find the next offset. */
2709 37 : if (need->vn_next == 0)
2710 : break;
2711 :
2712 27 : offset += need->vn_next;
2713 : }
2714 : }
2715 :
2716 :
2717 : static void
2718 4 : handle_verdef (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr)
2719 : {
2720 : /* Get the data of the section. */
2721 4 : Elf_Data *data = elf_getdata (scn, NULL);
2722 4 : if (data == NULL)
2723 0 : return;
2724 :
2725 : /* Get the section header string table index. */
2726 : size_t shstrndx;
2727 4 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
2728 : error (EXIT_FAILURE, 0,
2729 0 : gettext ("cannot get section header string table index"));
2730 :
2731 : GElf_Shdr glink_mem;
2732 4 : GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
2733 : &glink_mem);
2734 4 : if (glink == NULL)
2735 0 : error (EXIT_FAILURE, 0, gettext ("invalid sh_link value in section %zu"),
2736 : elf_ndxscn (scn));
2737 :
2738 4 : int class = gelf_getclass (ebl->elf);
2739 20 : printf (ngettext ("\
2740 : \nVersion definition section [%2u] '%s' contains %d entry:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'\n",
2741 : "\
2742 : \nVersion definition section [%2u] '%s' contains %d entries:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'\n",
2743 : shdr->sh_info),
2744 4 : (unsigned int) elf_ndxscn (scn),
2745 4 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
2746 : shdr->sh_info,
2747 : class == ELFCLASS32 ? 10 : 18, shdr->sh_addr,
2748 : shdr->sh_offset,
2749 4 : (unsigned int) shdr->sh_link,
2750 4 : elf_strptr (ebl->elf, shstrndx, glink->sh_name));
2751 :
2752 4 : unsigned int offset = 0;
2753 42 : for (int cnt = shdr->sh_info; --cnt >= 0; )
2754 : {
2755 : /* Get the data at the next offset. */
2756 : GElf_Verdef defmem;
2757 38 : GElf_Verdef *def = gelf_getverdef (data, offset, &defmem);
2758 38 : if (unlikely (def == NULL))
2759 : break;
2760 :
2761 38 : unsigned int auxoffset = offset + def->vd_aux;
2762 : GElf_Verdaux auxmem;
2763 38 : GElf_Verdaux *aux = gelf_getverdaux (data, auxoffset, &auxmem);
2764 38 : if (unlikely (aux == NULL))
2765 : break;
2766 :
2767 266 : printf (gettext ("\
2768 : %#06x: Version: %hd Flags: %s Index: %hd Cnt: %hd Name: %s\n"),
2769 38 : offset, def->vd_version,
2770 38 : get_ver_flags (def->vd_flags),
2771 38 : def->vd_ndx,
2772 38 : def->vd_cnt,
2773 76 : elf_strptr (ebl->elf, shdr->sh_link, aux->vda_name));
2774 :
2775 38 : auxoffset += aux->vda_next;
2776 38 : for (int cnt2 = 1; cnt2 < def->vd_cnt; ++cnt2)
2777 : {
2778 30 : aux = gelf_getverdaux (data, auxoffset, &auxmem);
2779 30 : if (unlikely (aux == NULL))
2780 : break;
2781 :
2782 90 : printf (gettext (" %#06x: Parent %d: %s\n"),
2783 : auxoffset, cnt2,
2784 60 : elf_strptr (ebl->elf, shdr->sh_link, aux->vda_name));
2785 :
2786 30 : if (aux->vda_next == 0)
2787 : break;
2788 :
2789 0 : auxoffset += aux->vda_next;
2790 : }
2791 :
2792 : /* Find the next offset. */
2793 38 : if (def->vd_next == 0)
2794 : break;
2795 34 : offset += def->vd_next;
2796 : }
2797 : }
2798 :
2799 :
2800 : static void
2801 10 : handle_versym (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr)
2802 : {
2803 10 : int class = gelf_getclass (ebl->elf);
2804 : const char **vername;
2805 : const char **filename;
2806 :
2807 : /* Get the data of the section. */
2808 10 : Elf_Data *data = elf_getdata (scn, NULL);
2809 10 : if (data == NULL)
2810 0 : return;
2811 :
2812 : /* Get the section header string table index. */
2813 : size_t shstrndx;
2814 10 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
2815 : error (EXIT_FAILURE, 0,
2816 0 : gettext ("cannot get section header string table index"));
2817 :
2818 : /* We have to find the version definition section and extract the
2819 : version names. */
2820 : Elf_Scn *defscn = NULL;
2821 : Elf_Scn *needscn = NULL;
2822 :
2823 : Elf_Scn *verscn = NULL;
2824 368 : while ((verscn = elf_nextscn (ebl->elf, verscn)) != NULL)
2825 : {
2826 : GElf_Shdr vershdr_mem;
2827 358 : GElf_Shdr *vershdr = gelf_getshdr (verscn, &vershdr_mem);
2828 :
2829 358 : if (likely (vershdr != NULL))
2830 : {
2831 358 : if (vershdr->sh_type == SHT_GNU_verdef)
2832 : defscn = verscn;
2833 354 : else if (vershdr->sh_type == SHT_GNU_verneed)
2834 10 : needscn = verscn;
2835 : }
2836 : }
2837 :
2838 : size_t nvername;
2839 10 : if (defscn != NULL || needscn != NULL)
2840 : {
2841 : /* We have a version information (better should have). Now get
2842 : the version names. First find the maximum version number. */
2843 10 : nvername = 0;
2844 10 : if (defscn != NULL)
2845 : {
2846 : /* Run through the version definitions and find the highest
2847 : index. */
2848 4 : unsigned int offset = 0;
2849 : Elf_Data *defdata;
2850 : GElf_Shdr defshdrmem;
2851 : GElf_Shdr *defshdr;
2852 :
2853 4 : defdata = elf_getdata (defscn, NULL);
2854 4 : if (unlikely (defdata == NULL))
2855 0 : return;
2856 :
2857 4 : defshdr = gelf_getshdr (defscn, &defshdrmem);
2858 4 : if (unlikely (defshdr == NULL))
2859 : return;
2860 :
2861 34 : for (unsigned int cnt = 0; cnt < defshdr->sh_info; ++cnt)
2862 : {
2863 : GElf_Verdef defmem;
2864 : GElf_Verdef *def;
2865 :
2866 : /* Get the data at the next offset. */
2867 38 : def = gelf_getverdef (defdata, offset, &defmem);
2868 38 : if (unlikely (def == NULL))
2869 : break;
2870 :
2871 38 : nvername = MAX (nvername, (size_t) (def->vd_ndx & 0x7fff));
2872 :
2873 38 : if (def->vd_next == 0)
2874 : break;
2875 34 : offset += def->vd_next;
2876 : }
2877 : }
2878 10 : if (needscn != NULL)
2879 : {
2880 10 : unsigned int offset = 0;
2881 : Elf_Data *needdata;
2882 : GElf_Shdr needshdrmem;
2883 : GElf_Shdr *needshdr;
2884 :
2885 10 : needdata = elf_getdata (needscn, NULL);
2886 10 : if (unlikely (needdata == NULL))
2887 0 : return;
2888 :
2889 10 : needshdr = gelf_getshdr (needscn, &needshdrmem);
2890 10 : if (unlikely (needshdr == NULL))
2891 : return;
2892 :
2893 27 : for (unsigned int cnt = 0; cnt < needshdr->sh_info; ++cnt)
2894 : {
2895 : GElf_Verneed needmem;
2896 : GElf_Verneed *need;
2897 : unsigned int auxoffset;
2898 : int cnt2;
2899 :
2900 : /* Get the data at the next offset. */
2901 37 : need = gelf_getverneed (needdata, offset, &needmem);
2902 37 : if (unlikely (need == NULL))
2903 : break;
2904 :
2905 : /* Run through the auxiliary entries. */
2906 37 : auxoffset = offset + need->vn_aux;
2907 119 : for (cnt2 = need->vn_cnt; --cnt2 >= 0; )
2908 : {
2909 : GElf_Vernaux auxmem;
2910 : GElf_Vernaux *aux;
2911 :
2912 82 : aux = gelf_getvernaux (needdata, auxoffset, &auxmem);
2913 82 : if (unlikely (aux == NULL))
2914 : break;
2915 :
2916 82 : nvername = MAX (nvername,
2917 : (size_t) (aux->vna_other & 0x7fff));
2918 :
2919 82 : if (aux->vna_next == 0)
2920 : break;
2921 45 : auxoffset += aux->vna_next;
2922 : }
2923 :
2924 37 : if (need->vn_next == 0)
2925 : break;
2926 27 : offset += need->vn_next;
2927 : }
2928 : }
2929 :
2930 : /* This is the number of versions we know about. */
2931 10 : ++nvername;
2932 :
2933 : /* Allocate the array. */
2934 10 : vername = (const char **) alloca (nvername * sizeof (const char *));
2935 20 : memset(vername, 0, nvername * sizeof (const char *));
2936 10 : filename = (const char **) alloca (nvername * sizeof (const char *));
2937 20 : memset(filename, 0, nvername * sizeof (const char *));
2938 :
2939 : /* Run through the data structures again and collect the strings. */
2940 10 : if (defscn != NULL)
2941 : {
2942 : /* Run through the version definitions and find the highest
2943 : index. */
2944 4 : unsigned int offset = 0;
2945 : Elf_Data *defdata;
2946 : GElf_Shdr defshdrmem;
2947 : GElf_Shdr *defshdr;
2948 :
2949 4 : defdata = elf_getdata (defscn, NULL);
2950 4 : if (unlikely (defdata == NULL))
2951 0 : return;
2952 :
2953 4 : defshdr = gelf_getshdr (defscn, &defshdrmem);
2954 4 : if (unlikely (defshdr == NULL))
2955 : return;
2956 :
2957 34 : for (unsigned int cnt = 0; cnt < defshdr->sh_info; ++cnt)
2958 : {
2959 :
2960 : /* Get the data at the next offset. */
2961 : GElf_Verdef defmem;
2962 38 : GElf_Verdef *def = gelf_getverdef (defdata, offset, &defmem);
2963 38 : if (unlikely (def == NULL))
2964 : break;
2965 :
2966 : GElf_Verdaux auxmem;
2967 38 : GElf_Verdaux *aux = gelf_getverdaux (defdata,
2968 38 : offset + def->vd_aux,
2969 : &auxmem);
2970 38 : if (unlikely (aux == NULL))
2971 : break;
2972 :
2973 38 : vername[def->vd_ndx & 0x7fff]
2974 76 : = elf_strptr (ebl->elf, defshdr->sh_link, aux->vda_name);
2975 38 : filename[def->vd_ndx & 0x7fff] = NULL;
2976 :
2977 38 : if (def->vd_next == 0)
2978 : break;
2979 34 : offset += def->vd_next;
2980 : }
2981 : }
2982 10 : if (needscn != NULL)
2983 : {
2984 10 : unsigned int offset = 0;
2985 :
2986 10 : Elf_Data *needdata = elf_getdata (needscn, NULL);
2987 : GElf_Shdr needshdrmem;
2988 10 : GElf_Shdr *needshdr = gelf_getshdr (needscn, &needshdrmem);
2989 10 : if (unlikely (needdata == NULL || needshdr == NULL))
2990 0 : return;
2991 :
2992 27 : for (unsigned int cnt = 0; cnt < needshdr->sh_info; ++cnt)
2993 : {
2994 : /* Get the data at the next offset. */
2995 : GElf_Verneed needmem;
2996 37 : GElf_Verneed *need = gelf_getverneed (needdata, offset,
2997 : &needmem);
2998 37 : if (unlikely (need == NULL))
2999 : break;
3000 :
3001 : /* Run through the auxiliary entries. */
3002 37 : unsigned int auxoffset = offset + need->vn_aux;
3003 119 : for (int cnt2 = need->vn_cnt; --cnt2 >= 0; )
3004 : {
3005 : GElf_Vernaux auxmem;
3006 82 : GElf_Vernaux *aux = gelf_getvernaux (needdata, auxoffset,
3007 : &auxmem);
3008 82 : if (unlikely (aux == NULL))
3009 : break;
3010 :
3011 82 : vername[aux->vna_other & 0x7fff]
3012 164 : = elf_strptr (ebl->elf, needshdr->sh_link, aux->vna_name);
3013 82 : filename[aux->vna_other & 0x7fff]
3014 164 : = elf_strptr (ebl->elf, needshdr->sh_link, need->vn_file);
3015 :
3016 82 : if (aux->vna_next == 0)
3017 : break;
3018 45 : auxoffset += aux->vna_next;
3019 : }
3020 :
3021 37 : if (need->vn_next == 0)
3022 : break;
3023 27 : offset += need->vn_next;
3024 : }
3025 : }
3026 : }
3027 : else
3028 : {
3029 : vername = NULL;
3030 : nvername = 1;
3031 : filename = NULL;
3032 : }
3033 :
3034 : GElf_Shdr glink_mem;
3035 10 : GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
3036 : &glink_mem);
3037 10 : size_t sh_entsize = gelf_fsize (ebl->elf, ELF_T_HALF, 1, EV_CURRENT);
3038 10 : if (glink == NULL)
3039 0 : error (EXIT_FAILURE, 0, gettext ("invalid sh_link value in section %zu"),
3040 : elf_ndxscn (scn));
3041 :
3042 : /* Print the header. */
3043 60 : printf (ngettext ("\
3044 : \nVersion symbols section [%2u] '%s' contains %d entry:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'",
3045 : "\
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 10 : (unsigned int) elf_ndxscn (scn),
3049 10 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
3050 10 : (int) (shdr->sh_size / sh_entsize),
3051 : class == ELFCLASS32 ? 10 : 18, shdr->sh_addr,
3052 : shdr->sh_offset,
3053 10 : (unsigned int) shdr->sh_link,
3054 10 : elf_strptr (ebl->elf, shstrndx, glink->sh_name));
3055 :
3056 : /* Now we can finally look at the actual contents of this section. */
3057 1317 : for (unsigned int cnt = 0; cnt < shdr->sh_size / sh_entsize; ++cnt)
3058 : {
3059 1307 : if (cnt % 2 == 0)
3060 : printf ("\n %4d:", cnt);
3061 :
3062 : GElf_Versym symmem;
3063 1307 : GElf_Versym *sym = gelf_getversym (data, cnt, &symmem);
3064 1307 : if (sym == NULL)
3065 : break;
3066 :
3067 1307 : switch (*sym)
3068 : {
3069 : ssize_t n;
3070 112 : case 0:
3071 112 : fputs_unlocked (gettext (" 0 *local* "),
3072 : stdout);
3073 : break;
3074 :
3075 32 : case 1:
3076 32 : fputs_unlocked (gettext (" 1 *global* "),
3077 : stdout);
3078 : break;
3079 :
3080 1163 : default:
3081 4652 : n = printf ("%4d%c%s",
3082 1163 : *sym & 0x7fff, *sym & 0x8000 ? 'h' : ' ',
3083 : (vername != NULL
3084 1163 : && (unsigned int) (*sym & 0x7fff) < nvername)
3085 1163 : ? vername[*sym & 0x7fff] : "???");
3086 1163 : if ((unsigned int) (*sym & 0x7fff) < nvername
3087 1163 : && filename != NULL && filename[*sym & 0x7fff] != NULL)
3088 1582 : n += printf ("(%s)", filename[*sym & 0x7fff]);
3089 1163 : printf ("%*s", MAX (0, 33 - (int) n), " ");
3090 : break;
3091 : }
3092 : }
3093 10 : putchar_unlocked ('\n');
3094 : }
3095 :
3096 :
3097 : static void
3098 10 : 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)
3101 : {
3102 10 : uint32_t *counts = (uint32_t *) xcalloc (maxlength + 1, sizeof (uint32_t));
3103 :
3104 326 : for (Elf32_Word cnt = 0; cnt < nbucket; ++cnt)
3105 316 : ++counts[lengths[cnt]];
3106 :
3107 : GElf_Shdr glink_mem;
3108 10 : GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf,
3109 10 : shdr->sh_link),
3110 : &glink_mem);
3111 10 : if (glink == NULL)
3112 : {
3113 0 : error (0, 0, gettext ("invalid sh_link value in section %zu"),
3114 : elf_ndxscn (scn));
3115 0 : return;
3116 : }
3117 :
3118 60 : 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",
3120 : "\
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",
3122 : nbucket),
3123 10 : (unsigned int) elf_ndxscn (scn),
3124 10 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
3125 : (int) nbucket,
3126 10 : gelf_getclass (ebl->elf) == ELFCLASS32 ? 10 : 18,
3127 : shdr->sh_addr,
3128 : shdr->sh_offset,
3129 10 : (unsigned int) shdr->sh_link,
3130 10 : elf_strptr (ebl->elf, shstrndx, glink->sh_name));
3131 :
3132 10 : if (extrastr != NULL)
3133 10 : fputs (extrastr, stdout);
3134 :
3135 10 : if (likely (nbucket > 0))
3136 : {
3137 10 : uint64_t success = 0;
3138 :
3139 : /* xgettext:no-c-format */
3140 10 : fputs_unlocked (gettext ("\
3141 : Length Number % of total Coverage\n"), stdout);
3142 20 : printf (gettext (" 0 %6" PRIu32 " %5.1f%%\n"),
3143 10 : counts[0], (counts[0] * 100.0) / nbucket);
3144 :
3145 10 : uint64_t nzero_counts = 0;
3146 53 : for (Elf32_Word cnt = 1; cnt <= maxlength; ++cnt)
3147 : {
3148 43 : nzero_counts += counts[cnt] * cnt;
3149 129 : printf (gettext ("\
3150 : %7d %6" PRIu32 " %5.1f%% %5.1f%%\n"),
3151 43 : (int) cnt, counts[cnt], (counts[cnt] * 100.0) / nbucket,
3152 43 : (nzero_counts * 100.0) / nsyms);
3153 : }
3154 :
3155 : Elf32_Word acc = 0;
3156 43 : for (Elf32_Word cnt = 1; cnt <= maxlength; ++cnt)
3157 : {
3158 43 : acc += cnt;
3159 43 : success += counts[cnt] * acc;
3160 : }
3161 :
3162 20 : printf (gettext ("\
3163 : Average number of tests: successful lookup: %f\n\
3164 : unsuccessful lookup: %f\n"),
3165 10 : (double) success / (double) nzero_counts,
3166 10 : (double) nzero_counts / (double) nbucket);
3167 : }
3168 :
3169 10 : free (counts);
3170 : }
3171 :
3172 :
3173 : /* This function handles the traditional System V-style hash table format. */
3174 : static void
3175 0 : handle_sysv_hash (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr, size_t shstrndx)
3176 : {
3177 0 : Elf_Data *data = elf_getdata (scn, NULL);
3178 0 : if (unlikely (data == NULL))
3179 : {
3180 0 : error (0, 0, gettext ("cannot get data for section %d: %s"),
3181 0 : (int) elf_ndxscn (scn), elf_errmsg (-1));
3182 : return;
3183 : }
3184 :
3185 0 : if (unlikely (data->d_size < 2 * sizeof (Elf32_Word)))
3186 : {
3187 0 : invalid_data:
3188 0 : error (0, 0, gettext ("invalid data in sysv.hash section %d"),
3189 0 : (int) elf_ndxscn (scn));
3190 : return;
3191 : }
3192 :
3193 0 : Elf32_Word nbucket = ((Elf32_Word *) data->d_buf)[0];
3194 0 : Elf32_Word nchain = ((Elf32_Word *) data->d_buf)[1];
3195 :
3196 0 : uint64_t used_buf = (2ULL + nchain + nbucket) * sizeof (Elf32_Word);
3197 0 : if (used_buf > data->d_size)
3198 : goto invalid_data;
3199 :
3200 0 : Elf32_Word *bucket = &((Elf32_Word *) data->d_buf)[2];
3201 0 : Elf32_Word *chain = &((Elf32_Word *) data->d_buf)[2 + nbucket];
3202 :
3203 0 : uint32_t *lengths = (uint32_t *) xcalloc (nbucket, sizeof (uint32_t));
3204 :
3205 0 : uint_fast32_t maxlength = 0;
3206 0 : uint_fast32_t nsyms = 0;
3207 0 : for (Elf32_Word cnt = 0; cnt < nbucket; ++cnt)
3208 : {
3209 0 : Elf32_Word inner = bucket[cnt];
3210 0 : Elf32_Word chain_len = 0;
3211 0 : while (inner > 0 && inner < nchain)
3212 : {
3213 0 : ++nsyms;
3214 0 : ++chain_len;
3215 0 : if (chain_len > nchain)
3216 : {
3217 0 : error (0, 0, gettext ("invalid chain in sysv.hash section %d"),
3218 0 : (int) elf_ndxscn (scn));
3219 0 : free (lengths);
3220 0 : return;
3221 : }
3222 0 : if (maxlength < ++lengths[cnt])
3223 0 : ++maxlength;
3224 :
3225 0 : inner = chain[inner];
3226 : }
3227 : }
3228 :
3229 0 : print_hash_info (ebl, scn, shdr, shstrndx, maxlength, nbucket, nsyms,
3230 : lengths, NULL);
3231 :
3232 0 : free (lengths);
3233 : }
3234 :
3235 :
3236 : /* This function handles the incorrect, System V-style hash table
3237 : format some 64-bit architectures use. */
3238 : static void
3239 0 : handle_sysv_hash64 (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr, size_t shstrndx)
3240 : {
3241 0 : Elf_Data *data = elf_getdata (scn, NULL);
3242 0 : if (unlikely (data == NULL))
3243 : {
3244 0 : error (0, 0, gettext ("cannot get data for section %d: %s"),
3245 0 : (int) elf_ndxscn (scn), elf_errmsg (-1));
3246 : return;
3247 : }
3248 :
3249 0 : if (unlikely (data->d_size < 2 * sizeof (Elf64_Xword)))
3250 : {
3251 0 : invalid_data:
3252 0 : error (0, 0, gettext ("invalid data in sysv.hash64 section %d"),
3253 0 : (int) elf_ndxscn (scn));
3254 : return;
3255 : }
3256 :
3257 0 : Elf64_Xword nbucket = ((Elf64_Xword *) data->d_buf)[0];
3258 0 : Elf64_Xword nchain = ((Elf64_Xword *) data->d_buf)[1];
3259 :
3260 0 : uint64_t maxwords = data->d_size / sizeof (Elf64_Xword);
3261 0 : if (maxwords < 2
3262 0 : || maxwords - 2 < nbucket
3263 0 : || maxwords - 2 - nbucket < nchain)
3264 : goto invalid_data;
3265 :
3266 0 : Elf64_Xword *bucket = &((Elf64_Xword *) data->d_buf)[2];
3267 0 : Elf64_Xword *chain = &((Elf64_Xword *) data->d_buf)[2 + nbucket];
3268 :
3269 0 : uint32_t *lengths = (uint32_t *) xcalloc (nbucket, sizeof (uint32_t));
3270 :
3271 0 : uint_fast32_t maxlength = 0;
3272 0 : uint_fast32_t nsyms = 0;
3273 0 : for (Elf64_Xword cnt = 0; cnt < nbucket; ++cnt)
3274 : {
3275 0 : Elf64_Xword inner = bucket[cnt];
3276 0 : Elf64_Xword chain_len = 0;
3277 0 : while (inner > 0 && inner < nchain)
3278 : {
3279 0 : ++nsyms;
3280 0 : ++chain_len;
3281 0 : if (chain_len > nchain)
3282 : {
3283 0 : error (0, 0, gettext ("invalid chain in sysv.hash64 section %d"),
3284 0 : (int) elf_ndxscn (scn));
3285 0 : free (lengths);
3286 0 : return;
3287 : }
3288 0 : if (maxlength < ++lengths[cnt])
3289 0 : ++maxlength;
3290 :
3291 0 : inner = chain[inner];
3292 : }
3293 : }
3294 :
3295 0 : print_hash_info (ebl, scn, shdr, shstrndx, maxlength, nbucket, nsyms,
3296 : lengths, NULL);
3297 :
3298 0 : free (lengths);
3299 : }
3300 :
3301 :
3302 : /* This function handles the GNU-style hash table format. */
3303 : static void
3304 10 : handle_gnu_hash (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr, size_t shstrndx)
3305 : {
3306 10 : uint32_t *lengths = NULL;
3307 10 : Elf_Data *data = elf_getdata (scn, NULL);
3308 10 : if (unlikely (data == NULL))
3309 : {
3310 0 : error (0, 0, gettext ("cannot get data for section %d: %s"),
3311 0 : (int) elf_ndxscn (scn), elf_errmsg (-1));
3312 0 : return;
3313 : }
3314 :
3315 10 : if (unlikely (data->d_size < 4 * sizeof (Elf32_Word)))
3316 : {
3317 0 : invalid_data:
3318 0 : free (lengths);
3319 0 : error (0, 0, gettext ("invalid data in gnu.hash section %d"),
3320 0 : (int) elf_ndxscn (scn));
3321 : return;
3322 : }
3323 :
3324 10 : Elf32_Word nbucket = ((Elf32_Word *) data->d_buf)[0];
3325 10 : Elf32_Word symbias = ((Elf32_Word *) data->d_buf)[1];
3326 :
3327 : /* Next comes the size of the bitmap. It's measured in words for
3328 : the architecture. It's 32 bits for 32 bit archs, and 64 bits for
3329 : 64 bit archs. There is always a bloom filter present, so zero is
3330 : an invalid value. */
3331 10 : Elf32_Word bitmask_words = ((Elf32_Word *) data->d_buf)[2];
3332 10 : if (gelf_getclass (ebl->elf) == ELFCLASS64)
3333 10 : bitmask_words *= 2;
3334 :
3335 10 : if (bitmask_words == 0)
3336 : goto invalid_data;
3337 :
3338 10 : Elf32_Word shift = ((Elf32_Word *) data->d_buf)[3];
3339 :
3340 : /* Is there still room for the sym chain?
3341 : Use uint64_t calculation to prevent 32bit overlow. */
3342 10 : uint64_t used_buf = (4ULL + bitmask_words + nbucket) * sizeof (Elf32_Word);
3343 10 : uint32_t max_nsyms = (data->d_size - used_buf) / sizeof (Elf32_Word);
3344 10 : if (used_buf > data->d_size)
3345 : goto invalid_data;
3346 :
3347 10 : lengths = (uint32_t *) xcalloc (nbucket, sizeof (uint32_t));
3348 :
3349 10 : Elf32_Word *bitmask = &((Elf32_Word *) data->d_buf)[4];
3350 10 : Elf32_Word *bucket = &((Elf32_Word *) data->d_buf)[4 + bitmask_words];
3351 10 : Elf32_Word *chain = &((Elf32_Word *) data->d_buf)[4 + bitmask_words
3352 10 : + nbucket];
3353 :
3354 : /* Compute distribution of chain lengths. */
3355 10 : uint_fast32_t maxlength = 0;
3356 10 : uint_fast32_t nsyms = 0;
3357 326 : for (Elf32_Word cnt = 0; cnt < nbucket; ++cnt)
3358 316 : if (bucket[cnt] != 0)
3359 : {
3360 221 : Elf32_Word inner = bucket[cnt] - symbias;
3361 : do
3362 : {
3363 425 : ++nsyms;
3364 425 : if (maxlength < ++lengths[cnt])
3365 43 : ++maxlength;
3366 425 : if (inner >= max_nsyms)
3367 : goto invalid_data;
3368 : }
3369 425 : while ((chain[inner++] & 1) == 0);
3370 : }
3371 :
3372 : /* Count bits in bitmask. */
3373 : uint_fast32_t nbits = 0;
3374 114 : for (Elf32_Word cnt = 0; cnt < bitmask_words; ++cnt)
3375 : {
3376 114 : uint_fast32_t word = bitmask[cnt];
3377 :
3378 114 : word = (word & 0x55555555) + ((word >> 1) & 0x55555555);
3379 114 : word = (word & 0x33333333) + ((word >> 2) & 0x33333333);
3380 114 : word = (word & 0x0f0f0f0f) + ((word >> 4) & 0x0f0f0f0f);
3381 114 : word = (word & 0x00ff00ff) + ((word >> 8) & 0x00ff00ff);
3382 114 : nbits += (word & 0x0000ffff) + ((word >> 16) & 0x0000ffff);
3383 : }
3384 :
3385 : char *str;
3386 20 : if (unlikely (asprintf (&str, gettext ("\
3387 : Symbol Bias: %u\n\
3388 : Bitmask Size: %zu bytes %" PRIuFAST32 "%% bits set 2nd hash shift: %u\n"),
3389 : (unsigned int) symbias,
3390 : bitmask_words * sizeof (Elf32_Word),
3391 : ((nbits * 100 + 50)
3392 : / (uint_fast32_t) (bitmask_words
3393 : * sizeof (Elf32_Word) * 8)),
3394 : (unsigned int) shift) == -1))
3395 0 : error (EXIT_FAILURE, 0, gettext ("memory exhausted"));
3396 :
3397 10 : print_hash_info (ebl, scn, shdr, shstrndx, maxlength, nbucket, nsyms,
3398 : lengths, str);
3399 :
3400 10 : free (str);
3401 10 : free (lengths);
3402 : }
3403 :
3404 :
3405 : /* Find the symbol table(s). For this we have to search through the
3406 : section table. */
3407 : static void
3408 32 : handle_hash (Ebl *ebl)
3409 : {
3410 : /* Get the section header string table index. */
3411 : size_t shstrndx;
3412 32 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
3413 : error (EXIT_FAILURE, 0,
3414 0 : gettext ("cannot get section header string table index"));
3415 :
3416 : Elf_Scn *scn = NULL;
3417 919 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
3418 : {
3419 : /* Handle the section if it is a symbol table. */
3420 : GElf_Shdr shdr_mem;
3421 887 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
3422 :
3423 887 : if (likely (shdr != NULL))
3424 : {
3425 887 : if ((shdr->sh_type == SHT_HASH || shdr->sh_type == SHT_GNU_HASH)
3426 10 : && (shdr->sh_flags & SHF_COMPRESSED) != 0)
3427 : {
3428 0 : if (elf_compress (scn, 0, 0) < 0)
3429 0 : printf ("WARNING: %s [%zd]\n",
3430 : gettext ("Couldn't uncompress section"),
3431 : elf_ndxscn (scn));
3432 0 : shdr = gelf_getshdr (scn, &shdr_mem);
3433 0 : if (unlikely (shdr == NULL))
3434 0 : error (EXIT_FAILURE, 0,
3435 0 : gettext ("cannot get section [%zd] header: %s"),
3436 : elf_ndxscn (scn), elf_errmsg (-1));
3437 : }
3438 :
3439 887 : if (shdr->sh_type == SHT_HASH)
3440 : {
3441 0 : if (ebl_sysvhash_entrysize (ebl) == sizeof (Elf64_Xword))
3442 0 : handle_sysv_hash64 (ebl, scn, shdr, shstrndx);
3443 : else
3444 0 : handle_sysv_hash (ebl, scn, shdr, shstrndx);
3445 : }
3446 887 : else if (shdr->sh_type == SHT_GNU_HASH)
3447 10 : handle_gnu_hash (ebl, scn, shdr, shstrndx);
3448 : }
3449 : }
3450 32 : }
3451 :
3452 :
3453 : static void
3454 36 : print_liblist (Ebl *ebl)
3455 : {
3456 : /* Find the library list sections. For this we have to search
3457 : through the section table. */
3458 36 : Elf_Scn *scn = NULL;
3459 :
3460 : /* Get the section header string table index. */
3461 : size_t shstrndx;
3462 36 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
3463 : error (EXIT_FAILURE, 0,
3464 0 : gettext ("cannot get section header string table index"));
3465 :
3466 982 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
3467 : {
3468 : GElf_Shdr shdr_mem;
3469 946 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
3470 :
3471 946 : if (shdr != NULL && shdr->sh_type == SHT_GNU_LIBLIST)
3472 : {
3473 0 : size_t sh_entsize = gelf_fsize (ebl->elf, ELF_T_LIB, 1, EV_CURRENT);
3474 0 : int nentries = shdr->sh_size / sh_entsize;
3475 0 : printf (ngettext ("\
3476 : \nLibrary list section [%2zu] '%s' at offset %#0" PRIx64 " contains %d entry:\n",
3477 : "\
3478 : \nLibrary list section [%2zu] '%s' at offset %#0" PRIx64 " contains %d entries:\n",
3479 : nentries),
3480 : elf_ndxscn (scn),
3481 0 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
3482 : shdr->sh_offset,
3483 : nentries);
3484 :
3485 0 : Elf_Data *data = elf_getdata (scn, NULL);
3486 0 : if (data == NULL)
3487 0 : return;
3488 :
3489 0 : puts (gettext ("\
3490 : Library Time Stamp Checksum Version Flags"));
3491 :
3492 0 : for (int cnt = 0; cnt < nentries; ++cnt)
3493 : {
3494 : GElf_Lib lib_mem;
3495 0 : GElf_Lib *lib = gelf_getlib (data, cnt, &lib_mem);
3496 0 : if (unlikely (lib == NULL))
3497 0 : continue;
3498 :
3499 0 : time_t t = (time_t) lib->l_time_stamp;
3500 0 : struct tm *tm = gmtime (&t);
3501 0 : if (unlikely (tm == NULL))
3502 0 : continue;
3503 :
3504 0 : printf (" [%2d] %-29s %04u-%02u-%02uT%02u:%02u:%02u %08x %-7u %u\n",
3505 0 : cnt, elf_strptr (ebl->elf, shdr->sh_link, lib->l_name),
3506 0 : tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
3507 : tm->tm_hour, tm->tm_min, tm->tm_sec,
3508 0 : (unsigned int) lib->l_checksum,
3509 0 : (unsigned int) lib->l_version,
3510 0 : (unsigned int) lib->l_flags);
3511 : }
3512 : }
3513 : }
3514 : }
3515 :
3516 : static void
3517 36 : print_attributes (Ebl *ebl, const GElf_Ehdr *ehdr)
3518 : {
3519 : /* Find the object attributes sections. For this we have to search
3520 : through the section table. */
3521 36 : Elf_Scn *scn = NULL;
3522 :
3523 : /* Get the section header string table index. */
3524 : size_t shstrndx;
3525 36 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
3526 : error (EXIT_FAILURE, 0,
3527 0 : gettext ("cannot get section header string table index"));
3528 :
3529 982 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
3530 : {
3531 : GElf_Shdr shdr_mem;
3532 946 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
3533 :
3534 946 : if (shdr == NULL || (shdr->sh_type != SHT_GNU_ATTRIBUTES
3535 943 : && (shdr->sh_type != SHT_ARM_ATTRIBUTES
3536 1 : || ehdr->e_machine != EM_ARM)))
3537 942 : continue;
3538 :
3539 12 : printf (gettext ("\
3540 : \nObject attributes section [%2zu] '%s' of %" PRIu64
3541 : " bytes at offset %#0" PRIx64 ":\n"),
3542 : elf_ndxscn (scn),
3543 4 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
3544 : shdr->sh_size, shdr->sh_offset);
3545 :
3546 4 : Elf_Data *data = elf_rawdata (scn, NULL);
3547 4 : if (unlikely (data == NULL || data->d_size == 0))
3548 0 : return;
3549 :
3550 4 : const unsigned char *p = data->d_buf;
3551 :
3552 : /* There is only one 'version', A. */
3553 4 : if (unlikely (*p++ != 'A'))
3554 : return;
3555 :
3556 4 : fputs_unlocked (gettext (" Owner Size\n"), stdout);
3557 :
3558 : inline size_t left (void)
3559 : {
3560 8 : return (const unsigned char *) data->d_buf + data->d_size - p;
3561 : }
3562 :
3563 : /* Loop over the sections. */
3564 8 : while (left () >= 4)
3565 : {
3566 : /* Section length. */
3567 : uint32_t len;
3568 4 : memcpy (&len, p, sizeof len);
3569 :
3570 4 : if (MY_ELFDATA != ehdr->e_ident[EI_DATA])
3571 4 : CONVERT (len);
3572 :
3573 4 : if (unlikely (len > left ()))
3574 : break;
3575 :
3576 : /* Section vendor name. */
3577 4 : const unsigned char *name = p + sizeof len;
3578 4 : p += len;
3579 :
3580 4 : unsigned const char *q = memchr (name, '\0', len);
3581 4 : if (unlikely (q == NULL))
3582 : break;
3583 4 : ++q;
3584 :
3585 8 : printf (gettext (" %-13s %4" PRIu32 "\n"), name, len);
3586 :
3587 8 : bool gnu_vendor = (q - name == sizeof "gnu"
3588 4 : && !memcmp (name, "gnu", sizeof "gnu"));
3589 :
3590 : /* Loop over subsections. */
3591 4 : if (shdr->sh_type != SHT_GNU_ATTRIBUTES
3592 3 : || gnu_vendor)
3593 8 : while (q < p)
3594 : {
3595 4 : const unsigned char *const sub = q;
3596 :
3597 : unsigned int subsection_tag;
3598 4 : get_uleb128 (subsection_tag, q, p);
3599 4 : if (unlikely (q >= p))
3600 : break;
3601 :
3602 : uint32_t subsection_len;
3603 4 : if (unlikely (p - sub < (ptrdiff_t) sizeof subsection_len))
3604 : break;
3605 :
3606 4 : memcpy (&subsection_len, q, sizeof subsection_len);
3607 :
3608 4 : if (MY_ELFDATA != ehdr->e_ident[EI_DATA])
3609 4 : CONVERT (subsection_len);
3610 :
3611 : /* Don't overflow, ptrdiff_t might be 32bits, but signed. */
3612 4 : if (unlikely (subsection_len == 0
3613 : || subsection_len >= (uint32_t) PTRDIFF_MAX
3614 : || p - sub < (ptrdiff_t) subsection_len))
3615 : break;
3616 :
3617 4 : const unsigned char *r = q + sizeof subsection_len;
3618 4 : q = sub + subsection_len;
3619 :
3620 4 : switch (subsection_tag)
3621 : {
3622 0 : default:
3623 : /* Unknown subsection, print and skip. */
3624 0 : printf (gettext (" %-4u %12" PRIu32 "\n"),
3625 : subsection_tag, subsection_len);
3626 : break;
3627 :
3628 4 : case 1: /* Tag_File */
3629 4 : printf (gettext (" File: %11" PRIu32 "\n"),
3630 : subsection_len);
3631 :
3632 26 : while (r < q)
3633 22 : {
3634 : unsigned int tag;
3635 22 : get_uleb128 (tag, r, q);
3636 22 : if (unlikely (r >= q))
3637 : break;
3638 :
3639 : /* GNU style tags have either a uleb128 value,
3640 : when lowest bit is not set, or a string
3641 : when the lowest bit is set.
3642 : "compatibility" (32) is special. It has
3643 : both a string and a uleb128 value. For
3644 : non-gnu we assume 6 till 31 only take ints.
3645 : XXX see arm backend, do we need a separate
3646 : hook? */
3647 22 : uint64_t value = 0;
3648 22 : const char *string = NULL;
3649 22 : if (tag == 32 || (tag & 1) == 0
3650 8 : || (! gnu_vendor && (tag > 5 && tag < 32)))
3651 : {
3652 21 : get_uleb128 (value, r, q);
3653 21 : if (r > q)
3654 : break;
3655 : }
3656 22 : if (tag == 32
3657 22 : || ((tag & 1) != 0
3658 8 : && (gnu_vendor
3659 8 : || (! gnu_vendor && tag > 32)))
3660 22 : || (! gnu_vendor && tag > 3 && tag < 6))
3661 : {
3662 1 : string = (const char *) r;
3663 1 : r = memchr (r, '\0', q - r);
3664 1 : if (r == NULL)
3665 : break;
3666 1 : ++r;
3667 : }
3668 :
3669 22 : const char *tag_name = NULL;
3670 22 : const char *value_name = NULL;
3671 22 : ebl_check_object_attribute (ebl, (const char *) name,
3672 : tag, value,
3673 : &tag_name, &value_name);
3674 :
3675 22 : if (tag_name != NULL)
3676 : {
3677 22 : if (tag == 32)
3678 0 : printf (gettext (" %s: %" PRId64 ", %s\n"),
3679 : tag_name, value, string);
3680 22 : else if (string == NULL && value_name == NULL)
3681 1 : printf (gettext (" %s: %" PRId64 "\n"),
3682 : tag_name, value);
3683 : else
3684 21 : printf (gettext (" %s: %s\n"),
3685 : tag_name, string ?: value_name);
3686 : }
3687 : else
3688 : {
3689 : /* For "gnu" vendor 32 "compatibility" has
3690 : already been handled above. */
3691 0 : assert (tag != 32
3692 : || strcmp ((const char *) name, "gnu"));
3693 0 : if (string == NULL)
3694 0 : printf (gettext (" %u: %" PRId64 "\n"),
3695 : tag, value);
3696 : else
3697 0 : printf (gettext (" %u: %s\n"),
3698 : tag, string);
3699 : }
3700 : }
3701 : }
3702 : }
3703 : }
3704 : }
3705 : }
3706 :
3707 :
3708 : void
3709 581578 : print_dwarf_addr (Dwfl_Module *dwflmod,
3710 : int address_size, Dwarf_Addr address, Dwarf_Addr raw)
3711 : {
3712 : /* See if there is a name we can give for this address. */
3713 : GElf_Sym sym;
3714 581578 : GElf_Off off = 0;
3715 1744706 : const char *name = (print_address_names && ! print_unresolved_addresses)
3716 581493 : ? dwfl_module_addrinfo (dwflmod, address, &off, &sym, NULL, NULL, NULL)
3717 1163071 : : NULL;
3718 :
3719 : const char *scn;
3720 581578 : if (print_unresolved_addresses)
3721 : {
3722 57 : address = raw;
3723 57 : scn = NULL;
3724 : }
3725 : else
3726 : {
3727 : /* Relativize the address. */
3728 581521 : int n = dwfl_module_relocations (dwflmod);
3729 581521 : int i = n < 1 ? -1 : dwfl_module_relocate_address (dwflmod, &address);
3730 :
3731 : /* In an ET_REL file there is a section name to refer to. */
3732 : scn = (i < 0 ? NULL
3733 580529 : : dwfl_module_relocation_info (dwflmod, i, NULL));
3734 : }
3735 :
3736 581578 : if ((name != NULL
3737 571113 : ? (off != 0
3738 : ? (scn != NULL
3739 : ? (address_size == 0
3740 282630 : ? printf ("%s+%#" PRIx64 " <%s+%#" PRIx64 ">",
3741 : scn, address, name, off)
3742 1216311 : : printf ("%s+%#0*" PRIx64 " <%s+%#" PRIx64 ">",
3743 405437 : scn, 2 + address_size * 2, address,
3744 : name, off))
3745 : : (address_size == 0
3746 338 : ? printf ("%#" PRIx64 " <%s+%#" PRIx64 ">",
3747 : address, name, off)
3748 1410 : : printf ("%#0*" PRIx64 " <%s+%#" PRIx64 ">",
3749 470 : 2 + address_size * 2, address,
3750 : name, off)))
3751 : : (scn != NULL
3752 : ? (address_size == 0
3753 12032 : ? printf ("%s+%#" PRIx64 " <%s>", scn, address, name)
3754 52299 : : printf ("%s+%#0*" PRIx64 " <%s>",
3755 17433 : scn, 2 + address_size * 2, address, name))
3756 : : (address_size == 0
3757 158 : ? printf ("%#" PRIx64 " <%s>", address, name)
3758 582 : : printf ("%#0*" PRIx64 " <%s>",
3759 194 : 2 + address_size * 2, address, name))))
3760 : : (scn != NULL
3761 : ? (address_size == 0
3762 10456 : ? printf ("%s+%#" PRIx64, scn, address)
3763 10200 : : printf ("%s+%#0*" PRIx64, scn, 2 + address_size * 2, address))
3764 : : (address_size == 0
3765 64 : ? printf ("%#" PRIx64, address)
3766 1734269 : : printf ("%#0*" PRIx64, 2 + address_size * 2, address)))) < 0)
3767 0 : error (EXIT_FAILURE, 0, _("sprintf failure"));
3768 581578 : }
3769 :
3770 :
3771 : static const char *
3772 754143 : dwarf_tag_string (unsigned int tag)
3773 : {
3774 754143 : switch (tag)
3775 : {
3776 : #define DWARF_ONE_KNOWN_DW_TAG(NAME, CODE) case CODE: return #NAME;
3777 0 : DWARF_ALL_KNOWN_DW_TAG
3778 : #undef DWARF_ONE_KNOWN_DW_TAG
3779 0 : default:
3780 0 : return NULL;
3781 : }
3782 : }
3783 :
3784 :
3785 : static const char *
3786 2663786 : dwarf_attr_string (unsigned int attrnum)
3787 : {
3788 2663786 : switch (attrnum)
3789 : {
3790 : #define DWARF_ONE_KNOWN_DW_AT(NAME, CODE) case CODE: return #NAME;
3791 4304 : DWARF_ALL_KNOWN_DW_AT
3792 : #undef DWARF_ONE_KNOWN_DW_AT
3793 0 : default:
3794 0 : return NULL;
3795 : }
3796 : }
3797 :
3798 :
3799 : static const char *
3800 2663792 : dwarf_form_string (unsigned int form)
3801 : {
3802 2663792 : switch (form)
3803 : {
3804 : #define DWARF_ONE_KNOWN_DW_FORM(NAME, CODE) case CODE: return #NAME;
3805 21 : DWARF_ALL_KNOWN_DW_FORM
3806 : #undef DWARF_ONE_KNOWN_DW_FORM
3807 0 : default:
3808 0 : return NULL;
3809 : }
3810 : }
3811 :
3812 :
3813 : static const char *
3814 1126 : dwarf_lang_string (unsigned int lang)
3815 : {
3816 1126 : switch (lang)
3817 : {
3818 : #define DWARF_ONE_KNOWN_DW_LANG(NAME, CODE) case CODE: return #NAME;
3819 1 : DWARF_ALL_KNOWN_DW_LANG
3820 : #undef DWARF_ONE_KNOWN_DW_LANG
3821 0 : default:
3822 0 : return NULL;
3823 : }
3824 : }
3825 :
3826 :
3827 : static const char *
3828 : dwarf_inline_string (unsigned int code)
3829 : {
3830 : static const char *const known[] =
3831 : {
3832 : #define DWARF_ONE_KNOWN_DW_INL(NAME, CODE) [CODE] = #NAME,
3833 : DWARF_ALL_KNOWN_DW_INL
3834 : #undef DWARF_ONE_KNOWN_DW_INL
3835 : };
3836 :
3837 2795 : if (likely (code < sizeof (known) / sizeof (known[0])))
3838 2795 : return known[code];
3839 :
3840 : return NULL;
3841 : }
3842 :
3843 :
3844 : static const char *
3845 : dwarf_encoding_string (unsigned int code)
3846 : {
3847 : static const char *const known[] =
3848 : {
3849 : #define DWARF_ONE_KNOWN_DW_ATE(NAME, CODE) [CODE] = #NAME,
3850 : DWARF_ALL_KNOWN_DW_ATE
3851 : #undef DWARF_ONE_KNOWN_DW_ATE
3852 : };
3853 :
3854 16115 : if (likely (code < sizeof (known) / sizeof (known[0])))
3855 16115 : return known[code];
3856 :
3857 : return NULL;
3858 : }
3859 :
3860 :
3861 : static const char *
3862 : dwarf_access_string (unsigned int code)
3863 : {
3864 : static const char *const known[] =
3865 : {
3866 : #define DWARF_ONE_KNOWN_DW_ACCESS(NAME, CODE) [CODE] = #NAME,
3867 : DWARF_ALL_KNOWN_DW_ACCESS
3868 : #undef DWARF_ONE_KNOWN_DW_ACCESS
3869 : };
3870 :
3871 0 : if (likely (code < sizeof (known) / sizeof (known[0])))
3872 0 : return known[code];
3873 :
3874 : return NULL;
3875 : }
3876 :
3877 :
3878 : static const char *
3879 : dwarf_defaulted_string (unsigned int code)
3880 : {
3881 : static const char *const known[] =
3882 : {
3883 : #define DWARF_ONE_KNOWN_DW_DEFAULTED(NAME, CODE) [CODE] = #NAME,
3884 : DWARF_ALL_KNOWN_DW_DEFAULTED
3885 : #undef DWARF_ONE_KNOWN_DW_DEFAULTED
3886 : };
3887 :
3888 0 : if (likely (code < sizeof (known) / sizeof (known[0])))
3889 0 : return known[code];
3890 :
3891 : return NULL;
3892 : }
3893 :
3894 :
3895 : static const char *
3896 : dwarf_visibility_string (unsigned int code)
3897 : {
3898 : static const char *const known[] =
3899 : {
3900 : #define DWARF_ONE_KNOWN_DW_VIS(NAME, CODE) [CODE] = #NAME,
3901 : DWARF_ALL_KNOWN_DW_VIS
3902 : #undef DWARF_ONE_KNOWN_DW_VIS
3903 : };
3904 :
3905 0 : if (likely (code < sizeof (known) / sizeof (known[0])))
3906 0 : return known[code];
3907 :
3908 : return NULL;
3909 : }
3910 :
3911 :
3912 : static const char *
3913 : dwarf_virtuality_string (unsigned int code)
3914 : {
3915 : static const char *const known[] =
3916 : {
3917 : #define DWARF_ONE_KNOWN_DW_VIRTUALITY(NAME, CODE) [CODE] = #NAME,
3918 : DWARF_ALL_KNOWN_DW_VIRTUALITY
3919 : #undef DWARF_ONE_KNOWN_DW_VIRTUALITY
3920 : };
3921 :
3922 0 : if (likely (code < sizeof (known) / sizeof (known[0])))
3923 0 : return known[code];
3924 :
3925 : return NULL;
3926 : }
3927 :
3928 :
3929 : static const char *
3930 : dwarf_identifier_case_string (unsigned int code)
3931 : {
3932 : static const char *const known[] =
3933 : {
3934 : #define DWARF_ONE_KNOWN_DW_ID(NAME, CODE) [CODE] = #NAME,
3935 : DWARF_ALL_KNOWN_DW_ID
3936 : #undef DWARF_ONE_KNOWN_DW_ID
3937 : };
3938 :
3939 0 : if (likely (code < sizeof (known) / sizeof (known[0])))
3940 0 : return known[code];
3941 :
3942 : return NULL;
3943 : }
3944 :
3945 :
3946 : static const char *
3947 : dwarf_calling_convention_string (unsigned int code)
3948 : {
3949 : static const char *const known[] =
3950 : {
3951 : #define DWARF_ONE_KNOWN_DW_CC(NAME, CODE) [CODE] = #NAME,
3952 : DWARF_ALL_KNOWN_DW_CC
3953 : #undef DWARF_ONE_KNOWN_DW_CC
3954 : };
3955 :
3956 0 : if (likely (code < sizeof (known) / sizeof (known[0])))
3957 0 : return known[code];
3958 :
3959 : return NULL;
3960 : }
3961 :
3962 :
3963 : static const char *
3964 : dwarf_ordering_string (unsigned int code)
3965 : {
3966 : static const char *const known[] =
3967 : {
3968 : #define DWARF_ONE_KNOWN_DW_ORD(NAME, CODE) [CODE] = #NAME,
3969 : DWARF_ALL_KNOWN_DW_ORD
3970 : #undef DWARF_ONE_KNOWN_DW_ORD
3971 : };
3972 :
3973 0 : if (likely (code < sizeof (known) / sizeof (known[0])))
3974 0 : return known[code];
3975 :
3976 : return NULL;
3977 : }
3978 :
3979 :
3980 : static const char *
3981 : dwarf_discr_list_string (unsigned int code)
3982 : {
3983 : static const char *const known[] =
3984 : {
3985 : #define DWARF_ONE_KNOWN_DW_DSC(NAME, CODE) [CODE] = #NAME,
3986 : DWARF_ALL_KNOWN_DW_DSC
3987 : #undef DWARF_ONE_KNOWN_DW_DSC
3988 : };
3989 :
3990 0 : if (likely (code < sizeof (known) / sizeof (known[0])))
3991 0 : return known[code];
3992 :
3993 : return NULL;
3994 : }
3995 :
3996 :
3997 : static const char *
3998 : dwarf_locexpr_opcode_string (unsigned int code)
3999 : {
4000 : static const char *const known[] =
4001 : {
4002 : /* Normally we can't affort building huge table of 64K entries,
4003 : most of them zero, just because there are a couple defined
4004 : values at the far end. In case of opcodes, it's OK. */
4005 : #define DWARF_ONE_KNOWN_DW_OP(NAME, CODE) [CODE] = #NAME,
4006 : DWARF_ALL_KNOWN_DW_OP
4007 : #undef DWARF_ONE_KNOWN_DW_OP
4008 : };
4009 :
4010 362096 : if (likely (code < sizeof (known) / sizeof (known[0])))
4011 362096 : return known[code];
4012 :
4013 : return NULL;
4014 : }
4015 :
4016 :
4017 : static const char *
4018 : dwarf_unit_string (unsigned int type)
4019 : {
4020 7 : switch (type)
4021 : {
4022 : #define DWARF_ONE_KNOWN_DW_UT(NAME, CODE) case CODE: return #NAME;
4023 0 : DWARF_ALL_KNOWN_DW_UT
4024 : #undef DWARF_ONE_KNOWN_DW_UT
4025 0 : default:
4026 : return NULL;
4027 : }
4028 : }
4029 :
4030 :
4031 : static const char *
4032 14 : dwarf_range_list_encoding_string (unsigned int kind)
4033 : {
4034 14 : switch (kind)
4035 : {
4036 : #define DWARF_ONE_KNOWN_DW_RLE(NAME, CODE) case CODE: return #NAME;
4037 0 : DWARF_ALL_KNOWN_DW_RLE
4038 : #undef DWARF_ONE_KNOWN_DW_RLE
4039 0 : default:
4040 0 : return NULL;
4041 : }
4042 : }
4043 :
4044 :
4045 : static const char *
4046 89 : dwarf_loc_list_encoding_string (unsigned int kind)
4047 : {
4048 89 : switch (kind)
4049 : {
4050 : #define DWARF_ONE_KNOWN_DW_LLE(NAME, CODE) case CODE: return #NAME;
4051 0 : DWARF_ALL_KNOWN_DW_LLE
4052 : #undef DWARF_ONE_KNOWN_DW_LLE
4053 0 : default:
4054 0 : return NULL;
4055 : }
4056 : }
4057 :
4058 :
4059 : static const char *
4060 : dwarf_line_content_description_string (unsigned int kind)
4061 : {
4062 6 : switch (kind)
4063 : {
4064 : #define DWARF_ONE_KNOWN_DW_LNCT(NAME, CODE) case CODE: return #NAME;
4065 2 : DWARF_ALL_KNOWN_DW_LNCT
4066 : #undef DWARF_ONE_KNOWN_DW_LNCT
4067 0 : default:
4068 : return NULL;
4069 : }
4070 : }
4071 :
4072 :
4073 : /* Used by all dwarf_foo_name functions. */
4074 : static const char *
4075 6101873 : string_or_unknown (const char *known, unsigned int code,
4076 : unsigned int lo_user, unsigned int hi_user,
4077 : bool print_unknown_num)
4078 : {
4079 : static char unknown_buf[20];
4080 :
4081 6101873 : if (likely (known != NULL))
4082 : return known;
4083 :
4084 0 : if (lo_user != 0 && code >= lo_user && code <= hi_user)
4085 : {
4086 0 : snprintf (unknown_buf, sizeof unknown_buf, "lo_user+%#x",
4087 : code - lo_user);
4088 0 : return unknown_buf;
4089 : }
4090 :
4091 0 : if (print_unknown_num)
4092 : {
4093 0 : snprintf (unknown_buf, sizeof unknown_buf, "??? (%#x)", code);
4094 0 : return unknown_buf;
4095 : }
4096 :
4097 : return "???";
4098 : }
4099 :
4100 :
4101 : static const char *
4102 754143 : dwarf_tag_name (unsigned int tag)
4103 : {
4104 754143 : const char *ret = dwarf_tag_string (tag);
4105 754143 : return string_or_unknown (ret, tag, DW_TAG_lo_user, DW_TAG_hi_user, true);
4106 : }
4107 :
4108 : static const char *
4109 2663786 : dwarf_attr_name (unsigned int attr)
4110 : {
4111 2663786 : const char *ret = dwarf_attr_string (attr);
4112 2663786 : return string_or_unknown (ret, attr, DW_AT_lo_user, DW_AT_hi_user, true);
4113 : }
4114 :
4115 :
4116 : static const char *
4117 2663792 : dwarf_form_name (unsigned int form)
4118 : {
4119 2663792 : const char *ret = dwarf_form_string (form);
4120 2663792 : return string_or_unknown (ret, form, 0, 0, true);
4121 : }
4122 :
4123 :
4124 : static const char *
4125 1126 : dwarf_lang_name (unsigned int lang)
4126 : {
4127 1126 : const char *ret = dwarf_lang_string (lang);
4128 1126 : return string_or_unknown (ret, lang, DW_LANG_lo_user, DW_LANG_hi_user, false);
4129 : }
4130 :
4131 :
4132 : static const char *
4133 : dwarf_inline_name (unsigned int code)
4134 : {
4135 2795 : const char *ret = dwarf_inline_string (code);
4136 2795 : return string_or_unknown (ret, code, 0, 0, false);
4137 : }
4138 :
4139 :
4140 : static const char *
4141 : dwarf_encoding_name (unsigned int code)
4142 : {
4143 16115 : const char *ret = dwarf_encoding_string (code);
4144 16115 : return string_or_unknown (ret, code, DW_ATE_lo_user, DW_ATE_hi_user, false);
4145 : }
4146 :
4147 :
4148 : static const char *
4149 : dwarf_access_name (unsigned int code)
4150 : {
4151 0 : const char *ret = dwarf_access_string (code);
4152 0 : return string_or_unknown (ret, code, 0, 0, false);
4153 : }
4154 :
4155 :
4156 : static const char *
4157 : dwarf_defaulted_name (unsigned int code)
4158 : {
4159 0 : const char *ret = dwarf_defaulted_string (code);
4160 0 : return string_or_unknown (ret, code, 0, 0, false);
4161 : }
4162 :
4163 :
4164 : static const char *
4165 : dwarf_visibility_name (unsigned int code)
4166 : {
4167 0 : const char *ret = dwarf_visibility_string (code);
4168 0 : return string_or_unknown (ret, code, 0, 0, false);
4169 : }
4170 :
4171 :
4172 : static const char *
4173 : dwarf_virtuality_name (unsigned int code)
4174 : {
4175 0 : const char *ret = dwarf_virtuality_string (code);
4176 0 : return string_or_unknown (ret, code, 0, 0, false);
4177 : }
4178 :
4179 :
4180 : static const char *
4181 : dwarf_identifier_case_name (unsigned int code)
4182 : {
4183 0 : const char *ret = dwarf_identifier_case_string (code);
4184 0 : return string_or_unknown (ret, code, 0, 0, false);
4185 : }
4186 :
4187 :
4188 : static const char *
4189 : dwarf_calling_convention_name (unsigned int code)
4190 : {
4191 0 : const char *ret = dwarf_calling_convention_string (code);
4192 0 : return string_or_unknown (ret, code, DW_CC_lo_user, DW_CC_hi_user, false);
4193 : }
4194 :
4195 :
4196 : static const char *
4197 : dwarf_ordering_name (unsigned int code)
4198 : {
4199 0 : const char *ret = dwarf_ordering_string (code);
4200 0 : return string_or_unknown (ret, code, 0, 0, false);
4201 : }
4202 :
4203 :
4204 : static const char *
4205 : dwarf_discr_list_name (unsigned int code)
4206 : {
4207 0 : const char *ret = dwarf_discr_list_string (code);
4208 0 : return string_or_unknown (ret, code, 0, 0, false);
4209 : }
4210 :
4211 :
4212 : static const char *
4213 7 : dwarf_unit_name (unsigned int type)
4214 : {
4215 7 : const char *ret = dwarf_unit_string (type);
4216 7 : return string_or_unknown (ret, type, DW_UT_lo_user, DW_UT_hi_user, true);
4217 : }
4218 :
4219 :
4220 : static const char *
4221 14 : dwarf_range_list_encoding_name (unsigned int kind)
4222 : {
4223 14 : const char *ret = dwarf_range_list_encoding_string (kind);
4224 14 : return string_or_unknown (ret, kind, 0, 0, false);
4225 : }
4226 :
4227 :
4228 : static const char *
4229 89 : dwarf_loc_list_encoding_name (unsigned int kind)
4230 : {
4231 89 : const char *ret = dwarf_loc_list_encoding_string (kind);
4232 89 : return string_or_unknown (ret, kind, 0, 0, false);
4233 : }
4234 :
4235 :
4236 : static const char *
4237 6 : dwarf_line_content_description_name (unsigned int kind)
4238 : {
4239 6 : const char *ret = dwarf_line_content_description_string (kind);
4240 6 : return string_or_unknown (ret, kind, DW_LNCT_lo_user, DW_LNCT_hi_user,
4241 : false);
4242 : }
4243 :
4244 :
4245 : static void
4246 135 : print_block (size_t n, const void *block)
4247 : {
4248 135 : if (n == 0)
4249 0 : puts (_("empty block"));
4250 : else
4251 : {
4252 270 : printf (_("%zu byte block:"), n);
4253 135 : const unsigned char *data = block;
4254 : do
4255 2694 : printf (" %02x", *data++);
4256 1347 : while (--n > 0);
4257 : putchar ('\n');
4258 : }
4259 135 : }
4260 :
4261 : static void
4262 0 : print_bytes (size_t n, const unsigned char *bytes)
4263 : {
4264 0 : while (n-- > 0)
4265 : {
4266 0 : printf ("%02x", *bytes++);
4267 0 : if (n > 0)
4268 : printf (" ");
4269 : }
4270 0 : }
4271 :
4272 : static int
4273 68 : get_indexed_addr (Dwarf_CU *cu, Dwarf_Word idx, Dwarf_Addr *addr)
4274 : {
4275 68 : if (cu == NULL)
4276 : return -1;
4277 :
4278 68 : Elf_Data *debug_addr = cu->dbg->sectiondata[IDX_debug_addr];
4279 68 : if (debug_addr == NULL)
4280 : return -1;
4281 :
4282 68 : Dwarf_Off base = __libdw_cu_addr_base (cu);
4283 68 : Dwarf_Word off = idx * cu->address_size;
4284 68 : if (base > debug_addr->d_size
4285 68 : || off > debug_addr->d_size - base
4286 68 : || cu->address_size > debug_addr->d_size - base - off)
4287 : return -1;
4288 :
4289 68 : const unsigned char *addrp = debug_addr->d_buf + base + off;
4290 68 : if (cu->address_size == 4)
4291 0 : *addr = read_4ubyte_unaligned (cu->dbg, addrp);
4292 : else
4293 68 : *addr = read_8ubyte_unaligned (cu->dbg, addrp);
4294 :
4295 : return 0;
4296 : }
4297 :
4298 : static void
4299 253628 : print_ops (Dwfl_Module *dwflmod, Dwarf *dbg, int indent, int indentrest,
4300 : unsigned int vers, unsigned int addrsize, unsigned int offset_size,
4301 : struct Dwarf_CU *cu, Dwarf_Word len, const unsigned char *data)
4302 : {
4303 253628 : const unsigned int ref_size = vers < 3 ? addrsize : offset_size;
4304 :
4305 253628 : if (len == 0)
4306 : {
4307 : printf ("%*s(empty)\n", indent, "");
4308 : return;
4309 : }
4310 :
4311 : #define NEED(n) if (len < (Dwarf_Word) (n)) goto invalid
4312 : #define CONSUME(n) NEED (n); else len -= (n)
4313 :
4314 : Dwarf_Word offset = 0;
4315 615724 : while (len-- > 0)
4316 : {
4317 362096 : uint_fast8_t op = *data++;
4318 :
4319 724192 : const char *op_name = dwarf_locexpr_opcode_string (op);
4320 362096 : if (unlikely (op_name == NULL))
4321 : {
4322 : static char buf[20];
4323 0 : if (op >= DW_OP_lo_user)
4324 0 : snprintf (buf, sizeof buf, "lo_user+%#x", op - DW_OP_lo_user);
4325 : else
4326 0 : snprintf (buf, sizeof buf, "??? (%#x)", op);
4327 : op_name = buf;
4328 : }
4329 :
4330 362096 : switch (op)
4331 : {
4332 6898 : case DW_OP_addr:;
4333 : /* Address operand. */
4334 : Dwarf_Word addr;
4335 6898 : NEED (addrsize);
4336 6898 : if (addrsize == 4)
4337 48 : addr = read_4ubyte_unaligned (dbg, data);
4338 6870 : else if (addrsize == 8)
4339 6888 : addr = read_8ubyte_unaligned (dbg, data);
4340 : else
4341 : goto invalid;
4342 6898 : data += addrsize;
4343 6898 : CONSUME (addrsize);
4344 :
4345 6898 : printf ("%*s[%2" PRIuMAX "] %s ",
4346 : indent, "", (uintmax_t) offset, op_name);
4347 6898 : print_dwarf_addr (dwflmod, 0, addr, addr);
4348 6898 : printf ("\n");
4349 :
4350 6898 : offset += 1 + addrsize;
4351 368994 : break;
4352 :
4353 0 : case DW_OP_call_ref:
4354 : case DW_OP_GNU_variable_value:
4355 : /* Offset operand. */
4356 0 : if (ref_size != 4 && ref_size != 8)
4357 : goto invalid; /* Cannot be used in CFA. */
4358 0 : NEED (ref_size);
4359 0 : if (ref_size == 4)
4360 0 : addr = read_4ubyte_unaligned (dbg, data);
4361 : else
4362 0 : addr = read_8ubyte_unaligned (dbg, data);
4363 0 : data += ref_size;
4364 0 : CONSUME (ref_size);
4365 : /* addr is a DIE offset, so format it as one. */
4366 0 : printf ("%*s[%2" PRIuMAX "] %s [%6" PRIxMAX "]\n",
4367 : indent, "", (uintmax_t) offset,
4368 : op_name, (uintmax_t) addr);
4369 0 : offset += 1 + ref_size;
4370 0 : break;
4371 :
4372 9397 : case DW_OP_deref_size:
4373 : case DW_OP_xderef_size:
4374 : case DW_OP_pick:
4375 : case DW_OP_const1u:
4376 : // XXX value might be modified by relocation
4377 9397 : NEED (1);
4378 18794 : printf ("%*s[%2" PRIuMAX "] %s %" PRIu8 "\n",
4379 : indent, "", (uintmax_t) offset,
4380 9397 : op_name, *((uint8_t *) data));
4381 9397 : ++data;
4382 9397 : --len;
4383 9397 : offset += 2;
4384 9397 : break;
4385 :
4386 975 : case DW_OP_const2u:
4387 975 : NEED (2);
4388 : // XXX value might be modified by relocation
4389 2925 : printf ("%*s[%2" PRIuMAX "] %s %" PRIu16 "\n",
4390 : indent, "", (uintmax_t) offset,
4391 1950 : op_name, read_2ubyte_unaligned (dbg, data));
4392 975 : CONSUME (2);
4393 975 : data += 2;
4394 975 : offset += 3;
4395 975 : break;
4396 :
4397 540 : case DW_OP_const4u:
4398 540 : NEED (4);
4399 : // XXX value might be modified by relocation
4400 1620 : printf ("%*s[%2" PRIuMAX "] %s %" PRIu32 "\n",
4401 : indent, "", (uintmax_t) offset,
4402 1080 : op_name, read_4ubyte_unaligned (dbg, data));
4403 540 : CONSUME (4);
4404 540 : data += 4;
4405 540 : offset += 5;
4406 540 : break;
4407 :
4408 46 : case DW_OP_const8u:
4409 46 : NEED (8);
4410 : // XXX value might be modified by relocation
4411 138 : printf ("%*s[%2" PRIuMAX "] %s %" PRIu64 "\n",
4412 : indent, "", (uintmax_t) offset,
4413 92 : op_name, (uint64_t) read_8ubyte_unaligned (dbg, data));
4414 46 : CONSUME (8);
4415 46 : data += 8;
4416 46 : offset += 9;
4417 46 : break;
4418 :
4419 1850 : case DW_OP_const1s:
4420 1850 : NEED (1);
4421 : // XXX value might be modified by relocation
4422 3700 : printf ("%*s[%2" PRIuMAX "] %s %" PRId8 "\n",
4423 : indent, "", (uintmax_t) offset,
4424 1850 : op_name, *((int8_t *) data));
4425 1850 : ++data;
4426 1850 : --len;
4427 1850 : offset += 2;
4428 1850 : break;
4429 :
4430 3 : case DW_OP_const2s:
4431 3 : NEED (2);
4432 : // XXX value might be modified by relocation
4433 9 : printf ("%*s[%2" PRIuMAX "] %s %" PRId16 "\n",
4434 : indent, "", (uintmax_t) offset,
4435 6 : op_name, read_2sbyte_unaligned (dbg, data));
4436 3 : CONSUME (2);
4437 3 : data += 2;
4438 3 : offset += 3;
4439 3 : break;
4440 :
4441 0 : case DW_OP_const4s:
4442 0 : NEED (4);
4443 : // XXX value might be modified by relocation
4444 0 : printf ("%*s[%2" PRIuMAX "] %s %" PRId32 "\n",
4445 : indent, "", (uintmax_t) offset,
4446 0 : op_name, read_4sbyte_unaligned (dbg, data));
4447 0 : CONSUME (4);
4448 0 : data += 4;
4449 0 : offset += 5;
4450 0 : break;
4451 :
4452 0 : case DW_OP_const8s:
4453 0 : NEED (8);
4454 : // XXX value might be modified by relocation
4455 0 : printf ("%*s[%2" PRIuMAX "] %s %" PRId64 "\n",
4456 : indent, "", (uintmax_t) offset,
4457 0 : op_name, read_8sbyte_unaligned (dbg, data));
4458 0 : CONSUME (8);
4459 0 : data += 8;
4460 0 : offset += 9;
4461 0 : break;
4462 :
4463 7862 : case DW_OP_piece:
4464 : case DW_OP_regx:
4465 : case DW_OP_plus_uconst:
4466 : case DW_OP_constu:;
4467 7862 : const unsigned char *start = data;
4468 : uint64_t uleb;
4469 7862 : NEED (1);
4470 7862 : get_uleb128 (uleb, data, data + len);
4471 7862 : printf ("%*s[%2" PRIuMAX "] %s %" PRIu64 "\n",
4472 : indent, "", (uintmax_t) offset, op_name, uleb);
4473 7862 : CONSUME (data - start);
4474 7862 : offset += 1 + (data - start);
4475 7862 : break;
4476 :
4477 2 : case DW_OP_addrx:
4478 : case DW_OP_GNU_addr_index:
4479 : case DW_OP_constx:
4480 : case DW_OP_GNU_const_index:;
4481 2 : start = data;
4482 2 : NEED (1);
4483 2 : get_uleb128 (uleb, data, data + len);
4484 2 : printf ("%*s[%2" PRIuMAX "] %s [%" PRIu64 "] ",
4485 : indent, "", (uintmax_t) offset, op_name, uleb);
4486 2 : CONSUME (data - start);
4487 2 : offset += 1 + (data - start);
4488 2 : if (get_indexed_addr (cu, uleb, &addr) != 0)
4489 : printf ("???\n");
4490 : else
4491 : {
4492 2 : print_dwarf_addr (dwflmod, 0, addr, addr);
4493 : printf ("\n");
4494 : }
4495 : break;
4496 :
4497 0 : case DW_OP_bit_piece:
4498 0 : start = data;
4499 : uint64_t uleb2;
4500 0 : NEED (1);
4501 0 : get_uleb128 (uleb, data, data + len);
4502 : NEED (1);
4503 0 : get_uleb128 (uleb2, data, data + len);
4504 0 : printf ("%*s[%2" PRIuMAX "] %s %" PRIu64 ", %" PRIu64 "\n",
4505 : indent, "", (uintmax_t) offset, op_name, uleb, uleb2);
4506 0 : CONSUME (data - start);
4507 0 : offset += 1 + (data - start);
4508 0 : break;
4509 :
4510 65121 : case DW_OP_fbreg:
4511 : case DW_OP_breg0 ... DW_OP_breg31:
4512 : case DW_OP_consts:
4513 65121 : start = data;
4514 : int64_t sleb;
4515 65121 : NEED (1);
4516 65121 : get_sleb128 (sleb, data, data + len);
4517 65121 : printf ("%*s[%2" PRIuMAX "] %s %" PRId64 "\n",
4518 : indent, "", (uintmax_t) offset, op_name, sleb);
4519 65121 : CONSUME (data - start);
4520 65121 : offset += 1 + (data - start);
4521 65121 : break;
4522 :
4523 0 : case DW_OP_bregx:
4524 0 : start = data;
4525 0 : NEED (1);
4526 0 : get_uleb128 (uleb, data, data + len);
4527 : NEED (1);
4528 0 : get_sleb128 (sleb, data, data + len);
4529 0 : printf ("%*s[%2" PRIuMAX "] %s %" PRIu64 " %" PRId64 "\n",
4530 : indent, "", (uintmax_t) offset, op_name, uleb, sleb);
4531 0 : CONSUME (data - start);
4532 0 : offset += 1 + (data - start);
4533 0 : break;
4534 :
4535 0 : case DW_OP_call2:
4536 0 : NEED (2);
4537 0 : printf ("%*s[%2" PRIuMAX "] %s [%6" PRIx16 "]\n",
4538 : indent, "", (uintmax_t) offset, op_name,
4539 0 : read_2ubyte_unaligned (dbg, data));
4540 0 : CONSUME (2);
4541 0 : data += 2;
4542 0 : offset += 3;
4543 0 : break;
4544 :
4545 1 : case DW_OP_call4:
4546 1 : NEED (4);
4547 3 : printf ("%*s[%2" PRIuMAX "] %s [%6" PRIx32 "]\n",
4548 : indent, "", (uintmax_t) offset, op_name,
4549 2 : read_4ubyte_unaligned (dbg, data));
4550 1 : CONSUME (4);
4551 1 : data += 4;
4552 1 : offset += 5;
4553 1 : break;
4554 :
4555 254 : case DW_OP_skip:
4556 : case DW_OP_bra:
4557 254 : NEED (2);
4558 254 : printf ("%*s[%2" PRIuMAX "] %s %" PRIuMAX "\n",
4559 : indent, "", (uintmax_t) offset, op_name,
4560 254 : (uintmax_t) (offset + read_2sbyte_unaligned (dbg, data) + 3));
4561 254 : CONSUME (2);
4562 254 : data += 2;
4563 254 : offset += 3;
4564 254 : break;
4565 :
4566 120 : case DW_OP_implicit_value:
4567 120 : start = data;
4568 120 : NEED (1);
4569 120 : get_uleb128 (uleb, data, data + len);
4570 120 : printf ("%*s[%2" PRIuMAX "] %s: ",
4571 : indent, "", (uintmax_t) offset, op_name);
4572 120 : NEED (uleb);
4573 120 : print_block (uleb, data);
4574 120 : data += uleb;
4575 120 : CONSUME (data - start);
4576 120 : offset += 1 + (data - start);
4577 120 : break;
4578 :
4579 3360 : case DW_OP_implicit_pointer:
4580 : case DW_OP_GNU_implicit_pointer:
4581 : /* DIE offset operand. */
4582 3360 : start = data;
4583 3360 : NEED (ref_size);
4584 3360 : if (ref_size != 4 && ref_size != 8)
4585 : goto invalid; /* Cannot be used in CFA. */
4586 3360 : if (ref_size == 4)
4587 3360 : addr = read_4ubyte_unaligned (dbg, data);
4588 : else
4589 0 : addr = read_8ubyte_unaligned (dbg, data);
4590 3360 : data += ref_size;
4591 : /* Byte offset operand. */
4592 3360 : NEED (1);
4593 3360 : get_sleb128 (sleb, data, data + len);
4594 :
4595 6720 : printf ("%*s[%2" PRIuMAX "] %s [%6" PRIxMAX "] %+" PRId64 "\n",
4596 : indent, "", (intmax_t) offset,
4597 : op_name, (uintmax_t) addr, sleb);
4598 3360 : CONSUME (data - start);
4599 3360 : offset += 1 + (data - start);
4600 3360 : break;
4601 :
4602 19651 : case DW_OP_entry_value:
4603 : case DW_OP_GNU_entry_value:
4604 : /* Size plus expression block. */
4605 19651 : start = data;
4606 19651 : NEED (1);
4607 19651 : get_uleb128 (uleb, data, data + len);
4608 19651 : printf ("%*s[%2" PRIuMAX "] %s:\n",
4609 : indent, "", (uintmax_t) offset, op_name);
4610 19651 : NEED (uleb);
4611 19651 : print_ops (dwflmod, dbg, indent + 5, indent + 5, vers,
4612 : addrsize, offset_size, cu, uleb, data);
4613 19651 : data += uleb;
4614 19651 : CONSUME (data - start);
4615 19651 : offset += 1 + (data - start);
4616 19651 : break;
4617 :
4618 1 : case DW_OP_const_type:
4619 : case DW_OP_GNU_const_type:
4620 : /* uleb128 CU relative DW_TAG_base_type DIE offset, 1-byte
4621 : unsigned size plus block. */
4622 1 : start = data;
4623 1 : NEED (1);
4624 1 : get_uleb128 (uleb, data, data + len);
4625 1 : if (! print_unresolved_addresses && cu != NULL)
4626 1 : uleb += cu->start;
4627 : NEED (1);
4628 1 : uint8_t usize = *(uint8_t *) data++;
4629 1 : NEED (usize);
4630 1 : printf ("%*s[%2" PRIuMAX "] %s [%6" PRIxMAX "] ",
4631 : indent, "", (uintmax_t) offset, op_name, uleb);
4632 1 : print_block (usize, data);
4633 1 : data += usize;
4634 1 : CONSUME (data - start);
4635 1 : offset += 1 + (data - start);
4636 1 : break;
4637 :
4638 0 : case DW_OP_regval_type:
4639 : case DW_OP_GNU_regval_type:
4640 : /* uleb128 register number, uleb128 CU relative
4641 : DW_TAG_base_type DIE offset. */
4642 0 : start = data;
4643 0 : NEED (1);
4644 0 : get_uleb128 (uleb, data, data + len);
4645 : NEED (1);
4646 0 : get_uleb128 (uleb2, data, data + len);
4647 0 : if (! print_unresolved_addresses && cu != NULL)
4648 0 : uleb2 += cu->start;
4649 0 : printf ("%*s[%2" PRIuMAX "] %s %" PRIu64 " [%6" PRIx64 "]\n",
4650 : indent, "", (uintmax_t) offset, op_name, uleb, uleb2);
4651 0 : CONSUME (data - start);
4652 0 : offset += 1 + (data - start);
4653 0 : break;
4654 :
4655 6 : case DW_OP_deref_type:
4656 : case DW_OP_GNU_deref_type:
4657 : /* 1-byte unsigned size of value, uleb128 CU relative
4658 : DW_TAG_base_type DIE offset. */
4659 6 : start = data;
4660 6 : NEED (1);
4661 6 : usize = *(uint8_t *) data++;
4662 : NEED (1);
4663 6 : get_uleb128 (uleb, data, data + len);
4664 6 : if (! print_unresolved_addresses && cu != NULL)
4665 6 : uleb += cu->start;
4666 12 : printf ("%*s[%2" PRIuMAX "] %s %" PRIu8 " [%6" PRIxMAX "]\n",
4667 : indent, "", (uintmax_t) offset,
4668 : op_name, usize, uleb);
4669 6 : CONSUME (data - start);
4670 6 : offset += 1 + (data - start);
4671 6 : break;
4672 :
4673 0 : case DW_OP_xderef_type:
4674 : /* 1-byte unsigned size of value, uleb128 base_type DIE offset. */
4675 0 : start = data;
4676 0 : NEED (1);
4677 0 : usize = *(uint8_t *) data++;
4678 : NEED (1);
4679 0 : get_uleb128 (uleb, data, data + len);
4680 0 : printf ("%*s[%4" PRIuMAX "] %s %" PRIu8 " [%6" PRIxMAX "]\n",
4681 : indent, "", (uintmax_t) offset,
4682 : op_name, usize, uleb);
4683 0 : CONSUME (data - start);
4684 0 : offset += 1 + (data - start);
4685 0 : break;
4686 :
4687 306 : case DW_OP_convert:
4688 : case DW_OP_GNU_convert:
4689 : case DW_OP_reinterpret:
4690 : case DW_OP_GNU_reinterpret:
4691 : /* uleb128 CU relative offset to DW_TAG_base_type, or zero
4692 : for conversion to untyped. */
4693 306 : start = data;
4694 306 : NEED (1);
4695 306 : get_uleb128 (uleb, data, data + len);
4696 306 : if (uleb != 0 && ! print_unresolved_addresses && cu != NULL)
4697 205 : uleb += cu->start;
4698 306 : printf ("%*s[%2" PRIuMAX "] %s [%6" PRIxMAX "]\n",
4699 : indent, "", (uintmax_t) offset, op_name, uleb);
4700 306 : CONSUME (data - start);
4701 306 : offset += 1 + (data - start);
4702 306 : break;
4703 :
4704 21 : case DW_OP_GNU_parameter_ref:
4705 : /* 4 byte CU relative reference to the abstract optimized away
4706 : DW_TAG_formal_parameter. */
4707 21 : NEED (4);
4708 21 : uintmax_t param_off = (uintmax_t) read_4ubyte_unaligned (dbg, data);
4709 21 : if (! print_unresolved_addresses && cu != NULL)
4710 21 : param_off += cu->start;
4711 21 : printf ("%*s[%2" PRIuMAX "] %s [%6" PRIxMAX "]\n",
4712 : indent, "", (uintmax_t) offset, op_name, param_off);
4713 21 : CONSUME (4);
4714 21 : data += 4;
4715 21 : offset += 5;
4716 21 : break;
4717 :
4718 245682 : default:
4719 : /* No Operand. */
4720 245682 : printf ("%*s[%2" PRIuMAX "] %s\n",
4721 : indent, "", (uintmax_t) offset, op_name);
4722 245682 : ++offset;
4723 245682 : break;
4724 : }
4725 :
4726 362096 : indent = indentrest;
4727 362096 : continue;
4728 :
4729 0 : invalid:
4730 0 : printf (gettext ("%*s[%2" PRIuMAX "] %s <TRUNCATED>\n"),
4731 : indent, "", (uintmax_t) offset, op_name);
4732 : break;
4733 : }
4734 : }
4735 :
4736 :
4737 : struct listptr
4738 : {
4739 : Dwarf_Off offset:(64 - 3);
4740 : bool addr64:1;
4741 : bool dwarf64:1;
4742 : bool warned:1;
4743 : struct Dwarf_CU *cu;
4744 : unsigned int attr;
4745 : };
4746 :
4747 : #define listptr_offset_size(p) ((p)->dwarf64 ? 8 : 4)
4748 : #define listptr_address_size(p) ((p)->addr64 ? 8 : 4)
4749 :
4750 : static Dwarf_Addr
4751 55686 : cudie_base (Dwarf_Die *cudie)
4752 : {
4753 : Dwarf_Addr base;
4754 : /* Find the base address of the compilation unit. It will normally
4755 : be specified by DW_AT_low_pc. In DWARF-3 draft 4, the base
4756 : address could be overridden by DW_AT_entry_pc. It's been
4757 : removed, but GCC emits DW_AT_entry_pc and not DW_AT_lowpc for
4758 : compilation units with discontinuous ranges. */
4759 55686 : if (unlikely (dwarf_lowpc (cudie, &base) != 0))
4760 : {
4761 : Dwarf_Attribute attr_mem;
4762 0 : if (dwarf_formaddr (dwarf_attr (cudie, DW_AT_entry_pc, &attr_mem),
4763 : &base) != 0)
4764 0 : base = 0;
4765 : }
4766 55686 : return base;
4767 : }
4768 :
4769 : static Dwarf_Addr
4770 55686 : listptr_base (struct listptr *p)
4771 : {
4772 111372 : Dwarf_Die cu = CUDIE (p->cu);
4773 55686 : return cudie_base (&cu);
4774 : }
4775 :
4776 : static int
4777 309574 : compare_listptr (const void *a, const void *b, void *arg)
4778 : {
4779 309574 : const char *name = arg;
4780 309574 : struct listptr *p1 = (void *) a;
4781 309574 : struct listptr *p2 = (void *) b;
4782 :
4783 309574 : if (p1->offset < p2->offset)
4784 : return -1;
4785 20795 : if (p1->offset > p2->offset)
4786 : return 1;
4787 :
4788 4074 : if (!p1->warned && !p2->warned)
4789 : {
4790 4074 : if (p1->addr64 != p2->addr64)
4791 : {
4792 0 : p1->warned = p2->warned = true;
4793 0 : error (0, 0,
4794 0 : gettext ("%s %#" PRIx64 " used with different address sizes"),
4795 : name, (uint64_t) p1->offset);
4796 : }
4797 4074 : if (p1->dwarf64 != p2->dwarf64)
4798 : {
4799 0 : p1->warned = p2->warned = true;
4800 0 : error (0, 0,
4801 0 : gettext ("%s %#" PRIx64 " used with different offset sizes"),
4802 0 : name, (uint64_t) p1->offset);
4803 : }
4804 4074 : if (listptr_base (p1) != listptr_base (p2))
4805 : {
4806 0 : p1->warned = p2->warned = true;
4807 0 : error (0, 0,
4808 0 : gettext ("%s %#" PRIx64 " used with different base addresses"),
4809 0 : name, (uint64_t) p1->offset);
4810 : }
4811 4074 : if (p1->attr != p2 ->attr)
4812 : {
4813 0 : p1->warned = p2->warned = true;
4814 0 : error (0, 0,
4815 0 : gettext ("%s %#" PRIx64
4816 : " used with different attribute %s and %s"),
4817 0 : name, (uint64_t) p1->offset, dwarf_attr_name (p2->attr),
4818 : dwarf_attr_name (p2->attr));
4819 : }
4820 : }
4821 :
4822 : return 0;
4823 : }
4824 :
4825 : struct listptr_table
4826 : {
4827 : size_t n;
4828 : size_t alloc;
4829 : struct listptr *table;
4830 : };
4831 :
4832 : static struct listptr_table known_locsptr;
4833 : static struct listptr_table known_loclistsptr;
4834 : static struct listptr_table known_rangelistptr;
4835 : static struct listptr_table known_rnglistptr;
4836 : static struct listptr_table known_addrbases;
4837 : static struct listptr_table known_stroffbases;
4838 :
4839 : static void
4840 : reset_listptr (struct listptr_table *table)
4841 : {
4842 744 : free (table->table);
4843 744 : table->table = NULL;
4844 744 : table->n = table->alloc = 0;
4845 : }
4846 :
4847 : /* Returns false if offset doesn't fit. See struct listptr. */
4848 : static bool
4849 51524 : notice_listptr (enum section_e section, struct listptr_table *table,
4850 : uint_fast8_t address_size, uint_fast8_t offset_size,
4851 : struct Dwarf_CU *cu, Dwarf_Off offset, unsigned int attr)
4852 : {
4853 51524 : if (print_debug_sections & section)
4854 : {
4855 51430 : if (table->n == table->alloc)
4856 : {
4857 157 : if (table->alloc == 0)
4858 70 : table->alloc = 128;
4859 : else
4860 87 : table->alloc *= 2;
4861 157 : table->table = xrealloc (table->table,
4862 157 : table->alloc * sizeof table->table[0]);
4863 : }
4864 :
4865 51430 : struct listptr *p = &table->table[table->n++];
4866 :
4867 102860 : *p = (struct listptr)
4868 : {
4869 51430 : .addr64 = address_size == 8,
4870 51430 : .dwarf64 = offset_size == 8,
4871 : .offset = offset,
4872 : .cu = cu,
4873 : .attr = attr
4874 : };
4875 :
4876 51430 : if (p->offset != offset)
4877 : {
4878 0 : table->n--;
4879 0 : return false;
4880 : }
4881 : }
4882 : return true;
4883 : }
4884 :
4885 : static void
4886 : sort_listptr (struct listptr_table *table, const char *name)
4887 : {
4888 74 : if (table->n > 0)
4889 70 : qsort_r (table->table, table->n, sizeof table->table[0],
4890 : &compare_listptr, (void *) name);
4891 : }
4892 :
4893 : static bool
4894 47532 : skip_listptr_hole (struct listptr_table *table, size_t *idxp,
4895 : uint_fast8_t *address_sizep, uint_fast8_t *offset_sizep,
4896 : Dwarf_Addr *base, struct Dwarf_CU **cu, ptrdiff_t offset,
4897 : unsigned char **readp, unsigned char *endp,
4898 : unsigned int *attr)
4899 : {
4900 47532 : if (table->n == 0)
4901 : return false;
4902 :
4903 98872 : while (*idxp < table->n && table->table[*idxp].offset < (Dwarf_Off) offset)
4904 51340 : ++*idxp;
4905 :
4906 47532 : struct listptr *p = &table->table[*idxp];
4907 :
4908 47532 : if (*idxp == table->n
4909 47532 : || p->offset >= (Dwarf_Off) (endp - *readp + offset))
4910 : {
4911 0 : *readp = endp;
4912 0 : printf (gettext (" [%6tx] <UNUSED GARBAGE IN REST OF SECTION>\n"),
4913 : offset);
4914 : return true;
4915 : }
4916 :
4917 47532 : if (p->offset != (Dwarf_Off) offset)
4918 : {
4919 0 : *readp += p->offset - offset;
4920 0 : printf (gettext (" [%6tx] <UNUSED GARBAGE> ... %" PRIu64 " bytes ...\n"),
4921 : offset, (Dwarf_Off) p->offset - offset);
4922 : return true;
4923 : }
4924 :
4925 47532 : if (address_sizep != NULL)
4926 47532 : *address_sizep = listptr_address_size (p);
4927 47532 : if (offset_sizep != NULL)
4928 38890 : *offset_sizep = listptr_offset_size (p);
4929 47532 : if (base != NULL)
4930 47532 : *base = listptr_base (p);
4931 47532 : if (cu != NULL)
4932 47532 : *cu = p->cu;
4933 47532 : if (attr != NULL)
4934 38890 : *attr = p->attr;
4935 :
4936 : return false;
4937 : }
4938 :
4939 : static Dwarf_Off
4940 : next_listptr_offset (struct listptr_table *table, size_t idx)
4941 : {
4942 : /* Note that multiple attributes could in theory point to the same loclist
4943 : offset, so make sure we pick one that is bigger than the current one.
4944 : The table is sorted on offset. */
4945 0 : Dwarf_Off offset = table->table[idx].offset;
4946 0 : while (++idx < table->n)
4947 : {
4948 0 : Dwarf_Off next = table->table[idx].offset;
4949 0 : if (next > offset)
4950 : return next;
4951 : }
4952 : return 0;
4953 : }
4954 :
4955 : /* Returns the listptr associated with the given index, or NULL. */
4956 : static struct listptr *
4957 : get_listptr (struct listptr_table *table, size_t idx)
4958 : {
4959 10 : if (idx >= table->n)
4960 : return NULL;
4961 5 : return &table->table[idx];
4962 : }
4963 :
4964 : /* Returns the next index, base address and CU associated with the
4965 : list unit offsets. If there is none false is returned, otherwise
4966 : true. Assumes the table has been sorted. */
4967 : static bool
4968 6 : listptr_cu (struct listptr_table *table, size_t *idxp,
4969 : Dwarf_Off start, Dwarf_Off end,
4970 : Dwarf_Addr *base, struct Dwarf_CU **cu)
4971 : {
4972 14 : while (*idxp < table->n
4973 14 : && table->table[*idxp].offset < start)
4974 8 : ++*idxp;
4975 :
4976 6 : if (*idxp < table->n
4977 6 : && table->table[*idxp].offset >= start
4978 6 : && table->table[*idxp].offset < end)
4979 : {
4980 6 : struct listptr *p = &table->table[*idxp];
4981 6 : *base = listptr_base (p);
4982 6 : *cu = p->cu;
4983 6 : ++*idxp;
4984 : return true;
4985 : }
4986 :
4987 : return false;
4988 : }
4989 :
4990 : static void
4991 34 : print_debug_abbrev_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
4992 : Ebl *ebl, GElf_Ehdr *ehdr,
4993 : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
4994 : {
4995 68 : const size_t sh_size = (dbg->sectiondata[IDX_debug_abbrev] ?
4996 34 : dbg->sectiondata[IDX_debug_abbrev]->d_size : 0);
4997 :
4998 102 : printf (gettext ("\nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"
4999 : " [ Code]\n"),
5000 : elf_ndxscn (scn), section_name (ebl, ehdr, shdr),
5001 34 : (uint64_t) shdr->sh_offset);
5002 :
5003 34 : Dwarf_Off offset = 0;
5004 34 : while (offset < sh_size)
5005 : {
5006 1069 : printf (gettext ("\nAbbreviation section at offset %" PRIu64 ":\n"),
5007 : offset);
5008 :
5009 : while (1)
5010 56064 : {
5011 : size_t length;
5012 : Dwarf_Abbrev abbrev;
5013 :
5014 57133 : int res = dwarf_offabbrev (dbg, offset, &length, &abbrev);
5015 57133 : if (res != 0)
5016 : {
5017 1069 : if (unlikely (res < 0))
5018 : {
5019 0 : printf (gettext ("\
5020 : *** error while reading abbreviation: %s\n"),
5021 : dwarf_errmsg (-1));
5022 0 : return;
5023 : }
5024 :
5025 : /* This is the NUL byte at the end of the section. */
5026 1069 : ++offset;
5027 1069 : break;
5028 : }
5029 :
5030 : /* We know these calls can never fail. */
5031 56064 : unsigned int code = dwarf_getabbrevcode (&abbrev);
5032 56064 : unsigned int tag = dwarf_getabbrevtag (&abbrev);
5033 56064 : int has_children = dwarf_abbrevhaschildren (&abbrev);
5034 :
5035 112128 : printf (gettext (" [%5u] offset: %" PRId64
5036 : ", children: %s, tag: %s\n"),
5037 : code, (int64_t) offset,
5038 : has_children ? yes_str : no_str,
5039 : dwarf_tag_name (tag));
5040 :
5041 56064 : size_t cnt = 0;
5042 : unsigned int name;
5043 : unsigned int form;
5044 : Dwarf_Sword data;
5045 : Dwarf_Off enoffset;
5046 328552 : while (dwarf_getabbrevattr_data (&abbrev, cnt, &name, &form,
5047 : &data, &enoffset) == 0)
5048 : {
5049 432848 : printf (" attr: %s, form: %s",
5050 : dwarf_attr_name (name), dwarf_form_name (form));
5051 216424 : if (form == DW_FORM_implicit_const)
5052 0 : printf (" (%" PRId64 ")", data);
5053 432848 : printf (", offset: %#" PRIx64 "\n", (uint64_t) enoffset);
5054 216424 : ++cnt;
5055 : }
5056 :
5057 56064 : offset += length;
5058 : }
5059 : }
5060 : }
5061 :
5062 :
5063 : static void
5064 2 : print_debug_addr_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
5065 : Ebl *ebl, GElf_Ehdr *ehdr,
5066 : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
5067 : {
5068 6 : printf (gettext ("\
5069 : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
5070 : elf_ndxscn (scn), section_name (ebl, ehdr, shdr),
5071 2 : (uint64_t) shdr->sh_offset);
5072 :
5073 2 : if (shdr->sh_size == 0)
5074 : return;
5075 :
5076 : /* We like to get the section from libdw to make sure they are relocated. */
5077 4 : Elf_Data *data = (dbg->sectiondata[IDX_debug_addr]
5078 2 : ?: elf_rawdata (scn, NULL));
5079 2 : if (unlikely (data == NULL))
5080 : {
5081 0 : error (0, 0, gettext ("cannot get .debug_addr section data: %s"),
5082 : elf_errmsg (-1));
5083 : return;
5084 : }
5085 :
5086 2 : size_t idx = 0;
5087 4 : sort_listptr (&known_addrbases, "addr_base");
5088 :
5089 2 : const unsigned char *start = (const unsigned char *) data->d_buf;
5090 2 : const unsigned char *readp = start;
5091 2 : const unsigned char *readendp = ((const unsigned char *) data->d_buf
5092 2 : + data->d_size);
5093 :
5094 8 : while (readp < readendp)
5095 : {
5096 : /* We cannot really know whether or not there is an header. The
5097 : DebugFission extension to DWARF4 doesn't add one. The DWARF5
5098 : .debug_addr variant does. Whether or not we have an header,
5099 : DW_AT_[GNU_]addr_base points at "index 0". So if the current
5100 : offset equals the CU addr_base then we can just start
5101 : printing addresses. If there is no CU with an exact match
5102 : then we'll try to parse the header first. */
5103 4 : Dwarf_Off off = (Dwarf_Off) (readp
5104 4 : - (const unsigned char *) data->d_buf);
5105 :
5106 4 : printf ("Table at offset %" PRIx64 " ", off);
5107 :
5108 8 : struct listptr *listptr = get_listptr (&known_addrbases, idx++);
5109 : const unsigned char *next_unitp;
5110 :
5111 : uint64_t unit_length;
5112 : uint16_t version;
5113 : uint8_t address_size;
5114 : uint8_t segment_size;
5115 4 : if (listptr == NULL)
5116 : {
5117 0 : error (0, 0, "Warning: No CU references .debug_addr after %" PRIx64,
5118 : off);
5119 :
5120 : /* We will have to assume it is just addresses to the end... */
5121 0 : address_size = ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 4 : 8;
5122 0 : next_unitp = readendp;
5123 : printf ("Unknown CU:\n");
5124 : }
5125 : else
5126 : {
5127 : Dwarf_Die cudie;
5128 4 : if (dwarf_cu_die (listptr->cu, &cudie,
5129 : NULL, NULL, NULL, NULL,
5130 : NULL, NULL) == NULL)
5131 0 : printf ("Unknown CU (%s):\n", dwarf_errmsg (-1));
5132 : else
5133 4 : printf ("for CU [%6" PRIx64 "]:\n", dwarf_dieoffset (&cudie));
5134 :
5135 4 : if (listptr->offset == off)
5136 : {
5137 2 : address_size = listptr_address_size (listptr);
5138 2 : segment_size = 0;
5139 2 : version = 4;
5140 :
5141 : /* The addresses start here, but where do they end? */
5142 3 : listptr = get_listptr (&known_addrbases, idx);
5143 1 : if (listptr == NULL)
5144 : next_unitp = readendp;
5145 1 : else if (listptr->cu->version < 5)
5146 : {
5147 1 : next_unitp = start + listptr->offset;
5148 1 : if (listptr->offset < off || listptr->offset > data->d_size)
5149 : {
5150 0 : error (0, 0,
5151 : "Warning: Bad address base for next unit at %"
5152 : PRIx64, off);
5153 0 : next_unitp = readendp;
5154 : }
5155 : }
5156 : else
5157 : {
5158 : /* Tricky, we don't have a header for this unit, but
5159 : there is one for the next. We will have to
5160 : "guess" how big it is and subtract it from the
5161 : offset (because that points after the header). */
5162 0 : unsigned int offset_size = listptr_offset_size (listptr);
5163 0 : Dwarf_Off next_off = (listptr->offset
5164 0 : - (offset_size == 4 ? 4 : 12) /* len */
5165 : - 2 /* version */
5166 : - 1 /* address size */
5167 0 : - 1); /* segment selector size */
5168 0 : next_unitp = start + next_off;
5169 0 : if (next_off < off || next_off > data->d_size)
5170 : {
5171 0 : error (0, 0,
5172 : "Warning: Couldn't calculate .debug_addr "
5173 : " unit lenght at %" PRIx64, off);
5174 0 : next_unitp = readendp;
5175 : }
5176 : }
5177 2 : unit_length = (uint64_t) (next_unitp - readp);
5178 :
5179 : /* Pretend we have a header. */
5180 2 : printf ("\n");
5181 4 : printf (gettext (" Length: %8" PRIu64 "\n"),
5182 : unit_length);
5183 4 : printf (gettext (" DWARF version: %8" PRIu16 "\n"), version);
5184 4 : printf (gettext (" Address size: %8" PRIu64 "\n"),
5185 : (uint64_t) address_size);
5186 4 : printf (gettext (" Segment size: %8" PRIu64 "\n"),
5187 : (uint64_t) segment_size);
5188 : printf ("\n");
5189 : }
5190 : else
5191 : {
5192 : /* OK, we have to parse an header first. */
5193 2 : unit_length = read_4ubyte_unaligned_inc (dbg, readp);
5194 2 : if (unlikely (unit_length == 0xffffffff))
5195 : {
5196 0 : if (unlikely (readp > readendp - 8))
5197 : {
5198 0 : invalid_data:
5199 0 : error (0, 0, "Invalid data");
5200 0 : return;
5201 : }
5202 0 : unit_length = read_8ubyte_unaligned_inc (dbg, readp);
5203 : }
5204 2 : printf ("\n");
5205 4 : printf (gettext (" Length: %8" PRIu64 "\n"),
5206 : unit_length);
5207 :
5208 : /* We need at least 2-bytes (version) + 1-byte
5209 : (addr_size) + 1-byte (segment_size) = 4 bytes to
5210 : complete the header. And this unit cannot go beyond
5211 : the section data. */
5212 2 : if (readp > readendp - 4
5213 2 : || unit_length < 4
5214 2 : || unit_length > (uint64_t) (readendp - readp))
5215 : goto invalid_data;
5216 :
5217 2 : next_unitp = readp + unit_length;
5218 :
5219 2 : version = read_2ubyte_unaligned_inc (dbg, readp);
5220 4 : printf (gettext (" DWARF version: %8" PRIu16 "\n"), version);
5221 :
5222 2 : if (version != 5)
5223 : {
5224 0 : error (0, 0, gettext ("Unknown version"));
5225 0 : goto next_unit;
5226 : }
5227 :
5228 2 : address_size = *readp++;
5229 4 : printf (gettext (" Address size: %8" PRIu64 "\n"),
5230 : (uint64_t) address_size);
5231 :
5232 2 : if (address_size != 4 && address_size != 8)
5233 : {
5234 0 : error (0, 0, gettext ("unsupported address size"));
5235 : goto next_unit;
5236 : }
5237 :
5238 2 : segment_size = *readp++;
5239 4 : printf (gettext (" Segment size: %8" PRIu64 "\n"),
5240 : (uint64_t) segment_size);
5241 2 : printf ("\n");
5242 :
5243 2 : if (segment_size != 0)
5244 : {
5245 0 : error (0, 0, gettext ("unsupported segment size"));
5246 : goto next_unit;
5247 : }
5248 :
5249 2 : if (listptr->offset != (Dwarf_Off) (readp - start))
5250 : {
5251 : error (0, 0, "Address index doesn't start after header");
5252 : goto next_unit;
5253 : }
5254 : }
5255 : }
5256 :
5257 4 : int digits = 1;
5258 4 : size_t addresses = (next_unitp - readp) / address_size;
5259 12 : while (addresses >= 10)
5260 : {
5261 4 : ++digits;
5262 4 : addresses /= 10;
5263 : }
5264 :
5265 4 : unsigned int index = 0;
5266 4 : size_t index_offset = readp - (const unsigned char *) data->d_buf;
5267 : printf (" Addresses start at offset 0x%zx:\n", index_offset);
5268 76 : while (readp <= next_unitp - address_size)
5269 : {
5270 72 : Dwarf_Addr addr = read_addr_unaligned_inc (address_size, dbg,
5271 : readp);
5272 144 : printf (" [%*u] ", digits, index++);
5273 72 : print_dwarf_addr (dwflmod, address_size, addr, addr);
5274 : printf ("\n");
5275 : }
5276 4 : printf ("\n");
5277 :
5278 4 : if (readp != next_unitp)
5279 0 : error (0, 0, "extra %zd bytes at end of unit",
5280 0 : (size_t) (next_unitp - readp));
5281 :
5282 4 : next_unit:
5283 : readp = next_unitp;
5284 : }
5285 : }
5286 :
5287 : /* Print content of DWARF .debug_aranges section. We fortunately do
5288 : not have to know a bit about the structure of the section, libdwarf
5289 : takes care of it. */
5290 : static void
5291 1 : print_decoded_aranges_section (Ebl *ebl, GElf_Ehdr *ehdr, Elf_Scn *scn,
5292 : GElf_Shdr *shdr, Dwarf *dbg)
5293 : {
5294 : Dwarf_Aranges *aranges;
5295 : size_t cnt;
5296 1 : if (unlikely (dwarf_getaranges (dbg, &aranges, &cnt) != 0))
5297 : {
5298 0 : error (0, 0, gettext ("cannot get .debug_aranges content: %s"),
5299 : dwarf_errmsg (-1));
5300 0 : return;
5301 : }
5302 :
5303 : GElf_Shdr glink_mem;
5304 : GElf_Shdr *glink;
5305 1 : glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link), &glink_mem);
5306 1 : if (glink == NULL)
5307 : {
5308 0 : error (0, 0, gettext ("invalid sh_link value in section %zu"),
5309 : elf_ndxscn (scn));
5310 : return;
5311 : }
5312 :
5313 4 : printf (ngettext ("\
5314 : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 " contains %zu entry:\n",
5315 : "\
5316 : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 " contains %zu entries:\n",
5317 : cnt),
5318 : elf_ndxscn (scn), section_name (ebl, ehdr, shdr),
5319 1 : (uint64_t) shdr->sh_offset, cnt);
5320 :
5321 : /* Compute floor(log16(cnt)). */
5322 1 : size_t tmp = cnt;
5323 1 : int digits = 1;
5324 2 : while (tmp >= 16)
5325 : {
5326 0 : ++digits;
5327 0 : tmp >>= 4;
5328 : }
5329 :
5330 5 : for (size_t n = 0; n < cnt; ++n)
5331 : {
5332 5 : Dwarf_Arange *runp = dwarf_onearange (aranges, n);
5333 5 : if (unlikely (runp == NULL))
5334 : {
5335 0 : printf ("cannot get arange %zu: %s\n", n, dwarf_errmsg (-1));
5336 0 : return;
5337 : }
5338 :
5339 : Dwarf_Addr start;
5340 : Dwarf_Word length;
5341 : Dwarf_Off offset;
5342 :
5343 5 : if (unlikely (dwarf_getarangeinfo (runp, &start, &length, &offset) != 0))
5344 0 : printf (gettext (" [%*zu] ???\n"), digits, n);
5345 : else
5346 10 : printf (gettext (" [%*zu] start: %0#*" PRIx64
5347 : ", length: %5" PRIu64 ", CU DIE offset: %6"
5348 : PRId64 "\n"),
5349 5 : digits, n, ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 10 : 18,
5350 : (uint64_t) start, (uint64_t) length, (int64_t) offset);
5351 : }
5352 : }
5353 :
5354 :
5355 : /* Print content of DWARF .debug_aranges section. */
5356 : static void
5357 38 : print_debug_aranges_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
5358 : Ebl *ebl, GElf_Ehdr *ehdr, Elf_Scn *scn,
5359 : GElf_Shdr *shdr, Dwarf *dbg)
5360 : {
5361 38 : if (decodedaranges)
5362 : {
5363 1 : print_decoded_aranges_section (ebl, ehdr, scn, shdr, dbg);
5364 1 : return;
5365 : }
5366 :
5367 74 : Elf_Data *data = (dbg->sectiondata[IDX_debug_aranges]
5368 37 : ?: elf_rawdata (scn, NULL));
5369 :
5370 37 : if (unlikely (data == NULL))
5371 : {
5372 0 : error (0, 0, gettext ("cannot get .debug_aranges content: %s"),
5373 : elf_errmsg (-1));
5374 : return;
5375 : }
5376 :
5377 111 : printf (gettext ("\
5378 : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
5379 : elf_ndxscn (scn), section_name (ebl, ehdr, shdr),
5380 37 : (uint64_t) shdr->sh_offset);
5381 :
5382 37 : const unsigned char *readp = data->d_buf;
5383 37 : const unsigned char *readendp = readp + data->d_size;
5384 :
5385 1140 : while (readp < readendp)
5386 : {
5387 1066 : const unsigned char *hdrstart = readp;
5388 1066 : size_t start_offset = hdrstart - (const unsigned char *) data->d_buf;
5389 :
5390 2132 : printf (gettext ("\nTable at offset %zu:\n"), start_offset);
5391 1066 : if (readp + 4 > readendp)
5392 : {
5393 0 : invalid_data:
5394 0 : error (0, 0, gettext ("invalid data in section [%zu] '%s'"),
5395 : elf_ndxscn (scn), section_name (ebl, ehdr, shdr));
5396 : return;
5397 : }
5398 :
5399 1086 : Dwarf_Word length = read_4ubyte_unaligned_inc (dbg, readp);
5400 1066 : unsigned int length_bytes = 4;
5401 1066 : if (length == DWARF3_LENGTH_64_BIT)
5402 : {
5403 0 : if (readp + 8 > readendp)
5404 : goto invalid_data;
5405 0 : length = read_8ubyte_unaligned_inc (dbg, readp);
5406 0 : length_bytes = 8;
5407 : }
5408 :
5409 1066 : const unsigned char *nexthdr = readp + length;
5410 2132 : printf (gettext ("\n Length: %6" PRIu64 "\n"),
5411 : (uint64_t) length);
5412 :
5413 1066 : if (unlikely (length > (size_t) (readendp - readp)))
5414 : goto invalid_data;
5415 :
5416 1066 : if (length == 0)
5417 0 : continue;
5418 :
5419 1066 : if (readp + 2 > readendp)
5420 : goto invalid_data;
5421 1066 : uint_fast16_t version = read_2ubyte_unaligned_inc (dbg, readp);
5422 2132 : printf (gettext (" DWARF version: %6" PRIuFAST16 "\n"),
5423 : version);
5424 1066 : if (version != 2)
5425 : {
5426 0 : error (0, 0, gettext ("unsupported aranges version"));
5427 : goto next_table;
5428 : }
5429 :
5430 : Dwarf_Word offset;
5431 1066 : if (readp + length_bytes > readendp)
5432 : goto invalid_data;
5433 1066 : if (length_bytes == 8)
5434 0 : offset = read_8ubyte_unaligned_inc (dbg, readp);
5435 : else
5436 1086 : offset = read_4ubyte_unaligned_inc (dbg, readp);
5437 2132 : printf (gettext (" CU offset: %6" PRIx64 "\n"),
5438 : (uint64_t) offset);
5439 :
5440 1066 : if (readp + 1 > readendp)
5441 : goto invalid_data;
5442 1066 : unsigned int address_size = *readp++;
5443 2132 : printf (gettext (" Address size: %6" PRIu64 "\n"),
5444 : (uint64_t) address_size);
5445 1066 : if (address_size != 4 && address_size != 8)
5446 : {
5447 0 : error (0, 0, gettext ("unsupported address size"));
5448 : goto next_table;
5449 : }
5450 :
5451 1066 : unsigned int segment_size = *readp++;
5452 2132 : printf (gettext (" Segment size: %6" PRIu64 "\n\n"),
5453 : (uint64_t) segment_size);
5454 1066 : if (segment_size != 0 && segment_size != 4 && segment_size != 8)
5455 : {
5456 0 : error (0, 0, gettext ("unsupported segment size"));
5457 : goto next_table;
5458 : }
5459 :
5460 : /* Round the address to the next multiple of 2*address_size. */
5461 2132 : readp += ((2 * address_size - ((readp - hdrstart) % (2 * address_size)))
5462 1066 : % (2 * address_size));
5463 :
5464 3249 : while (readp < nexthdr)
5465 : {
5466 : Dwarf_Word range_address;
5467 : Dwarf_Word range_length;
5468 2183 : Dwarf_Word segment = 0;
5469 2183 : if (readp + 2 * address_size + segment_size > readendp)
5470 : goto invalid_data;
5471 2183 : if (address_size == 4)
5472 : {
5473 56 : range_address = read_4ubyte_unaligned_inc (dbg, readp);
5474 56 : range_length = read_4ubyte_unaligned_inc (dbg, readp);
5475 : }
5476 : else
5477 : {
5478 2163 : range_address = read_8ubyte_unaligned_inc (dbg, readp);
5479 2163 : range_length = read_8ubyte_unaligned_inc (dbg, readp);
5480 : }
5481 :
5482 2183 : if (segment_size == 4)
5483 0 : segment = read_4ubyte_unaligned_inc (dbg, readp);
5484 2183 : else if (segment_size == 8)
5485 0 : segment = read_8ubyte_unaligned_inc (dbg, readp);
5486 :
5487 2183 : if (range_address == 0 && range_length == 0 && segment == 0)
5488 : break;
5489 :
5490 1117 : printf (" ");
5491 1117 : print_dwarf_addr (dwflmod, address_size, range_address,
5492 : range_address);
5493 1117 : printf ("..");
5494 1117 : print_dwarf_addr (dwflmod, address_size,
5495 1117 : range_address + range_length - 1,
5496 : range_length);
5497 1117 : if (segment_size != 0)
5498 : printf (" (%" PRIx64 ")\n", (uint64_t) segment);
5499 : else
5500 : printf ("\n");
5501 : }
5502 :
5503 1066 : next_table:
5504 1066 : if (readp != nexthdr)
5505 : {
5506 0 : size_t padding = nexthdr - readp;
5507 0 : printf (gettext (" %zu padding bytes\n"), padding);
5508 0 : readp = nexthdr;
5509 : }
5510 : }
5511 : }
5512 :
5513 :
5514 : static bool is_split_dwarf (Dwarf *dbg, uint64_t *id, Dwarf_CU **split_cu);
5515 :
5516 : /* Returns true and sets cu and cu_base if the given Dwarf is a split
5517 : DWARF (.dwo) file. */
5518 : static bool
5519 0 : split_dwarf_cu_base (Dwarf *dbg, Dwarf_CU **cu, Dwarf_Addr *cu_base)
5520 : {
5521 : uint64_t id;
5522 0 : if (is_split_dwarf (dbg, &id, cu))
5523 : {
5524 : Dwarf_Die cudie;
5525 0 : if (dwarf_cu_info (*cu, NULL, NULL, &cudie, NULL, NULL, NULL, NULL) == 0)
5526 : {
5527 0 : *cu_base = cudie_base (&cudie);
5528 0 : return true;
5529 : }
5530 : }
5531 : return false;
5532 : }
5533 :
5534 : /* Print content of DWARF .debug_rnglists section. */
5535 : static void
5536 2 : print_debug_rnglists_section (Dwfl_Module *dwflmod,
5537 : Ebl *ebl, GElf_Ehdr *ehdr,
5538 : Elf_Scn *scn, GElf_Shdr *shdr,
5539 : Dwarf *dbg __attribute__((unused)))
5540 : {
5541 6 : printf (gettext ("\
5542 : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
5543 : elf_ndxscn (scn), section_name (ebl, ehdr, shdr),
5544 2 : (uint64_t) shdr->sh_offset);
5545 :
5546 4 : Elf_Data *data =(dbg->sectiondata[IDX_debug_rnglists]
5547 2 : ?: elf_rawdata (scn, NULL));
5548 2 : if (unlikely (data == NULL))
5549 : {
5550 0 : error (0, 0, gettext ("cannot get .debug_rnglists content: %s"),
5551 : elf_errmsg (-1));
5552 0 : return;
5553 : }
5554 :
5555 : /* For the listptr to get the base address/CU. */
5556 4 : sort_listptr (&known_rnglistptr, "rnglistptr");
5557 2 : size_t listptr_idx = 0;
5558 :
5559 2 : const unsigned char *readp = data->d_buf;
5560 2 : const unsigned char *const dataend = ((unsigned char *) data->d_buf
5561 2 : + data->d_size);
5562 6 : while (readp < dataend)
5563 : {
5564 2 : if (unlikely (readp > dataend - 4))
5565 : {
5566 0 : invalid_data:
5567 0 : error (0, 0, gettext ("invalid data in section [%zu] '%s'"),
5568 : elf_ndxscn (scn), section_name (ebl, ehdr, shdr));
5569 0 : return;
5570 : }
5571 :
5572 2 : ptrdiff_t offset = readp - (unsigned char *) data->d_buf;
5573 4 : printf (gettext ("Table at Offset 0x%" PRIx64 ":\n\n"),
5574 : (uint64_t) offset);
5575 :
5576 2 : uint64_t unit_length = read_4ubyte_unaligned_inc (dbg, readp);
5577 2 : unsigned int offset_size = 4;
5578 2 : if (unlikely (unit_length == 0xffffffff))
5579 : {
5580 0 : if (unlikely (readp > dataend - 8))
5581 : goto invalid_data;
5582 :
5583 0 : unit_length = read_8ubyte_unaligned_inc (dbg, readp);
5584 0 : offset_size = 8;
5585 : }
5586 4 : printf (gettext (" Length: %8" PRIu64 "\n"), unit_length);
5587 :
5588 : /* We need at least 2-bytes + 1-byte + 1-byte + 4-bytes = 8
5589 : bytes to complete the header. And this unit cannot go beyond
5590 : the section data. */
5591 2 : if (readp > dataend - 8
5592 2 : || unit_length < 8
5593 2 : || unit_length > (uint64_t) (dataend - readp))
5594 : goto invalid_data;
5595 :
5596 2 : const unsigned char *nexthdr = readp + unit_length;
5597 :
5598 2 : uint16_t version = read_2ubyte_unaligned_inc (dbg, readp);
5599 4 : printf (gettext (" DWARF version: %8" PRIu16 "\n"), version);
5600 :
5601 2 : if (version != 5)
5602 : {
5603 0 : error (0, 0, gettext ("Unknown version"));
5604 : goto next_table;
5605 : }
5606 :
5607 2 : uint8_t address_size = *readp++;
5608 4 : printf (gettext (" Address size: %8" PRIu64 "\n"),
5609 : (uint64_t) address_size);
5610 :
5611 2 : if (address_size != 4 && address_size != 8)
5612 : {
5613 0 : error (0, 0, gettext ("unsupported address size"));
5614 : goto next_table;
5615 : }
5616 :
5617 2 : uint8_t segment_size = *readp++;
5618 4 : printf (gettext (" Segment size: %8" PRIu64 "\n"),
5619 : (uint64_t) segment_size);
5620 :
5621 2 : if (segment_size != 0 && segment_size != 4 && segment_size != 8)
5622 : {
5623 0 : error (0, 0, gettext ("unsupported segment size"));
5624 : goto next_table;
5625 : }
5626 :
5627 2 : uint32_t offset_entry_count = read_4ubyte_unaligned_inc (dbg, readp);
5628 4 : printf (gettext (" Offset entries: %8" PRIu64 "\n"),
5629 : (uint64_t) offset_entry_count);
5630 :
5631 : /* We need the CU that uses this unit to get the initial base address. */
5632 2 : Dwarf_Addr cu_base = 0;
5633 2 : struct Dwarf_CU *cu = NULL;
5634 2 : if (listptr_cu (&known_rnglistptr, &listptr_idx,
5635 : (Dwarf_Off) offset,
5636 2 : (Dwarf_Off) (nexthdr - (unsigned char *) data->d_buf),
5637 : &cu_base, &cu)
5638 0 : || split_dwarf_cu_base (dbg, &cu, &cu_base))
5639 2 : {
5640 : Dwarf_Die cudie;
5641 2 : if (dwarf_cu_die (cu, &cudie,
5642 : NULL, NULL, NULL, NULL,
5643 : NULL, NULL) == NULL)
5644 0 : printf (gettext (" Unknown CU base: "));
5645 : else
5646 2 : printf (gettext (" CU [%6" PRIx64 "] base: "),
5647 : dwarf_dieoffset (&cudie));
5648 2 : print_dwarf_addr (dwflmod, address_size, cu_base, cu_base);
5649 2 : printf ("\n");
5650 : }
5651 : else
5652 0 : printf (gettext (" Not associated with a CU.\n"));
5653 :
5654 2 : printf ("\n");
5655 :
5656 2 : const unsigned char *offset_array_start = readp;
5657 2 : if (offset_entry_count > 0)
5658 : {
5659 1 : uint64_t max_entries = (unit_length - 8) / offset_size;
5660 1 : if (offset_entry_count > max_entries)
5661 : {
5662 0 : error (0, 0,
5663 0 : gettext ("too many offset entries for unit length"));
5664 0 : offset_entry_count = max_entries;
5665 : }
5666 :
5667 2 : printf (gettext (" Offsets starting at 0x%" PRIx64 ":\n"),
5668 : (uint64_t) (offset_array_start
5669 1 : - (unsigned char *) data->d_buf));
5670 3 : for (uint32_t idx = 0; idx < offset_entry_count; idx++)
5671 : {
5672 2 : printf (" [%6" PRIu32 "] ", idx);
5673 2 : if (offset_size == 4)
5674 : {
5675 2 : uint32_t off = read_4ubyte_unaligned_inc (dbg, readp);
5676 : printf ("0x%" PRIx32 "\n", off);
5677 : }
5678 : else
5679 : {
5680 0 : uint64_t off = read_8ubyte_unaligned_inc (dbg, readp);
5681 : printf ("0x%" PRIx64 "\n", off);
5682 : }
5683 : }
5684 : printf ("\n");
5685 : }
5686 :
5687 2 : Dwarf_Addr base = cu_base;
5688 2 : bool start_of_list = true;
5689 18 : while (readp < nexthdr)
5690 : {
5691 14 : uint8_t kind = *readp++;
5692 : uint64_t op1, op2;
5693 :
5694 : /* Skip padding. */
5695 14 : if (start_of_list && kind == DW_RLE_end_of_list)
5696 0 : continue;
5697 :
5698 14 : if (start_of_list)
5699 : {
5700 4 : base = cu_base;
5701 12 : printf (" Offset: %" PRIx64 ", Index: %" PRIx64 "\n",
5702 4 : (uint64_t) (readp - (unsigned char *) data->d_buf - 1),
5703 4 : (uint64_t) (readp - offset_array_start - 1));
5704 4 : start_of_list = false;
5705 : }
5706 :
5707 28 : printf (" %s", dwarf_range_list_encoding_name (kind));
5708 14 : switch (kind)
5709 : {
5710 4 : case DW_RLE_end_of_list:
5711 4 : start_of_list = true;
5712 : printf ("\n\n");
5713 : break;
5714 :
5715 0 : case DW_RLE_base_addressx:
5716 0 : if ((uint64_t) (nexthdr - readp) < 1)
5717 : {
5718 0 : invalid_range:
5719 0 : error (0, 0, gettext ("invalid range list data"));
5720 : goto next_table;
5721 : }
5722 0 : get_uleb128 (op1, readp, nexthdr);
5723 0 : printf (" %" PRIx64 "\n", op1);
5724 0 : if (! print_unresolved_addresses)
5725 : {
5726 : Dwarf_Addr addr;
5727 0 : if (get_indexed_addr (cu, op1, &addr) != 0)
5728 : printf (" ???\n");
5729 : else
5730 : {
5731 0 : printf (" ");
5732 0 : print_dwarf_addr (dwflmod, address_size, addr, addr);
5733 : printf ("\n");
5734 : }
5735 : }
5736 : break;
5737 :
5738 0 : case DW_RLE_startx_endx:
5739 0 : if ((uint64_t) (nexthdr - readp) < 1)
5740 : goto invalid_range;
5741 0 : get_uleb128 (op1, readp, nexthdr);
5742 0 : if ((uint64_t) (nexthdr - readp) < 1)
5743 : goto invalid_range;
5744 0 : get_uleb128 (op2, readp, nexthdr);
5745 0 : printf (" %" PRIx64 ", %" PRIx64 "\n", op1, op2);
5746 0 : if (! print_unresolved_addresses)
5747 : {
5748 : Dwarf_Addr addr1;
5749 : Dwarf_Addr addr2;
5750 0 : if (get_indexed_addr (cu, op1, &addr1) != 0
5751 0 : || get_indexed_addr (cu, op2, &addr2) != 0)
5752 : {
5753 0 : printf (" ???..\n");
5754 : printf (" ???\n");
5755 : }
5756 : else
5757 : {
5758 0 : printf (" ");
5759 0 : print_dwarf_addr (dwflmod, address_size, addr1, addr1);
5760 0 : printf ("..\n ");
5761 0 : print_dwarf_addr (dwflmod, address_size,
5762 : addr2 - 1, addr2);
5763 : printf ("\n");
5764 : }
5765 : }
5766 : break;
5767 :
5768 0 : case DW_RLE_startx_length:
5769 0 : if ((uint64_t) (nexthdr - readp) < 1)
5770 : goto invalid_range;
5771 0 : get_uleb128 (op1, readp, nexthdr);
5772 0 : if ((uint64_t) (nexthdr - readp) < 1)
5773 : goto invalid_range;
5774 0 : get_uleb128 (op2, readp, nexthdr);
5775 0 : printf (" %" PRIx64 ", %" PRIx64 "\n", op1, op2);
5776 0 : if (! print_unresolved_addresses)
5777 : {
5778 : Dwarf_Addr addr1;
5779 : Dwarf_Addr addr2;
5780 0 : if (get_indexed_addr (cu, op1, &addr1) != 0)
5781 : {
5782 0 : printf (" ???..\n");
5783 : printf (" ???\n");
5784 : }
5785 : else
5786 : {
5787 0 : addr2 = addr1 + op2;
5788 0 : printf (" ");
5789 0 : print_dwarf_addr (dwflmod, address_size, addr1, addr1);
5790 0 : printf ("..\n ");
5791 0 : print_dwarf_addr (dwflmod, address_size,
5792 : addr2 - 1, addr2);
5793 : printf ("\n");
5794 : }
5795 : }
5796 : break;
5797 :
5798 4 : case DW_RLE_offset_pair:
5799 4 : if ((uint64_t) (nexthdr - readp) < 1)
5800 : goto invalid_range;
5801 4 : get_uleb128 (op1, readp, nexthdr);
5802 4 : if ((uint64_t) (nexthdr - readp) < 1)
5803 : goto invalid_range;
5804 4 : get_uleb128 (op2, readp, nexthdr);
5805 4 : printf (" %" PRIx64 ", %" PRIx64 "\n", op1, op2);
5806 4 : if (! print_unresolved_addresses)
5807 : {
5808 4 : op1 += base;
5809 4 : op2 += base;
5810 4 : printf (" ");
5811 4 : print_dwarf_addr (dwflmod, address_size, op1, op1);
5812 4 : printf ("..\n ");
5813 4 : print_dwarf_addr (dwflmod, address_size, op2 - 1, op2);
5814 : printf ("\n");
5815 : }
5816 : break;
5817 :
5818 2 : case DW_RLE_base_address:
5819 2 : if (address_size == 4)
5820 : {
5821 0 : if ((uint64_t) (nexthdr - readp) < 4)
5822 : goto invalid_range;
5823 0 : op1 = read_4ubyte_unaligned_inc (dbg, readp);
5824 : }
5825 : else
5826 : {
5827 2 : if ((uint64_t) (nexthdr - readp) < 8)
5828 : goto invalid_range;
5829 2 : op1 = read_8ubyte_unaligned_inc (dbg, readp);
5830 : }
5831 2 : base = op1;
5832 2 : printf (" 0x%" PRIx64 "\n", base);
5833 2 : if (! print_unresolved_addresses)
5834 : {
5835 2 : printf (" ");
5836 2 : print_dwarf_addr (dwflmod, address_size, base, base);
5837 : printf ("\n");
5838 : }
5839 : break;
5840 :
5841 0 : case DW_RLE_start_end:
5842 0 : if (address_size == 4)
5843 : {
5844 0 : if ((uint64_t) (nexthdr - readp) < 8)
5845 : goto invalid_range;
5846 0 : op1 = read_4ubyte_unaligned_inc (dbg, readp);
5847 0 : op2 = read_4ubyte_unaligned_inc (dbg, readp);
5848 : }
5849 : else
5850 : {
5851 0 : if ((uint64_t) (nexthdr - readp) < 16)
5852 : goto invalid_range;
5853 0 : op1 = read_8ubyte_unaligned_inc (dbg, readp);
5854 0 : op2 = read_8ubyte_unaligned_inc (dbg, readp);
5855 : }
5856 0 : printf (" 0x%" PRIx64 "..0x%" PRIx64 "\n", op1, op2);
5857 0 : if (! print_unresolved_addresses)
5858 : {
5859 0 : printf (" ");
5860 0 : print_dwarf_addr (dwflmod, address_size, op1, op1);
5861 0 : printf ("..\n ");
5862 0 : print_dwarf_addr (dwflmod, address_size, op2 - 1, op2);
5863 : printf ("\n");
5864 : }
5865 : break;
5866 :
5867 4 : case DW_RLE_start_length:
5868 4 : if (address_size == 4)
5869 : {
5870 0 : if ((uint64_t) (nexthdr - readp) < 4)
5871 : goto invalid_range;
5872 0 : op1 = read_4ubyte_unaligned_inc (dbg, readp);
5873 : }
5874 : else
5875 : {
5876 4 : if ((uint64_t) (nexthdr - readp) < 8)
5877 : goto invalid_range;
5878 4 : op1 = read_8ubyte_unaligned_inc (dbg, readp);
5879 : }
5880 4 : if ((uint64_t) (nexthdr - readp) < 1)
5881 : goto invalid_range;
5882 4 : get_uleb128 (op2, readp, nexthdr);
5883 4 : printf (" 0x%" PRIx64 ", %" PRIx64 "\n", op1, op2);
5884 4 : if (! print_unresolved_addresses)
5885 : {
5886 4 : op2 = op1 + op2;
5887 4 : printf (" ");
5888 4 : print_dwarf_addr (dwflmod, address_size, op1, op1);
5889 4 : printf ("..\n ");
5890 4 : print_dwarf_addr (dwflmod, address_size, op2 - 1, op2);
5891 : printf ("\n");
5892 : }
5893 : break;
5894 :
5895 : default:
5896 : goto invalid_range;
5897 : }
5898 : }
5899 :
5900 2 : next_table:
5901 2 : if (readp != nexthdr)
5902 : {
5903 0 : size_t padding = nexthdr - readp;
5904 0 : printf (gettext (" %zu padding bytes\n\n"), padding);
5905 0 : readp = nexthdr;
5906 : }
5907 : }
5908 : }
5909 :
5910 : /* Print content of DWARF .debug_ranges section. */
5911 : static void
5912 28 : print_debug_ranges_section (Dwfl_Module *dwflmod,
5913 : Ebl *ebl, GElf_Ehdr *ehdr,
5914 : Elf_Scn *scn, GElf_Shdr *shdr,
5915 : Dwarf *dbg)
5916 : {
5917 56 : Elf_Data *data = (dbg->sectiondata[IDX_debug_ranges]
5918 28 : ?: elf_rawdata (scn, NULL));
5919 28 : if (unlikely (data == NULL))
5920 : {
5921 0 : error (0, 0, gettext ("cannot get .debug_ranges content: %s"),
5922 : elf_errmsg (-1));
5923 0 : return;
5924 : }
5925 :
5926 84 : printf (gettext ("\
5927 : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
5928 : elf_ndxscn (scn), section_name (ebl, ehdr, shdr),
5929 28 : (uint64_t) shdr->sh_offset);
5930 :
5931 56 : sort_listptr (&known_rangelistptr, "rangelistptr");
5932 28 : size_t listptr_idx = 0;
5933 :
5934 28 : uint_fast8_t address_size = ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 4 : 8;
5935 :
5936 28 : bool first = true;
5937 28 : Dwarf_Addr base = 0;
5938 28 : unsigned char *const endp = (unsigned char *) data->d_buf + data->d_size;
5939 28 : unsigned char *readp = data->d_buf;
5940 28 : Dwarf_CU *last_cu = NULL;
5941 36850 : while (readp < endp)
5942 : {
5943 36794 : ptrdiff_t offset = readp - (unsigned char *) data->d_buf;
5944 36794 : Dwarf_CU *cu = last_cu;
5945 :
5946 36794 : if (first && skip_listptr_hole (&known_rangelistptr, &listptr_idx,
5947 : &address_size, NULL, &base, &cu,
5948 : offset, &readp, endp, NULL))
5949 0 : continue;
5950 :
5951 36794 : if (last_cu != cu)
5952 : {
5953 : Dwarf_Die cudie;
5954 562 : if (dwarf_cu_die (cu, &cudie,
5955 : NULL, NULL, NULL, NULL,
5956 : NULL, NULL) == NULL)
5957 0 : printf (gettext ("\n Unknown CU base: "));
5958 : else
5959 562 : printf (gettext ("\n CU [%6" PRIx64 "] base: "),
5960 : dwarf_dieoffset (&cudie));
5961 562 : print_dwarf_addr (dwflmod, address_size, base, base);
5962 562 : printf ("\n");
5963 : }
5964 36794 : last_cu = cu;
5965 :
5966 36794 : if (unlikely (data->d_size - offset < (size_t) address_size * 2))
5967 : {
5968 0 : printf (gettext (" [%6tx] <INVALID DATA>\n"), offset);
5969 0 : break;
5970 : }
5971 :
5972 : Dwarf_Addr begin;
5973 : Dwarf_Addr end;
5974 36794 : if (address_size == 8)
5975 : {
5976 36794 : begin = read_8ubyte_unaligned_inc (dbg, readp);
5977 36794 : end = read_8ubyte_unaligned_inc (dbg, readp);
5978 : }
5979 : else
5980 : {
5981 0 : begin = read_4ubyte_unaligned_inc (dbg, readp);
5982 0 : end = read_4ubyte_unaligned_inc (dbg, readp);
5983 0 : if (begin == (Dwarf_Addr) (uint32_t) -1)
5984 : begin = (Dwarf_Addr) -1l;
5985 : }
5986 :
5987 36794 : if (begin == (Dwarf_Addr) -1l) /* Base address entry. */
5988 : {
5989 0 : printf (gettext (" [%6tx] base address\n "), offset);
5990 0 : print_dwarf_addr (dwflmod, address_size, end, end);
5991 0 : printf ("\n");
5992 0 : base = end;
5993 : }
5994 36794 : else if (begin == 0 && end == 0) /* End of list entry. */
5995 : {
5996 8642 : if (first)
5997 0 : printf (gettext (" [%6tx] empty list\n"), offset);
5998 : first = true;
5999 : }
6000 : else
6001 : {
6002 : /* We have an address range entry. */
6003 28152 : if (first) /* First address range entry in a list. */
6004 : printf (" [%6tx] ", offset);
6005 : else
6006 : printf (" ");
6007 :
6008 28152 : printf ("range %" PRIx64 ", %" PRIx64 "\n", begin, end);
6009 28152 : if (! print_unresolved_addresses)
6010 : {
6011 28142 : printf (" ");
6012 28142 : print_dwarf_addr (dwflmod, address_size, base + begin,
6013 : base + begin);
6014 28142 : printf ("..\n ");
6015 28142 : print_dwarf_addr (dwflmod, address_size,
6016 : base + end - 1, base + end);
6017 : printf ("\n");
6018 : }
6019 :
6020 : first = false;
6021 : }
6022 : }
6023 : }
6024 :
6025 : #define REGNAMESZ 16
6026 : static const char *
6027 9427 : register_info (Ebl *ebl, unsigned int regno, const Ebl_Register_Location *loc,
6028 : char name[REGNAMESZ], int *bits, int *type)
6029 : {
6030 : const char *set;
6031 : const char *pfx;
6032 : int ignore;
6033 9427 : ssize_t n = ebl_register_info (ebl, regno, name, REGNAMESZ, &pfx, &set,
6034 : bits ?: &ignore, type ?: &ignore);
6035 9427 : if (n <= 0)
6036 : {
6037 0 : if (loc != NULL)
6038 0 : snprintf (name, REGNAMESZ, "reg%u", loc->regno);
6039 : else
6040 : snprintf (name, REGNAMESZ, "??? 0x%x", regno);
6041 0 : if (bits != NULL)
6042 0 : *bits = loc != NULL ? loc->bits : 0;
6043 0 : if (type != NULL)
6044 0 : *type = DW_ATE_unsigned;
6045 0 : set = "??? unrecognized";
6046 : }
6047 : else
6048 : {
6049 9427 : if (bits != NULL && *bits <= 0)
6050 0 : *bits = loc != NULL ? loc->bits : 0;
6051 9427 : if (type != NULL && *type == DW_ATE_void)
6052 0 : *type = DW_ATE_unsigned;
6053 :
6054 : }
6055 9427 : return set;
6056 : }
6057 :
6058 : static const unsigned char *
6059 28 : read_encoded (unsigned int encoding, const unsigned char *readp,
6060 : const unsigned char *const endp, uint64_t *res, Dwarf *dbg)
6061 : {
6062 28 : if ((encoding & 0xf) == DW_EH_PE_absptr)
6063 0 : encoding = gelf_getclass (dbg->elf) == ELFCLASS32
6064 0 : ? DW_EH_PE_udata4 : DW_EH_PE_udata8;
6065 :
6066 28 : switch (encoding & 0xf)
6067 : {
6068 0 : case DW_EH_PE_uleb128:
6069 0 : get_uleb128 (*res, readp, endp);
6070 : break;
6071 0 : case DW_EH_PE_sleb128:
6072 0 : get_sleb128 (*res, readp, endp);
6073 : break;
6074 0 : case DW_EH_PE_udata2:
6075 0 : if (readp + 2 > endp)
6076 : goto invalid;
6077 0 : *res = read_2ubyte_unaligned_inc (dbg, readp);
6078 : break;
6079 14 : case DW_EH_PE_udata4:
6080 14 : if (readp + 4 > endp)
6081 : goto invalid;
6082 14 : *res = read_4ubyte_unaligned_inc (dbg, readp);
6083 : break;
6084 0 : case DW_EH_PE_udata8:
6085 0 : if (readp + 8 > endp)
6086 : goto invalid;
6087 0 : *res = read_8ubyte_unaligned_inc (dbg, readp);
6088 : break;
6089 0 : case DW_EH_PE_sdata2:
6090 0 : if (readp + 2 > endp)
6091 : goto invalid;
6092 0 : *res = read_2sbyte_unaligned_inc (dbg, readp);
6093 : break;
6094 14 : case DW_EH_PE_sdata4:
6095 14 : if (readp + 4 > endp)
6096 : goto invalid;
6097 14 : *res = read_4sbyte_unaligned_inc (dbg, readp);
6098 : break;
6099 0 : case DW_EH_PE_sdata8:
6100 0 : if (readp + 8 > endp)
6101 : goto invalid;
6102 0 : *res = read_8sbyte_unaligned_inc (dbg, readp);
6103 : break;
6104 : default:
6105 0 : invalid:
6106 : error (1, 0,
6107 0 : gettext ("invalid encoding"));
6108 : }
6109 :
6110 28 : return readp;
6111 : }
6112 :
6113 :
6114 : static void
6115 4535 : print_cfa_program (const unsigned char *readp, const unsigned char *const endp,
6116 : Dwarf_Word vma_base, unsigned int code_align,
6117 : int data_align,
6118 : unsigned int version, unsigned int ptr_size,
6119 : unsigned int encoding,
6120 : Dwfl_Module *dwflmod, Ebl *ebl, Dwarf *dbg)
6121 : {
6122 : char regnamebuf[REGNAMESZ];
6123 : const char *regname (unsigned int regno)
6124 : {
6125 8641 : register_info (ebl, regno, NULL, regnamebuf, NULL, NULL);
6126 : return regnamebuf;
6127 : }
6128 :
6129 4535 : puts ("\n Program:");
6130 4535 : Dwarf_Word pc = vma_base;
6131 83236 : while (readp < endp)
6132 : {
6133 74166 : unsigned int opcode = *readp++;
6134 :
6135 74166 : if (opcode < DW_CFA_advance_loc)
6136 : /* Extended opcode. */
6137 42782 : switch (opcode)
6138 : {
6139 : uint64_t op1;
6140 : int64_t sop1;
6141 : uint64_t op2;
6142 : int64_t sop2;
6143 :
6144 12725 : case DW_CFA_nop:
6145 12725 : puts (" nop");
6146 55507 : break;
6147 0 : case DW_CFA_set_loc:
6148 0 : if ((uint64_t) (endp - readp) < 1)
6149 : goto invalid;
6150 0 : readp = read_encoded (encoding, readp, endp, &op1, dbg);
6151 0 : printf (" set_loc %#" PRIx64 " to %#" PRIx64 "\n",
6152 0 : op1, pc = vma_base + op1);
6153 : break;
6154 1605 : case DW_CFA_advance_loc1:
6155 1605 : if ((uint64_t) (endp - readp) < 1)
6156 : goto invalid;
6157 3210 : printf (" advance_loc1 %u to %#" PRIx64 "\n",
6158 1605 : *readp, pc += *readp * code_align);
6159 1605 : ++readp;
6160 1605 : break;
6161 734 : case DW_CFA_advance_loc2:
6162 734 : if ((uint64_t) (endp - readp) < 2)
6163 : goto invalid;
6164 734 : op1 = read_2ubyte_unaligned_inc (dbg, readp);
6165 734 : printf (" advance_loc2 %" PRIu64 " to %#" PRIx64 "\n",
6166 734 : op1, pc += op1 * code_align);
6167 : break;
6168 24 : case DW_CFA_advance_loc4:
6169 24 : if ((uint64_t) (endp - readp) < 4)
6170 : goto invalid;
6171 48 : op1 = read_4ubyte_unaligned_inc (dbg, readp);
6172 24 : printf (" advance_loc4 %" PRIu64 " to %#" PRIx64 "\n",
6173 24 : op1, pc += op1 * code_align);
6174 : break;
6175 4 : case DW_CFA_offset_extended:
6176 4 : if ((uint64_t) (endp - readp) < 1)
6177 : goto invalid;
6178 4 : get_uleb128 (op1, readp, endp);
6179 4 : if ((uint64_t) (endp - readp) < 1)
6180 : goto invalid;
6181 4 : get_uleb128 (op2, readp, endp);
6182 8 : printf (" offset_extended r%" PRIu64 " (%s) at cfa%+" PRId64
6183 : "\n",
6184 : op1, regname (op1), op2 * data_align);
6185 : break;
6186 0 : case DW_CFA_restore_extended:
6187 0 : if ((uint64_t) (endp - readp) < 1)
6188 : goto invalid;
6189 0 : get_uleb128 (op1, readp, endp);
6190 0 : printf (" restore_extended r%" PRIu64 " (%s)\n",
6191 : op1, regname (op1));
6192 : break;
6193 6 : case DW_CFA_undefined:
6194 6 : if ((uint64_t) (endp - readp) < 1)
6195 : goto invalid;
6196 6 : get_uleb128 (op1, readp, endp);
6197 12 : printf (" undefined r%" PRIu64 " (%s)\n", op1, regname (op1));
6198 : break;
6199 0 : case DW_CFA_same_value:
6200 0 : if ((uint64_t) (endp - readp) < 1)
6201 : goto invalid;
6202 0 : get_uleb128 (op1, readp, endp);
6203 0 : printf (" same_value r%" PRIu64 " (%s)\n", op1, regname (op1));
6204 : break;
6205 0 : case DW_CFA_register:
6206 0 : if ((uint64_t) (endp - readp) < 1)
6207 : goto invalid;
6208 0 : get_uleb128 (op1, readp, endp);
6209 0 : if ((uint64_t) (endp - readp) < 1)
6210 : goto invalid;
6211 0 : get_uleb128 (op2, readp, endp);
6212 0 : printf (" register r%" PRIu64 " (%s) in r%" PRIu64 " (%s)\n",
6213 : op1, regname (op1), op2, regname (op2));
6214 : break;
6215 2514 : case DW_CFA_remember_state:
6216 2514 : puts (" remember_state");
6217 2514 : break;
6218 2514 : case DW_CFA_restore_state:
6219 2514 : puts (" restore_state");
6220 2514 : break;
6221 114 : case DW_CFA_def_cfa:
6222 114 : if ((uint64_t) (endp - readp) < 1)
6223 : goto invalid;
6224 114 : get_uleb128 (op1, readp, endp);
6225 114 : if ((uint64_t) (endp - readp) < 1)
6226 : goto invalid;
6227 114 : get_uleb128 (op2, readp, endp);
6228 228 : printf (" def_cfa r%" PRIu64 " (%s) at offset %" PRIu64 "\n",
6229 : op1, regname (op1), op2);
6230 : break;
6231 38 : case DW_CFA_def_cfa_register:
6232 38 : if ((uint64_t) (endp - readp) < 1)
6233 : goto invalid;
6234 38 : get_uleb128 (op1, readp, endp);
6235 76 : printf (" def_cfa_register r%" PRIu64 " (%s)\n",
6236 : op1, regname (op1));
6237 : break;
6238 22476 : case DW_CFA_def_cfa_offset:
6239 22476 : if ((uint64_t) (endp - readp) < 1)
6240 : goto invalid;
6241 22476 : get_uleb128 (op1, readp, endp);
6242 22476 : printf (" def_cfa_offset %" PRIu64 "\n", op1);
6243 : break;
6244 12 : case DW_CFA_def_cfa_expression:
6245 12 : if ((uint64_t) (endp - readp) < 1)
6246 : goto invalid;
6247 12 : get_uleb128 (op1, readp, endp); /* Length of DW_FORM_block. */
6248 24 : printf (" def_cfa_expression %" PRIu64 "\n", op1);
6249 12 : if ((uint64_t) (endp - readp) < op1)
6250 : {
6251 0 : invalid:
6252 0 : fputs (gettext (" <INVALID DATA>\n"), stdout);
6253 0 : return;
6254 : }
6255 12 : print_ops (dwflmod, dbg, 10, 10, version, ptr_size, 0, NULL,
6256 : op1, readp);
6257 12 : readp += op1;
6258 12 : break;
6259 0 : case DW_CFA_expression:
6260 0 : if ((uint64_t) (endp - readp) < 1)
6261 : goto invalid;
6262 0 : get_uleb128 (op1, readp, endp);
6263 0 : if ((uint64_t) (endp - readp) < 1)
6264 : goto invalid;
6265 0 : get_uleb128 (op2, readp, endp); /* Length of DW_FORM_block. */
6266 0 : printf (" expression r%" PRIu64 " (%s) \n",
6267 : op1, regname (op1));
6268 0 : if ((uint64_t) (endp - readp) < op2)
6269 : goto invalid;
6270 0 : print_ops (dwflmod, dbg, 10, 10, version, ptr_size, 0, NULL,
6271 : op2, readp);
6272 0 : readp += op2;
6273 0 : break;
6274 16 : case DW_CFA_offset_extended_sf:
6275 16 : if ((uint64_t) (endp - readp) < 1)
6276 : goto invalid;
6277 16 : get_uleb128 (op1, readp, endp);
6278 16 : if ((uint64_t) (endp - readp) < 1)
6279 : goto invalid;
6280 16 : get_sleb128 (sop2, readp, endp);
6281 32 : printf (" offset_extended_sf r%" PRIu64 " (%s) at cfa%+"
6282 : PRId64 "\n",
6283 : op1, regname (op1), sop2 * data_align);
6284 : break;
6285 0 : case DW_CFA_def_cfa_sf:
6286 0 : if ((uint64_t) (endp - readp) < 1)
6287 : goto invalid;
6288 0 : get_uleb128 (op1, readp, endp);
6289 0 : if ((uint64_t) (endp - readp) < 1)
6290 : goto invalid;
6291 0 : get_sleb128 (sop2, readp, endp);
6292 0 : printf (" def_cfa_sf r%" PRIu64 " (%s) at offset %" PRId64 "\n",
6293 : op1, regname (op1), sop2 * data_align);
6294 : break;
6295 0 : case DW_CFA_def_cfa_offset_sf:
6296 0 : if ((uint64_t) (endp - readp) < 1)
6297 : goto invalid;
6298 0 : get_sleb128 (sop1, readp, endp);
6299 0 : printf (" def_cfa_offset_sf %" PRId64 "\n", sop1 * data_align);
6300 : break;
6301 0 : case DW_CFA_val_offset:
6302 0 : if ((uint64_t) (endp - readp) < 1)
6303 : goto invalid;
6304 0 : get_uleb128 (op1, readp, endp);
6305 0 : if ((uint64_t) (endp - readp) < 1)
6306 : goto invalid;
6307 0 : get_uleb128 (op2, readp, endp);
6308 0 : printf (" val_offset %" PRIu64 " at offset %" PRIu64 "\n",
6309 : op1, op2 * data_align);
6310 : break;
6311 0 : case DW_CFA_val_offset_sf:
6312 0 : if ((uint64_t) (endp - readp) < 1)
6313 : goto invalid;
6314 0 : get_uleb128 (op1, readp, endp);
6315 0 : if ((uint64_t) (endp - readp) < 1)
6316 : goto invalid;
6317 0 : get_sleb128 (sop2, readp, endp);
6318 0 : printf (" val_offset_sf %" PRIu64 " at offset %" PRId64 "\n",
6319 : op1, sop2 * data_align);
6320 : break;
6321 0 : case DW_CFA_val_expression:
6322 0 : if ((uint64_t) (endp - readp) < 1)
6323 : goto invalid;
6324 0 : get_uleb128 (op1, readp, endp);
6325 0 : if ((uint64_t) (endp - readp) < 1)
6326 : goto invalid;
6327 0 : get_uleb128 (op2, readp, endp); /* Length of DW_FORM_block. */
6328 0 : printf (" val_expression r%" PRIu64 " (%s)\n",
6329 : op1, regname (op1));
6330 0 : if ((uint64_t) (endp - readp) < op2)
6331 : goto invalid;
6332 0 : print_ops (dwflmod, dbg, 10, 10, version, ptr_size, 0,
6333 : NULL, op2, readp);
6334 0 : readp += op2;
6335 0 : break;
6336 0 : case DW_CFA_MIPS_advance_loc8:
6337 0 : if ((uint64_t) (endp - readp) < 8)
6338 : goto invalid;
6339 0 : op1 = read_8ubyte_unaligned_inc (dbg, readp);
6340 0 : printf (" MIPS_advance_loc8 %" PRIu64 " to %#" PRIx64 "\n",
6341 0 : op1, pc += op1 * code_align);
6342 : break;
6343 0 : case DW_CFA_GNU_window_save:
6344 0 : puts (" GNU_window_save");
6345 0 : break;
6346 0 : case DW_CFA_GNU_args_size:
6347 0 : if ((uint64_t) (endp - readp) < 1)
6348 : goto invalid;
6349 0 : get_uleb128 (op1, readp, endp);
6350 0 : printf (" args_size %" PRIu64 "\n", op1);
6351 : break;
6352 0 : default:
6353 : printf (" ??? (%u)\n", opcode);
6354 : break;
6355 : }
6356 31384 : else if (opcode < DW_CFA_offset)
6357 22921 : printf (" advance_loc %u to %#" PRIx64 "\n",
6358 22921 : opcode & 0x3f, pc += (opcode & 0x3f) * code_align);
6359 8463 : else if (opcode < DW_CFA_restore)
6360 : {
6361 : uint64_t offset;
6362 7169 : if ((uint64_t) (endp - readp) < 1)
6363 : goto invalid;
6364 7169 : get_uleb128 (offset, readp, endp);
6365 14338 : printf (" offset r%u (%s) at cfa%+" PRId64 "\n",
6366 : opcode & 0x3f, regname (opcode & 0x3f), offset * data_align);
6367 : }
6368 : else
6369 2588 : printf (" restore r%u (%s)\n",
6370 : opcode & 0x3f, regname (opcode & 0x3f));
6371 : }
6372 : }
6373 :
6374 :
6375 : static unsigned int
6376 4473 : encoded_ptr_size (int encoding, unsigned int ptr_size)
6377 : {
6378 4473 : switch (encoding & 7)
6379 : {
6380 : case DW_EH_PE_udata4:
6381 : return 4;
6382 0 : case DW_EH_PE_udata8:
6383 0 : return 8;
6384 34 : case 0:
6385 34 : return ptr_size;
6386 : }
6387 :
6388 0 : fprintf (stderr, "Unsupported pointer encoding: %#x, "
6389 : "assuming pointer size of %d.\n", encoding, ptr_size);
6390 0 : return ptr_size;
6391 : }
6392 :
6393 :
6394 : static unsigned int
6395 78 : print_encoding (unsigned int val)
6396 : {
6397 78 : switch (val & 0xf)
6398 : {
6399 0 : case DW_EH_PE_absptr:
6400 0 : fputs ("absptr", stdout);
6401 0 : break;
6402 0 : case DW_EH_PE_uleb128:
6403 0 : fputs ("uleb128", stdout);
6404 0 : break;
6405 0 : case DW_EH_PE_udata2:
6406 0 : fputs ("udata2", stdout);
6407 0 : break;
6408 14 : case DW_EH_PE_udata4:
6409 14 : fputs ("udata4", stdout);
6410 14 : break;
6411 0 : case DW_EH_PE_udata8:
6412 0 : fputs ("udata8", stdout);
6413 0 : break;
6414 0 : case DW_EH_PE_sleb128:
6415 0 : fputs ("sleb128", stdout);
6416 0 : break;
6417 0 : case DW_EH_PE_sdata2:
6418 0 : fputs ("sdata2", stdout);
6419 0 : break;
6420 64 : case DW_EH_PE_sdata4:
6421 64 : fputs ("sdata4", stdout);
6422 64 : break;
6423 0 : case DW_EH_PE_sdata8:
6424 0 : fputs ("sdata8", stdout);
6425 0 : break;
6426 : default:
6427 : /* We did not use any of the bits after all. */
6428 : return val;
6429 : }
6430 :
6431 78 : return val & ~0xf;
6432 : }
6433 :
6434 :
6435 : static unsigned int
6436 64 : print_relinfo (unsigned int val)
6437 : {
6438 64 : switch (val & 0x70)
6439 : {
6440 50 : case DW_EH_PE_pcrel:
6441 50 : fputs ("pcrel", stdout);
6442 50 : break;
6443 0 : case DW_EH_PE_textrel:
6444 0 : fputs ("textrel", stdout);
6445 0 : break;
6446 14 : case DW_EH_PE_datarel:
6447 14 : fputs ("datarel", stdout);
6448 14 : break;
6449 0 : case DW_EH_PE_funcrel:
6450 0 : fputs ("funcrel", stdout);
6451 0 : break;
6452 0 : case DW_EH_PE_aligned:
6453 0 : fputs ("aligned", stdout);
6454 0 : break;
6455 : default:
6456 : return val;
6457 : }
6458 :
6459 64 : return val & ~0x70;
6460 : }
6461 :
6462 :
6463 : static void
6464 78 : print_encoding_base (const char *pfx, unsigned int fde_encoding)
6465 : {
6466 78 : printf ("(%s", pfx);
6467 :
6468 78 : if (fde_encoding == DW_EH_PE_omit)
6469 0 : puts ("omit)");
6470 : else
6471 : {
6472 78 : unsigned int w = fde_encoding;
6473 :
6474 78 : w = print_encoding (w);
6475 :
6476 78 : if (w & 0x70)
6477 : {
6478 64 : if (w != fde_encoding)
6479 64 : fputc_unlocked (' ', stdout);
6480 :
6481 64 : w = print_relinfo (w);
6482 : }
6483 :
6484 78 : if (w != 0)
6485 0 : printf ("%s%x", w != fde_encoding ? " " : "", w);
6486 :
6487 78 : puts (")");
6488 : }
6489 78 : }
6490 :
6491 :
6492 : static void
6493 40 : print_debug_frame_section (Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr,
6494 : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
6495 : {
6496 : size_t shstrndx;
6497 : /* We know this call will succeed since it did in the caller. */
6498 40 : (void) elf_getshdrstrndx (ebl->elf, &shstrndx);
6499 40 : const char *scnname = elf_strptr (ebl->elf, shstrndx, shdr->sh_name);
6500 :
6501 : /* Needed if we find PC-relative addresses. */
6502 : GElf_Addr bias;
6503 40 : if (dwfl_module_getelf (dwflmod, &bias) == NULL)
6504 : {
6505 0 : error (0, 0, gettext ("cannot get ELF: %s"), dwfl_errmsg (-1));
6506 0 : return;
6507 : }
6508 :
6509 40 : bool is_eh_frame = strcmp (scnname, ".eh_frame") == 0;
6510 40 : Elf_Data *data = (is_eh_frame
6511 : ? elf_rawdata (scn, NULL)
6512 40 : : (dbg->sectiondata[IDX_debug_frame]
6513 14 : ?: elf_rawdata (scn, NULL)));
6514 :
6515 40 : if (unlikely (data == NULL))
6516 : {
6517 0 : error (0, 0, gettext ("cannot get %s content: %s"),
6518 : scnname, elf_errmsg (-1));
6519 : return;
6520 : }
6521 :
6522 40 : if (is_eh_frame)
6523 26 : printf (gettext ("\
6524 : \nCall frame information section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
6525 26 : elf_ndxscn (scn), scnname, (uint64_t) shdr->sh_offset);
6526 : else
6527 14 : printf (gettext ("\
6528 : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
6529 14 : elf_ndxscn (scn), scnname, (uint64_t) shdr->sh_offset);
6530 :
6531 : struct cieinfo
6532 : {
6533 : ptrdiff_t cie_offset;
6534 : const char *augmentation;
6535 : unsigned int code_alignment_factor;
6536 : unsigned int data_alignment_factor;
6537 : uint8_t address_size;
6538 : uint8_t fde_encoding;
6539 : uint8_t lsda_encoding;
6540 : struct cieinfo *next;
6541 40 : } *cies = NULL;
6542 :
6543 40 : const unsigned char *readp = data->d_buf;
6544 40 : const unsigned char *const dataend = ((unsigned char *) data->d_buf
6545 40 : + data->d_size);
6546 4629 : while (readp < dataend)
6547 : {
6548 4549 : if (unlikely (readp + 4 > dataend))
6549 : {
6550 0 : invalid_data:
6551 0 : error (0, 0, gettext ("invalid data in section [%zu] '%s'"),
6552 : elf_ndxscn (scn), scnname);
6553 : return;
6554 : }
6555 :
6556 : /* At the beginning there must be a CIE. There can be multiple,
6557 : hence we test tis in a loop. */
6558 4549 : ptrdiff_t offset = readp - (unsigned char *) data->d_buf;
6559 :
6560 4581 : Dwarf_Word unit_length = read_4ubyte_unaligned_inc (dbg, readp);
6561 4549 : unsigned int length = 4;
6562 4549 : if (unlikely (unit_length == 0xffffffff))
6563 : {
6564 0 : if (unlikely (readp + 8 > dataend))
6565 : goto invalid_data;
6566 :
6567 0 : unit_length = read_8ubyte_unaligned_inc (dbg, readp);
6568 0 : length = 8;
6569 : }
6570 :
6571 4549 : if (unlikely (unit_length == 0))
6572 : {
6573 28 : printf (gettext ("\n [%6tx] Zero terminator\n"), offset);
6574 14 : continue;
6575 : }
6576 :
6577 4535 : Dwarf_Word maxsize = dataend - readp;
6578 4535 : if (unlikely (unit_length > maxsize))
6579 : goto invalid_data;
6580 :
6581 4535 : unsigned int ptr_size = ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 4 : 8;
6582 :
6583 4535 : ptrdiff_t start = readp - (unsigned char *) data->d_buf;
6584 4535 : const unsigned char *const cieend = readp + unit_length;
6585 9070 : if (unlikely (cieend > dataend || readp + 8 > dataend))
6586 : goto invalid_data;
6587 :
6588 : Dwarf_Off cie_id;
6589 4535 : if (length == 4)
6590 : {
6591 4567 : cie_id = read_4ubyte_unaligned_inc (dbg, readp);
6592 4535 : if (!is_eh_frame && cie_id == DW_CIE_ID_32)
6593 24 : cie_id = DW_CIE_ID_64;
6594 : }
6595 : else
6596 0 : cie_id = read_8ubyte_unaligned_inc (dbg, readp);
6597 :
6598 4535 : uint_fast8_t version = 2;
6599 : unsigned int code_alignment_factor;
6600 : int data_alignment_factor;
6601 4535 : unsigned int fde_encoding = 0;
6602 4535 : unsigned int lsda_encoding = 0;
6603 4535 : Dwarf_Word initial_location = 0;
6604 4535 : Dwarf_Word vma_base = 0;
6605 :
6606 4535 : if (cie_id == (is_eh_frame ? 0 : DW_CIE_ID_64))
6607 : {
6608 62 : version = *readp++;
6609 62 : const char *const augmentation = (const char *) readp;
6610 62 : readp = memchr (readp, '\0', cieend - readp);
6611 62 : if (unlikely (readp == NULL))
6612 : goto invalid_data;
6613 62 : ++readp;
6614 :
6615 62 : uint_fast8_t segment_size = 0;
6616 62 : if (version >= 4)
6617 : {
6618 0 : if (cieend - readp < 5)
6619 : goto invalid_data;
6620 0 : ptr_size = *readp++;
6621 0 : segment_size = *readp++;
6622 : }
6623 :
6624 62 : if (cieend - readp < 1)
6625 : goto invalid_data;
6626 62 : get_uleb128 (code_alignment_factor, readp, cieend);
6627 62 : if (cieend - readp < 1)
6628 : goto invalid_data;
6629 62 : get_sleb128 (data_alignment_factor, readp, cieend);
6630 :
6631 : /* In some variant for unwind data there is another field. */
6632 62 : if (strcmp (augmentation, "eh") == 0)
6633 0 : readp += ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 4 : 8;
6634 :
6635 : unsigned int return_address_register;
6636 62 : if (cieend - readp < 1)
6637 : goto invalid_data;
6638 62 : if (unlikely (version == 1))
6639 50 : return_address_register = *readp++;
6640 : else
6641 12 : get_uleb128 (return_address_register, readp, cieend);
6642 :
6643 124 : printf ("\n [%6tx] CIE length=%" PRIu64 "\n"
6644 : " CIE_id: %" PRIu64 "\n"
6645 : " version: %u\n"
6646 : " augmentation: \"%s\"\n",
6647 : offset, (uint64_t) unit_length, (uint64_t) cie_id,
6648 : version, augmentation);
6649 62 : if (version >= 4)
6650 0 : printf (" address_size: %u\n"
6651 : " segment_size: %u\n",
6652 : ptr_size, segment_size);
6653 62 : printf (" code_alignment_factor: %u\n"
6654 : " data_alignment_factor: %d\n"
6655 : " return_address_register: %u\n",
6656 : code_alignment_factor,
6657 : data_alignment_factor, return_address_register);
6658 :
6659 62 : if (augmentation[0] == 'z')
6660 : {
6661 : unsigned int augmentationlen;
6662 36 : get_uleb128 (augmentationlen, readp, cieend);
6663 :
6664 36 : if (augmentationlen > (size_t) (cieend - readp))
6665 : {
6666 0 : error (0, 0, gettext ("invalid augmentation length"));
6667 0 : readp = cieend;
6668 0 : continue;
6669 : }
6670 :
6671 36 : const char *hdr = "Augmentation data:";
6672 36 : const char *cp = augmentation + 1;
6673 108 : while (*cp != '\0' && cp < augmentation + augmentationlen + 1)
6674 : {
6675 72 : printf (" %-26s%#x ", hdr, *readp);
6676 36 : hdr = "";
6677 :
6678 36 : if (*cp == 'R')
6679 : {
6680 36 : fde_encoding = *readp++;
6681 36 : print_encoding_base (gettext ("FDE address encoding: "),
6682 : fde_encoding);
6683 : }
6684 0 : else if (*cp == 'L')
6685 : {
6686 0 : lsda_encoding = *readp++;
6687 0 : print_encoding_base (gettext ("LSDA pointer encoding: "),
6688 : lsda_encoding);
6689 : }
6690 0 : else if (*cp == 'P')
6691 : {
6692 : /* Personality. This field usually has a relocation
6693 : attached pointing to __gcc_personality_v0. */
6694 0 : const unsigned char *startp = readp;
6695 0 : unsigned int encoding = *readp++;
6696 0 : uint64_t val = 0;
6697 0 : readp = read_encoded (encoding, readp,
6698 0 : readp - 1 + augmentationlen,
6699 : &val, dbg);
6700 :
6701 0 : while (++startp < readp)
6702 0 : printf ("%#x ", *startp);
6703 :
6704 0 : putchar ('(');
6705 0 : print_encoding (encoding);
6706 0 : putchar (' ');
6707 0 : switch (encoding & 0xf)
6708 : {
6709 0 : case DW_EH_PE_sleb128:
6710 : case DW_EH_PE_sdata2:
6711 : case DW_EH_PE_sdata4:
6712 0 : printf ("%" PRId64 ")\n", val);
6713 : break;
6714 0 : default:
6715 0 : printf ("%#" PRIx64 ")\n", val);
6716 : break;
6717 : }
6718 : }
6719 : else
6720 0 : printf ("(%x)\n", *readp++);
6721 :
6722 36 : ++cp;
6723 : }
6724 : }
6725 :
6726 62 : if (likely (ptr_size == 4 || ptr_size == 8))
6727 : {
6728 62 : struct cieinfo *newp = alloca (sizeof (*newp));
6729 62 : newp->cie_offset = offset;
6730 62 : newp->augmentation = augmentation;
6731 62 : newp->fde_encoding = fde_encoding;
6732 62 : newp->lsda_encoding = lsda_encoding;
6733 62 : newp->address_size = ptr_size;
6734 62 : newp->code_alignment_factor = code_alignment_factor;
6735 62 : newp->data_alignment_factor = data_alignment_factor;
6736 62 : newp->next = cies;
6737 62 : cies = newp;
6738 : }
6739 : }
6740 : else
6741 : {
6742 : struct cieinfo *cie = cies;
6743 4473 : while (cie != NULL)
6744 8946 : if (is_eh_frame
6745 4445 : ? ((Dwarf_Off) start - cie_id) == (Dwarf_Off) cie->cie_offset
6746 28 : : cie_id == (Dwarf_Off) cie->cie_offset)
6747 : break;
6748 : else
6749 0 : cie = cie->next;
6750 4473 : if (unlikely (cie == NULL))
6751 : {
6752 0 : puts ("invalid CIE reference in FDE");
6753 0 : return;
6754 : }
6755 :
6756 : /* Initialize from CIE data. */
6757 4473 : fde_encoding = cie->fde_encoding;
6758 4473 : lsda_encoding = cie->lsda_encoding;
6759 4473 : ptr_size = encoded_ptr_size (fde_encoding, cie->address_size);
6760 4473 : code_alignment_factor = cie->code_alignment_factor;
6761 4473 : data_alignment_factor = cie->data_alignment_factor;
6762 :
6763 4473 : const unsigned char *base = readp;
6764 : // XXX There are sometimes relocations for this value
6765 4489 : initial_location = read_addr_unaligned_inc (ptr_size, dbg, readp);
6766 4473 : Dwarf_Word address_range
6767 4489 : = read_addr_unaligned_inc (ptr_size, dbg, readp);
6768 :
6769 : /* pcrel for an FDE address is relative to the runtime
6770 : address of the start_address field itself. Sign extend
6771 : if necessary to make sure the calculation is done on the
6772 : full 64 bit address even when initial_location only holds
6773 : the lower 32 bits. */
6774 4473 : Dwarf_Addr pc_start = initial_location;
6775 4473 : if (ptr_size == 4)
6776 4443 : pc_start = (uint64_t) (int32_t) pc_start;
6777 4473 : if ((fde_encoding & 0x70) == DW_EH_PE_pcrel)
6778 8878 : pc_start += ((uint64_t) shdr->sh_addr
6779 4439 : + (base - (const unsigned char *) data->d_buf)
6780 4439 : - bias);
6781 :
6782 8946 : printf ("\n [%6tx] FDE length=%" PRIu64 " cie=[%6tx]\n"
6783 : " CIE_pointer: %" PRIu64 "\n"
6784 : " initial_location: ",
6785 : offset, (uint64_t) unit_length,
6786 : cie->cie_offset, (uint64_t) cie_id);
6787 4473 : print_dwarf_addr (dwflmod, cie->address_size,
6788 : pc_start, initial_location);
6789 4473 : if ((fde_encoding & 0x70) == DW_EH_PE_pcrel)
6790 : {
6791 8878 : vma_base = (((uint64_t) shdr->sh_offset
6792 4439 : + (base - (const unsigned char *) data->d_buf)
6793 4439 : + (uint64_t) initial_location)
6794 : & (ptr_size == 4
6795 : ? UINT64_C (0xffffffff)
6796 4439 : : UINT64_C (0xffffffffffffffff)));
6797 4439 : printf (gettext (" (offset: %#" PRIx64 ")"),
6798 : (uint64_t) vma_base);
6799 : }
6800 :
6801 4473 : printf ("\n address_range: %#" PRIx64,
6802 : (uint64_t) address_range);
6803 4473 : if ((fde_encoding & 0x70) == DW_EH_PE_pcrel)
6804 4439 : printf (gettext (" (end offset: %#" PRIx64 ")"),
6805 4439 : ((uint64_t) vma_base + (uint64_t) address_range)
6806 : & (ptr_size == 4
6807 : ? UINT64_C (0xffffffff)
6808 4439 : : UINT64_C (0xffffffffffffffff)));
6809 4473 : putchar ('\n');
6810 :
6811 4473 : if (cie->augmentation[0] == 'z')
6812 : {
6813 : unsigned int augmentationlen;
6814 4439 : if (cieend - readp < 1)
6815 : goto invalid_data;
6816 4439 : get_uleb128 (augmentationlen, readp, cieend);
6817 :
6818 4439 : if (augmentationlen > (size_t) (cieend - readp))
6819 : {
6820 0 : error (0, 0, gettext ("invalid augmentation length"));
6821 0 : readp = cieend;
6822 0 : continue;
6823 : }
6824 :
6825 4439 : if (augmentationlen > 0)
6826 : {
6827 0 : const char *hdr = "Augmentation data:";
6828 0 : const char *cp = cie->augmentation + 1;
6829 0 : unsigned int u = 0;
6830 0 : while (*cp != '\0'
6831 0 : && cp < cie->augmentation + augmentationlen + 1)
6832 : {
6833 0 : if (*cp == 'L')
6834 : {
6835 : uint64_t lsda_pointer;
6836 0 : const unsigned char *p
6837 0 : = read_encoded (lsda_encoding, &readp[u],
6838 : &readp[augmentationlen],
6839 : &lsda_pointer, dbg);
6840 0 : u = p - readp;
6841 0 : printf (gettext ("\
6842 : %-26sLSDA pointer: %#" PRIx64 "\n"),
6843 : hdr, lsda_pointer);
6844 0 : hdr = "";
6845 : }
6846 0 : ++cp;
6847 : }
6848 :
6849 0 : while (u < augmentationlen)
6850 : {
6851 0 : printf (" %-26s%#x\n", hdr, readp[u++]);
6852 0 : hdr = "";
6853 : }
6854 : }
6855 :
6856 4439 : readp += augmentationlen;
6857 : }
6858 : }
6859 :
6860 : /* Handle the initialization instructions. */
6861 4535 : if (ptr_size != 4 && ptr_size !=8)
6862 : printf ("invalid CIE pointer size (%u), must be 4 or 8.\n", ptr_size);
6863 : else
6864 4535 : print_cfa_program (readp, cieend, vma_base, code_alignment_factor,
6865 : data_alignment_factor, version, ptr_size,
6866 : fde_encoding, dwflmod, ebl, dbg);
6867 4535 : readp = cieend;
6868 : }
6869 : }
6870 :
6871 :
6872 : struct attrcb_args
6873 : {
6874 : Dwfl_Module *dwflmod;
6875 : Dwarf *dbg;
6876 : Dwarf_Die *die;
6877 : int level;
6878 : bool silent;
6879 : bool is_split;
6880 : unsigned int version;
6881 : unsigned int addrsize;
6882 : unsigned int offset_size;
6883 : struct Dwarf_CU *cu;
6884 : };
6885 :
6886 :
6887 : static int
6888 2451568 : attr_callback (Dwarf_Attribute *attrp, void *arg)
6889 : {
6890 2451568 : struct attrcb_args *cbargs = (struct attrcb_args *) arg;
6891 2451568 : const int level = cbargs->level;
6892 2451568 : Dwarf_Die *die = cbargs->die;
6893 2451568 : bool is_split = cbargs->is_split;
6894 :
6895 2451568 : unsigned int attr = dwarf_whatattr (attrp);
6896 2451568 : if (unlikely (attr == 0))
6897 : {
6898 0 : if (!cbargs->silent)
6899 0 : error (0, 0, gettext ("DIE [%" PRIx64 "] "
6900 : "cannot get attribute code: %s"),
6901 : dwarf_dieoffset (die), dwarf_errmsg (-1));
6902 : return DWARF_CB_ABORT;
6903 : }
6904 :
6905 2451568 : unsigned int form = dwarf_whatform (attrp);
6906 2451568 : if (unlikely (form == 0))
6907 : {
6908 0 : if (!cbargs->silent)
6909 0 : error (0, 0, gettext ("DIE [%" PRIx64 "] "
6910 : "cannot get attribute form: %s"),
6911 : dwarf_dieoffset (die), dwarf_errmsg (-1));
6912 : return DWARF_CB_ABORT;
6913 : }
6914 :
6915 2451568 : switch (form)
6916 : {
6917 37905 : case DW_FORM_addr:
6918 : case DW_FORM_addrx:
6919 : case DW_FORM_addrx1:
6920 : case DW_FORM_addrx2:
6921 : case DW_FORM_addrx3:
6922 : case DW_FORM_addrx4:
6923 : case DW_FORM_GNU_addr_index:
6924 37905 : if (!cbargs->silent)
6925 : {
6926 : Dwarf_Addr addr;
6927 37642 : if (unlikely (dwarf_formaddr (attrp, &addr) != 0))
6928 : {
6929 0 : attrval_out:
6930 0 : if (!cbargs->silent)
6931 0 : error (0, 0, gettext ("DIE [%" PRIx64 "] "
6932 : "cannot get attribute '%s' (%s) value: "
6933 : "%s"),
6934 : dwarf_dieoffset (die),
6935 : dwarf_attr_name (attr),
6936 : dwarf_form_name (form),
6937 : dwarf_errmsg (-1));
6938 : /* Don't ABORT, it might be other attributes can be resolved. */
6939 0 : return DWARF_CB_OK;
6940 : }
6941 37642 : if (form != DW_FORM_addr )
6942 : {
6943 : Dwarf_Word index;
6944 8 : if (dwarf_formudata (attrp, &index) != 0)
6945 : goto attrval_out;
6946 16 : printf (" %*s%-20s (%s) [%" PRIx64 "] ",
6947 : (int) (level * 2), "", dwarf_attr_name (attr),
6948 : dwarf_form_name (form), index);
6949 : }
6950 : else
6951 37634 : printf (" %*s%-20s (%s) ",
6952 : (int) (level * 2), "", dwarf_attr_name (attr),
6953 : dwarf_form_name (form));
6954 37642 : print_dwarf_addr (cbargs->dwflmod, cbargs->addrsize, addr, addr);
6955 37642 : printf ("\n");
6956 : }
6957 2194463 : break;
6958 :
6959 458098 : case DW_FORM_indirect:
6960 : case DW_FORM_strp:
6961 : case DW_FORM_line_strp:
6962 : case DW_FORM_strx:
6963 : case DW_FORM_strx1:
6964 : case DW_FORM_strx2:
6965 : case DW_FORM_strx3:
6966 : case DW_FORM_strx4:
6967 : case DW_FORM_string:
6968 : case DW_FORM_GNU_strp_alt:
6969 : case DW_FORM_GNU_str_index:
6970 458098 : if (cbargs->silent)
6971 : break;
6972 457383 : const char *str = dwarf_formstring (attrp);
6973 457383 : if (unlikely (str == NULL))
6974 : goto attrval_out;
6975 457383 : printf (" %*s%-20s (%s) \"%s\"\n",
6976 : (int) (level * 2), "", dwarf_attr_name (attr),
6977 : dwarf_form_name (form), str);
6978 : break;
6979 :
6980 581811 : case DW_FORM_ref_addr:
6981 : case DW_FORM_ref_udata:
6982 : case DW_FORM_ref8:
6983 : case DW_FORM_ref4:
6984 : case DW_FORM_ref2:
6985 : case DW_FORM_ref1:
6986 : case DW_FORM_GNU_ref_alt:
6987 : case DW_FORM_ref_sup4:
6988 : case DW_FORM_ref_sup8:
6989 581811 : if (cbargs->silent)
6990 : break;
6991 : Dwarf_Die ref;
6992 581048 : if (unlikely (dwarf_formref_die (attrp, &ref) == NULL))
6993 : goto attrval_out;
6994 :
6995 1162096 : printf (" %*s%-20s (%s) ",
6996 : (int) (level * 2), "", dwarf_attr_name (attr),
6997 : dwarf_form_name (form));
6998 581048 : if (is_split)
6999 33 : printf ("{%6" PRIxMAX "}\n", (uintmax_t) dwarf_dieoffset (&ref));
7000 : else
7001 581015 : printf ("[%6" PRIxMAX "]\n", (uintmax_t) dwarf_dieoffset (&ref));
7002 : break;
7003 :
7004 4 : case DW_FORM_ref_sig8:
7005 4 : if (cbargs->silent)
7006 : break;
7007 8 : printf (" %*s%-20s (%s) {%6" PRIx64 "}\n",
7008 : (int) (level * 2), "", dwarf_attr_name (attr),
7009 : dwarf_form_name (form),
7010 8 : (uint64_t) read_8ubyte_unaligned (attrp->cu->dbg, attrp->valp));
7011 : break;
7012 :
7013 1223834 : case DW_FORM_sec_offset:
7014 : case DW_FORM_rnglistx:
7015 : case DW_FORM_loclistx:
7016 : case DW_FORM_implicit_const:
7017 : case DW_FORM_udata:
7018 : case DW_FORM_sdata:
7019 : case DW_FORM_data8: /* Note no data16 here, we see that as block. */
7020 : case DW_FORM_data4:
7021 : case DW_FORM_data2:
7022 : case DW_FORM_data1:;
7023 : Dwarf_Word num;
7024 1223834 : if (unlikely (dwarf_formudata (attrp, &num) != 0))
7025 : goto attrval_out;
7026 :
7027 1223834 : const char *valuestr = NULL;
7028 1223834 : bool as_hex_id = false;
7029 1223834 : switch (attr)
7030 : {
7031 : /* This case can take either a constant or a loclistptr. */
7032 205516 : case DW_AT_data_member_location:
7033 205516 : if (form != DW_FORM_sec_offset
7034 205516 : && (cbargs->version >= 4
7035 23806 : || (form != DW_FORM_data4 && form != DW_FORM_data8)))
7036 : {
7037 205516 : if (!cbargs->silent)
7038 205516 : printf (" %*s%-20s (%s) %" PRIxMAX "\n",
7039 : (int) (level * 2), "", dwarf_attr_name (attr),
7040 : dwarf_form_name (form), (uintmax_t) num);
7041 : return DWARF_CB_OK;
7042 : }
7043 : FALLTHROUGH;
7044 :
7045 : /* These cases always take a loclist[ptr] and no constant. */
7046 : case DW_AT_location:
7047 : case DW_AT_data_location:
7048 : case DW_AT_vtable_elem_location:
7049 : case DW_AT_string_length:
7050 : case DW_AT_use_location:
7051 : case DW_AT_frame_base:
7052 : case DW_AT_return_addr:
7053 : case DW_AT_static_link:
7054 : case DW_AT_segment:
7055 : case DW_AT_GNU_call_site_value:
7056 : case DW_AT_GNU_call_site_data_value:
7057 : case DW_AT_GNU_call_site_target:
7058 : case DW_AT_GNU_call_site_target_clobbered:
7059 : case DW_AT_GNU_locviews:
7060 : {
7061 : bool nlpt;
7062 40897 : if (cbargs->cu->version < 5)
7063 : {
7064 40826 : if (! cbargs->is_split)
7065 : {
7066 122418 : nlpt = notice_listptr (section_loc, &known_locsptr,
7067 40806 : cbargs->addrsize,
7068 40806 : cbargs->offset_size,
7069 : cbargs->cu, num, attr);
7070 : }
7071 : else
7072 : nlpt = true;
7073 : }
7074 : else
7075 : {
7076 : /* Only register for a real section offset. Otherwise
7077 : it is a DW_FORM_loclistx which is just an index
7078 : number and we should already have registered the
7079 : section offset for the index when we saw the
7080 : DW_AT_loclists_base CU attribute. */
7081 71 : if (form == DW_FORM_sec_offset)
7082 96 : nlpt = notice_listptr (section_loc, &known_loclistsptr,
7083 64 : cbargs->addrsize, cbargs->offset_size,
7084 : cbargs->cu, num, attr);
7085 : else
7086 : nlpt = true;
7087 :
7088 : }
7089 :
7090 40897 : if (!cbargs->silent)
7091 : {
7092 40722 : if (cbargs->cu->version < 5 || form == DW_FORM_sec_offset)
7093 40715 : printf (" %*s%-20s (%s) location list [%6"
7094 : PRIxMAX "]%s\n",
7095 : (int) (level * 2), "", dwarf_attr_name (attr),
7096 : dwarf_form_name (form), (uintmax_t) num,
7097 : nlpt ? "" : " <WARNING offset too big>");
7098 : else
7099 7 : printf (" %*s%-20s (%s) location index [%6"
7100 : PRIxMAX "]\n",
7101 : (int) (level * 2), "", dwarf_attr_name (attr),
7102 : dwarf_form_name (form), (uintmax_t) num);
7103 : }
7104 : }
7105 : return DWARF_CB_OK;
7106 :
7107 5 : case DW_AT_loclists_base:
7108 4 : {
7109 15 : bool nlpt = notice_listptr (section_loc, &known_loclistsptr,
7110 10 : cbargs->addrsize, cbargs->offset_size,
7111 : cbargs->cu, num, attr);
7112 :
7113 5 : if (!cbargs->silent)
7114 1 : printf (" %*s%-20s (%s) location list [%6" PRIxMAX "]%s\n",
7115 : (int) (level * 2), "", dwarf_attr_name (attr),
7116 : dwarf_form_name (form), (uintmax_t) num,
7117 : nlpt ? "" : " <WARNING offset too big>");
7118 : }
7119 : return DWARF_CB_OK;
7120 :
7121 10670 : case DW_AT_ranges:
7122 : case DW_AT_start_scope:
7123 : {
7124 : bool nlpt;
7125 10670 : if (cbargs->cu->version < 5)
7126 31962 : nlpt = notice_listptr (section_ranges, &known_rangelistptr,
7127 21308 : cbargs->addrsize, cbargs->offset_size,
7128 : cbargs->cu, num, attr);
7129 : else
7130 : {
7131 : /* Only register for a real section offset. Otherwise
7132 : it is a DW_FORM_rangelistx which is just an index
7133 : number and we should already have registered the
7134 : section offset for the index when we saw the
7135 : DW_AT_rnglists_base CU attribute. */
7136 16 : if (form == DW_FORM_sec_offset)
7137 30 : nlpt = notice_listptr (section_ranges, &known_rnglistptr,
7138 20 : cbargs->addrsize, cbargs->offset_size,
7139 : cbargs->cu, num, attr);
7140 : else
7141 : nlpt = true;
7142 : }
7143 :
7144 10670 : if (!cbargs->silent)
7145 : {
7146 10628 : if (cbargs->cu->version < 5 || form == DW_FORM_sec_offset)
7147 10626 : printf (" %*s%-20s (%s) range list [%6"
7148 : 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>");
7152 : else
7153 2 : printf (" %*s%-20s (%s) range index [%6"
7154 : PRIxMAX "]\n",
7155 : (int) (level * 2), "", dwarf_attr_name (attr),
7156 : dwarf_form_name (form), (uintmax_t) num);
7157 : }
7158 : }
7159 : return DWARF_CB_OK;
7160 :
7161 4 : case DW_AT_rnglists_base:
7162 2 : {
7163 12 : bool nlpt = notice_listptr (section_ranges, &known_rnglistptr,
7164 8 : cbargs->addrsize, cbargs->offset_size,
7165 : cbargs->cu, num, attr);
7166 4 : if (!cbargs->silent)
7167 2 : printf (" %*s%-20s (%s) range list [%6"
7168 : PRIxMAX "]%s\n",
7169 : (int) (level * 2), "", dwarf_attr_name (attr),
7170 : dwarf_form_name (form), (uintmax_t) num,
7171 : nlpt ? "" : " <WARNING offset too big>");
7172 : }
7173 : return DWARF_CB_OK;
7174 :
7175 13 : case DW_AT_addr_base:
7176 : case DW_AT_GNU_addr_base:
7177 8 : {
7178 39 : bool addrbase = notice_listptr (section_addr, &known_addrbases,
7179 13 : cbargs->addrsize,
7180 13 : cbargs->offset_size,
7181 : cbargs->cu, num, attr);
7182 13 : if (!cbargs->silent)
7183 5 : printf (" %*s%-20s (%s) address base [%6"
7184 : PRIxMAX "]%s\n",
7185 : (int) (level * 2), "", dwarf_attr_name (attr),
7186 : dwarf_form_name (form), (uintmax_t) num,
7187 : addrbase ? "" : " <WARNING offset too big>");
7188 : }
7189 : return DWARF_CB_OK;
7190 :
7191 0 : case DW_AT_str_offsets_base:
7192 0 : {
7193 0 : bool stroffbase = notice_listptr (section_str, &known_stroffbases,
7194 0 : cbargs->addrsize,
7195 0 : cbargs->offset_size,
7196 : cbargs->cu, num, attr);
7197 0 : if (!cbargs->silent)
7198 0 : printf (" %*s%-20s (%s) str offsets base [%6"
7199 : PRIxMAX "]%s\n",
7200 : (int) (level * 2), "", dwarf_attr_name (attr),
7201 : dwarf_form_name (form), (uintmax_t) num,
7202 : stroffbase ? "" : " <WARNING offset too big>");
7203 : }
7204 : return DWARF_CB_OK;
7205 :
7206 1126 : case DW_AT_language:
7207 1126 : valuestr = dwarf_lang_name (num);
7208 1126 : break;
7209 16115 : case DW_AT_encoding:
7210 32230 : valuestr = dwarf_encoding_name (num);
7211 16115 : break;
7212 0 : case DW_AT_accessibility:
7213 0 : valuestr = dwarf_access_name (num);
7214 0 : break;
7215 0 : case DW_AT_defaulted:
7216 0 : valuestr = dwarf_defaulted_name (num);
7217 0 : break;
7218 0 : case DW_AT_visibility:
7219 0 : valuestr = dwarf_visibility_name (num);
7220 0 : break;
7221 0 : case DW_AT_virtuality:
7222 0 : valuestr = dwarf_virtuality_name (num);
7223 0 : break;
7224 0 : case DW_AT_identifier_case:
7225 0 : valuestr = dwarf_identifier_case_name (num);
7226 0 : break;
7227 0 : case DW_AT_calling_convention:
7228 0 : valuestr = dwarf_calling_convention_name (num);
7229 0 : break;
7230 2795 : case DW_AT_inline:
7231 5590 : valuestr = dwarf_inline_name (num);
7232 2795 : break;
7233 0 : case DW_AT_ordering:
7234 0 : valuestr = dwarf_ordering_name (num);
7235 0 : break;
7236 0 : case DW_AT_discr_list:
7237 0 : valuestr = dwarf_discr_list_name (num);
7238 0 : break;
7239 361616 : case DW_AT_decl_file:
7240 : case DW_AT_call_file:
7241 361616 : {
7242 361616 : if (cbargs->silent)
7243 : break;
7244 :
7245 : /* Try to get the actual file, the current interface only
7246 : gives us full paths, but we only want to show the file
7247 : name for now. */
7248 : Dwarf_Die cudie;
7249 361210 : if (dwarf_cu_die (cbargs->cu, &cudie,
7250 : NULL, NULL, NULL, NULL, NULL, NULL) != NULL)
7251 : {
7252 : Dwarf_Files *files;
7253 : size_t nfiles;
7254 361210 : if (dwarf_getsrcfiles (&cudie, &files, &nfiles) == 0)
7255 : {
7256 361210 : valuestr = dwarf_filesrc (files, num, NULL, NULL);
7257 361210 : if (valuestr != NULL)
7258 : {
7259 361210 : char *filename = strrchr (valuestr, '/');
7260 361210 : if (filename != NULL)
7261 361210 : valuestr = filename + 1;
7262 : }
7263 : else
7264 0 : error (0, 0, gettext ("invalid file (%" PRId64 "): %s"),
7265 : num, dwarf_errmsg (-1));
7266 : }
7267 : else
7268 0 : error (0, 0, gettext ("no srcfiles for CU [%" PRIx64 "]"),
7269 : dwarf_dieoffset (&cudie));
7270 : }
7271 : else
7272 0 : error (0, 0, gettext ("couldn't get DWARF CU: %s"),
7273 : dwarf_errmsg (-1));
7274 361210 : if (valuestr == NULL)
7275 : valuestr = "???";
7276 : }
7277 361210 : break;
7278 13 : case DW_AT_GNU_dwo_id:
7279 13 : as_hex_id = true;
7280 13 : break;
7281 :
7282 : default:
7283 : /* Nothing. */
7284 : break;
7285 : }
7286 :
7287 966729 : if (cbargs->silent)
7288 : break;
7289 :
7290 : /* When highpc is in constant form it is relative to lowpc.
7291 : In that case also show the address. */
7292 : Dwarf_Addr highpc;
7293 964957 : if (attr == DW_AT_high_pc && dwarf_highpc (cbargs->die, &highpc) == 0)
7294 : {
7295 26776 : printf (" %*s%-20s (%s) %" PRIuMAX " (",
7296 : (int) (level * 2), "", dwarf_attr_name (attr),
7297 : dwarf_form_name (form), (uintmax_t) num);
7298 13388 : print_dwarf_addr (cbargs->dwflmod, cbargs->addrsize, highpc, highpc);
7299 : printf (")\n");
7300 : }
7301 : else
7302 951569 : {
7303 951569 : Dwarf_Sword snum = 0;
7304 951569 : if (form == DW_FORM_sdata)
7305 1214 : if (unlikely (dwarf_formsdata (attrp, &snum) != 0))
7306 : goto attrval_out;
7307 :
7308 951569 : if (as_hex_id)
7309 : {
7310 2 : printf (" %*s%-20s (%s) 0x%.16" PRIx64 "\n",
7311 : (int) (level * 2), "", dwarf_attr_name (attr),
7312 : dwarf_form_name (form), num);
7313 : }
7314 951567 : else if (valuestr == NULL)
7315 : {
7316 1141224 : printf (" %*s%-20s (%s)",
7317 : (int) (level * 2), "", dwarf_attr_name (attr),
7318 : dwarf_form_name (form));
7319 570612 : if (form == DW_FORM_sdata)
7320 1214 : printf (" %" PRIdMAX "\n", (intmax_t) snum);
7321 : else
7322 569398 : printf (" %" PRIuMAX "\n", (uintmax_t) num);
7323 : }
7324 : else
7325 : {
7326 761910 : printf (" %*s%-20s (%s) %s",
7327 : (int) (level * 2), "", dwarf_attr_name (attr),
7328 : dwarf_form_name (form), valuestr);
7329 380955 : if (form == DW_FORM_sdata)
7330 0 : printf (" (%" PRIdMAX ")\n", (intmax_t) snum);
7331 : else
7332 380955 : printf (" (%" PRIuMAX ")\n", (uintmax_t) num);
7333 : }
7334 : }
7335 : break;
7336 :
7337 9122 : case DW_FORM_flag:
7338 9122 : if (cbargs->silent)
7339 : break;
7340 : bool flag;
7341 9048 : if (unlikely (dwarf_formflag (attrp, &flag) != 0))
7342 : goto attrval_out;
7343 :
7344 9048 : printf (" %*s%-20s (%s) %s\n",
7345 : (int) (level * 2), "", dwarf_attr_name (attr),
7346 9048 : dwarf_form_name (form), flag ? yes_str : no_str);
7347 : break;
7348 :
7349 63177 : case DW_FORM_flag_present:
7350 63177 : if (cbargs->silent)
7351 : break;
7352 62912 : printf (" %*s%-20s (%s) %s\n",
7353 : (int) (level * 2), "", dwarf_attr_name (attr),
7354 : dwarf_form_name (form), yes_str);
7355 : break;
7356 :
7357 77617 : case DW_FORM_exprloc:
7358 : case DW_FORM_block4:
7359 : case DW_FORM_block2:
7360 : case DW_FORM_block1:
7361 : case DW_FORM_block:
7362 : case DW_FORM_data16: /* DWARF5 calls this a constant class. */
7363 77617 : if (cbargs->silent)
7364 : break;
7365 : Dwarf_Block block;
7366 77494 : if (unlikely (dwarf_formblock (attrp, &block) != 0))
7367 : goto attrval_out;
7368 :
7369 154988 : printf (" %*s%-20s (%s) ",
7370 : (int) (level * 2), "", dwarf_attr_name (attr),
7371 : dwarf_form_name (form));
7372 :
7373 77494 : switch (attr)
7374 : {
7375 14 : default:
7376 14 : if (form != DW_FORM_exprloc)
7377 : {
7378 14 : print_block (block.length, block.data);
7379 14 : break;
7380 : }
7381 : FALLTHROUGH;
7382 :
7383 : case DW_AT_location:
7384 : case DW_AT_data_location:
7385 : case DW_AT_data_member_location:
7386 : case DW_AT_vtable_elem_location:
7387 : case DW_AT_string_length:
7388 : case DW_AT_use_location:
7389 : case DW_AT_frame_base:
7390 : case DW_AT_return_addr:
7391 : case DW_AT_static_link:
7392 : case DW_AT_allocated:
7393 : case DW_AT_associated:
7394 : case DW_AT_bit_size:
7395 : case DW_AT_bit_offset:
7396 : case DW_AT_bit_stride:
7397 : case DW_AT_byte_size:
7398 : case DW_AT_byte_stride:
7399 : case DW_AT_count:
7400 : case DW_AT_lower_bound:
7401 : case DW_AT_upper_bound:
7402 : case DW_AT_GNU_call_site_value:
7403 : case DW_AT_GNU_call_site_data_value:
7404 : case DW_AT_GNU_call_site_target:
7405 : case DW_AT_GNU_call_site_target_clobbered:
7406 77480 : putchar ('\n');
7407 154960 : print_ops (cbargs->dwflmod, cbargs->dbg,
7408 77480 : 12 + level * 2, 12 + level * 2,
7409 : cbargs->version, cbargs->addrsize, cbargs->offset_size,
7410 77480 : attrp->cu, block.length, block.data);
7411 77480 : break;
7412 : }
7413 : break;
7414 :
7415 0 : default:
7416 0 : if (cbargs->silent)
7417 : break;
7418 0 : printf (" %*s%-20s (%s) ???\n",
7419 : (int) (level * 2), "", dwarf_attr_name (attr),
7420 : dwarf_form_name (form));
7421 : break;
7422 : }
7423 :
7424 2194463 : return DWARF_CB_OK;
7425 : }
7426 :
7427 : static void
7428 82 : print_debug_units (Dwfl_Module *dwflmod,
7429 : Ebl *ebl, GElf_Ehdr *ehdr,
7430 : Elf_Scn *scn, GElf_Shdr *shdr,
7431 : Dwarf *dbg, bool debug_types)
7432 : {
7433 82 : const bool silent = !(print_debug_sections & section_info) && !debug_types;
7434 164 : const char *secname = section_name (ebl, ehdr, shdr);
7435 :
7436 82 : if (!silent)
7437 50 : printf (gettext ("\
7438 : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n [Offset]\n"),
7439 50 : elf_ndxscn (scn), secname, (uint64_t) shdr->sh_offset);
7440 :
7441 : /* If the section is empty we don't have to do anything. */
7442 82 : if (!silent && shdr->sh_size == 0)
7443 0 : return;
7444 :
7445 82 : int maxdies = 20;
7446 82 : Dwarf_Die *dies = (Dwarf_Die *) xmalloc (maxdies * sizeof (Dwarf_Die));
7447 :
7448 : /* New compilation unit. */
7449 : Dwarf_Half version;
7450 :
7451 : Dwarf_Die result;
7452 : Dwarf_Off abbroffset;
7453 : uint8_t addrsize;
7454 : uint8_t offsize;
7455 : uint64_t unit_id;
7456 : Dwarf_Off subdie_off;
7457 :
7458 : int unit_res;
7459 : Dwarf_CU *cu;
7460 : Dwarf_CU cu_mem;
7461 : uint8_t unit_type;
7462 : Dwarf_Die cudie;
7463 :
7464 : /* We cheat a little because we want to see only the CUs from .debug_info
7465 : or .debug_types. We know the Dwarf_CU struct layout. Set it up at
7466 : the end of .debug_info if we want .debug_types only. Check the returned
7467 : Dwarf_CU is still in the expected section. */
7468 82 : if (debug_types)
7469 : {
7470 1 : cu_mem.dbg = dbg;
7471 1 : cu_mem.end = dbg->sectiondata[IDX_debug_info]->d_size;
7472 1 : cu_mem.sec_idx = IDX_debug_info;
7473 1 : cu = &cu_mem;
7474 : }
7475 : else
7476 81 : cu = NULL;
7477 :
7478 : next_cu:
7479 1217 : unit_res = dwarf_get_units (dbg, cu, &cu, &version, &unit_type,
7480 : &cudie, NULL);
7481 1217 : if (unit_res == 1)
7482 : goto do_return;
7483 :
7484 1136 : if (unit_res == -1)
7485 : {
7486 0 : if (!silent)
7487 0 : error (0, 0, gettext ("cannot get next unit: %s"), dwarf_errmsg (-1));
7488 : goto do_return;
7489 : }
7490 :
7491 1136 : if (cu->sec_idx != (size_t) (debug_types ? IDX_debug_types : IDX_debug_info))
7492 : goto do_return;
7493 :
7494 1135 : dwarf_cu_die (cu, &result, NULL, &abbroffset, &addrsize, &offsize,
7495 : &unit_id, &subdie_off);
7496 :
7497 1135 : if (!silent)
7498 : {
7499 1089 : Dwarf_Off offset = cu->start;
7500 1089 : if (debug_types && version < 5)
7501 2 : {
7502 : Dwarf_Die typedie;
7503 : Dwarf_Off dieoffset;
7504 2 : dieoffset = dwarf_dieoffset (dwarf_offdie_types (dbg, subdie_off,
7505 : &typedie));
7506 4 : printf (gettext (" Type unit at offset %" PRIu64 ":\n"
7507 : " Version: %" PRIu16
7508 : ", Abbreviation section offset: %" PRIu64
7509 : ", Address size: %" PRIu8
7510 : ", Offset size: %" PRIu8
7511 : "\n Type signature: %#" PRIx64
7512 : ", Type offset: %#" PRIx64 " [%" PRIx64 "]\n"),
7513 : (uint64_t) offset, version, abbroffset, addrsize, offsize,
7514 : unit_id, (uint64_t) subdie_off, dieoffset);
7515 : }
7516 : else
7517 : {
7518 2174 : printf (gettext (" Compilation unit at offset %" PRIu64 ":\n"
7519 : " Version: %" PRIu16
7520 : ", Abbreviation section offset: %" PRIu64
7521 : ", Address size: %" PRIu8
7522 : ", Offset size: %" PRIu8 "\n"),
7523 : (uint64_t) offset, version, abbroffset, addrsize, offsize);
7524 :
7525 2170 : if (version >= 5 || (unit_type != DW_UT_compile
7526 1083 : && unit_type != DW_UT_partial))
7527 : {
7528 10 : printf (gettext (" Unit type: %s (%" PRIu8 ")"),
7529 : dwarf_unit_name (unit_type), unit_type);
7530 5 : if (unit_type == DW_UT_type
7531 5 : || unit_type == DW_UT_skeleton
7532 0 : || unit_type == DW_UT_split_compile
7533 0 : || unit_type == DW_UT_split_type)
7534 5 : printf (", Unit id: 0x%.16" PRIx64 "", unit_id);
7535 5 : if (unit_type == DW_UT_type
7536 5 : || unit_type == DW_UT_split_type)
7537 : {
7538 : Dwarf_Die typedie;
7539 : Dwarf_Off dieoffset;
7540 0 : dwarf_cu_info (cu, NULL, NULL, NULL, &typedie,
7541 : NULL, NULL, NULL);
7542 0 : dieoffset = dwarf_dieoffset (&typedie);
7543 0 : printf (", Unit DIE off: %#" PRIx64 " [%" PRIx64 "]",
7544 : subdie_off, dieoffset);
7545 : }
7546 : printf ("\n");
7547 : }
7548 : }
7549 : }
7550 :
7551 1135 : if (version < 2 || version > 5
7552 1135 : || unit_type < DW_UT_compile || unit_type > DW_UT_split_type)
7553 : {
7554 0 : if (!silent)
7555 0 : error (0, 0, gettext ("unknown version (%d) or unit type (%d)"),
7556 : version, unit_type);
7557 : goto next_cu;
7558 : }
7559 :
7560 1135 : struct attrcb_args args =
7561 : {
7562 : .dwflmod = dwflmod,
7563 : .silent = silent,
7564 : .version = version,
7565 : .addrsize = addrsize,
7566 : .offset_size = offsize
7567 : };
7568 :
7569 1135 : bool is_split = false;
7570 1135 : int level = 0;
7571 1135 : dies[0] = cudie;
7572 1135 : args.cu = dies[0].cu;
7573 1135 : args.dbg = dbg;
7574 : args.is_split = is_split;
7575 :
7576 : /* We might return here again for the split CU subdie. */
7577 697895 : do_cu:
7578 : do
7579 : {
7580 699034 : Dwarf_Off offset = dwarf_dieoffset (&dies[level]);
7581 699034 : if (unlikely (offset == (Dwarf_Off) -1))
7582 : {
7583 0 : if (!silent)
7584 0 : error (0, 0, gettext ("cannot get DIE offset: %s"),
7585 : dwarf_errmsg (-1));
7586 : goto do_return;
7587 : }
7588 :
7589 699034 : int tag = dwarf_tag (&dies[level]);
7590 699034 : if (unlikely (tag == DW_TAG_invalid))
7591 : {
7592 0 : if (!silent)
7593 0 : error (0, 0, gettext ("cannot get tag of DIE at offset [%" PRIx64
7594 : "] in section '%s': %s"),
7595 : (uint64_t) offset, secname, dwarf_errmsg (-1));
7596 : goto do_return;
7597 : }
7598 :
7599 699034 : if (!silent)
7600 : {
7601 698079 : unsigned int code = dwarf_getabbrevcode (dies[level].abbrev);
7602 698079 : if (is_split)
7603 : printf (" {%6" PRIx64 "} ", (uint64_t) offset);
7604 : else
7605 : printf (" [%6" PRIx64 "] ", (uint64_t) offset);
7606 698079 : printf ("%*s%-20s abbrev: %u\n", (int) (level * 2), "",
7607 : dwarf_tag_name (tag), code);
7608 : }
7609 :
7610 : /* Print the attribute values. */
7611 699034 : args.level = level;
7612 699034 : args.die = &dies[level];
7613 699034 : (void) dwarf_getattrs (&dies[level], attr_callback, &args, 0);
7614 :
7615 : /* Make room for the next level's DIE. */
7616 699034 : if (level + 1 == maxdies)
7617 0 : dies = (Dwarf_Die *) xrealloc (dies,
7618 0 : (maxdies += 10)
7619 : * sizeof (Dwarf_Die));
7620 :
7621 699034 : int res = dwarf_child (&dies[level], &dies[level + 1]);
7622 699034 : if (res > 0)
7623 : {
7624 699034 : while ((res = dwarf_siblingof (&dies[level], &dies[level])) == 1)
7625 99214 : if (level-- == 0)
7626 : break;
7627 :
7628 600959 : if (unlikely (res == -1))
7629 : {
7630 0 : if (!silent)
7631 0 : error (0, 0, gettext ("cannot get next DIE: %s\n"),
7632 : dwarf_errmsg (-1));
7633 : goto do_return;
7634 : }
7635 : }
7636 98075 : else if (unlikely (res < 0))
7637 : {
7638 0 : if (!silent)
7639 0 : error (0, 0, gettext ("cannot get next DIE: %s"),
7640 : dwarf_errmsg (-1));
7641 : goto do_return;
7642 : }
7643 : else
7644 : ++level;
7645 : }
7646 699034 : while (level >= 0);
7647 :
7648 : /* We might want to show the split compile unit if this was a skeleton.
7649 : We need to scan it if we are requesting printing .debug_ranges for
7650 : DWARF4 since GNU DebugFission uses "offsets" into the main ranges
7651 : section. */
7652 1139 : if (unit_type == DW_UT_skeleton
7653 13 : && ((!silent && show_split_units)
7654 10 : || (version < 5 && (print_debug_sections & section_ranges) != 0)))
7655 : {
7656 : Dwarf_Die subdie;
7657 5 : if (dwarf_cu_info (cu, NULL, NULL, NULL, &subdie, NULL, NULL, NULL) != 0
7658 5 : || dwarf_tag (&subdie) == DW_TAG_invalid)
7659 : {
7660 1 : if (!silent)
7661 : {
7662 : Dwarf_Attribute dwo_at;
7663 1 : const char *dwo_name =
7664 1 : (dwarf_formstring (dwarf_attr (&cudie, DW_AT_dwo_name,
7665 : &dwo_at))
7666 1 : ?: (dwarf_formstring (dwarf_attr (&cudie, DW_AT_GNU_dwo_name,
7667 : &dwo_at))
7668 0 : ?: "<unknown>"));
7669 2 : fprintf (stderr,
7670 : "Could not find split unit '%s', id: %" PRIx64 "\n",
7671 : dwo_name, unit_id);
7672 : }
7673 : }
7674 : else
7675 : {
7676 4 : Dwarf_CU *split_cu = subdie.cu;
7677 4 : dwarf_cu_die (split_cu, &result, NULL, &abbroffset,
7678 : &addrsize, &offsize, &unit_id, &subdie_off);
7679 4 : Dwarf_Off offset = cu->start;
7680 :
7681 4 : if (!silent)
7682 : {
7683 4 : printf (gettext (" Split compilation unit at offset %"
7684 : PRIu64 ":\n"
7685 : " Version: %" PRIu16
7686 : ", Abbreviation section offset: %" PRIu64
7687 : ", Address size: %" PRIu8
7688 : ", Offset size: %" PRIu8 "\n"),
7689 : (uint64_t) offset, version, abbroffset,
7690 : addrsize, offsize);
7691 4 : printf (gettext (" Unit type: %s (%" PRIu8 ")"),
7692 : dwarf_unit_name (unit_type), unit_type);
7693 4 : printf (", Unit id: 0x%.16" PRIx64 "", unit_id);
7694 : printf ("\n");
7695 : }
7696 :
7697 4 : unit_type = DW_UT_split_compile;
7698 4 : is_split = true;
7699 4 : level = 0;
7700 4 : dies[0] = subdie;
7701 4 : args.cu = dies[0].cu;
7702 4 : args.dbg = split_cu->dbg;
7703 4 : args.is_split = is_split;
7704 4 : goto do_cu;
7705 : }
7706 : }
7707 :
7708 : /* And again... */
7709 : goto next_cu;
7710 :
7711 82 : do_return:
7712 82 : free (dies);
7713 : }
7714 :
7715 : static void
7716 13 : print_debug_info_section (Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr,
7717 : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
7718 : {
7719 81 : print_debug_units (dwflmod, ebl, ehdr, scn, shdr, dbg, false);
7720 13 : }
7721 :
7722 : static void
7723 1 : print_debug_types_section (Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr,
7724 : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
7725 : {
7726 1 : print_debug_units (dwflmod, ebl, ehdr, scn, shdr, dbg, true);
7727 1 : }
7728 :
7729 :
7730 : static void
7731 4 : print_decoded_line_section (Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr,
7732 : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
7733 : {
7734 12 : printf (gettext ("\
7735 : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n\n"),
7736 : elf_ndxscn (scn), section_name (ebl, ehdr, shdr),
7737 4 : (uint64_t) shdr->sh_offset);
7738 :
7739 4 : size_t address_size
7740 4 : = elf_getident (ebl->elf, NULL)[EI_CLASS] == ELFCLASS32 ? 4 : 8;
7741 :
7742 : Dwarf_Off cuoffset;
7743 4 : Dwarf_Off ncuoffset = 0;
7744 : size_t hsize;
7745 16 : while (dwarf_nextcu (dbg, cuoffset = ncuoffset, &ncuoffset, &hsize,
7746 : NULL, NULL, NULL) == 0)
7747 : {
7748 : Dwarf_Die cudie;
7749 8 : if (dwarf_offdie (dbg, cuoffset + hsize, &cudie) == NULL)
7750 0 : continue;
7751 :
7752 : size_t nlines;
7753 : Dwarf_Lines *lines;
7754 8 : if (dwarf_getsrclines (&cudie, &lines, &nlines) != 0)
7755 0 : continue;
7756 :
7757 16 : printf (" CU [%" PRIx64 "] %s\n",
7758 : dwarf_dieoffset (&cudie), dwarf_diename (&cudie));
7759 8 : printf (" line:col SBPE* disc isa op address"
7760 : " (Statement Block Prologue Epilogue *End)\n");
7761 8 : const char *last_file = "";
7762 168 : for (size_t n = 0; n < nlines; n++)
7763 : {
7764 160 : Dwarf_Line *line = dwarf_onesrcline (lines, n);
7765 160 : if (line == NULL)
7766 : {
7767 0 : printf (" dwarf_onesrcline: %s\n", dwarf_errmsg (-1));
7768 0 : continue;
7769 : }
7770 : Dwarf_Word mtime, length;
7771 160 : const char *file = dwarf_linesrc (line, &mtime, &length);
7772 160 : if (file == NULL)
7773 : {
7774 0 : printf (" <%s> (mtime: ?, length: ?)\n", dwarf_errmsg (-1));
7775 0 : last_file = "";
7776 : }
7777 160 : else if (strcmp (last_file, file) != 0)
7778 : {
7779 40 : printf (" %s (mtime: %" PRIu64 ", length: %" PRIu64 ")\n",
7780 : file, mtime, length);
7781 20 : last_file = file;
7782 : }
7783 :
7784 : int lineno, colno;
7785 : bool statement, endseq, block, prologue_end, epilogue_begin;
7786 : unsigned int lineop, isa, disc;
7787 : Dwarf_Addr address;
7788 160 : dwarf_lineaddr (line, &address);
7789 160 : dwarf_lineno (line, &lineno);
7790 160 : dwarf_linecol (line, &colno);
7791 160 : dwarf_lineop_index (line, &lineop);
7792 160 : dwarf_linebeginstatement (line, &statement);
7793 160 : dwarf_lineendsequence (line, &endseq);
7794 160 : dwarf_lineblock (line, &block);
7795 160 : dwarf_lineprologueend (line, &prologue_end);
7796 160 : dwarf_lineepiloguebegin (line, &epilogue_begin);
7797 160 : dwarf_lineisa (line, &isa);
7798 160 : dwarf_linediscriminator (line, &disc);
7799 :
7800 : /* End sequence is special, it is one byte past. */
7801 1120 : printf (" %4d:%-3d %c%c%c%c%c %4d %3d %2d ",
7802 : lineno, colno,
7803 160 : (statement ? 'S' : ' '),
7804 160 : (block ? 'B' : ' '),
7805 160 : (prologue_end ? 'P' : ' '),
7806 160 : (epilogue_begin ? 'E' : ' '),
7807 160 : (endseq ? '*' : ' '),
7808 : disc, isa, lineop);
7809 320 : print_dwarf_addr (dwflmod, address_size,
7810 160 : address - (endseq ? 1 : 0), address);
7811 160 : printf ("\n");
7812 :
7813 160 : if (endseq)
7814 : printf("\n");
7815 : }
7816 : }
7817 4 : }
7818 :
7819 :
7820 : /* Print the value of a form.
7821 : Returns new value of readp, or readendp on failure. */
7822 : static const unsigned char *
7823 20 : print_form_data (Dwarf *dbg, int form, const unsigned char *readp,
7824 : const unsigned char *readendp, unsigned int offset_len,
7825 : Dwarf_Off str_offsets_base)
7826 : {
7827 : Dwarf_Word val;
7828 : unsigned char *endp;
7829 : Elf_Data *data;
7830 : char *str;
7831 20 : switch (form)
7832 : {
7833 8 : case DW_FORM_data1:
7834 8 : if (readendp - readp < 1)
7835 : {
7836 0 : invalid_data:
7837 0 : error (0, 0, "invalid data");
7838 0 : return readendp;
7839 : }
7840 8 : val = *readp++;
7841 8 : printf (" %" PRIx8, (unsigned int) val);
7842 : break;
7843 :
7844 0 : case DW_FORM_data2:
7845 0 : if (readendp - readp < 2)
7846 : goto invalid_data;
7847 0 : val = read_2ubyte_unaligned_inc (dbg, readp);
7848 0 : printf(" %" PRIx16, (unsigned int) val);
7849 : break;
7850 :
7851 0 : case DW_FORM_data4:
7852 0 : if (readendp - readp < 4)
7853 : goto invalid_data;
7854 0 : val = read_4ubyte_unaligned_inc (dbg, readp);
7855 0 : printf (" %" PRIx32, (unsigned int) val);
7856 : break;
7857 :
7858 0 : case DW_FORM_data8:
7859 0 : if (readendp - readp < 8)
7860 : goto invalid_data;
7861 0 : val = read_8ubyte_unaligned_inc (dbg, readp);
7862 : printf (" %" PRIx64, val);
7863 : break;
7864 :
7865 0 : case DW_FORM_sdata:
7866 0 : if (readendp - readp < 1)
7867 : goto invalid_data;
7868 0 : get_sleb128 (val, readp, readendp);
7869 : printf (" %" PRIx64, val);
7870 : break;
7871 :
7872 0 : case DW_FORM_udata:
7873 0 : if (readendp - readp < 1)
7874 : goto invalid_data;
7875 0 : get_uleb128 (val, readp, readendp);
7876 : printf (" %" PRIx64, val);
7877 : break;
7878 :
7879 0 : case DW_FORM_block:
7880 0 : if (readendp - readp < 1)
7881 : goto invalid_data;
7882 0 : get_uleb128 (val, readp, readendp);
7883 0 : if ((size_t) (readendp - readp) < val)
7884 : goto invalid_data;
7885 0 : print_bytes (val, readp);
7886 0 : readp += val;
7887 0 : break;
7888 :
7889 0 : case DW_FORM_block1:
7890 0 : if (readendp - readp < 1)
7891 : goto invalid_data;
7892 0 : val = *readp++;
7893 0 : if ((size_t) (readendp - readp) < val)
7894 : goto invalid_data;
7895 0 : print_bytes (val, readp);
7896 0 : readp += val;
7897 0 : break;
7898 :
7899 0 : case DW_FORM_block2:
7900 0 : if (readendp - readp < 2)
7901 : goto invalid_data;
7902 0 : val = read_2ubyte_unaligned_inc (dbg, readp);
7903 0 : if ((size_t) (readendp - readp) < val)
7904 : goto invalid_data;
7905 0 : print_bytes (val, readp);
7906 0 : readp += val;
7907 0 : break;
7908 :
7909 0 : case DW_FORM_block4:
7910 0 : if (readendp - readp < 2)
7911 : goto invalid_data;
7912 0 : val = read_4ubyte_unaligned_inc (dbg, readp);
7913 0 : if ((size_t) (readendp - readp) < val)
7914 : goto invalid_data;
7915 0 : print_bytes (val, readp);
7916 0 : readp += val;
7917 0 : break;
7918 :
7919 0 : case DW_FORM_data16:
7920 0 : if (readendp - readp < 16)
7921 : goto invalid_data;
7922 0 : print_bytes (16, readp);
7923 0 : readp += 16;
7924 0 : break;
7925 :
7926 0 : case DW_FORM_flag:
7927 0 : if (readendp - readp < 1)
7928 : goto invalid_data;
7929 0 : val = *readp++;
7930 0 : printf ("%s", val != 0 ? yes_str : no_str);
7931 : break;
7932 :
7933 0 : case DW_FORM_string:
7934 0 : endp = memchr (readp, '\0', readendp - readp);
7935 0 : if (endp == NULL)
7936 : goto invalid_data;
7937 0 : printf ("%s", readp);
7938 0 : readp = endp + 1;
7939 0 : break;
7940 :
7941 12 : case DW_FORM_strp:
7942 : case DW_FORM_line_strp:
7943 : case DW_FORM_strp_sup:
7944 12 : if ((size_t) (readendp - readp) < offset_len)
7945 : goto invalid_data;
7946 12 : if (offset_len == 8)
7947 0 : val = read_8ubyte_unaligned_inc (dbg, readp);
7948 : else
7949 12 : val = read_4ubyte_unaligned_inc (dbg, readp);
7950 12 : if (form == DW_FORM_strp)
7951 0 : data = dbg->sectiondata[IDX_debug_str];
7952 12 : else if (form == DW_FORM_line_strp)
7953 12 : data = dbg->sectiondata[IDX_debug_line_str];
7954 : else /* form == DW_FORM_strp_sup */
7955 : {
7956 0 : Dwarf *alt = dwarf_getalt (dbg);
7957 0 : data = alt != NULL ? alt->sectiondata[IDX_debug_str] : NULL;
7958 : }
7959 12 : if (data == NULL || val >= data->d_size
7960 12 : || memchr (data->d_buf + val, '\0', data->d_size - val) == NULL)
7961 : str = "???";
7962 : else
7963 12 : str = (char *) data->d_buf + val;
7964 : printf ("%s (%" PRIu64 ")", str, val);
7965 : break;
7966 :
7967 0 : case DW_FORM_sec_offset:
7968 0 : if ((size_t) (readendp - readp) < offset_len)
7969 : goto invalid_data;
7970 0 : if (offset_len == 8)
7971 0 : val = read_8ubyte_unaligned_inc (dbg, readp);
7972 : else
7973 0 : val = read_4ubyte_unaligned_inc (dbg, readp);
7974 : printf ("[%" PRIx64 "]", val);
7975 : break;
7976 :
7977 0 : case DW_FORM_strx:
7978 : case DW_FORM_GNU_str_index:
7979 0 : if (readendp - readp < 1)
7980 : goto invalid_data;
7981 0 : get_uleb128 (val, readp, readendp);
7982 0 : strx_val:
7983 0 : data = dbg->sectiondata[IDX_debug_str_offsets];
7984 0 : if (data == NULL
7985 0 : || data->d_size - str_offsets_base < val)
7986 : str = "???";
7987 : else
7988 : {
7989 0 : const unsigned char *strreadp = data->d_buf + str_offsets_base + val;
7990 0 : const unsigned char *strreadendp = data->d_buf + data->d_size;
7991 0 : if ((size_t) (strreadendp - strreadp) < offset_len)
7992 : str = "???";
7993 : else
7994 : {
7995 : Dwarf_Off idx;
7996 0 : if (offset_len == 8)
7997 0 : idx = read_8ubyte_unaligned_inc (dbg, strreadp);
7998 : else
7999 0 : idx = read_4ubyte_unaligned_inc (dbg, strreadp);
8000 :
8001 0 : data = dbg->sectiondata[IDX_debug_str];
8002 0 : if (data == NULL || idx >= data->d_size
8003 0 : || memchr (data->d_buf + idx, '\0',
8004 : data->d_size - idx) == NULL)
8005 : str = "???";
8006 : else
8007 0 : str = (char *) data->d_buf + idx;
8008 : }
8009 : }
8010 : printf ("%s (%" PRIu64 ")", str, val);
8011 : break;
8012 :
8013 0 : case DW_FORM_strx1:
8014 0 : if (readendp - readp < 1)
8015 : goto invalid_data;
8016 0 : val = *readp;
8017 0 : goto strx_val;
8018 :
8019 0 : case DW_FORM_strx2:
8020 0 : if (readendp - readp < 2)
8021 : goto invalid_data;
8022 0 : val = read_2ubyte_unaligned (dbg, readp);
8023 : goto strx_val;
8024 :
8025 0 : case DW_FORM_strx3:
8026 0 : if (readendp - readp < 3)
8027 : goto invalid_data;
8028 0 : val = read_3ubyte_unaligned (dbg, readp);
8029 0 : goto strx_val;
8030 :
8031 0 : case DW_FORM_strx4:
8032 0 : if (readendp - readp < 4)
8033 : goto invalid_data;
8034 0 : val = read_4ubyte_unaligned (dbg, readp);
8035 : goto strx_val;
8036 :
8037 0 : default:
8038 0 : error (0, 0, gettext ("unknown form: %s"), dwarf_form_name (form));
8039 0 : return readendp;
8040 : }
8041 :
8042 20 : return readp;
8043 : }
8044 :
8045 : static void
8046 44 : print_debug_line_section (Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr,
8047 : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
8048 : {
8049 44 : if (decodedline)
8050 : {
8051 4 : print_decoded_line_section (dwflmod, ebl, ehdr, scn, shdr, dbg);
8052 4 : return;
8053 : }
8054 :
8055 120 : printf (gettext ("\
8056 : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
8057 : elf_ndxscn (scn), section_name (ebl, ehdr, shdr),
8058 40 : (uint64_t) shdr->sh_offset);
8059 :
8060 40 : if (shdr->sh_size == 0)
8061 : return;
8062 :
8063 : /* There is no functionality in libdw to read the information in the
8064 : way it is represented here. Hardcode the decoder. */
8065 80 : Elf_Data *data = (dbg->sectiondata[IDX_debug_line]
8066 40 : ?: elf_rawdata (scn, NULL));
8067 40 : if (unlikely (data == NULL))
8068 : {
8069 0 : error (0, 0, gettext ("cannot get line data section data: %s"),
8070 : elf_errmsg (-1));
8071 : return;
8072 : }
8073 :
8074 40 : const unsigned char *linep = (const unsigned char *) data->d_buf;
8075 : const unsigned char *lineendp;
8076 :
8077 1159 : while (linep
8078 1119 : < (lineendp = (const unsigned char *) data->d_buf + data->d_size))
8079 : {
8080 1079 : size_t start_offset = linep - (const unsigned char *) data->d_buf;
8081 :
8082 2158 : printf (gettext ("\nTable at offset %zu:\n"), start_offset);
8083 :
8084 1079 : if (unlikely (linep + 4 > lineendp))
8085 : goto invalid_data;
8086 1104 : Dwarf_Word unit_length = read_4ubyte_unaligned_inc (dbg, linep);
8087 1079 : unsigned int length = 4;
8088 1079 : if (unlikely (unit_length == 0xffffffff))
8089 : {
8090 0 : if (unlikely (linep + 8 > lineendp))
8091 : {
8092 0 : invalid_data:
8093 0 : error (0, 0, gettext ("invalid data in section [%zu] '%s'"),
8094 : elf_ndxscn (scn), section_name (ebl, ehdr, shdr));
8095 0 : return;
8096 : }
8097 0 : unit_length = read_8ubyte_unaligned_inc (dbg, linep);
8098 0 : length = 8;
8099 : }
8100 :
8101 : /* Check whether we have enough room in the section. */
8102 1079 : if (unlikely (unit_length > (size_t) (lineendp - linep)))
8103 : goto invalid_data;
8104 1079 : lineendp = linep + unit_length;
8105 :
8106 : /* The next element of the header is the version identifier. */
8107 1079 : if ((size_t) (lineendp - linep) < 2)
8108 : goto invalid_data;
8109 1079 : uint_fast16_t version = read_2ubyte_unaligned_inc (dbg, linep);
8110 :
8111 1079 : size_t address_size
8112 1079 : = elf_getident (ebl->elf, NULL)[EI_CLASS] == ELFCLASS32 ? 4 : 8;
8113 1079 : unsigned char segment_selector_size = 0;
8114 1079 : if (version > 4)
8115 : {
8116 2 : if ((size_t) (lineendp - linep) < 2)
8117 : goto invalid_data;
8118 2 : address_size = *linep++;
8119 2 : segment_selector_size = *linep++;
8120 : }
8121 :
8122 : /* Next comes the header length. */
8123 : Dwarf_Word header_length;
8124 1079 : if (length == 4)
8125 : {
8126 1079 : if ((size_t) (lineendp - linep) < 4)
8127 : goto invalid_data;
8128 1104 : header_length = read_4ubyte_unaligned_inc (dbg, linep);
8129 : }
8130 : else
8131 : {
8132 0 : if ((size_t) (lineendp - linep) < 8)
8133 : goto invalid_data;
8134 0 : header_length = read_8ubyte_unaligned_inc (dbg, linep);
8135 : }
8136 :
8137 : /* Next the minimum instruction length. */
8138 1079 : if ((size_t) (lineendp - linep) < 1)
8139 : goto invalid_data;
8140 1079 : uint_fast8_t minimum_instr_len = *linep++;
8141 :
8142 : /* Next the maximum operations per instruction, in version 4 format. */
8143 : uint_fast8_t max_ops_per_instr;
8144 1079 : if (version < 4)
8145 : max_ops_per_instr = 1;
8146 : else
8147 : {
8148 10 : if ((size_t) (lineendp - linep) < 1)
8149 : goto invalid_data;
8150 10 : max_ops_per_instr = *linep++;
8151 : }
8152 :
8153 : /* We need at least 4 more bytes. */
8154 1079 : if ((size_t) (lineendp - linep) < 4)
8155 : goto invalid_data;
8156 :
8157 : /* Then the flag determining the default value of the is_stmt
8158 : register. */
8159 1079 : uint_fast8_t default_is_stmt = *linep++;
8160 :
8161 : /* Now the line base. */
8162 1079 : int_fast8_t line_base = *linep++;
8163 :
8164 : /* And the line range. */
8165 1079 : uint_fast8_t line_range = *linep++;
8166 :
8167 : /* The opcode base. */
8168 1079 : uint_fast8_t opcode_base = *linep++;
8169 :
8170 : /* Print what we got so far. */
8171 2158 : printf (gettext ("\n"
8172 : " Length: %" PRIu64 "\n"
8173 : " DWARF version: %" PRIuFAST16 "\n"
8174 : " Prologue length: %" PRIu64 "\n"
8175 : " Address size: %zd\n"
8176 : " Segment selector size: %zd\n"
8177 : " Min instruction length: %" PRIuFAST8 "\n"
8178 : " Max operations per instruction: %" PRIuFAST8 "\n"
8179 : " Initial value if 'is_stmt': %" PRIuFAST8 "\n"
8180 : " Line base: %" PRIdFAST8 "\n"
8181 : " Line range: %" PRIuFAST8 "\n"
8182 : " Opcode base: %" PRIuFAST8 "\n"
8183 : "\n"
8184 : "Opcodes:\n"),
8185 : (uint64_t) unit_length, version, (uint64_t) header_length,
8186 : address_size, (size_t) segment_selector_size,
8187 : minimum_instr_len, max_ops_per_instr,
8188 : default_is_stmt, line_base,
8189 : line_range, opcode_base);
8190 :
8191 1079 : if (version < 2 || version > 5)
8192 : {
8193 0 : error (0, 0, gettext ("cannot handle .debug_line version: %u\n"),
8194 : (unsigned int) version);
8195 0 : linep = lineendp;
8196 0 : continue;
8197 : }
8198 :
8199 1079 : if (address_size != 4 && address_size != 8)
8200 : {
8201 0 : error (0, 0, gettext ("cannot handle address size: %u\n"),
8202 : (unsigned int) address_size);
8203 0 : linep = lineendp;
8204 0 : continue;
8205 : }
8206 :
8207 1079 : if (segment_selector_size != 0)
8208 : {
8209 0 : error (0, 0, gettext ("cannot handle segment selector size: %u\n"),
8210 : (unsigned int) segment_selector_size);
8211 0 : linep = lineendp;
8212 0 : continue;
8213 : }
8214 :
8215 1079 : if (unlikely (linep + opcode_base - 1 >= lineendp))
8216 : {
8217 0 : invalid_unit:
8218 0 : error (0, 0,
8219 0 : gettext ("invalid data at offset %tu in section [%zu] '%s'"),
8220 0 : linep - (const unsigned char *) data->d_buf,
8221 : elf_ndxscn (scn), section_name (ebl, ehdr, shdr));
8222 0 : linep = lineendp;
8223 0 : continue;
8224 : }
8225 1079 : int opcode_base_l10 = 1;
8226 1079 : unsigned int tmp = opcode_base;
8227 3235 : while (tmp > 10)
8228 : {
8229 1077 : tmp /= 10;
8230 1077 : ++opcode_base_l10;
8231 : }
8232 1079 : const uint8_t *standard_opcode_lengths = linep - 1;
8233 14021 : for (uint_fast8_t cnt = 1; cnt < opcode_base; ++cnt)
8234 25884 : printf (ngettext (" [%*" PRIuFAST8 "] %hhu argument\n",
8235 : " [%*" PRIuFAST8 "] %hhu arguments\n",
8236 : (int) linep[cnt - 1]),
8237 12942 : opcode_base_l10, cnt, linep[cnt - 1]);
8238 1079 : linep += opcode_base - 1;
8239 :
8240 1079 : if (unlikely (linep >= lineendp))
8241 : goto invalid_unit;
8242 :
8243 1079 : Dwarf_Off str_offsets_base = str_offsets_base_off (dbg, NULL);
8244 :
8245 1079 : puts (gettext ("\nDirectory table:"));
8246 1079 : if (version > 4)
8247 : {
8248 : struct encpair { uint16_t desc; uint16_t form; };
8249 : struct encpair enc[256];
8250 :
8251 4 : printf (gettext (" ["));
8252 2 : if ((size_t) (lineendp - linep) < 1)
8253 : goto invalid_data;
8254 2 : unsigned char directory_entry_format_count = *linep++;
8255 6 : for (int i = 0; i < directory_entry_format_count; i++)
8256 : {
8257 : uint16_t desc, form;
8258 2 : if ((size_t) (lineendp - linep) < 1)
8259 : goto invalid_data;
8260 2 : get_uleb128 (desc, linep, lineendp);
8261 2 : if ((size_t) (lineendp - linep) < 1)
8262 : goto invalid_data;
8263 2 : get_uleb128 (form, linep, lineendp);
8264 :
8265 : enc[i].desc = desc;
8266 2 : enc[i].form = form;
8267 :
8268 4 : printf ("%s(%s)",
8269 : dwarf_line_content_description_name (desc),
8270 : dwarf_form_name (form));
8271 2 : if (i + 1 < directory_entry_format_count)
8272 : printf (", ");
8273 : }
8274 2 : printf ("]\n");
8275 :
8276 : uint64_t directories_count;
8277 2 : if ((size_t) (lineendp - linep) < 1)
8278 : goto invalid_data;
8279 2 : get_uleb128 (directories_count, linep, lineendp);
8280 :
8281 4 : if (directory_entry_format_count == 0
8282 2 : && directories_count != 0)
8283 : goto invalid_data;
8284 :
8285 4 : for (uint64_t i = 0; i < directories_count; i++)
8286 : {
8287 4 : printf (" %-5" PRIu64 " ", i);
8288 12 : for (int j = 0; j < directory_entry_format_count; j++)
8289 : {
8290 4 : linep = print_form_data (dbg, enc[j].form,
8291 : linep, lineendp, length,
8292 : str_offsets_base);
8293 4 : if (j + 1 < directory_entry_format_count)
8294 : printf (", ");
8295 : }
8296 4 : printf ("\n");
8297 4 : if (linep >= lineendp)
8298 : goto invalid_unit;
8299 : }
8300 : }
8301 : else
8302 : {
8303 8063 : while (*linep != 0)
8304 : {
8305 6986 : unsigned char *endp = memchr (linep, '\0', lineendp - linep);
8306 6986 : if (unlikely (endp == NULL))
8307 : goto invalid_unit;
8308 :
8309 13972 : printf (" %s\n", (char *) linep);
8310 :
8311 6986 : linep = endp + 1;
8312 : }
8313 : /* Skip the final NUL byte. */
8314 1077 : ++linep;
8315 : }
8316 :
8317 1079 : if (unlikely (linep >= lineendp))
8318 : goto invalid_unit;
8319 :
8320 1079 : puts (gettext ("\nFile name table:"));
8321 1079 : if (version > 4)
8322 : {
8323 : struct encpair { uint16_t desc; uint16_t form; };
8324 : struct encpair enc[256];
8325 :
8326 4 : printf (gettext (" ["));
8327 2 : if ((size_t) (lineendp - linep) < 1)
8328 : goto invalid_data;
8329 2 : unsigned char file_name_format_count = *linep++;
8330 8 : for (int i = 0; i < file_name_format_count; i++)
8331 : {
8332 : uint64_t desc, form;
8333 4 : if ((size_t) (lineendp - linep) < 1)
8334 : goto invalid_data;
8335 4 : get_uleb128 (desc, linep, lineendp);
8336 4 : if ((size_t) (lineendp - linep) < 1)
8337 : goto invalid_data;
8338 4 : get_uleb128 (form, linep, lineendp);
8339 :
8340 4 : if (! libdw_valid_user_form (form))
8341 : goto invalid_data;
8342 :
8343 : enc[i].desc = desc;
8344 4 : enc[i].form = form;
8345 :
8346 8 : printf ("%s(%s)",
8347 : dwarf_line_content_description_name (desc),
8348 : dwarf_form_name (form));
8349 4 : if (i + 1 < file_name_format_count)
8350 : printf (", ");
8351 : }
8352 2 : printf ("]\n");
8353 :
8354 : uint64_t file_name_count;
8355 2 : if ((size_t) (lineendp - linep) < 1)
8356 : goto invalid_data;
8357 2 : get_uleb128 (file_name_count, linep, lineendp);
8358 :
8359 4 : if (file_name_format_count == 0
8360 2 : && file_name_count != 0)
8361 : goto invalid_data;
8362 :
8363 8 : for (uint64_t i = 0; i < file_name_count; i++)
8364 : {
8365 8 : printf (" %-5" PRIu64 " ", i);
8366 32 : for (int j = 0; j < file_name_format_count; j++)
8367 : {
8368 16 : linep = print_form_data (dbg, enc[j].form,
8369 : linep, lineendp, length,
8370 : str_offsets_base);
8371 16 : if (j + 1 < file_name_format_count)
8372 : printf (", ");
8373 : }
8374 8 : printf ("\n");
8375 8 : if (linep >= lineendp)
8376 : goto invalid_unit;
8377 : }
8378 : }
8379 : else
8380 : {
8381 1077 : puts (gettext (" Entry Dir Time Size Name"));
8382 21318 : for (unsigned int cnt = 1; *linep != 0; ++cnt)
8383 : {
8384 : /* First comes the file name. */
8385 20241 : char *fname = (char *) linep;
8386 20241 : unsigned char *endp = memchr (fname, '\0', lineendp - linep);
8387 20241 : if (unlikely (endp == NULL))
8388 : goto invalid_unit;
8389 20241 : linep = endp + 1;
8390 :
8391 : /* Then the index. */
8392 : unsigned int diridx;
8393 20241 : if (lineendp - linep < 1)
8394 : goto invalid_unit;
8395 20241 : get_uleb128 (diridx, linep, lineendp);
8396 :
8397 : /* Next comes the modification time. */
8398 : unsigned int mtime;
8399 20241 : if (lineendp - linep < 1)
8400 : goto invalid_unit;
8401 20241 : get_uleb128 (mtime, linep, lineendp);
8402 :
8403 : /* Finally the length of the file. */
8404 : unsigned int fsize;
8405 20241 : if (lineendp - linep < 1)
8406 : goto invalid_unit;
8407 20241 : get_uleb128 (fsize, linep, lineendp);
8408 :
8409 20241 : printf (" %-5u %-5u %-9u %-9u %s\n",
8410 : cnt, diridx, mtime, fsize, fname);
8411 : }
8412 : /* Skip the final NUL byte. */
8413 1077 : ++linep;
8414 : }
8415 :
8416 1079 : puts (gettext ("\nLine number statements:"));
8417 : Dwarf_Word address = 0;
8418 : unsigned int op_index = 0;
8419 1079 : size_t line = 1;
8420 1079 : uint_fast8_t is_stmt = default_is_stmt;
8421 :
8422 : /* Determine the CU this block is for. */
8423 : Dwarf_Off cuoffset;
8424 1079 : Dwarf_Off ncuoffset = 0;
8425 : size_t hsize;
8426 85513 : while (dwarf_nextcu (dbg, cuoffset = ncuoffset, &ncuoffset, &hsize,
8427 : NULL, NULL, NULL) == 0)
8428 : {
8429 : Dwarf_Die cudie;
8430 84434 : if (dwarf_offdie (dbg, cuoffset + hsize, &cudie) == NULL)
8431 0 : continue;
8432 : Dwarf_Attribute stmt_list;
8433 84434 : if (dwarf_attr (&cudie, DW_AT_stmt_list, &stmt_list) == NULL)
8434 0 : continue;
8435 : Dwarf_Word lineoff;
8436 84434 : if (dwarf_formudata (&stmt_list, &lineoff) != 0)
8437 0 : continue;
8438 84434 : if (lineoff == start_offset)
8439 : {
8440 : /* Found the CU. */
8441 1079 : address_size = cudie.cu->address_size;
8442 1079 : break;
8443 : }
8444 : }
8445 :
8446 : /* Apply the "operation advance" from a special opcode
8447 : or DW_LNS_advance_pc (as per DWARF4 6.2.5.1). */
8448 : unsigned int op_addr_advance;
8449 : bool show_op_index;
8450 : inline void advance_pc (unsigned int op_advance)
8451 : {
8452 289406 : op_addr_advance = minimum_instr_len * ((op_index + op_advance)
8453 144703 : / max_ops_per_instr);
8454 144703 : address += op_addr_advance;
8455 289406 : show_op_index = (op_index > 0 ||
8456 144703 : (op_index + op_advance) % max_ops_per_instr > 0);
8457 144703 : op_index = (op_index + op_advance) % max_ops_per_instr;
8458 : }
8459 :
8460 1079 : if (max_ops_per_instr == 0)
8461 : {
8462 0 : error (0, 0,
8463 0 : gettext ("invalid maximum operations per instruction is zero"));
8464 0 : linep = lineendp;
8465 0 : continue;
8466 : }
8467 :
8468 234734 : while (linep < lineendp)
8469 : {
8470 233655 : size_t offset = linep - (const unsigned char *) data->d_buf;
8471 : unsigned int u128;
8472 : int s128;
8473 :
8474 : /* Read the opcode. */
8475 233655 : unsigned int opcode = *linep++;
8476 :
8477 233655 : printf (" [%6" PRIx64 "]", (uint64_t)offset);
8478 : /* Is this a special opcode? */
8479 233655 : if (likely (opcode >= opcode_base))
8480 : {
8481 107770 : if (unlikely (line_range == 0))
8482 : goto invalid_unit;
8483 :
8484 : /* Yes. Handling this is quite easy since the opcode value
8485 : is computed with
8486 :
8487 : opcode = (desired line increment - line_base)
8488 : + (line_range * address advance) + opcode_base
8489 : */
8490 107770 : int line_increment = (line_base
8491 107770 : + (opcode - opcode_base) % line_range);
8492 :
8493 : /* Perform the increments. */
8494 107770 : line += line_increment;
8495 215540 : advance_pc ((opcode - opcode_base) / line_range);
8496 :
8497 215540 : printf (gettext (" special opcode %u: address+%u = "),
8498 : opcode, op_addr_advance);
8499 107770 : print_dwarf_addr (dwflmod, 0, address, address);
8500 107770 : if (show_op_index)
8501 0 : printf (gettext (", op_index = %u, line%+d = %zu\n"),
8502 : op_index, line_increment, line);
8503 : else
8504 107770 : printf (gettext (", line%+d = %zu\n"),
8505 : line_increment, line);
8506 : }
8507 125885 : else if (opcode == 0)
8508 : {
8509 : /* This an extended opcode. */
8510 12445 : if (unlikely (linep + 2 > lineendp))
8511 : goto invalid_unit;
8512 :
8513 : /* The length. */
8514 12445 : unsigned int len = *linep++;
8515 :
8516 12445 : if (unlikely (linep + len > lineendp))
8517 : goto invalid_unit;
8518 :
8519 : /* The sub-opcode. */
8520 12445 : opcode = *linep++;
8521 :
8522 24890 : printf (gettext (" extended opcode %u: "), opcode);
8523 :
8524 12445 : switch (opcode)
8525 : {
8526 1108 : case DW_LNE_end_sequence:
8527 1108 : puts (gettext (" end of sequence"));
8528 :
8529 : /* Reset the registers we care about. */
8530 : address = 0;
8531 : op_index = 0;
8532 1108 : line = 1;
8533 1108 : is_stmt = default_is_stmt;
8534 1108 : break;
8535 :
8536 1236 : case DW_LNE_set_address:
8537 : op_index = 0;
8538 1236 : if (unlikely ((size_t) (lineendp - linep) < address_size))
8539 : goto invalid_unit;
8540 1236 : if (address_size == 4)
8541 25 : address = read_4ubyte_unaligned_inc (dbg, linep);
8542 : else
8543 1228 : address = read_8ubyte_unaligned_inc (dbg, linep);
8544 : {
8545 2472 : printf (gettext (" set address to "));
8546 1236 : print_dwarf_addr (dwflmod, 0, address, address);
8547 : printf ("\n");
8548 : }
8549 : break;
8550 :
8551 0 : case DW_LNE_define_file:
8552 : {
8553 0 : char *fname = (char *) linep;
8554 0 : unsigned char *endp = memchr (linep, '\0',
8555 0 : lineendp - linep);
8556 0 : if (unlikely (endp == NULL))
8557 : goto invalid_unit;
8558 0 : linep = endp + 1;
8559 :
8560 : unsigned int diridx;
8561 0 : if (lineendp - linep < 1)
8562 : goto invalid_unit;
8563 0 : get_uleb128 (diridx, linep, lineendp);
8564 : Dwarf_Word mtime;
8565 0 : if (lineendp - linep < 1)
8566 : goto invalid_unit;
8567 0 : get_uleb128 (mtime, linep, lineendp);
8568 : Dwarf_Word filelength;
8569 0 : if (lineendp - linep < 1)
8570 : goto invalid_unit;
8571 0 : get_uleb128 (filelength, linep, lineendp);
8572 :
8573 0 : printf (gettext ("\
8574 : define new file: dir=%u, mtime=%" PRIu64 ", length=%" PRIu64 ", name=%s\n"),
8575 : diridx, (uint64_t) mtime, (uint64_t) filelength,
8576 : fname);
8577 : }
8578 : break;
8579 :
8580 10101 : case DW_LNE_set_discriminator:
8581 : /* Takes one ULEB128 parameter, the discriminator. */
8582 10101 : if (unlikely (standard_opcode_lengths[opcode] != 1))
8583 : goto invalid_unit;
8584 :
8585 10101 : get_uleb128 (u128, linep, lineendp);
8586 10101 : printf (gettext (" set discriminator to %u\n"), u128);
8587 : break;
8588 :
8589 0 : default:
8590 : /* Unknown, ignore it. */
8591 0 : puts (gettext (" unknown opcode"));
8592 0 : linep += len - 1;
8593 0 : break;
8594 : }
8595 : }
8596 113440 : else if (opcode <= DW_LNS_set_isa)
8597 : {
8598 : /* This is a known standard opcode. */
8599 113440 : switch (opcode)
8600 : {
8601 8097 : case DW_LNS_copy:
8602 : /* Takes no argument. */
8603 8097 : puts (gettext (" copy"));
8604 8097 : break;
8605 :
8606 11681 : case DW_LNS_advance_pc:
8607 : /* Takes one uleb128 parameter which is added to the
8608 : address. */
8609 11681 : get_uleb128 (u128, linep, lineendp);
8610 11681 : advance_pc (u128);
8611 : {
8612 23362 : printf (gettext (" advance address by %u to "),
8613 : op_addr_advance);
8614 11681 : print_dwarf_addr (dwflmod, 0, address, address);
8615 11681 : if (show_op_index)
8616 0 : printf (gettext (", op_index to %u"), op_index);
8617 : printf ("\n");
8618 : }
8619 : break;
8620 :
8621 46478 : case DW_LNS_advance_line:
8622 : /* Takes one sleb128 parameter which is added to the
8623 : line. */
8624 46478 : get_sleb128 (s128, linep, lineendp);
8625 46478 : line += s128;
8626 46478 : printf (gettext ("\
8627 : advance line by constant %d to %" PRId64 "\n"),
8628 : s128, (int64_t) line);
8629 : break;
8630 :
8631 17638 : case DW_LNS_set_file:
8632 : /* Takes one uleb128 parameter which is stored in file. */
8633 17638 : get_uleb128 (u128, linep, lineendp);
8634 17638 : printf (gettext (" set file to %" PRIu64 "\n"),
8635 : (uint64_t) u128);
8636 : break;
8637 :
8638 111 : case DW_LNS_set_column:
8639 : /* Takes one uleb128 parameter which is stored in column. */
8640 111 : if (unlikely (standard_opcode_lengths[opcode] != 1))
8641 : goto invalid_unit;
8642 :
8643 111 : get_uleb128 (u128, linep, lineendp);
8644 111 : printf (gettext (" set column to %" PRIu64 "\n"),
8645 : (uint64_t) u128);
8646 : break;
8647 :
8648 4182 : case DW_LNS_negate_stmt:
8649 : /* Takes no argument. */
8650 4182 : is_stmt = 1 - is_stmt;
8651 4182 : printf (gettext (" set '%s' to %" PRIuFAST8 "\n"),
8652 : "is_stmt", is_stmt);
8653 : break;
8654 :
8655 0 : case DW_LNS_set_basic_block:
8656 : /* Takes no argument. */
8657 0 : puts (gettext (" set basic block flag"));
8658 0 : break;
8659 :
8660 25252 : case DW_LNS_const_add_pc:
8661 : /* Takes no argument. */
8662 :
8663 25252 : if (unlikely (line_range == 0))
8664 : goto invalid_unit;
8665 :
8666 50504 : advance_pc ((255 - opcode_base) / line_range);
8667 : {
8668 50504 : printf (gettext (" advance address by constant %u to "),
8669 : op_addr_advance);
8670 25252 : print_dwarf_addr (dwflmod, 0, address, address);
8671 25252 : if (show_op_index)
8672 0 : printf (gettext (", op_index to %u"), op_index);
8673 : printf ("\n");
8674 : }
8675 : break;
8676 :
8677 0 : case DW_LNS_fixed_advance_pc:
8678 : /* Takes one 16 bit parameter which is added to the
8679 : address. */
8680 0 : if (unlikely (standard_opcode_lengths[opcode] != 1))
8681 : goto invalid_unit;
8682 :
8683 0 : u128 = read_2ubyte_unaligned_inc (dbg, linep);
8684 0 : address += u128;
8685 : op_index = 0;
8686 : {
8687 0 : printf (gettext ("\
8688 : advance address by fixed value %u to \n"),
8689 : u128);
8690 0 : print_dwarf_addr (dwflmod, 0, address, address);
8691 : printf ("\n");
8692 : }
8693 : break;
8694 :
8695 1 : case DW_LNS_set_prologue_end:
8696 : /* Takes no argument. */
8697 1 : puts (gettext (" set prologue end flag"));
8698 1 : break;
8699 :
8700 0 : case DW_LNS_set_epilogue_begin:
8701 : /* Takes no argument. */
8702 0 : puts (gettext (" set epilogue begin flag"));
8703 0 : break;
8704 :
8705 0 : case DW_LNS_set_isa:
8706 : /* Takes one uleb128 parameter which is stored in isa. */
8707 0 : if (unlikely (standard_opcode_lengths[opcode] != 1))
8708 : goto invalid_unit;
8709 :
8710 0 : get_uleb128 (u128, linep, lineendp);
8711 0 : printf (gettext (" set isa to %u\n"), u128);
8712 : break;
8713 : }
8714 : }
8715 : else
8716 : {
8717 : /* This is a new opcode the generator but not we know about.
8718 : Read the parameters associated with it but then discard
8719 : everything. Read all the parameters for this opcode. */
8720 0 : printf (ngettext (" unknown opcode with %" PRIu8 " parameter:",
8721 : " unknown opcode with %" PRIu8 " parameters:",
8722 : standard_opcode_lengths[opcode]),
8723 0 : standard_opcode_lengths[opcode]);
8724 0 : for (int n = standard_opcode_lengths[opcode]; n > 0; --n)
8725 : {
8726 0 : get_uleb128 (u128, linep, lineendp);
8727 0 : if (n != standard_opcode_lengths[opcode])
8728 0 : putc_unlocked (',', stdout);
8729 0 : printf (" %u", u128);
8730 : }
8731 :
8732 : /* Next round, ignore this opcode. */
8733 0 : continue;
8734 : }
8735 : }
8736 : }
8737 :
8738 : /* There must only be one data block. */
8739 40 : assert (elf_getdata (scn, data) == NULL);
8740 : }
8741 :
8742 :
8743 : static void
8744 3 : print_debug_loclists_section (Dwfl_Module *dwflmod,
8745 : Ebl *ebl, GElf_Ehdr *ehdr,
8746 : Elf_Scn *scn, GElf_Shdr *shdr,
8747 : Dwarf *dbg)
8748 : {
8749 9 : printf (gettext ("\
8750 : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
8751 : elf_ndxscn (scn), section_name (ebl, ehdr, shdr),
8752 3 : (uint64_t) shdr->sh_offset);
8753 :
8754 6 : Elf_Data *data = (dbg->sectiondata[IDX_debug_loclists]
8755 3 : ?: elf_rawdata (scn, NULL));
8756 3 : if (unlikely (data == NULL))
8757 : {
8758 0 : error (0, 0, gettext ("cannot get .debug_loclists content: %s"),
8759 : elf_errmsg (-1));
8760 0 : return;
8761 : }
8762 :
8763 : /* For the listptr to get the base address/CU. */
8764 6 : sort_listptr (&known_loclistsptr, "loclistsptr");
8765 3 : size_t listptr_idx = 0;
8766 :
8767 3 : const unsigned char *readp = data->d_buf;
8768 3 : const unsigned char *const dataend = ((unsigned char *) data->d_buf
8769 3 : + data->d_size);
8770 10 : while (readp < dataend)
8771 : {
8772 4 : if (unlikely (readp > dataend - 4))
8773 : {
8774 0 : invalid_data:
8775 0 : error (0, 0, gettext ("invalid data in section [%zu] '%s'"),
8776 : elf_ndxscn (scn), section_name (ebl, ehdr, shdr));
8777 0 : return;
8778 : }
8779 :
8780 4 : ptrdiff_t offset = readp - (unsigned char *) data->d_buf;
8781 8 : printf (gettext ("Table at Offset 0x%" PRIx64 ":\n\n"),
8782 : (uint64_t) offset);
8783 :
8784 4 : uint64_t unit_length = read_4ubyte_unaligned_inc (dbg, readp);
8785 4 : unsigned int offset_size = 4;
8786 4 : if (unlikely (unit_length == 0xffffffff))
8787 : {
8788 0 : if (unlikely (readp > dataend - 8))
8789 : goto invalid_data;
8790 :
8791 0 : unit_length = read_8ubyte_unaligned_inc (dbg, readp);
8792 0 : offset_size = 8;
8793 : }
8794 8 : printf (gettext (" Length: %8" PRIu64 "\n"), unit_length);
8795 :
8796 : /* We need at least 2-bytes + 1-byte + 1-byte + 4-bytes = 8
8797 : bytes to complete the header. And this unit cannot go beyond
8798 : the section data. */
8799 4 : if (readp > dataend - 8
8800 4 : || unit_length < 8
8801 4 : || unit_length > (uint64_t) (dataend - readp))
8802 : goto invalid_data;
8803 :
8804 4 : const unsigned char *nexthdr = readp + unit_length;
8805 :
8806 4 : uint16_t version = read_2ubyte_unaligned_inc (dbg, readp);
8807 8 : printf (gettext (" DWARF version: %8" PRIu16 "\n"), version);
8808 :
8809 4 : if (version != 5)
8810 : {
8811 0 : error (0, 0, gettext ("Unknown version"));
8812 : goto next_table;
8813 : }
8814 :
8815 4 : uint8_t address_size = *readp++;
8816 8 : printf (gettext (" Address size: %8" PRIu64 "\n"),
8817 : (uint64_t) address_size);
8818 :
8819 4 : if (address_size != 4 && address_size != 8)
8820 : {
8821 0 : error (0, 0, gettext ("unsupported address size"));
8822 : goto next_table;
8823 : }
8824 :
8825 4 : uint8_t segment_size = *readp++;
8826 8 : printf (gettext (" Segment size: %8" PRIu64 "\n"),
8827 : (uint64_t) segment_size);
8828 :
8829 4 : if (segment_size != 0)
8830 : {
8831 0 : error (0, 0, gettext ("unsupported segment size"));
8832 : goto next_table;
8833 : }
8834 :
8835 4 : uint32_t offset_entry_count = read_4ubyte_unaligned_inc (dbg, readp);
8836 8 : printf (gettext (" Offset entries: %8" PRIu64 "\n"),
8837 : (uint64_t) offset_entry_count);
8838 :
8839 : /* We need the CU that uses this unit to get the initial base address. */
8840 4 : Dwarf_Addr cu_base = 0;
8841 4 : struct Dwarf_CU *cu = NULL;
8842 4 : if (listptr_cu (&known_loclistsptr, &listptr_idx,
8843 : (Dwarf_Off) offset,
8844 4 : (Dwarf_Off) (nexthdr - (unsigned char *) data->d_buf),
8845 : &cu_base, &cu)
8846 0 : || split_dwarf_cu_base (dbg, &cu, &cu_base))
8847 4 : {
8848 : Dwarf_Die cudie;
8849 4 : if (dwarf_cu_die (cu, &cudie,
8850 : NULL, NULL, NULL, NULL,
8851 : NULL, NULL) == NULL)
8852 0 : printf (gettext (" Unknown CU base: "));
8853 : else
8854 4 : printf (gettext (" CU [%6" PRIx64 "] base: "),
8855 : dwarf_dieoffset (&cudie));
8856 4 : print_dwarf_addr (dwflmod, address_size, cu_base, cu_base);
8857 4 : printf ("\n");
8858 : }
8859 : else
8860 0 : printf (gettext (" Not associated with a CU.\n"));
8861 :
8862 4 : printf ("\n");
8863 :
8864 4 : const unsigned char *offset_array_start = readp;
8865 4 : if (offset_entry_count > 0)
8866 : {
8867 2 : uint64_t max_entries = (unit_length - 8) / offset_size;
8868 2 : if (offset_entry_count > max_entries)
8869 : {
8870 0 : error (0, 0,
8871 0 : gettext ("too many offset entries for unit length"));
8872 0 : offset_entry_count = max_entries;
8873 : }
8874 :
8875 4 : printf (gettext (" Offsets starting at 0x%" PRIx64 ":\n"),
8876 : (uint64_t) (offset_array_start
8877 2 : - (unsigned char *) data->d_buf));
8878 18 : for (uint32_t idx = 0; idx < offset_entry_count; idx++)
8879 : {
8880 16 : printf (" [%6" PRIu32 "] ", idx);
8881 16 : if (offset_size == 4)
8882 : {
8883 16 : uint32_t off = read_4ubyte_unaligned_inc (dbg, readp);
8884 : printf ("0x%" PRIx32 "\n", off);
8885 : }
8886 : else
8887 : {
8888 0 : uint64_t off = read_8ubyte_unaligned_inc (dbg, readp);
8889 : printf ("0x%" PRIx64 "\n", off);
8890 : }
8891 : }
8892 : printf ("\n");
8893 : }
8894 :
8895 4 : Dwarf_Addr base = cu_base;
8896 4 : bool start_of_list = true;
8897 97 : while (readp < nexthdr)
8898 : {
8899 89 : uint8_t kind = *readp++;
8900 : uint64_t op1, op2, len;
8901 :
8902 : /* Skip padding. */
8903 89 : if (start_of_list && kind == DW_LLE_end_of_list)
8904 0 : continue;
8905 :
8906 89 : if (start_of_list)
8907 : {
8908 32 : base = cu_base;
8909 96 : printf (" Offset: %" PRIx64 ", Index: %" PRIx64 "\n",
8910 32 : (uint64_t) (readp - (unsigned char *) data->d_buf - 1),
8911 32 : (uint64_t) (readp - offset_array_start - 1));
8912 32 : start_of_list = false;
8913 : }
8914 :
8915 178 : printf (" %s", dwarf_loc_list_encoding_name (kind));
8916 89 : switch (kind)
8917 : {
8918 32 : case DW_LLE_end_of_list:
8919 32 : start_of_list = true;
8920 : printf ("\n\n");
8921 : break;
8922 :
8923 0 : case DW_LLE_base_addressx:
8924 0 : if ((uint64_t) (nexthdr - readp) < 1)
8925 : {
8926 0 : invalid_entry:
8927 0 : error (0, 0, gettext ("invalid loclists data"));
8928 : goto next_table;
8929 : }
8930 0 : get_uleb128 (op1, readp, nexthdr);
8931 0 : printf (" %" PRIx64 "\n", op1);
8932 0 : if (! print_unresolved_addresses)
8933 : {
8934 : Dwarf_Addr addr;
8935 0 : if (get_indexed_addr (cu, op1, &addr) != 0)
8936 : printf (" ???\n");
8937 : else
8938 : {
8939 0 : printf (" ");
8940 0 : print_dwarf_addr (dwflmod, address_size, addr, addr);
8941 : printf ("\n");
8942 : }
8943 : }
8944 : break;
8945 :
8946 0 : case DW_LLE_startx_endx:
8947 0 : if ((uint64_t) (nexthdr - readp) < 1)
8948 : goto invalid_entry;
8949 0 : get_uleb128 (op1, readp, nexthdr);
8950 0 : if ((uint64_t) (nexthdr - readp) < 1)
8951 : goto invalid_entry;
8952 0 : get_uleb128 (op2, readp, nexthdr);
8953 0 : printf (" %" PRIx64 ", %" PRIx64 "\n", op1, op2);
8954 0 : if (! print_unresolved_addresses)
8955 : {
8956 : Dwarf_Addr addr1;
8957 : Dwarf_Addr addr2;
8958 0 : if (get_indexed_addr (cu, op1, &addr1) != 0
8959 0 : || get_indexed_addr (cu, op2, &addr2) != 0)
8960 : {
8961 0 : printf (" ???..\n");
8962 : printf (" ???\n");
8963 : }
8964 : else
8965 : {
8966 0 : printf (" ");
8967 0 : print_dwarf_addr (dwflmod, address_size, addr1, addr1);
8968 0 : printf ("..\n ");
8969 0 : print_dwarf_addr (dwflmod, address_size,
8970 : addr2 - 1, addr2);
8971 : printf ("\n");
8972 : }
8973 : }
8974 0 : if ((uint64_t) (nexthdr - readp) < 1)
8975 : goto invalid_entry;
8976 0 : get_uleb128 (len, readp, nexthdr);
8977 0 : if ((uint64_t) (nexthdr - readp) < len)
8978 : goto invalid_entry;
8979 0 : print_ops (dwflmod, dbg, 8, 8, version,
8980 : address_size, offset_size, cu, len, readp);
8981 0 : readp += len;
8982 0 : break;
8983 :
8984 26 : case DW_LLE_startx_length:
8985 26 : if ((uint64_t) (nexthdr - readp) < 1)
8986 : goto invalid_entry;
8987 26 : get_uleb128 (op1, readp, nexthdr);
8988 26 : if ((uint64_t) (nexthdr - readp) < 1)
8989 : goto invalid_entry;
8990 26 : get_uleb128 (op2, readp, nexthdr);
8991 26 : printf (" %" PRIx64 ", %" PRIx64 "\n", op1, op2);
8992 26 : if (! print_unresolved_addresses)
8993 : {
8994 : Dwarf_Addr addr1;
8995 : Dwarf_Addr addr2;
8996 26 : if (get_indexed_addr (cu, op1, &addr1) != 0)
8997 : {
8998 0 : printf (" ???..\n");
8999 : printf (" ???\n");
9000 : }
9001 : else
9002 : {
9003 26 : addr2 = addr1 + op2;
9004 26 : printf (" ");
9005 26 : print_dwarf_addr (dwflmod, address_size, addr1, addr1);
9006 26 : printf ("..\n ");
9007 26 : print_dwarf_addr (dwflmod, address_size,
9008 : addr2 - 1, addr2);
9009 : printf ("\n");
9010 : }
9011 : }
9012 26 : if ((uint64_t) (nexthdr - readp) < 1)
9013 : goto invalid_entry;
9014 26 : get_uleb128 (len, readp, nexthdr);
9015 26 : if ((uint64_t) (nexthdr - readp) < len)
9016 : goto invalid_entry;
9017 26 : print_ops (dwflmod, dbg, 8, 8, version,
9018 : address_size, offset_size, cu, len, readp);
9019 26 : readp += len;
9020 26 : break;
9021 :
9022 24 : case DW_LLE_offset_pair:
9023 24 : if ((uint64_t) (nexthdr - readp) < 1)
9024 : goto invalid_entry;
9025 24 : get_uleb128 (op1, readp, nexthdr);
9026 24 : if ((uint64_t) (nexthdr - readp) < 1)
9027 : goto invalid_entry;
9028 24 : get_uleb128 (op2, readp, nexthdr);
9029 24 : printf (" %" PRIx64 ", %" PRIx64 "\n", op1, op2);
9030 24 : if (! print_unresolved_addresses)
9031 : {
9032 24 : op1 += base;
9033 24 : op2 += base;
9034 24 : printf (" ");
9035 24 : print_dwarf_addr (dwflmod, address_size, op1, op1);
9036 24 : printf ("..\n ");
9037 24 : print_dwarf_addr (dwflmod, address_size, op2 - 1, op2);
9038 : printf ("\n");
9039 : }
9040 24 : if ((uint64_t) (nexthdr - readp) < 1)
9041 : goto invalid_entry;
9042 24 : get_uleb128 (len, readp, nexthdr);
9043 24 : if ((uint64_t) (nexthdr - readp) < len)
9044 : goto invalid_entry;
9045 24 : print_ops (dwflmod, dbg, 8, 8, version,
9046 : address_size, offset_size, cu, len, readp);
9047 24 : readp += len;
9048 24 : break;
9049 :
9050 0 : case DW_LLE_default_location:
9051 0 : if ((uint64_t) (nexthdr - readp) < 1)
9052 : goto invalid_entry;
9053 0 : get_uleb128 (len, readp, nexthdr);
9054 0 : if ((uint64_t) (nexthdr - readp) < len)
9055 : goto invalid_entry;
9056 0 : print_ops (dwflmod, dbg, 8, 8, version,
9057 : address_size, offset_size, cu, len, readp);
9058 0 : readp += len;
9059 0 : break;
9060 :
9061 5 : case DW_LLE_base_address:
9062 5 : if (address_size == 4)
9063 : {
9064 0 : if ((uint64_t) (nexthdr - readp) < 4)
9065 : goto invalid_entry;
9066 0 : op1 = read_4ubyte_unaligned_inc (dbg, readp);
9067 : }
9068 : else
9069 : {
9070 5 : if ((uint64_t) (nexthdr - readp) < 8)
9071 : goto invalid_entry;
9072 5 : op1 = read_8ubyte_unaligned_inc (dbg, readp);
9073 : }
9074 5 : base = op1;
9075 5 : printf (" 0x%" PRIx64 "\n", base);
9076 5 : if (! print_unresolved_addresses)
9077 : {
9078 5 : printf (" ");
9079 5 : print_dwarf_addr (dwflmod, address_size, base, base);
9080 : printf ("\n");
9081 : }
9082 : break;
9083 :
9084 0 : case DW_LLE_start_end:
9085 0 : if (address_size == 4)
9086 : {
9087 0 : if ((uint64_t) (nexthdr - readp) < 8)
9088 : goto invalid_entry;
9089 0 : op1 = read_4ubyte_unaligned_inc (dbg, readp);
9090 0 : op2 = read_4ubyte_unaligned_inc (dbg, readp);
9091 : }
9092 : else
9093 : {
9094 0 : if ((uint64_t) (nexthdr - readp) < 16)
9095 : goto invalid_entry;
9096 0 : op1 = read_8ubyte_unaligned_inc (dbg, readp);
9097 0 : op2 = read_8ubyte_unaligned_inc (dbg, readp);
9098 : }
9099 0 : printf (" 0x%" PRIx64 "..0x%" PRIx64 "\n", op1, op2);
9100 0 : if (! print_unresolved_addresses)
9101 : {
9102 0 : printf (" ");
9103 0 : print_dwarf_addr (dwflmod, address_size, op1, op1);
9104 0 : printf ("..\n ");
9105 0 : print_dwarf_addr (dwflmod, address_size, op2 - 1, op2);
9106 : printf ("\n");
9107 : }
9108 0 : if ((uint64_t) (nexthdr - readp) < 1)
9109 : goto invalid_entry;
9110 0 : get_uleb128 (len, readp, nexthdr);
9111 0 : if ((uint64_t) (nexthdr - readp) < len)
9112 : goto invalid_entry;
9113 0 : print_ops (dwflmod, dbg, 8, 8, version,
9114 : address_size, offset_size, cu, len, readp);
9115 0 : readp += len;
9116 0 : break;
9117 :
9118 2 : case DW_LLE_start_length:
9119 2 : if (address_size == 4)
9120 : {
9121 0 : if ((uint64_t) (nexthdr - readp) < 4)
9122 : goto invalid_entry;
9123 0 : op1 = read_4ubyte_unaligned_inc (dbg, readp);
9124 : }
9125 : else
9126 : {
9127 2 : if ((uint64_t) (nexthdr - readp) < 8)
9128 : goto invalid_entry;
9129 2 : op1 = read_8ubyte_unaligned_inc (dbg, readp);
9130 : }
9131 2 : if ((uint64_t) (nexthdr - readp) < 1)
9132 : goto invalid_entry;
9133 2 : get_uleb128 (op2, readp, nexthdr);
9134 2 : printf (" 0x%" PRIx64 ", %" PRIx64 "\n", op1, op2);
9135 2 : if (! print_unresolved_addresses)
9136 : {
9137 2 : op2 = op1 + op2;
9138 2 : printf (" ");
9139 2 : print_dwarf_addr (dwflmod, address_size, op1, op1);
9140 2 : printf ("..\n ");
9141 2 : print_dwarf_addr (dwflmod, address_size, op2 - 1, op2);
9142 : printf ("\n");
9143 : }
9144 2 : if ((uint64_t) (nexthdr - readp) < 1)
9145 : goto invalid_entry;
9146 2 : get_uleb128 (len, readp, nexthdr);
9147 2 : if ((uint64_t) (nexthdr - readp) < len)
9148 : goto invalid_entry;
9149 2 : print_ops (dwflmod, dbg, 8, 8, version,
9150 : address_size, offset_size, cu, len, readp);
9151 2 : readp += len;
9152 2 : break;
9153 :
9154 : default:
9155 : goto invalid_entry;
9156 : }
9157 : }
9158 :
9159 4 : next_table:
9160 4 : if (readp != nexthdr)
9161 : {
9162 0 : size_t padding = nexthdr - readp;
9163 0 : printf (gettext (" %zu padding bytes\n\n"), padding);
9164 0 : readp = nexthdr;
9165 : }
9166 : }
9167 : }
9168 :
9169 :
9170 : static void
9171 35 : print_debug_loc_section (Dwfl_Module *dwflmod,
9172 : Ebl *ebl, GElf_Ehdr *ehdr,
9173 : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
9174 : {
9175 70 : Elf_Data *data = (dbg->sectiondata[IDX_debug_loc]
9176 35 : ?: elf_rawdata (scn, NULL));
9177 :
9178 35 : if (unlikely (data == NULL))
9179 : {
9180 0 : error (0, 0, gettext ("cannot get .debug_loc content: %s"),
9181 : elf_errmsg (-1));
9182 0 : return;
9183 : }
9184 :
9185 105 : printf (gettext ("\
9186 : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
9187 : elf_ndxscn (scn), section_name (ebl, ehdr, shdr),
9188 35 : (uint64_t) shdr->sh_offset);
9189 :
9190 70 : sort_listptr (&known_locsptr, "loclistptr");
9191 35 : size_t listptr_idx = 0;
9192 :
9193 35 : uint_fast8_t address_size = ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 4 : 8;
9194 35 : uint_fast8_t offset_size = 4;
9195 :
9196 35 : bool first = true;
9197 35 : Dwarf_Addr base = 0;
9198 35 : unsigned char *readp = data->d_buf;
9199 35 : unsigned char *const endp = (unsigned char *) data->d_buf + data->d_size;
9200 35 : Dwarf_CU *last_cu = NULL;
9201 195393 : while (readp < endp)
9202 : {
9203 195323 : ptrdiff_t offset = readp - (unsigned char *) data->d_buf;
9204 195323 : Dwarf_CU *cu = last_cu;
9205 195323 : unsigned int attr = 0;
9206 :
9207 195323 : if (first && skip_listptr_hole (&known_locsptr, &listptr_idx,
9208 : &address_size, &offset_size, &base,
9209 : &cu, offset, &readp, endp, &attr))
9210 0 : continue;
9211 :
9212 195323 : if (last_cu != cu)
9213 : {
9214 : Dwarf_Die cudie;
9215 953 : if (dwarf_cu_die (cu, &cudie,
9216 : NULL, NULL, NULL, NULL,
9217 : NULL, NULL) == NULL)
9218 0 : printf (gettext ("\n Unknown CU base: "));
9219 : else
9220 953 : printf (gettext ("\n CU [%6" PRIx64 "] base: "),
9221 : dwarf_dieoffset (&cudie));
9222 953 : print_dwarf_addr (dwflmod, address_size, base, base);
9223 953 : printf ("\n");
9224 : }
9225 195323 : last_cu = cu;
9226 :
9227 195323 : if (attr == DW_AT_GNU_locviews)
9228 : {
9229 0 : Dwarf_Off next_off = next_listptr_offset (&known_locsptr,
9230 : listptr_idx);
9231 0 : const unsigned char *locp = readp;
9232 : const unsigned char *locendp;
9233 0 : if (next_off == 0)
9234 : locendp = endp;
9235 : else
9236 0 : locendp = (const unsigned char *) data->d_buf + next_off;
9237 :
9238 0 : while (locp < locendp)
9239 : {
9240 : uint64_t v1, v2;
9241 0 : get_uleb128 (v1, locp, locendp);
9242 0 : if (locp >= locendp)
9243 : {
9244 0 : printf (gettext (" [%6tx] <INVALID DATA>\n"), offset);
9245 : break;
9246 : }
9247 0 : get_uleb128 (v2, locp, locendp);
9248 0 : if (first) /* First view pair in a list. */
9249 : printf (" [%6tx] ", offset);
9250 : else
9251 : printf (" ");
9252 0 : printf ("view pair %" PRId64 ", %" PRId64 "\n", v1, v2);
9253 0 : first = false;
9254 : }
9255 :
9256 0 : first = true;
9257 0 : readp = (unsigned char *) locendp;
9258 0 : continue;
9259 : }
9260 :
9261 : /* GNU DebugFission encoded addresses as addrx. */
9262 195323 : bool is_debugfission = ((cu != NULL
9263 0 : || split_dwarf_cu_base (dbg, &cu, &base))
9264 390646 : && (cu->version < 5
9265 195323 : && cu->unit_type == DW_UT_split_compile));
9266 195323 : if (!is_debugfission
9267 195263 : && unlikely (data->d_size - offset < (size_t) address_size * 2))
9268 : {
9269 0 : invalid_data:
9270 0 : printf (gettext (" [%6tx] <INVALID DATA>\n"), offset);
9271 0 : break;
9272 : }
9273 :
9274 : Dwarf_Addr begin;
9275 : Dwarf_Addr end;
9276 195323 : bool use_base = true;
9277 195323 : if (is_debugfission)
9278 : {
9279 60 : const unsigned char *locp = readp;
9280 60 : const unsigned char *locendp = readp + data->d_size;
9281 60 : if (locp >= locendp)
9282 : goto invalid_data;
9283 :
9284 : Dwarf_Word idx;
9285 60 : unsigned char code = *locp++;
9286 60 : switch (code)
9287 : {
9288 20 : case DW_LLE_GNU_end_of_list_entry:
9289 20 : begin = 0;
9290 20 : end = 0;
9291 20 : break;
9292 :
9293 0 : case DW_LLE_GNU_base_address_selection_entry:
9294 0 : if (locp >= locendp)
9295 : goto invalid_data;
9296 0 : begin = (Dwarf_Addr) -1;
9297 0 : get_uleb128 (idx, locp, locendp);
9298 0 : if (get_indexed_addr (cu, idx, &end) != 0)
9299 0 : end = idx; /* ... */
9300 : break;
9301 :
9302 0 : case DW_LLE_GNU_start_end_entry:
9303 0 : if (locp >= locendp)
9304 : goto invalid_data;
9305 0 : get_uleb128 (idx, locp, locendp);
9306 0 : if (get_indexed_addr (cu, idx, &begin) != 0)
9307 0 : begin = idx; /* ... */
9308 0 : if (locp >= locendp)
9309 : goto invalid_data;
9310 0 : get_uleb128 (idx, locp, locendp);
9311 0 : if (get_indexed_addr (cu, idx, &end) != 0)
9312 0 : end = idx; /* ... */
9313 : use_base = false;
9314 : break;
9315 :
9316 40 : case DW_LLE_GNU_start_length_entry:
9317 40 : if (locp >= locendp)
9318 : goto invalid_data;
9319 40 : get_uleb128 (idx, locp, locendp);
9320 40 : if (get_indexed_addr (cu, idx, &begin) != 0)
9321 0 : begin = idx; /* ... */
9322 40 : if (locendp - locp < 4)
9323 : goto invalid_data;
9324 40 : end = read_4ubyte_unaligned_inc (dbg, locp);
9325 40 : end += begin;
9326 40 : use_base = false;
9327 40 : break;
9328 :
9329 : default:
9330 : goto invalid_data;
9331 : }
9332 :
9333 60 : readp = (unsigned char *) locp;
9334 : }
9335 195263 : else if (address_size == 8)
9336 : {
9337 195263 : begin = read_8ubyte_unaligned_inc (dbg, readp);
9338 195263 : end = read_8ubyte_unaligned_inc (dbg, readp);
9339 : }
9340 : else
9341 : {
9342 0 : begin = read_4ubyte_unaligned_inc (dbg, readp);
9343 0 : end = read_4ubyte_unaligned_inc (dbg, readp);
9344 0 : if (begin == (Dwarf_Addr) (uint32_t) -1)
9345 0 : begin = (Dwarf_Addr) -1l;
9346 : }
9347 :
9348 195323 : if (begin == (Dwarf_Addr) -1l) /* Base address entry. */
9349 : {
9350 0 : printf (gettext (" [%6tx] base address\n "), offset);
9351 0 : print_dwarf_addr (dwflmod, address_size, end, end);
9352 0 : printf ("\n");
9353 0 : base = end;
9354 : }
9355 195323 : else if (begin == 0 && end == 0) /* End of list entry. */
9356 : {
9357 38890 : if (first)
9358 0 : printf (gettext (" [%6tx] empty list\n"), offset);
9359 : first = true;
9360 : }
9361 : else
9362 : {
9363 : /* We have a location expression entry. */
9364 156433 : uint_fast16_t len = read_2ubyte_unaligned_inc (dbg, readp);
9365 :
9366 156433 : if (first) /* First entry in a list. */
9367 : printf (" [%6tx] ", offset);
9368 : else
9369 : printf (" ");
9370 :
9371 312866 : printf ("range %" PRIx64 ", %" PRIx64 "\n", begin, end);
9372 156433 : if (! print_unresolved_addresses)
9373 : {
9374 156415 : Dwarf_Addr dab = use_base ? base + begin : begin;
9375 156415 : Dwarf_Addr dae = use_base ? base + end : end;
9376 156415 : printf (" ");
9377 156415 : print_dwarf_addr (dwflmod, address_size, dab, dab);
9378 156415 : printf ("..\n ");
9379 156415 : print_dwarf_addr (dwflmod, address_size, dae - 1, dae);
9380 : printf ("\n");
9381 : }
9382 :
9383 156433 : if (endp - readp <= (ptrdiff_t) len)
9384 : {
9385 0 : fputs (gettext (" <INVALID DATA>\n"), stdout);
9386 0 : break;
9387 : }
9388 :
9389 312866 : print_ops (dwflmod, dbg, 11, 11,
9390 156433 : cu != NULL ? cu->version : 3,
9391 : address_size, offset_size, cu, len, readp);
9392 :
9393 156433 : first = false;
9394 156433 : readp += len;
9395 : }
9396 : }
9397 : }
9398 :
9399 : struct mac_culist
9400 : {
9401 : Dwarf_Die die;
9402 : Dwarf_Off offset;
9403 : Dwarf_Files *files;
9404 : struct mac_culist *next;
9405 : };
9406 :
9407 :
9408 : static int
9409 0 : mac_compare (const void *p1, const void *p2)
9410 : {
9411 0 : struct mac_culist *m1 = (struct mac_culist *) p1;
9412 0 : struct mac_culist *m2 = (struct mac_culist *) p2;
9413 :
9414 0 : if (m1->offset < m2->offset)
9415 : return -1;
9416 0 : if (m1->offset > m2->offset)
9417 : return 1;
9418 0 : return 0;
9419 : }
9420 :
9421 :
9422 : static void
9423 0 : print_debug_macinfo_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
9424 : Ebl *ebl, GElf_Ehdr *ehdr,
9425 : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
9426 : {
9427 0 : printf (gettext ("\
9428 : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
9429 : elf_ndxscn (scn), section_name (ebl, ehdr, shdr),
9430 0 : (uint64_t) shdr->sh_offset);
9431 0 : putc_unlocked ('\n', stdout);
9432 :
9433 : /* There is no function in libdw to iterate over the raw content of
9434 : the section but it is easy enough to do. */
9435 0 : Elf_Data *data = (dbg->sectiondata[IDX_debug_macinfo]
9436 0 : ?: elf_rawdata (scn, NULL));
9437 0 : if (unlikely (data == NULL))
9438 : {
9439 0 : error (0, 0, gettext ("cannot get macro information section data: %s"),
9440 : elf_errmsg (-1));
9441 0 : return;
9442 : }
9443 :
9444 : /* Get the source file information for all CUs. */
9445 : Dwarf_Off offset;
9446 0 : Dwarf_Off ncu = 0;
9447 : size_t hsize;
9448 0 : struct mac_culist *culist = NULL;
9449 0 : size_t nculist = 0;
9450 0 : while (dwarf_nextcu (dbg, offset = ncu, &ncu, &hsize, NULL, NULL, NULL) == 0)
9451 : {
9452 : Dwarf_Die cudie;
9453 0 : if (dwarf_offdie (dbg, offset + hsize, &cudie) == NULL)
9454 0 : continue;
9455 :
9456 : Dwarf_Attribute attr;
9457 0 : if (dwarf_attr (&cudie, DW_AT_macro_info, &attr) == NULL)
9458 0 : continue;
9459 :
9460 : Dwarf_Word macoff;
9461 0 : if (dwarf_formudata (&attr, &macoff) != 0)
9462 0 : continue;
9463 :
9464 0 : struct mac_culist *newp = (struct mac_culist *) alloca (sizeof (*newp));
9465 0 : newp->die = cudie;
9466 0 : newp->offset = macoff;
9467 0 : newp->files = NULL;
9468 0 : newp->next = culist;
9469 0 : culist = newp;
9470 0 : ++nculist;
9471 : }
9472 :
9473 : /* Convert the list into an array for easier consumption. */
9474 0 : struct mac_culist *cus = (struct mac_culist *) alloca ((nculist + 1)
9475 : * sizeof (*cus));
9476 : /* Add sentinel. */
9477 0 : cus[nculist].offset = data->d_size;
9478 0 : cus[nculist].files = (Dwarf_Files *) -1l;
9479 0 : if (nculist > 0)
9480 : {
9481 0 : for (size_t cnt = nculist - 1; culist != NULL; --cnt)
9482 : {
9483 0 : assert (cnt < nculist);
9484 0 : cus[cnt] = *culist;
9485 0 : culist = culist->next;
9486 : }
9487 :
9488 : /* Sort the array according to the offset in the .debug_macinfo
9489 : section. Note we keep the sentinel at the end. */
9490 0 : qsort (cus, nculist, sizeof (*cus), mac_compare);
9491 : }
9492 :
9493 0 : const unsigned char *readp = (const unsigned char *) data->d_buf;
9494 0 : const unsigned char *readendp = readp + data->d_size;
9495 0 : int level = 1;
9496 :
9497 0 : while (readp < readendp)
9498 : {
9499 0 : unsigned int opcode = *readp++;
9500 : unsigned int u128;
9501 : unsigned int u128_2;
9502 : const unsigned char *endp;
9503 :
9504 0 : switch (opcode)
9505 : {
9506 0 : case DW_MACINFO_define:
9507 : case DW_MACINFO_undef:
9508 : case DW_MACINFO_vendor_ext:
9509 : /* For the first two opcodes the parameters are
9510 : line, string
9511 : For the latter
9512 : number, string.
9513 : We can treat these cases together. */
9514 0 : get_uleb128 (u128, readp, readendp);
9515 :
9516 0 : endp = memchr (readp, '\0', readendp - readp);
9517 0 : if (unlikely (endp == NULL))
9518 : {
9519 0 : printf (gettext ("\
9520 : %*s*** non-terminated string at end of section"),
9521 : level, "");
9522 : return;
9523 : }
9524 :
9525 0 : if (opcode == DW_MACINFO_define)
9526 0 : printf ("%*s#define %s, line %u\n",
9527 : level, "", (char *) readp, u128);
9528 0 : else if (opcode == DW_MACINFO_undef)
9529 0 : printf ("%*s#undef %s, line %u\n",
9530 : level, "", (char *) readp, u128);
9531 : else
9532 0 : printf (" #vendor-ext %s, number %u\n", (char *) readp, u128);
9533 :
9534 0 : readp = endp + 1;
9535 0 : break;
9536 :
9537 0 : case DW_MACINFO_start_file:
9538 : /* The two parameters are line and file index, in this order. */
9539 0 : get_uleb128 (u128, readp, readendp);
9540 0 : if (readendp - readp < 1)
9541 : {
9542 0 : printf (gettext ("\
9543 : %*s*** missing DW_MACINFO_start_file argument at end of section"),
9544 : level, "");
9545 : return;
9546 : }
9547 0 : get_uleb128 (u128_2, readp, readendp);
9548 :
9549 : /* Find the CU DIE for this file. */
9550 0 : size_t macoff = readp - (const unsigned char *) data->d_buf;
9551 0 : const char *fname = "???";
9552 0 : if (macoff >= cus[0].offset)
9553 : {
9554 0 : while (macoff >= cus[1].offset && cus[1].offset != data->d_size)
9555 0 : ++cus;
9556 :
9557 0 : if (cus[0].files == NULL
9558 0 : && dwarf_getsrcfiles (&cus[0].die, &cus[0].files, NULL) != 0)
9559 0 : cus[0].files = (Dwarf_Files *) -1l;
9560 :
9561 0 : if (cus[0].files != (Dwarf_Files *) -1l)
9562 0 : fname = (dwarf_filesrc (cus[0].files, u128_2, NULL, NULL)
9563 0 : ?: "???");
9564 : }
9565 :
9566 0 : printf ("%*sstart_file %u, [%u] %s\n",
9567 : level, "", u128, u128_2, fname);
9568 0 : ++level;
9569 0 : break;
9570 :
9571 0 : case DW_MACINFO_end_file:
9572 0 : --level;
9573 : printf ("%*send_file\n", level, "");
9574 : /* Nothing more to do. */
9575 : break;
9576 :
9577 0 : default:
9578 : // XXX gcc seems to generate files with a trailing zero.
9579 0 : if (unlikely (opcode != 0 || readp != readendp))
9580 : printf ("%*s*** invalid opcode %u\n", level, "", opcode);
9581 : break;
9582 : }
9583 : }
9584 : }
9585 :
9586 :
9587 : static void
9588 3 : print_debug_macro_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
9589 : Ebl *ebl, GElf_Ehdr *ehdr,
9590 : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
9591 : {
9592 9 : printf (gettext ("\
9593 : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
9594 : elf_ndxscn (scn), section_name (ebl, ehdr, shdr),
9595 3 : (uint64_t) shdr->sh_offset);
9596 6 : putc_unlocked ('\n', stdout);
9597 :
9598 6 : Elf_Data *data = (dbg->sectiondata[IDX_debug_macro]
9599 3 : ?: elf_rawdata (scn, NULL));
9600 3 : if (unlikely (data == NULL))
9601 : {
9602 0 : error (0, 0, gettext ("cannot get macro information section data: %s"),
9603 : elf_errmsg (-1));
9604 0 : return;
9605 : }
9606 :
9607 : /* Get the source file information for all CUs. Uses same
9608 : datastructure as macinfo. But uses offset field to directly
9609 : match .debug_line offset. And just stored in a list. */
9610 : Dwarf_Off offset;
9611 3 : Dwarf_Off ncu = 0;
9612 : size_t hsize;
9613 3 : struct mac_culist *culist = NULL;
9614 3 : size_t nculist = 0;
9615 10 : while (dwarf_nextcu (dbg, offset = ncu, &ncu, &hsize, NULL, NULL, NULL) == 0)
9616 : {
9617 : Dwarf_Die cudie;
9618 4 : if (dwarf_offdie (dbg, offset + hsize, &cudie) == NULL)
9619 0 : continue;
9620 :
9621 : Dwarf_Attribute attr;
9622 4 : if (dwarf_attr (&cudie, DW_AT_stmt_list, &attr) == NULL)
9623 0 : continue;
9624 :
9625 : Dwarf_Word lineoff;
9626 4 : if (dwarf_formudata (&attr, &lineoff) != 0)
9627 0 : continue;
9628 :
9629 4 : struct mac_culist *newp = (struct mac_culist *) alloca (sizeof (*newp));
9630 4 : newp->die = cudie;
9631 4 : newp->offset = lineoff;
9632 4 : newp->files = NULL;
9633 4 : newp->next = culist;
9634 4 : culist = newp;
9635 4 : ++nculist;
9636 : }
9637 :
9638 3 : const unsigned char *readp = (const unsigned char *) data->d_buf;
9639 3 : const unsigned char *readendp = readp + data->d_size;
9640 :
9641 14 : while (readp < readendp)
9642 : {
9643 16 : printf (gettext (" Offset: 0x%" PRIx64 "\n"),
9644 8 : (uint64_t) (readp - (const unsigned char *) data->d_buf));
9645 :
9646 : // Header, 2 byte version, 1 byte flag, optional .debug_line offset,
9647 : // optional vendor extension macro entry table.
9648 8 : if (readp + 2 > readendp)
9649 : {
9650 0 : invalid_data:
9651 0 : error (0, 0, gettext ("invalid data"));
9652 0 : return;
9653 : }
9654 8 : const uint16_t vers = read_2ubyte_unaligned_inc (dbg, readp);
9655 16 : printf (gettext (" Version: %" PRIu16 "\n"), vers);
9656 :
9657 : // Version 4 is the GNU extension for DWARF4. DWARF5 will use version
9658 : // 5 when it gets standardized.
9659 8 : if (vers != 4 && vers != 5)
9660 : {
9661 0 : printf (gettext (" unknown version, cannot parse section\n"));
9662 : return;
9663 : }
9664 :
9665 8 : if (readp + 1 > readendp)
9666 : goto invalid_data;
9667 8 : const unsigned char flag = *readp++;
9668 16 : printf (gettext (" Flag: 0x%" PRIx8 "\n"), flag);
9669 :
9670 8 : unsigned int offset_len = (flag & 0x01) ? 8 : 4;
9671 16 : printf (gettext (" Offset length: %" PRIu8 "\n"), offset_len);
9672 8 : Dwarf_Off line_offset = -1;
9673 8 : if (flag & 0x02)
9674 : {
9675 4 : if (offset_len == 8)
9676 0 : line_offset = read_8ubyte_unaligned_inc (dbg, readp);
9677 : else
9678 4 : line_offset = read_4ubyte_unaligned_inc (dbg, readp);
9679 4 : printf (gettext (" .debug_line offset: 0x%" PRIx64 "\n"),
9680 : line_offset);
9681 : }
9682 :
9683 4 : struct mac_culist *cu = NULL;
9684 4 : if (line_offset != (Dwarf_Off) -1)
9685 : {
9686 : cu = culist;
9687 5 : while (cu != NULL && line_offset != cu->offset)
9688 1 : cu = cu->next;
9689 : }
9690 :
9691 8 : Dwarf_Off str_offsets_base = str_offsets_base_off (dbg, (cu != NULL
9692 : ? cu->die.cu
9693 : : NULL));
9694 :
9695 : const unsigned char *vendor[DW_MACRO_hi_user - DW_MACRO_lo_user + 1];
9696 8 : memset (vendor, 0, sizeof vendor);
9697 8 : if (flag & 0x04)
9698 : {
9699 : // 1 byte length, for each item, 1 byte opcode, uleb128 number
9700 : // of arguments, for each argument 1 byte form code.
9701 0 : if (readp + 1 > readendp)
9702 : goto invalid_data;
9703 0 : unsigned int tlen = *readp++;
9704 0 : printf (gettext (" extension opcode table, %" PRIu8 " items:\n"),
9705 : tlen);
9706 0 : for (unsigned int i = 0; i < tlen; i++)
9707 : {
9708 0 : if (readp + 1 > readendp)
9709 : goto invalid_data;
9710 0 : unsigned int opcode = *readp++;
9711 0 : printf (gettext (" [%" PRIx8 "]"), opcode);
9712 0 : if (opcode < DW_MACRO_lo_user
9713 0 : || opcode > DW_MACRO_hi_user)
9714 : goto invalid_data;
9715 : // Record the start of description for this vendor opcode.
9716 : // uleb128 nr args, 1 byte per arg form.
9717 0 : vendor[opcode - DW_MACRO_lo_user] = readp;
9718 0 : if (readp + 1 > readendp)
9719 : goto invalid_data;
9720 0 : unsigned int args = *readp++;
9721 0 : if (args > 0)
9722 : {
9723 0 : printf (gettext (" %" PRIu8 " arguments:"), args);
9724 0 : while (args > 0)
9725 : {
9726 0 : if (readp + 1 > readendp)
9727 : goto invalid_data;
9728 0 : unsigned int form = *readp++;
9729 0 : printf (" %s", dwarf_form_name (form));
9730 0 : if (! libdw_valid_user_form (form))
9731 : goto invalid_data;
9732 0 : args--;
9733 0 : if (args > 0)
9734 : putchar_unlocked (',');
9735 : }
9736 : }
9737 : else
9738 0 : printf (gettext (" no arguments."));
9739 0 : putchar_unlocked ('\n');
9740 : }
9741 : }
9742 8 : putchar_unlocked ('\n');
9743 :
9744 8 : int level = 1;
9745 8 : if (readp + 1 > readendp)
9746 : goto invalid_data;
9747 8 : unsigned int opcode = *readp++;
9748 743 : while (opcode != 0)
9749 : {
9750 : unsigned int u128;
9751 : unsigned int u128_2;
9752 : const unsigned char *endp;
9753 : uint64_t off;
9754 :
9755 727 : switch (opcode)
9756 : {
9757 6 : case DW_MACRO_start_file:
9758 6 : get_uleb128 (u128, readp, readendp);
9759 6 : if (readp >= readendp)
9760 : goto invalid_data;
9761 6 : get_uleb128 (u128_2, readp, readendp);
9762 :
9763 : /* Find the CU DIE that matches this line offset. */
9764 6 : const char *fname = "???";
9765 6 : if (cu != NULL)
9766 : {
9767 6 : if (cu->files == NULL
9768 4 : && dwarf_getsrcfiles (&cu->die, &cu->files,
9769 : NULL) != 0)
9770 0 : cu->files = (Dwarf_Files *) -1l;
9771 :
9772 6 : if (cu->files != (Dwarf_Files *) -1l)
9773 6 : fname = (dwarf_filesrc (cu->files, u128_2,
9774 6 : NULL, NULL) ?: "???");
9775 : }
9776 6 : printf ("%*sstart_file %u, [%u] %s\n",
9777 : level, "", u128, u128_2, fname);
9778 6 : ++level;
9779 6 : break;
9780 :
9781 6 : case DW_MACRO_end_file:
9782 6 : --level;
9783 : printf ("%*send_file\n", level, "");
9784 : break;
9785 :
9786 1 : case DW_MACRO_define:
9787 1 : get_uleb128 (u128, readp, readendp);
9788 1 : endp = memchr (readp, '\0', readendp - readp);
9789 1 : if (endp == NULL)
9790 : goto invalid_data;
9791 2 : printf ("%*s#define %s, line %u\n",
9792 : level, "", readp, u128);
9793 1 : readp = endp + 1;
9794 1 : break;
9795 :
9796 0 : case DW_MACRO_undef:
9797 0 : get_uleb128 (u128, readp, readendp);
9798 0 : endp = memchr (readp, '\0', readendp - readp);
9799 0 : if (endp == NULL)
9800 : goto invalid_data;
9801 0 : printf ("%*s#undef %s, line %u\n",
9802 : level, "", readp, u128);
9803 0 : readp = endp + 1;
9804 0 : break;
9805 :
9806 707 : case DW_MACRO_define_strp:
9807 707 : get_uleb128 (u128, readp, readendp);
9808 707 : if (readp + offset_len > readendp)
9809 : goto invalid_data;
9810 707 : if (offset_len == 8)
9811 0 : off = read_8ubyte_unaligned_inc (dbg, readp);
9812 : else
9813 707 : off = read_4ubyte_unaligned_inc (dbg, readp);
9814 707 : printf ("%*s#define %s, line %u (indirect)\n",
9815 : level, "", dwarf_getstring (dbg, off, NULL), u128);
9816 : break;
9817 :
9818 1 : case DW_MACRO_undef_strp:
9819 1 : get_uleb128 (u128, readp, readendp);
9820 1 : if (readp + offset_len > readendp)
9821 : goto invalid_data;
9822 1 : if (offset_len == 8)
9823 0 : off = read_8ubyte_unaligned_inc (dbg, readp);
9824 : else
9825 1 : off = read_4ubyte_unaligned_inc (dbg, readp);
9826 1 : printf ("%*s#undef %s, line %u (indirect)\n",
9827 : level, "", dwarf_getstring (dbg, off, NULL), u128);
9828 : break;
9829 :
9830 6 : case DW_MACRO_import:
9831 6 : if (readp + offset_len > readendp)
9832 : goto invalid_data;
9833 6 : if (offset_len == 8)
9834 0 : off = read_8ubyte_unaligned_inc (dbg, readp);
9835 : else
9836 6 : off = read_4ubyte_unaligned_inc (dbg, readp);
9837 : printf ("%*s#include offset 0x%" PRIx64 "\n",
9838 : level, "", off);
9839 : break;
9840 :
9841 0 : case DW_MACRO_define_sup:
9842 0 : get_uleb128 (u128, readp, readendp);
9843 0 : if (readp + offset_len > readendp)
9844 : goto invalid_data;
9845 0 : printf ("%*s#define ", level, "");
9846 0 : readp = print_form_data (dbg, DW_FORM_strp_sup,
9847 : readp, readendp, offset_len,
9848 : str_offsets_base);
9849 : printf (", line %u (sup)\n", u128);
9850 : break;
9851 :
9852 0 : case DW_MACRO_undef_sup:
9853 0 : get_uleb128 (u128, readp, readendp);
9854 0 : if (readp + offset_len > readendp)
9855 : goto invalid_data;
9856 0 : printf ("%*s#undef ", level, "");
9857 0 : readp = print_form_data (dbg, DW_FORM_strp_sup,
9858 : readp, readendp, offset_len,
9859 : str_offsets_base);
9860 : printf (", line %u (sup)\n", u128);
9861 : break;
9862 :
9863 0 : case DW_MACRO_import_sup:
9864 0 : if (readp + offset_len > readendp)
9865 : goto invalid_data;
9866 0 : if (offset_len == 8)
9867 0 : off = read_8ubyte_unaligned_inc (dbg, readp);
9868 : else
9869 0 : off = read_4ubyte_unaligned_inc (dbg, readp);
9870 : // XXX Needs support for reading from supplementary object file.
9871 : printf ("%*s#include offset 0x%" PRIx64 " (sup)\n",
9872 : level, "", off);
9873 : break;
9874 :
9875 0 : case DW_MACRO_define_strx:
9876 0 : get_uleb128 (u128, readp, readendp);
9877 0 : if (readp + offset_len > readendp)
9878 : goto invalid_data;
9879 0 : printf ("%*s#define ", level, "");
9880 0 : readp = print_form_data (dbg, DW_FORM_strx,
9881 : readp, readendp, offset_len,
9882 : str_offsets_base);
9883 : printf (", line %u (strx)\n", u128);
9884 : break;
9885 :
9886 0 : case DW_MACRO_undef_strx:
9887 0 : get_uleb128 (u128, readp, readendp);
9888 0 : if (readp + offset_len > readendp)
9889 : goto invalid_data;
9890 0 : printf ("%*s#undef ", level, "");
9891 0 : readp = print_form_data (dbg, DW_FORM_strx,
9892 : readp, readendp, offset_len,
9893 : str_offsets_base);
9894 : printf (", line %u (strx)\n", u128);
9895 : break;
9896 :
9897 0 : default:
9898 0 : printf ("%*svendor opcode 0x%" PRIx8, level, "", opcode);
9899 0 : if (opcode < DW_MACRO_lo_user
9900 : || opcode > DW_MACRO_lo_user
9901 0 : || vendor[opcode - DW_MACRO_lo_user] == NULL)
9902 : goto invalid_data;
9903 :
9904 : const unsigned char *op_desc;
9905 0 : op_desc = vendor[opcode - DW_MACRO_lo_user];
9906 :
9907 : // Just skip the arguments, we cannot really interpret them,
9908 : // but print as much as we can.
9909 0 : unsigned int args = *op_desc++;
9910 0 : while (args > 0 && readp < readendp)
9911 0 : {
9912 0 : unsigned int form = *op_desc++;
9913 0 : readp = print_form_data (dbg, form, readp, readendp,
9914 : offset_len, str_offsets_base);
9915 0 : args--;
9916 0 : if (args > 0)
9917 : printf (", ");
9918 : }
9919 : putchar_unlocked ('\n');
9920 : }
9921 :
9922 727 : if (readp + 1 > readendp)
9923 : goto invalid_data;
9924 727 : opcode = *readp++;
9925 727 : if (opcode == 0)
9926 : putchar_unlocked ('\n');
9927 : }
9928 : }
9929 : }
9930 :
9931 :
9932 : /* Callback for printing global names. */
9933 : static int
9934 34 : print_pubnames (Dwarf *dbg __attribute__ ((unused)), Dwarf_Global *global,
9935 : void *arg)
9936 : {
9937 34 : int *np = (int *) arg;
9938 :
9939 102 : printf (gettext (" [%5d] DIE offset: %6" PRId64
9940 : ", CU DIE offset: %6" PRId64 ", name: %s\n"),
9941 34 : (*np)++, global->die_offset, global->cu_offset, global->name);
9942 :
9943 34 : return 0;
9944 : }
9945 :
9946 :
9947 : /* Print the known exported symbols in the DWARF section '.debug_pubnames'. */
9948 : static void
9949 8 : print_debug_pubnames_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
9950 : Ebl *ebl, GElf_Ehdr *ehdr,
9951 : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
9952 : {
9953 24 : printf (gettext ("\nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
9954 : elf_ndxscn (scn), section_name (ebl, ehdr, shdr),
9955 8 : (uint64_t) shdr->sh_offset);
9956 :
9957 8 : int n = 0;
9958 8 : (void) dwarf_getpubnames (dbg, print_pubnames, &n, 0);
9959 8 : }
9960 :
9961 : /* Print the content of the DWARF string section '.debug_str'. */
9962 : static void
9963 39 : print_debug_str_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
9964 : Ebl *ebl, GElf_Ehdr *ehdr,
9965 : Elf_Scn *scn, GElf_Shdr *shdr,
9966 : Dwarf *dbg __attribute__ ((unused)))
9967 : {
9968 39 : Elf_Data *data = elf_rawdata (scn, NULL);
9969 39 : const size_t sh_size = data ? data->d_size : 0;
9970 :
9971 : /* Compute floor(log16(shdr->sh_size)). */
9972 39 : GElf_Addr tmp = sh_size;
9973 39 : int digits = 1;
9974 182 : while (tmp >= 16)
9975 : {
9976 104 : ++digits;
9977 104 : tmp >>= 4;
9978 : }
9979 39 : digits = MAX (4, digits);
9980 :
9981 156 : printf (gettext ("\nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"
9982 : " %*s String\n"),
9983 : elf_ndxscn (scn),
9984 39 : section_name (ebl, ehdr, shdr), (uint64_t) shdr->sh_offset,
9985 : /* TRANS: the debugstr| prefix makes the string unique. */
9986 39 : digits + 2, sgettext ("debugstr|Offset"));
9987 :
9988 39 : Dwarf_Off offset = 0;
9989 60547 : while (offset < sh_size)
9990 : {
9991 : size_t len;
9992 60469 : const char *str = (const char *) data->d_buf + offset;
9993 60469 : const char *endp = memchr (str, '\0', sh_size - offset);
9994 60469 : if (unlikely (endp == NULL))
9995 : {
9996 0 : printf (gettext (" *** error, missing string terminator\n"));
9997 : break;
9998 : }
9999 :
10000 60469 : printf (" [%*" PRIx64 "] \"%s\"\n", digits, (uint64_t) offset, str);
10001 60469 : len = endp - str;
10002 60469 : offset += len + 1;
10003 : }
10004 39 : }
10005 :
10006 : static void
10007 4 : print_debug_str_offsets_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
10008 : Ebl *ebl, GElf_Ehdr *ehdr,
10009 : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
10010 : {
10011 12 : printf (gettext ("\
10012 : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
10013 : elf_ndxscn (scn), section_name (ebl, ehdr, shdr),
10014 4 : (uint64_t) shdr->sh_offset);
10015 :
10016 4 : if (shdr->sh_size == 0)
10017 : return;
10018 :
10019 : /* We like to get the section from libdw to make sure they are relocated. */
10020 8 : Elf_Data *data = (dbg->sectiondata[IDX_debug_str_offsets]
10021 4 : ?: elf_rawdata (scn, NULL));
10022 4 : if (unlikely (data == NULL))
10023 : {
10024 0 : error (0, 0, gettext ("cannot get .debug_str_offsets section data: %s"),
10025 : elf_errmsg (-1));
10026 : return;
10027 : }
10028 :
10029 4 : size_t idx = 0;
10030 8 : sort_listptr (&known_stroffbases, "str_offsets");
10031 :
10032 4 : const unsigned char *start = (const unsigned char *) data->d_buf;
10033 4 : const unsigned char *readp = start;
10034 4 : const unsigned char *readendp = ((const unsigned char *) data->d_buf
10035 4 : + data->d_size);
10036 :
10037 12 : while (readp < readendp)
10038 : {
10039 : /* Most string offset tables will have a header. For split
10040 : dwarf unit GNU DebugFission didn't add one. But they were
10041 : also only defined for split units (main or skeleton units
10042 : didn't have indirect strings). So if we don't have a
10043 : DW_AT_str_offsets_base at all and this is offset zero, then
10044 : just start printing offsets immediately, if this is a .dwo
10045 : section. */
10046 4 : Dwarf_Off off = (Dwarf_Off) (readp
10047 4 : - (const unsigned char *) data->d_buf);
10048 :
10049 4 : printf ("Table at offset %" PRIx64 " ", off);
10050 :
10051 4 : struct listptr *listptr = get_listptr (&known_stroffbases, idx++);
10052 0 : const unsigned char *next_unitp = readendp;
10053 : uint8_t offset_size;
10054 : bool has_header;
10055 0 : if (listptr == NULL)
10056 : {
10057 : /* This can happen for .dwo files. There is only an header
10058 : in the case this is a version 5 split DWARF file. */
10059 : Dwarf_CU *cu;
10060 : uint8_t unit_type;
10061 4 : if (dwarf_get_units (dbg, NULL, &cu, NULL, &unit_type,
10062 : NULL, NULL) != 0)
10063 : {
10064 0 : error (0, 0, "Warning: Cannot find any DWARF unit.");
10065 : /* Just guess some values. */
10066 0 : has_header = false;
10067 0 : offset_size = 4;
10068 : }
10069 4 : else if (off == 0
10070 4 : && (unit_type == DW_UT_split_type
10071 4 : || unit_type == DW_UT_split_compile))
10072 : {
10073 4 : has_header = cu->version > 4;
10074 4 : offset_size = cu->offset_size;
10075 : }
10076 : else
10077 : {
10078 0 : error (0, 0,
10079 : "Warning: No CU references .debug_str_offsets after %"
10080 : PRIx64, off);
10081 0 : has_header = cu->version > 4;
10082 0 : offset_size = cu->offset_size;
10083 : }
10084 4 : printf ("\n");
10085 : }
10086 : else
10087 : {
10088 : /* This must be DWARF5, since GNU DebugFission didn't define
10089 : DW_AT_str_offsets_base. */
10090 0 : has_header = true;
10091 :
10092 : Dwarf_Die cudie;
10093 0 : if (dwarf_cu_die (listptr->cu, &cudie,
10094 : NULL, NULL, NULL, NULL,
10095 : NULL, NULL) == NULL)
10096 0 : printf ("Unknown CU (%s):\n", dwarf_errmsg (-1));
10097 : else
10098 0 : printf ("for CU [%6" PRIx64 "]:\n", dwarf_dieoffset (&cudie));
10099 : }
10100 :
10101 4 : if (has_header)
10102 : {
10103 : uint64_t unit_length;
10104 : uint16_t version;
10105 : uint16_t padding;
10106 :
10107 2 : unit_length = read_4ubyte_unaligned_inc (dbg, readp);
10108 2 : if (unlikely (unit_length == 0xffffffff))
10109 : {
10110 0 : if (unlikely (readp > readendp - 8))
10111 : {
10112 0 : invalid_data:
10113 : error (0, 0, "Invalid data");
10114 : return;
10115 : }
10116 0 : unit_length = read_8ubyte_unaligned_inc (dbg, readp);
10117 0 : offset_size = 8;
10118 : }
10119 : else
10120 : offset_size = 4;
10121 :
10122 2 : printf ("\n");
10123 4 : printf (gettext (" Length: %8" PRIu64 "\n"),
10124 : unit_length);
10125 4 : printf (gettext (" Offset size: %8" PRIu8 "\n"),
10126 : offset_size);
10127 :
10128 : /* We need at least 2-bytes (version) + 2-bytes (padding) =
10129 : 4 bytes to complete the header. And this unit cannot go
10130 : beyond the section data. */
10131 2 : if (readp > readendp - 4
10132 2 : || unit_length < 4
10133 2 : || unit_length > (uint64_t) (readendp - readp))
10134 : goto invalid_data;
10135 :
10136 2 : next_unitp = readp + unit_length;
10137 :
10138 2 : version = read_2ubyte_unaligned_inc (dbg, readp);
10139 4 : printf (gettext (" DWARF version: %8" PRIu16 "\n"), version);
10140 :
10141 2 : if (version != 5)
10142 : {
10143 0 : error (0, 0, gettext ("Unknown version"));
10144 : goto next_unit;
10145 : }
10146 :
10147 2 : padding = read_2ubyte_unaligned_inc (dbg, readp);
10148 4 : printf (gettext (" Padding: %8" PRIx16 "\n"), padding);
10149 :
10150 2 : if (listptr != NULL
10151 0 : && listptr->offset != (Dwarf_Off) (readp - start))
10152 : {
10153 : error (0, 0, "String offsets index doesn't start after header");
10154 : goto next_unit;
10155 : }
10156 :
10157 : printf ("\n");
10158 : }
10159 :
10160 4 : int digits = 1;
10161 4 : size_t offsets = (next_unitp - readp) / offset_size;
10162 12 : while (offsets >= 10)
10163 : {
10164 4 : ++digits;
10165 4 : offsets /= 10;
10166 : }
10167 :
10168 4 : unsigned int index = 0;
10169 4 : size_t index_offset = readp - (const unsigned char *) data->d_buf;
10170 : printf (" Offsets start at 0x%zx:\n", index_offset);
10171 64 : while (readp <= next_unitp - offset_size)
10172 : {
10173 : Dwarf_Word offset;
10174 60 : if (offset_size == 4)
10175 60 : offset = read_4ubyte_unaligned_inc (dbg, readp);
10176 : else
10177 0 : offset = read_8ubyte_unaligned_inc (dbg, readp);
10178 60 : const char *str = dwarf_getstring (dbg, offset, NULL);
10179 120 : printf (" [%*u] [%*" PRIx64 "] \"%s\"\n",
10180 60 : digits, index++, (int) offset_size * 2, offset, str ?: "???");
10181 : }
10182 4 : printf ("\n");
10183 :
10184 4 : if (readp != next_unitp)
10185 0 : error (0, 0, "extra %zd bytes at end of unit",
10186 0 : (size_t) (next_unitp - readp));
10187 :
10188 4 : next_unit:
10189 : readp = next_unitp;
10190 : }
10191 : }
10192 :
10193 :
10194 : /* Print the content of the call frame search table section
10195 : '.eh_frame_hdr'. */
10196 : static void
10197 14 : print_debug_frame_hdr_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
10198 : Ebl *ebl __attribute__ ((unused)),
10199 : GElf_Ehdr *ehdr __attribute__ ((unused)),
10200 : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
10201 : {
10202 28 : printf (gettext ("\
10203 : \nCall frame search table section [%2zu] '.eh_frame_hdr':\n"),
10204 : elf_ndxscn (scn));
10205 :
10206 14 : Elf_Data *data = elf_rawdata (scn, NULL);
10207 :
10208 14 : if (unlikely (data == NULL))
10209 : {
10210 0 : error (0, 0, gettext ("cannot get %s content: %s"),
10211 : ".eh_frame_hdr", elf_errmsg (-1));
10212 0 : return;
10213 : }
10214 :
10215 14 : const unsigned char *readp = data->d_buf;
10216 14 : const unsigned char *const dataend = ((unsigned char *) data->d_buf
10217 14 : + data->d_size);
10218 :
10219 14 : if (unlikely (readp + 4 > dataend))
10220 : {
10221 0 : invalid_data:
10222 0 : error (0, 0, gettext ("invalid data"));
10223 : return;
10224 : }
10225 :
10226 14 : unsigned int version = *readp++;
10227 14 : unsigned int eh_frame_ptr_enc = *readp++;
10228 14 : unsigned int fde_count_enc = *readp++;
10229 14 : unsigned int table_enc = *readp++;
10230 :
10231 14 : printf (" version: %u\n"
10232 : " eh_frame_ptr_enc: %#x ",
10233 : version, eh_frame_ptr_enc);
10234 14 : print_encoding_base ("", eh_frame_ptr_enc);
10235 14 : printf (" fde_count_enc: %#x ", fde_count_enc);
10236 14 : print_encoding_base ("", fde_count_enc);
10237 14 : printf (" table_enc: %#x ", table_enc);
10238 14 : print_encoding_base ("", table_enc);
10239 :
10240 14 : uint64_t eh_frame_ptr = 0;
10241 14 : if (eh_frame_ptr_enc != DW_EH_PE_omit)
10242 : {
10243 14 : readp = read_encoded (eh_frame_ptr_enc, readp, dataend, &eh_frame_ptr,
10244 : dbg);
10245 14 : if (unlikely (readp == NULL))
10246 : goto invalid_data;
10247 :
10248 28 : printf (" eh_frame_ptr: %#" PRIx64, eh_frame_ptr);
10249 14 : if ((eh_frame_ptr_enc & 0x70) == DW_EH_PE_pcrel)
10250 14 : printf (" (offset: %#" PRIx64 ")",
10251 : /* +4 because of the 4 byte header of the section. */
10252 14 : (uint64_t) shdr->sh_offset + 4 + eh_frame_ptr);
10253 :
10254 : putchar_unlocked ('\n');
10255 : }
10256 :
10257 14 : uint64_t fde_count = 0;
10258 14 : if (fde_count_enc != DW_EH_PE_omit)
10259 : {
10260 14 : readp = read_encoded (fde_count_enc, readp, dataend, &fde_count, dbg);
10261 14 : if (unlikely (readp == NULL))
10262 : goto invalid_data;
10263 :
10264 14 : printf (" fde_count: %" PRIu64 "\n", fde_count);
10265 : }
10266 :
10267 14 : if (fde_count == 0 || table_enc == DW_EH_PE_omit)
10268 : return;
10269 :
10270 14 : puts (" Table:");
10271 :
10272 : /* Optimize for the most common case. */
10273 14 : if (table_enc == (DW_EH_PE_datarel | DW_EH_PE_sdata4))
10274 4360 : while (fde_count > 0 && readp + 8 <= dataend)
10275 : {
10276 4346 : int32_t initial_location = read_4sbyte_unaligned_inc (dbg, readp);
10277 8692 : uint64_t initial_offset = ((uint64_t) shdr->sh_offset
10278 4346 : + (int64_t) initial_location);
10279 4346 : int32_t address = read_4sbyte_unaligned_inc (dbg, readp);
10280 : // XXX Possibly print symbol name or section offset for initial_offset
10281 4346 : printf (" %#" PRIx32 " (offset: %#6" PRIx64 ") -> %#" PRIx32
10282 : " fde=[%6" PRIx64 "]\n",
10283 : initial_location, initial_offset,
10284 4346 : address, address - (eh_frame_ptr + 4));
10285 : }
10286 : else
10287 : while (0 && readp < dataend)
10288 : {
10289 :
10290 : }
10291 : }
10292 :
10293 :
10294 : /* Print the content of the exception handling table section
10295 : '.eh_frame_hdr'. */
10296 : static void
10297 0 : print_debug_exception_table (Dwfl_Module *dwflmod __attribute__ ((unused)),
10298 : Ebl *ebl __attribute__ ((unused)),
10299 : GElf_Ehdr *ehdr __attribute__ ((unused)),
10300 : Elf_Scn *scn,
10301 : GElf_Shdr *shdr __attribute__ ((unused)),
10302 : Dwarf *dbg __attribute__ ((unused)))
10303 : {
10304 0 : printf (gettext ("\
10305 : \nException handling table section [%2zu] '.gcc_except_table':\n"),
10306 : elf_ndxscn (scn));
10307 :
10308 0 : Elf_Data *data = elf_rawdata (scn, NULL);
10309 :
10310 0 : if (unlikely (data == NULL))
10311 : {
10312 0 : error (0, 0, gettext ("cannot get %s content: %s"),
10313 : ".gcc_except_table", elf_errmsg (-1));
10314 0 : return;
10315 : }
10316 :
10317 0 : const unsigned char *readp = data->d_buf;
10318 0 : const unsigned char *const dataend = readp + data->d_size;
10319 :
10320 0 : if (unlikely (readp + 1 > dataend))
10321 : {
10322 0 : invalid_data:
10323 0 : error (0, 0, gettext ("invalid data"));
10324 : return;
10325 : }
10326 0 : unsigned int lpstart_encoding = *readp++;
10327 0 : printf (gettext (" LPStart encoding: %#x "), lpstart_encoding);
10328 0 : print_encoding_base ("", lpstart_encoding);
10329 0 : if (lpstart_encoding != DW_EH_PE_omit)
10330 : {
10331 : uint64_t lpstart;
10332 0 : readp = read_encoded (lpstart_encoding, readp, dataend, &lpstart, dbg);
10333 0 : printf (" LPStart: %#" PRIx64 "\n", lpstart);
10334 : }
10335 :
10336 0 : if (unlikely (readp + 1 > dataend))
10337 : goto invalid_data;
10338 0 : unsigned int ttype_encoding = *readp++;
10339 0 : printf (gettext (" TType encoding: %#x "), ttype_encoding);
10340 0 : print_encoding_base ("", ttype_encoding);
10341 0 : const unsigned char *ttype_base = NULL;
10342 0 : if (ttype_encoding != DW_EH_PE_omit)
10343 : {
10344 : unsigned int ttype_base_offset;
10345 0 : get_uleb128 (ttype_base_offset, readp, dataend);
10346 0 : printf (" TType base offset: %#x\n", ttype_base_offset);
10347 0 : if ((size_t) (dataend - readp) > ttype_base_offset)
10348 0 : ttype_base = readp + ttype_base_offset;
10349 : }
10350 :
10351 0 : if (unlikely (readp + 1 > dataend))
10352 : goto invalid_data;
10353 0 : unsigned int call_site_encoding = *readp++;
10354 0 : printf (gettext (" Call site encoding: %#x "), call_site_encoding);
10355 0 : print_encoding_base ("", call_site_encoding);
10356 : unsigned int call_site_table_len;
10357 0 : get_uleb128 (call_site_table_len, readp, dataend);
10358 :
10359 0 : const unsigned char *const action_table = readp + call_site_table_len;
10360 0 : if (unlikely (action_table > dataend))
10361 : goto invalid_data;
10362 : unsigned int u = 0;
10363 : unsigned int max_action = 0;
10364 0 : while (readp < action_table)
10365 : {
10366 0 : if (u == 0)
10367 0 : puts (gettext ("\n Call site table:"));
10368 :
10369 : uint64_t call_site_start;
10370 0 : readp = read_encoded (call_site_encoding, readp, dataend,
10371 : &call_site_start, dbg);
10372 : uint64_t call_site_length;
10373 0 : readp = read_encoded (call_site_encoding, readp, dataend,
10374 : &call_site_length, dbg);
10375 : uint64_t landing_pad;
10376 0 : readp = read_encoded (call_site_encoding, readp, dataend,
10377 : &landing_pad, dbg);
10378 : unsigned int action;
10379 0 : get_uleb128 (action, readp, dataend);
10380 0 : max_action = MAX (action, max_action);
10381 0 : printf (gettext (" [%4u] Call site start: %#" PRIx64 "\n"
10382 : " Call site length: %" PRIu64 "\n"
10383 : " Landing pad: %#" PRIx64 "\n"
10384 : " Action: %u\n"),
10385 : u++, call_site_start, call_site_length, landing_pad, action);
10386 : }
10387 0 : if (readp != action_table)
10388 : goto invalid_data;
10389 :
10390 0 : unsigned int max_ar_filter = 0;
10391 0 : if (max_action > 0)
10392 : {
10393 0 : puts ("\n Action table:");
10394 :
10395 0 : size_t maxdata = (size_t) (dataend - action_table);
10396 0 : if (max_action > maxdata || maxdata - max_action < 1)
10397 : {
10398 0 : invalid_action_table:
10399 0 : fputs (gettext (" <INVALID DATA>\n"), stdout);
10400 0 : return;
10401 : }
10402 :
10403 0 : const unsigned char *const action_table_end
10404 0 : = action_table + max_action + 1;
10405 :
10406 0 : u = 0;
10407 : do
10408 : {
10409 : int ar_filter;
10410 0 : get_sleb128 (ar_filter, readp, action_table_end);
10411 0 : if (ar_filter > 0 && (unsigned int) ar_filter > max_ar_filter)
10412 0 : max_ar_filter = ar_filter;
10413 : int ar_disp;
10414 0 : if (readp >= action_table_end)
10415 : goto invalid_action_table;
10416 0 : get_sleb128 (ar_disp, readp, action_table_end);
10417 :
10418 0 : printf (" [%4u] ar_filter: % d\n"
10419 : " ar_disp: % -5d",
10420 : u, ar_filter, ar_disp);
10421 0 : if (abs (ar_disp) & 1)
10422 0 : printf (" -> [%4u]\n", u + (ar_disp + 1) / 2);
10423 0 : else if (ar_disp != 0)
10424 0 : puts (" -> ???");
10425 : else
10426 : putchar_unlocked ('\n');
10427 0 : ++u;
10428 : }
10429 0 : while (readp < action_table_end);
10430 : }
10431 :
10432 0 : if (max_ar_filter > 0 && ttype_base != NULL)
10433 : {
10434 : unsigned char dsize;
10435 0 : puts ("\n TType table:");
10436 :
10437 : // XXX Not *4, size of encoding;
10438 : switch (ttype_encoding & 7)
10439 : {
10440 : case DW_EH_PE_udata2:
10441 : case DW_EH_PE_sdata2:
10442 : dsize = 2;
10443 : break;
10444 : case DW_EH_PE_udata4:
10445 : case DW_EH_PE_sdata4:
10446 : dsize = 4;
10447 : break;
10448 : case DW_EH_PE_udata8:
10449 : case DW_EH_PE_sdata8:
10450 : dsize = 8;
10451 : break;
10452 0 : default:
10453 0 : dsize = 0;
10454 0 : error (1, 0, gettext ("invalid TType encoding"));
10455 : }
10456 :
10457 0 : if (max_ar_filter
10458 0 : > (size_t) (ttype_base - (const unsigned char *) data->d_buf) / dsize)
10459 : goto invalid_data;
10460 :
10461 0 : readp = ttype_base - max_ar_filter * dsize;
10462 : do
10463 : {
10464 : uint64_t ttype;
10465 0 : readp = read_encoded (ttype_encoding, readp, ttype_base, &ttype,
10466 : dbg);
10467 0 : printf (" [%4u] %#" PRIx64 "\n", max_ar_filter--, ttype);
10468 : }
10469 0 : while (readp < ttype_base);
10470 : }
10471 : }
10472 :
10473 : /* Print the content of the '.gdb_index' section.
10474 : http://sourceware.org/gdb/current/onlinedocs/gdb/Index-Section-Format.html
10475 : */
10476 : static void
10477 2 : print_gdb_index_section (Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr,
10478 : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
10479 : {
10480 6 : printf (gettext ("\nGDB section [%2zu] '%s' at offset %#" PRIx64
10481 : " contains %" PRId64 " bytes :\n"),
10482 : elf_ndxscn (scn), section_name (ebl, ehdr, shdr),
10483 2 : (uint64_t) shdr->sh_offset, (uint64_t) shdr->sh_size);
10484 :
10485 2 : Elf_Data *data = elf_rawdata (scn, NULL);
10486 :
10487 2 : if (unlikely (data == NULL))
10488 : {
10489 0 : error (0, 0, gettext ("cannot get %s content: %s"),
10490 : ".gdb_index", elf_errmsg (-1));
10491 0 : return;
10492 : }
10493 :
10494 : // .gdb_index is always in little endian.
10495 2 : Dwarf dummy_dbg = { .other_byte_order = MY_ELFDATA != ELFDATA2LSB };
10496 2 : dbg = &dummy_dbg;
10497 :
10498 2 : const unsigned char *readp = data->d_buf;
10499 2 : const unsigned char *const dataend = readp + data->d_size;
10500 :
10501 2 : if (unlikely (readp + 4 > dataend))
10502 : {
10503 0 : invalid_data:
10504 0 : error (0, 0, gettext ("invalid data"));
10505 : return;
10506 : }
10507 :
10508 2 : int32_t vers = read_4ubyte_unaligned (dbg, readp);
10509 4 : printf (gettext (" Version: %" PRId32 "\n"), vers);
10510 :
10511 : // The only difference between version 4 and version 5 is the
10512 : // hash used for generating the table. Version 6 contains symbols
10513 : // for inlined functions, older versions didn't. Version 7 adds
10514 : // symbol kinds. Version 8 just indicates that it correctly includes
10515 : // TUs for symbols.
10516 2 : if (vers < 4 || vers > 8)
10517 : {
10518 0 : printf (gettext (" unknown version, cannot parse section\n"));
10519 : return;
10520 : }
10521 :
10522 2 : readp += 4;
10523 2 : if (unlikely (readp + 4 > dataend))
10524 : goto invalid_data;
10525 :
10526 2 : uint32_t cu_off = read_4ubyte_unaligned (dbg, readp);
10527 4 : printf (gettext (" CU offset: %#" PRIx32 "\n"), cu_off);
10528 :
10529 2 : readp += 4;
10530 2 : if (unlikely (readp + 4 > dataend))
10531 : goto invalid_data;
10532 :
10533 2 : uint32_t tu_off = read_4ubyte_unaligned (dbg, readp);
10534 4 : printf (gettext (" TU offset: %#" PRIx32 "\n"), tu_off);
10535 :
10536 2 : readp += 4;
10537 2 : if (unlikely (readp + 4 > dataend))
10538 : goto invalid_data;
10539 :
10540 2 : uint32_t addr_off = read_4ubyte_unaligned (dbg, readp);
10541 4 : printf (gettext (" address offset: %#" PRIx32 "\n"), addr_off);
10542 :
10543 2 : readp += 4;
10544 2 : if (unlikely (readp + 4 > dataend))
10545 : goto invalid_data;
10546 :
10547 2 : uint32_t sym_off = read_4ubyte_unaligned (dbg, readp);
10548 4 : printf (gettext (" symbol offset: %#" PRIx32 "\n"), sym_off);
10549 :
10550 2 : readp += 4;
10551 2 : if (unlikely (readp + 4 > dataend))
10552 : goto invalid_data;
10553 :
10554 2 : uint32_t const_off = read_4ubyte_unaligned (dbg, readp);
10555 4 : printf (gettext (" constant offset: %#" PRIx32 "\n"), const_off);
10556 :
10557 2 : if (unlikely ((size_t) (dataend - (const unsigned char *) data->d_buf)
10558 : < const_off))
10559 : goto invalid_data;
10560 :
10561 2 : readp = data->d_buf + cu_off;
10562 :
10563 2 : const unsigned char *nextp = data->d_buf + tu_off;
10564 2 : if (tu_off >= data->d_size)
10565 : goto invalid_data;
10566 :
10567 2 : size_t cu_nr = (nextp - readp) / 16;
10568 :
10569 4 : printf (gettext ("\n CU list at offset %#" PRIx32
10570 : " contains %zu entries:\n"),
10571 : cu_off, cu_nr);
10572 :
10573 2 : size_t n = 0;
10574 8 : while (dataend - readp >= 16 && n < cu_nr)
10575 : {
10576 4 : uint64_t off = read_8ubyte_unaligned (dbg, readp);
10577 4 : readp += 8;
10578 :
10579 4 : uint64_t len = read_8ubyte_unaligned (dbg, readp);
10580 4 : readp += 8;
10581 :
10582 4 : printf (" [%4zu] start: %0#8" PRIx64
10583 : ", length: %5" PRIu64 "\n", n, off, len);
10584 4 : n++;
10585 : }
10586 :
10587 2 : readp = data->d_buf + tu_off;
10588 2 : nextp = data->d_buf + addr_off;
10589 2 : if (addr_off >= data->d_size)
10590 : goto invalid_data;
10591 :
10592 2 : size_t tu_nr = (nextp - readp) / 24;
10593 :
10594 4 : printf (gettext ("\n TU list at offset %#" PRIx32
10595 : " contains %zu entries:\n"),
10596 : tu_off, tu_nr);
10597 :
10598 2 : n = 0;
10599 6 : while (dataend - readp >= 24 && n < tu_nr)
10600 : {
10601 2 : uint64_t off = read_8ubyte_unaligned (dbg, readp);
10602 2 : readp += 8;
10603 :
10604 2 : uint64_t type = read_8ubyte_unaligned (dbg, readp);
10605 2 : readp += 8;
10606 :
10607 2 : uint64_t sig = read_8ubyte_unaligned (dbg, readp);
10608 2 : readp += 8;
10609 :
10610 2 : printf (" [%4zu] CU offset: %5" PRId64
10611 : ", type offset: %5" PRId64
10612 : ", signature: %0#8" PRIx64 "\n", n, off, type, sig);
10613 2 : n++;
10614 : }
10615 :
10616 2 : readp = data->d_buf + addr_off;
10617 2 : nextp = data->d_buf + sym_off;
10618 2 : if (sym_off >= data->d_size)
10619 : goto invalid_data;
10620 :
10621 2 : size_t addr_nr = (nextp - readp) / 20;
10622 :
10623 4 : printf (gettext ("\n Address list at offset %#" PRIx32
10624 : " contains %zu entries:\n"),
10625 : addr_off, addr_nr);
10626 :
10627 2 : n = 0;
10628 8 : while (dataend - readp >= 20 && n < addr_nr)
10629 : {
10630 4 : uint64_t low = read_8ubyte_unaligned (dbg, readp);
10631 4 : readp += 8;
10632 :
10633 4 : uint64_t high = read_8ubyte_unaligned (dbg, readp);
10634 4 : readp += 8;
10635 :
10636 4 : uint32_t idx = read_4ubyte_unaligned (dbg, readp);
10637 4 : readp += 4;
10638 :
10639 4 : printf (" [%4zu] ", n);
10640 4 : print_dwarf_addr (dwflmod, 8, low, low);
10641 4 : printf ("..");
10642 4 : print_dwarf_addr (dwflmod, 8, high - 1, high);
10643 4 : printf (", CU index: %5" PRId32 "\n", idx);
10644 4 : n++;
10645 : }
10646 :
10647 2 : const unsigned char *const_start = data->d_buf + const_off;
10648 2 : if (const_off >= data->d_size)
10649 : goto invalid_data;
10650 :
10651 2 : readp = data->d_buf + sym_off;
10652 2 : nextp = const_start;
10653 2 : size_t sym_nr = (nextp - readp) / 8;
10654 :
10655 4 : printf (gettext ("\n Symbol table at offset %#" PRIx32
10656 : " contains %zu slots:\n"),
10657 : addr_off, sym_nr);
10658 :
10659 2 : n = 0;
10660 2052 : while (dataend - readp >= 8 && n < sym_nr)
10661 : {
10662 2048 : uint32_t name = read_4ubyte_unaligned (dbg, readp);
10663 2048 : readp += 4;
10664 :
10665 2048 : uint32_t vector = read_4ubyte_unaligned (dbg, readp);
10666 2048 : readp += 4;
10667 :
10668 2048 : if (name != 0 || vector != 0)
10669 : {
10670 14 : const unsigned char *sym = const_start + name;
10671 28 : if (unlikely ((size_t) (dataend - const_start) < name
10672 : || memchr (sym, '\0', dataend - sym) == NULL))
10673 : goto invalid_data;
10674 :
10675 14 : printf (" [%4zu] symbol: %s, CUs: ", n, sym);
10676 :
10677 14 : const unsigned char *readcus = const_start + vector;
10678 14 : if (unlikely ((size_t) (dataend - const_start) < vector))
10679 : goto invalid_data;
10680 14 : uint32_t cus = read_4ubyte_unaligned (dbg, readcus);
10681 44 : while (cus--)
10682 : {
10683 : uint32_t cu_kind, cu, kind;
10684 : bool is_static;
10685 16 : readcus += 4;
10686 16 : if (unlikely (readcus + 4 > dataend))
10687 : goto invalid_data;
10688 16 : cu_kind = read_4ubyte_unaligned (dbg, readcus);
10689 16 : cu = cu_kind & ((1 << 24) - 1);
10690 16 : kind = (cu_kind >> 28) & 7;
10691 16 : is_static = cu_kind & (1U << 31);
10692 16 : if (cu > cu_nr - 1)
10693 2 : printf ("%" PRId32 "T", cu - (uint32_t) cu_nr);
10694 : else
10695 : printf ("%" PRId32, cu);
10696 16 : if (kind != 0)
10697 : {
10698 8 : printf (" (");
10699 8 : switch (kind)
10700 : {
10701 3 : case 1:
10702 : printf ("type");
10703 : break;
10704 2 : case 2:
10705 : printf ("var");
10706 : break;
10707 3 : case 3:
10708 : printf ("func");
10709 : break;
10710 0 : case 4:
10711 : printf ("other");
10712 : break;
10713 0 : default:
10714 : printf ("unknown-0x%" PRIx32, kind);
10715 : break;
10716 : }
10717 8 : printf (":%c)", (is_static ? 'S' : 'G'));
10718 : }
10719 16 : if (cus > 0)
10720 : printf (", ");
10721 : }
10722 : printf ("\n");
10723 : }
10724 2048 : n++;
10725 : }
10726 : }
10727 :
10728 : /* Returns true and sets split DWARF CU id if there is a split compile
10729 : unit in the given Dwarf, and no non-split units are found (before it). */
10730 : static bool
10731 102 : is_split_dwarf (Dwarf *dbg, uint64_t *id, Dwarf_CU **split_cu)
10732 : {
10733 102 : Dwarf_CU *cu = NULL;
10734 204 : while (dwarf_get_units (dbg, cu, &cu, NULL, NULL, NULL, NULL) == 0)
10735 : {
10736 : uint8_t unit_type;
10737 102 : if (dwarf_cu_info (cu, NULL, &unit_type, NULL, NULL,
10738 : id, NULL, NULL) != 0)
10739 102 : return false;
10740 :
10741 102 : if (unit_type != DW_UT_split_compile && unit_type != DW_UT_split_type)
10742 : return false;
10743 :
10744 : /* We really only care about the split compile unit, the types
10745 : should be fine and self sufficient. Also they don't have an
10746 : id that we can match with a skeleton unit. */
10747 9 : if (unit_type == DW_UT_split_compile)
10748 : {
10749 9 : *split_cu = cu;
10750 9 : return true;
10751 : }
10752 : }
10753 :
10754 : return false;
10755 : }
10756 :
10757 : /* Check that there is one and only one Dwfl_Module, return in arg. */
10758 : static int
10759 9 : getone_dwflmod (Dwfl_Module *dwflmod,
10760 : void **userdata __attribute__ ((unused)),
10761 : const char *name __attribute__ ((unused)),
10762 : Dwarf_Addr base __attribute__ ((unused)),
10763 : void *arg)
10764 : {
10765 9 : Dwfl_Module **m = (Dwfl_Module **) arg;
10766 9 : if (*m != NULL)
10767 : return DWARF_CB_ABORT;
10768 9 : *m = dwflmod;
10769 9 : return DWARF_CB_OK;
10770 : }
10771 :
10772 : static void
10773 124 : print_debug (Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr)
10774 : {
10775 : /* Used for skeleton file, if necessary for split DWARF. */
10776 124 : Dwfl *skel_dwfl = NULL;
10777 124 : Dwfl_Module *skel_mod = NULL;
10778 124 : char *skel_name = NULL;
10779 124 : Dwarf *split_dbg = NULL;
10780 124 : Dwarf_CU *split_cu = NULL;
10781 :
10782 : /* Before we start the real work get a debug context descriptor. */
10783 : Dwarf_Addr dwbias;
10784 124 : Dwarf *dbg = dwfl_module_getdwarf (dwflmod, &dwbias);
10785 372 : Dwarf dummy_dbg =
10786 : {
10787 124 : .elf = ebl->elf,
10788 124 : .other_byte_order = MY_ELFDATA != ehdr->e_ident[EI_DATA]
10789 : };
10790 124 : if (dbg == NULL)
10791 : {
10792 22 : if ((print_debug_sections & ~section_exception) != 0)
10793 0 : error (0, 0, gettext ("cannot get debug context descriptor: %s"),
10794 : dwfl_errmsg (-1));
10795 : dbg = &dummy_dbg;
10796 : }
10797 : else
10798 : {
10799 : /* If we are asked about a split dwarf (.dwo) file, use the user
10800 : provided, or find the corresponding skeleton file. If we got
10801 : a skeleton file, replace the given dwflmod and dbg, with one
10802 : derived from the skeleton file to provide enough context. */
10803 : uint64_t split_id;
10804 102 : if (is_split_dwarf (dbg, &split_id, &split_cu))
10805 : {
10806 9 : if (dwarf_skeleton != NULL)
10807 9 : skel_name = strdup (dwarf_skeleton);
10808 : else
10809 : {
10810 : /* Replace file.dwo with file.o and see if that matches. */
10811 : const char *fname;
10812 0 : dwfl_module_info (dwflmod, NULL, NULL, NULL, NULL, NULL,
10813 : &fname, NULL);
10814 0 : if (fname != NULL)
10815 : {
10816 0 : size_t flen = strlen (fname);
10817 0 : if (flen > 4 && strcmp (".dwo", fname + flen - 4) == 0)
10818 : {
10819 0 : skel_name = strdup (fname);
10820 0 : if (skel_name != NULL)
10821 : {
10822 0 : skel_name[flen - 3] = 'o';
10823 0 : skel_name[flen - 2] = '\0';
10824 : }
10825 : }
10826 : }
10827 : }
10828 :
10829 9 : if (skel_name != NULL)
10830 : {
10831 9 : int skel_fd = open (skel_name, O_RDONLY);
10832 9 : if (skel_fd == -1)
10833 0 : fprintf (stderr, "Warning: Couldn't open DWARF skeleton file"
10834 : " '%s'\n", skel_name);
10835 : else
10836 9 : skel_dwfl = create_dwfl (skel_fd, skel_name);
10837 :
10838 9 : if (skel_dwfl != NULL)
10839 : {
10840 9 : if (dwfl_getmodules (skel_dwfl, &getone_dwflmod,
10841 : &skel_mod, 0) != 0)
10842 : {
10843 0 : fprintf (stderr, "Warning: Bad DWARF skeleton,"
10844 : " multiple modules '%s'\n", skel_name);
10845 0 : dwfl_end (skel_dwfl);
10846 0 : skel_mod = NULL;
10847 : }
10848 : }
10849 0 : else if (skel_fd != -1)
10850 0 : fprintf (stderr, "Warning: Couldn't create skeleton dwfl for"
10851 : " '%s': %s\n", skel_name, dwfl_errmsg (-1));
10852 :
10853 9 : if (skel_mod != NULL)
10854 : {
10855 9 : Dwarf *skel_dbg = dwfl_module_getdwarf (skel_mod, &dwbias);
10856 9 : if (skel_dbg != NULL)
10857 : {
10858 : /* First check the skeleton CU DIE, only fetch
10859 : the split DIE if we know the id matches to
10860 : not unnecessary search for any split DIEs we
10861 : don't need. */
10862 9 : Dwarf_CU *cu = NULL;
10863 22 : while (dwarf_get_units (skel_dbg, cu, &cu,
10864 : NULL, NULL, NULL, NULL) == 0)
10865 : {
10866 : uint8_t unit_type;
10867 : uint64_t skel_id;
10868 13 : if (dwarf_cu_info (cu, NULL, &unit_type, NULL, NULL,
10869 : &skel_id, NULL, NULL) == 0
10870 13 : && unit_type == DW_UT_skeleton
10871 13 : && split_id == skel_id)
10872 : {
10873 : Dwarf_Die subdie;
10874 9 : if (dwarf_cu_info (cu, NULL, NULL, NULL,
10875 : &subdie,
10876 : NULL, NULL, NULL) == 0
10877 9 : && dwarf_tag (&subdie) != DW_TAG_invalid)
10878 : {
10879 9 : split_dbg = dwarf_cu_getdwarf (subdie.cu);
10880 9 : if (split_dbg == NULL)
10881 0 : fprintf (stderr,
10882 : "Warning: Couldn't get split_dbg:"
10883 : " %s\n", dwarf_errmsg (-1));
10884 9 : break;
10885 : }
10886 : else
10887 : {
10888 : /* Everything matches up, but not
10889 : according to libdw. Which means
10890 : the user knew better. So...
10891 : Terrible hack... We can never
10892 : destroy the underlying dwfl
10893 : because it would free the wrong
10894 : Dwarfs... So we leak memory...*/
10895 0 : if (cu->split == NULL
10896 0 : && dwarf_skeleton != NULL)
10897 : {
10898 0 : do_not_close_dwfl = true;
10899 0 : __libdw_link_skel_split (cu, split_cu);
10900 0 : split_dbg = dwarf_cu_getdwarf (split_cu);
10901 0 : break;
10902 : }
10903 : else
10904 0 : fprintf (stderr, "Warning: Couldn't get"
10905 : " skeleton subdie: %s\n",
10906 : dwarf_errmsg (-1));
10907 : }
10908 : }
10909 : }
10910 9 : if (split_dbg == NULL)
10911 0 : fprintf (stderr, "Warning: '%s' didn't contain a skeleton for split id %" PRIx64 "\n", skel_name, split_id);
10912 : }
10913 : else
10914 0 : fprintf (stderr, "Warning: Couldn't get skeleton DWARF:"
10915 : " %s\n", dwfl_errmsg (-1));
10916 : }
10917 : }
10918 :
10919 9 : if (split_dbg != NULL)
10920 : {
10921 9 : dbg = split_dbg;
10922 9 : dwflmod = skel_mod;
10923 : }
10924 0 : else if (skel_name == NULL)
10925 0 : fprintf (stderr,
10926 : "Warning: split DWARF file, but no skeleton found.\n");
10927 : }
10928 93 : else if (dwarf_skeleton != NULL)
10929 0 : fprintf (stderr, "Warning: DWARF skeleton given,"
10930 : " but not a split DWARF file\n");
10931 : }
10932 :
10933 : /* Get the section header string table index. */
10934 : size_t shstrndx;
10935 124 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
10936 : error (EXIT_FAILURE, 0,
10937 0 : gettext ("cannot get section header string table index"));
10938 :
10939 : /* If the .debug_info section is listed as implicitly required then
10940 : we must make sure to handle it before handling any other debug
10941 : section. Various other sections depend on the CU DIEs being
10942 : scanned (silently) first. */
10943 124 : bool implicit_info = (implicit_debug_sections & section_info) != 0;
10944 124 : bool explicit_info = (print_debug_sections & section_info) != 0;
10945 124 : if (implicit_info)
10946 : {
10947 : Elf_Scn *scn = NULL;
10948 1458 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
10949 : {
10950 : GElf_Shdr shdr_mem;
10951 1458 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
10952 :
10953 1458 : if (shdr != NULL && shdr->sh_type == SHT_PROGBITS)
10954 : {
10955 604 : const char *name = elf_strptr (ebl->elf, shstrndx,
10956 604 : shdr->sh_name);
10957 604 : if (name == NULL)
10958 0 : continue;
10959 :
10960 604 : if (strcmp (name, ".debug_info") == 0
10961 548 : || strcmp (name, ".debug_info.dwo") == 0
10962 539 : || strcmp (name, ".zdebug_info") == 0
10963 536 : || strcmp (name, ".zdebug_info.dwo") == 0)
10964 : {
10965 68 : print_debug_info_section (dwflmod, ebl, ehdr,
10966 : scn, shdr, dbg);
10967 68 : break;
10968 : }
10969 : }
10970 : }
10971 68 : print_debug_sections &= ~section_info;
10972 68 : implicit_debug_sections &= ~section_info;
10973 : }
10974 :
10975 : /* Look through all the sections for the debugging sections to print. */
10976 : Elf_Scn *scn = NULL;
10977 3966 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
10978 : {
10979 : GElf_Shdr shdr_mem;
10980 3842 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
10981 :
10982 3842 : if (shdr != NULL && shdr->sh_type == SHT_PROGBITS)
10983 : {
10984 : static const struct
10985 : {
10986 : const char *name;
10987 : enum section_e bitmask;
10988 : void (*fp) (Dwfl_Module *, Ebl *,
10989 : GElf_Ehdr *, Elf_Scn *, GElf_Shdr *, Dwarf *);
10990 : } debug_sections[] =
10991 : {
10992 : #define NEW_SECTION(name) \
10993 : { ".debug_" #name, section_##name, print_debug_##name##_section }
10994 : NEW_SECTION (abbrev),
10995 : NEW_SECTION (addr),
10996 : NEW_SECTION (aranges),
10997 : NEW_SECTION (frame),
10998 : NEW_SECTION (info),
10999 : NEW_SECTION (types),
11000 : NEW_SECTION (line),
11001 : NEW_SECTION (loc),
11002 : /* loclists is loc for DWARF5. */
11003 : { ".debug_loclists", section_loc,
11004 : print_debug_loclists_section },
11005 : NEW_SECTION (pubnames),
11006 : NEW_SECTION (str),
11007 : /* A DWARF5 specialised debug string section. */
11008 : { ".debug_line_str", section_str,
11009 : print_debug_str_section },
11010 : /* DWARF5 string offsets table. */
11011 : { ".debug_str_offsets", section_str,
11012 : print_debug_str_offsets_section },
11013 : NEW_SECTION (macinfo),
11014 : NEW_SECTION (macro),
11015 : NEW_SECTION (ranges),
11016 : /* rnglists is ranges for DWARF5. */
11017 : { ".debug_rnglists", section_ranges,
11018 : print_debug_rnglists_section },
11019 : { ".eh_frame", section_frame | section_exception,
11020 : print_debug_frame_section },
11021 : { ".eh_frame_hdr", section_frame | section_exception,
11022 : print_debug_frame_hdr_section },
11023 : { ".gcc_except_table", section_frame | section_exception,
11024 : print_debug_exception_table },
11025 : { ".gdb_index", section_gdb_index, print_gdb_index_section }
11026 : };
11027 1885 : const int ndebug_sections = (sizeof (debug_sections)
11028 : / sizeof (debug_sections[0]));
11029 1885 : const char *name = elf_strptr (ebl->elf, shstrndx,
11030 1885 : shdr->sh_name);
11031 1885 : if (name == NULL)
11032 0 : continue;
11033 :
11034 : int n;
11035 28375 : for (n = 0; n < ndebug_sections; ++n)
11036 : {
11037 29231 : size_t dbglen = strlen (debug_sections[n].name);
11038 29231 : size_t scnlen = strlen (name);
11039 29231 : if ((strncmp (name, debug_sections[n].name, dbglen) == 0
11040 878 : && (dbglen == scnlen
11041 138 : || (scnlen == dbglen + 4
11042 121 : && strstr (name, ".dwo") == name + dbglen)))
11043 28437 : || (name[0] == '.' && name[1] == 'z'
11044 454 : && debug_sections[n].name[1] == 'd'
11045 454 : && strncmp (&name[2], &debug_sections[n].name[1],
11046 : dbglen - 1) == 0
11047 62 : && (scnlen == dbglen + 1
11048 0 : || (scnlen == dbglen + 5
11049 0 : && strstr (name, ".dwo") == name + dbglen + 1))))
11050 : {
11051 1712 : if ((print_debug_sections | implicit_debug_sections)
11052 856 : & debug_sections[n].bitmask)
11053 310 : debug_sections[n].fp (dwflmod, ebl, ehdr, scn, shdr, dbg);
11054 : break;
11055 : }
11056 : }
11057 : }
11058 : }
11059 :
11060 124 : dwfl_end (skel_dwfl);
11061 124 : free (skel_name);
11062 :
11063 : /* Turn implicit and/or explicit back on in case we go over another file. */
11064 124 : if (implicit_info)
11065 68 : implicit_debug_sections |= section_info;
11066 124 : if (explicit_info)
11067 49 : print_debug_sections |= section_info;
11068 :
11069 124 : reset_listptr (&known_locsptr);
11070 124 : reset_listptr (&known_loclistsptr);
11071 124 : reset_listptr (&known_rangelistptr);
11072 124 : reset_listptr (&known_rnglistptr);
11073 124 : reset_listptr (&known_addrbases);
11074 124 : reset_listptr (&known_stroffbases);
11075 124 : }
11076 :
11077 :
11078 : #define ITEM_INDENT 4
11079 : #define WRAP_COLUMN 75
11080 :
11081 : /* Print "NAME: FORMAT", wrapping when output text would make the line
11082 : exceed WRAP_COLUMN. Unpadded numbers look better for the core items
11083 : but this function is also used for registers which should be printed
11084 : aligned. Fortunately registers output uses fixed fields width (such
11085 : as %11d) for the alignment.
11086 :
11087 : Line breaks should not depend on the particular values although that
11088 : may happen in some cases of the core items. */
11089 :
11090 : static unsigned int
11091 : __attribute__ ((format (printf, 6, 7)))
11092 761 : print_core_item (unsigned int colno, char sep, unsigned int wrap,
11093 : size_t name_width, const char *name, const char *format, ...)
11094 : {
11095 761 : size_t len = strlen (name);
11096 761 : if (name_width < len)
11097 368 : name_width = len;
11098 :
11099 : char *out;
11100 : va_list ap;
11101 761 : va_start (ap, format);
11102 761 : int out_len = vasprintf (&out, format, ap);
11103 761 : va_end (ap);
11104 761 : if (out_len == -1)
11105 0 : error (EXIT_FAILURE, 0, _("memory exhausted"));
11106 :
11107 761 : size_t n = name_width + sizeof ": " - 1 + out_len;
11108 :
11109 761 : if (colno == 0)
11110 : {
11111 48 : printf ("%*s", ITEM_INDENT, "");
11112 48 : colno = ITEM_INDENT + n;
11113 : }
11114 713 : else if (colno + 2 + n < wrap)
11115 : {
11116 866 : printf ("%c ", sep);
11117 433 : colno += 2 + n;
11118 : }
11119 : else
11120 : {
11121 280 : printf ("\n%*s", ITEM_INDENT, "");
11122 280 : colno = ITEM_INDENT + n;
11123 : }
11124 :
11125 1522 : printf ("%s: %*s%s", name, (int) (name_width - len), "", out);
11126 :
11127 761 : free (out);
11128 :
11129 761 : return colno;
11130 : }
11131 :
11132 : static const void *
11133 838 : convert (Elf *core, Elf_Type type, uint_fast16_t count,
11134 : void *value, const void *data, size_t size)
11135 : {
11136 1676 : Elf_Data valuedata =
11137 : {
11138 : .d_type = type,
11139 : .d_buf = value,
11140 838 : .d_size = size ?: gelf_fsize (core, type, count, EV_CURRENT),
11141 : .d_version = EV_CURRENT,
11142 : };
11143 838 : Elf_Data indata =
11144 : {
11145 : .d_type = type,
11146 : .d_buf = (void *) data,
11147 : .d_size = valuedata.d_size,
11148 : .d_version = EV_CURRENT,
11149 : };
11150 :
11151 1676 : Elf_Data *d = (gelf_getclass (core) == ELFCLASS32
11152 1676 : ? elf32_xlatetom : elf64_xlatetom)
11153 838 : (&valuedata, &indata, elf_getident (core, NULL)[EI_DATA]);
11154 838 : if (d == NULL)
11155 0 : error (EXIT_FAILURE, 0,
11156 0 : gettext ("cannot convert core note data: %s"), elf_errmsg (-1));
11157 :
11158 838 : return data + indata.d_size;
11159 : }
11160 :
11161 : typedef uint8_t GElf_Byte;
11162 :
11163 : static unsigned int
11164 371 : handle_core_item (Elf *core, const Ebl_Core_Item *item, const void *desc,
11165 : unsigned int colno, size_t *repeated_size)
11166 : {
11167 371 : uint_fast16_t count = item->count ?: 1;
11168 : /* Ebl_Core_Item count is always a small number.
11169 : Make sure the backend didn't put in some large bogus value. */
11170 56 : assert (count < 128);
11171 :
11172 : #define TYPES \
11173 : DO_TYPE (BYTE, Byte, "0x%.2" PRIx8, "%" PRId8); \
11174 : DO_TYPE (HALF, Half, "0x%.4" PRIx16, "%" PRId16); \
11175 : DO_TYPE (WORD, Word, "0x%.8" PRIx32, "%" PRId32); \
11176 : DO_TYPE (SWORD, Sword, "%" PRId32, "%" PRId32); \
11177 : DO_TYPE (XWORD, Xword, "0x%.16" PRIx64, "%" PRId64); \
11178 : DO_TYPE (SXWORD, Sxword, "%" PRId64, "%" PRId64)
11179 :
11180 : #define DO_TYPE(NAME, Name, hex, dec) GElf_##Name Name
11181 : typedef union { TYPES; } value_t;
11182 371 : void *data = alloca (count * sizeof (value_t));
11183 : #undef DO_TYPE
11184 :
11185 : #define DO_TYPE(NAME, Name, hex, dec) \
11186 : GElf_##Name *value_##Name __attribute__((unused)) = data
11187 371 : TYPES;
11188 : #undef DO_TYPE
11189 :
11190 371 : size_t size = gelf_fsize (core, item->type, count, EV_CURRENT);
11191 371 : size_t convsize = size;
11192 371 : if (repeated_size != NULL)
11193 : {
11194 1 : if (*repeated_size > size && (item->format == 'b' || item->format == 'B'))
11195 : {
11196 0 : data = alloca (*repeated_size);
11197 0 : count *= *repeated_size / size;
11198 0 : convsize = count * size;
11199 0 : *repeated_size -= convsize;
11200 : }
11201 1 : else if (item->count != 0 || item->format != '\n')
11202 0 : *repeated_size -= size;
11203 : }
11204 :
11205 371 : convert (core, item->type, count, data, desc + item->offset, convsize);
11206 :
11207 371 : Elf_Type type = item->type;
11208 371 : if (type == ELF_T_ADDR)
11209 0 : type = gelf_getclass (core) == ELFCLASS32 ? ELF_T_WORD : ELF_T_XWORD;
11210 :
11211 371 : switch (item->format)
11212 : {
11213 175 : case 'd':
11214 175 : assert (count == 1);
11215 175 : switch (type)
11216 : {
11217 : #define DO_TYPE(NAME, Name, hex, dec) \
11218 : case ELF_T_##NAME: \
11219 : colno = print_core_item (colno, ',', WRAP_COLUMN, \
11220 : 0, item->name, dec, value_##Name[0]); \
11221 : break
11222 24 : TYPES;
11223 : #undef DO_TYPE
11224 0 : default:
11225 0 : abort ();
11226 : }
11227 : break;
11228 :
11229 109 : case 'x':
11230 109 : assert (count == 1);
11231 109 : switch (type)
11232 : {
11233 : #define DO_TYPE(NAME, Name, hex, dec) \
11234 : case ELF_T_##NAME: \
11235 : colno = print_core_item (colno, ',', WRAP_COLUMN, \
11236 : 0, item->name, hex, value_##Name[0]); \
11237 : break
11238 0 : TYPES;
11239 : #undef DO_TYPE
11240 0 : default:
11241 0 : abort ();
11242 : }
11243 : break;
11244 :
11245 20 : case 'b':
11246 : case 'B':
11247 20 : assert (size % sizeof (unsigned int) == 0);
11248 20 : unsigned int nbits = count * size * 8;
11249 20 : unsigned int pop = 0;
11250 50 : for (const unsigned int *i = data; (void *) i < data + count * size; ++i)
11251 30 : pop += __builtin_popcount (*i);
11252 20 : bool negate = pop > nbits / 2;
11253 20 : const unsigned int bias = item->format == 'b';
11254 :
11255 40 : {
11256 20 : char printed[(negate ? nbits - pop : pop) * 16 + 1];
11257 20 : char *p = printed;
11258 20 : *p = '\0';
11259 :
11260 : if (BYTE_ORDER != LITTLE_ENDIAN && size > sizeof (unsigned int))
11261 : {
11262 : assert (size == sizeof (unsigned int) * 2);
11263 : for (unsigned int *i = data;
11264 : (void *) i < data + count * size; i += 2)
11265 : {
11266 : unsigned int w = i[1];
11267 : i[1] = i[0];
11268 : i[0] = w;
11269 : }
11270 : }
11271 :
11272 20 : unsigned int lastbit = 0;
11273 20 : unsigned int run = 0;
11274 70 : for (const unsigned int *i = data;
11275 30 : (void *) i < data + count * size; ++i)
11276 : {
11277 30 : unsigned int bit = ((void *) i - data) * 8;
11278 30 : unsigned int w = negate ? ~*i : *i;
11279 30 : while (w != 0)
11280 : {
11281 : /* Note that a right shift equal to (or greater than)
11282 : the number of bits of w is undefined behaviour. In
11283 : particular when the least significant bit is bit 32
11284 : (w = 0x8000000) then w >>= n is undefined. So
11285 : explicitly handle that case separately. */
11286 0 : unsigned int n = ffs (w);
11287 0 : if (n < sizeof (w) * 8)
11288 0 : w >>= n;
11289 : else
11290 : w = 0;
11291 0 : bit += n;
11292 :
11293 0 : if (lastbit != 0 && lastbit + 1 == bit)
11294 0 : ++run;
11295 : else
11296 : {
11297 0 : if (lastbit == 0)
11298 0 : p += sprintf (p, "%u", bit - bias);
11299 0 : else if (run == 0)
11300 0 : p += sprintf (p, ",%u", bit - bias);
11301 : else
11302 0 : p += sprintf (p, "-%u,%u", lastbit - bias, bit - bias);
11303 : run = 0;
11304 : }
11305 :
11306 : lastbit = bit;
11307 : }
11308 : }
11309 20 : if (lastbit > 0 && run > 0 && lastbit + 1 != nbits)
11310 0 : p += sprintf (p, "-%u", lastbit - bias);
11311 :
11312 20 : colno = print_core_item (colno, ',', WRAP_COLUMN, 0, item->name,
11313 : negate ? "~<%s>" : "<%s>", printed);
11314 : }
11315 20 : break;
11316 :
11317 40 : case 'T':
11318 : case (char) ('T'|0x80):
11319 40 : assert (count == 2);
11320 : Dwarf_Word sec;
11321 : Dwarf_Word usec;
11322 40 : switch (type)
11323 : {
11324 : #define DO_TYPE(NAME, Name, hex, dec) \
11325 : case ELF_T_##NAME: \
11326 : sec = value_##Name[0]; \
11327 : usec = value_##Name[1]; \
11328 : break
11329 0 : TYPES;
11330 : #undef DO_TYPE
11331 0 : default:
11332 0 : abort ();
11333 : }
11334 40 : if (unlikely (item->format == (char) ('T'|0x80)))
11335 : {
11336 : /* This is a hack for an ill-considered 64-bit ABI where
11337 : tv_usec is actually a 32-bit field with 32 bits of padding
11338 : rounding out struct timeval. We've already converted it as
11339 : a 64-bit field. For little-endian, this just means the
11340 : high half is the padding; it's presumably zero, but should
11341 : be ignored anyway. For big-endian, it means the 32-bit
11342 : field went into the high half of USEC. */
11343 : GElf_Ehdr ehdr_mem;
11344 0 : GElf_Ehdr *ehdr = gelf_getehdr (core, &ehdr_mem);
11345 0 : if (likely (ehdr->e_ident[EI_DATA] == ELFDATA2MSB))
11346 0 : usec >>= 32;
11347 : else
11348 0 : usec &= UINT32_MAX;
11349 : }
11350 40 : colno = print_core_item (colno, ',', WRAP_COLUMN, 0, item->name,
11351 : "%" PRIu64 ".%.6" PRIu64, sec, usec);
11352 40 : break;
11353 :
11354 8 : case 'c':
11355 8 : assert (count == 1);
11356 8 : colno = print_core_item (colno, ',', WRAP_COLUMN, 0, item->name,
11357 8 : "%c", value_Byte[0]);
11358 8 : break;
11359 :
11360 16 : case 's':
11361 16 : colno = print_core_item (colno, ',', WRAP_COLUMN, 0, item->name,
11362 : "%.*s", (int) count, value_Byte);
11363 16 : break;
11364 :
11365 1 : case '\n':
11366 : /* This is a list of strings separated by '\n'. */
11367 1 : assert (item->count == 0);
11368 1 : assert (repeated_size != NULL);
11369 1 : assert (item->name == NULL);
11370 1 : if (unlikely (item->offset >= *repeated_size))
11371 : break;
11372 :
11373 1 : const char *s = desc + item->offset;
11374 1 : size = *repeated_size - item->offset;
11375 1 : *repeated_size = 0;
11376 49 : while (size > 0)
11377 : {
11378 48 : const char *eol = memchr (s, '\n', size);
11379 48 : int len = size;
11380 48 : if (eol != NULL)
11381 47 : len = eol - s;
11382 48 : printf ("%*s%.*s\n", ITEM_INDENT, "", len, s);
11383 48 : if (eol == NULL)
11384 : break;
11385 47 : size -= eol + 1 - s;
11386 47 : s = eol + 1;
11387 : }
11388 :
11389 : colno = WRAP_COLUMN;
11390 : break;
11391 :
11392 : case 'h':
11393 : break;
11394 :
11395 0 : default:
11396 0 : error (0, 0, "XXX not handling format '%c' for %s",
11397 : item->format, item->name);
11398 : break;
11399 : }
11400 :
11401 : #undef TYPES
11402 :
11403 371 : return colno;
11404 : }
11405 :
11406 :
11407 : /* Sort items by group, and by layout offset within each group. */
11408 : static int
11409 827 : compare_core_items (const void *a, const void *b)
11410 : {
11411 827 : const Ebl_Core_Item *const *p1 = a;
11412 827 : const Ebl_Core_Item *const *p2 = b;
11413 827 : const Ebl_Core_Item *item1 = *p1;
11414 827 : const Ebl_Core_Item *item2 = *p2;
11415 :
11416 827 : return ((item1->group == item2->group ? 0
11417 827 : : strcmp (item1->group, item2->group))
11418 391 : ?: (int) item1->offset - (int) item2->offset);
11419 : }
11420 :
11421 : /* Sort item groups by layout offset of the first item in the group. */
11422 : static int
11423 126 : compare_core_item_groups (const void *a, const void *b)
11424 : {
11425 126 : const Ebl_Core_Item *const *const *p1 = a;
11426 126 : const Ebl_Core_Item *const *const *p2 = b;
11427 126 : const Ebl_Core_Item *const *group1 = *p1;
11428 126 : const Ebl_Core_Item *const *group2 = *p2;
11429 126 : const Ebl_Core_Item *item1 = *group1;
11430 126 : const Ebl_Core_Item *item2 = *group2;
11431 :
11432 126 : return (int) item1->offset - (int) item2->offset;
11433 : }
11434 :
11435 : static unsigned int
11436 35 : handle_core_items (Elf *core, const void *desc, size_t descsz,
11437 : const Ebl_Core_Item *items, size_t nitems)
11438 : {
11439 35 : if (nitems == 0)
11440 : return 0;
11441 32 : unsigned int colno = 0;
11442 :
11443 : /* FORMAT '\n' makes sense to be present only as a single item as it
11444 : processes all the data of a note. FORMATs 'b' and 'B' have a special case
11445 : if present as a single item but they can be also processed with other
11446 : items below. */
11447 32 : if (nitems == 1 && (items[0].format == '\n' || items[0].format == 'b'
11448 8 : || items[0].format == 'B'))
11449 : {
11450 1 : assert (items[0].offset == 0);
11451 1 : size_t size = descsz;
11452 1 : colno = handle_core_item (core, items, desc, colno, &size);
11453 : /* If SIZE is not zero here there is some remaining data. But we do not
11454 : know how to process it anyway. */
11455 : return colno;
11456 : }
11457 362 : for (size_t i = 0; i < nitems; ++i)
11458 362 : assert (items[i].format != '\n');
11459 :
11460 : /* Sort to collect the groups together. */
11461 31 : const Ebl_Core_Item *sorted_items[nitems];
11462 393 : for (size_t i = 0; i < nitems; ++i)
11463 362 : sorted_items[i] = &items[i];
11464 31 : qsort (sorted_items, nitems, sizeof sorted_items[0], &compare_core_items);
11465 :
11466 : /* Collect the unique groups and sort them. */
11467 31 : const Ebl_Core_Item **groups[nitems];
11468 31 : groups[0] = &sorted_items[0];
11469 31 : size_t ngroups = 1;
11470 362 : for (size_t i = 1; i < nitems; ++i)
11471 331 : if (sorted_items[i]->group != sorted_items[i - 1]->group
11472 68 : && strcmp (sorted_items[i]->group, sorted_items[i - 1]->group))
11473 68 : groups[ngroups++] = &sorted_items[i];
11474 31 : qsort (groups, ngroups, sizeof groups[0], &compare_core_item_groups);
11475 :
11476 : /* Write out all the groups. */
11477 31 : const void *last = desc;
11478 : do
11479 : {
11480 134 : for (size_t i = 0; i < ngroups; ++i)
11481 : {
11482 572 : for (const Ebl_Core_Item **item = groups[i];
11483 471 : (item < &sorted_items[nitems]
11484 438 : && ((*item)->group == groups[i][0]->group
11485 68 : || !strcmp ((*item)->group, groups[i][0]->group)));
11486 370 : ++item)
11487 370 : colno = handle_core_item (core, *item, desc, colno, NULL);
11488 :
11489 : /* Force a line break at the end of the group. */
11490 101 : colno = WRAP_COLUMN;
11491 : }
11492 :
11493 33 : if (descsz == 0)
11494 : break;
11495 :
11496 : /* This set of items consumed a certain amount of the note's data.
11497 : If there is more data there, we have another unit of the same size.
11498 : Loop to print that out too. */
11499 19 : const Ebl_Core_Item *item = &items[nitems - 1];
11500 38 : size_t eltsz = item->offset + gelf_fsize (core, item->type,
11501 19 : item->count ?: 1, EV_CURRENT);
11502 :
11503 19 : int reps = -1;
11504 : do
11505 : {
11506 19 : ++reps;
11507 19 : desc += eltsz;
11508 19 : descsz -= eltsz;
11509 : }
11510 19 : while (descsz >= eltsz && !memcmp (desc, last, eltsz));
11511 :
11512 19 : if (reps == 1)
11513 : {
11514 : /* For just one repeat, print it unabridged twice. */
11515 : desc -= eltsz;
11516 : descsz += eltsz;
11517 : }
11518 19 : else if (reps > 1)
11519 0 : printf (gettext ("\n%*s... <repeats %u more times> ..."),
11520 : ITEM_INDENT, "", reps);
11521 :
11522 19 : last = desc;
11523 : }
11524 19 : while (descsz > 0);
11525 :
11526 : return colno;
11527 : }
11528 :
11529 : static unsigned int
11530 : handle_bit_registers (const Ebl_Register_Location *regloc, const void *desc,
11531 : unsigned int colno)
11532 : {
11533 0 : desc += regloc->offset;
11534 :
11535 0 : abort (); /* XXX */
11536 : return colno;
11537 : }
11538 :
11539 :
11540 : static unsigned int
11541 161 : handle_core_register (Ebl *ebl, Elf *core, int maxregname,
11542 : const Ebl_Register_Location *regloc, const void *desc,
11543 : unsigned int colno)
11544 : {
11545 161 : if (regloc->bits % 8 != 0)
11546 0 : return handle_bit_registers (regloc, desc, colno);
11547 :
11548 161 : desc += regloc->offset;
11549 :
11550 554 : for (int reg = regloc->regno; reg < regloc->regno + regloc->count; ++reg)
11551 : {
11552 : char name[REGNAMESZ];
11553 : int bits;
11554 : int type;
11555 393 : register_info (ebl, reg, regloc, name, &bits, &type);
11556 :
11557 : #define TYPES \
11558 : BITS (8, BYTE, "%4" PRId8, "0x%.2" PRIx8); \
11559 : BITS (16, HALF, "%6" PRId16, "0x%.4" PRIx16); \
11560 : BITS (32, WORD, "%11" PRId32, " 0x%.8" PRIx32); \
11561 : BITS (64, XWORD, "%20" PRId64, " 0x%.16" PRIx64)
11562 :
11563 : #define BITS(bits, xtype, sfmt, ufmt) \
11564 : uint##bits##_t b##bits; int##bits##_t b##bits##s
11565 : union { TYPES; uint64_t b128[2]; } value;
11566 : #undef BITS
11567 :
11568 393 : switch (type)
11569 : {
11570 305 : case DW_ATE_unsigned:
11571 : case DW_ATE_signed:
11572 : case DW_ATE_address:
11573 305 : switch (bits)
11574 : {
11575 : #define BITS(bits, xtype, sfmt, ufmt) \
11576 : case bits: \
11577 : desc = convert (core, ELF_T_##xtype, 1, &value, desc, 0); \
11578 : if (type == DW_ATE_signed) \
11579 : colno = print_core_item (colno, ' ', WRAP_COLUMN, \
11580 : maxregname, name, \
11581 : sfmt, value.b##bits##s); \
11582 : else \
11583 : colno = print_core_item (colno, ' ', WRAP_COLUMN, \
11584 : maxregname, name, \
11585 : ufmt, value.b##bits); \
11586 : break
11587 :
11588 0 : TYPES;
11589 :
11590 48 : case 128:
11591 48 : assert (type == DW_ATE_unsigned);
11592 48 : desc = convert (core, ELF_T_XWORD, 2, &value, desc, 0);
11593 48 : int be = elf_getident (core, NULL)[EI_DATA] == ELFDATA2MSB;
11594 96 : colno = print_core_item (colno, ' ', WRAP_COLUMN,
11595 : maxregname, name,
11596 : "0x%.16" PRIx64 "%.16" PRIx64,
11597 48 : value.b128[!be], value.b128[be]);
11598 48 : break;
11599 :
11600 0 : default:
11601 0 : abort ();
11602 : #undef BITS
11603 : }
11604 : break;
11605 :
11606 88 : default:
11607 : /* Print each byte in hex, the whole thing in native byte order. */
11608 88 : assert (bits % 8 == 0);
11609 88 : const uint8_t *bytes = desc;
11610 88 : desc += bits / 8;
11611 88 : char hex[bits / 4 + 1];
11612 88 : hex[bits / 4] = '\0';
11613 88 : int incr = 1;
11614 88 : if (elf_getident (core, NULL)[EI_DATA] == ELFDATA2LSB)
11615 : {
11616 48 : bytes += bits / 8 - 1;
11617 48 : incr = -1;
11618 : }
11619 88 : size_t idx = 0;
11620 872 : for (char *h = hex; bits > 0; bits -= 8, idx += incr)
11621 : {
11622 784 : *h++ = "0123456789abcdef"[bytes[idx] >> 4];
11623 784 : *h++ = "0123456789abcdef"[bytes[idx] & 0xf];
11624 : }
11625 88 : colno = print_core_item (colno, ' ', WRAP_COLUMN,
11626 : maxregname, name, "0x%s", hex);
11627 : break;
11628 : }
11629 393 : desc += regloc->pad;
11630 :
11631 : #undef TYPES
11632 : }
11633 :
11634 : return colno;
11635 : }
11636 :
11637 :
11638 : struct register_info
11639 : {
11640 : const Ebl_Register_Location *regloc;
11641 : const char *set;
11642 : char name[REGNAMESZ];
11643 : int regno;
11644 : int bits;
11645 : int type;
11646 : };
11647 :
11648 : static int
11649 : register_bitpos (const struct register_info *r)
11650 : {
11651 1916 : return (r->regloc->offset * 8
11652 3832 : + ((r->regno - r->regloc->regno)
11653 1916 : * (r->regloc->bits + r->regloc->pad * 8)));
11654 : }
11655 :
11656 : static int
11657 987 : compare_sets_by_info (const struct register_info *r1,
11658 : const struct register_info *r2)
11659 : {
11660 987 : return ((int) r2->bits - (int) r1->bits
11661 2903 : ?: register_bitpos (r1) - register_bitpos (r2));
11662 : }
11663 :
11664 : /* Sort registers by set, and by size and layout offset within each set. */
11665 : static int
11666 4489 : compare_registers (const void *a, const void *b)
11667 : {
11668 4489 : const struct register_info *r1 = a;
11669 4489 : const struct register_info *r2 = b;
11670 :
11671 : /* Unused elements sort last. */
11672 4489 : if (r1->regloc == NULL)
11673 3278 : return r2->regloc == NULL ? 0 : 1;
11674 1211 : if (r2->regloc == NULL)
11675 : return -1;
11676 :
11677 1046 : return ((r1->set == r2->set ? 0 : strcmp (r1->set, r2->set))
11678 79 : ?: compare_sets_by_info (r1, r2));
11679 : }
11680 :
11681 : /* Sort register sets by layout offset of the first register in the set. */
11682 : static int
11683 20 : compare_register_sets (const void *a, const void *b)
11684 : {
11685 20 : const struct register_info *const *p1 = a;
11686 20 : const struct register_info *const *p2 = b;
11687 20 : return compare_sets_by_info (*p1, *p2);
11688 : }
11689 :
11690 : static unsigned int
11691 35 : handle_core_registers (Ebl *ebl, Elf *core, const void *desc,
11692 : const Ebl_Register_Location *reglocs, size_t nregloc)
11693 : {
11694 35 : if (nregloc == 0)
11695 : return 0;
11696 :
11697 17 : ssize_t maxnreg = ebl_register_info (ebl, 0, NULL, 0, NULL, NULL, NULL, NULL);
11698 17 : if (maxnreg <= 0)
11699 : {
11700 0 : for (size_t i = 0; i < nregloc; ++i)
11701 0 : if (maxnreg < reglocs[i].regno + reglocs[i].count)
11702 0 : maxnreg = reglocs[i].regno + reglocs[i].count;
11703 0 : assert (maxnreg > 0);
11704 : }
11705 :
11706 17 : struct register_info regs[maxnreg];
11707 34 : memset (regs, 0, sizeof regs);
11708 :
11709 : /* Sort to collect the sets together. */
11710 17 : int maxreg = 0;
11711 178 : for (size_t i = 0; i < nregloc; ++i)
11712 715 : for (int reg = reglocs[i].regno;
11713 554 : reg < reglocs[i].regno + reglocs[i].count;
11714 393 : ++reg)
11715 : {
11716 393 : assert (reg < maxnreg);
11717 393 : if (reg > maxreg)
11718 199 : maxreg = reg;
11719 393 : struct register_info *info = ®s[reg];
11720 393 : info->regloc = ®locs[i];
11721 393 : info->regno = reg;
11722 786 : info->set = register_info (ebl, reg, ®locs[i],
11723 393 : info->name, &info->bits, &info->type);
11724 : }
11725 17 : qsort (regs, maxreg + 1, sizeof regs[0], &compare_registers);
11726 :
11727 : /* Collect the unique sets and sort them. */
11728 802 : inline bool same_set (const struct register_info *a,
11729 : const struct register_info *b)
11730 : {
11731 1604 : return (a < ®s[maxnreg] && a->regloc != NULL
11732 802 : && b < ®s[maxnreg] && b->regloc != NULL
11733 785 : && a->bits == b->bits
11734 1569 : && (a->set == b->set || !strcmp (a->set, b->set)));
11735 : }
11736 17 : struct register_info *sets[maxreg + 1];
11737 17 : sets[0] = ®s[0];
11738 17 : size_t nsets = 1;
11739 1247 : for (int i = 1; i <= maxreg; ++i)
11740 1230 : if (regs[i].regloc != NULL && !same_set (®s[i], ®s[i - 1]))
11741 16 : sets[nsets++] = ®s[i];
11742 17 : qsort (sets, nsets, sizeof sets[0], &compare_register_sets);
11743 :
11744 : /* Write out all the sets. */
11745 17 : unsigned int colno = 0;
11746 50 : for (size_t i = 0; i < nsets; ++i)
11747 : {
11748 : /* Find the longest name of a register in this set. */
11749 33 : size_t maxname = 0;
11750 : const struct register_info *end;
11751 426 : for (end = sets[i]; same_set (sets[i], end); ++end)
11752 : {
11753 393 : size_t len = strlen (end->name);
11754 393 : if (len > maxname)
11755 50 : maxname = len;
11756 : }
11757 :
11758 194 : for (const struct register_info *reg = sets[i];
11759 : reg < end;
11760 161 : reg += reg->regloc->count ?: 1)
11761 161 : colno = handle_core_register (ebl, core, maxname,
11762 : reg->regloc, desc, colno);
11763 :
11764 : /* Force a line break at the end of the group. */
11765 33 : colno = WRAP_COLUMN;
11766 : }
11767 :
11768 : return colno;
11769 : }
11770 :
11771 : static void
11772 8 : handle_auxv_note (Ebl *ebl, Elf *core, GElf_Word descsz, GElf_Off desc_pos)
11773 : {
11774 8 : Elf_Data *data = elf_getdata_rawchunk (core, desc_pos, descsz, ELF_T_AUXV);
11775 8 : if (data == NULL)
11776 0 : elf_error:
11777 0 : error (EXIT_FAILURE, 0,
11778 0 : gettext ("cannot convert core note data: %s"), elf_errmsg (-1));
11779 :
11780 8 : const size_t nauxv = descsz / gelf_fsize (core, ELF_T_AUXV, 1, EV_CURRENT);
11781 158 : for (size_t i = 0; i < nauxv; ++i)
11782 : {
11783 : GElf_auxv_t av_mem;
11784 150 : GElf_auxv_t *av = gelf_getauxv (data, i, &av_mem);
11785 150 : if (av == NULL)
11786 : goto elf_error;
11787 :
11788 : const char *name;
11789 : const char *fmt;
11790 150 : if (ebl_auxv_info (ebl, av->a_type, &name, &fmt) == 0)
11791 : {
11792 : /* Unknown type. */
11793 0 : if (av->a_un.a_val == 0)
11794 0 : printf (" %" PRIu64 "\n", av->a_type);
11795 : else
11796 0 : printf (" %" PRIu64 ": %#" PRIx64 "\n",
11797 : av->a_type, av->a_un.a_val);
11798 : }
11799 : else
11800 150 : switch (fmt[0])
11801 : {
11802 8 : case '\0': /* Normally zero. */
11803 8 : if (av->a_un.a_val == 0)
11804 : {
11805 8 : printf (" %s\n", name);
11806 : break;
11807 : }
11808 : FALLTHROUGH;
11809 : case 'x': /* hex */
11810 : case 'p': /* address */
11811 : case 's': /* address of string */
11812 66 : printf (" %s: %#" PRIx64 "\n", name, av->a_un.a_val);
11813 : break;
11814 72 : case 'u':
11815 72 : printf (" %s: %" PRIu64 "\n", name, av->a_un.a_val);
11816 : break;
11817 0 : case 'd':
11818 0 : printf (" %s: %" PRId64 "\n", name, av->a_un.a_val);
11819 : break;
11820 :
11821 4 : case 'b':
11822 8 : printf (" %s: %#" PRIx64 " ", name, av->a_un.a_val);
11823 4 : GElf_Xword bit = 1;
11824 4 : const char *pfx = "<";
11825 110 : for (const char *p = fmt + 1; *p != 0; p = strchr (p, '\0') + 1)
11826 : {
11827 106 : if (av->a_un.a_val & bit)
11828 : {
11829 77 : printf ("%s%s", pfx, p);
11830 77 : pfx = " ";
11831 : }
11832 106 : bit <<= 1;
11833 : }
11834 : printf (">\n");
11835 : break;
11836 :
11837 0 : default:
11838 0 : abort ();
11839 : }
11840 : }
11841 8 : }
11842 :
11843 : static bool
11844 : buf_has_data (unsigned char const *ptr, unsigned char const *end, size_t sz)
11845 : {
11846 162 : return ptr < end && (size_t) (end - ptr) >= sz;
11847 : }
11848 :
11849 : static bool
11850 15 : buf_read_int (Elf *core, unsigned char const **ptrp, unsigned char const *end,
11851 : int *retp)
11852 : {
11853 30 : if (! buf_has_data (*ptrp, end, 4))
11854 : return false;
11855 :
11856 15 : *ptrp = convert (core, ELF_T_WORD, 1, retp, *ptrp, 4);
11857 15 : return true;
11858 : }
11859 :
11860 : static bool
11861 147 : buf_read_ulong (Elf *core, unsigned char const **ptrp, unsigned char const *end,
11862 : uint64_t *retp)
11863 : {
11864 147 : size_t sz = gelf_fsize (core, ELF_T_ADDR, 1, EV_CURRENT);
11865 294 : if (! buf_has_data (*ptrp, end, sz))
11866 : return false;
11867 :
11868 : union
11869 : {
11870 : uint64_t u64;
11871 : uint32_t u32;
11872 : } u;
11873 :
11874 147 : *ptrp = convert (core, ELF_T_ADDR, 1, &u, *ptrp, sz);
11875 :
11876 147 : if (sz == 4)
11877 93 : *retp = u.u32;
11878 : else
11879 54 : *retp = u.u64;
11880 : return true;
11881 : }
11882 :
11883 : static void
11884 5 : handle_siginfo_note (Elf *core, GElf_Word descsz, GElf_Off desc_pos)
11885 : {
11886 5 : Elf_Data *data = elf_getdata_rawchunk (core, desc_pos, descsz, ELF_T_BYTE);
11887 5 : if (data == NULL)
11888 0 : error (EXIT_FAILURE, 0,
11889 0 : gettext ("cannot convert core note data: %s"), elf_errmsg (-1));
11890 :
11891 5 : unsigned char const *ptr = data->d_buf;
11892 5 : unsigned char const *const end = data->d_buf + data->d_size;
11893 :
11894 : /* Siginfo head is three ints: signal number, error number, origin
11895 : code. */
11896 : int si_signo, si_errno, si_code;
11897 5 : if (! buf_read_int (core, &ptr, end, &si_signo)
11898 5 : || ! buf_read_int (core, &ptr, end, &si_errno)
11899 5 : || ! buf_read_int (core, &ptr, end, &si_code))
11900 : {
11901 0 : fail:
11902 0 : printf (" Not enough data in NT_SIGINFO note.\n");
11903 0 : return;
11904 : }
11905 :
11906 : /* Next is a pointer-aligned union of structures. On 64-bit
11907 : machines, that implies a word of padding. */
11908 5 : if (gelf_getclass (core) == ELFCLASS64)
11909 2 : ptr += 4;
11910 :
11911 10 : printf (" si_signo: %d, si_errno: %d, si_code: %d\n",
11912 : si_signo, si_errno, si_code);
11913 :
11914 5 : if (si_code > 0)
11915 5 : switch (si_signo)
11916 : {
11917 5 : case CORE_SIGILL:
11918 : case CORE_SIGFPE:
11919 : case CORE_SIGSEGV:
11920 : case CORE_SIGBUS:
11921 : {
11922 : uint64_t addr;
11923 5 : if (! buf_read_ulong (core, &ptr, end, &addr))
11924 : goto fail;
11925 10 : printf (" fault address: %#" PRIx64 "\n", addr);
11926 5 : break;
11927 : }
11928 : default:
11929 : ;
11930 : }
11931 0 : else if (si_code == CORE_SI_USER)
11932 : {
11933 : int pid, uid;
11934 0 : if (! buf_read_int (core, &ptr, end, &pid)
11935 0 : || ! buf_read_int (core, &ptr, end, &uid))
11936 : goto fail;
11937 0 : printf (" sender PID: %d, sender UID: %d\n", pid, uid);
11938 : }
11939 : }
11940 :
11941 : static void
11942 5 : handle_file_note (Elf *core, GElf_Word descsz, GElf_Off desc_pos)
11943 : {
11944 5 : Elf_Data *data = elf_getdata_rawchunk (core, desc_pos, descsz, ELF_T_BYTE);
11945 5 : if (data == NULL)
11946 0 : error (EXIT_FAILURE, 0,
11947 0 : gettext ("cannot convert core note data: %s"), elf_errmsg (-1));
11948 :
11949 5 : unsigned char const *ptr = data->d_buf;
11950 5 : unsigned char const *const end = data->d_buf + data->d_size;
11951 :
11952 : uint64_t count, page_size;
11953 5 : if (! buf_read_ulong (core, &ptr, end, &count)
11954 5 : || ! buf_read_ulong (core, &ptr, end, &page_size))
11955 : {
11956 0 : fail:
11957 0 : printf (" Not enough data in NT_FILE note.\n");
11958 0 : return;
11959 : }
11960 :
11961 5 : size_t addrsize = gelf_fsize (core, ELF_T_ADDR, 1, EV_CURRENT);
11962 5 : uint64_t maxcount = (size_t) (end - ptr) / (3 * addrsize);
11963 5 : if (count > maxcount)
11964 : goto fail;
11965 :
11966 : /* Where file names are stored. */
11967 5 : unsigned char const *const fstart = ptr + 3 * count * addrsize;
11968 5 : char const *fptr = (char *) fstart;
11969 :
11970 10 : printf (" %" PRId64 " files:\n", count);
11971 49 : for (uint64_t i = 0; i < count; ++i)
11972 : {
11973 : uint64_t mstart, mend, moffset;
11974 44 : if (! buf_read_ulong (core, &ptr, fstart, &mstart)
11975 44 : || ! buf_read_ulong (core, &ptr, fstart, &mend)
11976 44 : || ! buf_read_ulong (core, &ptr, fstart, &moffset))
11977 : goto fail;
11978 :
11979 44 : const char *fnext = memchr (fptr, '\0', (char *) end - fptr);
11980 44 : if (fnext == NULL)
11981 : goto fail;
11982 :
11983 88 : int ct = printf (" %08" PRIx64 "-%08" PRIx64
11984 : " %08" PRIx64 " %" PRId64,
11985 : mstart, mend, moffset * page_size, mend - mstart);
11986 88 : printf ("%*s%s\n", ct > 50 ? 3 : 53 - ct, "", fptr);
11987 :
11988 44 : fptr = fnext + 1;
11989 : }
11990 : }
11991 :
11992 : static void
11993 36 : handle_core_note (Ebl *ebl, const GElf_Nhdr *nhdr,
11994 : const char *name, const void *desc)
11995 : {
11996 : GElf_Word regs_offset;
11997 : size_t nregloc;
11998 : const Ebl_Register_Location *reglocs;
11999 : size_t nitems;
12000 : const Ebl_Core_Item *items;
12001 :
12002 36 : if (! ebl_core_note (ebl, nhdr, name,
12003 : ®s_offset, &nregloc, ®locs, &nitems, &items))
12004 1 : return;
12005 :
12006 : /* Pass 0 for DESCSZ when there are registers in the note,
12007 : so that the ITEMS array does not describe the whole thing.
12008 : For non-register notes, the actual descsz might be a multiple
12009 : of the unit size, not just exactly the unit size. */
12010 88 : unsigned int colno = handle_core_items (ebl->elf, desc,
12011 53 : nregloc == 0 ? nhdr->n_descsz : 0,
12012 : items, nitems);
12013 35 : if (colno != 0)
12014 : putchar_unlocked ('\n');
12015 :
12016 35 : colno = handle_core_registers (ebl, ebl->elf, desc + regs_offset,
12017 : reglocs, nregloc);
12018 35 : if (colno != 0)
12019 : putchar_unlocked ('\n');
12020 : }
12021 :
12022 : static void
12023 40 : handle_notes_data (Ebl *ebl, const GElf_Ehdr *ehdr,
12024 : GElf_Off start, Elf_Data *data)
12025 : {
12026 40 : fputs_unlocked (gettext (" Owner Data size Type\n"), stdout);
12027 :
12028 40 : if (data == NULL)
12029 : goto bad_note;
12030 :
12031 : size_t offset = 0;
12032 : GElf_Nhdr nhdr;
12033 : size_t name_offset;
12034 : size_t desc_offset;
12035 126 : while (offset < data->d_size
12036 86 : && (offset = gelf_getnote (data, offset,
12037 : &nhdr, &name_offset, &desc_offset)) > 0)
12038 : {
12039 86 : const char *name = nhdr.n_namesz == 0 ? "" : data->d_buf + name_offset;
12040 86 : const char *desc = data->d_buf + desc_offset;
12041 :
12042 : char buf[100];
12043 : char buf2[100];
12044 344 : printf (gettext (" %-13.*s %9" PRId32 " %s\n"),
12045 86 : (int) nhdr.n_namesz, name, nhdr.n_descsz,
12046 86 : ehdr->e_type == ET_CORE
12047 54 : ? ebl_core_note_type_name (ebl, nhdr.n_type,
12048 : buf, sizeof (buf))
12049 32 : : ebl_object_note_type_name (ebl, name, nhdr.n_type,
12050 : buf2, sizeof (buf2)));
12051 :
12052 : /* Filter out invalid entries. */
12053 : if (memchr (name, '\0', nhdr.n_namesz) != NULL
12054 : /* XXX For now help broken Linux kernels. */
12055 : || 1)
12056 : {
12057 86 : if (ehdr->e_type == ET_CORE)
12058 : {
12059 54 : if (nhdr.n_type == NT_AUXV
12060 8 : && (nhdr.n_namesz == 4 /* Broken old Linux kernels. */
12061 8 : || (nhdr.n_namesz == 5 && name[4] == '\0'))
12062 8 : && !memcmp (name, "CORE", 4))
12063 8 : handle_auxv_note (ebl, ebl->elf, nhdr.n_descsz,
12064 : start + desc_offset);
12065 46 : else if (nhdr.n_namesz == 5 && strcmp (name, "CORE") == 0)
12066 34 : switch (nhdr.n_type)
12067 : {
12068 5 : case NT_SIGINFO:
12069 5 : handle_siginfo_note (ebl->elf, nhdr.n_descsz,
12070 : start + desc_offset);
12071 : break;
12072 :
12073 5 : case NT_FILE:
12074 5 : handle_file_note (ebl->elf, nhdr.n_descsz,
12075 : start + desc_offset);
12076 : break;
12077 :
12078 24 : default:
12079 24 : handle_core_note (ebl, &nhdr, name, desc);
12080 : }
12081 : else
12082 12 : handle_core_note (ebl, &nhdr, name, desc);
12083 : }
12084 : else
12085 32 : ebl_object_note (ebl, name, nhdr.n_type, nhdr.n_descsz, desc);
12086 : }
12087 : }
12088 :
12089 40 : if (offset == data->d_size)
12090 40 : return;
12091 :
12092 0 : bad_note:
12093 0 : error (0, 0,
12094 0 : gettext ("cannot get content of note: %s"),
12095 : data != NULL ? "garbage data" : elf_errmsg (-1));
12096 : }
12097 :
12098 : static void
12099 42 : handle_notes (Ebl *ebl, GElf_Ehdr *ehdr)
12100 : {
12101 : /* If we have section headers, just look for SHT_NOTE sections.
12102 : In a debuginfo file, the program headers are not reliable. */
12103 42 : if (shnum != 0)
12104 : {
12105 : /* Get the section header string table index. */
12106 : size_t shstrndx;
12107 33 : if (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0)
12108 : error (EXIT_FAILURE, 0,
12109 0 : gettext ("cannot get section header string table index"));
12110 :
12111 : Elf_Scn *scn = NULL;
12112 940 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
12113 : {
12114 : GElf_Shdr shdr_mem;
12115 907 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
12116 :
12117 907 : if (shdr == NULL || shdr->sh_type != SHT_NOTE)
12118 : /* Not what we are looking for. */
12119 876 : continue;
12120 :
12121 93 : printf (gettext ("\
12122 : \nNote section [%2zu] '%s' of %" PRIu64 " bytes at offset %#0" PRIx64 ":\n"),
12123 : elf_ndxscn (scn),
12124 31 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
12125 : shdr->sh_size, shdr->sh_offset);
12126 :
12127 31 : handle_notes_data (ebl, ehdr, shdr->sh_offset,
12128 : elf_getdata (scn, NULL));
12129 : }
12130 : return;
12131 : }
12132 :
12133 : /* We have to look through the program header to find the note
12134 : sections. There can be more than one. */
12135 102 : for (size_t cnt = 0; cnt < phnum; ++cnt)
12136 : {
12137 : GElf_Phdr mem;
12138 102 : GElf_Phdr *phdr = gelf_getphdr (ebl->elf, cnt, &mem);
12139 :
12140 102 : if (phdr == NULL || phdr->p_type != PT_NOTE)
12141 : /* Not what we are looking for. */
12142 93 : continue;
12143 :
12144 18 : printf (gettext ("\
12145 : \nNote segment of %" PRIu64 " bytes at offset %#0" PRIx64 ":\n"),
12146 : phdr->p_filesz, phdr->p_offset);
12147 :
12148 18 : handle_notes_data (ebl, ehdr, phdr->p_offset,
12149 : elf_getdata_rawchunk (ebl->elf,
12150 9 : phdr->p_offset, phdr->p_filesz,
12151 : ELF_T_NHDR));
12152 : }
12153 : }
12154 :
12155 :
12156 : static void
12157 5 : hex_dump (const uint8_t *data, size_t len)
12158 : {
12159 5 : size_t pos = 0;
12160 31 : while (pos < len)
12161 : {
12162 21 : printf (" 0x%08zx ", pos);
12163 :
12164 21 : const size_t chunk = MIN (len - pos, 16);
12165 :
12166 342 : for (size_t i = 0; i < chunk; ++i)
12167 321 : if (i % 4 == 3)
12168 80 : printf ("%02x ", data[pos + i]);
12169 : else
12170 241 : printf ("%02x", data[pos + i]);
12171 :
12172 21 : if (chunk < 16)
12173 1 : printf ("%*s", (int) ((16 - chunk) * 2 + (16 - chunk + 3) / 4), "");
12174 :
12175 321 : for (size_t i = 0; i < chunk; ++i)
12176 : {
12177 321 : unsigned char b = data[pos + i];
12178 642 : printf ("%c", isprint (b) ? b : '.');
12179 : }
12180 :
12181 21 : putchar ('\n');
12182 21 : pos += chunk;
12183 : }
12184 5 : }
12185 :
12186 : static void
12187 5 : dump_data_section (Elf_Scn *scn, const GElf_Shdr *shdr, const char *name)
12188 : {
12189 5 : if (shdr->sh_size == 0 || shdr->sh_type == SHT_NOBITS)
12190 0 : printf (gettext ("\nSection [%zu] '%s' has no data to dump.\n"),
12191 : elf_ndxscn (scn), name);
12192 : else
12193 : {
12194 5 : if (print_decompress)
12195 : {
12196 : /* We try to decompress the section, but keep the old shdr around
12197 : so we can show both the original shdr size and the uncompressed
12198 : data size. */
12199 4 : if ((shdr->sh_flags & SHF_COMPRESSED) != 0)
12200 : {
12201 2 : if (elf_compress (scn, 0, 0) < 0)
12202 0 : printf ("WARNING: %s [%zd]\n",
12203 : gettext ("Couldn't uncompress section"),
12204 : elf_ndxscn (scn));
12205 : }
12206 2 : else if (strncmp (name, ".zdebug", strlen (".zdebug")) == 0)
12207 : {
12208 2 : if (elf_compress_gnu (scn, 0, 0) < 0)
12209 0 : printf ("WARNING: %s [%zd]\n",
12210 : gettext ("Couldn't uncompress section"),
12211 : elf_ndxscn (scn));
12212 : }
12213 : }
12214 :
12215 5 : Elf_Data *data = elf_rawdata (scn, NULL);
12216 5 : if (data == NULL)
12217 0 : error (0, 0, gettext ("cannot get data for section [%zu] '%s': %s"),
12218 : elf_ndxscn (scn), name, elf_errmsg (-1));
12219 : else
12220 : {
12221 5 : if (data->d_size == shdr->sh_size)
12222 1 : printf (gettext ("\nHex dump of section [%zu] '%s', %" PRIu64
12223 : " bytes at offset %#0" PRIx64 ":\n"),
12224 : elf_ndxscn (scn), name,
12225 : shdr->sh_size, shdr->sh_offset);
12226 : else
12227 4 : printf (gettext ("\nHex dump of section [%zu] '%s', %" PRIu64
12228 : " bytes (%zd uncompressed) at offset %#0"
12229 : PRIx64 ":\n"),
12230 : elf_ndxscn (scn), name,
12231 : shdr->sh_size, data->d_size, shdr->sh_offset);
12232 5 : hex_dump (data->d_buf, data->d_size);
12233 : }
12234 : }
12235 5 : }
12236 :
12237 : static void
12238 73 : print_string_section (Elf_Scn *scn, const GElf_Shdr *shdr, const char *name)
12239 : {
12240 73 : if (shdr->sh_size == 0 || shdr->sh_type == SHT_NOBITS)
12241 0 : printf (gettext ("\nSection [%zu] '%s' has no strings to dump.\n"),
12242 : elf_ndxscn (scn), name);
12243 : else
12244 : {
12245 73 : if (print_decompress)
12246 : {
12247 : /* We try to decompress the section, but keep the old shdr around
12248 : so we can show both the original shdr size and the uncompressed
12249 : data size. */
12250 1 : if ((shdr->sh_flags & SHF_COMPRESSED) != 0)
12251 : {
12252 0 : if (elf_compress (scn, 0, 0) < 0)
12253 0 : printf ("WARNING: %s [%zd]\n",
12254 : gettext ("Couldn't uncompress section"),
12255 : elf_ndxscn (scn));
12256 : }
12257 1 : else if (strncmp (name, ".zdebug", strlen (".zdebug")) == 0)
12258 : {
12259 1 : if (elf_compress_gnu (scn, 0, 0) < 0)
12260 0 : printf ("WARNING: %s [%zd]\n",
12261 : gettext ("Couldn't uncompress section"),
12262 : elf_ndxscn (scn));
12263 : }
12264 : }
12265 :
12266 73 : Elf_Data *data = elf_rawdata (scn, NULL);
12267 73 : if (data == NULL)
12268 0 : error (0, 0, gettext ("cannot get data for section [%zu] '%s': %s"),
12269 : elf_ndxscn (scn), name, elf_errmsg (-1));
12270 : else
12271 : {
12272 73 : if (data->d_size == shdr->sh_size)
12273 72 : printf (gettext ("\nString section [%zu] '%s' contains %" PRIu64
12274 : " bytes at offset %#0" PRIx64 ":\n"),
12275 : elf_ndxscn (scn), name,
12276 : shdr->sh_size, shdr->sh_offset);
12277 : else
12278 1 : printf (gettext ("\nString section [%zu] '%s' contains %" PRIu64
12279 : " bytes (%zd uncompressed) at offset %#0"
12280 : PRIx64 ":\n"),
12281 : elf_ndxscn (scn), name,
12282 : shdr->sh_size, data->d_size, shdr->sh_offset);
12283 :
12284 73 : const char *start = data->d_buf;
12285 73 : const char *const limit = start + data->d_size;
12286 : do
12287 : {
12288 14494 : const char *end = memchr (start, '\0', limit - start);
12289 14494 : const size_t pos = start - (const char *) data->d_buf;
12290 14494 : if (unlikely (end == NULL))
12291 : {
12292 0 : printf (" [%6zx]- %.*s\n",
12293 : pos, (int) (limit - start), start);
12294 : break;
12295 : }
12296 14494 : printf (" [%6zx] %s\n", pos, start);
12297 14494 : start = end + 1;
12298 14494 : } while (start < limit);
12299 : }
12300 : }
12301 73 : }
12302 :
12303 : static void
12304 38 : for_each_section_argument (Elf *elf, const struct section_argument *list,
12305 : void (*dump) (Elf_Scn *scn, const GElf_Shdr *shdr,
12306 : const char *name))
12307 : {
12308 : /* Get the section header string table index. */
12309 : size_t shstrndx;
12310 38 : if (elf_getshdrstrndx (elf, &shstrndx) < 0)
12311 : error (EXIT_FAILURE, 0,
12312 0 : gettext ("cannot get section header string table index"));
12313 :
12314 102 : for (const struct section_argument *a = list; a != NULL; a = a->next)
12315 : {
12316 : Elf_Scn *scn;
12317 : GElf_Shdr shdr_mem;
12318 102 : const char *name = NULL;
12319 :
12320 102 : char *endp = NULL;
12321 102 : unsigned long int shndx = strtoul (a->arg, &endp, 0);
12322 102 : if (endp != a->arg && *endp == '\0')
12323 : {
12324 0 : scn = elf_getscn (elf, shndx);
12325 0 : if (scn == NULL)
12326 : {
12327 0 : error (0, 0, gettext ("\nsection [%lu] does not exist"), shndx);
12328 0 : continue;
12329 : }
12330 :
12331 0 : if (gelf_getshdr (scn, &shdr_mem) == NULL)
12332 0 : error (EXIT_FAILURE, 0, gettext ("cannot get section header: %s"),
12333 : elf_errmsg (-1));
12334 0 : name = elf_strptr (elf, shstrndx, shdr_mem.sh_name);
12335 : }
12336 : else
12337 : {
12338 : /* Need to look up the section by name. */
12339 : scn = NULL;
12340 : bool found = false;
12341 2841 : while ((scn = elf_nextscn (elf, scn)) != NULL)
12342 : {
12343 2739 : if (gelf_getshdr (scn, &shdr_mem) == NULL)
12344 0 : continue;
12345 2739 : name = elf_strptr (elf, shstrndx, shdr_mem.sh_name);
12346 2739 : if (name == NULL)
12347 0 : continue;
12348 2739 : if (!strcmp (name, a->arg))
12349 : {
12350 78 : found = true;
12351 78 : (*dump) (scn, &shdr_mem, name);
12352 : }
12353 : }
12354 :
12355 102 : if (unlikely (!found) && !a->implicit)
12356 0 : error (0, 0, gettext ("\nsection '%s' does not exist"), a->arg);
12357 : }
12358 : }
12359 38 : }
12360 :
12361 : static void
12362 : dump_data (Ebl *ebl)
12363 : {
12364 5 : for_each_section_argument (ebl->elf, dump_data_sections, &dump_data_section);
12365 : }
12366 :
12367 : static void
12368 : dump_strings (Ebl *ebl)
12369 : {
12370 33 : for_each_section_argument (ebl->elf, string_sections, &print_string_section);
12371 : }
12372 :
12373 : static void
12374 0 : print_strings (Ebl *ebl)
12375 : {
12376 : /* Get the section header string table index. */
12377 : size_t shstrndx;
12378 0 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
12379 : error (EXIT_FAILURE, 0,
12380 0 : gettext ("cannot get section header string table index"));
12381 :
12382 : Elf_Scn *scn;
12383 : GElf_Shdr shdr_mem;
12384 : const char *name;
12385 : scn = NULL;
12386 0 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
12387 : {
12388 0 : if (gelf_getshdr (scn, &shdr_mem) == NULL)
12389 0 : continue;
12390 :
12391 0 : if (shdr_mem.sh_type != SHT_PROGBITS
12392 0 : || !(shdr_mem.sh_flags & SHF_STRINGS))
12393 0 : continue;
12394 :
12395 0 : name = elf_strptr (ebl->elf, shstrndx, shdr_mem.sh_name);
12396 0 : if (name == NULL)
12397 0 : continue;
12398 :
12399 0 : print_string_section (scn, &shdr_mem, name);
12400 : }
12401 0 : }
12402 :
12403 : static void
12404 2 : dump_archive_index (Elf *elf, const char *fname)
12405 : {
12406 : size_t narsym;
12407 2 : const Elf_Arsym *arsym = elf_getarsym (elf, &narsym);
12408 2 : if (arsym == NULL)
12409 : {
12410 0 : int result = elf_errno ();
12411 0 : if (unlikely (result != ELF_E_NO_INDEX))
12412 0 : error (EXIT_FAILURE, 0,
12413 0 : gettext ("cannot get symbol index of archive '%s': %s"),
12414 : fname, elf_errmsg (result));
12415 : else
12416 0 : printf (gettext ("\nArchive '%s' has no symbol index\n"), fname);
12417 0 : return;
12418 : }
12419 :
12420 4 : printf (gettext ("\nIndex of archive '%s' has %zu entries:\n"),
12421 : fname, narsym);
12422 :
12423 2 : size_t as_off = 0;
12424 11 : for (const Elf_Arsym *s = arsym; s < &arsym[narsym - 1]; ++s)
12425 : {
12426 9 : if (s->as_off != as_off)
12427 : {
12428 6 : as_off = s->as_off;
12429 :
12430 6 : Elf *subelf = NULL;
12431 6 : if (unlikely (elf_rand (elf, as_off) == 0)
12432 6 : || unlikely ((subelf = elf_begin (-1, ELF_C_READ_MMAP, elf))
12433 : == NULL))
12434 : #if __GLIBC__ < 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ < 7)
12435 : while (1)
12436 : #endif
12437 0 : error (EXIT_FAILURE, 0,
12438 0 : gettext ("cannot extract member at offset %zu in '%s': %s"),
12439 : as_off, fname, elf_errmsg (-1));
12440 :
12441 6 : const Elf_Arhdr *h = elf_getarhdr (subelf);
12442 :
12443 12 : printf (gettext ("Archive member '%s' contains:\n"), h->ar_name);
12444 :
12445 6 : elf_end (subelf);
12446 : }
12447 :
12448 18 : printf ("\t%s\n", s->as_name);
12449 : }
12450 : }
12451 :
12452 : #include "debugpred.h"
|