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 : 766 : cleanup_list (struct section_argument *list)
328 : : {
329 [ + + + + ]: 1133 : 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 : 383 : main (int argc, char *argv[])
339 : : {
340 : : /* We use no threads here which can interfere with handling a stream. */
341 : 383 : (void) __fsetlocking (stdout, FSETLOCKING_BYCALLER);
342 : :
343 : : /* Set locale. */
344 : 383 : setlocale (LC_ALL, "");
345 : :
346 : : /* Initialize the message catalog. */
347 : 383 : textdomain (PACKAGE_TARNAME);
348 : :
349 : : /* Look up once. */
350 : 383 : yes_str = _("yes");
351 : 383 : no_str = _("no");
352 : :
353 : : /* Parse and process arguments. */
354 : 383 : int remaining;
355 : 383 : argp_parse (&argp, argc, argv, 0, &remaining, NULL);
356 : :
357 : : /* Before we start tell the ELF library which version we are using. */
358 : 383 : elf_version (EV_CURRENT);
359 : :
360 : : /* Now process all the files given at the command line. */
361 : 383 : bool only_one = remaining + 1 == argc;
362 : 422 : do
363 : : {
364 : : /* Open the file. */
365 : 422 : int fd = open (argv[remaining], O_RDONLY);
366 [ - + ]: 422 : if (fd == -1)
367 : : {
368 : 0 : error (0, errno, _("cannot open input file '%s'"), argv[remaining]);
369 : 0 : continue;
370 : : }
371 : :
372 : 422 : process_file (fd, argv[remaining], only_one);
373 : :
374 : 422 : close (fd);
375 : : }
376 [ + + ]: 422 : while (++remaining < argc);
377 : :
378 : 383 : cleanup_list (dump_data_sections);
379 : 383 : cleanup_list (string_sections);
380 : :
381 : 383 : 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 : 2367 : parse_opt (int key, char *arg,
402 : : struct argp_state *state __attribute__ ((unused)))
403 : : {
404 [ + + + - : 2367 : 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 : 1 : case 'h':
441 : 1 : print_file_header = true;
442 : 1 : any_control_option = true;
443 : 1 : break;
444 : 0 : case 'I':
445 : 0 : print_histogram = true;
446 : 0 : any_control_option = true;
447 : 0 : break;
448 : 2 : case 'l':
449 : 2 : print_program_header = true;
450 : 2 : any_control_option = true;
451 : 2 : break;
452 : 23 : case 'n':
453 : 23 : print_notes = true;
454 : 23 : any_control_option = true;
455 : 23 : notes_section = arg;
456 : 23 : break;
457 : 1 : case 'r':
458 : 1 : print_relocations = true;
459 : 1 : any_control_option = true;
460 : 1 : break;
461 : 89 : case 'S':
462 : 89 : print_section_header = true;
463 : 89 : any_control_option = true;
464 : 89 : break;
465 : 14 : case 's':
466 : 14 : print_symbol_table = true;
467 : 14 : any_control_option = true;
468 : 14 : symbol_table_section = arg;
469 : 14 : break;
470 : 1 : case PRINT_DYNSYM_TABLE:
471 : 1 : print_dynsym_table = true;
472 : 1 : any_control_option = true;
473 : 1 : break;
474 : 0 : case 'V':
475 : 0 : print_version_info = true;
476 : 0 : any_control_option = true;
477 : 0 : break;
478 : 2 : case 'c':
479 : 2 : print_archive_index = true;
480 : 2 : break;
481 : 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 : 383 : case ARGP_KEY_FINI:
580 [ + + - + ]: 383 : 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 : 372 : 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 [ + - ]: 372 : if (*(bool *) arg)
763 : : return DWARF_CB_ABORT;
764 : 372 : *(bool *) arg = true;
765 : 372 : 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 : 420 : 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 : 420 : const struct process_dwflmod_args *a = arg;
782 : :
783 : : /* Print the file name. */
784 [ + + ]: 420 : 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 : 420 : process_elf_file (dwflmod, a->fd);
793 : :
794 : 420 : 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 : 429 : create_dwfl (int fd, const char *fname)
827 : : {
828 : : /* Duplicate an fd for dwfl_report_offline to swallow. */
829 : 429 : int dwfl_fd = dup (fd);
830 [ - + ]: 429 : if (unlikely (dwfl_fd < 0))
831 : 0 : error (EXIT_FAILURE, errno, "dup");
832 : :
833 : : /* Use libdwfl in a trivial way to open the libdw handle for us.
834 : : This takes care of applying relocations to DWARF data in ET_REL files. */
835 : 429 : static const Dwfl_Callbacks callbacks =
836 : : {
837 : : .section_address = dwfl_offline_section_address,
838 : : .find_debuginfo = find_no_debuginfo
839 : : };
840 : 429 : Dwfl *dwfl = dwfl_begin (&callbacks);
841 [ + - ]: 429 : if (likely (dwfl != NULL))
842 : : /* Let 0 be the logical address of the file (or first in archive). */
843 : 429 : dwfl->offline_next_address = 0;
844 [ - + ]: 429 : 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 : 429 : dwfl_report_end (dwfl, NULL, NULL);
859 : :
860 : 429 : return dwfl;
861 : : }
862 : :
863 : : /* Process one input file. */
864 : : static void
865 : 422 : process_file (int fd, const char *fname, bool only_one)
866 : : {
867 [ + + ]: 422 : if (print_archive_index)
868 : 2 : check_archive_index (fd, fname, only_one);
869 : :
870 [ + + ]: 422 : if (!any_control_option)
871 : : return;
872 : :
873 [ + + ]: 420 : 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 : 420 : Dwfl *dwfl = create_dwfl (fd, fname);
889 [ + - ]: 420 : if (dwfl != NULL)
890 : : {
891 [ + + ]: 420 : if (only_one)
892 : : {
893 : : /* Clear ONLY_ONE if we have multiple modules, from an archive. */
894 : 372 : bool seen = false;
895 : 372 : only_one = dwfl_getmodules (dwfl, &count_dwflmod, &seen, 0) == 0;
896 : : }
897 : :
898 : : /* Process the one or more modules gleaned from this file. */
899 : 420 : struct process_dwflmod_args a = { .fd = fd, .only_one = only_one };
900 : 420 : 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 [ + - ]: 420 : if (! do_not_close_dwfl)
905 : 420 : dwfl_end (dwfl);
906 : :
907 : : /* Need to close the replaced fd if we created it. Caller takes
908 : : care of original. */
909 [ + + ]: 420 : 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 : 420 : process_elf_file (Dwfl_Module *dwflmod, int fd)
931 : : {
932 : 420 : GElf_Addr dwflbias;
933 : 420 : Elf *elf = dwfl_module_getelf (dwflmod, &dwflbias);
934 : :
935 : 420 : GElf_Ehdr ehdr_mem;
936 : 420 : GElf_Ehdr *ehdr = gelf_getehdr (elf, &ehdr_mem);
937 : :
938 [ - + ]: 420 : if (ehdr == NULL)
939 : : {
940 : 0 : error (0, 0, _("cannot read ELF header: %s"), elf_errmsg (-1));
941 : 0 : return;
942 : : }
943 : :
944 : 420 : Ebl *ebl = ebl_openbackend (elf);
945 [ - + ]: 420 : 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 [ - + ]: 420 : if (unlikely (elf_getshdrnum (ebl->elf, &shnum) < 0))
954 : 0 : error (EXIT_FAILURE, 0,
955 : 0 : _("cannot determine number of sections: %s"),
956 : : elf_errmsg (-1));
957 : :
958 : : /* Determine the number of phdrs. */
959 [ - + ]: 420 : if (unlikely (elf_getphdrnum (ebl->elf, &phnum) < 0))
960 : 0 : error (EXIT_FAILURE, 0,
961 : 0 : _("cannot determine number of program headers: %s"),
962 : : elf_errmsg (-1));
963 : :
964 : : /* For an ET_REL file, libdwfl has adjusted the in-core shdrs and
965 : : may have applied relocation to some sections. If there are any
966 : : compressed sections, any pass (or libdw/libdwfl) might have
967 : : uncompressed them. So we need to get a fresh Elf handle on the
968 : : file to display those. */
969 : 840 : bool print_unchanged = ((print_section_header
970 [ + + ]: 211 : || print_relocations
971 [ + + ]: 210 : || dump_data_sections != NULL
972 [ + + ]: 204 : || print_notes)
973 [ + + + + ]: 450 : && (ehdr->e_type == ET_REL
974 [ + + ]: 174 : || elf_contains_chdrs (ebl->elf)));
975 : :
976 : 420 : Elf *pure_elf = NULL;
977 : 420 : Ebl *pure_ebl = ebl;
978 [ + + ]: 420 : if (print_unchanged)
979 : : {
980 : : /* Read the file afresh. */
981 : 74 : off_t aroff = elf_getaroff (elf);
982 : 74 : pure_elf = dwelf_elf_begin (fd);
983 [ - + ]: 74 : if (aroff > 0)
984 : : {
985 : : /* Archive member. */
986 : 0 : (void) elf_rand (pure_elf, aroff);
987 : 0 : Elf *armem = elf_begin (-1, ELF_C_READ_MMAP, pure_elf);
988 : 0 : elf_end (pure_elf);
989 : 0 : pure_elf = armem;
990 : : }
991 [ - + ]: 74 : if (pure_elf == NULL)
992 : : {
993 : 0 : error (0, 0, _("cannot read ELF: %s"), elf_errmsg (-1));
994 : 0 : return;
995 : : }
996 : 74 : pure_ebl = ebl_openbackend (pure_elf);
997 [ - + ]: 74 : if (pure_ebl == NULL)
998 : 0 : goto ebl_error;
999 : : }
1000 : :
1001 [ + + ]: 420 : if (print_file_header)
1002 : 121 : print_ehdr (ebl, ehdr);
1003 [ + + ]: 420 : if (print_section_header)
1004 : 209 : print_shdr (pure_ebl, ehdr);
1005 [ + + ]: 420 : if (print_program_header)
1006 : 122 : print_phdr (ebl, ehdr);
1007 [ + + ]: 420 : if (print_section_groups)
1008 : 129 : print_scngrp (ebl);
1009 [ + + ]: 420 : if (print_dynamic_table)
1010 : 122 : print_dynamic (ebl);
1011 [ + + ]: 420 : if (print_relocations)
1012 : 121 : print_relocs (pure_ebl, ehdr);
1013 [ + + ]: 420 : if (print_histogram)
1014 : 120 : handle_hash (ebl);
1015 [ + + + + ]: 420 : if (print_symbol_table || print_dynsym_table)
1016 : 135 : print_symtab (ebl, SHT_DYNSYM);
1017 [ + + ]: 420 : if (print_version_info)
1018 : 120 : print_verinfo (ebl);
1019 [ + + ]: 420 : if (print_symbol_table)
1020 : 134 : print_symtab (ebl, SHT_SYMTAB);
1021 [ + + ]: 420 : if (print_arch)
1022 : 124 : print_liblist (ebl);
1023 [ + + ]: 420 : if (print_arch)
1024 : 124 : print_attributes (ebl, ehdr);
1025 [ + + ]: 420 : if (dump_data_sections != NULL)
1026 : 6 : dump_data (pure_ebl);
1027 [ + + ]: 420 : if (string_sections != NULL)
1028 : 121 : dump_strings (ebl);
1029 [ + + ]: 420 : if ((print_debug_sections | implicit_debug_sections) != 0)
1030 : 269 : print_debug (dwflmod, ebl, ehdr);
1031 [ + + ]: 420 : if (print_notes)
1032 : 143 : handle_notes (pure_ebl, ehdr);
1033 [ - + ]: 420 : if (print_string_sections)
1034 : 0 : print_strings (ebl);
1035 : :
1036 : 420 : ebl_closebackend (ebl);
1037 : :
1038 [ + + ]: 420 : if (pure_ebl != ebl)
1039 : : {
1040 : 74 : ebl_closebackend (pure_ebl);
1041 : 74 : elf_end (pure_elf);
1042 : : }
1043 : : }
1044 : :
1045 : :
1046 : : /* Print file type. */
1047 : : static void
1048 : 121 : print_file_type (unsigned short int e_type)
1049 : : {
1050 [ + - ]: 121 : if (likely (e_type <= ET_CORE))
1051 : : {
1052 : 121 : static const char *const knowntypes[] =
1053 : : {
1054 : : N_("NONE (None)"),
1055 : : N_("REL (Relocatable file)"),
1056 : : N_("EXEC (Executable file)"),
1057 : : N_("DYN (Shared object file)"),
1058 : : N_("CORE (Core file)")
1059 : : };
1060 : 121 : puts (_(knowntypes[e_type]));
1061 : : }
1062 [ # # ]: 0 : else if (e_type >= ET_LOOS && e_type <= ET_HIOS)
1063 : 0 : printf (_("OS Specific: (%x)\n"), e_type);
1064 [ # # ]: 0 : else if (e_type >= ET_LOPROC /* && e_type <= ET_HIPROC always true */)
1065 : 0 : printf (_("Processor Specific: (%x)\n"), e_type);
1066 : : else
1067 : 0 : puts ("???");
1068 : 121 : }
1069 : :
1070 : :
1071 : : /* Print ELF header. */
1072 : : static void
1073 : 121 : print_ehdr (Ebl *ebl, GElf_Ehdr *ehdr)
1074 : : {
1075 : 121 : fputs_unlocked (_("ELF Header:\n Magic: "), stdout);
1076 [ + + ]: 2057 : for (size_t cnt = 0; cnt < EI_NIDENT; ++cnt)
1077 : 1936 : printf (" %02hhx", ehdr->e_ident[cnt]);
1078 : :
1079 : 242 : printf (_("\n Class: %s\n"),
1080 [ + + ]: 121 : ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? "ELF32"
1081 : : : ehdr->e_ident[EI_CLASS] == ELFCLASS64 ? "ELF64"
1082 [ - + ]: 99 : : "\?\?\?");
1083 : :
1084 : 242 : printf (_(" Data: %s\n"),
1085 [ + + ]: 121 : ehdr->e_ident[EI_DATA] == ELFDATA2LSB
1086 : : ? "2's complement, little endian"
1087 : : : ehdr->e_ident[EI_DATA] == ELFDATA2MSB
1088 [ - + ]: 12 : ? "2's complement, big endian" : "\?\?\?");
1089 : :
1090 : 242 : printf (_(" Ident Version: %hhd %s\n"),
1091 [ + - ]: 121 : ehdr->e_ident[EI_VERSION],
1092 : 121 : ehdr->e_ident[EI_VERSION] == EV_CURRENT ? _("(current)")
1093 : : : "(\?\?\?)");
1094 : :
1095 : 121 : char buf[512];
1096 : 121 : printf (_(" OS/ABI: %s\n"),
1097 : 121 : ebl_osabi_name (ebl, ehdr->e_ident[EI_OSABI], buf, sizeof (buf)));
1098 : :
1099 : 242 : printf (_(" ABI Version: %hhd\n"),
1100 : 121 : ehdr->e_ident[EI_ABIVERSION]);
1101 : :
1102 : 121 : fputs_unlocked (_(" Type: "), stdout);
1103 : 121 : print_file_type (ehdr->e_type);
1104 : :
1105 : 121 : const char *machine = dwelf_elf_e_machine_string (ehdr->e_machine);
1106 [ + - ]: 121 : if (machine != NULL)
1107 : 121 : printf (_(" Machine: %s\n"), machine);
1108 : : else
1109 : 121 : printf (_(" Machine: <unknown>: 0x%x\n"),
1110 : 0 : ehdr->e_machine);
1111 : :
1112 [ + - ]: 121 : printf (_(" Version: %d %s\n"),
1113 : : ehdr->e_version,
1114 : 121 : ehdr->e_version == EV_CURRENT ? _("(current)") : "(\?\?\?)");
1115 : :
1116 : 121 : printf (_(" Entry point address: %#" PRIx64 "\n"),
1117 : : ehdr->e_entry);
1118 : :
1119 : 121 : printf (_(" Start of program headers: %" PRId64 " %s\n"),
1120 : : ehdr->e_phoff, _("(bytes into file)"));
1121 : :
1122 : 121 : printf (_(" Start of section headers: %" PRId64 " %s\n"),
1123 : : ehdr->e_shoff, _("(bytes into file)"));
1124 : :
1125 : 121 : printf (_(" Flags: %s\n"),
1126 : : ebl_machine_flag_name (ebl, ehdr->e_flags, buf, sizeof (buf)));
1127 : :
1128 : 242 : printf (_(" Size of this header: %" PRId16 " %s\n"),
1129 : 121 : ehdr->e_ehsize, _("(bytes)"));
1130 : :
1131 : 242 : printf (_(" Size of program header entries: %" PRId16 " %s\n"),
1132 : 121 : ehdr->e_phentsize, _("(bytes)"));
1133 : :
1134 : 242 : printf (_(" Number of program headers entries: %" PRId16),
1135 : 121 : ehdr->e_phnum);
1136 [ + + ]: 121 : if (ehdr->e_phnum == PN_XNUM)
1137 : : {
1138 : 1 : GElf_Shdr shdr_mem;
1139 : 1 : GElf_Shdr *shdr = gelf_getshdr (elf_getscn (ebl->elf, 0), &shdr_mem);
1140 [ + - ]: 1 : if (shdr != NULL)
1141 : 1 : printf (_(" (%" PRIu32 " in [0].sh_info)"),
1142 : 1 : (uint32_t) shdr->sh_info);
1143 : : else
1144 : 0 : fputs_unlocked (_(" ([0] not available)"), stdout);
1145 : : }
1146 [ - + ]: 121 : fputc_unlocked ('\n', stdout);
1147 : :
1148 : 242 : printf (_(" Size of section header entries: %" PRId16 " %s\n"),
1149 : 121 : ehdr->e_shentsize, _("(bytes)"));
1150 : :
1151 : 242 : printf (_(" Number of section headers entries: %" PRId16),
1152 : 121 : ehdr->e_shnum);
1153 [ - + ]: 121 : if (ehdr->e_shnum == 0)
1154 : : {
1155 : 0 : GElf_Shdr shdr_mem;
1156 : 0 : GElf_Shdr *shdr = gelf_getshdr (elf_getscn (ebl->elf, 0), &shdr_mem);
1157 [ # # ]: 0 : if (shdr != NULL)
1158 : 0 : printf (_(" (%" PRIu32 " in [0].sh_size)"),
1159 : 0 : (uint32_t) shdr->sh_size);
1160 : : else
1161 : 0 : fputs_unlocked (_(" ([0] not available)"), stdout);
1162 : : }
1163 [ - + ]: 121 : fputc_unlocked ('\n', stdout);
1164 : :
1165 [ - + ]: 121 : if (unlikely (ehdr->e_shstrndx == SHN_XINDEX))
1166 : : {
1167 : 0 : GElf_Shdr shdr_mem;
1168 : 0 : GElf_Shdr *shdr = gelf_getshdr (elf_getscn (ebl->elf, 0), &shdr_mem);
1169 [ # # ]: 0 : if (shdr != NULL)
1170 : : /* We managed to get the zeroth section. */
1171 : 0 : snprintf (buf, sizeof (buf), _(" (%" PRIu32 " in [0].sh_link)"),
1172 : 0 : (uint32_t) shdr->sh_link);
1173 : : else
1174 : : {
1175 : 0 : strncpy (buf, _(" ([0] not available)"), sizeof (buf));
1176 : 0 : buf[sizeof (buf) - 1] = '\0';
1177 : : }
1178 : :
1179 : 0 : printf (_(" Section header string table index: XINDEX%s\n\n"),
1180 : : buf);
1181 : : }
1182 : : else
1183 : 242 : printf (_(" Section header string table index: %" PRId16 "\n\n"),
1184 : 121 : ehdr->e_shstrndx);
1185 : 121 : }
1186 : :
1187 : :
1188 : : static const char *
1189 : 58707 : get_visibility_type (int value)
1190 : : {
1191 [ - + - - : 58707 : switch (value)
+ ]
1192 : : {
1193 : : case STV_DEFAULT:
1194 : : return "DEFAULT";
1195 : 0 : case STV_INTERNAL:
1196 : 0 : return "INTERNAL";
1197 : 469 : case STV_HIDDEN:
1198 : 469 : return "HIDDEN";
1199 : 0 : case STV_PROTECTED:
1200 : 0 : return "PROTECTED";
1201 : 0 : default:
1202 : 0 : return "???";
1203 : : }
1204 : : }
1205 : :
1206 : : static const char *
1207 : 12 : elf_ch_type_name (unsigned int code)
1208 : : {
1209 : 12 : if (code == 0)
1210 : : return "NONE";
1211 : :
1212 [ + - ]: 12 : if (code == ELFCOMPRESS_ZLIB)
1213 : 12 : return "ZLIB";
1214 : :
1215 : : return "UNKNOWN";
1216 : : }
1217 : :
1218 : : /* Print the section headers. */
1219 : : static void
1220 : 209 : print_shdr (Ebl *ebl, GElf_Ehdr *ehdr)
1221 : : {
1222 : 209 : size_t cnt;
1223 : 209 : size_t shstrndx;
1224 : :
1225 [ + + ]: 209 : if (! print_file_header)
1226 : : {
1227 : 89 : size_t sections;
1228 [ - + ]: 89 : if (unlikely (elf_getshdrnum (ebl->elf, §ions) < 0))
1229 : 0 : error (EXIT_FAILURE, 0,
1230 : 0 : _("cannot get number of sections: %s"),
1231 : : elf_errmsg (-1));
1232 : :
1233 : 89 : printf (_("\
1234 : : There are %zd section headers, starting at offset %#" PRIx64 ":\n\
1235 : : \n"),
1236 : : sections, ehdr->e_shoff);
1237 : : }
1238 : :
1239 : : /* Get the section header string table index. */
1240 [ - + ]: 209 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
1241 : 0 : error (EXIT_FAILURE, 0,
1242 : 0 : _("cannot get section header string table index: %s"),
1243 : : elf_errmsg (-1));
1244 : :
1245 : 209 : puts (_("Section Headers:"));
1246 : :
1247 [ + + ]: 209 : if (ehdr->e_ident[EI_CLASS] == ELFCLASS32)
1248 : 68 : puts (_("[Nr] Name Type Addr Off Size ES Flags Lk Inf Al"));
1249 : : else
1250 : 141 : puts (_("[Nr] Name Type Addr Off Size ES Flags Lk Inf Al"));
1251 : :
1252 [ + + ]: 209 : if (print_decompress)
1253 : : {
1254 [ + + ]: 8 : if (ehdr->e_ident[EI_CLASS] == ELFCLASS32)
1255 : 4 : puts (_(" [Compression Size Al]"));
1256 : : else
1257 : 4 : puts (_(" [Compression Size Al]"));
1258 : : }
1259 : :
1260 [ + + ]: 1578706 : for (cnt = 0; cnt < shnum; ++cnt)
1261 : : {
1262 : 1578497 : Elf_Scn *scn = elf_getscn (ebl->elf, cnt);
1263 : :
1264 [ - + ]: 1578497 : if (unlikely (scn == NULL))
1265 : 0 : error (EXIT_FAILURE, 0, _("cannot get section: %s"),
1266 : : elf_errmsg (-1));
1267 : :
1268 : : /* Get the section header. */
1269 : 1578497 : GElf_Shdr shdr_mem;
1270 : 1578497 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
1271 [ - + ]: 1578497 : if (unlikely (shdr == NULL))
1272 : 0 : error (EXIT_FAILURE, 0, _("cannot get section header: %s"),
1273 : : elf_errmsg (-1));
1274 : :
1275 : 1578497 : char flagbuf[20];
1276 : 1578497 : char *cp = flagbuf;
1277 [ + + ]: 1578497 : if (shdr->sh_flags & SHF_WRITE)
1278 : 1304 : *cp++ = 'W';
1279 [ + + ]: 1578497 : if (shdr->sh_flags & SHF_ALLOC)
1280 : 3855 : *cp++ = 'A';
1281 [ + + ]: 1578497 : if (shdr->sh_flags & SHF_EXECINSTR)
1282 : 672 : *cp++ = 'X';
1283 [ + + ]: 1578497 : if (shdr->sh_flags & SHF_MERGE)
1284 : 205 : *cp++ = 'M';
1285 [ + + ]: 1578497 : if (shdr->sh_flags & SHF_STRINGS)
1286 : 189 : *cp++ = 'S';
1287 [ + + ]: 1578497 : if (shdr->sh_flags & SHF_INFO_LINK)
1288 : 191 : *cp++ = 'I';
1289 [ + + ]: 1578497 : if (shdr->sh_flags & SHF_LINK_ORDER)
1290 : 2 : *cp++ = 'L';
1291 [ - + ]: 1578497 : if (shdr->sh_flags & SHF_OS_NONCONFORMING)
1292 : 0 : *cp++ = 'N';
1293 [ - + ]: 1578497 : if (shdr->sh_flags & SHF_GROUP)
1294 : 0 : *cp++ = 'G';
1295 [ + + ]: 1578497 : if (shdr->sh_flags & SHF_TLS)
1296 : 19 : *cp++ = 'T';
1297 [ + + ]: 1578497 : if (shdr->sh_flags & SHF_COMPRESSED)
1298 : 27 : *cp++ = 'C';
1299 [ - + ]: 1578497 : if (shdr->sh_flags & SHF_ORDERED)
1300 : 0 : *cp++ = 'O';
1301 [ - + ]: 1578497 : if (shdr->sh_flags & SHF_EXCLUDE)
1302 : 0 : *cp++ = 'E';
1303 [ + + ]: 1578497 : if (shdr->sh_flags & SHF_GNU_RETAIN)
1304 : 1 : *cp++ = 'R';
1305 : 1578497 : *cp = '\0';
1306 : :
1307 : 1578497 : const char *sname;
1308 : 1578497 : char buf[128];
1309 [ - + ]: 1578497 : sname = elf_strptr (ebl->elf, shstrndx, shdr->sh_name) ?: "<corrupt>";
1310 [ + + ]: 2368873 : printf ("[%2zu] %-20s %-12s %0*" PRIx64 " %0*" PRIx64 " %0*" PRIx64
1311 : : " %2" PRId64 " %-5s %2" PRId32 " %3" PRId32
1312 : : " %2" PRId64 "\n",
1313 : : cnt, sname,
1314 : 1578497 : ebl_section_type_name (ebl, shdr->sh_type, buf, sizeof (buf)),
1315 [ + + ]: 1578497 : ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 8 : 16, shdr->sh_addr,
1316 : : ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 6 : 8, shdr->sh_offset,
1317 : : ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 6 : 8, shdr->sh_size,
1318 : : shdr->sh_entsize, flagbuf, shdr->sh_link, shdr->sh_info,
1319 : : shdr->sh_addralign);
1320 : :
1321 [ + + ]: 1578497 : if (print_decompress)
1322 : : {
1323 [ + + ]: 76 : if ((shdr->sh_flags & SHF_COMPRESSED) != 0)
1324 : : {
1325 : 12 : GElf_Chdr chdr;
1326 [ + - ]: 12 : if (gelf_getchdr (scn, &chdr) != NULL)
1327 [ + - ]: 24 : printf (" [ELF %s (%" PRId32 ") %0*" PRIx64
1328 : : " %2" PRId64 "]\n",
1329 : : elf_ch_type_name (chdr.ch_type),
1330 : : chdr.ch_type,
1331 [ + + ]: 12 : ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 6 : 8,
1332 : : chdr.ch_size, chdr.ch_addralign);
1333 : : else
1334 : 12 : error (0, 0,
1335 : 0 : _("bad compression header for section %zd: %s"),
1336 : : elf_ndxscn (scn), elf_errmsg (-1));
1337 : : }
1338 [ + + ]: 64 : else if (startswith (sname, ".zdebug"))
1339 : : {
1340 : 12 : ssize_t size;
1341 [ + - ]: 12 : if ((size = dwelf_scn_gnu_compressed_size (scn)) >= 0)
1342 : 1578509 : printf (" [GNU ZLIB %0*zx ]\n",
1343 [ + + ]: 12 : ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 6 : 8, size);
1344 : : else
1345 : 1578497 : error (0, 0,
1346 : 0 : _("bad gnu compressed size for section %zd: %s"),
1347 : : elf_ndxscn (scn), elf_errmsg (-1));
1348 : : }
1349 : : }
1350 : : }
1351 : :
1352 [ - + ]: 209 : fputc_unlocked ('\n', stdout);
1353 : 209 : }
1354 : :
1355 : :
1356 : : /* Print the program header. */
1357 : : static void
1358 : 122 : print_phdr (Ebl *ebl, GElf_Ehdr *ehdr)
1359 : : {
1360 [ + + ]: 122 : if (phnum == 0)
1361 : : /* No program header, this is OK in relocatable objects. */
1362 : 34 : return;
1363 : :
1364 : 88 : puts (_("Program Headers:"));
1365 [ + + ]: 88 : if (ehdr->e_ident[EI_CLASS] == ELFCLASS32)
1366 : 13 : puts (_("\
1367 : : Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align"));
1368 : : else
1369 : 75 : puts (_("\
1370 : : Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align"));
1371 : :
1372 : : /* Process all program headers. */
1373 : : bool has_relro = false;
1374 : : GElf_Addr relro_from = 0;
1375 : : GElf_Addr relro_to = 0;
1376 [ + + ]: 930 : for (size_t cnt = 0; cnt < phnum; ++cnt)
1377 : : {
1378 : 842 : char buf[128];
1379 : 842 : GElf_Phdr mem;
1380 : 842 : GElf_Phdr *phdr = gelf_getphdr (ebl->elf, cnt, &mem);
1381 : :
1382 : : /* If for some reason the header cannot be returned show this. */
1383 [ - + ]: 842 : if (unlikely (phdr == NULL))
1384 : : {
1385 : 0 : puts (" ???");
1386 : 0 : continue;
1387 : : }
1388 : :
1389 : 842 : printf (" %-14s 0x%06" PRIx64 " 0x%0*" PRIx64 " 0x%0*" PRIx64
1390 : : " 0x%06" PRIx64 " 0x%06" PRIx64 " %c%c%c 0x%" PRIx64 "\n",
1391 : 842 : ebl_segment_type_name (ebl, phdr->p_type, buf, sizeof (buf)),
1392 : : phdr->p_offset,
1393 [ + + ]: 842 : ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 8 : 16, phdr->p_vaddr,
1394 : : ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 8 : 16, phdr->p_paddr,
1395 : : phdr->p_filesz,
1396 : : phdr->p_memsz,
1397 [ - + ]: 842 : phdr->p_flags & PF_R ? 'R' : ' ',
1398 [ + + ]: 842 : phdr->p_flags & PF_W ? 'W' : ' ',
1399 [ + + ]: 842 : phdr->p_flags & PF_X ? 'E' : ' ',
1400 : : phdr->p_align);
1401 : :
1402 [ + + ]: 842 : if (phdr->p_type == PT_INTERP)
1403 : : {
1404 : : /* If we are sure the file offset is valid then we can show
1405 : : the user the name of the interpreter. We check whether
1406 : : there is a section at the file offset. Normally there
1407 : : would be a section called ".interp". But in separate
1408 : : .debug files it is a NOBITS section (and so doesn't match
1409 : : with gelf_offscn). Which probably means the offset is
1410 : : not valid another reason could be because the ELF file
1411 : : just doesn't contain any section headers, in that case
1412 : : just play it safe and don't display anything. */
1413 : :
1414 : 86 : Elf_Scn *scn = gelf_offscn (ebl->elf, phdr->p_offset);
1415 : 86 : GElf_Shdr shdr_mem;
1416 : 86 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
1417 : :
1418 : 86 : size_t maxsize;
1419 : 86 : char *filedata = elf_rawfile (ebl->elf, &maxsize);
1420 : :
1421 [ + - + + ]: 86 : if (shdr != NULL && shdr->sh_type == SHT_PROGBITS
1422 [ + - + - ]: 65 : && filedata != NULL && phdr->p_offset < maxsize
1423 [ + - ]: 65 : && phdr->p_filesz <= maxsize - phdr->p_offset
1424 [ + - ]: 65 : && memchr (filedata + phdr->p_offset, '\0',
1425 : : phdr->p_filesz) != NULL)
1426 : 86 : printf (_("\t[Requesting program interpreter: %s]\n"),
1427 : 65 : filedata + phdr->p_offset);
1428 : : }
1429 [ + + ]: 756 : else if (phdr->p_type == PT_GNU_RELRO)
1430 : : {
1431 : 76 : has_relro = true;
1432 : 76 : relro_from = phdr->p_vaddr;
1433 : 76 : relro_to = relro_from + phdr->p_memsz;
1434 : : }
1435 : : }
1436 : :
1437 : 88 : size_t sections;
1438 [ - + ]: 88 : if (unlikely (elf_getshdrnum (ebl->elf, §ions) < 0))
1439 : 0 : error (EXIT_FAILURE, 0,
1440 : 0 : _("cannot get number of sections: %s"),
1441 : : elf_errmsg (-1));
1442 : :
1443 [ + - ]: 88 : if (sections == 0)
1444 : : /* No sections in the file. Punt. */
1445 : : return;
1446 : :
1447 : : /* Get the section header string table index. */
1448 : 88 : size_t shstrndx;
1449 [ - + ]: 88 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
1450 : 0 : error (EXIT_FAILURE, 0,
1451 : 0 : _("cannot get section header string table index"));
1452 : :
1453 : 88 : puts (_("\n Section to Segment mapping:\n Segment Sections..."));
1454 : :
1455 [ + + ]: 930 : for (size_t cnt = 0; cnt < phnum; ++cnt)
1456 : : {
1457 : : /* Print the segment number. */
1458 : 842 : printf (" %2.2zu ", cnt);
1459 : :
1460 : 842 : GElf_Phdr phdr_mem;
1461 : 842 : GElf_Phdr *phdr = gelf_getphdr (ebl->elf, cnt, &phdr_mem);
1462 : : /* This must not happen. */
1463 [ - + ]: 842 : if (unlikely (phdr == NULL))
1464 : 0 : error (EXIT_FAILURE, 0, _("cannot get program header: %s"),
1465 : : elf_errmsg (-1));
1466 : :
1467 : : /* Iterate over the sections. */
1468 : : bool in_relro = false;
1469 : : bool in_ro = false;
1470 [ + + ]: 26852 : for (size_t inner = 1; inner < shnum; ++inner)
1471 : : {
1472 : 26010 : Elf_Scn *scn = elf_getscn (ebl->elf, inner);
1473 : : /* This should not happen. */
1474 [ - + ]: 26010 : if (unlikely (scn == NULL))
1475 : 0 : error (EXIT_FAILURE, 0, _("cannot get section: %s"),
1476 : : elf_errmsg (-1));
1477 : :
1478 : : /* Get the section header. */
1479 : 26010 : GElf_Shdr shdr_mem;
1480 : 26010 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
1481 [ - + ]: 26010 : if (unlikely (shdr == NULL))
1482 : 0 : error (EXIT_FAILURE, 0,
1483 : 0 : _("cannot get section header: %s"),
1484 : : elf_errmsg (-1));
1485 : :
1486 [ + - ]: 26010 : if (shdr->sh_size > 0
1487 : : /* Compare allocated sections by VMA, unallocated
1488 : : sections by file offset. */
1489 [ + + + + ]: 52020 : && (shdr->sh_flags & SHF_ALLOC
1490 : 20753 : ? (shdr->sh_addr >= phdr->p_vaddr
1491 [ + + ]: 20753 : && (shdr->sh_addr + shdr->sh_size
1492 [ + + ]: 13979 : <= phdr->p_vaddr + phdr->p_memsz))
1493 : 5257 : : (shdr->sh_offset >= phdr->p_offset
1494 [ + + ]: 5257 : && (shdr->sh_offset + shdr->sh_size
1495 [ + + ]: 4494 : <= phdr->p_offset + phdr->p_filesz))))
1496 : : {
1497 [ + + ]: 3061 : if (has_relro && !in_relro
1498 [ + + ]: 2033 : && shdr->sh_addr >= relro_from
1499 [ + + ]: 382 : && shdr->sh_addr + shdr->sh_size <= relro_to)
1500 : : {
1501 : 239 : fputs_unlocked (" [RELRO:", stdout);
1502 : 239 : in_relro = true;
1503 : : }
1504 [ + + + + ]: 2822 : else if (has_relro && in_relro && shdr->sh_addr >= relro_to)
1505 : : {
1506 : 53 : fputs_unlocked ("]", stdout);
1507 : 53 : in_relro = false;
1508 : : }
1509 [ + + ]: 2769 : else if (has_relro && in_relro
1510 [ + + ]: 583 : && shdr->sh_addr + shdr->sh_size > relro_to)
1511 : 23 : fputs_unlocked ("] <RELRO:", stdout);
1512 [ + + ]: 2746 : else if (phdr->p_type == PT_LOAD && (phdr->p_flags & PF_W) == 0)
1513 : : {
1514 [ + + ]: 1583 : if (!in_ro)
1515 : : {
1516 : 140 : fputs_unlocked (" [RO:", stdout);
1517 : 140 : in_ro = true;
1518 : : }
1519 : : }
1520 : : else
1521 : : {
1522 : : /* Determine the segment this section is part of. */
1523 : : size_t cnt2;
1524 : : GElf_Phdr phdr2_mem;
1525 : : GElf_Phdr *phdr2 = NULL;
1526 [ + - ]: 4712 : for (cnt2 = 0; cnt2 < phnum; ++cnt2)
1527 : : {
1528 : 4712 : phdr2 = gelf_getphdr (ebl->elf, cnt2, &phdr2_mem);
1529 : :
1530 [ + - + + ]: 4712 : if (phdr2 != NULL && phdr2->p_type == PT_LOAD
1531 [ + - ]: 2442 : && shdr->sh_addr >= phdr2->p_vaddr
1532 : 2442 : && (shdr->sh_addr + shdr->sh_size
1533 [ + + ]: 2442 : <= phdr2->p_vaddr + phdr2->p_memsz))
1534 : : break;
1535 : : }
1536 : :
1537 [ + - ]: 1163 : if (cnt2 < phnum)
1538 : : {
1539 [ + + + + ]: 1163 : if ((phdr2->p_flags & PF_W) == 0 && !in_ro)
1540 : : {
1541 : 265 : fputs_unlocked (" [RO:", stdout);
1542 : 265 : in_ro = true;
1543 : : }
1544 [ + + - + ]: 898 : else if ((phdr2->p_flags & PF_W) != 0 && in_ro)
1545 : : {
1546 : 0 : fputs_unlocked ("]", stdout);
1547 : 0 : in_ro = false;
1548 : : }
1549 : : }
1550 : : }
1551 : :
1552 : 3061 : printf (" %s",
1553 : 3061 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name));
1554 : :
1555 : : /* Signal that this section is only partially covered. */
1556 [ + + ]: 3061 : if (has_relro && in_relro
1557 [ + + ]: 822 : && shdr->sh_addr + shdr->sh_size > relro_to)
1558 : : {
1559 : 23 : fputs_unlocked (">", stdout);
1560 : 23 : in_relro = false;
1561 : : }
1562 : : }
1563 : : }
1564 [ + + ]: 842 : if (in_relro || in_ro)
1565 : 568 : fputs_unlocked ("]", stdout);
1566 : :
1567 : : /* Finish the line. */
1568 [ - + ]: 1684 : fputc_unlocked ('\n', stdout);
1569 : : }
1570 : : }
1571 : :
1572 : :
1573 : : static const char *
1574 : 413 : section_name (Ebl *ebl, GElf_Shdr *shdr)
1575 : : {
1576 : 413 : size_t shstrndx;
1577 [ + - ]: 413 : if (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0)
1578 : : return "???";
1579 [ - + ]: 413 : return elf_strptr (ebl->elf, shstrndx, shdr->sh_name) ?: "???";
1580 : : }
1581 : :
1582 : :
1583 : : static void
1584 : 16 : handle_scngrp (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr)
1585 : : {
1586 : : /* Get the data of the section. */
1587 : 16 : Elf_Data *data = elf_getdata (scn, NULL);
1588 : :
1589 : 16 : Elf_Scn *symscn = elf_getscn (ebl->elf, shdr->sh_link);
1590 : 16 : GElf_Shdr symshdr_mem;
1591 : 16 : GElf_Shdr *symshdr = gelf_getshdr (symscn, &symshdr_mem);
1592 : 16 : Elf_Data *symdata = elf_getdata (symscn, NULL);
1593 : :
1594 [ + - + - ]: 16 : if (data == NULL || data->d_size < sizeof (Elf32_Word) || symshdr == NULL
1595 [ - + ]: 16 : || symdata == NULL)
1596 : 0 : return;
1597 : :
1598 : : /* Get the section header string table index. */
1599 : 16 : size_t shstrndx;
1600 [ - + ]: 16 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
1601 : 0 : error (EXIT_FAILURE, 0,
1602 : 0 : _("cannot get section header string table index"));
1603 : :
1604 : 16 : Elf32_Word *grpref = (Elf32_Word *) data->d_buf;
1605 : :
1606 : 16 : GElf_Sym sym_mem;
1607 : 16 : GElf_Sym *sym = gelf_getsym (symdata, shdr->sh_info, &sym_mem);
1608 : :
1609 [ + + - + ]: 48 : printf ((grpref[0] & GRP_COMDAT)
1610 : 4 : ? ngettext ("\
1611 : : \nCOMDAT section group [%2zu] '%s' with signature '%s' contains %zu entry:\n",
1612 : : "\
1613 : : \nCOMDAT section group [%2zu] '%s' with signature '%s' contains %zu entries:\n",
1614 : : data->d_size / sizeof (Elf32_Word) - 1)
1615 : 12 : : ngettext ("\
1616 : : \nSection group [%2zu] '%s' with signature '%s' contains %zu entry:\n", "\
1617 : : \nSection group [%2zu] '%s' with signature '%s' contains %zu entries:\n",
1618 : : data->d_size / sizeof (Elf32_Word) - 1),
1619 : : elf_ndxscn (scn),
1620 : 16 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
1621 : : (sym == NULL ? NULL
1622 [ + - ]: 16 : : elf_strptr (ebl->elf, symshdr->sh_link, sym->st_name))
1623 : 0 : ?: _("<INVALID SYMBOL>"),
1624 : 16 : data->d_size / sizeof (Elf32_Word) - 1);
1625 : :
1626 [ + + ]: 52 : for (size_t cnt = 1; cnt < data->d_size / sizeof (Elf32_Word); ++cnt)
1627 : : {
1628 : 36 : GElf_Shdr grpshdr_mem;
1629 : 36 : GElf_Shdr *grpshdr = gelf_getshdr (elf_getscn (ebl->elf, grpref[cnt]),
1630 : : &grpshdr_mem);
1631 : :
1632 : 36 : const char *str;
1633 [ + - ]: 72 : printf (" [%2u] %s\n",
1634 : : grpref[cnt],
1635 : : grpshdr != NULL
1636 [ - + ]: 36 : && (str = elf_strptr (ebl->elf, shstrndx, grpshdr->sh_name))
1637 : 0 : ? str : _("<INVALID SECTION>"));
1638 : : }
1639 : : }
1640 : :
1641 : :
1642 : : static void
1643 : 129 : print_scngrp (Ebl *ebl)
1644 : : {
1645 : : /* Find all relocation sections and handle them. */
1646 : 129 : Elf_Scn *scn = NULL;
1647 : :
1648 [ + + ]: 3744 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
1649 : : {
1650 : : /* Handle the section if it is a symbol table. */
1651 : 3615 : GElf_Shdr shdr_mem;
1652 : 3615 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
1653 : :
1654 [ + - + + ]: 3615 : if (shdr != NULL && shdr->sh_type == SHT_GROUP)
1655 : : {
1656 [ - + ]: 16 : if ((shdr->sh_flags & SHF_COMPRESSED) != 0)
1657 : : {
1658 [ # # ]: 0 : if (elf_compress (scn, 0, 0) < 0)
1659 : 0 : printf ("WARNING: %s [%zd]\n",
1660 : : _("Couldn't uncompress section"),
1661 : : elf_ndxscn (scn));
1662 : 0 : shdr = gelf_getshdr (scn, &shdr_mem);
1663 [ # # ]: 0 : if (unlikely (shdr == NULL))
1664 : 0 : error (EXIT_FAILURE, 0,
1665 : 0 : _("cannot get section [%zd] header: %s"),
1666 : : elf_ndxscn (scn),
1667 : : elf_errmsg (-1));
1668 : : }
1669 : 16 : handle_scngrp (ebl, scn, shdr);
1670 : : }
1671 : : }
1672 : 129 : }
1673 : :
1674 : :
1675 : : static const struct flags
1676 : : {
1677 : : int mask;
1678 : : const char *str;
1679 : : } dt_flags[] =
1680 : : {
1681 : : { DF_ORIGIN, "ORIGIN" },
1682 : : { DF_SYMBOLIC, "SYMBOLIC" },
1683 : : { DF_TEXTREL, "TEXTREL" },
1684 : : { DF_BIND_NOW, "BIND_NOW" },
1685 : : { DF_STATIC_TLS, "STATIC_TLS" }
1686 : : };
1687 : : static const int ndt_flags = sizeof (dt_flags) / sizeof (dt_flags[0]);
1688 : :
1689 : : static const struct flags dt_flags_1[] =
1690 : : {
1691 : : { DF_1_NOW, "NOW" },
1692 : : { DF_1_GLOBAL, "GLOBAL" },
1693 : : { DF_1_GROUP, "GROUP" },
1694 : : { DF_1_NODELETE, "NODELETE" },
1695 : : { DF_1_LOADFLTR, "LOADFLTR" },
1696 : : { DF_1_INITFIRST, "INITFIRST" },
1697 : : { DF_1_NOOPEN, "NOOPEN" },
1698 : : { DF_1_ORIGIN, "ORIGIN" },
1699 : : { DF_1_DIRECT, "DIRECT" },
1700 : : { DF_1_TRANS, "TRANS" },
1701 : : { DF_1_INTERPOSE, "INTERPOSE" },
1702 : : { DF_1_NODEFLIB, "NODEFLIB" },
1703 : : { DF_1_NODUMP, "NODUMP" },
1704 : : { DF_1_CONFALT, "CONFALT" },
1705 : : { DF_1_ENDFILTEE, "ENDFILTEE" },
1706 : : { DF_1_DISPRELDNE, "DISPRELDNE" },
1707 : : { DF_1_DISPRELPND, "DISPRELPND" },
1708 : : };
1709 : : static const int ndt_flags_1 = sizeof (dt_flags_1) / sizeof (dt_flags_1[0]);
1710 : :
1711 : : static const struct flags dt_feature_1[] =
1712 : : {
1713 : : { DTF_1_PARINIT, "PARINIT" },
1714 : : { DTF_1_CONFEXP, "CONFEXP" }
1715 : : };
1716 : : static const int ndt_feature_1 = (sizeof (dt_feature_1)
1717 : : / sizeof (dt_feature_1[0]));
1718 : :
1719 : : static const struct flags dt_posflag_1[] =
1720 : : {
1721 : : { DF_P1_LAZYLOAD, "LAZYLOAD" },
1722 : : { DF_P1_GROUPPERM, "GROUPPERM" }
1723 : : };
1724 : : static const int ndt_posflag_1 = (sizeof (dt_posflag_1)
1725 : : / sizeof (dt_posflag_1[0]));
1726 : :
1727 : :
1728 : : static void
1729 : 37 : print_flags (int class, GElf_Xword d_val, const struct flags *flags,
1730 : : int nflags)
1731 : : {
1732 : 37 : bool first = true;
1733 : 37 : int cnt;
1734 : :
1735 [ + + ]: 552 : for (cnt = 0; cnt < nflags; ++cnt)
1736 [ + + ]: 515 : if (d_val & flags[cnt].mask)
1737 : : {
1738 [ + + ]: 34 : if (!first)
1739 [ - + ]: 18 : putchar_unlocked (' ');
1740 : 34 : fputs_unlocked (flags[cnt].str, stdout);
1741 : 34 : d_val &= ~flags[cnt].mask;
1742 : 34 : first = false;
1743 : : }
1744 : :
1745 [ + + ]: 37 : if (d_val != 0)
1746 : : {
1747 [ + + ]: 31 : if (!first)
1748 [ - + ]: 10 : putchar_unlocked (' ');
1749 [ + + ]: 31 : printf ("%#0*" PRIx64, class == ELFCLASS32 ? 10 : 18, d_val);
1750 : : }
1751 : :
1752 [ - + ]: 37 : putchar_unlocked ('\n');
1753 : 37 : }
1754 : :
1755 : :
1756 : : static void
1757 : 7 : print_dt_flags (int class, GElf_Xword d_val)
1758 : : {
1759 : 7 : print_flags (class, d_val, dt_flags, ndt_flags);
1760 : 7 : }
1761 : :
1762 : :
1763 : : static void
1764 : 28 : print_dt_flags_1 (int class, GElf_Xword d_val)
1765 : : {
1766 : 28 : print_flags (class, d_val, dt_flags_1, ndt_flags_1);
1767 : 28 : }
1768 : :
1769 : :
1770 : : static void
1771 : 1 : print_dt_feature_1 (int class, GElf_Xword d_val)
1772 : : {
1773 : 1 : print_flags (class, d_val, dt_feature_1, ndt_feature_1);
1774 : 1 : }
1775 : :
1776 : :
1777 : : static void
1778 : 1 : print_dt_posflag_1 (int class, GElf_Xword d_val)
1779 : : {
1780 : 1 : print_flags (class, d_val, dt_posflag_1, ndt_posflag_1);
1781 : 1 : }
1782 : :
1783 : :
1784 : : static void
1785 : 67 : handle_dynamic (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr)
1786 : : {
1787 : 67 : int class = gelf_getclass (ebl->elf);
1788 : 67 : GElf_Shdr glink_mem;
1789 : 67 : GElf_Shdr *glink;
1790 : 67 : Elf_Data *data;
1791 : 67 : size_t cnt;
1792 : 67 : size_t shstrndx;
1793 : 67 : size_t sh_entsize;
1794 : :
1795 : : /* Get the data of the section. */
1796 : 67 : data = elf_getdata (scn, NULL);
1797 [ - + ]: 67 : if (data == NULL)
1798 : 0 : return;
1799 : :
1800 : : /* Get the section header string table index. */
1801 [ - + ]: 67 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
1802 : 0 : error (EXIT_FAILURE, 0,
1803 : 0 : _("cannot get section header string table index"));
1804 : :
1805 : 67 : sh_entsize = gelf_fsize (ebl->elf, ELF_T_DYN, 1, EV_CURRENT);
1806 : :
1807 : 67 : glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link), &glink_mem);
1808 [ - + ]: 67 : if (glink == NULL)
1809 : 0 : error (EXIT_FAILURE, 0, _("invalid sh_link value in section %zu"),
1810 : : elf_ndxscn (scn));
1811 : :
1812 : 134 : printf (ngettext ("\
1813 : : \nDynamic segment contains %lu entry:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'\n",
1814 : : "\
1815 : : \nDynamic segment contains %lu entries:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'\n",
1816 : : shdr->sh_size / sh_entsize),
1817 [ + + ]: 67 : (unsigned long int) (shdr->sh_size / sh_entsize),
1818 : : class == ELFCLASS32 ? 10 : 18, shdr->sh_addr,
1819 : : shdr->sh_offset,
1820 : 67 : (int) shdr->sh_link,
1821 : 67 : elf_strptr (ebl->elf, shstrndx, glink->sh_name));
1822 : 67 : fputs_unlocked (_(" Type Value\n"), stdout);
1823 : :
1824 [ + + ]: 2172 : for (cnt = 0; cnt < shdr->sh_size / sh_entsize; ++cnt)
1825 : : {
1826 : 2038 : GElf_Dyn dynmem;
1827 : 2038 : GElf_Dyn *dyn = gelf_getdyn (data, cnt, &dynmem);
1828 [ + - ]: 2038 : if (dyn == NULL)
1829 : : break;
1830 : :
1831 : 2038 : char buf[64];
1832 : 2038 : printf (" %-17s ",
1833 : : ebl_dynamic_tag_name (ebl, dyn->d_tag, buf, sizeof (buf)));
1834 : :
1835 [ + + + + : 2038 : switch (dyn->d_tag)
+ + + + +
+ + + + ]
1836 : : {
1837 : 434 : case DT_NULL:
1838 : : case DT_DEBUG:
1839 : : case DT_BIND_NOW:
1840 : : case DT_TEXTREL:
1841 : : /* No further output. */
1842 [ - + ]: 434 : fputc_unlocked ('\n', stdout);
1843 : : break;
1844 : :
1845 : 118 : case DT_NEEDED:
1846 : 118 : printf (_("Shared library: [%s]\n"),
1847 : 118 : elf_strptr (ebl->elf, shdr->sh_link, dyn->d_un.d_val));
1848 : : break;
1849 : :
1850 : 3 : case DT_SONAME:
1851 : 3 : printf (_("Library soname: [%s]\n"),
1852 : 3 : elf_strptr (ebl->elf, shdr->sh_link, dyn->d_un.d_val));
1853 : : break;
1854 : :
1855 : 1 : case DT_RPATH:
1856 : 1 : printf (_("Library rpath: [%s]\n"),
1857 : 1 : elf_strptr (ebl->elf, shdr->sh_link, dyn->d_un.d_val));
1858 : : break;
1859 : :
1860 : 1 : case DT_RUNPATH:
1861 : 1 : printf (_("Library runpath: [%s]\n"),
1862 : 1 : elf_strptr (ebl->elf, shdr->sh_link, dyn->d_un.d_val));
1863 : : break;
1864 : :
1865 : 460 : case DT_PLTRELSZ:
1866 : : case DT_RELASZ:
1867 : : case DT_STRSZ:
1868 : : case DT_RELSZ:
1869 : : case DT_RELAENT:
1870 : : case DT_SYMENT:
1871 : : case DT_RELENT:
1872 : : case DT_PLTPADSZ:
1873 : : case DT_MOVEENT:
1874 : : case DT_MOVESZ:
1875 : : case DT_INIT_ARRAYSZ:
1876 : : case DT_FINI_ARRAYSZ:
1877 : : case DT_SYMINSZ:
1878 : : case DT_SYMINENT:
1879 : : case DT_GNU_CONFLICTSZ:
1880 : : case DT_GNU_LIBLISTSZ:
1881 : 460 : printf (_("%" PRId64 " (bytes)\n"), dyn->d_un.d_val);
1882 : : break;
1883 : :
1884 : 103 : case DT_VERDEFNUM:
1885 : : case DT_VERNEEDNUM:
1886 : : case DT_RELACOUNT:
1887 : : case DT_RELCOUNT:
1888 : 103 : printf ("%" PRId64 "\n", dyn->d_un.d_val);
1889 : : break;
1890 : :
1891 : 67 : case DT_PLTREL:;
1892 : 67 : const char *tagname = ebl_dynamic_tag_name (ebl, dyn->d_un.d_val,
1893 : : NULL, 0);
1894 [ + + ]: 67 : puts (tagname ?: "???");
1895 : 67 : break;
1896 : :
1897 : 7 : case DT_FLAGS:
1898 : 7 : print_dt_flags (class, dyn->d_un.d_val);
1899 : : break;
1900 : :
1901 : 28 : case DT_FLAGS_1:
1902 : 28 : print_dt_flags_1 (class, dyn->d_un.d_val);
1903 : : break;
1904 : :
1905 : 1 : case DT_FEATURE_1:
1906 : 1 : print_dt_feature_1 (class, dyn->d_un.d_val);
1907 : : break;
1908 : :
1909 : 1 : case DT_POSFLAG_1:
1910 : 1 : print_dt_posflag_1 (class, dyn->d_un.d_val);
1911 : : break;
1912 : :
1913 : : default:
1914 : 2038 : printf ("%#0*" PRIx64 "\n",
1915 : : class == ELFCLASS32 ? 10 : 18, dyn->d_un.d_val);
1916 : : break;
1917 : : }
1918 : : }
1919 : : }
1920 : :
1921 : :
1922 : : /* Print the dynamic segment. */
1923 : : static void
1924 : 122 : print_dynamic (Ebl *ebl)
1925 : : {
1926 [ + + ]: 514 : for (size_t i = 0; i < phnum; ++i)
1927 : : {
1928 : 480 : GElf_Phdr phdr_mem;
1929 : 480 : GElf_Phdr *phdr = gelf_getphdr (ebl->elf, i, &phdr_mem);
1930 : :
1931 [ + - + + ]: 480 : if (phdr != NULL && phdr->p_type == PT_DYNAMIC)
1932 : : {
1933 : 88 : Elf_Scn *scn = gelf_offscn (ebl->elf, phdr->p_offset);
1934 : 88 : GElf_Shdr shdr_mem;
1935 : 88 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
1936 [ + + + + ]: 88 : if (shdr != NULL && shdr->sh_type == SHT_DYNAMIC)
1937 : 67 : handle_dynamic (ebl, scn, shdr);
1938 : 88 : break;
1939 : : }
1940 : : }
1941 : 122 : }
1942 : :
1943 : :
1944 : : /* Print relocations. */
1945 : : static void
1946 : 121 : print_relocs (Ebl *ebl, GElf_Ehdr *ehdr)
1947 : : {
1948 : : /* Find all relocation sections and handle them. */
1949 : 121 : Elf_Scn *scn = NULL;
1950 : :
1951 [ + + ]: 3520 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
1952 : : {
1953 : : /* Handle the section if it is a symbol table. */
1954 : 3399 : GElf_Shdr shdr_mem;
1955 : 3399 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
1956 : :
1957 [ + - ]: 3399 : if (likely (shdr != NULL))
1958 : : {
1959 [ + + ]: 3399 : if (shdr->sh_type == SHT_REL)
1960 : 52 : handle_relocs_rel (ebl, ehdr, scn, shdr);
1961 [ + + ]: 3347 : else if (shdr->sh_type == SHT_RELA)
1962 : 252 : handle_relocs_rela (ebl, ehdr, scn, shdr);
1963 : : }
1964 : : }
1965 : 121 : }
1966 : :
1967 : :
1968 : : /* Handle a relocation section. */
1969 : : static void
1970 : 52 : handle_relocs_rel (Ebl *ebl, GElf_Ehdr *ehdr, Elf_Scn *scn, GElf_Shdr *shdr)
1971 : : {
1972 : 52 : int class = gelf_getclass (ebl->elf);
1973 : 52 : size_t sh_entsize = gelf_fsize (ebl->elf, ELF_T_REL, 1, EV_CURRENT);
1974 : 52 : int nentries = shdr->sh_size / sh_entsize;
1975 : :
1976 : : /* Get the data of the section. */
1977 : 52 : Elf_Data *data = elf_getdata (scn, NULL);
1978 [ + - ]: 52 : if (data == NULL)
1979 : 0 : return;
1980 : :
1981 : : /* Get the symbol table information. */
1982 : 52 : Elf_Scn *symscn = elf_getscn (ebl->elf, shdr->sh_link);
1983 : 52 : GElf_Shdr symshdr_mem;
1984 : 52 : GElf_Shdr *symshdr = gelf_getshdr (symscn, &symshdr_mem);
1985 : 52 : Elf_Data *symdata = elf_getdata (symscn, NULL);
1986 : :
1987 : : /* Get the section header of the section the relocations are for. */
1988 : 52 : GElf_Shdr destshdr_mem;
1989 : 52 : GElf_Shdr *destshdr = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_info),
1990 : : &destshdr_mem);
1991 : :
1992 [ + - - + ]: 52 : if (unlikely (symshdr == NULL || symdata == NULL || destshdr == NULL))
1993 : : {
1994 : 0 : printf (_("\nInvalid symbol table at offset %#0" PRIx64 "\n"),
1995 : : shdr->sh_offset);
1996 : 0 : return;
1997 : : }
1998 : :
1999 : : /* Search for the optional extended section index table. */
2000 : 52 : Elf_Data *xndxdata = NULL;
2001 : 52 : int xndxscnidx = elf_scnshndx (scn);
2002 [ - + ]: 52 : if (unlikely (xndxscnidx > 0))
2003 : 0 : xndxdata = elf_getdata (elf_getscn (ebl->elf, xndxscnidx), NULL);
2004 : :
2005 : : /* Get the section header string table index. */
2006 : 52 : size_t shstrndx;
2007 [ - + ]: 52 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
2008 : 0 : error (EXIT_FAILURE, 0,
2009 : 0 : _("cannot get section header string table index"));
2010 : :
2011 [ + + ]: 52 : if (shdr->sh_info != 0)
2012 : 88 : printf (ngettext ("\
2013 : : \nRelocation section [%2zu] '%s' for section [%2u] '%s' at offset %#0" PRIx64 " contains %d entry:\n",
2014 : : "\
2015 : : \nRelocation section [%2zu] '%s' for section [%2u] '%s' at offset %#0" PRIx64 " contains %d entries:\n",
2016 : : nentries),
2017 : : elf_ndxscn (scn),
2018 : 44 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
2019 : 44 : (unsigned int) shdr->sh_info,
2020 : 44 : elf_strptr (ebl->elf, shstrndx, destshdr->sh_name),
2021 : : shdr->sh_offset,
2022 : : nentries);
2023 : : else
2024 : : /* The .rel.dyn section does not refer to a specific section but
2025 : : instead of section index zero. Do not try to print a section
2026 : : name. */
2027 : 16 : printf (ngettext ("\
2028 : : \nRelocation section [%2u] '%s' at offset %#0" PRIx64 " contains %d entry:\n",
2029 : : "\
2030 : : \nRelocation section [%2u] '%s' at offset %#0" PRIx64 " contains %d entries:\n",
2031 : : nentries),
2032 : 8 : (unsigned int) elf_ndxscn (scn),
2033 : 8 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
2034 : : shdr->sh_offset,
2035 : : nentries);
2036 [ + - ]: 52 : fputs_unlocked (class == ELFCLASS32
2037 : 52 : ? _("\
2038 : : Offset Type Value Name\n")
2039 : 0 : : _("\
2040 : : Offset Type Value Name\n"),
2041 : : stdout);
2042 : :
2043 : 52 : int is_statically_linked = 0;
2044 [ + + ]: 12997 : for (int cnt = 0; cnt < nentries; ++cnt)
2045 : : {
2046 : 12945 : GElf_Rel relmem;
2047 : 12945 : GElf_Rel *rel = gelf_getrel (data, cnt, &relmem);
2048 [ + - ]: 12945 : if (likely (rel != NULL))
2049 : : {
2050 : 12945 : char buf[128];
2051 : 12945 : GElf_Sym symmem;
2052 : 12945 : Elf32_Word xndx;
2053 : 25890 : GElf_Sym *sym = gelf_getsymshndx (symdata, xndxdata,
2054 : 12945 : GELF_R_SYM (rel->r_info),
2055 : : &symmem, &xndx);
2056 [ - + ]: 12945 : if (unlikely (sym == NULL))
2057 : : {
2058 : : /* As a special case we have to handle relocations in static
2059 : : executables. This only happens for IRELATIVE relocations
2060 : : (so far). There is no symbol table. */
2061 [ # # ]: 0 : if (is_statically_linked == 0)
2062 : : {
2063 : : /* Find the program header and look for a PT_INTERP entry. */
2064 : 0 : is_statically_linked = -1;
2065 [ # # ]: 0 : if (ehdr->e_type == ET_EXEC)
2066 : : {
2067 : 0 : is_statically_linked = 1;
2068 : :
2069 [ # # ]: 0 : for (size_t inner = 0; inner < phnum; ++inner)
2070 : : {
2071 : 0 : GElf_Phdr phdr_mem;
2072 : 0 : GElf_Phdr *phdr = gelf_getphdr (ebl->elf, inner,
2073 : : &phdr_mem);
2074 [ # # # # ]: 0 : if (phdr != NULL && phdr->p_type == PT_INTERP)
2075 : : {
2076 : 0 : is_statically_linked = -1;
2077 : 0 : break;
2078 : : }
2079 : : }
2080 : : }
2081 : : }
2082 : :
2083 [ # # # # ]: 0 : if (is_statically_linked > 0 && shdr->sh_link == 0)
2084 [ # # # # ]: 0 : printf ("\
2085 : : %#0*" PRIx64 " %-20s %*s %s\n",
2086 : : class == ELFCLASS32 ? 10 : 18, rel->r_offset,
2087 : 0 : ebl_reloc_type_check (ebl, GELF_R_TYPE (rel->r_info))
2088 : : /* Avoid the leading R_ which isn't carrying any
2089 : : information. */
2090 : 0 : ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info),
2091 : : buf, sizeof (buf)) + 2
2092 : 0 : : _("<INVALID RELOC>"),
2093 : : class == ELFCLASS32 ? 10 : 18, "",
2094 : 0 : elf_strptr (ebl->elf, shstrndx, destshdr->sh_name));
2095 : : else
2096 [ # # # # ]: 0 : printf (" %#0*" PRIx64 " %-20s <%s %ld>\n",
2097 : : class == ELFCLASS32 ? 10 : 18, rel->r_offset,
2098 : 0 : ebl_reloc_type_check (ebl, GELF_R_TYPE (rel->r_info))
2099 : : /* Avoid the leading R_ which isn't carrying any
2100 : : information. */
2101 : 0 : ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info),
2102 : : buf, sizeof (buf)) + 2
2103 : 0 : : _("<INVALID RELOC>"),
2104 : : _("INVALID SYMBOL"),
2105 : 0 : (long int) GELF_R_SYM (rel->r_info));
2106 : : }
2107 [ + + ]: 12945 : else if (GELF_ST_TYPE (sym->st_info) != STT_SECTION)
2108 [ - + ]: 13099 : printf (" %#0*" PRIx64 " %-20s %#0*" PRIx64 " %s\n",
2109 : : class == ELFCLASS32 ? 10 : 18, rel->r_offset,
2110 [ + - ]: 77 : likely (ebl_reloc_type_check (ebl,
2111 : : GELF_R_TYPE (rel->r_info)))
2112 : : /* Avoid the leading R_ which isn't carrying any
2113 : : information. */
2114 : 77 : ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info),
2115 : : buf, sizeof (buf)) + 2
2116 : 0 : : _("<INVALID RELOC>"),
2117 : : class == ELFCLASS32 ? 10 : 18, sym->st_value,
2118 : 77 : elf_strptr (ebl->elf, symshdr->sh_link, sym->st_name));
2119 : : else
2120 : : {
2121 : : /* This is a relocation against a STT_SECTION symbol. */
2122 : 12868 : GElf_Shdr secshdr_mem;
2123 : 12868 : GElf_Shdr *secshdr;
2124 : 12868 : secshdr = gelf_getshdr (elf_getscn (ebl->elf,
2125 [ + - ]: 12868 : sym->st_shndx == SHN_XINDEX
2126 : 0 : ? xndx : sym->st_shndx),
2127 : : &secshdr_mem);
2128 : :
2129 [ - + ]: 12868 : if (unlikely (secshdr == NULL))
2130 [ # # # # ]: 0 : printf (" %#0*" PRIx64 " %-20s <%s %ld>\n",
2131 : : class == ELFCLASS32 ? 10 : 18, rel->r_offset,
2132 : 0 : ebl_reloc_type_check (ebl, GELF_R_TYPE (rel->r_info))
2133 : : /* Avoid the leading R_ which isn't carrying any
2134 : : information. */
2135 : 0 : ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info),
2136 : : buf, sizeof (buf)) + 2
2137 : 0 : : _("<INVALID RELOC>"),
2138 : : _("INVALID SECTION"),
2139 [ # # ]: 0 : (long int) (sym->st_shndx == SHN_XINDEX
2140 : 0 : ? xndx : sym->st_shndx));
2141 : : else
2142 [ - + + - ]: 51472 : printf (" %#0*" PRIx64 " %-20s %#0*" PRIx64 " %s\n",
2143 : : class == ELFCLASS32 ? 10 : 18, rel->r_offset,
2144 : 12868 : ebl_reloc_type_check (ebl, GELF_R_TYPE (rel->r_info))
2145 : : /* Avoid the leading R_ which isn't carrying any
2146 : : information. */
2147 : 12868 : ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info),
2148 : : buf, sizeof (buf)) + 2
2149 : 0 : : _("<INVALID RELOC>"),
2150 : : class == ELFCLASS32 ? 10 : 18, sym->st_value,
2151 : 12868 : elf_strptr (ebl->elf, shstrndx, secshdr->sh_name));
2152 : : }
2153 : : }
2154 : : }
2155 : : }
2156 : :
2157 : :
2158 : : /* Handle a relocation section. */
2159 : : static void
2160 : 252 : handle_relocs_rela (Ebl *ebl, GElf_Ehdr *ehdr, Elf_Scn *scn, GElf_Shdr *shdr)
2161 : : {
2162 : 252 : int class = gelf_getclass (ebl->elf);
2163 : 252 : size_t sh_entsize = gelf_fsize (ebl->elf, ELF_T_RELA, 1, EV_CURRENT);
2164 : 252 : int nentries = shdr->sh_size / sh_entsize;
2165 : :
2166 : : /* Get the data of the section. */
2167 : 252 : Elf_Data *data = elf_getdata (scn, NULL);
2168 [ + - ]: 252 : if (data == NULL)
2169 : 0 : return;
2170 : :
2171 : : /* Get the symbol table information. */
2172 : 252 : Elf_Scn *symscn = elf_getscn (ebl->elf, shdr->sh_link);
2173 : 252 : GElf_Shdr symshdr_mem;
2174 : 252 : GElf_Shdr *symshdr = gelf_getshdr (symscn, &symshdr_mem);
2175 : 252 : Elf_Data *symdata = elf_getdata (symscn, NULL);
2176 : :
2177 : : /* Get the section header of the section the relocations are for. */
2178 : 252 : GElf_Shdr destshdr_mem;
2179 : 252 : GElf_Shdr *destshdr = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_info),
2180 : : &destshdr_mem);
2181 : :
2182 [ + - - + ]: 252 : if (unlikely (symshdr == NULL || symdata == NULL || destshdr == NULL))
2183 : : {
2184 : 0 : printf (_("\nInvalid symbol table at offset %#0" PRIx64 "\n"),
2185 : : shdr->sh_offset);
2186 : 0 : return;
2187 : : }
2188 : :
2189 : : /* Search for the optional extended section index table. */
2190 : 252 : Elf_Data *xndxdata = NULL;
2191 : 252 : int xndxscnidx = elf_scnshndx (scn);
2192 [ - + ]: 252 : if (unlikely (xndxscnidx > 0))
2193 : 0 : xndxdata = elf_getdata (elf_getscn (ebl->elf, xndxscnidx), NULL);
2194 : :
2195 : : /* Get the section header string table index. */
2196 : 252 : size_t shstrndx;
2197 [ - + ]: 252 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
2198 : 0 : error (EXIT_FAILURE, 0,
2199 : 0 : _("cannot get section header string table index"));
2200 : :
2201 [ + + ]: 252 : if (shdr->sh_info != 0)
2202 : 390 : printf (ngettext ("\
2203 : : \nRelocation section [%2zu] '%s' for section [%2u] '%s' at offset %#0" PRIx64 " contains %d entry:\n",
2204 : : "\
2205 : : \nRelocation section [%2zu] '%s' for section [%2u] '%s' at offset %#0" PRIx64 " contains %d entries:\n",
2206 : : nentries),
2207 : : elf_ndxscn (scn),
2208 : 195 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
2209 : 195 : (unsigned int) shdr->sh_info,
2210 : 195 : elf_strptr (ebl->elf, shstrndx, destshdr->sh_name),
2211 : : shdr->sh_offset,
2212 : : nentries);
2213 : : else
2214 : : /* The .rela.dyn section does not refer to a specific section but
2215 : : instead of section index zero. Do not try to print a section
2216 : : name. */
2217 : 114 : printf (ngettext ("\
2218 : : \nRelocation section [%2u] '%s' at offset %#0" PRIx64 " contains %d entry:\n",
2219 : : "\
2220 : : \nRelocation section [%2u] '%s' at offset %#0" PRIx64 " contains %d entries:\n",
2221 : : nentries),
2222 : 57 : (unsigned int) elf_ndxscn (scn),
2223 : 57 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
2224 : : shdr->sh_offset,
2225 : : nentries);
2226 [ + + ]: 252 : fputs_unlocked (class == ELFCLASS32
2227 : 10 : ? _("\
2228 : : Offset Type Value Addend Name\n")
2229 : 242 : : _("\
2230 : : Offset Type Value Addend Name\n"),
2231 : : stdout);
2232 : :
2233 : 252 : int is_statically_linked = 0;
2234 [ + + ]: 82481 : for (int cnt = 0; cnt < nentries; ++cnt)
2235 : : {
2236 : 82229 : GElf_Rela relmem;
2237 : 82229 : GElf_Rela *rel = gelf_getrela (data, cnt, &relmem);
2238 [ + - ]: 82229 : if (likely (rel != NULL))
2239 : : {
2240 : 82229 : char buf[64];
2241 : 82229 : GElf_Sym symmem;
2242 : 82229 : Elf32_Word xndx;
2243 : 164458 : GElf_Sym *sym = gelf_getsymshndx (symdata, xndxdata,
2244 : 82229 : GELF_R_SYM (rel->r_info),
2245 : : &symmem, &xndx);
2246 : :
2247 [ - + ]: 82229 : if (unlikely (sym == NULL))
2248 : : {
2249 : : /* As a special case we have to handle relocations in static
2250 : : executables. This only happens for IRELATIVE relocations
2251 : : (so far). There is no symbol table. */
2252 [ # # ]: 0 : if (is_statically_linked == 0)
2253 : : {
2254 : : /* Find the program header and look for a PT_INTERP entry. */
2255 : 0 : is_statically_linked = -1;
2256 [ # # ]: 0 : if (ehdr->e_type == ET_EXEC)
2257 : : {
2258 : 0 : is_statically_linked = 1;
2259 : :
2260 [ # # ]: 0 : for (size_t inner = 0; inner < phnum; ++inner)
2261 : : {
2262 : 0 : GElf_Phdr phdr_mem;
2263 : 0 : GElf_Phdr *phdr = gelf_getphdr (ebl->elf, inner,
2264 : : &phdr_mem);
2265 [ # # # # ]: 0 : if (phdr != NULL && phdr->p_type == PT_INTERP)
2266 : : {
2267 : 0 : is_statically_linked = -1;
2268 : 0 : break;
2269 : : }
2270 : : }
2271 : : }
2272 : : }
2273 : :
2274 [ # # # # ]: 0 : if (is_statically_linked > 0 && shdr->sh_link == 0)
2275 [ # # # # ]: 0 : printf ("\
2276 : : %#0*" PRIx64 " %-15s %*s %#6" PRIx64 " %s\n",
2277 : : class == ELFCLASS32 ? 10 : 18, rel->r_offset,
2278 : 0 : ebl_reloc_type_check (ebl, GELF_R_TYPE (rel->r_info))
2279 : : /* Avoid the leading R_ which isn't carrying any
2280 : : information. */
2281 : 0 : ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info),
2282 : : buf, sizeof (buf)) + 2
2283 : 0 : : _("<INVALID RELOC>"),
2284 : : class == ELFCLASS32 ? 10 : 18, "",
2285 : : rel->r_addend,
2286 : 0 : elf_strptr (ebl->elf, shstrndx, destshdr->sh_name));
2287 : : else
2288 [ # # # # ]: 0 : printf (" %#0*" PRIx64 " %-15s <%s %ld>\n",
2289 : : class == ELFCLASS32 ? 10 : 18, rel->r_offset,
2290 : 0 : ebl_reloc_type_check (ebl, GELF_R_TYPE (rel->r_info))
2291 : : /* Avoid the leading R_ which isn't carrying any
2292 : : information. */
2293 : 0 : ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info),
2294 : : buf, sizeof (buf)) + 2
2295 : 0 : : _("<INVALID RELOC>"),
2296 : : _("INVALID SYMBOL"),
2297 : 0 : (long int) GELF_R_SYM (rel->r_info));
2298 : : }
2299 [ + + ]: 82229 : else if (GELF_ST_TYPE (sym->st_info) != STT_SECTION)
2300 [ + + ]: 208537 : printf ("\
2301 : : %#0*" PRIx64 " %-15s %#0*" PRIx64 " %+6" PRId64 " %s\n",
2302 : : class == ELFCLASS32 ? 10 : 18, rel->r_offset,
2303 [ + - ]: 63154 : likely (ebl_reloc_type_check (ebl,
2304 : : GELF_R_TYPE (rel->r_info)))
2305 : : /* Avoid the leading R_ which isn't carrying any
2306 : : information. */
2307 : 63154 : ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info),
2308 : : buf, sizeof (buf)) + 2
2309 : 0 : : _("<INVALID RELOC>"),
2310 : : class == ELFCLASS32 ? 10 : 18, sym->st_value,
2311 : : rel->r_addend,
2312 : 63154 : elf_strptr (ebl->elf, symshdr->sh_link, sym->st_name));
2313 : : else
2314 : : {
2315 : : /* This is a relocation against a STT_SECTION symbol. */
2316 : 19075 : GElf_Shdr secshdr_mem;
2317 : 19075 : GElf_Shdr *secshdr;
2318 : 19075 : secshdr = gelf_getshdr (elf_getscn (ebl->elf,
2319 [ + - ]: 19075 : sym->st_shndx == SHN_XINDEX
2320 : 0 : ? xndx : sym->st_shndx),
2321 : : &secshdr_mem);
2322 : :
2323 [ - + ]: 19075 : if (unlikely (secshdr == NULL))
2324 [ # # # # ]: 0 : printf (" %#0*" PRIx64 " %-15s <%s %ld>\n",
2325 : : class == ELFCLASS32 ? 10 : 18, rel->r_offset,
2326 : 0 : ebl_reloc_type_check (ebl, GELF_R_TYPE (rel->r_info))
2327 : : /* Avoid the leading R_ which isn't carrying any
2328 : : information. */
2329 : 0 : ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info),
2330 : : buf, sizeof (buf)) + 2
2331 : 0 : : _("<INVALID RELOC>"),
2332 : : _("INVALID SECTION"),
2333 [ # # ]: 0 : (long int) (sym->st_shndx == SHN_XINDEX
2334 : 0 : ? xndx : sym->st_shndx));
2335 : : else
2336 [ + + + - ]: 76300 : printf ("\
2337 : : %#0*" PRIx64 " %-15s %#0*" PRIx64 " %+6" PRId64 " %s\n",
2338 : : class == ELFCLASS32 ? 10 : 18, rel->r_offset,
2339 : 19075 : ebl_reloc_type_check (ebl, GELF_R_TYPE (rel->r_info))
2340 : : /* Avoid the leading R_ which isn't carrying any
2341 : : information. */
2342 : 19075 : ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info),
2343 : : buf, sizeof (buf)) + 2
2344 : 0 : : _("<INVALID RELOC>"),
2345 : : class == ELFCLASS32 ? 10 : 18, sym->st_value,
2346 : : rel->r_addend,
2347 : 19075 : elf_strptr (ebl->elf, shstrndx, secshdr->sh_name));
2348 : : }
2349 : : }
2350 : : }
2351 : : }
2352 : :
2353 : :
2354 : : /* Print the program header. */
2355 : : static void
2356 : 269 : print_symtab (Ebl *ebl, int type)
2357 : : {
2358 : : /* Find the symbol table(s). For this we have to search through the
2359 : : section table. */
2360 : 269 : Elf_Scn *scn = NULL;
2361 : :
2362 [ + + ]: 7792 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
2363 : : {
2364 : : /* Handle the section if it is a symbol table. */
2365 : 7523 : GElf_Shdr shdr_mem;
2366 : 7523 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
2367 : :
2368 [ + - + + ]: 7523 : if (shdr != NULL && shdr->sh_type == (GElf_Word) type)
2369 : : {
2370 [ + + ]: 158 : if (symbol_table_section != NULL)
2371 : : {
2372 : : /* Get the section header string table index. */
2373 : 4 : size_t shstrndx;
2374 : 4 : const char *sname;
2375 [ - + ]: 4 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
2376 : 0 : error (EXIT_FAILURE, 0,
2377 : 0 : _("cannot get section header string table index"));
2378 : 4 : sname = elf_strptr (ebl->elf, shstrndx, shdr->sh_name);
2379 [ + - + + ]: 4 : if (sname == NULL || strcmp (sname, symbol_table_section) != 0)
2380 : 2 : continue;
2381 : : }
2382 : :
2383 [ - + ]: 156 : if ((shdr->sh_flags & SHF_COMPRESSED) != 0)
2384 : : {
2385 [ # # ]: 0 : if (elf_compress (scn, 0, 0) < 0)
2386 : 0 : printf ("WARNING: %s [%zd]\n",
2387 : : _("Couldn't uncompress section"),
2388 : : elf_ndxscn (scn));
2389 : 0 : shdr = gelf_getshdr (scn, &shdr_mem);
2390 [ # # ]: 0 : if (unlikely (shdr == NULL))
2391 : 0 : error (EXIT_FAILURE, 0,
2392 : 0 : _("cannot get section [%zd] header: %s"),
2393 : : elf_ndxscn (scn), elf_errmsg (-1));
2394 : : }
2395 : 156 : handle_symtab (ebl, scn, shdr);
2396 : : }
2397 : : }
2398 : 269 : }
2399 : :
2400 : :
2401 : : static void
2402 : 156 : handle_symtab (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr)
2403 : : {
2404 : 156 : Elf_Data *versym_data = NULL;
2405 : 156 : Elf_Data *verneed_data = NULL;
2406 : 156 : Elf_Data *verdef_data = NULL;
2407 : 156 : Elf_Data *xndx_data = NULL;
2408 : 156 : int class = gelf_getclass (ebl->elf);
2409 : 156 : Elf32_Word verneed_stridx = 0;
2410 : 156 : Elf32_Word verdef_stridx = 0;
2411 : :
2412 : : /* Get the data of the section. */
2413 : 156 : Elf_Data *data = elf_getdata (scn, NULL);
2414 [ - + ]: 156 : if (data == NULL)
2415 : 0 : return;
2416 : :
2417 : : /* Find out whether we have other sections we might need. */
2418 : : Elf_Scn *runscn = NULL;
2419 [ + + ]: 4660 : while ((runscn = elf_nextscn (ebl->elf, runscn)) != NULL)
2420 : : {
2421 : 4504 : GElf_Shdr runshdr_mem;
2422 : 4504 : GElf_Shdr *runshdr = gelf_getshdr (runscn, &runshdr_mem);
2423 : :
2424 [ + - ]: 4504 : if (likely (runshdr != NULL))
2425 : : {
2426 [ + + ]: 4504 : if (runshdr->sh_type == SHT_GNU_versym
2427 [ + + ]: 96 : && runshdr->sh_link == elf_ndxscn (scn))
2428 : : /* Bingo, found the version information. Now get the data. */
2429 : 73 : versym_data = elf_getdata (runscn, NULL);
2430 [ + + ]: 4431 : else if (runshdr->sh_type == SHT_GNU_verneed)
2431 : : {
2432 : : /* This is the information about the needed versions. */
2433 : 96 : verneed_data = elf_getdata (runscn, NULL);
2434 : 96 : verneed_stridx = runshdr->sh_link;
2435 : : }
2436 [ + + ]: 4335 : else if (runshdr->sh_type == SHT_GNU_verdef)
2437 : : {
2438 : : /* This is the information about the defined versions. */
2439 : 4 : verdef_data = elf_getdata (runscn, NULL);
2440 : 4 : verdef_stridx = runshdr->sh_link;
2441 : : }
2442 [ - + ]: 4331 : else if (runshdr->sh_type == SHT_SYMTAB_SHNDX
2443 [ # # ]: 0 : && runshdr->sh_link == elf_ndxscn (scn))
2444 : : /* Extended section index. */
2445 : 0 : xndx_data = elf_getdata (runscn, NULL);
2446 : : }
2447 : : }
2448 : :
2449 : : /* Get the section header string table index. */
2450 : 156 : size_t shstrndx;
2451 [ - + ]: 156 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
2452 : 0 : error (EXIT_FAILURE, 0,
2453 : 0 : _("cannot get section header string table index"));
2454 : :
2455 : 156 : GElf_Shdr glink_mem;
2456 : 156 : GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
2457 : : &glink_mem);
2458 [ - + ]: 156 : if (glink == NULL)
2459 : 0 : error (EXIT_FAILURE, 0, _("invalid sh_link value in section %zu"),
2460 : : elf_ndxscn (scn));
2461 : :
2462 : : /* Now we can compute the number of entries in the section. */
2463 : 312 : unsigned int nsyms = data->d_size / (class == ELFCLASS32
2464 : : ? sizeof (Elf32_Sym)
2465 [ + + ]: 156 : : sizeof (Elf64_Sym));
2466 : :
2467 : 312 : printf (ngettext ("\nSymbol table [%2u] '%s' contains %u entry:\n",
2468 : : "\nSymbol table [%2u] '%s' contains %u entries:\n",
2469 : : nsyms),
2470 : 156 : (unsigned int) elf_ndxscn (scn),
2471 : 156 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name), nsyms);
2472 : 312 : printf (ngettext (" %lu local symbol String table: [%2u] '%s'\n",
2473 : : " %lu local symbols String table: [%2u] '%s'\n",
2474 : : shdr->sh_info),
2475 : 156 : (unsigned long int) shdr->sh_info,
2476 : 156 : (unsigned int) shdr->sh_link,
2477 : 156 : elf_strptr (ebl->elf, shstrndx, glink->sh_name));
2478 : :
2479 [ + + ]: 156 : fputs_unlocked (class == ELFCLASS32
2480 : 22 : ? _("\
2481 : : Num: Value Size Type Bind Vis Ndx Name\n")
2482 : 134 : : _("\
2483 : : Num: Value Size Type Bind Vis Ndx Name\n"),
2484 : : stdout);
2485 : :
2486 [ + + ]: 58863 : for (unsigned int cnt = 0; cnt < nsyms; ++cnt)
2487 : : {
2488 : 58707 : char typebuf[64];
2489 : 58707 : char bindbuf[64];
2490 : 58707 : char scnbuf[64];
2491 : 58707 : Elf32_Word xndx;
2492 : 58707 : GElf_Sym sym_mem;
2493 : 58707 : GElf_Sym *sym = gelf_getsymshndx (data, xndx_data, cnt, &sym_mem, &xndx);
2494 : :
2495 [ - + ]: 58707 : if (unlikely (sym == NULL))
2496 : 0 : continue;
2497 : :
2498 : : /* Determine the real section index. */
2499 [ + - ]: 58707 : if (likely (sym->st_shndx != SHN_XINDEX))
2500 : 58707 : xndx = sym->st_shndx;
2501 : :
2502 [ + + ]: 117414 : printf (_("\
2503 : : %5u: %0*" PRIx64 " %6" PRId64 " %-7s %-6s %-9s %6s %s"),
2504 : : cnt,
2505 : : class == ELFCLASS32 ? 8 : 16,
2506 : : sym->st_value,
2507 : : sym->st_size,
2508 : 58707 : ebl_symbol_type_name (ebl, GELF_ST_TYPE (sym->st_info),
2509 : : typebuf, sizeof (typebuf)),
2510 : 58707 : ebl_symbol_binding_name (ebl, GELF_ST_BIND (sym->st_info),
2511 : : bindbuf, sizeof (bindbuf)),
2512 : 58707 : get_visibility_type (GELF_ST_VISIBILITY (sym->st_other)),
2513 : 58707 : ebl_section_name (ebl, sym->st_shndx, xndx, scnbuf,
2514 : : sizeof (scnbuf), NULL, shnum),
2515 : 58707 : elf_strptr (ebl->elf, shdr->sh_link, sym->st_name));
2516 : :
2517 [ + + ]: 58707 : if (versym_data != NULL)
2518 : : {
2519 : : /* Get the version information. */
2520 : 1873 : GElf_Versym versym_mem;
2521 : 1873 : GElf_Versym *versym = gelf_getversym (versym_data, cnt, &versym_mem);
2522 : :
2523 [ + - + + ]: 1873 : if (versym != NULL && ((*versym & 0x8000) != 0 || *versym > 1))
2524 : : {
2525 : 1384 : bool is_nobits = false;
2526 : 1384 : bool check_def = xndx != SHN_UNDEF;
2527 : :
2528 [ + + - + ]: 1384 : if (xndx < SHN_LORESERVE || sym->st_shndx == SHN_XINDEX)
2529 : : {
2530 : 1374 : GElf_Shdr symshdr_mem;
2531 : 1374 : GElf_Shdr *symshdr =
2532 : 1374 : gelf_getshdr (elf_getscn (ebl->elf, xndx), &symshdr_mem);
2533 : :
2534 : 1374 : is_nobits = (symshdr != NULL
2535 [ + - + - ]: 1374 : && symshdr->sh_type == SHT_NOBITS);
2536 : : }
2537 : :
2538 [ + + ]: 1384 : if (is_nobits || ! check_def)
2539 : : {
2540 : : /* We must test both. */
2541 : 1232 : GElf_Vernaux vernaux_mem;
2542 : 1232 : GElf_Vernaux *vernaux = NULL;
2543 : 1232 : size_t vn_offset = 0;
2544 : :
2545 : 1232 : GElf_Verneed verneed_mem;
2546 : 1232 : GElf_Verneed *verneed = gelf_getverneed (verneed_data, 0,
2547 : : &verneed_mem);
2548 [ + - ]: 4575 : while (verneed != NULL)
2549 : : {
2550 : 4575 : size_t vna_offset = vn_offset;
2551 : :
2552 : 4575 : vernaux = gelf_getvernaux (verneed_data,
2553 : 4575 : vna_offset += verneed->vn_aux,
2554 : : &vernaux_mem);
2555 [ + - ]: 4623 : while (vernaux != NULL
2556 [ + + ]: 4623 : && vernaux->vna_other != *versym
2557 [ + + ]: 3391 : && vernaux->vna_next != 0
2558 : 48 : && (verneed_data->d_size - vna_offset
2559 [ + - ]: 48 : >= vernaux->vna_next))
2560 : : {
2561 : : /* Update the offset. */
2562 : 48 : vna_offset += vernaux->vna_next;
2563 : :
2564 : 48 : vernaux = (vernaux->vna_next == 0
2565 : : ? NULL
2566 : 48 : : gelf_getvernaux (verneed_data,
2567 : : vna_offset,
2568 : : &vernaux_mem));
2569 : : }
2570 : :
2571 : : /* Check whether we found the version. */
2572 [ + - + + ]: 4575 : if (vernaux != NULL && vernaux->vna_other == *versym)
2573 : : /* Found it. */
2574 : : break;
2575 : :
2576 [ + - ]: 3343 : if (verneed_data->d_size - vn_offset < verneed->vn_next)
2577 : : break;
2578 : :
2579 : 3343 : vn_offset += verneed->vn_next;
2580 : 3343 : verneed = (verneed->vn_next == 0
2581 : : ? NULL
2582 [ + - ]: 3343 : : gelf_getverneed (verneed_data, vn_offset,
2583 : : &verneed_mem));
2584 : : }
2585 : :
2586 [ + - + - ]: 1232 : if (vernaux != NULL && vernaux->vna_other == *versym)
2587 : : {
2588 : 3696 : printf ("@%s (%u)",
2589 : : elf_strptr (ebl->elf, verneed_stridx,
2590 : 1232 : vernaux->vna_name),
2591 : 1232 : (unsigned int) vernaux->vna_other);
2592 : 1232 : check_def = 0;
2593 : : }
2594 [ # # ]: 0 : else if (unlikely (! is_nobits))
2595 : 0 : error (0, 0, _("bad dynamic symbol"));
2596 : : else
2597 : : check_def = 1;
2598 : : }
2599 : :
2600 [ + - + - ]: 1384 : if (check_def && *versym != 0x8001)
2601 : : {
2602 : : /* We must test both. */
2603 : 152 : size_t vd_offset = 0;
2604 : :
2605 : 152 : GElf_Verdef verdef_mem;
2606 : 152 : GElf_Verdef *verdef = gelf_getverdef (verdef_data, 0,
2607 : : &verdef_mem);
2608 [ + - ]: 433 : while (verdef != NULL)
2609 : : {
2610 [ + + ]: 433 : if (verdef->vd_ndx == (*versym & 0x7fff))
2611 : : /* Found the definition. */
2612 : : break;
2613 : :
2614 [ + - ]: 281 : if (verdef_data->d_size - vd_offset < verdef->vd_next)
2615 : : break;
2616 : :
2617 : 281 : vd_offset += verdef->vd_next;
2618 : 281 : verdef = (verdef->vd_next == 0
2619 : : ? NULL
2620 [ + - ]: 281 : : gelf_getverdef (verdef_data, vd_offset,
2621 : : &verdef_mem));
2622 : : }
2623 : :
2624 [ + - ]: 152 : if (verdef != NULL)
2625 : : {
2626 : 152 : GElf_Verdaux verdaux_mem;
2627 : 152 : GElf_Verdaux *verdaux
2628 : 304 : = gelf_getverdaux (verdef_data,
2629 : 152 : vd_offset + verdef->vd_aux,
2630 : : &verdaux_mem);
2631 : :
2632 [ + - ]: 152 : if (verdaux != NULL)
2633 [ + - ]: 304 : printf ((*versym & 0x8000) ? "@%s" : "@@%s",
2634 : : elf_strptr (ebl->elf, verdef_stridx,
2635 : 152 : verdaux->vda_name));
2636 : : }
2637 : : }
2638 : : }
2639 : : }
2640 : :
2641 [ + + ]: 117414 : putchar_unlocked ('\n');
2642 : : }
2643 : : }
2644 : :
2645 : :
2646 : : /* Print version information. */
2647 : : static void
2648 : 120 : print_verinfo (Ebl *ebl)
2649 : : {
2650 : : /* Find the version information sections. For this we have to
2651 : : search through the section table. */
2652 : 120 : Elf_Scn *scn = NULL;
2653 : :
2654 [ + + ]: 3485 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
2655 : : {
2656 : : /* Handle the section if it is part of the versioning handling. */
2657 : 3365 : GElf_Shdr shdr_mem;
2658 : 3365 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
2659 : :
2660 [ + - ]: 3365 : if (likely (shdr != NULL))
2661 : : {
2662 [ + + ]: 3365 : if (shdr->sh_type == SHT_GNU_verneed)
2663 : 65 : handle_verneed (ebl, scn, shdr);
2664 [ + + ]: 3300 : else if (shdr->sh_type == SHT_GNU_verdef)
2665 : 2 : handle_verdef (ebl, scn, shdr);
2666 [ + + ]: 3298 : else if (shdr->sh_type == SHT_GNU_versym)
2667 : 65 : handle_versym (ebl, scn, shdr);
2668 : : }
2669 : : }
2670 : 120 : }
2671 : :
2672 : :
2673 : : static const char *
2674 : 111 : get_ver_flags (unsigned int flags)
2675 : : {
2676 : 111 : static char buf[32];
2677 : 111 : char *endp;
2678 : :
2679 [ + + ]: 111 : if (flags == 0)
2680 : 109 : return _("none");
2681 : :
2682 [ + - ]: 2 : if (flags & VER_FLG_BASE)
2683 : 2 : endp = stpcpy (buf, "BASE ");
2684 : : else
2685 : : endp = buf;
2686 : :
2687 [ - + ]: 2 : if (flags & VER_FLG_WEAK)
2688 : : {
2689 [ # # ]: 0 : if (endp != buf)
2690 : 0 : endp = stpcpy (endp, "| ");
2691 : :
2692 : 0 : endp = stpcpy (endp, "WEAK ");
2693 : : }
2694 : :
2695 [ - + ]: 2 : if (unlikely (flags & ~(VER_FLG_BASE | VER_FLG_WEAK)))
2696 : : {
2697 : 0 : strncpy (endp, _("| <unknown>"), buf + sizeof (buf) - endp);
2698 : 0 : buf[sizeof (buf) - 1] = '\0';
2699 : : }
2700 : :
2701 : : return buf;
2702 : : }
2703 : :
2704 : :
2705 : : static void
2706 : 65 : handle_verneed (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr)
2707 : : {
2708 : 65 : int class = gelf_getclass (ebl->elf);
2709 : :
2710 : : /* Get the data of the section. */
2711 : 65 : Elf_Data *data = elf_getdata (scn, NULL);
2712 [ - + ]: 65 : if (data == NULL)
2713 : 0 : return;
2714 : :
2715 : : /* Get the section header string table index. */
2716 : 65 : size_t shstrndx;
2717 [ - + ]: 65 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
2718 : 0 : error (EXIT_FAILURE, 0,
2719 : 0 : _("cannot get section header string table index"));
2720 : :
2721 : 65 : GElf_Shdr glink_mem;
2722 : 65 : GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
2723 : : &glink_mem);
2724 [ - + ]: 65 : if (glink == NULL)
2725 : 0 : error (EXIT_FAILURE, 0, _("invalid sh_link value in section %zu"),
2726 : : elf_ndxscn (scn));
2727 : :
2728 [ + + ]: 195 : printf (ngettext ("\
2729 : : \nVersion needs section [%2u] '%s' contains %d entry:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'\n",
2730 : : "\
2731 : : \nVersion needs section [%2u] '%s' contains %d entries:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'\n",
2732 : : shdr->sh_info),
2733 : 65 : (unsigned int) elf_ndxscn (scn),
2734 : 65 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name), shdr->sh_info,
2735 : : class == ELFCLASS32 ? 10 : 18, shdr->sh_addr,
2736 : : shdr->sh_offset,
2737 : 65 : (unsigned int) shdr->sh_link,
2738 : 65 : elf_strptr (ebl->elf, shstrndx, glink->sh_name));
2739 : :
2740 : 65 : unsigned int offset = 0;
2741 [ + - ]: 98 : for (int cnt = shdr->sh_info; --cnt >= 0; )
2742 : : {
2743 : : /* Get the data at the next offset. */
2744 : 98 : GElf_Verneed needmem;
2745 : 98 : GElf_Verneed *need = gelf_getverneed (data, offset, &needmem);
2746 [ + - ]: 98 : if (unlikely (need == NULL))
2747 : : break;
2748 : :
2749 : 294 : printf (_(" %#06x: Version: %hu File: %s Cnt: %hu\n"),
2750 : 98 : offset, (unsigned short int) need->vn_version,
2751 : 98 : elf_strptr (ebl->elf, shdr->sh_link, need->vn_file),
2752 : 98 : (unsigned short int) need->vn_cnt);
2753 : :
2754 : 98 : unsigned int auxoffset = offset + need->vn_aux;
2755 [ + - ]: 99 : for (int cnt2 = need->vn_cnt; --cnt2 >= 0; )
2756 : : {
2757 : 99 : GElf_Vernaux auxmem;
2758 : 99 : GElf_Vernaux *aux = gelf_getvernaux (data, auxoffset, &auxmem);
2759 [ + - ]: 99 : if (unlikely (aux == NULL))
2760 : : break;
2761 : :
2762 : 396 : printf (_(" %#06x: Name: %s Flags: %s Version: %hu\n"),
2763 : : auxoffset,
2764 : 99 : elf_strptr (ebl->elf, shdr->sh_link, aux->vna_name),
2765 : 99 : get_ver_flags (aux->vna_flags),
2766 : 99 : (unsigned short int) aux->vna_other);
2767 : :
2768 [ + + ]: 99 : if (aux->vna_next == 0)
2769 : : break;
2770 : :
2771 : 1 : auxoffset += aux->vna_next;
2772 : : }
2773 : :
2774 : : /* Find the next offset. */
2775 [ + + ]: 98 : if (need->vn_next == 0)
2776 : : break;
2777 : :
2778 : 33 : offset += need->vn_next;
2779 : : }
2780 : : }
2781 : :
2782 : :
2783 : : static void
2784 : 2 : handle_verdef (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr)
2785 : : {
2786 : : /* Get the data of the section. */
2787 : 2 : Elf_Data *data = elf_getdata (scn, NULL);
2788 [ - + ]: 2 : if (data == NULL)
2789 : 0 : return;
2790 : :
2791 : : /* Get the section header string table index. */
2792 : 2 : size_t shstrndx;
2793 [ - + ]: 2 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
2794 : 0 : error (EXIT_FAILURE, 0,
2795 : 0 : _("cannot get section header string table index"));
2796 : :
2797 : 2 : GElf_Shdr glink_mem;
2798 : 2 : GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
2799 : : &glink_mem);
2800 [ - + ]: 2 : if (glink == NULL)
2801 : 0 : error (EXIT_FAILURE, 0, _("invalid sh_link value in section %zu"),
2802 : : elf_ndxscn (scn));
2803 : :
2804 : 2 : int class = gelf_getclass (ebl->elf);
2805 [ + - ]: 6 : printf (ngettext ("\
2806 : : \nVersion definition section [%2u] '%s' contains %d entry:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'\n",
2807 : : "\
2808 : : \nVersion definition section [%2u] '%s' contains %d entries:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'\n",
2809 : : shdr->sh_info),
2810 : 2 : (unsigned int) elf_ndxscn (scn),
2811 : 2 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
2812 : : shdr->sh_info,
2813 : : class == ELFCLASS32 ? 10 : 18, shdr->sh_addr,
2814 : : shdr->sh_offset,
2815 : 2 : (unsigned int) shdr->sh_link,
2816 : 2 : elf_strptr (ebl->elf, shstrndx, glink->sh_name));
2817 : :
2818 : 2 : unsigned int offset = 0;
2819 [ + - ]: 12 : for (int cnt = shdr->sh_info; --cnt >= 0; )
2820 : : {
2821 : : /* Get the data at the next offset. */
2822 : 12 : GElf_Verdef defmem;
2823 : 12 : GElf_Verdef *def = gelf_getverdef (data, offset, &defmem);
2824 [ + - ]: 12 : if (unlikely (def == NULL))
2825 : : break;
2826 : :
2827 : 12 : unsigned int auxoffset = offset + def->vd_aux;
2828 : 12 : GElf_Verdaux auxmem;
2829 : 12 : GElf_Verdaux *aux = gelf_getverdaux (data, auxoffset, &auxmem);
2830 [ + - ]: 12 : if (unlikely (aux == NULL))
2831 : : break;
2832 : :
2833 : 12 : printf (_("\
2834 : : %#06x: Version: %hd Flags: %s Index: %hd Cnt: %hd Name: %s\n"),
2835 : 12 : offset, def->vd_version,
2836 : 12 : get_ver_flags (def->vd_flags),
2837 : 12 : def->vd_ndx,
2838 : 12 : def->vd_cnt,
2839 : 12 : elf_strptr (ebl->elf, shdr->sh_link, aux->vda_name));
2840 : :
2841 : 12 : auxoffset += aux->vda_next;
2842 [ + + ]: 12 : for (int cnt2 = 1; cnt2 < def->vd_cnt; ++cnt2)
2843 : : {
2844 : 8 : aux = gelf_getverdaux (data, auxoffset, &auxmem);
2845 [ + - ]: 8 : if (unlikely (aux == NULL))
2846 : : break;
2847 : :
2848 : 8 : printf (_(" %#06x: Parent %d: %s\n"),
2849 : : auxoffset, cnt2,
2850 : 8 : elf_strptr (ebl->elf, shdr->sh_link, aux->vda_name));
2851 : :
2852 [ - + ]: 8 : if (aux->vda_next == 0)
2853 : : break;
2854 : :
2855 : 0 : auxoffset += aux->vda_next;
2856 : : }
2857 : :
2858 : : /* Find the next offset. */
2859 [ + + ]: 12 : if (def->vd_next == 0)
2860 : : break;
2861 : 10 : offset += def->vd_next;
2862 : : }
2863 : : }
2864 : :
2865 : :
2866 : : static void
2867 : 65 : handle_versym (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr)
2868 : : {
2869 : 65 : int class = gelf_getclass (ebl->elf);
2870 : 65 : const char **vername;
2871 : 65 : const char **filename;
2872 : :
2873 : : /* Get the data of the section. */
2874 : 65 : Elf_Data *data = elf_getdata (scn, NULL);
2875 [ + - ]: 65 : if (data == NULL)
2876 : 0 : return;
2877 : :
2878 : : /* Get the section header string table index. */
2879 : 65 : size_t shstrndx;
2880 [ - + ]: 65 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
2881 : 0 : error (EXIT_FAILURE, 0,
2882 : 0 : _("cannot get section header string table index"));
2883 : :
2884 : : /* We have to find the version definition section and extract the
2885 : : version names. */
2886 : : Elf_Scn *defscn = NULL;
2887 : : Elf_Scn *needscn = NULL;
2888 : :
2889 : : Elf_Scn *verscn = NULL;
2890 [ + + ]: 1978 : while ((verscn = elf_nextscn (ebl->elf, verscn)) != NULL)
2891 : : {
2892 : 1913 : GElf_Shdr vershdr_mem;
2893 : 1913 : GElf_Shdr *vershdr = gelf_getshdr (verscn, &vershdr_mem);
2894 : :
2895 [ + - ]: 1913 : if (likely (vershdr != NULL))
2896 : : {
2897 [ + + ]: 1913 : if (vershdr->sh_type == SHT_GNU_verdef)
2898 : : defscn = verscn;
2899 [ + + ]: 1911 : else if (vershdr->sh_type == SHT_GNU_verneed)
2900 : 65 : needscn = verscn;
2901 : : }
2902 : : }
2903 : :
2904 : 65 : size_t nvername;
2905 [ + - ]: 65 : if (defscn != NULL || needscn != NULL)
2906 : : {
2907 : : /* We have a version information (better should have). Now get
2908 : : the version names. First find the maximum version number. */
2909 : 65 : nvername = 0;
2910 [ + + ]: 65 : if (defscn != NULL)
2911 : : {
2912 : : /* Run through the version definitions and find the highest
2913 : : index. */
2914 : 2 : unsigned int offset = 0;
2915 : 2 : Elf_Data *defdata;
2916 : 2 : GElf_Shdr defshdrmem;
2917 : 2 : GElf_Shdr *defshdr;
2918 : :
2919 : 2 : defdata = elf_getdata (defscn, NULL);
2920 [ + - ]: 2 : if (unlikely (defdata == NULL))
2921 : 0 : return;
2922 : :
2923 : 2 : defshdr = gelf_getshdr (defscn, &defshdrmem);
2924 [ + - ]: 2 : if (unlikely (defshdr == NULL))
2925 : : return;
2926 : :
2927 [ + - ]: 12 : for (unsigned int cnt = 0; cnt < defshdr->sh_info; ++cnt)
2928 : : {
2929 : 12 : GElf_Verdef defmem;
2930 : 12 : GElf_Verdef *def;
2931 : :
2932 : : /* Get the data at the next offset. */
2933 : 12 : def = gelf_getverdef (defdata, offset, &defmem);
2934 [ + - ]: 12 : if (unlikely (def == NULL))
2935 : : break;
2936 : :
2937 : 12 : nvername = MAX (nvername, (size_t) (def->vd_ndx & 0x7fff));
2938 : :
2939 [ + + ]: 12 : if (def->vd_next == 0)
2940 : : break;
2941 : 10 : offset += def->vd_next;
2942 : : }
2943 : : }
2944 [ + - ]: 65 : if (needscn != NULL)
2945 : : {
2946 : 65 : unsigned int offset = 0;
2947 : 65 : Elf_Data *needdata;
2948 : 65 : GElf_Shdr needshdrmem;
2949 : 65 : GElf_Shdr *needshdr;
2950 : :
2951 : 65 : needdata = elf_getdata (needscn, NULL);
2952 [ + - ]: 65 : if (unlikely (needdata == NULL))
2953 : 0 : return;
2954 : :
2955 : 65 : needshdr = gelf_getshdr (needscn, &needshdrmem);
2956 [ + - ]: 65 : if (unlikely (needshdr == NULL))
2957 : : return;
2958 : :
2959 [ + - ]: 98 : for (unsigned int cnt = 0; cnt < needshdr->sh_info; ++cnt)
2960 : : {
2961 : 98 : GElf_Verneed needmem;
2962 : 98 : GElf_Verneed *need;
2963 : 98 : unsigned int auxoffset;
2964 : 98 : int cnt2;
2965 : :
2966 : : /* Get the data at the next offset. */
2967 : 98 : need = gelf_getverneed (needdata, offset, &needmem);
2968 [ + - ]: 98 : if (unlikely (need == NULL))
2969 : : break;
2970 : :
2971 : : /* Run through the auxiliary entries. */
2972 : 98 : auxoffset = offset + need->vn_aux;
2973 [ + - ]: 99 : for (cnt2 = need->vn_cnt; --cnt2 >= 0; )
2974 : : {
2975 : 99 : GElf_Vernaux auxmem;
2976 : 99 : GElf_Vernaux *aux;
2977 : :
2978 : 99 : aux = gelf_getvernaux (needdata, auxoffset, &auxmem);
2979 [ + - ]: 99 : if (unlikely (aux == NULL))
2980 : : break;
2981 : :
2982 : 99 : nvername = MAX (nvername,
2983 : : (size_t) (aux->vna_other & 0x7fff));
2984 : :
2985 [ + + ]: 99 : if (aux->vna_next == 0)
2986 : : break;
2987 : 1 : auxoffset += aux->vna_next;
2988 : : }
2989 : :
2990 [ + + ]: 98 : if (need->vn_next == 0)
2991 : : break;
2992 : 33 : offset += need->vn_next;
2993 : : }
2994 : : }
2995 : :
2996 : : /* This is the number of versions we know about. */
2997 : 65 : ++nvername;
2998 : :
2999 : : /* Allocate the array. */
3000 : 65 : vername = (const char **) alloca (nvername * sizeof (const char *));
3001 [ + + ]: 65 : memset(vername, 0, nvername * sizeof (const char *));
3002 : 65 : filename = (const char **) alloca (nvername * sizeof (const char *));
3003 : 65 : memset(filename, 0, nvername * sizeof (const char *));
3004 : :
3005 : : /* Run through the data structures again and collect the strings. */
3006 [ + + ]: 65 : if (defscn != NULL)
3007 : : {
3008 : : /* Run through the version definitions and find the highest
3009 : : index. */
3010 : 2 : unsigned int offset = 0;
3011 : 2 : Elf_Data *defdata;
3012 : 2 : GElf_Shdr defshdrmem;
3013 : 2 : GElf_Shdr *defshdr;
3014 : :
3015 : 2 : defdata = elf_getdata (defscn, NULL);
3016 [ + - ]: 2 : if (unlikely (defdata == NULL))
3017 : 0 : return;
3018 : :
3019 : 2 : defshdr = gelf_getshdr (defscn, &defshdrmem);
3020 [ + - ]: 2 : if (unlikely (defshdr == NULL))
3021 : : return;
3022 : :
3023 [ + - ]: 12 : for (unsigned int cnt = 0; cnt < defshdr->sh_info; ++cnt)
3024 : : {
3025 : :
3026 : : /* Get the data at the next offset. */
3027 : 12 : GElf_Verdef defmem;
3028 : 12 : GElf_Verdef *def = gelf_getverdef (defdata, offset, &defmem);
3029 [ + - ]: 12 : if (unlikely (def == NULL))
3030 : : break;
3031 : :
3032 : 12 : GElf_Verdaux auxmem;
3033 : 24 : GElf_Verdaux *aux = gelf_getverdaux (defdata,
3034 : 12 : offset + def->vd_aux,
3035 : : &auxmem);
3036 [ + - ]: 12 : if (unlikely (aux == NULL))
3037 : : break;
3038 : :
3039 : 12 : vername[def->vd_ndx & 0x7fff]
3040 : 12 : = elf_strptr (ebl->elf, defshdr->sh_link, aux->vda_name);
3041 : 12 : filename[def->vd_ndx & 0x7fff] = NULL;
3042 : :
3043 [ + + ]: 12 : if (def->vd_next == 0)
3044 : : break;
3045 : 10 : offset += def->vd_next;
3046 : : }
3047 : : }
3048 [ + - ]: 65 : if (needscn != NULL)
3049 : : {
3050 : 65 : unsigned int offset = 0;
3051 : :
3052 : 65 : Elf_Data *needdata = elf_getdata (needscn, NULL);
3053 : 65 : GElf_Shdr needshdrmem;
3054 : 65 : GElf_Shdr *needshdr = gelf_getshdr (needscn, &needshdrmem);
3055 [ - + ]: 65 : if (unlikely (needdata == NULL || needshdr == NULL))
3056 : 0 : return;
3057 : :
3058 [ + - ]: 98 : for (unsigned int cnt = 0; cnt < needshdr->sh_info; ++cnt)
3059 : : {
3060 : : /* Get the data at the next offset. */
3061 : 98 : GElf_Verneed needmem;
3062 : 98 : GElf_Verneed *need = gelf_getverneed (needdata, offset,
3063 : : &needmem);
3064 [ + - ]: 98 : if (unlikely (need == NULL))
3065 : : break;
3066 : :
3067 : : /* Run through the auxiliary entries. */
3068 : 98 : unsigned int auxoffset = offset + need->vn_aux;
3069 [ + - ]: 99 : for (int cnt2 = need->vn_cnt; --cnt2 >= 0; )
3070 : : {
3071 : 99 : GElf_Vernaux auxmem;
3072 : 99 : GElf_Vernaux *aux = gelf_getvernaux (needdata, auxoffset,
3073 : : &auxmem);
3074 [ + - ]: 99 : if (unlikely (aux == NULL))
3075 : : break;
3076 : :
3077 : 99 : vername[aux->vna_other & 0x7fff]
3078 : 99 : = elf_strptr (ebl->elf, needshdr->sh_link, aux->vna_name);
3079 : 99 : filename[aux->vna_other & 0x7fff]
3080 : 99 : = elf_strptr (ebl->elf, needshdr->sh_link, need->vn_file);
3081 : :
3082 [ + + ]: 99 : if (aux->vna_next == 0)
3083 : : break;
3084 : 1 : auxoffset += aux->vna_next;
3085 : : }
3086 : :
3087 [ + + ]: 98 : if (need->vn_next == 0)
3088 : : break;
3089 : 33 : offset += need->vn_next;
3090 : : }
3091 : : }
3092 : : }
3093 : : else
3094 : : {
3095 : : vername = NULL;
3096 : : nvername = 1;
3097 : : filename = NULL;
3098 : : }
3099 : :
3100 : 65 : GElf_Shdr glink_mem;
3101 : 65 : GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
3102 : : &glink_mem);
3103 : 65 : size_t sh_entsize = gelf_fsize (ebl->elf, ELF_T_HALF, 1, EV_CURRENT);
3104 [ - + ]: 65 : if (glink == NULL)
3105 : 0 : error (EXIT_FAILURE, 0, _("invalid sh_link value in section %zu"),
3106 : : elf_ndxscn (scn));
3107 : :
3108 : : /* Print the header. */
3109 : 130 : printf (ngettext ("\
3110 : : \nVersion symbols section [%2u] '%s' contains %d entry:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'",
3111 : : "\
3112 : : \nVersion symbols section [%2u] '%s' contains %d entries:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'",
3113 : : shdr->sh_size / sh_entsize),
3114 : 65 : (unsigned int) elf_ndxscn (scn),
3115 : 65 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
3116 [ + + ]: 65 : (int) (shdr->sh_size / sh_entsize),
3117 : : class == ELFCLASS32 ? 10 : 18, shdr->sh_addr,
3118 : : shdr->sh_offset,
3119 : 65 : (unsigned int) shdr->sh_link,
3120 : 65 : elf_strptr (ebl->elf, shstrndx, glink->sh_name));
3121 : :
3122 : : /* Now we can finally look at the actual contents of this section. */
3123 [ + + ]: 1837 : for (unsigned int cnt = 0; cnt < shdr->sh_size / sh_entsize; ++cnt)
3124 : : {
3125 [ + + ]: 1772 : if (cnt % 2 == 0)
3126 : 902 : printf ("\n %4d:", cnt);
3127 : :
3128 : 1772 : GElf_Versym symmem;
3129 : 1772 : GElf_Versym *sym = gelf_getversym (data, cnt, &symmem);
3130 [ + - ]: 1772 : if (sym == NULL)
3131 : : break;
3132 : :
3133 [ + + + ]: 1772 : switch (*sym)
3134 : : {
3135 : 383 : ssize_t n;
3136 : 383 : case 0:
3137 : 383 : fputs_unlocked (_(" 0 *local* "),
3138 : : stdout);
3139 : 383 : break;
3140 : :
3141 : 20 : case 1:
3142 : 20 : fputs_unlocked (_(" 1 *global* "),
3143 : : stdout);
3144 : 20 : break;
3145 : :
3146 : 1369 : default:
3147 [ + - ]: 1369 : n = printf ("%4d%c%s",
3148 [ + - ]: 1369 : *sym & 0x7fff, *sym & 0x8000 ? 'h' : ' ',
3149 : : (vername != NULL
3150 [ + - ]: 1369 : && (unsigned int) (*sym & 0x7fff) < nvername)
3151 : 1369 : ? vername[*sym & 0x7fff] : "???");
3152 [ + - ]: 1369 : if ((unsigned int) (*sym & 0x7fff) < nvername
3153 [ + - + + ]: 1369 : && filename != NULL && filename[*sym & 0x7fff] != NULL)
3154 : 1217 : n += printf ("(%s)", filename[*sym & 0x7fff]);
3155 : 1772 : printf ("%*s", MAX (0, 33 - (int) n), " ");
3156 : : break;
3157 : : }
3158 : : }
3159 [ - + ]: 130 : putchar_unlocked ('\n');
3160 : : }
3161 : :
3162 : :
3163 : : static void
3164 : 65 : print_hash_info (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr, size_t shstrndx,
3165 : : uint_fast32_t maxlength, Elf32_Word nbucket,
3166 : : uint_fast32_t nsyms, uint32_t *lengths, const char *extrastr)
3167 : : {
3168 : 65 : uint32_t *counts = xcalloc (maxlength + 1, sizeof (uint32_t));
3169 : :
3170 [ + + ]: 256 : for (Elf32_Word cnt = 0; cnt < nbucket; ++cnt)
3171 : 191 : ++counts[lengths[cnt]];
3172 : :
3173 : 65 : GElf_Shdr glink_mem;
3174 : 65 : GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf,
3175 : 65 : shdr->sh_link),
3176 : : &glink_mem);
3177 [ - + ]: 65 : if (glink == NULL)
3178 : : {
3179 : 0 : error (0, 0, _("invalid sh_link value in section %zu"),
3180 : : elf_ndxscn (scn));
3181 : 0 : return;
3182 : : }
3183 : :
3184 [ + + ]: 195 : printf (ngettext ("\
3185 : : \nHistogram for bucket list length in section [%2u] '%s' (total of %d bucket):\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'\n",
3186 : : "\
3187 : : \nHistogram for bucket list length in section [%2u] '%s' (total of %d buckets):\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'\n",
3188 : : nbucket),
3189 : 65 : (unsigned int) elf_ndxscn (scn),
3190 : 65 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
3191 : : (int) nbucket,
3192 : 65 : gelf_getclass (ebl->elf) == ELFCLASS32 ? 10 : 18,
3193 : : shdr->sh_addr,
3194 : : shdr->sh_offset,
3195 : 65 : (unsigned int) shdr->sh_link,
3196 : 65 : elf_strptr (ebl->elf, shstrndx, glink->sh_name));
3197 : :
3198 [ + - ]: 65 : if (extrastr != NULL)
3199 : 65 : fputs (extrastr, stdout);
3200 : :
3201 [ + - ]: 65 : if (likely (nbucket > 0))
3202 : : {
3203 : 65 : uint64_t success = 0;
3204 : :
3205 : : /* xgettext:no-c-format */
3206 : 65 : fputs_unlocked (_("\
3207 : : Length Number % of total Coverage\n"), stdout);
3208 : 65 : printf (_(" 0 %6" PRIu32 " %5.1f%%\n"),
3209 : 65 : counts[0], (counts[0] * 100.0) / nbucket);
3210 : :
3211 : 65 : uint64_t nzero_counts = 0;
3212 [ + + ]: 89 : for (Elf32_Word cnt = 1; cnt <= maxlength; ++cnt)
3213 : : {
3214 : 24 : nzero_counts += counts[cnt] * cnt;
3215 : 24 : printf (_("\
3216 : : %7d %6" PRIu32 " %5.1f%% %5.1f%%\n"),
3217 : 24 : (int) cnt, counts[cnt], (counts[cnt] * 100.0) / nbucket,
3218 : 24 : (nzero_counts * 100.0) / nsyms);
3219 : : }
3220 : :
3221 : : Elf32_Word acc = 0;
3222 [ + + ]: 89 : for (Elf32_Word cnt = 1; cnt <= maxlength; ++cnt)
3223 : : {
3224 : 24 : acc += cnt;
3225 : 24 : success += counts[cnt] * acc;
3226 : : }
3227 : :
3228 : 130 : printf (_("\
3229 : : Average number of tests: successful lookup: %f\n\
3230 : : unsuccessful lookup: %f\n"),
3231 : 65 : (double) success / (double) nzero_counts,
3232 : : (double) nzero_counts / (double) nbucket);
3233 : : }
3234 : :
3235 : 65 : free (counts);
3236 : : }
3237 : :
3238 : :
3239 : : /* This function handles the traditional System V-style hash table format. */
3240 : : static void
3241 : 0 : handle_sysv_hash (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr, size_t shstrndx)
3242 : : {
3243 : 0 : Elf_Data *data = elf_getdata (scn, NULL);
3244 [ # # ]: 0 : if (unlikely (data == NULL))
3245 : : {
3246 : 0 : error (0, 0, _("cannot get data for section %d: %s"),
3247 : 0 : (int) elf_ndxscn (scn), elf_errmsg (-1));
3248 : 0 : return;
3249 : : }
3250 : :
3251 [ # # ]: 0 : if (unlikely (data->d_size < 2 * sizeof (Elf32_Word)))
3252 : : {
3253 : 0 : invalid_data:
3254 : 0 : error (0, 0, _("invalid data in sysv.hash section %d"),
3255 : 0 : (int) elf_ndxscn (scn));
3256 : 0 : return;
3257 : : }
3258 : :
3259 : 0 : Elf32_Word nbucket = ((Elf32_Word *) data->d_buf)[0];
3260 : 0 : Elf32_Word nchain = ((Elf32_Word *) data->d_buf)[1];
3261 : :
3262 : 0 : uint64_t used_buf = (2ULL + nchain + nbucket) * sizeof (Elf32_Word);
3263 [ # # ]: 0 : if (used_buf > data->d_size)
3264 : 0 : goto invalid_data;
3265 : :
3266 : 0 : Elf32_Word *bucket = &((Elf32_Word *) data->d_buf)[2];
3267 : 0 : Elf32_Word *chain = &((Elf32_Word *) data->d_buf)[2 + nbucket];
3268 : :
3269 : 0 : uint32_t *lengths = xcalloc (nbucket, sizeof (uint32_t));
3270 : :
3271 : 0 : uint_fast32_t maxlength = 0;
3272 : 0 : uint_fast32_t nsyms = 0;
3273 [ # # ]: 0 : for (Elf32_Word cnt = 0; cnt < nbucket; ++cnt)
3274 : : {
3275 : 0 : Elf32_Word inner = bucket[cnt];
3276 : 0 : Elf32_Word chain_len = 0;
3277 [ # # ]: 0 : while (inner > 0 && inner < nchain)
3278 : : {
3279 : 0 : ++nsyms;
3280 : 0 : ++chain_len;
3281 [ # # ]: 0 : if (chain_len > nchain)
3282 : : {
3283 : 0 : error (0, 0, _("invalid chain in sysv.hash section %d"),
3284 : 0 : (int) elf_ndxscn (scn));
3285 : 0 : free (lengths);
3286 : 0 : return;
3287 : : }
3288 [ # # ]: 0 : if (maxlength < ++lengths[cnt])
3289 : 0 : ++maxlength;
3290 : :
3291 : 0 : inner = chain[inner];
3292 : : }
3293 : : }
3294 : :
3295 : 0 : print_hash_info (ebl, scn, shdr, shstrndx, maxlength, nbucket, nsyms,
3296 : : lengths, NULL);
3297 : :
3298 : 0 : free (lengths);
3299 : : }
3300 : :
3301 : :
3302 : : /* This function handles the incorrect, System V-style hash table
3303 : : format some 64-bit architectures use. */
3304 : : static void
3305 : 0 : handle_sysv_hash64 (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr, size_t shstrndx)
3306 : : {
3307 : 0 : Elf_Data *data = elf_getdata (scn, NULL);
3308 [ # # ]: 0 : if (unlikely (data == NULL))
3309 : : {
3310 : 0 : error (0, 0, _("cannot get data for section %d: %s"),
3311 : 0 : (int) elf_ndxscn (scn), elf_errmsg (-1));
3312 : 0 : return;
3313 : : }
3314 : :
3315 [ # # ]: 0 : if (unlikely (data->d_size < 2 * sizeof (Elf64_Xword)))
3316 : : {
3317 : 0 : invalid_data:
3318 : 0 : error (0, 0, _("invalid data in sysv.hash64 section %d"),
3319 : 0 : (int) elf_ndxscn (scn));
3320 : 0 : return;
3321 : : }
3322 : :
3323 : 0 : Elf64_Xword nbucket = ((Elf64_Xword *) data->d_buf)[0];
3324 : 0 : Elf64_Xword nchain = ((Elf64_Xword *) data->d_buf)[1];
3325 : :
3326 : 0 : uint64_t maxwords = data->d_size / sizeof (Elf64_Xword);
3327 [ # # ]: 0 : if (maxwords < 2
3328 [ # # ]: 0 : || maxwords - 2 < nbucket
3329 [ # # ]: 0 : || maxwords - 2 - nbucket < nchain)
3330 : 0 : goto invalid_data;
3331 : :
3332 : 0 : Elf64_Xword *bucket = &((Elf64_Xword *) data->d_buf)[2];
3333 : 0 : Elf64_Xword *chain = &((Elf64_Xword *) data->d_buf)[2 + nbucket];
3334 : :
3335 : 0 : uint32_t *lengths = xcalloc (nbucket, sizeof (uint32_t));
3336 : :
3337 : 0 : uint_fast32_t maxlength = 0;
3338 : 0 : uint_fast32_t nsyms = 0;
3339 [ # # ]: 0 : for (Elf64_Xword cnt = 0; cnt < nbucket; ++cnt)
3340 : : {
3341 : 0 : Elf64_Xword inner = bucket[cnt];
3342 : 0 : Elf64_Xword chain_len = 0;
3343 [ # # ]: 0 : while (inner > 0 && inner < nchain)
3344 : : {
3345 : 0 : ++nsyms;
3346 : 0 : ++chain_len;
3347 [ # # ]: 0 : if (chain_len > nchain)
3348 : : {
3349 : 0 : error (0, 0, _("invalid chain in sysv.hash64 section %d"),
3350 : 0 : (int) elf_ndxscn (scn));
3351 : 0 : free (lengths);
3352 : 0 : return;
3353 : : }
3354 [ # # ]: 0 : if (maxlength < ++lengths[cnt])
3355 : 0 : ++maxlength;
3356 : :
3357 : 0 : inner = chain[inner];
3358 : : }
3359 : : }
3360 : :
3361 : 0 : print_hash_info (ebl, scn, shdr, shstrndx, maxlength, nbucket, nsyms,
3362 : : lengths, NULL);
3363 : :
3364 : 0 : free (lengths);
3365 : : }
3366 : :
3367 : :
3368 : : /* This function handles the GNU-style hash table format. */
3369 : : static void
3370 : 65 : handle_gnu_hash (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr, size_t shstrndx)
3371 : : {
3372 : 65 : uint32_t *lengths = NULL;
3373 : 65 : Elf_Data *data = elf_getdata (scn, NULL);
3374 [ - + ]: 65 : if (unlikely (data == NULL))
3375 : : {
3376 : 0 : error (0, 0, _("cannot get data for section %d: %s"),
3377 : 0 : (int) elf_ndxscn (scn), elf_errmsg (-1));
3378 : 0 : return;
3379 : : }
3380 : :
3381 [ - + ]: 65 : if (unlikely (data->d_size < 4 * sizeof (Elf32_Word)))
3382 : : {
3383 : 0 : invalid_data:
3384 : 0 : free (lengths);
3385 : 0 : error (0, 0, _("invalid data in gnu.hash section %d"),
3386 : 0 : (int) elf_ndxscn (scn));
3387 : 0 : return;
3388 : : }
3389 : :
3390 : 65 : Elf32_Word nbucket = ((Elf32_Word *) data->d_buf)[0];
3391 : 65 : Elf32_Word symbias = ((Elf32_Word *) data->d_buf)[1];
3392 : :
3393 : : /* Next comes the size of the bitmap. It's measured in words for
3394 : : the architecture. It's 32 bits for 32 bit archs, and 64 bits for
3395 : : 64 bit archs. There is always a bloom filter present, so zero is
3396 : : an invalid value. */
3397 : 65 : Elf32_Word bitmask_words = ((Elf32_Word *) data->d_buf)[2];
3398 [ + + ]: 65 : if (gelf_getclass (ebl->elf) == ELFCLASS64)
3399 : 57 : bitmask_words *= 2;
3400 : :
3401 [ - + ]: 65 : if (bitmask_words == 0)
3402 : 0 : goto invalid_data;
3403 : :
3404 : 65 : Elf32_Word shift = ((Elf32_Word *) data->d_buf)[3];
3405 : :
3406 : : /* Is there still room for the sym chain?
3407 : : Use uint64_t calculation to prevent 32bit overflow. */
3408 : 65 : uint64_t used_buf = (4ULL + bitmask_words + nbucket) * sizeof (Elf32_Word);
3409 : 65 : uint32_t max_nsyms = (data->d_size - used_buf) / sizeof (Elf32_Word);
3410 [ - + ]: 65 : if (used_buf > data->d_size)
3411 : 0 : goto invalid_data;
3412 : :
3413 : 65 : lengths = xcalloc (nbucket, sizeof (uint32_t));
3414 : :
3415 : 65 : Elf32_Word *bitmask = &((Elf32_Word *) data->d_buf)[4];
3416 : 65 : Elf32_Word *bucket = &((Elf32_Word *) data->d_buf)[4 + bitmask_words];
3417 : 65 : Elf32_Word *chain = &((Elf32_Word *) data->d_buf)[4 + bitmask_words
3418 : 65 : + nbucket];
3419 : :
3420 : : /* Compute distribution of chain lengths. */
3421 : 65 : uint_fast32_t maxlength = 0;
3422 : 65 : uint_fast32_t nsyms = 0;
3423 [ + + ]: 256 : for (Elf32_Word cnt = 0; cnt < nbucket; ++cnt)
3424 [ + + ]: 191 : if (bucket[cnt] != 0)
3425 : : {
3426 : 100 : Elf32_Word inner = bucket[cnt] - symbias;
3427 : 172 : do
3428 : : {
3429 : 172 : ++nsyms;
3430 [ + + ]: 172 : if (maxlength < ++lengths[cnt])
3431 : 24 : ++maxlength;
3432 [ - + ]: 172 : if (inner >= max_nsyms)
3433 : 0 : goto invalid_data;
3434 : : }
3435 [ + + ]: 172 : while ((chain[inner++] & 1) == 0);
3436 : : }
3437 : :
3438 : : /* Count bits in bitmask. */
3439 : : uint_fast32_t nbits = 0;
3440 [ + + ]: 223 : for (Elf32_Word cnt = 0; cnt < bitmask_words; ++cnt)
3441 : : {
3442 : 158 : uint_fast32_t word = bitmask[cnt];
3443 : :
3444 : 158 : word = (word & 0x55555555) + ((word >> 1) & 0x55555555);
3445 : 158 : word = (word & 0x33333333) + ((word >> 2) & 0x33333333);
3446 : 158 : word = (word & 0x0f0f0f0f) + ((word >> 4) & 0x0f0f0f0f);
3447 : 158 : word = (word & 0x00ff00ff) + ((word >> 8) & 0x00ff00ff);
3448 : 158 : nbits += (word & 0x0000ffff) + ((word >> 16) & 0x0000ffff);
3449 : : }
3450 : :
3451 : 130 : char *str = xasprintf (_("\
3452 : : Symbol Bias: %u\n\
3453 : : Bitmask Size: %zu bytes %" PRIuFAST32 "%% bits set 2nd hash shift: %u\n"),
3454 : : (unsigned int) symbias,
3455 : : bitmask_words * sizeof (Elf32_Word),
3456 : 65 : ((nbits * 100 + 50)
3457 : 65 : / (uint_fast32_t) (bitmask_words
3458 : : * sizeof (Elf32_Word) * 8)),
3459 : : (unsigned int) shift);
3460 : :
3461 : 65 : print_hash_info (ebl, scn, shdr, shstrndx, maxlength, nbucket, nsyms,
3462 : : lengths, str);
3463 : :
3464 : 65 : free (str);
3465 : 65 : free (lengths);
3466 : : }
3467 : :
3468 : :
3469 : : /* Find the symbol table(s). For this we have to search through the
3470 : : section table. */
3471 : : static void
3472 : 120 : handle_hash (Ebl *ebl)
3473 : : {
3474 : : /* Get the section header string table index. */
3475 : 120 : size_t shstrndx;
3476 [ - + ]: 120 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
3477 : 0 : error (EXIT_FAILURE, 0,
3478 : 0 : _("cannot get section header string table index"));
3479 : :
3480 : : Elf_Scn *scn = NULL;
3481 [ + + ]: 3485 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
3482 : : {
3483 : : /* Handle the section if it is a symbol table. */
3484 : 3365 : GElf_Shdr shdr_mem;
3485 : 3365 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
3486 : :
3487 [ + - ]: 3365 : if (likely (shdr != NULL))
3488 : : {
3489 [ + + ]: 3365 : if ((shdr->sh_type == SHT_HASH || shdr->sh_type == SHT_GNU_HASH)
3490 [ - + ]: 65 : && (shdr->sh_flags & SHF_COMPRESSED) != 0)
3491 : : {
3492 [ # # ]: 0 : if (elf_compress (scn, 0, 0) < 0)
3493 : 0 : printf ("WARNING: %s [%zd]\n",
3494 : : _("Couldn't uncompress section"),
3495 : : elf_ndxscn (scn));
3496 : 0 : shdr = gelf_getshdr (scn, &shdr_mem);
3497 [ # # ]: 0 : if (unlikely (shdr == NULL))
3498 : 0 : error (EXIT_FAILURE, 0,
3499 : 0 : _("cannot get section [%zd] header: %s"),
3500 : : elf_ndxscn (scn), elf_errmsg (-1));
3501 : : }
3502 : :
3503 [ - + ]: 3365 : if (shdr->sh_type == SHT_HASH)
3504 : : {
3505 [ # # ]: 0 : if (ebl_sysvhash_entrysize (ebl) == sizeof (Elf64_Xword))
3506 : 0 : handle_sysv_hash64 (ebl, scn, shdr, shstrndx);
3507 : : else
3508 : 0 : handle_sysv_hash (ebl, scn, shdr, shstrndx);
3509 : : }
3510 [ + + ]: 3365 : else if (shdr->sh_type == SHT_GNU_HASH)
3511 : 65 : handle_gnu_hash (ebl, scn, shdr, shstrndx);
3512 : : }
3513 : : }
3514 : 120 : }
3515 : :
3516 : :
3517 : : static void
3518 : 124 : print_liblist (Ebl *ebl)
3519 : : {
3520 : : /* Find the library list sections. For this we have to search
3521 : : through the section table. */
3522 : 124 : Elf_Scn *scn = NULL;
3523 : :
3524 : : /* Get the section header string table index. */
3525 : 124 : size_t shstrndx;
3526 [ - + ]: 124 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
3527 : 0 : error (EXIT_FAILURE, 0,
3528 : 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_FAILURE, 0,
3598 : 0 : _("cannot get section header string table index"));
3599 : :
3600 [ + + ]: 3548 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
3601 : : {
3602 : 3424 : GElf_Shdr shdr_mem;
3603 : 3424 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
3604 : :
3605 [ + - + + ]: 3424 : if (shdr == NULL || (shdr->sh_type != SHT_GNU_ATTRIBUTES
3606 [ + + ]: 3421 : && (shdr->sh_type != SHT_ARM_ATTRIBUTES
3607 [ - + ]: 1 : || ehdr->e_machine != EM_ARM)
3608 [ + + ]: 3420 : && (shdr->sh_type != SHT_CSKY_ATTRIBUTES
3609 [ + - ]: 1 : || ehdr->e_machine != EM_CSKY)))
3610 : 3420 : continue;
3611 : :
3612 : 8 : printf (_("\
3613 : : \nObject attributes section [%2zu] '%s' of %" PRIu64
3614 : : " bytes at offset %#0" PRIx64 ":\n"),
3615 : : elf_ndxscn (scn),
3616 : 4 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
3617 : : shdr->sh_size, shdr->sh_offset);
3618 : :
3619 : 4 : Elf_Data *data = elf_rawdata (scn, NULL);
3620 [ + - + - ]: 4 : if (unlikely (data == NULL || data->d_size == 0))
3621 : 0 : return;
3622 : :
3623 : 4 : const unsigned char *p = data->d_buf;
3624 : :
3625 : : /* There is only one 'version', A. */
3626 [ + - ]: 4 : if (unlikely (*p++ != 'A'))
3627 : : return;
3628 : :
3629 : 4 : fputs_unlocked (_(" Owner Size\n"), stdout);
3630 : :
3631 : : /* Loop over the sections. */
3632 [ + + ]: 8 : while (left (data, p) >= 4)
3633 : : {
3634 : : /* Section length. */
3635 : 4 : uint32_t len;
3636 [ + + ]: 4 : memcpy (&len, p, sizeof len);
3637 : :
3638 [ + + ]: 4 : if (MY_ELFDATA != ehdr->e_ident[EI_DATA])
3639 : 2 : CONVERT (len);
3640 : :
3641 [ + - ]: 4 : if (unlikely (len > left (data, p)))
3642 : : break;
3643 : :
3644 : : /* Section vendor name. */
3645 : 4 : const unsigned char *name = p + sizeof len;
3646 : 4 : p += len;
3647 : :
3648 : 4 : unsigned const char *q = memchr (name, '\0', len);
3649 [ + - ]: 4 : if (unlikely (q == NULL))
3650 : : break;
3651 : 4 : ++q;
3652 : :
3653 : 4 : printf (_(" %-13s %4" PRIu32 "\n"), name, len);
3654 : :
3655 : 8 : bool gnu_vendor = (q - name == sizeof "gnu"
3656 [ + + - + ]: 4 : && !memcmp (name, "gnu", sizeof "gnu"));
3657 : :
3658 : : /* Loop over subsections. */
3659 [ + + ]: 4 : if (shdr->sh_type != SHT_GNU_ATTRIBUTES
3660 [ + - ]: 3 : || gnu_vendor)
3661 [ + + ]: 8 : while (q < p)
3662 : : {
3663 : 4 : const unsigned char *const sub = q;
3664 : :
3665 : 4 : unsigned int subsection_tag;
3666 : 4 : get_uleb128 (subsection_tag, q, p);
3667 [ + - ]: 4 : if (unlikely (q >= p))
3668 : : break;
3669 : :
3670 : 4 : uint32_t subsection_len;
3671 [ + - ]: 4 : if (unlikely (p - sub < (ptrdiff_t) sizeof subsection_len))
3672 : : break;
3673 : :
3674 [ + + ]: 4 : memcpy (&subsection_len, q, sizeof subsection_len);
3675 : :
3676 [ + + ]: 4 : if (MY_ELFDATA != ehdr->e_ident[EI_DATA])
3677 : 2 : CONVERT (subsection_len);
3678 : :
3679 : : /* Don't overflow, ptrdiff_t might be 32bits, but signed. */
3680 [ + - + - ]: 4 : if (unlikely (subsection_len == 0
3681 : : || subsection_len >= (uint32_t) PTRDIFF_MAX
3682 : : || p - sub < (ptrdiff_t) subsection_len))
3683 : : break;
3684 : :
3685 : 4 : const unsigned char *r = q + sizeof subsection_len;
3686 : 4 : q = sub + subsection_len;
3687 : :
3688 [ - + ]: 4 : switch (subsection_tag)
3689 : : {
3690 : 0 : default:
3691 : : /* Unknown subsection, print and skip. */
3692 : 4 : printf (_(" %-4u %12" PRIu32 "\n"),
3693 : : subsection_tag, subsection_len);
3694 : : break;
3695 : :
3696 : 4 : case 1: /* Tag_File */
3697 : 4 : printf (_(" File: %11" PRIu32 "\n"),
3698 : : subsection_len);
3699 : :
3700 [ + + ]: 26 : while (r < q)
3701 : : {
3702 : 22 : unsigned int tag;
3703 : 22 : get_uleb128 (tag, r, q);
3704 [ + - ]: 22 : if (unlikely (r >= q))
3705 : : break;
3706 : :
3707 : : /* GNU style tags have either a uleb128 value,
3708 : : when lowest bit is not set, or a string
3709 : : when the lowest bit is set.
3710 : : "compatibility" (32) is special. It has
3711 : : both a string and a uleb128 value. For
3712 : : non-gnu we assume 6 till 31 only take ints.
3713 : : XXX see arm backend, do we need a separate
3714 : : hook? */
3715 : 22 : uint64_t value = 0;
3716 : 22 : const char *string = NULL;
3717 [ + - + + ]: 22 : if (tag == 32 || (tag & 1) == 0
3718 [ + - + + ]: 8 : || (! gnu_vendor && (tag > 5 && tag < 32)))
3719 : : {
3720 : 21 : get_uleb128 (value, r, q);
3721 [ + - ]: 21 : if (r > q)
3722 : : break;
3723 : : }
3724 [ + - ]: 22 : if (tag == 32
3725 [ + + ]: 22 : || ((tag & 1) != 0
3726 [ + - ]: 8 : && (gnu_vendor
3727 [ + - ]: 8 : || (! gnu_vendor && tag > 32)))
3728 [ + + + + ]: 22 : || (! gnu_vendor && tag > 3 && tag < 6))
3729 : : {
3730 : 1 : string = (const char *) r;
3731 : 1 : r = memchr (r, '\0', q - r);
3732 [ + - ]: 1 : if (r == NULL)
3733 : : break;
3734 : 1 : ++r;
3735 : : }
3736 : :
3737 : 22 : const char *tag_name = NULL;
3738 : 22 : const char *value_name = NULL;
3739 : 22 : ebl_check_object_attribute (ebl, (const char *) name,
3740 : : tag, value,
3741 : : &tag_name, &value_name);
3742 : :
3743 [ + - ]: 22 : if (tag_name != NULL)
3744 : : {
3745 [ - + ]: 22 : if (tag == 32)
3746 : 0 : printf (_(" %s: %" PRId64 ", %s\n"),
3747 : : tag_name, value, string);
3748 [ + + + + ]: 22 : else if (string == NULL && value_name == NULL)
3749 : 1 : printf (_(" %s: %" PRId64 "\n"),
3750 : : tag_name, value);
3751 : : else
3752 [ + + ]: 21 : printf (_(" %s: %s\n"),
3753 : : tag_name, string ?: value_name);
3754 : : }
3755 : : else
3756 : : {
3757 : : /* For "gnu" vendor 32 "compatibility" has
3758 : : already been handled above. */
3759 [ # # # # ]: 0 : assert (tag != 32
3760 : : || strcmp ((const char *) name, "gnu"));
3761 [ # # ]: 0 : if (string == NULL)
3762 : 0 : printf (_(" %u: %" PRId64 "\n"),
3763 : : tag, value);
3764 : : else
3765 : 22 : printf (_(" %u: %s\n"),
3766 : : tag, string);
3767 : : }
3768 : : }
3769 : : }
3770 : : }
3771 : : }
3772 : : }
3773 : : }
3774 : :
3775 : :
3776 : : void
3777 : 1135177 : print_dwarf_addr (Dwfl_Module *dwflmod,
3778 : : int address_size, Dwarf_Addr address, Dwarf_Addr raw)
3779 : : {
3780 : : /* See if there is a name we can give for this address. */
3781 : 1135177 : GElf_Sym sym;
3782 : 1135177 : GElf_Off off = 0;
3783 [ + + ]: 1135147 : const char *name = (print_address_names && ! print_unresolved_addresses)
3784 : 1135065 : ? dwfl_module_addrinfo (dwflmod, address, &off, &sym, NULL, NULL, NULL)
3785 [ + + ]: 1135177 : : NULL;
3786 : :
3787 : 1135177 : const char *scn;
3788 [ + + ]: 1135177 : if (print_unresolved_addresses)
3789 : : {
3790 : 84 : address = raw;
3791 : 84 : scn = NULL;
3792 : : }
3793 : : else
3794 : : {
3795 : : /* Relativize the address. */
3796 : 1135093 : int n = dwfl_module_relocations (dwflmod);
3797 [ + + ]: 1135093 : int i = n < 1 ? -1 : dwfl_module_relocate_address (dwflmod, &address);
3798 : :
3799 : : /* In an ET_REL file there is a section name to refer to. */
3800 : 1133741 : scn = (i < 0 ? NULL
3801 [ + - ]: 1133741 : : dwfl_module_relocation_info (dwflmod, i, NULL));
3802 : : }
3803 : :
3804 [ - + ]: 1135177 : if ((name != NULL
3805 : 1126172 : ? (off != 0
3806 : : ? (scn != NULL
3807 : : ? (address_size == 0
3808 : 374310 : ? printf ("%s+%#" PRIx64 " <%s+%#" PRIx64 ">",
3809 : : scn, address, name, off)
3810 : 1399700 : : printf ("%s+%#0*" PRIx64 " <%s+%#" PRIx64 ">",
3811 : 699850 : scn, 2 + address_size * 2, address,
3812 : : name, off))
3813 : : : (address_size == 0
3814 : 182 : ? printf ("%#" PRIx64 " <%s+%#" PRIx64 ">",
3815 : : address, name, off)
3816 : 1120 : : printf ("%#0*" PRIx64 " <%s+%#" PRIx64 ">",
3817 : 560 : 2 + address_size * 2, address,
3818 : : name, off)))
3819 : : : (scn != NULL
3820 : : ? (address_size == 0
3821 : 15500 : ? printf ("%s+%#" PRIx64 " <%s>", scn, address, name)
3822 : 70630 : : printf ("%s+%#0*" PRIx64 " <%s>",
3823 : 35315 : scn, 2 + address_size * 2, address, name))
3824 : : : (address_size == 0
3825 : 80 : ? printf ("%#" PRIx64 " <%s>", address, name)
3826 : 750 : : printf ("%#0*" PRIx64 " <%s>",
3827 : 375 : 2 + address_size * 2, address, name))))
3828 : : : (scn != NULL
3829 : : ? (address_size == 0
3830 : 1829 : ? printf ("%s+%#" PRIx64, scn, address)
3831 : 6937 : : printf ("%s+%#0*" PRIx64, scn, 2 + address_size * 2, address))
3832 : : : (address_size == 0
3833 : 35 : ? printf ("%#" PRIx64, address)
3834 [ + + + + : 2270150 : : printf ("%#0*" PRIx64, 2 + address_size * 2, address)))) < 0)
+ + + + +
+ + + + +
+ + + + +
+ + + ]
3835 : 0 : error (EXIT_FAILURE, 0, _("sprintf failure"));
3836 : 1135177 : }
3837 : :
3838 : :
3839 : : static const char *
3840 : 1027233 : dwarf_tag_string (unsigned int tag)
3841 : : {
3842 [ - + + - : 1027233 : switch (tag)
- - - - +
- + + - -
- - - - -
+ - + - +
- - + + -
+ - - - -
- - - + -
+ - + + +
- - - - -
- + - - +
- - - + -
+ + + + -
- - - - +
+ + + - +
+ + + - -
- ]
3843 : : {
3844 : : #define DWARF_ONE_KNOWN_DW_TAG(NAME, CODE) case CODE: return #NAME;
3845 : 1027233 : DWARF_ALL_KNOWN_DW_TAG
3846 : : #undef DWARF_ONE_KNOWN_DW_TAG
3847 : 0 : default:
3848 : 0 : return NULL;
3849 : : }
3850 : : }
3851 : :
3852 : :
3853 : : static const char *
3854 : 4003047 : dwarf_attr_string (unsigned int attrnum)
3855 : : {
3856 [ + - + - : 4003047 : switch (attrnum)
- + - + -
- + + + -
- - - + +
- + - - +
- + - + -
- - - - -
- - - - -
- - - - -
- - - + -
+ - + - +
- - - + +
- - - + -
+ - - + -
- + + + -
- + + - -
- - - + -
+ - + - -
+ - - + +
+ + - - -
- - + + +
+ - + - +
- - - - +
+ - + - +
+ - + + +
+ + - - +
- - - + -
+ - - - -
+ + - + -
- - - + -
- - + + -
+ - - - -
+ - - - -
- - + + -
- - - - -
- + ]
3857 : : {
3858 : : #define DWARF_ONE_KNOWN_DW_AT(NAME, CODE) case CODE: return #NAME;
3859 : 4003046 : DWARF_ALL_KNOWN_DW_AT
3860 : : #undef DWARF_ONE_KNOWN_DW_AT
3861 : 0 : default:
3862 : 0 : return NULL;
3863 : : }
3864 : : }
3865 : :
3866 : :
3867 : : static const char *
3868 : 4003053 : dwarf_form_string (unsigned int form)
3869 : : {
3870 [ + + + + : 4003053 : switch (form)
+ - - - -
- + - - +
- + + + +
+ + + - +
+ - - + -
- + - - +
+ + + + +
- + - - -
- + - + ]
3871 : : {
3872 : : #define DWARF_ONE_KNOWN_DW_FORM(NAME, CODE) case CODE: return #NAME;
3873 : 4003051 : DWARF_ALL_KNOWN_DW_FORM
3874 : : #undef DWARF_ONE_KNOWN_DW_FORM
3875 : 0 : default:
3876 : 0 : return NULL;
3877 : : }
3878 : : }
3879 : :
3880 : :
3881 : : static const char *
3882 : 1702 : dwarf_lang_string (unsigned int lang)
3883 : : {
3884 [ + - - + : 1702 : switch (lang)
+ + + - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - -
- ]
3885 : : {
3886 : : #define DWARF_ONE_KNOWN_DW_LANG(NAME, CODE) case CODE: return #NAME;
3887 : 1702 : DWARF_ALL_KNOWN_DW_LANG
3888 : : #undef DWARF_ONE_KNOWN_DW_LANG
3889 : 0 : default:
3890 : 0 : return NULL;
3891 : : }
3892 : : }
3893 : :
3894 : :
3895 : : static const char *
3896 : 3603 : dwarf_inline_string (unsigned int code)
3897 : : {
3898 : 3603 : static const char *const known[] =
3899 : : {
3900 : : #define DWARF_ONE_KNOWN_DW_INL(NAME, CODE) [CODE] = #NAME,
3901 : : DWARF_ALL_KNOWN_DW_INL
3902 : : #undef DWARF_ONE_KNOWN_DW_INL
3903 : : };
3904 : :
3905 : 3603 : if (likely (code < sizeof (known) / sizeof (known[0])))
3906 : 3603 : return known[code];
3907 : :
3908 : : return NULL;
3909 : : }
3910 : :
3911 : :
3912 : : static const char *
3913 : 30664 : dwarf_encoding_string (unsigned int code)
3914 : : {
3915 : 30664 : static const char *const known[] =
3916 : : {
3917 : : #define DWARF_ONE_KNOWN_DW_ATE(NAME, CODE) [CODE] = #NAME,
3918 : : DWARF_ALL_KNOWN_DW_ATE
3919 : : #undef DWARF_ONE_KNOWN_DW_ATE
3920 : : };
3921 : :
3922 : 30664 : if (likely (code < sizeof (known) / sizeof (known[0])))
3923 : 30664 : return known[code];
3924 : :
3925 : : return NULL;
3926 : : }
3927 : :
3928 : :
3929 : : static const char *
3930 : 0 : dwarf_access_string (unsigned int code)
3931 : : {
3932 : 0 : static const char *const known[] =
3933 : : {
3934 : : #define DWARF_ONE_KNOWN_DW_ACCESS(NAME, CODE) [CODE] = #NAME,
3935 : : DWARF_ALL_KNOWN_DW_ACCESS
3936 : : #undef DWARF_ONE_KNOWN_DW_ACCESS
3937 : : };
3938 : :
3939 : 0 : if (likely (code < sizeof (known) / sizeof (known[0])))
3940 : 0 : return known[code];
3941 : :
3942 : : return NULL;
3943 : : }
3944 : :
3945 : :
3946 : : static const char *
3947 : 0 : dwarf_defaulted_string (unsigned int code)
3948 : : {
3949 : 0 : static const char *const known[] =
3950 : : {
3951 : : #define DWARF_ONE_KNOWN_DW_DEFAULTED(NAME, CODE) [CODE] = #NAME,
3952 : : DWARF_ALL_KNOWN_DW_DEFAULTED
3953 : : #undef DWARF_ONE_KNOWN_DW_DEFAULTED
3954 : : };
3955 : :
3956 : 0 : if (likely (code < sizeof (known) / sizeof (known[0])))
3957 : 0 : return known[code];
3958 : :
3959 : : return NULL;
3960 : : }
3961 : :
3962 : :
3963 : : static const char *
3964 : 0 : dwarf_visibility_string (unsigned int code)
3965 : : {
3966 : 0 : static const char *const known[] =
3967 : : {
3968 : : #define DWARF_ONE_KNOWN_DW_VIS(NAME, CODE) [CODE] = #NAME,
3969 : : DWARF_ALL_KNOWN_DW_VIS
3970 : : #undef DWARF_ONE_KNOWN_DW_VIS
3971 : : };
3972 : :
3973 : 0 : if (likely (code < sizeof (known) / sizeof (known[0])))
3974 : 0 : return known[code];
3975 : :
3976 : : return NULL;
3977 : : }
3978 : :
3979 : :
3980 : : static const char *
3981 : 0 : dwarf_virtuality_string (unsigned int code)
3982 : : {
3983 : 0 : static const char *const known[] =
3984 : : {
3985 : : #define DWARF_ONE_KNOWN_DW_VIRTUALITY(NAME, CODE) [CODE] = #NAME,
3986 : : DWARF_ALL_KNOWN_DW_VIRTUALITY
3987 : : #undef DWARF_ONE_KNOWN_DW_VIRTUALITY
3988 : : };
3989 : :
3990 : 0 : if (likely (code < sizeof (known) / sizeof (known[0])))
3991 : 0 : return known[code];
3992 : :
3993 : : return NULL;
3994 : : }
3995 : :
3996 : :
3997 : : static const char *
3998 : 0 : dwarf_identifier_case_string (unsigned int code)
3999 : : {
4000 : 0 : static const char *const known[] =
4001 : : {
4002 : : #define DWARF_ONE_KNOWN_DW_ID(NAME, CODE) [CODE] = #NAME,
4003 : : DWARF_ALL_KNOWN_DW_ID
4004 : : #undef DWARF_ONE_KNOWN_DW_ID
4005 : : };
4006 : :
4007 : 0 : if (likely (code < sizeof (known) / sizeof (known[0])))
4008 : 0 : return known[code];
4009 : :
4010 : : return NULL;
4011 : : }
4012 : :
4013 : :
4014 : : static const char *
4015 : 0 : dwarf_calling_convention_string (unsigned int code)
4016 : : {
4017 : 0 : static const char *const known[] =
4018 : : {
4019 : : #define DWARF_ONE_KNOWN_DW_CC(NAME, CODE) [CODE] = #NAME,
4020 : : DWARF_ALL_KNOWN_DW_CC
4021 : : #undef DWARF_ONE_KNOWN_DW_CC
4022 : : };
4023 : :
4024 : 0 : if (likely (code < sizeof (known) / sizeof (known[0])))
4025 : 0 : return known[code];
4026 : :
4027 : : return NULL;
4028 : : }
4029 : :
4030 : :
4031 : : static const char *
4032 : 0 : dwarf_ordering_string (unsigned int code)
4033 : : {
4034 : 0 : static const char *const known[] =
4035 : : {
4036 : : #define DWARF_ONE_KNOWN_DW_ORD(NAME, CODE) [CODE] = #NAME,
4037 : : DWARF_ALL_KNOWN_DW_ORD
4038 : : #undef DWARF_ONE_KNOWN_DW_ORD
4039 : : };
4040 : :
4041 : 0 : if (likely (code < sizeof (known) / sizeof (known[0])))
4042 : 0 : return known[code];
4043 : :
4044 : : return NULL;
4045 : : }
4046 : :
4047 : :
4048 : : static const char *
4049 : 8 : dwarf_discr_list_string (unsigned int code)
4050 : : {
4051 : 8 : static const char *const known[] =
4052 : : {
4053 : : #define DWARF_ONE_KNOWN_DW_DSC(NAME, CODE) [CODE] = #NAME,
4054 : : DWARF_ALL_KNOWN_DW_DSC
4055 : : #undef DWARF_ONE_KNOWN_DW_DSC
4056 : : };
4057 : :
4058 : 8 : if (likely (code < sizeof (known) / sizeof (known[0])))
4059 : 8 : return known[code];
4060 : :
4061 : : return NULL;
4062 : : }
4063 : :
4064 : :
4065 : : static const char *
4066 : 619442 : dwarf_locexpr_opcode_string (unsigned int code)
4067 : : {
4068 : 619442 : static const char *const known[] =
4069 : : {
4070 : : /* Normally we can't afford building huge table of 64K entries,
4071 : : most of them zero, just because there are a couple defined
4072 : : values at the far end. In case of opcodes, it's OK. */
4073 : : #define DWARF_ONE_KNOWN_DW_OP(NAME, CODE) [CODE] = #NAME,
4074 : : DWARF_ALL_KNOWN_DW_OP
4075 : : #undef DWARF_ONE_KNOWN_DW_OP
4076 : : };
4077 : :
4078 : 619442 : if (likely (code < sizeof (known) / sizeof (known[0])))
4079 : 619442 : return known[code];
4080 : :
4081 : : return NULL;
4082 : : }
4083 : :
4084 : :
4085 : : static const char *
4086 : 8 : dwarf_unit_string (unsigned int type)
4087 : : {
4088 [ - + - - : 8 : switch (type)
- - + ]
4089 : : {
4090 : : #define DWARF_ONE_KNOWN_DW_UT(NAME, CODE) case CODE: return #NAME;
4091 : 7 : DWARF_ALL_KNOWN_DW_UT
4092 : : #undef DWARF_ONE_KNOWN_DW_UT
4093 : 0 : default:
4094 : 0 : return NULL;
4095 : : }
4096 : : }
4097 : :
4098 : :
4099 : : static const char *
4100 : 16 : dwarf_range_list_encoding_string (unsigned int kind)
4101 : : {
4102 [ - + + - : 16 : switch (kind)
+ - - -
+ ]
4103 : : {
4104 : : #define DWARF_ONE_KNOWN_DW_RLE(NAME, CODE) case CODE: return #NAME;
4105 : 14 : DWARF_ALL_KNOWN_DW_RLE
4106 : : #undef DWARF_ONE_KNOWN_DW_RLE
4107 : 0 : default:
4108 : 0 : return NULL;
4109 : : }
4110 : : }
4111 : :
4112 : :
4113 : : static const char *
4114 : 91 : dwarf_loc_list_encoding_string (unsigned int kind)
4115 : : {
4116 [ - - + + : 91 : switch (kind)
- + - + -
+ ]
4117 : : {
4118 : : #define DWARF_ONE_KNOWN_DW_LLE(NAME, CODE) case CODE: return #NAME;
4119 : 86 : DWARF_ALL_KNOWN_DW_LLE
4120 : : #undef DWARF_ONE_KNOWN_DW_LLE
4121 : 0 : default:
4122 : 0 : return NULL;
4123 : : }
4124 : : }
4125 : :
4126 : :
4127 : : static const char *
4128 : 6 : dwarf_line_content_description_string (unsigned int kind)
4129 : : {
4130 [ + + - - : 6 : switch (kind)
- - ]
4131 : : {
4132 : : #define DWARF_ONE_KNOWN_DW_LNCT(NAME, CODE) case CODE: return #NAME;
4133 : 6 : DWARF_ALL_KNOWN_DW_LNCT
4134 : : #undef DWARF_ONE_KNOWN_DW_LNCT
4135 : 0 : default:
4136 : 0 : return NULL;
4137 : : }
4138 : : }
4139 : :
4140 : :
4141 : : /* Used by all dwarf_foo_name functions. */
4142 : : static const char *
4143 : 9069431 : string_or_unknown (const char *known, unsigned int code,
4144 : : unsigned int lo_user, unsigned int hi_user,
4145 : : bool print_unknown_num)
4146 : : {
4147 : 9069431 : static char unknown_buf[20];
4148 : :
4149 [ - + ]: 9069431 : if (likely (known != NULL))
4150 : : return known;
4151 : :
4152 [ # # # # ]: 0 : if (lo_user != 0 && code >= lo_user && code <= hi_user)
4153 : : {
4154 : 0 : snprintf (unknown_buf, sizeof unknown_buf, "lo_user+%#x",
4155 : : code - lo_user);
4156 : 0 : return unknown_buf;
4157 : : }
4158 : :
4159 [ # # ]: 0 : if (print_unknown_num)
4160 : : {
4161 : 0 : snprintf (unknown_buf, sizeof unknown_buf, "??? (%#x)", code);
4162 : 0 : return unknown_buf;
4163 : : }
4164 : :
4165 : : return "???";
4166 : : }
4167 : :
4168 : :
4169 : : static const char *
4170 : 1027233 : dwarf_tag_name (unsigned int tag)
4171 : : {
4172 : 1027233 : const char *ret = dwarf_tag_string (tag);
4173 : 1027233 : return string_or_unknown (ret, tag, DW_TAG_lo_user, DW_TAG_hi_user, true);
4174 : : }
4175 : :
4176 : : static const char *
4177 : 4003047 : dwarf_attr_name (unsigned int attr)
4178 : : {
4179 : 4003047 : const char *ret = dwarf_attr_string (attr);
4180 : 4003047 : return string_or_unknown (ret, attr, DW_AT_lo_user, DW_AT_hi_user, true);
4181 : : }
4182 : :
4183 : :
4184 : : static const char *
4185 : 4003053 : dwarf_form_name (unsigned int form)
4186 : : {
4187 : 4003053 : const char *ret = dwarf_form_string (form);
4188 : 4003053 : return string_or_unknown (ret, form, 0, 0, true);
4189 : : }
4190 : :
4191 : :
4192 : : static const char *
4193 : 1702 : dwarf_lang_name (unsigned int lang)
4194 : : {
4195 : 1702 : const char *ret = dwarf_lang_string (lang);
4196 : 1702 : return string_or_unknown (ret, lang, DW_LANG_lo_user, DW_LANG_hi_user, false);
4197 : : }
4198 : :
4199 : :
4200 : : static const char *
4201 : 3603 : dwarf_inline_name (unsigned int code)
4202 : : {
4203 [ + - ]: 3603 : const char *ret = dwarf_inline_string (code);
4204 : 3603 : return string_or_unknown (ret, code, 0, 0, false);
4205 : : }
4206 : :
4207 : :
4208 : : static const char *
4209 : 30664 : dwarf_encoding_name (unsigned int code)
4210 : : {
4211 [ + - ]: 30664 : const char *ret = dwarf_encoding_string (code);
4212 : 30664 : return string_or_unknown (ret, code, DW_ATE_lo_user, DW_ATE_hi_user, false);
4213 : : }
4214 : :
4215 : :
4216 : : static const char *
4217 : 0 : dwarf_access_name (unsigned int code)
4218 : : {
4219 [ # # ]: 0 : const char *ret = dwarf_access_string (code);
4220 : 0 : return string_or_unknown (ret, code, 0, 0, false);
4221 : : }
4222 : :
4223 : :
4224 : : static const char *
4225 : 0 : dwarf_defaulted_name (unsigned int code)
4226 : : {
4227 [ # # ]: 0 : const char *ret = dwarf_defaulted_string (code);
4228 : 0 : return string_or_unknown (ret, code, 0, 0, false);
4229 : : }
4230 : :
4231 : :
4232 : : static const char *
4233 : 0 : dwarf_visibility_name (unsigned int code)
4234 : : {
4235 [ # # ]: 0 : const char *ret = dwarf_visibility_string (code);
4236 : 0 : return string_or_unknown (ret, code, 0, 0, false);
4237 : : }
4238 : :
4239 : :
4240 : : static const char *
4241 : 0 : dwarf_virtuality_name (unsigned int code)
4242 : : {
4243 [ # # ]: 0 : const char *ret = dwarf_virtuality_string (code);
4244 : 0 : return string_or_unknown (ret, code, 0, 0, false);
4245 : : }
4246 : :
4247 : :
4248 : : static const char *
4249 : 0 : dwarf_identifier_case_name (unsigned int code)
4250 : : {
4251 [ # # ]: 0 : const char *ret = dwarf_identifier_case_string (code);
4252 : 0 : return string_or_unknown (ret, code, 0, 0, false);
4253 : : }
4254 : :
4255 : :
4256 : : static const char *
4257 : 0 : dwarf_calling_convention_name (unsigned int code)
4258 : : {
4259 [ # # ]: 0 : const char *ret = dwarf_calling_convention_string (code);
4260 : 0 : return string_or_unknown (ret, code, DW_CC_lo_user, DW_CC_hi_user, false);
4261 : : }
4262 : :
4263 : :
4264 : : static const char *
4265 : 0 : dwarf_ordering_name (unsigned int code)
4266 : : {
4267 [ # # ]: 0 : const char *ret = dwarf_ordering_string (code);
4268 : 0 : return string_or_unknown (ret, code, 0, 0, false);
4269 : : }
4270 : :
4271 : :
4272 : : static const char *
4273 : 8 : dwarf_discr_list_name (unsigned int code)
4274 : : {
4275 [ + - ]: 8 : const char *ret = dwarf_discr_list_string (code);
4276 : 8 : return string_or_unknown (ret, code, 0, 0, false);
4277 : : }
4278 : :
4279 : :
4280 : : static const char *
4281 : 8 : dwarf_unit_name (unsigned int type)
4282 : : {
4283 : 8 : const char *ret = dwarf_unit_string (type);
4284 : 8 : return string_or_unknown (ret, type, DW_UT_lo_user, DW_UT_hi_user, true);
4285 : : }
4286 : :
4287 : :
4288 : : static const char *
4289 : 16 : dwarf_range_list_encoding_name (unsigned int kind)
4290 : : {
4291 : 16 : const char *ret = dwarf_range_list_encoding_string (kind);
4292 : 16 : return string_or_unknown (ret, kind, 0, 0, false);
4293 : : }
4294 : :
4295 : :
4296 : : static const char *
4297 : 91 : dwarf_loc_list_encoding_name (unsigned int kind)
4298 : : {
4299 : 91 : const char *ret = dwarf_loc_list_encoding_string (kind);
4300 : 91 : return string_or_unknown (ret, kind, 0, 0, false);
4301 : : }
4302 : :
4303 : :
4304 : : static const char *
4305 : 6 : dwarf_line_content_description_name (unsigned int kind)
4306 : : {
4307 : 6 : const char *ret = dwarf_line_content_description_string (kind);
4308 : 6 : return string_or_unknown (ret, kind, DW_LNCT_lo_user, DW_LNCT_hi_user,
4309 : : false);
4310 : : }
4311 : :
4312 : :
4313 : : static void
4314 : 188 : print_block (size_t n, const void *block)
4315 : : {
4316 [ - + ]: 188 : if (n == 0)
4317 : 0 : puts (_("empty block"));
4318 : : else
4319 : : {
4320 : 188 : printf (_("%zu byte block:"), n);
4321 : 188 : const unsigned char *data = block;
4322 : 827 : do
4323 : 827 : printf (" %02x", *data++);
4324 [ + + ]: 827 : while (--n > 0);
4325 : 188 : putchar ('\n');
4326 : : }
4327 : 188 : }
4328 : :
4329 : : static void
4330 : 0 : print_bytes (size_t n, const unsigned char *bytes)
4331 : : {
4332 : 0 : while (n-- > 0)
4333 : : {
4334 : 0 : printf ("%02x", *bytes++);
4335 [ # # ]: 0 : if (n > 0)
4336 [ # # ]: 0 : printf (" ");
4337 : : }
4338 : 0 : }
4339 : :
4340 : : static int
4341 : 68 : get_indexed_addr (Dwarf_CU *cu, Dwarf_Word idx, Dwarf_Addr *addr)
4342 : : {
4343 [ + - ]: 68 : if (cu == NULL)
4344 : : return -1;
4345 : :
4346 : 68 : Elf_Data *debug_addr = cu->dbg->sectiondata[IDX_debug_addr];
4347 [ + - ]: 68 : if (debug_addr == NULL)
4348 : : return -1;
4349 : :
4350 : 68 : Dwarf_Off base = __libdw_cu_addr_base (cu);
4351 : 68 : Dwarf_Word off = idx * cu->address_size;
4352 [ + - ]: 68 : if (base > debug_addr->d_size
4353 [ + - ]: 68 : || off > debug_addr->d_size - base
4354 [ + - ]: 68 : || cu->address_size > debug_addr->d_size - base - off)
4355 : : return -1;
4356 : :
4357 : 68 : const unsigned char *addrp = debug_addr->d_buf + base + off;
4358 [ - + ]: 68 : if (cu->address_size == 4)
4359 [ # # ]: 0 : *addr = read_4ubyte_unaligned (cu->dbg, addrp);
4360 : : else
4361 [ - + ]: 136 : *addr = read_8ubyte_unaligned (cu->dbg, addrp);
4362 : :
4363 : : return 0;
4364 : : }
4365 : :
4366 : : static void
4367 : 423876 : print_ops (Dwfl_Module *dwflmod, Dwarf *dbg, int indent, int indentrest,
4368 : : unsigned int vers, unsigned int addrsize, unsigned int offset_size,
4369 : : struct Dwarf_CU *cu, Dwarf_Word len, const unsigned char *data)
4370 : : {
4371 [ + + ]: 423876 : const unsigned int ref_size = vers < 3 ? addrsize : offset_size;
4372 : :
4373 [ - + ]: 423876 : if (len == 0)
4374 : : {
4375 : 0 : printf ("%*s(empty)\n", indent, "");
4376 : 0 : return;
4377 : : }
4378 : :
4379 : : #define NEED(n) if (len < (Dwarf_Word) (n)) goto invalid
4380 : : #define CONSUME(n) NEED (n); else len -= (n)
4381 : :
4382 : : Dwarf_Word offset = 0;
4383 [ + + ]: 1043318 : while (len-- > 0)
4384 : : {
4385 : 619442 : uint_fast8_t op = *data++;
4386 : :
4387 [ + - ]: 619442 : const char *op_name = dwarf_locexpr_opcode_string (op);
4388 [ - + ]: 619442 : if (unlikely (op_name == NULL))
4389 : : {
4390 : 0 : static char buf[20];
4391 [ # # ]: 0 : if (op >= DW_OP_lo_user)
4392 : 0 : snprintf (buf, sizeof buf, "lo_user+%#x", op - DW_OP_lo_user);
4393 : : else
4394 : 0 : snprintf (buf, sizeof buf, "??? (%#x)", op);
4395 : : op_name = buf;
4396 : : }
4397 : :
4398 [ + - + + : 619442 : switch (op)
+ - + + -
- + + - +
- - + + +
+ + - - -
- + + + ]
4399 : : {
4400 : 12783 : case DW_OP_addr:;
4401 : : /* Address operand. */
4402 : 12783 : Dwarf_Word addr;
4403 [ - + ]: 12783 : NEED (addrsize);
4404 [ + + ]: 12783 : if (addrsize == 4)
4405 [ + + ]: 48 : addr = read_4ubyte_unaligned (dbg, data);
4406 [ + - ]: 12759 : else if (addrsize == 8)
4407 [ + + ]: 25518 : addr = read_8ubyte_unaligned (dbg, data);
4408 : : else
4409 : 0 : goto invalid;
4410 : 12783 : data += addrsize;
4411 : 12783 : CONSUME (addrsize);
4412 : :
4413 : 12783 : printf ("%*s[%2" PRIuMAX "] %s ",
4414 : : indent, "", (uintmax_t) offset, op_name);
4415 : 12783 : print_dwarf_addr (dwflmod, 0, addr, addr);
4416 : 12783 : printf ("\n");
4417 : :
4418 : 12783 : offset += 1 + addrsize;
4419 : 12783 : break;
4420 : :
4421 : 0 : case DW_OP_call_ref:
4422 : : case DW_OP_GNU_variable_value:
4423 : : /* Offset operand. */
4424 [ # # ]: 0 : if (ref_size != 4 && ref_size != 8)
4425 : 0 : goto invalid; /* Cannot be used in CFA. */
4426 [ # # ]: 0 : NEED (ref_size);
4427 [ # # ]: 0 : if (ref_size == 4)
4428 [ # # ]: 0 : addr = read_4ubyte_unaligned (dbg, data);
4429 : : else
4430 [ # # ]: 0 : addr = read_8ubyte_unaligned (dbg, data);
4431 : 0 : data += ref_size;
4432 : 0 : CONSUME (ref_size);
4433 : : /* addr is a DIE offset, so format it as one. */
4434 : 0 : printf ("%*s[%2" PRIuMAX "] %s [%6" PRIxMAX "]\n",
4435 : : indent, "", (uintmax_t) offset,
4436 : : op_name, (uintmax_t) addr);
4437 : 0 : offset += 1 + ref_size;
4438 : 0 : break;
4439 : :
4440 : 16430 : case DW_OP_deref_size:
4441 : : case DW_OP_xderef_size:
4442 : : case DW_OP_pick:
4443 : : case DW_OP_const1u:
4444 : : // XXX value might be modified by relocation
4445 [ - + ]: 16430 : NEED (1);
4446 : 32860 : printf ("%*s[%2" PRIuMAX "] %s %" PRIu8 "\n",
4447 : : indent, "", (uintmax_t) offset,
4448 : 16430 : op_name, *((uint8_t *) data));
4449 : 16430 : ++data;
4450 : 16430 : --len;
4451 : 16430 : offset += 2;
4452 : 16430 : break;
4453 : :
4454 : 2534 : case DW_OP_const2u:
4455 [ - + ]: 2534 : NEED (2);
4456 : : // XXX value might be modified by relocation
4457 : 7602 : printf ("%*s[%2" PRIuMAX "] %s %" PRIu16 "\n",
4458 : : indent, "", (uintmax_t) offset,
4459 [ - + ]: 5068 : op_name, read_2ubyte_unaligned (dbg, data));
4460 : 2534 : CONSUME (2);
4461 : 2534 : data += 2;
4462 : 2534 : offset += 3;
4463 : 2534 : break;
4464 : :
4465 : 1896 : case DW_OP_const4u:
4466 [ - + ]: 1896 : NEED (4);
4467 : : // XXX value might be modified by relocation
4468 [ - + ]: 3792 : printf ("%*s[%2" PRIuMAX "] %s %" PRIu32 "\n",
4469 : : indent, "", (uintmax_t) offset,
4470 : : op_name, read_4ubyte_unaligned (dbg, data));
4471 : 1896 : CONSUME (4);
4472 : 1896 : data += 4;
4473 : 1896 : offset += 5;
4474 : 1896 : break;
4475 : :
4476 : 0 : case DW_OP_const8u:
4477 [ # # ]: 0 : NEED (8);
4478 : : // XXX value might be modified by relocation
4479 : 0 : printf ("%*s[%2" PRIuMAX "] %s %" PRIu64 "\n",
4480 : : indent, "", (uintmax_t) offset,
4481 [ # # ]: 0 : op_name, (uint64_t) read_8ubyte_unaligned (dbg, data));
4482 : 0 : CONSUME (8);
4483 : 0 : data += 8;
4484 : 0 : offset += 9;
4485 : 0 : break;
4486 : :
4487 : 3009 : case DW_OP_const1s:
4488 [ - + ]: 3009 : NEED (1);
4489 : : // XXX value might be modified by relocation
4490 : 6018 : printf ("%*s[%2" PRIuMAX "] %s %" PRId8 "\n",
4491 : : indent, "", (uintmax_t) offset,
4492 : 3009 : op_name, *((int8_t *) data));
4493 : 3009 : ++data;
4494 : 3009 : --len;
4495 : 3009 : offset += 2;
4496 : 3009 : break;
4497 : :
4498 : 10 : case DW_OP_const2s:
4499 [ - + ]: 10 : NEED (2);
4500 : : // XXX value might be modified by relocation
4501 : 30 : printf ("%*s[%2" PRIuMAX "] %s %" PRId16 "\n",
4502 : : indent, "", (uintmax_t) offset,
4503 [ - + ]: 20 : op_name, read_2sbyte_unaligned (dbg, data));
4504 : 10 : CONSUME (2);
4505 : 10 : data += 2;
4506 : 10 : offset += 3;
4507 : 10 : break;
4508 : :
4509 : 0 : case DW_OP_const4s:
4510 [ # # ]: 0 : NEED (4);
4511 : : // XXX value might be modified by relocation
4512 [ # # ]: 0 : printf ("%*s[%2" PRIuMAX "] %s %" PRId32 "\n",
4513 : : indent, "", (uintmax_t) offset,
4514 : : op_name, read_4sbyte_unaligned (dbg, data));
4515 : 0 : CONSUME (4);
4516 : 0 : data += 4;
4517 : 0 : offset += 5;
4518 : 0 : break;
4519 : :
4520 : 0 : case DW_OP_const8s:
4521 [ # # ]: 0 : NEED (8);
4522 : : // XXX value might be modified by relocation
4523 [ # # ]: 0 : printf ("%*s[%2" PRIuMAX "] %s %" PRId64 "\n",
4524 : : indent, "", (uintmax_t) offset,
4525 : : op_name, read_8sbyte_unaligned (dbg, data));
4526 : 0 : CONSUME (8);
4527 : 0 : data += 8;
4528 : 0 : offset += 9;
4529 : 0 : break;
4530 : :
4531 : 7302 : case DW_OP_piece:
4532 : : case DW_OP_regx:
4533 : : case DW_OP_plus_uconst:
4534 : 7302 : case DW_OP_constu:;
4535 : 7302 : const unsigned char *start = data;
4536 : 7302 : uint64_t uleb;
4537 [ - + ]: 7302 : NEED (1);
4538 : 7302 : get_uleb128 (uleb, data, data + len);
4539 : 7302 : printf ("%*s[%2" PRIuMAX "] %s %" PRIu64 "\n",
4540 : : indent, "", (uintmax_t) offset, op_name, uleb);
4541 [ - + ]: 7302 : CONSUME (data - start);
4542 : 7302 : offset += 1 + (data - start);
4543 : 7302 : break;
4544 : :
4545 : 2 : case DW_OP_addrx:
4546 : : case DW_OP_GNU_addr_index:
4547 : : case DW_OP_constx:
4548 : 2 : case DW_OP_GNU_const_index:;
4549 : 2 : start = data;
4550 [ - + ]: 2 : NEED (1);
4551 : 2 : get_uleb128 (uleb, data, data + len);
4552 : 2 : printf ("%*s[%2" PRIuMAX "] %s [%" PRIu64 "] ",
4553 : : indent, "", (uintmax_t) offset, op_name, uleb);
4554 [ - + ]: 2 : CONSUME (data - start);
4555 : 2 : offset += 1 + (data - start);
4556 [ - + ]: 2 : if (get_indexed_addr (cu, uleb, &addr) != 0)
4557 : 0 : printf ("???\n");
4558 : : else
4559 : : {
4560 : 2 : print_dwarf_addr (dwflmod, 0, addr, addr);
4561 : 2 : printf ("\n");
4562 : : }
4563 : : break;
4564 : :
4565 : 0 : case DW_OP_bit_piece:
4566 : 0 : start = data;
4567 : 0 : uint64_t uleb2;
4568 [ # # ]: 0 : NEED (1);
4569 : 0 : get_uleb128 (uleb, data, data + len);
4570 : 0 : NEED (1);
4571 : 0 : get_uleb128 (uleb2, data, data + len);
4572 : 0 : printf ("%*s[%2" PRIuMAX "] %s %" PRIu64 ", %" PRIu64 "\n",
4573 : : indent, "", (uintmax_t) offset, op_name, uleb, uleb2);
4574 [ # # ]: 0 : CONSUME (data - start);
4575 : 0 : offset += 1 + (data - start);
4576 : 0 : break;
4577 : :
4578 : 95657 : case DW_OP_fbreg:
4579 : : case DW_OP_breg0 ... DW_OP_breg31:
4580 : : case DW_OP_consts:
4581 : 95657 : start = data;
4582 : 95657 : int64_t sleb;
4583 [ - + ]: 95657 : NEED (1);
4584 : 95657 : get_sleb128 (sleb, data, data + len);
4585 : 95657 : printf ("%*s[%2" PRIuMAX "] %s %" PRId64 "\n",
4586 : : indent, "", (uintmax_t) offset, op_name, sleb);
4587 [ - + ]: 95657 : CONSUME (data - start);
4588 : 95657 : offset += 1 + (data - start);
4589 : 95657 : break;
4590 : :
4591 : 0 : case DW_OP_bregx:
4592 : 0 : start = data;
4593 [ # # ]: 0 : NEED (1);
4594 : 0 : get_uleb128 (uleb, data, data + len);
4595 : 0 : NEED (1);
4596 : 0 : get_sleb128 (sleb, data, data + len);
4597 : 0 : printf ("%*s[%2" PRIuMAX "] %s %" PRIu64 " %" PRId64 "\n",
4598 : : indent, "", (uintmax_t) offset, op_name, uleb, sleb);
4599 [ # # ]: 0 : CONSUME (data - start);
4600 : 0 : offset += 1 + (data - start);
4601 : 0 : break;
4602 : :
4603 : 0 : case DW_OP_call2:
4604 [ # # ]: 0 : NEED (2);
4605 : 0 : printf ("%*s[%2" PRIuMAX "] %s [%6" PRIx16 "]\n",
4606 : : indent, "", (uintmax_t) offset, op_name,
4607 [ # # ]: 0 : read_2ubyte_unaligned (dbg, data));
4608 : 0 : CONSUME (2);
4609 : 0 : data += 2;
4610 : 0 : offset += 3;
4611 : 0 : break;
4612 : :
4613 : 4 : case DW_OP_call4:
4614 [ - + ]: 4 : NEED (4);
4615 [ - + ]: 8 : printf ("%*s[%2" PRIuMAX "] %s [%6" PRIx32 "]\n",
4616 : : indent, "", (uintmax_t) offset, op_name,
4617 : : read_4ubyte_unaligned (dbg, data));
4618 : 4 : CONSUME (4);
4619 : 4 : data += 4;
4620 : 4 : offset += 5;
4621 : 4 : break;
4622 : :
4623 : 213 : case DW_OP_skip:
4624 : : case DW_OP_bra:
4625 [ - + ]: 213 : NEED (2);
4626 : 426 : printf ("%*s[%2" PRIuMAX "] %s %" PRIuMAX "\n",
4627 : : indent, "", (uintmax_t) offset, op_name,
4628 [ - + ]: 426 : (uintmax_t) (offset + read_2sbyte_unaligned (dbg, data) + 3));
4629 : 213 : CONSUME (2);
4630 : 213 : data += 2;
4631 : 213 : offset += 3;
4632 : 213 : break;
4633 : :
4634 : 188 : case DW_OP_implicit_value:
4635 : 188 : start = data;
4636 [ - + ]: 188 : NEED (1);
4637 : 188 : get_uleb128 (uleb, data, data + len);
4638 : 188 : printf ("%*s[%2" PRIuMAX "] %s: ",
4639 : : indent, "", (uintmax_t) offset, op_name);
4640 [ - + ]: 188 : NEED (uleb);
4641 : 188 : print_block (uleb, data);
4642 : 188 : data += uleb;
4643 [ - + ]: 188 : CONSUME (data - start);
4644 : 188 : offset += 1 + (data - start);
4645 : 188 : break;
4646 : :
4647 : 2618 : case DW_OP_implicit_pointer:
4648 : : case DW_OP_GNU_implicit_pointer:
4649 : : /* DIE offset operand. */
4650 : 2618 : start = data;
4651 [ - + ]: 2618 : NEED (ref_size);
4652 [ - + ]: 2618 : if (ref_size != 4 && ref_size != 8)
4653 : 0 : goto invalid; /* Cannot be used in CFA. */
4654 [ + - ]: 2618 : if (ref_size == 4)
4655 [ - + ]: 5236 : addr = read_4ubyte_unaligned (dbg, data);
4656 : : else
4657 [ # # ]: 0 : addr = read_8ubyte_unaligned (dbg, data);
4658 : 2618 : data += ref_size;
4659 : : /* Byte offset operand. */
4660 [ - + ]: 2618 : NEED (1);
4661 : 2618 : get_sleb128 (sleb, data, data + len);
4662 : :
4663 : 2618 : printf ("%*s[%2" PRIuMAX "] %s [%6" PRIxMAX "] %+" PRId64 "\n",
4664 : : indent, "", (intmax_t) offset,
4665 : : op_name, (uintmax_t) addr, sleb);
4666 [ - + ]: 2618 : CONSUME (data - start);
4667 : 2618 : offset += 1 + (data - start);
4668 : 2618 : break;
4669 : :
4670 : 41695 : case DW_OP_entry_value:
4671 : : case DW_OP_GNU_entry_value:
4672 : : /* Size plus expression block. */
4673 : 41695 : start = data;
4674 [ - + ]: 41695 : NEED (1);
4675 : 41695 : get_uleb128 (uleb, data, data + len);
4676 : 41695 : printf ("%*s[%2" PRIuMAX "] %s:\n",
4677 : : indent, "", (uintmax_t) offset, op_name);
4678 [ - + ]: 41695 : NEED (uleb);
4679 : 41695 : print_ops (dwflmod, dbg, indent + 5, indent + 5, vers,
4680 : : addrsize, offset_size, cu, uleb, data);
4681 : 41695 : data += uleb;
4682 [ - + ]: 41695 : CONSUME (data - start);
4683 : 41695 : offset += 1 + (data - start);
4684 : 41695 : break;
4685 : :
4686 : 0 : case DW_OP_const_type:
4687 : : case DW_OP_GNU_const_type:
4688 : : /* uleb128 CU relative DW_TAG_base_type DIE offset, 1-byte
4689 : : unsigned size plus block. */
4690 : 0 : start = data;
4691 [ # # ]: 0 : NEED (1);
4692 : 0 : get_uleb128 (uleb, data, data + len);
4693 [ # # # # ]: 0 : if (! print_unresolved_addresses && cu != NULL)
4694 : 0 : uleb += cu->start;
4695 : 0 : NEED (1);
4696 : 0 : uint8_t usize = *(uint8_t *) data++;
4697 [ # # ]: 0 : NEED (usize);
4698 : 0 : printf ("%*s[%2" PRIuMAX "] %s [%6" PRIxMAX "] ",
4699 : : indent, "", (uintmax_t) offset, op_name, uleb);
4700 : 0 : print_block (usize, data);
4701 : 0 : data += usize;
4702 [ # # ]: 0 : CONSUME (data - start);
4703 : 0 : offset += 1 + (data - start);
4704 : 0 : break;
4705 : :
4706 : 0 : case DW_OP_regval_type:
4707 : : case DW_OP_GNU_regval_type:
4708 : : /* uleb128 register number, uleb128 CU relative
4709 : : DW_TAG_base_type DIE offset. */
4710 : 0 : start = data;
4711 [ # # ]: 0 : NEED (1);
4712 : 0 : get_uleb128 (uleb, data, data + len);
4713 : 0 : NEED (1);
4714 : 0 : get_uleb128 (uleb2, data, data + len);
4715 [ # # # # ]: 0 : if (! print_unresolved_addresses && cu != NULL)
4716 : 0 : uleb2 += cu->start;
4717 : 0 : printf ("%*s[%2" PRIuMAX "] %s %" PRIu64 " [%6" PRIx64 "]\n",
4718 : : indent, "", (uintmax_t) offset, op_name, uleb, uleb2);
4719 [ # # ]: 0 : CONSUME (data - start);
4720 : 0 : offset += 1 + (data - start);
4721 : 0 : break;
4722 : :
4723 : 0 : case DW_OP_deref_type:
4724 : : case DW_OP_GNU_deref_type:
4725 : : /* 1-byte unsigned size of value, uleb128 CU relative
4726 : : DW_TAG_base_type DIE offset. */
4727 : 0 : start = data;
4728 [ # # ]: 0 : NEED (1);
4729 : 0 : usize = *(uint8_t *) data++;
4730 : 0 : NEED (1);
4731 : 0 : get_uleb128 (uleb, data, data + len);
4732 [ # # # # ]: 0 : if (! print_unresolved_addresses && cu != NULL)
4733 : 0 : uleb += cu->start;
4734 : 0 : printf ("%*s[%2" PRIuMAX "] %s %" PRIu8 " [%6" PRIxMAX "]\n",
4735 : : indent, "", (uintmax_t) offset,
4736 : : op_name, usize, uleb);
4737 [ # # ]: 0 : CONSUME (data - start);
4738 : 0 : offset += 1 + (data - start);
4739 : 0 : break;
4740 : :
4741 : 0 : case DW_OP_xderef_type:
4742 : : /* 1-byte unsigned size of value, uleb128 base_type DIE offset. */
4743 : 0 : start = data;
4744 [ # # ]: 0 : NEED (1);
4745 : 0 : usize = *(uint8_t *) data++;
4746 : 0 : NEED (1);
4747 : 0 : get_uleb128 (uleb, data, data + len);
4748 : 0 : printf ("%*s[%4" PRIuMAX "] %s %" PRIu8 " [%6" PRIxMAX "]\n",
4749 : : indent, "", (uintmax_t) offset,
4750 : : op_name, usize, uleb);
4751 [ # # ]: 0 : CONSUME (data - start);
4752 : 0 : offset += 1 + (data - start);
4753 : 0 : break;
4754 : :
4755 : 390 : case DW_OP_convert:
4756 : : case DW_OP_GNU_convert:
4757 : : case DW_OP_reinterpret:
4758 : : case DW_OP_GNU_reinterpret:
4759 : : /* uleb128 CU relative offset to DW_TAG_base_type, or zero
4760 : : for conversion to untyped. */
4761 : 390 : start = data;
4762 [ - + ]: 390 : NEED (1);
4763 : 390 : get_uleb128 (uleb, data, data + len);
4764 [ + + + - : 390 : if (uleb != 0 && ! print_unresolved_addresses && cu != NULL)
+ - ]
4765 : 260 : uleb += cu->start;
4766 : 390 : printf ("%*s[%2" PRIuMAX "] %s [%6" PRIxMAX "]\n",
4767 : : indent, "", (uintmax_t) offset, op_name, uleb);
4768 [ - + ]: 390 : CONSUME (data - start);
4769 : 390 : offset += 1 + (data - start);
4770 : 390 : break;
4771 : :
4772 : 3 : case DW_OP_GNU_parameter_ref:
4773 : : /* 4 byte CU relative reference to the abstract optimized away
4774 : : DW_TAG_formal_parameter. */
4775 [ - + ]: 3 : NEED (4);
4776 [ - + ]: 3 : uintmax_t param_off = (uintmax_t) read_4ubyte_unaligned (dbg, data);
4777 [ + - + - ]: 3 : if (! print_unresolved_addresses && cu != NULL)
4778 : 3 : param_off += cu->start;
4779 : 3 : printf ("%*s[%2" PRIuMAX "] %s [%6" PRIxMAX "]\n",
4780 : : indent, "", (uintmax_t) offset, op_name, param_off);
4781 : 3 : CONSUME (4);
4782 : 3 : data += 4;
4783 : 3 : offset += 5;
4784 : 3 : break;
4785 : :
4786 : : default:
4787 : : /* No Operand. */
4788 : 434708 : printf ("%*s[%2" PRIuMAX "] %s\n",
4789 : : indent, "", (uintmax_t) offset, op_name);
4790 : 434708 : ++offset;
4791 : 434708 : break;
4792 : : }
4793 : :
4794 : 619442 : indent = indentrest;
4795 : 619442 : continue;
4796 : :
4797 : 0 : invalid:
4798 : 0 : printf (_("%*s[%2" PRIuMAX "] %s <TRUNCATED>\n"),
4799 : : indent, "", (uintmax_t) offset, op_name);
4800 : : break;
4801 : : }
4802 : : }
4803 : :
4804 : :
4805 : : struct listptr
4806 : : {
4807 : : Dwarf_Off offset:(64 - 3);
4808 : : bool addr64:1;
4809 : : bool dwarf64:1;
4810 : : bool warned:1;
4811 : : struct Dwarf_CU *cu;
4812 : : unsigned int attr;
4813 : : };
4814 : :
4815 : : #define listptr_offset_size(p) ((p)->dwarf64 ? 8 : 4)
4816 : : #define listptr_address_size(p) ((p)->addr64 ? 8 : 4)
4817 : :
4818 : : static Dwarf_Addr
4819 : 127769 : cudie_base (Dwarf_Die *cudie)
4820 : : {
4821 : 127769 : Dwarf_Addr base;
4822 : : /* Find the base address of the compilation unit. It will normally
4823 : : be specified by DW_AT_low_pc. In DWARF-3 draft 4, the base
4824 : : address could be overridden by DW_AT_entry_pc. It's been
4825 : : removed, but GCC emits DW_AT_entry_pc and not DW_AT_lowpc for
4826 : : compilation units with discontinuous ranges. */
4827 [ - + ]: 127769 : if (unlikely (dwarf_lowpc (cudie, &base) != 0))
4828 : : {
4829 : 0 : Dwarf_Attribute attr_mem;
4830 [ # # ]: 0 : if (dwarf_formaddr (dwarf_attr (cudie, DW_AT_entry_pc, &attr_mem),
4831 : : &base) != 0)
4832 : 0 : base = 0;
4833 : : }
4834 : 127769 : return base;
4835 : : }
4836 : :
4837 : : static Dwarf_Addr
4838 : 127769 : listptr_base (struct listptr *p)
4839 : : {
4840 : 127769 : Dwarf_Die cu = CUDIE (p->cu);
4841 : 127769 : return cudie_base (&cu);
4842 : : }
4843 : :
4844 : : /* To store the name used in compare_listptr */
4845 : : static const char *sort_listptr_name;
4846 : :
4847 : : static int
4848 : 856548 : compare_listptr (const void *a, const void *b)
4849 : : {
4850 : 856548 : const char *name = sort_listptr_name;
4851 : 856548 : struct listptr *p1 = (void *) a;
4852 : 856548 : struct listptr *p2 = (void *) b;
4853 : :
4854 [ + + ]: 856548 : if (p1->offset < p2->offset)
4855 : : return -1;
4856 [ + + ]: 82212 : if (p1->offset > p2->offset)
4857 : : return 1;
4858 : :
4859 [ + - + - ]: 3740 : if (!p1->warned && !p2->warned)
4860 : : {
4861 [ - + ]: 3740 : if (p1->addr64 != p2->addr64)
4862 : : {
4863 : 0 : p1->warned = p2->warned = true;
4864 : 3740 : error (0, 0,
4865 : 0 : _("%s %#" PRIx64 " used with different address sizes"),
4866 : 0 : name, (uint64_t) p1->offset);
4867 : : }
4868 [ - + ]: 3740 : if (p1->dwarf64 != p2->dwarf64)
4869 : : {
4870 : 0 : p1->warned = p2->warned = true;
4871 : 3740 : error (0, 0,
4872 : 0 : _("%s %#" PRIx64 " used with different offset sizes"),
4873 : 0 : name, (uint64_t) p1->offset);
4874 : : }
4875 [ - + ]: 3740 : if (listptr_base (p1) != listptr_base (p2))
4876 : : {
4877 : 0 : p1->warned = p2->warned = true;
4878 : 3740 : error (0, 0,
4879 : 0 : _("%s %#" PRIx64 " used with different base addresses"),
4880 : 0 : name, (uint64_t) p1->offset);
4881 : : }
4882 [ - + ]: 3740 : if (p1->attr != p2 ->attr)
4883 : : {
4884 : 0 : p1->warned = p2->warned = true;
4885 : 0 : error (0, 0,
4886 : 0 : _("%s %#" PRIx64
4887 : : " used with different attribute %s and %s"),
4888 : 0 : name, (uint64_t) p1->offset, dwarf_attr_name (p1->attr),
4889 : : dwarf_attr_name (p2->attr));
4890 : : }
4891 : : }
4892 : :
4893 : : return 0;
4894 : : }
4895 : :
4896 : : struct listptr_table
4897 : : {
4898 : : size_t n;
4899 : : size_t alloc;
4900 : : struct listptr *table;
4901 : : };
4902 : :
4903 : : static struct listptr_table known_locsptr;
4904 : : static struct listptr_table known_loclistsptr;
4905 : : static struct listptr_table known_rangelistptr;
4906 : : static struct listptr_table known_rnglistptr;
4907 : : static struct listptr_table known_addrbases;
4908 : : static struct listptr_table known_stroffbases;
4909 : :
4910 : : static void
4911 : 269 : reset_listptr (struct listptr_table *table)
4912 : : {
4913 : 269 : free (table->table);
4914 : 269 : table->table = NULL;
4915 : 269 : table->n = table->alloc = 0;
4916 : : }
4917 : :
4918 : : /* Returns false if offset doesn't fit. See struct listptr. */
4919 : : static bool
4920 : 123943 : notice_listptr (enum section_e section, struct listptr_table *table,
4921 : : uint_fast8_t address_size, uint_fast8_t offset_size,
4922 : : struct Dwarf_CU *cu, Dwarf_Off offset, unsigned int attr)
4923 : : {
4924 [ + + ]: 123943 : if (print_debug_sections & section)
4925 : : {
4926 [ + + ]: 123817 : if (table->n == table->alloc)
4927 : : {
4928 [ + + ]: 192 : if (table->alloc == 0)
4929 : 81 : table->alloc = 128;
4930 : : else
4931 : 111 : table->alloc *= 2;
4932 : 192 : table->table = xrealloc (table->table,
4933 : 192 : table->alloc * sizeof table->table[0]);
4934 : : }
4935 : :
4936 : 123817 : struct listptr *p = &table->table[table->n++];
4937 : :
4938 : 123817 : *p = (struct listptr)
4939 : : {
4940 : 123817 : .addr64 = address_size == 8,
4941 : 123817 : .dwarf64 = offset_size == 8,
4942 : : .offset = offset,
4943 : : .cu = cu,
4944 : : .attr = attr
4945 : : };
4946 : :
4947 [ - + ]: 123817 : if (p->offset != offset)
4948 : : {
4949 : 0 : table->n--;
4950 : 0 : return false;
4951 : : }
4952 : : }
4953 : : return true;
4954 : : }
4955 : :
4956 : : static void
4957 : 85 : sort_listptr (struct listptr_table *table, const char *name)
4958 : : {
4959 [ + + ]: 85 : if (table->n > 0)
4960 : : {
4961 : 81 : sort_listptr_name = name;
4962 : 81 : qsort (table->table, table->n, sizeof table->table[0],
4963 : : &compare_listptr);
4964 : : }
4965 : 85 : }
4966 : :
4967 : : static bool
4968 : 120286 : skip_listptr_hole (struct listptr_table *table, size_t *idxp,
4969 : : uint_fast8_t *address_sizep, uint_fast8_t *offset_sizep,
4970 : : Dwarf_Addr *base, struct Dwarf_CU **cu, ptrdiff_t offset,
4971 : : unsigned char **readp, unsigned char *endp,
4972 : : unsigned int *attr)
4973 : : {
4974 [ + - ]: 120286 : if (table->n == 0)
4975 : : return false;
4976 : :
4977 [ + - + + ]: 190327 : while (*idxp < table->n && table->table[*idxp].offset < (Dwarf_Off) offset)
4978 : 70041 : ++*idxp;
4979 : :
4980 : 120286 : struct listptr *p = &table->table[*idxp];
4981 : :
4982 [ + - ]: 120286 : if (*idxp == table->n
4983 [ - + ]: 120286 : || p->offset >= (Dwarf_Off) (endp - *readp + offset))
4984 : : {
4985 : 0 : *readp = endp;
4986 : 0 : printf (_(" [%6tx] <UNUSED GARBAGE IN REST OF SECTION>\n"),
4987 : : offset);
4988 : 0 : return true;
4989 : : }
4990 : :
4991 [ + + ]: 120286 : if (p->offset != (Dwarf_Off) offset)
4992 : : {
4993 : 5 : *readp += p->offset - offset;
4994 : 5 : printf (_(" [%6tx] <UNUSED GARBAGE> ... %" PRIu64 " bytes ...\n"),
4995 : 5 : offset, (Dwarf_Off) p->offset - offset);
4996 : 5 : return true;
4997 : : }
4998 : :
4999 [ + - ]: 120281 : if (address_sizep != NULL)
5000 [ + + ]: 120285 : *address_sizep = listptr_address_size (p);
5001 [ + + ]: 120281 : if (offset_sizep != NULL)
5002 [ + - ]: 213180 : *offset_sizep = listptr_offset_size (p);
5003 [ + - ]: 120281 : if (base != NULL)
5004 : 120281 : *base = listptr_base (p);
5005 [ + - ]: 120281 : if (cu != NULL)
5006 : 120281 : *cu = p->cu;
5007 [ + + ]: 120281 : if (attr != NULL)
5008 : 106590 : *attr = p->attr;
5009 : :
5010 : : return false;
5011 : : }
5012 : :
5013 : : static Dwarf_Off
5014 : 53259 : next_listptr_offset (struct listptr_table *table, size_t *idxp, Dwarf_Off off)
5015 : : {
5016 : : /* Note that multiple attributes could in theory point to the same loclist
5017 : : offset, so make sure we pick one that is bigger than the current one.
5018 : : The table is sorted on offset. */
5019 [ + - ]: 53259 : if (*idxp < table->n)
5020 : : {
5021 [ + - ]: 53675 : while (++*idxp < table->n)
5022 : : {
5023 : 53675 : Dwarf_Off next = table->table[*idxp].offset;
5024 [ + + ]: 53675 : if (next > off)
5025 : 53259 : return next;
5026 : : }
5027 : : }
5028 : : return 0;
5029 : : }
5030 : :
5031 : : /* Returns the listptr associated with the given index, or NULL. */
5032 : : static struct listptr *
5033 : 356 : get_listptr (struct listptr_table *table, size_t idx)
5034 : : {
5035 : 356 : if (idx >= table->n)
5036 : : return NULL;
5037 : 303 : return &table->table[idx];
5038 : : }
5039 : :
5040 : : /* Returns the next index, base address and CU associated with the
5041 : : list unit offsets. If there is none false is returned, otherwise
5042 : : true. Assumes the table has been sorted. */
5043 : : static bool
5044 : 8 : listptr_cu (struct listptr_table *table, size_t *idxp,
5045 : : Dwarf_Off start, Dwarf_Off end,
5046 : : Dwarf_Addr *base, struct Dwarf_CU **cu)
5047 : : {
5048 [ + - ]: 17 : while (*idxp < table->n
5049 [ + + ]: 17 : && table->table[*idxp].offset < start)
5050 : 9 : ++*idxp;
5051 : :
5052 [ + - ]: 8 : if (*idxp < table->n
5053 [ + - ]: 8 : && table->table[*idxp].offset >= start
5054 [ + - ]: 8 : && table->table[*idxp].offset < end)
5055 : : {
5056 : 8 : struct listptr *p = &table->table[*idxp];
5057 : 8 : *base = listptr_base (p);
5058 : 8 : *cu = p->cu;
5059 : 8 : return true;
5060 : : }
5061 : :
5062 : : return false;
5063 : : }
5064 : :
5065 : : /* Returns the next index with the current CU for the given attribute.
5066 : : If there is none false is returned, otherwise true. Assumes the
5067 : : table has been sorted. */
5068 : : static bool
5069 : 92 : listptr_attr (struct listptr_table *table, size_t idxp,
5070 : : Dwarf_Off offset, unsigned int attr)
5071 : : {
5072 : 346 : struct listptr *listptr;
5073 : 346 : do
5074 : : {
5075 [ + + ]: 346 : listptr = get_listptr (table, idxp);
5076 [ + - ]: 298 : if (listptr == NULL)
5077 : : return false;
5078 : :
5079 [ + + + + ]: 298 : if (listptr->offset == offset && listptr->attr == attr)
5080 : : return true;
5081 : :
5082 : 297 : idxp++;
5083 : : }
5084 [ + + ]: 297 : while (listptr->offset <= offset);
5085 : :
5086 : : return false;
5087 : : }
5088 : :
5089 : : static void
5090 : 40 : print_debug_abbrev_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
5091 : : Ebl *ebl, GElf_Ehdr *ehdr __attribute__ ((unused)),
5092 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
5093 : : {
5094 : 80 : const size_t sh_size = (dbg->sectiondata[IDX_debug_abbrev] ?
5095 [ + - ]: 40 : dbg->sectiondata[IDX_debug_abbrev]->d_size : 0);
5096 : :
5097 : 40 : printf (_("\nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"
5098 : : " [ Code]\n"),
5099 : : elf_ndxscn (scn), section_name (ebl, shdr),
5100 : 40 : (uint64_t) shdr->sh_offset);
5101 : :
5102 : 40 : Dwarf_Off offset = 0;
5103 [ + + ]: 1673 : while (offset < sh_size)
5104 : : {
5105 : 1633 : printf (_("\nAbbreviation section at offset %" PRIu64 ":\n"),
5106 : : offset);
5107 : :
5108 : 148505 : while (1)
5109 : 73436 : {
5110 : 75069 : size_t length;
5111 : 75069 : Dwarf_Abbrev abbrev;
5112 : :
5113 : 75069 : int res = dwarf_offabbrev (dbg, offset, &length, &abbrev);
5114 [ + + ]: 75069 : if (res != 0)
5115 : : {
5116 [ - + ]: 1633 : if (unlikely (res < 0))
5117 : : {
5118 : 0 : printf (_("\
5119 : : *** error while reading abbreviation: %s\n"),
5120 : : dwarf_errmsg (-1));
5121 : 0 : return;
5122 : : }
5123 : :
5124 : : /* This is the NUL byte at the end of the section. */
5125 : 1633 : ++offset;
5126 : 1633 : break;
5127 : : }
5128 : :
5129 : : /* We know these calls can never fail. */
5130 : 73436 : unsigned int code = dwarf_getabbrevcode (&abbrev);
5131 : 73436 : unsigned int tag = dwarf_getabbrevtag (&abbrev);
5132 : 73436 : int has_children = dwarf_abbrevhaschildren (&abbrev);
5133 : :
5134 [ + + ]: 73436 : printf (_(" [%5u] offset: %" PRId64
5135 : : ", children: %s, tag: %s\n"),
5136 : : code, (int64_t) offset,
5137 : : has_children ? yes_str : no_str,
5138 : : dwarf_tag_name (tag));
5139 : :
5140 : 73436 : size_t cnt = 0;
5141 : 73436 : unsigned int name;
5142 : 73436 : unsigned int form;
5143 : 73436 : Dwarf_Sword data;
5144 : 73436 : Dwarf_Off enoffset;
5145 [ + + ]: 415418 : while (dwarf_getabbrevattr_data (&abbrev, cnt, &name, &form,
5146 : : &data, &enoffset) == 0)
5147 : : {
5148 : 341982 : printf (" attr: %s, form: %s",
5149 : : dwarf_attr_name (name), dwarf_form_name (form));
5150 [ - + ]: 341982 : if (form == DW_FORM_implicit_const)
5151 : 0 : printf (" (%" PRId64 ")", data);
5152 : 341982 : printf (", offset: %#" PRIx64 "\n", (uint64_t) enoffset);
5153 : 341982 : ++cnt;
5154 : : }
5155 : :
5156 : 73436 : offset += length;
5157 : : }
5158 : : }
5159 : : }
5160 : :
5161 : :
5162 : : static void
5163 : 2 : print_debug_addr_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
5164 : : Ebl *ebl, GElf_Ehdr *ehdr,
5165 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
5166 : : {
5167 : 2 : printf (_("\
5168 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
5169 : : elf_ndxscn (scn), section_name (ebl, shdr),
5170 : 2 : (uint64_t) shdr->sh_offset);
5171 : :
5172 [ + - ]: 2 : if (shdr->sh_size == 0)
5173 : : return;
5174 : :
5175 : : /* We like to get the section from libdw to make sure they are relocated. */
5176 : 4 : Elf_Data *data = (dbg->sectiondata[IDX_debug_addr]
5177 [ - + ]: 2 : ?: elf_rawdata (scn, NULL));
5178 [ - + ]: 2 : if (unlikely (data == NULL))
5179 : : {
5180 : 0 : error (0, 0, _("cannot get .debug_addr section data: %s"),
5181 : : elf_errmsg (-1));
5182 : 0 : return;
5183 : : }
5184 : :
5185 : 2 : size_t idx = 0;
5186 : 2 : sort_listptr (&known_addrbases, "addr_base");
5187 : :
5188 : 2 : const unsigned char *start = (const unsigned char *) data->d_buf;
5189 : 2 : const unsigned char *readp = start;
5190 : 2 : const unsigned char *readendp = ((const unsigned char *) data->d_buf
5191 : 2 : + data->d_size);
5192 : :
5193 : 2 : while (readp < readendp)
5194 : : {
5195 : : /* We cannot really know whether or not there is an header. The
5196 : : DebugFission extension to DWARF4 doesn't add one. The DWARF5
5197 : : .debug_addr variant does. Whether or not we have an header,
5198 : : DW_AT_[GNU_]addr_base points at "index 0". So if the current
5199 : : offset equals the CU addr_base then we can just start
5200 : : printing addresses. If there is no CU with an exact match
5201 : : then we'll try to parse the header first. */
5202 : 4 : Dwarf_Off off = (Dwarf_Off) (readp
5203 : 4 : - (const unsigned char *) data->d_buf);
5204 : :
5205 : 4 : printf ("Table at offset %" PRIx64 " ", off);
5206 : :
5207 [ + - ]: 4 : struct listptr *listptr = get_listptr (&known_addrbases, idx++);
5208 : 4 : const unsigned char *next_unitp;
5209 : :
5210 : 4 : uint64_t unit_length;
5211 : 4 : uint16_t version;
5212 : 4 : uint8_t address_size;
5213 : 4 : uint8_t segment_size;
5214 [ - + ]: 4 : if (listptr == NULL)
5215 : : {
5216 : 0 : error (0, 0, "Warning: No CU references .debug_addr after %" PRIx64,
5217 : : off);
5218 : :
5219 : : /* We will have to assume it is just addresses to the end... */
5220 [ # # ]: 0 : address_size = ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 4 : 8;
5221 : 0 : next_unitp = readendp;
5222 : 0 : printf ("Unknown CU:\n");
5223 : : }
5224 : : else
5225 : : {
5226 : 4 : Dwarf_Die cudie;
5227 [ - + ]: 4 : if (dwarf_cu_die (listptr->cu, &cudie,
5228 : : NULL, NULL, NULL, NULL,
5229 : : NULL, NULL) == NULL)
5230 : 0 : printf ("Unknown CU (%s):\n", dwarf_errmsg (-1));
5231 : : else
5232 : 4 : printf ("for CU [%6" PRIx64 "]:\n", dwarf_dieoffset (&cudie));
5233 : :
5234 [ + + ]: 4 : if (listptr->offset == off)
5235 : : {
5236 [ - + ]: 2 : address_size = listptr_address_size (listptr);
5237 : 2 : segment_size = 0;
5238 : 2 : version = 4;
5239 : :
5240 : : /* The addresses start here, but where do they end? */
5241 [ + + ]: 2 : listptr = get_listptr (&known_addrbases, idx);
5242 [ + - ]: 1 : if (listptr == NULL)
5243 : : next_unitp = readendp;
5244 [ + - ]: 1 : else if (listptr->cu->version < 5)
5245 : : {
5246 : 1 : next_unitp = start + listptr->offset;
5247 [ + - - + ]: 1 : if (listptr->offset < off || listptr->offset > data->d_size)
5248 : : {
5249 : 0 : error (0, 0,
5250 : : "Warning: Bad address base for next unit at %"
5251 : : PRIx64, off);
5252 : 0 : next_unitp = readendp;
5253 : : }
5254 : : }
5255 : : else
5256 : : {
5257 : : /* Tricky, we don't have a header for this unit, but
5258 : : there is one for the next. We will have to
5259 : : "guess" how big it is and subtract it from the
5260 : : offset (because that points after the header). */
5261 [ # # ]: 0 : unsigned int offset_size = listptr_offset_size (listptr);
5262 : 0 : Dwarf_Off next_off = (listptr->offset
5263 : 0 : - (offset_size == 4 ? 4 : 12) /* len */
5264 : : - 2 /* version */
5265 : : - 1 /* address size */
5266 : 0 : - 1); /* segment selector size */
5267 : 0 : next_unitp = start + next_off;
5268 [ # # # # ]: 0 : if (next_off < off || next_off > data->d_size)
5269 : : {
5270 : 0 : error (0, 0,
5271 : : "Warning: Couldn't calculate .debug_addr "
5272 : : " unit length at %" PRIx64, off);
5273 : 0 : next_unitp = readendp;
5274 : : }
5275 : : }
5276 : 2 : unit_length = (uint64_t) (next_unitp - readp);
5277 : :
5278 : : /* Pretend we have a header. */
5279 : 2 : printf ("\n");
5280 : 2 : printf (_(" Length: %8" PRIu64 "\n"),
5281 : : unit_length);
5282 : 2 : printf (_(" DWARF version: %8" PRIu16 "\n"), version);
5283 : 2 : printf (_(" Address size: %8" PRIu64 "\n"),
5284 : : (uint64_t) address_size);
5285 : 2 : printf (_(" Segment size: %8" PRIu64 "\n"),
5286 : : (uint64_t) segment_size);
5287 : 4 : printf ("\n");
5288 : : }
5289 : : else
5290 : : {
5291 : : /* OK, we have to parse an header first. */
5292 [ - + ]: 2 : unit_length = read_4ubyte_unaligned_inc (dbg, readp);
5293 [ - + ]: 2 : if (unlikely (unit_length == 0xffffffff))
5294 : : {
5295 [ # # ]: 0 : if (unlikely (readp > readendp - 8))
5296 : : {
5297 : 0 : invalid_data:
5298 : 0 : error (0, 0, "Invalid data");
5299 : 0 : return;
5300 : : }
5301 [ # # ]: 0 : unit_length = read_8ubyte_unaligned_inc (dbg, readp);
5302 : : }
5303 : 2 : printf ("\n");
5304 : 2 : printf (_(" Length: %8" PRIu64 "\n"),
5305 : : unit_length);
5306 : :
5307 : : /* We need at least 2-bytes (version) + 1-byte
5308 : : (addr_size) + 1-byte (segment_size) = 4 bytes to
5309 : : complete the header. And this unit cannot go beyond
5310 : : the section data. */
5311 [ + - ]: 2 : if (readp > readendp - 4
5312 [ + - ]: 2 : || unit_length < 4
5313 [ - + ]: 2 : || unit_length > (uint64_t) (readendp - readp))
5314 : 0 : goto invalid_data;
5315 : :
5316 : 2 : next_unitp = readp + unit_length;
5317 : :
5318 [ - + ]: 2 : version = read_2ubyte_unaligned_inc (dbg, readp);
5319 : 2 : printf (_(" DWARF version: %8" PRIu16 "\n"), version);
5320 : :
5321 [ - + ]: 2 : if (version != 5)
5322 : : {
5323 : 0 : error (0, 0, _("Unknown version"));
5324 : 0 : goto next_unit;
5325 : : }
5326 : :
5327 : 2 : address_size = *readp++;
5328 : 2 : printf (_(" Address size: %8" PRIu64 "\n"),
5329 : : (uint64_t) address_size);
5330 : :
5331 [ - + ]: 2 : if (address_size != 4 && address_size != 8)
5332 : : {
5333 : 0 : error (0, 0, _("unsupported address size"));
5334 : 0 : goto next_unit;
5335 : : }
5336 : :
5337 : 2 : segment_size = *readp++;
5338 : 2 : printf (_(" Segment size: %8" PRIu64 "\n"),
5339 : : (uint64_t) segment_size);
5340 : 2 : printf ("\n");
5341 : :
5342 [ - + ]: 2 : if (segment_size != 0)
5343 : : {
5344 : 0 : error (0, 0, _("unsupported segment size"));
5345 : 0 : goto next_unit;
5346 : : }
5347 : :
5348 [ - + ]: 2 : if (listptr->offset != (Dwarf_Off) (readp - start))
5349 : : {
5350 : 0 : error (0, 0, "Address index doesn't start after header");
5351 : 0 : goto next_unit;
5352 : : }
5353 : : }
5354 : : }
5355 : :
5356 : 4 : int digits = 1;
5357 : 4 : size_t addresses = (next_unitp - readp) / address_size;
5358 [ + + ]: 8 : while (addresses >= 10)
5359 : : {
5360 : 4 : ++digits;
5361 : 4 : addresses /= 10;
5362 : : }
5363 : :
5364 : 4 : unsigned int uidx = 0;
5365 : 4 : size_t index_offset = readp - (const unsigned char *) data->d_buf;
5366 : 4 : printf (" Addresses start at offset 0x%zx:\n", index_offset);
5367 : 76 : while (readp <= next_unitp - address_size)
5368 : : {
5369 [ - + - + : 144 : Dwarf_Addr addr = read_addr_unaligned_inc (address_size, dbg,
- - - + ]
5370 : : readp);
5371 : 72 : printf (" [%*u] ", digits, uidx++);
5372 : 72 : print_dwarf_addr (dwflmod, address_size, addr, addr);
5373 [ + + ]: 76 : printf ("\n");
5374 : : }
5375 : 4 : printf ("\n");
5376 : :
5377 [ + - ]: 4 : if (readp != next_unitp)
5378 [ + + ]: 6 : error (0, 0, "extra %zd bytes at end of unit",
5379 : 0 : (size_t) (next_unitp - readp));
5380 : :
5381 : 4 : next_unit:
5382 : : readp = next_unitp;
5383 : : }
5384 : : }
5385 : :
5386 : : /* Print content of DWARF .debug_aranges section. We fortunately do
5387 : : not have to know a bit about the structure of the section, libdwarf
5388 : : takes care of it. */
5389 : : static void
5390 : 1 : print_decoded_aranges_section (Ebl *ebl, GElf_Ehdr *ehdr, Elf_Scn *scn,
5391 : : GElf_Shdr *shdr, Dwarf *dbg)
5392 : : {
5393 : 1 : Dwarf_Aranges *aranges;
5394 : 1 : size_t cnt;
5395 [ - + ]: 1 : if (unlikely (dwarf_getaranges (dbg, &aranges, &cnt) != 0))
5396 : : {
5397 : 0 : error (0, 0, _("cannot get .debug_aranges content: %s"),
5398 : : dwarf_errmsg (-1));
5399 : 0 : return;
5400 : : }
5401 : :
5402 : 1 : GElf_Shdr glink_mem;
5403 : 1 : GElf_Shdr *glink;
5404 : 1 : glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link), &glink_mem);
5405 [ - + ]: 1 : if (glink == NULL)
5406 : : {
5407 : 0 : error (0, 0, _("invalid sh_link value in section %zu"),
5408 : : elf_ndxscn (scn));
5409 : 0 : return;
5410 : : }
5411 : :
5412 : 1 : printf (ngettext ("\
5413 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 " contains %zu entry:\n",
5414 : : "\
5415 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 " contains %zu entries:\n",
5416 : : cnt),
5417 : : elf_ndxscn (scn), section_name (ebl, shdr),
5418 : 1 : (uint64_t) shdr->sh_offset, cnt);
5419 : :
5420 : : /* Compute floor(log16(cnt)). */
5421 : 1 : size_t tmp = cnt;
5422 : 1 : int digits = 1;
5423 [ - + ]: 1 : while (tmp >= 16)
5424 : : {
5425 : 0 : ++digits;
5426 : 0 : tmp >>= 4;
5427 : : }
5428 : :
5429 [ + + ]: 6 : for (size_t n = 0; n < cnt; ++n)
5430 : : {
5431 : 5 : Dwarf_Arange *runp = dwarf_onearange (aranges, n);
5432 [ - + ]: 5 : if (unlikely (runp == NULL))
5433 : : {
5434 : 0 : printf ("cannot get arange %zu: %s\n", n, dwarf_errmsg (-1));
5435 : 0 : return;
5436 : : }
5437 : :
5438 : 5 : Dwarf_Addr start;
5439 : 5 : Dwarf_Word length;
5440 : 5 : Dwarf_Off offset;
5441 : :
5442 [ - + ]: 5 : if (unlikely (dwarf_getarangeinfo (runp, &start, &length, &offset) != 0))
5443 : 0 : printf (_(" [%*zu] ???\n"), digits, n);
5444 : : else
5445 : 5 : printf (_(" [%*zu] start: %0#*" PRIx64
5446 : : ", length: %5" PRIu64 ", CU DIE offset: %6"
5447 : : PRId64 "\n"),
5448 [ - + ]: 5 : digits, n, ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 10 : 18,
5449 : : (uint64_t) start, (uint64_t) length, (int64_t) offset);
5450 : : }
5451 : : }
5452 : :
5453 : :
5454 : : /* Print content of DWARF .debug_aranges section. */
5455 : : static void
5456 : 44 : print_debug_aranges_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
5457 : : Ebl *ebl, GElf_Ehdr *ehdr, Elf_Scn *scn,
5458 : : GElf_Shdr *shdr, Dwarf *dbg)
5459 : : {
5460 [ + + ]: 44 : if (decodedaranges)
5461 : : {
5462 : 1 : print_decoded_aranges_section (ebl, ehdr, scn, shdr, dbg);
5463 : 1 : return;
5464 : : }
5465 : :
5466 : 86 : Elf_Data *data = (dbg->sectiondata[IDX_debug_aranges]
5467 [ - + ]: 43 : ?: elf_rawdata (scn, NULL));
5468 : :
5469 [ - + ]: 43 : if (unlikely (data == NULL))
5470 : : {
5471 : 0 : error (0, 0, _("cannot get .debug_aranges content: %s"),
5472 : : elf_errmsg (-1));
5473 : 0 : return;
5474 : : }
5475 : :
5476 : 43 : printf (_("\
5477 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
5478 : : elf_ndxscn (scn), section_name (ebl, shdr),
5479 : 43 : (uint64_t) shdr->sh_offset);
5480 : :
5481 : 43 : const unsigned char *readp = data->d_buf;
5482 : 43 : const unsigned char *readendp = readp + data->d_size;
5483 : :
5484 [ + + ]: 1673 : while (readp < readendp)
5485 : : {
5486 : 1630 : const unsigned char *hdrstart = readp;
5487 : 1630 : size_t start_offset = hdrstart - (const unsigned char *) data->d_buf;
5488 : :
5489 : 1630 : printf (_("\nTable at offset %zu:\n"), start_offset);
5490 [ - + ]: 1630 : if (readp + 4 > readendp)
5491 : : {
5492 : 0 : invalid_data:
5493 : 0 : error (0, 0, _("invalid data in section [%zu] '%s'"),
5494 : : elf_ndxscn (scn), section_name (ebl, shdr));
5495 : 0 : return;
5496 : : }
5497 : :
5498 [ + + ]: 1630 : Dwarf_Word length = read_4ubyte_unaligned_inc (dbg, readp);
5499 : 1630 : unsigned int length_bytes = 4;
5500 [ - + ]: 1630 : if (length == DWARF3_LENGTH_64_BIT)
5501 : : {
5502 [ # # ]: 0 : if (readp + 8 > readendp)
5503 : 0 : goto invalid_data;
5504 [ # # ]: 0 : length = read_8ubyte_unaligned_inc (dbg, readp);
5505 : 0 : length_bytes = 8;
5506 : : }
5507 : :
5508 : 1630 : const unsigned char *nexthdr = readp + length;
5509 : 1630 : printf (_("\n Length: %6" PRIu64 "\n"),
5510 : : (uint64_t) length);
5511 : :
5512 [ - + ]: 1630 : if (unlikely (length > (size_t) (readendp - readp)))
5513 : 0 : goto invalid_data;
5514 : :
5515 [ - + ]: 1630 : if (length == 0)
5516 : 0 : continue;
5517 : :
5518 [ - + ]: 1630 : if (readp + 2 > readendp)
5519 : 0 : goto invalid_data;
5520 [ + + ]: 1630 : uint_fast16_t version = read_2ubyte_unaligned_inc (dbg, readp);
5521 : 1630 : printf (_(" DWARF version: %6" PRIuFAST16 "\n"),
5522 : : version);
5523 [ - + ]: 1630 : if (version != 2)
5524 : : {
5525 : 0 : error (0, 0, _("unsupported aranges version"));
5526 : 0 : goto next_table;
5527 : : }
5528 : :
5529 : 1630 : Dwarf_Word offset;
5530 [ - + ]: 1630 : if (readp + length_bytes > readendp)
5531 : 0 : goto invalid_data;
5532 [ - + ]: 1630 : if (length_bytes == 8)
5533 [ # # ]: 0 : offset = read_8ubyte_unaligned_inc (dbg, readp);
5534 : : else
5535 [ + + ]: 3260 : offset = read_4ubyte_unaligned_inc (dbg, readp);
5536 : 1630 : printf (_(" CU offset: %6" PRIx64 "\n"),
5537 : : (uint64_t) offset);
5538 : :
5539 [ - + ]: 1630 : if (readp + 1 > readendp)
5540 : 0 : goto invalid_data;
5541 : 1630 : unsigned int address_size = *readp++;
5542 : 1630 : printf (_(" Address size: %6" PRIu64 "\n"),
5543 : : (uint64_t) address_size);
5544 [ - + ]: 1630 : if (address_size != 4 && address_size != 8)
5545 : : {
5546 : 0 : error (0, 0, _("unsupported address size"));
5547 : 0 : goto next_table;
5548 : : }
5549 : :
5550 [ - + ]: 1630 : if (readp + 1 > readendp)
5551 : 0 : goto invalid_data;
5552 : 1630 : unsigned int segment_size = *readp++;
5553 : 1630 : printf (_(" Segment size: %6" PRIu64 "\n\n"),
5554 : : (uint64_t) segment_size);
5555 [ - + - - ]: 1630 : if (segment_size != 0 && segment_size != 4 && segment_size != 8)
5556 : : {
5557 : 0 : error (0, 0, _("unsupported segment size"));
5558 : 0 : goto next_table;
5559 : : }
5560 : :
5561 : : /* Round the address to the next multiple of 2*address_size. */
5562 : 1630 : readp += ((2 * address_size - ((readp - hdrstart) % (2 * address_size)))
5563 : 1630 : % (2 * address_size));
5564 : :
5565 : 1630 : while (readp < nexthdr)
5566 : : {
5567 : 3277 : Dwarf_Word range_address;
5568 : 3277 : Dwarf_Word range_length;
5569 : 3277 : Dwarf_Word segment = 0;
5570 [ - + ]: 3277 : if (readp + 2 * address_size + segment_size > readendp)
5571 : 0 : goto invalid_data;
5572 [ + + ]: 3277 : if (address_size == 4)
5573 : : {
5574 [ + + ]: 36 : range_address = read_4ubyte_unaligned_inc (dbg, readp);
5575 [ + + ]: 72 : range_length = read_4ubyte_unaligned_inc (dbg, readp);
5576 : : }
5577 : : else
5578 : : {
5579 [ + + ]: 3241 : range_address = read_8ubyte_unaligned_inc (dbg, readp);
5580 [ + + ]: 6482 : range_length = read_8ubyte_unaligned_inc (dbg, readp);
5581 : : }
5582 : :
5583 [ - + ]: 3277 : if (segment_size == 4)
5584 [ # # ]: 0 : segment = read_4ubyte_unaligned_inc (dbg, readp);
5585 [ - + ]: 3277 : else if (segment_size == 8)
5586 [ # # ]: 0 : segment = read_8ubyte_unaligned_inc (dbg, readp);
5587 : :
5588 [ + + ]: 3277 : if (range_address == 0 && range_length == 0 && segment == 0)
5589 : : break;
5590 : :
5591 : 1647 : printf (" ");
5592 : 1647 : print_dwarf_addr (dwflmod, address_size, range_address,
5593 : : range_address);
5594 : 1647 : printf ("..");
5595 : 1647 : print_dwarf_addr (dwflmod, address_size,
5596 : 1647 : range_address + range_length - 1,
5597 : : range_length);
5598 [ - + ]: 1647 : if (segment_size != 0)
5599 : 0 : printf (" (%" PRIx64 ")\n", (uint64_t) segment);
5600 : : else
5601 [ + - ]: 4924 : printf ("\n");
5602 : : }
5603 : :
5604 : 1630 : next_table:
5605 [ - + ]: 1630 : if (readp != nexthdr)
5606 : : {
5607 : 0 : size_t padding = nexthdr - readp;
5608 : 0 : printf (_(" %zu padding bytes\n"), padding);
5609 : 0 : readp = nexthdr;
5610 : : }
5611 : : }
5612 : : }
5613 : :
5614 : :
5615 : : static bool is_split_dwarf (Dwarf *dbg, uint64_t *id, Dwarf_CU **split_cu);
5616 : :
5617 : : /* Returns true and sets cu and cu_base if the given Dwarf is a split
5618 : : DWARF (.dwo) file. */
5619 : : static bool
5620 : 0 : split_dwarf_cu_base (Dwarf *dbg, Dwarf_CU **cu, Dwarf_Addr *cu_base)
5621 : : {
5622 : 0 : uint64_t id;
5623 [ # # ]: 0 : if (is_split_dwarf (dbg, &id, cu))
5624 : : {
5625 : 0 : Dwarf_Die cudie;
5626 [ # # ]: 0 : if (dwarf_cu_info (*cu, NULL, NULL, &cudie, NULL, NULL, NULL, NULL) == 0)
5627 : : {
5628 : 0 : *cu_base = cudie_base (&cudie);
5629 : 0 : return true;
5630 : : }
5631 : : }
5632 : : return false;
5633 : : }
5634 : :
5635 : : /* Print content of DWARF .debug_rnglists section. */
5636 : : static void
5637 : 3 : print_debug_rnglists_section (Dwfl_Module *dwflmod,
5638 : : Ebl *ebl,
5639 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
5640 : : Elf_Scn *scn, GElf_Shdr *shdr,
5641 : : Dwarf *dbg __attribute__((unused)))
5642 : : {
5643 : 3 : printf (_("\
5644 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
5645 : : elf_ndxscn (scn), section_name (ebl, shdr),
5646 : 3 : (uint64_t) shdr->sh_offset);
5647 : :
5648 : 6 : Elf_Data *data =(dbg->sectiondata[IDX_debug_rnglists]
5649 [ - + ]: 3 : ?: elf_rawdata (scn, NULL));
5650 [ - + ]: 3 : if (unlikely (data == NULL))
5651 : : {
5652 : 0 : error (0, 0, _("cannot get .debug_rnglists content: %s"),
5653 : : elf_errmsg (-1));
5654 : 0 : return;
5655 : : }
5656 : :
5657 : : /* For the listptr to get the base address/CU. */
5658 : 3 : sort_listptr (&known_rnglistptr, "rnglistptr");
5659 : 3 : size_t listptr_idx = 0;
5660 : :
5661 : 3 : const unsigned char *readp = data->d_buf;
5662 : 3 : const unsigned char *const dataend = ((unsigned char *) data->d_buf
5663 : 3 : + data->d_size);
5664 [ + + ]: 6 : while (readp < dataend)
5665 : : {
5666 [ - + ]: 3 : if (unlikely (readp > dataend - 4))
5667 : : {
5668 : 0 : invalid_data:
5669 : 0 : error (0, 0, _("invalid data in section [%zu] '%s'"),
5670 : : elf_ndxscn (scn), section_name (ebl, shdr));
5671 : 0 : return;
5672 : : }
5673 : :
5674 : 3 : ptrdiff_t offset = readp - (unsigned char *) data->d_buf;
5675 : 3 : printf (_("Table at Offset 0x%" PRIx64 ":\n\n"),
5676 : : (uint64_t) offset);
5677 : :
5678 [ - + ]: 3 : uint64_t unit_length = read_4ubyte_unaligned_inc (dbg, readp);
5679 : 3 : unsigned int offset_size = 4;
5680 [ - + ]: 3 : if (unlikely (unit_length == 0xffffffff))
5681 : : {
5682 [ # # ]: 0 : if (unlikely (readp > dataend - 8))
5683 : 0 : goto invalid_data;
5684 : :
5685 [ # # ]: 0 : unit_length = read_8ubyte_unaligned_inc (dbg, readp);
5686 : 0 : offset_size = 8;
5687 : : }
5688 : 3 : printf (_(" Length: %8" PRIu64 "\n"), unit_length);
5689 : :
5690 : : /* We need at least 2-bytes + 1-byte + 1-byte + 4-bytes = 8
5691 : : bytes to complete the header. And this unit cannot go beyond
5692 : : the section data. */
5693 [ + - ]: 3 : if (readp > dataend - 8
5694 [ + - ]: 3 : || unit_length < 8
5695 [ - + ]: 3 : || unit_length > (uint64_t) (dataend - readp))
5696 : 0 : goto invalid_data;
5697 : :
5698 : 3 : const unsigned char *nexthdr = readp + unit_length;
5699 : :
5700 [ - + ]: 3 : uint16_t version = read_2ubyte_unaligned_inc (dbg, readp);
5701 : 3 : printf (_(" DWARF version: %8" PRIu16 "\n"), version);
5702 : :
5703 [ - + ]: 3 : if (version != 5)
5704 : : {
5705 : 0 : error (0, 0, _("Unknown version"));
5706 : 0 : goto next_table;
5707 : : }
5708 : :
5709 : 3 : uint8_t address_size = *readp++;
5710 : 3 : printf (_(" Address size: %8" PRIu64 "\n"),
5711 : : (uint64_t) address_size);
5712 : :
5713 [ - + ]: 3 : if (address_size != 4 && address_size != 8)
5714 : : {
5715 : 0 : error (0, 0, _("unsupported address size"));
5716 : 0 : goto next_table;
5717 : : }
5718 : :
5719 : 3 : uint8_t segment_size = *readp++;
5720 : 3 : printf (_(" Segment size: %8" PRIu64 "\n"),
5721 : : (uint64_t) segment_size);
5722 : :
5723 [ - + - - ]: 3 : if (segment_size != 0 && segment_size != 4 && segment_size != 8)
5724 : : {
5725 : 0 : error (0, 0, _("unsupported segment size"));
5726 : 0 : goto next_table;
5727 : : }
5728 : :
5729 [ - + ]: 3 : uint32_t offset_entry_count = read_4ubyte_unaligned_inc (dbg, readp);
5730 : 3 : printf (_(" Offset entries: %8" PRIu64 "\n"),
5731 : : (uint64_t) offset_entry_count);
5732 : :
5733 : : /* We need the CU that uses this unit to get the initial base address. */
5734 : 3 : Dwarf_Addr cu_base = 0;
5735 : 3 : struct Dwarf_CU *cu = NULL;
5736 [ - + ]: 3 : if (listptr_cu (&known_rnglistptr, &listptr_idx,
5737 : : (Dwarf_Off) offset,
5738 : 3 : (Dwarf_Off) (nexthdr - (unsigned char *) data->d_buf),
5739 : : &cu_base, &cu)
5740 [ # # ]: 0 : || split_dwarf_cu_base (dbg, &cu, &cu_base))
5741 : 3 : {
5742 : 3 : Dwarf_Die cudie;
5743 [ - + ]: 3 : if (dwarf_cu_die (cu, &cudie,
5744 : : NULL, NULL, NULL, NULL,
5745 : : NULL, NULL) == NULL)
5746 : 0 : printf (_(" Unknown CU base: "));
5747 : : else
5748 : 3 : printf (_(" CU [%6" PRIx64 "] base: "),
5749 : : dwarf_dieoffset (&cudie));
5750 : 3 : print_dwarf_addr (dwflmod, address_size, cu_base, cu_base);
5751 : 3 : printf ("\n");
5752 : : }
5753 : : else
5754 : 0 : printf (_(" Not associated with a CU.\n"));
5755 : :
5756 : 3 : printf ("\n");
5757 : :
5758 : 3 : const unsigned char *offset_array_start = readp;
5759 [ + + ]: 3 : if (offset_entry_count > 0)
5760 : : {
5761 : 1 : uint64_t max_entries = (unit_length - 8) / offset_size;
5762 [ - + ]: 1 : if (offset_entry_count > max_entries)
5763 : : {
5764 : 0 : error (0, 0,
5765 : 0 : _("too many offset entries for unit length"));
5766 : 0 : offset_entry_count = max_entries;
5767 : : }
5768 : :
5769 : 1 : printf (_(" Offsets starting at 0x%" PRIx64 ":\n"),
5770 : : (uint64_t) (offset_array_start
5771 : 1 : - (unsigned char *) data->d_buf));
5772 [ + + ]: 3 : for (uint32_t idx = 0; idx < offset_entry_count; idx++)
5773 : : {
5774 : 2 : printf (" [%6" PRIu32 "] ", idx);
5775 [ + - ]: 2 : if (offset_size == 4)
5776 : : {
5777 [ - + ]: 2 : uint32_t off = read_4ubyte_unaligned_inc (dbg, readp);
5778 : 2 : printf ("0x%" PRIx32 "\n", off);
5779 : : }
5780 : : else
5781 : : {
5782 [ # # ]: 0 : uint64_t off = read_8ubyte_unaligned_inc (dbg, readp);
5783 : 2 : printf ("0x%" PRIx64 "\n", off);
5784 : : }
5785 : : }
5786 : 1 : printf ("\n");
5787 : : }
5788 : :
5789 : 3 : Dwarf_Addr base = cu_base;
5790 : 3 : bool start_of_list = true;
5791 : 3 : while (readp < nexthdr)
5792 : : {
5793 : 16 : uint8_t kind = *readp++;
5794 : 16 : uint64_t op1, op2;
5795 : :
5796 : : /* Skip padding. */
5797 [ - + ]: 16 : if (start_of_list && kind == DW_RLE_end_of_list)
5798 : 0 : continue;
5799 : :
5800 [ + + ]: 16 : if (start_of_list)
5801 : : {
5802 : 5 : base = cu_base;
5803 : 5 : printf (" Offset: %" PRIx64 ", Index: %" PRIx64 "\n",
5804 : 5 : (uint64_t) (readp - (unsigned char *) data->d_buf - 1),
5805 : 5 : (uint64_t) (readp - offset_array_start - 1));
5806 : 5 : start_of_list = false;
5807 : : }
5808 : :
5809 : 16 : printf (" %s", dwarf_range_list_encoding_name (kind));
5810 [ + - - - : 16 : switch (kind)
+ + - +
- ]
5811 : : {
5812 : 5 : case DW_RLE_end_of_list:
5813 : 5 : start_of_list = true;
5814 : 5 : printf ("\n\n");
5815 : : break;
5816 : :
5817 : 0 : case DW_RLE_base_addressx:
5818 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
5819 : : {
5820 : 0 : invalid_range:
5821 : 0 : error (0, 0, _("invalid range list data"));
5822 : 0 : goto next_table;
5823 : : }
5824 : 0 : get_uleb128 (op1, readp, nexthdr);
5825 : 0 : printf (" %" PRIx64 "\n", op1);
5826 [ # # ]: 0 : if (! print_unresolved_addresses)
5827 : : {
5828 : 0 : Dwarf_Addr addr;
5829 [ # # ]: 0 : if (get_indexed_addr (cu, op1, &addr) != 0)
5830 : 0 : printf (" ???\n");
5831 : : else
5832 : : {
5833 : 0 : printf (" ");
5834 : 0 : print_dwarf_addr (dwflmod, address_size, addr, addr);
5835 : 0 : printf ("\n");
5836 : : }
5837 : : }
5838 : : break;
5839 : :
5840 : 0 : case DW_RLE_startx_endx:
5841 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
5842 : 0 : goto invalid_range;
5843 : 0 : get_uleb128 (op1, readp, nexthdr);
5844 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
5845 : 0 : goto invalid_range;
5846 : 0 : get_uleb128 (op2, readp, nexthdr);
5847 : 0 : printf (" %" PRIx64 ", %" PRIx64 "\n", op1, op2);
5848 [ # # ]: 0 : if (! print_unresolved_addresses)
5849 : : {
5850 : 0 : Dwarf_Addr addr1;
5851 : 0 : Dwarf_Addr addr2;
5852 [ # # ]: 0 : if (get_indexed_addr (cu, op1, &addr1) != 0
5853 [ # # ]: 0 : || get_indexed_addr (cu, op2, &addr2) != 0)
5854 : : {
5855 : 0 : printf (" ???..\n");
5856 : 0 : printf (" ???\n");
5857 : : }
5858 : : else
5859 : : {
5860 : 0 : printf (" ");
5861 : 0 : print_dwarf_addr (dwflmod, address_size, addr1, addr1);
5862 : 0 : printf ("..\n ");
5863 : 0 : print_dwarf_addr (dwflmod, address_size,
5864 : : addr2 - 1, addr2);
5865 : 0 : printf ("\n");
5866 : : }
5867 : : }
5868 : : break;
5869 : :
5870 : 0 : case DW_RLE_startx_length:
5871 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
5872 : 0 : goto invalid_range;
5873 : 0 : get_uleb128 (op1, readp, nexthdr);
5874 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
5875 : 0 : goto invalid_range;
5876 : 0 : get_uleb128 (op2, readp, nexthdr);
5877 : 0 : printf (" %" PRIx64 ", %" PRIx64 "\n", op1, op2);
5878 [ # # ]: 0 : if (! print_unresolved_addresses)
5879 : : {
5880 : 0 : Dwarf_Addr addr1;
5881 : 0 : Dwarf_Addr addr2;
5882 [ # # ]: 0 : if (get_indexed_addr (cu, op1, &addr1) != 0)
5883 : : {
5884 : 0 : printf (" ???..\n");
5885 : 0 : printf (" ???\n");
5886 : : }
5887 : : else
5888 : : {
5889 : 0 : addr2 = addr1 + op2;
5890 : 0 : printf (" ");
5891 : 0 : print_dwarf_addr (dwflmod, address_size, addr1, addr1);
5892 : 0 : printf ("..\n ");
5893 : 0 : print_dwarf_addr (dwflmod, address_size,
5894 : : addr2 - 1, addr2);
5895 : 0 : printf ("\n");
5896 : : }
5897 : : }
5898 : : break;
5899 : :
5900 : 4 : case DW_RLE_offset_pair:
5901 [ - + ]: 4 : if ((uint64_t) (nexthdr - readp) < 1)
5902 : 0 : goto invalid_range;
5903 : 4 : get_uleb128 (op1, readp, nexthdr);
5904 [ - + ]: 4 : if ((uint64_t) (nexthdr - readp) < 1)
5905 : 0 : goto invalid_range;
5906 : 4 : get_uleb128 (op2, readp, nexthdr);
5907 : 4 : printf (" %" PRIx64 ", %" PRIx64 "\n", op1, op2);
5908 [ + - ]: 4 : if (! print_unresolved_addresses)
5909 : : {
5910 : 4 : op1 += base;
5911 : 4 : op2 += base;
5912 : 4 : printf (" ");
5913 : 4 : print_dwarf_addr (dwflmod, address_size, op1, op1);
5914 : 4 : printf ("..\n ");
5915 : 4 : print_dwarf_addr (dwflmod, address_size, op2 - 1, op2);
5916 : 4 : printf ("\n");
5917 : : }
5918 : : break;
5919 : :
5920 : 2 : case DW_RLE_base_address:
5921 [ - + ]: 2 : if (address_size == 4)
5922 : : {
5923 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 4)
5924 : 0 : goto invalid_range;
5925 [ # # ]: 0 : op1 = read_4ubyte_unaligned_inc (dbg, readp);
5926 : : }
5927 : : else
5928 : : {
5929 [ - + ]: 2 : if ((uint64_t) (nexthdr - readp) < 8)
5930 : 0 : goto invalid_range;
5931 [ - + ]: 4 : op1 = read_8ubyte_unaligned_inc (dbg, readp);
5932 : : }
5933 : 2 : base = op1;
5934 : 2 : printf (" 0x%" PRIx64 "\n", base);
5935 [ + - ]: 2 : if (! print_unresolved_addresses)
5936 : : {
5937 : 2 : printf (" ");
5938 : 2 : print_dwarf_addr (dwflmod, address_size, base, base);
5939 : 2 : printf ("\n");
5940 : : }
5941 : : break;
5942 : :
5943 : 0 : case DW_RLE_start_end:
5944 [ # # ]: 0 : if (address_size == 4)
5945 : : {
5946 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 8)
5947 : 0 : goto invalid_range;
5948 [ # # ]: 0 : op1 = read_4ubyte_unaligned_inc (dbg, readp);
5949 [ # # ]: 0 : op2 = read_4ubyte_unaligned_inc (dbg, readp);
5950 : : }
5951 : : else
5952 : : {
5953 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 16)
5954 : 0 : goto invalid_range;
5955 [ # # ]: 0 : op1 = read_8ubyte_unaligned_inc (dbg, readp);
5956 [ # # ]: 0 : op2 = read_8ubyte_unaligned_inc (dbg, readp);
5957 : : }
5958 : 0 : printf (" 0x%" PRIx64 "..0x%" PRIx64 "\n", op1, op2);
5959 [ # # ]: 0 : if (! print_unresolved_addresses)
5960 : : {
5961 : 0 : printf (" ");
5962 : 0 : print_dwarf_addr (dwflmod, address_size, op1, op1);
5963 : 0 : printf ("..\n ");
5964 : 0 : print_dwarf_addr (dwflmod, address_size, op2 - 1, op2);
5965 : 0 : printf ("\n");
5966 : : }
5967 : : break;
5968 : :
5969 : 5 : case DW_RLE_start_length:
5970 [ - + ]: 5 : if (address_size == 4)
5971 : : {
5972 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 4)
5973 : 0 : goto invalid_range;
5974 [ # # ]: 0 : op1 = read_4ubyte_unaligned_inc (dbg, readp);
5975 : : }
5976 : : else
5977 : : {
5978 [ - + ]: 5 : if ((uint64_t) (nexthdr - readp) < 8)
5979 : 0 : goto invalid_range;
5980 [ - + ]: 10 : op1 = read_8ubyte_unaligned_inc (dbg, readp);
5981 : : }
5982 [ - + ]: 5 : if ((uint64_t) (nexthdr - readp) < 1)
5983 : 0 : goto invalid_range;
5984 : 5 : get_uleb128 (op2, readp, nexthdr);
5985 : 5 : printf (" 0x%" PRIx64 ", %" PRIx64 "\n", op1, op2);
5986 [ + + ]: 5 : if (! print_unresolved_addresses)
5987 : : {
5988 : 4 : op2 = op1 + op2;
5989 : 4 : printf (" ");
5990 : 4 : print_dwarf_addr (dwflmod, address_size, op1, op1);
5991 : 4 : printf ("..\n ");
5992 : 4 : print_dwarf_addr (dwflmod, address_size, op2 - 1, op2);
5993 [ + + ]: 23 : printf ("\n");
5994 : : }
5995 : : break;
5996 : :
5997 : 0 : default:
5998 : 0 : goto invalid_range;
5999 : : }
6000 : : }
6001 : :
6002 : 3 : next_table:
6003 [ - + ]: 3 : if (readp != nexthdr)
6004 : : {
6005 : 0 : size_t padding = nexthdr - readp;
6006 : 0 : printf (_(" %zu padding bytes\n\n"), padding);
6007 : 0 : readp = nexthdr;
6008 : : }
6009 : : }
6010 : : }
6011 : :
6012 : : /* Print content of DWARF .debug_ranges section. */
6013 : : static void
6014 : 30 : print_debug_ranges_section (Dwfl_Module *dwflmod,
6015 : : Ebl *ebl, GElf_Ehdr *ehdr,
6016 : : Elf_Scn *scn, GElf_Shdr *shdr,
6017 : : Dwarf *dbg)
6018 : : {
6019 : 60 : Elf_Data *data = (dbg->sectiondata[IDX_debug_ranges]
6020 [ - + ]: 30 : ?: elf_rawdata (scn, NULL));
6021 [ - + ]: 30 : if (unlikely (data == NULL))
6022 : : {
6023 : 0 : error (0, 0, _("cannot get .debug_ranges content: %s"),
6024 : : elf_errmsg (-1));
6025 : 0 : return;
6026 : : }
6027 : :
6028 : 30 : printf (_("\
6029 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
6030 : : elf_ndxscn (scn), section_name (ebl, shdr),
6031 : 30 : (uint64_t) shdr->sh_offset);
6032 : :
6033 : 30 : sort_listptr (&known_rangelistptr, "rangelistptr");
6034 : 30 : size_t listptr_idx = 0;
6035 : :
6036 [ + + ]: 30 : uint_fast8_t address_size = ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 4 : 8;
6037 : :
6038 : 30 : bool first = true;
6039 : 30 : Dwarf_Addr base = 0;
6040 : 30 : unsigned char *const endp = (unsigned char *) data->d_buf + data->d_size;
6041 : 30 : unsigned char *readp = data->d_buf;
6042 : 30 : Dwarf_CU *last_cu = NULL;
6043 [ + + ]: 63619 : while (readp < endp)
6044 : : {
6045 : 63589 : ptrdiff_t offset = readp - (unsigned char *) data->d_buf;
6046 : 63589 : Dwarf_CU *cu = last_cu;
6047 : :
6048 [ + + + + ]: 63589 : if (first && skip_listptr_hole (&known_rangelistptr, &listptr_idx,
6049 : : &address_size, NULL, &base, &cu,
6050 : : offset, &readp, endp, NULL))
6051 : 5 : continue;
6052 : :
6053 [ + + ]: 63584 : if (last_cu != cu)
6054 : : {
6055 : 945 : Dwarf_Die cudie;
6056 [ - + ]: 945 : if (dwarf_cu_die (cu, &cudie,
6057 : : NULL, NULL, NULL, NULL,
6058 : : NULL, NULL) == NULL)
6059 : 0 : printf (_("\n Unknown CU base: "));
6060 : : else
6061 : 945 : printf (_("\n CU [%6" PRIx64 "] base: "),
6062 : : dwarf_dieoffset (&cudie));
6063 : 945 : print_dwarf_addr (dwflmod, address_size, base, base);
6064 : 945 : printf ("\n");
6065 : : }
6066 : 63584 : last_cu = cu;
6067 : :
6068 [ - + ]: 63584 : if (unlikely (data->d_size - offset < (size_t) address_size * 2))
6069 : : {
6070 : 0 : printf (_(" [%6tx] <INVALID DATA>\n"), offset);
6071 : 0 : break;
6072 : : }
6073 : :
6074 : 63584 : Dwarf_Addr begin;
6075 : 63584 : Dwarf_Addr end;
6076 [ + + ]: 63584 : if (address_size == 8)
6077 : : {
6078 [ - + ]: 63576 : begin = read_8ubyte_unaligned_inc (dbg, readp);
6079 [ - + ]: 127152 : end = read_8ubyte_unaligned_inc (dbg, readp);
6080 : : }
6081 : : else
6082 : : {
6083 [ - + ]: 8 : begin = read_4ubyte_unaligned_inc (dbg, readp);
6084 [ - + ]: 8 : end = read_4ubyte_unaligned_inc (dbg, readp);
6085 [ + - ]: 8 : if (begin == (Dwarf_Addr) (uint32_t) -1)
6086 : : begin = (Dwarf_Addr) -1l;
6087 : : }
6088 : :
6089 [ - + ]: 63584 : if (begin == (Dwarf_Addr) -1l) /* Base address entry. */
6090 : : {
6091 [ # # ]: 0 : if (first)
6092 : 0 : printf (" [%6tx] ", offset);
6093 : : else
6094 : 0 : printf (" ");
6095 : 0 : puts (_("base address"));
6096 : 0 : printf (" ");
6097 : 0 : print_dwarf_addr (dwflmod, address_size, end, end);
6098 : 0 : printf ("\n");
6099 : 0 : base = end;
6100 : 0 : first = false;
6101 : : }
6102 [ + + ]: 63584 : else if (begin == 0 && end == 0) /* End of list entry. */
6103 : : {
6104 [ + + ]: 13691 : if (first)
6105 : 5 : printf (_(" [%6tx] empty list\n"), offset);
6106 : : first = true;
6107 : : }
6108 : : else
6109 : : {
6110 : : /* We have an address range entry. */
6111 [ + + ]: 49893 : if (first) /* First address range entry in a list. */
6112 : 13686 : printf (" [%6tx] ", offset);
6113 : : else
6114 : 36207 : printf (" ");
6115 : :
6116 : 49893 : printf ("range %" PRIx64 ", %" PRIx64 "\n", begin, end);
6117 [ + + ]: 49893 : if (! print_unresolved_addresses)
6118 : : {
6119 : 49883 : printf (" ");
6120 : 49883 : print_dwarf_addr (dwflmod, address_size, base + begin,
6121 : : base + begin);
6122 : 49883 : printf ("..\n ");
6123 : 49883 : print_dwarf_addr (dwflmod, address_size,
6124 : 49883 : base + end - 1, base + end);
6125 : 63584 : printf ("\n");
6126 : : }
6127 : :
6128 : : first = false;
6129 : : }
6130 : : }
6131 : : }
6132 : :
6133 : : #define REGNAMESZ 16
6134 : : static const char *
6135 : 171870 : register_info (Ebl *ebl, unsigned int regno, const Ebl_Register_Location *loc,
6136 : : char name[REGNAMESZ], int *bits, int *type)
6137 : : {
6138 : 171870 : const char *set;
6139 : 171870 : const char *pfx;
6140 : 171870 : int ignore;
6141 [ + + + + ]: 513914 : ssize_t n = ebl_register_info (ebl, regno, name, REGNAMESZ, &pfx, &set,
6142 : : bits ?: &ignore, type ?: &ignore);
6143 [ - + ]: 171870 : if (n <= 0)
6144 : : {
6145 [ # # ]: 0 : if (loc != NULL)
6146 : 0 : snprintf (name, REGNAMESZ, "reg%u", loc->regno);
6147 : : else
6148 : 0 : snprintf (name, REGNAMESZ, "??? 0x%x", regno);
6149 [ # # ]: 0 : if (bits != NULL)
6150 [ # # ]: 0 : *bits = loc != NULL ? loc->bits : 0;
6151 [ # # ]: 0 : if (type != NULL)
6152 : 0 : *type = DW_ATE_unsigned;
6153 : 0 : set = "??? unrecognized";
6154 : : }
6155 : : else
6156 : : {
6157 [ + + - + ]: 171870 : if (bits != NULL && *bits <= 0)
6158 [ # # ]: 0 : *bits = loc != NULL ? loc->bits : 0;
6159 [ + + - + ]: 171870 : if (type != NULL && *type == DW_ATE_void)
6160 : 0 : *type = DW_ATE_unsigned;
6161 : :
6162 : : }
6163 : 171870 : return set;
6164 : : }
6165 : :
6166 : : static const unsigned char *
6167 : 140 : read_encoded (unsigned int encoding, const unsigned char *readp,
6168 : : const unsigned char *const endp, uint64_t *res, Dwarf *dbg)
6169 : : {
6170 [ - + ]: 140 : if ((encoding & 0xf) == DW_EH_PE_absptr)
6171 : 0 : encoding = gelf_getclass (dbg->elf) == ELFCLASS32
6172 [ # # ]: 0 : ? DW_EH_PE_udata4 : DW_EH_PE_udata8;
6173 : :
6174 [ - - - + : 140 : switch (encoding & 0xf)
- - + -
- ]
6175 : : {
6176 : 0 : case DW_EH_PE_uleb128:
6177 : 0 : get_uleb128 (*res, readp, endp);
6178 : 0 : break;
6179 : 0 : case DW_EH_PE_sleb128:
6180 : 0 : get_sleb128 (*res, readp, endp);
6181 : 0 : break;
6182 : 0 : case DW_EH_PE_udata2:
6183 [ # # ]: 0 : if (readp + 2 > endp)
6184 : 0 : goto invalid;
6185 [ # # ]: 0 : *res = read_2ubyte_unaligned_inc (dbg, readp);
6186 : 0 : break;
6187 : 70 : case DW_EH_PE_udata4:
6188 [ - + ]: 70 : if (readp + 4 > endp)
6189 : 0 : goto invalid;
6190 [ - + ]: 70 : *res = read_4ubyte_unaligned_inc (dbg, readp);
6191 : 70 : break;
6192 : 0 : case DW_EH_PE_udata8:
6193 [ # # ]: 0 : if (readp + 8 > endp)
6194 : 0 : goto invalid;
6195 [ # # ]: 0 : *res = read_8ubyte_unaligned_inc (dbg, readp);
6196 : 0 : break;
6197 : 0 : case DW_EH_PE_sdata2:
6198 [ # # ]: 0 : if (readp + 2 > endp)
6199 : 0 : goto invalid;
6200 [ # # ]: 0 : *res = read_2sbyte_unaligned_inc (dbg, readp);
6201 : 0 : break;
6202 : 70 : case DW_EH_PE_sdata4:
6203 [ - + ]: 70 : if (readp + 4 > endp)
6204 : 0 : goto invalid;
6205 [ - + ]: 70 : *res = read_4sbyte_unaligned_inc (dbg, readp);
6206 : 70 : break;
6207 : 0 : case DW_EH_PE_sdata8:
6208 [ # # ]: 0 : if (readp + 8 > endp)
6209 : 0 : goto invalid;
6210 [ # # ]: 0 : *res = read_8sbyte_unaligned_inc (dbg, readp);
6211 : 0 : break;
6212 : : default:
6213 : 0 : invalid:
6214 : 0 : error (1, 0,
6215 : 0 : _("invalid encoding"));
6216 : : }
6217 : :
6218 : 140 : return readp;
6219 : : }
6220 : :
6221 : : static const char *
6222 : 171022 : regname (Ebl *ebl, unsigned int regno, char *regnamebuf)
6223 : : {
6224 : 171022 : register_info (ebl, regno, NULL, regnamebuf, NULL, NULL);
6225 : :
6226 : 171022 : return regnamebuf;
6227 : : }
6228 : :
6229 : : static void
6230 : 12739 : print_cfa_program (const unsigned char *readp, const unsigned char *const endp,
6231 : : Dwarf_Word vma_base, unsigned int code_align,
6232 : : int data_align,
6233 : : unsigned int version, unsigned int ptr_size,
6234 : : unsigned int encoding,
6235 : : Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr, Dwarf *dbg)
6236 : : {
6237 : 12739 : char regnamebuf[REGNAMESZ];
6238 : :
6239 : 12739 : puts ("\n Program:");
6240 : 12739 : Dwarf_Word pc = vma_base;
6241 : 12739 : while (readp < endp)
6242 : : {
6243 : 316899 : unsigned int opcode = *readp++;
6244 : :
6245 [ + + ]: 316899 : if (opcode < DW_CFA_advance_loc)
6246 : : /* Extended opcode. */
6247 [ + - + + : 80980 : switch (opcode)
+ + - + -
- + + + +
+ + - + -
- - - - -
+ - - ]
6248 : : {
6249 : 37132 : uint64_t op1;
6250 : 37132 : int64_t sop1;
6251 : 37132 : uint64_t op2;
6252 : 37132 : int64_t sop2;
6253 : :
6254 : 37132 : case DW_CFA_nop:
6255 : 37132 : puts (" nop");
6256 : 37132 : break;
6257 : 0 : case DW_CFA_set_loc:
6258 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
6259 : 0 : goto invalid;
6260 : 0 : readp = read_encoded (encoding, readp, endp, &op1, dbg);
6261 : 0 : printf (" set_loc %#" PRIx64 " to %#" PRIx64 "\n",
6262 : : op1, pc = vma_base + op1);
6263 : : break;
6264 : 1603 : case DW_CFA_advance_loc1:
6265 [ - + ]: 1603 : if ((uint64_t) (endp - readp) < 1)
6266 : 0 : goto invalid;
6267 : 3206 : printf (" advance_loc1 %u to %#" PRIx64 "\n",
6268 : 1603 : *readp, pc += *readp * code_align);
6269 : 1603 : ++readp;
6270 : 1603 : break;
6271 : 189 : case DW_CFA_advance_loc2:
6272 [ - + ]: 189 : if ((uint64_t) (endp - readp) < 2)
6273 : 0 : goto invalid;
6274 [ - + ]: 189 : op1 = read_2ubyte_unaligned_inc (dbg, readp);
6275 : 81169 : printf (" advance_loc2 %" PRIu64 " to %#" PRIx64 "\n",
6276 : 189 : op1, pc += op1 * code_align);
6277 : : break;
6278 : 24 : case DW_CFA_advance_loc4:
6279 [ - + ]: 24 : if ((uint64_t) (endp - readp) < 4)
6280 : 0 : goto invalid;
6281 [ + - ]: 24 : op1 = read_4ubyte_unaligned_inc (dbg, readp);
6282 : 81004 : printf (" advance_loc4 %" PRIu64 " to %#" PRIx64 "\n",
6283 : 24 : op1, pc += op1 * code_align);
6284 : : break;
6285 : 4 : case DW_CFA_offset_extended:
6286 [ - + ]: 4 : if ((uint64_t) (endp - readp) < 1)
6287 : 0 : goto invalid;
6288 : 4 : get_uleb128 (op1, readp, endp);
6289 [ - + ]: 4 : if ((uint64_t) (endp - readp) < 1)
6290 : 0 : goto invalid;
6291 : 4 : get_uleb128 (op2, readp, endp);
6292 : 4 : printf (" offset_extended r%" PRIu64 " (%s) at cfa%+" PRId64
6293 : : "\n",
6294 : : op1, regname (ebl, op1, regnamebuf), op2 * data_align);
6295 : : break;
6296 : 0 : case DW_CFA_restore_extended:
6297 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
6298 : 0 : goto invalid;
6299 : 0 : get_uleb128 (op1, readp, endp);
6300 : 0 : printf (" restore_extended r%" PRIu64 " (%s)\n",
6301 : : op1, regname (ebl, op1, regnamebuf));
6302 : : break;
6303 : 34 : case DW_CFA_undefined:
6304 [ - + ]: 34 : if ((uint64_t) (endp - readp) < 1)
6305 : 0 : goto invalid;
6306 : 34 : get_uleb128 (op1, readp, endp);
6307 : 34 : printf (" undefined r%" PRIu64 " (%s)\n", op1,
6308 : : regname (ebl, op1, regnamebuf));
6309 : : break;
6310 : 0 : case DW_CFA_same_value:
6311 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
6312 : 0 : goto invalid;
6313 : 0 : get_uleb128 (op1, readp, endp);
6314 : 0 : printf (" same_value r%" PRIu64 " (%s)\n", op1,
6315 : : regname (ebl, op1, regnamebuf));
6316 : : break;
6317 : 0 : case DW_CFA_register:
6318 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
6319 : 0 : goto invalid;
6320 : 0 : get_uleb128 (op1, readp, endp);
6321 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
6322 : 0 : goto invalid;
6323 : 0 : get_uleb128 (op2, readp, endp);
6324 : 0 : printf (" register r%" PRIu64 " (%s) in r%" PRIu64 " (%s)\n",
6325 : : op1, regname (ebl, op1, regnamebuf), op2,
6326 : : regname (ebl, op2, regnamebuf));
6327 : : break;
6328 : 10943 : case DW_CFA_remember_state:
6329 : 10943 : puts (" remember_state");
6330 : 10943 : break;
6331 : 10943 : case DW_CFA_restore_state:
6332 : 10943 : puts (" restore_state");
6333 : 10943 : break;
6334 : 179 : case DW_CFA_def_cfa:
6335 [ - + ]: 179 : if ((uint64_t) (endp - readp) < 1)
6336 : 0 : goto invalid;
6337 : 179 : get_uleb128 (op1, readp, endp);
6338 [ - + ]: 179 : if ((uint64_t) (endp - readp) < 1)
6339 : 0 : goto invalid;
6340 : 179 : get_uleb128 (op2, readp, endp);
6341 : 179 : printf (" def_cfa r%" PRIu64 " (%s) at offset %" PRIu64 "\n",
6342 : : op1, regname (ebl, op1, regnamebuf), op2);
6343 : : break;
6344 : 65 : case DW_CFA_def_cfa_register:
6345 [ - + ]: 65 : if ((uint64_t) (endp - readp) < 1)
6346 : 0 : goto invalid;
6347 : 65 : get_uleb128 (op1, readp, endp);
6348 : 65 : printf (" def_cfa_register r%" PRIu64 " (%s)\n",
6349 : : op1, regname (ebl, op1, regnamebuf));
6350 : : break;
6351 : 19808 : case DW_CFA_def_cfa_offset:
6352 [ - + ]: 19808 : if ((uint64_t) (endp - readp) < 1)
6353 : 0 : goto invalid;
6354 : 19808 : get_uleb128 (op1, readp, endp);
6355 : 19808 : printf (" def_cfa_offset %" PRIu64 "\n", op1);
6356 : : break;
6357 : 36 : case DW_CFA_def_cfa_expression:
6358 [ - + ]: 36 : if ((uint64_t) (endp - readp) < 1)
6359 : 0 : goto invalid;
6360 : 36 : get_uleb128 (op1, readp, endp); /* Length of DW_FORM_block. */
6361 : 36 : printf (" def_cfa_expression %" PRIu64 "\n", op1);
6362 [ - + ]: 36 : if ((uint64_t) (endp - readp) < op1)
6363 : : {
6364 : 0 : invalid:
6365 : 0 : fputs (_(" <INVALID DATA>\n"), stdout);
6366 : 0 : return;
6367 : : }
6368 : 36 : print_ops (dwflmod, dbg, 10, 10, version, ptr_size, 0, NULL,
6369 : : op1, readp);
6370 : 36 : readp += op1;
6371 : 36 : break;
6372 : 0 : case DW_CFA_expression:
6373 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
6374 : 0 : goto invalid;
6375 : 0 : get_uleb128 (op1, readp, endp);
6376 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
6377 : 0 : goto invalid;
6378 : 0 : get_uleb128 (op2, readp, endp); /* Length of DW_FORM_block. */
6379 : 0 : printf (" expression r%" PRIu64 " (%s) \n",
6380 : : op1, regname (ebl, op1, regnamebuf));
6381 [ # # ]: 0 : if ((uint64_t) (endp - readp) < op2)
6382 : 0 : goto invalid;
6383 : 0 : print_ops (dwflmod, dbg, 10, 10, version, ptr_size, 0, NULL,
6384 : : op2, readp);
6385 : 0 : readp += op2;
6386 : 0 : break;
6387 : 16 : case DW_CFA_offset_extended_sf:
6388 [ - + ]: 16 : if ((uint64_t) (endp - readp) < 1)
6389 : 0 : goto invalid;
6390 : 16 : get_uleb128 (op1, readp, endp);
6391 [ - + ]: 16 : if ((uint64_t) (endp - readp) < 1)
6392 : 0 : goto invalid;
6393 : 16 : get_sleb128 (sop2, readp, endp);
6394 : 16 : printf (" offset_extended_sf r%" PRIu64 " (%s) at cfa%+"
6395 : : PRId64 "\n",
6396 : : op1, regname (ebl, op1, regnamebuf), sop2 * data_align);
6397 : : break;
6398 : 0 : case DW_CFA_def_cfa_sf:
6399 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
6400 : 0 : goto invalid;
6401 : 0 : get_uleb128 (op1, readp, endp);
6402 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
6403 : 0 : goto invalid;
6404 : 0 : get_sleb128 (sop2, readp, endp);
6405 : 0 : printf (" def_cfa_sf r%" PRIu64 " (%s) at offset %" PRId64 "\n",
6406 : : op1, regname (ebl, op1, regnamebuf), sop2 * data_align);
6407 : : break;
6408 : 0 : case DW_CFA_def_cfa_offset_sf:
6409 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
6410 : 0 : goto invalid;
6411 : 0 : get_sleb128 (sop1, readp, endp);
6412 : 0 : printf (" def_cfa_offset_sf %" PRId64 "\n", sop1 * data_align);
6413 : : break;
6414 : 0 : case DW_CFA_val_offset:
6415 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
6416 : 0 : goto invalid;
6417 : 0 : get_uleb128 (op1, readp, endp);
6418 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
6419 : 0 : goto invalid;
6420 : 0 : get_uleb128 (op2, readp, endp);
6421 : 0 : printf (" val_offset %" PRIu64 " at offset %" PRIu64 "\n",
6422 : : op1, op2 * data_align);
6423 : : break;
6424 : 0 : case DW_CFA_val_offset_sf:
6425 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
6426 : 0 : goto invalid;
6427 : 0 : get_uleb128 (op1, readp, endp);
6428 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
6429 : 0 : goto invalid;
6430 : 0 : get_sleb128 (sop2, readp, endp);
6431 : 0 : printf (" val_offset_sf %" PRIu64 " at offset %" PRId64 "\n",
6432 : : op1, sop2 * data_align);
6433 : : break;
6434 : 0 : case DW_CFA_val_expression:
6435 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
6436 : 0 : goto invalid;
6437 : 0 : get_uleb128 (op1, readp, endp);
6438 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
6439 : 0 : goto invalid;
6440 : 0 : get_uleb128 (op2, readp, endp); /* Length of DW_FORM_block. */
6441 : 0 : printf (" val_expression r%" PRIu64 " (%s)\n",
6442 : : op1, regname (ebl, op1, regnamebuf));
6443 [ # # ]: 0 : if ((uint64_t) (endp - readp) < op2)
6444 : 0 : goto invalid;
6445 : 0 : print_ops (dwflmod, dbg, 10, 10, version, ptr_size, 0,
6446 : : NULL, op2, readp);
6447 : 0 : readp += op2;
6448 : 0 : break;
6449 : 0 : case DW_CFA_MIPS_advance_loc8:
6450 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 8)
6451 : 0 : goto invalid;
6452 [ # # ]: 0 : op1 = read_8ubyte_unaligned_inc (dbg, readp);
6453 : 80980 : printf (" MIPS_advance_loc8 %" PRIu64 " to %#" PRIx64 "\n",
6454 : 0 : op1, pc += op1 * code_align);
6455 : : break;
6456 : 4 : case DW_CFA_GNU_window_save: /* DW_CFA_AARCH64_negate_ra_state */
6457 [ + - ]: 4 : if (ehdr->e_machine == EM_AARCH64)
6458 : 4 : puts (" AARCH64_negate_ra_state");
6459 : : else
6460 : 0 : puts (" GNU_window_save");
6461 : : break;
6462 : 0 : case DW_CFA_GNU_args_size:
6463 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
6464 : 0 : goto invalid;
6465 : 0 : get_uleb128 (op1, readp, endp);
6466 : 0 : printf (" args_size %" PRIu64 "\n", op1);
6467 : : break;
6468 : : default:
6469 : 80980 : printf (" ??? (%u)\n", opcode);
6470 : : break;
6471 : : }
6472 [ + + ]: 235919 : else if (opcode < DW_CFA_offset)
6473 : 394833 : printf (" advance_loc %u to %#" PRIx64 "\n",
6474 : 65195 : opcode & 0x3f, pc += (opcode & 0x3f) * code_align);
6475 [ + + ]: 170724 : else if (opcode < DW_CFA_restore)
6476 : : {
6477 : 67632 : uint64_t offset;
6478 [ - + ]: 67632 : if ((uint64_t) (endp - readp) < 1)
6479 : 0 : goto invalid;
6480 : 67632 : get_uleb128 (offset, readp, endp);
6481 : 67632 : printf (" offset r%u (%s) at cfa%+" PRId64 "\n",
6482 : : opcode & 0x3f, regname (ebl, opcode & 0x3f, regnamebuf),
6483 : : offset * data_align);
6484 : : }
6485 : : else
6486 [ + + ]: 432730 : printf (" restore r%u (%s)\n",
6487 : : opcode & 0x3f, regname (ebl, opcode & 0x3f, regnamebuf));
6488 : : }
6489 : : }
6490 : :
6491 : :
6492 : : static unsigned int
6493 : 12595 : encoded_ptr_size (int encoding, unsigned int ptr_size)
6494 : : {
6495 [ - + - + ]: 12595 : switch (encoding & 7)
6496 : : {
6497 : : case DW_EH_PE_udata4:
6498 : : return 4;
6499 : 0 : case DW_EH_PE_udata8:
6500 : 0 : return 8;
6501 : 48 : case 0:
6502 : 48 : return ptr_size;
6503 : : }
6504 : :
6505 : 0 : fprintf (stderr, "Unsupported pointer encoding: %#x, "
6506 : : "assuming pointer size of %d.\n", encoding, ptr_size);
6507 : 0 : return ptr_size;
6508 : : }
6509 : :
6510 : :
6511 : : static unsigned int
6512 : 314 : print_encoding (unsigned int val)
6513 : : {
6514 [ - - - + : 314 : switch (val & 0xf)
- - - + -
- ]
6515 : : {
6516 : 0 : case DW_EH_PE_absptr:
6517 : 0 : fputs ("absptr", stdout);
6518 : 0 : break;
6519 : 0 : case DW_EH_PE_uleb128:
6520 : 0 : fputs ("uleb128", stdout);
6521 : 0 : break;
6522 : 0 : case DW_EH_PE_udata2:
6523 : 0 : fputs ("udata2", stdout);
6524 : 0 : break;
6525 : 70 : case DW_EH_PE_udata4:
6526 : 70 : fputs ("udata4", stdout);
6527 : 70 : break;
6528 : 0 : case DW_EH_PE_udata8:
6529 : 0 : fputs ("udata8", stdout);
6530 : 0 : break;
6531 : 0 : case DW_EH_PE_sleb128:
6532 : 0 : fputs ("sleb128", stdout);
6533 : 0 : break;
6534 : 0 : case DW_EH_PE_sdata2:
6535 : 0 : fputs ("sdata2", stdout);
6536 : 0 : break;
6537 : 244 : case DW_EH_PE_sdata4:
6538 : 244 : fputs ("sdata4", stdout);
6539 : 244 : break;
6540 : 0 : case DW_EH_PE_sdata8:
6541 : 0 : fputs ("sdata8", stdout);
6542 : 0 : break;
6543 : : default:
6544 : : /* We did not use any of the bits after all. */
6545 : : return val;
6546 : : }
6547 : :
6548 : 314 : return val & ~0xf;
6549 : : }
6550 : :
6551 : :
6552 : : static unsigned int
6553 : 244 : print_relinfo (unsigned int val)
6554 : : {
6555 [ + - + - : 244 : switch (val & 0x70)
- - ]
6556 : : {
6557 : 174 : case DW_EH_PE_pcrel:
6558 : 174 : fputs ("pcrel", stdout);
6559 : 174 : break;
6560 : 0 : case DW_EH_PE_textrel:
6561 : 0 : fputs ("textrel", stdout);
6562 : 0 : break;
6563 : 70 : case DW_EH_PE_datarel:
6564 : 70 : fputs ("datarel", stdout);
6565 : 70 : break;
6566 : 0 : case DW_EH_PE_funcrel:
6567 : 0 : fputs ("funcrel", stdout);
6568 : 0 : break;
6569 : 0 : case DW_EH_PE_aligned:
6570 : 0 : fputs ("aligned", stdout);
6571 : 0 : break;
6572 : : default:
6573 : : return val;
6574 : : }
6575 : :
6576 : 244 : return val & ~0x70;
6577 : : }
6578 : :
6579 : :
6580 : : static void
6581 : 314 : print_encoding_base (const char *pfx, unsigned int fde_encoding)
6582 : : {
6583 : 314 : printf ("(%s", pfx);
6584 : :
6585 [ - + ]: 314 : if (fde_encoding == DW_EH_PE_omit)
6586 : 0 : puts ("omit)");
6587 : : else
6588 : : {
6589 : 314 : unsigned int w = fde_encoding;
6590 : :
6591 : 314 : w = print_encoding (w);
6592 : :
6593 [ + + ]: 314 : if (w & 0x70)
6594 : : {
6595 [ + - ]: 244 : if (w != fde_encoding)
6596 [ + + ]: 244 : fputc_unlocked (' ', stdout);
6597 : :
6598 : 244 : w = print_relinfo (w);
6599 : : }
6600 : :
6601 [ - + ]: 314 : if (w != 0)
6602 [ # # ]: 0 : printf ("%s%x", w != fde_encoding ? " " : "", w);
6603 : :
6604 : 314 : puts (")");
6605 : : }
6606 : 314 : }
6607 : :
6608 : :
6609 : : static void
6610 : 106 : print_debug_frame_section (Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr,
6611 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
6612 : : {
6613 : 106 : size_t shstrndx;
6614 : : /* We know this call will succeed since it did in the caller. */
6615 : 106 : (void) elf_getshdrstrndx (ebl->elf, &shstrndx);
6616 : 106 : const char *scnname = elf_strptr (ebl->elf, shstrndx, shdr->sh_name);
6617 : :
6618 : : /* Needed if we find PC-relative addresses. */
6619 : 106 : GElf_Addr bias;
6620 [ - + ]: 106 : if (dwfl_module_getelf (dwflmod, &bias) == NULL)
6621 : : {
6622 : 0 : error (0, 0, _("cannot get ELF: %s"), dwfl_errmsg (-1));
6623 : 0 : return;
6624 : : }
6625 : :
6626 : 106 : bool is_eh_frame = strcmp (scnname, ".eh_frame") == 0;
6627 : 212 : Elf_Data *data = (is_eh_frame
6628 : 82 : ? elf_rawdata (scn, NULL)
6629 [ + + ]: 106 : : (dbg->sectiondata[IDX_debug_frame]
6630 [ - + ]: 24 : ?: elf_rawdata (scn, NULL)));
6631 : :
6632 [ - + ]: 106 : if (unlikely (data == NULL))
6633 : : {
6634 : 0 : error (0, 0, _("cannot get %s content: %s"),
6635 : : scnname, elf_errmsg (-1));
6636 : 0 : return;
6637 : : }
6638 : :
6639 [ + + ]: 106 : if (is_eh_frame)
6640 : 82 : printf (_("\
6641 : : \nCall frame information section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
6642 : 82 : elf_ndxscn (scn), scnname, (uint64_t) shdr->sh_offset);
6643 : : else
6644 : 24 : printf (_("\
6645 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
6646 : 24 : elf_ndxscn (scn), scnname, (uint64_t) shdr->sh_offset);
6647 : :
6648 : 106 : struct cieinfo
6649 : : {
6650 : : ptrdiff_t cie_offset;
6651 : : const char *augmentation;
6652 : : unsigned int code_alignment_factor;
6653 : : unsigned int data_alignment_factor;
6654 : : uint8_t address_size;
6655 : : uint8_t fde_encoding;
6656 : : uint8_t lsda_encoding;
6657 : : struct cieinfo *next;
6658 : 106 : } *cies = NULL;
6659 : :
6660 : 106 : const unsigned char *readp = data->d_buf;
6661 : 106 : const unsigned char *const dataend = ((unsigned char *) data->d_buf
6662 : 106 : + data->d_size);
6663 [ + + ]: 12915 : while (readp < dataend)
6664 : : {
6665 [ - + ]: 12809 : if (unlikely (readp + 4 > dataend))
6666 : : {
6667 : 0 : invalid_data:
6668 : 0 : error (0, 0, _("invalid data in section [%zu] '%s'"),
6669 : : elf_ndxscn (scn), scnname);
6670 : 0 : return;
6671 : : }
6672 : :
6673 : : /* At the beginning there must be a CIE. There can be multiple,
6674 : : hence we test tis in a loop. */
6675 : 12809 : ptrdiff_t offset = readp - (unsigned char *) data->d_buf;
6676 : :
6677 [ + + ]: 12809 : Dwarf_Word unit_length = read_4ubyte_unaligned_inc (dbg, readp);
6678 : 12809 : unsigned int length = 4;
6679 [ - + ]: 12809 : if (unlikely (unit_length == 0xffffffff))
6680 : : {
6681 [ # # ]: 0 : if (unlikely (readp + 8 > dataend))
6682 : 0 : goto invalid_data;
6683 : :
6684 [ # # ]: 0 : unit_length = read_8ubyte_unaligned_inc (dbg, readp);
6685 : 0 : length = 8;
6686 : : }
6687 : :
6688 [ + + ]: 12809 : if (unlikely (unit_length == 0))
6689 : : {
6690 : 70 : printf (_("\n [%6tx] Zero terminator\n"), offset);
6691 : 70 : continue;
6692 : : }
6693 : :
6694 : 12739 : Dwarf_Word maxsize = dataend - readp;
6695 [ - + ]: 12739 : if (unlikely (unit_length > maxsize))
6696 : 0 : goto invalid_data;
6697 : :
6698 [ + + ]: 12739 : unsigned int ptr_size = ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 4 : 8;
6699 : :
6700 : 12739 : ptrdiff_t start = readp - (unsigned char *) data->d_buf;
6701 : 12739 : const unsigned char *const cieend = readp + unit_length;
6702 [ - + ]: 12739 : if (unlikely (cieend > dataend))
6703 : 0 : goto invalid_data;
6704 : :
6705 : 12739 : Dwarf_Off cie_id;
6706 [ + - ]: 12739 : if (length == 4)
6707 : : {
6708 [ - + ]: 12739 : if (unlikely (cieend - readp < 4))
6709 : 0 : goto invalid_data;
6710 [ + + ]: 12739 : cie_id = read_4ubyte_unaligned_inc (dbg, readp);
6711 [ + + ]: 12739 : if (!is_eh_frame && cie_id == DW_CIE_ID_32)
6712 : 38 : cie_id = DW_CIE_ID_64;
6713 : : }
6714 : : else
6715 : : {
6716 [ # # ]: 0 : if (unlikely (cieend - readp < 8))
6717 : 0 : goto invalid_data;
6718 [ # # ]: 0 : cie_id = read_8ubyte_unaligned_inc (dbg, readp);
6719 : : }
6720 : :
6721 : 12739 : uint_fast8_t version = 2;
6722 : 12739 : unsigned int code_alignment_factor;
6723 : 12739 : int data_alignment_factor;
6724 : 12739 : unsigned int fde_encoding = 0;
6725 : 12739 : unsigned int lsda_encoding = 0;
6726 : 12739 : Dwarf_Word initial_location = 0;
6727 : 12739 : Dwarf_Word vma_base = 0;
6728 : :
6729 [ + + + + ]: 12819 : if (cie_id == (is_eh_frame ? 0 : DW_CIE_ID_64))
6730 : : {
6731 [ - + ]: 144 : if (unlikely (cieend - readp < 2))
6732 : 0 : goto invalid_data;
6733 : 144 : version = *readp++;
6734 : 144 : const char *const augmentation = (const char *) readp;
6735 : 144 : readp = memchr (readp, '\0', cieend - readp);
6736 [ - + ]: 144 : if (unlikely (readp == NULL))
6737 : 0 : goto invalid_data;
6738 : 144 : ++readp;
6739 : :
6740 : 144 : uint_fast8_t segment_size = 0;
6741 [ - + ]: 144 : if (version >= 4)
6742 : : {
6743 [ # # ]: 0 : if (cieend - readp < 5)
6744 : 0 : goto invalid_data;
6745 : 0 : ptr_size = *readp++;
6746 : 0 : segment_size = *readp++;
6747 : : }
6748 : :
6749 [ - + ]: 144 : if (cieend - readp < 1)
6750 : 0 : goto invalid_data;
6751 : 144 : get_uleb128 (code_alignment_factor, readp, cieend);
6752 [ - + ]: 144 : if (cieend - readp < 1)
6753 : 0 : goto invalid_data;
6754 : 144 : get_sleb128 (data_alignment_factor, readp, cieend);
6755 : :
6756 : : /* In some variant for unwind data there is another field. */
6757 [ - + ]: 144 : if (strcmp (augmentation, "eh") == 0)
6758 [ # # ]: 0 : readp += ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 4 : 8;
6759 : :
6760 : 144 : unsigned int return_address_register;
6761 [ - + ]: 144 : if (cieend - readp < 1)
6762 : 0 : goto invalid_data;
6763 [ + + ]: 144 : if (unlikely (version == 1))
6764 : 132 : return_address_register = *readp++;
6765 : : else
6766 : 12 : get_uleb128 (return_address_register, readp, cieend);
6767 : :
6768 : 144 : printf ("\n [%6tx] CIE length=%" PRIu64 "\n"
6769 : : " CIE_id: %" PRIu64 "\n"
6770 : : " version: %u\n"
6771 : : " augmentation: \"%s\"\n",
6772 : : offset, (uint64_t) unit_length, (uint64_t) cie_id,
6773 : : version, augmentation);
6774 [ - + ]: 144 : if (version >= 4)
6775 : 0 : printf (" address_size: %u\n"
6776 : : " segment_size: %u\n",
6777 : : ptr_size, segment_size);
6778 : 144 : printf (" code_alignment_factor: %u\n"
6779 : : " data_alignment_factor: %d\n"
6780 : : " return_address_register: %u\n",
6781 : : code_alignment_factor,
6782 : : data_alignment_factor, return_address_register);
6783 : :
6784 [ + + ]: 144 : if (augmentation[0] == 'z')
6785 : : {
6786 : 104 : unsigned int augmentationlen;
6787 : 104 : get_uleb128 (augmentationlen, readp, cieend);
6788 : :
6789 [ - + ]: 104 : if (augmentationlen > (size_t) (cieend - readp))
6790 : : {
6791 : 0 : error (0, 0, _("invalid augmentation length"));
6792 : 0 : readp = cieend;
6793 : 0 : continue;
6794 : : }
6795 : :
6796 : 104 : const char *hdr = "Augmentation data:";
6797 : 104 : const char *cp = augmentation + 1;
6798 [ + + + - ]: 208 : while (*cp != '\0' && cp < augmentation + augmentationlen + 1)
6799 : : {
6800 : 104 : printf (" %-26s%#x ", hdr, *readp);
6801 : 104 : hdr = "";
6802 : :
6803 [ + - ]: 104 : if (*cp == 'R')
6804 : : {
6805 : 104 : fde_encoding = *readp++;
6806 : 104 : print_encoding_base (_("FDE address encoding: "),
6807 : : fde_encoding);
6808 : : }
6809 [ # # ]: 0 : else if (*cp == 'L')
6810 : : {
6811 : 0 : lsda_encoding = *readp++;
6812 : 0 : print_encoding_base (_("LSDA pointer encoding: "),
6813 : : lsda_encoding);
6814 : : }
6815 [ # # ]: 0 : else if (*cp == 'P')
6816 : : {
6817 : : /* Personality. This field usually has a relocation
6818 : : attached pointing to __gcc_personality_v0. */
6819 : 0 : const unsigned char *startp = readp;
6820 : 0 : unsigned int encoding = *readp++;
6821 : 0 : uint64_t val = 0;
6822 : 0 : readp = read_encoded (encoding, readp,
6823 : 0 : readp - 1 + augmentationlen,
6824 : : &val, dbg);
6825 : :
6826 : 0 : while (++startp < readp)
6827 [ # # ]: 0 : printf ("%#x ", *startp);
6828 : :
6829 : 0 : putchar ('(');
6830 : 0 : print_encoding (encoding);
6831 : 0 : putchar (' ');
6832 [ # # ]: 0 : switch (encoding & 0xf)
6833 : : {
6834 : 0 : case DW_EH_PE_sleb128:
6835 : : case DW_EH_PE_sdata2:
6836 : : case DW_EH_PE_sdata4:
6837 : 0 : printf ("%" PRId64 ")\n", val);
6838 : : break;
6839 : 0 : default:
6840 : 0 : printf ("%#" PRIx64 ")\n", val);
6841 : : break;
6842 : : }
6843 : : }
6844 : : else
6845 : 0 : printf ("(%x)\n", *readp++);
6846 : :
6847 : 104 : ++cp;
6848 : : }
6849 : : }
6850 : :
6851 [ + - ]: 144 : if (likely (ptr_size == 4 || ptr_size == 8))
6852 : : {
6853 : 144 : struct cieinfo *newp = alloca (sizeof (*newp));
6854 : 144 : newp->cie_offset = offset;
6855 : 144 : newp->augmentation = augmentation;
6856 : 144 : newp->fde_encoding = fde_encoding;
6857 : 144 : newp->lsda_encoding = lsda_encoding;
6858 : 144 : newp->address_size = ptr_size;
6859 : 144 : newp->code_alignment_factor = code_alignment_factor;
6860 : 144 : newp->data_alignment_factor = data_alignment_factor;
6861 : 144 : newp->next = cies;
6862 : 144 : cies = newp;
6863 : : }
6864 : : }
6865 : : else
6866 : : {
6867 : : struct cieinfo *cie = cies;
6868 [ + - ]: 12595 : while (cie != NULL)
6869 [ + + - + ]: 25190 : if (is_eh_frame
6870 : 12553 : ? ((Dwarf_Off) start - cie_id) == (Dwarf_Off) cie->cie_offset
6871 : 42 : : cie_id == (Dwarf_Off) cie->cie_offset)
6872 : : break;
6873 : : else
6874 : 0 : cie = cie->next;
6875 [ - + ]: 12595 : if (unlikely (cie == NULL))
6876 : : {
6877 : 0 : puts ("invalid CIE reference in FDE");
6878 : 0 : return;
6879 : : }
6880 : :
6881 : : /* Initialize from CIE data. */
6882 : 12595 : fde_encoding = cie->fde_encoding;
6883 : 12595 : lsda_encoding = cie->lsda_encoding;
6884 : 12595 : ptr_size = encoded_ptr_size (fde_encoding, cie->address_size);
6885 : 12595 : code_alignment_factor = cie->code_alignment_factor;
6886 : 12595 : data_alignment_factor = cie->data_alignment_factor;
6887 : :
6888 : 12595 : const unsigned char *base = readp;
6889 : : // XXX There are sometimes relocations for this value
6890 [ - + + + : 25190 : initial_location = read_addr_unaligned_inc (ptr_size, dbg, readp);
+ + + + ]
6891 : 25190 : Dwarf_Word address_range
6892 [ + + + + : 25190 : = read_addr_unaligned_inc (ptr_size, dbg, readp);
+ + ]
6893 : :
6894 : : /* pcrel for an FDE address is relative to the runtime
6895 : : address of the start_address field itself. Sign extend
6896 : : if necessary to make sure the calculation is done on the
6897 : : full 64 bit address even when initial_location only holds
6898 : : the lower 32 bits. */
6899 : 12595 : Dwarf_Addr pc_start = initial_location;
6900 [ + + ]: 12595 : if (ptr_size == 4)
6901 : 12555 : pc_start = (uint64_t) (int32_t) pc_start;
6902 [ + + ]: 12595 : if ((fde_encoding & 0x70) == DW_EH_PE_pcrel)
6903 : 12547 : pc_start += ((uint64_t) shdr->sh_addr
6904 : 12547 : + (base - (const unsigned char *) data->d_buf)
6905 : 12547 : - bias);
6906 : :
6907 : 12595 : printf ("\n [%6tx] FDE length=%" PRIu64 " cie=[%6tx]\n"
6908 : : " CIE_pointer: %" PRIu64 "\n"
6909 : : " initial_location: ",
6910 : : offset, (uint64_t) unit_length,
6911 : : cie->cie_offset, (uint64_t) cie_id);
6912 : 12595 : print_dwarf_addr (dwflmod, cie->address_size,
6913 : : pc_start, initial_location);
6914 [ + + ]: 12595 : if ((fde_encoding & 0x70) == DW_EH_PE_pcrel)
6915 : : {
6916 : 25094 : vma_base = (((uint64_t) shdr->sh_offset
6917 : 12547 : + (base - (const unsigned char *) data->d_buf)
6918 : 12547 : + (uint64_t) initial_location)
6919 : : & (ptr_size == 4
6920 : : ? UINT64_C (0xffffffff)
6921 [ - + ]: 12547 : : UINT64_C (0xffffffffffffffff)));
6922 : 12547 : printf (_(" (offset: %#" PRIx64 ")"),
6923 : : (uint64_t) vma_base);
6924 : : }
6925 : :
6926 : 12595 : printf ("\n address_range: %#" PRIx64,
6927 : : (uint64_t) address_range);
6928 [ + + ]: 12595 : if ((fde_encoding & 0x70) == DW_EH_PE_pcrel)
6929 : 12547 : printf (_(" (end offset: %#" PRIx64 ")"),
6930 : 12547 : ((uint64_t) vma_base + (uint64_t) address_range)
6931 : : & (ptr_size == 4
6932 : : ? UINT64_C (0xffffffff)
6933 [ - + ]: 12547 : : UINT64_C (0xffffffffffffffff)));
6934 : 12595 : putchar ('\n');
6935 : :
6936 [ + + ]: 12595 : if (cie->augmentation[0] == 'z')
6937 : : {
6938 : 12547 : unsigned int augmentationlen;
6939 [ - + ]: 12547 : if (cieend - readp < 1)
6940 : 0 : goto invalid_data;
6941 : 12547 : get_uleb128 (augmentationlen, readp, cieend);
6942 : :
6943 [ - + ]: 12547 : if (augmentationlen > (size_t) (cieend - readp))
6944 : : {
6945 : 0 : error (0, 0, _("invalid augmentation length"));
6946 : 0 : readp = cieend;
6947 : 0 : continue;
6948 : : }
6949 : :
6950 [ - + ]: 12547 : if (augmentationlen > 0)
6951 : : {
6952 : 0 : const char *hdr = "Augmentation data:";
6953 : 0 : const char *cp = cie->augmentation + 1;
6954 : 0 : unsigned int u = 0;
6955 [ # # ]: 0 : while (*cp != '\0'
6956 [ # # ]: 0 : && cp < cie->augmentation + augmentationlen + 1)
6957 : : {
6958 [ # # ]: 0 : if (*cp == 'L')
6959 : : {
6960 : 0 : uint64_t lsda_pointer;
6961 : 0 : const unsigned char *p
6962 : 0 : = read_encoded (lsda_encoding, &readp[u],
6963 : : &readp[augmentationlen],
6964 : : &lsda_pointer, dbg);
6965 : 0 : u = p - readp;
6966 : 0 : printf (_("\
6967 : : %-26sLSDA pointer: %#" PRIx64 "\n"),
6968 : : hdr, lsda_pointer);
6969 : 0 : hdr = "";
6970 : : }
6971 : 0 : ++cp;
6972 : : }
6973 : :
6974 [ # # ]: 0 : while (u < augmentationlen)
6975 : : {
6976 : 0 : printf (" %-26s%#x\n", hdr, readp[u++]);
6977 : 0 : hdr = "";
6978 : : }
6979 : : }
6980 : :
6981 : 12547 : readp += augmentationlen;
6982 : : }
6983 : : }
6984 : :
6985 : : /* Handle the initialization instructions. */
6986 [ - + ]: 12739 : if (ptr_size != 4 && ptr_size !=8)
6987 : 0 : printf ("invalid CIE pointer size (%u), must be 4 or 8.\n", ptr_size);
6988 : : else
6989 : 12739 : print_cfa_program (readp, cieend, vma_base, code_alignment_factor,
6990 : : data_alignment_factor, version, ptr_size,
6991 : : fde_encoding, dwflmod, ebl, ehdr, dbg);
6992 : 12739 : readp = cieend;
6993 : : }
6994 : : }
6995 : :
6996 : :
6997 : : /* Returns the signedness (or false if it cannot be determined) and
6998 : : the byte size (or zero if it cannot be gotten) of the given DIE
6999 : : DW_AT_type attribute. Uses dwarf_peel_type and dwarf_aggregate_size. */
7000 : : static void
7001 : 159292 : die_type_sign_bytes (Dwarf_Die *die, bool *is_signed, int *bytes)
7002 : : {
7003 : 159292 : Dwarf_Attribute attr;
7004 : 159292 : Dwarf_Die type;
7005 : :
7006 : 159292 : *bytes = 0;
7007 : 159292 : *is_signed = false;
7008 : :
7009 [ + + ]: 159292 : if (dwarf_peel_type (dwarf_formref_die (dwarf_attr_integrate (die,
7010 : : DW_AT_type,
7011 : : &attr), &type),
7012 : : &type) == 0)
7013 : : {
7014 : 312 : Dwarf_Word val;
7015 : 312 : *is_signed = (dwarf_formudata (dwarf_attr (&type, DW_AT_encoding,
7016 : : &attr), &val) == 0
7017 [ + + + + ]: 312 : && (val == DW_ATE_signed || val == DW_ATE_signed_char));
7018 : :
7019 [ + - ]: 312 : if (dwarf_aggregate_size (&type, &val) == 0)
7020 : 312 : *bytes = val;
7021 : : }
7022 : 159292 : }
7023 : :
7024 : : struct attrcb_args
7025 : : {
7026 : : Dwfl_Module *dwflmod;
7027 : : Dwarf *dbg;
7028 : : Dwarf_Die *dies;
7029 : : int level;
7030 : : bool silent;
7031 : : bool is_split;
7032 : : unsigned int version;
7033 : : unsigned int addrsize;
7034 : : unsigned int offset_size;
7035 : : struct Dwarf_CU *cu;
7036 : : };
7037 : :
7038 : :
7039 : : static int
7040 : 3665458 : attr_callback (Dwarf_Attribute *attrp, void *arg)
7041 : : {
7042 : 3665458 : struct attrcb_args *cbargs = (struct attrcb_args *) arg;
7043 : 3665458 : const int level = cbargs->level;
7044 : 3665458 : Dwarf_Die *die = &cbargs->dies[level];
7045 : 3665458 : bool is_split = cbargs->is_split;
7046 : :
7047 [ + - ]: 3665458 : unsigned int attr = dwarf_whatattr (attrp);
7048 [ - + ]: 3665458 : if (unlikely (attr == 0))
7049 : : {
7050 [ # # ]: 0 : if (!cbargs->silent)
7051 : 0 : error (0, 0, _("DIE [%" PRIx64 "] "
7052 : : "cannot get attribute code: %s"),
7053 : : dwarf_dieoffset (die), dwarf_errmsg (-1));
7054 : 0 : return DWARF_CB_ABORT;
7055 : : }
7056 : :
7057 [ + - ]: 3665458 : unsigned int form = dwarf_whatform (attrp);
7058 [ - + ]: 3665458 : if (unlikely (form == 0))
7059 : : {
7060 [ # # ]: 0 : if (!cbargs->silent)
7061 : 0 : error (0, 0, _("DIE [%" PRIx64 "] "
7062 : : "cannot get attribute form: %s"),
7063 : : dwarf_dieoffset (die), dwarf_errmsg (-1));
7064 : 0 : return DWARF_CB_ABORT;
7065 : : }
7066 : :
7067 [ + + + + : 3665458 : switch (form)
+ + + +
- ]
7068 : : {
7069 : 65487 : case DW_FORM_addr:
7070 : : case DW_FORM_addrx:
7071 : : case DW_FORM_addrx1:
7072 : : case DW_FORM_addrx2:
7073 : : case DW_FORM_addrx3:
7074 : : case DW_FORM_addrx4:
7075 : : case DW_FORM_GNU_addr_index:
7076 [ + + ]: 65487 : if (!cbargs->silent)
7077 : : {
7078 : 65216 : Dwarf_Addr addr;
7079 [ - + ]: 65216 : if (unlikely (dwarf_formaddr (attrp, &addr) != 0))
7080 : : {
7081 : 0 : attrval_out:
7082 [ # # ]: 0 : if (!cbargs->silent)
7083 : 0 : error (0, 0, _("DIE [%" PRIx64 "] "
7084 : : "cannot get attribute '%s' (%s) value: "
7085 : : "%s"),
7086 : : dwarf_dieoffset (die),
7087 : : dwarf_attr_name (attr),
7088 : : dwarf_form_name (form),
7089 : : dwarf_errmsg (-1));
7090 : : /* Don't ABORT, it might be other attributes can be resolved. */
7091 : 0 : return DWARF_CB_OK;
7092 : : }
7093 [ + + ]: 65216 : if (form != DW_FORM_addr )
7094 : : {
7095 : 8 : Dwarf_Word word;
7096 [ - + ]: 8 : if (dwarf_formudata (attrp, &word) != 0)
7097 : 0 : goto attrval_out;
7098 : 8 : printf (" %*s%-20s (%s) [%" PRIx64 "] ",
7099 : : (int) (level * 2), "", dwarf_attr_name (attr),
7100 : : dwarf_form_name (form), word);
7101 : : }
7102 : : else
7103 : 65208 : printf (" %*s%-20s (%s) ",
7104 : : (int) (level * 2), "", dwarf_attr_name (attr),
7105 : : dwarf_form_name (form));
7106 : 65216 : print_dwarf_addr (cbargs->dwflmod, cbargs->addrsize, addr, addr);
7107 : 65216 : printf ("\n");
7108 : : }
7109 : 3289901 : break;
7110 : :
7111 : 614162 : case DW_FORM_indirect:
7112 : : case DW_FORM_strp:
7113 : : case DW_FORM_line_strp:
7114 : : case DW_FORM_strx:
7115 : : case DW_FORM_strx1:
7116 : : case DW_FORM_strx2:
7117 : : case DW_FORM_strx3:
7118 : : case DW_FORM_strx4:
7119 : : case DW_FORM_string:
7120 : : case DW_FORM_GNU_strp_alt:
7121 : : case DW_FORM_GNU_str_index:
7122 [ + + ]: 614162 : if (cbargs->silent)
7123 : : break;
7124 : 613407 : const char *str = dwarf_formstring (attrp);
7125 [ - + ]: 613407 : if (unlikely (str == NULL))
7126 : 0 : goto attrval_out;
7127 : 613407 : printf (" %*s%-20s (%s) \"%s\"\n",
7128 : : (int) (level * 2), "", dwarf_attr_name (attr),
7129 : : dwarf_form_name (form), str);
7130 : : break;
7131 : :
7132 : 763582 : case DW_FORM_ref_addr:
7133 : : case DW_FORM_ref_udata:
7134 : : case DW_FORM_ref8:
7135 : : case DW_FORM_ref4:
7136 : : case DW_FORM_ref2:
7137 : : case DW_FORM_ref1:
7138 : : case DW_FORM_GNU_ref_alt:
7139 : : case DW_FORM_ref_sup4:
7140 : : case DW_FORM_ref_sup8:
7141 [ + + ]: 763582 : if (cbargs->silent)
7142 : : break;
7143 : 762792 : Dwarf_Die ref;
7144 [ - + ]: 762792 : if (unlikely (dwarf_formref_die (attrp, &ref) == NULL))
7145 : 0 : goto attrval_out;
7146 : :
7147 : 762792 : printf (" %*s%-20s (%s) ",
7148 : : (int) (level * 2), "", dwarf_attr_name (attr),
7149 : : dwarf_form_name (form));
7150 [ + + ]: 762792 : if (is_split)
7151 : 33 : printf ("{%6" PRIxMAX "}\n", (uintmax_t) dwarf_dieoffset (&ref));
7152 : : else
7153 : 762759 : printf ("[%6" PRIxMAX "]\n", (uintmax_t) dwarf_dieoffset (&ref));
7154 : : break;
7155 : :
7156 : 4 : case DW_FORM_ref_sig8:
7157 [ + - ]: 4 : if (cbargs->silent)
7158 : : break;
7159 : 4 : printf (" %*s%-20s (%s) {%6" PRIx64 "}\n",
7160 : : (int) (level * 2), "", dwarf_attr_name (attr),
7161 : : dwarf_form_name (form),
7162 [ - + ]: 8 : (uint64_t) read_8ubyte_unaligned (attrp->cu->dbg, attrp->valp));
7163 : : break;
7164 : :
7165 : 2044279 : case DW_FORM_sec_offset:
7166 : : case DW_FORM_rnglistx:
7167 : : case DW_FORM_loclistx:
7168 : : case DW_FORM_implicit_const:
7169 : : case DW_FORM_udata:
7170 : : case DW_FORM_sdata:
7171 : : case DW_FORM_data8: /* Note no data16 here, we see that as block. */
7172 : : case DW_FORM_data4:
7173 : : case DW_FORM_data2:
7174 : 2044279 : case DW_FORM_data1:;
7175 : 2044279 : Dwarf_Word num;
7176 [ - + ]: 2044279 : if (unlikely (dwarf_formudata (attrp, &num) != 0))
7177 : 0 : goto attrval_out;
7178 : :
7179 : 2044279 : const char *valuestr = NULL;
7180 : 2044279 : bool as_hex_id = false;
7181 [ + + + + : 2044279 : switch (attr)
+ + - + +
- - - - -
- + - + +
+ ]
7182 : : {
7183 : : /* This case can take either a constant or a loclistptr. */
7184 : 251549 : case DW_AT_data_member_location:
7185 [ + - ]: 251549 : if (form != DW_FORM_sec_offset
7186 [ + + ]: 251549 : && (cbargs->version >= 4
7187 [ + - ]: 23806 : || (form != DW_FORM_data4 && form != DW_FORM_data8)))
7188 : : {
7189 [ + - ]: 251549 : if (!cbargs->silent)
7190 : 251549 : printf (" %*s%-20s (%s) %" PRIuMAX "\n",
7191 : : (int) (level * 2), "", dwarf_attr_name (attr),
7192 : : dwarf_form_name (form), (uintmax_t) num);
7193 : 251549 : return DWARF_CB_OK;
7194 : : }
7195 : 107583 : FALLTHROUGH;
7196 : :
7197 : : /* These cases always take a loclist[ptr] and no constant. */
7198 : : case DW_AT_location:
7199 : : case DW_AT_data_location:
7200 : : case DW_AT_vtable_elem_location:
7201 : : case DW_AT_string_length:
7202 : : case DW_AT_use_location:
7203 : : case DW_AT_frame_base:
7204 : : case DW_AT_return_addr:
7205 : : case DW_AT_static_link:
7206 : : case DW_AT_segment:
7207 : : case DW_AT_GNU_call_site_value:
7208 : : case DW_AT_GNU_call_site_data_value:
7209 : : case DW_AT_GNU_call_site_target:
7210 : : case DW_AT_GNU_call_site_target_clobbered:
7211 : : case DW_AT_GNU_locviews:
7212 : : {
7213 : 107583 : bool nlpt;
7214 [ + + ]: 107583 : if (cbargs->cu->version < 5)
7215 : : {
7216 [ + + ]: 107510 : if (! cbargs->is_split)
7217 : : {
7218 : 107490 : nlpt = notice_listptr (section_loc, &known_locsptr,
7219 : 107490 : cbargs->addrsize,
7220 : 107490 : cbargs->offset_size,
7221 : : cbargs->cu, num, attr);
7222 : : }
7223 : : else
7224 : : nlpt = true;
7225 : : }
7226 : : else
7227 : : {
7228 : : /* Only register for a real section offset. Otherwise
7229 : : it is a DW_FORM_loclistx which is just an index
7230 : : number and we should already have registered the
7231 : : section offset for the index when we saw the
7232 : : DW_AT_loclists_base CU attribute. */
7233 [ + + ]: 73 : if (form == DW_FORM_sec_offset)
7234 : 34 : nlpt = notice_listptr (section_loc, &known_loclistsptr,
7235 : 34 : cbargs->addrsize, cbargs->offset_size,
7236 : : cbargs->cu, num, attr);
7237 : : else
7238 : : nlpt = true;
7239 : :
7240 : : }
7241 : :
7242 [ + + ]: 107583 : if (!cbargs->silent)
7243 : : {
7244 [ + + - + ]: 107400 : if (cbargs->cu->version < 5 || form == DW_FORM_sec_offset)
7245 [ - + ]: 107393 : printf (" %*s%-20s (%s) location list [%6"
7246 : : PRIxMAX "]%s\n",
7247 : : (int) (level * 2), "", dwarf_attr_name (attr),
7248 : : dwarf_form_name (form), (uintmax_t) num,
7249 : : nlpt ? "" : " <WARNING offset too big>");
7250 : : else
7251 : 7 : printf (" %*s%-20s (%s) location index [%6"
7252 : : PRIxMAX "]\n",
7253 : : (int) (level * 2), "", dwarf_attr_name (attr),
7254 : : dwarf_form_name (form), (uintmax_t) num);
7255 : : }
7256 : : }
7257 : : return DWARF_CB_OK;
7258 : :
7259 : 5 : case DW_AT_loclists_base:
7260 : : {
7261 : 10 : bool nlpt = notice_listptr (section_loc, &known_loclistsptr,
7262 : 5 : cbargs->addrsize, cbargs->offset_size,
7263 : : cbargs->cu, num, attr);
7264 : :
7265 [ + + ]: 5 : if (!cbargs->silent)
7266 [ - + ]: 1 : printf (" %*s%-20s (%s) location list [%6" PRIxMAX "]%s\n",
7267 : : (int) (level * 2), "", dwarf_attr_name (attr),
7268 : : dwarf_form_name (form), (uintmax_t) num,
7269 : : nlpt ? "" : " <WARNING offset too big>");
7270 : : }
7271 : : return DWARF_CB_OK;
7272 : :
7273 : 16403 : case DW_AT_ranges:
7274 : : case DW_AT_start_scope:
7275 : : {
7276 : 16403 : bool nlpt;
7277 [ + + ]: 16403 : if (cbargs->cu->version < 5)
7278 : 16386 : nlpt = notice_listptr (section_ranges, &known_rangelistptr,
7279 : 16386 : cbargs->addrsize, cbargs->offset_size,
7280 : : cbargs->cu, num, attr);
7281 : : else
7282 : : {
7283 : : /* Only register for a real section offset. Otherwise
7284 : : it is a DW_FORM_rangelistx which is just an index
7285 : : number and we should already have registered the
7286 : : section offset for the index when we saw the
7287 : : DW_AT_rnglists_base CU attribute. */
7288 [ + + ]: 17 : if (form == DW_FORM_sec_offset)
7289 : 11 : nlpt = notice_listptr (section_ranges, &known_rnglistptr,
7290 : 11 : cbargs->addrsize, cbargs->offset_size,
7291 : : cbargs->cu, num, attr);
7292 : : else
7293 : : nlpt = true;
7294 : : }
7295 : :
7296 [ + + ]: 16403 : if (!cbargs->silent)
7297 : : {
7298 [ + + + + ]: 16360 : if (cbargs->cu->version < 5 || form == DW_FORM_sec_offset)
7299 [ - + ]: 16358 : printf (" %*s%-20s (%s) range list [%6"
7300 : : PRIxMAX "]%s\n",
7301 : : (int) (level * 2), "", dwarf_attr_name (attr),
7302 : : dwarf_form_name (form), (uintmax_t) num,
7303 : : nlpt ? "" : " <WARNING offset too big>");
7304 : : else
7305 : 2 : printf (" %*s%-20s (%s) range index [%6"
7306 : : PRIxMAX "]\n",
7307 : : (int) (level * 2), "", dwarf_attr_name (attr),
7308 : : dwarf_form_name (form), (uintmax_t) num);
7309 : : }
7310 : : }
7311 : : return DWARF_CB_OK;
7312 : :
7313 : 4 : case DW_AT_rnglists_base:
7314 : : {
7315 : 8 : bool nlpt = notice_listptr (section_ranges, &known_rnglistptr,
7316 : 4 : cbargs->addrsize, cbargs->offset_size,
7317 : : cbargs->cu, num, attr);
7318 [ + + ]: 4 : if (!cbargs->silent)
7319 [ - + ]: 2 : printf (" %*s%-20s (%s) range list [%6"
7320 : : PRIxMAX "]%s\n",
7321 : : (int) (level * 2), "", dwarf_attr_name (attr),
7322 : : dwarf_form_name (form), (uintmax_t) num,
7323 : : nlpt ? "" : " <WARNING offset too big>");
7324 : : }
7325 : : return DWARF_CB_OK;
7326 : :
7327 : 13 : case DW_AT_addr_base:
7328 : : case DW_AT_GNU_addr_base:
7329 : : {
7330 : 26 : bool addrbase = notice_listptr (section_addr, &known_addrbases,
7331 : 13 : cbargs->addrsize,
7332 : 13 : cbargs->offset_size,
7333 : : cbargs->cu, num, attr);
7334 [ + + ]: 13 : if (!cbargs->silent)
7335 [ - + ]: 5 : printf (" %*s%-20s (%s) address base [%6"
7336 : : PRIxMAX "]%s\n",
7337 : : (int) (level * 2), "", dwarf_attr_name (attr),
7338 : : dwarf_form_name (form), (uintmax_t) num,
7339 : : addrbase ? "" : " <WARNING offset too big>");
7340 : : }
7341 : : return DWARF_CB_OK;
7342 : :
7343 : 0 : case DW_AT_str_offsets_base:
7344 : : {
7345 : 0 : bool stroffbase = notice_listptr (section_str, &known_stroffbases,
7346 : 0 : cbargs->addrsize,
7347 : 0 : cbargs->offset_size,
7348 : : cbargs->cu, num, attr);
7349 [ # # ]: 0 : if (!cbargs->silent)
7350 [ - - ]: 375557 : printf (" %*s%-20s (%s) str offsets base [%6"
7351 : : PRIxMAX "]%s\n",
7352 : : (int) (level * 2), "", dwarf_attr_name (attr),
7353 : : dwarf_form_name (form), (uintmax_t) num,
7354 : : stroffbase ? "" : " <WARNING offset too big>");
7355 : : }
7356 : : return DWARF_CB_OK;
7357 : :
7358 : 1702 : case DW_AT_language:
7359 : 1702 : valuestr = dwarf_lang_name (num);
7360 : 1702 : break;
7361 : 30664 : case DW_AT_encoding:
7362 : 30664 : valuestr = dwarf_encoding_name (num);
7363 : 30664 : break;
7364 : 0 : case DW_AT_accessibility:
7365 : 0 : valuestr = dwarf_access_name (num);
7366 : 0 : break;
7367 : 0 : case DW_AT_defaulted:
7368 : 0 : valuestr = dwarf_defaulted_name (num);
7369 : 0 : break;
7370 : 0 : case DW_AT_visibility:
7371 : 0 : valuestr = dwarf_visibility_name (num);
7372 : 0 : break;
7373 : 0 : case DW_AT_virtuality:
7374 : 0 : valuestr = dwarf_virtuality_name (num);
7375 : 0 : break;
7376 : 0 : case DW_AT_identifier_case:
7377 : 0 : valuestr = dwarf_identifier_case_name (num);
7378 : 0 : break;
7379 : 0 : case DW_AT_calling_convention:
7380 : 0 : valuestr = dwarf_calling_convention_name (num);
7381 : 0 : break;
7382 : 3603 : case DW_AT_inline:
7383 : 3603 : valuestr = dwarf_inline_name (num);
7384 : 3603 : break;
7385 : 0 : case DW_AT_ordering:
7386 : 0 : valuestr = dwarf_ordering_name (num);
7387 : 0 : break;
7388 : 444878 : case DW_AT_decl_file:
7389 : : case DW_AT_call_file:
7390 : : {
7391 [ + + ]: 444878 : if (cbargs->silent)
7392 : : break;
7393 : :
7394 : : /* Try to get the actual file, the current interface only
7395 : : gives us full paths, but we only want to show the file
7396 : : name for now. */
7397 : 444454 : Dwarf_Die cudie;
7398 [ + - ]: 444454 : if (dwarf_cu_die (cbargs->cu, &cudie,
7399 : : NULL, NULL, NULL, NULL, NULL, NULL) != NULL)
7400 : : {
7401 : 444454 : Dwarf_Files *files;
7402 : 444454 : size_t nfiles;
7403 [ + - ]: 444454 : if (dwarf_getsrcfiles (&cudie, &files, &nfiles) == 0)
7404 : : {
7405 : 444454 : valuestr = dwarf_filesrc (files, num, NULL, NULL);
7406 [ + - ]: 444454 : if (valuestr != NULL)
7407 : : {
7408 : 444454 : char *filename = strrchr (valuestr, '/');
7409 [ + - ]: 444454 : if (filename != NULL)
7410 : 444454 : valuestr = filename + 1;
7411 : : }
7412 : : else
7413 : 0 : error (0, 0, _("invalid file (%" PRId64 "): %s"),
7414 : : num, dwarf_errmsg (-1));
7415 : : }
7416 : : else
7417 : 0 : error (0, 0, _("no srcfiles for CU [%" PRIx64 "]"),
7418 : : dwarf_dieoffset (&cudie));
7419 : : }
7420 : : else
7421 : 0 : error (0, 0, _("couldn't get DWARF CU: %s"),
7422 : : dwarf_errmsg (-1));
7423 [ - + ]: 444454 : if (valuestr == NULL)
7424 : : valuestr = "???";
7425 : : }
7426 : 444454 : break;
7427 : 13 : case DW_AT_GNU_dwo_id:
7428 : 13 : as_hex_id = true;
7429 : 13 : break;
7430 : :
7431 : : default:
7432 : : /* Nothing. */
7433 : : break;
7434 : : }
7435 : :
7436 [ + + ]: 1668722 : if (cbargs->silent)
7437 : : break;
7438 : :
7439 : : /* When highpc is in constant form it is relative to lowpc.
7440 : : In that case also show the address. */
7441 : 1666867 : Dwarf_Addr highpc;
7442 [ + + + - ]: 1666867 : if (attr == DW_AT_high_pc && dwarf_highpc (die, &highpc) == 0)
7443 : : {
7444 : 12352 : printf (" %*s%-20s (%s) %" PRIuMAX " (",
7445 : : (int) (level * 2), "", dwarf_attr_name (attr),
7446 : : dwarf_form_name (form), (uintmax_t) num);
7447 : 12352 : print_dwarf_addr (cbargs->dwflmod, cbargs->addrsize, highpc, highpc);
7448 : 12352 : printf (")\n");
7449 : : }
7450 : : else
7451 : : {
7452 [ + + ]: 1654515 : if (as_hex_id)
7453 : : {
7454 : 2 : printf (" %*s%-20s (%s) 0x%.16" PRIx64 "\n",
7455 : : (int) (level * 2), "", dwarf_attr_name (attr),
7456 : : dwarf_form_name (form), num);
7457 : : }
7458 : : else
7459 : : {
7460 : 1654513 : Dwarf_Sword snum = 0;
7461 : 1654513 : bool is_signed;
7462 : 1654513 : int bytes = 0;
7463 [ + + ]: 1654513 : if (attr == DW_AT_const_value)
7464 : 159288 : die_type_sign_bytes (die, &is_signed, &bytes);
7465 : : else
7466 : 1495225 : is_signed = (form == DW_FORM_sdata
7467 : 1495225 : || form == DW_FORM_implicit_const);
7468 : :
7469 [ + + ]: 1654513 : if (is_signed)
7470 [ - + ]: 140 : if (unlikely (dwarf_formsdata (attrp, &snum) != 0))
7471 : 0 : goto attrval_out;
7472 : :
7473 [ + + ]: 1654513 : if (valuestr == NULL)
7474 : : {
7475 : 1174395 : printf (" %*s%-20s (%s) ",
7476 : : (int) (level * 2), "", dwarf_attr_name (attr),
7477 : : dwarf_form_name (form));
7478 : : }
7479 : : else
7480 : : {
7481 : 480118 : printf (" %*s%-20s (%s) %s (",
7482 : : (int) (level * 2), "", dwarf_attr_name (attr),
7483 : : dwarf_form_name (form), valuestr);
7484 : : }
7485 : :
7486 [ + + + + : 1654513 : switch (bytes)
+ ]
7487 : : {
7488 : 2 : case 1:
7489 [ + + ]: 2 : if (is_signed)
7490 : 1 : printf ("%" PRId8, (int8_t) snum);
7491 : : else
7492 : 1 : printf ("%" PRIu8, (uint8_t) num);
7493 : : break;
7494 : :
7495 : 2 : case 2:
7496 [ + + ]: 2 : if (is_signed)
7497 : 1 : printf ("%" PRId16, (int16_t) snum);
7498 : : else
7499 : 1 : printf ("%" PRIu16, (uint16_t) num);
7500 : : break;
7501 : :
7502 : 123 : case 4:
7503 [ + + ]: 123 : if (is_signed)
7504 : 116 : printf ("%" PRId32, (int32_t) snum);
7505 : : else
7506 : 7 : printf ("%" PRIu32, (uint32_t) num);
7507 : : break;
7508 : :
7509 : 181 : case 8:
7510 [ + + ]: 181 : if (is_signed)
7511 : 1 : printf ("%" PRId64, (int64_t) snum);
7512 : : else
7513 : 180 : printf ("%" PRIu64, (uint64_t) num);
7514 : : break;
7515 : :
7516 : 1654205 : default:
7517 [ + + ]: 1654205 : if (is_signed)
7518 : 21 : printf ("%" PRIdMAX, (intmax_t) snum);
7519 : : else
7520 : 1654184 : printf ("%" PRIuMAX, (uintmax_t) num);
7521 : : break;
7522 : : }
7523 : :
7524 : : /* Make clear if we switched from a signed encoding to
7525 : : an unsigned value. */
7526 [ + + ]: 1654513 : if (attr == DW_AT_const_value
7527 [ + + ]: 159288 : && (form == DW_FORM_sdata || form == DW_FORM_implicit_const)
7528 [ + + ]: 1218 : && !is_signed)
7529 : 1212 : printf (" (%" PRIdMAX ")", (intmax_t) num);
7530 : :
7531 [ + + ]: 1654513 : if (valuestr == NULL)
7532 : 1174395 : printf ("\n");
7533 : : else
7534 : 1654513 : printf (")\n");
7535 : : }
7536 : : }
7537 : : break;
7538 : :
7539 : 9122 : case DW_FORM_flag:
7540 [ + + ]: 9122 : if (cbargs->silent)
7541 : : break;
7542 : 9048 : bool flag;
7543 [ - + ]: 9048 : if (unlikely (dwarf_formflag (attrp, &flag) != 0))
7544 : 0 : goto attrval_out;
7545 : :
7546 : 18096 : printf (" %*s%-20s (%s) %s\n",
7547 : : (int) (level * 2), "", dwarf_attr_name (attr),
7548 [ + - ]: 9048 : dwarf_form_name (form), flag ? yes_str : no_str);
7549 : : break;
7550 : :
7551 : 60101 : case DW_FORM_flag_present:
7552 [ + + ]: 60101 : if (cbargs->silent)
7553 : : break;
7554 : 59824 : printf (" %*s%-20s (%s) %s\n",
7555 : : (int) (level * 2), "", dwarf_attr_name (attr),
7556 : : dwarf_form_name (form), yes_str);
7557 : : break;
7558 : :
7559 : 108721 : case DW_FORM_exprloc:
7560 : : case DW_FORM_block4:
7561 : : case DW_FORM_block2:
7562 : : case DW_FORM_block1:
7563 : : case DW_FORM_block:
7564 : : case DW_FORM_data16: /* DWARF5 calls this a constant class. */
7565 [ + + ]: 108721 : if (cbargs->silent)
7566 : : break;
7567 : 108590 : Dwarf_Block block;
7568 [ - + ]: 108590 : if (unlikely (dwarf_formblock (attrp, &block) != 0))
7569 : 0 : goto attrval_out;
7570 : :
7571 : 108590 : printf (" %*s%-20s (%s) ",
7572 : : (int) (level * 2), "", dwarf_attr_name (attr),
7573 : : dwarf_form_name (form));
7574 : :
7575 [ - + + ]: 108590 : switch (attr)
7576 : : {
7577 : 0 : default:
7578 [ # # ]: 0 : if (form != DW_FORM_exprloc)
7579 : : {
7580 : 0 : print_block (block.length, block.data);
7581 : 0 : break;
7582 : : }
7583 : 108586 : FALLTHROUGH;
7584 : :
7585 : : case DW_AT_location:
7586 : : case DW_AT_data_location:
7587 : : case DW_AT_data_member_location:
7588 : : case DW_AT_vtable_elem_location:
7589 : : case DW_AT_string_length:
7590 : : case DW_AT_use_location:
7591 : : case DW_AT_frame_base:
7592 : : case DW_AT_return_addr:
7593 : : case DW_AT_static_link:
7594 : : case DW_AT_allocated:
7595 : : case DW_AT_associated:
7596 : : case DW_AT_bit_size:
7597 : : case DW_AT_bit_offset:
7598 : : case DW_AT_bit_stride:
7599 : : case DW_AT_byte_size:
7600 : : case DW_AT_byte_stride:
7601 : : case DW_AT_count:
7602 : : case DW_AT_lower_bound:
7603 : : case DW_AT_upper_bound:
7604 : : case DW_AT_GNU_call_site_value:
7605 : : case DW_AT_GNU_call_site_data_value:
7606 : : case DW_AT_GNU_call_site_target:
7607 : : case DW_AT_GNU_call_site_target_clobbered:
7608 [ + + ]: 108586 : if (form == DW_FORM_exprloc
7609 [ + - ]: 70 : || (form != DW_FORM_data16
7610 [ + - ]: 70 : && attrp->cu->version < 4)) /* blocks were expressions. */
7611 : : {
7612 : 108586 : putchar ('\n');
7613 : 108586 : print_ops (cbargs->dwflmod, cbargs->dbg,
7614 : 108586 : 12 + level * 2, 12 + level * 2,
7615 : : cbargs->version, cbargs->addrsize, cbargs->offset_size,
7616 : 108586 : attrp->cu, block.length, block.data);
7617 : : }
7618 : : else
7619 : 0 : print_block (block.length, block.data);
7620 : : break;
7621 : :
7622 : 4 : case DW_AT_discr_list:
7623 [ - + ]: 4 : if (block.length == 0)
7624 : 0 : puts ("<default>");
7625 [ + - ]: 4 : else if (form != DW_FORM_data16)
7626 : : {
7627 : 4 : const unsigned char *readp = block.data;
7628 : 4 : const unsigned char *readendp = readp + block.length;
7629 : :
7630 : : /* See if we are dealing with a signed or unsigned
7631 : : values. If the parent of this variant DIE is a
7632 : : variant_part then it will either have a discriminant
7633 : : which points to the member which type is the
7634 : : discriminant type. Or the variant_part itself has a
7635 : : type representing the discriminant. */
7636 : 4 : bool is_signed = false;
7637 [ + - ]: 4 : if (level > 0)
7638 : : {
7639 : 4 : Dwarf_Die *parent = &cbargs->dies[level - 1];
7640 [ + - ]: 4 : if (dwarf_tag (die) == DW_TAG_variant
7641 [ + - ]: 4 : && dwarf_tag (parent) == DW_TAG_variant_part)
7642 : : {
7643 : 4 : Dwarf_Die member;
7644 : 4 : Dwarf_Attribute discr_attr;
7645 : 4 : int bytes;
7646 [ + - ]: 4 : if (dwarf_formref_die (dwarf_attr (parent,
7647 : : DW_AT_discr,
7648 : : &discr_attr),
7649 : : &member) != NULL)
7650 : 4 : die_type_sign_bytes (&member, &is_signed, &bytes);
7651 : : else
7652 : 0 : die_type_sign_bytes (parent, &is_signed, &bytes);
7653 : : }
7654 : : }
7655 : 12 : while (readp < readendp)
7656 : : {
7657 : 8 : int d = (int) *readp++;
7658 : 8 : printf ("%s ", dwarf_discr_list_name (d));
7659 [ - + ]: 8 : if (readp >= readendp)
7660 : 0 : goto attrval_out;
7661 : :
7662 : 8 : Dwarf_Word val;
7663 : 8 : Dwarf_Sword sval;
7664 [ + + ]: 8 : if (d == DW_DSC_label)
7665 : : {
7666 [ + + ]: 4 : if (is_signed)
7667 : : {
7668 : 2 : get_sleb128 (sval, readp, readendp);
7669 : 2 : printf ("%" PRId64 "", sval);
7670 : : }
7671 : : else
7672 : : {
7673 : 2 : get_uleb128 (val, readp, readendp);
7674 : 2 : printf ("%" PRIu64 "", val);
7675 : : }
7676 : : }
7677 [ + - ]: 4 : else if (d == DW_DSC_range)
7678 : : {
7679 [ + + ]: 4 : if (is_signed)
7680 : : {
7681 : 3 : get_sleb128 (sval, readp, readendp);
7682 : 3 : printf ("%" PRId64 "..", sval);
7683 [ - + ]: 3 : if (readp >= readendp)
7684 : 0 : goto attrval_out;
7685 : 3 : get_sleb128 (sval, readp, readendp);
7686 : 3 : printf ("%" PRId64 "", sval);
7687 : : }
7688 : : else
7689 : : {
7690 : 1 : get_uleb128 (val, readp, readendp);
7691 : 1 : printf ("%" PRIu64 "..", val);
7692 [ - + ]: 1 : if (readp >= readendp)
7693 : 0 : goto attrval_out;
7694 : 1 : get_uleb128 (val, readp, readendp);
7695 : 1 : printf ("%" PRIu64 "", val);
7696 : : }
7697 : : }
7698 : : else
7699 : : {
7700 : 0 : print_block (readendp - readp, readp);
7701 : 0 : break;
7702 : : }
7703 [ + + ]: 8 : if (readp < readendp)
7704 [ + + ]: 16 : printf (", ");
7705 : : }
7706 : 4 : putchar ('\n');
7707 : : }
7708 : : else
7709 : 0 : print_block (block.length, block.data);
7710 : : break;
7711 : : }
7712 : : break;
7713 : :
7714 : 0 : default:
7715 [ # # ]: 0 : if (cbargs->silent)
7716 : : break;
7717 : 0 : printf (" %*s%-20s (%s) ???\n",
7718 : : (int) (level * 2), "", dwarf_attr_name (attr),
7719 : : dwarf_form_name (form));
7720 : : break;
7721 : : }
7722 : :
7723 : 3289901 : return DWARF_CB_OK;
7724 : : }
7725 : :
7726 : : static void
7727 : 99 : print_debug_units (Dwfl_Module *dwflmod,
7728 : : Ebl *ebl, GElf_Ehdr *ehdr __attribute__ ((unused)),
7729 : : Elf_Scn *scn, GElf_Shdr *shdr,
7730 : : Dwarf *dbg, bool debug_types)
7731 : : {
7732 [ + + + + ]: 99 : const bool silent = !(print_debug_sections & section_info) && !debug_types;
7733 : 99 : const char *secname = section_name (ebl, shdr);
7734 : :
7735 [ + + ]: 99 : if (!silent)
7736 : 63 : printf (_("\
7737 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n [Offset]\n"),
7738 : 63 : elf_ndxscn (scn), secname, (uint64_t) shdr->sh_offset);
7739 : :
7740 : : /* If the section is empty we don't have to do anything. */
7741 [ + + - + ]: 99 : if (!silent && shdr->sh_size == 0)
7742 : 0 : return;
7743 : :
7744 : 99 : int maxdies = 20;
7745 : 99 : Dwarf_Die *dies = xmalloc (maxdies * sizeof (Dwarf_Die));
7746 : :
7747 : : /* New compilation unit. */
7748 : 99 : Dwarf_Half version;
7749 : :
7750 : 99 : Dwarf_Die result;
7751 : 99 : Dwarf_Off abbroffset;
7752 : 99 : uint8_t addrsize;
7753 : 99 : uint8_t offsize;
7754 : 99 : uint64_t unit_id;
7755 : 99 : Dwarf_Off subdie_off;
7756 : :
7757 : 99 : int unit_res;
7758 : 99 : Dwarf_CU *cu;
7759 : 99 : Dwarf_CU cu_mem;
7760 : 99 : uint8_t unit_type;
7761 : 99 : Dwarf_Die cudie;
7762 : :
7763 : : /* We cheat a little because we want to see only the CUs from .debug_info
7764 : : or .debug_types. We know the Dwarf_CU struct layout. Set it up at
7765 : : the end of .debug_info if we want .debug_types only. Check the returned
7766 : : Dwarf_CU is still in the expected section. */
7767 [ + + ]: 99 : if (debug_types)
7768 : : {
7769 : 1 : cu_mem.dbg = dbg;
7770 : 1 : cu_mem.end = dbg->sectiondata[IDX_debug_info]->d_size;
7771 : 1 : cu_mem.sec_idx = IDX_debug_info;
7772 : 1 : cu = &cu_mem;
7773 : : }
7774 : : else
7775 : 98 : cu = NULL;
7776 : :
7777 : : next_cu:
7778 : 1810 : unit_res = dwarf_get_units (dbg, cu, &cu, &version, &unit_type,
7779 : : &cudie, NULL);
7780 [ + + ]: 1810 : if (unit_res == 1)
7781 : 98 : goto do_return;
7782 : :
7783 [ - + ]: 1712 : if (unit_res == -1)
7784 : : {
7785 [ # # ]: 0 : if (!silent)
7786 : 0 : error (0, 0, _("cannot get next unit: %s"), dwarf_errmsg (-1));
7787 : 0 : goto do_return;
7788 : : }
7789 : :
7790 [ + + + + ]: 3422 : if (cu->sec_idx != (size_t) (debug_types ? IDX_debug_types : IDX_debug_info))
7791 : 1 : goto do_return;
7792 : :
7793 : 1711 : dwarf_cu_die (cu, &result, NULL, &abbroffset, &addrsize, &offsize,
7794 : : &unit_id, &subdie_off);
7795 : :
7796 [ + + ]: 1711 : if (!silent)
7797 : : {
7798 : 1661 : Dwarf_Off offset = cu->start;
7799 [ + + + - ]: 1661 : if (debug_types && version < 5)
7800 : 2 : {
7801 : 2 : Dwarf_Die typedie;
7802 : 2 : Dwarf_Off dieoffset;
7803 : 2 : dieoffset = dwarf_dieoffset (dwarf_offdie_types (dbg, cu->start
7804 : : + subdie_off,
7805 : : &typedie));
7806 : 2 : printf (_(" Type unit at offset %" PRIu64 ":\n"
7807 : : " Version: %" PRIu16
7808 : : ", Abbreviation section offset: %" PRIu64
7809 : : ", Address size: %" PRIu8
7810 : : ", Offset size: %" PRIu8
7811 : : "\n Type signature: %#" PRIx64
7812 : : ", Type offset: %#" PRIx64 " [%" PRIx64 "]\n"),
7813 : : (uint64_t) offset, version, abbroffset, addrsize, offsize,
7814 : : unit_id, (uint64_t) subdie_off, dieoffset);
7815 : : }
7816 : : else
7817 : : {
7818 : 1659 : printf (_(" Compilation unit at offset %" PRIu64 ":\n"
7819 : : " Version: %" PRIu16
7820 : : ", Abbreviation section offset: %" PRIu64
7821 : : ", Address size: %" PRIu8
7822 : : ", Offset size: %" PRIu8 "\n"),
7823 : : (uint64_t) offset, version, abbroffset, addrsize, offsize);
7824 : :
7825 [ + + ]: 1659 : if (version >= 5 || (unit_type != DW_UT_compile
7826 [ + + ]: 1654 : && unit_type != DW_UT_partial))
7827 : : {
7828 : 6 : printf (_(" Unit type: %s (%" PRIu8 ")"),
7829 : : dwarf_unit_name (unit_type), unit_type);
7830 : 6 : if (unit_type == DW_UT_type
7831 [ + + ]: 6 : || unit_type == DW_UT_skeleton
7832 [ + - ]: 1 : || unit_type == DW_UT_split_compile
7833 [ - + ]: 1 : || unit_type == DW_UT_split_type)
7834 : 5 : printf (", Unit id: 0x%.16" PRIx64 "", unit_id);
7835 : 6 : if (unit_type == DW_UT_type
7836 [ - + ]: 6 : || unit_type == DW_UT_split_type)
7837 : : {
7838 : 0 : Dwarf_Die typedie;
7839 : 0 : Dwarf_Off dieoffset;
7840 : 0 : dwarf_cu_info (cu, NULL, NULL, NULL, &typedie,
7841 : : NULL, NULL, NULL);
7842 : 0 : dieoffset = dwarf_dieoffset (&typedie);
7843 : 0 : printf (", Unit DIE off: %#" PRIx64 " [%" PRIx64 "]",
7844 : : subdie_off, dieoffset);
7845 : : }
7846 : 6 : printf ("\n");
7847 : : }
7848 : : }
7849 : : }
7850 : :
7851 [ + - ]: 1711 : if (version < 2 || version > 5
7852 [ + - - + ]: 1711 : || unit_type < DW_UT_compile || unit_type > DW_UT_split_type)
7853 : : {
7854 [ # # ]: 0 : if (!silent)
7855 : 0 : error (0, 0, _("unknown version (%d) or unit type (%d)"),
7856 : : version, unit_type);
7857 : 0 : goto next_cu;
7858 : : }
7859 : :
7860 : 1711 : struct attrcb_args args =
7861 : : {
7862 : : .dwflmod = dwflmod,
7863 : : .silent = silent,
7864 : : .version = version,
7865 : : .addrsize = addrsize,
7866 : : .offset_size = offsize
7867 : : };
7868 : :
7869 : 1711 : bool is_split = false;
7870 : 1711 : int level = 0;
7871 : 1711 : dies[0] = cudie;
7872 : 1711 : args.cu = dies[0].cu;
7873 : 1711 : args.dbg = dbg;
7874 : 1711 : args.is_split = is_split;
7875 : :
7876 : : /* We might return here again for the split CU subdie. */
7877 : : do_cu:
7878 : 954790 : do
7879 : : {
7880 : 954790 : Dwarf_Off offset = dwarf_dieoffset (&dies[level]);
7881 [ - + ]: 954790 : if (unlikely (offset == (Dwarf_Off) -1))
7882 : : {
7883 [ # # ]: 0 : if (!silent)
7884 : 0 : error (0, 0, _("cannot get DIE offset: %s"),
7885 : : dwarf_errmsg (-1));
7886 : 0 : goto do_return;
7887 : : }
7888 : :
7889 : 954790 : int tag = dwarf_tag (&dies[level]);
7890 [ - + ]: 954790 : if (unlikely (tag == DW_TAG_invalid))
7891 : : {
7892 [ # # ]: 0 : if (!silent)
7893 : 0 : error (0, 0, _("cannot get tag of DIE at offset [%" PRIx64
7894 : : "] in section '%s': %s"),
7895 : : (uint64_t) offset, secname, dwarf_errmsg (-1));
7896 : 0 : goto do_return;
7897 : : }
7898 : :
7899 [ + + ]: 954790 : if (!silent)
7900 : : {
7901 : 953797 : unsigned int code = dwarf_getabbrevcode (dies[level].abbrev);
7902 [ + + ]: 953797 : if (is_split)
7903 : 48 : printf (" {%6" PRIx64 "} ", (uint64_t) offset);
7904 : : else
7905 : 953749 : printf (" [%6" PRIx64 "] ", (uint64_t) offset);
7906 : 953797 : printf ("%*s%-20s abbrev: %u\n", (int) (level * 2), "",
7907 : : dwarf_tag_name (tag), code);
7908 : : }
7909 : :
7910 : : /* Print the attribute values. */
7911 : 954790 : args.level = level;
7912 : 954790 : args.dies = dies;
7913 : 954790 : (void) dwarf_getattrs (&dies[level], attr_callback, &args, 0);
7914 : :
7915 : : /* Make room for the next level's DIE. */
7916 [ - + ]: 954790 : if (level + 1 == maxdies)
7917 : 0 : dies = xrealloc (dies, (maxdies += 10) * sizeof (Dwarf_Die));
7918 : :
7919 : 954790 : int res = dwarf_child (&dies[level], &dies[level + 1]);
7920 [ + + ]: 954790 : if (res > 0)
7921 : : {
7922 [ + + ]: 954790 : while ((res = dwarf_siblingof (&dies[level], &dies[level])) == 1)
7923 [ + + ]: 127061 : if (level-- == 0)
7924 : : break;
7925 : :
7926 [ - + ]: 829444 : if (unlikely (res == -1))
7927 : : {
7928 [ # # ]: 0 : if (!silent)
7929 : 0 : error (0, 0, _("cannot get next DIE: %s\n"),
7930 : : dwarf_errmsg (-1));
7931 : 0 : goto do_return;
7932 : : }
7933 : : }
7934 [ - + ]: 125346 : else if (unlikely (res < 0))
7935 : : {
7936 [ # # ]: 0 : if (!silent)
7937 : 0 : error (0, 0, _("cannot get next DIE: %s"),
7938 : : dwarf_errmsg (-1));
7939 : 0 : goto do_return;
7940 : : }
7941 : : else
7942 : : ++level;
7943 : : }
7944 [ + + ]: 954790 : while (level >= 0);
7945 : :
7946 : : /* We might want to show the split compile unit if this was a skeleton.
7947 : : We need to scan it if we are requesting printing .debug_ranges for
7948 : : DWARF4 since GNU DebugFission uses "offsets" into the main ranges
7949 : : section. */
7950 [ + + ]: 1715 : if (unit_type == DW_UT_skeleton
7951 [ + + + + ]: 13 : && ((!silent && show_split_units)
7952 [ + + + + ]: 10 : || (version < 5 && (print_debug_sections & section_ranges) != 0)))
7953 : : {
7954 : 5 : Dwarf_Die subdie;
7955 [ + - ]: 5 : if (dwarf_cu_info (cu, NULL, NULL, NULL, &subdie, NULL, NULL, NULL) != 0
7956 [ + + ]: 5 : || dwarf_tag (&subdie) == DW_TAG_invalid)
7957 : : {
7958 [ + - ]: 1 : if (!silent)
7959 : : {
7960 : 1 : Dwarf_Attribute dwo_at;
7961 : 2 : const char *dwo_name =
7962 : 1 : (dwarf_formstring (dwarf_attr (&cudie, DW_AT_dwo_name,
7963 : : &dwo_at))
7964 [ - + ]: 1 : ?: (dwarf_formstring (dwarf_attr (&cudie, DW_AT_GNU_dwo_name,
7965 : : &dwo_at))
7966 [ # # ]: 0 : ?: "<unknown>"));
7967 : 1 : fprintf (stderr,
7968 : : "Could not find split unit '%s', id: %" PRIx64 "\n",
7969 : : dwo_name, unit_id);
7970 : : }
7971 : : }
7972 : : else
7973 : : {
7974 : 4 : Dwarf_CU *split_cu = subdie.cu;
7975 : 4 : dwarf_cu_die (split_cu, &result, NULL, &abbroffset,
7976 : : &addrsize, &offsize, &unit_id, &subdie_off);
7977 : 4 : Dwarf_Off offset = cu->start;
7978 : :
7979 [ + + ]: 4 : if (!silent)
7980 : : {
7981 : 2 : printf (_(" Split compilation unit at offset %"
7982 : : PRIu64 ":\n"
7983 : : " Version: %" PRIu16
7984 : : ", Abbreviation section offset: %" PRIu64
7985 : : ", Address size: %" PRIu8
7986 : : ", Offset size: %" PRIu8 "\n"),
7987 : : (uint64_t) offset, version, abbroffset,
7988 : : addrsize, offsize);
7989 : 2 : printf (_(" Unit type: %s (%" PRIu8 ")"),
7990 : : dwarf_unit_name (unit_type), unit_type);
7991 : 2 : printf (", Unit id: 0x%.16" PRIx64 "", unit_id);
7992 : 2 : printf ("\n");
7993 : : }
7994 : :
7995 : 4 : unit_type = DW_UT_split_compile;
7996 : 4 : is_split = true;
7997 : 4 : level = 0;
7998 : 4 : dies[0] = subdie;
7999 : 4 : args.cu = dies[0].cu;
8000 : 4 : args.dbg = split_cu->dbg;
8001 : 4 : args.is_split = is_split;
8002 : 4 : goto do_cu;
8003 : : }
8004 : : }
8005 : :
8006 : : /* And again... */
8007 : 1711 : goto next_cu;
8008 : :
8009 : 99 : do_return:
8010 : 99 : free (dies);
8011 : : }
8012 : :
8013 : : static void
8014 : 98 : print_debug_info_section (Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr,
8015 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
8016 : : {
8017 : 20 : print_debug_units (dwflmod, ebl, ehdr, scn, shdr, dbg, false);
8018 : 20 : }
8019 : :
8020 : : static void
8021 : 1 : print_debug_types_section (Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr,
8022 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
8023 : : {
8024 : 1 : print_debug_units (dwflmod, ebl, ehdr, scn, shdr, dbg, true);
8025 : 1 : }
8026 : :
8027 : :
8028 : : static void
8029 : 39 : print_decoded_line_section (Dwfl_Module *dwflmod, Ebl *ebl,
8030 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
8031 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
8032 : : {
8033 : 39 : printf (_("\
8034 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n\n"),
8035 : : elf_ndxscn (scn), section_name (ebl, shdr),
8036 : 39 : (uint64_t) shdr->sh_offset);
8037 : :
8038 : 78 : size_t address_size
8039 [ + + ]: 39 : = elf_getident (ebl->elf, NULL)[EI_CLASS] == ELFCLASS32 ? 4 : 8;
8040 : :
8041 : 39 : Dwarf_Lines *lines;
8042 : 39 : size_t nlines;
8043 : 39 : Dwarf_Off off, next_off = 0;
8044 : 39 : Dwarf_CU *cu = NULL;
8045 [ + + ]: 84 : while (dwarf_next_lines (dbg, off = next_off, &next_off, &cu, NULL, NULL,
8046 : : &lines, &nlines) == 0)
8047 : : {
8048 : 45 : Dwarf_Die cudie;
8049 [ + + + - ]: 45 : if (cu != NULL && dwarf_cu_info (cu, NULL, NULL, &cudie,
8050 : : NULL, NULL, NULL, NULL) == 0)
8051 : 43 : printf (" CU [%" PRIx64 "] %s\n",
8052 : : dwarf_dieoffset (&cudie), dwarf_diename (&cudie));
8053 : : else
8054 : : {
8055 : : /* DWARF5 lines can be independent of any CU, but they probably
8056 : : are used by some CU. Determine the CU this block is for. */
8057 : 2 : Dwarf_Off cuoffset;
8058 : 2 : Dwarf_Off ncuoffset = 0;
8059 : 2 : size_t hsize;
8060 [ + - ]: 3 : while (dwarf_nextcu (dbg, cuoffset = ncuoffset, &ncuoffset, &hsize,
8061 : : NULL, NULL, NULL) == 0)
8062 : : {
8063 [ - + ]: 3 : if (dwarf_offdie (dbg, cuoffset + hsize, &cudie) == NULL)
8064 : 0 : continue;
8065 : 3 : Dwarf_Attribute stmt_list;
8066 [ - + ]: 3 : if (dwarf_attr (&cudie, DW_AT_stmt_list, &stmt_list) == NULL)
8067 : 0 : continue;
8068 : 3 : Dwarf_Word lineoff;
8069 [ - + ]: 3 : if (dwarf_formudata (&stmt_list, &lineoff) != 0)
8070 : 0 : continue;
8071 [ + + ]: 3 : if (lineoff == off)
8072 : : {
8073 : : /* Found the CU. */
8074 : 2 : cu = cudie.cu;
8075 : 2 : break;
8076 : : }
8077 : : }
8078 : :
8079 [ + - ]: 2 : if (cu != NULL)
8080 : 2 : printf (" CU [%" PRIx64 "] %s\n",
8081 : : dwarf_dieoffset (&cudie), dwarf_diename (&cudie));
8082 : : else
8083 : 2 : printf (" No CU\n");
8084 : : }
8085 : :
8086 : 45 : printf (" line:col SBPE* disc isa op address"
8087 : : " (Statement Block Prologue Epilogue *End)\n");
8088 : 45 : const char *last_file = "";
8089 [ + + ]: 348 : for (size_t n = 0; n < nlines; n++)
8090 : : {
8091 : 303 : Dwarf_Line *line = dwarf_onesrcline (lines, n);
8092 [ - + ]: 303 : if (line == NULL)
8093 : : {
8094 : 0 : printf (" dwarf_onesrcline: %s\n", dwarf_errmsg (-1));
8095 : 0 : continue;
8096 : : }
8097 : 303 : Dwarf_Word mtime, length;
8098 : 303 : const char *file = dwarf_linesrc (line, &mtime, &length);
8099 [ - + ]: 303 : if (file == NULL)
8100 : : {
8101 : 0 : printf (" <%s> (mtime: ?, length: ?)\n", dwarf_errmsg (-1));
8102 : 0 : last_file = "";
8103 : : }
8104 [ + + ]: 303 : else if (strcmp (last_file, file) != 0)
8105 : : {
8106 : 53 : printf (" %s (mtime: %" PRIu64 ", length: %" PRIu64 ")\n",
8107 : : file, mtime, length);
8108 : 53 : last_file = file;
8109 : : }
8110 : :
8111 : 303 : int lineno, colno;
8112 : 303 : bool statement, endseq, block, prologue_end, epilogue_begin;
8113 : 303 : unsigned int lineop, isa, disc;
8114 : 303 : Dwarf_Addr address;
8115 : 303 : dwarf_lineaddr (line, &address);
8116 : 303 : dwarf_lineno (line, &lineno);
8117 : 303 : dwarf_linecol (line, &colno);
8118 : 303 : dwarf_lineop_index (line, &lineop);
8119 : 303 : dwarf_linebeginstatement (line, &statement);
8120 : 303 : dwarf_lineendsequence (line, &endseq);
8121 : 303 : dwarf_lineblock (line, &block);
8122 : 303 : dwarf_lineprologueend (line, &prologue_end);
8123 : 303 : dwarf_lineepiloguebegin (line, &epilogue_begin);
8124 : 303 : dwarf_lineisa (line, &isa);
8125 : 303 : dwarf_linediscriminator (line, &disc);
8126 : :
8127 : : /* End sequence is special, it is one byte past. */
8128 : 303 : printf (" %4d:%-3d %c%c%c%c%c %4d %3d %2d ",
8129 : : lineno, colno,
8130 [ + + ]: 303 : (statement ? 'S' : ' '),
8131 [ + - ]: 303 : (block ? 'B' : ' '),
8132 [ + + ]: 303 : (prologue_end ? 'P' : ' '),
8133 [ + - ]: 303 : (epilogue_begin ? 'E' : ' '),
8134 [ + + ]: 303 : (endseq ? '*' : ' '),
8135 : : disc, isa, lineop);
8136 : 303 : print_dwarf_addr (dwflmod, address_size,
8137 [ + + ]: 303 : address - (endseq ? 1 : 0), address);
8138 : 303 : printf ("\n");
8139 : :
8140 [ + + ]: 303 : if (endseq)
8141 : 303 : printf("\n");
8142 : : }
8143 : : }
8144 : 39 : }
8145 : :
8146 : :
8147 : : /* Print the value of a form.
8148 : : Returns new value of readp, or readendp on failure. */
8149 : : static const unsigned char *
8150 : 20 : print_form_data (Dwarf *dbg, int form, const unsigned char *readp,
8151 : : const unsigned char *readendp, unsigned int offset_len,
8152 : : Dwarf_Off str_offsets_base)
8153 : : {
8154 : 20 : Dwarf_Word val;
8155 : 20 : unsigned char *endp;
8156 : 20 : Elf_Data *data;
8157 : 20 : char *str;
8158 [ + - - - : 20 : switch (form)
- - - - -
- - - - +
- - - - -
- - ]
8159 : : {
8160 : 8 : case DW_FORM_data1:
8161 [ - + ]: 8 : if (readendp - readp < 1)
8162 : : {
8163 : 0 : invalid_data:
8164 : 0 : error (0, 0, "invalid data");
8165 : 0 : return readendp;
8166 : : }
8167 : 8 : val = *readp++;
8168 : 8 : printf (" %" PRIx8, (unsigned int) val);
8169 : : break;
8170 : :
8171 : 0 : case DW_FORM_data2:
8172 [ # # ]: 0 : if (readendp - readp < 2)
8173 : 0 : goto invalid_data;
8174 [ # # ]: 0 : val = read_2ubyte_unaligned_inc (dbg, readp);
8175 : 0 : printf(" %" PRIx16, (unsigned int) val);
8176 : : break;
8177 : :
8178 : 0 : case DW_FORM_data4:
8179 [ # # ]: 0 : if (readendp - readp < 4)
8180 : 0 : goto invalid_data;
8181 [ # # ]: 0 : val = read_4ubyte_unaligned_inc (dbg, readp);
8182 : 0 : printf (" %" PRIx32, (unsigned int) val);
8183 : : break;
8184 : :
8185 : 0 : case DW_FORM_data8:
8186 [ # # ]: 0 : if (readendp - readp < 8)
8187 : 0 : goto invalid_data;
8188 [ # # ]: 0 : val = read_8ubyte_unaligned_inc (dbg, readp);
8189 : 0 : printf (" %" PRIx64, val);
8190 : : break;
8191 : :
8192 : 0 : case DW_FORM_sdata:
8193 [ # # ]: 0 : if (readendp - readp < 1)
8194 : 0 : goto invalid_data;
8195 : 0 : get_sleb128 (val, readp, readendp);
8196 : 0 : printf (" %" PRIx64, val);
8197 : : break;
8198 : :
8199 : 0 : case DW_FORM_udata:
8200 [ # # ]: 0 : if (readendp - readp < 1)
8201 : 0 : goto invalid_data;
8202 : 0 : get_uleb128 (val, readp, readendp);
8203 : 0 : printf (" %" PRIx64, val);
8204 : : break;
8205 : :
8206 : 0 : case DW_FORM_block:
8207 [ # # ]: 0 : if (readendp - readp < 1)
8208 : 0 : goto invalid_data;
8209 : 0 : get_uleb128 (val, readp, readendp);
8210 [ # # ]: 0 : if ((size_t) (readendp - readp) < val)
8211 : 0 : goto invalid_data;
8212 : 0 : print_bytes (val, readp);
8213 : 0 : readp += val;
8214 : 0 : break;
8215 : :
8216 : 0 : case DW_FORM_block1:
8217 [ # # ]: 0 : if (readendp - readp < 1)
8218 : 0 : goto invalid_data;
8219 : 0 : val = *readp++;
8220 [ # # ]: 0 : if ((size_t) (readendp - readp) < val)
8221 : 0 : goto invalid_data;
8222 : 0 : print_bytes (val, readp);
8223 : 0 : readp += val;
8224 : 0 : break;
8225 : :
8226 : 0 : case DW_FORM_block2:
8227 [ # # ]: 0 : if (readendp - readp < 2)
8228 : 0 : goto invalid_data;
8229 [ # # ]: 0 : val = read_2ubyte_unaligned_inc (dbg, readp);
8230 [ # # ]: 0 : if ((size_t) (readendp - readp) < val)
8231 : 0 : goto invalid_data;
8232 : 0 : print_bytes (val, readp);
8233 : 0 : readp += val;
8234 : 0 : break;
8235 : :
8236 : 0 : case DW_FORM_block4:
8237 [ # # ]: 0 : if (readendp - readp < 4)
8238 : 0 : goto invalid_data;
8239 [ # # ]: 0 : val = read_4ubyte_unaligned_inc (dbg, readp);
8240 [ # # ]: 0 : if ((size_t) (readendp - readp) < val)
8241 : 0 : goto invalid_data;
8242 : 0 : print_bytes (val, readp);
8243 : 0 : readp += val;
8244 : 0 : break;
8245 : :
8246 : 0 : case DW_FORM_data16:
8247 [ # # ]: 0 : if (readendp - readp < 16)
8248 : 0 : goto invalid_data;
8249 : 0 : print_bytes (16, readp);
8250 : 0 : readp += 16;
8251 : 0 : break;
8252 : :
8253 : 0 : case DW_FORM_flag:
8254 [ # # ]: 0 : if (readendp - readp < 1)
8255 : 0 : goto invalid_data;
8256 : 0 : val = *readp++;
8257 [ # # ]: 0 : printf ("%s", val != 0 ? yes_str : no_str);
8258 : : break;
8259 : :
8260 : 0 : case DW_FORM_string:
8261 : 0 : endp = memchr (readp, '\0', readendp - readp);
8262 [ # # ]: 0 : if (endp == NULL)
8263 : 0 : goto invalid_data;
8264 : 0 : printf ("%s", readp);
8265 : 0 : readp = endp + 1;
8266 : 0 : break;
8267 : :
8268 : 12 : case DW_FORM_strp:
8269 : : case DW_FORM_line_strp:
8270 : : case DW_FORM_strp_sup:
8271 [ - + ]: 12 : if ((size_t) (readendp - readp) < offset_len)
8272 : 0 : goto invalid_data;
8273 [ - + ]: 12 : if (offset_len == 8)
8274 [ # # ]: 0 : val = read_8ubyte_unaligned_inc (dbg, readp);
8275 : : else
8276 [ - + ]: 24 : val = read_4ubyte_unaligned_inc (dbg, readp);
8277 [ - + ]: 12 : if (form == DW_FORM_strp)
8278 : 0 : data = dbg->sectiondata[IDX_debug_str];
8279 [ + - ]: 12 : else if (form == DW_FORM_line_strp)
8280 : 12 : data = dbg->sectiondata[IDX_debug_line_str];
8281 : : else /* form == DW_FORM_strp_sup */
8282 : : {
8283 : 0 : Dwarf *alt = dwarf_getalt (dbg);
8284 [ # # ]: 0 : data = alt != NULL ? alt->sectiondata[IDX_debug_str] : NULL;
8285 : : }
8286 [ + - + - ]: 12 : if (data == NULL || val >= data->d_size
8287 [ + - ]: 12 : || memchr (data->d_buf + val, '\0', data->d_size - val) == NULL)
8288 : : str = "???";
8289 : : else
8290 : 12 : str = (char *) data->d_buf + val;
8291 : 12 : printf ("%s (%" PRIu64 ")", str, val);
8292 : : break;
8293 : :
8294 : 0 : case DW_FORM_sec_offset:
8295 [ # # ]: 0 : if ((size_t) (readendp - readp) < offset_len)
8296 : 0 : goto invalid_data;
8297 [ # # ]: 0 : if (offset_len == 8)
8298 [ # # ]: 0 : val = read_8ubyte_unaligned_inc (dbg, readp);
8299 : : else
8300 [ # # ]: 0 : val = read_4ubyte_unaligned_inc (dbg, readp);
8301 : 0 : printf ("[%" PRIx64 "]", val);
8302 : : break;
8303 : :
8304 : 0 : case DW_FORM_strx:
8305 : : case DW_FORM_GNU_str_index:
8306 [ # # ]: 0 : if (readendp - readp < 1)
8307 : 0 : goto invalid_data;
8308 : 0 : get_uleb128 (val, readp, readendp);
8309 : 0 : strx_val:
8310 : 0 : data = dbg->sectiondata[IDX_debug_str_offsets];
8311 [ # # ]: 0 : if (data == NULL
8312 [ # # ]: 0 : || data->d_size - str_offsets_base < val)
8313 : : str = "???";
8314 : : else
8315 : : {
8316 : 0 : const unsigned char *strreadp = data->d_buf + str_offsets_base + val;
8317 : 0 : const unsigned char *strreadendp = data->d_buf + data->d_size;
8318 [ # # ]: 0 : if ((size_t) (strreadendp - strreadp) < offset_len)
8319 : : str = "???";
8320 : : else
8321 : : {
8322 : 0 : Dwarf_Off idx;
8323 [ # # ]: 0 : if (offset_len == 8)
8324 [ # # ]: 0 : idx = read_8ubyte_unaligned (dbg, strreadp);
8325 : : else
8326 [ # # ]: 0 : idx = read_4ubyte_unaligned (dbg, strreadp);
8327 : :
8328 : 0 : data = dbg->sectiondata[IDX_debug_str];
8329 [ # # # # ]: 0 : if (data == NULL || idx >= data->d_size
8330 [ # # ]: 0 : || memchr (data->d_buf + idx, '\0',
8331 : : data->d_size - idx) == NULL)
8332 : : str = "???";
8333 : : else
8334 : 0 : str = (char *) data->d_buf + idx;
8335 : : }
8336 : : }
8337 : 0 : printf ("%s (%" PRIu64 ")", str, val);
8338 : : break;
8339 : :
8340 : 0 : case DW_FORM_strx1:
8341 [ # # ]: 0 : if (readendp - readp < 1)
8342 : 0 : goto invalid_data;
8343 : 0 : val = *readp++;
8344 : 0 : goto strx_val;
8345 : :
8346 : 0 : case DW_FORM_strx2:
8347 [ # # ]: 0 : if (readendp - readp < 2)
8348 : 0 : goto invalid_data;
8349 [ # # ]: 0 : val = read_2ubyte_unaligned_inc (dbg, readp);
8350 : 0 : goto strx_val;
8351 : :
8352 : 0 : case DW_FORM_strx3:
8353 [ # # ]: 0 : if (readendp - readp < 3)
8354 : 0 : goto invalid_data;
8355 : 0 : val = read_3ubyte_unaligned_inc (dbg, readp);
8356 : 0 : goto strx_val;
8357 : :
8358 : 0 : case DW_FORM_strx4:
8359 [ # # ]: 0 : if (readendp - readp < 4)
8360 : 0 : goto invalid_data;
8361 [ # # ]: 0 : val = read_4ubyte_unaligned_inc (dbg, readp);
8362 : 0 : goto strx_val;
8363 : :
8364 : 0 : default:
8365 : 0 : error (0, 0, _("unknown form: %s"), dwarf_form_name (form));
8366 : 0 : return readendp;
8367 : : }
8368 : :
8369 : 20 : return readp;
8370 : : }
8371 : :
8372 : : /* Only used via run_advance_pc() macro */
8373 : : static inline void
8374 : 377230 : run_advance_pc (unsigned int op_advance,
8375 : : unsigned int minimum_instr_len,
8376 : : unsigned int max_ops_per_instr,
8377 : : unsigned int *op_addr_advance,
8378 : : Dwarf_Word *address,
8379 : : unsigned int *op_index)
8380 : : {
8381 : 377230 : const unsigned int advanced_op_index = (*op_index) + op_advance;
8382 : :
8383 : 377230 : *op_addr_advance = minimum_instr_len * (advanced_op_index
8384 : 377230 : / max_ops_per_instr);
8385 : 377230 : *address = *address + *op_addr_advance;
8386 : 377230 : *op_index = advanced_op_index % max_ops_per_instr;
8387 : : }
8388 : :
8389 : : static void
8390 : 87 : print_debug_line_section (Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr,
8391 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
8392 : : {
8393 [ + + ]: 87 : if (decodedline)
8394 : : {
8395 : 39 : print_decoded_line_section (dwflmod, ebl, ehdr, scn, shdr, dbg);
8396 : 78 : return;
8397 : : }
8398 : :
8399 : 48 : printf (_("\
8400 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
8401 : : elf_ndxscn (scn), section_name (ebl, shdr),
8402 : 48 : (uint64_t) shdr->sh_offset);
8403 : :
8404 [ + - ]: 48 : if (shdr->sh_size == 0)
8405 : : return;
8406 : :
8407 : : /* There is no functionality in libdw to read the information in the
8408 : : way it is represented here. Hardcode the decoder. */
8409 : 96 : Elf_Data *data = (dbg->sectiondata[IDX_debug_line]
8410 [ - + ]: 48 : ?: elf_rawdata (scn, NULL));
8411 [ - + ]: 48 : if (unlikely (data == NULL))
8412 : : {
8413 : 0 : error (0, 0, _("cannot get line data section data: %s"),
8414 : : elf_errmsg (-1));
8415 : 0 : return;
8416 : : }
8417 : :
8418 : 48 : const unsigned char *linep = (const unsigned char *) data->d_buf;
8419 : 48 : const unsigned char *lineendp;
8420 : :
8421 : 48 : while (linep
8422 [ + + ]: 1695 : < (lineendp = (const unsigned char *) data->d_buf + data->d_size))
8423 : : {
8424 : 1647 : size_t start_offset = linep - (const unsigned char *) data->d_buf;
8425 : :
8426 : 1647 : printf (_("\nTable at offset %zu:\n"), start_offset);
8427 : :
8428 [ - + ]: 1647 : if (unlikely (linep + 4 > lineendp))
8429 : 0 : goto invalid_data;
8430 [ + + ]: 1647 : Dwarf_Word unit_length = read_4ubyte_unaligned_inc (dbg, linep);
8431 : 1647 : unsigned int length = 4;
8432 [ - + ]: 1647 : if (unlikely (unit_length == 0xffffffff))
8433 : : {
8434 [ # # ]: 0 : if (unlikely (linep + 8 > lineendp))
8435 : : {
8436 : 0 : invalid_data:
8437 : 0 : error (0, 0, _("invalid data in section [%zu] '%s'"),
8438 : : elf_ndxscn (scn), section_name (ebl, shdr));
8439 : 0 : return;
8440 : : }
8441 [ # # ]: 0 : unit_length = read_8ubyte_unaligned_inc (dbg, linep);
8442 : 0 : length = 8;
8443 : : }
8444 : :
8445 : : /* Check whether we have enough room in the section. */
8446 [ - + ]: 1647 : if (unlikely (unit_length > (size_t) (lineendp - linep)))
8447 : 0 : goto invalid_data;
8448 : 1647 : lineendp = linep + unit_length;
8449 : :
8450 : : /* The next element of the header is the version identifier. */
8451 [ - + ]: 1647 : if ((size_t) (lineendp - linep) < 2)
8452 : 0 : goto invalid_data;
8453 [ + + ]: 1647 : uint_fast16_t version = read_2ubyte_unaligned_inc (dbg, linep);
8454 : :
8455 : 3294 : size_t address_size
8456 [ + + ]: 1647 : = elf_getident (ebl->elf, NULL)[EI_CLASS] == ELFCLASS32 ? 4 : 8;
8457 : 1647 : unsigned char segment_selector_size = 0;
8458 [ + + ]: 1647 : if (version > 4)
8459 : : {
8460 [ - + ]: 2 : if ((size_t) (lineendp - linep) < 2)
8461 : 0 : goto invalid_data;
8462 : 2 : address_size = *linep++;
8463 : 2 : segment_selector_size = *linep++;
8464 : : }
8465 : :
8466 : : /* Next comes the header length. */
8467 : 1647 : Dwarf_Word header_length;
8468 [ + - ]: 1647 : if (length == 4)
8469 : : {
8470 [ - + ]: 1647 : if ((size_t) (lineendp - linep) < 4)
8471 : 0 : goto invalid_data;
8472 [ + + ]: 3294 : header_length = read_4ubyte_unaligned_inc (dbg, linep);
8473 : : }
8474 : : else
8475 : : {
8476 [ # # ]: 0 : if ((size_t) (lineendp - linep) < 8)
8477 : 0 : goto invalid_data;
8478 [ # # ]: 0 : header_length = read_8ubyte_unaligned_inc (dbg, linep);
8479 : : }
8480 : :
8481 : 1647 : const unsigned char *header_start = linep;
8482 : :
8483 : : /* Next the minimum instruction length. */
8484 [ - + ]: 1647 : if ((size_t) (lineendp - linep) < 1)
8485 : 0 : goto invalid_data;
8486 : 1647 : uint_fast8_t minimum_instr_len = *linep++;
8487 : :
8488 : : /* Next the maximum operations per instruction, in version 4 format. */
8489 : 1647 : uint_fast8_t max_ops_per_instr;
8490 [ + + ]: 1647 : if (version < 4)
8491 : : max_ops_per_instr = 1;
8492 : : else
8493 : : {
8494 [ - + ]: 10 : if ((size_t) (lineendp - linep) < 1)
8495 : 0 : goto invalid_data;
8496 : 10 : max_ops_per_instr = *linep++;
8497 : : }
8498 : :
8499 : : /* We need at least 4 more bytes. */
8500 [ - + ]: 1647 : if ((size_t) (lineendp - linep) < 4)
8501 : 0 : goto invalid_data;
8502 : :
8503 : : /* Then the flag determining the default value of the is_stmt
8504 : : register. */
8505 : 1647 : uint_fast8_t default_is_stmt = *linep++;
8506 : :
8507 : : /* Now the line base. */
8508 : 1647 : int_fast8_t line_base = *linep++;
8509 : :
8510 : : /* And the line range. */
8511 : 1647 : uint_fast8_t line_range = *linep++;
8512 : :
8513 : : /* The opcode base. */
8514 : 1647 : uint_fast8_t opcode_base = *linep++;
8515 : :
8516 : : /* Print what we got so far. */
8517 : 1647 : printf (_("\n"
8518 : : " Length: %" PRIu64 "\n"
8519 : : " DWARF version: %" PRIuFAST16 "\n"
8520 : : " Prologue length: %" PRIu64 "\n"
8521 : : " Address size: %zd\n"
8522 : : " Segment selector size: %zd\n"
8523 : : " Min instruction length: %" PRIuFAST8 "\n"
8524 : : " Max operations per instruction: %" PRIuFAST8 "\n"
8525 : : " Initial value if 'is_stmt': %" PRIuFAST8 "\n"
8526 : : " Line base: %" PRIdFAST8 "\n"
8527 : : " Line range: %" PRIuFAST8 "\n"
8528 : : " Opcode base: %" PRIuFAST8 "\n"
8529 : : "\n"
8530 : : "Opcodes:\n"),
8531 : : (uint64_t) unit_length, version, (uint64_t) header_length,
8532 : : address_size, (size_t) segment_selector_size,
8533 : : minimum_instr_len, max_ops_per_instr,
8534 : : default_is_stmt, line_base,
8535 : : line_range, opcode_base);
8536 : :
8537 [ - + ]: 1647 : if (version < 2 || version > 5)
8538 : : {
8539 : 0 : error (0, 0, _("cannot handle .debug_line version: %u\n"),
8540 : : (unsigned int) version);
8541 : 0 : linep = lineendp;
8542 : 0 : continue;
8543 : : }
8544 : :
8545 [ - + ]: 1647 : if (address_size != 4 && address_size != 8)
8546 : : {
8547 : 0 : error (0, 0, _("cannot handle address size: %u\n"),
8548 : : (unsigned int) address_size);
8549 : 0 : linep = lineendp;
8550 : 0 : continue;
8551 : : }
8552 : :
8553 [ - + ]: 1647 : if (segment_selector_size != 0)
8554 : : {
8555 : 0 : error (0, 0, _("cannot handle segment selector size: %u\n"),
8556 : : (unsigned int) segment_selector_size);
8557 : 0 : linep = lineendp;
8558 : 0 : continue;
8559 : : }
8560 : :
8561 [ - + ]: 1647 : if (unlikely (linep + opcode_base - 1 >= lineendp))
8562 : : {
8563 : 0 : invalid_unit:
8564 : 0 : error (0, 0,
8565 : 0 : _("invalid data at offset %tu in section [%zu] '%s'"),
8566 : 0 : linep - (const unsigned char *) data->d_buf,
8567 : : elf_ndxscn (scn), section_name (ebl, shdr));
8568 : 0 : linep = lineendp;
8569 : 0 : continue;
8570 : : }
8571 : 1647 : int opcode_base_l10 = 1;
8572 : 1647 : unsigned int tmp = opcode_base;
8573 [ + + ]: 3291 : while (tmp > 10)
8574 : : {
8575 : 1644 : tmp /= 10;
8576 : 1644 : ++opcode_base_l10;
8577 : : }
8578 : 1647 : const uint8_t *standard_opcode_lengths = linep - 1;
8579 [ + + ]: 21402 : for (uint_fast8_t cnt = 1; cnt < opcode_base; ++cnt)
8580 : 39510 : printf (ngettext (" [%*" PRIuFAST8 "] %hhu argument\n",
8581 : : " [%*" PRIuFAST8 "] %hhu arguments\n",
8582 : : (int) linep[cnt - 1]),
8583 : 19755 : opcode_base_l10, cnt, linep[cnt - 1]);
8584 : 1647 : linep += opcode_base - 1;
8585 : :
8586 [ - + ]: 1647 : if (unlikely (linep >= lineendp))
8587 : 0 : goto invalid_unit;
8588 : :
8589 : 1647 : Dwarf_Off str_offsets_base = str_offsets_base_off (dbg, NULL);
8590 : :
8591 : 1647 : puts (_("\nDirectory table:"));
8592 [ + + ]: 1647 : if (version > 4)
8593 : : {
8594 : 2 : struct encpair { uint16_t desc; uint16_t form; };
8595 : 2 : struct encpair enc[256];
8596 : :
8597 : 2 : printf (_(" ["));
8598 [ - + ]: 2 : if ((size_t) (lineendp - linep) < 1)
8599 : 0 : goto invalid_data;
8600 : 2 : unsigned char directory_entry_format_count = *linep++;
8601 : 2 : for (int i = 0; i < directory_entry_format_count; i++)
8602 : : {
8603 : 2 : uint16_t desc, form;
8604 [ - + ]: 2 : if ((size_t) (lineendp - linep) < 1)
8605 : 0 : goto invalid_data;
8606 : 2 : get_uleb128 (desc, linep, lineendp);
8607 [ - + ]: 2 : if ((size_t) (lineendp - linep) < 1)
8608 : 0 : goto invalid_data;
8609 : 2 : get_uleb128 (form, linep, lineendp);
8610 : :
8611 : 2 : enc[i].desc = desc;
8612 : 2 : enc[i].form = form;
8613 : :
8614 : 2 : printf ("%s(%s)",
8615 : : dwarf_line_content_description_name (desc),
8616 : : dwarf_form_name (form));
8617 [ - + ]: 2 : if (i + 1 < directory_entry_format_count)
8618 [ + + ]: 4 : printf (", ");
8619 : : }
8620 : 2 : printf ("]\n");
8621 : :
8622 : 2 : uint64_t directories_count;
8623 [ - + ]: 2 : if ((size_t) (lineendp - linep) < 1)
8624 : 0 : goto invalid_data;
8625 : 2 : get_uleb128 (directories_count, linep, lineendp);
8626 : :
8627 : 2 : if (directory_entry_format_count == 0
8628 [ - + ]: 2 : && directories_count != 0)
8629 : 0 : goto invalid_data;
8630 : :
8631 [ + + ]: 6 : for (uint64_t i = 0; i < directories_count; i++)
8632 : : {
8633 : 4 : printf (" %-5" PRIu64 " ", i);
8634 : 4 : for (int j = 0; j < directory_entry_format_count; j++)
8635 : : {
8636 : 4 : linep = print_form_data (dbg, enc[j].form,
8637 : : linep, lineendp, length,
8638 : : str_offsets_base);
8639 [ - + ]: 4 : if (j + 1 < directory_entry_format_count)
8640 [ + + ]: 8 : printf (", ");
8641 : : }
8642 : 4 : printf ("\n");
8643 [ - + ]: 4 : if (linep >= lineendp)
8644 : 0 : goto invalid_unit;
8645 : : }
8646 : : }
8647 : : else
8648 : : {
8649 [ + - + + ]: 12282 : while (linep < lineendp && *linep != 0)
8650 : : {
8651 : 10637 : unsigned char *endp = memchr (linep, '\0', lineendp - linep);
8652 [ - + ]: 10637 : if (unlikely (endp == NULL))
8653 : 0 : goto invalid_unit;
8654 : :
8655 : 10637 : printf (" %s\n", (char *) linep);
8656 : :
8657 : 10637 : linep = endp + 1;
8658 : : }
8659 [ + - - + ]: 1645 : if (linep >= lineendp || *linep != 0)
8660 : 0 : goto invalid_unit;
8661 : : /* Skip the final NUL byte. */
8662 : 1645 : ++linep;
8663 : : }
8664 : :
8665 [ - + ]: 1647 : if (unlikely (linep >= lineendp))
8666 : 0 : goto invalid_unit;
8667 : :
8668 : 1647 : puts (_("\nFile name table:"));
8669 [ + + ]: 1647 : if (version > 4)
8670 : : {
8671 : 2 : struct encpair { uint16_t desc; uint16_t form; };
8672 : 2 : struct encpair enc[256];
8673 : :
8674 : 2 : printf (_(" ["));
8675 [ - + ]: 2 : if ((size_t) (lineendp - linep) < 1)
8676 : 0 : goto invalid_data;
8677 : 2 : unsigned char file_name_format_count = *linep++;
8678 : 2 : for (int i = 0; i < file_name_format_count; i++)
8679 : : {
8680 : 4 : uint64_t desc, form;
8681 [ - + ]: 4 : if ((size_t) (lineendp - linep) < 1)
8682 : 0 : goto invalid_data;
8683 : 4 : get_uleb128 (desc, linep, lineendp);
8684 [ - + ]: 4 : if ((size_t) (lineendp - linep) < 1)
8685 : 0 : goto invalid_data;
8686 : 4 : get_uleb128 (form, linep, lineendp);
8687 : :
8688 [ - + ]: 4 : if (! libdw_valid_user_form (form))
8689 : 0 : goto invalid_data;
8690 : :
8691 : 4 : enc[i].desc = desc;
8692 : 4 : enc[i].form = form;
8693 : :
8694 : 4 : printf ("%s(%s)",
8695 : : dwarf_line_content_description_name (desc),
8696 : : dwarf_form_name (form));
8697 [ + + ]: 4 : if (i + 1 < file_name_format_count)
8698 [ + + ]: 8 : printf (", ");
8699 : : }
8700 : 2 : printf ("]\n");
8701 : :
8702 : 2 : uint64_t file_name_count;
8703 [ - + ]: 2 : if ((size_t) (lineendp - linep) < 1)
8704 : 0 : goto invalid_data;
8705 : 2 : get_uleb128 (file_name_count, linep, lineendp);
8706 : :
8707 : 2 : if (file_name_format_count == 0
8708 [ - + ]: 2 : && file_name_count != 0)
8709 : 0 : goto invalid_data;
8710 : :
8711 [ + + ]: 10 : for (uint64_t i = 0; i < file_name_count; i++)
8712 : : {
8713 : 8 : printf (" %-5" PRIu64 " ", i);
8714 : 8 : for (int j = 0; j < file_name_format_count; j++)
8715 : : {
8716 : 16 : linep = print_form_data (dbg, enc[j].form,
8717 : : linep, lineendp, length,
8718 : : str_offsets_base);
8719 [ + + ]: 16 : if (j + 1 < file_name_format_count)
8720 [ + + ]: 32 : printf (", ");
8721 : : }
8722 : 8 : printf ("\n");
8723 [ - + ]: 8 : if (linep > lineendp)
8724 : 0 : goto invalid_unit;
8725 : : }
8726 : : }
8727 : : else
8728 : : {
8729 : 1645 : puts (_(" Entry Dir Time Size Name"));
8730 [ + - + + ]: 27995 : for (unsigned int cnt = 1; linep < lineendp && *linep != 0; ++cnt)
8731 : : {
8732 : : /* First comes the file name. */
8733 : 26350 : char *fname = (char *) linep;
8734 : 26350 : unsigned char *endp = memchr (fname, '\0', lineendp - linep);
8735 [ - + ]: 26350 : if (unlikely (endp == NULL))
8736 : 0 : goto invalid_unit;
8737 : 26350 : linep = endp + 1;
8738 : :
8739 : : /* Then the index. */
8740 : 26350 : unsigned int diridx;
8741 [ - + ]: 26350 : if (lineendp - linep < 1)
8742 : 0 : goto invalid_unit;
8743 : 26350 : get_uleb128 (diridx, linep, lineendp);
8744 : :
8745 : : /* Next comes the modification time. */
8746 : 26350 : unsigned int mtime;
8747 [ - + ]: 26350 : if (lineendp - linep < 1)
8748 : 0 : goto invalid_unit;
8749 : 26350 : get_uleb128 (mtime, linep, lineendp);
8750 : :
8751 : : /* Finally the length of the file. */
8752 : 26350 : unsigned int fsize;
8753 [ - + ]: 26350 : if (lineendp - linep < 1)
8754 : 0 : goto invalid_unit;
8755 : 26350 : get_uleb128 (fsize, linep, lineendp);
8756 : :
8757 : 26350 : printf (" %-5u %-5u %-9u %-9u %s\n",
8758 : : cnt, diridx, mtime, fsize, fname);
8759 : : }
8760 [ + - - + ]: 1645 : if (linep >= lineendp || *linep != '\0')
8761 : 0 : goto invalid_unit;
8762 : : /* Skip the final NUL byte. */
8763 : 1645 : ++linep;
8764 : : }
8765 : :
8766 : 1647 : unsigned int debug_str_offset = 0;
8767 [ + + ]: 1647 : if (unlikely (linep == header_start + header_length - 4))
8768 : : {
8769 : : /* CUBINs contain an unsigned 4-byte offset */
8770 [ - + ]: 2 : debug_str_offset = read_4ubyte_unaligned_inc (dbg, linep);
8771 : : }
8772 : :
8773 [ + + ]: 1647 : if (linep == lineendp)
8774 : : {
8775 : 16 : puts (_("\nNo line number statements."));
8776 : 16 : continue;
8777 : : }
8778 : :
8779 : 1631 : puts (_("\nLine number statements:"));
8780 : 1631 : Dwarf_Word address = 0;
8781 : 1631 : unsigned int op_index = 0;
8782 : 1631 : size_t line = 1;
8783 : 1631 : uint_fast8_t is_stmt = default_is_stmt;
8784 : :
8785 : : /* Apply the "operation advance" from a special opcode
8786 : : or DW_LNS_advance_pc (as per DWARF4 6.2.5.1). */
8787 : 1631 : unsigned int op_addr_advance;
8788 : : #define advance_pc(op_advance) run_advance_pc(op_advance, minimum_instr_len, \
8789 : : max_ops_per_instr, &op_addr_advance, &address, &op_index)
8790 : :
8791 [ - + ]: 1631 : if (max_ops_per_instr == 0)
8792 : : {
8793 : 0 : error (0, 0,
8794 : 0 : _("invalid maximum operations per instruction is zero"));
8795 : 0 : linep = lineendp;
8796 : 0 : continue;
8797 : : }
8798 : :
8799 : 1117166 : while (linep < lineendp)
8800 : : {
8801 : 1115535 : size_t offset = linep - (const unsigned char *) data->d_buf;
8802 : 1115535 : unsigned int u128;
8803 : 1115535 : int s128;
8804 : :
8805 : : /* Read the opcode. */
8806 : 1115535 : unsigned int opcode = *linep++;
8807 : :
8808 : 1115535 : printf (" [%6" PRIx64 "]", (uint64_t)offset);
8809 : : /* Is this a special opcode? */
8810 [ + + ]: 1115535 : if (likely (opcode >= opcode_base))
8811 : : {
8812 [ - + ]: 375403 : if (unlikely (line_range == 0))
8813 : 0 : goto invalid_unit;
8814 : :
8815 : : /* Yes. Handling this is quite easy since the opcode value
8816 : : is computed with
8817 : :
8818 : : opcode = (desired line increment - line_base)
8819 : : + (line_range * address advance) + opcode_base
8820 : : */
8821 : 375403 : int line_increment = (line_base
8822 : 375403 : + (opcode - opcode_base) % line_range);
8823 : :
8824 : : /* Perform the increments. */
8825 : 375403 : line += line_increment;
8826 : 375403 : advance_pc ((opcode - opcode_base) / line_range);
8827 : :
8828 : 375403 : printf (_(" special opcode %u: address+%u = "),
8829 : : opcode, op_addr_advance);
8830 : 375403 : print_dwarf_addr (dwflmod, 0, address, address);
8831 [ - + ]: 375403 : if (op_index > 0)
8832 : 0 : printf (_(", op_index = %u, line%+d = %zu\n"),
8833 : : op_index, line_increment, line);
8834 : : else
8835 : 375403 : printf (_(", line%+d = %zu\n"),
8836 : : line_increment, line);
8837 : : }
8838 [ + + ]: 740132 : else if (opcode == 0)
8839 : : {
8840 : : /* This an extended opcode. */
8841 [ - + ]: 54040 : if (unlikely (linep + 2 > lineendp))
8842 : 0 : goto invalid_unit;
8843 : :
8844 : : /* The length. */
8845 : 54040 : unsigned int len = *linep++;
8846 : :
8847 [ - + ]: 54040 : if (unlikely (linep + len > lineendp))
8848 : 0 : goto invalid_unit;
8849 : :
8850 : : /* The sub-opcode. */
8851 : 54040 : opcode = *linep++;
8852 : :
8853 : 54040 : printf (_(" extended opcode %u: "), opcode);
8854 : :
8855 [ + + - + : 54040 : switch (opcode)
+ - - ]
8856 : : {
8857 : 1658 : case DW_LNE_end_sequence:
8858 : 1658 : puts (_(" end of sequence"));
8859 : :
8860 : : /* Reset the registers we care about. */
8861 : 1658 : address = 0;
8862 : 1658 : op_index = 0;
8863 : 1658 : line = 1;
8864 : 1658 : is_stmt = default_is_stmt;
8865 : 1658 : break;
8866 : :
8867 : 1899 : case DW_LNE_set_address:
8868 : 1899 : op_index = 0;
8869 [ - + ]: 1899 : if (unlikely ((size_t) (lineendp - linep) < address_size))
8870 : 0 : goto invalid_unit;
8871 [ + + ]: 1899 : if (address_size == 4)
8872 [ + + ]: 34 : address = read_4ubyte_unaligned_inc (dbg, linep);
8873 : : else
8874 [ + + ]: 3764 : address = read_8ubyte_unaligned_inc (dbg, linep);
8875 : : {
8876 : 1899 : printf (_(" set address to "));
8877 : 1899 : print_dwarf_addr (dwflmod, 0, address, address);
8878 : 1899 : printf ("\n");
8879 : : }
8880 : : break;
8881 : :
8882 : 0 : case DW_LNE_define_file:
8883 : : {
8884 : 0 : char *fname = (char *) linep;
8885 : 0 : unsigned char *endp = memchr (linep, '\0',
8886 : 0 : lineendp - linep);
8887 [ # # ]: 0 : if (unlikely (endp == NULL))
8888 : 0 : goto invalid_unit;
8889 : 0 : linep = endp + 1;
8890 : :
8891 : 0 : unsigned int diridx;
8892 [ # # ]: 0 : if (lineendp - linep < 1)
8893 : 0 : goto invalid_unit;
8894 : 0 : get_uleb128 (diridx, linep, lineendp);
8895 : 0 : Dwarf_Word mtime;
8896 [ # # ]: 0 : if (lineendp - linep < 1)
8897 : 0 : goto invalid_unit;
8898 : 0 : get_uleb128 (mtime, linep, lineendp);
8899 : 0 : Dwarf_Word filelength;
8900 [ # # ]: 0 : if (lineendp - linep < 1)
8901 : 0 : goto invalid_unit;
8902 : 0 : get_uleb128 (filelength, linep, lineendp);
8903 : :
8904 : 0 : printf (_("\
8905 : : define new file: dir=%u, mtime=%" PRIu64 ", length=%" PRIu64 ", name=%s\n"),
8906 : : diridx, (uint64_t) mtime, (uint64_t) filelength,
8907 : : fname);
8908 : : }
8909 : : break;
8910 : :
8911 : 50476 : case DW_LNE_set_discriminator:
8912 : : /* Takes one ULEB128 parameter, the discriminator. */
8913 [ + - - + ]: 50476 : if (unlikely (standard_opcode_lengths[opcode] != 1
8914 : : || lineendp - linep < 1))
8915 : 0 : goto invalid_unit;
8916 : :
8917 : 50476 : get_uleb128 (u128, linep, lineendp);
8918 : 50476 : printf (_(" set discriminator to %u\n"), u128);
8919 : : break;
8920 : :
8921 : 7 : case DW_LNE_NVIDIA_inlined_call:
8922 : : {
8923 [ - + ]: 7 : if (unlikely (linep >= lineendp))
8924 : 0 : goto invalid_data;
8925 : :
8926 : 7 : unsigned int context;
8927 : 7 : get_uleb128 (context, linep, lineendp);
8928 : :
8929 [ - + ]: 7 : if (unlikely (linep >= lineendp))
8930 : 0 : goto invalid_data;
8931 : :
8932 : 7 : unsigned int function_name;
8933 : 7 : get_uleb128 (function_name, linep, lineendp);
8934 : 7 : function_name += debug_str_offset;
8935 : :
8936 : 7 : Elf_Data *str_data = dbg->sectiondata[IDX_debug_str];
8937 : 7 : char *function_str;
8938 [ + - + - ]: 7 : if (str_data == NULL || function_name >= str_data->d_size
8939 [ + - ]: 7 : || memchr (str_data->d_buf + function_name, '\0',
8940 : : str_data->d_size - function_name) == NULL)
8941 : : function_str = "???";
8942 : : else
8943 : 7 : function_str = (char *) str_data->d_buf + function_name;
8944 : :
8945 : 7 : printf (_(" set inlined context %u,"
8946 : : " function name %s (0x%x)\n"),
8947 : : context, function_str, function_name);
8948 : : break;
8949 : : }
8950 : :
8951 : 0 : case DW_LNE_NVIDIA_set_function_name:
8952 : : {
8953 [ # # ]: 0 : if (unlikely (linep >= lineendp))
8954 : 0 : goto invalid_data;
8955 : :
8956 : 0 : unsigned int function_name;
8957 : 0 : get_uleb128 (function_name, linep, lineendp);
8958 : 0 : function_name += debug_str_offset;
8959 : :
8960 : 0 : Elf_Data *str_data = dbg->sectiondata[IDX_debug_str];
8961 : 0 : char *function_str;
8962 [ # # # # ]: 0 : if (str_data == NULL || function_name >= str_data->d_size
8963 [ # # ]: 0 : || memchr (str_data->d_buf + function_name, '\0',
8964 : : str_data->d_size - function_name) == NULL)
8965 : : function_str = "???";
8966 : : else
8967 : 0 : function_str = (char *) str_data->d_buf + function_name;
8968 : :
8969 : 0 : printf (_(" set function name %s (0x%x)\n"),
8970 : : function_str, function_name);
8971 : : }
8972 : : break;
8973 : :
8974 : 0 : default:
8975 : : /* Unknown, ignore it. */
8976 : 0 : puts (_(" unknown opcode"));
8977 : 0 : linep += len - 1;
8978 : 0 : break;
8979 : : }
8980 : : }
8981 [ + - ]: 686092 : else if (opcode <= DW_LNS_set_isa)
8982 : : {
8983 : : /* This is a known standard opcode. */
8984 [ + + + + : 686092 : switch (opcode)
+ + - + +
+ - - ]
8985 : : {
8986 : 101308 : case DW_LNS_copy:
8987 : : /* Takes no argument. */
8988 : 101308 : puts (_(" copy"));
8989 : 101308 : break;
8990 : :
8991 : 1599 : case DW_LNS_advance_pc:
8992 : : /* Takes one uleb128 parameter which is added to the
8993 : : address. */
8994 [ - + ]: 1599 : if (lineendp - linep < 1)
8995 : 0 : goto invalid_unit;
8996 : 1599 : get_uleb128 (u128, linep, lineendp);
8997 : 1599 : advance_pc (u128);
8998 : : {
8999 : 1599 : printf (_(" advance address by %u to "),
9000 : : op_addr_advance);
9001 : 1599 : print_dwarf_addr (dwflmod, 0, address, address);
9002 [ - + ]: 1599 : if (op_index > 0)
9003 : 0 : printf (_(", op_index to %u"), op_index);
9004 : 1599 : printf ("\n");
9005 : : }
9006 : : break;
9007 : :
9008 : 88180 : case DW_LNS_advance_line:
9009 : : /* Takes one sleb128 parameter which is added to the
9010 : : line. */
9011 [ - + ]: 88180 : if (lineendp - linep < 1)
9012 : 0 : goto invalid_unit;
9013 : 88180 : get_sleb128 (s128, linep, lineendp);
9014 : 88180 : line += s128;
9015 : 88180 : printf (_("\
9016 : : advance line by constant %d to %" PRId64 "\n"),
9017 : : s128, (int64_t) line);
9018 : : break;
9019 : :
9020 : 31268 : case DW_LNS_set_file:
9021 : : /* Takes one uleb128 parameter which is stored in file. */
9022 [ - + ]: 31268 : if (lineendp - linep < 1)
9023 : 0 : goto invalid_unit;
9024 : 31268 : get_uleb128 (u128, linep, lineendp);
9025 : 31268 : printf (_(" set file to %" PRIu64 "\n"),
9026 : : (uint64_t) u128);
9027 : : break;
9028 : :
9029 : 284601 : case DW_LNS_set_column:
9030 : : /* Takes one uleb128 parameter which is stored in column. */
9031 [ + - - + ]: 284601 : if (unlikely (standard_opcode_lengths[opcode] != 1
9032 : : || lineendp - linep < 1))
9033 : 0 : goto invalid_unit;
9034 : :
9035 : 284601 : get_uleb128 (u128, linep, lineendp);
9036 : 284601 : printf (_(" set column to %" PRIu64 "\n"),
9037 : : (uint64_t) u128);
9038 : : break;
9039 : :
9040 : 178885 : case DW_LNS_negate_stmt:
9041 : : /* Takes no argument. */
9042 : 178885 : is_stmt = 1 - is_stmt;
9043 : 178885 : printf (_(" set '%s' to %" PRIuFAST8 "\n"),
9044 : : "is_stmt", is_stmt);
9045 : : break;
9046 : :
9047 : 0 : case DW_LNS_set_basic_block:
9048 : : /* Takes no argument. */
9049 : 0 : puts (_(" set basic block flag"));
9050 : 0 : break;
9051 : :
9052 : 228 : case DW_LNS_const_add_pc:
9053 : : /* Takes no argument. */
9054 : :
9055 [ - + ]: 228 : if (unlikely (line_range == 0))
9056 : 0 : goto invalid_unit;
9057 : :
9058 : 228 : advance_pc ((255 - opcode_base) / line_range);
9059 : : {
9060 : 228 : printf (_(" advance address by constant %u to "),
9061 : : op_addr_advance);
9062 : 228 : print_dwarf_addr (dwflmod, 0, address, address);
9063 [ - + ]: 228 : if (op_index > 0)
9064 : 0 : printf (_(", op_index to %u"), op_index);
9065 : 228 : printf ("\n");
9066 : : }
9067 : : break;
9068 : :
9069 : 22 : case DW_LNS_fixed_advance_pc:
9070 : : /* Takes one 16 bit parameter which is added to the
9071 : : address. */
9072 [ + - - + ]: 22 : if (unlikely (standard_opcode_lengths[opcode] != 1
9073 : : || lineendp - linep < 2))
9074 : 0 : goto invalid_unit;
9075 : :
9076 [ - + ]: 22 : u128 = read_2ubyte_unaligned_inc (dbg, linep);
9077 : 22 : address += u128;
9078 : 22 : op_index = 0;
9079 : : {
9080 : 22 : printf (_("\
9081 : : advance address by fixed value %u to \n"),
9082 : : u128);
9083 : 22 : print_dwarf_addr (dwflmod, 0, address, address);
9084 : 22 : printf ("\n");
9085 : : }
9086 : : break;
9087 : :
9088 : 1 : case DW_LNS_set_prologue_end:
9089 : : /* Takes no argument. */
9090 : 1 : puts (_(" set prologue end flag"));
9091 : 1 : break;
9092 : :
9093 : 0 : case DW_LNS_set_epilogue_begin:
9094 : : /* Takes no argument. */
9095 : 0 : puts (_(" set epilogue begin flag"));
9096 : 0 : break;
9097 : :
9098 : 0 : case DW_LNS_set_isa:
9099 : : /* Takes one uleb128 parameter which is stored in isa. */
9100 [ # # # # ]: 0 : if (unlikely (standard_opcode_lengths[opcode] != 1
9101 : : || lineendp - linep < 1))
9102 : 0 : goto invalid_unit;
9103 : :
9104 : 0 : get_uleb128 (u128, linep, lineendp);
9105 [ + + ]: 1117166 : printf (_(" set isa to %u\n"), u128);
9106 : : break;
9107 : : }
9108 : : }
9109 : : else
9110 : : {
9111 : : /* This is a new opcode the generator but not we know about.
9112 : : Read the parameters associated with it but then discard
9113 : : everything. Read all the parameters for this opcode. */
9114 : 0 : printf (ngettext (" unknown opcode with %" PRIu8 " parameter:",
9115 : : " unknown opcode with %" PRIu8 " parameters:",
9116 : : standard_opcode_lengths[opcode]),
9117 : 0 : standard_opcode_lengths[opcode]);
9118 : 0 : for (int n = standard_opcode_lengths[opcode];
9119 [ # # # # ]: 0 : n > 0 && linep < lineendp; --n)
9120 : : {
9121 : 0 : get_uleb128 (u128, linep, lineendp);
9122 [ # # ]: 0 : if (n != standard_opcode_lengths[opcode])
9123 [ # # ]: 0 : putc_unlocked (',', stdout);
9124 : 0 : printf (" %u", u128);
9125 : : }
9126 : :
9127 : : /* Next round, ignore this opcode. */
9128 : 0 : continue;
9129 : : }
9130 : : }
9131 : : }
9132 : :
9133 : : /* There must only be one data block. */
9134 [ - + ]: 48 : assert (elf_getdata (scn, data) == NULL);
9135 : : }
9136 : :
9137 : :
9138 : : static void
9139 : 4 : print_debug_loclists_section (Dwfl_Module *dwflmod,
9140 : : Ebl *ebl,
9141 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
9142 : : Elf_Scn *scn, GElf_Shdr *shdr,
9143 : : Dwarf *dbg)
9144 : : {
9145 : 4 : printf (_("\
9146 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
9147 : : elf_ndxscn (scn), section_name (ebl, shdr),
9148 : 4 : (uint64_t) shdr->sh_offset);
9149 : :
9150 : 8 : Elf_Data *data = (dbg->sectiondata[IDX_debug_loclists]
9151 [ - + ]: 4 : ?: elf_rawdata (scn, NULL));
9152 [ - + ]: 4 : if (unlikely (data == NULL))
9153 : : {
9154 : 0 : error (0, 0, _("cannot get .debug_loclists content: %s"),
9155 : : elf_errmsg (-1));
9156 : 0 : return;
9157 : : }
9158 : :
9159 : : /* For the listptr to get the base address/CU. */
9160 : 4 : sort_listptr (&known_loclistsptr, "loclistsptr");
9161 : 4 : size_t listptr_idx = 0;
9162 : :
9163 : 4 : const unsigned char *readp = data->d_buf;
9164 : 4 : const unsigned char *const dataend = ((unsigned char *) data->d_buf
9165 : 4 : + data->d_size);
9166 [ + + ]: 9 : while (readp < dataend)
9167 : : {
9168 [ - + ]: 5 : if (unlikely (readp > dataend - 4))
9169 : : {
9170 : 0 : invalid_data:
9171 : 0 : error (0, 0, _("invalid data in section [%zu] '%s'"),
9172 : : elf_ndxscn (scn), section_name (ebl, shdr));
9173 : 0 : return;
9174 : : }
9175 : :
9176 : 5 : ptrdiff_t offset = readp - (unsigned char *) data->d_buf;
9177 : 5 : printf (_("Table at Offset 0x%" PRIx64 ":\n\n"),
9178 : : (uint64_t) offset);
9179 : :
9180 [ - + ]: 5 : uint64_t unit_length = read_4ubyte_unaligned_inc (dbg, readp);
9181 : 5 : unsigned int offset_size = 4;
9182 [ - + ]: 5 : if (unlikely (unit_length == 0xffffffff))
9183 : : {
9184 [ # # ]: 0 : if (unlikely (readp > dataend - 8))
9185 : 0 : goto invalid_data;
9186 : :
9187 [ # # ]: 0 : unit_length = read_8ubyte_unaligned_inc (dbg, readp);
9188 : 0 : offset_size = 8;
9189 : : }
9190 : 5 : printf (_(" Length: %8" PRIu64 "\n"), unit_length);
9191 : :
9192 : : /* We need at least 2-bytes + 1-byte + 1-byte + 4-bytes = 8
9193 : : bytes to complete the header. And this unit cannot go beyond
9194 : : the section data. */
9195 [ + - ]: 5 : if (readp > dataend - 8
9196 [ + - ]: 5 : || unit_length < 8
9197 [ - + ]: 5 : || unit_length > (uint64_t) (dataend - readp))
9198 : 0 : goto invalid_data;
9199 : :
9200 : 5 : const unsigned char *nexthdr = readp + unit_length;
9201 : :
9202 [ - + ]: 5 : uint16_t version = read_2ubyte_unaligned_inc (dbg, readp);
9203 : 5 : printf (_(" DWARF version: %8" PRIu16 "\n"), version);
9204 : :
9205 [ - + ]: 5 : if (version != 5)
9206 : : {
9207 : 0 : error (0, 0, _("Unknown version"));
9208 : 0 : goto next_table;
9209 : : }
9210 : :
9211 : 5 : uint8_t address_size = *readp++;
9212 : 5 : printf (_(" Address size: %8" PRIu64 "\n"),
9213 : : (uint64_t) address_size);
9214 : :
9215 [ - + ]: 5 : if (address_size != 4 && address_size != 8)
9216 : : {
9217 : 0 : error (0, 0, _("unsupported address size"));
9218 : 0 : goto next_table;
9219 : : }
9220 : :
9221 : 5 : uint8_t segment_size = *readp++;
9222 : 5 : printf (_(" Segment size: %8" PRIu64 "\n"),
9223 : : (uint64_t) segment_size);
9224 : :
9225 [ - + ]: 5 : if (segment_size != 0)
9226 : : {
9227 : 0 : error (0, 0, _("unsupported segment size"));
9228 : 0 : goto next_table;
9229 : : }
9230 : :
9231 [ - + ]: 5 : uint32_t offset_entry_count = read_4ubyte_unaligned_inc (dbg, readp);
9232 : 5 : printf (_(" Offset entries: %8" PRIu64 "\n"),
9233 : : (uint64_t) offset_entry_count);
9234 : :
9235 : : /* We need the CU that uses this unit to get the initial base address. */
9236 : 5 : Dwarf_Addr cu_base = 0;
9237 : 5 : struct Dwarf_CU *cu = NULL;
9238 [ - + ]: 5 : if (listptr_cu (&known_loclistsptr, &listptr_idx,
9239 : : (Dwarf_Off) offset,
9240 : 5 : (Dwarf_Off) (nexthdr - (unsigned char *) data->d_buf),
9241 : : &cu_base, &cu)
9242 [ # # ]: 0 : || split_dwarf_cu_base (dbg, &cu, &cu_base))
9243 : 5 : {
9244 : 5 : Dwarf_Die cudie;
9245 [ - + ]: 5 : if (dwarf_cu_die (cu, &cudie,
9246 : : NULL, NULL, NULL, NULL,
9247 : : NULL, NULL) == NULL)
9248 : 0 : printf (_(" Unknown CU base: "));
9249 : : else
9250 : 5 : printf (_(" CU [%6" PRIx64 "] base: "),
9251 : : dwarf_dieoffset (&cudie));
9252 : 5 : print_dwarf_addr (dwflmod, address_size, cu_base, cu_base);
9253 : 5 : printf ("\n");
9254 : : }
9255 : : else
9256 : 0 : printf (_(" Not associated with a CU.\n"));
9257 : :
9258 : 5 : printf ("\n");
9259 : :
9260 : 5 : const unsigned char *offset_array_start = readp;
9261 [ + + ]: 5 : if (offset_entry_count > 0)
9262 : : {
9263 : 2 : uint64_t max_entries = (unit_length - 8) / offset_size;
9264 [ - + ]: 2 : if (offset_entry_count > max_entries)
9265 : : {
9266 : 0 : error (0, 0,
9267 : 0 : _("too many offset entries for unit length"));
9268 : 0 : offset_entry_count = max_entries;
9269 : : }
9270 : :
9271 : 2 : printf (_(" Offsets starting at 0x%" PRIx64 ":\n"),
9272 : : (uint64_t) (offset_array_start
9273 : 2 : - (unsigned char *) data->d_buf));
9274 [ + + ]: 18 : for (uint32_t idx = 0; idx < offset_entry_count; idx++)
9275 : : {
9276 : 16 : printf (" [%6" PRIu32 "] ", idx);
9277 [ + - ]: 16 : if (offset_size == 4)
9278 : : {
9279 [ - + ]: 16 : uint32_t off = read_4ubyte_unaligned_inc (dbg, readp);
9280 : 16 : printf ("0x%" PRIx32 "\n", off);
9281 : : }
9282 : : else
9283 : : {
9284 [ # # ]: 0 : uint64_t off = read_8ubyte_unaligned_inc (dbg, readp);
9285 : 16 : printf ("0x%" PRIx64 "\n", off);
9286 : : }
9287 : : }
9288 : 2 : printf ("\n");
9289 : : }
9290 : :
9291 : 5 : Dwarf_Addr base = cu_base;
9292 : 5 : bool start_of_list = true;
9293 : 5 : while (readp < nexthdr)
9294 : : {
9295 : 92 : Dwarf_Off off = (Dwarf_Off) (readp - (unsigned char *) data->d_buf);
9296 [ + + ]: 92 : if (listptr_attr (&known_loclistsptr, listptr_idx, off,
9297 : : DW_AT_GNU_locviews))
9298 : : {
9299 : 1 : Dwarf_Off next_off = next_listptr_offset (&known_loclistsptr,
9300 : : &listptr_idx, off);
9301 : 1 : const unsigned char *locp = readp;
9302 : 1 : const unsigned char *locendp;
9303 [ + - ]: 1 : if (next_off == 0
9304 : 1 : || next_off > (size_t) (nexthdr - ((const unsigned char *)
9305 [ + - ]: 1 : data->d_buf)))
9306 : : locendp = nexthdr;
9307 : : else
9308 : 1 : locendp = (const unsigned char *) data->d_buf + next_off;
9309 : :
9310 : 3 : printf (" Offset: %" PRIx64 ", Index: %" PRIx64 "\n",
9311 : 1 : (uint64_t) (readp - (unsigned char *) data->d_buf),
9312 : 1 : (uint64_t) (readp - offset_array_start));
9313 : :
9314 : 2 : while (locp < locendp)
9315 : : {
9316 : 1 : uint64_t v1, v2;
9317 : 1 : get_uleb128 (v1, locp, locendp);
9318 [ - + ]: 1 : if (locp >= locendp)
9319 : : {
9320 : 0 : printf (_(" <INVALID DATA>\n"));
9321 : : break;
9322 : : }
9323 : 1 : get_uleb128 (v2, locp, locendp);
9324 [ + + ]: 2 : printf (" view pair %" PRId64 ", %" PRId64 "\n", v1, v2);
9325 : : }
9326 : :
9327 : 1 : printf ("\n");
9328 : 1 : readp = (unsigned char *) locendp;
9329 : 1 : continue;
9330 : : }
9331 : :
9332 : 91 : uint8_t kind = *readp++;
9333 : 91 : uint64_t op1, op2, len;
9334 : :
9335 : : /* Skip padding. */
9336 [ - + ]: 91 : if (start_of_list && kind == DW_LLE_end_of_list)
9337 : 0 : continue;
9338 : :
9339 [ + + ]: 91 : if (start_of_list)
9340 : : {
9341 : 33 : base = cu_base;
9342 : 33 : printf (" Offset: %" PRIx64 ", Index: %" PRIx64 "\n",
9343 : 33 : (uint64_t) (readp - (unsigned char *) data->d_buf - 1),
9344 : 33 : (uint64_t) (readp - offset_array_start - 1));
9345 : 33 : start_of_list = false;
9346 : : }
9347 : :
9348 : 91 : printf (" %s", dwarf_loc_list_encoding_name (kind));
9349 [ + - - + : 91 : switch (kind)
+ - + - +
- ]
9350 : : {
9351 : 33 : case DW_LLE_end_of_list:
9352 : 33 : start_of_list = true;
9353 : 33 : printf ("\n\n");
9354 : : break;
9355 : :
9356 : 0 : case DW_LLE_base_addressx:
9357 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
9358 : : {
9359 : 0 : invalid_entry:
9360 : 0 : error (0, 0, _("invalid loclists data"));
9361 : 0 : goto next_table;
9362 : : }
9363 : 0 : get_uleb128 (op1, readp, nexthdr);
9364 : 0 : printf (" %" PRIx64 "\n", op1);
9365 [ # # ]: 0 : if (! print_unresolved_addresses)
9366 : : {
9367 : 0 : Dwarf_Addr addr;
9368 [ # # ]: 0 : if (get_indexed_addr (cu, op1, &addr) != 0)
9369 : 0 : printf (" ???\n");
9370 : : else
9371 : : {
9372 : 0 : printf (" ");
9373 : 0 : print_dwarf_addr (dwflmod, address_size, addr, addr);
9374 : 0 : printf ("\n");
9375 : : }
9376 : : }
9377 : : break;
9378 : :
9379 : 0 : case DW_LLE_startx_endx:
9380 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
9381 : 0 : goto invalid_entry;
9382 : 0 : get_uleb128 (op1, readp, nexthdr);
9383 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
9384 : 0 : goto invalid_entry;
9385 : 0 : get_uleb128 (op2, readp, nexthdr);
9386 : 0 : printf (" %" PRIx64 ", %" PRIx64 "\n", op1, op2);
9387 [ # # ]: 0 : if (! print_unresolved_addresses)
9388 : : {
9389 : 0 : Dwarf_Addr addr1;
9390 : 0 : Dwarf_Addr addr2;
9391 [ # # ]: 0 : if (get_indexed_addr (cu, op1, &addr1) != 0
9392 [ # # ]: 0 : || get_indexed_addr (cu, op2, &addr2) != 0)
9393 : : {
9394 : 0 : printf (" ???..\n");
9395 : 0 : printf (" ???\n");
9396 : : }
9397 : : else
9398 : : {
9399 : 0 : printf (" ");
9400 : 0 : print_dwarf_addr (dwflmod, address_size, addr1, addr1);
9401 : 0 : printf ("..\n ");
9402 : 0 : print_dwarf_addr (dwflmod, address_size,
9403 : : addr2 - 1, addr2);
9404 : 0 : printf ("\n");
9405 : : }
9406 : : }
9407 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
9408 : 0 : goto invalid_entry;
9409 : 0 : get_uleb128 (len, readp, nexthdr);
9410 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < len)
9411 : 0 : goto invalid_entry;
9412 : 0 : print_ops (dwflmod, dbg, 8, 8, version,
9413 : : address_size, offset_size, cu, len, readp);
9414 : 0 : readp += len;
9415 : 0 : break;
9416 : :
9417 : 26 : case DW_LLE_startx_length:
9418 [ - + ]: 26 : if ((uint64_t) (nexthdr - readp) < 1)
9419 : 0 : goto invalid_entry;
9420 : 26 : get_uleb128 (op1, readp, nexthdr);
9421 [ - + ]: 26 : if ((uint64_t) (nexthdr - readp) < 1)
9422 : 0 : goto invalid_entry;
9423 : 26 : get_uleb128 (op2, readp, nexthdr);
9424 : 26 : printf (" %" PRIx64 ", %" PRIx64 "\n", op1, op2);
9425 [ + - ]: 26 : if (! print_unresolved_addresses)
9426 : : {
9427 : 26 : Dwarf_Addr addr1;
9428 : 26 : Dwarf_Addr addr2;
9429 [ - + ]: 26 : if (get_indexed_addr (cu, op1, &addr1) != 0)
9430 : : {
9431 : 0 : printf (" ???..\n");
9432 : 0 : printf (" ???\n");
9433 : : }
9434 : : else
9435 : : {
9436 : 26 : addr2 = addr1 + op2;
9437 : 26 : printf (" ");
9438 : 26 : print_dwarf_addr (dwflmod, address_size, addr1, addr1);
9439 : 26 : printf ("..\n ");
9440 : 26 : print_dwarf_addr (dwflmod, address_size,
9441 : : addr2 - 1, addr2);
9442 : 26 : printf ("\n");
9443 : : }
9444 : : }
9445 [ - + ]: 26 : if ((uint64_t) (nexthdr - readp) < 1)
9446 : 0 : goto invalid_entry;
9447 : 26 : get_uleb128 (len, readp, nexthdr);
9448 [ - + ]: 26 : if ((uint64_t) (nexthdr - readp) < len)
9449 : 0 : goto invalid_entry;
9450 : 26 : print_ops (dwflmod, dbg, 8, 8, version,
9451 : : address_size, offset_size, cu, len, readp);
9452 : 26 : readp += len;
9453 : 26 : break;
9454 : :
9455 : 24 : case DW_LLE_offset_pair:
9456 [ - + ]: 24 : if ((uint64_t) (nexthdr - readp) < 1)
9457 : 0 : goto invalid_entry;
9458 : 24 : get_uleb128 (op1, readp, nexthdr);
9459 [ - + ]: 24 : if ((uint64_t) (nexthdr - readp) < 1)
9460 : 0 : goto invalid_entry;
9461 : 24 : get_uleb128 (op2, readp, nexthdr);
9462 : 24 : printf (" %" PRIx64 ", %" PRIx64 "\n", op1, op2);
9463 [ + - ]: 24 : if (! print_unresolved_addresses)
9464 : : {
9465 : 24 : op1 += base;
9466 : 24 : op2 += base;
9467 : 24 : printf (" ");
9468 : 24 : print_dwarf_addr (dwflmod, address_size, op1, op1);
9469 : 24 : printf ("..\n ");
9470 : 24 : print_dwarf_addr (dwflmod, address_size, op2 - 1, op2);
9471 : 24 : printf ("\n");
9472 : : }
9473 [ - + ]: 24 : if ((uint64_t) (nexthdr - readp) < 1)
9474 : 0 : goto invalid_entry;
9475 : 24 : get_uleb128 (len, readp, nexthdr);
9476 [ - + ]: 24 : if ((uint64_t) (nexthdr - readp) < len)
9477 : 0 : goto invalid_entry;
9478 : 24 : print_ops (dwflmod, dbg, 8, 8, version,
9479 : : address_size, offset_size, cu, len, readp);
9480 : 24 : readp += len;
9481 : 24 : break;
9482 : :
9483 : 0 : case DW_LLE_default_location:
9484 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
9485 : 0 : goto invalid_entry;
9486 : 0 : get_uleb128 (len, readp, nexthdr);
9487 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < len)
9488 : 0 : goto invalid_entry;
9489 : 0 : print_ops (dwflmod, dbg, 8, 8, version,
9490 : : address_size, offset_size, cu, len, readp);
9491 : 0 : readp += len;
9492 : 0 : break;
9493 : :
9494 : 5 : case DW_LLE_base_address:
9495 [ - + ]: 5 : if (address_size == 4)
9496 : : {
9497 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 4)
9498 : 0 : goto invalid_entry;
9499 [ # # ]: 0 : op1 = read_4ubyte_unaligned_inc (dbg, readp);
9500 : : }
9501 : : else
9502 : : {
9503 [ - + ]: 5 : if ((uint64_t) (nexthdr - readp) < 8)
9504 : 0 : goto invalid_entry;
9505 [ - + ]: 10 : op1 = read_8ubyte_unaligned_inc (dbg, readp);
9506 : : }
9507 : 5 : base = op1;
9508 : 5 : printf (" 0x%" PRIx64 "\n", base);
9509 [ + - ]: 5 : if (! print_unresolved_addresses)
9510 : : {
9511 : 5 : printf (" ");
9512 : 5 : print_dwarf_addr (dwflmod, address_size, base, base);
9513 [ + + ]: 102 : printf ("\n");
9514 : : }
9515 : : break;
9516 : :
9517 : 0 : case DW_LLE_start_end:
9518 [ # # ]: 0 : if (address_size == 4)
9519 : : {
9520 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 8)
9521 : 0 : goto invalid_entry;
9522 [ # # ]: 0 : op1 = read_4ubyte_unaligned_inc (dbg, readp);
9523 [ # # ]: 0 : op2 = read_4ubyte_unaligned_inc (dbg, readp);
9524 : : }
9525 : : else
9526 : : {
9527 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 16)
9528 : 0 : goto invalid_entry;
9529 [ # # ]: 0 : op1 = read_8ubyte_unaligned_inc (dbg, readp);
9530 [ # # ]: 0 : op2 = read_8ubyte_unaligned_inc (dbg, readp);
9531 : : }
9532 : 0 : printf (" 0x%" PRIx64 "..0x%" PRIx64 "\n", op1, op2);
9533 [ # # ]: 0 : if (! print_unresolved_addresses)
9534 : : {
9535 : 0 : printf (" ");
9536 : 0 : print_dwarf_addr (dwflmod, address_size, op1, op1);
9537 : 0 : printf ("..\n ");
9538 : 0 : print_dwarf_addr (dwflmod, address_size, op2 - 1, op2);
9539 : 0 : printf ("\n");
9540 : : }
9541 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
9542 : 0 : goto invalid_entry;
9543 : 0 : get_uleb128 (len, readp, nexthdr);
9544 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < len)
9545 : 0 : goto invalid_entry;
9546 : 0 : print_ops (dwflmod, dbg, 8, 8, version,
9547 : : address_size, offset_size, cu, len, readp);
9548 : 0 : readp += len;
9549 : 0 : break;
9550 : :
9551 : 3 : case DW_LLE_start_length:
9552 [ - + ]: 3 : if (address_size == 4)
9553 : : {
9554 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 4)
9555 : 0 : goto invalid_entry;
9556 [ # # ]: 0 : op1 = read_4ubyte_unaligned_inc (dbg, readp);
9557 : : }
9558 : : else
9559 : : {
9560 [ - + ]: 3 : if ((uint64_t) (nexthdr - readp) < 8)
9561 : 0 : goto invalid_entry;
9562 [ - + ]: 6 : op1 = read_8ubyte_unaligned_inc (dbg, readp);
9563 : : }
9564 [ - + ]: 3 : if ((uint64_t) (nexthdr - readp) < 1)
9565 : 0 : goto invalid_entry;
9566 : 3 : get_uleb128 (op2, readp, nexthdr);
9567 : 3 : printf (" 0x%" PRIx64 ", %" PRIx64 "\n", op1, op2);
9568 [ + + ]: 3 : if (! print_unresolved_addresses)
9569 : : {
9570 : 2 : op2 = op1 + op2;
9571 : 2 : printf (" ");
9572 : 2 : print_dwarf_addr (dwflmod, address_size, op1, op1);
9573 : 2 : printf ("..\n ");
9574 : 2 : print_dwarf_addr (dwflmod, address_size, op2 - 1, op2);
9575 : 2 : printf ("\n");
9576 : : }
9577 [ - + ]: 3 : if ((uint64_t) (nexthdr - readp) < 1)
9578 : 0 : goto invalid_entry;
9579 : 3 : get_uleb128 (len, readp, nexthdr);
9580 [ - + ]: 3 : if ((uint64_t) (nexthdr - readp) < len)
9581 : 0 : goto invalid_entry;
9582 : 3 : print_ops (dwflmod, dbg, 8, 8, version,
9583 : : address_size, offset_size, cu, len, readp);
9584 : 3 : readp += len;
9585 : 3 : break;
9586 : :
9587 : 0 : default:
9588 : 0 : goto invalid_entry;
9589 : : }
9590 : : }
9591 : :
9592 : 5 : next_table:
9593 [ - + ]: 5 : if (readp != nexthdr)
9594 : : {
9595 : 0 : size_t padding = nexthdr - readp;
9596 : 0 : printf (_(" %zu padding bytes\n\n"), padding);
9597 : 0 : readp = nexthdr;
9598 : : }
9599 : : }
9600 : : }
9601 : :
9602 : :
9603 : : static void
9604 : 42 : print_debug_loc_section (Dwfl_Module *dwflmod,
9605 : : Ebl *ebl, GElf_Ehdr *ehdr,
9606 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
9607 : : {
9608 : 84 : Elf_Data *data = (dbg->sectiondata[IDX_debug_loc]
9609 [ - + ]: 42 : ?: elf_rawdata (scn, NULL));
9610 : :
9611 [ - + ]: 42 : if (unlikely (data == NULL))
9612 : : {
9613 : 0 : error (0, 0, _("cannot get .debug_loc content: %s"),
9614 : : elf_errmsg (-1));
9615 : 0 : return;
9616 : : }
9617 : :
9618 : 42 : printf (_("\
9619 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
9620 : : elf_ndxscn (scn), section_name (ebl, shdr),
9621 : 42 : (uint64_t) shdr->sh_offset);
9622 : :
9623 : 42 : sort_listptr (&known_locsptr, "loclistptr");
9624 : 42 : size_t listptr_idx = 0;
9625 : :
9626 [ + - ]: 42 : uint_fast8_t address_size = ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 4 : 8;
9627 : 42 : uint_fast8_t offset_size = 4;
9628 : :
9629 : 42 : bool first = true;
9630 : 42 : Dwarf_Addr base = 0;
9631 : 42 : unsigned char *readp = data->d_buf;
9632 : 42 : unsigned char *const endp = (unsigned char *) data->d_buf + data->d_size;
9633 : 42 : Dwarf_CU *last_cu = NULL;
9634 [ + + ]: 380138 : while (readp < endp)
9635 : : {
9636 : 380096 : ptrdiff_t offset = readp - (unsigned char *) data->d_buf;
9637 : 380096 : Dwarf_CU *cu = last_cu;
9638 : 380096 : unsigned int attr = 0;
9639 : :
9640 [ + + - + ]: 380096 : if (first && skip_listptr_hole (&known_locsptr, &listptr_idx,
9641 : : &address_size, &offset_size, &base,
9642 : : &cu, offset, &readp, endp, &attr))
9643 : 53258 : continue;
9644 : :
9645 [ + + ]: 380096 : if (last_cu != cu)
9646 : : {
9647 : 1603 : Dwarf_Die cudie;
9648 [ - + ]: 1603 : if (dwarf_cu_die (cu, &cudie,
9649 : : NULL, NULL, NULL, NULL,
9650 : : NULL, NULL) == NULL)
9651 : 0 : printf (_("\n Unknown CU base: "));
9652 : : else
9653 : 1603 : printf (_("\n CU [%6" PRIx64 "] base: "),
9654 : : dwarf_dieoffset (&cudie));
9655 : 1603 : print_dwarf_addr (dwflmod, address_size, base, base);
9656 : 1603 : printf ("\n");
9657 : : }
9658 : 380096 : last_cu = cu;
9659 : :
9660 [ + + ]: 380096 : if (attr == DW_AT_GNU_locviews)
9661 : : {
9662 : 53258 : Dwarf_Off next_off = next_listptr_offset (&known_locsptr,
9663 : : &listptr_idx, offset);
9664 : 53258 : const unsigned char *locp = readp;
9665 : 53258 : const unsigned char *locendp;
9666 [ + - ]: 53258 : if (next_off == 0
9667 : 53258 : || next_off > (size_t) (endp
9668 [ + - ]: 53258 : - (const unsigned char *) data->d_buf))
9669 : 53258 : locendp = endp;
9670 : : else
9671 : 53258 : locendp = (const unsigned char *) data->d_buf + next_off;
9672 : :
9673 [ + + ]: 326619 : while (locp < locendp)
9674 : : {
9675 : 273361 : uint64_t v1, v2;
9676 : 273361 : get_uleb128 (v1, locp, locendp);
9677 [ - + ]: 273361 : if (locp >= locendp)
9678 : : {
9679 : 0 : printf (_(" [%6tx] <INVALID DATA>\n"), offset);
9680 : : break;
9681 : : }
9682 : 273361 : get_uleb128 (v2, locp, locendp);
9683 [ + + ]: 273361 : if (first) /* First view pair in a list. */
9684 : 53258 : printf (" [%6tx] ", offset);
9685 : : else
9686 : 220103 : printf (" ");
9687 : 273361 : printf ("view pair %" PRId64 ", %" PRId64 "\n", v1, v2);
9688 : 273361 : first = false;
9689 : : }
9690 : :
9691 : 53258 : first = true;
9692 : 53258 : readp = (unsigned char *) locendp;
9693 : 53258 : continue;
9694 : : }
9695 : :
9696 : : /* GNU DebugFission encoded addresses as addrx. */
9697 : 653676 : bool is_debugfission = ((cu != NULL
9698 [ # # ]: 0 : || split_dwarf_cu_base (dbg, &cu, &base))
9699 [ - + + - ]: 326838 : && (cu->version < 5
9700 [ + + ]: 326838 : && cu->unit_type == DW_UT_split_compile));
9701 [ + + ]: 326838 : if (!is_debugfission
9702 [ - + ]: 326778 : && unlikely (data->d_size - offset < (size_t) address_size * 2))
9703 : : {
9704 : 0 : invalid_data:
9705 : 0 : printf (_(" [%6tx] <INVALID DATA>\n"), offset);
9706 : 0 : break;
9707 : : }
9708 : :
9709 : 326838 : Dwarf_Addr begin;
9710 : 326838 : Dwarf_Addr end;
9711 : 326838 : bool use_base = true;
9712 [ + + ]: 326838 : if (is_debugfission)
9713 : : {
9714 : 60 : const unsigned char *locp = readp;
9715 : 60 : const unsigned char *locendp = readp + data->d_size;
9716 [ - + ]: 60 : if (locp >= locendp)
9717 : 0 : goto invalid_data;
9718 : :
9719 : 60 : Dwarf_Word idx;
9720 : 60 : unsigned char code = *locp++;
9721 [ + - - + : 60 : switch (code)
- ]
9722 : : {
9723 : 20 : case DW_LLE_GNU_end_of_list_entry:
9724 : 20 : begin = 0;
9725 : 20 : end = 0;
9726 : 20 : break;
9727 : :
9728 : 0 : case DW_LLE_GNU_base_address_selection_entry:
9729 [ # # ]: 0 : if (locp >= locendp)
9730 : 0 : goto invalid_data;
9731 : 0 : begin = (Dwarf_Addr) -1;
9732 : 0 : get_uleb128 (idx, locp, locendp);
9733 [ # # ]: 0 : if (get_indexed_addr (cu, idx, &end) != 0)
9734 : 0 : end = idx; /* ... */
9735 : : break;
9736 : :
9737 : 0 : case DW_LLE_GNU_start_end_entry:
9738 [ # # ]: 0 : if (locp >= locendp)
9739 : 0 : goto invalid_data;
9740 : 0 : get_uleb128 (idx, locp, locendp);
9741 [ # # ]: 0 : if (get_indexed_addr (cu, idx, &begin) != 0)
9742 : 0 : begin = idx; /* ... */
9743 [ # # ]: 0 : if (locp >= locendp)
9744 : 0 : goto invalid_data;
9745 : 0 : get_uleb128 (idx, locp, locendp);
9746 [ # # ]: 0 : if (get_indexed_addr (cu, idx, &end) != 0)
9747 : 0 : end = idx; /* ... */
9748 : : use_base = false;
9749 : : break;
9750 : :
9751 : 40 : case DW_LLE_GNU_start_length_entry:
9752 [ - + ]: 40 : if (locp >= locendp)
9753 : 0 : goto invalid_data;
9754 : 40 : get_uleb128 (idx, locp, locendp);
9755 [ - + ]: 40 : if (get_indexed_addr (cu, idx, &begin) != 0)
9756 : 0 : begin = idx; /* ... */
9757 [ - + ]: 40 : if (locendp - locp < 4)
9758 : 0 : goto invalid_data;
9759 [ - + ]: 40 : end = read_4ubyte_unaligned_inc (dbg, locp);
9760 : 40 : end += begin;
9761 : 40 : use_base = false;
9762 : 40 : break;
9763 : :
9764 : 0 : default:
9765 : 0 : goto invalid_data;
9766 : : }
9767 : :
9768 : 60 : readp = (unsigned char *) locp;
9769 : : }
9770 [ + - ]: 326778 : else if (address_size == 8)
9771 : : {
9772 [ + + ]: 326778 : begin = read_8ubyte_unaligned_inc (dbg, readp);
9773 [ + + ]: 653556 : end = read_8ubyte_unaligned_inc (dbg, readp);
9774 : : }
9775 : : else
9776 : : {
9777 [ # # ]: 0 : begin = read_4ubyte_unaligned_inc (dbg, readp);
9778 [ # # ]: 0 : end = read_4ubyte_unaligned_inc (dbg, readp);
9779 [ # # ]: 0 : if (begin == (Dwarf_Addr) (uint32_t) -1)
9780 : 0 : begin = (Dwarf_Addr) -1l;
9781 : : }
9782 : :
9783 [ - + ]: 326838 : if (begin == (Dwarf_Addr) -1l) /* Base address entry. */
9784 : : {
9785 [ # # ]: 0 : if (first)
9786 : 0 : printf (" [%6tx] ", offset);
9787 : : else
9788 : 0 : printf (" ");
9789 : 0 : puts (_("base address"));
9790 : 0 : printf (" ");
9791 : 0 : print_dwarf_addr (dwflmod, address_size, end, end);
9792 : 0 : printf ("\n");
9793 : 0 : base = end;
9794 : 0 : first = false;
9795 : : }
9796 [ + + + + ]: 326838 : else if (begin == 0 && end == 0) /* End of list entry. */
9797 : : {
9798 [ - + ]: 53332 : if (first)
9799 : 326838 : printf (_(" [%6tx] empty list\n"), offset);
9800 : : first = true;
9801 : : }
9802 : : else
9803 : : {
9804 : : /* We have a location expression entry. */
9805 [ + + ]: 273506 : uint_fast16_t len = read_2ubyte_unaligned_inc (dbg, readp);
9806 : :
9807 [ + + ]: 273506 : if (first) /* First entry in a list. */
9808 : 53332 : printf (" [%6tx] ", offset);
9809 : : else
9810 : 220174 : printf (" ");
9811 : :
9812 : 273506 : printf ("range %" PRIx64 ", %" PRIx64 "\n", begin, end);
9813 [ + + ]: 273506 : if (! print_unresolved_addresses)
9814 : : {
9815 [ + + ]: 273476 : Dwarf_Addr dab = use_base ? base + begin : begin;
9816 [ + + ]: 273476 : Dwarf_Addr dae = use_base ? base + end : end;
9817 : 273476 : printf (" ");
9818 : 273476 : print_dwarf_addr (dwflmod, address_size, dab, dab);
9819 : 273476 : printf ("..\n ");
9820 : 273476 : print_dwarf_addr (dwflmod, address_size, dae - 1, dae);
9821 : 273476 : printf ("\n");
9822 : : }
9823 : :
9824 [ - + ]: 273506 : if (endp - readp <= (ptrdiff_t) len)
9825 : : {
9826 : 0 : fputs (_(" <INVALID DATA>\n"), stdout);
9827 : 0 : break;
9828 : : }
9829 : :
9830 : 273506 : print_ops (dwflmod, dbg, 11, 11,
9831 [ + - ]: 273506 : cu != NULL ? cu->version : 3,
9832 : : address_size, offset_size, cu, len, readp);
9833 : :
9834 : 273506 : first = false;
9835 : 273506 : readp += len;
9836 : : }
9837 : : }
9838 : : }
9839 : :
9840 : : struct mac_culist
9841 : : {
9842 : : Dwarf_Die die;
9843 : : Dwarf_Off offset;
9844 : : Dwarf_Files *files;
9845 : : struct mac_culist *next;
9846 : : };
9847 : :
9848 : :
9849 : : static int
9850 : 0 : mac_compare (const void *p1, const void *p2)
9851 : : {
9852 : 0 : struct mac_culist *m1 = (struct mac_culist *) p1;
9853 : 0 : struct mac_culist *m2 = (struct mac_culist *) p2;
9854 : :
9855 [ # # ]: 0 : if (m1->offset < m2->offset)
9856 : : return -1;
9857 [ # # ]: 0 : if (m1->offset > m2->offset)
9858 : 0 : return 1;
9859 : : return 0;
9860 : : }
9861 : :
9862 : :
9863 : : static void
9864 : 0 : print_debug_macinfo_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
9865 : : Ebl *ebl,
9866 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
9867 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
9868 : : {
9869 : 0 : printf (_("\
9870 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
9871 : : elf_ndxscn (scn), section_name (ebl, shdr),
9872 : 0 : (uint64_t) shdr->sh_offset);
9873 [ # # ]: 0 : putc_unlocked ('\n', stdout);
9874 : :
9875 : : /* There is no function in libdw to iterate over the raw content of
9876 : : the section but it is easy enough to do. */
9877 : 0 : Elf_Data *data = (dbg->sectiondata[IDX_debug_macinfo]
9878 [ # # ]: 0 : ?: elf_rawdata (scn, NULL));
9879 [ # # ]: 0 : if (unlikely (data == NULL))
9880 : : {
9881 : 0 : error (0, 0, _("cannot get macro information section data: %s"),
9882 : : elf_errmsg (-1));
9883 : 0 : return;
9884 : : }
9885 : :
9886 : : /* Get the source file information for all CUs. */
9887 : 0 : Dwarf_Off offset;
9888 : 0 : Dwarf_Off ncu = 0;
9889 : 0 : size_t hsize;
9890 : 0 : struct mac_culist *culist = NULL;
9891 : 0 : size_t nculist = 0;
9892 [ # # ]: 0 : while (dwarf_nextcu (dbg, offset = ncu, &ncu, &hsize, NULL, NULL, NULL) == 0)
9893 : : {
9894 : 0 : Dwarf_Die cudie;
9895 [ # # ]: 0 : if (dwarf_offdie (dbg, offset + hsize, &cudie) == NULL)
9896 : 0 : continue;
9897 : :
9898 : 0 : Dwarf_Attribute attr;
9899 [ # # ]: 0 : if (dwarf_attr (&cudie, DW_AT_macro_info, &attr) == NULL)
9900 : 0 : continue;
9901 : :
9902 : 0 : Dwarf_Word macoff;
9903 [ # # ]: 0 : if (dwarf_formudata (&attr, &macoff) != 0)
9904 : 0 : continue;
9905 : :
9906 : 0 : struct mac_culist *newp = (struct mac_culist *) alloca (sizeof (*newp));
9907 : 0 : newp->die = cudie;
9908 : 0 : newp->offset = macoff;
9909 : 0 : newp->files = NULL;
9910 : 0 : newp->next = culist;
9911 : 0 : culist = newp;
9912 : 0 : ++nculist;
9913 : : }
9914 : :
9915 : : /* Convert the list into an array for easier consumption. */
9916 : 0 : struct mac_culist *cus = (struct mac_culist *) alloca ((nculist + 1)
9917 : : * sizeof (*cus));
9918 : : /* Add sentinel. */
9919 : 0 : cus[nculist].offset = data->d_size;
9920 : 0 : cus[nculist].files = (Dwarf_Files *) -1l;
9921 [ # # ]: 0 : if (nculist > 0)
9922 : : {
9923 [ # # ]: 0 : for (size_t cnt = nculist - 1; culist != NULL; --cnt)
9924 : : {
9925 [ # # ]: 0 : assert (cnt < nculist);
9926 : 0 : cus[cnt] = *culist;
9927 : 0 : culist = culist->next;
9928 : : }
9929 : :
9930 : : /* Sort the array according to the offset in the .debug_macinfo
9931 : : section. Note we keep the sentinel at the end. */
9932 : 0 : qsort (cus, nculist, sizeof (*cus), mac_compare);
9933 : : }
9934 : :
9935 : 0 : const unsigned char *readp = (const unsigned char *) data->d_buf;
9936 : 0 : const unsigned char *readendp = readp + data->d_size;
9937 : 0 : int level = 1;
9938 : :
9939 : 0 : while (readp < readendp)
9940 : : {
9941 : 0 : unsigned int opcode = *readp++;
9942 : 0 : unsigned int u128;
9943 : 0 : unsigned int u128_2;
9944 : 0 : const unsigned char *endp;
9945 : :
9946 [ # # # # ]: 0 : switch (opcode)
9947 : : {
9948 : 0 : case DW_MACINFO_define:
9949 : : case DW_MACINFO_undef:
9950 : : case DW_MACINFO_vendor_ext:
9951 : : /* For the first two opcodes the parameters are
9952 : : line, string
9953 : : For the latter
9954 : : number, string.
9955 : : We can treat these cases together. */
9956 : 0 : get_uleb128 (u128, readp, readendp);
9957 : :
9958 : 0 : endp = memchr (readp, '\0', readendp - readp);
9959 [ # # ]: 0 : if (unlikely (endp == NULL))
9960 : : {
9961 : 0 : printf (_("\
9962 : : %*s*** non-terminated string at end of section"),
9963 : : level, "");
9964 : 0 : return;
9965 : : }
9966 : :
9967 [ # # ]: 0 : if (opcode == DW_MACINFO_define)
9968 : 0 : printf ("%*s#define %s, line %u\n",
9969 : : level, "", (char *) readp, u128);
9970 [ # # ]: 0 : else if (opcode == DW_MACINFO_undef)
9971 : 0 : printf ("%*s#undef %s, line %u\n",
9972 : : level, "", (char *) readp, u128);
9973 : : else
9974 : 0 : printf (" #vendor-ext %s, number %u\n", (char *) readp, u128);
9975 : :
9976 : 0 : readp = endp + 1;
9977 : 0 : break;
9978 : :
9979 : 0 : case DW_MACINFO_start_file:
9980 : : /* The two parameters are line and file index, in this order. */
9981 : 0 : get_uleb128 (u128, readp, readendp);
9982 [ # # ]: 0 : if (readendp - readp < 1)
9983 : : {
9984 : 0 : printf (_("\
9985 : : %*s*** missing DW_MACINFO_start_file argument at end of section"),
9986 : : level, "");
9987 : 0 : return;
9988 : : }
9989 : 0 : get_uleb128 (u128_2, readp, readendp);
9990 : :
9991 : : /* Find the CU DIE for this file. */
9992 : 0 : size_t macoff = readp - (const unsigned char *) data->d_buf;
9993 : 0 : const char *fname = "???";
9994 [ # # # # ]: 0 : if (macoff >= cus[0].offset && cus[0].offset != data->d_size)
9995 : : {
9996 [ # # # # ]: 0 : while (macoff >= cus[1].offset && cus[1].offset != data->d_size)
9997 : 0 : ++cus;
9998 : :
9999 [ # # ]: 0 : if (cus[0].files == NULL
10000 [ # # ]: 0 : && dwarf_getsrcfiles (&cus[0].die, &cus[0].files, NULL) != 0)
10001 : 0 : cus[0].files = (Dwarf_Files *) -1l;
10002 : :
10003 [ # # ]: 0 : if (cus[0].files != (Dwarf_Files *) -1l)
10004 : 0 : fname = (dwarf_filesrc (cus[0].files, u128_2, NULL, NULL)
10005 [ # # ]: 0 : ?: "???");
10006 : : }
10007 : :
10008 : 0 : printf ("%*sstart_file %u, [%u] %s\n",
10009 : : level, "", u128, u128_2, fname);
10010 : 0 : ++level;
10011 : 0 : break;
10012 : :
10013 : 0 : case DW_MACINFO_end_file:
10014 : 0 : --level;
10015 : 0 : printf ("%*send_file\n", level, "");
10016 : : /* Nothing more to do. */
10017 : : break;
10018 : :
10019 : 0 : default:
10020 : : // XXX gcc seems to generate files with a trailing zero.
10021 [ # # # # ]: 0 : if (unlikely (opcode != 0 || readp != readendp))
10022 [ # # ]: 0 : printf ("%*s*** invalid opcode %u\n", level, "", opcode);
10023 : : break;
10024 : : }
10025 : : }
10026 : : }
10027 : :
10028 : :
10029 : : static void
10030 : 3 : print_debug_macro_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
10031 : : Ebl *ebl,
10032 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
10033 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
10034 : : {
10035 : 3 : printf (_("\
10036 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
10037 : : elf_ndxscn (scn), section_name (ebl, shdr),
10038 : 3 : (uint64_t) shdr->sh_offset);
10039 [ - + ]: 3 : putc_unlocked ('\n', stdout);
10040 : :
10041 : 3 : Elf_Data *data = elf_getdata (scn, NULL);
10042 [ - + ]: 3 : if (unlikely (data == NULL))
10043 : : {
10044 : 0 : error (0, 0, _("cannot get macro information section data: %s"),
10045 : : elf_errmsg (-1));
10046 : 0 : return;
10047 : : }
10048 : :
10049 : : /* Get the source file information for all CUs. Uses same
10050 : : datastructure as macinfo. But uses offset field to directly
10051 : : match .debug_line offset. And just stored in a list. */
10052 : 3 : Dwarf_Off offset;
10053 : 3 : Dwarf_Off ncu = 0;
10054 : 3 : size_t hsize;
10055 : 3 : struct mac_culist *culist = NULL;
10056 : 3 : size_t nculist = 0;
10057 [ + + ]: 7 : while (dwarf_nextcu (dbg, offset = ncu, &ncu, &hsize, NULL, NULL, NULL) == 0)
10058 : : {
10059 : 4 : Dwarf_Die cudie;
10060 [ - + ]: 4 : if (dwarf_offdie (dbg, offset + hsize, &cudie) == NULL)
10061 : 0 : continue;
10062 : :
10063 : 4 : Dwarf_Attribute attr;
10064 [ - + ]: 4 : if (dwarf_attr (&cudie, DW_AT_stmt_list, &attr) == NULL)
10065 : 0 : continue;
10066 : :
10067 : 4 : Dwarf_Word lineoff;
10068 [ - + ]: 4 : if (dwarf_formudata (&attr, &lineoff) != 0)
10069 : 0 : continue;
10070 : :
10071 : 4 : struct mac_culist *newp = (struct mac_culist *) alloca (sizeof (*newp));
10072 : 4 : newp->die = cudie;
10073 : 4 : newp->offset = lineoff;
10074 : 4 : newp->files = NULL;
10075 : 4 : newp->next = culist;
10076 : 4 : culist = newp;
10077 : 4 : ++nculist;
10078 : : }
10079 : :
10080 : 3 : const unsigned char *readp = (const unsigned char *) data->d_buf;
10081 : 3 : const unsigned char *readendp = readp + data->d_size;
10082 : :
10083 [ + + ]: 11 : while (readp < readendp)
10084 : : {
10085 : 16 : printf (_(" Offset: 0x%" PRIx64 "\n"),
10086 : 8 : (uint64_t) (readp - (const unsigned char *) data->d_buf));
10087 : :
10088 : : // Header, 2 byte version, 1 byte flag, optional .debug_line offset,
10089 : : // optional vendor extension macro entry table.
10090 [ - + ]: 8 : if (readp + 2 > readendp)
10091 : : {
10092 : 0 : invalid_data:
10093 : 0 : error (0, 0, _("invalid data"));
10094 : 0 : return;
10095 : : }
10096 [ - + ]: 8 : const uint16_t vers = read_2ubyte_unaligned_inc (dbg, readp);
10097 : 8 : printf (_(" Version: %" PRIu16 "\n"), vers);
10098 : :
10099 : : // Version 4 is the GNU extension for DWARF4. DWARF5 will use version
10100 : : // 5 when it gets standardized.
10101 [ - + ]: 8 : if (vers != 4 && vers != 5)
10102 : : {
10103 : 0 : printf (_(" unknown version, cannot parse section\n"));
10104 : 0 : return;
10105 : : }
10106 : :
10107 [ - + ]: 8 : if (readp + 1 > readendp)
10108 : 0 : goto invalid_data;
10109 : 8 : const unsigned char flag = *readp++;
10110 : 8 : printf (_(" Flag: 0x%" PRIx8), flag);
10111 [ + + ]: 8 : if (flag != 0)
10112 : : {
10113 : 4 : printf (" (");
10114 [ - + ]: 4 : if ((flag & 0x01) != 0)
10115 : : {
10116 : 0 : printf ("offset_size");
10117 [ # # ]: 0 : if ((flag & 0xFE) != 0)
10118 : 0 : printf (", ");
10119 : : }
10120 [ + - ]: 4 : if ((flag & 0x02) != 0)
10121 : : {
10122 : 4 : printf ("debug_line_offset");
10123 [ - + ]: 4 : if ((flag & 0xFC) != 0)
10124 : 0 : printf (", ");
10125 : : }
10126 [ - + ]: 4 : if ((flag & 0x04) != 0)
10127 : : {
10128 : 0 : printf ("operands_table");
10129 [ # # ]: 0 : if ((flag & 0xF8) != 0)
10130 : 0 : printf (", ");
10131 : : }
10132 [ - + ]: 4 : if ((flag & 0xF8) != 0)
10133 : 0 : printf ("unknown");
10134 : 4 : printf (")");
10135 : : }
10136 : 8 : printf ("\n");
10137 : :
10138 [ + - ]: 8 : unsigned int offset_len = (flag & 0x01) ? 8 : 4;
10139 : 8 : printf (_(" Offset length: %" PRIu8 "\n"), offset_len);
10140 : 8 : Dwarf_Off line_offset = -1;
10141 [ + + ]: 8 : if (flag & 0x02)
10142 : : {
10143 [ - + ]: 4 : if (offset_len == 8)
10144 [ # # ]: 0 : line_offset = read_8ubyte_unaligned_inc (dbg, readp);
10145 : : else
10146 [ - + ]: 8 : line_offset = read_4ubyte_unaligned_inc (dbg, readp);
10147 : 4 : printf (_(" .debug_line offset: 0x%" PRIx64 "\n"),
10148 : : line_offset);
10149 : : }
10150 : :
10151 : 4 : struct mac_culist *cu = NULL;
10152 [ + - ]: 4 : if (line_offset != (Dwarf_Off) -1)
10153 : : {
10154 : : cu = culist;
10155 [ + - + + ]: 5 : while (cu != NULL && line_offset != cu->offset)
10156 : 1 : cu = cu->next;
10157 : : }
10158 : :
10159 [ + + ]: 8 : Dwarf_Off str_offsets_base = str_offsets_base_off (dbg, (cu != NULL
10160 : : ? cu->die.cu
10161 : : : NULL));
10162 : :
10163 : 8 : const unsigned char *vendor[DW_MACRO_hi_user - DW_MACRO_lo_user + 1];
10164 [ - + ]: 8 : memset (vendor, 0, sizeof vendor);
10165 [ - + ]: 8 : if (flag & 0x04)
10166 : : {
10167 : : // 1 byte length, for each item, 1 byte opcode, uleb128 number
10168 : : // of arguments, for each argument 1 byte form code.
10169 [ # # ]: 0 : if (readp + 1 > readendp)
10170 : 0 : goto invalid_data;
10171 : 0 : unsigned int tlen = *readp++;
10172 : 0 : printf (_(" extension opcode table, %" PRIu8 " items:\n"),
10173 : : tlen);
10174 [ # # ]: 0 : for (unsigned int i = 0; i < tlen; i++)
10175 : : {
10176 [ # # ]: 0 : if (readp + 1 > readendp)
10177 : 0 : goto invalid_data;
10178 : 0 : unsigned int opcode = *readp++;
10179 : 0 : printf (_(" [%" PRIx8 "]"), opcode);
10180 : 0 : if (opcode < DW_MACRO_lo_user
10181 [ # # ]: 0 : || opcode > DW_MACRO_hi_user)
10182 : 0 : goto invalid_data;
10183 : : // Record the start of description for this vendor opcode.
10184 : : // uleb128 nr args, 1 byte per arg form.
10185 : 0 : vendor[opcode - DW_MACRO_lo_user] = readp;
10186 [ # # ]: 0 : if (readp + 1 > readendp)
10187 : 0 : goto invalid_data;
10188 : 0 : unsigned int args = *readp++;
10189 [ # # ]: 0 : if (args > 0)
10190 : : {
10191 : 0 : printf (_(" %" PRIu8 " arguments:"), args);
10192 : 0 : while (args > 0)
10193 : : {
10194 [ # # ]: 0 : if (readp + 1 > readendp)
10195 : 0 : goto invalid_data;
10196 : 0 : unsigned int form = *readp++;
10197 : 0 : printf (" %s", dwarf_form_name (form));
10198 [ # # ]: 0 : if (! libdw_valid_user_form (form))
10199 : 0 : goto invalid_data;
10200 : 0 : args--;
10201 [ # # ]: 0 : if (args > 0)
10202 [ # # # # ]: 0 : putchar_unlocked (',');
10203 : : }
10204 : : }
10205 : : else
10206 : 0 : printf (_(" no arguments."));
10207 [ # # ]: 0 : putchar_unlocked ('\n');
10208 : : }
10209 : : }
10210 [ - + ]: 8 : putchar_unlocked ('\n');
10211 : :
10212 : 8 : int level = 1;
10213 [ - + ]: 8 : if (readp + 1 > readendp)
10214 : 0 : goto invalid_data;
10215 : 8 : unsigned int opcode = *readp++;
10216 : 8 : while (opcode != 0)
10217 : : {
10218 : 727 : unsigned int u128;
10219 : 727 : unsigned int u128_2;
10220 : 727 : const unsigned char *endp;
10221 : 727 : uint64_t off;
10222 : :
10223 [ + + + - : 727 : switch (opcode)
+ + + - -
- - - - ]
10224 : : {
10225 : 6 : case DW_MACRO_start_file:
10226 : 6 : get_uleb128 (u128, readp, readendp);
10227 [ - + ]: 6 : if (readp >= readendp)
10228 : 0 : goto invalid_data;
10229 : 6 : get_uleb128 (u128_2, readp, readendp);
10230 : :
10231 : : /* Find the CU DIE that matches this line offset. */
10232 : 6 : const char *fname = "???";
10233 [ + - ]: 6 : if (cu != NULL)
10234 : : {
10235 [ + + ]: 6 : if (cu->files == NULL
10236 [ - + ]: 4 : && dwarf_getsrcfiles (&cu->die, &cu->files,
10237 : : NULL) != 0)
10238 : 0 : cu->files = (Dwarf_Files *) -1l;
10239 : :
10240 [ + - ]: 6 : if (cu->files != (Dwarf_Files *) -1l)
10241 : 6 : fname = (dwarf_filesrc (cu->files, u128_2,
10242 [ - + ]: 6 : NULL, NULL) ?: "???");
10243 : : }
10244 : 6 : printf ("%*sstart_file %u, [%u] %s\n",
10245 : : level, "", u128, u128_2, fname);
10246 : 6 : ++level;
10247 : 6 : break;
10248 : :
10249 : 6 : case DW_MACRO_end_file:
10250 : 6 : --level;
10251 : 6 : printf ("%*send_file\n", level, "");
10252 : : break;
10253 : :
10254 : 1 : case DW_MACRO_define:
10255 : 1 : get_uleb128 (u128, readp, readendp);
10256 : 1 : endp = memchr (readp, '\0', readendp - readp);
10257 [ - + ]: 1 : if (endp == NULL)
10258 : 0 : goto invalid_data;
10259 : 1 : printf ("%*s#define %s, line %u\n",
10260 : : level, "", readp, u128);
10261 : 1 : readp = endp + 1;
10262 : 1 : break;
10263 : :
10264 : 0 : case DW_MACRO_undef:
10265 : 0 : get_uleb128 (u128, readp, readendp);
10266 : 0 : endp = memchr (readp, '\0', readendp - readp);
10267 [ # # ]: 0 : if (endp == NULL)
10268 : 0 : goto invalid_data;
10269 : 0 : printf ("%*s#undef %s, line %u\n",
10270 : : level, "", readp, u128);
10271 : 0 : readp = endp + 1;
10272 : 0 : break;
10273 : :
10274 : 707 : case DW_MACRO_define_strp:
10275 : 707 : get_uleb128 (u128, readp, readendp);
10276 [ - + ]: 707 : if (readp + offset_len > readendp)
10277 : 0 : goto invalid_data;
10278 [ - + ]: 707 : if (offset_len == 8)
10279 [ # # ]: 0 : off = read_8ubyte_unaligned_inc (dbg, readp);
10280 : : else
10281 [ - + ]: 1414 : off = read_4ubyte_unaligned_inc (dbg, readp);
10282 : 707 : printf ("%*s#define %s, line %u (indirect)\n",
10283 : : level, "", dwarf_getstring (dbg, off, NULL), u128);
10284 : : break;
10285 : :
10286 : 1 : case DW_MACRO_undef_strp:
10287 : 1 : get_uleb128 (u128, readp, readendp);
10288 [ - + ]: 1 : if (readp + offset_len > readendp)
10289 : 0 : goto invalid_data;
10290 [ - + ]: 1 : if (offset_len == 8)
10291 [ # # ]: 0 : off = read_8ubyte_unaligned_inc (dbg, readp);
10292 : : else
10293 [ - + ]: 2 : off = read_4ubyte_unaligned_inc (dbg, readp);
10294 : 1 : printf ("%*s#undef %s, line %u (indirect)\n",
10295 : : level, "", dwarf_getstring (dbg, off, NULL), u128);
10296 : : break;
10297 : :
10298 : 6 : case DW_MACRO_import:
10299 [ - + ]: 6 : if (readp + offset_len > readendp)
10300 : 0 : goto invalid_data;
10301 [ - + ]: 6 : if (offset_len == 8)
10302 [ # # ]: 0 : off = read_8ubyte_unaligned_inc (dbg, readp);
10303 : : else
10304 [ - + ]: 12 : off = read_4ubyte_unaligned_inc (dbg, readp);
10305 : 6 : printf ("%*s#include offset 0x%" PRIx64 "\n",
10306 : : level, "", off);
10307 : : break;
10308 : :
10309 : 0 : case DW_MACRO_define_sup:
10310 : 0 : get_uleb128 (u128, readp, readendp);
10311 [ # # ]: 0 : if (readp + offset_len > readendp)
10312 : 0 : goto invalid_data;
10313 : 0 : printf ("%*s#define ", level, "");
10314 : 0 : readp = print_form_data (dbg, DW_FORM_strp_sup,
10315 : : readp, readendp, offset_len,
10316 : : str_offsets_base);
10317 : 0 : printf (", line %u (sup)\n", u128);
10318 : : break;
10319 : :
10320 : 0 : case DW_MACRO_undef_sup:
10321 : 0 : get_uleb128 (u128, readp, readendp);
10322 [ # # ]: 0 : if (readp + offset_len > readendp)
10323 : 0 : goto invalid_data;
10324 : 0 : printf ("%*s#undef ", level, "");
10325 : 0 : readp = print_form_data (dbg, DW_FORM_strp_sup,
10326 : : readp, readendp, offset_len,
10327 : : str_offsets_base);
10328 : 0 : printf (", line %u (sup)\n", u128);
10329 : : break;
10330 : :
10331 : 0 : case DW_MACRO_import_sup:
10332 [ # # ]: 0 : if (readp + offset_len > readendp)
10333 : 0 : goto invalid_data;
10334 [ # # ]: 0 : if (offset_len == 8)
10335 [ # # ]: 0 : off = read_8ubyte_unaligned_inc (dbg, readp);
10336 : : else
10337 [ # # ]: 0 : off = read_4ubyte_unaligned_inc (dbg, readp);
10338 : : // XXX Needs support for reading from supplementary object file.
10339 : 0 : printf ("%*s#include offset 0x%" PRIx64 " (sup)\n",
10340 : : level, "", off);
10341 : : break;
10342 : :
10343 : 0 : case DW_MACRO_define_strx:
10344 : 0 : get_uleb128 (u128, readp, readendp);
10345 [ # # ]: 0 : if (readp + offset_len > readendp)
10346 : 0 : goto invalid_data;
10347 : 0 : printf ("%*s#define ", level, "");
10348 : 0 : readp = print_form_data (dbg, DW_FORM_strx,
10349 : : readp, readendp, offset_len,
10350 : : str_offsets_base);
10351 : 0 : printf (", line %u (strx)\n", u128);
10352 : : break;
10353 : :
10354 : 0 : case DW_MACRO_undef_strx:
10355 : 0 : get_uleb128 (u128, readp, readendp);
10356 [ # # ]: 0 : if (readp + offset_len > readendp)
10357 : 0 : goto invalid_data;
10358 : 0 : printf ("%*s#undef ", level, "");
10359 : 0 : readp = print_form_data (dbg, DW_FORM_strx,
10360 : : readp, readendp, offset_len,
10361 : : str_offsets_base);
10362 : 0 : printf (", line %u (strx)\n", u128);
10363 : : break;
10364 : :
10365 : : default:
10366 : 0 : printf ("%*svendor opcode 0x%" PRIx8, level, "", opcode);
10367 [ # # ]: 0 : if (opcode < DW_MACRO_lo_user
10368 : : || opcode > DW_MACRO_lo_user
10369 [ # # ]: 0 : || vendor[opcode - DW_MACRO_lo_user] == NULL)
10370 : 0 : goto invalid_data;
10371 : :
10372 : 0 : const unsigned char *op_desc;
10373 : 0 : op_desc = vendor[opcode - DW_MACRO_lo_user];
10374 : :
10375 : : // Just skip the arguments, we cannot really interpret them,
10376 : : // but print as much as we can.
10377 : 0 : unsigned int args = *op_desc++;
10378 [ # # ]: 0 : while (args > 0 && readp < readendp)
10379 : : {
10380 : 0 : unsigned int form = *op_desc++;
10381 : 0 : readp = print_form_data (dbg, form, readp, readendp,
10382 : : offset_len, str_offsets_base);
10383 : 0 : args--;
10384 [ # # ]: 0 : if (args > 0)
10385 [ # # ]: 0 : printf (", ");
10386 : : }
10387 [ # # ]: 0 : putchar_unlocked ('\n');
10388 : : }
10389 : :
10390 [ - + ]: 727 : if (readp + 1 > readendp)
10391 : 0 : goto invalid_data;
10392 : 727 : opcode = *readp++;
10393 [ + + ]: 727 : if (opcode == 0)
10394 [ - + + + ]: 743 : putchar_unlocked ('\n');
10395 : : }
10396 : : }
10397 : : }
10398 : :
10399 : :
10400 : : /* Callback for printing global names. */
10401 : : static int
10402 : 34 : print_pubnames (Dwarf *dbg __attribute__ ((unused)), Dwarf_Global *global,
10403 : : void *arg)
10404 : : {
10405 : 34 : int *np = (int *) arg;
10406 : :
10407 : 68 : printf (_(" [%5d] DIE offset: %6" PRId64
10408 : : ", CU DIE offset: %6" PRId64 ", name: %s\n"),
10409 : 34 : (*np)++, global->die_offset, global->cu_offset, global->name);
10410 : :
10411 : 34 : return 0;
10412 : : }
10413 : :
10414 : :
10415 : : /* Print the known exported symbols in the DWARF section '.debug_pubnames'. */
10416 : : static void
10417 : 8 : print_debug_pubnames_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
10418 : : Ebl *ebl,
10419 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
10420 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
10421 : : {
10422 : 8 : printf (_("\nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
10423 : : elf_ndxscn (scn), section_name (ebl, shdr),
10424 : 8 : (uint64_t) shdr->sh_offset);
10425 : :
10426 : 8 : int n = 0;
10427 : 8 : (void) dwarf_getpubnames (dbg, print_pubnames, &n, 0);
10428 : 8 : }
10429 : :
10430 : : /* Print the content of the DWARF string section '.debug_str'. */
10431 : : static void
10432 : 45 : print_debug_str_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
10433 : : Ebl *ebl,
10434 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
10435 : : Elf_Scn *scn, GElf_Shdr *shdr,
10436 : : Dwarf *dbg __attribute__ ((unused)))
10437 : : {
10438 : 45 : Elf_Data *data = elf_rawdata (scn, NULL);
10439 [ + - ]: 45 : const size_t sh_size = data ? data->d_size : 0;
10440 : :
10441 : : /* Compute floor(log16(shdr->sh_size)). */
10442 : 45 : GElf_Addr tmp = sh_size;
10443 : 45 : int digits = 1;
10444 [ + + ]: 160 : while (tmp >= 16)
10445 : : {
10446 : 115 : ++digits;
10447 : 115 : tmp >>= 4;
10448 : : }
10449 : 45 : digits = MAX (4, digits);
10450 : :
10451 : 45 : printf (_("\nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"
10452 : : " %*s String\n"),
10453 : : elf_ndxscn (scn),
10454 : 45 : section_name (ebl, shdr), (uint64_t) shdr->sh_offset,
10455 : : /* TRANS: the debugstr| prefix makes the string unique. */
10456 [ + - ]: 45 : digits + 2, sgettext ("debugstr|Offset"));
10457 : :
10458 : 45 : Dwarf_Off offset = 0;
10459 [ + + ]: 74143 : while (offset < sh_size)
10460 : : {
10461 : 74098 : size_t len;
10462 : 74098 : const char *str = (const char *) data->d_buf + offset;
10463 : 74098 : const char *endp = memchr (str, '\0', sh_size - offset);
10464 [ - + ]: 74098 : if (unlikely (endp == NULL))
10465 : : {
10466 : 0 : printf (_(" *** error, missing string terminator\n"));
10467 : : break;
10468 : : }
10469 : :
10470 : 74098 : printf (" [%*" PRIx64 "] \"%s\"\n", digits, (uint64_t) offset, str);
10471 : 74098 : len = endp - str;
10472 : 74098 : offset += len + 1;
10473 : : }
10474 : 45 : }
10475 : :
10476 : : static void
10477 : 4 : print_debug_str_offsets_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
10478 : : Ebl *ebl,
10479 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
10480 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
10481 : : {
10482 : 4 : printf (_("\
10483 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
10484 : : elf_ndxscn (scn), section_name (ebl, shdr),
10485 : 4 : (uint64_t) shdr->sh_offset);
10486 : :
10487 [ + - ]: 4 : if (shdr->sh_size == 0)
10488 : : return;
10489 : :
10490 : : /* We like to get the section from libdw to make sure they are relocated. */
10491 : 8 : Elf_Data *data = (dbg->sectiondata[IDX_debug_str_offsets]
10492 [ - + ]: 4 : ?: elf_rawdata (scn, NULL));
10493 [ - + ]: 4 : if (unlikely (data == NULL))
10494 : : {
10495 : 0 : error (0, 0, _("cannot get .debug_str_offsets section data: %s"),
10496 : : elf_errmsg (-1));
10497 : 0 : return;
10498 : : }
10499 : :
10500 : 4 : size_t idx = 0;
10501 : 4 : sort_listptr (&known_stroffbases, "str_offsets");
10502 : :
10503 : 4 : const unsigned char *start = (const unsigned char *) data->d_buf;
10504 : 4 : const unsigned char *readp = start;
10505 : 4 : const unsigned char *readendp = ((const unsigned char *) data->d_buf
10506 : 4 : + data->d_size);
10507 : :
10508 : 4 : while (readp < readendp)
10509 : : {
10510 : : /* Most string offset tables will have a header. For split
10511 : : dwarf unit GNU DebugFission didn't add one. But they were
10512 : : also only defined for split units (main or skeleton units
10513 : : didn't have indirect strings). So if we don't have a
10514 : : DW_AT_str_offsets_base at all and this is offset zero, then
10515 : : just start printing offsets immediately, if this is a .dwo
10516 : : section. */
10517 : 4 : Dwarf_Off off = (Dwarf_Off) (readp
10518 : 4 : - (const unsigned char *) data->d_buf);
10519 : :
10520 : 4 : printf ("Table at offset %" PRIx64 " ", off);
10521 : :
10522 [ - + ]: 4 : struct listptr *listptr = get_listptr (&known_stroffbases, idx++);
10523 : 4 : const unsigned char *next_unitp = readendp;
10524 : 0 : uint8_t offset_size;
10525 : 0 : bool has_header;
10526 [ # # ]: 0 : if (listptr == NULL)
10527 : : {
10528 : : /* This can happen for .dwo files. There is only an header
10529 : : in the case this is a version 5 split DWARF file. */
10530 : 4 : Dwarf_CU *cu;
10531 : 4 : uint8_t unit_type;
10532 [ - + ]: 4 : if (dwarf_get_units (dbg, NULL, &cu, NULL, &unit_type,
10533 : : NULL, NULL) != 0)
10534 : : {
10535 : 0 : error (0, 0, "Warning: Cannot find any DWARF unit.");
10536 : : /* Just guess some values. */
10537 : 0 : has_header = false;
10538 : 0 : offset_size = 4;
10539 : : }
10540 [ + - ]: 4 : else if (off == 0
10541 : 4 : && (unit_type == DW_UT_split_type
10542 [ + - ]: 4 : || unit_type == DW_UT_split_compile))
10543 : : {
10544 : 4 : has_header = cu->version > 4;
10545 : 4 : offset_size = cu->offset_size;
10546 : : }
10547 : : else
10548 : : {
10549 : 0 : error (0, 0,
10550 : : "Warning: No CU references .debug_str_offsets after %"
10551 : : PRIx64, off);
10552 : 0 : has_header = cu->version > 4;
10553 : 0 : offset_size = cu->offset_size;
10554 : : }
10555 : 4 : printf ("\n");
10556 : : }
10557 : : else
10558 : : {
10559 : : /* This must be DWARF5, since GNU DebugFission didn't define
10560 : : DW_AT_str_offsets_base. */
10561 : 0 : has_header = true;
10562 : :
10563 : 0 : Dwarf_Die cudie;
10564 [ # # ]: 0 : if (dwarf_cu_die (listptr->cu, &cudie,
10565 : : NULL, NULL, NULL, NULL,
10566 : : NULL, NULL) == NULL)
10567 : 0 : printf ("Unknown CU (%s):\n", dwarf_errmsg (-1));
10568 : : else
10569 : 0 : printf ("for CU [%6" PRIx64 "]:\n", dwarf_dieoffset (&cudie));
10570 : : }
10571 : :
10572 [ + + ]: 4 : if (has_header)
10573 : : {
10574 : 2 : uint64_t unit_length;
10575 : 2 : uint16_t version;
10576 : 2 : uint16_t padding;
10577 : :
10578 [ - + ]: 2 : unit_length = read_4ubyte_unaligned_inc (dbg, readp);
10579 [ - + ]: 2 : if (unlikely (unit_length == 0xffffffff))
10580 : : {
10581 [ # # ]: 0 : if (unlikely (readp > readendp - 8))
10582 : : {
10583 : 0 : invalid_data:
10584 : 0 : error (0, 0, "Invalid data");
10585 : 0 : return;
10586 : : }
10587 [ # # ]: 0 : unit_length = read_8ubyte_unaligned_inc (dbg, readp);
10588 : 0 : offset_size = 8;
10589 : : }
10590 : : else
10591 : : offset_size = 4;
10592 : :
10593 : 2 : printf ("\n");
10594 : 2 : printf (_(" Length: %8" PRIu64 "\n"),
10595 : : unit_length);
10596 : 2 : printf (_(" Offset size: %8" PRIu8 "\n"),
10597 : : offset_size);
10598 : :
10599 : : /* We need at least 2-bytes (version) + 2-bytes (padding) =
10600 : : 4 bytes to complete the header. And this unit cannot go
10601 : : beyond the section data. */
10602 [ + - ]: 2 : if (readp > readendp - 4
10603 [ + - ]: 2 : || unit_length < 4
10604 [ - + ]: 2 : || unit_length > (uint64_t) (readendp - readp))
10605 : 0 : goto invalid_data;
10606 : :
10607 : 2 : next_unitp = readp + unit_length;
10608 : :
10609 [ - + ]: 2 : version = read_2ubyte_unaligned_inc (dbg, readp);
10610 : 2 : printf (_(" DWARF version: %8" PRIu16 "\n"), version);
10611 : :
10612 [ - + ]: 2 : if (version != 5)
10613 : : {
10614 : 0 : error (0, 0, _("Unknown version"));
10615 : 0 : goto next_unit;
10616 : : }
10617 : :
10618 [ - + ]: 2 : padding = read_2ubyte_unaligned_inc (dbg, readp);
10619 : 2 : printf (_(" Padding: %8" PRIx16 "\n"), padding);
10620 : :
10621 [ - + ]: 2 : if (listptr != NULL
10622 [ # # ]: 0 : && listptr->offset != (Dwarf_Off) (readp - start))
10623 : : {
10624 : 0 : error (0, 0, "String offsets index doesn't start after header");
10625 : 0 : goto next_unit;
10626 : : }
10627 : :
10628 : 2 : printf ("\n");
10629 : : }
10630 : :
10631 : 4 : int digits = 1;
10632 : 4 : size_t offsets = (next_unitp - readp) / offset_size;
10633 [ + + ]: 8 : while (offsets >= 10)
10634 : : {
10635 : 4 : ++digits;
10636 : 4 : offsets /= 10;
10637 : : }
10638 : :
10639 : 4 : unsigned int uidx = 0;
10640 : 4 : size_t index_offset = readp - (const unsigned char *) data->d_buf;
10641 : 4 : printf (" Offsets start at 0x%zx:\n", index_offset);
10642 : 64 : while (readp <= next_unitp - offset_size)
10643 : : {
10644 : 60 : Dwarf_Word offset;
10645 [ + - ]: 60 : if (offset_size == 4)
10646 [ - + ]: 120 : offset = read_4ubyte_unaligned_inc (dbg, readp);
10647 : : else
10648 [ # # ]: 0 : offset = read_8ubyte_unaligned_inc (dbg, readp);
10649 : 60 : const char *str = dwarf_getstring (dbg, offset, NULL);
10650 [ + + ]: 64 : printf (" [%*u] [%*" PRIx64 "] \"%s\"\n",
10651 [ - + ]: 60 : digits, uidx++, (int) offset_size * 2, offset, str ?: "???");
10652 : : }
10653 : 4 : printf ("\n");
10654 : :
10655 [ + - ]: 4 : if (readp != next_unitp)
10656 [ + + ]: 8 : error (0, 0, "extra %zd bytes at end of unit",
10657 : 0 : (size_t) (next_unitp - readp));
10658 : :
10659 : 4 : next_unit:
10660 : : readp = next_unitp;
10661 : : }
10662 : : }
10663 : :
10664 : :
10665 : : /* Print the content of the call frame search table section
10666 : : '.eh_frame_hdr'. */
10667 : : static void
10668 : 70 : print_debug_frame_hdr_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
10669 : : Ebl *ebl __attribute__ ((unused)),
10670 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
10671 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
10672 : : {
10673 : 70 : printf (_("\
10674 : : \nCall frame search table section [%2zu] '.eh_frame_hdr':\n"),
10675 : : elf_ndxscn (scn));
10676 : :
10677 : 70 : Elf_Data *data = elf_rawdata (scn, NULL);
10678 : :
10679 [ - + ]: 70 : if (unlikely (data == NULL))
10680 : : {
10681 : 0 : error (0, 0, _("cannot get %s content: %s"),
10682 : : ".eh_frame_hdr", elf_errmsg (-1));
10683 : 0 : return;
10684 : : }
10685 : :
10686 : 70 : const unsigned char *readp = data->d_buf;
10687 : 70 : const unsigned char *const dataend = ((unsigned char *) data->d_buf
10688 : 70 : + data->d_size);
10689 : :
10690 [ - + ]: 70 : if (unlikely (readp + 4 > dataend))
10691 : : {
10692 : 0 : invalid_data:
10693 : 0 : error (0, 0, _("invalid data"));
10694 : 0 : return;
10695 : : }
10696 : :
10697 : 70 : unsigned int version = *readp++;
10698 : 70 : unsigned int eh_frame_ptr_enc = *readp++;
10699 : 70 : unsigned int fde_count_enc = *readp++;
10700 : 70 : unsigned int table_enc = *readp++;
10701 : :
10702 : 70 : printf (" version: %u\n"
10703 : : " eh_frame_ptr_enc: %#x ",
10704 : : version, eh_frame_ptr_enc);
10705 : 70 : print_encoding_base ("", eh_frame_ptr_enc);
10706 : 70 : printf (" fde_count_enc: %#x ", fde_count_enc);
10707 : 70 : print_encoding_base ("", fde_count_enc);
10708 : 70 : printf (" table_enc: %#x ", table_enc);
10709 : 70 : print_encoding_base ("", table_enc);
10710 : :
10711 : 70 : uint64_t eh_frame_ptr = 0;
10712 [ + - ]: 70 : if (eh_frame_ptr_enc != DW_EH_PE_omit)
10713 : : {
10714 : 70 : readp = read_encoded (eh_frame_ptr_enc, readp, dataend, &eh_frame_ptr,
10715 : : dbg);
10716 [ - + ]: 70 : if (unlikely (readp == NULL))
10717 : 0 : goto invalid_data;
10718 : :
10719 : 70 : printf (" eh_frame_ptr: %#" PRIx64, eh_frame_ptr);
10720 [ + - ]: 70 : if ((eh_frame_ptr_enc & 0x70) == DW_EH_PE_pcrel)
10721 : 140 : printf (" (offset: %#" PRIx64 ")",
10722 : : /* +4 because of the 4 byte header of the section. */
10723 : 70 : (uint64_t) shdr->sh_offset + 4 + eh_frame_ptr);
10724 : :
10725 [ - + ]: 70 : putchar_unlocked ('\n');
10726 : : }
10727 : :
10728 : 70 : uint64_t fde_count = 0;
10729 [ + - ]: 70 : if (fde_count_enc != DW_EH_PE_omit)
10730 : : {
10731 : 70 : readp = read_encoded (fde_count_enc, readp, dataend, &fde_count, dbg);
10732 [ - + ]: 70 : if (unlikely (readp == NULL))
10733 : 0 : goto invalid_data;
10734 : :
10735 : 70 : printf (" fde_count: %" PRIu64 "\n", fde_count);
10736 : : }
10737 : :
10738 [ + - + - ]: 70 : if (fde_count == 0 || table_enc == DW_EH_PE_omit)
10739 : : return;
10740 : :
10741 : 70 : puts (" Table:");
10742 : :
10743 : : /* Optimize for the most common case. */
10744 [ + - ]: 70 : if (table_enc == (DW_EH_PE_datarel | DW_EH_PE_sdata4))
10745 [ + + ]: 12517 : while (fde_count > 0 && readp + 8 <= dataend)
10746 : : {
10747 [ - + ]: 12447 : int32_t initial_location = read_4sbyte_unaligned_inc (dbg, readp);
10748 : 12447 : uint64_t initial_offset = ((uint64_t) shdr->sh_offset
10749 : 12447 : + (int64_t) initial_location);
10750 [ - + ]: 12447 : int32_t address = read_4sbyte_unaligned_inc (dbg, readp);
10751 : : // XXX Possibly print symbol name or section offset for initial_offset
10752 [ + - ]: 12517 : printf (" %#" PRIx32 " (offset: %#6" PRIx64 ") -> %#" PRIx32
10753 : : " fde=[%6" PRIx64 "]\n",
10754 : : initial_location, initial_offset,
10755 : 12447 : address, address - (eh_frame_ptr + 4));
10756 : : }
10757 : : else
10758 : 70 : while (0 && readp < dataend)
10759 : : {
10760 : :
10761 : 70 : }
10762 : : }
10763 : :
10764 : :
10765 : : /* Print the content of the exception handling table section
10766 : : '.eh_frame_hdr'. */
10767 : : static void
10768 : 0 : print_debug_exception_table (Dwfl_Module *dwflmod __attribute__ ((unused)),
10769 : : Ebl *ebl __attribute__ ((unused)),
10770 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
10771 : : Elf_Scn *scn,
10772 : : GElf_Shdr *shdr __attribute__ ((unused)),
10773 : : Dwarf *dbg __attribute__ ((unused)))
10774 : : {
10775 : 0 : printf (_("\
10776 : : \nException handling table section [%2zu] '.gcc_except_table':\n"),
10777 : : elf_ndxscn (scn));
10778 : :
10779 : 0 : Elf_Data *data = elf_rawdata (scn, NULL);
10780 : :
10781 [ # # ]: 0 : if (unlikely (data == NULL))
10782 : : {
10783 : 0 : error (0, 0, _("cannot get %s content: %s"),
10784 : : ".gcc_except_table", elf_errmsg (-1));
10785 : 0 : return;
10786 : : }
10787 : :
10788 : 0 : const unsigned char *readp = data->d_buf;
10789 : 0 : const unsigned char *const dataend = readp + data->d_size;
10790 : :
10791 [ # # ]: 0 : if (unlikely (readp + 1 > dataend))
10792 : : {
10793 : 0 : invalid_data:
10794 : 0 : error (0, 0, _("invalid data"));
10795 : 0 : return;
10796 : : }
10797 : 0 : unsigned int lpstart_encoding = *readp++;
10798 : 0 : printf (_(" LPStart encoding: %#x "), lpstart_encoding);
10799 : 0 : print_encoding_base ("", lpstart_encoding);
10800 [ # # ]: 0 : if (lpstart_encoding != DW_EH_PE_omit)
10801 : : {
10802 : 0 : uint64_t lpstart;
10803 : 0 : readp = read_encoded (lpstart_encoding, readp, dataend, &lpstart, dbg);
10804 : 0 : printf (" LPStart: %#" PRIx64 "\n", lpstart);
10805 : : }
10806 : :
10807 [ # # ]: 0 : if (unlikely (readp + 1 > dataend))
10808 : 0 : goto invalid_data;
10809 : 0 : unsigned int ttype_encoding = *readp++;
10810 : 0 : printf (_(" TType encoding: %#x "), ttype_encoding);
10811 : 0 : print_encoding_base ("", ttype_encoding);
10812 : 0 : const unsigned char *ttype_base = NULL;
10813 [ # # ]: 0 : if (ttype_encoding != DW_EH_PE_omit)
10814 : : {
10815 : 0 : unsigned int ttype_base_offset;
10816 : 0 : get_uleb128 (ttype_base_offset, readp, dataend);
10817 : 0 : printf (" TType base offset: %#x\n", ttype_base_offset);
10818 [ # # ]: 0 : if ((size_t) (dataend - readp) > ttype_base_offset)
10819 : 0 : ttype_base = readp + ttype_base_offset;
10820 : : }
10821 : :
10822 [ # # ]: 0 : if (unlikely (readp + 1 > dataend))
10823 : 0 : goto invalid_data;
10824 : 0 : unsigned int call_site_encoding = *readp++;
10825 : 0 : printf (_(" Call site encoding: %#x "), call_site_encoding);
10826 : 0 : print_encoding_base ("", call_site_encoding);
10827 : 0 : unsigned int call_site_table_len;
10828 : 0 : get_uleb128 (call_site_table_len, readp, dataend);
10829 : :
10830 : 0 : const unsigned char *const action_table = readp + call_site_table_len;
10831 [ # # ]: 0 : if (unlikely (action_table > dataend))
10832 : 0 : goto invalid_data;
10833 : : unsigned int u = 0;
10834 : : unsigned int max_action = 0;
10835 [ # # ]: 0 : while (readp < action_table)
10836 : : {
10837 [ # # ]: 0 : if (u == 0)
10838 : 0 : puts (_("\n Call site table:"));
10839 : :
10840 : 0 : uint64_t call_site_start;
10841 : 0 : readp = read_encoded (call_site_encoding, readp, dataend,
10842 : : &call_site_start, dbg);
10843 : 0 : uint64_t call_site_length;
10844 : 0 : readp = read_encoded (call_site_encoding, readp, dataend,
10845 : : &call_site_length, dbg);
10846 : 0 : uint64_t landing_pad;
10847 : 0 : readp = read_encoded (call_site_encoding, readp, dataend,
10848 : : &landing_pad, dbg);
10849 : 0 : unsigned int action;
10850 : 0 : get_uleb128 (action, readp, dataend);
10851 : 0 : max_action = MAX (action, max_action);
10852 : 0 : printf (_(" [%4u] Call site start: %#" PRIx64 "\n"
10853 : : " Call site length: %" PRIu64 "\n"
10854 : : " Landing pad: %#" PRIx64 "\n"
10855 : : " Action: %u\n"),
10856 : : u++, call_site_start, call_site_length, landing_pad, action);
10857 : : }
10858 [ # # ]: 0 : if (readp != action_table)
10859 : 0 : goto invalid_data;
10860 : :
10861 : 0 : unsigned int max_ar_filter = 0;
10862 [ # # ]: 0 : if (max_action > 0)
10863 : : {
10864 : 0 : puts ("\n Action table:");
10865 : :
10866 : 0 : size_t maxdata = (size_t) (dataend - action_table);
10867 [ # # # # ]: 0 : if (max_action > maxdata || maxdata - max_action < 1)
10868 : : {
10869 : 0 : invalid_action_table:
10870 : 0 : fputs (_(" <INVALID DATA>\n"), stdout);
10871 : 0 : return;
10872 : : }
10873 : :
10874 : 0 : const unsigned char *const action_table_end
10875 : 0 : = action_table + max_action + 1;
10876 : :
10877 : 0 : u = 0;
10878 : 0 : do
10879 : : {
10880 : 0 : int ar_filter;
10881 : 0 : get_sleb128 (ar_filter, readp, action_table_end);
10882 [ # # ]: 0 : if (ar_filter > 0 && (unsigned int) ar_filter > max_ar_filter)
10883 : 0 : max_ar_filter = ar_filter;
10884 : 0 : int ar_disp;
10885 [ # # ]: 0 : if (readp >= action_table_end)
10886 : 0 : goto invalid_action_table;
10887 : 0 : get_sleb128 (ar_disp, readp, action_table_end);
10888 : :
10889 : 0 : printf (" [%4u] ar_filter: % d\n"
10890 : : " ar_disp: % -5d",
10891 : : u, ar_filter, ar_disp);
10892 [ # # ]: 0 : if (abs (ar_disp) & 1)
10893 : 0 : printf (" -> [%4u]\n", u + (ar_disp + 1) / 2);
10894 [ # # ]: 0 : else if (ar_disp != 0)
10895 : 0 : puts (" -> ???");
10896 : : else
10897 [ # # ]: 0 : putchar_unlocked ('\n');
10898 : 0 : ++u;
10899 : : }
10900 [ # # ]: 0 : while (readp < action_table_end);
10901 : : }
10902 : :
10903 [ # # ]: 0 : if (max_ar_filter > 0 && ttype_base != NULL)
10904 : : {
10905 : 0 : unsigned char dsize;
10906 : 0 : puts ("\n TType table:");
10907 : :
10908 : : // XXX Not *4, size of encoding;
10909 [ # # ]: 0 : switch (ttype_encoding & 7)
10910 : : {
10911 : : case DW_EH_PE_udata2:
10912 : : case DW_EH_PE_sdata2:
10913 : : dsize = 2;
10914 : : break;
10915 : : case DW_EH_PE_udata4:
10916 : : case DW_EH_PE_sdata4:
10917 : : dsize = 4;
10918 : : break;
10919 : : case DW_EH_PE_udata8:
10920 : : case DW_EH_PE_sdata8:
10921 : : dsize = 8;
10922 : : break;
10923 : 0 : default:
10924 : 0 : dsize = 0;
10925 : 0 : error (1, 0, _("invalid TType encoding"));
10926 : : }
10927 : :
10928 : 0 : if (max_ar_filter
10929 [ # # ]: 0 : > (size_t) (ttype_base - (const unsigned char *) data->d_buf) / dsize)
10930 : 0 : goto invalid_data;
10931 : :
10932 : 0 : readp = ttype_base - max_ar_filter * dsize;
10933 : 0 : do
10934 : : {
10935 : 0 : uint64_t ttype;
10936 : 0 : readp = read_encoded (ttype_encoding, readp, ttype_base, &ttype,
10937 : : dbg);
10938 : 0 : printf (" [%4u] %#" PRIx64 "\n", max_ar_filter--, ttype);
10939 : : }
10940 [ # # ]: 0 : while (readp < ttype_base);
10941 : : }
10942 : : }
10943 : :
10944 : : /* Print the content of the '.gdb_index' section.
10945 : : http://sourceware.org/gdb/current/onlinedocs/gdb/Index-Section-Format.html
10946 : : */
10947 : : static void
10948 : 2 : print_gdb_index_section (Dwfl_Module *dwflmod, Ebl *ebl,
10949 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
10950 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
10951 : : {
10952 : 2 : printf (_("\nGDB section [%2zu] '%s' at offset %#" PRIx64
10953 : : " contains %" PRId64 " bytes :\n"),
10954 : : elf_ndxscn (scn), section_name (ebl, shdr),
10955 : 2 : (uint64_t) shdr->sh_offset, (uint64_t) shdr->sh_size);
10956 : :
10957 : 2 : Elf_Data *data = elf_rawdata (scn, NULL);
10958 : :
10959 [ - + ]: 2 : if (unlikely (data == NULL))
10960 : : {
10961 : 0 : error (0, 0, _("cannot get %s content: %s"),
10962 : : ".gdb_index", elf_errmsg (-1));
10963 : 0 : return;
10964 : : }
10965 : :
10966 : : // .gdb_index is always in little endian.
10967 : 2 : Dwarf dummy_dbg = { .other_byte_order = MY_ELFDATA != ELFDATA2LSB };
10968 : 2 : dbg = &dummy_dbg;
10969 : :
10970 : 2 : const unsigned char *readp = data->d_buf;
10971 : 2 : const unsigned char *const dataend = readp + data->d_size;
10972 : :
10973 [ - + ]: 2 : if (unlikely (readp + 4 > dataend))
10974 : : {
10975 : 0 : invalid_data:
10976 : 0 : error (0, 0, _("invalid data"));
10977 : 0 : return;
10978 : : }
10979 : :
10980 : 2 : int32_t vers = read_4ubyte_unaligned (dbg, readp);
10981 : 2 : printf (_(" Version: %" PRId32 "\n"), vers);
10982 : :
10983 : : // The only difference between version 4 and version 5 is the
10984 : : // hash used for generating the table. Version 6 contains symbols
10985 : : // for inlined functions, older versions didn't. Version 7 adds
10986 : : // symbol kinds. Version 8 just indicates that it correctly includes
10987 : : // TUs for symbols.
10988 [ - + ]: 2 : if (vers < 4 || vers > 8)
10989 : : {
10990 : 0 : printf (_(" unknown version, cannot parse section\n"));
10991 : 0 : return;
10992 : : }
10993 : :
10994 : 2 : readp += 4;
10995 [ - + ]: 2 : if (unlikely (readp + 4 > dataend))
10996 : 0 : goto invalid_data;
10997 : :
10998 : 2 : uint32_t cu_off = read_4ubyte_unaligned (dbg, readp);
10999 : 2 : printf (_(" CU offset: %#" PRIx32 "\n"), cu_off);
11000 : :
11001 : 2 : readp += 4;
11002 [ - + ]: 2 : if (unlikely (readp + 4 > dataend))
11003 : 0 : goto invalid_data;
11004 : :
11005 : 2 : uint32_t tu_off = read_4ubyte_unaligned (dbg, readp);
11006 : 2 : printf (_(" TU offset: %#" PRIx32 "\n"), tu_off);
11007 : :
11008 : 2 : readp += 4;
11009 [ - + ]: 2 : if (unlikely (readp + 4 > dataend))
11010 : 0 : goto invalid_data;
11011 : :
11012 : 2 : uint32_t addr_off = read_4ubyte_unaligned (dbg, readp);
11013 : 2 : printf (_(" address offset: %#" PRIx32 "\n"), addr_off);
11014 : :
11015 : 2 : readp += 4;
11016 [ - + ]: 2 : if (unlikely (readp + 4 > dataend))
11017 : 0 : goto invalid_data;
11018 : :
11019 : 2 : uint32_t sym_off = read_4ubyte_unaligned (dbg, readp);
11020 : 2 : printf (_(" symbol offset: %#" PRIx32 "\n"), sym_off);
11021 : :
11022 : 2 : readp += 4;
11023 [ - + ]: 2 : if (unlikely (readp + 4 > dataend))
11024 : 0 : goto invalid_data;
11025 : :
11026 : 2 : uint32_t const_off = read_4ubyte_unaligned (dbg, readp);
11027 : 2 : printf (_(" constant offset: %#" PRIx32 "\n"), const_off);
11028 : :
11029 [ - + ]: 2 : if (unlikely ((size_t) (dataend - (const unsigned char *) data->d_buf)
11030 : : < const_off))
11031 : 0 : goto invalid_data;
11032 : :
11033 : 2 : readp = data->d_buf + cu_off;
11034 : :
11035 : 2 : const unsigned char *nextp = data->d_buf + tu_off;
11036 [ - + ]: 2 : if (tu_off >= data->d_size)
11037 : 0 : goto invalid_data;
11038 : :
11039 : 2 : size_t cu_nr = (nextp - readp) / 16;
11040 : :
11041 : 2 : printf (_("\n CU list at offset %#" PRIx32
11042 : : " contains %zu entries:\n"),
11043 : : cu_off, cu_nr);
11044 : :
11045 : 2 : size_t n = 0;
11046 [ + - + + ]: 6 : while (dataend - readp >= 16 && n < cu_nr)
11047 : : {
11048 : 4 : uint64_t off = read_8ubyte_unaligned (dbg, readp);
11049 : 4 : readp += 8;
11050 : :
11051 : 4 : uint64_t len = read_8ubyte_unaligned (dbg, readp);
11052 : 4 : readp += 8;
11053 : :
11054 : 4 : printf (" [%4zu] start: %0#8" PRIx64
11055 : : ", length: %5" PRIu64 "\n", n, off, len);
11056 : 4 : n++;
11057 : : }
11058 : :
11059 : 2 : readp = data->d_buf + tu_off;
11060 : 2 : nextp = data->d_buf + addr_off;
11061 [ - + ]: 2 : if (addr_off >= data->d_size)
11062 : 0 : goto invalid_data;
11063 : :
11064 : 2 : size_t tu_nr = (nextp - readp) / 24;
11065 : :
11066 : 2 : printf (_("\n TU list at offset %#" PRIx32
11067 : : " contains %zu entries:\n"),
11068 : : tu_off, tu_nr);
11069 : :
11070 : 2 : n = 0;
11071 [ + - + + ]: 4 : while (dataend - readp >= 24 && n < tu_nr)
11072 : : {
11073 : 2 : uint64_t off = read_8ubyte_unaligned (dbg, readp);
11074 : 2 : readp += 8;
11075 : :
11076 : 2 : uint64_t type = read_8ubyte_unaligned (dbg, readp);
11077 : 2 : readp += 8;
11078 : :
11079 : 2 : uint64_t sig = read_8ubyte_unaligned (dbg, readp);
11080 : 2 : readp += 8;
11081 : :
11082 : 2 : printf (" [%4zu] CU offset: %5" PRId64
11083 : : ", type offset: %5" PRId64
11084 : : ", signature: %0#8" PRIx64 "\n", n, off, type, sig);
11085 : 2 : n++;
11086 : : }
11087 : :
11088 : 2 : readp = data->d_buf + addr_off;
11089 : 2 : nextp = data->d_buf + sym_off;
11090 [ - + ]: 2 : if (sym_off >= data->d_size)
11091 : 0 : goto invalid_data;
11092 : :
11093 : 2 : size_t addr_nr = (nextp - readp) / 20;
11094 : :
11095 : 2 : printf (_("\n Address list at offset %#" PRIx32
11096 : : " contains %zu entries:\n"),
11097 : : addr_off, addr_nr);
11098 : :
11099 : 2 : n = 0;
11100 [ + - + + ]: 6 : while (dataend - readp >= 20 && n < addr_nr)
11101 : : {
11102 : 4 : uint64_t low = read_8ubyte_unaligned (dbg, readp);
11103 : 4 : readp += 8;
11104 : :
11105 : 4 : uint64_t high = read_8ubyte_unaligned (dbg, readp);
11106 : 4 : readp += 8;
11107 : :
11108 : 4 : uint32_t idx = read_4ubyte_unaligned (dbg, readp);
11109 : 4 : readp += 4;
11110 : :
11111 : 4 : printf (" [%4zu] ", n);
11112 : 4 : print_dwarf_addr (dwflmod, 8, low, low);
11113 : 4 : printf ("..");
11114 : 4 : print_dwarf_addr (dwflmod, 8, high - 1, high);
11115 : 4 : printf (", CU index: %5" PRId32 "\n", idx);
11116 : 4 : n++;
11117 : : }
11118 : :
11119 : 2 : const unsigned char *const_start = data->d_buf + const_off;
11120 [ - + ]: 2 : if (const_off >= data->d_size)
11121 : 0 : goto invalid_data;
11122 : :
11123 : 2 : readp = data->d_buf + sym_off;
11124 : 2 : nextp = const_start;
11125 : 2 : size_t sym_nr = (nextp - readp) / 8;
11126 : :
11127 : 2 : printf (_("\n Symbol table at offset %#" PRIx32
11128 : : " contains %zu slots:\n"),
11129 : : addr_off, sym_nr);
11130 : :
11131 : 2 : n = 0;
11132 [ + - + + ]: 2050 : while (dataend - readp >= 8 && n < sym_nr)
11133 : : {
11134 [ + + ]: 2048 : uint32_t name = read_4ubyte_unaligned (dbg, readp);
11135 : 2048 : readp += 4;
11136 : :
11137 : 2048 : uint32_t vector = read_4ubyte_unaligned (dbg, readp);
11138 : 2048 : readp += 4;
11139 : :
11140 [ + + ]: 2048 : if (name != 0 || vector != 0)
11141 : : {
11142 : 14 : const unsigned char *sym = const_start + name;
11143 [ + - - + ]: 14 : if (unlikely ((size_t) (dataend - const_start) < name
11144 : : || memchr (sym, '\0', dataend - sym) == NULL))
11145 : 0 : goto invalid_data;
11146 : :
11147 : 14 : printf (" [%4zu] symbol: %s, CUs: ", n, sym);
11148 : :
11149 : 14 : const unsigned char *readcus = const_start + vector;
11150 [ - + ]: 14 : if (unlikely ((size_t) (dataend - const_start) < vector))
11151 : 0 : goto invalid_data;
11152 : 14 : uint32_t cus = read_4ubyte_unaligned (dbg, readcus);
11153 : 14 : while (cus--)
11154 : : {
11155 : 16 : uint32_t cu_kind, cu, kind;
11156 : 16 : bool is_static;
11157 : 16 : readcus += 4;
11158 [ - + ]: 16 : if (unlikely (readcus + 4 > dataend))
11159 : 0 : goto invalid_data;
11160 [ + + ]: 16 : cu_kind = read_4ubyte_unaligned (dbg, readcus);
11161 : 16 : cu = cu_kind & ((1 << 24) - 1);
11162 : 16 : kind = (cu_kind >> 28) & 7;
11163 : 16 : is_static = cu_kind & (1U << 31);
11164 [ + + ]: 16 : if (cu > cu_nr - 1)
11165 : 2 : printf ("%" PRId32 "T", cu - (uint32_t) cu_nr);
11166 : : else
11167 : 14 : printf ("%" PRId32, cu);
11168 [ + + ]: 16 : if (kind != 0)
11169 : : {
11170 : 8 : printf (" (");
11171 [ + + + - : 8 : switch (kind)
- ]
11172 : : {
11173 : : case 1:
11174 : 3 : printf ("type");
11175 : : break;
11176 : : case 2:
11177 : 2 : printf ("var");
11178 : : break;
11179 : : case 3:
11180 : 3 : printf ("func");
11181 : : break;
11182 : : case 4:
11183 : 0 : printf ("other");
11184 : : break;
11185 : : default:
11186 : 0 : printf ("unknown-0x%" PRIx32, kind);
11187 : : break;
11188 : : }
11189 [ + + ]: 8 : printf (":%c)", (is_static ? 'S' : 'G'));
11190 : : }
11191 [ + + ]: 16 : if (cus > 0)
11192 [ + + ]: 32 : printf (", ");
11193 : : }
11194 : 14 : printf ("\n");
11195 : : }
11196 : 2048 : n++;
11197 : : }
11198 : : }
11199 : :
11200 : : /* Returns true and sets split DWARF CU id if there is a split compile
11201 : : unit in the given Dwarf, and no non-split units are found (before it). */
11202 : : static bool
11203 : 187 : is_split_dwarf (Dwarf *dbg, uint64_t *id, Dwarf_CU **split_cu)
11204 : : {
11205 : 187 : Dwarf_CU *cu = NULL;
11206 [ + + ]: 187 : while (dwarf_get_units (dbg, cu, &cu, NULL, NULL, NULL, NULL) == 0)
11207 : : {
11208 : 186 : uint8_t unit_type;
11209 [ + - ]: 186 : if (dwarf_cu_info (cu, NULL, &unit_type, NULL, NULL,
11210 : : id, NULL, NULL) != 0)
11211 : 186 : return false;
11212 : :
11213 [ + + ]: 186 : if (unit_type != DW_UT_split_compile && unit_type != DW_UT_split_type)
11214 : : return false;
11215 : :
11216 : : /* We really only care about the split compile unit, the types
11217 : : should be fine and self sufficient. Also they don't have an
11218 : : id that we can match with a skeleton unit. */
11219 [ + - ]: 9 : if (unit_type == DW_UT_split_compile)
11220 : : {
11221 : 9 : *split_cu = cu;
11222 : 9 : return true;
11223 : : }
11224 : : }
11225 : :
11226 : : return false;
11227 : : }
11228 : :
11229 : : /* Check that there is one and only one Dwfl_Module, return in arg. */
11230 : : static int
11231 : 9 : getone_dwflmod (Dwfl_Module *dwflmod,
11232 : : void **userdata __attribute__ ((unused)),
11233 : : const char *name __attribute__ ((unused)),
11234 : : Dwarf_Addr base __attribute__ ((unused)),
11235 : : void *arg)
11236 : : {
11237 : 9 : Dwfl_Module **m = (Dwfl_Module **) arg;
11238 [ + - ]: 9 : if (*m != NULL)
11239 : : return DWARF_CB_ABORT;
11240 : 9 : *m = dwflmod;
11241 : 9 : return DWARF_CB_OK;
11242 : : }
11243 : :
11244 : : static void
11245 : 269 : print_debug (Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr)
11246 : : {
11247 : : /* Used for skeleton file, if necessary for split DWARF. */
11248 : 269 : Dwfl *skel_dwfl = NULL;
11249 : 269 : Dwfl_Module *skel_mod = NULL;
11250 : 269 : char *skel_name = NULL;
11251 : 269 : Dwarf *split_dbg = NULL;
11252 : 269 : Dwarf_CU *split_cu = NULL;
11253 : :
11254 : : /* Before we start the real work get a debug context descriptor. */
11255 : 269 : Dwarf_Addr dwbias;
11256 : 269 : Dwarf *dbg = dwfl_module_getdwarf (dwflmod, &dwbias);
11257 : 269 : Dwarf dummy_dbg =
11258 : : {
11259 : 269 : .elf = ebl->elf,
11260 : 269 : .other_byte_order = MY_ELFDATA != ehdr->e_ident[EI_DATA]
11261 : : };
11262 [ + + ]: 269 : if (dbg == NULL)
11263 : : {
11264 [ - + ]: 82 : if ((print_debug_sections & ~(section_exception|section_frame)) != 0)
11265 : 0 : error (0, 0, _("cannot get debug context descriptor: %s"),
11266 : : dwfl_errmsg (-1));
11267 : : dbg = &dummy_dbg;
11268 : : }
11269 : : else
11270 : : {
11271 : : /* If we are asked about a split dwarf (.dwo) file, use the user
11272 : : provided, or find the corresponding skeleton file. If we got
11273 : : a skeleton file, replace the given dwflmod and dbg, with one
11274 : : derived from the skeleton file to provide enough context. */
11275 : 187 : uint64_t split_id;
11276 [ + + ]: 187 : if (is_split_dwarf (dbg, &split_id, &split_cu))
11277 : : {
11278 [ + - ]: 9 : if (dwarf_skeleton != NULL)
11279 : 9 : skel_name = strdup (dwarf_skeleton);
11280 : : else
11281 : : {
11282 : : /* Replace file.dwo with file.o and see if that matches. */
11283 : 0 : const char *fname;
11284 : 0 : dwfl_module_info (dwflmod, NULL, NULL, NULL, NULL, NULL,
11285 : : &fname, NULL);
11286 [ # # ]: 0 : if (fname != NULL)
11287 : : {
11288 : 0 : size_t flen = strlen (fname);
11289 [ # # # # ]: 0 : if (flen > 4 && strcmp (".dwo", fname + flen - 4) == 0)
11290 : : {
11291 : 0 : skel_name = strdup (fname);
11292 [ # # ]: 0 : if (skel_name != NULL)
11293 : : {
11294 : 0 : skel_name[flen - 3] = 'o';
11295 : 0 : skel_name[flen - 2] = '\0';
11296 : : }
11297 : : }
11298 : : }
11299 : : }
11300 : :
11301 [ + - ]: 9 : if (skel_name != NULL)
11302 : : {
11303 : 9 : int skel_fd = open (skel_name, O_RDONLY);
11304 [ - + ]: 9 : if (skel_fd == -1)
11305 : 0 : fprintf (stderr, "Warning: Couldn't open DWARF skeleton file"
11306 : : " '%s'\n", skel_name);
11307 : : else
11308 : 9 : skel_dwfl = create_dwfl (skel_fd, skel_name);
11309 : :
11310 [ + - ]: 9 : if (skel_dwfl != NULL)
11311 : : {
11312 [ - + ]: 9 : if (dwfl_getmodules (skel_dwfl, &getone_dwflmod,
11313 : : &skel_mod, 0) != 0)
11314 : : {
11315 : 0 : fprintf (stderr, "Warning: Bad DWARF skeleton,"
11316 : : " multiple modules '%s'\n", skel_name);
11317 : 0 : dwfl_end (skel_dwfl);
11318 : 0 : skel_mod = NULL;
11319 : : }
11320 : : }
11321 [ # # ]: 0 : else if (skel_fd != -1)
11322 : 0 : fprintf (stderr, "Warning: Couldn't create skeleton dwfl for"
11323 : : " '%s': %s\n", skel_name, dwfl_errmsg (-1));
11324 : :
11325 [ + - ]: 9 : if (skel_mod != NULL)
11326 : : {
11327 : 9 : Dwarf *skel_dbg = dwfl_module_getdwarf (skel_mod, &dwbias);
11328 [ + - ]: 9 : if (skel_dbg != NULL)
11329 : : {
11330 : : /* First check the skeleton CU DIE, only fetch
11331 : : the split DIE if we know the id matches to
11332 : : not unnecessary search for any split DIEs we
11333 : : don't need. */
11334 : 9 : Dwarf_CU *cu = NULL;
11335 [ + - ]: 13 : while (dwarf_get_units (skel_dbg, cu, &cu,
11336 : : NULL, NULL, NULL, NULL) == 0)
11337 : : {
11338 : 13 : uint8_t unit_type;
11339 : 13 : uint64_t skel_id;
11340 [ + - ]: 13 : if (dwarf_cu_info (cu, NULL, &unit_type, NULL, NULL,
11341 : : &skel_id, NULL, NULL) == 0
11342 [ + - ]: 13 : && unit_type == DW_UT_skeleton
11343 [ + + ]: 13 : && split_id == skel_id)
11344 : : {
11345 : 9 : Dwarf_Die subdie;
11346 [ + - ]: 9 : if (dwarf_cu_info (cu, NULL, NULL, NULL,
11347 : : &subdie,
11348 : : NULL, NULL, NULL) == 0
11349 [ + - ]: 9 : && dwarf_tag (&subdie) != DW_TAG_invalid)
11350 : : {
11351 : 9 : split_dbg = dwarf_cu_getdwarf (subdie.cu);
11352 [ - + ]: 9 : if (split_dbg == NULL)
11353 : 0 : fprintf (stderr,
11354 : : "Warning: Couldn't get split_dbg:"
11355 : : " %s\n", dwarf_errmsg (-1));
11356 : 9 : break;
11357 : : }
11358 : : else
11359 : : {
11360 : : /* Everything matches up, but not
11361 : : according to libdw. Which means
11362 : : the user knew better. So...
11363 : : Terrible hack... We can never
11364 : : destroy the underlying dwfl
11365 : : because it would free the wrong
11366 : : Dwarfs... So we leak memory...*/
11367 [ # # ]: 0 : if (cu->split == NULL
11368 [ # # ]: 0 : && dwarf_skeleton != NULL)
11369 : : {
11370 : 0 : do_not_close_dwfl = true;
11371 : 0 : __libdw_link_skel_split (cu, split_cu);
11372 : 0 : split_dbg = dwarf_cu_getdwarf (split_cu);
11373 : 0 : break;
11374 : : }
11375 : : else
11376 : 0 : fprintf (stderr, "Warning: Couldn't get"
11377 : : " skeleton subdie: %s\n",
11378 : : dwarf_errmsg (-1));
11379 : : }
11380 : : }
11381 : : }
11382 [ - + ]: 9 : if (split_dbg == NULL)
11383 : 9 : fprintf (stderr, "Warning: '%s' didn't contain a skeleton for split id %" PRIx64 "\n", skel_name, split_id);
11384 : : }
11385 : : else
11386 : 0 : fprintf (stderr, "Warning: Couldn't get skeleton DWARF:"
11387 : : " %s\n", dwfl_errmsg (-1));
11388 : : }
11389 : : }
11390 : :
11391 [ + - ]: 9 : if (split_dbg != NULL)
11392 : : {
11393 : 9 : dbg = split_dbg;
11394 : 9 : dwflmod = skel_mod;
11395 : : }
11396 [ # # ]: 0 : else if (skel_name == NULL)
11397 : 0 : fprintf (stderr,
11398 : : "Warning: split DWARF file, but no skeleton found.\n");
11399 : : }
11400 [ - + ]: 178 : else if (dwarf_skeleton != NULL)
11401 : 187 : fprintf (stderr, "Warning: DWARF skeleton given,"
11402 : : " but not a split DWARF file\n");
11403 : : }
11404 : :
11405 : : /* Get the section header string table index. */
11406 : 269 : size_t shstrndx;
11407 [ - + ]: 269 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
11408 : 0 : error (EXIT_FAILURE, 0,
11409 : 0 : _("cannot get section header string table index"));
11410 : :
11411 : : /* If the .debug_info section is listed as implicitly required then
11412 : : we must make sure to handle it before handling any other debug
11413 : : section. Various other sections depend on the CU DIEs being
11414 : : scanned (silently) first. */
11415 : 269 : bool implicit_info = (implicit_debug_sections & section_info) != 0;
11416 : 269 : bool explicit_info = (print_debug_sections & section_info) != 0;
11417 [ + + ]: 269 : if (implicit_info)
11418 : : {
11419 : : Elf_Scn *scn = NULL;
11420 [ + - ]: 1458 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
11421 : : {
11422 : 1458 : GElf_Shdr shdr_mem;
11423 : 1458 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
11424 : :
11425 [ + - + + ]: 1458 : if (shdr != NULL && shdr->sh_type == SHT_PROGBITS)
11426 : : {
11427 : 1182 : const char *name = elf_strptr (ebl->elf, shstrndx,
11428 : 591 : shdr->sh_name);
11429 [ - + ]: 591 : if (name == NULL)
11430 : 0 : continue;
11431 : :
11432 [ + + ]: 591 : if (strcmp (name, ".debug_info") == 0
11433 [ + + ]: 529 : || strcmp (name, ".debug_info.dwo") == 0
11434 [ + + ]: 520 : || strcmp (name, ".zdebug_info") == 0
11435 [ + - ]: 514 : || strcmp (name, ".zdebug_info.dwo") == 0
11436 [ + + ]: 514 : || strcmp (name, ".gnu.debuglto_.debug_info") == 0)
11437 : : {
11438 : 78 : print_debug_info_section (dwflmod, ebl, ehdr,
11439 : : scn, shdr, dbg);
11440 : 78 : break;
11441 : : }
11442 : : }
11443 : : }
11444 : 78 : print_debug_sections &= ~section_info;
11445 : 78 : implicit_debug_sections &= ~section_info;
11446 : : }
11447 : :
11448 : : /* Look through all the sections for the debugging sections to print. */
11449 : : Elf_Scn *scn = NULL;
11450 [ + + ]: 8314 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
11451 : : {
11452 : 8045 : GElf_Shdr shdr_mem;
11453 : 8045 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
11454 : :
11455 [ + - + + ]: 8045 : if (shdr != NULL && shdr->sh_type == SHT_PROGBITS)
11456 : : {
11457 : 3429 : static const struct
11458 : : {
11459 : : const char *name;
11460 : : enum section_e bitmask;
11461 : : void (*fp) (Dwfl_Module *, Ebl *,
11462 : : GElf_Ehdr *, Elf_Scn *, GElf_Shdr *, Dwarf *);
11463 : : } debug_sections[] =
11464 : : {
11465 : : #define NEW_SECTION(name) \
11466 : : { ".debug_" #name, section_##name, print_debug_##name##_section }
11467 : : NEW_SECTION (abbrev),
11468 : : NEW_SECTION (addr),
11469 : : NEW_SECTION (aranges),
11470 : : NEW_SECTION (frame),
11471 : : NEW_SECTION (info),
11472 : : NEW_SECTION (types),
11473 : : NEW_SECTION (line),
11474 : : NEW_SECTION (loc),
11475 : : /* loclists is loc for DWARF5. */
11476 : : { ".debug_loclists", section_loc,
11477 : : print_debug_loclists_section },
11478 : : NEW_SECTION (pubnames),
11479 : : NEW_SECTION (str),
11480 : : /* A DWARF5 specialised debug string section. */
11481 : : { ".debug_line_str", section_str,
11482 : : print_debug_str_section },
11483 : : /* DWARF5 string offsets table. */
11484 : : { ".debug_str_offsets", section_str,
11485 : : print_debug_str_offsets_section },
11486 : : NEW_SECTION (macinfo),
11487 : : NEW_SECTION (macro),
11488 : : NEW_SECTION (ranges),
11489 : : /* rnglists is ranges for DWARF5. */
11490 : : { ".debug_rnglists", section_ranges,
11491 : : print_debug_rnglists_section },
11492 : : { ".eh_frame", section_frame | section_exception,
11493 : : print_debug_frame_section },
11494 : : { ".eh_frame_hdr", section_frame | section_exception,
11495 : : print_debug_frame_hdr_section },
11496 : : { ".gcc_except_table", section_frame | section_exception,
11497 : : print_debug_exception_table },
11498 : : { ".gdb_index", section_gdb_index, print_gdb_index_section }
11499 : : };
11500 : 3429 : const int ndebug_sections = (sizeof (debug_sections)
11501 : : / sizeof (debug_sections[0]));
11502 : 6858 : const char *name = elf_strptr (ebl->elf, shstrndx,
11503 : 3429 : shdr->sh_name);
11504 [ - + ]: 3429 : if (name == NULL)
11505 : 0 : continue;
11506 : :
11507 : : int n;
11508 [ + + ]: 55557 : for (n = 0; n < ndebug_sections; ++n)
11509 : : {
11510 : 53673 : size_t dbglen = strlen (debug_sections[n].name);
11511 : 53673 : size_t scnlen = strlen (name);
11512 [ + + ]: 53673 : if ((strncmp (name, debug_sections[n].name, dbglen) == 0
11513 [ + + ]: 1602 : && (dbglen == scnlen
11514 [ + + ]: 198 : || (scnlen == dbglen + 4
11515 [ + + ]: 180 : && strstr (name, ".dwo") == name + dbglen)))
11516 [ + + + + ]: 52215 : || (name[0] == '.' && name[1] == 'z'
11517 [ + - ]: 566 : && debug_sections[n].name[1] == 'd'
11518 [ + + ]: 566 : && strncmp (&name[2], &debug_sections[n].name[1],
11519 : : dbglen - 1) == 0
11520 [ - + ]: 82 : && (scnlen == dbglen + 1
11521 [ # # ]: 0 : || (scnlen == dbglen + 5
11522 [ # # ]: 0 : && strstr (name, ".dwo") == name + dbglen + 1)))
11523 [ + + ]: 52133 : || (scnlen > 14 /* .gnu.debuglto_ prefix. */
11524 [ + + ]: 3938 : && startswith (name, ".gnu.debuglto_")
11525 [ + + ]: 36 : && strcmp (&name[14], debug_sections[n].name) == 0)
11526 : : )
11527 : : {
11528 : 1545 : if ((print_debug_sections | implicit_debug_sections)
11529 [ + + ]: 1545 : & debug_sections[n].bitmask)
11530 : 511 : debug_sections[n].fp (dwflmod, ebl, ehdr, scn, shdr, dbg);
11531 : : break;
11532 : : }
11533 : : }
11534 : : }
11535 : : }
11536 : :
11537 : 269 : dwfl_end (skel_dwfl);
11538 : 269 : free (skel_name);
11539 : :
11540 : : /* Turn implicit and/or explicit back on in case we go over another file. */
11541 [ + + ]: 269 : if (implicit_info)
11542 : 78 : implicit_debug_sections |= section_info;
11543 [ + + ]: 269 : if (explicit_info)
11544 : 62 : print_debug_sections |= section_info;
11545 : :
11546 : 269 : reset_listptr (&known_locsptr);
11547 : 269 : reset_listptr (&known_loclistsptr);
11548 : 269 : reset_listptr (&known_rangelistptr);
11549 : 269 : reset_listptr (&known_rnglistptr);
11550 : 269 : reset_listptr (&known_addrbases);
11551 : 269 : reset_listptr (&known_stroffbases);
11552 : 269 : }
11553 : :
11554 : :
11555 : : #define ITEM_INDENT 4
11556 : : #define WRAP_COLUMN 75
11557 : :
11558 : : /* Print "NAME: FORMAT", wrapping when output text would make the line
11559 : : exceed WRAP_COLUMN. Unpadded numbers look better for the core items
11560 : : but this function is also used for registers which should be printed
11561 : : aligned. Fortunately registers output uses fixed fields width (such
11562 : : as %11d) for the alignment.
11563 : :
11564 : : Line breaks should not depend on the particular values although that
11565 : : may happen in some cases of the core items. */
11566 : :
11567 : : static unsigned int
11568 : : __attribute__ ((format (printf, 6, 7)))
11569 : 821 : print_core_item (unsigned int colno, char sep, unsigned int wrap,
11570 : : size_t name_width, const char *name, const char *format, ...)
11571 : : {
11572 : 821 : size_t len = strlen (name);
11573 : 821 : if (name_width < len)
11574 : : name_width = len;
11575 : :
11576 : 821 : char *out;
11577 : 821 : va_list ap;
11578 : 821 : va_start (ap, format);
11579 : 821 : int out_len = vasprintf (&out, format, ap);
11580 : 821 : va_end (ap);
11581 [ - + ]: 821 : if (out_len == -1)
11582 : 0 : error (EXIT_FAILURE, 0, _("memory exhausted"));
11583 : :
11584 : 821 : size_t n = name_width + sizeof ": " - 1 + out_len;
11585 : :
11586 [ + + ]: 821 : if (colno == 0)
11587 : : {
11588 : 51 : printf ("%*s", ITEM_INDENT, "");
11589 : 51 : colno = ITEM_INDENT + n;
11590 : : }
11591 [ + + ]: 770 : else if (colno + 2 + n < wrap)
11592 : : {
11593 : 468 : printf ("%c ", sep);
11594 : 468 : colno += 2 + n;
11595 : : }
11596 : : else
11597 : : {
11598 : 302 : printf ("\n%*s", ITEM_INDENT, "");
11599 : 302 : colno = ITEM_INDENT + n;
11600 : : }
11601 : :
11602 : 821 : printf ("%s: %*s%s", name, (int) (name_width - len), "", out);
11603 : :
11604 : 821 : free (out);
11605 : :
11606 : 821 : return colno;
11607 : : }
11608 : :
11609 : : static const void *
11610 : 931 : convert (Elf *core, Elf_Type type, uint_fast16_t count,
11611 : : void *value, const void *data, size_t size)
11612 : : {
11613 : 1862 : Elf_Data valuedata =
11614 : : {
11615 : : .d_type = type,
11616 : : .d_buf = value,
11617 [ + + ]: 931 : .d_size = size ?: gelf_fsize (core, type, count, EV_CURRENT),
11618 : : .d_version = EV_CURRENT,
11619 : : };
11620 : 931 : Elf_Data indata =
11621 : : {
11622 : : .d_type = type,
11623 : : .d_buf = (void *) data,
11624 : : .d_size = valuedata.d_size,
11625 : : .d_version = EV_CURRENT,
11626 : : };
11627 : :
11628 : 931 : Elf_Data *d = (gelf_getclass (core) == ELFCLASS32
11629 [ + + ]: 931 : ? elf32_xlatetom : elf64_xlatetom)
11630 : 931 : (&valuedata, &indata, elf_getident (core, NULL)[EI_DATA]);
11631 [ - + ]: 931 : if (d == NULL)
11632 : 0 : error (EXIT_FAILURE, 0,
11633 : 0 : _("cannot convert core note data: %s"), elf_errmsg (-1));
11634 : :
11635 : 931 : return data + indata.d_size;
11636 : : }
11637 : :
11638 : : typedef uint8_t GElf_Byte;
11639 : :
11640 : : static unsigned int
11641 : 400 : handle_core_item (Elf *core, const Ebl_Core_Item *item, const void *desc,
11642 : : unsigned int colno, size_t *repeated_size)
11643 : : {
11644 [ + + ]: 400 : uint_fast16_t count = item->count ?: 1;
11645 : : /* Ebl_Core_Item count is always a small number.
11646 : : Make sure the backend didn't put in some large bogus value. */
11647 [ - + ]: 62 : assert (count < 128);
11648 : :
11649 : : #define TYPES \
11650 : : DO_TYPE (BYTE, Byte, "0x%.2" PRIx8, "%" PRId8); \
11651 : : DO_TYPE (HALF, Half, "0x%.4" PRIx16, "%" PRId16); \
11652 : : DO_TYPE (WORD, Word, "0x%.8" PRIx32, "%" PRId32); \
11653 : : DO_TYPE (SWORD, Sword, "%" PRId32, "%" PRId32); \
11654 : : DO_TYPE (XWORD, Xword, "0x%.16" PRIx64, "%" PRId64); \
11655 : : DO_TYPE (SXWORD, Sxword, "%" PRId64, "%" PRId64)
11656 : :
11657 : : #define DO_TYPE(NAME, Name, hex, dec) GElf_##Name Name
11658 : 400 : typedef union { TYPES; } value_t;
11659 : 400 : void *data = alloca (count * sizeof (value_t));
11660 : : #undef DO_TYPE
11661 : :
11662 : : #define DO_TYPE(NAME, Name, hex, dec) \
11663 : : GElf_##Name *value_##Name __attribute__((unused)) = data
11664 : 400 : TYPES;
11665 : : #undef DO_TYPE
11666 : :
11667 : 400 : size_t size = gelf_fsize (core, item->type, count, EV_CURRENT);
11668 : 400 : size_t convsize = size;
11669 [ + + ]: 400 : if (repeated_size != NULL)
11670 : : {
11671 [ + - - + ]: 1 : if (*repeated_size > size && (item->format == 'b' || item->format == 'B'))
11672 : : {
11673 : 0 : data = alloca (*repeated_size);
11674 : 0 : count *= *repeated_size / size;
11675 : 0 : convsize = count * size;
11676 : 0 : *repeated_size -= convsize;
11677 : : }
11678 [ + - - + ]: 1 : else if (item->count != 0 || item->format != '\n')
11679 : 0 : *repeated_size -= size;
11680 : : }
11681 : :
11682 : 400 : convert (core, item->type, count, data, desc + item->offset, convsize);
11683 : :
11684 : 400 : Elf_Type type = item->type;
11685 [ + + ]: 400 : if (type == ELF_T_ADDR)
11686 [ + - ]: 1 : type = gelf_getclass (core) == ELFCLASS32 ? ELF_T_WORD : ELF_T_XWORD;
11687 : :
11688 [ + + + + : 400 : switch (item->format)
+ + + -
+ ]
11689 : : {
11690 : 193 : case 'd':
11691 [ - + ]: 193 : assert (count == 1);
11692 [ + + + + : 193 : switch (type)
- + - ]
11693 : : {
11694 : : #define DO_TYPE(NAME, Name, hex, dec) \
11695 : : case ELF_T_##NAME: \
11696 : : colno = print_core_item (colno, ',', WRAP_COLUMN, \
11697 : : 0, item->name, dec, value_##Name[0]); \
11698 : : break
11699 : 193 : TYPES;
11700 : : #undef DO_TYPE
11701 : 0 : default:
11702 : 0 : abort ();
11703 : : }
11704 : : break;
11705 : :
11706 : 111 : case 'x':
11707 [ - + ]: 111 : assert (count == 1);
11708 [ - - + - : 111 : switch (type)
+ - - ]
11709 : : {
11710 : : #define DO_TYPE(NAME, Name, hex, dec) \
11711 : : case ELF_T_##NAME: \
11712 : : colno = print_core_item (colno, ',', WRAP_COLUMN, \
11713 : : 0, item->name, hex, value_##Name[0]); \
11714 : : break
11715 : 111 : TYPES;
11716 : : #undef DO_TYPE
11717 : 0 : default:
11718 : 0 : abort ();
11719 : : }
11720 : : break;
11721 : :
11722 : 22 : case 'b':
11723 : : case 'B':
11724 [ - + ]: 22 : assert (size % sizeof (unsigned int) == 0);
11725 : 22 : unsigned int nbits = count * size * 8;
11726 : 22 : unsigned int pop = 0;
11727 [ + + ]: 56 : for (const unsigned int *i = data; (void *) i < data + count * size; ++i)
11728 : 34 : pop += __builtin_popcount (*i);
11729 : 22 : bool negate = pop > nbits / 2;
11730 : 22 : const unsigned int bias = item->format == 'b';
11731 : :
11732 : 22 : {
11733 [ - + ]: 22 : char printed[(negate ? nbits - pop : pop) * 16 + 1];
11734 : 22 : char *p = printed;
11735 : 22 : *p = '\0';
11736 : :
11737 : 22 : if (BYTE_ORDER != LITTLE_ENDIAN && size > sizeof (unsigned int))
11738 : : {
11739 : : assert (size == sizeof (unsigned int) * 2);
11740 : : for (unsigned int *i = data;
11741 : : (void *) i < data + count * size; i += 2)
11742 : : {
11743 : : unsigned int w = i[1];
11744 : : i[1] = i[0];
11745 : : i[0] = w;
11746 : : }
11747 : : }
11748 : :
11749 : 22 : unsigned int lastbit = 0;
11750 : 22 : unsigned int run = 0;
11751 : 22 : for (const unsigned int *i = data;
11752 [ + + ]: 56 : (void *) i < data + count * size; ++i)
11753 : : {
11754 : 34 : unsigned int bit = ((void *) i - data) * 8;
11755 [ - + ]: 34 : unsigned int w = negate ? ~*i : *i;
11756 [ - + ]: 34 : while (w != 0)
11757 : : {
11758 : : /* Note that a right shift equal to (or greater than)
11759 : : the number of bits of w is undefined behaviour. In
11760 : : particular when the least significant bit is bit 32
11761 : : (w = 0x8000000) then w >>= n is undefined. So
11762 : : explicitly handle that case separately. */
11763 : 0 : unsigned int n = ffs (w);
11764 [ # # ]: 0 : if (n < sizeof (w) * 8)
11765 : 0 : w >>= n;
11766 : : else
11767 : : w = 0;
11768 : 0 : bit += n;
11769 : :
11770 [ # # # # ]: 0 : if (lastbit != 0 && lastbit + 1 == bit)
11771 : 0 : ++run;
11772 : : else
11773 : : {
11774 [ # # ]: 0 : if (lastbit == 0)
11775 : 0 : p += sprintf (p, "%u", bit - bias);
11776 [ # # ]: 0 : else if (run == 0)
11777 : 0 : p += sprintf (p, ",%u", bit - bias);
11778 : : else
11779 : 0 : p += sprintf (p, "-%u,%u", lastbit - bias, bit - bias);
11780 : : run = 0;
11781 : : }
11782 : :
11783 : : lastbit = bit;
11784 : : }
11785 : : }
11786 [ - + - - ]: 22 : if (lastbit > 0 && run > 0 && lastbit + 1 != nbits)
11787 : 0 : p += sprintf (p, "-%u", lastbit - bias);
11788 : :
11789 [ + - ]: 44 : colno = print_core_item (colno, ',', WRAP_COLUMN, 0, item->name,
11790 : : negate ? "~<%s>" : "<%s>", printed);
11791 : : }
11792 : 22 : break;
11793 : :
11794 : 44 : case 'T':
11795 : : case (char) ('T'|0x80):
11796 [ - + ]: 44 : assert (count == 2);
11797 : 44 : Dwarf_Word sec;
11798 : 44 : Dwarf_Word usec;
11799 [ - - + - : 44 : switch (type)
+ - - ]
11800 : : {
11801 : : #define DO_TYPE(NAME, Name, hex, dec) \
11802 : : case ELF_T_##NAME: \
11803 : : sec = value_##Name[0]; \
11804 : : usec = value_##Name[1]; \
11805 : : break
11806 : 44 : TYPES;
11807 : : #undef DO_TYPE
11808 : 0 : default:
11809 : 0 : abort ();
11810 : : }
11811 [ - + ]: 44 : if (unlikely (item->format == (char) ('T'|0x80)))
11812 : : {
11813 : : /* This is a hack for an ill-considered 64-bit ABI where
11814 : : tv_usec is actually a 32-bit field with 32 bits of padding
11815 : : rounding out struct timeval. We've already converted it as
11816 : : a 64-bit field. For little-endian, this just means the
11817 : : high half is the padding; it's presumably zero, but should
11818 : : be ignored anyway. For big-endian, it means the 32-bit
11819 : : field went into the high half of USEC. */
11820 : 0 : GElf_Ehdr ehdr_mem;
11821 : 0 : GElf_Ehdr *ehdr = gelf_getehdr (core, &ehdr_mem);
11822 [ # # ]: 0 : if (likely (ehdr->e_ident[EI_DATA] == ELFDATA2MSB))
11823 : 0 : usec >>= 32;
11824 : : else
11825 : 0 : usec &= UINT32_MAX;
11826 : : }
11827 : 44 : colno = print_core_item (colno, ',', WRAP_COLUMN, 0, item->name,
11828 : : "%" PRIu64 ".%.6" PRIu64, sec, usec);
11829 : 44 : break;
11830 : :
11831 : 9 : case 'c':
11832 [ - + ]: 9 : assert (count == 1);
11833 : 9 : colno = print_core_item (colno, ',', WRAP_COLUMN, 0, item->name,
11834 : 9 : "%c", value_Byte[0]);
11835 : 9 : break;
11836 : :
11837 : 18 : case 's':
11838 : 18 : colno = print_core_item (colno, ',', WRAP_COLUMN, 0, item->name,
11839 : : "%.*s", (int) count, value_Byte);
11840 : 18 : break;
11841 : :
11842 : 1 : case '\n':
11843 : : /* This is a list of strings separated by '\n'. */
11844 [ - + ]: 1 : assert (item->count == 0);
11845 [ - + ]: 1 : assert (repeated_size != NULL);
11846 [ - + ]: 1 : assert (item->name == NULL);
11847 [ - + ]: 1 : if (unlikely (item->offset >= *repeated_size))
11848 : : break;
11849 : :
11850 : 1 : const char *s = desc + item->offset;
11851 : 1 : size = *repeated_size - item->offset;
11852 : 1 : *repeated_size = 0;
11853 [ + - ]: 48 : while (size > 0)
11854 : : {
11855 : 48 : const char *eol = memchr (s, '\n', size);
11856 : 48 : int len = size;
11857 [ + + ]: 48 : if (eol != NULL)
11858 : 47 : len = eol - s;
11859 : 48 : printf ("%*s%.*s\n", ITEM_INDENT, "", len, s);
11860 [ + + ]: 48 : if (eol == NULL)
11861 : : break;
11862 : 47 : size -= eol + 1 - s;
11863 : 47 : s = eol + 1;
11864 : : }
11865 : :
11866 : : colno = WRAP_COLUMN;
11867 : : break;
11868 : :
11869 : : case 'h':
11870 : : break;
11871 : :
11872 : 0 : default:
11873 : 0 : error (0, 0, "XXX not handling format '%c' for %s",
11874 : : item->format, item->name);
11875 : : break;
11876 : : }
11877 : :
11878 : : #undef TYPES
11879 : :
11880 : 0 : return colno;
11881 : : }
11882 : :
11883 : :
11884 : : /* Sort items by group, and by layout offset within each group. */
11885 : : static int
11886 : 895 : compare_core_items (const void *a, const void *b)
11887 : : {
11888 : 895 : const Ebl_Core_Item *const *p1 = a;
11889 : 895 : const Ebl_Core_Item *const *p2 = b;
11890 : 895 : const Ebl_Core_Item *item1 = *p1;
11891 : 895 : const Ebl_Core_Item *item2 = *p2;
11892 : :
11893 : 895 : return ((item1->group == item2->group ? 0
11894 [ + + ]: 895 : : strcmp (item1->group, item2->group))
11895 [ - + ]: 895 : ?: (int) item1->offset - (int) item2->offset);
11896 : : }
11897 : :
11898 : : /* Sort item groups by layout offset of the first item in the group. */
11899 : : static int
11900 : 139 : compare_core_item_groups (const void *a, const void *b)
11901 : : {
11902 : 139 : const Ebl_Core_Item *const *const *p1 = a;
11903 : 139 : const Ebl_Core_Item *const *const *p2 = b;
11904 : 139 : const Ebl_Core_Item *const *group1 = *p1;
11905 : 139 : const Ebl_Core_Item *const *group2 = *p2;
11906 : 139 : const Ebl_Core_Item *item1 = *group1;
11907 : 139 : const Ebl_Core_Item *item2 = *group2;
11908 : :
11909 : 139 : return (int) item1->offset - (int) item2->offset;
11910 : : }
11911 : :
11912 : : static unsigned int
11913 : 37 : handle_core_items (Elf *core, const void *desc, size_t descsz,
11914 : : const Ebl_Core_Item *items, size_t nitems)
11915 : : {
11916 [ + + ]: 37 : if (nitems == 0)
11917 : : return 0;
11918 : 34 : unsigned int colno = 0;
11919 : :
11920 : : /* FORMAT '\n' makes sense to be present only as a single item as it
11921 : : processes all the data of a note. FORMATs 'b' and 'B' have a special case
11922 : : if present as a single item but they can be also processed with other
11923 : : items below. */
11924 [ + + + + ]: 34 : if (nitems == 1 && (items[0].format == '\n' || items[0].format == 'b'
11925 [ + - ]: 8 : || items[0].format == 'B'))
11926 : : {
11927 [ - + ]: 1 : assert (items[0].offset == 0);
11928 : 1 : size_t size = descsz;
11929 : 1 : colno = handle_core_item (core, items, desc, colno, &size);
11930 : : /* If SIZE is not zero here there is some remaining data. But we do not
11931 : : know how to process it anyway. */
11932 : 1 : return colno;
11933 : : }
11934 [ + + ]: 424 : for (size_t i = 0; i < nitems; ++i)
11935 [ - + ]: 391 : assert (items[i].format != '\n');
11936 : :
11937 : : /* Sort to collect the groups together. */
11938 : 33 : const Ebl_Core_Item *sorted_items[nitems];
11939 [ + + ]: 424 : for (size_t i = 0; i < nitems; ++i)
11940 : 391 : sorted_items[i] = &items[i];
11941 : 33 : qsort (sorted_items, nitems, sizeof sorted_items[0], &compare_core_items);
11942 : :
11943 : : /* Collect the unique groups and sort them. */
11944 : 33 : const Ebl_Core_Item **groups[nitems];
11945 : 33 : groups[0] = &sorted_items[0];
11946 : 33 : size_t ngroups = 1;
11947 [ + + ]: 391 : for (size_t i = 1; i < nitems; ++i)
11948 [ + + ]: 358 : if (sorted_items[i]->group != sorted_items[i - 1]->group
11949 [ + - ]: 75 : && strcmp (sorted_items[i]->group, sorted_items[i - 1]->group))
11950 : 75 : groups[ngroups++] = &sorted_items[i];
11951 : 33 : qsort (groups, ngroups, sizeof groups[0], &compare_core_item_groups);
11952 : :
11953 : : /* Write out all the groups. */
11954 : 33 : const void *last = desc;
11955 : 35 : do
11956 : : {
11957 [ + + ]: 145 : for (size_t i = 0; i < ngroups; ++i)
11958 : : {
11959 : 110 : for (const Ebl_Core_Item **item = groups[i];
11960 [ + + ]: 509 : (item < &sorted_items[nitems]
11961 [ + + ]: 474 : && ((*item)->group == groups[i][0]->group
11962 [ - + ]: 75 : || !strcmp ((*item)->group, groups[i][0]->group)));
11963 : 399 : ++item)
11964 : 399 : colno = handle_core_item (core, *item, desc, colno, NULL);
11965 : :
11966 : : /* Force a line break at the end of the group. */
11967 : 110 : colno = WRAP_COLUMN;
11968 : : }
11969 : :
11970 [ + + ]: 35 : if (descsz == 0)
11971 : : break;
11972 : :
11973 : : /* This set of items consumed a certain amount of the note's data.
11974 : : If there is more data there, we have another unit of the same size.
11975 : : Loop to print that out too. */
11976 : 20 : const Ebl_Core_Item *item = &items[nitems - 1];
11977 : 40 : size_t eltsz = item->offset + gelf_fsize (core, item->type,
11978 : 20 : item->count ?: 1, EV_CURRENT);
11979 : :
11980 : 20 : int reps = -1;
11981 : 20 : do
11982 : : {
11983 : 20 : ++reps;
11984 : 20 : desc += eltsz;
11985 : 20 : descsz -= eltsz;
11986 : : }
11987 [ + + - + ]: 20 : while (descsz >= eltsz && !memcmp (desc, last, eltsz));
11988 : :
11989 [ + - ]: 20 : if (reps == 1)
11990 : : {
11991 : : /* For just one repeat, print it unabridged twice. */
11992 : 20 : desc -= eltsz;
11993 : 20 : descsz += eltsz;
11994 : : }
11995 [ - + ]: 20 : else if (reps > 1)
11996 : 0 : printf (_("\n%*s... <repeats %u more times> ..."),
11997 : : ITEM_INDENT, "", reps);
11998 : :
11999 : 20 : last = desc;
12000 : : }
12001 [ + + ]: 20 : while (descsz > 0);
12002 : :
12003 : 33 : return colno;
12004 : : }
12005 : :
12006 : : static unsigned int
12007 : 0 : handle_bit_registers (const Ebl_Register_Location *regloc, const void *desc,
12008 : : unsigned int colno)
12009 : : {
12010 : 0 : desc += regloc->offset;
12011 : :
12012 : 0 : abort (); /* XXX */
12013 : : return colno;
12014 : : }
12015 : :
12016 : :
12017 : : static unsigned int
12018 : 162 : handle_core_register (Ebl *ebl, Elf *core, int maxregname,
12019 : : const Ebl_Register_Location *regloc, const void *desc,
12020 : : unsigned int colno)
12021 : : {
12022 [ - + ]: 162 : if (regloc->bits % 8 != 0)
12023 : 0 : return handle_bit_registers (regloc, desc, colno);
12024 : :
12025 : 162 : desc += regloc->offset;
12026 : :
12027 [ + + ]: 586 : for (int reg = regloc->regno; reg < regloc->regno + regloc->count; ++reg)
12028 : : {
12029 : 424 : char name[REGNAMESZ];
12030 : 424 : int bits;
12031 : 424 : int type;
12032 : 424 : register_info (ebl, reg, regloc, name, &bits, &type);
12033 : :
12034 : : #define TYPES \
12035 : : BITS (8, BYTE, "%4" PRId8, "0x%.2" PRIx8); \
12036 : : BITS (16, HALF, "%6" PRId16, "0x%.4" PRIx16); \
12037 : : BITS (32, WORD, "%11" PRId32, " 0x%.8" PRIx32); \
12038 : : BITS (64, XWORD, "%20" PRId64, " 0x%.16" PRIx64)
12039 : :
12040 : : #define BITS(bits, xtype, sfmt, ufmt) \
12041 : : uint##bits##_t b##bits; int##bits##_t b##bits##s
12042 : 424 : union { TYPES; uint64_t b128[2]; } value;
12043 : : #undef BITS
12044 : :
12045 [ + + ]: 424 : switch (type)
12046 : : {
12047 : 336 : case DW_ATE_unsigned:
12048 : : case DW_ATE_signed:
12049 : : case DW_ATE_address:
12050 [ - + + + : 336 : switch (bits)
+ - ]
12051 : : {
12052 : : #define BITS(bits, xtype, sfmt, ufmt) \
12053 : : case bits: \
12054 : : desc = convert (core, ELF_T_##xtype, 1, &value, desc, 0); \
12055 : : if (type == DW_ATE_signed) \
12056 : : colno = print_core_item (colno, ' ', WRAP_COLUMN, \
12057 : : maxregname, name, \
12058 : : sfmt, value.b##bits##s); \
12059 : : else \
12060 : : colno = print_core_item (colno, ' ', WRAP_COLUMN, \
12061 : : maxregname, name, \
12062 : : ufmt, value.b##bits); \
12063 : : break
12064 : :
12065 [ - - - + : 288 : TYPES;
+ + + + ]
12066 : :
12067 : 48 : case 128:
12068 [ - + ]: 48 : assert (type == DW_ATE_unsigned);
12069 : 48 : desc = convert (core, ELF_T_XWORD, 2, &value, desc, 0);
12070 : 48 : int be = elf_getident (core, NULL)[EI_DATA] == ELFDATA2MSB;
12071 : 48 : colno = print_core_item (colno, ' ', WRAP_COLUMN,
12072 : : maxregname, name,
12073 : : "0x%.16" PRIx64 "%.16" PRIx64,
12074 : 48 : value.b128[!be], value.b128[be]);
12075 : 48 : break;
12076 : :
12077 : 0 : default:
12078 : 0 : abort ();
12079 : : #undef BITS
12080 : : }
12081 : : break;
12082 : :
12083 : 88 : default:
12084 : : /* Print each byte in hex, the whole thing in native byte order. */
12085 [ - + ]: 88 : assert (bits % 8 == 0);
12086 : 88 : const uint8_t *bytes = desc;
12087 : 88 : desc += bits / 8;
12088 : 88 : char hex[bits / 4 + 1];
12089 : 88 : hex[bits / 4] = '\0';
12090 : 88 : int incr = 1;
12091 [ + + ]: 88 : if (elf_getident (core, NULL)[EI_DATA] == ELFDATA2LSB)
12092 : : {
12093 : 48 : bytes += bits / 8 - 1;
12094 : 48 : incr = -1;
12095 : : }
12096 : 88 : size_t idx = 0;
12097 [ + + ]: 872 : for (char *h = hex; bits > 0; bits -= 8, idx += incr)
12098 : : {
12099 : 784 : *h++ = "0123456789abcdef"[bytes[idx] >> 4];
12100 : 784 : *h++ = "0123456789abcdef"[bytes[idx] & 0xf];
12101 : : }
12102 : 88 : colno = print_core_item (colno, ' ', WRAP_COLUMN,
12103 : : maxregname, name, "0x%s", hex);
12104 : 88 : break;
12105 : : }
12106 : 424 : desc += regloc->pad;
12107 : :
12108 : : #undef TYPES
12109 : : }
12110 : :
12111 : 162 : return colno;
12112 : : }
12113 : :
12114 : :
12115 : : struct register_info
12116 : : {
12117 : : const Ebl_Register_Location *regloc;
12118 : : const char *set;
12119 : : char name[REGNAMESZ];
12120 : : int regno;
12121 : : int bits;
12122 : : int type;
12123 : : };
12124 : :
12125 : : static int
12126 : 2066 : register_bitpos (const struct register_info *r)
12127 : : {
12128 : 2066 : return (r->regloc->offset * 8
12129 : 2066 : + ((r->regno - r->regloc->regno)
12130 : 2066 : * (r->regloc->bits + r->regloc->pad * 8)));
12131 : : }
12132 : :
12133 : : static int
12134 : 1062 : compare_sets_by_info (const struct register_info *r1,
12135 : : const struct register_info *r2)
12136 : : {
12137 : 1062 : return ((int) r2->bits - (int) r1->bits
12138 [ + + ]: 1062 : ?: register_bitpos (r1) - register_bitpos (r2));
12139 : : }
12140 : :
12141 : : /* Sort registers by set, and by size and layout offset within each set. */
12142 : : static int
12143 : 4595 : compare_registers (const void *a, const void *b)
12144 : : {
12145 : 4595 : const struct register_info *r1 = a;
12146 : 4595 : const struct register_info *r2 = b;
12147 : :
12148 : : /* Unused elements sort last. */
12149 [ + + ]: 4595 : if (r1->regloc == NULL)
12150 : 3309 : return r2->regloc == NULL ? 0 : 1;
12151 [ + + ]: 1286 : if (r2->regloc == NULL)
12152 : : return -1;
12153 : :
12154 [ + + ]: 1121 : return ((r1->set == r2->set ? 0 : strcmp (r1->set, r2->set))
12155 [ - + ]: 1121 : ?: compare_sets_by_info (r1, r2));
12156 : : }
12157 : :
12158 : : /* Sort register sets by layout offset of the first register in the set. */
12159 : : static int
12160 : 20 : compare_register_sets (const void *a, const void *b)
12161 : : {
12162 : 20 : const struct register_info *const *p1 = a;
12163 : 20 : const struct register_info *const *p2 = b;
12164 : 20 : return compare_sets_by_info (*p1, *p2);
12165 : : }
12166 : :
12167 : : static inline bool
12168 : 864 : same_set (const struct register_info *a,
12169 : : const struct register_info *b,
12170 : : const struct register_info *regs,
12171 : : size_t maxnreg)
12172 : : {
12173 [ + - ]: 864 : return (a < ®s[maxnreg] && a->regloc != NULL
12174 [ + - + + ]: 864 : && b < ®s[maxnreg] && b->regloc != NULL
12175 [ + + ]: 846 : && a->bits == b->bits
12176 [ + - + + : 1692 : && (a->set == b->set || !strcmp (a->set, b->set)));
+ - ]
12177 : : }
12178 : :
12179 : : static unsigned int
12180 : 37 : handle_core_registers (Ebl *ebl, Elf *core, const void *desc,
12181 : : const Ebl_Register_Location *reglocs, size_t nregloc)
12182 : : {
12183 [ + + ]: 37 : if (nregloc == 0)
12184 : : return 0;
12185 : :
12186 : 18 : ssize_t maxnreg = ebl_register_info (ebl, 0, NULL, 0, NULL, NULL, NULL, NULL);
12187 [ - + ]: 18 : if (maxnreg <= 0)
12188 : : {
12189 [ # # ]: 0 : for (size_t i = 0; i < nregloc; ++i)
12190 : 0 : if (maxnreg < reglocs[i].regno + reglocs[i].count)
12191 : : maxnreg = reglocs[i].regno + reglocs[i].count;
12192 [ # # ]: 0 : assert (maxnreg > 0);
12193 : : }
12194 : :
12195 : 18 : struct register_info regs[maxnreg];
12196 : 18 : memset (regs, 0, sizeof regs);
12197 : :
12198 : : /* Sort to collect the sets together. */
12199 : 18 : int maxreg = 0;
12200 [ + + ]: 180 : for (size_t i = 0; i < nregloc; ++i)
12201 : 162 : for (int reg = reglocs[i].regno;
12202 [ + + ]: 586 : reg < reglocs[i].regno + reglocs[i].count;
12203 : 424 : ++reg)
12204 : : {
12205 [ - + ]: 424 : assert (reg < maxnreg);
12206 : 424 : if (reg > maxreg)
12207 : : maxreg = reg;
12208 : 424 : struct register_info *info = ®s[reg];
12209 : 424 : info->regloc = ®locs[i];
12210 : 424 : info->regno = reg;
12211 : 424 : info->set = register_info (ebl, reg, ®locs[i],
12212 : 424 : info->name, &info->bits, &info->type);
12213 : : }
12214 : 18 : qsort (regs, maxreg + 1, sizeof regs[0], &compare_registers);
12215 : :
12216 : : /* Collect the unique sets and sort them. */
12217 : 18 : struct register_info *sets[maxreg + 1];
12218 : 18 : sets[0] = ®s[0];
12219 : 18 : size_t nsets = 1;
12220 [ + + ]: 1279 : for (int i = 1; i <= maxreg; ++i)
12221 [ + + ]: 1261 : if (regs[i].regloc != NULL
12222 [ + + ]: 406 : && !same_set (®s[i], ®s[i - 1], regs, maxnreg))
12223 : 16 : sets[nsets++] = ®s[i];
12224 : 18 : qsort (sets, nsets, sizeof sets[0], &compare_register_sets);
12225 : :
12226 : : /* Write out all the sets. */
12227 : 18 : unsigned int colno = 0;
12228 [ + + ]: 52 : for (size_t i = 0; i < nsets; ++i)
12229 : : {
12230 : : /* Find the longest name of a register in this set. */
12231 : 34 : size_t maxname = 0;
12232 : 34 : const struct register_info *end;
12233 [ + + ]: 458 : for (end = sets[i]; same_set (sets[i], end, regs, maxnreg); ++end)
12234 : : {
12235 : 424 : size_t len = strlen (end->name);
12236 : 424 : if (len > maxname)
12237 : : maxname = len;
12238 : : }
12239 : :
12240 : 196 : for (const struct register_info *reg = sets[i];
12241 [ + + ]: 196 : reg < end;
12242 : 162 : reg += reg->regloc->count ?: 1)
12243 [ + - ]: 162 : colno = handle_core_register (ebl, core, maxname,
12244 : : reg->regloc, desc, colno);
12245 : :
12246 : : /* Force a line break at the end of the group. */
12247 : 34 : colno = WRAP_COLUMN;
12248 : : }
12249 : :
12250 : 18 : return colno;
12251 : : }
12252 : :
12253 : : static void
12254 : 9 : handle_auxv_note (Ebl *ebl, Elf *core, GElf_Word descsz, GElf_Off desc_pos)
12255 : : {
12256 : 9 : Elf_Data *data = elf_getdata_rawchunk (core, desc_pos, descsz, ELF_T_AUXV);
12257 [ - + ]: 9 : if (data == NULL)
12258 : 0 : elf_error:
12259 : 0 : error (EXIT_FAILURE, 0,
12260 : 0 : _("cannot convert core note data: %s"), elf_errmsg (-1));
12261 : :
12262 : 9 : const size_t nauxv = descsz / gelf_fsize (core, ELF_T_AUXV, 1, EV_CURRENT);
12263 [ + + ]: 177 : for (size_t i = 0; i < nauxv; ++i)
12264 : : {
12265 : 168 : GElf_auxv_t av_mem;
12266 : 168 : GElf_auxv_t *av = gelf_getauxv (data, i, &av_mem);
12267 [ - + ]: 168 : if (av == NULL)
12268 : 0 : goto elf_error;
12269 : :
12270 : 168 : const char *name;
12271 : 168 : const char *fmt;
12272 [ - + ]: 168 : if (ebl_auxv_info (ebl, av->a_type, &name, &fmt) == 0)
12273 : : {
12274 : : /* Unknown type. */
12275 [ # # ]: 0 : if (av->a_un.a_val == 0)
12276 : 0 : printf (" %" PRIu64 "\n", av->a_type);
12277 : : else
12278 : 0 : printf (" %" PRIu64 ": %#" PRIx64 "\n",
12279 : : av->a_type, av->a_un.a_val);
12280 : : }
12281 : : else
12282 [ + + + - : 168 : switch (fmt[0])
+ - ]
12283 : : {
12284 : 9 : case '\0': /* Normally zero. */
12285 [ + - ]: 9 : if (av->a_un.a_val == 0)
12286 : : {
12287 : 9 : printf (" %s\n", name);
12288 : : break;
12289 : : }
12290 : 74 : FALLTHROUGH;
12291 : : case 'x': /* hex */
12292 : : case 'p': /* address */
12293 : : case 's': /* address of string */
12294 : 74 : printf (" %s: %#" PRIx64 "\n", name, av->a_un.a_val);
12295 : : break;
12296 : 81 : case 'u':
12297 : 81 : printf (" %s: %" PRIu64 "\n", name, av->a_un.a_val);
12298 : : break;
12299 : 0 : case 'd':
12300 : 0 : printf (" %s: %" PRId64 "\n", name, av->a_un.a_val);
12301 : : break;
12302 : :
12303 : 4 : case 'b':
12304 : 4 : printf (" %s: %#" PRIx64 " ", name, av->a_un.a_val);
12305 : 4 : GElf_Xword bit = 1;
12306 : 4 : const char *pfx = "<";
12307 [ + + ]: 110 : for (const char *p = fmt + 1; *p != 0; p = strchr (p, '\0') + 1)
12308 : : {
12309 [ + + ]: 106 : if (av->a_un.a_val & bit)
12310 : : {
12311 : 77 : printf ("%s%s", pfx, p);
12312 : 77 : pfx = " ";
12313 : : }
12314 : 106 : bit <<= 1;
12315 : : }
12316 : 168 : printf (">\n");
12317 : : break;
12318 : :
12319 : 0 : default:
12320 : 0 : abort ();
12321 : : }
12322 : : }
12323 : 9 : }
12324 : :
12325 : : static bool
12326 : 195 : buf_has_data (unsigned char const *ptr, unsigned char const *end, size_t sz)
12327 : : {
12328 [ - + - + ]: 195 : return ptr < end && (size_t) (end - ptr) >= sz;
12329 : : }
12330 : :
12331 : : static bool
12332 : 18 : buf_read_int (Elf *core, unsigned char const **ptrp, unsigned char const *end,
12333 : : int *retp)
12334 : : {
12335 [ + - + - ]: 36 : if (! buf_has_data (*ptrp, end, 4))
12336 : : return false;
12337 : :
12338 : 18 : *ptrp = convert (core, ELF_T_WORD, 1, retp, *ptrp, 4);
12339 : 18 : return true;
12340 : : }
12341 : :
12342 : : static bool
12343 : 177 : buf_read_ulong (Elf *core, unsigned char const **ptrp, unsigned char const *end,
12344 : : uint64_t *retp)
12345 : : {
12346 : 177 : size_t sz = gelf_fsize (core, ELF_T_ADDR, 1, EV_CURRENT);
12347 [ + - + - ]: 354 : if (! buf_has_data (*ptrp, end, sz))
12348 : : return false;
12349 : :
12350 : 177 : union
12351 : : {
12352 : : uint64_t u64;
12353 : : uint32_t u32;
12354 : : } u;
12355 : :
12356 : 177 : *ptrp = convert (core, ELF_T_ADDR, 1, &u, *ptrp, sz);
12357 : :
12358 [ + + ]: 177 : if (sz == 4)
12359 : 93 : *retp = u.u32;
12360 : : else
12361 : 84 : *retp = u.u64;
12362 : : return true;
12363 : : }
12364 : :
12365 : : static void
12366 : 6 : handle_siginfo_note (Elf *core, GElf_Word descsz, GElf_Off desc_pos)
12367 : : {
12368 : 6 : Elf_Data *data = elf_getdata_rawchunk (core, desc_pos, descsz, ELF_T_BYTE);
12369 [ - + ]: 6 : if (data == NULL)
12370 : 0 : error (EXIT_FAILURE, 0,
12371 : 0 : _("cannot convert core note data: %s"), elf_errmsg (-1));
12372 : :
12373 : 6 : unsigned char const *ptr = data->d_buf;
12374 : 6 : unsigned char const *const end = data->d_buf + data->d_size;
12375 : :
12376 : : /* Siginfo head is three ints: signal number, error number, origin
12377 : : code. */
12378 : 6 : int si_signo, si_errno, si_code;
12379 [ - + ]: 6 : if (! buf_read_int (core, &ptr, end, &si_signo)
12380 [ - + ]: 6 : || ! buf_read_int (core, &ptr, end, &si_errno)
12381 [ - + ]: 6 : || ! buf_read_int (core, &ptr, end, &si_code))
12382 : : {
12383 : 0 : fail:
12384 : 0 : printf (" Not enough data in NT_SIGINFO note.\n");
12385 : 0 : return;
12386 : : }
12387 : :
12388 : : /* Next is a pointer-aligned union of structures. On 64-bit
12389 : : machines, that implies a word of padding. */
12390 [ + + ]: 6 : if (gelf_getclass (core) == ELFCLASS64)
12391 : 3 : ptr += 4;
12392 : :
12393 : 6 : printf (" si_signo: %d, si_errno: %d, si_code: %d\n",
12394 : : si_signo, si_errno, si_code);
12395 : :
12396 [ + - ]: 6 : if (si_code > 0)
12397 [ + - ]: 6 : switch (si_signo)
12398 : : {
12399 : 6 : case CORE_SIGILL:
12400 : : case CORE_SIGFPE:
12401 : : case CORE_SIGSEGV:
12402 : : case CORE_SIGBUS:
12403 : : {
12404 : 6 : uint64_t addr;
12405 [ - + ]: 6 : if (! buf_read_ulong (core, &ptr, end, &addr))
12406 : 0 : goto fail;
12407 : 6 : printf (" fault address: %#" PRIx64 "\n", addr);
12408 : 6 : break;
12409 : : }
12410 : : default:
12411 : : ;
12412 : : }
12413 [ # # ]: 0 : else if (si_code == CORE_SI_USER)
12414 : : {
12415 : 0 : int pid, uid;
12416 [ # # ]: 0 : if (! buf_read_int (core, &ptr, end, &pid)
12417 [ # # ]: 0 : || ! buf_read_int (core, &ptr, end, &uid))
12418 : 0 : goto fail;
12419 : 0 : printf (" sender PID: %d, sender UID: %d\n", pid, uid);
12420 : : }
12421 : : }
12422 : :
12423 : : static void
12424 : 6 : handle_file_note (Elf *core, GElf_Word descsz, GElf_Off desc_pos)
12425 : : {
12426 : 6 : Elf_Data *data = elf_getdata_rawchunk (core, desc_pos, descsz, ELF_T_BYTE);
12427 [ - + ]: 6 : if (data == NULL)
12428 : 0 : error (EXIT_FAILURE, 0,
12429 : 0 : _("cannot convert core note data: %s"), elf_errmsg (-1));
12430 : :
12431 : 6 : unsigned char const *ptr = data->d_buf;
12432 : 6 : unsigned char const *const end = data->d_buf + data->d_size;
12433 : :
12434 : 6 : uint64_t count, page_size;
12435 [ - + ]: 6 : if (! buf_read_ulong (core, &ptr, end, &count)
12436 [ - + ]: 6 : || ! buf_read_ulong (core, &ptr, end, &page_size))
12437 : : {
12438 : 0 : fail:
12439 : 0 : printf (" Not enough data in NT_FILE note.\n");
12440 : 0 : return;
12441 : : }
12442 : :
12443 : 6 : size_t addrsize = gelf_fsize (core, ELF_T_ADDR, 1, EV_CURRENT);
12444 : 6 : uint64_t maxcount = (size_t) (end - ptr) / (3 * addrsize);
12445 [ - + ]: 6 : if (count > maxcount)
12446 : 0 : goto fail;
12447 : :
12448 : : /* Where file names are stored. */
12449 : 6 : unsigned char const *const fstart = ptr + 3 * count * addrsize;
12450 : 6 : char const *fptr = (char *) fstart;
12451 : :
12452 : 6 : printf (" %" PRId64 " files:\n", count);
12453 [ + + ]: 59 : for (uint64_t i = 0; i < count; ++i)
12454 : : {
12455 : 53 : uint64_t mstart, mend, moffset;
12456 [ + - ]: 53 : if (! buf_read_ulong (core, &ptr, fstart, &mstart)
12457 [ + - ]: 53 : || ! buf_read_ulong (core, &ptr, fstart, &mend)
12458 [ - + ]: 53 : || ! buf_read_ulong (core, &ptr, fstart, &moffset))
12459 : 0 : goto fail;
12460 : :
12461 : 53 : const char *fnext = memchr (fptr, '\0', (char *) end - fptr);
12462 [ - + ]: 53 : if (fnext == NULL)
12463 : 0 : goto fail;
12464 : :
12465 : 53 : int ct = printf (" %08" PRIx64 "-%08" PRIx64
12466 : : " %08" PRIx64 " %" PRId64,
12467 : : mstart, mend, moffset * page_size, mend - mstart);
12468 [ + - ]: 53 : printf ("%*s%s\n", ct > 50 ? 3 : 53 - ct, "", fptr);
12469 : :
12470 : 53 : fptr = fnext + 1;
12471 : : }
12472 : : }
12473 : :
12474 : : static void
12475 : 38 : handle_core_note (Ebl *ebl, const GElf_Nhdr *nhdr,
12476 : : const char *name, const void *desc)
12477 : : {
12478 : 38 : GElf_Word regs_offset;
12479 : 38 : size_t nregloc;
12480 : 38 : const Ebl_Register_Location *reglocs;
12481 : 38 : size_t nitems;
12482 : 38 : const Ebl_Core_Item *items;
12483 : :
12484 [ + + ]: 38 : if (! ebl_core_note (ebl, nhdr, name, desc,
12485 : : ®s_offset, &nregloc, ®locs, &nitems, &items))
12486 : 1 : return;
12487 : :
12488 : : /* Pass 0 for DESCSZ when there are registers in the note,
12489 : : so that the ITEMS array does not describe the whole thing.
12490 : : For non-register notes, the actual descsz might be a multiple
12491 : : of the unit size, not just exactly the unit size. */
12492 : 37 : unsigned int colno = handle_core_items (ebl->elf, desc,
12493 [ + + ]: 37 : nregloc == 0 ? nhdr->n_descsz : 0,
12494 : : items, nitems);
12495 [ + + ]: 37 : if (colno != 0)
12496 [ - + ]: 34 : putchar_unlocked ('\n');
12497 : :
12498 : 37 : colno = handle_core_registers (ebl, ebl->elf, desc + regs_offset,
12499 : : reglocs, nregloc);
12500 [ + + ]: 37 : if (colno != 0)
12501 [ - + ]: 55 : putchar_unlocked ('\n');
12502 : : }
12503 : :
12504 : : static void
12505 : 248 : handle_notes_data (Ebl *ebl, const GElf_Ehdr *ehdr,
12506 : : GElf_Off start, Elf_Data *data)
12507 : : {
12508 : 248 : fputs_unlocked (_(" Owner Data size Type\n"), stdout);
12509 : :
12510 [ - + ]: 248 : if (data == NULL)
12511 : 0 : goto bad_note;
12512 : :
12513 : : size_t offset = 0;
12514 : : GElf_Nhdr nhdr;
12515 : : size_t name_offset;
12516 : : size_t desc_offset;
12517 [ + + ]: 2550 : while (offset < data->d_size
12518 [ + - ]: 2302 : && (offset = gelf_getnote (data, offset,
12519 : : &nhdr, &name_offset, &desc_offset)) > 0)
12520 : : {
12521 [ + - ]: 2302 : const char *name = nhdr.n_namesz == 0 ? "" : data->d_buf + name_offset;
12522 : 2302 : const char *desc = data->d_buf + desc_offset;
12523 : :
12524 : : /* GNU Build Attributes are weird, they store most of their data
12525 : : into the owner name field. Extract just the owner name
12526 : : prefix here, then use the rest later as data. */
12527 : 2302 : bool is_gnu_build_attr
12528 : 2302 : = startswith (name, ELF_NOTE_GNU_BUILD_ATTRIBUTE_PREFIX);
12529 : 4604 : const char *print_name = (is_gnu_build_attr
12530 [ + + ]: 2302 : ? ELF_NOTE_GNU_BUILD_ATTRIBUTE_PREFIX : name);
12531 : 4604 : size_t print_namesz = (is_gnu_build_attr
12532 [ + + ]: 2302 : ? strlen (print_name) : nhdr.n_namesz);
12533 : :
12534 : 2302 : char buf[100];
12535 : 2302 : char buf2[100];
12536 : 4604 : printf (_(" %-13.*s %9" PRId32 " %s\n"),
12537 : : (int) print_namesz, print_name, nhdr.n_descsz,
12538 [ + + ]: 2302 : ehdr->e_type == ET_CORE
12539 : 59 : ? ebl_core_note_type_name (ebl, nhdr.n_type,
12540 : : buf, sizeof (buf))
12541 : 2243 : : ebl_object_note_type_name (ebl, name, nhdr.n_type,
12542 : : nhdr.n_descsz,
12543 : : buf2, sizeof (buf2)));
12544 : :
12545 : : /* Filter out invalid entries. */
12546 : 2302 : if (memchr (name, '\0', nhdr.n_namesz) != NULL
12547 : : /* XXX For now help broken Linux kernels. */
12548 : : || 1)
12549 : : {
12550 [ + + ]: 2302 : if (ehdr->e_type == ET_CORE)
12551 : : {
12552 [ + + ]: 59 : if (nhdr.n_type == NT_AUXV
12553 [ + - ]: 9 : && (nhdr.n_namesz == 4 /* Broken old Linux kernels. */
12554 [ + - + - ]: 9 : || (nhdr.n_namesz == 5 && name[4] == '\0'))
12555 [ + - ]: 9 : && !memcmp (name, "CORE", 4))
12556 : 9 : handle_auxv_note (ebl, ebl->elf, nhdr.n_descsz,
12557 : : start + desc_offset);
12558 [ + + + - ]: 50 : else if (nhdr.n_namesz == 5 && strcmp (name, "CORE") == 0)
12559 [ + + + ]: 38 : switch (nhdr.n_type)
12560 : : {
12561 : 6 : case NT_SIGINFO:
12562 : 6 : handle_siginfo_note (ebl->elf, nhdr.n_descsz,
12563 : : start + desc_offset);
12564 : 6 : break;
12565 : :
12566 : 6 : case NT_FILE:
12567 : 6 : handle_file_note (ebl->elf, nhdr.n_descsz,
12568 : : start + desc_offset);
12569 : 6 : break;
12570 : :
12571 : 26 : default:
12572 : 26 : handle_core_note (ebl, &nhdr, name, desc);
12573 : : }
12574 : : else
12575 : 12 : handle_core_note (ebl, &nhdr, name, desc);
12576 : : }
12577 : : else
12578 : 2243 : ebl_object_note (ebl, nhdr.n_namesz, name, nhdr.n_type,
12579 : : nhdr.n_descsz, desc);
12580 : : }
12581 : : }
12582 : :
12583 [ + - ]: 248 : if (offset == data->d_size)
12584 : 248 : return;
12585 : :
12586 : 0 : bad_note:
12587 : 0 : error (0, 0,
12588 [ # # ]: 0 : _("cannot get content of note: %s"),
12589 : 0 : data != NULL ? "garbage data" : elf_errmsg (-1));
12590 : : }
12591 : :
12592 : : static void
12593 : 143 : handle_notes (Ebl *ebl, GElf_Ehdr *ehdr)
12594 : : {
12595 : : /* If we have section headers, just look for SHT_NOTE sections.
12596 : : In a debuginfo file, the program headers are not reliable. */
12597 [ + + ]: 143 : if (shnum != 0)
12598 : : {
12599 : : /* Get the section header string table index. */
12600 : 132 : size_t shstrndx;
12601 [ - + ]: 132 : if (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0)
12602 : 0 : error (EXIT_FAILURE, 0,
12603 : 0 : _("cannot get section header string table index"));
12604 : :
12605 : : Elf_Scn *scn = NULL;
12606 [ + + ]: 3706 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
12607 : : {
12608 : 3574 : GElf_Shdr shdr_mem;
12609 : 3574 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
12610 : :
12611 [ + - + + ]: 3574 : if (shdr == NULL || shdr->sh_type != SHT_NOTE)
12612 : : /* Not what we are looking for. */
12613 : 3338 : continue;
12614 : :
12615 [ - + ]: 236 : if (notes_section != NULL)
12616 : : {
12617 : 0 : char *sname = elf_strptr (ebl->elf, shstrndx, shdr->sh_name);
12618 [ # # # # ]: 0 : if (sname == NULL || strcmp (sname, notes_section) != 0)
12619 : 0 : continue;
12620 : : }
12621 : :
12622 : 472 : printf (_("\
12623 : : \nNote section [%2zu] '%s' of %" PRIu64 " bytes at offset %#0" PRIx64 ":\n"),
12624 : : elf_ndxscn (scn),
12625 : 236 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
12626 : : shdr->sh_size, shdr->sh_offset);
12627 : :
12628 : 236 : handle_notes_data (ebl, ehdr, shdr->sh_offset,
12629 : : elf_getdata (scn, NULL));
12630 : : }
12631 : 132 : return;
12632 : : }
12633 : :
12634 : : /* We have to look through the program header to find the note
12635 : : sections. There can be more than one. */
12636 [ + + ]: 140 : for (size_t cnt = 0; cnt < phnum; ++cnt)
12637 : : {
12638 : 129 : GElf_Phdr mem;
12639 : 129 : GElf_Phdr *phdr = gelf_getphdr (ebl->elf, cnt, &mem);
12640 : :
12641 [ + - + + ]: 129 : if (phdr == NULL || phdr->p_type != PT_NOTE)
12642 : : /* Not what we are looking for. */
12643 : 117 : continue;
12644 : :
12645 : 12 : printf (_("\
12646 : : \nNote segment of %" PRIu64 " bytes at offset %#0" PRIx64 ":\n"),
12647 : : phdr->p_filesz, phdr->p_offset);
12648 : :
12649 : 12 : handle_notes_data (ebl, ehdr, phdr->p_offset,
12650 : : elf_getdata_rawchunk (ebl->elf,
12651 : : phdr->p_offset, phdr->p_filesz,
12652 [ + + ]: 12 : (phdr->p_align == 8
12653 : : ? ELF_T_NHDR8 : ELF_T_NHDR)));
12654 : : }
12655 : : }
12656 : :
12657 : :
12658 : : static void
12659 : 6 : hex_dump (const uint8_t *data, size_t len)
12660 : : {
12661 : 6 : size_t pos = 0;
12662 [ + + ]: 28 : while (pos < len)
12663 : : {
12664 : 22 : printf (" 0x%08zx ", pos);
12665 : :
12666 : 22 : const size_t chunk = MIN (len - pos, 16);
12667 : :
12668 [ + + ]: 344 : for (size_t i = 0; i < chunk; ++i)
12669 [ + + ]: 322 : if (i % 4 == 3)
12670 : 80 : printf ("%02x ", data[pos + i]);
12671 : : else
12672 : 322 : printf ("%02x", data[pos + i]);
12673 : :
12674 [ + + ]: 22 : if (chunk < 16)
12675 : 2 : printf ("%*s", (int) ((16 - chunk) * 2 + (16 - chunk + 3) / 4), "");
12676 : :
12677 [ + + ]: 344 : for (size_t i = 0; i < chunk; ++i)
12678 : : {
12679 : 322 : unsigned char b = data[pos + i];
12680 [ + + ]: 644 : printf ("%c", isprint (b) ? b : '.');
12681 : : }
12682 : :
12683 : 22 : putchar ('\n');
12684 : 22 : pos += chunk;
12685 : : }
12686 : 6 : }
12687 : :
12688 : : static void
12689 : 6 : dump_data_section (Elf_Scn *scn, const GElf_Shdr *shdr, const char *name)
12690 : : {
12691 [ + - - + ]: 6 : if (shdr->sh_size == 0 || shdr->sh_type == SHT_NOBITS)
12692 : 0 : printf (_("\nSection [%zu] '%s' has no data to dump.\n"),
12693 : : elf_ndxscn (scn), name);
12694 : : else
12695 : : {
12696 [ + + ]: 6 : if (print_decompress)
12697 : : {
12698 : : /* We try to decompress the section, but keep the old shdr around
12699 : : so we can show both the original shdr size and the uncompressed
12700 : : data size. */
12701 [ + + ]: 4 : if ((shdr->sh_flags & SHF_COMPRESSED) != 0)
12702 : : {
12703 [ - + ]: 2 : if (elf_compress (scn, 0, 0) < 0)
12704 : 0 : printf ("WARNING: %s [%zd]\n",
12705 : : _("Couldn't uncompress section"),
12706 : : elf_ndxscn (scn));
12707 : : }
12708 [ + - ]: 2 : else if (startswith (name, ".zdebug"))
12709 : : {
12710 [ - + ]: 2 : if (elf_compress_gnu (scn, 0, 0) < 0)
12711 : 0 : printf ("WARNING: %s [%zd]\n",
12712 : : _("Couldn't uncompress section"),
12713 : : elf_ndxscn (scn));
12714 : : }
12715 : : }
12716 : :
12717 : 6 : Elf_Data *data = elf_rawdata (scn, NULL);
12718 [ - + ]: 6 : if (data == NULL)
12719 : 0 : error (0, 0, _("cannot get data for section [%zu] '%s': %s"),
12720 : : elf_ndxscn (scn), name, elf_errmsg (-1));
12721 : : else
12722 : : {
12723 [ + + ]: 6 : if (data->d_size == shdr->sh_size)
12724 : 2 : printf (_("\nHex dump of section [%zu] '%s', %" PRIu64
12725 : : " bytes at offset %#0" PRIx64 ":\n"),
12726 : : elf_ndxscn (scn), name,
12727 : : shdr->sh_size, shdr->sh_offset);
12728 : : else
12729 : 4 : printf (_("\nHex dump of section [%zu] '%s', %" PRIu64
12730 : : " bytes (%zd uncompressed) at offset %#0"
12731 : : PRIx64 ":\n"),
12732 : : elf_ndxscn (scn), name,
12733 : : shdr->sh_size, data->d_size, shdr->sh_offset);
12734 : 6 : hex_dump (data->d_buf, data->d_size);
12735 : : }
12736 : : }
12737 : 6 : }
12738 : :
12739 : : static void
12740 : 241 : print_string_section (Elf_Scn *scn, const GElf_Shdr *shdr, const char *name)
12741 : : {
12742 [ + - + + ]: 241 : if (shdr->sh_size == 0 || shdr->sh_type == SHT_NOBITS)
12743 : 21 : printf (_("\nSection [%zu] '%s' has no strings to dump.\n"),
12744 : : elf_ndxscn (scn), name);
12745 : : else
12746 : : {
12747 [ + + ]: 220 : if (print_decompress)
12748 : : {
12749 : : /* We try to decompress the section, but keep the old shdr around
12750 : : so we can show both the original shdr size and the uncompressed
12751 : : data size. */
12752 [ - + ]: 1 : if ((shdr->sh_flags & SHF_COMPRESSED) != 0)
12753 : : {
12754 [ # # ]: 0 : if (elf_compress (scn, 0, 0) < 0)
12755 : 0 : printf ("WARNING: %s [%zd]\n",
12756 : : _("Couldn't uncompress section"),
12757 : : elf_ndxscn (scn));
12758 : : }
12759 [ + - ]: 1 : else if (startswith (name, ".zdebug"))
12760 : : {
12761 [ - + ]: 1 : if (elf_compress_gnu (scn, 0, 0) < 0)
12762 : 0 : printf ("WARNING: %s [%zd]\n",
12763 : : _("Couldn't uncompress section"),
12764 : : elf_ndxscn (scn));
12765 : : }
12766 : : }
12767 : :
12768 : 220 : Elf_Data *data = elf_rawdata (scn, NULL);
12769 [ - + ]: 220 : if (data == NULL)
12770 : 0 : error (0, 0, _("cannot get data for section [%zu] '%s': %s"),
12771 : : elf_ndxscn (scn), name, elf_errmsg (-1));
12772 : : else
12773 : : {
12774 [ + + ]: 220 : if (data->d_size == shdr->sh_size)
12775 : 219 : printf (_("\nString section [%zu] '%s' contains %" PRIu64
12776 : : " bytes at offset %#0" PRIx64 ":\n"),
12777 : : elf_ndxscn (scn), name,
12778 : : shdr->sh_size, shdr->sh_offset);
12779 : : else
12780 : 1 : printf (_("\nString section [%zu] '%s' contains %" PRIu64
12781 : : " bytes (%zd uncompressed) at offset %#0"
12782 : : PRIx64 ":\n"),
12783 : : elf_ndxscn (scn), name,
12784 : : shdr->sh_size, data->d_size, shdr->sh_offset);
12785 : :
12786 : 220 : const char *start = data->d_buf;
12787 : 220 : const char *const limit = start + data->d_size;
12788 : 27076 : do
12789 : : {
12790 : 27076 : const char *end = memchr (start, '\0', limit - start);
12791 : 27076 : const size_t pos = start - (const char *) data->d_buf;
12792 [ - + ]: 27076 : if (unlikely (end == NULL))
12793 : : {
12794 : 0 : printf (" [%6zx]- %.*s\n",
12795 : : pos, (int) (limit - start), start);
12796 : : break;
12797 : : }
12798 : 27076 : printf (" [%6zx] %s\n", pos, start);
12799 : 27076 : start = end + 1;
12800 [ + + ]: 27076 : } while (start < limit);
12801 : : }
12802 : : }
12803 : 241 : }
12804 : :
12805 : : static void
12806 : 127 : for_each_section_argument (Elf *elf, const struct section_argument *list,
12807 : : void (*dump) (Elf_Scn *scn, const GElf_Shdr *shdr,
12808 : : const char *name))
12809 : : {
12810 : : /* Get the section header string table index. */
12811 : 127 : size_t shstrndx;
12812 [ - + ]: 127 : if (elf_getshdrstrndx (elf, &shstrndx) < 0)
12813 : 0 : error (EXIT_FAILURE, 0,
12814 : 0 : _("cannot get section header string table index"));
12815 : :
12816 [ + + ]: 494 : for (const struct section_argument *a = list; a != NULL; a = a->next)
12817 : : {
12818 : 367 : Elf_Scn *scn;
12819 : 367 : GElf_Shdr shdr_mem;
12820 : 367 : const char *name = NULL;
12821 : :
12822 : 367 : char *endp = NULL;
12823 : 367 : unsigned long int shndx = strtoul (a->arg, &endp, 0);
12824 [ + + - + ]: 367 : if (endp != a->arg && *endp == '\0')
12825 : : {
12826 : 1 : scn = elf_getscn (elf, shndx);
12827 [ - + ]: 1 : if (scn == NULL)
12828 : : {
12829 : 0 : error (0, 0, _("\nsection [%lu] does not exist"), shndx);
12830 : 0 : continue;
12831 : : }
12832 : :
12833 [ - + ]: 1 : if (gelf_getshdr (scn, &shdr_mem) == NULL)
12834 : 0 : error (EXIT_FAILURE, 0, _("cannot get section header: %s"),
12835 : : elf_errmsg (-1));
12836 : 1 : name = elf_strptr (elf, shstrndx, shdr_mem.sh_name);
12837 : 1 : (*dump) (scn, &shdr_mem, name);
12838 : : }
12839 : : else
12840 : : {
12841 : : /* Need to look up the section by name. */
12842 : : scn = NULL;
12843 : : bool found = false;
12844 [ + + ]: 10539 : while ((scn = elf_nextscn (elf, scn)) != NULL)
12845 : : {
12846 [ - + ]: 10173 : if (gelf_getshdr (scn, &shdr_mem) == NULL)
12847 : 0 : continue;
12848 : 10173 : name = elf_strptr (elf, shstrndx, shdr_mem.sh_name);
12849 [ - + ]: 10173 : if (name == NULL)
12850 : 0 : continue;
12851 [ + + ]: 10173 : if (!strcmp (name, a->arg))
12852 : : {
12853 : 246 : found = true;
12854 : 246 : (*dump) (scn, &shdr_mem, name);
12855 : : }
12856 : : }
12857 : :
12858 [ + + - + ]: 366 : if (unlikely (!found) && !a->implicit)
12859 : 367 : error (0, 0, _("\nsection '%s' does not exist"), a->arg);
12860 : : }
12861 : : }
12862 : 127 : }
12863 : :
12864 : : static void
12865 : 6 : dump_data (Ebl *ebl)
12866 : : {
12867 : 6 : for_each_section_argument (ebl->elf, dump_data_sections, &dump_data_section);
12868 : 6 : }
12869 : :
12870 : : static void
12871 : 121 : dump_strings (Ebl *ebl)
12872 : : {
12873 : 121 : for_each_section_argument (ebl->elf, string_sections, &print_string_section);
12874 : 121 : }
12875 : :
12876 : : static void
12877 : 0 : print_strings (Ebl *ebl)
12878 : : {
12879 : : /* Get the section header string table index. */
12880 : 0 : size_t shstrndx;
12881 [ # # ]: 0 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
12882 : 0 : error (EXIT_FAILURE, 0,
12883 : 0 : _("cannot get section header string table index"));
12884 : :
12885 : : Elf_Scn *scn;
12886 : : GElf_Shdr shdr_mem;
12887 : : const char *name;
12888 : : scn = NULL;
12889 [ # # ]: 0 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
12890 : : {
12891 [ # # ]: 0 : if (gelf_getshdr (scn, &shdr_mem) == NULL)
12892 : 0 : continue;
12893 : :
12894 [ # # ]: 0 : if (shdr_mem.sh_type != SHT_PROGBITS
12895 [ # # ]: 0 : || !(shdr_mem.sh_flags & SHF_STRINGS))
12896 : 0 : continue;
12897 : :
12898 : 0 : name = elf_strptr (ebl->elf, shstrndx, shdr_mem.sh_name);
12899 [ # # ]: 0 : if (name == NULL)
12900 : 0 : continue;
12901 : :
12902 : 0 : print_string_section (scn, &shdr_mem, name);
12903 : : }
12904 : 0 : }
12905 : :
12906 : : static void
12907 : 2 : dump_archive_index (Elf *elf, const char *fname)
12908 : : {
12909 : 2 : size_t narsym;
12910 : 2 : const Elf_Arsym *arsym = elf_getarsym (elf, &narsym);
12911 [ - + ]: 2 : if (arsym == NULL)
12912 : : {
12913 : 0 : int result = elf_errno ();
12914 [ # # ]: 0 : if (unlikely (result != ELF_E_NO_INDEX))
12915 : 0 : error (EXIT_FAILURE, 0,
12916 : 0 : _("cannot get symbol index of archive '%s': %s"),
12917 : : fname, elf_errmsg (result));
12918 : : else
12919 : 0 : printf (_("\nArchive '%s' has no symbol index\n"), fname);
12920 : 0 : return;
12921 : : }
12922 : :
12923 : 2 : printf (_("\nIndex of archive '%s' has %zu entries:\n"),
12924 : : fname, narsym);
12925 : :
12926 : 2 : size_t as_off = 0;
12927 [ + + ]: 11 : for (const Elf_Arsym *s = arsym; s < &arsym[narsym - 1]; ++s)
12928 : : {
12929 [ + + ]: 9 : if (s->as_off != as_off)
12930 : : {
12931 : 6 : as_off = s->as_off;
12932 : :
12933 : 6 : Elf *subelf = NULL;
12934 [ + - ]: 6 : if (unlikely (elf_rand (elf, as_off) == 0)
12935 [ - + ]: 6 : || unlikely ((subelf = elf_begin (-1, ELF_C_READ_MMAP, elf))
12936 : : == NULL))
12937 : : #if __GLIBC__ < 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ < 7)
12938 : : while (1)
12939 : : #endif
12940 : 0 : error (EXIT_FAILURE, 0,
12941 : 0 : _("cannot extract member at offset %zu in '%s': %s"),
12942 : : as_off, fname, elf_errmsg (-1));
12943 : :
12944 : 6 : const Elf_Arhdr *h = elf_getarhdr (subelf);
12945 : :
12946 : 6 : printf (_("Archive member '%s' contains:\n"), h->ar_name);
12947 : :
12948 : 6 : elf_end (subelf);
12949 : : }
12950 : :
12951 : 9 : printf ("\t%s\n", s->as_name);
12952 : : }
12953 : : }
12954 : :
12955 : : #include "debugpred.h"
|