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 : : Copyright (C) 2023, 2025 Mark J. Wielaard <mark@klomp.org>
4 : : This file is part of elfutils.
5 : :
6 : : This file is free software; you can redistribute it and/or modify
7 : : it under the terms of the GNU General Public License as published by
8 : : the Free Software Foundation; either version 3 of the License, or
9 : : (at your option) any later version.
10 : :
11 : : elfutils is distributed in the hope that it will be useful, but
12 : : WITHOUT ANY WARRANTY; without even the implied warranty of
13 : : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 : : GNU General Public License for more details.
15 : :
16 : : You should have received a copy of the GNU General Public License
17 : : along with this program. If not, see <http://www.gnu.org/licenses/>. */
18 : :
19 : : #ifdef HAVE_CONFIG_H
20 : : # include <config.h>
21 : : #endif
22 : :
23 : : #include <argp.h>
24 : : #include <assert.h>
25 : : #include <ctype.h>
26 : : #include <dwarf.h>
27 : : #include <errno.h>
28 : : #include <fcntl.h>
29 : : #include <gelf.h>
30 : : #include <inttypes.h>
31 : : #include <langinfo.h>
32 : : #include <libdw.h>
33 : : #include <libdwfl.h>
34 : : #include <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 : : { "syms", 's', NULL, OPTION_ALIAS | OPTION_HIDDEN, NULL, 0 },
120 : : { "dyn-syms", PRINT_DYNSYM_TABLE, NULL, 0,
121 : : N_("Display (only) the dynamic symbol table"), 0 },
122 : : { "version-info", 'V', NULL, 0, N_("Display versioning information"), 0 },
123 : : { "notes", 'n', "SECTION", OPTION_ARG_OPTIONAL, N_("Display the ELF notes"), 0 },
124 : : { "arch-specific", 'A', NULL, 0,
125 : : N_("Display architecture specific information, if any"), 0 },
126 : : { "exception", 'e', NULL, 0,
127 : : N_("Display sections for exception handling"), 0 },
128 : :
129 : : { NULL, 0, NULL, 0, N_("Additional output selection:"), 0 },
130 : : { "debug-dump", 'w', "SECTION", OPTION_ARG_OPTIONAL,
131 : : N_("Display DWARF section content. SECTION can be one of abbrev, addr, "
132 : : "aranges, decodedaranges, frame, gdb_index, info, info+, loc, line, "
133 : : "decodedline, ranges, pubnames, str, macinfo, macro or exception"), 0 },
134 : : { "hex-dump", 'x', "SECTION", 0,
135 : : N_("Dump the uninterpreted contents of SECTION, by number or name"), 0 },
136 : : { "strings", 'p', "SECTION", OPTION_ARG_OPTIONAL,
137 : : N_("Print string contents of sections"), 0 },
138 : : { "string-dump", 'p', NULL, OPTION_ALIAS | OPTION_HIDDEN, NULL, 0 },
139 : : { "archive-index", 'c', NULL, 0,
140 : : N_("Display the symbol index of an archive"), 0 },
141 : : { "use-dynamic", 'D', NULL, 0,
142 : : N_("Use the dynamic segment when possible for displaying info"), 0 },
143 : :
144 : : { NULL, 0, NULL, 0, N_("Output control:"), 0 },
145 : : { "numeric-addresses", 'N', NULL, 0,
146 : : N_("Do not find symbol names for addresses in DWARF data"), 0 },
147 : : { "unresolved-address-offsets", 'U', NULL, 0,
148 : : N_("Display just offsets instead of resolving values to addresses in DWARF data"), 0 },
149 : : { "wide", 'W', NULL, 0,
150 : : N_("Ignored for compatibility (lines always wide)"), 0 },
151 : : { "decompress", 'z', NULL, 0,
152 : : N_("Show compression information for compressed sections (when used with -S); decompress section before dumping data (when used with -p or -x)"), 0 },
153 : : { NULL, 0, NULL, 0, NULL, 0 }
154 : : };
155 : :
156 : : /* Short description of program. */
157 : : static const char doc[] = N_("\
158 : : Print information from ELF file in human-readable form.");
159 : :
160 : : /* Strings for arguments in help texts. */
161 : : static const char args_doc[] = N_("FILE...");
162 : :
163 : : /* Prototype for option handler. */
164 : : static error_t parse_opt (int key, char *arg, struct argp_state *state);
165 : :
166 : : /* Data structure to communicate with argp functions. */
167 : : static struct argp argp =
168 : : {
169 : : options, parse_opt, args_doc, doc, NULL, NULL, NULL
170 : : };
171 : :
172 : : /* If non-null, the section from which we should read to (compressed) ELF. */
173 : : static const char *elf_input_section = NULL;
174 : :
175 : : /* If non-null, the file that contains the skeleton CUs. */
176 : : static const char *dwarf_skeleton = NULL;
177 : :
178 : : /* Flags set by the option controlling the output. */
179 : :
180 : : /* True if dynamic segment should be printed. */
181 : : static bool print_dynamic_table;
182 : :
183 : : /* True if the file header should be printed. */
184 : : static bool print_file_header;
185 : :
186 : : /* True if the program headers should be printed. */
187 : : static bool print_program_header;
188 : :
189 : : /* True if relocations should be printed. */
190 : : static bool print_relocations;
191 : :
192 : : /* True if the section headers should be printed. */
193 : : static bool print_section_header;
194 : :
195 : : /* True if the symbol table should be printed. */
196 : : static bool print_symbol_table;
197 : :
198 : : /* True if (only) the dynsym table should be printed. */
199 : : static bool print_dynsym_table;
200 : :
201 : : /* True if reconstruct dynamic symbol table from the PT_DYNAMIC segment. */
202 : : static bool use_dynamic_segment;
203 : :
204 : : /* A specific section name, or NULL to print all symbol tables. */
205 : : static char *symbol_table_section;
206 : :
207 : : /* A specific section name, or NULL to print all ELF notes. */
208 : : static char *notes_section;
209 : :
210 : : /* True if the version information should be printed. */
211 : : static bool print_version_info;
212 : :
213 : : /* True if section groups should be printed. */
214 : : static bool print_section_groups;
215 : :
216 : : /* True if bucket list length histogram should be printed. */
217 : : static bool print_histogram;
218 : :
219 : : /* True if the architecture specific data should be printed. */
220 : : static bool print_arch;
221 : :
222 : : /* True if note section content should be printed. */
223 : : static bool print_notes;
224 : :
225 : : /* True if SHF_STRINGS section content should be printed. */
226 : : static bool print_string_sections;
227 : :
228 : : /* True if archive index should be printed. */
229 : : static bool print_archive_index;
230 : :
231 : : /* True if any of the control options except print_archive_index is set. */
232 : : static bool any_control_option;
233 : :
234 : : /* True if we should print addresses from DWARF in symbolic form. */
235 : : static bool print_address_names = true;
236 : :
237 : : /* True if we should print raw values instead of relativized addresses. */
238 : : static bool print_unresolved_addresses = false;
239 : :
240 : : /* True if we should print the .debug_aranges section using libdw. */
241 : : static bool decodedaranges = false;
242 : :
243 : : /* True if we should print the .debug_aranges section using libdw. */
244 : : static bool decodedline = false;
245 : :
246 : : /* True if we want to show more information about compressed sections. */
247 : : static bool print_decompress = false;
248 : :
249 : : /* True if we want to show split compile units for debug_info skeletons. */
250 : : static bool show_split_units = false;
251 : :
252 : : /* Select printing of debugging sections. */
253 : : static enum section_e
254 : : {
255 : : section_abbrev = 1, /* .debug_abbrev */
256 : : section_aranges = 2, /* .debug_aranges */
257 : : section_frame = 4, /* .debug_frame or .eh_frame & al. */
258 : : section_info = 8, /* .debug_info, (implies .debug_types) */
259 : : section_line = 16, /* .debug_line */
260 : : section_loc = 32, /* .debug_loc */
261 : : section_pubnames = 64, /* .debug_pubnames */
262 : : section_str = 128, /* .debug_str */
263 : : section_macinfo = 256, /* .debug_macinfo */
264 : : section_ranges = 512, /* .debug_ranges */
265 : : section_exception = 1024, /* .eh_frame & al. */
266 : : section_gdb_index = 2048, /* .gdb_index */
267 : : section_macro = 4096, /* .debug_macro */
268 : : section_addr = 8192, /* .debug_addr */
269 : : section_types = 16384, /* .debug_types (implied by .debug_info) */
270 : : section_all = (section_abbrev | section_aranges | section_frame
271 : : | section_info | section_line | section_loc
272 : : | section_pubnames | section_str | section_macinfo
273 : : | section_ranges | section_exception | section_gdb_index
274 : : | section_macro | section_addr | section_types)
275 : : } print_debug_sections, implicit_debug_sections;
276 : :
277 : : /* Select hex dumping of sections. */
278 : : static struct section_argument *dump_data_sections;
279 : : static struct section_argument **dump_data_sections_tail = &dump_data_sections;
280 : :
281 : : /* Select string dumping of sections. */
282 : : static struct section_argument *string_sections;
283 : : static struct section_argument **string_sections_tail = &string_sections;
284 : :
285 : : struct section_argument
286 : : {
287 : : struct section_argument *next;
288 : : const char *arg;
289 : : bool implicit;
290 : : };
291 : :
292 : : /* Numbers of sections and program headers in the file. */
293 : : static size_t shnum;
294 : : static size_t phnum;
295 : :
296 : :
297 : : /* Declarations of local functions. */
298 : : static void process_file (int fd, const char *fname, bool only_one);
299 : : static void process_elf_file (Dwfl_Module *dwflmod, int fd);
300 : : static void print_ehdr (Ebl *ebl, GElf_Ehdr *ehdr);
301 : : static void print_shdr (Ebl *ebl, GElf_Ehdr *ehdr);
302 : : static void print_phdr (Ebl *ebl, GElf_Ehdr *ehdr);
303 : : static void print_scngrp (Ebl *ebl);
304 : : static void print_dynamic (Ebl *ebl);
305 : : static void print_relocs (Ebl *ebl, Dwfl_Module *mod, GElf_Ehdr *ehdr);
306 : : static void handle_relocs_rel (Ebl *ebl, GElf_Ehdr *ehdr, Elf_Scn *scn,
307 : : GElf_Shdr *shdr);
308 : : static void handle_relocs_rela (Ebl *ebl, GElf_Ehdr *ehdr, Elf_Scn *scn,
309 : : GElf_Shdr *shdr);
310 : : static void handle_relocs_relr (Ebl *ebl, Dwfl_Module *mod, Elf_Scn *scn,
311 : : GElf_Shdr *shdr);
312 : : static bool print_symtab (Ebl *ebl, int type);
313 : : static bool handle_symtab (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr);
314 : : static bool handle_dynamic_symtab (Ebl *ebl);
315 : : static void
316 : : process_symtab(
317 : : Ebl * ebl,
318 : : unsigned int nsyms,
319 : : Elf64_Word idx,
320 : : Elf32_Word verneed_stridx,
321 : : Elf32_Word verdef_stridx,
322 : : Elf_Data * symdata,
323 : : Elf_Data * versym_data,
324 : : Elf_Data * symstr_data,
325 : : Elf_Data * verneed_data,
326 : : Elf_Data * verdef_data,
327 : : Elf_Data * xndx_data);
328 : : static void print_verinfo (Ebl *ebl);
329 : : static void handle_verneed (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr);
330 : : static void handle_verdef (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr);
331 : : static void handle_versym (Ebl *ebl, Elf_Scn *scn,
332 : : GElf_Shdr *shdr);
333 : : static void print_debug (Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr);
334 : : static void handle_hash (Ebl *ebl);
335 : : static void handle_notes (Ebl *ebl, GElf_Ehdr *ehdr);
336 : : static void print_liblist (Ebl *ebl);
337 : : static void print_attributes (Ebl *ebl, const GElf_Ehdr *ehdr);
338 : : static void dump_data (Ebl *ebl);
339 : : static void dump_strings (Ebl *ebl);
340 : : static void print_strings (Ebl *ebl);
341 : : static void dump_archive_index (Elf *, const char *);
342 : : static void print_dwarf_addr (Dwfl_Module *dwflmod, int address_size,
343 : : Dwarf_Addr address, Dwarf_Addr raw);
344 : :
345 : : enum dyn_idx
346 : : {
347 : : i_symtab_shndx,
348 : : i_strsz,
349 : : i_verneed,
350 : : i_verneednum,
351 : : i_verdef,
352 : : i_verdefnum,
353 : : i_versym,
354 : : i_symtab,
355 : : i_strtab,
356 : : i_hash,
357 : : i_gnu_hash,
358 : : i_max
359 : : };
360 : :
361 : : /* Declarations of local functions for use-dynamic. */
362 : : static Elf_Data *get_dynscn_strtab (Elf *elf, GElf_Phdr *phdr);
363 : : static void get_dynscn_addrs (Elf *elf, GElf_Phdr *phdr, GElf_Addr addrs[i_max]);
364 : : static void find_offsets (Elf *elf, GElf_Addr main_bias, size_t n,
365 : : GElf_Addr addrs[n], GElf_Off offs[n]);
366 : :
367 : : /* Looked up once with gettext in main. */
368 : : static char *yes_str;
369 : : static char *no_str;
370 : :
371 : : static void
372 : 1776 : cleanup_list (struct section_argument *list)
373 : : {
374 [ + + + + ]: 2342 : while (list != NULL)
375 : : {
376 : 566 : struct section_argument *a = list;
377 : 566 : list = a->next;
378 : 566 : free (a);
379 : : }
380 : : }
381 : :
382 : : int
383 : 888 : main (int argc, char *argv[])
384 : : {
385 : : /* We use no threads here which can interfere with handling a stream. */
386 : 888 : (void) __fsetlocking (stdout, FSETLOCKING_BYCALLER);
387 : :
388 : : /* Set locale. */
389 : 888 : setlocale (LC_ALL, "");
390 : :
391 : : /* Initialize the message catalog. */
392 : 888 : textdomain (PACKAGE_TARNAME);
393 : :
394 : : /* Look up once. */
395 : 888 : yes_str = _("yes");
396 : 888 : no_str = _("no");
397 : :
398 : : /* Parse and process arguments. */
399 : 888 : int remaining;
400 : 888 : argp_parse (&argp, argc, argv, 0, &remaining, NULL);
401 : :
402 : : /* Before we start tell the ELF library which version we are using. */
403 : 888 : elf_version (EV_CURRENT);
404 : :
405 : : /* Now process all the files given at the command line. */
406 : 888 : bool only_one = remaining + 1 == argc;
407 : 970 : do
408 : : {
409 : : /* Open the file. */
410 : 970 : int fd = open (argv[remaining], O_RDONLY);
411 [ - + ]: 970 : if (fd == -1)
412 : : {
413 : 0 : error (0, errno, _("cannot open input file '%s'"), argv[remaining]);
414 : 0 : continue;
415 : : }
416 : :
417 : 970 : process_file (fd, argv[remaining], only_one);
418 : :
419 : 970 : close (fd);
420 : : }
421 [ + + ]: 970 : while (++remaining < argc);
422 : :
423 : 888 : cleanup_list (dump_data_sections);
424 : 888 : cleanup_list (string_sections);
425 : :
426 : 888 : return error_message_count != 0;
427 : : }
428 : :
429 : : static void
430 : 566 : add_dump_section (const char *name,
431 : : int key,
432 : : bool implicit)
433 : : {
434 : 566 : struct section_argument *a = xmalloc (sizeof *a);
435 : 566 : a->arg = name;
436 : 566 : a->next = NULL;
437 : 566 : a->implicit = implicit;
438 : 1132 : struct section_argument ***tailp
439 [ + + ]: 566 : = key == 'x' ? &dump_data_sections_tail : &string_sections_tail;
440 : 566 : **tailp = a;
441 : 566 : *tailp = &a->next;
442 : 566 : }
443 : :
444 : : /* Handle program arguments. */
445 : : static error_t
446 : 5650 : parse_opt (int key, char *arg,
447 : : struct argp_state *state __attribute__ ((unused)))
448 : : {
449 [ + + - + : 5650 : switch (key)
+ - + + -
+ + + + +
+ - + + +
+ + + - +
+ + + + ]
450 : : {
451 : 184 : case 'a':
452 : 184 : print_file_header = true;
453 : 184 : print_program_header = true;
454 : 184 : print_relocations = true;
455 : 184 : print_section_header = true;
456 : 184 : print_symbol_table = true;
457 : 184 : print_version_info = true;
458 : 184 : print_dynamic_table = true;
459 : 184 : print_section_groups = true;
460 : 184 : print_histogram = true;
461 : 184 : print_arch = true;
462 : 184 : print_notes = true;
463 : 184 : implicit_debug_sections |= section_exception;
464 : 184 : add_dump_section (".strtab", key, true);
465 : 184 : add_dump_section (".dynstr", key, true);
466 : 184 : add_dump_section (".comment", key, true);
467 : 184 : any_control_option = true;
468 : 184 : break;
469 : 8 : case 'A':
470 : 8 : print_arch = true;
471 : 8 : any_control_option = true;
472 : 8 : break;
473 : 6 : case 'd':
474 : 6 : print_dynamic_table = true;
475 : 6 : any_control_option = true;
476 : 6 : break;
477 : 2 : case 'D':
478 : 2 : use_dynamic_segment = true;
479 : 2 : break;
480 : 0 : case 'e':
481 : 0 : print_debug_sections |= section_exception;
482 : 0 : any_control_option = true;
483 : 0 : break;
484 : 18 : case 'g':
485 : 18 : print_section_groups = true;
486 : 18 : any_control_option = true;
487 : 18 : break;
488 : 6 : case 'h':
489 : 6 : print_file_header = true;
490 : 6 : any_control_option = true;
491 : 6 : break;
492 : 0 : case 'I':
493 : 0 : print_histogram = true;
494 : 0 : any_control_option = true;
495 : 0 : break;
496 : 4 : case 'l':
497 : 4 : print_program_header = true;
498 : 4 : any_control_option = true;
499 : 4 : break;
500 : 46 : case 'n':
501 : 46 : print_notes = true;
502 : 46 : any_control_option = true;
503 : 46 : notes_section = arg;
504 : 46 : break;
505 : 10 : case 'r':
506 : 10 : print_relocations = true;
507 : 10 : any_control_option = true;
508 : 10 : break;
509 : 322 : case 'S':
510 : 322 : print_section_header = true;
511 : 322 : any_control_option = true;
512 : 322 : break;
513 : 34 : case 's':
514 : 34 : print_symbol_table = true;
515 : 34 : any_control_option = true;
516 : 34 : symbol_table_section = arg;
517 : 34 : break;
518 : 2 : case PRINT_DYNSYM_TABLE:
519 : 2 : print_dynsym_table = true;
520 : 2 : any_control_option = true;
521 : 2 : break;
522 : 0 : case 'V':
523 : 0 : print_version_info = true;
524 : 0 : any_control_option = true;
525 : 0 : break;
526 : 4 : case 'c':
527 : 4 : print_archive_index = true;
528 : 4 : break;
529 : 262 : case 'w':
530 [ + + ]: 262 : if (arg == NULL)
531 : : {
532 : 86 : print_debug_sections = section_all;
533 : 86 : implicit_debug_sections = section_info;
534 : 86 : show_split_units = true;
535 : : }
536 [ - + ]: 176 : else if (strcmp (arg, "abbrev") == 0)
537 : 0 : print_debug_sections |= section_abbrev;
538 [ + + ]: 176 : else if (strcmp (arg, "addr") == 0)
539 : : {
540 : 4 : print_debug_sections |= section_addr;
541 : 4 : implicit_debug_sections |= section_info;
542 : : }
543 [ + + ]: 172 : else if (strcmp (arg, "aranges") == 0)
544 : 6 : print_debug_sections |= section_aranges;
545 [ + + ]: 166 : else if (strcmp (arg, "decodedaranges") == 0)
546 : : {
547 : 2 : print_debug_sections |= section_aranges;
548 : 2 : decodedaranges = true;
549 : : }
550 [ + + ]: 164 : else if (strcmp (arg, "ranges") == 0)
551 : : {
552 : 26 : print_debug_sections |= section_ranges;
553 : 26 : implicit_debug_sections |= section_info;
554 : : }
555 [ + + + + ]: 138 : else if (strcmp (arg, "frame") == 0 || strcmp (arg, "frames") == 0)
556 : 6 : print_debug_sections |= section_frame;
557 [ + + ]: 132 : else if (strcmp (arg, "info") == 0)
558 : : {
559 : 38 : print_debug_sections |= section_info;
560 : 38 : print_debug_sections |= section_types;
561 : : }
562 [ + + ]: 94 : else if (strcmp (arg, "info+") == 0)
563 : : {
564 : 4 : print_debug_sections |= section_info;
565 : 4 : print_debug_sections |= section_types;
566 : 4 : show_split_units = true;
567 : : }
568 [ + + ]: 90 : else if (strcmp (arg, "loc") == 0)
569 : : {
570 : 38 : print_debug_sections |= section_loc;
571 : 38 : implicit_debug_sections |= section_info;
572 : : }
573 [ + + ]: 52 : else if (strcmp (arg, "line") == 0)
574 : 16 : print_debug_sections |= section_line;
575 [ + + ]: 36 : else if (strcmp (arg, "decodedline") == 0)
576 : : {
577 : 14 : print_debug_sections |= section_line;
578 : 14 : decodedline = true;
579 : : }
580 [ - + ]: 22 : else if (strcmp (arg, "pubnames") == 0)
581 : 0 : print_debug_sections |= section_pubnames;
582 [ + + ]: 22 : else if (strcmp (arg, "str") == 0)
583 : : {
584 : 6 : print_debug_sections |= section_str;
585 : : /* For mapping string offset tables to CUs. */
586 : 6 : implicit_debug_sections |= section_info;
587 : : }
588 [ - + ]: 16 : else if (strcmp (arg, "macinfo") == 0)
589 : 0 : print_debug_sections |= section_macinfo;
590 [ + + ]: 16 : else if (strcmp (arg, "macro") == 0)
591 : 8 : print_debug_sections |= section_macro;
592 [ - + ]: 8 : else if (strcmp (arg, "exception") == 0)
593 : 0 : print_debug_sections |= section_exception;
594 [ + - ]: 8 : else if (strcmp (arg, "gdb_index") == 0)
595 : 8 : print_debug_sections |= section_gdb_index;
596 : : else
597 : : {
598 : 0 : fprintf (stderr, _("Unknown DWARF debug section `%s'.\n"),
599 : : arg);
600 : 0 : argp_help (&argp, stderr, ARGP_HELP_SEE,
601 : : program_invocation_short_name);
602 : 0 : exit (1);
603 : : }
604 : 262 : any_control_option = true;
605 : 262 : break;
606 : 2 : case 'p':
607 : 2 : any_control_option = true;
608 [ - + ]: 2 : if (arg == NULL)
609 : : {
610 : 0 : print_string_sections = true;
611 : 0 : break;
612 : : }
613 : 14 : FALLTHROUGH;
614 : : case 'x':
615 : 14 : add_dump_section (arg, key, false);
616 : 14 : any_control_option = true;
617 : 14 : break;
618 : 90 : case 'N':
619 : 90 : print_address_names = false;
620 : 90 : break;
621 : 56 : case 'U':
622 : 56 : print_unresolved_addresses = true;
623 : 56 : break;
624 : 0 : case ARGP_KEY_NO_ARGS:
625 : 0 : fputs (_("Missing file name.\n"), stderr);
626 : 0 : goto do_argp_help;
627 : 888 : case ARGP_KEY_FINI:
628 [ + + + - ]: 888 : if (! any_control_option && ! print_archive_index)
629 : : {
630 : 0 : fputs (_("No operation specified.\n"), stderr);
631 : 0 : do_argp_help:
632 : 0 : argp_help (&argp, stderr, ARGP_HELP_SEE,
633 : : program_invocation_short_name);
634 : 0 : exit (EXIT_FAILURE);
635 : : }
636 : : break;
637 : : case 'W': /* Ignored. */
638 : : break;
639 : 122 : case 'z':
640 : 122 : print_decompress = true;
641 : 122 : break;
642 : 10 : case ELF_INPUT_SECTION:
643 [ + - ]: 10 : if (arg == NULL)
644 : 10 : elf_input_section = ".gnu_debugdata";
645 : : else
646 : 0 : elf_input_section = arg;
647 : : break;
648 : 10 : case DWARF_SKELETON:
649 : 10 : dwarf_skeleton = arg;
650 : 10 : break;
651 : : default:
652 : : return ARGP_ERR_UNKNOWN;
653 : : }
654 : : return 0;
655 : : }
656 : :
657 : :
658 : : /* Create a file descriptor to read the data from the
659 : : elf_input_section given a file descriptor to an ELF file. */
660 : : static int
661 : 10 : open_input_section (int fd)
662 : : {
663 : 10 : size_t shnums;
664 : 10 : size_t cnt;
665 : 10 : size_t shstrndx;
666 : 10 : Elf *elf = elf_begin (fd, ELF_C_READ_MMAP, NULL);
667 [ - + ]: 10 : if (elf == NULL)
668 : : {
669 : 0 : error (0, 0, _("cannot generate Elf descriptor: %s"),
670 : : elf_errmsg (-1));
671 : 0 : return -1;
672 : : }
673 : :
674 [ - + ]: 10 : if (elf_getshdrnum (elf, &shnums) < 0)
675 : : {
676 : 0 : error (0, 0, _("cannot determine number of sections: %s"),
677 : : elf_errmsg (-1));
678 : 0 : open_error:
679 : 0 : elf_end (elf);
680 : 0 : return -1;
681 : : }
682 : :
683 [ - + ]: 10 : if (elf_getshdrstrndx (elf, &shstrndx) < 0)
684 : : {
685 : 0 : error (0, 0, _("cannot get section header string table index"));
686 : 0 : goto open_error;
687 : : }
688 : :
689 [ + - ]: 236 : for (cnt = 0; cnt < shnums; ++cnt)
690 : : {
691 : 236 : Elf_Scn *scn = elf_getscn (elf, cnt);
692 [ - + ]: 236 : if (scn == NULL)
693 : : {
694 : 0 : error (0, 0, _("cannot get section: %s"),
695 : : elf_errmsg (-1));
696 : 0 : goto open_error;
697 : : }
698 : :
699 : 236 : GElf_Shdr shdr_mem;
700 : 236 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
701 [ - + ]: 236 : if (unlikely (shdr == NULL))
702 : : {
703 : 0 : error (0, 0, _("cannot get section header: %s"),
704 : : elf_errmsg (-1));
705 : 0 : goto open_error;
706 : : }
707 : :
708 : 236 : const char *sname = elf_strptr (elf, shstrndx, shdr->sh_name);
709 [ - + ]: 236 : if (sname == NULL)
710 : : {
711 : 0 : error (0, 0, _("cannot get section name"));
712 : 0 : goto open_error;
713 : : }
714 : :
715 [ + + ]: 236 : if (strcmp (sname, elf_input_section) == 0)
716 : : {
717 : 10 : Elf_Data *data = elf_rawdata (scn, NULL);
718 [ - + ]: 10 : if (data == NULL)
719 : : {
720 : 0 : error (0, 0, _("cannot get %s content: %s"),
721 : : sname, elf_errmsg (-1));
722 : 0 : goto open_error;
723 : : }
724 : :
725 : : /* Create (and immediately unlink) a temporary file to store
726 : : section data in to create a file descriptor for it. */
727 [ + - ]: 10 : const char *tmpdir = getenv ("TMPDIR") ?: P_tmpdir;
728 : 10 : static const char suffix[] = "/readelfXXXXXX";
729 : 10 : int tmplen = strlen (tmpdir) + sizeof (suffix);
730 : 10 : char *tempname = alloca (tmplen);
731 : 10 : sprintf (tempname, "%s%s", tmpdir, suffix);
732 : :
733 : 10 : int sfd = mkstemp (tempname);
734 [ - + ]: 10 : if (sfd == -1)
735 : : {
736 : 0 : error (0, 0, _("cannot create temp file '%s'"),
737 : : tempname);
738 : 0 : goto open_error;
739 : : }
740 : 10 : unlink (tempname);
741 : :
742 : 10 : ssize_t size = data->d_size;
743 [ - + ]: 10 : if (write_retry (sfd, data->d_buf, size) != size)
744 : : {
745 : 0 : error (0, 0, _("cannot write section data"));
746 : 0 : goto open_error;
747 : : }
748 : :
749 [ - + ]: 10 : if (elf_end (elf) != 0)
750 : : {
751 : 0 : error (0, 0, _("error while closing Elf descriptor: %s"),
752 : : elf_errmsg (-1));
753 : 10 : return -1;
754 : : }
755 : :
756 [ - + ]: 10 : if (lseek (sfd, 0, SEEK_SET) == -1)
757 : : {
758 : 0 : error (0, 0, _("error while rewinding file descriptor"));
759 : 0 : return -1;
760 : : }
761 : :
762 : : return sfd;
763 : : }
764 : : }
765 : :
766 : : /* Named section not found. */
767 [ # # ]: 0 : if (elf_end (elf) != 0)
768 : 0 : error (0, 0, _("error while closing Elf descriptor: %s"),
769 : : elf_errmsg (-1));
770 : : return -1;
771 : : }
772 : :
773 : : /* Check if the file is an archive, and if so dump its index. */
774 : : static void
775 : 4 : check_archive_index (int fd, const char *fname, bool only_one)
776 : : {
777 : : /* Create an `Elf' descriptor. */
778 : 4 : Elf *elf = elf_begin (fd, ELF_C_READ_MMAP, NULL);
779 [ - + ]: 4 : if (elf == NULL)
780 : 0 : error (0, 0, _("cannot generate Elf descriptor: %s"),
781 : : elf_errmsg (-1));
782 : : else
783 : : {
784 [ + - ]: 4 : if (elf_kind (elf) == ELF_K_AR)
785 : : {
786 [ - + ]: 4 : if (!only_one)
787 : 0 : printf ("\n%s:\n\n", fname);
788 : 4 : dump_archive_index (elf, fname);
789 : : }
790 : : else
791 : 4 : error (0, 0,
792 : 0 : _("'%s' is not an archive, cannot print archive index"),
793 : : fname);
794 : :
795 : : /* Now we can close the descriptor. */
796 [ - + ]: 4 : if (elf_end (elf) != 0)
797 : 0 : error (0, 0, _("error while closing Elf descriptor: %s"),
798 : : elf_errmsg (-1));
799 : : }
800 : 4 : }
801 : :
802 : : /* Trivial callback used for checking if we opened an archive. */
803 : : static int
804 : 866 : count_dwflmod (Dwfl_Module *dwflmod __attribute__ ((unused)),
805 : : void **userdata __attribute__ ((unused)),
806 : : const char *name __attribute__ ((unused)),
807 : : Dwarf_Addr base __attribute__ ((unused)),
808 : : void *arg)
809 : : {
810 [ + - ]: 866 : if (*(bool *) arg)
811 : : return DWARF_CB_ABORT;
812 : 866 : *(bool *) arg = true;
813 : 866 : return DWARF_CB_OK;
814 : : }
815 : :
816 : : struct process_dwflmod_args
817 : : {
818 : : int fd;
819 : : bool only_one;
820 : : };
821 : :
822 : : static int
823 : 966 : process_dwflmod (Dwfl_Module *dwflmod,
824 : : void **userdata __attribute__ ((unused)),
825 : : const char *name __attribute__ ((unused)),
826 : : Dwarf_Addr base __attribute__ ((unused)),
827 : : void *arg)
828 : : {
829 : 966 : const struct process_dwflmod_args *a = arg;
830 : :
831 : : /* Print the file name. */
832 [ + + ]: 966 : if (!a->only_one)
833 : : {
834 : 100 : const char *fname;
835 : 100 : dwfl_module_info (dwflmod, NULL, NULL, NULL, NULL, NULL, &fname, NULL);
836 : :
837 : 100 : printf ("\n%s:\n\n", fname);
838 : : }
839 : :
840 : 966 : process_elf_file (dwflmod, a->fd);
841 : :
842 : 966 : return DWARF_CB_OK;
843 : : }
844 : :
845 : : /* Stub libdwfl callback, only the ELF handle already open is ever used.
846 : : Only used for finding the alternate debug file if the Dwarf comes from
847 : : the main file. We are not interested in separate debuginfo. */
848 : : static int
849 : 262 : find_no_debuginfo (Dwfl_Module *mod,
850 : : void **userdata,
851 : : const char *modname,
852 : : Dwarf_Addr base,
853 : : const char *file_name,
854 : : const char *debuglink_file,
855 : : GElf_Word debuglink_crc,
856 : : char **debuginfo_file_name)
857 : : {
858 : 262 : Dwarf_Addr dwbias;
859 : 262 : dwfl_module_info (mod, NULL, NULL, NULL, &dwbias, NULL, NULL, NULL);
860 : :
861 : : /* We are only interested if the Dwarf has been setup on the main
862 : : elf file but is only missing the alternate debug link. If dwbias
863 : : hasn't even been setup, this is searching for separate debuginfo
864 : : for the main elf. We don't care in that case. */
865 [ + + ]: 262 : if (dwbias == (Dwarf_Addr) -1)
866 : : return -1;
867 : :
868 : 70 : return dwfl_standard_find_debuginfo (mod, userdata, modname, base,
869 : : file_name, debuglink_file,
870 : : debuglink_crc, debuginfo_file_name);
871 : : }
872 : :
873 : : static Dwfl *
874 : 984 : create_dwfl (int fd, const char *fname)
875 : : {
876 : : /* Duplicate an fd for dwfl_report_offline to swallow. */
877 : 984 : int dwfl_fd = dup (fd);
878 [ - + ]: 984 : if (unlikely (dwfl_fd < 0))
879 : 0 : error_exit (errno, "dup");
880 : :
881 : : /* Use libdwfl in a trivial way to open the libdw handle for us.
882 : : This takes care of applying relocations to DWARF data in ET_REL files. */
883 : 984 : static const Dwfl_Callbacks callbacks =
884 : : {
885 : : .section_address = dwfl_offline_section_address,
886 : : .find_debuginfo = find_no_debuginfo
887 : : };
888 : 984 : Dwfl *dwfl = dwfl_begin (&callbacks);
889 [ + - ]: 984 : if (likely (dwfl != NULL))
890 : : /* Let 0 be the logical address of the file (or first in archive). */
891 : 984 : dwfl->offline_next_address = 0;
892 [ - + ]: 984 : if (dwfl_report_offline (dwfl, fname, fname, dwfl_fd) == NULL)
893 : : {
894 : 0 : struct stat st;
895 [ # # ]: 0 : if (fstat (dwfl_fd, &st) != 0)
896 : 0 : error (0, errno, _("cannot stat input file"));
897 [ # # ]: 0 : else if (unlikely (st.st_size == 0))
898 : 0 : error (0, 0, _("input file is empty"));
899 : : else
900 : 0 : error (0, 0, _("failed reading '%s': %s"),
901 : : fname, dwfl_errmsg (-1));
902 : 0 : close (dwfl_fd); /* Consumed on success, not on failure. */
903 : 0 : dwfl = NULL;
904 : : }
905 : : else
906 : 984 : dwfl_report_end (dwfl, NULL, NULL);
907 : :
908 : 984 : return dwfl;
909 : : }
910 : :
911 : : /* Process one input file. */
912 : : static void
913 : 970 : process_file (int fd, const char *fname, bool only_one)
914 : : {
915 [ + + ]: 970 : if (print_archive_index)
916 : 4 : check_archive_index (fd, fname, only_one);
917 : :
918 [ + + ]: 970 : if (!any_control_option)
919 : : return;
920 : :
921 [ + + ]: 966 : if (elf_input_section != NULL)
922 : : {
923 : : /* Replace fname and fd with section content. */
924 : 10 : char *fnname = alloca (strlen (fname) + strlen (elf_input_section) + 2);
925 : 10 : sprintf (fnname, "%s:%s", fname, elf_input_section);
926 : 10 : fd = open_input_section (fd);
927 [ - + ]: 10 : if (fd == -1)
928 : : {
929 : 0 : error (0, 0, _("No such section '%s' in '%s'"),
930 : : elf_input_section, fname);
931 : 0 : return;
932 : : }
933 : : fname = fnname;
934 : : }
935 : :
936 : 966 : Dwfl *dwfl = create_dwfl (fd, fname);
937 [ + - ]: 966 : if (dwfl != NULL)
938 : : {
939 [ + + ]: 966 : if (only_one)
940 : : {
941 : : /* Clear ONLY_ONE if we have multiple modules, from an archive. */
942 : 866 : bool seen = false;
943 : 866 : only_one = dwfl_getmodules (dwfl, &count_dwflmod, &seen, 0) == 0;
944 : : }
945 : :
946 : : /* Process the one or more modules gleaned from this file. */
947 : 966 : struct process_dwflmod_args a = { .fd = fd, .only_one = only_one };
948 : 966 : dwfl_getmodules (dwfl, &process_dwflmod, &a, 0);
949 : : }
950 : : /* Terrible hack for hooking unrelated skeleton/split compile units,
951 : : see __libdw_link_skel_split in print_debug. */
952 [ + - ]: 966 : if (! do_not_close_dwfl)
953 : 966 : dwfl_end (dwfl);
954 : :
955 : : /* Need to close the replaced fd if we created it. Caller takes
956 : : care of original. */
957 [ + + ]: 966 : if (elf_input_section != NULL)
958 : 10 : close (fd);
959 : : }
960 : :
961 : : /* Check whether there are any compressed sections in the ELF file. */
962 : : static bool
963 : 492 : elf_contains_chdrs (Elf *elf)
964 : : {
965 : 492 : Elf_Scn *scn = NULL;
966 [ + + ]: 797906 : while ((scn = elf_nextscn (elf, scn)) != NULL)
967 : : {
968 : 797528 : GElf_Shdr shdr_mem;
969 : 797528 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
970 [ + - + + ]: 797528 : if (shdr != NULL && (shdr->sh_flags & SHF_COMPRESSED) != 0)
971 : 114 : return true;
972 : : }
973 : : return false;
974 : : }
975 : :
976 : : /* Process one ELF file. */
977 : : static void
978 : 966 : process_elf_file (Dwfl_Module *dwflmod, int fd)
979 : : {
980 : 966 : GElf_Addr dwflbias;
981 : 966 : Elf *elf = dwfl_module_getelf (dwflmod, &dwflbias);
982 : :
983 : 966 : GElf_Ehdr ehdr_mem;
984 : 966 : GElf_Ehdr *ehdr = gelf_getehdr (elf, &ehdr_mem);
985 : :
986 [ - + ]: 966 : if (ehdr == NULL)
987 : : {
988 : 0 : error (0, 0, _("cannot read ELF header: %s"), elf_errmsg (-1));
989 : 0 : return;
990 : : }
991 : :
992 : 966 : Ebl *ebl = ebl_openbackend (elf);
993 [ - + ]: 966 : if (unlikely (ebl == NULL))
994 : : {
995 : 0 : ebl_error:
996 : 0 : error (0, errno, _("cannot create EBL handle"));
997 : 0 : return;
998 : : }
999 : :
1000 : : /* Determine the number of sections. */
1001 [ - + ]: 966 : if (unlikely (elf_getshdrnum (ebl->elf, &shnum) < 0))
1002 : 0 : error_exit (0, _("cannot determine number of sections: %s"),
1003 : : elf_errmsg (-1));
1004 : :
1005 : : /* Determine the number of phdrs. */
1006 [ - + ]: 966 : if (unlikely (elf_getphdrnum (ebl->elf, &phnum) < 0))
1007 : 0 : error_exit (0, _("cannot determine number of program headers: %s"),
1008 : : elf_errmsg (-1));
1009 : :
1010 : : /* For an ET_REL file, libdwfl has adjusted the in-core shdrs and
1011 : : may have applied relocation to some sections. If there are any
1012 : : compressed sections, any pass (or libdw/libdwfl) might have
1013 : : uncompressed them. So we need to get a fresh Elf handle on the
1014 : : file to display those. */
1015 : 2128 : bool print_unchanged = ((print_section_header
1016 [ + + ]: 460 : || print_relocations
1017 [ + + ]: 450 : || dump_data_sections != NULL
1018 [ + + ]: 438 : || print_notes)
1019 [ + + + + ]: 1034 : && (ehdr->e_type == ET_REL
1020 [ + + ]: 492 : || elf_contains_chdrs (ebl->elf)));
1021 : :
1022 : 196 : Elf *pure_elf = NULL;
1023 : 196 : Ebl *pure_ebl = ebl;
1024 : 196 : if (print_unchanged)
1025 : : {
1026 : : /* Read the file afresh. */
1027 : 196 : off_t aroff = elf_getaroff (elf);
1028 : 196 : pure_elf = dwelf_elf_begin (fd);
1029 [ - + ]: 196 : if (aroff > 0)
1030 : : {
1031 : : /* Archive member. */
1032 : 0 : (void) elf_rand (pure_elf, aroff);
1033 : 0 : Elf *armem = elf_begin (-1, ELF_C_READ_MMAP, pure_elf);
1034 : 0 : elf_end (pure_elf);
1035 : 0 : pure_elf = armem;
1036 : : }
1037 [ - + ]: 196 : if (pure_elf == NULL)
1038 : : {
1039 : 0 : error (0, 0, _("cannot read ELF: %s"), elf_errmsg (-1));
1040 : 0 : return;
1041 : : }
1042 : 196 : pure_ebl = ebl_openbackend (pure_elf);
1043 [ - + ]: 196 : if (pure_ebl == NULL)
1044 : 0 : goto ebl_error;
1045 : : }
1046 : :
1047 : 966 : bool symtab_printed = false;
1048 : :
1049 [ + + ]: 966 : if (print_file_header)
1050 : 190 : print_ehdr (ebl, ehdr);
1051 [ + + ]: 966 : if (print_section_header)
1052 : 506 : print_shdr (pure_ebl, ehdr);
1053 [ + + ]: 966 : if (print_program_header)
1054 : 188 : print_phdr (ebl, ehdr);
1055 [ + + ]: 966 : if (print_section_groups)
1056 : 202 : print_scngrp (ebl);
1057 [ + + ]: 966 : if (print_dynamic_table)
1058 : 190 : print_dynamic (ebl);
1059 [ + + ]: 966 : if (print_relocations)
1060 : 194 : print_relocs (pure_ebl, dwflmod, ehdr);
1061 [ + + ]: 966 : if (print_histogram)
1062 : 184 : handle_hash (ebl);
1063 [ + + + + ]: 966 : if (print_symbol_table || print_dynsym_table)
1064 : 220 : symtab_printed |= print_symtab (ebl, SHT_DYNSYM);
1065 [ + + ]: 966 : if (print_version_info)
1066 : 184 : print_verinfo (ebl);
1067 [ + + + - ]: 966 : if (print_symbol_table && !use_dynamic_segment)
1068 : 218 : symtab_printed |= print_symtab (ebl, SHT_SYMTAB);
1069 : :
1070 [ + + + + ]: 966 : if ((print_symbol_table || print_dynsym_table)
1071 [ + + + + ]: 220 : && !symtab_printed && symbol_table_section != NULL)
1072 : 2 : printf ("WARNING: %s: '%s'\n", _("cannot find section"),
1073 : : symbol_table_section);
1074 : :
1075 [ + + ]: 966 : if (print_arch)
1076 : 192 : print_liblist (ebl);
1077 [ + + ]: 966 : if (print_arch)
1078 : 192 : print_attributes (ebl, ehdr);
1079 [ + + ]: 966 : if (dump_data_sections != NULL)
1080 : 12 : dump_data (pure_ebl);
1081 [ + + ]: 966 : if (string_sections != NULL)
1082 : 186 : dump_strings (ebl);
1083 [ + + ]: 966 : if ((print_debug_sections | implicit_debug_sections) != 0)
1084 : 500 : print_debug (dwflmod, ebl, ehdr);
1085 [ + + ]: 966 : if (print_notes)
1086 : 230 : handle_notes (pure_ebl, ehdr);
1087 [ - + ]: 966 : if (print_string_sections)
1088 : 0 : print_strings (ebl);
1089 : :
1090 [ + + ]: 966 : if (pure_ebl != ebl)
1091 : : {
1092 : 196 : ebl_closebackend (ebl);
1093 : 196 : ebl_closebackend (pure_ebl);
1094 : 196 : elf_end (pure_elf);
1095 : : }
1096 : : else
1097 : 770 : ebl_closebackend (ebl);
1098 : : }
1099 : :
1100 : :
1101 : : /* Print file type. */
1102 : : static void
1103 : 190 : print_file_type (unsigned short int e_type)
1104 : : {
1105 [ + - ]: 190 : if (likely (e_type <= ET_CORE))
1106 : : {
1107 : 190 : static const char *const knowntypes[] =
1108 : : {
1109 : : N_("NONE (None)"),
1110 : : N_("REL (Relocatable file)"),
1111 : : N_("EXEC (Executable file)"),
1112 : : N_("DYN (Shared object file)"),
1113 : : N_("CORE (Core file)")
1114 : : };
1115 : 190 : puts (_(knowntypes[e_type]));
1116 : : }
1117 [ # # ]: 0 : else if (e_type >= ET_LOOS && e_type <= ET_HIOS)
1118 : 0 : printf (_("OS Specific: (%x)\n"), e_type);
1119 [ # # ]: 0 : else if (e_type >= ET_LOPROC /* && e_type <= ET_HIPROC always true */)
1120 : 0 : printf (_("Processor Specific: (%x)\n"), e_type);
1121 : : else
1122 : 0 : puts ("???");
1123 : 190 : }
1124 : :
1125 : :
1126 : : /* Print ELF header. */
1127 : : static void
1128 : 190 : print_ehdr (Ebl *ebl, GElf_Ehdr *ehdr)
1129 : : {
1130 : 190 : fputs (_("ELF Header:\n Magic: "), stdout);
1131 [ + + ]: 3230 : for (size_t cnt = 0; cnt < EI_NIDENT; ++cnt)
1132 : 3040 : printf (" %02hhx", ehdr->e_ident[cnt]);
1133 : :
1134 : 190 : printf (_("\n Class: %s\n"),
1135 [ + + ]: 190 : ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? "ELF32"
1136 : : : ehdr->e_ident[EI_CLASS] == ELFCLASS64 ? "ELF64"
1137 [ - + ]: 156 : : "\?\?\?");
1138 : :
1139 : 190 : printf (_(" Data: %s\n"),
1140 [ - + ]: 190 : ehdr->e_ident[EI_DATA] == ELFDATA2LSB
1141 : : ? "2's complement, little endian"
1142 : : : ehdr->e_ident[EI_DATA] == ELFDATA2MSB
1143 [ # # ]: 0 : ? "2's complement, big endian" : "\?\?\?");
1144 : :
1145 : 570 : printf (_(" Ident Version: %hhd %s\n"),
1146 : 190 : ehdr->e_ident[EI_VERSION],
1147 [ + - ]: 190 : ehdr->e_ident[EI_VERSION] == EV_CURRENT ? _("(current)")
1148 : : : "(\?\?\?)");
1149 : :
1150 : 190 : char buf[512];
1151 : 190 : printf (_(" OS/ABI: %s\n"),
1152 : 190 : ebl_osabi_name (ebl, ehdr->e_ident[EI_OSABI], buf, sizeof (buf)));
1153 : :
1154 : 380 : printf (_(" ABI Version: %hhd\n"),
1155 : 190 : ehdr->e_ident[EI_ABIVERSION]);
1156 : :
1157 : 190 : fputs (_(" Type: "), stdout);
1158 : 190 : print_file_type (ehdr->e_type);
1159 : :
1160 : 190 : const char *machine = dwelf_elf_e_machine_string (ehdr->e_machine);
1161 [ + - ]: 190 : if (machine != NULL)
1162 : 190 : printf (_(" Machine: %s\n"), machine);
1163 : : else
1164 : 190 : printf (_(" Machine: <unknown>: 0x%x\n"),
1165 : 0 : ehdr->e_machine);
1166 : :
1167 : 190 : printf (_(" Version: %d %s\n"),
1168 : : ehdr->e_version,
1169 [ + - ]: 190 : ehdr->e_version == EV_CURRENT ? _("(current)") : "(\?\?\?)");
1170 : :
1171 : 190 : printf (_(" Entry point address: %#" PRIx64 "\n"),
1172 : : ehdr->e_entry);
1173 : :
1174 : 190 : printf (_(" Start of program headers: %" PRId64 " %s\n"),
1175 : : ehdr->e_phoff, _("(bytes into file)"));
1176 : :
1177 : 190 : printf (_(" Start of section headers: %" PRId64 " %s\n"),
1178 : : ehdr->e_shoff, _("(bytes into file)"));
1179 : :
1180 : 190 : printf (_(" Flags: %s\n"),
1181 : : ebl_machine_flag_name (ebl, ehdr->e_flags, buf, sizeof (buf)));
1182 : :
1183 : 380 : printf (_(" Size of this header: %" PRId16 " %s\n"),
1184 : 190 : ehdr->e_ehsize, _("(bytes)"));
1185 : :
1186 : 380 : printf (_(" Size of program header entries: %" PRId16 " %s\n"),
1187 : 190 : ehdr->e_phentsize, _("(bytes)"));
1188 : :
1189 : 380 : printf (_(" Number of program headers entries: %" PRId16),
1190 : 190 : ehdr->e_phnum);
1191 [ + + ]: 190 : if (ehdr->e_phnum == PN_XNUM)
1192 : : {
1193 : 2 : GElf_Shdr shdr_mem;
1194 : 2 : GElf_Shdr *shdr = gelf_getshdr (elf_getscn (ebl->elf, 0), &shdr_mem);
1195 [ + - ]: 2 : if (shdr != NULL)
1196 : 2 : printf (_(" (%" PRIu32 " in [0].sh_info)"),
1197 : 2 : (uint32_t) shdr->sh_info);
1198 : : else
1199 : 0 : fputs (_(" ([0] not available)"), stdout);
1200 : : }
1201 : 190 : fputc ('\n', stdout);
1202 : :
1203 : 380 : printf (_(" Size of section header entries: %" PRId16 " %s\n"),
1204 : 190 : ehdr->e_shentsize, _("(bytes)"));
1205 : :
1206 : 380 : printf (_(" Number of section headers entries: %" PRId16),
1207 : 190 : ehdr->e_shnum);
1208 [ - + ]: 190 : if (ehdr->e_shnum == 0)
1209 : : {
1210 : 0 : GElf_Shdr shdr_mem;
1211 : 0 : GElf_Shdr *shdr = gelf_getshdr (elf_getscn (ebl->elf, 0), &shdr_mem);
1212 [ # # ]: 0 : if (shdr != NULL)
1213 : 0 : printf (_(" (%" PRIu32 " in [0].sh_size)"),
1214 : 0 : (uint32_t) shdr->sh_size);
1215 : : else
1216 : 0 : fputs (_(" ([0] not available)"), stdout);
1217 : : }
1218 : 190 : fputc ('\n', stdout);
1219 : :
1220 [ - + ]: 190 : if (unlikely (ehdr->e_shstrndx == SHN_XINDEX))
1221 : : {
1222 : 0 : GElf_Shdr shdr_mem;
1223 : 0 : GElf_Shdr *shdr = gelf_getshdr (elf_getscn (ebl->elf, 0), &shdr_mem);
1224 [ # # ]: 0 : if (shdr != NULL)
1225 : : /* We managed to get the zeroth section. */
1226 : 0 : snprintf (buf, sizeof (buf), _(" (%" PRIu32 " in [0].sh_link)"),
1227 : 0 : (uint32_t) shdr->sh_link);
1228 : : else
1229 : : {
1230 : 0 : strncpy (buf, _(" ([0] not available)"), sizeof (buf) - 1);
1231 : 0 : buf[sizeof (buf) - 1] = '\0';
1232 : : }
1233 : :
1234 : 0 : printf (_(" Section header string table index: XINDEX%s\n\n"),
1235 : : buf);
1236 : : }
1237 : : else
1238 : 190 : printf (_(" Section header string table index: %" PRId16 "\n\n"),
1239 : : ehdr->e_shstrndx);
1240 : 190 : }
1241 : :
1242 : :
1243 : : static const char *
1244 : 76748 : get_visibility_type (int value)
1245 : : {
1246 [ - + - - : 76748 : switch (value)
+ ]
1247 : : {
1248 : : case STV_DEFAULT:
1249 : : return "DEFAULT";
1250 : 0 : case STV_INTERNAL:
1251 : 0 : return "INTERNAL";
1252 : 984 : case STV_HIDDEN:
1253 : 984 : return "HIDDEN";
1254 : 0 : case STV_PROTECTED:
1255 : 0 : return "PROTECTED";
1256 : 0 : default:
1257 : 0 : return "???";
1258 : : }
1259 : : }
1260 : :
1261 : : static const char *
1262 : 384 : elf_ch_type_name (unsigned int code)
1263 : : {
1264 : 384 : switch (code)
1265 : : {
1266 : : case 0:
1267 : : return "NONE";
1268 : 24 : case ELFCOMPRESS_ZLIB:
1269 : 24 : return "ZLIB";
1270 : 360 : case ELFCOMPRESS_ZSTD:
1271 : 360 : return "ZSTD";
1272 : 0 : default:
1273 : 0 : return "UNKNOWN";
1274 : : }
1275 : : }
1276 : :
1277 : : /* Print the section headers. */
1278 : : static void
1279 : 506 : print_shdr (Ebl *ebl, GElf_Ehdr *ehdr)
1280 : : {
1281 : 506 : size_t cnt;
1282 : 506 : size_t shstrndx;
1283 : :
1284 [ + + ]: 506 : if (! print_file_header)
1285 : : {
1286 : 322 : size_t sections;
1287 [ - + ]: 322 : if (unlikely (elf_getshdrnum (ebl->elf, §ions) < 0))
1288 : 0 : error_exit (0, _("cannot get number of sections: %s"),
1289 : : elf_errmsg (-1));
1290 : :
1291 : 322 : printf (_("\
1292 : : There are %zd section headers, starting at offset %#" PRIx64 ":\n\
1293 : : \n"),
1294 : : sections, ehdr->e_shoff);
1295 : : }
1296 : :
1297 : : /* Get the section header string table index. */
1298 [ - + ]: 506 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
1299 : 0 : error_exit (0, _("cannot get section header string table index: %s"),
1300 : : elf_errmsg (-1));
1301 : :
1302 : 506 : puts (_("Section Headers:"));
1303 : :
1304 [ + + ]: 506 : if (ehdr->e_ident[EI_CLASS] == ELFCLASS32)
1305 : 196 : puts (_("[Nr] Name Type Addr Off Size ES Flags Lk Inf Al"));
1306 : : else
1307 : 310 : puts (_("[Nr] Name Type Addr Off Size ES Flags Lk Inf Al"));
1308 : :
1309 [ + + ]: 506 : if (print_decompress)
1310 : : {
1311 [ + + ]: 112 : if (ehdr->e_ident[EI_CLASS] == ELFCLASS32)
1312 : 56 : puts (_(" [Compression Size Al]"));
1313 : : else
1314 : 56 : puts (_(" [Compression Size Al]"));
1315 : : }
1316 : :
1317 [ + + ]: 1586316 : for (cnt = 0; cnt < shnum; ++cnt)
1318 : : {
1319 : 1585810 : Elf_Scn *scn = elf_getscn (ebl->elf, cnt);
1320 : :
1321 [ - + ]: 1585810 : if (unlikely (scn == NULL))
1322 : 0 : error_exit (0, _("cannot get section: %s"),
1323 : : elf_errmsg (-1));
1324 : :
1325 : : /* Get the section header. */
1326 : 1585810 : GElf_Shdr shdr_mem;
1327 : 1585810 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
1328 [ - + ]: 1585810 : if (unlikely (shdr == NULL))
1329 : 0 : error_exit (0, _("cannot get section header: %s"),
1330 : : elf_errmsg (-1));
1331 : :
1332 : 1585810 : char flagbuf[20];
1333 : 1585810 : char *cp = flagbuf;
1334 [ + + ]: 1585810 : if (shdr->sh_flags & SHF_WRITE)
1335 : 2764 : *cp++ = 'W';
1336 [ + + ]: 1585810 : if (shdr->sh_flags & SHF_ALLOC)
1337 : 8520 : *cp++ = 'A';
1338 [ + + ]: 1585810 : if (shdr->sh_flags & SHF_EXECINSTR)
1339 : 1564 : *cp++ = 'X';
1340 [ + + ]: 1585810 : if (shdr->sh_flags & SHF_MERGE)
1341 : 420 : *cp++ = 'M';
1342 [ + + ]: 1585810 : if (shdr->sh_flags & SHF_STRINGS)
1343 : 380 : *cp++ = 'S';
1344 [ + + ]: 1585810 : if (shdr->sh_flags & SHF_INFO_LINK)
1345 : 196 : *cp++ = 'I';
1346 [ + + ]: 1585810 : if (shdr->sh_flags & SHF_LINK_ORDER)
1347 : 4 : *cp++ = 'L';
1348 [ - + ]: 1585810 : if (shdr->sh_flags & SHF_OS_NONCONFORMING)
1349 : 0 : *cp++ = 'N';
1350 [ - + ]: 1585810 : if (shdr->sh_flags & SHF_GROUP)
1351 : 0 : *cp++ = 'G';
1352 [ + + ]: 1585810 : if (shdr->sh_flags & SHF_TLS)
1353 : 30 : *cp++ = 'T';
1354 [ + + ]: 1585810 : if (shdr->sh_flags & SHF_COMPRESSED)
1355 : 414 : *cp++ = 'C';
1356 [ - + ]: 1585810 : if (shdr->sh_flags & SHF_ORDERED)
1357 : 0 : *cp++ = 'O';
1358 [ - + ]: 1585810 : if (shdr->sh_flags & SHF_EXCLUDE)
1359 : 0 : *cp++ = 'E';
1360 [ + + ]: 1585810 : if (shdr->sh_flags & SHF_GNU_RETAIN)
1361 : 2 : *cp++ = 'R';
1362 : 1585810 : *cp = '\0';
1363 : :
1364 : 1585810 : const char *sname;
1365 : 1585810 : char buf[128];
1366 [ - + ]: 1585810 : sname = elf_strptr (ebl->elf, shstrndx, shdr->sh_name) ?: "<corrupt>";
1367 [ + + ]: 3171620 : printf ("[%2zu] %-20s %-12s %0*" PRIx64 " %0*" PRIx64 " %0*" PRIx64
1368 : : " %2" PRId64 " %-5s %2" PRId32 " %3" PRId32
1369 : : " %2" PRId64 "\n",
1370 : : cnt, sname,
1371 : 1585810 : ebl_section_type_name (ebl, shdr->sh_type, buf, sizeof (buf)),
1372 : : ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 8 : 16, shdr->sh_addr,
1373 : : ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 6 : 8, shdr->sh_offset,
1374 [ + + ]: 1585810 : ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 6 : 8, shdr->sh_size,
1375 : : shdr->sh_entsize, flagbuf, shdr->sh_link, shdr->sh_info,
1376 : : shdr->sh_addralign);
1377 : :
1378 [ + + ]: 1585810 : if (print_decompress)
1379 : : {
1380 [ + + ]: 1880 : if ((shdr->sh_flags & SHF_COMPRESSED) != 0)
1381 : : {
1382 : 384 : GElf_Chdr chdr;
1383 [ + - ]: 384 : if (gelf_getchdr (scn, &chdr) != NULL)
1384 [ + + - - ]: 768 : printf (" [ELF %s (%" PRId32 ") %0*" PRIx64
1385 : : " %2" PRId64 "]\n",
1386 : : elf_ch_type_name (chdr.ch_type),
1387 : : chdr.ch_type,
1388 [ + + ]: 384 : ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 6 : 8,
1389 : : chdr.ch_size, chdr.ch_addralign);
1390 : : else
1391 : 384 : error (0, 0,
1392 : 0 : _("bad compression header for section %zd: %s"),
1393 : : elf_ndxscn (scn), elf_errmsg (-1));
1394 : : }
1395 [ + + ]: 1496 : else if (startswith (sname, ".zdebug"))
1396 : : {
1397 : 24 : ssize_t size;
1398 [ + - ]: 24 : if ((size = dwelf_scn_gnu_compressed_size (scn)) >= 0)
1399 : 1585834 : printf (" [GNU ZLIB %0*zx ]\n",
1400 [ + + ]: 24 : ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 6 : 8, size);
1401 : : else
1402 : 1585810 : error (0, 0,
1403 : 0 : _("bad gnu compressed size for section %zd: %s"),
1404 : : elf_ndxscn (scn), elf_errmsg (-1));
1405 : : }
1406 : : }
1407 : : }
1408 : :
1409 : 506 : fputc ('\n', stdout);
1410 : 506 : }
1411 : :
1412 : :
1413 : : /* Print the program header. */
1414 : : static void
1415 : 188 : print_phdr (Ebl *ebl, GElf_Ehdr *ehdr)
1416 : : {
1417 [ + + ]: 188 : if (phnum == 0)
1418 : : /* No program header, this is OK in relocatable objects. */
1419 : 12 : return;
1420 : :
1421 : 176 : puts (_("Program Headers:"));
1422 [ + + ]: 176 : if (ehdr->e_ident[EI_CLASS] == ELFCLASS32)
1423 : 26 : puts (_("\
1424 : : Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align"));
1425 : : else
1426 : 150 : puts (_("\
1427 : : Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align"));
1428 : :
1429 : : /* Process all program headers. */
1430 : : bool has_relro = false;
1431 : : GElf_Addr relro_from = 0;
1432 : : GElf_Addr relro_to = 0;
1433 [ + + ]: 2032 : for (size_t cnt = 0; cnt < phnum; ++cnt)
1434 : : {
1435 : 1856 : char buf[128];
1436 : 1856 : GElf_Phdr mem;
1437 : 1856 : GElf_Phdr *phdr = gelf_getphdr (ebl->elf, cnt, &mem);
1438 : :
1439 : : /* If for some reason the header cannot be returned show this. */
1440 [ - + ]: 1856 : if (unlikely (phdr == NULL))
1441 : : {
1442 : 0 : puts (" ???");
1443 : 0 : continue;
1444 : : }
1445 : :
1446 : 1856 : printf (" %-14s 0x%06" PRIx64 " 0x%0*" PRIx64 " 0x%0*" PRIx64
1447 : : " 0x%06" PRIx64 " 0x%06" PRIx64 " %c%c%c 0x%" PRIx64 "\n",
1448 : 1856 : ebl_segment_type_name (ebl, phdr->p_type, buf, sizeof (buf)),
1449 : : phdr->p_offset,
1450 : : ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 8 : 16, phdr->p_vaddr,
1451 [ + + ]: 1856 : ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 8 : 16, phdr->p_paddr,
1452 : : phdr->p_filesz,
1453 : : phdr->p_memsz,
1454 [ - + ]: 1856 : phdr->p_flags & PF_R ? 'R' : ' ',
1455 [ + + ]: 1856 : phdr->p_flags & PF_W ? 'W' : ' ',
1456 [ + + ]: 1856 : phdr->p_flags & PF_X ? 'E' : ' ',
1457 : : phdr->p_align);
1458 : :
1459 [ + + ]: 1856 : if (phdr->p_type == PT_INTERP)
1460 : : {
1461 : : /* If we are sure the file offset is valid then we can show
1462 : : the user the name of the interpreter. We check whether
1463 : : there is a section at the file offset. Normally there
1464 : : would be a section called ".interp". But in separate
1465 : : .debug files it is a NOBITS section (and so doesn't match
1466 : : with gelf_offscn). Which probably means the offset is
1467 : : not valid another reason could be because the ELF file
1468 : : just doesn't contain any section headers, in that case
1469 : : just play it safe and don't display anything. */
1470 : :
1471 : 172 : Elf_Scn *scn = gelf_offscn (ebl->elf, phdr->p_offset);
1472 : 172 : GElf_Shdr shdr_mem;
1473 : 172 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
1474 : :
1475 : 172 : size_t maxsize;
1476 : 172 : char *filedata = elf_rawfile (ebl->elf, &maxsize);
1477 : :
1478 [ + - + + ]: 172 : if (shdr != NULL && shdr->sh_type == SHT_PROGBITS
1479 [ + - + - ]: 130 : && filedata != NULL && phdr->p_offset < maxsize
1480 [ + - ]: 130 : && phdr->p_filesz <= maxsize - phdr->p_offset
1481 [ + - ]: 130 : && memchr (filedata + phdr->p_offset, '\0',
1482 : : phdr->p_filesz) != NULL)
1483 : 172 : printf (_("\t[Requesting program interpreter: %s]\n"),
1484 : : filedata + phdr->p_offset);
1485 : : }
1486 [ + + ]: 1684 : else if (phdr->p_type == PT_GNU_RELRO)
1487 : : {
1488 : 152 : has_relro = true;
1489 : 152 : relro_from = phdr->p_vaddr;
1490 : 152 : relro_to = relro_from + phdr->p_memsz;
1491 : : }
1492 : : }
1493 : :
1494 : 176 : size_t sections;
1495 [ - + ]: 176 : if (unlikely (elf_getshdrnum (ebl->elf, §ions) < 0))
1496 : 0 : error_exit (0, _("cannot get number of sections: %s"),
1497 : : elf_errmsg (-1));
1498 : :
1499 [ + - ]: 176 : if (sections == 0)
1500 : : /* No sections in the file. Punt. */
1501 : : return;
1502 : :
1503 : : /* Get the section header string table index. */
1504 : 176 : size_t shstrndx;
1505 [ - + ]: 176 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
1506 : 0 : error_exit (0, _("cannot get section header string table index"));
1507 : :
1508 : 176 : puts (_("\n Section to Segment mapping:\n Segment Sections..."));
1509 : :
1510 [ + + ]: 2032 : for (size_t cnt = 0; cnt < phnum; ++cnt)
1511 : : {
1512 : : /* Print the segment number. */
1513 : 1856 : printf (" %2.2zu ", cnt);
1514 : :
1515 : 1856 : GElf_Phdr phdr_mem;
1516 : 1856 : GElf_Phdr *phdr = gelf_getphdr (ebl->elf, cnt, &phdr_mem);
1517 : : /* This must not happen. */
1518 [ - + ]: 1856 : if (unlikely (phdr == NULL))
1519 : 0 : error_exit (0, _("cannot get program header: %s"),
1520 : : elf_errmsg (-1));
1521 : :
1522 : : /* Iterate over the sections. */
1523 : : bool in_relro = false;
1524 : : bool in_ro = false;
1525 [ + + ]: 60712 : for (size_t inner = 1; inner < shnum; ++inner)
1526 : : {
1527 : 58856 : Elf_Scn *scn = elf_getscn (ebl->elf, inner);
1528 : : /* This should not happen. */
1529 [ - + ]: 58856 : if (unlikely (scn == NULL))
1530 : 0 : error_exit (0, _("cannot get section: %s"),
1531 : : elf_errmsg (-1));
1532 : :
1533 : : /* Get the section header. */
1534 : 58856 : GElf_Shdr shdr_mem;
1535 : 58856 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
1536 [ - + ]: 58856 : if (unlikely (shdr == NULL))
1537 : 0 : error_exit (0, _("cannot get section header: %s"),
1538 : : elf_errmsg (-1));
1539 : :
1540 [ + - ]: 58856 : if (shdr->sh_size > 0
1541 : : /* Compare allocated sections by VMA, unallocated
1542 : : sections by file offset. */
1543 [ + + ]: 58856 : && (shdr->sh_flags & SHF_ALLOC
1544 : 46478 : ? (shdr->sh_addr >= phdr->p_vaddr
1545 [ + + ]: 46478 : && (shdr->sh_addr + shdr->sh_size
1546 [ + + ]: 31442 : <= phdr->p_vaddr + phdr->p_memsz))
1547 : 12378 : : (shdr->sh_offset >= phdr->p_offset
1548 [ + + ]: 12378 : && (shdr->sh_offset + shdr->sh_size
1549 [ + + ]: 10852 : <= phdr->p_offset + phdr->p_filesz))))
1550 : : {
1551 [ + + ]: 6262 : if (has_relro && !in_relro
1552 [ + + ]: 4178 : && shdr->sh_addr >= relro_from
1553 [ + + ]: 732 : && shdr->sh_addr + shdr->sh_size <= relro_to)
1554 : : {
1555 : 474 : fputs (" [RELRO:", stdout);
1556 : 474 : in_relro = true;
1557 : : }
1558 [ + + + + ]: 5788 : else if (has_relro && in_relro && shdr->sh_addr >= relro_to)
1559 : : {
1560 : 134 : fputs ("]", stdout);
1561 : 134 : in_relro = false;
1562 : : }
1563 [ + + ]: 5654 : else if (has_relro && in_relro
1564 [ + + ]: 1166 : && shdr->sh_addr + shdr->sh_size > relro_to)
1565 : 18 : fputs ("] <RELRO:", stdout);
1566 [ + + ]: 5636 : else if (phdr->p_type == PT_LOAD && (phdr->p_flags & PF_W) == 0)
1567 : : {
1568 [ + + ]: 3226 : if (!in_ro)
1569 : : {
1570 : 372 : fputs (" [RO:", stdout);
1571 : 372 : in_ro = true;
1572 : : }
1573 : : }
1574 : : else
1575 : : {
1576 : : /* Determine the segment this section is part of. */
1577 : : size_t cnt2;
1578 : : GElf_Phdr phdr2_mem;
1579 : : GElf_Phdr *phdr2 = NULL;
1580 [ + - ]: 10776 : for (cnt2 = 0; cnt2 < phnum; ++cnt2)
1581 : : {
1582 : 10776 : phdr2 = gelf_getphdr (ebl->elf, cnt2, &phdr2_mem);
1583 : :
1584 [ + - + + ]: 10776 : if (phdr2 != NULL && phdr2->p_type == PT_LOAD
1585 [ + - ]: 6068 : && shdr->sh_addr >= phdr2->p_vaddr
1586 : 6068 : && (shdr->sh_addr + shdr->sh_size
1587 [ + + ]: 6068 : <= phdr2->p_vaddr + phdr2->p_memsz))
1588 : : break;
1589 : : }
1590 : :
1591 [ + - ]: 2410 : if (cnt2 < phnum)
1592 : : {
1593 [ + + + + ]: 2410 : if ((phdr2->p_flags & PF_W) == 0 && !in_ro)
1594 : : {
1595 : 614 : fputs (" [RO:", stdout);
1596 : 614 : in_ro = true;
1597 : : }
1598 [ + + + - ]: 1796 : else if ((phdr2->p_flags & PF_W) != 0 && in_ro)
1599 : : {
1600 : 0 : fputs ("]", stdout);
1601 : 0 : in_ro = false;
1602 : : }
1603 : : }
1604 : : }
1605 : :
1606 : 6262 : printf (" %s",
1607 : 6262 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name));
1608 : :
1609 : : /* Signal that this section is only partially covered. */
1610 [ + + ]: 6262 : if (has_relro && in_relro
1611 [ + + ]: 1640 : && shdr->sh_addr + shdr->sh_size > relro_to)
1612 : : {
1613 : 18 : fputs (">", stdout);
1614 : 18 : in_relro = false;
1615 : : }
1616 : : }
1617 : : }
1618 [ + + ]: 1856 : if (in_relro || in_ro)
1619 : 1308 : fputs ("]", stdout);
1620 : :
1621 : : /* Finish the line. */
1622 : 1856 : fputc ('\n', stdout);
1623 : : }
1624 : : }
1625 : :
1626 : :
1627 : : static const char *
1628 : 1250 : section_name (Ebl *ebl, GElf_Shdr *shdr)
1629 : : {
1630 : 1250 : size_t shstrndx;
1631 [ + - - + ]: 1250 : if (shdr == NULL || elf_getshdrstrndx (ebl->elf, &shstrndx) < 0)
1632 : 0 : return "???";
1633 [ - + ]: 1250 : return elf_strptr (ebl->elf, shstrndx, shdr->sh_name) ?: "???";
1634 : : }
1635 : :
1636 : :
1637 : : static void
1638 : 32 : handle_scngrp (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr)
1639 : : {
1640 : : /* Get the data of the section. */
1641 : 32 : Elf_Data *data = elf_getdata (scn, NULL);
1642 : :
1643 : 32 : Elf_Scn *symscn = elf_getscn (ebl->elf, shdr->sh_link);
1644 : 32 : GElf_Shdr symshdr_mem;
1645 : 32 : GElf_Shdr *symshdr = gelf_getshdr (symscn, &symshdr_mem);
1646 : 32 : Elf_Data *symdata = elf_getdata (symscn, NULL);
1647 : :
1648 [ + - + - ]: 32 : if (data == NULL || data->d_size < sizeof (Elf32_Word) || symshdr == NULL
1649 [ - + ]: 32 : || symdata == NULL)
1650 : 0 : return;
1651 : :
1652 : : /* Get the section header string table index. */
1653 : 32 : size_t shstrndx;
1654 [ - + ]: 32 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
1655 : 0 : error_exit (0, _("cannot get section header string table index"));
1656 : :
1657 : 32 : Elf32_Word *grpref = (Elf32_Word *) data->d_buf;
1658 : :
1659 : 32 : GElf_Sym sym_mem;
1660 : 32 : GElf_Sym *sym = gelf_getsym (symdata, shdr->sh_info, &sym_mem);
1661 : :
1662 [ - + + + ]: 64 : printf ((grpref[0] & GRP_COMDAT)
1663 : 8 : ? ngettext ("\
1664 : : \nCOMDAT section group [%2zu] '%s' with signature '%s' contains %zu entry:\n",
1665 : : "\
1666 : : \nCOMDAT section group [%2zu] '%s' with signature '%s' contains %zu entries:\n",
1667 : : data->d_size / sizeof (Elf32_Word) - 1)
1668 : 24 : : ngettext ("\
1669 : : \nSection group [%2zu] '%s' with signature '%s' contains %zu entry:\n", "\
1670 : : \nSection group [%2zu] '%s' with signature '%s' contains %zu entries:\n",
1671 : : data->d_size / sizeof (Elf32_Word) - 1),
1672 : : elf_ndxscn (scn),
1673 : 32 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
1674 : : (sym == NULL ? NULL
1675 : 32 : : elf_strptr (ebl->elf, symshdr->sh_link, sym->st_name))
1676 : 0 : ?: _("<INVALID SYMBOL>"),
1677 [ + - ]: 32 : data->d_size / sizeof (Elf32_Word) - 1);
1678 : :
1679 [ + + ]: 104 : for (size_t cnt = 1; cnt < data->d_size / sizeof (Elf32_Word); ++cnt)
1680 : : {
1681 : 72 : GElf_Shdr grpshdr_mem;
1682 : 72 : GElf_Shdr *grpshdr = gelf_getshdr (elf_getscn (ebl->elf, grpref[cnt]),
1683 : : &grpshdr_mem);
1684 : :
1685 : 72 : const char *str;
1686 [ + - ]: 144 : printf (" [%2u] %s\n",
1687 : : grpref[cnt],
1688 : : grpshdr != NULL
1689 [ - + ]: 72 : && (str = elf_strptr (ebl->elf, shstrndx, grpshdr->sh_name))
1690 : 0 : ? str : _("<INVALID SECTION>"));
1691 : : }
1692 : : }
1693 : :
1694 : :
1695 : : static void
1696 : 202 : print_scngrp (Ebl *ebl)
1697 : : {
1698 : : /* Find all relocation sections and handle them. */
1699 : 202 : Elf_Scn *scn = NULL;
1700 : :
1701 [ + + ]: 6522 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
1702 : : {
1703 : : /* Handle the section if it is a symbol table. */
1704 : 6320 : GElf_Shdr shdr_mem;
1705 : 6320 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
1706 : :
1707 [ + - + + ]: 6320 : if (shdr != NULL && shdr->sh_type == SHT_GROUP)
1708 : : {
1709 [ - + ]: 32 : if ((shdr->sh_flags & SHF_COMPRESSED) != 0)
1710 : : {
1711 [ # # ]: 0 : if (elf_compress (scn, 0, 0) < 0)
1712 : 0 : printf ("WARNING: %s [%zd]\n",
1713 : : _("Couldn't uncompress section"),
1714 : : elf_ndxscn (scn));
1715 : 0 : shdr = gelf_getshdr (scn, &shdr_mem);
1716 [ # # ]: 0 : if (unlikely (shdr == NULL))
1717 : 0 : error_exit (0, _("cannot get section [%zd] header: %s"),
1718 : : elf_ndxscn (scn),
1719 : : elf_errmsg (-1));
1720 : : }
1721 : 32 : handle_scngrp (ebl, scn, shdr);
1722 : : }
1723 : : }
1724 : 202 : }
1725 : :
1726 : :
1727 : : static const struct flags
1728 : : {
1729 : : int mask;
1730 : : const char *str;
1731 : : } dt_flags[] =
1732 : : {
1733 : : { DF_ORIGIN, "ORIGIN" },
1734 : : { DF_SYMBOLIC, "SYMBOLIC" },
1735 : : { DF_TEXTREL, "TEXTREL" },
1736 : : { DF_BIND_NOW, "BIND_NOW" },
1737 : : { DF_STATIC_TLS, "STATIC_TLS" }
1738 : : };
1739 : : static const int ndt_flags = sizeof (dt_flags) / sizeof (dt_flags[0]);
1740 : :
1741 : : static const struct flags dt_flags_1[] =
1742 : : {
1743 : : { DF_1_NOW, "NOW" },
1744 : : { DF_1_GLOBAL, "GLOBAL" },
1745 : : { DF_1_GROUP, "GROUP" },
1746 : : { DF_1_NODELETE, "NODELETE" },
1747 : : { DF_1_LOADFLTR, "LOADFLTR" },
1748 : : { DF_1_INITFIRST, "INITFIRST" },
1749 : : { DF_1_NOOPEN, "NOOPEN" },
1750 : : { DF_1_ORIGIN, "ORIGIN" },
1751 : : { DF_1_DIRECT, "DIRECT" },
1752 : : { DF_1_TRANS, "TRANS" },
1753 : : { DF_1_INTERPOSE, "INTERPOSE" },
1754 : : { DF_1_NODEFLIB, "NODEFLIB" },
1755 : : { DF_1_NODUMP, "NODUMP" },
1756 : : { DF_1_CONFALT, "CONFALT" },
1757 : : { DF_1_ENDFILTEE, "ENDFILTEE" },
1758 : : { DF_1_DISPRELDNE, "DISPRELDNE" },
1759 : : { DF_1_DISPRELPND, "DISPRELPND" },
1760 : : };
1761 : : static const int ndt_flags_1 = sizeof (dt_flags_1) / sizeof (dt_flags_1[0]);
1762 : :
1763 : : static const struct flags dt_feature_1[] =
1764 : : {
1765 : : { DTF_1_PARINIT, "PARINIT" },
1766 : : { DTF_1_CONFEXP, "CONFEXP" }
1767 : : };
1768 : : static const int ndt_feature_1 = (sizeof (dt_feature_1)
1769 : : / sizeof (dt_feature_1[0]));
1770 : :
1771 : : static const struct flags dt_posflag_1[] =
1772 : : {
1773 : : { DF_P1_LAZYLOAD, "LAZYLOAD" },
1774 : : { DF_P1_GROUPPERM, "GROUPPERM" }
1775 : : };
1776 : : static const int ndt_posflag_1 = (sizeof (dt_posflag_1)
1777 : : / sizeof (dt_posflag_1[0]));
1778 : :
1779 : :
1780 : : static void
1781 : 74 : print_flags (int class, GElf_Xword d_val, const struct flags *flags,
1782 : : int nflags)
1783 : : {
1784 : 74 : bool first = true;
1785 : 74 : int cnt;
1786 : :
1787 [ + + ]: 1104 : for (cnt = 0; cnt < nflags; ++cnt)
1788 [ + + ]: 1030 : if (d_val & flags[cnt].mask)
1789 : : {
1790 [ + + ]: 68 : if (!first)
1791 : 36 : putchar (' ');
1792 : 68 : fputs (flags[cnt].str, stdout);
1793 : 68 : d_val &= ~flags[cnt].mask;
1794 : 68 : first = false;
1795 : : }
1796 : :
1797 [ + + ]: 74 : if (d_val != 0)
1798 : : {
1799 [ + + ]: 62 : if (!first)
1800 : 20 : putchar (' ');
1801 [ + + ]: 62 : printf ("%#0*" PRIx64, class == ELFCLASS32 ? 10 : 18, d_val);
1802 : : }
1803 : :
1804 : 74 : putchar ('\n');
1805 : 74 : }
1806 : :
1807 : :
1808 : : static void
1809 : 14 : print_dt_flags (int class, GElf_Xword d_val)
1810 : : {
1811 : 14 : print_flags (class, d_val, dt_flags, ndt_flags);
1812 : 14 : }
1813 : :
1814 : :
1815 : : static void
1816 : 56 : print_dt_flags_1 (int class, GElf_Xword d_val)
1817 : : {
1818 : 56 : print_flags (class, d_val, dt_flags_1, ndt_flags_1);
1819 : 56 : }
1820 : :
1821 : :
1822 : : static void
1823 : 2 : print_dt_feature_1 (int class, GElf_Xword d_val)
1824 : : {
1825 : 2 : print_flags (class, d_val, dt_feature_1, ndt_feature_1);
1826 : 2 : }
1827 : :
1828 : :
1829 : : static void
1830 : 2 : print_dt_posflag_1 (int class, GElf_Xword d_val)
1831 : : {
1832 : 2 : print_flags (class, d_val, dt_posflag_1, ndt_posflag_1);
1833 : 2 : }
1834 : :
1835 : :
1836 : : static size_t
1837 : 136 : get_dyn_ents (Elf_Data * dyn_data)
1838 : : {
1839 : 136 : GElf_Dyn *dyn;
1840 : 136 : GElf_Dyn dyn_mem;
1841 : 136 : size_t dyn_idx = 0;
1842 : 3414 : do
1843 : : {
1844 : 3414 : dyn = gelf_getdyn(dyn_data, dyn_idx, &dyn_mem);
1845 [ + - ]: 3414 : if (dyn != NULL)
1846 : 3414 : ++dyn_idx;
1847 : : }
1848 [ + + ]: 3414 : while (dyn != NULL && dyn->d_tag != DT_NULL);
1849 : :
1850 : 136 : return dyn_idx;
1851 : : }
1852 : :
1853 : :
1854 : : static void
1855 : 136 : handle_dynamic (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr, GElf_Phdr *phdr)
1856 : : {
1857 : 136 : int class = gelf_getclass (ebl->elf);
1858 : 136 : GElf_Shdr glink_mem;
1859 : 136 : GElf_Shdr *glink;
1860 : 136 : Elf_Data *data;
1861 : 136 : size_t cnt;
1862 : 136 : size_t shstrndx;
1863 : 136 : size_t dyn_ents;
1864 : :
1865 : : /* Get the data of the section. */
1866 [ + + + - ]: 136 : if (use_dynamic_segment && phdr != NULL)
1867 : 2 : data = elf_getdata_rawchunk(ebl->elf, phdr->p_offset,
1868 : : phdr->p_filesz, ELF_T_DYN);
1869 : : else
1870 : 134 : data = elf_getdata (scn, NULL);
1871 : :
1872 [ - + ]: 136 : if (data == NULL)
1873 : 0 : return;
1874 : :
1875 : : /* Get the dynamic section entry number */
1876 : 136 : dyn_ents = get_dyn_ents (data);
1877 : :
1878 [ + + + - ]: 136 : if (!use_dynamic_segment && shdr != NULL)
1879 : : {
1880 : : /* Get the section header string table index. */
1881 [ - + ]: 134 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
1882 : 0 : error_exit (0, _("cannot get section header string table index"));
1883 : :
1884 : 134 : glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link), &glink_mem);
1885 [ - + ]: 134 : if (glink == NULL)
1886 : 0 : error_exit (0, _("invalid sh_link value in section %zu"),
1887 : : elf_ndxscn (scn));
1888 : :
1889 : 134 : printf (ngettext ("\
1890 : : \nDynamic segment contains %lu entry:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'\n",
1891 : : "\
1892 : : \nDynamic segment contains %lu entries:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'\n",
1893 : : dyn_ents),
1894 : : (unsigned long int) dyn_ents,
1895 : : class == ELFCLASS32 ? 10 : 18, shdr->sh_addr,
1896 : : shdr->sh_offset,
1897 [ + + ]: 134 : (int) shdr->sh_link,
1898 : 134 : elf_strptr (ebl->elf, shstrndx, glink->sh_name));
1899 : : }
1900 [ + - ]: 2 : else if (phdr != NULL)
1901 : : {
1902 [ + - ]: 4 : printf (ngettext ("\
1903 : : \nDynamic segment contains %lu entry:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 "\n",
1904 : : "\
1905 : : \nDynamic segment contains %lu entries:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 "\n",
1906 : : dyn_ents),
1907 : : (unsigned long int) dyn_ents,
1908 : : class == ELFCLASS32 ? 10 : 18, phdr->p_paddr,
1909 : : phdr->p_offset);
1910 : : }
1911 : :
1912 : 136 : fputs (_(" Type Value\n"), stdout);
1913 : :
1914 : : /* if --use-dynamic option is enabled,
1915 : : use the string table to get the related library info. */
1916 : 136 : Elf_Data *strtab_data = NULL;
1917 [ + + + - ]: 136 : if (use_dynamic_segment && phdr != NULL)
1918 : : {
1919 : 2 : strtab_data = get_dynscn_strtab(ebl->elf, phdr);
1920 [ - + ]: 2 : if (strtab_data == NULL)
1921 : 0 : error_exit (0, _("cannot get string table by using dynamic segment"));
1922 : : }
1923 : :
1924 [ + + ]: 3550 : for (cnt = 0; cnt < dyn_ents; ++cnt)
1925 : : {
1926 : 3414 : GElf_Dyn dynmem;
1927 : 3414 : GElf_Dyn *dyn = gelf_getdyn (data, cnt, &dynmem);
1928 [ + - ]: 3414 : if (dyn == NULL)
1929 : : break;
1930 : :
1931 : 3414 : char buf[64];
1932 : 3414 : printf (" %-17s ",
1933 : : ebl_dynamic_tag_name (ebl, dyn->d_tag, buf, sizeof (buf)));
1934 : :
1935 : 3414 : char *name = NULL;
1936 : 3414 : if (dyn->d_tag == DT_NEEDED
1937 [ + + ]: 3414 : || dyn->d_tag == DT_SONAME
1938 [ + + ]: 3182 : || dyn->d_tag == DT_RPATH
1939 [ + + ]: 3180 : || dyn->d_tag == DT_RUNPATH)
1940 : : {
1941 [ + + + - ]: 236 : if (! use_dynamic_segment && shdr != NULL)
1942 : 232 : name = elf_strptr (ebl->elf, shdr->sh_link, dyn->d_un.d_val);
1943 [ - + ]: 4 : else if (dyn->d_un.d_val < strtab_data->d_size
1944 : 4 : && memrchr (strtab_data->d_buf + dyn->d_un.d_val, '\0',
1945 [ - + ]: 4 : strtab_data->d_size - 1 - dyn->d_un.d_val) != NULL)
1946 : 3414 : name = ((char *) strtab_data->d_buf) + dyn->d_un.d_val;
1947 : : }
1948 : :
1949 [ + + + + : 3414 : switch (dyn->d_tag)
+ + + + +
+ + + + ]
1950 : : {
1951 : 268 : case DT_NULL:
1952 : : case DT_DEBUG:
1953 : : case DT_BIND_NOW:
1954 : : case DT_TEXTREL:
1955 : : /* No further output. */
1956 : 268 : fputc ('\n', stdout);
1957 : 268 : break;
1958 : :
1959 : 226 : case DT_NEEDED:
1960 : 226 : printf (_("Shared library: [%s]\n"), name);
1961 : : break;
1962 : :
1963 : 6 : case DT_SONAME:
1964 : 6 : printf (_("Library soname: [%s]\n"), name);
1965 : : break;
1966 : :
1967 : 2 : case DT_RPATH:
1968 : 2 : printf (_("Library rpath: [%s]\n"), name);
1969 : : break;
1970 : :
1971 : 2 : case DT_RUNPATH:
1972 : 2 : printf (_("Library runpath: [%s]\n"), name);
1973 : : break;
1974 : :
1975 : 902 : case DT_PLTRELSZ:
1976 : : case DT_RELASZ:
1977 : : case DT_STRSZ:
1978 : : case DT_RELSZ:
1979 : : case DT_RELRSZ:
1980 : : case DT_RELAENT:
1981 : : case DT_SYMENT:
1982 : : case DT_RELENT:
1983 : : case DT_RELRENT:
1984 : : case DT_PLTPADSZ:
1985 : : case DT_MOVEENT:
1986 : : case DT_MOVESZ:
1987 : : case DT_INIT_ARRAYSZ:
1988 : : case DT_FINI_ARRAYSZ:
1989 : : case DT_SYMINSZ:
1990 : : case DT_SYMINENT:
1991 : : case DT_GNU_CONFLICTSZ:
1992 : : case DT_GNU_LIBLISTSZ:
1993 : 902 : printf (_("%" PRId64 " (bytes)\n"), dyn->d_un.d_val);
1994 : : break;
1995 : :
1996 : 212 : case DT_VERDEFNUM:
1997 : : case DT_VERNEEDNUM:
1998 : : case DT_RELACOUNT:
1999 : : case DT_RELCOUNT:
2000 : 212 : printf ("%" PRId64 "\n", dyn->d_un.d_val);
2001 : : break;
2002 : :
2003 : 108 : case DT_PLTREL:;
2004 : 108 : const char *tagname = ebl_dynamic_tag_name (ebl, dyn->d_un.d_val,
2005 : : NULL, 0);
2006 [ + + ]: 108 : puts (tagname ?: "???");
2007 : 108 : break;
2008 : :
2009 : 14 : case DT_FLAGS:
2010 : 14 : print_dt_flags (class, dyn->d_un.d_val);
2011 : : break;
2012 : :
2013 : 56 : case DT_FLAGS_1:
2014 : 56 : print_dt_flags_1 (class, dyn->d_un.d_val);
2015 : : break;
2016 : :
2017 : 2 : case DT_FEATURE_1:
2018 : 2 : print_dt_feature_1 (class, dyn->d_un.d_val);
2019 : : break;
2020 : :
2021 : 2 : case DT_POSFLAG_1:
2022 : 2 : print_dt_posflag_1 (class, dyn->d_un.d_val);
2023 : : break;
2024 : :
2025 : 1614 : default:
2026 [ + + ]: 5028 : printf ("%#0*" PRIx64 "\n",
2027 : : class == ELFCLASS32 ? 10 : 18, dyn->d_un.d_val);
2028 : : break;
2029 : : }
2030 : : }
2031 : : }
2032 : :
2033 : :
2034 : : /* Print the dynamic segment. */
2035 : : static void
2036 : 190 : print_dynamic (Ebl *ebl)
2037 : : {
2038 [ + + ]: 1072 : for (size_t i = 0; i < phnum; ++i)
2039 : : {
2040 : 1060 : GElf_Phdr phdr_mem;
2041 : 1060 : GElf_Phdr *phdr = gelf_getphdr (ebl->elf, i, &phdr_mem);
2042 : :
2043 [ + - + + ]: 1060 : if (phdr != NULL && phdr->p_type == PT_DYNAMIC)
2044 : : {
2045 : 178 : Elf_Scn *scn = gelf_offscn (ebl->elf, phdr->p_offset);
2046 : 178 : GElf_Shdr shdr_mem;
2047 : 178 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
2048 [ + + ]: 178 : if ((use_dynamic_segment && phdr != NULL)
2049 [ + + + + ]: 176 : || (shdr != NULL && shdr->sh_type == SHT_DYNAMIC))
2050 : 136 : handle_dynamic (ebl, scn, shdr, phdr);
2051 : 178 : break;
2052 : : }
2053 : : }
2054 : 190 : }
2055 : :
2056 : :
2057 : : /* Print relocations. */
2058 : : static void
2059 : 194 : print_relocs (Ebl *ebl, Dwfl_Module *mod, GElf_Ehdr *ehdr)
2060 : : {
2061 : : /* Find all relocation sections and handle them. */
2062 : 194 : Elf_Scn *scn = NULL;
2063 : :
2064 [ + + ]: 530466 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
2065 : : {
2066 : : /* Handle the section if it is a symbol table. */
2067 : 530272 : GElf_Shdr shdr_mem;
2068 : 530272 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
2069 : :
2070 [ + - ]: 530272 : if (likely (shdr != NULL))
2071 : : {
2072 [ + + ]: 530272 : if (shdr->sh_type == SHT_REL)
2073 : 92 : handle_relocs_rel (ebl, ehdr, scn, shdr);
2074 [ + + ]: 530180 : else if (shdr->sh_type == SHT_RELA)
2075 : 282 : handle_relocs_rela (ebl, ehdr, scn, shdr);
2076 [ - + ]: 529898 : else if (shdr->sh_type == SHT_RELR)
2077 : 0 : handle_relocs_relr (ebl, mod, scn, shdr);
2078 : : }
2079 : : }
2080 : 194 : }
2081 : :
2082 : :
2083 : : /* Handle a relocation section. */
2084 : : static void
2085 : 92 : handle_relocs_rel (Ebl *ebl, GElf_Ehdr *ehdr, Elf_Scn *scn, GElf_Shdr *shdr)
2086 : : {
2087 : 92 : int class = gelf_getclass (ebl->elf);
2088 : 92 : size_t sh_entsize = gelf_fsize (ebl->elf, ELF_T_REL, 1, EV_CURRENT);
2089 : 92 : int nentries = shdr->sh_size / sh_entsize;
2090 : :
2091 : : /* Get the data of the section. */
2092 : 92 : Elf_Data *data = elf_getdata (scn, NULL);
2093 [ + - ]: 92 : if (data == NULL)
2094 : 0 : return;
2095 : :
2096 : : /* Get the symbol table information. */
2097 : 92 : Elf_Scn *symscn = elf_getscn (ebl->elf, shdr->sh_link);
2098 : 92 : GElf_Shdr symshdr_mem;
2099 : 92 : GElf_Shdr *symshdr = gelf_getshdr (symscn, &symshdr_mem);
2100 : 92 : Elf_Data *symdata = elf_getdata (symscn, NULL);
2101 : :
2102 : : /* Get the section header of the section the relocations are for. */
2103 : 92 : GElf_Shdr destshdr_mem;
2104 : 92 : GElf_Shdr *destshdr = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_info),
2105 : : &destshdr_mem);
2106 : :
2107 [ + - - + ]: 92 : if (unlikely (symshdr == NULL || symdata == NULL || destshdr == NULL))
2108 : : {
2109 : 0 : printf (_("\nInvalid symbol table at offset %#0" PRIx64 "\n"),
2110 : : shdr->sh_offset);
2111 : 0 : return;
2112 : : }
2113 : :
2114 : : /* Search for the optional extended section index table if there are
2115 : : more than 64k sections. */
2116 : 92 : Elf_Data *xndxdata = NULL;
2117 [ - + ]: 92 : int xndxscnidx = shnum >= SHN_LORESERVE ? elf_scnshndx (symscn) : 0;
2118 [ - + ]: 92 : if (unlikely (xndxscnidx > 0))
2119 : 0 : xndxdata = elf_getdata (elf_getscn (ebl->elf, xndxscnidx), NULL);
2120 : :
2121 : : /* Get the section header string table index. */
2122 : 92 : size_t shstrndx;
2123 [ - + ]: 92 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
2124 : 0 : error_exit (0, _("cannot get section header string table index"));
2125 : :
2126 [ + + ]: 92 : if (shdr->sh_info != 0)
2127 : 76 : printf (ngettext ("\
2128 : : \nRelocation section [%2zu] '%s' for section [%2u] '%s' at offset %#0" PRIx64 " contains %d entry:\n",
2129 : : "\
2130 : : \nRelocation section [%2zu] '%s' for section [%2u] '%s' at offset %#0" PRIx64 " contains %d entries:\n",
2131 : : nentries),
2132 : : elf_ndxscn (scn),
2133 : 76 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
2134 : 76 : (unsigned int) shdr->sh_info,
2135 : 76 : elf_strptr (ebl->elf, shstrndx, destshdr->sh_name),
2136 : : shdr->sh_offset,
2137 : : nentries);
2138 : : else
2139 : : /* The .rel.dyn section does not refer to a specific section but
2140 : : instead of section index zero. Do not try to print a section
2141 : : name. */
2142 : 16 : printf (ngettext ("\
2143 : : \nRelocation section [%2u] '%s' at offset %#0" PRIx64 " contains %d entry:\n",
2144 : : "\
2145 : : \nRelocation section [%2u] '%s' at offset %#0" PRIx64 " contains %d entries:\n",
2146 : : nentries),
2147 : 16 : (unsigned int) elf_ndxscn (scn),
2148 : 16 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
2149 : : shdr->sh_offset,
2150 : : nentries);
2151 [ + - ]: 92 : fputs (class == ELFCLASS32
2152 : 92 : ? _("\
2153 : : Offset Type Value Name\n")
2154 : 0 : : _("\
2155 : : Offset Type Value Name\n"),
2156 : : stdout);
2157 : :
2158 : 92 : int is_statically_linked = 0;
2159 [ + + ]: 25942 : for (int cnt = 0; cnt < nentries; ++cnt)
2160 : : {
2161 : 25850 : GElf_Rel relmem;
2162 : 25850 : GElf_Rel *rel = gelf_getrel (data, cnt, &relmem);
2163 [ + - ]: 25850 : if (likely (rel != NULL))
2164 : : {
2165 : 25850 : char buf[128];
2166 : 25850 : GElf_Sym symmem;
2167 : 25850 : Elf32_Word xndx;
2168 : 51700 : GElf_Sym *sym = gelf_getsymshndx (symdata, xndxdata,
2169 : 25850 : GELF_R_SYM (rel->r_info),
2170 : : &symmem, &xndx);
2171 [ - + ]: 25850 : if (unlikely (sym == NULL))
2172 : : {
2173 : : /* As a special case we have to handle relocations in static
2174 : : executables. This only happens for IRELATIVE relocations
2175 : : (so far). There is no symbol table. */
2176 [ # # ]: 0 : if (is_statically_linked == 0)
2177 : : {
2178 : : /* Find the program header and look for a PT_INTERP entry. */
2179 : 0 : is_statically_linked = -1;
2180 [ # # ]: 0 : if (ehdr->e_type == ET_EXEC)
2181 : : {
2182 : 0 : is_statically_linked = 1;
2183 : :
2184 [ # # ]: 0 : for (size_t inner = 0; inner < phnum; ++inner)
2185 : : {
2186 : 0 : GElf_Phdr phdr_mem;
2187 : 0 : GElf_Phdr *phdr = gelf_getphdr (ebl->elf, inner,
2188 : : &phdr_mem);
2189 [ # # # # ]: 0 : if (phdr != NULL && phdr->p_type == PT_INTERP)
2190 : : {
2191 : 0 : is_statically_linked = -1;
2192 : 0 : break;
2193 : : }
2194 : : }
2195 : : }
2196 : : }
2197 : :
2198 [ # # # # ]: 0 : if (is_statically_linked > 0 && shdr->sh_link == 0)
2199 [ # # # # ]: 0 : printf ("\
2200 : : %#0*" PRIx64 " %-20s %*s %s\n",
2201 : : class == ELFCLASS32 ? 10 : 18, rel->r_offset,
2202 : 0 : ebl_reloc_type_check (ebl, GELF_R_TYPE (rel->r_info))
2203 : : /* Avoid the leading R_ which isn't carrying any
2204 : : information. */
2205 : 0 : ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info),
2206 : : buf, sizeof (buf)) + 2
2207 : 0 : : _("<INVALID RELOC>"),
2208 : : class == ELFCLASS32 ? 10 : 18, "",
2209 : 0 : elf_strptr (ebl->elf, shstrndx, destshdr->sh_name));
2210 : : else
2211 [ # # # # ]: 0 : printf (" %#0*" PRIx64 " %-20s <%s %ld>\n",
2212 : : class == ELFCLASS32 ? 10 : 18, rel->r_offset,
2213 : 0 : ebl_reloc_type_check (ebl, GELF_R_TYPE (rel->r_info))
2214 : : /* Avoid the leading R_ which isn't carrying any
2215 : : information. */
2216 : 0 : ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info),
2217 : : buf, sizeof (buf)) + 2
2218 : 0 : : _("<INVALID RELOC>"),
2219 : : _("INVALID SYMBOL"),
2220 : 0 : (long int) GELF_R_SYM (rel->r_info));
2221 : : }
2222 [ + + ]: 25850 : else if (GELF_ST_TYPE (sym->st_info) != STT_SECTION
2223 [ + + ]: 130 : && !(GELF_ST_TYPE (sym->st_info) == STT_NOTYPE
2224 [ - + ]: 66 : && GELF_ST_BIND (sym->st_info) == STB_LOCAL
2225 [ # # ]: 0 : && sym->st_shndx != SHN_UNDEF
2226 [ # # ]: 0 : && sym->st_value == 0)) // local start section label
2227 [ - + ]: 26110 : printf (" %#0*" PRIx64 " %-20s %#0*" PRIx64 " %s\n",
2228 : : class == ELFCLASS32 ? 10 : 18, rel->r_offset,
2229 [ + - ]: 130 : likely (ebl_reloc_type_check (ebl,
2230 : : GELF_R_TYPE (rel->r_info)))
2231 : : /* Avoid the leading R_ which isn't carrying any
2232 : : information. */
2233 : 130 : ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info),
2234 : : buf, sizeof (buf)) + 2
2235 : 0 : : _("<INVALID RELOC>"),
2236 : : class == ELFCLASS32 ? 10 : 18, sym->st_value,
2237 : 130 : elf_strptr (ebl->elf, symshdr->sh_link, sym->st_name));
2238 : : else
2239 : : {
2240 : : /* This is a relocation against a STT_SECTION symbol
2241 : : or a local start section label for which we print
2242 : : section name. */
2243 : 25720 : GElf_Shdr secshdr_mem;
2244 : 25720 : GElf_Shdr *secshdr;
2245 : 25720 : secshdr = gelf_getshdr (elf_getscn (ebl->elf,
2246 [ + - ]: 25720 : sym->st_shndx == SHN_XINDEX
2247 : 0 : ? xndx : sym->st_shndx),
2248 : : &secshdr_mem);
2249 : :
2250 [ - + ]: 25720 : if (unlikely (secshdr == NULL))
2251 [ # # # # ]: 0 : printf (" %#0*" PRIx64 " %-20s <%s %ld>\n",
2252 : : class == ELFCLASS32 ? 10 : 18, rel->r_offset,
2253 : 0 : ebl_reloc_type_check (ebl, GELF_R_TYPE (rel->r_info))
2254 : : /* Avoid the leading R_ which isn't carrying any
2255 : : information. */
2256 : 0 : ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info),
2257 : : buf, sizeof (buf)) + 2
2258 : 0 : : _("<INVALID RELOC>"),
2259 : : _("INVALID SECTION"),
2260 [ # # ]: 0 : (long int) (sym->st_shndx == SHN_XINDEX
2261 : 0 : ? xndx : sym->st_shndx));
2262 : : else
2263 [ - + + - ]: 77160 : printf (" %#0*" PRIx64 " %-20s %#0*" PRIx64 " %s\n",
2264 : : class == ELFCLASS32 ? 10 : 18, rel->r_offset,
2265 : 25720 : ebl_reloc_type_check (ebl, GELF_R_TYPE (rel->r_info))
2266 : : /* Avoid the leading R_ which isn't carrying any
2267 : : information. */
2268 : 25720 : ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info),
2269 : : buf, sizeof (buf)) + 2
2270 : 0 : : _("<INVALID RELOC>"),
2271 : : class == ELFCLASS32 ? 10 : 18, sym->st_value,
2272 : 25720 : elf_strptr (ebl->elf, shstrndx, secshdr->sh_name));
2273 : : }
2274 : : }
2275 : : }
2276 : : }
2277 : :
2278 : :
2279 : : /* Handle a relocation section. */
2280 : : static void
2281 : 282 : handle_relocs_rela (Ebl *ebl, GElf_Ehdr *ehdr, Elf_Scn *scn, GElf_Shdr *shdr)
2282 : : {
2283 : 282 : int class = gelf_getclass (ebl->elf);
2284 : 282 : size_t sh_entsize = gelf_fsize (ebl->elf, ELF_T_RELA, 1, EV_CURRENT);
2285 : 282 : int nentries = shdr->sh_size / sh_entsize;
2286 : :
2287 : : /* Get the data of the section. */
2288 : 282 : Elf_Data *data = elf_getdata (scn, NULL);
2289 [ + - ]: 282 : if (data == NULL)
2290 : 0 : return;
2291 : :
2292 : : /* Get the symbol table information. */
2293 : 282 : Elf_Scn *symscn = elf_getscn (ebl->elf, shdr->sh_link);
2294 : 282 : GElf_Shdr symshdr_mem;
2295 : 282 : GElf_Shdr *symshdr = gelf_getshdr (symscn, &symshdr_mem);
2296 : 282 : Elf_Data *symdata = elf_getdata (symscn, NULL);
2297 : :
2298 : : /* Get the section header of the section the relocations are for. */
2299 : 282 : GElf_Shdr destshdr_mem;
2300 : 282 : GElf_Shdr *destshdr = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_info),
2301 : : &destshdr_mem);
2302 : :
2303 [ + - - + ]: 282 : if (unlikely (symshdr == NULL || symdata == NULL || destshdr == NULL))
2304 : : {
2305 : 0 : printf (_("\nInvalid symbol table at offset %#0" PRIx64 "\n"),
2306 : : shdr->sh_offset);
2307 : 0 : return;
2308 : : }
2309 : :
2310 : : /* Search for the optional extended section index table if there are
2311 : : more than 64k sections. */
2312 : 282 : Elf_Data *xndxdata = NULL;
2313 [ + + ]: 282 : int xndxscnidx = shnum >= SHN_LORESERVE ? elf_scnshndx (symscn) : 0;
2314 [ + + ]: 282 : if (unlikely (xndxscnidx > 0))
2315 : 16 : xndxdata = elf_getdata (elf_getscn (ebl->elf, xndxscnidx), NULL);
2316 : :
2317 : : /* Get the section header string table index. */
2318 : 282 : size_t shstrndx;
2319 [ - + ]: 282 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
2320 : 0 : error_exit (0, _("cannot get section header string table index"));
2321 : :
2322 [ + + ]: 282 : if (shdr->sh_info != 0)
2323 : 168 : printf (ngettext ("\
2324 : : \nRelocation section [%2zu] '%s' for section [%2u] '%s' at offset %#0" PRIx64 " contains %d entry:\n",
2325 : : "\
2326 : : \nRelocation section [%2zu] '%s' for section [%2u] '%s' at offset %#0" PRIx64 " contains %d entries:\n",
2327 : : nentries),
2328 : : elf_ndxscn (scn),
2329 : 168 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
2330 : 168 : (unsigned int) shdr->sh_info,
2331 : 168 : elf_strptr (ebl->elf, shstrndx, destshdr->sh_name),
2332 : : shdr->sh_offset,
2333 : : nentries);
2334 : : else
2335 : : /* The .rela.dyn section does not refer to a specific section but
2336 : : instead of section index zero. Do not try to print a section
2337 : : name. */
2338 : 114 : printf (ngettext ("\
2339 : : \nRelocation section [%2u] '%s' at offset %#0" PRIx64 " contains %d entry:\n",
2340 : : "\
2341 : : \nRelocation section [%2u] '%s' at offset %#0" PRIx64 " contains %d entries:\n",
2342 : : nentries),
2343 : 114 : (unsigned int) elf_ndxscn (scn),
2344 : 114 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
2345 : : shdr->sh_offset,
2346 : : nentries);
2347 [ - + ]: 282 : fputs (class == ELFCLASS32
2348 : 0 : ? _("\
2349 : : Offset Type Value Addend Name\n")
2350 : 282 : : _("\
2351 : : Offset Type Value Addend Name\n"),
2352 : : stdout);
2353 : :
2354 : 282 : int is_statically_linked = 0;
2355 [ + + ]: 1165770 : for (int cnt = 0; cnt < nentries; ++cnt)
2356 : : {
2357 : 1165488 : GElf_Rela relmem;
2358 : 1165488 : GElf_Rela *rel = gelf_getrela (data, cnt, &relmem);
2359 [ + - ]: 1165488 : if (likely (rel != NULL))
2360 : : {
2361 : 1165488 : char buf[64];
2362 : 1165488 : GElf_Sym symmem;
2363 : 1165488 : Elf32_Word xndx;
2364 : 2330976 : GElf_Sym *sym = gelf_getsymshndx (symdata, xndxdata,
2365 : 1165488 : GELF_R_SYM (rel->r_info),
2366 : : &symmem, &xndx);
2367 : :
2368 [ - + ]: 1165488 : if (unlikely (sym == NULL))
2369 : : {
2370 : : /* As a special case we have to handle relocations in static
2371 : : executables. This only happens for IRELATIVE relocations
2372 : : (so far). There is no symbol table. */
2373 [ # # ]: 0 : if (is_statically_linked == 0)
2374 : : {
2375 : : /* Find the program header and look for a PT_INTERP entry. */
2376 : 0 : is_statically_linked = -1;
2377 [ # # ]: 0 : if (ehdr->e_type == ET_EXEC)
2378 : : {
2379 : 0 : is_statically_linked = 1;
2380 : :
2381 [ # # ]: 0 : for (size_t inner = 0; inner < phnum; ++inner)
2382 : : {
2383 : 0 : GElf_Phdr phdr_mem;
2384 : 0 : GElf_Phdr *phdr = gelf_getphdr (ebl->elf, inner,
2385 : : &phdr_mem);
2386 [ # # # # ]: 0 : if (phdr != NULL && phdr->p_type == PT_INTERP)
2387 : : {
2388 : 0 : is_statically_linked = -1;
2389 : 0 : break;
2390 : : }
2391 : : }
2392 : : }
2393 : : }
2394 : :
2395 [ # # # # ]: 0 : if (is_statically_linked > 0 && shdr->sh_link == 0)
2396 [ # # # # ]: 0 : printf ("\
2397 : : %#0*" PRIx64 " %-15s %*s %#6" PRIx64 " %s\n",
2398 : : class == ELFCLASS32 ? 10 : 18, rel->r_offset,
2399 : 0 : ebl_reloc_type_check (ebl, GELF_R_TYPE (rel->r_info))
2400 : : /* Avoid the leading R_ which isn't carrying any
2401 : : information. */
2402 : 0 : ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info),
2403 : : buf, sizeof (buf)) + 2
2404 : 0 : : _("<INVALID RELOC>"),
2405 : : class == ELFCLASS32 ? 10 : 18, "",
2406 : : rel->r_addend,
2407 : 0 : elf_strptr (ebl->elf, shstrndx, destshdr->sh_name));
2408 : : else
2409 [ # # # # ]: 0 : printf (" %#0*" PRIx64 " %-15s <%s %ld>\n",
2410 : : class == ELFCLASS32 ? 10 : 18, rel->r_offset,
2411 : 0 : ebl_reloc_type_check (ebl, GELF_R_TYPE (rel->r_info))
2412 : : /* Avoid the leading R_ which isn't carrying any
2413 : : information. */
2414 : 0 : ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info),
2415 : : buf, sizeof (buf)) + 2
2416 : 0 : : _("<INVALID RELOC>"),
2417 : : _("INVALID SYMBOL"),
2418 : 0 : (long int) GELF_R_SYM (rel->r_info));
2419 : : }
2420 [ + + ]: 1165488 : else if (GELF_ST_TYPE (sym->st_info) != STT_SECTION
2421 [ + + ]: 627582 : && !(GELF_ST_TYPE (sym->st_info) == STT_NOTYPE
2422 [ + + ]: 100842 : && GELF_ST_BIND (sym->st_info) == STB_LOCAL
2423 [ + + ]: 99312 : && sym->st_shndx != SHN_UNDEF
2424 [ + + ]: 716 : && sym->st_value == 0)) // local start section label
2425 [ + - ]: 2420628 : printf ("\
2426 : : %#0*" PRIx64 " %-15s %#0*" PRIx64 " %+6" PRId64 " %s\n",
2427 : : class == ELFCLASS32 ? 10 : 18, rel->r_offset,
2428 [ + - ]: 627570 : likely (ebl_reloc_type_check (ebl,
2429 : : GELF_R_TYPE (rel->r_info)))
2430 : : /* Avoid the leading R_ which isn't carrying any
2431 : : information. */
2432 : 627570 : ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info),
2433 : : buf, sizeof (buf)) + 2
2434 : 0 : : _("<INVALID RELOC>"),
2435 : : class == ELFCLASS32 ? 10 : 18, sym->st_value,
2436 : : rel->r_addend,
2437 : 627570 : elf_strptr (ebl->elf, symshdr->sh_link, sym->st_name));
2438 : : else
2439 : : {
2440 : : /* This is a relocation against a STT_SECTION symbol
2441 : : or a local start section label for which we print
2442 : : section name. */
2443 : 537918 : GElf_Shdr secshdr_mem;
2444 : 537918 : GElf_Shdr *secshdr;
2445 : 537918 : secshdr = gelf_getshdr (elf_getscn (ebl->elf,
2446 [ + + ]: 537918 : sym->st_shndx == SHN_XINDEX
2447 : 2088 : ? xndx : sym->st_shndx),
2448 : : &secshdr_mem);
2449 : :
2450 [ - + ]: 537918 : if (unlikely (secshdr == NULL))
2451 [ # # # # ]: 0 : printf (" %#0*" PRIx64 " %-15s <%s %ld>\n",
2452 : : class == ELFCLASS32 ? 10 : 18, rel->r_offset,
2453 : 0 : ebl_reloc_type_check (ebl, GELF_R_TYPE (rel->r_info))
2454 : : /* Avoid the leading R_ which isn't carrying any
2455 : : information. */
2456 : 0 : ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info),
2457 : : buf, sizeof (buf)) + 2
2458 : 0 : : _("<INVALID RELOC>"),
2459 : : _("INVALID SECTION"),
2460 [ # # ]: 0 : (long int) (sym->st_shndx == SHN_XINDEX
2461 : 0 : ? xndx : sym->st_shndx));
2462 : : else
2463 [ + - + - ]: 1613754 : printf ("\
2464 : : %#0*" PRIx64 " %-15s %#0*" PRIx64 " %+6" PRId64 " %s\n",
2465 : : class == ELFCLASS32 ? 10 : 18, rel->r_offset,
2466 : 537918 : ebl_reloc_type_check (ebl, GELF_R_TYPE (rel->r_info))
2467 : : /* Avoid the leading R_ which isn't carrying any
2468 : : information. */
2469 : 537918 : ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info),
2470 : : buf, sizeof (buf)) + 2
2471 : 0 : : _("<INVALID RELOC>"),
2472 : : class == ELFCLASS32 ? 10 : 18, sym->st_value,
2473 : : rel->r_addend,
2474 : 537918 : elf_strptr (ebl->elf, shstrndx, secshdr->sh_name));
2475 : : }
2476 : : }
2477 : : }
2478 : : }
2479 : :
2480 : : /* Handle a relocation section. */
2481 : : static void
2482 : 0 : handle_relocs_relr (Ebl *ebl, Dwfl_Module *mod, Elf_Scn *scn, GElf_Shdr *shdr)
2483 : : {
2484 : 0 : int class = gelf_getclass (ebl->elf);
2485 : 0 : size_t sh_entsize = gelf_fsize (ebl->elf, ELF_T_RELR, 1, EV_CURRENT);
2486 : 0 : int nentries = shdr->sh_size / sh_entsize;
2487 : :
2488 : : /* Get the data of the section. */
2489 : 0 : Elf_Data *data = elf_getdata (scn, NULL);
2490 [ # # ]: 0 : if (data == NULL)
2491 : 0 : return;
2492 : :
2493 : : /* Get the section header string table index. */
2494 : 0 : size_t shstrndx;
2495 [ # # ]: 0 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
2496 : 0 : error_exit (0, _("cannot get section header string table index"));
2497 : :
2498 : : /* A .relr.dyn section does not refer to a specific section. */
2499 : 0 : printf (ngettext ("\
2500 : : \nRelocation section [%2u] '%s' at offset %#0" PRIx64 " contains %d entry:\n",
2501 : : "\
2502 : : \nRelocation section [%2u] '%s' at offset %#0" PRIx64 " contains %d entries:\n",
2503 : : nentries),
2504 : 0 : (unsigned int) elf_ndxscn (scn),
2505 : 0 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
2506 : : shdr->sh_offset,
2507 : : nentries);
2508 : :
2509 [ # # ]: 0 : if (class == ELFCLASS32)
2510 : : {
2511 : : uint32_t base = 0;
2512 [ # # ]: 0 : for (int cnt = 0; cnt < nentries; ++cnt)
2513 : : {
2514 : 0 : Elf32_Word *words = data->d_buf;
2515 : 0 : Elf32_Word entry = words[cnt];
2516 : :
2517 : : /* Just the raw entries? */
2518 [ # # ]: 0 : if (print_unresolved_addresses)
2519 : 0 : printf (" %#010" PRIx32 "%s\n", entry,
2520 [ # # ]: 0 : (entry & 1) == 0 ? " *" : "");
2521 : : else
2522 : : {
2523 : : /* A real address, also sets base. */
2524 [ # # ]: 0 : if ((entry & 1) == 0)
2525 : : {
2526 : 0 : printf (" ");
2527 : 0 : print_dwarf_addr (mod, 4, entry, entry);
2528 : 0 : printf (" *\n");
2529 : :
2530 : 0 : base = entry + 4;
2531 : : }
2532 : : else
2533 : : {
2534 : : /* Untangle address from base and bits. */
2535 : : uint32_t addr;
2536 [ # # ]: 0 : for (addr = base; (entry >>= 1) != 0; addr += 4)
2537 [ # # ]: 0 : if ((entry & 1) != 0)
2538 : : {
2539 : 0 : printf (" ");
2540 : 0 : print_dwarf_addr (mod, 4, addr, addr);
2541 : 0 : printf ("\n");
2542 : : }
2543 : 0 : base += 4 * (4 * 8 - 1);
2544 : : }
2545 : : }
2546 : : }
2547 : : }
2548 : : else
2549 : : {
2550 : : uint64_t base = 0;
2551 [ # # ]: 0 : for (int cnt = 0; cnt < nentries; ++cnt)
2552 : : {
2553 : 0 : Elf64_Xword *xwords = data->d_buf;
2554 : 0 : Elf64_Xword entry = xwords[cnt];
2555 : :
2556 : : /* Just the raw entries? */
2557 [ # # ]: 0 : if (print_unresolved_addresses)
2558 : 0 : printf (" %#018" PRIx64 "%s\n", entry,
2559 [ # # ]: 0 : (entry & 1) == 0 ? " *" : "");
2560 : : else
2561 : : {
2562 : : /* A real address, also sets base. */
2563 [ # # ]: 0 : if ((entry & 1) == 0)
2564 : : {
2565 : 0 : printf (" ");
2566 : 0 : print_dwarf_addr (mod, 8, entry, entry);
2567 : 0 : printf (" *\n");
2568 : :
2569 : 0 : base = entry + 8;
2570 : : }
2571 : : else
2572 : : {
2573 : : /* Untangle address from base and bits. */
2574 : : uint64_t addr;
2575 [ # # ]: 0 : for (addr = base; (entry >>= 1) != 0; addr += 8)
2576 [ # # ]: 0 : if ((entry & 1) != 0)
2577 : : {
2578 : 0 : printf (" ");
2579 : 0 : print_dwarf_addr (mod, 8, addr, addr);
2580 : 0 : printf ("\n");
2581 : : }
2582 : 0 : base += 8 * (8 * 8 - 1);
2583 : : }
2584 : : }
2585 : : }
2586 : : }
2587 : : }
2588 : :
2589 : : /* Print the program header. Return true if a symtab is printed,
2590 : : false otherwise. */
2591 : : static bool
2592 : 438 : print_symtab (Ebl *ebl, int type)
2593 : : {
2594 : : /* Use the dynamic section info to display symbol tables. */
2595 [ - + - - ]: 438 : if (use_dynamic_segment && type == SHT_DYNSYM)
2596 : 0 : return handle_dynamic_symtab(ebl);
2597 : :
2598 : : /* Find the symbol table(s). For this we have to search through the
2599 : : section table. */
2600 : : Elf_Scn *scn = NULL;
2601 : : bool symtab_printed = false;
2602 : :
2603 [ + + ]: 14056 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
2604 : : {
2605 : : /* Handle the section if it is a symbol table. */
2606 : 13618 : GElf_Shdr shdr_mem;
2607 : 13618 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
2608 : :
2609 [ + - + + ]: 13618 : if (shdr != NULL && shdr->sh_type == (GElf_Word) type)
2610 : : {
2611 [ + + ]: 274 : if (symbol_table_section != NULL)
2612 : : {
2613 : : /* Get the section header string table index. */
2614 : 14 : size_t shstrndx;
2615 : 14 : const char *sname;
2616 [ - + ]: 14 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
2617 : 0 : error_exit (0,
2618 : : _("cannot get section header string table index"));
2619 : 14 : sname = elf_strptr (ebl->elf, shstrndx, shdr->sh_name);
2620 [ + - + + ]: 14 : if (sname == NULL || strcmp (sname, symbol_table_section) != 0)
2621 : 8 : continue;
2622 : : }
2623 : :
2624 [ - + ]: 266 : if ((shdr->sh_flags & SHF_COMPRESSED) != 0)
2625 : : {
2626 [ # # ]: 0 : if (elf_compress (scn, 0, 0) < 0)
2627 : 0 : printf ("WARNING: %s [%zd]\n",
2628 : : _("Couldn't uncompress section"),
2629 : : elf_ndxscn (scn));
2630 : 0 : shdr = gelf_getshdr (scn, &shdr_mem);
2631 [ # # ]: 0 : if (unlikely (shdr == NULL))
2632 : 0 : error_exit (0,
2633 : : _("cannot get section [%zd] header: %s"),
2634 : : elf_ndxscn (scn), elf_errmsg (-1));
2635 : : }
2636 : 266 : symtab_printed = handle_symtab (ebl, scn, shdr);
2637 : : }
2638 : : }
2639 : :
2640 : : return symtab_printed;
2641 : : }
2642 : :
2643 : :
2644 : : static void
2645 : 266 : process_symtab (Ebl *ebl, unsigned int nsyms, Elf64_Word idx,
2646 : : Elf32_Word verneed_stridx, Elf32_Word verdef_stridx,
2647 : : Elf_Data *symdata, Elf_Data *versym_data,
2648 : : Elf_Data *symstr_data, Elf_Data *verneed_data,
2649 : : Elf_Data *verdef_data, Elf_Data *xndx_data)
2650 : : {
2651 [ + + ]: 77014 : for (unsigned int cnt = 0; cnt < nsyms; ++cnt)
2652 : : {
2653 : 76748 : char typebuf[64];
2654 : 76748 : char bindbuf[64];
2655 : 76748 : char scnbuf[64];
2656 : 76748 : const char *sym_name;
2657 : 76748 : Elf32_Word xndx;
2658 : 76748 : GElf_Sym sym_mem;
2659 : 76748 : GElf_Sym *sym
2660 : 76748 : = gelf_getsymshndx (symdata, xndx_data, cnt, &sym_mem, &xndx);
2661 : :
2662 [ - + ]: 76748 : if (unlikely (sym == NULL))
2663 : 0 : continue;
2664 : :
2665 : : /* Determine the real section index. */
2666 [ + - ]: 76748 : if (likely (sym->st_shndx != SHN_XINDEX))
2667 : 76748 : xndx = sym->st_shndx;
2668 [ - + ]: 76748 : if (use_dynamic_segment == true)
2669 : : {
2670 [ # # ]: 0 : if (validate_str (symstr_data->d_buf, sym->st_name,
2671 : : symstr_data->d_size))
2672 : 0 : sym_name = (char *)symstr_data->d_buf + sym->st_name;
2673 : : else
2674 : : sym_name = NULL;
2675 : : }
2676 : : else
2677 : 76748 : sym_name = elf_strptr (ebl->elf, idx, sym->st_name);
2678 : :
2679 [ - + ]: 76748 : if (sym_name == NULL)
2680 : : sym_name = "???";
2681 : :
2682 [ + + ]: 152608 : printf (_ ("\
2683 : : %5u: %0*" PRIx64 " %6" PRId64 " %-7s %-6s %-9s %6s %s"),
2684 : 76748 : cnt, gelf_getclass (ebl->elf) == ELFCLASS32 ? 8 : 16,
2685 : : sym->st_value, sym->st_size,
2686 : 76748 : ebl_symbol_type_name (ebl, GELF_ST_TYPE (sym->st_info), typebuf,
2687 : : sizeof (typebuf)),
2688 : 76748 : ebl_symbol_binding_name (ebl, GELF_ST_BIND (sym->st_info),
2689 : : bindbuf, sizeof (bindbuf)),
2690 : 76748 : get_visibility_type (GELF_ST_VISIBILITY (sym->st_other)),
2691 : 76748 : ebl_section_name (ebl, sym->st_shndx, xndx, scnbuf,
2692 : : sizeof (scnbuf), NULL, shnum),
2693 : : sym_name);
2694 : :
2695 [ + + ]: 76748 : if (versym_data != NULL)
2696 : : {
2697 : : /* Get the version information. */
2698 : 3420 : GElf_Versym versym_mem;
2699 : 3420 : GElf_Versym *versym = gelf_getversym (versym_data, cnt, &versym_mem);
2700 : :
2701 [ + - + + ]: 3420 : if (versym != NULL && ((*versym & 0x8000) != 0 || *versym > 1))
2702 : : {
2703 : 2460 : bool is_nobits = false;
2704 : 2460 : bool check_def = xndx != SHN_UNDEF;
2705 : :
2706 [ + + - + ]: 2460 : if (xndx < SHN_LORESERVE || sym->st_shndx == SHN_XINDEX)
2707 : : {
2708 : 2440 : GElf_Shdr symshdr_mem;
2709 : 2440 : GElf_Shdr *symshdr = gelf_getshdr (
2710 : : elf_getscn (ebl->elf, xndx), &symshdr_mem);
2711 : :
2712 : 2440 : is_nobits
2713 [ + - + + ]: 4856 : = (symshdr != NULL && symshdr->sh_type == SHT_NOBITS);
2714 : : }
2715 : :
2716 [ + + ]: 2460 : if (is_nobits || !check_def)
2717 : : {
2718 : : /* We must test both. */
2719 : 2156 : GElf_Vernaux vernaux_mem;
2720 : 2156 : GElf_Vernaux *vernaux = NULL;
2721 : 2156 : size_t vn_offset = 0;
2722 : :
2723 : 2156 : GElf_Verneed verneed_mem;
2724 : 2156 : GElf_Verneed *verneed
2725 : 2156 : = gelf_getverneed (verneed_data, 0, &verneed_mem);
2726 [ + - ]: 7838 : while (verneed != NULL)
2727 : : {
2728 : 7838 : size_t vna_offset = vn_offset;
2729 : :
2730 : 7838 : vernaux = gelf_getvernaux (verneed_data,
2731 : 7838 : vna_offset += verneed->vn_aux,
2732 : : &vernaux_mem);
2733 [ + + ]: 28170 : while (vernaux != NULL && vernaux->vna_other != *versym
2734 [ + + ]: 18176 : && vernaux->vna_next != 0
2735 [ + - ]: 20332 : && (verneed_data->d_size - vna_offset
2736 [ + - ]: 12494 : >= vernaux->vna_next))
2737 : : {
2738 : : /* Update the offset. */
2739 : 12494 : vna_offset += vernaux->vna_next;
2740 : :
2741 : 12494 : vernaux = (vernaux->vna_next == 0
2742 : : ? NULL
2743 : 12494 : : gelf_getvernaux (verneed_data,
2744 : : vna_offset,
2745 : : &vernaux_mem));
2746 : : }
2747 : :
2748 : : /* Check whether we found the version. */
2749 [ + - + + ]: 7838 : if (vernaux != NULL && vernaux->vna_other == *versym)
2750 : : /* Found it. */
2751 : : break;
2752 : :
2753 [ + - ]: 5682 : if (verneed_data->d_size - vn_offset < verneed->vn_next)
2754 : : break;
2755 : :
2756 : 5682 : vn_offset += verneed->vn_next;
2757 : 5682 : verneed
2758 : : = (verneed->vn_next == 0
2759 : : ? NULL
2760 [ + - ]: 5682 : : gelf_getverneed (verneed_data, vn_offset,
2761 : : &verneed_mem));
2762 : : }
2763 : :
2764 [ + - + - ]: 2156 : if (vernaux != NULL && vernaux->vna_other == *versym)
2765 : : {
2766 : 4312 : printf ("@%s (%u)",
2767 [ - + ]: 2156 : use_dynamic_segment == true
2768 : 0 : ? (char *)symstr_data->d_buf
2769 : 0 : + vernaux->vna_name
2770 : 2156 : : elf_strptr (ebl->elf, verneed_stridx,
2771 : 2156 : vernaux->vna_name),
2772 : : (unsigned int)vernaux->vna_other);
2773 : 2156 : check_def = 0;
2774 : : }
2775 [ # # ]: 0 : else if (unlikely (!is_nobits))
2776 : 0 : error (0, 0, _ ("bad dynamic symbol"));
2777 : : else
2778 : : check_def = 1;
2779 : : }
2780 : :
2781 [ + - + - ]: 2460 : if (check_def && *versym != 0x8001)
2782 : : {
2783 : : /* We must test both. */
2784 : 304 : size_t vd_offset = 0;
2785 : :
2786 : 304 : GElf_Verdef verdef_mem;
2787 : 304 : GElf_Verdef *verdef
2788 : 304 : = gelf_getverdef (verdef_data, 0, &verdef_mem);
2789 [ + - ]: 866 : while (verdef != NULL)
2790 : : {
2791 [ + + ]: 866 : if (verdef->vd_ndx == (*versym & 0x7fff))
2792 : : /* Found the definition. */
2793 : : break;
2794 : :
2795 [ + - ]: 562 : if (verdef_data->d_size - vd_offset < verdef->vd_next)
2796 : : break;
2797 : :
2798 : 562 : vd_offset += verdef->vd_next;
2799 : 562 : verdef = (verdef->vd_next == 0
2800 : : ? NULL
2801 [ + - ]: 562 : : gelf_getverdef (verdef_data, vd_offset,
2802 : : &verdef_mem));
2803 : : }
2804 : :
2805 [ + - ]: 304 : if (verdef != NULL)
2806 : : {
2807 : 304 : GElf_Verdaux verdaux_mem;
2808 : 608 : GElf_Verdaux *verdaux = gelf_getverdaux (
2809 : 304 : verdef_data, vd_offset + verdef->vd_aux,
2810 : : &verdaux_mem);
2811 : :
2812 [ + - ]: 304 : if (verdaux != NULL)
2813 [ + - ]: 608 : printf ((*versym & 0x8000) ? "@%s" : "@@%s",
2814 [ - + ]: 304 : use_dynamic_segment == true
2815 : 0 : ? (char *)symstr_data->d_buf
2816 : 0 : + verdaux->vda_name
2817 : 304 : : elf_strptr (ebl->elf, verdef_stridx,
2818 : 304 : verdaux->vda_name));
2819 : : }
2820 : : }
2821 : : }
2822 : : }
2823 : :
2824 : 76748 : putchar ('\n');
2825 : : }
2826 : 266 : }
2827 : :
2828 : :
2829 : : static bool
2830 : 266 : handle_symtab (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr)
2831 : : {
2832 : 266 : Elf_Data *versym_data = NULL;
2833 : 266 : Elf_Data *verneed_data = NULL;
2834 : 266 : Elf_Data *verdef_data = NULL;
2835 : 266 : Elf_Data *xndx_data = NULL;
2836 : 266 : int class = gelf_getclass (ebl->elf);
2837 : 266 : Elf32_Word verneed_stridx = 0;
2838 : 266 : Elf32_Word verdef_stridx = 0;
2839 : :
2840 : : /* Get the data of the section. */
2841 : 266 : Elf_Data *data = elf_getdata (scn, NULL);
2842 [ + - ]: 266 : if (data == NULL)
2843 : : return false;
2844 : :
2845 : : /* Find out whether we have other sections we might need. */
2846 : : Elf_Scn *runscn = NULL;
2847 [ + + ]: 8784 : while ((runscn = elf_nextscn (ebl->elf, runscn)) != NULL)
2848 : : {
2849 : 8518 : GElf_Shdr runshdr_mem;
2850 : 8518 : GElf_Shdr *runshdr = gelf_getshdr (runscn, &runshdr_mem);
2851 : :
2852 [ + - ]: 8518 : if (likely (runshdr != NULL))
2853 : : {
2854 [ + + ]: 8518 : if (runshdr->sh_type == SHT_GNU_versym
2855 [ + + ]: 202 : && runshdr->sh_link == elf_ndxscn (scn))
2856 : : /* Bingo, found the version information. Now get the data. */
2857 : 150 : versym_data = elf_getdata (runscn, NULL);
2858 [ + + ]: 8368 : else if (runshdr->sh_type == SHT_GNU_verneed)
2859 : : {
2860 : : /* This is the information about the needed versions. */
2861 : 202 : verneed_data = elf_getdata (runscn, NULL);
2862 : 202 : verneed_stridx = runshdr->sh_link;
2863 : : }
2864 [ + + ]: 8166 : else if (runshdr->sh_type == SHT_GNU_verdef)
2865 : : {
2866 : : /* This is the information about the defined versions. */
2867 : 8 : verdef_data = elf_getdata (runscn, NULL);
2868 : 8 : verdef_stridx = runshdr->sh_link;
2869 : : }
2870 [ + - ]: 8158 : else if (runshdr->sh_type == SHT_SYMTAB_SHNDX
2871 [ # # ]: 0 : && runshdr->sh_link == elf_ndxscn (scn))
2872 : : /* Extended section index. */
2873 : 0 : xndx_data = elf_getdata (runscn, NULL);
2874 : : }
2875 : : }
2876 : :
2877 : : /* Get the section header string table index. */
2878 : 266 : size_t shstrndx;
2879 [ - + ]: 266 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
2880 : 0 : error_exit (0, _("cannot get section header string table index"));
2881 : :
2882 : 266 : GElf_Shdr glink_mem;
2883 : 266 : GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
2884 : : &glink_mem);
2885 [ - + ]: 266 : if (glink == NULL)
2886 : 0 : error_exit (0, _("invalid sh_link value in section %zu"),
2887 : : elf_ndxscn (scn));
2888 : :
2889 : : /* Now we can compute the number of entries in the section. */
2890 : 532 : unsigned int nsyms = data->d_size / (class == ELFCLASS32
2891 : : ? sizeof (Elf32_Sym)
2892 [ + + ]: 266 : : sizeof (Elf64_Sym));
2893 : :
2894 : 266 : printf (ngettext ("\nSymbol table [%2u] '%s' contains %u entry:\n",
2895 : : "\nSymbol table [%2u] '%s' contains %u entries:\n",
2896 : : nsyms),
2897 : 266 : (unsigned int) elf_ndxscn (scn),
2898 : 266 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name), nsyms);
2899 : 266 : printf (ngettext (" %lu local symbol String table: [%2u] '%s'\n",
2900 : : " %lu local symbols String table: [%2u] '%s'\n",
2901 : : shdr->sh_info),
2902 : 266 : (unsigned long int) shdr->sh_info,
2903 : 266 : (unsigned int) shdr->sh_link,
2904 : 266 : elf_strptr (ebl->elf, shstrndx, glink->sh_name));
2905 : :
2906 [ + + ]: 532 : fputs (class == ELFCLASS32
2907 : 32 : ? _("\
2908 : : Num: Value Size Type Bind Vis Ndx Name\n")
2909 : 234 : : _("\
2910 : : Num: Value Size Type Bind Vis Ndx Name\n"),
2911 : : stdout);
2912 : :
2913 : 266 : process_symtab(ebl, nsyms, shdr->sh_link, verneed_stridx, verdef_stridx,
2914 : : data, versym_data, NULL, verneed_data, verdef_data, xndx_data);
2915 : 266 : return true;
2916 : : }
2917 : :
2918 : :
2919 : : static bool
2920 : 0 : handle_dynamic_symtab (Ebl *ebl)
2921 : : {
2922 : 0 : GElf_Phdr phdr_mem;
2923 : 0 : GElf_Phdr *phdr = NULL;
2924 : : /* phnum is a static variable which was already fetched in function
2925 : : process_elf_file. */
2926 [ # # ]: 0 : for (size_t i = 0; i < phnum; ++i)
2927 : : {
2928 : 0 : phdr = gelf_getphdr (ebl->elf, i, &phdr_mem);
2929 [ # # # # ]: 0 : if (phdr == NULL || phdr->p_type == PT_DYNAMIC)
2930 : : break;
2931 : : }
2932 [ # # ]: 0 : if (phdr == NULL)
2933 : : return false;
2934 : :
2935 : 0 : GElf_Addr addrs[i_max] = {
2936 : : 0,
2937 : : };
2938 : 0 : GElf_Off offs[i_max] = {
2939 : : 0,
2940 : : };
2941 : 0 : get_dynscn_addrs (ebl->elf, phdr, addrs);
2942 : 0 : find_offsets (ebl->elf, 0, i_max, addrs, offs);
2943 : :
2944 : 0 : size_t syments = 0;
2945 : :
2946 : 0 : GElf_Ehdr ehdr_mem;
2947 : 0 : GElf_Ehdr *ehdr = gelf_getehdr (ebl->elf, &ehdr_mem);
2948 : :
2949 [ # # ]: 0 : if (offs[i_hash] != 0)
2950 : : {
2951 : : /* In the original format, .hash says the size of .dynsym. */
2952 : :
2953 [ # # # # : 0 : size_t entsz = SH_ENTSIZE_HASH (ehdr);
# # ]
2954 : : Elf_Data *data
2955 : 0 : = elf_getdata_rawchunk (ebl->elf, offs[i_hash] + entsz, entsz,
2956 : : (entsz == 4 ? ELF_T_WORD : ELF_T_XWORD));
2957 [ # # ]: 0 : if (data != NULL)
2958 : 0 : syments = (entsz == 4 ? *(const GElf_Word *)data->d_buf
2959 [ # # ]: 0 : : *(const GElf_Xword *)data->d_buf);
2960 : : }
2961 [ # # # # ]: 0 : if (offs[i_gnu_hash] != 0 && syments == 0)
2962 : : {
2963 : : /* In the new format, we can derive it with some work. */
2964 : :
2965 : 0 : const struct
2966 : : {
2967 : : Elf32_Word nbuckets;
2968 : : Elf32_Word symndx;
2969 : : Elf32_Word maskwords;
2970 : : Elf32_Word shift2;
2971 : : } * header;
2972 : :
2973 : 0 : Elf_Data *data = elf_getdata_rawchunk (ebl->elf, offs[i_gnu_hash],
2974 : : sizeof *header, ELF_T_WORD);
2975 [ # # ]: 0 : if (data != NULL)
2976 : : {
2977 : 0 : header = data->d_buf;
2978 : 0 : Elf32_Word nbuckets = header->nbuckets;
2979 : 0 : Elf32_Word symndx = header->symndx;
2980 : 0 : GElf_Off buckets_at
2981 : : = (offs[i_gnu_hash] + sizeof *header
2982 : 0 : + (gelf_getclass (ebl->elf) * sizeof (Elf32_Word)
2983 : 0 : * header->maskwords));
2984 : :
2985 : : // elf_getdata_rawchunk takes a size_t, make sure it
2986 : : // doesn't overflow.
2987 : : #if SIZE_MAX <= UINT32_MAX
2988 : : if (nbuckets > SIZE_MAX / sizeof (Elf32_Word))
2989 : : data = NULL;
2990 : : else
2991 : : #endif
2992 : 0 : data = elf_getdata_rawchunk (ebl->elf, buckets_at,
2993 : : nbuckets * sizeof (Elf32_Word),
2994 : : ELF_T_WORD);
2995 [ # # ]: 0 : if (data != NULL && symndx < nbuckets)
2996 : : {
2997 : 0 : const Elf32_Word *const buckets = data->d_buf;
2998 : 0 : Elf32_Word maxndx = symndx;
2999 [ # # ]: 0 : for (Elf32_Word bucket = 0; bucket < nbuckets; ++bucket)
3000 : 0 : if (buckets[bucket] > maxndx)
3001 : : maxndx = buckets[bucket];
3002 : :
3003 : 0 : GElf_Off hasharr_at
3004 : : = (buckets_at + nbuckets * sizeof (Elf32_Word));
3005 : 0 : hasharr_at += (maxndx - symndx) * sizeof (Elf32_Word);
3006 : 0 : do
3007 : : {
3008 : 0 : data = elf_getdata_rawchunk (
3009 : : ebl->elf, hasharr_at, sizeof (Elf32_Word), ELF_T_WORD);
3010 [ # # # # ]: 0 : if (data != NULL && (*(const Elf32_Word *)data->d_buf & 1u))
3011 : : {
3012 : 0 : syments = maxndx + 1;
3013 : 0 : break;
3014 : : }
3015 : 0 : ++maxndx;
3016 : 0 : hasharr_at += sizeof (Elf32_Word);
3017 : : }
3018 [ # # ]: 0 : while (data != NULL);
3019 : : }
3020 : : }
3021 : : }
3022 [ # # # # ]: 0 : if (offs[i_strtab] > offs[i_symtab] && syments == 0)
3023 : 0 : syments = ((offs[i_strtab] - offs[i_symtab])
3024 : 0 : / gelf_fsize (ebl->elf, ELF_T_SYM, 1, EV_CURRENT));
3025 : :
3026 [ # # # # : 0 : if (syments <= 0 || offs[i_strtab] == 0 || offs[i_symtab] == 0)
# # ]
3027 : : {
3028 : 0 : error_exit (0, _ ("Dynamic symbol information is not available for "
3029 : : "displaying symbols."));
3030 : : }
3031 : :
3032 : : /* All the data chunk initializaion. */
3033 : 0 : Elf_Data *symdata = NULL;
3034 : 0 : Elf_Data *symstrdata = NULL;
3035 : 0 : Elf_Data *versym_data = NULL;
3036 : 0 : Elf_Data *verdef_data = NULL;
3037 : 0 : Elf_Data *verneed_data = NULL;
3038 : :
3039 : 0 : symdata = elf_getdata_rawchunk (
3040 : : ebl->elf, offs[i_symtab],
3041 : : gelf_fsize (ebl->elf, ELF_T_SYM, syments, EV_CURRENT), ELF_T_SYM);
3042 : 0 : symstrdata = elf_getdata_rawchunk (ebl->elf, offs[i_strtab], addrs[i_strsz],
3043 : : ELF_T_BYTE);
3044 : 0 : versym_data = elf_getdata_rawchunk (
3045 : 0 : ebl->elf, offs[i_versym], syments * sizeof (Elf64_Half), ELF_T_HALF);
3046 : :
3047 : : /* Get the verneed_data without vernaux. */
3048 : 0 : verneed_data = elf_getdata_rawchunk (
3049 : 0 : ebl->elf, offs[i_verneed], addrs[i_verneednum] * sizeof (Elf64_Verneed),
3050 : : ELF_T_VNEED);
3051 : 0 : size_t vernauxnum = 0;
3052 : 0 : size_t vn_next_offset = 0;
3053 : :
3054 [ # # ]: 0 : for (size_t i = 0; i < addrs[i_verneednum]; i++)
3055 : : {
3056 : 0 : GElf_Verneed *verneed
3057 : 0 : = (GElf_Verneed *)(verneed_data->d_buf + vn_next_offset);
3058 : 0 : vernauxnum += verneed->vn_cnt;
3059 : 0 : vn_next_offset += verneed->vn_next;
3060 : : }
3061 : :
3062 : : /* Update the verneed_data to include the vernaux. */
3063 : 0 : verneed_data = elf_getdata_rawchunk (
3064 : : ebl->elf, offs[i_verneed],
3065 : 0 : (addrs[i_verneednum] + vernauxnum) * sizeof (GElf_Verneed), ELF_T_VNEED);
3066 : :
3067 : : /* Get the verdef_data without verdaux. */
3068 : 0 : verdef_data = elf_getdata_rawchunk (
3069 : 0 : ebl->elf, offs[i_verdef], addrs[i_verdefnum] * sizeof (Elf64_Verdef),
3070 : : ELF_T_VDEF);
3071 : 0 : size_t verdauxnum = 0;
3072 : 0 : size_t vd_next_offset = 0;
3073 : :
3074 [ # # ]: 0 : for (size_t i = 0; i < addrs[i_verdefnum]; i++)
3075 : : {
3076 : 0 : GElf_Verdef *verdef
3077 : 0 : = (GElf_Verdef *)(verdef_data->d_buf + vd_next_offset);
3078 : 0 : verdauxnum += verdef->vd_cnt;
3079 : 0 : vd_next_offset += verdef->vd_next;
3080 : : }
3081 : :
3082 : : /* Update the verdef_data to include the verdaux. */
3083 : 0 : verdef_data = elf_getdata_rawchunk (
3084 : : ebl->elf, offs[i_verdef],
3085 : 0 : (addrs[i_verdefnum] + verdauxnum) * sizeof (GElf_Verdef), ELF_T_VDEF);
3086 : :
3087 : 0 : unsigned int nsyms = (unsigned int)syments;
3088 : 0 : process_symtab (ebl, nsyms, 0, 0, 0, symdata, versym_data, symstrdata,
3089 : : verneed_data, verdef_data, NULL);
3090 : 0 : return true;
3091 : : }
3092 : :
3093 : :
3094 : : /* Print version information. */
3095 : : static void
3096 : 184 : print_verinfo (Ebl *ebl)
3097 : : {
3098 : : /* Find the version information sections. For this we have to
3099 : : search through the section table. */
3100 : 184 : Elf_Scn *scn = NULL;
3101 : :
3102 [ + + ]: 6004 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
3103 : : {
3104 : : /* Handle the section if it is part of the versioning handling. */
3105 : 5820 : GElf_Shdr shdr_mem;
3106 : 5820 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
3107 : :
3108 [ + - ]: 5820 : if (likely (shdr != NULL))
3109 : : {
3110 [ + + ]: 5820 : if (shdr->sh_type == SHT_GNU_verneed)
3111 : 130 : handle_verneed (ebl, scn, shdr);
3112 [ + + ]: 5690 : else if (shdr->sh_type == SHT_GNU_verdef)
3113 : 4 : handle_verdef (ebl, scn, shdr);
3114 [ + + ]: 5686 : else if (shdr->sh_type == SHT_GNU_versym)
3115 : 130 : handle_versym (ebl, scn, shdr);
3116 : : }
3117 : : }
3118 : 184 : }
3119 : :
3120 : :
3121 : : static const char *
3122 : 374 : get_ver_flags (unsigned int flags)
3123 : : {
3124 : 374 : static char buf[32];
3125 : 374 : char *endp;
3126 : :
3127 [ + + ]: 374 : if (flags == 0)
3128 : 370 : return _("none");
3129 : :
3130 [ + - ]: 4 : if (flags & VER_FLG_BASE)
3131 : 4 : endp = stpcpy (buf, "BASE ");
3132 : : else
3133 : : endp = buf;
3134 : :
3135 [ - + ]: 4 : if (flags & VER_FLG_WEAK)
3136 : : {
3137 [ # # ]: 0 : if (endp != buf)
3138 : 0 : endp = stpcpy (endp, "| ");
3139 : :
3140 : 0 : endp = stpcpy (endp, "WEAK ");
3141 : : }
3142 : :
3143 [ - + ]: 4 : if (unlikely (flags & ~(VER_FLG_BASE | VER_FLG_WEAK)))
3144 : : {
3145 : 0 : strncpy (endp, _("| <unknown>"), buf + sizeof (buf) - endp);
3146 : 0 : buf[sizeof (buf) - 1] = '\0';
3147 : : }
3148 : :
3149 : : return buf;
3150 : : }
3151 : :
3152 : :
3153 : : static void
3154 : 130 : handle_verneed (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr)
3155 : : {
3156 : 130 : int class = gelf_getclass (ebl->elf);
3157 : :
3158 : : /* Get the data of the section. */
3159 : 130 : Elf_Data *data = elf_getdata (scn, NULL);
3160 [ - + ]: 130 : if (data == NULL)
3161 : 0 : return;
3162 : :
3163 : : /* Get the section header string table index. */
3164 : 130 : size_t shstrndx;
3165 [ - + ]: 130 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
3166 : 0 : error_exit (0, _("cannot get section header string table index"));
3167 : :
3168 : 130 : GElf_Shdr glink_mem;
3169 : 130 : GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
3170 : : &glink_mem);
3171 [ - + ]: 130 : if (glink == NULL)
3172 : 0 : error_exit (0, _("invalid sh_link value in section %zu"),
3173 : : elf_ndxscn (scn));
3174 : :
3175 : 130 : printf (ngettext ("\
3176 : : \nVersion needs section [%2u] '%s' contains %d entry:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'\n",
3177 : : "\
3178 : : \nVersion needs section [%2u] '%s' contains %d entries:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'\n",
3179 : : shdr->sh_info),
3180 : 130 : (unsigned int) elf_ndxscn (scn),
3181 : 130 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name), shdr->sh_info,
3182 : : class == ELFCLASS32 ? 10 : 18, shdr->sh_addr,
3183 : : shdr->sh_offset,
3184 [ + + ]: 130 : (unsigned int) shdr->sh_link,
3185 : 130 : elf_strptr (ebl->elf, shstrndx, glink->sh_name));
3186 : :
3187 : 130 : unsigned int offset = 0;
3188 [ + - ]: 184 : for (unsigned int cnt = shdr->sh_info; cnt > 0; cnt--)
3189 : : {
3190 : : /* Get the data at the next offset. */
3191 : 184 : GElf_Verneed needmem;
3192 : 184 : GElf_Verneed *need = gelf_getverneed (data, offset, &needmem);
3193 [ + - ]: 184 : if (unlikely (need == NULL))
3194 : : break;
3195 : :
3196 : 552 : printf (_(" %#06x: Version: %hu File: %s Cnt: %hu\n"),
3197 : 184 : offset, (unsigned short int) need->vn_version,
3198 : 184 : elf_strptr (ebl->elf, shdr->sh_link, need->vn_file),
3199 : 184 : (unsigned short int) need->vn_cnt);
3200 : :
3201 : 184 : unsigned int auxoffset = offset + need->vn_aux;
3202 [ + - ]: 350 : for (unsigned int cnt2 = need->vn_cnt; cnt2 > 0; cnt2--)
3203 : : {
3204 : 350 : GElf_Vernaux auxmem;
3205 : 350 : GElf_Vernaux *aux = gelf_getvernaux (data, auxoffset, &auxmem);
3206 [ + - ]: 350 : if (unlikely (aux == NULL))
3207 : : break;
3208 : :
3209 : 350 : printf (_(" %#06x: Name: %s Flags: %s Version: %hu\n"),
3210 : : auxoffset,
3211 : 350 : elf_strptr (ebl->elf, shdr->sh_link, aux->vna_name),
3212 : 350 : get_ver_flags (aux->vna_flags),
3213 : 350 : (unsigned short int) aux->vna_other);
3214 : :
3215 [ + + ]: 350 : if (aux->vna_next == 0)
3216 : : break;
3217 : :
3218 : 166 : auxoffset += aux->vna_next;
3219 : : }
3220 : :
3221 : : /* Find the next offset. */
3222 [ + + ]: 184 : if (need->vn_next == 0)
3223 : : break;
3224 : :
3225 : 54 : offset += need->vn_next;
3226 : : }
3227 : : }
3228 : :
3229 : :
3230 : : static void
3231 : 4 : handle_verdef (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr)
3232 : : {
3233 : : /* Get the data of the section. */
3234 : 4 : Elf_Data *data = elf_getdata (scn, NULL);
3235 [ - + ]: 4 : if (data == NULL)
3236 : 0 : return;
3237 : :
3238 : : /* Get the section header string table index. */
3239 : 4 : size_t shstrndx;
3240 [ - + ]: 4 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
3241 : 0 : error_exit (0, _("cannot get section header string table index"));
3242 : :
3243 : 4 : GElf_Shdr glink_mem;
3244 : 4 : GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
3245 : : &glink_mem);
3246 [ - + ]: 4 : if (glink == NULL)
3247 : 0 : error_exit (0, _("invalid sh_link value in section %zu"),
3248 : : elf_ndxscn (scn));
3249 : :
3250 : 4 : int class = gelf_getclass (ebl->elf);
3251 : 4 : printf (ngettext ("\
3252 : : \nVersion definition section [%2u] '%s' contains %d entry:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'\n",
3253 : : "\
3254 : : \nVersion definition section [%2u] '%s' contains %d entries:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'\n",
3255 : : shdr->sh_info),
3256 : 4 : (unsigned int) elf_ndxscn (scn),
3257 : 4 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
3258 : : shdr->sh_info,
3259 : : class == ELFCLASS32 ? 10 : 18, shdr->sh_addr,
3260 : : shdr->sh_offset,
3261 [ + - ]: 4 : (unsigned int) shdr->sh_link,
3262 : 4 : elf_strptr (ebl->elf, shstrndx, glink->sh_name));
3263 : :
3264 : 4 : unsigned int offset = 0;
3265 [ + - ]: 24 : for (unsigned int cnt = shdr->sh_info; cnt > 0; cnt--)
3266 : : {
3267 : : /* Get the data at the next offset. */
3268 : 24 : GElf_Verdef defmem;
3269 : 24 : GElf_Verdef *def = gelf_getverdef (data, offset, &defmem);
3270 [ + - ]: 24 : if (unlikely (def == NULL))
3271 : : break;
3272 : :
3273 : 24 : unsigned int auxoffset = offset + def->vd_aux;
3274 : 24 : GElf_Verdaux auxmem;
3275 : 24 : GElf_Verdaux *aux = gelf_getverdaux (data, auxoffset, &auxmem);
3276 [ + - ]: 24 : if (unlikely (aux == NULL))
3277 : : break;
3278 : :
3279 : 96 : printf (_("\
3280 : : %#06x: Version: %hd Flags: %s Index: %hd Cnt: %hd Name: %s\n"),
3281 : 24 : offset, def->vd_version,
3282 : 24 : get_ver_flags (def->vd_flags),
3283 : 24 : def->vd_ndx,
3284 : 24 : def->vd_cnt,
3285 : 24 : elf_strptr (ebl->elf, shdr->sh_link, aux->vda_name));
3286 : :
3287 : 24 : auxoffset += aux->vda_next;
3288 [ + + ]: 24 : for (unsigned int cnt2 = 1; cnt2 < def->vd_cnt; ++cnt2)
3289 : : {
3290 : 16 : aux = gelf_getverdaux (data, auxoffset, &auxmem);
3291 [ + - ]: 16 : if (unlikely (aux == NULL))
3292 : : break;
3293 : :
3294 : 16 : printf (_(" %#06x: Parent %d: %s\n"),
3295 : : auxoffset, cnt2,
3296 : 16 : elf_strptr (ebl->elf, shdr->sh_link, aux->vda_name));
3297 : :
3298 [ - + ]: 16 : if (aux->vda_next == 0)
3299 : : break;
3300 : :
3301 : 0 : auxoffset += aux->vda_next;
3302 : : }
3303 : :
3304 : : /* Find the next offset. */
3305 [ + + ]: 24 : if (def->vd_next == 0)
3306 : : break;
3307 : 20 : offset += def->vd_next;
3308 : : }
3309 : : }
3310 : :
3311 : :
3312 : : static void
3313 : 130 : handle_versym (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr)
3314 : : {
3315 : 130 : int class = gelf_getclass (ebl->elf);
3316 : 130 : const char **vername;
3317 : 130 : const char **filename;
3318 : :
3319 : : /* Get the data of the section. */
3320 : 130 : Elf_Data *data = elf_getdata (scn, NULL);
3321 [ + - ]: 130 : if (data == NULL)
3322 : 0 : return;
3323 : :
3324 : : /* Get the section header string table index. */
3325 : 130 : size_t shstrndx;
3326 [ - + ]: 130 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
3327 : 0 : error_exit (0, _("cannot get section header string table index"));
3328 : :
3329 : : /* We have to find the version definition section and extract the
3330 : : version names. */
3331 : : Elf_Scn *defscn = NULL;
3332 : : Elf_Scn *needscn = NULL;
3333 : :
3334 : : Elf_Scn *verscn = NULL;
3335 [ + + ]: 4046 : while ((verscn = elf_nextscn (ebl->elf, verscn)) != NULL)
3336 : : {
3337 : 3916 : GElf_Shdr vershdr_mem;
3338 : 3916 : GElf_Shdr *vershdr = gelf_getshdr (verscn, &vershdr_mem);
3339 : :
3340 [ - + ]: 3916 : if (likely (vershdr != NULL))
3341 : : {
3342 [ + + ]: 3916 : if (vershdr->sh_type == SHT_GNU_verdef)
3343 : : defscn = verscn;
3344 [ + + ]: 3912 : else if (vershdr->sh_type == SHT_GNU_verneed)
3345 : 3916 : needscn = verscn;
3346 : : }
3347 : : }
3348 : :
3349 : 130 : size_t nvername;
3350 [ + - ]: 130 : if (defscn != NULL || needscn != NULL)
3351 : : {
3352 : : /* We have a version information (better should have). Now get
3353 : : the version names. First find the maximum version number. */
3354 : 130 : nvername = 0;
3355 [ + + ]: 130 : if (defscn != NULL)
3356 : : {
3357 : : /* Run through the version definitions and find the highest
3358 : : index. */
3359 : 4 : unsigned int offset = 0;
3360 : 4 : Elf_Data *defdata;
3361 : 4 : GElf_Shdr defshdrmem;
3362 : 4 : GElf_Shdr *defshdr;
3363 : :
3364 : 4 : defdata = elf_getdata (defscn, NULL);
3365 [ + - ]: 4 : if (unlikely (defdata == NULL))
3366 : 0 : return;
3367 : :
3368 : 4 : defshdr = gelf_getshdr (defscn, &defshdrmem);
3369 [ + - ]: 4 : if (unlikely (defshdr == NULL))
3370 : : return;
3371 : :
3372 [ + - ]: 24 : for (unsigned int cnt = 0; cnt < defshdr->sh_info; ++cnt)
3373 : : {
3374 : 24 : GElf_Verdef defmem;
3375 : 24 : GElf_Verdef *def;
3376 : :
3377 : : /* Get the data at the next offset. */
3378 : 24 : def = gelf_getverdef (defdata, offset, &defmem);
3379 [ + - ]: 24 : if (unlikely (def == NULL))
3380 : : break;
3381 : :
3382 : 24 : nvername = MAX (nvername, (size_t) (def->vd_ndx & 0x7fff));
3383 : :
3384 [ + + ]: 24 : if (def->vd_next == 0)
3385 : : break;
3386 : 20 : offset += def->vd_next;
3387 : : }
3388 : : }
3389 [ + - ]: 130 : if (needscn != NULL)
3390 : : {
3391 : 130 : unsigned int offset = 0;
3392 : 130 : Elf_Data *needdata;
3393 : 130 : GElf_Shdr needshdrmem;
3394 : 130 : GElf_Shdr *needshdr;
3395 : :
3396 : 130 : needdata = elf_getdata (needscn, NULL);
3397 [ + - ]: 130 : if (unlikely (needdata == NULL))
3398 : 0 : return;
3399 : :
3400 : 130 : needshdr = gelf_getshdr (needscn, &needshdrmem);
3401 [ + - ]: 130 : if (unlikely (needshdr == NULL))
3402 : : return;
3403 : :
3404 [ + - ]: 184 : for (unsigned int cnt = 0; cnt < needshdr->sh_info; ++cnt)
3405 : : {
3406 : 184 : GElf_Verneed needmem;
3407 : 184 : GElf_Verneed *need;
3408 : 184 : unsigned int auxoffset;
3409 : 184 : int cnt2;
3410 : :
3411 : : /* Get the data at the next offset. */
3412 : 184 : need = gelf_getverneed (needdata, offset, &needmem);
3413 [ + - ]: 184 : if (unlikely (need == NULL))
3414 : : break;
3415 : :
3416 : : /* Run through the auxiliary entries. */
3417 : 184 : auxoffset = offset + need->vn_aux;
3418 [ + - ]: 350 : for (cnt2 = need->vn_cnt; --cnt2 >= 0; )
3419 : : {
3420 : 350 : GElf_Vernaux auxmem;
3421 : 350 : GElf_Vernaux *aux;
3422 : :
3423 : 350 : aux = gelf_getvernaux (needdata, auxoffset, &auxmem);
3424 [ + - ]: 350 : if (unlikely (aux == NULL))
3425 : : break;
3426 : :
3427 : 350 : nvername = MAX (nvername,
3428 : : (size_t) (aux->vna_other & 0x7fff));
3429 : :
3430 [ + + ]: 350 : if (aux->vna_next == 0)
3431 : : break;
3432 : 166 : auxoffset += aux->vna_next;
3433 : : }
3434 : :
3435 [ + + ]: 184 : if (need->vn_next == 0)
3436 : : break;
3437 : 54 : offset += need->vn_next;
3438 : : }
3439 : : }
3440 : :
3441 : : /* This is the number of versions we know about. */
3442 : 130 : ++nvername;
3443 : :
3444 : : /* Allocate the array. */
3445 : 130 : vername = (const char **) alloca (nvername * sizeof (const char *));
3446 [ + + ]: 130 : memset(vername, 0, nvername * sizeof (const char *));
3447 : 130 : filename = (const char **) alloca (nvername * sizeof (const char *));
3448 : 130 : memset(filename, 0, nvername * sizeof (const char *));
3449 : :
3450 : : /* Run through the data structures again and collect the strings. */
3451 [ + + ]: 130 : if (defscn != NULL)
3452 : : {
3453 : : /* Run through the version definitions and find the highest
3454 : : index. */
3455 : 4 : unsigned int offset = 0;
3456 : 4 : Elf_Data *defdata;
3457 : 4 : GElf_Shdr defshdrmem;
3458 : 4 : GElf_Shdr *defshdr;
3459 : :
3460 : 4 : defdata = elf_getdata (defscn, NULL);
3461 [ + - ]: 4 : if (unlikely (defdata == NULL))
3462 : 0 : return;
3463 : :
3464 : 4 : defshdr = gelf_getshdr (defscn, &defshdrmem);
3465 [ + - ]: 4 : if (unlikely (defshdr == NULL))
3466 : : return;
3467 : :
3468 [ + - ]: 24 : for (unsigned int cnt = 0; cnt < defshdr->sh_info; ++cnt)
3469 : : {
3470 : :
3471 : : /* Get the data at the next offset. */
3472 : 24 : GElf_Verdef defmem;
3473 : 24 : GElf_Verdef *def = gelf_getverdef (defdata, offset, &defmem);
3474 [ + - ]: 24 : if (unlikely (def == NULL))
3475 : : break;
3476 : :
3477 : 24 : GElf_Verdaux auxmem;
3478 : 48 : GElf_Verdaux *aux = gelf_getverdaux (defdata,
3479 : 24 : offset + def->vd_aux,
3480 : : &auxmem);
3481 [ + - ]: 24 : if (unlikely (aux == NULL))
3482 : : break;
3483 : :
3484 : 24 : vername[def->vd_ndx & 0x7fff]
3485 : 24 : = elf_strptr (ebl->elf, defshdr->sh_link, aux->vda_name);
3486 : 24 : filename[def->vd_ndx & 0x7fff] = NULL;
3487 : :
3488 [ + + ]: 24 : if (def->vd_next == 0)
3489 : : break;
3490 : 20 : offset += def->vd_next;
3491 : : }
3492 : : }
3493 [ + - ]: 130 : if (needscn != NULL)
3494 : : {
3495 : 130 : unsigned int offset = 0;
3496 : :
3497 : 130 : Elf_Data *needdata = elf_getdata (needscn, NULL);
3498 : 130 : GElf_Shdr needshdrmem;
3499 : 130 : GElf_Shdr *needshdr = gelf_getshdr (needscn, &needshdrmem);
3500 [ - + ]: 130 : if (unlikely (needdata == NULL || needshdr == NULL))
3501 : 0 : return;
3502 : :
3503 [ + - ]: 184 : for (unsigned int cnt = 0; cnt < needshdr->sh_info; ++cnt)
3504 : : {
3505 : : /* Get the data at the next offset. */
3506 : 184 : GElf_Verneed needmem;
3507 : 184 : GElf_Verneed *need = gelf_getverneed (needdata, offset,
3508 : : &needmem);
3509 [ + - ]: 184 : if (unlikely (need == NULL))
3510 : : break;
3511 : :
3512 : : /* Run through the auxiliary entries. */
3513 : 184 : unsigned int auxoffset = offset + need->vn_aux;
3514 [ + - ]: 350 : for (int cnt2 = need->vn_cnt; --cnt2 >= 0; )
3515 : : {
3516 : 350 : GElf_Vernaux auxmem;
3517 : 350 : GElf_Vernaux *aux = gelf_getvernaux (needdata, auxoffset,
3518 : : &auxmem);
3519 [ + - ]: 350 : if (unlikely (aux == NULL))
3520 : : break;
3521 : :
3522 : 350 : vername[aux->vna_other & 0x7fff]
3523 : 350 : = elf_strptr (ebl->elf, needshdr->sh_link, aux->vna_name);
3524 : 350 : filename[aux->vna_other & 0x7fff]
3525 : 350 : = elf_strptr (ebl->elf, needshdr->sh_link, need->vn_file);
3526 : :
3527 [ + + ]: 350 : if (aux->vna_next == 0)
3528 : : break;
3529 : 166 : auxoffset += aux->vna_next;
3530 : : }
3531 : :
3532 [ + + ]: 184 : if (need->vn_next == 0)
3533 : : break;
3534 : 54 : offset += need->vn_next;
3535 : : }
3536 : : }
3537 : : }
3538 : : else
3539 : : {
3540 : : vername = NULL;
3541 : : nvername = 1;
3542 : : filename = NULL;
3543 : : }
3544 : :
3545 : 130 : GElf_Shdr glink_mem;
3546 : 130 : GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
3547 : : &glink_mem);
3548 : 130 : size_t sh_entsize = gelf_fsize (ebl->elf, ELF_T_HALF, 1, EV_CURRENT);
3549 [ - + ]: 130 : if (glink == NULL)
3550 : 0 : error_exit (0, _("invalid sh_link value in section %zu"),
3551 : : elf_ndxscn (scn));
3552 : :
3553 : : /* Print the header. */
3554 : 130 : printf (ngettext ("\
3555 : : \nVersion symbols section [%2u] '%s' contains %d entry:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'",
3556 : : "\
3557 : : \nVersion symbols section [%2u] '%s' contains %d entries:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'",
3558 : : shdr->sh_size / sh_entsize),
3559 : 130 : (unsigned int) elf_ndxscn (scn),
3560 : 130 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
3561 : 130 : (int) (shdr->sh_size / sh_entsize),
3562 : : class == ELFCLASS32 ? 10 : 18, shdr->sh_addr,
3563 : : shdr->sh_offset,
3564 [ + + ]: 130 : (unsigned int) shdr->sh_link,
3565 : 130 : elf_strptr (ebl->elf, shstrndx, glink->sh_name));
3566 : :
3567 : : /* Now we can finally look at the actual contents of this section. */
3568 [ + + ]: 3292 : for (unsigned int cnt = 0; cnt < shdr->sh_size / sh_entsize; ++cnt)
3569 : : {
3570 [ + + ]: 3162 : if (cnt % 2 == 0)
3571 : 1598 : printf ("\n %4d:", cnt);
3572 : :
3573 : 3162 : GElf_Versym symmem;
3574 : 3162 : GElf_Versym *sym = gelf_getversym (data, cnt, &symmem);
3575 [ + - ]: 3162 : if (sym == NULL)
3576 : : break;
3577 : :
3578 [ + + + ]: 3162 : switch (*sym)
3579 : : {
3580 : 238 : ssize_t n;
3581 : 238 : case 0:
3582 : 238 : fputs (_(" 0 *local* "),
3583 : : stdout);
3584 : 238 : break;
3585 : :
3586 : 502 : case 1:
3587 : 502 : fputs (_(" 1 *global* "),
3588 : : stdout);
3589 : 502 : break;
3590 : :
3591 : 2422 : default:
3592 [ + - ]: 4844 : n = printf ("%4d%c%s",
3593 [ + - ]: 2422 : *sym & 0x7fff, *sym & 0x8000 ? 'h' : ' ',
3594 : : (vername != NULL
3595 [ + - ]: 2422 : && (unsigned int) (*sym & 0x7fff) < nvername)
3596 : 2422 : ? vername[*sym & 0x7fff] : "???");
3597 [ + - ]: 2422 : if ((unsigned int) (*sym & 0x7fff) < nvername
3598 [ + - + + ]: 2422 : && filename != NULL && filename[*sym & 0x7fff] != NULL)
3599 : 2118 : n += printf ("(%s)", filename[*sym & 0x7fff]);
3600 : 3162 : printf ("%*s", MAX (0, 33 - (int) n), " ");
3601 : : break;
3602 : : }
3603 : : }
3604 : 130 : putchar ('\n');
3605 : : }
3606 : :
3607 : :
3608 : : static void
3609 : 130 : print_hash_info (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr, size_t shstrndx,
3610 : : uint_fast32_t maxlength, Elf32_Word nbucket,
3611 : : uint_fast32_t nsyms, uint32_t *lengths, const char *extrastr)
3612 : : {
3613 : 130 : uint32_t *counts = xcalloc (maxlength + 1, sizeof (uint32_t));
3614 : :
3615 [ + + ]: 550 : for (Elf32_Word cnt = 0; cnt < nbucket; ++cnt)
3616 : 420 : ++counts[lengths[cnt]];
3617 : :
3618 : 130 : GElf_Shdr glink_mem;
3619 : 130 : GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf,
3620 : 130 : shdr->sh_link),
3621 : : &glink_mem);
3622 [ - + ]: 130 : if (glink == NULL)
3623 : : {
3624 : 0 : error (0, 0, _("invalid sh_link value in section %zu"),
3625 : : elf_ndxscn (scn));
3626 : 0 : free (counts);
3627 : 0 : return;
3628 : : }
3629 : :
3630 [ + + ]: 260 : printf (ngettext ("\
3631 : : \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",
3632 : : "\
3633 : : \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",
3634 : : nbucket),
3635 : 130 : (unsigned int) elf_ndxscn (scn),
3636 : 130 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
3637 : : (int) nbucket,
3638 : 130 : gelf_getclass (ebl->elf) == ELFCLASS32 ? 10 : 18,
3639 : : shdr->sh_addr,
3640 : : shdr->sh_offset,
3641 : 130 : (unsigned int) shdr->sh_link,
3642 : 130 : elf_strptr (ebl->elf, shstrndx, glink->sh_name));
3643 : :
3644 [ + - ]: 130 : if (extrastr != NULL)
3645 : 130 : fputs (extrastr, stdout);
3646 : :
3647 [ + - ]: 130 : if (likely (nbucket > 0))
3648 : : {
3649 : 130 : uint64_t success = 0;
3650 : :
3651 : : /* xgettext:no-c-format */
3652 : 130 : fputs (_("\
3653 : : Length Number % of total Coverage\n"), stdout);
3654 : 130 : printf (_(" 0 %6" PRIu32 " %5.1f%%\n"),
3655 : 130 : counts[0], (counts[0] * 100.0) / nbucket);
3656 : :
3657 : 130 : uint64_t nzero_counts = 0;
3658 [ + + ]: 228 : for (Elf32_Word cnt = 1; cnt <= maxlength; ++cnt)
3659 : : {
3660 : 98 : nzero_counts += counts[cnt] * cnt;
3661 : 98 : printf (_("\
3662 : : %7d %6" PRIu32 " %5.1f%% %5.1f%%\n"),
3663 : 98 : (int) cnt, counts[cnt], (counts[cnt] * 100.0) / nbucket,
3664 : 98 : (nzero_counts * 100.0) / nsyms);
3665 : : }
3666 : :
3667 : : Elf32_Word acc = 0;
3668 [ + + ]: 228 : for (Elf32_Word cnt = 1; cnt <= maxlength; ++cnt)
3669 : : {
3670 : 98 : acc += cnt;
3671 : 98 : success += counts[cnt] * acc;
3672 : : }
3673 : :
3674 [ + + ]: 130 : if (nzero_counts > 0)
3675 : 192 : printf (_("\
3676 : : Average number of tests: successful lookup: %f\n\
3677 : : unsuccessful lookup: %f\n"),
3678 : 62 : (double) success / (double) nzero_counts,
3679 : 62 : (double) nzero_counts / (double) nbucket);
3680 : : }
3681 : :
3682 : 130 : free (counts);
3683 : : }
3684 : :
3685 : :
3686 : : /* This function handles the traditional System V-style hash table format. */
3687 : : static void
3688 : 0 : handle_sysv_hash (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr, size_t shstrndx)
3689 : : {
3690 : 0 : Elf_Data *data = elf_getdata (scn, NULL);
3691 [ # # ]: 0 : if (unlikely (data == NULL))
3692 : : {
3693 : 0 : error (0, 0, _("cannot get data for section %d: %s"),
3694 : 0 : (int) elf_ndxscn (scn), elf_errmsg (-1));
3695 : 0 : return;
3696 : : }
3697 : :
3698 [ # # ]: 0 : if (unlikely (data->d_size < 2 * sizeof (Elf32_Word)))
3699 : : {
3700 : 0 : invalid_data:
3701 : 0 : error (0, 0, _("invalid data in sysv.hash section %d"),
3702 : 0 : (int) elf_ndxscn (scn));
3703 : 0 : return;
3704 : : }
3705 : :
3706 : 0 : Elf32_Word nbucket = ((Elf32_Word *) data->d_buf)[0];
3707 : 0 : Elf32_Word nchain = ((Elf32_Word *) data->d_buf)[1];
3708 : :
3709 : 0 : uint64_t used_buf = (2ULL + nchain + nbucket) * sizeof (Elf32_Word);
3710 [ # # ]: 0 : if (used_buf > data->d_size)
3711 : 0 : goto invalid_data;
3712 : :
3713 : 0 : Elf32_Word *bucket = &((Elf32_Word *) data->d_buf)[2];
3714 : 0 : Elf32_Word *chain = &((Elf32_Word *) data->d_buf)[2 + nbucket];
3715 : :
3716 : 0 : uint32_t *lengths = xcalloc (nbucket, sizeof (uint32_t));
3717 : :
3718 : 0 : uint_fast32_t maxlength = 0;
3719 : 0 : uint_fast32_t nsyms = 0;
3720 [ # # ]: 0 : for (Elf32_Word cnt = 0; cnt < nbucket; ++cnt)
3721 : : {
3722 : 0 : Elf32_Word inner = bucket[cnt];
3723 : 0 : Elf32_Word chain_len = 0;
3724 [ # # ]: 0 : while (inner > 0 && inner < nchain)
3725 : : {
3726 : 0 : ++nsyms;
3727 : 0 : ++chain_len;
3728 [ # # ]: 0 : if (chain_len > nchain)
3729 : : {
3730 : 0 : error (0, 0, _("invalid chain in sysv.hash section %d"),
3731 : 0 : (int) elf_ndxscn (scn));
3732 : 0 : free (lengths);
3733 : 0 : return;
3734 : : }
3735 [ # # ]: 0 : if (maxlength < ++lengths[cnt])
3736 : 0 : ++maxlength;
3737 : :
3738 : 0 : inner = chain[inner];
3739 : : }
3740 : : }
3741 : :
3742 : 0 : print_hash_info (ebl, scn, shdr, shstrndx, maxlength, nbucket, nsyms,
3743 : : lengths, NULL);
3744 : :
3745 : 0 : free (lengths);
3746 : : }
3747 : :
3748 : :
3749 : : /* This function handles the incorrect, System V-style hash table
3750 : : format some 64-bit architectures use. */
3751 : : static void
3752 : 0 : handle_sysv_hash64 (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr, size_t shstrndx)
3753 : : {
3754 : 0 : Elf_Data *data = elf_getdata (scn, NULL);
3755 [ # # ]: 0 : if (unlikely (data == NULL))
3756 : : {
3757 : 0 : error (0, 0, _("cannot get data for section %d: %s"),
3758 : 0 : (int) elf_ndxscn (scn), elf_errmsg (-1));
3759 : 0 : return;
3760 : : }
3761 : :
3762 [ # # ]: 0 : if (unlikely (data->d_size < 2 * sizeof (Elf64_Xword)))
3763 : : {
3764 : 0 : invalid_data:
3765 : 0 : error (0, 0, _("invalid data in sysv.hash64 section %d"),
3766 : 0 : (int) elf_ndxscn (scn));
3767 : 0 : return;
3768 : : }
3769 : :
3770 : 0 : Elf64_Xword nbucket = ((Elf64_Xword *) data->d_buf)[0];
3771 : 0 : Elf64_Xword nchain = ((Elf64_Xword *) data->d_buf)[1];
3772 : :
3773 : 0 : uint64_t maxwords = data->d_size / sizeof (Elf64_Xword);
3774 [ # # ]: 0 : if (maxwords < 2
3775 [ # # ]: 0 : || maxwords - 2 < nbucket
3776 [ # # ]: 0 : || maxwords - 2 - nbucket < nchain)
3777 : 0 : goto invalid_data;
3778 : :
3779 : 0 : Elf64_Xword *bucket = &((Elf64_Xword *) data->d_buf)[2];
3780 : 0 : Elf64_Xword *chain = &((Elf64_Xword *) data->d_buf)[2 + nbucket];
3781 : :
3782 : 0 : uint32_t *lengths = xcalloc (nbucket, sizeof (uint32_t));
3783 : :
3784 : 0 : uint_fast32_t maxlength = 0;
3785 : 0 : uint_fast32_t nsyms = 0;
3786 [ # # ]: 0 : for (Elf64_Xword cnt = 0; cnt < nbucket; ++cnt)
3787 : : {
3788 : 0 : Elf64_Xword inner = bucket[cnt];
3789 : 0 : Elf64_Xword chain_len = 0;
3790 [ # # ]: 0 : while (inner > 0 && inner < nchain)
3791 : : {
3792 : 0 : ++nsyms;
3793 : 0 : ++chain_len;
3794 [ # # ]: 0 : if (chain_len > nchain)
3795 : : {
3796 : 0 : error (0, 0, _("invalid chain in sysv.hash64 section %d"),
3797 : 0 : (int) elf_ndxscn (scn));
3798 : 0 : free (lengths);
3799 : 0 : return;
3800 : : }
3801 [ # # ]: 0 : if (maxlength < ++lengths[cnt])
3802 : 0 : ++maxlength;
3803 : :
3804 : 0 : inner = chain[inner];
3805 : : }
3806 : : }
3807 : :
3808 : 0 : print_hash_info (ebl, scn, shdr, shstrndx, maxlength, nbucket, nsyms,
3809 : : lengths, NULL);
3810 : :
3811 : 0 : free (lengths);
3812 : : }
3813 : :
3814 : :
3815 : : /* This function handles the GNU-style hash table format. */
3816 : : static void
3817 : 130 : handle_gnu_hash (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr, size_t shstrndx)
3818 : : {
3819 : 130 : uint32_t *lengths = NULL;
3820 : 130 : Elf_Data *data = elf_getdata (scn, NULL);
3821 [ - + ]: 130 : if (unlikely (data == NULL))
3822 : : {
3823 : 0 : error (0, 0, _("cannot get data for section %d: %s"),
3824 : 0 : (int) elf_ndxscn (scn), elf_errmsg (-1));
3825 : 0 : return;
3826 : : }
3827 : :
3828 [ - + ]: 130 : if (unlikely (data->d_size < 4 * sizeof (Elf32_Word)))
3829 : : {
3830 : 0 : invalid_data:
3831 : 0 : free (lengths);
3832 : 0 : error (0, 0, _("invalid data in gnu.hash section %d"),
3833 : 0 : (int) elf_ndxscn (scn));
3834 : 0 : return;
3835 : : }
3836 : :
3837 : 130 : Elf32_Word nbucket = ((Elf32_Word *) data->d_buf)[0];
3838 : 130 : Elf32_Word symbias = ((Elf32_Word *) data->d_buf)[1];
3839 : :
3840 : : /* Next comes the size of the bitmap. It's measured in words for
3841 : : the architecture. It's 32 bits for 32 bit archs, and 64 bits for
3842 : : 64 bit archs. There is always a bloom filter present, so zero is
3843 : : an invalid value. */
3844 : 130 : Elf32_Word bitmask_words = ((Elf32_Word *) data->d_buf)[2];
3845 [ + + ]: 130 : if (gelf_getclass (ebl->elf) == ELFCLASS64)
3846 : 114 : bitmask_words *= 2;
3847 : :
3848 [ - + ]: 130 : if (bitmask_words == 0)
3849 : 0 : goto invalid_data;
3850 : :
3851 : 130 : Elf32_Word shift = ((Elf32_Word *) data->d_buf)[3];
3852 : :
3853 : : /* Is there still room for the sym chain?
3854 : : Use uint64_t calculation to prevent 32bit overflow. */
3855 : 130 : uint64_t used_buf = (4ULL + bitmask_words + nbucket) * sizeof (Elf32_Word);
3856 : 130 : uint32_t max_nsyms = (data->d_size - used_buf) / sizeof (Elf32_Word);
3857 [ - + ]: 130 : if (used_buf > data->d_size)
3858 : 0 : goto invalid_data;
3859 : :
3860 : 130 : lengths = xcalloc (nbucket, sizeof (uint32_t));
3861 : :
3862 : 130 : Elf32_Word *bitmask = &((Elf32_Word *) data->d_buf)[4];
3863 : 130 : Elf32_Word *bucket = &((Elf32_Word *) data->d_buf)[4 + bitmask_words];
3864 : 130 : Elf32_Word *chain = &((Elf32_Word *) data->d_buf)[4 + bitmask_words
3865 : 130 : + nbucket];
3866 : :
3867 : : /* Compute distribution of chain lengths. */
3868 : 130 : uint_fast32_t maxlength = 0;
3869 : 130 : uint_fast32_t nsyms = 0;
3870 [ + + ]: 550 : for (Elf32_Word cnt = 0; cnt < nbucket; ++cnt)
3871 [ + + ]: 420 : if (bucket[cnt] != 0)
3872 : : {
3873 : 232 : Elf32_Word inner = bucket[cnt] - symbias;
3874 : 414 : do
3875 : : {
3876 : 414 : ++nsyms;
3877 [ + + ]: 414 : if (maxlength < ++lengths[cnt])
3878 : 98 : ++maxlength;
3879 [ - + ]: 414 : if (inner >= max_nsyms)
3880 : 0 : goto invalid_data;
3881 : : }
3882 [ + + ]: 414 : while ((chain[inner++] & 1) == 0);
3883 : : }
3884 : :
3885 : : /* Count bits in bitmask. */
3886 : : uint_fast32_t nbits = 0;
3887 [ + + ]: 446 : for (Elf32_Word cnt = 0; cnt < bitmask_words; ++cnt)
3888 : : {
3889 : 316 : uint_fast32_t word = bitmask[cnt];
3890 : :
3891 : 316 : word = (word & 0x55555555) + ((word >> 1) & 0x55555555);
3892 : 316 : word = (word & 0x33333333) + ((word >> 2) & 0x33333333);
3893 : 316 : word = (word & 0x0f0f0f0f) + ((word >> 4) & 0x0f0f0f0f);
3894 : 316 : word = (word & 0x00ff00ff) + ((word >> 8) & 0x00ff00ff);
3895 : 316 : nbits += (word & 0x0000ffff) + ((word >> 16) & 0x0000ffff);
3896 : : }
3897 : :
3898 : 260 : char *str = xasprintf (_("\
3899 : : Symbol Bias: %u\n\
3900 : : Bitmask Size: %zu bytes %" PRIuFAST32 "%% bits set 2nd hash shift: %u\n"),
3901 : : (unsigned int) symbias,
3902 : : bitmask_words * sizeof (Elf32_Word),
3903 : 130 : ((nbits * 100 + 50)
3904 : 130 : / (uint_fast32_t) (bitmask_words
3905 : : * sizeof (Elf32_Word) * 8)),
3906 : : (unsigned int) shift);
3907 : :
3908 : 130 : print_hash_info (ebl, scn, shdr, shstrndx, maxlength, nbucket, nsyms,
3909 : : lengths, str);
3910 : :
3911 : 130 : free (str);
3912 : 130 : free (lengths);
3913 : : }
3914 : :
3915 : :
3916 : : /* Find the symbol table(s). For this we have to search through the
3917 : : section table. */
3918 : : static void
3919 : 184 : handle_hash (Ebl *ebl)
3920 : : {
3921 : : /* Get the section header string table index. */
3922 : 184 : size_t shstrndx;
3923 [ - + ]: 184 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
3924 : 0 : error_exit (0, _("cannot get section header string table index"));
3925 : :
3926 : : Elf_Scn *scn = NULL;
3927 [ + + ]: 6004 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
3928 : : {
3929 : : /* Handle the section if it is a symbol table. */
3930 : 5820 : GElf_Shdr shdr_mem;
3931 : 5820 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
3932 : :
3933 [ + - ]: 5820 : if (likely (shdr != NULL))
3934 : : {
3935 [ + + ]: 5820 : if ((shdr->sh_type == SHT_HASH || shdr->sh_type == SHT_GNU_HASH)
3936 [ - + ]: 130 : && (shdr->sh_flags & SHF_COMPRESSED) != 0)
3937 : : {
3938 [ # # ]: 0 : if (elf_compress (scn, 0, 0) < 0)
3939 : 0 : printf ("WARNING: %s [%zd]\n",
3940 : : _("Couldn't uncompress section"),
3941 : : elf_ndxscn (scn));
3942 : 0 : shdr = gelf_getshdr (scn, &shdr_mem);
3943 [ # # ]: 0 : if (unlikely (shdr == NULL))
3944 : 0 : error_exit (0, _("cannot get section [%zd] header: %s"),
3945 : : elf_ndxscn (scn), elf_errmsg (-1));
3946 : : }
3947 : :
3948 [ - + ]: 5820 : if (shdr->sh_type == SHT_HASH)
3949 : : {
3950 [ # # ]: 0 : if (ebl_sysvhash_entrysize (ebl) == sizeof (Elf64_Xword))
3951 : 0 : handle_sysv_hash64 (ebl, scn, shdr, shstrndx);
3952 : : else
3953 : 0 : handle_sysv_hash (ebl, scn, shdr, shstrndx);
3954 : : }
3955 [ + + ]: 5820 : else if (shdr->sh_type == SHT_GNU_HASH)
3956 : 130 : handle_gnu_hash (ebl, scn, shdr, shstrndx);
3957 : : }
3958 : : }
3959 : 184 : }
3960 : :
3961 : :
3962 : : static void
3963 : 192 : print_liblist (Ebl *ebl)
3964 : : {
3965 : : /* Find the library list sections. For this we have to search
3966 : : through the section table. */
3967 : 192 : Elf_Scn *scn = NULL;
3968 : :
3969 : : /* Get the section header string table index. */
3970 : 192 : size_t shstrndx;
3971 [ - + ]: 192 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
3972 : 0 : error_exit (0, _("cannot get section header string table index"));
3973 : :
3974 [ + + ]: 6130 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
3975 : : {
3976 : 5938 : GElf_Shdr shdr_mem;
3977 : 5938 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
3978 : :
3979 [ + - - + ]: 5938 : if (shdr != NULL && shdr->sh_type == SHT_GNU_LIBLIST)
3980 : : {
3981 : 0 : size_t sh_entsize = gelf_fsize (ebl->elf, ELF_T_LIB, 1, EV_CURRENT);
3982 : 0 : int nentries = shdr->sh_size / sh_entsize;
3983 : 0 : printf (ngettext ("\
3984 : : \nLibrary list section [%2zu] '%s' at offset %#0" PRIx64 " contains %d entry:\n",
3985 : : "\
3986 : : \nLibrary list section [%2zu] '%s' at offset %#0" PRIx64 " contains %d entries:\n",
3987 : : nentries),
3988 : : elf_ndxscn (scn),
3989 : 0 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
3990 : : shdr->sh_offset,
3991 : : nentries);
3992 : :
3993 : 0 : Elf_Data *data = elf_getdata (scn, NULL);
3994 [ # # ]: 0 : if (data == NULL)
3995 : 0 : return;
3996 : :
3997 : 0 : puts (_("\
3998 : : Library Time Stamp Checksum Version Flags"));
3999 : :
4000 [ # # ]: 0 : for (int cnt = 0; cnt < nentries; ++cnt)
4001 : : {
4002 : 0 : GElf_Lib lib_mem;
4003 : 0 : GElf_Lib *lib = gelf_getlib (data, cnt, &lib_mem);
4004 [ # # ]: 0 : if (unlikely (lib == NULL))
4005 : 0 : continue;
4006 : :
4007 : 0 : time_t t = (time_t) lib->l_time_stamp;
4008 : 0 : struct tm *tm = gmtime (&t);
4009 [ # # ]: 0 : if (unlikely (tm == NULL))
4010 : 0 : continue;
4011 : :
4012 : 0 : printf (" [%2d] %-29s %04u-%02u-%02uT%02u:%02u:%02u %08x %-7u %u\n",
4013 : 0 : cnt, elf_strptr (ebl->elf, shdr->sh_link, lib->l_name),
4014 : 0 : tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
4015 : : tm->tm_hour, tm->tm_min, tm->tm_sec,
4016 : 0 : (unsigned int) lib->l_checksum,
4017 : 0 : (unsigned int) lib->l_version,
4018 : 0 : (unsigned int) lib->l_flags);
4019 : : }
4020 : : }
4021 : : }
4022 : : }
4023 : :
4024 : : static inline size_t
4025 : 16 : left (Elf_Data *data,
4026 : : const unsigned char *p)
4027 : : {
4028 : 16 : return (const unsigned char *) data->d_buf + data->d_size - p;
4029 : : }
4030 : :
4031 : : static void
4032 : 192 : print_attributes (Ebl *ebl, const GElf_Ehdr *ehdr)
4033 : : {
4034 : : /* Find the object attributes sections. For this we have to search
4035 : : through the section table. */
4036 : 192 : Elf_Scn *scn = NULL;
4037 : :
4038 : : /* Get the section header string table index. */
4039 : 192 : size_t shstrndx;
4040 [ - + ]: 192 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
4041 : 0 : error_exit (0, _("cannot get section header string table index"));
4042 : :
4043 [ + + ]: 6130 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
4044 : : {
4045 : 5938 : GElf_Shdr shdr_mem;
4046 : 5938 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
4047 : :
4048 [ + - + + ]: 5938 : if (shdr == NULL || (shdr->sh_type != SHT_GNU_ATTRIBUTES
4049 [ + + ]: 5932 : && (shdr->sh_type != SHT_ARM_ATTRIBUTES
4050 [ - + ]: 2 : || ehdr->e_machine != EM_ARM)
4051 [ + + ]: 5930 : && (shdr->sh_type != SHT_CSKY_ATTRIBUTES
4052 [ + - ]: 2 : || ehdr->e_machine != EM_CSKY)
4053 [ - + ]: 5930 : && (shdr->sh_type != SHT_RISCV_ATTRIBUTES
4054 [ # # ]: 0 : || ehdr->e_machine != EM_RISCV)))
4055 : 5930 : continue;
4056 : :
4057 : 8 : printf (_("\
4058 : : \nObject attributes section [%2zu] '%s' of %" PRIu64
4059 : : " bytes at offset %#0" PRIx64 ":\n"),
4060 : : elf_ndxscn (scn),
4061 : 8 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
4062 : : shdr->sh_size, shdr->sh_offset);
4063 : :
4064 : 8 : Elf_Data *data = elf_rawdata (scn, NULL);
4065 [ + - + - ]: 8 : if (unlikely (data == NULL || data->d_size == 0))
4066 : 0 : return;
4067 : :
4068 : 8 : const unsigned char *p = data->d_buf;
4069 : :
4070 : : /* There is only one 'version', A. */
4071 [ + - ]: 8 : if (unlikely (*p++ != 'A'))
4072 : : return;
4073 : :
4074 : 8 : fputs (_(" Owner Size\n"), stdout);
4075 : :
4076 : : /* Loop over the sections. */
4077 [ + + ]: 16 : while (left (data, p) >= 4)
4078 : : {
4079 : : /* Section length. */
4080 : 8 : uint32_t len;
4081 [ + + ]: 8 : memcpy (&len, p, sizeof len);
4082 : :
4083 [ + + ]: 8 : if (MY_ELFDATA != ehdr->e_ident[EI_DATA])
4084 : 4 : CONVERT (len);
4085 : :
4086 [ + - ]: 8 : if (unlikely (len > left (data, p)))
4087 : : break;
4088 : :
4089 : : /* Section vendor name. */
4090 : 8 : const unsigned char *name = p + sizeof len;
4091 : 8 : p += len;
4092 : :
4093 : 8 : unsigned const char *q = memchr (name, '\0', len);
4094 [ + - ]: 8 : if (unlikely (q == NULL))
4095 : : break;
4096 : 8 : ++q;
4097 : :
4098 : 8 : printf (_(" %-13s %4" PRIu32 "\n"), name, len);
4099 : :
4100 : 16 : bool gnu_vendor = (q - name == sizeof "gnu"
4101 [ + + - + ]: 8 : && !memcmp (name, "gnu", sizeof "gnu"));
4102 : :
4103 : : /* Loop over subsections. */
4104 [ + + ]: 8 : if (shdr->sh_type != SHT_GNU_ATTRIBUTES
4105 [ + - ]: 6 : || gnu_vendor)
4106 [ + + ]: 16 : while (q < p)
4107 : : {
4108 : 8 : const unsigned char *const sub = q;
4109 : :
4110 : 8 : unsigned int subsection_tag;
4111 : 8 : get_uleb128 (subsection_tag, q, p);
4112 [ + - ]: 8 : if (unlikely (q >= p))
4113 : : break;
4114 : :
4115 : 8 : uint32_t subsection_len;
4116 [ + - ]: 8 : if (unlikely (p - sub < (ptrdiff_t) sizeof subsection_len))
4117 : : break;
4118 : :
4119 [ + + ]: 8 : memcpy (&subsection_len, q, sizeof subsection_len);
4120 : :
4121 [ + + ]: 8 : if (MY_ELFDATA != ehdr->e_ident[EI_DATA])
4122 : 4 : CONVERT (subsection_len);
4123 : :
4124 : : /* Don't overflow, ptrdiff_t might be 32bits, but signed. */
4125 [ + - + - ]: 8 : if (unlikely (subsection_len == 0
4126 : : || subsection_len >= (uint32_t) PTRDIFF_MAX
4127 : : || p - sub < (ptrdiff_t) subsection_len))
4128 : : break;
4129 : :
4130 : 8 : const unsigned char *r = q + sizeof subsection_len;
4131 : 8 : q = sub + subsection_len;
4132 : :
4133 [ - + ]: 8 : switch (subsection_tag)
4134 : : {
4135 : 0 : default:
4136 : : /* Unknown subsection, print and skip. */
4137 : 8 : printf (_(" %-4u %12" PRIu32 "\n"),
4138 : : subsection_tag, subsection_len);
4139 : : break;
4140 : :
4141 : 8 : case 1: /* Tag_File */
4142 : 8 : printf (_(" File: %11" PRIu32 "\n"),
4143 : : subsection_len);
4144 : :
4145 [ + + ]: 52 : while (r < q)
4146 : : {
4147 : 44 : unsigned int tag;
4148 : 44 : get_uleb128 (tag, r, q);
4149 [ + - ]: 44 : if (unlikely (r >= q))
4150 : : break;
4151 : :
4152 : : /* GNU style tags have either a uleb128 value,
4153 : : when lowest bit is not set, or a string
4154 : : when the lowest bit is set.
4155 : : "compatibility" (32) is special. It has
4156 : : both a string and a uleb128 value. For
4157 : : non-gnu we assume 6 till 31 only take ints.
4158 : : XXX see arm backend, do we need a separate
4159 : : hook? */
4160 : 44 : uint64_t value = 0;
4161 : 44 : const char *string = NULL;
4162 [ + - + + ]: 44 : if (tag == 32 || (tag & 1) == 0
4163 [ + - + + ]: 16 : || (! gnu_vendor && (tag > 5 && tag < 32)))
4164 : : {
4165 : : // Note r >= q check above.
4166 : 42 : get_uleb128 (value, r, q);
4167 [ + - ]: 42 : if (r > q)
4168 : : break;
4169 : : }
4170 [ + - ]: 44 : if (tag == 32
4171 [ + + ]: 44 : || ((tag & 1) != 0
4172 [ + - ]: 16 : && (gnu_vendor
4173 [ + - ]: 16 : || (! gnu_vendor && tag > 32)))
4174 [ + + + + ]: 44 : || (! gnu_vendor && tag > 3 && tag < 6))
4175 : : {
4176 : 2 : string = (const char *) r;
4177 : 2 : r = memchr (r, '\0', q - r);
4178 [ + - ]: 2 : if (r == NULL)
4179 : : break;
4180 : 2 : ++r;
4181 : : }
4182 : :
4183 : 44 : const char *tag_name = NULL;
4184 : 44 : const char *value_name = NULL;
4185 : 44 : ebl_check_object_attribute (ebl, (const char *) name,
4186 : : tag, value,
4187 : : &tag_name, &value_name);
4188 : :
4189 [ + - ]: 44 : if (tag_name != NULL)
4190 : : {
4191 [ - + ]: 44 : if (tag == 32)
4192 : 0 : printf (_(" %s: %" PRId64 ", %s\n"),
4193 : : tag_name, value, string);
4194 [ + + + + ]: 44 : else if (string == NULL && value_name == NULL)
4195 : 2 : printf (_(" %s: %" PRId64 "\n"),
4196 : : tag_name, value);
4197 : : else
4198 : 42 : printf (_(" %s: %s\n"),
4199 : : tag_name, string ?: value_name);
4200 : : }
4201 : : else
4202 : : {
4203 : : /* For "gnu" vendor 32 "compatibility" has
4204 : : already been handled above. */
4205 [ # # # # ]: 0 : assert (tag != 32
4206 : : || strcmp ((const char *) name, "gnu"));
4207 [ # # ]: 0 : if (string == NULL)
4208 : 0 : printf (_(" %u: %" PRId64 "\n"),
4209 : : tag, value);
4210 : : else
4211 : 44 : printf (_(" %u: %s\n"),
4212 : : tag, string);
4213 : : }
4214 : : }
4215 : : }
4216 : : }
4217 : : }
4218 : : }
4219 : : }
4220 : :
4221 : : /* Returns either the (relocated) data from the Dwarf, or tries to get
4222 : : the "raw" (uncompressed) data from the Elf section. Produces a
4223 : : warning if the data cannot be found (or decompressed). */
4224 : : static Elf_Data *
4225 : 808 : get_debug_elf_data (Dwarf *dbg, Ebl *ebl, int idx, Elf_Scn *scn)
4226 : : {
4227 : : /* We prefer to get the section data from the Dwarf because that
4228 : : might have been relocated already. Note this is subtly wrong if
4229 : : there are multiple sections with the same .debug name. */
4230 [ - + ]: 808 : if (dbg->sectiondata[idx] != NULL)
4231 : : return dbg->sectiondata[idx];
4232 : :
4233 : 0 : GElf_Shdr shdr_mem;
4234 : 0 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
4235 [ # # # # ]: 0 : if (shdr != NULL && (shdr->sh_flags & SHF_COMPRESSED) != 0)
4236 : : {
4237 [ # # ]: 0 : if (elf_compress (scn, 0, 0) < 0)
4238 : : {
4239 : 0 : error (0, 0, "%s [%zd] '%s'\n",
4240 : : _("Couldn't uncompress section"),
4241 : : elf_ndxscn (scn), section_name (ebl, shdr));
4242 : 0 : return NULL;
4243 : : }
4244 : : }
4245 : :
4246 : 0 : Elf_Data *data = elf_getdata (scn, NULL);
4247 [ # # ]: 0 : if (data == NULL)
4248 : 0 : error (0, 0, "%s [%zd] '%s': %s\n",
4249 : : _("Couldn't get data from section"),
4250 : : elf_ndxscn (scn), section_name (ebl, shdr), elf_errmsg (-1));
4251 : :
4252 : 0 : return elf_getdata (scn, NULL);
4253 : : }
4254 : :
4255 : : static void
4256 : 2155062 : print_dwarf_addr (Dwfl_Module *dwflmod,
4257 : : int address_size, Dwarf_Addr address, Dwarf_Addr raw)
4258 : : {
4259 : : /* See if there is a name we can give for this address. */
4260 : 2155062 : GElf_Sym sym;
4261 : 2155062 : GElf_Off off = 0;
4262 [ + + ]: 13048 : const char *name = (print_address_names && ! print_unresolved_addresses)
4263 : 12884 : ? dwfl_module_addrinfo (dwflmod, address, &off, &sym, NULL, NULL, NULL)
4264 [ + + ]: 2155062 : : NULL;
4265 : :
4266 : 2155062 : const char *scn;
4267 [ + + ]: 2155062 : if (print_unresolved_addresses)
4268 : : {
4269 : 168 : address = raw;
4270 : 168 : scn = NULL;
4271 : : }
4272 : : else
4273 : : {
4274 : : /* Relativize the address. */
4275 : 2154894 : int n = dwfl_module_relocations (dwflmod);
4276 [ + + ]: 2154894 : int i = n < 1 ? -1 : dwfl_module_relocate_address (dwflmod, &address);
4277 : :
4278 : : /* In an ET_REL file there is a section name to refer to. */
4279 : 2152158 : scn = (i < 0 ? NULL
4280 [ - + ]: 2152158 : : dwfl_module_relocation_info (dwflmod, i, NULL));
4281 : : }
4282 : :
4283 [ - + ]: 2155062 : if ((name != NULL
4284 : 12476 : ? (off != 0
4285 : : ? (scn != NULL
4286 : : ? (address_size == 0
4287 : 2 : ? printf ("%s+%#" PRIx64 " <%s+%#" PRIx64 ">",
4288 : : scn, address, name, off)
4289 : 8 : : printf ("%s+%#0*" PRIx64 " <%s+%#" PRIx64 ">",
4290 : 4 : scn, 2 + address_size * 2, address,
4291 : : name, off))
4292 : : : (address_size == 0
4293 : 364 : ? printf ("%#" PRIx64 " <%s+%#" PRIx64 ">",
4294 : : address, name, off)
4295 : 2156 : : printf ("%#0*" PRIx64 " <%s+%#" PRIx64 ">",
4296 : 1078 : 2 + address_size * 2, address,
4297 : : name, off)))
4298 : : : (scn != NULL
4299 : : ? (address_size == 0
4300 : 2 : ? printf ("%s+%#" PRIx64 " <%s>", scn, address, name)
4301 : 20084 : : printf ("%s+%#0*" PRIx64 " <%s>",
4302 : 10042 : scn, 2 + address_size * 2, address, name))
4303 : : : (address_size == 0
4304 : 160 : ? printf ("%#" PRIx64 " <%s>", address, name)
4305 : 1648 : : printf ("%#0*" PRIx64 " <%s>",
4306 : 824 : 2 + address_size * 2, address, name))))
4307 : : : (scn != NULL
4308 : : ? (address_size == 0
4309 : 781326 : ? printf ("%s+%#" PRIx64, scn, address)
4310 : 1360782 : : printf ("%s+%#0*" PRIx64, scn, 2 + address_size * 2, address))
4311 : : : (address_size == 0
4312 : 70 : ? printf ("%#" PRIx64, address)
4313 [ + + + + : 4309716 : : printf ("%#0*" PRIx64, 2 + address_size * 2, address)))) < 0)
+ + + + +
+ + + + +
+ + + + +
+ + + ]
4314 : 0 : error_exit (0, _("sprintf failure"));
4315 : 2155062 : }
4316 : :
4317 : :
4318 : : static const char *
4319 : 2237784 : dwarf_tag_string (unsigned int tag)
4320 : : {
4321 [ - + + - : 2237784 : switch (tag)
- - - - +
+ + + + -
- - - - -
+ - + - +
- - + + -
+ - - - -
- - - + -
+ - + + +
- - - - -
- + - - +
- - - + -
+ + + + -
- - - - +
+ + + - +
+ + + - -
- ]
4322 : : {
4323 : : #define DWARF_ONE_KNOWN_DW_TAG(NAME, CODE) case CODE: return #NAME;
4324 : 2237784 : DWARF_ALL_KNOWN_DW_TAG
4325 : : #undef DWARF_ONE_KNOWN_DW_TAG
4326 : 0 : default:
4327 : 0 : return NULL;
4328 : : }
4329 : : }
4330 : :
4331 : :
4332 : : static const char *
4333 : 8661424 : dwarf_attr_string (unsigned int attrnum)
4334 : : {
4335 [ + - + - : 8661424 : switch (attrnum)
- - - + -
- + + + -
- - - + +
- + - - +
- + - + -
- - - - -
- - - - -
- - - - -
- - - + -
+ - + - +
- - - + +
- - - + -
+ - + + -
- + + + +
- + + + -
+ - - + -
+ - + + -
+ - - + +
+ + - - -
- - + + +
+ - + - +
- - - - +
+ - + - +
+ - + + +
+ + - - +
- - - + -
+ - - - -
+ + - + -
- - - + -
- - + + -
+ - - - -
+ + - - -
- - + + -
- - - - -
- + ]
4336 : : {
4337 : : #define DWARF_ONE_KNOWN_DW_AT(NAME, CODE) case CODE: return #NAME;
4338 : 8661422 : DWARF_ALL_KNOWN_DW_AT
4339 : : #undef DWARF_ONE_KNOWN_DW_AT
4340 : 0 : default:
4341 : 0 : return NULL;
4342 : : }
4343 : : }
4344 : :
4345 : :
4346 : : static const char *
4347 : 8671662 : dwarf_form_string (unsigned int form)
4348 : : {
4349 [ + + + + : 8671662 : switch (form)
+ - - - -
- + - - +
+ + + + +
+ + + - +
+ - - + -
- + - - +
+ + + + +
- + + - -
- + - + ]
4350 : : {
4351 : : #define DWARF_ONE_KNOWN_DW_FORM(NAME, CODE) case CODE: return #NAME;
4352 : 8671658 : DWARF_ALL_KNOWN_DW_FORM
4353 : : #undef DWARF_ONE_KNOWN_DW_FORM
4354 : 0 : default:
4355 : 0 : return NULL;
4356 : : }
4357 : : }
4358 : :
4359 : :
4360 : : static const char *
4361 : 3676 : dwarf_lang_string (unsigned int lang)
4362 : : {
4363 [ - - + - : 3676 : switch (lang)
- - + - -
+ + - + -
- - - - -
- - - - -
- - + - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- + - ]
4364 : : {
4365 : : #define DWARF_ONE_KNOWN_DW_LANG(NAME, CODE) case CODE: return #NAME;
4366 : 3674 : DWARF_ALL_KNOWN_DW_LANG
4367 : : #undef DWARF_ONE_KNOWN_DW_LANG
4368 : 2 : default:
4369 : 2 : return NULL;
4370 : : }
4371 : : }
4372 : :
4373 : :
4374 : : static const char *
4375 : 7118 : dwarf_inline_string (unsigned int code)
4376 : : {
4377 : 7118 : static const char *const known[] =
4378 : : {
4379 : : #define DWARF_ONE_KNOWN_DW_INL(NAME, CODE) [CODE] = #NAME,
4380 : : DWARF_ALL_KNOWN_DW_INL
4381 : : #undef DWARF_ONE_KNOWN_DW_INL
4382 : : };
4383 : :
4384 : 7118 : if (likely (code < sizeof (known) / sizeof (known[0])))
4385 : 7118 : return known[code];
4386 : :
4387 : : return NULL;
4388 : : }
4389 : :
4390 : :
4391 : : static const char *
4392 : 56440 : dwarf_encoding_string (unsigned int code)
4393 : : {
4394 : 56440 : static const char *const known[] =
4395 : : {
4396 : : #define DWARF_ONE_KNOWN_DW_ATE(NAME, CODE) [CODE] = #NAME,
4397 : : DWARF_ALL_KNOWN_DW_ATE
4398 : : #undef DWARF_ONE_KNOWN_DW_ATE
4399 : : };
4400 : :
4401 : 56440 : if (likely (code < sizeof (known) / sizeof (known[0])))
4402 : 56440 : return known[code];
4403 : :
4404 : : return NULL;
4405 : : }
4406 : :
4407 : :
4408 : : static const char *
4409 : 0 : dwarf_access_string (unsigned int code)
4410 : : {
4411 : 0 : static const char *const known[] =
4412 : : {
4413 : : #define DWARF_ONE_KNOWN_DW_ACCESS(NAME, CODE) [CODE] = #NAME,
4414 : : DWARF_ALL_KNOWN_DW_ACCESS
4415 : : #undef DWARF_ONE_KNOWN_DW_ACCESS
4416 : : };
4417 : :
4418 : 0 : if (likely (code < sizeof (known) / sizeof (known[0])))
4419 : 0 : return known[code];
4420 : :
4421 : : return NULL;
4422 : : }
4423 : :
4424 : :
4425 : : static const char *
4426 : 0 : dwarf_defaulted_string (unsigned int code)
4427 : : {
4428 : 0 : static const char *const known[] =
4429 : : {
4430 : : #define DWARF_ONE_KNOWN_DW_DEFAULTED(NAME, CODE) [CODE] = #NAME,
4431 : : DWARF_ALL_KNOWN_DW_DEFAULTED
4432 : : #undef DWARF_ONE_KNOWN_DW_DEFAULTED
4433 : : };
4434 : :
4435 : 0 : if (likely (code < sizeof (known) / sizeof (known[0])))
4436 : 0 : return known[code];
4437 : :
4438 : : return NULL;
4439 : : }
4440 : :
4441 : :
4442 : : static const char *
4443 : 0 : dwarf_visibility_string (unsigned int code)
4444 : : {
4445 : 0 : static const char *const known[] =
4446 : : {
4447 : : #define DWARF_ONE_KNOWN_DW_VIS(NAME, CODE) [CODE] = #NAME,
4448 : : DWARF_ALL_KNOWN_DW_VIS
4449 : : #undef DWARF_ONE_KNOWN_DW_VIS
4450 : : };
4451 : :
4452 : 0 : if (likely (code < sizeof (known) / sizeof (known[0])))
4453 : 0 : return known[code];
4454 : :
4455 : : return NULL;
4456 : : }
4457 : :
4458 : :
4459 : : static const char *
4460 : 0 : dwarf_virtuality_string (unsigned int code)
4461 : : {
4462 : 0 : static const char *const known[] =
4463 : : {
4464 : : #define DWARF_ONE_KNOWN_DW_VIRTUALITY(NAME, CODE) [CODE] = #NAME,
4465 : : DWARF_ALL_KNOWN_DW_VIRTUALITY
4466 : : #undef DWARF_ONE_KNOWN_DW_VIRTUALITY
4467 : : };
4468 : :
4469 : 0 : if (likely (code < sizeof (known) / sizeof (known[0])))
4470 : 0 : return known[code];
4471 : :
4472 : : return NULL;
4473 : : }
4474 : :
4475 : :
4476 : : static const char *
4477 : 0 : dwarf_identifier_case_string (unsigned int code)
4478 : : {
4479 : 0 : static const char *const known[] =
4480 : : {
4481 : : #define DWARF_ONE_KNOWN_DW_ID(NAME, CODE) [CODE] = #NAME,
4482 : : DWARF_ALL_KNOWN_DW_ID
4483 : : #undef DWARF_ONE_KNOWN_DW_ID
4484 : : };
4485 : :
4486 : 0 : if (likely (code < sizeof (known) / sizeof (known[0])))
4487 : 0 : return known[code];
4488 : :
4489 : : return NULL;
4490 : : }
4491 : :
4492 : :
4493 : : static const char *
4494 : 0 : dwarf_calling_convention_string (unsigned int code)
4495 : : {
4496 : 0 : static const char *const known[] =
4497 : : {
4498 : : #define DWARF_ONE_KNOWN_DW_CC(NAME, CODE) [CODE] = #NAME,
4499 : : DWARF_ALL_KNOWN_DW_CC
4500 : : #undef DWARF_ONE_KNOWN_DW_CC
4501 : : };
4502 : :
4503 : 0 : if (likely (code < sizeof (known) / sizeof (known[0])))
4504 : 0 : return known[code];
4505 : :
4506 : : return NULL;
4507 : : }
4508 : :
4509 : :
4510 : : static const char *
4511 : 0 : dwarf_ordering_string (unsigned int code)
4512 : : {
4513 : 0 : static const char *const known[] =
4514 : : {
4515 : : #define DWARF_ONE_KNOWN_DW_ORD(NAME, CODE) [CODE] = #NAME,
4516 : : DWARF_ALL_KNOWN_DW_ORD
4517 : : #undef DWARF_ONE_KNOWN_DW_ORD
4518 : : };
4519 : :
4520 : 0 : if (likely (code < sizeof (known) / sizeof (known[0])))
4521 : 0 : return known[code];
4522 : :
4523 : : return NULL;
4524 : : }
4525 : :
4526 : :
4527 : : static const char *
4528 : 16 : dwarf_discr_list_string (unsigned int code)
4529 : : {
4530 : 16 : static const char *const known[] =
4531 : : {
4532 : : #define DWARF_ONE_KNOWN_DW_DSC(NAME, CODE) [CODE] = #NAME,
4533 : : DWARF_ALL_KNOWN_DW_DSC
4534 : : #undef DWARF_ONE_KNOWN_DW_DSC
4535 : : };
4536 : :
4537 : 16 : if (likely (code < sizeof (known) / sizeof (known[0])))
4538 : 16 : return known[code];
4539 : :
4540 : : return NULL;
4541 : : }
4542 : :
4543 : :
4544 : : static const char *
4545 : 1141254 : dwarf_locexpr_opcode_string (unsigned int code)
4546 : : {
4547 : 1141254 : static const char *const known[] =
4548 : : {
4549 : : /* Normally we can't afford building huge table of 64K entries,
4550 : : most of them zero, just because there are a couple defined
4551 : : values at the far end. In case of opcodes, it's OK. */
4552 : : #define DWARF_ONE_KNOWN_DW_OP(NAME, CODE) [CODE] = #NAME,
4553 : : DWARF_ALL_KNOWN_DW_OP
4554 : : #undef DWARF_ONE_KNOWN_DW_OP
4555 : : };
4556 : :
4557 : 1141254 : if (likely (code < sizeof (known) / sizeof (known[0])))
4558 : 1141254 : return known[code];
4559 : :
4560 : : return NULL;
4561 : : }
4562 : :
4563 : :
4564 : : static const char *
4565 : 3422 : dwarf_unit_string (unsigned int type)
4566 : : {
4567 [ - + - - : 3422 : switch (type)
- - + ]
4568 : : {
4569 : : #define DWARF_ONE_KNOWN_DW_UT(NAME, CODE) case CODE: return #NAME;
4570 : 14 : DWARF_ALL_KNOWN_DW_UT
4571 : : #undef DWARF_ONE_KNOWN_DW_UT
4572 : 0 : default:
4573 : 0 : return NULL;
4574 : : }
4575 : : }
4576 : :
4577 : :
4578 : : static const char *
4579 : 132086 : dwarf_range_list_encoding_string (unsigned int kind)
4580 : : {
4581 [ - + + - : 132086 : switch (kind)
+ - - -
+ ]
4582 : : {
4583 : : #define DWARF_ONE_KNOWN_DW_RLE(NAME, CODE) case CODE: return #NAME;
4584 : 123622 : DWARF_ALL_KNOWN_DW_RLE
4585 : : #undef DWARF_ONE_KNOWN_DW_RLE
4586 : 0 : default:
4587 : 0 : return NULL;
4588 : : }
4589 : : }
4590 : :
4591 : :
4592 : : static const char *
4593 : 622434 : dwarf_loc_list_encoding_string (unsigned int kind)
4594 : : {
4595 [ - - + + : 622434 : switch (kind)
- + - + -
- + ]
4596 : : {
4597 : : #define DWARF_ONE_KNOWN_DW_LLE(NAME, CODE) case CODE: return #NAME;
4598 : 604252 : DWARF_ALL_KNOWN_DW_LLE
4599 : : #undef DWARF_ONE_KNOWN_DW_LLE
4600 : : /* DW_LLE_GNU_view_pair is special/incompatible with default codes. */
4601 : 0 : case DW_LLE_GNU_view_pair: return "GNU_view_pair";
4602 : 0 : default:
4603 : 0 : return NULL;
4604 : : }
4605 : : }
4606 : :
4607 : :
4608 : : static const char *
4609 : 10238 : dwarf_line_content_description_string (unsigned int kind)
4610 : : {
4611 [ + + - - : 10238 : switch (kind)
- + ]
4612 : : {
4613 : : #define DWARF_ONE_KNOWN_DW_LNCT(NAME, CODE) case CODE: return #NAME;
4614 : 10230 : DWARF_ALL_KNOWN_DW_LNCT
4615 : : #undef DWARF_ONE_KNOWN_DW_LNCT
4616 : 0 : default:
4617 : 0 : return NULL;
4618 : : }
4619 : : }
4620 : :
4621 : :
4622 : : /* Used by all dwarf_foo_name functions. */
4623 : : static const char *
4624 : 20406300 : string_or_unknown (const char *known, unsigned int code,
4625 : : unsigned int lo_user, unsigned int hi_user,
4626 : : bool print_unknown_num)
4627 : : {
4628 : 20406300 : static char unknown_buf[20];
4629 : :
4630 [ + + ]: 20406300 : if (likely (known != NULL))
4631 : : return known;
4632 : :
4633 [ - + - - ]: 2 : if (lo_user != 0 && code >= lo_user && code <= hi_user)
4634 : : {
4635 : 0 : snprintf (unknown_buf, sizeof unknown_buf, "lo_user+%#x",
4636 : : code - lo_user);
4637 : 0 : return unknown_buf;
4638 : : }
4639 : :
4640 [ + - ]: 2 : if (print_unknown_num)
4641 : : {
4642 : 2 : snprintf (unknown_buf, sizeof unknown_buf, "??? (%#x)", code);
4643 : 2 : return unknown_buf;
4644 : : }
4645 : :
4646 : : return "???";
4647 : : }
4648 : :
4649 : :
4650 : : static const char *
4651 : 2237784 : dwarf_tag_name (unsigned int tag)
4652 : : {
4653 : 2237784 : const char *ret = dwarf_tag_string (tag);
4654 : 2237784 : return string_or_unknown (ret, tag, DW_TAG_lo_user, DW_TAG_hi_user, true);
4655 : : }
4656 : :
4657 : : static const char *
4658 : 8661424 : dwarf_attr_name (unsigned int attr)
4659 : : {
4660 : 8661424 : const char *ret = dwarf_attr_string (attr);
4661 : 8661424 : return string_or_unknown (ret, attr, DW_AT_lo_user, DW_AT_hi_user, true);
4662 : : }
4663 : :
4664 : :
4665 : : static const char *
4666 : 8671662 : dwarf_form_name (unsigned int form)
4667 : : {
4668 : 8671662 : const char *ret = dwarf_form_string (form);
4669 : 8671662 : return string_or_unknown (ret, form, 0, 0, true);
4670 : : }
4671 : :
4672 : :
4673 : : static const char *
4674 : 3672 : dwarf_lang_name (unsigned int lang)
4675 : : {
4676 : 3672 : const char *ret = dwarf_lang_string (lang);
4677 : 3672 : return string_or_unknown (ret, lang, DW_LANG_lo_user, DW_LANG_hi_user, false);
4678 : : }
4679 : :
4680 : :
4681 : : static const char *
4682 : 7118 : dwarf_inline_name (unsigned int code)
4683 : : {
4684 [ + - ]: 7118 : const char *ret = dwarf_inline_string (code);
4685 : 7118 : return string_or_unknown (ret, code, 0, 0, false);
4686 : : }
4687 : :
4688 : :
4689 : : static const char *
4690 : 56440 : dwarf_encoding_name (unsigned int code)
4691 : : {
4692 [ + - ]: 56440 : const char *ret = dwarf_encoding_string (code);
4693 : 56440 : return string_or_unknown (ret, code, DW_ATE_lo_user, DW_ATE_hi_user, false);
4694 : : }
4695 : :
4696 : :
4697 : : static const char *
4698 : 0 : dwarf_access_name (unsigned int code)
4699 : : {
4700 [ # # ]: 0 : const char *ret = dwarf_access_string (code);
4701 : 0 : return string_or_unknown (ret, code, 0, 0, false);
4702 : : }
4703 : :
4704 : :
4705 : : static const char *
4706 : 0 : dwarf_defaulted_name (unsigned int code)
4707 : : {
4708 [ # # ]: 0 : const char *ret = dwarf_defaulted_string (code);
4709 : 0 : return string_or_unknown (ret, code, 0, 0, false);
4710 : : }
4711 : :
4712 : :
4713 : : static const char *
4714 : 0 : dwarf_visibility_name (unsigned int code)
4715 : : {
4716 [ # # ]: 0 : const char *ret = dwarf_visibility_string (code);
4717 : 0 : return string_or_unknown (ret, code, 0, 0, false);
4718 : : }
4719 : :
4720 : :
4721 : : static const char *
4722 : 0 : dwarf_virtuality_name (unsigned int code)
4723 : : {
4724 [ # # ]: 0 : const char *ret = dwarf_virtuality_string (code);
4725 : 0 : return string_or_unknown (ret, code, 0, 0, false);
4726 : : }
4727 : :
4728 : :
4729 : : static const char *
4730 : 0 : dwarf_identifier_case_name (unsigned int code)
4731 : : {
4732 [ # # ]: 0 : const char *ret = dwarf_identifier_case_string (code);
4733 : 0 : return string_or_unknown (ret, code, 0, 0, false);
4734 : : }
4735 : :
4736 : :
4737 : : static const char *
4738 : 0 : dwarf_calling_convention_name (unsigned int code)
4739 : : {
4740 [ # # ]: 0 : const char *ret = dwarf_calling_convention_string (code);
4741 : 0 : return string_or_unknown (ret, code, DW_CC_lo_user, DW_CC_hi_user, false);
4742 : : }
4743 : :
4744 : :
4745 : : static const char *
4746 : 0 : dwarf_ordering_name (unsigned int code)
4747 : : {
4748 [ # # ]: 0 : const char *ret = dwarf_ordering_string (code);
4749 : 0 : return string_or_unknown (ret, code, 0, 0, false);
4750 : : }
4751 : :
4752 : :
4753 : : static const char *
4754 : 16 : dwarf_discr_list_name (unsigned int code)
4755 : : {
4756 [ + - ]: 16 : const char *ret = dwarf_discr_list_string (code);
4757 : 16 : return string_or_unknown (ret, code, 0, 0, false);
4758 : : }
4759 : :
4760 : :
4761 : : static const char *
4762 : 3422 : dwarf_unit_name (unsigned int type)
4763 : : {
4764 : 3422 : const char *ret = dwarf_unit_string (type);
4765 : 3422 : return string_or_unknown (ret, type, DW_UT_lo_user, DW_UT_hi_user, true);
4766 : : }
4767 : :
4768 : :
4769 : : static const char *
4770 : 132086 : dwarf_range_list_encoding_name (unsigned int kind)
4771 : : {
4772 : 132086 : const char *ret = dwarf_range_list_encoding_string (kind);
4773 : 132086 : return string_or_unknown (ret, kind, 0, 0, false);
4774 : : }
4775 : :
4776 : :
4777 : : static const char *
4778 : 622434 : dwarf_loc_list_encoding_name (unsigned int kind)
4779 : : {
4780 : 622434 : const char *ret = dwarf_loc_list_encoding_string (kind);
4781 : 622434 : return string_or_unknown (ret, kind, 0, 0, false);
4782 : : }
4783 : :
4784 : :
4785 : : static const char *
4786 : 10238 : dwarf_line_content_description_name (unsigned int kind)
4787 : : {
4788 : 10238 : const char *ret = dwarf_line_content_description_string (kind);
4789 : 10238 : return string_or_unknown (ret, kind, DW_LNCT_lo_user, DW_LNCT_hi_user,
4790 : : false);
4791 : : }
4792 : :
4793 : :
4794 : : static void
4795 : 510 : print_block (size_t n, const void *block)
4796 : : {
4797 [ - + ]: 510 : if (n == 0)
4798 : 0 : puts (_("empty block"));
4799 : : else
4800 : : {
4801 : 510 : printf (_("%zu byte block:"), n);
4802 : 510 : const unsigned char *data = block;
4803 : 2250 : do
4804 : 2250 : printf (" %02x", *data++);
4805 [ + + ]: 2250 : while (--n > 0);
4806 : 510 : putchar ('\n');
4807 : : }
4808 : 510 : }
4809 : :
4810 : : static void
4811 : 24 : print_bytes (size_t n, const unsigned char *bytes)
4812 : : {
4813 : 24 : while (n-- > 0)
4814 : : {
4815 : 384 : printf ("%02x", *bytes++);
4816 [ + + ]: 384 : if (n > 0)
4817 [ + + ]: 768 : printf (" ");
4818 : : }
4819 : 24 : }
4820 : :
4821 : : static int
4822 : 148 : get_indexed_addr (Dwarf_CU *cu, Dwarf_Word idx, Dwarf_Addr *addr)
4823 : : {
4824 [ - + ]: 148 : if (cu == NULL)
4825 : : return -1;
4826 : :
4827 : 148 : Elf_Data *debug_addr = cu->dbg->sectiondata[IDX_debug_addr];
4828 [ - + ]: 148 : if (debug_addr == NULL)
4829 : : return -1;
4830 : :
4831 : 148 : Dwarf_Off base = __libdw_cu_addr_base (cu);
4832 : 148 : Dwarf_Word off = idx * cu->address_size;
4833 [ - + ]: 148 : if (base > debug_addr->d_size
4834 [ - + ]: 148 : || off > debug_addr->d_size - base
4835 [ - + ]: 148 : || cu->address_size > debug_addr->d_size - base - off)
4836 : : return -1;
4837 : :
4838 : 148 : const unsigned char *addrp = debug_addr->d_buf + base + off;
4839 [ + + ]: 148 : if (cu->address_size == 4)
4840 [ - + ]: 12 : *addr = read_4ubyte_unaligned (cu->dbg, addrp);
4841 : : else
4842 [ - + ]: 136 : *addr = read_8ubyte_unaligned (cu->dbg, addrp);
4843 : :
4844 : : return 0;
4845 : : }
4846 : :
4847 : : static void
4848 : 780028 : print_ops (Dwfl_Module *dwflmod, Dwarf *dbg, int indent, int indentrest,
4849 : : unsigned int vers, unsigned int addrsize, unsigned int offset_size,
4850 : : struct Dwarf_CU *cu, Dwarf_Word len, const unsigned char *data)
4851 : : {
4852 [ + + ]: 780028 : const unsigned int ref_size = vers < 3 ? addrsize : offset_size;
4853 : :
4854 [ - + ]: 780028 : if (len == 0)
4855 : : {
4856 : 0 : printf ("%*s(empty)\n", indent, "");
4857 : 0 : return;
4858 : : }
4859 : :
4860 : : #define NEED(n) if (len < (Dwarf_Word) (n)) goto invalid
4861 : : #define CONSUME(n) NEED (n); else len -= (n)
4862 : :
4863 : : Dwarf_Word offset = 0;
4864 [ + + ]: 1921282 : while (len-- > 0)
4865 : 1141254 : {
4866 : 1141254 : uint_fast8_t op = *data++;
4867 : :
4868 [ + - ]: 1141254 : const char *op_name = dwarf_locexpr_opcode_string (op);
4869 [ - + ]: 1141254 : if (unlikely (op_name == NULL))
4870 : : {
4871 : 0 : static char buf[20];
4872 [ # # ]: 0 : if (op >= DW_OP_lo_user)
4873 : 0 : snprintf (buf, sizeof buf, "lo_user+%#x", op - DW_OP_lo_user);
4874 : : else
4875 : 0 : snprintf (buf, sizeof buf, "??? (%#x)", op);
4876 : : op_name = buf;
4877 : : }
4878 : :
4879 [ + - + + : 1141254 : switch (op)
+ + + + -
- + + - +
- - + + +
+ + - - -
- + + + ]
4880 : : {
4881 : 25942 : case DW_OP_addr:;
4882 : : /* Address operand. */
4883 : 25942 : Dwarf_Word addr;
4884 [ - + ]: 25942 : NEED (addrsize);
4885 [ + + ]: 25942 : if (addrsize == 4)
4886 [ + + ]: 108 : addr = read_4ubyte_unaligned (dbg, data);
4887 [ + - ]: 25834 : else if (addrsize == 8)
4888 [ + + ]: 25834 : addr = read_8ubyte_unaligned (dbg, data);
4889 : : else
4890 : 0 : goto invalid;
4891 : 25942 : data += addrsize;
4892 : 25942 : CONSUME (addrsize);
4893 : :
4894 : 25942 : printf ("%*s[%2" PRIuMAX "] %s ",
4895 : : indent, "", (uintmax_t) offset, op_name);
4896 : 25942 : print_dwarf_addr (dwflmod, 0, addr, addr);
4897 : 25942 : printf ("\n");
4898 : :
4899 : 25942 : offset += 1 + addrsize;
4900 : 25942 : break;
4901 : :
4902 : 0 : case DW_OP_call_ref:
4903 : : case DW_OP_GNU_variable_value:
4904 : : /* Offset operand. */
4905 [ # # ]: 0 : if (ref_size != 4 && ref_size != 8)
4906 : 0 : goto invalid; /* Cannot be used in CFA. */
4907 [ # # ]: 0 : NEED (ref_size);
4908 [ # # ]: 0 : if (ref_size == 4)
4909 [ # # ]: 0 : addr = read_4ubyte_unaligned (dbg, data);
4910 : : else
4911 [ # # ]: 0 : addr = read_8ubyte_unaligned (dbg, data);
4912 : 0 : data += ref_size;
4913 : 0 : CONSUME (ref_size);
4914 : : /* addr is a DIE offset, so format it as one. */
4915 : 0 : printf ("%*s[%2" PRIuMAX "] %s [%6" PRIxMAX "]\n",
4916 : : indent, "", (uintmax_t) offset,
4917 : : op_name, (uintmax_t) addr);
4918 : 0 : offset += 1 + ref_size;
4919 : 0 : break;
4920 : :
4921 : 27006 : case DW_OP_deref_size:
4922 : : case DW_OP_xderef_size:
4923 : : case DW_OP_pick:
4924 : : case DW_OP_const1u:
4925 : : // XXX value might be modified by relocation
4926 [ - + ]: 27006 : NEED (1);
4927 : 54012 : printf ("%*s[%2" PRIuMAX "] %s %" PRIu8 "\n",
4928 : : indent, "", (uintmax_t) offset,
4929 : 27006 : op_name, *((uint8_t *) data));
4930 : 27006 : ++data;
4931 : 27006 : --len;
4932 : 27006 : offset += 2;
4933 : 27006 : break;
4934 : :
4935 : 3242 : case DW_OP_const2u:
4936 [ - + ]: 3242 : NEED (2);
4937 : : // XXX value might be modified by relocation
4938 : 0 : printf ("%*s[%2" PRIuMAX "] %s %" PRIu16 "\n",
4939 : : indent, "", (uintmax_t) offset,
4940 [ - + ]: 3242 : op_name, read_2ubyte_unaligned (dbg, data));
4941 : 3242 : CONSUME (2);
4942 : 3242 : data += 2;
4943 : 3242 : offset += 3;
4944 : 3242 : break;
4945 : :
4946 : 2324 : case DW_OP_const4u:
4947 [ - + ]: 2324 : NEED (4);
4948 : : // XXX value might be modified by relocation
4949 : 2324 : printf ("%*s[%2" PRIuMAX "] %s %" PRIu32 "\n",
4950 : : indent, "", (uintmax_t) offset,
4951 [ - + ]: 2324 : op_name, read_4ubyte_unaligned (dbg, data));
4952 : 2324 : CONSUME (4);
4953 : 2324 : data += 4;
4954 : 2324 : offset += 5;
4955 : 2324 : break;
4956 : :
4957 : 112 : case DW_OP_const8u:
4958 [ - + ]: 112 : NEED (8);
4959 : : // XXX value might be modified by relocation
4960 : 112 : printf ("%*s[%2" PRIuMAX "] %s %" PRIu64 "\n",
4961 : : indent, "", (uintmax_t) offset,
4962 [ - + ]: 112 : op_name, (uint64_t) read_8ubyte_unaligned (dbg, data));
4963 : 112 : CONSUME (8);
4964 : 112 : data += 8;
4965 : 112 : offset += 9;
4966 : 112 : break;
4967 : :
4968 : 4900 : case DW_OP_const1s:
4969 [ - + ]: 4900 : NEED (1);
4970 : : // XXX value might be modified by relocation
4971 : 9800 : printf ("%*s[%2" PRIuMAX "] %s %" PRId8 "\n",
4972 : : indent, "", (uintmax_t) offset,
4973 : 4900 : op_name, *((int8_t *) data));
4974 : 4900 : ++data;
4975 : 4900 : --len;
4976 : 4900 : offset += 2;
4977 : 4900 : break;
4978 : :
4979 : 10 : case DW_OP_const2s:
4980 [ - + ]: 10 : NEED (2);
4981 : : // XXX value might be modified by relocation
4982 : 0 : printf ("%*s[%2" PRIuMAX "] %s %" PRId16 "\n",
4983 : : indent, "", (uintmax_t) offset,
4984 [ - + ]: 10 : op_name, read_2sbyte_unaligned (dbg, data));
4985 : 10 : CONSUME (2);
4986 : 10 : data += 2;
4987 : 10 : offset += 3;
4988 : 10 : break;
4989 : :
4990 : 0 : case DW_OP_const4s:
4991 [ # # ]: 0 : NEED (4);
4992 : : // XXX value might be modified by relocation
4993 : 0 : printf ("%*s[%2" PRIuMAX "] %s %" PRId32 "\n",
4994 : : indent, "", (uintmax_t) offset,
4995 [ # # ]: 0 : op_name, read_4sbyte_unaligned (dbg, data));
4996 : 0 : CONSUME (4);
4997 : 0 : data += 4;
4998 : 0 : offset += 5;
4999 : 0 : break;
5000 : :
5001 : 0 : case DW_OP_const8s:
5002 [ # # ]: 0 : NEED (8);
5003 : : // XXX value might be modified by relocation
5004 : 0 : printf ("%*s[%2" PRIuMAX "] %s %" PRId64 "\n",
5005 : : indent, "", (uintmax_t) offset,
5006 [ # # ]: 0 : op_name, read_8sbyte_unaligned (dbg, data));
5007 : 0 : CONSUME (8);
5008 : 0 : data += 8;
5009 : 0 : offset += 9;
5010 : 0 : break;
5011 : :
5012 : 27034 : case DW_OP_piece:
5013 : : case DW_OP_regx:
5014 : : case DW_OP_plus_uconst:
5015 : 27034 : case DW_OP_constu:;
5016 : 27034 : const unsigned char *start = data;
5017 : 27034 : uint64_t uleb;
5018 [ - + ]: 27034 : NEED (1);
5019 : 27034 : get_uleb128 (uleb, data, data + len);
5020 : 27034 : printf ("%*s[%2" PRIuMAX "] %s %" PRIu64 "\n",
5021 : : indent, "", (uintmax_t) offset, op_name, uleb);
5022 [ - + ]: 27034 : CONSUME (data - start);
5023 : 27034 : offset += 1 + (data - start);
5024 : 27034 : break;
5025 : :
5026 : 16 : case DW_OP_addrx:
5027 : : case DW_OP_GNU_addr_index:
5028 : : case DW_OP_constx:
5029 : 16 : case DW_OP_GNU_const_index:;
5030 : 16 : start = data;
5031 [ - + ]: 16 : NEED (1);
5032 : 16 : get_uleb128 (uleb, data, data + len);
5033 : 16 : printf ("%*s[%2" PRIuMAX "] %s [%" PRIu64 "] ",
5034 : : indent, "", (uintmax_t) offset, op_name, uleb);
5035 [ - + ]: 16 : CONSUME (data - start);
5036 : 16 : offset += 1 + (data - start);
5037 [ - + ]: 16 : if (get_indexed_addr (cu, uleb, &addr) != 0)
5038 : 0 : printf ("???\n");
5039 : : else
5040 : : {
5041 : 16 : print_dwarf_addr (dwflmod, 0, addr, addr);
5042 : 16 : printf ("\n");
5043 : : }
5044 : : break;
5045 : :
5046 : 0 : case DW_OP_bit_piece:
5047 : 0 : start = data;
5048 : 0 : uint64_t uleb2;
5049 [ # # ]: 0 : NEED (1);
5050 : 0 : get_uleb128 (uleb, data, data + len);
5051 : 0 : NEED (1);
5052 : 0 : get_uleb128 (uleb2, data, data + len);
5053 : 0 : printf ("%*s[%2" PRIuMAX "] %s %" PRIu64 ", %" PRIu64 "\n",
5054 : : indent, "", (uintmax_t) offset, op_name, uleb, uleb2);
5055 [ # # ]: 0 : CONSUME (data - start);
5056 : 0 : offset += 1 + (data - start);
5057 : 0 : break;
5058 : :
5059 : 200442 : case DW_OP_fbreg:
5060 : : case DW_OP_breg0 ... DW_OP_breg31:
5061 : : case DW_OP_consts:
5062 : 200442 : start = data;
5063 : 200442 : int64_t sleb;
5064 [ - + ]: 200442 : NEED (1);
5065 : 200442 : get_sleb128 (sleb, data, data + len);
5066 : 200442 : printf ("%*s[%2" PRIuMAX "] %s %" PRId64 "\n",
5067 : : indent, "", (uintmax_t) offset, op_name, sleb);
5068 [ - + ]: 200442 : CONSUME (data - start);
5069 : 200442 : offset += 1 + (data - start);
5070 : 200442 : break;
5071 : :
5072 : 0 : case DW_OP_bregx:
5073 : 0 : start = data;
5074 [ # # ]: 0 : NEED (1);
5075 : 0 : get_uleb128 (uleb, data, data + len);
5076 : 0 : NEED (1);
5077 : 0 : get_sleb128 (sleb, data, data + len);
5078 : 0 : printf ("%*s[%2" PRIuMAX "] %s %" PRIu64 " %" PRId64 "\n",
5079 : : indent, "", (uintmax_t) offset, op_name, uleb, sleb);
5080 [ # # ]: 0 : CONSUME (data - start);
5081 : 0 : offset += 1 + (data - start);
5082 : 0 : break;
5083 : :
5084 : 0 : case DW_OP_call2:
5085 [ # # ]: 0 : NEED (2);
5086 : 0 : printf ("%*s[%2" PRIuMAX "] %s [%6" PRIx16 "]\n",
5087 : : indent, "", (uintmax_t) offset, op_name,
5088 [ # # ]: 0 : read_2ubyte_unaligned (dbg, data));
5089 : 0 : CONSUME (2);
5090 : 0 : data += 2;
5091 : 0 : offset += 3;
5092 : 0 : break;
5093 : :
5094 : 8 : case DW_OP_call4:
5095 [ - + ]: 8 : NEED (4);
5096 : 8 : printf ("%*s[%2" PRIuMAX "] %s [%6" PRIx32 "]\n",
5097 : : indent, "", (uintmax_t) offset, op_name,
5098 [ - + ]: 8 : read_4ubyte_unaligned (dbg, data));
5099 : 8 : CONSUME (4);
5100 : 8 : data += 4;
5101 : 8 : offset += 5;
5102 : 8 : break;
5103 : :
5104 : 1128 : case DW_OP_skip:
5105 : : case DW_OP_bra:
5106 [ - + ]: 1128 : NEED (2);
5107 : 2256 : printf ("%*s[%2" PRIuMAX "] %s %" PRIuMAX "\n",
5108 : : indent, "", (uintmax_t) offset, op_name,
5109 [ - + ]: 1128 : (uintmax_t) (offset + read_2sbyte_unaligned (dbg, data) + 3));
5110 : 1128 : CONSUME (2);
5111 : 1128 : data += 2;
5112 : 1128 : offset += 3;
5113 : 1128 : break;
5114 : :
5115 : 510 : case DW_OP_implicit_value:
5116 : 510 : start = data;
5117 [ - + ]: 510 : NEED (1);
5118 : 510 : get_uleb128 (uleb, data, data + len);
5119 : 510 : printf ("%*s[%2" PRIuMAX "] %s: ",
5120 : : indent, "", (uintmax_t) offset, op_name);
5121 [ - + ]: 510 : NEED (uleb);
5122 : 510 : print_block (uleb, data);
5123 : 510 : data += uleb;
5124 [ - + ]: 510 : CONSUME (data - start);
5125 : 510 : offset += 1 + (data - start);
5126 : 510 : break;
5127 : :
5128 : 5024 : case DW_OP_implicit_pointer:
5129 : : case DW_OP_GNU_implicit_pointer:
5130 : : /* DIE offset operand. */
5131 : 5024 : start = data;
5132 [ - + ]: 5024 : NEED (ref_size);
5133 [ - + ]: 5024 : if (ref_size != 4 && ref_size != 8)
5134 : 0 : goto invalid; /* Cannot be used in CFA. */
5135 [ + - ]: 5024 : if (ref_size == 4)
5136 [ - + ]: 5024 : addr = read_4ubyte_unaligned (dbg, data);
5137 : : else
5138 [ # # ]: 0 : addr = read_8ubyte_unaligned (dbg, data);
5139 : 5024 : data += ref_size;
5140 : : /* Byte offset operand. */
5141 [ - + ]: 5024 : NEED (1);
5142 : 5024 : get_sleb128 (sleb, data, data + len);
5143 : :
5144 : 5024 : printf ("%*s[%2" PRIuMAX "] %s [%6" PRIxMAX "] %+" PRId64 "\n",
5145 : : indent, "", (intmax_t) offset,
5146 : : op_name, (uintmax_t) addr, sleb);
5147 [ - + ]: 5024 : CONSUME (data - start);
5148 : 5024 : offset += 1 + (data - start);
5149 : 5024 : break;
5150 : :
5151 : 63472 : case DW_OP_entry_value:
5152 : : case DW_OP_GNU_entry_value:
5153 : : /* Size plus expression block. */
5154 : 63472 : start = data;
5155 [ - + ]: 63472 : NEED (1);
5156 : 63472 : get_uleb128 (uleb, data, data + len);
5157 : 63472 : printf ("%*s[%2" PRIuMAX "] %s:\n",
5158 : : indent, "", (uintmax_t) offset, op_name);
5159 [ - + ]: 63472 : NEED (uleb);
5160 : 63472 : print_ops (dwflmod, dbg, indent + 5, indent + 5, vers,
5161 : : addrsize, offset_size, cu, uleb, data);
5162 : 63472 : data += uleb;
5163 [ - + ]: 63472 : CONSUME (data - start);
5164 : 63472 : offset += 1 + (data - start);
5165 : 63472 : break;
5166 : :
5167 : 0 : case DW_OP_const_type:
5168 : : case DW_OP_GNU_const_type:
5169 : : /* uleb128 CU relative DW_TAG_base_type DIE offset, 1-byte
5170 : : unsigned size plus block. */
5171 : 0 : start = data;
5172 [ # # ]: 0 : NEED (1);
5173 : 0 : get_uleb128 (uleb, data, data + len);
5174 [ # # # # ]: 0 : if (! print_unresolved_addresses && cu != NULL)
5175 : 0 : uleb += cu->start;
5176 : 0 : NEED (1);
5177 : 0 : uint8_t usize = *(uint8_t *) data++;
5178 [ # # ]: 0 : NEED (usize);
5179 : 0 : printf ("%*s[%2" PRIuMAX "] %s [%6" PRIxMAX "] ",
5180 : : indent, "", (uintmax_t) offset, op_name, uleb);
5181 : 0 : print_block (usize, data);
5182 : 0 : data += usize;
5183 [ # # ]: 0 : CONSUME (data - start);
5184 : 0 : offset += 1 + (data - start);
5185 : 0 : break;
5186 : :
5187 : 0 : case DW_OP_regval_type:
5188 : : case DW_OP_GNU_regval_type:
5189 : : /* uleb128 register number, uleb128 CU relative
5190 : : DW_TAG_base_type DIE offset. */
5191 : 0 : start = data;
5192 [ # # ]: 0 : NEED (1);
5193 : 0 : get_uleb128 (uleb, data, data + len);
5194 : 0 : NEED (1);
5195 : 0 : get_uleb128 (uleb2, data, data + len);
5196 [ # # # # ]: 0 : if (! print_unresolved_addresses && cu != NULL)
5197 : 0 : uleb2 += cu->start;
5198 : 0 : printf ("%*s[%2" PRIuMAX "] %s %" PRIu64 " [%6" PRIx64 "]\n",
5199 : : indent, "", (uintmax_t) offset, op_name, uleb, uleb2);
5200 [ # # ]: 0 : CONSUME (data - start);
5201 : 0 : offset += 1 + (data - start);
5202 : 0 : break;
5203 : :
5204 : 0 : case DW_OP_deref_type:
5205 : : case DW_OP_GNU_deref_type:
5206 : : /* 1-byte unsigned size of value, uleb128 CU relative
5207 : : DW_TAG_base_type DIE offset. */
5208 : 0 : start = data;
5209 [ # # ]: 0 : NEED (1);
5210 : 0 : usize = *(uint8_t *) data++;
5211 : 0 : NEED (1);
5212 : 0 : get_uleb128 (uleb, data, data + len);
5213 [ # # # # ]: 0 : if (! print_unresolved_addresses && cu != NULL)
5214 : 0 : uleb += cu->start;
5215 : 0 : printf ("%*s[%2" PRIuMAX "] %s %" PRIu8 " [%6" PRIxMAX "]\n",
5216 : : indent, "", (uintmax_t) offset,
5217 : : op_name, usize, uleb);
5218 [ # # ]: 0 : CONSUME (data - start);
5219 : 0 : offset += 1 + (data - start);
5220 : 0 : break;
5221 : :
5222 : 0 : case DW_OP_xderef_type:
5223 : : /* 1-byte unsigned size of value, uleb128 base_type DIE offset. */
5224 : 0 : start = data;
5225 [ # # ]: 0 : NEED (1);
5226 : 0 : usize = *(uint8_t *) data++;
5227 : 0 : NEED (1);
5228 : 0 : get_uleb128 (uleb, data, data + len);
5229 : 0 : printf ("%*s[%4" PRIuMAX "] %s %" PRIu8 " [%6" PRIxMAX "]\n",
5230 : : indent, "", (uintmax_t) offset,
5231 : : op_name, usize, uleb);
5232 [ # # ]: 0 : CONSUME (data - start);
5233 : 0 : offset += 1 + (data - start);
5234 : 0 : break;
5235 : :
5236 : 810 : case DW_OP_convert:
5237 : : case DW_OP_GNU_convert:
5238 : : case DW_OP_reinterpret:
5239 : : case DW_OP_GNU_reinterpret:
5240 : : /* uleb128 CU relative offset to DW_TAG_base_type, or zero
5241 : : for conversion to untyped. */
5242 : 810 : start = data;
5243 [ - + ]: 810 : NEED (1);
5244 : 810 : get_uleb128 (uleb, data, data + len);
5245 [ + + + - : 810 : if (uleb != 0 && ! print_unresolved_addresses && cu != NULL)
+ - ]
5246 : 540 : uleb += cu->start;
5247 : 810 : printf ("%*s[%2" PRIuMAX "] %s [%6" PRIxMAX "]\n",
5248 : : indent, "", (uintmax_t) offset, op_name, uleb);
5249 [ - + ]: 810 : CONSUME (data - start);
5250 : 810 : offset += 1 + (data - start);
5251 : 810 : break;
5252 : :
5253 : 8 : case DW_OP_GNU_parameter_ref:
5254 : : /* 4 byte CU relative reference to the abstract optimized away
5255 : : DW_TAG_formal_parameter. */
5256 [ - + ]: 8 : NEED (4);
5257 [ - + ]: 8 : uintmax_t param_off = (uintmax_t) read_4ubyte_unaligned (dbg, data);
5258 [ + - + - ]: 8 : if (! print_unresolved_addresses && cu != NULL)
5259 : 8 : param_off += cu->start;
5260 : 8 : printf ("%*s[%2" PRIuMAX "] %s [%6" PRIxMAX "]\n",
5261 : : indent, "", (uintmax_t) offset, op_name, param_off);
5262 : 8 : CONSUME (4);
5263 : 8 : data += 4;
5264 : 8 : offset += 5;
5265 : 8 : break;
5266 : :
5267 : : default:
5268 : : /* No Operand. */
5269 : 779266 : printf ("%*s[%2" PRIuMAX "] %s\n",
5270 : : indent, "", (uintmax_t) offset, op_name);
5271 : 779266 : ++offset;
5272 : 779266 : break;
5273 : : }
5274 : :
5275 : 1141254 : indent = indentrest;
5276 : 1141254 : continue;
5277 : :
5278 : 0 : invalid:
5279 : 0 : printf (_("%*s[%2" PRIuMAX "] %s <TRUNCATED>\n"),
5280 : : indent, "", (uintmax_t) offset, op_name);
5281 : : break;
5282 : : }
5283 : : }
5284 : :
5285 : :
5286 : : /* Turn the addresses into file offsets by using the phdrs. */
5287 : : static void
5288 : 2 : find_offsets(Elf *elf, GElf_Addr main_bias, size_t n,
5289 : : GElf_Addr addrs[n], GElf_Off offs[n])
5290 : : {
5291 : 2 : size_t unsolved = n;
5292 [ + + ]: 18 : for (size_t i = 0; i < phnum; ++i) {
5293 : 16 : GElf_Phdr phdr_mem;
5294 : 16 : GElf_Phdr *phdr = gelf_getphdr(elf, i, &phdr_mem);
5295 [ + - + + : 16 : if (phdr != NULL && phdr->p_type == PT_LOAD && phdr->p_memsz > 0)
+ - ]
5296 [ + + ]: 46 : for (size_t j = 0; j < n; ++j)
5297 [ + + + + ]: 44 : if (offs[j] == 0 && addrs[j] >= phdr->p_vaddr + main_bias &&
5298 [ + - ]: 22 : addrs[j] - (phdr->p_vaddr + main_bias) < phdr->p_filesz) {
5299 : 22 : offs[j] = addrs[j] - (phdr->p_vaddr + main_bias) + phdr->p_offset;
5300 [ + + ]: 22 : if (--unsolved == 0)
5301 : : break;
5302 : : }
5303 : : }
5304 : 2 : }
5305 : :
5306 : : /* The dynamic segment (type PT_DYNAMIC), contains the .dynamic section.
5307 : : And .dynamic section contains an array of the dynamic structures.
5308 : : We use the array to get:
5309 : : DT_STRTAB: the address of the string table
5310 : : DT_SYMTAB: the address of the symbol table
5311 : : DT_STRSZ: the size, in bytes, of the string table
5312 : : ... */
5313 : : static void
5314 : 2 : get_dynscn_addrs(Elf *elf, GElf_Phdr *phdr, GElf_Addr addrs[i_max])
5315 : : {
5316 : 2 : Elf_Data *data = elf_getdata_rawchunk(
5317 : 2 : elf, phdr->p_offset, phdr->p_filesz, ELF_T_DYN);
5318 : :
5319 : 2 : int dyn_idx = 0;
5320 : 44 : for (;; ++dyn_idx) {
5321 : 46 : GElf_Dyn dyn_mem;
5322 : 46 : GElf_Dyn *dyn = gelf_getdyn(data, dyn_idx, &dyn_mem);
5323 : : /* DT_NULL Marks end of dynamic section. */
5324 [ + - + + ]: 46 : if (dyn == NULL || dyn->d_tag == DT_NULL)
5325 : : break;
5326 : :
5327 [ + - + + : 44 : switch (dyn->d_tag) {
+ + + + +
+ - + ]
5328 : 2 : case DT_SYMTAB:
5329 : 2 : addrs[i_symtab] = dyn->d_un.d_ptr;
5330 : 2 : break;
5331 : :
5332 : 0 : case DT_HASH:
5333 : 0 : addrs[i_hash] = dyn->d_un.d_ptr;
5334 : 0 : break;
5335 : :
5336 : 2 : case DT_GNU_HASH:
5337 : 2 : addrs[i_gnu_hash] = dyn->d_un.d_ptr;
5338 : 2 : break;
5339 : :
5340 : 2 : case DT_STRTAB:
5341 : 2 : addrs[i_strtab] = dyn->d_un.d_ptr;
5342 : 2 : break;
5343 : :
5344 : 2 : case DT_VERSYM:
5345 : 2 : addrs[i_versym] = dyn->d_un.d_ptr;
5346 : 2 : break;
5347 : :
5348 : 2 : case DT_VERDEF:
5349 : 2 : addrs[i_verdef] = dyn->d_un.d_ptr;
5350 : 2 : break;
5351 : :
5352 : 2 : case DT_VERDEFNUM:
5353 : 2 : addrs[i_verdefnum] = dyn->d_un.d_val;
5354 : 2 : break;
5355 : :
5356 : 2 : case DT_VERNEED:
5357 : 2 : addrs[i_verneed] = dyn->d_un.d_ptr;
5358 : 2 : break;
5359 : :
5360 : 2 : case DT_VERNEEDNUM:
5361 : 2 : addrs[i_verneednum] = dyn->d_un.d_val;
5362 : 2 : break;
5363 : :
5364 : 2 : case DT_STRSZ:
5365 : 2 : addrs[i_strsz] = dyn->d_un.d_val;
5366 : 2 : break;
5367 : :
5368 : 0 : case DT_SYMTAB_SHNDX:
5369 : 0 : addrs[i_symtab_shndx] = dyn->d_un.d_ptr;
5370 : 0 : break;
5371 : : }
5372 : : }
5373 : 2 : }
5374 : :
5375 : :
5376 : : /* Use dynamic segment to get data for the string table section. */
5377 : : static Elf_Data *
5378 : 2 : get_dynscn_strtab(Elf *elf, GElf_Phdr *phdr)
5379 : : {
5380 : 2 : Elf_Data *strtab_data;
5381 : 2 : GElf_Addr addrs[i_max] = {0,};
5382 : 2 : GElf_Off offs[i_max] = {0,};
5383 : 2 : get_dynscn_addrs(elf, phdr, addrs);
5384 : 2 : find_offsets(elf, 0, i_max, addrs, offs);
5385 : 4 : strtab_data = elf_getdata_rawchunk(
5386 : 2 : elf, offs[i_strtab], addrs[i_strsz], ELF_T_BYTE);
5387 : 2 : return strtab_data;
5388 : : }
5389 : :
5390 : :
5391 : : struct listptr
5392 : : {
5393 : : Dwarf_Off offset:(64 - 3);
5394 : : bool addr64:1;
5395 : : bool dwarf64:1;
5396 : : bool warned:1;
5397 : : struct Dwarf_CU *cu;
5398 : : unsigned int attr;
5399 : : };
5400 : :
5401 : : #define listptr_offset_size(p) ((p)->dwarf64 ? 8 : 4)
5402 : : #define listptr_address_size(p) ((p)->addr64 ? 8 : 4)
5403 : :
5404 : : static Dwarf_Addr
5405 : 21450 : cudie_base (Dwarf_Die *cudie)
5406 : : {
5407 : 21450 : Dwarf_Addr base;
5408 : : /* Find the base address of the compilation unit. It will normally
5409 : : be specified by DW_AT_low_pc. In DWARF-3 draft 4, the base
5410 : : address could be overridden by DW_AT_entry_pc. It's been
5411 : : removed, but GCC emits DW_AT_entry_pc and not DW_AT_lowpc for
5412 : : compilation units with discontinuous ranges. */
5413 [ - + ]: 21450 : if (unlikely (dwarf_lowpc (cudie, &base) != 0))
5414 : : {
5415 : 0 : Dwarf_Attribute attr_mem;
5416 [ # # ]: 0 : if (dwarf_formaddr (dwarf_attr (cudie, DW_AT_entry_pc, &attr_mem),
5417 : : &base) != 0)
5418 : 0 : base = 0;
5419 : : }
5420 : 21450 : return base;
5421 : : }
5422 : :
5423 : : static Dwarf_Addr
5424 : 21450 : listptr_base (struct listptr *p)
5425 : : {
5426 : 21450 : Dwarf_Die cu = CUDIE (p->cu);
5427 : 21450 : return cudie_base (&cu);
5428 : : }
5429 : :
5430 : : /* To store the name used in compare_listptr */
5431 : : static const char *sort_listptr_name;
5432 : :
5433 : : static int
5434 : 1728698 : compare_listptr (const void *a, const void *b)
5435 : : {
5436 : 1728698 : const char *name = sort_listptr_name;
5437 : 1728698 : struct listptr *p1 = (void *) a;
5438 : 1728698 : struct listptr *p2 = (void *) b;
5439 : :
5440 [ + + ]: 1728698 : if (p1->offset < p2->offset)
5441 : : return -1;
5442 [ + + ]: 166534 : if (p1->offset > p2->offset)
5443 : : return 1;
5444 : :
5445 [ + - - + ]: 8156 : if (!p1->warned && !p2->warned)
5446 : : {
5447 [ - + ]: 8156 : if (p1->addr64 != p2->addr64)
5448 : : {
5449 : 0 : p1->warned = p2->warned = true;
5450 : 8156 : error (0, 0,
5451 : 0 : _("%s %#" PRIx64 " used with different address sizes"),
5452 : : name, (uint64_t) p1->offset);
5453 : : }
5454 [ - + ]: 8156 : if (p1->dwarf64 != p2->dwarf64)
5455 : : {
5456 : 0 : p1->warned = p2->warned = true;
5457 : 8156 : error (0, 0,
5458 : 0 : _("%s %#" PRIx64 " used with different offset sizes"),
5459 : 0 : name, (uint64_t) p1->offset);
5460 : : }
5461 [ - + ]: 8156 : if (listptr_base (p1) != listptr_base (p2))
5462 : : {
5463 : 0 : p1->warned = p2->warned = true;
5464 : 8156 : error (0, 0,
5465 : 0 : _("%s %#" PRIx64 " used with different base addresses"),
5466 : 0 : name, (uint64_t) p1->offset);
5467 : : }
5468 [ + - ]: 8156 : if (p1->attr != p2 ->attr)
5469 : : {
5470 : 0 : p1->warned = p2->warned = true;
5471 : 0 : error (0, 0,
5472 : 0 : _("%s %#" PRIx64
5473 : : " used with different attribute %s and %s"),
5474 : 0 : name, (uint64_t) p1->offset, dwarf_attr_name (p1->attr),
5475 : : dwarf_attr_name (p2->attr));
5476 : : }
5477 : : }
5478 : :
5479 : : return 0;
5480 : : }
5481 : :
5482 : : struct listptr_table
5483 : : {
5484 : : size_t n;
5485 : : size_t alloc;
5486 : : struct listptr *table;
5487 : : };
5488 : :
5489 : : static struct listptr_table known_locsptr;
5490 : : static struct listptr_table known_loclistsptr;
5491 : : static struct listptr_table known_rangelistptr;
5492 : : static struct listptr_table known_rnglistptr;
5493 : : static struct listptr_table known_addrbases;
5494 : : static struct listptr_table known_stroffbases;
5495 : :
5496 : : static void
5497 : 500 : reset_listptr (struct listptr_table *table)
5498 : : {
5499 : 500 : free (table->table);
5500 : 500 : table->table = NULL;
5501 : 500 : table->n = table->alloc = 0;
5502 : : }
5503 : :
5504 : : /* Returns false if offset doesn't fit. See struct listptr. */
5505 : : static bool
5506 : 250634 : notice_listptr (enum section_e section, struct listptr_table *table,
5507 : : uint_fast8_t address_size, uint_fast8_t offset_size,
5508 : : struct Dwarf_CU *cu, Dwarf_Off offset, unsigned int attr)
5509 : : {
5510 [ + + ]: 250634 : if (print_debug_sections & section)
5511 : : {
5512 [ + + ]: 250382 : if (table->n == table->alloc)
5513 : : {
5514 [ + + ]: 408 : if (table->alloc == 0)
5515 : 174 : table->alloc = 128;
5516 : : else
5517 : 234 : table->alloc *= 2;
5518 : 408 : table->table = xrealloc (table->table,
5519 : 408 : table->alloc * sizeof table->table[0]);
5520 : : }
5521 : :
5522 : 250382 : struct listptr *p = &table->table[table->n++];
5523 : :
5524 : 250382 : *p = (struct listptr)
5525 : : {
5526 : 250382 : .addr64 = address_size == 8,
5527 : 250382 : .dwarf64 = offset_size == 8,
5528 : : .offset = offset,
5529 : : .cu = cu,
5530 : : .attr = attr
5531 : : };
5532 : :
5533 [ + - ]: 250382 : if (p->offset != offset)
5534 : : {
5535 : 0 : table->n--;
5536 : 0 : return false;
5537 : : }
5538 : : }
5539 : : return true;
5540 : : }
5541 : :
5542 : : static void
5543 : 182 : sort_listptr (struct listptr_table *table, const char *name)
5544 : : {
5545 [ + + ]: 182 : if (table->n > 0)
5546 : : {
5547 : 174 : sort_listptr_name = name;
5548 : 174 : qsort (table->table, table->n, sizeof table->table[0],
5549 : : &compare_listptr);
5550 : : }
5551 : 182 : }
5552 : :
5553 : : static bool
5554 : 208 : skip_listptr_hole (struct listptr_table *table, size_t *idxp,
5555 : : uint_fast8_t *address_sizep, uint_fast8_t *offset_sizep,
5556 : : Dwarf_Addr *base, struct Dwarf_CU **cu, ptrdiff_t offset,
5557 : : unsigned char **readp, unsigned char *endp,
5558 : : unsigned int *attr)
5559 : : {
5560 [ - + ]: 208 : if (table->n == 0)
5561 : : return false;
5562 : :
5563 [ + - + + ]: 336 : while (*idxp < table->n && table->table[*idxp].offset < (Dwarf_Off) offset)
5564 : 128 : ++*idxp;
5565 : :
5566 : 208 : struct listptr *p = &table->table[*idxp];
5567 : :
5568 [ + - ]: 208 : if (*idxp == table->n
5569 [ - + ]: 208 : || p->offset >= (Dwarf_Off) (endp - *readp + offset))
5570 : : {
5571 : 0 : *readp = endp;
5572 : 0 : printf (_(" [%6tx] <UNUSED GARBAGE IN REST OF SECTION>\n"),
5573 : : offset);
5574 : 0 : return true;
5575 : : }
5576 : :
5577 [ - + ]: 208 : if (p->offset != (Dwarf_Off) offset)
5578 : : {
5579 : 0 : *readp += p->offset - offset;
5580 : 0 : printf (_(" [%6tx] <UNUSED GARBAGE> ... %" PRIu64 " bytes ...\n"),
5581 : : offset, (Dwarf_Off) p->offset - offset);
5582 : 0 : return true;
5583 : : }
5584 : :
5585 [ + - ]: 208 : if (address_sizep != NULL)
5586 [ + + ]: 224 : *address_sizep = listptr_address_size (p);
5587 [ + + ]: 208 : if (offset_sizep != NULL)
5588 [ + - ]: 296 : *offset_sizep = listptr_offset_size (p);
5589 [ + - ]: 208 : if (base != NULL)
5590 : 208 : *base = listptr_base (p);
5591 [ + - ]: 208 : if (cu != NULL)
5592 : 208 : *cu = p->cu;
5593 [ + + ]: 208 : if (attr != NULL)
5594 : 148 : *attr = p->attr;
5595 : :
5596 : : return false;
5597 : : }
5598 : :
5599 : : static Dwarf_Off
5600 : 107542 : next_listptr_offset (struct listptr_table *table, size_t *idxp, Dwarf_Off off)
5601 : : {
5602 : : /* Note that multiple attributes could in theory point to the same loclist
5603 : : offset, so make sure we pick one that is bigger than the current one.
5604 : : The table is sorted on offset. */
5605 [ + - ]: 107542 : if (*idxp < table->n)
5606 : : {
5607 [ + - ]: 213868 : while (++*idxp < table->n)
5608 : : {
5609 : 213868 : Dwarf_Off next = table->table[*idxp].offset;
5610 [ + + ]: 213868 : if (next > off)
5611 : 107542 : return next;
5612 : : }
5613 : : }
5614 : : return 0;
5615 : : }
5616 : :
5617 : : /* Returns the listptr associated with the given index, or NULL. */
5618 : : static struct listptr *
5619 : 1460340 : get_listptr (struct listptr_table *table, size_t idx)
5620 : : {
5621 : 1460340 : if (idx >= table->n)
5622 : : return NULL;
5623 : 1460088 : return &table->table[idx];
5624 : : }
5625 : :
5626 : : /* Returns the next index, base address and CU associated with the
5627 : : list unit offsets. If there is none false is returned, otherwise
5628 : : true. Assumes the table has been sorted. */
5629 : : static bool
5630 : 4930 : listptr_cu (struct listptr_table *table, size_t *idxp,
5631 : : Dwarf_Off start, Dwarf_Off end,
5632 : : Dwarf_Addr *base, struct Dwarf_CU **cu)
5633 : : {
5634 : 4930 : while (*idxp < table->n
5635 [ + - + + ]: 38394 : && table->table[*idxp].offset < start)
5636 : 33464 : ++*idxp;
5637 : :
5638 [ + - ]: 4930 : if (*idxp < table->n
5639 [ + - ]: 4930 : && table->table[*idxp].offset >= start
5640 [ + - ]: 4930 : && table->table[*idxp].offset < end)
5641 : : {
5642 : 4930 : struct listptr *p = &table->table[*idxp];
5643 : 4930 : *base = listptr_base (p);
5644 : 4930 : *cu = p->cu;
5645 : 4930 : return true;
5646 : : }
5647 : :
5648 : : return false;
5649 : : }
5650 : :
5651 : : /* Returns the next index with the current CU for the given attribute.
5652 : : If there is none false is returned, otherwise true. Assumes the
5653 : : table has been sorted. */
5654 : : static bool
5655 : 729976 : listptr_attr (struct listptr_table *table, size_t idxp,
5656 : : Dwarf_Off offset, unsigned int attr)
5657 : : {
5658 : 1460304 : struct listptr *listptr;
5659 : 1460304 : do
5660 : : {
5661 [ + + ]: 1460304 : listptr = get_listptr (table, idxp);
5662 [ + - ]: 1460062 : if (listptr == NULL)
5663 : : return false;
5664 : :
5665 [ + + + + ]: 1460062 : if (listptr->offset == offset && listptr->attr == attr)
5666 : : return true;
5667 : :
5668 : 1352520 : idxp++;
5669 : : }
5670 [ + + ]: 1352520 : while (listptr->offset <= offset);
5671 : :
5672 : : return false;
5673 : : }
5674 : :
5675 : : static void
5676 : 88 : print_debug_abbrev_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
5677 : : Ebl *ebl, GElf_Ehdr *ehdr __attribute__ ((unused)),
5678 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
5679 : : {
5680 : 88 : Elf_Data *elf_data = get_debug_elf_data (dbg, ebl, IDX_debug_abbrev, scn);
5681 [ + - ]: 88 : if (elf_data == NULL)
5682 : : return;
5683 : :
5684 : 88 : const size_t sh_size = elf_data->d_size;
5685 : :
5686 : 88 : printf (_("\nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"
5687 : : " [ Code]\n"),
5688 : : elf_ndxscn (scn), section_name (ebl, shdr),
5689 : 88 : (uint64_t) shdr->sh_offset);
5690 : :
5691 : 88 : Dwarf_Off offset = 0;
5692 [ + + ]: 3622 : while (offset < sh_size)
5693 : : {
5694 : 3534 : printf (_("\nAbbreviation section at offset %" PRIu64 ":\n"),
5695 : : offset);
5696 : :
5697 : 325174 : while (1)
5698 : 160820 : {
5699 : 164354 : size_t length;
5700 : 164354 : Dwarf_Abbrev abbrev;
5701 : :
5702 : 164354 : int res = dwarf_offabbrev (dbg, offset, &length, &abbrev);
5703 [ + + ]: 164354 : if (res != 0)
5704 : : {
5705 [ - + ]: 3534 : if (unlikely (res < 0))
5706 : : {
5707 : 0 : printf (_("\
5708 : : *** error while reading abbreviation: %s\n"),
5709 : : dwarf_errmsg (-1));
5710 : 0 : return;
5711 : : }
5712 : :
5713 : : /* This is the NUL byte at the end of the section. */
5714 : 3534 : ++offset;
5715 : 3534 : break;
5716 : : }
5717 : :
5718 : : /* We know these calls can never fail. */
5719 : 160820 : unsigned int code = dwarf_getabbrevcode (&abbrev);
5720 : 160820 : unsigned int tag = dwarf_getabbrevtag (&abbrev);
5721 : 160820 : int has_children = dwarf_abbrevhaschildren (&abbrev);
5722 : :
5723 [ + + ]: 160820 : printf (_(" [%5u] offset: %" PRId64
5724 : : ", children: %s, tag: %s\n"),
5725 : : code, (int64_t) offset,
5726 : : has_children ? yes_str : no_str,
5727 : : dwarf_tag_name (tag));
5728 : :
5729 : 160820 : size_t cnt = 0;
5730 : 160820 : unsigned int name;
5731 : 160820 : unsigned int form;
5732 : 160820 : Dwarf_Sword data;
5733 : 160820 : Dwarf_Off enoffset;
5734 : 160820 : while (dwarf_getabbrevattr_data (&abbrev, cnt, &name, &form,
5735 [ + + ]: 919326 : &data, &enoffset) == 0)
5736 : : {
5737 : 758506 : printf (" attr: %s, form: %s",
5738 : : dwarf_attr_name (name), dwarf_form_name (form));
5739 [ + + ]: 758506 : if (form == DW_FORM_implicit_const)
5740 : 54396 : printf (" (%" PRId64 ")", data);
5741 : 758506 : printf (", offset: %#" PRIx64 "\n", (uint64_t) enoffset);
5742 : 758506 : ++cnt;
5743 : : }
5744 : :
5745 : 160820 : offset += length;
5746 : : }
5747 : : }
5748 : : }
5749 : :
5750 : :
5751 : : static void
5752 : 8 : print_debug_addr_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
5753 : : Ebl *ebl, GElf_Ehdr *ehdr,
5754 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
5755 : : {
5756 : 8 : Elf_Data *data = get_debug_elf_data (dbg, ebl, IDX_debug_addr, scn);
5757 [ + - ]: 8 : if (data == NULL)
5758 : : return;
5759 : :
5760 : 8 : printf (_("\
5761 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
5762 : : elf_ndxscn (scn), section_name (ebl, shdr),
5763 : 8 : (uint64_t) shdr->sh_offset);
5764 : :
5765 [ + - ]: 8 : if (shdr->sh_size == 0)
5766 : : return;
5767 : :
5768 : 8 : size_t idx = 0;
5769 : 8 : sort_listptr (&known_addrbases, "addr_base");
5770 : :
5771 : 8 : const unsigned char *start = (const unsigned char *) data->d_buf;
5772 : 8 : const unsigned char *readp = start;
5773 : 8 : const unsigned char *readendp = ((const unsigned char *) data->d_buf
5774 : 8 : + data->d_size);
5775 : :
5776 : 8 : while (readp < readendp)
5777 : : {
5778 : : /* We cannot really know whether or not there is an header. The
5779 : : DebugFission extension to DWARF4 doesn't add one. The DWARF5
5780 : : .debug_addr variant does. Whether or not we have an header,
5781 : : DW_AT_[GNU_]addr_base points at "index 0". So if the current
5782 : : offset equals the CU addr_base then we can just start
5783 : : printing addresses. If there is no CU with an exact match
5784 : : then we'll try to parse the header first. */
5785 : 16 : Dwarf_Off off = (Dwarf_Off) (readp
5786 : 16 : - (const unsigned char *) data->d_buf);
5787 : :
5788 : 16 : printf ("Table at offset %" PRIx64 " ", off);
5789 : :
5790 [ + - ]: 16 : struct listptr *listptr = get_listptr (&known_addrbases, idx++);
5791 : 16 : const unsigned char *next_unitp;
5792 : :
5793 : 16 : uint64_t unit_length;
5794 : 16 : uint16_t version;
5795 : 16 : uint8_t address_size;
5796 : 16 : uint8_t segment_size;
5797 [ - + ]: 16 : if (listptr == NULL)
5798 : : {
5799 : 0 : error (0, 0, "Warning: No CU references .debug_addr after %" PRIx64,
5800 : : off);
5801 : :
5802 : : /* We will have to assume it is just addresses to the end... */
5803 [ # # ]: 0 : address_size = ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 4 : 8;
5804 : 0 : next_unitp = readendp;
5805 : 0 : printf ("Unknown CU:\n");
5806 : : }
5807 : : else
5808 : : {
5809 : 16 : Dwarf_Die cudie;
5810 [ - + ]: 16 : if (dwarf_cu_die (listptr->cu, &cudie,
5811 : : NULL, NULL, NULL, NULL,
5812 : : NULL, NULL) == NULL)
5813 : 0 : printf ("Unknown CU (%s):\n", dwarf_errmsg (-1));
5814 : : else
5815 : 16 : printf ("for CU [%6" PRIx64 "]:\n", dwarf_dieoffset (&cudie));
5816 : :
5817 [ + + ]: 16 : if (listptr->offset == off)
5818 : : {
5819 [ - + ]: 4 : address_size = listptr_address_size (listptr);
5820 : 4 : segment_size = 0;
5821 : 4 : version = 4;
5822 : :
5823 : : /* The addresses start here, but where do they end? */
5824 [ + + ]: 4 : listptr = get_listptr (&known_addrbases, idx);
5825 [ - + ]: 2 : if (listptr == NULL)
5826 : : next_unitp = readendp;
5827 [ + - ]: 2 : else if (listptr->cu->version < 5)
5828 : : {
5829 : 2 : next_unitp = start + listptr->offset;
5830 [ + - - + ]: 2 : if (listptr->offset < off || listptr->offset > data->d_size)
5831 : : {
5832 : 0 : error (0, 0,
5833 : : "Warning: Bad address base for next unit at %"
5834 : : PRIx64, off);
5835 : 0 : next_unitp = readendp;
5836 : : }
5837 : : }
5838 : : else
5839 : : {
5840 : : /* Tricky, we don't have a header for this unit, but
5841 : : there is one for the next. We will have to
5842 : : "guess" how big it is and subtract it from the
5843 : : offset (because that points after the header). */
5844 [ # # ]: 0 : unsigned int offset_size = listptr_offset_size (listptr);
5845 : 0 : Dwarf_Off next_off = (listptr->offset
5846 : 0 : - (offset_size == 4 ? 4 : 12) /* len */
5847 : : - 2 /* version */
5848 : : - 1 /* address size */
5849 : 0 : - 1); /* segment selector size */
5850 : 0 : next_unitp = start + next_off;
5851 [ # # # # ]: 0 : if (next_off < off || next_off > data->d_size)
5852 : : {
5853 : 0 : error (0, 0,
5854 : : "Warning: Couldn't calculate .debug_addr "
5855 : : " unit length at %" PRIx64, off);
5856 : 0 : next_unitp = readendp;
5857 : : }
5858 : : }
5859 : 4 : unit_length = (uint64_t) (next_unitp - readp);
5860 : :
5861 : : /* Pretend we have a header. */
5862 : 4 : printf ("\n");
5863 : 4 : printf (_(" Length: %8" PRIu64 "\n"),
5864 : : unit_length);
5865 : 4 : printf (_(" DWARF version: %8" PRIu16 "\n"), version);
5866 : 4 : printf (_(" Address size: %8" PRIu64 "\n"),
5867 : : (uint64_t) address_size);
5868 : 4 : printf (_(" Segment size: %8" PRIu64 "\n"),
5869 : : (uint64_t) segment_size);
5870 : 16 : printf ("\n");
5871 : : }
5872 : : else
5873 : : {
5874 : : /* OK, we have to parse an header first. */
5875 [ - + ]: 12 : unit_length = read_4ubyte_unaligned_inc (dbg, readp);
5876 [ - + ]: 12 : if (unlikely (unit_length == 0xffffffff))
5877 : : {
5878 [ # # ]: 0 : if (unlikely (readp > readendp - 8))
5879 : : {
5880 : 0 : invalid_data:
5881 : 0 : error (0, 0, "Invalid data");
5882 : 0 : return;
5883 : : }
5884 [ # # ]: 0 : unit_length = read_8ubyte_unaligned_inc (dbg, readp);
5885 : : }
5886 : 12 : printf ("\n");
5887 : 12 : printf (_(" Length: %8" PRIu64 "\n"),
5888 : : unit_length);
5889 : :
5890 : : /* We need at least 2-bytes (version) + 1-byte
5891 : : (addr_size) + 1-byte (segment_size) = 4 bytes to
5892 : : complete the header. And this unit cannot go beyond
5893 : : the section data. */
5894 [ + - ]: 12 : if (readp > readendp - 4
5895 [ + - ]: 12 : || unit_length < 4
5896 [ - + ]: 12 : || unit_length > (uint64_t) (readendp - readp))
5897 : 0 : goto invalid_data;
5898 : :
5899 : 12 : next_unitp = readp + unit_length;
5900 : :
5901 [ - + ]: 12 : version = read_2ubyte_unaligned_inc (dbg, readp);
5902 : 12 : printf (_(" DWARF version: %8" PRIu16 "\n"), version);
5903 : :
5904 [ - + ]: 12 : if (version != 5)
5905 : : {
5906 : 0 : error (0, 0, _("Unknown version"));
5907 : 0 : goto next_unit;
5908 : : }
5909 : :
5910 : 12 : address_size = *readp++;
5911 : 12 : printf (_(" Address size: %8" PRIu64 "\n"),
5912 : : (uint64_t) address_size);
5913 : :
5914 [ - + ]: 12 : if (address_size != 4 && address_size != 8)
5915 : : {
5916 : 0 : error (0, 0, _("unsupported address size"));
5917 : 0 : goto next_unit;
5918 : : }
5919 : :
5920 : 12 : segment_size = *readp++;
5921 : 12 : printf (_(" Segment size: %8" PRIu64 "\n"),
5922 : : (uint64_t) segment_size);
5923 : 12 : printf ("\n");
5924 : :
5925 [ - + ]: 12 : if (segment_size != 0)
5926 : : {
5927 : 0 : error (0, 0, _("unsupported segment size"));
5928 : 0 : goto next_unit;
5929 : : }
5930 : :
5931 [ - + ]: 12 : if (listptr->offset != (Dwarf_Off) (readp - start))
5932 : : {
5933 : 0 : error (0, 0, "Address index doesn't start after header");
5934 : 0 : goto next_unit;
5935 : : }
5936 : : }
5937 : : }
5938 : :
5939 : 16 : int digits = 1;
5940 : 16 : size_t addresses = (next_unitp - readp) / address_size;
5941 [ + + ]: 24 : while (addresses >= 10)
5942 : : {
5943 : 8 : ++digits;
5944 : 8 : addresses /= 10;
5945 : : }
5946 : :
5947 : 16 : unsigned int uidx = 0;
5948 : 16 : size_t index_offset = readp - (const unsigned char *) data->d_buf;
5949 : 16 : printf (" Addresses start at offset 0x%zx:\n", index_offset);
5950 : 188 : while (readp <= next_unitp - address_size)
5951 : : {
5952 [ - + + + : 172 : Dwarf_Addr addr = read_addr_unaligned_inc (address_size, dbg,
- + - + ]
5953 : : readp);
5954 : 172 : printf (" [%*u] ", digits, uidx++);
5955 : 172 : print_dwarf_addr (dwflmod, address_size, addr, addr);
5956 [ + + ]: 188 : printf ("\n");
5957 : : }
5958 : 16 : printf ("\n");
5959 : :
5960 [ + - ]: 16 : if (readp != next_unitp)
5961 [ + + ]: 24 : error (0, 0, "extra %zd bytes at end of unit",
5962 : 0 : (size_t) (next_unitp - readp));
5963 : :
5964 : 16 : next_unit:
5965 : : readp = next_unitp;
5966 : : }
5967 : : }
5968 : :
5969 : : /* Print content of DWARF .debug_aranges section. We fortunately do
5970 : : not have to know a bit about the structure of the section, libdwarf
5971 : : takes care of it. */
5972 : : static void
5973 : 2 : print_decoded_aranges_section (Ebl *ebl, GElf_Ehdr *ehdr, Elf_Scn *scn,
5974 : : GElf_Shdr *shdr, Dwarf *dbg)
5975 : : {
5976 : 2 : Dwarf_Aranges *aranges;
5977 : 2 : size_t cnt;
5978 [ - + ]: 2 : if (unlikely (dwarf_getaranges (dbg, &aranges, &cnt) != 0))
5979 : : {
5980 : 0 : error (0, 0, _("cannot get .debug_aranges content: %s"),
5981 : : dwarf_errmsg (-1));
5982 : 0 : return;
5983 : : }
5984 : :
5985 : 2 : GElf_Shdr glink_mem;
5986 : 2 : GElf_Shdr *glink;
5987 : 2 : glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link), &glink_mem);
5988 [ - + ]: 2 : if (glink == NULL)
5989 : : {
5990 : 0 : error (0, 0, _("invalid sh_link value in section %zu"),
5991 : : elf_ndxscn (scn));
5992 : 0 : return;
5993 : : }
5994 : :
5995 : 2 : printf (ngettext ("\
5996 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 " contains %zu entry:\n",
5997 : : "\
5998 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 " contains %zu entries:\n",
5999 : : cnt),
6000 : : elf_ndxscn (scn), section_name (ebl, shdr),
6001 : 2 : (uint64_t) shdr->sh_offset, cnt);
6002 : :
6003 : : /* Compute floor(log16(cnt)). */
6004 : 2 : size_t tmp = cnt;
6005 : 2 : int digits = 1;
6006 [ - + ]: 2 : while (tmp >= 16)
6007 : : {
6008 : 0 : ++digits;
6009 : 0 : tmp >>= 4;
6010 : : }
6011 : :
6012 [ + + ]: 12 : for (size_t n = 0; n < cnt; ++n)
6013 : : {
6014 : 10 : Dwarf_Arange *runp = dwarf_onearange (aranges, n);
6015 [ - + ]: 10 : if (unlikely (runp == NULL))
6016 : : {
6017 : 0 : printf ("cannot get arange %zu: %s\n", n, dwarf_errmsg (-1));
6018 : 0 : return;
6019 : : }
6020 : :
6021 : 10 : Dwarf_Addr start;
6022 : 10 : Dwarf_Word length;
6023 : 10 : Dwarf_Off offset;
6024 : :
6025 [ - + ]: 10 : if (unlikely (dwarf_getarangeinfo (runp, &start, &length, &offset) != 0))
6026 : 0 : printf (_(" [%*zu] ???\n"), digits, n);
6027 : : else
6028 : 10 : printf (_(" [%*zu] start: %0#*" PRIx64
6029 : : ", length: %5" PRIu64 ", CU DIE offset: %6"
6030 : : PRId64 "\n"),
6031 [ - + ]: 10 : digits, n, ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 10 : 18,
6032 : : (uint64_t) start, (uint64_t) length, (int64_t) offset);
6033 : : }
6034 : : }
6035 : :
6036 : :
6037 : : /* Print content of DWARF .debug_aranges section. */
6038 : : static void
6039 : 92 : print_debug_aranges_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
6040 : : Ebl *ebl, GElf_Ehdr *ehdr, Elf_Scn *scn,
6041 : : GElf_Shdr *shdr, Dwarf *dbg)
6042 : : {
6043 [ + + ]: 92 : if (decodedaranges)
6044 : : {
6045 : 2 : print_decoded_aranges_section (ebl, ehdr, scn, shdr, dbg);
6046 : 2 : return;
6047 : : }
6048 : :
6049 : 90 : Elf_Data *data = get_debug_elf_data (dbg, ebl, IDX_debug_aranges, scn);
6050 [ + - ]: 90 : if (data == NULL)
6051 : : return;
6052 : :
6053 : 90 : printf (_("\
6054 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
6055 : : elf_ndxscn (scn), section_name (ebl, shdr),
6056 : 90 : (uint64_t) shdr->sh_offset);
6057 : :
6058 : 90 : const unsigned char *readp = data->d_buf;
6059 : 90 : const unsigned char *readendp = readp + data->d_size;
6060 : :
6061 [ + + ]: 3606 : while (readp < readendp)
6062 : : {
6063 : 3516 : const unsigned char *hdrstart = readp;
6064 : 3516 : size_t start_offset = hdrstart - (const unsigned char *) data->d_buf;
6065 : :
6066 : 3516 : printf (_("\nTable at offset %zu:\n"), start_offset);
6067 [ - + ]: 3516 : if (readp + 4 > readendp)
6068 : : {
6069 : 0 : invalid_data:
6070 : 0 : error (0, 0, _("invalid data in section [%zu] '%s'"),
6071 : : elf_ndxscn (scn), section_name (ebl, shdr));
6072 : 0 : return;
6073 : : }
6074 : :
6075 [ + + ]: 3516 : Dwarf_Word length = read_4ubyte_unaligned_inc (dbg, readp);
6076 : 3516 : unsigned int length_bytes = 4;
6077 [ - + ]: 3516 : if (length == DWARF3_LENGTH_64_BIT)
6078 : : {
6079 [ # # ]: 0 : if (readp + 8 > readendp)
6080 : 0 : goto invalid_data;
6081 [ # # ]: 0 : length = read_8ubyte_unaligned_inc (dbg, readp);
6082 : 0 : length_bytes = 8;
6083 : : }
6084 : :
6085 : 3516 : const unsigned char *nexthdr = readp + length;
6086 : 3516 : printf (_("\n Length: %6" PRIu64 "\n"),
6087 : : (uint64_t) length);
6088 : :
6089 [ - + ]: 3516 : if (unlikely (length > (size_t) (readendp - readp)))
6090 : 0 : goto invalid_data;
6091 : :
6092 [ - + ]: 3516 : if (length == 0)
6093 : 0 : continue;
6094 : :
6095 [ - + ]: 3516 : if (readp + 2 > readendp)
6096 : 0 : goto invalid_data;
6097 [ + + ]: 3516 : uint_fast16_t version = read_2ubyte_unaligned_inc (dbg, readp);
6098 : 3516 : printf (_(" DWARF version: %6" PRIuFAST16 "\n"),
6099 : : version);
6100 [ - + ]: 3516 : if (version != 2)
6101 : : {
6102 : 0 : error (0, 0, _("unsupported aranges version"));
6103 : 0 : goto next_table;
6104 : : }
6105 : :
6106 : 3516 : Dwarf_Word offset;
6107 [ - + ]: 3516 : if (readp + length_bytes > readendp)
6108 : 0 : goto invalid_data;
6109 [ - + ]: 3516 : if (length_bytes == 8)
6110 [ # # ]: 0 : offset = read_8ubyte_unaligned_inc (dbg, readp);
6111 : : else
6112 [ + + ]: 3516 : offset = read_4ubyte_unaligned_inc (dbg, readp);
6113 : 3516 : printf (_(" CU offset: %6" PRIx64 "\n"),
6114 : : (uint64_t) offset);
6115 : :
6116 [ - + ]: 3516 : if (readp + 1 > readendp)
6117 : 0 : goto invalid_data;
6118 : 3516 : unsigned int address_size = *readp++;
6119 : 3516 : printf (_(" Address size: %6" PRIu64 "\n"),
6120 : : (uint64_t) address_size);
6121 [ - + ]: 3516 : if (address_size != 4 && address_size != 8)
6122 : : {
6123 : 0 : error (0, 0, _("unsupported address size"));
6124 : 0 : goto next_table;
6125 : : }
6126 : :
6127 [ - + ]: 3516 : if (readp + 1 > readendp)
6128 : 0 : goto invalid_data;
6129 : 3516 : unsigned int segment_size = *readp++;
6130 : 3516 : printf (_(" Segment size: %6" PRIu64 "\n\n"),
6131 : : (uint64_t) segment_size);
6132 [ - + - - ]: 3516 : if (segment_size != 0 && segment_size != 4 && segment_size != 8)
6133 : : {
6134 : 0 : error (0, 0, _("unsupported segment size"));
6135 : 0 : goto next_table;
6136 : : }
6137 : :
6138 : : /* Round the address to the next multiple of 2*address_size. */
6139 : 3516 : readp += ((2 * address_size - ((readp - hdrstart) % (2 * address_size)))
6140 : 3516 : % (2 * address_size));
6141 : :
6142 : 3516 : while (readp < nexthdr)
6143 : : {
6144 : 7306 : Dwarf_Word range_address;
6145 : 7306 : Dwarf_Word range_length;
6146 : 7306 : Dwarf_Word segment = 0;
6147 [ - + ]: 7306 : if (readp + 2 * address_size + segment_size > readendp)
6148 : 0 : goto invalid_data;
6149 [ + + ]: 7306 : if (address_size == 4)
6150 : : {
6151 [ + + ]: 92 : range_address = read_4ubyte_unaligned_inc (dbg, readp);
6152 [ + + ]: 92 : range_length = read_4ubyte_unaligned_inc (dbg, readp);
6153 : : }
6154 : : else
6155 : : {
6156 [ + + ]: 7214 : range_address = read_8ubyte_unaligned_inc (dbg, readp);
6157 [ + + ]: 7214 : range_length = read_8ubyte_unaligned_inc (dbg, readp);
6158 : : }
6159 : :
6160 [ - + ]: 7306 : if (segment_size == 4)
6161 [ # # ]: 0 : segment = read_4ubyte_unaligned_inc (dbg, readp);
6162 [ - + ]: 7306 : else if (segment_size == 8)
6163 [ # # ]: 0 : segment = read_8ubyte_unaligned_inc (dbg, readp);
6164 : :
6165 [ + + ]: 7306 : if (range_address == 0 && range_length == 0 && segment == 0)
6166 : : break;
6167 : :
6168 : 3790 : printf (" ");
6169 : 3790 : print_dwarf_addr (dwflmod, address_size, range_address,
6170 : : range_address);
6171 : 3790 : printf ("..");
6172 : 3790 : print_dwarf_addr (dwflmod, address_size,
6173 : 3790 : range_address + range_length - 1,
6174 : : range_length);
6175 [ - + ]: 3790 : if (segment_size != 0)
6176 : 0 : printf (" (%" PRIx64 ")\n", (uint64_t) segment);
6177 : : else
6178 [ + - ]: 11096 : printf ("\n");
6179 : : }
6180 : :
6181 : 3516 : next_table:
6182 [ - + ]: 3516 : if (readp != nexthdr)
6183 : : {
6184 : 0 : size_t padding = nexthdr - readp;
6185 : 0 : printf (_(" %zu padding bytes\n"), padding);
6186 : 0 : readp = nexthdr;
6187 : : }
6188 : : }
6189 : : }
6190 : :
6191 : :
6192 : : static bool is_split_dwarf (Dwarf *dbg, uint64_t *id, Dwarf_CU **split_cu);
6193 : :
6194 : : /* Returns true and sets cu and cu_base if the given Dwarf is a split
6195 : : DWARF (.dwo) file. */
6196 : : static bool
6197 : 0 : split_dwarf_cu_base (Dwarf *dbg, Dwarf_CU **cu, Dwarf_Addr *cu_base)
6198 : : {
6199 : 0 : uint64_t id;
6200 [ # # ]: 0 : if (is_split_dwarf (dbg, &id, cu))
6201 : : {
6202 : 0 : Dwarf_Die cudie;
6203 [ # # ]: 0 : if (dwarf_cu_info (*cu, NULL, NULL, &cudie, NULL, NULL, NULL, NULL) == 0)
6204 : : {
6205 : 0 : *cu_base = cudie_base (&cudie);
6206 : 0 : return true;
6207 : : }
6208 : : }
6209 : : return false;
6210 : : }
6211 : :
6212 : : /* Print content of DWARF .debug_rnglists section. */
6213 : : static void
6214 : 38 : print_debug_rnglists_section (Dwfl_Module *dwflmod,
6215 : : Ebl *ebl,
6216 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
6217 : : Elf_Scn *scn, GElf_Shdr *shdr,
6218 : : Dwarf *dbg __attribute__((unused)))
6219 : : {
6220 : 38 : Elf_Data *data = get_debug_elf_data (dbg, ebl, IDX_debug_rnglists, scn);
6221 [ + - ]: 38 : if (data == NULL)
6222 : 0 : return;
6223 : :
6224 : 38 : printf (_("\
6225 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
6226 : : elf_ndxscn (scn), section_name (ebl, shdr),
6227 : 38 : (uint64_t) shdr->sh_offset);
6228 : :
6229 : : /* For the listptr to get the base address/CU. */
6230 : 38 : sort_listptr (&known_rnglistptr, "rnglistptr");
6231 : 38 : size_t listptr_idx = 0;
6232 : :
6233 : 38 : const unsigned char *readp = data->d_buf;
6234 : 38 : const unsigned char *const dataend = ((unsigned char *) data->d_buf
6235 : 38 : + data->d_size);
6236 [ + + ]: 1936 : while (readp < dataend)
6237 : : {
6238 [ - + ]: 1898 : if (unlikely (readp > dataend - 4))
6239 : : {
6240 : 0 : invalid_data:
6241 : 0 : error (0, 0, _("invalid data in section [%zu] '%s'"),
6242 : : elf_ndxscn (scn), section_name (ebl, shdr));
6243 : 0 : return;
6244 : : }
6245 : :
6246 : 1898 : ptrdiff_t offset = readp - (unsigned char *) data->d_buf;
6247 : 1898 : printf (_("Table at Offset 0x%" PRIx64 ":\n\n"),
6248 : : (uint64_t) offset);
6249 : :
6250 [ - + ]: 1898 : uint64_t unit_length = read_4ubyte_unaligned_inc (dbg, readp);
6251 : 1898 : unsigned int offset_size = 4;
6252 [ - + ]: 1898 : if (unlikely (unit_length == 0xffffffff))
6253 : : {
6254 [ # # ]: 0 : if (unlikely (readp > dataend - 8))
6255 : 0 : goto invalid_data;
6256 : :
6257 [ # # ]: 0 : unit_length = read_8ubyte_unaligned_inc (dbg, readp);
6258 : 0 : offset_size = 8;
6259 : : }
6260 : 1898 : printf (_(" Length: %8" PRIu64 "\n"), unit_length);
6261 : :
6262 : : /* We need at least 2-bytes + 1-byte + 1-byte + 4-bytes = 8
6263 : : bytes to complete the header. And this unit cannot go beyond
6264 : : the section data. */
6265 [ + - ]: 1898 : if (readp > dataend - 8
6266 [ + - ]: 1898 : || unit_length < 8
6267 [ - + ]: 1898 : || unit_length > (uint64_t) (dataend - readp))
6268 : 0 : goto invalid_data;
6269 : :
6270 : 1898 : const unsigned char *nexthdr = readp + unit_length;
6271 : :
6272 [ - + ]: 1898 : uint16_t version = read_2ubyte_unaligned_inc (dbg, readp);
6273 : 1898 : printf (_(" DWARF version: %8" PRIu16 "\n"), version);
6274 : :
6275 [ - + ]: 1898 : if (version != 5)
6276 : : {
6277 : 0 : error (0, 0, _("Unknown version"));
6278 : 0 : goto next_table;
6279 : : }
6280 : :
6281 : 1898 : uint8_t address_size = *readp++;
6282 : 1898 : printf (_(" Address size: %8" PRIu64 "\n"),
6283 : : (uint64_t) address_size);
6284 : :
6285 [ - + ]: 1898 : if (address_size != 4 && address_size != 8)
6286 : : {
6287 : 0 : error (0, 0, _("unsupported address size"));
6288 : 0 : goto next_table;
6289 : : }
6290 : :
6291 : 1898 : uint8_t segment_size = *readp++;
6292 : 1898 : printf (_(" Segment size: %8" PRIu64 "\n"),
6293 : : (uint64_t) segment_size);
6294 : :
6295 [ - + - - ]: 1898 : if (segment_size != 0 && segment_size != 4 && segment_size != 8)
6296 : : {
6297 : 0 : error (0, 0, _("unsupported segment size"));
6298 : 0 : goto next_table;
6299 : : }
6300 : :
6301 [ - + ]: 1898 : uint32_t offset_entry_count = read_4ubyte_unaligned_inc (dbg, readp);
6302 : 1898 : printf (_(" Offset entries: %8" PRIu64 "\n"),
6303 : : (uint64_t) offset_entry_count);
6304 : :
6305 : : /* We need the CU that uses this unit to get the initial base address. */
6306 : 1898 : Dwarf_Addr cu_base = 0;
6307 : 1898 : struct Dwarf_CU *cu = NULL;
6308 [ - + ]: 1898 : if (listptr_cu (&known_rnglistptr, &listptr_idx,
6309 : : (Dwarf_Off) offset,
6310 : 1898 : (Dwarf_Off) (nexthdr - (unsigned char *) data->d_buf),
6311 : : &cu_base, &cu)
6312 [ # # ]: 0 : || split_dwarf_cu_base (dbg, &cu, &cu_base))
6313 : 1898 : {
6314 : 1898 : Dwarf_Die cudie;
6315 [ - + ]: 1898 : if (dwarf_cu_die (cu, &cudie,
6316 : : NULL, NULL, NULL, NULL,
6317 : : NULL, NULL) == NULL)
6318 : 0 : printf (_(" Unknown CU base: "));
6319 : : else
6320 : 1898 : printf (_(" CU [%6" PRIx64 "] base: "),
6321 : : dwarf_dieoffset (&cudie));
6322 : 1898 : print_dwarf_addr (dwflmod, address_size, cu_base, cu_base);
6323 : 1898 : printf ("\n");
6324 : : }
6325 : : else
6326 : 0 : printf (_(" Not associated with a CU.\n"));
6327 : :
6328 : 1898 : printf ("\n");
6329 : :
6330 : 1898 : const unsigned char *offset_array_start = readp;
6331 [ + + ]: 1898 : if (offset_entry_count > 0)
6332 : : {
6333 : 2 : uint64_t max_entries = (unit_length - 8) / offset_size;
6334 [ - + ]: 2 : if (offset_entry_count > max_entries)
6335 : : {
6336 : 0 : error (0, 0,
6337 : 0 : _("too many offset entries for unit length"));
6338 : 0 : offset_entry_count = max_entries;
6339 : : }
6340 : :
6341 : 2 : printf (_(" Offsets starting at 0x%" PRIx64 ":\n"),
6342 : : (uint64_t) (offset_array_start
6343 : 2 : - (unsigned char *) data->d_buf));
6344 [ + + ]: 6 : for (uint32_t idx = 0; idx < offset_entry_count; idx++)
6345 : : {
6346 : 4 : printf (" [%6" PRIu32 "] ", idx);
6347 [ + - ]: 4 : if (offset_size == 4)
6348 : : {
6349 [ - + ]: 4 : uint32_t off = read_4ubyte_unaligned_inc (dbg, readp);
6350 : 4 : printf ("0x%" PRIx32 "\n", off);
6351 : : }
6352 : : else
6353 : : {
6354 [ # # ]: 0 : uint64_t off = read_8ubyte_unaligned_inc (dbg, readp);
6355 : 4 : printf ("0x%" PRIx64 "\n", off);
6356 : : }
6357 : : }
6358 : 2 : printf ("\n");
6359 : : }
6360 : :
6361 : 1898 : Dwarf_Addr base = cu_base;
6362 : 1898 : bool start_of_list = true;
6363 : 1898 : while (readp < nexthdr)
6364 : : {
6365 : 132086 : uint8_t kind = *readp++;
6366 : 132086 : uint64_t op1, op2;
6367 : :
6368 : : /* Skip padding. */
6369 [ - + ]: 132086 : if (start_of_list && kind == DW_RLE_end_of_list)
6370 : 0 : continue;
6371 : :
6372 [ + + ]: 132086 : if (start_of_list)
6373 : : {
6374 : 27672 : base = cu_base;
6375 : 27672 : printf (" Offset: %" PRIx64 ", Index: %" PRIx64 "\n",
6376 : 27672 : (uint64_t) (readp - (unsigned char *) data->d_buf - 1),
6377 : 27672 : (uint64_t) (readp - offset_array_start - 1));
6378 : 27672 : start_of_list = false;
6379 : : }
6380 : :
6381 : 132086 : printf (" %s", dwarf_range_list_encoding_name (kind));
6382 [ + - - - : 132086 : switch (kind)
+ + - +
- ]
6383 : : {
6384 : 27672 : case DW_RLE_end_of_list:
6385 : 27672 : start_of_list = true;
6386 : 27672 : printf ("\n\n");
6387 : : break;
6388 : :
6389 : 0 : case DW_RLE_base_addressx:
6390 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
6391 : : {
6392 : 0 : invalid_range:
6393 : 0 : error (0, 0, _("invalid range list data"));
6394 : 0 : goto next_table;
6395 : : }
6396 : 0 : get_uleb128 (op1, readp, nexthdr);
6397 : 0 : printf (" %" PRIx64 "\n", op1);
6398 [ # # ]: 0 : if (! print_unresolved_addresses)
6399 : : {
6400 : 0 : Dwarf_Addr addr;
6401 [ # # ]: 0 : if (get_indexed_addr (cu, op1, &addr) != 0)
6402 : 0 : printf (" ???\n");
6403 : : else
6404 : : {
6405 : 0 : printf (" ");
6406 : 0 : print_dwarf_addr (dwflmod, address_size, addr, addr);
6407 : 0 : printf ("\n");
6408 : : }
6409 : : }
6410 : : break;
6411 : :
6412 : 0 : case DW_RLE_startx_endx:
6413 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
6414 : 0 : goto invalid_range;
6415 : 0 : get_uleb128 (op1, readp, nexthdr);
6416 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
6417 : 0 : goto invalid_range;
6418 : 0 : get_uleb128 (op2, readp, nexthdr);
6419 : 0 : printf (" %" PRIx64 ", %" PRIx64 "\n", op1, op2);
6420 [ # # ]: 0 : if (! print_unresolved_addresses)
6421 : : {
6422 : 0 : Dwarf_Addr addr1;
6423 : 0 : Dwarf_Addr addr2;
6424 [ # # ]: 0 : if (get_indexed_addr (cu, op1, &addr1) != 0
6425 [ # # ]: 0 : || get_indexed_addr (cu, op2, &addr2) != 0)
6426 : : {
6427 : 0 : printf (" ???..\n");
6428 : 0 : printf (" ???\n");
6429 : : }
6430 : : else
6431 : : {
6432 : 0 : printf (" ");
6433 : 0 : print_dwarf_addr (dwflmod, address_size, addr1, addr1);
6434 : 0 : printf ("..\n ");
6435 : 0 : print_dwarf_addr (dwflmod, address_size,
6436 : : addr2 - 1, addr2);
6437 : 0 : printf ("\n");
6438 : : }
6439 : : }
6440 : : break;
6441 : :
6442 : 0 : case DW_RLE_startx_length:
6443 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
6444 : 0 : goto invalid_range;
6445 : 0 : get_uleb128 (op1, readp, nexthdr);
6446 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
6447 : 0 : goto invalid_range;
6448 : 0 : get_uleb128 (op2, readp, nexthdr);
6449 : 0 : printf (" %" PRIx64 ", %" PRIx64 "\n", op1, op2);
6450 [ # # ]: 0 : if (! print_unresolved_addresses)
6451 : : {
6452 : 0 : Dwarf_Addr addr1;
6453 : 0 : Dwarf_Addr addr2;
6454 [ # # ]: 0 : if (get_indexed_addr (cu, op1, &addr1) != 0)
6455 : : {
6456 : 0 : printf (" ???..\n");
6457 : 0 : printf (" ???\n");
6458 : : }
6459 : : else
6460 : : {
6461 : 0 : addr2 = addr1 + op2;
6462 : 0 : printf (" ");
6463 : 0 : print_dwarf_addr (dwflmod, address_size, addr1, addr1);
6464 : 0 : printf ("..\n ");
6465 : 0 : print_dwarf_addr (dwflmod, address_size,
6466 : : addr2 - 1, addr2);
6467 : 0 : printf ("\n");
6468 : : }
6469 : : }
6470 : : break;
6471 : :
6472 : 95036 : case DW_RLE_offset_pair:
6473 [ - + ]: 95036 : if ((uint64_t) (nexthdr - readp) < 1)
6474 : 0 : goto invalid_range;
6475 : 95036 : get_uleb128 (op1, readp, nexthdr);
6476 [ - + ]: 95036 : if ((uint64_t) (nexthdr - readp) < 1)
6477 : 0 : goto invalid_range;
6478 : 95036 : get_uleb128 (op2, readp, nexthdr);
6479 : 95036 : printf (" %" PRIx64 ", %" PRIx64 "\n", op1, op2);
6480 [ + - ]: 95036 : if (! print_unresolved_addresses)
6481 : : {
6482 : 95036 : op1 += base;
6483 : 95036 : op2 += base;
6484 : 95036 : printf (" ");
6485 : 95036 : print_dwarf_addr (dwflmod, address_size, op1, op1);
6486 : 95036 : printf ("..\n ");
6487 : 95036 : print_dwarf_addr (dwflmod, address_size, op2 - 1, op2);
6488 : 95036 : printf ("\n");
6489 : : }
6490 : : break;
6491 : :
6492 : 8464 : case DW_RLE_base_address:
6493 [ - + ]: 8464 : if (address_size == 4)
6494 : : {
6495 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 4)
6496 : 0 : goto invalid_range;
6497 [ # # ]: 0 : op1 = read_4ubyte_unaligned_inc (dbg, readp);
6498 : : }
6499 : : else
6500 : : {
6501 [ - + ]: 8464 : if ((uint64_t) (nexthdr - readp) < 8)
6502 : 0 : goto invalid_range;
6503 [ - + ]: 8464 : op1 = read_8ubyte_unaligned_inc (dbg, readp);
6504 : : }
6505 : 8464 : base = op1;
6506 : 8464 : printf (" 0x%" PRIx64 "\n", base);
6507 [ - + ]: 8464 : if (! print_unresolved_addresses)
6508 : : {
6509 : 8464 : printf (" ");
6510 : 8464 : print_dwarf_addr (dwflmod, address_size, base, base);
6511 : 8464 : printf ("\n");
6512 : : }
6513 : : break;
6514 : :
6515 : 0 : case DW_RLE_start_end:
6516 [ # # ]: 0 : if (address_size == 4)
6517 : : {
6518 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 8)
6519 : 0 : goto invalid_range;
6520 [ # # ]: 0 : op1 = read_4ubyte_unaligned_inc (dbg, readp);
6521 [ # # ]: 0 : op2 = read_4ubyte_unaligned_inc (dbg, readp);
6522 : : }
6523 : : else
6524 : : {
6525 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 16)
6526 : 0 : goto invalid_range;
6527 [ # # ]: 0 : op1 = read_8ubyte_unaligned_inc (dbg, readp);
6528 [ # # ]: 0 : op2 = read_8ubyte_unaligned_inc (dbg, readp);
6529 : : }
6530 : 0 : printf (" 0x%" PRIx64 "..0x%" PRIx64 "\n", op1, op2);
6531 [ # # ]: 0 : if (! print_unresolved_addresses)
6532 : : {
6533 : 0 : printf (" ");
6534 : 0 : print_dwarf_addr (dwflmod, address_size, op1, op1);
6535 : 0 : printf ("..\n ");
6536 : 0 : print_dwarf_addr (dwflmod, address_size, op2 - 1, op2);
6537 : 0 : printf ("\n");
6538 : : }
6539 : : break;
6540 : :
6541 : 914 : case DW_RLE_start_length:
6542 [ - + ]: 914 : if (address_size == 4)
6543 : : {
6544 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 4)
6545 : 0 : goto invalid_range;
6546 [ # # ]: 0 : op1 = read_4ubyte_unaligned_inc (dbg, readp);
6547 : : }
6548 : : else
6549 : : {
6550 [ - + ]: 914 : if ((uint64_t) (nexthdr - readp) < 8)
6551 : 0 : goto invalid_range;
6552 [ - + ]: 914 : op1 = read_8ubyte_unaligned_inc (dbg, readp);
6553 : : }
6554 [ - + ]: 914 : if ((uint64_t) (nexthdr - readp) < 1)
6555 : 0 : goto invalid_range;
6556 : 914 : get_uleb128 (op2, readp, nexthdr);
6557 : 914 : printf (" 0x%" PRIx64 ", %" PRIx64 "\n", op1, op2);
6558 [ + + ]: 914 : if (! print_unresolved_addresses)
6559 : : {
6560 : 912 : op2 = op1 + op2;
6561 : 912 : printf (" ");
6562 : 912 : print_dwarf_addr (dwflmod, address_size, op1, op1);
6563 : 912 : printf ("..\n ");
6564 : 912 : print_dwarf_addr (dwflmod, address_size, op2 - 1, op2);
6565 [ + + ]: 134896 : printf ("\n");
6566 : : }
6567 : : break;
6568 : :
6569 : 0 : default:
6570 : 0 : goto invalid_range;
6571 : : }
6572 : : }
6573 : :
6574 : 1898 : next_table:
6575 [ - + ]: 1898 : if (readp != nexthdr)
6576 : : {
6577 : 0 : size_t padding = nexthdr - readp;
6578 : 0 : printf (_(" %zu padding bytes\n\n"), padding);
6579 : 0 : readp = nexthdr;
6580 : : }
6581 : : }
6582 : : }
6583 : :
6584 : : /* Print content of DWARF .debug_ranges section. */
6585 : : static void
6586 : 32 : print_debug_ranges_section (Dwfl_Module *dwflmod,
6587 : : Ebl *ebl, GElf_Ehdr *ehdr,
6588 : : Elf_Scn *scn, GElf_Shdr *shdr,
6589 : : Dwarf *dbg)
6590 : : {
6591 : 32 : Elf_Data *data = get_debug_elf_data (dbg, ebl, IDX_debug_ranges, scn);
6592 [ - + ]: 32 : if (data == NULL)
6593 : 0 : return;
6594 : :
6595 : 32 : printf (_("\
6596 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
6597 : : elf_ndxscn (scn), section_name (ebl, shdr),
6598 : 32 : (uint64_t) shdr->sh_offset);
6599 : :
6600 : 32 : sort_listptr (&known_rangelistptr, "rangelistptr");
6601 : 32 : size_t listptr_idx = 0;
6602 : :
6603 [ + + ]: 32 : uint_fast8_t address_size = ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 4 : 8;
6604 : :
6605 : 32 : bool first = true;
6606 : 32 : Dwarf_Addr base = 0;
6607 : 32 : unsigned char *const endp = (unsigned char *) data->d_buf + data->d_size;
6608 : 32 : unsigned char *readp = data->d_buf;
6609 : 32 : Dwarf_CU *last_cu = NULL;
6610 [ + + ]: 184 : while (readp < endp)
6611 : : {
6612 : 152 : ptrdiff_t offset = readp - (unsigned char *) data->d_buf;
6613 : 152 : Dwarf_CU *cu = last_cu;
6614 : :
6615 [ + + - + ]: 152 : if (first && skip_listptr_hole (&known_rangelistptr, &listptr_idx,
6616 : : &address_size, NULL, &base, &cu,
6617 : : offset, &readp, endp, NULL))
6618 : 0 : continue;
6619 : :
6620 [ + + ]: 152 : if (last_cu != cu)
6621 : : {
6622 : 58 : Dwarf_Die cudie;
6623 [ - + ]: 58 : if (dwarf_cu_die (cu, &cudie,
6624 : : NULL, NULL, NULL, NULL,
6625 : : NULL, NULL) == NULL)
6626 : 0 : printf (_("\n Unknown CU base: "));
6627 : : else
6628 : 58 : printf (_("\n CU [%6" PRIx64 "] base: "),
6629 : : dwarf_dieoffset (&cudie));
6630 : 58 : print_dwarf_addr (dwflmod, address_size, base, base);
6631 : 58 : printf ("\n");
6632 : : }
6633 : 152 : last_cu = cu;
6634 : :
6635 [ - + ]: 152 : if (unlikely (data->d_size - offset < (size_t) address_size * 2))
6636 : : {
6637 : 0 : printf (_(" [%6tx] <INVALID DATA>\n"), offset);
6638 : 0 : break;
6639 : : }
6640 : :
6641 : 152 : Dwarf_Addr begin;
6642 : 152 : Dwarf_Addr end;
6643 [ + + ]: 152 : if (address_size == 8)
6644 : : {
6645 [ - + ]: 120 : begin = read_8ubyte_unaligned_inc (dbg, readp);
6646 [ - + ]: 120 : end = read_8ubyte_unaligned_inc (dbg, readp);
6647 : : }
6648 : : else
6649 : : {
6650 [ - + ]: 32 : begin = read_4ubyte_unaligned_inc (dbg, readp);
6651 [ - + ]: 32 : end = read_4ubyte_unaligned_inc (dbg, readp);
6652 [ + - ]: 32 : if (begin == (Dwarf_Addr) (uint32_t) -1)
6653 : : begin = (Dwarf_Addr) -1l;
6654 : : }
6655 : :
6656 [ - + ]: 152 : if (begin == (Dwarf_Addr) -1l) /* Base address entry. */
6657 : : {
6658 [ # # ]: 0 : if (first)
6659 : 0 : printf (" [%6tx] ", offset);
6660 : : else
6661 : 0 : printf (" ");
6662 : 0 : puts (_("base address"));
6663 : 0 : printf (" ");
6664 : 0 : print_dwarf_addr (dwflmod, address_size, end, end);
6665 : 0 : printf ("\n");
6666 : 0 : base = end;
6667 : 0 : first = false;
6668 : : }
6669 [ + + ]: 152 : else if (begin == 0 && end == 0) /* End of list entry. */
6670 : : {
6671 [ - + ]: 60 : if (first)
6672 : 0 : printf (_(" [%6tx] empty list\n"), offset);
6673 : : first = true;
6674 : : }
6675 : : else
6676 : : {
6677 : : /* We have an address range entry. */
6678 [ + + ]: 92 : if (first) /* First address range entry in a list. */
6679 : 60 : printf (" [%6tx] ", offset);
6680 : : else
6681 : 32 : printf (" ");
6682 : :
6683 : 92 : printf ("range %" PRIx64 ", %" PRIx64 "\n", begin, end);
6684 [ + + ]: 92 : if (! print_unresolved_addresses)
6685 : : {
6686 : 72 : printf (" ");
6687 : 72 : print_dwarf_addr (dwflmod, address_size, base + begin,
6688 : : base + begin);
6689 : 72 : printf ("..\n ");
6690 : 72 : print_dwarf_addr (dwflmod, address_size,
6691 : : base + end - 1, base + end);
6692 : 224 : printf ("\n");
6693 : : }
6694 : :
6695 : : first = false;
6696 : : }
6697 : : }
6698 : : }
6699 : :
6700 : : #define REGNAMESZ 16
6701 : : static const char *
6702 : 33010 : register_info (Ebl *ebl, unsigned int regno, const Ebl_Register_Location *loc,
6703 : : char name[REGNAMESZ], int *bits, int *type)
6704 : : {
6705 : 33010 : const char *set;
6706 : 33010 : const char *pfx;
6707 : 33010 : int ignore;
6708 [ + + + + ]: 95638 : ssize_t n = ebl_register_info (ebl, regno, name, REGNAMESZ, &pfx, &set,
6709 : : bits ?: &ignore, type ?: &ignore);
6710 [ - + ]: 33010 : if (n <= 0)
6711 : : {
6712 [ # # ]: 0 : if (loc != NULL)
6713 : 0 : snprintf (name, REGNAMESZ, "reg%u", loc->regno);
6714 : : else
6715 : 0 : snprintf (name, REGNAMESZ, "??? 0x%x", regno);
6716 [ # # ]: 0 : if (bits != NULL)
6717 [ # # ]: 0 : *bits = loc != NULL ? loc->bits : 0;
6718 [ # # ]: 0 : if (type != NULL)
6719 : 0 : *type = DW_ATE_unsigned;
6720 : 0 : set = "??? unrecognized";
6721 : : }
6722 : : else
6723 : : {
6724 [ + + - + ]: 33010 : if (bits != NULL && *bits <= 0)
6725 [ # # ]: 0 : *bits = loc != NULL ? loc->bits : 0;
6726 [ + + - + ]: 33010 : if (type != NULL && *type == DW_ATE_void)
6727 : 0 : *type = DW_ATE_unsigned;
6728 : :
6729 : : }
6730 : 33010 : return set;
6731 : : }
6732 : :
6733 : : static const unsigned char *
6734 : 280 : read_encoded (unsigned int encoding, const unsigned char *readp,
6735 : : const unsigned char *const endp, uint64_t *res, Dwarf *dbg)
6736 : : {
6737 [ - + ]: 280 : if ((encoding & 0xf) == DW_EH_PE_absptr)
6738 : 0 : encoding = gelf_getclass (dbg->elf) == ELFCLASS32
6739 [ # # ]: 0 : ? DW_EH_PE_udata4 : DW_EH_PE_udata8;
6740 : :
6741 [ - - - + : 280 : switch (encoding & 0xf)
- - + -
- ]
6742 : : {
6743 : 0 : case DW_EH_PE_uleb128:
6744 [ # # ]: 0 : if (readp >= endp)
6745 : 0 : goto invalid;
6746 : 0 : get_uleb128 (*res, readp, endp);
6747 : 0 : break;
6748 : 0 : case DW_EH_PE_sleb128:
6749 [ # # ]: 0 : if (readp >= endp)
6750 : 0 : goto invalid;
6751 : 0 : get_sleb128 (*res, readp, endp);
6752 : 0 : break;
6753 : 0 : case DW_EH_PE_udata2:
6754 [ # # ]: 0 : if (readp + 2 > endp)
6755 : 0 : goto invalid;
6756 [ # # ]: 0 : *res = read_2ubyte_unaligned_inc (dbg, readp);
6757 : 0 : break;
6758 : 140 : case DW_EH_PE_udata4:
6759 [ - + ]: 140 : if (readp + 4 > endp)
6760 : 0 : goto invalid;
6761 [ - + ]: 140 : *res = read_4ubyte_unaligned_inc (dbg, readp);
6762 : 140 : break;
6763 : 0 : case DW_EH_PE_udata8:
6764 [ # # ]: 0 : if (readp + 8 > endp)
6765 : 0 : goto invalid;
6766 [ # # ]: 0 : *res = read_8ubyte_unaligned_inc (dbg, readp);
6767 : 0 : break;
6768 : 0 : case DW_EH_PE_sdata2:
6769 [ # # ]: 0 : if (readp + 2 > endp)
6770 : 0 : goto invalid;
6771 [ # # ]: 0 : *res = read_2sbyte_unaligned_inc (dbg, readp);
6772 : 0 : break;
6773 : 140 : case DW_EH_PE_sdata4:
6774 [ - + ]: 140 : if (readp + 4 > endp)
6775 : 0 : goto invalid;
6776 [ - + ]: 140 : *res = read_4sbyte_unaligned_inc (dbg, readp);
6777 : 140 : break;
6778 : 0 : case DW_EH_PE_sdata8:
6779 [ # # ]: 0 : if (readp + 8 > endp)
6780 : 0 : goto invalid;
6781 [ # # ]: 0 : *res = read_8sbyte_unaligned_inc (dbg, readp);
6782 : 0 : break;
6783 : : default:
6784 : 0 : invalid:
6785 : 0 : error (1, 0,
6786 : 0 : _("invalid encoding"));
6787 : : }
6788 : :
6789 : 280 : return readp;
6790 : : }
6791 : :
6792 : : static const char *
6793 : 31314 : regname (Ebl *ebl, unsigned int regno, char *regnamebuf)
6794 : : {
6795 : 31314 : register_info (ebl, regno, NULL, regnamebuf, NULL, NULL);
6796 : :
6797 : 31314 : return regnamebuf;
6798 : : }
6799 : :
6800 : : static void
6801 : 22194 : print_cfa_program (const unsigned char *readp, const unsigned char *const endp,
6802 : : Dwarf_Word vma_base, unsigned int code_align,
6803 : : int data_align,
6804 : : unsigned int version, unsigned int ptr_size,
6805 : : unsigned int encoding,
6806 : : Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr, Dwarf *dbg)
6807 : : {
6808 : 22194 : char regnamebuf[REGNAMESZ];
6809 : :
6810 : 22194 : puts ("\n Program:");
6811 : 22194 : Dwarf_Word pc = vma_base;
6812 : 22194 : while (readp < endp)
6813 : : {
6814 : 302176 : unsigned int opcode = *readp++;
6815 : :
6816 [ + + ]: 302176 : if (opcode < DW_CFA_advance_loc)
6817 : : /* Extended opcode. */
6818 [ + - + + : 182290 : switch (opcode)
+ + - + -
- + + + +
+ + - + -
- - - - -
+ - - ]
6819 : : {
6820 : 63356 : uint64_t op1;
6821 : 63356 : int64_t sop1;
6822 : 63356 : uint64_t op2;
6823 : 63356 : int64_t sop2;
6824 : :
6825 : 63356 : case DW_CFA_nop:
6826 : 63356 : puts (" nop");
6827 : 63356 : break;
6828 : 0 : case DW_CFA_set_loc:
6829 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
6830 : 0 : goto invalid;
6831 : 0 : readp = read_encoded (encoding, readp, endp, &op1, dbg);
6832 : 182290 : printf (" set_loc %#" PRIx64 " to %#" PRIx64 "\n",
6833 : 0 : op1, pc = vma_base + op1);
6834 : : break;
6835 : 6868 : case DW_CFA_advance_loc1:
6836 [ - + ]: 6868 : if ((uint64_t) (endp - readp) < 1)
6837 : 0 : goto invalid;
6838 : 13736 : printf (" advance_loc1 %u to %#" PRIx64 "\n",
6839 : 6868 : *readp, pc += *readp * code_align);
6840 : 6868 : ++readp;
6841 : 6868 : break;
6842 : 2322 : case DW_CFA_advance_loc2:
6843 [ - + ]: 2322 : if ((uint64_t) (endp - readp) < 2)
6844 : 0 : goto invalid;
6845 [ - + ]: 2322 : op1 = read_2ubyte_unaligned_inc (dbg, readp);
6846 : 184612 : printf (" advance_loc2 %" PRIu64 " to %#" PRIx64 "\n",
6847 : 2322 : op1, pc += op1 * code_align);
6848 : : break;
6849 : 36 : case DW_CFA_advance_loc4:
6850 [ - + ]: 36 : if ((uint64_t) (endp - readp) < 4)
6851 : 0 : goto invalid;
6852 [ + - ]: 36 : op1 = read_4ubyte_unaligned_inc (dbg, readp);
6853 : 182326 : printf (" advance_loc4 %" PRIu64 " to %#" PRIx64 "\n",
6854 : 36 : op1, pc += op1 * code_align);
6855 : : break;
6856 : 8 : case DW_CFA_offset_extended:
6857 [ - + ]: 8 : if ((uint64_t) (endp - readp) < 1)
6858 : 0 : goto invalid;
6859 : 8 : get_uleb128 (op1, readp, endp);
6860 [ - + ]: 8 : if ((uint64_t) (endp - readp) < 1)
6861 : 0 : goto invalid;
6862 : 8 : get_uleb128 (op2, readp, endp);
6863 : 8 : printf (" offset_extended r%" PRIu64 " (%s) at cfa%+" PRId64
6864 : : "\n",
6865 : : op1, regname (ebl, op1, regnamebuf), op2 * data_align);
6866 : : break;
6867 : 0 : case DW_CFA_restore_extended:
6868 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
6869 : 0 : goto invalid;
6870 : 0 : get_uleb128 (op1, readp, endp);
6871 : 0 : printf (" restore_extended r%" PRIu64 " (%s)\n",
6872 : : op1, regname (ebl, op1, regnamebuf));
6873 : : break;
6874 : 110 : case DW_CFA_undefined:
6875 [ - + ]: 110 : if ((uint64_t) (endp - readp) < 1)
6876 : 0 : goto invalid;
6877 : 110 : get_uleb128 (op1, readp, endp);
6878 : 110 : printf (" undefined r%" PRIu64 " (%s)\n", op1,
6879 : : regname (ebl, op1, regnamebuf));
6880 : : break;
6881 : 0 : case DW_CFA_same_value:
6882 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
6883 : 0 : goto invalid;
6884 : 0 : get_uleb128 (op1, readp, endp);
6885 : 0 : printf (" same_value r%" PRIu64 " (%s)\n", op1,
6886 : : regname (ebl, op1, regnamebuf));
6887 : : break;
6888 : 0 : case DW_CFA_register:
6889 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
6890 : 0 : goto invalid;
6891 : 0 : get_uleb128 (op1, readp, endp);
6892 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
6893 : 0 : goto invalid;
6894 : 0 : get_uleb128 (op2, readp, endp);
6895 : 0 : printf (" register r%" PRIu64 " (%s) in r%" PRIu64 " (%s)\n",
6896 : : op1, regname (ebl, op1, regnamebuf), op2,
6897 : : regname (ebl, op2, regnamebuf));
6898 : : break;
6899 : 11068 : case DW_CFA_remember_state:
6900 : 11068 : puts (" remember_state");
6901 : 11068 : break;
6902 : 11068 : case DW_CFA_restore_state:
6903 : 11068 : puts (" restore_state");
6904 : 11068 : break;
6905 : 416 : case DW_CFA_def_cfa:
6906 [ - + ]: 416 : if ((uint64_t) (endp - readp) < 1)
6907 : 0 : goto invalid;
6908 : 416 : get_uleb128 (op1, readp, endp);
6909 [ - + ]: 416 : if ((uint64_t) (endp - readp) < 1)
6910 : 0 : goto invalid;
6911 : 416 : get_uleb128 (op2, readp, endp);
6912 : 416 : printf (" def_cfa r%" PRIu64 " (%s) at offset %" PRIu64 "\n",
6913 : : op1, regname (ebl, op1, regnamebuf), op2);
6914 : : break;
6915 : 126 : case DW_CFA_def_cfa_register:
6916 [ - + ]: 126 : if ((uint64_t) (endp - readp) < 1)
6917 : 0 : goto invalid;
6918 : 126 : get_uleb128 (op1, readp, endp);
6919 : 126 : printf (" def_cfa_register r%" PRIu64 " (%s)\n",
6920 : : op1, regname (ebl, op1, regnamebuf));
6921 : : break;
6922 : 86762 : case DW_CFA_def_cfa_offset:
6923 [ - + ]: 86762 : if ((uint64_t) (endp - readp) < 1)
6924 : 0 : goto invalid;
6925 : 86762 : get_uleb128 (op1, readp, endp);
6926 : 86762 : printf (" def_cfa_offset %" PRIu64 "\n", op1);
6927 : : break;
6928 : 118 : case DW_CFA_def_cfa_expression:
6929 [ - + ]: 118 : if ((uint64_t) (endp - readp) < 1)
6930 : 0 : goto invalid;
6931 : 118 : get_uleb128 (op1, readp, endp); /* Length of DW_FORM_block. */
6932 : 118 : printf (" def_cfa_expression %" PRIu64 "\n", op1);
6933 [ - + ]: 118 : if ((uint64_t) (endp - readp) < op1)
6934 : : {
6935 : 0 : invalid:
6936 : 0 : fputs (_(" <INVALID DATA>\n"), stdout);
6937 : 0 : return;
6938 : : }
6939 : 118 : print_ops (dwflmod, dbg, 10, 10, version, ptr_size, 0, NULL,
6940 : : op1, readp);
6941 : 118 : readp += op1;
6942 : 118 : break;
6943 : 0 : case DW_CFA_expression:
6944 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
6945 : 0 : goto invalid;
6946 : 0 : get_uleb128 (op1, readp, endp);
6947 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
6948 : 0 : goto invalid;
6949 : 0 : get_uleb128 (op2, readp, endp); /* Length of DW_FORM_block. */
6950 : 0 : printf (" expression r%" PRIu64 " (%s) \n",
6951 : : op1, regname (ebl, op1, regnamebuf));
6952 [ # # ]: 0 : if ((uint64_t) (endp - readp) < op2)
6953 : 0 : goto invalid;
6954 : 0 : print_ops (dwflmod, dbg, 10, 10, version, ptr_size, 0, NULL,
6955 : : op2, readp);
6956 : 0 : readp += op2;
6957 : 0 : break;
6958 : 24 : case DW_CFA_offset_extended_sf:
6959 [ - + ]: 24 : if ((uint64_t) (endp - readp) < 1)
6960 : 0 : goto invalid;
6961 : 24 : get_uleb128 (op1, readp, endp);
6962 [ - + ]: 24 : if ((uint64_t) (endp - readp) < 1)
6963 : 0 : goto invalid;
6964 : 24 : get_sleb128 (sop2, readp, endp);
6965 : 24 : printf (" offset_extended_sf r%" PRIu64 " (%s) at cfa%+"
6966 : : PRId64 "\n",
6967 : : op1, regname (ebl, op1, regnamebuf), sop2 * data_align);
6968 : : break;
6969 : 0 : case DW_CFA_def_cfa_sf:
6970 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
6971 : 0 : goto invalid;
6972 : 0 : get_uleb128 (op1, readp, endp);
6973 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
6974 : 0 : goto invalid;
6975 : 0 : get_sleb128 (sop2, readp, endp);
6976 : 0 : printf (" def_cfa_sf r%" PRIu64 " (%s) at offset %" PRId64 "\n",
6977 : : op1, regname (ebl, op1, regnamebuf), sop2 * data_align);
6978 : : break;
6979 : 0 : case DW_CFA_def_cfa_offset_sf:
6980 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
6981 : 0 : goto invalid;
6982 : 0 : get_sleb128 (sop1, readp, endp);
6983 : 0 : printf (" def_cfa_offset_sf %" PRId64 "\n", sop1 * data_align);
6984 : : break;
6985 : 0 : case DW_CFA_val_offset:
6986 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
6987 : 0 : goto invalid;
6988 : 0 : get_uleb128 (op1, readp, endp);
6989 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
6990 : 0 : goto invalid;
6991 : 0 : get_uleb128 (op2, readp, endp);
6992 : 0 : printf (" val_offset %" PRIu64 " at offset %" PRIu64 "\n",
6993 : : op1, op2 * data_align);
6994 : : break;
6995 : 0 : case DW_CFA_val_offset_sf:
6996 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
6997 : 0 : goto invalid;
6998 : 0 : get_uleb128 (op1, readp, endp);
6999 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
7000 : 0 : goto invalid;
7001 : 0 : get_sleb128 (sop2, readp, endp);
7002 : 0 : printf (" val_offset_sf %" PRIu64 " at offset %" PRId64 "\n",
7003 : : op1, sop2 * data_align);
7004 : : break;
7005 : 0 : case DW_CFA_val_expression:
7006 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
7007 : 0 : goto invalid;
7008 : 0 : get_uleb128 (op1, readp, endp);
7009 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
7010 : 0 : goto invalid;
7011 : 0 : get_uleb128 (op2, readp, endp); /* Length of DW_FORM_block. */
7012 : 0 : printf (" val_expression r%" PRIu64 " (%s)\n",
7013 : : op1, regname (ebl, op1, regnamebuf));
7014 [ # # ]: 0 : if ((uint64_t) (endp - readp) < op2)
7015 : 0 : goto invalid;
7016 : 0 : print_ops (dwflmod, dbg, 10, 10, version, ptr_size, 0,
7017 : : NULL, op2, readp);
7018 : 0 : readp += op2;
7019 : 0 : break;
7020 : 0 : case DW_CFA_MIPS_advance_loc8:
7021 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 8)
7022 : 0 : goto invalid;
7023 [ # # ]: 0 : op1 = read_8ubyte_unaligned_inc (dbg, readp);
7024 : 182290 : printf (" MIPS_advance_loc8 %" PRIu64 " to %#" PRIx64 "\n",
7025 : 0 : op1, pc += op1 * code_align);
7026 : : break;
7027 : 8 : case DW_CFA_GNU_window_save: /* DW_CFA_AARCH64_negate_ra_state */
7028 [ + - ]: 8 : if (ehdr->e_machine == EM_AARCH64)
7029 : 8 : puts (" AARCH64_negate_ra_state");
7030 : : else
7031 : 0 : puts (" GNU_window_save");
7032 : : break;
7033 : 0 : case DW_CFA_GNU_args_size:
7034 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
7035 : 0 : goto invalid;
7036 : 0 : get_uleb128 (op1, readp, endp);
7037 : 0 : printf (" args_size %" PRIu64 "\n", op1);
7038 : : break;
7039 : : default:
7040 : 182290 : printf (" ??? (%u)\n", opcode);
7041 : : break;
7042 : : }
7043 [ + + ]: 119886 : else if (opcode < DW_CFA_offset)
7044 : 413626 : printf (" advance_loc %u to %#" PRIx64 "\n",
7045 : 89256 : opcode & 0x3f, pc += (opcode & 0x3f) * code_align);
7046 [ + + ]: 30630 : else if (opcode < DW_CFA_restore)
7047 : : {
7048 : 27448 : uint64_t offset;
7049 [ - + ]: 27448 : if ((uint64_t) (endp - readp) < 1)
7050 : 0 : goto invalid;
7051 : 27448 : get_uleb128 (offset, readp, endp);
7052 : 27448 : printf (" offset r%u (%s) at cfa%+" PRId64 "\n",
7053 : : opcode & 0x3f, regname (ebl, opcode & 0x3f, regnamebuf),
7054 : : offset * data_align);
7055 : : }
7056 : : else
7057 [ + + ]: 327552 : printf (" restore r%u (%s)\n",
7058 : : opcode & 0x3f, regname (ebl, opcode & 0x3f, regnamebuf));
7059 : : }
7060 : : }
7061 : :
7062 : :
7063 : : static unsigned int
7064 : 21884 : encoded_ptr_size (int encoding, unsigned int ptr_size)
7065 : : {
7066 [ - + - + ]: 21884 : switch (encoding & 7)
7067 : : {
7068 : : case DW_EH_PE_udata4:
7069 : : return 4;
7070 : 0 : case DW_EH_PE_udata8:
7071 : 0 : return 8;
7072 : : case 0:
7073 : : return ptr_size;
7074 : : }
7075 : :
7076 : 0 : fprintf (stderr, "Unsupported pointer encoding: %#x, "
7077 : : "assuming pointer size of %d.\n", encoding, ptr_size);
7078 : 0 : return ptr_size;
7079 : : }
7080 : :
7081 : :
7082 : : static unsigned int
7083 : 642 : print_encoding (unsigned int val)
7084 : : {
7085 [ - - - + : 642 : switch (val & 0xf)
- - - + -
- ]
7086 : : {
7087 : 0 : case DW_EH_PE_absptr:
7088 : 0 : fputs ("absptr", stdout);
7089 : 0 : break;
7090 : 0 : case DW_EH_PE_uleb128:
7091 : 0 : fputs ("uleb128", stdout);
7092 : 0 : break;
7093 : 0 : case DW_EH_PE_udata2:
7094 : 0 : fputs ("udata2", stdout);
7095 : 0 : break;
7096 : 140 : case DW_EH_PE_udata4:
7097 : 140 : fputs ("udata4", stdout);
7098 : 140 : break;
7099 : 0 : case DW_EH_PE_udata8:
7100 : 0 : fputs ("udata8", stdout);
7101 : 0 : break;
7102 : 0 : case DW_EH_PE_sleb128:
7103 : 0 : fputs ("sleb128", stdout);
7104 : 0 : break;
7105 : 0 : case DW_EH_PE_sdata2:
7106 : 0 : fputs ("sdata2", stdout);
7107 : 0 : break;
7108 : 502 : case DW_EH_PE_sdata4:
7109 : 502 : fputs ("sdata4", stdout);
7110 : 502 : break;
7111 : 0 : case DW_EH_PE_sdata8:
7112 : 0 : fputs ("sdata8", stdout);
7113 : 0 : break;
7114 : : default:
7115 : : /* We did not use any of the bits after all. */
7116 : : return val;
7117 : : }
7118 : :
7119 : 642 : return val & ~0xf;
7120 : : }
7121 : :
7122 : :
7123 : : static unsigned int
7124 : 502 : print_relinfo (unsigned int val)
7125 : : {
7126 [ + - + - : 502 : switch (val & 0x70)
- - ]
7127 : : {
7128 : 362 : case DW_EH_PE_pcrel:
7129 : 362 : fputs ("pcrel", stdout);
7130 : 362 : break;
7131 : 0 : case DW_EH_PE_textrel:
7132 : 0 : fputs ("textrel", stdout);
7133 : 0 : break;
7134 : 140 : case DW_EH_PE_datarel:
7135 : 140 : fputs ("datarel", stdout);
7136 : 140 : break;
7137 : 0 : case DW_EH_PE_funcrel:
7138 : 0 : fputs ("funcrel", stdout);
7139 : 0 : break;
7140 : 0 : case DW_EH_PE_aligned:
7141 : 0 : fputs ("aligned", stdout);
7142 : 0 : break;
7143 : : default:
7144 : : return val;
7145 : : }
7146 : :
7147 : 502 : return val & ~0x70;
7148 : : }
7149 : :
7150 : :
7151 : : static void
7152 : 642 : print_encoding_base (const char *pfx, unsigned int fde_encoding)
7153 : : {
7154 : 642 : printf ("(%s", pfx);
7155 : :
7156 [ - + ]: 642 : if (fde_encoding == DW_EH_PE_omit)
7157 : 0 : puts ("omit)");
7158 : : else
7159 : : {
7160 : 642 : unsigned int w = fde_encoding;
7161 : :
7162 : 642 : w = print_encoding (w);
7163 : :
7164 [ + + ]: 642 : if (w & 0x70)
7165 : : {
7166 [ + - ]: 502 : if (w != fde_encoding)
7167 : 502 : fputc (' ', stdout);
7168 : :
7169 : 502 : w = print_relinfo (w);
7170 : : }
7171 : :
7172 [ - + ]: 642 : if (w != 0)
7173 [ # # ]: 0 : printf ("%s%x", w != fde_encoding ? " " : "", w);
7174 : :
7175 : 642 : puts (")");
7176 : : }
7177 : 642 : }
7178 : :
7179 : :
7180 : : static void
7181 : 196 : print_debug_frame_section (Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr,
7182 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
7183 : : {
7184 : 196 : size_t shstrndx;
7185 : : /* We know this call will succeed since it did in the caller. */
7186 : 196 : (void) elf_getshdrstrndx (ebl->elf, &shstrndx);
7187 : 196 : const char *scnname = section_name (ebl, shdr);
7188 : :
7189 : : /* Needed if we find PC-relative addresses. */
7190 : 196 : GElf_Addr bias;
7191 [ - + ]: 196 : if (dwfl_module_getelf (dwflmod, &bias) == NULL)
7192 : : {
7193 : 0 : error (0, 0, _("cannot get ELF: %s"), dwfl_errmsg (-1));
7194 : 0 : return;
7195 : : }
7196 : :
7197 : 196 : bool is_eh_frame = strcmp (scnname, ".eh_frame") == 0;
7198 : 196 : Elf_Data *data;
7199 [ + + ]: 196 : if (is_eh_frame)
7200 : : {
7201 : 144 : data = elf_rawdata (scn, NULL);
7202 [ - + ]: 144 : if (data == NULL)
7203 : : {
7204 : 0 : error (0, 0, _("cannot get %s content: %s"),
7205 : : scnname, elf_errmsg (-1));
7206 : 0 : return;
7207 : : }
7208 : : }
7209 : : else
7210 : : {
7211 : 52 : data = get_debug_elf_data (dbg, ebl, IDX_debug_frame, scn);
7212 [ + - ]: 52 : if (data == NULL)
7213 : : return;
7214 : : }
7215 : :
7216 [ + + ]: 196 : if (is_eh_frame)
7217 : 144 : printf (_("\
7218 : : \nCall frame information section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
7219 : 144 : elf_ndxscn (scn), scnname, (uint64_t) shdr->sh_offset);
7220 : : else
7221 : 52 : printf (_("\
7222 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
7223 : 52 : elf_ndxscn (scn), scnname, (uint64_t) shdr->sh_offset);
7224 : :
7225 : 196 : struct cieinfo
7226 : : {
7227 : : ptrdiff_t cie_offset;
7228 : : const char *augmentation;
7229 : : unsigned int code_alignment_factor;
7230 : : unsigned int data_alignment_factor;
7231 : : uint8_t address_size;
7232 : : uint8_t fde_encoding;
7233 : : uint8_t lsda_encoding;
7234 : : struct cieinfo *next;
7235 : 196 : } *cies = NULL;
7236 : :
7237 : 196 : const unsigned char *readp = data->d_buf;
7238 : 196 : const unsigned char *const dataend = ((unsigned char *) data->d_buf
7239 : 196 : + data->d_size);
7240 [ + + ]: 22530 : while (readp < dataend)
7241 : : {
7242 [ - + ]: 22334 : if (unlikely (readp + 4 > dataend))
7243 : : {
7244 : 0 : invalid_data:
7245 : 0 : error (0, 0, _("invalid data in section [%zu] '%s'"),
7246 : : elf_ndxscn (scn), scnname);
7247 : 0 : return;
7248 : : }
7249 : :
7250 : : /* At the beginning there must be a CIE. There can be multiple,
7251 : : hence we test tis in a loop. */
7252 : 22334 : ptrdiff_t offset = readp - (unsigned char *) data->d_buf;
7253 : :
7254 [ + + ]: 22334 : Dwarf_Word unit_length = read_4ubyte_unaligned_inc (dbg, readp);
7255 : 22334 : unsigned int length = 4;
7256 [ - + ]: 22334 : if (unlikely (unit_length == 0xffffffff))
7257 : : {
7258 [ # # ]: 0 : if (unlikely (readp + 8 > dataend))
7259 : 0 : goto invalid_data;
7260 : :
7261 [ # # ]: 0 : unit_length = read_8ubyte_unaligned_inc (dbg, readp);
7262 : 0 : length = 8;
7263 : : }
7264 : :
7265 [ + + ]: 22334 : if (unlikely (unit_length == 0))
7266 : : {
7267 : 140 : printf (_("\n [%6tx] Zero terminator\n"), offset);
7268 : 140 : continue;
7269 : : }
7270 : :
7271 : 22194 : Dwarf_Word maxsize = dataend - readp;
7272 [ - + ]: 22194 : if (unlikely (unit_length > maxsize))
7273 : 0 : goto invalid_data;
7274 : :
7275 [ + + ]: 22194 : unsigned int ptr_size = ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 4 : 8;
7276 : :
7277 : 22194 : ptrdiff_t start = readp - (unsigned char *) data->d_buf;
7278 : 22194 : const unsigned char *const cieend = readp + unit_length;
7279 [ - + ]: 22194 : if (unlikely (cieend > dataend))
7280 : 0 : goto invalid_data;
7281 : :
7282 : 22194 : Dwarf_Off cie_id;
7283 [ + - ]: 22194 : if (length == 4)
7284 : : {
7285 [ - + ]: 22194 : if (unlikely (cieend - readp < 4))
7286 : 0 : goto invalid_data;
7287 [ + + ]: 22194 : cie_id = read_4ubyte_unaligned_inc (dbg, readp);
7288 [ + + ]: 22194 : if (!is_eh_frame && cie_id == DW_CIE_ID_32)
7289 : : cie_id = DW_CIE_ID_64;
7290 : : }
7291 : : else
7292 : : {
7293 [ # # ]: 0 : if (unlikely (cieend - readp < 8))
7294 : 0 : goto invalid_data;
7295 [ # # ]: 0 : cie_id = read_8ubyte_unaligned_inc (dbg, readp);
7296 : : }
7297 : :
7298 : 44304 : uint_fast8_t version = 2;
7299 : 22110 : unsigned int code_alignment_factor;
7300 : 22110 : int data_alignment_factor;
7301 : 44304 : unsigned int fde_encoding = 0;
7302 : 44304 : unsigned int lsda_encoding = 0;
7303 : 44304 : Dwarf_Word initial_location = 0;
7304 : 44304 : Dwarf_Word vma_base = 0;
7305 : :
7306 [ + + + + ]: 22286 : if (cie_id == (is_eh_frame ? 0 : DW_CIE_ID_64))
7307 : : {
7308 [ - + ]: 310 : if (unlikely (cieend - readp < 2))
7309 : 0 : goto invalid_data;
7310 : 310 : version = *readp++;
7311 : 310 : const char *const augmentation = (const char *) readp;
7312 : 310 : readp = memchr (readp, '\0', cieend - readp);
7313 [ - + ]: 310 : if (unlikely (readp == NULL))
7314 : 0 : goto invalid_data;
7315 : 310 : ++readp;
7316 : :
7317 : 310 : uint_fast8_t segment_size = 0;
7318 [ + + ]: 310 : if (version >= 4)
7319 : : {
7320 [ - + ]: 8 : if (cieend - readp < 5)
7321 : 0 : goto invalid_data;
7322 : 8 : ptr_size = *readp++;
7323 : 8 : segment_size = *readp++;
7324 : : }
7325 : :
7326 [ - + ]: 310 : if (cieend - readp < 1)
7327 : 0 : goto invalid_data;
7328 : 310 : get_uleb128 (code_alignment_factor, readp, cieend);
7329 [ - + ]: 310 : if (cieend - readp < 1)
7330 : 0 : goto invalid_data;
7331 : 310 : get_sleb128 (data_alignment_factor, readp, cieend);
7332 : :
7333 : : /* In some variant for unwind data there is another field. */
7334 [ - + ]: 310 : if (strcmp (augmentation, "eh") == 0)
7335 [ # # ]: 0 : readp += ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 4 : 8;
7336 : :
7337 : 310 : unsigned int return_address_register;
7338 [ - + ]: 310 : if (cieend - readp < 1)
7339 : 0 : goto invalid_data;
7340 [ + + ]: 310 : if (unlikely (version == 1))
7341 : 286 : return_address_register = *readp++;
7342 : : else
7343 : 24 : get_uleb128 (return_address_register, readp, cieend);
7344 : :
7345 : 310 : printf ("\n [%6tx] CIE length=%" PRIu64 "\n"
7346 : : " CIE_id: %" PRIu64 "\n"
7347 : : " version: %u\n"
7348 : : " augmentation: \"%s\"\n",
7349 : : offset, (uint64_t) unit_length, (uint64_t) cie_id,
7350 : : version, augmentation);
7351 [ + + ]: 310 : if (version >= 4)
7352 : 8 : printf (" address_size: %u\n"
7353 : : " segment_size: %u\n",
7354 : : ptr_size, segment_size);
7355 : 310 : printf (" code_alignment_factor: %u\n"
7356 : : " data_alignment_factor: %d\n"
7357 : : " return_address_register: %u\n",
7358 : : code_alignment_factor,
7359 : : data_alignment_factor, return_address_register);
7360 : :
7361 [ + + ]: 310 : if (augmentation[0] == 'z')
7362 : : {
7363 [ - + ]: 222 : if (cieend - readp < 1)
7364 : 0 : goto invalid_data;
7365 : :
7366 : 222 : unsigned int augmentationlen;
7367 : 222 : get_uleb128 (augmentationlen, readp, cieend);
7368 : :
7369 [ - + ]: 222 : if (augmentationlen > (size_t) (cieend - readp))
7370 : : {
7371 : 0 : error (0, 0, _("invalid augmentation length"));
7372 : 0 : readp = cieend;
7373 : 0 : continue;
7374 : : }
7375 : :
7376 : 222 : const char *hdr = "Augmentation data:";
7377 : 222 : const char *cp = augmentation + 1;
7378 [ + + + - ]: 444 : while (*cp != '\0' && cp < augmentation + augmentationlen + 1)
7379 : : {
7380 : 222 : printf (" %-26s%#x ", hdr, *readp);
7381 : 222 : hdr = "";
7382 : :
7383 [ + - ]: 222 : if (*cp == 'R')
7384 : : {
7385 : 222 : fde_encoding = *readp++;
7386 : 222 : print_encoding_base (_("FDE address encoding: "),
7387 : : fde_encoding);
7388 : : }
7389 [ # # ]: 0 : else if (*cp == 'L')
7390 : : {
7391 : 0 : lsda_encoding = *readp++;
7392 : 0 : print_encoding_base (_("LSDA pointer encoding: "),
7393 : : lsda_encoding);
7394 : : }
7395 [ # # ]: 0 : else if (*cp == 'P')
7396 : : {
7397 : : /* Personality. This field usually has a relocation
7398 : : attached pointing to __gcc_personality_v0. */
7399 : 0 : const unsigned char *startp = readp;
7400 : 0 : unsigned int encoding = *readp++;
7401 : 0 : uint64_t val = 0;
7402 : 0 : readp = read_encoded (encoding, readp,
7403 : : readp - 1 + augmentationlen,
7404 : : &val, dbg);
7405 : :
7406 : 0 : while (++startp < readp)
7407 [ # # ]: 0 : printf ("%#x ", *startp);
7408 : :
7409 : 0 : putchar ('(');
7410 : 0 : print_encoding (encoding);
7411 : 0 : putchar (' ');
7412 [ # # ]: 0 : switch (encoding & 0xf)
7413 : : {
7414 : 0 : case DW_EH_PE_sleb128:
7415 : : case DW_EH_PE_sdata2:
7416 : : case DW_EH_PE_sdata4:
7417 : 0 : printf ("%" PRId64 ")\n", val);
7418 : : break;
7419 : 0 : default:
7420 : 0 : printf ("%#" PRIx64 ")\n", val);
7421 : : break;
7422 : : }
7423 : : }
7424 : : else
7425 : 0 : printf ("(%x)\n", *readp++);
7426 : :
7427 : 222 : ++cp;
7428 : : }
7429 : : }
7430 : :
7431 [ + - ]: 310 : if (likely (ptr_size == 4 || ptr_size == 8))
7432 : : {
7433 : 310 : struct cieinfo *newp = alloca (sizeof (*newp));
7434 : 310 : newp->cie_offset = offset;
7435 : 310 : newp->augmentation = augmentation;
7436 : 310 : newp->fde_encoding = fde_encoding;
7437 : 310 : newp->lsda_encoding = lsda_encoding;
7438 : 310 : newp->address_size = ptr_size;
7439 : 310 : newp->code_alignment_factor = code_alignment_factor;
7440 : 310 : newp->data_alignment_factor = data_alignment_factor;
7441 : 310 : newp->next = cies;
7442 : 310 : cies = newp;
7443 : : }
7444 : : }
7445 : : else
7446 : : {
7447 : : struct cieinfo *cie = cies;
7448 [ + - ]: 21884 : while (cie != NULL)
7449 [ + + - + ]: 43768 : if (is_eh_frame
7450 : 21792 : ? ((Dwarf_Off) start - cie_id) == (Dwarf_Off) cie->cie_offset
7451 : 92 : : cie_id == (Dwarf_Off) cie->cie_offset)
7452 : : break;
7453 : : else
7454 : 0 : cie = cie->next;
7455 [ - + ]: 21884 : if (unlikely (cie == NULL))
7456 : : {
7457 : 0 : puts ("invalid CIE reference in FDE");
7458 : 0 : return;
7459 : : }
7460 : :
7461 : : /* Initialize from CIE data. */
7462 : 21884 : fde_encoding = cie->fde_encoding;
7463 : 21884 : lsda_encoding = cie->lsda_encoding;
7464 : 21884 : ptr_size = encoded_ptr_size (fde_encoding, cie->address_size);
7465 : 21884 : code_alignment_factor = cie->code_alignment_factor;
7466 : 21884 : data_alignment_factor = cie->data_alignment_factor;
7467 : :
7468 : 21884 : const unsigned char *base = readp;
7469 : : // XXX There are sometimes relocations for this value
7470 [ - + + + : 21884 : initial_location = read_addr_unaligned_inc (ptr_size, dbg, readp);
- + + + ]
7471 : 43768 : Dwarf_Word address_range
7472 [ + + - + : 21884 : = read_addr_unaligned_inc (ptr_size, dbg, readp);
+ + ]
7473 : :
7474 : : /* pcrel for an FDE address is relative to the runtime
7475 : : address of the start_address field itself. Sign extend
7476 : : if necessary to make sure the calculation is done on the
7477 : : full 64 bit address even when initial_location only holds
7478 : : the lower 32 bits. */
7479 : 21884 : Dwarf_Addr pc_start = initial_location;
7480 [ + + ]: 21884 : if (ptr_size == 4)
7481 : 21804 : pc_start = (uint64_t) (int32_t) pc_start;
7482 [ + + ]: 21884 : if ((fde_encoding & 0x70) == DW_EH_PE_pcrel)
7483 : 21780 : pc_start += ((uint64_t) shdr->sh_addr
7484 : 21780 : + (base - (const unsigned char *) data->d_buf)
7485 : 21780 : - bias);
7486 : :
7487 : 21884 : printf ("\n [%6tx] FDE length=%" PRIu64 " cie=[%6tx]\n"
7488 : : " CIE_pointer: %" PRIu64 "\n"
7489 : : " initial_location: ",
7490 : : offset, (uint64_t) unit_length,
7491 : : cie->cie_offset, (uint64_t) cie_id);
7492 : 21884 : print_dwarf_addr (dwflmod, cie->address_size,
7493 : : pc_start, initial_location);
7494 [ + + ]: 21884 : if ((fde_encoding & 0x70) == DW_EH_PE_pcrel)
7495 : : {
7496 : 43560 : vma_base = (((uint64_t) shdr->sh_offset
7497 : 21780 : + (base - (const unsigned char *) data->d_buf)
7498 : 21780 : + (uint64_t) initial_location)
7499 : : & (ptr_size == 4
7500 : : ? UINT64_C (0xffffffff)
7501 [ - + ]: 21780 : : UINT64_C (0xffffffffffffffff)));
7502 : 21780 : printf (_(" (offset: %#" PRIx64 ")"),
7503 : : (uint64_t) vma_base);
7504 : : }
7505 : :
7506 : 21884 : printf ("\n address_range: %#" PRIx64,
7507 : : (uint64_t) address_range);
7508 [ + + ]: 21884 : if ((fde_encoding & 0x70) == DW_EH_PE_pcrel)
7509 : 21780 : printf (_(" (end offset: %#" PRIx64 ")"),
7510 : 21780 : ((uint64_t) vma_base + (uint64_t) address_range)
7511 : : & (ptr_size == 4
7512 : : ? UINT64_C (0xffffffff)
7513 [ - + ]: 21780 : : UINT64_C (0xffffffffffffffff)));
7514 : 21884 : putchar ('\n');
7515 : :
7516 [ + + ]: 21884 : if (cie->augmentation[0] == 'z')
7517 : : {
7518 : 21780 : unsigned int augmentationlen;
7519 [ - + ]: 21780 : if (cieend - readp < 1)
7520 : 0 : goto invalid_data;
7521 : 21780 : get_uleb128 (augmentationlen, readp, cieend);
7522 : :
7523 [ - + ]: 21780 : if (augmentationlen > (size_t) (cieend - readp))
7524 : : {
7525 : 0 : error (0, 0, _("invalid augmentation length"));
7526 : 0 : readp = cieend;
7527 : 0 : continue;
7528 : : }
7529 : :
7530 [ - + ]: 21780 : if (augmentationlen > 0)
7531 : : {
7532 : 0 : const char *hdr = "Augmentation data:";
7533 : 0 : const char *cp = cie->augmentation + 1;
7534 : 0 : unsigned int u = 0;
7535 : 0 : while (*cp != '\0'
7536 [ # # # # ]: 0 : && cp < cie->augmentation + augmentationlen + 1)
7537 : : {
7538 [ # # ]: 0 : if (*cp == 'L')
7539 : : {
7540 : 0 : uint64_t lsda_pointer;
7541 : 0 : const unsigned char *p
7542 : 0 : = read_encoded (lsda_encoding, &readp[u],
7543 : : &readp[augmentationlen],
7544 : : &lsda_pointer, dbg);
7545 : 0 : u = p - readp;
7546 : 0 : printf (_("\
7547 : : %-26sLSDA pointer: %#" PRIx64 "\n"),
7548 : : hdr, lsda_pointer);
7549 : 0 : hdr = "";
7550 : : }
7551 : 0 : ++cp;
7552 : : }
7553 : :
7554 [ # # ]: 0 : while (u < augmentationlen)
7555 : : {
7556 : 0 : printf (" %-26s%#x\n", hdr, readp[u++]);
7557 : 0 : hdr = "";
7558 : : }
7559 : : }
7560 : :
7561 : 21780 : readp += augmentationlen;
7562 : : }
7563 : : }
7564 : :
7565 : : /* Handle the initialization instructions. */
7566 [ - + ]: 22194 : if (ptr_size != 4 && ptr_size !=8)
7567 : 0 : printf ("invalid CIE pointer size (%u), must be 4 or 8.\n", ptr_size);
7568 : : else
7569 : 22194 : print_cfa_program (readp, cieend, vma_base, code_alignment_factor,
7570 : : data_alignment_factor, version, ptr_size,
7571 : : fde_encoding, dwflmod, ebl, ehdr, dbg);
7572 : 22194 : readp = cieend;
7573 : : }
7574 : : }
7575 : :
7576 : :
7577 : : /* Returns the signedness (or false if it cannot be determined) and
7578 : : the byte size (or zero if it cannot be gotten) of the given DIE
7579 : : DW_AT_type attribute. Uses dwarf_peel_type and dwarf_aggregate_size. */
7580 : : static void
7581 : 346122 : die_type_sign_bytes (Dwarf_Die *die, bool *is_signed, int *bytes)
7582 : : {
7583 : 346122 : Dwarf_Attribute attr;
7584 : 346122 : Dwarf_Die type;
7585 : :
7586 : 346122 : *bytes = 0;
7587 : 346122 : *is_signed = false;
7588 : :
7589 [ + + ]: 346122 : if (dwarf_peel_type (dwarf_formref_die (dwarf_attr_integrate (die,
7590 : : DW_AT_type,
7591 : : &attr), &type),
7592 : : &type) == 0)
7593 : : {
7594 : 896 : Dwarf_Word val;
7595 : 896 : *is_signed = (dwarf_formudata (dwarf_attr (&type, DW_AT_encoding,
7596 : : &attr), &val) == 0
7597 [ + + + + ]: 896 : && (val == DW_ATE_signed || val == DW_ATE_signed_char));
7598 : :
7599 [ + - ]: 896 : if (dwarf_aggregate_size (&type, &val) == 0)
7600 : 896 : *bytes = val;
7601 : : }
7602 : 346122 : }
7603 : :
7604 : : struct attrcb_args
7605 : : {
7606 : : Dwfl_Module *dwflmod;
7607 : : Dwarf *dbg;
7608 : : Dwarf_Die *dies;
7609 : : int level;
7610 : : bool silent;
7611 : : bool is_split;
7612 : : unsigned int version;
7613 : : unsigned int addrsize;
7614 : : unsigned int offset_size;
7615 : : struct Dwarf_CU *cu;
7616 : : };
7617 : :
7618 : :
7619 : : static int
7620 : 7911704 : attr_callback (Dwarf_Attribute *attrp, void *arg)
7621 : : {
7622 : 7911704 : struct attrcb_args *cbargs = (struct attrcb_args *) arg;
7623 : 7911704 : const int level = cbargs->level;
7624 : 7911704 : Dwarf_Die *die = &cbargs->dies[level];
7625 : 7911704 : bool is_split = cbargs->is_split;
7626 : :
7627 [ + - ]: 7911704 : unsigned int attr = dwarf_whatattr (attrp);
7628 [ - + ]: 7911704 : if (unlikely (attr == 0))
7629 : : {
7630 [ # # ]: 0 : if (!cbargs->silent)
7631 : 0 : error (0, 0, _("DIE [%" PRIx64 "] "
7632 : : "cannot get attribute code: %s"),
7633 : : dwarf_dieoffset (die), dwarf_errmsg (-1));
7634 : 0 : return DWARF_CB_ABORT;
7635 : : }
7636 : :
7637 [ + - ]: 7911704 : unsigned int form = dwarf_whatform (attrp);
7638 [ - + ]: 7911704 : if (unlikely (form == 0))
7639 : : {
7640 [ # # ]: 0 : if (!cbargs->silent)
7641 : 0 : error (0, 0, _("DIE [%" PRIx64 "] "
7642 : : "cannot get attribute form: %s"),
7643 : : dwarf_dieoffset (die), dwarf_errmsg (-1));
7644 : 0 : return DWARF_CB_ABORT;
7645 : : }
7646 : :
7647 [ + + + + : 7911704 : switch (form)
+ + + +
- ]
7648 : : {
7649 : 101200 : case DW_FORM_addr:
7650 : : case DW_FORM_addrx:
7651 : : case DW_FORM_addrx1:
7652 : : case DW_FORM_addrx2:
7653 : : case DW_FORM_addrx3:
7654 : : case DW_FORM_addrx4:
7655 : : case DW_FORM_GNU_addr_index:
7656 [ + + ]: 101200 : if (!cbargs->silent)
7657 : : {
7658 : 100658 : Dwarf_Addr addr;
7659 [ - + ]: 100658 : if (unlikely (dwarf_formaddr (attrp, &addr) != 0))
7660 : : {
7661 : 0 : attrval_out:
7662 [ # # ]: 0 : if (!cbargs->silent)
7663 : 0 : error (0, 0, _("DIE [%" PRIx64 "] "
7664 : : "cannot get attribute '%s' (%s) value: "
7665 : : "%s"),
7666 : : dwarf_dieoffset (die),
7667 : : dwarf_attr_name (attr),
7668 : : dwarf_form_name (form),
7669 : : dwarf_errmsg (-1));
7670 : : /* Don't ABORT, it might be other attributes can be resolved. */
7671 : 0 : return DWARF_CB_OK;
7672 : : }
7673 [ + + ]: 100658 : if (form != DW_FORM_addr )
7674 : : {
7675 : 40 : Dwarf_Word word;
7676 [ - + ]: 40 : if (dwarf_formudata (attrp, &word) != 0)
7677 : 0 : goto attrval_out;
7678 : 40 : printf (" %*s%-20s (%s) [%" PRIx64 "] ",
7679 : : (int) (level * 2), "", dwarf_attr_name (attr),
7680 : : dwarf_form_name (form), word);
7681 : : }
7682 : : else
7683 : 100618 : printf (" %*s%-20s (%s) ",
7684 : : (int) (level * 2), "", dwarf_attr_name (attr),
7685 : : dwarf_form_name (form));
7686 : 100658 : print_dwarf_addr (cbargs->dwflmod, cbargs->addrsize, addr, addr);
7687 : 100658 : printf ("\n");
7688 : : }
7689 : 7116410 : break;
7690 : :
7691 : 1308994 : case DW_FORM_indirect:
7692 : : case DW_FORM_strp:
7693 : : case DW_FORM_line_strp:
7694 : : case DW_FORM_strx:
7695 : : case DW_FORM_strx1:
7696 : : case DW_FORM_strx2:
7697 : : case DW_FORM_strx3:
7698 : : case DW_FORM_strx4:
7699 : : case DW_FORM_string:
7700 : : case DW_FORM_GNU_strp_alt:
7701 : : case DW_FORM_GNU_str_index:
7702 [ + + ]: 1308994 : if (cbargs->silent)
7703 : : break;
7704 : 1307484 : const char *str = dwarf_formstring (attrp);
7705 [ - + ]: 1307484 : if (unlikely (str == NULL))
7706 : 0 : goto attrval_out;
7707 : 1307484 : printf (" %*s%-20s (%s) \"%s\"\n",
7708 : : (int) (level * 2), "", dwarf_attr_name (attr),
7709 : : dwarf_form_name (form), str);
7710 : : break;
7711 : :
7712 : 1713370 : case DW_FORM_ref_addr:
7713 : : case DW_FORM_ref_udata:
7714 : : case DW_FORM_ref8:
7715 : : case DW_FORM_ref4:
7716 : : case DW_FORM_ref2:
7717 : : case DW_FORM_ref1:
7718 : : case DW_FORM_GNU_ref_alt:
7719 : : case DW_FORM_ref_sup4:
7720 : : case DW_FORM_ref_sup8:
7721 [ + + ]: 1713370 : if (cbargs->silent)
7722 : : break;
7723 : 1711790 : Dwarf_Die ref;
7724 [ - + ]: 1711790 : if (unlikely (dwarf_formref_die (attrp, &ref) == NULL))
7725 : 0 : goto attrval_out;
7726 : :
7727 : 1711790 : printf (" %*s%-20s (%s) ",
7728 : : (int) (level * 2), "", dwarf_attr_name (attr),
7729 : : dwarf_form_name (form));
7730 [ + + ]: 1711790 : if (is_split)
7731 : 66 : printf ("{%6" PRIxMAX "}\n", (uintmax_t) dwarf_dieoffset (&ref));
7732 : : else
7733 : 1711724 : printf ("[%6" PRIxMAX "]\n", (uintmax_t) dwarf_dieoffset (&ref));
7734 : : break;
7735 : :
7736 : 8 : case DW_FORM_ref_sig8:
7737 [ + - ]: 8 : if (cbargs->silent)
7738 : : break;
7739 : 8 : printf (" %*s%-20s (%s) {%6" PRIx64 "}\n",
7740 : : (int) (level * 2), "", dwarf_attr_name (attr),
7741 : : dwarf_form_name (form),
7742 [ - + ]: 8 : (uint64_t) read_8ubyte_unaligned (attrp->cu->dbg, attrp->valp));
7743 : : break;
7744 : :
7745 : 4394034 : case DW_FORM_sec_offset:
7746 : : case DW_FORM_rnglistx:
7747 : : case DW_FORM_loclistx:
7748 : : case DW_FORM_implicit_const:
7749 : : case DW_FORM_udata:
7750 : : case DW_FORM_sdata:
7751 : : case DW_FORM_data8: /* Note no data16 here, we see that as block. */
7752 : : case DW_FORM_data4:
7753 : : case DW_FORM_data2:
7754 : 4394034 : case DW_FORM_data1:;
7755 : 4394034 : Dwarf_Word num;
7756 [ - + ]: 4394034 : if (unlikely (dwarf_formudata (attrp, &num) != 0))
7757 : 0 : goto attrval_out;
7758 : :
7759 : 4394034 : const char *valuestr = NULL;
7760 : 4394034 : bool as_hex_id = false;
7761 [ + + + + : 4394034 : switch (attr)
+ + + + +
- - - - -
- + - + +
+ ]
7762 : : {
7763 : : /* This case can take either a constant or a loclistptr. */
7764 : 544530 : case DW_AT_data_member_location:
7765 [ + - ]: 544530 : if (form != DW_FORM_sec_offset
7766 [ + + ]: 544530 : && (cbargs->version >= 4
7767 [ + - ]: 47612 : || (form != DW_FORM_data4 && form != DW_FORM_data8)))
7768 : : {
7769 [ + - ]: 544530 : if (!cbargs->silent)
7770 : 544530 : printf (" %*s%-20s (%s) %" PRIuMAX "\n",
7771 : : (int) (level * 2), "", dwarf_attr_name (attr),
7772 : : dwarf_form_name (form), (uintmax_t) num);
7773 : 544530 : return DWARF_CB_OK;
7774 : : }
7775 : 217358 : FALLTHROUGH;
7776 : :
7777 : : /* These cases always take a loclist[ptr] and no constant. */
7778 : : case DW_AT_location:
7779 : : case DW_AT_data_location:
7780 : : case DW_AT_vtable_elem_location:
7781 : : case DW_AT_string_length:
7782 : : case DW_AT_use_location:
7783 : : case DW_AT_frame_base:
7784 : : case DW_AT_return_addr:
7785 : : case DW_AT_static_link:
7786 : : case DW_AT_segment:
7787 : : case DW_AT_GNU_call_site_value:
7788 : : case DW_AT_GNU_call_site_data_value:
7789 : : case DW_AT_GNU_call_site_target:
7790 : : case DW_AT_GNU_call_site_target_clobbered:
7791 : : case DW_AT_GNU_locviews:
7792 : : {
7793 : 217358 : bool nlpt;
7794 [ + + ]: 217358 : if (cbargs->cu->version < 5)
7795 : : {
7796 [ + + ]: 324 : if (! cbargs->is_split)
7797 : : {
7798 : 284 : nlpt = notice_listptr (section_loc, &known_locsptr,
7799 : 284 : cbargs->addrsize,
7800 : 284 : cbargs->offset_size,
7801 : : cbargs->cu, num, attr);
7802 : : }
7803 : : else
7804 : : nlpt = true;
7805 : : }
7806 : : else
7807 : : {
7808 : : /* Only register for a real section offset. Otherwise
7809 : : it is a DW_FORM_loclistx which is just an index
7810 : : number and we should already have registered the
7811 : : section offset for the index when we saw the
7812 : : DW_AT_loclists_base CU attribute. */
7813 [ + + ]: 217034 : if (form == DW_FORM_sec_offset)
7814 : 216956 : nlpt = notice_listptr (section_loc, &known_loclistsptr,
7815 : 216956 : cbargs->addrsize, cbargs->offset_size,
7816 : : cbargs->cu, num, attr);
7817 : : else
7818 : : nlpt = true;
7819 : :
7820 : : }
7821 : :
7822 [ + + ]: 217358 : if (!cbargs->silent)
7823 : : {
7824 [ + + + + ]: 216992 : if (cbargs->cu->version < 5 || form == DW_FORM_sec_offset)
7825 [ - + ]: 216978 : printf (" %*s%-20s (%s) location list [%6"
7826 : : PRIxMAX "]%s\n",
7827 : : (int) (level * 2), "", dwarf_attr_name (attr),
7828 : : dwarf_form_name (form), (uintmax_t) num,
7829 : : nlpt ? "" : " <WARNING offset too big>");
7830 : : else
7831 : 14 : printf (" %*s%-20s (%s) location index [%6"
7832 : : PRIxMAX "]\n",
7833 : : (int) (level * 2), "", dwarf_attr_name (attr),
7834 : : dwarf_form_name (form), (uintmax_t) num);
7835 : : }
7836 : : }
7837 : : return DWARF_CB_OK;
7838 : :
7839 : 10 : case DW_AT_loclists_base:
7840 : : {
7841 : 20 : bool nlpt = notice_listptr (section_loc, &known_loclistsptr,
7842 : 10 : cbargs->addrsize, cbargs->offset_size,
7843 : : cbargs->cu, num, attr);
7844 : :
7845 [ + + ]: 10 : if (!cbargs->silent)
7846 [ - + ]: 2 : printf (" %*s%-20s (%s) location list [%6" PRIxMAX "]%s\n",
7847 : : (int) (level * 2), "", dwarf_attr_name (attr),
7848 : : dwarf_form_name (form), (uintmax_t) num,
7849 : : nlpt ? "" : " <WARNING offset too big>");
7850 : : }
7851 : : return DWARF_CB_OK;
7852 : :
7853 : 33346 : case DW_AT_ranges:
7854 : : case DW_AT_start_scope:
7855 : : {
7856 : 33346 : bool nlpt;
7857 [ + + ]: 33346 : if (cbargs->cu->version < 5)
7858 : 106 : nlpt = notice_listptr (section_ranges, &known_rangelistptr,
7859 : 106 : cbargs->addrsize, cbargs->offset_size,
7860 : : cbargs->cu, num, attr);
7861 : : else
7862 : : {
7863 : : /* Only register for a real section offset. Otherwise
7864 : : it is a DW_FORM_rangelistx which is just an index
7865 : : number and we should already have registered the
7866 : : section offset for the index when we saw the
7867 : : DW_AT_rnglists_base CU attribute. */
7868 [ + + ]: 33240 : if (form == DW_FORM_sec_offset)
7869 : 33228 : nlpt = notice_listptr (section_ranges, &known_rnglistptr,
7870 : 33228 : cbargs->addrsize, cbargs->offset_size,
7871 : : cbargs->cu, num, attr);
7872 : : else
7873 : : nlpt = true;
7874 : : }
7875 : :
7876 [ + + ]: 33346 : if (!cbargs->silent)
7877 : : {
7878 [ + + + + ]: 33260 : if (cbargs->cu->version < 5 || form == DW_FORM_sec_offset)
7879 [ - + ]: 33256 : printf (" %*s%-20s (%s) range list [%6"
7880 : : PRIxMAX "]%s\n",
7881 : : (int) (level * 2), "", dwarf_attr_name (attr),
7882 : : dwarf_form_name (form), (uintmax_t) num,
7883 : : nlpt ? "" : " <WARNING offset too big>");
7884 : : else
7885 : 4 : printf (" %*s%-20s (%s) range index [%6"
7886 : : PRIxMAX "]\n",
7887 : : (int) (level * 2), "", dwarf_attr_name (attr),
7888 : : dwarf_form_name (form), (uintmax_t) num);
7889 : : }
7890 : : }
7891 : : return DWARF_CB_OK;
7892 : :
7893 : 8 : case DW_AT_rnglists_base:
7894 : : {
7895 : 16 : bool nlpt = notice_listptr (section_ranges, &known_rnglistptr,
7896 : 8 : cbargs->addrsize, cbargs->offset_size,
7897 : : cbargs->cu, num, attr);
7898 [ + + ]: 8 : if (!cbargs->silent)
7899 [ - + ]: 4 : printf (" %*s%-20s (%s) range list [%6"
7900 : : PRIxMAX "]%s\n",
7901 : : (int) (level * 2), "", dwarf_attr_name (attr),
7902 : : dwarf_form_name (form), (uintmax_t) num,
7903 : : nlpt ? "" : " <WARNING offset too big>");
7904 : : }
7905 : : return DWARF_CB_OK;
7906 : :
7907 : 34 : case DW_AT_addr_base:
7908 : : case DW_AT_GNU_addr_base:
7909 : : {
7910 : 68 : bool addrbase = notice_listptr (section_addr, &known_addrbases,
7911 : 34 : cbargs->addrsize,
7912 : 34 : cbargs->offset_size,
7913 : : cbargs->cu, num, attr);
7914 [ + + ]: 34 : if (!cbargs->silent)
7915 [ - + ]: 18 : printf (" %*s%-20s (%s) address base [%6"
7916 : : PRIxMAX "]%s\n",
7917 : : (int) (level * 2), "", dwarf_attr_name (attr),
7918 : : dwarf_form_name (form), (uintmax_t) num,
7919 : : addrbase ? "" : " <WARNING offset too big>");
7920 : : }
7921 : : return DWARF_CB_OK;
7922 : :
7923 : 8 : case DW_AT_str_offsets_base:
7924 : : {
7925 : 16 : bool stroffbase = notice_listptr (section_str, &known_stroffbases,
7926 : 8 : cbargs->addrsize,
7927 : 8 : cbargs->offset_size,
7928 : : cbargs->cu, num, attr);
7929 [ + - ]: 8 : if (!cbargs->silent)
7930 [ - + ]: 795294 : printf (" %*s%-20s (%s) str offsets base [%6"
7931 : : PRIxMAX "]%s\n",
7932 : : (int) (level * 2), "", dwarf_attr_name (attr),
7933 : : dwarf_form_name (form), (uintmax_t) num,
7934 : : stroffbase ? "" : " <WARNING offset too big>");
7935 : : }
7936 : : return DWARF_CB_OK;
7937 : :
7938 : 3672 : case DW_AT_language:
7939 : 3672 : valuestr = dwarf_lang_name (num);
7940 : 3672 : break;
7941 : 56440 : case DW_AT_encoding:
7942 : 56440 : valuestr = dwarf_encoding_name (num);
7943 : 56440 : break;
7944 : 0 : case DW_AT_accessibility:
7945 : 0 : valuestr = dwarf_access_name (num);
7946 : 0 : break;
7947 : 0 : case DW_AT_defaulted:
7948 : 0 : valuestr = dwarf_defaulted_name (num);
7949 : 0 : break;
7950 : 0 : case DW_AT_visibility:
7951 : 0 : valuestr = dwarf_visibility_name (num);
7952 : 0 : break;
7953 : 0 : case DW_AT_virtuality:
7954 : 0 : valuestr = dwarf_virtuality_name (num);
7955 : 0 : break;
7956 : 0 : case DW_AT_identifier_case:
7957 : 0 : valuestr = dwarf_identifier_case_name (num);
7958 : 0 : break;
7959 : 0 : case DW_AT_calling_convention:
7960 : 0 : valuestr = dwarf_calling_convention_name (num);
7961 : 0 : break;
7962 : 7118 : case DW_AT_inline:
7963 : 7118 : valuestr = dwarf_inline_name (num);
7964 : 7118 : break;
7965 : 0 : case DW_AT_ordering:
7966 : 0 : valuestr = dwarf_ordering_name (num);
7967 : 0 : break;
7968 : 966290 : case DW_AT_decl_file:
7969 : : case DW_AT_call_file:
7970 : : {
7971 [ + + ]: 966290 : if (cbargs->silent)
7972 : : break;
7973 : :
7974 : : /* Try to get the actual file, the current interface only
7975 : : gives us full paths, but we only want to show the file
7976 : : name for now. */
7977 : 965442 : Dwarf_Die cudie;
7978 [ + - ]: 965442 : if (dwarf_cu_die (cbargs->cu, &cudie,
7979 : : NULL, NULL, NULL, NULL, NULL, NULL) != NULL)
7980 : : {
7981 : 965442 : Dwarf_Files *files;
7982 : 965442 : size_t nfiles;
7983 [ + - ]: 965442 : if (dwarf_getsrcfiles (&cudie, &files, &nfiles) == 0)
7984 : : {
7985 : 965442 : valuestr = dwarf_filesrc (files, num, NULL, NULL);
7986 [ + - ]: 965442 : if (valuestr != NULL)
7987 : : {
7988 : 965442 : char *filename = strrchr (valuestr, '/');
7989 [ + - ]: 965442 : if (filename != NULL)
7990 : 965442 : valuestr = filename + 1;
7991 : : }
7992 : : else
7993 : 0 : error (0, 0, _("invalid file (%" PRId64 "): %s"),
7994 : : num, dwarf_errmsg (-1));
7995 : : }
7996 : : else
7997 : 0 : error (0, 0, _("no srcfiles for CU [%" PRIx64 "]"),
7998 : : dwarf_dieoffset (&cudie));
7999 : : }
8000 : : else
8001 : 0 : error (0, 0, _("couldn't get DWARF CU: %s"),
8002 : : dwarf_errmsg (-1));
8003 : 965442 : if (valuestr == NULL)
8004 : : valuestr = "???";
8005 : : }
8006 : 965442 : break;
8007 : 26 : case DW_AT_GNU_dwo_id:
8008 : 26 : as_hex_id = true;
8009 : 26 : break;
8010 : :
8011 : : default:
8012 : : /* Nothing. */
8013 : : break;
8014 : : }
8015 : :
8016 [ + + ]: 3598740 : if (cbargs->silent)
8017 : : break;
8018 : :
8019 : : /* When highpc is in constant form it is relative to lowpc.
8020 : : In that case also show the address. */
8021 : 3595030 : Dwarf_Addr highpc;
8022 [ + + + - ]: 3595030 : if (attr == DW_AT_high_pc && dwarf_highpc (die, &highpc) == 0)
8023 : : {
8024 : 24708 : printf (" %*s%-20s (%s) %" PRIuMAX " (",
8025 : : (int) (level * 2), "", dwarf_attr_name (attr),
8026 : : dwarf_form_name (form), (uintmax_t) num);
8027 : 24708 : print_dwarf_addr (cbargs->dwflmod, cbargs->addrsize, highpc, highpc);
8028 : 24708 : printf (")\n");
8029 : : }
8030 : : else
8031 : : {
8032 [ + + ]: 3570322 : if (as_hex_id)
8033 : : {
8034 : 4 : printf (" %*s%-20s (%s) 0x%.16" PRIx64 "\n",
8035 : : (int) (level * 2), "", dwarf_attr_name (attr),
8036 : : dwarf_form_name (form), num);
8037 : : }
8038 : : else
8039 : : {
8040 : 3570318 : Dwarf_Sword snum = 0;
8041 : 3570318 : bool is_signed;
8042 : 3570318 : int bytes = 0;
8043 [ + + ]: 3570318 : if (attr == DW_AT_const_value)
8044 : 346114 : die_type_sign_bytes (die, &is_signed, &bytes);
8045 : : else
8046 : 3224204 : is_signed = (form == DW_FORM_sdata
8047 : 3224204 : || form == DW_FORM_implicit_const);
8048 : :
8049 [ + + ]: 3570318 : if (is_signed)
8050 [ - + ]: 371240 : if (unlikely (dwarf_formsdata (attrp, &snum) != 0))
8051 : 0 : goto attrval_out;
8052 : :
8053 [ + + ]: 3570318 : if (valuestr == NULL)
8054 : : {
8055 : 2538256 : printf (" %*s%-20s (%s) ",
8056 : : (int) (level * 2), "", dwarf_attr_name (attr),
8057 : : dwarf_form_name (form));
8058 : : }
8059 : : else
8060 : : {
8061 : 1032062 : printf (" %*s%-20s (%s) %s (",
8062 : : (int) (level * 2), "", dwarf_attr_name (attr),
8063 : : dwarf_form_name (form), valuestr);
8064 : : }
8065 : :
8066 [ + + + + : 3570318 : switch (bytes)
+ ]
8067 : : {
8068 : 10 : case 1:
8069 [ + + ]: 10 : if (is_signed)
8070 : 2 : printf ("%" PRId8, (int8_t) snum);
8071 : : else
8072 : 8 : printf ("%" PRIu8, (uint8_t) num);
8073 : : break;
8074 : :
8075 : 4 : case 2:
8076 [ + + ]: 4 : if (is_signed)
8077 : 2 : printf ("%" PRId16, (int16_t) snum);
8078 : : else
8079 : 2 : printf ("%" PRIu16, (uint16_t) num);
8080 : : break;
8081 : :
8082 : 258 : case 4:
8083 [ + + ]: 258 : if (is_signed)
8084 : 246 : printf ("%" PRId32, (int32_t) snum);
8085 : : else
8086 : 12 : printf ("%" PRIu32, (uint32_t) num);
8087 : : break;
8088 : :
8089 : 616 : case 8:
8090 [ + + ]: 616 : if (is_signed)
8091 : 2 : printf ("%" PRId64, (int64_t) snum);
8092 : : else
8093 : 614 : printf ("%" PRIu64, (uint64_t) num);
8094 : : break;
8095 : :
8096 : 3569430 : default:
8097 [ + + ]: 3569430 : if (is_signed)
8098 : 370988 : printf ("%" PRIdMAX, (intmax_t) snum);
8099 : : else
8100 : 3198442 : printf ("%" PRIuMAX, (uintmax_t) num);
8101 : : break;
8102 : : }
8103 : :
8104 : : /* Make clear if we switched from a signed encoding to
8105 : : an unsigned value. */
8106 [ + + ]: 3570318 : if (attr == DW_AT_const_value
8107 [ + + ]: 346114 : && (form == DW_FORM_sdata || form == DW_FORM_implicit_const)
8108 [ + + ]: 2578 : && !is_signed)
8109 : 2560 : printf (" (%" PRIdMAX ")", (intmax_t) num);
8110 : :
8111 [ + + ]: 3570318 : if (valuestr == NULL)
8112 : 2538256 : printf ("\n");
8113 : : else
8114 : 3570318 : printf (")\n");
8115 : : }
8116 : : }
8117 : : break;
8118 : :
8119 : 18244 : case DW_FORM_flag:
8120 [ + + ]: 18244 : if (cbargs->silent)
8121 : : break;
8122 : 18096 : bool flag;
8123 [ - + ]: 18096 : if (unlikely (dwarf_formflag (attrp, &flag) != 0))
8124 : 0 : goto attrval_out;
8125 : :
8126 : 18096 : printf (" %*s%-20s (%s) %s\n",
8127 : : (int) (level * 2), "", dwarf_attr_name (attr),
8128 [ + - ]: 18096 : dwarf_form_name (form), flag ? yes_str : no_str);
8129 : : break;
8130 : :
8131 : 156082 : case DW_FORM_flag_present:
8132 [ + + ]: 156082 : if (cbargs->silent)
8133 : : break;
8134 : 155528 : printf (" %*s%-20s (%s) %s\n",
8135 : : (int) (level * 2), "", dwarf_attr_name (attr),
8136 : : dwarf_form_name (form), yes_str);
8137 : : break;
8138 : :
8139 : 219772 : case DW_FORM_exprloc:
8140 : : case DW_FORM_block4:
8141 : : case DW_FORM_block2:
8142 : : case DW_FORM_block1:
8143 : : case DW_FORM_block:
8144 : : case DW_FORM_data16: /* DWARF5 calls this a constant class. */
8145 [ + + ]: 219772 : if (cbargs->silent)
8146 : : break;
8147 : 219510 : Dwarf_Block block;
8148 [ - + ]: 219510 : if (unlikely (dwarf_formblock (attrp, &block) != 0))
8149 : 0 : goto attrval_out;
8150 : :
8151 : 219510 : printf (" %*s%-20s (%s) ",
8152 : : (int) (level * 2), "", dwarf_attr_name (attr),
8153 : : dwarf_form_name (form));
8154 : :
8155 [ + + + ]: 219510 : switch (attr)
8156 : : {
8157 : 94616 : default:
8158 [ - + ]: 94616 : if (form != DW_FORM_exprloc)
8159 : : {
8160 : 0 : print_block (block.length, block.data);
8161 : 0 : break;
8162 : : }
8163 : 124886 : FALLTHROUGH;
8164 : :
8165 : : case DW_AT_location:
8166 : : case DW_AT_data_location:
8167 : : case DW_AT_data_member_location:
8168 : : case DW_AT_vtable_elem_location:
8169 : : case DW_AT_string_length:
8170 : : case DW_AT_use_location:
8171 : : case DW_AT_frame_base:
8172 : : case DW_AT_return_addr:
8173 : : case DW_AT_static_link:
8174 : : case DW_AT_allocated:
8175 : : case DW_AT_associated:
8176 : : case DW_AT_bit_size:
8177 : : case DW_AT_bit_offset:
8178 : : case DW_AT_bit_stride:
8179 : : case DW_AT_byte_size:
8180 : : case DW_AT_byte_stride:
8181 : : case DW_AT_count:
8182 : : case DW_AT_lower_bound:
8183 : : case DW_AT_upper_bound:
8184 : : case DW_AT_GNU_call_site_value:
8185 : : case DW_AT_GNU_call_site_data_value:
8186 : : case DW_AT_GNU_call_site_target:
8187 : : case DW_AT_GNU_call_site_target_clobbered:
8188 [ + + ]: 124886 : if (form == DW_FORM_exprloc
8189 [ + - ]: 140 : || (form != DW_FORM_data16
8190 [ + - ]: 140 : && attrp->cu->version < 4)) /* blocks were expressions. */
8191 : : {
8192 : 219502 : putchar ('\n');
8193 : 219502 : print_ops (cbargs->dwflmod, cbargs->dbg,
8194 : 219502 : 12 + level * 2, 12 + level * 2,
8195 : : cbargs->version, cbargs->addrsize, cbargs->offset_size,
8196 : 219502 : attrp->cu, block.length, block.data);
8197 : : }
8198 : : else
8199 : 0 : print_block (block.length, block.data);
8200 : : break;
8201 : :
8202 : 8 : case DW_AT_discr_list:
8203 [ - + ]: 8 : if (block.length == 0)
8204 : 0 : puts ("<default>");
8205 [ + - ]: 8 : else if (form != DW_FORM_data16)
8206 : : {
8207 : 8 : const unsigned char *readp = block.data;
8208 : 8 : const unsigned char *readendp = readp + block.length;
8209 : :
8210 : : /* See if we are dealing with a signed or unsigned
8211 : : values. If the parent of this variant DIE is a
8212 : : variant_part then it will either have a discriminant
8213 : : which points to the member which type is the
8214 : : discriminant type. Or the variant_part itself has a
8215 : : type representing the discriminant. */
8216 : 8 : bool is_signed = false;
8217 [ + - ]: 8 : if (level > 0)
8218 : : {
8219 : 8 : Dwarf_Die *parent = &cbargs->dies[level - 1];
8220 [ + - ]: 8 : if (dwarf_tag (die) == DW_TAG_variant
8221 [ + - ]: 8 : && dwarf_tag (parent) == DW_TAG_variant_part)
8222 : : {
8223 : 8 : Dwarf_Die member;
8224 : 8 : Dwarf_Attribute discr_attr;
8225 : 8 : int bytes;
8226 [ + - ]: 8 : if (dwarf_formref_die (dwarf_attr (parent,
8227 : : DW_AT_discr,
8228 : : &discr_attr),
8229 : : &member) != NULL)
8230 : 8 : die_type_sign_bytes (&member, &is_signed, &bytes);
8231 : : else
8232 : 0 : die_type_sign_bytes (parent, &is_signed, &bytes);
8233 : : }
8234 : : }
8235 : 24 : while (readp < readendp)
8236 : : {
8237 : 16 : int d = (int) *readp++;
8238 : 16 : printf ("%s ", dwarf_discr_list_name (d));
8239 [ - + ]: 16 : if (readp >= readendp)
8240 : 0 : goto attrval_out;
8241 : :
8242 : 16 : Dwarf_Word val;
8243 : 16 : Dwarf_Sword sval;
8244 [ + + ]: 16 : if (d == DW_DSC_label)
8245 : : {
8246 [ + + ]: 8 : if (is_signed)
8247 : : {
8248 : 4 : get_sleb128 (sval, readp, readendp);
8249 : 4 : printf ("%" PRId64 "", sval);
8250 : : }
8251 : : else
8252 : : {
8253 : 4 : get_uleb128 (val, readp, readendp);
8254 : 4 : printf ("%" PRIu64 "", val);
8255 : : }
8256 : : }
8257 [ + - ]: 8 : else if (d == DW_DSC_range)
8258 : : {
8259 [ + + ]: 8 : if (is_signed)
8260 : : {
8261 : 6 : get_sleb128 (sval, readp, readendp);
8262 : 6 : printf ("%" PRId64 "..", sval);
8263 [ - + ]: 6 : if (readp >= readendp)
8264 : 0 : goto attrval_out;
8265 : 6 : get_sleb128 (sval, readp, readendp);
8266 : 6 : printf ("%" PRId64 "", sval);
8267 : : }
8268 : : else
8269 : : {
8270 : 2 : get_uleb128 (val, readp, readendp);
8271 : 2 : printf ("%" PRIu64 "..", val);
8272 [ - + ]: 2 : if (readp >= readendp)
8273 : 0 : goto attrval_out;
8274 : 2 : get_uleb128 (val, readp, readendp);
8275 : 2 : printf ("%" PRIu64 "", val);
8276 : : }
8277 : : }
8278 : : else
8279 : : {
8280 : 0 : print_block (readendp - readp, readp);
8281 : 0 : break;
8282 : : }
8283 [ + + ]: 16 : if (readp < readendp)
8284 [ + + ]: 32 : printf (", ");
8285 : : }
8286 : 8 : putchar ('\n');
8287 : : }
8288 : : else
8289 : 0 : print_block (block.length, block.data);
8290 : : break;
8291 : : }
8292 : : break;
8293 : :
8294 : 0 : default:
8295 [ # # ]: 0 : if (cbargs->silent)
8296 : : break;
8297 : 0 : printf (" %*s%-20s (%s) ???\n",
8298 : : (int) (level * 2), "", dwarf_attr_name (attr),
8299 : : dwarf_form_name (form));
8300 : : break;
8301 : : }
8302 : :
8303 : 7116410 : return DWARF_CB_OK;
8304 : : }
8305 : :
8306 : : static void
8307 : 206 : print_debug_units (Dwfl_Module *dwflmod,
8308 : : Ebl *ebl, GElf_Ehdr *ehdr __attribute__ ((unused)),
8309 : : Elf_Scn *scn, GElf_Shdr *shdr,
8310 : : Dwarf *dbg, bool debug_types)
8311 : : {
8312 [ + + + + ]: 206 : const bool silent = !(print_debug_sections & section_info) && !debug_types;
8313 : 206 : const char *secname = section_name (ebl, shdr);
8314 : :
8315 : : /* Check section actually exists. */
8316 [ + + ]: 206 : if (!silent)
8317 [ + - ]: 134 : if (get_debug_elf_data (dbg, ebl,
8318 : : debug_types ? IDX_debug_types : IDX_debug_info,
8319 : : scn) == NULL)
8320 : 0 : return;
8321 : :
8322 : 134 : if (!silent)
8323 : 134 : printf (_("\
8324 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n [Offset]\n"),
8325 : 134 : elf_ndxscn (scn), secname, (uint64_t) shdr->sh_offset);
8326 : :
8327 : : /* If the section is empty we don't have to do anything. */
8328 [ + + + - ]: 206 : if (!silent && shdr->sh_size == 0)
8329 : : return;
8330 : :
8331 : 206 : int maxdies = 20;
8332 : 206 : Dwarf_Die *dies = xmalloc (maxdies * sizeof (Dwarf_Die));
8333 : :
8334 : : /* New compilation unit. */
8335 : 206 : Dwarf_Half version;
8336 : :
8337 : 206 : Dwarf_Die result;
8338 : 206 : Dwarf_Off abbroffset;
8339 : 206 : uint8_t addrsize;
8340 : 206 : uint8_t offsize;
8341 : 206 : uint64_t unit_id;
8342 : 206 : Dwarf_Off subdie_off;
8343 : :
8344 : 206 : int unit_res;
8345 : 206 : Dwarf_CU *cu;
8346 : 206 : Dwarf_CU cu_mem;
8347 : 206 : uint8_t unit_type;
8348 : 206 : Dwarf_Die cudie;
8349 : :
8350 : : /* We cheat a little because we want to see only the CUs from .debug_info
8351 : : or .debug_types. We know the Dwarf_CU struct layout. Set it up at
8352 : : the end of .debug_info if we want .debug_types only. Check the returned
8353 : : Dwarf_CU is still in the expected section. */
8354 [ + + ]: 206 : if (debug_types)
8355 : : {
8356 : 2 : cu_mem.dbg = dbg;
8357 : 2 : cu_mem.end = dbg->sectiondata[IDX_debug_info]->d_size;
8358 : 2 : cu_mem.sec_idx = IDX_debug_info;
8359 : 2 : cu = &cu_mem;
8360 : : }
8361 : : else
8362 : 204 : cu = NULL;
8363 : :
8364 : : next_cu:
8365 : 3896 : unit_res = dwarf_get_units (dbg, cu, &cu, &version, &unit_type,
8366 : : &cudie, NULL);
8367 [ + + ]: 3896 : if (unit_res == 1)
8368 : 204 : goto do_return;
8369 : :
8370 [ - + ]: 3692 : if (unit_res == -1)
8371 : : {
8372 [ # # ]: 0 : if (!silent)
8373 : 0 : error (0, 0, _("cannot get next unit: %s"), dwarf_errmsg (-1));
8374 : 0 : goto do_return;
8375 : : }
8376 : :
8377 [ + + + + ]: 7380 : if (cu->sec_idx != (size_t) (debug_types ? IDX_debug_types : IDX_debug_info))
8378 : 2 : goto do_return;
8379 : :
8380 : 3690 : dwarf_cu_die (cu, &result, NULL, &abbroffset, &addrsize, &offsize,
8381 : : &unit_id, &subdie_off);
8382 : :
8383 [ + + ]: 3690 : if (!silent)
8384 : : {
8385 : 3590 : Dwarf_Off offset = cu->start;
8386 [ + + + - ]: 3590 : if (debug_types && version < 5)
8387 : 4 : {
8388 : 4 : Dwarf_Die typedie;
8389 : 4 : Dwarf_Off dieoffset;
8390 : 4 : dieoffset = dwarf_dieoffset (dwarf_offdie_types (dbg, cu->start
8391 : : + subdie_off,
8392 : : &typedie));
8393 : 4 : printf (_(" Type unit at offset %" PRIu64 ":\n"
8394 : : " Version: %" PRIu16
8395 : : ", Abbreviation section offset: %" PRIu64
8396 : : ", Address size: %" PRIu8
8397 : : ", Offset size: %" PRIu8
8398 : : "\n Type signature: %#" PRIx64
8399 : : ", Type offset: %#" PRIx64 " [%" PRIx64 "]\n"),
8400 : : (uint64_t) offset, version, abbroffset, addrsize, offsize,
8401 : : unit_id, (uint64_t) subdie_off, dieoffset);
8402 : : }
8403 : : else
8404 : : {
8405 : 3586 : printf (_(" Compilation unit at offset %" PRIu64 ":\n"
8406 : : " Version: %" PRIu16
8407 : : ", Abbreviation section offset: %" PRIu64
8408 : : ", Address size: %" PRIu8
8409 : : ", Offset size: %" PRIu8 "\n"),
8410 : : (uint64_t) offset, version, abbroffset, addrsize, offsize);
8411 : :
8412 [ + + ]: 3586 : if (version >= 5 || (unit_type != DW_UT_compile
8413 [ + + ]: 170 : && unit_type != DW_UT_partial))
8414 : : {
8415 : 3418 : printf (_(" Unit type: %s (%" PRIu8 ")"),
8416 : : dwarf_unit_name (unit_type), unit_type);
8417 : 3418 : if (unit_type == DW_UT_type
8418 [ + + ]: 3418 : || unit_type == DW_UT_skeleton
8419 [ + - ]: 3408 : || unit_type == DW_UT_split_compile
8420 [ - + ]: 3408 : || unit_type == DW_UT_split_type)
8421 : 10 : printf (", Unit id: 0x%.16" PRIx64 "", unit_id);
8422 : 3418 : if (unit_type == DW_UT_type
8423 [ - + ]: 3418 : || unit_type == DW_UT_split_type)
8424 : : {
8425 : 0 : Dwarf_Die typedie;
8426 : 0 : Dwarf_Off dieoffset;
8427 : 0 : dwarf_cu_info (cu, NULL, NULL, NULL, &typedie,
8428 : : NULL, NULL, NULL);
8429 : 0 : dieoffset = dwarf_dieoffset (&typedie);
8430 : 0 : printf (", Unit DIE off: %#" PRIx64 " [%" PRIx64 "]",
8431 : : subdie_off, dieoffset);
8432 : : }
8433 : 3418 : printf ("\n");
8434 : : }
8435 : : }
8436 : : }
8437 : :
8438 [ + - ]: 3690 : if (version < 2 || version > 5
8439 [ + - - + ]: 3690 : || unit_type < DW_UT_compile || unit_type > DW_UT_split_type)
8440 : : {
8441 [ # # ]: 0 : if (!silent)
8442 : 0 : error (0, 0, _("unknown version (%d) or unit type (%d)"),
8443 : : version, unit_type);
8444 : 0 : goto next_cu;
8445 : : }
8446 : :
8447 : 3690 : struct attrcb_args args =
8448 : : {
8449 : : .dwflmod = dwflmod,
8450 : : .silent = silent,
8451 : : .version = version,
8452 : : .addrsize = addrsize,
8453 : : .offset_size = offsize
8454 : : };
8455 : :
8456 : 3690 : bool is_split = false;
8457 : 3690 : int level = 0;
8458 : 3690 : dies[0] = cudie;
8459 : 3690 : args.cu = dies[0].cu;
8460 : 3690 : args.dbg = dbg;
8461 : 3690 : args.is_split = is_split;
8462 : :
8463 : : /* We might return here again for the split CU subdie. */
8464 : : do_cu:
8465 : 2078950 : do
8466 : : {
8467 : 2078950 : Dwarf_Off offset = dwarf_dieoffset (&dies[level]);
8468 [ - + ]: 2078950 : if (unlikely (offset == (Dwarf_Off) -1))
8469 : : {
8470 [ # # ]: 0 : if (!silent)
8471 : 0 : error (0, 0, _("cannot get DIE offset: %s"),
8472 : : dwarf_errmsg (-1));
8473 : 0 : goto do_return;
8474 : : }
8475 : :
8476 : 2078950 : int tag = dwarf_tag (&dies[level]);
8477 [ - + ]: 2078950 : if (unlikely (tag == DW_TAG_invalid))
8478 : : {
8479 [ # # ]: 0 : if (!silent)
8480 : 0 : error (0, 0, _("cannot get tag of DIE at offset [%" PRIx64
8481 : : "] in section '%s': %s"),
8482 : : (uint64_t) offset, secname, dwarf_errmsg (-1));
8483 : 0 : goto do_return;
8484 : : }
8485 : :
8486 [ + + ]: 2078950 : if (!silent)
8487 : : {
8488 : 2076964 : unsigned int code = dwarf_getabbrevcode (dies[level].abbrev);
8489 [ + + ]: 2076964 : if (is_split)
8490 : 96 : printf (" {%6" PRIx64 "} ", (uint64_t) offset);
8491 : : else
8492 : 2076868 : printf (" [%6" PRIx64 "] ", (uint64_t) offset);
8493 : 2076964 : printf ("%*s%-20s abbrev: %u\n", (int) (level * 2), "",
8494 : : dwarf_tag_name (tag), code);
8495 : : }
8496 : :
8497 : : /* Print the attribute values. */
8498 : 2078950 : args.level = level;
8499 : 2078950 : args.dies = dies;
8500 : 2078950 : (void) dwarf_getattrs (&dies[level], attr_callback, &args, 0);
8501 : :
8502 : : /* Make room for the next level's DIE. */
8503 [ - + ]: 2078950 : if (level + 1 == maxdies)
8504 : 0 : dies = xrealloc (dies, (maxdies += 10) * sizeof (Dwarf_Die));
8505 : :
8506 : 2078950 : int res = dwarf_child (&dies[level], &dies[level + 1]);
8507 [ + + ]: 2078950 : if (res > 0)
8508 : : {
8509 [ + + ]: 2078950 : while ((res = dwarf_siblingof (&dies[level], &dies[level])) == 1)
8510 [ + + ]: 288544 : if (level-- == 0)
8511 : : break;
8512 : :
8513 [ - + ]: 1794104 : if (unlikely (res == -1))
8514 : : {
8515 [ # # ]: 0 : if (!silent)
8516 : 0 : error (0, 0, _("cannot get next DIE: %s\n"),
8517 : : dwarf_errmsg (-1));
8518 : 0 : goto do_return;
8519 : : }
8520 : : }
8521 [ - + ]: 284846 : else if (unlikely (res < 0))
8522 : : {
8523 [ # # ]: 0 : if (!silent)
8524 : 0 : error (0, 0, _("cannot get next DIE: %s"),
8525 : : dwarf_errmsg (-1));
8526 : 0 : goto do_return;
8527 : : }
8528 : : else
8529 : : ++level;
8530 : : }
8531 [ + + ]: 2078950 : while (level >= 0);
8532 : :
8533 : : /* We might want to show the split compile unit if this was a skeleton.
8534 : : We need to scan it if we are requesting printing .debug_ranges for
8535 : : DWARF4 since GNU DebugFission uses "offsets" into the main ranges
8536 : : section. */
8537 [ + + ]: 3698 : if (unit_type == DW_UT_skeleton
8538 [ + + + + ]: 26 : && ((!silent && show_split_units)
8539 [ + + + + ]: 20 : || (version < 5 && (print_debug_sections & section_ranges) != 0)))
8540 : : {
8541 : 10 : Dwarf_Die subdie;
8542 [ + - ]: 10 : if (dwarf_cu_info (cu, NULL, NULL, NULL, &subdie, NULL, NULL, NULL) != 0
8543 [ + + ]: 10 : || dwarf_tag (&subdie) == DW_TAG_invalid)
8544 : : {
8545 [ + - ]: 2 : if (!silent)
8546 : : {
8547 : 2 : Dwarf_Attribute dwo_at;
8548 : 4 : const char *dwo_name =
8549 : 2 : (dwarf_formstring (dwarf_attr (&cudie, DW_AT_dwo_name,
8550 : : &dwo_at))
8551 [ - + ]: 2 : ?: (dwarf_formstring (dwarf_attr (&cudie, DW_AT_GNU_dwo_name,
8552 : : &dwo_at))
8553 [ # # ]: 0 : ?: "<unknown>"));
8554 : 2 : fprintf (stderr,
8555 : : "Could not find split unit '%s', id: %" PRIx64 "\n",
8556 : : dwo_name, unit_id);
8557 : : }
8558 : : }
8559 : : else
8560 : : {
8561 : 8 : Dwarf_CU *split_cu = subdie.cu;
8562 : 8 : dwarf_cu_die (split_cu, &result, NULL, &abbroffset,
8563 : : &addrsize, &offsize, &unit_id, &subdie_off);
8564 : 8 : Dwarf_Off offset = cu->start;
8565 : :
8566 [ + + ]: 8 : if (!silent)
8567 : : {
8568 : 4 : printf (_(" Split compilation unit at offset %"
8569 : : PRIu64 ":\n"
8570 : : " Version: %" PRIu16
8571 : : ", Abbreviation section offset: %" PRIu64
8572 : : ", Address size: %" PRIu8
8573 : : ", Offset size: %" PRIu8 "\n"),
8574 : : (uint64_t) offset, version, abbroffset,
8575 : : addrsize, offsize);
8576 : 4 : printf (_(" Unit type: %s (%" PRIu8 ")"),
8577 : : dwarf_unit_name (unit_type), unit_type);
8578 : 4 : printf (", Unit id: 0x%.16" PRIx64 "", unit_id);
8579 : 4 : printf ("\n");
8580 : : }
8581 : :
8582 : 8 : unit_type = DW_UT_split_compile;
8583 : 8 : is_split = true;
8584 : 8 : level = 0;
8585 : 8 : dies[0] = subdie;
8586 : 8 : args.cu = dies[0].cu;
8587 : 8 : args.dbg = split_cu->dbg;
8588 : 8 : args.is_split = is_split;
8589 : 8 : goto do_cu;
8590 : : }
8591 : : }
8592 : :
8593 : : /* And again... */
8594 : 3690 : goto next_cu;
8595 : :
8596 : 206 : do_return:
8597 : 206 : free (dies);
8598 : : }
8599 : :
8600 : : static void
8601 : 204 : print_debug_info_section (Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr,
8602 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
8603 : : {
8604 : 40 : print_debug_units (dwflmod, ebl, ehdr, scn, shdr, dbg, false);
8605 : 40 : }
8606 : :
8607 : : static void
8608 : 2 : print_debug_types_section (Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr,
8609 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
8610 : : {
8611 : 2 : print_debug_units (dwflmod, ebl, ehdr, scn, shdr, dbg, true);
8612 : 2 : }
8613 : :
8614 : :
8615 : : static void
8616 : 82 : print_decoded_line_section (Dwfl_Module *dwflmod, Ebl *ebl,
8617 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
8618 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
8619 : : {
8620 : 82 : printf (_("\
8621 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n\n"),
8622 : : elf_ndxscn (scn), section_name (ebl, shdr),
8623 : 82 : (uint64_t) shdr->sh_offset);
8624 : :
8625 : 164 : size_t address_size
8626 [ + + ]: 82 : = elf_getident (ebl->elf, NULL)[EI_CLASS] == ELFCLASS32 ? 4 : 8;
8627 : :
8628 : 82 : Dwarf_Lines *lines;
8629 : 82 : size_t nlines;
8630 : 82 : Dwarf_Off off, next_off = 0;
8631 : 82 : Dwarf_CU *cu = NULL;
8632 : 82 : while (dwarf_next_lines (dbg, off = next_off, &next_off, &cu, NULL, NULL,
8633 [ + + ]: 176 : &lines, &nlines) == 0)
8634 : : {
8635 : 94 : Dwarf_Die cudie;
8636 [ + + + - ]: 94 : if (cu != NULL && dwarf_cu_info (cu, NULL, NULL, &cudie,
8637 : : NULL, NULL, NULL, NULL) == 0)
8638 : 50 : printf (" CU [%" PRIx64 "] %s\n",
8639 : : dwarf_dieoffset (&cudie), dwarf_diename (&cudie));
8640 : : else
8641 : : {
8642 : : /* DWARF5 lines can be independent of any CU, but they probably
8643 : : are used by some CU. Determine the CU this block is for. */
8644 : 44 : Dwarf_Off cuoffset;
8645 : 44 : Dwarf_Off ncuoffset = 0;
8646 : 44 : size_t hsize;
8647 : 44 : while (dwarf_nextcu (dbg, cuoffset = ncuoffset, &ncuoffset, &hsize,
8648 [ + - ]: 46 : NULL, NULL, NULL) == 0)
8649 : : {
8650 [ - + ]: 46 : if (dwarf_offdie (dbg, cuoffset + hsize, &cudie) == NULL)
8651 : 0 : continue;
8652 : 46 : Dwarf_Attribute stmt_list;
8653 [ - + ]: 46 : if (dwarf_attr (&cudie, DW_AT_stmt_list, &stmt_list) == NULL)
8654 : 0 : continue;
8655 : 46 : Dwarf_Word lineoff;
8656 [ - + ]: 46 : if (dwarf_formudata (&stmt_list, &lineoff) != 0)
8657 : 0 : continue;
8658 [ + + ]: 46 : if (lineoff == off)
8659 : : {
8660 : : /* Found the CU. */
8661 : 44 : cu = cudie.cu;
8662 : 44 : break;
8663 : : }
8664 : : }
8665 : :
8666 [ + - ]: 44 : if (cu != NULL)
8667 : 44 : printf (" CU [%" PRIx64 "] %s\n",
8668 : : dwarf_dieoffset (&cudie), dwarf_diename (&cudie));
8669 : : else
8670 : 44 : printf (" No CU\n");
8671 : : }
8672 : :
8673 : 94 : printf (" line:col SBPE* disc isa op address"
8674 : : " (Statement Block Prologue Epilogue *End)\n");
8675 : 94 : const char *last_file = "";
8676 [ + + ]: 720 : for (size_t n = 0; n < nlines; n++)
8677 : : {
8678 : 626 : Dwarf_Line *line = dwarf_onesrcline (lines, n);
8679 [ - + ]: 626 : if (line == NULL)
8680 : : {
8681 : 0 : printf (" dwarf_onesrcline: %s\n", dwarf_errmsg (-1));
8682 : 0 : continue;
8683 : : }
8684 : 626 : Dwarf_Word mtime, length;
8685 : 626 : const char *file = dwarf_linesrc (line, &mtime, &length);
8686 [ - + ]: 626 : if (file == NULL)
8687 : : {
8688 : 0 : printf (" <%s> (mtime: ?, length: ?)\n", dwarf_errmsg (-1));
8689 : 0 : last_file = "";
8690 : : }
8691 [ + + ]: 626 : else if (strcmp (last_file, file) != 0)
8692 : : {
8693 : 114 : printf (" %s (mtime: %" PRIu64 ", length: %" PRIu64 ")\n",
8694 : : file, mtime, length);
8695 : 114 : last_file = file;
8696 : : }
8697 : :
8698 : 626 : int lineno, colno;
8699 : 626 : bool statement, endseq, block, prologue_end, epilogue_begin;
8700 : 626 : unsigned int lineop, isa, disc;
8701 : 626 : Dwarf_Addr address;
8702 : 626 : dwarf_lineaddr (line, &address);
8703 : 626 : dwarf_lineno (line, &lineno);
8704 : 626 : dwarf_linecol (line, &colno);
8705 : 626 : dwarf_lineop_index (line, &lineop);
8706 : 626 : dwarf_linebeginstatement (line, &statement);
8707 : 626 : dwarf_lineendsequence (line, &endseq);
8708 : 626 : dwarf_lineblock (line, &block);
8709 : 626 : dwarf_lineprologueend (line, &prologue_end);
8710 : 626 : dwarf_lineepiloguebegin (line, &epilogue_begin);
8711 : 626 : dwarf_lineisa (line, &isa);
8712 : 626 : dwarf_linediscriminator (line, &disc);
8713 : :
8714 : : /* End sequence is special, it is one byte past. */
8715 : 626 : printf (" %4d:%-3d %c%c%c%c%c %4d %3d %2d ",
8716 : : lineno, colno,
8717 [ + + ]: 626 : (statement ? 'S' : ' '),
8718 [ + - ]: 626 : (block ? 'B' : ' '),
8719 [ + + ]: 626 : (prologue_end ? 'P' : ' '),
8720 [ + - ]: 626 : (epilogue_begin ? 'E' : ' '),
8721 [ + + ]: 626 : (endseq ? '*' : ' '),
8722 : : disc, isa, lineop);
8723 : 626 : print_dwarf_addr (dwflmod, address_size,
8724 [ + + ]: 626 : address - (endseq ? 1 : 0), address);
8725 : 626 : printf ("\n");
8726 : :
8727 [ + + ]: 626 : if (endseq)
8728 : 626 : printf("\n");
8729 : : }
8730 : : }
8731 : 82 : }
8732 : :
8733 : :
8734 : : /* Print the value of a form.
8735 : : Returns new value of readp, or readendp on failure. */
8736 : : static const unsigned char *
8737 : 141226 : print_form_data (Dwarf *dbg, int form, const unsigned char *readp,
8738 : : const unsigned char *readendp, unsigned int offset_len,
8739 : : Dwarf_Off str_offsets_base)
8740 : : {
8741 : 141226 : Dwarf_Word val;
8742 : 141226 : unsigned char *endp;
8743 : 141226 : Elf_Data *data;
8744 : 141226 : char *str;
8745 [ + - - - : 141226 : switch (form)
- + - - -
- + - - +
- + - - -
- - ]
8746 : : {
8747 : 16 : case DW_FORM_data1:
8748 [ - + ]: 16 : if (readendp - readp < 1)
8749 : : {
8750 : 0 : invalid_data:
8751 : 0 : error (0, 0, "invalid data");
8752 : 0 : return readendp;
8753 : : }
8754 : 16 : val = *readp++;
8755 : 16 : printf (" %" PRIx8, (unsigned int) val);
8756 : : break;
8757 : :
8758 : 0 : case DW_FORM_data2:
8759 [ # # ]: 0 : if (readendp - readp < 2)
8760 : 0 : goto invalid_data;
8761 [ # # ]: 0 : val = read_2ubyte_unaligned_inc (dbg, readp);
8762 : 0 : printf(" %" PRIx16, (unsigned int) val);
8763 : : break;
8764 : :
8765 : 0 : case DW_FORM_data4:
8766 [ # # ]: 0 : if (readendp - readp < 4)
8767 : 0 : goto invalid_data;
8768 [ # # ]: 0 : val = read_4ubyte_unaligned_inc (dbg, readp);
8769 : 0 : printf (" %" PRIx32, (unsigned int) val);
8770 : : break;
8771 : :
8772 : 0 : case DW_FORM_data8:
8773 [ # # ]: 0 : if (readendp - readp < 8)
8774 : 0 : goto invalid_data;
8775 [ # # ]: 0 : val = read_8ubyte_unaligned_inc (dbg, readp);
8776 : 0 : printf (" %" PRIx64, val);
8777 : : break;
8778 : :
8779 : 0 : case DW_FORM_sdata:
8780 [ # # ]: 0 : if (readendp - readp < 1)
8781 : 0 : goto invalid_data;
8782 : 0 : get_sleb128 (val, readp, readendp);
8783 : 0 : printf (" %" PRIx64, val);
8784 : : break;
8785 : :
8786 : 56746 : case DW_FORM_udata:
8787 [ - + ]: 56746 : if (readendp - readp < 1)
8788 : 0 : goto invalid_data;
8789 : 56746 : get_uleb128 (val, readp, readendp);
8790 : 56746 : printf (" %" PRIx64, val);
8791 : : break;
8792 : :
8793 : 0 : case DW_FORM_block:
8794 [ # # ]: 0 : if (readendp - readp < 1)
8795 : 0 : goto invalid_data;
8796 : 0 : get_uleb128 (val, readp, readendp);
8797 [ # # ]: 0 : if ((size_t) (readendp - readp) < val)
8798 : 0 : goto invalid_data;
8799 : 0 : print_bytes (val, readp);
8800 : 0 : readp += val;
8801 : 0 : break;
8802 : :
8803 : 0 : case DW_FORM_block1:
8804 [ # # ]: 0 : if (readendp - readp < 1)
8805 : 0 : goto invalid_data;
8806 : 0 : val = *readp++;
8807 [ # # ]: 0 : if ((size_t) (readendp - readp) < val)
8808 : 0 : goto invalid_data;
8809 : 0 : print_bytes (val, readp);
8810 : 0 : readp += val;
8811 : 0 : break;
8812 : :
8813 : 0 : case DW_FORM_block2:
8814 [ # # ]: 0 : if (readendp - readp < 2)
8815 : 0 : goto invalid_data;
8816 [ # # ]: 0 : val = read_2ubyte_unaligned_inc (dbg, readp);
8817 [ # # ]: 0 : if ((size_t) (readendp - readp) < val)
8818 : 0 : goto invalid_data;
8819 : 0 : print_bytes (val, readp);
8820 : 0 : readp += val;
8821 : 0 : break;
8822 : :
8823 : 0 : case DW_FORM_block4:
8824 [ # # ]: 0 : if (readendp - readp < 4)
8825 : 0 : goto invalid_data;
8826 [ # # ]: 0 : val = read_4ubyte_unaligned_inc (dbg, readp);
8827 [ # # ]: 0 : if ((size_t) (readendp - readp) < val)
8828 : 0 : goto invalid_data;
8829 : 0 : print_bytes (val, readp);
8830 : 0 : readp += val;
8831 : 0 : break;
8832 : :
8833 : 24 : case DW_FORM_data16:
8834 [ - + ]: 24 : if (readendp - readp < 16)
8835 : 0 : goto invalid_data;
8836 : 24 : print_bytes (16, readp);
8837 : 24 : readp += 16;
8838 : 24 : break;
8839 : :
8840 : 0 : case DW_FORM_flag:
8841 [ # # ]: 0 : if (readendp - readp < 1)
8842 : 0 : goto invalid_data;
8843 : 0 : val = *readp++;
8844 [ # # ]: 0 : printf ("%s", val != 0 ? yes_str : no_str);
8845 : : break;
8846 : :
8847 : 0 : case DW_FORM_string:
8848 : 0 : endp = memchr (readp, '\0', readendp - readp);
8849 [ # # ]: 0 : if (endp == NULL)
8850 : 0 : goto invalid_data;
8851 : 0 : printf ("%s", readp);
8852 : 0 : readp = endp + 1;
8853 : 0 : break;
8854 : :
8855 : 82884 : case DW_FORM_strp:
8856 : : case DW_FORM_line_strp:
8857 : : case DW_FORM_strp_sup:
8858 [ - + ]: 82884 : if ((size_t) (readendp - readp) < offset_len)
8859 : 0 : goto invalid_data;
8860 [ - + ]: 82884 : if (offset_len == 8)
8861 [ # # ]: 0 : val = read_8ubyte_unaligned_inc (dbg, readp);
8862 : : else
8863 [ - + ]: 82884 : val = read_4ubyte_unaligned_inc (dbg, readp);
8864 [ - + ]: 82884 : if (form == DW_FORM_strp)
8865 : 0 : data = dbg->sectiondata[IDX_debug_str];
8866 [ + - ]: 82884 : else if (form == DW_FORM_line_strp)
8867 : 82884 : data = dbg->sectiondata[IDX_debug_line_str];
8868 : : else /* form == DW_FORM_strp_sup */
8869 : : {
8870 : 0 : Dwarf *alt = dwarf_getalt (dbg);
8871 [ # # ]: 0 : data = alt != NULL ? alt->sectiondata[IDX_debug_str] : NULL;
8872 : : }
8873 [ + - - + ]: 82884 : if (data == NULL || val >= data->d_size
8874 [ - + ]: 82884 : || memchr (data->d_buf + val, '\0', data->d_size - val) == NULL)
8875 : : str = "???";
8876 : : else
8877 : 82884 : str = (char *) data->d_buf + val;
8878 : 82884 : printf ("%s (%" PRIu64 ")", str, val);
8879 : : break;
8880 : :
8881 : 0 : case DW_FORM_sec_offset:
8882 [ # # ]: 0 : if ((size_t) (readendp - readp) < offset_len)
8883 : 0 : goto invalid_data;
8884 [ # # ]: 0 : if (offset_len == 8)
8885 [ # # ]: 0 : val = read_8ubyte_unaligned_inc (dbg, readp);
8886 : : else
8887 [ # # ]: 0 : val = read_4ubyte_unaligned_inc (dbg, readp);
8888 : 0 : printf ("[%" PRIx64 "]", val);
8889 : : break;
8890 : :
8891 : 1556 : case DW_FORM_strx:
8892 : : case DW_FORM_GNU_str_index:
8893 [ - + ]: 1556 : if (readendp - readp < 1)
8894 : 0 : goto invalid_data;
8895 : 1556 : get_uleb128 (val, readp, readendp);
8896 : 1556 : strx_val:
8897 : 1556 : data = dbg->sectiondata[IDX_debug_str_offsets];
8898 [ + - ]: 1556 : if (data == NULL
8899 [ - + ]: 1556 : || data->d_size - str_offsets_base < val * offset_len)
8900 : : str = "???";
8901 : : else
8902 : : {
8903 : 1556 : const unsigned char *strreadp = (data->d_buf + str_offsets_base
8904 : 1556 : + val * offset_len);
8905 : 1556 : const unsigned char *strreadendp = data->d_buf + data->d_size;
8906 [ - + ]: 1556 : if ((size_t) (strreadendp - strreadp) < offset_len)
8907 : : str = "???";
8908 : : else
8909 : : {
8910 : 1556 : Dwarf_Off idx;
8911 [ - + ]: 1556 : if (offset_len == 8)
8912 [ # # ]: 0 : idx = read_8ubyte_unaligned (dbg, strreadp);
8913 : : else
8914 [ - + ]: 1556 : idx = read_4ubyte_unaligned (dbg, strreadp);
8915 : :
8916 : 1556 : data = dbg->sectiondata[IDX_debug_str];
8917 [ - + - + ]: 1556 : if (data == NULL || idx >= data->d_size
8918 [ - + ]: 1556 : || memchr (data->d_buf + idx, '\0',
8919 : : data->d_size - idx) == NULL)
8920 : : str = "???";
8921 : : else
8922 : 1556 : str = (char *) data->d_buf + idx;
8923 : : }
8924 : : }
8925 : 1556 : printf ("%s (%" PRIu64 ")", str, val);
8926 : : break;
8927 : :
8928 : 0 : case DW_FORM_strx1:
8929 [ # # ]: 0 : if (readendp - readp < 1)
8930 : 0 : goto invalid_data;
8931 : 0 : val = *readp++;
8932 : 0 : goto strx_val;
8933 : :
8934 : 0 : case DW_FORM_strx2:
8935 [ # # ]: 0 : if (readendp - readp < 2)
8936 : 0 : goto invalid_data;
8937 [ # # ]: 0 : val = read_2ubyte_unaligned_inc (dbg, readp);
8938 : 0 : goto strx_val;
8939 : :
8940 : 0 : case DW_FORM_strx3:
8941 [ # # ]: 0 : if (readendp - readp < 3)
8942 : 0 : goto invalid_data;
8943 : 0 : val = read_3ubyte_unaligned_inc (dbg, readp);
8944 : 0 : goto strx_val;
8945 : :
8946 : 0 : case DW_FORM_strx4:
8947 [ # # ]: 0 : if (readendp - readp < 4)
8948 : 0 : goto invalid_data;
8949 [ # # ]: 0 : val = read_4ubyte_unaligned_inc (dbg, readp);
8950 : 0 : goto strx_val;
8951 : :
8952 : 0 : default:
8953 : 0 : error (0, 0, _("unknown form: %s"), dwarf_form_name (form));
8954 : 0 : return readendp;
8955 : : }
8956 : :
8957 : 141226 : return readp;
8958 : : }
8959 : :
8960 : : /* Only used via run_advance_pc() macro */
8961 : : static inline void
8962 : 744880 : run_advance_pc (unsigned int op_advance,
8963 : : unsigned int minimum_instr_len,
8964 : : unsigned int max_ops_per_instr,
8965 : : unsigned int *op_addr_advance,
8966 : : Dwarf_Word *address,
8967 : : unsigned int *op_index)
8968 : : {
8969 : 744880 : const unsigned int advanced_op_index = (*op_index) + op_advance;
8970 : :
8971 : 744880 : *op_addr_advance = minimum_instr_len * (advanced_op_index
8972 : 744880 : / max_ops_per_instr);
8973 : 744880 : *address = *address + *op_addr_advance;
8974 : 744880 : *op_index = advanced_op_index % max_ops_per_instr;
8975 : : }
8976 : :
8977 : : static void
8978 : 186 : print_debug_line_section (Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr,
8979 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
8980 : : {
8981 [ + + ]: 186 : if (decodedline)
8982 : : {
8983 : 82 : print_decoded_line_section (dwflmod, ebl, ehdr, scn, shdr, dbg);
8984 : 164 : return;
8985 : : }
8986 : :
8987 : 104 : Elf_Data *data = get_debug_elf_data (dbg, ebl, IDX_debug_line, scn);
8988 [ + - ]: 104 : if (data == NULL)
8989 : : return;
8990 : :
8991 : 104 : printf (_("\
8992 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
8993 : : elf_ndxscn (scn), section_name (ebl, shdr),
8994 : 104 : (uint64_t) shdr->sh_offset);
8995 : :
8996 [ + - ]: 104 : if (shdr->sh_size == 0)
8997 : : return;
8998 : :
8999 : : /* There is no functionality in libdw to read the information in the
9000 : : way it is represented here. Hardcode the decoder. */
9001 : :
9002 : 104 : const unsigned char *linep = (const unsigned char *) data->d_buf;
9003 : 104 : const unsigned char *lineendp;
9004 : :
9005 : 104 : while (linep
9006 [ + + ]: 3666 : < (lineendp = (const unsigned char *) data->d_buf + data->d_size))
9007 : : {
9008 : 3562 : size_t start_offset = linep - (const unsigned char *) data->d_buf;
9009 : :
9010 : 3562 : printf (_("\nTable at offset %zu:\n"), start_offset);
9011 : :
9012 [ - + ]: 3562 : if (unlikely (linep + 4 > lineendp))
9013 : 0 : goto invalid_data;
9014 [ + + ]: 3562 : Dwarf_Word unit_length = read_4ubyte_unaligned_inc (dbg, linep);
9015 : 3562 : unsigned int length = 4;
9016 [ - + ]: 3562 : if (unlikely (unit_length == 0xffffffff))
9017 : : {
9018 [ # # ]: 0 : if (unlikely (linep + 8 > lineendp))
9019 : : {
9020 : 0 : invalid_data:
9021 : 0 : error (0, 0, _("invalid data in section [%zu] '%s'"),
9022 : : elf_ndxscn (scn), section_name (ebl, shdr));
9023 : 0 : return;
9024 : : }
9025 [ # # ]: 0 : unit_length = read_8ubyte_unaligned_inc (dbg, linep);
9026 : 0 : length = 8;
9027 : : }
9028 : :
9029 : : /* Check whether we have enough room in the section. */
9030 [ - + ]: 3562 : if (unlikely (unit_length > (size_t) (lineendp - linep)))
9031 : 0 : goto invalid_data;
9032 : 3562 : lineendp = linep + unit_length;
9033 : :
9034 : : /* The next element of the header is the version identifier. */
9035 [ - + ]: 3562 : if ((size_t) (lineendp - linep) < 2)
9036 : 0 : goto invalid_data;
9037 [ + + ]: 3562 : uint_fast16_t version = read_2ubyte_unaligned_inc (dbg, linep);
9038 : :
9039 : 7124 : size_t address_size
9040 [ + + ]: 3562 : = elf_getident (ebl->elf, NULL)[EI_CLASS] == ELFCLASS32 ? 4 : 8;
9041 : 3562 : unsigned char segment_selector_size = 0;
9042 [ + + ]: 3562 : if (version > 4)
9043 : : {
9044 [ - + ]: 3410 : if ((size_t) (lineendp - linep) < 2)
9045 : 0 : goto invalid_data;
9046 : 3410 : address_size = *linep++;
9047 : 3410 : segment_selector_size = *linep++;
9048 : : }
9049 : :
9050 : : /* Next comes the header length. */
9051 : 3562 : Dwarf_Word header_length;
9052 [ + - ]: 3562 : if (length == 4)
9053 : : {
9054 [ - + ]: 3562 : if ((size_t) (lineendp - linep) < 4)
9055 : 0 : goto invalid_data;
9056 [ + + ]: 3562 : header_length = read_4ubyte_unaligned_inc (dbg, linep);
9057 : : }
9058 : : else
9059 : : {
9060 [ # # ]: 0 : if ((size_t) (lineendp - linep) < 8)
9061 : 0 : goto invalid_data;
9062 [ # # ]: 0 : header_length = read_8ubyte_unaligned_inc (dbg, linep);
9063 : : }
9064 : :
9065 : 3562 : const unsigned char *header_start = linep;
9066 : :
9067 : : /* Next the minimum instruction length. */
9068 [ - + ]: 3562 : if ((size_t) (lineendp - linep) < 1)
9069 : 0 : goto invalid_data;
9070 : 3562 : uint_fast8_t minimum_instr_len = *linep++;
9071 : :
9072 : : /* Next the maximum operations per instruction, in version 4 format. */
9073 : 3562 : uint_fast8_t max_ops_per_instr;
9074 [ + + ]: 3562 : if (version < 4)
9075 : : max_ops_per_instr = 1;
9076 : : else
9077 : : {
9078 [ - + ]: 3430 : if ((size_t) (lineendp - linep) < 1)
9079 : 0 : goto invalid_data;
9080 : 3430 : max_ops_per_instr = *linep++;
9081 : : }
9082 : :
9083 : : /* We need at least 4 more bytes. */
9084 [ - + ]: 3562 : if ((size_t) (lineendp - linep) < 4)
9085 : 0 : goto invalid_data;
9086 : :
9087 : : /* Then the flag determining the default value of the is_stmt
9088 : : register. */
9089 : 3562 : uint_fast8_t default_is_stmt = *linep++;
9090 : :
9091 : : /* Now the line base. */
9092 : 3562 : int_fast8_t line_base = *linep++;
9093 : :
9094 : : /* And the line range. */
9095 : 3562 : uint_fast8_t line_range = *linep++;
9096 : :
9097 : : /* The opcode base. */
9098 : 3562 : uint_fast8_t opcode_base = *linep++;
9099 : :
9100 : : /* Print what we got so far. */
9101 : 3562 : printf (_("\n"
9102 : : " Length: %" PRIu64 "\n"
9103 : : " DWARF version: %" PRIuFAST16 "\n"
9104 : : " Prologue length: %" PRIu64 "\n"
9105 : : " Address size: %zd\n"
9106 : : " Segment selector size: %zd\n"
9107 : : " Min instruction length: %" PRIuFAST8 "\n"
9108 : : " Max operations per instruction: %" PRIuFAST8 "\n"
9109 : : " Initial value if 'is_stmt': %" PRIuFAST8 "\n"
9110 : : " Line base: %" PRIdFAST8 "\n"
9111 : : " Line range: %" PRIuFAST8 "\n"
9112 : : " Opcode base: %" PRIuFAST8 "\n"
9113 : : "\n"
9114 : : "Opcodes:\n"),
9115 : : (uint64_t) unit_length, version, (uint64_t) header_length,
9116 : : address_size, (size_t) segment_selector_size,
9117 : : minimum_instr_len, max_ops_per_instr,
9118 : : default_is_stmt, line_base,
9119 : : line_range, opcode_base);
9120 : :
9121 [ - + ]: 3562 : if (version < 2 || version > 5)
9122 : : {
9123 : 0 : error (0, 0, _("cannot handle .debug_line version: %u\n"),
9124 : : (unsigned int) version);
9125 : 0 : linep = lineendp;
9126 : 0 : continue;
9127 : : }
9128 : :
9129 [ - + ]: 3562 : if (address_size != 4 && address_size != 8)
9130 : : {
9131 : 0 : error (0, 0, _("cannot handle address size: %u\n"),
9132 : : (unsigned int) address_size);
9133 : 0 : linep = lineendp;
9134 : 0 : continue;
9135 : : }
9136 : :
9137 [ - + ]: 3562 : if (segment_selector_size != 0)
9138 : : {
9139 : 0 : error (0, 0, _("cannot handle segment selector size: %u\n"),
9140 : : (unsigned int) segment_selector_size);
9141 : 0 : linep = lineendp;
9142 : 0 : continue;
9143 : : }
9144 : :
9145 [ - + ]: 3562 : if (unlikely (linep + opcode_base - 1 >= lineendp))
9146 : : {
9147 : 0 : invalid_unit:
9148 : 0 : error (0, 0,
9149 : 0 : _("invalid data at offset %tu in section [%zu] '%s'"),
9150 : 0 : linep - (const unsigned char *) data->d_buf,
9151 : : elf_ndxscn (scn), section_name (ebl, shdr));
9152 : 0 : linep = lineendp;
9153 : 0 : continue;
9154 : : }
9155 : 3562 : int opcode_base_l10 = 1;
9156 : 3562 : unsigned int tmp = opcode_base;
9157 [ + + ]: 7118 : while (tmp > 10)
9158 : : {
9159 : 3556 : tmp /= 10;
9160 : 3556 : ++opcode_base_l10;
9161 : : }
9162 : : const uint8_t *standard_opcode_lengths = linep - 1;
9163 [ + + ]: 46288 : for (uint_fast8_t cnt = 1; cnt < opcode_base; ++cnt)
9164 : 42726 : printf (ngettext (" [%*" PRIuFAST8 "] %hhu argument\n",
9165 : : " [%*" PRIuFAST8 "] %hhu arguments\n",
9166 : : (int) linep[cnt - 1]),
9167 : 42726 : opcode_base_l10, cnt, linep[cnt - 1]);
9168 : 3562 : linep += opcode_base - 1;
9169 : :
9170 : 3562 : if (unlikely (linep >= lineendp))
9171 : : goto invalid_unit;
9172 : :
9173 : 3562 : Dwarf_Off str_offsets_base = str_offsets_base_off (dbg, NULL);
9174 : :
9175 : 3562 : puts (_("\nDirectory table:"));
9176 [ + + ]: 3562 : if (version > 4)
9177 : : {
9178 : 3410 : struct encpair { uint16_t desc; uint16_t form; };
9179 : 3410 : struct encpair enc[256];
9180 : :
9181 : 3410 : printf (_(" ["));
9182 [ - + ]: 3410 : if ((size_t) (lineendp - linep) < 1)
9183 : 0 : goto invalid_data;
9184 : 3410 : unsigned char directory_entry_format_count = *linep++;
9185 : 3410 : for (int i = 0; i < directory_entry_format_count; i++)
9186 : : {
9187 : 3410 : uint16_t desc, form;
9188 [ - + ]: 3410 : if ((size_t) (lineendp - linep) < 1)
9189 : 0 : goto invalid_data;
9190 : 3410 : get_uleb128 (desc, linep, lineendp);
9191 [ - + ]: 3410 : if ((size_t) (lineendp - linep) < 1)
9192 : 0 : goto invalid_data;
9193 : 3410 : get_uleb128 (form, linep, lineendp);
9194 : :
9195 : 3410 : enc[i].desc = desc;
9196 : 3410 : enc[i].form = form;
9197 : :
9198 : 3410 : printf ("%s(%s)",
9199 : : dwarf_line_content_description_name (desc),
9200 : : dwarf_form_name (form));
9201 [ - + ]: 3410 : if (i + 1 < directory_entry_format_count)
9202 [ + + ]: 6820 : printf (", ");
9203 : : }
9204 : 3410 : printf ("]\n");
9205 : :
9206 : 3410 : uint64_t directories_count;
9207 [ - + ]: 3410 : if ((size_t) (lineendp - linep) < 1)
9208 : 0 : goto invalid_data;
9209 : 3410 : get_uleb128 (directories_count, linep, lineendp);
9210 : :
9211 : 3410 : if (directory_entry_format_count == 0
9212 [ - + ]: 3410 : && directories_count != 0)
9213 : 0 : goto invalid_data;
9214 : :
9215 [ + + ]: 29532 : for (uint64_t i = 0; i < directories_count; i++)
9216 : : {
9217 : 26122 : printf (" %-5" PRIu64 " ", i);
9218 : 26122 : for (int j = 0; j < directory_entry_format_count; j++)
9219 : : {
9220 : 26122 : linep = print_form_data (dbg, enc[j].form,
9221 : : linep, lineendp, length,
9222 : : str_offsets_base);
9223 [ - + ]: 26122 : if (j + 1 < directory_entry_format_count)
9224 [ + + ]: 52244 : printf (", ");
9225 : : }
9226 : 26122 : printf ("\n");
9227 [ - + ]: 26122 : if (linep >= lineendp)
9228 : 0 : goto invalid_unit;
9229 : : }
9230 : : }
9231 : : else
9232 : : {
9233 [ + - + + ]: 766 : while (linep < lineendp && *linep != 0)
9234 : : {
9235 : 614 : unsigned char *endp = memchr (linep, '\0', lineendp - linep);
9236 [ - + ]: 614 : if (unlikely (endp == NULL))
9237 : 0 : goto invalid_unit;
9238 : :
9239 : 614 : printf (" %s\n", (char *) linep);
9240 : :
9241 : 614 : linep = endp + 1;
9242 : : }
9243 [ + - - + ]: 152 : if (linep >= lineendp || *linep != 0)
9244 : 0 : goto invalid_unit;
9245 : : /* Skip the final NUL byte. */
9246 : 152 : ++linep;
9247 : : }
9248 : :
9249 [ - + ]: 3562 : if (unlikely (linep >= lineendp))
9250 : 0 : goto invalid_unit;
9251 : :
9252 : 3562 : puts (_("\nFile name table:"));
9253 [ + + ]: 3562 : if (version > 4)
9254 : : {
9255 : 3410 : struct encpair { uint16_t desc; uint16_t form; };
9256 : 3410 : struct encpair enc[256];
9257 : :
9258 : 3410 : printf (_(" ["));
9259 [ - + ]: 3410 : if ((size_t) (lineendp - linep) < 1)
9260 : 0 : goto invalid_data;
9261 : 3410 : unsigned char file_name_format_count = *linep++;
9262 : 3410 : for (int i = 0; i < file_name_format_count; i++)
9263 : : {
9264 : 6828 : uint64_t desc, form;
9265 [ - + ]: 6828 : if ((size_t) (lineendp - linep) < 1)
9266 : 0 : goto invalid_data;
9267 : 6828 : get_uleb128 (desc, linep, lineendp);
9268 [ - + ]: 6828 : if ((size_t) (lineendp - linep) < 1)
9269 : 0 : goto invalid_data;
9270 : 6828 : get_uleb128 (form, linep, lineendp);
9271 : :
9272 [ - + ]: 6828 : if (! libdw_valid_user_form (form))
9273 : 0 : goto invalid_data;
9274 : :
9275 : 6828 : enc[i].desc = desc;
9276 : 6828 : enc[i].form = form;
9277 : :
9278 : 6828 : printf ("%s(%s)",
9279 : : dwarf_line_content_description_name (desc),
9280 : : dwarf_form_name (form));
9281 [ + + ]: 6828 : if (i + 1 < file_name_format_count)
9282 [ + + ]: 13656 : printf (", ");
9283 : : }
9284 : 3410 : printf ("]\n");
9285 : :
9286 : 3410 : uint64_t file_name_count;
9287 [ - + ]: 3410 : if ((size_t) (lineendp - linep) < 1)
9288 : 0 : goto invalid_data;
9289 : 3410 : get_uleb128 (file_name_count, linep, lineendp);
9290 : :
9291 : 3410 : if (file_name_format_count == 0
9292 [ - + ]: 3410 : && file_name_count != 0)
9293 : 0 : goto invalid_data;
9294 : :
9295 [ + + ]: 60172 : for (uint64_t i = 0; i < file_name_count; i++)
9296 : : {
9297 : 56762 : printf (" %-5" PRIu64 " ", i);
9298 : 56762 : for (int j = 0; j < file_name_format_count; j++)
9299 : : {
9300 : 113548 : linep = print_form_data (dbg, enc[j].form,
9301 : : linep, lineendp, length,
9302 : : str_offsets_base);
9303 [ + + ]: 113548 : if (j + 1 < file_name_format_count)
9304 [ + + ]: 227096 : printf (", ");
9305 : : }
9306 : 56762 : printf ("\n");
9307 [ - + ]: 56762 : if (linep > lineendp)
9308 : 0 : goto invalid_unit;
9309 : : }
9310 : : }
9311 : : else
9312 : : {
9313 : 152 : puts (_(" Entry Dir Time Size Name"));
9314 [ + - + + ]: 6154 : for (unsigned int cnt = 1; linep < lineendp && *linep != 0; ++cnt)
9315 : : {
9316 : : /* First comes the file name. */
9317 : 6002 : char *fname = (char *) linep;
9318 : 6002 : unsigned char *endp = memchr (fname, '\0', lineendp - linep);
9319 [ - + ]: 6002 : if (unlikely (endp == NULL))
9320 : 0 : goto invalid_unit;
9321 : 6002 : linep = endp + 1;
9322 : :
9323 : : /* Then the index. */
9324 : 6002 : unsigned int diridx;
9325 [ - + ]: 6002 : if (lineendp - linep < 1)
9326 : 0 : goto invalid_unit;
9327 : 6002 : get_uleb128 (diridx, linep, lineendp);
9328 : :
9329 : : /* Next comes the modification time. */
9330 : 6002 : unsigned int mtime;
9331 [ - + ]: 6002 : if (lineendp - linep < 1)
9332 : 0 : goto invalid_unit;
9333 : 6002 : get_uleb128 (mtime, linep, lineendp);
9334 : :
9335 : : /* Finally the length of the file. */
9336 : 6002 : unsigned int fsize;
9337 [ - + ]: 6002 : if (lineendp - linep < 1)
9338 : 0 : goto invalid_unit;
9339 : 6002 : get_uleb128 (fsize, linep, lineendp);
9340 : :
9341 : 6002 : printf (" %-5u %-5u %-9u %-9u %s\n",
9342 : : cnt, diridx, mtime, fsize, fname);
9343 : : }
9344 [ + - - + ]: 152 : if (linep >= lineendp || *linep != '\0')
9345 : 0 : goto invalid_unit;
9346 : : /* Skip the final NUL byte. */
9347 : 152 : ++linep;
9348 : : }
9349 : :
9350 : 3562 : unsigned int debug_str_offset = 0;
9351 [ + + ]: 3562 : if (unlikely (linep == header_start + header_length - 4))
9352 : : {
9353 : : /* CUBINs contain an unsigned 4-byte offset */
9354 [ - + ]: 2 : debug_str_offset = read_4ubyte_unaligned_inc (dbg, linep);
9355 : : }
9356 : :
9357 [ + + ]: 3562 : if (linep == lineendp)
9358 : : {
9359 : 40 : puts (_("\nNo line number statements."));
9360 : 40 : continue;
9361 : : }
9362 : :
9363 : 3522 : puts (_("\nLine number statements:"));
9364 : 3522 : Dwarf_Word address = 0;
9365 : 3522 : unsigned int op_index = 0;
9366 : 3522 : size_t line = 1;
9367 : 3522 : uint_fast8_t is_stmt = default_is_stmt;
9368 : :
9369 : : /* Apply the "operation advance" from a special opcode
9370 : : or DW_LNS_advance_pc (as per DWARF4 6.2.5.1). */
9371 : 3522 : unsigned int op_addr_advance;
9372 : : #define advance_pc(op_advance) run_advance_pc(op_advance, minimum_instr_len, \
9373 : : max_ops_per_instr, &op_addr_advance, &address, &op_index)
9374 : :
9375 [ - + ]: 3522 : if (max_ops_per_instr == 0)
9376 : : {
9377 : 0 : error (0, 0,
9378 : 0 : _("invalid maximum operations per instruction is zero"));
9379 : 0 : linep = lineendp;
9380 : 0 : continue;
9381 : : }
9382 : :
9383 : 2344674 : while (linep < lineendp)
9384 : : {
9385 : 2341152 : size_t offset = linep - (const unsigned char *) data->d_buf;
9386 : 2341152 : unsigned int u128;
9387 : 2341152 : int s128;
9388 : :
9389 : : /* Read the opcode. */
9390 : 2341152 : unsigned int opcode = *linep++;
9391 : :
9392 : 2341152 : printf (" [%6" PRIx64 "]", (uint64_t)offset);
9393 : : /* Is this a special opcode? */
9394 [ + + ]: 2341152 : if (likely (opcode >= opcode_base))
9395 : : {
9396 [ - + ]: 676750 : if (unlikely (line_range == 0))
9397 : 0 : goto invalid_unit;
9398 : :
9399 : : /* Yes. Handling this is quite easy since the opcode value
9400 : : is computed with
9401 : :
9402 : : opcode = (desired line increment - line_base)
9403 : : + (line_range * address advance) + opcode_base
9404 : : */
9405 : 676750 : int line_increment = (line_base
9406 : 676750 : + (opcode - opcode_base) % line_range);
9407 : :
9408 : : /* Perform the increments. */
9409 : 676750 : line += line_increment;
9410 : 676750 : advance_pc ((opcode - opcode_base) / line_range);
9411 : :
9412 : 676750 : printf (_(" special opcode %u: address+%u = "),
9413 : : opcode, op_addr_advance);
9414 : 676750 : print_dwarf_addr (dwflmod, 0, address, address);
9415 [ - + ]: 676750 : if (op_index > 0)
9416 : 0 : printf (_(", op_index = %u, line%+d = %zu\n"),
9417 : : op_index, line_increment, line);
9418 : : else
9419 : 676750 : printf (_(", line%+d = %zu\n"),
9420 : : line_increment, line);
9421 : : }
9422 [ + + ]: 1664402 : else if (opcode == 0)
9423 : : {
9424 : : /* This an extended opcode. */
9425 [ - + ]: 128128 : if (unlikely (linep + 2 > lineendp))
9426 : 0 : goto invalid_unit;
9427 : :
9428 : : /* The length. */
9429 : 128128 : unsigned int len = *linep++;
9430 : :
9431 [ - + ]: 128128 : if (unlikely (linep + len > lineendp))
9432 : 0 : goto invalid_unit;
9433 : :
9434 : : /* The sub-opcode. */
9435 : 128128 : opcode = *linep++;
9436 : :
9437 : 128128 : printf (_(" extended opcode %u: "), opcode);
9438 : :
9439 [ + + - + : 128128 : switch (opcode)
+ - - ]
9440 : : {
9441 : 9812 : case DW_LNE_end_sequence:
9442 : 9812 : puts (_(" end of sequence"));
9443 : :
9444 : : /* Reset the registers we care about. */
9445 : 9812 : address = 0;
9446 : 9812 : op_index = 0;
9447 : 9812 : line = 1;
9448 : 9812 : is_stmt = default_is_stmt;
9449 : 9812 : break;
9450 : :
9451 : 11042 : case DW_LNE_set_address:
9452 : 11042 : op_index = 0;
9453 [ - + ]: 11042 : if (unlikely ((size_t) (lineendp - linep) < address_size))
9454 : 0 : goto invalid_unit;
9455 [ + + ]: 11042 : if (address_size == 4)
9456 [ + + ]: 50 : address = read_4ubyte_unaligned_inc (dbg, linep);
9457 : : else
9458 [ + + ]: 10992 : address = read_8ubyte_unaligned_inc (dbg, linep);
9459 : : {
9460 : 11042 : printf (_(" set address to "));
9461 : 11042 : print_dwarf_addr (dwflmod, 0, address, address);
9462 : 11042 : printf ("\n");
9463 : : }
9464 : : break;
9465 : :
9466 : 0 : case DW_LNE_define_file:
9467 : : {
9468 : 0 : char *fname = (char *) linep;
9469 : 0 : unsigned char *endp = memchr (linep, '\0',
9470 : 0 : lineendp - linep);
9471 [ # # ]: 0 : if (unlikely (endp == NULL))
9472 : 0 : goto invalid_unit;
9473 : 0 : linep = endp + 1;
9474 : :
9475 : 0 : unsigned int diridx;
9476 [ # # ]: 0 : if (lineendp - linep < 1)
9477 : 0 : goto invalid_unit;
9478 : 0 : get_uleb128 (diridx, linep, lineendp);
9479 : 0 : Dwarf_Word mtime;
9480 [ # # ]: 0 : if (lineendp - linep < 1)
9481 : 0 : goto invalid_unit;
9482 : 0 : get_uleb128 (mtime, linep, lineendp);
9483 : 0 : Dwarf_Word filelength;
9484 [ # # ]: 0 : if (lineendp - linep < 1)
9485 : 0 : goto invalid_unit;
9486 : 0 : get_uleb128 (filelength, linep, lineendp);
9487 : :
9488 : 0 : printf (_("\
9489 : : define new file: dir=%u, mtime=%" PRIu64 ", length=%" PRIu64 ", name=%s\n"),
9490 : : diridx, (uint64_t) mtime, (uint64_t) filelength,
9491 : : fname);
9492 : : }
9493 : : break;
9494 : :
9495 : 107260 : case DW_LNE_set_discriminator:
9496 : : /* Takes one ULEB128 parameter, the discriminator. */
9497 [ + - - + ]: 107260 : if (unlikely (standard_opcode_lengths[opcode] != 1
9498 : : || lineendp - linep < 1))
9499 : 0 : goto invalid_unit;
9500 : :
9501 : 107260 : get_uleb128 (u128, linep, lineendp);
9502 : 107260 : printf (_(" set discriminator to %u\n"), u128);
9503 : : break;
9504 : :
9505 : 14 : case DW_LNE_NVIDIA_inlined_call:
9506 : : {
9507 [ - + ]: 14 : if (unlikely (linep >= lineendp))
9508 : 0 : goto invalid_data;
9509 : :
9510 : 14 : unsigned int context;
9511 : 14 : get_uleb128 (context, linep, lineendp);
9512 : :
9513 [ - + ]: 14 : if (unlikely (linep >= lineendp))
9514 : 0 : goto invalid_data;
9515 : :
9516 : 14 : unsigned int function_name;
9517 : 14 : get_uleb128 (function_name, linep, lineendp);
9518 : 14 : function_name += debug_str_offset;
9519 : :
9520 : 14 : Elf_Data *str_data = dbg->sectiondata[IDX_debug_str];
9521 : 14 : char *function_str;
9522 [ + - - + ]: 14 : if (str_data == NULL || function_name >= str_data->d_size
9523 [ - + ]: 14 : || memchr (str_data->d_buf + function_name, '\0',
9524 : : str_data->d_size - function_name) == NULL)
9525 : : function_str = "???";
9526 : : else
9527 : 14 : function_str = (char *) str_data->d_buf + function_name;
9528 : :
9529 : 14 : printf (_(" set inlined context %u,"
9530 : : " function name %s (0x%x)\n"),
9531 : : context, function_str, function_name);
9532 : : break;
9533 : : }
9534 : :
9535 : 0 : case DW_LNE_NVIDIA_set_function_name:
9536 : : {
9537 [ # # ]: 0 : if (unlikely (linep >= lineendp))
9538 : 0 : goto invalid_data;
9539 : :
9540 : 0 : unsigned int function_name;
9541 : 0 : get_uleb128 (function_name, linep, lineendp);
9542 : 0 : function_name += debug_str_offset;
9543 : :
9544 : 0 : Elf_Data *str_data = dbg->sectiondata[IDX_debug_str];
9545 : 0 : char *function_str;
9546 [ # # # # ]: 0 : if (str_data == NULL || function_name >= str_data->d_size
9547 [ # # ]: 0 : || memchr (str_data->d_buf + function_name, '\0',
9548 : : str_data->d_size - function_name) == NULL)
9549 : : function_str = "???";
9550 : : else
9551 : 0 : function_str = (char *) str_data->d_buf + function_name;
9552 : :
9553 : 0 : printf (_(" set function name %s (0x%x)\n"),
9554 : : function_str, function_name);
9555 : : }
9556 : : break;
9557 : :
9558 : 0 : default:
9559 : : /* Unknown, ignore it. */
9560 : 0 : puts (_(" unknown opcode"));
9561 : 0 : linep += len - 1;
9562 : 0 : break;
9563 : : }
9564 : : }
9565 [ + - ]: 1536274 : else if (opcode <= DW_LNS_set_isa)
9566 : : {
9567 : : /* This is a known standard opcode. */
9568 [ + + + + : 1536274 : switch (opcode)
+ + - + +
+ - - ]
9569 : : {
9570 : 233064 : case DW_LNS_copy:
9571 : : /* Takes no argument. */
9572 : 233064 : puts (_(" copy"));
9573 : 233064 : break;
9574 : :
9575 : 15284 : case DW_LNS_advance_pc:
9576 : : /* Takes one uleb128 parameter which is added to the
9577 : : address. */
9578 [ - + ]: 15284 : if (lineendp - linep < 1)
9579 : 0 : goto invalid_unit;
9580 : 15284 : get_uleb128 (u128, linep, lineendp);
9581 : 15284 : advance_pc (u128);
9582 : : {
9583 : 15284 : printf (_(" advance address by %u to "),
9584 : : op_addr_advance);
9585 : 15284 : print_dwarf_addr (dwflmod, 0, address, address);
9586 [ - + ]: 15284 : if (op_index > 0)
9587 : 0 : printf (_(", op_index to %u"), op_index);
9588 : 15284 : printf ("\n");
9589 : : }
9590 : : break;
9591 : :
9592 : 179558 : case DW_LNS_advance_line:
9593 : : /* Takes one sleb128 parameter which is added to the
9594 : : line. */
9595 [ - + ]: 179558 : if (lineendp - linep < 1)
9596 : 0 : goto invalid_unit;
9597 : 179558 : get_sleb128 (s128, linep, lineendp);
9598 : 179558 : line += s128;
9599 : 179558 : printf (_("\
9600 : : advance line by constant %d to %" PRId64 "\n"),
9601 : : s128, (int64_t) line);
9602 : : break;
9603 : :
9604 : 65712 : case DW_LNS_set_file:
9605 : : /* Takes one uleb128 parameter which is stored in file. */
9606 [ - + ]: 65712 : if (lineendp - linep < 1)
9607 : 0 : goto invalid_unit;
9608 : 65712 : get_uleb128 (u128, linep, lineendp);
9609 : 65712 : printf (_(" set file to %" PRIu64 "\n"),
9610 : : (uint64_t) u128);
9611 : : break;
9612 : :
9613 : 583418 : case DW_LNS_set_column:
9614 : : /* Takes one uleb128 parameter which is stored in column. */
9615 [ + - - + ]: 583418 : if (unlikely (standard_opcode_lengths[opcode] != 1
9616 : : || lineendp - linep < 1))
9617 : 0 : goto invalid_unit;
9618 : :
9619 : 583418 : get_uleb128 (u128, linep, lineendp);
9620 : 583418 : printf (_(" set column to %" PRIu64 "\n"),
9621 : : (uint64_t) u128);
9622 : : break;
9623 : :
9624 : 406338 : case DW_LNS_negate_stmt:
9625 : : /* Takes no argument. */
9626 : 406338 : is_stmt = 1 - is_stmt;
9627 : 406338 : printf (_(" set '%s' to %" PRIuFAST8 "\n"),
9628 : : "is_stmt", is_stmt);
9629 : : break;
9630 : :
9631 : 0 : case DW_LNS_set_basic_block:
9632 : : /* Takes no argument. */
9633 : 0 : puts (_(" set basic block flag"));
9634 : 0 : break;
9635 : :
9636 : 52846 : case DW_LNS_const_add_pc:
9637 : : /* Takes no argument. */
9638 : :
9639 [ - + ]: 52846 : if (unlikely (line_range == 0))
9640 : 0 : goto invalid_unit;
9641 : :
9642 : 52846 : advance_pc ((255 - opcode_base) / line_range);
9643 : : {
9644 : 52846 : printf (_(" advance address by constant %u to "),
9645 : : op_addr_advance);
9646 : 52846 : print_dwarf_addr (dwflmod, 0, address, address);
9647 [ - + ]: 52846 : if (op_index > 0)
9648 : 0 : printf (_(", op_index to %u"), op_index);
9649 : 52846 : printf ("\n");
9650 : : }
9651 : : break;
9652 : :
9653 : 44 : case DW_LNS_fixed_advance_pc:
9654 : : /* Takes one 16 bit parameter which is added to the
9655 : : address. */
9656 [ + - - + ]: 44 : if (unlikely (standard_opcode_lengths[opcode] != 1
9657 : : || lineendp - linep < 2))
9658 : 0 : goto invalid_unit;
9659 : :
9660 [ - + ]: 44 : u128 = read_2ubyte_unaligned_inc (dbg, linep);
9661 : 44 : address += u128;
9662 : 44 : op_index = 0;
9663 : : {
9664 : 44 : printf (_("\
9665 : : advance address by fixed value %u to \n"),
9666 : : u128);
9667 : 44 : print_dwarf_addr (dwflmod, 0, address, address);
9668 : 44 : printf ("\n");
9669 : : }
9670 : : break;
9671 : :
9672 : 10 : case DW_LNS_set_prologue_end:
9673 : : /* Takes no argument. */
9674 : 10 : puts (_(" set prologue end flag"));
9675 : 10 : break;
9676 : :
9677 : 0 : case DW_LNS_set_epilogue_begin:
9678 : : /* Takes no argument. */
9679 : 0 : puts (_(" set epilogue begin flag"));
9680 : 0 : break;
9681 : :
9682 : 0 : case DW_LNS_set_isa:
9683 : : /* Takes one uleb128 parameter which is stored in isa. */
9684 [ # # # # ]: 0 : if (unlikely (standard_opcode_lengths[opcode] != 1
9685 : : || lineendp - linep < 1))
9686 : 0 : goto invalid_unit;
9687 : :
9688 : 0 : get_uleb128 (u128, linep, lineendp);
9689 [ + + ]: 2344674 : printf (_(" set isa to %u\n"), u128);
9690 : : break;
9691 : : }
9692 : : }
9693 : : else
9694 : : {
9695 : : /* This is a new opcode the generator but not we know about.
9696 : : Read the parameters associated with it but then discard
9697 : : everything. Read all the parameters for this opcode. */
9698 : 0 : printf (ngettext (" unknown opcode with %" PRIu8 " parameter:",
9699 : : " unknown opcode with %" PRIu8 " parameters:",
9700 : : standard_opcode_lengths[opcode]),
9701 : 0 : standard_opcode_lengths[opcode]);
9702 : 0 : for (int n = standard_opcode_lengths[opcode];
9703 [ # # # # ]: 0 : n > 0 && linep < lineendp; --n)
9704 : : {
9705 : 0 : get_uleb128 (u128, linep, lineendp);
9706 [ # # ]: 0 : if (n != standard_opcode_lengths[opcode])
9707 : 0 : fputc (',', stdout);
9708 : 0 : printf (" %u", u128);
9709 : : }
9710 : :
9711 : : /* Next round, ignore this opcode. */
9712 : 0 : continue;
9713 : : }
9714 : : }
9715 : : }
9716 : :
9717 : : /* There must only be one data block. */
9718 [ - + ]: 104 : assert (elf_getdata (scn, data) == NULL);
9719 : : }
9720 : :
9721 : :
9722 : : static void
9723 : 40 : print_debug_loclists_section (Dwfl_Module *dwflmod,
9724 : : Ebl *ebl,
9725 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
9726 : : Elf_Scn *scn, GElf_Shdr *shdr,
9727 : : Dwarf *dbg)
9728 : : {
9729 : 40 : Elf_Data *data = get_debug_elf_data (dbg, ebl, IDX_debug_loclists, scn);
9730 [ + - ]: 40 : if (data == NULL)
9731 : 0 : return;
9732 : :
9733 : 40 : printf (_("\
9734 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
9735 : : elf_ndxscn (scn), section_name (ebl, shdr),
9736 : 40 : (uint64_t) shdr->sh_offset);
9737 : :
9738 : : /* For the listptr to get the base address/CU. */
9739 : 40 : sort_listptr (&known_loclistsptr, "loclistsptr");
9740 : 40 : size_t listptr_idx = 0;
9741 : :
9742 : 40 : const unsigned char *readp = data->d_buf;
9743 : 40 : const unsigned char *const dataend = ((unsigned char *) data->d_buf
9744 : 40 : + data->d_size);
9745 [ + + ]: 3072 : while (readp < dataend)
9746 : : {
9747 [ - + ]: 3032 : if (unlikely (readp > dataend - 4))
9748 : : {
9749 : 0 : invalid_data:
9750 : 0 : error (0, 0, _("invalid data in section [%zu] '%s'"),
9751 : : elf_ndxscn (scn), section_name (ebl, shdr));
9752 : 0 : return;
9753 : : }
9754 : :
9755 : 3032 : ptrdiff_t offset = readp - (unsigned char *) data->d_buf;
9756 : 3032 : printf (_("Table at Offset 0x%" PRIx64 ":\n\n"),
9757 : : (uint64_t) offset);
9758 : :
9759 [ - + ]: 3032 : uint64_t unit_length = read_4ubyte_unaligned_inc (dbg, readp);
9760 : 3032 : unsigned int offset_size = 4;
9761 [ - + ]: 3032 : if (unlikely (unit_length == 0xffffffff))
9762 : : {
9763 [ # # ]: 0 : if (unlikely (readp > dataend - 8))
9764 : 0 : goto invalid_data;
9765 : :
9766 [ # # ]: 0 : unit_length = read_8ubyte_unaligned_inc (dbg, readp);
9767 : 0 : offset_size = 8;
9768 : : }
9769 : 3032 : printf (_(" Length: %8" PRIu64 "\n"), unit_length);
9770 : :
9771 : : /* We need at least 2-bytes + 1-byte + 1-byte + 4-bytes = 8
9772 : : bytes to complete the header. And this unit cannot go beyond
9773 : : the section data. */
9774 [ + - ]: 3032 : if (readp > dataend - 8
9775 [ + - ]: 3032 : || unit_length < 8
9776 [ - + ]: 3032 : || unit_length > (uint64_t) (dataend - readp))
9777 : 0 : goto invalid_data;
9778 : :
9779 : 3032 : const unsigned char *nexthdr = readp + unit_length;
9780 : :
9781 [ - + ]: 3032 : uint16_t version = read_2ubyte_unaligned_inc (dbg, readp);
9782 : 3032 : printf (_(" DWARF version: %8" PRIu16 "\n"), version);
9783 : :
9784 [ - + ]: 3032 : if (version != 5)
9785 : : {
9786 : 0 : error (0, 0, _("Unknown version"));
9787 : 0 : goto next_table;
9788 : : }
9789 : :
9790 : 3032 : uint8_t address_size = *readp++;
9791 : 3032 : printf (_(" Address size: %8" PRIu64 "\n"),
9792 : : (uint64_t) address_size);
9793 : :
9794 [ - + ]: 3032 : if (address_size != 4 && address_size != 8)
9795 : : {
9796 : 0 : error (0, 0, _("unsupported address size"));
9797 : 0 : goto next_table;
9798 : : }
9799 : :
9800 : 3032 : uint8_t segment_size = *readp++;
9801 : 3032 : printf (_(" Segment size: %8" PRIu64 "\n"),
9802 : : (uint64_t) segment_size);
9803 : :
9804 [ - + ]: 3032 : if (segment_size != 0)
9805 : : {
9806 : 0 : error (0, 0, _("unsupported segment size"));
9807 : 0 : goto next_table;
9808 : : }
9809 : :
9810 [ - + ]: 3032 : uint32_t offset_entry_count = read_4ubyte_unaligned_inc (dbg, readp);
9811 : 3032 : printf (_(" Offset entries: %8" PRIu64 "\n"),
9812 : : (uint64_t) offset_entry_count);
9813 : :
9814 : : /* We need the CU that uses this unit to get the initial base address. */
9815 : 3032 : Dwarf_Addr cu_base = 0;
9816 : 3032 : struct Dwarf_CU *cu = NULL;
9817 [ - + ]: 3032 : if (listptr_cu (&known_loclistsptr, &listptr_idx,
9818 : : (Dwarf_Off) offset,
9819 : 3032 : (Dwarf_Off) (nexthdr - (unsigned char *) data->d_buf),
9820 : : &cu_base, &cu)
9821 [ # # ]: 0 : || split_dwarf_cu_base (dbg, &cu, &cu_base))
9822 : 3032 : {
9823 : 3032 : Dwarf_Die cudie;
9824 [ - + ]: 3032 : if (dwarf_cu_die (cu, &cudie,
9825 : : NULL, NULL, NULL, NULL,
9826 : : NULL, NULL) == NULL)
9827 : 0 : printf (_(" Unknown CU base: "));
9828 : : else
9829 : 3032 : printf (_(" CU [%6" PRIx64 "] base: "),
9830 : : dwarf_dieoffset (&cudie));
9831 : 3032 : print_dwarf_addr (dwflmod, address_size, cu_base, cu_base);
9832 : 3032 : printf ("\n");
9833 : : }
9834 : : else
9835 : 0 : printf (_(" Not associated with a CU.\n"));
9836 : :
9837 : 3032 : printf ("\n");
9838 : :
9839 : 3032 : const unsigned char *offset_array_start = readp;
9840 [ + + ]: 3032 : if (offset_entry_count > 0)
9841 : : {
9842 : 4 : uint64_t max_entries = (unit_length - 8) / offset_size;
9843 [ - + ]: 4 : if (offset_entry_count > max_entries)
9844 : : {
9845 : 0 : error (0, 0,
9846 : 0 : _("too many offset entries for unit length"));
9847 : 0 : offset_entry_count = max_entries;
9848 : : }
9849 : :
9850 : 4 : printf (_(" Offsets starting at 0x%" PRIx64 ":\n"),
9851 : : (uint64_t) (offset_array_start
9852 : 4 : - (unsigned char *) data->d_buf));
9853 [ + + ]: 36 : for (uint32_t idx = 0; idx < offset_entry_count; idx++)
9854 : : {
9855 : 32 : printf (" [%6" PRIu32 "] ", idx);
9856 [ + - ]: 32 : if (offset_size == 4)
9857 : : {
9858 [ - + ]: 32 : uint32_t off = read_4ubyte_unaligned_inc (dbg, readp);
9859 : 32 : printf ("0x%" PRIx32 "\n", off);
9860 : : }
9861 : : else
9862 : : {
9863 [ # # ]: 0 : uint64_t off = read_8ubyte_unaligned_inc (dbg, readp);
9864 : 32 : printf ("0x%" PRIx64 "\n", off);
9865 : : }
9866 : : }
9867 : 4 : printf ("\n");
9868 : : }
9869 : :
9870 : 3032 : Dwarf_Addr base = cu_base;
9871 : 3032 : bool start_of_list = true;
9872 : 3032 : while (readp < nexthdr)
9873 : : {
9874 : 729976 : Dwarf_Off off = (Dwarf_Off) (readp - (unsigned char *) data->d_buf);
9875 [ + + ]: 729976 : if (listptr_attr (&known_loclistsptr, listptr_idx, off,
9876 : : DW_AT_GNU_locviews))
9877 : 107542 : {
9878 : 107542 : Dwarf_Off next_off = next_listptr_offset (&known_loclistsptr,
9879 : : &listptr_idx, off);
9880 : 107542 : const unsigned char *locp = readp;
9881 : 107542 : const unsigned char *locendp;
9882 [ + - ]: 107542 : if (next_off == 0
9883 [ + - ]: 107542 : || next_off > (size_t) (nexthdr - ((const unsigned char *)
9884 : : data->d_buf)))
9885 : : locendp = nexthdr;
9886 : : else
9887 : 107542 : locendp = (const unsigned char *) data->d_buf + next_off;
9888 : :
9889 : 711626 : printf (" Offset: %" PRIx64 ", Index: %" PRIx64 "\n",
9890 : : (uint64_t) (readp - (unsigned char *) data->d_buf),
9891 : 107542 : (uint64_t) (readp - offset_array_start));
9892 : :
9893 : 604084 : while (locp < locendp)
9894 : : {
9895 : 496542 : uint64_t v1, v2;
9896 : 496542 : get_uleb128 (v1, locp, locendp);
9897 [ - + ]: 496542 : if (locp >= locendp)
9898 : : {
9899 : 0 : printf (_(" <INVALID DATA>\n"));
9900 : : break;
9901 : : }
9902 : 496542 : get_uleb128 (v2, locp, locendp);
9903 [ + + ]: 604084 : printf (" view pair %" PRId64 ", %" PRId64 "\n", v1, v2);
9904 : : }
9905 : :
9906 : 107542 : printf ("\n");
9907 : 107542 : readp = (unsigned char *) locendp;
9908 : 107542 : continue;
9909 : : }
9910 : :
9911 : 622434 : uint8_t kind = *readp++;
9912 : 622434 : uint64_t op1, op2, len;
9913 : :
9914 : : /* Skip padding. */
9915 [ - + ]: 622434 : if (start_of_list && kind == DW_LLE_end_of_list)
9916 : 0 : continue;
9917 : :
9918 [ + + ]: 622434 : if (start_of_list)
9919 : : {
9920 : 107606 : base = cu_base;
9921 : 107606 : printf (" Offset: %" PRIx64 ", Index: %" PRIx64 "\n",
9922 : 107606 : (uint64_t) (readp - (unsigned char *) data->d_buf - 1),
9923 : 107606 : (uint64_t) (readp - offset_array_start - 1));
9924 : 107606 : start_of_list = false;
9925 : : }
9926 : :
9927 : 622434 : printf (" %s", dwarf_loc_list_encoding_name (kind));
9928 [ + - - + : 622434 : switch (kind)
+ - + - +
- - ]
9929 : : {
9930 : 107606 : case DW_LLE_end_of_list:
9931 : 107606 : start_of_list = true;
9932 : 107606 : printf ("\n\n");
9933 : : break;
9934 : :
9935 : 0 : case DW_LLE_base_addressx:
9936 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
9937 : : {
9938 : 0 : invalid_entry:
9939 : 0 : error (0, 0, _("invalid loclists data"));
9940 : 0 : goto next_table;
9941 : : }
9942 : 0 : get_uleb128 (op1, readp, nexthdr);
9943 : 0 : printf (" %" PRIx64 "\n", op1);
9944 [ # # ]: 0 : if (! print_unresolved_addresses)
9945 : : {
9946 : 0 : Dwarf_Addr addr;
9947 [ # # ]: 0 : if (get_indexed_addr (cu, op1, &addr) != 0)
9948 : 0 : printf (" ???\n");
9949 : : else
9950 : : {
9951 : 0 : printf (" ");
9952 : 0 : print_dwarf_addr (dwflmod, address_size, addr, addr);
9953 : 0 : printf ("\n");
9954 : : }
9955 : : }
9956 : : break;
9957 : :
9958 : 0 : case DW_LLE_startx_endx:
9959 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
9960 : 0 : goto invalid_entry;
9961 : 0 : get_uleb128 (op1, readp, nexthdr);
9962 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
9963 : 0 : goto invalid_entry;
9964 : 0 : get_uleb128 (op2, readp, nexthdr);
9965 : 0 : printf (" %" PRIx64 ", %" PRIx64 "\n", op1, op2);
9966 [ # # ]: 0 : if (! print_unresolved_addresses)
9967 : : {
9968 : 0 : Dwarf_Addr addr1;
9969 : 0 : Dwarf_Addr addr2;
9970 [ # # ]: 0 : if (get_indexed_addr (cu, op1, &addr1) != 0
9971 [ # # ]: 0 : || get_indexed_addr (cu, op2, &addr2) != 0)
9972 : : {
9973 : 0 : printf (" ???..\n");
9974 : 0 : printf (" ???\n");
9975 : : }
9976 : : else
9977 : : {
9978 : 0 : printf (" ");
9979 : 0 : print_dwarf_addr (dwflmod, address_size, addr1, addr1);
9980 : 0 : printf ("..\n ");
9981 : 0 : print_dwarf_addr (dwflmod, address_size,
9982 : : addr2 - 1, addr2);
9983 : 0 : printf ("\n");
9984 : : }
9985 : : }
9986 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
9987 : 0 : goto invalid_entry;
9988 : 0 : get_uleb128 (len, readp, nexthdr);
9989 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < len)
9990 : 0 : goto invalid_entry;
9991 : 0 : print_ops (dwflmod, dbg, 8, 8, version,
9992 : : address_size, offset_size, cu, len, readp);
9993 : 0 : readp += len;
9994 : 0 : break;
9995 : :
9996 : 52 : case DW_LLE_startx_length:
9997 [ - + ]: 52 : if ((uint64_t) (nexthdr - readp) < 1)
9998 : 0 : goto invalid_entry;
9999 : 52 : get_uleb128 (op1, readp, nexthdr);
10000 [ - + ]: 52 : if ((uint64_t) (nexthdr - readp) < 1)
10001 : 0 : goto invalid_entry;
10002 : 52 : get_uleb128 (op2, readp, nexthdr);
10003 : 52 : printf (" %" PRIx64 ", %" PRIx64 "\n", op1, op2);
10004 [ + - ]: 52 : if (! print_unresolved_addresses)
10005 : : {
10006 : 52 : Dwarf_Addr addr1;
10007 : 52 : Dwarf_Addr addr2;
10008 [ - + ]: 52 : if (get_indexed_addr (cu, op1, &addr1) != 0)
10009 : : {
10010 : 0 : printf (" ???..\n");
10011 : 0 : printf (" ???\n");
10012 : : }
10013 : : else
10014 : : {
10015 : 52 : addr2 = addr1 + op2;
10016 : 52 : printf (" ");
10017 : 52 : print_dwarf_addr (dwflmod, address_size, addr1, addr1);
10018 : 52 : printf ("..\n ");
10019 : 52 : print_dwarf_addr (dwflmod, address_size,
10020 : : addr2 - 1, addr2);
10021 : 52 : printf ("\n");
10022 : : }
10023 : : }
10024 [ - + ]: 52 : if ((uint64_t) (nexthdr - readp) < 1)
10025 : 0 : goto invalid_entry;
10026 : 52 : get_uleb128 (len, readp, nexthdr);
10027 [ - + ]: 52 : if ((uint64_t) (nexthdr - readp) < len)
10028 : 0 : goto invalid_entry;
10029 : 52 : print_ops (dwflmod, dbg, 8, 8, version,
10030 : : address_size, offset_size, cu, len, readp);
10031 : 52 : readp += len;
10032 : 52 : break;
10033 : :
10034 : 484796 : case DW_LLE_offset_pair:
10035 [ - + ]: 484796 : if ((uint64_t) (nexthdr - readp) < 1)
10036 : 0 : goto invalid_entry;
10037 : 484796 : get_uleb128 (op1, readp, nexthdr);
10038 [ - + ]: 484796 : if ((uint64_t) (nexthdr - readp) < 1)
10039 : 0 : goto invalid_entry;
10040 : 484796 : get_uleb128 (op2, readp, nexthdr);
10041 : 484796 : printf (" %" PRIx64 ", %" PRIx64 "\n", op1, op2);
10042 [ + - ]: 484796 : if (! print_unresolved_addresses)
10043 : : {
10044 : 484796 : op1 += base;
10045 : 484796 : op2 += base;
10046 : 484796 : printf (" ");
10047 : 484796 : print_dwarf_addr (dwflmod, address_size, op1, op1);
10048 : 484796 : printf ("..\n ");
10049 : 484796 : print_dwarf_addr (dwflmod, address_size, op2 - 1, op2);
10050 : 484796 : printf ("\n");
10051 : : }
10052 [ - + ]: 484796 : if ((uint64_t) (nexthdr - readp) < 1)
10053 : 0 : goto invalid_entry;
10054 : 484796 : get_uleb128 (len, readp, nexthdr);
10055 [ - + ]: 484796 : if ((uint64_t) (nexthdr - readp) < len)
10056 : 0 : goto invalid_entry;
10057 : 484796 : print_ops (dwflmod, dbg, 8, 8, version,
10058 : : address_size, offset_size, cu, len, readp);
10059 : 484796 : readp += len;
10060 : 484796 : break;
10061 : :
10062 : 0 : case DW_LLE_default_location:
10063 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
10064 : 0 : goto invalid_entry;
10065 : 0 : get_uleb128 (len, readp, nexthdr);
10066 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < len)
10067 : 0 : goto invalid_entry;
10068 : 0 : print_ops (dwflmod, dbg, 8, 8, version,
10069 : : address_size, offset_size, cu, len, readp);
10070 : 0 : readp += len;
10071 : 0 : break;
10072 : :
10073 : 18182 : case DW_LLE_base_address:
10074 [ - + ]: 18182 : if (address_size == 4)
10075 : : {
10076 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 4)
10077 : 0 : goto invalid_entry;
10078 [ # # ]: 0 : op1 = read_4ubyte_unaligned_inc (dbg, readp);
10079 : : }
10080 : : else
10081 : : {
10082 [ - + ]: 18182 : if ((uint64_t) (nexthdr - readp) < 8)
10083 : 0 : goto invalid_entry;
10084 [ - + ]: 18182 : op1 = read_8ubyte_unaligned_inc (dbg, readp);
10085 : : }
10086 : 18182 : base = op1;
10087 : 18182 : printf (" 0x%" PRIx64 "\n", base);
10088 [ - + ]: 18182 : if (! print_unresolved_addresses)
10089 : : {
10090 : 18182 : printf (" ");
10091 : 18182 : print_dwarf_addr (dwflmod, address_size, base, base);
10092 : 18182 : printf ("\n");
10093 : : }
10094 : : break;
10095 : :
10096 : 0 : case DW_LLE_start_end:
10097 [ # # ]: 0 : if (address_size == 4)
10098 : : {
10099 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 8)
10100 : 0 : goto invalid_entry;
10101 [ # # ]: 0 : op1 = read_4ubyte_unaligned_inc (dbg, readp);
10102 [ # # ]: 0 : op2 = read_4ubyte_unaligned_inc (dbg, readp);
10103 : : }
10104 : : else
10105 : : {
10106 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 16)
10107 : 0 : goto invalid_entry;
10108 [ # # ]: 0 : op1 = read_8ubyte_unaligned_inc (dbg, readp);
10109 [ # # ]: 0 : op2 = read_8ubyte_unaligned_inc (dbg, readp);
10110 : : }
10111 : 0 : printf (" 0x%" PRIx64 "..0x%" PRIx64 "\n", op1, op2);
10112 [ # # ]: 0 : if (! print_unresolved_addresses)
10113 : : {
10114 : 0 : printf (" ");
10115 : 0 : print_dwarf_addr (dwflmod, address_size, op1, op1);
10116 : 0 : printf ("..\n ");
10117 : 0 : print_dwarf_addr (dwflmod, address_size, op2 - 1, op2);
10118 : 0 : printf ("\n");
10119 : : }
10120 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
10121 : 0 : goto invalid_entry;
10122 : 0 : get_uleb128 (len, readp, nexthdr);
10123 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < len)
10124 : 0 : goto invalid_entry;
10125 : 0 : print_ops (dwflmod, dbg, 8, 8, version,
10126 : : address_size, offset_size, cu, len, readp);
10127 : 0 : readp += len;
10128 : 0 : break;
10129 : :
10130 : 11798 : case DW_LLE_start_length:
10131 [ - + ]: 11798 : if (address_size == 4)
10132 : : {
10133 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 4)
10134 : 0 : goto invalid_entry;
10135 [ # # ]: 0 : op1 = read_4ubyte_unaligned_inc (dbg, readp);
10136 : : }
10137 : : else
10138 : : {
10139 [ - + ]: 11798 : if ((uint64_t) (nexthdr - readp) < 8)
10140 : 0 : goto invalid_entry;
10141 [ - + ]: 11798 : op1 = read_8ubyte_unaligned_inc (dbg, readp);
10142 : : }
10143 [ - + ]: 11798 : if ((uint64_t) (nexthdr - readp) < 1)
10144 : 0 : goto invalid_entry;
10145 : 11798 : get_uleb128 (op2, readp, nexthdr);
10146 : 11798 : printf (" 0x%" PRIx64 ", %" PRIx64 "\n", op1, op2);
10147 [ + + ]: 11798 : if (! print_unresolved_addresses)
10148 : : {
10149 : 11796 : op2 = op1 + op2;
10150 : 11796 : printf (" ");
10151 : 11796 : print_dwarf_addr (dwflmod, address_size, op1, op1);
10152 : 11796 : printf ("..\n ");
10153 : 11796 : print_dwarf_addr (dwflmod, address_size, op2 - 1, op2);
10154 : 11796 : printf ("\n");
10155 : : }
10156 [ - + ]: 11798 : if ((uint64_t) (nexthdr - readp) < 1)
10157 : 0 : goto invalid_entry;
10158 : 11798 : get_uleb128 (len, readp, nexthdr);
10159 [ - + ]: 11798 : if ((uint64_t) (nexthdr - readp) < len)
10160 : 0 : goto invalid_entry;
10161 : 11798 : print_ops (dwflmod, dbg, 8, 8, version,
10162 : : address_size, offset_size, cu, len, readp);
10163 : 11798 : readp += len;
10164 : 11798 : break;
10165 : :
10166 : 0 : case DW_LLE_GNU_view_pair:
10167 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
10168 : 0 : goto invalid_entry;
10169 : 0 : get_uleb128 (op1, readp, nexthdr);
10170 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
10171 : 0 : goto invalid_entry;
10172 : 0 : get_uleb128 (op2, readp, nexthdr);
10173 [ + + ]: 733008 : printf (" %" PRIx64 ", %" PRIx64 "\n", op1, op2);
10174 : : break;
10175 : :
10176 : 0 : default:
10177 : 0 : goto invalid_entry;
10178 : : }
10179 : : }
10180 : :
10181 : 3032 : next_table:
10182 [ - + ]: 3032 : if (readp != nexthdr)
10183 : : {
10184 : 0 : size_t padding = nexthdr - readp;
10185 : 0 : printf (_(" %zu padding bytes\n\n"), padding);
10186 : 0 : readp = nexthdr;
10187 : : }
10188 : : }
10189 : : }
10190 : :
10191 : :
10192 : : static void
10193 : 52 : print_debug_loc_section (Dwfl_Module *dwflmod,
10194 : : Ebl *ebl, GElf_Ehdr *ehdr,
10195 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
10196 : : {
10197 : 52 : Elf_Data *data = get_debug_elf_data (dbg, ebl, IDX_debug_loc, scn);
10198 [ - + ]: 52 : if (data == NULL)
10199 : 0 : return;
10200 : :
10201 : 52 : printf (_("\
10202 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
10203 : : elf_ndxscn (scn), section_name (ebl, shdr),
10204 : 52 : (uint64_t) shdr->sh_offset);
10205 : :
10206 : 52 : sort_listptr (&known_locsptr, "loclistptr");
10207 : 52 : size_t listptr_idx = 0;
10208 : :
10209 [ + - ]: 52 : uint_fast8_t address_size = ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 4 : 8;
10210 : 52 : uint_fast8_t offset_size = 4;
10211 : :
10212 : 52 : bool first = true;
10213 : 52 : Dwarf_Addr base = 0;
10214 : 52 : unsigned char *readp = data->d_buf;
10215 : 52 : unsigned char *const endp = (unsigned char *) data->d_buf + data->d_size;
10216 : 52 : Dwarf_CU *last_cu = NULL;
10217 [ + + ]: 490 : while (readp < endp)
10218 : : {
10219 : 438 : ptrdiff_t offset = readp - (unsigned char *) data->d_buf;
10220 : 438 : Dwarf_CU *cu = last_cu;
10221 : 438 : unsigned int attr = 0;
10222 : :
10223 [ + + - + ]: 438 : if (first && skip_listptr_hole (&known_locsptr, &listptr_idx,
10224 : : &address_size, &offset_size, &base,
10225 : : &cu, offset, &readp, endp, &attr))
10226 : 0 : continue;
10227 : :
10228 [ + + ]: 438 : if (last_cu != cu)
10229 : : {
10230 : 60 : Dwarf_Die cudie;
10231 [ - + ]: 60 : if (dwarf_cu_die (cu, &cudie,
10232 : : NULL, NULL, NULL, NULL,
10233 : : NULL, NULL) == NULL)
10234 : 0 : printf (_("\n Unknown CU base: "));
10235 : : else
10236 : 60 : printf (_("\n CU [%6" PRIx64 "] base: "),
10237 : : dwarf_dieoffset (&cudie));
10238 : 60 : print_dwarf_addr (dwflmod, address_size, base, base);
10239 : 60 : printf ("\n");
10240 : : }
10241 : 438 : last_cu = cu;
10242 : :
10243 [ - + ]: 438 : if (attr == DW_AT_GNU_locviews)
10244 : 0 : {
10245 : 0 : Dwarf_Off next_off = next_listptr_offset (&known_locsptr,
10246 : : &listptr_idx, offset);
10247 : 0 : const unsigned char *locp = readp;
10248 : 0 : const unsigned char *locendp;
10249 [ # # ]: 0 : if (next_off == 0
10250 : 0 : || next_off > (size_t) (endp
10251 [ # # ]: 0 : - (const unsigned char *) data->d_buf))
10252 : 0 : locendp = endp;
10253 : : else
10254 : 0 : locendp = (const unsigned char *) data->d_buf + next_off;
10255 : :
10256 [ # # ]: 0 : while (locp < locendp)
10257 : : {
10258 : 0 : uint64_t v1, v2;
10259 : 0 : get_uleb128 (v1, locp, locendp);
10260 [ # # ]: 0 : if (locp >= locendp)
10261 : : {
10262 : 0 : printf (_(" [%6tx] <INVALID DATA>\n"), offset);
10263 : : break;
10264 : : }
10265 : 0 : get_uleb128 (v2, locp, locendp);
10266 [ # # ]: 0 : if (first) /* First view pair in a list. */
10267 : 0 : printf (" [%6tx] ", offset);
10268 : : else
10269 : 0 : printf (" ");
10270 : 0 : printf ("view pair %" PRId64 ", %" PRId64 "\n", v1, v2);
10271 : 0 : first = false;
10272 : : }
10273 : :
10274 : 0 : first = true;
10275 : 0 : readp = (unsigned char *) locendp;
10276 : 0 : continue;
10277 : : }
10278 : :
10279 : : /* GNU DebugFission encoded addresses as addrx. */
10280 : 876 : bool is_debugfission = ((cu != NULL
10281 [ # # ]: 0 : || split_dwarf_cu_base (dbg, &cu, &base))
10282 [ - + - + ]: 438 : && (cu->version < 5
10283 [ + + ]: 438 : && cu->unit_type == DW_UT_split_compile));
10284 [ + + ]: 438 : if (!is_debugfission
10285 [ - + ]: 318 : && unlikely (data->d_size - offset < (size_t) address_size * 2))
10286 : : {
10287 : 0 : invalid_data:
10288 : 0 : printf (_(" [%6tx] <INVALID DATA>\n"), offset);
10289 : 0 : break;
10290 : : }
10291 : :
10292 : 318 : Dwarf_Addr begin;
10293 : 318 : Dwarf_Addr end;
10294 : 438 : bool use_base = true;
10295 : 318 : if (is_debugfission)
10296 : : {
10297 : 120 : const unsigned char *locp = readp;
10298 : 120 : const unsigned char *locendp = readp + data->d_size;
10299 [ - + ]: 120 : if (locp >= locendp)
10300 : 0 : goto invalid_data;
10301 : :
10302 : 120 : Dwarf_Word idx;
10303 : 120 : unsigned char code = *locp++;
10304 [ + - - + : 120 : switch (code)
- ]
10305 : : {
10306 : 40 : case DW_LLE_GNU_end_of_list_entry:
10307 : 40 : begin = 0;
10308 : 40 : end = 0;
10309 : 40 : break;
10310 : :
10311 : 0 : case DW_LLE_GNU_base_address_selection_entry:
10312 [ # # ]: 0 : if (locp >= locendp)
10313 : 0 : goto invalid_data;
10314 : 0 : begin = (Dwarf_Addr) -1;
10315 : 0 : get_uleb128 (idx, locp, locendp);
10316 [ # # ]: 0 : if (get_indexed_addr (cu, idx, &end) != 0)
10317 : 0 : end = idx; /* ... */
10318 : : break;
10319 : :
10320 : 0 : case DW_LLE_GNU_start_end_entry:
10321 [ # # ]: 0 : if (locp >= locendp)
10322 : 0 : goto invalid_data;
10323 : 0 : get_uleb128 (idx, locp, locendp);
10324 [ # # ]: 0 : if (get_indexed_addr (cu, idx, &begin) != 0)
10325 : 0 : begin = idx; /* ... */
10326 [ # # ]: 0 : if (locp >= locendp)
10327 : 0 : goto invalid_data;
10328 : 0 : get_uleb128 (idx, locp, locendp);
10329 [ # # ]: 0 : if (get_indexed_addr (cu, idx, &end) != 0)
10330 : 0 : end = idx; /* ... */
10331 : : use_base = false;
10332 : : break;
10333 : :
10334 : 80 : case DW_LLE_GNU_start_length_entry:
10335 [ - + ]: 80 : if (locp >= locendp)
10336 : 0 : goto invalid_data;
10337 : 80 : get_uleb128 (idx, locp, locendp);
10338 [ - + ]: 80 : if (get_indexed_addr (cu, idx, &begin) != 0)
10339 : 0 : begin = idx; /* ... */
10340 [ - + ]: 80 : if (locendp - locp < 4)
10341 : 0 : goto invalid_data;
10342 [ - + ]: 80 : end = read_4ubyte_unaligned_inc (dbg, locp);
10343 : 80 : end += begin;
10344 : 80 : use_base = false;
10345 : 80 : break;
10346 : :
10347 : 0 : default:
10348 : 0 : goto invalid_data;
10349 : : }
10350 : :
10351 : 120 : readp = (unsigned char *) locp;
10352 : : }
10353 [ + - ]: 318 : else if (address_size == 8)
10354 : : {
10355 [ + + ]: 318 : begin = read_8ubyte_unaligned_inc (dbg, readp);
10356 [ + + ]: 318 : end = read_8ubyte_unaligned_inc (dbg, readp);
10357 : : }
10358 : : else
10359 : : {
10360 [ # # ]: 0 : begin = read_4ubyte_unaligned_inc (dbg, readp);
10361 [ # # ]: 0 : end = read_4ubyte_unaligned_inc (dbg, readp);
10362 [ # # ]: 0 : if (begin == (Dwarf_Addr) (uint32_t) -1)
10363 : 0 : begin = (Dwarf_Addr) -1l;
10364 : : }
10365 : :
10366 [ - + ]: 438 : if (begin == (Dwarf_Addr) -1l) /* Base address entry. */
10367 : : {
10368 [ # # ]: 0 : if (first)
10369 : 0 : printf (" [%6tx] ", offset);
10370 : : else
10371 : 0 : printf (" ");
10372 : 0 : puts (_("base address"));
10373 : 0 : printf (" ");
10374 : 0 : print_dwarf_addr (dwflmod, address_size, end, end);
10375 : 0 : printf ("\n");
10376 : 0 : base = end;
10377 : 0 : first = false;
10378 : : }
10379 [ + + + + ]: 438 : else if (begin == 0 && end == 0) /* End of list entry. */
10380 : : {
10381 [ - + ]: 148 : if (first)
10382 : 438 : printf (_(" [%6tx] empty list\n"), offset);
10383 : : first = true;
10384 : : }
10385 : : else
10386 : : {
10387 : : /* We have a location expression entry. */
10388 [ + + ]: 290 : uint_fast16_t len = read_2ubyte_unaligned_inc (dbg, readp);
10389 : :
10390 [ + + ]: 290 : if (first) /* First entry in a list. */
10391 : 148 : printf (" [%6tx] ", offset);
10392 : : else
10393 : 142 : printf (" ");
10394 : :
10395 : 290 : printf ("range %" PRIx64 ", %" PRIx64 "\n", begin, end);
10396 [ + + ]: 290 : if (! print_unresolved_addresses)
10397 : : {
10398 [ + + ]: 230 : Dwarf_Addr dab = use_base ? base + begin : begin;
10399 [ + + ]: 230 : Dwarf_Addr dae = use_base ? base + end : end;
10400 : 230 : printf (" ");
10401 : 230 : print_dwarf_addr (dwflmod, address_size, dab, dab);
10402 : 230 : printf ("..\n ");
10403 : 230 : print_dwarf_addr (dwflmod, address_size, dae - 1, dae);
10404 : 230 : printf ("\n");
10405 : : }
10406 : :
10407 [ - + ]: 290 : if (endp - readp <= (ptrdiff_t) len)
10408 : : {
10409 : 0 : fputs (_(" <INVALID DATA>\n"), stdout);
10410 : 0 : break;
10411 : : }
10412 : :
10413 [ + - ]: 580 : print_ops (dwflmod, dbg, 11, 11,
10414 : 290 : cu != NULL ? cu->version : 3,
10415 : : address_size, offset_size, cu, len, readp);
10416 : :
10417 : 290 : first = false;
10418 : 290 : readp += len;
10419 : : }
10420 : : }
10421 : : }
10422 : :
10423 : : struct mac_culist
10424 : : {
10425 : : Dwarf_Die die;
10426 : : Dwarf_Off offset;
10427 : : Dwarf_Files *files;
10428 : : struct mac_culist *next;
10429 : : };
10430 : :
10431 : :
10432 : : static int
10433 : 0 : mac_compare (const void *p1, const void *p2)
10434 : : {
10435 : 0 : struct mac_culist *m1 = (struct mac_culist *) p1;
10436 : 0 : struct mac_culist *m2 = (struct mac_culist *) p2;
10437 : :
10438 [ # # ]: 0 : if (m1->offset < m2->offset)
10439 : : return -1;
10440 [ # # ]: 0 : if (m1->offset > m2->offset)
10441 : 0 : return 1;
10442 : : return 0;
10443 : : }
10444 : :
10445 : :
10446 : : static void
10447 : 0 : print_debug_macinfo_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
10448 : : Ebl *ebl,
10449 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
10450 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
10451 : : {
10452 : 0 : Elf_Data *data = get_debug_elf_data (dbg, ebl, IDX_debug_macinfo, scn);
10453 [ # # ]: 0 : if (data == NULL)
10454 : 0 : return;
10455 : :
10456 : 0 : printf (_("\
10457 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
10458 : : elf_ndxscn (scn), section_name (ebl, shdr),
10459 : 0 : (uint64_t) shdr->sh_offset);
10460 : 0 : fputc ('\n', stdout);
10461 : :
10462 : : /* There is no function in libdw to iterate over the raw content of
10463 : : the section but it is easy enough to do. */
10464 : :
10465 : : /* Get the source file information for all CUs. */
10466 : 0 : Dwarf_Off offset;
10467 : 0 : Dwarf_Off ncu = 0;
10468 : 0 : size_t hsize;
10469 : 0 : struct mac_culist *culist = NULL;
10470 : 0 : size_t nculist = 0;
10471 [ # # ]: 0 : while (dwarf_nextcu (dbg, offset = ncu, &ncu, &hsize, NULL, NULL, NULL) == 0)
10472 : : {
10473 : 0 : Dwarf_Die cudie;
10474 [ # # ]: 0 : if (dwarf_offdie (dbg, offset + hsize, &cudie) == NULL)
10475 : 0 : continue;
10476 : :
10477 : 0 : Dwarf_Attribute attr;
10478 [ # # ]: 0 : if (dwarf_attr (&cudie, DW_AT_macro_info, &attr) == NULL)
10479 : 0 : continue;
10480 : :
10481 : 0 : Dwarf_Word macoff;
10482 [ # # ]: 0 : if (dwarf_formudata (&attr, &macoff) != 0)
10483 : 0 : continue;
10484 : :
10485 : 0 : struct mac_culist *newp = (struct mac_culist *) alloca (sizeof (*newp));
10486 : 0 : newp->die = cudie;
10487 : 0 : newp->offset = macoff;
10488 : 0 : newp->files = NULL;
10489 : 0 : newp->next = culist;
10490 : 0 : culist = newp;
10491 : 0 : ++nculist;
10492 : : }
10493 : :
10494 : : /* Convert the list into an array for easier consumption. */
10495 : 0 : struct mac_culist *cus = (struct mac_culist *) alloca ((nculist + 1)
10496 : : * sizeof (*cus));
10497 : : /* Add sentinel. */
10498 : 0 : cus[nculist].offset = data->d_size;
10499 : 0 : cus[nculist].files = (Dwarf_Files *) -1l;
10500 [ # # ]: 0 : if (nculist > 0)
10501 : : {
10502 [ # # ]: 0 : for (size_t cnt = nculist - 1; culist != NULL; --cnt)
10503 : : {
10504 [ # # ]: 0 : assert (cnt < nculist);
10505 : 0 : cus[cnt] = *culist;
10506 : 0 : culist = culist->next;
10507 : : }
10508 : :
10509 : : /* Sort the array according to the offset in the .debug_macinfo
10510 : : section. Note we keep the sentinel at the end. */
10511 : 0 : qsort (cus, nculist, sizeof (*cus), mac_compare);
10512 : : }
10513 : :
10514 : 0 : const unsigned char *readp = (const unsigned char *) data->d_buf;
10515 : 0 : const unsigned char *readendp = readp + data->d_size;
10516 : 0 : int level = 1;
10517 : :
10518 : 0 : while (readp < readendp)
10519 : : {
10520 : 0 : unsigned int opcode = *readp++;
10521 : 0 : unsigned int u128;
10522 : 0 : unsigned int u128_2;
10523 : 0 : const unsigned char *endp;
10524 : :
10525 [ # # # # ]: 0 : switch (opcode)
10526 : : {
10527 : 0 : case DW_MACINFO_define:
10528 : : case DW_MACINFO_undef:
10529 : : case DW_MACINFO_vendor_ext:
10530 : : /* For the first two opcodes the parameters are
10531 : : line, string
10532 : : For the latter
10533 : : number, string.
10534 : : We can treat these cases together. */
10535 : 0 : get_uleb128 (u128, readp, readendp);
10536 : :
10537 : 0 : endp = memchr (readp, '\0', readendp - readp);
10538 [ # # ]: 0 : if (unlikely (endp == NULL))
10539 : : {
10540 : 0 : printf (_("\
10541 : : %*s*** non-terminated string at end of section"),
10542 : : level, "");
10543 : 0 : return;
10544 : : }
10545 : :
10546 [ # # ]: 0 : if (opcode == DW_MACINFO_define)
10547 : 0 : printf ("%*s#define %s, line %u\n",
10548 : : level, "", (char *) readp, u128);
10549 [ # # ]: 0 : else if (opcode == DW_MACINFO_undef)
10550 : 0 : printf ("%*s#undef %s, line %u\n",
10551 : : level, "", (char *) readp, u128);
10552 : : else
10553 : 0 : printf (" #vendor-ext %s, number %u\n", (char *) readp, u128);
10554 : :
10555 : 0 : readp = endp + 1;
10556 : 0 : break;
10557 : :
10558 : 0 : case DW_MACINFO_start_file:
10559 : : /* The two parameters are line and file index, in this order. */
10560 : 0 : get_uleb128 (u128, readp, readendp);
10561 [ # # ]: 0 : if (readendp - readp < 1)
10562 : : {
10563 : 0 : printf (_("\
10564 : : %*s*** missing DW_MACINFO_start_file argument at end of section"),
10565 : : level, "");
10566 : 0 : return;
10567 : : }
10568 : 0 : get_uleb128 (u128_2, readp, readendp);
10569 : :
10570 : : /* Find the CU DIE for this file. */
10571 : 0 : size_t macoff = readp - (const unsigned char *) data->d_buf;
10572 : 0 : const char *fname = "???";
10573 [ # # # # ]: 0 : if (macoff >= cus[0].offset && cus[0].offset != data->d_size)
10574 : : {
10575 [ # # # # ]: 0 : while (macoff >= cus[1].offset && cus[1].offset != data->d_size)
10576 : 0 : ++cus;
10577 : :
10578 [ # # ]: 0 : if (cus[0].files == NULL
10579 [ # # ]: 0 : && dwarf_getsrcfiles (&cus[0].die, &cus[0].files, NULL) != 0)
10580 : 0 : cus[0].files = (Dwarf_Files *) -1l;
10581 : :
10582 [ # # ]: 0 : if (cus[0].files != (Dwarf_Files *) -1l)
10583 : 0 : fname = (dwarf_filesrc (cus[0].files, u128_2, NULL, NULL)
10584 [ # # ]: 0 : ?: "???");
10585 : : }
10586 : :
10587 : 0 : printf ("%*sstart_file %u, [%u] %s\n",
10588 : : level, "", u128, u128_2, fname);
10589 : 0 : ++level;
10590 : 0 : break;
10591 : :
10592 : 0 : case DW_MACINFO_end_file:
10593 : 0 : --level;
10594 : 0 : printf ("%*send_file\n", level, "");
10595 : : /* Nothing more to do. */
10596 : : break;
10597 : :
10598 : 0 : default:
10599 : : // XXX gcc seems to generate files with a trailing zero.
10600 [ # # # # ]: 0 : if (unlikely (opcode != 0 || readp != readendp))
10601 [ # # ]: 0 : printf ("%*s*** invalid opcode %u\n", level, "", opcode);
10602 : : break;
10603 : : }
10604 : : }
10605 : : }
10606 : :
10607 : :
10608 : : static void
10609 : 8 : print_debug_macro_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
10610 : : Ebl *ebl,
10611 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
10612 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
10613 : : {
10614 : 8 : Elf_Data *data = get_debug_elf_data (dbg, ebl, IDX_debug_macro, scn);
10615 [ + - ]: 8 : if (data == NULL)
10616 : 0 : return;
10617 : :
10618 : 8 : printf (_("\
10619 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
10620 : : elf_ndxscn (scn), section_name (ebl, shdr),
10621 : 8 : (uint64_t) shdr->sh_offset);
10622 : 8 : fputc ('\n', stdout);
10623 : :
10624 : : /* Get the source file information for all CUs. Uses same
10625 : : datastructure as macinfo. But uses offset field to directly
10626 : : match .debug_line offset. And just stored in a list. */
10627 : 8 : Dwarf_Off offset;
10628 : 8 : Dwarf_Off ncu = 0;
10629 : 8 : size_t hsize;
10630 : 8 : struct mac_culist *culist = NULL;
10631 [ + + ]: 20 : while (dwarf_nextcu (dbg, offset = ncu, &ncu, &hsize, NULL, NULL, NULL) == 0)
10632 : : {
10633 : 12 : Dwarf_Die cudie;
10634 [ - + ]: 12 : if (dwarf_offdie (dbg, offset + hsize, &cudie) == NULL)
10635 : 0 : continue;
10636 : :
10637 : 12 : Dwarf_Attribute attr;
10638 [ - + ]: 12 : if (dwarf_attr (&cudie, DW_AT_stmt_list, &attr) == NULL)
10639 : 0 : continue;
10640 : :
10641 : 12 : Dwarf_Word lineoff;
10642 [ - + ]: 12 : if (dwarf_formudata (&attr, &lineoff) != 0)
10643 : 0 : continue;
10644 : :
10645 : 12 : struct mac_culist *newp = (struct mac_culist *) alloca (sizeof (*newp));
10646 : 12 : newp->die = cudie;
10647 : 12 : newp->offset = lineoff;
10648 : 12 : newp->files = NULL;
10649 : 12 : newp->next = culist;
10650 : 12 : culist = newp;
10651 : : }
10652 : :
10653 : 8 : const unsigned char *readp = (const unsigned char *) data->d_buf;
10654 : 8 : const unsigned char *readendp = readp + data->d_size;
10655 : :
10656 [ + + ]: 28 : while (readp < readendp)
10657 : : {
10658 : 40 : printf (_(" Offset: 0x%" PRIx64 "\n"),
10659 : 20 : (uint64_t) (readp - (const unsigned char *) data->d_buf));
10660 : :
10661 : : // Header, 2 byte version, 1 byte flag, optional .debug_line offset,
10662 : : // optional vendor extension macro entry table.
10663 [ - + ]: 20 : if (readp + 2 > readendp)
10664 : : {
10665 : 0 : invalid_data:
10666 : 0 : error (0, 0, _("invalid data"));
10667 : 0 : return;
10668 : : }
10669 [ - + ]: 20 : const uint16_t vers = read_2ubyte_unaligned_inc (dbg, readp);
10670 : 20 : printf (_(" Version: %" PRIu16 "\n"), vers);
10671 : :
10672 : : // Version 4 is the GNU extension for DWARF4. DWARF5 will use version
10673 : : // 5 when it gets standardized.
10674 [ - + ]: 20 : if (vers != 4 && vers != 5)
10675 : : {
10676 : 0 : printf (_(" unknown version, cannot parse section\n"));
10677 : 0 : return;
10678 : : }
10679 : :
10680 [ - + ]: 20 : if (readp + 1 > readendp)
10681 : 0 : goto invalid_data;
10682 : 20 : const unsigned char flag = *readp++;
10683 : 20 : printf (_(" Flag: 0x%" PRIx8), flag);
10684 [ + + ]: 20 : if (flag != 0)
10685 : : {
10686 : 12 : printf (" (");
10687 [ - + ]: 12 : if ((flag & 0x01) != 0)
10688 : : {
10689 : 0 : printf ("offset_size");
10690 [ # # ]: 0 : if ((flag & 0xFE) != 0)
10691 : 0 : printf (", ");
10692 : : }
10693 [ + - ]: 12 : if ((flag & 0x02) != 0)
10694 : : {
10695 : 12 : printf ("debug_line_offset");
10696 [ - + ]: 12 : if ((flag & 0xFC) != 0)
10697 : 0 : printf (", ");
10698 : : }
10699 [ # # ]: 0 : if ((flag & 0x04) != 0)
10700 : : {
10701 : 0 : printf ("operands_table");
10702 [ # # ]: 0 : if ((flag & 0xF8) != 0)
10703 : 0 : printf (", ");
10704 : : }
10705 [ - + ]: 12 : if ((flag & 0xF8) != 0)
10706 : 0 : printf ("unknown");
10707 : 12 : printf (")");
10708 : : }
10709 : 20 : printf ("\n");
10710 : :
10711 [ + - ]: 20 : unsigned int offset_len = (flag & 0x01) ? 8 : 4;
10712 : 20 : printf (_(" Offset length: %" PRIu8 "\n"), offset_len);
10713 : 20 : Dwarf_Off line_offset = -1;
10714 [ + + ]: 20 : if (flag & 0x02)
10715 : : {
10716 [ - + ]: 12 : if (offset_len == 8)
10717 [ # # ]: 0 : line_offset = read_8ubyte_unaligned_inc (dbg, readp);
10718 : : else
10719 [ - + ]: 12 : line_offset = read_4ubyte_unaligned_inc (dbg, readp);
10720 : 12 : printf (_(" .debug_line offset: 0x%" PRIx64 "\n"),
10721 : : line_offset);
10722 : : }
10723 : :
10724 : 12 : struct mac_culist *cu = NULL;
10725 [ + - ]: 12 : if (line_offset != (Dwarf_Off) -1)
10726 : : {
10727 : : cu = culist;
10728 [ + - + + ]: 16 : while (cu != NULL && line_offset != cu->offset)
10729 : 4 : cu = cu->next;
10730 : : }
10731 : :
10732 [ - + ]: 20 : Dwarf_Off str_offsets_base = str_offsets_base_off (dbg, (cu != NULL
10733 : : ? cu->die.cu
10734 : : : NULL));
10735 : :
10736 : 20 : const unsigned char *vendor[DW_MACRO_hi_user - DW_MACRO_lo_user + 1];
10737 [ - + ]: 20 : memset (vendor, 0, sizeof vendor);
10738 [ - + ]: 20 : if (flag & 0x04)
10739 : : {
10740 : : // 1 byte length, for each item, 1 byte opcode, uleb128 number
10741 : : // of arguments, for each argument 1 byte form code.
10742 [ # # ]: 0 : if (readp + 1 > readendp)
10743 : 0 : goto invalid_data;
10744 : 0 : unsigned int tlen = *readp++;
10745 : 0 : printf (_(" extension opcode table, %" PRIu8 " items:\n"),
10746 : : tlen);
10747 [ # # ]: 0 : for (unsigned int i = 0; i < tlen; i++)
10748 : : {
10749 [ # # ]: 0 : if (readp + 1 > readendp)
10750 : 0 : goto invalid_data;
10751 : 0 : unsigned int opcode = *readp++;
10752 : 0 : printf (_(" [%" PRIx8 "]"), opcode);
10753 : 0 : if (opcode < DW_MACRO_lo_user
10754 [ # # ]: 0 : || opcode > DW_MACRO_hi_user)
10755 : 0 : goto invalid_data;
10756 : : // Record the start of description for this vendor opcode.
10757 : : // uleb128 nr args, 1 byte per arg form.
10758 : 0 : vendor[opcode - DW_MACRO_lo_user] = readp;
10759 [ # # ]: 0 : if (readp + 1 > readendp)
10760 : 0 : goto invalid_data;
10761 : 0 : unsigned int args = *readp++;
10762 [ # # ]: 0 : if (args > 0)
10763 : : {
10764 : 0 : printf (_(" %" PRIu8 " arguments:"), args);
10765 : 0 : while (args > 0)
10766 : : {
10767 : 0 : if (readp + 1 > readendp)
10768 : 0 : goto invalid_data;
10769 : 0 : unsigned int form = *readp++;
10770 : 0 : printf (" %s", dwarf_form_name (form));
10771 [ # # ]: 0 : if (! libdw_valid_user_form (form))
10772 : 0 : goto invalid_data;
10773 : 0 : args--;
10774 [ # # ]: 0 : if (args > 0)
10775 [ # # ]: 0 : putchar (',');
10776 : : }
10777 : : }
10778 : : else
10779 : 0 : printf (_(" no arguments."));
10780 : 0 : putchar ('\n');
10781 : : }
10782 : : }
10783 : 20 : putchar ('\n');
10784 : :
10785 : 20 : int level = 1;
10786 [ - + ]: 20 : if (readp + 1 > readendp)
10787 : 0 : goto invalid_data;
10788 : 20 : unsigned int opcode = *readp++;
10789 : 20 : while (opcode != 0)
10790 : : {
10791 : 3026 : unsigned int u128;
10792 : 3026 : unsigned int u128_2;
10793 : 3026 : const unsigned char *endp;
10794 : 3026 : uint64_t off;
10795 : :
10796 [ + + + - : 3026 : switch (opcode)
+ + + - -
- + + - ]
10797 : : {
10798 : 20 : case DW_MACRO_start_file:
10799 : 20 : get_uleb128 (u128, readp, readendp);
10800 [ - + ]: 20 : if (readp >= readendp)
10801 : 0 : goto invalid_data;
10802 : 20 : get_uleb128 (u128_2, readp, readendp);
10803 : :
10804 : : /* Find the CU DIE that matches this line offset. */
10805 : 20 : const char *fname = "???";
10806 [ - + ]: 20 : if (cu != NULL)
10807 : : {
10808 [ + + ]: 20 : if (cu->files == NULL
10809 [ - + ]: 12 : && dwarf_getsrcfiles (&cu->die, &cu->files,
10810 : : NULL) != 0)
10811 : 0 : cu->files = (Dwarf_Files *) -1l;
10812 : :
10813 [ - + ]: 20 : if (cu->files != (Dwarf_Files *) -1l)
10814 : 20 : fname = (dwarf_filesrc (cu->files, u128_2,
10815 [ - + ]: 20 : NULL, NULL) ?: "???");
10816 : : }
10817 : 20 : printf ("%*sstart_file %u, [%u] %s\n",
10818 : : level, "", u128, u128_2, fname);
10819 : 20 : ++level;
10820 : 20 : break;
10821 : :
10822 : 20 : case DW_MACRO_end_file:
10823 : 20 : --level;
10824 : 20 : printf ("%*send_file\n", level, "");
10825 : : break;
10826 : :
10827 : 2 : case DW_MACRO_define:
10828 : 2 : get_uleb128 (u128, readp, readendp);
10829 : 2 : endp = memchr (readp, '\0', readendp - readp);
10830 [ - + ]: 2 : if (endp == NULL)
10831 : 0 : goto invalid_data;
10832 : 2 : printf ("%*s#define %s, line %u\n",
10833 : : level, "", readp, u128);
10834 : 2 : readp = endp + 1;
10835 : 2 : break;
10836 : :
10837 : 0 : case DW_MACRO_undef:
10838 : 0 : get_uleb128 (u128, readp, readendp);
10839 : 0 : endp = memchr (readp, '\0', readendp - readp);
10840 [ # # ]: 0 : if (endp == NULL)
10841 : 0 : goto invalid_data;
10842 : 0 : printf ("%*s#undef %s, line %u\n",
10843 : : level, "", readp, u128);
10844 : 0 : readp = endp + 1;
10845 : 0 : break;
10846 : :
10847 : 1414 : case DW_MACRO_define_strp:
10848 : 1414 : get_uleb128 (u128, readp, readendp);
10849 [ - + ]: 1414 : if (readp + offset_len > readendp)
10850 : 0 : goto invalid_data;
10851 [ - + ]: 1414 : if (offset_len == 8)
10852 [ # # ]: 0 : off = read_8ubyte_unaligned_inc (dbg, readp);
10853 : : else
10854 [ - + ]: 1414 : off = read_4ubyte_unaligned_inc (dbg, readp);
10855 : 1414 : printf ("%*s#define %s, line %u (indirect)\n",
10856 : : level, "", dwarf_getstring (dbg, off, NULL), u128);
10857 : : break;
10858 : :
10859 : 2 : case DW_MACRO_undef_strp:
10860 : 2 : get_uleb128 (u128, readp, readendp);
10861 [ - + ]: 2 : if (readp + offset_len > readendp)
10862 : 0 : goto invalid_data;
10863 [ - + ]: 2 : if (offset_len == 8)
10864 [ # # ]: 0 : off = read_8ubyte_unaligned_inc (dbg, readp);
10865 : : else
10866 [ - + ]: 2 : off = read_4ubyte_unaligned_inc (dbg, readp);
10867 : 2 : printf ("%*s#undef %s, line %u (indirect)\n",
10868 : : level, "", dwarf_getstring (dbg, off, NULL), u128);
10869 : : break;
10870 : :
10871 : 12 : case DW_MACRO_import:
10872 [ - + ]: 12 : if (readp + offset_len > readendp)
10873 : 0 : goto invalid_data;
10874 [ - + ]: 12 : if (offset_len == 8)
10875 [ # # ]: 0 : off = read_8ubyte_unaligned_inc (dbg, readp);
10876 : : else
10877 [ - + ]: 12 : off = read_4ubyte_unaligned_inc (dbg, readp);
10878 : 12 : printf ("%*s#include offset 0x%" PRIx64 "\n",
10879 : : level, "", off);
10880 : : break;
10881 : :
10882 : 0 : case DW_MACRO_define_sup:
10883 : 0 : get_uleb128 (u128, readp, readendp);
10884 : 0 : printf ("%*s#define ", level, "");
10885 : 0 : readp = print_form_data (dbg, DW_FORM_strp_sup,
10886 : : readp, readendp, offset_len,
10887 : : str_offsets_base);
10888 : 0 : printf (", line %u (sup)\n", u128);
10889 : : break;
10890 : :
10891 : 0 : case DW_MACRO_undef_sup:
10892 : 0 : get_uleb128 (u128, readp, readendp);
10893 : 0 : printf ("%*s#undef ", level, "");
10894 : 0 : readp = print_form_data (dbg, DW_FORM_strp_sup,
10895 : : readp, readendp, offset_len,
10896 : : str_offsets_base);
10897 : 0 : printf (", line %u (sup)\n", u128);
10898 : : break;
10899 : :
10900 : 0 : case DW_MACRO_import_sup:
10901 [ # # ]: 0 : if (readp + offset_len > readendp)
10902 : 0 : goto invalid_data;
10903 [ # # ]: 0 : if (offset_len == 8)
10904 [ # # ]: 0 : off = read_8ubyte_unaligned_inc (dbg, readp);
10905 : : else
10906 [ # # ]: 0 : off = read_4ubyte_unaligned_inc (dbg, readp);
10907 : : // XXX Needs support for reading from supplementary object file.
10908 : 0 : printf ("%*s#include offset 0x%" PRIx64 " (sup)\n",
10909 : : level, "", off);
10910 : : break;
10911 : :
10912 : 1552 : case DW_MACRO_define_strx:
10913 : 1552 : get_uleb128 (u128, readp, readendp);
10914 : 1552 : printf ("%*s#define ", level, "");
10915 : 1552 : readp = print_form_data (dbg, DW_FORM_strx,
10916 : : readp, readendp, offset_len,
10917 : : str_offsets_base);
10918 : 1552 : printf (", line %u (strx)\n", u128);
10919 : : break;
10920 : :
10921 : 4 : case DW_MACRO_undef_strx:
10922 : 4 : get_uleb128 (u128, readp, readendp);
10923 : 4 : printf ("%*s#undef ", level, "");
10924 : 4 : readp = print_form_data (dbg, DW_FORM_strx,
10925 : : readp, readendp, offset_len,
10926 : : str_offsets_base);
10927 : 4 : printf (", line %u (strx)\n", u128);
10928 : : break;
10929 : :
10930 : : default:
10931 : 0 : printf ("%*svendor opcode 0x%" PRIx8, level, "", opcode);
10932 [ # # ]: 0 : if (opcode < DW_MACRO_lo_user
10933 : : || opcode > DW_MACRO_lo_user
10934 [ # # ]: 0 : || vendor[opcode - DW_MACRO_lo_user] == NULL)
10935 : 0 : goto invalid_data;
10936 : :
10937 : 0 : const unsigned char *op_desc;
10938 : 0 : op_desc = vendor[opcode - DW_MACRO_lo_user];
10939 : :
10940 : : // Just skip the arguments, we cannot really interpret them,
10941 : : // but print as much as we can.
10942 : 0 : unsigned int args = *op_desc++;
10943 [ # # ]: 0 : while (args > 0 && readp < readendp)
10944 : : {
10945 : 0 : unsigned int form = *op_desc++;
10946 : 0 : readp = print_form_data (dbg, form, readp, readendp,
10947 : : offset_len, str_offsets_base);
10948 : 0 : args--;
10949 [ # # ]: 0 : if (args > 0)
10950 [ # # ]: 0 : printf (", ");
10951 : : }
10952 : 0 : putchar ('\n');
10953 : : }
10954 : :
10955 [ - + ]: 3026 : if (readp + 1 > readendp)
10956 : 0 : goto invalid_data;
10957 : 3026 : opcode = *readp++;
10958 [ + + ]: 3026 : if (opcode == 0)
10959 [ + + ]: 3066 : putchar ('\n');
10960 : : }
10961 : : }
10962 : : }
10963 : :
10964 : :
10965 : : /* Callback for printing global names. */
10966 : : static int
10967 : 68 : print_pubnames (Dwarf *dbg __attribute__ ((unused)), Dwarf_Global *global,
10968 : : void *arg)
10969 : : {
10970 : 68 : int *np = (int *) arg;
10971 : :
10972 : 136 : printf (_(" [%5d] DIE offset: %6" PRId64
10973 : : ", CU DIE offset: %6" PRId64 ", name: %s\n"),
10974 : 68 : (*np)++, global->die_offset, global->cu_offset, global->name);
10975 : :
10976 : 68 : return 0;
10977 : : }
10978 : :
10979 : :
10980 : : /* Print the known exported symbols in the DWARF section '.debug_pubnames'. */
10981 : : static void
10982 : 16 : print_debug_pubnames_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
10983 : : Ebl *ebl,
10984 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
10985 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
10986 : : {
10987 : : /* Check section actually exists. */
10988 [ - + ]: 16 : if (get_debug_elf_data (dbg, ebl, IDX_debug_pubnames, scn) == NULL)
10989 : 0 : return;
10990 : :
10991 : 16 : printf (_("\nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
10992 : : elf_ndxscn (scn), section_name (ebl, shdr),
10993 : 16 : (uint64_t) shdr->sh_offset);
10994 : :
10995 : 16 : int n = 0;
10996 : 16 : (void) dwarf_getpubnames (dbg, print_pubnames, &n, 0);
10997 : : }
10998 : :
10999 : : /* Print the content of the DWARF string section '.debug_str'
11000 : : or 'debug_line_str'. */
11001 : : static void
11002 : 134 : print_debug_str_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
11003 : : Ebl *ebl,
11004 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
11005 : : Elf_Scn *scn, GElf_Shdr *shdr,
11006 : : Dwarf *dbg __attribute__ ((unused)))
11007 : : {
11008 : 134 : const char *name = section_name (ebl, shdr);
11009 [ + + ]: 134 : int idx = ((name != NULL && strstr (name, "debug_line_str") != NULL)
11010 [ + - ]: 134 : ? IDX_debug_line_str : IDX_debug_str);
11011 : 134 : Elf_Data *data = get_debug_elf_data (dbg, ebl, idx, scn);
11012 [ + - ]: 134 : if (data == NULL)
11013 : : return;
11014 : :
11015 : 134 : const size_t sh_size = data->d_size;
11016 : :
11017 : : /* Compute floor(log16(shdr->sh_size)). */
11018 : 134 : GElf_Addr tmp = sh_size;
11019 : 134 : int digits = 1;
11020 [ + + ]: 466 : while (tmp >= 16)
11021 : : {
11022 : 332 : ++digits;
11023 : 332 : tmp >>= 4;
11024 : : }
11025 : 134 : digits = MAX (4, digits);
11026 : :
11027 : 134 : printf (_("\nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"
11028 : : " %*s String\n"),
11029 : : elf_ndxscn (scn),
11030 : 134 : section_name (ebl, shdr), (uint64_t) shdr->sh_offset,
11031 : : /* TRANS: the debugstr| prefix makes the string unique. */
11032 [ + - ]: 134 : digits + 2, sgettext ("debugstr|Offset"));
11033 : :
11034 : 134 : Dwarf_Off offset = 0;
11035 [ + + ]: 160460 : while (offset < sh_size)
11036 : : {
11037 : 160326 : size_t len;
11038 : 160326 : const char *str = (const char *) data->d_buf + offset;
11039 : 160326 : const char *endp = memchr (str, '\0', sh_size - offset);
11040 [ - + ]: 160326 : if (unlikely (endp == NULL))
11041 : : {
11042 : 0 : printf (_(" *** error, missing string terminator\n"));
11043 : : break;
11044 : : }
11045 : :
11046 : 160326 : printf (" [%*" PRIx64 "] \"%s\"\n", digits, (uint64_t) offset, str);
11047 : 160326 : len = endp - str;
11048 : 160326 : offset += len + 1;
11049 : : }
11050 : : }
11051 : :
11052 : : static void
11053 : 12 : print_debug_str_offsets_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
11054 : : Ebl *ebl,
11055 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
11056 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
11057 : : {
11058 : 12 : Elf_Data *data = get_debug_elf_data (dbg, ebl, IDX_debug_str_offsets, scn);
11059 [ + - ]: 12 : if (data == NULL)
11060 : : return;
11061 : :
11062 : 12 : printf (_("\
11063 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
11064 : : elf_ndxscn (scn), section_name (ebl, shdr),
11065 : 12 : (uint64_t) shdr->sh_offset);
11066 : :
11067 [ + - ]: 12 : if (shdr->sh_size == 0)
11068 : : return;
11069 : :
11070 : 12 : size_t idx = 0;
11071 : 12 : sort_listptr (&known_stroffbases, "str_offsets");
11072 : :
11073 : 12 : const unsigned char *start = (const unsigned char *) data->d_buf;
11074 : 12 : const unsigned char *readp = start;
11075 : 12 : const unsigned char *readendp = ((const unsigned char *) data->d_buf
11076 : 12 : + data->d_size);
11077 : :
11078 : 12 : while (readp < readendp)
11079 : : {
11080 : : /* Most string offset tables will have a header. For split
11081 : : dwarf unit GNU DebugFission didn't add one. But they were
11082 : : also only defined for split units (main or skeleton units
11083 : : didn't have indirect strings). So if we don't have a
11084 : : DW_AT_str_offsets_base at all and this is offset zero, then
11085 : : just start printing offsets immediately, if this is a .dwo
11086 : : section. */
11087 : 16 : Dwarf_Off off = (Dwarf_Off) (readp
11088 : 16 : - (const unsigned char *) data->d_buf);
11089 : :
11090 : 16 : printf ("Table at offset %" PRIx64 " ", off);
11091 : :
11092 [ + + ]: 16 : struct listptr *listptr = get_listptr (&known_stroffbases, idx++);
11093 : 16 : const unsigned char *next_unitp = readendp;
11094 : 8 : uint8_t offset_size;
11095 : 8 : bool has_header;
11096 [ - + ]: 8 : if (listptr == NULL)
11097 : : {
11098 : : /* This can happen for .dwo files. There is only an header
11099 : : in the case this is a version 5 split DWARF file. */
11100 : 8 : Dwarf_CU *cu;
11101 : 8 : uint8_t unit_type;
11102 [ - + ]: 8 : if (dwarf_get_units (dbg, NULL, &cu, NULL, &unit_type,
11103 : : NULL, NULL) != 0)
11104 : : {
11105 : 0 : error (0, 0, "Warning: Cannot find any DWARF unit.");
11106 : : /* Just guess some values. */
11107 : 0 : has_header = false;
11108 : 0 : offset_size = 4;
11109 : : }
11110 [ + - ]: 8 : else if (off == 0
11111 : 8 : && (unit_type == DW_UT_split_type
11112 [ + - ]: 8 : || unit_type == DW_UT_split_compile))
11113 : : {
11114 : 8 : has_header = cu->version > 4;
11115 : 8 : offset_size = cu->offset_size;
11116 : : }
11117 : : else
11118 : : {
11119 : 0 : error (0, 0,
11120 : : "Warning: No CU references .debug_str_offsets after %"
11121 : : PRIx64, off);
11122 : 0 : has_header = cu->version > 4;
11123 : 0 : offset_size = cu->offset_size;
11124 : : }
11125 : 8 : printf ("\n");
11126 : : }
11127 : : else
11128 : : {
11129 : : /* This must be DWARF5, since GNU DebugFission didn't define
11130 : : DW_AT_str_offsets_base. */
11131 : 8 : has_header = true;
11132 : :
11133 : 8 : Dwarf_Die cudie;
11134 [ - + ]: 8 : if (dwarf_cu_die (listptr->cu, &cudie,
11135 : : NULL, NULL, NULL, NULL,
11136 : : NULL, NULL) == NULL)
11137 : 0 : printf ("Unknown CU (%s):\n", dwarf_errmsg (-1));
11138 : : else
11139 : 8 : printf ("for CU [%6" PRIx64 "]:\n", dwarf_dieoffset (&cudie));
11140 : : }
11141 : :
11142 [ + + ]: 16 : if (has_header)
11143 : : {
11144 : 12 : uint64_t unit_length;
11145 : 12 : uint16_t version;
11146 : 12 : uint16_t padding;
11147 : :
11148 [ - + ]: 12 : unit_length = read_4ubyte_unaligned_inc (dbg, readp);
11149 [ - + ]: 12 : if (unlikely (unit_length == 0xffffffff))
11150 : : {
11151 [ # # ]: 0 : if (unlikely (readp > readendp - 8))
11152 : : {
11153 : 0 : invalid_data:
11154 : 0 : error (0, 0, "Invalid data");
11155 : 0 : return;
11156 : : }
11157 [ # # ]: 0 : unit_length = read_8ubyte_unaligned_inc (dbg, readp);
11158 : 0 : offset_size = 8;
11159 : : }
11160 : : else
11161 : : offset_size = 4;
11162 : :
11163 : 12 : printf ("\n");
11164 : 12 : printf (_(" Length: %8" PRIu64 "\n"),
11165 : : unit_length);
11166 : 12 : printf (_(" Offset size: %8" PRIu8 "\n"),
11167 : : offset_size);
11168 : :
11169 : : /* We need at least 2-bytes (version) + 2-bytes (padding) =
11170 : : 4 bytes to complete the header. And this unit cannot go
11171 : : beyond the section data. */
11172 [ + - ]: 12 : if (readp > readendp - 4
11173 [ + - ]: 12 : || unit_length < 4
11174 [ - + ]: 12 : || unit_length > (uint64_t) (readendp - readp))
11175 : 0 : goto invalid_data;
11176 : :
11177 : 12 : next_unitp = readp + unit_length;
11178 : :
11179 [ - + ]: 12 : version = read_2ubyte_unaligned_inc (dbg, readp);
11180 : 12 : printf (_(" DWARF version: %8" PRIu16 "\n"), version);
11181 : :
11182 [ - + ]: 12 : if (version != 5)
11183 : : {
11184 : 0 : error (0, 0, _("Unknown version"));
11185 : 0 : goto next_unit;
11186 : : }
11187 : :
11188 [ - + ]: 12 : padding = read_2ubyte_unaligned_inc (dbg, readp);
11189 : 12 : printf (_(" Padding: %8" PRIx16 "\n"), padding);
11190 : :
11191 [ + + ]: 12 : if (listptr != NULL
11192 [ - + ]: 8 : && listptr->offset != (Dwarf_Off) (readp - start))
11193 : : {
11194 : 0 : error (0, 0, "String offsets index doesn't start after header");
11195 : 0 : goto next_unit;
11196 : : }
11197 : :
11198 : 12 : printf ("\n");
11199 : : }
11200 : :
11201 : 16 : int digits = 1;
11202 : 16 : size_t offsets = (next_unitp - readp) / offset_size;
11203 [ + + ]: 24 : while (offsets >= 10)
11204 : : {
11205 : 8 : ++digits;
11206 : 8 : offsets /= 10;
11207 : : }
11208 : :
11209 : 16 : unsigned int uidx = 0;
11210 : 16 : size_t index_offset = readp - (const unsigned char *) data->d_buf;
11211 : 16 : printf (" Offsets start at 0x%zx:\n", index_offset);
11212 : 204 : while (readp <= next_unitp - offset_size)
11213 : : {
11214 : 188 : Dwarf_Word offset;
11215 [ + - ]: 188 : if (offset_size == 4)
11216 [ - + ]: 188 : offset = read_4ubyte_unaligned_inc (dbg, readp);
11217 : : else
11218 [ # # ]: 0 : offset = read_8ubyte_unaligned_inc (dbg, readp);
11219 : 188 : const char *str = dwarf_getstring (dbg, offset, NULL);
11220 [ - + + + ]: 392 : printf (" [%*u] [%*" PRIx64 "] \"%s\"\n",
11221 : 188 : digits, uidx++, (int) offset_size * 2, offset, str ?: "???");
11222 : : }
11223 : 16 : printf ("\n");
11224 : :
11225 [ + - ]: 16 : if (readp != next_unitp)
11226 [ + + ]: 28 : error (0, 0, "extra %zd bytes at end of unit",
11227 : 0 : (size_t) (next_unitp - readp));
11228 : :
11229 : 16 : next_unit:
11230 : : readp = next_unitp;
11231 : : }
11232 : : }
11233 : :
11234 : :
11235 : : /* Print the content of the call frame search table section
11236 : : '.eh_frame_hdr'. */
11237 : : static void
11238 : 140 : print_debug_frame_hdr_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
11239 : : Ebl *ebl __attribute__ ((unused)),
11240 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
11241 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
11242 : : {
11243 : 140 : printf (_("\
11244 : : \nCall frame search table section [%2zu] '.eh_frame_hdr':\n"),
11245 : : elf_ndxscn (scn));
11246 : :
11247 : 140 : Elf_Data *data = elf_rawdata (scn, NULL);
11248 : :
11249 [ - + ]: 140 : if (unlikely (data == NULL))
11250 : : {
11251 : 0 : error (0, 0, _("cannot get %s content: %s"),
11252 : : ".eh_frame_hdr", elf_errmsg (-1));
11253 : 0 : return;
11254 : : }
11255 : :
11256 : 140 : const unsigned char *readp = data->d_buf;
11257 : 140 : const unsigned char *const dataend = ((unsigned char *) data->d_buf
11258 : 140 : + data->d_size);
11259 : :
11260 [ - + ]: 140 : if (unlikely (readp + 4 > dataend))
11261 : : {
11262 : 0 : invalid_data:
11263 : 0 : error (0, 0, _("invalid data"));
11264 : 0 : return;
11265 : : }
11266 : :
11267 : 140 : unsigned int version = *readp++;
11268 : 140 : unsigned int eh_frame_ptr_enc = *readp++;
11269 : 140 : unsigned int fde_count_enc = *readp++;
11270 : 140 : unsigned int table_enc = *readp++;
11271 : :
11272 : 140 : printf (" version: %u\n"
11273 : : " eh_frame_ptr_enc: %#x ",
11274 : : version, eh_frame_ptr_enc);
11275 : 140 : print_encoding_base ("", eh_frame_ptr_enc);
11276 : 140 : printf (" fde_count_enc: %#x ", fde_count_enc);
11277 : 140 : print_encoding_base ("", fde_count_enc);
11278 : 140 : printf (" table_enc: %#x ", table_enc);
11279 : 140 : print_encoding_base ("", table_enc);
11280 : :
11281 : 140 : uint64_t eh_frame_ptr = 0;
11282 [ + - ]: 140 : if (eh_frame_ptr_enc != DW_EH_PE_omit)
11283 : : {
11284 : 140 : readp = read_encoded (eh_frame_ptr_enc, readp, dataend, &eh_frame_ptr,
11285 : : dbg);
11286 [ - + ]: 140 : if (unlikely (readp == NULL))
11287 : 0 : goto invalid_data;
11288 : :
11289 : 140 : printf (" eh_frame_ptr: %#" PRIx64, eh_frame_ptr);
11290 [ + - ]: 140 : if ((eh_frame_ptr_enc & 0x70) == DW_EH_PE_pcrel)
11291 : 280 : printf (" (offset: %#" PRIx64 ")",
11292 : : /* +4 because of the 4 byte header of the section. */
11293 : 140 : (uint64_t) shdr->sh_offset + 4 + eh_frame_ptr);
11294 : :
11295 : 140 : putchar ('\n');
11296 : : }
11297 : :
11298 : 140 : uint64_t fde_count = 0;
11299 [ + - ]: 140 : if (fde_count_enc != DW_EH_PE_omit)
11300 : : {
11301 : 140 : readp = read_encoded (fde_count_enc, readp, dataend, &fde_count, dbg);
11302 [ - + ]: 140 : if (unlikely (readp == NULL))
11303 : 0 : goto invalid_data;
11304 : :
11305 : 140 : printf (" fde_count: %" PRIu64 "\n", fde_count);
11306 : : }
11307 : :
11308 [ + - + - ]: 140 : if (fde_count == 0 || table_enc == DW_EH_PE_omit)
11309 : : return;
11310 : :
11311 : 140 : puts (" Table:");
11312 : :
11313 : : /* Optimize for the most common case. */
11314 [ + - ]: 140 : if (table_enc == (DW_EH_PE_datarel | DW_EH_PE_sdata4))
11315 : 21888 : while (fde_count > 0 && readp + 8 <= dataend)
11316 : : {
11317 [ - + ]: 21748 : int32_t initial_location = read_4sbyte_unaligned_inc (dbg, readp);
11318 : 21748 : uint64_t initial_offset = ((uint64_t) shdr->sh_offset
11319 : 21748 : + (int64_t) initial_location);
11320 [ - + ]: 21748 : int32_t address = read_4sbyte_unaligned_inc (dbg, readp);
11321 : : // XXX Possibly print symbol name or section offset for initial_offset
11322 [ + + ]: 21888 : printf (" %#" PRIx32 " (offset: %#6" PRIx64 ") -> %#" PRIx32
11323 : : " fde=[%6" PRIx64 "]\n",
11324 : : initial_location, initial_offset,
11325 : 21748 : address, address - (eh_frame_ptr + 4));
11326 : : }
11327 : : else
11328 : 140 : while (0 && readp < dataend)
11329 : : {
11330 : :
11331 : 140 : }
11332 : : }
11333 : :
11334 : :
11335 : : /* Print the content of the exception handling table section
11336 : : '.eh_frame_hdr'. */
11337 : : static void
11338 : 0 : print_debug_exception_table (Dwfl_Module *dwflmod __attribute__ ((unused)),
11339 : : Ebl *ebl __attribute__ ((unused)),
11340 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
11341 : : Elf_Scn *scn,
11342 : : GElf_Shdr *shdr __attribute__ ((unused)),
11343 : : Dwarf *dbg __attribute__ ((unused)))
11344 : : {
11345 : 0 : printf (_("\
11346 : : \nException handling table section [%2zu] '.gcc_except_table':\n"),
11347 : : elf_ndxscn (scn));
11348 : :
11349 : 0 : Elf_Data *data = elf_rawdata (scn, NULL);
11350 : :
11351 [ # # ]: 0 : if (unlikely (data == NULL))
11352 : : {
11353 : 0 : error (0, 0, _("cannot get %s content: %s"),
11354 : : ".gcc_except_table", elf_errmsg (-1));
11355 : 0 : return;
11356 : : }
11357 : :
11358 : 0 : const unsigned char *readp = data->d_buf;
11359 : 0 : const unsigned char *const dataend = readp + data->d_size;
11360 : :
11361 [ # # ]: 0 : if (unlikely (readp + 1 > dataend))
11362 : : {
11363 : 0 : invalid_data:
11364 : 0 : error (0, 0, _("invalid data"));
11365 : 0 : return;
11366 : : }
11367 : 0 : unsigned int lpstart_encoding = *readp++;
11368 : 0 : printf (_(" LPStart encoding: %#x "), lpstart_encoding);
11369 : 0 : print_encoding_base ("", lpstart_encoding);
11370 [ # # ]: 0 : if (lpstart_encoding != DW_EH_PE_omit)
11371 : : {
11372 : 0 : uint64_t lpstart;
11373 : 0 : readp = read_encoded (lpstart_encoding, readp, dataend, &lpstart, dbg);
11374 : 0 : printf (" LPStart: %#" PRIx64 "\n", lpstart);
11375 : : }
11376 : :
11377 [ # # ]: 0 : if (unlikely (readp + 1 > dataend))
11378 : 0 : goto invalid_data;
11379 : 0 : unsigned int ttype_encoding = *readp++;
11380 : 0 : printf (_(" TType encoding: %#x "), ttype_encoding);
11381 : 0 : print_encoding_base ("", ttype_encoding);
11382 : 0 : const unsigned char *ttype_base = NULL;
11383 [ # # ]: 0 : if (ttype_encoding != DW_EH_PE_omit)
11384 : : {
11385 : 0 : unsigned int ttype_base_offset;
11386 [ # # ]: 0 : if (readp >= dataend)
11387 : 0 : goto invalid_data;
11388 : 0 : get_uleb128 (ttype_base_offset, readp, dataend);
11389 : 0 : printf (" TType base offset: %#x\n", ttype_base_offset);
11390 [ # # ]: 0 : if ((size_t) (dataend - readp) > ttype_base_offset)
11391 : 0 : ttype_base = readp + ttype_base_offset;
11392 : : }
11393 : :
11394 [ # # ]: 0 : if (unlikely (readp + 1 > dataend))
11395 : 0 : goto invalid_data;
11396 : 0 : unsigned int call_site_encoding = *readp++;
11397 : 0 : printf (_(" Call site encoding: %#x "), call_site_encoding);
11398 : 0 : print_encoding_base ("", call_site_encoding);
11399 : 0 : unsigned int call_site_table_len;
11400 [ # # ]: 0 : if (readp >= dataend)
11401 : 0 : goto invalid_data;
11402 : 0 : get_uleb128 (call_site_table_len, readp, dataend);
11403 : :
11404 : 0 : const unsigned char *const action_table = readp + call_site_table_len;
11405 [ # # ]: 0 : if (unlikely (action_table > dataend))
11406 : 0 : goto invalid_data;
11407 : : unsigned int u = 0;
11408 : : unsigned int max_action = 0;
11409 [ # # ]: 0 : while (readp < action_table)
11410 : : {
11411 [ # # ]: 0 : if (u == 0)
11412 : 0 : puts (_("\n Call site table:"));
11413 : :
11414 : 0 : uint64_t call_site_start;
11415 : 0 : readp = read_encoded (call_site_encoding, readp, dataend,
11416 : : &call_site_start, dbg);
11417 : 0 : uint64_t call_site_length;
11418 : 0 : readp = read_encoded (call_site_encoding, readp, dataend,
11419 : : &call_site_length, dbg);
11420 : 0 : uint64_t landing_pad;
11421 : 0 : readp = read_encoded (call_site_encoding, readp, dataend,
11422 : : &landing_pad, dbg);
11423 : 0 : unsigned int action;
11424 [ # # ]: 0 : if (readp >= dataend)
11425 : 0 : goto invalid_data;
11426 : 0 : get_uleb128 (action, readp, dataend);
11427 : 0 : max_action = MAX (action, max_action);
11428 : 0 : printf (_(" [%4u] Call site start: %#" PRIx64 "\n"
11429 : : " Call site length: %" PRIu64 "\n"
11430 : : " Landing pad: %#" PRIx64 "\n"
11431 : : " Action: %u\n"),
11432 : : u++, call_site_start, call_site_length, landing_pad, action);
11433 : : }
11434 [ # # ]: 0 : if (readp != action_table)
11435 : 0 : goto invalid_data;
11436 : :
11437 : 0 : unsigned int max_ar_filter = 0;
11438 [ # # ]: 0 : if (max_action > 0)
11439 : : {
11440 : 0 : puts ("\n Action table:");
11441 : :
11442 : 0 : size_t maxdata = (size_t) (dataend - action_table);
11443 [ # # # # ]: 0 : if (max_action > maxdata || maxdata - max_action < 1)
11444 : : {
11445 : 0 : invalid_action_table:
11446 : 0 : fputs (_(" <INVALID DATA>\n"), stdout);
11447 : 0 : return;
11448 : : }
11449 : :
11450 : 0 : const unsigned char *const action_table_end
11451 : 0 : = action_table + max_action + 1;
11452 : :
11453 : 0 : u = 0;
11454 : 0 : do
11455 : : {
11456 : 0 : int ar_filter;
11457 : 0 : get_sleb128 (ar_filter, readp, action_table_end);
11458 [ # # ]: 0 : if (ar_filter > 0 && (unsigned int) ar_filter > max_ar_filter)
11459 : 0 : max_ar_filter = ar_filter;
11460 : 0 : int ar_disp;
11461 [ # # ]: 0 : if (readp >= action_table_end)
11462 : 0 : goto invalid_action_table;
11463 : 0 : get_sleb128 (ar_disp, readp, action_table_end);
11464 : :
11465 : 0 : printf (" [%4u] ar_filter: % d\n"
11466 : : " ar_disp: % -5d",
11467 : : u, ar_filter, ar_disp);
11468 [ # # ]: 0 : if (abs (ar_disp) & 1)
11469 : 0 : printf (" -> [%4u]\n", u + (ar_disp + 1) / 2);
11470 [ # # ]: 0 : else if (ar_disp != 0)
11471 : 0 : puts (" -> ???");
11472 : : else
11473 : 0 : putchar ('\n');
11474 : 0 : ++u;
11475 : : }
11476 [ # # ]: 0 : while (readp < action_table_end);
11477 : : }
11478 : :
11479 [ # # ]: 0 : if (max_ar_filter > 0 && ttype_base != NULL)
11480 : : {
11481 : 0 : unsigned char dsize;
11482 : 0 : puts ("\n TType table:");
11483 : :
11484 : : // XXX Not *4, size of encoding;
11485 [ # # ]: 0 : switch (ttype_encoding & 7)
11486 : : {
11487 : : case DW_EH_PE_udata2:
11488 : : case DW_EH_PE_sdata2:
11489 : : dsize = 2;
11490 : : break;
11491 : : case DW_EH_PE_udata4:
11492 : : case DW_EH_PE_sdata4:
11493 : : dsize = 4;
11494 : : break;
11495 : : case DW_EH_PE_udata8:
11496 : : case DW_EH_PE_sdata8:
11497 : : dsize = 8;
11498 : : break;
11499 : 0 : default:
11500 : 0 : dsize = 0;
11501 : 0 : error (1, 0, _("invalid TType encoding"));
11502 : : }
11503 : :
11504 : 0 : if (max_ar_filter
11505 [ # # ]: 0 : > (size_t) (ttype_base - (const unsigned char *) data->d_buf) / dsize)
11506 : 0 : goto invalid_data;
11507 : :
11508 : 0 : readp = ttype_base - max_ar_filter * dsize;
11509 : 0 : do
11510 : : {
11511 : 0 : uint64_t ttype;
11512 : 0 : readp = read_encoded (ttype_encoding, readp, ttype_base, &ttype,
11513 : : dbg);
11514 : 0 : printf (" [%4u] %#" PRIx64 "\n", max_ar_filter--, ttype);
11515 : : }
11516 [ # # ]: 0 : while (readp < ttype_base);
11517 : : }
11518 : : }
11519 : :
11520 : : /* Print the content of the '.gdb_index' section.
11521 : : http://sourceware.org/gdb/current/onlinedocs/gdb/Index-Section-Format.html
11522 : : */
11523 : : static void
11524 : 8 : print_gdb_index_section (Dwfl_Module *dwflmod, Ebl *ebl,
11525 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
11526 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
11527 : : {
11528 : 8 : printf (_("\nGDB section [%2zu] '%s' at offset %#" PRIx64
11529 : : " contains %" PRId64 " bytes :\n"),
11530 : : elf_ndxscn (scn), section_name (ebl, shdr),
11531 : 8 : (uint64_t) shdr->sh_offset, (uint64_t) shdr->sh_size);
11532 : :
11533 : 8 : Elf_Data *data = elf_rawdata (scn, NULL);
11534 : :
11535 [ - + ]: 8 : if (unlikely (data == NULL))
11536 : : {
11537 : 0 : error (0, 0, _("cannot get %s content: %s"),
11538 : : ".gdb_index", elf_errmsg (-1));
11539 : 4 : return;
11540 : : }
11541 : :
11542 : : // .gdb_index is always in little endian.
11543 : 8 : Dwarf dummy_dbg = { .other_byte_order = MY_ELFDATA != ELFDATA2LSB };
11544 : 8 : dbg = &dummy_dbg;
11545 : :
11546 : 8 : const unsigned char *readp = data->d_buf;
11547 : 8 : const unsigned char *const dataend = readp + data->d_size;
11548 : :
11549 [ - + ]: 8 : if (unlikely (readp + 4 > dataend))
11550 : : {
11551 : 0 : invalid_data:
11552 : 0 : error (0, 0, _("invalid data"));
11553 : 0 : return;
11554 : : }
11555 : :
11556 : 8 : int32_t vers = read_4ubyte_unaligned (dbg, readp);
11557 : 8 : printf (_(" Version: %" PRId32 "\n"), vers);
11558 : :
11559 : : // The only difference between version 4 and version 5 is the
11560 : : // hash used for generating the table. Version 6 contains symbols
11561 : : // for inlined functions, older versions didn't. Version 7 adds
11562 : : // symbol kinds. Version 8 just indicates that it correctly includes
11563 : : // TUs for symbols. Version 9 adds shortcut table for information
11564 : : // regarding the main function.
11565 [ - + ]: 8 : if (vers < 4 || vers > 9)
11566 : : {
11567 : 0 : printf (_(" unknown version, cannot parse section\n"));
11568 : 0 : return;
11569 : : }
11570 : :
11571 : 8 : readp += 4;
11572 [ - + ]: 8 : if (unlikely (readp + 4 > dataend))
11573 : 0 : goto invalid_data;
11574 : :
11575 : 8 : uint32_t cu_off = read_4ubyte_unaligned (dbg, readp);
11576 : 8 : printf (_(" CU offset: %#" PRIx32 "\n"), cu_off);
11577 : :
11578 : 8 : readp += 4;
11579 [ - + ]: 8 : if (unlikely (readp + 4 > dataend))
11580 : 0 : goto invalid_data;
11581 : :
11582 : 8 : uint32_t tu_off = read_4ubyte_unaligned (dbg, readp);
11583 : 8 : printf (_(" TU offset: %#" PRIx32 "\n"), tu_off);
11584 : :
11585 : 8 : readp += 4;
11586 [ - + ]: 8 : if (unlikely (readp + 4 > dataend))
11587 : 0 : goto invalid_data;
11588 : :
11589 : 8 : uint32_t addr_off = read_4ubyte_unaligned (dbg, readp);
11590 : 8 : printf (_(" address offset: %#" PRIx32 "\n"), addr_off);
11591 : :
11592 : 8 : readp += 4;
11593 [ - + ]: 8 : if (unlikely (readp + 4 > dataend))
11594 : 0 : goto invalid_data;
11595 : :
11596 : 8 : uint32_t sym_off = read_4ubyte_unaligned (dbg, readp);
11597 : 8 : printf (_(" symbol offset: %#" PRIx32 "\n"), sym_off);
11598 : :
11599 : 8 : readp += 4;
11600 [ - + ]: 8 : if (unlikely (readp + 4 > dataend))
11601 : 0 : goto invalid_data;
11602 : :
11603 : 8 : uint32_t shortcut_off = 0;
11604 [ + + ]: 8 : if (vers >= 9)
11605 : : {
11606 : 4 : shortcut_off = read_4ubyte_unaligned (dbg, readp);
11607 : 4 : printf (_(" shortcut offset: %#" PRIx32 "\n"), shortcut_off);
11608 : :
11609 : 4 : readp += 4;
11610 [ - + ]: 4 : if (unlikely (readp + 4 > dataend))
11611 : 0 : goto invalid_data;
11612 : : }
11613 : :
11614 : 8 : uint32_t const_off = read_4ubyte_unaligned (dbg, readp);
11615 : 8 : printf (_(" constant offset: %#" PRIx32 "\n"), const_off);
11616 : :
11617 [ - + ]: 8 : if (unlikely ((size_t) (dataend - (const unsigned char *) data->d_buf)
11618 : : < const_off))
11619 : 0 : goto invalid_data;
11620 : :
11621 : 8 : readp = data->d_buf + cu_off;
11622 : :
11623 : 8 : const unsigned char *nextp = data->d_buf + tu_off;
11624 [ - + ]: 8 : if (tu_off >= data->d_size)
11625 : 0 : goto invalid_data;
11626 : :
11627 : 8 : size_t cu_nr = (nextp - readp) / 16;
11628 : :
11629 : 8 : printf (_("\n CU list at offset %#" PRIx32
11630 : : " contains %zu entries:\n"),
11631 : : cu_off, cu_nr);
11632 : :
11633 : 8 : size_t n = 0;
11634 [ + - + + ]: 22 : while (dataend - readp >= 16 && n < cu_nr)
11635 : : {
11636 : 14 : uint64_t off = read_8ubyte_unaligned (dbg, readp);
11637 : 14 : readp += 8;
11638 : :
11639 : 14 : uint64_t len = read_8ubyte_unaligned (dbg, readp);
11640 : 14 : readp += 8;
11641 : :
11642 : 14 : printf (" [%4zu] start: %0#8" PRIx64
11643 : : ", length: %5" PRIu64 "\n", n, off, len);
11644 : 14 : n++;
11645 : : }
11646 : :
11647 : 8 : readp = data->d_buf + tu_off;
11648 : 8 : nextp = data->d_buf + addr_off;
11649 [ - + ]: 8 : if (addr_off >= data->d_size)
11650 : 0 : goto invalid_data;
11651 : :
11652 : 8 : size_t tu_nr = (nextp - readp) / 24;
11653 : :
11654 : 8 : printf (_("\n TU list at offset %#" PRIx32
11655 : : " contains %zu entries:\n"),
11656 : : tu_off, tu_nr);
11657 : :
11658 : 8 : n = 0;
11659 [ + - + + ]: 14 : while (dataend - readp >= 24 && n < tu_nr)
11660 : : {
11661 : 6 : uint64_t off = read_8ubyte_unaligned (dbg, readp);
11662 : 6 : readp += 8;
11663 : :
11664 : 6 : uint64_t type = read_8ubyte_unaligned (dbg, readp);
11665 : 6 : readp += 8;
11666 : :
11667 : 6 : uint64_t sig = read_8ubyte_unaligned (dbg, readp);
11668 : 6 : readp += 8;
11669 : :
11670 : 6 : printf (" [%4zu] CU offset: %5" PRId64
11671 : : ", type offset: %5" PRId64
11672 : : ", signature: %0#8" PRIx64 "\n", n, off, type, sig);
11673 : 6 : n++;
11674 : : }
11675 : :
11676 : 8 : readp = data->d_buf + addr_off;
11677 : 8 : nextp = data->d_buf + sym_off;
11678 [ - + ]: 8 : if (sym_off >= data->d_size)
11679 : 0 : goto invalid_data;
11680 : :
11681 : 8 : size_t addr_nr = (nextp - readp) / 20;
11682 : :
11683 : 8 : printf (_("\n Address list at offset %#" PRIx32
11684 : : " contains %zu entries:\n"),
11685 : : addr_off, addr_nr);
11686 : :
11687 : 8 : n = 0;
11688 [ + - + + ]: 22 : while (dataend - readp >= 20 && n < addr_nr)
11689 : : {
11690 : 14 : uint64_t low = read_8ubyte_unaligned (dbg, readp);
11691 : 14 : readp += 8;
11692 : :
11693 : 14 : uint64_t high = read_8ubyte_unaligned (dbg, readp);
11694 : 14 : readp += 8;
11695 : :
11696 : 14 : uint32_t idx = read_4ubyte_unaligned (dbg, readp);
11697 : 14 : readp += 4;
11698 : :
11699 : 14 : printf (" [%4zu] ", n);
11700 : 14 : print_dwarf_addr (dwflmod, 8, low, low);
11701 : 14 : printf ("..");
11702 : 14 : print_dwarf_addr (dwflmod, 8, high - 1, high);
11703 : 14 : printf (", CU index: %5" PRId32 "\n", idx);
11704 : 14 : n++;
11705 : : }
11706 : :
11707 : 8 : const unsigned char *const_start = data->d_buf + const_off;
11708 [ - + ]: 8 : if (const_off > data->d_size)
11709 : 0 : goto invalid_data;
11710 : :
11711 : 8 : const unsigned char *shortcut_start = NULL;
11712 [ + + ]: 8 : if (vers >= 9)
11713 : : {
11714 [ - + ]: 4 : if (shortcut_off >= data->d_size)
11715 : 0 : goto invalid_data;
11716 : :
11717 : 4 : shortcut_start = data->d_buf + shortcut_off;
11718 : 4 : nextp = shortcut_start;
11719 : : }
11720 : : else
11721 : : nextp = const_start;
11722 : :
11723 : 8 : readp = data->d_buf + sym_off;
11724 : 8 : size_t sym_nr = (nextp - readp) / 8;
11725 : :
11726 : 8 : printf (_("\n Symbol table at offset %#" PRIx32
11727 : : " contains %zu slots:\n"),
11728 : : sym_off, sym_nr);
11729 : :
11730 : 8 : n = 0;
11731 [ + - + + ]: 8200 : while (dataend - readp >= 8 && n < sym_nr)
11732 : : {
11733 : 8192 : uint32_t name = read_4ubyte_unaligned (dbg, readp);
11734 : 8192 : readp += 4;
11735 : :
11736 : 8192 : uint32_t vector = read_4ubyte_unaligned (dbg, readp);
11737 : 8192 : readp += 4;
11738 : :
11739 [ + + ]: 8192 : if (name != 0 || vector != 0)
11740 : : {
11741 : 60 : const unsigned char *sym = const_start + name;
11742 [ + - - + ]: 60 : if (unlikely ((size_t) (dataend - const_start) < name
11743 : : || memchr (sym, '\0', dataend - sym) == NULL))
11744 : 0 : goto invalid_data;
11745 : :
11746 : 60 : printf (" [%4zu] symbol: %s, CUs: ", n, sym);
11747 : :
11748 : 60 : const unsigned char *readcus = const_start + vector;
11749 [ - + ]: 60 : if (unlikely ((size_t) (dataend - const_start) < vector))
11750 : 0 : goto invalid_data;
11751 : 60 : uint32_t cus = read_4ubyte_unaligned (dbg, readcus);
11752 : 60 : while (cus--)
11753 : : {
11754 : 68 : uint32_t cu_kind, cu, kind;
11755 : 68 : bool is_static;
11756 : 68 : readcus += 4;
11757 [ - + ]: 68 : if (unlikely (readcus + 4 > dataend))
11758 : 0 : goto invalid_data;
11759 : 68 : cu_kind = read_4ubyte_unaligned (dbg, readcus);
11760 : 68 : cu = cu_kind & ((1 << 24) - 1);
11761 : 68 : kind = (cu_kind >> 28) & 7;
11762 : 68 : is_static = cu_kind & (1U << 31);
11763 [ + + ]: 68 : if (cu > cu_nr - 1)
11764 : 10 : printf ("%" PRId32 "T", cu - (uint32_t) cu_nr);
11765 : : else
11766 : 58 : printf ("%" PRId32, cu);
11767 [ + + ]: 68 : if (kind != 0)
11768 : : {
11769 : 52 : printf (" (");
11770 [ + + + - : 52 : switch (kind)
- ]
11771 : : {
11772 : : case 1:
11773 : 20 : printf ("type");
11774 : : break;
11775 : : case 2:
11776 : 16 : printf ("var");
11777 : : break;
11778 : : case 3:
11779 : 16 : printf ("func");
11780 : : break;
11781 : : case 4:
11782 : 0 : printf ("other");
11783 : : break;
11784 : : default:
11785 : 0 : printf ("unknown-0x%" PRIx32, kind);
11786 : : break;
11787 : : }
11788 [ + + ]: 52 : printf (":%c)", (is_static ? 'S' : 'G'));
11789 : : }
11790 [ + + ]: 68 : if (cus > 0)
11791 [ + + ]: 136 : printf (", ");
11792 : : }
11793 : 60 : printf ("\n");
11794 : : }
11795 : 8192 : n++;
11796 : : }
11797 : :
11798 [ + + ]: 8 : if (vers < 9)
11799 : : return;
11800 : :
11801 [ - + ]: 4 : if (unlikely (shortcut_start == NULL))
11802 : 0 : goto invalid_data;
11803 : :
11804 : 4 : readp = shortcut_start;
11805 : 4 : nextp = const_start;
11806 : 4 : size_t shortcut_nr = (nextp - readp) / 4;
11807 : :
11808 [ - + ]: 4 : if (unlikely (shortcut_nr != 2))
11809 : 0 : goto invalid_data;
11810 : :
11811 : 4 : printf (_("\nShortcut table at offset %#" PRIx32 " contains %zu slots:\n"),
11812 : : shortcut_off, shortcut_nr);
11813 : :
11814 : 4 : uint32_t lang = read_4ubyte_unaligned (dbg, readp);
11815 : 4 : readp += 4;
11816 : :
11817 : : /* Include the hex number of LANG in the output if the language
11818 : : is unknown. */
11819 : 4 : const char *lang_str = dwarf_lang_string (lang);
11820 : 4 : lang_str = string_or_unknown (lang_str, lang, DW_LANG_lo_user,
11821 : : DW_LANG_hi_user, true);
11822 : :
11823 : 4 : printf (_("Language of main: %s\n"), lang_str);
11824 : 4 : printf (_("Name of main: "));
11825 : :
11826 [ + + ]: 4 : if (lang != 0)
11827 : : {
11828 : 2 : uint32_t name = read_4ubyte_unaligned (dbg, readp);
11829 : 2 : readp += 4;
11830 : 2 : const unsigned char *sym = const_start + name;
11831 : :
11832 [ + - - + ]: 2 : if (unlikely ((size_t) (dataend - const_start) < name
11833 : : || memchr (sym, '\0', dataend - sym) == NULL))
11834 : 0 : goto invalid_data;
11835 : :
11836 : 2 : printf ("%s\n", sym);
11837 : : }
11838 : : else
11839 : 4 : printf ("<unknown>\n");
11840 : : }
11841 : :
11842 : : /* Returns true and sets split DWARF CU id if there is a split compile
11843 : : unit in the given Dwarf, and no non-split units are found (before it). */
11844 : : static bool
11845 : 394 : is_split_dwarf (Dwarf *dbg, uint64_t *id, Dwarf_CU **split_cu)
11846 : : {
11847 : 394 : Dwarf_CU *cu = NULL;
11848 [ + + ]: 394 : while (dwarf_get_units (dbg, cu, &cu, NULL, NULL, NULL, NULL) == 0)
11849 : : {
11850 : 392 : uint8_t unit_type;
11851 [ + - ]: 392 : if (dwarf_cu_info (cu, NULL, &unit_type, NULL, NULL,
11852 : : id, NULL, NULL) != 0)
11853 : 392 : return false;
11854 : :
11855 [ + + ]: 392 : if (unit_type != DW_UT_split_compile && unit_type != DW_UT_split_type)
11856 : : return false;
11857 : :
11858 : : /* We really only care about the split compile unit, the types
11859 : : should be fine and self sufficient. Also they don't have an
11860 : : id that we can match with a skeleton unit. */
11861 [ + - ]: 18 : if (unit_type == DW_UT_split_compile)
11862 : : {
11863 : 18 : *split_cu = cu;
11864 : 18 : return true;
11865 : : }
11866 : : }
11867 : :
11868 : : return false;
11869 : : }
11870 : :
11871 : : /* Check that there is one and only one Dwfl_Module, return in arg. */
11872 : : static int
11873 : 18 : getone_dwflmod (Dwfl_Module *dwflmod,
11874 : : void **userdata __attribute__ ((unused)),
11875 : : const char *name __attribute__ ((unused)),
11876 : : Dwarf_Addr base __attribute__ ((unused)),
11877 : : void *arg)
11878 : : {
11879 : 18 : Dwfl_Module **m = (Dwfl_Module **) arg;
11880 [ + - ]: 18 : if (*m != NULL)
11881 : : return DWARF_CB_ABORT;
11882 : 18 : *m = dwflmod;
11883 : 18 : return DWARF_CB_OK;
11884 : : }
11885 : :
11886 : : static void
11887 : 500 : print_debug (Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr)
11888 : : {
11889 : : /* Used for skeleton file, if necessary for split DWARF. */
11890 : 500 : Dwfl *skel_dwfl = NULL;
11891 : 500 : Dwfl_Module *skel_mod = NULL;
11892 : 500 : char *skel_name = NULL;
11893 : 500 : Dwarf *split_dbg = NULL;
11894 : 500 : Dwarf_CU *split_cu = NULL;
11895 : :
11896 : : /* Before we start the real work get a debug context descriptor. */
11897 : 500 : Dwarf_Addr dwbias;
11898 : 500 : Dwarf *dbg = dwfl_module_getdwarf (dwflmod, &dwbias);
11899 : 500 : Dwarf dummy_dbg =
11900 : : {
11901 : 500 : .elf = ebl->elf,
11902 : 500 : .other_byte_order = MY_ELFDATA != ehdr->e_ident[EI_DATA]
11903 : : };
11904 [ + + ]: 500 : if (dbg == NULL)
11905 : : {
11906 [ - + ]: 106 : if ((print_debug_sections & ~(section_exception|section_frame)) != 0)
11907 : 0 : error (0, 0, _("cannot get debug context descriptor: %s"),
11908 : : dwfl_errmsg (-1));
11909 : : dbg = &dummy_dbg;
11910 : : }
11911 : : else
11912 : : {
11913 : : /* If we are asked about a split dwarf (.dwo) file, use the user
11914 : : provided, or find the corresponding skeleton file. If we got
11915 : : a skeleton file, replace the given dwflmod and dbg, with one
11916 : : derived from the skeleton file to provide enough context. */
11917 : 394 : uint64_t split_id;
11918 [ + + ]: 394 : if (is_split_dwarf (dbg, &split_id, &split_cu))
11919 : : {
11920 [ + - ]: 18 : if (dwarf_skeleton != NULL)
11921 : 18 : skel_name = strdup (dwarf_skeleton);
11922 : : else
11923 : : {
11924 : : /* Replace file.dwo with file.o and see if that matches. */
11925 : 0 : const char *fname;
11926 : 0 : dwfl_module_info (dwflmod, NULL, NULL, NULL, NULL, NULL,
11927 : : &fname, NULL);
11928 [ # # ]: 0 : if (fname != NULL)
11929 : : {
11930 : 0 : size_t flen = strlen (fname);
11931 [ # # # # ]: 0 : if (flen > 4 && strcmp (".dwo", fname + flen - 4) == 0)
11932 : : {
11933 : 0 : skel_name = strdup (fname);
11934 [ # # ]: 0 : if (skel_name != NULL)
11935 : : {
11936 : 0 : skel_name[flen - 3] = 'o';
11937 : 0 : skel_name[flen - 2] = '\0';
11938 : : }
11939 : : }
11940 : : }
11941 : : }
11942 : :
11943 [ - + ]: 18 : if (skel_name != NULL)
11944 : : {
11945 : 18 : int skel_fd = open (skel_name, O_RDONLY);
11946 [ - + ]: 18 : if (skel_fd == -1)
11947 : 0 : fprintf (stderr, "Warning: Couldn't open DWARF skeleton file"
11948 : : " '%s'\n", skel_name);
11949 : : else
11950 : : {
11951 : 18 : skel_dwfl = create_dwfl (skel_fd, skel_name);
11952 : :
11953 : : /* skel_fd was dup'ed by create_dwfl. We can close the
11954 : : original now. */
11955 : 18 : close (skel_fd);
11956 : : }
11957 : :
11958 [ + - ]: 18 : if (skel_dwfl != NULL)
11959 : : {
11960 [ - + ]: 18 : if (dwfl_getmodules (skel_dwfl, &getone_dwflmod,
11961 : : &skel_mod, 0) != 0)
11962 : : {
11963 : 0 : fprintf (stderr, "Warning: Bad DWARF skeleton,"
11964 : : " multiple modules '%s'\n", skel_name);
11965 : 0 : dwfl_end (skel_dwfl);
11966 : 0 : skel_mod = NULL;
11967 : : }
11968 : : }
11969 [ # # ]: 0 : else if (skel_fd != -1)
11970 : 0 : fprintf (stderr, "Warning: Couldn't create skeleton dwfl for"
11971 : : " '%s': %s\n", skel_name, dwfl_errmsg (-1));
11972 : :
11973 [ + - ]: 18 : if (skel_mod != NULL)
11974 : : {
11975 : 18 : Dwarf *skel_dbg = dwfl_module_getdwarf (skel_mod, &dwbias);
11976 [ + - ]: 18 : if (skel_dbg != NULL)
11977 : : {
11978 : : /* First check the skeleton CU DIE, only fetch
11979 : : the split DIE if we know the id matches to
11980 : : not unnecessary search for any split DIEs we
11981 : : don't need. */
11982 : 18 : Dwarf_CU *cu = NULL;
11983 : 18 : while (dwarf_get_units (skel_dbg, cu, &cu,
11984 [ + - ]: 26 : NULL, NULL, NULL, NULL) == 0)
11985 : : {
11986 : 26 : uint8_t unit_type;
11987 : 26 : uint64_t skel_id;
11988 [ + - ]: 26 : if (dwarf_cu_info (cu, NULL, &unit_type, NULL, NULL,
11989 : : &skel_id, NULL, NULL) == 0
11990 [ + - ]: 26 : && unit_type == DW_UT_skeleton
11991 [ + + ]: 26 : && split_id == skel_id)
11992 : : {
11993 : 18 : Dwarf_Die subdie;
11994 [ + - ]: 18 : if (dwarf_cu_info (cu, NULL, NULL, NULL,
11995 : : &subdie,
11996 : : NULL, NULL, NULL) == 0
11997 [ + - ]: 18 : && dwarf_tag (&subdie) != DW_TAG_invalid)
11998 : : {
11999 : 18 : split_dbg = dwarf_cu_getdwarf (subdie.cu);
12000 [ - + ]: 18 : if (split_dbg == NULL)
12001 : 0 : fprintf (stderr,
12002 : : "Warning: Couldn't get split_dbg:"
12003 : : " %s\n", dwarf_errmsg (-1));
12004 : 0 : break;
12005 : : }
12006 : : else
12007 : : {
12008 : : /* Everything matches up, but not
12009 : : according to libdw. Which means
12010 : : the user knew better. So...
12011 : : Terrible hack... We can never
12012 : : destroy the underlying dwfl
12013 : : because it would free the wrong
12014 : : Dwarfs... So we leak memory...*/
12015 [ # # ]: 0 : if (cu->split == NULL
12016 [ # # ]: 0 : && dwarf_skeleton != NULL)
12017 : : {
12018 : 0 : do_not_close_dwfl = true;
12019 : 0 : __libdw_link_skel_split (cu, split_cu);
12020 : 0 : split_dbg = dwarf_cu_getdwarf (split_cu);
12021 : 0 : break;
12022 : : }
12023 : : else
12024 : 0 : fprintf (stderr, "Warning: Couldn't get"
12025 : : " skeleton subdie: %s\n",
12026 : : dwarf_errmsg (-1));
12027 : : }
12028 : : }
12029 : : }
12030 [ - - ]: 18 : if (split_dbg == NULL)
12031 : 0 : fprintf (stderr, "Warning: '%s' didn't contain a skeleton for split id %" PRIx64 "\n", skel_name, split_id);
12032 : : }
12033 : : else
12034 : 0 : fprintf (stderr, "Warning: Couldn't get skeleton DWARF:"
12035 : : " %s\n", dwfl_errmsg (-1));
12036 : : }
12037 : : }
12038 : :
12039 : 0 : if (split_dbg != NULL)
12040 : : {
12041 : 18 : dbg = split_dbg;
12042 : 18 : dwflmod = skel_mod;
12043 : : }
12044 [ # # ]: 0 : else if (skel_name == NULL)
12045 : 0 : fprintf (stderr,
12046 : : "Warning: split DWARF file, but no skeleton found.\n");
12047 : : }
12048 [ + - ]: 376 : else if (dwarf_skeleton != NULL)
12049 : 394 : fprintf (stderr, "Warning: DWARF skeleton given,"
12050 : : " but not a split DWARF file\n");
12051 : : }
12052 : :
12053 : : /* Get the section header string table index. */
12054 : 500 : size_t shstrndx;
12055 [ - + ]: 500 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
12056 : 0 : error_exit (0, _("cannot get section header string table index"));
12057 : :
12058 : : /* If the .debug_info section is listed as implicitly required then
12059 : : we must make sure to handle it before handling any other debug
12060 : : section. Various other sections depend on the CU DIEs being
12061 : : scanned (silently) first. */
12062 : 500 : bool implicit_info = (implicit_debug_sections & section_info) != 0;
12063 : 500 : bool explicit_info = (print_debug_sections & section_info) != 0;
12064 [ + + ]: 500 : if (implicit_info)
12065 : : {
12066 : : Elf_Scn *scn = NULL;
12067 [ + - ]: 3132 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
12068 : : {
12069 : 3132 : GElf_Shdr shdr_mem;
12070 : 3132 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
12071 : :
12072 [ + - + + ]: 3132 : if (shdr != NULL && shdr->sh_type == SHT_PROGBITS)
12073 : : {
12074 : 2448 : const char *name = elf_strptr (ebl->elf, shstrndx,
12075 : 1224 : shdr->sh_name);
12076 [ - + ]: 1224 : if (name == NULL)
12077 : 0 : continue;
12078 : :
12079 [ + + ]: 1224 : if (strcmp (name, ".debug_info") == 0
12080 [ + + ]: 1092 : || strcmp (name, ".debug_info.dwo") == 0
12081 [ + + ]: 1074 : || strcmp (name, ".zdebug_info") == 0
12082 [ + - ]: 1062 : || strcmp (name, ".zdebug_info.dwo") == 0
12083 [ + + ]: 1062 : || strcmp (name, ".gnu.debuglto_.debug_info") == 0)
12084 : : {
12085 : 164 : print_debug_info_section (dwflmod, ebl, ehdr,
12086 : : scn, shdr, dbg);
12087 : 164 : break;
12088 : : }
12089 : : }
12090 : : }
12091 : 164 : print_debug_sections &= ~section_info;
12092 : 164 : implicit_debug_sections &= ~section_info;
12093 : : }
12094 : :
12095 : : /* Look through all the sections for the debugging sections to print. */
12096 : : Elf_Scn *scn = NULL;
12097 [ + + ]: 16518 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
12098 : : {
12099 : 16018 : GElf_Shdr shdr_mem;
12100 : 16018 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
12101 : :
12102 [ + - + + ]: 16018 : if (shdr != NULL && shdr->sh_type == SHT_PROGBITS)
12103 : : {
12104 : 6724 : static const struct
12105 : : {
12106 : : const char *name;
12107 : : enum section_e bitmask;
12108 : : void (*fp) (Dwfl_Module *, Ebl *,
12109 : : GElf_Ehdr *, Elf_Scn *, GElf_Shdr *, Dwarf *);
12110 : : } debug_sections[] =
12111 : : {
12112 : : #define NEW_SECTION(name) \
12113 : : { ".debug_" #name, section_##name, print_debug_##name##_section }
12114 : : NEW_SECTION (abbrev),
12115 : : NEW_SECTION (addr),
12116 : : NEW_SECTION (aranges),
12117 : : NEW_SECTION (frame),
12118 : : NEW_SECTION (info),
12119 : : NEW_SECTION (types),
12120 : : NEW_SECTION (line),
12121 : : NEW_SECTION (loc),
12122 : : /* loclists is loc for DWARF5. */
12123 : : { ".debug_loclists", section_loc,
12124 : : print_debug_loclists_section },
12125 : : NEW_SECTION (pubnames),
12126 : : NEW_SECTION (str),
12127 : : /* A DWARF5 specialised debug string section. */
12128 : : { ".debug_line_str", section_str,
12129 : : print_debug_str_section },
12130 : : /* DWARF5 string offsets table. */
12131 : : { ".debug_str_offsets", section_str,
12132 : : print_debug_str_offsets_section },
12133 : : NEW_SECTION (macinfo),
12134 : : NEW_SECTION (macro),
12135 : : NEW_SECTION (ranges),
12136 : : /* rnglists is ranges for DWARF5. */
12137 : : { ".debug_rnglists", section_ranges,
12138 : : print_debug_rnglists_section },
12139 : : { ".eh_frame", section_frame | section_exception,
12140 : : print_debug_frame_section },
12141 : : { ".eh_frame_hdr", section_frame | section_exception,
12142 : : print_debug_frame_hdr_section },
12143 : : { ".gcc_except_table", section_frame | section_exception,
12144 : : print_debug_exception_table },
12145 : : { ".gdb_index", section_gdb_index, print_gdb_index_section }
12146 : : };
12147 : 6724 : const int ndebug_sections = (sizeof (debug_sections)
12148 : : / sizeof (debug_sections[0]));
12149 : 13448 : const char *name = elf_strptr (ebl->elf, shstrndx,
12150 : 6724 : shdr->sh_name);
12151 [ - + ]: 6724 : if (name == NULL)
12152 : 0 : continue;
12153 : :
12154 : : int n;
12155 [ + + ]: 105958 : for (n = 0; n < ndebug_sections; ++n)
12156 : : {
12157 : 102512 : size_t dbglen = strlen (debug_sections[n].name);
12158 : 102512 : size_t scnlen = strlen (name);
12159 [ + + ]: 102512 : if ((strncmp (name, debug_sections[n].name, dbglen) == 0
12160 [ + + ]: 3538 : && (dbglen == scnlen
12161 [ + + ]: 542 : || (scnlen == dbglen + 4
12162 [ + + ]: 462 : && strstr (name, ".dwo") == name + dbglen)))
12163 [ + + + + ]: 99408 : || (name[0] == '.' && name[1] == 'z'
12164 [ + - ]: 1132 : && debug_sections[n].name[1] == 'd'
12165 [ + + ]: 1132 : && strncmp (&name[2], &debug_sections[n].name[1],
12166 : : dbglen - 1) == 0
12167 [ - + ]: 164 : && (scnlen == dbglen + 1
12168 [ # # ]: 0 : || (scnlen == dbglen + 5
12169 [ # # ]: 0 : && strstr (name, ".dwo") == name + dbglen + 1)))
12170 [ + + ]: 99244 : || (scnlen > 14 /* .gnu.debuglto_ prefix. */
12171 [ + + ]: 10148 : && startswith (name, ".gnu.debuglto_")
12172 [ + + ]: 72 : && strcmp (&name[14], debug_sections[n].name) == 0)
12173 : : )
12174 : : {
12175 : 3278 : if ((print_debug_sections | implicit_debug_sections)
12176 [ + + ]: 3278 : & debug_sections[n].bitmask)
12177 : 1092 : debug_sections[n].fp (dwflmod, ebl, ehdr, scn, shdr, dbg);
12178 : : break;
12179 : : }
12180 : : }
12181 : : }
12182 : : }
12183 : :
12184 : 500 : dwfl_end (skel_dwfl);
12185 : 500 : free (skel_name);
12186 : :
12187 : : /* Turn implicit and/or explicit back on in case we go over another file. */
12188 [ + + ]: 500 : if (implicit_info)
12189 : 164 : implicit_debug_sections |= section_info;
12190 [ + + ]: 500 : if (explicit_info)
12191 : 132 : print_debug_sections |= section_info;
12192 : :
12193 : 500 : reset_listptr (&known_locsptr);
12194 : 500 : reset_listptr (&known_loclistsptr);
12195 : 500 : reset_listptr (&known_rangelistptr);
12196 : 500 : reset_listptr (&known_rnglistptr);
12197 : 500 : reset_listptr (&known_addrbases);
12198 : 500 : reset_listptr (&known_stroffbases);
12199 : 500 : }
12200 : :
12201 : :
12202 : : #define ITEM_INDENT 4
12203 : : #define WRAP_COLUMN 75
12204 : :
12205 : : /* Print "NAME: FORMAT", wrapping when output text would make the line
12206 : : exceed WRAP_COLUMN. Unpadded numbers look better for the core items
12207 : : but this function is also used for registers which should be printed
12208 : : aligned. Fortunately registers output uses fixed fields width (such
12209 : : as %11d) for the alignment.
12210 : :
12211 : : Line breaks should not depend on the particular values although that
12212 : : may happen in some cases of the core items. */
12213 : :
12214 : : static unsigned int
12215 : : __attribute__ ((format (printf, 6, 7)))
12216 : 1642 : print_core_item (unsigned int colno, char sep, unsigned int wrap,
12217 : : size_t name_width, const char *name, const char *format, ...)
12218 : : {
12219 : 1642 : size_t len = strlen (name);
12220 : 1642 : if (name_width < len)
12221 : : name_width = len;
12222 : :
12223 : 1642 : char *out;
12224 : 1642 : va_list ap;
12225 : 1642 : va_start (ap, format);
12226 : 1642 : int out_len = vasprintf (&out, format, ap);
12227 : 1642 : va_end (ap);
12228 [ - + ]: 1642 : if (out_len == -1)
12229 : 0 : error_exit (0, _("memory exhausted"));
12230 : :
12231 : 1642 : size_t n = name_width + sizeof ": " - 1 + out_len;
12232 : :
12233 [ + + ]: 1642 : if (colno == 0)
12234 : : {
12235 : 102 : printf ("%*s", ITEM_INDENT, "");
12236 : 102 : colno = ITEM_INDENT + n;
12237 : : }
12238 [ + + ]: 1540 : else if (colno + 2 + n < wrap)
12239 : : {
12240 : 936 : printf ("%c ", sep);
12241 : 936 : colno += 2 + n;
12242 : : }
12243 : : else
12244 : : {
12245 : 604 : printf ("\n%*s", ITEM_INDENT, "");
12246 : 604 : colno = ITEM_INDENT + n;
12247 : : }
12248 : :
12249 : 1642 : printf ("%s: %*s%s", name, (int) (name_width - len), "", out);
12250 : :
12251 : 1642 : free (out);
12252 : :
12253 : 1642 : return colno;
12254 : : }
12255 : :
12256 : : static const void *
12257 : 1862 : convert (Elf *core, Elf_Type type, uint_fast16_t count,
12258 : : void *value, const void *data, size_t size)
12259 : : {
12260 : 3724 : Elf_Data valuedata =
12261 : : {
12262 : : .d_type = type,
12263 : : .d_buf = value,
12264 [ + + ]: 1862 : .d_size = size ?: gelf_fsize (core, type, count, EV_CURRENT),
12265 : : .d_version = EV_CURRENT,
12266 : : };
12267 : 1862 : Elf_Data indata =
12268 : : {
12269 : : .d_type = type,
12270 : : .d_buf = (void *) data,
12271 : : .d_size = valuedata.d_size,
12272 : : .d_version = EV_CURRENT,
12273 : : };
12274 : :
12275 : 1862 : Elf_Data *d = (gelf_getclass (core) == ELFCLASS32
12276 [ + + ]: 1862 : ? elf32_xlatetom : elf64_xlatetom)
12277 : 1862 : (&valuedata, &indata, elf_getident (core, NULL)[EI_DATA]);
12278 [ - + ]: 1862 : if (d == NULL)
12279 : 0 : error_exit (0, _("cannot convert core note data: %s"),
12280 : : elf_errmsg (-1));
12281 : :
12282 : 1862 : return data + indata.d_size;
12283 : : }
12284 : :
12285 : : typedef uint8_t GElf_Byte;
12286 : :
12287 : : static unsigned int
12288 : 800 : handle_core_item (Elf *core, const Ebl_Core_Item *item, const void *desc,
12289 : : unsigned int colno, size_t *repeated_size)
12290 : : {
12291 [ + + ]: 800 : uint_fast16_t count = item->count ?: 1;
12292 : : /* Ebl_Core_Item count is always a small number.
12293 : : Make sure the backend didn't put in some large bogus value. */
12294 [ - + ]: 124 : assert (count < 128);
12295 : :
12296 : : #define TYPES \
12297 : : DO_TYPE (BYTE, Byte, "0x%.2" PRIx8, "%" PRId8); \
12298 : : DO_TYPE (HALF, Half, "0x%.4" PRIx16, "%" PRId16); \
12299 : : DO_TYPE (WORD, Word, "0x%.8" PRIx32, "%" PRId32); \
12300 : : DO_TYPE (SWORD, Sword, "%" PRId32, "%" PRId32); \
12301 : : DO_TYPE (XWORD, Xword, "0x%.16" PRIx64, "%" PRId64); \
12302 : : DO_TYPE (SXWORD, Sxword, "%" PRId64, "%" PRId64)
12303 : :
12304 : : #define DO_TYPE(NAME, Name, hex, dec) GElf_##Name Name
12305 : 800 : typedef union { TYPES; } value_t;
12306 : 800 : void *data = alloca (count * sizeof (value_t));
12307 : : #undef DO_TYPE
12308 : :
12309 : : #define DO_TYPE(NAME, Name, hex, dec) \
12310 : : GElf_##Name *value_##Name __attribute__((unused)) = data
12311 : 800 : TYPES;
12312 : : #undef DO_TYPE
12313 : :
12314 : 800 : size_t size = gelf_fsize (core, item->type, count, EV_CURRENT);
12315 : 800 : size_t convsize = size;
12316 [ + + ]: 800 : if (repeated_size != NULL)
12317 : : {
12318 [ + - - + ]: 2 : if (*repeated_size > size && (item->format == 'b' || item->format == 'B'))
12319 : : {
12320 : 0 : data = alloca (*repeated_size);
12321 : 0 : count *= *repeated_size / size;
12322 : 0 : convsize = count * size;
12323 : 0 : *repeated_size -= convsize;
12324 : : }
12325 [ + - + - ]: 2 : else if (item->count != 0 || item->format != '\n')
12326 : 0 : *repeated_size -= size;
12327 : : }
12328 : :
12329 : 800 : convert (core, item->type, count, data, desc + item->offset, convsize);
12330 : :
12331 : 800 : Elf_Type type = item->type;
12332 [ + + ]: 800 : if (type == ELF_T_ADDR)
12333 [ + - ]: 2 : type = gelf_getclass (core) == ELFCLASS32 ? ELF_T_WORD : ELF_T_XWORD;
12334 : :
12335 [ + + + + : 800 : switch (item->format)
+ + + +
- ]
12336 : : {
12337 : 386 : case 'd':
12338 [ - + ]: 386 : assert (count == 1);
12339 [ + + + + : 386 : switch (type)
- + - ]
12340 : : {
12341 : : #define DO_TYPE(NAME, Name, hex, dec) \
12342 : : case ELF_T_##NAME: \
12343 : : colno = print_core_item (colno, ',', WRAP_COLUMN, \
12344 : : 0, item->name, dec, value_##Name[0]); \
12345 : : break
12346 : 386 : TYPES;
12347 : : #undef DO_TYPE
12348 : 0 : default:
12349 : 0 : abort ();
12350 : : }
12351 : : break;
12352 : :
12353 : 222 : case 'x':
12354 [ - + ]: 222 : assert (count == 1);
12355 [ - - + - : 222 : switch (type)
+ - - ]
12356 : : {
12357 : : #define DO_TYPE(NAME, Name, hex, dec) \
12358 : : case ELF_T_##NAME: \
12359 : : colno = print_core_item (colno, ',', WRAP_COLUMN, \
12360 : : 0, item->name, hex, value_##Name[0]); \
12361 : : break
12362 : 222 : TYPES;
12363 : : #undef DO_TYPE
12364 : 0 : default:
12365 : 0 : abort ();
12366 : : }
12367 : : break;
12368 : :
12369 : 44 : case 'b':
12370 : : case 'B':
12371 [ - + ]: 44 : assert (size % sizeof (unsigned int) == 0);
12372 : 44 : unsigned int nbits = count * size * 8;
12373 : 44 : unsigned int pop = 0;
12374 [ + + ]: 112 : for (const unsigned int *i = data; (void *) i < data + count * size; ++i)
12375 : 68 : pop += __builtin_popcount (*i);
12376 : 44 : bool negate = pop > nbits / 2;
12377 : 44 : const unsigned int bias = item->format == 'b';
12378 : :
12379 : 44 : {
12380 [ - + ]: 44 : char printed[(negate ? nbits - pop : pop) * 16 + 1];
12381 : 44 : char *p = printed;
12382 : 44 : *p = '\0';
12383 : :
12384 : 44 : if (BYTE_ORDER != LITTLE_ENDIAN && size > sizeof (unsigned int))
12385 : : {
12386 : : assert (size == sizeof (unsigned int) * 2);
12387 : : for (unsigned int *i = data;
12388 : : (void *) i < data + count * size; i += 2)
12389 : : {
12390 : : unsigned int w = i[1];
12391 : : i[1] = i[0];
12392 : : i[0] = w;
12393 : : }
12394 : : }
12395 : :
12396 : 44 : unsigned int lastbit = 0;
12397 : 44 : unsigned int run = 0;
12398 : 44 : for (const unsigned int *i = data;
12399 [ + + ]: 112 : (void *) i < data + count * size; ++i)
12400 : : {
12401 : 68 : unsigned int bit = ((void *) i - data) * 8;
12402 [ - + ]: 68 : unsigned int w = negate ? ~*i : *i;
12403 [ - + ]: 68 : while (w != 0)
12404 : : {
12405 : : /* Note that a right shift equal to (or greater than)
12406 : : the number of bits of w is undefined behaviour. In
12407 : : particular when the least significant bit is bit 32
12408 : : (w = 0x8000000) then w >>= n is undefined. So
12409 : : explicitly handle that case separately. */
12410 : 0 : unsigned int n = ffs (w);
12411 [ # # ]: 0 : if (n < sizeof (w) * 8)
12412 : 0 : w >>= n;
12413 : : else
12414 : : w = 0;
12415 : 0 : bit += n;
12416 : :
12417 [ # # # # ]: 0 : if (lastbit != 0 && lastbit + 1 == bit)
12418 : 0 : ++run;
12419 : : else
12420 : : {
12421 : 0 : if (lastbit == 0)
12422 : 0 : p += sprintf (p, "%u", bit - bias);
12423 [ # # ]: 0 : else if (run == 0)
12424 : 0 : p += sprintf (p, ",%u", bit - bias);
12425 : : else
12426 : 0 : p += sprintf (p, "-%u,%u", lastbit - bias, bit - bias);
12427 : : run = 0;
12428 : : }
12429 : :
12430 : : lastbit = bit;
12431 : : }
12432 : : }
12433 [ - + - - ]: 44 : if (lastbit > 0 && run > 0 && lastbit + 1 != nbits)
12434 : 0 : p += sprintf (p, "-%u", lastbit - bias);
12435 : :
12436 [ + - ]: 88 : colno = print_core_item (colno, ',', WRAP_COLUMN, 0, item->name,
12437 : : negate ? "~<%s>" : "<%s>", printed);
12438 : : }
12439 : 44 : break;
12440 : :
12441 : 88 : case 'T':
12442 : : case (char) ('T'|0x80):
12443 [ - + ]: 88 : assert (count == 2);
12444 : 88 : Dwarf_Word sec;
12445 : 88 : Dwarf_Word usec;
12446 [ - - + - : 88 : switch (type)
+ - - ]
12447 : : {
12448 : : #define DO_TYPE(NAME, Name, hex, dec) \
12449 : : case ELF_T_##NAME: \
12450 : : sec = value_##Name[0]; \
12451 : : usec = value_##Name[1]; \
12452 : : break
12453 : 88 : TYPES;
12454 : : #undef DO_TYPE
12455 : 0 : default:
12456 : 0 : abort ();
12457 : : }
12458 [ - + ]: 88 : if (unlikely (item->format == (char) ('T'|0x80)))
12459 : : {
12460 : : /* This is a hack for an ill-considered 64-bit ABI where
12461 : : tv_usec is actually a 32-bit field with 32 bits of padding
12462 : : rounding out struct timeval. We've already converted it as
12463 : : a 64-bit field. For little-endian, this just means the
12464 : : high half is the padding; it's presumably zero, but should
12465 : : be ignored anyway. For big-endian, it means the 32-bit
12466 : : field went into the high half of USEC. */
12467 : 0 : GElf_Ehdr ehdr_mem;
12468 : 0 : GElf_Ehdr *ehdr = gelf_getehdr (core, &ehdr_mem);
12469 [ # # ]: 0 : if (likely (ehdr->e_ident[EI_DATA] == ELFDATA2MSB))
12470 : 0 : usec >>= 32;
12471 : : else
12472 : 0 : usec &= UINT32_MAX;
12473 : : }
12474 : 88 : colno = print_core_item (colno, ',', WRAP_COLUMN, 0, item->name,
12475 : : "%" PRIu64 ".%.6" PRIu64, sec, usec);
12476 : 88 : break;
12477 : :
12478 : 18 : case 'c':
12479 [ - + ]: 18 : assert (count == 1);
12480 : 18 : colno = print_core_item (colno, ',', WRAP_COLUMN, 0, item->name,
12481 : 18 : "%c", value_Byte[0]);
12482 : 18 : break;
12483 : :
12484 : 36 : case 's':
12485 : 36 : colno = print_core_item (colno, ',', WRAP_COLUMN, 0, item->name,
12486 : : "%.*s", (int) count, value_Byte);
12487 : 36 : break;
12488 : :
12489 : 2 : case '\n':
12490 : : /* This is a list of strings separated by '\n'. */
12491 [ - + ]: 2 : assert (item->count == 0);
12492 [ - + ]: 2 : assert (repeated_size != NULL);
12493 [ - + ]: 2 : assert (item->name == NULL);
12494 [ - + ]: 2 : if (unlikely (item->offset >= *repeated_size))
12495 : : break;
12496 : :
12497 : 2 : const char *s = desc + item->offset;
12498 : 2 : size = *repeated_size - item->offset;
12499 : 2 : *repeated_size = 0;
12500 [ + - ]: 96 : while (size > 0)
12501 : : {
12502 : 96 : const char *eol = memchr (s, '\n', size);
12503 : 96 : int len = size;
12504 [ + + ]: 96 : if (eol != NULL)
12505 : 94 : len = eol - s;
12506 : 96 : printf ("%*s%.*s\n", ITEM_INDENT, "", len, s);
12507 [ + + ]: 96 : if (eol == NULL)
12508 : : break;
12509 : 94 : size -= eol + 1 - s;
12510 : 94 : s = eol + 1;
12511 : : }
12512 : :
12513 : : colno = WRAP_COLUMN;
12514 : : break;
12515 : :
12516 : : case 'h':
12517 : : break;
12518 : :
12519 : 0 : default:
12520 : 800 : error (0, 0, "XXX not handling format '%c' for %s",
12521 : 0 : item->format, item->name);
12522 : : break;
12523 : : }
12524 : :
12525 : : #undef TYPES
12526 : :
12527 : 800 : return colno;
12528 : : }
12529 : :
12530 : :
12531 : : /* Sort items by group, and by layout offset within each group. */
12532 : : static int
12533 : 1790 : compare_core_items (const void *a, const void *b)
12534 : : {
12535 : 1790 : const Ebl_Core_Item *const *p1 = a;
12536 : 1790 : const Ebl_Core_Item *const *p2 = b;
12537 : 1790 : const Ebl_Core_Item *item1 = *p1;
12538 : 1790 : const Ebl_Core_Item *item2 = *p2;
12539 : :
12540 : 1790 : return ((item1->group == item2->group ? 0
12541 [ + + ]: 1790 : : strcmp (item1->group, item2->group))
12542 [ - + ]: 1790 : ?: (int) item1->offset - (int) item2->offset);
12543 : : }
12544 : :
12545 : : /* Sort item groups by layout offset of the first item in the group. */
12546 : : static int
12547 : 278 : compare_core_item_groups (const void *a, const void *b)
12548 : : {
12549 : 278 : const Ebl_Core_Item *const *const *p1 = a;
12550 : 278 : const Ebl_Core_Item *const *const *p2 = b;
12551 : 278 : const Ebl_Core_Item *const *group1 = *p1;
12552 : 278 : const Ebl_Core_Item *const *group2 = *p2;
12553 : 278 : const Ebl_Core_Item *item1 = *group1;
12554 : 278 : const Ebl_Core_Item *item2 = *group2;
12555 : :
12556 : 278 : return (int) item1->offset - (int) item2->offset;
12557 : : }
12558 : :
12559 : : static unsigned int
12560 : 74 : handle_core_items (Elf *core, const void *desc, size_t descsz,
12561 : : const Ebl_Core_Item *items, size_t nitems)
12562 : 74 : {
12563 [ + + ]: 74 : if (nitems == 0)
12564 : : return 0;
12565 : 68 : unsigned int colno = 0;
12566 : :
12567 : : /* FORMAT '\n' makes sense to be present only as a single item as it
12568 : : processes all the data of a note. FORMATs 'b' and 'B' have a special case
12569 : : if present as a single item but they can be also processed with other
12570 : : items below. */
12571 [ + + + + ]: 68 : if (nitems == 1 && (items[0].format == '\n' || items[0].format == 'b'
12572 [ + - ]: 16 : || items[0].format == 'B'))
12573 : : {
12574 [ - + ]: 2 : assert (items[0].offset == 0);
12575 : 2 : size_t size = descsz;
12576 : 2 : colno = handle_core_item (core, items, desc, colno, &size);
12577 : : /* If SIZE is not zero here there is some remaining data. But we do not
12578 : : know how to process it anyway. */
12579 : 2 : return colno;
12580 : : }
12581 [ + + ]: 848 : for (size_t i = 0; i < nitems; ++i)
12582 [ - + ]: 782 : assert (items[i].format != '\n');
12583 : :
12584 : : /* Sort to collect the groups together. */
12585 : 66 : const Ebl_Core_Item *sorted_items[nitems];
12586 [ + + ]: 848 : for (size_t i = 0; i < nitems; ++i)
12587 : 782 : sorted_items[i] = &items[i];
12588 : 66 : qsort (sorted_items, nitems, sizeof sorted_items[0], &compare_core_items);
12589 : :
12590 : : /* Collect the unique groups and sort them. */
12591 : 66 : const Ebl_Core_Item **groups[nitems];
12592 : 66 : groups[0] = &sorted_items[0];
12593 : 66 : size_t ngroups = 1;
12594 [ + + ]: 782 : for (size_t i = 1; i < nitems; ++i)
12595 [ + + ]: 716 : if (sorted_items[i]->group != sorted_items[i - 1]->group
12596 [ + - ]: 150 : && strcmp (sorted_items[i]->group, sorted_items[i - 1]->group))
12597 : 150 : groups[ngroups++] = &sorted_items[i];
12598 : 66 : qsort (groups, ngroups, sizeof groups[0], &compare_core_item_groups);
12599 : :
12600 : : /* Write out all the groups. */
12601 : 66 : const void *last = desc;
12602 : 70 : do
12603 : : {
12604 [ + + ]: 290 : for (size_t i = 0; i < ngroups; ++i)
12605 : : {
12606 : 220 : for (const Ebl_Core_Item **item = groups[i];
12607 : 1018 : (item < &sorted_items[nitems]
12608 [ + + + + ]: 1018 : && ((*item)->group == groups[i][0]->group
12609 [ - + ]: 150 : || !strcmp ((*item)->group, groups[i][0]->group)));
12610 : 798 : ++item)
12611 : 798 : colno = handle_core_item (core, *item, desc, colno, NULL);
12612 : :
12613 : : /* Force a line break at the end of the group. */
12614 : 220 : colno = WRAP_COLUMN;
12615 : : }
12616 : :
12617 [ + + ]: 70 : if (descsz == 0)
12618 : : break;
12619 : :
12620 : : /* This set of items consumed a certain amount of the note's data.
12621 : : If there is more data there, we have another unit of the same size.
12622 : : Loop to print that out too. */
12623 : 40 : const Ebl_Core_Item *item = &items[nitems - 1];
12624 : 80 : size_t eltsz = item->offset + gelf_fsize (core, item->type,
12625 : 40 : item->count ?: 1, EV_CURRENT);
12626 : :
12627 : 40 : int reps = -1;
12628 : 40 : do
12629 : : {
12630 : 40 : ++reps;
12631 : 40 : desc += eltsz;
12632 : 40 : descsz -= eltsz;
12633 : : }
12634 [ + + - + ]: 40 : while (descsz >= eltsz && !memcmp (desc, last, eltsz));
12635 : :
12636 [ + - ]: 40 : if (reps == 1)
12637 : : {
12638 : : /* For just one repeat, print it unabridged twice. */
12639 : 40 : desc -= eltsz;
12640 : 40 : descsz += eltsz;
12641 : : }
12642 [ + - ]: 40 : else if (reps > 1)
12643 : 0 : printf (_("\n%*s... <repeats %u more times> ..."),
12644 : : ITEM_INDENT, "", reps);
12645 : :
12646 : 40 : last = desc;
12647 : : }
12648 [ + + ]: 40 : while (descsz > 0);
12649 : :
12650 : : return colno;
12651 : : }
12652 : :
12653 : : static unsigned int
12654 : 324 : handle_core_register (Ebl *ebl, Elf *core, int maxregname,
12655 : : const Ebl_Register_Location *regloc, const void *desc,
12656 : : unsigned int colno)
12657 : : {
12658 [ - + ]: 324 : if (regloc->bits % 8 != 0)
12659 : : {
12660 : 0 : error (0, 0, "Warning: Cannot handle register with %" PRIu8 "bits\n",
12661 : : regloc->bits);
12662 : 0 : return colno;
12663 : : }
12664 : :
12665 : 324 : desc += regloc->offset;
12666 : :
12667 [ + + ]: 1172 : for (int reg = regloc->regno; reg < regloc->regno + regloc->count; ++reg)
12668 : : {
12669 : 848 : char name[REGNAMESZ];
12670 : 848 : int bits;
12671 : 848 : int type;
12672 : 848 : register_info (ebl, reg, regloc, name, &bits, &type);
12673 : :
12674 : : #define TYPES \
12675 : : BITS (8, BYTE, "%4" PRId8, "0x%.2" PRIx8); \
12676 : : BITS (16, HALF, "%6" PRId16, "0x%.4" PRIx16); \
12677 : : BITS (32, WORD, "%11" PRId32, " 0x%.8" PRIx32); \
12678 : : BITS (64, XWORD, "%20" PRId64, " 0x%.16" PRIx64)
12679 : :
12680 : : #define BITS(bits, xtype, sfmt, ufmt) \
12681 : : uint##bits##_t b##bits; int##bits##_t b##bits##s
12682 : 848 : union { TYPES; uint64_t b128[2]; } value;
12683 : : #undef BITS
12684 : :
12685 [ + + ]: 848 : switch (type)
12686 : : {
12687 : 672 : case DW_ATE_unsigned:
12688 : : case DW_ATE_signed:
12689 : : case DW_ATE_address:
12690 [ - + + + : 672 : switch (bits)
+ - ]
12691 : : {
12692 : : #define BITS(bits, xtype, sfmt, ufmt) \
12693 : : case bits: \
12694 : : desc = convert (core, ELF_T_##xtype, 1, &value, desc, 0); \
12695 : : if (type == DW_ATE_signed) \
12696 : : colno = print_core_item (colno, ' ', WRAP_COLUMN, \
12697 : : maxregname, name, \
12698 : : sfmt, value.b##bits##s); \
12699 : : else \
12700 : : colno = print_core_item (colno, ' ', WRAP_COLUMN, \
12701 : : maxregname, name, \
12702 : : ufmt, value.b##bits); \
12703 : : break
12704 : :
12705 [ - - - + : 576 : TYPES;
+ + + + ]
12706 : :
12707 : 96 : case 128:
12708 [ - + ]: 96 : assert (type == DW_ATE_unsigned);
12709 : 96 : desc = convert (core, ELF_T_XWORD, 2, &value, desc, 0);
12710 : 96 : int be = elf_getident (core, NULL)[EI_DATA] == ELFDATA2MSB;
12711 : 96 : colno = print_core_item (colno, ' ', WRAP_COLUMN,
12712 : : maxregname, name,
12713 : : "0x%.16" PRIx64 "%.16" PRIx64,
12714 : 96 : value.b128[!be], value.b128[be]);
12715 : 96 : break;
12716 : :
12717 : 0 : default:
12718 : 0 : abort ();
12719 : : #undef BITS
12720 : : }
12721 : : break;
12722 : :
12723 : 176 : default:
12724 : : /* Print each byte in hex, the whole thing in native byte order. */
12725 [ - + ]: 176 : assert (bits % 8 == 0);
12726 : 176 : const uint8_t *bytes = desc;
12727 : 176 : desc += bits / 8;
12728 : 176 : char hex[bits / 4 + 1];
12729 : 176 : hex[bits / 4] = '\0';
12730 : 176 : int incr = 1;
12731 [ + + ]: 176 : if (elf_getident (core, NULL)[EI_DATA] == ELFDATA2LSB)
12732 : : {
12733 : 96 : bytes += bits / 8 - 1;
12734 : 96 : incr = -1;
12735 : : }
12736 : 176 : size_t idx = 0;
12737 [ + + ]: 1744 : for (char *h = hex; bits > 0; bits -= 8, idx += incr)
12738 : : {
12739 : 1568 : *h++ = "0123456789abcdef"[bytes[idx] >> 4];
12740 : 1568 : *h++ = "0123456789abcdef"[bytes[idx] & 0xf];
12741 : : }
12742 : 176 : colno = print_core_item (colno, ' ', WRAP_COLUMN,
12743 : : maxregname, name, "0x%s", hex);
12744 : 176 : break;
12745 : : }
12746 : 848 : desc += regloc->pad;
12747 : :
12748 : : #undef TYPES
12749 : : }
12750 : :
12751 : : return colno;
12752 : : }
12753 : :
12754 : :
12755 : : struct register_info
12756 : : {
12757 : : const Ebl_Register_Location *regloc;
12758 : : const char *set;
12759 : : char name[REGNAMESZ];
12760 : : int regno;
12761 : : int bits;
12762 : : int type;
12763 : : };
12764 : :
12765 : : static int
12766 : 4132 : register_bitpos (const struct register_info *r)
12767 : : {
12768 : 4132 : return (r->regloc->offset * 8
12769 : 4132 : + ((r->regno - r->regloc->regno)
12770 : 4132 : * (r->regloc->bits + r->regloc->pad * 8)));
12771 : : }
12772 : :
12773 : : static int
12774 : 2124 : compare_sets_by_info (const struct register_info *r1,
12775 : : const struct register_info *r2)
12776 : : {
12777 : 2124 : return ((int) r2->bits - (int) r1->bits
12778 [ + + ]: 2124 : ?: register_bitpos (r1) - register_bitpos (r2));
12779 : : }
12780 : :
12781 : : /* Sort registers by set, and by size and layout offset within each set. */
12782 : : static int
12783 : 9190 : compare_registers (const void *a, const void *b)
12784 : : {
12785 : 9190 : const struct register_info *r1 = a;
12786 : 9190 : const struct register_info *r2 = b;
12787 : :
12788 : : /* Unused elements sort last. */
12789 [ + + ]: 9190 : if (r1->regloc == NULL)
12790 : 6618 : return r2->regloc == NULL ? 0 : 1;
12791 [ + + ]: 2572 : if (r2->regloc == NULL)
12792 : : return -1;
12793 : :
12794 [ + + ]: 2242 : return ((r1->set == r2->set ? 0 : strcmp (r1->set, r2->set))
12795 [ - + ]: 2242 : ?: compare_sets_by_info (r1, r2));
12796 : : }
12797 : :
12798 : : /* Sort register sets by layout offset of the first register in the set. */
12799 : : static int
12800 : 40 : compare_register_sets (const void *a, const void *b)
12801 : : {
12802 : 40 : const struct register_info *const *p1 = a;
12803 : 40 : const struct register_info *const *p2 = b;
12804 : 40 : return compare_sets_by_info (*p1, *p2);
12805 : : }
12806 : :
12807 : : static inline bool
12808 : 1728 : same_set (const struct register_info *a,
12809 : : const struct register_info *b,
12810 : : const struct register_info *regs,
12811 : : size_t maxnreg)
12812 : : {
12813 [ + - ]: 1728 : return (a < ®s[maxnreg] && a->regloc != NULL
12814 [ + - + + ]: 1728 : && b < ®s[maxnreg] && b->regloc != NULL
12815 [ + + ]: 1692 : && a->bits == b->bits
12816 [ + - + + : 3384 : && (a->set == b->set || !strcmp (a->set, b->set)));
- + ]
12817 : : }
12818 : :
12819 : : static unsigned int
12820 : 74 : handle_core_registers (Ebl *ebl, Elf *core, const void *desc,
12821 : : const Ebl_Register_Location *reglocs, size_t nregloc)
12822 : 74 : {
12823 [ + + ]: 74 : if (nregloc == 0)
12824 : : return 0;
12825 : :
12826 : 36 : ssize_t maxnreg = ebl_register_info (ebl, 0, NULL, 0, NULL, NULL, NULL, NULL);
12827 [ - + ]: 36 : if (maxnreg <= 0)
12828 : : {
12829 [ # # ]: 0 : for (size_t i = 0; i < nregloc; ++i)
12830 : 0 : if (maxnreg < reglocs[i].regno + reglocs[i].count)
12831 : : maxnreg = reglocs[i].regno + reglocs[i].count;
12832 [ # # ]: 0 : assert (maxnreg > 0);
12833 : : }
12834 : :
12835 : 36 : struct register_info regs[maxnreg];
12836 : 36 : memset (regs, 0, sizeof regs);
12837 : :
12838 : : /* Sort to collect the sets together. */
12839 : 36 : int maxreg = 0;
12840 [ + + ]: 360 : for (size_t i = 0; i < nregloc; ++i)
12841 : 324 : for (int reg = reglocs[i].regno;
12842 [ + + ]: 1172 : reg < reglocs[i].regno + reglocs[i].count;
12843 : 848 : ++reg)
12844 : : {
12845 [ - + ]: 848 : assert (reg < maxnreg);
12846 : 848 : if (reg > maxreg)
12847 : : maxreg = reg;
12848 : 848 : struct register_info *info = ®s[reg];
12849 : 848 : info->regloc = ®locs[i];
12850 : 848 : info->regno = reg;
12851 : 848 : info->set = register_info (ebl, reg, ®locs[i],
12852 : 848 : info->name, &info->bits, &info->type);
12853 : : }
12854 : 36 : qsort (regs, maxreg + 1, sizeof regs[0], &compare_registers);
12855 : :
12856 : : /* Collect the unique sets and sort them. */
12857 : 36 : struct register_info *sets[maxreg + 1];
12858 : 36 : sets[0] = ®s[0];
12859 : 36 : size_t nsets = 1;
12860 [ + + ]: 2558 : for (int i = 1; i <= maxreg; ++i)
12861 [ + + ]: 2522 : if (regs[i].regloc != NULL
12862 [ + + ]: 812 : && !same_set (®s[i], ®s[i - 1], regs, maxnreg))
12863 : 32 : sets[nsets++] = ®s[i];
12864 : 36 : qsort (sets, nsets, sizeof sets[0], &compare_register_sets);
12865 : :
12866 : : /* Write out all the sets. */
12867 : 36 : unsigned int colno = 0;
12868 [ + + ]: 104 : for (size_t i = 0; i < nsets; ++i)
12869 : : {
12870 : : /* Find the longest name of a register in this set. */
12871 : 68 : size_t maxname = 0;
12872 : 68 : const struct register_info *end;
12873 [ + + ]: 916 : for (end = sets[i]; same_set (sets[i], end, regs, maxnreg); ++end)
12874 : : {
12875 : 848 : size_t len = strlen (end->name);
12876 : 848 : if (len > maxname)
12877 : : maxname = len;
12878 : : }
12879 : :
12880 : : for (const struct register_info *reg = sets[i];
12881 [ + + ]: 392 : reg < end;
12882 : 324 : reg += reg->regloc->count ?: 1)
12883 [ + - ]: 324 : colno = handle_core_register (ebl, core, maxname,
12884 : 324 : reg->regloc, desc, colno);
12885 : :
12886 : : /* Force a line break at the end of the group. */
12887 : 68 : colno = WRAP_COLUMN;
12888 : : }
12889 : :
12890 : : return colno;
12891 : : }
12892 : :
12893 : : static void
12894 : 18 : handle_auxv_note (Ebl *ebl, Elf *core, GElf_Word descsz, GElf_Off desc_pos)
12895 : : {
12896 : 18 : Elf_Data *data = elf_getdata_rawchunk (core, desc_pos, descsz, ELF_T_AUXV);
12897 [ - + ]: 18 : if (data == NULL)
12898 : 0 : elf_error:
12899 : 0 : error_exit (0, _("cannot convert core note data: %s"), elf_errmsg (-1));
12900 : :
12901 : 18 : const size_t nauxv = descsz / gelf_fsize (core, ELF_T_AUXV, 1, EV_CURRENT);
12902 [ + + ]: 354 : for (size_t i = 0; i < nauxv; ++i)
12903 : : {
12904 : 336 : GElf_auxv_t av_mem;
12905 : 336 : GElf_auxv_t *av = gelf_getauxv (data, i, &av_mem);
12906 [ - + ]: 336 : if (av == NULL)
12907 : 0 : goto elf_error;
12908 : :
12909 : 336 : const char *name;
12910 : 336 : const char *fmt;
12911 [ - + ]: 336 : if (ebl_auxv_info (ebl, av->a_type, &name, &fmt) == 0)
12912 : : {
12913 : : /* Unknown type. */
12914 [ # # ]: 0 : if (av->a_un.a_val == 0)
12915 : 0 : printf (" %" PRIu64 "\n", av->a_type);
12916 : : else
12917 : 0 : printf (" %" PRIu64 ": %#" PRIx64 "\n",
12918 : : av->a_type, av->a_un.a_val);
12919 : : }
12920 : : else
12921 [ + + + - : 336 : switch (fmt[0])
+ - ]
12922 : : {
12923 : 18 : case '\0': /* Normally zero. */
12924 [ + - ]: 18 : if (av->a_un.a_val == 0)
12925 : : {
12926 : 18 : printf (" %s\n", name);
12927 : : break;
12928 : : }
12929 : 148 : FALLTHROUGH;
12930 : : case 'x': /* hex */
12931 : : case 'p': /* address */
12932 : : case 's': /* address of string */
12933 : 148 : printf (" %s: %#" PRIx64 "\n", name, av->a_un.a_val);
12934 : : break;
12935 : 162 : case 'u':
12936 : 162 : printf (" %s: %" PRIu64 "\n", name, av->a_un.a_val);
12937 : : break;
12938 : 0 : case 'd':
12939 : 0 : printf (" %s: %" PRId64 "\n", name, av->a_un.a_val);
12940 : : break;
12941 : :
12942 : 8 : case 'b':
12943 : 8 : printf (" %s: %#" PRIx64 " ", name, av->a_un.a_val);
12944 : 8 : GElf_Xword bit = 1;
12945 : 8 : const char *pfx = "<";
12946 [ + + ]: 220 : for (const char *p = fmt + 1; *p != 0; p = strchr (p, '\0') + 1)
12947 : : {
12948 [ + + ]: 212 : if (av->a_un.a_val & bit)
12949 : : {
12950 : 154 : printf ("%s%s", pfx, p);
12951 : 154 : pfx = " ";
12952 : : }
12953 : 212 : bit <<= 1;
12954 : : }
12955 : 336 : printf (">\n");
12956 : : break;
12957 : :
12958 : 0 : default:
12959 : 0 : abort ();
12960 : : }
12961 : : }
12962 : 18 : }
12963 : :
12964 : : static bool
12965 : 390 : buf_has_data (unsigned char const *ptr, unsigned char const *end, size_t sz)
12966 : : {
12967 [ - + - + ]: 390 : return ptr < end && (size_t) (end - ptr) >= sz;
12968 : : }
12969 : :
12970 : : static bool
12971 : 36 : buf_read_int (Elf *core, unsigned char const **ptrp, unsigned char const *end,
12972 : : int *retp)
12973 : : {
12974 [ + - + - ]: 72 : if (! buf_has_data (*ptrp, end, 4))
12975 : : return false;
12976 : :
12977 : 36 : *ptrp = convert (core, ELF_T_WORD, 1, retp, *ptrp, 4);
12978 : 36 : return true;
12979 : : }
12980 : :
12981 : : static bool
12982 : 354 : buf_read_ulong (Elf *core, unsigned char const **ptrp, unsigned char const *end,
12983 : : uint64_t *retp)
12984 : : {
12985 : 354 : size_t sz = gelf_fsize (core, ELF_T_ADDR, 1, EV_CURRENT);
12986 [ + - + - ]: 708 : if (! buf_has_data (*ptrp, end, sz))
12987 : : return false;
12988 : :
12989 : 354 : union
12990 : : {
12991 : : uint64_t u64;
12992 : : uint32_t u32;
12993 : : } u;
12994 : :
12995 : 354 : *ptrp = convert (core, ELF_T_ADDR, 1, &u, *ptrp, sz);
12996 : :
12997 [ + + ]: 354 : if (sz == 4)
12998 : 186 : *retp = u.u32;
12999 : : else
13000 : 168 : *retp = u.u64;
13001 : : return true;
13002 : : }
13003 : :
13004 : : static void
13005 : 12 : handle_siginfo_note (Elf *core, GElf_Word descsz, GElf_Off desc_pos)
13006 : : {
13007 : 12 : Elf_Data *data = elf_getdata_rawchunk (core, desc_pos, descsz, ELF_T_BYTE);
13008 [ - + ]: 12 : if (data == NULL)
13009 : 0 : error_exit (0, _("cannot convert core note data: %s"), elf_errmsg (-1));
13010 : :
13011 : 12 : unsigned char const *ptr = data->d_buf;
13012 : 12 : unsigned char const *const end = data->d_buf + data->d_size;
13013 : :
13014 : : /* Siginfo head is three ints: signal number, error number, origin
13015 : : code. */
13016 : 12 : int si_signo, si_errno, si_code;
13017 [ - + ]: 12 : if (! buf_read_int (core, &ptr, end, &si_signo)
13018 [ - + ]: 12 : || ! buf_read_int (core, &ptr, end, &si_errno)
13019 [ - + ]: 12 : || ! buf_read_int (core, &ptr, end, &si_code))
13020 : : {
13021 : 0 : fail:
13022 : 0 : printf (" Not enough data in NT_SIGINFO note.\n");
13023 : 0 : return;
13024 : : }
13025 : :
13026 : : /* Next is a pointer-aligned union of structures. On 64-bit
13027 : : machines, that implies a word of padding. */
13028 [ + + ]: 12 : if (gelf_getclass (core) == ELFCLASS64)
13029 : 6 : ptr += 4;
13030 : :
13031 : 12 : printf (" si_signo: %d, si_errno: %d, si_code: %d\n",
13032 : : si_signo, si_errno, si_code);
13033 : :
13034 [ + - ]: 12 : if (si_code > 0)
13035 [ + - ]: 12 : switch (si_signo)
13036 : : {
13037 : 12 : case CORE_SIGILL:
13038 : : case CORE_SIGFPE:
13039 : : case CORE_SIGSEGV:
13040 : : case CORE_SIGBUS:
13041 : : {
13042 : 12 : uint64_t addr;
13043 [ - + ]: 12 : if (! buf_read_ulong (core, &ptr, end, &addr))
13044 : 0 : goto fail;
13045 : 12 : printf (" fault address: %#" PRIx64 "\n", addr);
13046 : 12 : break;
13047 : : }
13048 : : default:
13049 : : ;
13050 : : }
13051 [ # # ]: 0 : else if (si_code == CORE_SI_USER)
13052 : : {
13053 : 0 : int pid, uid;
13054 [ # # ]: 0 : if (! buf_read_int (core, &ptr, end, &pid)
13055 [ # # ]: 0 : || ! buf_read_int (core, &ptr, end, &uid))
13056 : 0 : goto fail;
13057 : 0 : printf (" sender PID: %d, sender UID: %d\n", pid, uid);
13058 : : }
13059 : : }
13060 : :
13061 : : static void
13062 : 12 : handle_file_note (Elf *core, GElf_Word descsz, GElf_Off desc_pos)
13063 : : {
13064 : 12 : Elf_Data *data = elf_getdata_rawchunk (core, desc_pos, descsz, ELF_T_BYTE);
13065 [ - + ]: 12 : if (data == NULL)
13066 : 0 : error_exit (0, _("cannot convert core note data: %s"), elf_errmsg (-1));
13067 : :
13068 : 12 : unsigned char const *ptr = data->d_buf;
13069 : 12 : unsigned char const *const end = data->d_buf + data->d_size;
13070 : :
13071 : 12 : uint64_t count, page_size;
13072 [ - + ]: 12 : if (! buf_read_ulong (core, &ptr, end, &count)
13073 [ - + ]: 12 : || ! buf_read_ulong (core, &ptr, end, &page_size))
13074 : : {
13075 : 0 : fail:
13076 : 0 : printf (" Not enough data in NT_FILE note.\n");
13077 : 0 : return;
13078 : : }
13079 : :
13080 : 12 : size_t addrsize = gelf_fsize (core, ELF_T_ADDR, 1, EV_CURRENT);
13081 : 12 : uint64_t maxcount = (size_t) (end - ptr) / (3 * addrsize);
13082 [ - + ]: 12 : if (count > maxcount)
13083 : 0 : goto fail;
13084 : :
13085 : : /* Where file names are stored. */
13086 : 12 : unsigned char const *const fstart = ptr + 3 * count * addrsize;
13087 : 12 : char const *fptr = (char *) fstart;
13088 : :
13089 : 12 : printf (" %" PRId64 " files:\n", count);
13090 [ + + ]: 118 : for (uint64_t i = 0; i < count; ++i)
13091 : : {
13092 : 106 : uint64_t mstart, mend, moffset;
13093 [ + - ]: 106 : if (! buf_read_ulong (core, &ptr, fstart, &mstart)
13094 [ + - ]: 106 : || ! buf_read_ulong (core, &ptr, fstart, &mend)
13095 [ - + ]: 106 : || ! buf_read_ulong (core, &ptr, fstart, &moffset))
13096 : 0 : goto fail;
13097 : :
13098 : 106 : const char *fnext = memchr (fptr, '\0', (char *) end - fptr);
13099 [ - + ]: 106 : if (fnext == NULL)
13100 : 0 : goto fail;
13101 : :
13102 : 106 : int ct = printf (" %08" PRIx64 "-%08" PRIx64
13103 : : " %08" PRIx64 " %" PRId64,
13104 : : mstart, mend, moffset * page_size, mend - mstart);
13105 [ + - ]: 106 : printf ("%*s%s\n", ct > 50 ? 3 : 53 - ct, "", fptr);
13106 : :
13107 : 106 : fptr = fnext + 1;
13108 : : }
13109 : : }
13110 : :
13111 : : static void
13112 : 76 : handle_core_note (Ebl *ebl, const GElf_Nhdr *nhdr,
13113 : : const char *name, const void *desc)
13114 : : {
13115 : 76 : GElf_Word regs_offset;
13116 : 76 : size_t nregloc;
13117 : 76 : const Ebl_Register_Location *reglocs;
13118 : 76 : size_t nitems;
13119 : 76 : const Ebl_Core_Item *items;
13120 : :
13121 [ + + ]: 76 : if (! ebl_core_note (ebl, nhdr, name, desc,
13122 : : ®s_offset, &nregloc, ®locs, &nitems, &items))
13123 : 2 : return;
13124 : :
13125 : : /* Pass 0 for DESCSZ when there are registers in the note,
13126 : : so that the ITEMS array does not describe the whole thing.
13127 : : For non-register notes, the actual descsz might be a multiple
13128 : : of the unit size, not just exactly the unit size. */
13129 : 74 : unsigned int colno = handle_core_items (ebl->elf, desc,
13130 [ + + ]: 74 : nregloc == 0 ? nhdr->n_descsz : 0,
13131 : : items, nitems);
13132 [ + + ]: 74 : if (colno != 0)
13133 : 68 : putchar ('\n');
13134 : :
13135 : 74 : colno = handle_core_registers (ebl, ebl->elf, desc + regs_offset,
13136 : : reglocs, nregloc);
13137 [ + + ]: 74 : if (colno != 0)
13138 : 74 : putchar ('\n');
13139 : : }
13140 : :
13141 : : static void
13142 : 506 : handle_notes_data (Ebl *ebl, const GElf_Ehdr *ehdr,
13143 : : GElf_Off start, Elf_Data *data)
13144 : : {
13145 : 506 : fputs (_(" Owner Data size Type\n"), stdout);
13146 : :
13147 [ - + ]: 506 : if (data == NULL)
13148 : 0 : goto bad_note;
13149 : :
13150 : : size_t offset = 0;
13151 : : GElf_Nhdr nhdr;
13152 : : size_t name_offset;
13153 : : size_t desc_offset;
13154 : 5120 : while (offset < data->d_size
13155 [ + + + - ]: 5120 : && (offset = gelf_getnote (data, offset,
13156 : : &nhdr, &name_offset, &desc_offset)) > 0)
13157 : : {
13158 [ + - ]: 4614 : const char *name = nhdr.n_namesz == 0 ? "" : data->d_buf + name_offset;
13159 : 4614 : const char *desc = data->d_buf + desc_offset;
13160 : :
13161 : : /* GNU Build Attributes are weird, they store most of their data
13162 : : into the owner name field. Extract just the owner name
13163 : : prefix here, then use the rest later as data. */
13164 : 4614 : bool is_gnu_build_attr
13165 : 4614 : = startswith (name, ELF_NOTE_GNU_BUILD_ATTRIBUTE_PREFIX);
13166 : 9798 : const char *print_name = (is_gnu_build_attr
13167 [ + + ]: 4614 : ? ELF_NOTE_GNU_BUILD_ATTRIBUTE_PREFIX : name);
13168 : 5184 : size_t print_namesz = (is_gnu_build_attr
13169 : 570 : ? strlen (print_name) : nhdr.n_namesz);
13170 : :
13171 : 4614 : char buf[100];
13172 : 4614 : char buf2[100];
13173 : 4614 : printf (_(" %-13.*s %9" PRId32 " %s\n"),
13174 : : (int) print_namesz, print_name, nhdr.n_descsz,
13175 [ + + ]: 4614 : ehdr->e_type == ET_CORE
13176 : 118 : ? ebl_core_note_type_name (ebl, nhdr.n_type,
13177 : : buf, sizeof (buf))
13178 : 4496 : : ebl_object_note_type_name (ebl, name, nhdr.n_type,
13179 : : nhdr.n_descsz,
13180 : : buf2, sizeof (buf2)));
13181 : :
13182 : : /* Filter out invalid entries. */
13183 : 4614 : if (memchr (name, '\0', nhdr.n_namesz) != NULL
13184 : : /* XXX For now help broken Linux kernels. */
13185 : : || 1)
13186 : : {
13187 [ + + ]: 4614 : if (ehdr->e_type == ET_CORE)
13188 : : {
13189 [ + + ]: 118 : if (nhdr.n_type == NT_AUXV
13190 [ + - ]: 18 : && (nhdr.n_namesz == 4 /* Broken old Linux kernels. */
13191 [ + - + - ]: 18 : || (nhdr.n_namesz == 5 && name[4] == '\0'))
13192 [ + - ]: 18 : && !memcmp (name, "CORE", 4))
13193 : 18 : handle_auxv_note (ebl, ebl->elf, nhdr.n_descsz,
13194 : : start + desc_offset);
13195 [ + + + - ]: 100 : else if (nhdr.n_namesz == 5 && strcmp (name, "CORE") == 0)
13196 [ + + + ]: 76 : switch (nhdr.n_type)
13197 : : {
13198 : 12 : case NT_SIGINFO:
13199 : 12 : handle_siginfo_note (ebl->elf, nhdr.n_descsz,
13200 : : start + desc_offset);
13201 : 12 : break;
13202 : :
13203 : 12 : case NT_FILE:
13204 : 12 : handle_file_note (ebl->elf, nhdr.n_descsz,
13205 : : start + desc_offset);
13206 : 12 : break;
13207 : :
13208 : 52 : default:
13209 : 52 : handle_core_note (ebl, &nhdr, name, desc);
13210 : : }
13211 : : else
13212 : 24 : handle_core_note (ebl, &nhdr, name, desc);
13213 : : }
13214 : : else
13215 : 4496 : ebl_object_note (ebl, nhdr.n_namesz, name, nhdr.n_type,
13216 : : nhdr.n_descsz, desc);
13217 : : }
13218 : : }
13219 : :
13220 [ + - ]: 506 : if (offset == data->d_size)
13221 : 506 : return;
13222 : :
13223 : 0 : bad_note:
13224 : 0 : error (0, 0,
13225 : 0 : _("cannot get content of note: %s"),
13226 : 0 : data != NULL ? "garbage data" : elf_errmsg (-1));
13227 : : }
13228 : :
13229 : : static void
13230 : 230 : handle_notes (Ebl *ebl, GElf_Ehdr *ehdr)
13231 : : {
13232 : : /* If we have section headers, just look for SHT_NOTE sections.
13233 : : In a debuginfo file, the program headers are not reliable. */
13234 [ + + ]: 230 : if (shnum != 0)
13235 : : {
13236 : : /* Get the section header string table index. */
13237 : 208 : size_t shstrndx;
13238 [ - + ]: 208 : if (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0)
13239 : 0 : error_exit (0, _("cannot get section header string table index"));
13240 : :
13241 : : Elf_Scn *scn = NULL;
13242 [ + + ]: 6446 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
13243 : : {
13244 : 6238 : GElf_Shdr shdr_mem;
13245 : 6238 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
13246 : :
13247 [ + - + + ]: 6238 : if (shdr == NULL || shdr->sh_type != SHT_NOTE)
13248 : : /* Not what we are looking for. */
13249 : 5756 : continue;
13250 : :
13251 [ - + ]: 482 : if (notes_section != NULL)
13252 : : {
13253 : 0 : char *sname = elf_strptr (ebl->elf, shstrndx, shdr->sh_name);
13254 [ # # # # ]: 0 : if (sname == NULL || strcmp (sname, notes_section) != 0)
13255 : 0 : continue;
13256 : : }
13257 : :
13258 : 482 : printf (_("\
13259 : : \nNote section [%2zu] '%s' of %" PRIu64 " bytes at offset %#0" PRIx64 ":\n"),
13260 : : elf_ndxscn (scn),
13261 : 482 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
13262 : : shdr->sh_size, shdr->sh_offset);
13263 : :
13264 : 482 : handle_notes_data (ebl, ehdr, shdr->sh_offset,
13265 : : elf_getdata (scn, NULL));
13266 : : }
13267 : 208 : return;
13268 : : }
13269 : :
13270 : : /* We have to look through the program header to find the note
13271 : : sections. There can be more than one. */
13272 [ + + ]: 280 : for (size_t cnt = 0; cnt < phnum; ++cnt)
13273 : : {
13274 : 258 : GElf_Phdr mem;
13275 : 258 : GElf_Phdr *phdr = gelf_getphdr (ebl->elf, cnt, &mem);
13276 : :
13277 [ + - + + ]: 258 : if (phdr == NULL || phdr->p_type != PT_NOTE)
13278 : : /* Not what we are looking for. */
13279 : 234 : continue;
13280 : :
13281 : 24 : printf (_("\
13282 : : \nNote segment of %" PRIu64 " bytes at offset %#0" PRIx64 ":\n"),
13283 : : phdr->p_filesz, phdr->p_offset);
13284 : :
13285 : 24 : handle_notes_data (ebl, ehdr, phdr->p_offset,
13286 : : elf_getdata_rawchunk (ebl->elf,
13287 : 24 : phdr->p_offset, phdr->p_filesz,
13288 [ + + ]: 24 : (phdr->p_align == 8
13289 : : ? ELF_T_NHDR8 : ELF_T_NHDR)));
13290 : : }
13291 : : }
13292 : :
13293 : :
13294 : : static void
13295 : 12 : hex_dump (const uint8_t *data, size_t len)
13296 : : {
13297 : 12 : size_t pos = 0;
13298 [ + + ]: 56 : while (pos < len)
13299 : : {
13300 : 44 : printf (" 0x%08zx ", pos);
13301 : :
13302 : 44 : const size_t chunk = MIN (len - pos, 16);
13303 : :
13304 [ + + ]: 688 : for (size_t i = 0; i < chunk; ++i)
13305 [ + + ]: 644 : if (i % 4 == 3)
13306 : 160 : printf ("%02x ", data[pos + i]);
13307 : : else
13308 : 644 : printf ("%02x", data[pos + i]);
13309 : :
13310 [ + + ]: 44 : if (chunk < 16)
13311 : 4 : printf ("%*s", (int) ((16 - chunk) * 2 + (16 - chunk + 3) / 4), "");
13312 : :
13313 [ + + ]: 688 : for (size_t i = 0; i < chunk; ++i)
13314 : : {
13315 : 644 : unsigned char b = data[pos + i];
13316 [ + + ]: 1288 : printf ("%c", isprint (b) ? b : '.');
13317 : : }
13318 : :
13319 : 44 : putchar ('\n');
13320 : 44 : pos += chunk;
13321 : : }
13322 : 12 : }
13323 : :
13324 : : static void
13325 : 12 : dump_data_section (Elf_Scn *scn, const GElf_Shdr *shdr, const char *name)
13326 : : {
13327 [ + - - + ]: 12 : if (shdr->sh_size == 0 || shdr->sh_type == SHT_NOBITS)
13328 : 0 : printf (_("\nSection [%zu] '%s' has no data to dump.\n"),
13329 : : elf_ndxscn (scn), name);
13330 : : else
13331 : : {
13332 [ + + ]: 12 : if (print_decompress)
13333 : : {
13334 : : /* We try to decompress the section, but keep the old shdr around
13335 : : so we can show both the original shdr size and the uncompressed
13336 : : data size. */
13337 [ + + ]: 8 : if ((shdr->sh_flags & SHF_COMPRESSED) != 0)
13338 : : {
13339 [ - + ]: 4 : if (elf_compress (scn, 0, 0) < 0)
13340 : 0 : printf ("WARNING: %s [%zd]\n",
13341 : : _("Couldn't uncompress section"),
13342 : : elf_ndxscn (scn));
13343 : : }
13344 [ + - + - ]: 4 : else if (name && startswith (name, ".zdebug"))
13345 : : {
13346 [ - + ]: 4 : if (elf_compress_gnu (scn, 0, 0) < 0)
13347 : 0 : printf ("WARNING: %s [%zd]\n",
13348 : : _("Couldn't uncompress section"),
13349 : : elf_ndxscn (scn));
13350 : : }
13351 : : }
13352 : :
13353 : 12 : Elf_Data *data = elf_rawdata (scn, NULL);
13354 [ - + ]: 12 : if (data == NULL)
13355 : 0 : error (0, 0, _("cannot get data for section [%zu] '%s': %s"),
13356 : : elf_ndxscn (scn), name, elf_errmsg (-1));
13357 : : else
13358 : : {
13359 [ + + ]: 12 : if (data->d_size == shdr->sh_size)
13360 : 4 : printf (_("\nHex dump of section [%zu] '%s', %" PRIu64
13361 : : " bytes at offset %#0" PRIx64 ":\n"),
13362 : : elf_ndxscn (scn), name,
13363 : 4 : shdr->sh_size, shdr->sh_offset);
13364 : : else
13365 : 8 : printf (_("\nHex dump of section [%zu] '%s', %" PRIu64
13366 : : " bytes (%zd uncompressed) at offset %#0"
13367 : : PRIx64 ":\n"),
13368 : : elf_ndxscn (scn), name,
13369 : 8 : shdr->sh_size, data->d_size, shdr->sh_offset);
13370 : 12 : hex_dump (data->d_buf, data->d_size);
13371 : : }
13372 : : }
13373 : 12 : }
13374 : :
13375 : : static void
13376 : 374 : print_string_section (Elf_Scn *scn, const GElf_Shdr *shdr, const char *name)
13377 : : {
13378 [ + - + + ]: 374 : if (shdr->sh_size == 0 || shdr->sh_type == SHT_NOBITS)
13379 : 42 : printf (_("\nSection [%zu] '%s' has no strings to dump.\n"),
13380 : : elf_ndxscn (scn), name);
13381 : : else
13382 : : {
13383 [ + + ]: 332 : if (print_decompress)
13384 : : {
13385 : : /* We try to decompress the section, but keep the old shdr around
13386 : : so we can show both the original shdr size and the uncompressed
13387 : : data size. */
13388 [ - + ]: 2 : if ((shdr->sh_flags & SHF_COMPRESSED) != 0)
13389 : : {
13390 [ # # ]: 0 : if (elf_compress (scn, 0, 0) < 0)
13391 : 0 : printf ("WARNING: %s [%zd]\n",
13392 : : _("Couldn't uncompress section"),
13393 : : elf_ndxscn (scn));
13394 : : }
13395 [ + - + - ]: 2 : else if (name && startswith (name, ".zdebug"))
13396 : : {
13397 [ - + ]: 2 : if (elf_compress_gnu (scn, 0, 0) < 0)
13398 : 0 : printf ("WARNING: %s [%zd]\n",
13399 : : _("Couldn't uncompress section"),
13400 : : elf_ndxscn (scn));
13401 : : }
13402 : : }
13403 : :
13404 : 332 : Elf_Data *data = elf_rawdata (scn, NULL);
13405 [ - + ]: 332 : if (data == NULL)
13406 : 0 : error (0, 0, _("cannot get data for section [%zu] '%s': %s"),
13407 : : elf_ndxscn (scn), name, elf_errmsg (-1));
13408 : : else
13409 : : {
13410 [ + + ]: 332 : if (data->d_size == shdr->sh_size)
13411 : 330 : printf (_("\nString section [%zu] '%s' contains %" PRIu64
13412 : : " bytes at offset %#0" PRIx64 ":\n"),
13413 : : elf_ndxscn (scn), name,
13414 : 330 : shdr->sh_size, shdr->sh_offset);
13415 : : else
13416 : 2 : printf (_("\nString section [%zu] '%s' contains %" PRIu64
13417 : : " bytes (%zd uncompressed) at offset %#0"
13418 : : PRIx64 ":\n"),
13419 : : elf_ndxscn (scn), name,
13420 : 2 : shdr->sh_size, data->d_size, shdr->sh_offset);
13421 : :
13422 : 332 : const char *start = data->d_buf;
13423 : 332 : const char *const limit = start + data->d_size;
13424 : 44590 : do
13425 : : {
13426 : 44590 : const char *end = memchr (start, '\0', limit - start);
13427 : 44590 : const size_t pos = start - (const char *) data->d_buf;
13428 [ - + ]: 44590 : if (unlikely (end == NULL))
13429 : : {
13430 : 0 : printf (" [%6zx]- %.*s\n",
13431 : : pos, (int) (limit - start), start);
13432 : : break;
13433 : : }
13434 : 44590 : printf (" [%6zx] %s\n", pos, start);
13435 : 44590 : start = end + 1;
13436 [ + + ]: 44590 : } while (start < limit);
13437 : : }
13438 : : }
13439 : 374 : }
13440 : :
13441 : : static void
13442 : 198 : for_each_section_argument (Elf *elf, const struct section_argument *list,
13443 : : void (*dump) (Elf_Scn *scn, const GElf_Shdr *shdr,
13444 : : const char *name))
13445 : : {
13446 : : /* Get the section header string table index. */
13447 : 198 : size_t shstrndx;
13448 [ - + ]: 198 : if (elf_getshdrstrndx (elf, &shstrndx) < 0)
13449 : 0 : error_exit (0, _("cannot get section header string table index"));
13450 : :
13451 [ + + ]: 764 : for (const struct section_argument *a = list; a != NULL; a = a->next)
13452 : : {
13453 : 566 : Elf_Scn *scn;
13454 : 566 : GElf_Shdr shdr_mem;
13455 : 566 : const char *name = NULL;
13456 : :
13457 : 566 : char *endp = NULL;
13458 : 566 : unsigned long int shndx = strtoul (a->arg, &endp, 0);
13459 [ + + - + ]: 566 : if (endp != a->arg && *endp == '\0')
13460 : : {
13461 : 2 : scn = elf_getscn (elf, shndx);
13462 [ - + ]: 2 : if (scn == NULL)
13463 : : {
13464 : 0 : error (0, 0, _("\nsection [%lu] does not exist"), shndx);
13465 : 0 : continue;
13466 : : }
13467 : :
13468 [ - + ]: 2 : if (gelf_getshdr (scn, &shdr_mem) == NULL)
13469 : 0 : error_exit (0, _("cannot get section header: %s"),
13470 : : elf_errmsg (-1));
13471 : 2 : name = elf_strptr (elf, shstrndx, shdr_mem.sh_name);
13472 : 2 : (*dump) (scn, &shdr_mem, name);
13473 : : }
13474 : : else
13475 : : {
13476 : : /* Need to look up the section by name. */
13477 : : scn = NULL;
13478 : : bool found = false;
13479 [ + + ]: 18180 : while ((scn = elf_nextscn (elf, scn)) != NULL)
13480 : : {
13481 [ - + ]: 17616 : if (gelf_getshdr (scn, &shdr_mem) == NULL)
13482 : 0 : continue;
13483 : 17616 : name = elf_strptr (elf, shstrndx, shdr_mem.sh_name);
13484 [ - + ]: 17616 : if (name == NULL)
13485 : 0 : continue;
13486 [ + + ]: 17616 : if (!strcmp (name, a->arg))
13487 : : {
13488 : 384 : found = true;
13489 : 384 : (*dump) (scn, &shdr_mem, name);
13490 : : }
13491 : : }
13492 : :
13493 [ + + - + ]: 564 : if (unlikely (!found) && !a->implicit)
13494 : 566 : error (0, 0, _("\nsection '%s' does not exist"), a->arg);
13495 : : }
13496 : : }
13497 : 198 : }
13498 : :
13499 : : static void
13500 : 12 : dump_data (Ebl *ebl)
13501 : : {
13502 : 12 : for_each_section_argument (ebl->elf, dump_data_sections, &dump_data_section);
13503 : 12 : }
13504 : :
13505 : : static void
13506 : 186 : dump_strings (Ebl *ebl)
13507 : : {
13508 : 186 : for_each_section_argument (ebl->elf, string_sections, &print_string_section);
13509 : 186 : }
13510 : :
13511 : : static void
13512 : 0 : print_strings (Ebl *ebl)
13513 : : {
13514 : : /* Get the section header string table index. */
13515 : 0 : size_t shstrndx;
13516 [ # # ]: 0 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
13517 : 0 : error_exit (0, _("cannot get section header string table index"));
13518 : :
13519 : : Elf_Scn *scn;
13520 : : GElf_Shdr shdr_mem;
13521 : : const char *name;
13522 : : scn = NULL;
13523 [ # # ]: 0 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
13524 : : {
13525 [ # # ]: 0 : if (gelf_getshdr (scn, &shdr_mem) == NULL)
13526 : 0 : continue;
13527 : :
13528 [ # # ]: 0 : if (shdr_mem.sh_type != SHT_PROGBITS
13529 [ # # ]: 0 : || !(shdr_mem.sh_flags & SHF_STRINGS))
13530 : 0 : continue;
13531 : :
13532 : 0 : name = elf_strptr (ebl->elf, shstrndx, shdr_mem.sh_name);
13533 [ # # ]: 0 : if (name == NULL)
13534 : 0 : continue;
13535 : :
13536 : 0 : print_string_section (scn, &shdr_mem, name);
13537 : : }
13538 : 0 : }
13539 : :
13540 : : static void
13541 : 4 : dump_archive_index (Elf *elf, const char *fname)
13542 : : {
13543 : 4 : size_t narsym;
13544 : 4 : const Elf_Arsym *arsym = elf_getarsym (elf, &narsym);
13545 [ - + ]: 4 : if (arsym == NULL)
13546 : : {
13547 : 0 : int result = elf_errno ();
13548 [ # # ]: 0 : if (unlikely (result != ELF_E_NO_INDEX))
13549 : 0 : error_exit (0, _("cannot get symbol index of archive '%s': %s"),
13550 : : fname, elf_errmsg (result));
13551 : : else
13552 : 0 : printf (_("\nArchive '%s' has no symbol index\n"), fname);
13553 : 0 : return;
13554 : : }
13555 : :
13556 : 4 : printf (_("\nIndex of archive '%s' has %zu entries:\n"),
13557 : : fname, narsym);
13558 : :
13559 : 4 : size_t as_off = 0;
13560 [ + + ]: 22 : for (const Elf_Arsym *s = arsym; s < &arsym[narsym - 1]; ++s)
13561 : : {
13562 [ + + ]: 18 : if (s->as_off != as_off)
13563 : : {
13564 : 12 : as_off = s->as_off;
13565 : :
13566 : 12 : Elf *subelf = NULL;
13567 [ + - ]: 12 : if (unlikely (elf_rand (elf, as_off) == 0)
13568 [ - + ]: 12 : || unlikely ((subelf = elf_begin (-1, ELF_C_READ_MMAP, elf))
13569 : : == NULL))
13570 : : #if __GLIBC__ < 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ < 7)
13571 : : while (1)
13572 : : #endif
13573 : 0 : error_exit (0,
13574 : : _("cannot extract member at offset %zu in '%s': %s"),
13575 : : as_off, fname, elf_errmsg (-1));
13576 : :
13577 : 12 : const Elf_Arhdr *h = elf_getarhdr (subelf);
13578 [ + - ]: 12 : if (h != NULL)
13579 : 12 : printf (_("Archive member '%s' contains:\n"), h->ar_name);
13580 : :
13581 : 12 : elf_end (subelf);
13582 : : }
13583 : :
13584 : 18 : printf ("\t%s\n", s->as_name);
13585 : : }
13586 : : }
13587 : :
13588 : : #include "debugpred.h"
|