Branch data 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 <fcntl.h>
28 : : #include <gelf.h>
29 : : #include <inttypes.h>
30 : : #include <langinfo.h>
31 : : #include <libdw.h>
32 : : #include <libdwfl.h>
33 : : #include <libintl.h>
34 : : #include <locale.h>
35 : : #include <stdarg.h>
36 : : #include <stdbool.h>
37 : : #include <stdio.h>
38 : : #include <stdio_ext.h>
39 : : #include <stdlib.h>
40 : : #include <string.h>
41 : : #include <strings.h>
42 : : #include <time.h>
43 : : #include <unistd.h>
44 : : #include <sys/stat.h>
45 : : #include <signal.h>
46 : :
47 : : #include <libeu.h>
48 : : #include <system.h>
49 : : #include <printversion.h>
50 : : #include "../libelf/libelfP.h"
51 : : #include "../libelf/common.h"
52 : : #include "../libebl/libeblP.h"
53 : : #include "../libdwelf/libdwelf.h"
54 : : #include "../libdw/libdwP.h"
55 : : #include "../libdwfl/libdwflP.h"
56 : : #include "../libdw/memory-access.h"
57 : :
58 : : #include "../libdw/known-dwarf.h"
59 : :
60 : : #ifdef __linux__
61 : : #define CORE_SIGILL SIGILL
62 : : #define CORE_SIGBUS SIGBUS
63 : : #define CORE_SIGFPE SIGFPE
64 : : #define CORE_SIGSEGV SIGSEGV
65 : : #define CORE_SI_USER SI_USER
66 : : #else
67 : : /* We want the linux version of those as that is what shows up in the core files. */
68 : : #define CORE_SIGILL 4 /* Illegal instruction (ANSI). */
69 : : #define CORE_SIGBUS 7 /* BUS error (4.2 BSD). */
70 : : #define CORE_SIGFPE 8 /* Floating-point exception (ANSI). */
71 : : #define CORE_SIGSEGV 11 /* Segmentation violation (ANSI). */
72 : : #define CORE_SI_USER 0 /* Sent by kill, sigsend. */
73 : : #endif
74 : :
75 : : /* Name and version of program. */
76 : : ARGP_PROGRAM_VERSION_HOOK_DEF = print_version;
77 : :
78 : : /* Bug report address. */
79 : : ARGP_PROGRAM_BUG_ADDRESS_DEF = PACKAGE_BUGREPORT;
80 : :
81 : : /* argp key value for --elf-section, non-ascii. */
82 : : #define ELF_INPUT_SECTION 256
83 : :
84 : : /* argp key value for --dwarf-skeleton, non-ascii. */
85 : : #define DWARF_SKELETON 257
86 : :
87 : : /* argp key value for --dyn-syms, non-ascii. */
88 : : #define PRINT_DYNSYM_TABLE 258
89 : :
90 : : /* Terrible hack for hooking unrelated skeleton/split compile units,
91 : : see __libdw_link_skel_split in print_debug. */
92 : : static bool do_not_close_dwfl = false;
93 : :
94 : : /* Definitions of arguments for argp functions. */
95 : : static const struct argp_option options[] =
96 : : {
97 : : { NULL, 0, NULL, 0, N_("ELF input selection:"), 0 },
98 : : { "elf-section", ELF_INPUT_SECTION, "SECTION", OPTION_ARG_OPTIONAL,
99 : : N_("Use the named SECTION (default .gnu_debugdata) as (compressed) ELF "
100 : : "input data"), 0 },
101 : : { "dwarf-skeleton", DWARF_SKELETON, "FILE", 0,
102 : : N_("Used with -w to find the skeleton Compile Units in FILE associated "
103 : : "with the Split Compile units in a .dwo input file"), 0 },
104 : : { NULL, 0, NULL, 0, N_("ELF output selection:"), 0 },
105 : : { "all", 'a', NULL, 0,
106 : : N_("All these plus -p .strtab -p .dynstr -p .comment"), 0 },
107 : : { "dynamic", 'd', NULL, 0, N_("Display the dynamic segment"), 0 },
108 : : { "file-header", 'h', NULL, 0, N_("Display the ELF file header"), 0 },
109 : : { "histogram", 'I', NULL, 0,
110 : : N_("Display histogram of bucket list lengths"), 0 },
111 : : { "program-headers", 'l', NULL, 0, N_("Display the program headers"), 0 },
112 : : { "segments", 'l', NULL, OPTION_ALIAS | OPTION_HIDDEN, NULL, 0 },
113 : : { "relocs", 'r', NULL, 0, N_("Display relocations"), 0 },
114 : : { "section-groups", 'g', NULL, 0, N_("Display the section groups"), 0 },
115 : : { "section-headers", 'S', NULL, 0, N_("Display the sections' headers"), 0 },
116 : : { "sections", 'S', NULL, OPTION_ALIAS | OPTION_HIDDEN, NULL, 0 },
117 : : { "symbols", 's', "SECTION", OPTION_ARG_OPTIONAL,
118 : : N_("Display the symbol table sections"), 0 },
119 : : { "dyn-syms", PRINT_DYNSYM_TABLE, NULL, 0,
120 : : N_("Display (only) the dynamic symbol table"), 0 },
121 : : { "version-info", 'V', NULL, 0, N_("Display versioning information"), 0 },
122 : : { "notes", 'n', "SECTION", OPTION_ARG_OPTIONAL, N_("Display the ELF notes"), 0 },
123 : : { "arch-specific", 'A', NULL, 0,
124 : : N_("Display architecture specific information, if any"), 0 },
125 : : { "exception", 'e', NULL, 0,
126 : : N_("Display sections for exception handling"), 0 },
127 : :
128 : : { NULL, 0, NULL, 0, N_("Additional output selection:"), 0 },
129 : : { "debug-dump", 'w', "SECTION", OPTION_ARG_OPTIONAL,
130 : : N_("Display DWARF section content. SECTION can be one of abbrev, addr, "
131 : : "aranges, decodedaranges, frame, gdb_index, info, info+, loc, line, "
132 : : "decodedline, ranges, pubnames, str, macinfo, macro or exception"), 0 },
133 : : { "hex-dump", 'x', "SECTION", 0,
134 : : N_("Dump the uninterpreted contents of SECTION, by number or name"), 0 },
135 : : { "strings", 'p', "SECTION", OPTION_ARG_OPTIONAL,
136 : : N_("Print string contents of sections"), 0 },
137 : : { "string-dump", 'p', NULL, OPTION_ALIAS | OPTION_HIDDEN, NULL, 0 },
138 : : { "archive-index", 'c', NULL, 0,
139 : : N_("Display the symbol index of an archive"), 0 },
140 : :
141 : : { NULL, 0, NULL, 0, N_("Output control:"), 0 },
142 : : { "numeric-addresses", 'N', NULL, 0,
143 : : N_("Do not find symbol names for addresses in DWARF data"), 0 },
144 : : { "unresolved-address-offsets", 'U', NULL, 0,
145 : : N_("Display just offsets instead of resolving values to addresses in DWARF data"), 0 },
146 : : { "wide", 'W', NULL, 0,
147 : : N_("Ignored for compatibility (lines always wide)"), 0 },
148 : : { "decompress", 'z', NULL, 0,
149 : : N_("Show compression information for compressed sections (when used with -S); decompress section before dumping data (when used with -p or -x)"), 0 },
150 : : { NULL, 0, NULL, 0, NULL, 0 }
151 : : };
152 : :
153 : : /* Short description of program. */
154 : : static const char doc[] = N_("\
155 : : Print information from ELF file in human-readable form.");
156 : :
157 : : /* Strings for arguments in help texts. */
158 : : static const char args_doc[] = N_("FILE...");
159 : :
160 : : /* Prototype for option handler. */
161 : : static error_t parse_opt (int key, char *arg, struct argp_state *state);
162 : :
163 : : /* Data structure to communicate with argp functions. */
164 : : static struct argp argp =
165 : : {
166 : : options, parse_opt, args_doc, doc, NULL, NULL, NULL
167 : : };
168 : :
169 : : /* If non-null, the section from which we should read to (compressed) ELF. */
170 : : static const char *elf_input_section = NULL;
171 : :
172 : : /* If non-null, the file that contains the skeleton CUs. */
173 : : static const char *dwarf_skeleton = NULL;
174 : :
175 : : /* Flags set by the option controlling the output. */
176 : :
177 : : /* True if dynamic segment should be printed. */
178 : : static bool print_dynamic_table;
179 : :
180 : : /* True if the file header should be printed. */
181 : : static bool print_file_header;
182 : :
183 : : /* True if the program headers should be printed. */
184 : : static bool print_program_header;
185 : :
186 : : /* True if relocations should be printed. */
187 : : static bool print_relocations;
188 : :
189 : : /* True if the section headers should be printed. */
190 : : static bool print_section_header;
191 : :
192 : : /* True if the symbol table should be printed. */
193 : : static bool print_symbol_table;
194 : :
195 : : /* True if (only) the dynsym table should be printed. */
196 : : static bool print_dynsym_table;
197 : :
198 : : /* A specific section name, or NULL to print all symbol tables. */
199 : : static char *symbol_table_section;
200 : :
201 : : /* A specific section name, or NULL to print all ELF notes. */
202 : : static char *notes_section;
203 : :
204 : : /* True if the version information should be printed. */
205 : : static bool print_version_info;
206 : :
207 : : /* True if section groups should be printed. */
208 : : static bool print_section_groups;
209 : :
210 : : /* True if bucket list length histogram should be printed. */
211 : : static bool print_histogram;
212 : :
213 : : /* True if the architecture specific data should be printed. */
214 : : static bool print_arch;
215 : :
216 : : /* True if note section content should be printed. */
217 : : static bool print_notes;
218 : :
219 : : /* True if SHF_STRINGS section content should be printed. */
220 : : static bool print_string_sections;
221 : :
222 : : /* True if archive index should be printed. */
223 : : static bool print_archive_index;
224 : :
225 : : /* True if any of the control options except print_archive_index is set. */
226 : : static bool any_control_option;
227 : :
228 : : /* True if we should print addresses from DWARF in symbolic form. */
229 : : static bool print_address_names = true;
230 : :
231 : : /* True if we should print raw values instead of relativized addresses. */
232 : : static bool print_unresolved_addresses = false;
233 : :
234 : : /* True if we should print the .debug_aranges section using libdw. */
235 : : static bool decodedaranges = false;
236 : :
237 : : /* True if we should print the .debug_aranges section using libdw. */
238 : : static bool decodedline = false;
239 : :
240 : : /* True if we want to show more information about compressed sections. */
241 : : static bool print_decompress = false;
242 : :
243 : : /* True if we want to show split compile units for debug_info skeletons. */
244 : : static bool show_split_units = false;
245 : :
246 : : /* Select printing of debugging sections. */
247 : : static enum section_e
248 : : {
249 : : section_abbrev = 1, /* .debug_abbrev */
250 : : section_aranges = 2, /* .debug_aranges */
251 : : section_frame = 4, /* .debug_frame or .eh_frame & al. */
252 : : section_info = 8, /* .debug_info, (implies .debug_types) */
253 : : section_line = 16, /* .debug_line */
254 : : section_loc = 32, /* .debug_loc */
255 : : section_pubnames = 64, /* .debug_pubnames */
256 : : section_str = 128, /* .debug_str */
257 : : section_macinfo = 256, /* .debug_macinfo */
258 : : section_ranges = 512, /* .debug_ranges */
259 : : section_exception = 1024, /* .eh_frame & al. */
260 : : section_gdb_index = 2048, /* .gdb_index */
261 : : section_macro = 4096, /* .debug_macro */
262 : : section_addr = 8192, /* .debug_addr */
263 : : section_types = 16384, /* .debug_types (implied by .debug_info) */
264 : : section_all = (section_abbrev | section_aranges | section_frame
265 : : | section_info | section_line | section_loc
266 : : | section_pubnames | section_str | section_macinfo
267 : : | section_ranges | section_exception | section_gdb_index
268 : : | section_macro | section_addr | section_types)
269 : : } print_debug_sections, implicit_debug_sections;
270 : :
271 : : /* Select hex dumping of sections. */
272 : : static struct section_argument *dump_data_sections;
273 : : static struct section_argument **dump_data_sections_tail = &dump_data_sections;
274 : :
275 : : /* Select string dumping of sections. */
276 : : static struct section_argument *string_sections;
277 : : static struct section_argument **string_sections_tail = &string_sections;
278 : :
279 : : struct section_argument
280 : : {
281 : : struct section_argument *next;
282 : : const char *arg;
283 : : bool implicit;
284 : : };
285 : :
286 : : /* Numbers of sections and program headers in the file. */
287 : : static size_t shnum;
288 : : static size_t phnum;
289 : :
290 : :
291 : : /* Declarations of local functions. */
292 : : static void process_file (int fd, const char *fname, bool only_one);
293 : : static void process_elf_file (Dwfl_Module *dwflmod, int fd);
294 : : static void print_ehdr (Ebl *ebl, GElf_Ehdr *ehdr);
295 : : static void print_shdr (Ebl *ebl, GElf_Ehdr *ehdr);
296 : : static void print_phdr (Ebl *ebl, GElf_Ehdr *ehdr);
297 : : static void print_scngrp (Ebl *ebl);
298 : : static void print_dynamic (Ebl *ebl);
299 : : static void print_relocs (Ebl *ebl, GElf_Ehdr *ehdr);
300 : : static void handle_relocs_rel (Ebl *ebl, GElf_Ehdr *ehdr, Elf_Scn *scn,
301 : : GElf_Shdr *shdr);
302 : : static void handle_relocs_rela (Ebl *ebl, GElf_Ehdr *ehdr, Elf_Scn *scn,
303 : : GElf_Shdr *shdr);
304 : : static void print_symtab (Ebl *ebl, int type);
305 : : static void handle_symtab (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr);
306 : : static void print_verinfo (Ebl *ebl);
307 : : static void handle_verneed (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr);
308 : : static void handle_verdef (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr);
309 : : static void handle_versym (Ebl *ebl, Elf_Scn *scn,
310 : : GElf_Shdr *shdr);
311 : : static void print_debug (Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr);
312 : : static void handle_hash (Ebl *ebl);
313 : : static void handle_notes (Ebl *ebl, GElf_Ehdr *ehdr);
314 : : static void print_liblist (Ebl *ebl);
315 : : static void print_attributes (Ebl *ebl, const GElf_Ehdr *ehdr);
316 : : static void dump_data (Ebl *ebl);
317 : : static void dump_strings (Ebl *ebl);
318 : : static void print_strings (Ebl *ebl);
319 : : static void dump_archive_index (Elf *, const char *);
320 : :
321 : :
322 : : /* Looked up once with gettext in main. */
323 : : static char *yes_str;
324 : : static char *no_str;
325 : :
326 : : static void
327 : 678 : cleanup_list (struct section_argument *list)
328 : : {
329 [ + + + + ]: 928 : while (list != NULL)
330 : : {
331 : 250 : struct section_argument *a = list;
332 : 250 : list = a->next;
333 : 250 : free (a);
334 : : }
335 : : }
336 : :
337 : : int
338 : 339 : main (int argc, char *argv[])
339 : : {
340 : : /* We use no threads here which can interfere with handling a stream. */
341 : 339 : (void) __fsetlocking (stdout, FSETLOCKING_BYCALLER);
342 : :
343 : : /* Set locale. */
344 : 339 : setlocale (LC_ALL, "");
345 : :
346 : : /* Initialize the message catalog. */
347 : 339 : textdomain (PACKAGE_TARNAME);
348 : :
349 : : /* Look up once. */
350 : 339 : yes_str = _("yes");
351 : 339 : no_str = _("no");
352 : :
353 : : /* Parse and process arguments. */
354 : 339 : int remaining;
355 : 339 : argp_parse (&argp, argc, argv, 0, &remaining, NULL);
356 : :
357 : : /* Before we start tell the ELF library which version we are using. */
358 : 339 : elf_version (EV_CURRENT);
359 : :
360 : : /* Now process all the files given at the command line. */
361 : 339 : bool only_one = remaining + 1 == argc;
362 : 362 : do
363 : : {
364 : : /* Open the file. */
365 : 362 : int fd = open (argv[remaining], O_RDONLY);
366 [ - + ]: 362 : if (fd == -1)
367 : : {
368 : 0 : error (0, errno, _("cannot open input file '%s'"), argv[remaining]);
369 : 0 : continue;
370 : : }
371 : :
372 : 362 : process_file (fd, argv[remaining], only_one);
373 : :
374 : 362 : close (fd);
375 : : }
376 [ + + ]: 362 : while (++remaining < argc);
377 : :
378 : 339 : cleanup_list (dump_data_sections);
379 : 339 : cleanup_list (string_sections);
380 : :
381 : 339 : return error_message_count != 0;
382 : : }
383 : :
384 : : static void
385 : 250 : add_dump_section (const char *name,
386 : : int key,
387 : : bool implicit)
388 : : {
389 : 250 : struct section_argument *a = xmalloc (sizeof *a);
390 : 250 : a->arg = name;
391 : 250 : a->next = NULL;
392 : 250 : a->implicit = implicit;
393 : 500 : struct section_argument ***tailp
394 [ + + ]: 250 : = key == 'x' ? &dump_data_sections_tail : &string_sections_tail;
395 : 250 : **tailp = a;
396 : 250 : *tailp = &a->next;
397 : 250 : }
398 : :
399 : : /* Handle program arguments. */
400 : : static error_t
401 : 2100 : parse_opt (int key, char *arg,
402 : : struct argp_state *state __attribute__ ((unused)))
403 : : {
404 [ + + + - : 2100 : switch (key)
+ + - + +
+ + + + -
+ + + + +
+ - + + +
+ - + ]
405 : : {
406 : 81 : case 'a':
407 : 81 : print_file_header = true;
408 : 81 : print_program_header = true;
409 : 81 : print_relocations = true;
410 : 81 : print_section_header = true;
411 : 81 : print_symbol_table = true;
412 : 81 : print_version_info = true;
413 : 81 : print_dynamic_table = true;
414 : 81 : print_section_groups = true;
415 : 81 : print_histogram = true;
416 : 81 : print_arch = true;
417 : 81 : print_notes = true;
418 : 81 : implicit_debug_sections |= section_exception;
419 : 81 : add_dump_section (".strtab", key, true);
420 : 81 : add_dump_section (".dynstr", key, true);
421 : 81 : add_dump_section (".comment", key, true);
422 : 81 : any_control_option = true;
423 : 81 : break;
424 : 4 : case 'A':
425 : 4 : print_arch = true;
426 : 4 : any_control_option = true;
427 : 4 : break;
428 : 2 : case 'd':
429 : 2 : print_dynamic_table = true;
430 : 2 : any_control_option = true;
431 : 2 : break;
432 : 0 : case 'e':
433 : 0 : print_debug_sections |= section_exception;
434 : 0 : any_control_option = true;
435 : 0 : break;
436 : 9 : case 'g':
437 : 9 : print_section_groups = true;
438 : 9 : any_control_option = true;
439 : 9 : break;
440 : 1 : case 'h':
441 : 1 : print_file_header = true;
442 : 1 : any_control_option = true;
443 : 1 : break;
444 : 0 : case 'I':
445 : 0 : print_histogram = true;
446 : 0 : any_control_option = true;
447 : 0 : break;
448 : 2 : case 'l':
449 : 2 : print_program_header = true;
450 : 2 : any_control_option = true;
451 : 2 : break;
452 : 23 : case 'n':
453 : 23 : print_notes = true;
454 : 23 : any_control_option = true;
455 : 23 : notes_section = arg;
456 : 23 : break;
457 : 1 : case 'r':
458 : 1 : print_relocations = true;
459 : 1 : any_control_option = true;
460 : 1 : break;
461 : 89 : case 'S':
462 : 89 : print_section_header = true;
463 : 89 : any_control_option = true;
464 : 89 : break;
465 : 14 : case 's':
466 : 14 : print_symbol_table = true;
467 : 14 : any_control_option = true;
468 : 14 : symbol_table_section = arg;
469 : 14 : break;
470 : 1 : case PRINT_DYNSYM_TABLE:
471 : 1 : print_dynsym_table = true;
472 : 1 : any_control_option = true;
473 : 1 : break;
474 : 0 : case 'V':
475 : 0 : print_version_info = true;
476 : 0 : any_control_option = true;
477 : 0 : break;
478 : 2 : case 'c':
479 : 2 : print_archive_index = true;
480 : 2 : break;
481 : 118 : case 'w':
482 [ + + ]: 118 : if (arg == NULL)
483 : : {
484 : 39 : print_debug_sections = section_all;
485 : 39 : implicit_debug_sections = section_info;
486 : 39 : show_split_units = true;
487 : : }
488 [ - + ]: 79 : else if (strcmp (arg, "abbrev") == 0)
489 : 0 : print_debug_sections |= section_abbrev;
490 [ + + ]: 79 : else if (strcmp (arg, "addr") == 0)
491 : : {
492 : 2 : print_debug_sections |= section_addr;
493 : 2 : implicit_debug_sections |= section_info;
494 : : }
495 [ + + ]: 77 : else if (strcmp (arg, "aranges") == 0)
496 : 3 : print_debug_sections |= section_aranges;
497 [ + + ]: 74 : else if (strcmp (arg, "decodedaranges") == 0)
498 : : {
499 : 1 : print_debug_sections |= section_aranges;
500 : 1 : decodedaranges = true;
501 : : }
502 [ + + ]: 73 : else if (strcmp (arg, "ranges") == 0)
503 : : {
504 : 12 : print_debug_sections |= section_ranges;
505 : 12 : implicit_debug_sections |= section_info;
506 : : }
507 [ + + + + ]: 61 : else if (strcmp (arg, "frame") == 0 || strcmp (arg, "frames") == 0)
508 : 3 : print_debug_sections |= section_frame;
509 [ + + ]: 58 : else if (strcmp (arg, "info") == 0)
510 : : {
511 : 19 : print_debug_sections |= section_info;
512 : 19 : print_debug_sections |= section_types;
513 : : }
514 [ + + ]: 39 : else if (strcmp (arg, "info+") == 0)
515 : : {
516 : 2 : print_debug_sections |= section_info;
517 : 2 : print_debug_sections |= section_types;
518 : 2 : show_split_units = true;
519 : : }
520 [ + + ]: 37 : else if (strcmp (arg, "loc") == 0)
521 : : {
522 : 18 : print_debug_sections |= section_loc;
523 : 18 : implicit_debug_sections |= section_info;
524 : : }
525 [ + + ]: 19 : else if (strcmp (arg, "line") == 0)
526 : 6 : print_debug_sections |= section_line;
527 [ + + ]: 13 : else if (strcmp (arg, "decodedline") == 0)
528 : : {
529 : 5 : print_debug_sections |= section_line;
530 : 5 : decodedline = true;
531 : : }
532 [ - + ]: 8 : else if (strcmp (arg, "pubnames") == 0)
533 : 0 : print_debug_sections |= section_pubnames;
534 [ + + ]: 8 : else if (strcmp (arg, "str") == 0)
535 : : {
536 : 3 : print_debug_sections |= section_str;
537 : : /* For mapping string offset tables to CUs. */
538 : 3 : implicit_debug_sections |= section_info;
539 : : }
540 [ - + ]: 5 : else if (strcmp (arg, "macinfo") == 0)
541 : 0 : print_debug_sections |= section_macinfo;
542 [ + + ]: 5 : else if (strcmp (arg, "macro") == 0)
543 : 3 : print_debug_sections |= section_macro;
544 [ - + ]: 2 : else if (strcmp (arg, "exception") == 0)
545 : 0 : print_debug_sections |= section_exception;
546 [ + - ]: 2 : else if (strcmp (arg, "gdb_index") == 0)
547 : 2 : print_debug_sections |= section_gdb_index;
548 : : else
549 : : {
550 : 0 : fprintf (stderr, _("Unknown DWARF debug section `%s'.\n"),
551 : : arg);
552 : 0 : argp_help (&argp, stderr, ARGP_HELP_SEE,
553 : : program_invocation_short_name);
554 : 0 : exit (1);
555 : : }
556 : 118 : any_control_option = true;
557 : 118 : break;
558 : 1 : case 'p':
559 : 1 : any_control_option = true;
560 [ - + ]: 1 : if (arg == NULL)
561 : : {
562 : 0 : print_string_sections = true;
563 : 0 : break;
564 : : }
565 : 7 : FALLTHROUGH;
566 : : case 'x':
567 : 7 : add_dump_section (arg, key, false);
568 : 7 : any_control_option = true;
569 : 7 : break;
570 : 2 : case 'N':
571 : 2 : print_address_names = false;
572 : 2 : break;
573 : 27 : case 'U':
574 : 27 : print_unresolved_addresses = true;
575 : 27 : break;
576 : 0 : case ARGP_KEY_NO_ARGS:
577 : 0 : fputs (_("Missing file name.\n"), stderr);
578 : 0 : goto do_argp_help;
579 : 339 : case ARGP_KEY_FINI:
580 [ + + - + ]: 339 : if (! any_control_option && ! print_archive_index)
581 : : {
582 : 0 : fputs (_("No operation specified.\n"), stderr);
583 : 0 : do_argp_help:
584 : 0 : argp_help (&argp, stderr, ARGP_HELP_SEE,
585 : : program_invocation_short_name);
586 : 0 : exit (EXIT_FAILURE);
587 : : }
588 : : break;
589 : : case 'W': /* Ignored. */
590 : : break;
591 : 13 : case 'z':
592 : 13 : print_decompress = true;
593 : 13 : break;
594 : 4 : case ELF_INPUT_SECTION:
595 [ + - ]: 4 : if (arg == NULL)
596 : 4 : elf_input_section = ".gnu_debugdata";
597 : : else
598 : 0 : elf_input_section = arg;
599 : : break;
600 : 5 : case DWARF_SKELETON:
601 : 5 : dwarf_skeleton = arg;
602 : 5 : break;
603 : : default:
604 : : return ARGP_ERR_UNKNOWN;
605 : : }
606 : : return 0;
607 : : }
608 : :
609 : :
610 : : /* Create a file descriptor to read the data from the
611 : : elf_input_section given a file descriptor to an ELF file. */
612 : : static int
613 : 4 : open_input_section (int fd)
614 : : {
615 : 4 : size_t shnums;
616 : 4 : size_t cnt;
617 : 4 : size_t shstrndx;
618 : 4 : Elf *elf = elf_begin (fd, ELF_C_READ_MMAP, NULL);
619 [ - + ]: 4 : if (elf == NULL)
620 : : {
621 : 0 : error (0, 0, _("cannot generate Elf descriptor: %s"),
622 : : elf_errmsg (-1));
623 : 0 : return -1;
624 : : }
625 : :
626 [ - + ]: 4 : if (elf_getshdrnum (elf, &shnums) < 0)
627 : : {
628 : 0 : error (0, 0, _("cannot determine number of sections: %s"),
629 : : elf_errmsg (-1));
630 : 0 : open_error:
631 : 0 : elf_end (elf);
632 : 0 : return -1;
633 : : }
634 : :
635 [ - + ]: 4 : if (elf_getshdrstrndx (elf, &shstrndx) < 0)
636 : : {
637 : 0 : error (0, 0, _("cannot get section header string table index"));
638 : 0 : goto open_error;
639 : : }
640 : :
641 [ + - ]: 91 : for (cnt = 0; cnt < shnums; ++cnt)
642 : : {
643 : 91 : Elf_Scn *scn = elf_getscn (elf, cnt);
644 [ - + ]: 91 : if (scn == NULL)
645 : : {
646 : 0 : error (0, 0, _("cannot get section: %s"),
647 : : elf_errmsg (-1));
648 : 0 : goto open_error;
649 : : }
650 : :
651 : 91 : GElf_Shdr shdr_mem;
652 : 91 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
653 [ - + ]: 91 : if (unlikely (shdr == NULL))
654 : : {
655 : 0 : error (0, 0, _("cannot get section header: %s"),
656 : : elf_errmsg (-1));
657 : 0 : goto open_error;
658 : : }
659 : :
660 : 91 : const char *sname = elf_strptr (elf, shstrndx, shdr->sh_name);
661 [ - + ]: 91 : if (sname == NULL)
662 : : {
663 : 0 : error (0, 0, _("cannot get section name"));
664 : 0 : goto open_error;
665 : : }
666 : :
667 [ + + ]: 91 : if (strcmp (sname, elf_input_section) == 0)
668 : : {
669 : 4 : Elf_Data *data = elf_rawdata (scn, NULL);
670 [ - + ]: 4 : if (data == NULL)
671 : : {
672 : 0 : error (0, 0, _("cannot get %s content: %s"),
673 : : sname, elf_errmsg (-1));
674 : 0 : goto open_error;
675 : : }
676 : :
677 : : /* Create (and immediately unlink) a temporary file to store
678 : : section data in to create a file descriptor for it. */
679 [ + - ]: 4 : const char *tmpdir = getenv ("TMPDIR") ?: P_tmpdir;
680 : 4 : static const char suffix[] = "/readelfXXXXXX";
681 : 4 : int tmplen = strlen (tmpdir) + sizeof (suffix);
682 : 4 : char *tempname = alloca (tmplen);
683 : 4 : sprintf (tempname, "%s%s", tmpdir, suffix);
684 : :
685 : 4 : int sfd = mkstemp (tempname);
686 [ - + ]: 4 : if (sfd == -1)
687 : : {
688 : 0 : error (0, 0, _("cannot create temp file '%s'"),
689 : : tempname);
690 : 0 : goto open_error;
691 : : }
692 : 4 : unlink (tempname);
693 : :
694 : 4 : ssize_t size = data->d_size;
695 [ - + ]: 4 : if (write_retry (sfd, data->d_buf, size) != size)
696 : : {
697 : 0 : error (0, 0, _("cannot write section data"));
698 : 0 : goto open_error;
699 : : }
700 : :
701 [ - + ]: 4 : if (elf_end (elf) != 0)
702 : : {
703 : 0 : error (0, 0, _("error while closing Elf descriptor: %s"),
704 : : elf_errmsg (-1));
705 : 4 : return -1;
706 : : }
707 : :
708 [ - + ]: 4 : if (lseek (sfd, 0, SEEK_SET) == -1)
709 : : {
710 : 0 : error (0, 0, _("error while rewinding file descriptor"));
711 : 0 : return -1;
712 : : }
713 : :
714 : : return sfd;
715 : : }
716 : : }
717 : :
718 : : /* Named section not found. */
719 [ # # ]: 0 : if (elf_end (elf) != 0)
720 : 0 : error (0, 0, _("error while closing Elf descriptor: %s"),
721 : : elf_errmsg (-1));
722 : : return -1;
723 : : }
724 : :
725 : : /* Check if the file is an archive, and if so dump its index. */
726 : : static void
727 : 2 : check_archive_index (int fd, const char *fname, bool only_one)
728 : : {
729 : : /* Create an `Elf' descriptor. */
730 : 2 : Elf *elf = elf_begin (fd, ELF_C_READ_MMAP, NULL);
731 [ - + ]: 2 : if (elf == NULL)
732 : 0 : error (0, 0, _("cannot generate Elf descriptor: %s"),
733 : : elf_errmsg (-1));
734 : : else
735 : : {
736 [ + - ]: 2 : if (elf_kind (elf) == ELF_K_AR)
737 : : {
738 [ - + ]: 2 : if (!only_one)
739 : 0 : printf ("\n%s:\n\n", fname);
740 : 2 : dump_archive_index (elf, fname);
741 : : }
742 : : else
743 : 2 : error (0, 0,
744 : 0 : _("'%s' is not an archive, cannot print archive index"),
745 : : fname);
746 : :
747 : : /* Now we can close the descriptor. */
748 [ - + ]: 2 : if (elf_end (elf) != 0)
749 : 0 : error (0, 0, _("error while closing Elf descriptor: %s"),
750 : : elf_errmsg (-1));
751 : : }
752 : 2 : }
753 : :
754 : : /* Trivial callback used for checking if we opened an archive. */
755 : : static int
756 : 329 : count_dwflmod (Dwfl_Module *dwflmod __attribute__ ((unused)),
757 : : void **userdata __attribute__ ((unused)),
758 : : const char *name __attribute__ ((unused)),
759 : : Dwarf_Addr base __attribute__ ((unused)),
760 : : void *arg)
761 : : {
762 [ + - ]: 329 : if (*(bool *) arg)
763 : : return DWARF_CB_ABORT;
764 : 329 : *(bool *) arg = true;
765 : 329 : return DWARF_CB_OK;
766 : : }
767 : :
768 : : struct process_dwflmod_args
769 : : {
770 : : int fd;
771 : : bool only_one;
772 : : };
773 : :
774 : : static int
775 : 360 : process_dwflmod (Dwfl_Module *dwflmod,
776 : : void **userdata __attribute__ ((unused)),
777 : : const char *name __attribute__ ((unused)),
778 : : Dwarf_Addr base __attribute__ ((unused)),
779 : : void *arg)
780 : : {
781 : 360 : const struct process_dwflmod_args *a = arg;
782 : :
783 : : /* Print the file name. */
784 [ + + ]: 360 : if (!a->only_one)
785 : : {
786 : 31 : const char *fname;
787 : 31 : dwfl_module_info (dwflmod, NULL, NULL, NULL, NULL, NULL, &fname, NULL);
788 : :
789 : 31 : printf ("\n%s:\n\n", fname);
790 : : }
791 : :
792 : 360 : process_elf_file (dwflmod, a->fd);
793 : :
794 : 360 : return DWARF_CB_OK;
795 : : }
796 : :
797 : : /* Stub libdwfl callback, only the ELF handle already open is ever used.
798 : : Only used for finding the alternate debug file if the Dwarf comes from
799 : : the main file. We are not interested in separate debuginfo. */
800 : : static int
801 : 96 : find_no_debuginfo (Dwfl_Module *mod,
802 : : void **userdata,
803 : : const char *modname,
804 : : Dwarf_Addr base,
805 : : const char *file_name,
806 : : const char *debuglink_file,
807 : : GElf_Word debuglink_crc,
808 : : char **debuginfo_file_name)
809 : : {
810 : 96 : Dwarf_Addr dwbias;
811 : 96 : dwfl_module_info (mod, NULL, NULL, NULL, &dwbias, NULL, NULL, NULL);
812 : :
813 : : /* We are only interested if the Dwarf has been setup on the main
814 : : elf file but is only missing the alternate debug link. If dwbias
815 : : hasn't even been setup, this is searching for separate debuginfo
816 : : for the main elf. We don't care in that case. */
817 [ + + ]: 96 : if (dwbias == (Dwarf_Addr) -1)
818 : : return -1;
819 : :
820 : 13 : return dwfl_standard_find_debuginfo (mod, userdata, modname, base,
821 : : file_name, debuglink_file,
822 : : debuglink_crc, debuginfo_file_name);
823 : : }
824 : :
825 : : static Dwfl *
826 : 369 : create_dwfl (int fd, const char *fname)
827 : : {
828 : : /* Duplicate an fd for dwfl_report_offline to swallow. */
829 : 369 : int dwfl_fd = dup (fd);
830 [ - + ]: 369 : if (unlikely (dwfl_fd < 0))
831 : 0 : error (EXIT_FAILURE, errno, "dup");
832 : :
833 : : /* Use libdwfl in a trivial way to open the libdw handle for us.
834 : : This takes care of applying relocations to DWARF data in ET_REL files. */
835 : 369 : static const Dwfl_Callbacks callbacks =
836 : : {
837 : : .section_address = dwfl_offline_section_address,
838 : : .find_debuginfo = find_no_debuginfo
839 : : };
840 : 369 : Dwfl *dwfl = dwfl_begin (&callbacks);
841 [ + - ]: 369 : if (likely (dwfl != NULL))
842 : : /* Let 0 be the logical address of the file (or first in archive). */
843 : 369 : dwfl->offline_next_address = 0;
844 [ - + ]: 369 : if (dwfl_report_offline (dwfl, fname, fname, dwfl_fd) == NULL)
845 : : {
846 : 0 : struct stat st;
847 [ # # ]: 0 : if (fstat (dwfl_fd, &st) != 0)
848 : 0 : error (0, errno, _("cannot stat input file"));
849 [ # # ]: 0 : else if (unlikely (st.st_size == 0))
850 : 0 : error (0, 0, _("input file is empty"));
851 : : else
852 : 0 : error (0, 0, _("failed reading '%s': %s"),
853 : : fname, dwfl_errmsg (-1));
854 : 0 : close (dwfl_fd); /* Consumed on success, not on failure. */
855 : 0 : dwfl = NULL;
856 : : }
857 : : else
858 : 369 : dwfl_report_end (dwfl, NULL, NULL);
859 : :
860 : 369 : return dwfl;
861 : : }
862 : :
863 : : /* Process one input file. */
864 : : static void
865 : 362 : process_file (int fd, const char *fname, bool only_one)
866 : : {
867 [ + + ]: 362 : if (print_archive_index)
868 : 2 : check_archive_index (fd, fname, only_one);
869 : :
870 [ + + ]: 362 : if (!any_control_option)
871 : : return;
872 : :
873 [ + + ]: 360 : if (elf_input_section != NULL)
874 : : {
875 : : /* Replace fname and fd with section content. */
876 : 4 : char *fnname = alloca (strlen (fname) + strlen (elf_input_section) + 2);
877 : 4 : sprintf (fnname, "%s:%s", fname, elf_input_section);
878 : 4 : fd = open_input_section (fd);
879 [ - + ]: 4 : if (fd == -1)
880 : : {
881 : 0 : error (0, 0, _("No such section '%s' in '%s'"),
882 : : elf_input_section, fname);
883 : 0 : return;
884 : : }
885 : : fname = fnname;
886 : : }
887 : :
888 : 360 : Dwfl *dwfl = create_dwfl (fd, fname);
889 [ + - ]: 360 : if (dwfl != NULL)
890 : : {
891 [ + + ]: 360 : if (only_one)
892 : : {
893 : : /* Clear ONLY_ONE if we have multiple modules, from an archive. */
894 : 329 : bool seen = false;
895 : 329 : only_one = dwfl_getmodules (dwfl, &count_dwflmod, &seen, 0) == 0;
896 : : }
897 : :
898 : : /* Process the one or more modules gleaned from this file. */
899 : 360 : struct process_dwflmod_args a = { .fd = fd, .only_one = only_one };
900 : 360 : dwfl_getmodules (dwfl, &process_dwflmod, &a, 0);
901 : : }
902 : : /* Terrible hack for hooking unrelated skeleton/split compile units,
903 : : see __libdw_link_skel_split in print_debug. */
904 [ + - ]: 360 : if (! do_not_close_dwfl)
905 : 360 : dwfl_end (dwfl);
906 : :
907 : : /* Need to close the replaced fd if we created it. Caller takes
908 : : care of original. */
909 [ + + ]: 360 : if (elf_input_section != NULL)
910 : 4 : close (fd);
911 : : }
912 : :
913 : : /* Check whether there are any compressed sections in the ELF file. */
914 : : static bool
915 : 135 : elf_contains_chdrs (Elf *elf)
916 : : {
917 : 135 : Elf_Scn *scn = NULL;
918 [ + + ]: 789951 : while ((scn = elf_nextscn (elf, scn)) != NULL)
919 : : {
920 : 789823 : GElf_Shdr shdr_mem;
921 : 789823 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
922 [ + - + + ]: 789823 : if (shdr != NULL && (shdr->sh_flags & SHF_COMPRESSED) != 0)
923 : 7 : return true;
924 : : }
925 : : return false;
926 : : }
927 : :
928 : : /* Process one ELF file. */
929 : : static void
930 : 360 : process_elf_file (Dwfl_Module *dwflmod, int fd)
931 : : {
932 : 360 : GElf_Addr dwflbias;
933 : 360 : Elf *elf = dwfl_module_getelf (dwflmod, &dwflbias);
934 : :
935 : 360 : GElf_Ehdr ehdr_mem;
936 : 360 : GElf_Ehdr *ehdr = gelf_getehdr (elf, &ehdr_mem);
937 : :
938 [ - + ]: 360 : if (ehdr == NULL)
939 : : {
940 : 0 : error (0, 0, _("cannot read ELF header: %s"), elf_errmsg (-1));
941 : 0 : return;
942 : : }
943 : :
944 : 360 : Ebl *ebl = ebl_openbackend (elf);
945 [ - + ]: 360 : if (unlikely (ebl == NULL))
946 : : {
947 : 0 : ebl_error:
948 : 0 : error (0, errno, _("cannot create EBL handle"));
949 : 0 : return;
950 : : }
951 : :
952 : : /* Determine the number of sections. */
953 [ - + ]: 360 : if (unlikely (elf_getshdrnum (ebl->elf, &shnum) < 0))
954 : 0 : error (EXIT_FAILURE, 0,
955 : 0 : _("cannot determine number of sections: %s"),
956 : : elf_errmsg (-1));
957 : :
958 : : /* Determine the number of phdrs. */
959 [ - + ]: 360 : if (unlikely (elf_getphdrnum (ebl->elf, &phnum) < 0))
960 : 0 : error (EXIT_FAILURE, 0,
961 : 0 : _("cannot determine number of program headers: %s"),
962 : : elf_errmsg (-1));
963 : :
964 : : /* For an ET_REL file, libdwfl has adjusted the in-core shdrs and
965 : : may have applied relocation to some sections. If there are any
966 : : compressed sections, any pass (or libdw/libdwfl) might have
967 : : uncompressed them. So we need to get a fresh Elf handle on the
968 : : file to display those. */
969 : 720 : bool print_unchanged = ((print_section_header
970 [ + + ]: 190 : || print_relocations
971 [ + + ]: 189 : || dump_data_sections != NULL
972 [ + + ]: 183 : || print_notes)
973 [ + + + + ]: 390 : && (ehdr->e_type == ET_REL
974 [ + + ]: 135 : || elf_contains_chdrs (ebl->elf)));
975 : :
976 : 360 : Elf *pure_elf = NULL;
977 : 360 : Ebl *pure_ebl = ebl;
978 [ + + ]: 360 : if (print_unchanged)
979 : : {
980 : : /* Read the file afresh. */
981 : 72 : off_t aroff = elf_getaroff (elf);
982 : 72 : pure_elf = dwelf_elf_begin (fd);
983 [ - + ]: 72 : if (aroff > 0)
984 : : {
985 : : /* Archive member. */
986 : 0 : (void) elf_rand (pure_elf, aroff);
987 : 0 : Elf *armem = elf_begin (-1, ELF_C_READ_MMAP, pure_elf);
988 : 0 : elf_end (pure_elf);
989 : 0 : pure_elf = armem;
990 : : }
991 [ - + ]: 72 : if (pure_elf == NULL)
992 : : {
993 : 0 : error (0, 0, _("cannot read ELF: %s"), elf_errmsg (-1));
994 : 0 : return;
995 : : }
996 : 72 : pure_ebl = ebl_openbackend (pure_elf);
997 [ - + ]: 72 : if (pure_ebl == NULL)
998 : 0 : goto ebl_error;
999 : : }
1000 : :
1001 [ + + ]: 360 : if (print_file_header)
1002 : 82 : print_ehdr (ebl, ehdr);
1003 [ + + ]: 360 : if (print_section_header)
1004 : 170 : print_shdr (pure_ebl, ehdr);
1005 [ + + ]: 360 : if (print_program_header)
1006 : 83 : print_phdr (ebl, ehdr);
1007 [ + + ]: 360 : if (print_section_groups)
1008 : 90 : print_scngrp (ebl);
1009 [ + + ]: 360 : if (print_dynamic_table)
1010 : 83 : print_dynamic (ebl);
1011 [ + + ]: 360 : if (print_relocations)
1012 : 82 : print_relocs (pure_ebl, ehdr);
1013 [ + + ]: 360 : if (print_histogram)
1014 : 81 : handle_hash (ebl);
1015 [ + + + + ]: 360 : if (print_symbol_table || print_dynsym_table)
1016 : 96 : print_symtab (ebl, SHT_DYNSYM);
1017 [ + + ]: 360 : if (print_version_info)
1018 : 81 : print_verinfo (ebl);
1019 [ + + ]: 360 : if (print_symbol_table)
1020 : 95 : print_symtab (ebl, SHT_SYMTAB);
1021 [ + + ]: 360 : if (print_arch)
1022 : 85 : print_liblist (ebl);
1023 [ + + ]: 360 : if (print_arch)
1024 : 85 : print_attributes (ebl, ehdr);
1025 [ + + ]: 360 : if (dump_data_sections != NULL)
1026 : 6 : dump_data (pure_ebl);
1027 [ + + ]: 360 : if (string_sections != NULL)
1028 : 82 : dump_strings (ebl);
1029 [ + + ]: 360 : if ((print_debug_sections | implicit_debug_sections) != 0)
1030 : 209 : print_debug (dwflmod, ebl, ehdr);
1031 [ + + ]: 360 : if (print_notes)
1032 : 104 : handle_notes (pure_ebl, ehdr);
1033 [ - + ]: 360 : if (print_string_sections)
1034 : 0 : print_strings (ebl);
1035 : :
1036 : 360 : ebl_closebackend (ebl);
1037 : :
1038 [ + + ]: 360 : if (pure_ebl != ebl)
1039 : : {
1040 : 72 : ebl_closebackend (pure_ebl);
1041 : 72 : elf_end (pure_elf);
1042 : : }
1043 : : }
1044 : :
1045 : :
1046 : : /* Print file type. */
1047 : : static void
1048 : 82 : print_file_type (unsigned short int e_type)
1049 : : {
1050 [ + - ]: 82 : if (likely (e_type <= ET_CORE))
1051 : : {
1052 : 82 : static const char *const knowntypes[] =
1053 : : {
1054 : : N_("NONE (None)"),
1055 : : N_("REL (Relocatable file)"),
1056 : : N_("EXEC (Executable file)"),
1057 : : N_("DYN (Shared object file)"),
1058 : : N_("CORE (Core file)")
1059 : : };
1060 : 82 : puts (_(knowntypes[e_type]));
1061 : : }
1062 [ # # ]: 0 : else if (e_type >= ET_LOOS && e_type <= ET_HIOS)
1063 : 0 : printf (_("OS Specific: (%x)\n"), e_type);
1064 [ # # ]: 0 : else if (e_type >= ET_LOPROC /* && e_type <= ET_HIPROC always true */)
1065 : 0 : printf (_("Processor Specific: (%x)\n"), e_type);
1066 : : else
1067 : 0 : puts ("???");
1068 : 82 : }
1069 : :
1070 : :
1071 : : /* Print ELF header. */
1072 : : static void
1073 : 82 : print_ehdr (Ebl *ebl, GElf_Ehdr *ehdr)
1074 : : {
1075 : 82 : fputs_unlocked (_("ELF Header:\n Magic: "), stdout);
1076 [ + + ]: 1394 : for (size_t cnt = 0; cnt < EI_NIDENT; ++cnt)
1077 : 1312 : printf (" %02hhx", ehdr->e_ident[cnt]);
1078 : :
1079 : 164 : printf (_("\n Class: %s\n"),
1080 [ + + ]: 82 : ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? "ELF32"
1081 : : : ehdr->e_ident[EI_CLASS] == ELFCLASS64 ? "ELF64"
1082 [ - + ]: 66 : : "\?\?\?");
1083 : :
1084 : 164 : printf (_(" Data: %s\n"),
1085 [ + + ]: 82 : ehdr->e_ident[EI_DATA] == ELFDATA2LSB
1086 : : ? "2's complement, little endian"
1087 : : : ehdr->e_ident[EI_DATA] == ELFDATA2MSB
1088 [ - + ]: 12 : ? "2's complement, big endian" : "\?\?\?");
1089 : :
1090 : 164 : printf (_(" Ident Version: %hhd %s\n"),
1091 [ + - ]: 82 : ehdr->e_ident[EI_VERSION],
1092 : 82 : ehdr->e_ident[EI_VERSION] == EV_CURRENT ? _("(current)")
1093 : : : "(\?\?\?)");
1094 : :
1095 : 82 : char buf[512];
1096 : 82 : printf (_(" OS/ABI: %s\n"),
1097 : 82 : ebl_osabi_name (ebl, ehdr->e_ident[EI_OSABI], buf, sizeof (buf)));
1098 : :
1099 : 164 : printf (_(" ABI Version: %hhd\n"),
1100 : 82 : ehdr->e_ident[EI_ABIVERSION]);
1101 : :
1102 : 82 : fputs_unlocked (_(" Type: "), stdout);
1103 : 82 : print_file_type (ehdr->e_type);
1104 : :
1105 : 82 : const char *machine = dwelf_elf_e_machine_string (ehdr->e_machine);
1106 [ + - ]: 82 : if (machine != NULL)
1107 : 82 : printf (_(" Machine: %s\n"), machine);
1108 : : else
1109 : 82 : printf (_(" Machine: <unknown>: 0x%x\n"),
1110 : 0 : ehdr->e_machine);
1111 : :
1112 [ + - ]: 82 : printf (_(" Version: %d %s\n"),
1113 : : ehdr->e_version,
1114 : 82 : ehdr->e_version == EV_CURRENT ? _("(current)") : "(\?\?\?)");
1115 : :
1116 : 82 : printf (_(" Entry point address: %#" PRIx64 "\n"),
1117 : : ehdr->e_entry);
1118 : :
1119 : 82 : printf (_(" Start of program headers: %" PRId64 " %s\n"),
1120 : : ehdr->e_phoff, _("(bytes into file)"));
1121 : :
1122 : 82 : printf (_(" Start of section headers: %" PRId64 " %s\n"),
1123 : : ehdr->e_shoff, _("(bytes into file)"));
1124 : :
1125 : 82 : printf (_(" Flags: %s\n"),
1126 : : ebl_machine_flag_name (ebl, ehdr->e_flags, buf, sizeof (buf)));
1127 : :
1128 : 164 : printf (_(" Size of this header: %" PRId16 " %s\n"),
1129 : 82 : ehdr->e_ehsize, _("(bytes)"));
1130 : :
1131 : 164 : printf (_(" Size of program header entries: %" PRId16 " %s\n"),
1132 : 82 : ehdr->e_phentsize, _("(bytes)"));
1133 : :
1134 : 164 : printf (_(" Number of program headers entries: %" PRId16),
1135 : 82 : ehdr->e_phnum);
1136 [ + + ]: 82 : if (ehdr->e_phnum == PN_XNUM)
1137 : : {
1138 : 1 : GElf_Shdr shdr_mem;
1139 : 1 : GElf_Shdr *shdr = gelf_getshdr (elf_getscn (ebl->elf, 0), &shdr_mem);
1140 [ + - ]: 1 : if (shdr != NULL)
1141 : 1 : printf (_(" (%" PRIu32 " in [0].sh_info)"),
1142 : 1 : (uint32_t) shdr->sh_info);
1143 : : else
1144 : 0 : fputs_unlocked (_(" ([0] not available)"), stdout);
1145 : : }
1146 [ - + ]: 82 : fputc_unlocked ('\n', stdout);
1147 : :
1148 : 164 : printf (_(" Size of section header entries: %" PRId16 " %s\n"),
1149 : 82 : ehdr->e_shentsize, _("(bytes)"));
1150 : :
1151 : 164 : printf (_(" Number of section headers entries: %" PRId16),
1152 : 82 : ehdr->e_shnum);
1153 [ - + ]: 82 : if (ehdr->e_shnum == 0)
1154 : : {
1155 : 0 : GElf_Shdr shdr_mem;
1156 : 0 : GElf_Shdr *shdr = gelf_getshdr (elf_getscn (ebl->elf, 0), &shdr_mem);
1157 [ # # ]: 0 : if (shdr != NULL)
1158 : 0 : printf (_(" (%" PRIu32 " in [0].sh_size)"),
1159 : 0 : (uint32_t) shdr->sh_size);
1160 : : else
1161 : 0 : fputs_unlocked (_(" ([0] not available)"), stdout);
1162 : : }
1163 [ - + ]: 82 : fputc_unlocked ('\n', stdout);
1164 : :
1165 [ - + ]: 82 : if (unlikely (ehdr->e_shstrndx == SHN_XINDEX))
1166 : : {
1167 : 0 : GElf_Shdr shdr_mem;
1168 : 0 : GElf_Shdr *shdr = gelf_getshdr (elf_getscn (ebl->elf, 0), &shdr_mem);
1169 [ # # ]: 0 : if (shdr != NULL)
1170 : : /* We managed to get the zeroth section. */
1171 : 0 : snprintf (buf, sizeof (buf), _(" (%" PRIu32 " in [0].sh_link)"),
1172 : 0 : (uint32_t) shdr->sh_link);
1173 : : else
1174 : : {
1175 : 0 : strncpy (buf, _(" ([0] not available)"), sizeof (buf));
1176 : 0 : buf[sizeof (buf) - 1] = '\0';
1177 : : }
1178 : :
1179 : 0 : printf (_(" Section header string table index: XINDEX%s\n\n"),
1180 : : buf);
1181 : : }
1182 : : else
1183 : 164 : printf (_(" Section header string table index: %" PRId16 "\n\n"),
1184 : 82 : ehdr->e_shstrndx);
1185 : 82 : }
1186 : :
1187 : :
1188 : : static const char *
1189 : 40139 : get_visibility_type (int value)
1190 : : {
1191 [ - + - - : 40139 : switch (value)
+ ]
1192 : : {
1193 : : case STV_DEFAULT:
1194 : : return "DEFAULT";
1195 : 0 : case STV_INTERNAL:
1196 : 0 : return "INTERNAL";
1197 : 258 : case STV_HIDDEN:
1198 : 258 : return "HIDDEN";
1199 : 0 : case STV_PROTECTED:
1200 : 0 : return "PROTECTED";
1201 : 0 : default:
1202 : 0 : return "???";
1203 : : }
1204 : : }
1205 : :
1206 : : static const char *
1207 : 12 : elf_ch_type_name (unsigned int code)
1208 : : {
1209 : 12 : if (code == 0)
1210 : : return "NONE";
1211 : :
1212 [ + - ]: 12 : if (code == ELFCOMPRESS_ZLIB)
1213 : 12 : return "ZLIB";
1214 : :
1215 : : return "UNKNOWN";
1216 : : }
1217 : :
1218 : : /* Print the section headers. */
1219 : : static void
1220 : 170 : print_shdr (Ebl *ebl, GElf_Ehdr *ehdr)
1221 : : {
1222 : 170 : size_t cnt;
1223 : 170 : size_t shstrndx;
1224 : :
1225 [ + + ]: 170 : if (! print_file_header)
1226 : : {
1227 : 89 : size_t sections;
1228 [ - + ]: 89 : if (unlikely (elf_getshdrnum (ebl->elf, §ions) < 0))
1229 : 0 : error (EXIT_FAILURE, 0,
1230 : 0 : _("cannot get number of sections: %s"),
1231 : : elf_errmsg (-1));
1232 : :
1233 : 89 : printf (_("\
1234 : : There are %zd section headers, starting at offset %#" PRIx64 ":\n\
1235 : : \n"),
1236 : : sections, ehdr->e_shoff);
1237 : : }
1238 : :
1239 : : /* Get the section header string table index. */
1240 [ - + ]: 170 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
1241 : 0 : error (EXIT_FAILURE, 0,
1242 : 0 : _("cannot get section header string table index: %s"),
1243 : : elf_errmsg (-1));
1244 : :
1245 : 170 : puts (_("Section Headers:"));
1246 : :
1247 [ + + ]: 170 : if (ehdr->e_ident[EI_CLASS] == ELFCLASS32)
1248 : 62 : puts (_("[Nr] Name Type Addr Off Size ES Flags Lk Inf Al"));
1249 : : else
1250 : 108 : puts (_("[Nr] Name Type Addr Off Size ES Flags Lk Inf Al"));
1251 : :
1252 [ + + ]: 170 : if (print_decompress)
1253 : : {
1254 [ + + ]: 8 : if (ehdr->e_ident[EI_CLASS] == ELFCLASS32)
1255 : 4 : puts (_(" [Compression Size Al]"));
1256 : : else
1257 : 4 : puts (_(" [Compression Size Al]"));
1258 : : }
1259 : :
1260 [ + + ]: 1577427 : for (cnt = 0; cnt < shnum; ++cnt)
1261 : : {
1262 : 1577257 : Elf_Scn *scn = elf_getscn (ebl->elf, cnt);
1263 : :
1264 [ - + ]: 1577257 : if (unlikely (scn == NULL))
1265 : 0 : error (EXIT_FAILURE, 0, _("cannot get section: %s"),
1266 : : elf_errmsg (-1));
1267 : :
1268 : : /* Get the section header. */
1269 : 1577257 : GElf_Shdr shdr_mem;
1270 : 1577257 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
1271 [ - + ]: 1577257 : if (unlikely (shdr == NULL))
1272 : 0 : error (EXIT_FAILURE, 0, _("cannot get section header: %s"),
1273 : : elf_errmsg (-1));
1274 : :
1275 : 1577257 : char flagbuf[20];
1276 : 1577257 : char *cp = flagbuf;
1277 [ + + ]: 1577257 : if (shdr->sh_flags & SHF_WRITE)
1278 : 1016 : *cp++ = 'W';
1279 [ + + ]: 1577257 : if (shdr->sh_flags & SHF_ALLOC)
1280 : 2892 : *cp++ = 'A';
1281 [ + + ]: 1577257 : if (shdr->sh_flags & SHF_EXECINSTR)
1282 : 507 : *cp++ = 'X';
1283 [ + + ]: 1577257 : if (shdr->sh_flags & SHF_MERGE)
1284 : 163 : *cp++ = 'M';
1285 [ + + ]: 1577257 : if (shdr->sh_flags & SHF_STRINGS)
1286 : 157 : *cp++ = 'S';
1287 [ + + ]: 1577257 : if (shdr->sh_flags & SHF_INFO_LINK)
1288 : 157 : *cp++ = 'I';
1289 [ + + ]: 1577257 : if (shdr->sh_flags & SHF_LINK_ORDER)
1290 : 2 : *cp++ = 'L';
1291 [ - + ]: 1577257 : if (shdr->sh_flags & SHF_OS_NONCONFORMING)
1292 : 0 : *cp++ = 'N';
1293 [ - + ]: 1577257 : if (shdr->sh_flags & SHF_GROUP)
1294 : 0 : *cp++ = 'G';
1295 [ + + ]: 1577257 : if (shdr->sh_flags & SHF_TLS)
1296 : 15 : *cp++ = 'T';
1297 [ + + ]: 1577257 : if (shdr->sh_flags & SHF_COMPRESSED)
1298 : 17 : *cp++ = 'C';
1299 [ - + ]: 1577257 : if (shdr->sh_flags & SHF_ORDERED)
1300 : 0 : *cp++ = 'O';
1301 [ - + ]: 1577257 : if (shdr->sh_flags & SHF_EXCLUDE)
1302 : 0 : *cp++ = 'E';
1303 [ + + ]: 1577257 : if (shdr->sh_flags & SHF_GNU_RETAIN)
1304 : 1 : *cp++ = 'R';
1305 : 1577257 : *cp = '\0';
1306 : :
1307 : 1577257 : const char *sname;
1308 : 1577257 : char buf[128];
1309 [ - + ]: 1577257 : sname = elf_strptr (ebl->elf, shstrndx, shdr->sh_name) ?: "<corrupt>";
1310 [ + + ]: 2366581 : printf ("[%2zu] %-20s %-12s %0*" PRIx64 " %0*" PRIx64 " %0*" PRIx64
1311 : : " %2" PRId64 " %-5s %2" PRId32 " %3" PRId32
1312 : : " %2" PRId64 "\n",
1313 : : cnt, sname,
1314 : 1577257 : ebl_section_type_name (ebl, shdr->sh_type, buf, sizeof (buf)),
1315 [ + + ]: 1577257 : ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 8 : 16, shdr->sh_addr,
1316 : : ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 6 : 8, shdr->sh_offset,
1317 : : ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 6 : 8, shdr->sh_size,
1318 : : shdr->sh_entsize, flagbuf, shdr->sh_link, shdr->sh_info,
1319 : : shdr->sh_addralign);
1320 : :
1321 [ + + ]: 1577257 : if (print_decompress)
1322 : : {
1323 [ + + ]: 76 : if ((shdr->sh_flags & SHF_COMPRESSED) != 0)
1324 : : {
1325 : 12 : GElf_Chdr chdr;
1326 [ + - ]: 12 : if (gelf_getchdr (scn, &chdr) != NULL)
1327 [ + - ]: 24 : printf (" [ELF %s (%" PRId32 ") %0*" PRIx64
1328 : : " %2" PRId64 "]\n",
1329 : : elf_ch_type_name (chdr.ch_type),
1330 : : chdr.ch_type,
1331 [ + + ]: 12 : ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 6 : 8,
1332 : : chdr.ch_size, chdr.ch_addralign);
1333 : : else
1334 : 12 : error (0, 0,
1335 : 0 : _("bad compression header for section %zd: %s"),
1336 : : elf_ndxscn (scn), elf_errmsg (-1));
1337 : : }
1338 [ + + ]: 64 : else if (strncmp(".zdebug", sname, strlen (".zdebug")) == 0)
1339 : : {
1340 : 12 : ssize_t size;
1341 [ + - ]: 12 : if ((size = dwelf_scn_gnu_compressed_size (scn)) >= 0)
1342 : 1577269 : printf (" [GNU ZLIB %0*zx ]\n",
1343 [ + + ]: 12 : ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 6 : 8, size);
1344 : : else
1345 : 1577257 : error (0, 0,
1346 : 0 : _("bad gnu compressed size for section %zd: %s"),
1347 : : elf_ndxscn (scn), elf_errmsg (-1));
1348 : : }
1349 : : }
1350 : : }
1351 : :
1352 [ - + ]: 170 : fputc_unlocked ('\n', stdout);
1353 : 170 : }
1354 : :
1355 : :
1356 : : /* Print the program header. */
1357 : : static void
1358 : 83 : print_phdr (Ebl *ebl, GElf_Ehdr *ehdr)
1359 : : {
1360 [ + + ]: 83 : if (phnum == 0)
1361 : : /* No program header, this is OK in relocatable objects. */
1362 : 34 : return;
1363 : :
1364 : 49 : puts (_("Program Headers:"));
1365 [ + + ]: 49 : if (ehdr->e_ident[EI_CLASS] == ELFCLASS32)
1366 : 7 : puts (_("\
1367 : : Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align"));
1368 : : else
1369 : 42 : puts (_("\
1370 : : Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align"));
1371 : :
1372 : : /* Process all program headers. */
1373 : : bool has_relro = false;
1374 : : GElf_Addr relro_from = 0;
1375 : : GElf_Addr relro_to = 0;
1376 [ + + ]: 526 : for (size_t cnt = 0; cnt < phnum; ++cnt)
1377 : : {
1378 : 477 : char buf[128];
1379 : 477 : GElf_Phdr mem;
1380 : 477 : GElf_Phdr *phdr = gelf_getphdr (ebl->elf, cnt, &mem);
1381 : :
1382 : : /* If for some reason the header cannot be returned show this. */
1383 [ - + ]: 477 : if (unlikely (phdr == NULL))
1384 : : {
1385 : 0 : puts (" ???");
1386 : 0 : continue;
1387 : : }
1388 : :
1389 : 477 : printf (" %-14s 0x%06" PRIx64 " 0x%0*" PRIx64 " 0x%0*" PRIx64
1390 : : " 0x%06" PRIx64 " 0x%06" PRIx64 " %c%c%c 0x%" PRIx64 "\n",
1391 : 477 : ebl_segment_type_name (ebl, phdr->p_type, buf, sizeof (buf)),
1392 : : phdr->p_offset,
1393 [ + + ]: 477 : ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 8 : 16, phdr->p_vaddr,
1394 : : ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 8 : 16, phdr->p_paddr,
1395 : : phdr->p_filesz,
1396 : : phdr->p_memsz,
1397 [ - + ]: 477 : phdr->p_flags & PF_R ? 'R' : ' ',
1398 [ + + ]: 477 : phdr->p_flags & PF_W ? 'W' : ' ',
1399 [ + + ]: 477 : phdr->p_flags & PF_X ? 'E' : ' ',
1400 : : phdr->p_align);
1401 : :
1402 [ + + ]: 477 : if (phdr->p_type == PT_INTERP)
1403 : : {
1404 : : /* If we are sure the file offset is valid then we can show
1405 : : the user the name of the interpreter. We check whether
1406 : : there is a section at the file offset. Normally there
1407 : : would be a section called ".interp". But in separate
1408 : : .debug files it is a NOBITS section (and so doesn't match
1409 : : with gelf_offscn). Which probably means the offset is
1410 : : not valid another reason could be because the ELF file
1411 : : just doesn't contain any section headers, in that case
1412 : : just play it safe and don't display anything. */
1413 : :
1414 : 47 : Elf_Scn *scn = gelf_offscn (ebl->elf, phdr->p_offset);
1415 : 47 : GElf_Shdr shdr_mem;
1416 : 47 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
1417 : :
1418 : 47 : size_t maxsize;
1419 : 47 : char *filedata = elf_rawfile (ebl->elf, &maxsize);
1420 : :
1421 [ + - + + ]: 47 : if (shdr != NULL && shdr->sh_type == SHT_PROGBITS
1422 [ + - + - ]: 35 : && filedata != NULL && phdr->p_offset < maxsize
1423 [ + - ]: 35 : && phdr->p_filesz <= maxsize - phdr->p_offset
1424 [ + - ]: 35 : && memchr (filedata + phdr->p_offset, '\0',
1425 : : phdr->p_filesz) != NULL)
1426 : 47 : printf (_("\t[Requesting program interpreter: %s]\n"),
1427 : 35 : filedata + phdr->p_offset);
1428 : : }
1429 [ + + ]: 430 : else if (phdr->p_type == PT_GNU_RELRO)
1430 : : {
1431 : 43 : has_relro = true;
1432 : 43 : relro_from = phdr->p_vaddr;
1433 : 43 : relro_to = relro_from + phdr->p_memsz;
1434 : : }
1435 : : }
1436 : :
1437 : 49 : size_t sections;
1438 [ - + ]: 49 : if (unlikely (elf_getshdrnum (ebl->elf, §ions) < 0))
1439 : 0 : error (EXIT_FAILURE, 0,
1440 : 0 : _("cannot get number of sections: %s"),
1441 : : elf_errmsg (-1));
1442 : :
1443 [ + - ]: 49 : if (sections == 0)
1444 : : /* No sections in the file. Punt. */
1445 : : return;
1446 : :
1447 : : /* Get the section header string table index. */
1448 : 49 : size_t shstrndx;
1449 [ - + ]: 49 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
1450 : 0 : error (EXIT_FAILURE, 0,
1451 : 0 : _("cannot get section header string table index"));
1452 : :
1453 : 49 : puts (_("\n Section to Segment mapping:\n Segment Sections..."));
1454 : :
1455 [ + + ]: 526 : for (size_t cnt = 0; cnt < phnum; ++cnt)
1456 : : {
1457 : : /* Print the segment number. */
1458 : 477 : printf (" %2.2zu ", cnt);
1459 : :
1460 : 477 : GElf_Phdr phdr_mem;
1461 : 477 : GElf_Phdr *phdr = gelf_getphdr (ebl->elf, cnt, &phdr_mem);
1462 : : /* This must not happen. */
1463 [ - + ]: 477 : if (unlikely (phdr == NULL))
1464 : 0 : error (EXIT_FAILURE, 0, _("cannot get program header: %s"),
1465 : : elf_errmsg (-1));
1466 : :
1467 : : /* Iterate over the sections. */
1468 : : bool in_relro = false;
1469 : : bool in_ro = false;
1470 [ + + ]: 15212 : for (size_t inner = 1; inner < shnum; ++inner)
1471 : : {
1472 : 14735 : Elf_Scn *scn = elf_getscn (ebl->elf, inner);
1473 : : /* This should not happen. */
1474 [ - + ]: 14735 : if (unlikely (scn == NULL))
1475 : 0 : error (EXIT_FAILURE, 0, _("cannot get section: %s"),
1476 : : elf_errmsg (-1));
1477 : :
1478 : : /* Get the section header. */
1479 : 14735 : GElf_Shdr shdr_mem;
1480 : 14735 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
1481 [ - + ]: 14735 : if (unlikely (shdr == NULL))
1482 : 0 : error (EXIT_FAILURE, 0,
1483 : 0 : _("cannot get section header: %s"),
1484 : : elf_errmsg (-1));
1485 : :
1486 [ + - ]: 14735 : if (shdr->sh_size > 0
1487 : : /* Compare allocated sections by VMA, unallocated
1488 : : sections by file offset. */
1489 [ + + + + ]: 29470 : && (shdr->sh_flags & SHF_ALLOC
1490 : 11732 : ? (shdr->sh_addr >= phdr->p_vaddr
1491 [ + + ]: 11732 : && (shdr->sh_addr + shdr->sh_size
1492 [ + + ]: 7872 : <= phdr->p_vaddr + phdr->p_memsz))
1493 : 3003 : : (shdr->sh_offset >= phdr->p_offset
1494 [ + + ]: 3003 : && (shdr->sh_offset + shdr->sh_size
1495 [ + + ]: 2551 : <= phdr->p_offset + phdr->p_filesz))))
1496 : : {
1497 [ + + ]: 1722 : if (has_relro && !in_relro
1498 [ + + ]: 1147 : && shdr->sh_addr >= relro_from
1499 [ + + ]: 218 : && shdr->sh_addr + shdr->sh_size <= relro_to)
1500 : : {
1501 : 138 : fputs_unlocked (" [RELRO:", stdout);
1502 : 138 : in_relro = true;
1503 : : }
1504 [ + + + + ]: 1584 : else if (has_relro && in_relro && shdr->sh_addr >= relro_to)
1505 : : {
1506 : 32 : fputs_unlocked ("]", stdout);
1507 : 32 : in_relro = false;
1508 : : }
1509 [ + + ]: 1552 : else if (has_relro && in_relro
1510 [ + + ]: 347 : && shdr->sh_addr + shdr->sh_size > relro_to)
1511 : 11 : fputs_unlocked ("] <RELRO:", stdout);
1512 [ + + ]: 1541 : else if (phdr->p_type == PT_LOAD && (phdr->p_flags & PF_W) == 0)
1513 : : {
1514 [ + + ]: 875 : if (!in_ro)
1515 : : {
1516 : 83 : fputs_unlocked (" [RO:", stdout);
1517 : 83 : in_ro = true;
1518 : : }
1519 : : }
1520 : : else
1521 : : {
1522 : : /* Determine the segment this section is part of. */
1523 : : size_t cnt2;
1524 : : GElf_Phdr phdr2_mem;
1525 : : GElf_Phdr *phdr2 = NULL;
1526 [ + - ]: 2712 : for (cnt2 = 0; cnt2 < phnum; ++cnt2)
1527 : : {
1528 : 2712 : phdr2 = gelf_getphdr (ebl->elf, cnt2, &phdr2_mem);
1529 : :
1530 [ + - + + ]: 2712 : if (phdr2 != NULL && phdr2->p_type == PT_LOAD
1531 [ + - ]: 1436 : && shdr->sh_addr >= phdr2->p_vaddr
1532 : 1436 : && (shdr->sh_addr + shdr->sh_size
1533 [ + + ]: 1436 : <= phdr2->p_vaddr + phdr2->p_memsz))
1534 : : break;
1535 : : }
1536 : :
1537 [ + - ]: 666 : if (cnt2 < phnum)
1538 : : {
1539 [ + + + + ]: 666 : if ((phdr2->p_flags & PF_W) == 0 && !in_ro)
1540 : : {
1541 : 148 : fputs_unlocked (" [RO:", stdout);
1542 : 148 : in_ro = true;
1543 : : }
1544 [ + + - + ]: 518 : else if ((phdr2->p_flags & PF_W) != 0 && in_ro)
1545 : : {
1546 : 0 : fputs_unlocked ("]", stdout);
1547 : 0 : in_ro = false;
1548 : : }
1549 : : }
1550 : : }
1551 : :
1552 : 1722 : printf (" %s",
1553 : 1722 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name));
1554 : :
1555 : : /* Signal that this section is only partially covered. */
1556 [ + + ]: 1722 : if (has_relro && in_relro
1557 [ + + ]: 485 : && shdr->sh_addr + shdr->sh_size > relro_to)
1558 : : {
1559 : 11 : fputs_unlocked (">", stdout);
1560 : 11 : in_relro = false;
1561 : : }
1562 : : }
1563 : : }
1564 [ + + ]: 477 : if (in_relro || in_ro)
1565 : 326 : fputs_unlocked ("]", stdout);
1566 : :
1567 : : /* Finish the line. */
1568 [ - + ]: 954 : fputc_unlocked ('\n', stdout);
1569 : : }
1570 : : }
1571 : :
1572 : :
1573 : : static const char *
1574 : 390 : section_name (Ebl *ebl, GElf_Shdr *shdr)
1575 : : {
1576 : 390 : size_t shstrndx;
1577 [ + - ]: 390 : if (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0)
1578 : : return "???";
1579 [ - + ]: 390 : return elf_strptr (ebl->elf, shstrndx, shdr->sh_name) ?: "???";
1580 : : }
1581 : :
1582 : :
1583 : : static void
1584 : 16 : handle_scngrp (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr)
1585 : : {
1586 : : /* Get the data of the section. */
1587 : 16 : Elf_Data *data = elf_getdata (scn, NULL);
1588 : :
1589 : 16 : Elf_Scn *symscn = elf_getscn (ebl->elf, shdr->sh_link);
1590 : 16 : GElf_Shdr symshdr_mem;
1591 : 16 : GElf_Shdr *symshdr = gelf_getshdr (symscn, &symshdr_mem);
1592 : 16 : Elf_Data *symdata = elf_getdata (symscn, NULL);
1593 : :
1594 [ + - + - ]: 16 : if (data == NULL || data->d_size < sizeof (Elf32_Word) || symshdr == NULL
1595 [ - + ]: 16 : || symdata == NULL)
1596 : 0 : return;
1597 : :
1598 : : /* Get the section header string table index. */
1599 : 16 : size_t shstrndx;
1600 [ - + ]: 16 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
1601 : 0 : error (EXIT_FAILURE, 0,
1602 : 0 : _("cannot get section header string table index"));
1603 : :
1604 : 16 : Elf32_Word *grpref = (Elf32_Word *) data->d_buf;
1605 : :
1606 : 16 : GElf_Sym sym_mem;
1607 : 16 : GElf_Sym *sym = gelf_getsym (symdata, shdr->sh_info, &sym_mem);
1608 : :
1609 [ + + - + ]: 48 : printf ((grpref[0] & GRP_COMDAT)
1610 : 4 : ? ngettext ("\
1611 : : \nCOMDAT section group [%2zu] '%s' with signature '%s' contains %zu entry:\n",
1612 : : "\
1613 : : \nCOMDAT section group [%2zu] '%s' with signature '%s' contains %zu entries:\n",
1614 : : data->d_size / sizeof (Elf32_Word) - 1)
1615 : 12 : : ngettext ("\
1616 : : \nSection group [%2zu] '%s' with signature '%s' contains %zu entry:\n", "\
1617 : : \nSection group [%2zu] '%s' with signature '%s' contains %zu entries:\n",
1618 : : data->d_size / sizeof (Elf32_Word) - 1),
1619 : : elf_ndxscn (scn),
1620 : 16 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
1621 : : (sym == NULL ? NULL
1622 [ + - ]: 16 : : elf_strptr (ebl->elf, symshdr->sh_link, sym->st_name))
1623 : 0 : ?: _("<INVALID SYMBOL>"),
1624 : 16 : data->d_size / sizeof (Elf32_Word) - 1);
1625 : :
1626 [ + + ]: 52 : for (size_t cnt = 1; cnt < data->d_size / sizeof (Elf32_Word); ++cnt)
1627 : : {
1628 : 36 : GElf_Shdr grpshdr_mem;
1629 : 36 : GElf_Shdr *grpshdr = gelf_getshdr (elf_getscn (ebl->elf, grpref[cnt]),
1630 : : &grpshdr_mem);
1631 : :
1632 : 36 : const char *str;
1633 [ + - ]: 72 : printf (" [%2u] %s\n",
1634 : : grpref[cnt],
1635 : : grpshdr != NULL
1636 [ - + ]: 36 : && (str = elf_strptr (ebl->elf, shstrndx, grpshdr->sh_name))
1637 : 0 : ? str : _("<INVALID SECTION>"));
1638 : : }
1639 : : }
1640 : :
1641 : :
1642 : : static void
1643 : 90 : print_scngrp (Ebl *ebl)
1644 : : {
1645 : : /* Find all relocation sections and handle them. */
1646 : 90 : Elf_Scn *scn = NULL;
1647 : :
1648 [ + + ]: 2504 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
1649 : : {
1650 : : /* Handle the section if it is a symbol table. */
1651 : 2414 : GElf_Shdr shdr_mem;
1652 : 2414 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
1653 : :
1654 [ + - + + ]: 2414 : if (shdr != NULL && shdr->sh_type == SHT_GROUP)
1655 : : {
1656 [ - + ]: 16 : if ((shdr->sh_flags & SHF_COMPRESSED) != 0)
1657 : : {
1658 [ # # ]: 0 : if (elf_compress (scn, 0, 0) < 0)
1659 : 0 : printf ("WARNING: %s [%zd]\n",
1660 : : _("Couldn't uncompress section"),
1661 : : elf_ndxscn (scn));
1662 : 0 : shdr = gelf_getshdr (scn, &shdr_mem);
1663 [ # # ]: 0 : if (unlikely (shdr == NULL))
1664 : 0 : error (EXIT_FAILURE, 0,
1665 : 0 : _("cannot get section [%zd] header: %s"),
1666 : : elf_ndxscn (scn),
1667 : : elf_errmsg (-1));
1668 : : }
1669 : 16 : handle_scngrp (ebl, scn, shdr);
1670 : : }
1671 : : }
1672 : 90 : }
1673 : :
1674 : :
1675 : : static const struct flags
1676 : : {
1677 : : int mask;
1678 : : const char *str;
1679 : : } dt_flags[] =
1680 : : {
1681 : : { DF_ORIGIN, "ORIGIN" },
1682 : : { DF_SYMBOLIC, "SYMBOLIC" },
1683 : : { DF_TEXTREL, "TEXTREL" },
1684 : : { DF_BIND_NOW, "BIND_NOW" },
1685 : : { DF_STATIC_TLS, "STATIC_TLS" }
1686 : : };
1687 : : static const int ndt_flags = sizeof (dt_flags) / sizeof (dt_flags[0]);
1688 : :
1689 : : static const struct flags dt_flags_1[] =
1690 : : {
1691 : : { DF_1_NOW, "NOW" },
1692 : : { DF_1_GLOBAL, "GLOBAL" },
1693 : : { DF_1_GROUP, "GROUP" },
1694 : : { DF_1_NODELETE, "NODELETE" },
1695 : : { DF_1_LOADFLTR, "LOADFLTR" },
1696 : : { DF_1_INITFIRST, "INITFIRST" },
1697 : : { DF_1_NOOPEN, "NOOPEN" },
1698 : : { DF_1_ORIGIN, "ORIGIN" },
1699 : : { DF_1_DIRECT, "DIRECT" },
1700 : : { DF_1_TRANS, "TRANS" },
1701 : : { DF_1_INTERPOSE, "INTERPOSE" },
1702 : : { DF_1_NODEFLIB, "NODEFLIB" },
1703 : : { DF_1_NODUMP, "NODUMP" },
1704 : : { DF_1_CONFALT, "CONFALT" },
1705 : : { DF_1_ENDFILTEE, "ENDFILTEE" },
1706 : : { DF_1_DISPRELDNE, "DISPRELDNE" },
1707 : : { DF_1_DISPRELPND, "DISPRELPND" },
1708 : : };
1709 : : static const int ndt_flags_1 = sizeof (dt_flags_1) / sizeof (dt_flags_1[0]);
1710 : :
1711 : : static const struct flags dt_feature_1[] =
1712 : : {
1713 : : { DTF_1_PARINIT, "PARINIT" },
1714 : : { DTF_1_CONFEXP, "CONFEXP" }
1715 : : };
1716 : : static const int ndt_feature_1 = (sizeof (dt_feature_1)
1717 : : / sizeof (dt_feature_1[0]));
1718 : :
1719 : : static const struct flags dt_posflag_1[] =
1720 : : {
1721 : : { DF_P1_LAZYLOAD, "LAZYLOAD" },
1722 : : { DF_P1_GROUPPERM, "GROUPPERM" }
1723 : : };
1724 : : static const int ndt_posflag_1 = (sizeof (dt_posflag_1)
1725 : : / sizeof (dt_posflag_1[0]));
1726 : :
1727 : :
1728 : : static void
1729 : 19 : print_flags (int class, GElf_Xword d_val, const struct flags *flags,
1730 : : int nflags)
1731 : : {
1732 : 19 : bool first = true;
1733 : 19 : int cnt;
1734 : :
1735 [ + + ]: 276 : for (cnt = 0; cnt < nflags; ++cnt)
1736 [ + + ]: 257 : if (d_val & flags[cnt].mask)
1737 : : {
1738 [ + + ]: 28 : if (!first)
1739 [ - + ]: 18 : putchar_unlocked (' ');
1740 : 28 : fputs_unlocked (flags[cnt].str, stdout);
1741 : 28 : d_val &= ~flags[cnt].mask;
1742 : 28 : first = false;
1743 : : }
1744 : :
1745 [ + + ]: 19 : if (d_val != 0)
1746 : : {
1747 [ + + ]: 17 : if (!first)
1748 [ - + ]: 8 : putchar_unlocked (' ');
1749 [ + + ]: 17 : printf ("%#0*" PRIx64, class == ELFCLASS32 ? 10 : 18, d_val);
1750 : : }
1751 : :
1752 [ - + ]: 19 : putchar_unlocked ('\n');
1753 : 19 : }
1754 : :
1755 : :
1756 : : static void
1757 : 3 : print_dt_flags (int class, GElf_Xword d_val)
1758 : : {
1759 : 3 : print_flags (class, d_val, dt_flags, ndt_flags);
1760 : 3 : }
1761 : :
1762 : :
1763 : : static void
1764 : 14 : print_dt_flags_1 (int class, GElf_Xword d_val)
1765 : : {
1766 : 14 : print_flags (class, d_val, dt_flags_1, ndt_flags_1);
1767 : 14 : }
1768 : :
1769 : :
1770 : : static void
1771 : 1 : print_dt_feature_1 (int class, GElf_Xword d_val)
1772 : : {
1773 : 1 : print_flags (class, d_val, dt_feature_1, ndt_feature_1);
1774 : 1 : }
1775 : :
1776 : :
1777 : : static void
1778 : 1 : print_dt_posflag_1 (int class, GElf_Xword d_val)
1779 : : {
1780 : 1 : print_flags (class, d_val, dt_posflag_1, ndt_posflag_1);
1781 : 1 : }
1782 : :
1783 : :
1784 : : static void
1785 : 37 : handle_dynamic (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr)
1786 : : {
1787 : 37 : int class = gelf_getclass (ebl->elf);
1788 : 37 : GElf_Shdr glink_mem;
1789 : 37 : GElf_Shdr *glink;
1790 : 37 : Elf_Data *data;
1791 : 37 : size_t cnt;
1792 : 37 : size_t shstrndx;
1793 : 37 : size_t sh_entsize;
1794 : :
1795 : : /* Get the data of the section. */
1796 : 37 : data = elf_getdata (scn, NULL);
1797 [ - + ]: 37 : if (data == NULL)
1798 : 0 : return;
1799 : :
1800 : : /* Get the section header string table index. */
1801 [ - + ]: 37 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
1802 : 0 : error (EXIT_FAILURE, 0,
1803 : 0 : _("cannot get section header string table index"));
1804 : :
1805 : 37 : sh_entsize = gelf_fsize (ebl->elf, ELF_T_DYN, 1, EV_CURRENT);
1806 : :
1807 : 37 : glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link), &glink_mem);
1808 [ - + ]: 37 : if (glink == NULL)
1809 : 0 : error (EXIT_FAILURE, 0, _("invalid sh_link value in section %zu"),
1810 : : elf_ndxscn (scn));
1811 : :
1812 : 74 : printf (ngettext ("\
1813 : : \nDynamic segment contains %lu entry:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'\n",
1814 : : "\
1815 : : \nDynamic segment contains %lu entries:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'\n",
1816 : : shdr->sh_size / sh_entsize),
1817 [ + + ]: 37 : (unsigned long int) (shdr->sh_size / sh_entsize),
1818 : : class == ELFCLASS32 ? 10 : 18, shdr->sh_addr,
1819 : : shdr->sh_offset,
1820 : 37 : (int) shdr->sh_link,
1821 : 37 : elf_strptr (ebl->elf, shstrndx, glink->sh_name));
1822 : 37 : fputs_unlocked (_(" Type Value\n"), stdout);
1823 : :
1824 [ + + ]: 1222 : for (cnt = 0; cnt < shdr->sh_size / sh_entsize; ++cnt)
1825 : : {
1826 : 1148 : GElf_Dyn dynmem;
1827 : 1148 : GElf_Dyn *dyn = gelf_getdyn (data, cnt, &dynmem);
1828 [ + - ]: 1148 : if (dyn == NULL)
1829 : : break;
1830 : :
1831 : 1148 : char buf[64];
1832 : 1148 : printf (" %-17s ",
1833 : : ebl_dynamic_tag_name (ebl, dyn->d_tag, buf, sizeof (buf)));
1834 : :
1835 [ + + + + : 1148 : switch (dyn->d_tag)
+ + + + +
+ + + + ]
1836 : : {
1837 : 240 : case DT_NULL:
1838 : : case DT_DEBUG:
1839 : : case DT_BIND_NOW:
1840 : : case DT_TEXTREL:
1841 : : /* No further output. */
1842 [ - + ]: 240 : fputc_unlocked ('\n', stdout);
1843 : : break;
1844 : :
1845 : 76 : case DT_NEEDED:
1846 : 76 : printf (_("Shared library: [%s]\n"),
1847 : 76 : elf_strptr (ebl->elf, shdr->sh_link, dyn->d_un.d_val));
1848 : : break;
1849 : :
1850 : 3 : case DT_SONAME:
1851 : 3 : printf (_("Library soname: [%s]\n"),
1852 : 3 : elf_strptr (ebl->elf, shdr->sh_link, dyn->d_un.d_val));
1853 : : break;
1854 : :
1855 : 1 : case DT_RPATH:
1856 : 1 : printf (_("Library rpath: [%s]\n"),
1857 : 1 : elf_strptr (ebl->elf, shdr->sh_link, dyn->d_un.d_val));
1858 : : break;
1859 : :
1860 : 1 : case DT_RUNPATH:
1861 : 1 : printf (_("Library runpath: [%s]\n"),
1862 : 1 : elf_strptr (ebl->elf, shdr->sh_link, dyn->d_un.d_val));
1863 : : break;
1864 : :
1865 : 256 : case DT_PLTRELSZ:
1866 : : case DT_RELASZ:
1867 : : case DT_STRSZ:
1868 : : case DT_RELSZ:
1869 : : case DT_RELAENT:
1870 : : case DT_SYMENT:
1871 : : case DT_RELENT:
1872 : : case DT_PLTPADSZ:
1873 : : case DT_MOVEENT:
1874 : : case DT_MOVESZ:
1875 : : case DT_INIT_ARRAYSZ:
1876 : : case DT_FINI_ARRAYSZ:
1877 : : case DT_SYMINSZ:
1878 : : case DT_SYMINENT:
1879 : : case DT_GNU_CONFLICTSZ:
1880 : : case DT_GNU_LIBLISTSZ:
1881 : 256 : printf (_("%" PRId64 " (bytes)\n"), dyn->d_un.d_val);
1882 : : break;
1883 : :
1884 : 59 : case DT_VERDEFNUM:
1885 : : case DT_VERNEEDNUM:
1886 : : case DT_RELACOUNT:
1887 : : case DT_RELCOUNT:
1888 : 59 : printf ("%" PRId64 "\n", dyn->d_un.d_val);
1889 : : break;
1890 : :
1891 : 35 : case DT_PLTREL:;
1892 : 35 : const char *tagname = ebl_dynamic_tag_name (ebl, dyn->d_un.d_val,
1893 : : NULL, 0);
1894 [ + + ]: 35 : puts (tagname ?: "???");
1895 : 35 : break;
1896 : :
1897 : 3 : case DT_FLAGS:
1898 : 3 : print_dt_flags (class, dyn->d_un.d_val);
1899 : : break;
1900 : :
1901 : 14 : case DT_FLAGS_1:
1902 : 14 : print_dt_flags_1 (class, dyn->d_un.d_val);
1903 : : break;
1904 : :
1905 : 1 : case DT_FEATURE_1:
1906 : 1 : print_dt_feature_1 (class, dyn->d_un.d_val);
1907 : : break;
1908 : :
1909 : 1 : case DT_POSFLAG_1:
1910 : 1 : print_dt_posflag_1 (class, dyn->d_un.d_val);
1911 : : break;
1912 : :
1913 : : default:
1914 : 1148 : printf ("%#0*" PRIx64 "\n",
1915 : : class == ELFCLASS32 ? 10 : 18, dyn->d_un.d_val);
1916 : : break;
1917 : : }
1918 : : }
1919 : : }
1920 : :
1921 : :
1922 : : /* Print the dynamic segment. */
1923 : : static void
1924 : 83 : print_dynamic (Ebl *ebl)
1925 : : {
1926 [ + + ]: 301 : for (size_t i = 0; i < phnum; ++i)
1927 : : {
1928 : 267 : GElf_Phdr phdr_mem;
1929 : 267 : GElf_Phdr *phdr = gelf_getphdr (ebl->elf, i, &phdr_mem);
1930 : :
1931 [ + - + + ]: 267 : if (phdr != NULL && phdr->p_type == PT_DYNAMIC)
1932 : : {
1933 : 49 : Elf_Scn *scn = gelf_offscn (ebl->elf, phdr->p_offset);
1934 : 49 : GElf_Shdr shdr_mem;
1935 : 49 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
1936 [ + + + + ]: 49 : if (shdr != NULL && shdr->sh_type == SHT_DYNAMIC)
1937 : 37 : handle_dynamic (ebl, scn, shdr);
1938 : 49 : break;
1939 : : }
1940 : : }
1941 : 83 : }
1942 : :
1943 : :
1944 : : /* Print relocations. */
1945 : : static void
1946 : 82 : print_relocs (Ebl *ebl, GElf_Ehdr *ehdr)
1947 : : {
1948 : : /* Find all relocation sections and handle them. */
1949 : 82 : Elf_Scn *scn = NULL;
1950 : :
1951 [ + + ]: 2280 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
1952 : : {
1953 : : /* Handle the section if it is a symbol table. */
1954 : 2198 : GElf_Shdr shdr_mem;
1955 : 2198 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
1956 : :
1957 [ + - ]: 2198 : if (likely (shdr != NULL))
1958 : : {
1959 [ + + ]: 2198 : if (shdr->sh_type == SHT_REL)
1960 : 44 : handle_relocs_rel (ebl, ehdr, scn, shdr);
1961 [ + + ]: 2154 : else if (shdr->sh_type == SHT_RELA)
1962 : 198 : handle_relocs_rela (ebl, ehdr, scn, shdr);
1963 : : }
1964 : : }
1965 : 82 : }
1966 : :
1967 : :
1968 : : /* Handle a relocation section. */
1969 : : static void
1970 : 44 : handle_relocs_rel (Ebl *ebl, GElf_Ehdr *ehdr, Elf_Scn *scn, GElf_Shdr *shdr)
1971 : : {
1972 : 44 : int class = gelf_getclass (ebl->elf);
1973 : 44 : size_t sh_entsize = gelf_fsize (ebl->elf, ELF_T_REL, 1, EV_CURRENT);
1974 : 44 : int nentries = shdr->sh_size / sh_entsize;
1975 : :
1976 : : /* Get the data of the section. */
1977 : 44 : Elf_Data *data = elf_getdata (scn, NULL);
1978 [ + - ]: 44 : if (data == NULL)
1979 : 0 : return;
1980 : :
1981 : : /* Get the symbol table information. */
1982 : 44 : Elf_Scn *symscn = elf_getscn (ebl->elf, shdr->sh_link);
1983 : 44 : GElf_Shdr symshdr_mem;
1984 : 44 : GElf_Shdr *symshdr = gelf_getshdr (symscn, &symshdr_mem);
1985 : 44 : Elf_Data *symdata = elf_getdata (symscn, NULL);
1986 : :
1987 : : /* Get the section header of the section the relocations are for. */
1988 : 44 : GElf_Shdr destshdr_mem;
1989 : 44 : GElf_Shdr *destshdr = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_info),
1990 : : &destshdr_mem);
1991 : :
1992 [ + - - + ]: 44 : if (unlikely (symshdr == NULL || symdata == NULL || destshdr == NULL))
1993 : : {
1994 : 0 : printf (_("\nInvalid symbol table at offset %#0" PRIx64 "\n"),
1995 : : shdr->sh_offset);
1996 : 0 : return;
1997 : : }
1998 : :
1999 : : /* Search for the optional extended section index table. */
2000 : 44 : Elf_Data *xndxdata = NULL;
2001 : 44 : int xndxscnidx = elf_scnshndx (scn);
2002 [ - + ]: 44 : if (unlikely (xndxscnidx > 0))
2003 : 0 : xndxdata = elf_getdata (elf_getscn (ebl->elf, xndxscnidx), NULL);
2004 : :
2005 : : /* Get the section header string table index. */
2006 : 44 : size_t shstrndx;
2007 [ - + ]: 44 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
2008 : 0 : error (EXIT_FAILURE, 0,
2009 : 0 : _("cannot get section header string table index"));
2010 : :
2011 [ + + ]: 44 : if (shdr->sh_info != 0)
2012 : 80 : printf (ngettext ("\
2013 : : \nRelocation section [%2zu] '%s' for section [%2u] '%s' at offset %#0" PRIx64 " contains %d entry:\n",
2014 : : "\
2015 : : \nRelocation section [%2zu] '%s' for section [%2u] '%s' at offset %#0" PRIx64 " contains %d entries:\n",
2016 : : nentries),
2017 : : elf_ndxscn (scn),
2018 : 40 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
2019 : 40 : (unsigned int) shdr->sh_info,
2020 : 40 : elf_strptr (ebl->elf, shstrndx, destshdr->sh_name),
2021 : : shdr->sh_offset,
2022 : : nentries);
2023 : : else
2024 : : /* The .rel.dyn section does not refer to a specific section but
2025 : : instead of section index zero. Do not try to print a section
2026 : : name. */
2027 : 8 : printf (ngettext ("\
2028 : : \nRelocation section [%2u] '%s' at offset %#0" PRIx64 " contains %d entry:\n",
2029 : : "\
2030 : : \nRelocation section [%2u] '%s' at offset %#0" PRIx64 " contains %d entries:\n",
2031 : : nentries),
2032 : 4 : (unsigned int) elf_ndxscn (scn),
2033 : 4 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
2034 : : shdr->sh_offset,
2035 : : nentries);
2036 [ + - ]: 44 : fputs_unlocked (class == ELFCLASS32
2037 : 44 : ? _("\
2038 : : Offset Type Value Name\n")
2039 : 0 : : _("\
2040 : : Offset Type Value Name\n"),
2041 : : stdout);
2042 : :
2043 : 44 : int is_statically_linked = 0;
2044 [ + + ]: 12973 : for (int cnt = 0; cnt < nentries; ++cnt)
2045 : : {
2046 : 12929 : GElf_Rel relmem;
2047 : 12929 : GElf_Rel *rel = gelf_getrel (data, cnt, &relmem);
2048 [ + - ]: 12929 : if (likely (rel != NULL))
2049 : : {
2050 : 12929 : char buf[128];
2051 : 12929 : GElf_Sym symmem;
2052 : 12929 : Elf32_Word xndx;
2053 : 25858 : GElf_Sym *sym = gelf_getsymshndx (symdata, xndxdata,
2054 : 12929 : GELF_R_SYM (rel->r_info),
2055 : : &symmem, &xndx);
2056 [ - + ]: 12929 : if (unlikely (sym == NULL))
2057 : : {
2058 : : /* As a special case we have to handle relocations in static
2059 : : executables. This only happens for IRELATIVE relocations
2060 : : (so far). There is no symbol table. */
2061 [ # # ]: 0 : if (is_statically_linked == 0)
2062 : : {
2063 : : /* Find the program header and look for a PT_INTERP entry. */
2064 : 0 : is_statically_linked = -1;
2065 [ # # ]: 0 : if (ehdr->e_type == ET_EXEC)
2066 : : {
2067 : 0 : is_statically_linked = 1;
2068 : :
2069 [ # # ]: 0 : for (size_t inner = 0; inner < phnum; ++inner)
2070 : : {
2071 : 0 : GElf_Phdr phdr_mem;
2072 : 0 : GElf_Phdr *phdr = gelf_getphdr (ebl->elf, inner,
2073 : : &phdr_mem);
2074 [ # # # # ]: 0 : if (phdr != NULL && phdr->p_type == PT_INTERP)
2075 : : {
2076 : 0 : is_statically_linked = -1;
2077 : 0 : break;
2078 : : }
2079 : : }
2080 : : }
2081 : : }
2082 : :
2083 [ # # # # ]: 0 : if (is_statically_linked > 0 && shdr->sh_link == 0)
2084 [ # # # # ]: 0 : printf ("\
2085 : : %#0*" PRIx64 " %-20s %*s %s\n",
2086 : : class == ELFCLASS32 ? 10 : 18, rel->r_offset,
2087 : 0 : ebl_reloc_type_check (ebl, GELF_R_TYPE (rel->r_info))
2088 : : /* Avoid the leading R_ which isn't carrying any
2089 : : information. */
2090 : 0 : ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info),
2091 : : buf, sizeof (buf)) + 2
2092 : 0 : : _("<INVALID RELOC>"),
2093 : : class == ELFCLASS32 ? 10 : 18, "",
2094 : 0 : elf_strptr (ebl->elf, shstrndx, destshdr->sh_name));
2095 : : else
2096 [ # # # # ]: 0 : printf (" %#0*" PRIx64 " %-20s <%s %ld>\n",
2097 : : class == ELFCLASS32 ? 10 : 18, rel->r_offset,
2098 : 0 : ebl_reloc_type_check (ebl, GELF_R_TYPE (rel->r_info))
2099 : : /* Avoid the leading R_ which isn't carrying any
2100 : : information. */
2101 : 0 : ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info),
2102 : : buf, sizeof (buf)) + 2
2103 : 0 : : _("<INVALID RELOC>"),
2104 : : _("INVALID SYMBOL"),
2105 : 0 : (long int) GELF_R_SYM (rel->r_info));
2106 : : }
2107 [ + + ]: 12929 : else if (GELF_ST_TYPE (sym->st_info) != STT_SECTION)
2108 [ - + ]: 13051 : printf (" %#0*" PRIx64 " %-20s %#0*" PRIx64 " %s\n",
2109 : : class == ELFCLASS32 ? 10 : 18, rel->r_offset,
2110 [ + - ]: 61 : likely (ebl_reloc_type_check (ebl,
2111 : : GELF_R_TYPE (rel->r_info)))
2112 : : /* Avoid the leading R_ which isn't carrying any
2113 : : information. */
2114 : 61 : ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info),
2115 : : buf, sizeof (buf)) + 2
2116 : 0 : : _("<INVALID RELOC>"),
2117 : : class == ELFCLASS32 ? 10 : 18, sym->st_value,
2118 : 61 : elf_strptr (ebl->elf, symshdr->sh_link, sym->st_name));
2119 : : else
2120 : : {
2121 : : /* This is a relocation against a STT_SECTION symbol. */
2122 : 12868 : GElf_Shdr secshdr_mem;
2123 : 12868 : GElf_Shdr *secshdr;
2124 : 12868 : secshdr = gelf_getshdr (elf_getscn (ebl->elf,
2125 [ + - ]: 12868 : sym->st_shndx == SHN_XINDEX
2126 : 0 : ? xndx : sym->st_shndx),
2127 : : &secshdr_mem);
2128 : :
2129 [ - + ]: 12868 : if (unlikely (secshdr == NULL))
2130 [ # # # # ]: 0 : printf (" %#0*" PRIx64 " %-20s <%s %ld>\n",
2131 : : class == ELFCLASS32 ? 10 : 18, rel->r_offset,
2132 : 0 : ebl_reloc_type_check (ebl, GELF_R_TYPE (rel->r_info))
2133 : : /* Avoid the leading R_ which isn't carrying any
2134 : : information. */
2135 : 0 : ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info),
2136 : : buf, sizeof (buf)) + 2
2137 : 0 : : _("<INVALID RELOC>"),
2138 : : _("INVALID SECTION"),
2139 [ # # ]: 0 : (long int) (sym->st_shndx == SHN_XINDEX
2140 : 0 : ? xndx : sym->st_shndx));
2141 : : else
2142 [ - + + - ]: 51472 : printf (" %#0*" PRIx64 " %-20s %#0*" PRIx64 " %s\n",
2143 : : class == ELFCLASS32 ? 10 : 18, rel->r_offset,
2144 : 12868 : ebl_reloc_type_check (ebl, GELF_R_TYPE (rel->r_info))
2145 : : /* Avoid the leading R_ which isn't carrying any
2146 : : information. */
2147 : 12868 : ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info),
2148 : : buf, sizeof (buf)) + 2
2149 : 0 : : _("<INVALID RELOC>"),
2150 : : class == ELFCLASS32 ? 10 : 18, sym->st_value,
2151 : 12868 : elf_strptr (ebl->elf, shstrndx, secshdr->sh_name));
2152 : : }
2153 : : }
2154 : : }
2155 : : }
2156 : :
2157 : :
2158 : : /* Handle a relocation section. */
2159 : : static void
2160 : 198 : handle_relocs_rela (Ebl *ebl, GElf_Ehdr *ehdr, Elf_Scn *scn, GElf_Shdr *shdr)
2161 : : {
2162 : 198 : int class = gelf_getclass (ebl->elf);
2163 : 198 : size_t sh_entsize = gelf_fsize (ebl->elf, ELF_T_RELA, 1, EV_CURRENT);
2164 : 198 : int nentries = shdr->sh_size / sh_entsize;
2165 : :
2166 : : /* Get the data of the section. */
2167 : 198 : Elf_Data *data = elf_getdata (scn, NULL);
2168 [ + - ]: 198 : if (data == NULL)
2169 : 0 : return;
2170 : :
2171 : : /* Get the symbol table information. */
2172 : 198 : Elf_Scn *symscn = elf_getscn (ebl->elf, shdr->sh_link);
2173 : 198 : GElf_Shdr symshdr_mem;
2174 : 198 : GElf_Shdr *symshdr = gelf_getshdr (symscn, &symshdr_mem);
2175 : 198 : Elf_Data *symdata = elf_getdata (symscn, NULL);
2176 : :
2177 : : /* Get the section header of the section the relocations are for. */
2178 : 198 : GElf_Shdr destshdr_mem;
2179 : 198 : GElf_Shdr *destshdr = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_info),
2180 : : &destshdr_mem);
2181 : :
2182 [ + - - + ]: 198 : if (unlikely (symshdr == NULL || symdata == NULL || destshdr == NULL))
2183 : : {
2184 : 0 : printf (_("\nInvalid symbol table at offset %#0" PRIx64 "\n"),
2185 : : shdr->sh_offset);
2186 : 0 : return;
2187 : : }
2188 : :
2189 : : /* Search for the optional extended section index table. */
2190 : 198 : Elf_Data *xndxdata = NULL;
2191 : 198 : int xndxscnidx = elf_scnshndx (scn);
2192 [ - + ]: 198 : if (unlikely (xndxscnidx > 0))
2193 : 0 : xndxdata = elf_getdata (elf_getscn (ebl->elf, xndxscnidx), NULL);
2194 : :
2195 : : /* Get the section header string table index. */
2196 : 198 : size_t shstrndx;
2197 [ - + ]: 198 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
2198 : 0 : error (EXIT_FAILURE, 0,
2199 : 0 : _("cannot get section header string table index"));
2200 : :
2201 [ + + ]: 198 : if (shdr->sh_info != 0)
2202 : 334 : printf (ngettext ("\
2203 : : \nRelocation section [%2zu] '%s' for section [%2u] '%s' at offset %#0" PRIx64 " contains %d entry:\n",
2204 : : "\
2205 : : \nRelocation section [%2zu] '%s' for section [%2u] '%s' at offset %#0" PRIx64 " contains %d entries:\n",
2206 : : nentries),
2207 : : elf_ndxscn (scn),
2208 : 167 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
2209 : 167 : (unsigned int) shdr->sh_info,
2210 : 167 : elf_strptr (ebl->elf, shstrndx, destshdr->sh_name),
2211 : : shdr->sh_offset,
2212 : : nentries);
2213 : : else
2214 : : /* The .rela.dyn section does not refer to a specific section but
2215 : : instead of section index zero. Do not try to print a section
2216 : : name. */
2217 : 62 : printf (ngettext ("\
2218 : : \nRelocation section [%2u] '%s' at offset %#0" PRIx64 " contains %d entry:\n",
2219 : : "\
2220 : : \nRelocation section [%2u] '%s' at offset %#0" PRIx64 " contains %d entries:\n",
2221 : : nentries),
2222 : 31 : (unsigned int) elf_ndxscn (scn),
2223 : 31 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
2224 : : shdr->sh_offset,
2225 : : nentries);
2226 [ + + ]: 198 : fputs_unlocked (class == ELFCLASS32
2227 : 10 : ? _("\
2228 : : Offset Type Value Addend Name\n")
2229 : 188 : : _("\
2230 : : Offset Type Value Addend Name\n"),
2231 : : stdout);
2232 : :
2233 : 198 : int is_statically_linked = 0;
2234 [ + + ]: 66521 : for (int cnt = 0; cnt < nentries; ++cnt)
2235 : : {
2236 : 66323 : GElf_Rela relmem;
2237 : 66323 : GElf_Rela *rel = gelf_getrela (data, cnt, &relmem);
2238 [ + - ]: 66323 : if (likely (rel != NULL))
2239 : : {
2240 : 66323 : char buf[64];
2241 : 66323 : GElf_Sym symmem;
2242 : 66323 : Elf32_Word xndx;
2243 : 132646 : GElf_Sym *sym = gelf_getsymshndx (symdata, xndxdata,
2244 : 66323 : GELF_R_SYM (rel->r_info),
2245 : : &symmem, &xndx);
2246 : :
2247 [ - + ]: 66323 : if (unlikely (sym == NULL))
2248 : : {
2249 : : /* As a special case we have to handle relocations in static
2250 : : executables. This only happens for IRELATIVE relocations
2251 : : (so far). There is no symbol table. */
2252 [ # # ]: 0 : if (is_statically_linked == 0)
2253 : : {
2254 : : /* Find the program header and look for a PT_INTERP entry. */
2255 : 0 : is_statically_linked = -1;
2256 [ # # ]: 0 : if (ehdr->e_type == ET_EXEC)
2257 : : {
2258 : 0 : is_statically_linked = 1;
2259 : :
2260 [ # # ]: 0 : for (size_t inner = 0; inner < phnum; ++inner)
2261 : : {
2262 : 0 : GElf_Phdr phdr_mem;
2263 : 0 : GElf_Phdr *phdr = gelf_getphdr (ebl->elf, inner,
2264 : : &phdr_mem);
2265 [ # # # # ]: 0 : if (phdr != NULL && phdr->p_type == PT_INTERP)
2266 : : {
2267 : 0 : is_statically_linked = -1;
2268 : 0 : break;
2269 : : }
2270 : : }
2271 : : }
2272 : : }
2273 : :
2274 [ # # # # ]: 0 : if (is_statically_linked > 0 && shdr->sh_link == 0)
2275 [ # # # # ]: 0 : printf ("\
2276 : : %#0*" PRIx64 " %-15s %*s %#6" PRIx64 " %s\n",
2277 : : class == ELFCLASS32 ? 10 : 18, rel->r_offset,
2278 : 0 : ebl_reloc_type_check (ebl, GELF_R_TYPE (rel->r_info))
2279 : : /* Avoid the leading R_ which isn't carrying any
2280 : : information. */
2281 : 0 : ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info),
2282 : : buf, sizeof (buf)) + 2
2283 : 0 : : _("<INVALID RELOC>"),
2284 : : class == ELFCLASS32 ? 10 : 18, "",
2285 : : rel->r_addend,
2286 : 0 : elf_strptr (ebl->elf, shstrndx, destshdr->sh_name));
2287 : : else
2288 [ # # # # ]: 0 : printf (" %#0*" PRIx64 " %-15s <%s %ld>\n",
2289 : : class == ELFCLASS32 ? 10 : 18, rel->r_offset,
2290 : 0 : ebl_reloc_type_check (ebl, GELF_R_TYPE (rel->r_info))
2291 : : /* Avoid the leading R_ which isn't carrying any
2292 : : information. */
2293 : 0 : ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info),
2294 : : buf, sizeof (buf)) + 2
2295 : 0 : : _("<INVALID RELOC>"),
2296 : : _("INVALID SYMBOL"),
2297 : 0 : (long int) GELF_R_SYM (rel->r_info));
2298 : : }
2299 [ + + ]: 66323 : else if (GELF_ST_TYPE (sym->st_info) != STT_SECTION)
2300 [ + + ]: 161497 : printf ("\
2301 : : %#0*" PRIx64 " %-15s %#0*" PRIx64 " %+6" PRId64 " %s\n",
2302 : : class == ELFCLASS32 ? 10 : 18, rel->r_offset,
2303 [ + - ]: 47587 : likely (ebl_reloc_type_check (ebl,
2304 : : GELF_R_TYPE (rel->r_info)))
2305 : : /* Avoid the leading R_ which isn't carrying any
2306 : : information. */
2307 : 47587 : ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info),
2308 : : buf, sizeof (buf)) + 2
2309 : 0 : : _("<INVALID RELOC>"),
2310 : : class == ELFCLASS32 ? 10 : 18, sym->st_value,
2311 : : rel->r_addend,
2312 : 47587 : elf_strptr (ebl->elf, symshdr->sh_link, sym->st_name));
2313 : : else
2314 : : {
2315 : : /* This is a relocation against a STT_SECTION symbol. */
2316 : 18736 : GElf_Shdr secshdr_mem;
2317 : 18736 : GElf_Shdr *secshdr;
2318 : 18736 : secshdr = gelf_getshdr (elf_getscn (ebl->elf,
2319 [ + - ]: 18736 : sym->st_shndx == SHN_XINDEX
2320 : 0 : ? xndx : sym->st_shndx),
2321 : : &secshdr_mem);
2322 : :
2323 [ - + ]: 18736 : if (unlikely (secshdr == NULL))
2324 [ # # # # ]: 0 : printf (" %#0*" PRIx64 " %-15s <%s %ld>\n",
2325 : : class == ELFCLASS32 ? 10 : 18, rel->r_offset,
2326 : 0 : ebl_reloc_type_check (ebl, GELF_R_TYPE (rel->r_info))
2327 : : /* Avoid the leading R_ which isn't carrying any
2328 : : information. */
2329 : 0 : ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info),
2330 : : buf, sizeof (buf)) + 2
2331 : 0 : : _("<INVALID RELOC>"),
2332 : : _("INVALID SECTION"),
2333 [ # # ]: 0 : (long int) (sym->st_shndx == SHN_XINDEX
2334 : 0 : ? xndx : sym->st_shndx));
2335 : : else
2336 [ + + + - ]: 74944 : printf ("\
2337 : : %#0*" PRIx64 " %-15s %#0*" PRIx64 " %+6" PRId64 " %s\n",
2338 : : class == ELFCLASS32 ? 10 : 18, rel->r_offset,
2339 : 18736 : ebl_reloc_type_check (ebl, GELF_R_TYPE (rel->r_info))
2340 : : /* Avoid the leading R_ which isn't carrying any
2341 : : information. */
2342 : 18736 : ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info),
2343 : : buf, sizeof (buf)) + 2
2344 : 0 : : _("<INVALID RELOC>"),
2345 : : class == ELFCLASS32 ? 10 : 18, sym->st_value,
2346 : : rel->r_addend,
2347 : 18736 : elf_strptr (ebl->elf, shstrndx, secshdr->sh_name));
2348 : : }
2349 : : }
2350 : : }
2351 : : }
2352 : :
2353 : :
2354 : : /* Print the program header. */
2355 : : static void
2356 : 191 : print_symtab (Ebl *ebl, int type)
2357 : : {
2358 : : /* Find the symbol table(s). For this we have to search through the
2359 : : section table. */
2360 : 191 : Elf_Scn *scn = NULL;
2361 : :
2362 [ + + ]: 5312 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
2363 : : {
2364 : : /* Handle the section if it is a symbol table. */
2365 : 5121 : GElf_Shdr shdr_mem;
2366 : 5121 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
2367 : :
2368 [ + - + + ]: 5121 : if (shdr != NULL && shdr->sh_type == (GElf_Word) type)
2369 : : {
2370 [ + + ]: 107 : if (symbol_table_section != NULL)
2371 : : {
2372 : : /* Get the section header string table index. */
2373 : 4 : size_t shstrndx;
2374 : 4 : const char *sname;
2375 [ - + ]: 4 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
2376 : 0 : error (EXIT_FAILURE, 0,
2377 : 0 : _("cannot get section header string table index"));
2378 : 4 : sname = elf_strptr (ebl->elf, shstrndx, shdr->sh_name);
2379 [ + - + + ]: 4 : if (sname == NULL || strcmp (sname, symbol_table_section) != 0)
2380 : 2 : continue;
2381 : : }
2382 : :
2383 [ - + ]: 105 : if ((shdr->sh_flags & SHF_COMPRESSED) != 0)
2384 : : {
2385 [ # # ]: 0 : if (elf_compress (scn, 0, 0) < 0)
2386 : 0 : printf ("WARNING: %s [%zd]\n",
2387 : : _("Couldn't uncompress section"),
2388 : : elf_ndxscn (scn));
2389 : 0 : shdr = gelf_getshdr (scn, &shdr_mem);
2390 [ # # ]: 0 : if (unlikely (shdr == NULL))
2391 : 0 : error (EXIT_FAILURE, 0,
2392 : 0 : _("cannot get section [%zd] header: %s"),
2393 : : elf_ndxscn (scn), elf_errmsg (-1));
2394 : : }
2395 : 105 : handle_symtab (ebl, scn, shdr);
2396 : : }
2397 : : }
2398 : 191 : }
2399 : :
2400 : :
2401 : : static void
2402 : 105 : handle_symtab (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr)
2403 : : {
2404 : 105 : Elf_Data *versym_data = NULL;
2405 : 105 : Elf_Data *verneed_data = NULL;
2406 : 105 : Elf_Data *verdef_data = NULL;
2407 : 105 : Elf_Data *xndx_data = NULL;
2408 : 105 : int class = gelf_getclass (ebl->elf);
2409 : 105 : Elf32_Word verneed_stridx = 0;
2410 : 105 : Elf32_Word verdef_stridx = 0;
2411 : :
2412 : : /* Get the data of the section. */
2413 : 105 : Elf_Data *data = elf_getdata (scn, NULL);
2414 [ - + ]: 105 : if (data == NULL)
2415 : 0 : return;
2416 : :
2417 : : /* Find out whether we have other sections we might need. */
2418 : : Elf_Scn *runscn = NULL;
2419 [ + + ]: 3034 : while ((runscn = elf_nextscn (ebl->elf, runscn)) != NULL)
2420 : : {
2421 : 2929 : GElf_Shdr runshdr_mem;
2422 : 2929 : GElf_Shdr *runshdr = gelf_getshdr (runscn, &runshdr_mem);
2423 : :
2424 [ + - ]: 2929 : if (likely (runshdr != NULL))
2425 : : {
2426 [ + + ]: 2929 : if (runshdr->sh_type == SHT_GNU_versym
2427 [ + + ]: 54 : && runshdr->sh_link == elf_ndxscn (scn))
2428 : : /* Bingo, found the version information. Now get the data. */
2429 : 43 : versym_data = elf_getdata (runscn, NULL);
2430 [ + + ]: 2886 : else if (runshdr->sh_type == SHT_GNU_verneed)
2431 : : {
2432 : : /* This is the information about the needed versions. */
2433 : 54 : verneed_data = elf_getdata (runscn, NULL);
2434 : 54 : verneed_stridx = runshdr->sh_link;
2435 : : }
2436 [ + + ]: 2832 : else if (runshdr->sh_type == SHT_GNU_verdef)
2437 : : {
2438 : : /* This is the information about the defined versions. */
2439 : 4 : verdef_data = elf_getdata (runscn, NULL);
2440 : 4 : verdef_stridx = runshdr->sh_link;
2441 : : }
2442 [ - + ]: 2828 : else if (runshdr->sh_type == SHT_SYMTAB_SHNDX
2443 [ # # ]: 0 : && runshdr->sh_link == elf_ndxscn (scn))
2444 : : /* Extended section index. */
2445 : 0 : xndx_data = elf_getdata (runscn, NULL);
2446 : : }
2447 : : }
2448 : :
2449 : : /* Get the section header string table index. */
2450 : 105 : size_t shstrndx;
2451 [ - + ]: 105 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
2452 : 0 : error (EXIT_FAILURE, 0,
2453 : 0 : _("cannot get section header string table index"));
2454 : :
2455 : 105 : GElf_Shdr glink_mem;
2456 : 105 : GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
2457 : : &glink_mem);
2458 [ - + ]: 105 : if (glink == NULL)
2459 : 0 : error (EXIT_FAILURE, 0, _("invalid sh_link value in section %zu"),
2460 : : elf_ndxscn (scn));
2461 : :
2462 : : /* Now we can compute the number of entries in the section. */
2463 : 210 : unsigned int nsyms = data->d_size / (class == ELFCLASS32
2464 : : ? sizeof (Elf32_Sym)
2465 [ + + ]: 105 : : sizeof (Elf64_Sym));
2466 : :
2467 : 210 : printf (ngettext ("\nSymbol table [%2u] '%s' contains %u entry:\n",
2468 : : "\nSymbol table [%2u] '%s' contains %u entries:\n",
2469 : : nsyms),
2470 : 105 : (unsigned int) elf_ndxscn (scn),
2471 : 105 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name), nsyms);
2472 : 210 : printf (ngettext (" %lu local symbol String table: [%2u] '%s'\n",
2473 : : " %lu local symbols String table: [%2u] '%s'\n",
2474 : : shdr->sh_info),
2475 : 105 : (unsigned long int) shdr->sh_info,
2476 : 105 : (unsigned int) shdr->sh_link,
2477 : 105 : elf_strptr (ebl->elf, shstrndx, glink->sh_name));
2478 : :
2479 [ + + ]: 105 : fputs_unlocked (class == ELFCLASS32
2480 : 16 : ? _("\
2481 : : Num: Value Size Type Bind Vis Ndx Name\n")
2482 : 89 : : _("\
2483 : : Num: Value Size Type Bind Vis Ndx Name\n"),
2484 : : stdout);
2485 : :
2486 [ + + ]: 40244 : for (unsigned int cnt = 0; cnt < nsyms; ++cnt)
2487 : : {
2488 : 40139 : char typebuf[64];
2489 : 40139 : char bindbuf[64];
2490 : 40139 : char scnbuf[64];
2491 : 40139 : Elf32_Word xndx;
2492 : 40139 : GElf_Sym sym_mem;
2493 : 40139 : GElf_Sym *sym = gelf_getsymshndx (data, xndx_data, cnt, &sym_mem, &xndx);
2494 : :
2495 [ - + ]: 40139 : if (unlikely (sym == NULL))
2496 : 0 : continue;
2497 : :
2498 : : /* Determine the real section index. */
2499 [ + - ]: 40139 : if (likely (sym->st_shndx != SHN_XINDEX))
2500 : 40139 : xndx = sym->st_shndx;
2501 : :
2502 [ + + ]: 80278 : printf (_("\
2503 : : %5u: %0*" PRIx64 " %6" PRId64 " %-7s %-6s %-9s %6s %s"),
2504 : : cnt,
2505 : : class == ELFCLASS32 ? 8 : 16,
2506 : : sym->st_value,
2507 : : sym->st_size,
2508 : 40139 : ebl_symbol_type_name (ebl, GELF_ST_TYPE (sym->st_info),
2509 : : typebuf, sizeof (typebuf)),
2510 : 40139 : ebl_symbol_binding_name (ebl, GELF_ST_BIND (sym->st_info),
2511 : : bindbuf, sizeof (bindbuf)),
2512 : 40139 : get_visibility_type (GELF_ST_VISIBILITY (sym->st_other)),
2513 : 40139 : ebl_section_name (ebl, sym->st_shndx, xndx, scnbuf,
2514 : : sizeof (scnbuf), NULL, shnum),
2515 : 40139 : elf_strptr (ebl->elf, shdr->sh_link, sym->st_name));
2516 : :
2517 [ + + ]: 40139 : if (versym_data != NULL)
2518 : : {
2519 : : /* Get the version information. */
2520 : 1420 : GElf_Versym versym_mem;
2521 : 1420 : GElf_Versym *versym = gelf_getversym (versym_data, cnt, &versym_mem);
2522 : :
2523 [ + - + + ]: 1420 : if (versym != NULL && ((*versym & 0x8000) != 0 || *versym > 1))
2524 : : {
2525 : 1083 : bool is_nobits = false;
2526 : 1083 : bool check_def = xndx != SHN_UNDEF;
2527 : :
2528 [ + + - + ]: 1083 : if (xndx < SHN_LORESERVE || sym->st_shndx == SHN_XINDEX)
2529 : : {
2530 : 1073 : GElf_Shdr symshdr_mem;
2531 : 1073 : GElf_Shdr *symshdr =
2532 : 1073 : gelf_getshdr (elf_getscn (ebl->elf, xndx), &symshdr_mem);
2533 : :
2534 : 1073 : is_nobits = (symshdr != NULL
2535 [ + - + - ]: 1073 : && symshdr->sh_type == SHT_NOBITS);
2536 : : }
2537 : :
2538 [ + + ]: 1083 : if (is_nobits || ! check_def)
2539 : : {
2540 : : /* We must test both. */
2541 : 931 : GElf_Vernaux vernaux_mem;
2542 : 931 : GElf_Vernaux *vernaux = NULL;
2543 : 931 : size_t vn_offset = 0;
2544 : :
2545 : 931 : GElf_Verneed verneed_mem;
2546 : 931 : GElf_Verneed *verneed = gelf_getverneed (verneed_data, 0,
2547 : : &verneed_mem);
2548 [ + - ]: 3442 : while (verneed != NULL)
2549 : : {
2550 : 3442 : size_t vna_offset = vn_offset;
2551 : :
2552 : 3442 : vernaux = gelf_getvernaux (verneed_data,
2553 : 3442 : vna_offset += verneed->vn_aux,
2554 : : &vernaux_mem);
2555 [ + - ]: 3490 : while (vernaux != NULL
2556 [ + + ]: 3490 : && vernaux->vna_other != *versym
2557 [ + + ]: 2559 : && vernaux->vna_next != 0
2558 : 48 : && (verneed_data->d_size - vna_offset
2559 [ + - ]: 48 : >= vernaux->vna_next))
2560 : : {
2561 : : /* Update the offset. */
2562 : 48 : vna_offset += vernaux->vna_next;
2563 : :
2564 : 48 : vernaux = (vernaux->vna_next == 0
2565 : : ? NULL
2566 : 48 : : gelf_getvernaux (verneed_data,
2567 : : vna_offset,
2568 : : &vernaux_mem));
2569 : : }
2570 : :
2571 : : /* Check whether we found the version. */
2572 [ + - + + ]: 3442 : if (vernaux != NULL && vernaux->vna_other == *versym)
2573 : : /* Found it. */
2574 : : break;
2575 : :
2576 [ + - ]: 2511 : if (verneed_data->d_size - vn_offset < verneed->vn_next)
2577 : : break;
2578 : :
2579 : 2511 : vn_offset += verneed->vn_next;
2580 : 2511 : verneed = (verneed->vn_next == 0
2581 : : ? NULL
2582 [ + - ]: 2511 : : gelf_getverneed (verneed_data, vn_offset,
2583 : : &verneed_mem));
2584 : : }
2585 : :
2586 [ + - + - ]: 931 : if (vernaux != NULL && vernaux->vna_other == *versym)
2587 : : {
2588 : 2793 : printf ("@%s (%u)",
2589 : : elf_strptr (ebl->elf, verneed_stridx,
2590 : 931 : vernaux->vna_name),
2591 : 931 : (unsigned int) vernaux->vna_other);
2592 : 931 : check_def = 0;
2593 : : }
2594 [ # # ]: 0 : else if (unlikely (! is_nobits))
2595 : 0 : error (0, 0, _("bad dynamic symbol"));
2596 : : else
2597 : : check_def = 1;
2598 : : }
2599 : :
2600 [ + - + - ]: 1083 : if (check_def && *versym != 0x8001)
2601 : : {
2602 : : /* We must test both. */
2603 : 152 : size_t vd_offset = 0;
2604 : :
2605 : 152 : GElf_Verdef verdef_mem;
2606 : 152 : GElf_Verdef *verdef = gelf_getverdef (verdef_data, 0,
2607 : : &verdef_mem);
2608 [ + - ]: 433 : while (verdef != NULL)
2609 : : {
2610 [ + + ]: 433 : if (verdef->vd_ndx == (*versym & 0x7fff))
2611 : : /* Found the definition. */
2612 : : break;
2613 : :
2614 [ + - ]: 281 : if (verdef_data->d_size - vd_offset < verdef->vd_next)
2615 : : break;
2616 : :
2617 : 281 : vd_offset += verdef->vd_next;
2618 : 281 : verdef = (verdef->vd_next == 0
2619 : : ? NULL
2620 [ + - ]: 281 : : gelf_getverdef (verdef_data, vd_offset,
2621 : : &verdef_mem));
2622 : : }
2623 : :
2624 [ + - ]: 152 : if (verdef != NULL)
2625 : : {
2626 : 152 : GElf_Verdaux verdaux_mem;
2627 : 152 : GElf_Verdaux *verdaux
2628 : 304 : = gelf_getverdaux (verdef_data,
2629 : 152 : vd_offset + verdef->vd_aux,
2630 : : &verdaux_mem);
2631 : :
2632 [ + - ]: 152 : if (verdaux != NULL)
2633 [ + - ]: 304 : printf ((*versym & 0x8000) ? "@%s" : "@@%s",
2634 : : elf_strptr (ebl->elf, verdef_stridx,
2635 : 152 : verdaux->vda_name));
2636 : : }
2637 : : }
2638 : : }
2639 : : }
2640 : :
2641 [ + + ]: 80278 : putchar_unlocked ('\n');
2642 : : }
2643 : : }
2644 : :
2645 : :
2646 : : /* Print version information. */
2647 : : static void
2648 : 81 : print_verinfo (Ebl *ebl)
2649 : : {
2650 : : /* Find the version information sections. For this we have to
2651 : : search through the section table. */
2652 : 81 : Elf_Scn *scn = NULL;
2653 : :
2654 [ + + ]: 2245 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
2655 : : {
2656 : : /* Handle the section if it is part of the versioning handling. */
2657 : 2164 : GElf_Shdr shdr_mem;
2658 : 2164 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
2659 : :
2660 [ + - ]: 2164 : if (likely (shdr != NULL))
2661 : : {
2662 [ + + ]: 2164 : if (shdr->sh_type == SHT_GNU_verneed)
2663 : 35 : handle_verneed (ebl, scn, shdr);
2664 [ + + ]: 2129 : else if (shdr->sh_type == SHT_GNU_verdef)
2665 : 2 : handle_verdef (ebl, scn, shdr);
2666 [ + + ]: 2127 : else if (shdr->sh_type == SHT_GNU_versym)
2667 : 35 : handle_versym (ebl, scn, shdr);
2668 : : }
2669 : : }
2670 : 81 : }
2671 : :
2672 : :
2673 : : static const char *
2674 : 73 : get_ver_flags (unsigned int flags)
2675 : : {
2676 : 73 : static char buf[32];
2677 : 73 : char *endp;
2678 : :
2679 [ + + ]: 73 : if (flags == 0)
2680 : 71 : return _("none");
2681 : :
2682 [ + - ]: 2 : if (flags & VER_FLG_BASE)
2683 : 2 : endp = stpcpy (buf, "BASE ");
2684 : : else
2685 : : endp = buf;
2686 : :
2687 [ - + ]: 2 : if (flags & VER_FLG_WEAK)
2688 : : {
2689 [ # # ]: 0 : if (endp != buf)
2690 : 0 : endp = stpcpy (endp, "| ");
2691 : :
2692 : 0 : endp = stpcpy (endp, "WEAK ");
2693 : : }
2694 : :
2695 [ - + ]: 2 : if (unlikely (flags & ~(VER_FLG_BASE | VER_FLG_WEAK)))
2696 : : {
2697 : 0 : strncpy (endp, _("| <unknown>"), buf + sizeof (buf) - endp);
2698 : 0 : buf[sizeof (buf) - 1] = '\0';
2699 : : }
2700 : :
2701 : : return buf;
2702 : : }
2703 : :
2704 : :
2705 : : static void
2706 : 35 : handle_verneed (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr)
2707 : : {
2708 : 35 : int class = gelf_getclass (ebl->elf);
2709 : :
2710 : : /* Get the data of the section. */
2711 : 35 : Elf_Data *data = elf_getdata (scn, NULL);
2712 [ - + ]: 35 : if (data == NULL)
2713 : 0 : return;
2714 : :
2715 : : /* Get the section header string table index. */
2716 : 35 : size_t shstrndx;
2717 [ - + ]: 35 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
2718 : 0 : error (EXIT_FAILURE, 0,
2719 : 0 : _("cannot get section header string table index"));
2720 : :
2721 : 35 : GElf_Shdr glink_mem;
2722 : 35 : GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
2723 : : &glink_mem);
2724 [ - + ]: 35 : if (glink == NULL)
2725 : 0 : error (EXIT_FAILURE, 0, _("invalid sh_link value in section %zu"),
2726 : : elf_ndxscn (scn));
2727 : :
2728 [ + + ]: 105 : printf (ngettext ("\
2729 : : \nVersion needs section [%2u] '%s' contains %d entry:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'\n",
2730 : : "\
2731 : : \nVersion needs section [%2u] '%s' contains %d entries:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'\n",
2732 : : shdr->sh_info),
2733 : 35 : (unsigned int) elf_ndxscn (scn),
2734 : 35 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name), shdr->sh_info,
2735 : : class == ELFCLASS32 ? 10 : 18, shdr->sh_addr,
2736 : : shdr->sh_offset,
2737 : 35 : (unsigned int) shdr->sh_link,
2738 : 35 : elf_strptr (ebl->elf, shstrndx, glink->sh_name));
2739 : :
2740 : 35 : unsigned int offset = 0;
2741 [ + - ]: 60 : for (int cnt = shdr->sh_info; --cnt >= 0; )
2742 : : {
2743 : : /* Get the data at the next offset. */
2744 : 60 : GElf_Verneed needmem;
2745 : 60 : GElf_Verneed *need = gelf_getverneed (data, offset, &needmem);
2746 [ + - ]: 60 : if (unlikely (need == NULL))
2747 : : break;
2748 : :
2749 : 180 : printf (_(" %#06x: Version: %hu File: %s Cnt: %hu\n"),
2750 : 60 : offset, (unsigned short int) need->vn_version,
2751 : 60 : elf_strptr (ebl->elf, shdr->sh_link, need->vn_file),
2752 : 60 : (unsigned short int) need->vn_cnt);
2753 : :
2754 : 60 : unsigned int auxoffset = offset + need->vn_aux;
2755 [ + - ]: 61 : for (int cnt2 = need->vn_cnt; --cnt2 >= 0; )
2756 : : {
2757 : 61 : GElf_Vernaux auxmem;
2758 : 61 : GElf_Vernaux *aux = gelf_getvernaux (data, auxoffset, &auxmem);
2759 [ + - ]: 61 : if (unlikely (aux == NULL))
2760 : : break;
2761 : :
2762 : 244 : printf (_(" %#06x: Name: %s Flags: %s Version: %hu\n"),
2763 : : auxoffset,
2764 : 61 : elf_strptr (ebl->elf, shdr->sh_link, aux->vna_name),
2765 : 61 : get_ver_flags (aux->vna_flags),
2766 : 61 : (unsigned short int) aux->vna_other);
2767 : :
2768 [ + + ]: 61 : if (aux->vna_next == 0)
2769 : : break;
2770 : :
2771 : 1 : auxoffset += aux->vna_next;
2772 : : }
2773 : :
2774 : : /* Find the next offset. */
2775 [ + + ]: 60 : if (need->vn_next == 0)
2776 : : break;
2777 : :
2778 : 25 : offset += need->vn_next;
2779 : : }
2780 : : }
2781 : :
2782 : :
2783 : : static void
2784 : 2 : handle_verdef (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr)
2785 : : {
2786 : : /* Get the data of the section. */
2787 : 2 : Elf_Data *data = elf_getdata (scn, NULL);
2788 [ - + ]: 2 : if (data == NULL)
2789 : 0 : return;
2790 : :
2791 : : /* Get the section header string table index. */
2792 : 2 : size_t shstrndx;
2793 [ - + ]: 2 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
2794 : 0 : error (EXIT_FAILURE, 0,
2795 : 0 : _("cannot get section header string table index"));
2796 : :
2797 : 2 : GElf_Shdr glink_mem;
2798 : 2 : GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
2799 : : &glink_mem);
2800 [ - + ]: 2 : if (glink == NULL)
2801 : 0 : error (EXIT_FAILURE, 0, _("invalid sh_link value in section %zu"),
2802 : : elf_ndxscn (scn));
2803 : :
2804 : 2 : int class = gelf_getclass (ebl->elf);
2805 [ + - ]: 6 : printf (ngettext ("\
2806 : : \nVersion definition section [%2u] '%s' contains %d entry:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'\n",
2807 : : "\
2808 : : \nVersion definition section [%2u] '%s' contains %d entries:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'\n",
2809 : : shdr->sh_info),
2810 : 2 : (unsigned int) elf_ndxscn (scn),
2811 : 2 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
2812 : : shdr->sh_info,
2813 : : class == ELFCLASS32 ? 10 : 18, shdr->sh_addr,
2814 : : shdr->sh_offset,
2815 : 2 : (unsigned int) shdr->sh_link,
2816 : 2 : elf_strptr (ebl->elf, shstrndx, glink->sh_name));
2817 : :
2818 : 2 : unsigned int offset = 0;
2819 [ + - ]: 12 : for (int cnt = shdr->sh_info; --cnt >= 0; )
2820 : : {
2821 : : /* Get the data at the next offset. */
2822 : 12 : GElf_Verdef defmem;
2823 : 12 : GElf_Verdef *def = gelf_getverdef (data, offset, &defmem);
2824 [ + - ]: 12 : if (unlikely (def == NULL))
2825 : : break;
2826 : :
2827 : 12 : unsigned int auxoffset = offset + def->vd_aux;
2828 : 12 : GElf_Verdaux auxmem;
2829 : 12 : GElf_Verdaux *aux = gelf_getverdaux (data, auxoffset, &auxmem);
2830 [ + - ]: 12 : if (unlikely (aux == NULL))
2831 : : break;
2832 : :
2833 : 12 : printf (_("\
2834 : : %#06x: Version: %hd Flags: %s Index: %hd Cnt: %hd Name: %s\n"),
2835 : 12 : offset, def->vd_version,
2836 : 12 : get_ver_flags (def->vd_flags),
2837 : 12 : def->vd_ndx,
2838 : 12 : def->vd_cnt,
2839 : 12 : elf_strptr (ebl->elf, shdr->sh_link, aux->vda_name));
2840 : :
2841 : 12 : auxoffset += aux->vda_next;
2842 [ + + ]: 12 : for (int cnt2 = 1; cnt2 < def->vd_cnt; ++cnt2)
2843 : : {
2844 : 8 : aux = gelf_getverdaux (data, auxoffset, &auxmem);
2845 [ + - ]: 8 : if (unlikely (aux == NULL))
2846 : : break;
2847 : :
2848 : 8 : printf (_(" %#06x: Parent %d: %s\n"),
2849 : : auxoffset, cnt2,
2850 : 8 : elf_strptr (ebl->elf, shdr->sh_link, aux->vda_name));
2851 : :
2852 [ - + ]: 8 : if (aux->vda_next == 0)
2853 : : break;
2854 : :
2855 : 0 : auxoffset += aux->vda_next;
2856 : : }
2857 : :
2858 : : /* Find the next offset. */
2859 [ + + ]: 12 : if (def->vd_next == 0)
2860 : : break;
2861 : 10 : offset += def->vd_next;
2862 : : }
2863 : : }
2864 : :
2865 : :
2866 : : static void
2867 : 35 : handle_versym (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr)
2868 : : {
2869 : 35 : int class = gelf_getclass (ebl->elf);
2870 : 35 : const char **vername;
2871 : 35 : const char **filename;
2872 : :
2873 : : /* Get the data of the section. */
2874 : 35 : Elf_Data *data = elf_getdata (scn, NULL);
2875 [ + - ]: 35 : if (data == NULL)
2876 : 0 : return;
2877 : :
2878 : : /* Get the section header string table index. */
2879 : 35 : size_t shstrndx;
2880 [ - + ]: 35 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
2881 : 0 : error (EXIT_FAILURE, 0,
2882 : 0 : _("cannot get section header string table index"));
2883 : :
2884 : : /* We have to find the version definition section and extract the
2885 : : version names. */
2886 : : Elf_Scn *defscn = NULL;
2887 : : Elf_Scn *needscn = NULL;
2888 : :
2889 : : Elf_Scn *verscn = NULL;
2890 [ + + ]: 1074 : while ((verscn = elf_nextscn (ebl->elf, verscn)) != NULL)
2891 : : {
2892 : 1039 : GElf_Shdr vershdr_mem;
2893 : 1039 : GElf_Shdr *vershdr = gelf_getshdr (verscn, &vershdr_mem);
2894 : :
2895 [ + - ]: 1039 : if (likely (vershdr != NULL))
2896 : : {
2897 [ + + ]: 1039 : if (vershdr->sh_type == SHT_GNU_verdef)
2898 : : defscn = verscn;
2899 [ + + ]: 1037 : else if (vershdr->sh_type == SHT_GNU_verneed)
2900 : 35 : needscn = verscn;
2901 : : }
2902 : : }
2903 : :
2904 : 35 : size_t nvername;
2905 [ + - ]: 35 : if (defscn != NULL || needscn != NULL)
2906 : : {
2907 : : /* We have a version information (better should have). Now get
2908 : : the version names. First find the maximum version number. */
2909 : 35 : nvername = 0;
2910 [ + + ]: 35 : if (defscn != NULL)
2911 : : {
2912 : : /* Run through the version definitions and find the highest
2913 : : index. */
2914 : 2 : unsigned int offset = 0;
2915 : 2 : Elf_Data *defdata;
2916 : 2 : GElf_Shdr defshdrmem;
2917 : 2 : GElf_Shdr *defshdr;
2918 : :
2919 : 2 : defdata = elf_getdata (defscn, NULL);
2920 [ + - ]: 2 : if (unlikely (defdata == NULL))
2921 : 0 : return;
2922 : :
2923 : 2 : defshdr = gelf_getshdr (defscn, &defshdrmem);
2924 [ + - ]: 2 : if (unlikely (defshdr == NULL))
2925 : : return;
2926 : :
2927 [ + - ]: 12 : for (unsigned int cnt = 0; cnt < defshdr->sh_info; ++cnt)
2928 : : {
2929 : 12 : GElf_Verdef defmem;
2930 : 12 : GElf_Verdef *def;
2931 : :
2932 : : /* Get the data at the next offset. */
2933 : 12 : def = gelf_getverdef (defdata, offset, &defmem);
2934 [ + - ]: 12 : if (unlikely (def == NULL))
2935 : : break;
2936 : :
2937 : 12 : nvername = MAX (nvername, (size_t) (def->vd_ndx & 0x7fff));
2938 : :
2939 [ + + ]: 12 : if (def->vd_next == 0)
2940 : : break;
2941 : 10 : offset += def->vd_next;
2942 : : }
2943 : : }
2944 [ + - ]: 35 : if (needscn != NULL)
2945 : : {
2946 : 35 : unsigned int offset = 0;
2947 : 35 : Elf_Data *needdata;
2948 : 35 : GElf_Shdr needshdrmem;
2949 : 35 : GElf_Shdr *needshdr;
2950 : :
2951 : 35 : needdata = elf_getdata (needscn, NULL);
2952 [ + - ]: 35 : if (unlikely (needdata == NULL))
2953 : 0 : return;
2954 : :
2955 : 35 : needshdr = gelf_getshdr (needscn, &needshdrmem);
2956 [ + - ]: 35 : if (unlikely (needshdr == NULL))
2957 : : return;
2958 : :
2959 [ + - ]: 60 : for (unsigned int cnt = 0; cnt < needshdr->sh_info; ++cnt)
2960 : : {
2961 : 60 : GElf_Verneed needmem;
2962 : 60 : GElf_Verneed *need;
2963 : 60 : unsigned int auxoffset;
2964 : 60 : int cnt2;
2965 : :
2966 : : /* Get the data at the next offset. */
2967 : 60 : need = gelf_getverneed (needdata, offset, &needmem);
2968 [ + - ]: 60 : if (unlikely (need == NULL))
2969 : : break;
2970 : :
2971 : : /* Run through the auxiliary entries. */
2972 : 60 : auxoffset = offset + need->vn_aux;
2973 [ + - ]: 61 : for (cnt2 = need->vn_cnt; --cnt2 >= 0; )
2974 : : {
2975 : 61 : GElf_Vernaux auxmem;
2976 : 61 : GElf_Vernaux *aux;
2977 : :
2978 : 61 : aux = gelf_getvernaux (needdata, auxoffset, &auxmem);
2979 [ + - ]: 61 : if (unlikely (aux == NULL))
2980 : : break;
2981 : :
2982 : 61 : nvername = MAX (nvername,
2983 : : (size_t) (aux->vna_other & 0x7fff));
2984 : :
2985 [ + + ]: 61 : if (aux->vna_next == 0)
2986 : : break;
2987 : 1 : auxoffset += aux->vna_next;
2988 : : }
2989 : :
2990 [ + + ]: 60 : if (need->vn_next == 0)
2991 : : break;
2992 : 25 : offset += need->vn_next;
2993 : : }
2994 : : }
2995 : :
2996 : : /* This is the number of versions we know about. */
2997 : 35 : ++nvername;
2998 : :
2999 : : /* Allocate the array. */
3000 : 35 : vername = (const char **) alloca (nvername * sizeof (const char *));
3001 [ + + ]: 35 : memset(vername, 0, nvername * sizeof (const char *));
3002 : 35 : filename = (const char **) alloca (nvername * sizeof (const char *));
3003 : 35 : memset(filename, 0, nvername * sizeof (const char *));
3004 : :
3005 : : /* Run through the data structures again and collect the strings. */
3006 [ + + ]: 35 : if (defscn != NULL)
3007 : : {
3008 : : /* Run through the version definitions and find the highest
3009 : : index. */
3010 : 2 : unsigned int offset = 0;
3011 : 2 : Elf_Data *defdata;
3012 : 2 : GElf_Shdr defshdrmem;
3013 : 2 : GElf_Shdr *defshdr;
3014 : :
3015 : 2 : defdata = elf_getdata (defscn, NULL);
3016 [ + - ]: 2 : if (unlikely (defdata == NULL))
3017 : 0 : return;
3018 : :
3019 : 2 : defshdr = gelf_getshdr (defscn, &defshdrmem);
3020 [ + - ]: 2 : if (unlikely (defshdr == NULL))
3021 : : return;
3022 : :
3023 [ + - ]: 12 : for (unsigned int cnt = 0; cnt < defshdr->sh_info; ++cnt)
3024 : : {
3025 : :
3026 : : /* Get the data at the next offset. */
3027 : 12 : GElf_Verdef defmem;
3028 : 12 : GElf_Verdef *def = gelf_getverdef (defdata, offset, &defmem);
3029 [ + - ]: 12 : if (unlikely (def == NULL))
3030 : : break;
3031 : :
3032 : 12 : GElf_Verdaux auxmem;
3033 : 24 : GElf_Verdaux *aux = gelf_getverdaux (defdata,
3034 : 12 : offset + def->vd_aux,
3035 : : &auxmem);
3036 [ + - ]: 12 : if (unlikely (aux == NULL))
3037 : : break;
3038 : :
3039 : 12 : vername[def->vd_ndx & 0x7fff]
3040 : 12 : = elf_strptr (ebl->elf, defshdr->sh_link, aux->vda_name);
3041 : 12 : filename[def->vd_ndx & 0x7fff] = NULL;
3042 : :
3043 [ + + ]: 12 : if (def->vd_next == 0)
3044 : : break;
3045 : 10 : offset += def->vd_next;
3046 : : }
3047 : : }
3048 [ + - ]: 35 : if (needscn != NULL)
3049 : : {
3050 : 35 : unsigned int offset = 0;
3051 : :
3052 : 35 : Elf_Data *needdata = elf_getdata (needscn, NULL);
3053 : 35 : GElf_Shdr needshdrmem;
3054 : 35 : GElf_Shdr *needshdr = gelf_getshdr (needscn, &needshdrmem);
3055 [ - + ]: 35 : if (unlikely (needdata == NULL || needshdr == NULL))
3056 : 0 : return;
3057 : :
3058 [ + - ]: 60 : for (unsigned int cnt = 0; cnt < needshdr->sh_info; ++cnt)
3059 : : {
3060 : : /* Get the data at the next offset. */
3061 : 60 : GElf_Verneed needmem;
3062 : 60 : GElf_Verneed *need = gelf_getverneed (needdata, offset,
3063 : : &needmem);
3064 [ + - ]: 60 : if (unlikely (need == NULL))
3065 : : break;
3066 : :
3067 : : /* Run through the auxiliary entries. */
3068 : 60 : unsigned int auxoffset = offset + need->vn_aux;
3069 [ + - ]: 61 : for (int cnt2 = need->vn_cnt; --cnt2 >= 0; )
3070 : : {
3071 : 61 : GElf_Vernaux auxmem;
3072 : 61 : GElf_Vernaux *aux = gelf_getvernaux (needdata, auxoffset,
3073 : : &auxmem);
3074 [ + - ]: 61 : if (unlikely (aux == NULL))
3075 : : break;
3076 : :
3077 : 61 : vername[aux->vna_other & 0x7fff]
3078 : 61 : = elf_strptr (ebl->elf, needshdr->sh_link, aux->vna_name);
3079 : 61 : filename[aux->vna_other & 0x7fff]
3080 : 61 : = elf_strptr (ebl->elf, needshdr->sh_link, need->vn_file);
3081 : :
3082 [ + + ]: 61 : if (aux->vna_next == 0)
3083 : : break;
3084 : 1 : auxoffset += aux->vna_next;
3085 : : }
3086 : :
3087 [ + + ]: 60 : if (need->vn_next == 0)
3088 : : break;
3089 : 25 : offset += need->vn_next;
3090 : : }
3091 : : }
3092 : : }
3093 : : else
3094 : : {
3095 : : vername = NULL;
3096 : : nvername = 1;
3097 : : filename = NULL;
3098 : : }
3099 : :
3100 : 35 : GElf_Shdr glink_mem;
3101 : 35 : GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
3102 : : &glink_mem);
3103 : 35 : size_t sh_entsize = gelf_fsize (ebl->elf, ELF_T_HALF, 1, EV_CURRENT);
3104 [ - + ]: 35 : if (glink == NULL)
3105 : 0 : error (EXIT_FAILURE, 0, _("invalid sh_link value in section %zu"),
3106 : : elf_ndxscn (scn));
3107 : :
3108 : : /* Print the header. */
3109 : 70 : printf (ngettext ("\
3110 : : \nVersion symbols section [%2u] '%s' contains %d entry:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'",
3111 : : "\
3112 : : \nVersion symbols section [%2u] '%s' contains %d entries:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'",
3113 : : shdr->sh_size / sh_entsize),
3114 : 35 : (unsigned int) elf_ndxscn (scn),
3115 : 35 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
3116 [ + + ]: 35 : (int) (shdr->sh_size / sh_entsize),
3117 : : class == ELFCLASS32 ? 10 : 18, shdr->sh_addr,
3118 : : shdr->sh_offset,
3119 : 35 : (unsigned int) shdr->sh_link,
3120 : 35 : elf_strptr (ebl->elf, shstrndx, glink->sh_name));
3121 : :
3122 : : /* Now we can finally look at the actual contents of this section. */
3123 [ + + ]: 1354 : for (unsigned int cnt = 0; cnt < shdr->sh_size / sh_entsize; ++cnt)
3124 : : {
3125 [ + + ]: 1319 : if (cnt % 2 == 0)
3126 : 667 : printf ("\n %4d:", cnt);
3127 : :
3128 : 1319 : GElf_Versym symmem;
3129 : 1319 : GElf_Versym *sym = gelf_getversym (data, cnt, &symmem);
3130 [ + - ]: 1319 : if (sym == NULL)
3131 : : break;
3132 : :
3133 [ + + + ]: 1319 : switch (*sym)
3134 : : {
3135 : 235 : ssize_t n;
3136 : 235 : case 0:
3137 : 235 : fputs_unlocked (_(" 0 *local* "),
3138 : : stdout);
3139 : 235 : break;
3140 : :
3141 : 16 : case 1:
3142 : 16 : fputs_unlocked (_(" 1 *global* "),
3143 : : stdout);
3144 : 16 : break;
3145 : :
3146 : 1068 : default:
3147 [ + - ]: 1068 : n = printf ("%4d%c%s",
3148 [ + - ]: 1068 : *sym & 0x7fff, *sym & 0x8000 ? 'h' : ' ',
3149 : : (vername != NULL
3150 [ + - ]: 1068 : && (unsigned int) (*sym & 0x7fff) < nvername)
3151 : 1068 : ? vername[*sym & 0x7fff] : "???");
3152 [ + - ]: 1068 : if ((unsigned int) (*sym & 0x7fff) < nvername
3153 [ + - + + ]: 1068 : && filename != NULL && filename[*sym & 0x7fff] != NULL)
3154 : 916 : n += printf ("(%s)", filename[*sym & 0x7fff]);
3155 : 1319 : printf ("%*s", MAX (0, 33 - (int) n), " ");
3156 : : break;
3157 : : }
3158 : : }
3159 [ - + ]: 70 : putchar_unlocked ('\n');
3160 : : }
3161 : :
3162 : :
3163 : : static void
3164 : 35 : print_hash_info (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr, size_t shstrndx,
3165 : : uint_fast32_t maxlength, Elf32_Word nbucket,
3166 : : uint_fast32_t nsyms, uint32_t *lengths, const char *extrastr)
3167 : : {
3168 : 35 : uint32_t *counts = (uint32_t *) xcalloc (maxlength + 1, sizeof (uint32_t));
3169 : :
3170 [ + + ]: 192 : for (Elf32_Word cnt = 0; cnt < nbucket; ++cnt)
3171 : 157 : ++counts[lengths[cnt]];
3172 : :
3173 : 35 : GElf_Shdr glink_mem;
3174 : 35 : GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf,
3175 : 35 : shdr->sh_link),
3176 : : &glink_mem);
3177 [ - + ]: 35 : if (glink == NULL)
3178 : : {
3179 : 0 : error (0, 0, _("invalid sh_link value in section %zu"),
3180 : : elf_ndxscn (scn));
3181 : 0 : return;
3182 : : }
3183 : :
3184 [ + + ]: 105 : printf (ngettext ("\
3185 : : \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",
3186 : : "\
3187 : : \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",
3188 : : nbucket),
3189 : 35 : (unsigned int) elf_ndxscn (scn),
3190 : 35 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
3191 : : (int) nbucket,
3192 : 35 : gelf_getclass (ebl->elf) == ELFCLASS32 ? 10 : 18,
3193 : : shdr->sh_addr,
3194 : : shdr->sh_offset,
3195 : 35 : (unsigned int) shdr->sh_link,
3196 : 35 : elf_strptr (ebl->elf, shstrndx, glink->sh_name));
3197 : :
3198 [ + - ]: 35 : if (extrastr != NULL)
3199 : 35 : fputs (extrastr, stdout);
3200 : :
3201 [ + - ]: 35 : if (likely (nbucket > 0))
3202 : : {
3203 : 35 : uint64_t success = 0;
3204 : :
3205 : : /* xgettext:no-c-format */
3206 : 35 : fputs_unlocked (_("\
3207 : : Length Number % of total Coverage\n"), stdout);
3208 : 35 : printf (_(" 0 %6" PRIu32 " %5.1f%%\n"),
3209 : 35 : counts[0], (counts[0] * 100.0) / nbucket);
3210 : :
3211 : 35 : uint64_t nzero_counts = 0;
3212 [ + + ]: 55 : for (Elf32_Word cnt = 1; cnt <= maxlength; ++cnt)
3213 : : {
3214 : 20 : nzero_counts += counts[cnt] * cnt;
3215 : 20 : printf (_("\
3216 : : %7d %6" PRIu32 " %5.1f%% %5.1f%%\n"),
3217 : 20 : (int) cnt, counts[cnt], (counts[cnt] * 100.0) / nbucket,
3218 : 20 : (nzero_counts * 100.0) / nsyms);
3219 : : }
3220 : :
3221 : : Elf32_Word acc = 0;
3222 [ + + ]: 55 : for (Elf32_Word cnt = 1; cnt <= maxlength; ++cnt)
3223 : : {
3224 : 20 : acc += cnt;
3225 : 20 : success += counts[cnt] * acc;
3226 : : }
3227 : :
3228 : 70 : printf (_("\
3229 : : Average number of tests: successful lookup: %f\n\
3230 : : unsuccessful lookup: %f\n"),
3231 : 35 : (double) success / (double) nzero_counts,
3232 : : (double) nzero_counts / (double) nbucket);
3233 : : }
3234 : :
3235 : 35 : free (counts);
3236 : : }
3237 : :
3238 : :
3239 : : /* This function handles the traditional System V-style hash table format. */
3240 : : static void
3241 : 0 : handle_sysv_hash (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr, size_t shstrndx)
3242 : : {
3243 : 0 : Elf_Data *data = elf_getdata (scn, NULL);
3244 [ # # ]: 0 : if (unlikely (data == NULL))
3245 : : {
3246 : 0 : error (0, 0, _("cannot get data for section %d: %s"),
3247 : 0 : (int) elf_ndxscn (scn), elf_errmsg (-1));
3248 : 0 : return;
3249 : : }
3250 : :
3251 [ # # ]: 0 : if (unlikely (data->d_size < 2 * sizeof (Elf32_Word)))
3252 : : {
3253 : 0 : invalid_data:
3254 : 0 : error (0, 0, _("invalid data in sysv.hash section %d"),
3255 : 0 : (int) elf_ndxscn (scn));
3256 : 0 : return;
3257 : : }
3258 : :
3259 : 0 : Elf32_Word nbucket = ((Elf32_Word *) data->d_buf)[0];
3260 : 0 : Elf32_Word nchain = ((Elf32_Word *) data->d_buf)[1];
3261 : :
3262 : 0 : uint64_t used_buf = (2ULL + nchain + nbucket) * sizeof (Elf32_Word);
3263 [ # # ]: 0 : if (used_buf > data->d_size)
3264 : 0 : goto invalid_data;
3265 : :
3266 : 0 : Elf32_Word *bucket = &((Elf32_Word *) data->d_buf)[2];
3267 : 0 : Elf32_Word *chain = &((Elf32_Word *) 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 (Elf32_Word cnt = 0; cnt < nbucket; ++cnt)
3274 : : {
3275 : 0 : Elf32_Word inner = bucket[cnt];
3276 : 0 : Elf32_Word 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, _("invalid chain in sysv.hash 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 incorrect, System V-style hash table
3303 : : format some 64-bit architectures use. */
3304 : : static void
3305 : 0 : handle_sysv_hash64 (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr, size_t shstrndx)
3306 : : {
3307 : 0 : Elf_Data *data = elf_getdata (scn, NULL);
3308 [ # # ]: 0 : if (unlikely (data == NULL))
3309 : : {
3310 : 0 : error (0, 0, _("cannot get data for section %d: %s"),
3311 : 0 : (int) elf_ndxscn (scn), elf_errmsg (-1));
3312 : 0 : return;
3313 : : }
3314 : :
3315 [ # # ]: 0 : if (unlikely (data->d_size < 2 * sizeof (Elf64_Xword)))
3316 : : {
3317 : 0 : invalid_data:
3318 : 0 : error (0, 0, _("invalid data in sysv.hash64 section %d"),
3319 : 0 : (int) elf_ndxscn (scn));
3320 : 0 : return;
3321 : : }
3322 : :
3323 : 0 : Elf64_Xword nbucket = ((Elf64_Xword *) data->d_buf)[0];
3324 : 0 : Elf64_Xword nchain = ((Elf64_Xword *) data->d_buf)[1];
3325 : :
3326 : 0 : uint64_t maxwords = data->d_size / sizeof (Elf64_Xword);
3327 [ # # ]: 0 : if (maxwords < 2
3328 [ # # ]: 0 : || maxwords - 2 < nbucket
3329 [ # # ]: 0 : || maxwords - 2 - nbucket < nchain)
3330 : 0 : goto invalid_data;
3331 : :
3332 : 0 : Elf64_Xword *bucket = &((Elf64_Xword *) data->d_buf)[2];
3333 : 0 : Elf64_Xword *chain = &((Elf64_Xword *) data->d_buf)[2 + nbucket];
3334 : :
3335 : 0 : uint32_t *lengths = (uint32_t *) xcalloc (nbucket, sizeof (uint32_t));
3336 : :
3337 : 0 : uint_fast32_t maxlength = 0;
3338 : 0 : uint_fast32_t nsyms = 0;
3339 [ # # ]: 0 : for (Elf64_Xword cnt = 0; cnt < nbucket; ++cnt)
3340 : : {
3341 : 0 : Elf64_Xword inner = bucket[cnt];
3342 : 0 : Elf64_Xword chain_len = 0;
3343 [ # # ]: 0 : while (inner > 0 && inner < nchain)
3344 : : {
3345 : 0 : ++nsyms;
3346 : 0 : ++chain_len;
3347 [ # # ]: 0 : if (chain_len > nchain)
3348 : : {
3349 : 0 : error (0, 0, _("invalid chain in sysv.hash64 section %d"),
3350 : 0 : (int) elf_ndxscn (scn));
3351 : 0 : free (lengths);
3352 : 0 : return;
3353 : : }
3354 [ # # ]: 0 : if (maxlength < ++lengths[cnt])
3355 : 0 : ++maxlength;
3356 : :
3357 : 0 : inner = chain[inner];
3358 : : }
3359 : : }
3360 : :
3361 : 0 : print_hash_info (ebl, scn, shdr, shstrndx, maxlength, nbucket, nsyms,
3362 : : lengths, NULL);
3363 : :
3364 : 0 : free (lengths);
3365 : : }
3366 : :
3367 : :
3368 : : /* This function handles the GNU-style hash table format. */
3369 : : static void
3370 : 35 : handle_gnu_hash (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr, size_t shstrndx)
3371 : : {
3372 : 35 : uint32_t *lengths = NULL;
3373 : 35 : Elf_Data *data = elf_getdata (scn, NULL);
3374 [ - + ]: 35 : if (unlikely (data == NULL))
3375 : : {
3376 : 0 : error (0, 0, _("cannot get data for section %d: %s"),
3377 : 0 : (int) elf_ndxscn (scn), elf_errmsg (-1));
3378 : 0 : return;
3379 : : }
3380 : :
3381 [ - + ]: 35 : if (unlikely (data->d_size < 4 * sizeof (Elf32_Word)))
3382 : : {
3383 : 0 : invalid_data:
3384 : 0 : free (lengths);
3385 : 0 : error (0, 0, _("invalid data in gnu.hash section %d"),
3386 : 0 : (int) elf_ndxscn (scn));
3387 : 0 : return;
3388 : : }
3389 : :
3390 : 35 : Elf32_Word nbucket = ((Elf32_Word *) data->d_buf)[0];
3391 : 35 : Elf32_Word symbias = ((Elf32_Word *) data->d_buf)[1];
3392 : :
3393 : : /* Next comes the size of the bitmap. It's measured in words for
3394 : : the architecture. It's 32 bits for 32 bit archs, and 64 bits for
3395 : : 64 bit archs. There is always a bloom filter present, so zero is
3396 : : an invalid value. */
3397 : 35 : Elf32_Word bitmask_words = ((Elf32_Word *) data->d_buf)[2];
3398 [ + + ]: 35 : if (gelf_getclass (ebl->elf) == ELFCLASS64)
3399 : 31 : bitmask_words *= 2;
3400 : :
3401 [ - + ]: 35 : if (bitmask_words == 0)
3402 : 0 : goto invalid_data;
3403 : :
3404 : 35 : Elf32_Word shift = ((Elf32_Word *) data->d_buf)[3];
3405 : :
3406 : : /* Is there still room for the sym chain?
3407 : : Use uint64_t calculation to prevent 32bit overflow. */
3408 : 35 : uint64_t used_buf = (4ULL + bitmask_words + nbucket) * sizeof (Elf32_Word);
3409 : 35 : uint32_t max_nsyms = (data->d_size - used_buf) / sizeof (Elf32_Word);
3410 [ - + ]: 35 : if (used_buf > data->d_size)
3411 : 0 : goto invalid_data;
3412 : :
3413 : 35 : lengths = (uint32_t *) xcalloc (nbucket, sizeof (uint32_t));
3414 : :
3415 : 35 : Elf32_Word *bitmask = &((Elf32_Word *) data->d_buf)[4];
3416 : 35 : Elf32_Word *bucket = &((Elf32_Word *) data->d_buf)[4 + bitmask_words];
3417 : 35 : Elf32_Word *chain = &((Elf32_Word *) data->d_buf)[4 + bitmask_words
3418 : 35 : + nbucket];
3419 : :
3420 : : /* Compute distribution of chain lengths. */
3421 : 35 : uint_fast32_t maxlength = 0;
3422 : 35 : uint_fast32_t nsyms = 0;
3423 [ + + ]: 192 : for (Elf32_Word cnt = 0; cnt < nbucket; ++cnt)
3424 [ + + ]: 157 : if (bucket[cnt] != 0)
3425 : : {
3426 : 96 : Elf32_Word inner = bucket[cnt] - symbias;
3427 : 168 : do
3428 : : {
3429 : 168 : ++nsyms;
3430 [ + + ]: 168 : if (maxlength < ++lengths[cnt])
3431 : 20 : ++maxlength;
3432 [ - + ]: 168 : if (inner >= max_nsyms)
3433 : 0 : goto invalid_data;
3434 : : }
3435 [ + + ]: 168 : while ((chain[inner++] & 1) == 0);
3436 : : }
3437 : :
3438 : : /* Count bits in bitmask. */
3439 : : uint_fast32_t nbits = 0;
3440 [ + + ]: 137 : for (Elf32_Word cnt = 0; cnt < bitmask_words; ++cnt)
3441 : : {
3442 : 102 : uint_fast32_t word = bitmask[cnt];
3443 : :
3444 : 102 : word = (word & 0x55555555) + ((word >> 1) & 0x55555555);
3445 : 102 : word = (word & 0x33333333) + ((word >> 2) & 0x33333333);
3446 : 102 : word = (word & 0x0f0f0f0f) + ((word >> 4) & 0x0f0f0f0f);
3447 : 102 : word = (word & 0x00ff00ff) + ((word >> 8) & 0x00ff00ff);
3448 : 102 : nbits += (word & 0x0000ffff) + ((word >> 16) & 0x0000ffff);
3449 : : }
3450 : :
3451 : 35 : char *str;
3452 [ - + ]: 35 : if (unlikely (asprintf (&str, _("\
3453 : : Symbol Bias: %u\n\
3454 : : Bitmask Size: %zu bytes %" PRIuFAST32 "%% bits set 2nd hash shift: %u\n"),
3455 : : (unsigned int) symbias,
3456 : : bitmask_words * sizeof (Elf32_Word),
3457 : : ((nbits * 100 + 50)
3458 : : / (uint_fast32_t) (bitmask_words
3459 : : * sizeof (Elf32_Word) * 8)),
3460 : : (unsigned int) shift) == -1))
3461 : 0 : error (EXIT_FAILURE, 0, _("memory exhausted"));
3462 : :
3463 : 35 : print_hash_info (ebl, scn, shdr, shstrndx, maxlength, nbucket, nsyms,
3464 : : lengths, str);
3465 : :
3466 : 35 : free (str);
3467 : 35 : free (lengths);
3468 : : }
3469 : :
3470 : :
3471 : : /* Find the symbol table(s). For this we have to search through the
3472 : : section table. */
3473 : : static void
3474 : 81 : handle_hash (Ebl *ebl)
3475 : : {
3476 : : /* Get the section header string table index. */
3477 : 81 : size_t shstrndx;
3478 [ - + ]: 81 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
3479 : 0 : error (EXIT_FAILURE, 0,
3480 : 0 : _("cannot get section header string table index"));
3481 : :
3482 : : Elf_Scn *scn = NULL;
3483 [ + + ]: 2245 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
3484 : : {
3485 : : /* Handle the section if it is a symbol table. */
3486 : 2164 : GElf_Shdr shdr_mem;
3487 : 2164 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
3488 : :
3489 [ + - ]: 2164 : if (likely (shdr != NULL))
3490 : : {
3491 [ + + ]: 2164 : if ((shdr->sh_type == SHT_HASH || shdr->sh_type == SHT_GNU_HASH)
3492 [ - + ]: 35 : && (shdr->sh_flags & SHF_COMPRESSED) != 0)
3493 : : {
3494 [ # # ]: 0 : if (elf_compress (scn, 0, 0) < 0)
3495 : 0 : printf ("WARNING: %s [%zd]\n",
3496 : : _("Couldn't uncompress section"),
3497 : : elf_ndxscn (scn));
3498 : 0 : shdr = gelf_getshdr (scn, &shdr_mem);
3499 [ # # ]: 0 : if (unlikely (shdr == NULL))
3500 : 0 : error (EXIT_FAILURE, 0,
3501 : 0 : _("cannot get section [%zd] header: %s"),
3502 : : elf_ndxscn (scn), elf_errmsg (-1));
3503 : : }
3504 : :
3505 [ - + ]: 2164 : if (shdr->sh_type == SHT_HASH)
3506 : : {
3507 [ # # ]: 0 : if (ebl_sysvhash_entrysize (ebl) == sizeof (Elf64_Xword))
3508 : 0 : handle_sysv_hash64 (ebl, scn, shdr, shstrndx);
3509 : : else
3510 : 0 : handle_sysv_hash (ebl, scn, shdr, shstrndx);
3511 : : }
3512 [ + + ]: 2164 : else if (shdr->sh_type == SHT_GNU_HASH)
3513 : 35 : handle_gnu_hash (ebl, scn, shdr, shstrndx);
3514 : : }
3515 : : }
3516 : 81 : }
3517 : :
3518 : :
3519 : : static void
3520 : 85 : print_liblist (Ebl *ebl)
3521 : : {
3522 : : /* Find the library list sections. For this we have to search
3523 : : through the section table. */
3524 : 85 : Elf_Scn *scn = NULL;
3525 : :
3526 : : /* Get the section header string table index. */
3527 : 85 : size_t shstrndx;
3528 [ - + ]: 85 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
3529 : 0 : error (EXIT_FAILURE, 0,
3530 : 0 : _("cannot get section header string table index"));
3531 : :
3532 [ + + ]: 2308 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
3533 : : {
3534 : 2223 : GElf_Shdr shdr_mem;
3535 : 2223 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
3536 : :
3537 [ + - - + ]: 2223 : if (shdr != NULL && shdr->sh_type == SHT_GNU_LIBLIST)
3538 : : {
3539 : 0 : size_t sh_entsize = gelf_fsize (ebl->elf, ELF_T_LIB, 1, EV_CURRENT);
3540 : 0 : int nentries = shdr->sh_size / sh_entsize;
3541 : 0 : printf (ngettext ("\
3542 : : \nLibrary list section [%2zu] '%s' at offset %#0" PRIx64 " contains %d entry:\n",
3543 : : "\
3544 : : \nLibrary list section [%2zu] '%s' at offset %#0" PRIx64 " contains %d entries:\n",
3545 : : nentries),
3546 : : elf_ndxscn (scn),
3547 : 0 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
3548 : : shdr->sh_offset,
3549 : : nentries);
3550 : :
3551 : 0 : Elf_Data *data = elf_getdata (scn, NULL);
3552 [ # # ]: 0 : if (data == NULL)
3553 : 0 : return;
3554 : :
3555 : 0 : puts (_("\
3556 : : Library Time Stamp Checksum Version Flags"));
3557 : :
3558 [ # # ]: 0 : for (int cnt = 0; cnt < nentries; ++cnt)
3559 : : {
3560 : 0 : GElf_Lib lib_mem;
3561 : 0 : GElf_Lib *lib = gelf_getlib (data, cnt, &lib_mem);
3562 [ # # ]: 0 : if (unlikely (lib == NULL))
3563 : 0 : continue;
3564 : :
3565 : 0 : time_t t = (time_t) lib->l_time_stamp;
3566 : 0 : struct tm *tm = gmtime (&t);
3567 [ # # ]: 0 : if (unlikely (tm == NULL))
3568 : 0 : continue;
3569 : :
3570 : 0 : printf (" [%2d] %-29s %04u-%02u-%02uT%02u:%02u:%02u %08x %-7u %u\n",
3571 : 0 : cnt, elf_strptr (ebl->elf, shdr->sh_link, lib->l_name),
3572 : 0 : tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
3573 : : tm->tm_hour, tm->tm_min, tm->tm_sec,
3574 : 0 : (unsigned int) lib->l_checksum,
3575 : 0 : (unsigned int) lib->l_version,
3576 : 0 : (unsigned int) lib->l_flags);
3577 : : }
3578 : : }
3579 : : }
3580 : : }
3581 : :
3582 : : static inline size_t
3583 : 12 : left (Elf_Data *data,
3584 : : const unsigned char *p)
3585 : : {
3586 : 12 : return (const unsigned char *) data->d_buf + data->d_size - p;
3587 : : }
3588 : :
3589 : : static void
3590 : 85 : print_attributes (Ebl *ebl, const GElf_Ehdr *ehdr)
3591 : : {
3592 : : /* Find the object attributes sections. For this we have to search
3593 : : through the section table. */
3594 : 85 : Elf_Scn *scn = NULL;
3595 : :
3596 : : /* Get the section header string table index. */
3597 : 85 : size_t shstrndx;
3598 [ - + ]: 85 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
3599 : 0 : error (EXIT_FAILURE, 0,
3600 : 0 : _("cannot get section header string table index"));
3601 : :
3602 [ + + ]: 2308 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
3603 : : {
3604 : 2223 : GElf_Shdr shdr_mem;
3605 : 2223 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
3606 : :
3607 [ + - + + ]: 2223 : if (shdr == NULL || (shdr->sh_type != SHT_GNU_ATTRIBUTES
3608 [ + + ]: 2220 : && (shdr->sh_type != SHT_ARM_ATTRIBUTES
3609 [ - + ]: 1 : || ehdr->e_machine != EM_ARM)
3610 [ + + ]: 2219 : && (shdr->sh_type != SHT_CSKY_ATTRIBUTES
3611 [ + - ]: 1 : || ehdr->e_machine != EM_CSKY)))
3612 : 2219 : continue;
3613 : :
3614 : 8 : printf (_("\
3615 : : \nObject attributes section [%2zu] '%s' of %" PRIu64
3616 : : " bytes at offset %#0" PRIx64 ":\n"),
3617 : : elf_ndxscn (scn),
3618 : 4 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
3619 : : shdr->sh_size, shdr->sh_offset);
3620 : :
3621 : 4 : Elf_Data *data = elf_rawdata (scn, NULL);
3622 [ + - + - ]: 4 : if (unlikely (data == NULL || data->d_size == 0))
3623 : 0 : return;
3624 : :
3625 : 4 : const unsigned char *p = data->d_buf;
3626 : :
3627 : : /* There is only one 'version', A. */
3628 [ + - ]: 4 : if (unlikely (*p++ != 'A'))
3629 : : return;
3630 : :
3631 : 4 : fputs_unlocked (_(" Owner Size\n"), stdout);
3632 : :
3633 : : /* Loop over the sections. */
3634 [ + + ]: 8 : while (left (data, p) >= 4)
3635 : : {
3636 : : /* Section length. */
3637 : 4 : uint32_t len;
3638 [ + + ]: 4 : memcpy (&len, p, sizeof len);
3639 : :
3640 [ + + ]: 4 : if (MY_ELFDATA != ehdr->e_ident[EI_DATA])
3641 : 2 : CONVERT (len);
3642 : :
3643 [ + - ]: 4 : if (unlikely (len > left (data, p)))
3644 : : break;
3645 : :
3646 : : /* Section vendor name. */
3647 : 4 : const unsigned char *name = p + sizeof len;
3648 : 4 : p += len;
3649 : :
3650 : 4 : unsigned const char *q = memchr (name, '\0', len);
3651 [ + - ]: 4 : if (unlikely (q == NULL))
3652 : : break;
3653 : 4 : ++q;
3654 : :
3655 : 4 : printf (_(" %-13s %4" PRIu32 "\n"), name, len);
3656 : :
3657 : 8 : bool gnu_vendor = (q - name == sizeof "gnu"
3658 [ + + - + ]: 4 : && !memcmp (name, "gnu", sizeof "gnu"));
3659 : :
3660 : : /* Loop over subsections. */
3661 [ + + ]: 4 : if (shdr->sh_type != SHT_GNU_ATTRIBUTES
3662 [ + - ]: 3 : || gnu_vendor)
3663 [ + + ]: 8 : while (q < p)
3664 : : {
3665 : 4 : const unsigned char *const sub = q;
3666 : :
3667 : 4 : unsigned int subsection_tag;
3668 : 4 : get_uleb128 (subsection_tag, q, p);
3669 [ + - ]: 4 : if (unlikely (q >= p))
3670 : : break;
3671 : :
3672 : 4 : uint32_t subsection_len;
3673 [ + - ]: 4 : if (unlikely (p - sub < (ptrdiff_t) sizeof subsection_len))
3674 : : break;
3675 : :
3676 [ + + ]: 4 : memcpy (&subsection_len, q, sizeof subsection_len);
3677 : :
3678 [ + + ]: 4 : if (MY_ELFDATA != ehdr->e_ident[EI_DATA])
3679 : 2 : CONVERT (subsection_len);
3680 : :
3681 : : /* Don't overflow, ptrdiff_t might be 32bits, but signed. */
3682 [ + - + - ]: 4 : if (unlikely (subsection_len == 0
3683 : : || subsection_len >= (uint32_t) PTRDIFF_MAX
3684 : : || p - sub < (ptrdiff_t) subsection_len))
3685 : : break;
3686 : :
3687 : 4 : const unsigned char *r = q + sizeof subsection_len;
3688 : 4 : q = sub + subsection_len;
3689 : :
3690 [ - + ]: 4 : switch (subsection_tag)
3691 : : {
3692 : 0 : default:
3693 : : /* Unknown subsection, print and skip. */
3694 : 4 : printf (_(" %-4u %12" PRIu32 "\n"),
3695 : : subsection_tag, subsection_len);
3696 : : break;
3697 : :
3698 : 4 : case 1: /* Tag_File */
3699 : 4 : printf (_(" File: %11" PRIu32 "\n"),
3700 : : subsection_len);
3701 : :
3702 [ + + ]: 26 : while (r < q)
3703 : : {
3704 : 22 : unsigned int tag;
3705 : 22 : get_uleb128 (tag, r, q);
3706 [ + - ]: 22 : if (unlikely (r >= q))
3707 : : break;
3708 : :
3709 : : /* GNU style tags have either a uleb128 value,
3710 : : when lowest bit is not set, or a string
3711 : : when the lowest bit is set.
3712 : : "compatibility" (32) is special. It has
3713 : : both a string and a uleb128 value. For
3714 : : non-gnu we assume 6 till 31 only take ints.
3715 : : XXX see arm backend, do we need a separate
3716 : : hook? */
3717 : 22 : uint64_t value = 0;
3718 : 22 : const char *string = NULL;
3719 [ + - + + ]: 22 : if (tag == 32 || (tag & 1) == 0
3720 [ + - + + ]: 8 : || (! gnu_vendor && (tag > 5 && tag < 32)))
3721 : : {
3722 : 21 : get_uleb128 (value, r, q);
3723 [ + - ]: 21 : if (r > q)
3724 : : break;
3725 : : }
3726 [ + - ]: 22 : if (tag == 32
3727 [ + + ]: 22 : || ((tag & 1) != 0
3728 [ + - ]: 8 : && (gnu_vendor
3729 [ + - ]: 8 : || (! gnu_vendor && tag > 32)))
3730 [ + + + + ]: 22 : || (! gnu_vendor && tag > 3 && tag < 6))
3731 : : {
3732 : 1 : string = (const char *) r;
3733 : 1 : r = memchr (r, '\0', q - r);
3734 [ + - ]: 1 : if (r == NULL)
3735 : : break;
3736 : 1 : ++r;
3737 : : }
3738 : :
3739 : 22 : const char *tag_name = NULL;
3740 : 22 : const char *value_name = NULL;
3741 : 22 : ebl_check_object_attribute (ebl, (const char *) name,
3742 : : tag, value,
3743 : : &tag_name, &value_name);
3744 : :
3745 [ + - ]: 22 : if (tag_name != NULL)
3746 : : {
3747 [ - + ]: 22 : if (tag == 32)
3748 : 0 : printf (_(" %s: %" PRId64 ", %s\n"),
3749 : : tag_name, value, string);
3750 [ + + + + ]: 22 : else if (string == NULL && value_name == NULL)
3751 : 1 : printf (_(" %s: %" PRId64 "\n"),
3752 : : tag_name, value);
3753 : : else
3754 [ + + ]: 21 : printf (_(" %s: %s\n"),
3755 : : tag_name, string ?: value_name);
3756 : : }
3757 : : else
3758 : : {
3759 : : /* For "gnu" vendor 32 "compatibility" has
3760 : : already been handled above. */
3761 [ # # # # ]: 0 : assert (tag != 32
3762 : : || strcmp ((const char *) name, "gnu"));
3763 [ # # ]: 0 : if (string == NULL)
3764 : 0 : printf (_(" %u: %" PRId64 "\n"),
3765 : : tag, value);
3766 : : else
3767 : 22 : printf (_(" %u: %s\n"),
3768 : : tag, string);
3769 : : }
3770 : : }
3771 : : }
3772 : : }
3773 : : }
3774 : : }
3775 : : }
3776 : :
3777 : :
3778 : : void
3779 : 1123473 : print_dwarf_addr (Dwfl_Module *dwflmod,
3780 : : int address_size, Dwarf_Addr address, Dwarf_Addr raw)
3781 : : {
3782 : : /* See if there is a name we can give for this address. */
3783 : 1123473 : GElf_Sym sym;
3784 : 1123473 : GElf_Off off = 0;
3785 [ + + ]: 1123445 : const char *name = (print_address_names && ! print_unresolved_addresses)
3786 : 1123363 : ? dwfl_module_addrinfo (dwflmod, address, &off, &sym, NULL, NULL, NULL)
3787 [ + + ]: 1123473 : : NULL;
3788 : :
3789 : 1123473 : const char *scn;
3790 [ + + ]: 1123473 : if (print_unresolved_addresses)
3791 : : {
3792 : 82 : address = raw;
3793 : 82 : scn = NULL;
3794 : : }
3795 : : else
3796 : : {
3797 : : /* Relativize the address. */
3798 : 1123391 : int n = dwfl_module_relocations (dwflmod);
3799 [ + + ]: 1123391 : int i = n < 1 ? -1 : dwfl_module_relocate_address (dwflmod, &address);
3800 : :
3801 : : /* In an ET_REL file there is a section name to refer to. */
3802 : 1122203 : scn = (i < 0 ? NULL
3803 [ + - ]: 1122203 : : dwfl_module_relocation_info (dwflmod, i, NULL));
3804 : : }
3805 : :
3806 [ - + ]: 1123473 : if ((name != NULL
3807 : 1114504 : ? (off != 0
3808 : : ? (scn != NULL
3809 : : ? (address_size == 0
3810 : 372068 : ? printf ("%s+%#" PRIx64 " <%s+%#" PRIx64 ">",
3811 : : scn, address, name, off)
3812 : 1387892 : : printf ("%s+%#0*" PRIx64 " <%s+%#" PRIx64 ">",
3813 : 693946 : scn, 2 + address_size * 2, address,
3814 : : name, off))
3815 : : : (address_size == 0
3816 : 170 : ? printf ("%#" PRIx64 " <%s+%#" PRIx64 ">",
3817 : : address, name, off)
3818 : 1032 : : printf ("%#0*" PRIx64 " <%s+%#" PRIx64 ">",
3819 : 516 : 2 + address_size * 2, address,
3820 : : name, off)))
3821 : : : (scn != NULL
3822 : : ? (address_size == 0
3823 : 15432 : ? printf ("%s+%#" PRIx64 " <%s>", scn, address, name)
3824 : 64000 : : printf ("%s+%#0*" PRIx64 " <%s>",
3825 : 32000 : scn, 2 + address_size * 2, address, name))
3826 : : : (address_size == 0
3827 : 79 : ? printf ("%#" PRIx64 " <%s>", address, name)
3828 : 586 : : printf ("%#0*" PRIx64 " <%s>",
3829 : 293 : 2 + address_size * 2, address, name))))
3830 : : : (scn != NULL
3831 : : ? (address_size == 0
3832 : 1836 : ? printf ("%s+%#" PRIx64, scn, address)
3833 : 6921 : : printf ("%s+%#0*" PRIx64, scn, 2 + address_size * 2, address))
3834 : : : (address_size == 0
3835 : 34 : ? printf ("%#" PRIx64, address)
3836 [ + + + + : 2246768 : : printf ("%#0*" PRIx64, 2 + address_size * 2, address)))) < 0)
+ + + + +
+ + + + +
+ + + + +
+ + + ]
3837 : 0 : error (EXIT_FAILURE, 0, _("sprintf failure"));
3838 : 1123473 : }
3839 : :
3840 : :
3841 : : static const char *
3842 : 1021871 : dwarf_tag_string (unsigned int tag)
3843 : : {
3844 [ - + + - : 1021871 : switch (tag)
- - - - +
- + + - -
- - - - -
+ - + - +
- - + + -
+ - - - -
- - - + -
+ - + + +
- - - - -
- + - - +
- - - + -
+ + + + -
- - - - +
+ + + - +
+ + + - -
- ]
3845 : : {
3846 : : #define DWARF_ONE_KNOWN_DW_TAG(NAME, CODE) case CODE: return #NAME;
3847 : 1021871 : DWARF_ALL_KNOWN_DW_TAG
3848 : : #undef DWARF_ONE_KNOWN_DW_TAG
3849 : 0 : default:
3850 : 0 : return NULL;
3851 : : }
3852 : : }
3853 : :
3854 : :
3855 : : static const char *
3856 : 3981776 : dwarf_attr_string (unsigned int attrnum)
3857 : : {
3858 [ + - + - : 3981776 : switch (attrnum)
- + - + -
- + + + -
- - - + +
- + - - +
- + - + -
- - - - -
- - - - -
- - - - -
- - - + -
+ - + - +
- - - + +
- - - + -
+ - - + -
- + + + -
- + + - -
- - - + -
+ - + - -
+ - - + +
+ + - - -
- - + + +
+ - + - +
- - - - +
+ - + - +
+ - + + +
+ + - - +
- - - + -
+ - - - -
+ + - + -
- - - + -
- - + + -
+ - - - -
+ - - - -
- - + + -
- - - - -
- + ]
3859 : : {
3860 : : #define DWARF_ONE_KNOWN_DW_AT(NAME, CODE) case CODE: return #NAME;
3861 : 3981775 : DWARF_ALL_KNOWN_DW_AT
3862 : : #undef DWARF_ONE_KNOWN_DW_AT
3863 : 0 : default:
3864 : 0 : return NULL;
3865 : : }
3866 : : }
3867 : :
3868 : :
3869 : : static const char *
3870 : 3981782 : dwarf_form_string (unsigned int form)
3871 : : {
3872 [ + + + + : 3981782 : switch (form)
+ - - - -
- + - - +
- + + + +
+ + + - +
+ - - + -
- + - - +
+ + + + +
- + - - -
- + - + ]
3873 : : {
3874 : : #define DWARF_ONE_KNOWN_DW_FORM(NAME, CODE) case CODE: return #NAME;
3875 : 3981780 : DWARF_ALL_KNOWN_DW_FORM
3876 : : #undef DWARF_ONE_KNOWN_DW_FORM
3877 : 0 : default:
3878 : 0 : return NULL;
3879 : : }
3880 : : }
3881 : :
3882 : :
3883 : : static const char *
3884 : 1700 : dwarf_lang_string (unsigned int lang)
3885 : : {
3886 [ + - - + : 1700 : switch (lang)
+ + + - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - -
- ]
3887 : : {
3888 : : #define DWARF_ONE_KNOWN_DW_LANG(NAME, CODE) case CODE: return #NAME;
3889 : 1700 : DWARF_ALL_KNOWN_DW_LANG
3890 : : #undef DWARF_ONE_KNOWN_DW_LANG
3891 : 0 : default:
3892 : 0 : return NULL;
3893 : : }
3894 : : }
3895 : :
3896 : :
3897 : : static const char *
3898 : 3557 : dwarf_inline_string (unsigned int code)
3899 : : {
3900 : 3557 : static const char *const known[] =
3901 : : {
3902 : : #define DWARF_ONE_KNOWN_DW_INL(NAME, CODE) [CODE] = #NAME,
3903 : : DWARF_ALL_KNOWN_DW_INL
3904 : : #undef DWARF_ONE_KNOWN_DW_INL
3905 : : };
3906 : :
3907 : 3557 : if (likely (code < sizeof (known) / sizeof (known[0])))
3908 : 3557 : return known[code];
3909 : :
3910 : : return NULL;
3911 : : }
3912 : :
3913 : :
3914 : : static const char *
3915 : 30272 : dwarf_encoding_string (unsigned int code)
3916 : : {
3917 : 30272 : static const char *const known[] =
3918 : : {
3919 : : #define DWARF_ONE_KNOWN_DW_ATE(NAME, CODE) [CODE] = #NAME,
3920 : : DWARF_ALL_KNOWN_DW_ATE
3921 : : #undef DWARF_ONE_KNOWN_DW_ATE
3922 : : };
3923 : :
3924 : 30272 : if (likely (code < sizeof (known) / sizeof (known[0])))
3925 : 30272 : return known[code];
3926 : :
3927 : : return NULL;
3928 : : }
3929 : :
3930 : :
3931 : : static const char *
3932 : 0 : dwarf_access_string (unsigned int code)
3933 : : {
3934 : 0 : static const char *const known[] =
3935 : : {
3936 : : #define DWARF_ONE_KNOWN_DW_ACCESS(NAME, CODE) [CODE] = #NAME,
3937 : : DWARF_ALL_KNOWN_DW_ACCESS
3938 : : #undef DWARF_ONE_KNOWN_DW_ACCESS
3939 : : };
3940 : :
3941 : 0 : if (likely (code < sizeof (known) / sizeof (known[0])))
3942 : 0 : return known[code];
3943 : :
3944 : : return NULL;
3945 : : }
3946 : :
3947 : :
3948 : : static const char *
3949 : 0 : dwarf_defaulted_string (unsigned int code)
3950 : : {
3951 : 0 : static const char *const known[] =
3952 : : {
3953 : : #define DWARF_ONE_KNOWN_DW_DEFAULTED(NAME, CODE) [CODE] = #NAME,
3954 : : DWARF_ALL_KNOWN_DW_DEFAULTED
3955 : : #undef DWARF_ONE_KNOWN_DW_DEFAULTED
3956 : : };
3957 : :
3958 : 0 : if (likely (code < sizeof (known) / sizeof (known[0])))
3959 : 0 : return known[code];
3960 : :
3961 : : return NULL;
3962 : : }
3963 : :
3964 : :
3965 : : static const char *
3966 : 0 : dwarf_visibility_string (unsigned int code)
3967 : : {
3968 : 0 : static const char *const known[] =
3969 : : {
3970 : : #define DWARF_ONE_KNOWN_DW_VIS(NAME, CODE) [CODE] = #NAME,
3971 : : DWARF_ALL_KNOWN_DW_VIS
3972 : : #undef DWARF_ONE_KNOWN_DW_VIS
3973 : : };
3974 : :
3975 : 0 : if (likely (code < sizeof (known) / sizeof (known[0])))
3976 : 0 : return known[code];
3977 : :
3978 : : return NULL;
3979 : : }
3980 : :
3981 : :
3982 : : static const char *
3983 : 0 : dwarf_virtuality_string (unsigned int code)
3984 : : {
3985 : 0 : static const char *const known[] =
3986 : : {
3987 : : #define DWARF_ONE_KNOWN_DW_VIRTUALITY(NAME, CODE) [CODE] = #NAME,
3988 : : DWARF_ALL_KNOWN_DW_VIRTUALITY
3989 : : #undef DWARF_ONE_KNOWN_DW_VIRTUALITY
3990 : : };
3991 : :
3992 : 0 : if (likely (code < sizeof (known) / sizeof (known[0])))
3993 : 0 : return known[code];
3994 : :
3995 : : return NULL;
3996 : : }
3997 : :
3998 : :
3999 : : static const char *
4000 : 0 : dwarf_identifier_case_string (unsigned int code)
4001 : : {
4002 : 0 : static const char *const known[] =
4003 : : {
4004 : : #define DWARF_ONE_KNOWN_DW_ID(NAME, CODE) [CODE] = #NAME,
4005 : : DWARF_ALL_KNOWN_DW_ID
4006 : : #undef DWARF_ONE_KNOWN_DW_ID
4007 : : };
4008 : :
4009 : 0 : if (likely (code < sizeof (known) / sizeof (known[0])))
4010 : 0 : return known[code];
4011 : :
4012 : : return NULL;
4013 : : }
4014 : :
4015 : :
4016 : : static const char *
4017 : 0 : dwarf_calling_convention_string (unsigned int code)
4018 : : {
4019 : 0 : static const char *const known[] =
4020 : : {
4021 : : #define DWARF_ONE_KNOWN_DW_CC(NAME, CODE) [CODE] = #NAME,
4022 : : DWARF_ALL_KNOWN_DW_CC
4023 : : #undef DWARF_ONE_KNOWN_DW_CC
4024 : : };
4025 : :
4026 : 0 : if (likely (code < sizeof (known) / sizeof (known[0])))
4027 : 0 : return known[code];
4028 : :
4029 : : return NULL;
4030 : : }
4031 : :
4032 : :
4033 : : static const char *
4034 : 0 : dwarf_ordering_string (unsigned int code)
4035 : : {
4036 : 0 : static const char *const known[] =
4037 : : {
4038 : : #define DWARF_ONE_KNOWN_DW_ORD(NAME, CODE) [CODE] = #NAME,
4039 : : DWARF_ALL_KNOWN_DW_ORD
4040 : : #undef DWARF_ONE_KNOWN_DW_ORD
4041 : : };
4042 : :
4043 : 0 : if (likely (code < sizeof (known) / sizeof (known[0])))
4044 : 0 : return known[code];
4045 : :
4046 : : return NULL;
4047 : : }
4048 : :
4049 : :
4050 : : static const char *
4051 : 8 : dwarf_discr_list_string (unsigned int code)
4052 : : {
4053 : 8 : static const char *const known[] =
4054 : : {
4055 : : #define DWARF_ONE_KNOWN_DW_DSC(NAME, CODE) [CODE] = #NAME,
4056 : : DWARF_ALL_KNOWN_DW_DSC
4057 : : #undef DWARF_ONE_KNOWN_DW_DSC
4058 : : };
4059 : :
4060 : 8 : if (likely (code < sizeof (known) / sizeof (known[0])))
4061 : 8 : return known[code];
4062 : :
4063 : : return NULL;
4064 : : }
4065 : :
4066 : :
4067 : : static const char *
4068 : 615130 : dwarf_locexpr_opcode_string (unsigned int code)
4069 : : {
4070 : 615130 : static const char *const known[] =
4071 : : {
4072 : : /* Normally we can't afford building huge table of 64K entries,
4073 : : most of them zero, just because there are a couple defined
4074 : : values at the far end. In case of opcodes, it's OK. */
4075 : : #define DWARF_ONE_KNOWN_DW_OP(NAME, CODE) [CODE] = #NAME,
4076 : : DWARF_ALL_KNOWN_DW_OP
4077 : : #undef DWARF_ONE_KNOWN_DW_OP
4078 : : };
4079 : :
4080 : 615130 : if (likely (code < sizeof (known) / sizeof (known[0])))
4081 : 615130 : return known[code];
4082 : :
4083 : : return NULL;
4084 : : }
4085 : :
4086 : :
4087 : : static const char *
4088 : 8 : dwarf_unit_string (unsigned int type)
4089 : : {
4090 [ - + - - : 8 : switch (type)
- - + ]
4091 : : {
4092 : : #define DWARF_ONE_KNOWN_DW_UT(NAME, CODE) case CODE: return #NAME;
4093 : 7 : DWARF_ALL_KNOWN_DW_UT
4094 : : #undef DWARF_ONE_KNOWN_DW_UT
4095 : 0 : default:
4096 : 0 : return NULL;
4097 : : }
4098 : : }
4099 : :
4100 : :
4101 : : static const char *
4102 : 14 : dwarf_range_list_encoding_string (unsigned int kind)
4103 : : {
4104 [ - + + - : 14 : switch (kind)
+ - - -
+ ]
4105 : : {
4106 : : #define DWARF_ONE_KNOWN_DW_RLE(NAME, CODE) case CODE: return #NAME;
4107 : 12 : DWARF_ALL_KNOWN_DW_RLE
4108 : : #undef DWARF_ONE_KNOWN_DW_RLE
4109 : 0 : default:
4110 : 0 : return NULL;
4111 : : }
4112 : : }
4113 : :
4114 : :
4115 : : static const char *
4116 : 89 : dwarf_loc_list_encoding_string (unsigned int kind)
4117 : : {
4118 [ - - + + : 89 : switch (kind)
- + - + -
+ ]
4119 : : {
4120 : : #define DWARF_ONE_KNOWN_DW_LLE(NAME, CODE) case CODE: return #NAME;
4121 : 84 : DWARF_ALL_KNOWN_DW_LLE
4122 : : #undef DWARF_ONE_KNOWN_DW_LLE
4123 : 0 : default:
4124 : 0 : return NULL;
4125 : : }
4126 : : }
4127 : :
4128 : :
4129 : : static const char *
4130 : 6 : dwarf_line_content_description_string (unsigned int kind)
4131 : : {
4132 [ + + - - : 6 : switch (kind)
- - ]
4133 : : {
4134 : : #define DWARF_ONE_KNOWN_DW_LNCT(NAME, CODE) case CODE: return #NAME;
4135 : 6 : DWARF_ALL_KNOWN_DW_LNCT
4136 : : #undef DWARF_ONE_KNOWN_DW_LNCT
4137 : 0 : default:
4138 : 0 : return NULL;
4139 : : }
4140 : : }
4141 : :
4142 : :
4143 : : /* Used by all dwarf_foo_name functions. */
4144 : : static const char *
4145 : 9021083 : string_or_unknown (const char *known, unsigned int code,
4146 : : unsigned int lo_user, unsigned int hi_user,
4147 : : bool print_unknown_num)
4148 : : {
4149 : 9021083 : static char unknown_buf[20];
4150 : :
4151 [ - + ]: 9021083 : if (likely (known != NULL))
4152 : : return known;
4153 : :
4154 [ # # # # ]: 0 : if (lo_user != 0 && code >= lo_user && code <= hi_user)
4155 : : {
4156 : 0 : snprintf (unknown_buf, sizeof unknown_buf, "lo_user+%#x",
4157 : : code - lo_user);
4158 : 0 : return unknown_buf;
4159 : : }
4160 : :
4161 [ # # ]: 0 : if (print_unknown_num)
4162 : : {
4163 : 0 : snprintf (unknown_buf, sizeof unknown_buf, "??? (%#x)", code);
4164 : 0 : return unknown_buf;
4165 : : }
4166 : :
4167 : : return "???";
4168 : : }
4169 : :
4170 : :
4171 : : static const char *
4172 : 1021871 : dwarf_tag_name (unsigned int tag)
4173 : : {
4174 : 1021871 : const char *ret = dwarf_tag_string (tag);
4175 : 1021871 : return string_or_unknown (ret, tag, DW_TAG_lo_user, DW_TAG_hi_user, true);
4176 : : }
4177 : :
4178 : : static const char *
4179 : 3981776 : dwarf_attr_name (unsigned int attr)
4180 : : {
4181 : 3981776 : const char *ret = dwarf_attr_string (attr);
4182 : 3981776 : return string_or_unknown (ret, attr, DW_AT_lo_user, DW_AT_hi_user, true);
4183 : : }
4184 : :
4185 : :
4186 : : static const char *
4187 : 3981782 : dwarf_form_name (unsigned int form)
4188 : : {
4189 : 3981782 : const char *ret = dwarf_form_string (form);
4190 : 3981782 : return string_or_unknown (ret, form, 0, 0, true);
4191 : : }
4192 : :
4193 : :
4194 : : static const char *
4195 : 1700 : dwarf_lang_name (unsigned int lang)
4196 : : {
4197 : 1700 : const char *ret = dwarf_lang_string (lang);
4198 : 1700 : return string_or_unknown (ret, lang, DW_LANG_lo_user, DW_LANG_hi_user, false);
4199 : : }
4200 : :
4201 : :
4202 : : static const char *
4203 : 3557 : dwarf_inline_name (unsigned int code)
4204 : : {
4205 [ + - ]: 3557 : const char *ret = dwarf_inline_string (code);
4206 : 3557 : return string_or_unknown (ret, code, 0, 0, false);
4207 : : }
4208 : :
4209 : :
4210 : : static const char *
4211 : 30272 : dwarf_encoding_name (unsigned int code)
4212 : : {
4213 [ + - ]: 30272 : const char *ret = dwarf_encoding_string (code);
4214 : 30272 : return string_or_unknown (ret, code, DW_ATE_lo_user, DW_ATE_hi_user, false);
4215 : : }
4216 : :
4217 : :
4218 : : static const char *
4219 : 0 : dwarf_access_name (unsigned int code)
4220 : : {
4221 [ # # ]: 0 : const char *ret = dwarf_access_string (code);
4222 : 0 : return string_or_unknown (ret, code, 0, 0, false);
4223 : : }
4224 : :
4225 : :
4226 : : static const char *
4227 : 0 : dwarf_defaulted_name (unsigned int code)
4228 : : {
4229 [ # # ]: 0 : const char *ret = dwarf_defaulted_string (code);
4230 : 0 : return string_or_unknown (ret, code, 0, 0, false);
4231 : : }
4232 : :
4233 : :
4234 : : static const char *
4235 : 0 : dwarf_visibility_name (unsigned int code)
4236 : : {
4237 [ # # ]: 0 : const char *ret = dwarf_visibility_string (code);
4238 : 0 : return string_or_unknown (ret, code, 0, 0, false);
4239 : : }
4240 : :
4241 : :
4242 : : static const char *
4243 : 0 : dwarf_virtuality_name (unsigned int code)
4244 : : {
4245 [ # # ]: 0 : const char *ret = dwarf_virtuality_string (code);
4246 : 0 : return string_or_unknown (ret, code, 0, 0, false);
4247 : : }
4248 : :
4249 : :
4250 : : static const char *
4251 : 0 : dwarf_identifier_case_name (unsigned int code)
4252 : : {
4253 [ # # ]: 0 : const char *ret = dwarf_identifier_case_string (code);
4254 : 0 : return string_or_unknown (ret, code, 0, 0, false);
4255 : : }
4256 : :
4257 : :
4258 : : static const char *
4259 : 0 : dwarf_calling_convention_name (unsigned int code)
4260 : : {
4261 [ # # ]: 0 : const char *ret = dwarf_calling_convention_string (code);
4262 : 0 : return string_or_unknown (ret, code, DW_CC_lo_user, DW_CC_hi_user, false);
4263 : : }
4264 : :
4265 : :
4266 : : static const char *
4267 : 0 : dwarf_ordering_name (unsigned int code)
4268 : : {
4269 [ # # ]: 0 : const char *ret = dwarf_ordering_string (code);
4270 : 0 : return string_or_unknown (ret, code, 0, 0, false);
4271 : : }
4272 : :
4273 : :
4274 : : static const char *
4275 : 8 : dwarf_discr_list_name (unsigned int code)
4276 : : {
4277 [ + - ]: 8 : const char *ret = dwarf_discr_list_string (code);
4278 : 8 : return string_or_unknown (ret, code, 0, 0, false);
4279 : : }
4280 : :
4281 : :
4282 : : static const char *
4283 : 8 : dwarf_unit_name (unsigned int type)
4284 : : {
4285 : 8 : const char *ret = dwarf_unit_string (type);
4286 : 8 : return string_or_unknown (ret, type, DW_UT_lo_user, DW_UT_hi_user, true);
4287 : : }
4288 : :
4289 : :
4290 : : static const char *
4291 : 14 : dwarf_range_list_encoding_name (unsigned int kind)
4292 : : {
4293 : 14 : const char *ret = dwarf_range_list_encoding_string (kind);
4294 : 14 : return string_or_unknown (ret, kind, 0, 0, false);
4295 : : }
4296 : :
4297 : :
4298 : : static const char *
4299 : 89 : dwarf_loc_list_encoding_name (unsigned int kind)
4300 : : {
4301 : 89 : const char *ret = dwarf_loc_list_encoding_string (kind);
4302 : 89 : return string_or_unknown (ret, kind, 0, 0, false);
4303 : : }
4304 : :
4305 : :
4306 : : static const char *
4307 : 6 : dwarf_line_content_description_name (unsigned int kind)
4308 : : {
4309 : 6 : const char *ret = dwarf_line_content_description_string (kind);
4310 : 6 : return string_or_unknown (ret, kind, DW_LNCT_lo_user, DW_LNCT_hi_user,
4311 : : false);
4312 : : }
4313 : :
4314 : :
4315 : : static void
4316 : 174 : print_block (size_t n, const void *block)
4317 : : {
4318 [ - + ]: 174 : if (n == 0)
4319 : 0 : puts (_("empty block"));
4320 : : else
4321 : : {
4322 : 174 : printf (_("%zu byte block:"), n);
4323 : 174 : const unsigned char *data = block;
4324 : 775 : do
4325 : 775 : printf (" %02x", *data++);
4326 [ + + ]: 775 : while (--n > 0);
4327 : 174 : putchar ('\n');
4328 : : }
4329 : 174 : }
4330 : :
4331 : : static void
4332 : 0 : print_bytes (size_t n, const unsigned char *bytes)
4333 : : {
4334 : 0 : while (n-- > 0)
4335 : : {
4336 : 0 : printf ("%02x", *bytes++);
4337 [ # # ]: 0 : if (n > 0)
4338 [ # # ]: 0 : printf (" ");
4339 : : }
4340 : 0 : }
4341 : :
4342 : : static int
4343 : 68 : get_indexed_addr (Dwarf_CU *cu, Dwarf_Word idx, Dwarf_Addr *addr)
4344 : : {
4345 [ + - ]: 68 : if (cu == NULL)
4346 : : return -1;
4347 : :
4348 : 68 : Elf_Data *debug_addr = cu->dbg->sectiondata[IDX_debug_addr];
4349 [ + - ]: 68 : if (debug_addr == NULL)
4350 : : return -1;
4351 : :
4352 : 68 : Dwarf_Off base = __libdw_cu_addr_base (cu);
4353 : 68 : Dwarf_Word off = idx * cu->address_size;
4354 [ + - ]: 68 : if (base > debug_addr->d_size
4355 [ + - ]: 68 : || off > debug_addr->d_size - base
4356 [ + - ]: 68 : || cu->address_size > debug_addr->d_size - base - off)
4357 : : return -1;
4358 : :
4359 : 68 : const unsigned char *addrp = debug_addr->d_buf + base + off;
4360 [ - + ]: 68 : if (cu->address_size == 4)
4361 [ # # ]: 0 : *addr = read_4ubyte_unaligned (cu->dbg, addrp);
4362 : : else
4363 [ - + ]: 136 : *addr = read_8ubyte_unaligned (cu->dbg, addrp);
4364 : :
4365 : : return 0;
4366 : : }
4367 : :
4368 : : static void
4369 : 420348 : print_ops (Dwfl_Module *dwflmod, Dwarf *dbg, int indent, int indentrest,
4370 : : unsigned int vers, unsigned int addrsize, unsigned int offset_size,
4371 : : struct Dwarf_CU *cu, Dwarf_Word len, const unsigned char *data)
4372 : : {
4373 [ + + ]: 420348 : const unsigned int ref_size = vers < 3 ? addrsize : offset_size;
4374 : :
4375 [ - + ]: 420348 : if (len == 0)
4376 : : {
4377 : 0 : printf ("%*s(empty)\n", indent, "");
4378 : 0 : return;
4379 : : }
4380 : :
4381 : : #define NEED(n) if (len < (Dwarf_Word) (n)) goto invalid
4382 : : #define CONSUME(n) NEED (n); else len -= (n)
4383 : :
4384 : : Dwarf_Word offset = 0;
4385 [ + + ]: 1035478 : while (len-- > 0)
4386 : : {
4387 : 615130 : uint_fast8_t op = *data++;
4388 : :
4389 [ + - ]: 615130 : const char *op_name = dwarf_locexpr_opcode_string (op);
4390 [ - + ]: 615130 : if (unlikely (op_name == NULL))
4391 : : {
4392 : 0 : static char buf[20];
4393 [ # # ]: 0 : if (op >= DW_OP_lo_user)
4394 : 0 : snprintf (buf, sizeof buf, "lo_user+%#x", op - DW_OP_lo_user);
4395 : : else
4396 : 0 : snprintf (buf, sizeof buf, "??? (%#x)", op);
4397 : : op_name = buf;
4398 : : }
4399 : :
4400 [ + - + + : 615130 : switch (op)
+ - + + -
- + + - +
- - + + +
+ + - - -
- + + + ]
4401 : : {
4402 : 12583 : case DW_OP_addr:;
4403 : : /* Address operand. */
4404 : 12583 : Dwarf_Word addr;
4405 [ - + ]: 12583 : NEED (addrsize);
4406 [ + + ]: 12583 : if (addrsize == 4)
4407 [ + + ]: 48 : addr = read_4ubyte_unaligned (dbg, data);
4408 [ + - ]: 12559 : else if (addrsize == 8)
4409 [ + + ]: 25118 : addr = read_8ubyte_unaligned (dbg, data);
4410 : : else
4411 : 0 : goto invalid;
4412 : 12583 : data += addrsize;
4413 : 12583 : CONSUME (addrsize);
4414 : :
4415 : 12583 : printf ("%*s[%2" PRIuMAX "] %s ",
4416 : : indent, "", (uintmax_t) offset, op_name);
4417 : 12583 : print_dwarf_addr (dwflmod, 0, addr, addr);
4418 : 12583 : printf ("\n");
4419 : :
4420 : 12583 : offset += 1 + addrsize;
4421 : 12583 : break;
4422 : :
4423 : 0 : case DW_OP_call_ref:
4424 : : case DW_OP_GNU_variable_value:
4425 : : /* Offset operand. */
4426 [ # # ]: 0 : if (ref_size != 4 && ref_size != 8)
4427 : 0 : goto invalid; /* Cannot be used in CFA. */
4428 [ # # ]: 0 : NEED (ref_size);
4429 [ # # ]: 0 : if (ref_size == 4)
4430 [ # # ]: 0 : addr = read_4ubyte_unaligned (dbg, data);
4431 : : else
4432 [ # # ]: 0 : addr = read_8ubyte_unaligned (dbg, data);
4433 : 0 : data += ref_size;
4434 : 0 : CONSUME (ref_size);
4435 : : /* addr is a DIE offset, so format it as one. */
4436 : 0 : printf ("%*s[%2" PRIuMAX "] %s [%6" PRIxMAX "]\n",
4437 : : indent, "", (uintmax_t) offset,
4438 : : op_name, (uintmax_t) addr);
4439 : 0 : offset += 1 + ref_size;
4440 : 0 : break;
4441 : :
4442 : 16326 : case DW_OP_deref_size:
4443 : : case DW_OP_xderef_size:
4444 : : case DW_OP_pick:
4445 : : case DW_OP_const1u:
4446 : : // XXX value might be modified by relocation
4447 [ - + ]: 16326 : NEED (1);
4448 : 32652 : printf ("%*s[%2" PRIuMAX "] %s %" PRIu8 "\n",
4449 : : indent, "", (uintmax_t) offset,
4450 : 16326 : op_name, *((uint8_t *) data));
4451 : 16326 : ++data;
4452 : 16326 : --len;
4453 : 16326 : offset += 2;
4454 : 16326 : break;
4455 : :
4456 : 2502 : case DW_OP_const2u:
4457 [ - + ]: 2502 : NEED (2);
4458 : : // XXX value might be modified by relocation
4459 : 7506 : printf ("%*s[%2" PRIuMAX "] %s %" PRIu16 "\n",
4460 : : indent, "", (uintmax_t) offset,
4461 [ - + ]: 5004 : op_name, read_2ubyte_unaligned (dbg, data));
4462 : 2502 : CONSUME (2);
4463 : 2502 : data += 2;
4464 : 2502 : offset += 3;
4465 : 2502 : break;
4466 : :
4467 : 1913 : case DW_OP_const4u:
4468 [ - + ]: 1913 : NEED (4);
4469 : : // XXX value might be modified by relocation
4470 [ - + ]: 3826 : printf ("%*s[%2" PRIuMAX "] %s %" PRIu32 "\n",
4471 : : indent, "", (uintmax_t) offset,
4472 : : op_name, read_4ubyte_unaligned (dbg, data));
4473 : 1913 : CONSUME (4);
4474 : 1913 : data += 4;
4475 : 1913 : offset += 5;
4476 : 1913 : break;
4477 : :
4478 : 0 : case DW_OP_const8u:
4479 [ # # ]: 0 : NEED (8);
4480 : : // XXX value might be modified by relocation
4481 : 0 : printf ("%*s[%2" PRIuMAX "] %s %" PRIu64 "\n",
4482 : : indent, "", (uintmax_t) offset,
4483 [ # # ]: 0 : op_name, (uint64_t) read_8ubyte_unaligned (dbg, data));
4484 : 0 : CONSUME (8);
4485 : 0 : data += 8;
4486 : 0 : offset += 9;
4487 : 0 : break;
4488 : :
4489 : 2983 : case DW_OP_const1s:
4490 [ - + ]: 2983 : NEED (1);
4491 : : // XXX value might be modified by relocation
4492 : 5966 : printf ("%*s[%2" PRIuMAX "] %s %" PRId8 "\n",
4493 : : indent, "", (uintmax_t) offset,
4494 : 2983 : op_name, *((int8_t *) data));
4495 : 2983 : ++data;
4496 : 2983 : --len;
4497 : 2983 : offset += 2;
4498 : 2983 : break;
4499 : :
4500 : 10 : case DW_OP_const2s:
4501 [ - + ]: 10 : NEED (2);
4502 : : // XXX value might be modified by relocation
4503 : 30 : printf ("%*s[%2" PRIuMAX "] %s %" PRId16 "\n",
4504 : : indent, "", (uintmax_t) offset,
4505 [ - + ]: 20 : op_name, read_2sbyte_unaligned (dbg, data));
4506 : 10 : CONSUME (2);
4507 : 10 : data += 2;
4508 : 10 : offset += 3;
4509 : 10 : break;
4510 : :
4511 : 0 : case DW_OP_const4s:
4512 [ # # ]: 0 : NEED (4);
4513 : : // XXX value might be modified by relocation
4514 [ # # ]: 0 : printf ("%*s[%2" PRIuMAX "] %s %" PRId32 "\n",
4515 : : indent, "", (uintmax_t) offset,
4516 : : op_name, read_4sbyte_unaligned (dbg, data));
4517 : 0 : CONSUME (4);
4518 : 0 : data += 4;
4519 : 0 : offset += 5;
4520 : 0 : break;
4521 : :
4522 : 0 : case DW_OP_const8s:
4523 [ # # ]: 0 : NEED (8);
4524 : : // XXX value might be modified by relocation
4525 [ # # ]: 0 : printf ("%*s[%2" PRIuMAX "] %s %" PRId64 "\n",
4526 : : indent, "", (uintmax_t) offset,
4527 : : op_name, read_8sbyte_unaligned (dbg, data));
4528 : 0 : CONSUME (8);
4529 : 0 : data += 8;
4530 : 0 : offset += 9;
4531 : 0 : break;
4532 : :
4533 : 7342 : case DW_OP_piece:
4534 : : case DW_OP_regx:
4535 : : case DW_OP_plus_uconst:
4536 : 7342 : case DW_OP_constu:;
4537 : 7342 : const unsigned char *start = data;
4538 : 7342 : uint64_t uleb;
4539 [ - + ]: 7342 : NEED (1);
4540 : 7342 : get_uleb128 (uleb, data, data + len);
4541 : 7342 : printf ("%*s[%2" PRIuMAX "] %s %" PRIu64 "\n",
4542 : : indent, "", (uintmax_t) offset, op_name, uleb);
4543 [ - + ]: 7342 : CONSUME (data - start);
4544 : 7342 : offset += 1 + (data - start);
4545 : 7342 : break;
4546 : :
4547 : 2 : case DW_OP_addrx:
4548 : : case DW_OP_GNU_addr_index:
4549 : : case DW_OP_constx:
4550 : 2 : case DW_OP_GNU_const_index:;
4551 : 2 : start = data;
4552 [ - + ]: 2 : NEED (1);
4553 : 2 : get_uleb128 (uleb, data, data + len);
4554 : 2 : printf ("%*s[%2" PRIuMAX "] %s [%" PRIu64 "] ",
4555 : : indent, "", (uintmax_t) offset, op_name, uleb);
4556 [ - + ]: 2 : CONSUME (data - start);
4557 : 2 : offset += 1 + (data - start);
4558 [ - + ]: 2 : if (get_indexed_addr (cu, uleb, &addr) != 0)
4559 : 0 : printf ("???\n");
4560 : : else
4561 : : {
4562 : 2 : print_dwarf_addr (dwflmod, 0, addr, addr);
4563 : 2 : printf ("\n");
4564 : : }
4565 : : break;
4566 : :
4567 : 0 : case DW_OP_bit_piece:
4568 : 0 : start = data;
4569 : 0 : uint64_t uleb2;
4570 [ # # ]: 0 : NEED (1);
4571 : 0 : get_uleb128 (uleb, data, data + len);
4572 : 0 : NEED (1);
4573 : 0 : get_uleb128 (uleb2, data, data + len);
4574 : 0 : printf ("%*s[%2" PRIuMAX "] %s %" PRIu64 ", %" PRIu64 "\n",
4575 : : indent, "", (uintmax_t) offset, op_name, uleb, uleb2);
4576 [ # # ]: 0 : CONSUME (data - start);
4577 : 0 : offset += 1 + (data - start);
4578 : 0 : break;
4579 : :
4580 : 94856 : case DW_OP_fbreg:
4581 : : case DW_OP_breg0 ... DW_OP_breg31:
4582 : : case DW_OP_consts:
4583 : 94856 : start = data;
4584 : 94856 : int64_t sleb;
4585 [ - + ]: 94856 : NEED (1);
4586 : 94856 : get_sleb128 (sleb, data, data + len);
4587 : 94856 : printf ("%*s[%2" PRIuMAX "] %s %" PRId64 "\n",
4588 : : indent, "", (uintmax_t) offset, op_name, sleb);
4589 [ - + ]: 94856 : CONSUME (data - start);
4590 : 94856 : offset += 1 + (data - start);
4591 : 94856 : break;
4592 : :
4593 : 0 : case DW_OP_bregx:
4594 : 0 : start = data;
4595 [ # # ]: 0 : NEED (1);
4596 : 0 : get_uleb128 (uleb, data, data + len);
4597 : 0 : NEED (1);
4598 : 0 : get_sleb128 (sleb, data, data + len);
4599 : 0 : printf ("%*s[%2" PRIuMAX "] %s %" PRIu64 " %" PRId64 "\n",
4600 : : indent, "", (uintmax_t) offset, op_name, uleb, sleb);
4601 [ # # ]: 0 : CONSUME (data - start);
4602 : 0 : offset += 1 + (data - start);
4603 : 0 : break;
4604 : :
4605 : 0 : case DW_OP_call2:
4606 [ # # ]: 0 : NEED (2);
4607 : 0 : printf ("%*s[%2" PRIuMAX "] %s [%6" PRIx16 "]\n",
4608 : : indent, "", (uintmax_t) offset, op_name,
4609 [ # # ]: 0 : read_2ubyte_unaligned (dbg, data));
4610 : 0 : CONSUME (2);
4611 : 0 : data += 2;
4612 : 0 : offset += 3;
4613 : 0 : break;
4614 : :
4615 : 4 : case DW_OP_call4:
4616 [ - + ]: 4 : NEED (4);
4617 [ - + ]: 8 : printf ("%*s[%2" PRIuMAX "] %s [%6" PRIx32 "]\n",
4618 : : indent, "", (uintmax_t) offset, op_name,
4619 : : read_4ubyte_unaligned (dbg, data));
4620 : 4 : CONSUME (4);
4621 : 4 : data += 4;
4622 : 4 : offset += 5;
4623 : 4 : break;
4624 : :
4625 : 213 : case DW_OP_skip:
4626 : : case DW_OP_bra:
4627 [ - + ]: 213 : NEED (2);
4628 : 426 : printf ("%*s[%2" PRIuMAX "] %s %" PRIuMAX "\n",
4629 : : indent, "", (uintmax_t) offset, op_name,
4630 [ - + ]: 426 : (uintmax_t) (offset + read_2sbyte_unaligned (dbg, data) + 3));
4631 : 213 : CONSUME (2);
4632 : 213 : data += 2;
4633 : 213 : offset += 3;
4634 : 213 : break;
4635 : :
4636 : 174 : case DW_OP_implicit_value:
4637 : 174 : start = data;
4638 [ - + ]: 174 : NEED (1);
4639 : 174 : get_uleb128 (uleb, data, data + len);
4640 : 174 : printf ("%*s[%2" PRIuMAX "] %s: ",
4641 : : indent, "", (uintmax_t) offset, op_name);
4642 [ - + ]: 174 : NEED (uleb);
4643 : 174 : print_block (uleb, data);
4644 : 174 : data += uleb;
4645 [ - + ]: 174 : CONSUME (data - start);
4646 : 174 : offset += 1 + (data - start);
4647 : 174 : break;
4648 : :
4649 : 2601 : case DW_OP_implicit_pointer:
4650 : : case DW_OP_GNU_implicit_pointer:
4651 : : /* DIE offset operand. */
4652 : 2601 : start = data;
4653 [ - + ]: 2601 : NEED (ref_size);
4654 [ - + ]: 2601 : if (ref_size != 4 && ref_size != 8)
4655 : 0 : goto invalid; /* Cannot be used in CFA. */
4656 [ + - ]: 2601 : if (ref_size == 4)
4657 [ - + ]: 5202 : addr = read_4ubyte_unaligned (dbg, data);
4658 : : else
4659 [ # # ]: 0 : addr = read_8ubyte_unaligned (dbg, data);
4660 : 2601 : data += ref_size;
4661 : : /* Byte offset operand. */
4662 [ - + ]: 2601 : NEED (1);
4663 : 2601 : get_sleb128 (sleb, data, data + len);
4664 : :
4665 : 2601 : printf ("%*s[%2" PRIuMAX "] %s [%6" PRIxMAX "] %+" PRId64 "\n",
4666 : : indent, "", (intmax_t) offset,
4667 : : op_name, (uintmax_t) addr, sleb);
4668 [ - + ]: 2601 : CONSUME (data - start);
4669 : 2601 : offset += 1 + (data - start);
4670 : 2601 : break;
4671 : :
4672 : 41522 : case DW_OP_entry_value:
4673 : : case DW_OP_GNU_entry_value:
4674 : : /* Size plus expression block. */
4675 : 41522 : start = data;
4676 [ - + ]: 41522 : NEED (1);
4677 : 41522 : get_uleb128 (uleb, data, data + len);
4678 : 41522 : printf ("%*s[%2" PRIuMAX "] %s:\n",
4679 : : indent, "", (uintmax_t) offset, op_name);
4680 [ - + ]: 41522 : NEED (uleb);
4681 : 41522 : print_ops (dwflmod, dbg, indent + 5, indent + 5, vers,
4682 : : addrsize, offset_size, cu, uleb, data);
4683 : 41522 : data += uleb;
4684 [ - + ]: 41522 : CONSUME (data - start);
4685 : 41522 : offset += 1 + (data - start);
4686 : 41522 : break;
4687 : :
4688 : 0 : case DW_OP_const_type:
4689 : : case DW_OP_GNU_const_type:
4690 : : /* uleb128 CU relative DW_TAG_base_type DIE offset, 1-byte
4691 : : unsigned size plus block. */
4692 : 0 : start = data;
4693 [ # # ]: 0 : NEED (1);
4694 : 0 : get_uleb128 (uleb, data, data + len);
4695 [ # # # # ]: 0 : if (! print_unresolved_addresses && cu != NULL)
4696 : 0 : uleb += cu->start;
4697 : 0 : NEED (1);
4698 : 0 : uint8_t usize = *(uint8_t *) data++;
4699 [ # # ]: 0 : NEED (usize);
4700 : 0 : printf ("%*s[%2" PRIuMAX "] %s [%6" PRIxMAX "] ",
4701 : : indent, "", (uintmax_t) offset, op_name, uleb);
4702 : 0 : print_block (usize, data);
4703 : 0 : data += usize;
4704 [ # # ]: 0 : CONSUME (data - start);
4705 : 0 : offset += 1 + (data - start);
4706 : 0 : break;
4707 : :
4708 : 0 : case DW_OP_regval_type:
4709 : : case DW_OP_GNU_regval_type:
4710 : : /* uleb128 register number, uleb128 CU relative
4711 : : DW_TAG_base_type DIE offset. */
4712 : 0 : start = data;
4713 [ # # ]: 0 : NEED (1);
4714 : 0 : get_uleb128 (uleb, data, data + len);
4715 : 0 : NEED (1);
4716 : 0 : get_uleb128 (uleb2, data, data + len);
4717 [ # # # # ]: 0 : if (! print_unresolved_addresses && cu != NULL)
4718 : 0 : uleb2 += cu->start;
4719 : 0 : printf ("%*s[%2" PRIuMAX "] %s %" PRIu64 " [%6" PRIx64 "]\n",
4720 : : indent, "", (uintmax_t) offset, op_name, uleb, uleb2);
4721 [ # # ]: 0 : CONSUME (data - start);
4722 : 0 : offset += 1 + (data - start);
4723 : 0 : break;
4724 : :
4725 : 0 : case DW_OP_deref_type:
4726 : : case DW_OP_GNU_deref_type:
4727 : : /* 1-byte unsigned size of value, uleb128 CU relative
4728 : : DW_TAG_base_type DIE offset. */
4729 : 0 : start = data;
4730 [ # # ]: 0 : NEED (1);
4731 : 0 : usize = *(uint8_t *) data++;
4732 : 0 : NEED (1);
4733 : 0 : get_uleb128 (uleb, data, data + len);
4734 [ # # # # ]: 0 : if (! print_unresolved_addresses && cu != NULL)
4735 : 0 : uleb += cu->start;
4736 : 0 : printf ("%*s[%2" PRIuMAX "] %s %" PRIu8 " [%6" PRIxMAX "]\n",
4737 : : indent, "", (uintmax_t) offset,
4738 : : op_name, usize, uleb);
4739 [ # # ]: 0 : CONSUME (data - start);
4740 : 0 : offset += 1 + (data - start);
4741 : 0 : break;
4742 : :
4743 : 0 : case DW_OP_xderef_type:
4744 : : /* 1-byte unsigned size of value, uleb128 base_type DIE offset. */
4745 : 0 : start = data;
4746 [ # # ]: 0 : NEED (1);
4747 : 0 : usize = *(uint8_t *) data++;
4748 : 0 : NEED (1);
4749 : 0 : get_uleb128 (uleb, data, data + len);
4750 : 0 : printf ("%*s[%4" PRIuMAX "] %s %" PRIu8 " [%6" PRIxMAX "]\n",
4751 : : indent, "", (uintmax_t) offset,
4752 : : op_name, usize, uleb);
4753 [ # # ]: 0 : CONSUME (data - start);
4754 : 0 : offset += 1 + (data - start);
4755 : 0 : break;
4756 : :
4757 : 396 : case DW_OP_convert:
4758 : : case DW_OP_GNU_convert:
4759 : : case DW_OP_reinterpret:
4760 : : case DW_OP_GNU_reinterpret:
4761 : : /* uleb128 CU relative offset to DW_TAG_base_type, or zero
4762 : : for conversion to untyped. */
4763 : 396 : start = data;
4764 [ - + ]: 396 : NEED (1);
4765 : 396 : get_uleb128 (uleb, data, data + len);
4766 [ + + + - : 396 : if (uleb != 0 && ! print_unresolved_addresses && cu != NULL)
+ - ]
4767 : 264 : uleb += cu->start;
4768 : 396 : printf ("%*s[%2" PRIuMAX "] %s [%6" PRIxMAX "]\n",
4769 : : indent, "", (uintmax_t) offset, op_name, uleb);
4770 [ - + ]: 396 : CONSUME (data - start);
4771 : 396 : offset += 1 + (data - start);
4772 : 396 : break;
4773 : :
4774 : 3 : case DW_OP_GNU_parameter_ref:
4775 : : /* 4 byte CU relative reference to the abstract optimized away
4776 : : DW_TAG_formal_parameter. */
4777 [ - + ]: 3 : NEED (4);
4778 [ - + ]: 3 : uintmax_t param_off = (uintmax_t) read_4ubyte_unaligned (dbg, data);
4779 [ + - + - ]: 3 : if (! print_unresolved_addresses && cu != NULL)
4780 : 3 : param_off += cu->start;
4781 : 3 : printf ("%*s[%2" PRIuMAX "] %s [%6" PRIxMAX "]\n",
4782 : : indent, "", (uintmax_t) offset, op_name, param_off);
4783 : 3 : CONSUME (4);
4784 : 3 : data += 4;
4785 : 3 : offset += 5;
4786 : 3 : break;
4787 : :
4788 : : default:
4789 : : /* No Operand. */
4790 : 431700 : printf ("%*s[%2" PRIuMAX "] %s\n",
4791 : : indent, "", (uintmax_t) offset, op_name);
4792 : 431700 : ++offset;
4793 : 431700 : break;
4794 : : }
4795 : :
4796 : 615130 : indent = indentrest;
4797 : 615130 : continue;
4798 : :
4799 : 0 : invalid:
4800 : 0 : printf (_("%*s[%2" PRIuMAX "] %s <TRUNCATED>\n"),
4801 : : indent, "", (uintmax_t) offset, op_name);
4802 : : break;
4803 : : }
4804 : : }
4805 : :
4806 : :
4807 : : struct listptr
4808 : : {
4809 : : Dwarf_Off offset:(64 - 3);
4810 : : bool addr64:1;
4811 : : bool dwarf64:1;
4812 : : bool warned:1;
4813 : : struct Dwarf_CU *cu;
4814 : : unsigned int attr;
4815 : : };
4816 : :
4817 : : #define listptr_offset_size(p) ((p)->dwarf64 ? 8 : 4)
4818 : : #define listptr_address_size(p) ((p)->addr64 ? 8 : 4)
4819 : :
4820 : : static Dwarf_Addr
4821 : 126824 : cudie_base (Dwarf_Die *cudie)
4822 : : {
4823 : 126824 : Dwarf_Addr base;
4824 : : /* Find the base address of the compilation unit. It will normally
4825 : : be specified by DW_AT_low_pc. In DWARF-3 draft 4, the base
4826 : : address could be overridden by DW_AT_entry_pc. It's been
4827 : : removed, but GCC emits DW_AT_entry_pc and not DW_AT_lowpc for
4828 : : compilation units with discontinuous ranges. */
4829 [ - + ]: 126824 : if (unlikely (dwarf_lowpc (cudie, &base) != 0))
4830 : : {
4831 : 0 : Dwarf_Attribute attr_mem;
4832 [ # # ]: 0 : if (dwarf_formaddr (dwarf_attr (cudie, DW_AT_entry_pc, &attr_mem),
4833 : : &base) != 0)
4834 : 0 : base = 0;
4835 : : }
4836 : 126824 : return base;
4837 : : }
4838 : :
4839 : : static Dwarf_Addr
4840 : 126824 : listptr_base (struct listptr *p)
4841 : : {
4842 : 126824 : Dwarf_Die cu = CUDIE (p->cu);
4843 : 126824 : return cudie_base (&cu);
4844 : : }
4845 : :
4846 : : /* To store the name used in compare_listptr */
4847 : : static const char *sort_listptr_name;
4848 : :
4849 : : static int
4850 : 849767 : compare_listptr (const void *a, const void *b)
4851 : : {
4852 : 849767 : const char *name = sort_listptr_name;
4853 : 849767 : struct listptr *p1 = (void *) a;
4854 : 849767 : struct listptr *p2 = (void *) b;
4855 : :
4856 [ + + ]: 849767 : if (p1->offset < p2->offset)
4857 : : return -1;
4858 [ + + ]: 81652 : if (p1->offset > p2->offset)
4859 : : return 1;
4860 : :
4861 [ + - + - ]: 3714 : if (!p1->warned && !p2->warned)
4862 : : {
4863 [ - + ]: 3714 : if (p1->addr64 != p2->addr64)
4864 : : {
4865 : 0 : p1->warned = p2->warned = true;
4866 : 3714 : error (0, 0,
4867 : 0 : _("%s %#" PRIx64 " used with different address sizes"),
4868 : 0 : name, (uint64_t) p1->offset);
4869 : : }
4870 [ - + ]: 3714 : if (p1->dwarf64 != p2->dwarf64)
4871 : : {
4872 : 0 : p1->warned = p2->warned = true;
4873 : 3714 : error (0, 0,
4874 : 0 : _("%s %#" PRIx64 " used with different offset sizes"),
4875 : 0 : name, (uint64_t) p1->offset);
4876 : : }
4877 [ - + ]: 3714 : if (listptr_base (p1) != listptr_base (p2))
4878 : : {
4879 : 0 : p1->warned = p2->warned = true;
4880 : 3714 : error (0, 0,
4881 : 0 : _("%s %#" PRIx64 " used with different base addresses"),
4882 : 0 : name, (uint64_t) p1->offset);
4883 : : }
4884 [ - + ]: 3714 : if (p1->attr != p2 ->attr)
4885 : : {
4886 : 0 : p1->warned = p2->warned = true;
4887 : 0 : error (0, 0,
4888 : 0 : _("%s %#" PRIx64
4889 : : " used with different attribute %s and %s"),
4890 : 0 : name, (uint64_t) p1->offset, dwarf_attr_name (p2->attr),
4891 : : dwarf_attr_name (p2->attr));
4892 : : }
4893 : : }
4894 : :
4895 : : return 0;
4896 : : }
4897 : :
4898 : : struct listptr_table
4899 : : {
4900 : : size_t n;
4901 : : size_t alloc;
4902 : : struct listptr *table;
4903 : : };
4904 : :
4905 : : static struct listptr_table known_locsptr;
4906 : : static struct listptr_table known_loclistsptr;
4907 : : static struct listptr_table known_rangelistptr;
4908 : : static struct listptr_table known_rnglistptr;
4909 : : static struct listptr_table known_addrbases;
4910 : : static struct listptr_table known_stroffbases;
4911 : :
4912 : : static void
4913 : 209 : reset_listptr (struct listptr_table *table)
4914 : : {
4915 : 209 : free (table->table);
4916 : 209 : table->table = NULL;
4917 : 209 : table->n = table->alloc = 0;
4918 : : }
4919 : :
4920 : : /* Returns false if offset doesn't fit. See struct listptr. */
4921 : : static bool
4922 : 123038 : notice_listptr (enum section_e section, struct listptr_table *table,
4923 : : uint_fast8_t address_size, uint_fast8_t offset_size,
4924 : : struct Dwarf_CU *cu, Dwarf_Off offset, unsigned int attr)
4925 : : {
4926 [ + + ]: 123038 : if (print_debug_sections & section)
4927 : : {
4928 [ + + ]: 122912 : if (table->n == table->alloc)
4929 : : {
4930 [ + + ]: 190 : if (table->alloc == 0)
4931 : 79 : table->alloc = 128;
4932 : : else
4933 : 111 : table->alloc *= 2;
4934 : 190 : table->table = xrealloc (table->table,
4935 : 190 : table->alloc * sizeof table->table[0]);
4936 : : }
4937 : :
4938 : 122912 : struct listptr *p = &table->table[table->n++];
4939 : :
4940 : 122912 : *p = (struct listptr)
4941 : : {
4942 : 122912 : .addr64 = address_size == 8,
4943 : 122912 : .dwarf64 = offset_size == 8,
4944 : : .offset = offset,
4945 : : .cu = cu,
4946 : : .attr = attr
4947 : : };
4948 : :
4949 [ - + ]: 122912 : if (p->offset != offset)
4950 : : {
4951 : 0 : table->n--;
4952 : 0 : return false;
4953 : : }
4954 : : }
4955 : : return true;
4956 : : }
4957 : :
4958 : : static void
4959 : 83 : sort_listptr (struct listptr_table *table, const char *name)
4960 : : {
4961 [ + + ]: 83 : if (table->n > 0)
4962 : : {
4963 : 79 : sort_listptr_name = name;
4964 : 79 : qsort (table->table, table->n, sizeof table->table[0],
4965 : : &compare_listptr);
4966 : : }
4967 : 83 : }
4968 : :
4969 : : static bool
4970 : 119395 : skip_listptr_hole (struct listptr_table *table, size_t *idxp,
4971 : : uint_fast8_t *address_sizep, uint_fast8_t *offset_sizep,
4972 : : Dwarf_Addr *base, struct Dwarf_CU **cu, ptrdiff_t offset,
4973 : : unsigned char **readp, unsigned char *endp,
4974 : : unsigned int *attr)
4975 : : {
4976 [ + - ]: 119395 : if (table->n == 0)
4977 : : return false;
4978 : :
4979 [ + - + + ]: 188916 : while (*idxp < table->n && table->table[*idxp].offset < (Dwarf_Off) offset)
4980 : 69521 : ++*idxp;
4981 : :
4982 : 119395 : struct listptr *p = &table->table[*idxp];
4983 : :
4984 [ + - ]: 119395 : if (*idxp == table->n
4985 [ - + ]: 119395 : || p->offset >= (Dwarf_Off) (endp - *readp + offset))
4986 : : {
4987 : 0 : *readp = endp;
4988 : 0 : printf (_(" [%6tx] <UNUSED GARBAGE IN REST OF SECTION>\n"),
4989 : : offset);
4990 : 0 : return true;
4991 : : }
4992 : :
4993 [ + + ]: 119395 : if (p->offset != (Dwarf_Off) offset)
4994 : : {
4995 : 5 : *readp += p->offset - offset;
4996 : 5 : printf (_(" [%6tx] <UNUSED GARBAGE> ... %" PRIu64 " bytes ...\n"),
4997 : 5 : offset, (Dwarf_Off) p->offset - offset);
4998 : 5 : return true;
4999 : : }
5000 : :
5001 [ + - ]: 119390 : if (address_sizep != NULL)
5002 [ + + ]: 119394 : *address_sizep = listptr_address_size (p);
5003 [ + + ]: 119390 : if (offset_sizep != NULL)
5004 [ + - ]: 211652 : *offset_sizep = listptr_offset_size (p);
5005 [ + - ]: 119390 : if (base != NULL)
5006 : 119390 : *base = listptr_base (p);
5007 [ + - ]: 119390 : if (cu != NULL)
5008 : 119390 : *cu = p->cu;
5009 [ + + ]: 119390 : if (attr != NULL)
5010 : 105826 : *attr = p->attr;
5011 : :
5012 : : return false;
5013 : : }
5014 : :
5015 : : static Dwarf_Off
5016 : 52876 : next_listptr_offset (struct listptr_table *table, size_t *idxp, Dwarf_Off off)
5017 : : {
5018 : : /* Note that multiple attributes could in theory point to the same loclist
5019 : : offset, so make sure we pick one that is bigger than the current one.
5020 : : The table is sorted on offset. */
5021 [ + - ]: 52876 : if (*idxp < table->n)
5022 : : {
5023 [ + - ]: 53292 : while (++*idxp < table->n)
5024 : : {
5025 : 53292 : Dwarf_Off next = table->table[*idxp].offset;
5026 [ + + ]: 53292 : if (next > off)
5027 : 52876 : return next;
5028 : : }
5029 : : }
5030 : : return 0;
5031 : : }
5032 : :
5033 : : /* Returns the listptr associated with the given index, or NULL. */
5034 : : static struct listptr *
5035 : 351 : get_listptr (struct listptr_table *table, size_t idx)
5036 : : {
5037 : 351 : if (idx >= table->n)
5038 : : return NULL;
5039 : 300 : return &table->table[idx];
5040 : : }
5041 : :
5042 : : /* Returns the next index, base address and CU associated with the
5043 : : list unit offsets. If there is none false is returned, otherwise
5044 : : true. Assumes the table has been sorted. */
5045 : : static bool
5046 : 6 : listptr_cu (struct listptr_table *table, size_t *idxp,
5047 : : Dwarf_Off start, Dwarf_Off end,
5048 : : Dwarf_Addr *base, struct Dwarf_CU **cu)
5049 : : {
5050 [ + - ]: 15 : while (*idxp < table->n
5051 [ + + ]: 15 : && table->table[*idxp].offset < start)
5052 : 9 : ++*idxp;
5053 : :
5054 [ + - ]: 6 : if (*idxp < table->n
5055 [ + - ]: 6 : && table->table[*idxp].offset >= start
5056 [ + - ]: 6 : && table->table[*idxp].offset < end)
5057 : : {
5058 : 6 : struct listptr *p = &table->table[*idxp];
5059 : 6 : *base = listptr_base (p);
5060 : 6 : *cu = p->cu;
5061 : 6 : return true;
5062 : : }
5063 : :
5064 : : return false;
5065 : : }
5066 : :
5067 : : /* Returns the next index with the current CU for the given attribute.
5068 : : If there is none false is returned, otherwise true. Assumes the
5069 : : table has been sorted. */
5070 : : static bool
5071 : 89 : listptr_attr (struct listptr_table *table, size_t idxp,
5072 : : Dwarf_Off offset, unsigned int attr)
5073 : : {
5074 : 341 : struct listptr *listptr;
5075 : 341 : do
5076 : : {
5077 [ + + ]: 341 : listptr = get_listptr (table, idxp);
5078 [ + - ]: 295 : if (listptr == NULL)
5079 : : return false;
5080 : :
5081 [ + + + - ]: 295 : if (listptr->offset == offset && listptr->attr == attr)
5082 : : return true;
5083 : :
5084 : 295 : idxp++;
5085 : : }
5086 [ + + ]: 295 : while (listptr->offset <= offset);
5087 : :
5088 : : return false;
5089 : : }
5090 : :
5091 : : static void
5092 : 40 : print_debug_abbrev_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
5093 : : Ebl *ebl, GElf_Ehdr *ehdr __attribute__ ((unused)),
5094 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
5095 : : {
5096 : 80 : const size_t sh_size = (dbg->sectiondata[IDX_debug_abbrev] ?
5097 [ + - ]: 40 : dbg->sectiondata[IDX_debug_abbrev]->d_size : 0);
5098 : :
5099 : 40 : printf (_("\nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"
5100 : : " [ Code]\n"),
5101 : : elf_ndxscn (scn), section_name (ebl, shdr),
5102 : 40 : (uint64_t) shdr->sh_offset);
5103 : :
5104 : 40 : Dwarf_Off offset = 0;
5105 [ + + ]: 1672 : while (offset < sh_size)
5106 : : {
5107 : 1632 : printf (_("\nAbbreviation section at offset %" PRIu64 ":\n"),
5108 : : offset);
5109 : :
5110 : 147856 : while (1)
5111 : 73112 : {
5112 : 74744 : size_t length;
5113 : 74744 : Dwarf_Abbrev abbrev;
5114 : :
5115 : 74744 : int res = dwarf_offabbrev (dbg, offset, &length, &abbrev);
5116 [ + + ]: 74744 : if (res != 0)
5117 : : {
5118 [ - + ]: 1632 : if (unlikely (res < 0))
5119 : : {
5120 : 0 : printf (_("\
5121 : : *** error while reading abbreviation: %s\n"),
5122 : : dwarf_errmsg (-1));
5123 : 0 : return;
5124 : : }
5125 : :
5126 : : /* This is the NUL byte at the end of the section. */
5127 : 1632 : ++offset;
5128 : 1632 : break;
5129 : : }
5130 : :
5131 : : /* We know these calls can never fail. */
5132 : 73112 : unsigned int code = dwarf_getabbrevcode (&abbrev);
5133 : 73112 : unsigned int tag = dwarf_getabbrevtag (&abbrev);
5134 : 73112 : int has_children = dwarf_abbrevhaschildren (&abbrev);
5135 : :
5136 [ + + ]: 73112 : printf (_(" [%5u] offset: %" PRId64
5137 : : ", children: %s, tag: %s\n"),
5138 : : code, (int64_t) offset,
5139 : : has_children ? yes_str : no_str,
5140 : : dwarf_tag_name (tag));
5141 : :
5142 : 73112 : size_t cnt = 0;
5143 : 73112 : unsigned int name;
5144 : 73112 : unsigned int form;
5145 : 73112 : Dwarf_Sword data;
5146 : 73112 : Dwarf_Off enoffset;
5147 [ + + ]: 412997 : while (dwarf_getabbrevattr_data (&abbrev, cnt, &name, &form,
5148 : : &data, &enoffset) == 0)
5149 : : {
5150 : 339885 : printf (" attr: %s, form: %s",
5151 : : dwarf_attr_name (name), dwarf_form_name (form));
5152 [ - + ]: 339885 : if (form == DW_FORM_implicit_const)
5153 : 0 : printf (" (%" PRId64 ")", data);
5154 : 339885 : printf (", offset: %#" PRIx64 "\n", (uint64_t) enoffset);
5155 : 339885 : ++cnt;
5156 : : }
5157 : :
5158 : 73112 : offset += length;
5159 : : }
5160 : : }
5161 : : }
5162 : :
5163 : :
5164 : : static void
5165 : 2 : print_debug_addr_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
5166 : : Ebl *ebl, GElf_Ehdr *ehdr,
5167 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
5168 : : {
5169 : 2 : printf (_("\
5170 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
5171 : : elf_ndxscn (scn), section_name (ebl, shdr),
5172 : 2 : (uint64_t) shdr->sh_offset);
5173 : :
5174 [ + - ]: 2 : if (shdr->sh_size == 0)
5175 : : return;
5176 : :
5177 : : /* We like to get the section from libdw to make sure they are relocated. */
5178 : 4 : Elf_Data *data = (dbg->sectiondata[IDX_debug_addr]
5179 [ - + ]: 2 : ?: elf_rawdata (scn, NULL));
5180 [ - + ]: 2 : if (unlikely (data == NULL))
5181 : : {
5182 : 0 : error (0, 0, _("cannot get .debug_addr section data: %s"),
5183 : : elf_errmsg (-1));
5184 : 0 : return;
5185 : : }
5186 : :
5187 : 2 : size_t idx = 0;
5188 : 2 : sort_listptr (&known_addrbases, "addr_base");
5189 : :
5190 : 2 : const unsigned char *start = (const unsigned char *) data->d_buf;
5191 : 2 : const unsigned char *readp = start;
5192 : 2 : const unsigned char *readendp = ((const unsigned char *) data->d_buf
5193 : 2 : + data->d_size);
5194 : :
5195 : 2 : while (readp < readendp)
5196 : : {
5197 : : /* We cannot really know whether or not there is an header. The
5198 : : DebugFission extension to DWARF4 doesn't add one. The DWARF5
5199 : : .debug_addr variant does. Whether or not we have an header,
5200 : : DW_AT_[GNU_]addr_base points at "index 0". So if the current
5201 : : offset equals the CU addr_base then we can just start
5202 : : printing addresses. If there is no CU with an exact match
5203 : : then we'll try to parse the header first. */
5204 : 4 : Dwarf_Off off = (Dwarf_Off) (readp
5205 : 4 : - (const unsigned char *) data->d_buf);
5206 : :
5207 : 4 : printf ("Table at offset %" PRIx64 " ", off);
5208 : :
5209 [ + - ]: 4 : struct listptr *listptr = get_listptr (&known_addrbases, idx++);
5210 : 4 : const unsigned char *next_unitp;
5211 : :
5212 : 4 : uint64_t unit_length;
5213 : 4 : uint16_t version;
5214 : 4 : uint8_t address_size;
5215 : 4 : uint8_t segment_size;
5216 [ - + ]: 4 : if (listptr == NULL)
5217 : : {
5218 : 0 : error (0, 0, "Warning: No CU references .debug_addr after %" PRIx64,
5219 : : off);
5220 : :
5221 : : /* We will have to assume it is just addresses to the end... */
5222 [ # # ]: 0 : address_size = ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 4 : 8;
5223 : 0 : next_unitp = readendp;
5224 : 0 : printf ("Unknown CU:\n");
5225 : : }
5226 : : else
5227 : : {
5228 : 4 : Dwarf_Die cudie;
5229 [ - + ]: 4 : if (dwarf_cu_die (listptr->cu, &cudie,
5230 : : NULL, NULL, NULL, NULL,
5231 : : NULL, NULL) == NULL)
5232 : 0 : printf ("Unknown CU (%s):\n", dwarf_errmsg (-1));
5233 : : else
5234 : 4 : printf ("for CU [%6" PRIx64 "]:\n", dwarf_dieoffset (&cudie));
5235 : :
5236 [ + + ]: 4 : if (listptr->offset == off)
5237 : : {
5238 [ - + ]: 2 : address_size = listptr_address_size (listptr);
5239 : 2 : segment_size = 0;
5240 : 2 : version = 4;
5241 : :
5242 : : /* The addresses start here, but where do they end? */
5243 [ + + ]: 2 : listptr = get_listptr (&known_addrbases, idx);
5244 [ + - ]: 1 : if (listptr == NULL)
5245 : : next_unitp = readendp;
5246 [ + - ]: 1 : else if (listptr->cu->version < 5)
5247 : : {
5248 : 1 : next_unitp = start + listptr->offset;
5249 [ + - - + ]: 1 : if (listptr->offset < off || listptr->offset > data->d_size)
5250 : : {
5251 : 0 : error (0, 0,
5252 : : "Warning: Bad address base for next unit at %"
5253 : : PRIx64, off);
5254 : 0 : next_unitp = readendp;
5255 : : }
5256 : : }
5257 : : else
5258 : : {
5259 : : /* Tricky, we don't have a header for this unit, but
5260 : : there is one for the next. We will have to
5261 : : "guess" how big it is and subtract it from the
5262 : : offset (because that points after the header). */
5263 [ # # ]: 0 : unsigned int offset_size = listptr_offset_size (listptr);
5264 : 0 : Dwarf_Off next_off = (listptr->offset
5265 : 0 : - (offset_size == 4 ? 4 : 12) /* len */
5266 : : - 2 /* version */
5267 : : - 1 /* address size */
5268 : 0 : - 1); /* segment selector size */
5269 : 0 : next_unitp = start + next_off;
5270 [ # # # # ]: 0 : if (next_off < off || next_off > data->d_size)
5271 : : {
5272 : 0 : error (0, 0,
5273 : : "Warning: Couldn't calculate .debug_addr "
5274 : : " unit length at %" PRIx64, off);
5275 : 0 : next_unitp = readendp;
5276 : : }
5277 : : }
5278 : 2 : unit_length = (uint64_t) (next_unitp - readp);
5279 : :
5280 : : /* Pretend we have a header. */
5281 : 2 : printf ("\n");
5282 : 2 : printf (_(" Length: %8" PRIu64 "\n"),
5283 : : unit_length);
5284 : 2 : printf (_(" DWARF version: %8" PRIu16 "\n"), version);
5285 : 2 : printf (_(" Address size: %8" PRIu64 "\n"),
5286 : : (uint64_t) address_size);
5287 : 2 : printf (_(" Segment size: %8" PRIu64 "\n"),
5288 : : (uint64_t) segment_size);
5289 : 4 : printf ("\n");
5290 : : }
5291 : : else
5292 : : {
5293 : : /* OK, we have to parse an header first. */
5294 [ - + ]: 2 : unit_length = read_4ubyte_unaligned_inc (dbg, readp);
5295 [ - + ]: 2 : if (unlikely (unit_length == 0xffffffff))
5296 : : {
5297 [ # # ]: 0 : if (unlikely (readp > readendp - 8))
5298 : : {
5299 : 0 : invalid_data:
5300 : 0 : error (0, 0, "Invalid data");
5301 : 0 : return;
5302 : : }
5303 [ # # ]: 0 : unit_length = read_8ubyte_unaligned_inc (dbg, readp);
5304 : : }
5305 : 2 : printf ("\n");
5306 : 2 : printf (_(" Length: %8" PRIu64 "\n"),
5307 : : unit_length);
5308 : :
5309 : : /* We need at least 2-bytes (version) + 1-byte
5310 : : (addr_size) + 1-byte (segment_size) = 4 bytes to
5311 : : complete the header. And this unit cannot go beyond
5312 : : the section data. */
5313 [ + - ]: 2 : if (readp > readendp - 4
5314 [ + - ]: 2 : || unit_length < 4
5315 [ - + ]: 2 : || unit_length > (uint64_t) (readendp - readp))
5316 : 0 : goto invalid_data;
5317 : :
5318 : 2 : next_unitp = readp + unit_length;
5319 : :
5320 [ - + ]: 2 : version = read_2ubyte_unaligned_inc (dbg, readp);
5321 : 2 : printf (_(" DWARF version: %8" PRIu16 "\n"), version);
5322 : :
5323 [ - + ]: 2 : if (version != 5)
5324 : : {
5325 : 0 : error (0, 0, _("Unknown version"));
5326 : 0 : goto next_unit;
5327 : : }
5328 : :
5329 : 2 : address_size = *readp++;
5330 : 2 : printf (_(" Address size: %8" PRIu64 "\n"),
5331 : : (uint64_t) address_size);
5332 : :
5333 [ - + ]: 2 : if (address_size != 4 && address_size != 8)
5334 : : {
5335 : 0 : error (0, 0, _("unsupported address size"));
5336 : 0 : goto next_unit;
5337 : : }
5338 : :
5339 : 2 : segment_size = *readp++;
5340 : 2 : printf (_(" Segment size: %8" PRIu64 "\n"),
5341 : : (uint64_t) segment_size);
5342 : 2 : printf ("\n");
5343 : :
5344 [ - + ]: 2 : if (segment_size != 0)
5345 : : {
5346 : 0 : error (0, 0, _("unsupported segment size"));
5347 : 0 : goto next_unit;
5348 : : }
5349 : :
5350 [ - + ]: 2 : if (listptr->offset != (Dwarf_Off) (readp - start))
5351 : : {
5352 : 0 : error (0, 0, "Address index doesn't start after header");
5353 : 0 : goto next_unit;
5354 : : }
5355 : : }
5356 : : }
5357 : :
5358 : 4 : int digits = 1;
5359 : 4 : size_t addresses = (next_unitp - readp) / address_size;
5360 [ + + ]: 8 : while (addresses >= 10)
5361 : : {
5362 : 4 : ++digits;
5363 : 4 : addresses /= 10;
5364 : : }
5365 : :
5366 : 4 : unsigned int uidx = 0;
5367 : 4 : size_t index_offset = readp - (const unsigned char *) data->d_buf;
5368 : 4 : printf (" Addresses start at offset 0x%zx:\n", index_offset);
5369 : 76 : while (readp <= next_unitp - address_size)
5370 : : {
5371 [ - + - + : 144 : Dwarf_Addr addr = read_addr_unaligned_inc (address_size, dbg,
- - - + ]
5372 : : readp);
5373 : 72 : printf (" [%*u] ", digits, uidx++);
5374 : 72 : print_dwarf_addr (dwflmod, address_size, addr, addr);
5375 [ + + ]: 76 : printf ("\n");
5376 : : }
5377 : 4 : printf ("\n");
5378 : :
5379 [ + - ]: 4 : if (readp != next_unitp)
5380 [ + + ]: 6 : error (0, 0, "extra %zd bytes at end of unit",
5381 : 0 : (size_t) (next_unitp - readp));
5382 : :
5383 : 4 : next_unit:
5384 : : readp = next_unitp;
5385 : : }
5386 : : }
5387 : :
5388 : : /* Print content of DWARF .debug_aranges section. We fortunately do
5389 : : not have to know a bit about the structure of the section, libdwarf
5390 : : takes care of it. */
5391 : : static void
5392 : 1 : print_decoded_aranges_section (Ebl *ebl, GElf_Ehdr *ehdr, Elf_Scn *scn,
5393 : : GElf_Shdr *shdr, Dwarf *dbg)
5394 : : {
5395 : 1 : Dwarf_Aranges *aranges;
5396 : 1 : size_t cnt;
5397 [ - + ]: 1 : if (unlikely (dwarf_getaranges (dbg, &aranges, &cnt) != 0))
5398 : : {
5399 : 0 : error (0, 0, _("cannot get .debug_aranges content: %s"),
5400 : : dwarf_errmsg (-1));
5401 : 0 : return;
5402 : : }
5403 : :
5404 : 1 : GElf_Shdr glink_mem;
5405 : 1 : GElf_Shdr *glink;
5406 : 1 : glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link), &glink_mem);
5407 [ - + ]: 1 : if (glink == NULL)
5408 : : {
5409 : 0 : error (0, 0, _("invalid sh_link value in section %zu"),
5410 : : elf_ndxscn (scn));
5411 : 0 : return;
5412 : : }
5413 : :
5414 : 1 : printf (ngettext ("\
5415 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 " contains %zu entry:\n",
5416 : : "\
5417 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 " contains %zu entries:\n",
5418 : : cnt),
5419 : : elf_ndxscn (scn), section_name (ebl, shdr),
5420 : 1 : (uint64_t) shdr->sh_offset, cnt);
5421 : :
5422 : : /* Compute floor(log16(cnt)). */
5423 : 1 : size_t tmp = cnt;
5424 : 1 : int digits = 1;
5425 [ - + ]: 1 : while (tmp >= 16)
5426 : : {
5427 : 0 : ++digits;
5428 : 0 : tmp >>= 4;
5429 : : }
5430 : :
5431 [ + + ]: 6 : for (size_t n = 0; n < cnt; ++n)
5432 : : {
5433 : 5 : Dwarf_Arange *runp = dwarf_onearange (aranges, n);
5434 [ - + ]: 5 : if (unlikely (runp == NULL))
5435 : : {
5436 : 0 : printf ("cannot get arange %zu: %s\n", n, dwarf_errmsg (-1));
5437 : 0 : return;
5438 : : }
5439 : :
5440 : 5 : Dwarf_Addr start;
5441 : 5 : Dwarf_Word length;
5442 : 5 : Dwarf_Off offset;
5443 : :
5444 [ - + ]: 5 : if (unlikely (dwarf_getarangeinfo (runp, &start, &length, &offset) != 0))
5445 : 0 : printf (_(" [%*zu] ???\n"), digits, n);
5446 : : else
5447 : 5 : printf (_(" [%*zu] start: %0#*" PRIx64
5448 : : ", length: %5" PRIu64 ", CU DIE offset: %6"
5449 : : PRId64 "\n"),
5450 [ - + ]: 5 : digits, n, ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 10 : 18,
5451 : : (uint64_t) start, (uint64_t) length, (int64_t) offset);
5452 : : }
5453 : : }
5454 : :
5455 : :
5456 : : /* Print content of DWARF .debug_aranges section. */
5457 : : static void
5458 : 44 : print_debug_aranges_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
5459 : : Ebl *ebl, GElf_Ehdr *ehdr, Elf_Scn *scn,
5460 : : GElf_Shdr *shdr, Dwarf *dbg)
5461 : : {
5462 [ + + ]: 44 : if (decodedaranges)
5463 : : {
5464 : 1 : print_decoded_aranges_section (ebl, ehdr, scn, shdr, dbg);
5465 : 1 : return;
5466 : : }
5467 : :
5468 : 86 : Elf_Data *data = (dbg->sectiondata[IDX_debug_aranges]
5469 [ - + ]: 43 : ?: elf_rawdata (scn, NULL));
5470 : :
5471 [ - + ]: 43 : if (unlikely (data == NULL))
5472 : : {
5473 : 0 : error (0, 0, _("cannot get .debug_aranges content: %s"),
5474 : : elf_errmsg (-1));
5475 : 0 : return;
5476 : : }
5477 : :
5478 : 43 : printf (_("\
5479 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
5480 : : elf_ndxscn (scn), section_name (ebl, shdr),
5481 : 43 : (uint64_t) shdr->sh_offset);
5482 : :
5483 : 43 : const unsigned char *readp = data->d_buf;
5484 : 43 : const unsigned char *readendp = readp + data->d_size;
5485 : :
5486 [ + + ]: 1672 : while (readp < readendp)
5487 : : {
5488 : 1629 : const unsigned char *hdrstart = readp;
5489 : 1629 : size_t start_offset = hdrstart - (const unsigned char *) data->d_buf;
5490 : :
5491 : 1629 : printf (_("\nTable at offset %zu:\n"), start_offset);
5492 [ - + ]: 1629 : if (readp + 4 > readendp)
5493 : : {
5494 : 0 : invalid_data:
5495 : 0 : error (0, 0, _("invalid data in section [%zu] '%s'"),
5496 : : elf_ndxscn (scn), section_name (ebl, shdr));
5497 : 0 : return;
5498 : : }
5499 : :
5500 [ + + ]: 1629 : Dwarf_Word length = read_4ubyte_unaligned_inc (dbg, readp);
5501 : 1629 : unsigned int length_bytes = 4;
5502 [ - + ]: 1629 : if (length == DWARF3_LENGTH_64_BIT)
5503 : : {
5504 [ # # ]: 0 : if (readp + 8 > readendp)
5505 : 0 : goto invalid_data;
5506 [ # # ]: 0 : length = read_8ubyte_unaligned_inc (dbg, readp);
5507 : 0 : length_bytes = 8;
5508 : : }
5509 : :
5510 : 1629 : const unsigned char *nexthdr = readp + length;
5511 : 1629 : printf (_("\n Length: %6" PRIu64 "\n"),
5512 : : (uint64_t) length);
5513 : :
5514 [ - + ]: 1629 : if (unlikely (length > (size_t) (readendp - readp)))
5515 : 0 : goto invalid_data;
5516 : :
5517 [ - + ]: 1629 : if (length == 0)
5518 : 0 : continue;
5519 : :
5520 [ - + ]: 1629 : if (readp + 2 > readendp)
5521 : 0 : goto invalid_data;
5522 [ + + ]: 1629 : uint_fast16_t version = read_2ubyte_unaligned_inc (dbg, readp);
5523 : 1629 : printf (_(" DWARF version: %6" PRIuFAST16 "\n"),
5524 : : version);
5525 [ - + ]: 1629 : if (version != 2)
5526 : : {
5527 : 0 : error (0, 0, _("unsupported aranges version"));
5528 : 0 : goto next_table;
5529 : : }
5530 : :
5531 : 1629 : Dwarf_Word offset;
5532 [ - + ]: 1629 : if (readp + length_bytes > readendp)
5533 : 0 : goto invalid_data;
5534 [ - + ]: 1629 : if (length_bytes == 8)
5535 [ # # ]: 0 : offset = read_8ubyte_unaligned_inc (dbg, readp);
5536 : : else
5537 [ + + ]: 3258 : offset = read_4ubyte_unaligned_inc (dbg, readp);
5538 : 1629 : printf (_(" CU offset: %6" PRIx64 "\n"),
5539 : : (uint64_t) offset);
5540 : :
5541 [ - + ]: 1629 : if (readp + 1 > readendp)
5542 : 0 : goto invalid_data;
5543 : 1629 : unsigned int address_size = *readp++;
5544 : 1629 : printf (_(" Address size: %6" PRIu64 "\n"),
5545 : : (uint64_t) address_size);
5546 [ - + ]: 1629 : if (address_size != 4 && address_size != 8)
5547 : : {
5548 : 0 : error (0, 0, _("unsupported address size"));
5549 : 0 : goto next_table;
5550 : : }
5551 : :
5552 [ - + ]: 1629 : if (readp + 1 > readendp)
5553 : 0 : goto invalid_data;
5554 : 1629 : unsigned int segment_size = *readp++;
5555 : 1629 : printf (_(" Segment size: %6" PRIu64 "\n\n"),
5556 : : (uint64_t) segment_size);
5557 [ - + - - ]: 1629 : if (segment_size != 0 && segment_size != 4 && segment_size != 8)
5558 : : {
5559 : 0 : error (0, 0, _("unsupported segment size"));
5560 : 0 : goto next_table;
5561 : : }
5562 : :
5563 : : /* Round the address to the next multiple of 2*address_size. */
5564 : 1629 : readp += ((2 * address_size - ((readp - hdrstart) % (2 * address_size)))
5565 : 1629 : % (2 * address_size));
5566 : :
5567 : 1629 : while (readp < nexthdr)
5568 : : {
5569 : 3275 : Dwarf_Word range_address;
5570 : 3275 : Dwarf_Word range_length;
5571 : 3275 : Dwarf_Word segment = 0;
5572 [ - + ]: 3275 : if (readp + 2 * address_size + segment_size > readendp)
5573 : 0 : goto invalid_data;
5574 [ + + ]: 3275 : if (address_size == 4)
5575 : : {
5576 [ + + ]: 36 : range_address = read_4ubyte_unaligned_inc (dbg, readp);
5577 [ + + ]: 72 : range_length = read_4ubyte_unaligned_inc (dbg, readp);
5578 : : }
5579 : : else
5580 : : {
5581 [ + + ]: 3239 : range_address = read_8ubyte_unaligned_inc (dbg, readp);
5582 [ + + ]: 6478 : range_length = read_8ubyte_unaligned_inc (dbg, readp);
5583 : : }
5584 : :
5585 [ - + ]: 3275 : if (segment_size == 4)
5586 [ # # ]: 0 : segment = read_4ubyte_unaligned_inc (dbg, readp);
5587 [ - + ]: 3275 : else if (segment_size == 8)
5588 [ # # ]: 0 : segment = read_8ubyte_unaligned_inc (dbg, readp);
5589 : :
5590 [ + + ]: 3275 : if (range_address == 0 && range_length == 0 && segment == 0)
5591 : : break;
5592 : :
5593 : 1646 : printf (" ");
5594 : 1646 : print_dwarf_addr (dwflmod, address_size, range_address,
5595 : : range_address);
5596 : 1646 : printf ("..");
5597 : 1646 : print_dwarf_addr (dwflmod, address_size,
5598 : 1646 : range_address + range_length - 1,
5599 : : range_length);
5600 [ - + ]: 1646 : if (segment_size != 0)
5601 : 0 : printf (" (%" PRIx64 ")\n", (uint64_t) segment);
5602 : : else
5603 [ + - ]: 4921 : printf ("\n");
5604 : : }
5605 : :
5606 : 1629 : next_table:
5607 [ - + ]: 1629 : if (readp != nexthdr)
5608 : : {
5609 : 0 : size_t padding = nexthdr - readp;
5610 : 0 : printf (_(" %zu padding bytes\n"), padding);
5611 : 0 : readp = nexthdr;
5612 : : }
5613 : : }
5614 : : }
5615 : :
5616 : :
5617 : : static bool is_split_dwarf (Dwarf *dbg, uint64_t *id, Dwarf_CU **split_cu);
5618 : :
5619 : : /* Returns true and sets cu and cu_base if the given Dwarf is a split
5620 : : DWARF (.dwo) file. */
5621 : : static bool
5622 : 0 : split_dwarf_cu_base (Dwarf *dbg, Dwarf_CU **cu, Dwarf_Addr *cu_base)
5623 : : {
5624 : 0 : uint64_t id;
5625 [ # # ]: 0 : if (is_split_dwarf (dbg, &id, cu))
5626 : : {
5627 : 0 : Dwarf_Die cudie;
5628 [ # # ]: 0 : if (dwarf_cu_info (*cu, NULL, NULL, &cudie, NULL, NULL, NULL, NULL) == 0)
5629 : : {
5630 : 0 : *cu_base = cudie_base (&cudie);
5631 : 0 : return true;
5632 : : }
5633 : : }
5634 : : return false;
5635 : : }
5636 : :
5637 : : /* Print content of DWARF .debug_rnglists section. */
5638 : : static void
5639 : 2 : print_debug_rnglists_section (Dwfl_Module *dwflmod,
5640 : : Ebl *ebl,
5641 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
5642 : : Elf_Scn *scn, GElf_Shdr *shdr,
5643 : : Dwarf *dbg __attribute__((unused)))
5644 : : {
5645 : 2 : printf (_("\
5646 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
5647 : : elf_ndxscn (scn), section_name (ebl, shdr),
5648 : 2 : (uint64_t) shdr->sh_offset);
5649 : :
5650 : 4 : Elf_Data *data =(dbg->sectiondata[IDX_debug_rnglists]
5651 [ - + ]: 2 : ?: elf_rawdata (scn, NULL));
5652 [ - + ]: 2 : if (unlikely (data == NULL))
5653 : : {
5654 : 0 : error (0, 0, _("cannot get .debug_rnglists content: %s"),
5655 : : elf_errmsg (-1));
5656 : 0 : return;
5657 : : }
5658 : :
5659 : : /* For the listptr to get the base address/CU. */
5660 : 2 : sort_listptr (&known_rnglistptr, "rnglistptr");
5661 : 2 : size_t listptr_idx = 0;
5662 : :
5663 : 2 : const unsigned char *readp = data->d_buf;
5664 : 2 : const unsigned char *const dataend = ((unsigned char *) data->d_buf
5665 : 2 : + data->d_size);
5666 [ + + ]: 4 : while (readp < dataend)
5667 : : {
5668 [ - + ]: 2 : if (unlikely (readp > dataend - 4))
5669 : : {
5670 : 0 : invalid_data:
5671 : 0 : error (0, 0, _("invalid data in section [%zu] '%s'"),
5672 : : elf_ndxscn (scn), section_name (ebl, shdr));
5673 : 0 : return;
5674 : : }
5675 : :
5676 : 2 : ptrdiff_t offset = readp - (unsigned char *) data->d_buf;
5677 : 2 : printf (_("Table at Offset 0x%" PRIx64 ":\n\n"),
5678 : : (uint64_t) offset);
5679 : :
5680 [ - + ]: 2 : uint64_t unit_length = read_4ubyte_unaligned_inc (dbg, readp);
5681 : 2 : unsigned int offset_size = 4;
5682 [ - + ]: 2 : if (unlikely (unit_length == 0xffffffff))
5683 : : {
5684 [ # # ]: 0 : if (unlikely (readp > dataend - 8))
5685 : 0 : goto invalid_data;
5686 : :
5687 [ # # ]: 0 : unit_length = read_8ubyte_unaligned_inc (dbg, readp);
5688 : 0 : offset_size = 8;
5689 : : }
5690 : 2 : printf (_(" Length: %8" PRIu64 "\n"), unit_length);
5691 : :
5692 : : /* We need at least 2-bytes + 1-byte + 1-byte + 4-bytes = 8
5693 : : bytes to complete the header. And this unit cannot go beyond
5694 : : the section data. */
5695 [ + - ]: 2 : if (readp > dataend - 8
5696 [ + - ]: 2 : || unit_length < 8
5697 [ - + ]: 2 : || unit_length > (uint64_t) (dataend - readp))
5698 : 0 : goto invalid_data;
5699 : :
5700 : 2 : const unsigned char *nexthdr = readp + unit_length;
5701 : :
5702 [ - + ]: 2 : uint16_t version = read_2ubyte_unaligned_inc (dbg, readp);
5703 : 2 : printf (_(" DWARF version: %8" PRIu16 "\n"), version);
5704 : :
5705 [ - + ]: 2 : if (version != 5)
5706 : : {
5707 : 0 : error (0, 0, _("Unknown version"));
5708 : 0 : goto next_table;
5709 : : }
5710 : :
5711 : 2 : uint8_t address_size = *readp++;
5712 : 2 : printf (_(" Address size: %8" PRIu64 "\n"),
5713 : : (uint64_t) address_size);
5714 : :
5715 [ - + ]: 2 : if (address_size != 4 && address_size != 8)
5716 : : {
5717 : 0 : error (0, 0, _("unsupported address size"));
5718 : 0 : goto next_table;
5719 : : }
5720 : :
5721 : 2 : uint8_t segment_size = *readp++;
5722 : 2 : printf (_(" Segment size: %8" PRIu64 "\n"),
5723 : : (uint64_t) segment_size);
5724 : :
5725 [ - + - - ]: 2 : if (segment_size != 0 && segment_size != 4 && segment_size != 8)
5726 : : {
5727 : 0 : error (0, 0, _("unsupported segment size"));
5728 : 0 : goto next_table;
5729 : : }
5730 : :
5731 [ - + ]: 2 : uint32_t offset_entry_count = read_4ubyte_unaligned_inc (dbg, readp);
5732 : 2 : printf (_(" Offset entries: %8" PRIu64 "\n"),
5733 : : (uint64_t) offset_entry_count);
5734 : :
5735 : : /* We need the CU that uses this unit to get the initial base address. */
5736 : 2 : Dwarf_Addr cu_base = 0;
5737 : 2 : struct Dwarf_CU *cu = NULL;
5738 [ - + ]: 2 : if (listptr_cu (&known_rnglistptr, &listptr_idx,
5739 : : (Dwarf_Off) offset,
5740 : 2 : (Dwarf_Off) (nexthdr - (unsigned char *) data->d_buf),
5741 : : &cu_base, &cu)
5742 [ # # ]: 0 : || split_dwarf_cu_base (dbg, &cu, &cu_base))
5743 : 2 : {
5744 : 2 : Dwarf_Die cudie;
5745 [ - + ]: 2 : if (dwarf_cu_die (cu, &cudie,
5746 : : NULL, NULL, NULL, NULL,
5747 : : NULL, NULL) == NULL)
5748 : 0 : printf (_(" Unknown CU base: "));
5749 : : else
5750 : 2 : printf (_(" CU [%6" PRIx64 "] base: "),
5751 : : dwarf_dieoffset (&cudie));
5752 : 2 : print_dwarf_addr (dwflmod, address_size, cu_base, cu_base);
5753 : 2 : printf ("\n");
5754 : : }
5755 : : else
5756 : 0 : printf (_(" Not associated with a CU.\n"));
5757 : :
5758 : 2 : printf ("\n");
5759 : :
5760 : 2 : const unsigned char *offset_array_start = readp;
5761 [ + + ]: 2 : if (offset_entry_count > 0)
5762 : : {
5763 : 1 : uint64_t max_entries = (unit_length - 8) / offset_size;
5764 [ - + ]: 1 : if (offset_entry_count > max_entries)
5765 : : {
5766 : 0 : error (0, 0,
5767 : 0 : _("too many offset entries for unit length"));
5768 : 0 : offset_entry_count = max_entries;
5769 : : }
5770 : :
5771 : 1 : printf (_(" Offsets starting at 0x%" PRIx64 ":\n"),
5772 : : (uint64_t) (offset_array_start
5773 : 1 : - (unsigned char *) data->d_buf));
5774 [ + + ]: 3 : for (uint32_t idx = 0; idx < offset_entry_count; idx++)
5775 : : {
5776 : 2 : printf (" [%6" PRIu32 "] ", idx);
5777 [ + - ]: 2 : if (offset_size == 4)
5778 : : {
5779 [ - + ]: 2 : uint32_t off = read_4ubyte_unaligned_inc (dbg, readp);
5780 : 2 : printf ("0x%" PRIx32 "\n", off);
5781 : : }
5782 : : else
5783 : : {
5784 [ # # ]: 0 : uint64_t off = read_8ubyte_unaligned_inc (dbg, readp);
5785 : 2 : printf ("0x%" PRIx64 "\n", off);
5786 : : }
5787 : : }
5788 : 1 : printf ("\n");
5789 : : }
5790 : :
5791 : 2 : Dwarf_Addr base = cu_base;
5792 : 2 : bool start_of_list = true;
5793 : 2 : while (readp < nexthdr)
5794 : : {
5795 : 14 : uint8_t kind = *readp++;
5796 : 14 : uint64_t op1, op2;
5797 : :
5798 : : /* Skip padding. */
5799 [ - + ]: 14 : if (start_of_list && kind == DW_RLE_end_of_list)
5800 : 0 : continue;
5801 : :
5802 [ + + ]: 14 : if (start_of_list)
5803 : : {
5804 : 4 : base = cu_base;
5805 : 4 : printf (" Offset: %" PRIx64 ", Index: %" PRIx64 "\n",
5806 : 4 : (uint64_t) (readp - (unsigned char *) data->d_buf - 1),
5807 : 4 : (uint64_t) (readp - offset_array_start - 1));
5808 : 4 : start_of_list = false;
5809 : : }
5810 : :
5811 : 14 : printf (" %s", dwarf_range_list_encoding_name (kind));
5812 [ + - - - : 14 : switch (kind)
+ + - +
- ]
5813 : : {
5814 : 4 : case DW_RLE_end_of_list:
5815 : 4 : start_of_list = true;
5816 : 4 : printf ("\n\n");
5817 : : break;
5818 : :
5819 : 0 : case DW_RLE_base_addressx:
5820 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
5821 : : {
5822 : 0 : invalid_range:
5823 : 0 : error (0, 0, _("invalid range list data"));
5824 : 0 : goto next_table;
5825 : : }
5826 : 0 : get_uleb128 (op1, readp, nexthdr);
5827 : 0 : printf (" %" PRIx64 "\n", op1);
5828 [ # # ]: 0 : if (! print_unresolved_addresses)
5829 : : {
5830 : 0 : Dwarf_Addr addr;
5831 [ # # ]: 0 : if (get_indexed_addr (cu, op1, &addr) != 0)
5832 : 0 : printf (" ???\n");
5833 : : else
5834 : : {
5835 : 0 : printf (" ");
5836 : 0 : print_dwarf_addr (dwflmod, address_size, addr, addr);
5837 : 0 : printf ("\n");
5838 : : }
5839 : : }
5840 : : break;
5841 : :
5842 : 0 : case DW_RLE_startx_endx:
5843 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
5844 : 0 : goto invalid_range;
5845 : 0 : get_uleb128 (op1, readp, nexthdr);
5846 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
5847 : 0 : goto invalid_range;
5848 : 0 : get_uleb128 (op2, readp, nexthdr);
5849 : 0 : printf (" %" PRIx64 ", %" PRIx64 "\n", op1, op2);
5850 [ # # ]: 0 : if (! print_unresolved_addresses)
5851 : : {
5852 : 0 : Dwarf_Addr addr1;
5853 : 0 : Dwarf_Addr addr2;
5854 [ # # ]: 0 : if (get_indexed_addr (cu, op1, &addr1) != 0
5855 [ # # ]: 0 : || get_indexed_addr (cu, op2, &addr2) != 0)
5856 : : {
5857 : 0 : printf (" ???..\n");
5858 : 0 : printf (" ???\n");
5859 : : }
5860 : : else
5861 : : {
5862 : 0 : printf (" ");
5863 : 0 : print_dwarf_addr (dwflmod, address_size, addr1, addr1);
5864 : 0 : printf ("..\n ");
5865 : 0 : print_dwarf_addr (dwflmod, address_size,
5866 : : addr2 - 1, addr2);
5867 : 0 : printf ("\n");
5868 : : }
5869 : : }
5870 : : break;
5871 : :
5872 : 0 : case DW_RLE_startx_length:
5873 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
5874 : 0 : goto invalid_range;
5875 : 0 : get_uleb128 (op1, readp, nexthdr);
5876 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
5877 : 0 : goto invalid_range;
5878 : 0 : get_uleb128 (op2, readp, nexthdr);
5879 : 0 : printf (" %" PRIx64 ", %" PRIx64 "\n", op1, op2);
5880 [ # # ]: 0 : if (! print_unresolved_addresses)
5881 : : {
5882 : 0 : Dwarf_Addr addr1;
5883 : 0 : Dwarf_Addr addr2;
5884 [ # # ]: 0 : if (get_indexed_addr (cu, op1, &addr1) != 0)
5885 : : {
5886 : 0 : printf (" ???..\n");
5887 : 0 : printf (" ???\n");
5888 : : }
5889 : : else
5890 : : {
5891 : 0 : addr2 = addr1 + op2;
5892 : 0 : printf (" ");
5893 : 0 : print_dwarf_addr (dwflmod, address_size, addr1, addr1);
5894 : 0 : printf ("..\n ");
5895 : 0 : print_dwarf_addr (dwflmod, address_size,
5896 : : addr2 - 1, addr2);
5897 : 0 : printf ("\n");
5898 : : }
5899 : : }
5900 : : break;
5901 : :
5902 : 4 : case DW_RLE_offset_pair:
5903 [ - + ]: 4 : if ((uint64_t) (nexthdr - readp) < 1)
5904 : 0 : goto invalid_range;
5905 : 4 : get_uleb128 (op1, readp, nexthdr);
5906 [ - + ]: 4 : if ((uint64_t) (nexthdr - readp) < 1)
5907 : 0 : goto invalid_range;
5908 : 4 : get_uleb128 (op2, readp, nexthdr);
5909 : 4 : printf (" %" PRIx64 ", %" PRIx64 "\n", op1, op2);
5910 [ + - ]: 4 : if (! print_unresolved_addresses)
5911 : : {
5912 : 4 : op1 += base;
5913 : 4 : op2 += base;
5914 : 4 : printf (" ");
5915 : 4 : print_dwarf_addr (dwflmod, address_size, op1, op1);
5916 : 4 : printf ("..\n ");
5917 : 4 : print_dwarf_addr (dwflmod, address_size, op2 - 1, op2);
5918 : 4 : printf ("\n");
5919 : : }
5920 : : break;
5921 : :
5922 : 2 : case DW_RLE_base_address:
5923 [ - + ]: 2 : if (address_size == 4)
5924 : : {
5925 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 4)
5926 : 0 : goto invalid_range;
5927 [ # # ]: 0 : op1 = read_4ubyte_unaligned_inc (dbg, readp);
5928 : : }
5929 : : else
5930 : : {
5931 [ - + ]: 2 : if ((uint64_t) (nexthdr - readp) < 8)
5932 : 0 : goto invalid_range;
5933 [ - + ]: 4 : op1 = read_8ubyte_unaligned_inc (dbg, readp);
5934 : : }
5935 : 2 : base = op1;
5936 : 2 : printf (" 0x%" PRIx64 "\n", base);
5937 [ + - ]: 2 : if (! print_unresolved_addresses)
5938 : : {
5939 : 2 : printf (" ");
5940 : 2 : print_dwarf_addr (dwflmod, address_size, base, base);
5941 : 2 : printf ("\n");
5942 : : }
5943 : : break;
5944 : :
5945 : 0 : case DW_RLE_start_end:
5946 [ # # ]: 0 : if (address_size == 4)
5947 : : {
5948 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 8)
5949 : 0 : goto invalid_range;
5950 [ # # ]: 0 : op1 = read_4ubyte_unaligned_inc (dbg, readp);
5951 [ # # ]: 0 : op2 = read_4ubyte_unaligned_inc (dbg, readp);
5952 : : }
5953 : : else
5954 : : {
5955 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 16)
5956 : 0 : goto invalid_range;
5957 [ # # ]: 0 : op1 = read_8ubyte_unaligned_inc (dbg, readp);
5958 [ # # ]: 0 : op2 = read_8ubyte_unaligned_inc (dbg, readp);
5959 : : }
5960 : 0 : printf (" 0x%" PRIx64 "..0x%" PRIx64 "\n", op1, op2);
5961 [ # # ]: 0 : if (! print_unresolved_addresses)
5962 : : {
5963 : 0 : printf (" ");
5964 : 0 : print_dwarf_addr (dwflmod, address_size, op1, op1);
5965 : 0 : printf ("..\n ");
5966 : 0 : print_dwarf_addr (dwflmod, address_size, op2 - 1, op2);
5967 : 0 : printf ("\n");
5968 : : }
5969 : : break;
5970 : :
5971 : 4 : case DW_RLE_start_length:
5972 [ - + ]: 4 : if (address_size == 4)
5973 : : {
5974 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 4)
5975 : 0 : goto invalid_range;
5976 [ # # ]: 0 : op1 = read_4ubyte_unaligned_inc (dbg, readp);
5977 : : }
5978 : : else
5979 : : {
5980 [ - + ]: 4 : if ((uint64_t) (nexthdr - readp) < 8)
5981 : 0 : goto invalid_range;
5982 [ - + ]: 8 : op1 = read_8ubyte_unaligned_inc (dbg, readp);
5983 : : }
5984 [ - + ]: 4 : if ((uint64_t) (nexthdr - readp) < 1)
5985 : 0 : goto invalid_range;
5986 : 4 : get_uleb128 (op2, readp, nexthdr);
5987 : 4 : printf (" 0x%" PRIx64 ", %" PRIx64 "\n", op1, op2);
5988 [ + - ]: 4 : if (! print_unresolved_addresses)
5989 : : {
5990 : 4 : op2 = op1 + op2;
5991 : 4 : printf (" ");
5992 : 4 : print_dwarf_addr (dwflmod, address_size, op1, op1);
5993 : 4 : printf ("..\n ");
5994 : 4 : print_dwarf_addr (dwflmod, address_size, op2 - 1, op2);
5995 [ + + ]: 20 : printf ("\n");
5996 : : }
5997 : : break;
5998 : :
5999 : 0 : default:
6000 : 0 : goto invalid_range;
6001 : : }
6002 : : }
6003 : :
6004 : 2 : next_table:
6005 [ - + ]: 2 : if (readp != nexthdr)
6006 : : {
6007 : 0 : size_t padding = nexthdr - readp;
6008 : 0 : printf (_(" %zu padding bytes\n\n"), padding);
6009 : 0 : readp = nexthdr;
6010 : : }
6011 : : }
6012 : : }
6013 : :
6014 : : /* Print content of DWARF .debug_ranges section. */
6015 : : static void
6016 : 30 : print_debug_ranges_section (Dwfl_Module *dwflmod,
6017 : : Ebl *ebl, GElf_Ehdr *ehdr,
6018 : : Elf_Scn *scn, GElf_Shdr *shdr,
6019 : : Dwarf *dbg)
6020 : : {
6021 : 60 : Elf_Data *data = (dbg->sectiondata[IDX_debug_ranges]
6022 [ - + ]: 30 : ?: elf_rawdata (scn, NULL));
6023 [ - + ]: 30 : if (unlikely (data == NULL))
6024 : : {
6025 : 0 : error (0, 0, _("cannot get .debug_ranges content: %s"),
6026 : : elf_errmsg (-1));
6027 : 0 : return;
6028 : : }
6029 : :
6030 : 30 : printf (_("\
6031 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
6032 : : elf_ndxscn (scn), section_name (ebl, shdr),
6033 : 30 : (uint64_t) shdr->sh_offset);
6034 : :
6035 : 30 : sort_listptr (&known_rangelistptr, "rangelistptr");
6036 : 30 : size_t listptr_idx = 0;
6037 : :
6038 [ + + ]: 30 : uint_fast8_t address_size = ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 4 : 8;
6039 : :
6040 : 30 : bool first = true;
6041 : 30 : Dwarf_Addr base = 0;
6042 : 30 : unsigned char *const endp = (unsigned char *) data->d_buf + data->d_size;
6043 : 30 : unsigned char *readp = data->d_buf;
6044 : 30 : Dwarf_CU *last_cu = NULL;
6045 [ + + ]: 62943 : while (readp < endp)
6046 : : {
6047 : 62913 : ptrdiff_t offset = readp - (unsigned char *) data->d_buf;
6048 : 62913 : Dwarf_CU *cu = last_cu;
6049 : :
6050 [ + + + + ]: 62913 : if (first && skip_listptr_hole (&known_rangelistptr, &listptr_idx,
6051 : : &address_size, NULL, &base, &cu,
6052 : : offset, &readp, endp, NULL))
6053 : 5 : continue;
6054 : :
6055 [ + + ]: 62908 : if (last_cu != cu)
6056 : : {
6057 : 944 : Dwarf_Die cudie;
6058 [ - + ]: 944 : if (dwarf_cu_die (cu, &cudie,
6059 : : NULL, NULL, NULL, NULL,
6060 : : NULL, NULL) == NULL)
6061 : 0 : printf (_("\n Unknown CU base: "));
6062 : : else
6063 : 944 : printf (_("\n CU [%6" PRIx64 "] base: "),
6064 : : dwarf_dieoffset (&cudie));
6065 : 944 : print_dwarf_addr (dwflmod, address_size, base, base);
6066 : 944 : printf ("\n");
6067 : : }
6068 : 62908 : last_cu = cu;
6069 : :
6070 [ - + ]: 62908 : if (unlikely (data->d_size - offset < (size_t) address_size * 2))
6071 : : {
6072 : 0 : printf (_(" [%6tx] <INVALID DATA>\n"), offset);
6073 : 0 : break;
6074 : : }
6075 : :
6076 : 62908 : Dwarf_Addr begin;
6077 : 62908 : Dwarf_Addr end;
6078 [ + + ]: 62908 : if (address_size == 8)
6079 : : {
6080 [ - + ]: 62900 : begin = read_8ubyte_unaligned_inc (dbg, readp);
6081 [ - + ]: 125800 : end = read_8ubyte_unaligned_inc (dbg, readp);
6082 : : }
6083 : : else
6084 : : {
6085 [ - + ]: 8 : begin = read_4ubyte_unaligned_inc (dbg, readp);
6086 [ - + ]: 8 : end = read_4ubyte_unaligned_inc (dbg, readp);
6087 [ + - ]: 8 : if (begin == (Dwarf_Addr) (uint32_t) -1)
6088 : : begin = (Dwarf_Addr) -1l;
6089 : : }
6090 : :
6091 [ - + ]: 62908 : if (begin == (Dwarf_Addr) -1l) /* Base address entry. */
6092 : : {
6093 [ # # ]: 0 : if (first)
6094 : 0 : printf (" [%6tx] ", offset);
6095 : : else
6096 : 0 : printf (" ");
6097 : 0 : puts (_("base address"));
6098 : 0 : printf (" ");
6099 : 0 : print_dwarf_addr (dwflmod, address_size, end, end);
6100 : 0 : printf ("\n");
6101 : 0 : base = end;
6102 : 0 : first = false;
6103 : : }
6104 [ + + ]: 62908 : else if (begin == 0 && end == 0) /* End of list entry. */
6105 : : {
6106 [ + + ]: 13564 : if (first)
6107 : 5 : printf (_(" [%6tx] empty list\n"), offset);
6108 : : first = true;
6109 : : }
6110 : : else
6111 : : {
6112 : : /* We have an address range entry. */
6113 [ + + ]: 49344 : if (first) /* First address range entry in a list. */
6114 : 13559 : printf (" [%6tx] ", offset);
6115 : : else
6116 : 35785 : printf (" ");
6117 : :
6118 : 49344 : printf ("range %" PRIx64 ", %" PRIx64 "\n", begin, end);
6119 [ + + ]: 49344 : if (! print_unresolved_addresses)
6120 : : {
6121 : 49334 : printf (" ");
6122 : 49334 : print_dwarf_addr (dwflmod, address_size, base + begin,
6123 : : base + begin);
6124 : 49334 : printf ("..\n ");
6125 : 49334 : print_dwarf_addr (dwflmod, address_size,
6126 : 49334 : base + end - 1, base + end);
6127 : 62908 : printf ("\n");
6128 : : }
6129 : :
6130 : : first = false;
6131 : : }
6132 : : }
6133 : : }
6134 : :
6135 : : #define REGNAMESZ 16
6136 : : static const char *
6137 : 126434 : register_info (Ebl *ebl, unsigned int regno, const Ebl_Register_Location *loc,
6138 : : char name[REGNAMESZ], int *bits, int *type)
6139 : : {
6140 : 126434 : const char *set;
6141 : 126434 : const char *pfx;
6142 : 126434 : int ignore;
6143 [ + + + + ]: 377606 : ssize_t n = ebl_register_info (ebl, regno, name, REGNAMESZ, &pfx, &set,
6144 : : bits ?: &ignore, type ?: &ignore);
6145 [ - + ]: 126434 : if (n <= 0)
6146 : : {
6147 [ # # ]: 0 : if (loc != NULL)
6148 : 0 : snprintf (name, REGNAMESZ, "reg%u", loc->regno);
6149 : : else
6150 : 0 : snprintf (name, REGNAMESZ, "??? 0x%x", regno);
6151 [ # # ]: 0 : if (bits != NULL)
6152 [ # # ]: 0 : *bits = loc != NULL ? loc->bits : 0;
6153 [ # # ]: 0 : if (type != NULL)
6154 : 0 : *type = DW_ATE_unsigned;
6155 : 0 : set = "??? unrecognized";
6156 : : }
6157 : : else
6158 : : {
6159 [ + + - + ]: 126434 : if (bits != NULL && *bits <= 0)
6160 [ # # ]: 0 : *bits = loc != NULL ? loc->bits : 0;
6161 [ + + - + ]: 126434 : if (type != NULL && *type == DW_ATE_void)
6162 : 0 : *type = DW_ATE_unsigned;
6163 : :
6164 : : }
6165 : 126434 : return set;
6166 : : }
6167 : :
6168 : : static const unsigned char *
6169 : 80 : read_encoded (unsigned int encoding, const unsigned char *readp,
6170 : : const unsigned char *const endp, uint64_t *res, Dwarf *dbg)
6171 : : {
6172 [ - + ]: 80 : if ((encoding & 0xf) == DW_EH_PE_absptr)
6173 : 0 : encoding = gelf_getclass (dbg->elf) == ELFCLASS32
6174 [ # # ]: 0 : ? DW_EH_PE_udata4 : DW_EH_PE_udata8;
6175 : :
6176 [ - - - + : 80 : switch (encoding & 0xf)
- - + -
- ]
6177 : : {
6178 : 0 : case DW_EH_PE_uleb128:
6179 : 0 : get_uleb128 (*res, readp, endp);
6180 : 0 : break;
6181 : 0 : case DW_EH_PE_sleb128:
6182 : 0 : get_sleb128 (*res, readp, endp);
6183 : 0 : break;
6184 : 0 : case DW_EH_PE_udata2:
6185 [ # # ]: 0 : if (readp + 2 > endp)
6186 : 0 : goto invalid;
6187 [ # # ]: 0 : *res = read_2ubyte_unaligned_inc (dbg, readp);
6188 : 0 : break;
6189 : 40 : case DW_EH_PE_udata4:
6190 [ - + ]: 40 : if (readp + 4 > endp)
6191 : 0 : goto invalid;
6192 [ - + ]: 40 : *res = read_4ubyte_unaligned_inc (dbg, readp);
6193 : 40 : break;
6194 : 0 : case DW_EH_PE_udata8:
6195 [ # # ]: 0 : if (readp + 8 > endp)
6196 : 0 : goto invalid;
6197 [ # # ]: 0 : *res = read_8ubyte_unaligned_inc (dbg, readp);
6198 : 0 : break;
6199 : 0 : case DW_EH_PE_sdata2:
6200 [ # # ]: 0 : if (readp + 2 > endp)
6201 : 0 : goto invalid;
6202 [ # # ]: 0 : *res = read_2sbyte_unaligned_inc (dbg, readp);
6203 : 0 : break;
6204 : 40 : case DW_EH_PE_sdata4:
6205 [ - + ]: 40 : if (readp + 4 > endp)
6206 : 0 : goto invalid;
6207 [ - + ]: 40 : *res = read_4sbyte_unaligned_inc (dbg, readp);
6208 : 40 : break;
6209 : 0 : case DW_EH_PE_sdata8:
6210 [ # # ]: 0 : if (readp + 8 > endp)
6211 : 0 : goto invalid;
6212 [ # # ]: 0 : *res = read_8sbyte_unaligned_inc (dbg, readp);
6213 : 0 : break;
6214 : : default:
6215 : 0 : invalid:
6216 : 0 : error (1, 0,
6217 : 0 : _("invalid encoding"));
6218 : : }
6219 : :
6220 : 80 : return readp;
6221 : : }
6222 : :
6223 : : static const char *
6224 : 125586 : regname (Ebl *ebl, unsigned int regno, char *regnamebuf)
6225 : : {
6226 : 125586 : register_info (ebl, regno, NULL, regnamebuf, NULL, NULL);
6227 : :
6228 : 125586 : return regnamebuf;
6229 : : }
6230 : :
6231 : : static void
6232 : 9423 : print_cfa_program (const unsigned char *readp, const unsigned char *const endp,
6233 : : Dwarf_Word vma_base, unsigned int code_align,
6234 : : int data_align,
6235 : : unsigned int version, unsigned int ptr_size,
6236 : : unsigned int encoding,
6237 : : Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr, Dwarf *dbg)
6238 : : {
6239 : 9423 : char regnamebuf[REGNAMESZ];
6240 : :
6241 : 9423 : puts ("\n Program:");
6242 : 9423 : Dwarf_Word pc = vma_base;
6243 : 9423 : while (readp < endp)
6244 : : {
6245 : 232519 : unsigned int opcode = *readp++;
6246 : :
6247 [ + + ]: 232519 : if (opcode < DW_CFA_advance_loc)
6248 : : /* Extended opcode. */
6249 [ + - + + : 59541 : switch (opcode)
+ + - + -
- + + + +
+ + - + -
- - - - -
+ - - ]
6250 : : {
6251 : 27508 : uint64_t op1;
6252 : 27508 : int64_t sop1;
6253 : 27508 : uint64_t op2;
6254 : 27508 : int64_t sop2;
6255 : :
6256 : 27508 : case DW_CFA_nop:
6257 : 27508 : puts (" nop");
6258 : 27508 : break;
6259 : 0 : case DW_CFA_set_loc:
6260 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
6261 : 0 : goto invalid;
6262 : 0 : readp = read_encoded (encoding, readp, endp, &op1, dbg);
6263 : 0 : printf (" set_loc %#" PRIx64 " to %#" PRIx64 "\n",
6264 : : op1, pc = vma_base + op1);
6265 : : break;
6266 : 1178 : case DW_CFA_advance_loc1:
6267 [ - + ]: 1178 : if ((uint64_t) (endp - readp) < 1)
6268 : 0 : goto invalid;
6269 : 2356 : printf (" advance_loc1 %u to %#" PRIx64 "\n",
6270 : 1178 : *readp, pc += *readp * code_align);
6271 : 1178 : ++readp;
6272 : 1178 : break;
6273 : 137 : case DW_CFA_advance_loc2:
6274 [ - + ]: 137 : if ((uint64_t) (endp - readp) < 2)
6275 : 0 : goto invalid;
6276 [ - + ]: 137 : op1 = read_2ubyte_unaligned_inc (dbg, readp);
6277 : 59678 : printf (" advance_loc2 %" PRIu64 " to %#" PRIx64 "\n",
6278 : 137 : op1, pc += op1 * code_align);
6279 : : break;
6280 : 24 : case DW_CFA_advance_loc4:
6281 [ - + ]: 24 : if ((uint64_t) (endp - readp) < 4)
6282 : 0 : goto invalid;
6283 [ + - ]: 24 : op1 = read_4ubyte_unaligned_inc (dbg, readp);
6284 : 59565 : printf (" advance_loc4 %" PRIu64 " to %#" PRIx64 "\n",
6285 : 24 : op1, pc += op1 * code_align);
6286 : : break;
6287 : 4 : case DW_CFA_offset_extended:
6288 [ - + ]: 4 : if ((uint64_t) (endp - readp) < 1)
6289 : 0 : goto invalid;
6290 : 4 : get_uleb128 (op1, readp, endp);
6291 [ - + ]: 4 : if ((uint64_t) (endp - readp) < 1)
6292 : 0 : goto invalid;
6293 : 4 : get_uleb128 (op2, readp, endp);
6294 : 4 : printf (" offset_extended r%" PRIu64 " (%s) at cfa%+" PRId64
6295 : : "\n",
6296 : : op1, regname (ebl, op1, regnamebuf), op2 * data_align);
6297 : : break;
6298 : 0 : case DW_CFA_restore_extended:
6299 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
6300 : 0 : goto invalid;
6301 : 0 : get_uleb128 (op1, readp, endp);
6302 : 0 : printf (" restore_extended r%" PRIu64 " (%s)\n",
6303 : : op1, regname (ebl, op1, regnamebuf));
6304 : : break;
6305 : 20 : case DW_CFA_undefined:
6306 [ - + ]: 20 : if ((uint64_t) (endp - readp) < 1)
6307 : 0 : goto invalid;
6308 : 20 : get_uleb128 (op1, readp, endp);
6309 : 20 : printf (" undefined r%" PRIu64 " (%s)\n", op1,
6310 : : regname (ebl, op1, regnamebuf));
6311 : : break;
6312 : 0 : case DW_CFA_same_value:
6313 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
6314 : 0 : goto invalid;
6315 : 0 : get_uleb128 (op1, readp, endp);
6316 : 0 : printf (" same_value r%" PRIu64 " (%s)\n", op1,
6317 : : regname (ebl, op1, regnamebuf));
6318 : : break;
6319 : 0 : case DW_CFA_register:
6320 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
6321 : 0 : goto invalid;
6322 : 0 : get_uleb128 (op1, readp, endp);
6323 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
6324 : 0 : goto invalid;
6325 : 0 : get_uleb128 (op2, readp, endp);
6326 : 0 : printf (" register r%" PRIu64 " (%s) in r%" PRIu64 " (%s)\n",
6327 : : op1, regname (ebl, op1, regnamebuf), op2,
6328 : : regname (ebl, op2, regnamebuf));
6329 : : break;
6330 : 7932 : case DW_CFA_remember_state:
6331 : 7932 : puts (" remember_state");
6332 : 7932 : break;
6333 : 7932 : case DW_CFA_restore_state:
6334 : 7932 : puts (" restore_state");
6335 : 7932 : break;
6336 : 133 : case DW_CFA_def_cfa:
6337 [ - + ]: 133 : if ((uint64_t) (endp - readp) < 1)
6338 : 0 : goto invalid;
6339 : 133 : get_uleb128 (op1, readp, endp);
6340 [ - + ]: 133 : if ((uint64_t) (endp - readp) < 1)
6341 : 0 : goto invalid;
6342 : 133 : get_uleb128 (op2, readp, endp);
6343 : 133 : printf (" def_cfa r%" PRIu64 " (%s) at offset %" PRIu64 "\n",
6344 : : op1, regname (ebl, op1, regnamebuf), op2);
6345 : : break;
6346 : 57 : case DW_CFA_def_cfa_register:
6347 [ - + ]: 57 : if ((uint64_t) (endp - readp) < 1)
6348 : 0 : goto invalid;
6349 : 57 : get_uleb128 (op1, readp, endp);
6350 : 57 : printf (" def_cfa_register r%" PRIu64 " (%s)\n",
6351 : : op1, regname (ebl, op1, regnamebuf));
6352 : : break;
6353 : 14576 : case DW_CFA_def_cfa_offset:
6354 [ - + ]: 14576 : if ((uint64_t) (endp - readp) < 1)
6355 : 0 : goto invalid;
6356 : 14576 : get_uleb128 (op1, readp, endp);
6357 : 14576 : printf (" def_cfa_offset %" PRIu64 "\n", op1);
6358 : : break;
6359 : 20 : case DW_CFA_def_cfa_expression:
6360 [ - + ]: 20 : if ((uint64_t) (endp - readp) < 1)
6361 : 0 : goto invalid;
6362 : 20 : get_uleb128 (op1, readp, endp); /* Length of DW_FORM_block. */
6363 : 20 : printf (" def_cfa_expression %" PRIu64 "\n", op1);
6364 [ - + ]: 20 : if ((uint64_t) (endp - readp) < op1)
6365 : : {
6366 : 0 : invalid:
6367 : 0 : fputs (_(" <INVALID DATA>\n"), stdout);
6368 : 0 : return;
6369 : : }
6370 : 20 : print_ops (dwflmod, dbg, 10, 10, version, ptr_size, 0, NULL,
6371 : : op1, readp);
6372 : 20 : readp += op1;
6373 : 20 : break;
6374 : 0 : case DW_CFA_expression:
6375 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
6376 : 0 : goto invalid;
6377 : 0 : get_uleb128 (op1, readp, endp);
6378 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
6379 : 0 : goto invalid;
6380 : 0 : get_uleb128 (op2, readp, endp); /* Length of DW_FORM_block. */
6381 : 0 : printf (" expression r%" PRIu64 " (%s) \n",
6382 : : op1, regname (ebl, op1, regnamebuf));
6383 [ # # ]: 0 : if ((uint64_t) (endp - readp) < op2)
6384 : 0 : goto invalid;
6385 : 0 : print_ops (dwflmod, dbg, 10, 10, version, ptr_size, 0, NULL,
6386 : : op2, readp);
6387 : 0 : readp += op2;
6388 : 0 : break;
6389 : 16 : case DW_CFA_offset_extended_sf:
6390 [ - + ]: 16 : if ((uint64_t) (endp - readp) < 1)
6391 : 0 : goto invalid;
6392 : 16 : get_uleb128 (op1, readp, endp);
6393 [ - + ]: 16 : if ((uint64_t) (endp - readp) < 1)
6394 : 0 : goto invalid;
6395 : 16 : get_sleb128 (sop2, readp, endp);
6396 : 16 : printf (" offset_extended_sf r%" PRIu64 " (%s) at cfa%+"
6397 : : PRId64 "\n",
6398 : : op1, regname (ebl, op1, regnamebuf), sop2 * data_align);
6399 : : break;
6400 : 0 : case DW_CFA_def_cfa_sf:
6401 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
6402 : 0 : goto invalid;
6403 : 0 : get_uleb128 (op1, readp, endp);
6404 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
6405 : 0 : goto invalid;
6406 : 0 : get_sleb128 (sop2, readp, endp);
6407 : 0 : printf (" def_cfa_sf r%" PRIu64 " (%s) at offset %" PRId64 "\n",
6408 : : op1, regname (ebl, op1, regnamebuf), sop2 * data_align);
6409 : : break;
6410 : 0 : case DW_CFA_def_cfa_offset_sf:
6411 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
6412 : 0 : goto invalid;
6413 : 0 : get_sleb128 (sop1, readp, endp);
6414 : 0 : printf (" def_cfa_offset_sf %" PRId64 "\n", sop1 * data_align);
6415 : : break;
6416 : 0 : case DW_CFA_val_offset:
6417 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
6418 : 0 : goto invalid;
6419 : 0 : get_uleb128 (op1, readp, endp);
6420 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
6421 : 0 : goto invalid;
6422 : 0 : get_uleb128 (op2, readp, endp);
6423 : 0 : printf (" val_offset %" PRIu64 " at offset %" PRIu64 "\n",
6424 : : op1, op2 * data_align);
6425 : : break;
6426 : 0 : case DW_CFA_val_offset_sf:
6427 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
6428 : 0 : goto invalid;
6429 : 0 : get_uleb128 (op1, readp, endp);
6430 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
6431 : 0 : goto invalid;
6432 : 0 : get_sleb128 (sop2, readp, endp);
6433 : 0 : printf (" val_offset_sf %" PRIu64 " at offset %" PRId64 "\n",
6434 : : op1, sop2 * data_align);
6435 : : break;
6436 : 0 : case DW_CFA_val_expression:
6437 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
6438 : 0 : goto invalid;
6439 : 0 : get_uleb128 (op1, readp, endp);
6440 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
6441 : 0 : goto invalid;
6442 : 0 : get_uleb128 (op2, readp, endp); /* Length of DW_FORM_block. */
6443 : 0 : printf (" val_expression r%" PRIu64 " (%s)\n",
6444 : : op1, regname (ebl, op1, regnamebuf));
6445 [ # # ]: 0 : if ((uint64_t) (endp - readp) < op2)
6446 : 0 : goto invalid;
6447 : 0 : print_ops (dwflmod, dbg, 10, 10, version, ptr_size, 0,
6448 : : NULL, op2, readp);
6449 : 0 : readp += op2;
6450 : 0 : break;
6451 : 0 : case DW_CFA_MIPS_advance_loc8:
6452 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 8)
6453 : 0 : goto invalid;
6454 [ # # ]: 0 : op1 = read_8ubyte_unaligned_inc (dbg, readp);
6455 : 59541 : printf (" MIPS_advance_loc8 %" PRIu64 " to %#" PRIx64 "\n",
6456 : 0 : op1, pc += op1 * code_align);
6457 : : break;
6458 : 4 : case DW_CFA_GNU_window_save: /* DW_CFA_AARCH64_negate_ra_state */
6459 [ + - ]: 4 : if (ehdr->e_machine == EM_AARCH64)
6460 : 4 : puts (" AARCH64_negate_ra_state");
6461 : : else
6462 : 0 : puts (" GNU_window_save");
6463 : : break;
6464 : 0 : case DW_CFA_GNU_args_size:
6465 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
6466 : 0 : goto invalid;
6467 : 0 : get_uleb128 (op1, readp, endp);
6468 : 0 : printf (" args_size %" PRIu64 "\n", op1);
6469 : : break;
6470 : : default:
6471 : 59541 : printf (" ??? (%u)\n", opcode);
6472 : : break;
6473 : : }
6474 [ + + ]: 172978 : else if (opcode < DW_CFA_offset)
6475 : 289564 : printf (" advance_loc %u to %#" PRIx64 "\n",
6476 : 47622 : opcode & 0x3f, pc += (opcode & 0x3f) * code_align);
6477 [ + + ]: 125356 : else if (opcode < DW_CFA_restore)
6478 : : {
6479 : 49598 : uint64_t offset;
6480 [ - + ]: 49598 : if ((uint64_t) (endp - readp) < 1)
6481 : 0 : goto invalid;
6482 : 49598 : get_uleb128 (offset, readp, endp);
6483 : 49598 : printf (" offset r%u (%s) at cfa%+" PRId64 "\n",
6484 : : opcode & 0x3f, regname (ebl, opcode & 0x3f, regnamebuf),
6485 : : offset * data_align);
6486 : : }
6487 : : else
6488 [ + + ]: 317700 : printf (" restore r%u (%s)\n",
6489 : : opcode & 0x3f, regname (ebl, opcode & 0x3f, regnamebuf));
6490 : : }
6491 : : }
6492 : :
6493 : :
6494 : : static unsigned int
6495 : 9317 : encoded_ptr_size (int encoding, unsigned int ptr_size)
6496 : : {
6497 [ - + - + ]: 9317 : switch (encoding & 7)
6498 : : {
6499 : : case DW_EH_PE_udata4:
6500 : : return 4;
6501 : 0 : case DW_EH_PE_udata8:
6502 : 0 : return 8;
6503 : 48 : case 0:
6504 : 48 : return ptr_size;
6505 : : }
6506 : :
6507 : 0 : fprintf (stderr, "Unsupported pointer encoding: %#x, "
6508 : : "assuming pointer size of %d.\n", encoding, ptr_size);
6509 : 0 : return ptr_size;
6510 : : }
6511 : :
6512 : :
6513 : : static unsigned int
6514 : 186 : print_encoding (unsigned int val)
6515 : : {
6516 [ - - - + : 186 : switch (val & 0xf)
- - - + -
- ]
6517 : : {
6518 : 0 : case DW_EH_PE_absptr:
6519 : 0 : fputs ("absptr", stdout);
6520 : 0 : break;
6521 : 0 : case DW_EH_PE_uleb128:
6522 : 0 : fputs ("uleb128", stdout);
6523 : 0 : break;
6524 : 0 : case DW_EH_PE_udata2:
6525 : 0 : fputs ("udata2", stdout);
6526 : 0 : break;
6527 : 40 : case DW_EH_PE_udata4:
6528 : 40 : fputs ("udata4", stdout);
6529 : 40 : break;
6530 : 0 : case DW_EH_PE_udata8:
6531 : 0 : fputs ("udata8", stdout);
6532 : 0 : break;
6533 : 0 : case DW_EH_PE_sleb128:
6534 : 0 : fputs ("sleb128", stdout);
6535 : 0 : break;
6536 : 0 : case DW_EH_PE_sdata2:
6537 : 0 : fputs ("sdata2", stdout);
6538 : 0 : break;
6539 : 146 : case DW_EH_PE_sdata4:
6540 : 146 : fputs ("sdata4", stdout);
6541 : 146 : break;
6542 : 0 : case DW_EH_PE_sdata8:
6543 : 0 : fputs ("sdata8", stdout);
6544 : 0 : break;
6545 : : default:
6546 : : /* We did not use any of the bits after all. */
6547 : : return val;
6548 : : }
6549 : :
6550 : 186 : return val & ~0xf;
6551 : : }
6552 : :
6553 : :
6554 : : static unsigned int
6555 : 146 : print_relinfo (unsigned int val)
6556 : : {
6557 [ + - + - : 146 : switch (val & 0x70)
- - ]
6558 : : {
6559 : 106 : case DW_EH_PE_pcrel:
6560 : 106 : fputs ("pcrel", stdout);
6561 : 106 : break;
6562 : 0 : case DW_EH_PE_textrel:
6563 : 0 : fputs ("textrel", stdout);
6564 : 0 : break;
6565 : 40 : case DW_EH_PE_datarel:
6566 : 40 : fputs ("datarel", stdout);
6567 : 40 : break;
6568 : 0 : case DW_EH_PE_funcrel:
6569 : 0 : fputs ("funcrel", stdout);
6570 : 0 : break;
6571 : 0 : case DW_EH_PE_aligned:
6572 : 0 : fputs ("aligned", stdout);
6573 : 0 : break;
6574 : : default:
6575 : : return val;
6576 : : }
6577 : :
6578 : 146 : return val & ~0x70;
6579 : : }
6580 : :
6581 : :
6582 : : static void
6583 : 186 : print_encoding_base (const char *pfx, unsigned int fde_encoding)
6584 : : {
6585 : 186 : printf ("(%s", pfx);
6586 : :
6587 [ - + ]: 186 : if (fde_encoding == DW_EH_PE_omit)
6588 : 0 : puts ("omit)");
6589 : : else
6590 : : {
6591 : 186 : unsigned int w = fde_encoding;
6592 : :
6593 : 186 : w = print_encoding (w);
6594 : :
6595 [ + + ]: 186 : if (w & 0x70)
6596 : : {
6597 [ + - ]: 146 : if (w != fde_encoding)
6598 [ - + ]: 146 : fputc_unlocked (' ', stdout);
6599 : :
6600 : 146 : w = print_relinfo (w);
6601 : : }
6602 : :
6603 [ - + ]: 186 : if (w != 0)
6604 [ # # ]: 0 : printf ("%s%x", w != fde_encoding ? " " : "", w);
6605 : :
6606 : 186 : puts (")");
6607 : : }
6608 : 186 : }
6609 : :
6610 : :
6611 : : static void
6612 : 76 : print_debug_frame_section (Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr,
6613 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
6614 : : {
6615 : 76 : size_t shstrndx;
6616 : : /* We know this call will succeed since it did in the caller. */
6617 : 76 : (void) elf_getshdrstrndx (ebl->elf, &shstrndx);
6618 : 76 : const char *scnname = elf_strptr (ebl->elf, shstrndx, shdr->sh_name);
6619 : :
6620 : : /* Needed if we find PC-relative addresses. */
6621 : 76 : GElf_Addr bias;
6622 [ - + ]: 76 : if (dwfl_module_getelf (dwflmod, &bias) == NULL)
6623 : : {
6624 : 0 : error (0, 0, _("cannot get ELF: %s"), dwfl_errmsg (-1));
6625 : 0 : return;
6626 : : }
6627 : :
6628 : 76 : bool is_eh_frame = strcmp (scnname, ".eh_frame") == 0;
6629 : 152 : Elf_Data *data = (is_eh_frame
6630 : 52 : ? elf_rawdata (scn, NULL)
6631 [ + + ]: 76 : : (dbg->sectiondata[IDX_debug_frame]
6632 [ - + ]: 24 : ?: elf_rawdata (scn, NULL)));
6633 : :
6634 [ - + ]: 76 : if (unlikely (data == NULL))
6635 : : {
6636 : 0 : error (0, 0, _("cannot get %s content: %s"),
6637 : : scnname, elf_errmsg (-1));
6638 : 0 : return;
6639 : : }
6640 : :
6641 [ + + ]: 76 : if (is_eh_frame)
6642 : 52 : printf (_("\
6643 : : \nCall frame information section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
6644 : 52 : elf_ndxscn (scn), scnname, (uint64_t) shdr->sh_offset);
6645 : : else
6646 : 24 : printf (_("\
6647 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
6648 : 24 : elf_ndxscn (scn), scnname, (uint64_t) shdr->sh_offset);
6649 : :
6650 : 76 : struct cieinfo
6651 : : {
6652 : : ptrdiff_t cie_offset;
6653 : : const char *augmentation;
6654 : : unsigned int code_alignment_factor;
6655 : : unsigned int data_alignment_factor;
6656 : : uint8_t address_size;
6657 : : uint8_t fde_encoding;
6658 : : uint8_t lsda_encoding;
6659 : : struct cieinfo *next;
6660 : 76 : } *cies = NULL;
6661 : :
6662 : 76 : const unsigned char *readp = data->d_buf;
6663 : 76 : const unsigned char *const dataend = ((unsigned char *) data->d_buf
6664 : 76 : + data->d_size);
6665 [ + + ]: 9539 : while (readp < dataend)
6666 : : {
6667 [ - + ]: 9463 : if (unlikely (readp + 4 > dataend))
6668 : : {
6669 : 0 : invalid_data:
6670 : 0 : error (0, 0, _("invalid data in section [%zu] '%s'"),
6671 : : elf_ndxscn (scn), scnname);
6672 : 0 : return;
6673 : : }
6674 : :
6675 : : /* At the beginning there must be a CIE. There can be multiple,
6676 : : hence we test tis in a loop. */
6677 : 9463 : ptrdiff_t offset = readp - (unsigned char *) data->d_buf;
6678 : :
6679 [ + + ]: 9463 : Dwarf_Word unit_length = read_4ubyte_unaligned_inc (dbg, readp);
6680 : 9463 : unsigned int length = 4;
6681 [ - + ]: 9463 : if (unlikely (unit_length == 0xffffffff))
6682 : : {
6683 [ # # ]: 0 : if (unlikely (readp + 8 > dataend))
6684 : 0 : goto invalid_data;
6685 : :
6686 [ # # ]: 0 : unit_length = read_8ubyte_unaligned_inc (dbg, readp);
6687 : 0 : length = 8;
6688 : : }
6689 : :
6690 [ + + ]: 9463 : if (unlikely (unit_length == 0))
6691 : : {
6692 : 40 : printf (_("\n [%6tx] Zero terminator\n"), offset);
6693 : 40 : continue;
6694 : : }
6695 : :
6696 : 9423 : Dwarf_Word maxsize = dataend - readp;
6697 [ - + ]: 9423 : if (unlikely (unit_length > maxsize))
6698 : 0 : goto invalid_data;
6699 : :
6700 [ + + ]: 9423 : unsigned int ptr_size = ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 4 : 8;
6701 : :
6702 : 9423 : ptrdiff_t start = readp - (unsigned char *) data->d_buf;
6703 : 9423 : const unsigned char *const cieend = readp + unit_length;
6704 [ - + ]: 9423 : if (unlikely (cieend > dataend))
6705 : 0 : goto invalid_data;
6706 : :
6707 : 9423 : Dwarf_Off cie_id;
6708 [ + - ]: 9423 : if (length == 4)
6709 : : {
6710 [ - + ]: 9423 : if (unlikely (cieend - readp < 4))
6711 : 0 : goto invalid_data;
6712 [ + + ]: 9423 : cie_id = read_4ubyte_unaligned_inc (dbg, readp);
6713 [ + + ]: 9423 : if (!is_eh_frame && cie_id == DW_CIE_ID_32)
6714 : 38 : cie_id = DW_CIE_ID_64;
6715 : : }
6716 : : else
6717 : : {
6718 [ # # ]: 0 : if (unlikely (cieend - readp < 8))
6719 : 0 : goto invalid_data;
6720 [ # # ]: 0 : cie_id = read_8ubyte_unaligned_inc (dbg, readp);
6721 : : }
6722 : :
6723 : 9423 : uint_fast8_t version = 2;
6724 : 9423 : unsigned int code_alignment_factor;
6725 : 9423 : int data_alignment_factor;
6726 : 9423 : unsigned int fde_encoding = 0;
6727 : 9423 : unsigned int lsda_encoding = 0;
6728 : 9423 : Dwarf_Word initial_location = 0;
6729 : 9423 : Dwarf_Word vma_base = 0;
6730 : :
6731 [ + + + + ]: 9503 : if (cie_id == (is_eh_frame ? 0 : DW_CIE_ID_64))
6732 : : {
6733 [ - + ]: 106 : if (unlikely (cieend - readp < 2))
6734 : 0 : goto invalid_data;
6735 : 106 : version = *readp++;
6736 : 106 : const char *const augmentation = (const char *) readp;
6737 : 106 : readp = memchr (readp, '\0', cieend - readp);
6738 [ - + ]: 106 : if (unlikely (readp == NULL))
6739 : 0 : goto invalid_data;
6740 : 106 : ++readp;
6741 : :
6742 : 106 : uint_fast8_t segment_size = 0;
6743 [ - + ]: 106 : if (version >= 4)
6744 : : {
6745 [ # # ]: 0 : if (cieend - readp < 5)
6746 : 0 : goto invalid_data;
6747 : 0 : ptr_size = *readp++;
6748 : 0 : segment_size = *readp++;
6749 : : }
6750 : :
6751 [ - + ]: 106 : if (cieend - readp < 1)
6752 : 0 : goto invalid_data;
6753 : 106 : get_uleb128 (code_alignment_factor, readp, cieend);
6754 [ - + ]: 106 : if (cieend - readp < 1)
6755 : 0 : goto invalid_data;
6756 : 106 : get_sleb128 (data_alignment_factor, readp, cieend);
6757 : :
6758 : : /* In some variant for unwind data there is another field. */
6759 [ - + ]: 106 : if (strcmp (augmentation, "eh") == 0)
6760 [ # # ]: 0 : readp += ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 4 : 8;
6761 : :
6762 : 106 : unsigned int return_address_register;
6763 [ - + ]: 106 : if (cieend - readp < 1)
6764 : 0 : goto invalid_data;
6765 [ + + ]: 106 : if (unlikely (version == 1))
6766 : 94 : return_address_register = *readp++;
6767 : : else
6768 : 12 : get_uleb128 (return_address_register, readp, cieend);
6769 : :
6770 : 106 : printf ("\n [%6tx] CIE length=%" PRIu64 "\n"
6771 : : " CIE_id: %" PRIu64 "\n"
6772 : : " version: %u\n"
6773 : : " augmentation: \"%s\"\n",
6774 : : offset, (uint64_t) unit_length, (uint64_t) cie_id,
6775 : : version, augmentation);
6776 [ - + ]: 106 : if (version >= 4)
6777 : 0 : printf (" address_size: %u\n"
6778 : : " segment_size: %u\n",
6779 : : ptr_size, segment_size);
6780 : 106 : printf (" code_alignment_factor: %u\n"
6781 : : " data_alignment_factor: %d\n"
6782 : : " return_address_register: %u\n",
6783 : : code_alignment_factor,
6784 : : data_alignment_factor, return_address_register);
6785 : :
6786 [ + + ]: 106 : if (augmentation[0] == 'z')
6787 : : {
6788 : 66 : unsigned int augmentationlen;
6789 : 66 : get_uleb128 (augmentationlen, readp, cieend);
6790 : :
6791 [ - + ]: 66 : if (augmentationlen > (size_t) (cieend - readp))
6792 : : {
6793 : 0 : error (0, 0, _("invalid augmentation length"));
6794 : 0 : readp = cieend;
6795 : 0 : continue;
6796 : : }
6797 : :
6798 : 66 : const char *hdr = "Augmentation data:";
6799 : 66 : const char *cp = augmentation + 1;
6800 [ + + + - ]: 132 : while (*cp != '\0' && cp < augmentation + augmentationlen + 1)
6801 : : {
6802 : 66 : printf (" %-26s%#x ", hdr, *readp);
6803 : 66 : hdr = "";
6804 : :
6805 [ + - ]: 66 : if (*cp == 'R')
6806 : : {
6807 : 66 : fde_encoding = *readp++;
6808 : 66 : print_encoding_base (_("FDE address encoding: "),
6809 : : fde_encoding);
6810 : : }
6811 [ # # ]: 0 : else if (*cp == 'L')
6812 : : {
6813 : 0 : lsda_encoding = *readp++;
6814 : 0 : print_encoding_base (_("LSDA pointer encoding: "),
6815 : : lsda_encoding);
6816 : : }
6817 [ # # ]: 0 : else if (*cp == 'P')
6818 : : {
6819 : : /* Personality. This field usually has a relocation
6820 : : attached pointing to __gcc_personality_v0. */
6821 : 0 : const unsigned char *startp = readp;
6822 : 0 : unsigned int encoding = *readp++;
6823 : 0 : uint64_t val = 0;
6824 : 0 : readp = read_encoded (encoding, readp,
6825 : 0 : readp - 1 + augmentationlen,
6826 : : &val, dbg);
6827 : :
6828 : 0 : while (++startp < readp)
6829 [ # # ]: 0 : printf ("%#x ", *startp);
6830 : :
6831 : 0 : putchar ('(');
6832 : 0 : print_encoding (encoding);
6833 : 0 : putchar (' ');
6834 [ # # ]: 0 : switch (encoding & 0xf)
6835 : : {
6836 : 0 : case DW_EH_PE_sleb128:
6837 : : case DW_EH_PE_sdata2:
6838 : : case DW_EH_PE_sdata4:
6839 : 0 : printf ("%" PRId64 ")\n", val);
6840 : : break;
6841 : 0 : default:
6842 : 0 : printf ("%#" PRIx64 ")\n", val);
6843 : : break;
6844 : : }
6845 : : }
6846 : : else
6847 : 0 : printf ("(%x)\n", *readp++);
6848 : :
6849 : 66 : ++cp;
6850 : : }
6851 : : }
6852 : :
6853 [ + - ]: 106 : if (likely (ptr_size == 4 || ptr_size == 8))
6854 : : {
6855 : 106 : struct cieinfo *newp = alloca (sizeof (*newp));
6856 : 106 : newp->cie_offset = offset;
6857 : 106 : newp->augmentation = augmentation;
6858 : 106 : newp->fde_encoding = fde_encoding;
6859 : 106 : newp->lsda_encoding = lsda_encoding;
6860 : 106 : newp->address_size = ptr_size;
6861 : 106 : newp->code_alignment_factor = code_alignment_factor;
6862 : 106 : newp->data_alignment_factor = data_alignment_factor;
6863 : 106 : newp->next = cies;
6864 : 106 : cies = newp;
6865 : : }
6866 : : }
6867 : : else
6868 : : {
6869 : : struct cieinfo *cie = cies;
6870 [ + - ]: 9317 : while (cie != NULL)
6871 [ + + - + ]: 18634 : if (is_eh_frame
6872 : 9275 : ? ((Dwarf_Off) start - cie_id) == (Dwarf_Off) cie->cie_offset
6873 : 42 : : cie_id == (Dwarf_Off) cie->cie_offset)
6874 : : break;
6875 : : else
6876 : 0 : cie = cie->next;
6877 [ - + ]: 9317 : if (unlikely (cie == NULL))
6878 : : {
6879 : 0 : puts ("invalid CIE reference in FDE");
6880 : 0 : return;
6881 : : }
6882 : :
6883 : : /* Initialize from CIE data. */
6884 : 9317 : fde_encoding = cie->fde_encoding;
6885 : 9317 : lsda_encoding = cie->lsda_encoding;
6886 : 9317 : ptr_size = encoded_ptr_size (fde_encoding, cie->address_size);
6887 : 9317 : code_alignment_factor = cie->code_alignment_factor;
6888 : 9317 : data_alignment_factor = cie->data_alignment_factor;
6889 : :
6890 : 9317 : const unsigned char *base = readp;
6891 : : // XXX There are sometimes relocations for this value
6892 [ - + + + : 18634 : initial_location = read_addr_unaligned_inc (ptr_size, dbg, readp);
+ + + + ]
6893 : 18634 : Dwarf_Word address_range
6894 [ + + + + : 18634 : = read_addr_unaligned_inc (ptr_size, dbg, readp);
+ + ]
6895 : :
6896 : : /* pcrel for an FDE address is relative to the runtime
6897 : : address of the start_address field itself. Sign extend
6898 : : if necessary to make sure the calculation is done on the
6899 : : full 64 bit address even when initial_location only holds
6900 : : the lower 32 bits. */
6901 : 9317 : Dwarf_Addr pc_start = initial_location;
6902 [ + + ]: 9317 : if (ptr_size == 4)
6903 : 9277 : pc_start = (uint64_t) (int32_t) pc_start;
6904 [ + + ]: 9317 : if ((fde_encoding & 0x70) == DW_EH_PE_pcrel)
6905 : 9269 : pc_start += ((uint64_t) shdr->sh_addr
6906 : 9269 : + (base - (const unsigned char *) data->d_buf)
6907 : 9269 : - bias);
6908 : :
6909 : 9317 : printf ("\n [%6tx] FDE length=%" PRIu64 " cie=[%6tx]\n"
6910 : : " CIE_pointer: %" PRIu64 "\n"
6911 : : " initial_location: ",
6912 : : offset, (uint64_t) unit_length,
6913 : : cie->cie_offset, (uint64_t) cie_id);
6914 : 9317 : print_dwarf_addr (dwflmod, cie->address_size,
6915 : : pc_start, initial_location);
6916 [ + + ]: 9317 : if ((fde_encoding & 0x70) == DW_EH_PE_pcrel)
6917 : : {
6918 : 18538 : vma_base = (((uint64_t) shdr->sh_offset
6919 : 9269 : + (base - (const unsigned char *) data->d_buf)
6920 : 9269 : + (uint64_t) initial_location)
6921 : : & (ptr_size == 4
6922 : : ? UINT64_C (0xffffffff)
6923 [ - + ]: 9269 : : UINT64_C (0xffffffffffffffff)));
6924 : 9269 : printf (_(" (offset: %#" PRIx64 ")"),
6925 : : (uint64_t) vma_base);
6926 : : }
6927 : :
6928 : 9317 : printf ("\n address_range: %#" PRIx64,
6929 : : (uint64_t) address_range);
6930 [ + + ]: 9317 : if ((fde_encoding & 0x70) == DW_EH_PE_pcrel)
6931 : 9269 : printf (_(" (end offset: %#" PRIx64 ")"),
6932 : 9269 : ((uint64_t) vma_base + (uint64_t) address_range)
6933 : : & (ptr_size == 4
6934 : : ? UINT64_C (0xffffffff)
6935 [ - + ]: 9269 : : UINT64_C (0xffffffffffffffff)));
6936 : 9317 : putchar ('\n');
6937 : :
6938 [ + + ]: 9317 : if (cie->augmentation[0] == 'z')
6939 : : {
6940 : 9269 : unsigned int augmentationlen;
6941 [ - + ]: 9269 : if (cieend - readp < 1)
6942 : 0 : goto invalid_data;
6943 : 9269 : get_uleb128 (augmentationlen, readp, cieend);
6944 : :
6945 [ - + ]: 9269 : if (augmentationlen > (size_t) (cieend - readp))
6946 : : {
6947 : 0 : error (0, 0, _("invalid augmentation length"));
6948 : 0 : readp = cieend;
6949 : 0 : continue;
6950 : : }
6951 : :
6952 [ - + ]: 9269 : if (augmentationlen > 0)
6953 : : {
6954 : 0 : const char *hdr = "Augmentation data:";
6955 : 0 : const char *cp = cie->augmentation + 1;
6956 : 0 : unsigned int u = 0;
6957 [ # # ]: 0 : while (*cp != '\0'
6958 [ # # ]: 0 : && cp < cie->augmentation + augmentationlen + 1)
6959 : : {
6960 [ # # ]: 0 : if (*cp == 'L')
6961 : : {
6962 : 0 : uint64_t lsda_pointer;
6963 : 0 : const unsigned char *p
6964 : 0 : = read_encoded (lsda_encoding, &readp[u],
6965 : : &readp[augmentationlen],
6966 : : &lsda_pointer, dbg);
6967 : 0 : u = p - readp;
6968 : 0 : printf (_("\
6969 : : %-26sLSDA pointer: %#" PRIx64 "\n"),
6970 : : hdr, lsda_pointer);
6971 : 0 : hdr = "";
6972 : : }
6973 : 0 : ++cp;
6974 : : }
6975 : :
6976 [ # # ]: 0 : while (u < augmentationlen)
6977 : : {
6978 : 0 : printf (" %-26s%#x\n", hdr, readp[u++]);
6979 : 0 : hdr = "";
6980 : : }
6981 : : }
6982 : :
6983 : 9269 : readp += augmentationlen;
6984 : : }
6985 : : }
6986 : :
6987 : : /* Handle the initialization instructions. */
6988 [ - + ]: 9423 : if (ptr_size != 4 && ptr_size !=8)
6989 : 0 : printf ("invalid CIE pointer size (%u), must be 4 or 8.\n", ptr_size);
6990 : : else
6991 : 9423 : print_cfa_program (readp, cieend, vma_base, code_alignment_factor,
6992 : : data_alignment_factor, version, ptr_size,
6993 : : fde_encoding, dwflmod, ebl, ehdr, dbg);
6994 : 9423 : readp = cieend;
6995 : : }
6996 : : }
6997 : :
6998 : :
6999 : : /* Returns the signedness (or false if it cannot be determined) and
7000 : : the byte size (or zero if it cannot be gotten) of the given DIE
7001 : : DW_AT_type attribute. Uses dwarf_peel_type and dwarf_aggregate_size. */
7002 : : static void
7003 : 157768 : die_type_sign_bytes (Dwarf_Die *die, bool *is_signed, int *bytes)
7004 : : {
7005 : 157768 : Dwarf_Attribute attr;
7006 : 157768 : Dwarf_Die type;
7007 : :
7008 : 157768 : *bytes = 0;
7009 : 157768 : *is_signed = false;
7010 : :
7011 [ + + ]: 157768 : if (dwarf_peel_type (dwarf_formref_die (dwarf_attr_integrate (die,
7012 : : DW_AT_type,
7013 : : &attr), &type),
7014 : : &type) == 0)
7015 : : {
7016 : 305 : Dwarf_Word val;
7017 : 305 : *is_signed = (dwarf_formudata (dwarf_attr (&type, DW_AT_encoding,
7018 : : &attr), &val) == 0
7019 [ + + + + ]: 305 : && (val == DW_ATE_signed || val == DW_ATE_signed_char));
7020 : :
7021 [ + - ]: 305 : if (dwarf_aggregate_size (&type, &val) == 0)
7022 : 305 : *bytes = val;
7023 : : }
7024 : 157768 : }
7025 : :
7026 : : struct attrcb_args
7027 : : {
7028 : : Dwfl_Module *dwflmod;
7029 : : Dwarf *dbg;
7030 : : Dwarf_Die *dies;
7031 : : int level;
7032 : : bool silent;
7033 : : bool is_split;
7034 : : unsigned int version;
7035 : : unsigned int addrsize;
7036 : : unsigned int offset_size;
7037 : : struct Dwarf_CU *cu;
7038 : : };
7039 : :
7040 : :
7041 : : static int
7042 : 3646250 : attr_callback (Dwarf_Attribute *attrp, void *arg)
7043 : : {
7044 : 3646250 : struct attrcb_args *cbargs = (struct attrcb_args *) arg;
7045 : 3646250 : const int level = cbargs->level;
7046 : 3646250 : Dwarf_Die *die = &cbargs->dies[level];
7047 : 3646250 : bool is_split = cbargs->is_split;
7048 : :
7049 [ + - ]: 3646250 : unsigned int attr = dwarf_whatattr (attrp);
7050 [ - + ]: 3646250 : if (unlikely (attr == 0))
7051 : : {
7052 [ # # ]: 0 : if (!cbargs->silent)
7053 : 0 : error (0, 0, _("DIE [%" PRIx64 "] "
7054 : : "cannot get attribute code: %s"),
7055 : : dwarf_dieoffset (die), dwarf_errmsg (-1));
7056 : 0 : return DWARF_CB_ABORT;
7057 : : }
7058 : :
7059 [ + - ]: 3646250 : unsigned int form = dwarf_whatform (attrp);
7060 [ - + ]: 3646250 : if (unlikely (form == 0))
7061 : : {
7062 [ # # ]: 0 : if (!cbargs->silent)
7063 : 0 : error (0, 0, _("DIE [%" PRIx64 "] "
7064 : : "cannot get attribute form: %s"),
7065 : : dwarf_dieoffset (die), dwarf_errmsg (-1));
7066 : 0 : return DWARF_CB_ABORT;
7067 : : }
7068 : :
7069 [ + + + + : 3646250 : switch (form)
+ + + +
- ]
7070 : : {
7071 : 64730 : case DW_FORM_addr:
7072 : : case DW_FORM_addrx:
7073 : : case DW_FORM_addrx1:
7074 : : case DW_FORM_addrx2:
7075 : : case DW_FORM_addrx3:
7076 : : case DW_FORM_addrx4:
7077 : : case DW_FORM_GNU_addr_index:
7078 [ + + ]: 64730 : if (!cbargs->silent)
7079 : : {
7080 : 64461 : Dwarf_Addr addr;
7081 [ - + ]: 64461 : if (unlikely (dwarf_formaddr (attrp, &addr) != 0))
7082 : : {
7083 : 0 : attrval_out:
7084 [ # # ]: 0 : if (!cbargs->silent)
7085 : 0 : error (0, 0, _("DIE [%" PRIx64 "] "
7086 : : "cannot get attribute '%s' (%s) value: "
7087 : : "%s"),
7088 : : dwarf_dieoffset (die),
7089 : : dwarf_attr_name (attr),
7090 : : dwarf_form_name (form),
7091 : : dwarf_errmsg (-1));
7092 : : /* Don't ABORT, it might be other attributes can be resolved. */
7093 : 0 : return DWARF_CB_OK;
7094 : : }
7095 [ + + ]: 64461 : if (form != DW_FORM_addr )
7096 : : {
7097 : 8 : Dwarf_Word word;
7098 [ - + ]: 8 : if (dwarf_formudata (attrp, &word) != 0)
7099 : 0 : goto attrval_out;
7100 : 8 : printf (" %*s%-20s (%s) [%" PRIx64 "] ",
7101 : : (int) (level * 2), "", dwarf_attr_name (attr),
7102 : : dwarf_form_name (form), word);
7103 : : }
7104 : : else
7105 : 64453 : printf (" %*s%-20s (%s) ",
7106 : : (int) (level * 2), "", dwarf_attr_name (attr),
7107 : : dwarf_form_name (form));
7108 : 64461 : print_dwarf_addr (cbargs->dwflmod, cbargs->addrsize, addr, addr);
7109 : 64461 : printf ("\n");
7110 : : }
7111 : 3272675 : break;
7112 : :
7113 : 610842 : case DW_FORM_indirect:
7114 : : case DW_FORM_strp:
7115 : : case DW_FORM_line_strp:
7116 : : case DW_FORM_strx:
7117 : : case DW_FORM_strx1:
7118 : : case DW_FORM_strx2:
7119 : : case DW_FORM_strx3:
7120 : : case DW_FORM_strx4:
7121 : : case DW_FORM_string:
7122 : : case DW_FORM_GNU_strp_alt:
7123 : : case DW_FORM_GNU_str_index:
7124 [ + + ]: 610842 : if (cbargs->silent)
7125 : : break;
7126 : 610094 : const char *str = dwarf_formstring (attrp);
7127 [ - + ]: 610094 : if (unlikely (str == NULL))
7128 : 0 : goto attrval_out;
7129 : 610094 : printf (" %*s%-20s (%s) \"%s\"\n",
7130 : : (int) (level * 2), "", dwarf_attr_name (attr),
7131 : : dwarf_form_name (form), str);
7132 : : break;
7133 : :
7134 : 760096 : case DW_FORM_ref_addr:
7135 : : case DW_FORM_ref_udata:
7136 : : case DW_FORM_ref8:
7137 : : case DW_FORM_ref4:
7138 : : case DW_FORM_ref2:
7139 : : case DW_FORM_ref1:
7140 : : case DW_FORM_GNU_ref_alt:
7141 : : case DW_FORM_ref_sup4:
7142 : : case DW_FORM_ref_sup8:
7143 [ + + ]: 760096 : if (cbargs->silent)
7144 : : break;
7145 : 759309 : Dwarf_Die ref;
7146 [ - + ]: 759309 : if (unlikely (dwarf_formref_die (attrp, &ref) == NULL))
7147 : 0 : goto attrval_out;
7148 : :
7149 : 759309 : printf (" %*s%-20s (%s) ",
7150 : : (int) (level * 2), "", dwarf_attr_name (attr),
7151 : : dwarf_form_name (form));
7152 [ + + ]: 759309 : if (is_split)
7153 : 33 : printf ("{%6" PRIxMAX "}\n", (uintmax_t) dwarf_dieoffset (&ref));
7154 : : else
7155 : 759276 : printf ("[%6" PRIxMAX "]\n", (uintmax_t) dwarf_dieoffset (&ref));
7156 : : break;
7157 : :
7158 : 4 : case DW_FORM_ref_sig8:
7159 [ + - ]: 4 : if (cbargs->silent)
7160 : : break;
7161 : 4 : printf (" %*s%-20s (%s) {%6" PRIx64 "}\n",
7162 : : (int) (level * 2), "", dwarf_attr_name (attr),
7163 : : dwarf_form_name (form),
7164 [ - + ]: 8 : (uint64_t) read_8ubyte_unaligned (attrp->cu->dbg, attrp->valp));
7165 : : break;
7166 : :
7167 : 2034080 : case DW_FORM_sec_offset:
7168 : : case DW_FORM_rnglistx:
7169 : : case DW_FORM_loclistx:
7170 : : case DW_FORM_implicit_const:
7171 : : case DW_FORM_udata:
7172 : : case DW_FORM_sdata:
7173 : : case DW_FORM_data8: /* Note no data16 here, we see that as block. */
7174 : : case DW_FORM_data4:
7175 : : case DW_FORM_data2:
7176 : 2034080 : case DW_FORM_data1:;
7177 : 2034080 : Dwarf_Word num;
7178 [ - + ]: 2034080 : if (unlikely (dwarf_formudata (attrp, &num) != 0))
7179 : 0 : goto attrval_out;
7180 : :
7181 : 2034080 : const char *valuestr = NULL;
7182 : 2034080 : bool as_hex_id = false;
7183 [ + + + + : 2034080 : switch (attr)
+ + - + +
- - - - -
- + - + +
+ ]
7184 : : {
7185 : : /* This case can take either a constant or a loclistptr. */
7186 : 250472 : case DW_AT_data_member_location:
7187 [ + - ]: 250472 : if (form != DW_FORM_sec_offset
7188 [ + + ]: 250472 : && (cbargs->version >= 4
7189 [ + - ]: 23806 : || (form != DW_FORM_data4 && form != DW_FORM_data8)))
7190 : : {
7191 [ + - ]: 250472 : if (!cbargs->silent)
7192 : 250472 : printf (" %*s%-20s (%s) %" PRIuMAX "\n",
7193 : : (int) (level * 2), "", dwarf_attr_name (attr),
7194 : : dwarf_form_name (form), (uintmax_t) num);
7195 : 250472 : return DWARF_CB_OK;
7196 : : }
7197 : 106817 : FALLTHROUGH;
7198 : :
7199 : : /* These cases always take a loclist[ptr] and no constant. */
7200 : : case DW_AT_location:
7201 : : case DW_AT_data_location:
7202 : : case DW_AT_vtable_elem_location:
7203 : : case DW_AT_string_length:
7204 : : case DW_AT_use_location:
7205 : : case DW_AT_frame_base:
7206 : : case DW_AT_return_addr:
7207 : : case DW_AT_static_link:
7208 : : case DW_AT_segment:
7209 : : case DW_AT_GNU_call_site_value:
7210 : : case DW_AT_GNU_call_site_data_value:
7211 : : case DW_AT_GNU_call_site_target:
7212 : : case DW_AT_GNU_call_site_target_clobbered:
7213 : : case DW_AT_GNU_locviews:
7214 : : {
7215 : 106817 : bool nlpt;
7216 [ + + ]: 106817 : if (cbargs->cu->version < 5)
7217 : : {
7218 [ + + ]: 106746 : if (! cbargs->is_split)
7219 : : {
7220 : 106726 : nlpt = notice_listptr (section_loc, &known_locsptr,
7221 : 106726 : cbargs->addrsize,
7222 : 106726 : cbargs->offset_size,
7223 : : cbargs->cu, num, attr);
7224 : : }
7225 : : else
7226 : : nlpt = true;
7227 : : }
7228 : : else
7229 : : {
7230 : : /* Only register for a real section offset. Otherwise
7231 : : it is a DW_FORM_loclistx which is just an index
7232 : : number and we should already have registered the
7233 : : section offset for the index when we saw the
7234 : : DW_AT_loclists_base CU attribute. */
7235 [ + + ]: 71 : if (form == DW_FORM_sec_offset)
7236 : 32 : nlpt = notice_listptr (section_loc, &known_loclistsptr,
7237 : 32 : cbargs->addrsize, cbargs->offset_size,
7238 : : cbargs->cu, num, attr);
7239 : : else
7240 : : nlpt = true;
7241 : :
7242 : : }
7243 : :
7244 [ + + ]: 106817 : if (!cbargs->silent)
7245 : : {
7246 [ + + - + ]: 106636 : if (cbargs->cu->version < 5 || form == DW_FORM_sec_offset)
7247 [ - + ]: 106629 : printf (" %*s%-20s (%s) location list [%6"
7248 : : PRIxMAX "]%s\n",
7249 : : (int) (level * 2), "", dwarf_attr_name (attr),
7250 : : dwarf_form_name (form), (uintmax_t) num,
7251 : : nlpt ? "" : " <WARNING offset too big>");
7252 : : else
7253 : 7 : printf (" %*s%-20s (%s) location index [%6"
7254 : : PRIxMAX "]\n",
7255 : : (int) (level * 2), "", dwarf_attr_name (attr),
7256 : : dwarf_form_name (form), (uintmax_t) num);
7257 : : }
7258 : : }
7259 : : return DWARF_CB_OK;
7260 : :
7261 : 5 : case DW_AT_loclists_base:
7262 : : {
7263 : 10 : bool nlpt = notice_listptr (section_loc, &known_loclistsptr,
7264 : 5 : cbargs->addrsize, cbargs->offset_size,
7265 : : cbargs->cu, num, attr);
7266 : :
7267 [ + + ]: 5 : if (!cbargs->silent)
7268 [ - + ]: 1 : printf (" %*s%-20s (%s) location list [%6" PRIxMAX "]%s\n",
7269 : : (int) (level * 2), "", dwarf_attr_name (attr),
7270 : : dwarf_form_name (form), (uintmax_t) num,
7271 : : nlpt ? "" : " <WARNING offset too big>");
7272 : : }
7273 : : return DWARF_CB_OK;
7274 : :
7275 : 16264 : case DW_AT_ranges:
7276 : : case DW_AT_start_scope:
7277 : : {
7278 : 16264 : bool nlpt;
7279 [ + + ]: 16264 : if (cbargs->cu->version < 5)
7280 : 16248 : nlpt = notice_listptr (section_ranges, &known_rangelistptr,
7281 : 16248 : cbargs->addrsize, cbargs->offset_size,
7282 : : cbargs->cu, num, attr);
7283 : : else
7284 : : {
7285 : : /* Only register for a real section offset. Otherwise
7286 : : it is a DW_FORM_rangelistx which is just an index
7287 : : number and we should already have registered the
7288 : : section offset for the index when we saw the
7289 : : DW_AT_rnglists_base CU attribute. */
7290 [ + + ]: 16 : if (form == DW_FORM_sec_offset)
7291 : 10 : nlpt = notice_listptr (section_ranges, &known_rnglistptr,
7292 : 10 : cbargs->addrsize, cbargs->offset_size,
7293 : : cbargs->cu, num, attr);
7294 : : else
7295 : : nlpt = true;
7296 : : }
7297 : :
7298 [ + + ]: 16264 : if (!cbargs->silent)
7299 : : {
7300 [ + + + + ]: 16222 : if (cbargs->cu->version < 5 || form == DW_FORM_sec_offset)
7301 [ - + ]: 16220 : printf (" %*s%-20s (%s) range list [%6"
7302 : : PRIxMAX "]%s\n",
7303 : : (int) (level * 2), "", dwarf_attr_name (attr),
7304 : : dwarf_form_name (form), (uintmax_t) num,
7305 : : nlpt ? "" : " <WARNING offset too big>");
7306 : : else
7307 : 2 : printf (" %*s%-20s (%s) range index [%6"
7308 : : PRIxMAX "]\n",
7309 : : (int) (level * 2), "", dwarf_attr_name (attr),
7310 : : dwarf_form_name (form), (uintmax_t) num);
7311 : : }
7312 : : }
7313 : : return DWARF_CB_OK;
7314 : :
7315 : 4 : case DW_AT_rnglists_base:
7316 : : {
7317 : 8 : bool nlpt = notice_listptr (section_ranges, &known_rnglistptr,
7318 : 4 : cbargs->addrsize, cbargs->offset_size,
7319 : : cbargs->cu, num, attr);
7320 [ + + ]: 4 : if (!cbargs->silent)
7321 [ - + ]: 2 : printf (" %*s%-20s (%s) range list [%6"
7322 : : PRIxMAX "]%s\n",
7323 : : (int) (level * 2), "", dwarf_attr_name (attr),
7324 : : dwarf_form_name (form), (uintmax_t) num,
7325 : : nlpt ? "" : " <WARNING offset too big>");
7326 : : }
7327 : : return DWARF_CB_OK;
7328 : :
7329 : 13 : case DW_AT_addr_base:
7330 : : case DW_AT_GNU_addr_base:
7331 : : {
7332 : 26 : bool addrbase = notice_listptr (section_addr, &known_addrbases,
7333 : 13 : cbargs->addrsize,
7334 : 13 : cbargs->offset_size,
7335 : : cbargs->cu, num, attr);
7336 [ + + ]: 13 : if (!cbargs->silent)
7337 [ - + ]: 5 : printf (" %*s%-20s (%s) address base [%6"
7338 : : PRIxMAX "]%s\n",
7339 : : (int) (level * 2), "", dwarf_attr_name (attr),
7340 : : dwarf_form_name (form), (uintmax_t) num,
7341 : : addrbase ? "" : " <WARNING offset too big>");
7342 : : }
7343 : : return DWARF_CB_OK;
7344 : :
7345 : 0 : case DW_AT_str_offsets_base:
7346 : : {
7347 : 0 : bool stroffbase = notice_listptr (section_str, &known_stroffbases,
7348 : 0 : cbargs->addrsize,
7349 : 0 : cbargs->offset_size,
7350 : : cbargs->cu, num, attr);
7351 [ # # ]: 0 : if (!cbargs->silent)
7352 [ - - ]: 373575 : printf (" %*s%-20s (%s) str offsets base [%6"
7353 : : PRIxMAX "]%s\n",
7354 : : (int) (level * 2), "", dwarf_attr_name (attr),
7355 : : dwarf_form_name (form), (uintmax_t) num,
7356 : : stroffbase ? "" : " <WARNING offset too big>");
7357 : : }
7358 : : return DWARF_CB_OK;
7359 : :
7360 : 1700 : case DW_AT_language:
7361 : 1700 : valuestr = dwarf_lang_name (num);
7362 : 1700 : break;
7363 : 30272 : case DW_AT_encoding:
7364 : 30272 : valuestr = dwarf_encoding_name (num);
7365 : 30272 : break;
7366 : 0 : case DW_AT_accessibility:
7367 : 0 : valuestr = dwarf_access_name (num);
7368 : 0 : break;
7369 : 0 : case DW_AT_defaulted:
7370 : 0 : valuestr = dwarf_defaulted_name (num);
7371 : 0 : break;
7372 : 0 : case DW_AT_visibility:
7373 : 0 : valuestr = dwarf_visibility_name (num);
7374 : 0 : break;
7375 : 0 : case DW_AT_virtuality:
7376 : 0 : valuestr = dwarf_virtuality_name (num);
7377 : 0 : break;
7378 : 0 : case DW_AT_identifier_case:
7379 : 0 : valuestr = dwarf_identifier_case_name (num);
7380 : 0 : break;
7381 : 0 : case DW_AT_calling_convention:
7382 : 0 : valuestr = dwarf_calling_convention_name (num);
7383 : 0 : break;
7384 : 3557 : case DW_AT_inline:
7385 : 3557 : valuestr = dwarf_inline_name (num);
7386 : 3557 : break;
7387 : 0 : case DW_AT_ordering:
7388 : 0 : valuestr = dwarf_ordering_name (num);
7389 : 0 : break;
7390 : 443001 : case DW_AT_decl_file:
7391 : : case DW_AT_call_file:
7392 : : {
7393 [ + + ]: 443001 : if (cbargs->silent)
7394 : : break;
7395 : :
7396 : : /* Try to get the actual file, the current interface only
7397 : : gives us full paths, but we only want to show the file
7398 : : name for now. */
7399 : 442580 : Dwarf_Die cudie;
7400 [ + - ]: 442580 : if (dwarf_cu_die (cbargs->cu, &cudie,
7401 : : NULL, NULL, NULL, NULL, NULL, NULL) != NULL)
7402 : : {
7403 : 442580 : Dwarf_Files *files;
7404 : 442580 : size_t nfiles;
7405 [ + - ]: 442580 : if (dwarf_getsrcfiles (&cudie, &files, &nfiles) == 0)
7406 : : {
7407 : 442580 : valuestr = dwarf_filesrc (files, num, NULL, NULL);
7408 [ + - ]: 442580 : if (valuestr != NULL)
7409 : : {
7410 : 442580 : char *filename = strrchr (valuestr, '/');
7411 [ + - ]: 442580 : if (filename != NULL)
7412 : 442580 : valuestr = filename + 1;
7413 : : }
7414 : : else
7415 : 0 : error (0, 0, _("invalid file (%" PRId64 "): %s"),
7416 : : num, dwarf_errmsg (-1));
7417 : : }
7418 : : else
7419 : 0 : error (0, 0, _("no srcfiles for CU [%" PRIx64 "]"),
7420 : : dwarf_dieoffset (&cudie));
7421 : : }
7422 : : else
7423 : 0 : error (0, 0, _("couldn't get DWARF CU: %s"),
7424 : : dwarf_errmsg (-1));
7425 [ - + ]: 442580 : if (valuestr == NULL)
7426 : : valuestr = "???";
7427 : : }
7428 : 442580 : break;
7429 : 13 : case DW_AT_GNU_dwo_id:
7430 : 13 : as_hex_id = true;
7431 : 13 : break;
7432 : :
7433 : : default:
7434 : : /* Nothing. */
7435 : : break;
7436 : : }
7437 : :
7438 [ + + ]: 1660505 : if (cbargs->silent)
7439 : : break;
7440 : :
7441 : : /* When highpc is in constant form it is relative to lowpc.
7442 : : In that case also show the address. */
7443 : 1658664 : Dwarf_Addr highpc;
7444 [ + + + - ]: 1658664 : if (attr == DW_AT_high_pc && dwarf_highpc (die, &highpc) == 0)
7445 : : {
7446 : 12261 : printf (" %*s%-20s (%s) %" PRIuMAX " (",
7447 : : (int) (level * 2), "", dwarf_attr_name (attr),
7448 : : dwarf_form_name (form), (uintmax_t) num);
7449 : 12261 : print_dwarf_addr (cbargs->dwflmod, cbargs->addrsize, highpc, highpc);
7450 : 12261 : printf (")\n");
7451 : : }
7452 : : else
7453 : : {
7454 [ + + ]: 1646403 : if (as_hex_id)
7455 : : {
7456 : 2 : printf (" %*s%-20s (%s) 0x%.16" PRIx64 "\n",
7457 : : (int) (level * 2), "", dwarf_attr_name (attr),
7458 : : dwarf_form_name (form), num);
7459 : : }
7460 : : else
7461 : : {
7462 : 1646401 : Dwarf_Sword snum = 0;
7463 : 1646401 : bool is_signed;
7464 : 1646401 : int bytes = 0;
7465 [ + + ]: 1646401 : if (attr == DW_AT_const_value)
7466 : 157764 : die_type_sign_bytes (die, &is_signed, &bytes);
7467 : : else
7468 : 1488637 : is_signed = (form == DW_FORM_sdata
7469 : 1488637 : || form == DW_FORM_implicit_const);
7470 : :
7471 [ + + ]: 1646401 : if (is_signed)
7472 [ - + ]: 138 : if (unlikely (dwarf_formsdata (attrp, &snum) != 0))
7473 : 0 : goto attrval_out;
7474 : :
7475 [ + + ]: 1646401 : if (valuestr == NULL)
7476 : : {
7477 : 1168595 : printf (" %*s%-20s (%s) ",
7478 : : (int) (level * 2), "", dwarf_attr_name (attr),
7479 : : dwarf_form_name (form));
7480 : : }
7481 : : else
7482 : : {
7483 : 477806 : printf (" %*s%-20s (%s) %s (",
7484 : : (int) (level * 2), "", dwarf_attr_name (attr),
7485 : : dwarf_form_name (form), valuestr);
7486 : : }
7487 : :
7488 [ + + + + : 1646401 : switch (bytes)
+ ]
7489 : : {
7490 : 2 : case 1:
7491 [ + + ]: 2 : if (is_signed)
7492 : 1 : printf ("%" PRId8, (int8_t) snum);
7493 : : else
7494 : 1 : printf ("%" PRIu8, (uint8_t) num);
7495 : : break;
7496 : :
7497 : 2 : case 2:
7498 [ + + ]: 2 : if (is_signed)
7499 : 1 : printf ("%" PRId16, (int16_t) snum);
7500 : : else
7501 : 1 : printf ("%" PRIu16, (uint16_t) num);
7502 : : break;
7503 : :
7504 : 121 : case 4:
7505 [ + + ]: 121 : if (is_signed)
7506 : 114 : printf ("%" PRId32, (int32_t) snum);
7507 : : else
7508 : 7 : printf ("%" PRIu32, (uint32_t) num);
7509 : : break;
7510 : :
7511 : 176 : case 8:
7512 [ + + ]: 176 : if (is_signed)
7513 : 1 : printf ("%" PRId64, (int64_t) snum);
7514 : : else
7515 : 175 : printf ("%" PRIu64, (uint64_t) num);
7516 : : break;
7517 : :
7518 : 1646100 : default:
7519 [ + + ]: 1646100 : if (is_signed)
7520 : 21 : printf ("%" PRIdMAX, (intmax_t) snum);
7521 : : else
7522 : 1646079 : printf ("%" PRIuMAX, (uintmax_t) num);
7523 : : break;
7524 : : }
7525 : :
7526 : : /* Make clear if we switched from a signed encoding to
7527 : : an unsigned value. */
7528 [ + + ]: 1646401 : if (attr == DW_AT_const_value
7529 [ + + ]: 157764 : && (form == DW_FORM_sdata || form == DW_FORM_implicit_const)
7530 [ + + ]: 1218 : && !is_signed)
7531 : 1212 : printf (" (%" PRIdMAX ")", (intmax_t) num);
7532 : :
7533 [ + + ]: 1646401 : if (valuestr == NULL)
7534 : 1168595 : printf ("\n");
7535 : : else
7536 : 1646401 : printf (")\n");
7537 : : }
7538 : : }
7539 : : break;
7540 : :
7541 : 9122 : case DW_FORM_flag:
7542 [ + + ]: 9122 : if (cbargs->silent)
7543 : : break;
7544 : 9048 : bool flag;
7545 [ - + ]: 9048 : if (unlikely (dwarf_formflag (attrp, &flag) != 0))
7546 : 0 : goto attrval_out;
7547 : :
7548 : 18096 : printf (" %*s%-20s (%s) %s\n",
7549 : : (int) (level * 2), "", dwarf_attr_name (attr),
7550 [ + - ]: 9048 : dwarf_form_name (form), flag ? yes_str : no_str);
7551 : : break;
7552 : :
7553 : 59952 : case DW_FORM_flag_present:
7554 [ + + ]: 59952 : if (cbargs->silent)
7555 : : break;
7556 : 59678 : printf (" %*s%-20s (%s) %s\n",
7557 : : (int) (level * 2), "", dwarf_attr_name (attr),
7558 : : dwarf_form_name (form), yes_str);
7559 : : break;
7560 : :
7561 : 107424 : case DW_FORM_exprloc:
7562 : : case DW_FORM_block4:
7563 : : case DW_FORM_block2:
7564 : : case DW_FORM_block1:
7565 : : case DW_FORM_block:
7566 : : case DW_FORM_data16: /* DWARF5 calls this a constant class. */
7567 [ + + ]: 107424 : if (cbargs->silent)
7568 : : break;
7569 : 107295 : Dwarf_Block block;
7570 [ - + ]: 107295 : if (unlikely (dwarf_formblock (attrp, &block) != 0))
7571 : 0 : goto attrval_out;
7572 : :
7573 : 107295 : printf (" %*s%-20s (%s) ",
7574 : : (int) (level * 2), "", dwarf_attr_name (attr),
7575 : : dwarf_form_name (form));
7576 : :
7577 [ - + + ]: 107295 : switch (attr)
7578 : : {
7579 : 0 : default:
7580 [ # # ]: 0 : if (form != DW_FORM_exprloc)
7581 : : {
7582 : 0 : print_block (block.length, block.data);
7583 : 0 : break;
7584 : : }
7585 : 107291 : FALLTHROUGH;
7586 : :
7587 : : case DW_AT_location:
7588 : : case DW_AT_data_location:
7589 : : case DW_AT_data_member_location:
7590 : : case DW_AT_vtable_elem_location:
7591 : : case DW_AT_string_length:
7592 : : case DW_AT_use_location:
7593 : : case DW_AT_frame_base:
7594 : : case DW_AT_return_addr:
7595 : : case DW_AT_static_link:
7596 : : case DW_AT_allocated:
7597 : : case DW_AT_associated:
7598 : : case DW_AT_bit_size:
7599 : : case DW_AT_bit_offset:
7600 : : case DW_AT_bit_stride:
7601 : : case DW_AT_byte_size:
7602 : : case DW_AT_byte_stride:
7603 : : case DW_AT_count:
7604 : : case DW_AT_lower_bound:
7605 : : case DW_AT_upper_bound:
7606 : : case DW_AT_GNU_call_site_value:
7607 : : case DW_AT_GNU_call_site_data_value:
7608 : : case DW_AT_GNU_call_site_target:
7609 : : case DW_AT_GNU_call_site_target_clobbered:
7610 [ + + ]: 107291 : if (form == DW_FORM_exprloc
7611 [ + - ]: 70 : || (form != DW_FORM_data16
7612 [ + - ]: 70 : && attrp->cu->version < 4)) /* blocks were expressions. */
7613 : : {
7614 : 107291 : putchar ('\n');
7615 : 107291 : print_ops (cbargs->dwflmod, cbargs->dbg,
7616 : 107291 : 12 + level * 2, 12 + level * 2,
7617 : : cbargs->version, cbargs->addrsize, cbargs->offset_size,
7618 : 107291 : attrp->cu, block.length, block.data);
7619 : : }
7620 : : else
7621 : 0 : print_block (block.length, block.data);
7622 : : break;
7623 : :
7624 : 4 : case DW_AT_discr_list:
7625 [ - + ]: 4 : if (block.length == 0)
7626 : 0 : puts ("<default>");
7627 [ + - ]: 4 : else if (form != DW_FORM_data16)
7628 : : {
7629 : 4 : const unsigned char *readp = block.data;
7630 : 4 : const unsigned char *readendp = readp + block.length;
7631 : :
7632 : : /* See if we are dealing with a signed or unsigned
7633 : : values. If the parent of this variant DIE is a
7634 : : variant_part then it will either have a discriminant
7635 : : which points to the member which type is the
7636 : : discriminant type. Or the variant_part itself has a
7637 : : type representing the discriminant. */
7638 : 4 : bool is_signed = false;
7639 [ + - ]: 4 : if (level > 0)
7640 : : {
7641 : 4 : Dwarf_Die *parent = &cbargs->dies[level - 1];
7642 [ + - ]: 4 : if (dwarf_tag (die) == DW_TAG_variant
7643 [ + - ]: 4 : && dwarf_tag (parent) == DW_TAG_variant_part)
7644 : : {
7645 : 4 : Dwarf_Die member;
7646 : 4 : Dwarf_Attribute discr_attr;
7647 : 4 : int bytes;
7648 [ + - ]: 4 : if (dwarf_formref_die (dwarf_attr (parent,
7649 : : DW_AT_discr,
7650 : : &discr_attr),
7651 : : &member) != NULL)
7652 : 4 : die_type_sign_bytes (&member, &is_signed, &bytes);
7653 : : else
7654 : 0 : die_type_sign_bytes (parent, &is_signed, &bytes);
7655 : : }
7656 : : }
7657 : 12 : while (readp < readendp)
7658 : : {
7659 : 8 : int d = (int) *readp++;
7660 : 8 : printf ("%s ", dwarf_discr_list_name (d));
7661 [ - + ]: 8 : if (readp >= readendp)
7662 : 0 : goto attrval_out;
7663 : :
7664 : 8 : Dwarf_Word val;
7665 : 8 : Dwarf_Sword sval;
7666 [ + + ]: 8 : if (d == DW_DSC_label)
7667 : : {
7668 [ + + ]: 4 : if (is_signed)
7669 : : {
7670 : 2 : get_sleb128 (sval, readp, readendp);
7671 : 2 : printf ("%" PRId64 "", sval);
7672 : : }
7673 : : else
7674 : : {
7675 : 2 : get_uleb128 (val, readp, readendp);
7676 : 2 : printf ("%" PRIu64 "", val);
7677 : : }
7678 : : }
7679 [ + - ]: 4 : else if (d == DW_DSC_range)
7680 : : {
7681 [ + + ]: 4 : if (is_signed)
7682 : : {
7683 : 3 : get_sleb128 (sval, readp, readendp);
7684 : 3 : printf ("%" PRId64 "..", sval);
7685 [ - + ]: 3 : if (readp >= readendp)
7686 : 0 : goto attrval_out;
7687 : 3 : get_sleb128 (sval, readp, readendp);
7688 : 3 : printf ("%" PRId64 "", sval);
7689 : : }
7690 : : else
7691 : : {
7692 : 1 : get_uleb128 (val, readp, readendp);
7693 : 1 : printf ("%" PRIu64 "..", val);
7694 [ - + ]: 1 : if (readp >= readendp)
7695 : 0 : goto attrval_out;
7696 : 1 : get_uleb128 (val, readp, readendp);
7697 : 1 : printf ("%" PRIu64 "", val);
7698 : : }
7699 : : }
7700 : : else
7701 : : {
7702 : 0 : print_block (readendp - readp, readp);
7703 : 0 : break;
7704 : : }
7705 [ + + ]: 8 : if (readp < readendp)
7706 [ + + ]: 16 : printf (", ");
7707 : : }
7708 : 4 : putchar ('\n');
7709 : : }
7710 : : else
7711 : 0 : print_block (block.length, block.data);
7712 : : break;
7713 : : }
7714 : : break;
7715 : :
7716 : 0 : default:
7717 [ # # ]: 0 : if (cbargs->silent)
7718 : : break;
7719 : 0 : printf (" %*s%-20s (%s) ???\n",
7720 : : (int) (level * 2), "", dwarf_attr_name (attr),
7721 : : dwarf_form_name (form));
7722 : : break;
7723 : : }
7724 : :
7725 : 3272675 : return DWARF_CB_OK;
7726 : : }
7727 : :
7728 : : static void
7729 : 98 : print_debug_units (Dwfl_Module *dwflmod,
7730 : : Ebl *ebl, GElf_Ehdr *ehdr __attribute__ ((unused)),
7731 : : Elf_Scn *scn, GElf_Shdr *shdr,
7732 : : Dwarf *dbg, bool debug_types)
7733 : : {
7734 [ + + + + ]: 98 : const bool silent = !(print_debug_sections & section_info) && !debug_types;
7735 : 98 : const char *secname = section_name (ebl, shdr);
7736 : :
7737 [ + + ]: 98 : if (!silent)
7738 : 63 : printf (_("\
7739 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n [Offset]\n"),
7740 : 63 : elf_ndxscn (scn), secname, (uint64_t) shdr->sh_offset);
7741 : :
7742 : : /* If the section is empty we don't have to do anything. */
7743 [ + + - + ]: 98 : if (!silent && shdr->sh_size == 0)
7744 : 0 : return;
7745 : :
7746 : 98 : int maxdies = 20;
7747 : 98 : Dwarf_Die *dies = (Dwarf_Die *) xmalloc (maxdies * sizeof (Dwarf_Die));
7748 : :
7749 : : /* New compilation unit. */
7750 : 98 : Dwarf_Half version;
7751 : :
7752 : 98 : Dwarf_Die result;
7753 : 98 : Dwarf_Off abbroffset;
7754 : 98 : uint8_t addrsize;
7755 : 98 : uint8_t offsize;
7756 : 98 : uint64_t unit_id;
7757 : 98 : Dwarf_Off subdie_off;
7758 : :
7759 : 98 : int unit_res;
7760 : 98 : Dwarf_CU *cu;
7761 : 98 : Dwarf_CU cu_mem;
7762 : 98 : uint8_t unit_type;
7763 : 98 : Dwarf_Die cudie;
7764 : :
7765 : : /* We cheat a little because we want to see only the CUs from .debug_info
7766 : : or .debug_types. We know the Dwarf_CU struct layout. Set it up at
7767 : : the end of .debug_info if we want .debug_types only. Check the returned
7768 : : Dwarf_CU is still in the expected section. */
7769 [ + + ]: 98 : if (debug_types)
7770 : : {
7771 : 1 : cu_mem.dbg = dbg;
7772 : 1 : cu_mem.end = dbg->sectiondata[IDX_debug_info]->d_size;
7773 : 1 : cu_mem.sec_idx = IDX_debug_info;
7774 : 1 : cu = &cu_mem;
7775 : : }
7776 : : else
7777 : 97 : cu = NULL;
7778 : :
7779 : : next_cu:
7780 : 1807 : unit_res = dwarf_get_units (dbg, cu, &cu, &version, &unit_type,
7781 : : &cudie, NULL);
7782 [ + + ]: 1807 : if (unit_res == 1)
7783 : 97 : goto do_return;
7784 : :
7785 [ - + ]: 1710 : if (unit_res == -1)
7786 : : {
7787 [ # # ]: 0 : if (!silent)
7788 : 0 : error (0, 0, _("cannot get next unit: %s"), dwarf_errmsg (-1));
7789 : 0 : goto do_return;
7790 : : }
7791 : :
7792 [ + + + + ]: 3418 : if (cu->sec_idx != (size_t) (debug_types ? IDX_debug_types : IDX_debug_info))
7793 : 1 : goto do_return;
7794 : :
7795 : 1709 : dwarf_cu_die (cu, &result, NULL, &abbroffset, &addrsize, &offsize,
7796 : : &unit_id, &subdie_off);
7797 : :
7798 [ + + ]: 1709 : if (!silent)
7799 : : {
7800 : 1660 : Dwarf_Off offset = cu->start;
7801 [ + + + - ]: 1660 : if (debug_types && version < 5)
7802 : 2 : {
7803 : 2 : Dwarf_Die typedie;
7804 : 2 : Dwarf_Off dieoffset;
7805 : 2 : dieoffset = dwarf_dieoffset (dwarf_offdie_types (dbg, cu->start
7806 : : + subdie_off,
7807 : : &typedie));
7808 : 2 : printf (_(" Type unit at offset %" PRIu64 ":\n"
7809 : : " Version: %" PRIu16
7810 : : ", Abbreviation section offset: %" PRIu64
7811 : : ", Address size: %" PRIu8
7812 : : ", Offset size: %" PRIu8
7813 : : "\n Type signature: %#" PRIx64
7814 : : ", Type offset: %#" PRIx64 " [%" PRIx64 "]\n"),
7815 : : (uint64_t) offset, version, abbroffset, addrsize, offsize,
7816 : : unit_id, (uint64_t) subdie_off, dieoffset);
7817 : : }
7818 : : else
7819 : : {
7820 : 1658 : printf (_(" Compilation unit at offset %" PRIu64 ":\n"
7821 : : " Version: %" PRIu16
7822 : : ", Abbreviation section offset: %" PRIu64
7823 : : ", Address size: %" PRIu8
7824 : : ", Offset size: %" PRIu8 "\n"),
7825 : : (uint64_t) offset, version, abbroffset, addrsize, offsize);
7826 : :
7827 [ + + ]: 1658 : if (version >= 5 || (unit_type != DW_UT_compile
7828 [ + + ]: 1653 : && unit_type != DW_UT_partial))
7829 : : {
7830 : 6 : printf (_(" Unit type: %s (%" PRIu8 ")"),
7831 : : dwarf_unit_name (unit_type), unit_type);
7832 : 6 : if (unit_type == DW_UT_type
7833 [ + + ]: 6 : || unit_type == DW_UT_skeleton
7834 [ + - ]: 1 : || unit_type == DW_UT_split_compile
7835 [ - + ]: 1 : || unit_type == DW_UT_split_type)
7836 : 5 : printf (", Unit id: 0x%.16" PRIx64 "", unit_id);
7837 : 6 : if (unit_type == DW_UT_type
7838 [ - + ]: 6 : || unit_type == DW_UT_split_type)
7839 : : {
7840 : 0 : Dwarf_Die typedie;
7841 : 0 : Dwarf_Off dieoffset;
7842 : 0 : dwarf_cu_info (cu, NULL, NULL, NULL, &typedie,
7843 : : NULL, NULL, NULL);
7844 : 0 : dieoffset = dwarf_dieoffset (&typedie);
7845 : 0 : printf (", Unit DIE off: %#" PRIx64 " [%" PRIx64 "]",
7846 : : subdie_off, dieoffset);
7847 : : }
7848 : 6 : printf ("\n");
7849 : : }
7850 : : }
7851 : : }
7852 : :
7853 [ + - ]: 1709 : if (version < 2 || version > 5
7854 [ + - - + ]: 1709 : || unit_type < DW_UT_compile || unit_type > DW_UT_split_type)
7855 : : {
7856 [ # # ]: 0 : if (!silent)
7857 : 0 : error (0, 0, _("unknown version (%d) or unit type (%d)"),
7858 : : version, unit_type);
7859 : 0 : goto next_cu;
7860 : : }
7861 : :
7862 : 1709 : struct attrcb_args args =
7863 : : {
7864 : : .dwflmod = dwflmod,
7865 : : .silent = silent,
7866 : : .version = version,
7867 : : .addrsize = addrsize,
7868 : : .offset_size = offsize
7869 : : };
7870 : :
7871 : 1709 : bool is_split = false;
7872 : 1709 : int level = 0;
7873 : 1709 : dies[0] = cudie;
7874 : 1709 : args.cu = dies[0].cu;
7875 : 1709 : args.dbg = dbg;
7876 : 1709 : args.is_split = is_split;
7877 : :
7878 : : /* We might return here again for the split CU subdie. */
7879 : : do_cu:
7880 : 949747 : do
7881 : : {
7882 : 949747 : Dwarf_Off offset = dwarf_dieoffset (&dies[level]);
7883 [ - + ]: 949747 : if (unlikely (offset == (Dwarf_Off) -1))
7884 : : {
7885 [ # # ]: 0 : if (!silent)
7886 : 0 : error (0, 0, _("cannot get DIE offset: %s"),
7887 : : dwarf_errmsg (-1));
7888 : 0 : goto do_return;
7889 : : }
7890 : :
7891 : 949747 : int tag = dwarf_tag (&dies[level]);
7892 [ - + ]: 949747 : if (unlikely (tag == DW_TAG_invalid))
7893 : : {
7894 [ # # ]: 0 : if (!silent)
7895 : 0 : error (0, 0, _("cannot get tag of DIE at offset [%" PRIx64
7896 : : "] in section '%s': %s"),
7897 : : (uint64_t) offset, secname, dwarf_errmsg (-1));
7898 : 0 : goto do_return;
7899 : : }
7900 : :
7901 [ + + ]: 949747 : if (!silent)
7902 : : {
7903 : 948759 : unsigned int code = dwarf_getabbrevcode (dies[level].abbrev);
7904 [ + + ]: 948759 : if (is_split)
7905 : 48 : printf (" {%6" PRIx64 "} ", (uint64_t) offset);
7906 : : else
7907 : 948711 : printf (" [%6" PRIx64 "] ", (uint64_t) offset);
7908 : 948759 : printf ("%*s%-20s abbrev: %u\n", (int) (level * 2), "",
7909 : : dwarf_tag_name (tag), code);
7910 : : }
7911 : :
7912 : : /* Print the attribute values. */
7913 : 949747 : args.level = level;
7914 : 949747 : args.dies = dies;
7915 : 949747 : (void) dwarf_getattrs (&dies[level], attr_callback, &args, 0);
7916 : :
7917 : : /* Make room for the next level's DIE. */
7918 [ - + ]: 949747 : if (level + 1 == maxdies)
7919 : 0 : dies = (Dwarf_Die *) xrealloc (dies,
7920 : 0 : (maxdies += 10)
7921 : : * sizeof (Dwarf_Die));
7922 : :
7923 : 949747 : int res = dwarf_child (&dies[level], &dies[level + 1]);
7924 [ + + ]: 949747 : if (res > 0)
7925 : : {
7926 [ + + ]: 949747 : while ((res = dwarf_siblingof (&dies[level], &dies[level])) == 1)
7927 [ + + ]: 126176 : if (level-- == 0)
7928 : : break;
7929 : :
7930 [ - + ]: 825284 : if (unlikely (res == -1))
7931 : : {
7932 [ # # ]: 0 : if (!silent)
7933 : 0 : error (0, 0, _("cannot get next DIE: %s\n"),
7934 : : dwarf_errmsg (-1));
7935 : 0 : goto do_return;
7936 : : }
7937 : : }
7938 [ - + ]: 124463 : else if (unlikely (res < 0))
7939 : : {
7940 [ # # ]: 0 : if (!silent)
7941 : 0 : error (0, 0, _("cannot get next DIE: %s"),
7942 : : dwarf_errmsg (-1));
7943 : 0 : goto do_return;
7944 : : }
7945 : : else
7946 : : ++level;
7947 : : }
7948 [ + + ]: 949747 : while (level >= 0);
7949 : :
7950 : : /* We might want to show the split compile unit if this was a skeleton.
7951 : : We need to scan it if we are requesting printing .debug_ranges for
7952 : : DWARF4 since GNU DebugFission uses "offsets" into the main ranges
7953 : : section. */
7954 [ + + ]: 1713 : if (unit_type == DW_UT_skeleton
7955 [ + + + + ]: 13 : && ((!silent && show_split_units)
7956 [ + + + + ]: 10 : || (version < 5 && (print_debug_sections & section_ranges) != 0)))
7957 : : {
7958 : 5 : Dwarf_Die subdie;
7959 [ + - ]: 5 : if (dwarf_cu_info (cu, NULL, NULL, NULL, &subdie, NULL, NULL, NULL) != 0
7960 [ + + ]: 5 : || dwarf_tag (&subdie) == DW_TAG_invalid)
7961 : : {
7962 [ + - ]: 1 : if (!silent)
7963 : : {
7964 : 1 : Dwarf_Attribute dwo_at;
7965 : 2 : const char *dwo_name =
7966 : 1 : (dwarf_formstring (dwarf_attr (&cudie, DW_AT_dwo_name,
7967 : : &dwo_at))
7968 [ - + ]: 1 : ?: (dwarf_formstring (dwarf_attr (&cudie, DW_AT_GNU_dwo_name,
7969 : : &dwo_at))
7970 [ # # ]: 0 : ?: "<unknown>"));
7971 : 1 : fprintf (stderr,
7972 : : "Could not find split unit '%s', id: %" PRIx64 "\n",
7973 : : dwo_name, unit_id);
7974 : : }
7975 : : }
7976 : : else
7977 : : {
7978 : 4 : Dwarf_CU *split_cu = subdie.cu;
7979 : 4 : dwarf_cu_die (split_cu, &result, NULL, &abbroffset,
7980 : : &addrsize, &offsize, &unit_id, &subdie_off);
7981 : 4 : Dwarf_Off offset = cu->start;
7982 : :
7983 [ + + ]: 4 : if (!silent)
7984 : : {
7985 : 2 : printf (_(" Split compilation unit at offset %"
7986 : : PRIu64 ":\n"
7987 : : " Version: %" PRIu16
7988 : : ", Abbreviation section offset: %" PRIu64
7989 : : ", Address size: %" PRIu8
7990 : : ", Offset size: %" PRIu8 "\n"),
7991 : : (uint64_t) offset, version, abbroffset,
7992 : : addrsize, offsize);
7993 : 2 : printf (_(" Unit type: %s (%" PRIu8 ")"),
7994 : : dwarf_unit_name (unit_type), unit_type);
7995 : 2 : printf (", Unit id: 0x%.16" PRIx64 "", unit_id);
7996 : 2 : printf ("\n");
7997 : : }
7998 : :
7999 : 4 : unit_type = DW_UT_split_compile;
8000 : 4 : is_split = true;
8001 : 4 : level = 0;
8002 : 4 : dies[0] = subdie;
8003 : 4 : args.cu = dies[0].cu;
8004 : 4 : args.dbg = split_cu->dbg;
8005 : 4 : args.is_split = is_split;
8006 : 4 : goto do_cu;
8007 : : }
8008 : : }
8009 : :
8010 : : /* And again... */
8011 : 1709 : goto next_cu;
8012 : :
8013 : 98 : do_return:
8014 : 98 : free (dies);
8015 : : }
8016 : :
8017 : : static void
8018 : 97 : print_debug_info_section (Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr,
8019 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
8020 : : {
8021 : 20 : print_debug_units (dwflmod, ebl, ehdr, scn, shdr, dbg, false);
8022 : 20 : }
8023 : :
8024 : : static void
8025 : 1 : print_debug_types_section (Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr,
8026 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
8027 : : {
8028 : 1 : print_debug_units (dwflmod, ebl, ehdr, scn, shdr, dbg, true);
8029 : 1 : }
8030 : :
8031 : :
8032 : : static void
8033 : 21 : print_decoded_line_section (Dwfl_Module *dwflmod, Ebl *ebl,
8034 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
8035 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
8036 : : {
8037 : 21 : printf (_("\
8038 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n\n"),
8039 : : elf_ndxscn (scn), section_name (ebl, shdr),
8040 : 21 : (uint64_t) shdr->sh_offset);
8041 : :
8042 : 42 : size_t address_size
8043 [ + + ]: 21 : = elf_getident (ebl->elf, NULL)[EI_CLASS] == ELFCLASS32 ? 4 : 8;
8044 : :
8045 : 21 : Dwarf_Lines *lines;
8046 : 21 : size_t nlines;
8047 : 21 : Dwarf_Off off, next_off = 0;
8048 : 21 : Dwarf_CU *cu = NULL;
8049 [ + + ]: 46 : while (dwarf_next_lines (dbg, off = next_off, &next_off, &cu, NULL, NULL,
8050 : : &lines, &nlines) == 0)
8051 : : {
8052 : 25 : Dwarf_Die cudie;
8053 [ + + + - ]: 25 : if (cu != NULL && dwarf_cu_info (cu, NULL, NULL, &cudie,
8054 : : NULL, NULL, NULL, NULL) == 0)
8055 : 23 : printf (" CU [%" PRIx64 "] %s\n",
8056 : : dwarf_dieoffset (&cudie), dwarf_diename (&cudie));
8057 : : else
8058 : : {
8059 : : /* DWARF5 lines can be independent of any CU, but they probably
8060 : : are used by some CU. Determine the CU this block is for. */
8061 : 2 : Dwarf_Off cuoffset;
8062 : 2 : Dwarf_Off ncuoffset = 0;
8063 : 2 : size_t hsize;
8064 [ + - ]: 3 : while (dwarf_nextcu (dbg, cuoffset = ncuoffset, &ncuoffset, &hsize,
8065 : : NULL, NULL, NULL) == 0)
8066 : : {
8067 [ - + ]: 3 : if (dwarf_offdie (dbg, cuoffset + hsize, &cudie) == NULL)
8068 : 0 : continue;
8069 : 3 : Dwarf_Attribute stmt_list;
8070 [ - + ]: 3 : if (dwarf_attr (&cudie, DW_AT_stmt_list, &stmt_list) == NULL)
8071 : 0 : continue;
8072 : 3 : Dwarf_Word lineoff;
8073 [ - + ]: 3 : if (dwarf_formudata (&stmt_list, &lineoff) != 0)
8074 : 0 : continue;
8075 [ + + ]: 3 : if (lineoff == off)
8076 : : {
8077 : : /* Found the CU. */
8078 : 2 : cu = cudie.cu;
8079 : 2 : break;
8080 : : }
8081 : : }
8082 : :
8083 [ + - ]: 2 : if (cu != NULL)
8084 : 2 : printf (" CU [%" PRIx64 "] %s\n",
8085 : : dwarf_dieoffset (&cudie), dwarf_diename (&cudie));
8086 : : else
8087 : 2 : printf (" No CU\n");
8088 : : }
8089 : :
8090 : 25 : printf (" line:col SBPE* disc isa op address"
8091 : : " (Statement Block Prologue Epilogue *End)\n");
8092 : 25 : const char *last_file = "";
8093 [ + + ]: 255 : for (size_t n = 0; n < nlines; n++)
8094 : : {
8095 : 230 : Dwarf_Line *line = dwarf_onesrcline (lines, n);
8096 [ - + ]: 230 : if (line == NULL)
8097 : : {
8098 : 0 : printf (" dwarf_onesrcline: %s\n", dwarf_errmsg (-1));
8099 : 0 : continue;
8100 : : }
8101 : 230 : Dwarf_Word mtime, length;
8102 : 230 : const char *file = dwarf_linesrc (line, &mtime, &length);
8103 [ - + ]: 230 : if (file == NULL)
8104 : : {
8105 : 0 : printf (" <%s> (mtime: ?, length: ?)\n", dwarf_errmsg (-1));
8106 : 0 : last_file = "";
8107 : : }
8108 [ + + ]: 230 : else if (strcmp (last_file, file) != 0)
8109 : : {
8110 : 36 : printf (" %s (mtime: %" PRIu64 ", length: %" PRIu64 ")\n",
8111 : : file, mtime, length);
8112 : 36 : last_file = file;
8113 : : }
8114 : :
8115 : 230 : int lineno, colno;
8116 : 230 : bool statement, endseq, block, prologue_end, epilogue_begin;
8117 : 230 : unsigned int lineop, isa, disc;
8118 : 230 : Dwarf_Addr address;
8119 : 230 : dwarf_lineaddr (line, &address);
8120 : 230 : dwarf_lineno (line, &lineno);
8121 : 230 : dwarf_linecol (line, &colno);
8122 : 230 : dwarf_lineop_index (line, &lineop);
8123 : 230 : dwarf_linebeginstatement (line, &statement);
8124 : 230 : dwarf_lineendsequence (line, &endseq);
8125 : 230 : dwarf_lineblock (line, &block);
8126 : 230 : dwarf_lineprologueend (line, &prologue_end);
8127 : 230 : dwarf_lineepiloguebegin (line, &epilogue_begin);
8128 : 230 : dwarf_lineisa (line, &isa);
8129 : 230 : dwarf_linediscriminator (line, &disc);
8130 : :
8131 : : /* End sequence is special, it is one byte past. */
8132 : 230 : printf (" %4d:%-3d %c%c%c%c%c %4d %3d %2d ",
8133 : : lineno, colno,
8134 [ + + ]: 230 : (statement ? 'S' : ' '),
8135 [ + - ]: 230 : (block ? 'B' : ' '),
8136 [ + + ]: 230 : (prologue_end ? 'P' : ' '),
8137 [ + - ]: 230 : (epilogue_begin ? 'E' : ' '),
8138 [ + + ]: 230 : (endseq ? '*' : ' '),
8139 : : disc, isa, lineop);
8140 : 230 : print_dwarf_addr (dwflmod, address_size,
8141 [ + + ]: 230 : address - (endseq ? 1 : 0), address);
8142 : 230 : printf ("\n");
8143 : :
8144 [ + + ]: 230 : if (endseq)
8145 : 230 : printf("\n");
8146 : : }
8147 : : }
8148 : 21 : }
8149 : :
8150 : :
8151 : : /* Print the value of a form.
8152 : : Returns new value of readp, or readendp on failure. */
8153 : : static const unsigned char *
8154 : 20 : print_form_data (Dwarf *dbg, int form, const unsigned char *readp,
8155 : : const unsigned char *readendp, unsigned int offset_len,
8156 : : Dwarf_Off str_offsets_base)
8157 : : {
8158 : 20 : Dwarf_Word val;
8159 : 20 : unsigned char *endp;
8160 : 20 : Elf_Data *data;
8161 : 20 : char *str;
8162 [ + - - - : 20 : switch (form)
- - - - -
- - - - +
- - - - -
- - ]
8163 : : {
8164 : 8 : case DW_FORM_data1:
8165 [ - + ]: 8 : if (readendp - readp < 1)
8166 : : {
8167 : 0 : invalid_data:
8168 : 0 : error (0, 0, "invalid data");
8169 : 0 : return readendp;
8170 : : }
8171 : 8 : val = *readp++;
8172 : 8 : printf (" %" PRIx8, (unsigned int) val);
8173 : : break;
8174 : :
8175 : 0 : case DW_FORM_data2:
8176 [ # # ]: 0 : if (readendp - readp < 2)
8177 : 0 : goto invalid_data;
8178 [ # # ]: 0 : val = read_2ubyte_unaligned_inc (dbg, readp);
8179 : 0 : printf(" %" PRIx16, (unsigned int) val);
8180 : : break;
8181 : :
8182 : 0 : case DW_FORM_data4:
8183 [ # # ]: 0 : if (readendp - readp < 4)
8184 : 0 : goto invalid_data;
8185 [ # # ]: 0 : val = read_4ubyte_unaligned_inc (dbg, readp);
8186 : 0 : printf (" %" PRIx32, (unsigned int) val);
8187 : : break;
8188 : :
8189 : 0 : case DW_FORM_data8:
8190 [ # # ]: 0 : if (readendp - readp < 8)
8191 : 0 : goto invalid_data;
8192 [ # # ]: 0 : val = read_8ubyte_unaligned_inc (dbg, readp);
8193 : 0 : printf (" %" PRIx64, val);
8194 : : break;
8195 : :
8196 : 0 : case DW_FORM_sdata:
8197 [ # # ]: 0 : if (readendp - readp < 1)
8198 : 0 : goto invalid_data;
8199 : 0 : get_sleb128 (val, readp, readendp);
8200 : 0 : printf (" %" PRIx64, val);
8201 : : break;
8202 : :
8203 : 0 : case DW_FORM_udata:
8204 [ # # ]: 0 : if (readendp - readp < 1)
8205 : 0 : goto invalid_data;
8206 : 0 : get_uleb128 (val, readp, readendp);
8207 : 0 : printf (" %" PRIx64, val);
8208 : : break;
8209 : :
8210 : 0 : case DW_FORM_block:
8211 [ # # ]: 0 : if (readendp - readp < 1)
8212 : 0 : goto invalid_data;
8213 : 0 : get_uleb128 (val, readp, readendp);
8214 [ # # ]: 0 : if ((size_t) (readendp - readp) < val)
8215 : 0 : goto invalid_data;
8216 : 0 : print_bytes (val, readp);
8217 : 0 : readp += val;
8218 : 0 : break;
8219 : :
8220 : 0 : case DW_FORM_block1:
8221 [ # # ]: 0 : if (readendp - readp < 1)
8222 : 0 : goto invalid_data;
8223 : 0 : val = *readp++;
8224 [ # # ]: 0 : if ((size_t) (readendp - readp) < val)
8225 : 0 : goto invalid_data;
8226 : 0 : print_bytes (val, readp);
8227 : 0 : readp += val;
8228 : 0 : break;
8229 : :
8230 : 0 : case DW_FORM_block2:
8231 [ # # ]: 0 : if (readendp - readp < 2)
8232 : 0 : goto invalid_data;
8233 [ # # ]: 0 : val = read_2ubyte_unaligned_inc (dbg, readp);
8234 [ # # ]: 0 : if ((size_t) (readendp - readp) < val)
8235 : 0 : goto invalid_data;
8236 : 0 : print_bytes (val, readp);
8237 : 0 : readp += val;
8238 : 0 : break;
8239 : :
8240 : 0 : case DW_FORM_block4:
8241 [ # # ]: 0 : if (readendp - readp < 4)
8242 : 0 : goto invalid_data;
8243 [ # # ]: 0 : val = read_4ubyte_unaligned_inc (dbg, readp);
8244 [ # # ]: 0 : if ((size_t) (readendp - readp) < val)
8245 : 0 : goto invalid_data;
8246 : 0 : print_bytes (val, readp);
8247 : 0 : readp += val;
8248 : 0 : break;
8249 : :
8250 : 0 : case DW_FORM_data16:
8251 [ # # ]: 0 : if (readendp - readp < 16)
8252 : 0 : goto invalid_data;
8253 : 0 : print_bytes (16, readp);
8254 : 0 : readp += 16;
8255 : 0 : break;
8256 : :
8257 : 0 : case DW_FORM_flag:
8258 [ # # ]: 0 : if (readendp - readp < 1)
8259 : 0 : goto invalid_data;
8260 : 0 : val = *readp++;
8261 [ # # ]: 0 : printf ("%s", val != 0 ? yes_str : no_str);
8262 : : break;
8263 : :
8264 : 0 : case DW_FORM_string:
8265 : 0 : endp = memchr (readp, '\0', readendp - readp);
8266 [ # # ]: 0 : if (endp == NULL)
8267 : 0 : goto invalid_data;
8268 : 0 : printf ("%s", readp);
8269 : 0 : readp = endp + 1;
8270 : 0 : break;
8271 : :
8272 : 12 : case DW_FORM_strp:
8273 : : case DW_FORM_line_strp:
8274 : : case DW_FORM_strp_sup:
8275 [ - + ]: 12 : if ((size_t) (readendp - readp) < offset_len)
8276 : 0 : goto invalid_data;
8277 [ - + ]: 12 : if (offset_len == 8)
8278 [ # # ]: 0 : val = read_8ubyte_unaligned_inc (dbg, readp);
8279 : : else
8280 [ - + ]: 24 : val = read_4ubyte_unaligned_inc (dbg, readp);
8281 [ - + ]: 12 : if (form == DW_FORM_strp)
8282 : 0 : data = dbg->sectiondata[IDX_debug_str];
8283 [ + - ]: 12 : else if (form == DW_FORM_line_strp)
8284 : 12 : data = dbg->sectiondata[IDX_debug_line_str];
8285 : : else /* form == DW_FORM_strp_sup */
8286 : : {
8287 : 0 : Dwarf *alt = dwarf_getalt (dbg);
8288 [ # # ]: 0 : data = alt != NULL ? alt->sectiondata[IDX_debug_str] : NULL;
8289 : : }
8290 [ + - + - ]: 12 : if (data == NULL || val >= data->d_size
8291 [ + - ]: 12 : || memchr (data->d_buf + val, '\0', data->d_size - val) == NULL)
8292 : : str = "???";
8293 : : else
8294 : 12 : str = (char *) data->d_buf + val;
8295 : 12 : printf ("%s (%" PRIu64 ")", str, val);
8296 : : break;
8297 : :
8298 : 0 : case DW_FORM_sec_offset:
8299 [ # # ]: 0 : if ((size_t) (readendp - readp) < offset_len)
8300 : 0 : goto invalid_data;
8301 [ # # ]: 0 : if (offset_len == 8)
8302 [ # # ]: 0 : val = read_8ubyte_unaligned_inc (dbg, readp);
8303 : : else
8304 [ # # ]: 0 : val = read_4ubyte_unaligned_inc (dbg, readp);
8305 : 0 : printf ("[%" PRIx64 "]", val);
8306 : : break;
8307 : :
8308 : 0 : case DW_FORM_strx:
8309 : : case DW_FORM_GNU_str_index:
8310 [ # # ]: 0 : if (readendp - readp < 1)
8311 : 0 : goto invalid_data;
8312 : 0 : get_uleb128 (val, readp, readendp);
8313 : 0 : strx_val:
8314 : 0 : data = dbg->sectiondata[IDX_debug_str_offsets];
8315 [ # # ]: 0 : if (data == NULL
8316 [ # # ]: 0 : || data->d_size - str_offsets_base < val)
8317 : : str = "???";
8318 : : else
8319 : : {
8320 : 0 : const unsigned char *strreadp = data->d_buf + str_offsets_base + val;
8321 : 0 : const unsigned char *strreadendp = data->d_buf + data->d_size;
8322 [ # # ]: 0 : if ((size_t) (strreadendp - strreadp) < offset_len)
8323 : : str = "???";
8324 : : else
8325 : : {
8326 : 0 : Dwarf_Off idx;
8327 [ # # ]: 0 : if (offset_len == 8)
8328 [ # # ]: 0 : idx = read_8ubyte_unaligned (dbg, strreadp);
8329 : : else
8330 [ # # ]: 0 : idx = read_4ubyte_unaligned (dbg, strreadp);
8331 : :
8332 : 0 : data = dbg->sectiondata[IDX_debug_str];
8333 [ # # # # ]: 0 : if (data == NULL || idx >= data->d_size
8334 [ # # ]: 0 : || memchr (data->d_buf + idx, '\0',
8335 : : data->d_size - idx) == NULL)
8336 : : str = "???";
8337 : : else
8338 : 0 : str = (char *) data->d_buf + idx;
8339 : : }
8340 : : }
8341 : 0 : printf ("%s (%" PRIu64 ")", str, val);
8342 : : break;
8343 : :
8344 : 0 : case DW_FORM_strx1:
8345 [ # # ]: 0 : if (readendp - readp < 1)
8346 : 0 : goto invalid_data;
8347 : 0 : val = *readp++;
8348 : 0 : goto strx_val;
8349 : :
8350 : 0 : case DW_FORM_strx2:
8351 [ # # ]: 0 : if (readendp - readp < 2)
8352 : 0 : goto invalid_data;
8353 [ # # ]: 0 : val = read_2ubyte_unaligned_inc (dbg, readp);
8354 : 0 : goto strx_val;
8355 : :
8356 : 0 : case DW_FORM_strx3:
8357 [ # # ]: 0 : if (readendp - readp < 3)
8358 : 0 : goto invalid_data;
8359 : 0 : val = read_3ubyte_unaligned_inc (dbg, readp);
8360 : 0 : goto strx_val;
8361 : :
8362 : 0 : case DW_FORM_strx4:
8363 [ # # ]: 0 : if (readendp - readp < 4)
8364 : 0 : goto invalid_data;
8365 [ # # ]: 0 : val = read_4ubyte_unaligned_inc (dbg, readp);
8366 : 0 : goto strx_val;
8367 : :
8368 : 0 : default:
8369 : 0 : error (0, 0, _("unknown form: %s"), dwarf_form_name (form));
8370 : 0 : return readendp;
8371 : : }
8372 : :
8373 : 20 : return readp;
8374 : : }
8375 : :
8376 : : static void
8377 : 67 : print_debug_line_section (Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr,
8378 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
8379 : : {
8380 [ + + ]: 67 : if (decodedline)
8381 : : {
8382 : 21 : print_decoded_line_section (dwflmod, ebl, ehdr, scn, shdr, dbg);
8383 : 56 : return;
8384 : : }
8385 : :
8386 : 46 : printf (_("\
8387 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
8388 : : elf_ndxscn (scn), section_name (ebl, shdr),
8389 : 46 : (uint64_t) shdr->sh_offset);
8390 : :
8391 [ + - ]: 46 : if (shdr->sh_size == 0)
8392 : : return;
8393 : :
8394 : : /* There is no functionality in libdw to read the information in the
8395 : : way it is represented here. Hardcode the decoder. */
8396 : 92 : Elf_Data *data = (dbg->sectiondata[IDX_debug_line]
8397 [ - + ]: 46 : ?: elf_rawdata (scn, NULL));
8398 [ - + ]: 46 : if (unlikely (data == NULL))
8399 : : {
8400 : 0 : error (0, 0, _("cannot get line data section data: %s"),
8401 : : elf_errmsg (-1));
8402 : 0 : return;
8403 : : }
8404 : :
8405 : 46 : const unsigned char *linep = (const unsigned char *) data->d_buf;
8406 : 46 : const unsigned char *lineendp;
8407 : :
8408 : 46 : while (linep
8409 [ + + ]: 1674 : < (lineendp = (const unsigned char *) data->d_buf + data->d_size))
8410 : : {
8411 : 1642 : size_t start_offset = linep - (const unsigned char *) data->d_buf;
8412 : :
8413 : 1642 : printf (_("\nTable at offset %zu:\n"), start_offset);
8414 : :
8415 [ - + ]: 1642 : if (unlikely (linep + 4 > lineendp))
8416 : 0 : goto invalid_data;
8417 [ + + ]: 1642 : Dwarf_Word unit_length = read_4ubyte_unaligned_inc (dbg, linep);
8418 : 1642 : unsigned int length = 4;
8419 [ - + ]: 1642 : if (unlikely (unit_length == 0xffffffff))
8420 : : {
8421 [ # # ]: 0 : if (unlikely (linep + 8 > lineendp))
8422 : : {
8423 : 0 : invalid_data:
8424 : 0 : error (0, 0, _("invalid data in section [%zu] '%s'"),
8425 : : elf_ndxscn (scn), section_name (ebl, shdr));
8426 : 0 : return;
8427 : : }
8428 [ # # ]: 0 : unit_length = read_8ubyte_unaligned_inc (dbg, linep);
8429 : 0 : length = 8;
8430 : : }
8431 : :
8432 : : /* Check whether we have enough room in the section. */
8433 [ - + ]: 1642 : if (unlikely (unit_length > (size_t) (lineendp - linep)))
8434 : 0 : goto invalid_data;
8435 : 1642 : lineendp = linep + unit_length;
8436 : :
8437 : : /* The next element of the header is the version identifier. */
8438 [ - + ]: 1642 : if ((size_t) (lineendp - linep) < 2)
8439 : 0 : goto invalid_data;
8440 [ + + ]: 1642 : uint_fast16_t version = read_2ubyte_unaligned_inc (dbg, linep);
8441 : :
8442 : 3284 : size_t address_size
8443 [ + + ]: 1642 : = elf_getident (ebl->elf, NULL)[EI_CLASS] == ELFCLASS32 ? 4 : 8;
8444 : 1642 : unsigned char segment_selector_size = 0;
8445 [ + + ]: 1642 : if (version > 4)
8446 : : {
8447 [ - + ]: 2 : if ((size_t) (lineendp - linep) < 2)
8448 : 0 : goto invalid_data;
8449 : 2 : address_size = *linep++;
8450 : 2 : segment_selector_size = *linep++;
8451 : : }
8452 : :
8453 : : /* Next comes the header length. */
8454 : 1642 : Dwarf_Word header_length;
8455 [ + - ]: 1642 : if (length == 4)
8456 : : {
8457 [ - + ]: 1642 : if ((size_t) (lineendp - linep) < 4)
8458 : 0 : goto invalid_data;
8459 [ + + ]: 3284 : header_length = read_4ubyte_unaligned_inc (dbg, linep);
8460 : : }
8461 : : else
8462 : : {
8463 [ # # ]: 0 : if ((size_t) (lineendp - linep) < 8)
8464 : 0 : goto invalid_data;
8465 [ # # ]: 0 : header_length = read_8ubyte_unaligned_inc (dbg, linep);
8466 : : }
8467 : :
8468 : : /* Next the minimum instruction length. */
8469 [ - + ]: 1642 : if ((size_t) (lineendp - linep) < 1)
8470 : 0 : goto invalid_data;
8471 : 1642 : uint_fast8_t minimum_instr_len = *linep++;
8472 : :
8473 : : /* Next the maximum operations per instruction, in version 4 format. */
8474 : 1642 : uint_fast8_t max_ops_per_instr;
8475 [ + + ]: 1642 : if (version < 4)
8476 : 1632 : max_ops_per_instr = 1;
8477 : : else
8478 : : {
8479 [ - + ]: 10 : if ((size_t) (lineendp - linep) < 1)
8480 : 0 : goto invalid_data;
8481 : 10 : max_ops_per_instr = *linep++;
8482 : : }
8483 : :
8484 : : /* We need at least 4 more bytes. */
8485 [ - + ]: 1642 : if ((size_t) (lineendp - linep) < 4)
8486 : 0 : goto invalid_data;
8487 : :
8488 : : /* Then the flag determining the default value of the is_stmt
8489 : : register. */
8490 : 1642 : uint_fast8_t default_is_stmt = *linep++;
8491 : :
8492 : : /* Now the line base. */
8493 : 1642 : int_fast8_t line_base = *linep++;
8494 : :
8495 : : /* And the line range. */
8496 : 1642 : uint_fast8_t line_range = *linep++;
8497 : :
8498 : : /* The opcode base. */
8499 : 1642 : uint_fast8_t opcode_base = *linep++;
8500 : :
8501 : : /* Print what we got so far. */
8502 : 1642 : printf (_("\n"
8503 : : " Length: %" PRIu64 "\n"
8504 : : " DWARF version: %" PRIuFAST16 "\n"
8505 : : " Prologue length: %" PRIu64 "\n"
8506 : : " Address size: %zd\n"
8507 : : " Segment selector size: %zd\n"
8508 : : " Min instruction length: %" PRIuFAST8 "\n"
8509 : : " Max operations per instruction: %" PRIuFAST8 "\n"
8510 : : " Initial value if 'is_stmt': %" PRIuFAST8 "\n"
8511 : : " Line base: %" PRIdFAST8 "\n"
8512 : : " Line range: %" PRIuFAST8 "\n"
8513 : : " Opcode base: %" PRIuFAST8 "\n"
8514 : : "\n"
8515 : : "Opcodes:\n"),
8516 : : (uint64_t) unit_length, version, (uint64_t) header_length,
8517 : : address_size, (size_t) segment_selector_size,
8518 : : minimum_instr_len, max_ops_per_instr,
8519 : : default_is_stmt, line_base,
8520 : : line_range, opcode_base);
8521 : :
8522 [ - + ]: 1642 : if (version < 2 || version > 5)
8523 : : {
8524 : 0 : error (0, 0, _("cannot handle .debug_line version: %u\n"),
8525 : : (unsigned int) version);
8526 : 0 : linep = lineendp;
8527 : 0 : continue;
8528 : : }
8529 : :
8530 [ - + ]: 1642 : if (address_size != 4 && address_size != 8)
8531 : : {
8532 : 0 : error (0, 0, _("cannot handle address size: %u\n"),
8533 : : (unsigned int) address_size);
8534 : 0 : linep = lineendp;
8535 : 0 : continue;
8536 : : }
8537 : :
8538 [ - + ]: 1642 : if (segment_selector_size != 0)
8539 : : {
8540 : 0 : error (0, 0, _("cannot handle segment selector size: %u\n"),
8541 : : (unsigned int) segment_selector_size);
8542 : 0 : linep = lineendp;
8543 : 0 : continue;
8544 : : }
8545 : :
8546 [ - + ]: 1642 : if (unlikely (linep + opcode_base - 1 >= lineendp))
8547 : : {
8548 : 0 : invalid_unit:
8549 : 0 : error (0, 0,
8550 : 0 : _("invalid data at offset %tu in section [%zu] '%s'"),
8551 : 0 : linep - (const unsigned char *) data->d_buf,
8552 : : elf_ndxscn (scn), section_name (ebl, shdr));
8553 : 0 : linep = lineendp;
8554 : 0 : continue;
8555 : : }
8556 : 1642 : int opcode_base_l10 = 1;
8557 : 1642 : unsigned int tmp = opcode_base;
8558 [ + + ]: 3282 : while (tmp > 10)
8559 : : {
8560 : 1640 : tmp /= 10;
8561 : 1640 : ++opcode_base_l10;
8562 : : }
8563 : 1642 : const uint8_t *standard_opcode_lengths = linep - 1;
8564 [ + + ]: 21340 : for (uint_fast8_t cnt = 1; cnt < opcode_base; ++cnt)
8565 : 39396 : printf (ngettext (" [%*" PRIuFAST8 "] %hhu argument\n",
8566 : : " [%*" PRIuFAST8 "] %hhu arguments\n",
8567 : : (int) linep[cnt - 1]),
8568 : 19698 : opcode_base_l10, cnt, linep[cnt - 1]);
8569 : 1642 : linep += opcode_base - 1;
8570 : :
8571 [ - + ]: 1642 : if (unlikely (linep >= lineendp))
8572 : 0 : goto invalid_unit;
8573 : :
8574 : 1642 : Dwarf_Off str_offsets_base = str_offsets_base_off (dbg, NULL);
8575 : :
8576 : 1642 : puts (_("\nDirectory table:"));
8577 [ + + ]: 1642 : if (version > 4)
8578 : : {
8579 : 2 : struct encpair { uint16_t desc; uint16_t form; };
8580 : 2 : struct encpair enc[256];
8581 : :
8582 : 2 : printf (_(" ["));
8583 [ - + ]: 2 : if ((size_t) (lineendp - linep) < 1)
8584 : 0 : goto invalid_data;
8585 : 2 : unsigned char directory_entry_format_count = *linep++;
8586 : 2 : for (int i = 0; i < directory_entry_format_count; i++)
8587 : : {
8588 : 2 : uint16_t desc, form;
8589 [ - + ]: 2 : if ((size_t) (lineendp - linep) < 1)
8590 : 0 : goto invalid_data;
8591 : 2 : get_uleb128 (desc, linep, lineendp);
8592 [ - + ]: 2 : if ((size_t) (lineendp - linep) < 1)
8593 : 0 : goto invalid_data;
8594 : 2 : get_uleb128 (form, linep, lineendp);
8595 : :
8596 : 2 : enc[i].desc = desc;
8597 : 2 : enc[i].form = form;
8598 : :
8599 : 2 : printf ("%s(%s)",
8600 : : dwarf_line_content_description_name (desc),
8601 : : dwarf_form_name (form));
8602 [ - + ]: 2 : if (i + 1 < directory_entry_format_count)
8603 [ + + ]: 4 : printf (", ");
8604 : : }
8605 : 2 : printf ("]\n");
8606 : :
8607 : 2 : uint64_t directories_count;
8608 [ - + ]: 2 : if ((size_t) (lineendp - linep) < 1)
8609 : 0 : goto invalid_data;
8610 : 2 : get_uleb128 (directories_count, linep, lineendp);
8611 : :
8612 : 2 : if (directory_entry_format_count == 0
8613 [ - + ]: 2 : && directories_count != 0)
8614 : 0 : goto invalid_data;
8615 : :
8616 [ + + ]: 6 : for (uint64_t i = 0; i < directories_count; i++)
8617 : : {
8618 : 4 : printf (" %-5" PRIu64 " ", i);
8619 : 4 : for (int j = 0; j < directory_entry_format_count; j++)
8620 : : {
8621 : 4 : linep = print_form_data (dbg, enc[j].form,
8622 : : linep, lineendp, length,
8623 : : str_offsets_base);
8624 [ - + ]: 4 : if (j + 1 < directory_entry_format_count)
8625 [ + + ]: 8 : printf (", ");
8626 : : }
8627 : 4 : printf ("\n");
8628 [ - + ]: 4 : if (linep >= lineendp)
8629 : 0 : goto invalid_unit;
8630 : : }
8631 : : }
8632 : : else
8633 : : {
8634 [ + - + + ]: 12257 : while (linep < lineendp && *linep != 0)
8635 : : {
8636 : 10617 : unsigned char *endp = memchr (linep, '\0', lineendp - linep);
8637 [ - + ]: 10617 : if (unlikely (endp == NULL))
8638 : 0 : goto invalid_unit;
8639 : :
8640 : 10617 : printf (" %s\n", (char *) linep);
8641 : :
8642 : 10617 : linep = endp + 1;
8643 : : }
8644 [ + - - + ]: 1640 : if (linep >= lineendp || *linep != 0)
8645 : 0 : goto invalid_unit;
8646 : : /* Skip the final NUL byte. */
8647 : 1640 : ++linep;
8648 : : }
8649 : :
8650 [ - + ]: 1642 : if (unlikely (linep >= lineendp))
8651 : 0 : goto invalid_unit;
8652 : :
8653 : 1642 : puts (_("\nFile name table:"));
8654 [ + + ]: 1642 : if (version > 4)
8655 : : {
8656 : 2 : struct encpair { uint16_t desc; uint16_t form; };
8657 : 2 : struct encpair enc[256];
8658 : :
8659 : 2 : printf (_(" ["));
8660 [ - + ]: 2 : if ((size_t) (lineendp - linep) < 1)
8661 : 0 : goto invalid_data;
8662 : 2 : unsigned char file_name_format_count = *linep++;
8663 : 2 : for (int i = 0; i < file_name_format_count; i++)
8664 : : {
8665 : 4 : uint64_t desc, form;
8666 [ - + ]: 4 : if ((size_t) (lineendp - linep) < 1)
8667 : 0 : goto invalid_data;
8668 : 4 : get_uleb128 (desc, linep, lineendp);
8669 [ - + ]: 4 : if ((size_t) (lineendp - linep) < 1)
8670 : 0 : goto invalid_data;
8671 : 4 : get_uleb128 (form, linep, lineendp);
8672 : :
8673 [ - + ]: 4 : if (! libdw_valid_user_form (form))
8674 : 0 : goto invalid_data;
8675 : :
8676 : 4 : enc[i].desc = desc;
8677 : 4 : enc[i].form = form;
8678 : :
8679 : 4 : printf ("%s(%s)",
8680 : : dwarf_line_content_description_name (desc),
8681 : : dwarf_form_name (form));
8682 [ + + ]: 4 : if (i + 1 < file_name_format_count)
8683 [ + + ]: 8 : printf (", ");
8684 : : }
8685 : 2 : printf ("]\n");
8686 : :
8687 : 2 : uint64_t file_name_count;
8688 [ - + ]: 2 : if ((size_t) (lineendp - linep) < 1)
8689 : 0 : goto invalid_data;
8690 : 2 : get_uleb128 (file_name_count, linep, lineendp);
8691 : :
8692 : 2 : if (file_name_format_count == 0
8693 [ - + ]: 2 : && file_name_count != 0)
8694 : 0 : goto invalid_data;
8695 : :
8696 [ + + ]: 10 : for (uint64_t i = 0; i < file_name_count; i++)
8697 : : {
8698 : 8 : printf (" %-5" PRIu64 " ", i);
8699 : 8 : for (int j = 0; j < file_name_format_count; j++)
8700 : : {
8701 : 16 : linep = print_form_data (dbg, enc[j].form,
8702 : : linep, lineendp, length,
8703 : : str_offsets_base);
8704 [ + + ]: 16 : if (j + 1 < file_name_format_count)
8705 [ + + ]: 32 : printf (", ");
8706 : : }
8707 : 8 : printf ("\n");
8708 [ - + ]: 8 : if (linep > lineendp)
8709 : 0 : goto invalid_unit;
8710 : : }
8711 : : }
8712 : : else
8713 : : {
8714 : 1640 : puts (_(" Entry Dir Time Size Name"));
8715 [ + - + + ]: 27936 : for (unsigned int cnt = 1; linep < lineendp && *linep != 0; ++cnt)
8716 : : {
8717 : : /* First comes the file name. */
8718 : 26296 : char *fname = (char *) linep;
8719 : 26296 : unsigned char *endp = memchr (fname, '\0', lineendp - linep);
8720 [ - + ]: 26296 : if (unlikely (endp == NULL))
8721 : 0 : goto invalid_unit;
8722 : 26296 : linep = endp + 1;
8723 : :
8724 : : /* Then the index. */
8725 : 26296 : unsigned int diridx;
8726 [ - + ]: 26296 : if (lineendp - linep < 1)
8727 : 0 : goto invalid_unit;
8728 : 26296 : get_uleb128 (diridx, linep, lineendp);
8729 : :
8730 : : /* Next comes the modification time. */
8731 : 26296 : unsigned int mtime;
8732 [ - + ]: 26296 : if (lineendp - linep < 1)
8733 : 0 : goto invalid_unit;
8734 : 26296 : get_uleb128 (mtime, linep, lineendp);
8735 : :
8736 : : /* Finally the length of the file. */
8737 : 26296 : unsigned int fsize;
8738 [ - + ]: 26296 : if (lineendp - linep < 1)
8739 : 0 : goto invalid_unit;
8740 : 26296 : get_uleb128 (fsize, linep, lineendp);
8741 : :
8742 : 26296 : printf (" %-5u %-5u %-9u %-9u %s\n",
8743 : : cnt, diridx, mtime, fsize, fname);
8744 : : }
8745 [ + - - + ]: 1640 : if (linep >= lineendp || *linep != '\0')
8746 : 0 : goto invalid_unit;
8747 : : /* Skip the final NUL byte. */
8748 : 1640 : ++linep;
8749 : : }
8750 : :
8751 [ + + ]: 1642 : if (linep == lineendp)
8752 : : {
8753 : 14 : puts (_("\nNo line number statements."));
8754 : 14 : return;
8755 : : }
8756 : :
8757 : 1628 : puts (_("\nLine number statements:"));
8758 : 1628 : Dwarf_Word address = 0;
8759 : 1628 : unsigned int op_index = 0;
8760 : 1628 : size_t line = 1;
8761 : 1628 : uint_fast8_t is_stmt = default_is_stmt;
8762 : :
8763 : : /* Apply the "operation advance" from a special opcode
8764 : : or DW_LNS_advance_pc (as per DWARF4 6.2.5.1). */
8765 : 1628 : unsigned int op_addr_advance;
8766 : 376735 : inline void advance_pc (unsigned int op_advance)
8767 : : {
8768 : 375107 : op_addr_advance = minimum_instr_len * ((op_index + op_advance)
8769 : 375107 : / max_ops_per_instr);
8770 : 375107 : address += op_addr_advance;
8771 : 375107 : op_index = (op_index + op_advance) % max_ops_per_instr;
8772 : 375107 : }
8773 : :
8774 [ - + ]: 1628 : if (max_ops_per_instr == 0)
8775 : : {
8776 : 0 : error (0, 0,
8777 : 0 : _("invalid maximum operations per instruction is zero"));
8778 : 0 : linep = lineendp;
8779 : 0 : continue;
8780 : : }
8781 : :
8782 : 1110536 : while (linep < lineendp)
8783 : : {
8784 : 1108908 : size_t offset = linep - (const unsigned char *) data->d_buf;
8785 : 1108908 : unsigned int u128;
8786 : 1108908 : int s128;
8787 : :
8788 : : /* Read the opcode. */
8789 : 1108908 : unsigned int opcode = *linep++;
8790 : :
8791 : 1108908 : printf (" [%6" PRIx64 "]", (uint64_t)offset);
8792 : : /* Is this a special opcode? */
8793 [ + + ]: 1108908 : if (likely (opcode >= opcode_base))
8794 : : {
8795 [ - + ]: 373292 : if (unlikely (line_range == 0))
8796 : 0 : goto invalid_unit;
8797 : :
8798 : : /* Yes. Handling this is quite easy since the opcode value
8799 : : is computed with
8800 : :
8801 : : opcode = (desired line increment - line_base)
8802 : : + (line_range * address advance) + opcode_base
8803 : : */
8804 : 373292 : int line_increment = (line_base
8805 : 373292 : + (opcode - opcode_base) % line_range);
8806 : :
8807 : : /* Perform the increments. */
8808 : 373292 : line += line_increment;
8809 : 373292 : advance_pc ((opcode - opcode_base) / line_range);
8810 : :
8811 : 373292 : printf (_(" special opcode %u: address+%u = "),
8812 : : opcode, op_addr_advance);
8813 : 373292 : print_dwarf_addr (dwflmod, 0, address, address);
8814 [ - + ]: 373292 : if (op_index > 0)
8815 : 0 : printf (_(", op_index = %u, line%+d = %zu\n"),
8816 : : op_index, line_increment, line);
8817 : : else
8818 : 373292 : printf (_(", line%+d = %zu\n"),
8819 : : line_increment, line);
8820 : : }
8821 [ + + ]: 735616 : else if (opcode == 0)
8822 : : {
8823 : : /* This an extended opcode. */
8824 [ - + ]: 53811 : if (unlikely (linep + 2 > lineendp))
8825 : 0 : goto invalid_unit;
8826 : :
8827 : : /* The length. */
8828 : 53811 : unsigned int len = *linep++;
8829 : :
8830 [ - + ]: 53811 : if (unlikely (linep + len > lineendp))
8831 : 0 : goto invalid_unit;
8832 : :
8833 : : /* The sub-opcode. */
8834 : 53811 : opcode = *linep++;
8835 : :
8836 : 53811 : printf (_(" extended opcode %u: "), opcode);
8837 : :
8838 [ + + - + : 53811 : switch (opcode)
- ]
8839 : : {
8840 : 1655 : case DW_LNE_end_sequence:
8841 : 1655 : puts (_(" end of sequence"));
8842 : :
8843 : : /* Reset the registers we care about. */
8844 : 1655 : address = 0;
8845 : 1655 : op_index = 0;
8846 : 1655 : line = 1;
8847 : 1655 : is_stmt = default_is_stmt;
8848 : 1655 : break;
8849 : :
8850 : 1905 : case DW_LNE_set_address:
8851 : 1905 : op_index = 0;
8852 [ - + ]: 1905 : if (unlikely ((size_t) (lineendp - linep) < address_size))
8853 : 0 : goto invalid_unit;
8854 [ + + ]: 1905 : if (address_size == 4)
8855 [ + + ]: 34 : address = read_4ubyte_unaligned_inc (dbg, linep);
8856 : : else
8857 [ + + ]: 3776 : address = read_8ubyte_unaligned_inc (dbg, linep);
8858 : : {
8859 : 1905 : printf (_(" set address to "));
8860 : 1905 : print_dwarf_addr (dwflmod, 0, address, address);
8861 : 1905 : printf ("\n");
8862 : : }
8863 : : break;
8864 : :
8865 : 0 : case DW_LNE_define_file:
8866 : : {
8867 : 0 : char *fname = (char *) linep;
8868 : 0 : unsigned char *endp = memchr (linep, '\0',
8869 : 0 : lineendp - linep);
8870 [ # # ]: 0 : if (unlikely (endp == NULL))
8871 : 0 : goto invalid_unit;
8872 : 0 : linep = endp + 1;
8873 : :
8874 : 0 : unsigned int diridx;
8875 [ # # ]: 0 : if (lineendp - linep < 1)
8876 : 0 : goto invalid_unit;
8877 : 0 : get_uleb128 (diridx, linep, lineendp);
8878 : 0 : Dwarf_Word mtime;
8879 [ # # ]: 0 : if (lineendp - linep < 1)
8880 : 0 : goto invalid_unit;
8881 : 0 : get_uleb128 (mtime, linep, lineendp);
8882 : 0 : Dwarf_Word filelength;
8883 [ # # ]: 0 : if (lineendp - linep < 1)
8884 : 0 : goto invalid_unit;
8885 : 0 : get_uleb128 (filelength, linep, lineendp);
8886 : :
8887 : 0 : printf (_("\
8888 : : define new file: dir=%u, mtime=%" PRIu64 ", length=%" PRIu64 ", name=%s\n"),
8889 : : diridx, (uint64_t) mtime, (uint64_t) filelength,
8890 : : fname);
8891 : : }
8892 : : break;
8893 : :
8894 : 50251 : case DW_LNE_set_discriminator:
8895 : : /* Takes one ULEB128 parameter, the discriminator. */
8896 [ + - - + ]: 50251 : if (unlikely (standard_opcode_lengths[opcode] != 1
8897 : : || lineendp - linep < 1))
8898 : 0 : goto invalid_unit;
8899 : :
8900 : 50251 : get_uleb128 (u128, linep, lineendp);
8901 : 50251 : printf (_(" set discriminator to %u\n"), u128);
8902 : : break;
8903 : :
8904 : 0 : default:
8905 : : /* Unknown, ignore it. */
8906 : 0 : puts (_(" unknown opcode"));
8907 : 0 : linep += len - 1;
8908 : 0 : break;
8909 : : }
8910 : : }
8911 [ + - ]: 681805 : else if (opcode <= DW_LNS_set_isa)
8912 : : {
8913 : : /* This is a known standard opcode. */
8914 [ + + + + : 681805 : switch (opcode)
+ + - + +
+ - - ]
8915 : : {
8916 : 100706 : case DW_LNS_copy:
8917 : : /* Takes no argument. */
8918 : 100706 : puts (_(" copy"));
8919 : 100706 : break;
8920 : :
8921 : 1587 : case DW_LNS_advance_pc:
8922 : : /* Takes one uleb128 parameter which is added to the
8923 : : address. */
8924 [ - + ]: 1587 : if (lineendp - linep < 1)
8925 : 0 : goto invalid_unit;
8926 : 1587 : get_uleb128 (u128, linep, lineendp);
8927 : 1587 : advance_pc (u128);
8928 : : {
8929 : 1587 : printf (_(" advance address by %u to "),
8930 : : op_addr_advance);
8931 : 1587 : print_dwarf_addr (dwflmod, 0, address, address);
8932 [ - + ]: 1587 : if (op_index > 0)
8933 : 0 : printf (_(", op_index to %u"), op_index);
8934 : 1587 : printf ("\n");
8935 : : }
8936 : : break;
8937 : :
8938 : 87611 : case DW_LNS_advance_line:
8939 : : /* Takes one sleb128 parameter which is added to the
8940 : : line. */
8941 [ - + ]: 87611 : if (lineendp - linep < 1)
8942 : 0 : goto invalid_unit;
8943 : 87611 : get_sleb128 (s128, linep, lineendp);
8944 : 87611 : line += s128;
8945 : 87611 : printf (_("\
8946 : : advance line by constant %d to %" PRId64 "\n"),
8947 : : s128, (int64_t) line);
8948 : : break;
8949 : :
8950 : 30856 : case DW_LNS_set_file:
8951 : : /* Takes one uleb128 parameter which is stored in file. */
8952 [ - + ]: 30856 : if (lineendp - linep < 1)
8953 : 0 : goto invalid_unit;
8954 : 30856 : get_uleb128 (u128, linep, lineendp);
8955 : 30856 : printf (_(" set file to %" PRIu64 "\n"),
8956 : : (uint64_t) u128);
8957 : : break;
8958 : :
8959 : 283031 : case DW_LNS_set_column:
8960 : : /* Takes one uleb128 parameter which is stored in column. */
8961 [ + - - + ]: 283031 : if (unlikely (standard_opcode_lengths[opcode] != 1
8962 : : || lineendp - linep < 1))
8963 : 0 : goto invalid_unit;
8964 : :
8965 : 283031 : get_uleb128 (u128, linep, lineendp);
8966 : 283031 : printf (_(" set column to %" PRIu64 "\n"),
8967 : : (uint64_t) u128);
8968 : : break;
8969 : :
8970 : 177763 : case DW_LNS_negate_stmt:
8971 : : /* Takes no argument. */
8972 : 177763 : is_stmt = 1 - is_stmt;
8973 : 177763 : printf (_(" set '%s' to %" PRIuFAST8 "\n"),
8974 : : "is_stmt", is_stmt);
8975 : : break;
8976 : :
8977 : 0 : case DW_LNS_set_basic_block:
8978 : : /* Takes no argument. */
8979 : 0 : puts (_(" set basic block flag"));
8980 : 0 : break;
8981 : :
8982 : 228 : case DW_LNS_const_add_pc:
8983 : : /* Takes no argument. */
8984 : :
8985 [ - + ]: 228 : if (unlikely (line_range == 0))
8986 : 0 : goto invalid_unit;
8987 : :
8988 : 228 : advance_pc ((255 - opcode_base) / line_range);
8989 : : {
8990 : 228 : printf (_(" advance address by constant %u to "),
8991 : : op_addr_advance);
8992 : 228 : print_dwarf_addr (dwflmod, 0, address, address);
8993 [ - + ]: 228 : if (op_index > 0)
8994 : 0 : printf (_(", op_index to %u"), op_index);
8995 : 228 : printf ("\n");
8996 : : }
8997 : : break;
8998 : :
8999 : 22 : case DW_LNS_fixed_advance_pc:
9000 : : /* Takes one 16 bit parameter which is added to the
9001 : : address. */
9002 [ + - - + ]: 22 : if (unlikely (standard_opcode_lengths[opcode] != 1
9003 : : || lineendp - linep < 2))
9004 : 0 : goto invalid_unit;
9005 : :
9006 [ - + ]: 22 : u128 = read_2ubyte_unaligned_inc (dbg, linep);
9007 : 22 : address += u128;
9008 : 22 : op_index = 0;
9009 : : {
9010 : 22 : printf (_("\
9011 : : advance address by fixed value %u to \n"),
9012 : : u128);
9013 : 22 : print_dwarf_addr (dwflmod, 0, address, address);
9014 : 22 : printf ("\n");
9015 : : }
9016 : : break;
9017 : :
9018 : 1 : case DW_LNS_set_prologue_end:
9019 : : /* Takes no argument. */
9020 : 1 : puts (_(" set prologue end flag"));
9021 : 1 : break;
9022 : :
9023 : 0 : case DW_LNS_set_epilogue_begin:
9024 : : /* Takes no argument. */
9025 : 0 : puts (_(" set epilogue begin flag"));
9026 : 0 : break;
9027 : :
9028 : 0 : case DW_LNS_set_isa:
9029 : : /* Takes one uleb128 parameter which is stored in isa. */
9030 [ # # # # ]: 0 : if (unlikely (standard_opcode_lengths[opcode] != 1
9031 : : || lineendp - linep < 1))
9032 : 0 : goto invalid_unit;
9033 : :
9034 : 0 : get_uleb128 (u128, linep, lineendp);
9035 [ + + ]: 1110536 : printf (_(" set isa to %u\n"), u128);
9036 : : break;
9037 : : }
9038 : : }
9039 : : else
9040 : : {
9041 : : /* This is a new opcode the generator but not we know about.
9042 : : Read the parameters associated with it but then discard
9043 : : everything. Read all the parameters for this opcode. */
9044 : 0 : printf (ngettext (" unknown opcode with %" PRIu8 " parameter:",
9045 : : " unknown opcode with %" PRIu8 " parameters:",
9046 : : standard_opcode_lengths[opcode]),
9047 : 0 : standard_opcode_lengths[opcode]);
9048 : 0 : for (int n = standard_opcode_lengths[opcode];
9049 [ # # # # ]: 0 : n > 0 && linep < lineendp; --n)
9050 : : {
9051 : 0 : get_uleb128 (u128, linep, lineendp);
9052 [ # # ]: 0 : if (n != standard_opcode_lengths[opcode])
9053 [ # # ]: 0 : putc_unlocked (',', stdout);
9054 : 0 : printf (" %u", u128);
9055 : : }
9056 : :
9057 : : /* Next round, ignore this opcode. */
9058 : 0 : continue;
9059 : : }
9060 : : }
9061 : : }
9062 : :
9063 : : /* There must only be one data block. */
9064 [ - + ]: 32 : assert (elf_getdata (scn, data) == NULL);
9065 : : }
9066 : :
9067 : :
9068 : : static void
9069 : 3 : print_debug_loclists_section (Dwfl_Module *dwflmod,
9070 : : Ebl *ebl,
9071 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
9072 : : Elf_Scn *scn, GElf_Shdr *shdr,
9073 : : Dwarf *dbg)
9074 : : {
9075 : 3 : printf (_("\
9076 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
9077 : : elf_ndxscn (scn), section_name (ebl, shdr),
9078 : 3 : (uint64_t) shdr->sh_offset);
9079 : :
9080 : 6 : Elf_Data *data = (dbg->sectiondata[IDX_debug_loclists]
9081 [ - + ]: 3 : ?: elf_rawdata (scn, NULL));
9082 [ - + ]: 3 : if (unlikely (data == NULL))
9083 : : {
9084 : 0 : error (0, 0, _("cannot get .debug_loclists content: %s"),
9085 : : elf_errmsg (-1));
9086 : 0 : return;
9087 : : }
9088 : :
9089 : : /* For the listptr to get the base address/CU. */
9090 : 3 : sort_listptr (&known_loclistsptr, "loclistsptr");
9091 : 3 : size_t listptr_idx = 0;
9092 : :
9093 : 3 : const unsigned char *readp = data->d_buf;
9094 : 3 : const unsigned char *const dataend = ((unsigned char *) data->d_buf
9095 : 3 : + data->d_size);
9096 [ + + ]: 7 : while (readp < dataend)
9097 : : {
9098 [ - + ]: 4 : if (unlikely (readp > dataend - 4))
9099 : : {
9100 : 0 : invalid_data:
9101 : 0 : error (0, 0, _("invalid data in section [%zu] '%s'"),
9102 : : elf_ndxscn (scn), section_name (ebl, shdr));
9103 : 0 : return;
9104 : : }
9105 : :
9106 : 4 : ptrdiff_t offset = readp - (unsigned char *) data->d_buf;
9107 : 4 : printf (_("Table at Offset 0x%" PRIx64 ":\n\n"),
9108 : : (uint64_t) offset);
9109 : :
9110 [ - + ]: 4 : uint64_t unit_length = read_4ubyte_unaligned_inc (dbg, readp);
9111 : 4 : unsigned int offset_size = 4;
9112 [ - + ]: 4 : if (unlikely (unit_length == 0xffffffff))
9113 : : {
9114 [ # # ]: 0 : if (unlikely (readp > dataend - 8))
9115 : 0 : goto invalid_data;
9116 : :
9117 [ # # ]: 0 : unit_length = read_8ubyte_unaligned_inc (dbg, readp);
9118 : 0 : offset_size = 8;
9119 : : }
9120 : 4 : printf (_(" Length: %8" PRIu64 "\n"), unit_length);
9121 : :
9122 : : /* We need at least 2-bytes + 1-byte + 1-byte + 4-bytes = 8
9123 : : bytes to complete the header. And this unit cannot go beyond
9124 : : the section data. */
9125 [ + - ]: 4 : if (readp > dataend - 8
9126 [ + - ]: 4 : || unit_length < 8
9127 [ - + ]: 4 : || unit_length > (uint64_t) (dataend - readp))
9128 : 0 : goto invalid_data;
9129 : :
9130 : 4 : const unsigned char *nexthdr = readp + unit_length;
9131 : :
9132 [ - + ]: 4 : uint16_t version = read_2ubyte_unaligned_inc (dbg, readp);
9133 : 4 : printf (_(" DWARF version: %8" PRIu16 "\n"), version);
9134 : :
9135 [ - + ]: 4 : if (version != 5)
9136 : : {
9137 : 0 : error (0, 0, _("Unknown version"));
9138 : 0 : goto next_table;
9139 : : }
9140 : :
9141 : 4 : uint8_t address_size = *readp++;
9142 : 4 : printf (_(" Address size: %8" PRIu64 "\n"),
9143 : : (uint64_t) address_size);
9144 : :
9145 [ - + ]: 4 : if (address_size != 4 && address_size != 8)
9146 : : {
9147 : 0 : error (0, 0, _("unsupported address size"));
9148 : 0 : goto next_table;
9149 : : }
9150 : :
9151 : 4 : uint8_t segment_size = *readp++;
9152 : 4 : printf (_(" Segment size: %8" PRIu64 "\n"),
9153 : : (uint64_t) segment_size);
9154 : :
9155 [ - + ]: 4 : if (segment_size != 0)
9156 : : {
9157 : 0 : error (0, 0, _("unsupported segment size"));
9158 : 0 : goto next_table;
9159 : : }
9160 : :
9161 [ - + ]: 4 : uint32_t offset_entry_count = read_4ubyte_unaligned_inc (dbg, readp);
9162 : 4 : printf (_(" Offset entries: %8" PRIu64 "\n"),
9163 : : (uint64_t) offset_entry_count);
9164 : :
9165 : : /* We need the CU that uses this unit to get the initial base address. */
9166 : 4 : Dwarf_Addr cu_base = 0;
9167 : 4 : struct Dwarf_CU *cu = NULL;
9168 [ - + ]: 4 : if (listptr_cu (&known_loclistsptr, &listptr_idx,
9169 : : (Dwarf_Off) offset,
9170 : 4 : (Dwarf_Off) (nexthdr - (unsigned char *) data->d_buf),
9171 : : &cu_base, &cu)
9172 [ # # ]: 0 : || split_dwarf_cu_base (dbg, &cu, &cu_base))
9173 : 4 : {
9174 : 4 : Dwarf_Die cudie;
9175 [ - + ]: 4 : if (dwarf_cu_die (cu, &cudie,
9176 : : NULL, NULL, NULL, NULL,
9177 : : NULL, NULL) == NULL)
9178 : 0 : printf (_(" Unknown CU base: "));
9179 : : else
9180 : 4 : printf (_(" CU [%6" PRIx64 "] base: "),
9181 : : dwarf_dieoffset (&cudie));
9182 : 4 : print_dwarf_addr (dwflmod, address_size, cu_base, cu_base);
9183 : 4 : printf ("\n");
9184 : : }
9185 : : else
9186 : 0 : printf (_(" Not associated with a CU.\n"));
9187 : :
9188 : 4 : printf ("\n");
9189 : :
9190 : 4 : const unsigned char *offset_array_start = readp;
9191 [ + + ]: 4 : if (offset_entry_count > 0)
9192 : : {
9193 : 2 : uint64_t max_entries = (unit_length - 8) / offset_size;
9194 [ - + ]: 2 : if (offset_entry_count > max_entries)
9195 : : {
9196 : 0 : error (0, 0,
9197 : 0 : _("too many offset entries for unit length"));
9198 : 0 : offset_entry_count = max_entries;
9199 : : }
9200 : :
9201 : 2 : printf (_(" Offsets starting at 0x%" PRIx64 ":\n"),
9202 : : (uint64_t) (offset_array_start
9203 : 2 : - (unsigned char *) data->d_buf));
9204 [ + + ]: 18 : for (uint32_t idx = 0; idx < offset_entry_count; idx++)
9205 : : {
9206 : 16 : printf (" [%6" PRIu32 "] ", idx);
9207 [ + - ]: 16 : if (offset_size == 4)
9208 : : {
9209 [ - + ]: 16 : uint32_t off = read_4ubyte_unaligned_inc (dbg, readp);
9210 : 16 : printf ("0x%" PRIx32 "\n", off);
9211 : : }
9212 : : else
9213 : : {
9214 [ # # ]: 0 : uint64_t off = read_8ubyte_unaligned_inc (dbg, readp);
9215 : 16 : printf ("0x%" PRIx64 "\n", off);
9216 : : }
9217 : : }
9218 : 2 : printf ("\n");
9219 : : }
9220 : :
9221 : 4 : Dwarf_Addr base = cu_base;
9222 : 4 : bool start_of_list = true;
9223 : 4 : while (readp < nexthdr)
9224 : : {
9225 : 89 : Dwarf_Off off = (Dwarf_Off) (readp - (unsigned char *) data->d_buf);
9226 [ - + ]: 89 : if (listptr_attr (&known_loclistsptr, listptr_idx, off,
9227 : : DW_AT_GNU_locviews))
9228 : : {
9229 : 0 : Dwarf_Off next_off = next_listptr_offset (&known_loclistsptr,
9230 : : &listptr_idx, off);
9231 : 0 : const unsigned char *locp = readp;
9232 : 0 : const unsigned char *locendp;
9233 [ # # ]: 0 : if (next_off == 0
9234 : 0 : || next_off > (size_t) (nexthdr - ((const unsigned char *)
9235 [ # # ]: 0 : data->d_buf)))
9236 : : locendp = nexthdr;
9237 : : else
9238 : 0 : locendp = (const unsigned char *) data->d_buf + next_off;
9239 : :
9240 : 0 : printf (" Offset: %" PRIx64 ", Index: %" PRIx64 "\n",
9241 : 0 : (uint64_t) (readp - (unsigned char *) data->d_buf),
9242 : 0 : (uint64_t) (readp - offset_array_start));
9243 : :
9244 : 0 : while (locp < locendp)
9245 : : {
9246 : 0 : uint64_t v1, v2;
9247 : 0 : get_uleb128 (v1, locp, locendp);
9248 [ # # ]: 0 : if (locp >= locendp)
9249 : : {
9250 : 0 : printf (_(" <INVALID DATA>\n"));
9251 : : break;
9252 : : }
9253 : 0 : get_uleb128 (v2, locp, locendp);
9254 [ # # ]: 0 : printf (" view pair %" PRId64 ", %" PRId64 "\n", v1, v2);
9255 : : }
9256 : :
9257 : 0 : printf ("\n");
9258 : 0 : readp = (unsigned char *) locendp;
9259 : 0 : continue;
9260 : : }
9261 : :
9262 : 89 : uint8_t kind = *readp++;
9263 : 89 : uint64_t op1, op2, len;
9264 : :
9265 : : /* Skip padding. */
9266 [ - + ]: 89 : if (start_of_list && kind == DW_LLE_end_of_list)
9267 : 0 : continue;
9268 : :
9269 [ + + ]: 89 : if (start_of_list)
9270 : : {
9271 : 32 : base = cu_base;
9272 : 32 : printf (" Offset: %" PRIx64 ", Index: %" PRIx64 "\n",
9273 : 32 : (uint64_t) (readp - (unsigned char *) data->d_buf - 1),
9274 : 32 : (uint64_t) (readp - offset_array_start - 1));
9275 : 32 : start_of_list = false;
9276 : : }
9277 : :
9278 : 89 : printf (" %s", dwarf_loc_list_encoding_name (kind));
9279 [ + - - + : 89 : switch (kind)
+ - + - +
- ]
9280 : : {
9281 : 32 : case DW_LLE_end_of_list:
9282 : 32 : start_of_list = true;
9283 : 32 : printf ("\n\n");
9284 : : break;
9285 : :
9286 : 0 : case DW_LLE_base_addressx:
9287 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
9288 : : {
9289 : 0 : invalid_entry:
9290 : 0 : error (0, 0, _("invalid loclists data"));
9291 : 0 : goto next_table;
9292 : : }
9293 : 0 : get_uleb128 (op1, readp, nexthdr);
9294 : 0 : printf (" %" PRIx64 "\n", op1);
9295 [ # # ]: 0 : if (! print_unresolved_addresses)
9296 : : {
9297 : 0 : Dwarf_Addr addr;
9298 [ # # ]: 0 : if (get_indexed_addr (cu, op1, &addr) != 0)
9299 : 0 : printf (" ???\n");
9300 : : else
9301 : : {
9302 : 0 : printf (" ");
9303 : 0 : print_dwarf_addr (dwflmod, address_size, addr, addr);
9304 : 0 : printf ("\n");
9305 : : }
9306 : : }
9307 : : break;
9308 : :
9309 : 0 : case DW_LLE_startx_endx:
9310 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
9311 : 0 : goto invalid_entry;
9312 : 0 : get_uleb128 (op1, readp, nexthdr);
9313 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
9314 : 0 : goto invalid_entry;
9315 : 0 : get_uleb128 (op2, readp, nexthdr);
9316 : 0 : printf (" %" PRIx64 ", %" PRIx64 "\n", op1, op2);
9317 [ # # ]: 0 : if (! print_unresolved_addresses)
9318 : : {
9319 : 0 : Dwarf_Addr addr1;
9320 : 0 : Dwarf_Addr addr2;
9321 [ # # ]: 0 : if (get_indexed_addr (cu, op1, &addr1) != 0
9322 [ # # ]: 0 : || get_indexed_addr (cu, op2, &addr2) != 0)
9323 : : {
9324 : 0 : printf (" ???..\n");
9325 : 0 : printf (" ???\n");
9326 : : }
9327 : : else
9328 : : {
9329 : 0 : printf (" ");
9330 : 0 : print_dwarf_addr (dwflmod, address_size, addr1, addr1);
9331 : 0 : printf ("..\n ");
9332 : 0 : print_dwarf_addr (dwflmod, address_size,
9333 : : addr2 - 1, addr2);
9334 : 0 : printf ("\n");
9335 : : }
9336 : : }
9337 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
9338 : 0 : goto invalid_entry;
9339 : 0 : get_uleb128 (len, readp, nexthdr);
9340 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < len)
9341 : 0 : goto invalid_entry;
9342 : 0 : print_ops (dwflmod, dbg, 8, 8, version,
9343 : : address_size, offset_size, cu, len, readp);
9344 : 0 : readp += len;
9345 : 0 : break;
9346 : :
9347 : 26 : case DW_LLE_startx_length:
9348 [ - + ]: 26 : if ((uint64_t) (nexthdr - readp) < 1)
9349 : 0 : goto invalid_entry;
9350 : 26 : get_uleb128 (op1, readp, nexthdr);
9351 [ - + ]: 26 : if ((uint64_t) (nexthdr - readp) < 1)
9352 : 0 : goto invalid_entry;
9353 : 26 : get_uleb128 (op2, readp, nexthdr);
9354 : 26 : printf (" %" PRIx64 ", %" PRIx64 "\n", op1, op2);
9355 [ + - ]: 26 : if (! print_unresolved_addresses)
9356 : : {
9357 : 26 : Dwarf_Addr addr1;
9358 : 26 : Dwarf_Addr addr2;
9359 [ - + ]: 26 : if (get_indexed_addr (cu, op1, &addr1) != 0)
9360 : : {
9361 : 0 : printf (" ???..\n");
9362 : 0 : printf (" ???\n");
9363 : : }
9364 : : else
9365 : : {
9366 : 26 : addr2 = addr1 + op2;
9367 : 26 : printf (" ");
9368 : 26 : print_dwarf_addr (dwflmod, address_size, addr1, addr1);
9369 : 26 : printf ("..\n ");
9370 : 26 : print_dwarf_addr (dwflmod, address_size,
9371 : : addr2 - 1, addr2);
9372 : 26 : printf ("\n");
9373 : : }
9374 : : }
9375 [ - + ]: 26 : if ((uint64_t) (nexthdr - readp) < 1)
9376 : 0 : goto invalid_entry;
9377 : 26 : get_uleb128 (len, readp, nexthdr);
9378 [ - + ]: 26 : if ((uint64_t) (nexthdr - readp) < len)
9379 : 0 : goto invalid_entry;
9380 : 26 : print_ops (dwflmod, dbg, 8, 8, version,
9381 : : address_size, offset_size, cu, len, readp);
9382 : 26 : readp += len;
9383 : 26 : break;
9384 : :
9385 : 24 : case DW_LLE_offset_pair:
9386 [ - + ]: 24 : if ((uint64_t) (nexthdr - readp) < 1)
9387 : 0 : goto invalid_entry;
9388 : 24 : get_uleb128 (op1, readp, nexthdr);
9389 [ - + ]: 24 : if ((uint64_t) (nexthdr - readp) < 1)
9390 : 0 : goto invalid_entry;
9391 : 24 : get_uleb128 (op2, readp, nexthdr);
9392 : 24 : printf (" %" PRIx64 ", %" PRIx64 "\n", op1, op2);
9393 [ + - ]: 24 : if (! print_unresolved_addresses)
9394 : : {
9395 : 24 : op1 += base;
9396 : 24 : op2 += base;
9397 : 24 : printf (" ");
9398 : 24 : print_dwarf_addr (dwflmod, address_size, op1, op1);
9399 : 24 : printf ("..\n ");
9400 : 24 : print_dwarf_addr (dwflmod, address_size, op2 - 1, op2);
9401 : 24 : printf ("\n");
9402 : : }
9403 [ - + ]: 24 : if ((uint64_t) (nexthdr - readp) < 1)
9404 : 0 : goto invalid_entry;
9405 : 24 : get_uleb128 (len, readp, nexthdr);
9406 [ - + ]: 24 : if ((uint64_t) (nexthdr - readp) < len)
9407 : 0 : goto invalid_entry;
9408 : 24 : print_ops (dwflmod, dbg, 8, 8, version,
9409 : : address_size, offset_size, cu, len, readp);
9410 : 24 : readp += len;
9411 : 24 : break;
9412 : :
9413 : 0 : case DW_LLE_default_location:
9414 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
9415 : 0 : goto invalid_entry;
9416 : 0 : get_uleb128 (len, readp, nexthdr);
9417 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < len)
9418 : 0 : goto invalid_entry;
9419 : 0 : print_ops (dwflmod, dbg, 8, 8, version,
9420 : : address_size, offset_size, cu, len, readp);
9421 : 0 : readp += len;
9422 : 0 : break;
9423 : :
9424 : 5 : case DW_LLE_base_address:
9425 [ - + ]: 5 : if (address_size == 4)
9426 : : {
9427 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 4)
9428 : 0 : goto invalid_entry;
9429 [ # # ]: 0 : op1 = read_4ubyte_unaligned_inc (dbg, readp);
9430 : : }
9431 : : else
9432 : : {
9433 [ - + ]: 5 : if ((uint64_t) (nexthdr - readp) < 8)
9434 : 0 : goto invalid_entry;
9435 [ - + ]: 10 : op1 = read_8ubyte_unaligned_inc (dbg, readp);
9436 : : }
9437 : 5 : base = op1;
9438 : 5 : printf (" 0x%" PRIx64 "\n", base);
9439 [ + - ]: 5 : if (! print_unresolved_addresses)
9440 : : {
9441 : 5 : printf (" ");
9442 : 5 : print_dwarf_addr (dwflmod, address_size, base, base);
9443 [ + + ]: 98 : printf ("\n");
9444 : : }
9445 : : break;
9446 : :
9447 : 0 : case DW_LLE_start_end:
9448 [ # # ]: 0 : if (address_size == 4)
9449 : : {
9450 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 8)
9451 : 0 : goto invalid_entry;
9452 [ # # ]: 0 : op1 = read_4ubyte_unaligned_inc (dbg, readp);
9453 [ # # ]: 0 : op2 = read_4ubyte_unaligned_inc (dbg, readp);
9454 : : }
9455 : : else
9456 : : {
9457 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 16)
9458 : 0 : goto invalid_entry;
9459 [ # # ]: 0 : op1 = read_8ubyte_unaligned_inc (dbg, readp);
9460 [ # # ]: 0 : op2 = read_8ubyte_unaligned_inc (dbg, readp);
9461 : : }
9462 : 0 : printf (" 0x%" PRIx64 "..0x%" PRIx64 "\n", op1, op2);
9463 [ # # ]: 0 : if (! print_unresolved_addresses)
9464 : : {
9465 : 0 : printf (" ");
9466 : 0 : print_dwarf_addr (dwflmod, address_size, op1, op1);
9467 : 0 : printf ("..\n ");
9468 : 0 : print_dwarf_addr (dwflmod, address_size, op2 - 1, op2);
9469 : 0 : printf ("\n");
9470 : : }
9471 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
9472 : 0 : goto invalid_entry;
9473 : 0 : get_uleb128 (len, readp, nexthdr);
9474 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < len)
9475 : 0 : goto invalid_entry;
9476 : 0 : print_ops (dwflmod, dbg, 8, 8, version,
9477 : : address_size, offset_size, cu, len, readp);
9478 : 0 : readp += len;
9479 : 0 : break;
9480 : :
9481 : 2 : case DW_LLE_start_length:
9482 [ - + ]: 2 : if (address_size == 4)
9483 : : {
9484 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 4)
9485 : 0 : goto invalid_entry;
9486 [ # # ]: 0 : op1 = read_4ubyte_unaligned_inc (dbg, readp);
9487 : : }
9488 : : else
9489 : : {
9490 [ - + ]: 2 : if ((uint64_t) (nexthdr - readp) < 8)
9491 : 0 : goto invalid_entry;
9492 [ - + ]: 4 : op1 = read_8ubyte_unaligned_inc (dbg, readp);
9493 : : }
9494 [ - + ]: 2 : if ((uint64_t) (nexthdr - readp) < 1)
9495 : 0 : goto invalid_entry;
9496 : 2 : get_uleb128 (op2, readp, nexthdr);
9497 : 2 : printf (" 0x%" PRIx64 ", %" PRIx64 "\n", op1, op2);
9498 [ + - ]: 2 : if (! print_unresolved_addresses)
9499 : : {
9500 : 2 : op2 = op1 + op2;
9501 : 2 : printf (" ");
9502 : 2 : print_dwarf_addr (dwflmod, address_size, op1, op1);
9503 : 2 : printf ("..\n ");
9504 : 2 : print_dwarf_addr (dwflmod, address_size, op2 - 1, op2);
9505 : 2 : printf ("\n");
9506 : : }
9507 [ - + ]: 2 : if ((uint64_t) (nexthdr - readp) < 1)
9508 : 0 : goto invalid_entry;
9509 : 2 : get_uleb128 (len, readp, nexthdr);
9510 [ - + ]: 2 : if ((uint64_t) (nexthdr - readp) < len)
9511 : 0 : goto invalid_entry;
9512 : 2 : print_ops (dwflmod, dbg, 8, 8, version,
9513 : : address_size, offset_size, cu, len, readp);
9514 : 2 : readp += len;
9515 : 2 : break;
9516 : :
9517 : 0 : default:
9518 : 0 : goto invalid_entry;
9519 : : }
9520 : : }
9521 : :
9522 : 4 : next_table:
9523 [ - + ]: 4 : if (readp != nexthdr)
9524 : : {
9525 : 0 : size_t padding = nexthdr - readp;
9526 : 0 : printf (_(" %zu padding bytes\n\n"), padding);
9527 : 0 : readp = nexthdr;
9528 : : }
9529 : : }
9530 : : }
9531 : :
9532 : :
9533 : : static void
9534 : 42 : print_debug_loc_section (Dwfl_Module *dwflmod,
9535 : : Ebl *ebl, GElf_Ehdr *ehdr,
9536 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
9537 : : {
9538 : 84 : Elf_Data *data = (dbg->sectiondata[IDX_debug_loc]
9539 [ - + ]: 42 : ?: elf_rawdata (scn, NULL));
9540 : :
9541 [ - + ]: 42 : if (unlikely (data == NULL))
9542 : : {
9543 : 0 : error (0, 0, _("cannot get .debug_loc content: %s"),
9544 : : elf_errmsg (-1));
9545 : 0 : return;
9546 : : }
9547 : :
9548 : 42 : printf (_("\
9549 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
9550 : : elf_ndxscn (scn), section_name (ebl, shdr),
9551 : 42 : (uint64_t) shdr->sh_offset);
9552 : :
9553 : 42 : sort_listptr (&known_locsptr, "loclistptr");
9554 : 42 : size_t listptr_idx = 0;
9555 : :
9556 [ + - ]: 42 : uint_fast8_t address_size = ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 4 : 8;
9557 : 42 : uint_fast8_t offset_size = 4;
9558 : :
9559 : 42 : bool first = true;
9560 : 42 : Dwarf_Addr base = 0;
9561 : 42 : unsigned char *readp = data->d_buf;
9562 : 42 : unsigned char *const endp = (unsigned char *) data->d_buf + data->d_size;
9563 : 42 : Dwarf_CU *last_cu = NULL;
9564 [ + + ]: 377331 : while (readp < endp)
9565 : : {
9566 : 377289 : ptrdiff_t offset = readp - (unsigned char *) data->d_buf;
9567 : 377289 : Dwarf_CU *cu = last_cu;
9568 : 377289 : unsigned int attr = 0;
9569 : :
9570 [ + + - + ]: 377289 : if (first && skip_listptr_hole (&known_locsptr, &listptr_idx,
9571 : : &address_size, &offset_size, &base,
9572 : : &cu, offset, &readp, endp, &attr))
9573 : 52876 : continue;
9574 : :
9575 [ + + ]: 377289 : if (last_cu != cu)
9576 : : {
9577 : 1602 : Dwarf_Die cudie;
9578 [ - + ]: 1602 : if (dwarf_cu_die (cu, &cudie,
9579 : : NULL, NULL, NULL, NULL,
9580 : : NULL, NULL) == NULL)
9581 : 0 : printf (_("\n Unknown CU base: "));
9582 : : else
9583 : 1602 : printf (_("\n CU [%6" PRIx64 "] base: "),
9584 : : dwarf_dieoffset (&cudie));
9585 : 1602 : print_dwarf_addr (dwflmod, address_size, base, base);
9586 : 1602 : printf ("\n");
9587 : : }
9588 : 377289 : last_cu = cu;
9589 : :
9590 [ + + ]: 377289 : if (attr == DW_AT_GNU_locviews)
9591 : : {
9592 : 52876 : Dwarf_Off next_off = next_listptr_offset (&known_locsptr,
9593 : : &listptr_idx, offset);
9594 : 52876 : const unsigned char *locp = readp;
9595 : 52876 : const unsigned char *locendp;
9596 [ + - ]: 52876 : if (next_off == 0
9597 : 52876 : || next_off > (size_t) (endp
9598 [ + - ]: 52876 : - (const unsigned char *) data->d_buf))
9599 : 52876 : locendp = endp;
9600 : : else
9601 : 52876 : locendp = (const unsigned char *) data->d_buf + next_off;
9602 : :
9603 [ + + ]: 324194 : while (locp < locendp)
9604 : : {
9605 : 271318 : uint64_t v1, v2;
9606 : 271318 : get_uleb128 (v1, locp, locendp);
9607 [ - + ]: 271318 : if (locp >= locendp)
9608 : : {
9609 : 0 : printf (_(" [%6tx] <INVALID DATA>\n"), offset);
9610 : : break;
9611 : : }
9612 : 271318 : get_uleb128 (v2, locp, locendp);
9613 [ + + ]: 271318 : if (first) /* First view pair in a list. */
9614 : 52876 : printf (" [%6tx] ", offset);
9615 : : else
9616 : 218442 : printf (" ");
9617 : 271318 : printf ("view pair %" PRId64 ", %" PRId64 "\n", v1, v2);
9618 : 271318 : first = false;
9619 : : }
9620 : :
9621 : 52876 : first = true;
9622 : 52876 : readp = (unsigned char *) locendp;
9623 : 52876 : continue;
9624 : : }
9625 : :
9626 : : /* GNU DebugFission encoded addresses as addrx. */
9627 : 648826 : bool is_debugfission = ((cu != NULL
9628 [ # # ]: 0 : || split_dwarf_cu_base (dbg, &cu, &base))
9629 [ - + + - ]: 324413 : && (cu->version < 5
9630 [ + + ]: 324413 : && cu->unit_type == DW_UT_split_compile));
9631 [ + + ]: 324413 : if (!is_debugfission
9632 [ - + ]: 324353 : && unlikely (data->d_size - offset < (size_t) address_size * 2))
9633 : : {
9634 : 0 : invalid_data:
9635 : 0 : printf (_(" [%6tx] <INVALID DATA>\n"), offset);
9636 : 0 : break;
9637 : : }
9638 : :
9639 : 324413 : Dwarf_Addr begin;
9640 : 324413 : Dwarf_Addr end;
9641 : 324413 : bool use_base = true;
9642 [ + + ]: 324413 : if (is_debugfission)
9643 : : {
9644 : 60 : const unsigned char *locp = readp;
9645 : 60 : const unsigned char *locendp = readp + data->d_size;
9646 [ - + ]: 60 : if (locp >= locendp)
9647 : 0 : goto invalid_data;
9648 : :
9649 : 60 : Dwarf_Word idx;
9650 : 60 : unsigned char code = *locp++;
9651 [ + - - + : 60 : switch (code)
- ]
9652 : : {
9653 : 20 : case DW_LLE_GNU_end_of_list_entry:
9654 : 20 : begin = 0;
9655 : 20 : end = 0;
9656 : 20 : break;
9657 : :
9658 : 0 : case DW_LLE_GNU_base_address_selection_entry:
9659 [ # # ]: 0 : if (locp >= locendp)
9660 : 0 : goto invalid_data;
9661 : 0 : begin = (Dwarf_Addr) -1;
9662 : 0 : get_uleb128 (idx, locp, locendp);
9663 [ # # ]: 0 : if (get_indexed_addr (cu, idx, &end) != 0)
9664 : 0 : end = idx; /* ... */
9665 : : break;
9666 : :
9667 : 0 : case DW_LLE_GNU_start_end_entry:
9668 [ # # ]: 0 : if (locp >= locendp)
9669 : 0 : goto invalid_data;
9670 : 0 : get_uleb128 (idx, locp, locendp);
9671 [ # # ]: 0 : if (get_indexed_addr (cu, idx, &begin) != 0)
9672 : 0 : begin = idx; /* ... */
9673 [ # # ]: 0 : if (locp >= locendp)
9674 : 0 : goto invalid_data;
9675 : 0 : get_uleb128 (idx, locp, locendp);
9676 [ # # ]: 0 : if (get_indexed_addr (cu, idx, &end) != 0)
9677 : 0 : end = idx; /* ... */
9678 : : use_base = false;
9679 : : break;
9680 : :
9681 : 40 : case DW_LLE_GNU_start_length_entry:
9682 [ - + ]: 40 : if (locp >= locendp)
9683 : 0 : goto invalid_data;
9684 : 40 : get_uleb128 (idx, locp, locendp);
9685 [ - + ]: 40 : if (get_indexed_addr (cu, idx, &begin) != 0)
9686 : 0 : begin = idx; /* ... */
9687 [ - + ]: 40 : if (locendp - locp < 4)
9688 : 0 : goto invalid_data;
9689 [ - + ]: 40 : end = read_4ubyte_unaligned_inc (dbg, locp);
9690 : 40 : end += begin;
9691 : 40 : use_base = false;
9692 : 40 : break;
9693 : :
9694 : 0 : default:
9695 : 0 : goto invalid_data;
9696 : : }
9697 : :
9698 : 60 : readp = (unsigned char *) locp;
9699 : : }
9700 [ + - ]: 324353 : else if (address_size == 8)
9701 : : {
9702 [ + + ]: 324353 : begin = read_8ubyte_unaligned_inc (dbg, readp);
9703 [ + + ]: 648706 : end = read_8ubyte_unaligned_inc (dbg, readp);
9704 : : }
9705 : : else
9706 : : {
9707 [ # # ]: 0 : begin = read_4ubyte_unaligned_inc (dbg, readp);
9708 [ # # ]: 0 : end = read_4ubyte_unaligned_inc (dbg, readp);
9709 [ # # ]: 0 : if (begin == (Dwarf_Addr) (uint32_t) -1)
9710 : 0 : begin = (Dwarf_Addr) -1l;
9711 : : }
9712 : :
9713 [ - + ]: 324413 : if (begin == (Dwarf_Addr) -1l) /* Base address entry. */
9714 : : {
9715 [ # # ]: 0 : if (first)
9716 : 0 : printf (" [%6tx] ", offset);
9717 : : else
9718 : 0 : printf (" ");
9719 : 0 : puts (_("base address"));
9720 : 0 : printf (" ");
9721 : 0 : print_dwarf_addr (dwflmod, address_size, end, end);
9722 : 0 : printf ("\n");
9723 : 0 : base = end;
9724 : 0 : first = false;
9725 : : }
9726 [ + + + + ]: 324413 : else if (begin == 0 && end == 0) /* End of list entry. */
9727 : : {
9728 [ - + ]: 52950 : if (first)
9729 : 324413 : printf (_(" [%6tx] empty list\n"), offset);
9730 : : first = true;
9731 : : }
9732 : : else
9733 : : {
9734 : : /* We have a location expression entry. */
9735 [ + + ]: 271463 : uint_fast16_t len = read_2ubyte_unaligned_inc (dbg, readp);
9736 : :
9737 [ + + ]: 271463 : if (first) /* First entry in a list. */
9738 : 52950 : printf (" [%6tx] ", offset);
9739 : : else
9740 : 218513 : printf (" ");
9741 : :
9742 : 271463 : printf ("range %" PRIx64 ", %" PRIx64 "\n", begin, end);
9743 [ + + ]: 271463 : if (! print_unresolved_addresses)
9744 : : {
9745 [ + + ]: 271433 : Dwarf_Addr dab = use_base ? base + begin : begin;
9746 [ + + ]: 271433 : Dwarf_Addr dae = use_base ? base + end : end;
9747 : 271433 : printf (" ");
9748 : 271433 : print_dwarf_addr (dwflmod, address_size, dab, dab);
9749 : 271433 : printf ("..\n ");
9750 : 271433 : print_dwarf_addr (dwflmod, address_size, dae - 1, dae);
9751 : 271433 : printf ("\n");
9752 : : }
9753 : :
9754 [ - + ]: 271463 : if (endp - readp <= (ptrdiff_t) len)
9755 : : {
9756 : 0 : fputs (_(" <INVALID DATA>\n"), stdout);
9757 : 0 : break;
9758 : : }
9759 : :
9760 : 271463 : print_ops (dwflmod, dbg, 11, 11,
9761 [ + - ]: 271463 : cu != NULL ? cu->version : 3,
9762 : : address_size, offset_size, cu, len, readp);
9763 : :
9764 : 271463 : first = false;
9765 : 271463 : readp += len;
9766 : : }
9767 : : }
9768 : : }
9769 : :
9770 : : struct mac_culist
9771 : : {
9772 : : Dwarf_Die die;
9773 : : Dwarf_Off offset;
9774 : : Dwarf_Files *files;
9775 : : struct mac_culist *next;
9776 : : };
9777 : :
9778 : :
9779 : : static int
9780 : 0 : mac_compare (const void *p1, const void *p2)
9781 : : {
9782 : 0 : struct mac_culist *m1 = (struct mac_culist *) p1;
9783 : 0 : struct mac_culist *m2 = (struct mac_culist *) p2;
9784 : :
9785 [ # # ]: 0 : if (m1->offset < m2->offset)
9786 : : return -1;
9787 [ # # ]: 0 : if (m1->offset > m2->offset)
9788 : 0 : return 1;
9789 : : return 0;
9790 : : }
9791 : :
9792 : :
9793 : : static void
9794 : 0 : print_debug_macinfo_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
9795 : : Ebl *ebl,
9796 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
9797 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
9798 : : {
9799 : 0 : printf (_("\
9800 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
9801 : : elf_ndxscn (scn), section_name (ebl, shdr),
9802 : 0 : (uint64_t) shdr->sh_offset);
9803 [ # # ]: 0 : putc_unlocked ('\n', stdout);
9804 : :
9805 : : /* There is no function in libdw to iterate over the raw content of
9806 : : the section but it is easy enough to do. */
9807 : 0 : Elf_Data *data = (dbg->sectiondata[IDX_debug_macinfo]
9808 [ # # ]: 0 : ?: elf_rawdata (scn, NULL));
9809 [ # # ]: 0 : if (unlikely (data == NULL))
9810 : : {
9811 : 0 : error (0, 0, _("cannot get macro information section data: %s"),
9812 : : elf_errmsg (-1));
9813 : 0 : return;
9814 : : }
9815 : :
9816 : : /* Get the source file information for all CUs. */
9817 : 0 : Dwarf_Off offset;
9818 : 0 : Dwarf_Off ncu = 0;
9819 : 0 : size_t hsize;
9820 : 0 : struct mac_culist *culist = NULL;
9821 : 0 : size_t nculist = 0;
9822 [ # # ]: 0 : while (dwarf_nextcu (dbg, offset = ncu, &ncu, &hsize, NULL, NULL, NULL) == 0)
9823 : : {
9824 : 0 : Dwarf_Die cudie;
9825 [ # # ]: 0 : if (dwarf_offdie (dbg, offset + hsize, &cudie) == NULL)
9826 : 0 : continue;
9827 : :
9828 : 0 : Dwarf_Attribute attr;
9829 [ # # ]: 0 : if (dwarf_attr (&cudie, DW_AT_macro_info, &attr) == NULL)
9830 : 0 : continue;
9831 : :
9832 : 0 : Dwarf_Word macoff;
9833 [ # # ]: 0 : if (dwarf_formudata (&attr, &macoff) != 0)
9834 : 0 : continue;
9835 : :
9836 : 0 : struct mac_culist *newp = (struct mac_culist *) alloca (sizeof (*newp));
9837 : 0 : newp->die = cudie;
9838 : 0 : newp->offset = macoff;
9839 : 0 : newp->files = NULL;
9840 : 0 : newp->next = culist;
9841 : 0 : culist = newp;
9842 : 0 : ++nculist;
9843 : : }
9844 : :
9845 : : /* Convert the list into an array for easier consumption. */
9846 : 0 : struct mac_culist *cus = (struct mac_culist *) alloca ((nculist + 1)
9847 : : * sizeof (*cus));
9848 : : /* Add sentinel. */
9849 : 0 : cus[nculist].offset = data->d_size;
9850 : 0 : cus[nculist].files = (Dwarf_Files *) -1l;
9851 [ # # ]: 0 : if (nculist > 0)
9852 : : {
9853 [ # # ]: 0 : for (size_t cnt = nculist - 1; culist != NULL; --cnt)
9854 : : {
9855 [ # # ]: 0 : assert (cnt < nculist);
9856 : 0 : cus[cnt] = *culist;
9857 : 0 : culist = culist->next;
9858 : : }
9859 : :
9860 : : /* Sort the array according to the offset in the .debug_macinfo
9861 : : section. Note we keep the sentinel at the end. */
9862 : 0 : qsort (cus, nculist, sizeof (*cus), mac_compare);
9863 : : }
9864 : :
9865 : 0 : const unsigned char *readp = (const unsigned char *) data->d_buf;
9866 : 0 : const unsigned char *readendp = readp + data->d_size;
9867 : 0 : int level = 1;
9868 : :
9869 : 0 : while (readp < readendp)
9870 : : {
9871 : 0 : unsigned int opcode = *readp++;
9872 : 0 : unsigned int u128;
9873 : 0 : unsigned int u128_2;
9874 : 0 : const unsigned char *endp;
9875 : :
9876 [ # # # # ]: 0 : switch (opcode)
9877 : : {
9878 : 0 : case DW_MACINFO_define:
9879 : : case DW_MACINFO_undef:
9880 : : case DW_MACINFO_vendor_ext:
9881 : : /* For the first two opcodes the parameters are
9882 : : line, string
9883 : : For the latter
9884 : : number, string.
9885 : : We can treat these cases together. */
9886 : 0 : get_uleb128 (u128, readp, readendp);
9887 : :
9888 : 0 : endp = memchr (readp, '\0', readendp - readp);
9889 [ # # ]: 0 : if (unlikely (endp == NULL))
9890 : : {
9891 : 0 : printf (_("\
9892 : : %*s*** non-terminated string at end of section"),
9893 : : level, "");
9894 : 0 : return;
9895 : : }
9896 : :
9897 [ # # ]: 0 : if (opcode == DW_MACINFO_define)
9898 : 0 : printf ("%*s#define %s, line %u\n",
9899 : : level, "", (char *) readp, u128);
9900 [ # # ]: 0 : else if (opcode == DW_MACINFO_undef)
9901 : 0 : printf ("%*s#undef %s, line %u\n",
9902 : : level, "", (char *) readp, u128);
9903 : : else
9904 : 0 : printf (" #vendor-ext %s, number %u\n", (char *) readp, u128);
9905 : :
9906 : 0 : readp = endp + 1;
9907 : 0 : break;
9908 : :
9909 : 0 : case DW_MACINFO_start_file:
9910 : : /* The two parameters are line and file index, in this order. */
9911 : 0 : get_uleb128 (u128, readp, readendp);
9912 [ # # ]: 0 : if (readendp - readp < 1)
9913 : : {
9914 : 0 : printf (_("\
9915 : : %*s*** missing DW_MACINFO_start_file argument at end of section"),
9916 : : level, "");
9917 : 0 : return;
9918 : : }
9919 : 0 : get_uleb128 (u128_2, readp, readendp);
9920 : :
9921 : : /* Find the CU DIE for this file. */
9922 : 0 : size_t macoff = readp - (const unsigned char *) data->d_buf;
9923 : 0 : const char *fname = "???";
9924 [ # # # # ]: 0 : if (macoff >= cus[0].offset && cus[0].offset != data->d_size)
9925 : : {
9926 [ # # # # ]: 0 : while (macoff >= cus[1].offset && cus[1].offset != data->d_size)
9927 : 0 : ++cus;
9928 : :
9929 [ # # ]: 0 : if (cus[0].files == NULL
9930 [ # # ]: 0 : && dwarf_getsrcfiles (&cus[0].die, &cus[0].files, NULL) != 0)
9931 : 0 : cus[0].files = (Dwarf_Files *) -1l;
9932 : :
9933 [ # # ]: 0 : if (cus[0].files != (Dwarf_Files *) -1l)
9934 : 0 : fname = (dwarf_filesrc (cus[0].files, u128_2, NULL, NULL)
9935 [ # # ]: 0 : ?: "???");
9936 : : }
9937 : :
9938 : 0 : printf ("%*sstart_file %u, [%u] %s\n",
9939 : : level, "", u128, u128_2, fname);
9940 : 0 : ++level;
9941 : 0 : break;
9942 : :
9943 : 0 : case DW_MACINFO_end_file:
9944 : 0 : --level;
9945 : 0 : printf ("%*send_file\n", level, "");
9946 : : /* Nothing more to do. */
9947 : : break;
9948 : :
9949 : 0 : default:
9950 : : // XXX gcc seems to generate files with a trailing zero.
9951 [ # # # # ]: 0 : if (unlikely (opcode != 0 || readp != readendp))
9952 [ # # ]: 0 : printf ("%*s*** invalid opcode %u\n", level, "", opcode);
9953 : : break;
9954 : : }
9955 : : }
9956 : : }
9957 : :
9958 : :
9959 : : static void
9960 : 3 : print_debug_macro_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
9961 : : Ebl *ebl,
9962 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
9963 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
9964 : : {
9965 : 3 : printf (_("\
9966 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
9967 : : elf_ndxscn (scn), section_name (ebl, shdr),
9968 : 3 : (uint64_t) shdr->sh_offset);
9969 [ - + ]: 3 : putc_unlocked ('\n', stdout);
9970 : :
9971 : 3 : Elf_Data *data = elf_getdata (scn, NULL);
9972 [ - + ]: 3 : if (unlikely (data == NULL))
9973 : : {
9974 : 0 : error (0, 0, _("cannot get macro information section data: %s"),
9975 : : elf_errmsg (-1));
9976 : 0 : return;
9977 : : }
9978 : :
9979 : : /* Get the source file information for all CUs. Uses same
9980 : : datastructure as macinfo. But uses offset field to directly
9981 : : match .debug_line offset. And just stored in a list. */
9982 : 3 : Dwarf_Off offset;
9983 : 3 : Dwarf_Off ncu = 0;
9984 : 3 : size_t hsize;
9985 : 3 : struct mac_culist *culist = NULL;
9986 : 3 : size_t nculist = 0;
9987 [ + + ]: 7 : while (dwarf_nextcu (dbg, offset = ncu, &ncu, &hsize, NULL, NULL, NULL) == 0)
9988 : : {
9989 : 4 : Dwarf_Die cudie;
9990 [ - + ]: 4 : if (dwarf_offdie (dbg, offset + hsize, &cudie) == NULL)
9991 : 0 : continue;
9992 : :
9993 : 4 : Dwarf_Attribute attr;
9994 [ - + ]: 4 : if (dwarf_attr (&cudie, DW_AT_stmt_list, &attr) == NULL)
9995 : 0 : continue;
9996 : :
9997 : 4 : Dwarf_Word lineoff;
9998 [ - + ]: 4 : if (dwarf_formudata (&attr, &lineoff) != 0)
9999 : 0 : continue;
10000 : :
10001 : 4 : struct mac_culist *newp = (struct mac_culist *) alloca (sizeof (*newp));
10002 : 4 : newp->die = cudie;
10003 : 4 : newp->offset = lineoff;
10004 : 4 : newp->files = NULL;
10005 : 4 : newp->next = culist;
10006 : 4 : culist = newp;
10007 : 4 : ++nculist;
10008 : : }
10009 : :
10010 : 3 : const unsigned char *readp = (const unsigned char *) data->d_buf;
10011 : 3 : const unsigned char *readendp = readp + data->d_size;
10012 : :
10013 [ + + ]: 11 : while (readp < readendp)
10014 : : {
10015 : 16 : printf (_(" Offset: 0x%" PRIx64 "\n"),
10016 : 8 : (uint64_t) (readp - (const unsigned char *) data->d_buf));
10017 : :
10018 : : // Header, 2 byte version, 1 byte flag, optional .debug_line offset,
10019 : : // optional vendor extension macro entry table.
10020 [ - + ]: 8 : if (readp + 2 > readendp)
10021 : : {
10022 : 0 : invalid_data:
10023 : 0 : error (0, 0, _("invalid data"));
10024 : 0 : return;
10025 : : }
10026 [ - + ]: 8 : const uint16_t vers = read_2ubyte_unaligned_inc (dbg, readp);
10027 : 8 : printf (_(" Version: %" PRIu16 "\n"), vers);
10028 : :
10029 : : // Version 4 is the GNU extension for DWARF4. DWARF5 will use version
10030 : : // 5 when it gets standardized.
10031 [ - + ]: 8 : if (vers != 4 && vers != 5)
10032 : : {
10033 : 0 : printf (_(" unknown version, cannot parse section\n"));
10034 : 0 : return;
10035 : : }
10036 : :
10037 [ - + ]: 8 : if (readp + 1 > readendp)
10038 : 0 : goto invalid_data;
10039 : 8 : const unsigned char flag = *readp++;
10040 : 8 : printf (_(" Flag: 0x%" PRIx8), flag);
10041 [ + + ]: 8 : if (flag != 0)
10042 : : {
10043 : 4 : printf (" (");
10044 [ - + ]: 4 : if ((flag & 0x01) != 0)
10045 : : {
10046 : 0 : printf ("offset_size");
10047 [ # # ]: 0 : if ((flag & 0xFE) != 0)
10048 : 0 : printf (", ");
10049 : : }
10050 [ + - ]: 4 : if ((flag & 0x02) != 0)
10051 : : {
10052 : 4 : printf ("debug_line_offset");
10053 [ - + ]: 4 : if ((flag & 0xFC) != 0)
10054 : 0 : printf (", ");
10055 : : }
10056 [ - + ]: 4 : if ((flag & 0x04) != 0)
10057 : : {
10058 : 0 : printf ("operands_table");
10059 [ # # ]: 0 : if ((flag & 0xF8) != 0)
10060 : 0 : printf (", ");
10061 : : }
10062 [ - + ]: 4 : if ((flag & 0xF8) != 0)
10063 : 0 : printf ("unknown");
10064 : 4 : printf (")");
10065 : : }
10066 : 8 : printf ("\n");
10067 : :
10068 [ + - ]: 8 : unsigned int offset_len = (flag & 0x01) ? 8 : 4;
10069 : 8 : printf (_(" Offset length: %" PRIu8 "\n"), offset_len);
10070 : 8 : Dwarf_Off line_offset = -1;
10071 [ + + ]: 8 : if (flag & 0x02)
10072 : : {
10073 [ - + ]: 4 : if (offset_len == 8)
10074 [ # # ]: 0 : line_offset = read_8ubyte_unaligned_inc (dbg, readp);
10075 : : else
10076 [ - + ]: 8 : line_offset = read_4ubyte_unaligned_inc (dbg, readp);
10077 : 4 : printf (_(" .debug_line offset: 0x%" PRIx64 "\n"),
10078 : : line_offset);
10079 : : }
10080 : :
10081 : 4 : struct mac_culist *cu = NULL;
10082 [ + - ]: 4 : if (line_offset != (Dwarf_Off) -1)
10083 : : {
10084 : : cu = culist;
10085 [ + - + + ]: 5 : while (cu != NULL && line_offset != cu->offset)
10086 : 1 : cu = cu->next;
10087 : : }
10088 : :
10089 [ + + ]: 8 : Dwarf_Off str_offsets_base = str_offsets_base_off (dbg, (cu != NULL
10090 : : ? cu->die.cu
10091 : : : NULL));
10092 : :
10093 : 8 : const unsigned char *vendor[DW_MACRO_hi_user - DW_MACRO_lo_user + 1];
10094 [ - + ]: 8 : memset (vendor, 0, sizeof vendor);
10095 [ - + ]: 8 : if (flag & 0x04)
10096 : : {
10097 : : // 1 byte length, for each item, 1 byte opcode, uleb128 number
10098 : : // of arguments, for each argument 1 byte form code.
10099 [ # # ]: 0 : if (readp + 1 > readendp)
10100 : 0 : goto invalid_data;
10101 : 0 : unsigned int tlen = *readp++;
10102 : 0 : printf (_(" extension opcode table, %" PRIu8 " items:\n"),
10103 : : tlen);
10104 [ # # ]: 0 : for (unsigned int i = 0; i < tlen; i++)
10105 : : {
10106 [ # # ]: 0 : if (readp + 1 > readendp)
10107 : 0 : goto invalid_data;
10108 : 0 : unsigned int opcode = *readp++;
10109 : 0 : printf (_(" [%" PRIx8 "]"), opcode);
10110 : 0 : if (opcode < DW_MACRO_lo_user
10111 [ # # ]: 0 : || opcode > DW_MACRO_hi_user)
10112 : 0 : goto invalid_data;
10113 : : // Record the start of description for this vendor opcode.
10114 : : // uleb128 nr args, 1 byte per arg form.
10115 : 0 : vendor[opcode - DW_MACRO_lo_user] = readp;
10116 [ # # ]: 0 : if (readp + 1 > readendp)
10117 : 0 : goto invalid_data;
10118 : 0 : unsigned int args = *readp++;
10119 [ # # ]: 0 : if (args > 0)
10120 : : {
10121 : 0 : printf (_(" %" PRIu8 " arguments:"), args);
10122 : 0 : while (args > 0)
10123 : : {
10124 [ # # ]: 0 : if (readp + 1 > readendp)
10125 : 0 : goto invalid_data;
10126 : 0 : unsigned int form = *readp++;
10127 : 0 : printf (" %s", dwarf_form_name (form));
10128 [ # # ]: 0 : if (! libdw_valid_user_form (form))
10129 : 0 : goto invalid_data;
10130 : 0 : args--;
10131 [ # # ]: 0 : if (args > 0)
10132 [ # # # # ]: 0 : putchar_unlocked (',');
10133 : : }
10134 : : }
10135 : : else
10136 : 0 : printf (_(" no arguments."));
10137 [ # # ]: 0 : putchar_unlocked ('\n');
10138 : : }
10139 : : }
10140 [ - + ]: 8 : putchar_unlocked ('\n');
10141 : :
10142 : 8 : int level = 1;
10143 [ - + ]: 8 : if (readp + 1 > readendp)
10144 : 0 : goto invalid_data;
10145 : 8 : unsigned int opcode = *readp++;
10146 : 8 : while (opcode != 0)
10147 : : {
10148 : 727 : unsigned int u128;
10149 : 727 : unsigned int u128_2;
10150 : 727 : const unsigned char *endp;
10151 : 727 : uint64_t off;
10152 : :
10153 [ + + + - : 727 : switch (opcode)
+ + + - -
- - - - ]
10154 : : {
10155 : 6 : case DW_MACRO_start_file:
10156 : 6 : get_uleb128 (u128, readp, readendp);
10157 [ - + ]: 6 : if (readp >= readendp)
10158 : 0 : goto invalid_data;
10159 : 6 : get_uleb128 (u128_2, readp, readendp);
10160 : :
10161 : : /* Find the CU DIE that matches this line offset. */
10162 : 6 : const char *fname = "???";
10163 [ + - ]: 6 : if (cu != NULL)
10164 : : {
10165 [ + + ]: 6 : if (cu->files == NULL
10166 [ - + ]: 4 : && dwarf_getsrcfiles (&cu->die, &cu->files,
10167 : : NULL) != 0)
10168 : 0 : cu->files = (Dwarf_Files *) -1l;
10169 : :
10170 [ + - ]: 6 : if (cu->files != (Dwarf_Files *) -1l)
10171 : 6 : fname = (dwarf_filesrc (cu->files, u128_2,
10172 [ - + ]: 6 : NULL, NULL) ?: "???");
10173 : : }
10174 : 6 : printf ("%*sstart_file %u, [%u] %s\n",
10175 : : level, "", u128, u128_2, fname);
10176 : 6 : ++level;
10177 : 6 : break;
10178 : :
10179 : 6 : case DW_MACRO_end_file:
10180 : 6 : --level;
10181 : 6 : printf ("%*send_file\n", level, "");
10182 : : break;
10183 : :
10184 : 1 : case DW_MACRO_define:
10185 : 1 : get_uleb128 (u128, readp, readendp);
10186 : 1 : endp = memchr (readp, '\0', readendp - readp);
10187 [ - + ]: 1 : if (endp == NULL)
10188 : 0 : goto invalid_data;
10189 : 1 : printf ("%*s#define %s, line %u\n",
10190 : : level, "", readp, u128);
10191 : 1 : readp = endp + 1;
10192 : 1 : break;
10193 : :
10194 : 0 : case DW_MACRO_undef:
10195 : 0 : get_uleb128 (u128, readp, readendp);
10196 : 0 : endp = memchr (readp, '\0', readendp - readp);
10197 [ # # ]: 0 : if (endp == NULL)
10198 : 0 : goto invalid_data;
10199 : 0 : printf ("%*s#undef %s, line %u\n",
10200 : : level, "", readp, u128);
10201 : 0 : readp = endp + 1;
10202 : 0 : break;
10203 : :
10204 : 707 : case DW_MACRO_define_strp:
10205 : 707 : get_uleb128 (u128, readp, readendp);
10206 [ - + ]: 707 : if (readp + offset_len > readendp)
10207 : 0 : goto invalid_data;
10208 [ - + ]: 707 : if (offset_len == 8)
10209 [ # # ]: 0 : off = read_8ubyte_unaligned_inc (dbg, readp);
10210 : : else
10211 [ - + ]: 1414 : off = read_4ubyte_unaligned_inc (dbg, readp);
10212 : 707 : printf ("%*s#define %s, line %u (indirect)\n",
10213 : : level, "", dwarf_getstring (dbg, off, NULL), u128);
10214 : : break;
10215 : :
10216 : 1 : case DW_MACRO_undef_strp:
10217 : 1 : get_uleb128 (u128, readp, readendp);
10218 [ - + ]: 1 : if (readp + offset_len > readendp)
10219 : 0 : goto invalid_data;
10220 [ - + ]: 1 : if (offset_len == 8)
10221 [ # # ]: 0 : off = read_8ubyte_unaligned_inc (dbg, readp);
10222 : : else
10223 [ - + ]: 2 : off = read_4ubyte_unaligned_inc (dbg, readp);
10224 : 1 : printf ("%*s#undef %s, line %u (indirect)\n",
10225 : : level, "", dwarf_getstring (dbg, off, NULL), u128);
10226 : : break;
10227 : :
10228 : 6 : case DW_MACRO_import:
10229 [ - + ]: 6 : if (readp + offset_len > readendp)
10230 : 0 : goto invalid_data;
10231 [ - + ]: 6 : if (offset_len == 8)
10232 [ # # ]: 0 : off = read_8ubyte_unaligned_inc (dbg, readp);
10233 : : else
10234 [ - + ]: 12 : off = read_4ubyte_unaligned_inc (dbg, readp);
10235 : 6 : printf ("%*s#include offset 0x%" PRIx64 "\n",
10236 : : level, "", off);
10237 : : break;
10238 : :
10239 : 0 : case DW_MACRO_define_sup:
10240 : 0 : get_uleb128 (u128, readp, readendp);
10241 [ # # ]: 0 : if (readp + offset_len > readendp)
10242 : 0 : goto invalid_data;
10243 : 0 : printf ("%*s#define ", level, "");
10244 : 0 : readp = print_form_data (dbg, DW_FORM_strp_sup,
10245 : : readp, readendp, offset_len,
10246 : : str_offsets_base);
10247 : 0 : printf (", line %u (sup)\n", u128);
10248 : : break;
10249 : :
10250 : 0 : case DW_MACRO_undef_sup:
10251 : 0 : get_uleb128 (u128, readp, readendp);
10252 [ # # ]: 0 : if (readp + offset_len > readendp)
10253 : 0 : goto invalid_data;
10254 : 0 : printf ("%*s#undef ", level, "");
10255 : 0 : readp = print_form_data (dbg, DW_FORM_strp_sup,
10256 : : readp, readendp, offset_len,
10257 : : str_offsets_base);
10258 : 0 : printf (", line %u (sup)\n", u128);
10259 : : break;
10260 : :
10261 : 0 : case DW_MACRO_import_sup:
10262 [ # # ]: 0 : if (readp + offset_len > readendp)
10263 : 0 : goto invalid_data;
10264 [ # # ]: 0 : if (offset_len == 8)
10265 [ # # ]: 0 : off = read_8ubyte_unaligned_inc (dbg, readp);
10266 : : else
10267 [ # # ]: 0 : off = read_4ubyte_unaligned_inc (dbg, readp);
10268 : : // XXX Needs support for reading from supplementary object file.
10269 : 0 : printf ("%*s#include offset 0x%" PRIx64 " (sup)\n",
10270 : : level, "", off);
10271 : : break;
10272 : :
10273 : 0 : case DW_MACRO_define_strx:
10274 : 0 : get_uleb128 (u128, readp, readendp);
10275 [ # # ]: 0 : if (readp + offset_len > readendp)
10276 : 0 : goto invalid_data;
10277 : 0 : printf ("%*s#define ", level, "");
10278 : 0 : readp = print_form_data (dbg, DW_FORM_strx,
10279 : : readp, readendp, offset_len,
10280 : : str_offsets_base);
10281 : 0 : printf (", line %u (strx)\n", u128);
10282 : : break;
10283 : :
10284 : 0 : case DW_MACRO_undef_strx:
10285 : 0 : get_uleb128 (u128, readp, readendp);
10286 [ # # ]: 0 : if (readp + offset_len > readendp)
10287 : 0 : goto invalid_data;
10288 : 0 : printf ("%*s#undef ", level, "");
10289 : 0 : readp = print_form_data (dbg, DW_FORM_strx,
10290 : : readp, readendp, offset_len,
10291 : : str_offsets_base);
10292 : 0 : printf (", line %u (strx)\n", u128);
10293 : : break;
10294 : :
10295 : : default:
10296 : 0 : printf ("%*svendor opcode 0x%" PRIx8, level, "", opcode);
10297 [ # # ]: 0 : if (opcode < DW_MACRO_lo_user
10298 : : || opcode > DW_MACRO_lo_user
10299 [ # # ]: 0 : || vendor[opcode - DW_MACRO_lo_user] == NULL)
10300 : 0 : goto invalid_data;
10301 : :
10302 : 0 : const unsigned char *op_desc;
10303 : 0 : op_desc = vendor[opcode - DW_MACRO_lo_user];
10304 : :
10305 : : // Just skip the arguments, we cannot really interpret them,
10306 : : // but print as much as we can.
10307 : 0 : unsigned int args = *op_desc++;
10308 [ # # ]: 0 : while (args > 0 && readp < readendp)
10309 : : {
10310 : 0 : unsigned int form = *op_desc++;
10311 : 0 : readp = print_form_data (dbg, form, readp, readendp,
10312 : : offset_len, str_offsets_base);
10313 : 0 : args--;
10314 [ # # ]: 0 : if (args > 0)
10315 [ # # ]: 0 : printf (", ");
10316 : : }
10317 [ # # ]: 0 : putchar_unlocked ('\n');
10318 : : }
10319 : :
10320 [ - + ]: 727 : if (readp + 1 > readendp)
10321 : 0 : goto invalid_data;
10322 : 727 : opcode = *readp++;
10323 [ + + ]: 727 : if (opcode == 0)
10324 [ - + + + ]: 743 : putchar_unlocked ('\n');
10325 : : }
10326 : : }
10327 : : }
10328 : :
10329 : :
10330 : : /* Callback for printing global names. */
10331 : : static int
10332 : 34 : print_pubnames (Dwarf *dbg __attribute__ ((unused)), Dwarf_Global *global,
10333 : : void *arg)
10334 : : {
10335 : 34 : int *np = (int *) arg;
10336 : :
10337 : 68 : printf (_(" [%5d] DIE offset: %6" PRId64
10338 : : ", CU DIE offset: %6" PRId64 ", name: %s\n"),
10339 : 34 : (*np)++, global->die_offset, global->cu_offset, global->name);
10340 : :
10341 : 34 : return 0;
10342 : : }
10343 : :
10344 : :
10345 : : /* Print the known exported symbols in the DWARF section '.debug_pubnames'. */
10346 : : static void
10347 : 8 : print_debug_pubnames_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
10348 : : Ebl *ebl,
10349 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
10350 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
10351 : : {
10352 : 8 : printf (_("\nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
10353 : : elf_ndxscn (scn), section_name (ebl, shdr),
10354 : 8 : (uint64_t) shdr->sh_offset);
10355 : :
10356 : 8 : int n = 0;
10357 : 8 : (void) dwarf_getpubnames (dbg, print_pubnames, &n, 0);
10358 : 8 : }
10359 : :
10360 : : /* Print the content of the DWARF string section '.debug_str'. */
10361 : : static void
10362 : 45 : print_debug_str_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
10363 : : Ebl *ebl,
10364 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
10365 : : Elf_Scn *scn, GElf_Shdr *shdr,
10366 : : Dwarf *dbg __attribute__ ((unused)))
10367 : : {
10368 : 45 : Elf_Data *data = elf_rawdata (scn, NULL);
10369 [ + - ]: 45 : const size_t sh_size = data ? data->d_size : 0;
10370 : :
10371 : : /* Compute floor(log16(shdr->sh_size)). */
10372 : 45 : GElf_Addr tmp = sh_size;
10373 : 45 : int digits = 1;
10374 [ + + ]: 160 : while (tmp >= 16)
10375 : : {
10376 : 115 : ++digits;
10377 : 115 : tmp >>= 4;
10378 : : }
10379 : 45 : digits = MAX (4, digits);
10380 : :
10381 : 45 : printf (_("\nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"
10382 : : " %*s String\n"),
10383 : : elf_ndxscn (scn),
10384 : 45 : section_name (ebl, shdr), (uint64_t) shdr->sh_offset,
10385 : : /* TRANS: the debugstr| prefix makes the string unique. */
10386 [ + - ]: 45 : digits + 2, sgettext ("debugstr|Offset"));
10387 : :
10388 : 45 : Dwarf_Off offset = 0;
10389 [ + + ]: 74042 : while (offset < sh_size)
10390 : : {
10391 : 73997 : size_t len;
10392 : 73997 : const char *str = (const char *) data->d_buf + offset;
10393 : 73997 : const char *endp = memchr (str, '\0', sh_size - offset);
10394 [ - + ]: 73997 : if (unlikely (endp == NULL))
10395 : : {
10396 : 0 : printf (_(" *** error, missing string terminator\n"));
10397 : : break;
10398 : : }
10399 : :
10400 : 73997 : printf (" [%*" PRIx64 "] \"%s\"\n", digits, (uint64_t) offset, str);
10401 : 73997 : len = endp - str;
10402 : 73997 : offset += len + 1;
10403 : : }
10404 : 45 : }
10405 : :
10406 : : static void
10407 : 4 : print_debug_str_offsets_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
10408 : : Ebl *ebl,
10409 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
10410 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
10411 : : {
10412 : 4 : printf (_("\
10413 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
10414 : : elf_ndxscn (scn), section_name (ebl, shdr),
10415 : 4 : (uint64_t) shdr->sh_offset);
10416 : :
10417 [ + - ]: 4 : if (shdr->sh_size == 0)
10418 : : return;
10419 : :
10420 : : /* We like to get the section from libdw to make sure they are relocated. */
10421 : 8 : Elf_Data *data = (dbg->sectiondata[IDX_debug_str_offsets]
10422 [ - + ]: 4 : ?: elf_rawdata (scn, NULL));
10423 [ - + ]: 4 : if (unlikely (data == NULL))
10424 : : {
10425 : 0 : error (0, 0, _("cannot get .debug_str_offsets section data: %s"),
10426 : : elf_errmsg (-1));
10427 : 0 : return;
10428 : : }
10429 : :
10430 : 4 : size_t idx = 0;
10431 : 4 : sort_listptr (&known_stroffbases, "str_offsets");
10432 : :
10433 : 4 : const unsigned char *start = (const unsigned char *) data->d_buf;
10434 : 4 : const unsigned char *readp = start;
10435 : 4 : const unsigned char *readendp = ((const unsigned char *) data->d_buf
10436 : 4 : + data->d_size);
10437 : :
10438 : 4 : while (readp < readendp)
10439 : : {
10440 : : /* Most string offset tables will have a header. For split
10441 : : dwarf unit GNU DebugFission didn't add one. But they were
10442 : : also only defined for split units (main or skeleton units
10443 : : didn't have indirect strings). So if we don't have a
10444 : : DW_AT_str_offsets_base at all and this is offset zero, then
10445 : : just start printing offsets immediately, if this is a .dwo
10446 : : section. */
10447 : 4 : Dwarf_Off off = (Dwarf_Off) (readp
10448 : 4 : - (const unsigned char *) data->d_buf);
10449 : :
10450 : 4 : printf ("Table at offset %" PRIx64 " ", off);
10451 : :
10452 [ - + ]: 4 : struct listptr *listptr = get_listptr (&known_stroffbases, idx++);
10453 : 4 : const unsigned char *next_unitp = readendp;
10454 : 0 : uint8_t offset_size;
10455 : 0 : bool has_header;
10456 [ # # ]: 0 : if (listptr == NULL)
10457 : : {
10458 : : /* This can happen for .dwo files. There is only an header
10459 : : in the case this is a version 5 split DWARF file. */
10460 : 4 : Dwarf_CU *cu;
10461 : 4 : uint8_t unit_type;
10462 [ - + ]: 4 : if (dwarf_get_units (dbg, NULL, &cu, NULL, &unit_type,
10463 : : NULL, NULL) != 0)
10464 : : {
10465 : 0 : error (0, 0, "Warning: Cannot find any DWARF unit.");
10466 : : /* Just guess some values. */
10467 : 0 : has_header = false;
10468 : 0 : offset_size = 4;
10469 : : }
10470 [ + - ]: 4 : else if (off == 0
10471 : 4 : && (unit_type == DW_UT_split_type
10472 [ + - ]: 4 : || unit_type == DW_UT_split_compile))
10473 : : {
10474 : 4 : has_header = cu->version > 4;
10475 : 4 : offset_size = cu->offset_size;
10476 : : }
10477 : : else
10478 : : {
10479 : 0 : error (0, 0,
10480 : : "Warning: No CU references .debug_str_offsets after %"
10481 : : PRIx64, off);
10482 : 0 : has_header = cu->version > 4;
10483 : 0 : offset_size = cu->offset_size;
10484 : : }
10485 : 4 : printf ("\n");
10486 : : }
10487 : : else
10488 : : {
10489 : : /* This must be DWARF5, since GNU DebugFission didn't define
10490 : : DW_AT_str_offsets_base. */
10491 : 0 : has_header = true;
10492 : :
10493 : 0 : Dwarf_Die cudie;
10494 [ # # ]: 0 : if (dwarf_cu_die (listptr->cu, &cudie,
10495 : : NULL, NULL, NULL, NULL,
10496 : : NULL, NULL) == NULL)
10497 : 0 : printf ("Unknown CU (%s):\n", dwarf_errmsg (-1));
10498 : : else
10499 : 0 : printf ("for CU [%6" PRIx64 "]:\n", dwarf_dieoffset (&cudie));
10500 : : }
10501 : :
10502 [ + + ]: 4 : if (has_header)
10503 : : {
10504 : 2 : uint64_t unit_length;
10505 : 2 : uint16_t version;
10506 : 2 : uint16_t padding;
10507 : :
10508 [ - + ]: 2 : unit_length = read_4ubyte_unaligned_inc (dbg, readp);
10509 [ - + ]: 2 : if (unlikely (unit_length == 0xffffffff))
10510 : : {
10511 [ # # ]: 0 : if (unlikely (readp > readendp - 8))
10512 : : {
10513 : 0 : invalid_data:
10514 : 0 : error (0, 0, "Invalid data");
10515 : 0 : return;
10516 : : }
10517 [ # # ]: 0 : unit_length = read_8ubyte_unaligned_inc (dbg, readp);
10518 : 0 : offset_size = 8;
10519 : : }
10520 : : else
10521 : : offset_size = 4;
10522 : :
10523 : 2 : printf ("\n");
10524 : 2 : printf (_(" Length: %8" PRIu64 "\n"),
10525 : : unit_length);
10526 : 2 : printf (_(" Offset size: %8" PRIu8 "\n"),
10527 : : offset_size);
10528 : :
10529 : : /* We need at least 2-bytes (version) + 2-bytes (padding) =
10530 : : 4 bytes to complete the header. And this unit cannot go
10531 : : beyond the section data. */
10532 [ + - ]: 2 : if (readp > readendp - 4
10533 [ + - ]: 2 : || unit_length < 4
10534 [ - + ]: 2 : || unit_length > (uint64_t) (readendp - readp))
10535 : 0 : goto invalid_data;
10536 : :
10537 : 2 : next_unitp = readp + unit_length;
10538 : :
10539 [ - + ]: 2 : version = read_2ubyte_unaligned_inc (dbg, readp);
10540 : 2 : printf (_(" DWARF version: %8" PRIu16 "\n"), version);
10541 : :
10542 [ - + ]: 2 : if (version != 5)
10543 : : {
10544 : 0 : error (0, 0, _("Unknown version"));
10545 : 0 : goto next_unit;
10546 : : }
10547 : :
10548 [ - + ]: 2 : padding = read_2ubyte_unaligned_inc (dbg, readp);
10549 : 2 : printf (_(" Padding: %8" PRIx16 "\n"), padding);
10550 : :
10551 [ - + ]: 2 : if (listptr != NULL
10552 [ # # ]: 0 : && listptr->offset != (Dwarf_Off) (readp - start))
10553 : : {
10554 : 0 : error (0, 0, "String offsets index doesn't start after header");
10555 : 0 : goto next_unit;
10556 : : }
10557 : :
10558 : 2 : printf ("\n");
10559 : : }
10560 : :
10561 : 4 : int digits = 1;
10562 : 4 : size_t offsets = (next_unitp - readp) / offset_size;
10563 [ + + ]: 8 : while (offsets >= 10)
10564 : : {
10565 : 4 : ++digits;
10566 : 4 : offsets /= 10;
10567 : : }
10568 : :
10569 : 4 : unsigned int uidx = 0;
10570 : 4 : size_t index_offset = readp - (const unsigned char *) data->d_buf;
10571 : 4 : printf (" Offsets start at 0x%zx:\n", index_offset);
10572 : 64 : while (readp <= next_unitp - offset_size)
10573 : : {
10574 : 60 : Dwarf_Word offset;
10575 [ + - ]: 60 : if (offset_size == 4)
10576 [ - + ]: 120 : offset = read_4ubyte_unaligned_inc (dbg, readp);
10577 : : else
10578 [ # # ]: 0 : offset = read_8ubyte_unaligned_inc (dbg, readp);
10579 : 60 : const char *str = dwarf_getstring (dbg, offset, NULL);
10580 [ + + ]: 64 : printf (" [%*u] [%*" PRIx64 "] \"%s\"\n",
10581 [ - + ]: 60 : digits, uidx++, (int) offset_size * 2, offset, str ?: "???");
10582 : : }
10583 : 4 : printf ("\n");
10584 : :
10585 [ + - ]: 4 : if (readp != next_unitp)
10586 [ + + ]: 8 : error (0, 0, "extra %zd bytes at end of unit",
10587 : 0 : (size_t) (next_unitp - readp));
10588 : :
10589 : 4 : next_unit:
10590 : : readp = next_unitp;
10591 : : }
10592 : : }
10593 : :
10594 : :
10595 : : /* Print the content of the call frame search table section
10596 : : '.eh_frame_hdr'. */
10597 : : static void
10598 : 40 : print_debug_frame_hdr_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
10599 : : Ebl *ebl __attribute__ ((unused)),
10600 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
10601 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
10602 : : {
10603 : 40 : printf (_("\
10604 : : \nCall frame search table section [%2zu] '.eh_frame_hdr':\n"),
10605 : : elf_ndxscn (scn));
10606 : :
10607 : 40 : Elf_Data *data = elf_rawdata (scn, NULL);
10608 : :
10609 [ - + ]: 40 : if (unlikely (data == NULL))
10610 : : {
10611 : 0 : error (0, 0, _("cannot get %s content: %s"),
10612 : : ".eh_frame_hdr", elf_errmsg (-1));
10613 : 0 : return;
10614 : : }
10615 : :
10616 : 40 : const unsigned char *readp = data->d_buf;
10617 : 40 : const unsigned char *const dataend = ((unsigned char *) data->d_buf
10618 : 40 : + data->d_size);
10619 : :
10620 [ - + ]: 40 : if (unlikely (readp + 4 > dataend))
10621 : : {
10622 : 0 : invalid_data:
10623 : 0 : error (0, 0, _("invalid data"));
10624 : 0 : return;
10625 : : }
10626 : :
10627 : 40 : unsigned int version = *readp++;
10628 : 40 : unsigned int eh_frame_ptr_enc = *readp++;
10629 : 40 : unsigned int fde_count_enc = *readp++;
10630 : 40 : unsigned int table_enc = *readp++;
10631 : :
10632 : 40 : printf (" version: %u\n"
10633 : : " eh_frame_ptr_enc: %#x ",
10634 : : version, eh_frame_ptr_enc);
10635 : 40 : print_encoding_base ("", eh_frame_ptr_enc);
10636 : 40 : printf (" fde_count_enc: %#x ", fde_count_enc);
10637 : 40 : print_encoding_base ("", fde_count_enc);
10638 : 40 : printf (" table_enc: %#x ", table_enc);
10639 : 40 : print_encoding_base ("", table_enc);
10640 : :
10641 : 40 : uint64_t eh_frame_ptr = 0;
10642 [ + - ]: 40 : if (eh_frame_ptr_enc != DW_EH_PE_omit)
10643 : : {
10644 : 40 : readp = read_encoded (eh_frame_ptr_enc, readp, dataend, &eh_frame_ptr,
10645 : : dbg);
10646 [ - + ]: 40 : if (unlikely (readp == NULL))
10647 : 0 : goto invalid_data;
10648 : :
10649 : 40 : printf (" eh_frame_ptr: %#" PRIx64, eh_frame_ptr);
10650 [ + - ]: 40 : if ((eh_frame_ptr_enc & 0x70) == DW_EH_PE_pcrel)
10651 : 80 : printf (" (offset: %#" PRIx64 ")",
10652 : : /* +4 because of the 4 byte header of the section. */
10653 : 40 : (uint64_t) shdr->sh_offset + 4 + eh_frame_ptr);
10654 : :
10655 [ - + ]: 40 : putchar_unlocked ('\n');
10656 : : }
10657 : :
10658 : 40 : uint64_t fde_count = 0;
10659 [ + - ]: 40 : if (fde_count_enc != DW_EH_PE_omit)
10660 : : {
10661 : 40 : readp = read_encoded (fde_count_enc, readp, dataend, &fde_count, dbg);
10662 [ - + ]: 40 : if (unlikely (readp == NULL))
10663 : 0 : goto invalid_data;
10664 : :
10665 : 40 : printf (" fde_count: %" PRIu64 "\n", fde_count);
10666 : : }
10667 : :
10668 [ + - + - ]: 40 : if (fde_count == 0 || table_enc == DW_EH_PE_omit)
10669 : : return;
10670 : :
10671 : 40 : puts (" Table:");
10672 : :
10673 : : /* Optimize for the most common case. */
10674 [ + - ]: 40 : if (table_enc == (DW_EH_PE_datarel | DW_EH_PE_sdata4))
10675 [ + + ]: 9209 : while (fde_count > 0 && readp + 8 <= dataend)
10676 : : {
10677 [ - + ]: 9169 : int32_t initial_location = read_4sbyte_unaligned_inc (dbg, readp);
10678 : 9169 : uint64_t initial_offset = ((uint64_t) shdr->sh_offset
10679 : 9169 : + (int64_t) initial_location);
10680 [ - + ]: 9169 : int32_t address = read_4sbyte_unaligned_inc (dbg, readp);
10681 : : // XXX Possibly print symbol name or section offset for initial_offset
10682 [ + - ]: 9209 : printf (" %#" PRIx32 " (offset: %#6" PRIx64 ") -> %#" PRIx32
10683 : : " fde=[%6" PRIx64 "]\n",
10684 : : initial_location, initial_offset,
10685 : 9169 : address, address - (eh_frame_ptr + 4));
10686 : : }
10687 : : else
10688 : 40 : while (0 && readp < dataend)
10689 : : {
10690 : :
10691 : 40 : }
10692 : : }
10693 : :
10694 : :
10695 : : /* Print the content of the exception handling table section
10696 : : '.eh_frame_hdr'. */
10697 : : static void
10698 : 0 : print_debug_exception_table (Dwfl_Module *dwflmod __attribute__ ((unused)),
10699 : : Ebl *ebl __attribute__ ((unused)),
10700 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
10701 : : Elf_Scn *scn,
10702 : : GElf_Shdr *shdr __attribute__ ((unused)),
10703 : : Dwarf *dbg __attribute__ ((unused)))
10704 : : {
10705 : 0 : printf (_("\
10706 : : \nException handling table section [%2zu] '.gcc_except_table':\n"),
10707 : : elf_ndxscn (scn));
10708 : :
10709 : 0 : Elf_Data *data = elf_rawdata (scn, NULL);
10710 : :
10711 [ # # ]: 0 : if (unlikely (data == NULL))
10712 : : {
10713 : 0 : error (0, 0, _("cannot get %s content: %s"),
10714 : : ".gcc_except_table", elf_errmsg (-1));
10715 : 0 : return;
10716 : : }
10717 : :
10718 : 0 : const unsigned char *readp = data->d_buf;
10719 : 0 : const unsigned char *const dataend = readp + data->d_size;
10720 : :
10721 [ # # ]: 0 : if (unlikely (readp + 1 > dataend))
10722 : : {
10723 : 0 : invalid_data:
10724 : 0 : error (0, 0, _("invalid data"));
10725 : 0 : return;
10726 : : }
10727 : 0 : unsigned int lpstart_encoding = *readp++;
10728 : 0 : printf (_(" LPStart encoding: %#x "), lpstart_encoding);
10729 : 0 : print_encoding_base ("", lpstart_encoding);
10730 [ # # ]: 0 : if (lpstart_encoding != DW_EH_PE_omit)
10731 : : {
10732 : 0 : uint64_t lpstart;
10733 : 0 : readp = read_encoded (lpstart_encoding, readp, dataend, &lpstart, dbg);
10734 : 0 : printf (" LPStart: %#" PRIx64 "\n", lpstart);
10735 : : }
10736 : :
10737 [ # # ]: 0 : if (unlikely (readp + 1 > dataend))
10738 : 0 : goto invalid_data;
10739 : 0 : unsigned int ttype_encoding = *readp++;
10740 : 0 : printf (_(" TType encoding: %#x "), ttype_encoding);
10741 : 0 : print_encoding_base ("", ttype_encoding);
10742 : 0 : const unsigned char *ttype_base = NULL;
10743 [ # # ]: 0 : if (ttype_encoding != DW_EH_PE_omit)
10744 : : {
10745 : 0 : unsigned int ttype_base_offset;
10746 : 0 : get_uleb128 (ttype_base_offset, readp, dataend);
10747 : 0 : printf (" TType base offset: %#x\n", ttype_base_offset);
10748 [ # # ]: 0 : if ((size_t) (dataend - readp) > ttype_base_offset)
10749 : 0 : ttype_base = readp + ttype_base_offset;
10750 : : }
10751 : :
10752 [ # # ]: 0 : if (unlikely (readp + 1 > dataend))
10753 : 0 : goto invalid_data;
10754 : 0 : unsigned int call_site_encoding = *readp++;
10755 : 0 : printf (_(" Call site encoding: %#x "), call_site_encoding);
10756 : 0 : print_encoding_base ("", call_site_encoding);
10757 : 0 : unsigned int call_site_table_len;
10758 : 0 : get_uleb128 (call_site_table_len, readp, dataend);
10759 : :
10760 : 0 : const unsigned char *const action_table = readp + call_site_table_len;
10761 [ # # ]: 0 : if (unlikely (action_table > dataend))
10762 : 0 : goto invalid_data;
10763 : : unsigned int u = 0;
10764 : : unsigned int max_action = 0;
10765 [ # # ]: 0 : while (readp < action_table)
10766 : : {
10767 [ # # ]: 0 : if (u == 0)
10768 : 0 : puts (_("\n Call site table:"));
10769 : :
10770 : 0 : uint64_t call_site_start;
10771 : 0 : readp = read_encoded (call_site_encoding, readp, dataend,
10772 : : &call_site_start, dbg);
10773 : 0 : uint64_t call_site_length;
10774 : 0 : readp = read_encoded (call_site_encoding, readp, dataend,
10775 : : &call_site_length, dbg);
10776 : 0 : uint64_t landing_pad;
10777 : 0 : readp = read_encoded (call_site_encoding, readp, dataend,
10778 : : &landing_pad, dbg);
10779 : 0 : unsigned int action;
10780 : 0 : get_uleb128 (action, readp, dataend);
10781 : 0 : max_action = MAX (action, max_action);
10782 : 0 : printf (_(" [%4u] Call site start: %#" PRIx64 "\n"
10783 : : " Call site length: %" PRIu64 "\n"
10784 : : " Landing pad: %#" PRIx64 "\n"
10785 : : " Action: %u\n"),
10786 : : u++, call_site_start, call_site_length, landing_pad, action);
10787 : : }
10788 [ # # ]: 0 : if (readp != action_table)
10789 : 0 : goto invalid_data;
10790 : :
10791 : 0 : unsigned int max_ar_filter = 0;
10792 [ # # ]: 0 : if (max_action > 0)
10793 : : {
10794 : 0 : puts ("\n Action table:");
10795 : :
10796 : 0 : size_t maxdata = (size_t) (dataend - action_table);
10797 [ # # # # ]: 0 : if (max_action > maxdata || maxdata - max_action < 1)
10798 : : {
10799 : 0 : invalid_action_table:
10800 : 0 : fputs (_(" <INVALID DATA>\n"), stdout);
10801 : 0 : return;
10802 : : }
10803 : :
10804 : 0 : const unsigned char *const action_table_end
10805 : 0 : = action_table + max_action + 1;
10806 : :
10807 : 0 : u = 0;
10808 : 0 : do
10809 : : {
10810 : 0 : int ar_filter;
10811 : 0 : get_sleb128 (ar_filter, readp, action_table_end);
10812 [ # # ]: 0 : if (ar_filter > 0 && (unsigned int) ar_filter > max_ar_filter)
10813 : 0 : max_ar_filter = ar_filter;
10814 : 0 : int ar_disp;
10815 [ # # ]: 0 : if (readp >= action_table_end)
10816 : 0 : goto invalid_action_table;
10817 : 0 : get_sleb128 (ar_disp, readp, action_table_end);
10818 : :
10819 : 0 : printf (" [%4u] ar_filter: % d\n"
10820 : : " ar_disp: % -5d",
10821 : : u, ar_filter, ar_disp);
10822 [ # # ]: 0 : if (abs (ar_disp) & 1)
10823 : 0 : printf (" -> [%4u]\n", u + (ar_disp + 1) / 2);
10824 [ # # ]: 0 : else if (ar_disp != 0)
10825 : 0 : puts (" -> ???");
10826 : : else
10827 [ # # ]: 0 : putchar_unlocked ('\n');
10828 : 0 : ++u;
10829 : : }
10830 [ # # ]: 0 : while (readp < action_table_end);
10831 : : }
10832 : :
10833 [ # # ]: 0 : if (max_ar_filter > 0 && ttype_base != NULL)
10834 : : {
10835 : 0 : unsigned char dsize;
10836 : 0 : puts ("\n TType table:");
10837 : :
10838 : : // XXX Not *4, size of encoding;
10839 [ # # ]: 0 : switch (ttype_encoding & 7)
10840 : : {
10841 : : case DW_EH_PE_udata2:
10842 : : case DW_EH_PE_sdata2:
10843 : : dsize = 2;
10844 : : break;
10845 : : case DW_EH_PE_udata4:
10846 : : case DW_EH_PE_sdata4:
10847 : : dsize = 4;
10848 : : break;
10849 : : case DW_EH_PE_udata8:
10850 : : case DW_EH_PE_sdata8:
10851 : : dsize = 8;
10852 : : break;
10853 : 0 : default:
10854 : 0 : dsize = 0;
10855 : 0 : error (1, 0, _("invalid TType encoding"));
10856 : : }
10857 : :
10858 : 0 : if (max_ar_filter
10859 [ # # ]: 0 : > (size_t) (ttype_base - (const unsigned char *) data->d_buf) / dsize)
10860 : 0 : goto invalid_data;
10861 : :
10862 : 0 : readp = ttype_base - max_ar_filter * dsize;
10863 : 0 : do
10864 : : {
10865 : 0 : uint64_t ttype;
10866 : 0 : readp = read_encoded (ttype_encoding, readp, ttype_base, &ttype,
10867 : : dbg);
10868 : 0 : printf (" [%4u] %#" PRIx64 "\n", max_ar_filter--, ttype);
10869 : : }
10870 [ # # ]: 0 : while (readp < ttype_base);
10871 : : }
10872 : : }
10873 : :
10874 : : /* Print the content of the '.gdb_index' section.
10875 : : http://sourceware.org/gdb/current/onlinedocs/gdb/Index-Section-Format.html
10876 : : */
10877 : : static void
10878 : 2 : print_gdb_index_section (Dwfl_Module *dwflmod, Ebl *ebl,
10879 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
10880 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
10881 : : {
10882 : 2 : printf (_("\nGDB section [%2zu] '%s' at offset %#" PRIx64
10883 : : " contains %" PRId64 " bytes :\n"),
10884 : : elf_ndxscn (scn), section_name (ebl, shdr),
10885 : 2 : (uint64_t) shdr->sh_offset, (uint64_t) shdr->sh_size);
10886 : :
10887 : 2 : Elf_Data *data = elf_rawdata (scn, NULL);
10888 : :
10889 [ - + ]: 2 : if (unlikely (data == NULL))
10890 : : {
10891 : 0 : error (0, 0, _("cannot get %s content: %s"),
10892 : : ".gdb_index", elf_errmsg (-1));
10893 : 0 : return;
10894 : : }
10895 : :
10896 : : // .gdb_index is always in little endian.
10897 : 2 : Dwarf dummy_dbg = { .other_byte_order = MY_ELFDATA != ELFDATA2LSB };
10898 : 2 : dbg = &dummy_dbg;
10899 : :
10900 : 2 : const unsigned char *readp = data->d_buf;
10901 : 2 : const unsigned char *const dataend = readp + data->d_size;
10902 : :
10903 [ - + ]: 2 : if (unlikely (readp + 4 > dataend))
10904 : : {
10905 : 0 : invalid_data:
10906 : 0 : error (0, 0, _("invalid data"));
10907 : 0 : return;
10908 : : }
10909 : :
10910 : 2 : int32_t vers = read_4ubyte_unaligned (dbg, readp);
10911 : 2 : printf (_(" Version: %" PRId32 "\n"), vers);
10912 : :
10913 : : // The only difference between version 4 and version 5 is the
10914 : : // hash used for generating the table. Version 6 contains symbols
10915 : : // for inlined functions, older versions didn't. Version 7 adds
10916 : : // symbol kinds. Version 8 just indicates that it correctly includes
10917 : : // TUs for symbols.
10918 [ - + ]: 2 : if (vers < 4 || vers > 8)
10919 : : {
10920 : 0 : printf (_(" unknown version, cannot parse section\n"));
10921 : 0 : return;
10922 : : }
10923 : :
10924 : 2 : readp += 4;
10925 [ - + ]: 2 : if (unlikely (readp + 4 > dataend))
10926 : 0 : goto invalid_data;
10927 : :
10928 : 2 : uint32_t cu_off = read_4ubyte_unaligned (dbg, readp);
10929 : 2 : printf (_(" CU offset: %#" PRIx32 "\n"), cu_off);
10930 : :
10931 : 2 : readp += 4;
10932 [ - + ]: 2 : if (unlikely (readp + 4 > dataend))
10933 : 0 : goto invalid_data;
10934 : :
10935 : 2 : uint32_t tu_off = read_4ubyte_unaligned (dbg, readp);
10936 : 2 : printf (_(" TU offset: %#" PRIx32 "\n"), tu_off);
10937 : :
10938 : 2 : readp += 4;
10939 [ - + ]: 2 : if (unlikely (readp + 4 > dataend))
10940 : 0 : goto invalid_data;
10941 : :
10942 : 2 : uint32_t addr_off = read_4ubyte_unaligned (dbg, readp);
10943 : 2 : printf (_(" address offset: %#" PRIx32 "\n"), addr_off);
10944 : :
10945 : 2 : readp += 4;
10946 [ - + ]: 2 : if (unlikely (readp + 4 > dataend))
10947 : 0 : goto invalid_data;
10948 : :
10949 : 2 : uint32_t sym_off = read_4ubyte_unaligned (dbg, readp);
10950 : 2 : printf (_(" symbol offset: %#" PRIx32 "\n"), sym_off);
10951 : :
10952 : 2 : readp += 4;
10953 [ - + ]: 2 : if (unlikely (readp + 4 > dataend))
10954 : 0 : goto invalid_data;
10955 : :
10956 : 2 : uint32_t const_off = read_4ubyte_unaligned (dbg, readp);
10957 : 2 : printf (_(" constant offset: %#" PRIx32 "\n"), const_off);
10958 : :
10959 [ - + ]: 2 : if (unlikely ((size_t) (dataend - (const unsigned char *) data->d_buf)
10960 : : < const_off))
10961 : 0 : goto invalid_data;
10962 : :
10963 : 2 : readp = data->d_buf + cu_off;
10964 : :
10965 : 2 : const unsigned char *nextp = data->d_buf + tu_off;
10966 [ - + ]: 2 : if (tu_off >= data->d_size)
10967 : 0 : goto invalid_data;
10968 : :
10969 : 2 : size_t cu_nr = (nextp - readp) / 16;
10970 : :
10971 : 2 : printf (_("\n CU list at offset %#" PRIx32
10972 : : " contains %zu entries:\n"),
10973 : : cu_off, cu_nr);
10974 : :
10975 : 2 : size_t n = 0;
10976 [ + - + + ]: 6 : while (dataend - readp >= 16 && n < cu_nr)
10977 : : {
10978 : 4 : uint64_t off = read_8ubyte_unaligned (dbg, readp);
10979 : 4 : readp += 8;
10980 : :
10981 : 4 : uint64_t len = read_8ubyte_unaligned (dbg, readp);
10982 : 4 : readp += 8;
10983 : :
10984 : 4 : printf (" [%4zu] start: %0#8" PRIx64
10985 : : ", length: %5" PRIu64 "\n", n, off, len);
10986 : 4 : n++;
10987 : : }
10988 : :
10989 : 2 : readp = data->d_buf + tu_off;
10990 : 2 : nextp = data->d_buf + addr_off;
10991 [ - + ]: 2 : if (addr_off >= data->d_size)
10992 : 0 : goto invalid_data;
10993 : :
10994 : 2 : size_t tu_nr = (nextp - readp) / 24;
10995 : :
10996 : 2 : printf (_("\n TU list at offset %#" PRIx32
10997 : : " contains %zu entries:\n"),
10998 : : tu_off, tu_nr);
10999 : :
11000 : 2 : n = 0;
11001 [ + - + + ]: 4 : while (dataend - readp >= 24 && n < tu_nr)
11002 : : {
11003 : 2 : uint64_t off = read_8ubyte_unaligned (dbg, readp);
11004 : 2 : readp += 8;
11005 : :
11006 : 2 : uint64_t type = read_8ubyte_unaligned (dbg, readp);
11007 : 2 : readp += 8;
11008 : :
11009 : 2 : uint64_t sig = read_8ubyte_unaligned (dbg, readp);
11010 : 2 : readp += 8;
11011 : :
11012 : 2 : printf (" [%4zu] CU offset: %5" PRId64
11013 : : ", type offset: %5" PRId64
11014 : : ", signature: %0#8" PRIx64 "\n", n, off, type, sig);
11015 : 2 : n++;
11016 : : }
11017 : :
11018 : 2 : readp = data->d_buf + addr_off;
11019 : 2 : nextp = data->d_buf + sym_off;
11020 [ - + ]: 2 : if (sym_off >= data->d_size)
11021 : 0 : goto invalid_data;
11022 : :
11023 : 2 : size_t addr_nr = (nextp - readp) / 20;
11024 : :
11025 : 2 : printf (_("\n Address list at offset %#" PRIx32
11026 : : " contains %zu entries:\n"),
11027 : : addr_off, addr_nr);
11028 : :
11029 : 2 : n = 0;
11030 [ + - + + ]: 6 : while (dataend - readp >= 20 && n < addr_nr)
11031 : : {
11032 : 4 : uint64_t low = read_8ubyte_unaligned (dbg, readp);
11033 : 4 : readp += 8;
11034 : :
11035 : 4 : uint64_t high = read_8ubyte_unaligned (dbg, readp);
11036 : 4 : readp += 8;
11037 : :
11038 : 4 : uint32_t idx = read_4ubyte_unaligned (dbg, readp);
11039 : 4 : readp += 4;
11040 : :
11041 : 4 : printf (" [%4zu] ", n);
11042 : 4 : print_dwarf_addr (dwflmod, 8, low, low);
11043 : 4 : printf ("..");
11044 : 4 : print_dwarf_addr (dwflmod, 8, high - 1, high);
11045 : 4 : printf (", CU index: %5" PRId32 "\n", idx);
11046 : 4 : n++;
11047 : : }
11048 : :
11049 : 2 : const unsigned char *const_start = data->d_buf + const_off;
11050 [ - + ]: 2 : if (const_off >= data->d_size)
11051 : 0 : goto invalid_data;
11052 : :
11053 : 2 : readp = data->d_buf + sym_off;
11054 : 2 : nextp = const_start;
11055 : 2 : size_t sym_nr = (nextp - readp) / 8;
11056 : :
11057 : 2 : printf (_("\n Symbol table at offset %#" PRIx32
11058 : : " contains %zu slots:\n"),
11059 : : addr_off, sym_nr);
11060 : :
11061 : 2 : n = 0;
11062 [ + - + + ]: 2050 : while (dataend - readp >= 8 && n < sym_nr)
11063 : : {
11064 [ + + ]: 2048 : uint32_t name = read_4ubyte_unaligned (dbg, readp);
11065 : 2048 : readp += 4;
11066 : :
11067 : 2048 : uint32_t vector = read_4ubyte_unaligned (dbg, readp);
11068 : 2048 : readp += 4;
11069 : :
11070 [ + + ]: 2048 : if (name != 0 || vector != 0)
11071 : : {
11072 : 14 : const unsigned char *sym = const_start + name;
11073 [ + - - + ]: 14 : if (unlikely ((size_t) (dataend - const_start) < name
11074 : : || memchr (sym, '\0', dataend - sym) == NULL))
11075 : 0 : goto invalid_data;
11076 : :
11077 : 14 : printf (" [%4zu] symbol: %s, CUs: ", n, sym);
11078 : :
11079 : 14 : const unsigned char *readcus = const_start + vector;
11080 [ - + ]: 14 : if (unlikely ((size_t) (dataend - const_start) < vector))
11081 : 0 : goto invalid_data;
11082 : 14 : uint32_t cus = read_4ubyte_unaligned (dbg, readcus);
11083 : 14 : while (cus--)
11084 : : {
11085 : 16 : uint32_t cu_kind, cu, kind;
11086 : 16 : bool is_static;
11087 : 16 : readcus += 4;
11088 [ - + ]: 16 : if (unlikely (readcus + 4 > dataend))
11089 : 0 : goto invalid_data;
11090 [ + + ]: 16 : cu_kind = read_4ubyte_unaligned (dbg, readcus);
11091 : 16 : cu = cu_kind & ((1 << 24) - 1);
11092 : 16 : kind = (cu_kind >> 28) & 7;
11093 : 16 : is_static = cu_kind & (1U << 31);
11094 [ + + ]: 16 : if (cu > cu_nr - 1)
11095 : 2 : printf ("%" PRId32 "T", cu - (uint32_t) cu_nr);
11096 : : else
11097 : 14 : printf ("%" PRId32, cu);
11098 [ + + ]: 16 : if (kind != 0)
11099 : : {
11100 : 8 : printf (" (");
11101 [ + + + - : 8 : switch (kind)
- ]
11102 : : {
11103 : : case 1:
11104 : 3 : printf ("type");
11105 : : break;
11106 : : case 2:
11107 : 2 : printf ("var");
11108 : : break;
11109 : : case 3:
11110 : 3 : printf ("func");
11111 : : break;
11112 : : case 4:
11113 : 0 : printf ("other");
11114 : : break;
11115 : : default:
11116 : 0 : printf ("unknown-0x%" PRIx32, kind);
11117 : : break;
11118 : : }
11119 [ + + ]: 8 : printf (":%c)", (is_static ? 'S' : 'G'));
11120 : : }
11121 [ + + ]: 16 : if (cus > 0)
11122 [ + + ]: 32 : printf (", ");
11123 : : }
11124 : 14 : printf ("\n");
11125 : : }
11126 : 2048 : n++;
11127 : : }
11128 : : }
11129 : :
11130 : : /* Returns true and sets split DWARF CU id if there is a split compile
11131 : : unit in the given Dwarf, and no non-split units are found (before it). */
11132 : : static bool
11133 : 153 : is_split_dwarf (Dwarf *dbg, uint64_t *id, Dwarf_CU **split_cu)
11134 : : {
11135 : 153 : Dwarf_CU *cu = NULL;
11136 [ + - ]: 153 : while (dwarf_get_units (dbg, cu, &cu, NULL, NULL, NULL, NULL) == 0)
11137 : : {
11138 : 153 : uint8_t unit_type;
11139 [ + - ]: 153 : if (dwarf_cu_info (cu, NULL, &unit_type, NULL, NULL,
11140 : : id, NULL, NULL) != 0)
11141 : 153 : return false;
11142 : :
11143 [ + + ]: 153 : if (unit_type != DW_UT_split_compile && unit_type != DW_UT_split_type)
11144 : : return false;
11145 : :
11146 : : /* We really only care about the split compile unit, the types
11147 : : should be fine and self sufficient. Also they don't have an
11148 : : id that we can match with a skeleton unit. */
11149 [ + - ]: 9 : if (unit_type == DW_UT_split_compile)
11150 : : {
11151 : 9 : *split_cu = cu;
11152 : 9 : return true;
11153 : : }
11154 : : }
11155 : :
11156 : : return false;
11157 : : }
11158 : :
11159 : : /* Check that there is one and only one Dwfl_Module, return in arg. */
11160 : : static int
11161 : 9 : getone_dwflmod (Dwfl_Module *dwflmod,
11162 : : void **userdata __attribute__ ((unused)),
11163 : : const char *name __attribute__ ((unused)),
11164 : : Dwarf_Addr base __attribute__ ((unused)),
11165 : : void *arg)
11166 : : {
11167 : 9 : Dwfl_Module **m = (Dwfl_Module **) arg;
11168 [ + - ]: 9 : if (*m != NULL)
11169 : : return DWARF_CB_ABORT;
11170 : 9 : *m = dwflmod;
11171 : 9 : return DWARF_CB_OK;
11172 : : }
11173 : :
11174 : : static void
11175 : 209 : print_debug (Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr)
11176 : : {
11177 : : /* Used for skeleton file, if necessary for split DWARF. */
11178 : 209 : Dwfl *skel_dwfl = NULL;
11179 : 209 : Dwfl_Module *skel_mod = NULL;
11180 : 209 : char *skel_name = NULL;
11181 : 209 : Dwarf *split_dbg = NULL;
11182 : 209 : Dwarf_CU *split_cu = NULL;
11183 : :
11184 : : /* Before we start the real work get a debug context descriptor. */
11185 : 209 : Dwarf_Addr dwbias;
11186 : 209 : Dwarf *dbg = dwfl_module_getdwarf (dwflmod, &dwbias);
11187 : 209 : Dwarf dummy_dbg =
11188 : : {
11189 : 209 : .elf = ebl->elf,
11190 : 209 : .other_byte_order = MY_ELFDATA != ehdr->e_ident[EI_DATA]
11191 : : };
11192 [ + + ]: 209 : if (dbg == NULL)
11193 : : {
11194 [ - + ]: 56 : if ((print_debug_sections & ~(section_exception|section_frame)) != 0)
11195 : 0 : error (0, 0, _("cannot get debug context descriptor: %s"),
11196 : : dwfl_errmsg (-1));
11197 : : dbg = &dummy_dbg;
11198 : : }
11199 : : else
11200 : : {
11201 : : /* If we are asked about a split dwarf (.dwo) file, use the user
11202 : : provided, or find the corresponding skeleton file. If we got
11203 : : a skeleton file, replace the given dwflmod and dbg, with one
11204 : : derived from the skeleton file to provide enough context. */
11205 : 153 : uint64_t split_id;
11206 [ + + ]: 153 : if (is_split_dwarf (dbg, &split_id, &split_cu))
11207 : : {
11208 [ + - ]: 9 : if (dwarf_skeleton != NULL)
11209 : 9 : skel_name = strdup (dwarf_skeleton);
11210 : : else
11211 : : {
11212 : : /* Replace file.dwo with file.o and see if that matches. */
11213 : 0 : const char *fname;
11214 : 0 : dwfl_module_info (dwflmod, NULL, NULL, NULL, NULL, NULL,
11215 : : &fname, NULL);
11216 [ # # ]: 0 : if (fname != NULL)
11217 : : {
11218 : 0 : size_t flen = strlen (fname);
11219 [ # # # # ]: 0 : if (flen > 4 && strcmp (".dwo", fname + flen - 4) == 0)
11220 : : {
11221 : 0 : skel_name = strdup (fname);
11222 [ # # ]: 0 : if (skel_name != NULL)
11223 : : {
11224 : 0 : skel_name[flen - 3] = 'o';
11225 : 0 : skel_name[flen - 2] = '\0';
11226 : : }
11227 : : }
11228 : : }
11229 : : }
11230 : :
11231 [ + - ]: 9 : if (skel_name != NULL)
11232 : : {
11233 : 9 : int skel_fd = open (skel_name, O_RDONLY);
11234 [ - + ]: 9 : if (skel_fd == -1)
11235 : 0 : fprintf (stderr, "Warning: Couldn't open DWARF skeleton file"
11236 : : " '%s'\n", skel_name);
11237 : : else
11238 : 9 : skel_dwfl = create_dwfl (skel_fd, skel_name);
11239 : :
11240 [ + - ]: 9 : if (skel_dwfl != NULL)
11241 : : {
11242 [ - + ]: 9 : if (dwfl_getmodules (skel_dwfl, &getone_dwflmod,
11243 : : &skel_mod, 0) != 0)
11244 : : {
11245 : 0 : fprintf (stderr, "Warning: Bad DWARF skeleton,"
11246 : : " multiple modules '%s'\n", skel_name);
11247 : 0 : dwfl_end (skel_dwfl);
11248 : 0 : skel_mod = NULL;
11249 : : }
11250 : : }
11251 [ # # ]: 0 : else if (skel_fd != -1)
11252 : 0 : fprintf (stderr, "Warning: Couldn't create skeleton dwfl for"
11253 : : " '%s': %s\n", skel_name, dwfl_errmsg (-1));
11254 : :
11255 [ + - ]: 9 : if (skel_mod != NULL)
11256 : : {
11257 : 9 : Dwarf *skel_dbg = dwfl_module_getdwarf (skel_mod, &dwbias);
11258 [ + - ]: 9 : if (skel_dbg != NULL)
11259 : : {
11260 : : /* First check the skeleton CU DIE, only fetch
11261 : : the split DIE if we know the id matches to
11262 : : not unnecessary search for any split DIEs we
11263 : : don't need. */
11264 : 9 : Dwarf_CU *cu = NULL;
11265 [ + - ]: 13 : while (dwarf_get_units (skel_dbg, cu, &cu,
11266 : : NULL, NULL, NULL, NULL) == 0)
11267 : : {
11268 : 13 : uint8_t unit_type;
11269 : 13 : uint64_t skel_id;
11270 [ + - ]: 13 : if (dwarf_cu_info (cu, NULL, &unit_type, NULL, NULL,
11271 : : &skel_id, NULL, NULL) == 0
11272 [ + - ]: 13 : && unit_type == DW_UT_skeleton
11273 [ + + ]: 13 : && split_id == skel_id)
11274 : : {
11275 : 9 : Dwarf_Die subdie;
11276 [ + - ]: 9 : if (dwarf_cu_info (cu, NULL, NULL, NULL,
11277 : : &subdie,
11278 : : NULL, NULL, NULL) == 0
11279 [ + - ]: 9 : && dwarf_tag (&subdie) != DW_TAG_invalid)
11280 : : {
11281 : 9 : split_dbg = dwarf_cu_getdwarf (subdie.cu);
11282 [ - + ]: 9 : if (split_dbg == NULL)
11283 : 0 : fprintf (stderr,
11284 : : "Warning: Couldn't get split_dbg:"
11285 : : " %s\n", dwarf_errmsg (-1));
11286 : 9 : break;
11287 : : }
11288 : : else
11289 : : {
11290 : : /* Everything matches up, but not
11291 : : according to libdw. Which means
11292 : : the user knew better. So...
11293 : : Terrible hack... We can never
11294 : : destroy the underlying dwfl
11295 : : because it would free the wrong
11296 : : Dwarfs... So we leak memory...*/
11297 [ # # ]: 0 : if (cu->split == NULL
11298 [ # # ]: 0 : && dwarf_skeleton != NULL)
11299 : : {
11300 : 0 : do_not_close_dwfl = true;
11301 : 0 : __libdw_link_skel_split (cu, split_cu);
11302 : 0 : split_dbg = dwarf_cu_getdwarf (split_cu);
11303 : 0 : break;
11304 : : }
11305 : : else
11306 : 0 : fprintf (stderr, "Warning: Couldn't get"
11307 : : " skeleton subdie: %s\n",
11308 : : dwarf_errmsg (-1));
11309 : : }
11310 : : }
11311 : : }
11312 [ - + ]: 9 : if (split_dbg == NULL)
11313 : 9 : fprintf (stderr, "Warning: '%s' didn't contain a skeleton for split id %" PRIx64 "\n", skel_name, split_id);
11314 : : }
11315 : : else
11316 : 0 : fprintf (stderr, "Warning: Couldn't get skeleton DWARF:"
11317 : : " %s\n", dwfl_errmsg (-1));
11318 : : }
11319 : : }
11320 : :
11321 [ + - ]: 9 : if (split_dbg != NULL)
11322 : : {
11323 : 9 : dbg = split_dbg;
11324 : 9 : dwflmod = skel_mod;
11325 : : }
11326 [ # # ]: 0 : else if (skel_name == NULL)
11327 : 0 : fprintf (stderr,
11328 : : "Warning: split DWARF file, but no skeleton found.\n");
11329 : : }
11330 [ - + ]: 144 : else if (dwarf_skeleton != NULL)
11331 : 153 : fprintf (stderr, "Warning: DWARF skeleton given,"
11332 : : " but not a split DWARF file\n");
11333 : : }
11334 : :
11335 : : /* Get the section header string table index. */
11336 : 209 : size_t shstrndx;
11337 [ - + ]: 209 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
11338 : 0 : error (EXIT_FAILURE, 0,
11339 : 0 : _("cannot get section header string table index"));
11340 : :
11341 : : /* If the .debug_info section is listed as implicitly required then
11342 : : we must make sure to handle it before handling any other debug
11343 : : section. Various other sections depend on the CU DIEs being
11344 : : scanned (silently) first. */
11345 : 209 : bool implicit_info = (implicit_debug_sections & section_info) != 0;
11346 : 209 : bool explicit_info = (print_debug_sections & section_info) != 0;
11347 [ + + ]: 209 : if (implicit_info)
11348 : : {
11349 : : Elf_Scn *scn = NULL;
11350 [ + - ]: 1454 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
11351 : : {
11352 : 1454 : GElf_Shdr shdr_mem;
11353 : 1454 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
11354 : :
11355 [ + - + + ]: 1454 : if (shdr != NULL && shdr->sh_type == SHT_PROGBITS)
11356 : : {
11357 : 1176 : const char *name = elf_strptr (ebl->elf, shstrndx,
11358 : 588 : shdr->sh_name);
11359 [ - + ]: 588 : if (name == NULL)
11360 : 0 : continue;
11361 : :
11362 [ + + ]: 588 : if (strcmp (name, ".debug_info") == 0
11363 [ + + ]: 526 : || strcmp (name, ".debug_info.dwo") == 0
11364 [ + + ]: 517 : || strcmp (name, ".zdebug_info") == 0
11365 [ + - ]: 511 : || strcmp (name, ".zdebug_info.dwo") == 0
11366 [ - + ]: 511 : || strcmp (name, ".gnu.debuglto_.debug_info") == 0)
11367 : : {
11368 : 77 : print_debug_info_section (dwflmod, ebl, ehdr,
11369 : : scn, shdr, dbg);
11370 : 77 : break;
11371 : : }
11372 : : }
11373 : : }
11374 : 77 : print_debug_sections &= ~section_info;
11375 : 77 : implicit_debug_sections &= ~section_info;
11376 : : }
11377 : :
11378 : : /* Look through all the sections for the debugging sections to print. */
11379 : : Elf_Scn *scn = NULL;
11380 [ + + ]: 6345 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
11381 : : {
11382 : 6136 : GElf_Shdr shdr_mem;
11383 : 6136 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
11384 : :
11385 [ + - + + ]: 6136 : if (shdr != NULL && shdr->sh_type == SHT_PROGBITS)
11386 : : {
11387 : 2725 : static const struct
11388 : : {
11389 : : const char *name;
11390 : : enum section_e bitmask;
11391 : : void (*fp) (Dwfl_Module *, Ebl *,
11392 : : GElf_Ehdr *, Elf_Scn *, GElf_Shdr *, Dwarf *);
11393 : : } debug_sections[] =
11394 : : {
11395 : : #define NEW_SECTION(name) \
11396 : : { ".debug_" #name, section_##name, print_debug_##name##_section }
11397 : : NEW_SECTION (abbrev),
11398 : : NEW_SECTION (addr),
11399 : : NEW_SECTION (aranges),
11400 : : NEW_SECTION (frame),
11401 : : NEW_SECTION (info),
11402 : : NEW_SECTION (types),
11403 : : NEW_SECTION (line),
11404 : : NEW_SECTION (loc),
11405 : : /* loclists is loc for DWARF5. */
11406 : : { ".debug_loclists", section_loc,
11407 : : print_debug_loclists_section },
11408 : : NEW_SECTION (pubnames),
11409 : : NEW_SECTION (str),
11410 : : /* A DWARF5 specialised debug string section. */
11411 : : { ".debug_line_str", section_str,
11412 : : print_debug_str_section },
11413 : : /* DWARF5 string offsets table. */
11414 : : { ".debug_str_offsets", section_str,
11415 : : print_debug_str_offsets_section },
11416 : : NEW_SECTION (macinfo),
11417 : : NEW_SECTION (macro),
11418 : : NEW_SECTION (ranges),
11419 : : /* rnglists is ranges for DWARF5. */
11420 : : { ".debug_rnglists", section_ranges,
11421 : : print_debug_rnglists_section },
11422 : : { ".eh_frame", section_frame | section_exception,
11423 : : print_debug_frame_section },
11424 : : { ".eh_frame_hdr", section_frame | section_exception,
11425 : : print_debug_frame_hdr_section },
11426 : : { ".gcc_except_table", section_frame | section_exception,
11427 : : print_debug_exception_table },
11428 : : { ".gdb_index", section_gdb_index, print_gdb_index_section }
11429 : : };
11430 : 2725 : const int ndebug_sections = (sizeof (debug_sections)
11431 : : / sizeof (debug_sections[0]));
11432 : 5450 : const char *name = elf_strptr (ebl->elf, shstrndx,
11433 : 2725 : shdr->sh_name);
11434 [ - + ]: 2725 : if (name == NULL)
11435 : 0 : continue;
11436 : :
11437 : : int n;
11438 [ + + ]: 43472 : for (n = 0; n < ndebug_sections; ++n)
11439 : : {
11440 : 42001 : size_t dbglen = strlen (debug_sections[n].name);
11441 : 42001 : size_t scnlen = strlen (name);
11442 [ + + ]: 42001 : if ((strncmp (name, debug_sections[n].name, dbglen) == 0
11443 [ + + ]: 1283 : && (dbglen == scnlen
11444 [ + + ]: 165 : || (scnlen == dbglen + 4
11445 [ + + ]: 148 : && strstr (name, ".dwo") == name + dbglen)))
11446 [ + + + + ]: 40829 : || (name[0] == '.' && name[1] == 'z'
11447 [ + - ]: 566 : && debug_sections[n].name[1] == 'd'
11448 [ + + ]: 566 : && strncmp (&name[2], &debug_sections[n].name[1],
11449 : : dbglen - 1) == 0
11450 [ - + ]: 82 : && (scnlen == dbglen + 1
11451 [ # # ]: 0 : || (scnlen == dbglen + 5
11452 [ # # ]: 0 : && strstr (name, ".dwo") == name + dbglen + 1)))
11453 [ + + ]: 40747 : || (scnlen > 14 /* .gnu.debuglto_ prefix. */
11454 [ - + ]: 3383 : && strncmp (name, ".gnu.debuglto_", 14) == 0
11455 [ # # ]: 0 : && strcmp (&name[14], debug_sections[n].name) == 0)
11456 : : )
11457 : : {
11458 : 1254 : if ((print_debug_sections | implicit_debug_sections)
11459 [ + + ]: 1254 : & debug_sections[n].bitmask)
11460 : 429 : debug_sections[n].fp (dwflmod, ebl, ehdr, scn, shdr, dbg);
11461 : : break;
11462 : : }
11463 : : }
11464 : : }
11465 : : }
11466 : :
11467 : 209 : dwfl_end (skel_dwfl);
11468 : 209 : free (skel_name);
11469 : :
11470 : : /* Turn implicit and/or explicit back on in case we go over another file. */
11471 [ + + ]: 209 : if (implicit_info)
11472 : 77 : implicit_debug_sections |= section_info;
11473 [ + + ]: 209 : if (explicit_info)
11474 : 62 : print_debug_sections |= section_info;
11475 : :
11476 : 209 : reset_listptr (&known_locsptr);
11477 : 209 : reset_listptr (&known_loclistsptr);
11478 : 209 : reset_listptr (&known_rangelistptr);
11479 : 209 : reset_listptr (&known_rnglistptr);
11480 : 209 : reset_listptr (&known_addrbases);
11481 : 209 : reset_listptr (&known_stroffbases);
11482 : 209 : }
11483 : :
11484 : :
11485 : : #define ITEM_INDENT 4
11486 : : #define WRAP_COLUMN 75
11487 : :
11488 : : /* Print "NAME: FORMAT", wrapping when output text would make the line
11489 : : exceed WRAP_COLUMN. Unpadded numbers look better for the core items
11490 : : but this function is also used for registers which should be printed
11491 : : aligned. Fortunately registers output uses fixed fields width (such
11492 : : as %11d) for the alignment.
11493 : :
11494 : : Line breaks should not depend on the particular values although that
11495 : : may happen in some cases of the core items. */
11496 : :
11497 : : static unsigned int
11498 : : __attribute__ ((format (printf, 6, 7)))
11499 : 821 : print_core_item (unsigned int colno, char sep, unsigned int wrap,
11500 : : size_t name_width, const char *name, const char *format, ...)
11501 : : {
11502 : 821 : size_t len = strlen (name);
11503 : 821 : if (name_width < len)
11504 : : name_width = len;
11505 : :
11506 : 821 : char *out;
11507 : 821 : va_list ap;
11508 : 821 : va_start (ap, format);
11509 : 821 : int out_len = vasprintf (&out, format, ap);
11510 : 821 : va_end (ap);
11511 [ - + ]: 821 : if (out_len == -1)
11512 : 0 : error (EXIT_FAILURE, 0, _("memory exhausted"));
11513 : :
11514 : 821 : size_t n = name_width + sizeof ": " - 1 + out_len;
11515 : :
11516 [ + + ]: 821 : if (colno == 0)
11517 : : {
11518 : 51 : printf ("%*s", ITEM_INDENT, "");
11519 : 51 : colno = ITEM_INDENT + n;
11520 : : }
11521 [ + + ]: 770 : else if (colno + 2 + n < wrap)
11522 : : {
11523 : 468 : printf ("%c ", sep);
11524 : 468 : colno += 2 + n;
11525 : : }
11526 : : else
11527 : : {
11528 : 302 : printf ("\n%*s", ITEM_INDENT, "");
11529 : 302 : colno = ITEM_INDENT + n;
11530 : : }
11531 : :
11532 : 821 : printf ("%s: %*s%s", name, (int) (name_width - len), "", out);
11533 : :
11534 : 821 : free (out);
11535 : :
11536 : 821 : return colno;
11537 : : }
11538 : :
11539 : : static const void *
11540 : 931 : convert (Elf *core, Elf_Type type, uint_fast16_t count,
11541 : : void *value, const void *data, size_t size)
11542 : : {
11543 : 1862 : Elf_Data valuedata =
11544 : : {
11545 : : .d_type = type,
11546 : : .d_buf = value,
11547 [ + + ]: 931 : .d_size = size ?: gelf_fsize (core, type, count, EV_CURRENT),
11548 : : .d_version = EV_CURRENT,
11549 : : };
11550 : 931 : Elf_Data indata =
11551 : : {
11552 : : .d_type = type,
11553 : : .d_buf = (void *) data,
11554 : : .d_size = valuedata.d_size,
11555 : : .d_version = EV_CURRENT,
11556 : : };
11557 : :
11558 : 931 : Elf_Data *d = (gelf_getclass (core) == ELFCLASS32
11559 [ + + ]: 931 : ? elf32_xlatetom : elf64_xlatetom)
11560 : 931 : (&valuedata, &indata, elf_getident (core, NULL)[EI_DATA]);
11561 [ - + ]: 931 : if (d == NULL)
11562 : 0 : error (EXIT_FAILURE, 0,
11563 : 0 : _("cannot convert core note data: %s"), elf_errmsg (-1));
11564 : :
11565 : 931 : return data + indata.d_size;
11566 : : }
11567 : :
11568 : : typedef uint8_t GElf_Byte;
11569 : :
11570 : : static unsigned int
11571 : 400 : handle_core_item (Elf *core, const Ebl_Core_Item *item, const void *desc,
11572 : : unsigned int colno, size_t *repeated_size)
11573 : : {
11574 [ + + ]: 400 : uint_fast16_t count = item->count ?: 1;
11575 : : /* Ebl_Core_Item count is always a small number.
11576 : : Make sure the backend didn't put in some large bogus value. */
11577 [ - + ]: 62 : assert (count < 128);
11578 : :
11579 : : #define TYPES \
11580 : : DO_TYPE (BYTE, Byte, "0x%.2" PRIx8, "%" PRId8); \
11581 : : DO_TYPE (HALF, Half, "0x%.4" PRIx16, "%" PRId16); \
11582 : : DO_TYPE (WORD, Word, "0x%.8" PRIx32, "%" PRId32); \
11583 : : DO_TYPE (SWORD, Sword, "%" PRId32, "%" PRId32); \
11584 : : DO_TYPE (XWORD, Xword, "0x%.16" PRIx64, "%" PRId64); \
11585 : : DO_TYPE (SXWORD, Sxword, "%" PRId64, "%" PRId64)
11586 : :
11587 : : #define DO_TYPE(NAME, Name, hex, dec) GElf_##Name Name
11588 : 400 : typedef union { TYPES; } value_t;
11589 : 400 : void *data = alloca (count * sizeof (value_t));
11590 : : #undef DO_TYPE
11591 : :
11592 : : #define DO_TYPE(NAME, Name, hex, dec) \
11593 : : GElf_##Name *value_##Name __attribute__((unused)) = data
11594 : 400 : TYPES;
11595 : : #undef DO_TYPE
11596 : :
11597 : 400 : size_t size = gelf_fsize (core, item->type, count, EV_CURRENT);
11598 : 400 : size_t convsize = size;
11599 [ + + ]: 400 : if (repeated_size != NULL)
11600 : : {
11601 [ + - - + ]: 1 : if (*repeated_size > size && (item->format == 'b' || item->format == 'B'))
11602 : : {
11603 : 0 : data = alloca (*repeated_size);
11604 : 0 : count *= *repeated_size / size;
11605 : 0 : convsize = count * size;
11606 : 0 : *repeated_size -= convsize;
11607 : : }
11608 [ + - - + ]: 1 : else if (item->count != 0 || item->format != '\n')
11609 : 0 : *repeated_size -= size;
11610 : : }
11611 : :
11612 : 400 : convert (core, item->type, count, data, desc + item->offset, convsize);
11613 : :
11614 : 400 : Elf_Type type = item->type;
11615 [ + + ]: 400 : if (type == ELF_T_ADDR)
11616 [ + - ]: 1 : type = gelf_getclass (core) == ELFCLASS32 ? ELF_T_WORD : ELF_T_XWORD;
11617 : :
11618 [ + + + + : 400 : switch (item->format)
+ + + -
+ ]
11619 : : {
11620 : 193 : case 'd':
11621 [ - + ]: 193 : assert (count == 1);
11622 [ + + + + : 193 : switch (type)
- + - ]
11623 : : {
11624 : : #define DO_TYPE(NAME, Name, hex, dec) \
11625 : : case ELF_T_##NAME: \
11626 : : colno = print_core_item (colno, ',', WRAP_COLUMN, \
11627 : : 0, item->name, dec, value_##Name[0]); \
11628 : : break
11629 : 193 : TYPES;
11630 : : #undef DO_TYPE
11631 : 0 : default:
11632 : 0 : abort ();
11633 : : }
11634 : : break;
11635 : :
11636 : 111 : case 'x':
11637 [ - + ]: 111 : assert (count == 1);
11638 [ - - + - : 111 : switch (type)
+ - - ]
11639 : : {
11640 : : #define DO_TYPE(NAME, Name, hex, dec) \
11641 : : case ELF_T_##NAME: \
11642 : : colno = print_core_item (colno, ',', WRAP_COLUMN, \
11643 : : 0, item->name, hex, value_##Name[0]); \
11644 : : break
11645 : 111 : TYPES;
11646 : : #undef DO_TYPE
11647 : 0 : default:
11648 : 0 : abort ();
11649 : : }
11650 : : break;
11651 : :
11652 : 22 : case 'b':
11653 : : case 'B':
11654 [ - + ]: 22 : assert (size % sizeof (unsigned int) == 0);
11655 : 22 : unsigned int nbits = count * size * 8;
11656 : 22 : unsigned int pop = 0;
11657 [ + + ]: 56 : for (const unsigned int *i = data; (void *) i < data + count * size; ++i)
11658 : 34 : pop += __builtin_popcount (*i);
11659 : 22 : bool negate = pop > nbits / 2;
11660 : 22 : const unsigned int bias = item->format == 'b';
11661 : :
11662 : 22 : {
11663 [ - + ]: 22 : char printed[(negate ? nbits - pop : pop) * 16 + 1];
11664 : 22 : char *p = printed;
11665 : 22 : *p = '\0';
11666 : :
11667 : 22 : if (BYTE_ORDER != LITTLE_ENDIAN && size > sizeof (unsigned int))
11668 : : {
11669 : : assert (size == sizeof (unsigned int) * 2);
11670 : : for (unsigned int *i = data;
11671 : : (void *) i < data + count * size; i += 2)
11672 : : {
11673 : : unsigned int w = i[1];
11674 : : i[1] = i[0];
11675 : : i[0] = w;
11676 : : }
11677 : : }
11678 : :
11679 : 22 : unsigned int lastbit = 0;
11680 : 22 : unsigned int run = 0;
11681 : 22 : for (const unsigned int *i = data;
11682 [ + + ]: 56 : (void *) i < data + count * size; ++i)
11683 : : {
11684 : 34 : unsigned int bit = ((void *) i - data) * 8;
11685 [ - + ]: 34 : unsigned int w = negate ? ~*i : *i;
11686 [ - + ]: 34 : while (w != 0)
11687 : : {
11688 : : /* Note that a right shift equal to (or greater than)
11689 : : the number of bits of w is undefined behaviour. In
11690 : : particular when the least significant bit is bit 32
11691 : : (w = 0x8000000) then w >>= n is undefined. So
11692 : : explicitly handle that case separately. */
11693 : 0 : unsigned int n = ffs (w);
11694 [ # # ]: 0 : if (n < sizeof (w) * 8)
11695 : 0 : w >>= n;
11696 : : else
11697 : : w = 0;
11698 : 0 : bit += n;
11699 : :
11700 [ # # # # ]: 0 : if (lastbit != 0 && lastbit + 1 == bit)
11701 : 0 : ++run;
11702 : : else
11703 : : {
11704 [ # # ]: 0 : if (lastbit == 0)
11705 : 0 : p += sprintf (p, "%u", bit - bias);
11706 [ # # ]: 0 : else if (run == 0)
11707 : 0 : p += sprintf (p, ",%u", bit - bias);
11708 : : else
11709 : 0 : p += sprintf (p, "-%u,%u", lastbit - bias, bit - bias);
11710 : : run = 0;
11711 : : }
11712 : :
11713 : : lastbit = bit;
11714 : : }
11715 : : }
11716 [ - + - - ]: 22 : if (lastbit > 0 && run > 0 && lastbit + 1 != nbits)
11717 : 0 : p += sprintf (p, "-%u", lastbit - bias);
11718 : :
11719 [ + - ]: 44 : colno = print_core_item (colno, ',', WRAP_COLUMN, 0, item->name,
11720 : : negate ? "~<%s>" : "<%s>", printed);
11721 : : }
11722 : 22 : break;
11723 : :
11724 : 44 : case 'T':
11725 : : case (char) ('T'|0x80):
11726 [ - + ]: 44 : assert (count == 2);
11727 : 44 : Dwarf_Word sec;
11728 : 44 : Dwarf_Word usec;
11729 [ - - + - : 44 : switch (type)
+ - - ]
11730 : : {
11731 : : #define DO_TYPE(NAME, Name, hex, dec) \
11732 : : case ELF_T_##NAME: \
11733 : : sec = value_##Name[0]; \
11734 : : usec = value_##Name[1]; \
11735 : : break
11736 : 44 : TYPES;
11737 : : #undef DO_TYPE
11738 : 0 : default:
11739 : 0 : abort ();
11740 : : }
11741 [ - + ]: 44 : if (unlikely (item->format == (char) ('T'|0x80)))
11742 : : {
11743 : : /* This is a hack for an ill-considered 64-bit ABI where
11744 : : tv_usec is actually a 32-bit field with 32 bits of padding
11745 : : rounding out struct timeval. We've already converted it as
11746 : : a 64-bit field. For little-endian, this just means the
11747 : : high half is the padding; it's presumably zero, but should
11748 : : be ignored anyway. For big-endian, it means the 32-bit
11749 : : field went into the high half of USEC. */
11750 : 0 : GElf_Ehdr ehdr_mem;
11751 : 0 : GElf_Ehdr *ehdr = gelf_getehdr (core, &ehdr_mem);
11752 [ # # ]: 0 : if (likely (ehdr->e_ident[EI_DATA] == ELFDATA2MSB))
11753 : 0 : usec >>= 32;
11754 : : else
11755 : 0 : usec &= UINT32_MAX;
11756 : : }
11757 : 44 : colno = print_core_item (colno, ',', WRAP_COLUMN, 0, item->name,
11758 : : "%" PRIu64 ".%.6" PRIu64, sec, usec);
11759 : 44 : break;
11760 : :
11761 : 9 : case 'c':
11762 [ - + ]: 9 : assert (count == 1);
11763 : 9 : colno = print_core_item (colno, ',', WRAP_COLUMN, 0, item->name,
11764 : 9 : "%c", value_Byte[0]);
11765 : 9 : break;
11766 : :
11767 : 18 : case 's':
11768 : 18 : colno = print_core_item (colno, ',', WRAP_COLUMN, 0, item->name,
11769 : : "%.*s", (int) count, value_Byte);
11770 : 18 : break;
11771 : :
11772 : 1 : case '\n':
11773 : : /* This is a list of strings separated by '\n'. */
11774 [ - + ]: 1 : assert (item->count == 0);
11775 [ - + ]: 1 : assert (repeated_size != NULL);
11776 [ - + ]: 1 : assert (item->name == NULL);
11777 [ - + ]: 1 : if (unlikely (item->offset >= *repeated_size))
11778 : : break;
11779 : :
11780 : 1 : const char *s = desc + item->offset;
11781 : 1 : size = *repeated_size - item->offset;
11782 : 1 : *repeated_size = 0;
11783 [ + - ]: 48 : while (size > 0)
11784 : : {
11785 : 48 : const char *eol = memchr (s, '\n', size);
11786 : 48 : int len = size;
11787 [ + + ]: 48 : if (eol != NULL)
11788 : 47 : len = eol - s;
11789 : 48 : printf ("%*s%.*s\n", ITEM_INDENT, "", len, s);
11790 [ + + ]: 48 : if (eol == NULL)
11791 : : break;
11792 : 47 : size -= eol + 1 - s;
11793 : 47 : s = eol + 1;
11794 : : }
11795 : :
11796 : : colno = WRAP_COLUMN;
11797 : : break;
11798 : :
11799 : : case 'h':
11800 : : break;
11801 : :
11802 : 0 : default:
11803 : 0 : error (0, 0, "XXX not handling format '%c' for %s",
11804 : : item->format, item->name);
11805 : : break;
11806 : : }
11807 : :
11808 : : #undef TYPES
11809 : :
11810 : 0 : return colno;
11811 : : }
11812 : :
11813 : :
11814 : : /* Sort items by group, and by layout offset within each group. */
11815 : : static int
11816 : 895 : compare_core_items (const void *a, const void *b)
11817 : : {
11818 : 895 : const Ebl_Core_Item *const *p1 = a;
11819 : 895 : const Ebl_Core_Item *const *p2 = b;
11820 : 895 : const Ebl_Core_Item *item1 = *p1;
11821 : 895 : const Ebl_Core_Item *item2 = *p2;
11822 : :
11823 : 895 : return ((item1->group == item2->group ? 0
11824 [ + + ]: 895 : : strcmp (item1->group, item2->group))
11825 [ - + ]: 895 : ?: (int) item1->offset - (int) item2->offset);
11826 : : }
11827 : :
11828 : : /* Sort item groups by layout offset of the first item in the group. */
11829 : : static int
11830 : 139 : compare_core_item_groups (const void *a, const void *b)
11831 : : {
11832 : 139 : const Ebl_Core_Item *const *const *p1 = a;
11833 : 139 : const Ebl_Core_Item *const *const *p2 = b;
11834 : 139 : const Ebl_Core_Item *const *group1 = *p1;
11835 : 139 : const Ebl_Core_Item *const *group2 = *p2;
11836 : 139 : const Ebl_Core_Item *item1 = *group1;
11837 : 139 : const Ebl_Core_Item *item2 = *group2;
11838 : :
11839 : 139 : return (int) item1->offset - (int) item2->offset;
11840 : : }
11841 : :
11842 : : static unsigned int
11843 : 37 : handle_core_items (Elf *core, const void *desc, size_t descsz,
11844 : : const Ebl_Core_Item *items, size_t nitems)
11845 : : {
11846 [ + + ]: 37 : if (nitems == 0)
11847 : : return 0;
11848 : 34 : unsigned int colno = 0;
11849 : :
11850 : : /* FORMAT '\n' makes sense to be present only as a single item as it
11851 : : processes all the data of a note. FORMATs 'b' and 'B' have a special case
11852 : : if present as a single item but they can be also processed with other
11853 : : items below. */
11854 [ + + + + ]: 34 : if (nitems == 1 && (items[0].format == '\n' || items[0].format == 'b'
11855 [ + - ]: 8 : || items[0].format == 'B'))
11856 : : {
11857 [ - + ]: 1 : assert (items[0].offset == 0);
11858 : 1 : size_t size = descsz;
11859 : 1 : colno = handle_core_item (core, items, desc, colno, &size);
11860 : : /* If SIZE is not zero here there is some remaining data. But we do not
11861 : : know how to process it anyway. */
11862 : 1 : return colno;
11863 : : }
11864 [ + + ]: 424 : for (size_t i = 0; i < nitems; ++i)
11865 [ - + ]: 391 : assert (items[i].format != '\n');
11866 : :
11867 : : /* Sort to collect the groups together. */
11868 : 33 : const Ebl_Core_Item *sorted_items[nitems];
11869 [ + + ]: 424 : for (size_t i = 0; i < nitems; ++i)
11870 : 391 : sorted_items[i] = &items[i];
11871 : 33 : qsort (sorted_items, nitems, sizeof sorted_items[0], &compare_core_items);
11872 : :
11873 : : /* Collect the unique groups and sort them. */
11874 : 33 : const Ebl_Core_Item **groups[nitems];
11875 : 33 : groups[0] = &sorted_items[0];
11876 : 33 : size_t ngroups = 1;
11877 [ + + ]: 391 : for (size_t i = 1; i < nitems; ++i)
11878 [ + + ]: 358 : if (sorted_items[i]->group != sorted_items[i - 1]->group
11879 [ + - ]: 75 : && strcmp (sorted_items[i]->group, sorted_items[i - 1]->group))
11880 : 75 : groups[ngroups++] = &sorted_items[i];
11881 : 33 : qsort (groups, ngroups, sizeof groups[0], &compare_core_item_groups);
11882 : :
11883 : : /* Write out all the groups. */
11884 : 33 : const void *last = desc;
11885 : 35 : do
11886 : : {
11887 [ + + ]: 145 : for (size_t i = 0; i < ngroups; ++i)
11888 : : {
11889 : 110 : for (const Ebl_Core_Item **item = groups[i];
11890 [ + + ]: 509 : (item < &sorted_items[nitems]
11891 [ + + ]: 474 : && ((*item)->group == groups[i][0]->group
11892 [ - + ]: 75 : || !strcmp ((*item)->group, groups[i][0]->group)));
11893 : 399 : ++item)
11894 : 399 : colno = handle_core_item (core, *item, desc, colno, NULL);
11895 : :
11896 : : /* Force a line break at the end of the group. */
11897 : 110 : colno = WRAP_COLUMN;
11898 : : }
11899 : :
11900 [ + + ]: 35 : if (descsz == 0)
11901 : : break;
11902 : :
11903 : : /* This set of items consumed a certain amount of the note's data.
11904 : : If there is more data there, we have another unit of the same size.
11905 : : Loop to print that out too. */
11906 : 20 : const Ebl_Core_Item *item = &items[nitems - 1];
11907 : 40 : size_t eltsz = item->offset + gelf_fsize (core, item->type,
11908 : 20 : item->count ?: 1, EV_CURRENT);
11909 : :
11910 : 20 : int reps = -1;
11911 : 20 : do
11912 : : {
11913 : 20 : ++reps;
11914 : 20 : desc += eltsz;
11915 : 20 : descsz -= eltsz;
11916 : : }
11917 [ + + - + ]: 20 : while (descsz >= eltsz && !memcmp (desc, last, eltsz));
11918 : :
11919 [ + - ]: 20 : if (reps == 1)
11920 : : {
11921 : : /* For just one repeat, print it unabridged twice. */
11922 : 20 : desc -= eltsz;
11923 : 20 : descsz += eltsz;
11924 : : }
11925 [ - + ]: 20 : else if (reps > 1)
11926 : 0 : printf (_("\n%*s... <repeats %u more times> ..."),
11927 : : ITEM_INDENT, "", reps);
11928 : :
11929 : 20 : last = desc;
11930 : : }
11931 [ + + ]: 20 : while (descsz > 0);
11932 : :
11933 : 33 : return colno;
11934 : : }
11935 : :
11936 : : static unsigned int
11937 : 0 : handle_bit_registers (const Ebl_Register_Location *regloc, const void *desc,
11938 : : unsigned int colno)
11939 : : {
11940 : 0 : desc += regloc->offset;
11941 : :
11942 : 0 : abort (); /* XXX */
11943 : : return colno;
11944 : : }
11945 : :
11946 : :
11947 : : static unsigned int
11948 : 162 : handle_core_register (Ebl *ebl, Elf *core, int maxregname,
11949 : : const Ebl_Register_Location *regloc, const void *desc,
11950 : : unsigned int colno)
11951 : : {
11952 [ - + ]: 162 : if (regloc->bits % 8 != 0)
11953 : 0 : return handle_bit_registers (regloc, desc, colno);
11954 : :
11955 : 162 : desc += regloc->offset;
11956 : :
11957 [ + + ]: 586 : for (int reg = regloc->regno; reg < regloc->regno + regloc->count; ++reg)
11958 : : {
11959 : 424 : char name[REGNAMESZ];
11960 : 424 : int bits;
11961 : 424 : int type;
11962 : 424 : register_info (ebl, reg, regloc, name, &bits, &type);
11963 : :
11964 : : #define TYPES \
11965 : : BITS (8, BYTE, "%4" PRId8, "0x%.2" PRIx8); \
11966 : : BITS (16, HALF, "%6" PRId16, "0x%.4" PRIx16); \
11967 : : BITS (32, WORD, "%11" PRId32, " 0x%.8" PRIx32); \
11968 : : BITS (64, XWORD, "%20" PRId64, " 0x%.16" PRIx64)
11969 : :
11970 : : #define BITS(bits, xtype, sfmt, ufmt) \
11971 : : uint##bits##_t b##bits; int##bits##_t b##bits##s
11972 : 424 : union { TYPES; uint64_t b128[2]; } value;
11973 : : #undef BITS
11974 : :
11975 [ + + ]: 424 : switch (type)
11976 : : {
11977 : 336 : case DW_ATE_unsigned:
11978 : : case DW_ATE_signed:
11979 : : case DW_ATE_address:
11980 [ - + + + : 336 : switch (bits)
+ - ]
11981 : : {
11982 : : #define BITS(bits, xtype, sfmt, ufmt) \
11983 : : case bits: \
11984 : : desc = convert (core, ELF_T_##xtype, 1, &value, desc, 0); \
11985 : : if (type == DW_ATE_signed) \
11986 : : colno = print_core_item (colno, ' ', WRAP_COLUMN, \
11987 : : maxregname, name, \
11988 : : sfmt, value.b##bits##s); \
11989 : : else \
11990 : : colno = print_core_item (colno, ' ', WRAP_COLUMN, \
11991 : : maxregname, name, \
11992 : : ufmt, value.b##bits); \
11993 : : break
11994 : :
11995 [ - - - + : 288 : TYPES;
+ + + + ]
11996 : :
11997 : 48 : case 128:
11998 [ - + ]: 48 : assert (type == DW_ATE_unsigned);
11999 : 48 : desc = convert (core, ELF_T_XWORD, 2, &value, desc, 0);
12000 : 48 : int be = elf_getident (core, NULL)[EI_DATA] == ELFDATA2MSB;
12001 : 48 : colno = print_core_item (colno, ' ', WRAP_COLUMN,
12002 : : maxregname, name,
12003 : : "0x%.16" PRIx64 "%.16" PRIx64,
12004 : 48 : value.b128[!be], value.b128[be]);
12005 : 48 : break;
12006 : :
12007 : 0 : default:
12008 : 0 : abort ();
12009 : : #undef BITS
12010 : : }
12011 : : break;
12012 : :
12013 : 88 : default:
12014 : : /* Print each byte in hex, the whole thing in native byte order. */
12015 [ - + ]: 88 : assert (bits % 8 == 0);
12016 : 88 : const uint8_t *bytes = desc;
12017 : 88 : desc += bits / 8;
12018 : 88 : char hex[bits / 4 + 1];
12019 : 88 : hex[bits / 4] = '\0';
12020 : 88 : int incr = 1;
12021 [ + + ]: 88 : if (elf_getident (core, NULL)[EI_DATA] == ELFDATA2LSB)
12022 : : {
12023 : 48 : bytes += bits / 8 - 1;
12024 : 48 : incr = -1;
12025 : : }
12026 : 88 : size_t idx = 0;
12027 [ + + ]: 872 : for (char *h = hex; bits > 0; bits -= 8, idx += incr)
12028 : : {
12029 : 784 : *h++ = "0123456789abcdef"[bytes[idx] >> 4];
12030 : 784 : *h++ = "0123456789abcdef"[bytes[idx] & 0xf];
12031 : : }
12032 : 88 : colno = print_core_item (colno, ' ', WRAP_COLUMN,
12033 : : maxregname, name, "0x%s", hex);
12034 : 88 : break;
12035 : : }
12036 : 424 : desc += regloc->pad;
12037 : :
12038 : : #undef TYPES
12039 : : }
12040 : :
12041 : 162 : return colno;
12042 : : }
12043 : :
12044 : :
12045 : : struct register_info
12046 : : {
12047 : : const Ebl_Register_Location *regloc;
12048 : : const char *set;
12049 : : char name[REGNAMESZ];
12050 : : int regno;
12051 : : int bits;
12052 : : int type;
12053 : : };
12054 : :
12055 : : static int
12056 : 2066 : register_bitpos (const struct register_info *r)
12057 : : {
12058 : 2066 : return (r->regloc->offset * 8
12059 : 2066 : + ((r->regno - r->regloc->regno)
12060 : 2066 : * (r->regloc->bits + r->regloc->pad * 8)));
12061 : : }
12062 : :
12063 : : static int
12064 : 1062 : compare_sets_by_info (const struct register_info *r1,
12065 : : const struct register_info *r2)
12066 : : {
12067 : 1062 : return ((int) r2->bits - (int) r1->bits
12068 [ + + ]: 1062 : ?: register_bitpos (r1) - register_bitpos (r2));
12069 : : }
12070 : :
12071 : : /* Sort registers by set, and by size and layout offset within each set. */
12072 : : static int
12073 : 4595 : compare_registers (const void *a, const void *b)
12074 : : {
12075 : 4595 : const struct register_info *r1 = a;
12076 : 4595 : const struct register_info *r2 = b;
12077 : :
12078 : : /* Unused elements sort last. */
12079 [ + + ]: 4595 : if (r1->regloc == NULL)
12080 : 3309 : return r2->regloc == NULL ? 0 : 1;
12081 [ + + ]: 1286 : if (r2->regloc == NULL)
12082 : : return -1;
12083 : :
12084 [ + + ]: 1121 : return ((r1->set == r2->set ? 0 : strcmp (r1->set, r2->set))
12085 [ - + ]: 1121 : ?: compare_sets_by_info (r1, r2));
12086 : : }
12087 : :
12088 : : /* Sort register sets by layout offset of the first register in the set. */
12089 : : static int
12090 : 20 : compare_register_sets (const void *a, const void *b)
12091 : : {
12092 : 20 : const struct register_info *const *p1 = a;
12093 : 20 : const struct register_info *const *p2 = b;
12094 : 20 : return compare_sets_by_info (*p1, *p2);
12095 : : }
12096 : :
12097 : : static inline bool
12098 : 864 : same_set (const struct register_info *a,
12099 : : const struct register_info *b,
12100 : : const struct register_info *regs,
12101 : : size_t maxnreg)
12102 : : {
12103 [ + - ]: 864 : return (a < ®s[maxnreg] && a->regloc != NULL
12104 [ + - + + ]: 864 : && b < ®s[maxnreg] && b->regloc != NULL
12105 [ + + ]: 846 : && a->bits == b->bits
12106 [ + - + + : 1692 : && (a->set == b->set || !strcmp (a->set, b->set)));
+ - ]
12107 : : }
12108 : :
12109 : : static unsigned int
12110 : 37 : handle_core_registers (Ebl *ebl, Elf *core, const void *desc,
12111 : : const Ebl_Register_Location *reglocs, size_t nregloc)
12112 : : {
12113 [ + + ]: 37 : if (nregloc == 0)
12114 : : return 0;
12115 : :
12116 : 18 : ssize_t maxnreg = ebl_register_info (ebl, 0, NULL, 0, NULL, NULL, NULL, NULL);
12117 [ - + ]: 18 : if (maxnreg <= 0)
12118 : : {
12119 [ # # ]: 0 : for (size_t i = 0; i < nregloc; ++i)
12120 : 0 : if (maxnreg < reglocs[i].regno + reglocs[i].count)
12121 : : maxnreg = reglocs[i].regno + reglocs[i].count;
12122 [ # # ]: 0 : assert (maxnreg > 0);
12123 : : }
12124 : :
12125 : 18 : struct register_info regs[maxnreg];
12126 : 18 : memset (regs, 0, sizeof regs);
12127 : :
12128 : : /* Sort to collect the sets together. */
12129 : 18 : int maxreg = 0;
12130 [ + + ]: 180 : for (size_t i = 0; i < nregloc; ++i)
12131 : 162 : for (int reg = reglocs[i].regno;
12132 [ + + ]: 586 : reg < reglocs[i].regno + reglocs[i].count;
12133 : 424 : ++reg)
12134 : : {
12135 [ - + ]: 424 : assert (reg < maxnreg);
12136 : 424 : if (reg > maxreg)
12137 : : maxreg = reg;
12138 : 424 : struct register_info *info = ®s[reg];
12139 : 424 : info->regloc = ®locs[i];
12140 : 424 : info->regno = reg;
12141 : 424 : info->set = register_info (ebl, reg, ®locs[i],
12142 : 424 : info->name, &info->bits, &info->type);
12143 : : }
12144 : 18 : qsort (regs, maxreg + 1, sizeof regs[0], &compare_registers);
12145 : :
12146 : : /* Collect the unique sets and sort them. */
12147 : 18 : struct register_info *sets[maxreg + 1];
12148 : 18 : sets[0] = ®s[0];
12149 : 18 : size_t nsets = 1;
12150 [ + + ]: 1279 : for (int i = 1; i <= maxreg; ++i)
12151 [ + + ]: 1261 : if (regs[i].regloc != NULL
12152 [ + + ]: 406 : && !same_set (®s[i], ®s[i - 1], regs, maxnreg))
12153 : 16 : sets[nsets++] = ®s[i];
12154 : 18 : qsort (sets, nsets, sizeof sets[0], &compare_register_sets);
12155 : :
12156 : : /* Write out all the sets. */
12157 : 18 : unsigned int colno = 0;
12158 [ + + ]: 52 : for (size_t i = 0; i < nsets; ++i)
12159 : : {
12160 : : /* Find the longest name of a register in this set. */
12161 : 34 : size_t maxname = 0;
12162 : 34 : const struct register_info *end;
12163 [ + + ]: 458 : for (end = sets[i]; same_set (sets[i], end, regs, maxnreg); ++end)
12164 : : {
12165 : 424 : size_t len = strlen (end->name);
12166 : 424 : if (len > maxname)
12167 : : maxname = len;
12168 : : }
12169 : :
12170 : 196 : for (const struct register_info *reg = sets[i];
12171 [ + + ]: 196 : reg < end;
12172 : 162 : reg += reg->regloc->count ?: 1)
12173 [ + - ]: 162 : colno = handle_core_register (ebl, core, maxname,
12174 : : reg->regloc, desc, colno);
12175 : :
12176 : : /* Force a line break at the end of the group. */
12177 : 34 : colno = WRAP_COLUMN;
12178 : : }
12179 : :
12180 : 18 : return colno;
12181 : : }
12182 : :
12183 : : static void
12184 : 9 : handle_auxv_note (Ebl *ebl, Elf *core, GElf_Word descsz, GElf_Off desc_pos)
12185 : : {
12186 : 9 : Elf_Data *data = elf_getdata_rawchunk (core, desc_pos, descsz, ELF_T_AUXV);
12187 [ - + ]: 9 : if (data == NULL)
12188 : 0 : elf_error:
12189 : 0 : error (EXIT_FAILURE, 0,
12190 : 0 : _("cannot convert core note data: %s"), elf_errmsg (-1));
12191 : :
12192 : 9 : const size_t nauxv = descsz / gelf_fsize (core, ELF_T_AUXV, 1, EV_CURRENT);
12193 [ + + ]: 177 : for (size_t i = 0; i < nauxv; ++i)
12194 : : {
12195 : 168 : GElf_auxv_t av_mem;
12196 : 168 : GElf_auxv_t *av = gelf_getauxv (data, i, &av_mem);
12197 [ - + ]: 168 : if (av == NULL)
12198 : 0 : goto elf_error;
12199 : :
12200 : 168 : const char *name;
12201 : 168 : const char *fmt;
12202 [ - + ]: 168 : if (ebl_auxv_info (ebl, av->a_type, &name, &fmt) == 0)
12203 : : {
12204 : : /* Unknown type. */
12205 [ # # ]: 0 : if (av->a_un.a_val == 0)
12206 : 0 : printf (" %" PRIu64 "\n", av->a_type);
12207 : : else
12208 : 0 : printf (" %" PRIu64 ": %#" PRIx64 "\n",
12209 : : av->a_type, av->a_un.a_val);
12210 : : }
12211 : : else
12212 [ + + + - : 168 : switch (fmt[0])
+ - ]
12213 : : {
12214 : 9 : case '\0': /* Normally zero. */
12215 [ + - ]: 9 : if (av->a_un.a_val == 0)
12216 : : {
12217 : 9 : printf (" %s\n", name);
12218 : : break;
12219 : : }
12220 : 74 : FALLTHROUGH;
12221 : : case 'x': /* hex */
12222 : : case 'p': /* address */
12223 : : case 's': /* address of string */
12224 : 74 : printf (" %s: %#" PRIx64 "\n", name, av->a_un.a_val);
12225 : : break;
12226 : 81 : case 'u':
12227 : 81 : printf (" %s: %" PRIu64 "\n", name, av->a_un.a_val);
12228 : : break;
12229 : 0 : case 'd':
12230 : 0 : printf (" %s: %" PRId64 "\n", name, av->a_un.a_val);
12231 : : break;
12232 : :
12233 : 4 : case 'b':
12234 : 4 : printf (" %s: %#" PRIx64 " ", name, av->a_un.a_val);
12235 : 4 : GElf_Xword bit = 1;
12236 : 4 : const char *pfx = "<";
12237 [ + + ]: 110 : for (const char *p = fmt + 1; *p != 0; p = strchr (p, '\0') + 1)
12238 : : {
12239 [ + + ]: 106 : if (av->a_un.a_val & bit)
12240 : : {
12241 : 77 : printf ("%s%s", pfx, p);
12242 : 77 : pfx = " ";
12243 : : }
12244 : 106 : bit <<= 1;
12245 : : }
12246 : 168 : printf (">\n");
12247 : : break;
12248 : :
12249 : 0 : default:
12250 : 0 : abort ();
12251 : : }
12252 : : }
12253 : 9 : }
12254 : :
12255 : : static bool
12256 : 195 : buf_has_data (unsigned char const *ptr, unsigned char const *end, size_t sz)
12257 : : {
12258 [ - + - + ]: 195 : return ptr < end && (size_t) (end - ptr) >= sz;
12259 : : }
12260 : :
12261 : : static bool
12262 : 18 : buf_read_int (Elf *core, unsigned char const **ptrp, unsigned char const *end,
12263 : : int *retp)
12264 : : {
12265 [ + - + - ]: 36 : if (! buf_has_data (*ptrp, end, 4))
12266 : : return false;
12267 : :
12268 : 18 : *ptrp = convert (core, ELF_T_WORD, 1, retp, *ptrp, 4);
12269 : 18 : return true;
12270 : : }
12271 : :
12272 : : static bool
12273 : 177 : buf_read_ulong (Elf *core, unsigned char const **ptrp, unsigned char const *end,
12274 : : uint64_t *retp)
12275 : : {
12276 : 177 : size_t sz = gelf_fsize (core, ELF_T_ADDR, 1, EV_CURRENT);
12277 [ + - + - ]: 354 : if (! buf_has_data (*ptrp, end, sz))
12278 : : return false;
12279 : :
12280 : 177 : union
12281 : : {
12282 : : uint64_t u64;
12283 : : uint32_t u32;
12284 : : } u;
12285 : :
12286 : 177 : *ptrp = convert (core, ELF_T_ADDR, 1, &u, *ptrp, sz);
12287 : :
12288 [ + + ]: 177 : if (sz == 4)
12289 : 93 : *retp = u.u32;
12290 : : else
12291 : 84 : *retp = u.u64;
12292 : : return true;
12293 : : }
12294 : :
12295 : : static void
12296 : 6 : handle_siginfo_note (Elf *core, GElf_Word descsz, GElf_Off desc_pos)
12297 : : {
12298 : 6 : Elf_Data *data = elf_getdata_rawchunk (core, desc_pos, descsz, ELF_T_BYTE);
12299 [ - + ]: 6 : if (data == NULL)
12300 : 0 : error (EXIT_FAILURE, 0,
12301 : 0 : _("cannot convert core note data: %s"), elf_errmsg (-1));
12302 : :
12303 : 6 : unsigned char const *ptr = data->d_buf;
12304 : 6 : unsigned char const *const end = data->d_buf + data->d_size;
12305 : :
12306 : : /* Siginfo head is three ints: signal number, error number, origin
12307 : : code. */
12308 : 6 : int si_signo, si_errno, si_code;
12309 [ - + ]: 6 : if (! buf_read_int (core, &ptr, end, &si_signo)
12310 [ - + ]: 6 : || ! buf_read_int (core, &ptr, end, &si_errno)
12311 [ - + ]: 6 : || ! buf_read_int (core, &ptr, end, &si_code))
12312 : : {
12313 : 0 : fail:
12314 : 0 : printf (" Not enough data in NT_SIGINFO note.\n");
12315 : 0 : return;
12316 : : }
12317 : :
12318 : : /* Next is a pointer-aligned union of structures. On 64-bit
12319 : : machines, that implies a word of padding. */
12320 [ + + ]: 6 : if (gelf_getclass (core) == ELFCLASS64)
12321 : 3 : ptr += 4;
12322 : :
12323 : 6 : printf (" si_signo: %d, si_errno: %d, si_code: %d\n",
12324 : : si_signo, si_errno, si_code);
12325 : :
12326 [ + - ]: 6 : if (si_code > 0)
12327 [ + - ]: 6 : switch (si_signo)
12328 : : {
12329 : 6 : case CORE_SIGILL:
12330 : : case CORE_SIGFPE:
12331 : : case CORE_SIGSEGV:
12332 : : case CORE_SIGBUS:
12333 : : {
12334 : 6 : uint64_t addr;
12335 [ - + ]: 6 : if (! buf_read_ulong (core, &ptr, end, &addr))
12336 : 0 : goto fail;
12337 : 6 : printf (" fault address: %#" PRIx64 "\n", addr);
12338 : 6 : break;
12339 : : }
12340 : : default:
12341 : : ;
12342 : : }
12343 [ # # ]: 0 : else if (si_code == CORE_SI_USER)
12344 : : {
12345 : 0 : int pid, uid;
12346 [ # # ]: 0 : if (! buf_read_int (core, &ptr, end, &pid)
12347 [ # # ]: 0 : || ! buf_read_int (core, &ptr, end, &uid))
12348 : 0 : goto fail;
12349 : 0 : printf (" sender PID: %d, sender UID: %d\n", pid, uid);
12350 : : }
12351 : : }
12352 : :
12353 : : static void
12354 : 6 : handle_file_note (Elf *core, GElf_Word descsz, GElf_Off desc_pos)
12355 : : {
12356 : 6 : Elf_Data *data = elf_getdata_rawchunk (core, desc_pos, descsz, ELF_T_BYTE);
12357 [ - + ]: 6 : if (data == NULL)
12358 : 0 : error (EXIT_FAILURE, 0,
12359 : 0 : _("cannot convert core note data: %s"), elf_errmsg (-1));
12360 : :
12361 : 6 : unsigned char const *ptr = data->d_buf;
12362 : 6 : unsigned char const *const end = data->d_buf + data->d_size;
12363 : :
12364 : 6 : uint64_t count, page_size;
12365 [ - + ]: 6 : if (! buf_read_ulong (core, &ptr, end, &count)
12366 [ - + ]: 6 : || ! buf_read_ulong (core, &ptr, end, &page_size))
12367 : : {
12368 : 0 : fail:
12369 : 0 : printf (" Not enough data in NT_FILE note.\n");
12370 : 0 : return;
12371 : : }
12372 : :
12373 : 6 : size_t addrsize = gelf_fsize (core, ELF_T_ADDR, 1, EV_CURRENT);
12374 : 6 : uint64_t maxcount = (size_t) (end - ptr) / (3 * addrsize);
12375 [ - + ]: 6 : if (count > maxcount)
12376 : 0 : goto fail;
12377 : :
12378 : : /* Where file names are stored. */
12379 : 6 : unsigned char const *const fstart = ptr + 3 * count * addrsize;
12380 : 6 : char const *fptr = (char *) fstart;
12381 : :
12382 : 6 : printf (" %" PRId64 " files:\n", count);
12383 [ + + ]: 59 : for (uint64_t i = 0; i < count; ++i)
12384 : : {
12385 : 53 : uint64_t mstart, mend, moffset;
12386 [ + - ]: 53 : if (! buf_read_ulong (core, &ptr, fstart, &mstart)
12387 [ + - ]: 53 : || ! buf_read_ulong (core, &ptr, fstart, &mend)
12388 [ - + ]: 53 : || ! buf_read_ulong (core, &ptr, fstart, &moffset))
12389 : 0 : goto fail;
12390 : :
12391 : 53 : const char *fnext = memchr (fptr, '\0', (char *) end - fptr);
12392 [ - + ]: 53 : if (fnext == NULL)
12393 : 0 : goto fail;
12394 : :
12395 : 53 : int ct = printf (" %08" PRIx64 "-%08" PRIx64
12396 : : " %08" PRIx64 " %" PRId64,
12397 : : mstart, mend, moffset * page_size, mend - mstart);
12398 [ + - ]: 53 : printf ("%*s%s\n", ct > 50 ? 3 : 53 - ct, "", fptr);
12399 : :
12400 : 53 : fptr = fnext + 1;
12401 : : }
12402 : : }
12403 : :
12404 : : static void
12405 : 38 : handle_core_note (Ebl *ebl, const GElf_Nhdr *nhdr,
12406 : : const char *name, const void *desc)
12407 : : {
12408 : 38 : GElf_Word regs_offset;
12409 : 38 : size_t nregloc;
12410 : 38 : const Ebl_Register_Location *reglocs;
12411 : 38 : size_t nitems;
12412 : 38 : const Ebl_Core_Item *items;
12413 : :
12414 [ + + ]: 38 : if (! ebl_core_note (ebl, nhdr, name, desc,
12415 : : ®s_offset, &nregloc, ®locs, &nitems, &items))
12416 : 1 : return;
12417 : :
12418 : : /* Pass 0 for DESCSZ when there are registers in the note,
12419 : : so that the ITEMS array does not describe the whole thing.
12420 : : For non-register notes, the actual descsz might be a multiple
12421 : : of the unit size, not just exactly the unit size. */
12422 : 37 : unsigned int colno = handle_core_items (ebl->elf, desc,
12423 [ + + ]: 37 : nregloc == 0 ? nhdr->n_descsz : 0,
12424 : : items, nitems);
12425 [ + + ]: 37 : if (colno != 0)
12426 [ - + ]: 34 : putchar_unlocked ('\n');
12427 : :
12428 : 37 : colno = handle_core_registers (ebl, ebl->elf, desc + regs_offset,
12429 : : reglocs, nregloc);
12430 [ + + ]: 37 : if (colno != 0)
12431 [ - + ]: 55 : putchar_unlocked ('\n');
12432 : : }
12433 : :
12434 : : static void
12435 : 158 : handle_notes_data (Ebl *ebl, const GElf_Ehdr *ehdr,
12436 : : GElf_Off start, Elf_Data *data)
12437 : : {
12438 : 158 : fputs_unlocked (_(" Owner Data size Type\n"), stdout);
12439 : :
12440 [ - + ]: 158 : if (data == NULL)
12441 : 0 : goto bad_note;
12442 : :
12443 : : size_t offset = 0;
12444 : : GElf_Nhdr nhdr;
12445 : : size_t name_offset;
12446 : : size_t desc_offset;
12447 [ + + ]: 1380 : while (offset < data->d_size
12448 [ + - ]: 1222 : && (offset = gelf_getnote (data, offset,
12449 : : &nhdr, &name_offset, &desc_offset)) > 0)
12450 : : {
12451 [ + - ]: 1222 : const char *name = nhdr.n_namesz == 0 ? "" : data->d_buf + name_offset;
12452 : 1222 : const char *desc = data->d_buf + desc_offset;
12453 : :
12454 : : /* GNU Build Attributes are weird, they store most of their data
12455 : : into the owner name field. Extract just the owner name
12456 : : prefix here, then use the rest later as data. */
12457 : 1222 : bool is_gnu_build_attr
12458 : 1222 : = strncmp (name, ELF_NOTE_GNU_BUILD_ATTRIBUTE_PREFIX,
12459 : : strlen (ELF_NOTE_GNU_BUILD_ATTRIBUTE_PREFIX)) == 0;
12460 : 2444 : const char *print_name = (is_gnu_build_attr
12461 [ + + ]: 1222 : ? ELF_NOTE_GNU_BUILD_ATTRIBUTE_PREFIX : name);
12462 : 2444 : size_t print_namesz = (is_gnu_build_attr
12463 [ + + ]: 1222 : ? strlen (print_name) : nhdr.n_namesz);
12464 : :
12465 : 1222 : char buf[100];
12466 : 1222 : char buf2[100];
12467 : 2444 : printf (_(" %-13.*s %9" PRId32 " %s\n"),
12468 : : (int) print_namesz, print_name, nhdr.n_descsz,
12469 [ + + ]: 1222 : ehdr->e_type == ET_CORE
12470 : 59 : ? ebl_core_note_type_name (ebl, nhdr.n_type,
12471 : : buf, sizeof (buf))
12472 : 1163 : : ebl_object_note_type_name (ebl, name, nhdr.n_type,
12473 : : nhdr.n_descsz,
12474 : : buf2, sizeof (buf2)));
12475 : :
12476 : : /* Filter out invalid entries. */
12477 : 1222 : if (memchr (name, '\0', nhdr.n_namesz) != NULL
12478 : : /* XXX For now help broken Linux kernels. */
12479 : : || 1)
12480 : : {
12481 [ + + ]: 1222 : if (ehdr->e_type == ET_CORE)
12482 : : {
12483 [ + + ]: 59 : if (nhdr.n_type == NT_AUXV
12484 [ + - ]: 9 : && (nhdr.n_namesz == 4 /* Broken old Linux kernels. */
12485 [ + - + - ]: 9 : || (nhdr.n_namesz == 5 && name[4] == '\0'))
12486 [ + - ]: 9 : && !memcmp (name, "CORE", 4))
12487 : 9 : handle_auxv_note (ebl, ebl->elf, nhdr.n_descsz,
12488 : : start + desc_offset);
12489 [ + + + - ]: 50 : else if (nhdr.n_namesz == 5 && strcmp (name, "CORE") == 0)
12490 [ + + + ]: 38 : switch (nhdr.n_type)
12491 : : {
12492 : 6 : case NT_SIGINFO:
12493 : 6 : handle_siginfo_note (ebl->elf, nhdr.n_descsz,
12494 : : start + desc_offset);
12495 : 6 : break;
12496 : :
12497 : 6 : case NT_FILE:
12498 : 6 : handle_file_note (ebl->elf, nhdr.n_descsz,
12499 : : start + desc_offset);
12500 : 6 : break;
12501 : :
12502 : 26 : default:
12503 : 26 : handle_core_note (ebl, &nhdr, name, desc);
12504 : : }
12505 : : else
12506 : 12 : handle_core_note (ebl, &nhdr, name, desc);
12507 : : }
12508 : : else
12509 : 1163 : ebl_object_note (ebl, nhdr.n_namesz, name, nhdr.n_type,
12510 : : nhdr.n_descsz, desc);
12511 : : }
12512 : : }
12513 : :
12514 [ + - ]: 158 : if (offset == data->d_size)
12515 : 158 : return;
12516 : :
12517 : 0 : bad_note:
12518 : 0 : error (0, 0,
12519 [ # # ]: 0 : _("cannot get content of note: %s"),
12520 : 0 : data != NULL ? "garbage data" : elf_errmsg (-1));
12521 : : }
12522 : :
12523 : : static void
12524 : 104 : handle_notes (Ebl *ebl, GElf_Ehdr *ehdr)
12525 : : {
12526 : : /* If we have section headers, just look for SHT_NOTE sections.
12527 : : In a debuginfo file, the program headers are not reliable. */
12528 [ + + ]: 104 : if (shnum != 0)
12529 : : {
12530 : : /* Get the section header string table index. */
12531 : 93 : size_t shstrndx;
12532 [ - + ]: 93 : if (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0)
12533 : 0 : error (EXIT_FAILURE, 0,
12534 : 0 : _("cannot get section header string table index"));
12535 : :
12536 : : Elf_Scn *scn = NULL;
12537 [ + + ]: 2466 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
12538 : : {
12539 : 2373 : GElf_Shdr shdr_mem;
12540 : 2373 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
12541 : :
12542 [ + - + + ]: 2373 : if (shdr == NULL || shdr->sh_type != SHT_NOTE)
12543 : : /* Not what we are looking for. */
12544 : 2227 : continue;
12545 : :
12546 [ - + ]: 146 : if (notes_section != NULL)
12547 : : {
12548 : 0 : char *sname = elf_strptr (ebl->elf, shstrndx, shdr->sh_name);
12549 [ # # # # ]: 0 : if (sname == NULL || strcmp (sname, notes_section) != 0)
12550 : 0 : continue;
12551 : : }
12552 : :
12553 : 292 : printf (_("\
12554 : : \nNote section [%2zu] '%s' of %" PRIu64 " bytes at offset %#0" PRIx64 ":\n"),
12555 : : elf_ndxscn (scn),
12556 : 146 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
12557 : : shdr->sh_size, shdr->sh_offset);
12558 : :
12559 : 146 : handle_notes_data (ebl, ehdr, shdr->sh_offset,
12560 : : elf_getdata (scn, NULL));
12561 : : }
12562 : 93 : return;
12563 : : }
12564 : :
12565 : : /* We have to look through the program header to find the note
12566 : : sections. There can be more than one. */
12567 [ + + ]: 140 : for (size_t cnt = 0; cnt < phnum; ++cnt)
12568 : : {
12569 : 129 : GElf_Phdr mem;
12570 : 129 : GElf_Phdr *phdr = gelf_getphdr (ebl->elf, cnt, &mem);
12571 : :
12572 [ + - + + ]: 129 : if (phdr == NULL || phdr->p_type != PT_NOTE)
12573 : : /* Not what we are looking for. */
12574 : 117 : continue;
12575 : :
12576 : 12 : printf (_("\
12577 : : \nNote segment of %" PRIu64 " bytes at offset %#0" PRIx64 ":\n"),
12578 : : phdr->p_filesz, phdr->p_offset);
12579 : :
12580 : 12 : handle_notes_data (ebl, ehdr, phdr->p_offset,
12581 : : elf_getdata_rawchunk (ebl->elf,
12582 : : phdr->p_offset, phdr->p_filesz,
12583 [ + + ]: 12 : (phdr->p_align == 8
12584 : : ? ELF_T_NHDR8 : ELF_T_NHDR)));
12585 : : }
12586 : : }
12587 : :
12588 : :
12589 : : static void
12590 : 6 : hex_dump (const uint8_t *data, size_t len)
12591 : : {
12592 : 6 : size_t pos = 0;
12593 [ + + ]: 28 : while (pos < len)
12594 : : {
12595 : 22 : printf (" 0x%08zx ", pos);
12596 : :
12597 : 22 : const size_t chunk = MIN (len - pos, 16);
12598 : :
12599 [ + + ]: 344 : for (size_t i = 0; i < chunk; ++i)
12600 [ + + ]: 322 : if (i % 4 == 3)
12601 : 80 : printf ("%02x ", data[pos + i]);
12602 : : else
12603 : 322 : printf ("%02x", data[pos + i]);
12604 : :
12605 [ + + ]: 22 : if (chunk < 16)
12606 : 2 : printf ("%*s", (int) ((16 - chunk) * 2 + (16 - chunk + 3) / 4), "");
12607 : :
12608 [ + + ]: 344 : for (size_t i = 0; i < chunk; ++i)
12609 : : {
12610 : 322 : unsigned char b = data[pos + i];
12611 [ + + ]: 644 : printf ("%c", isprint (b) ? b : '.');
12612 : : }
12613 : :
12614 : 22 : putchar ('\n');
12615 : 22 : pos += chunk;
12616 : : }
12617 : 6 : }
12618 : :
12619 : : static void
12620 : 6 : dump_data_section (Elf_Scn *scn, const GElf_Shdr *shdr, const char *name)
12621 : : {
12622 [ + - - + ]: 6 : if (shdr->sh_size == 0 || shdr->sh_type == SHT_NOBITS)
12623 : 0 : printf (_("\nSection [%zu] '%s' has no data to dump.\n"),
12624 : : elf_ndxscn (scn), name);
12625 : : else
12626 : : {
12627 [ + + ]: 6 : if (print_decompress)
12628 : : {
12629 : : /* We try to decompress the section, but keep the old shdr around
12630 : : so we can show both the original shdr size and the uncompressed
12631 : : data size. */
12632 [ + + ]: 4 : if ((shdr->sh_flags & SHF_COMPRESSED) != 0)
12633 : : {
12634 [ - + ]: 2 : if (elf_compress (scn, 0, 0) < 0)
12635 : 0 : printf ("WARNING: %s [%zd]\n",
12636 : : _("Couldn't uncompress section"),
12637 : : elf_ndxscn (scn));
12638 : : }
12639 [ + - ]: 2 : else if (strncmp (name, ".zdebug", strlen (".zdebug")) == 0)
12640 : : {
12641 [ - + ]: 2 : if (elf_compress_gnu (scn, 0, 0) < 0)
12642 : 0 : printf ("WARNING: %s [%zd]\n",
12643 : : _("Couldn't uncompress section"),
12644 : : elf_ndxscn (scn));
12645 : : }
12646 : : }
12647 : :
12648 : 6 : Elf_Data *data = elf_rawdata (scn, NULL);
12649 [ - + ]: 6 : if (data == NULL)
12650 : 0 : error (0, 0, _("cannot get data for section [%zu] '%s': %s"),
12651 : : elf_ndxscn (scn), name, elf_errmsg (-1));
12652 : : else
12653 : : {
12654 [ + + ]: 6 : if (data->d_size == shdr->sh_size)
12655 : 2 : printf (_("\nHex dump of section [%zu] '%s', %" PRIu64
12656 : : " bytes at offset %#0" PRIx64 ":\n"),
12657 : : elf_ndxscn (scn), name,
12658 : : shdr->sh_size, shdr->sh_offset);
12659 : : else
12660 : 4 : printf (_("\nHex dump of section [%zu] '%s', %" PRIu64
12661 : : " bytes (%zd uncompressed) at offset %#0"
12662 : : PRIx64 ":\n"),
12663 : : elf_ndxscn (scn), name,
12664 : : shdr->sh_size, data->d_size, shdr->sh_offset);
12665 : 6 : hex_dump (data->d_buf, data->d_size);
12666 : : }
12667 : : }
12668 : 6 : }
12669 : :
12670 : : static void
12671 : 162 : print_string_section (Elf_Scn *scn, const GElf_Shdr *shdr, const char *name)
12672 : : {
12673 [ + - + + ]: 162 : if (shdr->sh_size == 0 || shdr->sh_type == SHT_NOBITS)
12674 : 12 : printf (_("\nSection [%zu] '%s' has no strings to dump.\n"),
12675 : : elf_ndxscn (scn), name);
12676 : : else
12677 : : {
12678 [ + + ]: 150 : if (print_decompress)
12679 : : {
12680 : : /* We try to decompress the section, but keep the old shdr around
12681 : : so we can show both the original shdr size and the uncompressed
12682 : : data size. */
12683 [ - + ]: 1 : if ((shdr->sh_flags & SHF_COMPRESSED) != 0)
12684 : : {
12685 [ # # ]: 0 : if (elf_compress (scn, 0, 0) < 0)
12686 : 0 : printf ("WARNING: %s [%zd]\n",
12687 : : _("Couldn't uncompress section"),
12688 : : elf_ndxscn (scn));
12689 : : }
12690 [ + - ]: 1 : else if (strncmp (name, ".zdebug", strlen (".zdebug")) == 0)
12691 : : {
12692 [ - + ]: 1 : if (elf_compress_gnu (scn, 0, 0) < 0)
12693 : 0 : printf ("WARNING: %s [%zd]\n",
12694 : : _("Couldn't uncompress section"),
12695 : : elf_ndxscn (scn));
12696 : : }
12697 : : }
12698 : :
12699 : 150 : Elf_Data *data = elf_rawdata (scn, NULL);
12700 [ - + ]: 150 : if (data == NULL)
12701 : 0 : error (0, 0, _("cannot get data for section [%zu] '%s': %s"),
12702 : : elf_ndxscn (scn), name, elf_errmsg (-1));
12703 : : else
12704 : : {
12705 [ + + ]: 150 : if (data->d_size == shdr->sh_size)
12706 : 149 : printf (_("\nString section [%zu] '%s' contains %" PRIu64
12707 : : " bytes at offset %#0" PRIx64 ":\n"),
12708 : : elf_ndxscn (scn), name,
12709 : : shdr->sh_size, shdr->sh_offset);
12710 : : else
12711 : 1 : printf (_("\nString section [%zu] '%s' contains %" PRIu64
12712 : : " bytes (%zd uncompressed) at offset %#0"
12713 : : PRIx64 ":\n"),
12714 : : elf_ndxscn (scn), name,
12715 : : shdr->sh_size, data->d_size, shdr->sh_offset);
12716 : :
12717 : 150 : const char *start = data->d_buf;
12718 : 150 : const char *const limit = start + data->d_size;
12719 : 20463 : do
12720 : : {
12721 : 20463 : const char *end = memchr (start, '\0', limit - start);
12722 : 20463 : const size_t pos = start - (const char *) data->d_buf;
12723 [ - + ]: 20463 : if (unlikely (end == NULL))
12724 : : {
12725 : 0 : printf (" [%6zx]- %.*s\n",
12726 : : pos, (int) (limit - start), start);
12727 : : break;
12728 : : }
12729 : 20463 : printf (" [%6zx] %s\n", pos, start);
12730 : 20463 : start = end + 1;
12731 [ + + ]: 20463 : } while (start < limit);
12732 : : }
12733 : : }
12734 : 162 : }
12735 : :
12736 : : static void
12737 : 88 : for_each_section_argument (Elf *elf, const struct section_argument *list,
12738 : : void (*dump) (Elf_Scn *scn, const GElf_Shdr *shdr,
12739 : : const char *name))
12740 : : {
12741 : : /* Get the section header string table index. */
12742 : 88 : size_t shstrndx;
12743 [ - + ]: 88 : if (elf_getshdrstrndx (elf, &shstrndx) < 0)
12744 : 0 : error (EXIT_FAILURE, 0,
12745 : 0 : _("cannot get section header string table index"));
12746 : :
12747 [ + + ]: 338 : for (const struct section_argument *a = list; a != NULL; a = a->next)
12748 : : {
12749 : 250 : Elf_Scn *scn;
12750 : 250 : GElf_Shdr shdr_mem;
12751 : 250 : const char *name = NULL;
12752 : :
12753 : 250 : char *endp = NULL;
12754 : 250 : unsigned long int shndx = strtoul (a->arg, &endp, 0);
12755 [ + + - + ]: 250 : if (endp != a->arg && *endp == '\0')
12756 : : {
12757 : 1 : scn = elf_getscn (elf, shndx);
12758 [ - + ]: 1 : if (scn == NULL)
12759 : : {
12760 : 0 : error (0, 0, _("\nsection [%lu] does not exist"), shndx);
12761 : 0 : continue;
12762 : : }
12763 : :
12764 [ - + ]: 1 : if (gelf_getshdr (scn, &shdr_mem) == NULL)
12765 : 0 : error (EXIT_FAILURE, 0, _("cannot get section header: %s"),
12766 : : elf_errmsg (-1));
12767 : 1 : name = elf_strptr (elf, shstrndx, shdr_mem.sh_name);
12768 : 1 : (*dump) (scn, &shdr_mem, name);
12769 : : }
12770 : : else
12771 : : {
12772 : : /* Need to look up the section by name. */
12773 : : scn = NULL;
12774 : : bool found = false;
12775 [ + + ]: 6819 : while ((scn = elf_nextscn (elf, scn)) != NULL)
12776 : : {
12777 [ - + ]: 6570 : if (gelf_getshdr (scn, &shdr_mem) == NULL)
12778 : 0 : continue;
12779 : 6570 : name = elf_strptr (elf, shstrndx, shdr_mem.sh_name);
12780 [ - + ]: 6570 : if (name == NULL)
12781 : 0 : continue;
12782 [ + + ]: 6570 : if (!strcmp (name, a->arg))
12783 : : {
12784 : 167 : found = true;
12785 : 167 : (*dump) (scn, &shdr_mem, name);
12786 : : }
12787 : : }
12788 : :
12789 [ + + - + ]: 249 : if (unlikely (!found) && !a->implicit)
12790 : 250 : error (0, 0, _("\nsection '%s' does not exist"), a->arg);
12791 : : }
12792 : : }
12793 : 88 : }
12794 : :
12795 : : static void
12796 : 6 : dump_data (Ebl *ebl)
12797 : : {
12798 : 6 : for_each_section_argument (ebl->elf, dump_data_sections, &dump_data_section);
12799 : 6 : }
12800 : :
12801 : : static void
12802 : 82 : dump_strings (Ebl *ebl)
12803 : : {
12804 : 82 : for_each_section_argument (ebl->elf, string_sections, &print_string_section);
12805 : 82 : }
12806 : :
12807 : : static void
12808 : 0 : print_strings (Ebl *ebl)
12809 : : {
12810 : : /* Get the section header string table index. */
12811 : 0 : size_t shstrndx;
12812 [ # # ]: 0 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
12813 : 0 : error (EXIT_FAILURE, 0,
12814 : 0 : _("cannot get section header string table index"));
12815 : :
12816 : : Elf_Scn *scn;
12817 : : GElf_Shdr shdr_mem;
12818 : : const char *name;
12819 : : scn = NULL;
12820 [ # # ]: 0 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
12821 : : {
12822 [ # # ]: 0 : if (gelf_getshdr (scn, &shdr_mem) == NULL)
12823 : 0 : continue;
12824 : :
12825 [ # # ]: 0 : if (shdr_mem.sh_type != SHT_PROGBITS
12826 [ # # ]: 0 : || !(shdr_mem.sh_flags & SHF_STRINGS))
12827 : 0 : continue;
12828 : :
12829 : 0 : name = elf_strptr (ebl->elf, shstrndx, shdr_mem.sh_name);
12830 [ # # ]: 0 : if (name == NULL)
12831 : 0 : continue;
12832 : :
12833 : 0 : print_string_section (scn, &shdr_mem, name);
12834 : : }
12835 : 0 : }
12836 : :
12837 : : static void
12838 : 2 : dump_archive_index (Elf *elf, const char *fname)
12839 : : {
12840 : 2 : size_t narsym;
12841 : 2 : const Elf_Arsym *arsym = elf_getarsym (elf, &narsym);
12842 [ - + ]: 2 : if (arsym == NULL)
12843 : : {
12844 : 0 : int result = elf_errno ();
12845 [ # # ]: 0 : if (unlikely (result != ELF_E_NO_INDEX))
12846 : 0 : error (EXIT_FAILURE, 0,
12847 : 0 : _("cannot get symbol index of archive '%s': %s"),
12848 : : fname, elf_errmsg (result));
12849 : : else
12850 : 0 : printf (_("\nArchive '%s' has no symbol index\n"), fname);
12851 : 0 : return;
12852 : : }
12853 : :
12854 : 2 : printf (_("\nIndex of archive '%s' has %zu entries:\n"),
12855 : : fname, narsym);
12856 : :
12857 : 2 : size_t as_off = 0;
12858 [ + + ]: 11 : for (const Elf_Arsym *s = arsym; s < &arsym[narsym - 1]; ++s)
12859 : : {
12860 [ + + ]: 9 : if (s->as_off != as_off)
12861 : : {
12862 : 6 : as_off = s->as_off;
12863 : :
12864 : 6 : Elf *subelf = NULL;
12865 [ + - ]: 6 : if (unlikely (elf_rand (elf, as_off) == 0)
12866 [ - + ]: 6 : || unlikely ((subelf = elf_begin (-1, ELF_C_READ_MMAP, elf))
12867 : : == NULL))
12868 : : #if __GLIBC__ < 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ < 7)
12869 : : while (1)
12870 : : #endif
12871 : 0 : error (EXIT_FAILURE, 0,
12872 : 0 : _("cannot extract member at offset %zu in '%s': %s"),
12873 : : as_off, fname, elf_errmsg (-1));
12874 : :
12875 : 6 : const Elf_Arhdr *h = elf_getarhdr (subelf);
12876 : :
12877 : 6 : printf (_("Archive member '%s' contains:\n"), h->ar_name);
12878 : :
12879 : 6 : elf_end (subelf);
12880 : : }
12881 : :
12882 : 9 : printf ("\t%s\n", s->as_name);
12883 : : }
12884 : : }
12885 : :
12886 : : #include "debugpred.h"
|