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 : 768 : cleanup_list (struct section_argument *list)
328 : : {
329 [ + + + + ]: 1135 : while (list != NULL)
330 : : {
331 : 367 : struct section_argument *a = list;
332 : 367 : list = a->next;
333 : 367 : free (a);
334 : : }
335 : : }
336 : :
337 : : int
338 : 384 : main (int argc, char *argv[])
339 : : {
340 : : /* We use no threads here which can interfere with handling a stream. */
341 : 384 : (void) __fsetlocking (stdout, FSETLOCKING_BYCALLER);
342 : :
343 : : /* Set locale. */
344 : 384 : setlocale (LC_ALL, "");
345 : :
346 : : /* Initialize the message catalog. */
347 : 384 : textdomain (PACKAGE_TARNAME);
348 : :
349 : : /* Look up once. */
350 : 384 : yes_str = _("yes");
351 : 384 : no_str = _("no");
352 : :
353 : : /* Parse and process arguments. */
354 : 384 : int remaining;
355 : 384 : argp_parse (&argp, argc, argv, 0, &remaining, NULL);
356 : :
357 : : /* Before we start tell the ELF library which version we are using. */
358 : 384 : elf_version (EV_CURRENT);
359 : :
360 : : /* Now process all the files given at the command line. */
361 : 384 : bool only_one = remaining + 1 == argc;
362 : 423 : do
363 : : {
364 : : /* Open the file. */
365 : 423 : int fd = open (argv[remaining], O_RDONLY);
366 [ - + ]: 423 : if (fd == -1)
367 : : {
368 : 0 : error (0, errno, _("cannot open input file '%s'"), argv[remaining]);
369 : 0 : continue;
370 : : }
371 : :
372 : 423 : process_file (fd, argv[remaining], only_one);
373 : :
374 : 423 : close (fd);
375 : : }
376 [ + + ]: 423 : while (++remaining < argc);
377 : :
378 : 384 : cleanup_list (dump_data_sections);
379 : 384 : cleanup_list (string_sections);
380 : :
381 : 384 : return error_message_count != 0;
382 : : }
383 : :
384 : : static void
385 : 367 : add_dump_section (const char *name,
386 : : int key,
387 : : bool implicit)
388 : : {
389 : 367 : struct section_argument *a = xmalloc (sizeof *a);
390 : 367 : a->arg = name;
391 : 367 : a->next = NULL;
392 : 367 : a->implicit = implicit;
393 : 734 : struct section_argument ***tailp
394 [ + + ]: 367 : = key == 'x' ? &dump_data_sections_tail : &string_sections_tail;
395 : 367 : **tailp = a;
396 : 367 : *tailp = &a->next;
397 : 367 : }
398 : :
399 : : /* Handle program arguments. */
400 : : static error_t
401 : 2373 : parse_opt (int key, char *arg,
402 : : struct argp_state *state __attribute__ ((unused)))
403 : : {
404 [ + + + - : 2373 : switch (key)
+ + - + +
+ + + + -
+ + + + +
+ - + + +
+ - + ]
405 : : {
406 : 120 : case 'a':
407 : 120 : print_file_header = true;
408 : 120 : print_program_header = true;
409 : 120 : print_relocations = true;
410 : 120 : print_section_header = true;
411 : 120 : print_symbol_table = true;
412 : 120 : print_version_info = true;
413 : 120 : print_dynamic_table = true;
414 : 120 : print_section_groups = true;
415 : 120 : print_histogram = true;
416 : 120 : print_arch = true;
417 : 120 : print_notes = true;
418 : 120 : implicit_debug_sections |= section_exception;
419 : 120 : add_dump_section (".strtab", key, true);
420 : 120 : add_dump_section (".dynstr", key, true);
421 : 120 : add_dump_section (".comment", key, true);
422 : 120 : any_control_option = true;
423 : 120 : 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 : 2 : case 'h':
441 : 2 : print_file_header = true;
442 : 2 : any_control_option = true;
443 : 2 : 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 : 124 : case 'w':
482 [ + + ]: 124 : 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 [ - + ]: 85 : else if (strcmp (arg, "abbrev") == 0)
489 : 0 : print_debug_sections |= section_abbrev;
490 [ + + ]: 85 : else if (strcmp (arg, "addr") == 0)
491 : : {
492 : 2 : print_debug_sections |= section_addr;
493 : 2 : implicit_debug_sections |= section_info;
494 : : }
495 [ + + ]: 83 : else if (strcmp (arg, "aranges") == 0)
496 : 3 : print_debug_sections |= section_aranges;
497 [ + + ]: 80 : else if (strcmp (arg, "decodedaranges") == 0)
498 : : {
499 : 1 : print_debug_sections |= section_aranges;
500 : 1 : decodedaranges = true;
501 : : }
502 [ + + ]: 79 : else if (strcmp (arg, "ranges") == 0)
503 : : {
504 : 13 : print_debug_sections |= section_ranges;
505 : 13 : implicit_debug_sections |= section_info;
506 : : }
507 [ + + + + ]: 66 : else if (strcmp (arg, "frame") == 0 || strcmp (arg, "frames") == 0)
508 : 3 : print_debug_sections |= section_frame;
509 [ + + ]: 63 : else if (strcmp (arg, "info") == 0)
510 : : {
511 : 19 : print_debug_sections |= section_info;
512 : 19 : print_debug_sections |= section_types;
513 : : }
514 [ + + ]: 44 : 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 [ + + ]: 42 : else if (strcmp (arg, "loc") == 0)
521 : : {
522 : 19 : print_debug_sections |= section_loc;
523 : 19 : implicit_debug_sections |= section_info;
524 : : }
525 [ + + ]: 23 : else if (strcmp (arg, "line") == 0)
526 : 8 : print_debug_sections |= section_line;
527 [ + + ]: 15 : else if (strcmp (arg, "decodedline") == 0)
528 : : {
529 : 7 : print_debug_sections |= section_line;
530 : 7 : 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 : 124 : any_control_option = true;
557 : 124 : 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 : 3 : case 'N':
571 : 3 : print_address_names = false;
572 : 3 : break;
573 : 28 : case 'U':
574 : 28 : print_unresolved_addresses = true;
575 : 28 : break;
576 : 0 : case ARGP_KEY_NO_ARGS:
577 : 0 : fputs (_("Missing file name.\n"), stderr);
578 : 0 : goto do_argp_help;
579 : 384 : case ARGP_KEY_FINI:
580 [ + + - + ]: 384 : 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 : 373 : 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 [ + - ]: 373 : if (*(bool *) arg)
763 : : return DWARF_CB_ABORT;
764 : 373 : *(bool *) arg = true;
765 : 373 : 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 : 421 : 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 : 421 : const struct process_dwflmod_args *a = arg;
782 : :
783 : : /* Print the file name. */
784 [ + + ]: 421 : if (!a->only_one)
785 : : {
786 : 48 : const char *fname;
787 : 48 : dwfl_module_info (dwflmod, NULL, NULL, NULL, NULL, NULL, &fname, NULL);
788 : :
789 : 48 : printf ("\n%s:\n\n", fname);
790 : : }
791 : :
792 : 421 : process_elf_file (dwflmod, a->fd);
793 : :
794 : 421 : 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 : 146 : 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 : 146 : Dwarf_Addr dwbias;
811 : 146 : 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 [ + + ]: 146 : if (dwbias == (Dwarf_Addr) -1)
818 : : return -1;
819 : :
820 : 19 : 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 : 430 : create_dwfl (int fd, const char *fname)
827 : : {
828 : : /* Duplicate an fd for dwfl_report_offline to swallow. */
829 : 430 : int dwfl_fd = dup (fd);
830 [ - + ]: 430 : if (unlikely (dwfl_fd < 0))
831 : 0 : error_exit (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 : 430 : static const Dwfl_Callbacks callbacks =
836 : : {
837 : : .section_address = dwfl_offline_section_address,
838 : : .find_debuginfo = find_no_debuginfo
839 : : };
840 : 430 : Dwfl *dwfl = dwfl_begin (&callbacks);
841 [ + - ]: 430 : if (likely (dwfl != NULL))
842 : : /* Let 0 be the logical address of the file (or first in archive). */
843 : 430 : dwfl->offline_next_address = 0;
844 [ - + ]: 430 : 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 : 430 : dwfl_report_end (dwfl, NULL, NULL);
859 : :
860 : 430 : return dwfl;
861 : : }
862 : :
863 : : /* Process one input file. */
864 : : static void
865 : 423 : process_file (int fd, const char *fname, bool only_one)
866 : : {
867 [ + + ]: 423 : if (print_archive_index)
868 : 2 : check_archive_index (fd, fname, only_one);
869 : :
870 [ + + ]: 423 : if (!any_control_option)
871 : : return;
872 : :
873 [ + + ]: 421 : 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 : 421 : Dwfl *dwfl = create_dwfl (fd, fname);
889 [ + - ]: 421 : if (dwfl != NULL)
890 : : {
891 [ + + ]: 421 : if (only_one)
892 : : {
893 : : /* Clear ONLY_ONE if we have multiple modules, from an archive. */
894 : 373 : bool seen = false;
895 : 373 : only_one = dwfl_getmodules (dwfl, &count_dwflmod, &seen, 0) == 0;
896 : : }
897 : :
898 : : /* Process the one or more modules gleaned from this file. */
899 : 421 : struct process_dwflmod_args a = { .fd = fd, .only_one = only_one };
900 : 421 : 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 [ + - ]: 421 : if (! do_not_close_dwfl)
905 : 421 : dwfl_end (dwfl);
906 : :
907 : : /* Need to close the replaced fd if we created it. Caller takes
908 : : care of original. */
909 [ + + ]: 421 : 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 : 174 : elf_contains_chdrs (Elf *elf)
916 : : {
917 : 174 : Elf_Scn *scn = NULL;
918 [ + + ]: 791173 : while ((scn = elf_nextscn (elf, scn)) != NULL)
919 : : {
920 : 791008 : GElf_Shdr shdr_mem;
921 : 791008 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
922 [ + - + + ]: 791008 : if (shdr != NULL && (shdr->sh_flags & SHF_COMPRESSED) != 0)
923 : 9 : return true;
924 : : }
925 : : return false;
926 : : }
927 : :
928 : : /* Process one ELF file. */
929 : : static void
930 : 421 : process_elf_file (Dwfl_Module *dwflmod, int fd)
931 : : {
932 : 421 : GElf_Addr dwflbias;
933 : 421 : Elf *elf = dwfl_module_getelf (dwflmod, &dwflbias);
934 : :
935 : 421 : GElf_Ehdr ehdr_mem;
936 : 421 : GElf_Ehdr *ehdr = gelf_getehdr (elf, &ehdr_mem);
937 : :
938 [ - + ]: 421 : if (ehdr == NULL)
939 : : {
940 : 0 : error (0, 0, _("cannot read ELF header: %s"), elf_errmsg (-1));
941 : 0 : return;
942 : : }
943 : :
944 : 421 : Ebl *ebl = ebl_openbackend (elf);
945 [ - + ]: 421 : 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 [ - + ]: 421 : if (unlikely (elf_getshdrnum (ebl->elf, &shnum) < 0))
954 : 0 : error_exit (0, _("cannot determine number of sections: %s"),
955 : : elf_errmsg (-1));
956 : :
957 : : /* Determine the number of phdrs. */
958 [ - + ]: 421 : if (unlikely (elf_getphdrnum (ebl->elf, &phnum) < 0))
959 : 0 : error_exit (0, _("cannot determine number of program headers: %s"),
960 : : elf_errmsg (-1));
961 : :
962 : : /* For an ET_REL file, libdwfl has adjusted the in-core shdrs and
963 : : may have applied relocation to some sections. If there are any
964 : : compressed sections, any pass (or libdw/libdwfl) might have
965 : : uncompressed them. So we need to get a fresh Elf handle on the
966 : : file to display those. */
967 : 842 : bool print_unchanged = ((print_section_header
968 [ + + ]: 212 : || print_relocations
969 [ + + ]: 211 : || dump_data_sections != NULL
970 [ + + ]: 205 : || print_notes)
971 [ + + + + ]: 451 : && (ehdr->e_type == ET_REL
972 [ + + ]: 174 : || elf_contains_chdrs (ebl->elf)));
973 : :
974 : 421 : Elf *pure_elf = NULL;
975 : 421 : Ebl *pure_ebl = ebl;
976 [ + + ]: 421 : if (print_unchanged)
977 : : {
978 : : /* Read the file afresh. */
979 : 74 : off_t aroff = elf_getaroff (elf);
980 : 74 : pure_elf = dwelf_elf_begin (fd);
981 [ - + ]: 74 : if (aroff > 0)
982 : : {
983 : : /* Archive member. */
984 : 0 : (void) elf_rand (pure_elf, aroff);
985 : 0 : Elf *armem = elf_begin (-1, ELF_C_READ_MMAP, pure_elf);
986 : 0 : elf_end (pure_elf);
987 : 0 : pure_elf = armem;
988 : : }
989 [ - + ]: 74 : if (pure_elf == NULL)
990 : : {
991 : 0 : error (0, 0, _("cannot read ELF: %s"), elf_errmsg (-1));
992 : 0 : return;
993 : : }
994 : 74 : pure_ebl = ebl_openbackend (pure_elf);
995 [ - + ]: 74 : if (pure_ebl == NULL)
996 : 0 : goto ebl_error;
997 : : }
998 : :
999 [ + + ]: 421 : if (print_file_header)
1000 : 122 : print_ehdr (ebl, ehdr);
1001 [ + + ]: 421 : if (print_section_header)
1002 : 209 : print_shdr (pure_ebl, ehdr);
1003 [ + + ]: 421 : if (print_program_header)
1004 : 122 : print_phdr (ebl, ehdr);
1005 [ + + ]: 421 : if (print_section_groups)
1006 : 129 : print_scngrp (ebl);
1007 [ + + ]: 421 : if (print_dynamic_table)
1008 : 122 : print_dynamic (ebl);
1009 [ + + ]: 421 : if (print_relocations)
1010 : 121 : print_relocs (pure_ebl, ehdr);
1011 [ + + ]: 421 : if (print_histogram)
1012 : 120 : handle_hash (ebl);
1013 [ + + + + ]: 421 : if (print_symbol_table || print_dynsym_table)
1014 : 135 : print_symtab (ebl, SHT_DYNSYM);
1015 [ + + ]: 421 : if (print_version_info)
1016 : 120 : print_verinfo (ebl);
1017 [ + + ]: 421 : if (print_symbol_table)
1018 : 134 : print_symtab (ebl, SHT_SYMTAB);
1019 [ + + ]: 421 : if (print_arch)
1020 : 124 : print_liblist (ebl);
1021 [ + + ]: 421 : if (print_arch)
1022 : 124 : print_attributes (ebl, ehdr);
1023 [ + + ]: 421 : if (dump_data_sections != NULL)
1024 : 6 : dump_data (pure_ebl);
1025 [ + + ]: 421 : if (string_sections != NULL)
1026 : 121 : dump_strings (ebl);
1027 [ + + ]: 421 : if ((print_debug_sections | implicit_debug_sections) != 0)
1028 : 269 : print_debug (dwflmod, ebl, ehdr);
1029 [ + + ]: 421 : if (print_notes)
1030 : 143 : handle_notes (pure_ebl, ehdr);
1031 [ - + ]: 421 : if (print_string_sections)
1032 : 0 : print_strings (ebl);
1033 : :
1034 : 421 : ebl_closebackend (ebl);
1035 : :
1036 [ + + ]: 421 : if (pure_ebl != ebl)
1037 : : {
1038 : 74 : ebl_closebackend (pure_ebl);
1039 : 74 : elf_end (pure_elf);
1040 : : }
1041 : : }
1042 : :
1043 : :
1044 : : /* Print file type. */
1045 : : static void
1046 : 122 : print_file_type (unsigned short int e_type)
1047 : : {
1048 [ + - ]: 122 : if (likely (e_type <= ET_CORE))
1049 : : {
1050 : 122 : static const char *const knowntypes[] =
1051 : : {
1052 : : N_("NONE (None)"),
1053 : : N_("REL (Relocatable file)"),
1054 : : N_("EXEC (Executable file)"),
1055 : : N_("DYN (Shared object file)"),
1056 : : N_("CORE (Core file)")
1057 : : };
1058 : 122 : puts (_(knowntypes[e_type]));
1059 : : }
1060 [ # # ]: 0 : else if (e_type >= ET_LOOS && e_type <= ET_HIOS)
1061 : 0 : printf (_("OS Specific: (%x)\n"), e_type);
1062 [ # # ]: 0 : else if (e_type >= ET_LOPROC /* && e_type <= ET_HIPROC always true */)
1063 : 0 : printf (_("Processor Specific: (%x)\n"), e_type);
1064 : : else
1065 : 0 : puts ("???");
1066 : 122 : }
1067 : :
1068 : :
1069 : : /* Print ELF header. */
1070 : : static void
1071 : 122 : print_ehdr (Ebl *ebl, GElf_Ehdr *ehdr)
1072 : : {
1073 : 122 : fputs_unlocked (_("ELF Header:\n Magic: "), stdout);
1074 [ + + ]: 2074 : for (size_t cnt = 0; cnt < EI_NIDENT; ++cnt)
1075 : 1952 : printf (" %02hhx", ehdr->e_ident[cnt]);
1076 : :
1077 : 244 : printf (_("\n Class: %s\n"),
1078 [ + + ]: 122 : ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? "ELF32"
1079 : : : ehdr->e_ident[EI_CLASS] == ELFCLASS64 ? "ELF64"
1080 [ - + ]: 100 : : "\?\?\?");
1081 : :
1082 : 244 : printf (_(" Data: %s\n"),
1083 [ + + ]: 122 : ehdr->e_ident[EI_DATA] == ELFDATA2LSB
1084 : : ? "2's complement, little endian"
1085 : : : ehdr->e_ident[EI_DATA] == ELFDATA2MSB
1086 [ - + ]: 12 : ? "2's complement, big endian" : "\?\?\?");
1087 : :
1088 : 244 : printf (_(" Ident Version: %hhd %s\n"),
1089 [ + - ]: 122 : ehdr->e_ident[EI_VERSION],
1090 : 122 : ehdr->e_ident[EI_VERSION] == EV_CURRENT ? _("(current)")
1091 : : : "(\?\?\?)");
1092 : :
1093 : 122 : char buf[512];
1094 : 122 : printf (_(" OS/ABI: %s\n"),
1095 : 122 : ebl_osabi_name (ebl, ehdr->e_ident[EI_OSABI], buf, sizeof (buf)));
1096 : :
1097 : 244 : printf (_(" ABI Version: %hhd\n"),
1098 : 122 : ehdr->e_ident[EI_ABIVERSION]);
1099 : :
1100 : 122 : fputs_unlocked (_(" Type: "), stdout);
1101 : 122 : print_file_type (ehdr->e_type);
1102 : :
1103 : 122 : const char *machine = dwelf_elf_e_machine_string (ehdr->e_machine);
1104 [ + - ]: 122 : if (machine != NULL)
1105 : 122 : printf (_(" Machine: %s\n"), machine);
1106 : : else
1107 : 122 : printf (_(" Machine: <unknown>: 0x%x\n"),
1108 : 0 : ehdr->e_machine);
1109 : :
1110 [ + - ]: 122 : printf (_(" Version: %d %s\n"),
1111 : : ehdr->e_version,
1112 : 122 : ehdr->e_version == EV_CURRENT ? _("(current)") : "(\?\?\?)");
1113 : :
1114 : 122 : printf (_(" Entry point address: %#" PRIx64 "\n"),
1115 : : ehdr->e_entry);
1116 : :
1117 : 122 : printf (_(" Start of program headers: %" PRId64 " %s\n"),
1118 : : ehdr->e_phoff, _("(bytes into file)"));
1119 : :
1120 : 122 : printf (_(" Start of section headers: %" PRId64 " %s\n"),
1121 : : ehdr->e_shoff, _("(bytes into file)"));
1122 : :
1123 : 122 : printf (_(" Flags: %s\n"),
1124 : : ebl_machine_flag_name (ebl, ehdr->e_flags, buf, sizeof (buf)));
1125 : :
1126 : 244 : printf (_(" Size of this header: %" PRId16 " %s\n"),
1127 : 122 : ehdr->e_ehsize, _("(bytes)"));
1128 : :
1129 : 244 : printf (_(" Size of program header entries: %" PRId16 " %s\n"),
1130 : 122 : ehdr->e_phentsize, _("(bytes)"));
1131 : :
1132 : 244 : printf (_(" Number of program headers entries: %" PRId16),
1133 : 122 : ehdr->e_phnum);
1134 [ + + ]: 122 : if (ehdr->e_phnum == PN_XNUM)
1135 : : {
1136 : 1 : GElf_Shdr shdr_mem;
1137 : 1 : GElf_Shdr *shdr = gelf_getshdr (elf_getscn (ebl->elf, 0), &shdr_mem);
1138 [ + - ]: 1 : if (shdr != NULL)
1139 : 1 : printf (_(" (%" PRIu32 " in [0].sh_info)"),
1140 : 1 : (uint32_t) shdr->sh_info);
1141 : : else
1142 : 0 : fputs_unlocked (_(" ([0] not available)"), stdout);
1143 : : }
1144 [ - + ]: 122 : fputc_unlocked ('\n', stdout);
1145 : :
1146 : 244 : printf (_(" Size of section header entries: %" PRId16 " %s\n"),
1147 : 122 : ehdr->e_shentsize, _("(bytes)"));
1148 : :
1149 : 244 : printf (_(" Number of section headers entries: %" PRId16),
1150 : 122 : ehdr->e_shnum);
1151 [ - + ]: 122 : if (ehdr->e_shnum == 0)
1152 : : {
1153 : 0 : GElf_Shdr shdr_mem;
1154 : 0 : GElf_Shdr *shdr = gelf_getshdr (elf_getscn (ebl->elf, 0), &shdr_mem);
1155 [ # # ]: 0 : if (shdr != NULL)
1156 : 0 : printf (_(" (%" PRIu32 " in [0].sh_size)"),
1157 : 0 : (uint32_t) shdr->sh_size);
1158 : : else
1159 : 0 : fputs_unlocked (_(" ([0] not available)"), stdout);
1160 : : }
1161 [ - + ]: 122 : fputc_unlocked ('\n', stdout);
1162 : :
1163 [ - + ]: 122 : if (unlikely (ehdr->e_shstrndx == SHN_XINDEX))
1164 : : {
1165 : 0 : GElf_Shdr shdr_mem;
1166 : 0 : GElf_Shdr *shdr = gelf_getshdr (elf_getscn (ebl->elf, 0), &shdr_mem);
1167 [ # # ]: 0 : if (shdr != NULL)
1168 : : /* We managed to get the zeroth section. */
1169 : 0 : snprintf (buf, sizeof (buf), _(" (%" PRIu32 " in [0].sh_link)"),
1170 : 0 : (uint32_t) shdr->sh_link);
1171 : : else
1172 : : {
1173 : 0 : strncpy (buf, _(" ([0] not available)"), sizeof (buf) - 1);
1174 : 0 : buf[sizeof (buf) - 1] = '\0';
1175 : : }
1176 : :
1177 : 0 : printf (_(" Section header string table index: XINDEX%s\n\n"),
1178 : : buf);
1179 : : }
1180 : : else
1181 : 244 : printf (_(" Section header string table index: %" PRId16 "\n\n"),
1182 : 122 : ehdr->e_shstrndx);
1183 : 122 : }
1184 : :
1185 : :
1186 : : static const char *
1187 : 58719 : get_visibility_type (int value)
1188 : : {
1189 [ - + - - : 58719 : switch (value)
+ ]
1190 : : {
1191 : : case STV_DEFAULT:
1192 : : return "DEFAULT";
1193 : 0 : case STV_INTERNAL:
1194 : 0 : return "INTERNAL";
1195 : 469 : case STV_HIDDEN:
1196 : 469 : return "HIDDEN";
1197 : 0 : case STV_PROTECTED:
1198 : 0 : return "PROTECTED";
1199 : 0 : default:
1200 : 0 : return "???";
1201 : : }
1202 : : }
1203 : :
1204 : : static const char *
1205 : 12 : elf_ch_type_name (unsigned int code)
1206 : : {
1207 : 12 : if (code == 0)
1208 : : return "NONE";
1209 : :
1210 [ + - ]: 12 : if (code == ELFCOMPRESS_ZLIB)
1211 : 12 : return "ZLIB";
1212 : :
1213 : : return "UNKNOWN";
1214 : : }
1215 : :
1216 : : /* Print the section headers. */
1217 : : static void
1218 : 209 : print_shdr (Ebl *ebl, GElf_Ehdr *ehdr)
1219 : : {
1220 : 209 : size_t cnt;
1221 : 209 : size_t shstrndx;
1222 : :
1223 [ + + ]: 209 : if (! print_file_header)
1224 : : {
1225 : 89 : size_t sections;
1226 [ - + ]: 89 : if (unlikely (elf_getshdrnum (ebl->elf, §ions) < 0))
1227 : 0 : error_exit (0, _("cannot get number of sections: %s"),
1228 : : elf_errmsg (-1));
1229 : :
1230 : 89 : printf (_("\
1231 : : There are %zd section headers, starting at offset %#" PRIx64 ":\n\
1232 : : \n"),
1233 : : sections, ehdr->e_shoff);
1234 : : }
1235 : :
1236 : : /* Get the section header string table index. */
1237 [ - + ]: 209 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
1238 : 0 : error_exit (0, _("cannot get section header string table index: %s"),
1239 : : elf_errmsg (-1));
1240 : :
1241 : 209 : puts (_("Section Headers:"));
1242 : :
1243 [ + + ]: 209 : if (ehdr->e_ident[EI_CLASS] == ELFCLASS32)
1244 : 68 : puts (_("[Nr] Name Type Addr Off Size ES Flags Lk Inf Al"));
1245 : : else
1246 : 141 : puts (_("[Nr] Name Type Addr Off Size ES Flags Lk Inf Al"));
1247 : :
1248 [ + + ]: 209 : if (print_decompress)
1249 : : {
1250 [ + + ]: 8 : if (ehdr->e_ident[EI_CLASS] == ELFCLASS32)
1251 : 4 : puts (_(" [Compression Size Al]"));
1252 : : else
1253 : 4 : puts (_(" [Compression Size Al]"));
1254 : : }
1255 : :
1256 [ + + ]: 1578706 : for (cnt = 0; cnt < shnum; ++cnt)
1257 : : {
1258 : 1578497 : Elf_Scn *scn = elf_getscn (ebl->elf, cnt);
1259 : :
1260 [ - + ]: 1578497 : if (unlikely (scn == NULL))
1261 : 0 : error_exit (0, _("cannot get section: %s"),
1262 : : elf_errmsg (-1));
1263 : :
1264 : : /* Get the section header. */
1265 : 1578497 : GElf_Shdr shdr_mem;
1266 : 1578497 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
1267 [ - + ]: 1578497 : if (unlikely (shdr == NULL))
1268 : 0 : error_exit (0, _("cannot get section header: %s"),
1269 : : elf_errmsg (-1));
1270 : :
1271 : 1578497 : char flagbuf[20];
1272 : 1578497 : char *cp = flagbuf;
1273 [ + + ]: 1578497 : if (shdr->sh_flags & SHF_WRITE)
1274 : 1304 : *cp++ = 'W';
1275 [ + + ]: 1578497 : if (shdr->sh_flags & SHF_ALLOC)
1276 : 3855 : *cp++ = 'A';
1277 [ + + ]: 1578497 : if (shdr->sh_flags & SHF_EXECINSTR)
1278 : 672 : *cp++ = 'X';
1279 [ + + ]: 1578497 : if (shdr->sh_flags & SHF_MERGE)
1280 : 205 : *cp++ = 'M';
1281 [ + + ]: 1578497 : if (shdr->sh_flags & SHF_STRINGS)
1282 : 189 : *cp++ = 'S';
1283 [ + + ]: 1578497 : if (shdr->sh_flags & SHF_INFO_LINK)
1284 : 191 : *cp++ = 'I';
1285 [ + + ]: 1578497 : if (shdr->sh_flags & SHF_LINK_ORDER)
1286 : 2 : *cp++ = 'L';
1287 [ - + ]: 1578497 : if (shdr->sh_flags & SHF_OS_NONCONFORMING)
1288 : 0 : *cp++ = 'N';
1289 [ - + ]: 1578497 : if (shdr->sh_flags & SHF_GROUP)
1290 : 0 : *cp++ = 'G';
1291 [ + + ]: 1578497 : if (shdr->sh_flags & SHF_TLS)
1292 : 19 : *cp++ = 'T';
1293 [ + + ]: 1578497 : if (shdr->sh_flags & SHF_COMPRESSED)
1294 : 27 : *cp++ = 'C';
1295 [ - + ]: 1578497 : if (shdr->sh_flags & SHF_ORDERED)
1296 : 0 : *cp++ = 'O';
1297 [ - + ]: 1578497 : if (shdr->sh_flags & SHF_EXCLUDE)
1298 : 0 : *cp++ = 'E';
1299 [ + + ]: 1578497 : if (shdr->sh_flags & SHF_GNU_RETAIN)
1300 : 1 : *cp++ = 'R';
1301 : 1578497 : *cp = '\0';
1302 : :
1303 : 1578497 : const char *sname;
1304 : 1578497 : char buf[128];
1305 [ - + ]: 1578497 : sname = elf_strptr (ebl->elf, shstrndx, shdr->sh_name) ?: "<corrupt>";
1306 [ + + ]: 2368873 : printf ("[%2zu] %-20s %-12s %0*" PRIx64 " %0*" PRIx64 " %0*" PRIx64
1307 : : " %2" PRId64 " %-5s %2" PRId32 " %3" PRId32
1308 : : " %2" PRId64 "\n",
1309 : : cnt, sname,
1310 : 1578497 : ebl_section_type_name (ebl, shdr->sh_type, buf, sizeof (buf)),
1311 [ + + ]: 1578497 : ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 8 : 16, shdr->sh_addr,
1312 : : ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 6 : 8, shdr->sh_offset,
1313 : : ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 6 : 8, shdr->sh_size,
1314 : : shdr->sh_entsize, flagbuf, shdr->sh_link, shdr->sh_info,
1315 : : shdr->sh_addralign);
1316 : :
1317 [ + + ]: 1578497 : if (print_decompress)
1318 : : {
1319 [ + + ]: 76 : if ((shdr->sh_flags & SHF_COMPRESSED) != 0)
1320 : : {
1321 : 12 : GElf_Chdr chdr;
1322 [ + - ]: 12 : if (gelf_getchdr (scn, &chdr) != NULL)
1323 [ + - ]: 24 : printf (" [ELF %s (%" PRId32 ") %0*" PRIx64
1324 : : " %2" PRId64 "]\n",
1325 : : elf_ch_type_name (chdr.ch_type),
1326 : : chdr.ch_type,
1327 [ + + ]: 12 : ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 6 : 8,
1328 : : chdr.ch_size, chdr.ch_addralign);
1329 : : else
1330 : 12 : error (0, 0,
1331 : 0 : _("bad compression header for section %zd: %s"),
1332 : : elf_ndxscn (scn), elf_errmsg (-1));
1333 : : }
1334 [ + + ]: 64 : else if (startswith (sname, ".zdebug"))
1335 : : {
1336 : 12 : ssize_t size;
1337 [ + - ]: 12 : if ((size = dwelf_scn_gnu_compressed_size (scn)) >= 0)
1338 : 1578509 : printf (" [GNU ZLIB %0*zx ]\n",
1339 [ + + ]: 12 : ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 6 : 8, size);
1340 : : else
1341 : 1578497 : error (0, 0,
1342 : 0 : _("bad gnu compressed size for section %zd: %s"),
1343 : : elf_ndxscn (scn), elf_errmsg (-1));
1344 : : }
1345 : : }
1346 : : }
1347 : :
1348 [ - + ]: 209 : fputc_unlocked ('\n', stdout);
1349 : 209 : }
1350 : :
1351 : :
1352 : : /* Print the program header. */
1353 : : static void
1354 : 122 : print_phdr (Ebl *ebl, GElf_Ehdr *ehdr)
1355 : : {
1356 [ + + ]: 122 : if (phnum == 0)
1357 : : /* No program header, this is OK in relocatable objects. */
1358 : 34 : return;
1359 : :
1360 : 88 : puts (_("Program Headers:"));
1361 [ + + ]: 88 : if (ehdr->e_ident[EI_CLASS] == ELFCLASS32)
1362 : 13 : puts (_("\
1363 : : Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align"));
1364 : : else
1365 : 75 : puts (_("\
1366 : : Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align"));
1367 : :
1368 : : /* Process all program headers. */
1369 : : bool has_relro = false;
1370 : : GElf_Addr relro_from = 0;
1371 : : GElf_Addr relro_to = 0;
1372 [ + + ]: 930 : for (size_t cnt = 0; cnt < phnum; ++cnt)
1373 : : {
1374 : 842 : char buf[128];
1375 : 842 : GElf_Phdr mem;
1376 : 842 : GElf_Phdr *phdr = gelf_getphdr (ebl->elf, cnt, &mem);
1377 : :
1378 : : /* If for some reason the header cannot be returned show this. */
1379 [ - + ]: 842 : if (unlikely (phdr == NULL))
1380 : : {
1381 : 0 : puts (" ???");
1382 : 0 : continue;
1383 : : }
1384 : :
1385 : 842 : printf (" %-14s 0x%06" PRIx64 " 0x%0*" PRIx64 " 0x%0*" PRIx64
1386 : : " 0x%06" PRIx64 " 0x%06" PRIx64 " %c%c%c 0x%" PRIx64 "\n",
1387 : 842 : ebl_segment_type_name (ebl, phdr->p_type, buf, sizeof (buf)),
1388 : : phdr->p_offset,
1389 [ + + ]: 842 : ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 8 : 16, phdr->p_vaddr,
1390 : : ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 8 : 16, phdr->p_paddr,
1391 : : phdr->p_filesz,
1392 : : phdr->p_memsz,
1393 [ - + ]: 842 : phdr->p_flags & PF_R ? 'R' : ' ',
1394 [ + + ]: 842 : phdr->p_flags & PF_W ? 'W' : ' ',
1395 [ + + ]: 842 : phdr->p_flags & PF_X ? 'E' : ' ',
1396 : : phdr->p_align);
1397 : :
1398 [ + + ]: 842 : if (phdr->p_type == PT_INTERP)
1399 : : {
1400 : : /* If we are sure the file offset is valid then we can show
1401 : : the user the name of the interpreter. We check whether
1402 : : there is a section at the file offset. Normally there
1403 : : would be a section called ".interp". But in separate
1404 : : .debug files it is a NOBITS section (and so doesn't match
1405 : : with gelf_offscn). Which probably means the offset is
1406 : : not valid another reason could be because the ELF file
1407 : : just doesn't contain any section headers, in that case
1408 : : just play it safe and don't display anything. */
1409 : :
1410 : 86 : Elf_Scn *scn = gelf_offscn (ebl->elf, phdr->p_offset);
1411 : 86 : GElf_Shdr shdr_mem;
1412 : 86 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
1413 : :
1414 : 86 : size_t maxsize;
1415 : 86 : char *filedata = elf_rawfile (ebl->elf, &maxsize);
1416 : :
1417 [ + - + + ]: 86 : if (shdr != NULL && shdr->sh_type == SHT_PROGBITS
1418 [ + - + - ]: 65 : && filedata != NULL && phdr->p_offset < maxsize
1419 [ + - ]: 65 : && phdr->p_filesz <= maxsize - phdr->p_offset
1420 [ + - ]: 65 : && memchr (filedata + phdr->p_offset, '\0',
1421 : : phdr->p_filesz) != NULL)
1422 : 86 : printf (_("\t[Requesting program interpreter: %s]\n"),
1423 : 65 : filedata + phdr->p_offset);
1424 : : }
1425 [ + + ]: 756 : else if (phdr->p_type == PT_GNU_RELRO)
1426 : : {
1427 : 76 : has_relro = true;
1428 : 76 : relro_from = phdr->p_vaddr;
1429 : 76 : relro_to = relro_from + phdr->p_memsz;
1430 : : }
1431 : : }
1432 : :
1433 : 88 : size_t sections;
1434 [ - + ]: 88 : if (unlikely (elf_getshdrnum (ebl->elf, §ions) < 0))
1435 : 0 : error_exit (0, _("cannot get number of sections: %s"),
1436 : : elf_errmsg (-1));
1437 : :
1438 [ + - ]: 88 : if (sections == 0)
1439 : : /* No sections in the file. Punt. */
1440 : : return;
1441 : :
1442 : : /* Get the section header string table index. */
1443 : 88 : size_t shstrndx;
1444 [ - + ]: 88 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
1445 : 0 : error_exit (0, _("cannot get section header string table index"));
1446 : :
1447 : 88 : puts (_("\n Section to Segment mapping:\n Segment Sections..."));
1448 : :
1449 [ + + ]: 930 : for (size_t cnt = 0; cnt < phnum; ++cnt)
1450 : : {
1451 : : /* Print the segment number. */
1452 : 842 : printf (" %2.2zu ", cnt);
1453 : :
1454 : 842 : GElf_Phdr phdr_mem;
1455 : 842 : GElf_Phdr *phdr = gelf_getphdr (ebl->elf, cnt, &phdr_mem);
1456 : : /* This must not happen. */
1457 [ - + ]: 842 : if (unlikely (phdr == NULL))
1458 : 0 : error_exit (0, _("cannot get program header: %s"),
1459 : : elf_errmsg (-1));
1460 : :
1461 : : /* Iterate over the sections. */
1462 : : bool in_relro = false;
1463 : : bool in_ro = false;
1464 [ + + ]: 26852 : for (size_t inner = 1; inner < shnum; ++inner)
1465 : : {
1466 : 26010 : Elf_Scn *scn = elf_getscn (ebl->elf, inner);
1467 : : /* This should not happen. */
1468 [ - + ]: 26010 : if (unlikely (scn == NULL))
1469 : 0 : error_exit (0, _("cannot get section: %s"),
1470 : : elf_errmsg (-1));
1471 : :
1472 : : /* Get the section header. */
1473 : 26010 : GElf_Shdr shdr_mem;
1474 : 26010 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
1475 [ - + ]: 26010 : if (unlikely (shdr == NULL))
1476 : 0 : error_exit (0, _("cannot get section header: %s"),
1477 : : elf_errmsg (-1));
1478 : :
1479 [ + - ]: 26010 : if (shdr->sh_size > 0
1480 : : /* Compare allocated sections by VMA, unallocated
1481 : : sections by file offset. */
1482 [ + + + + ]: 52020 : && (shdr->sh_flags & SHF_ALLOC
1483 : 20753 : ? (shdr->sh_addr >= phdr->p_vaddr
1484 [ + + ]: 20753 : && (shdr->sh_addr + shdr->sh_size
1485 [ + + ]: 13979 : <= phdr->p_vaddr + phdr->p_memsz))
1486 : 5257 : : (shdr->sh_offset >= phdr->p_offset
1487 [ + + ]: 5257 : && (shdr->sh_offset + shdr->sh_size
1488 [ + + ]: 4494 : <= phdr->p_offset + phdr->p_filesz))))
1489 : : {
1490 [ + + ]: 3061 : if (has_relro && !in_relro
1491 [ + + ]: 2033 : && shdr->sh_addr >= relro_from
1492 [ + + ]: 382 : && shdr->sh_addr + shdr->sh_size <= relro_to)
1493 : : {
1494 : 239 : fputs_unlocked (" [RELRO:", stdout);
1495 : 239 : in_relro = true;
1496 : : }
1497 [ + + + + ]: 2822 : else if (has_relro && in_relro && shdr->sh_addr >= relro_to)
1498 : : {
1499 : 53 : fputs_unlocked ("]", stdout);
1500 : 53 : in_relro = false;
1501 : : }
1502 [ + + ]: 2769 : else if (has_relro && in_relro
1503 [ + + ]: 583 : && shdr->sh_addr + shdr->sh_size > relro_to)
1504 : 23 : fputs_unlocked ("] <RELRO:", stdout);
1505 [ + + ]: 2746 : else if (phdr->p_type == PT_LOAD && (phdr->p_flags & PF_W) == 0)
1506 : : {
1507 [ + + ]: 1583 : if (!in_ro)
1508 : : {
1509 : 140 : fputs_unlocked (" [RO:", stdout);
1510 : 140 : in_ro = true;
1511 : : }
1512 : : }
1513 : : else
1514 : : {
1515 : : /* Determine the segment this section is part of. */
1516 : : size_t cnt2;
1517 : : GElf_Phdr phdr2_mem;
1518 : : GElf_Phdr *phdr2 = NULL;
1519 [ + - ]: 4712 : for (cnt2 = 0; cnt2 < phnum; ++cnt2)
1520 : : {
1521 : 4712 : phdr2 = gelf_getphdr (ebl->elf, cnt2, &phdr2_mem);
1522 : :
1523 [ + - + + ]: 4712 : if (phdr2 != NULL && phdr2->p_type == PT_LOAD
1524 [ + - ]: 2442 : && shdr->sh_addr >= phdr2->p_vaddr
1525 : 2442 : && (shdr->sh_addr + shdr->sh_size
1526 [ + + ]: 2442 : <= phdr2->p_vaddr + phdr2->p_memsz))
1527 : : break;
1528 : : }
1529 : :
1530 [ + - ]: 1163 : if (cnt2 < phnum)
1531 : : {
1532 [ + + + + ]: 1163 : if ((phdr2->p_flags & PF_W) == 0 && !in_ro)
1533 : : {
1534 : 265 : fputs_unlocked (" [RO:", stdout);
1535 : 265 : in_ro = true;
1536 : : }
1537 [ + + - + ]: 898 : else if ((phdr2->p_flags & PF_W) != 0 && in_ro)
1538 : : {
1539 : 0 : fputs_unlocked ("]", stdout);
1540 : 0 : in_ro = false;
1541 : : }
1542 : : }
1543 : : }
1544 : :
1545 : 3061 : printf (" %s",
1546 : 3061 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name));
1547 : :
1548 : : /* Signal that this section is only partially covered. */
1549 [ + + ]: 3061 : if (has_relro && in_relro
1550 [ + + ]: 822 : && shdr->sh_addr + shdr->sh_size > relro_to)
1551 : : {
1552 : 23 : fputs_unlocked (">", stdout);
1553 : 23 : in_relro = false;
1554 : : }
1555 : : }
1556 : : }
1557 [ + + ]: 842 : if (in_relro || in_ro)
1558 : 568 : fputs_unlocked ("]", stdout);
1559 : :
1560 : : /* Finish the line. */
1561 [ - + ]: 1684 : fputc_unlocked ('\n', stdout);
1562 : : }
1563 : : }
1564 : :
1565 : :
1566 : : static const char *
1567 : 413 : section_name (Ebl *ebl, GElf_Shdr *shdr)
1568 : : {
1569 : 413 : size_t shstrndx;
1570 [ + - ]: 413 : if (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0)
1571 : : return "???";
1572 [ - + ]: 413 : return elf_strptr (ebl->elf, shstrndx, shdr->sh_name) ?: "???";
1573 : : }
1574 : :
1575 : :
1576 : : static void
1577 : 16 : handle_scngrp (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr)
1578 : : {
1579 : : /* Get the data of the section. */
1580 : 16 : Elf_Data *data = elf_getdata (scn, NULL);
1581 : :
1582 : 16 : Elf_Scn *symscn = elf_getscn (ebl->elf, shdr->sh_link);
1583 : 16 : GElf_Shdr symshdr_mem;
1584 : 16 : GElf_Shdr *symshdr = gelf_getshdr (symscn, &symshdr_mem);
1585 : 16 : Elf_Data *symdata = elf_getdata (symscn, NULL);
1586 : :
1587 [ + - + - ]: 16 : if (data == NULL || data->d_size < sizeof (Elf32_Word) || symshdr == NULL
1588 [ - + ]: 16 : || symdata == NULL)
1589 : 0 : return;
1590 : :
1591 : : /* Get the section header string table index. */
1592 : 16 : size_t shstrndx;
1593 [ - + ]: 16 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
1594 : 0 : error_exit (0, _("cannot get section header string table index"));
1595 : :
1596 : 16 : Elf32_Word *grpref = (Elf32_Word *) data->d_buf;
1597 : :
1598 : 16 : GElf_Sym sym_mem;
1599 : 16 : GElf_Sym *sym = gelf_getsym (symdata, shdr->sh_info, &sym_mem);
1600 : :
1601 [ + + - + ]: 48 : printf ((grpref[0] & GRP_COMDAT)
1602 : 4 : ? ngettext ("\
1603 : : \nCOMDAT section group [%2zu] '%s' with signature '%s' contains %zu entry:\n",
1604 : : "\
1605 : : \nCOMDAT section group [%2zu] '%s' with signature '%s' contains %zu entries:\n",
1606 : : data->d_size / sizeof (Elf32_Word) - 1)
1607 : 12 : : ngettext ("\
1608 : : \nSection group [%2zu] '%s' with signature '%s' contains %zu entry:\n", "\
1609 : : \nSection group [%2zu] '%s' with signature '%s' contains %zu entries:\n",
1610 : : data->d_size / sizeof (Elf32_Word) - 1),
1611 : : elf_ndxscn (scn),
1612 : 16 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
1613 : : (sym == NULL ? NULL
1614 [ + - ]: 16 : : elf_strptr (ebl->elf, symshdr->sh_link, sym->st_name))
1615 : 0 : ?: _("<INVALID SYMBOL>"),
1616 : 16 : data->d_size / sizeof (Elf32_Word) - 1);
1617 : :
1618 [ + + ]: 52 : for (size_t cnt = 1; cnt < data->d_size / sizeof (Elf32_Word); ++cnt)
1619 : : {
1620 : 36 : GElf_Shdr grpshdr_mem;
1621 : 36 : GElf_Shdr *grpshdr = gelf_getshdr (elf_getscn (ebl->elf, grpref[cnt]),
1622 : : &grpshdr_mem);
1623 : :
1624 : 36 : const char *str;
1625 [ + - ]: 72 : printf (" [%2u] %s\n",
1626 : : grpref[cnt],
1627 : : grpshdr != NULL
1628 [ - + ]: 36 : && (str = elf_strptr (ebl->elf, shstrndx, grpshdr->sh_name))
1629 : 0 : ? str : _("<INVALID SECTION>"));
1630 : : }
1631 : : }
1632 : :
1633 : :
1634 : : static void
1635 : 129 : print_scngrp (Ebl *ebl)
1636 : : {
1637 : : /* Find all relocation sections and handle them. */
1638 : 129 : Elf_Scn *scn = NULL;
1639 : :
1640 [ + + ]: 3744 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
1641 : : {
1642 : : /* Handle the section if it is a symbol table. */
1643 : 3615 : GElf_Shdr shdr_mem;
1644 : 3615 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
1645 : :
1646 [ + - + + ]: 3615 : if (shdr != NULL && shdr->sh_type == SHT_GROUP)
1647 : : {
1648 [ - + ]: 16 : if ((shdr->sh_flags & SHF_COMPRESSED) != 0)
1649 : : {
1650 [ # # ]: 0 : if (elf_compress (scn, 0, 0) < 0)
1651 : 0 : printf ("WARNING: %s [%zd]\n",
1652 : : _("Couldn't uncompress section"),
1653 : : elf_ndxscn (scn));
1654 : 0 : shdr = gelf_getshdr (scn, &shdr_mem);
1655 [ # # ]: 0 : if (unlikely (shdr == NULL))
1656 : 0 : error_exit (0, _("cannot get section [%zd] header: %s"),
1657 : : elf_ndxscn (scn),
1658 : : elf_errmsg (-1));
1659 : : }
1660 : 16 : handle_scngrp (ebl, scn, shdr);
1661 : : }
1662 : : }
1663 : 129 : }
1664 : :
1665 : :
1666 : : static const struct flags
1667 : : {
1668 : : int mask;
1669 : : const char *str;
1670 : : } dt_flags[] =
1671 : : {
1672 : : { DF_ORIGIN, "ORIGIN" },
1673 : : { DF_SYMBOLIC, "SYMBOLIC" },
1674 : : { DF_TEXTREL, "TEXTREL" },
1675 : : { DF_BIND_NOW, "BIND_NOW" },
1676 : : { DF_STATIC_TLS, "STATIC_TLS" }
1677 : : };
1678 : : static const int ndt_flags = sizeof (dt_flags) / sizeof (dt_flags[0]);
1679 : :
1680 : : static const struct flags dt_flags_1[] =
1681 : : {
1682 : : { DF_1_NOW, "NOW" },
1683 : : { DF_1_GLOBAL, "GLOBAL" },
1684 : : { DF_1_GROUP, "GROUP" },
1685 : : { DF_1_NODELETE, "NODELETE" },
1686 : : { DF_1_LOADFLTR, "LOADFLTR" },
1687 : : { DF_1_INITFIRST, "INITFIRST" },
1688 : : { DF_1_NOOPEN, "NOOPEN" },
1689 : : { DF_1_ORIGIN, "ORIGIN" },
1690 : : { DF_1_DIRECT, "DIRECT" },
1691 : : { DF_1_TRANS, "TRANS" },
1692 : : { DF_1_INTERPOSE, "INTERPOSE" },
1693 : : { DF_1_NODEFLIB, "NODEFLIB" },
1694 : : { DF_1_NODUMP, "NODUMP" },
1695 : : { DF_1_CONFALT, "CONFALT" },
1696 : : { DF_1_ENDFILTEE, "ENDFILTEE" },
1697 : : { DF_1_DISPRELDNE, "DISPRELDNE" },
1698 : : { DF_1_DISPRELPND, "DISPRELPND" },
1699 : : };
1700 : : static const int ndt_flags_1 = sizeof (dt_flags_1) / sizeof (dt_flags_1[0]);
1701 : :
1702 : : static const struct flags dt_feature_1[] =
1703 : : {
1704 : : { DTF_1_PARINIT, "PARINIT" },
1705 : : { DTF_1_CONFEXP, "CONFEXP" }
1706 : : };
1707 : : static const int ndt_feature_1 = (sizeof (dt_feature_1)
1708 : : / sizeof (dt_feature_1[0]));
1709 : :
1710 : : static const struct flags dt_posflag_1[] =
1711 : : {
1712 : : { DF_P1_LAZYLOAD, "LAZYLOAD" },
1713 : : { DF_P1_GROUPPERM, "GROUPPERM" }
1714 : : };
1715 : : static const int ndt_posflag_1 = (sizeof (dt_posflag_1)
1716 : : / sizeof (dt_posflag_1[0]));
1717 : :
1718 : :
1719 : : static void
1720 : 37 : print_flags (int class, GElf_Xword d_val, const struct flags *flags,
1721 : : int nflags)
1722 : : {
1723 : 37 : bool first = true;
1724 : 37 : int cnt;
1725 : :
1726 [ + + ]: 552 : for (cnt = 0; cnt < nflags; ++cnt)
1727 [ + + ]: 515 : if (d_val & flags[cnt].mask)
1728 : : {
1729 [ + + ]: 34 : if (!first)
1730 [ - + ]: 18 : putchar_unlocked (' ');
1731 : 34 : fputs_unlocked (flags[cnt].str, stdout);
1732 : 34 : d_val &= ~flags[cnt].mask;
1733 : 34 : first = false;
1734 : : }
1735 : :
1736 [ + + ]: 37 : if (d_val != 0)
1737 : : {
1738 [ + + ]: 31 : if (!first)
1739 [ - + ]: 10 : putchar_unlocked (' ');
1740 [ + + ]: 31 : printf ("%#0*" PRIx64, class == ELFCLASS32 ? 10 : 18, d_val);
1741 : : }
1742 : :
1743 [ - + ]: 37 : putchar_unlocked ('\n');
1744 : 37 : }
1745 : :
1746 : :
1747 : : static void
1748 : 7 : print_dt_flags (int class, GElf_Xword d_val)
1749 : : {
1750 : 7 : print_flags (class, d_val, dt_flags, ndt_flags);
1751 : 7 : }
1752 : :
1753 : :
1754 : : static void
1755 : 28 : print_dt_flags_1 (int class, GElf_Xword d_val)
1756 : : {
1757 : 28 : print_flags (class, d_val, dt_flags_1, ndt_flags_1);
1758 : 28 : }
1759 : :
1760 : :
1761 : : static void
1762 : 1 : print_dt_feature_1 (int class, GElf_Xword d_val)
1763 : : {
1764 : 1 : print_flags (class, d_val, dt_feature_1, ndt_feature_1);
1765 : 1 : }
1766 : :
1767 : :
1768 : : static void
1769 : 1 : print_dt_posflag_1 (int class, GElf_Xword d_val)
1770 : : {
1771 : 1 : print_flags (class, d_val, dt_posflag_1, ndt_posflag_1);
1772 : 1 : }
1773 : :
1774 : :
1775 : : static size_t
1776 : 67 : get_dyn_ents (Elf_Data * dyn_data)
1777 : : {
1778 : 67 : GElf_Dyn *dyn;
1779 : 67 : GElf_Dyn dyn_mem;
1780 : 67 : size_t dyn_idx = 0;
1781 : 1737 : do
1782 : : {
1783 : 1737 : dyn = gelf_getdyn(dyn_data, dyn_idx, &dyn_mem);
1784 [ + - ]: 1737 : if (dyn != NULL)
1785 : 1737 : ++dyn_idx;
1786 : : }
1787 [ + - + + ]: 1737 : while (dyn != NULL && dyn->d_tag != DT_NULL);
1788 : :
1789 : 67 : return dyn_idx;
1790 : : }
1791 : :
1792 : :
1793 : : static void
1794 : 67 : handle_dynamic (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr)
1795 : : {
1796 : 67 : int class = gelf_getclass (ebl->elf);
1797 : 67 : GElf_Shdr glink_mem;
1798 : 67 : GElf_Shdr *glink;
1799 : 67 : Elf_Data *data;
1800 : 67 : size_t cnt;
1801 : 67 : size_t shstrndx;
1802 : 67 : size_t dyn_ents;
1803 : :
1804 : : /* Get the data of the section. */
1805 : 67 : data = elf_getdata (scn, NULL);
1806 [ - + ]: 67 : if (data == NULL)
1807 : 0 : return;
1808 : :
1809 : : /* Get the dynamic section entry number */
1810 : 67 : dyn_ents = get_dyn_ents (data);
1811 : :
1812 : : /* Get the section header string table index. */
1813 [ - + ]: 67 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
1814 : 0 : error_exit (0, _("cannot get section header string table index"));
1815 : :
1816 : 67 : glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link), &glink_mem);
1817 [ - + ]: 67 : if (glink == NULL)
1818 : 0 : error_exit (0, _("invalid sh_link value in section %zu"),
1819 : : elf_ndxscn (scn));
1820 : :
1821 [ + + ]: 134 : printf (ngettext ("\
1822 : : \nDynamic segment contains %lu entry:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'\n",
1823 : : "\
1824 : : \nDynamic segment contains %lu entries:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'\n",
1825 : : dyn_ents),
1826 : : (unsigned long int) dyn_ents,
1827 : : class == ELFCLASS32 ? 10 : 18, shdr->sh_addr,
1828 : : shdr->sh_offset,
1829 : 67 : (int) shdr->sh_link,
1830 : 67 : elf_strptr (ebl->elf, shstrndx, glink->sh_name));
1831 : 67 : fputs_unlocked (_(" Type Value\n"), stdout);
1832 : :
1833 [ + + ]: 1871 : for (cnt = 0; cnt < dyn_ents; ++cnt)
1834 : : {
1835 : 1737 : GElf_Dyn dynmem;
1836 : 1737 : GElf_Dyn *dyn = gelf_getdyn (data, cnt, &dynmem);
1837 [ + - ]: 1737 : if (dyn == NULL)
1838 : : break;
1839 : :
1840 : 1737 : char buf[64];
1841 : 1737 : printf (" %-17s ",
1842 : : ebl_dynamic_tag_name (ebl, dyn->d_tag, buf, sizeof (buf)));
1843 : :
1844 [ + + + + : 1737 : switch (dyn->d_tag)
+ + + + +
+ + + + ]
1845 : : {
1846 : 133 : case DT_NULL:
1847 : : case DT_DEBUG:
1848 : : case DT_BIND_NOW:
1849 : : case DT_TEXTREL:
1850 : : /* No further output. */
1851 [ - + ]: 133 : fputc_unlocked ('\n', stdout);
1852 : : break;
1853 : :
1854 : 118 : case DT_NEEDED:
1855 : 118 : printf (_("Shared library: [%s]\n"),
1856 : 118 : elf_strptr (ebl->elf, shdr->sh_link, dyn->d_un.d_val));
1857 : : break;
1858 : :
1859 : 3 : case DT_SONAME:
1860 : 3 : printf (_("Library soname: [%s]\n"),
1861 : 3 : elf_strptr (ebl->elf, shdr->sh_link, dyn->d_un.d_val));
1862 : : break;
1863 : :
1864 : 1 : case DT_RPATH:
1865 : 1 : printf (_("Library rpath: [%s]\n"),
1866 : 1 : elf_strptr (ebl->elf, shdr->sh_link, dyn->d_un.d_val));
1867 : : break;
1868 : :
1869 : 1 : case DT_RUNPATH:
1870 : 1 : printf (_("Library runpath: [%s]\n"),
1871 : 1 : elf_strptr (ebl->elf, shdr->sh_link, dyn->d_un.d_val));
1872 : : break;
1873 : :
1874 : 460 : case DT_PLTRELSZ:
1875 : : case DT_RELASZ:
1876 : : case DT_STRSZ:
1877 : : case DT_RELSZ:
1878 : : case DT_RELAENT:
1879 : : case DT_SYMENT:
1880 : : case DT_RELENT:
1881 : : case DT_PLTPADSZ:
1882 : : case DT_MOVEENT:
1883 : : case DT_MOVESZ:
1884 : : case DT_INIT_ARRAYSZ:
1885 : : case DT_FINI_ARRAYSZ:
1886 : : case DT_SYMINSZ:
1887 : : case DT_SYMINENT:
1888 : : case DT_GNU_CONFLICTSZ:
1889 : : case DT_GNU_LIBLISTSZ:
1890 : 460 : printf (_("%" PRId64 " (bytes)\n"), dyn->d_un.d_val);
1891 : : break;
1892 : :
1893 : 103 : case DT_VERDEFNUM:
1894 : : case DT_VERNEEDNUM:
1895 : : case DT_RELACOUNT:
1896 : : case DT_RELCOUNT:
1897 : 103 : printf ("%" PRId64 "\n", dyn->d_un.d_val);
1898 : : break;
1899 : :
1900 : 67 : case DT_PLTREL:;
1901 : 67 : const char *tagname = ebl_dynamic_tag_name (ebl, dyn->d_un.d_val,
1902 : : NULL, 0);
1903 [ + + ]: 67 : puts (tagname ?: "???");
1904 : 67 : break;
1905 : :
1906 : 7 : case DT_FLAGS:
1907 : 7 : print_dt_flags (class, dyn->d_un.d_val);
1908 : : break;
1909 : :
1910 : 28 : case DT_FLAGS_1:
1911 : 28 : print_dt_flags_1 (class, dyn->d_un.d_val);
1912 : : break;
1913 : :
1914 : 1 : case DT_FEATURE_1:
1915 : 1 : print_dt_feature_1 (class, dyn->d_un.d_val);
1916 : : break;
1917 : :
1918 : 1 : case DT_POSFLAG_1:
1919 : 1 : print_dt_posflag_1 (class, dyn->d_un.d_val);
1920 : : break;
1921 : :
1922 : : default:
1923 : 1737 : printf ("%#0*" PRIx64 "\n",
1924 : : class == ELFCLASS32 ? 10 : 18, dyn->d_un.d_val);
1925 : : break;
1926 : : }
1927 : : }
1928 : : }
1929 : :
1930 : :
1931 : : /* Print the dynamic segment. */
1932 : : static void
1933 : 122 : print_dynamic (Ebl *ebl)
1934 : : {
1935 [ + + ]: 514 : for (size_t i = 0; i < phnum; ++i)
1936 : : {
1937 : 480 : GElf_Phdr phdr_mem;
1938 : 480 : GElf_Phdr *phdr = gelf_getphdr (ebl->elf, i, &phdr_mem);
1939 : :
1940 [ + - + + ]: 480 : if (phdr != NULL && phdr->p_type == PT_DYNAMIC)
1941 : : {
1942 : 88 : Elf_Scn *scn = gelf_offscn (ebl->elf, phdr->p_offset);
1943 : 88 : GElf_Shdr shdr_mem;
1944 : 88 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
1945 [ + + + + ]: 88 : if (shdr != NULL && shdr->sh_type == SHT_DYNAMIC)
1946 : 67 : handle_dynamic (ebl, scn, shdr);
1947 : 88 : break;
1948 : : }
1949 : : }
1950 : 122 : }
1951 : :
1952 : :
1953 : : /* Print relocations. */
1954 : : static void
1955 : 121 : print_relocs (Ebl *ebl, GElf_Ehdr *ehdr)
1956 : : {
1957 : : /* Find all relocation sections and handle them. */
1958 : 121 : Elf_Scn *scn = NULL;
1959 : :
1960 [ + + ]: 3520 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
1961 : : {
1962 : : /* Handle the section if it is a symbol table. */
1963 : 3399 : GElf_Shdr shdr_mem;
1964 : 3399 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
1965 : :
1966 [ + - ]: 3399 : if (likely (shdr != NULL))
1967 : : {
1968 [ + + ]: 3399 : if (shdr->sh_type == SHT_REL)
1969 : 52 : handle_relocs_rel (ebl, ehdr, scn, shdr);
1970 [ + + ]: 3347 : else if (shdr->sh_type == SHT_RELA)
1971 : 252 : handle_relocs_rela (ebl, ehdr, scn, shdr);
1972 : : }
1973 : : }
1974 : 121 : }
1975 : :
1976 : :
1977 : : /* Handle a relocation section. */
1978 : : static void
1979 : 52 : handle_relocs_rel (Ebl *ebl, GElf_Ehdr *ehdr, Elf_Scn *scn, GElf_Shdr *shdr)
1980 : : {
1981 : 52 : int class = gelf_getclass (ebl->elf);
1982 : 52 : size_t sh_entsize = gelf_fsize (ebl->elf, ELF_T_REL, 1, EV_CURRENT);
1983 : 52 : int nentries = shdr->sh_size / sh_entsize;
1984 : :
1985 : : /* Get the data of the section. */
1986 : 52 : Elf_Data *data = elf_getdata (scn, NULL);
1987 [ + - ]: 52 : if (data == NULL)
1988 : 0 : return;
1989 : :
1990 : : /* Get the symbol table information. */
1991 : 52 : Elf_Scn *symscn = elf_getscn (ebl->elf, shdr->sh_link);
1992 : 52 : GElf_Shdr symshdr_mem;
1993 : 52 : GElf_Shdr *symshdr = gelf_getshdr (symscn, &symshdr_mem);
1994 : 52 : Elf_Data *symdata = elf_getdata (symscn, NULL);
1995 : :
1996 : : /* Get the section header of the section the relocations are for. */
1997 : 52 : GElf_Shdr destshdr_mem;
1998 : 52 : GElf_Shdr *destshdr = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_info),
1999 : : &destshdr_mem);
2000 : :
2001 [ + - - + ]: 52 : if (unlikely (symshdr == NULL || symdata == NULL || destshdr == NULL))
2002 : : {
2003 : 0 : printf (_("\nInvalid symbol table at offset %#0" PRIx64 "\n"),
2004 : : shdr->sh_offset);
2005 : 0 : return;
2006 : : }
2007 : :
2008 : : /* Search for the optional extended section index table. */
2009 : 52 : Elf_Data *xndxdata = NULL;
2010 : 52 : int xndxscnidx = elf_scnshndx (scn);
2011 [ - + ]: 52 : if (unlikely (xndxscnidx > 0))
2012 : 0 : xndxdata = elf_getdata (elf_getscn (ebl->elf, xndxscnidx), NULL);
2013 : :
2014 : : /* Get the section header string table index. */
2015 : 52 : size_t shstrndx;
2016 [ - + ]: 52 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
2017 : 0 : error_exit (0, _("cannot get section header string table index"));
2018 : :
2019 [ + + ]: 52 : if (shdr->sh_info != 0)
2020 : 88 : printf (ngettext ("\
2021 : : \nRelocation section [%2zu] '%s' for section [%2u] '%s' at offset %#0" PRIx64 " contains %d entry:\n",
2022 : : "\
2023 : : \nRelocation section [%2zu] '%s' for section [%2u] '%s' at offset %#0" PRIx64 " contains %d entries:\n",
2024 : : nentries),
2025 : : elf_ndxscn (scn),
2026 : 44 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
2027 : 44 : (unsigned int) shdr->sh_info,
2028 : 44 : elf_strptr (ebl->elf, shstrndx, destshdr->sh_name),
2029 : : shdr->sh_offset,
2030 : : nentries);
2031 : : else
2032 : : /* The .rel.dyn section does not refer to a specific section but
2033 : : instead of section index zero. Do not try to print a section
2034 : : name. */
2035 : 16 : printf (ngettext ("\
2036 : : \nRelocation section [%2u] '%s' at offset %#0" PRIx64 " contains %d entry:\n",
2037 : : "\
2038 : : \nRelocation section [%2u] '%s' at offset %#0" PRIx64 " contains %d entries:\n",
2039 : : nentries),
2040 : 8 : (unsigned int) elf_ndxscn (scn),
2041 : 8 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
2042 : : shdr->sh_offset,
2043 : : nentries);
2044 [ + - ]: 52 : fputs_unlocked (class == ELFCLASS32
2045 : 52 : ? _("\
2046 : : Offset Type Value Name\n")
2047 : 0 : : _("\
2048 : : Offset Type Value Name\n"),
2049 : : stdout);
2050 : :
2051 : 52 : int is_statically_linked = 0;
2052 [ + + ]: 12997 : for (int cnt = 0; cnt < nentries; ++cnt)
2053 : : {
2054 : 12945 : GElf_Rel relmem;
2055 : 12945 : GElf_Rel *rel = gelf_getrel (data, cnt, &relmem);
2056 [ + - ]: 12945 : if (likely (rel != NULL))
2057 : : {
2058 : 12945 : char buf[128];
2059 : 12945 : GElf_Sym symmem;
2060 : 12945 : Elf32_Word xndx;
2061 : 25890 : GElf_Sym *sym = gelf_getsymshndx (symdata, xndxdata,
2062 : 12945 : GELF_R_SYM (rel->r_info),
2063 : : &symmem, &xndx);
2064 [ - + ]: 12945 : if (unlikely (sym == NULL))
2065 : : {
2066 : : /* As a special case we have to handle relocations in static
2067 : : executables. This only happens for IRELATIVE relocations
2068 : : (so far). There is no symbol table. */
2069 [ # # ]: 0 : if (is_statically_linked == 0)
2070 : : {
2071 : : /* Find the program header and look for a PT_INTERP entry. */
2072 : 0 : is_statically_linked = -1;
2073 [ # # ]: 0 : if (ehdr->e_type == ET_EXEC)
2074 : : {
2075 : 0 : is_statically_linked = 1;
2076 : :
2077 [ # # ]: 0 : for (size_t inner = 0; inner < phnum; ++inner)
2078 : : {
2079 : 0 : GElf_Phdr phdr_mem;
2080 : 0 : GElf_Phdr *phdr = gelf_getphdr (ebl->elf, inner,
2081 : : &phdr_mem);
2082 [ # # # # ]: 0 : if (phdr != NULL && phdr->p_type == PT_INTERP)
2083 : : {
2084 : 0 : is_statically_linked = -1;
2085 : 0 : break;
2086 : : }
2087 : : }
2088 : : }
2089 : : }
2090 : :
2091 [ # # # # ]: 0 : if (is_statically_linked > 0 && shdr->sh_link == 0)
2092 [ # # # # ]: 0 : printf ("\
2093 : : %#0*" PRIx64 " %-20s %*s %s\n",
2094 : : class == ELFCLASS32 ? 10 : 18, rel->r_offset,
2095 : 0 : ebl_reloc_type_check (ebl, GELF_R_TYPE (rel->r_info))
2096 : : /* Avoid the leading R_ which isn't carrying any
2097 : : information. */
2098 : 0 : ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info),
2099 : : buf, sizeof (buf)) + 2
2100 : 0 : : _("<INVALID RELOC>"),
2101 : : class == ELFCLASS32 ? 10 : 18, "",
2102 : 0 : elf_strptr (ebl->elf, shstrndx, destshdr->sh_name));
2103 : : else
2104 [ # # # # ]: 0 : printf (" %#0*" PRIx64 " %-20s <%s %ld>\n",
2105 : : class == ELFCLASS32 ? 10 : 18, rel->r_offset,
2106 : 0 : ebl_reloc_type_check (ebl, GELF_R_TYPE (rel->r_info))
2107 : : /* Avoid the leading R_ which isn't carrying any
2108 : : information. */
2109 : 0 : ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info),
2110 : : buf, sizeof (buf)) + 2
2111 : 0 : : _("<INVALID RELOC>"),
2112 : : _("INVALID SYMBOL"),
2113 : 0 : (long int) GELF_R_SYM (rel->r_info));
2114 : : }
2115 [ + + ]: 12945 : else if (GELF_ST_TYPE (sym->st_info) != STT_SECTION)
2116 [ - + ]: 13099 : printf (" %#0*" PRIx64 " %-20s %#0*" PRIx64 " %s\n",
2117 : : class == ELFCLASS32 ? 10 : 18, rel->r_offset,
2118 [ + - ]: 77 : likely (ebl_reloc_type_check (ebl,
2119 : : GELF_R_TYPE (rel->r_info)))
2120 : : /* Avoid the leading R_ which isn't carrying any
2121 : : information. */
2122 : 77 : ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info),
2123 : : buf, sizeof (buf)) + 2
2124 : 0 : : _("<INVALID RELOC>"),
2125 : : class == ELFCLASS32 ? 10 : 18, sym->st_value,
2126 : 77 : elf_strptr (ebl->elf, symshdr->sh_link, sym->st_name));
2127 : : else
2128 : : {
2129 : : /* This is a relocation against a STT_SECTION symbol. */
2130 : 12868 : GElf_Shdr secshdr_mem;
2131 : 12868 : GElf_Shdr *secshdr;
2132 : 12868 : secshdr = gelf_getshdr (elf_getscn (ebl->elf,
2133 [ + - ]: 12868 : sym->st_shndx == SHN_XINDEX
2134 : 0 : ? xndx : sym->st_shndx),
2135 : : &secshdr_mem);
2136 : :
2137 [ - + ]: 12868 : if (unlikely (secshdr == NULL))
2138 [ # # # # ]: 0 : printf (" %#0*" PRIx64 " %-20s <%s %ld>\n",
2139 : : class == ELFCLASS32 ? 10 : 18, rel->r_offset,
2140 : 0 : ebl_reloc_type_check (ebl, GELF_R_TYPE (rel->r_info))
2141 : : /* Avoid the leading R_ which isn't carrying any
2142 : : information. */
2143 : 0 : ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info),
2144 : : buf, sizeof (buf)) + 2
2145 : 0 : : _("<INVALID RELOC>"),
2146 : : _("INVALID SECTION"),
2147 [ # # ]: 0 : (long int) (sym->st_shndx == SHN_XINDEX
2148 : 0 : ? xndx : sym->st_shndx));
2149 : : else
2150 [ - + + - ]: 51472 : printf (" %#0*" PRIx64 " %-20s %#0*" PRIx64 " %s\n",
2151 : : class == ELFCLASS32 ? 10 : 18, rel->r_offset,
2152 : 12868 : ebl_reloc_type_check (ebl, GELF_R_TYPE (rel->r_info))
2153 : : /* Avoid the leading R_ which isn't carrying any
2154 : : information. */
2155 : 12868 : ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info),
2156 : : buf, sizeof (buf)) + 2
2157 : 0 : : _("<INVALID RELOC>"),
2158 : : class == ELFCLASS32 ? 10 : 18, sym->st_value,
2159 : 12868 : elf_strptr (ebl->elf, shstrndx, secshdr->sh_name));
2160 : : }
2161 : : }
2162 : : }
2163 : : }
2164 : :
2165 : :
2166 : : /* Handle a relocation section. */
2167 : : static void
2168 : 252 : handle_relocs_rela (Ebl *ebl, GElf_Ehdr *ehdr, Elf_Scn *scn, GElf_Shdr *shdr)
2169 : : {
2170 : 252 : int class = gelf_getclass (ebl->elf);
2171 : 252 : size_t sh_entsize = gelf_fsize (ebl->elf, ELF_T_RELA, 1, EV_CURRENT);
2172 : 252 : int nentries = shdr->sh_size / sh_entsize;
2173 : :
2174 : : /* Get the data of the section. */
2175 : 252 : Elf_Data *data = elf_getdata (scn, NULL);
2176 [ + - ]: 252 : if (data == NULL)
2177 : 0 : return;
2178 : :
2179 : : /* Get the symbol table information. */
2180 : 252 : Elf_Scn *symscn = elf_getscn (ebl->elf, shdr->sh_link);
2181 : 252 : GElf_Shdr symshdr_mem;
2182 : 252 : GElf_Shdr *symshdr = gelf_getshdr (symscn, &symshdr_mem);
2183 : 252 : Elf_Data *symdata = elf_getdata (symscn, NULL);
2184 : :
2185 : : /* Get the section header of the section the relocations are for. */
2186 : 252 : GElf_Shdr destshdr_mem;
2187 : 252 : GElf_Shdr *destshdr = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_info),
2188 : : &destshdr_mem);
2189 : :
2190 [ + - - + ]: 252 : if (unlikely (symshdr == NULL || symdata == NULL || destshdr == NULL))
2191 : : {
2192 : 0 : printf (_("\nInvalid symbol table at offset %#0" PRIx64 "\n"),
2193 : : shdr->sh_offset);
2194 : 0 : return;
2195 : : }
2196 : :
2197 : : /* Search for the optional extended section index table. */
2198 : 252 : Elf_Data *xndxdata = NULL;
2199 : 252 : int xndxscnidx = elf_scnshndx (scn);
2200 [ - + ]: 252 : if (unlikely (xndxscnidx > 0))
2201 : 0 : xndxdata = elf_getdata (elf_getscn (ebl->elf, xndxscnidx), NULL);
2202 : :
2203 : : /* Get the section header string table index. */
2204 : 252 : size_t shstrndx;
2205 [ - + ]: 252 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
2206 : 0 : error_exit (0, _("cannot get section header string table index"));
2207 : :
2208 [ + + ]: 252 : if (shdr->sh_info != 0)
2209 : 390 : printf (ngettext ("\
2210 : : \nRelocation section [%2zu] '%s' for section [%2u] '%s' at offset %#0" PRIx64 " contains %d entry:\n",
2211 : : "\
2212 : : \nRelocation section [%2zu] '%s' for section [%2u] '%s' at offset %#0" PRIx64 " contains %d entries:\n",
2213 : : nentries),
2214 : : elf_ndxscn (scn),
2215 : 195 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
2216 : 195 : (unsigned int) shdr->sh_info,
2217 : 195 : elf_strptr (ebl->elf, shstrndx, destshdr->sh_name),
2218 : : shdr->sh_offset,
2219 : : nentries);
2220 : : else
2221 : : /* The .rela.dyn section does not refer to a specific section but
2222 : : instead of section index zero. Do not try to print a section
2223 : : name. */
2224 : 114 : printf (ngettext ("\
2225 : : \nRelocation section [%2u] '%s' at offset %#0" PRIx64 " contains %d entry:\n",
2226 : : "\
2227 : : \nRelocation section [%2u] '%s' at offset %#0" PRIx64 " contains %d entries:\n",
2228 : : nentries),
2229 : 57 : (unsigned int) elf_ndxscn (scn),
2230 : 57 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
2231 : : shdr->sh_offset,
2232 : : nentries);
2233 [ + + ]: 252 : fputs_unlocked (class == ELFCLASS32
2234 : 10 : ? _("\
2235 : : Offset Type Value Addend Name\n")
2236 : 242 : : _("\
2237 : : Offset Type Value Addend Name\n"),
2238 : : stdout);
2239 : :
2240 : 252 : int is_statically_linked = 0;
2241 [ + + ]: 82483 : for (int cnt = 0; cnt < nentries; ++cnt)
2242 : : {
2243 : 82231 : GElf_Rela relmem;
2244 : 82231 : GElf_Rela *rel = gelf_getrela (data, cnt, &relmem);
2245 [ + - ]: 82231 : if (likely (rel != NULL))
2246 : : {
2247 : 82231 : char buf[64];
2248 : 82231 : GElf_Sym symmem;
2249 : 82231 : Elf32_Word xndx;
2250 : 164462 : GElf_Sym *sym = gelf_getsymshndx (symdata, xndxdata,
2251 : 82231 : GELF_R_SYM (rel->r_info),
2252 : : &symmem, &xndx);
2253 : :
2254 [ - + ]: 82231 : if (unlikely (sym == NULL))
2255 : : {
2256 : : /* As a special case we have to handle relocations in static
2257 : : executables. This only happens for IRELATIVE relocations
2258 : : (so far). There is no symbol table. */
2259 [ # # ]: 0 : if (is_statically_linked == 0)
2260 : : {
2261 : : /* Find the program header and look for a PT_INTERP entry. */
2262 : 0 : is_statically_linked = -1;
2263 [ # # ]: 0 : if (ehdr->e_type == ET_EXEC)
2264 : : {
2265 : 0 : is_statically_linked = 1;
2266 : :
2267 [ # # ]: 0 : for (size_t inner = 0; inner < phnum; ++inner)
2268 : : {
2269 : 0 : GElf_Phdr phdr_mem;
2270 : 0 : GElf_Phdr *phdr = gelf_getphdr (ebl->elf, inner,
2271 : : &phdr_mem);
2272 [ # # # # ]: 0 : if (phdr != NULL && phdr->p_type == PT_INTERP)
2273 : : {
2274 : 0 : is_statically_linked = -1;
2275 : 0 : break;
2276 : : }
2277 : : }
2278 : : }
2279 : : }
2280 : :
2281 [ # # # # ]: 0 : if (is_statically_linked > 0 && shdr->sh_link == 0)
2282 [ # # # # ]: 0 : printf ("\
2283 : : %#0*" PRIx64 " %-15s %*s %#6" PRIx64 " %s\n",
2284 : : class == ELFCLASS32 ? 10 : 18, rel->r_offset,
2285 : 0 : ebl_reloc_type_check (ebl, GELF_R_TYPE (rel->r_info))
2286 : : /* Avoid the leading R_ which isn't carrying any
2287 : : information. */
2288 : 0 : ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info),
2289 : : buf, sizeof (buf)) + 2
2290 : 0 : : _("<INVALID RELOC>"),
2291 : : class == ELFCLASS32 ? 10 : 18, "",
2292 : : rel->r_addend,
2293 : 0 : elf_strptr (ebl->elf, shstrndx, destshdr->sh_name));
2294 : : else
2295 [ # # # # ]: 0 : printf (" %#0*" PRIx64 " %-15s <%s %ld>\n",
2296 : : class == ELFCLASS32 ? 10 : 18, rel->r_offset,
2297 : 0 : ebl_reloc_type_check (ebl, GELF_R_TYPE (rel->r_info))
2298 : : /* Avoid the leading R_ which isn't carrying any
2299 : : information. */
2300 : 0 : ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info),
2301 : : buf, sizeof (buf)) + 2
2302 : 0 : : _("<INVALID RELOC>"),
2303 : : _("INVALID SYMBOL"),
2304 : 0 : (long int) GELF_R_SYM (rel->r_info));
2305 : : }
2306 [ + + ]: 82231 : else if (GELF_ST_TYPE (sym->st_info) != STT_SECTION)
2307 [ + + ]: 208543 : printf ("\
2308 : : %#0*" PRIx64 " %-15s %#0*" PRIx64 " %+6" PRId64 " %s\n",
2309 : : class == ELFCLASS32 ? 10 : 18, rel->r_offset,
2310 [ + - ]: 63156 : likely (ebl_reloc_type_check (ebl,
2311 : : GELF_R_TYPE (rel->r_info)))
2312 : : /* Avoid the leading R_ which isn't carrying any
2313 : : information. */
2314 : 63156 : ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info),
2315 : : buf, sizeof (buf)) + 2
2316 : 0 : : _("<INVALID RELOC>"),
2317 : : class == ELFCLASS32 ? 10 : 18, sym->st_value,
2318 : : rel->r_addend,
2319 : 63156 : elf_strptr (ebl->elf, symshdr->sh_link, sym->st_name));
2320 : : else
2321 : : {
2322 : : /* This is a relocation against a STT_SECTION symbol. */
2323 : 19075 : GElf_Shdr secshdr_mem;
2324 : 19075 : GElf_Shdr *secshdr;
2325 : 19075 : secshdr = gelf_getshdr (elf_getscn (ebl->elf,
2326 [ + - ]: 19075 : sym->st_shndx == SHN_XINDEX
2327 : 0 : ? xndx : sym->st_shndx),
2328 : : &secshdr_mem);
2329 : :
2330 [ - + ]: 19075 : if (unlikely (secshdr == NULL))
2331 [ # # # # ]: 0 : printf (" %#0*" PRIx64 " %-15s <%s %ld>\n",
2332 : : class == ELFCLASS32 ? 10 : 18, rel->r_offset,
2333 : 0 : ebl_reloc_type_check (ebl, GELF_R_TYPE (rel->r_info))
2334 : : /* Avoid the leading R_ which isn't carrying any
2335 : : information. */
2336 : 0 : ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info),
2337 : : buf, sizeof (buf)) + 2
2338 : 0 : : _("<INVALID RELOC>"),
2339 : : _("INVALID SECTION"),
2340 [ # # ]: 0 : (long int) (sym->st_shndx == SHN_XINDEX
2341 : 0 : ? xndx : sym->st_shndx));
2342 : : else
2343 [ + + + - ]: 76300 : printf ("\
2344 : : %#0*" PRIx64 " %-15s %#0*" PRIx64 " %+6" PRId64 " %s\n",
2345 : : class == ELFCLASS32 ? 10 : 18, rel->r_offset,
2346 : 19075 : ebl_reloc_type_check (ebl, GELF_R_TYPE (rel->r_info))
2347 : : /* Avoid the leading R_ which isn't carrying any
2348 : : information. */
2349 : 19075 : ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info),
2350 : : buf, sizeof (buf)) + 2
2351 : 0 : : _("<INVALID RELOC>"),
2352 : : class == ELFCLASS32 ? 10 : 18, sym->st_value,
2353 : : rel->r_addend,
2354 : 19075 : elf_strptr (ebl->elf, shstrndx, secshdr->sh_name));
2355 : : }
2356 : : }
2357 : : }
2358 : : }
2359 : :
2360 : :
2361 : : /* Print the program header. */
2362 : : static void
2363 : 269 : print_symtab (Ebl *ebl, int type)
2364 : : {
2365 : : /* Find the symbol table(s). For this we have to search through the
2366 : : section table. */
2367 : 269 : Elf_Scn *scn = NULL;
2368 : :
2369 [ + + ]: 7792 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
2370 : : {
2371 : : /* Handle the section if it is a symbol table. */
2372 : 7523 : GElf_Shdr shdr_mem;
2373 : 7523 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
2374 : :
2375 [ + - + + ]: 7523 : if (shdr != NULL && shdr->sh_type == (GElf_Word) type)
2376 : : {
2377 [ + + ]: 158 : if (symbol_table_section != NULL)
2378 : : {
2379 : : /* Get the section header string table index. */
2380 : 4 : size_t shstrndx;
2381 : 4 : const char *sname;
2382 [ - + ]: 4 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
2383 : 0 : error_exit (0,
2384 : : _("cannot get section header string table index"));
2385 : 4 : sname = elf_strptr (ebl->elf, shstrndx, shdr->sh_name);
2386 [ + - + + ]: 4 : if (sname == NULL || strcmp (sname, symbol_table_section) != 0)
2387 : 2 : continue;
2388 : : }
2389 : :
2390 [ - + ]: 156 : if ((shdr->sh_flags & SHF_COMPRESSED) != 0)
2391 : : {
2392 [ # # ]: 0 : if (elf_compress (scn, 0, 0) < 0)
2393 : 0 : printf ("WARNING: %s [%zd]\n",
2394 : : _("Couldn't uncompress section"),
2395 : : elf_ndxscn (scn));
2396 : 0 : shdr = gelf_getshdr (scn, &shdr_mem);
2397 [ # # ]: 0 : if (unlikely (shdr == NULL))
2398 : 0 : error_exit (0,
2399 : : _("cannot get section [%zd] header: %s"),
2400 : : elf_ndxscn (scn), elf_errmsg (-1));
2401 : : }
2402 : 156 : handle_symtab (ebl, scn, shdr);
2403 : : }
2404 : : }
2405 : 269 : }
2406 : :
2407 : :
2408 : : static void
2409 : 156 : handle_symtab (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr)
2410 : : {
2411 : 156 : Elf_Data *versym_data = NULL;
2412 : 156 : Elf_Data *verneed_data = NULL;
2413 : 156 : Elf_Data *verdef_data = NULL;
2414 : 156 : Elf_Data *xndx_data = NULL;
2415 : 156 : int class = gelf_getclass (ebl->elf);
2416 : 156 : Elf32_Word verneed_stridx = 0;
2417 : 156 : Elf32_Word verdef_stridx = 0;
2418 : :
2419 : : /* Get the data of the section. */
2420 : 156 : Elf_Data *data = elf_getdata (scn, NULL);
2421 [ - + ]: 156 : if (data == NULL)
2422 : 0 : return;
2423 : :
2424 : : /* Find out whether we have other sections we might need. */
2425 : : Elf_Scn *runscn = NULL;
2426 [ + + ]: 4660 : while ((runscn = elf_nextscn (ebl->elf, runscn)) != NULL)
2427 : : {
2428 : 4504 : GElf_Shdr runshdr_mem;
2429 : 4504 : GElf_Shdr *runshdr = gelf_getshdr (runscn, &runshdr_mem);
2430 : :
2431 [ + - ]: 4504 : if (likely (runshdr != NULL))
2432 : : {
2433 [ + + ]: 4504 : if (runshdr->sh_type == SHT_GNU_versym
2434 [ + + ]: 96 : && runshdr->sh_link == elf_ndxscn (scn))
2435 : : /* Bingo, found the version information. Now get the data. */
2436 : 73 : versym_data = elf_getdata (runscn, NULL);
2437 [ + + ]: 4431 : else if (runshdr->sh_type == SHT_GNU_verneed)
2438 : : {
2439 : : /* This is the information about the needed versions. */
2440 : 96 : verneed_data = elf_getdata (runscn, NULL);
2441 : 96 : verneed_stridx = runshdr->sh_link;
2442 : : }
2443 [ + + ]: 4335 : else if (runshdr->sh_type == SHT_GNU_verdef)
2444 : : {
2445 : : /* This is the information about the defined versions. */
2446 : 4 : verdef_data = elf_getdata (runscn, NULL);
2447 : 4 : verdef_stridx = runshdr->sh_link;
2448 : : }
2449 [ - + ]: 4331 : else if (runshdr->sh_type == SHT_SYMTAB_SHNDX
2450 [ # # ]: 0 : && runshdr->sh_link == elf_ndxscn (scn))
2451 : : /* Extended section index. */
2452 : 0 : xndx_data = elf_getdata (runscn, NULL);
2453 : : }
2454 : : }
2455 : :
2456 : : /* Get the section header string table index. */
2457 : 156 : size_t shstrndx;
2458 [ - + ]: 156 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
2459 : 0 : error_exit (0, _("cannot get section header string table index"));
2460 : :
2461 : 156 : GElf_Shdr glink_mem;
2462 : 156 : GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
2463 : : &glink_mem);
2464 [ - + ]: 156 : if (glink == NULL)
2465 : 0 : error_exit (0, _("invalid sh_link value in section %zu"),
2466 : : elf_ndxscn (scn));
2467 : :
2468 : : /* Now we can compute the number of entries in the section. */
2469 : 312 : unsigned int nsyms = data->d_size / (class == ELFCLASS32
2470 : : ? sizeof (Elf32_Sym)
2471 [ + + ]: 156 : : sizeof (Elf64_Sym));
2472 : :
2473 : 312 : printf (ngettext ("\nSymbol table [%2u] '%s' contains %u entry:\n",
2474 : : "\nSymbol table [%2u] '%s' contains %u entries:\n",
2475 : : nsyms),
2476 : 156 : (unsigned int) elf_ndxscn (scn),
2477 : 156 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name), nsyms);
2478 : 312 : printf (ngettext (" %lu local symbol String table: [%2u] '%s'\n",
2479 : : " %lu local symbols String table: [%2u] '%s'\n",
2480 : : shdr->sh_info),
2481 : 156 : (unsigned long int) shdr->sh_info,
2482 : 156 : (unsigned int) shdr->sh_link,
2483 : 156 : elf_strptr (ebl->elf, shstrndx, glink->sh_name));
2484 : :
2485 [ + + ]: 156 : fputs_unlocked (class == ELFCLASS32
2486 : 22 : ? _("\
2487 : : Num: Value Size Type Bind Vis Ndx Name\n")
2488 : 134 : : _("\
2489 : : Num: Value Size Type Bind Vis Ndx Name\n"),
2490 : : stdout);
2491 : :
2492 [ + + ]: 58875 : for (unsigned int cnt = 0; cnt < nsyms; ++cnt)
2493 : : {
2494 : 58719 : char typebuf[64];
2495 : 58719 : char bindbuf[64];
2496 : 58719 : char scnbuf[64];
2497 : 58719 : Elf32_Word xndx;
2498 : 58719 : GElf_Sym sym_mem;
2499 : 58719 : GElf_Sym *sym = gelf_getsymshndx (data, xndx_data, cnt, &sym_mem, &xndx);
2500 : :
2501 [ - + ]: 58719 : if (unlikely (sym == NULL))
2502 : 0 : continue;
2503 : :
2504 : : /* Determine the real section index. */
2505 [ + - ]: 58719 : if (likely (sym->st_shndx != SHN_XINDEX))
2506 : 58719 : xndx = sym->st_shndx;
2507 : :
2508 [ + + ]: 117438 : printf (_("\
2509 : : %5u: %0*" PRIx64 " %6" PRId64 " %-7s %-6s %-9s %6s %s"),
2510 : : cnt,
2511 : : class == ELFCLASS32 ? 8 : 16,
2512 : : sym->st_value,
2513 : : sym->st_size,
2514 : 58719 : ebl_symbol_type_name (ebl, GELF_ST_TYPE (sym->st_info),
2515 : : typebuf, sizeof (typebuf)),
2516 : 58719 : ebl_symbol_binding_name (ebl, GELF_ST_BIND (sym->st_info),
2517 : : bindbuf, sizeof (bindbuf)),
2518 : 58719 : get_visibility_type (GELF_ST_VISIBILITY (sym->st_other)),
2519 : 58719 : ebl_section_name (ebl, sym->st_shndx, xndx, scnbuf,
2520 : : sizeof (scnbuf), NULL, shnum),
2521 : 58719 : elf_strptr (ebl->elf, shdr->sh_link, sym->st_name));
2522 : :
2523 [ + + ]: 58719 : if (versym_data != NULL)
2524 : : {
2525 : : /* Get the version information. */
2526 : 1879 : GElf_Versym versym_mem;
2527 : 1879 : GElf_Versym *versym = gelf_getversym (versym_data, cnt, &versym_mem);
2528 : :
2529 [ + - + + ]: 1879 : if (versym != NULL && ((*versym & 0x8000) != 0 || *versym > 1))
2530 : : {
2531 : 1390 : bool is_nobits = false;
2532 : 1390 : bool check_def = xndx != SHN_UNDEF;
2533 : :
2534 [ + + - + ]: 1390 : if (xndx < SHN_LORESERVE || sym->st_shndx == SHN_XINDEX)
2535 : : {
2536 : 1380 : GElf_Shdr symshdr_mem;
2537 : 1380 : GElf_Shdr *symshdr =
2538 : 1380 : gelf_getshdr (elf_getscn (ebl->elf, xndx), &symshdr_mem);
2539 : :
2540 : 1380 : is_nobits = (symshdr != NULL
2541 [ + - + - ]: 1380 : && symshdr->sh_type == SHT_NOBITS);
2542 : : }
2543 : :
2544 [ + + ]: 1390 : if (is_nobits || ! check_def)
2545 : : {
2546 : : /* We must test both. */
2547 : 1238 : GElf_Vernaux vernaux_mem;
2548 : 1238 : GElf_Vernaux *vernaux = NULL;
2549 : 1238 : size_t vn_offset = 0;
2550 : :
2551 : 1238 : GElf_Verneed verneed_mem;
2552 : 1238 : GElf_Verneed *verneed = gelf_getverneed (verneed_data, 0,
2553 : : &verneed_mem);
2554 [ + - ]: 4613 : while (verneed != NULL)
2555 : : {
2556 : 4613 : size_t vna_offset = vn_offset;
2557 : :
2558 : 4613 : vernaux = gelf_getvernaux (verneed_data,
2559 : 4613 : vna_offset += verneed->vn_aux,
2560 : : &vernaux_mem);
2561 [ + - ]: 4661 : while (vernaux != NULL
2562 [ + + ]: 4661 : && vernaux->vna_other != *versym
2563 [ + + ]: 3423 : && vernaux->vna_next != 0
2564 : 48 : && (verneed_data->d_size - vna_offset
2565 [ + - ]: 48 : >= vernaux->vna_next))
2566 : : {
2567 : : /* Update the offset. */
2568 : 48 : vna_offset += vernaux->vna_next;
2569 : :
2570 : 48 : vernaux = (vernaux->vna_next == 0
2571 : : ? NULL
2572 : 48 : : gelf_getvernaux (verneed_data,
2573 : : vna_offset,
2574 : : &vernaux_mem));
2575 : : }
2576 : :
2577 : : /* Check whether we found the version. */
2578 [ + - + + ]: 4613 : if (vernaux != NULL && vernaux->vna_other == *versym)
2579 : : /* Found it. */
2580 : : break;
2581 : :
2582 [ + - ]: 3375 : if (verneed_data->d_size - vn_offset < verneed->vn_next)
2583 : : break;
2584 : :
2585 : 3375 : vn_offset += verneed->vn_next;
2586 : 3375 : verneed = (verneed->vn_next == 0
2587 : : ? NULL
2588 [ + - ]: 3375 : : gelf_getverneed (verneed_data, vn_offset,
2589 : : &verneed_mem));
2590 : : }
2591 : :
2592 [ + - + - ]: 1238 : if (vernaux != NULL && vernaux->vna_other == *versym)
2593 : : {
2594 : 3714 : printf ("@%s (%u)",
2595 : : elf_strptr (ebl->elf, verneed_stridx,
2596 : 1238 : vernaux->vna_name),
2597 : 1238 : (unsigned int) vernaux->vna_other);
2598 : 1238 : check_def = 0;
2599 : : }
2600 [ # # ]: 0 : else if (unlikely (! is_nobits))
2601 : 0 : error (0, 0, _("bad dynamic symbol"));
2602 : : else
2603 : : check_def = 1;
2604 : : }
2605 : :
2606 [ + - + - ]: 1390 : if (check_def && *versym != 0x8001)
2607 : : {
2608 : : /* We must test both. */
2609 : 152 : size_t vd_offset = 0;
2610 : :
2611 : 152 : GElf_Verdef verdef_mem;
2612 : 152 : GElf_Verdef *verdef = gelf_getverdef (verdef_data, 0,
2613 : : &verdef_mem);
2614 [ + - ]: 433 : while (verdef != NULL)
2615 : : {
2616 [ + + ]: 433 : if (verdef->vd_ndx == (*versym & 0x7fff))
2617 : : /* Found the definition. */
2618 : : break;
2619 : :
2620 [ + - ]: 281 : if (verdef_data->d_size - vd_offset < verdef->vd_next)
2621 : : break;
2622 : :
2623 : 281 : vd_offset += verdef->vd_next;
2624 : 281 : verdef = (verdef->vd_next == 0
2625 : : ? NULL
2626 [ + - ]: 281 : : gelf_getverdef (verdef_data, vd_offset,
2627 : : &verdef_mem));
2628 : : }
2629 : :
2630 [ + - ]: 152 : if (verdef != NULL)
2631 : : {
2632 : 152 : GElf_Verdaux verdaux_mem;
2633 : 152 : GElf_Verdaux *verdaux
2634 : 304 : = gelf_getverdaux (verdef_data,
2635 : 152 : vd_offset + verdef->vd_aux,
2636 : : &verdaux_mem);
2637 : :
2638 [ + - ]: 152 : if (verdaux != NULL)
2639 [ + - ]: 304 : printf ((*versym & 0x8000) ? "@%s" : "@@%s",
2640 : : elf_strptr (ebl->elf, verdef_stridx,
2641 : 152 : verdaux->vda_name));
2642 : : }
2643 : : }
2644 : : }
2645 : : }
2646 : :
2647 [ + + ]: 117438 : putchar_unlocked ('\n');
2648 : : }
2649 : : }
2650 : :
2651 : :
2652 : : /* Print version information. */
2653 : : static void
2654 : 120 : print_verinfo (Ebl *ebl)
2655 : : {
2656 : : /* Find the version information sections. For this we have to
2657 : : search through the section table. */
2658 : 120 : Elf_Scn *scn = NULL;
2659 : :
2660 [ + + ]: 3485 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
2661 : : {
2662 : : /* Handle the section if it is part of the versioning handling. */
2663 : 3365 : GElf_Shdr shdr_mem;
2664 : 3365 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
2665 : :
2666 [ + - ]: 3365 : if (likely (shdr != NULL))
2667 : : {
2668 [ + + ]: 3365 : if (shdr->sh_type == SHT_GNU_verneed)
2669 : 65 : handle_verneed (ebl, scn, shdr);
2670 [ + + ]: 3300 : else if (shdr->sh_type == SHT_GNU_verdef)
2671 : 2 : handle_verdef (ebl, scn, shdr);
2672 [ + + ]: 3298 : else if (shdr->sh_type == SHT_GNU_versym)
2673 : 65 : handle_versym (ebl, scn, shdr);
2674 : : }
2675 : : }
2676 : 120 : }
2677 : :
2678 : :
2679 : : static const char *
2680 : 111 : get_ver_flags (unsigned int flags)
2681 : : {
2682 : 111 : static char buf[32];
2683 : 111 : char *endp;
2684 : :
2685 [ + + ]: 111 : if (flags == 0)
2686 : 109 : return _("none");
2687 : :
2688 [ + - ]: 2 : if (flags & VER_FLG_BASE)
2689 : 2 : endp = stpcpy (buf, "BASE ");
2690 : : else
2691 : : endp = buf;
2692 : :
2693 [ - + ]: 2 : if (flags & VER_FLG_WEAK)
2694 : : {
2695 [ # # ]: 0 : if (endp != buf)
2696 : 0 : endp = stpcpy (endp, "| ");
2697 : :
2698 : 0 : endp = stpcpy (endp, "WEAK ");
2699 : : }
2700 : :
2701 [ - + ]: 2 : if (unlikely (flags & ~(VER_FLG_BASE | VER_FLG_WEAK)))
2702 : : {
2703 : 0 : strncpy (endp, _("| <unknown>"), buf + sizeof (buf) - endp);
2704 : 0 : buf[sizeof (buf) - 1] = '\0';
2705 : : }
2706 : :
2707 : : return buf;
2708 : : }
2709 : :
2710 : :
2711 : : static void
2712 : 65 : handle_verneed (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr)
2713 : : {
2714 : 65 : int class = gelf_getclass (ebl->elf);
2715 : :
2716 : : /* Get the data of the section. */
2717 : 65 : Elf_Data *data = elf_getdata (scn, NULL);
2718 [ - + ]: 65 : if (data == NULL)
2719 : 0 : return;
2720 : :
2721 : : /* Get the section header string table index. */
2722 : 65 : size_t shstrndx;
2723 [ - + ]: 65 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
2724 : 0 : error_exit (0, _("cannot get section header string table index"));
2725 : :
2726 : 65 : GElf_Shdr glink_mem;
2727 : 65 : GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
2728 : : &glink_mem);
2729 [ - + ]: 65 : if (glink == NULL)
2730 : 0 : error_exit (0, _("invalid sh_link value in section %zu"),
2731 : : elf_ndxscn (scn));
2732 : :
2733 [ + + ]: 195 : printf (ngettext ("\
2734 : : \nVersion needs section [%2u] '%s' contains %d entry:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'\n",
2735 : : "\
2736 : : \nVersion needs section [%2u] '%s' contains %d entries:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'\n",
2737 : : shdr->sh_info),
2738 : 65 : (unsigned int) elf_ndxscn (scn),
2739 : 65 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name), shdr->sh_info,
2740 : : class == ELFCLASS32 ? 10 : 18, shdr->sh_addr,
2741 : : shdr->sh_offset,
2742 : 65 : (unsigned int) shdr->sh_link,
2743 : 65 : elf_strptr (ebl->elf, shstrndx, glink->sh_name));
2744 : :
2745 : 65 : unsigned int offset = 0;
2746 [ + - ]: 98 : for (int cnt = shdr->sh_info; --cnt >= 0; )
2747 : : {
2748 : : /* Get the data at the next offset. */
2749 : 98 : GElf_Verneed needmem;
2750 : 98 : GElf_Verneed *need = gelf_getverneed (data, offset, &needmem);
2751 [ + - ]: 98 : if (unlikely (need == NULL))
2752 : : break;
2753 : :
2754 : 294 : printf (_(" %#06x: Version: %hu File: %s Cnt: %hu\n"),
2755 : 98 : offset, (unsigned short int) need->vn_version,
2756 : 98 : elf_strptr (ebl->elf, shdr->sh_link, need->vn_file),
2757 : 98 : (unsigned short int) need->vn_cnt);
2758 : :
2759 : 98 : unsigned int auxoffset = offset + need->vn_aux;
2760 [ + - ]: 99 : for (int cnt2 = need->vn_cnt; --cnt2 >= 0; )
2761 : : {
2762 : 99 : GElf_Vernaux auxmem;
2763 : 99 : GElf_Vernaux *aux = gelf_getvernaux (data, auxoffset, &auxmem);
2764 [ + - ]: 99 : if (unlikely (aux == NULL))
2765 : : break;
2766 : :
2767 : 396 : printf (_(" %#06x: Name: %s Flags: %s Version: %hu\n"),
2768 : : auxoffset,
2769 : 99 : elf_strptr (ebl->elf, shdr->sh_link, aux->vna_name),
2770 : 99 : get_ver_flags (aux->vna_flags),
2771 : 99 : (unsigned short int) aux->vna_other);
2772 : :
2773 [ + + ]: 99 : if (aux->vna_next == 0)
2774 : : break;
2775 : :
2776 : 1 : auxoffset += aux->vna_next;
2777 : : }
2778 : :
2779 : : /* Find the next offset. */
2780 [ + + ]: 98 : if (need->vn_next == 0)
2781 : : break;
2782 : :
2783 : 33 : offset += need->vn_next;
2784 : : }
2785 : : }
2786 : :
2787 : :
2788 : : static void
2789 : 2 : handle_verdef (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr)
2790 : : {
2791 : : /* Get the data of the section. */
2792 : 2 : Elf_Data *data = elf_getdata (scn, NULL);
2793 [ - + ]: 2 : if (data == NULL)
2794 : 0 : return;
2795 : :
2796 : : /* Get the section header string table index. */
2797 : 2 : size_t shstrndx;
2798 [ - + ]: 2 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
2799 : 0 : error_exit (0, _("cannot get section header string table index"));
2800 : :
2801 : 2 : GElf_Shdr glink_mem;
2802 : 2 : GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
2803 : : &glink_mem);
2804 [ - + ]: 2 : if (glink == NULL)
2805 : 0 : error_exit (0, _("invalid sh_link value in section %zu"),
2806 : : elf_ndxscn (scn));
2807 : :
2808 : 2 : int class = gelf_getclass (ebl->elf);
2809 [ + - ]: 6 : printf (ngettext ("\
2810 : : \nVersion definition section [%2u] '%s' contains %d entry:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'\n",
2811 : : "\
2812 : : \nVersion definition section [%2u] '%s' contains %d entries:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'\n",
2813 : : shdr->sh_info),
2814 : 2 : (unsigned int) elf_ndxscn (scn),
2815 : 2 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
2816 : : shdr->sh_info,
2817 : : class == ELFCLASS32 ? 10 : 18, shdr->sh_addr,
2818 : : shdr->sh_offset,
2819 : 2 : (unsigned int) shdr->sh_link,
2820 : 2 : elf_strptr (ebl->elf, shstrndx, glink->sh_name));
2821 : :
2822 : 2 : unsigned int offset = 0;
2823 [ + - ]: 12 : for (int cnt = shdr->sh_info; --cnt >= 0; )
2824 : : {
2825 : : /* Get the data at the next offset. */
2826 : 12 : GElf_Verdef defmem;
2827 : 12 : GElf_Verdef *def = gelf_getverdef (data, offset, &defmem);
2828 [ + - ]: 12 : if (unlikely (def == NULL))
2829 : : break;
2830 : :
2831 : 12 : unsigned int auxoffset = offset + def->vd_aux;
2832 : 12 : GElf_Verdaux auxmem;
2833 : 12 : GElf_Verdaux *aux = gelf_getverdaux (data, auxoffset, &auxmem);
2834 [ + - ]: 12 : if (unlikely (aux == NULL))
2835 : : break;
2836 : :
2837 : 12 : printf (_("\
2838 : : %#06x: Version: %hd Flags: %s Index: %hd Cnt: %hd Name: %s\n"),
2839 : 12 : offset, def->vd_version,
2840 : 12 : get_ver_flags (def->vd_flags),
2841 : 12 : def->vd_ndx,
2842 : 12 : def->vd_cnt,
2843 : 12 : elf_strptr (ebl->elf, shdr->sh_link, aux->vda_name));
2844 : :
2845 : 12 : auxoffset += aux->vda_next;
2846 [ + + ]: 12 : for (int cnt2 = 1; cnt2 < def->vd_cnt; ++cnt2)
2847 : : {
2848 : 8 : aux = gelf_getverdaux (data, auxoffset, &auxmem);
2849 [ + - ]: 8 : if (unlikely (aux == NULL))
2850 : : break;
2851 : :
2852 : 8 : printf (_(" %#06x: Parent %d: %s\n"),
2853 : : auxoffset, cnt2,
2854 : 8 : elf_strptr (ebl->elf, shdr->sh_link, aux->vda_name));
2855 : :
2856 [ - + ]: 8 : if (aux->vda_next == 0)
2857 : : break;
2858 : :
2859 : 0 : auxoffset += aux->vda_next;
2860 : : }
2861 : :
2862 : : /* Find the next offset. */
2863 [ + + ]: 12 : if (def->vd_next == 0)
2864 : : break;
2865 : 10 : offset += def->vd_next;
2866 : : }
2867 : : }
2868 : :
2869 : :
2870 : : static void
2871 : 65 : handle_versym (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr)
2872 : : {
2873 : 65 : int class = gelf_getclass (ebl->elf);
2874 : 65 : const char **vername;
2875 : 65 : const char **filename;
2876 : :
2877 : : /* Get the data of the section. */
2878 : 65 : Elf_Data *data = elf_getdata (scn, NULL);
2879 [ + - ]: 65 : if (data == NULL)
2880 : 0 : return;
2881 : :
2882 : : /* Get the section header string table index. */
2883 : 65 : size_t shstrndx;
2884 [ - + ]: 65 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
2885 : 0 : error_exit (0, _("cannot get section header string table index"));
2886 : :
2887 : : /* We have to find the version definition section and extract the
2888 : : version names. */
2889 : : Elf_Scn *defscn = NULL;
2890 : : Elf_Scn *needscn = NULL;
2891 : :
2892 : : Elf_Scn *verscn = NULL;
2893 [ + + ]: 1978 : while ((verscn = elf_nextscn (ebl->elf, verscn)) != NULL)
2894 : : {
2895 : 1913 : GElf_Shdr vershdr_mem;
2896 : 1913 : GElf_Shdr *vershdr = gelf_getshdr (verscn, &vershdr_mem);
2897 : :
2898 [ + - ]: 1913 : if (likely (vershdr != NULL))
2899 : : {
2900 [ + + ]: 1913 : if (vershdr->sh_type == SHT_GNU_verdef)
2901 : : defscn = verscn;
2902 [ + + ]: 1911 : else if (vershdr->sh_type == SHT_GNU_verneed)
2903 : 65 : needscn = verscn;
2904 : : }
2905 : : }
2906 : :
2907 : 65 : size_t nvername;
2908 [ + - ]: 65 : if (defscn != NULL || needscn != NULL)
2909 : : {
2910 : : /* We have a version information (better should have). Now get
2911 : : the version names. First find the maximum version number. */
2912 : 65 : nvername = 0;
2913 [ + + ]: 65 : if (defscn != NULL)
2914 : : {
2915 : : /* Run through the version definitions and find the highest
2916 : : index. */
2917 : 2 : unsigned int offset = 0;
2918 : 2 : Elf_Data *defdata;
2919 : 2 : GElf_Shdr defshdrmem;
2920 : 2 : GElf_Shdr *defshdr;
2921 : :
2922 : 2 : defdata = elf_getdata (defscn, NULL);
2923 [ + - ]: 2 : if (unlikely (defdata == NULL))
2924 : 0 : return;
2925 : :
2926 : 2 : defshdr = gelf_getshdr (defscn, &defshdrmem);
2927 [ + - ]: 2 : if (unlikely (defshdr == NULL))
2928 : : return;
2929 : :
2930 [ + - ]: 12 : for (unsigned int cnt = 0; cnt < defshdr->sh_info; ++cnt)
2931 : : {
2932 : 12 : GElf_Verdef defmem;
2933 : 12 : GElf_Verdef *def;
2934 : :
2935 : : /* Get the data at the next offset. */
2936 : 12 : def = gelf_getverdef (defdata, offset, &defmem);
2937 [ + - ]: 12 : if (unlikely (def == NULL))
2938 : : break;
2939 : :
2940 : 12 : nvername = MAX (nvername, (size_t) (def->vd_ndx & 0x7fff));
2941 : :
2942 [ + + ]: 12 : if (def->vd_next == 0)
2943 : : break;
2944 : 10 : offset += def->vd_next;
2945 : : }
2946 : : }
2947 [ + - ]: 65 : if (needscn != NULL)
2948 : : {
2949 : 65 : unsigned int offset = 0;
2950 : 65 : Elf_Data *needdata;
2951 : 65 : GElf_Shdr needshdrmem;
2952 : 65 : GElf_Shdr *needshdr;
2953 : :
2954 : 65 : needdata = elf_getdata (needscn, NULL);
2955 [ + - ]: 65 : if (unlikely (needdata == NULL))
2956 : 0 : return;
2957 : :
2958 : 65 : needshdr = gelf_getshdr (needscn, &needshdrmem);
2959 [ + - ]: 65 : if (unlikely (needshdr == NULL))
2960 : : return;
2961 : :
2962 [ + - ]: 98 : for (unsigned int cnt = 0; cnt < needshdr->sh_info; ++cnt)
2963 : : {
2964 : 98 : GElf_Verneed needmem;
2965 : 98 : GElf_Verneed *need;
2966 : 98 : unsigned int auxoffset;
2967 : 98 : int cnt2;
2968 : :
2969 : : /* Get the data at the next offset. */
2970 : 98 : need = gelf_getverneed (needdata, offset, &needmem);
2971 [ + - ]: 98 : if (unlikely (need == NULL))
2972 : : break;
2973 : :
2974 : : /* Run through the auxiliary entries. */
2975 : 98 : auxoffset = offset + need->vn_aux;
2976 [ + - ]: 99 : for (cnt2 = need->vn_cnt; --cnt2 >= 0; )
2977 : : {
2978 : 99 : GElf_Vernaux auxmem;
2979 : 99 : GElf_Vernaux *aux;
2980 : :
2981 : 99 : aux = gelf_getvernaux (needdata, auxoffset, &auxmem);
2982 [ + - ]: 99 : if (unlikely (aux == NULL))
2983 : : break;
2984 : :
2985 : 99 : nvername = MAX (nvername,
2986 : : (size_t) (aux->vna_other & 0x7fff));
2987 : :
2988 [ + + ]: 99 : if (aux->vna_next == 0)
2989 : : break;
2990 : 1 : auxoffset += aux->vna_next;
2991 : : }
2992 : :
2993 [ + + ]: 98 : if (need->vn_next == 0)
2994 : : break;
2995 : 33 : offset += need->vn_next;
2996 : : }
2997 : : }
2998 : :
2999 : : /* This is the number of versions we know about. */
3000 : 65 : ++nvername;
3001 : :
3002 : : /* Allocate the array. */
3003 : 65 : vername = (const char **) alloca (nvername * sizeof (const char *));
3004 [ + + ]: 65 : memset(vername, 0, nvername * sizeof (const char *));
3005 : 65 : filename = (const char **) alloca (nvername * sizeof (const char *));
3006 : 65 : memset(filename, 0, nvername * sizeof (const char *));
3007 : :
3008 : : /* Run through the data structures again and collect the strings. */
3009 [ + + ]: 65 : if (defscn != NULL)
3010 : : {
3011 : : /* Run through the version definitions and find the highest
3012 : : index. */
3013 : 2 : unsigned int offset = 0;
3014 : 2 : Elf_Data *defdata;
3015 : 2 : GElf_Shdr defshdrmem;
3016 : 2 : GElf_Shdr *defshdr;
3017 : :
3018 : 2 : defdata = elf_getdata (defscn, NULL);
3019 [ + - ]: 2 : if (unlikely (defdata == NULL))
3020 : 0 : return;
3021 : :
3022 : 2 : defshdr = gelf_getshdr (defscn, &defshdrmem);
3023 [ + - ]: 2 : if (unlikely (defshdr == NULL))
3024 : : return;
3025 : :
3026 [ + - ]: 12 : for (unsigned int cnt = 0; cnt < defshdr->sh_info; ++cnt)
3027 : : {
3028 : :
3029 : : /* Get the data at the next offset. */
3030 : 12 : GElf_Verdef defmem;
3031 : 12 : GElf_Verdef *def = gelf_getverdef (defdata, offset, &defmem);
3032 [ + - ]: 12 : if (unlikely (def == NULL))
3033 : : break;
3034 : :
3035 : 12 : GElf_Verdaux auxmem;
3036 : 24 : GElf_Verdaux *aux = gelf_getverdaux (defdata,
3037 : 12 : offset + def->vd_aux,
3038 : : &auxmem);
3039 [ + - ]: 12 : if (unlikely (aux == NULL))
3040 : : break;
3041 : :
3042 : 12 : vername[def->vd_ndx & 0x7fff]
3043 : 12 : = elf_strptr (ebl->elf, defshdr->sh_link, aux->vda_name);
3044 : 12 : filename[def->vd_ndx & 0x7fff] = NULL;
3045 : :
3046 [ + + ]: 12 : if (def->vd_next == 0)
3047 : : break;
3048 : 10 : offset += def->vd_next;
3049 : : }
3050 : : }
3051 [ + - ]: 65 : if (needscn != NULL)
3052 : : {
3053 : 65 : unsigned int offset = 0;
3054 : :
3055 : 65 : Elf_Data *needdata = elf_getdata (needscn, NULL);
3056 : 65 : GElf_Shdr needshdrmem;
3057 : 65 : GElf_Shdr *needshdr = gelf_getshdr (needscn, &needshdrmem);
3058 [ - + ]: 65 : if (unlikely (needdata == NULL || needshdr == NULL))
3059 : 0 : return;
3060 : :
3061 [ + - ]: 98 : for (unsigned int cnt = 0; cnt < needshdr->sh_info; ++cnt)
3062 : : {
3063 : : /* Get the data at the next offset. */
3064 : 98 : GElf_Verneed needmem;
3065 : 98 : GElf_Verneed *need = gelf_getverneed (needdata, offset,
3066 : : &needmem);
3067 [ + - ]: 98 : if (unlikely (need == NULL))
3068 : : break;
3069 : :
3070 : : /* Run through the auxiliary entries. */
3071 : 98 : unsigned int auxoffset = offset + need->vn_aux;
3072 [ + - ]: 99 : for (int cnt2 = need->vn_cnt; --cnt2 >= 0; )
3073 : : {
3074 : 99 : GElf_Vernaux auxmem;
3075 : 99 : GElf_Vernaux *aux = gelf_getvernaux (needdata, auxoffset,
3076 : : &auxmem);
3077 [ + - ]: 99 : if (unlikely (aux == NULL))
3078 : : break;
3079 : :
3080 : 99 : vername[aux->vna_other & 0x7fff]
3081 : 99 : = elf_strptr (ebl->elf, needshdr->sh_link, aux->vna_name);
3082 : 99 : filename[aux->vna_other & 0x7fff]
3083 : 99 : = elf_strptr (ebl->elf, needshdr->sh_link, need->vn_file);
3084 : :
3085 [ + + ]: 99 : if (aux->vna_next == 0)
3086 : : break;
3087 : 1 : auxoffset += aux->vna_next;
3088 : : }
3089 : :
3090 [ + + ]: 98 : if (need->vn_next == 0)
3091 : : break;
3092 : 33 : offset += need->vn_next;
3093 : : }
3094 : : }
3095 : : }
3096 : : else
3097 : : {
3098 : : vername = NULL;
3099 : : nvername = 1;
3100 : : filename = NULL;
3101 : : }
3102 : :
3103 : 65 : GElf_Shdr glink_mem;
3104 : 65 : GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
3105 : : &glink_mem);
3106 : 65 : size_t sh_entsize = gelf_fsize (ebl->elf, ELF_T_HALF, 1, EV_CURRENT);
3107 [ - + ]: 65 : if (glink == NULL)
3108 : 0 : error_exit (0, _("invalid sh_link value in section %zu"),
3109 : : elf_ndxscn (scn));
3110 : :
3111 : : /* Print the header. */
3112 : 130 : printf (ngettext ("\
3113 : : \nVersion symbols section [%2u] '%s' contains %d entry:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'",
3114 : : "\
3115 : : \nVersion symbols section [%2u] '%s' contains %d entries:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'",
3116 : : shdr->sh_size / sh_entsize),
3117 : 65 : (unsigned int) elf_ndxscn (scn),
3118 : 65 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
3119 [ + + ]: 65 : (int) (shdr->sh_size / sh_entsize),
3120 : : class == ELFCLASS32 ? 10 : 18, shdr->sh_addr,
3121 : : shdr->sh_offset,
3122 : 65 : (unsigned int) shdr->sh_link,
3123 : 65 : elf_strptr (ebl->elf, shstrndx, glink->sh_name));
3124 : :
3125 : : /* Now we can finally look at the actual contents of this section. */
3126 [ + + ]: 1843 : for (unsigned int cnt = 0; cnt < shdr->sh_size / sh_entsize; ++cnt)
3127 : : {
3128 [ + + ]: 1778 : if (cnt % 2 == 0)
3129 : 905 : printf ("\n %4d:", cnt);
3130 : :
3131 : 1778 : GElf_Versym symmem;
3132 : 1778 : GElf_Versym *sym = gelf_getversym (data, cnt, &symmem);
3133 [ + - ]: 1778 : if (sym == NULL)
3134 : : break;
3135 : :
3136 [ + + + ]: 1778 : switch (*sym)
3137 : : {
3138 : 383 : ssize_t n;
3139 : 383 : case 0:
3140 : 383 : fputs_unlocked (_(" 0 *local* "),
3141 : : stdout);
3142 : 383 : break;
3143 : :
3144 : 20 : case 1:
3145 : 20 : fputs_unlocked (_(" 1 *global* "),
3146 : : stdout);
3147 : 20 : break;
3148 : :
3149 : 1375 : default:
3150 [ + - ]: 1375 : n = printf ("%4d%c%s",
3151 [ + - ]: 1375 : *sym & 0x7fff, *sym & 0x8000 ? 'h' : ' ',
3152 : : (vername != NULL
3153 [ + - ]: 1375 : && (unsigned int) (*sym & 0x7fff) < nvername)
3154 : 1375 : ? vername[*sym & 0x7fff] : "???");
3155 [ + - ]: 1375 : if ((unsigned int) (*sym & 0x7fff) < nvername
3156 [ + - + + ]: 1375 : && filename != NULL && filename[*sym & 0x7fff] != NULL)
3157 : 1223 : n += printf ("(%s)", filename[*sym & 0x7fff]);
3158 : 1778 : printf ("%*s", MAX (0, 33 - (int) n), " ");
3159 : : break;
3160 : : }
3161 : : }
3162 [ - + ]: 130 : putchar_unlocked ('\n');
3163 : : }
3164 : :
3165 : :
3166 : : static void
3167 : 65 : print_hash_info (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr, size_t shstrndx,
3168 : : uint_fast32_t maxlength, Elf32_Word nbucket,
3169 : : uint_fast32_t nsyms, uint32_t *lengths, const char *extrastr)
3170 : : {
3171 : 65 : uint32_t *counts = xcalloc (maxlength + 1, sizeof (uint32_t));
3172 : :
3173 [ + + ]: 256 : for (Elf32_Word cnt = 0; cnt < nbucket; ++cnt)
3174 : 191 : ++counts[lengths[cnt]];
3175 : :
3176 : 65 : GElf_Shdr glink_mem;
3177 : 65 : GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf,
3178 : 65 : shdr->sh_link),
3179 : : &glink_mem);
3180 [ - + ]: 65 : if (glink == NULL)
3181 : : {
3182 : 0 : error (0, 0, _("invalid sh_link value in section %zu"),
3183 : : elf_ndxscn (scn));
3184 : 0 : return;
3185 : : }
3186 : :
3187 [ + + ]: 195 : printf (ngettext ("\
3188 : : \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",
3189 : : "\
3190 : : \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",
3191 : : nbucket),
3192 : 65 : (unsigned int) elf_ndxscn (scn),
3193 : 65 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
3194 : : (int) nbucket,
3195 : 65 : gelf_getclass (ebl->elf) == ELFCLASS32 ? 10 : 18,
3196 : : shdr->sh_addr,
3197 : : shdr->sh_offset,
3198 : 65 : (unsigned int) shdr->sh_link,
3199 : 65 : elf_strptr (ebl->elf, shstrndx, glink->sh_name));
3200 : :
3201 [ + - ]: 65 : if (extrastr != NULL)
3202 : 65 : fputs (extrastr, stdout);
3203 : :
3204 [ + - ]: 65 : if (likely (nbucket > 0))
3205 : : {
3206 : 65 : uint64_t success = 0;
3207 : :
3208 : : /* xgettext:no-c-format */
3209 : 65 : fputs_unlocked (_("\
3210 : : Length Number % of total Coverage\n"), stdout);
3211 : 65 : printf (_(" 0 %6" PRIu32 " %5.1f%%\n"),
3212 : 65 : counts[0], (counts[0] * 100.0) / nbucket);
3213 : :
3214 : 65 : uint64_t nzero_counts = 0;
3215 [ + + ]: 89 : for (Elf32_Word cnt = 1; cnt <= maxlength; ++cnt)
3216 : : {
3217 : 24 : nzero_counts += counts[cnt] * cnt;
3218 : 24 : printf (_("\
3219 : : %7d %6" PRIu32 " %5.1f%% %5.1f%%\n"),
3220 : 24 : (int) cnt, counts[cnt], (counts[cnt] * 100.0) / nbucket,
3221 : 24 : (nzero_counts * 100.0) / nsyms);
3222 : : }
3223 : :
3224 : : Elf32_Word acc = 0;
3225 [ + + ]: 89 : for (Elf32_Word cnt = 1; cnt <= maxlength; ++cnt)
3226 : : {
3227 : 24 : acc += cnt;
3228 : 24 : success += counts[cnt] * acc;
3229 : : }
3230 : :
3231 : 130 : printf (_("\
3232 : : Average number of tests: successful lookup: %f\n\
3233 : : unsuccessful lookup: %f\n"),
3234 : 65 : (double) success / (double) nzero_counts,
3235 : : (double) nzero_counts / (double) nbucket);
3236 : : }
3237 : :
3238 : 65 : free (counts);
3239 : : }
3240 : :
3241 : :
3242 : : /* This function handles the traditional System V-style hash table format. */
3243 : : static void
3244 : 0 : handle_sysv_hash (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr, size_t shstrndx)
3245 : : {
3246 : 0 : Elf_Data *data = elf_getdata (scn, NULL);
3247 [ # # ]: 0 : if (unlikely (data == NULL))
3248 : : {
3249 : 0 : error (0, 0, _("cannot get data for section %d: %s"),
3250 : 0 : (int) elf_ndxscn (scn), elf_errmsg (-1));
3251 : 0 : return;
3252 : : }
3253 : :
3254 [ # # ]: 0 : if (unlikely (data->d_size < 2 * sizeof (Elf32_Word)))
3255 : : {
3256 : 0 : invalid_data:
3257 : 0 : error (0, 0, _("invalid data in sysv.hash section %d"),
3258 : 0 : (int) elf_ndxscn (scn));
3259 : 0 : return;
3260 : : }
3261 : :
3262 : 0 : Elf32_Word nbucket = ((Elf32_Word *) data->d_buf)[0];
3263 : 0 : Elf32_Word nchain = ((Elf32_Word *) data->d_buf)[1];
3264 : :
3265 : 0 : uint64_t used_buf = (2ULL + nchain + nbucket) * sizeof (Elf32_Word);
3266 [ # # ]: 0 : if (used_buf > data->d_size)
3267 : 0 : goto invalid_data;
3268 : :
3269 : 0 : Elf32_Word *bucket = &((Elf32_Word *) data->d_buf)[2];
3270 : 0 : Elf32_Word *chain = &((Elf32_Word *) data->d_buf)[2 + nbucket];
3271 : :
3272 : 0 : uint32_t *lengths = xcalloc (nbucket, sizeof (uint32_t));
3273 : :
3274 : 0 : uint_fast32_t maxlength = 0;
3275 : 0 : uint_fast32_t nsyms = 0;
3276 [ # # ]: 0 : for (Elf32_Word cnt = 0; cnt < nbucket; ++cnt)
3277 : : {
3278 : 0 : Elf32_Word inner = bucket[cnt];
3279 : 0 : Elf32_Word chain_len = 0;
3280 [ # # ]: 0 : while (inner > 0 && inner < nchain)
3281 : : {
3282 : 0 : ++nsyms;
3283 : 0 : ++chain_len;
3284 [ # # ]: 0 : if (chain_len > nchain)
3285 : : {
3286 : 0 : error (0, 0, _("invalid chain in sysv.hash section %d"),
3287 : 0 : (int) elf_ndxscn (scn));
3288 : 0 : free (lengths);
3289 : 0 : return;
3290 : : }
3291 [ # # ]: 0 : if (maxlength < ++lengths[cnt])
3292 : 0 : ++maxlength;
3293 : :
3294 : 0 : inner = chain[inner];
3295 : : }
3296 : : }
3297 : :
3298 : 0 : print_hash_info (ebl, scn, shdr, shstrndx, maxlength, nbucket, nsyms,
3299 : : lengths, NULL);
3300 : :
3301 : 0 : free (lengths);
3302 : : }
3303 : :
3304 : :
3305 : : /* This function handles the incorrect, System V-style hash table
3306 : : format some 64-bit architectures use. */
3307 : : static void
3308 : 0 : handle_sysv_hash64 (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr, size_t shstrndx)
3309 : : {
3310 : 0 : Elf_Data *data = elf_getdata (scn, NULL);
3311 [ # # ]: 0 : if (unlikely (data == NULL))
3312 : : {
3313 : 0 : error (0, 0, _("cannot get data for section %d: %s"),
3314 : 0 : (int) elf_ndxscn (scn), elf_errmsg (-1));
3315 : 0 : return;
3316 : : }
3317 : :
3318 [ # # ]: 0 : if (unlikely (data->d_size < 2 * sizeof (Elf64_Xword)))
3319 : : {
3320 : 0 : invalid_data:
3321 : 0 : error (0, 0, _("invalid data in sysv.hash64 section %d"),
3322 : 0 : (int) elf_ndxscn (scn));
3323 : 0 : return;
3324 : : }
3325 : :
3326 : 0 : Elf64_Xword nbucket = ((Elf64_Xword *) data->d_buf)[0];
3327 : 0 : Elf64_Xword nchain = ((Elf64_Xword *) data->d_buf)[1];
3328 : :
3329 : 0 : uint64_t maxwords = data->d_size / sizeof (Elf64_Xword);
3330 [ # # ]: 0 : if (maxwords < 2
3331 [ # # ]: 0 : || maxwords - 2 < nbucket
3332 [ # # ]: 0 : || maxwords - 2 - nbucket < nchain)
3333 : 0 : goto invalid_data;
3334 : :
3335 : 0 : Elf64_Xword *bucket = &((Elf64_Xword *) data->d_buf)[2];
3336 : 0 : Elf64_Xword *chain = &((Elf64_Xword *) data->d_buf)[2 + nbucket];
3337 : :
3338 : 0 : uint32_t *lengths = xcalloc (nbucket, sizeof (uint32_t));
3339 : :
3340 : 0 : uint_fast32_t maxlength = 0;
3341 : 0 : uint_fast32_t nsyms = 0;
3342 [ # # ]: 0 : for (Elf64_Xword cnt = 0; cnt < nbucket; ++cnt)
3343 : : {
3344 : 0 : Elf64_Xword inner = bucket[cnt];
3345 : 0 : Elf64_Xword chain_len = 0;
3346 [ # # ]: 0 : while (inner > 0 && inner < nchain)
3347 : : {
3348 : 0 : ++nsyms;
3349 : 0 : ++chain_len;
3350 [ # # ]: 0 : if (chain_len > nchain)
3351 : : {
3352 : 0 : error (0, 0, _("invalid chain in sysv.hash64 section %d"),
3353 : 0 : (int) elf_ndxscn (scn));
3354 : 0 : free (lengths);
3355 : 0 : return;
3356 : : }
3357 [ # # ]: 0 : if (maxlength < ++lengths[cnt])
3358 : 0 : ++maxlength;
3359 : :
3360 : 0 : inner = chain[inner];
3361 : : }
3362 : : }
3363 : :
3364 : 0 : print_hash_info (ebl, scn, shdr, shstrndx, maxlength, nbucket, nsyms,
3365 : : lengths, NULL);
3366 : :
3367 : 0 : free (lengths);
3368 : : }
3369 : :
3370 : :
3371 : : /* This function handles the GNU-style hash table format. */
3372 : : static void
3373 : 65 : handle_gnu_hash (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr, size_t shstrndx)
3374 : : {
3375 : 65 : uint32_t *lengths = NULL;
3376 : 65 : Elf_Data *data = elf_getdata (scn, NULL);
3377 [ - + ]: 65 : if (unlikely (data == NULL))
3378 : : {
3379 : 0 : error (0, 0, _("cannot get data for section %d: %s"),
3380 : 0 : (int) elf_ndxscn (scn), elf_errmsg (-1));
3381 : 0 : return;
3382 : : }
3383 : :
3384 [ - + ]: 65 : if (unlikely (data->d_size < 4 * sizeof (Elf32_Word)))
3385 : : {
3386 : 0 : invalid_data:
3387 : 0 : free (lengths);
3388 : 0 : error (0, 0, _("invalid data in gnu.hash section %d"),
3389 : 0 : (int) elf_ndxscn (scn));
3390 : 0 : return;
3391 : : }
3392 : :
3393 : 65 : Elf32_Word nbucket = ((Elf32_Word *) data->d_buf)[0];
3394 : 65 : Elf32_Word symbias = ((Elf32_Word *) data->d_buf)[1];
3395 : :
3396 : : /* Next comes the size of the bitmap. It's measured in words for
3397 : : the architecture. It's 32 bits for 32 bit archs, and 64 bits for
3398 : : 64 bit archs. There is always a bloom filter present, so zero is
3399 : : an invalid value. */
3400 : 65 : Elf32_Word bitmask_words = ((Elf32_Word *) data->d_buf)[2];
3401 [ + + ]: 65 : if (gelf_getclass (ebl->elf) == ELFCLASS64)
3402 : 57 : bitmask_words *= 2;
3403 : :
3404 [ - + ]: 65 : if (bitmask_words == 0)
3405 : 0 : goto invalid_data;
3406 : :
3407 : 65 : Elf32_Word shift = ((Elf32_Word *) data->d_buf)[3];
3408 : :
3409 : : /* Is there still room for the sym chain?
3410 : : Use uint64_t calculation to prevent 32bit overflow. */
3411 : 65 : uint64_t used_buf = (4ULL + bitmask_words + nbucket) * sizeof (Elf32_Word);
3412 : 65 : uint32_t max_nsyms = (data->d_size - used_buf) / sizeof (Elf32_Word);
3413 [ - + ]: 65 : if (used_buf > data->d_size)
3414 : 0 : goto invalid_data;
3415 : :
3416 : 65 : lengths = xcalloc (nbucket, sizeof (uint32_t));
3417 : :
3418 : 65 : Elf32_Word *bitmask = &((Elf32_Word *) data->d_buf)[4];
3419 : 65 : Elf32_Word *bucket = &((Elf32_Word *) data->d_buf)[4 + bitmask_words];
3420 : 65 : Elf32_Word *chain = &((Elf32_Word *) data->d_buf)[4 + bitmask_words
3421 : 65 : + nbucket];
3422 : :
3423 : : /* Compute distribution of chain lengths. */
3424 : 65 : uint_fast32_t maxlength = 0;
3425 : 65 : uint_fast32_t nsyms = 0;
3426 [ + + ]: 256 : for (Elf32_Word cnt = 0; cnt < nbucket; ++cnt)
3427 [ + + ]: 191 : if (bucket[cnt] != 0)
3428 : : {
3429 : 100 : Elf32_Word inner = bucket[cnt] - symbias;
3430 : 172 : do
3431 : : {
3432 : 172 : ++nsyms;
3433 [ + + ]: 172 : if (maxlength < ++lengths[cnt])
3434 : 24 : ++maxlength;
3435 [ - + ]: 172 : if (inner >= max_nsyms)
3436 : 0 : goto invalid_data;
3437 : : }
3438 [ + + ]: 172 : while ((chain[inner++] & 1) == 0);
3439 : : }
3440 : :
3441 : : /* Count bits in bitmask. */
3442 : : uint_fast32_t nbits = 0;
3443 [ + + ]: 223 : for (Elf32_Word cnt = 0; cnt < bitmask_words; ++cnt)
3444 : : {
3445 : 158 : uint_fast32_t word = bitmask[cnt];
3446 : :
3447 : 158 : word = (word & 0x55555555) + ((word >> 1) & 0x55555555);
3448 : 158 : word = (word & 0x33333333) + ((word >> 2) & 0x33333333);
3449 : 158 : word = (word & 0x0f0f0f0f) + ((word >> 4) & 0x0f0f0f0f);
3450 : 158 : word = (word & 0x00ff00ff) + ((word >> 8) & 0x00ff00ff);
3451 : 158 : nbits += (word & 0x0000ffff) + ((word >> 16) & 0x0000ffff);
3452 : : }
3453 : :
3454 : 130 : char *str = xasprintf (_("\
3455 : : Symbol Bias: %u\n\
3456 : : Bitmask Size: %zu bytes %" PRIuFAST32 "%% bits set 2nd hash shift: %u\n"),
3457 : : (unsigned int) symbias,
3458 : : bitmask_words * sizeof (Elf32_Word),
3459 : 65 : ((nbits * 100 + 50)
3460 : 65 : / (uint_fast32_t) (bitmask_words
3461 : : * sizeof (Elf32_Word) * 8)),
3462 : : (unsigned int) shift);
3463 : :
3464 : 65 : print_hash_info (ebl, scn, shdr, shstrndx, maxlength, nbucket, nsyms,
3465 : : lengths, str);
3466 : :
3467 : 65 : free (str);
3468 : 65 : free (lengths);
3469 : : }
3470 : :
3471 : :
3472 : : /* Find the symbol table(s). For this we have to search through the
3473 : : section table. */
3474 : : static void
3475 : 120 : handle_hash (Ebl *ebl)
3476 : : {
3477 : : /* Get the section header string table index. */
3478 : 120 : size_t shstrndx;
3479 [ - + ]: 120 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
3480 : 0 : error_exit (0, _("cannot get section header string table index"));
3481 : :
3482 : : Elf_Scn *scn = NULL;
3483 [ + + ]: 3485 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
3484 : : {
3485 : : /* Handle the section if it is a symbol table. */
3486 : 3365 : GElf_Shdr shdr_mem;
3487 : 3365 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
3488 : :
3489 [ + - ]: 3365 : if (likely (shdr != NULL))
3490 : : {
3491 [ + + ]: 3365 : if ((shdr->sh_type == SHT_HASH || shdr->sh_type == SHT_GNU_HASH)
3492 [ - + ]: 65 : && (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 (0, _("cannot get section [%zd] header: %s"),
3501 : : elf_ndxscn (scn), elf_errmsg (-1));
3502 : : }
3503 : :
3504 [ - + ]: 3365 : if (shdr->sh_type == SHT_HASH)
3505 : : {
3506 [ # # ]: 0 : if (ebl_sysvhash_entrysize (ebl) == sizeof (Elf64_Xword))
3507 : 0 : handle_sysv_hash64 (ebl, scn, shdr, shstrndx);
3508 : : else
3509 : 0 : handle_sysv_hash (ebl, scn, shdr, shstrndx);
3510 : : }
3511 [ + + ]: 3365 : else if (shdr->sh_type == SHT_GNU_HASH)
3512 : 65 : handle_gnu_hash (ebl, scn, shdr, shstrndx);
3513 : : }
3514 : : }
3515 : 120 : }
3516 : :
3517 : :
3518 : : static void
3519 : 124 : print_liblist (Ebl *ebl)
3520 : : {
3521 : : /* Find the library list sections. For this we have to search
3522 : : through the section table. */
3523 : 124 : Elf_Scn *scn = NULL;
3524 : :
3525 : : /* Get the section header string table index. */
3526 : 124 : size_t shstrndx;
3527 [ - + ]: 124 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
3528 : 0 : error_exit (0, _("cannot get section header string table index"));
3529 : :
3530 [ + + ]: 3548 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
3531 : : {
3532 : 3424 : GElf_Shdr shdr_mem;
3533 : 3424 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
3534 : :
3535 [ + - - + ]: 3424 : if (shdr != NULL && shdr->sh_type == SHT_GNU_LIBLIST)
3536 : : {
3537 : 0 : size_t sh_entsize = gelf_fsize (ebl->elf, ELF_T_LIB, 1, EV_CURRENT);
3538 : 0 : int nentries = shdr->sh_size / sh_entsize;
3539 : 0 : printf (ngettext ("\
3540 : : \nLibrary list section [%2zu] '%s' at offset %#0" PRIx64 " contains %d entry:\n",
3541 : : "\
3542 : : \nLibrary list section [%2zu] '%s' at offset %#0" PRIx64 " contains %d entries:\n",
3543 : : nentries),
3544 : : elf_ndxscn (scn),
3545 : 0 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
3546 : : shdr->sh_offset,
3547 : : nentries);
3548 : :
3549 : 0 : Elf_Data *data = elf_getdata (scn, NULL);
3550 [ # # ]: 0 : if (data == NULL)
3551 : 0 : return;
3552 : :
3553 : 0 : puts (_("\
3554 : : Library Time Stamp Checksum Version Flags"));
3555 : :
3556 [ # # ]: 0 : for (int cnt = 0; cnt < nentries; ++cnt)
3557 : : {
3558 : 0 : GElf_Lib lib_mem;
3559 : 0 : GElf_Lib *lib = gelf_getlib (data, cnt, &lib_mem);
3560 [ # # ]: 0 : if (unlikely (lib == NULL))
3561 : 0 : continue;
3562 : :
3563 : 0 : time_t t = (time_t) lib->l_time_stamp;
3564 : 0 : struct tm *tm = gmtime (&t);
3565 [ # # ]: 0 : if (unlikely (tm == NULL))
3566 : 0 : continue;
3567 : :
3568 : 0 : printf (" [%2d] %-29s %04u-%02u-%02uT%02u:%02u:%02u %08x %-7u %u\n",
3569 : 0 : cnt, elf_strptr (ebl->elf, shdr->sh_link, lib->l_name),
3570 : 0 : tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
3571 : : tm->tm_hour, tm->tm_min, tm->tm_sec,
3572 : 0 : (unsigned int) lib->l_checksum,
3573 : 0 : (unsigned int) lib->l_version,
3574 : 0 : (unsigned int) lib->l_flags);
3575 : : }
3576 : : }
3577 : : }
3578 : : }
3579 : :
3580 : : static inline size_t
3581 : 12 : left (Elf_Data *data,
3582 : : const unsigned char *p)
3583 : : {
3584 : 12 : return (const unsigned char *) data->d_buf + data->d_size - p;
3585 : : }
3586 : :
3587 : : static void
3588 : 124 : print_attributes (Ebl *ebl, const GElf_Ehdr *ehdr)
3589 : : {
3590 : : /* Find the object attributes sections. For this we have to search
3591 : : through the section table. */
3592 : 124 : Elf_Scn *scn = NULL;
3593 : :
3594 : : /* Get the section header string table index. */
3595 : 124 : size_t shstrndx;
3596 [ - + ]: 124 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
3597 : 0 : error_exit (0, _("cannot get section header string table index"));
3598 : :
3599 [ + + ]: 3548 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
3600 : : {
3601 : 3424 : GElf_Shdr shdr_mem;
3602 : 3424 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
3603 : :
3604 [ + - + + ]: 3424 : if (shdr == NULL || (shdr->sh_type != SHT_GNU_ATTRIBUTES
3605 [ + + ]: 3421 : && (shdr->sh_type != SHT_ARM_ATTRIBUTES
3606 [ - + ]: 1 : || ehdr->e_machine != EM_ARM)
3607 [ + + ]: 3420 : && (shdr->sh_type != SHT_CSKY_ATTRIBUTES
3608 [ + - ]: 1 : || ehdr->e_machine != EM_CSKY)))
3609 : 3420 : continue;
3610 : :
3611 : 8 : printf (_("\
3612 : : \nObject attributes section [%2zu] '%s' of %" PRIu64
3613 : : " bytes at offset %#0" PRIx64 ":\n"),
3614 : : elf_ndxscn (scn),
3615 : 4 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
3616 : : shdr->sh_size, shdr->sh_offset);
3617 : :
3618 : 4 : Elf_Data *data = elf_rawdata (scn, NULL);
3619 [ + - + - ]: 4 : if (unlikely (data == NULL || data->d_size == 0))
3620 : 0 : return;
3621 : :
3622 : 4 : const unsigned char *p = data->d_buf;
3623 : :
3624 : : /* There is only one 'version', A. */
3625 [ + - ]: 4 : if (unlikely (*p++ != 'A'))
3626 : : return;
3627 : :
3628 : 4 : fputs_unlocked (_(" Owner Size\n"), stdout);
3629 : :
3630 : : /* Loop over the sections. */
3631 [ + + ]: 8 : while (left (data, p) >= 4)
3632 : : {
3633 : : /* Section length. */
3634 : 4 : uint32_t len;
3635 [ + + ]: 4 : memcpy (&len, p, sizeof len);
3636 : :
3637 [ + + ]: 4 : if (MY_ELFDATA != ehdr->e_ident[EI_DATA])
3638 : 2 : CONVERT (len);
3639 : :
3640 [ + - ]: 4 : if (unlikely (len > left (data, p)))
3641 : : break;
3642 : :
3643 : : /* Section vendor name. */
3644 : 4 : const unsigned char *name = p + sizeof len;
3645 : 4 : p += len;
3646 : :
3647 : 4 : unsigned const char *q = memchr (name, '\0', len);
3648 [ + - ]: 4 : if (unlikely (q == NULL))
3649 : : break;
3650 : 4 : ++q;
3651 : :
3652 : 4 : printf (_(" %-13s %4" PRIu32 "\n"), name, len);
3653 : :
3654 : 8 : bool gnu_vendor = (q - name == sizeof "gnu"
3655 [ + + - + ]: 4 : && !memcmp (name, "gnu", sizeof "gnu"));
3656 : :
3657 : : /* Loop over subsections. */
3658 [ + + ]: 4 : if (shdr->sh_type != SHT_GNU_ATTRIBUTES
3659 [ + - ]: 3 : || gnu_vendor)
3660 [ + + ]: 8 : while (q < p)
3661 : : {
3662 : 4 : const unsigned char *const sub = q;
3663 : :
3664 : 4 : unsigned int subsection_tag;
3665 : 4 : get_uleb128 (subsection_tag, q, p);
3666 [ + - ]: 4 : if (unlikely (q >= p))
3667 : : break;
3668 : :
3669 : 4 : uint32_t subsection_len;
3670 [ + - ]: 4 : if (unlikely (p - sub < (ptrdiff_t) sizeof subsection_len))
3671 : : break;
3672 : :
3673 [ + + ]: 4 : memcpy (&subsection_len, q, sizeof subsection_len);
3674 : :
3675 [ + + ]: 4 : if (MY_ELFDATA != ehdr->e_ident[EI_DATA])
3676 : 2 : CONVERT (subsection_len);
3677 : :
3678 : : /* Don't overflow, ptrdiff_t might be 32bits, but signed. */
3679 [ + - + - ]: 4 : if (unlikely (subsection_len == 0
3680 : : || subsection_len >= (uint32_t) PTRDIFF_MAX
3681 : : || p - sub < (ptrdiff_t) subsection_len))
3682 : : break;
3683 : :
3684 : 4 : const unsigned char *r = q + sizeof subsection_len;
3685 : 4 : q = sub + subsection_len;
3686 : :
3687 [ - + ]: 4 : switch (subsection_tag)
3688 : : {
3689 : 0 : default:
3690 : : /* Unknown subsection, print and skip. */
3691 : 4 : printf (_(" %-4u %12" PRIu32 "\n"),
3692 : : subsection_tag, subsection_len);
3693 : : break;
3694 : :
3695 : 4 : case 1: /* Tag_File */
3696 : 4 : printf (_(" File: %11" PRIu32 "\n"),
3697 : : subsection_len);
3698 : :
3699 [ + + ]: 26 : while (r < q)
3700 : : {
3701 : 22 : unsigned int tag;
3702 : 22 : get_uleb128 (tag, r, q);
3703 [ + - ]: 22 : if (unlikely (r >= q))
3704 : : break;
3705 : :
3706 : : /* GNU style tags have either a uleb128 value,
3707 : : when lowest bit is not set, or a string
3708 : : when the lowest bit is set.
3709 : : "compatibility" (32) is special. It has
3710 : : both a string and a uleb128 value. For
3711 : : non-gnu we assume 6 till 31 only take ints.
3712 : : XXX see arm backend, do we need a separate
3713 : : hook? */
3714 : 22 : uint64_t value = 0;
3715 : 22 : const char *string = NULL;
3716 [ + - + + ]: 22 : if (tag == 32 || (tag & 1) == 0
3717 [ + - + + ]: 8 : || (! gnu_vendor && (tag > 5 && tag < 32)))
3718 : : {
3719 : 21 : get_uleb128 (value, r, q);
3720 [ + - ]: 21 : if (r > q)
3721 : : break;
3722 : : }
3723 [ + - ]: 22 : if (tag == 32
3724 [ + + ]: 22 : || ((tag & 1) != 0
3725 [ + - ]: 8 : && (gnu_vendor
3726 [ + - ]: 8 : || (! gnu_vendor && tag > 32)))
3727 [ + + + + ]: 22 : || (! gnu_vendor && tag > 3 && tag < 6))
3728 : : {
3729 : 1 : string = (const char *) r;
3730 : 1 : r = memchr (r, '\0', q - r);
3731 [ + - ]: 1 : if (r == NULL)
3732 : : break;
3733 : 1 : ++r;
3734 : : }
3735 : :
3736 : 22 : const char *tag_name = NULL;
3737 : 22 : const char *value_name = NULL;
3738 : 22 : ebl_check_object_attribute (ebl, (const char *) name,
3739 : : tag, value,
3740 : : &tag_name, &value_name);
3741 : :
3742 [ + - ]: 22 : if (tag_name != NULL)
3743 : : {
3744 [ - + ]: 22 : if (tag == 32)
3745 : 0 : printf (_(" %s: %" PRId64 ", %s\n"),
3746 : : tag_name, value, string);
3747 [ + + + + ]: 22 : else if (string == NULL && value_name == NULL)
3748 : 1 : printf (_(" %s: %" PRId64 "\n"),
3749 : : tag_name, value);
3750 : : else
3751 [ + + ]: 21 : printf (_(" %s: %s\n"),
3752 : : tag_name, string ?: value_name);
3753 : : }
3754 : : else
3755 : : {
3756 : : /* For "gnu" vendor 32 "compatibility" has
3757 : : already been handled above. */
3758 [ # # # # ]: 0 : assert (tag != 32
3759 : : || strcmp ((const char *) name, "gnu"));
3760 [ # # ]: 0 : if (string == NULL)
3761 : 0 : printf (_(" %u: %" PRId64 "\n"),
3762 : : tag, value);
3763 : : else
3764 : 22 : printf (_(" %u: %s\n"),
3765 : : tag, string);
3766 : : }
3767 : : }
3768 : : }
3769 : : }
3770 : : }
3771 : : }
3772 : : }
3773 : :
3774 : :
3775 : : void
3776 : 1143663 : print_dwarf_addr (Dwfl_Module *dwflmod,
3777 : : int address_size, Dwarf_Addr address, Dwarf_Addr raw)
3778 : : {
3779 : : /* See if there is a name we can give for this address. */
3780 : 1143663 : GElf_Sym sym;
3781 : 1143663 : GElf_Off off = 0;
3782 [ + + ]: 1143633 : const char *name = (print_address_names && ! print_unresolved_addresses)
3783 : 1143551 : ? dwfl_module_addrinfo (dwflmod, address, &off, &sym, NULL, NULL, NULL)
3784 [ + + ]: 1143663 : : NULL;
3785 : :
3786 : 1143663 : const char *scn;
3787 [ + + ]: 1143663 : if (print_unresolved_addresses)
3788 : : {
3789 : 84 : address = raw;
3790 : 84 : scn = NULL;
3791 : : }
3792 : : else
3793 : : {
3794 : : /* Relativize the address. */
3795 : 1143579 : int n = dwfl_module_relocations (dwflmod);
3796 [ + + ]: 1143579 : int i = n < 1 ? -1 : dwfl_module_relocate_address (dwflmod, &address);
3797 : :
3798 : : /* In an ET_REL file there is a section name to refer to. */
3799 : 1142227 : scn = (i < 0 ? NULL
3800 [ + - ]: 1142227 : : dwfl_module_relocation_info (dwflmod, i, NULL));
3801 : : }
3802 : :
3803 [ - + ]: 1143663 : if ((name != NULL
3804 : 1134370 : ? (off != 0
3805 : : ? (scn != NULL
3806 : : ? (address_size == 0
3807 : 376214 : ? printf ("%s+%#" PRIx64 " <%s+%#" PRIx64 ">",
3808 : : scn, address, name, off)
3809 : 1411740 : : printf ("%s+%#0*" PRIx64 " <%s+%#" PRIx64 ">",
3810 : 705870 : scn, 2 + address_size * 2, address,
3811 : : name, off))
3812 : : : (address_size == 0
3813 : 182 : ? printf ("%#" PRIx64 " <%s+%#" PRIx64 ">",
3814 : : address, name, off)
3815 : 1120 : : printf ("%#0*" PRIx64 " <%s+%#" PRIx64 ">",
3816 : 560 : 2 + address_size * 2, address,
3817 : : name, off)))
3818 : : : (scn != NULL
3819 : : ? (address_size == 0
3820 : 15675 : ? printf ("%s+%#" PRIx64 " <%s>", scn, address, name)
3821 : 70828 : : printf ("%s+%#0*" PRIx64 " <%s>",
3822 : 35414 : scn, 2 + address_size * 2, address, name))
3823 : : : (address_size == 0
3824 : 80 : ? printf ("%#" PRIx64 " <%s>", address, name)
3825 : 750 : : printf ("%#0*" PRIx64 " <%s>",
3826 : 375 : 2 + address_size * 2, address, name))))
3827 : : : (scn != NULL
3828 : : ? (address_size == 0
3829 : 1918 : ? printf ("%s+%#" PRIx64, scn, address)
3830 : 7136 : : printf ("%s+%#0*" PRIx64, scn, 2 + address_size * 2, address))
3831 : : : (address_size == 0
3832 : 35 : ? printf ("%#" PRIx64, address)
3833 [ + + + + : 2287122 : : printf ("%#0*" PRIx64, 2 + address_size * 2, address)))) < 0)
+ + + + +
+ + + + +
+ + + + +
+ + + ]
3834 : 0 : error_exit (0, _("sprintf failure"));
3835 : 1143663 : }
3836 : :
3837 : :
3838 : : static const char *
3839 : 1029687 : dwarf_tag_string (unsigned int tag)
3840 : : {
3841 [ - + + - : 1029687 : switch (tag)
- - - - +
- + + - -
- - - - -
+ - + - +
- - + + -
+ - - - -
- - - + -
+ - + + +
- - - - -
- + - - +
- - - + -
+ + + + -
- - - - +
+ + + - +
+ + + - -
- ]
3842 : : {
3843 : : #define DWARF_ONE_KNOWN_DW_TAG(NAME, CODE) case CODE: return #NAME;
3844 : 1029687 : DWARF_ALL_KNOWN_DW_TAG
3845 : : #undef DWARF_ONE_KNOWN_DW_TAG
3846 : 0 : default:
3847 : 0 : return NULL;
3848 : : }
3849 : : }
3850 : :
3851 : :
3852 : : static const char *
3853 : 4014923 : dwarf_attr_string (unsigned int attrnum)
3854 : : {
3855 [ + - + - : 4014923 : switch (attrnum)
- + - + -
- + + + -
- - - + +
- + - - +
- + - + -
- - - - -
- - - - -
- - - - -
- - - + -
+ - + - +
- - - + +
- - - + -
+ - - + -
- + + + -
- + + - -
- - - + -
+ - + - -
+ - - + +
+ + - - -
- - + + +
+ - + - +
- - - - +
+ - + - +
+ - + + +
+ + - - +
- - - + -
+ - - - -
+ + - + -
- - - + -
- - + + -
+ - - - -
+ - - - -
- - + + -
- - - - -
- + ]
3856 : : {
3857 : : #define DWARF_ONE_KNOWN_DW_AT(NAME, CODE) case CODE: return #NAME;
3858 : 4014922 : DWARF_ALL_KNOWN_DW_AT
3859 : : #undef DWARF_ONE_KNOWN_DW_AT
3860 : 0 : default:
3861 : 0 : return NULL;
3862 : : }
3863 : : }
3864 : :
3865 : :
3866 : : static const char *
3867 : 4014929 : dwarf_form_string (unsigned int form)
3868 : : {
3869 [ + + + + : 4014929 : switch (form)
+ - - - -
- + - - +
- + + + +
+ + + - +
+ - - + -
- + - - +
+ + + + +
- + - - -
- + - + ]
3870 : : {
3871 : : #define DWARF_ONE_KNOWN_DW_FORM(NAME, CODE) case CODE: return #NAME;
3872 : 4014927 : DWARF_ALL_KNOWN_DW_FORM
3873 : : #undef DWARF_ONE_KNOWN_DW_FORM
3874 : 0 : default:
3875 : 0 : return NULL;
3876 : : }
3877 : : }
3878 : :
3879 : :
3880 : : static const char *
3881 : 1702 : dwarf_lang_string (unsigned int lang)
3882 : : {
3883 [ + - - + : 1702 : switch (lang)
+ + + - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - -
- ]
3884 : : {
3885 : : #define DWARF_ONE_KNOWN_DW_LANG(NAME, CODE) case CODE: return #NAME;
3886 : 1702 : DWARF_ALL_KNOWN_DW_LANG
3887 : : #undef DWARF_ONE_KNOWN_DW_LANG
3888 : 0 : default:
3889 : 0 : return NULL;
3890 : : }
3891 : : }
3892 : :
3893 : :
3894 : : static const char *
3895 : 3607 : dwarf_inline_string (unsigned int code)
3896 : : {
3897 : 3607 : static const char *const known[] =
3898 : : {
3899 : : #define DWARF_ONE_KNOWN_DW_INL(NAME, CODE) [CODE] = #NAME,
3900 : : DWARF_ALL_KNOWN_DW_INL
3901 : : #undef DWARF_ONE_KNOWN_DW_INL
3902 : : };
3903 : :
3904 : 3607 : if (likely (code < sizeof (known) / sizeof (known[0])))
3905 : 3607 : return known[code];
3906 : :
3907 : : return NULL;
3908 : : }
3909 : :
3910 : :
3911 : : static const char *
3912 : 30664 : dwarf_encoding_string (unsigned int code)
3913 : : {
3914 : 30664 : static const char *const known[] =
3915 : : {
3916 : : #define DWARF_ONE_KNOWN_DW_ATE(NAME, CODE) [CODE] = #NAME,
3917 : : DWARF_ALL_KNOWN_DW_ATE
3918 : : #undef DWARF_ONE_KNOWN_DW_ATE
3919 : : };
3920 : :
3921 : 30664 : if (likely (code < sizeof (known) / sizeof (known[0])))
3922 : 30664 : return known[code];
3923 : :
3924 : : return NULL;
3925 : : }
3926 : :
3927 : :
3928 : : static const char *
3929 : 0 : dwarf_access_string (unsigned int code)
3930 : : {
3931 : 0 : static const char *const known[] =
3932 : : {
3933 : : #define DWARF_ONE_KNOWN_DW_ACCESS(NAME, CODE) [CODE] = #NAME,
3934 : : DWARF_ALL_KNOWN_DW_ACCESS
3935 : : #undef DWARF_ONE_KNOWN_DW_ACCESS
3936 : : };
3937 : :
3938 : 0 : if (likely (code < sizeof (known) / sizeof (known[0])))
3939 : 0 : return known[code];
3940 : :
3941 : : return NULL;
3942 : : }
3943 : :
3944 : :
3945 : : static const char *
3946 : 0 : dwarf_defaulted_string (unsigned int code)
3947 : : {
3948 : 0 : static const char *const known[] =
3949 : : {
3950 : : #define DWARF_ONE_KNOWN_DW_DEFAULTED(NAME, CODE) [CODE] = #NAME,
3951 : : DWARF_ALL_KNOWN_DW_DEFAULTED
3952 : : #undef DWARF_ONE_KNOWN_DW_DEFAULTED
3953 : : };
3954 : :
3955 : 0 : if (likely (code < sizeof (known) / sizeof (known[0])))
3956 : 0 : return known[code];
3957 : :
3958 : : return NULL;
3959 : : }
3960 : :
3961 : :
3962 : : static const char *
3963 : 0 : dwarf_visibility_string (unsigned int code)
3964 : : {
3965 : 0 : static const char *const known[] =
3966 : : {
3967 : : #define DWARF_ONE_KNOWN_DW_VIS(NAME, CODE) [CODE] = #NAME,
3968 : : DWARF_ALL_KNOWN_DW_VIS
3969 : : #undef DWARF_ONE_KNOWN_DW_VIS
3970 : : };
3971 : :
3972 : 0 : if (likely (code < sizeof (known) / sizeof (known[0])))
3973 : 0 : return known[code];
3974 : :
3975 : : return NULL;
3976 : : }
3977 : :
3978 : :
3979 : : static const char *
3980 : 0 : dwarf_virtuality_string (unsigned int code)
3981 : : {
3982 : 0 : static const char *const known[] =
3983 : : {
3984 : : #define DWARF_ONE_KNOWN_DW_VIRTUALITY(NAME, CODE) [CODE] = #NAME,
3985 : : DWARF_ALL_KNOWN_DW_VIRTUALITY
3986 : : #undef DWARF_ONE_KNOWN_DW_VIRTUALITY
3987 : : };
3988 : :
3989 : 0 : if (likely (code < sizeof (known) / sizeof (known[0])))
3990 : 0 : return known[code];
3991 : :
3992 : : return NULL;
3993 : : }
3994 : :
3995 : :
3996 : : static const char *
3997 : 0 : dwarf_identifier_case_string (unsigned int code)
3998 : : {
3999 : 0 : static const char *const known[] =
4000 : : {
4001 : : #define DWARF_ONE_KNOWN_DW_ID(NAME, CODE) [CODE] = #NAME,
4002 : : DWARF_ALL_KNOWN_DW_ID
4003 : : #undef DWARF_ONE_KNOWN_DW_ID
4004 : : };
4005 : :
4006 : 0 : if (likely (code < sizeof (known) / sizeof (known[0])))
4007 : 0 : return known[code];
4008 : :
4009 : : return NULL;
4010 : : }
4011 : :
4012 : :
4013 : : static const char *
4014 : 0 : dwarf_calling_convention_string (unsigned int code)
4015 : : {
4016 : 0 : static const char *const known[] =
4017 : : {
4018 : : #define DWARF_ONE_KNOWN_DW_CC(NAME, CODE) [CODE] = #NAME,
4019 : : DWARF_ALL_KNOWN_DW_CC
4020 : : #undef DWARF_ONE_KNOWN_DW_CC
4021 : : };
4022 : :
4023 : 0 : if (likely (code < sizeof (known) / sizeof (known[0])))
4024 : 0 : return known[code];
4025 : :
4026 : : return NULL;
4027 : : }
4028 : :
4029 : :
4030 : : static const char *
4031 : 0 : dwarf_ordering_string (unsigned int code)
4032 : : {
4033 : 0 : static const char *const known[] =
4034 : : {
4035 : : #define DWARF_ONE_KNOWN_DW_ORD(NAME, CODE) [CODE] = #NAME,
4036 : : DWARF_ALL_KNOWN_DW_ORD
4037 : : #undef DWARF_ONE_KNOWN_DW_ORD
4038 : : };
4039 : :
4040 : 0 : if (likely (code < sizeof (known) / sizeof (known[0])))
4041 : 0 : return known[code];
4042 : :
4043 : : return NULL;
4044 : : }
4045 : :
4046 : :
4047 : : static const char *
4048 : 8 : dwarf_discr_list_string (unsigned int code)
4049 : : {
4050 : 8 : static const char *const known[] =
4051 : : {
4052 : : #define DWARF_ONE_KNOWN_DW_DSC(NAME, CODE) [CODE] = #NAME,
4053 : : DWARF_ALL_KNOWN_DW_DSC
4054 : : #undef DWARF_ONE_KNOWN_DW_DSC
4055 : : };
4056 : :
4057 : 8 : if (likely (code < sizeof (known) / sizeof (known[0])))
4058 : 8 : return known[code];
4059 : :
4060 : : return NULL;
4061 : : }
4062 : :
4063 : :
4064 : : static const char *
4065 : 624938 : dwarf_locexpr_opcode_string (unsigned int code)
4066 : : {
4067 : 624938 : static const char *const known[] =
4068 : : {
4069 : : /* Normally we can't afford building huge table of 64K entries,
4070 : : most of them zero, just because there are a couple defined
4071 : : values at the far end. In case of opcodes, it's OK. */
4072 : : #define DWARF_ONE_KNOWN_DW_OP(NAME, CODE) [CODE] = #NAME,
4073 : : DWARF_ALL_KNOWN_DW_OP
4074 : : #undef DWARF_ONE_KNOWN_DW_OP
4075 : : };
4076 : :
4077 : 624938 : if (likely (code < sizeof (known) / sizeof (known[0])))
4078 : 624938 : return known[code];
4079 : :
4080 : : return NULL;
4081 : : }
4082 : :
4083 : :
4084 : : static const char *
4085 : 8 : dwarf_unit_string (unsigned int type)
4086 : : {
4087 [ - + - - : 8 : switch (type)
- - + ]
4088 : : {
4089 : : #define DWARF_ONE_KNOWN_DW_UT(NAME, CODE) case CODE: return #NAME;
4090 : 7 : DWARF_ALL_KNOWN_DW_UT
4091 : : #undef DWARF_ONE_KNOWN_DW_UT
4092 : 0 : default:
4093 : 0 : return NULL;
4094 : : }
4095 : : }
4096 : :
4097 : :
4098 : : static const char *
4099 : 16 : dwarf_range_list_encoding_string (unsigned int kind)
4100 : : {
4101 [ - + + - : 16 : switch (kind)
+ - - -
+ ]
4102 : : {
4103 : : #define DWARF_ONE_KNOWN_DW_RLE(NAME, CODE) case CODE: return #NAME;
4104 : 14 : DWARF_ALL_KNOWN_DW_RLE
4105 : : #undef DWARF_ONE_KNOWN_DW_RLE
4106 : 0 : default:
4107 : 0 : return NULL;
4108 : : }
4109 : : }
4110 : :
4111 : :
4112 : : static const char *
4113 : 91 : dwarf_loc_list_encoding_string (unsigned int kind)
4114 : : {
4115 [ - - + + : 91 : switch (kind)
- + - + -
+ ]
4116 : : {
4117 : : #define DWARF_ONE_KNOWN_DW_LLE(NAME, CODE) case CODE: return #NAME;
4118 : 86 : DWARF_ALL_KNOWN_DW_LLE
4119 : : #undef DWARF_ONE_KNOWN_DW_LLE
4120 : 0 : default:
4121 : 0 : return NULL;
4122 : : }
4123 : : }
4124 : :
4125 : :
4126 : : static const char *
4127 : 6 : dwarf_line_content_description_string (unsigned int kind)
4128 : : {
4129 [ + + - - : 6 : switch (kind)
- - ]
4130 : : {
4131 : : #define DWARF_ONE_KNOWN_DW_LNCT(NAME, CODE) case CODE: return #NAME;
4132 : 6 : DWARF_ALL_KNOWN_DW_LNCT
4133 : : #undef DWARF_ONE_KNOWN_DW_LNCT
4134 : 0 : default:
4135 : 0 : return NULL;
4136 : : }
4137 : : }
4138 : :
4139 : :
4140 : : /* Used by all dwarf_foo_name functions. */
4141 : : static const char *
4142 : 9095641 : string_or_unknown (const char *known, unsigned int code,
4143 : : unsigned int lo_user, unsigned int hi_user,
4144 : : bool print_unknown_num)
4145 : : {
4146 : 9095641 : static char unknown_buf[20];
4147 : :
4148 [ - + ]: 9095641 : if (likely (known != NULL))
4149 : : return known;
4150 : :
4151 [ # # # # ]: 0 : if (lo_user != 0 && code >= lo_user && code <= hi_user)
4152 : : {
4153 : 0 : snprintf (unknown_buf, sizeof unknown_buf, "lo_user+%#x",
4154 : : code - lo_user);
4155 : 0 : return unknown_buf;
4156 : : }
4157 : :
4158 [ # # ]: 0 : if (print_unknown_num)
4159 : : {
4160 : 0 : snprintf (unknown_buf, sizeof unknown_buf, "??? (%#x)", code);
4161 : 0 : return unknown_buf;
4162 : : }
4163 : :
4164 : : return "???";
4165 : : }
4166 : :
4167 : :
4168 : : static const char *
4169 : 1029687 : dwarf_tag_name (unsigned int tag)
4170 : : {
4171 : 1029687 : const char *ret = dwarf_tag_string (tag);
4172 : 1029687 : return string_or_unknown (ret, tag, DW_TAG_lo_user, DW_TAG_hi_user, true);
4173 : : }
4174 : :
4175 : : static const char *
4176 : 4014923 : dwarf_attr_name (unsigned int attr)
4177 : : {
4178 : 4014923 : const char *ret = dwarf_attr_string (attr);
4179 : 4014923 : return string_or_unknown (ret, attr, DW_AT_lo_user, DW_AT_hi_user, true);
4180 : : }
4181 : :
4182 : :
4183 : : static const char *
4184 : 4014929 : dwarf_form_name (unsigned int form)
4185 : : {
4186 : 4014929 : const char *ret = dwarf_form_string (form);
4187 : 4014929 : return string_or_unknown (ret, form, 0, 0, true);
4188 : : }
4189 : :
4190 : :
4191 : : static const char *
4192 : 1702 : dwarf_lang_name (unsigned int lang)
4193 : : {
4194 : 1702 : const char *ret = dwarf_lang_string (lang);
4195 : 1702 : return string_or_unknown (ret, lang, DW_LANG_lo_user, DW_LANG_hi_user, false);
4196 : : }
4197 : :
4198 : :
4199 : : static const char *
4200 : 3607 : dwarf_inline_name (unsigned int code)
4201 : : {
4202 [ + - ]: 3607 : const char *ret = dwarf_inline_string (code);
4203 : 3607 : return string_or_unknown (ret, code, 0, 0, false);
4204 : : }
4205 : :
4206 : :
4207 : : static const char *
4208 : 30664 : dwarf_encoding_name (unsigned int code)
4209 : : {
4210 [ + - ]: 30664 : const char *ret = dwarf_encoding_string (code);
4211 : 30664 : return string_or_unknown (ret, code, DW_ATE_lo_user, DW_ATE_hi_user, false);
4212 : : }
4213 : :
4214 : :
4215 : : static const char *
4216 : 0 : dwarf_access_name (unsigned int code)
4217 : : {
4218 [ # # ]: 0 : const char *ret = dwarf_access_string (code);
4219 : 0 : return string_or_unknown (ret, code, 0, 0, false);
4220 : : }
4221 : :
4222 : :
4223 : : static const char *
4224 : 0 : dwarf_defaulted_name (unsigned int code)
4225 : : {
4226 [ # # ]: 0 : const char *ret = dwarf_defaulted_string (code);
4227 : 0 : return string_or_unknown (ret, code, 0, 0, false);
4228 : : }
4229 : :
4230 : :
4231 : : static const char *
4232 : 0 : dwarf_visibility_name (unsigned int code)
4233 : : {
4234 [ # # ]: 0 : const char *ret = dwarf_visibility_string (code);
4235 : 0 : return string_or_unknown (ret, code, 0, 0, false);
4236 : : }
4237 : :
4238 : :
4239 : : static const char *
4240 : 0 : dwarf_virtuality_name (unsigned int code)
4241 : : {
4242 [ # # ]: 0 : const char *ret = dwarf_virtuality_string (code);
4243 : 0 : return string_or_unknown (ret, code, 0, 0, false);
4244 : : }
4245 : :
4246 : :
4247 : : static const char *
4248 : 0 : dwarf_identifier_case_name (unsigned int code)
4249 : : {
4250 [ # # ]: 0 : const char *ret = dwarf_identifier_case_string (code);
4251 : 0 : return string_or_unknown (ret, code, 0, 0, false);
4252 : : }
4253 : :
4254 : :
4255 : : static const char *
4256 : 0 : dwarf_calling_convention_name (unsigned int code)
4257 : : {
4258 [ # # ]: 0 : const char *ret = dwarf_calling_convention_string (code);
4259 : 0 : return string_or_unknown (ret, code, DW_CC_lo_user, DW_CC_hi_user, false);
4260 : : }
4261 : :
4262 : :
4263 : : static const char *
4264 : 0 : dwarf_ordering_name (unsigned int code)
4265 : : {
4266 [ # # ]: 0 : const char *ret = dwarf_ordering_string (code);
4267 : 0 : return string_or_unknown (ret, code, 0, 0, false);
4268 : : }
4269 : :
4270 : :
4271 : : static const char *
4272 : 8 : dwarf_discr_list_name (unsigned int code)
4273 : : {
4274 [ + - ]: 8 : const char *ret = dwarf_discr_list_string (code);
4275 : 8 : return string_or_unknown (ret, code, 0, 0, false);
4276 : : }
4277 : :
4278 : :
4279 : : static const char *
4280 : 8 : dwarf_unit_name (unsigned int type)
4281 : : {
4282 : 8 : const char *ret = dwarf_unit_string (type);
4283 : 8 : return string_or_unknown (ret, type, DW_UT_lo_user, DW_UT_hi_user, true);
4284 : : }
4285 : :
4286 : :
4287 : : static const char *
4288 : 16 : dwarf_range_list_encoding_name (unsigned int kind)
4289 : : {
4290 : 16 : const char *ret = dwarf_range_list_encoding_string (kind);
4291 : 16 : return string_or_unknown (ret, kind, 0, 0, false);
4292 : : }
4293 : :
4294 : :
4295 : : static const char *
4296 : 91 : dwarf_loc_list_encoding_name (unsigned int kind)
4297 : : {
4298 : 91 : const char *ret = dwarf_loc_list_encoding_string (kind);
4299 : 91 : return string_or_unknown (ret, kind, 0, 0, false);
4300 : : }
4301 : :
4302 : :
4303 : : static const char *
4304 : 6 : dwarf_line_content_description_name (unsigned int kind)
4305 : : {
4306 : 6 : const char *ret = dwarf_line_content_description_string (kind);
4307 : 6 : return string_or_unknown (ret, kind, DW_LNCT_lo_user, DW_LNCT_hi_user,
4308 : : false);
4309 : : }
4310 : :
4311 : :
4312 : : static void
4313 : 188 : print_block (size_t n, const void *block)
4314 : : {
4315 [ - + ]: 188 : if (n == 0)
4316 : 0 : puts (_("empty block"));
4317 : : else
4318 : : {
4319 : 188 : printf (_("%zu byte block:"), n);
4320 : 188 : const unsigned char *data = block;
4321 : 827 : do
4322 : 827 : printf (" %02x", *data++);
4323 [ + + ]: 827 : while (--n > 0);
4324 : 188 : putchar ('\n');
4325 : : }
4326 : 188 : }
4327 : :
4328 : : static void
4329 : 0 : print_bytes (size_t n, const unsigned char *bytes)
4330 : : {
4331 : 0 : while (n-- > 0)
4332 : : {
4333 : 0 : printf ("%02x", *bytes++);
4334 [ # # ]: 0 : if (n > 0)
4335 [ # # ]: 0 : printf (" ");
4336 : : }
4337 : 0 : }
4338 : :
4339 : : static int
4340 : 68 : get_indexed_addr (Dwarf_CU *cu, Dwarf_Word idx, Dwarf_Addr *addr)
4341 : : {
4342 [ + - ]: 68 : if (cu == NULL)
4343 : : return -1;
4344 : :
4345 : 68 : Elf_Data *debug_addr = cu->dbg->sectiondata[IDX_debug_addr];
4346 [ + - ]: 68 : if (debug_addr == NULL)
4347 : : return -1;
4348 : :
4349 : 68 : Dwarf_Off base = __libdw_cu_addr_base (cu);
4350 : 68 : Dwarf_Word off = idx * cu->address_size;
4351 [ + - ]: 68 : if (base > debug_addr->d_size
4352 [ + - ]: 68 : || off > debug_addr->d_size - base
4353 [ + - ]: 68 : || cu->address_size > debug_addr->d_size - base - off)
4354 : : return -1;
4355 : :
4356 : 68 : const unsigned char *addrp = debug_addr->d_buf + base + off;
4357 [ - + ]: 68 : if (cu->address_size == 4)
4358 [ # # ]: 0 : *addr = read_4ubyte_unaligned (cu->dbg, addrp);
4359 : : else
4360 [ - + ]: 136 : *addr = read_8ubyte_unaligned (cu->dbg, addrp);
4361 : :
4362 : : return 0;
4363 : : }
4364 : :
4365 : : static void
4366 : 428464 : print_ops (Dwfl_Module *dwflmod, Dwarf *dbg, int indent, int indentrest,
4367 : : unsigned int vers, unsigned int addrsize, unsigned int offset_size,
4368 : : struct Dwarf_CU *cu, Dwarf_Word len, const unsigned char *data)
4369 : : {
4370 [ + + ]: 428464 : const unsigned int ref_size = vers < 3 ? addrsize : offset_size;
4371 : :
4372 [ - + ]: 428464 : if (len == 0)
4373 : : {
4374 : 0 : printf ("%*s(empty)\n", indent, "");
4375 : 0 : return;
4376 : : }
4377 : :
4378 : : #define NEED(n) if (len < (Dwarf_Word) (n)) goto invalid
4379 : : #define CONSUME(n) NEED (n); else len -= (n)
4380 : :
4381 : : Dwarf_Word offset = 0;
4382 [ + + ]: 1053402 : while (len-- > 0)
4383 : : {
4384 : 624938 : uint_fast8_t op = *data++;
4385 : :
4386 [ + - ]: 624938 : const char *op_name = dwarf_locexpr_opcode_string (op);
4387 [ - + ]: 624938 : if (unlikely (op_name == NULL))
4388 : : {
4389 : 0 : static char buf[20];
4390 [ # # ]: 0 : if (op >= DW_OP_lo_user)
4391 : 0 : snprintf (buf, sizeof buf, "lo_user+%#x", op - DW_OP_lo_user);
4392 : : else
4393 : 0 : snprintf (buf, sizeof buf, "??? (%#x)", op);
4394 : : op_name = buf;
4395 : : }
4396 : :
4397 [ + - + + : 624938 : switch (op)
+ - + + -
- + + - +
- - + + +
+ + - - -
- + + + ]
4398 : : {
4399 : 12786 : case DW_OP_addr:;
4400 : : /* Address operand. */
4401 : 12786 : Dwarf_Word addr;
4402 [ - + ]: 12786 : NEED (addrsize);
4403 [ + + ]: 12786 : if (addrsize == 4)
4404 [ + + ]: 48 : addr = read_4ubyte_unaligned (dbg, data);
4405 [ + - ]: 12762 : else if (addrsize == 8)
4406 [ + + ]: 25524 : addr = read_8ubyte_unaligned (dbg, data);
4407 : : else
4408 : 0 : goto invalid;
4409 : 12786 : data += addrsize;
4410 : 12786 : CONSUME (addrsize);
4411 : :
4412 : 12786 : printf ("%*s[%2" PRIuMAX "] %s ",
4413 : : indent, "", (uintmax_t) offset, op_name);
4414 : 12786 : print_dwarf_addr (dwflmod, 0, addr, addr);
4415 : 12786 : printf ("\n");
4416 : :
4417 : 12786 : offset += 1 + addrsize;
4418 : 12786 : break;
4419 : :
4420 : 0 : case DW_OP_call_ref:
4421 : : case DW_OP_GNU_variable_value:
4422 : : /* Offset operand. */
4423 [ # # ]: 0 : if (ref_size != 4 && ref_size != 8)
4424 : 0 : goto invalid; /* Cannot be used in CFA. */
4425 [ # # ]: 0 : NEED (ref_size);
4426 [ # # ]: 0 : if (ref_size == 4)
4427 [ # # ]: 0 : addr = read_4ubyte_unaligned (dbg, data);
4428 : : else
4429 [ # # ]: 0 : addr = read_8ubyte_unaligned (dbg, data);
4430 : 0 : data += ref_size;
4431 : 0 : CONSUME (ref_size);
4432 : : /* addr is a DIE offset, so format it as one. */
4433 : 0 : printf ("%*s[%2" PRIuMAX "] %s [%6" PRIxMAX "]\n",
4434 : : indent, "", (uintmax_t) offset,
4435 : : op_name, (uintmax_t) addr);
4436 : 0 : offset += 1 + ref_size;
4437 : 0 : break;
4438 : :
4439 : 16497 : case DW_OP_deref_size:
4440 : : case DW_OP_xderef_size:
4441 : : case DW_OP_pick:
4442 : : case DW_OP_const1u:
4443 : : // XXX value might be modified by relocation
4444 [ - + ]: 16497 : NEED (1);
4445 : 32994 : printf ("%*s[%2" PRIuMAX "] %s %" PRIu8 "\n",
4446 : : indent, "", (uintmax_t) offset,
4447 : 16497 : op_name, *((uint8_t *) data));
4448 : 16497 : ++data;
4449 : 16497 : --len;
4450 : 16497 : offset += 2;
4451 : 16497 : break;
4452 : :
4453 : 2533 : case DW_OP_const2u:
4454 [ - + ]: 2533 : NEED (2);
4455 : : // XXX value might be modified by relocation
4456 : 7599 : printf ("%*s[%2" PRIuMAX "] %s %" PRIu16 "\n",
4457 : : indent, "", (uintmax_t) offset,
4458 [ - + ]: 5066 : op_name, read_2ubyte_unaligned (dbg, data));
4459 : 2533 : CONSUME (2);
4460 : 2533 : data += 2;
4461 : 2533 : offset += 3;
4462 : 2533 : break;
4463 : :
4464 : 1908 : case DW_OP_const4u:
4465 [ - + ]: 1908 : NEED (4);
4466 : : // XXX value might be modified by relocation
4467 [ - + ]: 3816 : printf ("%*s[%2" PRIuMAX "] %s %" PRIu32 "\n",
4468 : : indent, "", (uintmax_t) offset,
4469 : : op_name, read_4ubyte_unaligned (dbg, data));
4470 : 1908 : CONSUME (4);
4471 : 1908 : data += 4;
4472 : 1908 : offset += 5;
4473 : 1908 : break;
4474 : :
4475 : 0 : case DW_OP_const8u:
4476 [ # # ]: 0 : NEED (8);
4477 : : // XXX value might be modified by relocation
4478 : 0 : printf ("%*s[%2" PRIuMAX "] %s %" PRIu64 "\n",
4479 : : indent, "", (uintmax_t) offset,
4480 [ # # ]: 0 : op_name, (uint64_t) read_8ubyte_unaligned (dbg, data));
4481 : 0 : CONSUME (8);
4482 : 0 : data += 8;
4483 : 0 : offset += 9;
4484 : 0 : break;
4485 : :
4486 : 3000 : case DW_OP_const1s:
4487 [ - + ]: 3000 : NEED (1);
4488 : : // XXX value might be modified by relocation
4489 : 6000 : printf ("%*s[%2" PRIuMAX "] %s %" PRId8 "\n",
4490 : : indent, "", (uintmax_t) offset,
4491 : 3000 : op_name, *((int8_t *) data));
4492 : 3000 : ++data;
4493 : 3000 : --len;
4494 : 3000 : offset += 2;
4495 : 3000 : break;
4496 : :
4497 : 10 : case DW_OP_const2s:
4498 [ - + ]: 10 : NEED (2);
4499 : : // XXX value might be modified by relocation
4500 : 30 : printf ("%*s[%2" PRIuMAX "] %s %" PRId16 "\n",
4501 : : indent, "", (uintmax_t) offset,
4502 [ - + ]: 20 : op_name, read_2sbyte_unaligned (dbg, data));
4503 : 10 : CONSUME (2);
4504 : 10 : data += 2;
4505 : 10 : offset += 3;
4506 : 10 : break;
4507 : :
4508 : 0 : case DW_OP_const4s:
4509 [ # # ]: 0 : NEED (4);
4510 : : // XXX value might be modified by relocation
4511 [ # # ]: 0 : printf ("%*s[%2" PRIuMAX "] %s %" PRId32 "\n",
4512 : : indent, "", (uintmax_t) offset,
4513 : : op_name, read_4sbyte_unaligned (dbg, data));
4514 : 0 : CONSUME (4);
4515 : 0 : data += 4;
4516 : 0 : offset += 5;
4517 : 0 : break;
4518 : :
4519 : 0 : case DW_OP_const8s:
4520 [ # # ]: 0 : NEED (8);
4521 : : // XXX value might be modified by relocation
4522 [ # # ]: 0 : printf ("%*s[%2" PRIuMAX "] %s %" PRId64 "\n",
4523 : : indent, "", (uintmax_t) offset,
4524 : : op_name, read_8sbyte_unaligned (dbg, data));
4525 : 0 : CONSUME (8);
4526 : 0 : data += 8;
4527 : 0 : offset += 9;
4528 : 0 : break;
4529 : :
4530 : 7315 : case DW_OP_piece:
4531 : : case DW_OP_regx:
4532 : : case DW_OP_plus_uconst:
4533 : 7315 : case DW_OP_constu:;
4534 : 7315 : const unsigned char *start = data;
4535 : 7315 : uint64_t uleb;
4536 [ - + ]: 7315 : NEED (1);
4537 : 7315 : get_uleb128 (uleb, data, data + len);
4538 : 7315 : printf ("%*s[%2" PRIuMAX "] %s %" PRIu64 "\n",
4539 : : indent, "", (uintmax_t) offset, op_name, uleb);
4540 [ - + ]: 7315 : CONSUME (data - start);
4541 : 7315 : offset += 1 + (data - start);
4542 : 7315 : break;
4543 : :
4544 : 2 : case DW_OP_addrx:
4545 : : case DW_OP_GNU_addr_index:
4546 : : case DW_OP_constx:
4547 : 2 : case DW_OP_GNU_const_index:;
4548 : 2 : start = data;
4549 [ - + ]: 2 : NEED (1);
4550 : 2 : get_uleb128 (uleb, data, data + len);
4551 : 2 : printf ("%*s[%2" PRIuMAX "] %s [%" PRIu64 "] ",
4552 : : indent, "", (uintmax_t) offset, op_name, uleb);
4553 [ - + ]: 2 : CONSUME (data - start);
4554 : 2 : offset += 1 + (data - start);
4555 [ - + ]: 2 : if (get_indexed_addr (cu, uleb, &addr) != 0)
4556 : 0 : printf ("???\n");
4557 : : else
4558 : : {
4559 : 2 : print_dwarf_addr (dwflmod, 0, addr, addr);
4560 : 2 : printf ("\n");
4561 : : }
4562 : : break;
4563 : :
4564 : 0 : case DW_OP_bit_piece:
4565 : 0 : start = data;
4566 : 0 : uint64_t uleb2;
4567 [ # # ]: 0 : NEED (1);
4568 : 0 : get_uleb128 (uleb, data, data + len);
4569 : 0 : NEED (1);
4570 : 0 : get_uleb128 (uleb2, data, data + len);
4571 : 0 : printf ("%*s[%2" PRIuMAX "] %s %" PRIu64 ", %" PRIu64 "\n",
4572 : : indent, "", (uintmax_t) offset, op_name, uleb, uleb2);
4573 [ # # ]: 0 : CONSUME (data - start);
4574 : 0 : offset += 1 + (data - start);
4575 : 0 : break;
4576 : :
4577 : 96419 : case DW_OP_fbreg:
4578 : : case DW_OP_breg0 ... DW_OP_breg31:
4579 : : case DW_OP_consts:
4580 : 96419 : start = data;
4581 : 96419 : int64_t sleb;
4582 [ - + ]: 96419 : NEED (1);
4583 : 96419 : get_sleb128 (sleb, data, data + len);
4584 : 96419 : printf ("%*s[%2" PRIuMAX "] %s %" PRId64 "\n",
4585 : : indent, "", (uintmax_t) offset, op_name, sleb);
4586 [ - + ]: 96419 : CONSUME (data - start);
4587 : 96419 : offset += 1 + (data - start);
4588 : 96419 : break;
4589 : :
4590 : 0 : case DW_OP_bregx:
4591 : 0 : start = data;
4592 [ # # ]: 0 : NEED (1);
4593 : 0 : get_uleb128 (uleb, data, data + len);
4594 : 0 : NEED (1);
4595 : 0 : get_sleb128 (sleb, data, data + len);
4596 : 0 : printf ("%*s[%2" PRIuMAX "] %s %" PRIu64 " %" PRId64 "\n",
4597 : : indent, "", (uintmax_t) offset, op_name, uleb, sleb);
4598 [ # # ]: 0 : CONSUME (data - start);
4599 : 0 : offset += 1 + (data - start);
4600 : 0 : break;
4601 : :
4602 : 0 : case DW_OP_call2:
4603 [ # # ]: 0 : NEED (2);
4604 : 0 : printf ("%*s[%2" PRIuMAX "] %s [%6" PRIx16 "]\n",
4605 : : indent, "", (uintmax_t) offset, op_name,
4606 [ # # ]: 0 : read_2ubyte_unaligned (dbg, data));
4607 : 0 : CONSUME (2);
4608 : 0 : data += 2;
4609 : 0 : offset += 3;
4610 : 0 : break;
4611 : :
4612 : 4 : case DW_OP_call4:
4613 [ - + ]: 4 : NEED (4);
4614 [ - + ]: 8 : printf ("%*s[%2" PRIuMAX "] %s [%6" PRIx32 "]\n",
4615 : : indent, "", (uintmax_t) offset, op_name,
4616 : : read_4ubyte_unaligned (dbg, data));
4617 : 4 : CONSUME (4);
4618 : 4 : data += 4;
4619 : 4 : offset += 5;
4620 : 4 : break;
4621 : :
4622 : 217 : case DW_OP_skip:
4623 : : case DW_OP_bra:
4624 [ - + ]: 217 : NEED (2);
4625 : 434 : printf ("%*s[%2" PRIuMAX "] %s %" PRIuMAX "\n",
4626 : : indent, "", (uintmax_t) offset, op_name,
4627 [ - + ]: 434 : (uintmax_t) (offset + read_2sbyte_unaligned (dbg, data) + 3));
4628 : 217 : CONSUME (2);
4629 : 217 : data += 2;
4630 : 217 : offset += 3;
4631 : 217 : break;
4632 : :
4633 : 188 : case DW_OP_implicit_value:
4634 : 188 : start = data;
4635 [ - + ]: 188 : NEED (1);
4636 : 188 : get_uleb128 (uleb, data, data + len);
4637 : 188 : printf ("%*s[%2" PRIuMAX "] %s: ",
4638 : : indent, "", (uintmax_t) offset, op_name);
4639 [ - + ]: 188 : NEED (uleb);
4640 : 188 : print_block (uleb, data);
4641 : 188 : data += uleb;
4642 [ - + ]: 188 : CONSUME (data - start);
4643 : 188 : offset += 1 + (data - start);
4644 : 188 : break;
4645 : :
4646 : 2627 : case DW_OP_implicit_pointer:
4647 : : case DW_OP_GNU_implicit_pointer:
4648 : : /* DIE offset operand. */
4649 : 2627 : start = data;
4650 [ - + ]: 2627 : NEED (ref_size);
4651 [ - + ]: 2627 : if (ref_size != 4 && ref_size != 8)
4652 : 0 : goto invalid; /* Cannot be used in CFA. */
4653 [ + - ]: 2627 : if (ref_size == 4)
4654 [ - + ]: 5254 : addr = read_4ubyte_unaligned (dbg, data);
4655 : : else
4656 [ # # ]: 0 : addr = read_8ubyte_unaligned (dbg, data);
4657 : 2627 : data += ref_size;
4658 : : /* Byte offset operand. */
4659 [ - + ]: 2627 : NEED (1);
4660 : 2627 : get_sleb128 (sleb, data, data + len);
4661 : :
4662 : 2627 : printf ("%*s[%2" PRIuMAX "] %s [%6" PRIxMAX "] %+" PRId64 "\n",
4663 : : indent, "", (intmax_t) offset,
4664 : : op_name, (uintmax_t) addr, sleb);
4665 [ - + ]: 2627 : CONSUME (data - start);
4666 : 2627 : offset += 1 + (data - start);
4667 : 2627 : break;
4668 : :
4669 : 42707 : case DW_OP_entry_value:
4670 : : case DW_OP_GNU_entry_value:
4671 : : /* Size plus expression block. */
4672 : 42707 : start = data;
4673 [ - + ]: 42707 : NEED (1);
4674 : 42707 : get_uleb128 (uleb, data, data + len);
4675 : 42707 : printf ("%*s[%2" PRIuMAX "] %s:\n",
4676 : : indent, "", (uintmax_t) offset, op_name);
4677 [ - + ]: 42707 : NEED (uleb);
4678 : 42707 : print_ops (dwflmod, dbg, indent + 5, indent + 5, vers,
4679 : : addrsize, offset_size, cu, uleb, data);
4680 : 42707 : data += uleb;
4681 [ - + ]: 42707 : CONSUME (data - start);
4682 : 42707 : offset += 1 + (data - start);
4683 : 42707 : break;
4684 : :
4685 : 0 : case DW_OP_const_type:
4686 : : case DW_OP_GNU_const_type:
4687 : : /* uleb128 CU relative DW_TAG_base_type DIE offset, 1-byte
4688 : : unsigned size plus block. */
4689 : 0 : start = data;
4690 [ # # ]: 0 : NEED (1);
4691 : 0 : get_uleb128 (uleb, data, data + len);
4692 [ # # # # ]: 0 : if (! print_unresolved_addresses && cu != NULL)
4693 : 0 : uleb += cu->start;
4694 : 0 : NEED (1);
4695 : 0 : uint8_t usize = *(uint8_t *) data++;
4696 [ # # ]: 0 : NEED (usize);
4697 : 0 : printf ("%*s[%2" PRIuMAX "] %s [%6" PRIxMAX "] ",
4698 : : indent, "", (uintmax_t) offset, op_name, uleb);
4699 : 0 : print_block (usize, data);
4700 : 0 : data += usize;
4701 [ # # ]: 0 : CONSUME (data - start);
4702 : 0 : offset += 1 + (data - start);
4703 : 0 : break;
4704 : :
4705 : 0 : case DW_OP_regval_type:
4706 : : case DW_OP_GNU_regval_type:
4707 : : /* uleb128 register number, uleb128 CU relative
4708 : : DW_TAG_base_type DIE offset. */
4709 : 0 : start = data;
4710 [ # # ]: 0 : NEED (1);
4711 : 0 : get_uleb128 (uleb, data, data + len);
4712 : 0 : NEED (1);
4713 : 0 : get_uleb128 (uleb2, data, data + len);
4714 [ # # # # ]: 0 : if (! print_unresolved_addresses && cu != NULL)
4715 : 0 : uleb2 += cu->start;
4716 : 0 : printf ("%*s[%2" PRIuMAX "] %s %" PRIu64 " [%6" PRIx64 "]\n",
4717 : : indent, "", (uintmax_t) offset, op_name, uleb, uleb2);
4718 [ # # ]: 0 : CONSUME (data - start);
4719 : 0 : offset += 1 + (data - start);
4720 : 0 : break;
4721 : :
4722 : 0 : case DW_OP_deref_type:
4723 : : case DW_OP_GNU_deref_type:
4724 : : /* 1-byte unsigned size of value, uleb128 CU relative
4725 : : DW_TAG_base_type DIE offset. */
4726 : 0 : start = data;
4727 [ # # ]: 0 : NEED (1);
4728 : 0 : usize = *(uint8_t *) data++;
4729 : 0 : NEED (1);
4730 : 0 : get_uleb128 (uleb, data, data + len);
4731 [ # # # # ]: 0 : if (! print_unresolved_addresses && cu != NULL)
4732 : 0 : uleb += cu->start;
4733 : 0 : printf ("%*s[%2" PRIuMAX "] %s %" PRIu8 " [%6" PRIxMAX "]\n",
4734 : : indent, "", (uintmax_t) offset,
4735 : : op_name, usize, uleb);
4736 [ # # ]: 0 : CONSUME (data - start);
4737 : 0 : offset += 1 + (data - start);
4738 : 0 : break;
4739 : :
4740 : 0 : case DW_OP_xderef_type:
4741 : : /* 1-byte unsigned size of value, uleb128 base_type DIE offset. */
4742 : 0 : start = data;
4743 [ # # ]: 0 : NEED (1);
4744 : 0 : usize = *(uint8_t *) data++;
4745 : 0 : NEED (1);
4746 : 0 : get_uleb128 (uleb, data, data + len);
4747 : 0 : printf ("%*s[%4" PRIuMAX "] %s %" PRIu8 " [%6" PRIxMAX "]\n",
4748 : : indent, "", (uintmax_t) offset,
4749 : : op_name, usize, uleb);
4750 [ # # ]: 0 : CONSUME (data - start);
4751 : 0 : offset += 1 + (data - start);
4752 : 0 : break;
4753 : :
4754 : 417 : case DW_OP_convert:
4755 : : case DW_OP_GNU_convert:
4756 : : case DW_OP_reinterpret:
4757 : : case DW_OP_GNU_reinterpret:
4758 : : /* uleb128 CU relative offset to DW_TAG_base_type, or zero
4759 : : for conversion to untyped. */
4760 : 417 : start = data;
4761 [ - + ]: 417 : NEED (1);
4762 : 417 : get_uleb128 (uleb, data, data + len);
4763 [ + + + - : 417 : if (uleb != 0 && ! print_unresolved_addresses && cu != NULL)
+ - ]
4764 : 278 : uleb += cu->start;
4765 : 417 : printf ("%*s[%2" PRIuMAX "] %s [%6" PRIxMAX "]\n",
4766 : : indent, "", (uintmax_t) offset, op_name, uleb);
4767 [ - + ]: 417 : CONSUME (data - start);
4768 : 417 : offset += 1 + (data - start);
4769 : 417 : break;
4770 : :
4771 : 3 : case DW_OP_GNU_parameter_ref:
4772 : : /* 4 byte CU relative reference to the abstract optimized away
4773 : : DW_TAG_formal_parameter. */
4774 [ - + ]: 3 : NEED (4);
4775 [ - + ]: 3 : uintmax_t param_off = (uintmax_t) read_4ubyte_unaligned (dbg, data);
4776 [ + - + - ]: 3 : if (! print_unresolved_addresses && cu != NULL)
4777 : 3 : param_off += cu->start;
4778 : 3 : printf ("%*s[%2" PRIuMAX "] %s [%6" PRIxMAX "]\n",
4779 : : indent, "", (uintmax_t) offset, op_name, param_off);
4780 : 3 : CONSUME (4);
4781 : 3 : data += 4;
4782 : 3 : offset += 5;
4783 : 3 : break;
4784 : :
4785 : : default:
4786 : : /* No Operand. */
4787 : 438305 : printf ("%*s[%2" PRIuMAX "] %s\n",
4788 : : indent, "", (uintmax_t) offset, op_name);
4789 : 438305 : ++offset;
4790 : 438305 : break;
4791 : : }
4792 : :
4793 : 624938 : indent = indentrest;
4794 : 624938 : continue;
4795 : :
4796 : 0 : invalid:
4797 : 0 : printf (_("%*s[%2" PRIuMAX "] %s <TRUNCATED>\n"),
4798 : : indent, "", (uintmax_t) offset, op_name);
4799 : : break;
4800 : : }
4801 : : }
4802 : :
4803 : :
4804 : : struct listptr
4805 : : {
4806 : : Dwarf_Off offset:(64 - 3);
4807 : : bool addr64:1;
4808 : : bool dwarf64:1;
4809 : : bool warned:1;
4810 : : struct Dwarf_CU *cu;
4811 : : unsigned int attr;
4812 : : };
4813 : :
4814 : : #define listptr_offset_size(p) ((p)->dwarf64 ? 8 : 4)
4815 : : #define listptr_address_size(p) ((p)->addr64 ? 8 : 4)
4816 : :
4817 : : static Dwarf_Addr
4818 : 128950 : cudie_base (Dwarf_Die *cudie)
4819 : : {
4820 : 128950 : Dwarf_Addr base;
4821 : : /* Find the base address of the compilation unit. It will normally
4822 : : be specified by DW_AT_low_pc. In DWARF-3 draft 4, the base
4823 : : address could be overridden by DW_AT_entry_pc. It's been
4824 : : removed, but GCC emits DW_AT_entry_pc and not DW_AT_lowpc for
4825 : : compilation units with discontinuous ranges. */
4826 [ - + ]: 128950 : if (unlikely (dwarf_lowpc (cudie, &base) != 0))
4827 : : {
4828 : 0 : Dwarf_Attribute attr_mem;
4829 [ # # ]: 0 : if (dwarf_formaddr (dwarf_attr (cudie, DW_AT_entry_pc, &attr_mem),
4830 : : &base) != 0)
4831 : 0 : base = 0;
4832 : : }
4833 : 128950 : return base;
4834 : : }
4835 : :
4836 : : static Dwarf_Addr
4837 : 128950 : listptr_base (struct listptr *p)
4838 : : {
4839 : 128950 : Dwarf_Die cu = CUDIE (p->cu);
4840 : 128950 : return cudie_base (&cu);
4841 : : }
4842 : :
4843 : : /* To store the name used in compare_listptr */
4844 : : static const char *sort_listptr_name;
4845 : :
4846 : : static int
4847 : 864980 : compare_listptr (const void *a, const void *b)
4848 : : {
4849 : 864980 : const char *name = sort_listptr_name;
4850 : 864980 : struct listptr *p1 = (void *) a;
4851 : 864980 : struct listptr *p2 = (void *) b;
4852 : :
4853 [ + + ]: 864980 : if (p1->offset < p2->offset)
4854 : : return -1;
4855 [ + + ]: 82585 : if (p1->offset > p2->offset)
4856 : : return 1;
4857 : :
4858 [ + - + - ]: 3742 : if (!p1->warned && !p2->warned)
4859 : : {
4860 [ - + ]: 3742 : if (p1->addr64 != p2->addr64)
4861 : : {
4862 : 0 : p1->warned = p2->warned = true;
4863 : 3742 : error (0, 0,
4864 : 0 : _("%s %#" PRIx64 " used with different address sizes"),
4865 : 0 : name, (uint64_t) p1->offset);
4866 : : }
4867 [ - + ]: 3742 : if (p1->dwarf64 != p2->dwarf64)
4868 : : {
4869 : 0 : p1->warned = p2->warned = true;
4870 : 3742 : error (0, 0,
4871 : 0 : _("%s %#" PRIx64 " used with different offset sizes"),
4872 : 0 : name, (uint64_t) p1->offset);
4873 : : }
4874 [ - + ]: 3742 : if (listptr_base (p1) != listptr_base (p2))
4875 : : {
4876 : 0 : p1->warned = p2->warned = true;
4877 : 3742 : error (0, 0,
4878 : 0 : _("%s %#" PRIx64 " used with different base addresses"),
4879 : 0 : name, (uint64_t) p1->offset);
4880 : : }
4881 [ - + ]: 3742 : if (p1->attr != p2 ->attr)
4882 : : {
4883 : 0 : p1->warned = p2->warned = true;
4884 : 0 : error (0, 0,
4885 : 0 : _("%s %#" PRIx64
4886 : : " used with different attribute %s and %s"),
4887 : 0 : name, (uint64_t) p1->offset, dwarf_attr_name (p1->attr),
4888 : : dwarf_attr_name (p2->attr));
4889 : : }
4890 : : }
4891 : :
4892 : : return 0;
4893 : : }
4894 : :
4895 : : struct listptr_table
4896 : : {
4897 : : size_t n;
4898 : : size_t alloc;
4899 : : struct listptr *table;
4900 : : };
4901 : :
4902 : : static struct listptr_table known_locsptr;
4903 : : static struct listptr_table known_loclistsptr;
4904 : : static struct listptr_table known_rangelistptr;
4905 : : static struct listptr_table known_rnglistptr;
4906 : : static struct listptr_table known_addrbases;
4907 : : static struct listptr_table known_stroffbases;
4908 : :
4909 : : static void
4910 : 269 : reset_listptr (struct listptr_table *table)
4911 : : {
4912 : 269 : free (table->table);
4913 : 269 : table->table = NULL;
4914 : 269 : table->n = table->alloc = 0;
4915 : : }
4916 : :
4917 : : /* Returns false if offset doesn't fit. See struct listptr. */
4918 : : static bool
4919 : 125117 : notice_listptr (enum section_e section, struct listptr_table *table,
4920 : : uint_fast8_t address_size, uint_fast8_t offset_size,
4921 : : struct Dwarf_CU *cu, Dwarf_Off offset, unsigned int attr)
4922 : : {
4923 [ + + ]: 125117 : if (print_debug_sections & section)
4924 : : {
4925 [ + + ]: 124991 : if (table->n == table->alloc)
4926 : : {
4927 [ + + ]: 192 : if (table->alloc == 0)
4928 : 81 : table->alloc = 128;
4929 : : else
4930 : 111 : table->alloc *= 2;
4931 : 192 : table->table = xrealloc (table->table,
4932 : 192 : table->alloc * sizeof table->table[0]);
4933 : : }
4934 : :
4935 : 124991 : struct listptr *p = &table->table[table->n++];
4936 : :
4937 : 124991 : *p = (struct listptr)
4938 : : {
4939 : 124991 : .addr64 = address_size == 8,
4940 : 124991 : .dwarf64 = offset_size == 8,
4941 : : .offset = offset,
4942 : : .cu = cu,
4943 : : .attr = attr
4944 : : };
4945 : :
4946 [ - + ]: 124991 : if (p->offset != offset)
4947 : : {
4948 : 0 : table->n--;
4949 : 0 : return false;
4950 : : }
4951 : : }
4952 : : return true;
4953 : : }
4954 : :
4955 : : static void
4956 : 85 : sort_listptr (struct listptr_table *table, const char *name)
4957 : : {
4958 [ + + ]: 85 : if (table->n > 0)
4959 : : {
4960 : 81 : sort_listptr_name = name;
4961 : 81 : qsort (table->table, table->n, sizeof table->table[0],
4962 : : &compare_listptr);
4963 : : }
4964 : 85 : }
4965 : :
4966 : : static bool
4967 : 121463 : skip_listptr_hole (struct listptr_table *table, size_t *idxp,
4968 : : uint_fast8_t *address_sizep, uint_fast8_t *offset_sizep,
4969 : : Dwarf_Addr *base, struct Dwarf_CU **cu, ptrdiff_t offset,
4970 : : unsigned char **readp, unsigned char *endp,
4971 : : unsigned int *attr)
4972 : : {
4973 [ + - ]: 121463 : if (table->n == 0)
4974 : : return false;
4975 : :
4976 [ + - + + ]: 192100 : while (*idxp < table->n && table->table[*idxp].offset < (Dwarf_Off) offset)
4977 : 70637 : ++*idxp;
4978 : :
4979 : 121463 : struct listptr *p = &table->table[*idxp];
4980 : :
4981 [ + - ]: 121463 : if (*idxp == table->n
4982 [ - + ]: 121463 : || p->offset >= (Dwarf_Off) (endp - *readp + offset))
4983 : : {
4984 : 0 : *readp = endp;
4985 : 0 : printf (_(" [%6tx] <UNUSED GARBAGE IN REST OF SECTION>\n"),
4986 : : offset);
4987 : 0 : return true;
4988 : : }
4989 : :
4990 [ + + ]: 121463 : if (p->offset != (Dwarf_Off) offset)
4991 : : {
4992 : 5 : *readp += p->offset - offset;
4993 : 5 : printf (_(" [%6tx] <UNUSED GARBAGE> ... %" PRIu64 " bytes ...\n"),
4994 : 5 : offset, (Dwarf_Off) p->offset - offset);
4995 : 5 : return true;
4996 : : }
4997 : :
4998 [ + - ]: 121458 : if (address_sizep != NULL)
4999 [ + + ]: 121462 : *address_sizep = listptr_address_size (p);
5000 [ + + ]: 121458 : if (offset_sizep != NULL)
5001 [ + - ]: 215492 : *offset_sizep = listptr_offset_size (p);
5002 [ + - ]: 121458 : if (base != NULL)
5003 : 121458 : *base = listptr_base (p);
5004 [ + - ]: 121458 : if (cu != NULL)
5005 : 121458 : *cu = p->cu;
5006 [ + + ]: 121458 : if (attr != NULL)
5007 : 107746 : *attr = p->attr;
5008 : :
5009 : : return false;
5010 : : }
5011 : :
5012 : : static Dwarf_Off
5013 : 53837 : next_listptr_offset (struct listptr_table *table, size_t *idxp, Dwarf_Off off)
5014 : : {
5015 : : /* Note that multiple attributes could in theory point to the same loclist
5016 : : offset, so make sure we pick one that is bigger than the current one.
5017 : : The table is sorted on offset. */
5018 [ + - ]: 53837 : if (*idxp < table->n)
5019 : : {
5020 [ + - ]: 54253 : while (++*idxp < table->n)
5021 : : {
5022 : 54253 : Dwarf_Off next = table->table[*idxp].offset;
5023 [ + + ]: 54253 : if (next > off)
5024 : 53837 : return next;
5025 : : }
5026 : : }
5027 : : return 0;
5028 : : }
5029 : :
5030 : : /* Returns the listptr associated with the given index, or NULL. */
5031 : : static struct listptr *
5032 : 356 : get_listptr (struct listptr_table *table, size_t idx)
5033 : : {
5034 : 356 : if (idx >= table->n)
5035 : : return NULL;
5036 : 303 : return &table->table[idx];
5037 : : }
5038 : :
5039 : : /* Returns the next index, base address and CU associated with the
5040 : : list unit offsets. If there is none false is returned, otherwise
5041 : : true. Assumes the table has been sorted. */
5042 : : static bool
5043 : 8 : listptr_cu (struct listptr_table *table, size_t *idxp,
5044 : : Dwarf_Off start, Dwarf_Off end,
5045 : : Dwarf_Addr *base, struct Dwarf_CU **cu)
5046 : : {
5047 [ + - ]: 17 : while (*idxp < table->n
5048 [ + + ]: 17 : && table->table[*idxp].offset < start)
5049 : 9 : ++*idxp;
5050 : :
5051 [ + - ]: 8 : if (*idxp < table->n
5052 [ + - ]: 8 : && table->table[*idxp].offset >= start
5053 [ + - ]: 8 : && table->table[*idxp].offset < end)
5054 : : {
5055 : 8 : struct listptr *p = &table->table[*idxp];
5056 : 8 : *base = listptr_base (p);
5057 : 8 : *cu = p->cu;
5058 : 8 : return true;
5059 : : }
5060 : :
5061 : : return false;
5062 : : }
5063 : :
5064 : : /* Returns the next index with the current CU for the given attribute.
5065 : : If there is none false is returned, otherwise true. Assumes the
5066 : : table has been sorted. */
5067 : : static bool
5068 : 92 : listptr_attr (struct listptr_table *table, size_t idxp,
5069 : : Dwarf_Off offset, unsigned int attr)
5070 : : {
5071 : 346 : struct listptr *listptr;
5072 : 346 : do
5073 : : {
5074 [ + + ]: 346 : listptr = get_listptr (table, idxp);
5075 [ + - ]: 298 : if (listptr == NULL)
5076 : : return false;
5077 : :
5078 [ + + + + ]: 298 : if (listptr->offset == offset && listptr->attr == attr)
5079 : : return true;
5080 : :
5081 : 297 : idxp++;
5082 : : }
5083 [ + + ]: 297 : while (listptr->offset <= offset);
5084 : :
5085 : : return false;
5086 : : }
5087 : :
5088 : : static void
5089 : 40 : print_debug_abbrev_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
5090 : : Ebl *ebl, GElf_Ehdr *ehdr __attribute__ ((unused)),
5091 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
5092 : : {
5093 : 80 : const size_t sh_size = (dbg->sectiondata[IDX_debug_abbrev] ?
5094 [ + - ]: 40 : dbg->sectiondata[IDX_debug_abbrev]->d_size : 0);
5095 : :
5096 : 40 : printf (_("\nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"
5097 : : " [ Code]\n"),
5098 : : elf_ndxscn (scn), section_name (ebl, shdr),
5099 : 40 : (uint64_t) shdr->sh_offset);
5100 : :
5101 : 40 : Dwarf_Off offset = 0;
5102 [ + + ]: 1673 : while (offset < sh_size)
5103 : : {
5104 : 1633 : printf (_("\nAbbreviation section at offset %" PRIu64 ":\n"),
5105 : : offset);
5106 : :
5107 : 149461 : while (1)
5108 : 73914 : {
5109 : 75547 : size_t length;
5110 : 75547 : Dwarf_Abbrev abbrev;
5111 : :
5112 : 75547 : int res = dwarf_offabbrev (dbg, offset, &length, &abbrev);
5113 [ + + ]: 75547 : if (res != 0)
5114 : : {
5115 [ - + ]: 1633 : if (unlikely (res < 0))
5116 : : {
5117 : 0 : printf (_("\
5118 : : *** error while reading abbreviation: %s\n"),
5119 : : dwarf_errmsg (-1));
5120 : 0 : return;
5121 : : }
5122 : :
5123 : : /* This is the NUL byte at the end of the section. */
5124 : 1633 : ++offset;
5125 : 1633 : break;
5126 : : }
5127 : :
5128 : : /* We know these calls can never fail. */
5129 : 73914 : unsigned int code = dwarf_getabbrevcode (&abbrev);
5130 : 73914 : unsigned int tag = dwarf_getabbrevtag (&abbrev);
5131 : 73914 : int has_children = dwarf_abbrevhaschildren (&abbrev);
5132 : :
5133 [ + + ]: 73914 : printf (_(" [%5u] offset: %" PRId64
5134 : : ", children: %s, tag: %s\n"),
5135 : : code, (int64_t) offset,
5136 : : has_children ? yes_str : no_str,
5137 : : dwarf_tag_name (tag));
5138 : :
5139 : 73914 : size_t cnt = 0;
5140 : 73914 : unsigned int name;
5141 : 73914 : unsigned int form;
5142 : 73914 : Dwarf_Sword data;
5143 : 73914 : Dwarf_Off enoffset;
5144 [ + + ]: 418821 : while (dwarf_getabbrevattr_data (&abbrev, cnt, &name, &form,
5145 : : &data, &enoffset) == 0)
5146 : : {
5147 : 344907 : printf (" attr: %s, form: %s",
5148 : : dwarf_attr_name (name), dwarf_form_name (form));
5149 [ - + ]: 344907 : if (form == DW_FORM_implicit_const)
5150 : 0 : printf (" (%" PRId64 ")", data);
5151 : 344907 : printf (", offset: %#" PRIx64 "\n", (uint64_t) enoffset);
5152 : 344907 : ++cnt;
5153 : : }
5154 : :
5155 : 73914 : offset += length;
5156 : : }
5157 : : }
5158 : : }
5159 : :
5160 : :
5161 : : static void
5162 : 2 : print_debug_addr_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
5163 : : Ebl *ebl, GElf_Ehdr *ehdr,
5164 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
5165 : : {
5166 : 2 : printf (_("\
5167 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
5168 : : elf_ndxscn (scn), section_name (ebl, shdr),
5169 : 2 : (uint64_t) shdr->sh_offset);
5170 : :
5171 [ + - ]: 2 : if (shdr->sh_size == 0)
5172 : : return;
5173 : :
5174 : : /* We like to get the section from libdw to make sure they are relocated. */
5175 : 4 : Elf_Data *data = (dbg->sectiondata[IDX_debug_addr]
5176 [ - + ]: 2 : ?: elf_rawdata (scn, NULL));
5177 [ - + ]: 2 : if (unlikely (data == NULL))
5178 : : {
5179 : 0 : error (0, 0, _("cannot get .debug_addr section data: %s"),
5180 : : elf_errmsg (-1));
5181 : 0 : return;
5182 : : }
5183 : :
5184 : 2 : size_t idx = 0;
5185 : 2 : sort_listptr (&known_addrbases, "addr_base");
5186 : :
5187 : 2 : const unsigned char *start = (const unsigned char *) data->d_buf;
5188 : 2 : const unsigned char *readp = start;
5189 : 2 : const unsigned char *readendp = ((const unsigned char *) data->d_buf
5190 : 2 : + data->d_size);
5191 : :
5192 : 2 : while (readp < readendp)
5193 : : {
5194 : : /* We cannot really know whether or not there is an header. The
5195 : : DebugFission extension to DWARF4 doesn't add one. The DWARF5
5196 : : .debug_addr variant does. Whether or not we have an header,
5197 : : DW_AT_[GNU_]addr_base points at "index 0". So if the current
5198 : : offset equals the CU addr_base then we can just start
5199 : : printing addresses. If there is no CU with an exact match
5200 : : then we'll try to parse the header first. */
5201 : 4 : Dwarf_Off off = (Dwarf_Off) (readp
5202 : 4 : - (const unsigned char *) data->d_buf);
5203 : :
5204 : 4 : printf ("Table at offset %" PRIx64 " ", off);
5205 : :
5206 [ + - ]: 4 : struct listptr *listptr = get_listptr (&known_addrbases, idx++);
5207 : 4 : const unsigned char *next_unitp;
5208 : :
5209 : 4 : uint64_t unit_length;
5210 : 4 : uint16_t version;
5211 : 4 : uint8_t address_size;
5212 : 4 : uint8_t segment_size;
5213 [ - + ]: 4 : if (listptr == NULL)
5214 : : {
5215 : 0 : error (0, 0, "Warning: No CU references .debug_addr after %" PRIx64,
5216 : : off);
5217 : :
5218 : : /* We will have to assume it is just addresses to the end... */
5219 [ # # ]: 0 : address_size = ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 4 : 8;
5220 : 0 : next_unitp = readendp;
5221 : 0 : printf ("Unknown CU:\n");
5222 : : }
5223 : : else
5224 : : {
5225 : 4 : Dwarf_Die cudie;
5226 [ - + ]: 4 : if (dwarf_cu_die (listptr->cu, &cudie,
5227 : : NULL, NULL, NULL, NULL,
5228 : : NULL, NULL) == NULL)
5229 : 0 : printf ("Unknown CU (%s):\n", dwarf_errmsg (-1));
5230 : : else
5231 : 4 : printf ("for CU [%6" PRIx64 "]:\n", dwarf_dieoffset (&cudie));
5232 : :
5233 [ + + ]: 4 : if (listptr->offset == off)
5234 : : {
5235 [ - + ]: 2 : address_size = listptr_address_size (listptr);
5236 : 2 : segment_size = 0;
5237 : 2 : version = 4;
5238 : :
5239 : : /* The addresses start here, but where do they end? */
5240 [ + + ]: 2 : listptr = get_listptr (&known_addrbases, idx);
5241 [ + - ]: 1 : if (listptr == NULL)
5242 : : next_unitp = readendp;
5243 [ + - ]: 1 : else if (listptr->cu->version < 5)
5244 : : {
5245 : 1 : next_unitp = start + listptr->offset;
5246 [ + - - + ]: 1 : if (listptr->offset < off || listptr->offset > data->d_size)
5247 : : {
5248 : 0 : error (0, 0,
5249 : : "Warning: Bad address base for next unit at %"
5250 : : PRIx64, off);
5251 : 0 : next_unitp = readendp;
5252 : : }
5253 : : }
5254 : : else
5255 : : {
5256 : : /* Tricky, we don't have a header for this unit, but
5257 : : there is one for the next. We will have to
5258 : : "guess" how big it is and subtract it from the
5259 : : offset (because that points after the header). */
5260 [ # # ]: 0 : unsigned int offset_size = listptr_offset_size (listptr);
5261 : 0 : Dwarf_Off next_off = (listptr->offset
5262 : 0 : - (offset_size == 4 ? 4 : 12) /* len */
5263 : : - 2 /* version */
5264 : : - 1 /* address size */
5265 : 0 : - 1); /* segment selector size */
5266 : 0 : next_unitp = start + next_off;
5267 [ # # # # ]: 0 : if (next_off < off || next_off > data->d_size)
5268 : : {
5269 : 0 : error (0, 0,
5270 : : "Warning: Couldn't calculate .debug_addr "
5271 : : " unit length at %" PRIx64, off);
5272 : 0 : next_unitp = readendp;
5273 : : }
5274 : : }
5275 : 2 : unit_length = (uint64_t) (next_unitp - readp);
5276 : :
5277 : : /* Pretend we have a header. */
5278 : 2 : printf ("\n");
5279 : 2 : printf (_(" Length: %8" PRIu64 "\n"),
5280 : : unit_length);
5281 : 2 : printf (_(" DWARF version: %8" PRIu16 "\n"), version);
5282 : 2 : printf (_(" Address size: %8" PRIu64 "\n"),
5283 : : (uint64_t) address_size);
5284 : 2 : printf (_(" Segment size: %8" PRIu64 "\n"),
5285 : : (uint64_t) segment_size);
5286 : 4 : printf ("\n");
5287 : : }
5288 : : else
5289 : : {
5290 : : /* OK, we have to parse an header first. */
5291 [ - + ]: 2 : unit_length = read_4ubyte_unaligned_inc (dbg, readp);
5292 [ - + ]: 2 : if (unlikely (unit_length == 0xffffffff))
5293 : : {
5294 [ # # ]: 0 : if (unlikely (readp > readendp - 8))
5295 : : {
5296 : 0 : invalid_data:
5297 : 0 : error (0, 0, "Invalid data");
5298 : 0 : return;
5299 : : }
5300 [ # # ]: 0 : unit_length = read_8ubyte_unaligned_inc (dbg, readp);
5301 : : }
5302 : 2 : printf ("\n");
5303 : 2 : printf (_(" Length: %8" PRIu64 "\n"),
5304 : : unit_length);
5305 : :
5306 : : /* We need at least 2-bytes (version) + 1-byte
5307 : : (addr_size) + 1-byte (segment_size) = 4 bytes to
5308 : : complete the header. And this unit cannot go beyond
5309 : : the section data. */
5310 [ + - ]: 2 : if (readp > readendp - 4
5311 [ + - ]: 2 : || unit_length < 4
5312 [ - + ]: 2 : || unit_length > (uint64_t) (readendp - readp))
5313 : 0 : goto invalid_data;
5314 : :
5315 : 2 : next_unitp = readp + unit_length;
5316 : :
5317 [ - + ]: 2 : version = read_2ubyte_unaligned_inc (dbg, readp);
5318 : 2 : printf (_(" DWARF version: %8" PRIu16 "\n"), version);
5319 : :
5320 [ - + ]: 2 : if (version != 5)
5321 : : {
5322 : 0 : error (0, 0, _("Unknown version"));
5323 : 0 : goto next_unit;
5324 : : }
5325 : :
5326 : 2 : address_size = *readp++;
5327 : 2 : printf (_(" Address size: %8" PRIu64 "\n"),
5328 : : (uint64_t) address_size);
5329 : :
5330 [ - + ]: 2 : if (address_size != 4 && address_size != 8)
5331 : : {
5332 : 0 : error (0, 0, _("unsupported address size"));
5333 : 0 : goto next_unit;
5334 : : }
5335 : :
5336 : 2 : segment_size = *readp++;
5337 : 2 : printf (_(" Segment size: %8" PRIu64 "\n"),
5338 : : (uint64_t) segment_size);
5339 : 2 : printf ("\n");
5340 : :
5341 [ - + ]: 2 : if (segment_size != 0)
5342 : : {
5343 : 0 : error (0, 0, _("unsupported segment size"));
5344 : 0 : goto next_unit;
5345 : : }
5346 : :
5347 [ - + ]: 2 : if (listptr->offset != (Dwarf_Off) (readp - start))
5348 : : {
5349 : 0 : error (0, 0, "Address index doesn't start after header");
5350 : 0 : goto next_unit;
5351 : : }
5352 : : }
5353 : : }
5354 : :
5355 : 4 : int digits = 1;
5356 : 4 : size_t addresses = (next_unitp - readp) / address_size;
5357 [ + + ]: 8 : while (addresses >= 10)
5358 : : {
5359 : 4 : ++digits;
5360 : 4 : addresses /= 10;
5361 : : }
5362 : :
5363 : 4 : unsigned int uidx = 0;
5364 : 4 : size_t index_offset = readp - (const unsigned char *) data->d_buf;
5365 : 4 : printf (" Addresses start at offset 0x%zx:\n", index_offset);
5366 : 76 : while (readp <= next_unitp - address_size)
5367 : : {
5368 [ - + - + : 144 : Dwarf_Addr addr = read_addr_unaligned_inc (address_size, dbg,
- - - + ]
5369 : : readp);
5370 : 72 : printf (" [%*u] ", digits, uidx++);
5371 : 72 : print_dwarf_addr (dwflmod, address_size, addr, addr);
5372 [ + + ]: 76 : printf ("\n");
5373 : : }
5374 : 4 : printf ("\n");
5375 : :
5376 [ + - ]: 4 : if (readp != next_unitp)
5377 [ + + ]: 6 : error (0, 0, "extra %zd bytes at end of unit",
5378 : 0 : (size_t) (next_unitp - readp));
5379 : :
5380 : 4 : next_unit:
5381 : : readp = next_unitp;
5382 : : }
5383 : : }
5384 : :
5385 : : /* Print content of DWARF .debug_aranges section. We fortunately do
5386 : : not have to know a bit about the structure of the section, libdwarf
5387 : : takes care of it. */
5388 : : static void
5389 : 1 : print_decoded_aranges_section (Ebl *ebl, GElf_Ehdr *ehdr, Elf_Scn *scn,
5390 : : GElf_Shdr *shdr, Dwarf *dbg)
5391 : : {
5392 : 1 : Dwarf_Aranges *aranges;
5393 : 1 : size_t cnt;
5394 [ - + ]: 1 : if (unlikely (dwarf_getaranges (dbg, &aranges, &cnt) != 0))
5395 : : {
5396 : 0 : error (0, 0, _("cannot get .debug_aranges content: %s"),
5397 : : dwarf_errmsg (-1));
5398 : 0 : return;
5399 : : }
5400 : :
5401 : 1 : GElf_Shdr glink_mem;
5402 : 1 : GElf_Shdr *glink;
5403 : 1 : glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link), &glink_mem);
5404 [ - + ]: 1 : if (glink == NULL)
5405 : : {
5406 : 0 : error (0, 0, _("invalid sh_link value in section %zu"),
5407 : : elf_ndxscn (scn));
5408 : 0 : return;
5409 : : }
5410 : :
5411 : 1 : printf (ngettext ("\
5412 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 " contains %zu entry:\n",
5413 : : "\
5414 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 " contains %zu entries:\n",
5415 : : cnt),
5416 : : elf_ndxscn (scn), section_name (ebl, shdr),
5417 : 1 : (uint64_t) shdr->sh_offset, cnt);
5418 : :
5419 : : /* Compute floor(log16(cnt)). */
5420 : 1 : size_t tmp = cnt;
5421 : 1 : int digits = 1;
5422 [ - + ]: 1 : while (tmp >= 16)
5423 : : {
5424 : 0 : ++digits;
5425 : 0 : tmp >>= 4;
5426 : : }
5427 : :
5428 [ + + ]: 6 : for (size_t n = 0; n < cnt; ++n)
5429 : : {
5430 : 5 : Dwarf_Arange *runp = dwarf_onearange (aranges, n);
5431 [ - + ]: 5 : if (unlikely (runp == NULL))
5432 : : {
5433 : 0 : printf ("cannot get arange %zu: %s\n", n, dwarf_errmsg (-1));
5434 : 0 : return;
5435 : : }
5436 : :
5437 : 5 : Dwarf_Addr start;
5438 : 5 : Dwarf_Word length;
5439 : 5 : Dwarf_Off offset;
5440 : :
5441 [ - + ]: 5 : if (unlikely (dwarf_getarangeinfo (runp, &start, &length, &offset) != 0))
5442 : 0 : printf (_(" [%*zu] ???\n"), digits, n);
5443 : : else
5444 : 5 : printf (_(" [%*zu] start: %0#*" PRIx64
5445 : : ", length: %5" PRIu64 ", CU DIE offset: %6"
5446 : : PRId64 "\n"),
5447 [ - + ]: 5 : digits, n, ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 10 : 18,
5448 : : (uint64_t) start, (uint64_t) length, (int64_t) offset);
5449 : : }
5450 : : }
5451 : :
5452 : :
5453 : : /* Print content of DWARF .debug_aranges section. */
5454 : : static void
5455 : 44 : print_debug_aranges_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
5456 : : Ebl *ebl, GElf_Ehdr *ehdr, Elf_Scn *scn,
5457 : : GElf_Shdr *shdr, Dwarf *dbg)
5458 : : {
5459 [ + + ]: 44 : if (decodedaranges)
5460 : : {
5461 : 1 : print_decoded_aranges_section (ebl, ehdr, scn, shdr, dbg);
5462 : 1 : return;
5463 : : }
5464 : :
5465 : 86 : Elf_Data *data = (dbg->sectiondata[IDX_debug_aranges]
5466 [ - + ]: 43 : ?: elf_rawdata (scn, NULL));
5467 : :
5468 [ - + ]: 43 : if (unlikely (data == NULL))
5469 : : {
5470 : 0 : error (0, 0, _("cannot get .debug_aranges content: %s"),
5471 : : elf_errmsg (-1));
5472 : 0 : return;
5473 : : }
5474 : :
5475 : 43 : printf (_("\
5476 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
5477 : : elf_ndxscn (scn), section_name (ebl, shdr),
5478 : 43 : (uint64_t) shdr->sh_offset);
5479 : :
5480 : 43 : const unsigned char *readp = data->d_buf;
5481 : 43 : const unsigned char *readendp = readp + data->d_size;
5482 : :
5483 [ + + ]: 1673 : while (readp < readendp)
5484 : : {
5485 : 1630 : const unsigned char *hdrstart = readp;
5486 : 1630 : size_t start_offset = hdrstart - (const unsigned char *) data->d_buf;
5487 : :
5488 : 1630 : printf (_("\nTable at offset %zu:\n"), start_offset);
5489 [ - + ]: 1630 : if (readp + 4 > readendp)
5490 : : {
5491 : 0 : invalid_data:
5492 : 0 : error (0, 0, _("invalid data in section [%zu] '%s'"),
5493 : : elf_ndxscn (scn), section_name (ebl, shdr));
5494 : 0 : return;
5495 : : }
5496 : :
5497 [ + + ]: 1630 : Dwarf_Word length = read_4ubyte_unaligned_inc (dbg, readp);
5498 : 1630 : unsigned int length_bytes = 4;
5499 [ - + ]: 1630 : if (length == DWARF3_LENGTH_64_BIT)
5500 : : {
5501 [ # # ]: 0 : if (readp + 8 > readendp)
5502 : 0 : goto invalid_data;
5503 [ # # ]: 0 : length = read_8ubyte_unaligned_inc (dbg, readp);
5504 : 0 : length_bytes = 8;
5505 : : }
5506 : :
5507 : 1630 : const unsigned char *nexthdr = readp + length;
5508 : 1630 : printf (_("\n Length: %6" PRIu64 "\n"),
5509 : : (uint64_t) length);
5510 : :
5511 [ - + ]: 1630 : if (unlikely (length > (size_t) (readendp - readp)))
5512 : 0 : goto invalid_data;
5513 : :
5514 [ - + ]: 1630 : if (length == 0)
5515 : 0 : continue;
5516 : :
5517 [ - + ]: 1630 : if (readp + 2 > readendp)
5518 : 0 : goto invalid_data;
5519 [ + + ]: 1630 : uint_fast16_t version = read_2ubyte_unaligned_inc (dbg, readp);
5520 : 1630 : printf (_(" DWARF version: %6" PRIuFAST16 "\n"),
5521 : : version);
5522 [ - + ]: 1630 : if (version != 2)
5523 : : {
5524 : 0 : error (0, 0, _("unsupported aranges version"));
5525 : 0 : goto next_table;
5526 : : }
5527 : :
5528 : 1630 : Dwarf_Word offset;
5529 [ - + ]: 1630 : if (readp + length_bytes > readendp)
5530 : 0 : goto invalid_data;
5531 [ - + ]: 1630 : if (length_bytes == 8)
5532 [ # # ]: 0 : offset = read_8ubyte_unaligned_inc (dbg, readp);
5533 : : else
5534 [ + + ]: 3260 : offset = read_4ubyte_unaligned_inc (dbg, readp);
5535 : 1630 : printf (_(" CU offset: %6" PRIx64 "\n"),
5536 : : (uint64_t) offset);
5537 : :
5538 [ - + ]: 1630 : if (readp + 1 > readendp)
5539 : 0 : goto invalid_data;
5540 : 1630 : unsigned int address_size = *readp++;
5541 : 1630 : printf (_(" Address size: %6" PRIu64 "\n"),
5542 : : (uint64_t) address_size);
5543 [ - + ]: 1630 : if (address_size != 4 && address_size != 8)
5544 : : {
5545 : 0 : error (0, 0, _("unsupported address size"));
5546 : 0 : goto next_table;
5547 : : }
5548 : :
5549 [ - + ]: 1630 : if (readp + 1 > readendp)
5550 : 0 : goto invalid_data;
5551 : 1630 : unsigned int segment_size = *readp++;
5552 : 1630 : printf (_(" Segment size: %6" PRIu64 "\n\n"),
5553 : : (uint64_t) segment_size);
5554 [ - + - - ]: 1630 : if (segment_size != 0 && segment_size != 4 && segment_size != 8)
5555 : : {
5556 : 0 : error (0, 0, _("unsupported segment size"));
5557 : 0 : goto next_table;
5558 : : }
5559 : :
5560 : : /* Round the address to the next multiple of 2*address_size. */
5561 : 1630 : readp += ((2 * address_size - ((readp - hdrstart) % (2 * address_size)))
5562 : 1630 : % (2 * address_size));
5563 : :
5564 : 1630 : while (readp < nexthdr)
5565 : : {
5566 : 3274 : Dwarf_Word range_address;
5567 : 3274 : Dwarf_Word range_length;
5568 : 3274 : Dwarf_Word segment = 0;
5569 [ - + ]: 3274 : if (readp + 2 * address_size + segment_size > readendp)
5570 : 0 : goto invalid_data;
5571 [ + + ]: 3274 : if (address_size == 4)
5572 : : {
5573 [ + + ]: 36 : range_address = read_4ubyte_unaligned_inc (dbg, readp);
5574 [ + + ]: 72 : range_length = read_4ubyte_unaligned_inc (dbg, readp);
5575 : : }
5576 : : else
5577 : : {
5578 [ + + ]: 3238 : range_address = read_8ubyte_unaligned_inc (dbg, readp);
5579 [ + + ]: 6476 : range_length = read_8ubyte_unaligned_inc (dbg, readp);
5580 : : }
5581 : :
5582 [ - + ]: 3274 : if (segment_size == 4)
5583 [ # # ]: 0 : segment = read_4ubyte_unaligned_inc (dbg, readp);
5584 [ - + ]: 3274 : else if (segment_size == 8)
5585 [ # # ]: 0 : segment = read_8ubyte_unaligned_inc (dbg, readp);
5586 : :
5587 [ + + ]: 3274 : if (range_address == 0 && range_length == 0 && segment == 0)
5588 : : break;
5589 : :
5590 : 1644 : printf (" ");
5591 : 1644 : print_dwarf_addr (dwflmod, address_size, range_address,
5592 : : range_address);
5593 : 1644 : printf ("..");
5594 : 1644 : print_dwarf_addr (dwflmod, address_size,
5595 : 1644 : range_address + range_length - 1,
5596 : : range_length);
5597 [ - + ]: 1644 : if (segment_size != 0)
5598 : 0 : printf (" (%" PRIx64 ")\n", (uint64_t) segment);
5599 : : else
5600 [ + - ]: 4918 : printf ("\n");
5601 : : }
5602 : :
5603 : 1630 : next_table:
5604 [ - + ]: 1630 : if (readp != nexthdr)
5605 : : {
5606 : 0 : size_t padding = nexthdr - readp;
5607 : 0 : printf (_(" %zu padding bytes\n"), padding);
5608 : 0 : readp = nexthdr;
5609 : : }
5610 : : }
5611 : : }
5612 : :
5613 : :
5614 : : static bool is_split_dwarf (Dwarf *dbg, uint64_t *id, Dwarf_CU **split_cu);
5615 : :
5616 : : /* Returns true and sets cu and cu_base if the given Dwarf is a split
5617 : : DWARF (.dwo) file. */
5618 : : static bool
5619 : 0 : split_dwarf_cu_base (Dwarf *dbg, Dwarf_CU **cu, Dwarf_Addr *cu_base)
5620 : : {
5621 : 0 : uint64_t id;
5622 [ # # ]: 0 : if (is_split_dwarf (dbg, &id, cu))
5623 : : {
5624 : 0 : Dwarf_Die cudie;
5625 [ # # ]: 0 : if (dwarf_cu_info (*cu, NULL, NULL, &cudie, NULL, NULL, NULL, NULL) == 0)
5626 : : {
5627 : 0 : *cu_base = cudie_base (&cudie);
5628 : 0 : return true;
5629 : : }
5630 : : }
5631 : : return false;
5632 : : }
5633 : :
5634 : : /* Print content of DWARF .debug_rnglists section. */
5635 : : static void
5636 : 3 : print_debug_rnglists_section (Dwfl_Module *dwflmod,
5637 : : Ebl *ebl,
5638 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
5639 : : Elf_Scn *scn, GElf_Shdr *shdr,
5640 : : Dwarf *dbg __attribute__((unused)))
5641 : : {
5642 : 3 : printf (_("\
5643 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
5644 : : elf_ndxscn (scn), section_name (ebl, shdr),
5645 : 3 : (uint64_t) shdr->sh_offset);
5646 : :
5647 : 6 : Elf_Data *data =(dbg->sectiondata[IDX_debug_rnglists]
5648 [ - + ]: 3 : ?: elf_rawdata (scn, NULL));
5649 [ - + ]: 3 : if (unlikely (data == NULL))
5650 : : {
5651 : 0 : error (0, 0, _("cannot get .debug_rnglists content: %s"),
5652 : : elf_errmsg (-1));
5653 : 0 : return;
5654 : : }
5655 : :
5656 : : /* For the listptr to get the base address/CU. */
5657 : 3 : sort_listptr (&known_rnglistptr, "rnglistptr");
5658 : 3 : size_t listptr_idx = 0;
5659 : :
5660 : 3 : const unsigned char *readp = data->d_buf;
5661 : 3 : const unsigned char *const dataend = ((unsigned char *) data->d_buf
5662 : 3 : + data->d_size);
5663 [ + + ]: 6 : while (readp < dataend)
5664 : : {
5665 [ - + ]: 3 : if (unlikely (readp > dataend - 4))
5666 : : {
5667 : 0 : invalid_data:
5668 : 0 : error (0, 0, _("invalid data in section [%zu] '%s'"),
5669 : : elf_ndxscn (scn), section_name (ebl, shdr));
5670 : 0 : return;
5671 : : }
5672 : :
5673 : 3 : ptrdiff_t offset = readp - (unsigned char *) data->d_buf;
5674 : 3 : printf (_("Table at Offset 0x%" PRIx64 ":\n\n"),
5675 : : (uint64_t) offset);
5676 : :
5677 [ - + ]: 3 : uint64_t unit_length = read_4ubyte_unaligned_inc (dbg, readp);
5678 : 3 : unsigned int offset_size = 4;
5679 [ - + ]: 3 : if (unlikely (unit_length == 0xffffffff))
5680 : : {
5681 [ # # ]: 0 : if (unlikely (readp > dataend - 8))
5682 : 0 : goto invalid_data;
5683 : :
5684 [ # # ]: 0 : unit_length = read_8ubyte_unaligned_inc (dbg, readp);
5685 : 0 : offset_size = 8;
5686 : : }
5687 : 3 : printf (_(" Length: %8" PRIu64 "\n"), unit_length);
5688 : :
5689 : : /* We need at least 2-bytes + 1-byte + 1-byte + 4-bytes = 8
5690 : : bytes to complete the header. And this unit cannot go beyond
5691 : : the section data. */
5692 [ + - ]: 3 : if (readp > dataend - 8
5693 [ + - ]: 3 : || unit_length < 8
5694 [ - + ]: 3 : || unit_length > (uint64_t) (dataend - readp))
5695 : 0 : goto invalid_data;
5696 : :
5697 : 3 : const unsigned char *nexthdr = readp + unit_length;
5698 : :
5699 [ - + ]: 3 : uint16_t version = read_2ubyte_unaligned_inc (dbg, readp);
5700 : 3 : printf (_(" DWARF version: %8" PRIu16 "\n"), version);
5701 : :
5702 [ - + ]: 3 : if (version != 5)
5703 : : {
5704 : 0 : error (0, 0, _("Unknown version"));
5705 : 0 : goto next_table;
5706 : : }
5707 : :
5708 : 3 : uint8_t address_size = *readp++;
5709 : 3 : printf (_(" Address size: %8" PRIu64 "\n"),
5710 : : (uint64_t) address_size);
5711 : :
5712 [ - + ]: 3 : if (address_size != 4 && address_size != 8)
5713 : : {
5714 : 0 : error (0, 0, _("unsupported address size"));
5715 : 0 : goto next_table;
5716 : : }
5717 : :
5718 : 3 : uint8_t segment_size = *readp++;
5719 : 3 : printf (_(" Segment size: %8" PRIu64 "\n"),
5720 : : (uint64_t) segment_size);
5721 : :
5722 [ - + - - ]: 3 : if (segment_size != 0 && segment_size != 4 && segment_size != 8)
5723 : : {
5724 : 0 : error (0, 0, _("unsupported segment size"));
5725 : 0 : goto next_table;
5726 : : }
5727 : :
5728 [ - + ]: 3 : uint32_t offset_entry_count = read_4ubyte_unaligned_inc (dbg, readp);
5729 : 3 : printf (_(" Offset entries: %8" PRIu64 "\n"),
5730 : : (uint64_t) offset_entry_count);
5731 : :
5732 : : /* We need the CU that uses this unit to get the initial base address. */
5733 : 3 : Dwarf_Addr cu_base = 0;
5734 : 3 : struct Dwarf_CU *cu = NULL;
5735 [ - + ]: 3 : if (listptr_cu (&known_rnglistptr, &listptr_idx,
5736 : : (Dwarf_Off) offset,
5737 : 3 : (Dwarf_Off) (nexthdr - (unsigned char *) data->d_buf),
5738 : : &cu_base, &cu)
5739 [ # # ]: 0 : || split_dwarf_cu_base (dbg, &cu, &cu_base))
5740 : 3 : {
5741 : 3 : Dwarf_Die cudie;
5742 [ - + ]: 3 : if (dwarf_cu_die (cu, &cudie,
5743 : : NULL, NULL, NULL, NULL,
5744 : : NULL, NULL) == NULL)
5745 : 0 : printf (_(" Unknown CU base: "));
5746 : : else
5747 : 3 : printf (_(" CU [%6" PRIx64 "] base: "),
5748 : : dwarf_dieoffset (&cudie));
5749 : 3 : print_dwarf_addr (dwflmod, address_size, cu_base, cu_base);
5750 : 3 : printf ("\n");
5751 : : }
5752 : : else
5753 : 0 : printf (_(" Not associated with a CU.\n"));
5754 : :
5755 : 3 : printf ("\n");
5756 : :
5757 : 3 : const unsigned char *offset_array_start = readp;
5758 [ + + ]: 3 : if (offset_entry_count > 0)
5759 : : {
5760 : 1 : uint64_t max_entries = (unit_length - 8) / offset_size;
5761 [ - + ]: 1 : if (offset_entry_count > max_entries)
5762 : : {
5763 : 0 : error (0, 0,
5764 : 0 : _("too many offset entries for unit length"));
5765 : 0 : offset_entry_count = max_entries;
5766 : : }
5767 : :
5768 : 1 : printf (_(" Offsets starting at 0x%" PRIx64 ":\n"),
5769 : : (uint64_t) (offset_array_start
5770 : 1 : - (unsigned char *) data->d_buf));
5771 [ + + ]: 3 : for (uint32_t idx = 0; idx < offset_entry_count; idx++)
5772 : : {
5773 : 2 : printf (" [%6" PRIu32 "] ", idx);
5774 [ + - ]: 2 : if (offset_size == 4)
5775 : : {
5776 [ - + ]: 2 : uint32_t off = read_4ubyte_unaligned_inc (dbg, readp);
5777 : 2 : printf ("0x%" PRIx32 "\n", off);
5778 : : }
5779 : : else
5780 : : {
5781 [ # # ]: 0 : uint64_t off = read_8ubyte_unaligned_inc (dbg, readp);
5782 : 2 : printf ("0x%" PRIx64 "\n", off);
5783 : : }
5784 : : }
5785 : 1 : printf ("\n");
5786 : : }
5787 : :
5788 : 3 : Dwarf_Addr base = cu_base;
5789 : 3 : bool start_of_list = true;
5790 : 3 : while (readp < nexthdr)
5791 : : {
5792 : 16 : uint8_t kind = *readp++;
5793 : 16 : uint64_t op1, op2;
5794 : :
5795 : : /* Skip padding. */
5796 [ - + ]: 16 : if (start_of_list && kind == DW_RLE_end_of_list)
5797 : 0 : continue;
5798 : :
5799 [ + + ]: 16 : if (start_of_list)
5800 : : {
5801 : 5 : base = cu_base;
5802 : 5 : printf (" Offset: %" PRIx64 ", Index: %" PRIx64 "\n",
5803 : 5 : (uint64_t) (readp - (unsigned char *) data->d_buf - 1),
5804 : 5 : (uint64_t) (readp - offset_array_start - 1));
5805 : 5 : start_of_list = false;
5806 : : }
5807 : :
5808 : 16 : printf (" %s", dwarf_range_list_encoding_name (kind));
5809 [ + - - - : 16 : switch (kind)
+ + - +
- ]
5810 : : {
5811 : 5 : case DW_RLE_end_of_list:
5812 : 5 : start_of_list = true;
5813 : 5 : printf ("\n\n");
5814 : : break;
5815 : :
5816 : 0 : case DW_RLE_base_addressx:
5817 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
5818 : : {
5819 : 0 : invalid_range:
5820 : 0 : error (0, 0, _("invalid range list data"));
5821 : 0 : goto next_table;
5822 : : }
5823 : 0 : get_uleb128 (op1, readp, nexthdr);
5824 : 0 : printf (" %" PRIx64 "\n", op1);
5825 [ # # ]: 0 : if (! print_unresolved_addresses)
5826 : : {
5827 : 0 : Dwarf_Addr addr;
5828 [ # # ]: 0 : if (get_indexed_addr (cu, op1, &addr) != 0)
5829 : 0 : printf (" ???\n");
5830 : : else
5831 : : {
5832 : 0 : printf (" ");
5833 : 0 : print_dwarf_addr (dwflmod, address_size, addr, addr);
5834 : 0 : printf ("\n");
5835 : : }
5836 : : }
5837 : : break;
5838 : :
5839 : 0 : case DW_RLE_startx_endx:
5840 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
5841 : 0 : goto invalid_range;
5842 : 0 : get_uleb128 (op1, readp, nexthdr);
5843 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
5844 : 0 : goto invalid_range;
5845 : 0 : get_uleb128 (op2, readp, nexthdr);
5846 : 0 : printf (" %" PRIx64 ", %" PRIx64 "\n", op1, op2);
5847 [ # # ]: 0 : if (! print_unresolved_addresses)
5848 : : {
5849 : 0 : Dwarf_Addr addr1;
5850 : 0 : Dwarf_Addr addr2;
5851 [ # # ]: 0 : if (get_indexed_addr (cu, op1, &addr1) != 0
5852 [ # # ]: 0 : || get_indexed_addr (cu, op2, &addr2) != 0)
5853 : : {
5854 : 0 : printf (" ???..\n");
5855 : 0 : printf (" ???\n");
5856 : : }
5857 : : else
5858 : : {
5859 : 0 : printf (" ");
5860 : 0 : print_dwarf_addr (dwflmod, address_size, addr1, addr1);
5861 : 0 : printf ("..\n ");
5862 : 0 : print_dwarf_addr (dwflmod, address_size,
5863 : : addr2 - 1, addr2);
5864 : 0 : printf ("\n");
5865 : : }
5866 : : }
5867 : : break;
5868 : :
5869 : 0 : case DW_RLE_startx_length:
5870 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
5871 : 0 : goto invalid_range;
5872 : 0 : get_uleb128 (op1, readp, nexthdr);
5873 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
5874 : 0 : goto invalid_range;
5875 : 0 : get_uleb128 (op2, readp, nexthdr);
5876 : 0 : printf (" %" PRIx64 ", %" PRIx64 "\n", op1, op2);
5877 [ # # ]: 0 : if (! print_unresolved_addresses)
5878 : : {
5879 : 0 : Dwarf_Addr addr1;
5880 : 0 : Dwarf_Addr addr2;
5881 [ # # ]: 0 : if (get_indexed_addr (cu, op1, &addr1) != 0)
5882 : : {
5883 : 0 : printf (" ???..\n");
5884 : 0 : printf (" ???\n");
5885 : : }
5886 : : else
5887 : : {
5888 : 0 : addr2 = addr1 + op2;
5889 : 0 : printf (" ");
5890 : 0 : print_dwarf_addr (dwflmod, address_size, addr1, addr1);
5891 : 0 : printf ("..\n ");
5892 : 0 : print_dwarf_addr (dwflmod, address_size,
5893 : : addr2 - 1, addr2);
5894 : 0 : printf ("\n");
5895 : : }
5896 : : }
5897 : : break;
5898 : :
5899 : 4 : case DW_RLE_offset_pair:
5900 [ - + ]: 4 : if ((uint64_t) (nexthdr - readp) < 1)
5901 : 0 : goto invalid_range;
5902 : 4 : get_uleb128 (op1, readp, nexthdr);
5903 [ - + ]: 4 : if ((uint64_t) (nexthdr - readp) < 1)
5904 : 0 : goto invalid_range;
5905 : 4 : get_uleb128 (op2, readp, nexthdr);
5906 : 4 : printf (" %" PRIx64 ", %" PRIx64 "\n", op1, op2);
5907 [ + - ]: 4 : if (! print_unresolved_addresses)
5908 : : {
5909 : 4 : op1 += base;
5910 : 4 : op2 += base;
5911 : 4 : printf (" ");
5912 : 4 : print_dwarf_addr (dwflmod, address_size, op1, op1);
5913 : 4 : printf ("..\n ");
5914 : 4 : print_dwarf_addr (dwflmod, address_size, op2 - 1, op2);
5915 : 4 : printf ("\n");
5916 : : }
5917 : : break;
5918 : :
5919 : 2 : case DW_RLE_base_address:
5920 [ - + ]: 2 : if (address_size == 4)
5921 : : {
5922 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 4)
5923 : 0 : goto invalid_range;
5924 [ # # ]: 0 : op1 = read_4ubyte_unaligned_inc (dbg, readp);
5925 : : }
5926 : : else
5927 : : {
5928 [ - + ]: 2 : if ((uint64_t) (nexthdr - readp) < 8)
5929 : 0 : goto invalid_range;
5930 [ - + ]: 4 : op1 = read_8ubyte_unaligned_inc (dbg, readp);
5931 : : }
5932 : 2 : base = op1;
5933 : 2 : printf (" 0x%" PRIx64 "\n", base);
5934 [ + - ]: 2 : if (! print_unresolved_addresses)
5935 : : {
5936 : 2 : printf (" ");
5937 : 2 : print_dwarf_addr (dwflmod, address_size, base, base);
5938 : 2 : printf ("\n");
5939 : : }
5940 : : break;
5941 : :
5942 : 0 : case DW_RLE_start_end:
5943 [ # # ]: 0 : if (address_size == 4)
5944 : : {
5945 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 8)
5946 : 0 : goto invalid_range;
5947 [ # # ]: 0 : op1 = read_4ubyte_unaligned_inc (dbg, readp);
5948 [ # # ]: 0 : op2 = read_4ubyte_unaligned_inc (dbg, readp);
5949 : : }
5950 : : else
5951 : : {
5952 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 16)
5953 : 0 : goto invalid_range;
5954 [ # # ]: 0 : op1 = read_8ubyte_unaligned_inc (dbg, readp);
5955 [ # # ]: 0 : op2 = read_8ubyte_unaligned_inc (dbg, readp);
5956 : : }
5957 : 0 : printf (" 0x%" PRIx64 "..0x%" PRIx64 "\n", op1, op2);
5958 [ # # ]: 0 : if (! print_unresolved_addresses)
5959 : : {
5960 : 0 : printf (" ");
5961 : 0 : print_dwarf_addr (dwflmod, address_size, op1, op1);
5962 : 0 : printf ("..\n ");
5963 : 0 : print_dwarf_addr (dwflmod, address_size, op2 - 1, op2);
5964 : 0 : printf ("\n");
5965 : : }
5966 : : break;
5967 : :
5968 : 5 : case DW_RLE_start_length:
5969 [ - + ]: 5 : if (address_size == 4)
5970 : : {
5971 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 4)
5972 : 0 : goto invalid_range;
5973 [ # # ]: 0 : op1 = read_4ubyte_unaligned_inc (dbg, readp);
5974 : : }
5975 : : else
5976 : : {
5977 [ - + ]: 5 : if ((uint64_t) (nexthdr - readp) < 8)
5978 : 0 : goto invalid_range;
5979 [ - + ]: 10 : op1 = read_8ubyte_unaligned_inc (dbg, readp);
5980 : : }
5981 [ - + ]: 5 : if ((uint64_t) (nexthdr - readp) < 1)
5982 : 0 : goto invalid_range;
5983 : 5 : get_uleb128 (op2, readp, nexthdr);
5984 : 5 : printf (" 0x%" PRIx64 ", %" PRIx64 "\n", op1, op2);
5985 [ + + ]: 5 : if (! print_unresolved_addresses)
5986 : : {
5987 : 4 : op2 = op1 + op2;
5988 : 4 : printf (" ");
5989 : 4 : print_dwarf_addr (dwflmod, address_size, op1, op1);
5990 : 4 : printf ("..\n ");
5991 : 4 : print_dwarf_addr (dwflmod, address_size, op2 - 1, op2);
5992 [ + + ]: 23 : printf ("\n");
5993 : : }
5994 : : break;
5995 : :
5996 : 0 : default:
5997 : 0 : goto invalid_range;
5998 : : }
5999 : : }
6000 : :
6001 : 3 : next_table:
6002 [ - + ]: 3 : if (readp != nexthdr)
6003 : : {
6004 : 0 : size_t padding = nexthdr - readp;
6005 : 0 : printf (_(" %zu padding bytes\n\n"), padding);
6006 : 0 : readp = nexthdr;
6007 : : }
6008 : : }
6009 : : }
6010 : :
6011 : : /* Print content of DWARF .debug_ranges section. */
6012 : : static void
6013 : 30 : print_debug_ranges_section (Dwfl_Module *dwflmod,
6014 : : Ebl *ebl, GElf_Ehdr *ehdr,
6015 : : Elf_Scn *scn, GElf_Shdr *shdr,
6016 : : Dwarf *dbg)
6017 : : {
6018 : 60 : Elf_Data *data = (dbg->sectiondata[IDX_debug_ranges]
6019 [ - + ]: 30 : ?: elf_rawdata (scn, NULL));
6020 [ - + ]: 30 : if (unlikely (data == NULL))
6021 : : {
6022 : 0 : error (0, 0, _("cannot get .debug_ranges content: %s"),
6023 : : elf_errmsg (-1));
6024 : 0 : return;
6025 : : }
6026 : :
6027 : 30 : printf (_("\
6028 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
6029 : : elf_ndxscn (scn), section_name (ebl, shdr),
6030 : 30 : (uint64_t) shdr->sh_offset);
6031 : :
6032 : 30 : sort_listptr (&known_rangelistptr, "rangelistptr");
6033 : 30 : size_t listptr_idx = 0;
6034 : :
6035 [ + + ]: 30 : uint_fast8_t address_size = ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 4 : 8;
6036 : :
6037 : 30 : bool first = true;
6038 : 30 : Dwarf_Addr base = 0;
6039 : 30 : unsigned char *const endp = (unsigned char *) data->d_buf + data->d_size;
6040 : 30 : unsigned char *readp = data->d_buf;
6041 : 30 : Dwarf_CU *last_cu = NULL;
6042 [ + + ]: 63734 : while (readp < endp)
6043 : : {
6044 : 63704 : ptrdiff_t offset = readp - (unsigned char *) data->d_buf;
6045 : 63704 : Dwarf_CU *cu = last_cu;
6046 : :
6047 [ + + + + ]: 63704 : if (first && skip_listptr_hole (&known_rangelistptr, &listptr_idx,
6048 : : &address_size, NULL, &base, &cu,
6049 : : offset, &readp, endp, NULL))
6050 : 5 : continue;
6051 : :
6052 [ + + ]: 63699 : if (last_cu != cu)
6053 : : {
6054 : 942 : Dwarf_Die cudie;
6055 [ - + ]: 942 : if (dwarf_cu_die (cu, &cudie,
6056 : : NULL, NULL, NULL, NULL,
6057 : : NULL, NULL) == NULL)
6058 : 0 : printf (_("\n Unknown CU base: "));
6059 : : else
6060 : 942 : printf (_("\n CU [%6" PRIx64 "] base: "),
6061 : : dwarf_dieoffset (&cudie));
6062 : 942 : print_dwarf_addr (dwflmod, address_size, base, base);
6063 : 942 : printf ("\n");
6064 : : }
6065 : 63699 : last_cu = cu;
6066 : :
6067 [ - + ]: 63699 : if (unlikely (data->d_size - offset < (size_t) address_size * 2))
6068 : : {
6069 : 0 : printf (_(" [%6tx] <INVALID DATA>\n"), offset);
6070 : 0 : break;
6071 : : }
6072 : :
6073 : 63699 : Dwarf_Addr begin;
6074 : 63699 : Dwarf_Addr end;
6075 [ + + ]: 63699 : if (address_size == 8)
6076 : : {
6077 [ - + ]: 63691 : begin = read_8ubyte_unaligned_inc (dbg, readp);
6078 [ - + ]: 127382 : end = read_8ubyte_unaligned_inc (dbg, readp);
6079 : : }
6080 : : else
6081 : : {
6082 [ - + ]: 8 : begin = read_4ubyte_unaligned_inc (dbg, readp);
6083 [ - + ]: 8 : end = read_4ubyte_unaligned_inc (dbg, readp);
6084 [ + - ]: 8 : if (begin == (Dwarf_Addr) (uint32_t) -1)
6085 : : begin = (Dwarf_Addr) -1l;
6086 : : }
6087 : :
6088 [ - + ]: 63699 : if (begin == (Dwarf_Addr) -1l) /* Base address entry. */
6089 : : {
6090 [ # # ]: 0 : if (first)
6091 : 0 : printf (" [%6tx] ", offset);
6092 : : else
6093 : 0 : printf (" ");
6094 : 0 : puts (_("base address"));
6095 : 0 : printf (" ");
6096 : 0 : print_dwarf_addr (dwflmod, address_size, end, end);
6097 : 0 : printf ("\n");
6098 : 0 : base = end;
6099 : 0 : first = false;
6100 : : }
6101 [ + + ]: 63699 : else if (begin == 0 && end == 0) /* End of list entry. */
6102 : : {
6103 [ + + ]: 13712 : if (first)
6104 : 5 : printf (_(" [%6tx] empty list\n"), offset);
6105 : : first = true;
6106 : : }
6107 : : else
6108 : : {
6109 : : /* We have an address range entry. */
6110 [ + + ]: 49987 : if (first) /* First address range entry in a list. */
6111 : 13707 : printf (" [%6tx] ", offset);
6112 : : else
6113 : 36280 : printf (" ");
6114 : :
6115 : 49987 : printf ("range %" PRIx64 ", %" PRIx64 "\n", begin, end);
6116 [ + + ]: 49987 : if (! print_unresolved_addresses)
6117 : : {
6118 : 49977 : printf (" ");
6119 : 49977 : print_dwarf_addr (dwflmod, address_size, base + begin,
6120 : : base + begin);
6121 : 49977 : printf ("..\n ");
6122 : 49977 : print_dwarf_addr (dwflmod, address_size,
6123 : 49977 : base + end - 1, base + end);
6124 : 63699 : printf ("\n");
6125 : : }
6126 : :
6127 : : first = false;
6128 : : }
6129 : : }
6130 : : }
6131 : :
6132 : : #define REGNAMESZ 16
6133 : : static const char *
6134 : 173107 : register_info (Ebl *ebl, unsigned int regno, const Ebl_Register_Location *loc,
6135 : : char name[REGNAMESZ], int *bits, int *type)
6136 : : {
6137 : 173107 : const char *set;
6138 : 173107 : const char *pfx;
6139 : 173107 : int ignore;
6140 [ + + + + ]: 517625 : ssize_t n = ebl_register_info (ebl, regno, name, REGNAMESZ, &pfx, &set,
6141 : : bits ?: &ignore, type ?: &ignore);
6142 [ - + ]: 173107 : if (n <= 0)
6143 : : {
6144 [ # # ]: 0 : if (loc != NULL)
6145 : 0 : snprintf (name, REGNAMESZ, "reg%u", loc->regno);
6146 : : else
6147 : 0 : snprintf (name, REGNAMESZ, "??? 0x%x", regno);
6148 [ # # ]: 0 : if (bits != NULL)
6149 [ # # ]: 0 : *bits = loc != NULL ? loc->bits : 0;
6150 [ # # ]: 0 : if (type != NULL)
6151 : 0 : *type = DW_ATE_unsigned;
6152 : 0 : set = "??? unrecognized";
6153 : : }
6154 : : else
6155 : : {
6156 [ + + - + ]: 173107 : if (bits != NULL && *bits <= 0)
6157 [ # # ]: 0 : *bits = loc != NULL ? loc->bits : 0;
6158 [ + + - + ]: 173107 : if (type != NULL && *type == DW_ATE_void)
6159 : 0 : *type = DW_ATE_unsigned;
6160 : :
6161 : : }
6162 : 173107 : return set;
6163 : : }
6164 : :
6165 : : static const unsigned char *
6166 : 140 : read_encoded (unsigned int encoding, const unsigned char *readp,
6167 : : const unsigned char *const endp, uint64_t *res, Dwarf *dbg)
6168 : : {
6169 [ - + ]: 140 : if ((encoding & 0xf) == DW_EH_PE_absptr)
6170 : 0 : encoding = gelf_getclass (dbg->elf) == ELFCLASS32
6171 [ # # ]: 0 : ? DW_EH_PE_udata4 : DW_EH_PE_udata8;
6172 : :
6173 [ - - - + : 140 : switch (encoding & 0xf)
- - + -
- ]
6174 : : {
6175 : 0 : case DW_EH_PE_uleb128:
6176 : 0 : get_uleb128 (*res, readp, endp);
6177 : 0 : break;
6178 : 0 : case DW_EH_PE_sleb128:
6179 : 0 : get_sleb128 (*res, readp, endp);
6180 : 0 : break;
6181 : 0 : case DW_EH_PE_udata2:
6182 [ # # ]: 0 : if (readp + 2 > endp)
6183 : 0 : goto invalid;
6184 [ # # ]: 0 : *res = read_2ubyte_unaligned_inc (dbg, readp);
6185 : 0 : break;
6186 : 70 : case DW_EH_PE_udata4:
6187 [ - + ]: 70 : if (readp + 4 > endp)
6188 : 0 : goto invalid;
6189 [ - + ]: 70 : *res = read_4ubyte_unaligned_inc (dbg, readp);
6190 : 70 : break;
6191 : 0 : case DW_EH_PE_udata8:
6192 [ # # ]: 0 : if (readp + 8 > endp)
6193 : 0 : goto invalid;
6194 [ # # ]: 0 : *res = read_8ubyte_unaligned_inc (dbg, readp);
6195 : 0 : break;
6196 : 0 : case DW_EH_PE_sdata2:
6197 [ # # ]: 0 : if (readp + 2 > endp)
6198 : 0 : goto invalid;
6199 [ # # ]: 0 : *res = read_2sbyte_unaligned_inc (dbg, readp);
6200 : 0 : break;
6201 : 70 : case DW_EH_PE_sdata4:
6202 [ - + ]: 70 : if (readp + 4 > endp)
6203 : 0 : goto invalid;
6204 [ - + ]: 70 : *res = read_4sbyte_unaligned_inc (dbg, readp);
6205 : 70 : break;
6206 : 0 : case DW_EH_PE_sdata8:
6207 [ # # ]: 0 : if (readp + 8 > endp)
6208 : 0 : goto invalid;
6209 [ # # ]: 0 : *res = read_8sbyte_unaligned_inc (dbg, readp);
6210 : 0 : break;
6211 : : default:
6212 : 0 : invalid:
6213 : 0 : error (1, 0,
6214 : 0 : _("invalid encoding"));
6215 : : }
6216 : :
6217 : 140 : return readp;
6218 : : }
6219 : :
6220 : : static const char *
6221 : 172259 : regname (Ebl *ebl, unsigned int regno, char *regnamebuf)
6222 : : {
6223 : 172259 : register_info (ebl, regno, NULL, regnamebuf, NULL, NULL);
6224 : :
6225 : 172259 : return regnamebuf;
6226 : : }
6227 : :
6228 : : static void
6229 : 12739 : print_cfa_program (const unsigned char *readp, const unsigned char *const endp,
6230 : : Dwarf_Word vma_base, unsigned int code_align,
6231 : : int data_align,
6232 : : unsigned int version, unsigned int ptr_size,
6233 : : unsigned int encoding,
6234 : : Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr, Dwarf *dbg)
6235 : : {
6236 : 12739 : char regnamebuf[REGNAMESZ];
6237 : :
6238 : 12739 : puts ("\n Program:");
6239 : 12739 : Dwarf_Word pc = vma_base;
6240 : 12739 : while (readp < endp)
6241 : : {
6242 : 319111 : unsigned int opcode = *readp++;
6243 : :
6244 [ + + ]: 319111 : if (opcode < DW_CFA_advance_loc)
6245 : : /* Extended opcode. */
6246 [ + - + + : 81362 : switch (opcode)
+ + - + -
- + + + +
+ + - + -
- - - - -
+ - - ]
6247 : : {
6248 : 37172 : uint64_t op1;
6249 : 37172 : int64_t sop1;
6250 : 37172 : uint64_t op2;
6251 : 37172 : int64_t sop2;
6252 : :
6253 : 37172 : case DW_CFA_nop:
6254 : 37172 : puts (" nop");
6255 : 37172 : break;
6256 : 0 : case DW_CFA_set_loc:
6257 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
6258 : 0 : goto invalid;
6259 : 0 : readp = read_encoded (encoding, readp, endp, &op1, dbg);
6260 : 0 : printf (" set_loc %#" PRIx64 " to %#" PRIx64 "\n",
6261 : : op1, pc = vma_base + op1);
6262 : : break;
6263 : 1599 : case DW_CFA_advance_loc1:
6264 [ - + ]: 1599 : if ((uint64_t) (endp - readp) < 1)
6265 : 0 : goto invalid;
6266 : 3198 : printf (" advance_loc1 %u to %#" PRIx64 "\n",
6267 : 1599 : *readp, pc += *readp * code_align);
6268 : 1599 : ++readp;
6269 : 1599 : break;
6270 : 183 : case DW_CFA_advance_loc2:
6271 [ - + ]: 183 : if ((uint64_t) (endp - readp) < 2)
6272 : 0 : goto invalid;
6273 [ - + ]: 183 : op1 = read_2ubyte_unaligned_inc (dbg, readp);
6274 : 81545 : printf (" advance_loc2 %" PRIu64 " to %#" PRIx64 "\n",
6275 : 183 : op1, pc += op1 * code_align);
6276 : : break;
6277 : 24 : case DW_CFA_advance_loc4:
6278 [ - + ]: 24 : if ((uint64_t) (endp - readp) < 4)
6279 : 0 : goto invalid;
6280 [ + - ]: 24 : op1 = read_4ubyte_unaligned_inc (dbg, readp);
6281 : 81386 : printf (" advance_loc4 %" PRIu64 " to %#" PRIx64 "\n",
6282 : 24 : op1, pc += op1 * code_align);
6283 : : break;
6284 : 4 : case DW_CFA_offset_extended:
6285 [ - + ]: 4 : if ((uint64_t) (endp - readp) < 1)
6286 : 0 : goto invalid;
6287 : 4 : get_uleb128 (op1, readp, endp);
6288 [ - + ]: 4 : if ((uint64_t) (endp - readp) < 1)
6289 : 0 : goto invalid;
6290 : 4 : get_uleb128 (op2, readp, endp);
6291 : 4 : printf (" offset_extended r%" PRIu64 " (%s) at cfa%+" PRId64
6292 : : "\n",
6293 : : op1, regname (ebl, op1, regnamebuf), op2 * data_align);
6294 : : break;
6295 : 0 : case DW_CFA_restore_extended:
6296 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
6297 : 0 : goto invalid;
6298 : 0 : get_uleb128 (op1, readp, endp);
6299 : 0 : printf (" restore_extended r%" PRIu64 " (%s)\n",
6300 : : op1, regname (ebl, op1, regnamebuf));
6301 : : break;
6302 : 34 : case DW_CFA_undefined:
6303 [ - + ]: 34 : if ((uint64_t) (endp - readp) < 1)
6304 : 0 : goto invalid;
6305 : 34 : get_uleb128 (op1, readp, endp);
6306 : 34 : printf (" undefined r%" PRIu64 " (%s)\n", op1,
6307 : : regname (ebl, op1, regnamebuf));
6308 : : break;
6309 : 0 : case DW_CFA_same_value:
6310 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
6311 : 0 : goto invalid;
6312 : 0 : get_uleb128 (op1, readp, endp);
6313 : 0 : printf (" same_value r%" PRIu64 " (%s)\n", op1,
6314 : : regname (ebl, op1, regnamebuf));
6315 : : break;
6316 : 0 : case DW_CFA_register:
6317 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
6318 : 0 : goto invalid;
6319 : 0 : get_uleb128 (op1, readp, endp);
6320 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
6321 : 0 : goto invalid;
6322 : 0 : get_uleb128 (op2, readp, endp);
6323 : 0 : printf (" register r%" PRIu64 " (%s) in r%" PRIu64 " (%s)\n",
6324 : : op1, regname (ebl, op1, regnamebuf), op2,
6325 : : regname (ebl, op2, regnamebuf));
6326 : : break;
6327 : 11090 : case DW_CFA_remember_state:
6328 : 11090 : puts (" remember_state");
6329 : 11090 : break;
6330 : 11090 : case DW_CFA_restore_state:
6331 : 11090 : puts (" restore_state");
6332 : 11090 : break;
6333 : 179 : case DW_CFA_def_cfa:
6334 [ - + ]: 179 : if ((uint64_t) (endp - readp) < 1)
6335 : 0 : goto invalid;
6336 : 179 : get_uleb128 (op1, readp, endp);
6337 [ - + ]: 179 : if ((uint64_t) (endp - readp) < 1)
6338 : 0 : goto invalid;
6339 : 179 : get_uleb128 (op2, readp, endp);
6340 : 179 : printf (" def_cfa r%" PRIu64 " (%s) at offset %" PRIu64 "\n",
6341 : : op1, regname (ebl, op1, regnamebuf), op2);
6342 : : break;
6343 : 65 : case DW_CFA_def_cfa_register:
6344 [ - + ]: 65 : if ((uint64_t) (endp - readp) < 1)
6345 : 0 : goto invalid;
6346 : 65 : get_uleb128 (op1, readp, endp);
6347 : 65 : printf (" def_cfa_register r%" PRIu64 " (%s)\n",
6348 : : op1, regname (ebl, op1, regnamebuf));
6349 : : break;
6350 : 19866 : case DW_CFA_def_cfa_offset:
6351 [ - + ]: 19866 : if ((uint64_t) (endp - readp) < 1)
6352 : 0 : goto invalid;
6353 : 19866 : get_uleb128 (op1, readp, endp);
6354 : 19866 : printf (" def_cfa_offset %" PRIu64 "\n", op1);
6355 : : break;
6356 : 36 : case DW_CFA_def_cfa_expression:
6357 [ - + ]: 36 : if ((uint64_t) (endp - readp) < 1)
6358 : 0 : goto invalid;
6359 : 36 : get_uleb128 (op1, readp, endp); /* Length of DW_FORM_block. */
6360 : 36 : printf (" def_cfa_expression %" PRIu64 "\n", op1);
6361 [ - + ]: 36 : if ((uint64_t) (endp - readp) < op1)
6362 : : {
6363 : 0 : invalid:
6364 : 0 : fputs (_(" <INVALID DATA>\n"), stdout);
6365 : 0 : return;
6366 : : }
6367 : 36 : print_ops (dwflmod, dbg, 10, 10, version, ptr_size, 0, NULL,
6368 : : op1, readp);
6369 : 36 : readp += op1;
6370 : 36 : break;
6371 : 0 : case DW_CFA_expression:
6372 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
6373 : 0 : goto invalid;
6374 : 0 : get_uleb128 (op1, readp, endp);
6375 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
6376 : 0 : goto invalid;
6377 : 0 : get_uleb128 (op2, readp, endp); /* Length of DW_FORM_block. */
6378 : 0 : printf (" expression r%" PRIu64 " (%s) \n",
6379 : : op1, regname (ebl, op1, regnamebuf));
6380 [ # # ]: 0 : if ((uint64_t) (endp - readp) < op2)
6381 : 0 : goto invalid;
6382 : 0 : print_ops (dwflmod, dbg, 10, 10, version, ptr_size, 0, NULL,
6383 : : op2, readp);
6384 : 0 : readp += op2;
6385 : 0 : break;
6386 : 16 : case DW_CFA_offset_extended_sf:
6387 [ - + ]: 16 : if ((uint64_t) (endp - readp) < 1)
6388 : 0 : goto invalid;
6389 : 16 : get_uleb128 (op1, readp, endp);
6390 [ - + ]: 16 : if ((uint64_t) (endp - readp) < 1)
6391 : 0 : goto invalid;
6392 : 16 : get_sleb128 (sop2, readp, endp);
6393 : 16 : printf (" offset_extended_sf r%" PRIu64 " (%s) at cfa%+"
6394 : : PRId64 "\n",
6395 : : op1, regname (ebl, op1, regnamebuf), sop2 * data_align);
6396 : : break;
6397 : 0 : case DW_CFA_def_cfa_sf:
6398 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
6399 : 0 : goto invalid;
6400 : 0 : get_uleb128 (op1, readp, endp);
6401 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
6402 : 0 : goto invalid;
6403 : 0 : get_sleb128 (sop2, readp, endp);
6404 : 0 : printf (" def_cfa_sf r%" PRIu64 " (%s) at offset %" PRId64 "\n",
6405 : : op1, regname (ebl, op1, regnamebuf), sop2 * data_align);
6406 : : break;
6407 : 0 : case DW_CFA_def_cfa_offset_sf:
6408 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
6409 : 0 : goto invalid;
6410 : 0 : get_sleb128 (sop1, readp, endp);
6411 : 0 : printf (" def_cfa_offset_sf %" PRId64 "\n", sop1 * data_align);
6412 : : break;
6413 : 0 : case DW_CFA_val_offset:
6414 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
6415 : 0 : goto invalid;
6416 : 0 : get_uleb128 (op1, readp, endp);
6417 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
6418 : 0 : goto invalid;
6419 : 0 : get_uleb128 (op2, readp, endp);
6420 : 0 : printf (" val_offset %" PRIu64 " at offset %" PRIu64 "\n",
6421 : : op1, op2 * data_align);
6422 : : break;
6423 : 0 : case DW_CFA_val_offset_sf:
6424 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
6425 : 0 : goto invalid;
6426 : 0 : get_uleb128 (op1, readp, endp);
6427 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
6428 : 0 : goto invalid;
6429 : 0 : get_sleb128 (sop2, readp, endp);
6430 : 0 : printf (" val_offset_sf %" PRIu64 " at offset %" PRId64 "\n",
6431 : : op1, sop2 * data_align);
6432 : : break;
6433 : 0 : case DW_CFA_val_expression:
6434 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
6435 : 0 : goto invalid;
6436 : 0 : get_uleb128 (op1, readp, endp);
6437 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
6438 : 0 : goto invalid;
6439 : 0 : get_uleb128 (op2, readp, endp); /* Length of DW_FORM_block. */
6440 : 0 : printf (" val_expression r%" PRIu64 " (%s)\n",
6441 : : op1, regname (ebl, op1, regnamebuf));
6442 [ # # ]: 0 : if ((uint64_t) (endp - readp) < op2)
6443 : 0 : goto invalid;
6444 : 0 : print_ops (dwflmod, dbg, 10, 10, version, ptr_size, 0,
6445 : : NULL, op2, readp);
6446 : 0 : readp += op2;
6447 : 0 : break;
6448 : 0 : case DW_CFA_MIPS_advance_loc8:
6449 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 8)
6450 : 0 : goto invalid;
6451 [ # # ]: 0 : op1 = read_8ubyte_unaligned_inc (dbg, readp);
6452 : 81362 : printf (" MIPS_advance_loc8 %" PRIu64 " to %#" PRIx64 "\n",
6453 : 0 : op1, pc += op1 * code_align);
6454 : : break;
6455 : 4 : case DW_CFA_GNU_window_save: /* DW_CFA_AARCH64_negate_ra_state */
6456 [ + - ]: 4 : if (ehdr->e_machine == EM_AARCH64)
6457 : 4 : puts (" AARCH64_negate_ra_state");
6458 : : else
6459 : 0 : puts (" GNU_window_save");
6460 : : break;
6461 : 0 : case DW_CFA_GNU_args_size:
6462 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
6463 : 0 : goto invalid;
6464 : 0 : get_uleb128 (op1, readp, endp);
6465 : 0 : printf (" args_size %" PRIu64 "\n", op1);
6466 : : break;
6467 : : default:
6468 : 81362 : printf (" ??? (%u)\n", opcode);
6469 : : break;
6470 : : }
6471 [ + + ]: 237749 : else if (opcode < DW_CFA_offset)
6472 : 397638 : printf (" advance_loc %u to %#" PRIx64 "\n",
6473 : 65788 : opcode & 0x3f, pc += (opcode & 0x3f) * code_align);
6474 [ + + ]: 171961 : else if (opcode < DW_CFA_restore)
6475 : : {
6476 : 67843 : uint64_t offset;
6477 [ - + ]: 67843 : if ((uint64_t) (endp - readp) < 1)
6478 : 0 : goto invalid;
6479 : 67843 : get_uleb128 (offset, readp, endp);
6480 : 67843 : printf (" offset r%u (%s) at cfa%+" PRId64 "\n",
6481 : : opcode & 0x3f, regname (ebl, opcode & 0x3f, regnamebuf),
6482 : : offset * data_align);
6483 : : }
6484 : : else
6485 [ + + ]: 435968 : printf (" restore r%u (%s)\n",
6486 : : opcode & 0x3f, regname (ebl, opcode & 0x3f, regnamebuf));
6487 : : }
6488 : : }
6489 : :
6490 : :
6491 : : static unsigned int
6492 : 12595 : encoded_ptr_size (int encoding, unsigned int ptr_size)
6493 : : {
6494 [ - + - + ]: 12595 : switch (encoding & 7)
6495 : : {
6496 : : case DW_EH_PE_udata4:
6497 : : return 4;
6498 : 0 : case DW_EH_PE_udata8:
6499 : 0 : return 8;
6500 : 48 : case 0:
6501 : 48 : return ptr_size;
6502 : : }
6503 : :
6504 : 0 : fprintf (stderr, "Unsupported pointer encoding: %#x, "
6505 : : "assuming pointer size of %d.\n", encoding, ptr_size);
6506 : 0 : return ptr_size;
6507 : : }
6508 : :
6509 : :
6510 : : static unsigned int
6511 : 314 : print_encoding (unsigned int val)
6512 : : {
6513 [ - - - + : 314 : switch (val & 0xf)
- - - + -
- ]
6514 : : {
6515 : 0 : case DW_EH_PE_absptr:
6516 : 0 : fputs ("absptr", stdout);
6517 : 0 : break;
6518 : 0 : case DW_EH_PE_uleb128:
6519 : 0 : fputs ("uleb128", stdout);
6520 : 0 : break;
6521 : 0 : case DW_EH_PE_udata2:
6522 : 0 : fputs ("udata2", stdout);
6523 : 0 : break;
6524 : 70 : case DW_EH_PE_udata4:
6525 : 70 : fputs ("udata4", stdout);
6526 : 70 : break;
6527 : 0 : case DW_EH_PE_udata8:
6528 : 0 : fputs ("udata8", stdout);
6529 : 0 : break;
6530 : 0 : case DW_EH_PE_sleb128:
6531 : 0 : fputs ("sleb128", stdout);
6532 : 0 : break;
6533 : 0 : case DW_EH_PE_sdata2:
6534 : 0 : fputs ("sdata2", stdout);
6535 : 0 : break;
6536 : 244 : case DW_EH_PE_sdata4:
6537 : 244 : fputs ("sdata4", stdout);
6538 : 244 : break;
6539 : 0 : case DW_EH_PE_sdata8:
6540 : 0 : fputs ("sdata8", stdout);
6541 : 0 : break;
6542 : : default:
6543 : : /* We did not use any of the bits after all. */
6544 : : return val;
6545 : : }
6546 : :
6547 : 314 : return val & ~0xf;
6548 : : }
6549 : :
6550 : :
6551 : : static unsigned int
6552 : 244 : print_relinfo (unsigned int val)
6553 : : {
6554 [ + - + - : 244 : switch (val & 0x70)
- - ]
6555 : : {
6556 : 174 : case DW_EH_PE_pcrel:
6557 : 174 : fputs ("pcrel", stdout);
6558 : 174 : break;
6559 : 0 : case DW_EH_PE_textrel:
6560 : 0 : fputs ("textrel", stdout);
6561 : 0 : break;
6562 : 70 : case DW_EH_PE_datarel:
6563 : 70 : fputs ("datarel", stdout);
6564 : 70 : break;
6565 : 0 : case DW_EH_PE_funcrel:
6566 : 0 : fputs ("funcrel", stdout);
6567 : 0 : break;
6568 : 0 : case DW_EH_PE_aligned:
6569 : 0 : fputs ("aligned", stdout);
6570 : 0 : break;
6571 : : default:
6572 : : return val;
6573 : : }
6574 : :
6575 : 244 : return val & ~0x70;
6576 : : }
6577 : :
6578 : :
6579 : : static void
6580 : 314 : print_encoding_base (const char *pfx, unsigned int fde_encoding)
6581 : : {
6582 : 314 : printf ("(%s", pfx);
6583 : :
6584 [ - + ]: 314 : if (fde_encoding == DW_EH_PE_omit)
6585 : 0 : puts ("omit)");
6586 : : else
6587 : : {
6588 : 314 : unsigned int w = fde_encoding;
6589 : :
6590 : 314 : w = print_encoding (w);
6591 : :
6592 [ + + ]: 314 : if (w & 0x70)
6593 : : {
6594 [ + - ]: 244 : if (w != fde_encoding)
6595 [ - + ]: 244 : fputc_unlocked (' ', stdout);
6596 : :
6597 : 244 : w = print_relinfo (w);
6598 : : }
6599 : :
6600 [ - + ]: 314 : if (w != 0)
6601 [ # # ]: 0 : printf ("%s%x", w != fde_encoding ? " " : "", w);
6602 : :
6603 : 314 : puts (")");
6604 : : }
6605 : 314 : }
6606 : :
6607 : :
6608 : : static void
6609 : 106 : print_debug_frame_section (Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr,
6610 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
6611 : : {
6612 : 106 : size_t shstrndx;
6613 : : /* We know this call will succeed since it did in the caller. */
6614 : 106 : (void) elf_getshdrstrndx (ebl->elf, &shstrndx);
6615 : 106 : const char *scnname = elf_strptr (ebl->elf, shstrndx, shdr->sh_name);
6616 : :
6617 : : /* Needed if we find PC-relative addresses. */
6618 : 106 : GElf_Addr bias;
6619 [ - + ]: 106 : if (dwfl_module_getelf (dwflmod, &bias) == NULL)
6620 : : {
6621 : 0 : error (0, 0, _("cannot get ELF: %s"), dwfl_errmsg (-1));
6622 : 0 : return;
6623 : : }
6624 : :
6625 : 106 : bool is_eh_frame = strcmp (scnname, ".eh_frame") == 0;
6626 : 212 : Elf_Data *data = (is_eh_frame
6627 : 82 : ? elf_rawdata (scn, NULL)
6628 [ + + ]: 106 : : (dbg->sectiondata[IDX_debug_frame]
6629 [ - + ]: 24 : ?: elf_rawdata (scn, NULL)));
6630 : :
6631 [ - + ]: 106 : if (unlikely (data == NULL))
6632 : : {
6633 : 0 : error (0, 0, _("cannot get %s content: %s"),
6634 : : scnname, elf_errmsg (-1));
6635 : 0 : return;
6636 : : }
6637 : :
6638 [ + + ]: 106 : if (is_eh_frame)
6639 : 82 : printf (_("\
6640 : : \nCall frame information section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
6641 : 82 : elf_ndxscn (scn), scnname, (uint64_t) shdr->sh_offset);
6642 : : else
6643 : 24 : printf (_("\
6644 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
6645 : 24 : elf_ndxscn (scn), scnname, (uint64_t) shdr->sh_offset);
6646 : :
6647 : 106 : struct cieinfo
6648 : : {
6649 : : ptrdiff_t cie_offset;
6650 : : const char *augmentation;
6651 : : unsigned int code_alignment_factor;
6652 : : unsigned int data_alignment_factor;
6653 : : uint8_t address_size;
6654 : : uint8_t fde_encoding;
6655 : : uint8_t lsda_encoding;
6656 : : struct cieinfo *next;
6657 : 106 : } *cies = NULL;
6658 : :
6659 : 106 : const unsigned char *readp = data->d_buf;
6660 : 106 : const unsigned char *const dataend = ((unsigned char *) data->d_buf
6661 : 106 : + data->d_size);
6662 [ + + ]: 12915 : while (readp < dataend)
6663 : : {
6664 [ - + ]: 12809 : if (unlikely (readp + 4 > dataend))
6665 : : {
6666 : 0 : invalid_data:
6667 : 0 : error (0, 0, _("invalid data in section [%zu] '%s'"),
6668 : : elf_ndxscn (scn), scnname);
6669 : 0 : return;
6670 : : }
6671 : :
6672 : : /* At the beginning there must be a CIE. There can be multiple,
6673 : : hence we test tis in a loop. */
6674 : 12809 : ptrdiff_t offset = readp - (unsigned char *) data->d_buf;
6675 : :
6676 [ + + ]: 12809 : Dwarf_Word unit_length = read_4ubyte_unaligned_inc (dbg, readp);
6677 : 12809 : unsigned int length = 4;
6678 [ - + ]: 12809 : if (unlikely (unit_length == 0xffffffff))
6679 : : {
6680 [ # # ]: 0 : if (unlikely (readp + 8 > dataend))
6681 : 0 : goto invalid_data;
6682 : :
6683 [ # # ]: 0 : unit_length = read_8ubyte_unaligned_inc (dbg, readp);
6684 : 0 : length = 8;
6685 : : }
6686 : :
6687 [ + + ]: 12809 : if (unlikely (unit_length == 0))
6688 : : {
6689 : 70 : printf (_("\n [%6tx] Zero terminator\n"), offset);
6690 : 70 : continue;
6691 : : }
6692 : :
6693 : 12739 : Dwarf_Word maxsize = dataend - readp;
6694 [ - + ]: 12739 : if (unlikely (unit_length > maxsize))
6695 : 0 : goto invalid_data;
6696 : :
6697 [ + + ]: 12739 : unsigned int ptr_size = ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 4 : 8;
6698 : :
6699 : 12739 : ptrdiff_t start = readp - (unsigned char *) data->d_buf;
6700 : 12739 : const unsigned char *const cieend = readp + unit_length;
6701 [ - + ]: 12739 : if (unlikely (cieend > dataend))
6702 : 0 : goto invalid_data;
6703 : :
6704 : 12739 : Dwarf_Off cie_id;
6705 [ + - ]: 12739 : if (length == 4)
6706 : : {
6707 [ - + ]: 12739 : if (unlikely (cieend - readp < 4))
6708 : 0 : goto invalid_data;
6709 [ + + ]: 12739 : cie_id = read_4ubyte_unaligned_inc (dbg, readp);
6710 [ + + ]: 12739 : if (!is_eh_frame && cie_id == DW_CIE_ID_32)
6711 : 38 : cie_id = DW_CIE_ID_64;
6712 : : }
6713 : : else
6714 : : {
6715 [ # # ]: 0 : if (unlikely (cieend - readp < 8))
6716 : 0 : goto invalid_data;
6717 [ # # ]: 0 : cie_id = read_8ubyte_unaligned_inc (dbg, readp);
6718 : : }
6719 : :
6720 : 12739 : uint_fast8_t version = 2;
6721 : 12739 : unsigned int code_alignment_factor;
6722 : 12739 : int data_alignment_factor;
6723 : 12739 : unsigned int fde_encoding = 0;
6724 : 12739 : unsigned int lsda_encoding = 0;
6725 : 12739 : Dwarf_Word initial_location = 0;
6726 : 12739 : Dwarf_Word vma_base = 0;
6727 : :
6728 [ + + + + ]: 12819 : if (cie_id == (is_eh_frame ? 0 : DW_CIE_ID_64))
6729 : : {
6730 [ - + ]: 144 : if (unlikely (cieend - readp < 2))
6731 : 0 : goto invalid_data;
6732 : 144 : version = *readp++;
6733 : 144 : const char *const augmentation = (const char *) readp;
6734 : 144 : readp = memchr (readp, '\0', cieend - readp);
6735 [ - + ]: 144 : if (unlikely (readp == NULL))
6736 : 0 : goto invalid_data;
6737 : 144 : ++readp;
6738 : :
6739 : 144 : uint_fast8_t segment_size = 0;
6740 [ - + ]: 144 : if (version >= 4)
6741 : : {
6742 [ # # ]: 0 : if (cieend - readp < 5)
6743 : 0 : goto invalid_data;
6744 : 0 : ptr_size = *readp++;
6745 : 0 : segment_size = *readp++;
6746 : : }
6747 : :
6748 [ - + ]: 144 : if (cieend - readp < 1)
6749 : 0 : goto invalid_data;
6750 : 144 : get_uleb128 (code_alignment_factor, readp, cieend);
6751 [ - + ]: 144 : if (cieend - readp < 1)
6752 : 0 : goto invalid_data;
6753 : 144 : get_sleb128 (data_alignment_factor, readp, cieend);
6754 : :
6755 : : /* In some variant for unwind data there is another field. */
6756 [ - + ]: 144 : if (strcmp (augmentation, "eh") == 0)
6757 [ # # ]: 0 : readp += ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 4 : 8;
6758 : :
6759 : 144 : unsigned int return_address_register;
6760 [ - + ]: 144 : if (cieend - readp < 1)
6761 : 0 : goto invalid_data;
6762 [ + + ]: 144 : if (unlikely (version == 1))
6763 : 132 : return_address_register = *readp++;
6764 : : else
6765 : 12 : get_uleb128 (return_address_register, readp, cieend);
6766 : :
6767 : 144 : printf ("\n [%6tx] CIE length=%" PRIu64 "\n"
6768 : : " CIE_id: %" PRIu64 "\n"
6769 : : " version: %u\n"
6770 : : " augmentation: \"%s\"\n",
6771 : : offset, (uint64_t) unit_length, (uint64_t) cie_id,
6772 : : version, augmentation);
6773 [ - + ]: 144 : if (version >= 4)
6774 : 0 : printf (" address_size: %u\n"
6775 : : " segment_size: %u\n",
6776 : : ptr_size, segment_size);
6777 : 144 : printf (" code_alignment_factor: %u\n"
6778 : : " data_alignment_factor: %d\n"
6779 : : " return_address_register: %u\n",
6780 : : code_alignment_factor,
6781 : : data_alignment_factor, return_address_register);
6782 : :
6783 [ + + ]: 144 : if (augmentation[0] == 'z')
6784 : : {
6785 : 104 : unsigned int augmentationlen;
6786 : 104 : get_uleb128 (augmentationlen, readp, cieend);
6787 : :
6788 [ - + ]: 104 : if (augmentationlen > (size_t) (cieend - readp))
6789 : : {
6790 : 0 : error (0, 0, _("invalid augmentation length"));
6791 : 0 : readp = cieend;
6792 : 0 : continue;
6793 : : }
6794 : :
6795 : 104 : const char *hdr = "Augmentation data:";
6796 : 104 : const char *cp = augmentation + 1;
6797 [ + + + - ]: 208 : while (*cp != '\0' && cp < augmentation + augmentationlen + 1)
6798 : : {
6799 : 104 : printf (" %-26s%#x ", hdr, *readp);
6800 : 104 : hdr = "";
6801 : :
6802 [ + - ]: 104 : if (*cp == 'R')
6803 : : {
6804 : 104 : fde_encoding = *readp++;
6805 : 104 : print_encoding_base (_("FDE address encoding: "),
6806 : : fde_encoding);
6807 : : }
6808 [ # # ]: 0 : else if (*cp == 'L')
6809 : : {
6810 : 0 : lsda_encoding = *readp++;
6811 : 0 : print_encoding_base (_("LSDA pointer encoding: "),
6812 : : lsda_encoding);
6813 : : }
6814 [ # # ]: 0 : else if (*cp == 'P')
6815 : : {
6816 : : /* Personality. This field usually has a relocation
6817 : : attached pointing to __gcc_personality_v0. */
6818 : 0 : const unsigned char *startp = readp;
6819 : 0 : unsigned int encoding = *readp++;
6820 : 0 : uint64_t val = 0;
6821 : 0 : readp = read_encoded (encoding, readp,
6822 : 0 : readp - 1 + augmentationlen,
6823 : : &val, dbg);
6824 : :
6825 : 0 : while (++startp < readp)
6826 [ # # ]: 0 : printf ("%#x ", *startp);
6827 : :
6828 : 0 : putchar ('(');
6829 : 0 : print_encoding (encoding);
6830 : 0 : putchar (' ');
6831 [ # # ]: 0 : switch (encoding & 0xf)
6832 : : {
6833 : 0 : case DW_EH_PE_sleb128:
6834 : : case DW_EH_PE_sdata2:
6835 : : case DW_EH_PE_sdata4:
6836 : 0 : printf ("%" PRId64 ")\n", val);
6837 : : break;
6838 : 0 : default:
6839 : 0 : printf ("%#" PRIx64 ")\n", val);
6840 : : break;
6841 : : }
6842 : : }
6843 : : else
6844 : 0 : printf ("(%x)\n", *readp++);
6845 : :
6846 : 104 : ++cp;
6847 : : }
6848 : : }
6849 : :
6850 [ + - ]: 144 : if (likely (ptr_size == 4 || ptr_size == 8))
6851 : : {
6852 : 144 : struct cieinfo *newp = alloca (sizeof (*newp));
6853 : 144 : newp->cie_offset = offset;
6854 : 144 : newp->augmentation = augmentation;
6855 : 144 : newp->fde_encoding = fde_encoding;
6856 : 144 : newp->lsda_encoding = lsda_encoding;
6857 : 144 : newp->address_size = ptr_size;
6858 : 144 : newp->code_alignment_factor = code_alignment_factor;
6859 : 144 : newp->data_alignment_factor = data_alignment_factor;
6860 : 144 : newp->next = cies;
6861 : 144 : cies = newp;
6862 : : }
6863 : : }
6864 : : else
6865 : : {
6866 : : struct cieinfo *cie = cies;
6867 [ + - ]: 12595 : while (cie != NULL)
6868 [ + + - + ]: 25190 : if (is_eh_frame
6869 : 12553 : ? ((Dwarf_Off) start - cie_id) == (Dwarf_Off) cie->cie_offset
6870 : 42 : : cie_id == (Dwarf_Off) cie->cie_offset)
6871 : : break;
6872 : : else
6873 : 0 : cie = cie->next;
6874 [ - + ]: 12595 : if (unlikely (cie == NULL))
6875 : : {
6876 : 0 : puts ("invalid CIE reference in FDE");
6877 : 0 : return;
6878 : : }
6879 : :
6880 : : /* Initialize from CIE data. */
6881 : 12595 : fde_encoding = cie->fde_encoding;
6882 : 12595 : lsda_encoding = cie->lsda_encoding;
6883 : 12595 : ptr_size = encoded_ptr_size (fde_encoding, cie->address_size);
6884 : 12595 : code_alignment_factor = cie->code_alignment_factor;
6885 : 12595 : data_alignment_factor = cie->data_alignment_factor;
6886 : :
6887 : 12595 : const unsigned char *base = readp;
6888 : : // XXX There are sometimes relocations for this value
6889 [ - + + + : 25190 : initial_location = read_addr_unaligned_inc (ptr_size, dbg, readp);
+ + + + ]
6890 : 25190 : Dwarf_Word address_range
6891 [ + + + + : 25190 : = read_addr_unaligned_inc (ptr_size, dbg, readp);
+ + ]
6892 : :
6893 : : /* pcrel for an FDE address is relative to the runtime
6894 : : address of the start_address field itself. Sign extend
6895 : : if necessary to make sure the calculation is done on the
6896 : : full 64 bit address even when initial_location only holds
6897 : : the lower 32 bits. */
6898 : 12595 : Dwarf_Addr pc_start = initial_location;
6899 [ + + ]: 12595 : if (ptr_size == 4)
6900 : 12555 : pc_start = (uint64_t) (int32_t) pc_start;
6901 [ + + ]: 12595 : if ((fde_encoding & 0x70) == DW_EH_PE_pcrel)
6902 : 12547 : pc_start += ((uint64_t) shdr->sh_addr
6903 : 12547 : + (base - (const unsigned char *) data->d_buf)
6904 : 12547 : - bias);
6905 : :
6906 : 12595 : printf ("\n [%6tx] FDE length=%" PRIu64 " cie=[%6tx]\n"
6907 : : " CIE_pointer: %" PRIu64 "\n"
6908 : : " initial_location: ",
6909 : : offset, (uint64_t) unit_length,
6910 : : cie->cie_offset, (uint64_t) cie_id);
6911 : 12595 : print_dwarf_addr (dwflmod, cie->address_size,
6912 : : pc_start, initial_location);
6913 [ + + ]: 12595 : if ((fde_encoding & 0x70) == DW_EH_PE_pcrel)
6914 : : {
6915 : 25094 : vma_base = (((uint64_t) shdr->sh_offset
6916 : 12547 : + (base - (const unsigned char *) data->d_buf)
6917 : 12547 : + (uint64_t) initial_location)
6918 : : & (ptr_size == 4
6919 : : ? UINT64_C (0xffffffff)
6920 [ - + ]: 12547 : : UINT64_C (0xffffffffffffffff)));
6921 : 12547 : printf (_(" (offset: %#" PRIx64 ")"),
6922 : : (uint64_t) vma_base);
6923 : : }
6924 : :
6925 : 12595 : printf ("\n address_range: %#" PRIx64,
6926 : : (uint64_t) address_range);
6927 [ + + ]: 12595 : if ((fde_encoding & 0x70) == DW_EH_PE_pcrel)
6928 : 12547 : printf (_(" (end offset: %#" PRIx64 ")"),
6929 : 12547 : ((uint64_t) vma_base + (uint64_t) address_range)
6930 : : & (ptr_size == 4
6931 : : ? UINT64_C (0xffffffff)
6932 [ - + ]: 12547 : : UINT64_C (0xffffffffffffffff)));
6933 : 12595 : putchar ('\n');
6934 : :
6935 [ + + ]: 12595 : if (cie->augmentation[0] == 'z')
6936 : : {
6937 : 12547 : unsigned int augmentationlen;
6938 [ - + ]: 12547 : if (cieend - readp < 1)
6939 : 0 : goto invalid_data;
6940 : 12547 : get_uleb128 (augmentationlen, readp, cieend);
6941 : :
6942 [ - + ]: 12547 : if (augmentationlen > (size_t) (cieend - readp))
6943 : : {
6944 : 0 : error (0, 0, _("invalid augmentation length"));
6945 : 0 : readp = cieend;
6946 : 0 : continue;
6947 : : }
6948 : :
6949 [ - + ]: 12547 : if (augmentationlen > 0)
6950 : : {
6951 : 0 : const char *hdr = "Augmentation data:";
6952 : 0 : const char *cp = cie->augmentation + 1;
6953 : 0 : unsigned int u = 0;
6954 [ # # ]: 0 : while (*cp != '\0'
6955 [ # # ]: 0 : && cp < cie->augmentation + augmentationlen + 1)
6956 : : {
6957 [ # # ]: 0 : if (*cp == 'L')
6958 : : {
6959 : 0 : uint64_t lsda_pointer;
6960 : 0 : const unsigned char *p
6961 : 0 : = read_encoded (lsda_encoding, &readp[u],
6962 : : &readp[augmentationlen],
6963 : : &lsda_pointer, dbg);
6964 : 0 : u = p - readp;
6965 : 0 : printf (_("\
6966 : : %-26sLSDA pointer: %#" PRIx64 "\n"),
6967 : : hdr, lsda_pointer);
6968 : 0 : hdr = "";
6969 : : }
6970 : 0 : ++cp;
6971 : : }
6972 : :
6973 [ # # ]: 0 : while (u < augmentationlen)
6974 : : {
6975 : 0 : printf (" %-26s%#x\n", hdr, readp[u++]);
6976 : 0 : hdr = "";
6977 : : }
6978 : : }
6979 : :
6980 : 12547 : readp += augmentationlen;
6981 : : }
6982 : : }
6983 : :
6984 : : /* Handle the initialization instructions. */
6985 [ - + ]: 12739 : if (ptr_size != 4 && ptr_size !=8)
6986 : 0 : printf ("invalid CIE pointer size (%u), must be 4 or 8.\n", ptr_size);
6987 : : else
6988 : 12739 : print_cfa_program (readp, cieend, vma_base, code_alignment_factor,
6989 : : data_alignment_factor, version, ptr_size,
6990 : : fde_encoding, dwflmod, ebl, ehdr, dbg);
6991 : 12739 : readp = cieend;
6992 : : }
6993 : : }
6994 : :
6995 : :
6996 : : /* Returns the signedness (or false if it cannot be determined) and
6997 : : the byte size (or zero if it cannot be gotten) of the given DIE
6998 : : DW_AT_type attribute. Uses dwarf_peel_type and dwarf_aggregate_size. */
6999 : : static void
7000 : 159440 : die_type_sign_bytes (Dwarf_Die *die, bool *is_signed, int *bytes)
7001 : : {
7002 : 159440 : Dwarf_Attribute attr;
7003 : 159440 : Dwarf_Die type;
7004 : :
7005 : 159440 : *bytes = 0;
7006 : 159440 : *is_signed = false;
7007 : :
7008 [ + + ]: 159440 : if (dwarf_peel_type (dwarf_formref_die (dwarf_attr_integrate (die,
7009 : : DW_AT_type,
7010 : : &attr), &type),
7011 : : &type) == 0)
7012 : : {
7013 : 460 : Dwarf_Word val;
7014 : 460 : *is_signed = (dwarf_formudata (dwarf_attr (&type, DW_AT_encoding,
7015 : : &attr), &val) == 0
7016 [ + + + + ]: 460 : && (val == DW_ATE_signed || val == DW_ATE_signed_char));
7017 : :
7018 [ + - ]: 460 : if (dwarf_aggregate_size (&type, &val) == 0)
7019 : 460 : *bytes = val;
7020 : : }
7021 : 159440 : }
7022 : :
7023 : : struct attrcb_args
7024 : : {
7025 : : Dwfl_Module *dwflmod;
7026 : : Dwarf *dbg;
7027 : : Dwarf_Die *dies;
7028 : : int level;
7029 : : bool silent;
7030 : : bool is_split;
7031 : : unsigned int version;
7032 : : unsigned int addrsize;
7033 : : unsigned int offset_size;
7034 : : struct Dwarf_CU *cu;
7035 : : };
7036 : :
7037 : :
7038 : : static int
7039 : 3674409 : attr_callback (Dwarf_Attribute *attrp, void *arg)
7040 : : {
7041 : 3674409 : struct attrcb_args *cbargs = (struct attrcb_args *) arg;
7042 : 3674409 : const int level = cbargs->level;
7043 : 3674409 : Dwarf_Die *die = &cbargs->dies[level];
7044 : 3674409 : bool is_split = cbargs->is_split;
7045 : :
7046 [ + - ]: 3674409 : unsigned int attr = dwarf_whatattr (attrp);
7047 [ - + ]: 3674409 : if (unlikely (attr == 0))
7048 : : {
7049 [ # # ]: 0 : if (!cbargs->silent)
7050 : 0 : error (0, 0, _("DIE [%" PRIx64 "] "
7051 : : "cannot get attribute code: %s"),
7052 : : dwarf_dieoffset (die), dwarf_errmsg (-1));
7053 : 0 : return DWARF_CB_ABORT;
7054 : : }
7055 : :
7056 [ + - ]: 3674409 : unsigned int form = dwarf_whatform (attrp);
7057 [ - + ]: 3674409 : if (unlikely (form == 0))
7058 : : {
7059 [ # # ]: 0 : if (!cbargs->silent)
7060 : 0 : error (0, 0, _("DIE [%" PRIx64 "] "
7061 : : "cannot get attribute form: %s"),
7062 : : dwarf_dieoffset (die), dwarf_errmsg (-1));
7063 : 0 : return DWARF_CB_ABORT;
7064 : : }
7065 : :
7066 [ + + + + : 3674409 : switch (form)
+ + + +
- ]
7067 : : {
7068 : 65969 : case DW_FORM_addr:
7069 : : case DW_FORM_addrx:
7070 : : case DW_FORM_addrx1:
7071 : : case DW_FORM_addrx2:
7072 : : case DW_FORM_addrx3:
7073 : : case DW_FORM_addrx4:
7074 : : case DW_FORM_GNU_addr_index:
7075 [ + + ]: 65969 : if (!cbargs->silent)
7076 : : {
7077 : 65698 : Dwarf_Addr addr;
7078 [ - + ]: 65698 : if (unlikely (dwarf_formaddr (attrp, &addr) != 0))
7079 : : {
7080 : 0 : attrval_out:
7081 [ # # ]: 0 : if (!cbargs->silent)
7082 : 0 : error (0, 0, _("DIE [%" PRIx64 "] "
7083 : : "cannot get attribute '%s' (%s) value: "
7084 : : "%s"),
7085 : : dwarf_dieoffset (die),
7086 : : dwarf_attr_name (attr),
7087 : : dwarf_form_name (form),
7088 : : dwarf_errmsg (-1));
7089 : : /* Don't ABORT, it might be other attributes can be resolved. */
7090 : 0 : return DWARF_CB_OK;
7091 : : }
7092 [ + + ]: 65698 : if (form != DW_FORM_addr )
7093 : : {
7094 : 8 : Dwarf_Word word;
7095 [ - + ]: 8 : if (dwarf_formudata (attrp, &word) != 0)
7096 : 0 : goto attrval_out;
7097 : 8 : printf (" %*s%-20s (%s) [%" PRIx64 "] ",
7098 : : (int) (level * 2), "", dwarf_attr_name (attr),
7099 : : dwarf_form_name (form), word);
7100 : : }
7101 : : else
7102 : 65690 : printf (" %*s%-20s (%s) ",
7103 : : (int) (level * 2), "", dwarf_attr_name (attr),
7104 : : dwarf_form_name (form));
7105 : 65698 : print_dwarf_addr (cbargs->dwflmod, cbargs->addrsize, addr, addr);
7106 : 65698 : printf ("\n");
7107 : : }
7108 : 3297160 : break;
7109 : :
7110 : 614952 : case DW_FORM_indirect:
7111 : : case DW_FORM_strp:
7112 : : case DW_FORM_line_strp:
7113 : : case DW_FORM_strx:
7114 : : case DW_FORM_strx1:
7115 : : case DW_FORM_strx2:
7116 : : case DW_FORM_strx3:
7117 : : case DW_FORM_strx4:
7118 : : case DW_FORM_string:
7119 : : case DW_FORM_GNU_strp_alt:
7120 : : case DW_FORM_GNU_str_index:
7121 [ + + ]: 614952 : if (cbargs->silent)
7122 : : break;
7123 : 614197 : const char *str = dwarf_formstring (attrp);
7124 [ - + ]: 614197 : if (unlikely (str == NULL))
7125 : 0 : goto attrval_out;
7126 : 614197 : printf (" %*s%-20s (%s) \"%s\"\n",
7127 : : (int) (level * 2), "", dwarf_attr_name (attr),
7128 : : dwarf_form_name (form), str);
7129 : : break;
7130 : :
7131 : 765290 : case DW_FORM_ref_addr:
7132 : : case DW_FORM_ref_udata:
7133 : : case DW_FORM_ref8:
7134 : : case DW_FORM_ref4:
7135 : : case DW_FORM_ref2:
7136 : : case DW_FORM_ref1:
7137 : : case DW_FORM_GNU_ref_alt:
7138 : : case DW_FORM_ref_sup4:
7139 : : case DW_FORM_ref_sup8:
7140 [ + + ]: 765290 : if (cbargs->silent)
7141 : : break;
7142 : 764500 : Dwarf_Die ref;
7143 [ - + ]: 764500 : if (unlikely (dwarf_formref_die (attrp, &ref) == NULL))
7144 : 0 : goto attrval_out;
7145 : :
7146 : 764500 : printf (" %*s%-20s (%s) ",
7147 : : (int) (level * 2), "", dwarf_attr_name (attr),
7148 : : dwarf_form_name (form));
7149 [ + + ]: 764500 : if (is_split)
7150 : 33 : printf ("{%6" PRIxMAX "}\n", (uintmax_t) dwarf_dieoffset (&ref));
7151 : : else
7152 : 764467 : printf ("[%6" PRIxMAX "]\n", (uintmax_t) dwarf_dieoffset (&ref));
7153 : : break;
7154 : :
7155 : 4 : case DW_FORM_ref_sig8:
7156 [ + - ]: 4 : if (cbargs->silent)
7157 : : break;
7158 : 4 : printf (" %*s%-20s (%s) {%6" PRIx64 "}\n",
7159 : : (int) (level * 2), "", dwarf_attr_name (attr),
7160 : : dwarf_form_name (form),
7161 [ - + ]: 8 : (uint64_t) read_8ubyte_unaligned (attrp->cu->dbg, attrp->valp));
7162 : : break;
7163 : :
7164 : 2049263 : case DW_FORM_sec_offset:
7165 : : case DW_FORM_rnglistx:
7166 : : case DW_FORM_loclistx:
7167 : : case DW_FORM_implicit_const:
7168 : : case DW_FORM_udata:
7169 : : case DW_FORM_sdata:
7170 : : case DW_FORM_data8: /* Note no data16 here, we see that as block. */
7171 : : case DW_FORM_data4:
7172 : : case DW_FORM_data2:
7173 : 2049263 : case DW_FORM_data1:;
7174 : 2049263 : Dwarf_Word num;
7175 [ - + ]: 2049263 : if (unlikely (dwarf_formudata (attrp, &num) != 0))
7176 : 0 : goto attrval_out;
7177 : :
7178 : 2049263 : const char *valuestr = NULL;
7179 : 2049263 : bool as_hex_id = false;
7180 [ + + + + : 2049263 : switch (attr)
+ + - + +
- - - - -
- + - + +
+ ]
7181 : : {
7182 : : /* This case can take either a constant or a loclistptr. */
7183 : 252067 : case DW_AT_data_member_location:
7184 [ + - ]: 252067 : if (form != DW_FORM_sec_offset
7185 [ + + ]: 252067 : && (cbargs->version >= 4
7186 [ + - ]: 23806 : || (form != DW_FORM_data4 && form != DW_FORM_data8)))
7187 : : {
7188 [ + - ]: 252067 : if (!cbargs->silent)
7189 : 252067 : printf (" %*s%-20s (%s) %" PRIuMAX "\n",
7190 : : (int) (level * 2), "", dwarf_attr_name (attr),
7191 : : dwarf_form_name (form), (uintmax_t) num);
7192 : 252067 : return DWARF_CB_OK;
7193 : : }
7194 : 108739 : FALLTHROUGH;
7195 : :
7196 : : /* These cases always take a loclist[ptr] and no constant. */
7197 : : case DW_AT_location:
7198 : : case DW_AT_data_location:
7199 : : case DW_AT_vtable_elem_location:
7200 : : case DW_AT_string_length:
7201 : : case DW_AT_use_location:
7202 : : case DW_AT_frame_base:
7203 : : case DW_AT_return_addr:
7204 : : case DW_AT_static_link:
7205 : : case DW_AT_segment:
7206 : : case DW_AT_GNU_call_site_value:
7207 : : case DW_AT_GNU_call_site_data_value:
7208 : : case DW_AT_GNU_call_site_target:
7209 : : case DW_AT_GNU_call_site_target_clobbered:
7210 : : case DW_AT_GNU_locviews:
7211 : : {
7212 : 108739 : bool nlpt;
7213 [ + + ]: 108739 : if (cbargs->cu->version < 5)
7214 : : {
7215 [ + + ]: 108666 : if (! cbargs->is_split)
7216 : : {
7217 : 108646 : nlpt = notice_listptr (section_loc, &known_locsptr,
7218 : 108646 : cbargs->addrsize,
7219 : 108646 : cbargs->offset_size,
7220 : : cbargs->cu, num, attr);
7221 : : }
7222 : : else
7223 : : nlpt = true;
7224 : : }
7225 : : else
7226 : : {
7227 : : /* Only register for a real section offset. Otherwise
7228 : : it is a DW_FORM_loclistx which is just an index
7229 : : number and we should already have registered the
7230 : : section offset for the index when we saw the
7231 : : DW_AT_loclists_base CU attribute. */
7232 [ + + ]: 73 : if (form == DW_FORM_sec_offset)
7233 : 34 : nlpt = notice_listptr (section_loc, &known_loclistsptr,
7234 : 34 : cbargs->addrsize, cbargs->offset_size,
7235 : : cbargs->cu, num, attr);
7236 : : else
7237 : : nlpt = true;
7238 : :
7239 : : }
7240 : :
7241 [ + + ]: 108739 : if (!cbargs->silent)
7242 : : {
7243 [ + + - + ]: 108556 : if (cbargs->cu->version < 5 || form == DW_FORM_sec_offset)
7244 [ - + ]: 108549 : printf (" %*s%-20s (%s) location list [%6"
7245 : : PRIxMAX "]%s\n",
7246 : : (int) (level * 2), "", dwarf_attr_name (attr),
7247 : : dwarf_form_name (form), (uintmax_t) num,
7248 : : nlpt ? "" : " <WARNING offset too big>");
7249 : : else
7250 : 7 : printf (" %*s%-20s (%s) location index [%6"
7251 : : PRIxMAX "]\n",
7252 : : (int) (level * 2), "", dwarf_attr_name (attr),
7253 : : dwarf_form_name (form), (uintmax_t) num);
7254 : : }
7255 : : }
7256 : : return DWARF_CB_OK;
7257 : :
7258 : 5 : case DW_AT_loclists_base:
7259 : : {
7260 : 10 : bool nlpt = notice_listptr (section_loc, &known_loclistsptr,
7261 : 5 : cbargs->addrsize, cbargs->offset_size,
7262 : : cbargs->cu, num, attr);
7263 : :
7264 [ + + ]: 5 : if (!cbargs->silent)
7265 [ - + ]: 1 : printf (" %*s%-20s (%s) location list [%6" PRIxMAX "]%s\n",
7266 : : (int) (level * 2), "", dwarf_attr_name (attr),
7267 : : dwarf_form_name (form), (uintmax_t) num,
7268 : : nlpt ? "" : " <WARNING offset too big>");
7269 : : }
7270 : : return DWARF_CB_OK;
7271 : :
7272 : 16421 : case DW_AT_ranges:
7273 : : case DW_AT_start_scope:
7274 : : {
7275 : 16421 : bool nlpt;
7276 [ + + ]: 16421 : if (cbargs->cu->version < 5)
7277 : 16404 : nlpt = notice_listptr (section_ranges, &known_rangelistptr,
7278 : 16404 : cbargs->addrsize, cbargs->offset_size,
7279 : : cbargs->cu, num, attr);
7280 : : else
7281 : : {
7282 : : /* Only register for a real section offset. Otherwise
7283 : : it is a DW_FORM_rangelistx which is just an index
7284 : : number and we should already have registered the
7285 : : section offset for the index when we saw the
7286 : : DW_AT_rnglists_base CU attribute. */
7287 [ + + ]: 17 : if (form == DW_FORM_sec_offset)
7288 : 11 : nlpt = notice_listptr (section_ranges, &known_rnglistptr,
7289 : 11 : cbargs->addrsize, cbargs->offset_size,
7290 : : cbargs->cu, num, attr);
7291 : : else
7292 : : nlpt = true;
7293 : : }
7294 : :
7295 [ + + ]: 16421 : if (!cbargs->silent)
7296 : : {
7297 [ + + + + ]: 16378 : if (cbargs->cu->version < 5 || form == DW_FORM_sec_offset)
7298 [ - + ]: 16376 : printf (" %*s%-20s (%s) range list [%6"
7299 : : PRIxMAX "]%s\n",
7300 : : (int) (level * 2), "", dwarf_attr_name (attr),
7301 : : dwarf_form_name (form), (uintmax_t) num,
7302 : : nlpt ? "" : " <WARNING offset too big>");
7303 : : else
7304 : 2 : printf (" %*s%-20s (%s) range index [%6"
7305 : : PRIxMAX "]\n",
7306 : : (int) (level * 2), "", dwarf_attr_name (attr),
7307 : : dwarf_form_name (form), (uintmax_t) num);
7308 : : }
7309 : : }
7310 : : return DWARF_CB_OK;
7311 : :
7312 : 4 : case DW_AT_rnglists_base:
7313 : : {
7314 : 8 : bool nlpt = notice_listptr (section_ranges, &known_rnglistptr,
7315 : 4 : cbargs->addrsize, cbargs->offset_size,
7316 : : cbargs->cu, num, attr);
7317 [ + + ]: 4 : if (!cbargs->silent)
7318 [ - + ]: 2 : printf (" %*s%-20s (%s) range list [%6"
7319 : : PRIxMAX "]%s\n",
7320 : : (int) (level * 2), "", dwarf_attr_name (attr),
7321 : : dwarf_form_name (form), (uintmax_t) num,
7322 : : nlpt ? "" : " <WARNING offset too big>");
7323 : : }
7324 : : return DWARF_CB_OK;
7325 : :
7326 : 13 : case DW_AT_addr_base:
7327 : : case DW_AT_GNU_addr_base:
7328 : : {
7329 : 26 : bool addrbase = notice_listptr (section_addr, &known_addrbases,
7330 : 13 : cbargs->addrsize,
7331 : 13 : cbargs->offset_size,
7332 : : cbargs->cu, num, attr);
7333 [ + + ]: 13 : if (!cbargs->silent)
7334 [ - + ]: 5 : printf (" %*s%-20s (%s) address base [%6"
7335 : : PRIxMAX "]%s\n",
7336 : : (int) (level * 2), "", dwarf_attr_name (attr),
7337 : : dwarf_form_name (form), (uintmax_t) num,
7338 : : addrbase ? "" : " <WARNING offset too big>");
7339 : : }
7340 : : return DWARF_CB_OK;
7341 : :
7342 : 0 : case DW_AT_str_offsets_base:
7343 : : {
7344 : 0 : bool stroffbase = notice_listptr (section_str, &known_stroffbases,
7345 : 0 : cbargs->addrsize,
7346 : 0 : cbargs->offset_size,
7347 : : cbargs->cu, num, attr);
7348 [ # # ]: 0 : if (!cbargs->silent)
7349 [ - - ]: 377249 : printf (" %*s%-20s (%s) str offsets base [%6"
7350 : : PRIxMAX "]%s\n",
7351 : : (int) (level * 2), "", dwarf_attr_name (attr),
7352 : : dwarf_form_name (form), (uintmax_t) num,
7353 : : stroffbase ? "" : " <WARNING offset too big>");
7354 : : }
7355 : : return DWARF_CB_OK;
7356 : :
7357 : 1702 : case DW_AT_language:
7358 : 1702 : valuestr = dwarf_lang_name (num);
7359 : 1702 : break;
7360 : 30664 : case DW_AT_encoding:
7361 : 30664 : valuestr = dwarf_encoding_name (num);
7362 : 30664 : break;
7363 : 0 : case DW_AT_accessibility:
7364 : 0 : valuestr = dwarf_access_name (num);
7365 : 0 : break;
7366 : 0 : case DW_AT_defaulted:
7367 : 0 : valuestr = dwarf_defaulted_name (num);
7368 : 0 : break;
7369 : 0 : case DW_AT_visibility:
7370 : 0 : valuestr = dwarf_visibility_name (num);
7371 : 0 : break;
7372 : 0 : case DW_AT_virtuality:
7373 : 0 : valuestr = dwarf_virtuality_name (num);
7374 : 0 : break;
7375 : 0 : case DW_AT_identifier_case:
7376 : 0 : valuestr = dwarf_identifier_case_name (num);
7377 : 0 : break;
7378 : 0 : case DW_AT_calling_convention:
7379 : 0 : valuestr = dwarf_calling_convention_name (num);
7380 : 0 : break;
7381 : 3607 : case DW_AT_inline:
7382 : 3607 : valuestr = dwarf_inline_name (num);
7383 : 3607 : break;
7384 : 0 : case DW_AT_ordering:
7385 : 0 : valuestr = dwarf_ordering_name (num);
7386 : 0 : break;
7387 : 445830 : case DW_AT_decl_file:
7388 : : case DW_AT_call_file:
7389 : : {
7390 [ + + ]: 445830 : if (cbargs->silent)
7391 : : break;
7392 : :
7393 : : /* Try to get the actual file, the current interface only
7394 : : gives us full paths, but we only want to show the file
7395 : : name for now. */
7396 : 445406 : Dwarf_Die cudie;
7397 [ + - ]: 445406 : if (dwarf_cu_die (cbargs->cu, &cudie,
7398 : : NULL, NULL, NULL, NULL, NULL, NULL) != NULL)
7399 : : {
7400 : 445406 : Dwarf_Files *files;
7401 : 445406 : size_t nfiles;
7402 [ + - ]: 445406 : if (dwarf_getsrcfiles (&cudie, &files, &nfiles) == 0)
7403 : : {
7404 : 445406 : valuestr = dwarf_filesrc (files, num, NULL, NULL);
7405 [ + - ]: 445406 : if (valuestr != NULL)
7406 : : {
7407 : 445406 : char *filename = strrchr (valuestr, '/');
7408 [ + - ]: 445406 : if (filename != NULL)
7409 : 445406 : valuestr = filename + 1;
7410 : : }
7411 : : else
7412 : 0 : error (0, 0, _("invalid file (%" PRId64 "): %s"),
7413 : : num, dwarf_errmsg (-1));
7414 : : }
7415 : : else
7416 : 0 : error (0, 0, _("no srcfiles for CU [%" PRIx64 "]"),
7417 : : dwarf_dieoffset (&cudie));
7418 : : }
7419 : : else
7420 : 0 : error (0, 0, _("couldn't get DWARF CU: %s"),
7421 : : dwarf_errmsg (-1));
7422 [ - + ]: 445406 : if (valuestr == NULL)
7423 : : valuestr = "???";
7424 : : }
7425 : 445406 : break;
7426 : 13 : case DW_AT_GNU_dwo_id:
7427 : 13 : as_hex_id = true;
7428 : 13 : break;
7429 : :
7430 : : default:
7431 : : /* Nothing. */
7432 : : break;
7433 : : }
7434 : :
7435 [ + + ]: 1672014 : if (cbargs->silent)
7436 : : break;
7437 : :
7438 : : /* When highpc is in constant form it is relative to lowpc.
7439 : : In that case also show the address. */
7440 : 1670159 : Dwarf_Addr highpc;
7441 [ + + + - ]: 1670159 : if (attr == DW_AT_high_pc && dwarf_highpc (die, &highpc) == 0)
7442 : : {
7443 : 12499 : printf (" %*s%-20s (%s) %" PRIuMAX " (",
7444 : : (int) (level * 2), "", dwarf_attr_name (attr),
7445 : : dwarf_form_name (form), (uintmax_t) num);
7446 : 12499 : print_dwarf_addr (cbargs->dwflmod, cbargs->addrsize, highpc, highpc);
7447 : 12499 : printf (")\n");
7448 : : }
7449 : : else
7450 : : {
7451 [ + + ]: 1657660 : if (as_hex_id)
7452 : : {
7453 : 2 : printf (" %*s%-20s (%s) 0x%.16" PRIx64 "\n",
7454 : : (int) (level * 2), "", dwarf_attr_name (attr),
7455 : : dwarf_form_name (form), num);
7456 : : }
7457 : : else
7458 : : {
7459 : 1657658 : Dwarf_Sword snum = 0;
7460 : 1657658 : bool is_signed;
7461 : 1657658 : int bytes = 0;
7462 [ + + ]: 1657658 : if (attr == DW_AT_const_value)
7463 : 159436 : die_type_sign_bytes (die, &is_signed, &bytes);
7464 : : else
7465 : 1498222 : is_signed = (form == DW_FORM_sdata
7466 : 1498222 : || form == DW_FORM_implicit_const);
7467 : :
7468 [ + + ]: 1657658 : if (is_signed)
7469 [ - + ]: 140 : if (unlikely (dwarf_formsdata (attrp, &snum) != 0))
7470 : 0 : goto attrval_out;
7471 : :
7472 [ + + ]: 1657658 : if (valuestr == NULL)
7473 : : {
7474 : 1176584 : printf (" %*s%-20s (%s) ",
7475 : : (int) (level * 2), "", dwarf_attr_name (attr),
7476 : : dwarf_form_name (form));
7477 : : }
7478 : : else
7479 : : {
7480 : 481074 : printf (" %*s%-20s (%s) %s (",
7481 : : (int) (level * 2), "", dwarf_attr_name (attr),
7482 : : dwarf_form_name (form), valuestr);
7483 : : }
7484 : :
7485 [ + + + + : 1657658 : switch (bytes)
+ ]
7486 : : {
7487 : 2 : case 1:
7488 [ + + ]: 2 : if (is_signed)
7489 : 1 : printf ("%" PRId8, (int8_t) snum);
7490 : : else
7491 : 1 : printf ("%" PRIu8, (uint8_t) num);
7492 : : break;
7493 : :
7494 : 2 : case 2:
7495 [ + + ]: 2 : if (is_signed)
7496 : 1 : printf ("%" PRId16, (int16_t) snum);
7497 : : else
7498 : 1 : printf ("%" PRIu16, (uint16_t) num);
7499 : : break;
7500 : :
7501 : 123 : case 4:
7502 [ + + ]: 123 : if (is_signed)
7503 : 116 : printf ("%" PRId32, (int32_t) snum);
7504 : : else
7505 : 7 : printf ("%" PRIu32, (uint32_t) num);
7506 : : break;
7507 : :
7508 : 329 : case 8:
7509 [ + + ]: 329 : if (is_signed)
7510 : 1 : printf ("%" PRId64, (int64_t) snum);
7511 : : else
7512 : 328 : printf ("%" PRIu64, (uint64_t) num);
7513 : : break;
7514 : :
7515 : 1657202 : default:
7516 [ + + ]: 1657202 : if (is_signed)
7517 : 21 : printf ("%" PRIdMAX, (intmax_t) snum);
7518 : : else
7519 : 1657181 : printf ("%" PRIuMAX, (uintmax_t) num);
7520 : : break;
7521 : : }
7522 : :
7523 : : /* Make clear if we switched from a signed encoding to
7524 : : an unsigned value. */
7525 [ + + ]: 1657658 : if (attr == DW_AT_const_value
7526 [ + + ]: 159436 : && (form == DW_FORM_sdata || form == DW_FORM_implicit_const)
7527 [ + + ]: 1218 : && !is_signed)
7528 : 1212 : printf (" (%" PRIdMAX ")", (intmax_t) num);
7529 : :
7530 [ + + ]: 1657658 : if (valuestr == NULL)
7531 : 1176584 : printf ("\n");
7532 : : else
7533 : 1657658 : printf (")\n");
7534 : : }
7535 : : }
7536 : : break;
7537 : :
7538 : 9122 : case DW_FORM_flag:
7539 [ + + ]: 9122 : if (cbargs->silent)
7540 : : break;
7541 : 9048 : bool flag;
7542 [ - + ]: 9048 : if (unlikely (dwarf_formflag (attrp, &flag) != 0))
7543 : 0 : goto attrval_out;
7544 : :
7545 : 18096 : printf (" %*s%-20s (%s) %s\n",
7546 : : (int) (level * 2), "", dwarf_attr_name (attr),
7547 [ + - ]: 9048 : dwarf_form_name (form), flag ? yes_str : no_str);
7548 : : break;
7549 : :
7550 : 60267 : case DW_FORM_flag_present:
7551 [ + + ]: 60267 : if (cbargs->silent)
7552 : : break;
7553 : 59990 : printf (" %*s%-20s (%s) %s\n",
7554 : : (int) (level * 2), "", dwarf_attr_name (attr),
7555 : : dwarf_form_name (form), yes_str);
7556 : : break;
7557 : :
7558 : 109542 : case DW_FORM_exprloc:
7559 : : case DW_FORM_block4:
7560 : : case DW_FORM_block2:
7561 : : case DW_FORM_block1:
7562 : : case DW_FORM_block:
7563 : : case DW_FORM_data16: /* DWARF5 calls this a constant class. */
7564 [ + + ]: 109542 : if (cbargs->silent)
7565 : : break;
7566 : 109411 : Dwarf_Block block;
7567 [ - + ]: 109411 : if (unlikely (dwarf_formblock (attrp, &block) != 0))
7568 : 0 : goto attrval_out;
7569 : :
7570 : 109411 : printf (" %*s%-20s (%s) ",
7571 : : (int) (level * 2), "", dwarf_attr_name (attr),
7572 : : dwarf_form_name (form));
7573 : :
7574 [ - + + ]: 109411 : switch (attr)
7575 : : {
7576 : 0 : default:
7577 [ # # ]: 0 : if (form != DW_FORM_exprloc)
7578 : : {
7579 : 0 : print_block (block.length, block.data);
7580 : 0 : break;
7581 : : }
7582 : 109407 : FALLTHROUGH;
7583 : :
7584 : : case DW_AT_location:
7585 : : case DW_AT_data_location:
7586 : : case DW_AT_data_member_location:
7587 : : case DW_AT_vtable_elem_location:
7588 : : case DW_AT_string_length:
7589 : : case DW_AT_use_location:
7590 : : case DW_AT_frame_base:
7591 : : case DW_AT_return_addr:
7592 : : case DW_AT_static_link:
7593 : : case DW_AT_allocated:
7594 : : case DW_AT_associated:
7595 : : case DW_AT_bit_size:
7596 : : case DW_AT_bit_offset:
7597 : : case DW_AT_bit_stride:
7598 : : case DW_AT_byte_size:
7599 : : case DW_AT_byte_stride:
7600 : : case DW_AT_count:
7601 : : case DW_AT_lower_bound:
7602 : : case DW_AT_upper_bound:
7603 : : case DW_AT_GNU_call_site_value:
7604 : : case DW_AT_GNU_call_site_data_value:
7605 : : case DW_AT_GNU_call_site_target:
7606 : : case DW_AT_GNU_call_site_target_clobbered:
7607 [ + + ]: 109407 : if (form == DW_FORM_exprloc
7608 [ + - ]: 70 : || (form != DW_FORM_data16
7609 [ + - ]: 70 : && attrp->cu->version < 4)) /* blocks were expressions. */
7610 : : {
7611 : 109407 : putchar ('\n');
7612 : 109407 : print_ops (cbargs->dwflmod, cbargs->dbg,
7613 : 109407 : 12 + level * 2, 12 + level * 2,
7614 : : cbargs->version, cbargs->addrsize, cbargs->offset_size,
7615 : 109407 : attrp->cu, block.length, block.data);
7616 : : }
7617 : : else
7618 : 0 : print_block (block.length, block.data);
7619 : : break;
7620 : :
7621 : 4 : case DW_AT_discr_list:
7622 [ - + ]: 4 : if (block.length == 0)
7623 : 0 : puts ("<default>");
7624 [ + - ]: 4 : else if (form != DW_FORM_data16)
7625 : : {
7626 : 4 : const unsigned char *readp = block.data;
7627 : 4 : const unsigned char *readendp = readp + block.length;
7628 : :
7629 : : /* See if we are dealing with a signed or unsigned
7630 : : values. If the parent of this variant DIE is a
7631 : : variant_part then it will either have a discriminant
7632 : : which points to the member which type is the
7633 : : discriminant type. Or the variant_part itself has a
7634 : : type representing the discriminant. */
7635 : 4 : bool is_signed = false;
7636 [ + - ]: 4 : if (level > 0)
7637 : : {
7638 : 4 : Dwarf_Die *parent = &cbargs->dies[level - 1];
7639 [ + - ]: 4 : if (dwarf_tag (die) == DW_TAG_variant
7640 [ + - ]: 4 : && dwarf_tag (parent) == DW_TAG_variant_part)
7641 : : {
7642 : 4 : Dwarf_Die member;
7643 : 4 : Dwarf_Attribute discr_attr;
7644 : 4 : int bytes;
7645 [ + - ]: 4 : if (dwarf_formref_die (dwarf_attr (parent,
7646 : : DW_AT_discr,
7647 : : &discr_attr),
7648 : : &member) != NULL)
7649 : 4 : die_type_sign_bytes (&member, &is_signed, &bytes);
7650 : : else
7651 : 0 : die_type_sign_bytes (parent, &is_signed, &bytes);
7652 : : }
7653 : : }
7654 : 12 : while (readp < readendp)
7655 : : {
7656 : 8 : int d = (int) *readp++;
7657 : 8 : printf ("%s ", dwarf_discr_list_name (d));
7658 [ - + ]: 8 : if (readp >= readendp)
7659 : 0 : goto attrval_out;
7660 : :
7661 : 8 : Dwarf_Word val;
7662 : 8 : Dwarf_Sword sval;
7663 [ + + ]: 8 : if (d == DW_DSC_label)
7664 : : {
7665 [ + + ]: 4 : if (is_signed)
7666 : : {
7667 : 2 : get_sleb128 (sval, readp, readendp);
7668 : 2 : printf ("%" PRId64 "", sval);
7669 : : }
7670 : : else
7671 : : {
7672 : 2 : get_uleb128 (val, readp, readendp);
7673 : 2 : printf ("%" PRIu64 "", val);
7674 : : }
7675 : : }
7676 [ + - ]: 4 : else if (d == DW_DSC_range)
7677 : : {
7678 [ + + ]: 4 : if (is_signed)
7679 : : {
7680 : 3 : get_sleb128 (sval, readp, readendp);
7681 : 3 : printf ("%" PRId64 "..", sval);
7682 [ - + ]: 3 : if (readp >= readendp)
7683 : 0 : goto attrval_out;
7684 : 3 : get_sleb128 (sval, readp, readendp);
7685 : 3 : printf ("%" PRId64 "", sval);
7686 : : }
7687 : : else
7688 : : {
7689 : 1 : get_uleb128 (val, readp, readendp);
7690 : 1 : printf ("%" PRIu64 "..", val);
7691 [ - + ]: 1 : if (readp >= readendp)
7692 : 0 : goto attrval_out;
7693 : 1 : get_uleb128 (val, readp, readendp);
7694 : 1 : printf ("%" PRIu64 "", val);
7695 : : }
7696 : : }
7697 : : else
7698 : : {
7699 : 0 : print_block (readendp - readp, readp);
7700 : 0 : break;
7701 : : }
7702 [ + + ]: 8 : if (readp < readendp)
7703 [ + + ]: 16 : printf (", ");
7704 : : }
7705 : 4 : putchar ('\n');
7706 : : }
7707 : : else
7708 : 0 : print_block (block.length, block.data);
7709 : : break;
7710 : : }
7711 : : break;
7712 : :
7713 : 0 : default:
7714 [ # # ]: 0 : if (cbargs->silent)
7715 : : break;
7716 : 0 : printf (" %*s%-20s (%s) ???\n",
7717 : : (int) (level * 2), "", dwarf_attr_name (attr),
7718 : : dwarf_form_name (form));
7719 : : break;
7720 : : }
7721 : :
7722 : 3297160 : return DWARF_CB_OK;
7723 : : }
7724 : :
7725 : : static void
7726 : 99 : print_debug_units (Dwfl_Module *dwflmod,
7727 : : Ebl *ebl, GElf_Ehdr *ehdr __attribute__ ((unused)),
7728 : : Elf_Scn *scn, GElf_Shdr *shdr,
7729 : : Dwarf *dbg, bool debug_types)
7730 : : {
7731 [ + + + + ]: 99 : const bool silent = !(print_debug_sections & section_info) && !debug_types;
7732 : 99 : const char *secname = section_name (ebl, shdr);
7733 : :
7734 [ + + ]: 99 : if (!silent)
7735 : 63 : printf (_("\
7736 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n [Offset]\n"),
7737 : 63 : elf_ndxscn (scn), secname, (uint64_t) shdr->sh_offset);
7738 : :
7739 : : /* If the section is empty we don't have to do anything. */
7740 [ + + - + ]: 99 : if (!silent && shdr->sh_size == 0)
7741 : 0 : return;
7742 : :
7743 : 99 : int maxdies = 20;
7744 : 99 : Dwarf_Die *dies = xmalloc (maxdies * sizeof (Dwarf_Die));
7745 : :
7746 : : /* New compilation unit. */
7747 : 99 : Dwarf_Half version;
7748 : :
7749 : 99 : Dwarf_Die result;
7750 : 99 : Dwarf_Off abbroffset;
7751 : 99 : uint8_t addrsize;
7752 : 99 : uint8_t offsize;
7753 : 99 : uint64_t unit_id;
7754 : 99 : Dwarf_Off subdie_off;
7755 : :
7756 : 99 : int unit_res;
7757 : 99 : Dwarf_CU *cu;
7758 : 99 : Dwarf_CU cu_mem;
7759 : 99 : uint8_t unit_type;
7760 : 99 : Dwarf_Die cudie;
7761 : :
7762 : : /* We cheat a little because we want to see only the CUs from .debug_info
7763 : : or .debug_types. We know the Dwarf_CU struct layout. Set it up at
7764 : : the end of .debug_info if we want .debug_types only. Check the returned
7765 : : Dwarf_CU is still in the expected section. */
7766 [ + + ]: 99 : if (debug_types)
7767 : : {
7768 : 1 : cu_mem.dbg = dbg;
7769 : 1 : cu_mem.end = dbg->sectiondata[IDX_debug_info]->d_size;
7770 : 1 : cu_mem.sec_idx = IDX_debug_info;
7771 : 1 : cu = &cu_mem;
7772 : : }
7773 : : else
7774 : 98 : cu = NULL;
7775 : :
7776 : : next_cu:
7777 : 1810 : unit_res = dwarf_get_units (dbg, cu, &cu, &version, &unit_type,
7778 : : &cudie, NULL);
7779 [ + + ]: 1810 : if (unit_res == 1)
7780 : 98 : goto do_return;
7781 : :
7782 [ - + ]: 1712 : if (unit_res == -1)
7783 : : {
7784 [ # # ]: 0 : if (!silent)
7785 : 0 : error (0, 0, _("cannot get next unit: %s"), dwarf_errmsg (-1));
7786 : 0 : goto do_return;
7787 : : }
7788 : :
7789 [ + + + + ]: 3422 : if (cu->sec_idx != (size_t) (debug_types ? IDX_debug_types : IDX_debug_info))
7790 : 1 : goto do_return;
7791 : :
7792 : 1711 : dwarf_cu_die (cu, &result, NULL, &abbroffset, &addrsize, &offsize,
7793 : : &unit_id, &subdie_off);
7794 : :
7795 [ + + ]: 1711 : if (!silent)
7796 : : {
7797 : 1661 : Dwarf_Off offset = cu->start;
7798 [ + + + - ]: 1661 : if (debug_types && version < 5)
7799 : 2 : {
7800 : 2 : Dwarf_Die typedie;
7801 : 2 : Dwarf_Off dieoffset;
7802 : 2 : dieoffset = dwarf_dieoffset (dwarf_offdie_types (dbg, cu->start
7803 : : + subdie_off,
7804 : : &typedie));
7805 : 2 : printf (_(" Type unit at offset %" PRIu64 ":\n"
7806 : : " Version: %" PRIu16
7807 : : ", Abbreviation section offset: %" PRIu64
7808 : : ", Address size: %" PRIu8
7809 : : ", Offset size: %" PRIu8
7810 : : "\n Type signature: %#" PRIx64
7811 : : ", Type offset: %#" PRIx64 " [%" PRIx64 "]\n"),
7812 : : (uint64_t) offset, version, abbroffset, addrsize, offsize,
7813 : : unit_id, (uint64_t) subdie_off, dieoffset);
7814 : : }
7815 : : else
7816 : : {
7817 : 1659 : printf (_(" Compilation unit at offset %" PRIu64 ":\n"
7818 : : " Version: %" PRIu16
7819 : : ", Abbreviation section offset: %" PRIu64
7820 : : ", Address size: %" PRIu8
7821 : : ", Offset size: %" PRIu8 "\n"),
7822 : : (uint64_t) offset, version, abbroffset, addrsize, offsize);
7823 : :
7824 [ + + ]: 1659 : if (version >= 5 || (unit_type != DW_UT_compile
7825 [ + + ]: 1654 : && unit_type != DW_UT_partial))
7826 : : {
7827 : 6 : printf (_(" Unit type: %s (%" PRIu8 ")"),
7828 : : dwarf_unit_name (unit_type), unit_type);
7829 : 6 : if (unit_type == DW_UT_type
7830 [ + + ]: 6 : || unit_type == DW_UT_skeleton
7831 [ + - ]: 1 : || unit_type == DW_UT_split_compile
7832 [ - + ]: 1 : || unit_type == DW_UT_split_type)
7833 : 5 : printf (", Unit id: 0x%.16" PRIx64 "", unit_id);
7834 : 6 : if (unit_type == DW_UT_type
7835 [ - + ]: 6 : || unit_type == DW_UT_split_type)
7836 : : {
7837 : 0 : Dwarf_Die typedie;
7838 : 0 : Dwarf_Off dieoffset;
7839 : 0 : dwarf_cu_info (cu, NULL, NULL, NULL, &typedie,
7840 : : NULL, NULL, NULL);
7841 : 0 : dieoffset = dwarf_dieoffset (&typedie);
7842 : 0 : printf (", Unit DIE off: %#" PRIx64 " [%" PRIx64 "]",
7843 : : subdie_off, dieoffset);
7844 : : }
7845 : 6 : printf ("\n");
7846 : : }
7847 : : }
7848 : : }
7849 : :
7850 [ + - ]: 1711 : if (version < 2 || version > 5
7851 [ + - - + ]: 1711 : || unit_type < DW_UT_compile || unit_type > DW_UT_split_type)
7852 : : {
7853 [ # # ]: 0 : if (!silent)
7854 : 0 : error (0, 0, _("unknown version (%d) or unit type (%d)"),
7855 : : version, unit_type);
7856 : 0 : goto next_cu;
7857 : : }
7858 : :
7859 : 1711 : struct attrcb_args args =
7860 : : {
7861 : : .dwflmod = dwflmod,
7862 : : .silent = silent,
7863 : : .version = version,
7864 : : .addrsize = addrsize,
7865 : : .offset_size = offsize
7866 : : };
7867 : :
7868 : 1711 : bool is_split = false;
7869 : 1711 : int level = 0;
7870 : 1711 : dies[0] = cudie;
7871 : 1711 : args.cu = dies[0].cu;
7872 : 1711 : args.dbg = dbg;
7873 : 1711 : args.is_split = is_split;
7874 : :
7875 : : /* We might return here again for the split CU subdie. */
7876 : : do_cu:
7877 : 956766 : do
7878 : : {
7879 : 956766 : Dwarf_Off offset = dwarf_dieoffset (&dies[level]);
7880 [ - + ]: 956766 : if (unlikely (offset == (Dwarf_Off) -1))
7881 : : {
7882 [ # # ]: 0 : if (!silent)
7883 : 0 : error (0, 0, _("cannot get DIE offset: %s"),
7884 : : dwarf_errmsg (-1));
7885 : 0 : goto do_return;
7886 : : }
7887 : :
7888 : 956766 : int tag = dwarf_tag (&dies[level]);
7889 [ - + ]: 956766 : if (unlikely (tag == DW_TAG_invalid))
7890 : : {
7891 [ # # ]: 0 : if (!silent)
7892 : 0 : error (0, 0, _("cannot get tag of DIE at offset [%" PRIx64
7893 : : "] in section '%s': %s"),
7894 : : (uint64_t) offset, secname, dwarf_errmsg (-1));
7895 : 0 : goto do_return;
7896 : : }
7897 : :
7898 [ + + ]: 956766 : if (!silent)
7899 : : {
7900 : 955773 : unsigned int code = dwarf_getabbrevcode (dies[level].abbrev);
7901 [ + + ]: 955773 : if (is_split)
7902 : 48 : printf (" {%6" PRIx64 "} ", (uint64_t) offset);
7903 : : else
7904 : 955725 : printf (" [%6" PRIx64 "] ", (uint64_t) offset);
7905 : 955773 : printf ("%*s%-20s abbrev: %u\n", (int) (level * 2), "",
7906 : : dwarf_tag_name (tag), code);
7907 : : }
7908 : :
7909 : : /* Print the attribute values. */
7910 : 956766 : args.level = level;
7911 : 956766 : args.dies = dies;
7912 : 956766 : (void) dwarf_getattrs (&dies[level], attr_callback, &args, 0);
7913 : :
7914 : : /* Make room for the next level's DIE. */
7915 [ - + ]: 956766 : if (level + 1 == maxdies)
7916 : 0 : dies = xrealloc (dies, (maxdies += 10) * sizeof (Dwarf_Die));
7917 : :
7918 : 956766 : int res = dwarf_child (&dies[level], &dies[level + 1]);
7919 [ + + ]: 956766 : if (res > 0)
7920 : : {
7921 [ + + ]: 956766 : while ((res = dwarf_siblingof (&dies[level], &dies[level])) == 1)
7922 [ + + ]: 127395 : if (level-- == 0)
7923 : : break;
7924 : :
7925 [ - + ]: 831086 : if (unlikely (res == -1))
7926 : : {
7927 [ # # ]: 0 : if (!silent)
7928 : 0 : error (0, 0, _("cannot get next DIE: %s\n"),
7929 : : dwarf_errmsg (-1));
7930 : 0 : goto do_return;
7931 : : }
7932 : : }
7933 [ - + ]: 125680 : else if (unlikely (res < 0))
7934 : : {
7935 [ # # ]: 0 : if (!silent)
7936 : 0 : error (0, 0, _("cannot get next DIE: %s"),
7937 : : dwarf_errmsg (-1));
7938 : 0 : goto do_return;
7939 : : }
7940 : : else
7941 : : ++level;
7942 : : }
7943 [ + + ]: 956766 : while (level >= 0);
7944 : :
7945 : : /* We might want to show the split compile unit if this was a skeleton.
7946 : : We need to scan it if we are requesting printing .debug_ranges for
7947 : : DWARF4 since GNU DebugFission uses "offsets" into the main ranges
7948 : : section. */
7949 [ + + ]: 1715 : if (unit_type == DW_UT_skeleton
7950 [ + + + + ]: 13 : && ((!silent && show_split_units)
7951 [ + + + + ]: 10 : || (version < 5 && (print_debug_sections & section_ranges) != 0)))
7952 : : {
7953 : 5 : Dwarf_Die subdie;
7954 [ + - ]: 5 : if (dwarf_cu_info (cu, NULL, NULL, NULL, &subdie, NULL, NULL, NULL) != 0
7955 [ + + ]: 5 : || dwarf_tag (&subdie) == DW_TAG_invalid)
7956 : : {
7957 [ + - ]: 1 : if (!silent)
7958 : : {
7959 : 1 : Dwarf_Attribute dwo_at;
7960 : 2 : const char *dwo_name =
7961 : 1 : (dwarf_formstring (dwarf_attr (&cudie, DW_AT_dwo_name,
7962 : : &dwo_at))
7963 [ - + ]: 1 : ?: (dwarf_formstring (dwarf_attr (&cudie, DW_AT_GNU_dwo_name,
7964 : : &dwo_at))
7965 [ # # ]: 0 : ?: "<unknown>"));
7966 : 1 : fprintf (stderr,
7967 : : "Could not find split unit '%s', id: %" PRIx64 "\n",
7968 : : dwo_name, unit_id);
7969 : : }
7970 : : }
7971 : : else
7972 : : {
7973 : 4 : Dwarf_CU *split_cu = subdie.cu;
7974 : 4 : dwarf_cu_die (split_cu, &result, NULL, &abbroffset,
7975 : : &addrsize, &offsize, &unit_id, &subdie_off);
7976 : 4 : Dwarf_Off offset = cu->start;
7977 : :
7978 [ + + ]: 4 : if (!silent)
7979 : : {
7980 : 2 : printf (_(" Split compilation unit at offset %"
7981 : : PRIu64 ":\n"
7982 : : " Version: %" PRIu16
7983 : : ", Abbreviation section offset: %" PRIu64
7984 : : ", Address size: %" PRIu8
7985 : : ", Offset size: %" PRIu8 "\n"),
7986 : : (uint64_t) offset, version, abbroffset,
7987 : : addrsize, offsize);
7988 : 2 : printf (_(" Unit type: %s (%" PRIu8 ")"),
7989 : : dwarf_unit_name (unit_type), unit_type);
7990 : 2 : printf (", Unit id: 0x%.16" PRIx64 "", unit_id);
7991 : 2 : printf ("\n");
7992 : : }
7993 : :
7994 : 4 : unit_type = DW_UT_split_compile;
7995 : 4 : is_split = true;
7996 : 4 : level = 0;
7997 : 4 : dies[0] = subdie;
7998 : 4 : args.cu = dies[0].cu;
7999 : 4 : args.dbg = split_cu->dbg;
8000 : 4 : args.is_split = is_split;
8001 : 4 : goto do_cu;
8002 : : }
8003 : : }
8004 : :
8005 : : /* And again... */
8006 : 1711 : goto next_cu;
8007 : :
8008 : 99 : do_return:
8009 : 99 : free (dies);
8010 : : }
8011 : :
8012 : : static void
8013 : 98 : print_debug_info_section (Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr,
8014 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
8015 : : {
8016 : 20 : print_debug_units (dwflmod, ebl, ehdr, scn, shdr, dbg, false);
8017 : 20 : }
8018 : :
8019 : : static void
8020 : 1 : print_debug_types_section (Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr,
8021 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
8022 : : {
8023 : 1 : print_debug_units (dwflmod, ebl, ehdr, scn, shdr, dbg, true);
8024 : 1 : }
8025 : :
8026 : :
8027 : : static void
8028 : 39 : print_decoded_line_section (Dwfl_Module *dwflmod, Ebl *ebl,
8029 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
8030 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
8031 : : {
8032 : 39 : printf (_("\
8033 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n\n"),
8034 : : elf_ndxscn (scn), section_name (ebl, shdr),
8035 : 39 : (uint64_t) shdr->sh_offset);
8036 : :
8037 : 78 : size_t address_size
8038 [ + + ]: 39 : = elf_getident (ebl->elf, NULL)[EI_CLASS] == ELFCLASS32 ? 4 : 8;
8039 : :
8040 : 39 : Dwarf_Lines *lines;
8041 : 39 : size_t nlines;
8042 : 39 : Dwarf_Off off, next_off = 0;
8043 : 39 : Dwarf_CU *cu = NULL;
8044 [ + + ]: 84 : while (dwarf_next_lines (dbg, off = next_off, &next_off, &cu, NULL, NULL,
8045 : : &lines, &nlines) == 0)
8046 : : {
8047 : 45 : Dwarf_Die cudie;
8048 [ + + + - ]: 45 : if (cu != NULL && dwarf_cu_info (cu, NULL, NULL, &cudie,
8049 : : NULL, NULL, NULL, NULL) == 0)
8050 : 43 : printf (" CU [%" PRIx64 "] %s\n",
8051 : : dwarf_dieoffset (&cudie), dwarf_diename (&cudie));
8052 : : else
8053 : : {
8054 : : /* DWARF5 lines can be independent of any CU, but they probably
8055 : : are used by some CU. Determine the CU this block is for. */
8056 : 2 : Dwarf_Off cuoffset;
8057 : 2 : Dwarf_Off ncuoffset = 0;
8058 : 2 : size_t hsize;
8059 [ + - ]: 3 : while (dwarf_nextcu (dbg, cuoffset = ncuoffset, &ncuoffset, &hsize,
8060 : : NULL, NULL, NULL) == 0)
8061 : : {
8062 [ - + ]: 3 : if (dwarf_offdie (dbg, cuoffset + hsize, &cudie) == NULL)
8063 : 0 : continue;
8064 : 3 : Dwarf_Attribute stmt_list;
8065 [ - + ]: 3 : if (dwarf_attr (&cudie, DW_AT_stmt_list, &stmt_list) == NULL)
8066 : 0 : continue;
8067 : 3 : Dwarf_Word lineoff;
8068 [ - + ]: 3 : if (dwarf_formudata (&stmt_list, &lineoff) != 0)
8069 : 0 : continue;
8070 [ + + ]: 3 : if (lineoff == off)
8071 : : {
8072 : : /* Found the CU. */
8073 : 2 : cu = cudie.cu;
8074 : 2 : break;
8075 : : }
8076 : : }
8077 : :
8078 [ + - ]: 2 : if (cu != NULL)
8079 : 2 : printf (" CU [%" PRIx64 "] %s\n",
8080 : : dwarf_dieoffset (&cudie), dwarf_diename (&cudie));
8081 : : else
8082 : 2 : printf (" No CU\n");
8083 : : }
8084 : :
8085 : 45 : printf (" line:col SBPE* disc isa op address"
8086 : : " (Statement Block Prologue Epilogue *End)\n");
8087 : 45 : const char *last_file = "";
8088 [ + + ]: 348 : for (size_t n = 0; n < nlines; n++)
8089 : : {
8090 : 303 : Dwarf_Line *line = dwarf_onesrcline (lines, n);
8091 [ - + ]: 303 : if (line == NULL)
8092 : : {
8093 : 0 : printf (" dwarf_onesrcline: %s\n", dwarf_errmsg (-1));
8094 : 0 : continue;
8095 : : }
8096 : 303 : Dwarf_Word mtime, length;
8097 : 303 : const char *file = dwarf_linesrc (line, &mtime, &length);
8098 [ - + ]: 303 : if (file == NULL)
8099 : : {
8100 : 0 : printf (" <%s> (mtime: ?, length: ?)\n", dwarf_errmsg (-1));
8101 : 0 : last_file = "";
8102 : : }
8103 [ + + ]: 303 : else if (strcmp (last_file, file) != 0)
8104 : : {
8105 : 53 : printf (" %s (mtime: %" PRIu64 ", length: %" PRIu64 ")\n",
8106 : : file, mtime, length);
8107 : 53 : last_file = file;
8108 : : }
8109 : :
8110 : 303 : int lineno, colno;
8111 : 303 : bool statement, endseq, block, prologue_end, epilogue_begin;
8112 : 303 : unsigned int lineop, isa, disc;
8113 : 303 : Dwarf_Addr address;
8114 : 303 : dwarf_lineaddr (line, &address);
8115 : 303 : dwarf_lineno (line, &lineno);
8116 : 303 : dwarf_linecol (line, &colno);
8117 : 303 : dwarf_lineop_index (line, &lineop);
8118 : 303 : dwarf_linebeginstatement (line, &statement);
8119 : 303 : dwarf_lineendsequence (line, &endseq);
8120 : 303 : dwarf_lineblock (line, &block);
8121 : 303 : dwarf_lineprologueend (line, &prologue_end);
8122 : 303 : dwarf_lineepiloguebegin (line, &epilogue_begin);
8123 : 303 : dwarf_lineisa (line, &isa);
8124 : 303 : dwarf_linediscriminator (line, &disc);
8125 : :
8126 : : /* End sequence is special, it is one byte past. */
8127 : 303 : printf (" %4d:%-3d %c%c%c%c%c %4d %3d %2d ",
8128 : : lineno, colno,
8129 [ + + ]: 303 : (statement ? 'S' : ' '),
8130 [ + - ]: 303 : (block ? 'B' : ' '),
8131 [ + + ]: 303 : (prologue_end ? 'P' : ' '),
8132 [ + - ]: 303 : (epilogue_begin ? 'E' : ' '),
8133 [ + + ]: 303 : (endseq ? '*' : ' '),
8134 : : disc, isa, lineop);
8135 : 303 : print_dwarf_addr (dwflmod, address_size,
8136 [ + + ]: 303 : address - (endseq ? 1 : 0), address);
8137 : 303 : printf ("\n");
8138 : :
8139 [ + + ]: 303 : if (endseq)
8140 : 303 : printf("\n");
8141 : : }
8142 : : }
8143 : 39 : }
8144 : :
8145 : :
8146 : : /* Print the value of a form.
8147 : : Returns new value of readp, or readendp on failure. */
8148 : : static const unsigned char *
8149 : 20 : print_form_data (Dwarf *dbg, int form, const unsigned char *readp,
8150 : : const unsigned char *readendp, unsigned int offset_len,
8151 : : Dwarf_Off str_offsets_base)
8152 : : {
8153 : 20 : Dwarf_Word val;
8154 : 20 : unsigned char *endp;
8155 : 20 : Elf_Data *data;
8156 : 20 : char *str;
8157 [ + - - - : 20 : switch (form)
- - - - -
- - - - +
- - - - -
- - ]
8158 : : {
8159 : 8 : case DW_FORM_data1:
8160 [ - + ]: 8 : if (readendp - readp < 1)
8161 : : {
8162 : 0 : invalid_data:
8163 : 0 : error (0, 0, "invalid data");
8164 : 0 : return readendp;
8165 : : }
8166 : 8 : val = *readp++;
8167 : 8 : printf (" %" PRIx8, (unsigned int) val);
8168 : : break;
8169 : :
8170 : 0 : case DW_FORM_data2:
8171 [ # # ]: 0 : if (readendp - readp < 2)
8172 : 0 : goto invalid_data;
8173 [ # # ]: 0 : val = read_2ubyte_unaligned_inc (dbg, readp);
8174 : 0 : printf(" %" PRIx16, (unsigned int) val);
8175 : : break;
8176 : :
8177 : 0 : case DW_FORM_data4:
8178 [ # # ]: 0 : if (readendp - readp < 4)
8179 : 0 : goto invalid_data;
8180 [ # # ]: 0 : val = read_4ubyte_unaligned_inc (dbg, readp);
8181 : 0 : printf (" %" PRIx32, (unsigned int) val);
8182 : : break;
8183 : :
8184 : 0 : case DW_FORM_data8:
8185 [ # # ]: 0 : if (readendp - readp < 8)
8186 : 0 : goto invalid_data;
8187 [ # # ]: 0 : val = read_8ubyte_unaligned_inc (dbg, readp);
8188 : 0 : printf (" %" PRIx64, val);
8189 : : break;
8190 : :
8191 : 0 : case DW_FORM_sdata:
8192 [ # # ]: 0 : if (readendp - readp < 1)
8193 : 0 : goto invalid_data;
8194 : 0 : get_sleb128 (val, readp, readendp);
8195 : 0 : printf (" %" PRIx64, val);
8196 : : break;
8197 : :
8198 : 0 : case DW_FORM_udata:
8199 [ # # ]: 0 : if (readendp - readp < 1)
8200 : 0 : goto invalid_data;
8201 : 0 : get_uleb128 (val, readp, readendp);
8202 : 0 : printf (" %" PRIx64, val);
8203 : : break;
8204 : :
8205 : 0 : case DW_FORM_block:
8206 [ # # ]: 0 : if (readendp - readp < 1)
8207 : 0 : goto invalid_data;
8208 : 0 : get_uleb128 (val, readp, readendp);
8209 [ # # ]: 0 : if ((size_t) (readendp - readp) < val)
8210 : 0 : goto invalid_data;
8211 : 0 : print_bytes (val, readp);
8212 : 0 : readp += val;
8213 : 0 : break;
8214 : :
8215 : 0 : case DW_FORM_block1:
8216 [ # # ]: 0 : if (readendp - readp < 1)
8217 : 0 : goto invalid_data;
8218 : 0 : val = *readp++;
8219 [ # # ]: 0 : if ((size_t) (readendp - readp) < val)
8220 : 0 : goto invalid_data;
8221 : 0 : print_bytes (val, readp);
8222 : 0 : readp += val;
8223 : 0 : break;
8224 : :
8225 : 0 : case DW_FORM_block2:
8226 [ # # ]: 0 : if (readendp - readp < 2)
8227 : 0 : goto invalid_data;
8228 [ # # ]: 0 : val = read_2ubyte_unaligned_inc (dbg, readp);
8229 [ # # ]: 0 : if ((size_t) (readendp - readp) < val)
8230 : 0 : goto invalid_data;
8231 : 0 : print_bytes (val, readp);
8232 : 0 : readp += val;
8233 : 0 : break;
8234 : :
8235 : 0 : case DW_FORM_block4:
8236 [ # # ]: 0 : if (readendp - readp < 4)
8237 : 0 : goto invalid_data;
8238 [ # # ]: 0 : val = read_4ubyte_unaligned_inc (dbg, readp);
8239 [ # # ]: 0 : if ((size_t) (readendp - readp) < val)
8240 : 0 : goto invalid_data;
8241 : 0 : print_bytes (val, readp);
8242 : 0 : readp += val;
8243 : 0 : break;
8244 : :
8245 : 0 : case DW_FORM_data16:
8246 [ # # ]: 0 : if (readendp - readp < 16)
8247 : 0 : goto invalid_data;
8248 : 0 : print_bytes (16, readp);
8249 : 0 : readp += 16;
8250 : 0 : break;
8251 : :
8252 : 0 : case DW_FORM_flag:
8253 [ # # ]: 0 : if (readendp - readp < 1)
8254 : 0 : goto invalid_data;
8255 : 0 : val = *readp++;
8256 [ # # ]: 0 : printf ("%s", val != 0 ? yes_str : no_str);
8257 : : break;
8258 : :
8259 : 0 : case DW_FORM_string:
8260 : 0 : endp = memchr (readp, '\0', readendp - readp);
8261 [ # # ]: 0 : if (endp == NULL)
8262 : 0 : goto invalid_data;
8263 : 0 : printf ("%s", readp);
8264 : 0 : readp = endp + 1;
8265 : 0 : break;
8266 : :
8267 : 12 : case DW_FORM_strp:
8268 : : case DW_FORM_line_strp:
8269 : : case DW_FORM_strp_sup:
8270 [ - + ]: 12 : if ((size_t) (readendp - readp) < offset_len)
8271 : 0 : goto invalid_data;
8272 [ - + ]: 12 : if (offset_len == 8)
8273 [ # # ]: 0 : val = read_8ubyte_unaligned_inc (dbg, readp);
8274 : : else
8275 [ - + ]: 24 : val = read_4ubyte_unaligned_inc (dbg, readp);
8276 [ - + ]: 12 : if (form == DW_FORM_strp)
8277 : 0 : data = dbg->sectiondata[IDX_debug_str];
8278 [ + - ]: 12 : else if (form == DW_FORM_line_strp)
8279 : 12 : data = dbg->sectiondata[IDX_debug_line_str];
8280 : : else /* form == DW_FORM_strp_sup */
8281 : : {
8282 : 0 : Dwarf *alt = dwarf_getalt (dbg);
8283 [ # # ]: 0 : data = alt != NULL ? alt->sectiondata[IDX_debug_str] : NULL;
8284 : : }
8285 [ + - + - ]: 12 : if (data == NULL || val >= data->d_size
8286 [ + - ]: 12 : || memchr (data->d_buf + val, '\0', data->d_size - val) == NULL)
8287 : : str = "???";
8288 : : else
8289 : 12 : str = (char *) data->d_buf + val;
8290 : 12 : printf ("%s (%" PRIu64 ")", str, val);
8291 : : break;
8292 : :
8293 : 0 : case DW_FORM_sec_offset:
8294 [ # # ]: 0 : if ((size_t) (readendp - readp) < offset_len)
8295 : 0 : goto invalid_data;
8296 [ # # ]: 0 : if (offset_len == 8)
8297 [ # # ]: 0 : val = read_8ubyte_unaligned_inc (dbg, readp);
8298 : : else
8299 [ # # ]: 0 : val = read_4ubyte_unaligned_inc (dbg, readp);
8300 : 0 : printf ("[%" PRIx64 "]", val);
8301 : : break;
8302 : :
8303 : 0 : case DW_FORM_strx:
8304 : : case DW_FORM_GNU_str_index:
8305 [ # # ]: 0 : if (readendp - readp < 1)
8306 : 0 : goto invalid_data;
8307 : 0 : get_uleb128 (val, readp, readendp);
8308 : 0 : strx_val:
8309 : 0 : data = dbg->sectiondata[IDX_debug_str_offsets];
8310 [ # # ]: 0 : if (data == NULL
8311 [ # # ]: 0 : || data->d_size - str_offsets_base < val)
8312 : : str = "???";
8313 : : else
8314 : : {
8315 : 0 : const unsigned char *strreadp = data->d_buf + str_offsets_base + val;
8316 : 0 : const unsigned char *strreadendp = data->d_buf + data->d_size;
8317 [ # # ]: 0 : if ((size_t) (strreadendp - strreadp) < offset_len)
8318 : : str = "???";
8319 : : else
8320 : : {
8321 : 0 : Dwarf_Off idx;
8322 [ # # ]: 0 : if (offset_len == 8)
8323 [ # # ]: 0 : idx = read_8ubyte_unaligned (dbg, strreadp);
8324 : : else
8325 [ # # ]: 0 : idx = read_4ubyte_unaligned (dbg, strreadp);
8326 : :
8327 : 0 : data = dbg->sectiondata[IDX_debug_str];
8328 [ # # # # ]: 0 : if (data == NULL || idx >= data->d_size
8329 [ # # ]: 0 : || memchr (data->d_buf + idx, '\0',
8330 : : data->d_size - idx) == NULL)
8331 : : str = "???";
8332 : : else
8333 : 0 : str = (char *) data->d_buf + idx;
8334 : : }
8335 : : }
8336 : 0 : printf ("%s (%" PRIu64 ")", str, val);
8337 : : break;
8338 : :
8339 : 0 : case DW_FORM_strx1:
8340 [ # # ]: 0 : if (readendp - readp < 1)
8341 : 0 : goto invalid_data;
8342 : 0 : val = *readp++;
8343 : 0 : goto strx_val;
8344 : :
8345 : 0 : case DW_FORM_strx2:
8346 [ # # ]: 0 : if (readendp - readp < 2)
8347 : 0 : goto invalid_data;
8348 [ # # ]: 0 : val = read_2ubyte_unaligned_inc (dbg, readp);
8349 : 0 : goto strx_val;
8350 : :
8351 : 0 : case DW_FORM_strx3:
8352 [ # # ]: 0 : if (readendp - readp < 3)
8353 : 0 : goto invalid_data;
8354 : 0 : val = read_3ubyte_unaligned_inc (dbg, readp);
8355 : 0 : goto strx_val;
8356 : :
8357 : 0 : case DW_FORM_strx4:
8358 [ # # ]: 0 : if (readendp - readp < 4)
8359 : 0 : goto invalid_data;
8360 [ # # ]: 0 : val = read_4ubyte_unaligned_inc (dbg, readp);
8361 : 0 : goto strx_val;
8362 : :
8363 : 0 : default:
8364 : 0 : error (0, 0, _("unknown form: %s"), dwarf_form_name (form));
8365 : 0 : return readendp;
8366 : : }
8367 : :
8368 : 20 : return readp;
8369 : : }
8370 : :
8371 : : /* Only used via run_advance_pc() macro */
8372 : : static inline void
8373 : 379250 : run_advance_pc (unsigned int op_advance,
8374 : : unsigned int minimum_instr_len,
8375 : : unsigned int max_ops_per_instr,
8376 : : unsigned int *op_addr_advance,
8377 : : Dwarf_Word *address,
8378 : : unsigned int *op_index)
8379 : : {
8380 : 379250 : const unsigned int advanced_op_index = (*op_index) + op_advance;
8381 : :
8382 : 379250 : *op_addr_advance = minimum_instr_len * (advanced_op_index
8383 : 379250 : / max_ops_per_instr);
8384 : 379250 : *address = *address + *op_addr_advance;
8385 : 379250 : *op_index = advanced_op_index % max_ops_per_instr;
8386 : : }
8387 : :
8388 : : static void
8389 : 87 : print_debug_line_section (Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr,
8390 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
8391 : : {
8392 [ + + ]: 87 : if (decodedline)
8393 : : {
8394 : 39 : print_decoded_line_section (dwflmod, ebl, ehdr, scn, shdr, dbg);
8395 : 78 : return;
8396 : : }
8397 : :
8398 : 48 : printf (_("\
8399 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
8400 : : elf_ndxscn (scn), section_name (ebl, shdr),
8401 : 48 : (uint64_t) shdr->sh_offset);
8402 : :
8403 [ + - ]: 48 : if (shdr->sh_size == 0)
8404 : : return;
8405 : :
8406 : : /* There is no functionality in libdw to read the information in the
8407 : : way it is represented here. Hardcode the decoder. */
8408 : 96 : Elf_Data *data = (dbg->sectiondata[IDX_debug_line]
8409 [ - + ]: 48 : ?: elf_rawdata (scn, NULL));
8410 [ - + ]: 48 : if (unlikely (data == NULL))
8411 : : {
8412 : 0 : error (0, 0, _("cannot get line data section data: %s"),
8413 : : elf_errmsg (-1));
8414 : 0 : return;
8415 : : }
8416 : :
8417 : 48 : const unsigned char *linep = (const unsigned char *) data->d_buf;
8418 : 48 : const unsigned char *lineendp;
8419 : :
8420 : 48 : while (linep
8421 [ + + ]: 1695 : < (lineendp = (const unsigned char *) data->d_buf + data->d_size))
8422 : : {
8423 : 1647 : size_t start_offset = linep - (const unsigned char *) data->d_buf;
8424 : :
8425 : 1647 : printf (_("\nTable at offset %zu:\n"), start_offset);
8426 : :
8427 [ - + ]: 1647 : if (unlikely (linep + 4 > lineendp))
8428 : 0 : goto invalid_data;
8429 [ + + ]: 1647 : Dwarf_Word unit_length = read_4ubyte_unaligned_inc (dbg, linep);
8430 : 1647 : unsigned int length = 4;
8431 [ - + ]: 1647 : if (unlikely (unit_length == 0xffffffff))
8432 : : {
8433 [ # # ]: 0 : if (unlikely (linep + 8 > lineendp))
8434 : : {
8435 : 0 : invalid_data:
8436 : 0 : error (0, 0, _("invalid data in section [%zu] '%s'"),
8437 : : elf_ndxscn (scn), section_name (ebl, shdr));
8438 : 0 : return;
8439 : : }
8440 [ # # ]: 0 : unit_length = read_8ubyte_unaligned_inc (dbg, linep);
8441 : 0 : length = 8;
8442 : : }
8443 : :
8444 : : /* Check whether we have enough room in the section. */
8445 [ - + ]: 1647 : if (unlikely (unit_length > (size_t) (lineendp - linep)))
8446 : 0 : goto invalid_data;
8447 : 1647 : lineendp = linep + unit_length;
8448 : :
8449 : : /* The next element of the header is the version identifier. */
8450 [ - + ]: 1647 : if ((size_t) (lineendp - linep) < 2)
8451 : 0 : goto invalid_data;
8452 [ + + ]: 1647 : uint_fast16_t version = read_2ubyte_unaligned_inc (dbg, linep);
8453 : :
8454 : 3294 : size_t address_size
8455 [ + + ]: 1647 : = elf_getident (ebl->elf, NULL)[EI_CLASS] == ELFCLASS32 ? 4 : 8;
8456 : 1647 : unsigned char segment_selector_size = 0;
8457 [ + + ]: 1647 : if (version > 4)
8458 : : {
8459 [ - + ]: 2 : if ((size_t) (lineendp - linep) < 2)
8460 : 0 : goto invalid_data;
8461 : 2 : address_size = *linep++;
8462 : 2 : segment_selector_size = *linep++;
8463 : : }
8464 : :
8465 : : /* Next comes the header length. */
8466 : 1647 : Dwarf_Word header_length;
8467 [ + - ]: 1647 : if (length == 4)
8468 : : {
8469 [ - + ]: 1647 : if ((size_t) (lineendp - linep) < 4)
8470 : 0 : goto invalid_data;
8471 [ + + ]: 3294 : header_length = read_4ubyte_unaligned_inc (dbg, linep);
8472 : : }
8473 : : else
8474 : : {
8475 [ # # ]: 0 : if ((size_t) (lineendp - linep) < 8)
8476 : 0 : goto invalid_data;
8477 [ # # ]: 0 : header_length = read_8ubyte_unaligned_inc (dbg, linep);
8478 : : }
8479 : :
8480 : 1647 : const unsigned char *header_start = linep;
8481 : :
8482 : : /* Next the minimum instruction length. */
8483 [ - + ]: 1647 : if ((size_t) (lineendp - linep) < 1)
8484 : 0 : goto invalid_data;
8485 : 1647 : uint_fast8_t minimum_instr_len = *linep++;
8486 : :
8487 : : /* Next the maximum operations per instruction, in version 4 format. */
8488 : 1647 : uint_fast8_t max_ops_per_instr;
8489 [ + + ]: 1647 : if (version < 4)
8490 : : max_ops_per_instr = 1;
8491 : : else
8492 : : {
8493 [ - + ]: 10 : if ((size_t) (lineendp - linep) < 1)
8494 : 0 : goto invalid_data;
8495 : 10 : max_ops_per_instr = *linep++;
8496 : : }
8497 : :
8498 : : /* We need at least 4 more bytes. */
8499 [ - + ]: 1647 : if ((size_t) (lineendp - linep) < 4)
8500 : 0 : goto invalid_data;
8501 : :
8502 : : /* Then the flag determining the default value of the is_stmt
8503 : : register. */
8504 : 1647 : uint_fast8_t default_is_stmt = *linep++;
8505 : :
8506 : : /* Now the line base. */
8507 : 1647 : int_fast8_t line_base = *linep++;
8508 : :
8509 : : /* And the line range. */
8510 : 1647 : uint_fast8_t line_range = *linep++;
8511 : :
8512 : : /* The opcode base. */
8513 : 1647 : uint_fast8_t opcode_base = *linep++;
8514 : :
8515 : : /* Print what we got so far. */
8516 : 1647 : printf (_("\n"
8517 : : " Length: %" PRIu64 "\n"
8518 : : " DWARF version: %" PRIuFAST16 "\n"
8519 : : " Prologue length: %" PRIu64 "\n"
8520 : : " Address size: %zd\n"
8521 : : " Segment selector size: %zd\n"
8522 : : " Min instruction length: %" PRIuFAST8 "\n"
8523 : : " Max operations per instruction: %" PRIuFAST8 "\n"
8524 : : " Initial value if 'is_stmt': %" PRIuFAST8 "\n"
8525 : : " Line base: %" PRIdFAST8 "\n"
8526 : : " Line range: %" PRIuFAST8 "\n"
8527 : : " Opcode base: %" PRIuFAST8 "\n"
8528 : : "\n"
8529 : : "Opcodes:\n"),
8530 : : (uint64_t) unit_length, version, (uint64_t) header_length,
8531 : : address_size, (size_t) segment_selector_size,
8532 : : minimum_instr_len, max_ops_per_instr,
8533 : : default_is_stmt, line_base,
8534 : : line_range, opcode_base);
8535 : :
8536 [ - + ]: 1647 : if (version < 2 || version > 5)
8537 : : {
8538 : 0 : error (0, 0, _("cannot handle .debug_line version: %u\n"),
8539 : : (unsigned int) version);
8540 : 0 : linep = lineendp;
8541 : 0 : continue;
8542 : : }
8543 : :
8544 [ - + ]: 1647 : if (address_size != 4 && address_size != 8)
8545 : : {
8546 : 0 : error (0, 0, _("cannot handle address size: %u\n"),
8547 : : (unsigned int) address_size);
8548 : 0 : linep = lineendp;
8549 : 0 : continue;
8550 : : }
8551 : :
8552 [ - + ]: 1647 : if (segment_selector_size != 0)
8553 : : {
8554 : 0 : error (0, 0, _("cannot handle segment selector size: %u\n"),
8555 : : (unsigned int) segment_selector_size);
8556 : 0 : linep = lineendp;
8557 : 0 : continue;
8558 : : }
8559 : :
8560 [ - + ]: 1647 : if (unlikely (linep + opcode_base - 1 >= lineendp))
8561 : : {
8562 : 0 : invalid_unit:
8563 : 0 : error (0, 0,
8564 : 0 : _("invalid data at offset %tu in section [%zu] '%s'"),
8565 : 0 : linep - (const unsigned char *) data->d_buf,
8566 : : elf_ndxscn (scn), section_name (ebl, shdr));
8567 : 0 : linep = lineendp;
8568 : 0 : continue;
8569 : : }
8570 : 1647 : int opcode_base_l10 = 1;
8571 : 1647 : unsigned int tmp = opcode_base;
8572 [ + + ]: 3291 : while (tmp > 10)
8573 : : {
8574 : 1644 : tmp /= 10;
8575 : 1644 : ++opcode_base_l10;
8576 : : }
8577 : 1647 : const uint8_t *standard_opcode_lengths = linep - 1;
8578 [ + + ]: 21402 : for (uint_fast8_t cnt = 1; cnt < opcode_base; ++cnt)
8579 : 39510 : printf (ngettext (" [%*" PRIuFAST8 "] %hhu argument\n",
8580 : : " [%*" PRIuFAST8 "] %hhu arguments\n",
8581 : : (int) linep[cnt - 1]),
8582 : 19755 : opcode_base_l10, cnt, linep[cnt - 1]);
8583 : 1647 : linep += opcode_base - 1;
8584 : :
8585 [ - + ]: 1647 : if (unlikely (linep >= lineendp))
8586 : 0 : goto invalid_unit;
8587 : :
8588 : 1647 : Dwarf_Off str_offsets_base = str_offsets_base_off (dbg, NULL);
8589 : :
8590 : 1647 : puts (_("\nDirectory table:"));
8591 [ + + ]: 1647 : if (version > 4)
8592 : : {
8593 : 2 : struct encpair { uint16_t desc; uint16_t form; };
8594 : 2 : struct encpair enc[256];
8595 : :
8596 : 2 : printf (_(" ["));
8597 [ - + ]: 2 : if ((size_t) (lineendp - linep) < 1)
8598 : 0 : goto invalid_data;
8599 : 2 : unsigned char directory_entry_format_count = *linep++;
8600 : 2 : for (int i = 0; i < directory_entry_format_count; i++)
8601 : : {
8602 : 2 : uint16_t desc, form;
8603 [ - + ]: 2 : if ((size_t) (lineendp - linep) < 1)
8604 : 0 : goto invalid_data;
8605 : 2 : get_uleb128 (desc, linep, lineendp);
8606 [ - + ]: 2 : if ((size_t) (lineendp - linep) < 1)
8607 : 0 : goto invalid_data;
8608 : 2 : get_uleb128 (form, linep, lineendp);
8609 : :
8610 : 2 : enc[i].desc = desc;
8611 : 2 : enc[i].form = form;
8612 : :
8613 : 2 : printf ("%s(%s)",
8614 : : dwarf_line_content_description_name (desc),
8615 : : dwarf_form_name (form));
8616 [ - + ]: 2 : if (i + 1 < directory_entry_format_count)
8617 [ + + ]: 4 : printf (", ");
8618 : : }
8619 : 2 : printf ("]\n");
8620 : :
8621 : 2 : uint64_t directories_count;
8622 [ - + ]: 2 : if ((size_t) (lineendp - linep) < 1)
8623 : 0 : goto invalid_data;
8624 : 2 : get_uleb128 (directories_count, linep, lineendp);
8625 : :
8626 : 2 : if (directory_entry_format_count == 0
8627 [ - + ]: 2 : && directories_count != 0)
8628 : 0 : goto invalid_data;
8629 : :
8630 [ + + ]: 6 : for (uint64_t i = 0; i < directories_count; i++)
8631 : : {
8632 : 4 : printf (" %-5" PRIu64 " ", i);
8633 : 4 : for (int j = 0; j < directory_entry_format_count; j++)
8634 : : {
8635 : 4 : linep = print_form_data (dbg, enc[j].form,
8636 : : linep, lineendp, length,
8637 : : str_offsets_base);
8638 [ - + ]: 4 : if (j + 1 < directory_entry_format_count)
8639 [ + + ]: 8 : printf (", ");
8640 : : }
8641 : 4 : printf ("\n");
8642 [ - + ]: 4 : if (linep >= lineendp)
8643 : 0 : goto invalid_unit;
8644 : : }
8645 : : }
8646 : : else
8647 : : {
8648 [ + - + + ]: 12282 : while (linep < lineendp && *linep != 0)
8649 : : {
8650 : 10637 : unsigned char *endp = memchr (linep, '\0', lineendp - linep);
8651 [ - + ]: 10637 : if (unlikely (endp == NULL))
8652 : 0 : goto invalid_unit;
8653 : :
8654 : 10637 : printf (" %s\n", (char *) linep);
8655 : :
8656 : 10637 : linep = endp + 1;
8657 : : }
8658 [ + - - + ]: 1645 : if (linep >= lineendp || *linep != 0)
8659 : 0 : goto invalid_unit;
8660 : : /* Skip the final NUL byte. */
8661 : 1645 : ++linep;
8662 : : }
8663 : :
8664 [ - + ]: 1647 : if (unlikely (linep >= lineendp))
8665 : 0 : goto invalid_unit;
8666 : :
8667 : 1647 : puts (_("\nFile name table:"));
8668 [ + + ]: 1647 : if (version > 4)
8669 : : {
8670 : 2 : struct encpair { uint16_t desc; uint16_t form; };
8671 : 2 : struct encpair enc[256];
8672 : :
8673 : 2 : printf (_(" ["));
8674 [ - + ]: 2 : if ((size_t) (lineendp - linep) < 1)
8675 : 0 : goto invalid_data;
8676 : 2 : unsigned char file_name_format_count = *linep++;
8677 : 2 : for (int i = 0; i < file_name_format_count; i++)
8678 : : {
8679 : 4 : uint64_t desc, form;
8680 [ - + ]: 4 : if ((size_t) (lineendp - linep) < 1)
8681 : 0 : goto invalid_data;
8682 : 4 : get_uleb128 (desc, linep, lineendp);
8683 [ - + ]: 4 : if ((size_t) (lineendp - linep) < 1)
8684 : 0 : goto invalid_data;
8685 : 4 : get_uleb128 (form, linep, lineendp);
8686 : :
8687 [ - + ]: 4 : if (! libdw_valid_user_form (form))
8688 : 0 : goto invalid_data;
8689 : :
8690 : 4 : enc[i].desc = desc;
8691 : 4 : enc[i].form = form;
8692 : :
8693 : 4 : printf ("%s(%s)",
8694 : : dwarf_line_content_description_name (desc),
8695 : : dwarf_form_name (form));
8696 [ + + ]: 4 : if (i + 1 < file_name_format_count)
8697 [ + + ]: 8 : printf (", ");
8698 : : }
8699 : 2 : printf ("]\n");
8700 : :
8701 : 2 : uint64_t file_name_count;
8702 [ - + ]: 2 : if ((size_t) (lineendp - linep) < 1)
8703 : 0 : goto invalid_data;
8704 : 2 : get_uleb128 (file_name_count, linep, lineendp);
8705 : :
8706 : 2 : if (file_name_format_count == 0
8707 [ - + ]: 2 : && file_name_count != 0)
8708 : 0 : goto invalid_data;
8709 : :
8710 [ + + ]: 10 : for (uint64_t i = 0; i < file_name_count; i++)
8711 : : {
8712 : 8 : printf (" %-5" PRIu64 " ", i);
8713 : 8 : for (int j = 0; j < file_name_format_count; j++)
8714 : : {
8715 : 16 : linep = print_form_data (dbg, enc[j].form,
8716 : : linep, lineendp, length,
8717 : : str_offsets_base);
8718 [ + + ]: 16 : if (j + 1 < file_name_format_count)
8719 [ + + ]: 32 : printf (", ");
8720 : : }
8721 : 8 : printf ("\n");
8722 [ - + ]: 8 : if (linep > lineendp)
8723 : 0 : goto invalid_unit;
8724 : : }
8725 : : }
8726 : : else
8727 : : {
8728 : 1645 : puts (_(" Entry Dir Time Size Name"));
8729 [ + - + + ]: 28002 : for (unsigned int cnt = 1; linep < lineendp && *linep != 0; ++cnt)
8730 : : {
8731 : : /* First comes the file name. */
8732 : 26357 : char *fname = (char *) linep;
8733 : 26357 : unsigned char *endp = memchr (fname, '\0', lineendp - linep);
8734 [ - + ]: 26357 : if (unlikely (endp == NULL))
8735 : 0 : goto invalid_unit;
8736 : 26357 : linep = endp + 1;
8737 : :
8738 : : /* Then the index. */
8739 : 26357 : unsigned int diridx;
8740 [ - + ]: 26357 : if (lineendp - linep < 1)
8741 : 0 : goto invalid_unit;
8742 : 26357 : get_uleb128 (diridx, linep, lineendp);
8743 : :
8744 : : /* Next comes the modification time. */
8745 : 26357 : unsigned int mtime;
8746 [ - + ]: 26357 : if (lineendp - linep < 1)
8747 : 0 : goto invalid_unit;
8748 : 26357 : get_uleb128 (mtime, linep, lineendp);
8749 : :
8750 : : /* Finally the length of the file. */
8751 : 26357 : unsigned int fsize;
8752 [ - + ]: 26357 : if (lineendp - linep < 1)
8753 : 0 : goto invalid_unit;
8754 : 26357 : get_uleb128 (fsize, linep, lineendp);
8755 : :
8756 : 26357 : printf (" %-5u %-5u %-9u %-9u %s\n",
8757 : : cnt, diridx, mtime, fsize, fname);
8758 : : }
8759 [ + - - + ]: 1645 : if (linep >= lineendp || *linep != '\0')
8760 : 0 : goto invalid_unit;
8761 : : /* Skip the final NUL byte. */
8762 : 1645 : ++linep;
8763 : : }
8764 : :
8765 : 1647 : unsigned int debug_str_offset = 0;
8766 [ + + ]: 1647 : if (unlikely (linep == header_start + header_length - 4))
8767 : : {
8768 : : /* CUBINs contain an unsigned 4-byte offset */
8769 [ - + ]: 2 : debug_str_offset = read_4ubyte_unaligned_inc (dbg, linep);
8770 : : }
8771 : :
8772 [ + + ]: 1647 : if (linep == lineendp)
8773 : : {
8774 : 16 : puts (_("\nNo line number statements."));
8775 : 16 : continue;
8776 : : }
8777 : :
8778 : 1631 : puts (_("\nLine number statements:"));
8779 : 1631 : Dwarf_Word address = 0;
8780 : 1631 : unsigned int op_index = 0;
8781 : 1631 : size_t line = 1;
8782 : 1631 : uint_fast8_t is_stmt = default_is_stmt;
8783 : :
8784 : : /* Apply the "operation advance" from a special opcode
8785 : : or DW_LNS_advance_pc (as per DWARF4 6.2.5.1). */
8786 : 1631 : unsigned int op_addr_advance;
8787 : : #define advance_pc(op_advance) run_advance_pc(op_advance, minimum_instr_len, \
8788 : : max_ops_per_instr, &op_addr_advance, &address, &op_index)
8789 : :
8790 [ - + ]: 1631 : if (max_ops_per_instr == 0)
8791 : : {
8792 : 0 : error (0, 0,
8793 : 0 : _("invalid maximum operations per instruction is zero"));
8794 : 0 : linep = lineendp;
8795 : 0 : continue;
8796 : : }
8797 : :
8798 : 1124582 : while (linep < lineendp)
8799 : : {
8800 : 1122951 : size_t offset = linep - (const unsigned char *) data->d_buf;
8801 : 1122951 : unsigned int u128;
8802 : 1122951 : int s128;
8803 : :
8804 : : /* Read the opcode. */
8805 : 1122951 : unsigned int opcode = *linep++;
8806 : :
8807 : 1122951 : printf (" [%6" PRIx64 "]", (uint64_t)offset);
8808 : : /* Is this a special opcode? */
8809 [ + + ]: 1122951 : if (likely (opcode >= opcode_base))
8810 : : {
8811 [ - + ]: 377403 : if (unlikely (line_range == 0))
8812 : 0 : goto invalid_unit;
8813 : :
8814 : : /* Yes. Handling this is quite easy since the opcode value
8815 : : is computed with
8816 : :
8817 : : opcode = (desired line increment - line_base)
8818 : : + (line_range * address advance) + opcode_base
8819 : : */
8820 : 377403 : int line_increment = (line_base
8821 : 377403 : + (opcode - opcode_base) % line_range);
8822 : :
8823 : : /* Perform the increments. */
8824 : 377403 : line += line_increment;
8825 : 377403 : advance_pc ((opcode - opcode_base) / line_range);
8826 : :
8827 : 377403 : printf (_(" special opcode %u: address+%u = "),
8828 : : opcode, op_addr_advance);
8829 : 377403 : print_dwarf_addr (dwflmod, 0, address, address);
8830 [ - + ]: 377403 : if (op_index > 0)
8831 : 0 : printf (_(", op_index = %u, line%+d = %zu\n"),
8832 : : op_index, line_increment, line);
8833 : : else
8834 : 377403 : printf (_(", line%+d = %zu\n"),
8835 : : line_increment, line);
8836 : : }
8837 [ + + ]: 745548 : else if (opcode == 0)
8838 : : {
8839 : : /* This an extended opcode. */
8840 [ - + ]: 54715 : if (unlikely (linep + 2 > lineendp))
8841 : 0 : goto invalid_unit;
8842 : :
8843 : : /* The length. */
8844 : 54715 : unsigned int len = *linep++;
8845 : :
8846 [ - + ]: 54715 : if (unlikely (linep + len > lineendp))
8847 : 0 : goto invalid_unit;
8848 : :
8849 : : /* The sub-opcode. */
8850 : 54715 : opcode = *linep++;
8851 : :
8852 : 54715 : printf (_(" extended opcode %u: "), opcode);
8853 : :
8854 [ + + - + : 54715 : switch (opcode)
+ - - ]
8855 : : {
8856 : 1655 : case DW_LNE_end_sequence:
8857 : 1655 : puts (_(" end of sequence"));
8858 : :
8859 : : /* Reset the registers we care about. */
8860 : 1655 : address = 0;
8861 : 1655 : op_index = 0;
8862 : 1655 : line = 1;
8863 : 1655 : is_stmt = default_is_stmt;
8864 : 1655 : break;
8865 : :
8866 : 2044 : case DW_LNE_set_address:
8867 : 2044 : op_index = 0;
8868 [ - + ]: 2044 : if (unlikely ((size_t) (lineendp - linep) < address_size))
8869 : 0 : goto invalid_unit;
8870 [ + + ]: 2044 : if (address_size == 4)
8871 [ + + ]: 34 : address = read_4ubyte_unaligned_inc (dbg, linep);
8872 : : else
8873 [ + + ]: 4054 : address = read_8ubyte_unaligned_inc (dbg, linep);
8874 : : {
8875 : 2044 : printf (_(" set address to "));
8876 : 2044 : print_dwarf_addr (dwflmod, 0, address, address);
8877 : 2044 : printf ("\n");
8878 : : }
8879 : : break;
8880 : :
8881 : 0 : case DW_LNE_define_file:
8882 : : {
8883 : 0 : char *fname = (char *) linep;
8884 : 0 : unsigned char *endp = memchr (linep, '\0',
8885 : 0 : lineendp - linep);
8886 [ # # ]: 0 : if (unlikely (endp == NULL))
8887 : 0 : goto invalid_unit;
8888 : 0 : linep = endp + 1;
8889 : :
8890 : 0 : unsigned int diridx;
8891 [ # # ]: 0 : if (lineendp - linep < 1)
8892 : 0 : goto invalid_unit;
8893 : 0 : get_uleb128 (diridx, linep, lineendp);
8894 : 0 : Dwarf_Word mtime;
8895 [ # # ]: 0 : if (lineendp - linep < 1)
8896 : 0 : goto invalid_unit;
8897 : 0 : get_uleb128 (mtime, linep, lineendp);
8898 : 0 : Dwarf_Word filelength;
8899 [ # # ]: 0 : if (lineendp - linep < 1)
8900 : 0 : goto invalid_unit;
8901 : 0 : get_uleb128 (filelength, linep, lineendp);
8902 : :
8903 : 0 : printf (_("\
8904 : : define new file: dir=%u, mtime=%" PRIu64 ", length=%" PRIu64 ", name=%s\n"),
8905 : : diridx, (uint64_t) mtime, (uint64_t) filelength,
8906 : : fname);
8907 : : }
8908 : : break;
8909 : :
8910 : 51009 : case DW_LNE_set_discriminator:
8911 : : /* Takes one ULEB128 parameter, the discriminator. */
8912 [ + - - + ]: 51009 : if (unlikely (standard_opcode_lengths[opcode] != 1
8913 : : || lineendp - linep < 1))
8914 : 0 : goto invalid_unit;
8915 : :
8916 : 51009 : get_uleb128 (u128, linep, lineendp);
8917 : 51009 : printf (_(" set discriminator to %u\n"), u128);
8918 : : break;
8919 : :
8920 : 7 : case DW_LNE_NVIDIA_inlined_call:
8921 : : {
8922 [ - + ]: 7 : if (unlikely (linep >= lineendp))
8923 : 0 : goto invalid_data;
8924 : :
8925 : 7 : unsigned int context;
8926 : 7 : get_uleb128 (context, linep, lineendp);
8927 : :
8928 [ - + ]: 7 : if (unlikely (linep >= lineendp))
8929 : 0 : goto invalid_data;
8930 : :
8931 : 7 : unsigned int function_name;
8932 : 7 : get_uleb128 (function_name, linep, lineendp);
8933 : 7 : function_name += debug_str_offset;
8934 : :
8935 : 7 : Elf_Data *str_data = dbg->sectiondata[IDX_debug_str];
8936 : 7 : char *function_str;
8937 [ + - + - ]: 7 : if (str_data == NULL || function_name >= str_data->d_size
8938 [ + - ]: 7 : || memchr (str_data->d_buf + function_name, '\0',
8939 : : str_data->d_size - function_name) == NULL)
8940 : : function_str = "???";
8941 : : else
8942 : 7 : function_str = (char *) str_data->d_buf + function_name;
8943 : :
8944 : 7 : printf (_(" set inlined context %u,"
8945 : : " function name %s (0x%x)\n"),
8946 : : context, function_str, function_name);
8947 : : break;
8948 : : }
8949 : :
8950 : 0 : case DW_LNE_NVIDIA_set_function_name:
8951 : : {
8952 [ # # ]: 0 : if (unlikely (linep >= lineendp))
8953 : 0 : goto invalid_data;
8954 : :
8955 : 0 : unsigned int function_name;
8956 : 0 : get_uleb128 (function_name, linep, lineendp);
8957 : 0 : function_name += debug_str_offset;
8958 : :
8959 : 0 : Elf_Data *str_data = dbg->sectiondata[IDX_debug_str];
8960 : 0 : char *function_str;
8961 [ # # # # ]: 0 : if (str_data == NULL || function_name >= str_data->d_size
8962 [ # # ]: 0 : || memchr (str_data->d_buf + function_name, '\0',
8963 : : str_data->d_size - function_name) == NULL)
8964 : : function_str = "???";
8965 : : else
8966 : 0 : function_str = (char *) str_data->d_buf + function_name;
8967 : :
8968 : 0 : printf (_(" set function name %s (0x%x)\n"),
8969 : : function_str, function_name);
8970 : : }
8971 : : break;
8972 : :
8973 : 0 : default:
8974 : : /* Unknown, ignore it. */
8975 : 0 : puts (_(" unknown opcode"));
8976 : 0 : linep += len - 1;
8977 : 0 : break;
8978 : : }
8979 : : }
8980 [ + - ]: 690833 : else if (opcode <= DW_LNS_set_isa)
8981 : : {
8982 : : /* This is a known standard opcode. */
8983 [ + + + + : 690833 : switch (opcode)
+ + - + +
+ - - ]
8984 : : {
8985 : 103166 : case DW_LNS_copy:
8986 : : /* Takes no argument. */
8987 : 103166 : puts (_(" copy"));
8988 : 103166 : break;
8989 : :
8990 : 1596 : case DW_LNS_advance_pc:
8991 : : /* Takes one uleb128 parameter which is added to the
8992 : : address. */
8993 [ - + ]: 1596 : if (lineendp - linep < 1)
8994 : 0 : goto invalid_unit;
8995 : 1596 : get_uleb128 (u128, linep, lineendp);
8996 : 1596 : advance_pc (u128);
8997 : : {
8998 : 1596 : printf (_(" advance address by %u to "),
8999 : : op_addr_advance);
9000 : 1596 : print_dwarf_addr (dwflmod, 0, address, address);
9001 [ - + ]: 1596 : if (op_index > 0)
9002 : 0 : printf (_(", op_index to %u"), op_index);
9003 : 1596 : printf ("\n");
9004 : : }
9005 : : break;
9006 : :
9007 : 88406 : case DW_LNS_advance_line:
9008 : : /* Takes one sleb128 parameter which is added to the
9009 : : line. */
9010 [ - + ]: 88406 : if (lineendp - linep < 1)
9011 : 0 : goto invalid_unit;
9012 : 88406 : get_sleb128 (s128, linep, lineendp);
9013 : 88406 : line += s128;
9014 : 88406 : printf (_("\
9015 : : advance line by constant %d to %" PRId64 "\n"),
9016 : : s128, (int64_t) line);
9017 : : break;
9018 : :
9019 : 31638 : case DW_LNS_set_file:
9020 : : /* Takes one uleb128 parameter which is stored in file. */
9021 [ - + ]: 31638 : if (lineendp - linep < 1)
9022 : 0 : goto invalid_unit;
9023 : 31638 : get_uleb128 (u128, linep, lineendp);
9024 : 31638 : printf (_(" set file to %" PRIu64 "\n"),
9025 : : (uint64_t) u128);
9026 : : break;
9027 : :
9028 : 285782 : case DW_LNS_set_column:
9029 : : /* Takes one uleb128 parameter which is stored in column. */
9030 [ + - - + ]: 285782 : if (unlikely (standard_opcode_lengths[opcode] != 1
9031 : : || lineendp - linep < 1))
9032 : 0 : goto invalid_unit;
9033 : :
9034 : 285782 : get_uleb128 (u128, linep, lineendp);
9035 : 285782 : printf (_(" set column to %" PRIu64 "\n"),
9036 : : (uint64_t) u128);
9037 : : break;
9038 : :
9039 : 179971 : case DW_LNS_negate_stmt:
9040 : : /* Takes no argument. */
9041 : 179971 : is_stmt = 1 - is_stmt;
9042 : 179971 : printf (_(" set '%s' to %" PRIuFAST8 "\n"),
9043 : : "is_stmt", is_stmt);
9044 : : break;
9045 : :
9046 : 0 : case DW_LNS_set_basic_block:
9047 : : /* Takes no argument. */
9048 : 0 : puts (_(" set basic block flag"));
9049 : 0 : break;
9050 : :
9051 : 251 : case DW_LNS_const_add_pc:
9052 : : /* Takes no argument. */
9053 : :
9054 [ - + ]: 251 : if (unlikely (line_range == 0))
9055 : 0 : goto invalid_unit;
9056 : :
9057 : 251 : advance_pc ((255 - opcode_base) / line_range);
9058 : : {
9059 : 251 : printf (_(" advance address by constant %u to "),
9060 : : op_addr_advance);
9061 : 251 : print_dwarf_addr (dwflmod, 0, address, address);
9062 [ - + ]: 251 : if (op_index > 0)
9063 : 0 : printf (_(", op_index to %u"), op_index);
9064 : 251 : printf ("\n");
9065 : : }
9066 : : break;
9067 : :
9068 : 22 : case DW_LNS_fixed_advance_pc:
9069 : : /* Takes one 16 bit parameter which is added to the
9070 : : address. */
9071 [ + - - + ]: 22 : if (unlikely (standard_opcode_lengths[opcode] != 1
9072 : : || lineendp - linep < 2))
9073 : 0 : goto invalid_unit;
9074 : :
9075 [ - + ]: 22 : u128 = read_2ubyte_unaligned_inc (dbg, linep);
9076 : 22 : address += u128;
9077 : 22 : op_index = 0;
9078 : : {
9079 : 22 : printf (_("\
9080 : : advance address by fixed value %u to \n"),
9081 : : u128);
9082 : 22 : print_dwarf_addr (dwflmod, 0, address, address);
9083 : 22 : printf ("\n");
9084 : : }
9085 : : break;
9086 : :
9087 : 1 : case DW_LNS_set_prologue_end:
9088 : : /* Takes no argument. */
9089 : 1 : puts (_(" set prologue end flag"));
9090 : 1 : break;
9091 : :
9092 : 0 : case DW_LNS_set_epilogue_begin:
9093 : : /* Takes no argument. */
9094 : 0 : puts (_(" set epilogue begin flag"));
9095 : 0 : break;
9096 : :
9097 : 0 : case DW_LNS_set_isa:
9098 : : /* Takes one uleb128 parameter which is stored in isa. */
9099 [ # # # # ]: 0 : if (unlikely (standard_opcode_lengths[opcode] != 1
9100 : : || lineendp - linep < 1))
9101 : 0 : goto invalid_unit;
9102 : :
9103 : 0 : get_uleb128 (u128, linep, lineendp);
9104 [ + + ]: 1124582 : printf (_(" set isa to %u\n"), u128);
9105 : : break;
9106 : : }
9107 : : }
9108 : : else
9109 : : {
9110 : : /* This is a new opcode the generator but not we know about.
9111 : : Read the parameters associated with it but then discard
9112 : : everything. Read all the parameters for this opcode. */
9113 : 0 : printf (ngettext (" unknown opcode with %" PRIu8 " parameter:",
9114 : : " unknown opcode with %" PRIu8 " parameters:",
9115 : : standard_opcode_lengths[opcode]),
9116 : 0 : standard_opcode_lengths[opcode]);
9117 : 0 : for (int n = standard_opcode_lengths[opcode];
9118 [ # # # # ]: 0 : n > 0 && linep < lineendp; --n)
9119 : : {
9120 : 0 : get_uleb128 (u128, linep, lineendp);
9121 [ # # ]: 0 : if (n != standard_opcode_lengths[opcode])
9122 [ # # ]: 0 : putc_unlocked (',', stdout);
9123 : 0 : printf (" %u", u128);
9124 : : }
9125 : :
9126 : : /* Next round, ignore this opcode. */
9127 : 0 : continue;
9128 : : }
9129 : : }
9130 : : }
9131 : :
9132 : : /* There must only be one data block. */
9133 [ - + ]: 48 : assert (elf_getdata (scn, data) == NULL);
9134 : : }
9135 : :
9136 : :
9137 : : static void
9138 : 4 : print_debug_loclists_section (Dwfl_Module *dwflmod,
9139 : : Ebl *ebl,
9140 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
9141 : : Elf_Scn *scn, GElf_Shdr *shdr,
9142 : : Dwarf *dbg)
9143 : : {
9144 : 4 : printf (_("\
9145 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
9146 : : elf_ndxscn (scn), section_name (ebl, shdr),
9147 : 4 : (uint64_t) shdr->sh_offset);
9148 : :
9149 : 8 : Elf_Data *data = (dbg->sectiondata[IDX_debug_loclists]
9150 [ - + ]: 4 : ?: elf_rawdata (scn, NULL));
9151 [ - + ]: 4 : if (unlikely (data == NULL))
9152 : : {
9153 : 0 : error (0, 0, _("cannot get .debug_loclists content: %s"),
9154 : : elf_errmsg (-1));
9155 : 0 : return;
9156 : : }
9157 : :
9158 : : /* For the listptr to get the base address/CU. */
9159 : 4 : sort_listptr (&known_loclistsptr, "loclistsptr");
9160 : 4 : size_t listptr_idx = 0;
9161 : :
9162 : 4 : const unsigned char *readp = data->d_buf;
9163 : 4 : const unsigned char *const dataend = ((unsigned char *) data->d_buf
9164 : 4 : + data->d_size);
9165 [ + + ]: 9 : while (readp < dataend)
9166 : : {
9167 [ - + ]: 5 : if (unlikely (readp > dataend - 4))
9168 : : {
9169 : 0 : invalid_data:
9170 : 0 : error (0, 0, _("invalid data in section [%zu] '%s'"),
9171 : : elf_ndxscn (scn), section_name (ebl, shdr));
9172 : 0 : return;
9173 : : }
9174 : :
9175 : 5 : ptrdiff_t offset = readp - (unsigned char *) data->d_buf;
9176 : 5 : printf (_("Table at Offset 0x%" PRIx64 ":\n\n"),
9177 : : (uint64_t) offset);
9178 : :
9179 [ - + ]: 5 : uint64_t unit_length = read_4ubyte_unaligned_inc (dbg, readp);
9180 : 5 : unsigned int offset_size = 4;
9181 [ - + ]: 5 : if (unlikely (unit_length == 0xffffffff))
9182 : : {
9183 [ # # ]: 0 : if (unlikely (readp > dataend - 8))
9184 : 0 : goto invalid_data;
9185 : :
9186 [ # # ]: 0 : unit_length = read_8ubyte_unaligned_inc (dbg, readp);
9187 : 0 : offset_size = 8;
9188 : : }
9189 : 5 : printf (_(" Length: %8" PRIu64 "\n"), unit_length);
9190 : :
9191 : : /* We need at least 2-bytes + 1-byte + 1-byte + 4-bytes = 8
9192 : : bytes to complete the header. And this unit cannot go beyond
9193 : : the section data. */
9194 [ + - ]: 5 : if (readp > dataend - 8
9195 [ + - ]: 5 : || unit_length < 8
9196 [ - + ]: 5 : || unit_length > (uint64_t) (dataend - readp))
9197 : 0 : goto invalid_data;
9198 : :
9199 : 5 : const unsigned char *nexthdr = readp + unit_length;
9200 : :
9201 [ - + ]: 5 : uint16_t version = read_2ubyte_unaligned_inc (dbg, readp);
9202 : 5 : printf (_(" DWARF version: %8" PRIu16 "\n"), version);
9203 : :
9204 [ - + ]: 5 : if (version != 5)
9205 : : {
9206 : 0 : error (0, 0, _("Unknown version"));
9207 : 0 : goto next_table;
9208 : : }
9209 : :
9210 : 5 : uint8_t address_size = *readp++;
9211 : 5 : printf (_(" Address size: %8" PRIu64 "\n"),
9212 : : (uint64_t) address_size);
9213 : :
9214 [ - + ]: 5 : if (address_size != 4 && address_size != 8)
9215 : : {
9216 : 0 : error (0, 0, _("unsupported address size"));
9217 : 0 : goto next_table;
9218 : : }
9219 : :
9220 : 5 : uint8_t segment_size = *readp++;
9221 : 5 : printf (_(" Segment size: %8" PRIu64 "\n"),
9222 : : (uint64_t) segment_size);
9223 : :
9224 [ - + ]: 5 : if (segment_size != 0)
9225 : : {
9226 : 0 : error (0, 0, _("unsupported segment size"));
9227 : 0 : goto next_table;
9228 : : }
9229 : :
9230 [ - + ]: 5 : uint32_t offset_entry_count = read_4ubyte_unaligned_inc (dbg, readp);
9231 : 5 : printf (_(" Offset entries: %8" PRIu64 "\n"),
9232 : : (uint64_t) offset_entry_count);
9233 : :
9234 : : /* We need the CU that uses this unit to get the initial base address. */
9235 : 5 : Dwarf_Addr cu_base = 0;
9236 : 5 : struct Dwarf_CU *cu = NULL;
9237 [ - + ]: 5 : if (listptr_cu (&known_loclistsptr, &listptr_idx,
9238 : : (Dwarf_Off) offset,
9239 : 5 : (Dwarf_Off) (nexthdr - (unsigned char *) data->d_buf),
9240 : : &cu_base, &cu)
9241 [ # # ]: 0 : || split_dwarf_cu_base (dbg, &cu, &cu_base))
9242 : 5 : {
9243 : 5 : Dwarf_Die cudie;
9244 [ - + ]: 5 : if (dwarf_cu_die (cu, &cudie,
9245 : : NULL, NULL, NULL, NULL,
9246 : : NULL, NULL) == NULL)
9247 : 0 : printf (_(" Unknown CU base: "));
9248 : : else
9249 : 5 : printf (_(" CU [%6" PRIx64 "] base: "),
9250 : : dwarf_dieoffset (&cudie));
9251 : 5 : print_dwarf_addr (dwflmod, address_size, cu_base, cu_base);
9252 : 5 : printf ("\n");
9253 : : }
9254 : : else
9255 : 0 : printf (_(" Not associated with a CU.\n"));
9256 : :
9257 : 5 : printf ("\n");
9258 : :
9259 : 5 : const unsigned char *offset_array_start = readp;
9260 [ + + ]: 5 : if (offset_entry_count > 0)
9261 : : {
9262 : 2 : uint64_t max_entries = (unit_length - 8) / offset_size;
9263 [ - + ]: 2 : if (offset_entry_count > max_entries)
9264 : : {
9265 : 0 : error (0, 0,
9266 : 0 : _("too many offset entries for unit length"));
9267 : 0 : offset_entry_count = max_entries;
9268 : : }
9269 : :
9270 : 2 : printf (_(" Offsets starting at 0x%" PRIx64 ":\n"),
9271 : : (uint64_t) (offset_array_start
9272 : 2 : - (unsigned char *) data->d_buf));
9273 [ + + ]: 18 : for (uint32_t idx = 0; idx < offset_entry_count; idx++)
9274 : : {
9275 : 16 : printf (" [%6" PRIu32 "] ", idx);
9276 [ + - ]: 16 : if (offset_size == 4)
9277 : : {
9278 [ - + ]: 16 : uint32_t off = read_4ubyte_unaligned_inc (dbg, readp);
9279 : 16 : printf ("0x%" PRIx32 "\n", off);
9280 : : }
9281 : : else
9282 : : {
9283 [ # # ]: 0 : uint64_t off = read_8ubyte_unaligned_inc (dbg, readp);
9284 : 16 : printf ("0x%" PRIx64 "\n", off);
9285 : : }
9286 : : }
9287 : 2 : printf ("\n");
9288 : : }
9289 : :
9290 : 5 : Dwarf_Addr base = cu_base;
9291 : 5 : bool start_of_list = true;
9292 : 5 : while (readp < nexthdr)
9293 : : {
9294 : 92 : Dwarf_Off off = (Dwarf_Off) (readp - (unsigned char *) data->d_buf);
9295 [ + + ]: 92 : if (listptr_attr (&known_loclistsptr, listptr_idx, off,
9296 : : DW_AT_GNU_locviews))
9297 : : {
9298 : 1 : Dwarf_Off next_off = next_listptr_offset (&known_loclistsptr,
9299 : : &listptr_idx, off);
9300 : 1 : const unsigned char *locp = readp;
9301 : 1 : const unsigned char *locendp;
9302 [ + - ]: 1 : if (next_off == 0
9303 : 1 : || next_off > (size_t) (nexthdr - ((const unsigned char *)
9304 [ + - ]: 1 : data->d_buf)))
9305 : : locendp = nexthdr;
9306 : : else
9307 : 1 : locendp = (const unsigned char *) data->d_buf + next_off;
9308 : :
9309 : 3 : printf (" Offset: %" PRIx64 ", Index: %" PRIx64 "\n",
9310 : 1 : (uint64_t) (readp - (unsigned char *) data->d_buf),
9311 : 1 : (uint64_t) (readp - offset_array_start));
9312 : :
9313 : 2 : while (locp < locendp)
9314 : : {
9315 : 1 : uint64_t v1, v2;
9316 : 1 : get_uleb128 (v1, locp, locendp);
9317 [ - + ]: 1 : if (locp >= locendp)
9318 : : {
9319 : 0 : printf (_(" <INVALID DATA>\n"));
9320 : : break;
9321 : : }
9322 : 1 : get_uleb128 (v2, locp, locendp);
9323 [ + + ]: 2 : printf (" view pair %" PRId64 ", %" PRId64 "\n", v1, v2);
9324 : : }
9325 : :
9326 : 1 : printf ("\n");
9327 : 1 : readp = (unsigned char *) locendp;
9328 : 1 : continue;
9329 : : }
9330 : :
9331 : 91 : uint8_t kind = *readp++;
9332 : 91 : uint64_t op1, op2, len;
9333 : :
9334 : : /* Skip padding. */
9335 [ - + ]: 91 : if (start_of_list && kind == DW_LLE_end_of_list)
9336 : 0 : continue;
9337 : :
9338 [ + + ]: 91 : if (start_of_list)
9339 : : {
9340 : 33 : base = cu_base;
9341 : 33 : printf (" Offset: %" PRIx64 ", Index: %" PRIx64 "\n",
9342 : 33 : (uint64_t) (readp - (unsigned char *) data->d_buf - 1),
9343 : 33 : (uint64_t) (readp - offset_array_start - 1));
9344 : 33 : start_of_list = false;
9345 : : }
9346 : :
9347 : 91 : printf (" %s", dwarf_loc_list_encoding_name (kind));
9348 [ + - - + : 91 : switch (kind)
+ - + - +
- ]
9349 : : {
9350 : 33 : case DW_LLE_end_of_list:
9351 : 33 : start_of_list = true;
9352 : 33 : printf ("\n\n");
9353 : : break;
9354 : :
9355 : 0 : case DW_LLE_base_addressx:
9356 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
9357 : : {
9358 : 0 : invalid_entry:
9359 : 0 : error (0, 0, _("invalid loclists data"));
9360 : 0 : goto next_table;
9361 : : }
9362 : 0 : get_uleb128 (op1, readp, nexthdr);
9363 : 0 : printf (" %" PRIx64 "\n", op1);
9364 [ # # ]: 0 : if (! print_unresolved_addresses)
9365 : : {
9366 : 0 : Dwarf_Addr addr;
9367 [ # # ]: 0 : if (get_indexed_addr (cu, op1, &addr) != 0)
9368 : 0 : printf (" ???\n");
9369 : : else
9370 : : {
9371 : 0 : printf (" ");
9372 : 0 : print_dwarf_addr (dwflmod, address_size, addr, addr);
9373 : 0 : printf ("\n");
9374 : : }
9375 : : }
9376 : : break;
9377 : :
9378 : 0 : case DW_LLE_startx_endx:
9379 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
9380 : 0 : goto invalid_entry;
9381 : 0 : get_uleb128 (op1, readp, nexthdr);
9382 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
9383 : 0 : goto invalid_entry;
9384 : 0 : get_uleb128 (op2, readp, nexthdr);
9385 : 0 : printf (" %" PRIx64 ", %" PRIx64 "\n", op1, op2);
9386 [ # # ]: 0 : if (! print_unresolved_addresses)
9387 : : {
9388 : 0 : Dwarf_Addr addr1;
9389 : 0 : Dwarf_Addr addr2;
9390 [ # # ]: 0 : if (get_indexed_addr (cu, op1, &addr1) != 0
9391 [ # # ]: 0 : || get_indexed_addr (cu, op2, &addr2) != 0)
9392 : : {
9393 : 0 : printf (" ???..\n");
9394 : 0 : printf (" ???\n");
9395 : : }
9396 : : else
9397 : : {
9398 : 0 : printf (" ");
9399 : 0 : print_dwarf_addr (dwflmod, address_size, addr1, addr1);
9400 : 0 : printf ("..\n ");
9401 : 0 : print_dwarf_addr (dwflmod, address_size,
9402 : : addr2 - 1, addr2);
9403 : 0 : printf ("\n");
9404 : : }
9405 : : }
9406 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
9407 : 0 : goto invalid_entry;
9408 : 0 : get_uleb128 (len, readp, nexthdr);
9409 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < len)
9410 : 0 : goto invalid_entry;
9411 : 0 : print_ops (dwflmod, dbg, 8, 8, version,
9412 : : address_size, offset_size, cu, len, readp);
9413 : 0 : readp += len;
9414 : 0 : break;
9415 : :
9416 : 26 : case DW_LLE_startx_length:
9417 [ - + ]: 26 : if ((uint64_t) (nexthdr - readp) < 1)
9418 : 0 : goto invalid_entry;
9419 : 26 : get_uleb128 (op1, readp, nexthdr);
9420 [ - + ]: 26 : if ((uint64_t) (nexthdr - readp) < 1)
9421 : 0 : goto invalid_entry;
9422 : 26 : get_uleb128 (op2, readp, nexthdr);
9423 : 26 : printf (" %" PRIx64 ", %" PRIx64 "\n", op1, op2);
9424 [ + - ]: 26 : if (! print_unresolved_addresses)
9425 : : {
9426 : 26 : Dwarf_Addr addr1;
9427 : 26 : Dwarf_Addr addr2;
9428 [ - + ]: 26 : if (get_indexed_addr (cu, op1, &addr1) != 0)
9429 : : {
9430 : 0 : printf (" ???..\n");
9431 : 0 : printf (" ???\n");
9432 : : }
9433 : : else
9434 : : {
9435 : 26 : addr2 = addr1 + op2;
9436 : 26 : printf (" ");
9437 : 26 : print_dwarf_addr (dwflmod, address_size, addr1, addr1);
9438 : 26 : printf ("..\n ");
9439 : 26 : print_dwarf_addr (dwflmod, address_size,
9440 : : addr2 - 1, addr2);
9441 : 26 : printf ("\n");
9442 : : }
9443 : : }
9444 [ - + ]: 26 : if ((uint64_t) (nexthdr - readp) < 1)
9445 : 0 : goto invalid_entry;
9446 : 26 : get_uleb128 (len, readp, nexthdr);
9447 [ - + ]: 26 : if ((uint64_t) (nexthdr - readp) < len)
9448 : 0 : goto invalid_entry;
9449 : 26 : print_ops (dwflmod, dbg, 8, 8, version,
9450 : : address_size, offset_size, cu, len, readp);
9451 : 26 : readp += len;
9452 : 26 : break;
9453 : :
9454 : 24 : case DW_LLE_offset_pair:
9455 [ - + ]: 24 : if ((uint64_t) (nexthdr - readp) < 1)
9456 : 0 : goto invalid_entry;
9457 : 24 : get_uleb128 (op1, readp, nexthdr);
9458 [ - + ]: 24 : if ((uint64_t) (nexthdr - readp) < 1)
9459 : 0 : goto invalid_entry;
9460 : 24 : get_uleb128 (op2, readp, nexthdr);
9461 : 24 : printf (" %" PRIx64 ", %" PRIx64 "\n", op1, op2);
9462 [ + - ]: 24 : if (! print_unresolved_addresses)
9463 : : {
9464 : 24 : op1 += base;
9465 : 24 : op2 += base;
9466 : 24 : printf (" ");
9467 : 24 : print_dwarf_addr (dwflmod, address_size, op1, op1);
9468 : 24 : printf ("..\n ");
9469 : 24 : print_dwarf_addr (dwflmod, address_size, op2 - 1, op2);
9470 : 24 : printf ("\n");
9471 : : }
9472 [ - + ]: 24 : if ((uint64_t) (nexthdr - readp) < 1)
9473 : 0 : goto invalid_entry;
9474 : 24 : get_uleb128 (len, readp, nexthdr);
9475 [ - + ]: 24 : if ((uint64_t) (nexthdr - readp) < len)
9476 : 0 : goto invalid_entry;
9477 : 24 : print_ops (dwflmod, dbg, 8, 8, version,
9478 : : address_size, offset_size, cu, len, readp);
9479 : 24 : readp += len;
9480 : 24 : break;
9481 : :
9482 : 0 : case DW_LLE_default_location:
9483 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
9484 : 0 : goto invalid_entry;
9485 : 0 : get_uleb128 (len, readp, nexthdr);
9486 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < len)
9487 : 0 : goto invalid_entry;
9488 : 0 : print_ops (dwflmod, dbg, 8, 8, version,
9489 : : address_size, offset_size, cu, len, readp);
9490 : 0 : readp += len;
9491 : 0 : break;
9492 : :
9493 : 5 : case DW_LLE_base_address:
9494 [ - + ]: 5 : if (address_size == 4)
9495 : : {
9496 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 4)
9497 : 0 : goto invalid_entry;
9498 [ # # ]: 0 : op1 = read_4ubyte_unaligned_inc (dbg, readp);
9499 : : }
9500 : : else
9501 : : {
9502 [ - + ]: 5 : if ((uint64_t) (nexthdr - readp) < 8)
9503 : 0 : goto invalid_entry;
9504 [ - + ]: 10 : op1 = read_8ubyte_unaligned_inc (dbg, readp);
9505 : : }
9506 : 5 : base = op1;
9507 : 5 : printf (" 0x%" PRIx64 "\n", base);
9508 [ + - ]: 5 : if (! print_unresolved_addresses)
9509 : : {
9510 : 5 : printf (" ");
9511 : 5 : print_dwarf_addr (dwflmod, address_size, base, base);
9512 [ + + ]: 102 : printf ("\n");
9513 : : }
9514 : : break;
9515 : :
9516 : 0 : case DW_LLE_start_end:
9517 [ # # ]: 0 : if (address_size == 4)
9518 : : {
9519 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 8)
9520 : 0 : goto invalid_entry;
9521 [ # # ]: 0 : op1 = read_4ubyte_unaligned_inc (dbg, readp);
9522 [ # # ]: 0 : op2 = read_4ubyte_unaligned_inc (dbg, readp);
9523 : : }
9524 : : else
9525 : : {
9526 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 16)
9527 : 0 : goto invalid_entry;
9528 [ # # ]: 0 : op1 = read_8ubyte_unaligned_inc (dbg, readp);
9529 [ # # ]: 0 : op2 = read_8ubyte_unaligned_inc (dbg, readp);
9530 : : }
9531 : 0 : printf (" 0x%" PRIx64 "..0x%" PRIx64 "\n", op1, op2);
9532 [ # # ]: 0 : if (! print_unresolved_addresses)
9533 : : {
9534 : 0 : printf (" ");
9535 : 0 : print_dwarf_addr (dwflmod, address_size, op1, op1);
9536 : 0 : printf ("..\n ");
9537 : 0 : print_dwarf_addr (dwflmod, address_size, op2 - 1, op2);
9538 : 0 : printf ("\n");
9539 : : }
9540 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
9541 : 0 : goto invalid_entry;
9542 : 0 : get_uleb128 (len, readp, nexthdr);
9543 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < len)
9544 : 0 : goto invalid_entry;
9545 : 0 : print_ops (dwflmod, dbg, 8, 8, version,
9546 : : address_size, offset_size, cu, len, readp);
9547 : 0 : readp += len;
9548 : 0 : break;
9549 : :
9550 : 3 : case DW_LLE_start_length:
9551 [ - + ]: 3 : if (address_size == 4)
9552 : : {
9553 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 4)
9554 : 0 : goto invalid_entry;
9555 [ # # ]: 0 : op1 = read_4ubyte_unaligned_inc (dbg, readp);
9556 : : }
9557 : : else
9558 : : {
9559 [ - + ]: 3 : if ((uint64_t) (nexthdr - readp) < 8)
9560 : 0 : goto invalid_entry;
9561 [ - + ]: 6 : op1 = read_8ubyte_unaligned_inc (dbg, readp);
9562 : : }
9563 [ - + ]: 3 : if ((uint64_t) (nexthdr - readp) < 1)
9564 : 0 : goto invalid_entry;
9565 : 3 : get_uleb128 (op2, readp, nexthdr);
9566 : 3 : printf (" 0x%" PRIx64 ", %" PRIx64 "\n", op1, op2);
9567 [ + + ]: 3 : if (! print_unresolved_addresses)
9568 : : {
9569 : 2 : op2 = op1 + op2;
9570 : 2 : printf (" ");
9571 : 2 : print_dwarf_addr (dwflmod, address_size, op1, op1);
9572 : 2 : printf ("..\n ");
9573 : 2 : print_dwarf_addr (dwflmod, address_size, op2 - 1, op2);
9574 : 2 : printf ("\n");
9575 : : }
9576 [ - + ]: 3 : if ((uint64_t) (nexthdr - readp) < 1)
9577 : 0 : goto invalid_entry;
9578 : 3 : get_uleb128 (len, readp, nexthdr);
9579 [ - + ]: 3 : if ((uint64_t) (nexthdr - readp) < len)
9580 : 0 : goto invalid_entry;
9581 : 3 : print_ops (dwflmod, dbg, 8, 8, version,
9582 : : address_size, offset_size, cu, len, readp);
9583 : 3 : readp += len;
9584 : 3 : break;
9585 : :
9586 : 0 : default:
9587 : 0 : goto invalid_entry;
9588 : : }
9589 : : }
9590 : :
9591 : 5 : next_table:
9592 [ - + ]: 5 : if (readp != nexthdr)
9593 : : {
9594 : 0 : size_t padding = nexthdr - readp;
9595 : 0 : printf (_(" %zu padding bytes\n\n"), padding);
9596 : 0 : readp = nexthdr;
9597 : : }
9598 : : }
9599 : : }
9600 : :
9601 : :
9602 : : static void
9603 : 42 : print_debug_loc_section (Dwfl_Module *dwflmod,
9604 : : Ebl *ebl, GElf_Ehdr *ehdr,
9605 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
9606 : : {
9607 : 84 : Elf_Data *data = (dbg->sectiondata[IDX_debug_loc]
9608 [ - + ]: 42 : ?: elf_rawdata (scn, NULL));
9609 : :
9610 [ - + ]: 42 : if (unlikely (data == NULL))
9611 : : {
9612 : 0 : error (0, 0, _("cannot get .debug_loc content: %s"),
9613 : : elf_errmsg (-1));
9614 : 0 : return;
9615 : : }
9616 : :
9617 : 42 : printf (_("\
9618 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
9619 : : elf_ndxscn (scn), section_name (ebl, shdr),
9620 : 42 : (uint64_t) shdr->sh_offset);
9621 : :
9622 : 42 : sort_listptr (&known_locsptr, "loclistptr");
9623 : 42 : size_t listptr_idx = 0;
9624 : :
9625 [ + - ]: 42 : uint_fast8_t address_size = ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 4 : 8;
9626 : 42 : uint_fast8_t offset_size = 4;
9627 : :
9628 : 42 : bool first = true;
9629 : 42 : Dwarf_Addr base = 0;
9630 : 42 : unsigned char *readp = data->d_buf;
9631 : 42 : unsigned char *const endp = (unsigned char *) data->d_buf + data->d_size;
9632 : 42 : Dwarf_CU *last_cu = NULL;
9633 [ + + ]: 384049 : while (readp < endp)
9634 : : {
9635 : 384007 : ptrdiff_t offset = readp - (unsigned char *) data->d_buf;
9636 : 384007 : Dwarf_CU *cu = last_cu;
9637 : 384007 : unsigned int attr = 0;
9638 : :
9639 [ + + - + ]: 384007 : if (first && skip_listptr_hole (&known_locsptr, &listptr_idx,
9640 : : &address_size, &offset_size, &base,
9641 : : &cu, offset, &readp, endp, &attr))
9642 : 53836 : continue;
9643 : :
9644 [ + + ]: 384007 : if (last_cu != cu)
9645 : : {
9646 : 1603 : Dwarf_Die cudie;
9647 [ - + ]: 1603 : if (dwarf_cu_die (cu, &cudie,
9648 : : NULL, NULL, NULL, NULL,
9649 : : NULL, NULL) == NULL)
9650 : 0 : printf (_("\n Unknown CU base: "));
9651 : : else
9652 : 1603 : printf (_("\n CU [%6" PRIx64 "] base: "),
9653 : : dwarf_dieoffset (&cudie));
9654 : 1603 : print_dwarf_addr (dwflmod, address_size, base, base);
9655 : 1603 : printf ("\n");
9656 : : }
9657 : 384007 : last_cu = cu;
9658 : :
9659 [ + + ]: 384007 : if (attr == DW_AT_GNU_locviews)
9660 : : {
9661 : 53836 : Dwarf_Off next_off = next_listptr_offset (&known_locsptr,
9662 : : &listptr_idx, offset);
9663 : 53836 : const unsigned char *locp = readp;
9664 : 53836 : const unsigned char *locendp;
9665 [ + - ]: 53836 : if (next_off == 0
9666 : 53836 : || next_off > (size_t) (endp
9667 [ + - ]: 53836 : - (const unsigned char *) data->d_buf))
9668 : 53836 : locendp = endp;
9669 : : else
9670 : 53836 : locendp = (const unsigned char *) data->d_buf + next_off;
9671 : :
9672 [ + + ]: 329952 : while (locp < locendp)
9673 : : {
9674 : 276116 : uint64_t v1, v2;
9675 : 276116 : get_uleb128 (v1, locp, locendp);
9676 [ - + ]: 276116 : if (locp >= locendp)
9677 : : {
9678 : 0 : printf (_(" [%6tx] <INVALID DATA>\n"), offset);
9679 : : break;
9680 : : }
9681 : 276116 : get_uleb128 (v2, locp, locendp);
9682 [ + + ]: 276116 : if (first) /* First view pair in a list. */
9683 : 53836 : printf (" [%6tx] ", offset);
9684 : : else
9685 : 222280 : printf (" ");
9686 : 276116 : printf ("view pair %" PRId64 ", %" PRId64 "\n", v1, v2);
9687 : 276116 : first = false;
9688 : : }
9689 : :
9690 : 53836 : first = true;
9691 : 53836 : readp = (unsigned char *) locendp;
9692 : 53836 : continue;
9693 : : }
9694 : :
9695 : : /* GNU DebugFission encoded addresses as addrx. */
9696 : 660342 : bool is_debugfission = ((cu != NULL
9697 [ # # ]: 0 : || split_dwarf_cu_base (dbg, &cu, &base))
9698 [ - + + - ]: 330171 : && (cu->version < 5
9699 [ + + ]: 330171 : && cu->unit_type == DW_UT_split_compile));
9700 [ + + ]: 330171 : if (!is_debugfission
9701 [ - + ]: 330111 : && unlikely (data->d_size - offset < (size_t) address_size * 2))
9702 : : {
9703 : 0 : invalid_data:
9704 : 0 : printf (_(" [%6tx] <INVALID DATA>\n"), offset);
9705 : 0 : break;
9706 : : }
9707 : :
9708 : 330171 : Dwarf_Addr begin;
9709 : 330171 : Dwarf_Addr end;
9710 : 330171 : bool use_base = true;
9711 [ + + ]: 330171 : if (is_debugfission)
9712 : : {
9713 : 60 : const unsigned char *locp = readp;
9714 : 60 : const unsigned char *locendp = readp + data->d_size;
9715 [ - + ]: 60 : if (locp >= locendp)
9716 : 0 : goto invalid_data;
9717 : :
9718 : 60 : Dwarf_Word idx;
9719 : 60 : unsigned char code = *locp++;
9720 [ + - - + : 60 : switch (code)
- ]
9721 : : {
9722 : 20 : case DW_LLE_GNU_end_of_list_entry:
9723 : 20 : begin = 0;
9724 : 20 : end = 0;
9725 : 20 : break;
9726 : :
9727 : 0 : case DW_LLE_GNU_base_address_selection_entry:
9728 [ # # ]: 0 : if (locp >= locendp)
9729 : 0 : goto invalid_data;
9730 : 0 : begin = (Dwarf_Addr) -1;
9731 : 0 : get_uleb128 (idx, locp, locendp);
9732 [ # # ]: 0 : if (get_indexed_addr (cu, idx, &end) != 0)
9733 : 0 : end = idx; /* ... */
9734 : : break;
9735 : :
9736 : 0 : case DW_LLE_GNU_start_end_entry:
9737 [ # # ]: 0 : if (locp >= locendp)
9738 : 0 : goto invalid_data;
9739 : 0 : get_uleb128 (idx, locp, locendp);
9740 [ # # ]: 0 : if (get_indexed_addr (cu, idx, &begin) != 0)
9741 : 0 : begin = idx; /* ... */
9742 [ # # ]: 0 : if (locp >= locendp)
9743 : 0 : goto invalid_data;
9744 : 0 : get_uleb128 (idx, locp, locendp);
9745 [ # # ]: 0 : if (get_indexed_addr (cu, idx, &end) != 0)
9746 : 0 : end = idx; /* ... */
9747 : : use_base = false;
9748 : : break;
9749 : :
9750 : 40 : case DW_LLE_GNU_start_length_entry:
9751 [ - + ]: 40 : if (locp >= locendp)
9752 : 0 : goto invalid_data;
9753 : 40 : get_uleb128 (idx, locp, locendp);
9754 [ - + ]: 40 : if (get_indexed_addr (cu, idx, &begin) != 0)
9755 : 0 : begin = idx; /* ... */
9756 [ - + ]: 40 : if (locendp - locp < 4)
9757 : 0 : goto invalid_data;
9758 [ - + ]: 40 : end = read_4ubyte_unaligned_inc (dbg, locp);
9759 : 40 : end += begin;
9760 : 40 : use_base = false;
9761 : 40 : break;
9762 : :
9763 : 0 : default:
9764 : 0 : goto invalid_data;
9765 : : }
9766 : :
9767 : 60 : readp = (unsigned char *) locp;
9768 : : }
9769 [ + - ]: 330111 : else if (address_size == 8)
9770 : : {
9771 [ + + ]: 330111 : begin = read_8ubyte_unaligned_inc (dbg, readp);
9772 [ + + ]: 660222 : end = read_8ubyte_unaligned_inc (dbg, readp);
9773 : : }
9774 : : else
9775 : : {
9776 [ # # ]: 0 : begin = read_4ubyte_unaligned_inc (dbg, readp);
9777 [ # # ]: 0 : end = read_4ubyte_unaligned_inc (dbg, readp);
9778 [ # # ]: 0 : if (begin == (Dwarf_Addr) (uint32_t) -1)
9779 : 0 : begin = (Dwarf_Addr) -1l;
9780 : : }
9781 : :
9782 [ - + ]: 330171 : if (begin == (Dwarf_Addr) -1l) /* Base address entry. */
9783 : : {
9784 [ # # ]: 0 : if (first)
9785 : 0 : printf (" [%6tx] ", offset);
9786 : : else
9787 : 0 : printf (" ");
9788 : 0 : puts (_("base address"));
9789 : 0 : printf (" ");
9790 : 0 : print_dwarf_addr (dwflmod, address_size, end, end);
9791 : 0 : printf ("\n");
9792 : 0 : base = end;
9793 : 0 : first = false;
9794 : : }
9795 [ + + + + ]: 330171 : else if (begin == 0 && end == 0) /* End of list entry. */
9796 : : {
9797 [ - + ]: 53910 : if (first)
9798 : 330171 : printf (_(" [%6tx] empty list\n"), offset);
9799 : : first = true;
9800 : : }
9801 : : else
9802 : : {
9803 : : /* We have a location expression entry. */
9804 [ + + ]: 276261 : uint_fast16_t len = read_2ubyte_unaligned_inc (dbg, readp);
9805 : :
9806 [ + + ]: 276261 : if (first) /* First entry in a list. */
9807 : 53910 : printf (" [%6tx] ", offset);
9808 : : else
9809 : 222351 : printf (" ");
9810 : :
9811 : 276261 : printf ("range %" PRIx64 ", %" PRIx64 "\n", begin, end);
9812 [ + + ]: 276261 : if (! print_unresolved_addresses)
9813 : : {
9814 [ + + ]: 276231 : Dwarf_Addr dab = use_base ? base + begin : begin;
9815 [ + + ]: 276231 : Dwarf_Addr dae = use_base ? base + end : end;
9816 : 276231 : printf (" ");
9817 : 276231 : print_dwarf_addr (dwflmod, address_size, dab, dab);
9818 : 276231 : printf ("..\n ");
9819 : 276231 : print_dwarf_addr (dwflmod, address_size, dae - 1, dae);
9820 : 276231 : printf ("\n");
9821 : : }
9822 : :
9823 [ - + ]: 276261 : if (endp - readp <= (ptrdiff_t) len)
9824 : : {
9825 : 0 : fputs (_(" <INVALID DATA>\n"), stdout);
9826 : 0 : break;
9827 : : }
9828 : :
9829 : 276261 : print_ops (dwflmod, dbg, 11, 11,
9830 [ + - ]: 276261 : cu != NULL ? cu->version : 3,
9831 : : address_size, offset_size, cu, len, readp);
9832 : :
9833 : 276261 : first = false;
9834 : 276261 : readp += len;
9835 : : }
9836 : : }
9837 : : }
9838 : :
9839 : : struct mac_culist
9840 : : {
9841 : : Dwarf_Die die;
9842 : : Dwarf_Off offset;
9843 : : Dwarf_Files *files;
9844 : : struct mac_culist *next;
9845 : : };
9846 : :
9847 : :
9848 : : static int
9849 : 0 : mac_compare (const void *p1, const void *p2)
9850 : : {
9851 : 0 : struct mac_culist *m1 = (struct mac_culist *) p1;
9852 : 0 : struct mac_culist *m2 = (struct mac_culist *) p2;
9853 : :
9854 [ # # ]: 0 : if (m1->offset < m2->offset)
9855 : : return -1;
9856 [ # # ]: 0 : if (m1->offset > m2->offset)
9857 : 0 : return 1;
9858 : : return 0;
9859 : : }
9860 : :
9861 : :
9862 : : static void
9863 : 0 : print_debug_macinfo_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
9864 : : Ebl *ebl,
9865 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
9866 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
9867 : : {
9868 : 0 : printf (_("\
9869 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
9870 : : elf_ndxscn (scn), section_name (ebl, shdr),
9871 : 0 : (uint64_t) shdr->sh_offset);
9872 [ # # ]: 0 : putc_unlocked ('\n', stdout);
9873 : :
9874 : : /* There is no function in libdw to iterate over the raw content of
9875 : : the section but it is easy enough to do. */
9876 : 0 : Elf_Data *data = (dbg->sectiondata[IDX_debug_macinfo]
9877 [ # # ]: 0 : ?: elf_rawdata (scn, NULL));
9878 [ # # ]: 0 : if (unlikely (data == NULL))
9879 : : {
9880 : 0 : error (0, 0, _("cannot get macro information section data: %s"),
9881 : : elf_errmsg (-1));
9882 : 0 : return;
9883 : : }
9884 : :
9885 : : /* Get the source file information for all CUs. */
9886 : 0 : Dwarf_Off offset;
9887 : 0 : Dwarf_Off ncu = 0;
9888 : 0 : size_t hsize;
9889 : 0 : struct mac_culist *culist = NULL;
9890 : 0 : size_t nculist = 0;
9891 [ # # ]: 0 : while (dwarf_nextcu (dbg, offset = ncu, &ncu, &hsize, NULL, NULL, NULL) == 0)
9892 : : {
9893 : 0 : Dwarf_Die cudie;
9894 [ # # ]: 0 : if (dwarf_offdie (dbg, offset + hsize, &cudie) == NULL)
9895 : 0 : continue;
9896 : :
9897 : 0 : Dwarf_Attribute attr;
9898 [ # # ]: 0 : if (dwarf_attr (&cudie, DW_AT_macro_info, &attr) == NULL)
9899 : 0 : continue;
9900 : :
9901 : 0 : Dwarf_Word macoff;
9902 [ # # ]: 0 : if (dwarf_formudata (&attr, &macoff) != 0)
9903 : 0 : continue;
9904 : :
9905 : 0 : struct mac_culist *newp = (struct mac_culist *) alloca (sizeof (*newp));
9906 : 0 : newp->die = cudie;
9907 : 0 : newp->offset = macoff;
9908 : 0 : newp->files = NULL;
9909 : 0 : newp->next = culist;
9910 : 0 : culist = newp;
9911 : 0 : ++nculist;
9912 : : }
9913 : :
9914 : : /* Convert the list into an array for easier consumption. */
9915 : 0 : struct mac_culist *cus = (struct mac_culist *) alloca ((nculist + 1)
9916 : : * sizeof (*cus));
9917 : : /* Add sentinel. */
9918 : 0 : cus[nculist].offset = data->d_size;
9919 : 0 : cus[nculist].files = (Dwarf_Files *) -1l;
9920 [ # # ]: 0 : if (nculist > 0)
9921 : : {
9922 [ # # ]: 0 : for (size_t cnt = nculist - 1; culist != NULL; --cnt)
9923 : : {
9924 [ # # ]: 0 : assert (cnt < nculist);
9925 : 0 : cus[cnt] = *culist;
9926 : 0 : culist = culist->next;
9927 : : }
9928 : :
9929 : : /* Sort the array according to the offset in the .debug_macinfo
9930 : : section. Note we keep the sentinel at the end. */
9931 : 0 : qsort (cus, nculist, sizeof (*cus), mac_compare);
9932 : : }
9933 : :
9934 : 0 : const unsigned char *readp = (const unsigned char *) data->d_buf;
9935 : 0 : const unsigned char *readendp = readp + data->d_size;
9936 : 0 : int level = 1;
9937 : :
9938 : 0 : while (readp < readendp)
9939 : : {
9940 : 0 : unsigned int opcode = *readp++;
9941 : 0 : unsigned int u128;
9942 : 0 : unsigned int u128_2;
9943 : 0 : const unsigned char *endp;
9944 : :
9945 [ # # # # ]: 0 : switch (opcode)
9946 : : {
9947 : 0 : case DW_MACINFO_define:
9948 : : case DW_MACINFO_undef:
9949 : : case DW_MACINFO_vendor_ext:
9950 : : /* For the first two opcodes the parameters are
9951 : : line, string
9952 : : For the latter
9953 : : number, string.
9954 : : We can treat these cases together. */
9955 : 0 : get_uleb128 (u128, readp, readendp);
9956 : :
9957 : 0 : endp = memchr (readp, '\0', readendp - readp);
9958 [ # # ]: 0 : if (unlikely (endp == NULL))
9959 : : {
9960 : 0 : printf (_("\
9961 : : %*s*** non-terminated string at end of section"),
9962 : : level, "");
9963 : 0 : return;
9964 : : }
9965 : :
9966 [ # # ]: 0 : if (opcode == DW_MACINFO_define)
9967 : 0 : printf ("%*s#define %s, line %u\n",
9968 : : level, "", (char *) readp, u128);
9969 [ # # ]: 0 : else if (opcode == DW_MACINFO_undef)
9970 : 0 : printf ("%*s#undef %s, line %u\n",
9971 : : level, "", (char *) readp, u128);
9972 : : else
9973 : 0 : printf (" #vendor-ext %s, number %u\n", (char *) readp, u128);
9974 : :
9975 : 0 : readp = endp + 1;
9976 : 0 : break;
9977 : :
9978 : 0 : case DW_MACINFO_start_file:
9979 : : /* The two parameters are line and file index, in this order. */
9980 : 0 : get_uleb128 (u128, readp, readendp);
9981 [ # # ]: 0 : if (readendp - readp < 1)
9982 : : {
9983 : 0 : printf (_("\
9984 : : %*s*** missing DW_MACINFO_start_file argument at end of section"),
9985 : : level, "");
9986 : 0 : return;
9987 : : }
9988 : 0 : get_uleb128 (u128_2, readp, readendp);
9989 : :
9990 : : /* Find the CU DIE for this file. */
9991 : 0 : size_t macoff = readp - (const unsigned char *) data->d_buf;
9992 : 0 : const char *fname = "???";
9993 [ # # # # ]: 0 : if (macoff >= cus[0].offset && cus[0].offset != data->d_size)
9994 : : {
9995 [ # # # # ]: 0 : while (macoff >= cus[1].offset && cus[1].offset != data->d_size)
9996 : 0 : ++cus;
9997 : :
9998 [ # # ]: 0 : if (cus[0].files == NULL
9999 [ # # ]: 0 : && dwarf_getsrcfiles (&cus[0].die, &cus[0].files, NULL) != 0)
10000 : 0 : cus[0].files = (Dwarf_Files *) -1l;
10001 : :
10002 [ # # ]: 0 : if (cus[0].files != (Dwarf_Files *) -1l)
10003 : 0 : fname = (dwarf_filesrc (cus[0].files, u128_2, NULL, NULL)
10004 [ # # ]: 0 : ?: "???");
10005 : : }
10006 : :
10007 : 0 : printf ("%*sstart_file %u, [%u] %s\n",
10008 : : level, "", u128, u128_2, fname);
10009 : 0 : ++level;
10010 : 0 : break;
10011 : :
10012 : 0 : case DW_MACINFO_end_file:
10013 : 0 : --level;
10014 : 0 : printf ("%*send_file\n", level, "");
10015 : : /* Nothing more to do. */
10016 : : break;
10017 : :
10018 : 0 : default:
10019 : : // XXX gcc seems to generate files with a trailing zero.
10020 [ # # # # ]: 0 : if (unlikely (opcode != 0 || readp != readendp))
10021 [ # # ]: 0 : printf ("%*s*** invalid opcode %u\n", level, "", opcode);
10022 : : break;
10023 : : }
10024 : : }
10025 : : }
10026 : :
10027 : :
10028 : : static void
10029 : 3 : print_debug_macro_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
10030 : : Ebl *ebl,
10031 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
10032 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
10033 : : {
10034 : 3 : printf (_("\
10035 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
10036 : : elf_ndxscn (scn), section_name (ebl, shdr),
10037 : 3 : (uint64_t) shdr->sh_offset);
10038 [ - + ]: 3 : putc_unlocked ('\n', stdout);
10039 : :
10040 : 3 : Elf_Data *data = elf_getdata (scn, NULL);
10041 [ - + ]: 3 : if (unlikely (data == NULL))
10042 : : {
10043 : 0 : error (0, 0, _("cannot get macro information section data: %s"),
10044 : : elf_errmsg (-1));
10045 : 0 : return;
10046 : : }
10047 : :
10048 : : /* Get the source file information for all CUs. Uses same
10049 : : datastructure as macinfo. But uses offset field to directly
10050 : : match .debug_line offset. And just stored in a list. */
10051 : 3 : Dwarf_Off offset;
10052 : 3 : Dwarf_Off ncu = 0;
10053 : 3 : size_t hsize;
10054 : 3 : struct mac_culist *culist = NULL;
10055 : 3 : size_t nculist = 0;
10056 [ + + ]: 7 : while (dwarf_nextcu (dbg, offset = ncu, &ncu, &hsize, NULL, NULL, NULL) == 0)
10057 : : {
10058 : 4 : Dwarf_Die cudie;
10059 [ - + ]: 4 : if (dwarf_offdie (dbg, offset + hsize, &cudie) == NULL)
10060 : 0 : continue;
10061 : :
10062 : 4 : Dwarf_Attribute attr;
10063 [ - + ]: 4 : if (dwarf_attr (&cudie, DW_AT_stmt_list, &attr) == NULL)
10064 : 0 : continue;
10065 : :
10066 : 4 : Dwarf_Word lineoff;
10067 [ - + ]: 4 : if (dwarf_formudata (&attr, &lineoff) != 0)
10068 : 0 : continue;
10069 : :
10070 : 4 : struct mac_culist *newp = (struct mac_culist *) alloca (sizeof (*newp));
10071 : 4 : newp->die = cudie;
10072 : 4 : newp->offset = lineoff;
10073 : 4 : newp->files = NULL;
10074 : 4 : newp->next = culist;
10075 : 4 : culist = newp;
10076 : 4 : ++nculist;
10077 : : }
10078 : :
10079 : 3 : const unsigned char *readp = (const unsigned char *) data->d_buf;
10080 : 3 : const unsigned char *readendp = readp + data->d_size;
10081 : :
10082 [ + + ]: 11 : while (readp < readendp)
10083 : : {
10084 : 16 : printf (_(" Offset: 0x%" PRIx64 "\n"),
10085 : 8 : (uint64_t) (readp - (const unsigned char *) data->d_buf));
10086 : :
10087 : : // Header, 2 byte version, 1 byte flag, optional .debug_line offset,
10088 : : // optional vendor extension macro entry table.
10089 [ - + ]: 8 : if (readp + 2 > readendp)
10090 : : {
10091 : 0 : invalid_data:
10092 : 0 : error (0, 0, _("invalid data"));
10093 : 0 : return;
10094 : : }
10095 [ - + ]: 8 : const uint16_t vers = read_2ubyte_unaligned_inc (dbg, readp);
10096 : 8 : printf (_(" Version: %" PRIu16 "\n"), vers);
10097 : :
10098 : : // Version 4 is the GNU extension for DWARF4. DWARF5 will use version
10099 : : // 5 when it gets standardized.
10100 [ - + ]: 8 : if (vers != 4 && vers != 5)
10101 : : {
10102 : 0 : printf (_(" unknown version, cannot parse section\n"));
10103 : 0 : return;
10104 : : }
10105 : :
10106 [ - + ]: 8 : if (readp + 1 > readendp)
10107 : 0 : goto invalid_data;
10108 : 8 : const unsigned char flag = *readp++;
10109 : 8 : printf (_(" Flag: 0x%" PRIx8), flag);
10110 [ + + ]: 8 : if (flag != 0)
10111 : : {
10112 : 4 : printf (" (");
10113 [ - + ]: 4 : if ((flag & 0x01) != 0)
10114 : : {
10115 : 0 : printf ("offset_size");
10116 [ # # ]: 0 : if ((flag & 0xFE) != 0)
10117 : 0 : printf (", ");
10118 : : }
10119 [ + - ]: 4 : if ((flag & 0x02) != 0)
10120 : : {
10121 : 4 : printf ("debug_line_offset");
10122 [ - + ]: 4 : if ((flag & 0xFC) != 0)
10123 : 0 : printf (", ");
10124 : : }
10125 [ - + ]: 4 : if ((flag & 0x04) != 0)
10126 : : {
10127 : 0 : printf ("operands_table");
10128 [ # # ]: 0 : if ((flag & 0xF8) != 0)
10129 : 0 : printf (", ");
10130 : : }
10131 [ - + ]: 4 : if ((flag & 0xF8) != 0)
10132 : 0 : printf ("unknown");
10133 : 4 : printf (")");
10134 : : }
10135 : 8 : printf ("\n");
10136 : :
10137 [ + - ]: 8 : unsigned int offset_len = (flag & 0x01) ? 8 : 4;
10138 : 8 : printf (_(" Offset length: %" PRIu8 "\n"), offset_len);
10139 : 8 : Dwarf_Off line_offset = -1;
10140 [ + + ]: 8 : if (flag & 0x02)
10141 : : {
10142 [ - + ]: 4 : if (offset_len == 8)
10143 [ # # ]: 0 : line_offset = read_8ubyte_unaligned_inc (dbg, readp);
10144 : : else
10145 [ - + ]: 8 : line_offset = read_4ubyte_unaligned_inc (dbg, readp);
10146 : 4 : printf (_(" .debug_line offset: 0x%" PRIx64 "\n"),
10147 : : line_offset);
10148 : : }
10149 : :
10150 : 4 : struct mac_culist *cu = NULL;
10151 [ + - ]: 4 : if (line_offset != (Dwarf_Off) -1)
10152 : : {
10153 : : cu = culist;
10154 [ + - + + ]: 5 : while (cu != NULL && line_offset != cu->offset)
10155 : 1 : cu = cu->next;
10156 : : }
10157 : :
10158 [ + + ]: 8 : Dwarf_Off str_offsets_base = str_offsets_base_off (dbg, (cu != NULL
10159 : : ? cu->die.cu
10160 : : : NULL));
10161 : :
10162 : 8 : const unsigned char *vendor[DW_MACRO_hi_user - DW_MACRO_lo_user + 1];
10163 [ - + ]: 8 : memset (vendor, 0, sizeof vendor);
10164 [ - + ]: 8 : if (flag & 0x04)
10165 : : {
10166 : : // 1 byte length, for each item, 1 byte opcode, uleb128 number
10167 : : // of arguments, for each argument 1 byte form code.
10168 [ # # ]: 0 : if (readp + 1 > readendp)
10169 : 0 : goto invalid_data;
10170 : 0 : unsigned int tlen = *readp++;
10171 : 0 : printf (_(" extension opcode table, %" PRIu8 " items:\n"),
10172 : : tlen);
10173 [ # # ]: 0 : for (unsigned int i = 0; i < tlen; i++)
10174 : : {
10175 [ # # ]: 0 : if (readp + 1 > readendp)
10176 : 0 : goto invalid_data;
10177 : 0 : unsigned int opcode = *readp++;
10178 : 0 : printf (_(" [%" PRIx8 "]"), opcode);
10179 : 0 : if (opcode < DW_MACRO_lo_user
10180 [ # # ]: 0 : || opcode > DW_MACRO_hi_user)
10181 : 0 : goto invalid_data;
10182 : : // Record the start of description for this vendor opcode.
10183 : : // uleb128 nr args, 1 byte per arg form.
10184 : 0 : vendor[opcode - DW_MACRO_lo_user] = readp;
10185 [ # # ]: 0 : if (readp + 1 > readendp)
10186 : 0 : goto invalid_data;
10187 : 0 : unsigned int args = *readp++;
10188 [ # # ]: 0 : if (args > 0)
10189 : : {
10190 : 0 : printf (_(" %" PRIu8 " arguments:"), args);
10191 : 0 : while (args > 0)
10192 : : {
10193 [ # # ]: 0 : if (readp + 1 > readendp)
10194 : 0 : goto invalid_data;
10195 : 0 : unsigned int form = *readp++;
10196 : 0 : printf (" %s", dwarf_form_name (form));
10197 [ # # ]: 0 : if (! libdw_valid_user_form (form))
10198 : 0 : goto invalid_data;
10199 : 0 : args--;
10200 [ # # ]: 0 : if (args > 0)
10201 [ # # # # ]: 0 : putchar_unlocked (',');
10202 : : }
10203 : : }
10204 : : else
10205 : 0 : printf (_(" no arguments."));
10206 [ # # ]: 0 : putchar_unlocked ('\n');
10207 : : }
10208 : : }
10209 [ - + ]: 8 : putchar_unlocked ('\n');
10210 : :
10211 : 8 : int level = 1;
10212 [ - + ]: 8 : if (readp + 1 > readendp)
10213 : 0 : goto invalid_data;
10214 : 8 : unsigned int opcode = *readp++;
10215 : 8 : while (opcode != 0)
10216 : : {
10217 : 727 : unsigned int u128;
10218 : 727 : unsigned int u128_2;
10219 : 727 : const unsigned char *endp;
10220 : 727 : uint64_t off;
10221 : :
10222 [ + + + - : 727 : switch (opcode)
+ + + - -
- - - - ]
10223 : : {
10224 : 6 : case DW_MACRO_start_file:
10225 : 6 : get_uleb128 (u128, readp, readendp);
10226 [ - + ]: 6 : if (readp >= readendp)
10227 : 0 : goto invalid_data;
10228 : 6 : get_uleb128 (u128_2, readp, readendp);
10229 : :
10230 : : /* Find the CU DIE that matches this line offset. */
10231 : 6 : const char *fname = "???";
10232 [ + - ]: 6 : if (cu != NULL)
10233 : : {
10234 [ + + ]: 6 : if (cu->files == NULL
10235 [ - + ]: 4 : && dwarf_getsrcfiles (&cu->die, &cu->files,
10236 : : NULL) != 0)
10237 : 0 : cu->files = (Dwarf_Files *) -1l;
10238 : :
10239 [ + - ]: 6 : if (cu->files != (Dwarf_Files *) -1l)
10240 : 6 : fname = (dwarf_filesrc (cu->files, u128_2,
10241 [ - + ]: 6 : NULL, NULL) ?: "???");
10242 : : }
10243 : 6 : printf ("%*sstart_file %u, [%u] %s\n",
10244 : : level, "", u128, u128_2, fname);
10245 : 6 : ++level;
10246 : 6 : break;
10247 : :
10248 : 6 : case DW_MACRO_end_file:
10249 : 6 : --level;
10250 : 6 : printf ("%*send_file\n", level, "");
10251 : : break;
10252 : :
10253 : 1 : case DW_MACRO_define:
10254 : 1 : get_uleb128 (u128, readp, readendp);
10255 : 1 : endp = memchr (readp, '\0', readendp - readp);
10256 [ - + ]: 1 : if (endp == NULL)
10257 : 0 : goto invalid_data;
10258 : 1 : printf ("%*s#define %s, line %u\n",
10259 : : level, "", readp, u128);
10260 : 1 : readp = endp + 1;
10261 : 1 : break;
10262 : :
10263 : 0 : case DW_MACRO_undef:
10264 : 0 : get_uleb128 (u128, readp, readendp);
10265 : 0 : endp = memchr (readp, '\0', readendp - readp);
10266 [ # # ]: 0 : if (endp == NULL)
10267 : 0 : goto invalid_data;
10268 : 0 : printf ("%*s#undef %s, line %u\n",
10269 : : level, "", readp, u128);
10270 : 0 : readp = endp + 1;
10271 : 0 : break;
10272 : :
10273 : 707 : case DW_MACRO_define_strp:
10274 : 707 : get_uleb128 (u128, readp, readendp);
10275 [ - + ]: 707 : if (readp + offset_len > readendp)
10276 : 0 : goto invalid_data;
10277 [ - + ]: 707 : if (offset_len == 8)
10278 [ # # ]: 0 : off = read_8ubyte_unaligned_inc (dbg, readp);
10279 : : else
10280 [ - + ]: 1414 : off = read_4ubyte_unaligned_inc (dbg, readp);
10281 : 707 : printf ("%*s#define %s, line %u (indirect)\n",
10282 : : level, "", dwarf_getstring (dbg, off, NULL), u128);
10283 : : break;
10284 : :
10285 : 1 : case DW_MACRO_undef_strp:
10286 : 1 : get_uleb128 (u128, readp, readendp);
10287 [ - + ]: 1 : if (readp + offset_len > readendp)
10288 : 0 : goto invalid_data;
10289 [ - + ]: 1 : if (offset_len == 8)
10290 [ # # ]: 0 : off = read_8ubyte_unaligned_inc (dbg, readp);
10291 : : else
10292 [ - + ]: 2 : off = read_4ubyte_unaligned_inc (dbg, readp);
10293 : 1 : printf ("%*s#undef %s, line %u (indirect)\n",
10294 : : level, "", dwarf_getstring (dbg, off, NULL), u128);
10295 : : break;
10296 : :
10297 : 6 : case DW_MACRO_import:
10298 [ - + ]: 6 : if (readp + offset_len > readendp)
10299 : 0 : goto invalid_data;
10300 [ - + ]: 6 : if (offset_len == 8)
10301 [ # # ]: 0 : off = read_8ubyte_unaligned_inc (dbg, readp);
10302 : : else
10303 [ - + ]: 12 : off = read_4ubyte_unaligned_inc (dbg, readp);
10304 : 6 : printf ("%*s#include offset 0x%" PRIx64 "\n",
10305 : : level, "", off);
10306 : : break;
10307 : :
10308 : 0 : case DW_MACRO_define_sup:
10309 : 0 : get_uleb128 (u128, readp, readendp);
10310 [ # # ]: 0 : if (readp + offset_len > readendp)
10311 : 0 : goto invalid_data;
10312 : 0 : printf ("%*s#define ", level, "");
10313 : 0 : readp = print_form_data (dbg, DW_FORM_strp_sup,
10314 : : readp, readendp, offset_len,
10315 : : str_offsets_base);
10316 : 0 : printf (", line %u (sup)\n", u128);
10317 : : break;
10318 : :
10319 : 0 : case DW_MACRO_undef_sup:
10320 : 0 : get_uleb128 (u128, readp, readendp);
10321 [ # # ]: 0 : if (readp + offset_len > readendp)
10322 : 0 : goto invalid_data;
10323 : 0 : printf ("%*s#undef ", level, "");
10324 : 0 : readp = print_form_data (dbg, DW_FORM_strp_sup,
10325 : : readp, readendp, offset_len,
10326 : : str_offsets_base);
10327 : 0 : printf (", line %u (sup)\n", u128);
10328 : : break;
10329 : :
10330 : 0 : case DW_MACRO_import_sup:
10331 [ # # ]: 0 : if (readp + offset_len > readendp)
10332 : 0 : goto invalid_data;
10333 [ # # ]: 0 : if (offset_len == 8)
10334 [ # # ]: 0 : off = read_8ubyte_unaligned_inc (dbg, readp);
10335 : : else
10336 [ # # ]: 0 : off = read_4ubyte_unaligned_inc (dbg, readp);
10337 : : // XXX Needs support for reading from supplementary object file.
10338 : 0 : printf ("%*s#include offset 0x%" PRIx64 " (sup)\n",
10339 : : level, "", off);
10340 : : break;
10341 : :
10342 : 0 : case DW_MACRO_define_strx:
10343 : 0 : get_uleb128 (u128, readp, readendp);
10344 [ # # ]: 0 : if (readp + offset_len > readendp)
10345 : 0 : goto invalid_data;
10346 : 0 : printf ("%*s#define ", level, "");
10347 : 0 : readp = print_form_data (dbg, DW_FORM_strx,
10348 : : readp, readendp, offset_len,
10349 : : str_offsets_base);
10350 : 0 : printf (", line %u (strx)\n", u128);
10351 : : break;
10352 : :
10353 : 0 : case DW_MACRO_undef_strx:
10354 : 0 : get_uleb128 (u128, readp, readendp);
10355 [ # # ]: 0 : if (readp + offset_len > readendp)
10356 : 0 : goto invalid_data;
10357 : 0 : printf ("%*s#undef ", level, "");
10358 : 0 : readp = print_form_data (dbg, DW_FORM_strx,
10359 : : readp, readendp, offset_len,
10360 : : str_offsets_base);
10361 : 0 : printf (", line %u (strx)\n", u128);
10362 : : break;
10363 : :
10364 : : default:
10365 : 0 : printf ("%*svendor opcode 0x%" PRIx8, level, "", opcode);
10366 [ # # ]: 0 : if (opcode < DW_MACRO_lo_user
10367 : : || opcode > DW_MACRO_lo_user
10368 [ # # ]: 0 : || vendor[opcode - DW_MACRO_lo_user] == NULL)
10369 : 0 : goto invalid_data;
10370 : :
10371 : 0 : const unsigned char *op_desc;
10372 : 0 : op_desc = vendor[opcode - DW_MACRO_lo_user];
10373 : :
10374 : : // Just skip the arguments, we cannot really interpret them,
10375 : : // but print as much as we can.
10376 : 0 : unsigned int args = *op_desc++;
10377 [ # # ]: 0 : while (args > 0 && readp < readendp)
10378 : : {
10379 : 0 : unsigned int form = *op_desc++;
10380 : 0 : readp = print_form_data (dbg, form, readp, readendp,
10381 : : offset_len, str_offsets_base);
10382 : 0 : args--;
10383 [ # # ]: 0 : if (args > 0)
10384 [ # # ]: 0 : printf (", ");
10385 : : }
10386 [ # # ]: 0 : putchar_unlocked ('\n');
10387 : : }
10388 : :
10389 [ - + ]: 727 : if (readp + 1 > readendp)
10390 : 0 : goto invalid_data;
10391 : 727 : opcode = *readp++;
10392 [ + + ]: 727 : if (opcode == 0)
10393 [ - + + + ]: 743 : putchar_unlocked ('\n');
10394 : : }
10395 : : }
10396 : : }
10397 : :
10398 : :
10399 : : /* Callback for printing global names. */
10400 : : static int
10401 : 34 : print_pubnames (Dwarf *dbg __attribute__ ((unused)), Dwarf_Global *global,
10402 : : void *arg)
10403 : : {
10404 : 34 : int *np = (int *) arg;
10405 : :
10406 : 68 : printf (_(" [%5d] DIE offset: %6" PRId64
10407 : : ", CU DIE offset: %6" PRId64 ", name: %s\n"),
10408 : 34 : (*np)++, global->die_offset, global->cu_offset, global->name);
10409 : :
10410 : 34 : return 0;
10411 : : }
10412 : :
10413 : :
10414 : : /* Print the known exported symbols in the DWARF section '.debug_pubnames'. */
10415 : : static void
10416 : 8 : print_debug_pubnames_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
10417 : : Ebl *ebl,
10418 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
10419 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
10420 : : {
10421 : 8 : printf (_("\nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
10422 : : elf_ndxscn (scn), section_name (ebl, shdr),
10423 : 8 : (uint64_t) shdr->sh_offset);
10424 : :
10425 : 8 : int n = 0;
10426 : 8 : (void) dwarf_getpubnames (dbg, print_pubnames, &n, 0);
10427 : 8 : }
10428 : :
10429 : : /* Print the content of the DWARF string section '.debug_str'. */
10430 : : static void
10431 : 45 : print_debug_str_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
10432 : : Ebl *ebl,
10433 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
10434 : : Elf_Scn *scn, GElf_Shdr *shdr,
10435 : : Dwarf *dbg __attribute__ ((unused)))
10436 : : {
10437 : 45 : Elf_Data *data = elf_rawdata (scn, NULL);
10438 [ + - ]: 45 : const size_t sh_size = data ? data->d_size : 0;
10439 : :
10440 : : /* Compute floor(log16(shdr->sh_size)). */
10441 : 45 : GElf_Addr tmp = sh_size;
10442 : 45 : int digits = 1;
10443 [ + + ]: 160 : while (tmp >= 16)
10444 : : {
10445 : 115 : ++digits;
10446 : 115 : tmp >>= 4;
10447 : : }
10448 : 45 : digits = MAX (4, digits);
10449 : :
10450 : 45 : printf (_("\nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"
10451 : : " %*s String\n"),
10452 : : elf_ndxscn (scn),
10453 : 45 : section_name (ebl, shdr), (uint64_t) shdr->sh_offset,
10454 : : /* TRANS: the debugstr| prefix makes the string unique. */
10455 [ + - ]: 45 : digits + 2, sgettext ("debugstr|Offset"));
10456 : :
10457 : 45 : Dwarf_Off offset = 0;
10458 [ + + ]: 74173 : while (offset < sh_size)
10459 : : {
10460 : 74128 : size_t len;
10461 : 74128 : const char *str = (const char *) data->d_buf + offset;
10462 : 74128 : const char *endp = memchr (str, '\0', sh_size - offset);
10463 [ - + ]: 74128 : if (unlikely (endp == NULL))
10464 : : {
10465 : 0 : printf (_(" *** error, missing string terminator\n"));
10466 : : break;
10467 : : }
10468 : :
10469 : 74128 : printf (" [%*" PRIx64 "] \"%s\"\n", digits, (uint64_t) offset, str);
10470 : 74128 : len = endp - str;
10471 : 74128 : offset += len + 1;
10472 : : }
10473 : 45 : }
10474 : :
10475 : : static void
10476 : 4 : print_debug_str_offsets_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
10477 : : Ebl *ebl,
10478 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
10479 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
10480 : : {
10481 : 4 : printf (_("\
10482 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
10483 : : elf_ndxscn (scn), section_name (ebl, shdr),
10484 : 4 : (uint64_t) shdr->sh_offset);
10485 : :
10486 [ + - ]: 4 : if (shdr->sh_size == 0)
10487 : : return;
10488 : :
10489 : : /* We like to get the section from libdw to make sure they are relocated. */
10490 : 8 : Elf_Data *data = (dbg->sectiondata[IDX_debug_str_offsets]
10491 [ - + ]: 4 : ?: elf_rawdata (scn, NULL));
10492 [ - + ]: 4 : if (unlikely (data == NULL))
10493 : : {
10494 : 0 : error (0, 0, _("cannot get .debug_str_offsets section data: %s"),
10495 : : elf_errmsg (-1));
10496 : 0 : return;
10497 : : }
10498 : :
10499 : 4 : size_t idx = 0;
10500 : 4 : sort_listptr (&known_stroffbases, "str_offsets");
10501 : :
10502 : 4 : const unsigned char *start = (const unsigned char *) data->d_buf;
10503 : 4 : const unsigned char *readp = start;
10504 : 4 : const unsigned char *readendp = ((const unsigned char *) data->d_buf
10505 : 4 : + data->d_size);
10506 : :
10507 : 4 : while (readp < readendp)
10508 : : {
10509 : : /* Most string offset tables will have a header. For split
10510 : : dwarf unit GNU DebugFission didn't add one. But they were
10511 : : also only defined for split units (main or skeleton units
10512 : : didn't have indirect strings). So if we don't have a
10513 : : DW_AT_str_offsets_base at all and this is offset zero, then
10514 : : just start printing offsets immediately, if this is a .dwo
10515 : : section. */
10516 : 4 : Dwarf_Off off = (Dwarf_Off) (readp
10517 : 4 : - (const unsigned char *) data->d_buf);
10518 : :
10519 : 4 : printf ("Table at offset %" PRIx64 " ", off);
10520 : :
10521 [ - + ]: 4 : struct listptr *listptr = get_listptr (&known_stroffbases, idx++);
10522 : 4 : const unsigned char *next_unitp = readendp;
10523 : 0 : uint8_t offset_size;
10524 : 0 : bool has_header;
10525 [ # # ]: 0 : if (listptr == NULL)
10526 : : {
10527 : : /* This can happen for .dwo files. There is only an header
10528 : : in the case this is a version 5 split DWARF file. */
10529 : 4 : Dwarf_CU *cu;
10530 : 4 : uint8_t unit_type;
10531 [ - + ]: 4 : if (dwarf_get_units (dbg, NULL, &cu, NULL, &unit_type,
10532 : : NULL, NULL) != 0)
10533 : : {
10534 : 0 : error (0, 0, "Warning: Cannot find any DWARF unit.");
10535 : : /* Just guess some values. */
10536 : 0 : has_header = false;
10537 : 0 : offset_size = 4;
10538 : : }
10539 [ + - ]: 4 : else if (off == 0
10540 : 4 : && (unit_type == DW_UT_split_type
10541 [ + - ]: 4 : || unit_type == DW_UT_split_compile))
10542 : : {
10543 : 4 : has_header = cu->version > 4;
10544 : 4 : offset_size = cu->offset_size;
10545 : : }
10546 : : else
10547 : : {
10548 : 0 : error (0, 0,
10549 : : "Warning: No CU references .debug_str_offsets after %"
10550 : : PRIx64, off);
10551 : 0 : has_header = cu->version > 4;
10552 : 0 : offset_size = cu->offset_size;
10553 : : }
10554 : 4 : printf ("\n");
10555 : : }
10556 : : else
10557 : : {
10558 : : /* This must be DWARF5, since GNU DebugFission didn't define
10559 : : DW_AT_str_offsets_base. */
10560 : 0 : has_header = true;
10561 : :
10562 : 0 : Dwarf_Die cudie;
10563 [ # # ]: 0 : if (dwarf_cu_die (listptr->cu, &cudie,
10564 : : NULL, NULL, NULL, NULL,
10565 : : NULL, NULL) == NULL)
10566 : 0 : printf ("Unknown CU (%s):\n", dwarf_errmsg (-1));
10567 : : else
10568 : 0 : printf ("for CU [%6" PRIx64 "]:\n", dwarf_dieoffset (&cudie));
10569 : : }
10570 : :
10571 [ + + ]: 4 : if (has_header)
10572 : : {
10573 : 2 : uint64_t unit_length;
10574 : 2 : uint16_t version;
10575 : 2 : uint16_t padding;
10576 : :
10577 [ - + ]: 2 : unit_length = read_4ubyte_unaligned_inc (dbg, readp);
10578 [ - + ]: 2 : if (unlikely (unit_length == 0xffffffff))
10579 : : {
10580 [ # # ]: 0 : if (unlikely (readp > readendp - 8))
10581 : : {
10582 : 0 : invalid_data:
10583 : 0 : error (0, 0, "Invalid data");
10584 : 0 : return;
10585 : : }
10586 [ # # ]: 0 : unit_length = read_8ubyte_unaligned_inc (dbg, readp);
10587 : 0 : offset_size = 8;
10588 : : }
10589 : : else
10590 : : offset_size = 4;
10591 : :
10592 : 2 : printf ("\n");
10593 : 2 : printf (_(" Length: %8" PRIu64 "\n"),
10594 : : unit_length);
10595 : 2 : printf (_(" Offset size: %8" PRIu8 "\n"),
10596 : : offset_size);
10597 : :
10598 : : /* We need at least 2-bytes (version) + 2-bytes (padding) =
10599 : : 4 bytes to complete the header. And this unit cannot go
10600 : : beyond the section data. */
10601 [ + - ]: 2 : if (readp > readendp - 4
10602 [ + - ]: 2 : || unit_length < 4
10603 [ - + ]: 2 : || unit_length > (uint64_t) (readendp - readp))
10604 : 0 : goto invalid_data;
10605 : :
10606 : 2 : next_unitp = readp + unit_length;
10607 : :
10608 [ - + ]: 2 : version = read_2ubyte_unaligned_inc (dbg, readp);
10609 : 2 : printf (_(" DWARF version: %8" PRIu16 "\n"), version);
10610 : :
10611 [ - + ]: 2 : if (version != 5)
10612 : : {
10613 : 0 : error (0, 0, _("Unknown version"));
10614 : 0 : goto next_unit;
10615 : : }
10616 : :
10617 [ - + ]: 2 : padding = read_2ubyte_unaligned_inc (dbg, readp);
10618 : 2 : printf (_(" Padding: %8" PRIx16 "\n"), padding);
10619 : :
10620 [ - + ]: 2 : if (listptr != NULL
10621 [ # # ]: 0 : && listptr->offset != (Dwarf_Off) (readp - start))
10622 : : {
10623 : 0 : error (0, 0, "String offsets index doesn't start after header");
10624 : 0 : goto next_unit;
10625 : : }
10626 : :
10627 : 2 : printf ("\n");
10628 : : }
10629 : :
10630 : 4 : int digits = 1;
10631 : 4 : size_t offsets = (next_unitp - readp) / offset_size;
10632 [ + + ]: 8 : while (offsets >= 10)
10633 : : {
10634 : 4 : ++digits;
10635 : 4 : offsets /= 10;
10636 : : }
10637 : :
10638 : 4 : unsigned int uidx = 0;
10639 : 4 : size_t index_offset = readp - (const unsigned char *) data->d_buf;
10640 : 4 : printf (" Offsets start at 0x%zx:\n", index_offset);
10641 : 64 : while (readp <= next_unitp - offset_size)
10642 : : {
10643 : 60 : Dwarf_Word offset;
10644 [ + - ]: 60 : if (offset_size == 4)
10645 [ - + ]: 120 : offset = read_4ubyte_unaligned_inc (dbg, readp);
10646 : : else
10647 [ # # ]: 0 : offset = read_8ubyte_unaligned_inc (dbg, readp);
10648 : 60 : const char *str = dwarf_getstring (dbg, offset, NULL);
10649 [ + + ]: 64 : printf (" [%*u] [%*" PRIx64 "] \"%s\"\n",
10650 [ - + ]: 60 : digits, uidx++, (int) offset_size * 2, offset, str ?: "???");
10651 : : }
10652 : 4 : printf ("\n");
10653 : :
10654 [ + - ]: 4 : if (readp != next_unitp)
10655 [ + + ]: 8 : error (0, 0, "extra %zd bytes at end of unit",
10656 : 0 : (size_t) (next_unitp - readp));
10657 : :
10658 : 4 : next_unit:
10659 : : readp = next_unitp;
10660 : : }
10661 : : }
10662 : :
10663 : :
10664 : : /* Print the content of the call frame search table section
10665 : : '.eh_frame_hdr'. */
10666 : : static void
10667 : 70 : print_debug_frame_hdr_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
10668 : : Ebl *ebl __attribute__ ((unused)),
10669 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
10670 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
10671 : : {
10672 : 70 : printf (_("\
10673 : : \nCall frame search table section [%2zu] '.eh_frame_hdr':\n"),
10674 : : elf_ndxscn (scn));
10675 : :
10676 : 70 : Elf_Data *data = elf_rawdata (scn, NULL);
10677 : :
10678 [ - + ]: 70 : if (unlikely (data == NULL))
10679 : : {
10680 : 0 : error (0, 0, _("cannot get %s content: %s"),
10681 : : ".eh_frame_hdr", elf_errmsg (-1));
10682 : 0 : return;
10683 : : }
10684 : :
10685 : 70 : const unsigned char *readp = data->d_buf;
10686 : 70 : const unsigned char *const dataend = ((unsigned char *) data->d_buf
10687 : 70 : + data->d_size);
10688 : :
10689 [ - + ]: 70 : if (unlikely (readp + 4 > dataend))
10690 : : {
10691 : 0 : invalid_data:
10692 : 0 : error (0, 0, _("invalid data"));
10693 : 0 : return;
10694 : : }
10695 : :
10696 : 70 : unsigned int version = *readp++;
10697 : 70 : unsigned int eh_frame_ptr_enc = *readp++;
10698 : 70 : unsigned int fde_count_enc = *readp++;
10699 : 70 : unsigned int table_enc = *readp++;
10700 : :
10701 : 70 : printf (" version: %u\n"
10702 : : " eh_frame_ptr_enc: %#x ",
10703 : : version, eh_frame_ptr_enc);
10704 : 70 : print_encoding_base ("", eh_frame_ptr_enc);
10705 : 70 : printf (" fde_count_enc: %#x ", fde_count_enc);
10706 : 70 : print_encoding_base ("", fde_count_enc);
10707 : 70 : printf (" table_enc: %#x ", table_enc);
10708 : 70 : print_encoding_base ("", table_enc);
10709 : :
10710 : 70 : uint64_t eh_frame_ptr = 0;
10711 [ + - ]: 70 : if (eh_frame_ptr_enc != DW_EH_PE_omit)
10712 : : {
10713 : 70 : readp = read_encoded (eh_frame_ptr_enc, readp, dataend, &eh_frame_ptr,
10714 : : dbg);
10715 [ - + ]: 70 : if (unlikely (readp == NULL))
10716 : 0 : goto invalid_data;
10717 : :
10718 : 70 : printf (" eh_frame_ptr: %#" PRIx64, eh_frame_ptr);
10719 [ + - ]: 70 : if ((eh_frame_ptr_enc & 0x70) == DW_EH_PE_pcrel)
10720 : 140 : printf (" (offset: %#" PRIx64 ")",
10721 : : /* +4 because of the 4 byte header of the section. */
10722 : 70 : (uint64_t) shdr->sh_offset + 4 + eh_frame_ptr);
10723 : :
10724 [ - + ]: 70 : putchar_unlocked ('\n');
10725 : : }
10726 : :
10727 : 70 : uint64_t fde_count = 0;
10728 [ + - ]: 70 : if (fde_count_enc != DW_EH_PE_omit)
10729 : : {
10730 : 70 : readp = read_encoded (fde_count_enc, readp, dataend, &fde_count, dbg);
10731 [ - + ]: 70 : if (unlikely (readp == NULL))
10732 : 0 : goto invalid_data;
10733 : :
10734 : 70 : printf (" fde_count: %" PRIu64 "\n", fde_count);
10735 : : }
10736 : :
10737 [ + - + - ]: 70 : if (fde_count == 0 || table_enc == DW_EH_PE_omit)
10738 : : return;
10739 : :
10740 : 70 : puts (" Table:");
10741 : :
10742 : : /* Optimize for the most common case. */
10743 [ + - ]: 70 : if (table_enc == (DW_EH_PE_datarel | DW_EH_PE_sdata4))
10744 [ + + ]: 12517 : while (fde_count > 0 && readp + 8 <= dataend)
10745 : : {
10746 [ - + ]: 12447 : int32_t initial_location = read_4sbyte_unaligned_inc (dbg, readp);
10747 : 12447 : uint64_t initial_offset = ((uint64_t) shdr->sh_offset
10748 : 12447 : + (int64_t) initial_location);
10749 [ - + ]: 12447 : int32_t address = read_4sbyte_unaligned_inc (dbg, readp);
10750 : : // XXX Possibly print symbol name or section offset for initial_offset
10751 [ + - ]: 12517 : printf (" %#" PRIx32 " (offset: %#6" PRIx64 ") -> %#" PRIx32
10752 : : " fde=[%6" PRIx64 "]\n",
10753 : : initial_location, initial_offset,
10754 : 12447 : address, address - (eh_frame_ptr + 4));
10755 : : }
10756 : : else
10757 : 70 : while (0 && readp < dataend)
10758 : : {
10759 : :
10760 : 70 : }
10761 : : }
10762 : :
10763 : :
10764 : : /* Print the content of the exception handling table section
10765 : : '.eh_frame_hdr'. */
10766 : : static void
10767 : 0 : print_debug_exception_table (Dwfl_Module *dwflmod __attribute__ ((unused)),
10768 : : Ebl *ebl __attribute__ ((unused)),
10769 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
10770 : : Elf_Scn *scn,
10771 : : GElf_Shdr *shdr __attribute__ ((unused)),
10772 : : Dwarf *dbg __attribute__ ((unused)))
10773 : : {
10774 : 0 : printf (_("\
10775 : : \nException handling table section [%2zu] '.gcc_except_table':\n"),
10776 : : elf_ndxscn (scn));
10777 : :
10778 : 0 : Elf_Data *data = elf_rawdata (scn, NULL);
10779 : :
10780 [ # # ]: 0 : if (unlikely (data == NULL))
10781 : : {
10782 : 0 : error (0, 0, _("cannot get %s content: %s"),
10783 : : ".gcc_except_table", elf_errmsg (-1));
10784 : 0 : return;
10785 : : }
10786 : :
10787 : 0 : const unsigned char *readp = data->d_buf;
10788 : 0 : const unsigned char *const dataend = readp + data->d_size;
10789 : :
10790 [ # # ]: 0 : if (unlikely (readp + 1 > dataend))
10791 : : {
10792 : 0 : invalid_data:
10793 : 0 : error (0, 0, _("invalid data"));
10794 : 0 : return;
10795 : : }
10796 : 0 : unsigned int lpstart_encoding = *readp++;
10797 : 0 : printf (_(" LPStart encoding: %#x "), lpstart_encoding);
10798 : 0 : print_encoding_base ("", lpstart_encoding);
10799 [ # # ]: 0 : if (lpstart_encoding != DW_EH_PE_omit)
10800 : : {
10801 : 0 : uint64_t lpstart;
10802 : 0 : readp = read_encoded (lpstart_encoding, readp, dataend, &lpstart, dbg);
10803 : 0 : printf (" LPStart: %#" PRIx64 "\n", lpstart);
10804 : : }
10805 : :
10806 [ # # ]: 0 : if (unlikely (readp + 1 > dataend))
10807 : 0 : goto invalid_data;
10808 : 0 : unsigned int ttype_encoding = *readp++;
10809 : 0 : printf (_(" TType encoding: %#x "), ttype_encoding);
10810 : 0 : print_encoding_base ("", ttype_encoding);
10811 : 0 : const unsigned char *ttype_base = NULL;
10812 [ # # ]: 0 : if (ttype_encoding != DW_EH_PE_omit)
10813 : : {
10814 : 0 : unsigned int ttype_base_offset;
10815 : 0 : get_uleb128 (ttype_base_offset, readp, dataend);
10816 : 0 : printf (" TType base offset: %#x\n", ttype_base_offset);
10817 [ # # ]: 0 : if ((size_t) (dataend - readp) > ttype_base_offset)
10818 : 0 : ttype_base = readp + ttype_base_offset;
10819 : : }
10820 : :
10821 [ # # ]: 0 : if (unlikely (readp + 1 > dataend))
10822 : 0 : goto invalid_data;
10823 : 0 : unsigned int call_site_encoding = *readp++;
10824 : 0 : printf (_(" Call site encoding: %#x "), call_site_encoding);
10825 : 0 : print_encoding_base ("", call_site_encoding);
10826 : 0 : unsigned int call_site_table_len;
10827 : 0 : get_uleb128 (call_site_table_len, readp, dataend);
10828 : :
10829 : 0 : const unsigned char *const action_table = readp + call_site_table_len;
10830 [ # # ]: 0 : if (unlikely (action_table > dataend))
10831 : 0 : goto invalid_data;
10832 : : unsigned int u = 0;
10833 : : unsigned int max_action = 0;
10834 [ # # ]: 0 : while (readp < action_table)
10835 : : {
10836 [ # # ]: 0 : if (u == 0)
10837 : 0 : puts (_("\n Call site table:"));
10838 : :
10839 : 0 : uint64_t call_site_start;
10840 : 0 : readp = read_encoded (call_site_encoding, readp, dataend,
10841 : : &call_site_start, dbg);
10842 : 0 : uint64_t call_site_length;
10843 : 0 : readp = read_encoded (call_site_encoding, readp, dataend,
10844 : : &call_site_length, dbg);
10845 : 0 : uint64_t landing_pad;
10846 : 0 : readp = read_encoded (call_site_encoding, readp, dataend,
10847 : : &landing_pad, dbg);
10848 : 0 : unsigned int action;
10849 : 0 : get_uleb128 (action, readp, dataend);
10850 : 0 : max_action = MAX (action, max_action);
10851 : 0 : printf (_(" [%4u] Call site start: %#" PRIx64 "\n"
10852 : : " Call site length: %" PRIu64 "\n"
10853 : : " Landing pad: %#" PRIx64 "\n"
10854 : : " Action: %u\n"),
10855 : : u++, call_site_start, call_site_length, landing_pad, action);
10856 : : }
10857 [ # # ]: 0 : if (readp != action_table)
10858 : 0 : goto invalid_data;
10859 : :
10860 : 0 : unsigned int max_ar_filter = 0;
10861 [ # # ]: 0 : if (max_action > 0)
10862 : : {
10863 : 0 : puts ("\n Action table:");
10864 : :
10865 : 0 : size_t maxdata = (size_t) (dataend - action_table);
10866 [ # # # # ]: 0 : if (max_action > maxdata || maxdata - max_action < 1)
10867 : : {
10868 : 0 : invalid_action_table:
10869 : 0 : fputs (_(" <INVALID DATA>\n"), stdout);
10870 : 0 : return;
10871 : : }
10872 : :
10873 : 0 : const unsigned char *const action_table_end
10874 : 0 : = action_table + max_action + 1;
10875 : :
10876 : 0 : u = 0;
10877 : 0 : do
10878 : : {
10879 : 0 : int ar_filter;
10880 : 0 : get_sleb128 (ar_filter, readp, action_table_end);
10881 [ # # ]: 0 : if (ar_filter > 0 && (unsigned int) ar_filter > max_ar_filter)
10882 : 0 : max_ar_filter = ar_filter;
10883 : 0 : int ar_disp;
10884 [ # # ]: 0 : if (readp >= action_table_end)
10885 : 0 : goto invalid_action_table;
10886 : 0 : get_sleb128 (ar_disp, readp, action_table_end);
10887 : :
10888 : 0 : printf (" [%4u] ar_filter: % d\n"
10889 : : " ar_disp: % -5d",
10890 : : u, ar_filter, ar_disp);
10891 [ # # ]: 0 : if (abs (ar_disp) & 1)
10892 : 0 : printf (" -> [%4u]\n", u + (ar_disp + 1) / 2);
10893 [ # # ]: 0 : else if (ar_disp != 0)
10894 : 0 : puts (" -> ???");
10895 : : else
10896 [ # # ]: 0 : putchar_unlocked ('\n');
10897 : 0 : ++u;
10898 : : }
10899 [ # # ]: 0 : while (readp < action_table_end);
10900 : : }
10901 : :
10902 [ # # ]: 0 : if (max_ar_filter > 0 && ttype_base != NULL)
10903 : : {
10904 : 0 : unsigned char dsize;
10905 : 0 : puts ("\n TType table:");
10906 : :
10907 : : // XXX Not *4, size of encoding;
10908 [ # # ]: 0 : switch (ttype_encoding & 7)
10909 : : {
10910 : : case DW_EH_PE_udata2:
10911 : : case DW_EH_PE_sdata2:
10912 : : dsize = 2;
10913 : : break;
10914 : : case DW_EH_PE_udata4:
10915 : : case DW_EH_PE_sdata4:
10916 : : dsize = 4;
10917 : : break;
10918 : : case DW_EH_PE_udata8:
10919 : : case DW_EH_PE_sdata8:
10920 : : dsize = 8;
10921 : : break;
10922 : 0 : default:
10923 : 0 : dsize = 0;
10924 : 0 : error (1, 0, _("invalid TType encoding"));
10925 : : }
10926 : :
10927 : 0 : if (max_ar_filter
10928 [ # # ]: 0 : > (size_t) (ttype_base - (const unsigned char *) data->d_buf) / dsize)
10929 : 0 : goto invalid_data;
10930 : :
10931 : 0 : readp = ttype_base - max_ar_filter * dsize;
10932 : 0 : do
10933 : : {
10934 : 0 : uint64_t ttype;
10935 : 0 : readp = read_encoded (ttype_encoding, readp, ttype_base, &ttype,
10936 : : dbg);
10937 : 0 : printf (" [%4u] %#" PRIx64 "\n", max_ar_filter--, ttype);
10938 : : }
10939 [ # # ]: 0 : while (readp < ttype_base);
10940 : : }
10941 : : }
10942 : :
10943 : : /* Print the content of the '.gdb_index' section.
10944 : : http://sourceware.org/gdb/current/onlinedocs/gdb/Index-Section-Format.html
10945 : : */
10946 : : static void
10947 : 2 : print_gdb_index_section (Dwfl_Module *dwflmod, Ebl *ebl,
10948 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
10949 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
10950 : : {
10951 : 2 : printf (_("\nGDB section [%2zu] '%s' at offset %#" PRIx64
10952 : : " contains %" PRId64 " bytes :\n"),
10953 : : elf_ndxscn (scn), section_name (ebl, shdr),
10954 : 2 : (uint64_t) shdr->sh_offset, (uint64_t) shdr->sh_size);
10955 : :
10956 : 2 : Elf_Data *data = elf_rawdata (scn, NULL);
10957 : :
10958 [ - + ]: 2 : if (unlikely (data == NULL))
10959 : : {
10960 : 0 : error (0, 0, _("cannot get %s content: %s"),
10961 : : ".gdb_index", elf_errmsg (-1));
10962 : 0 : return;
10963 : : }
10964 : :
10965 : : // .gdb_index is always in little endian.
10966 : 2 : Dwarf dummy_dbg = { .other_byte_order = MY_ELFDATA != ELFDATA2LSB };
10967 : 2 : dbg = &dummy_dbg;
10968 : :
10969 : 2 : const unsigned char *readp = data->d_buf;
10970 : 2 : const unsigned char *const dataend = readp + data->d_size;
10971 : :
10972 [ - + ]: 2 : if (unlikely (readp + 4 > dataend))
10973 : : {
10974 : 0 : invalid_data:
10975 : 0 : error (0, 0, _("invalid data"));
10976 : 0 : return;
10977 : : }
10978 : :
10979 : 2 : int32_t vers = read_4ubyte_unaligned (dbg, readp);
10980 : 2 : printf (_(" Version: %" PRId32 "\n"), vers);
10981 : :
10982 : : // The only difference between version 4 and version 5 is the
10983 : : // hash used for generating the table. Version 6 contains symbols
10984 : : // for inlined functions, older versions didn't. Version 7 adds
10985 : : // symbol kinds. Version 8 just indicates that it correctly includes
10986 : : // TUs for symbols.
10987 [ - + ]: 2 : if (vers < 4 || vers > 8)
10988 : : {
10989 : 0 : printf (_(" unknown version, cannot parse section\n"));
10990 : 0 : return;
10991 : : }
10992 : :
10993 : 2 : readp += 4;
10994 [ - + ]: 2 : if (unlikely (readp + 4 > dataend))
10995 : 0 : goto invalid_data;
10996 : :
10997 : 2 : uint32_t cu_off = read_4ubyte_unaligned (dbg, readp);
10998 : 2 : printf (_(" CU offset: %#" PRIx32 "\n"), cu_off);
10999 : :
11000 : 2 : readp += 4;
11001 [ - + ]: 2 : if (unlikely (readp + 4 > dataend))
11002 : 0 : goto invalid_data;
11003 : :
11004 : 2 : uint32_t tu_off = read_4ubyte_unaligned (dbg, readp);
11005 : 2 : printf (_(" TU offset: %#" PRIx32 "\n"), tu_off);
11006 : :
11007 : 2 : readp += 4;
11008 [ - + ]: 2 : if (unlikely (readp + 4 > dataend))
11009 : 0 : goto invalid_data;
11010 : :
11011 : 2 : uint32_t addr_off = read_4ubyte_unaligned (dbg, readp);
11012 : 2 : printf (_(" address offset: %#" PRIx32 "\n"), addr_off);
11013 : :
11014 : 2 : readp += 4;
11015 [ - + ]: 2 : if (unlikely (readp + 4 > dataend))
11016 : 0 : goto invalid_data;
11017 : :
11018 : 2 : uint32_t sym_off = read_4ubyte_unaligned (dbg, readp);
11019 : 2 : printf (_(" symbol offset: %#" PRIx32 "\n"), sym_off);
11020 : :
11021 : 2 : readp += 4;
11022 [ - + ]: 2 : if (unlikely (readp + 4 > dataend))
11023 : 0 : goto invalid_data;
11024 : :
11025 : 2 : uint32_t const_off = read_4ubyte_unaligned (dbg, readp);
11026 : 2 : printf (_(" constant offset: %#" PRIx32 "\n"), const_off);
11027 : :
11028 [ - + ]: 2 : if (unlikely ((size_t) (dataend - (const unsigned char *) data->d_buf)
11029 : : < const_off))
11030 : 0 : goto invalid_data;
11031 : :
11032 : 2 : readp = data->d_buf + cu_off;
11033 : :
11034 : 2 : const unsigned char *nextp = data->d_buf + tu_off;
11035 [ - + ]: 2 : if (tu_off >= data->d_size)
11036 : 0 : goto invalid_data;
11037 : :
11038 : 2 : size_t cu_nr = (nextp - readp) / 16;
11039 : :
11040 : 2 : printf (_("\n CU list at offset %#" PRIx32
11041 : : " contains %zu entries:\n"),
11042 : : cu_off, cu_nr);
11043 : :
11044 : 2 : size_t n = 0;
11045 [ + - + + ]: 6 : while (dataend - readp >= 16 && n < cu_nr)
11046 : : {
11047 : 4 : uint64_t off = read_8ubyte_unaligned (dbg, readp);
11048 : 4 : readp += 8;
11049 : :
11050 : 4 : uint64_t len = read_8ubyte_unaligned (dbg, readp);
11051 : 4 : readp += 8;
11052 : :
11053 : 4 : printf (" [%4zu] start: %0#8" PRIx64
11054 : : ", length: %5" PRIu64 "\n", n, off, len);
11055 : 4 : n++;
11056 : : }
11057 : :
11058 : 2 : readp = data->d_buf + tu_off;
11059 : 2 : nextp = data->d_buf + addr_off;
11060 [ - + ]: 2 : if (addr_off >= data->d_size)
11061 : 0 : goto invalid_data;
11062 : :
11063 : 2 : size_t tu_nr = (nextp - readp) / 24;
11064 : :
11065 : 2 : printf (_("\n TU list at offset %#" PRIx32
11066 : : " contains %zu entries:\n"),
11067 : : tu_off, tu_nr);
11068 : :
11069 : 2 : n = 0;
11070 [ + - + + ]: 4 : while (dataend - readp >= 24 && n < tu_nr)
11071 : : {
11072 : 2 : uint64_t off = read_8ubyte_unaligned (dbg, readp);
11073 : 2 : readp += 8;
11074 : :
11075 : 2 : uint64_t type = read_8ubyte_unaligned (dbg, readp);
11076 : 2 : readp += 8;
11077 : :
11078 : 2 : uint64_t sig = read_8ubyte_unaligned (dbg, readp);
11079 : 2 : readp += 8;
11080 : :
11081 : 2 : printf (" [%4zu] CU offset: %5" PRId64
11082 : : ", type offset: %5" PRId64
11083 : : ", signature: %0#8" PRIx64 "\n", n, off, type, sig);
11084 : 2 : n++;
11085 : : }
11086 : :
11087 : 2 : readp = data->d_buf + addr_off;
11088 : 2 : nextp = data->d_buf + sym_off;
11089 [ - + ]: 2 : if (sym_off >= data->d_size)
11090 : 0 : goto invalid_data;
11091 : :
11092 : 2 : size_t addr_nr = (nextp - readp) / 20;
11093 : :
11094 : 2 : printf (_("\n Address list at offset %#" PRIx32
11095 : : " contains %zu entries:\n"),
11096 : : addr_off, addr_nr);
11097 : :
11098 : 2 : n = 0;
11099 [ + - + + ]: 6 : while (dataend - readp >= 20 && n < addr_nr)
11100 : : {
11101 : 4 : uint64_t low = read_8ubyte_unaligned (dbg, readp);
11102 : 4 : readp += 8;
11103 : :
11104 : 4 : uint64_t high = read_8ubyte_unaligned (dbg, readp);
11105 : 4 : readp += 8;
11106 : :
11107 : 4 : uint32_t idx = read_4ubyte_unaligned (dbg, readp);
11108 : 4 : readp += 4;
11109 : :
11110 : 4 : printf (" [%4zu] ", n);
11111 : 4 : print_dwarf_addr (dwflmod, 8, low, low);
11112 : 4 : printf ("..");
11113 : 4 : print_dwarf_addr (dwflmod, 8, high - 1, high);
11114 : 4 : printf (", CU index: %5" PRId32 "\n", idx);
11115 : 4 : n++;
11116 : : }
11117 : :
11118 : 2 : const unsigned char *const_start = data->d_buf + const_off;
11119 [ - + ]: 2 : if (const_off >= data->d_size)
11120 : 0 : goto invalid_data;
11121 : :
11122 : 2 : readp = data->d_buf + sym_off;
11123 : 2 : nextp = const_start;
11124 : 2 : size_t sym_nr = (nextp - readp) / 8;
11125 : :
11126 : 2 : printf (_("\n Symbol table at offset %#" PRIx32
11127 : : " contains %zu slots:\n"),
11128 : : addr_off, sym_nr);
11129 : :
11130 : 2 : n = 0;
11131 [ + - + + ]: 2050 : while (dataend - readp >= 8 && n < sym_nr)
11132 : : {
11133 [ + + ]: 2048 : uint32_t name = read_4ubyte_unaligned (dbg, readp);
11134 : 2048 : readp += 4;
11135 : :
11136 : 2048 : uint32_t vector = read_4ubyte_unaligned (dbg, readp);
11137 : 2048 : readp += 4;
11138 : :
11139 [ + + ]: 2048 : if (name != 0 || vector != 0)
11140 : : {
11141 : 14 : const unsigned char *sym = const_start + name;
11142 [ + - - + ]: 14 : if (unlikely ((size_t) (dataend - const_start) < name
11143 : : || memchr (sym, '\0', dataend - sym) == NULL))
11144 : 0 : goto invalid_data;
11145 : :
11146 : 14 : printf (" [%4zu] symbol: %s, CUs: ", n, sym);
11147 : :
11148 : 14 : const unsigned char *readcus = const_start + vector;
11149 [ - + ]: 14 : if (unlikely ((size_t) (dataend - const_start) < vector))
11150 : 0 : goto invalid_data;
11151 : 14 : uint32_t cus = read_4ubyte_unaligned (dbg, readcus);
11152 : 14 : while (cus--)
11153 : : {
11154 : 16 : uint32_t cu_kind, cu, kind;
11155 : 16 : bool is_static;
11156 : 16 : readcus += 4;
11157 [ - + ]: 16 : if (unlikely (readcus + 4 > dataend))
11158 : 0 : goto invalid_data;
11159 [ + + ]: 16 : cu_kind = read_4ubyte_unaligned (dbg, readcus);
11160 : 16 : cu = cu_kind & ((1 << 24) - 1);
11161 : 16 : kind = (cu_kind >> 28) & 7;
11162 : 16 : is_static = cu_kind & (1U << 31);
11163 [ + + ]: 16 : if (cu > cu_nr - 1)
11164 : 2 : printf ("%" PRId32 "T", cu - (uint32_t) cu_nr);
11165 : : else
11166 : 14 : printf ("%" PRId32, cu);
11167 [ + + ]: 16 : if (kind != 0)
11168 : : {
11169 : 8 : printf (" (");
11170 [ + + + - : 8 : switch (kind)
- ]
11171 : : {
11172 : : case 1:
11173 : 3 : printf ("type");
11174 : : break;
11175 : : case 2:
11176 : 2 : printf ("var");
11177 : : break;
11178 : : case 3:
11179 : 3 : printf ("func");
11180 : : break;
11181 : : case 4:
11182 : 0 : printf ("other");
11183 : : break;
11184 : : default:
11185 : 0 : printf ("unknown-0x%" PRIx32, kind);
11186 : : break;
11187 : : }
11188 [ + + ]: 8 : printf (":%c)", (is_static ? 'S' : 'G'));
11189 : : }
11190 [ + + ]: 16 : if (cus > 0)
11191 [ + + ]: 32 : printf (", ");
11192 : : }
11193 : 14 : printf ("\n");
11194 : : }
11195 : 2048 : n++;
11196 : : }
11197 : : }
11198 : :
11199 : : /* Returns true and sets split DWARF CU id if there is a split compile
11200 : : unit in the given Dwarf, and no non-split units are found (before it). */
11201 : : static bool
11202 : 187 : is_split_dwarf (Dwarf *dbg, uint64_t *id, Dwarf_CU **split_cu)
11203 : : {
11204 : 187 : Dwarf_CU *cu = NULL;
11205 [ + + ]: 187 : while (dwarf_get_units (dbg, cu, &cu, NULL, NULL, NULL, NULL) == 0)
11206 : : {
11207 : 186 : uint8_t unit_type;
11208 [ + - ]: 186 : if (dwarf_cu_info (cu, NULL, &unit_type, NULL, NULL,
11209 : : id, NULL, NULL) != 0)
11210 : 186 : return false;
11211 : :
11212 [ + + ]: 186 : if (unit_type != DW_UT_split_compile && unit_type != DW_UT_split_type)
11213 : : return false;
11214 : :
11215 : : /* We really only care about the split compile unit, the types
11216 : : should be fine and self sufficient. Also they don't have an
11217 : : id that we can match with a skeleton unit. */
11218 [ + - ]: 9 : if (unit_type == DW_UT_split_compile)
11219 : : {
11220 : 9 : *split_cu = cu;
11221 : 9 : return true;
11222 : : }
11223 : : }
11224 : :
11225 : : return false;
11226 : : }
11227 : :
11228 : : /* Check that there is one and only one Dwfl_Module, return in arg. */
11229 : : static int
11230 : 9 : getone_dwflmod (Dwfl_Module *dwflmod,
11231 : : void **userdata __attribute__ ((unused)),
11232 : : const char *name __attribute__ ((unused)),
11233 : : Dwarf_Addr base __attribute__ ((unused)),
11234 : : void *arg)
11235 : : {
11236 : 9 : Dwfl_Module **m = (Dwfl_Module **) arg;
11237 [ + - ]: 9 : if (*m != NULL)
11238 : : return DWARF_CB_ABORT;
11239 : 9 : *m = dwflmod;
11240 : 9 : return DWARF_CB_OK;
11241 : : }
11242 : :
11243 : : static void
11244 : 269 : print_debug (Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr)
11245 : : {
11246 : : /* Used for skeleton file, if necessary for split DWARF. */
11247 : 269 : Dwfl *skel_dwfl = NULL;
11248 : 269 : Dwfl_Module *skel_mod = NULL;
11249 : 269 : char *skel_name = NULL;
11250 : 269 : Dwarf *split_dbg = NULL;
11251 : 269 : Dwarf_CU *split_cu = NULL;
11252 : :
11253 : : /* Before we start the real work get a debug context descriptor. */
11254 : 269 : Dwarf_Addr dwbias;
11255 : 269 : Dwarf *dbg = dwfl_module_getdwarf (dwflmod, &dwbias);
11256 : 269 : Dwarf dummy_dbg =
11257 : : {
11258 : 269 : .elf = ebl->elf,
11259 : 269 : .other_byte_order = MY_ELFDATA != ehdr->e_ident[EI_DATA]
11260 : : };
11261 [ + + ]: 269 : if (dbg == NULL)
11262 : : {
11263 [ - + ]: 82 : if ((print_debug_sections & ~(section_exception|section_frame)) != 0)
11264 : 0 : error (0, 0, _("cannot get debug context descriptor: %s"),
11265 : : dwfl_errmsg (-1));
11266 : : dbg = &dummy_dbg;
11267 : : }
11268 : : else
11269 : : {
11270 : : /* If we are asked about a split dwarf (.dwo) file, use the user
11271 : : provided, or find the corresponding skeleton file. If we got
11272 : : a skeleton file, replace the given dwflmod and dbg, with one
11273 : : derived from the skeleton file to provide enough context. */
11274 : 187 : uint64_t split_id;
11275 [ + + ]: 187 : if (is_split_dwarf (dbg, &split_id, &split_cu))
11276 : : {
11277 [ + - ]: 9 : if (dwarf_skeleton != NULL)
11278 : 9 : skel_name = strdup (dwarf_skeleton);
11279 : : else
11280 : : {
11281 : : /* Replace file.dwo with file.o and see if that matches. */
11282 : 0 : const char *fname;
11283 : 0 : dwfl_module_info (dwflmod, NULL, NULL, NULL, NULL, NULL,
11284 : : &fname, NULL);
11285 [ # # ]: 0 : if (fname != NULL)
11286 : : {
11287 : 0 : size_t flen = strlen (fname);
11288 [ # # # # ]: 0 : if (flen > 4 && strcmp (".dwo", fname + flen - 4) == 0)
11289 : : {
11290 : 0 : skel_name = strdup (fname);
11291 [ # # ]: 0 : if (skel_name != NULL)
11292 : : {
11293 : 0 : skel_name[flen - 3] = 'o';
11294 : 0 : skel_name[flen - 2] = '\0';
11295 : : }
11296 : : }
11297 : : }
11298 : : }
11299 : :
11300 [ + - ]: 9 : if (skel_name != NULL)
11301 : : {
11302 : 9 : int skel_fd = open (skel_name, O_RDONLY);
11303 [ - + ]: 9 : if (skel_fd == -1)
11304 : 0 : fprintf (stderr, "Warning: Couldn't open DWARF skeleton file"
11305 : : " '%s'\n", skel_name);
11306 : : else
11307 : 9 : skel_dwfl = create_dwfl (skel_fd, skel_name);
11308 : :
11309 [ + - ]: 9 : if (skel_dwfl != NULL)
11310 : : {
11311 [ - + ]: 9 : if (dwfl_getmodules (skel_dwfl, &getone_dwflmod,
11312 : : &skel_mod, 0) != 0)
11313 : : {
11314 : 0 : fprintf (stderr, "Warning: Bad DWARF skeleton,"
11315 : : " multiple modules '%s'\n", skel_name);
11316 : 0 : dwfl_end (skel_dwfl);
11317 : 0 : skel_mod = NULL;
11318 : : }
11319 : : }
11320 [ # # ]: 0 : else if (skel_fd != -1)
11321 : 0 : fprintf (stderr, "Warning: Couldn't create skeleton dwfl for"
11322 : : " '%s': %s\n", skel_name, dwfl_errmsg (-1));
11323 : :
11324 [ + - ]: 9 : if (skel_mod != NULL)
11325 : : {
11326 : 9 : Dwarf *skel_dbg = dwfl_module_getdwarf (skel_mod, &dwbias);
11327 [ + - ]: 9 : if (skel_dbg != NULL)
11328 : : {
11329 : : /* First check the skeleton CU DIE, only fetch
11330 : : the split DIE if we know the id matches to
11331 : : not unnecessary search for any split DIEs we
11332 : : don't need. */
11333 : 9 : Dwarf_CU *cu = NULL;
11334 [ + - ]: 13 : while (dwarf_get_units (skel_dbg, cu, &cu,
11335 : : NULL, NULL, NULL, NULL) == 0)
11336 : : {
11337 : 13 : uint8_t unit_type;
11338 : 13 : uint64_t skel_id;
11339 [ + - ]: 13 : if (dwarf_cu_info (cu, NULL, &unit_type, NULL, NULL,
11340 : : &skel_id, NULL, NULL) == 0
11341 [ + - ]: 13 : && unit_type == DW_UT_skeleton
11342 [ + + ]: 13 : && split_id == skel_id)
11343 : : {
11344 : 9 : Dwarf_Die subdie;
11345 [ + - ]: 9 : if (dwarf_cu_info (cu, NULL, NULL, NULL,
11346 : : &subdie,
11347 : : NULL, NULL, NULL) == 0
11348 [ + - ]: 9 : && dwarf_tag (&subdie) != DW_TAG_invalid)
11349 : : {
11350 : 9 : split_dbg = dwarf_cu_getdwarf (subdie.cu);
11351 [ - + ]: 9 : if (split_dbg == NULL)
11352 : 0 : fprintf (stderr,
11353 : : "Warning: Couldn't get split_dbg:"
11354 : : " %s\n", dwarf_errmsg (-1));
11355 : 9 : break;
11356 : : }
11357 : : else
11358 : : {
11359 : : /* Everything matches up, but not
11360 : : according to libdw. Which means
11361 : : the user knew better. So...
11362 : : Terrible hack... We can never
11363 : : destroy the underlying dwfl
11364 : : because it would free the wrong
11365 : : Dwarfs... So we leak memory...*/
11366 [ # # ]: 0 : if (cu->split == NULL
11367 [ # # ]: 0 : && dwarf_skeleton != NULL)
11368 : : {
11369 : 0 : do_not_close_dwfl = true;
11370 : 0 : __libdw_link_skel_split (cu, split_cu);
11371 : 0 : split_dbg = dwarf_cu_getdwarf (split_cu);
11372 : 0 : break;
11373 : : }
11374 : : else
11375 : 0 : fprintf (stderr, "Warning: Couldn't get"
11376 : : " skeleton subdie: %s\n",
11377 : : dwarf_errmsg (-1));
11378 : : }
11379 : : }
11380 : : }
11381 [ - + ]: 9 : if (split_dbg == NULL)
11382 : 9 : fprintf (stderr, "Warning: '%s' didn't contain a skeleton for split id %" PRIx64 "\n", skel_name, split_id);
11383 : : }
11384 : : else
11385 : 0 : fprintf (stderr, "Warning: Couldn't get skeleton DWARF:"
11386 : : " %s\n", dwfl_errmsg (-1));
11387 : : }
11388 : : }
11389 : :
11390 [ + - ]: 9 : if (split_dbg != NULL)
11391 : : {
11392 : 9 : dbg = split_dbg;
11393 : 9 : dwflmod = skel_mod;
11394 : : }
11395 [ # # ]: 0 : else if (skel_name == NULL)
11396 : 0 : fprintf (stderr,
11397 : : "Warning: split DWARF file, but no skeleton found.\n");
11398 : : }
11399 [ - + ]: 178 : else if (dwarf_skeleton != NULL)
11400 : 187 : fprintf (stderr, "Warning: DWARF skeleton given,"
11401 : : " but not a split DWARF file\n");
11402 : : }
11403 : :
11404 : : /* Get the section header string table index. */
11405 : 269 : size_t shstrndx;
11406 [ - + ]: 269 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
11407 : 0 : error_exit (0, _("cannot get section header string table index"));
11408 : :
11409 : : /* If the .debug_info section is listed as implicitly required then
11410 : : we must make sure to handle it before handling any other debug
11411 : : section. Various other sections depend on the CU DIEs being
11412 : : scanned (silently) first. */
11413 : 269 : bool implicit_info = (implicit_debug_sections & section_info) != 0;
11414 : 269 : bool explicit_info = (print_debug_sections & section_info) != 0;
11415 [ + + ]: 269 : if (implicit_info)
11416 : : {
11417 : : Elf_Scn *scn = NULL;
11418 [ + - ]: 1458 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
11419 : : {
11420 : 1458 : GElf_Shdr shdr_mem;
11421 : 1458 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
11422 : :
11423 [ + - + + ]: 1458 : if (shdr != NULL && shdr->sh_type == SHT_PROGBITS)
11424 : : {
11425 : 1182 : const char *name = elf_strptr (ebl->elf, shstrndx,
11426 : 591 : shdr->sh_name);
11427 [ - + ]: 591 : if (name == NULL)
11428 : 0 : continue;
11429 : :
11430 [ + + ]: 591 : if (strcmp (name, ".debug_info") == 0
11431 [ + + ]: 529 : || strcmp (name, ".debug_info.dwo") == 0
11432 [ + + ]: 520 : || strcmp (name, ".zdebug_info") == 0
11433 [ + - ]: 514 : || strcmp (name, ".zdebug_info.dwo") == 0
11434 [ + + ]: 514 : || strcmp (name, ".gnu.debuglto_.debug_info") == 0)
11435 : : {
11436 : 78 : print_debug_info_section (dwflmod, ebl, ehdr,
11437 : : scn, shdr, dbg);
11438 : 78 : break;
11439 : : }
11440 : : }
11441 : : }
11442 : 78 : print_debug_sections &= ~section_info;
11443 : 78 : implicit_debug_sections &= ~section_info;
11444 : : }
11445 : :
11446 : : /* Look through all the sections for the debugging sections to print. */
11447 : : Elf_Scn *scn = NULL;
11448 [ + + ]: 8314 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
11449 : : {
11450 : 8045 : GElf_Shdr shdr_mem;
11451 : 8045 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
11452 : :
11453 [ + - + + ]: 8045 : if (shdr != NULL && shdr->sh_type == SHT_PROGBITS)
11454 : : {
11455 : 3429 : static const struct
11456 : : {
11457 : : const char *name;
11458 : : enum section_e bitmask;
11459 : : void (*fp) (Dwfl_Module *, Ebl *,
11460 : : GElf_Ehdr *, Elf_Scn *, GElf_Shdr *, Dwarf *);
11461 : : } debug_sections[] =
11462 : : {
11463 : : #define NEW_SECTION(name) \
11464 : : { ".debug_" #name, section_##name, print_debug_##name##_section }
11465 : : NEW_SECTION (abbrev),
11466 : : NEW_SECTION (addr),
11467 : : NEW_SECTION (aranges),
11468 : : NEW_SECTION (frame),
11469 : : NEW_SECTION (info),
11470 : : NEW_SECTION (types),
11471 : : NEW_SECTION (line),
11472 : : NEW_SECTION (loc),
11473 : : /* loclists is loc for DWARF5. */
11474 : : { ".debug_loclists", section_loc,
11475 : : print_debug_loclists_section },
11476 : : NEW_SECTION (pubnames),
11477 : : NEW_SECTION (str),
11478 : : /* A DWARF5 specialised debug string section. */
11479 : : { ".debug_line_str", section_str,
11480 : : print_debug_str_section },
11481 : : /* DWARF5 string offsets table. */
11482 : : { ".debug_str_offsets", section_str,
11483 : : print_debug_str_offsets_section },
11484 : : NEW_SECTION (macinfo),
11485 : : NEW_SECTION (macro),
11486 : : NEW_SECTION (ranges),
11487 : : /* rnglists is ranges for DWARF5. */
11488 : : { ".debug_rnglists", section_ranges,
11489 : : print_debug_rnglists_section },
11490 : : { ".eh_frame", section_frame | section_exception,
11491 : : print_debug_frame_section },
11492 : : { ".eh_frame_hdr", section_frame | section_exception,
11493 : : print_debug_frame_hdr_section },
11494 : : { ".gcc_except_table", section_frame | section_exception,
11495 : : print_debug_exception_table },
11496 : : { ".gdb_index", section_gdb_index, print_gdb_index_section }
11497 : : };
11498 : 3429 : const int ndebug_sections = (sizeof (debug_sections)
11499 : : / sizeof (debug_sections[0]));
11500 : 6858 : const char *name = elf_strptr (ebl->elf, shstrndx,
11501 : 3429 : shdr->sh_name);
11502 [ - + ]: 3429 : if (name == NULL)
11503 : 0 : continue;
11504 : :
11505 : : int n;
11506 [ + + ]: 55557 : for (n = 0; n < ndebug_sections; ++n)
11507 : : {
11508 : 53673 : size_t dbglen = strlen (debug_sections[n].name);
11509 : 53673 : size_t scnlen = strlen (name);
11510 [ + + ]: 53673 : if ((strncmp (name, debug_sections[n].name, dbglen) == 0
11511 [ + + ]: 1602 : && (dbglen == scnlen
11512 [ + + ]: 198 : || (scnlen == dbglen + 4
11513 [ + + ]: 180 : && strstr (name, ".dwo") == name + dbglen)))
11514 [ + + + + ]: 52215 : || (name[0] == '.' && name[1] == 'z'
11515 [ + - ]: 566 : && debug_sections[n].name[1] == 'd'
11516 [ + + ]: 566 : && strncmp (&name[2], &debug_sections[n].name[1],
11517 : : dbglen - 1) == 0
11518 [ - + ]: 82 : && (scnlen == dbglen + 1
11519 [ # # ]: 0 : || (scnlen == dbglen + 5
11520 [ # # ]: 0 : && strstr (name, ".dwo") == name + dbglen + 1)))
11521 [ + + ]: 52133 : || (scnlen > 14 /* .gnu.debuglto_ prefix. */
11522 [ + + ]: 3938 : && startswith (name, ".gnu.debuglto_")
11523 [ + + ]: 36 : && strcmp (&name[14], debug_sections[n].name) == 0)
11524 : : )
11525 : : {
11526 : 1545 : if ((print_debug_sections | implicit_debug_sections)
11527 [ + + ]: 1545 : & debug_sections[n].bitmask)
11528 : 511 : debug_sections[n].fp (dwflmod, ebl, ehdr, scn, shdr, dbg);
11529 : : break;
11530 : : }
11531 : : }
11532 : : }
11533 : : }
11534 : :
11535 : 269 : dwfl_end (skel_dwfl);
11536 : 269 : free (skel_name);
11537 : :
11538 : : /* Turn implicit and/or explicit back on in case we go over another file. */
11539 [ + + ]: 269 : if (implicit_info)
11540 : 78 : implicit_debug_sections |= section_info;
11541 [ + + ]: 269 : if (explicit_info)
11542 : 62 : print_debug_sections |= section_info;
11543 : :
11544 : 269 : reset_listptr (&known_locsptr);
11545 : 269 : reset_listptr (&known_loclistsptr);
11546 : 269 : reset_listptr (&known_rangelistptr);
11547 : 269 : reset_listptr (&known_rnglistptr);
11548 : 269 : reset_listptr (&known_addrbases);
11549 : 269 : reset_listptr (&known_stroffbases);
11550 : 269 : }
11551 : :
11552 : :
11553 : : #define ITEM_INDENT 4
11554 : : #define WRAP_COLUMN 75
11555 : :
11556 : : /* Print "NAME: FORMAT", wrapping when output text would make the line
11557 : : exceed WRAP_COLUMN. Unpadded numbers look better for the core items
11558 : : but this function is also used for registers which should be printed
11559 : : aligned. Fortunately registers output uses fixed fields width (such
11560 : : as %11d) for the alignment.
11561 : :
11562 : : Line breaks should not depend on the particular values although that
11563 : : may happen in some cases of the core items. */
11564 : :
11565 : : static unsigned int
11566 : : __attribute__ ((format (printf, 6, 7)))
11567 : 821 : print_core_item (unsigned int colno, char sep, unsigned int wrap,
11568 : : size_t name_width, const char *name, const char *format, ...)
11569 : : {
11570 : 821 : size_t len = strlen (name);
11571 : 821 : if (name_width < len)
11572 : : name_width = len;
11573 : :
11574 : 821 : char *out;
11575 : 821 : va_list ap;
11576 : 821 : va_start (ap, format);
11577 : 821 : int out_len = vasprintf (&out, format, ap);
11578 : 821 : va_end (ap);
11579 [ - + ]: 821 : if (out_len == -1)
11580 : 0 : error_exit (0, _("memory exhausted"));
11581 : :
11582 : 821 : size_t n = name_width + sizeof ": " - 1 + out_len;
11583 : :
11584 [ + + ]: 821 : if (colno == 0)
11585 : : {
11586 : 51 : printf ("%*s", ITEM_INDENT, "");
11587 : 51 : colno = ITEM_INDENT + n;
11588 : : }
11589 [ + + ]: 770 : else if (colno + 2 + n < wrap)
11590 : : {
11591 : 468 : printf ("%c ", sep);
11592 : 468 : colno += 2 + n;
11593 : : }
11594 : : else
11595 : : {
11596 : 302 : printf ("\n%*s", ITEM_INDENT, "");
11597 : 302 : colno = ITEM_INDENT + n;
11598 : : }
11599 : :
11600 : 821 : printf ("%s: %*s%s", name, (int) (name_width - len), "", out);
11601 : :
11602 : 821 : free (out);
11603 : :
11604 : 821 : return colno;
11605 : : }
11606 : :
11607 : : static const void *
11608 : 931 : convert (Elf *core, Elf_Type type, uint_fast16_t count,
11609 : : void *value, const void *data, size_t size)
11610 : : {
11611 : 1862 : Elf_Data valuedata =
11612 : : {
11613 : : .d_type = type,
11614 : : .d_buf = value,
11615 [ + + ]: 931 : .d_size = size ?: gelf_fsize (core, type, count, EV_CURRENT),
11616 : : .d_version = EV_CURRENT,
11617 : : };
11618 : 931 : Elf_Data indata =
11619 : : {
11620 : : .d_type = type,
11621 : : .d_buf = (void *) data,
11622 : : .d_size = valuedata.d_size,
11623 : : .d_version = EV_CURRENT,
11624 : : };
11625 : :
11626 : 931 : Elf_Data *d = (gelf_getclass (core) == ELFCLASS32
11627 [ + + ]: 931 : ? elf32_xlatetom : elf64_xlatetom)
11628 : 931 : (&valuedata, &indata, elf_getident (core, NULL)[EI_DATA]);
11629 [ - + ]: 931 : if (d == NULL)
11630 : 0 : error_exit (0, _("cannot convert core note data: %s"),
11631 : : elf_errmsg (-1));
11632 : :
11633 : 931 : return data + indata.d_size;
11634 : : }
11635 : :
11636 : : typedef uint8_t GElf_Byte;
11637 : :
11638 : : static unsigned int
11639 : 400 : handle_core_item (Elf *core, const Ebl_Core_Item *item, const void *desc,
11640 : : unsigned int colno, size_t *repeated_size)
11641 : : {
11642 [ + + ]: 400 : uint_fast16_t count = item->count ?: 1;
11643 : : /* Ebl_Core_Item count is always a small number.
11644 : : Make sure the backend didn't put in some large bogus value. */
11645 [ - + ]: 62 : assert (count < 128);
11646 : :
11647 : : #define TYPES \
11648 : : DO_TYPE (BYTE, Byte, "0x%.2" PRIx8, "%" PRId8); \
11649 : : DO_TYPE (HALF, Half, "0x%.4" PRIx16, "%" PRId16); \
11650 : : DO_TYPE (WORD, Word, "0x%.8" PRIx32, "%" PRId32); \
11651 : : DO_TYPE (SWORD, Sword, "%" PRId32, "%" PRId32); \
11652 : : DO_TYPE (XWORD, Xword, "0x%.16" PRIx64, "%" PRId64); \
11653 : : DO_TYPE (SXWORD, Sxword, "%" PRId64, "%" PRId64)
11654 : :
11655 : : #define DO_TYPE(NAME, Name, hex, dec) GElf_##Name Name
11656 : 400 : typedef union { TYPES; } value_t;
11657 : 400 : void *data = alloca (count * sizeof (value_t));
11658 : : #undef DO_TYPE
11659 : :
11660 : : #define DO_TYPE(NAME, Name, hex, dec) \
11661 : : GElf_##Name *value_##Name __attribute__((unused)) = data
11662 : 400 : TYPES;
11663 : : #undef DO_TYPE
11664 : :
11665 : 400 : size_t size = gelf_fsize (core, item->type, count, EV_CURRENT);
11666 : 400 : size_t convsize = size;
11667 [ + + ]: 400 : if (repeated_size != NULL)
11668 : : {
11669 [ + - - + ]: 1 : if (*repeated_size > size && (item->format == 'b' || item->format == 'B'))
11670 : : {
11671 : 0 : data = alloca (*repeated_size);
11672 : 0 : count *= *repeated_size / size;
11673 : 0 : convsize = count * size;
11674 : 0 : *repeated_size -= convsize;
11675 : : }
11676 [ + - - + ]: 1 : else if (item->count != 0 || item->format != '\n')
11677 : 0 : *repeated_size -= size;
11678 : : }
11679 : :
11680 : 400 : convert (core, item->type, count, data, desc + item->offset, convsize);
11681 : :
11682 : 400 : Elf_Type type = item->type;
11683 [ + + ]: 400 : if (type == ELF_T_ADDR)
11684 [ + - ]: 1 : type = gelf_getclass (core) == ELFCLASS32 ? ELF_T_WORD : ELF_T_XWORD;
11685 : :
11686 [ + + + + : 400 : switch (item->format)
+ + + -
+ ]
11687 : : {
11688 : 193 : case 'd':
11689 [ - + ]: 193 : assert (count == 1);
11690 [ + + + + : 193 : switch (type)
- + - ]
11691 : : {
11692 : : #define DO_TYPE(NAME, Name, hex, dec) \
11693 : : case ELF_T_##NAME: \
11694 : : colno = print_core_item (colno, ',', WRAP_COLUMN, \
11695 : : 0, item->name, dec, value_##Name[0]); \
11696 : : break
11697 : 193 : TYPES;
11698 : : #undef DO_TYPE
11699 : 0 : default:
11700 : 0 : abort ();
11701 : : }
11702 : : break;
11703 : :
11704 : 111 : case 'x':
11705 [ - + ]: 111 : assert (count == 1);
11706 [ - - + - : 111 : switch (type)
+ - - ]
11707 : : {
11708 : : #define DO_TYPE(NAME, Name, hex, dec) \
11709 : : case ELF_T_##NAME: \
11710 : : colno = print_core_item (colno, ',', WRAP_COLUMN, \
11711 : : 0, item->name, hex, value_##Name[0]); \
11712 : : break
11713 : 111 : TYPES;
11714 : : #undef DO_TYPE
11715 : 0 : default:
11716 : 0 : abort ();
11717 : : }
11718 : : break;
11719 : :
11720 : 22 : case 'b':
11721 : : case 'B':
11722 [ - + ]: 22 : assert (size % sizeof (unsigned int) == 0);
11723 : 22 : unsigned int nbits = count * size * 8;
11724 : 22 : unsigned int pop = 0;
11725 [ + + ]: 56 : for (const unsigned int *i = data; (void *) i < data + count * size; ++i)
11726 : 34 : pop += __builtin_popcount (*i);
11727 : 22 : bool negate = pop > nbits / 2;
11728 : 22 : const unsigned int bias = item->format == 'b';
11729 : :
11730 : 22 : {
11731 [ - + ]: 22 : char printed[(negate ? nbits - pop : pop) * 16 + 1];
11732 : 22 : char *p = printed;
11733 : 22 : *p = '\0';
11734 : :
11735 : 22 : if (BYTE_ORDER != LITTLE_ENDIAN && size > sizeof (unsigned int))
11736 : : {
11737 : : assert (size == sizeof (unsigned int) * 2);
11738 : : for (unsigned int *i = data;
11739 : : (void *) i < data + count * size; i += 2)
11740 : : {
11741 : : unsigned int w = i[1];
11742 : : i[1] = i[0];
11743 : : i[0] = w;
11744 : : }
11745 : : }
11746 : :
11747 : 22 : unsigned int lastbit = 0;
11748 : 22 : unsigned int run = 0;
11749 : 22 : for (const unsigned int *i = data;
11750 [ + + ]: 56 : (void *) i < data + count * size; ++i)
11751 : : {
11752 : 34 : unsigned int bit = ((void *) i - data) * 8;
11753 [ - + ]: 34 : unsigned int w = negate ? ~*i : *i;
11754 [ - + ]: 34 : while (w != 0)
11755 : : {
11756 : : /* Note that a right shift equal to (or greater than)
11757 : : the number of bits of w is undefined behaviour. In
11758 : : particular when the least significant bit is bit 32
11759 : : (w = 0x8000000) then w >>= n is undefined. So
11760 : : explicitly handle that case separately. */
11761 : 0 : unsigned int n = ffs (w);
11762 [ # # ]: 0 : if (n < sizeof (w) * 8)
11763 : 0 : w >>= n;
11764 : : else
11765 : : w = 0;
11766 : 0 : bit += n;
11767 : :
11768 [ # # # # ]: 0 : if (lastbit != 0 && lastbit + 1 == bit)
11769 : 0 : ++run;
11770 : : else
11771 : : {
11772 [ # # ]: 0 : if (lastbit == 0)
11773 : 0 : p += sprintf (p, "%u", bit - bias);
11774 [ # # ]: 0 : else if (run == 0)
11775 : 0 : p += sprintf (p, ",%u", bit - bias);
11776 : : else
11777 : 0 : p += sprintf (p, "-%u,%u", lastbit - bias, bit - bias);
11778 : : run = 0;
11779 : : }
11780 : :
11781 : : lastbit = bit;
11782 : : }
11783 : : }
11784 [ - + - - ]: 22 : if (lastbit > 0 && run > 0 && lastbit + 1 != nbits)
11785 : 0 : p += sprintf (p, "-%u", lastbit - bias);
11786 : :
11787 [ + - ]: 44 : colno = print_core_item (colno, ',', WRAP_COLUMN, 0, item->name,
11788 : : negate ? "~<%s>" : "<%s>", printed);
11789 : : }
11790 : 22 : break;
11791 : :
11792 : 44 : case 'T':
11793 : : case (char) ('T'|0x80):
11794 [ - + ]: 44 : assert (count == 2);
11795 : 44 : Dwarf_Word sec;
11796 : 44 : Dwarf_Word usec;
11797 [ - - + - : 44 : switch (type)
+ - - ]
11798 : : {
11799 : : #define DO_TYPE(NAME, Name, hex, dec) \
11800 : : case ELF_T_##NAME: \
11801 : : sec = value_##Name[0]; \
11802 : : usec = value_##Name[1]; \
11803 : : break
11804 : 44 : TYPES;
11805 : : #undef DO_TYPE
11806 : 0 : default:
11807 : 0 : abort ();
11808 : : }
11809 [ - + ]: 44 : if (unlikely (item->format == (char) ('T'|0x80)))
11810 : : {
11811 : : /* This is a hack for an ill-considered 64-bit ABI where
11812 : : tv_usec is actually a 32-bit field with 32 bits of padding
11813 : : rounding out struct timeval. We've already converted it as
11814 : : a 64-bit field. For little-endian, this just means the
11815 : : high half is the padding; it's presumably zero, but should
11816 : : be ignored anyway. For big-endian, it means the 32-bit
11817 : : field went into the high half of USEC. */
11818 : 0 : GElf_Ehdr ehdr_mem;
11819 : 0 : GElf_Ehdr *ehdr = gelf_getehdr (core, &ehdr_mem);
11820 [ # # ]: 0 : if (likely (ehdr->e_ident[EI_DATA] == ELFDATA2MSB))
11821 : 0 : usec >>= 32;
11822 : : else
11823 : 0 : usec &= UINT32_MAX;
11824 : : }
11825 : 44 : colno = print_core_item (colno, ',', WRAP_COLUMN, 0, item->name,
11826 : : "%" PRIu64 ".%.6" PRIu64, sec, usec);
11827 : 44 : break;
11828 : :
11829 : 9 : case 'c':
11830 [ - + ]: 9 : assert (count == 1);
11831 : 9 : colno = print_core_item (colno, ',', WRAP_COLUMN, 0, item->name,
11832 : 9 : "%c", value_Byte[0]);
11833 : 9 : break;
11834 : :
11835 : 18 : case 's':
11836 : 18 : colno = print_core_item (colno, ',', WRAP_COLUMN, 0, item->name,
11837 : : "%.*s", (int) count, value_Byte);
11838 : 18 : break;
11839 : :
11840 : 1 : case '\n':
11841 : : /* This is a list of strings separated by '\n'. */
11842 [ - + ]: 1 : assert (item->count == 0);
11843 [ - + ]: 1 : assert (repeated_size != NULL);
11844 [ - + ]: 1 : assert (item->name == NULL);
11845 [ - + ]: 1 : if (unlikely (item->offset >= *repeated_size))
11846 : : break;
11847 : :
11848 : 1 : const char *s = desc + item->offset;
11849 : 1 : size = *repeated_size - item->offset;
11850 : 1 : *repeated_size = 0;
11851 [ + - ]: 48 : while (size > 0)
11852 : : {
11853 : 48 : const char *eol = memchr (s, '\n', size);
11854 : 48 : int len = size;
11855 [ + + ]: 48 : if (eol != NULL)
11856 : 47 : len = eol - s;
11857 : 48 : printf ("%*s%.*s\n", ITEM_INDENT, "", len, s);
11858 [ + + ]: 48 : if (eol == NULL)
11859 : : break;
11860 : 47 : size -= eol + 1 - s;
11861 : 47 : s = eol + 1;
11862 : : }
11863 : :
11864 : : colno = WRAP_COLUMN;
11865 : : break;
11866 : :
11867 : : case 'h':
11868 : : break;
11869 : :
11870 : 0 : default:
11871 : 0 : error (0, 0, "XXX not handling format '%c' for %s",
11872 : : item->format, item->name);
11873 : : break;
11874 : : }
11875 : :
11876 : : #undef TYPES
11877 : :
11878 : 0 : return colno;
11879 : : }
11880 : :
11881 : :
11882 : : /* Sort items by group, and by layout offset within each group. */
11883 : : static int
11884 : 895 : compare_core_items (const void *a, const void *b)
11885 : : {
11886 : 895 : const Ebl_Core_Item *const *p1 = a;
11887 : 895 : const Ebl_Core_Item *const *p2 = b;
11888 : 895 : const Ebl_Core_Item *item1 = *p1;
11889 : 895 : const Ebl_Core_Item *item2 = *p2;
11890 : :
11891 : 895 : return ((item1->group == item2->group ? 0
11892 [ + + ]: 895 : : strcmp (item1->group, item2->group))
11893 [ - + ]: 895 : ?: (int) item1->offset - (int) item2->offset);
11894 : : }
11895 : :
11896 : : /* Sort item groups by layout offset of the first item in the group. */
11897 : : static int
11898 : 139 : compare_core_item_groups (const void *a, const void *b)
11899 : : {
11900 : 139 : const Ebl_Core_Item *const *const *p1 = a;
11901 : 139 : const Ebl_Core_Item *const *const *p2 = b;
11902 : 139 : const Ebl_Core_Item *const *group1 = *p1;
11903 : 139 : const Ebl_Core_Item *const *group2 = *p2;
11904 : 139 : const Ebl_Core_Item *item1 = *group1;
11905 : 139 : const Ebl_Core_Item *item2 = *group2;
11906 : :
11907 : 139 : return (int) item1->offset - (int) item2->offset;
11908 : : }
11909 : :
11910 : : static unsigned int
11911 : 37 : handle_core_items (Elf *core, const void *desc, size_t descsz,
11912 : : const Ebl_Core_Item *items, size_t nitems)
11913 : : {
11914 [ + + ]: 37 : if (nitems == 0)
11915 : : return 0;
11916 : 34 : unsigned int colno = 0;
11917 : :
11918 : : /* FORMAT '\n' makes sense to be present only as a single item as it
11919 : : processes all the data of a note. FORMATs 'b' and 'B' have a special case
11920 : : if present as a single item but they can be also processed with other
11921 : : items below. */
11922 [ + + + + ]: 34 : if (nitems == 1 && (items[0].format == '\n' || items[0].format == 'b'
11923 [ + - ]: 8 : || items[0].format == 'B'))
11924 : : {
11925 [ - + ]: 1 : assert (items[0].offset == 0);
11926 : 1 : size_t size = descsz;
11927 : 1 : colno = handle_core_item (core, items, desc, colno, &size);
11928 : : /* If SIZE is not zero here there is some remaining data. But we do not
11929 : : know how to process it anyway. */
11930 : 1 : return colno;
11931 : : }
11932 [ + + ]: 424 : for (size_t i = 0; i < nitems; ++i)
11933 [ - + ]: 391 : assert (items[i].format != '\n');
11934 : :
11935 : : /* Sort to collect the groups together. */
11936 : 33 : const Ebl_Core_Item *sorted_items[nitems];
11937 [ + + ]: 424 : for (size_t i = 0; i < nitems; ++i)
11938 : 391 : sorted_items[i] = &items[i];
11939 : 33 : qsort (sorted_items, nitems, sizeof sorted_items[0], &compare_core_items);
11940 : :
11941 : : /* Collect the unique groups and sort them. */
11942 : 33 : const Ebl_Core_Item **groups[nitems];
11943 : 33 : groups[0] = &sorted_items[0];
11944 : 33 : size_t ngroups = 1;
11945 [ + + ]: 391 : for (size_t i = 1; i < nitems; ++i)
11946 [ + + ]: 358 : if (sorted_items[i]->group != sorted_items[i - 1]->group
11947 [ + - ]: 75 : && strcmp (sorted_items[i]->group, sorted_items[i - 1]->group))
11948 : 75 : groups[ngroups++] = &sorted_items[i];
11949 : 33 : qsort (groups, ngroups, sizeof groups[0], &compare_core_item_groups);
11950 : :
11951 : : /* Write out all the groups. */
11952 : 33 : const void *last = desc;
11953 : 35 : do
11954 : : {
11955 [ + + ]: 145 : for (size_t i = 0; i < ngroups; ++i)
11956 : : {
11957 : 110 : for (const Ebl_Core_Item **item = groups[i];
11958 [ + + ]: 509 : (item < &sorted_items[nitems]
11959 [ + + ]: 474 : && ((*item)->group == groups[i][0]->group
11960 [ - + ]: 75 : || !strcmp ((*item)->group, groups[i][0]->group)));
11961 : 399 : ++item)
11962 : 399 : colno = handle_core_item (core, *item, desc, colno, NULL);
11963 : :
11964 : : /* Force a line break at the end of the group. */
11965 : 110 : colno = WRAP_COLUMN;
11966 : : }
11967 : :
11968 [ + + ]: 35 : if (descsz == 0)
11969 : : break;
11970 : :
11971 : : /* This set of items consumed a certain amount of the note's data.
11972 : : If there is more data there, we have another unit of the same size.
11973 : : Loop to print that out too. */
11974 : 20 : const Ebl_Core_Item *item = &items[nitems - 1];
11975 : 40 : size_t eltsz = item->offset + gelf_fsize (core, item->type,
11976 : 20 : item->count ?: 1, EV_CURRENT);
11977 : :
11978 : 20 : int reps = -1;
11979 : 20 : do
11980 : : {
11981 : 20 : ++reps;
11982 : 20 : desc += eltsz;
11983 : 20 : descsz -= eltsz;
11984 : : }
11985 [ + + - + ]: 20 : while (descsz >= eltsz && !memcmp (desc, last, eltsz));
11986 : :
11987 [ + - ]: 20 : if (reps == 1)
11988 : : {
11989 : : /* For just one repeat, print it unabridged twice. */
11990 : 20 : desc -= eltsz;
11991 : 20 : descsz += eltsz;
11992 : : }
11993 [ - + ]: 20 : else if (reps > 1)
11994 : 0 : printf (_("\n%*s... <repeats %u more times> ..."),
11995 : : ITEM_INDENT, "", reps);
11996 : :
11997 : 20 : last = desc;
11998 : : }
11999 [ + + ]: 20 : while (descsz > 0);
12000 : :
12001 : 33 : return colno;
12002 : : }
12003 : :
12004 : : static unsigned int
12005 : 0 : handle_bit_registers (const Ebl_Register_Location *regloc, const void *desc,
12006 : : unsigned int colno)
12007 : : {
12008 : 0 : desc += regloc->offset;
12009 : :
12010 : 0 : abort (); /* XXX */
12011 : : return colno;
12012 : : }
12013 : :
12014 : :
12015 : : static unsigned int
12016 : 162 : handle_core_register (Ebl *ebl, Elf *core, int maxregname,
12017 : : const Ebl_Register_Location *regloc, const void *desc,
12018 : : unsigned int colno)
12019 : : {
12020 [ - + ]: 162 : if (regloc->bits % 8 != 0)
12021 : 0 : return handle_bit_registers (regloc, desc, colno);
12022 : :
12023 : 162 : desc += regloc->offset;
12024 : :
12025 [ + + ]: 586 : for (int reg = regloc->regno; reg < regloc->regno + regloc->count; ++reg)
12026 : : {
12027 : 424 : char name[REGNAMESZ];
12028 : 424 : int bits;
12029 : 424 : int type;
12030 : 424 : register_info (ebl, reg, regloc, name, &bits, &type);
12031 : :
12032 : : #define TYPES \
12033 : : BITS (8, BYTE, "%4" PRId8, "0x%.2" PRIx8); \
12034 : : BITS (16, HALF, "%6" PRId16, "0x%.4" PRIx16); \
12035 : : BITS (32, WORD, "%11" PRId32, " 0x%.8" PRIx32); \
12036 : : BITS (64, XWORD, "%20" PRId64, " 0x%.16" PRIx64)
12037 : :
12038 : : #define BITS(bits, xtype, sfmt, ufmt) \
12039 : : uint##bits##_t b##bits; int##bits##_t b##bits##s
12040 : 424 : union { TYPES; uint64_t b128[2]; } value;
12041 : : #undef BITS
12042 : :
12043 [ + + ]: 424 : switch (type)
12044 : : {
12045 : 336 : case DW_ATE_unsigned:
12046 : : case DW_ATE_signed:
12047 : : case DW_ATE_address:
12048 [ - + + + : 336 : switch (bits)
+ - ]
12049 : : {
12050 : : #define BITS(bits, xtype, sfmt, ufmt) \
12051 : : case bits: \
12052 : : desc = convert (core, ELF_T_##xtype, 1, &value, desc, 0); \
12053 : : if (type == DW_ATE_signed) \
12054 : : colno = print_core_item (colno, ' ', WRAP_COLUMN, \
12055 : : maxregname, name, \
12056 : : sfmt, value.b##bits##s); \
12057 : : else \
12058 : : colno = print_core_item (colno, ' ', WRAP_COLUMN, \
12059 : : maxregname, name, \
12060 : : ufmt, value.b##bits); \
12061 : : break
12062 : :
12063 [ - - - + : 288 : TYPES;
+ + + + ]
12064 : :
12065 : 48 : case 128:
12066 [ - + ]: 48 : assert (type == DW_ATE_unsigned);
12067 : 48 : desc = convert (core, ELF_T_XWORD, 2, &value, desc, 0);
12068 : 48 : int be = elf_getident (core, NULL)[EI_DATA] == ELFDATA2MSB;
12069 : 48 : colno = print_core_item (colno, ' ', WRAP_COLUMN,
12070 : : maxregname, name,
12071 : : "0x%.16" PRIx64 "%.16" PRIx64,
12072 : 48 : value.b128[!be], value.b128[be]);
12073 : 48 : break;
12074 : :
12075 : 0 : default:
12076 : 0 : abort ();
12077 : : #undef BITS
12078 : : }
12079 : : break;
12080 : :
12081 : 88 : default:
12082 : : /* Print each byte in hex, the whole thing in native byte order. */
12083 [ - + ]: 88 : assert (bits % 8 == 0);
12084 : 88 : const uint8_t *bytes = desc;
12085 : 88 : desc += bits / 8;
12086 : 88 : char hex[bits / 4 + 1];
12087 : 88 : hex[bits / 4] = '\0';
12088 : 88 : int incr = 1;
12089 [ + + ]: 88 : if (elf_getident (core, NULL)[EI_DATA] == ELFDATA2LSB)
12090 : : {
12091 : 48 : bytes += bits / 8 - 1;
12092 : 48 : incr = -1;
12093 : : }
12094 : 88 : size_t idx = 0;
12095 [ + + ]: 872 : for (char *h = hex; bits > 0; bits -= 8, idx += incr)
12096 : : {
12097 : 784 : *h++ = "0123456789abcdef"[bytes[idx] >> 4];
12098 : 784 : *h++ = "0123456789abcdef"[bytes[idx] & 0xf];
12099 : : }
12100 : 88 : colno = print_core_item (colno, ' ', WRAP_COLUMN,
12101 : : maxregname, name, "0x%s", hex);
12102 : 88 : break;
12103 : : }
12104 : 424 : desc += regloc->pad;
12105 : :
12106 : : #undef TYPES
12107 : : }
12108 : :
12109 : 162 : return colno;
12110 : : }
12111 : :
12112 : :
12113 : : struct register_info
12114 : : {
12115 : : const Ebl_Register_Location *regloc;
12116 : : const char *set;
12117 : : char name[REGNAMESZ];
12118 : : int regno;
12119 : : int bits;
12120 : : int type;
12121 : : };
12122 : :
12123 : : static int
12124 : 2066 : register_bitpos (const struct register_info *r)
12125 : : {
12126 : 2066 : return (r->regloc->offset * 8
12127 : 2066 : + ((r->regno - r->regloc->regno)
12128 : 2066 : * (r->regloc->bits + r->regloc->pad * 8)));
12129 : : }
12130 : :
12131 : : static int
12132 : 1062 : compare_sets_by_info (const struct register_info *r1,
12133 : : const struct register_info *r2)
12134 : : {
12135 : 1062 : return ((int) r2->bits - (int) r1->bits
12136 [ + + ]: 1062 : ?: register_bitpos (r1) - register_bitpos (r2));
12137 : : }
12138 : :
12139 : : /* Sort registers by set, and by size and layout offset within each set. */
12140 : : static int
12141 : 4595 : compare_registers (const void *a, const void *b)
12142 : : {
12143 : 4595 : const struct register_info *r1 = a;
12144 : 4595 : const struct register_info *r2 = b;
12145 : :
12146 : : /* Unused elements sort last. */
12147 [ + + ]: 4595 : if (r1->regloc == NULL)
12148 : 3309 : return r2->regloc == NULL ? 0 : 1;
12149 [ + + ]: 1286 : if (r2->regloc == NULL)
12150 : : return -1;
12151 : :
12152 [ + + ]: 1121 : return ((r1->set == r2->set ? 0 : strcmp (r1->set, r2->set))
12153 [ - + ]: 1121 : ?: compare_sets_by_info (r1, r2));
12154 : : }
12155 : :
12156 : : /* Sort register sets by layout offset of the first register in the set. */
12157 : : static int
12158 : 20 : compare_register_sets (const void *a, const void *b)
12159 : : {
12160 : 20 : const struct register_info *const *p1 = a;
12161 : 20 : const struct register_info *const *p2 = b;
12162 : 20 : return compare_sets_by_info (*p1, *p2);
12163 : : }
12164 : :
12165 : : static inline bool
12166 : 864 : same_set (const struct register_info *a,
12167 : : const struct register_info *b,
12168 : : const struct register_info *regs,
12169 : : size_t maxnreg)
12170 : : {
12171 [ + - ]: 864 : return (a < ®s[maxnreg] && a->regloc != NULL
12172 [ + - + + ]: 864 : && b < ®s[maxnreg] && b->regloc != NULL
12173 [ + + ]: 846 : && a->bits == b->bits
12174 [ + - + + : 1692 : && (a->set == b->set || !strcmp (a->set, b->set)));
+ - ]
12175 : : }
12176 : :
12177 : : static unsigned int
12178 : 37 : handle_core_registers (Ebl *ebl, Elf *core, const void *desc,
12179 : : const Ebl_Register_Location *reglocs, size_t nregloc)
12180 : : {
12181 [ + + ]: 37 : if (nregloc == 0)
12182 : : return 0;
12183 : :
12184 : 18 : ssize_t maxnreg = ebl_register_info (ebl, 0, NULL, 0, NULL, NULL, NULL, NULL);
12185 [ - + ]: 18 : if (maxnreg <= 0)
12186 : : {
12187 [ # # ]: 0 : for (size_t i = 0; i < nregloc; ++i)
12188 : 0 : if (maxnreg < reglocs[i].regno + reglocs[i].count)
12189 : : maxnreg = reglocs[i].regno + reglocs[i].count;
12190 [ # # ]: 0 : assert (maxnreg > 0);
12191 : : }
12192 : :
12193 : 18 : struct register_info regs[maxnreg];
12194 : 18 : memset (regs, 0, sizeof regs);
12195 : :
12196 : : /* Sort to collect the sets together. */
12197 : 18 : int maxreg = 0;
12198 [ + + ]: 180 : for (size_t i = 0; i < nregloc; ++i)
12199 : 162 : for (int reg = reglocs[i].regno;
12200 [ + + ]: 586 : reg < reglocs[i].regno + reglocs[i].count;
12201 : 424 : ++reg)
12202 : : {
12203 [ - + ]: 424 : assert (reg < maxnreg);
12204 : 424 : if (reg > maxreg)
12205 : : maxreg = reg;
12206 : 424 : struct register_info *info = ®s[reg];
12207 : 424 : info->regloc = ®locs[i];
12208 : 424 : info->regno = reg;
12209 : 424 : info->set = register_info (ebl, reg, ®locs[i],
12210 : 424 : info->name, &info->bits, &info->type);
12211 : : }
12212 : 18 : qsort (regs, maxreg + 1, sizeof regs[0], &compare_registers);
12213 : :
12214 : : /* Collect the unique sets and sort them. */
12215 : 18 : struct register_info *sets[maxreg + 1];
12216 : 18 : sets[0] = ®s[0];
12217 : 18 : size_t nsets = 1;
12218 [ + + ]: 1279 : for (int i = 1; i <= maxreg; ++i)
12219 [ + + ]: 1261 : if (regs[i].regloc != NULL
12220 [ + + ]: 406 : && !same_set (®s[i], ®s[i - 1], regs, maxnreg))
12221 : 16 : sets[nsets++] = ®s[i];
12222 : 18 : qsort (sets, nsets, sizeof sets[0], &compare_register_sets);
12223 : :
12224 : : /* Write out all the sets. */
12225 : 18 : unsigned int colno = 0;
12226 [ + + ]: 52 : for (size_t i = 0; i < nsets; ++i)
12227 : : {
12228 : : /* Find the longest name of a register in this set. */
12229 : 34 : size_t maxname = 0;
12230 : 34 : const struct register_info *end;
12231 [ + + ]: 458 : for (end = sets[i]; same_set (sets[i], end, regs, maxnreg); ++end)
12232 : : {
12233 : 424 : size_t len = strlen (end->name);
12234 : 424 : if (len > maxname)
12235 : : maxname = len;
12236 : : }
12237 : :
12238 : 196 : for (const struct register_info *reg = sets[i];
12239 [ + + ]: 196 : reg < end;
12240 : 162 : reg += reg->regloc->count ?: 1)
12241 [ + - ]: 162 : colno = handle_core_register (ebl, core, maxname,
12242 : : reg->regloc, desc, colno);
12243 : :
12244 : : /* Force a line break at the end of the group. */
12245 : 34 : colno = WRAP_COLUMN;
12246 : : }
12247 : :
12248 : 18 : return colno;
12249 : : }
12250 : :
12251 : : static void
12252 : 9 : handle_auxv_note (Ebl *ebl, Elf *core, GElf_Word descsz, GElf_Off desc_pos)
12253 : : {
12254 : 9 : Elf_Data *data = elf_getdata_rawchunk (core, desc_pos, descsz, ELF_T_AUXV);
12255 [ - + ]: 9 : if (data == NULL)
12256 : 0 : elf_error:
12257 : 0 : error_exit (0, _("cannot convert core note data: %s"), elf_errmsg (-1));
12258 : :
12259 : 9 : const size_t nauxv = descsz / gelf_fsize (core, ELF_T_AUXV, 1, EV_CURRENT);
12260 [ + + ]: 177 : for (size_t i = 0; i < nauxv; ++i)
12261 : : {
12262 : 168 : GElf_auxv_t av_mem;
12263 : 168 : GElf_auxv_t *av = gelf_getauxv (data, i, &av_mem);
12264 [ - + ]: 168 : if (av == NULL)
12265 : 0 : goto elf_error;
12266 : :
12267 : 168 : const char *name;
12268 : 168 : const char *fmt;
12269 [ - + ]: 168 : if (ebl_auxv_info (ebl, av->a_type, &name, &fmt) == 0)
12270 : : {
12271 : : /* Unknown type. */
12272 [ # # ]: 0 : if (av->a_un.a_val == 0)
12273 : 0 : printf (" %" PRIu64 "\n", av->a_type);
12274 : : else
12275 : 0 : printf (" %" PRIu64 ": %#" PRIx64 "\n",
12276 : : av->a_type, av->a_un.a_val);
12277 : : }
12278 : : else
12279 [ + + + - : 168 : switch (fmt[0])
+ - ]
12280 : : {
12281 : 9 : case '\0': /* Normally zero. */
12282 [ + - ]: 9 : if (av->a_un.a_val == 0)
12283 : : {
12284 : 9 : printf (" %s\n", name);
12285 : : break;
12286 : : }
12287 : 74 : FALLTHROUGH;
12288 : : case 'x': /* hex */
12289 : : case 'p': /* address */
12290 : : case 's': /* address of string */
12291 : 74 : printf (" %s: %#" PRIx64 "\n", name, av->a_un.a_val);
12292 : : break;
12293 : 81 : case 'u':
12294 : 81 : printf (" %s: %" PRIu64 "\n", name, av->a_un.a_val);
12295 : : break;
12296 : 0 : case 'd':
12297 : 0 : printf (" %s: %" PRId64 "\n", name, av->a_un.a_val);
12298 : : break;
12299 : :
12300 : 4 : case 'b':
12301 : 4 : printf (" %s: %#" PRIx64 " ", name, av->a_un.a_val);
12302 : 4 : GElf_Xword bit = 1;
12303 : 4 : const char *pfx = "<";
12304 [ + + ]: 110 : for (const char *p = fmt + 1; *p != 0; p = strchr (p, '\0') + 1)
12305 : : {
12306 [ + + ]: 106 : if (av->a_un.a_val & bit)
12307 : : {
12308 : 77 : printf ("%s%s", pfx, p);
12309 : 77 : pfx = " ";
12310 : : }
12311 : 106 : bit <<= 1;
12312 : : }
12313 : 168 : printf (">\n");
12314 : : break;
12315 : :
12316 : 0 : default:
12317 : 0 : abort ();
12318 : : }
12319 : : }
12320 : 9 : }
12321 : :
12322 : : static bool
12323 : 195 : buf_has_data (unsigned char const *ptr, unsigned char const *end, size_t sz)
12324 : : {
12325 [ - + - + ]: 195 : return ptr < end && (size_t) (end - ptr) >= sz;
12326 : : }
12327 : :
12328 : : static bool
12329 : 18 : buf_read_int (Elf *core, unsigned char const **ptrp, unsigned char const *end,
12330 : : int *retp)
12331 : : {
12332 [ + - + - ]: 36 : if (! buf_has_data (*ptrp, end, 4))
12333 : : return false;
12334 : :
12335 : 18 : *ptrp = convert (core, ELF_T_WORD, 1, retp, *ptrp, 4);
12336 : 18 : return true;
12337 : : }
12338 : :
12339 : : static bool
12340 : 177 : buf_read_ulong (Elf *core, unsigned char const **ptrp, unsigned char const *end,
12341 : : uint64_t *retp)
12342 : : {
12343 : 177 : size_t sz = gelf_fsize (core, ELF_T_ADDR, 1, EV_CURRENT);
12344 [ + - + - ]: 354 : if (! buf_has_data (*ptrp, end, sz))
12345 : : return false;
12346 : :
12347 : 177 : union
12348 : : {
12349 : : uint64_t u64;
12350 : : uint32_t u32;
12351 : : } u;
12352 : :
12353 : 177 : *ptrp = convert (core, ELF_T_ADDR, 1, &u, *ptrp, sz);
12354 : :
12355 [ + + ]: 177 : if (sz == 4)
12356 : 93 : *retp = u.u32;
12357 : : else
12358 : 84 : *retp = u.u64;
12359 : : return true;
12360 : : }
12361 : :
12362 : : static void
12363 : 6 : handle_siginfo_note (Elf *core, GElf_Word descsz, GElf_Off desc_pos)
12364 : : {
12365 : 6 : Elf_Data *data = elf_getdata_rawchunk (core, desc_pos, descsz, ELF_T_BYTE);
12366 [ - + ]: 6 : if (data == NULL)
12367 : 0 : error_exit (0, _("cannot convert core note data: %s"), elf_errmsg (-1));
12368 : :
12369 : 6 : unsigned char const *ptr = data->d_buf;
12370 : 6 : unsigned char const *const end = data->d_buf + data->d_size;
12371 : :
12372 : : /* Siginfo head is three ints: signal number, error number, origin
12373 : : code. */
12374 : 6 : int si_signo, si_errno, si_code;
12375 [ - + ]: 6 : if (! buf_read_int (core, &ptr, end, &si_signo)
12376 [ - + ]: 6 : || ! buf_read_int (core, &ptr, end, &si_errno)
12377 [ - + ]: 6 : || ! buf_read_int (core, &ptr, end, &si_code))
12378 : : {
12379 : 0 : fail:
12380 : 0 : printf (" Not enough data in NT_SIGINFO note.\n");
12381 : 0 : return;
12382 : : }
12383 : :
12384 : : /* Next is a pointer-aligned union of structures. On 64-bit
12385 : : machines, that implies a word of padding. */
12386 [ + + ]: 6 : if (gelf_getclass (core) == ELFCLASS64)
12387 : 3 : ptr += 4;
12388 : :
12389 : 6 : printf (" si_signo: %d, si_errno: %d, si_code: %d\n",
12390 : : si_signo, si_errno, si_code);
12391 : :
12392 [ + - ]: 6 : if (si_code > 0)
12393 [ + - ]: 6 : switch (si_signo)
12394 : : {
12395 : 6 : case CORE_SIGILL:
12396 : : case CORE_SIGFPE:
12397 : : case CORE_SIGSEGV:
12398 : : case CORE_SIGBUS:
12399 : : {
12400 : 6 : uint64_t addr;
12401 [ - + ]: 6 : if (! buf_read_ulong (core, &ptr, end, &addr))
12402 : 0 : goto fail;
12403 : 6 : printf (" fault address: %#" PRIx64 "\n", addr);
12404 : 6 : break;
12405 : : }
12406 : : default:
12407 : : ;
12408 : : }
12409 [ # # ]: 0 : else if (si_code == CORE_SI_USER)
12410 : : {
12411 : 0 : int pid, uid;
12412 [ # # ]: 0 : if (! buf_read_int (core, &ptr, end, &pid)
12413 [ # # ]: 0 : || ! buf_read_int (core, &ptr, end, &uid))
12414 : 0 : goto fail;
12415 : 0 : printf (" sender PID: %d, sender UID: %d\n", pid, uid);
12416 : : }
12417 : : }
12418 : :
12419 : : static void
12420 : 6 : handle_file_note (Elf *core, GElf_Word descsz, GElf_Off desc_pos)
12421 : : {
12422 : 6 : Elf_Data *data = elf_getdata_rawchunk (core, desc_pos, descsz, ELF_T_BYTE);
12423 [ - + ]: 6 : if (data == NULL)
12424 : 0 : error_exit (0, _("cannot convert core note data: %s"), elf_errmsg (-1));
12425 : :
12426 : 6 : unsigned char const *ptr = data->d_buf;
12427 : 6 : unsigned char const *const end = data->d_buf + data->d_size;
12428 : :
12429 : 6 : uint64_t count, page_size;
12430 [ - + ]: 6 : if (! buf_read_ulong (core, &ptr, end, &count)
12431 [ - + ]: 6 : || ! buf_read_ulong (core, &ptr, end, &page_size))
12432 : : {
12433 : 0 : fail:
12434 : 0 : printf (" Not enough data in NT_FILE note.\n");
12435 : 0 : return;
12436 : : }
12437 : :
12438 : 6 : size_t addrsize = gelf_fsize (core, ELF_T_ADDR, 1, EV_CURRENT);
12439 : 6 : uint64_t maxcount = (size_t) (end - ptr) / (3 * addrsize);
12440 [ - + ]: 6 : if (count > maxcount)
12441 : 0 : goto fail;
12442 : :
12443 : : /* Where file names are stored. */
12444 : 6 : unsigned char const *const fstart = ptr + 3 * count * addrsize;
12445 : 6 : char const *fptr = (char *) fstart;
12446 : :
12447 : 6 : printf (" %" PRId64 " files:\n", count);
12448 [ + + ]: 59 : for (uint64_t i = 0; i < count; ++i)
12449 : : {
12450 : 53 : uint64_t mstart, mend, moffset;
12451 [ + - ]: 53 : if (! buf_read_ulong (core, &ptr, fstart, &mstart)
12452 [ + - ]: 53 : || ! buf_read_ulong (core, &ptr, fstart, &mend)
12453 [ - + ]: 53 : || ! buf_read_ulong (core, &ptr, fstart, &moffset))
12454 : 0 : goto fail;
12455 : :
12456 : 53 : const char *fnext = memchr (fptr, '\0', (char *) end - fptr);
12457 [ - + ]: 53 : if (fnext == NULL)
12458 : 0 : goto fail;
12459 : :
12460 : 53 : int ct = printf (" %08" PRIx64 "-%08" PRIx64
12461 : : " %08" PRIx64 " %" PRId64,
12462 : : mstart, mend, moffset * page_size, mend - mstart);
12463 [ + - ]: 53 : printf ("%*s%s\n", ct > 50 ? 3 : 53 - ct, "", fptr);
12464 : :
12465 : 53 : fptr = fnext + 1;
12466 : : }
12467 : : }
12468 : :
12469 : : static void
12470 : 38 : handle_core_note (Ebl *ebl, const GElf_Nhdr *nhdr,
12471 : : const char *name, const void *desc)
12472 : : {
12473 : 38 : GElf_Word regs_offset;
12474 : 38 : size_t nregloc;
12475 : 38 : const Ebl_Register_Location *reglocs;
12476 : 38 : size_t nitems;
12477 : 38 : const Ebl_Core_Item *items;
12478 : :
12479 [ + + ]: 38 : if (! ebl_core_note (ebl, nhdr, name, desc,
12480 : : ®s_offset, &nregloc, ®locs, &nitems, &items))
12481 : 1 : return;
12482 : :
12483 : : /* Pass 0 for DESCSZ when there are registers in the note,
12484 : : so that the ITEMS array does not describe the whole thing.
12485 : : For non-register notes, the actual descsz might be a multiple
12486 : : of the unit size, not just exactly the unit size. */
12487 : 37 : unsigned int colno = handle_core_items (ebl->elf, desc,
12488 [ + + ]: 37 : nregloc == 0 ? nhdr->n_descsz : 0,
12489 : : items, nitems);
12490 [ + + ]: 37 : if (colno != 0)
12491 [ - + ]: 34 : putchar_unlocked ('\n');
12492 : :
12493 : 37 : colno = handle_core_registers (ebl, ebl->elf, desc + regs_offset,
12494 : : reglocs, nregloc);
12495 [ + + ]: 37 : if (colno != 0)
12496 [ - + ]: 55 : putchar_unlocked ('\n');
12497 : : }
12498 : :
12499 : : static void
12500 : 248 : handle_notes_data (Ebl *ebl, const GElf_Ehdr *ehdr,
12501 : : GElf_Off start, Elf_Data *data)
12502 : : {
12503 : 248 : fputs_unlocked (_(" Owner Data size Type\n"), stdout);
12504 : :
12505 [ - + ]: 248 : if (data == NULL)
12506 : 0 : goto bad_note;
12507 : :
12508 : : size_t offset = 0;
12509 : : GElf_Nhdr nhdr;
12510 : : size_t name_offset;
12511 : : size_t desc_offset;
12512 [ + + ]: 2550 : while (offset < data->d_size
12513 [ + - ]: 2302 : && (offset = gelf_getnote (data, offset,
12514 : : &nhdr, &name_offset, &desc_offset)) > 0)
12515 : : {
12516 [ + - ]: 2302 : const char *name = nhdr.n_namesz == 0 ? "" : data->d_buf + name_offset;
12517 : 2302 : const char *desc = data->d_buf + desc_offset;
12518 : :
12519 : : /* GNU Build Attributes are weird, they store most of their data
12520 : : into the owner name field. Extract just the owner name
12521 : : prefix here, then use the rest later as data. */
12522 : 2302 : bool is_gnu_build_attr
12523 : 2302 : = startswith (name, ELF_NOTE_GNU_BUILD_ATTRIBUTE_PREFIX);
12524 : 4604 : const char *print_name = (is_gnu_build_attr
12525 [ + + ]: 2302 : ? ELF_NOTE_GNU_BUILD_ATTRIBUTE_PREFIX : name);
12526 : 4604 : size_t print_namesz = (is_gnu_build_attr
12527 [ + + ]: 2302 : ? strlen (print_name) : nhdr.n_namesz);
12528 : :
12529 : 2302 : char buf[100];
12530 : 2302 : char buf2[100];
12531 : 4604 : printf (_(" %-13.*s %9" PRId32 " %s\n"),
12532 : : (int) print_namesz, print_name, nhdr.n_descsz,
12533 [ + + ]: 2302 : ehdr->e_type == ET_CORE
12534 : 59 : ? ebl_core_note_type_name (ebl, nhdr.n_type,
12535 : : buf, sizeof (buf))
12536 : 2243 : : ebl_object_note_type_name (ebl, name, nhdr.n_type,
12537 : : nhdr.n_descsz,
12538 : : buf2, sizeof (buf2)));
12539 : :
12540 : : /* Filter out invalid entries. */
12541 : 2302 : if (memchr (name, '\0', nhdr.n_namesz) != NULL
12542 : : /* XXX For now help broken Linux kernels. */
12543 : : || 1)
12544 : : {
12545 [ + + ]: 2302 : if (ehdr->e_type == ET_CORE)
12546 : : {
12547 [ + + ]: 59 : if (nhdr.n_type == NT_AUXV
12548 [ + - ]: 9 : && (nhdr.n_namesz == 4 /* Broken old Linux kernels. */
12549 [ + - + - ]: 9 : || (nhdr.n_namesz == 5 && name[4] == '\0'))
12550 [ + - ]: 9 : && !memcmp (name, "CORE", 4))
12551 : 9 : handle_auxv_note (ebl, ebl->elf, nhdr.n_descsz,
12552 : : start + desc_offset);
12553 [ + + + - ]: 50 : else if (nhdr.n_namesz == 5 && strcmp (name, "CORE") == 0)
12554 [ + + + ]: 38 : switch (nhdr.n_type)
12555 : : {
12556 : 6 : case NT_SIGINFO:
12557 : 6 : handle_siginfo_note (ebl->elf, nhdr.n_descsz,
12558 : : start + desc_offset);
12559 : 6 : break;
12560 : :
12561 : 6 : case NT_FILE:
12562 : 6 : handle_file_note (ebl->elf, nhdr.n_descsz,
12563 : : start + desc_offset);
12564 : 6 : break;
12565 : :
12566 : 26 : default:
12567 : 26 : handle_core_note (ebl, &nhdr, name, desc);
12568 : : }
12569 : : else
12570 : 12 : handle_core_note (ebl, &nhdr, name, desc);
12571 : : }
12572 : : else
12573 : 2243 : ebl_object_note (ebl, nhdr.n_namesz, name, nhdr.n_type,
12574 : : nhdr.n_descsz, desc);
12575 : : }
12576 : : }
12577 : :
12578 [ + - ]: 248 : if (offset == data->d_size)
12579 : 248 : return;
12580 : :
12581 : 0 : bad_note:
12582 : 0 : error (0, 0,
12583 [ # # ]: 0 : _("cannot get content of note: %s"),
12584 : 0 : data != NULL ? "garbage data" : elf_errmsg (-1));
12585 : : }
12586 : :
12587 : : static void
12588 : 143 : handle_notes (Ebl *ebl, GElf_Ehdr *ehdr)
12589 : : {
12590 : : /* If we have section headers, just look for SHT_NOTE sections.
12591 : : In a debuginfo file, the program headers are not reliable. */
12592 [ + + ]: 143 : if (shnum != 0)
12593 : : {
12594 : : /* Get the section header string table index. */
12595 : 132 : size_t shstrndx;
12596 [ - + ]: 132 : if (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0)
12597 : 0 : error_exit (0, _("cannot get section header string table index"));
12598 : :
12599 : : Elf_Scn *scn = NULL;
12600 [ + + ]: 3706 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
12601 : : {
12602 : 3574 : GElf_Shdr shdr_mem;
12603 : 3574 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
12604 : :
12605 [ + - + + ]: 3574 : if (shdr == NULL || shdr->sh_type != SHT_NOTE)
12606 : : /* Not what we are looking for. */
12607 : 3338 : continue;
12608 : :
12609 [ - + ]: 236 : if (notes_section != NULL)
12610 : : {
12611 : 0 : char *sname = elf_strptr (ebl->elf, shstrndx, shdr->sh_name);
12612 [ # # # # ]: 0 : if (sname == NULL || strcmp (sname, notes_section) != 0)
12613 : 0 : continue;
12614 : : }
12615 : :
12616 : 472 : printf (_("\
12617 : : \nNote section [%2zu] '%s' of %" PRIu64 " bytes at offset %#0" PRIx64 ":\n"),
12618 : : elf_ndxscn (scn),
12619 : 236 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
12620 : : shdr->sh_size, shdr->sh_offset);
12621 : :
12622 : 236 : handle_notes_data (ebl, ehdr, shdr->sh_offset,
12623 : : elf_getdata (scn, NULL));
12624 : : }
12625 : 132 : return;
12626 : : }
12627 : :
12628 : : /* We have to look through the program header to find the note
12629 : : sections. There can be more than one. */
12630 [ + + ]: 140 : for (size_t cnt = 0; cnt < phnum; ++cnt)
12631 : : {
12632 : 129 : GElf_Phdr mem;
12633 : 129 : GElf_Phdr *phdr = gelf_getphdr (ebl->elf, cnt, &mem);
12634 : :
12635 [ + - + + ]: 129 : if (phdr == NULL || phdr->p_type != PT_NOTE)
12636 : : /* Not what we are looking for. */
12637 : 117 : continue;
12638 : :
12639 : 12 : printf (_("\
12640 : : \nNote segment of %" PRIu64 " bytes at offset %#0" PRIx64 ":\n"),
12641 : : phdr->p_filesz, phdr->p_offset);
12642 : :
12643 : 12 : handle_notes_data (ebl, ehdr, phdr->p_offset,
12644 : : elf_getdata_rawchunk (ebl->elf,
12645 : : phdr->p_offset, phdr->p_filesz,
12646 [ + + ]: 12 : (phdr->p_align == 8
12647 : : ? ELF_T_NHDR8 : ELF_T_NHDR)));
12648 : : }
12649 : : }
12650 : :
12651 : :
12652 : : static void
12653 : 6 : hex_dump (const uint8_t *data, size_t len)
12654 : : {
12655 : 6 : size_t pos = 0;
12656 [ + + ]: 28 : while (pos < len)
12657 : : {
12658 : 22 : printf (" 0x%08zx ", pos);
12659 : :
12660 : 22 : const size_t chunk = MIN (len - pos, 16);
12661 : :
12662 [ + + ]: 344 : for (size_t i = 0; i < chunk; ++i)
12663 [ + + ]: 322 : if (i % 4 == 3)
12664 : 80 : printf ("%02x ", data[pos + i]);
12665 : : else
12666 : 322 : printf ("%02x", data[pos + i]);
12667 : :
12668 [ + + ]: 22 : if (chunk < 16)
12669 : 2 : printf ("%*s", (int) ((16 - chunk) * 2 + (16 - chunk + 3) / 4), "");
12670 : :
12671 [ + + ]: 344 : for (size_t i = 0; i < chunk; ++i)
12672 : : {
12673 : 322 : unsigned char b = data[pos + i];
12674 [ + + ]: 644 : printf ("%c", isprint (b) ? b : '.');
12675 : : }
12676 : :
12677 : 22 : putchar ('\n');
12678 : 22 : pos += chunk;
12679 : : }
12680 : 6 : }
12681 : :
12682 : : static void
12683 : 6 : dump_data_section (Elf_Scn *scn, const GElf_Shdr *shdr, const char *name)
12684 : : {
12685 [ + - - + ]: 6 : if (shdr->sh_size == 0 || shdr->sh_type == SHT_NOBITS)
12686 : 0 : printf (_("\nSection [%zu] '%s' has no data to dump.\n"),
12687 : : elf_ndxscn (scn), name);
12688 : : else
12689 : : {
12690 [ + + ]: 6 : if (print_decompress)
12691 : : {
12692 : : /* We try to decompress the section, but keep the old shdr around
12693 : : so we can show both the original shdr size and the uncompressed
12694 : : data size. */
12695 [ + + ]: 4 : if ((shdr->sh_flags & SHF_COMPRESSED) != 0)
12696 : : {
12697 [ - + ]: 2 : if (elf_compress (scn, 0, 0) < 0)
12698 : 0 : printf ("WARNING: %s [%zd]\n",
12699 : : _("Couldn't uncompress section"),
12700 : : elf_ndxscn (scn));
12701 : : }
12702 [ + - ]: 2 : else if (startswith (name, ".zdebug"))
12703 : : {
12704 [ - + ]: 2 : if (elf_compress_gnu (scn, 0, 0) < 0)
12705 : 0 : printf ("WARNING: %s [%zd]\n",
12706 : : _("Couldn't uncompress section"),
12707 : : elf_ndxscn (scn));
12708 : : }
12709 : : }
12710 : :
12711 : 6 : Elf_Data *data = elf_rawdata (scn, NULL);
12712 [ - + ]: 6 : if (data == NULL)
12713 : 0 : error (0, 0, _("cannot get data for section [%zu] '%s': %s"),
12714 : : elf_ndxscn (scn), name, elf_errmsg (-1));
12715 : : else
12716 : : {
12717 [ + + ]: 6 : if (data->d_size == shdr->sh_size)
12718 : 2 : printf (_("\nHex dump of section [%zu] '%s', %" PRIu64
12719 : : " bytes at offset %#0" PRIx64 ":\n"),
12720 : : elf_ndxscn (scn), name,
12721 : : shdr->sh_size, shdr->sh_offset);
12722 : : else
12723 : 4 : printf (_("\nHex dump of section [%zu] '%s', %" PRIu64
12724 : : " bytes (%zd uncompressed) at offset %#0"
12725 : : PRIx64 ":\n"),
12726 : : elf_ndxscn (scn), name,
12727 : : shdr->sh_size, data->d_size, shdr->sh_offset);
12728 : 6 : hex_dump (data->d_buf, data->d_size);
12729 : : }
12730 : : }
12731 : 6 : }
12732 : :
12733 : : static void
12734 : 241 : print_string_section (Elf_Scn *scn, const GElf_Shdr *shdr, const char *name)
12735 : : {
12736 [ + - + + ]: 241 : if (shdr->sh_size == 0 || shdr->sh_type == SHT_NOBITS)
12737 : 21 : printf (_("\nSection [%zu] '%s' has no strings to dump.\n"),
12738 : : elf_ndxscn (scn), name);
12739 : : else
12740 : : {
12741 [ + + ]: 220 : if (print_decompress)
12742 : : {
12743 : : /* We try to decompress the section, but keep the old shdr around
12744 : : so we can show both the original shdr size and the uncompressed
12745 : : data size. */
12746 [ - + ]: 1 : if ((shdr->sh_flags & SHF_COMPRESSED) != 0)
12747 : : {
12748 [ # # ]: 0 : if (elf_compress (scn, 0, 0) < 0)
12749 : 0 : printf ("WARNING: %s [%zd]\n",
12750 : : _("Couldn't uncompress section"),
12751 : : elf_ndxscn (scn));
12752 : : }
12753 [ + - ]: 1 : else if (startswith (name, ".zdebug"))
12754 : : {
12755 [ - + ]: 1 : if (elf_compress_gnu (scn, 0, 0) < 0)
12756 : 0 : printf ("WARNING: %s [%zd]\n",
12757 : : _("Couldn't uncompress section"),
12758 : : elf_ndxscn (scn));
12759 : : }
12760 : : }
12761 : :
12762 : 220 : Elf_Data *data = elf_rawdata (scn, NULL);
12763 [ - + ]: 220 : if (data == NULL)
12764 : 0 : error (0, 0, _("cannot get data for section [%zu] '%s': %s"),
12765 : : elf_ndxscn (scn), name, elf_errmsg (-1));
12766 : : else
12767 : : {
12768 [ + + ]: 220 : if (data->d_size == shdr->sh_size)
12769 : 219 : printf (_("\nString section [%zu] '%s' contains %" PRIu64
12770 : : " bytes at offset %#0" PRIx64 ":\n"),
12771 : : elf_ndxscn (scn), name,
12772 : : shdr->sh_size, shdr->sh_offset);
12773 : : else
12774 : 1 : printf (_("\nString section [%zu] '%s' contains %" PRIu64
12775 : : " bytes (%zd uncompressed) at offset %#0"
12776 : : PRIx64 ":\n"),
12777 : : elf_ndxscn (scn), name,
12778 : : shdr->sh_size, data->d_size, shdr->sh_offset);
12779 : :
12780 : 220 : const char *start = data->d_buf;
12781 : 220 : const char *const limit = start + data->d_size;
12782 : 27094 : do
12783 : : {
12784 : 27094 : const char *end = memchr (start, '\0', limit - start);
12785 : 27094 : const size_t pos = start - (const char *) data->d_buf;
12786 [ - + ]: 27094 : if (unlikely (end == NULL))
12787 : : {
12788 : 0 : printf (" [%6zx]- %.*s\n",
12789 : : pos, (int) (limit - start), start);
12790 : : break;
12791 : : }
12792 : 27094 : printf (" [%6zx] %s\n", pos, start);
12793 : 27094 : start = end + 1;
12794 [ + + ]: 27094 : } while (start < limit);
12795 : : }
12796 : : }
12797 : 241 : }
12798 : :
12799 : : static void
12800 : 127 : for_each_section_argument (Elf *elf, const struct section_argument *list,
12801 : : void (*dump) (Elf_Scn *scn, const GElf_Shdr *shdr,
12802 : : const char *name))
12803 : : {
12804 : : /* Get the section header string table index. */
12805 : 127 : size_t shstrndx;
12806 [ - + ]: 127 : if (elf_getshdrstrndx (elf, &shstrndx) < 0)
12807 : 0 : error_exit (0, _("cannot get section header string table index"));
12808 : :
12809 [ + + ]: 494 : for (const struct section_argument *a = list; a != NULL; a = a->next)
12810 : : {
12811 : 367 : Elf_Scn *scn;
12812 : 367 : GElf_Shdr shdr_mem;
12813 : 367 : const char *name = NULL;
12814 : :
12815 : 367 : char *endp = NULL;
12816 : 367 : unsigned long int shndx = strtoul (a->arg, &endp, 0);
12817 [ + + - + ]: 367 : if (endp != a->arg && *endp == '\0')
12818 : : {
12819 : 1 : scn = elf_getscn (elf, shndx);
12820 [ - + ]: 1 : if (scn == NULL)
12821 : : {
12822 : 0 : error (0, 0, _("\nsection [%lu] does not exist"), shndx);
12823 : 0 : continue;
12824 : : }
12825 : :
12826 [ - + ]: 1 : if (gelf_getshdr (scn, &shdr_mem) == NULL)
12827 : 0 : error_exit (0, _("cannot get section header: %s"),
12828 : : elf_errmsg (-1));
12829 : 1 : name = elf_strptr (elf, shstrndx, shdr_mem.sh_name);
12830 : 1 : (*dump) (scn, &shdr_mem, name);
12831 : : }
12832 : : else
12833 : : {
12834 : : /* Need to look up the section by name. */
12835 : : scn = NULL;
12836 : : bool found = false;
12837 [ + + ]: 10539 : while ((scn = elf_nextscn (elf, scn)) != NULL)
12838 : : {
12839 [ - + ]: 10173 : if (gelf_getshdr (scn, &shdr_mem) == NULL)
12840 : 0 : continue;
12841 : 10173 : name = elf_strptr (elf, shstrndx, shdr_mem.sh_name);
12842 [ - + ]: 10173 : if (name == NULL)
12843 : 0 : continue;
12844 [ + + ]: 10173 : if (!strcmp (name, a->arg))
12845 : : {
12846 : 246 : found = true;
12847 : 246 : (*dump) (scn, &shdr_mem, name);
12848 : : }
12849 : : }
12850 : :
12851 [ + + - + ]: 366 : if (unlikely (!found) && !a->implicit)
12852 : 367 : error (0, 0, _("\nsection '%s' does not exist"), a->arg);
12853 : : }
12854 : : }
12855 : 127 : }
12856 : :
12857 : : static void
12858 : 6 : dump_data (Ebl *ebl)
12859 : : {
12860 : 6 : for_each_section_argument (ebl->elf, dump_data_sections, &dump_data_section);
12861 : 6 : }
12862 : :
12863 : : static void
12864 : 121 : dump_strings (Ebl *ebl)
12865 : : {
12866 : 121 : for_each_section_argument (ebl->elf, string_sections, &print_string_section);
12867 : 121 : }
12868 : :
12869 : : static void
12870 : 0 : print_strings (Ebl *ebl)
12871 : : {
12872 : : /* Get the section header string table index. */
12873 : 0 : size_t shstrndx;
12874 [ # # ]: 0 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
12875 : 0 : error_exit (0, _("cannot get section header string table index"));
12876 : :
12877 : : Elf_Scn *scn;
12878 : : GElf_Shdr shdr_mem;
12879 : : const char *name;
12880 : : scn = NULL;
12881 [ # # ]: 0 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
12882 : : {
12883 [ # # ]: 0 : if (gelf_getshdr (scn, &shdr_mem) == NULL)
12884 : 0 : continue;
12885 : :
12886 [ # # ]: 0 : if (shdr_mem.sh_type != SHT_PROGBITS
12887 [ # # ]: 0 : || !(shdr_mem.sh_flags & SHF_STRINGS))
12888 : 0 : continue;
12889 : :
12890 : 0 : name = elf_strptr (ebl->elf, shstrndx, shdr_mem.sh_name);
12891 [ # # ]: 0 : if (name == NULL)
12892 : 0 : continue;
12893 : :
12894 : 0 : print_string_section (scn, &shdr_mem, name);
12895 : : }
12896 : 0 : }
12897 : :
12898 : : static void
12899 : 2 : dump_archive_index (Elf *elf, const char *fname)
12900 : : {
12901 : 2 : size_t narsym;
12902 : 2 : const Elf_Arsym *arsym = elf_getarsym (elf, &narsym);
12903 [ - + ]: 2 : if (arsym == NULL)
12904 : : {
12905 : 0 : int result = elf_errno ();
12906 [ # # ]: 0 : if (unlikely (result != ELF_E_NO_INDEX))
12907 : 0 : error_exit (0, _("cannot get symbol index of archive '%s': %s"),
12908 : : fname, elf_errmsg (result));
12909 : : else
12910 : 0 : printf (_("\nArchive '%s' has no symbol index\n"), fname);
12911 : 0 : return;
12912 : : }
12913 : :
12914 : 2 : printf (_("\nIndex of archive '%s' has %zu entries:\n"),
12915 : : fname, narsym);
12916 : :
12917 : 2 : size_t as_off = 0;
12918 [ + + ]: 11 : for (const Elf_Arsym *s = arsym; s < &arsym[narsym - 1]; ++s)
12919 : : {
12920 [ + + ]: 9 : if (s->as_off != as_off)
12921 : : {
12922 : 6 : as_off = s->as_off;
12923 : :
12924 : 6 : Elf *subelf = NULL;
12925 [ + - ]: 6 : if (unlikely (elf_rand (elf, as_off) == 0)
12926 [ - + ]: 6 : || unlikely ((subelf = elf_begin (-1, ELF_C_READ_MMAP, elf))
12927 : : == NULL))
12928 : : #if __GLIBC__ < 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ < 7)
12929 : : while (1)
12930 : : #endif
12931 : 0 : error_exit (0,
12932 : : _("cannot extract member at offset %zu in '%s': %s"),
12933 : : as_off, fname, elf_errmsg (-1));
12934 : :
12935 : 6 : const Elf_Arhdr *h = elf_getarhdr (subelf);
12936 : :
12937 : 6 : printf (_("Archive member '%s' contains:\n"), h->ar_name);
12938 : :
12939 : 6 : elf_end (subelf);
12940 : : }
12941 : :
12942 : 9 : printf ("\t%s\n", s->as_name);
12943 : : }
12944 : : }
12945 : :
12946 : : #include "debugpred.h"
|