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 212 : main (int argc, char *argv[])
318 : {
319 : /* We use no threads here which can interfere with handling a stream. */
320 212 : (void) __fsetlocking (stdout, FSETLOCKING_BYCALLER);
321 :
322 : /* Set locale. */
323 212 : setlocale (LC_ALL, "");
324 :
325 : /* Initialize the message catalog. */
326 212 : textdomain (PACKAGE_TARNAME);
327 :
328 : /* Look up once. */
329 212 : yes_str = gettext ("yes");
330 212 : no_str = gettext ("no");
331 :
332 : /* Parse and process arguments. */
333 : int remaining;
334 212 : argp_parse (&argp, argc, argv, 0, &remaining, NULL);
335 :
336 : /* Before we start tell the ELF library which version we are using. */
337 212 : elf_version (EV_CURRENT);
338 :
339 : /* Now process all the files given at the command line. */
340 212 : bool only_one = remaining + 1 == argc;
341 : do
342 : {
343 : /* Open the file. */
344 436 : int fd = open (argv[remaining], O_RDONLY);
345 218 : if (fd == -1)
346 : {
347 0 : error (0, errno, gettext ("cannot open input file"));
348 0 : continue;
349 : }
350 :
351 218 : process_file (fd, argv[remaining], only_one);
352 :
353 218 : close (fd);
354 : }
355 218 : while (++remaining < argc);
356 :
357 212 : return error_message_count != 0;
358 : }
359 :
360 :
361 : /* Handle program arguments. */
362 : static error_t
363 1331 : 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 1331 : switch (key)
379 : {
380 : 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 : case 'A':
399 4 : print_arch = true;
400 4 : any_control_option = true;
401 4 : break;
402 : case 'd':
403 2 : print_dynamic_table = true;
404 2 : any_control_option = true;
405 2 : break;
406 : case 'e':
407 0 : print_debug_sections |= section_exception;
408 0 : any_control_option = true;
409 0 : break;
410 : case 'g':
411 0 : print_section_groups = true;
412 0 : any_control_option = true;
413 0 : break;
414 : case 'h':
415 0 : print_file_header = true;
416 0 : any_control_option = true;
417 0 : break;
418 : case 'I':
419 0 : print_histogram = true;
420 0 : any_control_option = true;
421 0 : break;
422 : case 'l':
423 0 : print_program_header = true;
424 0 : any_control_option = true;
425 0 : break;
426 : case 'n':
427 10 : print_notes = true;
428 10 : any_control_option = true;
429 10 : break;
430 : case 'r':
431 1 : print_relocations = true;
432 1 : any_control_option = true;
433 1 : break;
434 : case 'S':
435 54 : print_section_header = true;
436 54 : any_control_option = true;
437 54 : break;
438 : case 's':
439 14 : print_symbol_table = true;
440 14 : any_control_option = true;
441 14 : symbol_table_section = arg;
442 14 : break;
443 : case 'V':
444 0 : print_version_info = true;
445 0 : any_control_option = true;
446 0 : break;
447 : case 'c':
448 2 : print_archive_index = true;
449 2 : break;
450 : case 'w':
451 102 : 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 69 : else if (strcmp (arg, "abbrev") == 0)
458 0 : print_debug_sections |= section_abbrev;
459 69 : else if (strcmp (arg, "addr") == 0)
460 : {
461 2 : print_debug_sections |= section_addr;
462 2 : implicit_debug_sections |= section_info;
463 : }
464 67 : else if (strcmp (arg, "aranges") == 0)
465 3 : print_debug_sections |= section_aranges;
466 64 : else if (strcmp (arg, "decodedaranges") == 0)
467 : {
468 1 : print_debug_sections |= section_aranges;
469 1 : decodedaranges = true;
470 : }
471 63 : else if (strcmp (arg, "ranges") == 0)
472 : {
473 12 : print_debug_sections |= section_ranges;
474 12 : implicit_debug_sections |= section_info;
475 : }
476 51 : else if (strcmp (arg, "frame") == 0 || strcmp (arg, "frames") == 0)
477 2 : print_debug_sections |= section_frame;
478 49 : else if (strcmp (arg, "info") == 0)
479 : {
480 14 : print_debug_sections |= section_info;
481 14 : 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 102 : any_control_option = true;
526 102 : break;
527 : 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 : case 'N':
540 2 : print_address_names = false;
541 2 : break;
542 : case 'U':
543 20 : print_unresolved_addresses = true;
544 20 : break;
545 : case ARGP_KEY_NO_ARGS:
546 0 : fputs (gettext ("Missing file name.\n"), stderr);
547 0 : goto do_argp_help;
548 : case ARGP_KEY_FINI:
549 212 : if (! any_control_option && ! print_archive_index)
550 : {
551 0 : fputs (gettext ("No operation specified.\n"), stderr);
552 : 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 : case 'z':
561 13 : print_decompress = true;
562 13 : break;
563 : 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 : 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 : 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 : 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 204 : 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 204 : if (*(bool *) arg)
732 : return DWARF_CB_ABORT;
733 204 : *(bool *) arg = true;
734 204 : 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 216 : 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 216 : const struct process_dwflmod_args *a = arg;
751 :
752 : /* Print the file name. */
753 216 : if (!a->only_one)
754 : {
755 : const char *fname;
756 12 : dwfl_module_info (dwflmod, NULL, NULL, NULL, NULL, NULL, &fname, NULL);
757 :
758 12 : printf ("\n%s:\n\n", fname);
759 : }
760 :
761 216 : process_elf_file (dwflmod, a->fd);
762 :
763 216 : 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 225 : create_dwfl (int fd, const char *fname)
796 : {
797 : /* Duplicate an fd for dwfl_report_offline to swallow. */
798 225 : int dwfl_fd = dup (fd);
799 225 : 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 225 : Dwfl *dwfl = dwfl_begin (&callbacks);
810 225 : if (likely (dwfl != NULL))
811 : /* Let 0 be the logical address of the file (or first in archive). */
812 225 : dwfl->offline_next_address = 0;
813 225 : 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 225 : dwfl_report_end (dwfl, NULL, NULL);
828 :
829 225 : return dwfl;
830 : }
831 :
832 : /* Process one input file. */
833 : static void
834 218 : process_file (int fd, const char *fname, bool only_one)
835 : {
836 218 : if (print_archive_index)
837 2 : check_archive_index (fd, fname, only_one);
838 :
839 218 : if (!any_control_option)
840 : return;
841 :
842 216 : 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 4 : 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 216 : Dwfl *dwfl = create_dwfl (fd, fname);
858 216 : if (dwfl != NULL)
859 : {
860 216 : if (only_one)
861 : {
862 : /* Clear ONLY_ONE if we have multiple modules, from an archive. */
863 204 : bool seen = false;
864 204 : only_one = dwfl_getmodules (dwfl, &count_dwflmod, &seen, 0) == 0;
865 : }
866 :
867 : /* Process the one or more modules gleaned from this file. */
868 216 : struct process_dwflmod_args a = { .fd = fd, .only_one = only_one };
869 216 : 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 216 : if (! do_not_close_dwfl)
874 216 : dwfl_end (dwfl);
875 :
876 : /* Need to close the replaced fd if we created it. Caller takes
877 : care of original. */
878 216 : 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 1763 : while ((scn = elf_nextscn (elf, scn)) != NULL)
888 : {
889 : GElf_Shdr shdr_mem;
890 1623 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
891 1623 : 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 216 : process_elf_file (Dwfl_Module *dwflmod, int fd)
900 : {
901 : GElf_Addr dwflbias;
902 216 : Elf *elf = dwfl_module_getelf (dwflmod, &dwflbias);
903 :
904 : GElf_Ehdr ehdr_mem;
905 216 : GElf_Ehdr *ehdr = gelf_getehdr (elf, &ehdr_mem);
906 :
907 216 : if (ehdr == NULL)
908 : {
909 : elf_error:
910 0 : error (0, 0, gettext ("cannot read ELF header: %s"), elf_errmsg (-1));
911 0 : return;
912 : }
913 :
914 249 : Ebl *ebl = ebl_openbackend (elf);
915 216 : if (unlikely (ebl == NULL))
916 : {
917 : ebl_error:
918 0 : error (0, errno, gettext ("cannot create EBL handle"));
919 : return;
920 : }
921 :
922 : /* Determine the number of sections. */
923 216 : 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 216 : 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 216 : bool print_unchanged = ((print_section_header
940 130 : || print_relocations
941 129 : || dump_data_sections != NULL
942 124 : || print_notes)
943 318 : && (ehdr->e_type == ET_REL
944 73 : || elf_contains_chdrs (ebl->elf)));
945 :
946 216 : Elf *pure_elf = NULL;
947 221 : Ebl *pure_ebl = ebl;
948 216 : 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 216 : if (print_file_header)
969 32 : print_ehdr (ebl, ehdr);
970 216 : if (print_section_header)
971 86 : print_shdr (pure_ebl, ehdr);
972 216 : if (print_program_header)
973 32 : print_phdr (ebl, ehdr);
974 216 : if (print_section_groups)
975 32 : print_scngrp (ebl);
976 216 : if (print_dynamic_table)
977 34 : print_dynamic (ebl);
978 216 : if (print_relocations)
979 33 : print_relocs (pure_ebl, ehdr);
980 216 : if (print_histogram)
981 32 : handle_hash (ebl);
982 216 : if (print_symbol_table)
983 46 : print_symtab (ebl, SHT_DYNSYM);
984 216 : if (print_version_info)
985 32 : print_verinfo (ebl);
986 216 : if (print_symbol_table)
987 46 : print_symtab (ebl, SHT_SYMTAB);
988 216 : if (print_arch)
989 36 : print_liblist (ebl);
990 216 : if (print_arch)
991 36 : print_attributes (ebl, ehdr);
992 216 : if (dump_data_sections != NULL)
993 5 : dump_data (pure_ebl);
994 216 : if (string_sections != NULL)
995 33 : dump_strings (ebl);
996 216 : if ((print_debug_sections | implicit_debug_sections) != 0)
997 125 : print_debug (dwflmod, ebl, ehdr);
998 216 : if (print_notes)
999 42 : handle_notes (pure_ebl, ehdr);
1000 216 : if (print_string_sections)
1001 0 : print_strings (ebl);
1002 :
1003 216 : ebl_closebackend (ebl);
1004 :
1005 216 : 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 512 : printf (" %02hhx", ehdr->e_ident[cnt]);
1045 :
1046 64 : 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 64 : 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 64 : 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 32 : printf (gettext (" OS/ABI: %s\n"),
1064 32 : ebl_osabi_name (ebl, ehdr->e_ident[EI_OSABI], buf, sizeof (buf)));
1065 :
1066 32 : 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 32 : printf (gettext (" Machine: %s\n"), ebl->name);
1073 :
1074 32 : printf (gettext (" Version: %d %s\n"),
1075 : ehdr->e_version,
1076 32 : ehdr->e_version == EV_CURRENT ? gettext ("(current)") : "(\?\?\?)");
1077 :
1078 32 : printf (gettext (" Entry point address: %#" PRIx64 "\n"),
1079 : ehdr->e_entry);
1080 :
1081 32 : printf (gettext (" Start of program headers: %" PRId64 " %s\n"),
1082 : ehdr->e_phoff, gettext ("(bytes into file)"));
1083 :
1084 32 : printf (gettext (" Start of section headers: %" PRId64 " %s\n"),
1085 : ehdr->e_shoff, gettext ("(bytes into file)"));
1086 :
1087 32 : printf (gettext (" Flags: %s\n"),
1088 : ebl_machine_flag_name (ebl, ehdr->e_flags, buf, sizeof (buf)));
1089 :
1090 64 : printf (gettext (" Size of this header: %" PRId16 " %s\n"),
1091 32 : ehdr->e_ehsize, gettext ("(bytes)"));
1092 :
1093 64 : printf (gettext (" Size of program header entries: %" PRId16 " %s\n"),
1094 32 : ehdr->e_phentsize, gettext ("(bytes)"));
1095 :
1096 32 : 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 : (uint32_t) shdr->sh_info);
1105 : else
1106 0 : fputs_unlocked (gettext (" ([0] not available)"), stdout);
1107 : }
1108 32 : fputc_unlocked ('\n', stdout);
1109 :
1110 64 : printf (gettext (" Size of section header entries: %" PRId16 " %s\n"),
1111 32 : ehdr->e_shentsize, gettext ("(bytes)"));
1112 :
1113 32 : 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 32 : 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 : (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 : switch (value)
1154 : {
1155 : case STV_DEFAULT:
1156 : return "DEFAULT";
1157 : case STV_INTERNAL:
1158 : return "INTERNAL";
1159 : case STV_HIDDEN:
1160 : return "HIDDEN";
1161 : case STV_PROTECTED:
1162 : return "PROTECTED";
1163 : 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 2173 : for (cnt = 0; cnt < shnum; ++cnt)
1214 : {
1215 2173 : Elf_Scn *scn = elf_getscn (ebl->elf, cnt);
1216 :
1217 2173 : 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 2173 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
1224 2173 : 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 2173 : char *cp = flagbuf;
1230 2173 : if (shdr->sh_flags & SHF_WRITE)
1231 490 : *cp++ = 'W';
1232 2173 : if (shdr->sh_flags & SHF_ALLOC)
1233 1435 : *cp++ = 'A';
1234 2173 : if (shdr->sh_flags & SHF_EXECINSTR)
1235 267 : *cp++ = 'X';
1236 2173 : if (shdr->sh_flags & SHF_MERGE)
1237 102 : *cp++ = 'M';
1238 2173 : if (shdr->sh_flags & SHF_STRINGS)
1239 102 : *cp++ = 'S';
1240 2173 : if (shdr->sh_flags & SHF_INFO_LINK)
1241 93 : *cp++ = 'I';
1242 2173 : if (shdr->sh_flags & SHF_LINK_ORDER)
1243 2 : *cp++ = 'L';
1244 2173 : if (shdr->sh_flags & SHF_OS_NONCONFORMING)
1245 0 : *cp++ = 'N';
1246 2173 : if (shdr->sh_flags & SHF_GROUP)
1247 0 : *cp++ = 'G';
1248 2173 : if (shdr->sh_flags & SHF_TLS)
1249 8 : *cp++ = 'T';
1250 2173 : if (shdr->sh_flags & SHF_COMPRESSED)
1251 12 : *cp++ = 'C';
1252 2173 : if (shdr->sh_flags & SHF_ORDERED)
1253 0 : *cp++ = 'O';
1254 2173 : if (shdr->sh_flags & SHF_EXCLUDE)
1255 0 : *cp++ = 'E';
1256 2173 : *cp = '\0';
1257 :
1258 : const char *sname;
1259 : char buf[128];
1260 2173 : sname = elf_strptr (ebl->elf, shstrndx, shdr->sh_name) ?: "<corrupt>";
1261 6519 : 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 2173 : 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 2173 : 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 2173 : 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 86 : 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 540 : 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 : 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 3306 : for (size_t inner = 1; inner < shnum; ++inner)
1416 : {
1417 3306 : Elf_Scn *scn = elf_getscn (ebl->elf, inner);
1418 : /* This should not happen. */
1419 3306 : 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 3306 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
1426 3306 : if (unlikely (shdr == NULL))
1427 0 : error (EXIT_FAILURE, 0,
1428 0 : gettext ("cannot get section header: %s"),
1429 : elf_errmsg (-1));
1430 :
1431 3306 : if (shdr->sh_size > 0
1432 : /* Compare allocated sections by VMA, unallocated
1433 : sections by file offset. */
1434 6612 : && (shdr->sh_flags & SHF_ALLOC
1435 2476 : ? (shdr->sh_addr >= phdr->p_vaddr
1436 4072 : && (shdr->sh_addr + shdr->sh_size
1437 1596 : <= 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 392 : 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 354 : else if (has_relro && in_relro && shdr->sh_addr >= relro_to)
1450 : {
1451 10 : fputs_unlocked ("]", stdout);
1452 10 : in_relro = false;
1453 : }
1454 344 : else if (has_relro && in_relro
1455 116 : && shdr->sh_addr + shdr->sh_size > relro_to)
1456 0 : fputs_unlocked ("] <RELRO:", stdout);
1457 344 : 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 352 : for (cnt2 = 0; cnt2 < phnum; ++cnt2)
1472 : {
1473 520 : phdr2 = gelf_getphdr (ebl->elf, cnt2, &phdr2_mem);
1474 :
1475 520 : if (phdr2 != NULL && phdr2->p_type == PT_LOAD
1476 304 : && shdr->sh_addr >= phdr2->p_vaddr
1477 608 : && (shdr->sh_addr + shdr->sh_size
1478 304 : <= phdr2->p_vaddr + phdr2->p_memsz))
1479 : break;
1480 : }
1481 :
1482 168 : if (cnt2 < phnum)
1483 : {
1484 168 : if ((phdr2->p_flags & PF_W) == 0 && !in_ro)
1485 : {
1486 26 : fputs_unlocked (" [RO:", stdout);
1487 26 : in_ro = true;
1488 : }
1489 142 : 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 392 : printf (" %s",
1498 392 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name));
1499 :
1500 : /* Signal that this sectin is only partially covered. */
1501 392 : if (has_relro && in_relro
1502 154 : && 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 90 : 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 325 : 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 : 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 874 : 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 810 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
1595 :
1596 810 : 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 4 : print_flags (int class, GElf_Xword d_val, const struct flags *flags,
1672 : int nflags)
1673 : {
1674 4 : bool first = true;
1675 : int cnt;
1676 :
1677 30 : for (cnt = 0; cnt < nflags; ++cnt)
1678 26 : 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 4 : if (d_val != 0)
1688 : {
1689 4 : if (!first)
1690 : putchar_unlocked (' ');
1691 4 : printf ("%#0*" PRIx64, class == ELFCLASS32 ? 10 : 18, d_val);
1692 : }
1693 :
1694 : putchar_unlocked ('\n');
1695 4 : }
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 1 : 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 24 : 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 : (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 442 : for (cnt = 0; cnt < shdr->sh_size / sh_entsize; ++cnt)
1767 : {
1768 : GElf_Dyn dynmem;
1769 430 : GElf_Dyn *dyn = gelf_getdyn (data, cnt, &dynmem);
1770 430 : if (dyn == NULL)
1771 : break;
1772 :
1773 : char buf[64];
1774 430 : printf (" %-17s ",
1775 : ebl_dynamic_tag_name (ebl, dyn->d_tag, buf, sizeof (buf)));
1776 :
1777 430 : switch (dyn->d_tag)
1778 : {
1779 : case DT_NULL:
1780 : case DT_DEBUG:
1781 : case DT_BIND_NOW:
1782 : case DT_TEXTREL:
1783 : /* No further output. */
1784 72 : fputc_unlocked ('\n', stdout);
1785 : break;
1786 :
1787 : 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 : 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 : 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 : 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 : 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 : case DT_VERDEFNUM:
1827 : case DT_VERNEEDNUM:
1828 : case DT_RELACOUNT:
1829 : case DT_RELCOUNT:
1830 25 : printf ("%" PRId64 "\n", dyn->d_un.d_val);
1831 : break;
1832 :
1833 : 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 : case DT_FLAGS:
1840 1 : print_dt_flags (class, dyn->d_un.d_val);
1841 : break;
1842 :
1843 : case DT_FLAGS_1:
1844 1 : print_dt_flags_1 (class, dyn->d_un.d_val);
1845 : break;
1846 :
1847 : case DT_FEATURE_1:
1848 1 : print_dt_feature_1 (class, dyn->d_un.d_val);
1849 : break;
1850 :
1851 : case DT_POSFLAG_1:
1852 1 : print_dt_posflag_1 (class, dyn->d_un.d_val);
1853 : break;
1854 :
1855 : 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 910 : 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 844 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
1898 :
1899 844 : if (likely (shdr != NULL))
1900 : {
1901 844 : if (shdr->sh_type == SHT_REL)
1902 8 : handle_relocs_rel (ebl, ehdr, scn, shdr);
1903 836 : else if (shdr->sh_type == SHT_RELA)
1904 123 : 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 : (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 : ? 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 : ? 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 123 : handle_relocs_rela (Ebl *ebl, GElf_Ehdr *ehdr, Elf_Scn *scn, GElf_Shdr *shdr)
2103 : {
2104 123 : int class = gelf_getclass (ebl->elf);
2105 123 : size_t sh_entsize = gelf_fsize (ebl->elf, ELF_T_RELA, 1, EV_CURRENT);
2106 123 : int nentries = shdr->sh_size / sh_entsize;
2107 :
2108 : /* Get the data of the section. */
2109 123 : Elf_Data *data = elf_getdata (scn, NULL);
2110 123 : if (data == NULL)
2111 0 : return;
2112 :
2113 : /* Get the symbol table information. */
2114 123 : Elf_Scn *symscn = elf_getscn (ebl->elf, shdr->sh_link);
2115 : GElf_Shdr symshdr_mem;
2116 123 : GElf_Shdr *symshdr = gelf_getshdr (symscn, &symshdr_mem);
2117 123 : 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 123 : GElf_Shdr *destshdr = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_info),
2122 : &destshdr_mem);
2123 :
2124 123 : 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 123 : Elf_Data *xndxdata = NULL;
2133 123 : int xndxscnidx = elf_scnshndx (scn);
2134 123 : 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 123 : 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 123 : if (shdr->sh_info != 0)
2144 339 : 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 113 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
2151 : (unsigned int) shdr->sh_info,
2152 113 : 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 123 : 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 123 : int is_statically_linked = 0;
2176 21859 : for (int cnt = 0; cnt < nentries; ++cnt)
2177 : {
2178 : GElf_Rela relmem;
2179 21736 : GElf_Rela *rel = gelf_getrela (data, cnt, &relmem);
2180 21736 : if (likely (rel != NULL))
2181 : {
2182 : char buf[64];
2183 : GElf_Sym symmem;
2184 : Elf32_Word xndx;
2185 21736 : GElf_Sym *sym = gelf_getsymshndx (symdata, xndxdata,
2186 21736 : GELF_R_SYM (rel->r_info),
2187 : &symmem, &xndx);
2188 :
2189 21736 : 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 21736 : else if (GELF_ST_TYPE (sym->st_info) != STT_SECTION)
2242 22356 : printf ("\
2243 : %#0*" PRIx64 " %-15s %#0*" PRIx64 " %+6" PRId64 " %s\n",
2244 : class == ELFCLASS32 ? 10 : 18, rel->r_offset,
2245 5589 : 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 5589 : ? 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 11178 : 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 16147 : secshdr = gelf_getshdr (elf_getscn (ebl->elf,
2261 16147 : sym->st_shndx == SHN_XINDEX
2262 : ? xndx : sym->st_shndx),
2263 : &secshdr_mem);
2264 :
2265 16147 : 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 : ? xndx : sym->st_shndx));
2277 : else
2278 48441 : printf ("\
2279 : %#0*" PRIx64 " %-15s %#0*" PRIx64 " %+6" PRId64 " %s\n",
2280 : class == ELFCLASS32 ? 10 : 18, rel->r_offset,
2281 16147 : ebl_reloc_type_check (ebl, GELF_R_TYPE (rel->r_info))
2282 : /* Avoid the leading R_ which isn't carrying any
2283 : information. */
2284 16147 : ? 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 16147 : 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 2562 : 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 2378 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
2309 :
2310 2378 : 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 1577 : while ((runscn = elf_nextscn (ebl->elf, runscn)) != NULL)
2362 : {
2363 : GElf_Shdr runshdr_mem;
2364 1523 : GElf_Shdr *runshdr = gelf_getshdr (runscn, &runshdr_mem);
2365 :
2366 1523 : if (likely (runshdr != NULL))
2367 : {
2368 1523 : 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 1506 : 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 1479 : 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 1471 : 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 108 : 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 54 : printf (ngettext (" %lu local symbol String table: [%2u] '%s'\n",
2415 : " %lu local symbols String table: [%2u] '%s'\n",
2416 : shdr->sh_info),
2417 : (unsigned long int) shdr->sh_info,
2418 : (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 13783 : 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 13729 : GElf_Sym *sym = gelf_getsymshndx (data, xndx_data, cnt, &sym_mem, &xndx);
2436 :
2437 13729 : if (unlikely (sym == NULL))
2438 0 : continue;
2439 :
2440 : /* Determine the real section index. */
2441 13729 : if (likely (sym->st_shndx != SHN_XINDEX))
2442 13729 : xndx = sym->st_shndx;
2443 :
2444 82374 : 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 13729 : ebl_symbol_type_name (ebl, GELF_ST_TYPE (sym->st_info),
2451 : typebuf, sizeof (typebuf)),
2452 13729 : ebl_symbol_binding_name (ebl, GELF_ST_BIND (sym->st_info),
2453 : bindbuf, sizeof (bindbuf)),
2454 13729 : get_visibility_type (GELF_ST_VISIBILITY (sym->st_other)),
2455 13729 : ebl_section_name (ebl, sym->st_shndx, xndx, scnbuf,
2456 : sizeof (scnbuf), NULL, shnum),
2457 27458 : elf_strptr (ebl->elf, shdr->sh_link, sym->st_name));
2458 :
2459 13729 : if (versym_data != NULL)
2460 : {
2461 : /* Get the version information. */
2462 : GElf_Versym versym_mem;
2463 1406 : GElf_Versym *versym = gelf_getversym (versym_data, cnt, &versym_mem);
2464 :
2465 1406 : if (versym != NULL && ((*versym & 0x8000) != 0 || *versym > 1))
2466 : {
2467 1187 : bool is_nobits = false;
2468 1187 : bool check_def = xndx != SHN_UNDEF;
2469 :
2470 1187 : if (xndx < SHN_LORESERVE || sym->st_shndx == SHN_XINDEX)
2471 : {
2472 : GElf_Shdr symshdr_mem;
2473 1152 : GElf_Shdr *symshdr =
2474 1152 : gelf_getshdr (elf_getscn (ebl->elf, xndx), &symshdr_mem);
2475 :
2476 1152 : is_nobits = (symshdr != NULL
2477 1152 : && symshdr->sh_type == SHT_NOBITS);
2478 : }
2479 :
2480 1187 : if (is_nobits || ! check_def)
2481 : {
2482 : /* We must test both. */
2483 : GElf_Vernaux vernaux_mem;
2484 813 : GElf_Vernaux *vernaux = NULL;
2485 813 : size_t vn_offset = 0;
2486 :
2487 : GElf_Verneed verneed_mem;
2488 813 : GElf_Verneed *verneed = gelf_getverneed (verneed_data, 0,
2489 : &verneed_mem);
2490 4040 : while (verneed != NULL)
2491 : {
2492 3227 : size_t vna_offset = vn_offset;
2493 :
2494 3227 : vernaux = gelf_getvernaux (verneed_data,
2495 3227 : vna_offset += verneed->vn_aux,
2496 : &vernaux_mem);
2497 9896 : while (vernaux != NULL
2498 6669 : && vernaux->vna_other != *versym
2499 5856 : && vernaux->vna_next != 0)
2500 : {
2501 : /* Update the offset. */
2502 3442 : vna_offset += vernaux->vna_next;
2503 :
2504 3442 : vernaux = (vernaux->vna_next == 0
2505 : ? NULL
2506 3442 : : gelf_getvernaux (verneed_data,
2507 : vna_offset,
2508 : &vernaux_mem));
2509 : }
2510 :
2511 : /* Check whether we found the version. */
2512 3227 : if (vernaux != NULL && vernaux->vna_other == *versym)
2513 : /* Found it. */
2514 : break;
2515 :
2516 2414 : vn_offset += verneed->vn_next;
2517 2414 : verneed = (verneed->vn_next == 0
2518 : ? NULL
2519 2414 : : gelf_getverneed (verneed_data, vn_offset,
2520 : &verneed_mem));
2521 : }
2522 :
2523 813 : if (vernaux != NULL && vernaux->vna_other == *versym)
2524 : {
2525 1626 : printf ("@%s (%u)",
2526 : elf_strptr (ebl->elf, verneed_stridx,
2527 813 : vernaux->vna_name),
2528 : (unsigned int) vernaux->vna_other);
2529 813 : 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 1187 : if (check_def && *versym != 0x8001)
2538 : {
2539 : /* We must test both. */
2540 374 : size_t vd_offset = 0;
2541 :
2542 : GElf_Verdef verdef_mem;
2543 374 : GElf_Verdef *verdef = gelf_getverdef (verdef_data, 0,
2544 : &verdef_mem);
2545 2794 : while (verdef != NULL)
2546 : {
2547 2420 : if (verdef->vd_ndx == (*versym & 0x7fff))
2548 : /* Found the definition. */
2549 : break;
2550 :
2551 2046 : vd_offset += verdef->vd_next;
2552 2046 : verdef = (verdef->vd_next == 0
2553 : ? NULL
2554 2046 : : gelf_getverdef (verdef_data, vd_offset,
2555 : &verdef_mem));
2556 : }
2557 :
2558 374 : if (verdef != NULL)
2559 : {
2560 : GElf_Verdaux verdaux_mem;
2561 374 : GElf_Verdaux *verdaux
2562 374 : = gelf_getverdaux (verdef_data,
2563 374 : vd_offset + verdef->vd_aux,
2564 : &verdaux_mem);
2565 :
2566 374 : if (verdaux != NULL)
2567 374 : printf ((*versym & 0x8000) ? "@%s" : "@@%s",
2568 : elf_strptr (ebl->elf, verdef_stridx,
2569 374 : verdaux->vda_name));
2570 : }
2571 : }
2572 : }
2573 : }
2574 :
2575 : 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 874 : 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 810 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
2593 :
2594 810 : if (likely (shdr != NULL))
2595 : {
2596 810 : if (shdr->sh_type == SHT_GNU_verneed)
2597 10 : handle_verneed (ebl, scn, shdr);
2598 800 : else if (shdr->sh_type == SHT_GNU_verdef)
2599 4 : handle_verdef (ebl, scn, shdr);
2600 796 : 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 121 : get_ver_flags (unsigned int flags)
2609 : {
2610 : static char buf[32];
2611 : char *endp;
2612 :
2613 121 : if (flags == 0)
2614 116 : 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 30 : 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 : (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 148 : 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 328 : 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 12 : 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 : (unsigned int) shdr->sh_link,
2750 4 : elf_strptr (ebl->elf, shstrndx, glink->sh_name));
2751 :
2752 4 : unsigned int offset = 0;
2753 43 : for (int cnt = shdr->sh_info; --cnt >= 0; )
2754 : {
2755 : /* Get the data at the next offset. */
2756 : GElf_Verdef defmem;
2757 39 : GElf_Verdef *def = gelf_getverdef (data, offset, &defmem);
2758 39 : if (unlikely (def == NULL))
2759 : break;
2760 :
2761 39 : unsigned int auxoffset = offset + def->vd_aux;
2762 : GElf_Verdaux auxmem;
2763 39 : GElf_Verdaux *aux = gelf_getverdaux (data, auxoffset, &auxmem);
2764 39 : if (unlikely (aux == NULL))
2765 : break;
2766 :
2767 234 : printf (gettext ("\
2768 : %#06x: Version: %hd Flags: %s Index: %hd Cnt: %hd Name: %s\n"),
2769 39 : offset, def->vd_version,
2770 39 : get_ver_flags (def->vd_flags),
2771 39 : def->vd_ndx,
2772 39 : def->vd_cnt,
2773 78 : elf_strptr (ebl->elf, shdr->sh_link, aux->vda_name));
2774 :
2775 39 : auxoffset += aux->vda_next;
2776 39 : for (int cnt2 = 1; cnt2 < def->vd_cnt; ++cnt2)
2777 : {
2778 31 : aux = gelf_getverdaux (data, auxoffset, &auxmem);
2779 31 : if (unlikely (aux == NULL))
2780 : break;
2781 :
2782 62 : printf (gettext (" %#06x: Parent %d: %s\n"),
2783 : auxoffset, cnt2,
2784 62 : elf_strptr (ebl->elf, shdr->sh_link, aux->vda_name));
2785 :
2786 31 : if (aux->vda_next == 0)
2787 : break;
2788 :
2789 0 : auxoffset += aux->vda_next;
2790 : }
2791 :
2792 : /* Find the next offset. */
2793 39 : if (def->vd_next == 0)
2794 : break;
2795 35 : 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 378 : while ((verscn = elf_nextscn (ebl->elf, verscn)) != NULL)
2825 : {
2826 : GElf_Shdr vershdr_mem;
2827 368 : GElf_Shdr *vershdr = gelf_getshdr (verscn, &vershdr_mem);
2828 :
2829 368 : if (likely (vershdr != NULL))
2830 : {
2831 368 : if (vershdr->sh_type == SHT_GNU_verdef)
2832 : defscn = verscn;
2833 364 : 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 35 : 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 39 : def = gelf_getverdef (defdata, offset, &defmem);
2868 39 : if (unlikely (def == NULL))
2869 : break;
2870 :
2871 39 : nvername = MAX (nvername, (size_t) (def->vd_ndx & 0x7fff));
2872 :
2873 39 : if (def->vd_next == 0)
2874 : break;
2875 35 : 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 10 : memset(vername, 0, nvername * sizeof (const char *));
2936 10 : filename = (const char **) alloca (nvername * sizeof (const char *));
2937 10 : 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 35 : 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 39 : GElf_Verdef *def = gelf_getverdef (defdata, offset, &defmem);
2963 39 : if (unlikely (def == NULL))
2964 : break;
2965 :
2966 : GElf_Verdaux auxmem;
2967 39 : GElf_Verdaux *aux = gelf_getverdaux (defdata,
2968 39 : offset + def->vd_aux,
2969 : &auxmem);
2970 39 : if (unlikely (aux == NULL))
2971 : break;
2972 :
2973 39 : vername[def->vd_ndx & 0x7fff]
2974 39 : = elf_strptr (ebl->elf, defshdr->sh_link, aux->vda_name);
2975 39 : filename[def->vd_ndx & 0x7fff] = NULL;
2976 :
2977 39 : if (def->vd_next == 0)
2978 : break;
2979 35 : 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 82 : = elf_strptr (ebl->elf, needshdr->sh_link, aux->vna_name);
3013 82 : filename[aux->vna_other & 0x7fff]
3014 82 : = 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 40 : 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 : (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 1329 : for (unsigned int cnt = 0; cnt < shdr->sh_size / sh_entsize; ++cnt)
3058 : {
3059 1319 : if (cnt % 2 == 0)
3060 : printf ("\n %4d:", cnt);
3061 :
3062 : GElf_Versym symmem;
3063 1319 : GElf_Versym *sym = gelf_getversym (data, cnt, &symmem);
3064 1319 : if (sym == NULL)
3065 : break;
3066 :
3067 1319 : switch (*sym)
3068 : {
3069 : ssize_t n;
3070 : case 0:
3071 113 : fputs_unlocked (gettext (" 0 *local* "),
3072 : stdout);
3073 : break;
3074 :
3075 : case 1:
3076 32 : fputs_unlocked (gettext (" 1 *global* "),
3077 : stdout);
3078 : break;
3079 :
3080 : default:
3081 4696 : n = printf ("%4d%c%s",
3082 1174 : *sym & 0x7fff, *sym & 0x8000 ? 'h' : ' ',
3083 : (vername != NULL
3084 1174 : && (unsigned int) (*sym & 0x7fff) < nvername)
3085 1174 : ? vername[*sym & 0x7fff] : "???");
3086 1174 : if ((unsigned int) (*sym & 0x7fff) < nvername
3087 1174 : && filename != NULL && filename[*sym & 0x7fff] != NULL)
3088 1600 : n += printf ("(%s)", filename[*sym & 0x7fff]);
3089 1174 : printf ("%*s", MAX (0, 33 - (int) n), " ");
3090 : break;
3091 : }
3092 : }
3093 : 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 10 : return;
3116 : }
3117 :
3118 40 : 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 : (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 10 : 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 55 : for (Elf32_Word cnt = 1; cnt <= maxlength; ++cnt)
3147 : {
3148 45 : nzero_counts += counts[cnt] * cnt;
3149 90 : printf (gettext ("\
3150 : %7d %6" PRIu32 " %5.1f%% %5.1f%%\n"),
3151 45 : (int) cnt, counts[cnt], (counts[cnt] * 100.0) / nbucket,
3152 45 : (nzero_counts * 100.0) / nsyms);
3153 : }
3154 :
3155 : Elf32_Word acc = 0;
3156 45 : for (Elf32_Word cnt = 1; cnt <= maxlength; ++cnt)
3157 : {
3158 45 : acc += cnt;
3159 45 : 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 : 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 : 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 : 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 222 : Elf32_Word inner = bucket[cnt] - symbias;
3361 : do
3362 : {
3363 429 : ++nsyms;
3364 429 : if (maxlength < ++lengths[cnt])
3365 45 : ++maxlength;
3366 429 : if (inner >= max_nsyms)
3367 : goto invalid_data;
3368 : }
3369 429 : 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 842 : 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 810 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
3422 :
3423 810 : if (likely (shdr != NULL))
3424 : {
3425 810 : 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 810 : 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 810 : 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 905 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
3467 : {
3468 : GElf_Shdr shdr_mem;
3469 869 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
3470 :
3471 869 : 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 36 : 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 : (unsigned int) lib->l_checksum,
3509 : (unsigned int) lib->l_version,
3510 : (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 905 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
3530 : {
3531 : GElf_Shdr shdr_mem;
3532 869 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
3533 :
3534 869 : if (shdr == NULL || (shdr->sh_type != SHT_GNU_ATTRIBUTES
3535 866 : && (shdr->sh_type != SHT_ARM_ATTRIBUTES
3536 1 : || ehdr->e_machine != EM_ARM)))
3537 865 : continue;
3538 :
3539 8 : 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 4 : 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 : default:
3623 : /* Unknown subsection, print and skip. */
3624 0 : printf (gettext (" %-4u %12" PRIu32 "\n"),
3625 : subsection_tag, subsection_len);
3626 : break;
3627 :
3628 : case 1: /* Tag_File */
3629 4 : printf (gettext (" File: %11" PRIu32 "\n"),
3630 : subsection_len);
3631 :
3632 26 : while (r < q)
3633 : {
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 585306 : 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 585306 : GElf_Off off = 0;
3715 1170584 : const char *name = (print_address_names && ! print_unresolved_addresses)
3716 585221 : ? dwfl_module_addrinfo (dwflmod, address, &off, &sym, NULL, NULL, NULL)
3717 1170527 : : NULL;
3718 :
3719 : const char *scn;
3720 585306 : if (print_unresolved_addresses)
3721 : {
3722 57 : address = raw;
3723 57 : scn = NULL;
3724 : }
3725 : else
3726 : {
3727 : /* Relativize the address. */
3728 585249 : int n = dwfl_module_relocations (dwflmod);
3729 585249 : 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 585249 : scn = (i < 0 ? NULL
3733 585249 : : dwfl_module_relocation_info (dwflmod, i, NULL));
3734 : }
3735 :
3736 585306 : if ((name != NULL
3737 572227 : ? (off != 0
3738 : ? (scn != NULL
3739 : ? (address_size == 0
3740 48395 : ? printf ("%s+%#" PRIx64 " <%s+%#" PRIx64 ">",
3741 : scn, address, name, off)
3742 258186 : : printf ("%s+%#0*" PRIx64 " <%s+%#" PRIx64 ">",
3743 129093 : scn, 2 + address_size * 2, address,
3744 : name, off))
3745 : : (address_size == 0
3746 94906 : ? printf ("%#" PRIx64 " <%s+%#" PRIx64 ">",
3747 : address, name, off)
3748 549960 : : printf ("%#0*" PRIx64 " <%s+%#" PRIx64 ">",
3749 274980 : 2 + address_size * 2, address,
3750 : name, off)))
3751 : : (scn != NULL
3752 : ? (address_size == 0
3753 2312 : ? printf ("%s+%#" PRIx64 " <%s>", scn, address, name)
3754 12348 : : printf ("%s+%#0*" PRIx64 " <%s>",
3755 6174 : scn, 2 + address_size * 2, address, name))
3756 : : (address_size == 0
3757 3964 : ? printf ("%#" PRIx64 " <%s>", address, name)
3758 24806 : : printf ("%#0*" PRIx64 " <%s>",
3759 12403 : 2 + address_size * 2, address, name))))
3760 : : (scn != NULL
3761 : ? (address_size == 0
3762 2321 : ? printf ("%s+%#" PRIx64, scn, address)
3763 1624 : : printf ("%s+%#0*" PRIx64, scn, 2 + address_size * 2, address))
3764 : : (address_size == 0
3765 3247 : ? printf ("%#" PRIx64, address)
3766 1742839 : : printf ("%#0*" PRIx64, 2 + address_size * 2, address)))) < 0)
3767 0 : error (EXIT_FAILURE, 0, _("sprintf failure"));
3768 585306 : }
3769 :
3770 :
3771 : static const char *
3772 745472 : dwarf_tag_string (unsigned int tag)
3773 : {
3774 745472 : 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 : default:
3780 0 : return NULL;
3781 : }
3782 : }
3783 :
3784 :
3785 : static const char *
3786 2610941 : dwarf_attr_string (unsigned int attrnum)
3787 : {
3788 2610941 : switch (attrnum)
3789 : {
3790 : #define DWARF_ONE_KNOWN_DW_AT(NAME, CODE) case CODE: return #NAME;
3791 5802 : DWARF_ALL_KNOWN_DW_AT
3792 : #undef DWARF_ONE_KNOWN_DW_AT
3793 : default:
3794 0 : return NULL;
3795 : }
3796 : }
3797 :
3798 :
3799 : static const char *
3800 2610947 : dwarf_form_string (unsigned int form)
3801 : {
3802 2610947 : 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 : default:
3808 0 : return NULL;
3809 : }
3810 : }
3811 :
3812 :
3813 : static const char *
3814 1134 : dwarf_lang_string (unsigned int lang)
3815 : {
3816 1134 : 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 : 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 2775 : if (likely (code < sizeof (known) / sizeof (known[0])))
3838 2775 : 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 13791 : if (likely (code < sizeof (known) / sizeof (known[0])))
3855 13791 : 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 355295 : if (likely (code < sizeof (known) / sizeof (known[0])))
4011 355295 : return known[code];
4012 :
4013 : return NULL;
4014 : }
4015 :
4016 :
4017 : static const char *
4018 : dwarf_unit_string (unsigned int type)
4019 : {
4020 : switch (type)
4021 : {
4022 : #define DWARF_ONE_KNOWN_DW_UT(NAME, CODE) case CODE: return #NAME;
4023 : DWARF_ALL_KNOWN_DW_UT
4024 : #undef DWARF_ONE_KNOWN_DW_UT
4025 : default:
4026 : return NULL;
4027 : }
4028 : }
4029 :
4030 :
4031 : static const char *
4032 : dwarf_range_list_encoding_string (unsigned int kind)
4033 : {
4034 : switch (kind)
4035 : {
4036 : #define DWARF_ONE_KNOWN_DW_RLE(NAME, CODE) case CODE: return #NAME;
4037 : DWARF_ALL_KNOWN_DW_RLE
4038 : #undef DWARF_ONE_KNOWN_DW_RLE
4039 : default:
4040 : return NULL;
4041 : }
4042 : }
4043 :
4044 :
4045 : static const char *
4046 : dwarf_loc_list_encoding_string (unsigned int kind)
4047 : {
4048 : switch (kind)
4049 : {
4050 : #define DWARF_ONE_KNOWN_DW_LLE(NAME, CODE) case CODE: return #NAME;
4051 : DWARF_ALL_KNOWN_DW_LLE
4052 : #undef DWARF_ONE_KNOWN_DW_LLE
4053 : default:
4054 : return NULL;
4055 : }
4056 : }
4057 :
4058 :
4059 : static const char *
4060 : dwarf_line_content_description_string (unsigned int kind)
4061 : {
4062 : switch (kind)
4063 : {
4064 : #define DWARF_ONE_KNOWN_DW_LNCT(NAME, CODE) case CODE: return #NAME;
4065 : DWARF_ALL_KNOWN_DW_LNCT
4066 : #undef DWARF_ONE_KNOWN_DW_LNCT
4067 : default:
4068 : return NULL;
4069 : }
4070 : }
4071 :
4072 :
4073 : /* Used by all dwarf_foo_name functions. */
4074 : static const char *
4075 5985177 : 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 5985177 : 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 : 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 745472 : dwarf_tag_name (unsigned int tag)
4103 : {
4104 745472 : const char *ret = dwarf_tag_string (tag);
4105 745472 : return string_or_unknown (ret, tag, DW_TAG_lo_user, DW_TAG_hi_user, true);
4106 : }
4107 :
4108 : static const char *
4109 2610941 : dwarf_attr_name (unsigned int attr)
4110 : {
4111 2610941 : const char *ret = dwarf_attr_string (attr);
4112 2610941 : return string_or_unknown (ret, attr, DW_AT_lo_user, DW_AT_hi_user, true);
4113 : }
4114 :
4115 :
4116 : static const char *
4117 2610947 : dwarf_form_name (unsigned int form)
4118 : {
4119 2610947 : const char *ret = dwarf_form_string (form);
4120 2610947 : return string_or_unknown (ret, form, 0, 0, true);
4121 : }
4122 :
4123 :
4124 : static const char *
4125 1134 : dwarf_lang_name (unsigned int lang)
4126 : {
4127 1134 : const char *ret = dwarf_lang_string (lang);
4128 1134 : return string_or_unknown (ret, lang, DW_LANG_lo_user, DW_LANG_hi_user, false);
4129 : }
4130 :
4131 :
4132 : static const char *
4133 2775 : dwarf_inline_name (unsigned int code)
4134 : {
4135 2775 : const char *ret = dwarf_inline_string (code);
4136 2775 : return string_or_unknown (ret, code, 0, 0, false);
4137 : }
4138 :
4139 :
4140 : static const char *
4141 13791 : dwarf_encoding_name (unsigned int code)
4142 : {
4143 13791 : const char *ret = dwarf_encoding_string (code);
4144 13791 : return string_or_unknown (ret, code, DW_ATE_lo_user, DW_ATE_hi_user, false);
4145 : }
4146 :
4147 :
4148 : static const char *
4149 0 : 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 0 : 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 0 : 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 0 : 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 0 : 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 0 : 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 0 : 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 0 : 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 8 : dwarf_unit_name (unsigned int type)
4214 : {
4215 8 : const char *ret = dwarf_unit_string (type);
4216 8 : 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 162 : print_block (size_t n, const void *block)
4247 : {
4248 162 : if (n == 0)
4249 0 : puts (_("empty block"));
4250 : else
4251 : {
4252 162 : printf (_("%zu byte block:"), n);
4253 162 : const unsigned char *data = block;
4254 : do
4255 1729 : printf (" %02x", *data++);
4256 1729 : while (--n > 0);
4257 : putchar ('\n');
4258 : }
4259 162 : }
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 136 : *addr = read_8ubyte_unaligned (cu->dbg, addrp);
4294 :
4295 : return 0;
4296 : }
4297 :
4298 : static void
4299 255972 : 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 255972 : const unsigned int ref_size = vers < 3 ? addrsize : offset_size;
4304 :
4305 255972 : if (len == 0)
4306 : {
4307 : printf ("%*s(empty)\n", indent, "");
4308 255972 : 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 611267 : while (len-- > 0)
4316 : {
4317 355295 : uint_fast8_t op = *data++;
4318 :
4319 710590 : const char *op_name = dwarf_locexpr_opcode_string (op);
4320 355295 : 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 355295 : switch (op)
4331 : {
4332 : case DW_OP_addr:;
4333 : /* Address operand. */
4334 : Dwarf_Word addr;
4335 7881 : NEED (addrsize);
4336 7881 : if (addrsize == 4)
4337 48 : addr = read_4ubyte_unaligned (dbg, data);
4338 7853 : else if (addrsize == 8)
4339 15688 : addr = read_8ubyte_unaligned (dbg, data);
4340 : else
4341 : goto invalid;
4342 7881 : data += addrsize;
4343 7881 : CONSUME (addrsize);
4344 :
4345 : printf ("%*s[%2" PRIuMAX "] %s ",
4346 : indent, "", (uintmax_t) offset, op_name);
4347 7881 : print_dwarf_addr (dwflmod, 0, addr, addr);
4348 : printf ("\n");
4349 :
4350 7881 : offset += 1 + addrsize;
4351 363176 : break;
4352 :
4353 : 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 : 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 7423 : NEED (1);
4378 7423 : printf ("%*s[%2" PRIuMAX "] %s %" PRIu8 "\n",
4379 : indent, "", (uintmax_t) offset,
4380 7423 : op_name, *((uint8_t *) data));
4381 7423 : ++data;
4382 7423 : --len;
4383 7423 : offset += 2;
4384 7423 : break;
4385 :
4386 : case DW_OP_const2u:
4387 848 : NEED (2);
4388 : // XXX value might be modified by relocation
4389 1696 : printf ("%*s[%2" PRIuMAX "] %s %" PRIu16 "\n",
4390 : indent, "", (uintmax_t) offset,
4391 1696 : op_name, read_2ubyte_unaligned (dbg, data));
4392 848 : CONSUME (2);
4393 848 : data += 2;
4394 848 : offset += 3;
4395 848 : break;
4396 :
4397 : case DW_OP_const4u:
4398 629 : NEED (4);
4399 : // XXX value might be modified by relocation
4400 1258 : printf ("%*s[%2" PRIuMAX "] %s %" PRIu32 "\n",
4401 : indent, "", (uintmax_t) offset,
4402 1258 : op_name, read_4ubyte_unaligned (dbg, data));
4403 629 : CONSUME (4);
4404 629 : data += 4;
4405 629 : offset += 5;
4406 629 : break;
4407 :
4408 : case DW_OP_const8u:
4409 59 : NEED (8);
4410 : // XXX value might be modified by relocation
4411 118 : printf ("%*s[%2" PRIuMAX "] %s %" PRIu64 "\n",
4412 : indent, "", (uintmax_t) offset,
4413 118 : op_name, (uint64_t) read_8ubyte_unaligned (dbg, data));
4414 59 : CONSUME (8);
4415 59 : data += 8;
4416 59 : offset += 9;
4417 59 : break;
4418 :
4419 : case DW_OP_const1s:
4420 1355 : NEED (1);
4421 : // XXX value might be modified by relocation
4422 1355 : printf ("%*s[%2" PRIuMAX "] %s %" PRId8 "\n",
4423 : indent, "", (uintmax_t) offset,
4424 1355 : op_name, *((int8_t *) data));
4425 1355 : ++data;
4426 1355 : --len;
4427 1355 : offset += 2;
4428 1355 : break;
4429 :
4430 : case DW_OP_const2s:
4431 3 : NEED (2);
4432 : // XXX value might be modified by relocation
4433 6 : 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 : 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 : 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 : case DW_OP_piece:
4464 : case DW_OP_regx:
4465 : case DW_OP_plus_uconst:
4466 : case DW_OP_constu:;
4467 5916 : const unsigned char *start = data;
4468 : uint64_t uleb;
4469 5916 : NEED (1);
4470 5916 : get_uleb128 (uleb, data, data + len);
4471 : printf ("%*s[%2" PRIuMAX "] %s %" PRIu64 "\n",
4472 : indent, "", (uintmax_t) offset, op_name, uleb);
4473 5916 : CONSUME (data - start);
4474 5916 : offset += 1 + (data - start);
4475 5916 : break;
4476 :
4477 : 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 : 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 : case DW_OP_bit_piece:
4498 16 : start = data;
4499 : uint64_t uleb2;
4500 16 : NEED (1);
4501 16 : get_uleb128 (uleb, data, data + len);
4502 16 : NEED (1);
4503 16 : get_uleb128 (uleb2, data, data + len);
4504 : printf ("%*s[%2" PRIuMAX "] %s %" PRIu64 ", %" PRIu64 "\n",
4505 : indent, "", (uintmax_t) offset, op_name, uleb, uleb2);
4506 16 : CONSUME (data - start);
4507 16 : offset += 1 + (data - start);
4508 16 : break;
4509 :
4510 : case DW_OP_fbreg:
4511 : case DW_OP_breg0 ... DW_OP_breg31:
4512 : case DW_OP_consts:
4513 64741 : start = data;
4514 : int64_t sleb;
4515 64741 : NEED (1);
4516 64741 : get_sleb128 (sleb, data, data + len);
4517 : printf ("%*s[%2" PRIuMAX "] %s %" PRId64 "\n",
4518 : indent, "", (uintmax_t) offset, op_name, sleb);
4519 64741 : CONSUME (data - start);
4520 64741 : offset += 1 + (data - start);
4521 64741 : break;
4522 :
4523 : case DW_OP_bregx:
4524 0 : start = data;
4525 0 : NEED (1);
4526 0 : get_uleb128 (uleb, data, data + len);
4527 0 : NEED (1);
4528 0 : get_sleb128 (sleb, data, data + len);
4529 : 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 : 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 : case DW_OP_call4:
4546 1 : NEED (4);
4547 2 : 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 : case DW_OP_skip:
4556 : case DW_OP_bra:
4557 138 : NEED (2);
4558 138 : printf ("%*s[%2" PRIuMAX "] %s %" PRIuMAX "\n",
4559 : indent, "", (uintmax_t) offset, op_name,
4560 138 : (uintmax_t) (offset + read_2sbyte_unaligned (dbg, data) + 3));
4561 138 : CONSUME (2);
4562 138 : data += 2;
4563 138 : offset += 3;
4564 138 : break;
4565 :
4566 : case DW_OP_implicit_value:
4567 145 : start = data;
4568 145 : NEED (1);
4569 145 : get_uleb128 (uleb, data, data + len);
4570 : printf ("%*s[%2" PRIuMAX "] %s: ",
4571 : indent, "", (uintmax_t) offset, op_name);
4572 145 : NEED (uleb);
4573 145 : print_block (uleb, data);
4574 145 : data += uleb;
4575 145 : CONSUME (data - start);
4576 145 : offset += 1 + (data - start);
4577 145 : break;
4578 :
4579 : case DW_OP_implicit_pointer:
4580 : case DW_OP_GNU_implicit_pointer:
4581 : /* DIE offset operand. */
4582 3151 : start = data;
4583 3151 : NEED (ref_size);
4584 3151 : if (ref_size != 4 && ref_size != 8)
4585 : goto invalid; /* Cannot be used in CFA. */
4586 3151 : if (ref_size == 4)
4587 3151 : addr = read_4ubyte_unaligned (dbg, data);
4588 : else
4589 0 : addr = read_8ubyte_unaligned (dbg, data);
4590 3151 : data += ref_size;
4591 : /* Byte offset operand. */
4592 3151 : NEED (1);
4593 3151 : get_sleb128 (sleb, data, data + len);
4594 :
4595 3151 : printf ("%*s[%2" PRIuMAX "] %s [%6" PRIxMAX "] %+" PRId64 "\n",
4596 : indent, "", (intmax_t) offset,
4597 : op_name, (uintmax_t) addr, sleb);
4598 3151 : CONSUME (data - start);
4599 3151 : offset += 1 + (data - start);
4600 3151 : break;
4601 :
4602 : case DW_OP_entry_value:
4603 : case DW_OP_GNU_entry_value:
4604 : /* Size plus expression block. */
4605 17269 : start = data;
4606 17269 : NEED (1);
4607 17269 : get_uleb128 (uleb, data, data + len);
4608 : printf ("%*s[%2" PRIuMAX "] %s:\n",
4609 : indent, "", (uintmax_t) offset, op_name);
4610 17269 : NEED (uleb);
4611 17269 : print_ops (dwflmod, dbg, indent + 5, indent + 5, vers,
4612 : addrsize, offset_size, cu, uleb, data);
4613 17269 : data += uleb;
4614 17269 : CONSUME (data - start);
4615 17269 : offset += 1 + (data - start);
4616 17269 : break;
4617 :
4618 : 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 3 : start = data;
4623 3 : NEED (1);
4624 3 : get_uleb128 (uleb, data, data + len);
4625 3 : if (! print_unresolved_addresses && cu != NULL)
4626 3 : uleb += cu->start;
4627 3 : NEED (1);
4628 3 : uint8_t usize = *(uint8_t *) data++;
4629 3 : NEED (usize);
4630 : printf ("%*s[%2" PRIuMAX "] %s [%6" PRIxMAX "] ",
4631 : indent, "", (uintmax_t) offset, op_name, uleb);
4632 3 : print_block (usize, data);
4633 3 : data += usize;
4634 3 : CONSUME (data - start);
4635 3 : offset += 1 + (data - start);
4636 3 : break;
4637 :
4638 : 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 0 : NEED (1);
4646 0 : get_uleb128 (uleb2, data, data + len);
4647 0 : if (! print_unresolved_addresses && cu != NULL)
4648 0 : uleb2 += cu->start;
4649 : 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 : 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 8 : start = data;
4660 8 : NEED (1);
4661 8 : usize = *(uint8_t *) data++;
4662 8 : NEED (1);
4663 8 : get_uleb128 (uleb, data, data + len);
4664 8 : if (! print_unresolved_addresses && cu != NULL)
4665 8 : uleb += cu->start;
4666 8 : printf ("%*s[%2" PRIuMAX "] %s %" PRIu8 " [%6" PRIxMAX "]\n",
4667 : indent, "", (uintmax_t) offset,
4668 : op_name, usize, uleb);
4669 8 : CONSUME (data - start);
4670 8 : offset += 1 + (data - start);
4671 8 : break;
4672 :
4673 : 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 0 : 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 : 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 210 : start = data;
4694 210 : NEED (1);
4695 210 : get_uleb128 (uleb, data, data + len);
4696 210 : if (uleb != 0 && ! print_unresolved_addresses && cu != NULL)
4697 143 : uleb += cu->start;
4698 : printf ("%*s[%2" PRIuMAX "] %s [%6" PRIxMAX "]\n",
4699 : indent, "", (uintmax_t) offset, op_name, uleb);
4700 210 : CONSUME (data - start);
4701 210 : offset += 1 + (data - start);
4702 210 : break;
4703 :
4704 : case DW_OP_GNU_parameter_ref:
4705 : /* 4 byte CU relative reference to the abstract optimized away
4706 : DW_TAG_formal_parameter. */
4707 189 : NEED (4);
4708 189 : uintmax_t param_off = (uintmax_t) read_4ubyte_unaligned (dbg, data);
4709 189 : if (! print_unresolved_addresses && cu != NULL)
4710 189 : param_off += cu->start;
4711 : printf ("%*s[%2" PRIuMAX "] %s [%6" PRIxMAX "]\n",
4712 : indent, "", (uintmax_t) offset, op_name, param_off);
4713 189 : CONSUME (4);
4714 189 : data += 4;
4715 189 : offset += 5;
4716 189 : break;
4717 :
4718 : default:
4719 : /* No Operand. */
4720 : printf ("%*s[%2" PRIuMAX "] %s\n",
4721 : indent, "", (uintmax_t) offset, op_name);
4722 245308 : ++offset;
4723 245308 : break;
4724 : }
4725 :
4726 355295 : indent = indentrest;
4727 355295 : continue;
4728 :
4729 : 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 59670 : 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 59670 : 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 59670 : return base;
4767 : }
4768 :
4769 : static Dwarf_Addr
4770 59670 : listptr_base (struct listptr *p)
4771 : {
4772 119340 : Dwarf_Die cu = CUDIE (p->cu);
4773 59670 : return cudie_base (&cu);
4774 : }
4775 :
4776 : static int
4777 320677 : compare_listptr (const void *a, const void *b, void *arg)
4778 : {
4779 320677 : const char *name = arg;
4780 326373 : struct listptr *p1 = (void *) a;
4781 326373 : struct listptr *p2 = (void *) b;
4782 :
4783 320677 : if (p1->offset < p2->offset)
4784 : return -1;
4785 15888 : if (p1->offset > p2->offset)
4786 : return 1;
4787 :
4788 5696 : if (!p1->warned && !p2->warned)
4789 : {
4790 5696 : 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 5696 : 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 11392 : 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 5696 : 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 750 : free (table->table);
4843 750 : table->table = NULL;
4844 750 : table->n = table->alloc = 0;
4845 : }
4846 :
4847 : /* Returns false if offset doesn't fit. See struct listptr. */
4848 : static bool
4849 53668 : 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 53668 : if (print_debug_sections & section)
4854 : {
4855 53573 : if (table->n == table->alloc)
4856 : {
4857 158 : if (table->alloc == 0)
4858 70 : table->alloc = 128;
4859 : else
4860 88 : table->alloc *= 2;
4861 158 : table->table = xrealloc (table->table,
4862 158 : table->alloc * sizeof table->table[0]);
4863 : }
4864 :
4865 53573 : struct listptr *p = &table->table[table->n++];
4866 :
4867 107146 : *p = (struct listptr)
4868 : {
4869 53573 : .addr64 = address_size == 8,
4870 53573 : .dwarf64 = offset_size == 8,
4871 : .offset = offset,
4872 : .cu = cu,
4873 : .attr = attr
4874 : };
4875 :
4876 53573 : 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 48272 : 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 48272 : if (table->n == 0)
4901 : return false;
4902 :
4903 101755 : while (*idxp < table->n && table->table[*idxp].offset < (Dwarf_Off) offset)
4904 53483 : ++*idxp;
4905 :
4906 96544 : struct listptr *p = &table->table[*idxp];
4907 :
4908 48272 : if (*idxp == table->n
4909 48272 : || 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 48272 : if (p->offset != (Dwarf_Off) offset)
4918 : {
4919 0 : *readp += p->offset - offset;
4920 0 : printf (gettext (" [%6tx] <UNUSED GARBAGE> ... %" PRIu64 " bytes ...\n"),
4921 0 : offset, (Dwarf_Off) p->offset - offset);
4922 : return true;
4923 : }
4924 :
4925 48272 : if (address_sizep != NULL)
4926 48272 : *address_sizep = listptr_address_size (p);
4927 48272 : if (offset_sizep != NULL)
4928 38841 : *offset_sizep = listptr_offset_size (p);
4929 48272 : if (base != NULL)
4930 48272 : *base = listptr_base (p);
4931 48272 : if (cu != NULL)
4932 48272 : *cu = p->cu;
4933 48272 : if (attr != NULL)
4934 38841 : *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 68 : Ebl *ebl, GElf_Ehdr *ehdr,
4993 34 : 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 170 : printf (gettext ("\nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"
4999 : " [ Code]\n"),
5000 : elf_ndxscn (scn), section_name (ebl, ehdr, shdr),
5001 : (uint64_t) shdr->sh_offset);
5002 :
5003 34 : Dwarf_Off offset = 0;
5004 34 : while (offset < sh_size)
5005 : {
5006 1075 : printf (gettext ("\nAbbreviation section at offset %" PRIu64 ":\n"),
5007 : offset);
5008 :
5009 : while (1)
5010 : {
5011 : size_t length;
5012 : Dwarf_Abbrev abbrev;
5013 :
5014 57229 : int res = dwarf_offabbrev (dbg, offset, &length, &abbrev);
5015 57229 : if (res != 0)
5016 : {
5017 1075 : if (unlikely (res < 0))
5018 : {
5019 0 : printf (gettext ("\
5020 : *** error while reading abbreviation: %s\n"),
5021 : dwarf_errmsg (-1));
5022 34 : return;
5023 : }
5024 :
5025 : /* This is the NUL byte at the end of the section. */
5026 1075 : ++offset;
5027 1075 : break;
5028 : }
5029 :
5030 : /* We know these calls can never fail. */
5031 56154 : unsigned int code = dwarf_getabbrevcode (&abbrev);
5032 56154 : unsigned int tag = dwarf_getabbrevtag (&abbrev);
5033 56154 : int has_children = dwarf_abbrevhaschildren (&abbrev);
5034 :
5035 56154 : 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 56154 : size_t cnt = 0;
5042 : unsigned int name;
5043 : unsigned int form;
5044 : Dwarf_Sword data;
5045 : Dwarf_Off enoffset;
5046 341531 : while (dwarf_getabbrevattr_data (&abbrev, cnt, &name, &form,
5047 : &data, &enoffset) == 0)
5048 : {
5049 229223 : printf (" attr: %s, form: %s",
5050 : dwarf_attr_name (name), dwarf_form_name (form));
5051 229223 : if (form == DW_FORM_implicit_const)
5052 0 : printf (" (%" PRId64 ")", data);
5053 229223 : printf (", offset: %#" PRIx64 "\n", (uint64_t) enoffset);
5054 229223 : ++cnt;
5055 : }
5056 :
5057 56154 : offset += length;
5058 56154 : }
5059 : }
5060 : }
5061 :
5062 :
5063 : static void
5064 2 : print_debug_addr_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
5065 4 : Ebl *ebl, GElf_Ehdr *ehdr,
5066 2 : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
5067 : {
5068 10 : printf (gettext ("\
5069 : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
5070 : elf_ndxscn (scn), section_name (ebl, ehdr, shdr),
5071 : (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 2 : 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 : 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 : 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 4 : listptr = get_listptr (&known_addrbases, idx);
5143 2 : 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 : 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 : 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 : printf ("\n");
5181 2 : printf (gettext (" Length: %8" PRIu64 "\n"),
5182 : unit_length);
5183 2 : printf (gettext (" DWARF version: %8" PRIu16 "\n"), version);
5184 2 : printf (gettext (" Address size: %8" PRIu64 "\n"),
5185 : (uint64_t) address_size);
5186 2 : 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 4 : 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 : invalid_data:
5199 : error (0, 0, "Invalid data");
5200 0 : return;
5201 : }
5202 0 : unit_length = read_8ubyte_unaligned_inc (dbg, readp);
5203 : }
5204 : printf ("\n");
5205 2 : 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 2 : 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 2 : 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 2 : printf (gettext (" Segment size: %8" PRIu64 "\n"),
5240 : (uint64_t) segment_size);
5241 : 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 144 : Dwarf_Addr addr = read_addr_unaligned_inc (address_size, dbg,
5271 : readp);
5272 72 : printf (" [%*u] ", digits, index++);
5273 72 : print_dwarf_addr (dwflmod, address_size, addr, addr);
5274 : printf ("\n");
5275 : }
5276 : 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 : next_unit:
5283 4 : 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 3 : print_decoded_aranges_section (Ebl *ebl, GElf_Ehdr *ehdr, Elf_Scn *scn,
5292 1 : 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 5 : 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 : (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 74 : Ebl *ebl, GElf_Ehdr *ehdr, Elf_Scn *scn,
5359 37 : 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 185 : printf (gettext ("\
5378 : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
5379 : elf_ndxscn (scn), section_name (ebl, ehdr, shdr),
5380 : (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 1146 : while (readp < readendp)
5386 : {
5387 1072 : const unsigned char *hdrstart = readp;
5388 1072 : size_t start_offset = hdrstart - (const unsigned char *) data->d_buf;
5389 :
5390 1072 : printf (gettext ("\nTable at offset %zu:\n"), start_offset);
5391 1072 : if (readp + 4 > readendp)
5392 : {
5393 : 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 2124 : Dwarf_Word length = read_4ubyte_unaligned_inc (dbg, readp);
5400 1072 : unsigned int length_bytes = 4;
5401 1072 : 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 1072 : const unsigned char *nexthdr = readp + length;
5410 1072 : printf (gettext ("\n Length: %6" PRIu64 "\n"),
5411 : (uint64_t) length);
5412 :
5413 1072 : if (unlikely (length > (size_t) (readendp - readp)))
5414 : goto invalid_data;
5415 :
5416 1072 : if (length == 0)
5417 0 : continue;
5418 :
5419 1072 : if (readp + 2 > readendp)
5420 : goto invalid_data;
5421 1072 : uint_fast16_t version = read_2ubyte_unaligned_inc (dbg, readp);
5422 1072 : printf (gettext (" DWARF version: %6" PRIuFAST16 "\n"),
5423 : version);
5424 1072 : if (version != 2)
5425 : {
5426 0 : error (0, 0, gettext ("unsupported aranges version"));
5427 : goto next_table;
5428 : }
5429 :
5430 : Dwarf_Word offset;
5431 1072 : if (readp + length_bytes > readendp)
5432 : goto invalid_data;
5433 1072 : if (length_bytes == 8)
5434 0 : offset = read_8ubyte_unaligned_inc (dbg, readp);
5435 : else
5436 2124 : offset = read_4ubyte_unaligned_inc (dbg, readp);
5437 1072 : printf (gettext (" CU offset: %6" PRIx64 "\n"),
5438 : (uint64_t) offset);
5439 :
5440 1072 : if (readp + 1 > readendp)
5441 : goto invalid_data;
5442 1072 : unsigned int address_size = *readp++;
5443 1072 : printf (gettext (" Address size: %6" PRIu64 "\n"),
5444 : (uint64_t) address_size);
5445 1072 : if (address_size != 4 && address_size != 8)
5446 : {
5447 0 : error (0, 0, gettext ("unsupported address size"));
5448 : goto next_table;
5449 : }
5450 :
5451 1072 : unsigned int segment_size = *readp++;
5452 1072 : printf (gettext (" Segment size: %6" PRIu64 "\n\n"),
5453 : (uint64_t) segment_size);
5454 1072 : 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 2144 : readp += ((2 * address_size - ((readp - hdrstart) % (2 * address_size)))
5462 1072 : % (2 * address_size));
5463 :
5464 3989 : while (readp < nexthdr)
5465 : {
5466 : Dwarf_Word range_address;
5467 : Dwarf_Word range_length;
5468 2917 : Dwarf_Word segment = 0;
5469 2917 : if (readp + 2 * address_size + segment_size > readendp)
5470 : goto invalid_data;
5471 2917 : if (address_size == 4)
5472 : {
5473 52 : range_address = read_4ubyte_unaligned_inc (dbg, readp);
5474 52 : range_length = read_4ubyte_unaligned_inc (dbg, readp);
5475 : }
5476 : else
5477 : {
5478 5746 : range_address = read_8ubyte_unaligned_inc (dbg, readp);
5479 5746 : range_length = read_8ubyte_unaligned_inc (dbg, readp);
5480 : }
5481 :
5482 2917 : if (segment_size == 4)
5483 0 : segment = read_4ubyte_unaligned_inc (dbg, readp);
5484 2917 : else if (segment_size == 8)
5485 0 : segment = read_8ubyte_unaligned_inc (dbg, readp);
5486 :
5487 2917 : if (range_address == 0 && range_length == 0 && segment == 0)
5488 : break;
5489 :
5490 : printf (" ");
5491 1845 : print_dwarf_addr (dwflmod, address_size, range_address,
5492 : range_address);
5493 : printf ("..");
5494 1845 : print_dwarf_addr (dwflmod, address_size,
5495 1845 : range_address + range_length - 1,
5496 : range_length);
5497 1845 : if (segment_size != 0)
5498 : printf (" (%" PRIx64 ")\n", (uint64_t) segment);
5499 : else
5500 : printf ("\n");
5501 : }
5502 :
5503 : next_table:
5504 1072 : 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 4 : Ebl *ebl, GElf_Ehdr *ehdr,
5538 2 : Elf_Scn *scn, GElf_Shdr *shdr,
5539 : Dwarf *dbg __attribute__((unused)))
5540 : {
5541 10 : printf (gettext ("\
5542 : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
5543 : elf_ndxscn (scn), section_name (ebl, ehdr, shdr),
5544 : (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 2 : 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 : 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 2 : printf (gettext ("Table at Offset 0x%" PRIx64 ":\n\n"),
5574 : (uint64_t) offset);
5575 :
5576 4 : 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 2 : 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 2 : 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 2 : 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 2 : 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 4 : uint32_t offset_entry_count = read_4ubyte_unaligned_inc (dbg, readp);
5628 2 : 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 : printf ("\n");
5650 : }
5651 : else
5652 0 : printf (gettext (" Not associated with a CU.\n"));
5653 :
5654 : 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 : error (0, 0,
5663 0 : gettext ("too many offset entries for unit length"));
5664 0 : offset_entry_count = max_entries;
5665 : }
5666 :
5667 1 : 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 : printf (" [%6" PRIu32 "] ", idx);
5673 2 : if (offset_size == 4)
5674 : {
5675 4 : 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 8 : 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 14 : printf (" %s", dwarf_range_list_encoding_name (kind));
5708 14 : switch (kind)
5709 : {
5710 : case DW_RLE_end_of_list:
5711 4 : start_of_list = true;
5712 : printf ("\n\n");
5713 : break;
5714 :
5715 : case DW_RLE_base_addressx:
5716 0 : if ((uint64_t) (nexthdr - readp) < 1)
5717 : {
5718 : 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 : 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 : printf (" ");
5732 0 : print_dwarf_addr (dwflmod, address_size, addr, addr);
5733 : printf ("\n");
5734 : }
5735 : }
5736 : break;
5737 :
5738 : 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 : 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 : printf (" ???..\n");
5754 : printf (" ???\n");
5755 : }
5756 : else
5757 : {
5758 : printf (" ");
5759 0 : print_dwarf_addr (dwflmod, address_size, addr1, addr1);
5760 : printf ("..\n ");
5761 0 : print_dwarf_addr (dwflmod, address_size,
5762 : addr2 - 1, addr2);
5763 : printf ("\n");
5764 : }
5765 : }
5766 : break;
5767 :
5768 : 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 : 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 : printf (" ???..\n");
5783 : printf (" ???\n");
5784 : }
5785 : else
5786 : {
5787 0 : addr2 = addr1 + op2;
5788 : printf (" ");
5789 0 : print_dwarf_addr (dwflmod, address_size, addr1, addr1);
5790 : printf ("..\n ");
5791 0 : print_dwarf_addr (dwflmod, address_size,
5792 : addr2 - 1, addr2);
5793 : printf ("\n");
5794 : }
5795 : }
5796 : break;
5797 :
5798 : 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 : printf (" %" PRIx64 ", %" PRIx64 "\n", op1, op2);
5806 4 : if (! print_unresolved_addresses)
5807 : {
5808 4 : op1 += base;
5809 4 : op2 += base;
5810 : printf (" ");
5811 4 : print_dwarf_addr (dwflmod, address_size, op1, op1);
5812 : printf ("..\n ");
5813 4 : print_dwarf_addr (dwflmod, address_size, op2 - 1, op2);
5814 : printf ("\n");
5815 : }
5816 : break;
5817 :
5818 : 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 4 : op1 = read_8ubyte_unaligned_inc (dbg, readp);
5830 : }
5831 2 : base = op1;
5832 : printf (" 0x%" PRIx64 "\n", base);
5833 2 : if (! print_unresolved_addresses)
5834 : {
5835 : printf (" ");
5836 2 : print_dwarf_addr (dwflmod, address_size, base, base);
5837 : printf ("\n");
5838 : }
5839 : break;
5840 :
5841 : 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 : printf (" 0x%" PRIx64 "..0x%" PRIx64 "\n", op1, op2);
5857 0 : if (! print_unresolved_addresses)
5858 : {
5859 : printf (" ");
5860 0 : print_dwarf_addr (dwflmod, address_size, op1, op1);
5861 : printf ("..\n ");
5862 0 : print_dwarf_addr (dwflmod, address_size, op2 - 1, op2);
5863 : printf ("\n");
5864 : }
5865 : break;
5866 :
5867 : 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 8 : 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 : printf (" 0x%" PRIx64 ", %" PRIx64 "\n", op1, op2);
5884 4 : if (! print_unresolved_addresses)
5885 : {
5886 4 : op2 = op1 + op2;
5887 : printf (" ");
5888 4 : print_dwarf_addr (dwflmod, address_size, op1, op1);
5889 : 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 : 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 56 : Ebl *ebl, GElf_Ehdr *ehdr,
5914 28 : 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 28 : return;
5924 : }
5925 :
5926 140 : printf (gettext ("\
5927 : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
5928 : elf_ndxscn (scn), section_name (ebl, ehdr, shdr),
5929 : (uint64_t) shdr->sh_offset);
5930 :
5931 28 : 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 37440 : while (readp < endp)
5942 : {
5943 37384 : ptrdiff_t offset = readp - (unsigned char *) data->d_buf;
5944 37384 : Dwarf_CU *cu = last_cu;
5945 :
5946 37384 : 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 37384 : if (last_cu != cu)
5952 : {
5953 : Dwarf_Die cudie;
5954 884 : 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 884 : printf (gettext ("\n CU [%6" PRIx64 "] base: "),
5960 : dwarf_dieoffset (&cudie));
5961 884 : print_dwarf_addr (dwflmod, address_size, base, base);
5962 : printf ("\n");
5963 : }
5964 37384 : last_cu = cu;
5965 :
5966 37384 : 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 37384 : if (address_size == 8)
5975 : {
5976 74768 : begin = read_8ubyte_unaligned_inc (dbg, readp);
5977 74768 : 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 0 : begin = (Dwarf_Addr) -1l;
5985 : }
5986 :
5987 37384 : 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 : printf ("\n");
5992 0 : base = end;
5993 : }
5994 37384 : else if (begin == 0 && end == 0) /* End of list entry. */
5995 : {
5996 9431 : 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 27953 : if (first) /* First address range entry in a list. */
6004 : printf (" [%6tx] ", offset);
6005 : else
6006 : printf (" ");
6007 :
6008 : printf ("range %" PRIx64 ", %" PRIx64 "\n", begin, end);
6009 27953 : if (! print_unresolved_addresses)
6010 : {
6011 : printf (" ");
6012 27943 : print_dwarf_addr (dwflmod, address_size, base + begin,
6013 : base + begin);
6014 : printf ("..\n ");
6015 27943 : print_dwarf_addr (dwflmod, address_size,
6016 27943 : 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 7605 : 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 7605 : ssize_t n = ebl_register_info (ebl, regno, name, REGNAMESZ, &pfx, &set,
6034 : bits ?: &ignore, type ?: &ignore);
6035 7605 : 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 7605 : if (bits != NULL && *bits <= 0)
6050 0 : *bits = loc != NULL ? loc->bits : 0;
6051 7605 : if (type != NULL && *type == DW_ATE_void)
6052 0 : *type = DW_ATE_unsigned;
6053 :
6054 : }
6055 7605 : 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 : ? DW_EH_PE_udata4 : DW_EH_PE_udata8;
6065 :
6066 28 : switch (encoding & 0xf)
6067 : {
6068 : case DW_EH_PE_uleb128:
6069 0 : get_uleb128 (*res, readp, endp);
6070 : break;
6071 : case DW_EH_PE_sleb128:
6072 0 : get_sleb128 (*res, readp, endp);
6073 : break;
6074 : 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 : case DW_EH_PE_udata4:
6080 14 : if (readp + 4 > endp)
6081 : goto invalid;
6082 28 : *res = read_4ubyte_unaligned_inc (dbg, readp);
6083 : break;
6084 : 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 : 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 : 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 : 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 : invalid:
6106 : error (1, 0,
6107 0 : gettext ("invalid encoding"));
6108 : }
6109 :
6110 28 : return readp;
6111 : }
6112 :
6113 :
6114 : static void
6115 3896 : 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 6819 : register_info (ebl, regno, NULL, regnamebuf, NULL, NULL);
6126 : return regnamebuf;
6127 : }
6128 :
6129 3896 : puts ("\n Program:");
6130 3896 : Dwarf_Word pc = vma_base;
6131 79605 : while (readp < endp)
6132 : {
6133 71813 : unsigned int opcode = *readp++;
6134 :
6135 71813 : if (opcode < DW_CFA_advance_loc)
6136 : /* Extended opcode. */
6137 45095 : switch (opcode)
6138 : {
6139 : uint64_t op1;
6140 : int64_t sop1;
6141 : uint64_t op2;
6142 : int64_t sop2;
6143 :
6144 : case DW_CFA_nop:
6145 18663 : puts (" nop");
6146 63758 : break;
6147 : 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 : case DW_CFA_advance_loc1:
6155 1374 : if ((uint64_t) (endp - readp) < 1)
6156 : goto invalid;
6157 1374 : printf (" advance_loc1 %u to %#" PRIx64 "\n",
6158 1374 : *readp, pc += *readp * code_align);
6159 1374 : ++readp;
6160 1374 : break;
6161 : case DW_CFA_advance_loc2:
6162 513 : if ((uint64_t) (endp - readp) < 2)
6163 : goto invalid;
6164 513 : op1 = read_2ubyte_unaligned_inc (dbg, readp);
6165 513 : printf (" advance_loc2 %" PRIu64 " to %#" PRIx64 "\n",
6166 513 : op1, pc += op1 * code_align);
6167 : break;
6168 : case DW_CFA_advance_loc4:
6169 24 : if ((uint64_t) (endp - readp) < 4)
6170 : goto invalid;
6171 24 : 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 : 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 : 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 : 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 : 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 : 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 : case DW_CFA_remember_state:
6216 2543 : puts (" remember_state");
6217 2543 : break;
6218 : case DW_CFA_restore_state:
6219 2543 : puts (" restore_state");
6220 2543 : break;
6221 : case DW_CFA_def_cfa:
6222 116 : if ((uint64_t) (endp - readp) < 1)
6223 : goto invalid;
6224 116 : get_uleb128 (op1, readp, endp);
6225 116 : if ((uint64_t) (endp - readp) < 1)
6226 : goto invalid;
6227 116 : get_uleb128 (op2, readp, endp);
6228 232 : printf (" def_cfa r%" PRIu64 " (%s) at offset %" PRIu64 "\n",
6229 : op1, regname (op1), op2);
6230 : break;
6231 : case DW_CFA_def_cfa_register:
6232 47 : if ((uint64_t) (endp - readp) < 1)
6233 : goto invalid;
6234 47 : get_uleb128 (op1, readp, endp);
6235 94 : printf (" def_cfa_register r%" PRIu64 " (%s)\n",
6236 : op1, regname (op1));
6237 : break;
6238 : case DW_CFA_def_cfa_offset:
6239 19234 : if ((uint64_t) (endp - readp) < 1)
6240 : goto invalid;
6241 19234 : get_uleb128 (op1, readp, endp);
6242 19234 : printf (" def_cfa_offset %" PRIu64 "\n", op1);
6243 : break;
6244 : 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 12 : printf (" def_cfa_expression %" PRIu64 "\n", op1);
6249 12 : if ((uint64_t) (endp - readp) < op1)
6250 : {
6251 : invalid:
6252 0 : fputs (gettext (" <INVALID DATA>\n"), stdout);
6253 3896 : 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 : 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 : 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 : 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 : 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 : 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 : 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 : 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 : 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 : case DW_CFA_GNU_window_save:
6344 0 : puts (" GNU_window_save");
6345 0 : break;
6346 : 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 : default:
6353 : printf (" ??? (%u)\n", opcode);
6354 : break;
6355 : }
6356 26718 : else if (opcode < DW_CFA_offset)
6357 20088 : printf (" advance_loc %u to %#" PRIx64 "\n",
6358 20088 : opcode & 0x3f, pc += (opcode & 0x3f) * code_align);
6359 6630 : else if (opcode < DW_CFA_restore)
6360 : {
6361 : uint64_t offset;
6362 6250 : if ((uint64_t) (endp - readp) < 1)
6363 : goto invalid;
6364 6250 : get_uleb128 (offset, readp, endp);
6365 12500 : printf (" offset r%u (%s) at cfa%+" PRId64 "\n",
6366 : opcode & 0x3f, regname (opcode & 0x3f), offset * data_align);
6367 : }
6368 : else
6369 760 : printf (" restore r%u (%s)\n",
6370 : opcode & 0x3f, regname (opcode & 0x3f));
6371 : }
6372 : }
6373 :
6374 :
6375 : static unsigned int
6376 3834 : encoded_ptr_size (int encoding, unsigned int ptr_size)
6377 : {
6378 3834 : switch (encoding & 7)
6379 : {
6380 : case DW_EH_PE_udata4:
6381 : return 4;
6382 : case DW_EH_PE_udata8:
6383 0 : return 8;
6384 : 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 : case DW_EH_PE_absptr:
6400 0 : fputs ("absptr", stdout);
6401 0 : break;
6402 : case DW_EH_PE_uleb128:
6403 0 : fputs ("uleb128", stdout);
6404 0 : break;
6405 : case DW_EH_PE_udata2:
6406 0 : fputs ("udata2", stdout);
6407 0 : break;
6408 : case DW_EH_PE_udata4:
6409 14 : fputs ("udata4", stdout);
6410 14 : break;
6411 : case DW_EH_PE_udata8:
6412 0 : fputs ("udata8", stdout);
6413 0 : break;
6414 : case DW_EH_PE_sleb128:
6415 0 : fputs ("sleb128", stdout);
6416 0 : break;
6417 : case DW_EH_PE_sdata2:
6418 0 : fputs ("sdata2", stdout);
6419 0 : break;
6420 : case DW_EH_PE_sdata4:
6421 64 : fputs ("sdata4", stdout);
6422 64 : break;
6423 : 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 : case DW_EH_PE_pcrel:
6441 50 : fputs ("pcrel", stdout);
6442 50 : break;
6443 : case DW_EH_PE_textrel:
6444 0 : fputs ("textrel", stdout);
6445 0 : break;
6446 : case DW_EH_PE_datarel:
6447 14 : fputs ("datarel", stdout);
6448 14 : break;
6449 : case DW_EH_PE_funcrel:
6450 0 : fputs ("funcrel", stdout);
6451 0 : break;
6452 : 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 : 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 : 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 : 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 3990 : while (readp < dataend)
6547 : {
6548 3910 : if (unlikely (readp + 4 > dataend))
6549 : {
6550 : 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 3910 : ptrdiff_t offset = readp - (unsigned char *) data->d_buf;
6559 :
6560 7788 : Dwarf_Word unit_length = read_4ubyte_unaligned_inc (dbg, readp);
6561 3910 : unsigned int length = 4;
6562 3910 : 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 3910 : if (unlikely (unit_length == 0))
6572 : {
6573 14 : printf (gettext ("\n [%6tx] Zero terminator\n"), offset);
6574 14 : continue;
6575 : }
6576 :
6577 3896 : Dwarf_Word maxsize = dataend - readp;
6578 3896 : if (unlikely (unit_length > maxsize))
6579 : goto invalid_data;
6580 :
6581 3896 : unsigned int ptr_size = ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 4 : 8;
6582 :
6583 3896 : ptrdiff_t start = readp - (unsigned char *) data->d_buf;
6584 3896 : const unsigned char *const cieend = readp + unit_length;
6585 3896 : if (unlikely (cieend > dataend || readp + 8 > dataend))
6586 : goto invalid_data;
6587 :
6588 : Dwarf_Off cie_id;
6589 3896 : if (length == 4)
6590 : {
6591 7760 : cie_id = read_4ubyte_unaligned_inc (dbg, readp);
6592 3896 : 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 3896 : uint_fast8_t version = 2;
6599 : unsigned int code_alignment_factor;
6600 : int data_alignment_factor;
6601 3896 : unsigned int fde_encoding = 0;
6602 3896 : unsigned int lsda_encoding = 0;
6603 3896 : Dwarf_Word initial_location = 0;
6604 3896 : Dwarf_Word vma_base = 0;
6605 :
6606 3896 : 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 62 : 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 : 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 36 : 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 : putchar ('(');
6705 0 : print_encoding (encoding);
6706 : putchar (' ');
6707 0 : switch (encoding & 0xf)
6708 : {
6709 : 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 : 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 3834 : while (cie != NULL)
6744 7668 : if (is_eh_frame
6745 3806 : ? ((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 3834 : if (unlikely (cie == NULL))
6751 : {
6752 0 : puts ("invalid CIE reference in FDE");
6753 0 : return;
6754 : }
6755 :
6756 : /* Initialize from CIE data. */
6757 3834 : fde_encoding = cie->fde_encoding;
6758 3834 : lsda_encoding = cie->lsda_encoding;
6759 3834 : ptr_size = encoded_ptr_size (fde_encoding, cie->address_size);
6760 3834 : code_alignment_factor = cie->code_alignment_factor;
6761 3834 : data_alignment_factor = cie->data_alignment_factor;
6762 :
6763 3834 : const unsigned char *base = readp;
6764 : // XXX There are sometimes relocations for this value
6765 7652 : initial_location = read_addr_unaligned_inc (ptr_size, dbg, readp);
6766 3834 : Dwarf_Word address_range
6767 7652 : = 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 3834 : Dwarf_Addr pc_start = initial_location;
6775 3834 : if (ptr_size == 4)
6776 3804 : pc_start = (uint64_t) (int32_t) pc_start;
6777 3834 : if ((fde_encoding & 0x70) == DW_EH_PE_pcrel)
6778 7600 : pc_start += ((uint64_t) shdr->sh_addr
6779 3800 : + (base - (const unsigned char *) data->d_buf)
6780 3800 : - bias);
6781 :
6782 3834 : 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 3834 : print_dwarf_addr (dwflmod, cie->address_size,
6788 : pc_start, initial_location);
6789 3834 : if ((fde_encoding & 0x70) == DW_EH_PE_pcrel)
6790 : {
6791 7600 : vma_base = (((uint64_t) shdr->sh_offset
6792 3800 : + (base - (const unsigned char *) data->d_buf)
6793 3800 : + (uint64_t) initial_location)
6794 : & (ptr_size == 4
6795 : ? UINT64_C (0xffffffff)
6796 3800 : : UINT64_C (0xffffffffffffffff)));
6797 3800 : printf (gettext (" (offset: %#" PRIx64 ")"),
6798 : (uint64_t) vma_base);
6799 : }
6800 :
6801 : printf ("\n address_range: %#" PRIx64,
6802 : (uint64_t) address_range);
6803 3834 : if ((fde_encoding & 0x70) == DW_EH_PE_pcrel)
6804 3800 : printf (gettext (" (end offset: %#" PRIx64 ")"),
6805 3800 : ((uint64_t) vma_base + (uint64_t) address_range)
6806 : & (ptr_size == 4
6807 : ? UINT64_C (0xffffffff)
6808 3800 : : UINT64_C (0xffffffffffffffff)));
6809 : putchar ('\n');
6810 :
6811 3834 : if (cie->augmentation[0] == 'z')
6812 : {
6813 : unsigned int augmentationlen;
6814 3800 : if (cieend - readp < 1)
6815 : goto invalid_data;
6816 3800 : get_uleb128 (augmentationlen, readp, cieend);
6817 :
6818 3800 : 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 3800 : 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 3800 : readp += augmentationlen;
6857 : }
6858 : }
6859 :
6860 : /* Handle the initialization instructions. */
6861 3896 : 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 3896 : print_cfa_program (readp, cieend, vma_base, code_alignment_factor,
6865 : data_alignment_factor, version, ptr_size,
6866 : fde_encoding, dwflmod, ebl, dbg);
6867 3896 : readp = cieend;
6868 : }
6869 : }
6870 :
6871 :
6872 : /* Returns the signedness (or false if it cannot be determined) and
6873 : the byte size (or zero if it cannot be gotten) of the given DIE
6874 : DW_AT_type attribute. Uses dwarf_peel_type and dwarf_aggregate_size. */
6875 : static void
6876 98621 : die_type_sign_bytes (Dwarf_Die *die, bool *is_signed, int *bytes)
6877 : {
6878 : Dwarf_Attribute attr;
6879 : Dwarf_Die type;
6880 :
6881 98621 : *bytes = 0;
6882 98621 : *is_signed = false;
6883 :
6884 98621 : if (dwarf_peel_type (dwarf_formref_die (dwarf_attr_integrate (die,
6885 : DW_AT_type,
6886 : &attr), &type),
6887 : &type) == 0)
6888 : {
6889 : Dwarf_Word val;
6890 590 : *is_signed = (dwarf_formudata (dwarf_attr (&type, DW_AT_encoding,
6891 : &attr), &val) == 0
6892 295 : && (val == DW_ATE_signed || val == DW_ATE_signed_char));
6893 :
6894 295 : if (dwarf_aggregate_size (&type, &val) == 0)
6895 295 : *bytes = val;
6896 : }
6897 98621 : }
6898 :
6899 : struct attrcb_args
6900 : {
6901 : Dwfl_Module *dwflmod;
6902 : Dwarf *dbg;
6903 : Dwarf_Die *die;
6904 : int level;
6905 : bool silent;
6906 : bool is_split;
6907 : unsigned int version;
6908 : unsigned int addrsize;
6909 : unsigned int offset_size;
6910 : struct Dwarf_CU *cu;
6911 : };
6912 :
6913 :
6914 : static int
6915 2385924 : attr_callback (Dwarf_Attribute *attrp, void *arg)
6916 : {
6917 2385924 : struct attrcb_args *cbargs = (struct attrcb_args *) arg;
6918 2385924 : const int level = cbargs->level;
6919 2385924 : Dwarf_Die *die = cbargs->die;
6920 2385924 : bool is_split = cbargs->is_split;
6921 :
6922 2385924 : unsigned int attr = dwarf_whatattr (attrp);
6923 2385924 : if (unlikely (attr == 0))
6924 : {
6925 0 : if (!cbargs->silent)
6926 0 : error (0, 0, gettext ("DIE [%" PRIx64 "] "
6927 : "cannot get attribute code: %s"),
6928 : dwarf_dieoffset (die), dwarf_errmsg (-1));
6929 : return DWARF_CB_ABORT;
6930 : }
6931 :
6932 2385924 : unsigned int form = dwarf_whatform (attrp);
6933 2385924 : if (unlikely (form == 0))
6934 : {
6935 0 : if (!cbargs->silent)
6936 0 : error (0, 0, gettext ("DIE [%" PRIx64 "] "
6937 : "cannot get attribute form: %s"),
6938 : dwarf_dieoffset (die), dwarf_errmsg (-1));
6939 : return DWARF_CB_ABORT;
6940 : }
6941 :
6942 2385924 : switch (form)
6943 : {
6944 : case DW_FORM_addr:
6945 : case DW_FORM_addrx:
6946 : case DW_FORM_addrx1:
6947 : case DW_FORM_addrx2:
6948 : case DW_FORM_addrx3:
6949 : case DW_FORM_addrx4:
6950 : case DW_FORM_GNU_addr_index:
6951 39708 : if (!cbargs->silent)
6952 : {
6953 : Dwarf_Addr addr;
6954 39445 : if (unlikely (dwarf_formaddr (attrp, &addr) != 0))
6955 : {
6956 : attrval_out:
6957 0 : if (!cbargs->silent)
6958 0 : error (0, 0, gettext ("DIE [%" PRIx64 "] "
6959 : "cannot get attribute '%s' (%s) value: "
6960 : "%s"),
6961 : dwarf_dieoffset (die),
6962 : dwarf_attr_name (attr),
6963 : dwarf_form_name (form),
6964 : dwarf_errmsg (-1));
6965 : /* Don't ABORT, it might be other attributes can be resolved. */
6966 0 : return DWARF_CB_OK;
6967 : }
6968 39445 : if (form != DW_FORM_addr )
6969 : {
6970 : Dwarf_Word index;
6971 8 : if (dwarf_formudata (attrp, &index) != 0)
6972 : goto attrval_out;
6973 8 : printf (" %*s%-20s (%s) [%" PRIx64 "] ",
6974 : (int) (level * 2), "", dwarf_attr_name (attr),
6975 : dwarf_form_name (form), index);
6976 : }
6977 : else
6978 39437 : printf (" %*s%-20s (%s) ",
6979 : (int) (level * 2), "", dwarf_attr_name (attr),
6980 : dwarf_form_name (form));
6981 39445 : print_dwarf_addr (cbargs->dwflmod, cbargs->addrsize, addr, addr);
6982 : printf ("\n");
6983 : }
6984 2127191 : break;
6985 :
6986 : case DW_FORM_indirect:
6987 : case DW_FORM_strp:
6988 : case DW_FORM_line_strp:
6989 : case DW_FORM_strx:
6990 : case DW_FORM_strx1:
6991 : case DW_FORM_strx2:
6992 : case DW_FORM_strx3:
6993 : case DW_FORM_strx4:
6994 : case DW_FORM_string:
6995 : case DW_FORM_GNU_strp_alt:
6996 : case DW_FORM_GNU_str_index:
6997 440750 : if (cbargs->silent)
6998 : break;
6999 440035 : const char *str = dwarf_formstring (attrp);
7000 440035 : if (unlikely (str == NULL))
7001 : goto attrval_out;
7002 440035 : printf (" %*s%-20s (%s) \"%s\"\n",
7003 : (int) (level * 2), "", dwarf_attr_name (attr),
7004 : dwarf_form_name (form), str);
7005 : break;
7006 :
7007 : case DW_FORM_ref_addr:
7008 : case DW_FORM_ref_udata:
7009 : case DW_FORM_ref8:
7010 : case DW_FORM_ref4:
7011 : case DW_FORM_ref2:
7012 : case DW_FORM_ref1:
7013 : case DW_FORM_GNU_ref_alt:
7014 : case DW_FORM_ref_sup4:
7015 : case DW_FORM_ref_sup8:
7016 570011 : if (cbargs->silent)
7017 : break;
7018 : Dwarf_Die ref;
7019 569248 : if (unlikely (dwarf_formref_die (attrp, &ref) == NULL))
7020 : goto attrval_out;
7021 :
7022 569248 : printf (" %*s%-20s (%s) ",
7023 : (int) (level * 2), "", dwarf_attr_name (attr),
7024 : dwarf_form_name (form));
7025 569248 : if (is_split)
7026 33 : printf ("{%6" PRIxMAX "}\n", (uintmax_t) dwarf_dieoffset (&ref));
7027 : else
7028 569215 : printf ("[%6" PRIxMAX "]\n", (uintmax_t) dwarf_dieoffset (&ref));
7029 : break;
7030 :
7031 : case DW_FORM_ref_sig8:
7032 4 : if (cbargs->silent)
7033 : break;
7034 8 : printf (" %*s%-20s (%s) {%6" PRIx64 "}\n",
7035 : (int) (level * 2), "", dwarf_attr_name (attr),
7036 : dwarf_form_name (form),
7037 8 : (uint64_t) read_8ubyte_unaligned (attrp->cu->dbg, attrp->valp));
7038 : break;
7039 :
7040 : case DW_FORM_sec_offset:
7041 : case DW_FORM_rnglistx:
7042 : case DW_FORM_loclistx:
7043 : case DW_FORM_implicit_const:
7044 : case DW_FORM_udata:
7045 : case DW_FORM_sdata:
7046 : case DW_FORM_data8: /* Note no data16 here, we see that as block. */
7047 : case DW_FORM_data4:
7048 : case DW_FORM_data2:
7049 : case DW_FORM_data1:;
7050 : Dwarf_Word num;
7051 1194127 : if (unlikely (dwarf_formudata (attrp, &num) != 0))
7052 : goto attrval_out;
7053 :
7054 1194127 : const char *valuestr = NULL;
7055 1194127 : bool as_hex_id = false;
7056 1194127 : switch (attr)
7057 : {
7058 : /* This case can take either a constant or a loclistptr. */
7059 : case DW_AT_data_member_location:
7060 205000 : if (form != DW_FORM_sec_offset
7061 205000 : && (cbargs->version >= 4
7062 23806 : || (form != DW_FORM_data4 && form != DW_FORM_data8)))
7063 : {
7064 205000 : if (!cbargs->silent)
7065 205000 : printf (" %*s%-20s (%s) %" PRIxMAX "\n",
7066 : (int) (level * 2), "", dwarf_attr_name (attr),
7067 : dwarf_form_name (form), (uintmax_t) num);
7068 : return DWARF_CB_OK;
7069 : }
7070 : FALLTHROUGH;
7071 :
7072 : /* These cases always take a loclist[ptr] and no constant. */
7073 : case DW_AT_location:
7074 : case DW_AT_data_location:
7075 : case DW_AT_vtable_elem_location:
7076 : case DW_AT_string_length:
7077 : case DW_AT_use_location:
7078 : case DW_AT_frame_base:
7079 : case DW_AT_return_addr:
7080 : case DW_AT_static_link:
7081 : case DW_AT_segment:
7082 : case DW_AT_GNU_call_site_value:
7083 : case DW_AT_GNU_call_site_data_value:
7084 : case DW_AT_GNU_call_site_target:
7085 : case DW_AT_GNU_call_site_target_clobbered:
7086 : case DW_AT_GNU_locviews:
7087 : {
7088 : bool nlpt;
7089 41688 : if (cbargs->cu->version < 5)
7090 : {
7091 41617 : if (! cbargs->is_split)
7092 : {
7093 124791 : nlpt = notice_listptr (section_loc, &known_locsptr,
7094 41597 : cbargs->addrsize,
7095 41597 : cbargs->offset_size,
7096 : cbargs->cu, num, attr);
7097 : }
7098 : else
7099 : nlpt = true;
7100 : }
7101 : else
7102 : {
7103 : /* Only register for a real section offset. Otherwise
7104 : it is a DW_FORM_loclistx which is just an index
7105 : number and we should already have registered the
7106 : section offset for the index when we saw the
7107 : DW_AT_loclists_base CU attribute. */
7108 71 : if (form == DW_FORM_sec_offset)
7109 96 : nlpt = notice_listptr (section_loc, &known_loclistsptr,
7110 64 : cbargs->addrsize, cbargs->offset_size,
7111 : cbargs->cu, num, attr);
7112 : else
7113 : nlpt = true;
7114 :
7115 : }
7116 :
7117 41688 : if (!cbargs->silent)
7118 : {
7119 41513 : if (cbargs->cu->version < 5 || form == DW_FORM_sec_offset)
7120 41506 : printf (" %*s%-20s (%s) location list [%6"
7121 : PRIxMAX "]%s\n",
7122 : (int) (level * 2), "", dwarf_attr_name (attr),
7123 : dwarf_form_name (form), (uintmax_t) num,
7124 : nlpt ? "" : " <WARNING offset too big>");
7125 : else
7126 7 : printf (" %*s%-20s (%s) location index [%6"
7127 : PRIxMAX "]\n",
7128 : (int) (level * 2), "", dwarf_attr_name (attr),
7129 : dwarf_form_name (form), (uintmax_t) num);
7130 : }
7131 : }
7132 : return DWARF_CB_OK;
7133 :
7134 : case DW_AT_loclists_base:
7135 : {
7136 15 : bool nlpt = notice_listptr (section_loc, &known_loclistsptr,
7137 10 : cbargs->addrsize, cbargs->offset_size,
7138 : cbargs->cu, num, attr);
7139 :
7140 5 : if (!cbargs->silent)
7141 1 : printf (" %*s%-20s (%s) location list [%6" PRIxMAX "]%s\n",
7142 : (int) (level * 2), "", dwarf_attr_name (attr),
7143 : dwarf_form_name (form), (uintmax_t) num,
7144 : nlpt ? "" : " <WARNING offset too big>");
7145 : }
7146 : return DWARF_CB_OK;
7147 :
7148 : case DW_AT_ranges:
7149 : case DW_AT_start_scope:
7150 : {
7151 : bool nlpt;
7152 12023 : if (cbargs->cu->version < 5)
7153 36021 : nlpt = notice_listptr (section_ranges, &known_rangelistptr,
7154 24014 : cbargs->addrsize, cbargs->offset_size,
7155 : cbargs->cu, num, attr);
7156 : else
7157 : {
7158 : /* Only register for a real section offset. Otherwise
7159 : it is a DW_FORM_rangelistx which is just an index
7160 : number and we should already have registered the
7161 : section offset for the index when we saw the
7162 : DW_AT_rnglists_base CU attribute. */
7163 16 : if (form == DW_FORM_sec_offset)
7164 30 : nlpt = notice_listptr (section_ranges, &known_rnglistptr,
7165 20 : cbargs->addrsize, cbargs->offset_size,
7166 : cbargs->cu, num, attr);
7167 : else
7168 : nlpt = true;
7169 : }
7170 :
7171 12023 : if (!cbargs->silent)
7172 : {
7173 11981 : if (cbargs->cu->version < 5 || form == DW_FORM_sec_offset)
7174 11979 : printf (" %*s%-20s (%s) range list [%6"
7175 : PRIxMAX "]%s\n",
7176 : (int) (level * 2), "", dwarf_attr_name (attr),
7177 : dwarf_form_name (form), (uintmax_t) num,
7178 : nlpt ? "" : " <WARNING offset too big>");
7179 : else
7180 2 : printf (" %*s%-20s (%s) range index [%6"
7181 : PRIxMAX "]\n",
7182 : (int) (level * 2), "", dwarf_attr_name (attr),
7183 : dwarf_form_name (form), (uintmax_t) num);
7184 : }
7185 : }
7186 : return DWARF_CB_OK;
7187 :
7188 : case DW_AT_rnglists_base:
7189 : {
7190 12 : bool nlpt = notice_listptr (section_ranges, &known_rnglistptr,
7191 8 : cbargs->addrsize, cbargs->offset_size,
7192 : cbargs->cu, num, attr);
7193 4 : if (!cbargs->silent)
7194 2 : printf (" %*s%-20s (%s) range list [%6"
7195 : PRIxMAX "]%s\n",
7196 : (int) (level * 2), "", dwarf_attr_name (attr),
7197 : dwarf_form_name (form), (uintmax_t) num,
7198 : nlpt ? "" : " <WARNING offset too big>");
7199 : }
7200 : return DWARF_CB_OK;
7201 :
7202 : case DW_AT_addr_base:
7203 : case DW_AT_GNU_addr_base:
7204 : {
7205 39 : bool addrbase = notice_listptr (section_addr, &known_addrbases,
7206 13 : cbargs->addrsize,
7207 13 : cbargs->offset_size,
7208 : cbargs->cu, num, attr);
7209 13 : if (!cbargs->silent)
7210 5 : printf (" %*s%-20s (%s) address base [%6"
7211 : PRIxMAX "]%s\n",
7212 : (int) (level * 2), "", dwarf_attr_name (attr),
7213 : dwarf_form_name (form), (uintmax_t) num,
7214 : addrbase ? "" : " <WARNING offset too big>");
7215 : }
7216 : return DWARF_CB_OK;
7217 :
7218 : case DW_AT_str_offsets_base:
7219 : {
7220 0 : bool stroffbase = notice_listptr (section_str, &known_stroffbases,
7221 0 : cbargs->addrsize,
7222 0 : cbargs->offset_size,
7223 : cbargs->cu, num, attr);
7224 0 : if (!cbargs->silent)
7225 0 : printf (" %*s%-20s (%s) str offsets base [%6"
7226 : PRIxMAX "]%s\n",
7227 : (int) (level * 2), "", dwarf_attr_name (attr),
7228 : dwarf_form_name (form), (uintmax_t) num,
7229 : stroffbase ? "" : " <WARNING offset too big>");
7230 : }
7231 : return DWARF_CB_OK;
7232 :
7233 : case DW_AT_language:
7234 1134 : valuestr = dwarf_lang_name (num);
7235 1134 : break;
7236 : case DW_AT_encoding:
7237 13791 : valuestr = dwarf_encoding_name (num);
7238 13791 : break;
7239 : case DW_AT_accessibility:
7240 0 : valuestr = dwarf_access_name (num);
7241 0 : break;
7242 : case DW_AT_defaulted:
7243 0 : valuestr = dwarf_defaulted_name (num);
7244 0 : break;
7245 : case DW_AT_visibility:
7246 0 : valuestr = dwarf_visibility_name (num);
7247 0 : break;
7248 : case DW_AT_virtuality:
7249 0 : valuestr = dwarf_virtuality_name (num);
7250 0 : break;
7251 : case DW_AT_identifier_case:
7252 0 : valuestr = dwarf_identifier_case_name (num);
7253 0 : break;
7254 : case DW_AT_calling_convention:
7255 0 : valuestr = dwarf_calling_convention_name (num);
7256 0 : break;
7257 : case DW_AT_inline:
7258 2775 : valuestr = dwarf_inline_name (num);
7259 2775 : break;
7260 : case DW_AT_ordering:
7261 0 : valuestr = dwarf_ordering_name (num);
7262 0 : break;
7263 : case DW_AT_discr_list:
7264 0 : valuestr = dwarf_discr_list_name (num);
7265 0 : break;
7266 : case DW_AT_decl_file:
7267 : case DW_AT_call_file:
7268 : {
7269 347246 : if (cbargs->silent)
7270 : break;
7271 :
7272 : /* Try to get the actual file, the current interface only
7273 : gives us full paths, but we only want to show the file
7274 : name for now. */
7275 : Dwarf_Die cudie;
7276 346840 : if (dwarf_cu_die (cbargs->cu, &cudie,
7277 : NULL, NULL, NULL, NULL, NULL, NULL) != NULL)
7278 : {
7279 : Dwarf_Files *files;
7280 : size_t nfiles;
7281 346840 : if (dwarf_getsrcfiles (&cudie, &files, &nfiles) == 0)
7282 : {
7283 346840 : valuestr = dwarf_filesrc (files, num, NULL, NULL);
7284 346840 : if (valuestr != NULL)
7285 : {
7286 346840 : char *filename = strrchr (valuestr, '/');
7287 346840 : if (filename != NULL)
7288 346840 : valuestr = filename + 1;
7289 : }
7290 : else
7291 0 : error (0, 0, gettext ("invalid file (%" PRId64 "): %s"),
7292 : num, dwarf_errmsg (-1));
7293 : }
7294 : else
7295 0 : error (0, 0, gettext ("no srcfiles for CU [%" PRIx64 "]"),
7296 : dwarf_dieoffset (&cudie));
7297 : }
7298 : else
7299 0 : error (0, 0, gettext ("couldn't get DWARF CU: %s"),
7300 : dwarf_errmsg (-1));
7301 346840 : if (valuestr == NULL)
7302 0 : valuestr = "???";
7303 : }
7304 346840 : break;
7305 : case DW_AT_GNU_dwo_id:
7306 13 : as_hex_id = true;
7307 13 : break;
7308 :
7309 : default:
7310 : /* Nothing. */
7311 : break;
7312 : }
7313 :
7314 935394 : if (cbargs->silent)
7315 : break;
7316 :
7317 : /* When highpc is in constant form it is relative to lowpc.
7318 : In that case also show the address. */
7319 : Dwarf_Addr highpc;
7320 933622 : if (attr == DW_AT_high_pc && dwarf_highpc (cbargs->die, &highpc) == 0)
7321 : {
7322 12866 : printf (" %*s%-20s (%s) %" PRIuMAX " (",
7323 : (int) (level * 2), "", dwarf_attr_name (attr),
7324 : dwarf_form_name (form), (uintmax_t) num);
7325 12866 : print_dwarf_addr (cbargs->dwflmod, cbargs->addrsize, highpc, highpc);
7326 : printf (")\n");
7327 : }
7328 : else
7329 : {
7330 920756 : if (as_hex_id)
7331 : {
7332 2 : printf (" %*s%-20s (%s) 0x%.16" PRIx64 "\n",
7333 : (int) (level * 2), "", dwarf_attr_name (attr),
7334 : dwarf_form_name (form), num);
7335 : }
7336 : else
7337 : {
7338 920754 : Dwarf_Sword snum = 0;
7339 : bool is_signed;
7340 920754 : int bytes = 0;
7341 920754 : if (attr == DW_AT_const_value)
7342 98621 : die_type_sign_bytes (cbargs->die, &is_signed, &bytes);
7343 : else
7344 1644266 : is_signed = (form == DW_FORM_sdata
7345 822133 : || form == DW_FORM_implicit_const);
7346 :
7347 920754 : if (is_signed)
7348 174 : if (unlikely (dwarf_formsdata (attrp, &snum) != 0))
7349 : goto attrval_out;
7350 :
7351 920754 : if (valuestr == NULL)
7352 : {
7353 556505 : printf (" %*s%-20s (%s) ",
7354 : (int) (level * 2), "", dwarf_attr_name (attr),
7355 : dwarf_form_name (form));
7356 : }
7357 : else
7358 : {
7359 364249 : printf (" %*s%-20s (%s) %s (",
7360 : (int) (level * 2), "", dwarf_attr_name (attr),
7361 : dwarf_form_name (form), valuestr);
7362 : }
7363 :
7364 920754 : switch (bytes)
7365 : {
7366 : case 1:
7367 6 : if (is_signed)
7368 1 : printf ("%" PRId8, (int8_t) snum);
7369 : else
7370 5 : printf ("%" PRIu8, (uint8_t) num);
7371 : break;
7372 :
7373 : case 2:
7374 2 : if (is_signed)
7375 1 : printf ("%" PRId16, (int16_t) snum);
7376 : else
7377 1 : printf ("%" PRIu16, (uint16_t) num);
7378 : break;
7379 :
7380 : case 4:
7381 153 : if (is_signed)
7382 151 : printf ("%" PRId32, (int32_t) snum);
7383 : else
7384 2 : printf ("%" PRIu32, (uint32_t) num);
7385 : break;
7386 :
7387 : case 8:
7388 134 : if (is_signed)
7389 1 : printf ("%" PRId64, (int64_t) snum);
7390 : else
7391 133 : printf ("%" PRIu64, (uint64_t) num);
7392 : break;
7393 :
7394 : default:
7395 920459 : if (is_signed)
7396 20 : printf ("%" PRIdMAX, (intmax_t) snum);
7397 : else
7398 920439 : printf ("%" PRIuMAX, (uintmax_t) num);
7399 : break;
7400 : }
7401 :
7402 : /* Make clear if we switched from a signed encoding to
7403 : an unsigned value. */
7404 920754 : if (attr == DW_AT_const_value
7405 98621 : && (form == DW_FORM_sdata || form == DW_FORM_implicit_const)
7406 97844 : && !is_signed)
7407 97837 : printf (" (%" PRIdMAX ")", (intmax_t) num);
7408 :
7409 920754 : if (valuestr == NULL)
7410 : printf ("\n");
7411 : else
7412 : printf (")\n");
7413 : }
7414 : }
7415 : break;
7416 :
7417 : case DW_FORM_flag:
7418 9122 : if (cbargs->silent)
7419 : break;
7420 : bool flag;
7421 9048 : if (unlikely (dwarf_formflag (attrp, &flag) != 0))
7422 : goto attrval_out;
7423 :
7424 9048 : printf (" %*s%-20s (%s) %s\n",
7425 : (int) (level * 2), "", dwarf_attr_name (attr),
7426 : dwarf_form_name (form), flag ? yes_str : no_str);
7427 : break;
7428 :
7429 : case DW_FORM_flag_present:
7430 49557 : if (cbargs->silent)
7431 : break;
7432 49292 : printf (" %*s%-20s (%s) %s\n",
7433 : (int) (level * 2), "", dwarf_attr_name (attr),
7434 : dwarf_form_name (form), yes_str);
7435 : break;
7436 :
7437 : case DW_FORM_exprloc:
7438 : case DW_FORM_block4:
7439 : case DW_FORM_block2:
7440 : case DW_FORM_block1:
7441 : case DW_FORM_block:
7442 : case DW_FORM_data16: /* DWARF5 calls this a constant class. */
7443 82645 : if (cbargs->silent)
7444 : break;
7445 : Dwarf_Block block;
7446 82522 : if (unlikely (dwarf_formblock (attrp, &block) != 0))
7447 : goto attrval_out;
7448 :
7449 82522 : printf (" %*s%-20s (%s) ",
7450 : (int) (level * 2), "", dwarf_attr_name (attr),
7451 : dwarf_form_name (form));
7452 :
7453 82522 : switch (attr)
7454 : {
7455 : default:
7456 14 : if (form != DW_FORM_exprloc)
7457 : {
7458 14 : print_block (block.length, block.data);
7459 14 : break;
7460 : }
7461 : FALLTHROUGH;
7462 :
7463 : case DW_AT_location:
7464 : case DW_AT_data_location:
7465 : case DW_AT_data_member_location:
7466 : case DW_AT_vtable_elem_location:
7467 : case DW_AT_string_length:
7468 : case DW_AT_use_location:
7469 : case DW_AT_frame_base:
7470 : case DW_AT_return_addr:
7471 : case DW_AT_static_link:
7472 : case DW_AT_allocated:
7473 : case DW_AT_associated:
7474 : case DW_AT_bit_size:
7475 : case DW_AT_bit_offset:
7476 : case DW_AT_bit_stride:
7477 : case DW_AT_byte_size:
7478 : case DW_AT_byte_stride:
7479 : case DW_AT_count:
7480 : case DW_AT_lower_bound:
7481 : case DW_AT_upper_bound:
7482 : case DW_AT_GNU_call_site_value:
7483 : case DW_AT_GNU_call_site_data_value:
7484 : case DW_AT_GNU_call_site_target:
7485 : case DW_AT_GNU_call_site_target_clobbered:
7486 82508 : if (form != DW_FORM_data16)
7487 : {
7488 : putchar ('\n');
7489 165016 : print_ops (cbargs->dwflmod, cbargs->dbg,
7490 82508 : 12 + level * 2, 12 + level * 2,
7491 : cbargs->version, cbargs->addrsize, cbargs->offset_size,
7492 82508 : attrp->cu, block.length, block.data);
7493 : }
7494 : else
7495 0 : print_block (block.length, block.data);
7496 : break;
7497 : }
7498 : break;
7499 :
7500 : default:
7501 0 : if (cbargs->silent)
7502 : break;
7503 0 : printf (" %*s%-20s (%s) ???\n",
7504 : (int) (level * 2), "", dwarf_attr_name (attr),
7505 : dwarf_form_name (form));
7506 : break;
7507 : }
7508 :
7509 2127191 : return DWARF_CB_OK;
7510 : }
7511 :
7512 : static void
7513 83 : print_debug_units (Dwfl_Module *dwflmod,
7514 166 : Ebl *ebl, GElf_Ehdr *ehdr,
7515 83 : Elf_Scn *scn, GElf_Shdr *shdr,
7516 : Dwarf *dbg, bool debug_types)
7517 : {
7518 83 : const bool silent = !(print_debug_sections & section_info) && !debug_types;
7519 332 : const char *secname = section_name (ebl, ehdr, shdr);
7520 :
7521 83 : if (!silent)
7522 51 : printf (gettext ("\
7523 : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n [Offset]\n"),
7524 : elf_ndxscn (scn), secname, (uint64_t) shdr->sh_offset);
7525 :
7526 : /* If the section is empty we don't have to do anything. */
7527 83 : if (!silent && shdr->sh_size == 0)
7528 0 : return;
7529 :
7530 83 : int maxdies = 20;
7531 83 : Dwarf_Die *dies = (Dwarf_Die *) xmalloc (maxdies * sizeof (Dwarf_Die));
7532 :
7533 : /* New compilation unit. */
7534 : Dwarf_Half version;
7535 :
7536 : Dwarf_Die result;
7537 : Dwarf_Off abbroffset;
7538 : uint8_t addrsize;
7539 : uint8_t offsize;
7540 : uint64_t unit_id;
7541 : Dwarf_Off subdie_off;
7542 :
7543 : int unit_res;
7544 : Dwarf_CU *cu;
7545 : Dwarf_CU cu_mem;
7546 : uint8_t unit_type;
7547 : Dwarf_Die cudie;
7548 :
7549 : /* We cheat a little because we want to see only the CUs from .debug_info
7550 : or .debug_types. We know the Dwarf_CU struct layout. Set it up at
7551 : the end of .debug_info if we want .debug_types only. Check the returned
7552 : Dwarf_CU is still in the expected section. */
7553 83 : if (debug_types)
7554 : {
7555 1 : cu_mem.dbg = dbg;
7556 1 : cu_mem.end = dbg->sectiondata[IDX_debug_info]->d_size;
7557 1 : cu_mem.sec_idx = IDX_debug_info;
7558 1 : cu = &cu_mem;
7559 : }
7560 : else
7561 82 : cu = NULL;
7562 :
7563 : next_cu:
7564 1226 : unit_res = dwarf_get_units (dbg, cu, &cu, &version, &unit_type,
7565 : &cudie, NULL);
7566 1226 : if (unit_res == 1)
7567 : goto do_return;
7568 :
7569 1144 : if (unit_res == -1)
7570 : {
7571 0 : if (!silent)
7572 0 : error (0, 0, gettext ("cannot get next unit: %s"), dwarf_errmsg (-1));
7573 : goto do_return;
7574 : }
7575 :
7576 1144 : if (cu->sec_idx != (size_t) (debug_types ? IDX_debug_types : IDX_debug_info))
7577 : goto do_return;
7578 :
7579 1143 : dwarf_cu_die (cu, &result, NULL, &abbroffset, &addrsize, &offsize,
7580 : &unit_id, &subdie_off);
7581 :
7582 1143 : if (!silent)
7583 : {
7584 1097 : Dwarf_Off offset = cu->start;
7585 1097 : if (debug_types && version < 5)
7586 2 : {
7587 : Dwarf_Die typedie;
7588 : Dwarf_Off dieoffset;
7589 2 : dieoffset = dwarf_dieoffset (dwarf_offdie_types (dbg, subdie_off,
7590 : &typedie));
7591 2 : printf (gettext (" Type unit at offset %" PRIu64 ":\n"
7592 : " Version: %" PRIu16
7593 : ", Abbreviation section offset: %" PRIu64
7594 : ", Address size: %" PRIu8
7595 : ", Offset size: %" PRIu8
7596 : "\n Type signature: %#" PRIx64
7597 : ", Type offset: %#" PRIx64 " [%" PRIx64 "]\n"),
7598 : (uint64_t) offset, version, abbroffset, addrsize, offsize,
7599 : unit_id, (uint64_t) subdie_off, dieoffset);
7600 : }
7601 : else
7602 : {
7603 1095 : printf (gettext (" Compilation unit at offset %" PRIu64 ":\n"
7604 : " Version: %" PRIu16
7605 : ", Abbreviation section offset: %" PRIu64
7606 : ", Address size: %" PRIu8
7607 : ", Offset size: %" PRIu8 "\n"),
7608 : (uint64_t) offset, version, abbroffset, addrsize, offsize);
7609 :
7610 2185 : if (version >= 5 || (unit_type != DW_UT_compile
7611 1090 : && unit_type != DW_UT_partial))
7612 : {
7613 6 : printf (gettext (" Unit type: %s (%" PRIu8 ")"),
7614 : dwarf_unit_name (unit_type), unit_type);
7615 6 : if (unit_type == DW_UT_type
7616 6 : || unit_type == DW_UT_skeleton
7617 1 : || unit_type == DW_UT_split_compile
7618 1 : || unit_type == DW_UT_split_type)
7619 5 : printf (", Unit id: 0x%.16" PRIx64 "", unit_id);
7620 6 : if (unit_type == DW_UT_type
7621 6 : || unit_type == DW_UT_split_type)
7622 : {
7623 : Dwarf_Die typedie;
7624 : Dwarf_Off dieoffset;
7625 0 : dwarf_cu_info (cu, NULL, NULL, NULL, &typedie,
7626 : NULL, NULL, NULL);
7627 0 : dieoffset = dwarf_dieoffset (&typedie);
7628 0 : printf (", Unit DIE off: %#" PRIx64 " [%" PRIx64 "]",
7629 : subdie_off, dieoffset);
7630 : }
7631 : printf ("\n");
7632 : }
7633 : }
7634 : }
7635 :
7636 1143 : if (version < 2 || version > 5
7637 1143 : || unit_type < DW_UT_compile || unit_type > DW_UT_split_type)
7638 : {
7639 0 : if (!silent)
7640 0 : error (0, 0, gettext ("unknown version (%d) or unit type (%d)"),
7641 : version, unit_type);
7642 : goto next_cu;
7643 : }
7644 :
7645 1143 : struct attrcb_args args =
7646 : {
7647 : .dwflmod = dwflmod,
7648 : .silent = silent,
7649 : .version = version,
7650 : .addrsize = addrsize,
7651 : .offset_size = offsize
7652 : };
7653 :
7654 1143 : bool is_split = false;
7655 1143 : int level = 0;
7656 1143 : dies[0] = cudie;
7657 1143 : args.cu = dies[0].cu;
7658 1143 : args.dbg = dbg;
7659 : args.is_split = is_split;
7660 :
7661 : /* We might return here again for the split CU subdie. */
7662 : do_cu:
7663 : do
7664 : {
7665 690273 : Dwarf_Off offset = dwarf_dieoffset (&dies[level]);
7666 690273 : if (unlikely (offset == (Dwarf_Off) -1))
7667 : {
7668 0 : if (!silent)
7669 0 : error (0, 0, gettext ("cannot get DIE offset: %s"),
7670 : dwarf_errmsg (-1));
7671 : goto do_return;
7672 : }
7673 :
7674 690273 : int tag = dwarf_tag (&dies[level]);
7675 690273 : if (unlikely (tag == DW_TAG_invalid))
7676 : {
7677 0 : if (!silent)
7678 0 : error (0, 0, gettext ("cannot get tag of DIE at offset [%" PRIx64
7679 : "] in section '%s': %s"),
7680 : (uint64_t) offset, secname, dwarf_errmsg (-1));
7681 : goto do_return;
7682 : }
7683 :
7684 690273 : if (!silent)
7685 : {
7686 689318 : unsigned int code = dwarf_getabbrevcode (dies[level].abbrev);
7687 689318 : if (is_split)
7688 : printf (" {%6" PRIx64 "} ", (uint64_t) offset);
7689 : else
7690 : printf (" [%6" PRIx64 "] ", (uint64_t) offset);
7691 689318 : printf ("%*s%-20s abbrev: %u\n", (int) (level * 2), "",
7692 : dwarf_tag_name (tag), code);
7693 : }
7694 :
7695 : /* Print the attribute values. */
7696 690273 : args.level = level;
7697 690273 : args.die = &dies[level];
7698 690273 : (void) dwarf_getattrs (&dies[level], attr_callback, &args, 0);
7699 :
7700 : /* Make room for the next level's DIE. */
7701 690273 : if (level + 1 == maxdies)
7702 0 : dies = (Dwarf_Die *) xrealloc (dies,
7703 0 : (maxdies += 10)
7704 : * sizeof (Dwarf_Die));
7705 :
7706 690273 : int res = dwarf_child (&dies[level], &dies[level + 1]);
7707 690273 : if (res > 0)
7708 : {
7709 690273 : while ((res = dwarf_siblingof (&dies[level], &dies[level])) == 1)
7710 104548 : if (level-- == 0)
7711 : break;
7712 :
7713 586872 : if (unlikely (res == -1))
7714 : {
7715 0 : if (!silent)
7716 0 : error (0, 0, gettext ("cannot get next DIE: %s\n"),
7717 : dwarf_errmsg (-1));
7718 : goto do_return;
7719 : }
7720 : }
7721 103401 : else if (unlikely (res < 0))
7722 : {
7723 0 : if (!silent)
7724 0 : error (0, 0, gettext ("cannot get next DIE: %s"),
7725 : dwarf_errmsg (-1));
7726 : goto do_return;
7727 : }
7728 : else
7729 : ++level;
7730 : }
7731 690273 : while (level >= 0);
7732 :
7733 : /* We might want to show the split compile unit if this was a skeleton.
7734 : We need to scan it if we are requesting printing .debug_ranges for
7735 : DWARF4 since GNU DebugFission uses "offsets" into the main ranges
7736 : section. */
7737 1147 : if (unit_type == DW_UT_skeleton
7738 13 : && ((!silent && show_split_units)
7739 10 : || (version < 5 && (print_debug_sections & section_ranges) != 0)))
7740 : {
7741 : Dwarf_Die subdie;
7742 5 : if (dwarf_cu_info (cu, NULL, NULL, NULL, &subdie, NULL, NULL, NULL) != 0
7743 5 : || dwarf_tag (&subdie) == DW_TAG_invalid)
7744 : {
7745 1 : if (!silent)
7746 : {
7747 : Dwarf_Attribute dwo_at;
7748 1 : const char *dwo_name =
7749 1 : (dwarf_formstring (dwarf_attr (&cudie, DW_AT_dwo_name,
7750 : &dwo_at))
7751 1 : ?: (dwarf_formstring (dwarf_attr (&cudie, DW_AT_GNU_dwo_name,
7752 : &dwo_at))
7753 0 : ?: "<unknown>"));
7754 1 : fprintf (stderr,
7755 : "Could not find split unit '%s', id: %" PRIx64 "\n",
7756 : dwo_name, unit_id);
7757 : }
7758 : }
7759 : else
7760 : {
7761 4 : Dwarf_CU *split_cu = subdie.cu;
7762 4 : dwarf_cu_die (split_cu, &result, NULL, &abbroffset,
7763 : &addrsize, &offsize, &unit_id, &subdie_off);
7764 4 : Dwarf_Off offset = cu->start;
7765 :
7766 4 : if (!silent)
7767 : {
7768 2 : printf (gettext (" Split compilation unit at offset %"
7769 : PRIu64 ":\n"
7770 : " Version: %" PRIu16
7771 : ", Abbreviation section offset: %" PRIu64
7772 : ", Address size: %" PRIu8
7773 : ", Offset size: %" PRIu8 "\n"),
7774 : (uint64_t) offset, version, abbroffset,
7775 : addrsize, offsize);
7776 2 : printf (gettext (" Unit type: %s (%" PRIu8 ")"),
7777 : dwarf_unit_name (unit_type), unit_type);
7778 2 : printf (", Unit id: 0x%.16" PRIx64 "", unit_id);
7779 : printf ("\n");
7780 : }
7781 :
7782 4 : unit_type = DW_UT_split_compile;
7783 4 : is_split = true;
7784 4 : level = 0;
7785 4 : dies[0] = subdie;
7786 4 : args.cu = dies[0].cu;
7787 4 : args.dbg = split_cu->dbg;
7788 4 : args.is_split = is_split;
7789 4 : goto do_cu;
7790 : }
7791 : }
7792 :
7793 : /* And again... */
7794 : goto next_cu;
7795 :
7796 : do_return:
7797 83 : free (dies);
7798 : }
7799 :
7800 : static void
7801 14 : print_debug_info_section (Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr,
7802 : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
7803 : {
7804 82 : print_debug_units (dwflmod, ebl, ehdr, scn, shdr, dbg, false);
7805 14 : }
7806 :
7807 : static void
7808 1 : print_debug_types_section (Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr,
7809 : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
7810 : {
7811 1 : print_debug_units (dwflmod, ebl, ehdr, scn, shdr, dbg, true);
7812 1 : }
7813 :
7814 :
7815 : static void
7816 12 : print_decoded_line_section (Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr,
7817 4 : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
7818 : {
7819 20 : printf (gettext ("\
7820 : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n\n"),
7821 : elf_ndxscn (scn), section_name (ebl, ehdr, shdr),
7822 : (uint64_t) shdr->sh_offset);
7823 :
7824 4 : size_t address_size
7825 4 : = elf_getident (ebl->elf, NULL)[EI_CLASS] == ELFCLASS32 ? 4 : 8;
7826 :
7827 : Dwarf_Lines *lines;
7828 : size_t nlines;
7829 4 : Dwarf_Off off, next_off = 0;
7830 4 : Dwarf_CU *cu = NULL;
7831 16 : while (dwarf_next_lines (dbg, off = next_off, &next_off, &cu, NULL, NULL,
7832 : &lines, &nlines) == 0)
7833 : {
7834 : Dwarf_Die cudie;
7835 8 : if (cu != NULL && dwarf_cu_info (cu, NULL, NULL, &cudie,
7836 : NULL, NULL, NULL, NULL) == 0)
7837 6 : printf (" CU [%" PRIx64 "] %s\n",
7838 : dwarf_dieoffset (&cudie), dwarf_diename (&cudie));
7839 : else
7840 : {
7841 : /* DWARF5 lines can be independent of any CU, but they probably
7842 : are used by some CU. Determine the CU this block is for. */
7843 : Dwarf_Off cuoffset;
7844 2 : Dwarf_Off ncuoffset = 0;
7845 : size_t hsize;
7846 5 : while (dwarf_nextcu (dbg, cuoffset = ncuoffset, &ncuoffset, &hsize,
7847 : NULL, NULL, NULL) == 0)
7848 : {
7849 3 : if (dwarf_offdie (dbg, cuoffset + hsize, &cudie) == NULL)
7850 0 : continue;
7851 : Dwarf_Attribute stmt_list;
7852 3 : if (dwarf_attr (&cudie, DW_AT_stmt_list, &stmt_list) == NULL)
7853 0 : continue;
7854 : Dwarf_Word lineoff;
7855 3 : if (dwarf_formudata (&stmt_list, &lineoff) != 0)
7856 0 : continue;
7857 3 : if (lineoff == off)
7858 : {
7859 : /* Found the CU. */
7860 2 : cu = cudie.cu;
7861 2 : break;
7862 : }
7863 : }
7864 :
7865 2 : if (cu != NULL)
7866 2 : printf (" CU [%" PRIx64 "] %s\n",
7867 : dwarf_dieoffset (&cudie), dwarf_diename (&cudie));
7868 : else
7869 : printf (" No CU\n");
7870 : }
7871 :
7872 : printf (" line:col SBPE* disc isa op address"
7873 : " (Statement Block Prologue Epilogue *End)\n");
7874 8 : const char *last_file = "";
7875 168 : for (size_t n = 0; n < nlines; n++)
7876 : {
7877 160 : Dwarf_Line *line = dwarf_onesrcline (lines, n);
7878 160 : if (line == NULL)
7879 : {
7880 0 : printf (" dwarf_onesrcline: %s\n", dwarf_errmsg (-1));
7881 0 : continue;
7882 : }
7883 : Dwarf_Word mtime, length;
7884 160 : const char *file = dwarf_linesrc (line, &mtime, &length);
7885 160 : if (file == NULL)
7886 : {
7887 0 : printf (" <%s> (mtime: ?, length: ?)\n", dwarf_errmsg (-1));
7888 0 : last_file = "";
7889 : }
7890 160 : else if (strcmp (last_file, file) != 0)
7891 : {
7892 20 : printf (" %s (mtime: %" PRIu64 ", length: %" PRIu64 ")\n",
7893 : file, mtime, length);
7894 20 : last_file = file;
7895 : }
7896 :
7897 : int lineno, colno;
7898 : bool statement, endseq, block, prologue_end, epilogue_begin;
7899 : unsigned int lineop, isa, disc;
7900 : Dwarf_Addr address;
7901 160 : dwarf_lineaddr (line, &address);
7902 160 : dwarf_lineno (line, &lineno);
7903 160 : dwarf_linecol (line, &colno);
7904 160 : dwarf_lineop_index (line, &lineop);
7905 160 : dwarf_linebeginstatement (line, &statement);
7906 160 : dwarf_lineendsequence (line, &endseq);
7907 160 : dwarf_lineblock (line, &block);
7908 160 : dwarf_lineprologueend (line, &prologue_end);
7909 160 : dwarf_lineepiloguebegin (line, &epilogue_begin);
7910 160 : dwarf_lineisa (line, &isa);
7911 160 : dwarf_linediscriminator (line, &disc);
7912 :
7913 : /* End sequence is special, it is one byte past. */
7914 160 : printf (" %4d:%-3d %c%c%c%c%c %4d %3d %2d ",
7915 : lineno, colno,
7916 : (statement ? 'S' : ' '),
7917 : (block ? 'B' : ' '),
7918 : (prologue_end ? 'P' : ' '),
7919 : (epilogue_begin ? 'E' : ' '),
7920 : (endseq ? '*' : ' '),
7921 : disc, isa, lineop);
7922 160 : print_dwarf_addr (dwflmod, address_size,
7923 : address - (endseq ? 1 : 0), address);
7924 : printf ("\n");
7925 :
7926 160 : if (endseq)
7927 : printf("\n");
7928 : }
7929 : }
7930 4 : }
7931 :
7932 :
7933 : /* Print the value of a form.
7934 : Returns new value of readp, or readendp on failure. */
7935 : static const unsigned char *
7936 20 : print_form_data (Dwarf *dbg, int form, const unsigned char *readp,
7937 : const unsigned char *readendp, unsigned int offset_len,
7938 : Dwarf_Off str_offsets_base)
7939 : {
7940 : Dwarf_Word val;
7941 : unsigned char *endp;
7942 : Elf_Data *data;
7943 : char *str;
7944 20 : switch (form)
7945 : {
7946 : case DW_FORM_data1:
7947 8 : if (readendp - readp < 1)
7948 : {
7949 : invalid_data:
7950 : error (0, 0, "invalid data");
7951 0 : return readendp;
7952 : }
7953 8 : val = *readp++;
7954 8 : printf (" %" PRIx8, (unsigned int) val);
7955 : break;
7956 :
7957 : case DW_FORM_data2:
7958 0 : if (readendp - readp < 2)
7959 : goto invalid_data;
7960 0 : val = read_2ubyte_unaligned_inc (dbg, readp);
7961 0 : printf(" %" PRIx16, (unsigned int) val);
7962 : break;
7963 :
7964 : case DW_FORM_data4:
7965 0 : if (readendp - readp < 4)
7966 : goto invalid_data;
7967 0 : val = read_4ubyte_unaligned_inc (dbg, readp);
7968 0 : printf (" %" PRIx32, (unsigned int) val);
7969 : break;
7970 :
7971 : case DW_FORM_data8:
7972 0 : if (readendp - readp < 8)
7973 : goto invalid_data;
7974 0 : val = read_8ubyte_unaligned_inc (dbg, readp);
7975 : printf (" %" PRIx64, val);
7976 : break;
7977 :
7978 : case DW_FORM_sdata:
7979 0 : if (readendp - readp < 1)
7980 : goto invalid_data;
7981 0 : get_sleb128 (val, readp, readendp);
7982 : printf (" %" PRIx64, val);
7983 : break;
7984 :
7985 : case DW_FORM_udata:
7986 0 : if (readendp - readp < 1)
7987 : goto invalid_data;
7988 0 : get_uleb128 (val, readp, readendp);
7989 : printf (" %" PRIx64, val);
7990 : break;
7991 :
7992 : case DW_FORM_block:
7993 0 : if (readendp - readp < 1)
7994 : goto invalid_data;
7995 0 : get_uleb128 (val, readp, readendp);
7996 0 : if ((size_t) (readendp - readp) < val)
7997 : goto invalid_data;
7998 0 : print_bytes (val, readp);
7999 0 : readp += val;
8000 0 : break;
8001 :
8002 : case DW_FORM_block1:
8003 0 : if (readendp - readp < 1)
8004 : goto invalid_data;
8005 0 : val = *readp++;
8006 0 : if ((size_t) (readendp - readp) < val)
8007 : goto invalid_data;
8008 0 : print_bytes (val, readp);
8009 0 : readp += val;
8010 0 : break;
8011 :
8012 : case DW_FORM_block2:
8013 0 : if (readendp - readp < 2)
8014 : goto invalid_data;
8015 0 : val = read_2ubyte_unaligned_inc (dbg, readp);
8016 0 : if ((size_t) (readendp - readp) < val)
8017 : goto invalid_data;
8018 0 : print_bytes (val, readp);
8019 0 : readp += val;
8020 0 : break;
8021 :
8022 : case DW_FORM_block4:
8023 0 : if (readendp - readp < 4)
8024 : goto invalid_data;
8025 0 : val = read_4ubyte_unaligned_inc (dbg, readp);
8026 0 : if ((size_t) (readendp - readp) < val)
8027 : goto invalid_data;
8028 0 : print_bytes (val, readp);
8029 0 : readp += val;
8030 0 : break;
8031 :
8032 : case DW_FORM_data16:
8033 0 : if (readendp - readp < 16)
8034 : goto invalid_data;
8035 0 : print_bytes (16, readp);
8036 0 : readp += 16;
8037 0 : break;
8038 :
8039 : case DW_FORM_flag:
8040 0 : if (readendp - readp < 1)
8041 : goto invalid_data;
8042 0 : val = *readp++;
8043 0 : printf ("%s", val != 0 ? yes_str : no_str);
8044 : break;
8045 :
8046 : case DW_FORM_string:
8047 0 : endp = memchr (readp, '\0', readendp - readp);
8048 0 : if (endp == NULL)
8049 : goto invalid_data;
8050 0 : printf ("%s", readp);
8051 0 : readp = endp + 1;
8052 0 : break;
8053 :
8054 : case DW_FORM_strp:
8055 : case DW_FORM_line_strp:
8056 : case DW_FORM_strp_sup:
8057 12 : if ((size_t) (readendp - readp) < offset_len)
8058 : goto invalid_data;
8059 12 : if (offset_len == 8)
8060 0 : val = read_8ubyte_unaligned_inc (dbg, readp);
8061 : else
8062 24 : val = read_4ubyte_unaligned_inc (dbg, readp);
8063 12 : if (form == DW_FORM_strp)
8064 0 : data = dbg->sectiondata[IDX_debug_str];
8065 12 : else if (form == DW_FORM_line_strp)
8066 12 : data = dbg->sectiondata[IDX_debug_line_str];
8067 : else /* form == DW_FORM_strp_sup */
8068 : {
8069 0 : Dwarf *alt = dwarf_getalt (dbg);
8070 0 : data = alt != NULL ? alt->sectiondata[IDX_debug_str] : NULL;
8071 : }
8072 12 : if (data == NULL || val >= data->d_size
8073 12 : || memchr (data->d_buf + val, '\0', data->d_size - val) == NULL)
8074 : str = "???";
8075 : else
8076 12 : str = (char *) data->d_buf + val;
8077 : printf ("%s (%" PRIu64 ")", str, val);
8078 : break;
8079 :
8080 : case DW_FORM_sec_offset:
8081 0 : if ((size_t) (readendp - readp) < offset_len)
8082 : goto invalid_data;
8083 0 : if (offset_len == 8)
8084 0 : val = read_8ubyte_unaligned_inc (dbg, readp);
8085 : else
8086 0 : val = read_4ubyte_unaligned_inc (dbg, readp);
8087 : printf ("[%" PRIx64 "]", val);
8088 : break;
8089 :
8090 : case DW_FORM_strx:
8091 : case DW_FORM_GNU_str_index:
8092 0 : if (readendp - readp < 1)
8093 : goto invalid_data;
8094 0 : get_uleb128 (val, readp, readendp);
8095 : strx_val:
8096 0 : data = dbg->sectiondata[IDX_debug_str_offsets];
8097 0 : if (data == NULL
8098 0 : || data->d_size - str_offsets_base < val)
8099 : str = "???";
8100 : else
8101 : {
8102 0 : const unsigned char *strreadp = data->d_buf + str_offsets_base + val;
8103 0 : const unsigned char *strreadendp = data->d_buf + data->d_size;
8104 0 : if ((size_t) (strreadendp - strreadp) < offset_len)
8105 : str = "???";
8106 : else
8107 : {
8108 : Dwarf_Off idx;
8109 0 : if (offset_len == 8)
8110 0 : idx = read_8ubyte_unaligned (dbg, strreadp);
8111 : else
8112 0 : idx = read_4ubyte_unaligned (dbg, strreadp);
8113 :
8114 0 : data = dbg->sectiondata[IDX_debug_str];
8115 0 : if (data == NULL || idx >= data->d_size
8116 0 : || memchr (data->d_buf + idx, '\0',
8117 : data->d_size - idx) == NULL)
8118 : str = "???";
8119 : else
8120 0 : str = (char *) data->d_buf + idx;
8121 : }
8122 : }
8123 : printf ("%s (%" PRIu64 ")", str, val);
8124 : break;
8125 :
8126 : case DW_FORM_strx1:
8127 0 : if (readendp - readp < 1)
8128 : goto invalid_data;
8129 0 : val = *readp++;
8130 0 : goto strx_val;
8131 :
8132 : case DW_FORM_strx2:
8133 0 : if (readendp - readp < 2)
8134 : goto invalid_data;
8135 0 : val = read_2ubyte_unaligned_inc (dbg, readp);
8136 0 : goto strx_val;
8137 :
8138 : case DW_FORM_strx3:
8139 0 : if (readendp - readp < 3)
8140 : goto invalid_data;
8141 0 : val = read_3ubyte_unaligned_inc (dbg, readp);
8142 0 : goto strx_val;
8143 :
8144 : case DW_FORM_strx4:
8145 0 : if (readendp - readp < 4)
8146 : goto invalid_data;
8147 0 : val = read_4ubyte_unaligned_inc (dbg, readp);
8148 0 : goto strx_val;
8149 :
8150 : default:
8151 0 : error (0, 0, gettext ("unknown form: %s"), dwarf_form_name (form));
8152 0 : return readendp;
8153 : }
8154 :
8155 20 : return readp;
8156 : }
8157 :
8158 : static void
8159 124 : print_debug_line_section (Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr,
8160 40 : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
8161 : {
8162 44 : if (decodedline)
8163 : {
8164 4 : print_decoded_line_section (dwflmod, ebl, ehdr, scn, shdr, dbg);
8165 4 : return;
8166 : }
8167 :
8168 200 : printf (gettext ("\
8169 : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
8170 : elf_ndxscn (scn), section_name (ebl, ehdr, shdr),
8171 : (uint64_t) shdr->sh_offset);
8172 :
8173 40 : if (shdr->sh_size == 0)
8174 : return;
8175 :
8176 : /* There is no functionality in libdw to read the information in the
8177 : way it is represented here. Hardcode the decoder. */
8178 80 : Elf_Data *data = (dbg->sectiondata[IDX_debug_line]
8179 40 : ?: elf_rawdata (scn, NULL));
8180 40 : if (unlikely (data == NULL))
8181 : {
8182 0 : error (0, 0, gettext ("cannot get line data section data: %s"),
8183 : elf_errmsg (-1));
8184 : return;
8185 : }
8186 :
8187 40 : const unsigned char *linep = (const unsigned char *) data->d_buf;
8188 : const unsigned char *lineendp;
8189 :
8190 1165 : while (linep
8191 1125 : < (lineendp = (const unsigned char *) data->d_buf + data->d_size))
8192 : {
8193 1085 : size_t start_offset = linep - (const unsigned char *) data->d_buf;
8194 :
8195 1085 : printf (gettext ("\nTable at offset %zu:\n"), start_offset);
8196 :
8197 1085 : if (unlikely (linep + 4 > lineendp))
8198 : goto invalid_data;
8199 2145 : Dwarf_Word unit_length = read_4ubyte_unaligned_inc (dbg, linep);
8200 1085 : unsigned int length = 4;
8201 1085 : if (unlikely (unit_length == 0xffffffff))
8202 : {
8203 0 : if (unlikely (linep + 8 > lineendp))
8204 : {
8205 : invalid_data:
8206 0 : error (0, 0, gettext ("invalid data in section [%zu] '%s'"),
8207 : elf_ndxscn (scn), section_name (ebl, ehdr, shdr));
8208 : return;
8209 : }
8210 0 : unit_length = read_8ubyte_unaligned_inc (dbg, linep);
8211 0 : length = 8;
8212 : }
8213 :
8214 : /* Check whether we have enough room in the section. */
8215 1085 : if (unlikely (unit_length > (size_t) (lineendp - linep)))
8216 : goto invalid_data;
8217 1085 : lineendp = linep + unit_length;
8218 :
8219 : /* The next element of the header is the version identifier. */
8220 1085 : if ((size_t) (lineendp - linep) < 2)
8221 : goto invalid_data;
8222 1085 : uint_fast16_t version = read_2ubyte_unaligned_inc (dbg, linep);
8223 :
8224 1085 : size_t address_size
8225 1085 : = elf_getident (ebl->elf, NULL)[EI_CLASS] == ELFCLASS32 ? 4 : 8;
8226 1085 : unsigned char segment_selector_size = 0;
8227 1085 : if (version > 4)
8228 : {
8229 2 : if ((size_t) (lineendp - linep) < 2)
8230 : goto invalid_data;
8231 2 : address_size = *linep++;
8232 2 : segment_selector_size = *linep++;
8233 : }
8234 :
8235 : /* Next comes the header length. */
8236 : Dwarf_Word header_length;
8237 1085 : if (length == 4)
8238 : {
8239 1085 : if ((size_t) (lineendp - linep) < 4)
8240 : goto invalid_data;
8241 2145 : header_length = read_4ubyte_unaligned_inc (dbg, linep);
8242 : }
8243 : else
8244 : {
8245 0 : if ((size_t) (lineendp - linep) < 8)
8246 : goto invalid_data;
8247 0 : header_length = read_8ubyte_unaligned_inc (dbg, linep);
8248 : }
8249 :
8250 : /* Next the minimum instruction length. */
8251 1085 : if ((size_t) (lineendp - linep) < 1)
8252 : goto invalid_data;
8253 1085 : uint_fast8_t minimum_instr_len = *linep++;
8254 :
8255 : /* Next the maximum operations per instruction, in version 4 format. */
8256 : uint_fast8_t max_ops_per_instr;
8257 1085 : if (version < 4)
8258 1075 : max_ops_per_instr = 1;
8259 : else
8260 : {
8261 10 : if ((size_t) (lineendp - linep) < 1)
8262 : goto invalid_data;
8263 10 : max_ops_per_instr = *linep++;
8264 : }
8265 :
8266 : /* We need at least 4 more bytes. */
8267 1085 : if ((size_t) (lineendp - linep) < 4)
8268 : goto invalid_data;
8269 :
8270 : /* Then the flag determining the default value of the is_stmt
8271 : register. */
8272 1085 : uint_fast8_t default_is_stmt = *linep++;
8273 :
8274 : /* Now the line base. */
8275 1085 : int_fast8_t line_base = *linep++;
8276 :
8277 : /* And the line range. */
8278 1085 : uint_fast8_t line_range = *linep++;
8279 :
8280 : /* The opcode base. */
8281 1085 : uint_fast8_t opcode_base = *linep++;
8282 :
8283 : /* Print what we got so far. */
8284 1085 : printf (gettext ("\n"
8285 : " Length: %" PRIu64 "\n"
8286 : " DWARF version: %" PRIuFAST16 "\n"
8287 : " Prologue length: %" PRIu64 "\n"
8288 : " Address size: %zd\n"
8289 : " Segment selector size: %zd\n"
8290 : " Min instruction length: %" PRIuFAST8 "\n"
8291 : " Max operations per instruction: %" PRIuFAST8 "\n"
8292 : " Initial value if 'is_stmt': %" PRIuFAST8 "\n"
8293 : " Line base: %" PRIdFAST8 "\n"
8294 : " Line range: %" PRIuFAST8 "\n"
8295 : " Opcode base: %" PRIuFAST8 "\n"
8296 : "\n"
8297 : "Opcodes:\n"),
8298 : (uint64_t) unit_length, version, (uint64_t) header_length,
8299 : address_size, (size_t) segment_selector_size,
8300 : minimum_instr_len, max_ops_per_instr,
8301 : default_is_stmt, line_base,
8302 : line_range, opcode_base);
8303 :
8304 1085 : if (version < 2 || version > 5)
8305 : {
8306 0 : error (0, 0, gettext ("cannot handle .debug_line version: %u\n"),
8307 : (unsigned int) version);
8308 0 : linep = lineendp;
8309 0 : continue;
8310 : }
8311 :
8312 1085 : if (address_size != 4 && address_size != 8)
8313 : {
8314 0 : error (0, 0, gettext ("cannot handle address size: %u\n"),
8315 : (unsigned int) address_size);
8316 0 : linep = lineendp;
8317 0 : continue;
8318 : }
8319 :
8320 1085 : if (segment_selector_size != 0)
8321 : {
8322 0 : error (0, 0, gettext ("cannot handle segment selector size: %u\n"),
8323 : (unsigned int) segment_selector_size);
8324 0 : linep = lineendp;
8325 0 : continue;
8326 : }
8327 :
8328 1085 : if (unlikely (linep + opcode_base - 1 >= lineendp))
8329 : {
8330 : invalid_unit:
8331 0 : error (0, 0,
8332 0 : gettext ("invalid data at offset %tu in section [%zu] '%s'"),
8333 0 : linep - (const unsigned char *) data->d_buf,
8334 : elf_ndxscn (scn), section_name (ebl, ehdr, shdr));
8335 0 : linep = lineendp;
8336 0 : continue;
8337 : }
8338 1085 : int opcode_base_l10 = 1;
8339 1085 : unsigned int tmp = opcode_base;
8340 3253 : while (tmp > 10)
8341 : {
8342 1083 : tmp /= 10;
8343 1083 : ++opcode_base_l10;
8344 : }
8345 1085 : const uint8_t *standard_opcode_lengths = linep - 1;
8346 14099 : for (uint_fast8_t cnt = 1; cnt < opcode_base; ++cnt)
8347 13014 : printf (ngettext (" [%*" PRIuFAST8 "] %hhu argument\n",
8348 : " [%*" PRIuFAST8 "] %hhu arguments\n",
8349 : (int) linep[cnt - 1]),
8350 13014 : opcode_base_l10, cnt, linep[cnt - 1]);
8351 1085 : linep += opcode_base - 1;
8352 :
8353 1085 : if (unlikely (linep >= lineendp))
8354 : goto invalid_unit;
8355 :
8356 1085 : Dwarf_Off str_offsets_base = str_offsets_base_off (dbg, NULL);
8357 :
8358 1085 : puts (gettext ("\nDirectory table:"));
8359 1085 : if (version > 4)
8360 : {
8361 : struct encpair { uint16_t desc; uint16_t form; };
8362 : struct encpair enc[256];
8363 :
8364 2 : printf (gettext (" ["));
8365 2 : if ((size_t) (lineendp - linep) < 1)
8366 : goto invalid_data;
8367 2 : unsigned char directory_entry_format_count = *linep++;
8368 4 : for (int i = 0; i < directory_entry_format_count; i++)
8369 : {
8370 : uint16_t desc, form;
8371 2 : if ((size_t) (lineendp - linep) < 1)
8372 : goto invalid_data;
8373 2 : get_uleb128 (desc, linep, lineendp);
8374 2 : if ((size_t) (lineendp - linep) < 1)
8375 : goto invalid_data;
8376 2 : get_uleb128 (form, linep, lineendp);
8377 :
8378 2 : enc[i].desc = desc;
8379 2 : enc[i].form = form;
8380 :
8381 2 : printf ("%s(%s)",
8382 : dwarf_line_content_description_name (desc),
8383 : dwarf_form_name (form));
8384 2 : if (i + 1 < directory_entry_format_count)
8385 : printf (", ");
8386 : }
8387 : printf ("]\n");
8388 :
8389 : uint64_t directories_count;
8390 2 : if ((size_t) (lineendp - linep) < 1)
8391 : goto invalid_data;
8392 2 : get_uleb128 (directories_count, linep, lineendp);
8393 :
8394 4 : if (directory_entry_format_count == 0
8395 2 : && directories_count != 0)
8396 : goto invalid_data;
8397 :
8398 4 : for (uint64_t i = 0; i < directories_count; i++)
8399 : {
8400 : printf (" %-5" PRIu64 " ", i);
8401 8 : for (int j = 0; j < directory_entry_format_count; j++)
8402 : {
8403 4 : linep = print_form_data (dbg, enc[j].form,
8404 : linep, lineendp, length,
8405 : str_offsets_base);
8406 4 : if (j + 1 < directory_entry_format_count)
8407 : printf (", ");
8408 : }
8409 : printf ("\n");
8410 4 : if (linep >= lineendp)
8411 : goto invalid_unit;
8412 : }
8413 : }
8414 : else
8415 : {
8416 7584 : while (*linep != 0)
8417 : {
8418 6501 : unsigned char *endp = memchr (linep, '\0', lineendp - linep);
8419 6501 : if (unlikely (endp == NULL))
8420 : goto invalid_unit;
8421 :
8422 6501 : printf (" %s\n", (char *) linep);
8423 :
8424 6501 : linep = endp + 1;
8425 : }
8426 : /* Skip the final NUL byte. */
8427 1083 : ++linep;
8428 : }
8429 :
8430 1085 : if (unlikely (linep >= lineendp))
8431 : goto invalid_unit;
8432 :
8433 1085 : puts (gettext ("\nFile name table:"));
8434 1085 : if (version > 4)
8435 : {
8436 : struct encpair { uint16_t desc; uint16_t form; };
8437 : struct encpair enc[256];
8438 :
8439 2 : printf (gettext (" ["));
8440 2 : if ((size_t) (lineendp - linep) < 1)
8441 : goto invalid_data;
8442 2 : unsigned char file_name_format_count = *linep++;
8443 6 : for (int i = 0; i < file_name_format_count; i++)
8444 : {
8445 : uint64_t desc, form;
8446 4 : if ((size_t) (lineendp - linep) < 1)
8447 : goto invalid_data;
8448 4 : get_uleb128 (desc, linep, lineendp);
8449 4 : if ((size_t) (lineendp - linep) < 1)
8450 : goto invalid_data;
8451 4 : get_uleb128 (form, linep, lineendp);
8452 :
8453 4 : if (! libdw_valid_user_form (form))
8454 : goto invalid_data;
8455 :
8456 4 : enc[i].desc = desc;
8457 4 : enc[i].form = form;
8458 :
8459 4 : printf ("%s(%s)",
8460 : dwarf_line_content_description_name (desc),
8461 : dwarf_form_name (form));
8462 4 : if (i + 1 < file_name_format_count)
8463 : printf (", ");
8464 : }
8465 : printf ("]\n");
8466 :
8467 : uint64_t file_name_count;
8468 2 : if ((size_t) (lineendp - linep) < 1)
8469 : goto invalid_data;
8470 2 : get_uleb128 (file_name_count, linep, lineendp);
8471 :
8472 4 : if (file_name_format_count == 0
8473 2 : && file_name_count != 0)
8474 : goto invalid_data;
8475 :
8476 8 : for (uint64_t i = 0; i < file_name_count; i++)
8477 : {
8478 : printf (" %-5" PRIu64 " ", i);
8479 24 : for (int j = 0; j < file_name_format_count; j++)
8480 : {
8481 16 : linep = print_form_data (dbg, enc[j].form,
8482 : linep, lineendp, length,
8483 : str_offsets_base);
8484 16 : if (j + 1 < file_name_format_count)
8485 : printf (", ");
8486 : }
8487 : printf ("\n");
8488 8 : if (linep >= lineendp)
8489 : goto invalid_unit;
8490 : }
8491 : }
8492 : else
8493 : {
8494 1083 : puts (gettext (" Entry Dir Time Size Name"));
8495 18871 : for (unsigned int cnt = 1; *linep != 0; ++cnt)
8496 : {
8497 : /* First comes the file name. */
8498 17788 : char *fname = (char *) linep;
8499 17788 : unsigned char *endp = memchr (fname, '\0', lineendp - linep);
8500 17788 : if (unlikely (endp == NULL))
8501 : goto invalid_unit;
8502 17788 : linep = endp + 1;
8503 :
8504 : /* Then the index. */
8505 : unsigned int diridx;
8506 17788 : if (lineendp - linep < 1)
8507 : goto invalid_unit;
8508 17788 : get_uleb128 (diridx, linep, lineendp);
8509 :
8510 : /* Next comes the modification time. */
8511 : unsigned int mtime;
8512 17788 : if (lineendp - linep < 1)
8513 : goto invalid_unit;
8514 17788 : get_uleb128 (mtime, linep, lineendp);
8515 :
8516 : /* Finally the length of the file. */
8517 : unsigned int fsize;
8518 17788 : if (lineendp - linep < 1)
8519 : goto invalid_unit;
8520 17788 : get_uleb128 (fsize, linep, lineendp);
8521 :
8522 : printf (" %-5u %-5u %-9u %-9u %s\n",
8523 : cnt, diridx, mtime, fsize, fname);
8524 : }
8525 : /* Skip the final NUL byte. */
8526 1083 : ++linep;
8527 : }
8528 :
8529 1085 : puts (gettext ("\nLine number statements:"));
8530 1085 : Dwarf_Word address = 0;
8531 1085 : unsigned int op_index = 0;
8532 1085 : size_t line = 1;
8533 1085 : uint_fast8_t is_stmt = default_is_stmt;
8534 :
8535 : /* Apply the "operation advance" from a special opcode
8536 : or DW_LNS_advance_pc (as per DWARF4 6.2.5.1). */
8537 : unsigned int op_addr_advance;
8538 : bool show_op_index;
8539 145327 : inline void advance_pc (unsigned int op_advance)
8540 : {
8541 290654 : op_addr_advance = minimum_instr_len * ((op_index + op_advance)
8542 145327 : / max_ops_per_instr);
8543 145327 : address += op_addr_advance;
8544 290654 : show_op_index = (op_index > 0 ||
8545 145327 : (op_index + op_advance) % max_ops_per_instr > 0);
8546 145327 : op_index = (op_index + op_advance) % max_ops_per_instr;
8547 145327 : }
8548 :
8549 1085 : if (max_ops_per_instr == 0)
8550 : {
8551 : error (0, 0,
8552 0 : gettext ("invalid maximum operations per instruction is zero"));
8553 0 : linep = lineendp;
8554 0 : continue;
8555 : }
8556 :
8557 237786 : while (linep < lineendp)
8558 : {
8559 236701 : size_t offset = linep - (const unsigned char *) data->d_buf;
8560 : unsigned int u128;
8561 : int s128;
8562 :
8563 : /* Read the opcode. */
8564 236701 : unsigned int opcode = *linep++;
8565 :
8566 : printf (" [%6" PRIx64 "]", (uint64_t)offset);
8567 : /* Is this a special opcode? */
8568 236701 : if (likely (opcode >= opcode_base))
8569 : {
8570 108901 : if (unlikely (line_range == 0))
8571 : goto invalid_unit;
8572 :
8573 : /* Yes. Handling this is quite easy since the opcode value
8574 : is computed with
8575 :
8576 : opcode = (desired line increment - line_base)
8577 : + (line_range * address advance) + opcode_base
8578 : */
8579 108901 : int line_increment = (line_base
8580 108901 : + (opcode - opcode_base) % line_range);
8581 :
8582 : /* Perform the increments. */
8583 108901 : line += line_increment;
8584 108901 : advance_pc ((opcode - opcode_base) / line_range);
8585 :
8586 108901 : printf (gettext (" special opcode %u: address+%u = "),
8587 : opcode, op_addr_advance);
8588 108901 : print_dwarf_addr (dwflmod, 0, address, address);
8589 108901 : if (show_op_index)
8590 0 : printf (gettext (", op_index = %u, line%+d = %zu\n"),
8591 : op_index, line_increment, line);
8592 : else
8593 108901 : printf (gettext (", line%+d = %zu\n"),
8594 : line_increment, line);
8595 : }
8596 127800 : else if (opcode == 0)
8597 : {
8598 : /* This an extended opcode. */
8599 14066 : if (unlikely (linep + 2 > lineendp))
8600 : goto invalid_unit;
8601 :
8602 : /* The length. */
8603 14066 : unsigned int len = *linep++;
8604 :
8605 14066 : if (unlikely (linep + len > lineendp))
8606 : goto invalid_unit;
8607 :
8608 : /* The sub-opcode. */
8609 14066 : opcode = *linep++;
8610 :
8611 14066 : printf (gettext (" extended opcode %u: "), opcode);
8612 :
8613 14066 : switch (opcode)
8614 : {
8615 : case DW_LNE_end_sequence:
8616 1807 : puts (gettext (" end of sequence"));
8617 :
8618 : /* Reset the registers we care about. */
8619 1807 : address = 0;
8620 1807 : op_index = 0;
8621 1807 : line = 1;
8622 1807 : is_stmt = default_is_stmt;
8623 1807 : break;
8624 :
8625 : case DW_LNE_set_address:
8626 1935 : op_index = 0;
8627 1935 : if (unlikely ((size_t) (lineendp - linep) < address_size))
8628 : goto invalid_unit;
8629 1935 : if (address_size == 4)
8630 26 : address = read_4ubyte_unaligned_inc (dbg, linep);
8631 : else
8632 3827 : address = read_8ubyte_unaligned_inc (dbg, linep);
8633 : {
8634 1935 : printf (gettext (" set address to "));
8635 1935 : print_dwarf_addr (dwflmod, 0, address, address);
8636 : printf ("\n");
8637 : }
8638 : break;
8639 :
8640 : case DW_LNE_define_file:
8641 : {
8642 0 : char *fname = (char *) linep;
8643 0 : unsigned char *endp = memchr (linep, '\0',
8644 0 : lineendp - linep);
8645 0 : if (unlikely (endp == NULL))
8646 : goto invalid_unit;
8647 0 : linep = endp + 1;
8648 :
8649 : unsigned int diridx;
8650 0 : if (lineendp - linep < 1)
8651 : goto invalid_unit;
8652 0 : get_uleb128 (diridx, linep, lineendp);
8653 : Dwarf_Word mtime;
8654 0 : if (lineendp - linep < 1)
8655 : goto invalid_unit;
8656 0 : get_uleb128 (mtime, linep, lineendp);
8657 : Dwarf_Word filelength;
8658 0 : if (lineendp - linep < 1)
8659 : goto invalid_unit;
8660 0 : get_uleb128 (filelength, linep, lineendp);
8661 :
8662 0 : printf (gettext ("\
8663 : define new file: dir=%u, mtime=%" PRIu64 ", length=%" PRIu64 ", name=%s\n"),
8664 : diridx, (uint64_t) mtime, (uint64_t) filelength,
8665 : fname);
8666 : }
8667 : break;
8668 :
8669 : case DW_LNE_set_discriminator:
8670 : /* Takes one ULEB128 parameter, the discriminator. */
8671 10324 : if (unlikely (standard_opcode_lengths[opcode] != 1))
8672 : goto invalid_unit;
8673 :
8674 10324 : get_uleb128 (u128, linep, lineendp);
8675 10324 : printf (gettext (" set discriminator to %u\n"), u128);
8676 : break;
8677 :
8678 : default:
8679 : /* Unknown, ignore it. */
8680 0 : puts (gettext (" unknown opcode"));
8681 0 : linep += len - 1;
8682 0 : break;
8683 : }
8684 : }
8685 113734 : else if (opcode <= DW_LNS_set_isa)
8686 : {
8687 : /* This is a known standard opcode. */
8688 113734 : switch (opcode)
8689 : {
8690 : case DW_LNS_copy:
8691 : /* Takes no argument. */
8692 8158 : puts (gettext (" copy"));
8693 8158 : break;
8694 :
8695 : case DW_LNS_advance_pc:
8696 : /* Takes one uleb128 parameter which is added to the
8697 : address. */
8698 10984 : get_uleb128 (u128, linep, lineendp);
8699 10984 : advance_pc (u128);
8700 : {
8701 10984 : printf (gettext (" advance address by %u to "),
8702 : op_addr_advance);
8703 10984 : print_dwarf_addr (dwflmod, 0, address, address);
8704 10984 : if (show_op_index)
8705 0 : printf (gettext (", op_index to %u"), op_index);
8706 : printf ("\n");
8707 : }
8708 : break;
8709 :
8710 : case DW_LNS_advance_line:
8711 : /* Takes one sleb128 parameter which is added to the
8712 : line. */
8713 45985 : get_sleb128 (s128, linep, lineendp);
8714 45985 : line += s128;
8715 45985 : printf (gettext ("\
8716 : advance line by constant %d to %" PRId64 "\n"),
8717 : s128, (int64_t) line);
8718 : break;
8719 :
8720 : case DW_LNS_set_file:
8721 : /* Takes one uleb128 parameter which is stored in file. */
8722 18610 : get_uleb128 (u128, linep, lineendp);
8723 18610 : printf (gettext (" set file to %" PRIu64 "\n"),
8724 : (uint64_t) u128);
8725 : break;
8726 :
8727 : case DW_LNS_set_column:
8728 : /* Takes one uleb128 parameter which is stored in column. */
8729 111 : if (unlikely (standard_opcode_lengths[opcode] != 1))
8730 : goto invalid_unit;
8731 :
8732 111 : get_uleb128 (u128, linep, lineendp);
8733 111 : printf (gettext (" set column to %" PRIu64 "\n"),
8734 : (uint64_t) u128);
8735 : break;
8736 :
8737 : case DW_LNS_negate_stmt:
8738 : /* Takes no argument. */
8739 4443 : is_stmt = 1 - is_stmt;
8740 4443 : printf (gettext (" set '%s' to %" PRIuFAST8 "\n"),
8741 : "is_stmt", is_stmt);
8742 : break;
8743 :
8744 : case DW_LNS_set_basic_block:
8745 : /* Takes no argument. */
8746 0 : puts (gettext (" set basic block flag"));
8747 0 : break;
8748 :
8749 : case DW_LNS_const_add_pc:
8750 : /* Takes no argument. */
8751 :
8752 25442 : if (unlikely (line_range == 0))
8753 : goto invalid_unit;
8754 :
8755 25442 : advance_pc ((255 - opcode_base) / line_range);
8756 : {
8757 25442 : printf (gettext (" advance address by constant %u to "),
8758 : op_addr_advance);
8759 25442 : print_dwarf_addr (dwflmod, 0, address, address);
8760 25442 : if (show_op_index)
8761 0 : printf (gettext (", op_index to %u"), op_index);
8762 : printf ("\n");
8763 : }
8764 : break;
8765 :
8766 : case DW_LNS_fixed_advance_pc:
8767 : /* Takes one 16 bit parameter which is added to the
8768 : address. */
8769 0 : if (unlikely (standard_opcode_lengths[opcode] != 1))
8770 : goto invalid_unit;
8771 :
8772 0 : u128 = read_2ubyte_unaligned_inc (dbg, linep);
8773 0 : address += u128;
8774 0 : op_index = 0;
8775 : {
8776 0 : printf (gettext ("\
8777 : advance address by fixed value %u to \n"),
8778 : u128);
8779 0 : print_dwarf_addr (dwflmod, 0, address, address);
8780 : printf ("\n");
8781 : }
8782 : break;
8783 :
8784 : case DW_LNS_set_prologue_end:
8785 : /* Takes no argument. */
8786 1 : puts (gettext (" set prologue end flag"));
8787 1 : break;
8788 :
8789 : case DW_LNS_set_epilogue_begin:
8790 : /* Takes no argument. */
8791 0 : puts (gettext (" set epilogue begin flag"));
8792 0 : break;
8793 :
8794 : case DW_LNS_set_isa:
8795 : /* Takes one uleb128 parameter which is stored in isa. */
8796 0 : if (unlikely (standard_opcode_lengths[opcode] != 1))
8797 : goto invalid_unit;
8798 :
8799 0 : get_uleb128 (u128, linep, lineendp);
8800 0 : printf (gettext (" set isa to %u\n"), u128);
8801 : break;
8802 : }
8803 : }
8804 : else
8805 : {
8806 : /* This is a new opcode the generator but not we know about.
8807 : Read the parameters associated with it but then discard
8808 : everything. Read all the parameters for this opcode. */
8809 0 : printf (ngettext (" unknown opcode with %" PRIu8 " parameter:",
8810 : " unknown opcode with %" PRIu8 " parameters:",
8811 : standard_opcode_lengths[opcode]),
8812 0 : standard_opcode_lengths[opcode]);
8813 0 : for (int n = standard_opcode_lengths[opcode]; n > 0; --n)
8814 : {
8815 0 : get_uleb128 (u128, linep, lineendp);
8816 0 : if (n != standard_opcode_lengths[opcode])
8817 0 : putc_unlocked (',', stdout);
8818 : printf (" %u", u128);
8819 : }
8820 :
8821 : /* Next round, ignore this opcode. */
8822 0 : continue;
8823 : }
8824 : }
8825 : }
8826 :
8827 : /* There must only be one data block. */
8828 40 : assert (elf_getdata (scn, data) == NULL);
8829 : }
8830 :
8831 :
8832 : static void
8833 3 : print_debug_loclists_section (Dwfl_Module *dwflmod,
8834 6 : Ebl *ebl, GElf_Ehdr *ehdr,
8835 3 : Elf_Scn *scn, GElf_Shdr *shdr,
8836 : Dwarf *dbg)
8837 : {
8838 15 : printf (gettext ("\
8839 : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
8840 : elf_ndxscn (scn), section_name (ebl, ehdr, shdr),
8841 : (uint64_t) shdr->sh_offset);
8842 :
8843 6 : Elf_Data *data = (dbg->sectiondata[IDX_debug_loclists]
8844 3 : ?: elf_rawdata (scn, NULL));
8845 3 : if (unlikely (data == NULL))
8846 : {
8847 0 : error (0, 0, gettext ("cannot get .debug_loclists content: %s"),
8848 : elf_errmsg (-1));
8849 0 : return;
8850 : }
8851 :
8852 : /* For the listptr to get the base address/CU. */
8853 3 : sort_listptr (&known_loclistsptr, "loclistsptr");
8854 3 : size_t listptr_idx = 0;
8855 :
8856 3 : const unsigned char *readp = data->d_buf;
8857 3 : const unsigned char *const dataend = ((unsigned char *) data->d_buf
8858 3 : + data->d_size);
8859 10 : while (readp < dataend)
8860 : {
8861 4 : if (unlikely (readp > dataend - 4))
8862 : {
8863 : invalid_data:
8864 0 : error (0, 0, gettext ("invalid data in section [%zu] '%s'"),
8865 : elf_ndxscn (scn), section_name (ebl, ehdr, shdr));
8866 0 : return;
8867 : }
8868 :
8869 4 : ptrdiff_t offset = readp - (unsigned char *) data->d_buf;
8870 4 : printf (gettext ("Table at Offset 0x%" PRIx64 ":\n\n"),
8871 : (uint64_t) offset);
8872 :
8873 8 : uint64_t unit_length = read_4ubyte_unaligned_inc (dbg, readp);
8874 4 : unsigned int offset_size = 4;
8875 4 : if (unlikely (unit_length == 0xffffffff))
8876 : {
8877 0 : if (unlikely (readp > dataend - 8))
8878 : goto invalid_data;
8879 :
8880 0 : unit_length = read_8ubyte_unaligned_inc (dbg, readp);
8881 0 : offset_size = 8;
8882 : }
8883 4 : printf (gettext (" Length: %8" PRIu64 "\n"), unit_length);
8884 :
8885 : /* We need at least 2-bytes + 1-byte + 1-byte + 4-bytes = 8
8886 : bytes to complete the header. And this unit cannot go beyond
8887 : the section data. */
8888 4 : if (readp > dataend - 8
8889 4 : || unit_length < 8
8890 4 : || unit_length > (uint64_t) (dataend - readp))
8891 : goto invalid_data;
8892 :
8893 4 : const unsigned char *nexthdr = readp + unit_length;
8894 :
8895 4 : uint16_t version = read_2ubyte_unaligned_inc (dbg, readp);
8896 4 : printf (gettext (" DWARF version: %8" PRIu16 "\n"), version);
8897 :
8898 4 : if (version != 5)
8899 : {
8900 0 : error (0, 0, gettext ("Unknown version"));
8901 : goto next_table;
8902 : }
8903 :
8904 4 : uint8_t address_size = *readp++;
8905 4 : printf (gettext (" Address size: %8" PRIu64 "\n"),
8906 : (uint64_t) address_size);
8907 :
8908 4 : if (address_size != 4 && address_size != 8)
8909 : {
8910 0 : error (0, 0, gettext ("unsupported address size"));
8911 : goto next_table;
8912 : }
8913 :
8914 4 : uint8_t segment_size = *readp++;
8915 4 : printf (gettext (" Segment size: %8" PRIu64 "\n"),
8916 : (uint64_t) segment_size);
8917 :
8918 4 : if (segment_size != 0)
8919 : {
8920 0 : error (0, 0, gettext ("unsupported segment size"));
8921 : goto next_table;
8922 : }
8923 :
8924 8 : uint32_t offset_entry_count = read_4ubyte_unaligned_inc (dbg, readp);
8925 4 : printf (gettext (" Offset entries: %8" PRIu64 "\n"),
8926 : (uint64_t) offset_entry_count);
8927 :
8928 : /* We need the CU that uses this unit to get the initial base address. */
8929 4 : Dwarf_Addr cu_base = 0;
8930 4 : struct Dwarf_CU *cu = NULL;
8931 4 : if (listptr_cu (&known_loclistsptr, &listptr_idx,
8932 : (Dwarf_Off) offset,
8933 4 : (Dwarf_Off) (nexthdr - (unsigned char *) data->d_buf),
8934 : &cu_base, &cu)
8935 0 : || split_dwarf_cu_base (dbg, &cu, &cu_base))
8936 4 : {
8937 : Dwarf_Die cudie;
8938 4 : if (dwarf_cu_die (cu, &cudie,
8939 : NULL, NULL, NULL, NULL,
8940 : NULL, NULL) == NULL)
8941 0 : printf (gettext (" Unknown CU base: "));
8942 : else
8943 4 : printf (gettext (" CU [%6" PRIx64 "] base: "),
8944 : dwarf_dieoffset (&cudie));
8945 4 : print_dwarf_addr (dwflmod, address_size, cu_base, cu_base);
8946 : printf ("\n");
8947 : }
8948 : else
8949 0 : printf (gettext (" Not associated with a CU.\n"));
8950 :
8951 : printf ("\n");
8952 :
8953 4 : const unsigned char *offset_array_start = readp;
8954 4 : if (offset_entry_count > 0)
8955 : {
8956 2 : uint64_t max_entries = (unit_length - 8) / offset_size;
8957 2 : if (offset_entry_count > max_entries)
8958 : {
8959 : error (0, 0,
8960 0 : gettext ("too many offset entries for unit length"));
8961 0 : offset_entry_count = max_entries;
8962 : }
8963 :
8964 2 : printf (gettext (" Offsets starting at 0x%" PRIx64 ":\n"),
8965 : (uint64_t) (offset_array_start
8966 2 : - (unsigned char *) data->d_buf));
8967 18 : for (uint32_t idx = 0; idx < offset_entry_count; idx++)
8968 : {
8969 : printf (" [%6" PRIu32 "] ", idx);
8970 16 : if (offset_size == 4)
8971 : {
8972 32 : uint32_t off = read_4ubyte_unaligned_inc (dbg, readp);
8973 : printf ("0x%" PRIx32 "\n", off);
8974 : }
8975 : else
8976 : {
8977 0 : uint64_t off = read_8ubyte_unaligned_inc (dbg, readp);
8978 : printf ("0x%" PRIx64 "\n", off);
8979 : }
8980 : }
8981 : printf ("\n");
8982 : }
8983 :
8984 4 : Dwarf_Addr base = cu_base;
8985 4 : bool start_of_list = true;
8986 97 : while (readp < nexthdr)
8987 : {
8988 89 : uint8_t kind = *readp++;
8989 : uint64_t op1, op2, len;
8990 :
8991 : /* Skip padding. */
8992 89 : if (start_of_list && kind == DW_LLE_end_of_list)
8993 0 : continue;
8994 :
8995 89 : if (start_of_list)
8996 : {
8997 32 : base = cu_base;
8998 64 : printf (" Offset: %" PRIx64 ", Index: %" PRIx64 "\n",
8999 32 : (uint64_t) (readp - (unsigned char *) data->d_buf - 1),
9000 32 : (uint64_t) (readp - offset_array_start - 1));
9001 32 : start_of_list = false;
9002 : }
9003 :
9004 89 : printf (" %s", dwarf_loc_list_encoding_name (kind));
9005 89 : switch (kind)
9006 : {
9007 : case DW_LLE_end_of_list:
9008 32 : start_of_list = true;
9009 : printf ("\n\n");
9010 : break;
9011 :
9012 : case DW_LLE_base_addressx:
9013 0 : if ((uint64_t) (nexthdr - readp) < 1)
9014 : {
9015 : invalid_entry:
9016 0 : error (0, 0, gettext ("invalid loclists data"));
9017 : goto next_table;
9018 : }
9019 0 : get_uleb128 (op1, readp, nexthdr);
9020 : printf (" %" PRIx64 "\n", op1);
9021 0 : if (! print_unresolved_addresses)
9022 : {
9023 : Dwarf_Addr addr;
9024 0 : if (get_indexed_addr (cu, op1, &addr) != 0)
9025 : printf (" ???\n");
9026 : else
9027 : {
9028 : printf (" ");
9029 0 : print_dwarf_addr (dwflmod, address_size, addr, addr);
9030 : printf ("\n");
9031 : }
9032 : }
9033 : break;
9034 :
9035 : case DW_LLE_startx_endx:
9036 0 : if ((uint64_t) (nexthdr - readp) < 1)
9037 : goto invalid_entry;
9038 0 : get_uleb128 (op1, readp, nexthdr);
9039 0 : if ((uint64_t) (nexthdr - readp) < 1)
9040 : goto invalid_entry;
9041 0 : get_uleb128 (op2, readp, nexthdr);
9042 : printf (" %" PRIx64 ", %" PRIx64 "\n", op1, op2);
9043 0 : if (! print_unresolved_addresses)
9044 : {
9045 : Dwarf_Addr addr1;
9046 : Dwarf_Addr addr2;
9047 0 : if (get_indexed_addr (cu, op1, &addr1) != 0
9048 0 : || get_indexed_addr (cu, op2, &addr2) != 0)
9049 : {
9050 : printf (" ???..\n");
9051 : printf (" ???\n");
9052 : }
9053 : else
9054 : {
9055 : printf (" ");
9056 0 : print_dwarf_addr (dwflmod, address_size, addr1, addr1);
9057 : printf ("..\n ");
9058 0 : print_dwarf_addr (dwflmod, address_size,
9059 : addr2 - 1, addr2);
9060 : printf ("\n");
9061 : }
9062 : }
9063 0 : if ((uint64_t) (nexthdr - readp) < 1)
9064 : goto invalid_entry;
9065 0 : get_uleb128 (len, readp, nexthdr);
9066 0 : if ((uint64_t) (nexthdr - readp) < len)
9067 : goto invalid_entry;
9068 0 : print_ops (dwflmod, dbg, 8, 8, version,
9069 : address_size, offset_size, cu, len, readp);
9070 0 : readp += len;
9071 0 : break;
9072 :
9073 : case DW_LLE_startx_length:
9074 26 : if ((uint64_t) (nexthdr - readp) < 1)
9075 : goto invalid_entry;
9076 26 : get_uleb128 (op1, readp, nexthdr);
9077 26 : if ((uint64_t) (nexthdr - readp) < 1)
9078 : goto invalid_entry;
9079 26 : get_uleb128 (op2, readp, nexthdr);
9080 : printf (" %" PRIx64 ", %" PRIx64 "\n", op1, op2);
9081 26 : if (! print_unresolved_addresses)
9082 : {
9083 : Dwarf_Addr addr1;
9084 : Dwarf_Addr addr2;
9085 26 : if (get_indexed_addr (cu, op1, &addr1) != 0)
9086 : {
9087 : printf (" ???..\n");
9088 : printf (" ???\n");
9089 : }
9090 : else
9091 : {
9092 26 : addr2 = addr1 + op2;
9093 : printf (" ");
9094 26 : print_dwarf_addr (dwflmod, address_size, addr1, addr1);
9095 : printf ("..\n ");
9096 26 : print_dwarf_addr (dwflmod, address_size,
9097 : addr2 - 1, addr2);
9098 : printf ("\n");
9099 : }
9100 : }
9101 26 : if ((uint64_t) (nexthdr - readp) < 1)
9102 : goto invalid_entry;
9103 26 : get_uleb128 (len, readp, nexthdr);
9104 26 : if ((uint64_t) (nexthdr - readp) < len)
9105 : goto invalid_entry;
9106 26 : print_ops (dwflmod, dbg, 8, 8, version,
9107 : address_size, offset_size, cu, len, readp);
9108 26 : readp += len;
9109 26 : break;
9110 :
9111 : case DW_LLE_offset_pair:
9112 24 : if ((uint64_t) (nexthdr - readp) < 1)
9113 : goto invalid_entry;
9114 24 : get_uleb128 (op1, readp, nexthdr);
9115 24 : if ((uint64_t) (nexthdr - readp) < 1)
9116 : goto invalid_entry;
9117 24 : get_uleb128 (op2, readp, nexthdr);
9118 : printf (" %" PRIx64 ", %" PRIx64 "\n", op1, op2);
9119 24 : if (! print_unresolved_addresses)
9120 : {
9121 24 : op1 += base;
9122 24 : op2 += base;
9123 : printf (" ");
9124 24 : print_dwarf_addr (dwflmod, address_size, op1, op1);
9125 : printf ("..\n ");
9126 24 : print_dwarf_addr (dwflmod, address_size, op2 - 1, op2);
9127 : printf ("\n");
9128 : }
9129 24 : if ((uint64_t) (nexthdr - readp) < 1)
9130 : goto invalid_entry;
9131 24 : get_uleb128 (len, readp, nexthdr);
9132 24 : if ((uint64_t) (nexthdr - readp) < len)
9133 : goto invalid_entry;
9134 24 : print_ops (dwflmod, dbg, 8, 8, version,
9135 : address_size, offset_size, cu, len, readp);
9136 24 : readp += len;
9137 24 : break;
9138 :
9139 : case DW_LLE_default_location:
9140 0 : if ((uint64_t) (nexthdr - readp) < 1)
9141 : goto invalid_entry;
9142 0 : get_uleb128 (len, readp, nexthdr);
9143 0 : if ((uint64_t) (nexthdr - readp) < len)
9144 : goto invalid_entry;
9145 0 : print_ops (dwflmod, dbg, 8, 8, version,
9146 : address_size, offset_size, cu, len, readp);
9147 0 : readp += len;
9148 0 : break;
9149 :
9150 : case DW_LLE_base_address:
9151 5 : if (address_size == 4)
9152 : {
9153 0 : if ((uint64_t) (nexthdr - readp) < 4)
9154 : goto invalid_entry;
9155 0 : op1 = read_4ubyte_unaligned_inc (dbg, readp);
9156 : }
9157 : else
9158 : {
9159 5 : if ((uint64_t) (nexthdr - readp) < 8)
9160 : goto invalid_entry;
9161 10 : op1 = read_8ubyte_unaligned_inc (dbg, readp);
9162 : }
9163 5 : base = op1;
9164 : printf (" 0x%" PRIx64 "\n", base);
9165 5 : if (! print_unresolved_addresses)
9166 : {
9167 : printf (" ");
9168 5 : print_dwarf_addr (dwflmod, address_size, base, base);
9169 : printf ("\n");
9170 : }
9171 : break;
9172 :
9173 : case DW_LLE_start_end:
9174 0 : if (address_size == 4)
9175 : {
9176 0 : if ((uint64_t) (nexthdr - readp) < 8)
9177 : goto invalid_entry;
9178 0 : op1 = read_4ubyte_unaligned_inc (dbg, readp);
9179 0 : op2 = read_4ubyte_unaligned_inc (dbg, readp);
9180 : }
9181 : else
9182 : {
9183 0 : if ((uint64_t) (nexthdr - readp) < 16)
9184 : goto invalid_entry;
9185 0 : op1 = read_8ubyte_unaligned_inc (dbg, readp);
9186 0 : op2 = read_8ubyte_unaligned_inc (dbg, readp);
9187 : }
9188 : printf (" 0x%" PRIx64 "..0x%" PRIx64 "\n", op1, op2);
9189 0 : if (! print_unresolved_addresses)
9190 : {
9191 : printf (" ");
9192 0 : print_dwarf_addr (dwflmod, address_size, op1, op1);
9193 : printf ("..\n ");
9194 0 : print_dwarf_addr (dwflmod, address_size, op2 - 1, op2);
9195 : printf ("\n");
9196 : }
9197 0 : if ((uint64_t) (nexthdr - readp) < 1)
9198 : goto invalid_entry;
9199 0 : get_uleb128 (len, readp, nexthdr);
9200 0 : if ((uint64_t) (nexthdr - readp) < len)
9201 : goto invalid_entry;
9202 0 : print_ops (dwflmod, dbg, 8, 8, version,
9203 : address_size, offset_size, cu, len, readp);
9204 0 : readp += len;
9205 0 : break;
9206 :
9207 : case DW_LLE_start_length:
9208 2 : if (address_size == 4)
9209 : {
9210 0 : if ((uint64_t) (nexthdr - readp) < 4)
9211 : goto invalid_entry;
9212 0 : op1 = read_4ubyte_unaligned_inc (dbg, readp);
9213 : }
9214 : else
9215 : {
9216 2 : if ((uint64_t) (nexthdr - readp) < 8)
9217 : goto invalid_entry;
9218 4 : op1 = read_8ubyte_unaligned_inc (dbg, readp);
9219 : }
9220 2 : if ((uint64_t) (nexthdr - readp) < 1)
9221 : goto invalid_entry;
9222 2 : get_uleb128 (op2, readp, nexthdr);
9223 : printf (" 0x%" PRIx64 ", %" PRIx64 "\n", op1, op2);
9224 2 : if (! print_unresolved_addresses)
9225 : {
9226 2 : op2 = op1 + op2;
9227 : printf (" ");
9228 2 : print_dwarf_addr (dwflmod, address_size, op1, op1);
9229 : printf ("..\n ");
9230 2 : print_dwarf_addr (dwflmod, address_size, op2 - 1, op2);
9231 : printf ("\n");
9232 : }
9233 2 : if ((uint64_t) (nexthdr - readp) < 1)
9234 : goto invalid_entry;
9235 2 : get_uleb128 (len, readp, nexthdr);
9236 2 : if ((uint64_t) (nexthdr - readp) < len)
9237 : goto invalid_entry;
9238 2 : print_ops (dwflmod, dbg, 8, 8, version,
9239 : address_size, offset_size, cu, len, readp);
9240 2 : readp += len;
9241 2 : break;
9242 :
9243 : default:
9244 : goto invalid_entry;
9245 : }
9246 : }
9247 :
9248 : next_table:
9249 4 : if (readp != nexthdr)
9250 : {
9251 0 : size_t padding = nexthdr - readp;
9252 0 : printf (gettext (" %zu padding bytes\n\n"), padding);
9253 0 : readp = nexthdr;
9254 : }
9255 : }
9256 : }
9257 :
9258 :
9259 : static void
9260 35 : print_debug_loc_section (Dwfl_Module *dwflmod,
9261 70 : Ebl *ebl, GElf_Ehdr *ehdr,
9262 35 : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
9263 : {
9264 70 : Elf_Data *data = (dbg->sectiondata[IDX_debug_loc]
9265 35 : ?: elf_rawdata (scn, NULL));
9266 :
9267 35 : if (unlikely (data == NULL))
9268 : {
9269 0 : error (0, 0, gettext ("cannot get .debug_loc content: %s"),
9270 : elf_errmsg (-1));
9271 35 : return;
9272 : }
9273 :
9274 175 : printf (gettext ("\
9275 : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
9276 : elf_ndxscn (scn), section_name (ebl, ehdr, shdr),
9277 : (uint64_t) shdr->sh_offset);
9278 :
9279 35 : sort_listptr (&known_locsptr, "loclistptr");
9280 35 : size_t listptr_idx = 0;
9281 :
9282 35 : uint_fast8_t address_size = ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 4 : 8;
9283 35 : uint_fast8_t offset_size = 4;
9284 :
9285 35 : bool first = true;
9286 35 : Dwarf_Addr base = 0;
9287 35 : unsigned char *readp = data->d_buf;
9288 35 : unsigned char *const endp = (unsigned char *) data->d_buf + data->d_size;
9289 35 : Dwarf_CU *last_cu = NULL;
9290 195042 : while (readp < endp)
9291 : {
9292 194972 : ptrdiff_t offset = readp - (unsigned char *) data->d_buf;
9293 194972 : Dwarf_CU *cu = last_cu;
9294 194972 : unsigned int attr = 0;
9295 :
9296 194972 : if (first && skip_listptr_hole (&known_locsptr, &listptr_idx,
9297 : &address_size, &offset_size, &base,
9298 : &cu, offset, &readp, endp, &attr))
9299 0 : continue;
9300 :
9301 194972 : if (last_cu != cu)
9302 : {
9303 : Dwarf_Die cudie;
9304 957 : if (dwarf_cu_die (cu, &cudie,
9305 : NULL, NULL, NULL, NULL,
9306 : NULL, NULL) == NULL)
9307 0 : printf (gettext ("\n Unknown CU base: "));
9308 : else
9309 957 : printf (gettext ("\n CU [%6" PRIx64 "] base: "),
9310 : dwarf_dieoffset (&cudie));
9311 957 : print_dwarf_addr (dwflmod, address_size, base, base);
9312 : printf ("\n");
9313 : }
9314 194972 : last_cu = cu;
9315 :
9316 194972 : if (attr == DW_AT_GNU_locviews)
9317 : {
9318 0 : Dwarf_Off next_off = next_listptr_offset (&known_locsptr,
9319 : listptr_idx);
9320 0 : const unsigned char *locp = readp;
9321 : const unsigned char *locendp;
9322 0 : if (next_off == 0
9323 0 : || next_off > (size_t) (endp
9324 0 : - (const unsigned char *) data->d_buf))
9325 : locendp = endp;
9326 : else
9327 0 : locendp = (const unsigned char *) data->d_buf + next_off;
9328 :
9329 0 : while (locp < locendp)
9330 : {
9331 : uint64_t v1, v2;
9332 0 : get_uleb128 (v1, locp, locendp);
9333 0 : if (locp >= locendp)
9334 : {
9335 0 : printf (gettext (" [%6tx] <INVALID DATA>\n"), offset);
9336 : break;
9337 : }
9338 0 : get_uleb128 (v2, locp, locendp);
9339 0 : if (first) /* First view pair in a list. */
9340 : printf (" [%6tx] ", offset);
9341 : else
9342 : printf (" ");
9343 : printf ("view pair %" PRId64 ", %" PRId64 "\n", v1, v2);
9344 0 : first = false;
9345 : }
9346 :
9347 0 : first = true;
9348 0 : readp = (unsigned char *) locendp;
9349 0 : continue;
9350 : }
9351 :
9352 : /* GNU DebugFission encoded addresses as addrx. */
9353 194972 : bool is_debugfission = ((cu != NULL
9354 0 : || split_dwarf_cu_base (dbg, &cu, &base))
9355 389944 : && (cu->version < 5
9356 194972 : && cu->unit_type == DW_UT_split_compile));
9357 194972 : if (!is_debugfission
9358 194912 : && unlikely (data->d_size - offset < (size_t) address_size * 2))
9359 : {
9360 : invalid_data:
9361 0 : printf (gettext (" [%6tx] <INVALID DATA>\n"), offset);
9362 0 : break;
9363 : }
9364 :
9365 : Dwarf_Addr begin;
9366 : Dwarf_Addr end;
9367 194972 : bool use_base = true;
9368 194972 : if (is_debugfission)
9369 : {
9370 60 : const unsigned char *locp = readp;
9371 60 : const unsigned char *locendp = readp + data->d_size;
9372 60 : if (locp >= locendp)
9373 : goto invalid_data;
9374 :
9375 : Dwarf_Word idx;
9376 60 : unsigned char code = *locp++;
9377 60 : switch (code)
9378 : {
9379 : case DW_LLE_GNU_end_of_list_entry:
9380 20 : begin = 0;
9381 20 : end = 0;
9382 20 : break;
9383 :
9384 : case DW_LLE_GNU_base_address_selection_entry:
9385 0 : if (locp >= locendp)
9386 : goto invalid_data;
9387 0 : begin = (Dwarf_Addr) -1;
9388 0 : get_uleb128 (idx, locp, locendp);
9389 0 : if (get_indexed_addr (cu, idx, &end) != 0)
9390 0 : end = idx; /* ... */
9391 : break;
9392 :
9393 : case DW_LLE_GNU_start_end_entry:
9394 0 : if (locp >= locendp)
9395 : goto invalid_data;
9396 0 : get_uleb128 (idx, locp, locendp);
9397 0 : if (get_indexed_addr (cu, idx, &begin) != 0)
9398 0 : begin = idx; /* ... */
9399 0 : if (locp >= locendp)
9400 : goto invalid_data;
9401 0 : get_uleb128 (idx, locp, locendp);
9402 0 : if (get_indexed_addr (cu, idx, &end) != 0)
9403 0 : end = idx; /* ... */
9404 : use_base = false;
9405 : break;
9406 :
9407 : case DW_LLE_GNU_start_length_entry:
9408 40 : if (locp >= locendp)
9409 : goto invalid_data;
9410 40 : get_uleb128 (idx, locp, locendp);
9411 40 : if (get_indexed_addr (cu, idx, &begin) != 0)
9412 0 : begin = idx; /* ... */
9413 40 : if (locendp - locp < 4)
9414 : goto invalid_data;
9415 80 : end = read_4ubyte_unaligned_inc (dbg, locp);
9416 40 : end += begin;
9417 40 : use_base = false;
9418 40 : break;
9419 :
9420 : default:
9421 : goto invalid_data;
9422 : }
9423 :
9424 60 : readp = (unsigned char *) locp;
9425 : }
9426 194912 : else if (address_size == 8)
9427 : {
9428 389824 : begin = read_8ubyte_unaligned_inc (dbg, readp);
9429 389824 : end = read_8ubyte_unaligned_inc (dbg, readp);
9430 : }
9431 : else
9432 : {
9433 0 : begin = read_4ubyte_unaligned_inc (dbg, readp);
9434 0 : end = read_4ubyte_unaligned_inc (dbg, readp);
9435 0 : if (begin == (Dwarf_Addr) (uint32_t) -1)
9436 0 : begin = (Dwarf_Addr) -1l;
9437 : }
9438 :
9439 194972 : if (begin == (Dwarf_Addr) -1l) /* Base address entry. */
9440 : {
9441 0 : printf (gettext (" [%6tx] base address\n "), offset);
9442 0 : print_dwarf_addr (dwflmod, address_size, end, end);
9443 : printf ("\n");
9444 0 : base = end;
9445 : }
9446 194972 : else if (begin == 0 && end == 0) /* End of list entry. */
9447 : {
9448 38841 : if (first)
9449 0 : printf (gettext (" [%6tx] empty list\n"), offset);
9450 : first = true;
9451 : }
9452 : else
9453 : {
9454 : /* We have a location expression entry. */
9455 156131 : uint_fast16_t len = read_2ubyte_unaligned_inc (dbg, readp);
9456 :
9457 156131 : if (first) /* First entry in a list. */
9458 : printf (" [%6tx] ", offset);
9459 : else
9460 : printf (" ");
9461 :
9462 156131 : printf ("range %" PRIx64 ", %" PRIx64 "\n", begin, end);
9463 156131 : if (! print_unresolved_addresses)
9464 : {
9465 156113 : Dwarf_Addr dab = use_base ? base + begin : begin;
9466 156113 : Dwarf_Addr dae = use_base ? base + end : end;
9467 : printf (" ");
9468 156113 : print_dwarf_addr (dwflmod, address_size, dab, dab);
9469 : printf ("..\n ");
9470 156113 : print_dwarf_addr (dwflmod, address_size, dae - 1, dae);
9471 : printf ("\n");
9472 : }
9473 :
9474 156131 : if (endp - readp <= (ptrdiff_t) len)
9475 : {
9476 0 : fputs (gettext (" <INVALID DATA>\n"), stdout);
9477 0 : break;
9478 : }
9479 :
9480 468393 : print_ops (dwflmod, dbg, 11, 11,
9481 312262 : cu != NULL ? cu->version : 3,
9482 : address_size, offset_size, cu, len, readp);
9483 :
9484 156131 : first = false;
9485 156131 : readp += len;
9486 : }
9487 : }
9488 : }
9489 :
9490 : struct mac_culist
9491 : {
9492 : Dwarf_Die die;
9493 : Dwarf_Off offset;
9494 : Dwarf_Files *files;
9495 : struct mac_culist *next;
9496 : };
9497 :
9498 :
9499 : static int
9500 0 : mac_compare (const void *p1, const void *p2)
9501 : {
9502 0 : struct mac_culist *m1 = (struct mac_culist *) p1;
9503 0 : struct mac_culist *m2 = (struct mac_culist *) p2;
9504 :
9505 0 : if (m1->offset < m2->offset)
9506 : return -1;
9507 0 : if (m1->offset > m2->offset)
9508 : return 1;
9509 0 : return 0;
9510 : }
9511 :
9512 :
9513 : static void
9514 0 : print_debug_macinfo_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
9515 0 : Ebl *ebl, GElf_Ehdr *ehdr,
9516 0 : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
9517 : {
9518 0 : printf (gettext ("\
9519 : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
9520 : elf_ndxscn (scn), section_name (ebl, ehdr, shdr),
9521 : (uint64_t) shdr->sh_offset);
9522 0 : putc_unlocked ('\n', stdout);
9523 :
9524 : /* There is no function in libdw to iterate over the raw content of
9525 : the section but it is easy enough to do. */
9526 0 : Elf_Data *data = (dbg->sectiondata[IDX_debug_macinfo]
9527 0 : ?: elf_rawdata (scn, NULL));
9528 0 : if (unlikely (data == NULL))
9529 : {
9530 0 : error (0, 0, gettext ("cannot get macro information section data: %s"),
9531 : elf_errmsg (-1));
9532 0 : return;
9533 : }
9534 :
9535 : /* Get the source file information for all CUs. */
9536 : Dwarf_Off offset;
9537 0 : Dwarf_Off ncu = 0;
9538 : size_t hsize;
9539 0 : struct mac_culist *culist = NULL;
9540 0 : size_t nculist = 0;
9541 0 : while (dwarf_nextcu (dbg, offset = ncu, &ncu, &hsize, NULL, NULL, NULL) == 0)
9542 : {
9543 : Dwarf_Die cudie;
9544 0 : if (dwarf_offdie (dbg, offset + hsize, &cudie) == NULL)
9545 0 : continue;
9546 :
9547 : Dwarf_Attribute attr;
9548 0 : if (dwarf_attr (&cudie, DW_AT_macro_info, &attr) == NULL)
9549 0 : continue;
9550 :
9551 : Dwarf_Word macoff;
9552 0 : if (dwarf_formudata (&attr, &macoff) != 0)
9553 0 : continue;
9554 :
9555 0 : struct mac_culist *newp = (struct mac_culist *) alloca (sizeof (*newp));
9556 0 : newp->die = cudie;
9557 0 : newp->offset = macoff;
9558 0 : newp->files = NULL;
9559 0 : newp->next = culist;
9560 0 : culist = newp;
9561 0 : ++nculist;
9562 : }
9563 :
9564 : /* Convert the list into an array for easier consumption. */
9565 0 : struct mac_culist *cus = (struct mac_culist *) alloca ((nculist + 1)
9566 : * sizeof (*cus));
9567 : /* Add sentinel. */
9568 0 : cus[nculist].offset = data->d_size;
9569 0 : cus[nculist].files = (Dwarf_Files *) -1l;
9570 0 : if (nculist > 0)
9571 : {
9572 0 : for (size_t cnt = nculist - 1; culist != NULL; --cnt)
9573 : {
9574 0 : assert (cnt < nculist);
9575 0 : cus[cnt] = *culist;
9576 0 : culist = culist->next;
9577 : }
9578 :
9579 : /* Sort the array according to the offset in the .debug_macinfo
9580 : section. Note we keep the sentinel at the end. */
9581 0 : qsort (cus, nculist, sizeof (*cus), mac_compare);
9582 : }
9583 :
9584 0 : const unsigned char *readp = (const unsigned char *) data->d_buf;
9585 0 : const unsigned char *readendp = readp + data->d_size;
9586 0 : int level = 1;
9587 :
9588 0 : while (readp < readendp)
9589 : {
9590 0 : unsigned int opcode = *readp++;
9591 : unsigned int u128;
9592 : unsigned int u128_2;
9593 : const unsigned char *endp;
9594 :
9595 0 : switch (opcode)
9596 : {
9597 : case DW_MACINFO_define:
9598 : case DW_MACINFO_undef:
9599 : case DW_MACINFO_vendor_ext:
9600 : /* For the first two opcodes the parameters are
9601 : line, string
9602 : For the latter
9603 : number, string.
9604 : We can treat these cases together. */
9605 0 : get_uleb128 (u128, readp, readendp);
9606 :
9607 0 : endp = memchr (readp, '\0', readendp - readp);
9608 0 : if (unlikely (endp == NULL))
9609 : {
9610 0 : printf (gettext ("\
9611 : %*s*** non-terminated string at end of section"),
9612 : level, "");
9613 : return;
9614 : }
9615 :
9616 0 : if (opcode == DW_MACINFO_define)
9617 0 : printf ("%*s#define %s, line %u\n",
9618 : level, "", (char *) readp, u128);
9619 0 : else if (opcode == DW_MACINFO_undef)
9620 0 : printf ("%*s#undef %s, line %u\n",
9621 : level, "", (char *) readp, u128);
9622 : else
9623 0 : printf (" #vendor-ext %s, number %u\n", (char *) readp, u128);
9624 :
9625 0 : readp = endp + 1;
9626 0 : break;
9627 :
9628 : case DW_MACINFO_start_file:
9629 : /* The two parameters are line and file index, in this order. */
9630 0 : get_uleb128 (u128, readp, readendp);
9631 0 : if (readendp - readp < 1)
9632 : {
9633 0 : printf (gettext ("\
9634 : %*s*** missing DW_MACINFO_start_file argument at end of section"),
9635 : level, "");
9636 : return;
9637 : }
9638 0 : get_uleb128 (u128_2, readp, readendp);
9639 :
9640 : /* Find the CU DIE for this file. */
9641 0 : size_t macoff = readp - (const unsigned char *) data->d_buf;
9642 0 : const char *fname = "???";
9643 0 : if (macoff >= cus[0].offset)
9644 : {
9645 0 : while (macoff >= cus[1].offset && cus[1].offset != data->d_size)
9646 0 : ++cus;
9647 :
9648 0 : if (cus[0].files == NULL
9649 0 : && dwarf_getsrcfiles (&cus[0].die, &cus[0].files, NULL) != 0)
9650 0 : cus[0].files = (Dwarf_Files *) -1l;
9651 :
9652 0 : if (cus[0].files != (Dwarf_Files *) -1l)
9653 0 : fname = (dwarf_filesrc (cus[0].files, u128_2, NULL, NULL)
9654 0 : ?: "???");
9655 : }
9656 :
9657 : printf ("%*sstart_file %u, [%u] %s\n",
9658 : level, "", u128, u128_2, fname);
9659 0 : ++level;
9660 0 : break;
9661 :
9662 : case DW_MACINFO_end_file:
9663 0 : --level;
9664 : printf ("%*send_file\n", level, "");
9665 : /* Nothing more to do. */
9666 : break;
9667 :
9668 : default:
9669 : // XXX gcc seems to generate files with a trailing zero.
9670 0 : if (unlikely (opcode != 0 || readp != readendp))
9671 : printf ("%*s*** invalid opcode %u\n", level, "", opcode);
9672 : break;
9673 : }
9674 : }
9675 : }
9676 :
9677 :
9678 : static void
9679 3 : print_debug_macro_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
9680 6 : Ebl *ebl, GElf_Ehdr *ehdr,
9681 3 : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
9682 : {
9683 15 : printf (gettext ("\
9684 : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
9685 : elf_ndxscn (scn), section_name (ebl, ehdr, shdr),
9686 : (uint64_t) shdr->sh_offset);
9687 3 : putc_unlocked ('\n', stdout);
9688 :
9689 6 : Elf_Data *data = (dbg->sectiondata[IDX_debug_macro]
9690 3 : ?: elf_rawdata (scn, NULL));
9691 3 : if (unlikely (data == NULL))
9692 : {
9693 0 : error (0, 0, gettext ("cannot get macro information section data: %s"),
9694 : elf_errmsg (-1));
9695 0 : return;
9696 : }
9697 :
9698 : /* Get the source file information for all CUs. Uses same
9699 : datastructure as macinfo. But uses offset field to directly
9700 : match .debug_line offset. And just stored in a list. */
9701 : Dwarf_Off offset;
9702 3 : Dwarf_Off ncu = 0;
9703 : size_t hsize;
9704 3 : struct mac_culist *culist = NULL;
9705 3 : size_t nculist = 0;
9706 10 : while (dwarf_nextcu (dbg, offset = ncu, &ncu, &hsize, NULL, NULL, NULL) == 0)
9707 : {
9708 : Dwarf_Die cudie;
9709 4 : if (dwarf_offdie (dbg, offset + hsize, &cudie) == NULL)
9710 0 : continue;
9711 :
9712 : Dwarf_Attribute attr;
9713 4 : if (dwarf_attr (&cudie, DW_AT_stmt_list, &attr) == NULL)
9714 0 : continue;
9715 :
9716 : Dwarf_Word lineoff;
9717 4 : if (dwarf_formudata (&attr, &lineoff) != 0)
9718 0 : continue;
9719 :
9720 4 : struct mac_culist *newp = (struct mac_culist *) alloca (sizeof (*newp));
9721 4 : newp->die = cudie;
9722 4 : newp->offset = lineoff;
9723 4 : newp->files = NULL;
9724 4 : newp->next = culist;
9725 4 : culist = newp;
9726 4 : ++nculist;
9727 : }
9728 :
9729 3 : const unsigned char *readp = (const unsigned char *) data->d_buf;
9730 3 : const unsigned char *readendp = readp + data->d_size;
9731 :
9732 14 : while (readp < readendp)
9733 : {
9734 8 : printf (gettext (" Offset: 0x%" PRIx64 "\n"),
9735 8 : (uint64_t) (readp - (const unsigned char *) data->d_buf));
9736 :
9737 : // Header, 2 byte version, 1 byte flag, optional .debug_line offset,
9738 : // optional vendor extension macro entry table.
9739 8 : if (readp + 2 > readendp)
9740 : {
9741 : invalid_data:
9742 0 : error (0, 0, gettext ("invalid data"));
9743 0 : return;
9744 : }
9745 8 : const uint16_t vers = read_2ubyte_unaligned_inc (dbg, readp);
9746 8 : printf (gettext (" Version: %" PRIu16 "\n"), vers);
9747 :
9748 : // Version 4 is the GNU extension for DWARF4. DWARF5 will use version
9749 : // 5 when it gets standardized.
9750 8 : if (vers != 4 && vers != 5)
9751 : {
9752 0 : printf (gettext (" unknown version, cannot parse section\n"));
9753 : return;
9754 : }
9755 :
9756 8 : if (readp + 1 > readendp)
9757 : goto invalid_data;
9758 8 : const unsigned char flag = *readp++;
9759 8 : printf (gettext (" Flag: 0x%" PRIx8 "\n"), flag);
9760 :
9761 8 : unsigned int offset_len = (flag & 0x01) ? 8 : 4;
9762 8 : printf (gettext (" Offset length: %" PRIu8 "\n"), offset_len);
9763 8 : Dwarf_Off line_offset = -1;
9764 8 : if (flag & 0x02)
9765 : {
9766 4 : if (offset_len == 8)
9767 0 : line_offset = read_8ubyte_unaligned_inc (dbg, readp);
9768 : else
9769 8 : line_offset = read_4ubyte_unaligned_inc (dbg, readp);
9770 4 : printf (gettext (" .debug_line offset: 0x%" PRIx64 "\n"),
9771 : line_offset);
9772 : }
9773 :
9774 8 : struct mac_culist *cu = NULL;
9775 8 : if (line_offset != (Dwarf_Off) -1)
9776 : {
9777 : cu = culist;
9778 5 : while (cu != NULL && line_offset != cu->offset)
9779 1 : cu = cu->next;
9780 : }
9781 :
9782 8 : Dwarf_Off str_offsets_base = str_offsets_base_off (dbg, (cu != NULL
9783 : ? cu->die.cu
9784 : : NULL));
9785 :
9786 : const unsigned char *vendor[DW_MACRO_hi_user - DW_MACRO_lo_user + 1];
9787 : memset (vendor, 0, sizeof vendor);
9788 8 : if (flag & 0x04)
9789 : {
9790 : // 1 byte length, for each item, 1 byte opcode, uleb128 number
9791 : // of arguments, for each argument 1 byte form code.
9792 0 : if (readp + 1 > readendp)
9793 : goto invalid_data;
9794 0 : unsigned int tlen = *readp++;
9795 0 : printf (gettext (" extension opcode table, %" PRIu8 " items:\n"),
9796 : tlen);
9797 0 : for (unsigned int i = 0; i < tlen; i++)
9798 : {
9799 0 : if (readp + 1 > readendp)
9800 : goto invalid_data;
9801 0 : unsigned int opcode = *readp++;
9802 0 : printf (gettext (" [%" PRIx8 "]"), opcode);
9803 0 : if (opcode < DW_MACRO_lo_user
9804 0 : || opcode > DW_MACRO_hi_user)
9805 : goto invalid_data;
9806 : // Record the start of description for this vendor opcode.
9807 : // uleb128 nr args, 1 byte per arg form.
9808 0 : vendor[opcode - DW_MACRO_lo_user] = readp;
9809 0 : if (readp + 1 > readendp)
9810 : goto invalid_data;
9811 0 : unsigned int args = *readp++;
9812 0 : if (args > 0)
9813 : {
9814 0 : printf (gettext (" %" PRIu8 " arguments:"), args);
9815 0 : while (args > 0)
9816 : {
9817 0 : if (readp + 1 > readendp)
9818 : goto invalid_data;
9819 0 : unsigned int form = *readp++;
9820 0 : printf (" %s", dwarf_form_name (form));
9821 0 : if (! libdw_valid_user_form (form))
9822 : goto invalid_data;
9823 0 : args--;
9824 0 : if (args > 0)
9825 : putchar_unlocked (',');
9826 : }
9827 : }
9828 : else
9829 0 : printf (gettext (" no arguments."));
9830 : putchar_unlocked ('\n');
9831 : }
9832 : }
9833 : putchar_unlocked ('\n');
9834 :
9835 8 : int level = 1;
9836 8 : if (readp + 1 > readendp)
9837 : goto invalid_data;
9838 8 : unsigned int opcode = *readp++;
9839 743 : while (opcode != 0)
9840 : {
9841 : unsigned int u128;
9842 : unsigned int u128_2;
9843 : const unsigned char *endp;
9844 : uint64_t off;
9845 :
9846 727 : switch (opcode)
9847 : {
9848 : case DW_MACRO_start_file:
9849 6 : get_uleb128 (u128, readp, readendp);
9850 6 : if (readp >= readendp)
9851 : goto invalid_data;
9852 6 : get_uleb128 (u128_2, readp, readendp);
9853 :
9854 : /* Find the CU DIE that matches this line offset. */
9855 6 : const char *fname = "???";
9856 6 : if (cu != NULL)
9857 : {
9858 6 : if (cu->files == NULL
9859 4 : && dwarf_getsrcfiles (&cu->die, &cu->files,
9860 : NULL) != 0)
9861 0 : cu->files = (Dwarf_Files *) -1l;
9862 :
9863 6 : if (cu->files != (Dwarf_Files *) -1l)
9864 12 : fname = (dwarf_filesrc (cu->files, u128_2,
9865 6 : NULL, NULL) ?: "???");
9866 : }
9867 : printf ("%*sstart_file %u, [%u] %s\n",
9868 : level, "", u128, u128_2, fname);
9869 6 : ++level;
9870 6 : break;
9871 :
9872 : case DW_MACRO_end_file:
9873 6 : --level;
9874 : printf ("%*send_file\n", level, "");
9875 : break;
9876 :
9877 : case DW_MACRO_define:
9878 1 : get_uleb128 (u128, readp, readendp);
9879 1 : endp = memchr (readp, '\0', readendp - readp);
9880 1 : if (endp == NULL)
9881 : goto invalid_data;
9882 1 : printf ("%*s#define %s, line %u\n",
9883 : level, "", readp, u128);
9884 1 : readp = endp + 1;
9885 1 : break;
9886 :
9887 : case DW_MACRO_undef:
9888 0 : get_uleb128 (u128, readp, readendp);
9889 0 : endp = memchr (readp, '\0', readendp - readp);
9890 0 : if (endp == NULL)
9891 : goto invalid_data;
9892 0 : printf ("%*s#undef %s, line %u\n",
9893 : level, "", readp, u128);
9894 0 : readp = endp + 1;
9895 0 : break;
9896 :
9897 : case DW_MACRO_define_strp:
9898 707 : get_uleb128 (u128, readp, readendp);
9899 707 : if (readp + offset_len > readendp)
9900 : goto invalid_data;
9901 707 : if (offset_len == 8)
9902 0 : off = read_8ubyte_unaligned_inc (dbg, readp);
9903 : else
9904 1414 : off = read_4ubyte_unaligned_inc (dbg, readp);
9905 707 : printf ("%*s#define %s, line %u (indirect)\n",
9906 : level, "", dwarf_getstring (dbg, off, NULL), u128);
9907 : break;
9908 :
9909 : case DW_MACRO_undef_strp:
9910 1 : get_uleb128 (u128, readp, readendp);
9911 1 : if (readp + offset_len > readendp)
9912 : goto invalid_data;
9913 1 : if (offset_len == 8)
9914 0 : off = read_8ubyte_unaligned_inc (dbg, readp);
9915 : else
9916 2 : off = read_4ubyte_unaligned_inc (dbg, readp);
9917 1 : printf ("%*s#undef %s, line %u (indirect)\n",
9918 : level, "", dwarf_getstring (dbg, off, NULL), u128);
9919 : break;
9920 :
9921 : case DW_MACRO_import:
9922 6 : if (readp + offset_len > readendp)
9923 : goto invalid_data;
9924 6 : if (offset_len == 8)
9925 0 : off = read_8ubyte_unaligned_inc (dbg, readp);
9926 : else
9927 12 : off = read_4ubyte_unaligned_inc (dbg, readp);
9928 : printf ("%*s#include offset 0x%" PRIx64 "\n",
9929 : level, "", off);
9930 : break;
9931 :
9932 : case DW_MACRO_define_sup:
9933 0 : get_uleb128 (u128, readp, readendp);
9934 0 : if (readp + offset_len > readendp)
9935 : goto invalid_data;
9936 : printf ("%*s#define ", level, "");
9937 0 : readp = print_form_data (dbg, DW_FORM_strp_sup,
9938 : readp, readendp, offset_len,
9939 : str_offsets_base);
9940 : printf (", line %u (sup)\n", u128);
9941 : break;
9942 :
9943 : case DW_MACRO_undef_sup:
9944 0 : get_uleb128 (u128, readp, readendp);
9945 0 : if (readp + offset_len > readendp)
9946 : goto invalid_data;
9947 : printf ("%*s#undef ", level, "");
9948 0 : readp = print_form_data (dbg, DW_FORM_strp_sup,
9949 : readp, readendp, offset_len,
9950 : str_offsets_base);
9951 : printf (", line %u (sup)\n", u128);
9952 : break;
9953 :
9954 : case DW_MACRO_import_sup:
9955 0 : if (readp + offset_len > readendp)
9956 : goto invalid_data;
9957 0 : if (offset_len == 8)
9958 0 : off = read_8ubyte_unaligned_inc (dbg, readp);
9959 : else
9960 0 : off = read_4ubyte_unaligned_inc (dbg, readp);
9961 : // XXX Needs support for reading from supplementary object file.
9962 : printf ("%*s#include offset 0x%" PRIx64 " (sup)\n",
9963 : level, "", off);
9964 : break;
9965 :
9966 : case DW_MACRO_define_strx:
9967 0 : get_uleb128 (u128, readp, readendp);
9968 0 : if (readp + offset_len > readendp)
9969 : goto invalid_data;
9970 : printf ("%*s#define ", level, "");
9971 0 : readp = print_form_data (dbg, DW_FORM_strx,
9972 : readp, readendp, offset_len,
9973 : str_offsets_base);
9974 : printf (", line %u (strx)\n", u128);
9975 : break;
9976 :
9977 : case DW_MACRO_undef_strx:
9978 0 : get_uleb128 (u128, readp, readendp);
9979 0 : if (readp + offset_len > readendp)
9980 : goto invalid_data;
9981 : printf ("%*s#undef ", level, "");
9982 0 : readp = print_form_data (dbg, DW_FORM_strx,
9983 : readp, readendp, offset_len,
9984 : str_offsets_base);
9985 : printf (", line %u (strx)\n", u128);
9986 : break;
9987 :
9988 : default:
9989 : printf ("%*svendor opcode 0x%" PRIx8, level, "", opcode);
9990 0 : if (opcode < DW_MACRO_lo_user
9991 : || opcode > DW_MACRO_lo_user
9992 0 : || vendor[opcode - DW_MACRO_lo_user] == NULL)
9993 : goto invalid_data;
9994 :
9995 : const unsigned char *op_desc;
9996 0 : op_desc = vendor[opcode - DW_MACRO_lo_user];
9997 :
9998 : // Just skip the arguments, we cannot really interpret them,
9999 : // but print as much as we can.
10000 0 : unsigned int args = *op_desc++;
10001 0 : while (args > 0 && readp < readendp)
10002 : {
10003 0 : unsigned int form = *op_desc++;
10004 0 : readp = print_form_data (dbg, form, readp, readendp,
10005 : offset_len, str_offsets_base);
10006 0 : args--;
10007 0 : if (args > 0)
10008 : printf (", ");
10009 : }
10010 : putchar_unlocked ('\n');
10011 : }
10012 :
10013 727 : if (readp + 1 > readendp)
10014 : goto invalid_data;
10015 727 : opcode = *readp++;
10016 727 : if (opcode == 0)
10017 : putchar_unlocked ('\n');
10018 : }
10019 : }
10020 : }
10021 :
10022 :
10023 : /* Callback for printing global names. */
10024 : static int
10025 34 : print_pubnames (Dwarf *dbg __attribute__ ((unused)), Dwarf_Global *global,
10026 : void *arg)
10027 : {
10028 34 : int *np = (int *) arg;
10029 :
10030 68 : printf (gettext (" [%5d] DIE offset: %6" PRId64
10031 : ", CU DIE offset: %6" PRId64 ", name: %s\n"),
10032 34 : (*np)++, global->die_offset, global->cu_offset, global->name);
10033 :
10034 34 : return 0;
10035 : }
10036 :
10037 :
10038 : /* Print the known exported symbols in the DWARF section '.debug_pubnames'. */
10039 : static void
10040 8 : print_debug_pubnames_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
10041 16 : Ebl *ebl, GElf_Ehdr *ehdr,
10042 8 : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
10043 : {
10044 40 : printf (gettext ("\nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
10045 : elf_ndxscn (scn), section_name (ebl, ehdr, shdr),
10046 : (uint64_t) shdr->sh_offset);
10047 :
10048 8 : int n = 0;
10049 8 : (void) dwarf_getpubnames (dbg, print_pubnames, &n, 0);
10050 8 : }
10051 :
10052 : /* Print the content of the DWARF string section '.debug_str'. */
10053 : static void
10054 39 : print_debug_str_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
10055 78 : Ebl *ebl, GElf_Ehdr *ehdr,
10056 39 : Elf_Scn *scn, GElf_Shdr *shdr,
10057 : Dwarf *dbg __attribute__ ((unused)))
10058 : {
10059 39 : Elf_Data *data = elf_rawdata (scn, NULL);
10060 39 : const size_t sh_size = data ? data->d_size : 0;
10061 :
10062 : /* Compute floor(log16(shdr->sh_size)). */
10063 39 : GElf_Addr tmp = sh_size;
10064 39 : int digits = 1;
10065 182 : while (tmp >= 16)
10066 : {
10067 104 : ++digits;
10068 104 : tmp >>= 4;
10069 : }
10070 39 : digits = MAX (4, digits);
10071 :
10072 195 : printf (gettext ("\nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"
10073 : " %*s String\n"),
10074 : elf_ndxscn (scn),
10075 : section_name (ebl, ehdr, shdr), (uint64_t) shdr->sh_offset,
10076 : /* TRANS: the debugstr| prefix makes the string unique. */
10077 39 : digits + 2, sgettext ("debugstr|Offset"));
10078 :
10079 39 : Dwarf_Off offset = 0;
10080 60203 : while (offset < sh_size)
10081 : {
10082 : size_t len;
10083 60125 : const char *str = (const char *) data->d_buf + offset;
10084 60125 : const char *endp = memchr (str, '\0', sh_size - offset);
10085 60125 : if (unlikely (endp == NULL))
10086 : {
10087 0 : printf (gettext (" *** error, missing string terminator\n"));
10088 : break;
10089 : }
10090 :
10091 : printf (" [%*" PRIx64 "] \"%s\"\n", digits, (uint64_t) offset, str);
10092 60125 : len = endp - str;
10093 60125 : offset += len + 1;
10094 : }
10095 39 : }
10096 :
10097 : static void
10098 4 : print_debug_str_offsets_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
10099 8 : Ebl *ebl, GElf_Ehdr *ehdr,
10100 4 : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
10101 : {
10102 20 : printf (gettext ("\
10103 : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
10104 : elf_ndxscn (scn), section_name (ebl, ehdr, shdr),
10105 : (uint64_t) shdr->sh_offset);
10106 :
10107 4 : if (shdr->sh_size == 0)
10108 : return;
10109 :
10110 : /* We like to get the section from libdw to make sure they are relocated. */
10111 8 : Elf_Data *data = (dbg->sectiondata[IDX_debug_str_offsets]
10112 4 : ?: elf_rawdata (scn, NULL));
10113 4 : if (unlikely (data == NULL))
10114 : {
10115 0 : error (0, 0, gettext ("cannot get .debug_str_offsets section data: %s"),
10116 : elf_errmsg (-1));
10117 : return;
10118 : }
10119 :
10120 4 : size_t idx = 0;
10121 4 : sort_listptr (&known_stroffbases, "str_offsets");
10122 :
10123 4 : const unsigned char *start = (const unsigned char *) data->d_buf;
10124 4 : const unsigned char *readp = start;
10125 4 : const unsigned char *readendp = ((const unsigned char *) data->d_buf
10126 4 : + data->d_size);
10127 :
10128 12 : while (readp < readendp)
10129 : {
10130 : /* Most string offset tables will have a header. For split
10131 : dwarf unit GNU DebugFission didn't add one. But they were
10132 : also only defined for split units (main or skeleton units
10133 : didn't have indirect strings). So if we don't have a
10134 : DW_AT_str_offsets_base at all and this is offset zero, then
10135 : just start printing offsets immediately, if this is a .dwo
10136 : section. */
10137 4 : Dwarf_Off off = (Dwarf_Off) (readp
10138 4 : - (const unsigned char *) data->d_buf);
10139 :
10140 : printf ("Table at offset %" PRIx64 " ", off);
10141 :
10142 8 : struct listptr *listptr = get_listptr (&known_stroffbases, idx++);
10143 4 : const unsigned char *next_unitp = readendp;
10144 : uint8_t offset_size;
10145 : bool has_header;
10146 4 : if (listptr == NULL)
10147 : {
10148 : /* This can happen for .dwo files. There is only an header
10149 : in the case this is a version 5 split DWARF file. */
10150 : Dwarf_CU *cu;
10151 : uint8_t unit_type;
10152 4 : if (dwarf_get_units (dbg, NULL, &cu, NULL, &unit_type,
10153 : NULL, NULL) != 0)
10154 : {
10155 : error (0, 0, "Warning: Cannot find any DWARF unit.");
10156 : /* Just guess some values. */
10157 0 : has_header = false;
10158 0 : offset_size = 4;
10159 : }
10160 4 : else if (off == 0
10161 4 : && (unit_type == DW_UT_split_type
10162 4 : || unit_type == DW_UT_split_compile))
10163 : {
10164 4 : has_header = cu->version > 4;
10165 4 : offset_size = cu->offset_size;
10166 : }
10167 : else
10168 : {
10169 : error (0, 0,
10170 : "Warning: No CU references .debug_str_offsets after %"
10171 : PRIx64, off);
10172 0 : has_header = cu->version > 4;
10173 0 : offset_size = cu->offset_size;
10174 : }
10175 : printf ("\n");
10176 : }
10177 : else
10178 : {
10179 : /* This must be DWARF5, since GNU DebugFission didn't define
10180 : DW_AT_str_offsets_base. */
10181 0 : has_header = true;
10182 :
10183 : Dwarf_Die cudie;
10184 0 : if (dwarf_cu_die (listptr->cu, &cudie,
10185 : NULL, NULL, NULL, NULL,
10186 : NULL, NULL) == NULL)
10187 0 : printf ("Unknown CU (%s):\n", dwarf_errmsg (-1));
10188 : else
10189 0 : printf ("for CU [%6" PRIx64 "]:\n", dwarf_dieoffset (&cudie));
10190 : }
10191 :
10192 4 : if (has_header)
10193 : {
10194 : uint64_t unit_length;
10195 : uint16_t version;
10196 : uint16_t padding;
10197 :
10198 4 : unit_length = read_4ubyte_unaligned_inc (dbg, readp);
10199 2 : if (unlikely (unit_length == 0xffffffff))
10200 : {
10201 0 : if (unlikely (readp > readendp - 8))
10202 : {
10203 : invalid_data:
10204 : error (0, 0, "Invalid data");
10205 : return;
10206 : }
10207 0 : unit_length = read_8ubyte_unaligned_inc (dbg, readp);
10208 0 : offset_size = 8;
10209 : }
10210 : else
10211 : offset_size = 4;
10212 :
10213 : printf ("\n");
10214 2 : printf (gettext (" Length: %8" PRIu64 "\n"),
10215 : unit_length);
10216 2 : printf (gettext (" Offset size: %8" PRIu8 "\n"),
10217 : offset_size);
10218 :
10219 : /* We need at least 2-bytes (version) + 2-bytes (padding) =
10220 : 4 bytes to complete the header. And this unit cannot go
10221 : beyond the section data. */
10222 2 : if (readp > readendp - 4
10223 2 : || unit_length < 4
10224 2 : || unit_length > (uint64_t) (readendp - readp))
10225 : goto invalid_data;
10226 :
10227 2 : next_unitp = readp + unit_length;
10228 :
10229 2 : version = read_2ubyte_unaligned_inc (dbg, readp);
10230 2 : printf (gettext (" DWARF version: %8" PRIu16 "\n"), version);
10231 :
10232 2 : if (version != 5)
10233 : {
10234 0 : error (0, 0, gettext ("Unknown version"));
10235 : goto next_unit;
10236 : }
10237 :
10238 2 : padding = read_2ubyte_unaligned_inc (dbg, readp);
10239 2 : printf (gettext (" Padding: %8" PRIx16 "\n"), padding);
10240 :
10241 2 : if (listptr != NULL
10242 0 : && listptr->offset != (Dwarf_Off) (readp - start))
10243 : {
10244 : error (0, 0, "String offsets index doesn't start after header");
10245 : goto next_unit;
10246 : }
10247 :
10248 : printf ("\n");
10249 : }
10250 :
10251 4 : int digits = 1;
10252 4 : size_t offsets = (next_unitp - readp) / offset_size;
10253 12 : while (offsets >= 10)
10254 : {
10255 4 : ++digits;
10256 4 : offsets /= 10;
10257 : }
10258 :
10259 4 : unsigned int index = 0;
10260 4 : size_t index_offset = readp - (const unsigned char *) data->d_buf;
10261 : printf (" Offsets start at 0x%zx:\n", index_offset);
10262 64 : while (readp <= next_unitp - offset_size)
10263 : {
10264 : Dwarf_Word offset;
10265 60 : if (offset_size == 4)
10266 120 : offset = read_4ubyte_unaligned_inc (dbg, readp);
10267 : else
10268 0 : offset = read_8ubyte_unaligned_inc (dbg, readp);
10269 60 : const char *str = dwarf_getstring (dbg, offset, NULL);
10270 120 : printf (" [%*u] [%*" PRIx64 "] \"%s\"\n",
10271 60 : digits, index++, (int) offset_size * 2, offset, str ?: "???");
10272 : }
10273 : printf ("\n");
10274 :
10275 4 : if (readp != next_unitp)
10276 0 : error (0, 0, "extra %zd bytes at end of unit",
10277 0 : (size_t) (next_unitp - readp));
10278 :
10279 : next_unit:
10280 4 : readp = next_unitp;
10281 : }
10282 : }
10283 :
10284 :
10285 : /* Print the content of the call frame search table section
10286 : '.eh_frame_hdr'. */
10287 : static void
10288 14 : print_debug_frame_hdr_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
10289 : Ebl *ebl __attribute__ ((unused)),
10290 : GElf_Ehdr *ehdr __attribute__ ((unused)),
10291 : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
10292 : {
10293 14 : printf (gettext ("\
10294 : \nCall frame search table section [%2zu] '.eh_frame_hdr':\n"),
10295 : elf_ndxscn (scn));
10296 :
10297 14 : Elf_Data *data = elf_rawdata (scn, NULL);
10298 :
10299 14 : if (unlikely (data == NULL))
10300 : {
10301 0 : error (0, 0, gettext ("cannot get %s content: %s"),
10302 : ".eh_frame_hdr", elf_errmsg (-1));
10303 0 : return;
10304 : }
10305 :
10306 14 : const unsigned char *readp = data->d_buf;
10307 14 : const unsigned char *const dataend = ((unsigned char *) data->d_buf
10308 14 : + data->d_size);
10309 :
10310 14 : if (unlikely (readp + 4 > dataend))
10311 : {
10312 : invalid_data:
10313 0 : error (0, 0, gettext ("invalid data"));
10314 : return;
10315 : }
10316 :
10317 14 : unsigned int version = *readp++;
10318 14 : unsigned int eh_frame_ptr_enc = *readp++;
10319 14 : unsigned int fde_count_enc = *readp++;
10320 14 : unsigned int table_enc = *readp++;
10321 :
10322 : printf (" version: %u\n"
10323 : " eh_frame_ptr_enc: %#x ",
10324 : version, eh_frame_ptr_enc);
10325 14 : print_encoding_base ("", eh_frame_ptr_enc);
10326 : printf (" fde_count_enc: %#x ", fde_count_enc);
10327 14 : print_encoding_base ("", fde_count_enc);
10328 : printf (" table_enc: %#x ", table_enc);
10329 14 : print_encoding_base ("", table_enc);
10330 :
10331 14 : uint64_t eh_frame_ptr = 0;
10332 14 : if (eh_frame_ptr_enc != DW_EH_PE_omit)
10333 : {
10334 14 : readp = read_encoded (eh_frame_ptr_enc, readp, dataend, &eh_frame_ptr,
10335 : dbg);
10336 14 : if (unlikely (readp == NULL))
10337 : goto invalid_data;
10338 :
10339 14 : printf (" eh_frame_ptr: %#" PRIx64, eh_frame_ptr);
10340 14 : if ((eh_frame_ptr_enc & 0x70) == DW_EH_PE_pcrel)
10341 14 : printf (" (offset: %#" PRIx64 ")",
10342 : /* +4 because of the 4 byte header of the section. */
10343 14 : (uint64_t) shdr->sh_offset + 4 + eh_frame_ptr);
10344 :
10345 : putchar_unlocked ('\n');
10346 : }
10347 :
10348 14 : uint64_t fde_count = 0;
10349 14 : if (fde_count_enc != DW_EH_PE_omit)
10350 : {
10351 14 : readp = read_encoded (fde_count_enc, readp, dataend, &fde_count, dbg);
10352 14 : if (unlikely (readp == NULL))
10353 : goto invalid_data;
10354 :
10355 14 : printf (" fde_count: %" PRIu64 "\n", fde_count);
10356 : }
10357 :
10358 14 : if (fde_count == 0 || table_enc == DW_EH_PE_omit)
10359 : return;
10360 :
10361 14 : puts (" Table:");
10362 :
10363 : /* Optimize for the most common case. */
10364 14 : if (table_enc == (DW_EH_PE_datarel | DW_EH_PE_sdata4))
10365 3715 : while (fde_count > 0 && readp + 8 <= dataend)
10366 : {
10367 3701 : int32_t initial_location = read_4sbyte_unaligned_inc (dbg, readp);
10368 7402 : uint64_t initial_offset = ((uint64_t) shdr->sh_offset
10369 3701 : + (int64_t) initial_location);
10370 3701 : int32_t address = read_4sbyte_unaligned_inc (dbg, readp);
10371 : // XXX Possibly print symbol name or section offset for initial_offset
10372 3701 : printf (" %#" PRIx32 " (offset: %#6" PRIx64 ") -> %#" PRIx32
10373 : " fde=[%6" PRIx64 "]\n",
10374 : initial_location, initial_offset,
10375 3701 : address, address - (eh_frame_ptr + 4));
10376 : }
10377 : else
10378 : while (0 && readp < dataend)
10379 : {
10380 :
10381 : }
10382 : }
10383 :
10384 :
10385 : /* Print the content of the exception handling table section
10386 : '.eh_frame_hdr'. */
10387 : static void
10388 0 : print_debug_exception_table (Dwfl_Module *dwflmod __attribute__ ((unused)),
10389 : Ebl *ebl __attribute__ ((unused)),
10390 : GElf_Ehdr *ehdr __attribute__ ((unused)),
10391 : Elf_Scn *scn,
10392 : GElf_Shdr *shdr __attribute__ ((unused)),
10393 : Dwarf *dbg __attribute__ ((unused)))
10394 : {
10395 0 : printf (gettext ("\
10396 : \nException handling table section [%2zu] '.gcc_except_table':\n"),
10397 : elf_ndxscn (scn));
10398 :
10399 0 : Elf_Data *data = elf_rawdata (scn, NULL);
10400 :
10401 0 : if (unlikely (data == NULL))
10402 : {
10403 0 : error (0, 0, gettext ("cannot get %s content: %s"),
10404 : ".gcc_except_table", elf_errmsg (-1));
10405 0 : return;
10406 : }
10407 :
10408 0 : const unsigned char *readp = data->d_buf;
10409 0 : const unsigned char *const dataend = readp + data->d_size;
10410 :
10411 0 : if (unlikely (readp + 1 > dataend))
10412 : {
10413 : invalid_data:
10414 0 : error (0, 0, gettext ("invalid data"));
10415 : return;
10416 : }
10417 0 : unsigned int lpstart_encoding = *readp++;
10418 0 : printf (gettext (" LPStart encoding: %#x "), lpstart_encoding);
10419 0 : print_encoding_base ("", lpstart_encoding);
10420 0 : if (lpstart_encoding != DW_EH_PE_omit)
10421 : {
10422 : uint64_t lpstart;
10423 0 : readp = read_encoded (lpstart_encoding, readp, dataend, &lpstart, dbg);
10424 0 : printf (" LPStart: %#" PRIx64 "\n", lpstart);
10425 : }
10426 :
10427 0 : if (unlikely (readp + 1 > dataend))
10428 : goto invalid_data;
10429 0 : unsigned int ttype_encoding = *readp++;
10430 0 : printf (gettext (" TType encoding: %#x "), ttype_encoding);
10431 0 : print_encoding_base ("", ttype_encoding);
10432 0 : const unsigned char *ttype_base = NULL;
10433 0 : if (ttype_encoding != DW_EH_PE_omit)
10434 : {
10435 : unsigned int ttype_base_offset;
10436 0 : get_uleb128 (ttype_base_offset, readp, dataend);
10437 : printf (" TType base offset: %#x\n", ttype_base_offset);
10438 0 : if ((size_t) (dataend - readp) > ttype_base_offset)
10439 0 : ttype_base = readp + ttype_base_offset;
10440 : }
10441 :
10442 0 : if (unlikely (readp + 1 > dataend))
10443 : goto invalid_data;
10444 0 : unsigned int call_site_encoding = *readp++;
10445 0 : printf (gettext (" Call site encoding: %#x "), call_site_encoding);
10446 0 : print_encoding_base ("", call_site_encoding);
10447 : unsigned int call_site_table_len;
10448 0 : get_uleb128 (call_site_table_len, readp, dataend);
10449 :
10450 0 : const unsigned char *const action_table = readp + call_site_table_len;
10451 0 : if (unlikely (action_table > dataend))
10452 : goto invalid_data;
10453 : unsigned int u = 0;
10454 : unsigned int max_action = 0;
10455 0 : while (readp < action_table)
10456 : {
10457 0 : if (u == 0)
10458 0 : puts (gettext ("\n Call site table:"));
10459 :
10460 : uint64_t call_site_start;
10461 0 : readp = read_encoded (call_site_encoding, readp, dataend,
10462 : &call_site_start, dbg);
10463 : uint64_t call_site_length;
10464 0 : readp = read_encoded (call_site_encoding, readp, dataend,
10465 : &call_site_length, dbg);
10466 : uint64_t landing_pad;
10467 0 : readp = read_encoded (call_site_encoding, readp, dataend,
10468 : &landing_pad, dbg);
10469 : unsigned int action;
10470 0 : get_uleb128 (action, readp, dataend);
10471 0 : max_action = MAX (action, max_action);
10472 0 : printf (gettext (" [%4u] Call site start: %#" PRIx64 "\n"
10473 : " Call site length: %" PRIu64 "\n"
10474 : " Landing pad: %#" PRIx64 "\n"
10475 : " Action: %u\n"),
10476 : u++, call_site_start, call_site_length, landing_pad, action);
10477 : }
10478 0 : if (readp != action_table)
10479 : goto invalid_data;
10480 :
10481 0 : unsigned int max_ar_filter = 0;
10482 0 : if (max_action > 0)
10483 : {
10484 0 : puts ("\n Action table:");
10485 :
10486 0 : size_t maxdata = (size_t) (dataend - action_table);
10487 0 : if (max_action > maxdata || maxdata - max_action < 1)
10488 : {
10489 : invalid_action_table:
10490 0 : fputs (gettext (" <INVALID DATA>\n"), stdout);
10491 0 : return;
10492 : }
10493 :
10494 0 : const unsigned char *const action_table_end
10495 0 : = action_table + max_action + 1;
10496 :
10497 0 : u = 0;
10498 : do
10499 : {
10500 : int ar_filter;
10501 0 : get_sleb128 (ar_filter, readp, action_table_end);
10502 0 : if (ar_filter > 0 && (unsigned int) ar_filter > max_ar_filter)
10503 0 : max_ar_filter = ar_filter;
10504 : int ar_disp;
10505 0 : if (readp >= action_table_end)
10506 : goto invalid_action_table;
10507 0 : get_sleb128 (ar_disp, readp, action_table_end);
10508 :
10509 : printf (" [%4u] ar_filter: % d\n"
10510 : " ar_disp: % -5d",
10511 : u, ar_filter, ar_disp);
10512 0 : if (abs (ar_disp) & 1)
10513 0 : printf (" -> [%4u]\n", u + (ar_disp + 1) / 2);
10514 0 : else if (ar_disp != 0)
10515 0 : puts (" -> ???");
10516 : else
10517 : putchar_unlocked ('\n');
10518 0 : ++u;
10519 : }
10520 0 : while (readp < action_table_end);
10521 : }
10522 :
10523 0 : if (max_ar_filter > 0 && ttype_base != NULL)
10524 : {
10525 : unsigned char dsize;
10526 0 : puts ("\n TType table:");
10527 :
10528 : // XXX Not *4, size of encoding;
10529 0 : switch (ttype_encoding & 7)
10530 : {
10531 : case DW_EH_PE_udata2:
10532 : case DW_EH_PE_sdata2:
10533 : dsize = 2;
10534 : break;
10535 : case DW_EH_PE_udata4:
10536 : case DW_EH_PE_sdata4:
10537 : dsize = 4;
10538 : break;
10539 : case DW_EH_PE_udata8:
10540 : case DW_EH_PE_sdata8:
10541 : dsize = 8;
10542 : break;
10543 : default:
10544 0 : dsize = 0;
10545 0 : error (1, 0, gettext ("invalid TType encoding"));
10546 : }
10547 :
10548 0 : if (max_ar_filter
10549 0 : > (size_t) (ttype_base - (const unsigned char *) data->d_buf) / dsize)
10550 : goto invalid_data;
10551 :
10552 0 : readp = ttype_base - max_ar_filter * dsize;
10553 : do
10554 : {
10555 : uint64_t ttype;
10556 0 : readp = read_encoded (ttype_encoding, readp, ttype_base, &ttype,
10557 : dbg);
10558 0 : printf (" [%4u] %#" PRIx64 "\n", max_ar_filter--, ttype);
10559 : }
10560 0 : while (readp < ttype_base);
10561 : }
10562 : }
10563 :
10564 : /* Print the content of the '.gdb_index' section.
10565 : http://sourceware.org/gdb/current/onlinedocs/gdb/Index-Section-Format.html
10566 : */
10567 : static void
10568 6 : print_gdb_index_section (Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr,
10569 2 : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
10570 : {
10571 10 : printf (gettext ("\nGDB section [%2zu] '%s' at offset %#" PRIx64
10572 : " contains %" PRId64 " bytes :\n"),
10573 : elf_ndxscn (scn), section_name (ebl, ehdr, shdr),
10574 : (uint64_t) shdr->sh_offset, (uint64_t) shdr->sh_size);
10575 :
10576 2 : Elf_Data *data = elf_rawdata (scn, NULL);
10577 :
10578 2 : if (unlikely (data == NULL))
10579 : {
10580 0 : error (0, 0, gettext ("cannot get %s content: %s"),
10581 : ".gdb_index", elf_errmsg (-1));
10582 0 : return;
10583 : }
10584 :
10585 : // .gdb_index is always in little endian.
10586 2 : Dwarf dummy_dbg = { .other_byte_order = MY_ELFDATA != ELFDATA2LSB };
10587 2 : dbg = &dummy_dbg;
10588 :
10589 2 : const unsigned char *readp = data->d_buf;
10590 2 : const unsigned char *const dataend = readp + data->d_size;
10591 :
10592 2 : if (unlikely (readp + 4 > dataend))
10593 : {
10594 : invalid_data:
10595 0 : error (0, 0, gettext ("invalid data"));
10596 : return;
10597 : }
10598 :
10599 2 : int32_t vers = read_4ubyte_unaligned (dbg, readp);
10600 2 : printf (gettext (" Version: %" PRId32 "\n"), vers);
10601 :
10602 : // The only difference between version 4 and version 5 is the
10603 : // hash used for generating the table. Version 6 contains symbols
10604 : // for inlined functions, older versions didn't. Version 7 adds
10605 : // symbol kinds. Version 8 just indicates that it correctly includes
10606 : // TUs for symbols.
10607 2 : if (vers < 4 || vers > 8)
10608 : {
10609 0 : printf (gettext (" unknown version, cannot parse section\n"));
10610 : return;
10611 : }
10612 :
10613 2 : readp += 4;
10614 2 : if (unlikely (readp + 4 > dataend))
10615 : goto invalid_data;
10616 :
10617 4 : uint32_t cu_off = read_4ubyte_unaligned (dbg, readp);
10618 2 : printf (gettext (" CU offset: %#" PRIx32 "\n"), cu_off);
10619 :
10620 2 : readp += 4;
10621 2 : if (unlikely (readp + 4 > dataend))
10622 : goto invalid_data;
10623 :
10624 4 : uint32_t tu_off = read_4ubyte_unaligned (dbg, readp);
10625 2 : printf (gettext (" TU offset: %#" PRIx32 "\n"), tu_off);
10626 :
10627 2 : readp += 4;
10628 2 : if (unlikely (readp + 4 > dataend))
10629 : goto invalid_data;
10630 :
10631 4 : uint32_t addr_off = read_4ubyte_unaligned (dbg, readp);
10632 2 : printf (gettext (" address offset: %#" PRIx32 "\n"), addr_off);
10633 :
10634 2 : readp += 4;
10635 2 : if (unlikely (readp + 4 > dataend))
10636 : goto invalid_data;
10637 :
10638 4 : uint32_t sym_off = read_4ubyte_unaligned (dbg, readp);
10639 2 : printf (gettext (" symbol offset: %#" PRIx32 "\n"), sym_off);
10640 :
10641 2 : readp += 4;
10642 2 : if (unlikely (readp + 4 > dataend))
10643 : goto invalid_data;
10644 :
10645 4 : uint32_t const_off = read_4ubyte_unaligned (dbg, readp);
10646 2 : printf (gettext (" constant offset: %#" PRIx32 "\n"), const_off);
10647 :
10648 2 : if (unlikely ((size_t) (dataend - (const unsigned char *) data->d_buf)
10649 : < const_off))
10650 : goto invalid_data;
10651 :
10652 2 : readp = data->d_buf + cu_off;
10653 :
10654 2 : const unsigned char *nextp = data->d_buf + tu_off;
10655 2 : if (tu_off >= data->d_size)
10656 : goto invalid_data;
10657 :
10658 2 : size_t cu_nr = (nextp - readp) / 16;
10659 :
10660 2 : printf (gettext ("\n CU list at offset %#" PRIx32
10661 : " contains %zu entries:\n"),
10662 : cu_off, cu_nr);
10663 :
10664 2 : size_t n = 0;
10665 8 : while (dataend - readp >= 16 && n < cu_nr)
10666 : {
10667 8 : uint64_t off = read_8ubyte_unaligned (dbg, readp);
10668 4 : readp += 8;
10669 :
10670 8 : uint64_t len = read_8ubyte_unaligned (dbg, readp);
10671 4 : readp += 8;
10672 :
10673 : printf (" [%4zu] start: %0#8" PRIx64
10674 : ", length: %5" PRIu64 "\n", n, off, len);
10675 4 : n++;
10676 : }
10677 :
10678 2 : readp = data->d_buf + tu_off;
10679 2 : nextp = data->d_buf + addr_off;
10680 2 : if (addr_off >= data->d_size)
10681 : goto invalid_data;
10682 :
10683 2 : size_t tu_nr = (nextp - readp) / 24;
10684 :
10685 2 : printf (gettext ("\n TU list at offset %#" PRIx32
10686 : " contains %zu entries:\n"),
10687 : tu_off, tu_nr);
10688 :
10689 2 : n = 0;
10690 6 : while (dataend - readp >= 24 && n < tu_nr)
10691 : {
10692 4 : uint64_t off = read_8ubyte_unaligned (dbg, readp);
10693 2 : readp += 8;
10694 :
10695 4 : uint64_t type = read_8ubyte_unaligned (dbg, readp);
10696 2 : readp += 8;
10697 :
10698 4 : uint64_t sig = read_8ubyte_unaligned (dbg, readp);
10699 2 : readp += 8;
10700 :
10701 : printf (" [%4zu] CU offset: %5" PRId64
10702 : ", type offset: %5" PRId64
10703 : ", signature: %0#8" PRIx64 "\n", n, off, type, sig);
10704 2 : n++;
10705 : }
10706 :
10707 2 : readp = data->d_buf + addr_off;
10708 2 : nextp = data->d_buf + sym_off;
10709 2 : if (sym_off >= data->d_size)
10710 : goto invalid_data;
10711 :
10712 2 : size_t addr_nr = (nextp - readp) / 20;
10713 :
10714 2 : printf (gettext ("\n Address list at offset %#" PRIx32
10715 : " contains %zu entries:\n"),
10716 : addr_off, addr_nr);
10717 :
10718 2 : n = 0;
10719 8 : while (dataend - readp >= 20 && n < addr_nr)
10720 : {
10721 8 : uint64_t low = read_8ubyte_unaligned (dbg, readp);
10722 4 : readp += 8;
10723 :
10724 8 : uint64_t high = read_8ubyte_unaligned (dbg, readp);
10725 4 : readp += 8;
10726 :
10727 8 : uint32_t idx = read_4ubyte_unaligned (dbg, readp);
10728 4 : readp += 4;
10729 :
10730 : printf (" [%4zu] ", n);
10731 4 : print_dwarf_addr (dwflmod, 8, low, low);
10732 : printf ("..");
10733 4 : print_dwarf_addr (dwflmod, 8, high - 1, high);
10734 : printf (", CU index: %5" PRId32 "\n", idx);
10735 4 : n++;
10736 : }
10737 :
10738 2 : const unsigned char *const_start = data->d_buf + const_off;
10739 2 : if (const_off >= data->d_size)
10740 : goto invalid_data;
10741 :
10742 2 : readp = data->d_buf + sym_off;
10743 2 : nextp = const_start;
10744 2 : size_t sym_nr = (nextp - readp) / 8;
10745 :
10746 2 : printf (gettext ("\n Symbol table at offset %#" PRIx32
10747 : " contains %zu slots:\n"),
10748 : addr_off, sym_nr);
10749 :
10750 2 : n = 0;
10751 2052 : while (dataend - readp >= 8 && n < sym_nr)
10752 : {
10753 4096 : uint32_t name = read_4ubyte_unaligned (dbg, readp);
10754 2048 : readp += 4;
10755 :
10756 4096 : uint32_t vector = read_4ubyte_unaligned (dbg, readp);
10757 2048 : readp += 4;
10758 :
10759 2048 : if (name != 0 || vector != 0)
10760 : {
10761 14 : const unsigned char *sym = const_start + name;
10762 14 : if (unlikely ((size_t) (dataend - const_start) < name
10763 : || memchr (sym, '\0', dataend - sym) == NULL))
10764 : goto invalid_data;
10765 :
10766 : printf (" [%4zu] symbol: %s, CUs: ", n, sym);
10767 :
10768 14 : const unsigned char *readcus = const_start + vector;
10769 14 : if (unlikely ((size_t) (dataend - const_start) < vector))
10770 : goto invalid_data;
10771 28 : uint32_t cus = read_4ubyte_unaligned (dbg, readcus);
10772 44 : while (cus--)
10773 : {
10774 : uint32_t cu_kind, cu, kind;
10775 : bool is_static;
10776 16 : readcus += 4;
10777 16 : if (unlikely (readcus + 4 > dataend))
10778 : goto invalid_data;
10779 32 : cu_kind = read_4ubyte_unaligned (dbg, readcus);
10780 16 : cu = cu_kind & ((1 << 24) - 1);
10781 16 : kind = (cu_kind >> 28) & 7;
10782 16 : is_static = cu_kind & (1U << 31);
10783 16 : if (cu > cu_nr - 1)
10784 2 : printf ("%" PRId32 "T", cu - (uint32_t) cu_nr);
10785 : else
10786 : printf ("%" PRId32, cu);
10787 16 : if (kind != 0)
10788 : {
10789 : printf (" (");
10790 8 : switch (kind)
10791 : {
10792 : case 1:
10793 : printf ("type");
10794 : break;
10795 : case 2:
10796 : printf ("var");
10797 : break;
10798 : case 3:
10799 : printf ("func");
10800 : break;
10801 : case 4:
10802 : printf ("other");
10803 : break;
10804 : default:
10805 : printf ("unknown-0x%" PRIx32, kind);
10806 : break;
10807 : }
10808 8 : printf (":%c)", (is_static ? 'S' : 'G'));
10809 : }
10810 16 : if (cus > 0)
10811 : printf (", ");
10812 : }
10813 : printf ("\n");
10814 : }
10815 2048 : n++;
10816 : }
10817 : }
10818 :
10819 : /* Returns true and sets split DWARF CU id if there is a split compile
10820 : unit in the given Dwarf, and no non-split units are found (before it). */
10821 : static bool
10822 103 : is_split_dwarf (Dwarf *dbg, uint64_t *id, Dwarf_CU **split_cu)
10823 : {
10824 103 : Dwarf_CU *cu = NULL;
10825 206 : while (dwarf_get_units (dbg, cu, &cu, NULL, NULL, NULL, NULL) == 0)
10826 : {
10827 : uint8_t unit_type;
10828 103 : if (dwarf_cu_info (cu, NULL, &unit_type, NULL, NULL,
10829 : id, NULL, NULL) != 0)
10830 103 : return false;
10831 :
10832 103 : if (unit_type != DW_UT_split_compile && unit_type != DW_UT_split_type)
10833 : return false;
10834 :
10835 : /* We really only care about the split compile unit, the types
10836 : should be fine and self sufficient. Also they don't have an
10837 : id that we can match with a skeleton unit. */
10838 9 : if (unit_type == DW_UT_split_compile)
10839 : {
10840 9 : *split_cu = cu;
10841 9 : return true;
10842 : }
10843 : }
10844 :
10845 : return false;
10846 : }
10847 :
10848 : /* Check that there is one and only one Dwfl_Module, return in arg. */
10849 : static int
10850 9 : getone_dwflmod (Dwfl_Module *dwflmod,
10851 : void **userdata __attribute__ ((unused)),
10852 : const char *name __attribute__ ((unused)),
10853 : Dwarf_Addr base __attribute__ ((unused)),
10854 : void *arg)
10855 : {
10856 9 : Dwfl_Module **m = (Dwfl_Module **) arg;
10857 9 : if (*m != NULL)
10858 : return DWARF_CB_ABORT;
10859 9 : *m = dwflmod;
10860 9 : return DWARF_CB_OK;
10861 : }
10862 :
10863 : static void
10864 125 : print_debug (Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr)
10865 : {
10866 : /* Used for skeleton file, if necessary for split DWARF. */
10867 125 : Dwfl *skel_dwfl = NULL;
10868 125 : Dwfl_Module *skel_mod = NULL;
10869 125 : char *skel_name = NULL;
10870 125 : Dwarf *split_dbg = NULL;
10871 125 : Dwarf_CU *split_cu = NULL;
10872 :
10873 : /* Before we start the real work get a debug context descriptor. */
10874 : Dwarf_Addr dwbias;
10875 125 : Dwarf *dbg = dwfl_module_getdwarf (dwflmod, &dwbias);
10876 375 : Dwarf dummy_dbg =
10877 : {
10878 125 : .elf = ebl->elf,
10879 125 : .other_byte_order = MY_ELFDATA != ehdr->e_ident[EI_DATA]
10880 : };
10881 125 : if (dbg == NULL)
10882 : {
10883 22 : if ((print_debug_sections & ~section_exception) != 0)
10884 0 : error (0, 0, gettext ("cannot get debug context descriptor: %s"),
10885 : dwfl_errmsg (-1));
10886 : dbg = &dummy_dbg;
10887 : }
10888 : else
10889 : {
10890 : /* If we are asked about a split dwarf (.dwo) file, use the user
10891 : provided, or find the corresponding skeleton file. If we got
10892 : a skeleton file, replace the given dwflmod and dbg, with one
10893 : derived from the skeleton file to provide enough context. */
10894 : uint64_t split_id;
10895 103 : if (is_split_dwarf (dbg, &split_id, &split_cu))
10896 : {
10897 9 : if (dwarf_skeleton != NULL)
10898 9 : skel_name = strdup (dwarf_skeleton);
10899 : else
10900 : {
10901 : /* Replace file.dwo with file.o and see if that matches. */
10902 : const char *fname;
10903 0 : dwfl_module_info (dwflmod, NULL, NULL, NULL, NULL, NULL,
10904 : &fname, NULL);
10905 0 : if (fname != NULL)
10906 : {
10907 0 : size_t flen = strlen (fname);
10908 0 : if (flen > 4 && strcmp (".dwo", fname + flen - 4) == 0)
10909 : {
10910 0 : skel_name = strdup (fname);
10911 0 : if (skel_name != NULL)
10912 : {
10913 0 : skel_name[flen - 3] = 'o';
10914 0 : skel_name[flen - 2] = '\0';
10915 : }
10916 : }
10917 : }
10918 : }
10919 :
10920 9 : if (skel_name != NULL)
10921 : {
10922 9 : int skel_fd = open (skel_name, O_RDONLY);
10923 9 : if (skel_fd == -1)
10924 0 : fprintf (stderr, "Warning: Couldn't open DWARF skeleton file"
10925 : " '%s'\n", skel_name);
10926 : else
10927 9 : skel_dwfl = create_dwfl (skel_fd, skel_name);
10928 :
10929 9 : if (skel_dwfl != NULL)
10930 : {
10931 9 : if (dwfl_getmodules (skel_dwfl, &getone_dwflmod,
10932 : &skel_mod, 0) != 0)
10933 : {
10934 0 : fprintf (stderr, "Warning: Bad DWARF skeleton,"
10935 : " multiple modules '%s'\n", skel_name);
10936 0 : dwfl_end (skel_dwfl);
10937 0 : skel_mod = NULL;
10938 : }
10939 : }
10940 0 : else if (skel_fd != -1)
10941 0 : fprintf (stderr, "Warning: Couldn't create skeleton dwfl for"
10942 : " '%s': %s\n", skel_name, dwfl_errmsg (-1));
10943 :
10944 9 : if (skel_mod != NULL)
10945 : {
10946 9 : Dwarf *skel_dbg = dwfl_module_getdwarf (skel_mod, &dwbias);
10947 9 : if (skel_dbg != NULL)
10948 : {
10949 : /* First check the skeleton CU DIE, only fetch
10950 : the split DIE if we know the id matches to
10951 : not unnecessary search for any split DIEs we
10952 : don't need. */
10953 9 : Dwarf_CU *cu = NULL;
10954 22 : while (dwarf_get_units (skel_dbg, cu, &cu,
10955 : NULL, NULL, NULL, NULL) == 0)
10956 : {
10957 : uint8_t unit_type;
10958 : uint64_t skel_id;
10959 13 : if (dwarf_cu_info (cu, NULL, &unit_type, NULL, NULL,
10960 : &skel_id, NULL, NULL) == 0
10961 13 : && unit_type == DW_UT_skeleton
10962 13 : && split_id == skel_id)
10963 : {
10964 : Dwarf_Die subdie;
10965 9 : if (dwarf_cu_info (cu, NULL, NULL, NULL,
10966 : &subdie,
10967 : NULL, NULL, NULL) == 0
10968 9 : && dwarf_tag (&subdie) != DW_TAG_invalid)
10969 : {
10970 9 : split_dbg = dwarf_cu_getdwarf (subdie.cu);
10971 9 : if (split_dbg == NULL)
10972 0 : fprintf (stderr,
10973 : "Warning: Couldn't get split_dbg:"
10974 : " %s\n", dwarf_errmsg (-1));
10975 9 : break;
10976 : }
10977 : else
10978 : {
10979 : /* Everything matches up, but not
10980 : according to libdw. Which means
10981 : the user knew better. So...
10982 : Terrible hack... We can never
10983 : destroy the underlying dwfl
10984 : because it would free the wrong
10985 : Dwarfs... So we leak memory...*/
10986 0 : if (cu->split == NULL
10987 0 : && dwarf_skeleton != NULL)
10988 : {
10989 0 : do_not_close_dwfl = true;
10990 0 : __libdw_link_skel_split (cu, split_cu);
10991 0 : split_dbg = dwarf_cu_getdwarf (split_cu);
10992 0 : break;
10993 : }
10994 : else
10995 0 : fprintf (stderr, "Warning: Couldn't get"
10996 : " skeleton subdie: %s\n",
10997 : dwarf_errmsg (-1));
10998 : }
10999 : }
11000 : }
11001 9 : if (split_dbg == NULL)
11002 0 : fprintf (stderr, "Warning: '%s' didn't contain a skeleton for split id %" PRIx64 "\n", skel_name, split_id);
11003 : }
11004 : else
11005 0 : fprintf (stderr, "Warning: Couldn't get skeleton DWARF:"
11006 : " %s\n", dwfl_errmsg (-1));
11007 : }
11008 : }
11009 :
11010 9 : if (split_dbg != NULL)
11011 : {
11012 9 : dbg = split_dbg;
11013 9 : dwflmod = skel_mod;
11014 : }
11015 0 : else if (skel_name == NULL)
11016 0 : fprintf (stderr,
11017 : "Warning: split DWARF file, but no skeleton found.\n");
11018 : }
11019 94 : else if (dwarf_skeleton != NULL)
11020 0 : fprintf (stderr, "Warning: DWARF skeleton given,"
11021 : " but not a split DWARF file\n");
11022 : }
11023 :
11024 : /* Get the section header string table index. */
11025 : size_t shstrndx;
11026 125 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
11027 : error (EXIT_FAILURE, 0,
11028 0 : gettext ("cannot get section header string table index"));
11029 :
11030 : /* If the .debug_info section is listed as implicitly required then
11031 : we must make sure to handle it before handling any other debug
11032 : section. Various other sections depend on the CU DIEs being
11033 : scanned (silently) first. */
11034 125 : bool implicit_info = (implicit_debug_sections & section_info) != 0;
11035 125 : bool explicit_info = (print_debug_sections & section_info) != 0;
11036 125 : if (implicit_info)
11037 : {
11038 : Elf_Scn *scn = NULL;
11039 1381 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
11040 : {
11041 : GElf_Shdr shdr_mem;
11042 1381 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
11043 :
11044 1381 : if (shdr != NULL && shdr->sh_type == SHT_PROGBITS)
11045 : {
11046 602 : const char *name = elf_strptr (ebl->elf, shstrndx,
11047 602 : shdr->sh_name);
11048 602 : if (name == NULL)
11049 0 : continue;
11050 :
11051 602 : if (strcmp (name, ".debug_info") == 0
11052 546 : || strcmp (name, ".debug_info.dwo") == 0
11053 537 : || strcmp (name, ".zdebug_info") == 0
11054 534 : || strcmp (name, ".zdebug_info.dwo") == 0)
11055 : {
11056 : print_debug_info_section (dwflmod, ebl, ehdr,
11057 : scn, shdr, dbg);
11058 68 : break;
11059 : }
11060 : }
11061 : }
11062 68 : print_debug_sections &= ~section_info;
11063 68 : implicit_debug_sections &= ~section_info;
11064 : }
11065 :
11066 : /* Look through all the sections for the debugging sections to print. */
11067 : Elf_Scn *scn = NULL;
11068 3861 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
11069 : {
11070 : GElf_Shdr shdr_mem;
11071 3736 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
11072 :
11073 3736 : if (shdr != NULL && shdr->sh_type == SHT_PROGBITS)
11074 : {
11075 : static const struct
11076 : {
11077 : const char *name;
11078 : enum section_e bitmask;
11079 : void (*fp) (Dwfl_Module *, Ebl *,
11080 : GElf_Ehdr *, Elf_Scn *, GElf_Shdr *, Dwarf *);
11081 : } debug_sections[] =
11082 : {
11083 : #define NEW_SECTION(name) \
11084 : { ".debug_" #name, section_##name, print_debug_##name##_section }
11085 : NEW_SECTION (abbrev),
11086 : NEW_SECTION (addr),
11087 : NEW_SECTION (aranges),
11088 : NEW_SECTION (frame),
11089 : NEW_SECTION (info),
11090 : NEW_SECTION (types),
11091 : NEW_SECTION (line),
11092 : NEW_SECTION (loc),
11093 : /* loclists is loc for DWARF5. */
11094 : { ".debug_loclists", section_loc,
11095 : print_debug_loclists_section },
11096 : NEW_SECTION (pubnames),
11097 : NEW_SECTION (str),
11098 : /* A DWARF5 specialised debug string section. */
11099 : { ".debug_line_str", section_str,
11100 : print_debug_str_section },
11101 : /* DWARF5 string offsets table. */
11102 : { ".debug_str_offsets", section_str,
11103 : print_debug_str_offsets_section },
11104 : NEW_SECTION (macinfo),
11105 : NEW_SECTION (macro),
11106 : NEW_SECTION (ranges),
11107 : /* rnglists is ranges for DWARF5. */
11108 : { ".debug_rnglists", section_ranges,
11109 : print_debug_rnglists_section },
11110 : { ".eh_frame", section_frame | section_exception,
11111 : print_debug_frame_section },
11112 : { ".eh_frame_hdr", section_frame | section_exception,
11113 : print_debug_frame_hdr_section },
11114 : { ".gcc_except_table", section_frame | section_exception,
11115 : print_debug_exception_table },
11116 : { ".gdb_index", section_gdb_index, print_gdb_index_section }
11117 : };
11118 1861 : const int ndebug_sections = (sizeof (debug_sections)
11119 : / sizeof (debug_sections[0]));
11120 1861 : const char *name = elf_strptr (ebl->elf, shstrndx,
11121 1861 : shdr->sh_name);
11122 1861 : if (name == NULL)
11123 0 : continue;
11124 :
11125 : int n;
11126 27782 : for (n = 0; n < ndebug_sections; ++n)
11127 : {
11128 28644 : size_t dbglen = strlen (debug_sections[n].name);
11129 28644 : size_t scnlen = strlen (name);
11130 28644 : if ((strncmp (name, debug_sections[n].name, dbglen) == 0
11131 884 : && (dbglen == scnlen
11132 138 : || (scnlen == dbglen + 4
11133 121 : && strstr (name, ".dwo") == name + dbglen)))
11134 27844 : || (name[0] == '.' && name[1] == 'z'
11135 454 : && debug_sections[n].name[1] == 'd'
11136 454 : && strncmp (&name[2], &debug_sections[n].name[1],
11137 : dbglen - 1) == 0
11138 62 : && (scnlen == dbglen + 1
11139 0 : || (scnlen == dbglen + 5
11140 0 : && strstr (name, ".dwo") == name + dbglen + 1))))
11141 : {
11142 1724 : if ((print_debug_sections | implicit_debug_sections)
11143 862 : & debug_sections[n].bitmask)
11144 311 : debug_sections[n].fp (dwflmod, ebl, ehdr, scn, shdr, dbg);
11145 : break;
11146 : }
11147 : }
11148 : }
11149 : }
11150 :
11151 125 : dwfl_end (skel_dwfl);
11152 125 : free (skel_name);
11153 :
11154 : /* Turn implicit and/or explicit back on in case we go over another file. */
11155 125 : if (implicit_info)
11156 68 : implicit_debug_sections |= section_info;
11157 125 : if (explicit_info)
11158 50 : print_debug_sections |= section_info;
11159 :
11160 : reset_listptr (&known_locsptr);
11161 : reset_listptr (&known_loclistsptr);
11162 : reset_listptr (&known_rangelistptr);
11163 : reset_listptr (&known_rnglistptr);
11164 : reset_listptr (&known_addrbases);
11165 : reset_listptr (&known_stroffbases);
11166 125 : }
11167 :
11168 :
11169 : #define ITEM_INDENT 4
11170 : #define WRAP_COLUMN 75
11171 :
11172 : /* Print "NAME: FORMAT", wrapping when output text would make the line
11173 : exceed WRAP_COLUMN. Unpadded numbers look better for the core items
11174 : but this function is also used for registers which should be printed
11175 : aligned. Fortunately registers output uses fixed fields width (such
11176 : as %11d) for the alignment.
11177 :
11178 : Line breaks should not depend on the particular values although that
11179 : may happen in some cases of the core items. */
11180 :
11181 : static unsigned int
11182 : __attribute__ ((format (printf, 6, 7)))
11183 761 : print_core_item (unsigned int colno, char sep, unsigned int wrap,
11184 : size_t name_width, const char *name, const char *format, ...)
11185 : {
11186 761 : size_t len = strlen (name);
11187 761 : if (name_width < len)
11188 368 : name_width = len;
11189 :
11190 : char *out;
11191 : va_list ap;
11192 761 : va_start (ap, format);
11193 761 : int out_len = vasprintf (&out, format, ap);
11194 761 : va_end (ap);
11195 761 : if (out_len == -1)
11196 0 : error (EXIT_FAILURE, 0, _("memory exhausted"));
11197 :
11198 761 : size_t n = name_width + sizeof ": " - 1 + out_len;
11199 :
11200 761 : if (colno == 0)
11201 : {
11202 : printf ("%*s", ITEM_INDENT, "");
11203 48 : colno = ITEM_INDENT + n;
11204 : }
11205 713 : else if (colno + 2 + n < wrap)
11206 : {
11207 433 : printf ("%c ", sep);
11208 433 : colno += 2 + n;
11209 : }
11210 : else
11211 : {
11212 : printf ("\n%*s", ITEM_INDENT, "");
11213 280 : colno = ITEM_INDENT + n;
11214 : }
11215 :
11216 761 : printf ("%s: %*s%s", name, (int) (name_width - len), "", out);
11217 :
11218 761 : free (out);
11219 :
11220 761 : return colno;
11221 : }
11222 :
11223 : static const void *
11224 838 : convert (Elf *core, Elf_Type type, uint_fast16_t count,
11225 : void *value, const void *data, size_t size)
11226 : {
11227 1676 : Elf_Data valuedata =
11228 : {
11229 : .d_type = type,
11230 : .d_buf = value,
11231 838 : .d_size = size ?: gelf_fsize (core, type, count, EV_CURRENT),
11232 : .d_version = EV_CURRENT,
11233 : };
11234 838 : Elf_Data indata =
11235 : {
11236 : .d_type = type,
11237 : .d_buf = (void *) data,
11238 : .d_size = valuedata.d_size,
11239 : .d_version = EV_CURRENT,
11240 : };
11241 :
11242 1676 : Elf_Data *d = (gelf_getclass (core) == ELFCLASS32
11243 838 : ? elf32_xlatetom : elf64_xlatetom)
11244 838 : (&valuedata, &indata, elf_getident (core, NULL)[EI_DATA]);
11245 838 : if (d == NULL)
11246 0 : error (EXIT_FAILURE, 0,
11247 0 : gettext ("cannot convert core note data: %s"), elf_errmsg (-1));
11248 :
11249 838 : return data + indata.d_size;
11250 : }
11251 :
11252 : typedef uint8_t GElf_Byte;
11253 :
11254 : static unsigned int
11255 371 : handle_core_item (Elf *core, const Ebl_Core_Item *item, const void *desc,
11256 : unsigned int colno, size_t *repeated_size)
11257 : {
11258 371 : uint_fast16_t count = item->count ?: 1;
11259 : /* Ebl_Core_Item count is always a small number.
11260 : Make sure the backend didn't put in some large bogus value. */
11261 371 : assert (count < 128);
11262 :
11263 : #define TYPES \
11264 : DO_TYPE (BYTE, Byte, "0x%.2" PRIx8, "%" PRId8); \
11265 : DO_TYPE (HALF, Half, "0x%.4" PRIx16, "%" PRId16); \
11266 : DO_TYPE (WORD, Word, "0x%.8" PRIx32, "%" PRId32); \
11267 : DO_TYPE (SWORD, Sword, "%" PRId32, "%" PRId32); \
11268 : DO_TYPE (XWORD, Xword, "0x%.16" PRIx64, "%" PRId64); \
11269 : DO_TYPE (SXWORD, Sxword, "%" PRId64, "%" PRId64)
11270 :
11271 : #define DO_TYPE(NAME, Name, hex, dec) GElf_##Name Name
11272 : typedef union { TYPES; } value_t;
11273 371 : void *data = alloca (count * sizeof (value_t));
11274 : #undef DO_TYPE
11275 :
11276 : #define DO_TYPE(NAME, Name, hex, dec) \
11277 : GElf_##Name *value_##Name __attribute__((unused)) = data
11278 371 : TYPES;
11279 : #undef DO_TYPE
11280 :
11281 371 : size_t size = gelf_fsize (core, item->type, count, EV_CURRENT);
11282 371 : size_t convsize = size;
11283 371 : if (repeated_size != NULL)
11284 : {
11285 1 : if (*repeated_size > size && (item->format == 'b' || item->format == 'B'))
11286 : {
11287 0 : data = alloca (*repeated_size);
11288 0 : count *= *repeated_size / size;
11289 0 : convsize = count * size;
11290 0 : *repeated_size -= convsize;
11291 : }
11292 1 : else if (item->count != 0 || item->format != '\n')
11293 0 : *repeated_size -= size;
11294 : }
11295 :
11296 371 : convert (core, item->type, count, data, desc + item->offset, convsize);
11297 :
11298 371 : Elf_Type type = item->type;
11299 371 : if (type == ELF_T_ADDR)
11300 0 : type = gelf_getclass (core) == ELFCLASS32 ? ELF_T_WORD : ELF_T_XWORD;
11301 :
11302 371 : switch (item->format)
11303 : {
11304 : case 'd':
11305 175 : assert (count == 1);
11306 175 : switch (type)
11307 : {
11308 : #define DO_TYPE(NAME, Name, hex, dec) \
11309 : case ELF_T_##NAME: \
11310 : colno = print_core_item (colno, ',', WRAP_COLUMN, \
11311 : 0, item->name, dec, value_##Name[0]); \
11312 : break
11313 24 : TYPES;
11314 : #undef DO_TYPE
11315 : default:
11316 0 : abort ();
11317 : }
11318 : break;
11319 :
11320 : case 'x':
11321 109 : assert (count == 1);
11322 109 : switch (type)
11323 : {
11324 : #define DO_TYPE(NAME, Name, hex, dec) \
11325 : case ELF_T_##NAME: \
11326 : colno = print_core_item (colno, ',', WRAP_COLUMN, \
11327 : 0, item->name, hex, value_##Name[0]); \
11328 : break
11329 0 : TYPES;
11330 : #undef DO_TYPE
11331 : default:
11332 0 : abort ();
11333 : }
11334 : break;
11335 :
11336 : case 'b':
11337 : case 'B':
11338 20 : assert (size % sizeof (unsigned int) == 0);
11339 20 : unsigned int nbits = count * size * 8;
11340 20 : unsigned int pop = 0;
11341 50 : for (const unsigned int *i = data; (void *) i < data + count * size; ++i)
11342 30 : pop += __builtin_popcount (*i);
11343 20 : bool negate = pop > nbits / 2;
11344 20 : const unsigned int bias = item->format == 'b';
11345 :
11346 : {
11347 20 : char printed[(negate ? nbits - pop : pop) * 16 + 1];
11348 20 : char *p = printed;
11349 20 : *p = '\0';
11350 :
11351 : if (BYTE_ORDER != LITTLE_ENDIAN && size > sizeof (unsigned int))
11352 : {
11353 : assert (size == sizeof (unsigned int) * 2);
11354 : for (unsigned int *i = data;
11355 : (void *) i < data + count * size; i += 2)
11356 : {
11357 : unsigned int w = i[1];
11358 : i[1] = i[0];
11359 : i[0] = w;
11360 : }
11361 : }
11362 :
11363 20 : unsigned int lastbit = 0;
11364 20 : unsigned int run = 0;
11365 70 : for (const unsigned int *i = data;
11366 30 : (void *) i < data + count * size; ++i)
11367 : {
11368 30 : unsigned int bit = ((void *) i - data) * 8;
11369 30 : unsigned int w = negate ? ~*i : *i;
11370 60 : while (w != 0)
11371 : {
11372 : /* Note that a right shift equal to (or greater than)
11373 : the number of bits of w is undefined behaviour. In
11374 : particular when the least significant bit is bit 32
11375 : (w = 0x8000000) then w >>= n is undefined. So
11376 : explicitly handle that case separately. */
11377 0 : unsigned int n = ffs (w);
11378 0 : if (n < sizeof (w) * 8)
11379 0 : w >>= n;
11380 : else
11381 : w = 0;
11382 0 : bit += n;
11383 :
11384 0 : if (lastbit != 0 && lastbit + 1 == bit)
11385 0 : ++run;
11386 : else
11387 : {
11388 0 : if (lastbit == 0)
11389 0 : p += sprintf (p, "%u", bit - bias);
11390 0 : else if (run == 0)
11391 0 : p += sprintf (p, ",%u", bit - bias);
11392 : else
11393 0 : p += sprintf (p, "-%u,%u", lastbit - bias, bit - bias);
11394 : run = 0;
11395 : }
11396 :
11397 0 : lastbit = bit;
11398 : }
11399 : }
11400 20 : if (lastbit > 0 && run > 0 && lastbit + 1 != nbits)
11401 0 : p += sprintf (p, "-%u", lastbit - bias);
11402 :
11403 20 : colno = print_core_item (colno, ',', WRAP_COLUMN, 0, item->name,
11404 : negate ? "~<%s>" : "<%s>", printed);
11405 : }
11406 20 : break;
11407 :
11408 : case 'T':
11409 : case (char) ('T'|0x80):
11410 40 : assert (count == 2);
11411 : Dwarf_Word sec;
11412 : Dwarf_Word usec;
11413 40 : switch (type)
11414 : {
11415 : #define DO_TYPE(NAME, Name, hex, dec) \
11416 : case ELF_T_##NAME: \
11417 : sec = value_##Name[0]; \
11418 : usec = value_##Name[1]; \
11419 : break
11420 0 : TYPES;
11421 : #undef DO_TYPE
11422 : default:
11423 0 : abort ();
11424 : }
11425 40 : if (unlikely (item->format == (char) ('T'|0x80)))
11426 : {
11427 : /* This is a hack for an ill-considered 64-bit ABI where
11428 : tv_usec is actually a 32-bit field with 32 bits of padding
11429 : rounding out struct timeval. We've already converted it as
11430 : a 64-bit field. For little-endian, this just means the
11431 : high half is the padding; it's presumably zero, but should
11432 : be ignored anyway. For big-endian, it means the 32-bit
11433 : field went into the high half of USEC. */
11434 : GElf_Ehdr ehdr_mem;
11435 0 : GElf_Ehdr *ehdr = gelf_getehdr (core, &ehdr_mem);
11436 0 : if (likely (ehdr->e_ident[EI_DATA] == ELFDATA2MSB))
11437 0 : usec >>= 32;
11438 : else
11439 0 : usec &= UINT32_MAX;
11440 : }
11441 40 : colno = print_core_item (colno, ',', WRAP_COLUMN, 0, item->name,
11442 : "%" PRIu64 ".%.6" PRIu64, sec, usec);
11443 40 : break;
11444 :
11445 : case 'c':
11446 8 : assert (count == 1);
11447 8 : colno = print_core_item (colno, ',', WRAP_COLUMN, 0, item->name,
11448 8 : "%c", value_Byte[0]);
11449 8 : break;
11450 :
11451 : case 's':
11452 16 : colno = print_core_item (colno, ',', WRAP_COLUMN, 0, item->name,
11453 : "%.*s", (int) count, value_Byte);
11454 16 : break;
11455 :
11456 : case '\n':
11457 : /* This is a list of strings separated by '\n'. */
11458 1 : assert (item->count == 0);
11459 1 : assert (repeated_size != NULL);
11460 1 : assert (item->name == NULL);
11461 1 : if (unlikely (item->offset >= *repeated_size))
11462 : break;
11463 :
11464 1 : const char *s = desc + item->offset;
11465 1 : size = *repeated_size - item->offset;
11466 1 : *repeated_size = 0;
11467 49 : while (size > 0)
11468 : {
11469 48 : const char *eol = memchr (s, '\n', size);
11470 48 : int len = size;
11471 48 : if (eol != NULL)
11472 47 : len = eol - s;
11473 : printf ("%*s%.*s\n", ITEM_INDENT, "", len, s);
11474 48 : if (eol == NULL)
11475 : break;
11476 47 : size -= eol + 1 - s;
11477 47 : s = eol + 1;
11478 : }
11479 :
11480 : colno = WRAP_COLUMN;
11481 : break;
11482 :
11483 : case 'h':
11484 : break;
11485 :
11486 : default:
11487 0 : error (0, 0, "XXX not handling format '%c' for %s",
11488 : item->format, item->name);
11489 : break;
11490 : }
11491 :
11492 : #undef TYPES
11493 :
11494 371 : return colno;
11495 : }
11496 :
11497 :
11498 : /* Sort items by group, and by layout offset within each group. */
11499 : static int
11500 827 : compare_core_items (const void *a, const void *b)
11501 : {
11502 827 : const Ebl_Core_Item *const *p1 = a;
11503 827 : const Ebl_Core_Item *const *p2 = b;
11504 827 : const Ebl_Core_Item *item1 = *p1;
11505 827 : const Ebl_Core_Item *item2 = *p2;
11506 :
11507 1654 : return ((item1->group == item2->group ? 0
11508 827 : : strcmp (item1->group, item2->group))
11509 827 : ?: (int) item1->offset - (int) item2->offset);
11510 : }
11511 :
11512 : /* Sort item groups by layout offset of the first item in the group. */
11513 : static int
11514 126 : compare_core_item_groups (const void *a, const void *b)
11515 : {
11516 126 : const Ebl_Core_Item *const *const *p1 = a;
11517 126 : const Ebl_Core_Item *const *const *p2 = b;
11518 126 : const Ebl_Core_Item *const *group1 = *p1;
11519 126 : const Ebl_Core_Item *const *group2 = *p2;
11520 126 : const Ebl_Core_Item *item1 = *group1;
11521 126 : const Ebl_Core_Item *item2 = *group2;
11522 :
11523 126 : return (int) item1->offset - (int) item2->offset;
11524 : }
11525 :
11526 : static unsigned int
11527 35 : handle_core_items (Elf *core, const void *desc, size_t descsz,
11528 : const Ebl_Core_Item *items, size_t nitems)
11529 : {
11530 35 : if (nitems == 0)
11531 : return 0;
11532 32 : unsigned int colno = 0;
11533 :
11534 : /* FORMAT '\n' makes sense to be present only as a single item as it
11535 : processes all the data of a note. FORMATs 'b' and 'B' have a special case
11536 : if present as a single item but they can be also processed with other
11537 : items below. */
11538 32 : if (nitems == 1 && (items[0].format == '\n' || items[0].format == 'b'
11539 8 : || items[0].format == 'B'))
11540 : {
11541 1 : assert (items[0].offset == 0);
11542 1 : size_t size = descsz;
11543 1 : colno = handle_core_item (core, items, desc, colno, &size);
11544 : /* If SIZE is not zero here there is some remaining data. But we do not
11545 : know how to process it anyway. */
11546 : return colno;
11547 : }
11548 362 : for (size_t i = 0; i < nitems; ++i)
11549 362 : assert (items[i].format != '\n');
11550 :
11551 : /* Sort to collect the groups together. */
11552 31 : const Ebl_Core_Item *sorted_items[nitems];
11553 393 : for (size_t i = 0; i < nitems; ++i)
11554 362 : sorted_items[i] = &items[i];
11555 31 : qsort (sorted_items, nitems, sizeof sorted_items[0], &compare_core_items);
11556 :
11557 : /* Collect the unique groups and sort them. */
11558 31 : const Ebl_Core_Item **groups[nitems];
11559 31 : groups[0] = &sorted_items[0];
11560 31 : size_t ngroups = 1;
11561 362 : for (size_t i = 1; i < nitems; ++i)
11562 331 : if (sorted_items[i]->group != sorted_items[i - 1]->group
11563 68 : && strcmp (sorted_items[i]->group, sorted_items[i - 1]->group))
11564 68 : groups[ngroups++] = &sorted_items[i];
11565 31 : qsort (groups, ngroups, sizeof groups[0], &compare_core_item_groups);
11566 :
11567 : /* Write out all the groups. */
11568 31 : const void *last = desc;
11569 : do
11570 : {
11571 134 : for (size_t i = 0; i < ngroups; ++i)
11572 : {
11573 572 : for (const Ebl_Core_Item **item = groups[i];
11574 471 : (item < &sorted_items[nitems]
11575 438 : && ((*item)->group == groups[i][0]->group
11576 68 : || !strcmp ((*item)->group, groups[i][0]->group)));
11577 370 : ++item)
11578 370 : colno = handle_core_item (core, *item, desc, colno, NULL);
11579 :
11580 : /* Force a line break at the end of the group. */
11581 101 : colno = WRAP_COLUMN;
11582 : }
11583 :
11584 33 : if (descsz == 0)
11585 : break;
11586 :
11587 : /* This set of items consumed a certain amount of the note's data.
11588 : If there is more data there, we have another unit of the same size.
11589 : Loop to print that out too. */
11590 19 : const Ebl_Core_Item *item = &items[nitems - 1];
11591 38 : size_t eltsz = item->offset + gelf_fsize (core, item->type,
11592 19 : item->count ?: 1, EV_CURRENT);
11593 :
11594 19 : int reps = -1;
11595 : do
11596 : {
11597 19 : ++reps;
11598 19 : desc += eltsz;
11599 19 : descsz -= eltsz;
11600 : }
11601 19 : while (descsz >= eltsz && !memcmp (desc, last, eltsz));
11602 :
11603 19 : if (reps == 1)
11604 : {
11605 : /* For just one repeat, print it unabridged twice. */
11606 0 : desc -= eltsz;
11607 0 : descsz += eltsz;
11608 : }
11609 19 : else if (reps > 1)
11610 0 : printf (gettext ("\n%*s... <repeats %u more times> ..."),
11611 : ITEM_INDENT, "", reps);
11612 :
11613 19 : last = desc;
11614 : }
11615 19 : while (descsz > 0);
11616 :
11617 : return colno;
11618 : }
11619 :
11620 : static unsigned int
11621 : handle_bit_registers (const Ebl_Register_Location *regloc, const void *desc,
11622 : unsigned int colno)
11623 : {
11624 0 : desc += regloc->offset;
11625 :
11626 0 : abort (); /* XXX */
11627 : return colno;
11628 : }
11629 :
11630 :
11631 : static unsigned int
11632 161 : handle_core_register (Ebl *ebl, Elf *core, int maxregname,
11633 : const Ebl_Register_Location *regloc, const void *desc,
11634 : unsigned int colno)
11635 : {
11636 161 : if (regloc->bits % 8 != 0)
11637 0 : return handle_bit_registers (regloc, desc, colno);
11638 :
11639 161 : desc += regloc->offset;
11640 :
11641 554 : for (int reg = regloc->regno; reg < regloc->regno + regloc->count; ++reg)
11642 : {
11643 : char name[REGNAMESZ];
11644 : int bits;
11645 : int type;
11646 393 : register_info (ebl, reg, regloc, name, &bits, &type);
11647 :
11648 : #define TYPES \
11649 : BITS (8, BYTE, "%4" PRId8, "0x%.2" PRIx8); \
11650 : BITS (16, HALF, "%6" PRId16, "0x%.4" PRIx16); \
11651 : BITS (32, WORD, "%11" PRId32, " 0x%.8" PRIx32); \
11652 : BITS (64, XWORD, "%20" PRId64, " 0x%.16" PRIx64)
11653 :
11654 : #define BITS(bits, xtype, sfmt, ufmt) \
11655 : uint##bits##_t b##bits; int##bits##_t b##bits##s
11656 : union { TYPES; uint64_t b128[2]; } value;
11657 : #undef BITS
11658 :
11659 393 : switch (type)
11660 : {
11661 : case DW_ATE_unsigned:
11662 : case DW_ATE_signed:
11663 : case DW_ATE_address:
11664 305 : switch (bits)
11665 : {
11666 : #define BITS(bits, xtype, sfmt, ufmt) \
11667 : case bits: \
11668 : desc = convert (core, ELF_T_##xtype, 1, &value, desc, 0); \
11669 : if (type == DW_ATE_signed) \
11670 : colno = print_core_item (colno, ' ', WRAP_COLUMN, \
11671 : maxregname, name, \
11672 : sfmt, value.b##bits##s); \
11673 : else \
11674 : colno = print_core_item (colno, ' ', WRAP_COLUMN, \
11675 : maxregname, name, \
11676 : ufmt, value.b##bits); \
11677 : break
11678 :
11679 0 : TYPES;
11680 :
11681 : case 128:
11682 48 : assert (type == DW_ATE_unsigned);
11683 48 : desc = convert (core, ELF_T_XWORD, 2, &value, desc, 0);
11684 48 : int be = elf_getident (core, NULL)[EI_DATA] == ELFDATA2MSB;
11685 96 : colno = print_core_item (colno, ' ', WRAP_COLUMN,
11686 : maxregname, name,
11687 : "0x%.16" PRIx64 "%.16" PRIx64,
11688 48 : value.b128[!be], value.b128[be]);
11689 48 : break;
11690 :
11691 : default:
11692 0 : abort ();
11693 : #undef BITS
11694 : }
11695 : break;
11696 :
11697 : default:
11698 : /* Print each byte in hex, the whole thing in native byte order. */
11699 88 : assert (bits % 8 == 0);
11700 88 : const uint8_t *bytes = desc;
11701 88 : desc += bits / 8;
11702 88 : char hex[bits / 4 + 1];
11703 88 : hex[bits / 4] = '\0';
11704 88 : int incr = 1;
11705 88 : if (elf_getident (core, NULL)[EI_DATA] == ELFDATA2LSB)
11706 : {
11707 48 : bytes += bits / 8 - 1;
11708 48 : incr = -1;
11709 : }
11710 88 : size_t idx = 0;
11711 872 : for (char *h = hex; bits > 0; bits -= 8, idx += incr)
11712 : {
11713 784 : *h++ = "0123456789abcdef"[bytes[idx] >> 4];
11714 784 : *h++ = "0123456789abcdef"[bytes[idx] & 0xf];
11715 : }
11716 88 : colno = print_core_item (colno, ' ', WRAP_COLUMN,
11717 : maxregname, name, "0x%s", hex);
11718 : break;
11719 : }
11720 393 : desc += regloc->pad;
11721 :
11722 : #undef TYPES
11723 : }
11724 :
11725 : return colno;
11726 : }
11727 :
11728 :
11729 : struct register_info
11730 : {
11731 : const Ebl_Register_Location *regloc;
11732 : const char *set;
11733 : char name[REGNAMESZ];
11734 : int regno;
11735 : int bits;
11736 : int type;
11737 : };
11738 :
11739 : static int
11740 : register_bitpos (const struct register_info *r)
11741 : {
11742 3832 : return (r->regloc->offset * 8
11743 1916 : + ((r->regno - r->regloc->regno)
11744 1916 : * (r->regloc->bits + r->regloc->pad * 8)));
11745 : }
11746 :
11747 : static int
11748 2903 : compare_sets_by_info (const struct register_info *r1,
11749 1916 : const struct register_info *r2)
11750 : {
11751 1974 : return ((int) r2->bits - (int) r1->bits
11752 5777 : ?: register_bitpos (r1) - register_bitpos (r2));
11753 : }
11754 :
11755 : /* Sort registers by set, and by size and layout offset within each set. */
11756 : static int
11757 4489 : compare_registers (const void *a, const void *b)
11758 : {
11759 4489 : const struct register_info *r1 = a;
11760 4489 : const struct register_info *r2 = b;
11761 :
11762 : /* Unused elements sort last. */
11763 4489 : if (r1->regloc == NULL)
11764 3278 : return r2->regloc == NULL ? 0 : 1;
11765 1211 : if (r2->regloc == NULL)
11766 : return -1;
11767 :
11768 2092 : return ((r1->set == r2->set ? 0 : strcmp (r1->set, r2->set))
11769 1046 : ?: compare_sets_by_info (r1, r2));
11770 : }
11771 :
11772 : /* Sort register sets by layout offset of the first register in the set. */
11773 : static int
11774 20 : compare_register_sets (const void *a, const void *b)
11775 : {
11776 20 : const struct register_info *const *p1 = a;
11777 20 : const struct register_info *const *p2 = b;
11778 20 : return compare_sets_by_info (*p1, *p2);
11779 : }
11780 :
11781 : static unsigned int
11782 35 : handle_core_registers (Ebl *ebl, Elf *core, const void *desc,
11783 : const Ebl_Register_Location *reglocs, size_t nregloc)
11784 : {
11785 35 : if (nregloc == 0)
11786 : return 0;
11787 :
11788 17 : ssize_t maxnreg = ebl_register_info (ebl, 0, NULL, 0, NULL, NULL, NULL, NULL);
11789 17 : if (maxnreg <= 0)
11790 : {
11791 0 : for (size_t i = 0; i < nregloc; ++i)
11792 0 : if (maxnreg < reglocs[i].regno + reglocs[i].count)
11793 0 : maxnreg = reglocs[i].regno + reglocs[i].count;
11794 0 : assert (maxnreg > 0);
11795 : }
11796 :
11797 17 : struct register_info regs[maxnreg];
11798 17 : memset (regs, 0, sizeof regs);
11799 :
11800 : /* Sort to collect the sets together. */
11801 17 : int maxreg = 0;
11802 178 : for (size_t i = 0; i < nregloc; ++i)
11803 715 : for (int reg = reglocs[i].regno;
11804 554 : reg < reglocs[i].regno + reglocs[i].count;
11805 393 : ++reg)
11806 : {
11807 393 : assert (reg < maxnreg);
11808 393 : if (reg > maxreg)
11809 199 : maxreg = reg;
11810 393 : struct register_info *info = ®s[reg];
11811 393 : info->regloc = ®locs[i];
11812 393 : info->regno = reg;
11813 786 : info->set = register_info (ebl, reg, ®locs[i],
11814 393 : info->name, &info->bits, &info->type);
11815 : }
11816 17 : qsort (regs, maxreg + 1, sizeof regs[0], &compare_registers);
11817 :
11818 : /* Collect the unique sets and sort them. */
11819 802 : inline bool same_set (const struct register_info *a,
11820 : const struct register_info *b)
11821 : {
11822 2406 : return (a < ®s[maxnreg] && a->regloc != NULL
11823 802 : && b < ®s[maxnreg] && b->regloc != NULL
11824 785 : && a->bits == b->bits
11825 1569 : && (a->set == b->set || !strcmp (a->set, b->set)));
11826 : }
11827 17 : struct register_info *sets[maxreg + 1];
11828 17 : sets[0] = ®s[0];
11829 17 : size_t nsets = 1;
11830 1247 : for (int i = 1; i <= maxreg; ++i)
11831 1230 : if (regs[i].regloc != NULL && !same_set (®s[i], ®s[i - 1]))
11832 16 : sets[nsets++] = ®s[i];
11833 17 : qsort (sets, nsets, sizeof sets[0], &compare_register_sets);
11834 :
11835 : /* Write out all the sets. */
11836 17 : unsigned int colno = 0;
11837 50 : for (size_t i = 0; i < nsets; ++i)
11838 : {
11839 : /* Find the longest name of a register in this set. */
11840 33 : size_t maxname = 0;
11841 : const struct register_info *end;
11842 426 : for (end = sets[i]; same_set (sets[i], end); ++end)
11843 : {
11844 393 : size_t len = strlen (end->name);
11845 393 : if (len > maxname)
11846 50 : maxname = len;
11847 : }
11848 :
11849 194 : for (const struct register_info *reg = sets[i];
11850 : reg < end;
11851 161 : reg += reg->regloc->count ?: 1)
11852 161 : colno = handle_core_register (ebl, core, maxname,
11853 : reg->regloc, desc, colno);
11854 :
11855 : /* Force a line break at the end of the group. */
11856 33 : colno = WRAP_COLUMN;
11857 : }
11858 :
11859 : return colno;
11860 : }
11861 :
11862 : static void
11863 8 : handle_auxv_note (Ebl *ebl, Elf *core, GElf_Word descsz, GElf_Off desc_pos)
11864 : {
11865 8 : Elf_Data *data = elf_getdata_rawchunk (core, desc_pos, descsz, ELF_T_AUXV);
11866 8 : if (data == NULL)
11867 : elf_error:
11868 0 : error (EXIT_FAILURE, 0,
11869 0 : gettext ("cannot convert core note data: %s"), elf_errmsg (-1));
11870 :
11871 8 : const size_t nauxv = descsz / gelf_fsize (core, ELF_T_AUXV, 1, EV_CURRENT);
11872 158 : for (size_t i = 0; i < nauxv; ++i)
11873 : {
11874 : GElf_auxv_t av_mem;
11875 150 : GElf_auxv_t *av = gelf_getauxv (data, i, &av_mem);
11876 150 : if (av == NULL)
11877 : goto elf_error;
11878 :
11879 : const char *name;
11880 : const char *fmt;
11881 150 : if (ebl_auxv_info (ebl, av->a_type, &name, &fmt) == 0)
11882 : {
11883 : /* Unknown type. */
11884 0 : if (av->a_un.a_val == 0)
11885 0 : printf (" %" PRIu64 "\n", av->a_type);
11886 : else
11887 0 : printf (" %" PRIu64 ": %#" PRIx64 "\n",
11888 : av->a_type, av->a_un.a_val);
11889 : }
11890 : else
11891 150 : switch (fmt[0])
11892 : {
11893 : case '\0': /* Normally zero. */
11894 8 : if (av->a_un.a_val == 0)
11895 : {
11896 8 : printf (" %s\n", name);
11897 : break;
11898 : }
11899 : FALLTHROUGH;
11900 : case 'x': /* hex */
11901 : case 'p': /* address */
11902 : case 's': /* address of string */
11903 66 : printf (" %s: %#" PRIx64 "\n", name, av->a_un.a_val);
11904 : break;
11905 : case 'u':
11906 72 : printf (" %s: %" PRIu64 "\n", name, av->a_un.a_val);
11907 : break;
11908 : case 'd':
11909 0 : printf (" %s: %" PRId64 "\n", name, av->a_un.a_val);
11910 : break;
11911 :
11912 : case 'b':
11913 4 : printf (" %s: %#" PRIx64 " ", name, av->a_un.a_val);
11914 4 : GElf_Xword bit = 1;
11915 4 : const char *pfx = "<";
11916 110 : for (const char *p = fmt + 1; *p != 0; p = strchr (p, '\0') + 1)
11917 : {
11918 106 : if (av->a_un.a_val & bit)
11919 : {
11920 : printf ("%s%s", pfx, p);
11921 77 : pfx = " ";
11922 : }
11923 106 : bit <<= 1;
11924 : }
11925 : printf (">\n");
11926 : break;
11927 :
11928 : default:
11929 0 : abort ();
11930 : }
11931 : }
11932 8 : }
11933 :
11934 : static bool
11935 : buf_has_data (unsigned char const *ptr, unsigned char const *end, size_t sz)
11936 : {
11937 162 : return ptr < end && (size_t) (end - ptr) >= sz;
11938 : }
11939 :
11940 : static bool
11941 15 : buf_read_int (Elf *core, unsigned char const **ptrp, unsigned char const *end,
11942 : int *retp)
11943 : {
11944 30 : if (! buf_has_data (*ptrp, end, 4))
11945 : return false;
11946 :
11947 15 : *ptrp = convert (core, ELF_T_WORD, 1, retp, *ptrp, 4);
11948 15 : return true;
11949 : }
11950 :
11951 : static bool
11952 147 : buf_read_ulong (Elf *core, unsigned char const **ptrp, unsigned char const *end,
11953 : uint64_t *retp)
11954 : {
11955 147 : size_t sz = gelf_fsize (core, ELF_T_ADDR, 1, EV_CURRENT);
11956 294 : if (! buf_has_data (*ptrp, end, sz))
11957 : return false;
11958 :
11959 : union
11960 : {
11961 : uint64_t u64;
11962 : uint32_t u32;
11963 : } u;
11964 :
11965 147 : *ptrp = convert (core, ELF_T_ADDR, 1, &u, *ptrp, sz);
11966 :
11967 147 : if (sz == 4)
11968 93 : *retp = u.u32;
11969 : else
11970 54 : *retp = u.u64;
11971 : return true;
11972 : }
11973 :
11974 : static void
11975 5 : handle_siginfo_note (Elf *core, GElf_Word descsz, GElf_Off desc_pos)
11976 : {
11977 5 : Elf_Data *data = elf_getdata_rawchunk (core, desc_pos, descsz, ELF_T_BYTE);
11978 5 : if (data == NULL)
11979 0 : error (EXIT_FAILURE, 0,
11980 0 : gettext ("cannot convert core note data: %s"), elf_errmsg (-1));
11981 :
11982 5 : unsigned char const *ptr = data->d_buf;
11983 5 : unsigned char const *const end = data->d_buf + data->d_size;
11984 :
11985 : /* Siginfo head is three ints: signal number, error number, origin
11986 : code. */
11987 : int si_signo, si_errno, si_code;
11988 5 : if (! buf_read_int (core, &ptr, end, &si_signo)
11989 5 : || ! buf_read_int (core, &ptr, end, &si_errno)
11990 5 : || ! buf_read_int (core, &ptr, end, &si_code))
11991 : {
11992 : fail:
11993 : printf (" Not enough data in NT_SIGINFO note.\n");
11994 5 : return;
11995 : }
11996 :
11997 : /* Next is a pointer-aligned union of structures. On 64-bit
11998 : machines, that implies a word of padding. */
11999 5 : if (gelf_getclass (core) == ELFCLASS64)
12000 2 : ptr += 4;
12001 :
12002 5 : printf (" si_signo: %d, si_errno: %d, si_code: %d\n",
12003 : si_signo, si_errno, si_code);
12004 :
12005 5 : if (si_code > 0)
12006 5 : switch (si_signo)
12007 : {
12008 : case CORE_SIGILL:
12009 : case CORE_SIGFPE:
12010 : case CORE_SIGSEGV:
12011 : case CORE_SIGBUS:
12012 : {
12013 : uint64_t addr;
12014 5 : if (! buf_read_ulong (core, &ptr, end, &addr))
12015 : goto fail;
12016 5 : printf (" fault address: %#" PRIx64 "\n", addr);
12017 5 : break;
12018 : }
12019 : default:
12020 : ;
12021 : }
12022 0 : else if (si_code == CORE_SI_USER)
12023 : {
12024 : int pid, uid;
12025 0 : if (! buf_read_int (core, &ptr, end, &pid)
12026 0 : || ! buf_read_int (core, &ptr, end, &uid))
12027 : goto fail;
12028 0 : printf (" sender PID: %d, sender UID: %d\n", pid, uid);
12029 : }
12030 : }
12031 :
12032 : static void
12033 5 : handle_file_note (Elf *core, GElf_Word descsz, GElf_Off desc_pos)
12034 : {
12035 5 : Elf_Data *data = elf_getdata_rawchunk (core, desc_pos, descsz, ELF_T_BYTE);
12036 5 : if (data == NULL)
12037 0 : error (EXIT_FAILURE, 0,
12038 0 : gettext ("cannot convert core note data: %s"), elf_errmsg (-1));
12039 :
12040 5 : unsigned char const *ptr = data->d_buf;
12041 5 : unsigned char const *const end = data->d_buf + data->d_size;
12042 :
12043 : uint64_t count, page_size;
12044 5 : if (! buf_read_ulong (core, &ptr, end, &count)
12045 5 : || ! buf_read_ulong (core, &ptr, end, &page_size))
12046 : {
12047 : fail:
12048 : printf (" Not enough data in NT_FILE note.\n");
12049 5 : return;
12050 : }
12051 :
12052 5 : size_t addrsize = gelf_fsize (core, ELF_T_ADDR, 1, EV_CURRENT);
12053 5 : uint64_t maxcount = (size_t) (end - ptr) / (3 * addrsize);
12054 5 : if (count > maxcount)
12055 : goto fail;
12056 :
12057 : /* Where file names are stored. */
12058 5 : unsigned char const *const fstart = ptr + 3 * count * addrsize;
12059 5 : char const *fptr = (char *) fstart;
12060 :
12061 5 : printf (" %" PRId64 " files:\n", count);
12062 49 : for (uint64_t i = 0; i < count; ++i)
12063 : {
12064 : uint64_t mstart, mend, moffset;
12065 44 : if (! buf_read_ulong (core, &ptr, fstart, &mstart)
12066 44 : || ! buf_read_ulong (core, &ptr, fstart, &mend)
12067 44 : || ! buf_read_ulong (core, &ptr, fstart, &moffset))
12068 : goto fail;
12069 :
12070 44 : const char *fnext = memchr (fptr, '\0', (char *) end - fptr);
12071 44 : if (fnext == NULL)
12072 : goto fail;
12073 :
12074 88 : int ct = printf (" %08" PRIx64 "-%08" PRIx64
12075 : " %08" PRIx64 " %" PRId64,
12076 : mstart, mend, moffset * page_size, mend - mstart);
12077 44 : printf ("%*s%s\n", ct > 50 ? 3 : 53 - ct, "", fptr);
12078 :
12079 44 : fptr = fnext + 1;
12080 : }
12081 : }
12082 :
12083 : static void
12084 36 : handle_core_note (Ebl *ebl, const GElf_Nhdr *nhdr,
12085 : const char *name, const void *desc)
12086 : {
12087 : GElf_Word regs_offset;
12088 : size_t nregloc;
12089 : const Ebl_Register_Location *reglocs;
12090 : size_t nitems;
12091 : const Ebl_Core_Item *items;
12092 :
12093 36 : if (! ebl_core_note (ebl, nhdr, name,
12094 : ®s_offset, &nregloc, ®locs, &nitems, &items))
12095 1 : return;
12096 :
12097 : /* Pass 0 for DESCSZ when there are registers in the note,
12098 : so that the ITEMS array does not describe the whole thing.
12099 : For non-register notes, the actual descsz might be a multiple
12100 : of the unit size, not just exactly the unit size. */
12101 88 : unsigned int colno = handle_core_items (ebl->elf, desc,
12102 53 : nregloc == 0 ? nhdr->n_descsz : 0,
12103 : items, nitems);
12104 35 : if (colno != 0)
12105 : putchar_unlocked ('\n');
12106 :
12107 35 : colno = handle_core_registers (ebl, ebl->elf, desc + regs_offset,
12108 : reglocs, nregloc);
12109 35 : if (colno != 0)
12110 : putchar_unlocked ('\n');
12111 : }
12112 :
12113 : static void
12114 40 : handle_notes_data (Ebl *ebl, const GElf_Ehdr *ehdr,
12115 : GElf_Off start, Elf_Data *data)
12116 : {
12117 40 : fputs_unlocked (gettext (" Owner Data size Type\n"), stdout);
12118 :
12119 40 : if (data == NULL)
12120 : goto bad_note;
12121 :
12122 : size_t offset = 0;
12123 : GElf_Nhdr nhdr;
12124 : size_t name_offset;
12125 : size_t desc_offset;
12126 126 : while (offset < data->d_size
12127 86 : && (offset = gelf_getnote (data, offset,
12128 : &nhdr, &name_offset, &desc_offset)) > 0)
12129 : {
12130 86 : const char *name = nhdr.n_namesz == 0 ? "" : data->d_buf + name_offset;
12131 86 : const char *desc = data->d_buf + desc_offset;
12132 :
12133 : char buf[100];
12134 : char buf2[100];
12135 258 : printf (gettext (" %-13.*s %9" PRId32 " %s\n"),
12136 86 : (int) nhdr.n_namesz, name, nhdr.n_descsz,
12137 86 : ehdr->e_type == ET_CORE
12138 54 : ? ebl_core_note_type_name (ebl, nhdr.n_type,
12139 : buf, sizeof (buf))
12140 32 : : ebl_object_note_type_name (ebl, name, nhdr.n_type,
12141 : buf2, sizeof (buf2)));
12142 :
12143 : /* Filter out invalid entries. */
12144 : if (memchr (name, '\0', nhdr.n_namesz) != NULL
12145 : /* XXX For now help broken Linux kernels. */
12146 : || 1)
12147 : {
12148 86 : if (ehdr->e_type == ET_CORE)
12149 : {
12150 54 : if (nhdr.n_type == NT_AUXV
12151 8 : && (nhdr.n_namesz == 4 /* Broken old Linux kernels. */
12152 8 : || (nhdr.n_namesz == 5 && name[4] == '\0'))
12153 8 : && !memcmp (name, "CORE", 4))
12154 8 : handle_auxv_note (ebl, ebl->elf, nhdr.n_descsz,
12155 : start + desc_offset);
12156 46 : else if (nhdr.n_namesz == 5 && strcmp (name, "CORE") == 0)
12157 34 : switch (nhdr.n_type)
12158 : {
12159 : case NT_SIGINFO:
12160 5 : handle_siginfo_note (ebl->elf, nhdr.n_descsz,
12161 : start + desc_offset);
12162 : break;
12163 :
12164 : case NT_FILE:
12165 5 : handle_file_note (ebl->elf, nhdr.n_descsz,
12166 : start + desc_offset);
12167 : break;
12168 :
12169 : default:
12170 24 : handle_core_note (ebl, &nhdr, name, desc);
12171 : }
12172 : else
12173 12 : handle_core_note (ebl, &nhdr, name, desc);
12174 : }
12175 : else
12176 32 : ebl_object_note (ebl, name, nhdr.n_type, nhdr.n_descsz, desc);
12177 : }
12178 : }
12179 :
12180 40 : if (offset == data->d_size)
12181 40 : return;
12182 :
12183 : bad_note:
12184 0 : error (0, 0,
12185 0 : gettext ("cannot get content of note: %s"),
12186 : data != NULL ? "garbage data" : elf_errmsg (-1));
12187 : }
12188 :
12189 : static void
12190 42 : handle_notes (Ebl *ebl, GElf_Ehdr *ehdr)
12191 : {
12192 : /* If we have section headers, just look for SHT_NOTE sections.
12193 : In a debuginfo file, the program headers are not reliable. */
12194 42 : if (shnum != 0)
12195 : {
12196 : /* Get the section header string table index. */
12197 : size_t shstrndx;
12198 33 : if (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0)
12199 : error (EXIT_FAILURE, 0,
12200 0 : gettext ("cannot get section header string table index"));
12201 :
12202 : Elf_Scn *scn = NULL;
12203 863 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
12204 : {
12205 : GElf_Shdr shdr_mem;
12206 830 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
12207 :
12208 830 : if (shdr == NULL || shdr->sh_type != SHT_NOTE)
12209 : /* Not what we are looking for. */
12210 799 : continue;
12211 :
12212 62 : printf (gettext ("\
12213 : \nNote section [%2zu] '%s' of %" PRIu64 " bytes at offset %#0" PRIx64 ":\n"),
12214 : elf_ndxscn (scn),
12215 31 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
12216 : shdr->sh_size, shdr->sh_offset);
12217 :
12218 31 : handle_notes_data (ebl, ehdr, shdr->sh_offset,
12219 : elf_getdata (scn, NULL));
12220 : }
12221 42 : return;
12222 : }
12223 :
12224 : /* We have to look through the program header to find the note
12225 : sections. There can be more than one. */
12226 102 : for (size_t cnt = 0; cnt < phnum; ++cnt)
12227 : {
12228 : GElf_Phdr mem;
12229 102 : GElf_Phdr *phdr = gelf_getphdr (ebl->elf, cnt, &mem);
12230 :
12231 102 : if (phdr == NULL || phdr->p_type != PT_NOTE)
12232 : /* Not what we are looking for. */
12233 93 : continue;
12234 :
12235 9 : printf (gettext ("\
12236 : \nNote segment of %" PRIu64 " bytes at offset %#0" PRIx64 ":\n"),
12237 : phdr->p_filesz, phdr->p_offset);
12238 :
12239 18 : handle_notes_data (ebl, ehdr, phdr->p_offset,
12240 : elf_getdata_rawchunk (ebl->elf,
12241 9 : phdr->p_offset, phdr->p_filesz,
12242 : ELF_T_NHDR));
12243 : }
12244 : }
12245 :
12246 :
12247 : static void
12248 5 : hex_dump (const uint8_t *data, size_t len)
12249 : {
12250 5 : size_t pos = 0;
12251 31 : while (pos < len)
12252 : {
12253 : printf (" 0x%08zx ", pos);
12254 :
12255 21 : const size_t chunk = MIN (len - pos, 16);
12256 :
12257 342 : for (size_t i = 0; i < chunk; ++i)
12258 321 : if (i % 4 == 3)
12259 80 : printf ("%02x ", data[pos + i]);
12260 : else
12261 241 : printf ("%02x", data[pos + i]);
12262 :
12263 21 : if (chunk < 16)
12264 1 : printf ("%*s", (int) ((16 - chunk) * 2 + (16 - chunk + 3) / 4), "");
12265 :
12266 321 : for (size_t i = 0; i < chunk; ++i)
12267 : {
12268 321 : unsigned char b = data[pos + i];
12269 321 : printf ("%c", isprint (b) ? b : '.');
12270 : }
12271 :
12272 : putchar ('\n');
12273 21 : pos += chunk;
12274 : }
12275 5 : }
12276 :
12277 : static void
12278 5 : dump_data_section (Elf_Scn *scn, const GElf_Shdr *shdr, const char *name)
12279 : {
12280 5 : if (shdr->sh_size == 0 || shdr->sh_type == SHT_NOBITS)
12281 0 : printf (gettext ("\nSection [%zu] '%s' has no data to dump.\n"),
12282 : elf_ndxscn (scn), name);
12283 : else
12284 : {
12285 5 : if (print_decompress)
12286 : {
12287 : /* We try to decompress the section, but keep the old shdr around
12288 : so we can show both the original shdr size and the uncompressed
12289 : data size. */
12290 4 : if ((shdr->sh_flags & SHF_COMPRESSED) != 0)
12291 : {
12292 2 : if (elf_compress (scn, 0, 0) < 0)
12293 0 : printf ("WARNING: %s [%zd]\n",
12294 : gettext ("Couldn't uncompress section"),
12295 : elf_ndxscn (scn));
12296 : }
12297 2 : else if (strncmp (name, ".zdebug", strlen (".zdebug")) == 0)
12298 : {
12299 2 : if (elf_compress_gnu (scn, 0, 0) < 0)
12300 0 : printf ("WARNING: %s [%zd]\n",
12301 : gettext ("Couldn't uncompress section"),
12302 : elf_ndxscn (scn));
12303 : }
12304 : }
12305 :
12306 5 : Elf_Data *data = elf_rawdata (scn, NULL);
12307 5 : if (data == NULL)
12308 0 : error (0, 0, gettext ("cannot get data for section [%zu] '%s': %s"),
12309 : elf_ndxscn (scn), name, elf_errmsg (-1));
12310 : else
12311 : {
12312 5 : if (data->d_size == shdr->sh_size)
12313 1 : printf (gettext ("\nHex dump of section [%zu] '%s', %" PRIu64
12314 : " bytes at offset %#0" PRIx64 ":\n"),
12315 : elf_ndxscn (scn), name,
12316 : shdr->sh_size, shdr->sh_offset);
12317 : else
12318 4 : printf (gettext ("\nHex dump of section [%zu] '%s', %" PRIu64
12319 : " bytes (%zd uncompressed) at offset %#0"
12320 : PRIx64 ":\n"),
12321 : elf_ndxscn (scn), name,
12322 : shdr->sh_size, data->d_size, shdr->sh_offset);
12323 5 : hex_dump (data->d_buf, data->d_size);
12324 : }
12325 : }
12326 5 : }
12327 :
12328 : static void
12329 73 : print_string_section (Elf_Scn *scn, const GElf_Shdr *shdr, const char *name)
12330 : {
12331 73 : if (shdr->sh_size == 0 || shdr->sh_type == SHT_NOBITS)
12332 0 : printf (gettext ("\nSection [%zu] '%s' has no strings to dump.\n"),
12333 : elf_ndxscn (scn), name);
12334 : else
12335 : {
12336 73 : if (print_decompress)
12337 : {
12338 : /* We try to decompress the section, but keep the old shdr around
12339 : so we can show both the original shdr size and the uncompressed
12340 : data size. */
12341 1 : if ((shdr->sh_flags & SHF_COMPRESSED) != 0)
12342 : {
12343 0 : if (elf_compress (scn, 0, 0) < 0)
12344 0 : printf ("WARNING: %s [%zd]\n",
12345 : gettext ("Couldn't uncompress section"),
12346 : elf_ndxscn (scn));
12347 : }
12348 1 : else if (strncmp (name, ".zdebug", strlen (".zdebug")) == 0)
12349 : {
12350 1 : if (elf_compress_gnu (scn, 0, 0) < 0)
12351 0 : printf ("WARNING: %s [%zd]\n",
12352 : gettext ("Couldn't uncompress section"),
12353 : elf_ndxscn (scn));
12354 : }
12355 : }
12356 :
12357 73 : Elf_Data *data = elf_rawdata (scn, NULL);
12358 73 : if (data == NULL)
12359 0 : error (0, 0, gettext ("cannot get data for section [%zu] '%s': %s"),
12360 : elf_ndxscn (scn), name, elf_errmsg (-1));
12361 : else
12362 : {
12363 73 : if (data->d_size == shdr->sh_size)
12364 72 : printf (gettext ("\nString section [%zu] '%s' contains %" PRIu64
12365 : " bytes at offset %#0" PRIx64 ":\n"),
12366 : elf_ndxscn (scn), name,
12367 : shdr->sh_size, shdr->sh_offset);
12368 : else
12369 1 : printf (gettext ("\nString section [%zu] '%s' contains %" PRIu64
12370 : " bytes (%zd uncompressed) at offset %#0"
12371 : PRIx64 ":\n"),
12372 : elf_ndxscn (scn), name,
12373 : shdr->sh_size, data->d_size, shdr->sh_offset);
12374 :
12375 73 : const char *start = data->d_buf;
12376 73 : const char *const limit = start + data->d_size;
12377 : do
12378 : {
12379 13738 : const char *end = memchr (start, '\0', limit - start);
12380 13738 : const size_t pos = start - (const char *) data->d_buf;
12381 13738 : if (unlikely (end == NULL))
12382 : {
12383 0 : printf (" [%6zx]- %.*s\n",
12384 : pos, (int) (limit - start), start);
12385 : break;
12386 : }
12387 : printf (" [%6zx] %s\n", pos, start);
12388 13738 : start = end + 1;
12389 13738 : } while (start < limit);
12390 : }
12391 : }
12392 73 : }
12393 :
12394 : static void
12395 38 : for_each_section_argument (Elf *elf, const struct section_argument *list,
12396 : void (*dump) (Elf_Scn *scn, const GElf_Shdr *shdr,
12397 : const char *name))
12398 : {
12399 : /* Get the section header string table index. */
12400 : size_t shstrndx;
12401 38 : if (elf_getshdrstrndx (elf, &shstrndx) < 0)
12402 : error (EXIT_FAILURE, 0,
12403 0 : gettext ("cannot get section header string table index"));
12404 :
12405 102 : for (const struct section_argument *a = list; a != NULL; a = a->next)
12406 : {
12407 : Elf_Scn *scn;
12408 : GElf_Shdr shdr_mem;
12409 102 : const char *name = NULL;
12410 :
12411 102 : char *endp = NULL;
12412 102 : unsigned long int shndx = strtoul (a->arg, &endp, 0);
12413 102 : if (endp != a->arg && *endp == '\0')
12414 : {
12415 0 : scn = elf_getscn (elf, shndx);
12416 0 : if (scn == NULL)
12417 : {
12418 0 : error (0, 0, gettext ("\nsection [%lu] does not exist"), shndx);
12419 0 : continue;
12420 : }
12421 :
12422 0 : if (gelf_getshdr (scn, &shdr_mem) == NULL)
12423 0 : error (EXIT_FAILURE, 0, gettext ("cannot get section header: %s"),
12424 : elf_errmsg (-1));
12425 0 : name = elf_strptr (elf, shstrndx, shdr_mem.sh_name);
12426 : }
12427 : else
12428 : {
12429 : /* Need to look up the section by name. */
12430 : scn = NULL;
12431 : bool found = false;
12432 2610 : while ((scn = elf_nextscn (elf, scn)) != NULL)
12433 : {
12434 2508 : if (gelf_getshdr (scn, &shdr_mem) == NULL)
12435 0 : continue;
12436 2508 : name = elf_strptr (elf, shstrndx, shdr_mem.sh_name);
12437 2508 : if (name == NULL)
12438 0 : continue;
12439 2508 : if (!strcmp (name, a->arg))
12440 : {
12441 78 : found = true;
12442 78 : (*dump) (scn, &shdr_mem, name);
12443 : }
12444 : }
12445 :
12446 102 : if (unlikely (!found) && !a->implicit)
12447 0 : error (0, 0, gettext ("\nsection '%s' does not exist"), a->arg);
12448 : }
12449 : }
12450 38 : }
12451 :
12452 : static void
12453 : dump_data (Ebl *ebl)
12454 : {
12455 5 : for_each_section_argument (ebl->elf, dump_data_sections, &dump_data_section);
12456 : }
12457 :
12458 : static void
12459 : dump_strings (Ebl *ebl)
12460 : {
12461 33 : for_each_section_argument (ebl->elf, string_sections, &print_string_section);
12462 : }
12463 :
12464 : static void
12465 0 : print_strings (Ebl *ebl)
12466 : {
12467 : /* Get the section header string table index. */
12468 : size_t shstrndx;
12469 0 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
12470 : error (EXIT_FAILURE, 0,
12471 0 : gettext ("cannot get section header string table index"));
12472 :
12473 : Elf_Scn *scn;
12474 : GElf_Shdr shdr_mem;
12475 : const char *name;
12476 : scn = NULL;
12477 0 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
12478 : {
12479 0 : if (gelf_getshdr (scn, &shdr_mem) == NULL)
12480 0 : continue;
12481 :
12482 0 : if (shdr_mem.sh_type != SHT_PROGBITS
12483 0 : || !(shdr_mem.sh_flags & SHF_STRINGS))
12484 0 : continue;
12485 :
12486 0 : name = elf_strptr (ebl->elf, shstrndx, shdr_mem.sh_name);
12487 0 : if (name == NULL)
12488 0 : continue;
12489 :
12490 0 : print_string_section (scn, &shdr_mem, name);
12491 : }
12492 0 : }
12493 :
12494 : static void
12495 2 : dump_archive_index (Elf *elf, const char *fname)
12496 : {
12497 : size_t narsym;
12498 2 : const Elf_Arsym *arsym = elf_getarsym (elf, &narsym);
12499 2 : if (arsym == NULL)
12500 : {
12501 0 : int result = elf_errno ();
12502 0 : if (unlikely (result != ELF_E_NO_INDEX))
12503 0 : error (EXIT_FAILURE, 0,
12504 0 : gettext ("cannot get symbol index of archive '%s': %s"),
12505 : fname, elf_errmsg (result));
12506 : else
12507 0 : printf (gettext ("\nArchive '%s' has no symbol index\n"), fname);
12508 2 : return;
12509 : }
12510 :
12511 2 : printf (gettext ("\nIndex of archive '%s' has %zu entries:\n"),
12512 : fname, narsym);
12513 :
12514 2 : size_t as_off = 0;
12515 11 : for (const Elf_Arsym *s = arsym; s < &arsym[narsym - 1]; ++s)
12516 : {
12517 9 : if (s->as_off != as_off)
12518 : {
12519 6 : as_off = s->as_off;
12520 :
12521 6 : Elf *subelf = NULL;
12522 6 : if (unlikely (elf_rand (elf, as_off) == 0)
12523 6 : || unlikely ((subelf = elf_begin (-1, ELF_C_READ_MMAP, elf))
12524 : == NULL))
12525 : #if __GLIBC__ < 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ < 7)
12526 : while (1)
12527 : #endif
12528 0 : error (EXIT_FAILURE, 0,
12529 0 : gettext ("cannot extract member at offset %zu in '%s': %s"),
12530 : as_off, fname, elf_errmsg (-1));
12531 :
12532 6 : const Elf_Arhdr *h = elf_getarhdr (subelf);
12533 :
12534 6 : printf (gettext ("Archive member '%s' contains:\n"), h->ar_name);
12535 :
12536 6 : elf_end (subelf);
12537 : }
12538 :
12539 9 : printf ("\t%s\n", s->as_name);
12540 : }
12541 : }
12542 :
12543 : #include "debugpred.h"
|