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 : 1780 : cleanup_list (struct section_argument *list)
373 : : {
374 [ + + + + ]: 2346 : 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 : 890 : main (int argc, char *argv[])
384 : : {
385 : : /* We use no threads here which can interfere with handling a stream. */
386 : 890 : (void) __fsetlocking (stdout, FSETLOCKING_BYCALLER);
387 : :
388 : : /* Set locale. */
389 : 890 : setlocale (LC_ALL, "");
390 : :
391 : : /* Initialize the message catalog. */
392 : 890 : textdomain (PACKAGE_TARNAME);
393 : :
394 : : /* Look up once. */
395 : 890 : yes_str = _("yes");
396 : 890 : no_str = _("no");
397 : :
398 : : /* Parse and process arguments. */
399 : 890 : int remaining;
400 : 890 : argp_parse (&argp, argc, argv, 0, &remaining, NULL);
401 : :
402 : : /* Before we start tell the ELF library which version we are using. */
403 : 890 : elf_version (EV_CURRENT);
404 : :
405 : : /* Now process all the files given at the command line. */
406 : 890 : bool only_one = remaining + 1 == argc;
407 : 972 : do
408 : : {
409 : : /* Open the file. */
410 : 972 : int fd = open (argv[remaining], O_RDONLY);
411 [ - + ]: 972 : if (fd == -1)
412 : : {
413 : 0 : error (0, errno, _("cannot open input file '%s'"), argv[remaining]);
414 : 0 : continue;
415 : : }
416 : :
417 : 972 : process_file (fd, argv[remaining], only_one);
418 : :
419 : 972 : close (fd);
420 : : }
421 [ + + ]: 972 : while (++remaining < argc);
422 : :
423 : 890 : cleanup_list (dump_data_sections);
424 : 890 : cleanup_list (string_sections);
425 : :
426 : 890 : 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 : 5662 : parse_opt (int key, char *arg,
447 : : struct argp_state *state __attribute__ ((unused)))
448 : : {
449 [ + + - + : 5662 : 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 : 264 : case 'w':
530 [ + + ]: 264 : 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 [ - + ]: 178 : else if (strcmp (arg, "abbrev") == 0)
537 : 0 : print_debug_sections |= section_abbrev;
538 [ + + ]: 178 : else if (strcmp (arg, "addr") == 0)
539 : : {
540 : 4 : print_debug_sections |= section_addr;
541 : 4 : implicit_debug_sections |= section_info;
542 : : }
543 [ + + ]: 174 : else if (strcmp (arg, "aranges") == 0)
544 : 6 : print_debug_sections |= section_aranges;
545 [ + + ]: 168 : else if (strcmp (arg, "decodedaranges") == 0)
546 : : {
547 : 2 : print_debug_sections |= section_aranges;
548 : 2 : decodedaranges = true;
549 : : }
550 [ + + ]: 166 : else if (strcmp (arg, "ranges") == 0)
551 : : {
552 : 26 : print_debug_sections |= section_ranges;
553 : 26 : implicit_debug_sections |= section_info;
554 : : }
555 [ + + + + ]: 140 : else if (strcmp (arg, "frame") == 0 || strcmp (arg, "frames") == 0)
556 : 6 : print_debug_sections |= section_frame;
557 [ + + ]: 134 : else if (strcmp (arg, "info") == 0)
558 : : {
559 : 40 : print_debug_sections |= section_info;
560 : 40 : 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 : 264 : any_control_option = true;
605 : 264 : 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 : 890 : case ARGP_KEY_FINI:
628 [ + + + - ]: 890 : 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 : 868 : 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 [ + - ]: 868 : if (*(bool *) arg)
811 : : return DWARF_CB_ABORT;
812 : 868 : *(bool *) arg = true;
813 : 868 : 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 : 968 : 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 : 968 : const struct process_dwflmod_args *a = arg;
830 : :
831 : : /* Print the file name. */
832 [ + + ]: 968 : 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 : 968 : process_elf_file (dwflmod, a->fd);
841 : :
842 : 968 : 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 : 986 : create_dwfl (int fd, const char *fname)
875 : : {
876 : : /* Duplicate an fd for dwfl_report_offline to swallow. */
877 : 986 : int dwfl_fd = dup (fd);
878 [ - + ]: 986 : 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 : 986 : static const Dwfl_Callbacks callbacks =
884 : : {
885 : : .section_address = dwfl_offline_section_address,
886 : : .find_debuginfo = find_no_debuginfo
887 : : };
888 : 986 : Dwfl *dwfl = dwfl_begin (&callbacks);
889 [ + - ]: 986 : if (likely (dwfl != NULL))
890 : : /* Let 0 be the logical address of the file (or first in archive). */
891 : 986 : dwfl->offline_next_address = 0;
892 [ - + ]: 986 : 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 : 986 : dwfl_report_end (dwfl, NULL, NULL);
907 : :
908 : 986 : return dwfl;
909 : : }
910 : :
911 : : /* Process one input file. */
912 : : static void
913 : 972 : process_file (int fd, const char *fname, bool only_one)
914 : : {
915 [ + + ]: 972 : if (print_archive_index)
916 : 4 : check_archive_index (fd, fname, only_one);
917 : :
918 [ + + ]: 972 : if (!any_control_option)
919 : : return;
920 : :
921 [ + + ]: 968 : 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 : 968 : Dwfl *dwfl = create_dwfl (fd, fname);
937 [ + - ]: 968 : if (dwfl != NULL)
938 : : {
939 [ + + ]: 968 : if (only_one)
940 : : {
941 : : /* Clear ONLY_ONE if we have multiple modules, from an archive. */
942 : 868 : bool seen = false;
943 : 868 : only_one = dwfl_getmodules (dwfl, &count_dwflmod, &seen, 0) == 0;
944 : : }
945 : :
946 : : /* Process the one or more modules gleaned from this file. */
947 : 968 : struct process_dwflmod_args a = { .fd = fd, .only_one = only_one };
948 : 968 : 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 [ + - ]: 968 : if (! do_not_close_dwfl)
953 : 968 : dwfl_end (dwfl);
954 : :
955 : : /* Need to close the replaced fd if we created it. Caller takes
956 : : care of original. */
957 [ + + ]: 968 : 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 : 968 : process_elf_file (Dwfl_Module *dwflmod, int fd)
979 : : {
980 : 968 : GElf_Addr dwflbias;
981 : 968 : Elf *elf = dwfl_module_getelf (dwflmod, &dwflbias);
982 : :
983 : 968 : GElf_Ehdr ehdr_mem;
984 : 968 : GElf_Ehdr *ehdr = gelf_getehdr (elf, &ehdr_mem);
985 : :
986 [ - + ]: 968 : if (ehdr == NULL)
987 : : {
988 : 0 : error (0, 0, _("cannot read ELF header: %s"), elf_errmsg (-1));
989 : 0 : return;
990 : : }
991 : :
992 : 968 : Ebl *ebl = ebl_openbackend (elf);
993 [ - + ]: 968 : 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 [ - + ]: 968 : 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 [ - + ]: 968 : 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 : 2132 : bool print_unchanged = ((print_section_header
1016 [ + + ]: 462 : || print_relocations
1017 [ + + ]: 452 : || dump_data_sections != NULL
1018 [ + + ]: 440 : || print_notes)
1019 [ + + + + ]: 1036 : && (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 : 968 : bool symtab_printed = false;
1048 : :
1049 [ + + ]: 968 : if (print_file_header)
1050 : 190 : print_ehdr (ebl, ehdr);
1051 [ + + ]: 968 : if (print_section_header)
1052 : 506 : print_shdr (pure_ebl, ehdr);
1053 [ + + ]: 968 : if (print_program_header)
1054 : 188 : print_phdr (ebl, ehdr);
1055 [ + + ]: 968 : if (print_section_groups)
1056 : 202 : print_scngrp (ebl);
1057 [ + + ]: 968 : if (print_dynamic_table)
1058 : 190 : print_dynamic (ebl);
1059 [ + + ]: 968 : if (print_relocations)
1060 : 194 : print_relocs (pure_ebl, dwflmod, ehdr);
1061 [ + + ]: 968 : if (print_histogram)
1062 : 184 : handle_hash (ebl);
1063 [ + + + + ]: 968 : if (print_symbol_table || print_dynsym_table)
1064 : 220 : symtab_printed |= print_symtab (ebl, SHT_DYNSYM);
1065 [ + + ]: 968 : if (print_version_info)
1066 : 184 : print_verinfo (ebl);
1067 [ + + + - ]: 968 : if (print_symbol_table && !use_dynamic_segment)
1068 : 218 : symtab_printed |= print_symtab (ebl, SHT_SYMTAB);
1069 : :
1070 [ + + + + ]: 968 : 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 [ + + ]: 968 : if (print_arch)
1076 : 192 : print_liblist (ebl);
1077 [ + + ]: 968 : if (print_arch)
1078 : 192 : print_attributes (ebl, ehdr);
1079 [ + + ]: 968 : if (dump_data_sections != NULL)
1080 : 12 : dump_data (pure_ebl);
1081 [ + + ]: 968 : if (string_sections != NULL)
1082 : 186 : dump_strings (ebl);
1083 [ + + ]: 968 : if ((print_debug_sections | implicit_debug_sections) != 0)
1084 : 502 : print_debug (dwflmod, ebl, ehdr);
1085 [ + + ]: 968 : if (print_notes)
1086 : 230 : handle_notes (pure_ebl, ehdr);
1087 [ - + ]: 968 : if (print_string_sections)
1088 : 0 : print_strings (ebl);
1089 : :
1090 [ + + ]: 968 : 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 : 772 : 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 : 76868 : get_visibility_type (int value)
1245 : : {
1246 [ - + - - : 76868 : 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 : 1252 : section_name (Ebl *ebl, GElf_Shdr *shdr)
1629 : : {
1630 : 1252 : size_t shstrndx;
1631 [ + - - + ]: 1252 : if (shdr == NULL || elf_getshdrstrndx (ebl->elf, &shstrndx) < 0)
1632 : 0 : return "???";
1633 [ - + ]: 1252 : 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 [ + + ]: 1165914 : for (int cnt = 0; cnt < nentries; ++cnt)
2356 : : {
2357 : 1165632 : GElf_Rela relmem;
2358 : 1165632 : GElf_Rela *rel = gelf_getrela (data, cnt, &relmem);
2359 [ + - ]: 1165632 : if (likely (rel != NULL))
2360 : : {
2361 : 1165632 : char buf[64];
2362 : 1165632 : GElf_Sym symmem;
2363 : 1165632 : Elf32_Word xndx;
2364 : 2331264 : GElf_Sym *sym = gelf_getsymshndx (symdata, xndxdata,
2365 : 1165632 : GELF_R_SYM (rel->r_info),
2366 : : &symmem, &xndx);
2367 : :
2368 [ - + ]: 1165632 : 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 [ + + ]: 1165632 : else if (GELF_ST_TYPE (sym->st_info) != STT_SECTION
2421 [ + + ]: 627726 : && !(GELF_ST_TYPE (sym->st_info) == STT_NOTYPE
2422 [ + + ]: 100986 : && GELF_ST_BIND (sym->st_info) == STB_LOCAL
2423 [ + + ]: 99456 : && sym->st_shndx != SHN_UNDEF
2424 [ + + ]: 716 : && sym->st_value == 0)) // local start section label
2425 [ + - ]: 2421060 : printf ("\
2426 : : %#0*" PRIx64 " %-15s %#0*" PRIx64 " %+6" PRId64 " %s\n",
2427 : : class == ELFCLASS32 ? 10 : 18, rel->r_offset,
2428 [ + - ]: 627714 : 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 : 627714 : ? 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 : 627714 : 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 [ + + ]: 77134 : for (unsigned int cnt = 0; cnt < nsyms; ++cnt)
2652 : : {
2653 : 76868 : char typebuf[64];
2654 : 76868 : char bindbuf[64];
2655 : 76868 : char scnbuf[64];
2656 : 76868 : const char *sym_name;
2657 : 76868 : Elf32_Word xndx;
2658 : 76868 : GElf_Sym sym_mem;
2659 : 76868 : GElf_Sym *sym
2660 : 76868 : = gelf_getsymshndx (symdata, xndx_data, cnt, &sym_mem, &xndx);
2661 : :
2662 [ - + ]: 76868 : if (unlikely (sym == NULL))
2663 : 0 : continue;
2664 : :
2665 : : /* Determine the real section index. */
2666 [ + - ]: 76868 : if (likely (sym->st_shndx != SHN_XINDEX))
2667 : 76868 : xndx = sym->st_shndx;
2668 [ - + ]: 76868 : 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 : 76868 : sym_name = elf_strptr (ebl->elf, idx, sym->st_name);
2678 : :
2679 [ - + ]: 76868 : if (sym_name == NULL)
2680 : : sym_name = "???";
2681 : :
2682 [ + + ]: 152848 : printf (_ ("\
2683 : : %5u: %0*" PRIx64 " %6" PRId64 " %-7s %-6s %-9s %6s %s"),
2684 : 76868 : cnt, gelf_getclass (ebl->elf) == ELFCLASS32 ? 8 : 16,
2685 : : sym->st_value, sym->st_size,
2686 : 76868 : ebl_symbol_type_name (ebl, GELF_ST_TYPE (sym->st_info), typebuf,
2687 : : sizeof (typebuf)),
2688 : 76868 : ebl_symbol_binding_name (ebl, GELF_ST_BIND (sym->st_info),
2689 : : bindbuf, sizeof (bindbuf)),
2690 : 76868 : get_visibility_type (GELF_ST_VISIBILITY (sym->st_other)),
2691 : 76868 : ebl_section_name (ebl, sym->st_shndx, xndx, scnbuf,
2692 : : sizeof (scnbuf), NULL, shnum),
2693 : : sym_name);
2694 : :
2695 [ + + ]: 76868 : 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 : 76868 : 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 : 810 : 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 [ - + ]: 810 : 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 : 2172166 : 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 : 2172166 : GElf_Sym sym;
4261 : 2172166 : GElf_Off off = 0;
4262 [ + + ]: 13076 : const char *name = (print_address_names && ! print_unresolved_addresses)
4263 : 12912 : ? dwfl_module_addrinfo (dwflmod, address, &off, &sym, NULL, NULL, NULL)
4264 [ + + ]: 2172166 : : NULL;
4265 : :
4266 : 2172166 : const char *scn;
4267 [ + + ]: 2172166 : if (print_unresolved_addresses)
4268 : : {
4269 : 168 : address = raw;
4270 : 168 : scn = NULL;
4271 : : }
4272 : : else
4273 : : {
4274 : : /* Relativize the address. */
4275 : 2171998 : int n = dwfl_module_relocations (dwflmod);
4276 [ + + ]: 2171998 : 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 : 2169246 : scn = (i < 0 ? NULL
4280 [ - + ]: 2169246 : : dwfl_module_relocation_info (dwflmod, i, NULL));
4281 : : }
4282 : :
4283 [ - + ]: 2172166 : if ((name != NULL
4284 : 12504 : ? (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 : 20108 : : printf ("%s+%#0*" PRIx64 " <%s>",
4302 : 10054 : scn, 2 + address_size * 2, address, name))
4303 : : : (address_size == 0
4304 : 160 : ? printf ("%#" PRIx64 " <%s>", address, name)
4305 : 1680 : : printf ("%#0*" PRIx64 " <%s>",
4306 : 840 : 2 + address_size * 2, address, name))))
4307 : : : (scn != NULL
4308 : : ? (address_size == 0
4309 : 788630 : ? printf ("%s+%#" PRIx64, scn, address)
4310 : 1370554 : : printf ("%s+%#0*" PRIx64, scn, 2 + address_size * 2, address))
4311 : : : (address_size == 0
4312 : 70 : ? printf ("%#" PRIx64, address)
4313 [ + + + + : 4343924 : : printf ("%#0*" PRIx64, 2 + address_size * 2, address)))) < 0)
+ + + + +
+ + + + +
+ + + + +
+ + + ]
4314 : 0 : error_exit (0, _("sprintf failure"));
4315 : 2172166 : }
4316 : :
4317 : :
4318 : : static const char *
4319 : 2243798 : dwarf_tag_string (unsigned int tag)
4320 : : {
4321 [ - + + - : 2243798 : switch (tag)
- - - - +
+ + + + -
- - - - -
+ - + - +
- - + + -
+ - - - -
- - - + -
+ - + + +
- - - - -
- + - - +
- - - + -
+ + + + -
- - - - +
+ + + - +
+ + + - -
- ]
4322 : : {
4323 : : #define DWARF_ONE_KNOWN_DW_TAG(NAME, CODE) case CODE: return #NAME;
4324 : 2243798 : 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 : 8684638 : dwarf_attr_string (unsigned int attrnum)
4334 : : {
4335 [ + - + - : 8684638 : switch (attrnum)
- - - + -
- + + + -
- - - + +
- + - - +
- + - + -
- - - - -
- - - - -
- - - - -
- - - + -
+ - + - +
- - - + +
- - - + -
+ - + + -
- + + + +
- + + + -
+ - - + -
+ - + + -
+ - - + +
+ + - - -
- - + + +
+ - + - +
- - - - +
+ - + - +
+ - + + +
+ + + + -
- + - - -
+ - + - -
- - + + -
+ - - - -
+ - - - +
+ - + - -
- - + + -
- - - - +
+ - - - -
- - - + ]
4336 : : {
4337 : : #define DWARF_ONE_KNOWN_DW_AT(NAME, CODE) case CODE: return #NAME;
4338 : 8684636 : 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 : 8694876 : dwarf_form_string (unsigned int form)
4348 : : {
4349 [ + + + + : 8694876 : switch (form)
+ - - - -
- + - - +
+ + + + +
+ + + - +
+ - - + -
- + - - +
+ + + + +
- + + - -
- + - + ]
4350 : : {
4351 : : #define DWARF_ONE_KNOWN_DW_FORM(NAME, CODE) case CODE: return #NAME;
4352 : 8694872 : 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 : 3680 : dwarf_lang_string (unsigned int lang)
4362 : : {
4363 [ - - + - : 3680 : switch (lang)
- - - + -
- + + - +
- - + - -
- - - - -
- - - + -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - +
- ]
4364 : : {
4365 : : #define DWARF_ONE_KNOWN_DW_LANG(NAME, CODE) case CODE: return #NAME;
4366 : 3678 : 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 : 4 : dwarf_lname_string (unsigned int lname)
4376 : : {
4377 [ - - - + : 4 : switch (lname)
- + - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - ]
4378 : : {
4379 : : #define DWARF_ONE_KNOWN_DW_LNAME(NAME, CODE) case CODE: return #NAME;
4380 : 4 : DWARF_ALL_KNOWN_DW_LNAME
4381 : : #undef DWARF_ONE_KNOWN_DW_LNAME
4382 : 0 : default:
4383 : 0 : return NULL;
4384 : : }
4385 : : }
4386 : :
4387 : :
4388 : : static const char *
4389 : 7138 : dwarf_inline_string (unsigned int code)
4390 : : {
4391 : 7138 : static const char *const known[] =
4392 : : {
4393 : : #define DWARF_ONE_KNOWN_DW_INL(NAME, CODE) [CODE] = #NAME,
4394 : : DWARF_ALL_KNOWN_DW_INL
4395 : : #undef DWARF_ONE_KNOWN_DW_INL
4396 : : };
4397 : :
4398 : 7138 : if (likely (code < sizeof (known) / sizeof (known[0])))
4399 : 7138 : return known[code];
4400 : :
4401 : : return NULL;
4402 : : }
4403 : :
4404 : :
4405 : : static const char *
4406 : 56482 : dwarf_encoding_string (unsigned int code)
4407 : : {
4408 : 56482 : static const char *const known[] =
4409 : : {
4410 : : #define DWARF_ONE_KNOWN_DW_ATE(NAME, CODE) [CODE] = #NAME,
4411 : : DWARF_ALL_KNOWN_DW_ATE
4412 : : #undef DWARF_ONE_KNOWN_DW_ATE
4413 : : };
4414 : :
4415 : 56482 : if (likely (code < sizeof (known) / sizeof (known[0])))
4416 : 56482 : return known[code];
4417 : :
4418 : : return NULL;
4419 : : }
4420 : :
4421 : :
4422 : : static const char *
4423 : 0 : dwarf_access_string (unsigned int code)
4424 : : {
4425 : 0 : static const char *const known[] =
4426 : : {
4427 : : #define DWARF_ONE_KNOWN_DW_ACCESS(NAME, CODE) [CODE] = #NAME,
4428 : : DWARF_ALL_KNOWN_DW_ACCESS
4429 : : #undef DWARF_ONE_KNOWN_DW_ACCESS
4430 : : };
4431 : :
4432 : 0 : if (likely (code < sizeof (known) / sizeof (known[0])))
4433 : 0 : return known[code];
4434 : :
4435 : : return NULL;
4436 : : }
4437 : :
4438 : :
4439 : : static const char *
4440 : 0 : dwarf_defaulted_string (unsigned int code)
4441 : : {
4442 : 0 : static const char *const known[] =
4443 : : {
4444 : : #define DWARF_ONE_KNOWN_DW_DEFAULTED(NAME, CODE) [CODE] = #NAME,
4445 : : DWARF_ALL_KNOWN_DW_DEFAULTED
4446 : : #undef DWARF_ONE_KNOWN_DW_DEFAULTED
4447 : : };
4448 : :
4449 : 0 : if (likely (code < sizeof (known) / sizeof (known[0])))
4450 : 0 : return known[code];
4451 : :
4452 : : return NULL;
4453 : : }
4454 : :
4455 : :
4456 : : static const char *
4457 : 0 : dwarf_visibility_string (unsigned int code)
4458 : : {
4459 : 0 : static const char *const known[] =
4460 : : {
4461 : : #define DWARF_ONE_KNOWN_DW_VIS(NAME, CODE) [CODE] = #NAME,
4462 : : DWARF_ALL_KNOWN_DW_VIS
4463 : : #undef DWARF_ONE_KNOWN_DW_VIS
4464 : : };
4465 : :
4466 : 0 : if (likely (code < sizeof (known) / sizeof (known[0])))
4467 : 0 : return known[code];
4468 : :
4469 : : return NULL;
4470 : : }
4471 : :
4472 : :
4473 : : static const char *
4474 : 0 : dwarf_virtuality_string (unsigned int code)
4475 : : {
4476 : 0 : static const char *const known[] =
4477 : : {
4478 : : #define DWARF_ONE_KNOWN_DW_VIRTUALITY(NAME, CODE) [CODE] = #NAME,
4479 : : DWARF_ALL_KNOWN_DW_VIRTUALITY
4480 : : #undef DWARF_ONE_KNOWN_DW_VIRTUALITY
4481 : : };
4482 : :
4483 : 0 : if (likely (code < sizeof (known) / sizeof (known[0])))
4484 : 0 : return known[code];
4485 : :
4486 : : return NULL;
4487 : : }
4488 : :
4489 : :
4490 : : static const char *
4491 : 0 : dwarf_identifier_case_string (unsigned int code)
4492 : : {
4493 : 0 : static const char *const known[] =
4494 : : {
4495 : : #define DWARF_ONE_KNOWN_DW_ID(NAME, CODE) [CODE] = #NAME,
4496 : : DWARF_ALL_KNOWN_DW_ID
4497 : : #undef DWARF_ONE_KNOWN_DW_ID
4498 : : };
4499 : :
4500 : 0 : if (likely (code < sizeof (known) / sizeof (known[0])))
4501 : 0 : return known[code];
4502 : :
4503 : : return NULL;
4504 : : }
4505 : :
4506 : :
4507 : : static const char *
4508 : 0 : dwarf_calling_convention_string (unsigned int code)
4509 : : {
4510 : 0 : static const char *const known[] =
4511 : : {
4512 : : #define DWARF_ONE_KNOWN_DW_CC(NAME, CODE) [CODE] = #NAME,
4513 : : DWARF_ALL_KNOWN_DW_CC
4514 : : #undef DWARF_ONE_KNOWN_DW_CC
4515 : : };
4516 : :
4517 : 0 : if (likely (code < sizeof (known) / sizeof (known[0])))
4518 : 0 : return known[code];
4519 : :
4520 : : return NULL;
4521 : : }
4522 : :
4523 : :
4524 : : static const char *
4525 : 0 : dwarf_ordering_string (unsigned int code)
4526 : : {
4527 : 0 : static const char *const known[] =
4528 : : {
4529 : : #define DWARF_ONE_KNOWN_DW_ORD(NAME, CODE) [CODE] = #NAME,
4530 : : DWARF_ALL_KNOWN_DW_ORD
4531 : : #undef DWARF_ONE_KNOWN_DW_ORD
4532 : : };
4533 : :
4534 : 0 : if (likely (code < sizeof (known) / sizeof (known[0])))
4535 : 0 : return known[code];
4536 : :
4537 : : return NULL;
4538 : : }
4539 : :
4540 : :
4541 : : static const char *
4542 : 16 : dwarf_discr_list_string (unsigned int code)
4543 : : {
4544 : 16 : static const char *const known[] =
4545 : : {
4546 : : #define DWARF_ONE_KNOWN_DW_DSC(NAME, CODE) [CODE] = #NAME,
4547 : : DWARF_ALL_KNOWN_DW_DSC
4548 : : #undef DWARF_ONE_KNOWN_DW_DSC
4549 : : };
4550 : :
4551 : 16 : if (likely (code < sizeof (known) / sizeof (known[0])))
4552 : 16 : return known[code];
4553 : :
4554 : : return NULL;
4555 : : }
4556 : :
4557 : :
4558 : : static const char *
4559 : 1146778 : dwarf_locexpr_opcode_string (unsigned int code)
4560 : : {
4561 : 1146778 : static const char *const known[] =
4562 : : {
4563 : : /* Normally we can't afford building huge table of 64K entries,
4564 : : most of them zero, just because there are a couple defined
4565 : : values at the far end. In case of opcodes, it's OK. */
4566 : : #define DWARF_ONE_KNOWN_DW_OP(NAME, CODE) [CODE] = #NAME,
4567 : : DWARF_ALL_KNOWN_DW_OP
4568 : : #undef DWARF_ONE_KNOWN_DW_OP
4569 : : };
4570 : :
4571 : 1146778 : if (likely (code < sizeof (known) / sizeof (known[0])))
4572 : 1146778 : return known[code];
4573 : :
4574 : : return NULL;
4575 : : }
4576 : :
4577 : :
4578 : : static const char *
4579 : 3426 : dwarf_unit_string (unsigned int type)
4580 : : {
4581 [ - + - - : 3426 : switch (type)
- - + ]
4582 : : {
4583 : : #define DWARF_ONE_KNOWN_DW_UT(NAME, CODE) case CODE: return #NAME;
4584 : 14 : DWARF_ALL_KNOWN_DW_UT
4585 : : #undef DWARF_ONE_KNOWN_DW_UT
4586 : 0 : default:
4587 : 0 : return NULL;
4588 : : }
4589 : : }
4590 : :
4591 : :
4592 : : static const char *
4593 : 132866 : dwarf_range_list_encoding_string (unsigned int kind)
4594 : : {
4595 [ - + + - : 132866 : switch (kind)
+ - - -
+ ]
4596 : : {
4597 : : #define DWARF_ONE_KNOWN_DW_RLE(NAME, CODE) case CODE: return #NAME;
4598 : 124402 : DWARF_ALL_KNOWN_DW_RLE
4599 : : #undef DWARF_ONE_KNOWN_DW_RLE
4600 : 0 : default:
4601 : 0 : return NULL;
4602 : : }
4603 : : }
4604 : :
4605 : :
4606 : : static const char *
4607 : 626624 : dwarf_loc_list_encoding_string (unsigned int kind)
4608 : : {
4609 [ - - + + : 626624 : switch (kind)
- + - + -
- + ]
4610 : : {
4611 : : #define DWARF_ONE_KNOWN_DW_LLE(NAME, CODE) case CODE: return #NAME;
4612 : 608442 : DWARF_ALL_KNOWN_DW_LLE
4613 : : #undef DWARF_ONE_KNOWN_DW_LLE
4614 : : /* DW_LLE_GNU_view_pair is special/incompatible with default codes. */
4615 : 0 : case DW_LLE_GNU_view_pair: return "GNU_view_pair";
4616 : 0 : default:
4617 : 0 : return NULL;
4618 : : }
4619 : : }
4620 : :
4621 : :
4622 : : static const char *
4623 : 10238 : dwarf_line_content_description_string (unsigned int kind)
4624 : : {
4625 [ + + - - : 10238 : switch (kind)
- + ]
4626 : : {
4627 : : #define DWARF_ONE_KNOWN_DW_LNCT(NAME, CODE) case CODE: return #NAME;
4628 : 10230 : DWARF_ALL_KNOWN_DW_LNCT
4629 : : #undef DWARF_ONE_KNOWN_DW_LNCT
4630 : 0 : default:
4631 : 0 : return NULL;
4632 : : }
4633 : : }
4634 : :
4635 : :
4636 : : /* Used by all dwarf_foo_name functions. */
4637 : : static const char *
4638 : 20463786 : string_or_unknown (const char *known, unsigned int code,
4639 : : unsigned int lo_user, unsigned int hi_user,
4640 : : bool print_unknown_num)
4641 : : {
4642 : 20463786 : static char unknown_buf[20];
4643 : :
4644 [ + + ]: 20463786 : if (likely (known != NULL))
4645 : : return known;
4646 : :
4647 [ - + - - ]: 2 : if (lo_user != 0 && code >= lo_user && code <= hi_user)
4648 : : {
4649 : 0 : snprintf (unknown_buf, sizeof unknown_buf, "lo_user+%#x",
4650 : : code - lo_user);
4651 : 0 : return unknown_buf;
4652 : : }
4653 : :
4654 [ + - ]: 2 : if (print_unknown_num)
4655 : : {
4656 : 2 : snprintf (unknown_buf, sizeof unknown_buf, "??? (%#x)", code);
4657 : 2 : return unknown_buf;
4658 : : }
4659 : :
4660 : : return "???";
4661 : : }
4662 : :
4663 : :
4664 : : static const char *
4665 : 2243798 : dwarf_tag_name (unsigned int tag)
4666 : : {
4667 : 2243798 : const char *ret = dwarf_tag_string (tag);
4668 : 2243798 : return string_or_unknown (ret, tag, DW_TAG_lo_user, DW_TAG_hi_user, true);
4669 : : }
4670 : :
4671 : : static const char *
4672 : 8684638 : dwarf_attr_name (unsigned int attr)
4673 : : {
4674 : 8684638 : const char *ret = dwarf_attr_string (attr);
4675 : 8684638 : return string_or_unknown (ret, attr, DW_AT_lo_user, DW_AT_hi_user, true);
4676 : : }
4677 : :
4678 : :
4679 : : static const char *
4680 : 8694876 : dwarf_form_name (unsigned int form)
4681 : : {
4682 : 8694876 : const char *ret = dwarf_form_string (form);
4683 : 8694876 : return string_or_unknown (ret, form, 0, 0, true);
4684 : : }
4685 : :
4686 : :
4687 : : static const char *
4688 : 3676 : dwarf_lang_name (unsigned int lang)
4689 : : {
4690 : 3676 : const char *ret = dwarf_lang_string (lang);
4691 : 3676 : return string_or_unknown (ret, lang, DW_LANG_lo_user, DW_LANG_hi_user, false);
4692 : : }
4693 : :
4694 : :
4695 : : static const char *
4696 : 4 : dwarf_lname_name (unsigned int lname)
4697 : : {
4698 : 4 : const char *ret = dwarf_lname_string (lname);
4699 : 4 : return string_or_unknown (ret, lname, DW_LNAME_lo_user, DW_LNAME_hi_user,
4700 : : false);
4701 : : }
4702 : :
4703 : :
4704 : : static const char *
4705 : 7138 : dwarf_inline_name (unsigned int code)
4706 : : {
4707 [ + - ]: 7138 : const char *ret = dwarf_inline_string (code);
4708 : 7138 : return string_or_unknown (ret, code, 0, 0, false);
4709 : : }
4710 : :
4711 : :
4712 : : static const char *
4713 : 56482 : dwarf_encoding_name (unsigned int code)
4714 : : {
4715 [ + - ]: 56482 : const char *ret = dwarf_encoding_string (code);
4716 : 56482 : return string_or_unknown (ret, code, DW_ATE_lo_user, DW_ATE_hi_user, false);
4717 : : }
4718 : :
4719 : :
4720 : : static const char *
4721 : 0 : dwarf_access_name (unsigned int code)
4722 : : {
4723 [ # # ]: 0 : const char *ret = dwarf_access_string (code);
4724 : 0 : return string_or_unknown (ret, code, 0, 0, false);
4725 : : }
4726 : :
4727 : :
4728 : : static const char *
4729 : 0 : dwarf_defaulted_name (unsigned int code)
4730 : : {
4731 [ # # ]: 0 : const char *ret = dwarf_defaulted_string (code);
4732 : 0 : return string_or_unknown (ret, code, 0, 0, false);
4733 : : }
4734 : :
4735 : :
4736 : : static const char *
4737 : 0 : dwarf_visibility_name (unsigned int code)
4738 : : {
4739 [ # # ]: 0 : const char *ret = dwarf_visibility_string (code);
4740 : 0 : return string_or_unknown (ret, code, 0, 0, false);
4741 : : }
4742 : :
4743 : :
4744 : : static const char *
4745 : 0 : dwarf_virtuality_name (unsigned int code)
4746 : : {
4747 [ # # ]: 0 : const char *ret = dwarf_virtuality_string (code);
4748 : 0 : return string_or_unknown (ret, code, 0, 0, false);
4749 : : }
4750 : :
4751 : :
4752 : : static const char *
4753 : 0 : dwarf_identifier_case_name (unsigned int code)
4754 : : {
4755 [ # # ]: 0 : const char *ret = dwarf_identifier_case_string (code);
4756 : 0 : return string_or_unknown (ret, code, 0, 0, false);
4757 : : }
4758 : :
4759 : :
4760 : : static const char *
4761 : 0 : dwarf_calling_convention_name (unsigned int code)
4762 : : {
4763 [ # # ]: 0 : const char *ret = dwarf_calling_convention_string (code);
4764 : 0 : return string_or_unknown (ret, code, DW_CC_lo_user, DW_CC_hi_user, false);
4765 : : }
4766 : :
4767 : :
4768 : : static const char *
4769 : 0 : dwarf_ordering_name (unsigned int code)
4770 : : {
4771 [ # # ]: 0 : const char *ret = dwarf_ordering_string (code);
4772 : 0 : return string_or_unknown (ret, code, 0, 0, false);
4773 : : }
4774 : :
4775 : :
4776 : : static const char *
4777 : 16 : dwarf_discr_list_name (unsigned int code)
4778 : : {
4779 [ + - ]: 16 : const char *ret = dwarf_discr_list_string (code);
4780 : 16 : return string_or_unknown (ret, code, 0, 0, false);
4781 : : }
4782 : :
4783 : :
4784 : : static const char *
4785 : 3426 : dwarf_unit_name (unsigned int type)
4786 : : {
4787 : 3426 : const char *ret = dwarf_unit_string (type);
4788 : 3426 : return string_or_unknown (ret, type, DW_UT_lo_user, DW_UT_hi_user, true);
4789 : : }
4790 : :
4791 : :
4792 : : static const char *
4793 : 132866 : dwarf_range_list_encoding_name (unsigned int kind)
4794 : : {
4795 : 132866 : const char *ret = dwarf_range_list_encoding_string (kind);
4796 : 132866 : return string_or_unknown (ret, kind, 0, 0, false);
4797 : : }
4798 : :
4799 : :
4800 : : static const char *
4801 : 626624 : dwarf_loc_list_encoding_name (unsigned int kind)
4802 : : {
4803 : 626624 : const char *ret = dwarf_loc_list_encoding_string (kind);
4804 : 626624 : return string_or_unknown (ret, kind, 0, 0, false);
4805 : : }
4806 : :
4807 : :
4808 : : static const char *
4809 : 10238 : dwarf_line_content_description_name (unsigned int kind)
4810 : : {
4811 : 10238 : const char *ret = dwarf_line_content_description_string (kind);
4812 : 10238 : return string_or_unknown (ret, kind, DW_LNCT_lo_user, DW_LNCT_hi_user,
4813 : : false);
4814 : : }
4815 : :
4816 : :
4817 : : static void
4818 : 510 : print_block (size_t n, const void *block)
4819 : : {
4820 [ - + ]: 510 : if (n == 0)
4821 : 0 : puts (_("empty block"));
4822 : : else
4823 : : {
4824 : 510 : printf (_("%zu byte block:"), n);
4825 : 510 : const unsigned char *data = block;
4826 : 2250 : do
4827 : 2250 : printf (" %02x", *data++);
4828 [ + + ]: 2250 : while (--n > 0);
4829 : 510 : putchar ('\n');
4830 : : }
4831 : 510 : }
4832 : :
4833 : : static void
4834 : 24 : print_bytes (size_t n, const unsigned char *bytes)
4835 : : {
4836 : 24 : while (n-- > 0)
4837 : : {
4838 : 384 : printf ("%02x", *bytes++);
4839 [ + + ]: 384 : if (n > 0)
4840 [ + + ]: 768 : printf (" ");
4841 : : }
4842 : 24 : }
4843 : :
4844 : : static int
4845 : 148 : get_indexed_addr (Dwarf_CU *cu, Dwarf_Word idx, Dwarf_Addr *addr)
4846 : : {
4847 [ - + ]: 148 : if (cu == NULL)
4848 : : return -1;
4849 : :
4850 : 148 : Elf_Data *debug_addr = cu->dbg->sectiondata[IDX_debug_addr];
4851 [ - + ]: 148 : if (debug_addr == NULL)
4852 : : return -1;
4853 : :
4854 : 148 : Dwarf_Off base = __libdw_cu_addr_base (cu);
4855 : 148 : Dwarf_Word off = idx * cu->address_size;
4856 [ - + ]: 148 : if (base > debug_addr->d_size
4857 [ - + ]: 148 : || off > debug_addr->d_size - base
4858 [ - + ]: 148 : || cu->address_size > debug_addr->d_size - base - off)
4859 : : return -1;
4860 : :
4861 : 148 : const unsigned char *addrp = debug_addr->d_buf + base + off;
4862 [ + + ]: 148 : if (cu->address_size == 4)
4863 [ - + ]: 12 : *addr = read_4ubyte_unaligned (cu->dbg, addrp);
4864 : : else
4865 [ - + ]: 136 : *addr = read_8ubyte_unaligned (cu->dbg, addrp);
4866 : :
4867 : : return 0;
4868 : : }
4869 : :
4870 : : static void
4871 : 784742 : print_ops (Dwfl_Module *dwflmod, Dwarf *dbg, int indent, int indentrest,
4872 : : unsigned int vers, unsigned int addrsize, unsigned int offset_size,
4873 : : struct Dwarf_CU *cu, Dwarf_Word len, const unsigned char *data)
4874 : : {
4875 [ + + ]: 784742 : const unsigned int ref_size = vers < 3 ? addrsize : offset_size;
4876 : :
4877 [ - + ]: 784742 : if (len == 0)
4878 : : {
4879 : 0 : printf ("%*s(empty)\n", indent, "");
4880 : 0 : return;
4881 : : }
4882 : :
4883 : : #define NEED(n) if (len < (Dwarf_Word) (n)) goto invalid
4884 : : #define CONSUME(n) NEED (n); else len -= (n)
4885 : :
4886 : : Dwarf_Word offset = 0;
4887 [ + + ]: 1931520 : while (len-- > 0)
4888 : 1146778 : {
4889 : 1146778 : uint_fast8_t op = *data++;
4890 : :
4891 [ + - ]: 1146778 : const char *op_name = dwarf_locexpr_opcode_string (op);
4892 [ - + ]: 1146778 : if (unlikely (op_name == NULL))
4893 : : {
4894 : 0 : static char buf[20];
4895 [ # # ]: 0 : if (op >= DW_OP_lo_user)
4896 : 0 : snprintf (buf, sizeof buf, "lo_user+%#x", op - DW_OP_lo_user);
4897 : : else
4898 : 0 : snprintf (buf, sizeof buf, "??? (%#x)", op);
4899 : : op_name = buf;
4900 : : }
4901 : :
4902 [ + - + + : 1146778 : switch (op)
+ + + + -
- + + - +
- - + + +
+ + - - -
- + + + ]
4903 : : {
4904 : 25942 : case DW_OP_addr:;
4905 : : /* Address operand. */
4906 : 25942 : Dwarf_Word addr;
4907 [ - + ]: 25942 : NEED (addrsize);
4908 [ + + ]: 25942 : if (addrsize == 4)
4909 [ + + ]: 108 : addr = read_4ubyte_unaligned (dbg, data);
4910 [ + - ]: 25834 : else if (addrsize == 8)
4911 [ + + ]: 25834 : addr = read_8ubyte_unaligned (dbg, data);
4912 : : else
4913 : 0 : goto invalid;
4914 : 25942 : data += addrsize;
4915 : 25942 : CONSUME (addrsize);
4916 : :
4917 : 25942 : printf ("%*s[%2" PRIuMAX "] %s ",
4918 : : indent, "", (uintmax_t) offset, op_name);
4919 : 25942 : print_dwarf_addr (dwflmod, 0, addr, addr);
4920 : 25942 : printf ("\n");
4921 : :
4922 : 25942 : offset += 1 + addrsize;
4923 : 25942 : break;
4924 : :
4925 : 0 : case DW_OP_call_ref:
4926 : : case DW_OP_GNU_variable_value:
4927 : : /* Offset operand. */
4928 [ # # ]: 0 : if (ref_size != 4 && ref_size != 8)
4929 : 0 : goto invalid; /* Cannot be used in CFA. */
4930 [ # # ]: 0 : NEED (ref_size);
4931 [ # # ]: 0 : if (ref_size == 4)
4932 [ # # ]: 0 : addr = read_4ubyte_unaligned (dbg, data);
4933 : : else
4934 [ # # ]: 0 : addr = read_8ubyte_unaligned (dbg, data);
4935 : 0 : data += ref_size;
4936 : 0 : CONSUME (ref_size);
4937 : : /* addr is a DIE offset, so format it as one. */
4938 : 0 : printf ("%*s[%2" PRIuMAX "] %s [%6" PRIxMAX "]\n",
4939 : : indent, "", (uintmax_t) offset,
4940 : : op_name, (uintmax_t) addr);
4941 : 0 : offset += 1 + ref_size;
4942 : 0 : break;
4943 : :
4944 : 27076 : case DW_OP_deref_size:
4945 : : case DW_OP_xderef_size:
4946 : : case DW_OP_pick:
4947 : : case DW_OP_const1u:
4948 : : // XXX value might be modified by relocation
4949 [ - + ]: 27076 : NEED (1);
4950 : 54152 : printf ("%*s[%2" PRIuMAX "] %s %" PRIu8 "\n",
4951 : : indent, "", (uintmax_t) offset,
4952 : 27076 : op_name, *((uint8_t *) data));
4953 : 27076 : ++data;
4954 : 27076 : --len;
4955 : 27076 : offset += 2;
4956 : 27076 : break;
4957 : :
4958 : 3242 : case DW_OP_const2u:
4959 [ - + ]: 3242 : NEED (2);
4960 : : // XXX value might be modified by relocation
4961 : 0 : printf ("%*s[%2" PRIuMAX "] %s %" PRIu16 "\n",
4962 : : indent, "", (uintmax_t) offset,
4963 [ - + ]: 3242 : op_name, read_2ubyte_unaligned (dbg, data));
4964 : 3242 : CONSUME (2);
4965 : 3242 : data += 2;
4966 : 3242 : offset += 3;
4967 : 3242 : break;
4968 : :
4969 : 2324 : case DW_OP_const4u:
4970 [ - + ]: 2324 : NEED (4);
4971 : : // XXX value might be modified by relocation
4972 : 2324 : printf ("%*s[%2" PRIuMAX "] %s %" PRIu32 "\n",
4973 : : indent, "", (uintmax_t) offset,
4974 [ - + ]: 2324 : op_name, read_4ubyte_unaligned (dbg, data));
4975 : 2324 : CONSUME (4);
4976 : 2324 : data += 4;
4977 : 2324 : offset += 5;
4978 : 2324 : break;
4979 : :
4980 : 112 : case DW_OP_const8u:
4981 [ - + ]: 112 : NEED (8);
4982 : : // XXX value might be modified by relocation
4983 : 112 : printf ("%*s[%2" PRIuMAX "] %s %" PRIu64 "\n",
4984 : : indent, "", (uintmax_t) offset,
4985 [ - + ]: 112 : op_name, (uint64_t) read_8ubyte_unaligned (dbg, data));
4986 : 112 : CONSUME (8);
4987 : 112 : data += 8;
4988 : 112 : offset += 9;
4989 : 112 : break;
4990 : :
4991 : 4910 : case DW_OP_const1s:
4992 [ - + ]: 4910 : NEED (1);
4993 : : // XXX value might be modified by relocation
4994 : 9820 : printf ("%*s[%2" PRIuMAX "] %s %" PRId8 "\n",
4995 : : indent, "", (uintmax_t) offset,
4996 : 4910 : op_name, *((int8_t *) data));
4997 : 4910 : ++data;
4998 : 4910 : --len;
4999 : 4910 : offset += 2;
5000 : 4910 : break;
5001 : :
5002 : 10 : case DW_OP_const2s:
5003 [ - + ]: 10 : NEED (2);
5004 : : // XXX value might be modified by relocation
5005 : 0 : printf ("%*s[%2" PRIuMAX "] %s %" PRId16 "\n",
5006 : : indent, "", (uintmax_t) offset,
5007 [ - + ]: 10 : op_name, read_2sbyte_unaligned (dbg, data));
5008 : 10 : CONSUME (2);
5009 : 10 : data += 2;
5010 : 10 : offset += 3;
5011 : 10 : break;
5012 : :
5013 : 0 : case DW_OP_const4s:
5014 [ # # ]: 0 : NEED (4);
5015 : : // XXX value might be modified by relocation
5016 : 0 : printf ("%*s[%2" PRIuMAX "] %s %" PRId32 "\n",
5017 : : indent, "", (uintmax_t) offset,
5018 [ # # ]: 0 : op_name, read_4sbyte_unaligned (dbg, data));
5019 : 0 : CONSUME (4);
5020 : 0 : data += 4;
5021 : 0 : offset += 5;
5022 : 0 : break;
5023 : :
5024 : 0 : case DW_OP_const8s:
5025 [ # # ]: 0 : NEED (8);
5026 : : // XXX value might be modified by relocation
5027 : 0 : printf ("%*s[%2" PRIuMAX "] %s %" PRId64 "\n",
5028 : : indent, "", (uintmax_t) offset,
5029 [ # # ]: 0 : op_name, read_8sbyte_unaligned (dbg, data));
5030 : 0 : CONSUME (8);
5031 : 0 : data += 8;
5032 : 0 : offset += 9;
5033 : 0 : break;
5034 : :
5035 : 27034 : case DW_OP_piece:
5036 : : case DW_OP_regx:
5037 : : case DW_OP_plus_uconst:
5038 : 27034 : case DW_OP_constu:;
5039 : 27034 : const unsigned char *start = data;
5040 : 27034 : uint64_t uleb;
5041 [ - + ]: 27034 : NEED (1);
5042 : 27034 : get_uleb128 (uleb, data, data + len);
5043 : 27034 : printf ("%*s[%2" PRIuMAX "] %s %" PRIu64 "\n",
5044 : : indent, "", (uintmax_t) offset, op_name, uleb);
5045 [ - + ]: 27034 : CONSUME (data - start);
5046 : 27034 : offset += 1 + (data - start);
5047 : 27034 : break;
5048 : :
5049 : 16 : case DW_OP_addrx:
5050 : : case DW_OP_GNU_addr_index:
5051 : : case DW_OP_constx:
5052 : 16 : case DW_OP_GNU_const_index:;
5053 : 16 : start = data;
5054 [ - + ]: 16 : NEED (1);
5055 : 16 : get_uleb128 (uleb, data, data + len);
5056 : 16 : printf ("%*s[%2" PRIuMAX "] %s [%" PRIu64 "] ",
5057 : : indent, "", (uintmax_t) offset, op_name, uleb);
5058 [ - + ]: 16 : CONSUME (data - start);
5059 : 16 : offset += 1 + (data - start);
5060 [ - + ]: 16 : if (get_indexed_addr (cu, uleb, &addr) != 0)
5061 : 0 : printf ("???\n");
5062 : : else
5063 : : {
5064 : 16 : print_dwarf_addr (dwflmod, 0, addr, addr);
5065 : 16 : printf ("\n");
5066 : : }
5067 : : break;
5068 : :
5069 : 0 : case DW_OP_bit_piece:
5070 : 0 : start = data;
5071 : 0 : uint64_t uleb2;
5072 [ # # ]: 0 : NEED (1);
5073 : 0 : get_uleb128 (uleb, data, data + len);
5074 : 0 : NEED (1);
5075 : 0 : get_uleb128 (uleb2, data, data + len);
5076 : 0 : printf ("%*s[%2" PRIuMAX "] %s %" PRIu64 ", %" PRIu64 "\n",
5077 : : indent, "", (uintmax_t) offset, op_name, uleb, uleb2);
5078 [ # # ]: 0 : CONSUME (data - start);
5079 : 0 : offset += 1 + (data - start);
5080 : 0 : break;
5081 : :
5082 : 201282 : case DW_OP_fbreg:
5083 : : case DW_OP_breg0 ... DW_OP_breg31:
5084 : : case DW_OP_consts:
5085 : 201282 : start = data;
5086 : 201282 : int64_t sleb;
5087 [ - + ]: 201282 : NEED (1);
5088 : 201282 : get_sleb128 (sleb, data, data + len);
5089 : 201282 : printf ("%*s[%2" PRIuMAX "] %s %" PRId64 "\n",
5090 : : indent, "", (uintmax_t) offset, op_name, sleb);
5091 [ - + ]: 201282 : CONSUME (data - start);
5092 : 201282 : offset += 1 + (data - start);
5093 : 201282 : break;
5094 : :
5095 : 0 : case DW_OP_bregx:
5096 : 0 : start = data;
5097 [ # # ]: 0 : NEED (1);
5098 : 0 : get_uleb128 (uleb, data, data + len);
5099 : 0 : NEED (1);
5100 : 0 : get_sleb128 (sleb, data, data + len);
5101 : 0 : printf ("%*s[%2" PRIuMAX "] %s %" PRIu64 " %" PRId64 "\n",
5102 : : indent, "", (uintmax_t) offset, op_name, uleb, sleb);
5103 [ # # ]: 0 : CONSUME (data - start);
5104 : 0 : offset += 1 + (data - start);
5105 : 0 : break;
5106 : :
5107 : 0 : case DW_OP_call2:
5108 [ # # ]: 0 : NEED (2);
5109 : 0 : printf ("%*s[%2" PRIuMAX "] %s [%6" PRIx16 "]\n",
5110 : : indent, "", (uintmax_t) offset, op_name,
5111 [ # # ]: 0 : read_2ubyte_unaligned (dbg, data));
5112 : 0 : CONSUME (2);
5113 : 0 : data += 2;
5114 : 0 : offset += 3;
5115 : 0 : break;
5116 : :
5117 : 8 : case DW_OP_call4:
5118 [ - + ]: 8 : NEED (4);
5119 : 8 : printf ("%*s[%2" PRIuMAX "] %s [%6" PRIx32 "]\n",
5120 : : indent, "", (uintmax_t) offset, op_name,
5121 [ - + ]: 8 : read_4ubyte_unaligned (dbg, data));
5122 : 8 : CONSUME (4);
5123 : 8 : data += 4;
5124 : 8 : offset += 5;
5125 : 8 : break;
5126 : :
5127 : 1128 : case DW_OP_skip:
5128 : : case DW_OP_bra:
5129 [ - + ]: 1128 : NEED (2);
5130 : 2256 : printf ("%*s[%2" PRIuMAX "] %s %" PRIuMAX "\n",
5131 : : indent, "", (uintmax_t) offset, op_name,
5132 [ - + ]: 1128 : (uintmax_t) (offset + read_2sbyte_unaligned (dbg, data) + 3));
5133 : 1128 : CONSUME (2);
5134 : 1128 : data += 2;
5135 : 1128 : offset += 3;
5136 : 1128 : break;
5137 : :
5138 : 510 : case DW_OP_implicit_value:
5139 : 510 : start = data;
5140 [ - + ]: 510 : NEED (1);
5141 : 510 : get_uleb128 (uleb, data, data + len);
5142 : 510 : printf ("%*s[%2" PRIuMAX "] %s: ",
5143 : : indent, "", (uintmax_t) offset, op_name);
5144 [ - + ]: 510 : NEED (uleb);
5145 : 510 : print_block (uleb, data);
5146 : 510 : data += uleb;
5147 [ - + ]: 510 : CONSUME (data - start);
5148 : 510 : offset += 1 + (data - start);
5149 : 510 : break;
5150 : :
5151 : 5024 : case DW_OP_implicit_pointer:
5152 : : case DW_OP_GNU_implicit_pointer:
5153 : : /* DIE offset operand. */
5154 : 5024 : start = data;
5155 [ - + ]: 5024 : NEED (ref_size);
5156 [ - + ]: 5024 : if (ref_size != 4 && ref_size != 8)
5157 : 0 : goto invalid; /* Cannot be used in CFA. */
5158 [ + - ]: 5024 : if (ref_size == 4)
5159 [ - + ]: 5024 : addr = read_4ubyte_unaligned (dbg, data);
5160 : : else
5161 [ # # ]: 0 : addr = read_8ubyte_unaligned (dbg, data);
5162 : 5024 : data += ref_size;
5163 : : /* Byte offset operand. */
5164 [ - + ]: 5024 : NEED (1);
5165 : 5024 : get_sleb128 (sleb, data, data + len);
5166 : :
5167 : 5024 : printf ("%*s[%2" PRIuMAX "] %s [%6" PRIxMAX "] %+" PRId64 "\n",
5168 : : indent, "", (intmax_t) offset,
5169 : : op_name, (uintmax_t) addr, sleb);
5170 [ - + ]: 5024 : CONSUME (data - start);
5171 : 5024 : offset += 1 + (data - start);
5172 : 5024 : break;
5173 : :
5174 : 63582 : case DW_OP_entry_value:
5175 : : case DW_OP_GNU_entry_value:
5176 : : /* Size plus expression block. */
5177 : 63582 : start = data;
5178 [ - + ]: 63582 : NEED (1);
5179 : 63582 : get_uleb128 (uleb, data, data + len);
5180 : 63582 : printf ("%*s[%2" PRIuMAX "] %s:\n",
5181 : : indent, "", (uintmax_t) offset, op_name);
5182 [ - + ]: 63582 : NEED (uleb);
5183 : 63582 : print_ops (dwflmod, dbg, indent + 5, indent + 5, vers,
5184 : : addrsize, offset_size, cu, uleb, data);
5185 : 63582 : data += uleb;
5186 [ - + ]: 63582 : CONSUME (data - start);
5187 : 63582 : offset += 1 + (data - start);
5188 : 63582 : break;
5189 : :
5190 : 0 : case DW_OP_const_type:
5191 : : case DW_OP_GNU_const_type:
5192 : : /* uleb128 CU relative DW_TAG_base_type DIE offset, 1-byte
5193 : : unsigned size plus block. */
5194 : 0 : start = data;
5195 [ # # ]: 0 : NEED (1);
5196 : 0 : get_uleb128 (uleb, data, data + len);
5197 [ # # # # ]: 0 : if (! print_unresolved_addresses && cu != NULL)
5198 : 0 : uleb += cu->start;
5199 : 0 : NEED (1);
5200 : 0 : uint8_t usize = *(uint8_t *) data++;
5201 [ # # ]: 0 : NEED (usize);
5202 : 0 : printf ("%*s[%2" PRIuMAX "] %s [%6" PRIxMAX "] ",
5203 : : indent, "", (uintmax_t) offset, op_name, uleb);
5204 : 0 : print_block (usize, data);
5205 : 0 : data += usize;
5206 [ # # ]: 0 : CONSUME (data - start);
5207 : 0 : offset += 1 + (data - start);
5208 : 0 : break;
5209 : :
5210 : 0 : case DW_OP_regval_type:
5211 : : case DW_OP_GNU_regval_type:
5212 : : /* uleb128 register number, uleb128 CU relative
5213 : : DW_TAG_base_type DIE offset. */
5214 : 0 : start = data;
5215 [ # # ]: 0 : NEED (1);
5216 : 0 : get_uleb128 (uleb, data, data + len);
5217 : 0 : NEED (1);
5218 : 0 : get_uleb128 (uleb2, data, data + len);
5219 [ # # # # ]: 0 : if (! print_unresolved_addresses && cu != NULL)
5220 : 0 : uleb2 += cu->start;
5221 : 0 : printf ("%*s[%2" PRIuMAX "] %s %" PRIu64 " [%6" PRIx64 "]\n",
5222 : : indent, "", (uintmax_t) offset, op_name, uleb, uleb2);
5223 [ # # ]: 0 : CONSUME (data - start);
5224 : 0 : offset += 1 + (data - start);
5225 : 0 : break;
5226 : :
5227 : 0 : case DW_OP_deref_type:
5228 : : case DW_OP_GNU_deref_type:
5229 : : /* 1-byte unsigned size of value, uleb128 CU relative
5230 : : DW_TAG_base_type DIE offset. */
5231 : 0 : start = data;
5232 [ # # ]: 0 : NEED (1);
5233 : 0 : usize = *(uint8_t *) data++;
5234 : 0 : NEED (1);
5235 : 0 : get_uleb128 (uleb, data, data + len);
5236 [ # # # # ]: 0 : if (! print_unresolved_addresses && cu != NULL)
5237 : 0 : uleb += cu->start;
5238 : 0 : printf ("%*s[%2" PRIuMAX "] %s %" PRIu8 " [%6" PRIxMAX "]\n",
5239 : : indent, "", (uintmax_t) offset,
5240 : : op_name, usize, uleb);
5241 [ # # ]: 0 : CONSUME (data - start);
5242 : 0 : offset += 1 + (data - start);
5243 : 0 : break;
5244 : :
5245 : 0 : case DW_OP_xderef_type:
5246 : : /* 1-byte unsigned size of value, uleb128 base_type DIE offset. */
5247 : 0 : start = data;
5248 [ # # ]: 0 : NEED (1);
5249 : 0 : usize = *(uint8_t *) data++;
5250 : 0 : NEED (1);
5251 : 0 : get_uleb128 (uleb, data, data + len);
5252 : 0 : printf ("%*s[%4" PRIuMAX "] %s %" PRIu8 " [%6" PRIxMAX "]\n",
5253 : : indent, "", (uintmax_t) offset,
5254 : : op_name, usize, uleb);
5255 [ # # ]: 0 : CONSUME (data - start);
5256 : 0 : offset += 1 + (data - start);
5257 : 0 : break;
5258 : :
5259 : 810 : case DW_OP_convert:
5260 : : case DW_OP_GNU_convert:
5261 : : case DW_OP_reinterpret:
5262 : : case DW_OP_GNU_reinterpret:
5263 : : /* uleb128 CU relative offset to DW_TAG_base_type, or zero
5264 : : for conversion to untyped. */
5265 : 810 : start = data;
5266 [ - + ]: 810 : NEED (1);
5267 : 810 : get_uleb128 (uleb, data, data + len);
5268 [ + + + - : 810 : if (uleb != 0 && ! print_unresolved_addresses && cu != NULL)
+ - ]
5269 : 540 : uleb += cu->start;
5270 : 810 : printf ("%*s[%2" PRIuMAX "] %s [%6" PRIxMAX "]\n",
5271 : : indent, "", (uintmax_t) offset, op_name, uleb);
5272 [ - + ]: 810 : CONSUME (data - start);
5273 : 810 : offset += 1 + (data - start);
5274 : 810 : break;
5275 : :
5276 : 8 : case DW_OP_GNU_parameter_ref:
5277 : : /* 4 byte CU relative reference to the abstract optimized away
5278 : : DW_TAG_formal_parameter. */
5279 [ - + ]: 8 : NEED (4);
5280 [ - + ]: 8 : uintmax_t param_off = (uintmax_t) read_4ubyte_unaligned (dbg, data);
5281 [ + - + - ]: 8 : if (! print_unresolved_addresses && cu != NULL)
5282 : 8 : param_off += cu->start;
5283 : 8 : printf ("%*s[%2" PRIuMAX "] %s [%6" PRIxMAX "]\n",
5284 : : indent, "", (uintmax_t) offset, op_name, param_off);
5285 : 8 : CONSUME (4);
5286 : 8 : data += 4;
5287 : 8 : offset += 5;
5288 : 8 : break;
5289 : :
5290 : : default:
5291 : : /* No Operand. */
5292 : 783760 : printf ("%*s[%2" PRIuMAX "] %s\n",
5293 : : indent, "", (uintmax_t) offset, op_name);
5294 : 783760 : ++offset;
5295 : 783760 : break;
5296 : : }
5297 : :
5298 : 1146778 : indent = indentrest;
5299 : 1146778 : continue;
5300 : :
5301 : 0 : invalid:
5302 : 0 : printf (_("%*s[%2" PRIuMAX "] %s <TRUNCATED>\n"),
5303 : : indent, "", (uintmax_t) offset, op_name);
5304 : : break;
5305 : : }
5306 : : }
5307 : :
5308 : :
5309 : : /* Turn the addresses into file offsets by using the phdrs. */
5310 : : static void
5311 : 2 : find_offsets(Elf *elf, GElf_Addr main_bias, size_t n,
5312 : : GElf_Addr addrs[n], GElf_Off offs[n])
5313 : : {
5314 : 2 : size_t unsolved = n;
5315 [ + + ]: 18 : for (size_t i = 0; i < phnum; ++i) {
5316 : 16 : GElf_Phdr phdr_mem;
5317 : 16 : GElf_Phdr *phdr = gelf_getphdr(elf, i, &phdr_mem);
5318 [ + - + + : 16 : if (phdr != NULL && phdr->p_type == PT_LOAD && phdr->p_memsz > 0)
+ - ]
5319 [ + + ]: 46 : for (size_t j = 0; j < n; ++j)
5320 [ + + + + ]: 44 : if (offs[j] == 0 && addrs[j] >= phdr->p_vaddr + main_bias &&
5321 [ + - ]: 22 : addrs[j] - (phdr->p_vaddr + main_bias) < phdr->p_filesz) {
5322 : 22 : offs[j] = addrs[j] - (phdr->p_vaddr + main_bias) + phdr->p_offset;
5323 [ + + ]: 22 : if (--unsolved == 0)
5324 : : break;
5325 : : }
5326 : : }
5327 : 2 : }
5328 : :
5329 : : /* The dynamic segment (type PT_DYNAMIC), contains the .dynamic section.
5330 : : And .dynamic section contains an array of the dynamic structures.
5331 : : We use the array to get:
5332 : : DT_STRTAB: the address of the string table
5333 : : DT_SYMTAB: the address of the symbol table
5334 : : DT_STRSZ: the size, in bytes, of the string table
5335 : : ... */
5336 : : static void
5337 : 2 : get_dynscn_addrs(Elf *elf, GElf_Phdr *phdr, GElf_Addr addrs[i_max])
5338 : : {
5339 : 2 : Elf_Data *data = elf_getdata_rawchunk(
5340 : 2 : elf, phdr->p_offset, phdr->p_filesz, ELF_T_DYN);
5341 : :
5342 : 2 : int dyn_idx = 0;
5343 : 44 : for (;; ++dyn_idx) {
5344 : 46 : GElf_Dyn dyn_mem;
5345 : 46 : GElf_Dyn *dyn = gelf_getdyn(data, dyn_idx, &dyn_mem);
5346 : : /* DT_NULL Marks end of dynamic section. */
5347 [ + - + + ]: 46 : if (dyn == NULL || dyn->d_tag == DT_NULL)
5348 : : break;
5349 : :
5350 [ + - + + : 44 : switch (dyn->d_tag) {
+ + + + +
+ - + ]
5351 : 2 : case DT_SYMTAB:
5352 : 2 : addrs[i_symtab] = dyn->d_un.d_ptr;
5353 : 2 : break;
5354 : :
5355 : 0 : case DT_HASH:
5356 : 0 : addrs[i_hash] = dyn->d_un.d_ptr;
5357 : 0 : break;
5358 : :
5359 : 2 : case DT_GNU_HASH:
5360 : 2 : addrs[i_gnu_hash] = dyn->d_un.d_ptr;
5361 : 2 : break;
5362 : :
5363 : 2 : case DT_STRTAB:
5364 : 2 : addrs[i_strtab] = dyn->d_un.d_ptr;
5365 : 2 : break;
5366 : :
5367 : 2 : case DT_VERSYM:
5368 : 2 : addrs[i_versym] = dyn->d_un.d_ptr;
5369 : 2 : break;
5370 : :
5371 : 2 : case DT_VERDEF:
5372 : 2 : addrs[i_verdef] = dyn->d_un.d_ptr;
5373 : 2 : break;
5374 : :
5375 : 2 : case DT_VERDEFNUM:
5376 : 2 : addrs[i_verdefnum] = dyn->d_un.d_val;
5377 : 2 : break;
5378 : :
5379 : 2 : case DT_VERNEED:
5380 : 2 : addrs[i_verneed] = dyn->d_un.d_ptr;
5381 : 2 : break;
5382 : :
5383 : 2 : case DT_VERNEEDNUM:
5384 : 2 : addrs[i_verneednum] = dyn->d_un.d_val;
5385 : 2 : break;
5386 : :
5387 : 2 : case DT_STRSZ:
5388 : 2 : addrs[i_strsz] = dyn->d_un.d_val;
5389 : 2 : break;
5390 : :
5391 : 0 : case DT_SYMTAB_SHNDX:
5392 : 0 : addrs[i_symtab_shndx] = dyn->d_un.d_ptr;
5393 : 0 : break;
5394 : : }
5395 : : }
5396 : 2 : }
5397 : :
5398 : :
5399 : : /* Use dynamic segment to get data for the string table section. */
5400 : : static Elf_Data *
5401 : 2 : get_dynscn_strtab(Elf *elf, GElf_Phdr *phdr)
5402 : : {
5403 : 2 : Elf_Data *strtab_data;
5404 : 2 : GElf_Addr addrs[i_max] = {0,};
5405 : 2 : GElf_Off offs[i_max] = {0,};
5406 : 2 : get_dynscn_addrs(elf, phdr, addrs);
5407 : 2 : find_offsets(elf, 0, i_max, addrs, offs);
5408 : 4 : strtab_data = elf_getdata_rawchunk(
5409 : 2 : elf, offs[i_strtab], addrs[i_strsz], ELF_T_BYTE);
5410 : 2 : return strtab_data;
5411 : : }
5412 : :
5413 : :
5414 : : struct listptr
5415 : : {
5416 : : Dwarf_Off offset:(64 - 3);
5417 : : bool addr64:1;
5418 : : bool dwarf64:1;
5419 : : bool warned:1;
5420 : : struct Dwarf_CU *cu;
5421 : : unsigned int attr;
5422 : : };
5423 : :
5424 : : #define listptr_offset_size(p) ((p)->dwarf64 ? 8 : 4)
5425 : : #define listptr_address_size(p) ((p)->addr64 ? 8 : 4)
5426 : :
5427 : : static Dwarf_Addr
5428 : 21356 : cudie_base (Dwarf_Die *cudie)
5429 : : {
5430 : 21356 : Dwarf_Addr base;
5431 : : /* Find the base address of the compilation unit. It will normally
5432 : : be specified by DW_AT_low_pc. In DWARF-3 draft 4, the base
5433 : : address could be overridden by DW_AT_entry_pc. It's been
5434 : : removed, but GCC emits DW_AT_entry_pc and not DW_AT_lowpc for
5435 : : compilation units with discontinuous ranges. */
5436 [ - + ]: 21356 : if (unlikely (dwarf_lowpc (cudie, &base) != 0))
5437 : : {
5438 : 0 : Dwarf_Attribute attr_mem;
5439 [ # # ]: 0 : if (dwarf_formaddr (dwarf_attr (cudie, DW_AT_entry_pc, &attr_mem),
5440 : : &base) != 0)
5441 : 0 : base = 0;
5442 : : }
5443 : 21356 : return base;
5444 : : }
5445 : :
5446 : : static Dwarf_Addr
5447 : 21356 : listptr_base (struct listptr *p)
5448 : : {
5449 : 21356 : Dwarf_Die cu = CUDIE (p->cu);
5450 : 21356 : return cudie_base (&cu);
5451 : : }
5452 : :
5453 : : /* To store the name used in compare_listptr */
5454 : : static const char *sort_listptr_name;
5455 : :
5456 : : static int
5457 : 1731032 : compare_listptr (const void *a, const void *b)
5458 : : {
5459 : 1731032 : const char *name = sort_listptr_name;
5460 : 1731032 : struct listptr *p1 = (void *) a;
5461 : 1731032 : struct listptr *p2 = (void *) b;
5462 : :
5463 [ + + ]: 1731032 : if (p1->offset < p2->offset)
5464 : : return -1;
5465 [ + + ]: 166406 : if (p1->offset > p2->offset)
5466 : : return 1;
5467 : :
5468 [ + - - + ]: 8104 : if (!p1->warned && !p2->warned)
5469 : : {
5470 [ - + ]: 8104 : if (p1->addr64 != p2->addr64)
5471 : : {
5472 : 0 : p1->warned = p2->warned = true;
5473 : 8104 : error (0, 0,
5474 : 0 : _("%s %#" PRIx64 " used with different address sizes"),
5475 : : name, (uint64_t) p1->offset);
5476 : : }
5477 [ - + ]: 8104 : if (p1->dwarf64 != p2->dwarf64)
5478 : : {
5479 : 0 : p1->warned = p2->warned = true;
5480 : 8104 : error (0, 0,
5481 : 0 : _("%s %#" PRIx64 " used with different offset sizes"),
5482 : 0 : name, (uint64_t) p1->offset);
5483 : : }
5484 [ - + ]: 8104 : if (listptr_base (p1) != listptr_base (p2))
5485 : : {
5486 : 0 : p1->warned = p2->warned = true;
5487 : 8104 : error (0, 0,
5488 : 0 : _("%s %#" PRIx64 " used with different base addresses"),
5489 : 0 : name, (uint64_t) p1->offset);
5490 : : }
5491 [ + - ]: 8104 : if (p1->attr != p2 ->attr)
5492 : : {
5493 : 0 : p1->warned = p2->warned = true;
5494 : 0 : error (0, 0,
5495 : 0 : _("%s %#" PRIx64
5496 : : " used with different attribute %s and %s"),
5497 : 0 : name, (uint64_t) p1->offset, dwarf_attr_name (p1->attr),
5498 : : dwarf_attr_name (p2->attr));
5499 : : }
5500 : : }
5501 : :
5502 : : return 0;
5503 : : }
5504 : :
5505 : : struct listptr_table
5506 : : {
5507 : : size_t n;
5508 : : size_t alloc;
5509 : : struct listptr *table;
5510 : : };
5511 : :
5512 : : static struct listptr_table known_locsptr;
5513 : : static struct listptr_table known_loclistsptr;
5514 : : static struct listptr_table known_rangelistptr;
5515 : : static struct listptr_table known_rnglistptr;
5516 : : static struct listptr_table known_addrbases;
5517 : : static struct listptr_table known_stroffbases;
5518 : :
5519 : : static void
5520 : 502 : reset_listptr (struct listptr_table *table)
5521 : : {
5522 : 502 : free (table->table);
5523 : 502 : table->table = NULL;
5524 : 502 : table->n = table->alloc = 0;
5525 : : }
5526 : :
5527 : : /* Returns false if offset doesn't fit. See struct listptr. */
5528 : : static bool
5529 : 250974 : notice_listptr (enum section_e section, struct listptr_table *table,
5530 : : uint_fast8_t address_size, uint_fast8_t offset_size,
5531 : : struct Dwarf_CU *cu, Dwarf_Off offset, unsigned int attr)
5532 : : {
5533 [ + + ]: 250974 : if (print_debug_sections & section)
5534 : : {
5535 [ + + ]: 250722 : if (table->n == table->alloc)
5536 : : {
5537 [ + + ]: 408 : if (table->alloc == 0)
5538 : 174 : table->alloc = 128;
5539 : : else
5540 : 234 : table->alloc *= 2;
5541 : 408 : table->table = xrealloc (table->table,
5542 : 408 : table->alloc * sizeof table->table[0]);
5543 : : }
5544 : :
5545 : 250722 : struct listptr *p = &table->table[table->n++];
5546 : :
5547 : 250722 : *p = (struct listptr)
5548 : : {
5549 : 250722 : .addr64 = address_size == 8,
5550 : 250722 : .dwarf64 = offset_size == 8,
5551 : : .offset = offset,
5552 : : .cu = cu,
5553 : : .attr = attr
5554 : : };
5555 : :
5556 [ + - ]: 250722 : if (p->offset != offset)
5557 : : {
5558 : 0 : table->n--;
5559 : 0 : return false;
5560 : : }
5561 : : }
5562 : : return true;
5563 : : }
5564 : :
5565 : : static void
5566 : 182 : sort_listptr (struct listptr_table *table, const char *name)
5567 : : {
5568 [ + + ]: 182 : if (table->n > 0)
5569 : : {
5570 : 174 : sort_listptr_name = name;
5571 : 174 : qsort (table->table, table->n, sizeof table->table[0],
5572 : : &compare_listptr);
5573 : : }
5574 : 182 : }
5575 : :
5576 : : static bool
5577 : 208 : skip_listptr_hole (struct listptr_table *table, size_t *idxp,
5578 : : uint_fast8_t *address_sizep, uint_fast8_t *offset_sizep,
5579 : : Dwarf_Addr *base, struct Dwarf_CU **cu, ptrdiff_t offset,
5580 : : unsigned char **readp, unsigned char *endp,
5581 : : unsigned int *attr)
5582 : : {
5583 [ - + ]: 208 : if (table->n == 0)
5584 : : return false;
5585 : :
5586 [ + - + + ]: 336 : while (*idxp < table->n && table->table[*idxp].offset < (Dwarf_Off) offset)
5587 : 128 : ++*idxp;
5588 : :
5589 : 208 : struct listptr *p = &table->table[*idxp];
5590 : :
5591 [ + - ]: 208 : if (*idxp == table->n
5592 [ - + ]: 208 : || p->offset >= (Dwarf_Off) (endp - *readp + offset))
5593 : : {
5594 : 0 : *readp = endp;
5595 : 0 : printf (_(" [%6tx] <UNUSED GARBAGE IN REST OF SECTION>\n"),
5596 : : offset);
5597 : 0 : return true;
5598 : : }
5599 : :
5600 [ - + ]: 208 : if (p->offset != (Dwarf_Off) offset)
5601 : : {
5602 : 0 : *readp += p->offset - offset;
5603 : 0 : printf (_(" [%6tx] <UNUSED GARBAGE> ... %" PRIu64 " bytes ...\n"),
5604 : : offset, (Dwarf_Off) p->offset - offset);
5605 : 0 : return true;
5606 : : }
5607 : :
5608 [ + - ]: 208 : if (address_sizep != NULL)
5609 [ + + ]: 224 : *address_sizep = listptr_address_size (p);
5610 [ + + ]: 208 : if (offset_sizep != NULL)
5611 [ + - ]: 296 : *offset_sizep = listptr_offset_size (p);
5612 [ + - ]: 208 : if (base != NULL)
5613 : 208 : *base = listptr_base (p);
5614 [ + - ]: 208 : if (cu != NULL)
5615 : 208 : *cu = p->cu;
5616 [ + + ]: 208 : if (attr != NULL)
5617 : 148 : *attr = p->attr;
5618 : :
5619 : : return false;
5620 : : }
5621 : :
5622 : : static Dwarf_Off
5623 : 107692 : next_listptr_offset (struct listptr_table *table, size_t *idxp, Dwarf_Off off)
5624 : : {
5625 : : /* Note that multiple attributes could in theory point to the same loclist
5626 : : offset, so make sure we pick one that is bigger than the current one.
5627 : : The table is sorted on offset. */
5628 [ + - ]: 107692 : if (*idxp < table->n)
5629 : : {
5630 [ + - ]: 214168 : while (++*idxp < table->n)
5631 : : {
5632 : 214168 : Dwarf_Off next = table->table[*idxp].offset;
5633 [ + + ]: 214168 : if (next > off)
5634 : 107692 : return next;
5635 : : }
5636 : : }
5637 : : return 0;
5638 : : }
5639 : :
5640 : : /* Returns the listptr associated with the given index, or NULL. */
5641 : : static struct listptr *
5642 : 1469020 : get_listptr (struct listptr_table *table, size_t idx)
5643 : : {
5644 : 1469020 : if (idx >= table->n)
5645 : : return NULL;
5646 : 1468768 : return &table->table[idx];
5647 : : }
5648 : :
5649 : : /* Returns the next index, base address and CU associated with the
5650 : : list unit offsets. If there is none false is returned, otherwise
5651 : : true. Assumes the table has been sorted. */
5652 : : static bool
5653 : 4940 : listptr_cu (struct listptr_table *table, size_t *idxp,
5654 : : Dwarf_Off start, Dwarf_Off end,
5655 : : Dwarf_Addr *base, struct Dwarf_CU **cu)
5656 : : {
5657 : 4940 : while (*idxp < table->n
5658 [ + - + + ]: 38444 : && table->table[*idxp].offset < start)
5659 : 33504 : ++*idxp;
5660 : :
5661 [ + - ]: 4940 : if (*idxp < table->n
5662 [ + - ]: 4940 : && table->table[*idxp].offset >= start
5663 [ + - ]: 4940 : && table->table[*idxp].offset < end)
5664 : : {
5665 : 4940 : struct listptr *p = &table->table[*idxp];
5666 : 4940 : *base = listptr_base (p);
5667 : 4940 : *cu = p->cu;
5668 : 4940 : return true;
5669 : : }
5670 : :
5671 : : return false;
5672 : : }
5673 : :
5674 : : /* Returns the next index with the current CU for the given attribute.
5675 : : If there is none false is returned, otherwise true. Assumes the
5676 : : table has been sorted. */
5677 : : static bool
5678 : 734316 : listptr_attr (struct listptr_table *table, size_t idxp,
5679 : : Dwarf_Off offset, unsigned int attr)
5680 : : {
5681 : 1468984 : struct listptr *listptr;
5682 : 1468984 : do
5683 : : {
5684 [ + + ]: 1468984 : listptr = get_listptr (table, idxp);
5685 [ + - ]: 1468742 : if (listptr == NULL)
5686 : : return false;
5687 : :
5688 [ + + + + ]: 1468742 : if (listptr->offset == offset && listptr->attr == attr)
5689 : : return true;
5690 : :
5691 : 1361050 : idxp++;
5692 : : }
5693 [ + + ]: 1361050 : while (listptr->offset <= offset);
5694 : :
5695 : : return false;
5696 : : }
5697 : :
5698 : : static void
5699 : 88 : print_debug_abbrev_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
5700 : : Ebl *ebl, GElf_Ehdr *ehdr __attribute__ ((unused)),
5701 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
5702 : : {
5703 : 88 : Elf_Data *elf_data = get_debug_elf_data (dbg, ebl, IDX_debug_abbrev, scn);
5704 [ + - ]: 88 : if (elf_data == NULL)
5705 : : return;
5706 : :
5707 : 88 : const size_t sh_size = elf_data->d_size;
5708 : :
5709 : 88 : printf (_("\nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"
5710 : : " [ Code]\n"),
5711 : : elf_ndxscn (scn), section_name (ebl, shdr),
5712 : 88 : (uint64_t) shdr->sh_offset);
5713 : :
5714 : 88 : Dwarf_Off offset = 0;
5715 [ + + ]: 3622 : while (offset < sh_size)
5716 : : {
5717 : 3534 : printf (_("\nAbbreviation section at offset %" PRIu64 ":\n"),
5718 : : offset);
5719 : :
5720 : 325474 : while (1)
5721 : 160970 : {
5722 : 164504 : size_t length;
5723 : 164504 : Dwarf_Abbrev abbrev;
5724 : :
5725 : 164504 : int res = dwarf_offabbrev (dbg, offset, &length, &abbrev);
5726 [ + + ]: 164504 : if (res != 0)
5727 : : {
5728 [ - + ]: 3534 : if (unlikely (res < 0))
5729 : : {
5730 : 0 : printf (_("\
5731 : : *** error while reading abbreviation: %s\n"),
5732 : : dwarf_errmsg (-1));
5733 : 0 : return;
5734 : : }
5735 : :
5736 : : /* This is the NUL byte at the end of the section. */
5737 : 3534 : ++offset;
5738 : 3534 : break;
5739 : : }
5740 : :
5741 : : /* We know these calls can never fail. */
5742 : 160970 : unsigned int code = dwarf_getabbrevcode (&abbrev);
5743 : 160970 : unsigned int tag = dwarf_getabbrevtag (&abbrev);
5744 : 160970 : int has_children = dwarf_abbrevhaschildren (&abbrev);
5745 : :
5746 [ + + ]: 160970 : printf (_(" [%5u] offset: %" PRId64
5747 : : ", children: %s, tag: %s\n"),
5748 : : code, (int64_t) offset,
5749 : : has_children ? yes_str : no_str,
5750 : : dwarf_tag_name (tag));
5751 : :
5752 : 160970 : size_t cnt = 0;
5753 : 160970 : unsigned int name;
5754 : 160970 : unsigned int form;
5755 : 160970 : Dwarf_Sword data;
5756 : 160970 : Dwarf_Off enoffset;
5757 : 160970 : while (dwarf_getabbrevattr_data (&abbrev, cnt, &name, &form,
5758 [ + + ]: 920466 : &data, &enoffset) == 0)
5759 : : {
5760 : 759496 : printf (" attr: %s, form: %s",
5761 : : dwarf_attr_name (name), dwarf_form_name (form));
5762 [ + + ]: 759496 : if (form == DW_FORM_implicit_const)
5763 : 54576 : printf (" (%" PRId64 ")", data);
5764 : 759496 : printf (", offset: %#" PRIx64 "\n", (uint64_t) enoffset);
5765 : 759496 : ++cnt;
5766 : : }
5767 : :
5768 : 160970 : offset += length;
5769 : : }
5770 : : }
5771 : : }
5772 : :
5773 : :
5774 : : static void
5775 : 8 : print_debug_addr_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
5776 : : Ebl *ebl, GElf_Ehdr *ehdr,
5777 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
5778 : : {
5779 : 8 : Elf_Data *data = get_debug_elf_data (dbg, ebl, IDX_debug_addr, scn);
5780 [ + - ]: 8 : if (data == NULL)
5781 : : return;
5782 : :
5783 : 8 : printf (_("\
5784 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
5785 : : elf_ndxscn (scn), section_name (ebl, shdr),
5786 : 8 : (uint64_t) shdr->sh_offset);
5787 : :
5788 [ + - ]: 8 : if (shdr->sh_size == 0)
5789 : : return;
5790 : :
5791 : 8 : size_t idx = 0;
5792 : 8 : sort_listptr (&known_addrbases, "addr_base");
5793 : :
5794 : 8 : const unsigned char *start = (const unsigned char *) data->d_buf;
5795 : 8 : const unsigned char *readp = start;
5796 : 8 : const unsigned char *readendp = ((const unsigned char *) data->d_buf
5797 : 8 : + data->d_size);
5798 : :
5799 : 8 : while (readp < readendp)
5800 : : {
5801 : : /* We cannot really know whether or not there is an header. The
5802 : : DebugFission extension to DWARF4 doesn't add one. The DWARF5
5803 : : .debug_addr variant does. Whether or not we have an header,
5804 : : DW_AT_[GNU_]addr_base points at "index 0". So if the current
5805 : : offset equals the CU addr_base then we can just start
5806 : : printing addresses. If there is no CU with an exact match
5807 : : then we'll try to parse the header first. */
5808 : 16 : Dwarf_Off off = (Dwarf_Off) (readp
5809 : 16 : - (const unsigned char *) data->d_buf);
5810 : :
5811 : 16 : printf ("Table at offset %" PRIx64 " ", off);
5812 : :
5813 [ + - ]: 16 : struct listptr *listptr = get_listptr (&known_addrbases, idx++);
5814 : 16 : const unsigned char *next_unitp;
5815 : :
5816 : 16 : uint64_t unit_length;
5817 : 16 : uint16_t version;
5818 : 16 : uint8_t address_size;
5819 : 16 : uint8_t segment_size;
5820 [ - + ]: 16 : if (listptr == NULL)
5821 : : {
5822 : 0 : error (0, 0, "Warning: No CU references .debug_addr after %" PRIx64,
5823 : : off);
5824 : :
5825 : : /* We will have to assume it is just addresses to the end... */
5826 [ # # ]: 0 : address_size = ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 4 : 8;
5827 : 0 : next_unitp = readendp;
5828 : 0 : printf ("Unknown CU:\n");
5829 : : }
5830 : : else
5831 : : {
5832 : 16 : Dwarf_Die cudie;
5833 [ - + ]: 16 : if (dwarf_cu_die (listptr->cu, &cudie,
5834 : : NULL, NULL, NULL, NULL,
5835 : : NULL, NULL) == NULL)
5836 : 0 : printf ("Unknown CU (%s):\n", dwarf_errmsg (-1));
5837 : : else
5838 : 16 : printf ("for CU [%6" PRIx64 "]:\n", dwarf_dieoffset (&cudie));
5839 : :
5840 [ + + ]: 16 : if (listptr->offset == off)
5841 : : {
5842 [ - + ]: 4 : address_size = listptr_address_size (listptr);
5843 : 4 : segment_size = 0;
5844 : 4 : version = 4;
5845 : :
5846 : : /* The addresses start here, but where do they end? */
5847 [ + + ]: 4 : listptr = get_listptr (&known_addrbases, idx);
5848 [ - + ]: 2 : if (listptr == NULL)
5849 : : next_unitp = readendp;
5850 [ + - ]: 2 : else if (listptr->cu->version < 5)
5851 : : {
5852 : 2 : next_unitp = start + listptr->offset;
5853 [ + - - + ]: 2 : if (listptr->offset < off || listptr->offset > data->d_size)
5854 : : {
5855 : 0 : error (0, 0,
5856 : : "Warning: Bad address base for next unit at %"
5857 : : PRIx64, off);
5858 : 0 : next_unitp = readendp;
5859 : : }
5860 : : }
5861 : : else
5862 : : {
5863 : : /* Tricky, we don't have a header for this unit, but
5864 : : there is one for the next. We will have to
5865 : : "guess" how big it is and subtract it from the
5866 : : offset (because that points after the header). */
5867 [ # # ]: 0 : unsigned int offset_size = listptr_offset_size (listptr);
5868 : 0 : Dwarf_Off next_off = (listptr->offset
5869 : 0 : - (offset_size == 4 ? 4 : 12) /* len */
5870 : : - 2 /* version */
5871 : : - 1 /* address size */
5872 : 0 : - 1); /* segment selector size */
5873 : 0 : next_unitp = start + next_off;
5874 [ # # # # ]: 0 : if (next_off < off || next_off > data->d_size)
5875 : : {
5876 : 0 : error (0, 0,
5877 : : "Warning: Couldn't calculate .debug_addr "
5878 : : " unit length at %" PRIx64, off);
5879 : 0 : next_unitp = readendp;
5880 : : }
5881 : : }
5882 : 4 : unit_length = (uint64_t) (next_unitp - readp);
5883 : :
5884 : : /* Pretend we have a header. */
5885 : 4 : printf ("\n");
5886 : 4 : printf (_(" Length: %8" PRIu64 "\n"),
5887 : : unit_length);
5888 : 4 : printf (_(" DWARF version: %8" PRIu16 "\n"), version);
5889 : 4 : printf (_(" Address size: %8" PRIu64 "\n"),
5890 : : (uint64_t) address_size);
5891 : 4 : printf (_(" Segment size: %8" PRIu64 "\n"),
5892 : : (uint64_t) segment_size);
5893 : 16 : printf ("\n");
5894 : : }
5895 : : else
5896 : : {
5897 : : /* OK, we have to parse an header first. */
5898 [ - + ]: 12 : unit_length = read_4ubyte_unaligned_inc (dbg, readp);
5899 [ - + ]: 12 : if (unlikely (unit_length == 0xffffffff))
5900 : : {
5901 [ # # ]: 0 : if (unlikely (readp > readendp - 8))
5902 : : {
5903 : 0 : invalid_data:
5904 : 0 : error (0, 0, "Invalid data");
5905 : 0 : return;
5906 : : }
5907 [ # # ]: 0 : unit_length = read_8ubyte_unaligned_inc (dbg, readp);
5908 : : }
5909 : 12 : printf ("\n");
5910 : 12 : printf (_(" Length: %8" PRIu64 "\n"),
5911 : : unit_length);
5912 : :
5913 : : /* We need at least 2-bytes (version) + 1-byte
5914 : : (addr_size) + 1-byte (segment_size) = 4 bytes to
5915 : : complete the header. And this unit cannot go beyond
5916 : : the section data. */
5917 [ + - ]: 12 : if (readp > readendp - 4
5918 [ + - ]: 12 : || unit_length < 4
5919 [ - + ]: 12 : || unit_length > (uint64_t) (readendp - readp))
5920 : 0 : goto invalid_data;
5921 : :
5922 : 12 : next_unitp = readp + unit_length;
5923 : :
5924 [ - + ]: 12 : version = read_2ubyte_unaligned_inc (dbg, readp);
5925 : 12 : printf (_(" DWARF version: %8" PRIu16 "\n"), version);
5926 : :
5927 [ - + ]: 12 : if (version != 5)
5928 : : {
5929 : 0 : error (0, 0, _("Unknown version"));
5930 : 0 : goto next_unit;
5931 : : }
5932 : :
5933 : 12 : address_size = *readp++;
5934 : 12 : printf (_(" Address size: %8" PRIu64 "\n"),
5935 : : (uint64_t) address_size);
5936 : :
5937 [ - + ]: 12 : if (address_size != 4 && address_size != 8)
5938 : : {
5939 : 0 : error (0, 0, _("unsupported address size"));
5940 : 0 : goto next_unit;
5941 : : }
5942 : :
5943 : 12 : segment_size = *readp++;
5944 : 12 : printf (_(" Segment size: %8" PRIu64 "\n"),
5945 : : (uint64_t) segment_size);
5946 : 12 : printf ("\n");
5947 : :
5948 [ - + ]: 12 : if (segment_size != 0)
5949 : : {
5950 : 0 : error (0, 0, _("unsupported segment size"));
5951 : 0 : goto next_unit;
5952 : : }
5953 : :
5954 [ - + ]: 12 : if (listptr->offset != (Dwarf_Off) (readp - start))
5955 : : {
5956 : 0 : error (0, 0, "Address index doesn't start after header");
5957 : 0 : goto next_unit;
5958 : : }
5959 : : }
5960 : : }
5961 : :
5962 : 16 : int digits = 1;
5963 : 16 : size_t addresses = (next_unitp - readp) / address_size;
5964 [ + + ]: 24 : while (addresses >= 10)
5965 : : {
5966 : 8 : ++digits;
5967 : 8 : addresses /= 10;
5968 : : }
5969 : :
5970 : 16 : unsigned int uidx = 0;
5971 : 16 : size_t index_offset = readp - (const unsigned char *) data->d_buf;
5972 : 16 : printf (" Addresses start at offset 0x%zx:\n", index_offset);
5973 : 188 : while (readp <= next_unitp - address_size)
5974 : : {
5975 [ - + + + : 172 : Dwarf_Addr addr = read_addr_unaligned_inc (address_size, dbg,
- + - + ]
5976 : : readp);
5977 : 172 : printf (" [%*u] ", digits, uidx++);
5978 : 172 : print_dwarf_addr (dwflmod, address_size, addr, addr);
5979 [ + + ]: 188 : printf ("\n");
5980 : : }
5981 : 16 : printf ("\n");
5982 : :
5983 [ + - ]: 16 : if (readp != next_unitp)
5984 [ + + ]: 24 : error (0, 0, "extra %zd bytes at end of unit",
5985 : 0 : (size_t) (next_unitp - readp));
5986 : :
5987 : 16 : next_unit:
5988 : : readp = next_unitp;
5989 : : }
5990 : : }
5991 : :
5992 : : /* Print content of DWARF .debug_aranges section. We fortunately do
5993 : : not have to know a bit about the structure of the section, libdwarf
5994 : : takes care of it. */
5995 : : static void
5996 : 2 : print_decoded_aranges_section (Ebl *ebl, GElf_Ehdr *ehdr, Elf_Scn *scn,
5997 : : GElf_Shdr *shdr, Dwarf *dbg)
5998 : : {
5999 : 2 : Dwarf_Aranges *aranges;
6000 : 2 : size_t cnt;
6001 [ - + ]: 2 : if (unlikely (dwarf_getaranges (dbg, &aranges, &cnt) != 0))
6002 : : {
6003 : 0 : error (0, 0, _("cannot get .debug_aranges content: %s"),
6004 : : dwarf_errmsg (-1));
6005 : 0 : return;
6006 : : }
6007 : :
6008 : 2 : GElf_Shdr glink_mem;
6009 : 2 : GElf_Shdr *glink;
6010 : 2 : glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link), &glink_mem);
6011 [ - + ]: 2 : if (glink == NULL)
6012 : : {
6013 : 0 : error (0, 0, _("invalid sh_link value in section %zu"),
6014 : : elf_ndxscn (scn));
6015 : 0 : return;
6016 : : }
6017 : :
6018 : 2 : printf (ngettext ("\
6019 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 " contains %zu entry:\n",
6020 : : "\
6021 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 " contains %zu entries:\n",
6022 : : cnt),
6023 : : elf_ndxscn (scn), section_name (ebl, shdr),
6024 : 2 : (uint64_t) shdr->sh_offset, cnt);
6025 : :
6026 : : /* Compute floor(log16(cnt)). */
6027 : 2 : size_t tmp = cnt;
6028 : 2 : int digits = 1;
6029 [ - + ]: 2 : while (tmp >= 16)
6030 : : {
6031 : 0 : ++digits;
6032 : 0 : tmp >>= 4;
6033 : : }
6034 : :
6035 [ + + ]: 12 : for (size_t n = 0; n < cnt; ++n)
6036 : : {
6037 : 10 : Dwarf_Arange *runp = dwarf_onearange (aranges, n);
6038 [ - + ]: 10 : if (unlikely (runp == NULL))
6039 : : {
6040 : 0 : printf ("cannot get arange %zu: %s\n", n, dwarf_errmsg (-1));
6041 : 0 : return;
6042 : : }
6043 : :
6044 : 10 : Dwarf_Addr start;
6045 : 10 : Dwarf_Word length;
6046 : 10 : Dwarf_Off offset;
6047 : :
6048 [ - + ]: 10 : if (unlikely (dwarf_getarangeinfo (runp, &start, &length, &offset) != 0))
6049 : 0 : printf (_(" [%*zu] ???\n"), digits, n);
6050 : : else
6051 : 10 : printf (_(" [%*zu] start: %0#*" PRIx64
6052 : : ", length: %5" PRIu64 ", CU DIE offset: %6"
6053 : : PRId64 "\n"),
6054 [ - + ]: 10 : digits, n, ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 10 : 18,
6055 : : (uint64_t) start, (uint64_t) length, (int64_t) offset);
6056 : : }
6057 : : }
6058 : :
6059 : :
6060 : : /* Print content of DWARF .debug_aranges section. */
6061 : : static void
6062 : 92 : print_debug_aranges_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
6063 : : Ebl *ebl, GElf_Ehdr *ehdr, Elf_Scn *scn,
6064 : : GElf_Shdr *shdr, Dwarf *dbg)
6065 : : {
6066 [ + + ]: 92 : if (decodedaranges)
6067 : : {
6068 : 2 : print_decoded_aranges_section (ebl, ehdr, scn, shdr, dbg);
6069 : 2 : return;
6070 : : }
6071 : :
6072 : 90 : Elf_Data *data = get_debug_elf_data (dbg, ebl, IDX_debug_aranges, scn);
6073 [ + - ]: 90 : if (data == NULL)
6074 : : return;
6075 : :
6076 : 90 : printf (_("\
6077 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
6078 : : elf_ndxscn (scn), section_name (ebl, shdr),
6079 : 90 : (uint64_t) shdr->sh_offset);
6080 : :
6081 : 90 : const unsigned char *readp = data->d_buf;
6082 : 90 : const unsigned char *readendp = readp + data->d_size;
6083 : :
6084 [ + + ]: 3606 : while (readp < readendp)
6085 : : {
6086 : 3516 : const unsigned char *hdrstart = readp;
6087 : 3516 : size_t start_offset = hdrstart - (const unsigned char *) data->d_buf;
6088 : :
6089 : 3516 : printf (_("\nTable at offset %zu:\n"), start_offset);
6090 [ - + ]: 3516 : if (readp + 4 > readendp)
6091 : : {
6092 : 0 : invalid_data:
6093 : 0 : error (0, 0, _("invalid data in section [%zu] '%s'"),
6094 : : elf_ndxscn (scn), section_name (ebl, shdr));
6095 : 0 : return;
6096 : : }
6097 : :
6098 [ + + ]: 3516 : Dwarf_Word length = read_4ubyte_unaligned_inc (dbg, readp);
6099 : 3516 : unsigned int length_bytes = 4;
6100 [ - + ]: 3516 : if (length == DWARF3_LENGTH_64_BIT)
6101 : : {
6102 [ # # ]: 0 : if (readp + 8 > readendp)
6103 : 0 : goto invalid_data;
6104 [ # # ]: 0 : length = read_8ubyte_unaligned_inc (dbg, readp);
6105 : 0 : length_bytes = 8;
6106 : : }
6107 : :
6108 : 3516 : const unsigned char *nexthdr = readp + length;
6109 : 3516 : printf (_("\n Length: %6" PRIu64 "\n"),
6110 : : (uint64_t) length);
6111 : :
6112 [ - + ]: 3516 : if (unlikely (length > (size_t) (readendp - readp)))
6113 : 0 : goto invalid_data;
6114 : :
6115 [ - + ]: 3516 : if (length == 0)
6116 : 0 : continue;
6117 : :
6118 [ - + ]: 3516 : if (readp + 2 > readendp)
6119 : 0 : goto invalid_data;
6120 [ + + ]: 3516 : uint_fast16_t version = read_2ubyte_unaligned_inc (dbg, readp);
6121 : 3516 : printf (_(" DWARF version: %6" PRIuFAST16 "\n"),
6122 : : version);
6123 [ - + ]: 3516 : if (version != 2)
6124 : : {
6125 : 0 : error (0, 0, _("unsupported aranges version"));
6126 : 0 : goto next_table;
6127 : : }
6128 : :
6129 : 3516 : Dwarf_Word offset;
6130 [ - + ]: 3516 : if (readp + length_bytes > readendp)
6131 : 0 : goto invalid_data;
6132 [ - + ]: 3516 : if (length_bytes == 8)
6133 [ # # ]: 0 : offset = read_8ubyte_unaligned_inc (dbg, readp);
6134 : : else
6135 [ + + ]: 3516 : offset = read_4ubyte_unaligned_inc (dbg, readp);
6136 : 3516 : printf (_(" CU offset: %6" PRIx64 "\n"),
6137 : : (uint64_t) offset);
6138 : :
6139 [ - + ]: 3516 : if (readp + 1 > readendp)
6140 : 0 : goto invalid_data;
6141 : 3516 : unsigned int address_size = *readp++;
6142 : 3516 : printf (_(" Address size: %6" PRIu64 "\n"),
6143 : : (uint64_t) address_size);
6144 [ - + ]: 3516 : if (address_size != 4 && address_size != 8)
6145 : : {
6146 : 0 : error (0, 0, _("unsupported address size"));
6147 : 0 : goto next_table;
6148 : : }
6149 : :
6150 [ - + ]: 3516 : if (readp + 1 > readendp)
6151 : 0 : goto invalid_data;
6152 : 3516 : unsigned int segment_size = *readp++;
6153 : 3516 : printf (_(" Segment size: %6" PRIu64 "\n\n"),
6154 : : (uint64_t) segment_size);
6155 [ - + - - ]: 3516 : if (segment_size != 0 && segment_size != 4 && segment_size != 8)
6156 : : {
6157 : 0 : error (0, 0, _("unsupported segment size"));
6158 : 0 : goto next_table;
6159 : : }
6160 : :
6161 : : /* Round the address to the next multiple of 2*address_size. */
6162 : 3516 : readp += ((2 * address_size - ((readp - hdrstart) % (2 * address_size)))
6163 : 3516 : % (2 * address_size));
6164 : :
6165 : 3516 : while (readp < nexthdr)
6166 : : {
6167 : 7306 : Dwarf_Word range_address;
6168 : 7306 : Dwarf_Word range_length;
6169 : 7306 : Dwarf_Word segment = 0;
6170 [ - + ]: 7306 : if (readp + 2 * address_size + segment_size > readendp)
6171 : 0 : goto invalid_data;
6172 [ + + ]: 7306 : if (address_size == 4)
6173 : : {
6174 [ + + ]: 92 : range_address = read_4ubyte_unaligned_inc (dbg, readp);
6175 [ + + ]: 92 : range_length = read_4ubyte_unaligned_inc (dbg, readp);
6176 : : }
6177 : : else
6178 : : {
6179 [ + + ]: 7214 : range_address = read_8ubyte_unaligned_inc (dbg, readp);
6180 [ + + ]: 7214 : range_length = read_8ubyte_unaligned_inc (dbg, readp);
6181 : : }
6182 : :
6183 [ - + ]: 7306 : if (segment_size == 4)
6184 [ # # ]: 0 : segment = read_4ubyte_unaligned_inc (dbg, readp);
6185 [ - + ]: 7306 : else if (segment_size == 8)
6186 [ # # ]: 0 : segment = read_8ubyte_unaligned_inc (dbg, readp);
6187 : :
6188 [ + + ]: 7306 : if (range_address == 0 && range_length == 0 && segment == 0)
6189 : : break;
6190 : :
6191 : 3790 : printf (" ");
6192 : 3790 : print_dwarf_addr (dwflmod, address_size, range_address,
6193 : : range_address);
6194 : 3790 : printf ("..");
6195 : 3790 : print_dwarf_addr (dwflmod, address_size,
6196 : 3790 : range_address + range_length - 1,
6197 : : range_length);
6198 [ - + ]: 3790 : if (segment_size != 0)
6199 : 0 : printf (" (%" PRIx64 ")\n", (uint64_t) segment);
6200 : : else
6201 [ + - ]: 11096 : printf ("\n");
6202 : : }
6203 : :
6204 : 3516 : next_table:
6205 [ - + ]: 3516 : if (readp != nexthdr)
6206 : : {
6207 : 0 : size_t padding = nexthdr - readp;
6208 : 0 : printf (_(" %zu padding bytes\n"), padding);
6209 : 0 : readp = nexthdr;
6210 : : }
6211 : : }
6212 : : }
6213 : :
6214 : :
6215 : : static bool is_split_dwarf (Dwarf *dbg, uint64_t *id, Dwarf_CU **split_cu);
6216 : :
6217 : : /* Returns true and sets cu and cu_base if the given Dwarf is a split
6218 : : DWARF (.dwo) file. */
6219 : : static bool
6220 : 0 : split_dwarf_cu_base (Dwarf *dbg, Dwarf_CU **cu, Dwarf_Addr *cu_base)
6221 : : {
6222 : 0 : uint64_t id;
6223 [ # # ]: 0 : if (is_split_dwarf (dbg, &id, cu))
6224 : : {
6225 : 0 : Dwarf_Die cudie;
6226 [ # # ]: 0 : if (dwarf_cu_info (*cu, NULL, NULL, &cudie, NULL, NULL, NULL, NULL) == 0)
6227 : : {
6228 : 0 : *cu_base = cudie_base (&cudie);
6229 : 0 : return true;
6230 : : }
6231 : : }
6232 : : return false;
6233 : : }
6234 : :
6235 : : /* Print content of DWARF .debug_rnglists section. */
6236 : : static void
6237 : 38 : print_debug_rnglists_section (Dwfl_Module *dwflmod,
6238 : : Ebl *ebl,
6239 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
6240 : : Elf_Scn *scn, GElf_Shdr *shdr,
6241 : : Dwarf *dbg __attribute__((unused)))
6242 : : {
6243 : 38 : Elf_Data *data = get_debug_elf_data (dbg, ebl, IDX_debug_rnglists, scn);
6244 [ + - ]: 38 : if (data == NULL)
6245 : 0 : return;
6246 : :
6247 : 38 : printf (_("\
6248 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
6249 : : elf_ndxscn (scn), section_name (ebl, shdr),
6250 : 38 : (uint64_t) shdr->sh_offset);
6251 : :
6252 : : /* For the listptr to get the base address/CU. */
6253 : 38 : sort_listptr (&known_rnglistptr, "rnglistptr");
6254 : 38 : size_t listptr_idx = 0;
6255 : :
6256 : 38 : const unsigned char *readp = data->d_buf;
6257 : 38 : const unsigned char *const dataend = ((unsigned char *) data->d_buf
6258 : 38 : + data->d_size);
6259 [ + + ]: 1946 : while (readp < dataend)
6260 : : {
6261 [ - + ]: 1908 : if (unlikely (readp > dataend - 4))
6262 : : {
6263 : 0 : invalid_data:
6264 : 0 : error (0, 0, _("invalid data in section [%zu] '%s'"),
6265 : : elf_ndxscn (scn), section_name (ebl, shdr));
6266 : 0 : return;
6267 : : }
6268 : :
6269 : 1908 : ptrdiff_t offset = readp - (unsigned char *) data->d_buf;
6270 : 1908 : printf (_("Table at Offset 0x%" PRIx64 ":\n\n"),
6271 : : (uint64_t) offset);
6272 : :
6273 [ - + ]: 1908 : uint64_t unit_length = read_4ubyte_unaligned_inc (dbg, readp);
6274 : 1908 : unsigned int offset_size = 4;
6275 [ - + ]: 1908 : if (unlikely (unit_length == 0xffffffff))
6276 : : {
6277 [ # # ]: 0 : if (unlikely (readp > dataend - 8))
6278 : 0 : goto invalid_data;
6279 : :
6280 [ # # ]: 0 : unit_length = read_8ubyte_unaligned_inc (dbg, readp);
6281 : 0 : offset_size = 8;
6282 : : }
6283 : 1908 : printf (_(" Length: %8" PRIu64 "\n"), unit_length);
6284 : :
6285 : : /* We need at least 2-bytes + 1-byte + 1-byte + 4-bytes = 8
6286 : : bytes to complete the header. And this unit cannot go beyond
6287 : : the section data. */
6288 [ + - ]: 1908 : if (readp > dataend - 8
6289 [ + - ]: 1908 : || unit_length < 8
6290 [ - + ]: 1908 : || unit_length > (uint64_t) (dataend - readp))
6291 : 0 : goto invalid_data;
6292 : :
6293 : 1908 : const unsigned char *nexthdr = readp + unit_length;
6294 : :
6295 [ - + ]: 1908 : uint16_t version = read_2ubyte_unaligned_inc (dbg, readp);
6296 : 1908 : printf (_(" DWARF version: %8" PRIu16 "\n"), version);
6297 : :
6298 [ - + ]: 1908 : if (version != 5)
6299 : : {
6300 : 0 : error (0, 0, _("Unknown version"));
6301 : 0 : goto next_table;
6302 : : }
6303 : :
6304 : 1908 : uint8_t address_size = *readp++;
6305 : 1908 : printf (_(" Address size: %8" PRIu64 "\n"),
6306 : : (uint64_t) address_size);
6307 : :
6308 [ - + ]: 1908 : if (address_size != 4 && address_size != 8)
6309 : : {
6310 : 0 : error (0, 0, _("unsupported address size"));
6311 : 0 : goto next_table;
6312 : : }
6313 : :
6314 : 1908 : uint8_t segment_size = *readp++;
6315 : 1908 : printf (_(" Segment size: %8" PRIu64 "\n"),
6316 : : (uint64_t) segment_size);
6317 : :
6318 [ - + - - ]: 1908 : if (segment_size != 0 && segment_size != 4 && segment_size != 8)
6319 : : {
6320 : 0 : error (0, 0, _("unsupported segment size"));
6321 : 0 : goto next_table;
6322 : : }
6323 : :
6324 [ - + ]: 1908 : uint32_t offset_entry_count = read_4ubyte_unaligned_inc (dbg, readp);
6325 : 1908 : printf (_(" Offset entries: %8" PRIu64 "\n"),
6326 : : (uint64_t) offset_entry_count);
6327 : :
6328 : : /* We need the CU that uses this unit to get the initial base address. */
6329 : 1908 : Dwarf_Addr cu_base = 0;
6330 : 1908 : struct Dwarf_CU *cu = NULL;
6331 [ - + ]: 1908 : if (listptr_cu (&known_rnglistptr, &listptr_idx,
6332 : : (Dwarf_Off) offset,
6333 : 1908 : (Dwarf_Off) (nexthdr - (unsigned char *) data->d_buf),
6334 : : &cu_base, &cu)
6335 [ # # ]: 0 : || split_dwarf_cu_base (dbg, &cu, &cu_base))
6336 : 1908 : {
6337 : 1908 : Dwarf_Die cudie;
6338 [ - + ]: 1908 : if (dwarf_cu_die (cu, &cudie,
6339 : : NULL, NULL, NULL, NULL,
6340 : : NULL, NULL) == NULL)
6341 : 0 : printf (_(" Unknown CU base: "));
6342 : : else
6343 : 1908 : printf (_(" CU [%6" PRIx64 "] base: "),
6344 : : dwarf_dieoffset (&cudie));
6345 : 1908 : print_dwarf_addr (dwflmod, address_size, cu_base, cu_base);
6346 : 1908 : printf ("\n");
6347 : : }
6348 : : else
6349 : 0 : printf (_(" Not associated with a CU.\n"));
6350 : :
6351 : 1908 : printf ("\n");
6352 : :
6353 : 1908 : const unsigned char *offset_array_start = readp;
6354 [ + + ]: 1908 : if (offset_entry_count > 0)
6355 : : {
6356 : 2 : uint64_t max_entries = (unit_length - 8) / offset_size;
6357 [ - + ]: 2 : if (offset_entry_count > max_entries)
6358 : : {
6359 : 0 : error (0, 0,
6360 : 0 : _("too many offset entries for unit length"));
6361 : 0 : offset_entry_count = max_entries;
6362 : : }
6363 : :
6364 : 2 : printf (_(" Offsets starting at 0x%" PRIx64 ":\n"),
6365 : : (uint64_t) (offset_array_start
6366 : 2 : - (unsigned char *) data->d_buf));
6367 [ + + ]: 6 : for (uint32_t idx = 0; idx < offset_entry_count; idx++)
6368 : : {
6369 : 4 : printf (" [%6" PRIu32 "] ", idx);
6370 [ + - ]: 4 : if (offset_size == 4)
6371 : : {
6372 [ - + ]: 4 : uint32_t off = read_4ubyte_unaligned_inc (dbg, readp);
6373 : 4 : printf ("0x%" PRIx32 "\n", off);
6374 : : }
6375 : : else
6376 : : {
6377 [ # # ]: 0 : uint64_t off = read_8ubyte_unaligned_inc (dbg, readp);
6378 : 4 : printf ("0x%" PRIx64 "\n", off);
6379 : : }
6380 : : }
6381 : 2 : printf ("\n");
6382 : : }
6383 : :
6384 : 1908 : Dwarf_Addr base = cu_base;
6385 : 1908 : bool start_of_list = true;
6386 : 1908 : while (readp < nexthdr)
6387 : : {
6388 : 132866 : uint8_t kind = *readp++;
6389 : 132866 : uint64_t op1, op2;
6390 : :
6391 : : /* Skip padding. */
6392 [ - + ]: 132866 : if (start_of_list && kind == DW_RLE_end_of_list)
6393 : 0 : continue;
6394 : :
6395 [ + + ]: 132866 : if (start_of_list)
6396 : : {
6397 : 27712 : base = cu_base;
6398 : 27712 : printf (" Offset: %" PRIx64 ", Index: %" PRIx64 "\n",
6399 : 27712 : (uint64_t) (readp - (unsigned char *) data->d_buf - 1),
6400 : 27712 : (uint64_t) (readp - offset_array_start - 1));
6401 : 27712 : start_of_list = false;
6402 : : }
6403 : :
6404 : 132866 : printf (" %s", dwarf_range_list_encoding_name (kind));
6405 [ + - - - : 132866 : switch (kind)
+ + - +
- ]
6406 : : {
6407 : 27712 : case DW_RLE_end_of_list:
6408 : 27712 : start_of_list = true;
6409 : 27712 : printf ("\n\n");
6410 : : break;
6411 : :
6412 : 0 : case DW_RLE_base_addressx:
6413 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
6414 : : {
6415 : 0 : invalid_range:
6416 : 0 : error (0, 0, _("invalid range list data"));
6417 : 0 : goto next_table;
6418 : : }
6419 : 0 : get_uleb128 (op1, readp, nexthdr);
6420 : 0 : printf (" %" PRIx64 "\n", op1);
6421 [ # # ]: 0 : if (! print_unresolved_addresses)
6422 : : {
6423 : 0 : Dwarf_Addr addr;
6424 [ # # ]: 0 : if (get_indexed_addr (cu, op1, &addr) != 0)
6425 : 0 : printf (" ???\n");
6426 : : else
6427 : : {
6428 : 0 : printf (" ");
6429 : 0 : print_dwarf_addr (dwflmod, address_size, addr, addr);
6430 : 0 : printf ("\n");
6431 : : }
6432 : : }
6433 : : break;
6434 : :
6435 : 0 : case DW_RLE_startx_endx:
6436 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
6437 : 0 : goto invalid_range;
6438 : 0 : get_uleb128 (op1, readp, nexthdr);
6439 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
6440 : 0 : goto invalid_range;
6441 : 0 : get_uleb128 (op2, readp, nexthdr);
6442 : 0 : printf (" %" PRIx64 ", %" PRIx64 "\n", op1, op2);
6443 [ # # ]: 0 : if (! print_unresolved_addresses)
6444 : : {
6445 : 0 : Dwarf_Addr addr1;
6446 : 0 : Dwarf_Addr addr2;
6447 [ # # ]: 0 : if (get_indexed_addr (cu, op1, &addr1) != 0
6448 [ # # ]: 0 : || get_indexed_addr (cu, op2, &addr2) != 0)
6449 : : {
6450 : 0 : printf (" ???..\n");
6451 : 0 : printf (" ???\n");
6452 : : }
6453 : : else
6454 : : {
6455 : 0 : printf (" ");
6456 : 0 : print_dwarf_addr (dwflmod, address_size, addr1, addr1);
6457 : 0 : printf ("..\n ");
6458 : 0 : print_dwarf_addr (dwflmod, address_size,
6459 : : addr2 - 1, addr2);
6460 : 0 : printf ("\n");
6461 : : }
6462 : : }
6463 : : break;
6464 : :
6465 : 0 : case DW_RLE_startx_length:
6466 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
6467 : 0 : goto invalid_range;
6468 : 0 : get_uleb128 (op1, readp, nexthdr);
6469 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
6470 : 0 : goto invalid_range;
6471 : 0 : get_uleb128 (op2, readp, nexthdr);
6472 : 0 : printf (" %" PRIx64 ", %" PRIx64 "\n", op1, op2);
6473 [ # # ]: 0 : if (! print_unresolved_addresses)
6474 : : {
6475 : 0 : Dwarf_Addr addr1;
6476 : 0 : Dwarf_Addr addr2;
6477 [ # # ]: 0 : if (get_indexed_addr (cu, op1, &addr1) != 0)
6478 : : {
6479 : 0 : printf (" ???..\n");
6480 : 0 : printf (" ???\n");
6481 : : }
6482 : : else
6483 : : {
6484 : 0 : addr2 = addr1 + op2;
6485 : 0 : printf (" ");
6486 : 0 : print_dwarf_addr (dwflmod, address_size, addr1, addr1);
6487 : 0 : printf ("..\n ");
6488 : 0 : print_dwarf_addr (dwflmod, address_size,
6489 : : addr2 - 1, addr2);
6490 : 0 : printf ("\n");
6491 : : }
6492 : : }
6493 : : break;
6494 : :
6495 : 95776 : case DW_RLE_offset_pair:
6496 [ - + ]: 95776 : if ((uint64_t) (nexthdr - readp) < 1)
6497 : 0 : goto invalid_range;
6498 : 95776 : get_uleb128 (op1, readp, nexthdr);
6499 [ - + ]: 95776 : if ((uint64_t) (nexthdr - readp) < 1)
6500 : 0 : goto invalid_range;
6501 : 95776 : get_uleb128 (op2, readp, nexthdr);
6502 : 95776 : printf (" %" PRIx64 ", %" PRIx64 "\n", op1, op2);
6503 [ + - ]: 95776 : if (! print_unresolved_addresses)
6504 : : {
6505 : 95776 : op1 += base;
6506 : 95776 : op2 += base;
6507 : 95776 : printf (" ");
6508 : 95776 : print_dwarf_addr (dwflmod, address_size, op1, op1);
6509 : 95776 : printf ("..\n ");
6510 : 95776 : print_dwarf_addr (dwflmod, address_size, op2 - 1, op2);
6511 : 95776 : printf ("\n");
6512 : : }
6513 : : break;
6514 : :
6515 : 8464 : case DW_RLE_base_address:
6516 [ - + ]: 8464 : if (address_size == 4)
6517 : : {
6518 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 4)
6519 : 0 : goto invalid_range;
6520 [ # # ]: 0 : op1 = read_4ubyte_unaligned_inc (dbg, readp);
6521 : : }
6522 : : else
6523 : : {
6524 [ - + ]: 8464 : if ((uint64_t) (nexthdr - readp) < 8)
6525 : 0 : goto invalid_range;
6526 [ - + ]: 8464 : op1 = read_8ubyte_unaligned_inc (dbg, readp);
6527 : : }
6528 : 8464 : base = op1;
6529 : 8464 : printf (" 0x%" PRIx64 "\n", base);
6530 [ - + ]: 8464 : if (! print_unresolved_addresses)
6531 : : {
6532 : 8464 : printf (" ");
6533 : 8464 : print_dwarf_addr (dwflmod, address_size, base, base);
6534 : 8464 : printf ("\n");
6535 : : }
6536 : : break;
6537 : :
6538 : 0 : case DW_RLE_start_end:
6539 [ # # ]: 0 : if (address_size == 4)
6540 : : {
6541 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 8)
6542 : 0 : goto invalid_range;
6543 [ # # ]: 0 : op1 = read_4ubyte_unaligned_inc (dbg, readp);
6544 [ # # ]: 0 : op2 = read_4ubyte_unaligned_inc (dbg, readp);
6545 : : }
6546 : : else
6547 : : {
6548 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 16)
6549 : 0 : goto invalid_range;
6550 [ # # ]: 0 : op1 = read_8ubyte_unaligned_inc (dbg, readp);
6551 [ # # ]: 0 : op2 = read_8ubyte_unaligned_inc (dbg, readp);
6552 : : }
6553 : 0 : printf (" 0x%" PRIx64 "..0x%" PRIx64 "\n", op1, op2);
6554 [ # # ]: 0 : if (! print_unresolved_addresses)
6555 : : {
6556 : 0 : printf (" ");
6557 : 0 : print_dwarf_addr (dwflmod, address_size, op1, op1);
6558 : 0 : printf ("..\n ");
6559 : 0 : print_dwarf_addr (dwflmod, address_size, op2 - 1, op2);
6560 : 0 : printf ("\n");
6561 : : }
6562 : : break;
6563 : :
6564 : 914 : case DW_RLE_start_length:
6565 [ - + ]: 914 : if (address_size == 4)
6566 : : {
6567 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 4)
6568 : 0 : goto invalid_range;
6569 [ # # ]: 0 : op1 = read_4ubyte_unaligned_inc (dbg, readp);
6570 : : }
6571 : : else
6572 : : {
6573 [ - + ]: 914 : if ((uint64_t) (nexthdr - readp) < 8)
6574 : 0 : goto invalid_range;
6575 [ - + ]: 914 : op1 = read_8ubyte_unaligned_inc (dbg, readp);
6576 : : }
6577 [ - + ]: 914 : if ((uint64_t) (nexthdr - readp) < 1)
6578 : 0 : goto invalid_range;
6579 : 914 : get_uleb128 (op2, readp, nexthdr);
6580 : 914 : printf (" 0x%" PRIx64 ", %" PRIx64 "\n", op1, op2);
6581 [ + + ]: 914 : if (! print_unresolved_addresses)
6582 : : {
6583 : 912 : op2 = op1 + op2;
6584 : 912 : printf (" ");
6585 : 912 : print_dwarf_addr (dwflmod, address_size, op1, op1);
6586 : 912 : printf ("..\n ");
6587 : 912 : print_dwarf_addr (dwflmod, address_size, op2 - 1, op2);
6588 [ + + ]: 135686 : printf ("\n");
6589 : : }
6590 : : break;
6591 : :
6592 : 0 : default:
6593 : 0 : goto invalid_range;
6594 : : }
6595 : : }
6596 : :
6597 : 1908 : next_table:
6598 [ - + ]: 1908 : if (readp != nexthdr)
6599 : : {
6600 : 0 : size_t padding = nexthdr - readp;
6601 : 0 : printf (_(" %zu padding bytes\n\n"), padding);
6602 : 0 : readp = nexthdr;
6603 : : }
6604 : : }
6605 : : }
6606 : :
6607 : : /* Print content of DWARF .debug_ranges section. */
6608 : : static void
6609 : 32 : print_debug_ranges_section (Dwfl_Module *dwflmod,
6610 : : Ebl *ebl, GElf_Ehdr *ehdr,
6611 : : Elf_Scn *scn, GElf_Shdr *shdr,
6612 : : Dwarf *dbg)
6613 : : {
6614 : 32 : Elf_Data *data = get_debug_elf_data (dbg, ebl, IDX_debug_ranges, scn);
6615 [ - + ]: 32 : if (data == NULL)
6616 : 0 : return;
6617 : :
6618 : 32 : printf (_("\
6619 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
6620 : : elf_ndxscn (scn), section_name (ebl, shdr),
6621 : 32 : (uint64_t) shdr->sh_offset);
6622 : :
6623 : 32 : sort_listptr (&known_rangelistptr, "rangelistptr");
6624 : 32 : size_t listptr_idx = 0;
6625 : :
6626 [ + + ]: 32 : uint_fast8_t address_size = ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 4 : 8;
6627 : :
6628 : 32 : bool first = true;
6629 : 32 : Dwarf_Addr base = 0;
6630 : 32 : unsigned char *const endp = (unsigned char *) data->d_buf + data->d_size;
6631 : 32 : unsigned char *readp = data->d_buf;
6632 : 32 : Dwarf_CU *last_cu = NULL;
6633 [ + + ]: 184 : while (readp < endp)
6634 : : {
6635 : 152 : ptrdiff_t offset = readp - (unsigned char *) data->d_buf;
6636 : 152 : Dwarf_CU *cu = last_cu;
6637 : :
6638 [ + + - + ]: 152 : if (first && skip_listptr_hole (&known_rangelistptr, &listptr_idx,
6639 : : &address_size, NULL, &base, &cu,
6640 : : offset, &readp, endp, NULL))
6641 : 0 : continue;
6642 : :
6643 [ + + ]: 152 : if (last_cu != cu)
6644 : : {
6645 : 58 : Dwarf_Die cudie;
6646 [ - + ]: 58 : if (dwarf_cu_die (cu, &cudie,
6647 : : NULL, NULL, NULL, NULL,
6648 : : NULL, NULL) == NULL)
6649 : 0 : printf (_("\n Unknown CU base: "));
6650 : : else
6651 : 58 : printf (_("\n CU [%6" PRIx64 "] base: "),
6652 : : dwarf_dieoffset (&cudie));
6653 : 58 : print_dwarf_addr (dwflmod, address_size, base, base);
6654 : 58 : printf ("\n");
6655 : : }
6656 : 152 : last_cu = cu;
6657 : :
6658 [ - + ]: 152 : if (unlikely (data->d_size - offset < (size_t) address_size * 2))
6659 : : {
6660 : 0 : printf (_(" [%6tx] <INVALID DATA>\n"), offset);
6661 : 0 : break;
6662 : : }
6663 : :
6664 : 152 : Dwarf_Addr begin;
6665 : 152 : Dwarf_Addr end;
6666 [ + + ]: 152 : if (address_size == 8)
6667 : : {
6668 [ - + ]: 120 : begin = read_8ubyte_unaligned_inc (dbg, readp);
6669 [ - + ]: 120 : end = read_8ubyte_unaligned_inc (dbg, readp);
6670 : : }
6671 : : else
6672 : : {
6673 [ - + ]: 32 : begin = read_4ubyte_unaligned_inc (dbg, readp);
6674 [ - + ]: 32 : end = read_4ubyte_unaligned_inc (dbg, readp);
6675 [ + - ]: 32 : if (begin == (Dwarf_Addr) (uint32_t) -1)
6676 : : begin = (Dwarf_Addr) -1l;
6677 : : }
6678 : :
6679 [ - + ]: 152 : if (begin == (Dwarf_Addr) -1l) /* Base address entry. */
6680 : : {
6681 [ # # ]: 0 : if (first)
6682 : 0 : printf (" [%6tx] ", offset);
6683 : : else
6684 : 0 : printf (" ");
6685 : 0 : puts (_("base address"));
6686 : 0 : printf (" ");
6687 : 0 : print_dwarf_addr (dwflmod, address_size, end, end);
6688 : 0 : printf ("\n");
6689 : 0 : base = end;
6690 : 0 : first = false;
6691 : : }
6692 [ + + ]: 152 : else if (begin == 0 && end == 0) /* End of list entry. */
6693 : : {
6694 [ - + ]: 60 : if (first)
6695 : 0 : printf (_(" [%6tx] empty list\n"), offset);
6696 : : first = true;
6697 : : }
6698 : : else
6699 : : {
6700 : : /* We have an address range entry. */
6701 [ + + ]: 92 : if (first) /* First address range entry in a list. */
6702 : 60 : printf (" [%6tx] ", offset);
6703 : : else
6704 : 32 : printf (" ");
6705 : :
6706 : 92 : printf ("range %" PRIx64 ", %" PRIx64 "\n", begin, end);
6707 [ + + ]: 92 : if (! print_unresolved_addresses)
6708 : : {
6709 : 72 : printf (" ");
6710 : 72 : print_dwarf_addr (dwflmod, address_size, base + begin,
6711 : : base + begin);
6712 : 72 : printf ("..\n ");
6713 : 72 : print_dwarf_addr (dwflmod, address_size,
6714 : : base + end - 1, base + end);
6715 : 224 : printf ("\n");
6716 : : }
6717 : :
6718 : : first = false;
6719 : : }
6720 : : }
6721 : : }
6722 : :
6723 : : #define REGNAMESZ 16
6724 : : static const char *
6725 : 33118 : register_info (Ebl *ebl, unsigned int regno, const Ebl_Register_Location *loc,
6726 : : char name[REGNAMESZ], int *bits, int *type)
6727 : : {
6728 : 33118 : const char *set;
6729 : 33118 : const char *pfx;
6730 : 33118 : int ignore;
6731 [ + + + + ]: 95962 : ssize_t n = ebl_register_info (ebl, regno, name, REGNAMESZ, &pfx, &set,
6732 : : bits ?: &ignore, type ?: &ignore);
6733 [ - + ]: 33118 : if (n <= 0)
6734 : : {
6735 [ # # ]: 0 : if (loc != NULL)
6736 : 0 : snprintf (name, REGNAMESZ, "reg%u", loc->regno);
6737 : : else
6738 : 0 : snprintf (name, REGNAMESZ, "??? 0x%x", regno);
6739 [ # # ]: 0 : if (bits != NULL)
6740 [ # # ]: 0 : *bits = loc != NULL ? loc->bits : 0;
6741 [ # # ]: 0 : if (type != NULL)
6742 : 0 : *type = DW_ATE_unsigned;
6743 : 0 : set = "??? unrecognized";
6744 : : }
6745 : : else
6746 : : {
6747 [ + + - + ]: 33118 : if (bits != NULL && *bits <= 0)
6748 [ # # ]: 0 : *bits = loc != NULL ? loc->bits : 0;
6749 [ + + - + ]: 33118 : if (type != NULL && *type == DW_ATE_void)
6750 : 0 : *type = DW_ATE_unsigned;
6751 : :
6752 : : }
6753 : 33118 : return set;
6754 : : }
6755 : :
6756 : : static const unsigned char *
6757 : 280 : read_encoded (unsigned int encoding, const unsigned char *readp,
6758 : : const unsigned char *const endp, uint64_t *res, Dwarf *dbg)
6759 : : {
6760 [ - + ]: 280 : if ((encoding & 0xf) == DW_EH_PE_absptr)
6761 : 0 : encoding = gelf_getclass (dbg->elf) == ELFCLASS32
6762 [ # # ]: 0 : ? DW_EH_PE_udata4 : DW_EH_PE_udata8;
6763 : :
6764 [ - - - + : 280 : switch (encoding & 0xf)
- - + -
- ]
6765 : : {
6766 : 0 : case DW_EH_PE_uleb128:
6767 [ # # ]: 0 : if (readp >= endp)
6768 : 0 : goto invalid;
6769 : 0 : get_uleb128 (*res, readp, endp);
6770 : 0 : break;
6771 : 0 : case DW_EH_PE_sleb128:
6772 [ # # ]: 0 : if (readp >= endp)
6773 : 0 : goto invalid;
6774 : 0 : get_sleb128 (*res, readp, endp);
6775 : 0 : break;
6776 : 0 : case DW_EH_PE_udata2:
6777 [ # # ]: 0 : if (readp + 2 > endp)
6778 : 0 : goto invalid;
6779 [ # # ]: 0 : *res = read_2ubyte_unaligned_inc (dbg, readp);
6780 : 0 : break;
6781 : 140 : case DW_EH_PE_udata4:
6782 [ - + ]: 140 : if (readp + 4 > endp)
6783 : 0 : goto invalid;
6784 [ - + ]: 140 : *res = read_4ubyte_unaligned_inc (dbg, readp);
6785 : 140 : break;
6786 : 0 : case DW_EH_PE_udata8:
6787 [ # # ]: 0 : if (readp + 8 > endp)
6788 : 0 : goto invalid;
6789 [ # # ]: 0 : *res = read_8ubyte_unaligned_inc (dbg, readp);
6790 : 0 : break;
6791 : 0 : case DW_EH_PE_sdata2:
6792 [ # # ]: 0 : if (readp + 2 > endp)
6793 : 0 : goto invalid;
6794 [ # # ]: 0 : *res = read_2sbyte_unaligned_inc (dbg, readp);
6795 : 0 : break;
6796 : 140 : case DW_EH_PE_sdata4:
6797 [ - + ]: 140 : if (readp + 4 > endp)
6798 : 0 : goto invalid;
6799 [ - + ]: 140 : *res = read_4sbyte_unaligned_inc (dbg, readp);
6800 : 140 : break;
6801 : 0 : case DW_EH_PE_sdata8:
6802 [ # # ]: 0 : if (readp + 8 > endp)
6803 : 0 : goto invalid;
6804 [ # # ]: 0 : *res = read_8sbyte_unaligned_inc (dbg, readp);
6805 : 0 : break;
6806 : : default:
6807 : 0 : invalid:
6808 : 0 : error (1, 0,
6809 : 0 : _("invalid encoding"));
6810 : : }
6811 : :
6812 : 280 : return readp;
6813 : : }
6814 : :
6815 : : static const char *
6816 : 31422 : regname (Ebl *ebl, unsigned int regno, char *regnamebuf)
6817 : : {
6818 : 31422 : register_info (ebl, regno, NULL, regnamebuf, NULL, NULL);
6819 : :
6820 : 31422 : return regnamebuf;
6821 : : }
6822 : :
6823 : : static void
6824 : 22218 : print_cfa_program (const unsigned char *readp, const unsigned char *const endp,
6825 : : Dwarf_Word vma_base, unsigned int code_align,
6826 : : int data_align,
6827 : : unsigned int version, unsigned int ptr_size,
6828 : : unsigned int encoding,
6829 : : Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr, Dwarf *dbg)
6830 : : {
6831 : 22218 : char regnamebuf[REGNAMESZ];
6832 : :
6833 : 22218 : puts ("\n Program:");
6834 : 22218 : Dwarf_Word pc = vma_base;
6835 : 22218 : while (readp < endp)
6836 : : {
6837 : 303244 : unsigned int opcode = *readp++;
6838 : :
6839 [ + + ]: 303244 : if (opcode < DW_CFA_advance_loc)
6840 : : /* Extended opcode. */
6841 [ + - + + : 182830 : switch (opcode)
+ + - + -
- + + + +
+ + - + -
- - - - -
+ - - ]
6842 : : {
6843 : 63392 : uint64_t op1;
6844 : 63392 : int64_t sop1;
6845 : 63392 : uint64_t op2;
6846 : 63392 : int64_t sop2;
6847 : :
6848 : 63392 : case DW_CFA_nop:
6849 : 63392 : puts (" nop");
6850 : 63392 : break;
6851 : 0 : case DW_CFA_set_loc:
6852 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
6853 : 0 : goto invalid;
6854 : 0 : readp = read_encoded (encoding, readp, endp, &op1, dbg);
6855 : 182830 : printf (" set_loc %#" PRIx64 " to %#" PRIx64 "\n",
6856 : 0 : op1, pc = vma_base + op1);
6857 : : break;
6858 : 6892 : case DW_CFA_advance_loc1:
6859 [ - + ]: 6892 : if ((uint64_t) (endp - readp) < 1)
6860 : 0 : goto invalid;
6861 : 13784 : printf (" advance_loc1 %u to %#" PRIx64 "\n",
6862 : 6892 : *readp, pc += *readp * code_align);
6863 : 6892 : ++readp;
6864 : 6892 : break;
6865 : 2322 : case DW_CFA_advance_loc2:
6866 [ - + ]: 2322 : if ((uint64_t) (endp - readp) < 2)
6867 : 0 : goto invalid;
6868 [ - + ]: 2322 : op1 = read_2ubyte_unaligned_inc (dbg, readp);
6869 : 185152 : printf (" advance_loc2 %" PRIu64 " to %#" PRIx64 "\n",
6870 : 2322 : op1, pc += op1 * code_align);
6871 : : break;
6872 : 36 : case DW_CFA_advance_loc4:
6873 [ - + ]: 36 : if ((uint64_t) (endp - readp) < 4)
6874 : 0 : goto invalid;
6875 [ + - ]: 36 : op1 = read_4ubyte_unaligned_inc (dbg, readp);
6876 : 182866 : printf (" advance_loc4 %" PRIu64 " to %#" PRIx64 "\n",
6877 : 36 : op1, pc += op1 * code_align);
6878 : : break;
6879 : 8 : case DW_CFA_offset_extended:
6880 [ - + ]: 8 : if ((uint64_t) (endp - readp) < 1)
6881 : 0 : goto invalid;
6882 : 8 : get_uleb128 (op1, readp, endp);
6883 [ - + ]: 8 : if ((uint64_t) (endp - readp) < 1)
6884 : 0 : goto invalid;
6885 : 8 : get_uleb128 (op2, readp, endp);
6886 : 8 : printf (" offset_extended r%" PRIu64 " (%s) at cfa%+" PRId64
6887 : : "\n",
6888 : : op1, regname (ebl, op1, regnamebuf), op2 * data_align);
6889 : : break;
6890 : 0 : case DW_CFA_restore_extended:
6891 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
6892 : 0 : goto invalid;
6893 : 0 : get_uleb128 (op1, readp, endp);
6894 : 0 : printf (" restore_extended r%" PRIu64 " (%s)\n",
6895 : : op1, regname (ebl, op1, regnamebuf));
6896 : : break;
6897 : 110 : case DW_CFA_undefined:
6898 [ - + ]: 110 : if ((uint64_t) (endp - readp) < 1)
6899 : 0 : goto invalid;
6900 : 110 : get_uleb128 (op1, readp, endp);
6901 : 110 : printf (" undefined r%" PRIu64 " (%s)\n", op1,
6902 : : regname (ebl, op1, regnamebuf));
6903 : : break;
6904 : 0 : case DW_CFA_same_value:
6905 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
6906 : 0 : goto invalid;
6907 : 0 : get_uleb128 (op1, readp, endp);
6908 : 0 : printf (" same_value r%" PRIu64 " (%s)\n", op1,
6909 : : regname (ebl, op1, regnamebuf));
6910 : : break;
6911 : 0 : case DW_CFA_register:
6912 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
6913 : 0 : goto invalid;
6914 : 0 : get_uleb128 (op1, readp, endp);
6915 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
6916 : 0 : goto invalid;
6917 : 0 : get_uleb128 (op2, readp, endp);
6918 : 0 : printf (" register r%" PRIu64 " (%s) in r%" PRIu64 " (%s)\n",
6919 : : op1, regname (ebl, op1, regnamebuf), op2,
6920 : : regname (ebl, op2, regnamebuf));
6921 : : break;
6922 : 11104 : case DW_CFA_remember_state:
6923 : 11104 : puts (" remember_state");
6924 : 11104 : break;
6925 : 11104 : case DW_CFA_restore_state:
6926 : 11104 : puts (" restore_state");
6927 : 11104 : break;
6928 : 416 : case DW_CFA_def_cfa:
6929 [ - + ]: 416 : if ((uint64_t) (endp - readp) < 1)
6930 : 0 : goto invalid;
6931 : 416 : get_uleb128 (op1, readp, endp);
6932 [ - + ]: 416 : if ((uint64_t) (endp - readp) < 1)
6933 : 0 : goto invalid;
6934 : 416 : get_uleb128 (op2, readp, endp);
6935 : 416 : printf (" def_cfa r%" PRIu64 " (%s) at offset %" PRIu64 "\n",
6936 : : op1, regname (ebl, op1, regnamebuf), op2);
6937 : : break;
6938 : 126 : case DW_CFA_def_cfa_register:
6939 [ - + ]: 126 : if ((uint64_t) (endp - readp) < 1)
6940 : 0 : goto invalid;
6941 : 126 : get_uleb128 (op1, readp, endp);
6942 : 126 : printf (" def_cfa_register r%" PRIu64 " (%s)\n",
6943 : : op1, regname (ebl, op1, regnamebuf));
6944 : : break;
6945 : 87170 : case DW_CFA_def_cfa_offset:
6946 [ - + ]: 87170 : if ((uint64_t) (endp - readp) < 1)
6947 : 0 : goto invalid;
6948 : 87170 : get_uleb128 (op1, readp, endp);
6949 : 87170 : printf (" def_cfa_offset %" PRIu64 "\n", op1);
6950 : : break;
6951 : 118 : case DW_CFA_def_cfa_expression:
6952 [ - + ]: 118 : if ((uint64_t) (endp - readp) < 1)
6953 : 0 : goto invalid;
6954 : 118 : get_uleb128 (op1, readp, endp); /* Length of DW_FORM_block. */
6955 : 118 : printf (" def_cfa_expression %" PRIu64 "\n", op1);
6956 [ - + ]: 118 : if ((uint64_t) (endp - readp) < op1)
6957 : : {
6958 : 0 : invalid:
6959 : 0 : fputs (_(" <INVALID DATA>\n"), stdout);
6960 : 0 : return;
6961 : : }
6962 : 118 : print_ops (dwflmod, dbg, 10, 10, version, ptr_size, 0, NULL,
6963 : : op1, readp);
6964 : 118 : readp += op1;
6965 : 118 : break;
6966 : 0 : case DW_CFA_expression:
6967 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
6968 : 0 : goto invalid;
6969 : 0 : get_uleb128 (op1, readp, endp);
6970 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
6971 : 0 : goto invalid;
6972 : 0 : get_uleb128 (op2, readp, endp); /* Length of DW_FORM_block. */
6973 : 0 : printf (" expression r%" PRIu64 " (%s) \n",
6974 : : op1, regname (ebl, op1, regnamebuf));
6975 [ # # ]: 0 : if ((uint64_t) (endp - readp) < op2)
6976 : 0 : goto invalid;
6977 : 0 : print_ops (dwflmod, dbg, 10, 10, version, ptr_size, 0, NULL,
6978 : : op2, readp);
6979 : 0 : readp += op2;
6980 : 0 : break;
6981 : 24 : case DW_CFA_offset_extended_sf:
6982 [ - + ]: 24 : if ((uint64_t) (endp - readp) < 1)
6983 : 0 : goto invalid;
6984 : 24 : get_uleb128 (op1, readp, endp);
6985 [ - + ]: 24 : if ((uint64_t) (endp - readp) < 1)
6986 : 0 : goto invalid;
6987 : 24 : get_sleb128 (sop2, readp, endp);
6988 : 24 : printf (" offset_extended_sf r%" PRIu64 " (%s) at cfa%+"
6989 : : PRId64 "\n",
6990 : : op1, regname (ebl, op1, regnamebuf), sop2 * data_align);
6991 : : break;
6992 : 0 : case DW_CFA_def_cfa_sf:
6993 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
6994 : 0 : goto invalid;
6995 : 0 : get_uleb128 (op1, readp, endp);
6996 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
6997 : 0 : goto invalid;
6998 : 0 : get_sleb128 (sop2, readp, endp);
6999 : 0 : printf (" def_cfa_sf r%" PRIu64 " (%s) at offset %" PRId64 "\n",
7000 : : op1, regname (ebl, op1, regnamebuf), sop2 * data_align);
7001 : : break;
7002 : 0 : case DW_CFA_def_cfa_offset_sf:
7003 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
7004 : 0 : goto invalid;
7005 : 0 : get_sleb128 (sop1, readp, endp);
7006 : 0 : printf (" def_cfa_offset_sf %" PRId64 "\n", sop1 * data_align);
7007 : : break;
7008 : 0 : case DW_CFA_val_offset:
7009 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
7010 : 0 : goto invalid;
7011 : 0 : get_uleb128 (op1, readp, endp);
7012 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
7013 : 0 : goto invalid;
7014 : 0 : get_uleb128 (op2, readp, endp);
7015 : 0 : printf (" val_offset %" PRIu64 " at offset %" PRIu64 "\n",
7016 : : op1, op2 * data_align);
7017 : : break;
7018 : 0 : case DW_CFA_val_offset_sf:
7019 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
7020 : 0 : goto invalid;
7021 : 0 : get_uleb128 (op1, readp, endp);
7022 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
7023 : 0 : goto invalid;
7024 : 0 : get_sleb128 (sop2, readp, endp);
7025 : 0 : printf (" val_offset_sf %" PRIu64 " at offset %" PRId64 "\n",
7026 : : op1, sop2 * data_align);
7027 : : break;
7028 : 0 : case DW_CFA_val_expression:
7029 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
7030 : 0 : goto invalid;
7031 : 0 : get_uleb128 (op1, readp, endp);
7032 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
7033 : 0 : goto invalid;
7034 : 0 : get_uleb128 (op2, readp, endp); /* Length of DW_FORM_block. */
7035 : 0 : printf (" val_expression r%" PRIu64 " (%s)\n",
7036 : : op1, regname (ebl, op1, regnamebuf));
7037 [ # # ]: 0 : if ((uint64_t) (endp - readp) < op2)
7038 : 0 : goto invalid;
7039 : 0 : print_ops (dwflmod, dbg, 10, 10, version, ptr_size, 0,
7040 : : NULL, op2, readp);
7041 : 0 : readp += op2;
7042 : 0 : break;
7043 : 0 : case DW_CFA_MIPS_advance_loc8:
7044 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 8)
7045 : 0 : goto invalid;
7046 [ # # ]: 0 : op1 = read_8ubyte_unaligned_inc (dbg, readp);
7047 : 182830 : printf (" MIPS_advance_loc8 %" PRIu64 " to %#" PRIx64 "\n",
7048 : 0 : op1, pc += op1 * code_align);
7049 : : break;
7050 : 8 : case DW_CFA_GNU_window_save: /* DW_CFA_AARCH64_negate_ra_state */
7051 [ + - ]: 8 : if (ehdr->e_machine == EM_AARCH64)
7052 : 8 : puts (" AARCH64_negate_ra_state");
7053 : : else
7054 : 0 : puts (" GNU_window_save");
7055 : : break;
7056 : 0 : case DW_CFA_GNU_args_size:
7057 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
7058 : 0 : goto invalid;
7059 : 0 : get_uleb128 (op1, readp, endp);
7060 : 0 : printf (" args_size %" PRIu64 "\n", op1);
7061 : : break;
7062 : : default:
7063 : 182830 : printf (" ??? (%u)\n", opcode);
7064 : : break;
7065 : : }
7066 [ + + ]: 120414 : else if (opcode < DW_CFA_offset)
7067 : 415138 : printf (" advance_loc %u to %#" PRIx64 "\n",
7068 : 89676 : opcode & 0x3f, pc += (opcode & 0x3f) * code_align);
7069 [ + + ]: 30738 : else if (opcode < DW_CFA_restore)
7070 : : {
7071 : 27556 : uint64_t offset;
7072 [ - + ]: 27556 : if ((uint64_t) (endp - readp) < 1)
7073 : 0 : goto invalid;
7074 : 27556 : get_uleb128 (offset, readp, endp);
7075 : 27556 : printf (" offset r%u (%s) at cfa%+" PRId64 "\n",
7076 : : opcode & 0x3f, regname (ebl, opcode & 0x3f, regnamebuf),
7077 : : offset * data_align);
7078 : : }
7079 : : else
7080 [ + + ]: 328644 : printf (" restore r%u (%s)\n",
7081 : : opcode & 0x3f, regname (ebl, opcode & 0x3f, regnamebuf));
7082 : : }
7083 : : }
7084 : :
7085 : :
7086 : : static unsigned int
7087 : 21908 : encoded_ptr_size (int encoding, unsigned int ptr_size)
7088 : : {
7089 [ - + - + ]: 21908 : switch (encoding & 7)
7090 : : {
7091 : : case DW_EH_PE_udata4:
7092 : : return 4;
7093 : 0 : case DW_EH_PE_udata8:
7094 : 0 : return 8;
7095 : : case 0:
7096 : : return ptr_size;
7097 : : }
7098 : :
7099 : 0 : fprintf (stderr, "Unsupported pointer encoding: %#x, "
7100 : : "assuming pointer size of %d.\n", encoding, ptr_size);
7101 : 0 : return ptr_size;
7102 : : }
7103 : :
7104 : :
7105 : : static unsigned int
7106 : 642 : print_encoding (unsigned int val)
7107 : : {
7108 [ - - - + : 642 : switch (val & 0xf)
- - - + -
- ]
7109 : : {
7110 : 0 : case DW_EH_PE_absptr:
7111 : 0 : fputs ("absptr", stdout);
7112 : 0 : break;
7113 : 0 : case DW_EH_PE_uleb128:
7114 : 0 : fputs ("uleb128", stdout);
7115 : 0 : break;
7116 : 0 : case DW_EH_PE_udata2:
7117 : 0 : fputs ("udata2", stdout);
7118 : 0 : break;
7119 : 140 : case DW_EH_PE_udata4:
7120 : 140 : fputs ("udata4", stdout);
7121 : 140 : break;
7122 : 0 : case DW_EH_PE_udata8:
7123 : 0 : fputs ("udata8", stdout);
7124 : 0 : break;
7125 : 0 : case DW_EH_PE_sleb128:
7126 : 0 : fputs ("sleb128", stdout);
7127 : 0 : break;
7128 : 0 : case DW_EH_PE_sdata2:
7129 : 0 : fputs ("sdata2", stdout);
7130 : 0 : break;
7131 : 502 : case DW_EH_PE_sdata4:
7132 : 502 : fputs ("sdata4", stdout);
7133 : 502 : break;
7134 : 0 : case DW_EH_PE_sdata8:
7135 : 0 : fputs ("sdata8", stdout);
7136 : 0 : break;
7137 : : default:
7138 : : /* We did not use any of the bits after all. */
7139 : : return val;
7140 : : }
7141 : :
7142 : 642 : return val & ~0xf;
7143 : : }
7144 : :
7145 : :
7146 : : static unsigned int
7147 : 502 : print_relinfo (unsigned int val)
7148 : : {
7149 [ + - + - : 502 : switch (val & 0x70)
- - ]
7150 : : {
7151 : 362 : case DW_EH_PE_pcrel:
7152 : 362 : fputs ("pcrel", stdout);
7153 : 362 : break;
7154 : 0 : case DW_EH_PE_textrel:
7155 : 0 : fputs ("textrel", stdout);
7156 : 0 : break;
7157 : 140 : case DW_EH_PE_datarel:
7158 : 140 : fputs ("datarel", stdout);
7159 : 140 : break;
7160 : 0 : case DW_EH_PE_funcrel:
7161 : 0 : fputs ("funcrel", stdout);
7162 : 0 : break;
7163 : 0 : case DW_EH_PE_aligned:
7164 : 0 : fputs ("aligned", stdout);
7165 : 0 : break;
7166 : : default:
7167 : : return val;
7168 : : }
7169 : :
7170 : 502 : return val & ~0x70;
7171 : : }
7172 : :
7173 : :
7174 : : static void
7175 : 642 : print_encoding_base (const char *pfx, unsigned int fde_encoding)
7176 : : {
7177 : 642 : printf ("(%s", pfx);
7178 : :
7179 [ - + ]: 642 : if (fde_encoding == DW_EH_PE_omit)
7180 : 0 : puts ("omit)");
7181 : : else
7182 : : {
7183 : 642 : unsigned int w = fde_encoding;
7184 : :
7185 : 642 : w = print_encoding (w);
7186 : :
7187 [ + + ]: 642 : if (w & 0x70)
7188 : : {
7189 [ + - ]: 502 : if (w != fde_encoding)
7190 : 502 : fputc (' ', stdout);
7191 : :
7192 : 502 : w = print_relinfo (w);
7193 : : }
7194 : :
7195 [ - + ]: 642 : if (w != 0)
7196 [ # # ]: 0 : printf ("%s%x", w != fde_encoding ? " " : "", w);
7197 : :
7198 : 642 : puts (")");
7199 : : }
7200 : 642 : }
7201 : :
7202 : :
7203 : : static void
7204 : 196 : print_debug_frame_section (Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr,
7205 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
7206 : : {
7207 : 196 : size_t shstrndx;
7208 : : /* We know this call will succeed since it did in the caller. */
7209 : 196 : (void) elf_getshdrstrndx (ebl->elf, &shstrndx);
7210 : 196 : const char *scnname = section_name (ebl, shdr);
7211 : :
7212 : : /* Needed if we find PC-relative addresses. */
7213 : 196 : GElf_Addr bias;
7214 [ - + ]: 196 : if (dwfl_module_getelf (dwflmod, &bias) == NULL)
7215 : : {
7216 : 0 : error (0, 0, _("cannot get ELF: %s"), dwfl_errmsg (-1));
7217 : 0 : return;
7218 : : }
7219 : :
7220 : 196 : bool is_eh_frame = strcmp (scnname, ".eh_frame") == 0;
7221 : 196 : Elf_Data *data;
7222 [ + + ]: 196 : if (is_eh_frame)
7223 : : {
7224 : 144 : data = elf_rawdata (scn, NULL);
7225 [ - + ]: 144 : if (data == NULL)
7226 : : {
7227 : 0 : error (0, 0, _("cannot get %s content: %s"),
7228 : : scnname, elf_errmsg (-1));
7229 : 0 : return;
7230 : : }
7231 : : }
7232 : : else
7233 : : {
7234 : 52 : data = get_debug_elf_data (dbg, ebl, IDX_debug_frame, scn);
7235 [ + - ]: 52 : if (data == NULL)
7236 : : return;
7237 : : }
7238 : :
7239 [ + + ]: 196 : if (is_eh_frame)
7240 : 144 : printf (_("\
7241 : : \nCall frame information section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
7242 : 144 : elf_ndxscn (scn), scnname, (uint64_t) shdr->sh_offset);
7243 : : else
7244 : 52 : printf (_("\
7245 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
7246 : 52 : elf_ndxscn (scn), scnname, (uint64_t) shdr->sh_offset);
7247 : :
7248 : 196 : struct cieinfo
7249 : : {
7250 : : ptrdiff_t cie_offset;
7251 : : const char *augmentation;
7252 : : unsigned int code_alignment_factor;
7253 : : unsigned int data_alignment_factor;
7254 : : uint8_t address_size;
7255 : : uint8_t fde_encoding;
7256 : : uint8_t lsda_encoding;
7257 : : struct cieinfo *next;
7258 : 196 : } *cies = NULL;
7259 : :
7260 : 196 : const unsigned char *readp = data->d_buf;
7261 : 196 : const unsigned char *const dataend = ((unsigned char *) data->d_buf
7262 : 196 : + data->d_size);
7263 [ + + ]: 22554 : while (readp < dataend)
7264 : : {
7265 [ - + ]: 22358 : if (unlikely (readp + 4 > dataend))
7266 : : {
7267 : 0 : invalid_data:
7268 : 0 : error (0, 0, _("invalid data in section [%zu] '%s'"),
7269 : : elf_ndxscn (scn), scnname);
7270 : 0 : return;
7271 : : }
7272 : :
7273 : : /* At the beginning there must be a CIE. There can be multiple,
7274 : : hence we test tis in a loop. */
7275 : 22358 : ptrdiff_t offset = readp - (unsigned char *) data->d_buf;
7276 : :
7277 [ + + ]: 22358 : Dwarf_Word unit_length = read_4ubyte_unaligned_inc (dbg, readp);
7278 : 22358 : unsigned int length = 4;
7279 [ - + ]: 22358 : if (unlikely (unit_length == 0xffffffff))
7280 : : {
7281 [ # # ]: 0 : if (unlikely (readp + 8 > dataend))
7282 : 0 : goto invalid_data;
7283 : :
7284 [ # # ]: 0 : unit_length = read_8ubyte_unaligned_inc (dbg, readp);
7285 : 0 : length = 8;
7286 : : }
7287 : :
7288 [ + + ]: 22358 : if (unlikely (unit_length == 0))
7289 : : {
7290 : 140 : printf (_("\n [%6tx] Zero terminator\n"), offset);
7291 : 140 : continue;
7292 : : }
7293 : :
7294 : 22218 : Dwarf_Word maxsize = dataend - readp;
7295 [ - + ]: 22218 : if (unlikely (unit_length > maxsize))
7296 : 0 : goto invalid_data;
7297 : :
7298 [ + + ]: 22218 : unsigned int ptr_size = ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 4 : 8;
7299 : :
7300 : 22218 : ptrdiff_t start = readp - (unsigned char *) data->d_buf;
7301 : 22218 : const unsigned char *const cieend = readp + unit_length;
7302 [ - + ]: 22218 : if (unlikely (cieend > dataend))
7303 : 0 : goto invalid_data;
7304 : :
7305 : 22218 : Dwarf_Off cie_id;
7306 [ + - ]: 22218 : if (length == 4)
7307 : : {
7308 [ - + ]: 22218 : if (unlikely (cieend - readp < 4))
7309 : 0 : goto invalid_data;
7310 [ + + ]: 22218 : cie_id = read_4ubyte_unaligned_inc (dbg, readp);
7311 [ + + ]: 22218 : if (!is_eh_frame && cie_id == DW_CIE_ID_32)
7312 : : cie_id = DW_CIE_ID_64;
7313 : : }
7314 : : else
7315 : : {
7316 [ # # ]: 0 : if (unlikely (cieend - readp < 8))
7317 : 0 : goto invalid_data;
7318 [ # # ]: 0 : cie_id = read_8ubyte_unaligned_inc (dbg, readp);
7319 : : }
7320 : :
7321 : 44352 : uint_fast8_t version = 2;
7322 : 22134 : unsigned int code_alignment_factor;
7323 : 22134 : int data_alignment_factor;
7324 : 44352 : unsigned int fde_encoding = 0;
7325 : 44352 : unsigned int lsda_encoding = 0;
7326 : 44352 : Dwarf_Word initial_location = 0;
7327 : 44352 : Dwarf_Word vma_base = 0;
7328 : :
7329 [ + + + + ]: 22310 : if (cie_id == (is_eh_frame ? 0 : DW_CIE_ID_64))
7330 : : {
7331 [ - + ]: 310 : if (unlikely (cieend - readp < 2))
7332 : 0 : goto invalid_data;
7333 : 310 : version = *readp++;
7334 : 310 : const char *const augmentation = (const char *) readp;
7335 : 310 : readp = memchr (readp, '\0', cieend - readp);
7336 [ - + ]: 310 : if (unlikely (readp == NULL))
7337 : 0 : goto invalid_data;
7338 : 310 : ++readp;
7339 : :
7340 : 310 : uint_fast8_t segment_size = 0;
7341 [ + + ]: 310 : if (version >= 4)
7342 : : {
7343 [ - + ]: 8 : if (cieend - readp < 5)
7344 : 0 : goto invalid_data;
7345 : 8 : ptr_size = *readp++;
7346 : 8 : segment_size = *readp++;
7347 : : }
7348 : :
7349 [ - + ]: 310 : if (cieend - readp < 1)
7350 : 0 : goto invalid_data;
7351 : 310 : get_uleb128 (code_alignment_factor, readp, cieend);
7352 [ - + ]: 310 : if (cieend - readp < 1)
7353 : 0 : goto invalid_data;
7354 : 310 : get_sleb128 (data_alignment_factor, readp, cieend);
7355 : :
7356 : : /* In some variant for unwind data there is another field. */
7357 [ - + ]: 310 : if (strcmp (augmentation, "eh") == 0)
7358 [ # # ]: 0 : readp += ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 4 : 8;
7359 : :
7360 : 310 : unsigned int return_address_register;
7361 [ - + ]: 310 : if (cieend - readp < 1)
7362 : 0 : goto invalid_data;
7363 [ + + ]: 310 : if (unlikely (version == 1))
7364 : 286 : return_address_register = *readp++;
7365 : : else
7366 : 24 : get_uleb128 (return_address_register, readp, cieend);
7367 : :
7368 : 310 : printf ("\n [%6tx] CIE length=%" PRIu64 "\n"
7369 : : " CIE_id: %" PRIu64 "\n"
7370 : : " version: %u\n"
7371 : : " augmentation: \"%s\"\n",
7372 : : offset, (uint64_t) unit_length, (uint64_t) cie_id,
7373 : : version, augmentation);
7374 [ + + ]: 310 : if (version >= 4)
7375 : 8 : printf (" address_size: %u\n"
7376 : : " segment_size: %u\n",
7377 : : ptr_size, segment_size);
7378 : 310 : printf (" code_alignment_factor: %u\n"
7379 : : " data_alignment_factor: %d\n"
7380 : : " return_address_register: %u\n",
7381 : : code_alignment_factor,
7382 : : data_alignment_factor, return_address_register);
7383 : :
7384 [ + + ]: 310 : if (augmentation[0] == 'z')
7385 : : {
7386 [ - + ]: 222 : if (cieend - readp < 1)
7387 : 0 : goto invalid_data;
7388 : :
7389 : 222 : unsigned int augmentationlen;
7390 : 222 : get_uleb128 (augmentationlen, readp, cieend);
7391 : :
7392 [ - + ]: 222 : if (augmentationlen > (size_t) (cieend - readp))
7393 : : {
7394 : 0 : error (0, 0, _("invalid augmentation length"));
7395 : 0 : readp = cieend;
7396 : 0 : continue;
7397 : : }
7398 : :
7399 : 222 : const char *hdr = "Augmentation data:";
7400 : 222 : const char *cp = augmentation + 1;
7401 [ + + + - ]: 444 : while (*cp != '\0' && cp < augmentation + augmentationlen + 1)
7402 : : {
7403 : 222 : printf (" %-26s%#x ", hdr, *readp);
7404 : 222 : hdr = "";
7405 : :
7406 [ + - ]: 222 : if (*cp == 'R')
7407 : : {
7408 : 222 : fde_encoding = *readp++;
7409 : 222 : print_encoding_base (_("FDE address encoding: "),
7410 : : fde_encoding);
7411 : : }
7412 [ # # ]: 0 : else if (*cp == 'L')
7413 : : {
7414 : 0 : lsda_encoding = *readp++;
7415 : 0 : print_encoding_base (_("LSDA pointer encoding: "),
7416 : : lsda_encoding);
7417 : : }
7418 [ # # ]: 0 : else if (*cp == 'P')
7419 : : {
7420 : : /* Personality. This field usually has a relocation
7421 : : attached pointing to __gcc_personality_v0. */
7422 : 0 : const unsigned char *startp = readp;
7423 : 0 : unsigned int encoding = *readp++;
7424 : 0 : uint64_t val = 0;
7425 : 0 : readp = read_encoded (encoding, readp,
7426 : : readp - 1 + augmentationlen,
7427 : : &val, dbg);
7428 : :
7429 : 0 : while (++startp < readp)
7430 [ # # ]: 0 : printf ("%#x ", *startp);
7431 : :
7432 : 0 : putchar ('(');
7433 : 0 : print_encoding (encoding);
7434 : 0 : putchar (' ');
7435 [ # # ]: 0 : switch (encoding & 0xf)
7436 : : {
7437 : 0 : case DW_EH_PE_sleb128:
7438 : : case DW_EH_PE_sdata2:
7439 : : case DW_EH_PE_sdata4:
7440 : 0 : printf ("%" PRId64 ")\n", val);
7441 : : break;
7442 : 0 : default:
7443 : 0 : printf ("%#" PRIx64 ")\n", val);
7444 : : break;
7445 : : }
7446 : : }
7447 : : else
7448 : 0 : printf ("(%x)\n", *readp++);
7449 : :
7450 : 222 : ++cp;
7451 : : }
7452 : : }
7453 : :
7454 [ + - ]: 310 : if (likely (ptr_size == 4 || ptr_size == 8))
7455 : : {
7456 : 310 : struct cieinfo *newp = alloca (sizeof (*newp));
7457 : 310 : newp->cie_offset = offset;
7458 : 310 : newp->augmentation = augmentation;
7459 : 310 : newp->fde_encoding = fde_encoding;
7460 : 310 : newp->lsda_encoding = lsda_encoding;
7461 : 310 : newp->address_size = ptr_size;
7462 : 310 : newp->code_alignment_factor = code_alignment_factor;
7463 : 310 : newp->data_alignment_factor = data_alignment_factor;
7464 : 310 : newp->next = cies;
7465 : 310 : cies = newp;
7466 : : }
7467 : : }
7468 : : else
7469 : : {
7470 : : struct cieinfo *cie = cies;
7471 [ + - ]: 21908 : while (cie != NULL)
7472 [ + + - + ]: 43816 : if (is_eh_frame
7473 : 21816 : ? ((Dwarf_Off) start - cie_id) == (Dwarf_Off) cie->cie_offset
7474 : 92 : : cie_id == (Dwarf_Off) cie->cie_offset)
7475 : : break;
7476 : : else
7477 : 0 : cie = cie->next;
7478 [ - + ]: 21908 : if (unlikely (cie == NULL))
7479 : : {
7480 : 0 : puts ("invalid CIE reference in FDE");
7481 : 0 : return;
7482 : : }
7483 : :
7484 : : /* Initialize from CIE data. */
7485 : 21908 : fde_encoding = cie->fde_encoding;
7486 : 21908 : lsda_encoding = cie->lsda_encoding;
7487 : 21908 : ptr_size = encoded_ptr_size (fde_encoding, cie->address_size);
7488 : 21908 : code_alignment_factor = cie->code_alignment_factor;
7489 : 21908 : data_alignment_factor = cie->data_alignment_factor;
7490 : :
7491 : 21908 : const unsigned char *base = readp;
7492 : : // XXX There are sometimes relocations for this value
7493 [ - + + + : 21908 : initial_location = read_addr_unaligned_inc (ptr_size, dbg, readp);
- + + + ]
7494 : 43816 : Dwarf_Word address_range
7495 [ + + - + : 21908 : = read_addr_unaligned_inc (ptr_size, dbg, readp);
+ + ]
7496 : :
7497 : : /* pcrel for an FDE address is relative to the runtime
7498 : : address of the start_address field itself. Sign extend
7499 : : if necessary to make sure the calculation is done on the
7500 : : full 64 bit address even when initial_location only holds
7501 : : the lower 32 bits. */
7502 : 21908 : Dwarf_Addr pc_start = initial_location;
7503 [ + + ]: 21908 : if (ptr_size == 4)
7504 : 21828 : pc_start = (uint64_t) (int32_t) pc_start;
7505 [ + + ]: 21908 : if ((fde_encoding & 0x70) == DW_EH_PE_pcrel)
7506 : 21804 : pc_start += ((uint64_t) shdr->sh_addr
7507 : 21804 : + (base - (const unsigned char *) data->d_buf)
7508 : 21804 : - bias);
7509 : :
7510 : 21908 : printf ("\n [%6tx] FDE length=%" PRIu64 " cie=[%6tx]\n"
7511 : : " CIE_pointer: %" PRIu64 "\n"
7512 : : " initial_location: ",
7513 : : offset, (uint64_t) unit_length,
7514 : : cie->cie_offset, (uint64_t) cie_id);
7515 : 21908 : print_dwarf_addr (dwflmod, cie->address_size,
7516 : : pc_start, initial_location);
7517 [ + + ]: 21908 : if ((fde_encoding & 0x70) == DW_EH_PE_pcrel)
7518 : : {
7519 : 43608 : vma_base = (((uint64_t) shdr->sh_offset
7520 : 21804 : + (base - (const unsigned char *) data->d_buf)
7521 : 21804 : + (uint64_t) initial_location)
7522 : : & (ptr_size == 4
7523 : : ? UINT64_C (0xffffffff)
7524 [ - + ]: 21804 : : UINT64_C (0xffffffffffffffff)));
7525 : 21804 : printf (_(" (offset: %#" PRIx64 ")"),
7526 : : (uint64_t) vma_base);
7527 : : }
7528 : :
7529 : 21908 : printf ("\n address_range: %#" PRIx64,
7530 : : (uint64_t) address_range);
7531 [ + + ]: 21908 : if ((fde_encoding & 0x70) == DW_EH_PE_pcrel)
7532 : 21804 : printf (_(" (end offset: %#" PRIx64 ")"),
7533 : 21804 : ((uint64_t) vma_base + (uint64_t) address_range)
7534 : : & (ptr_size == 4
7535 : : ? UINT64_C (0xffffffff)
7536 [ - + ]: 21804 : : UINT64_C (0xffffffffffffffff)));
7537 : 21908 : putchar ('\n');
7538 : :
7539 [ + + ]: 21908 : if (cie->augmentation[0] == 'z')
7540 : : {
7541 : 21804 : unsigned int augmentationlen;
7542 [ - + ]: 21804 : if (cieend - readp < 1)
7543 : 0 : goto invalid_data;
7544 : 21804 : get_uleb128 (augmentationlen, readp, cieend);
7545 : :
7546 [ - + ]: 21804 : if (augmentationlen > (size_t) (cieend - readp))
7547 : : {
7548 : 0 : error (0, 0, _("invalid augmentation length"));
7549 : 0 : readp = cieend;
7550 : 0 : continue;
7551 : : }
7552 : :
7553 [ - + ]: 21804 : if (augmentationlen > 0)
7554 : : {
7555 : 0 : const char *hdr = "Augmentation data:";
7556 : 0 : const char *cp = cie->augmentation + 1;
7557 : 0 : unsigned int u = 0;
7558 : 0 : while (*cp != '\0'
7559 [ # # # # ]: 0 : && cp < cie->augmentation + augmentationlen + 1)
7560 : : {
7561 [ # # ]: 0 : if (*cp == 'L')
7562 : : {
7563 : 0 : uint64_t lsda_pointer;
7564 : 0 : const unsigned char *p
7565 : 0 : = read_encoded (lsda_encoding, &readp[u],
7566 : : &readp[augmentationlen],
7567 : : &lsda_pointer, dbg);
7568 : 0 : u = p - readp;
7569 : 0 : printf (_("\
7570 : : %-26sLSDA pointer: %#" PRIx64 "\n"),
7571 : : hdr, lsda_pointer);
7572 : 0 : hdr = "";
7573 : : }
7574 : 0 : ++cp;
7575 : : }
7576 : :
7577 [ # # ]: 0 : while (u < augmentationlen)
7578 : : {
7579 : 0 : printf (" %-26s%#x\n", hdr, readp[u++]);
7580 : 0 : hdr = "";
7581 : : }
7582 : : }
7583 : :
7584 : 21804 : readp += augmentationlen;
7585 : : }
7586 : : }
7587 : :
7588 : : /* Handle the initialization instructions. */
7589 [ - + ]: 22218 : if (ptr_size != 4 && ptr_size !=8)
7590 : 0 : printf ("invalid CIE pointer size (%u), must be 4 or 8.\n", ptr_size);
7591 : : else
7592 : 22218 : print_cfa_program (readp, cieend, vma_base, code_alignment_factor,
7593 : : data_alignment_factor, version, ptr_size,
7594 : : fde_encoding, dwflmod, ebl, ehdr, dbg);
7595 : 22218 : readp = cieend;
7596 : : }
7597 : : }
7598 : :
7599 : :
7600 : : /* Returns the signedness (or false if it cannot be determined) and
7601 : : the byte size (or zero if it cannot be gotten) of the given DIE
7602 : : DW_AT_type attribute. Uses dwarf_peel_type and dwarf_aggregate_size. */
7603 : : static void
7604 : 348948 : die_type_sign_bytes (Dwarf_Die *die, bool *is_signed, int *bytes)
7605 : : {
7606 : 348948 : Dwarf_Attribute attr;
7607 : 348948 : Dwarf_Die type;
7608 : :
7609 : 348948 : *bytes = 0;
7610 : 348948 : *is_signed = false;
7611 : :
7612 [ + + ]: 348948 : if (dwarf_peel_type (dwarf_formref_die (dwarf_attr_integrate (die,
7613 : : DW_AT_type,
7614 : : &attr), &type),
7615 : : &type) == 0)
7616 : : {
7617 : 896 : Dwarf_Word val;
7618 : 896 : *is_signed = (dwarf_formudata (dwarf_attr (&type, DW_AT_encoding,
7619 : : &attr), &val) == 0
7620 [ + + + + ]: 896 : && (val == DW_ATE_signed || val == DW_ATE_signed_char));
7621 : :
7622 [ + - ]: 896 : if (dwarf_aggregate_size (&type, &val) == 0)
7623 : 896 : *bytes = val;
7624 : : }
7625 : 348948 : }
7626 : :
7627 : : struct attrcb_args
7628 : : {
7629 : : Dwfl_Module *dwflmod;
7630 : : Dwarf *dbg;
7631 : : Dwarf_Die *dies;
7632 : : int level;
7633 : : bool silent;
7634 : : bool is_split;
7635 : : unsigned int version;
7636 : : unsigned int addrsize;
7637 : : unsigned int offset_size;
7638 : : struct Dwarf_CU *cu;
7639 : : };
7640 : :
7641 : :
7642 : : static int
7643 : 7933928 : attr_callback (Dwarf_Attribute *attrp, void *arg)
7644 : : {
7645 : 7933928 : struct attrcb_args *cbargs = (struct attrcb_args *) arg;
7646 : 7933928 : const int level = cbargs->level;
7647 : 7933928 : Dwarf_Die *die = &cbargs->dies[level];
7648 : 7933928 : bool is_split = cbargs->is_split;
7649 : :
7650 [ + - ]: 7933928 : unsigned int attr = dwarf_whatattr (attrp);
7651 [ - + ]: 7933928 : if (unlikely (attr == 0))
7652 : : {
7653 [ # # ]: 0 : if (!cbargs->silent)
7654 : 0 : error (0, 0, _("DIE [%" PRIx64 "] "
7655 : : "cannot get attribute code: %s"),
7656 : : dwarf_dieoffset (die), dwarf_errmsg (-1));
7657 : 0 : return DWARF_CB_ABORT;
7658 : : }
7659 : :
7660 [ + - ]: 7933928 : unsigned int form = dwarf_whatform (attrp);
7661 [ - + ]: 7933928 : if (unlikely (form == 0))
7662 : : {
7663 [ # # ]: 0 : if (!cbargs->silent)
7664 : 0 : error (0, 0, _("DIE [%" PRIx64 "] "
7665 : : "cannot get attribute form: %s"),
7666 : : dwarf_dieoffset (die), dwarf_errmsg (-1));
7667 : 0 : return DWARF_CB_ABORT;
7668 : : }
7669 : :
7670 [ + + + + : 7933928 : switch (form)
+ + + +
- ]
7671 : : {
7672 : 101378 : case DW_FORM_addr:
7673 : : case DW_FORM_addrx:
7674 : : case DW_FORM_addrx1:
7675 : : case DW_FORM_addrx2:
7676 : : case DW_FORM_addrx3:
7677 : : case DW_FORM_addrx4:
7678 : : case DW_FORM_GNU_addr_index:
7679 [ + + ]: 101378 : if (!cbargs->silent)
7680 : : {
7681 : 100836 : Dwarf_Addr addr;
7682 [ - + ]: 100836 : if (unlikely (dwarf_formaddr (attrp, &addr) != 0))
7683 : : {
7684 : 0 : attrval_out:
7685 [ # # ]: 0 : if (!cbargs->silent)
7686 : 0 : error (0, 0, _("DIE [%" PRIx64 "] "
7687 : : "cannot get attribute '%s' (%s) value: "
7688 : : "%s"),
7689 : : dwarf_dieoffset (die),
7690 : : dwarf_attr_name (attr),
7691 : : dwarf_form_name (form),
7692 : : dwarf_errmsg (-1));
7693 : : /* Don't ABORT, it might be other attributes can be resolved. */
7694 : 0 : return DWARF_CB_OK;
7695 : : }
7696 [ + + ]: 100836 : if (form != DW_FORM_addr )
7697 : : {
7698 : 40 : Dwarf_Word word;
7699 [ - + ]: 40 : if (dwarf_formudata (attrp, &word) != 0)
7700 : 0 : goto attrval_out;
7701 : 40 : printf (" %*s%-20s (%s) [%" PRIx64 "] ",
7702 : : (int) (level * 2), "", dwarf_attr_name (attr),
7703 : : dwarf_form_name (form), word);
7704 : : }
7705 : : else
7706 : 100796 : printf (" %*s%-20s (%s) ",
7707 : : (int) (level * 2), "", dwarf_attr_name (attr),
7708 : : dwarf_form_name (form));
7709 : 100836 : print_dwarf_addr (cbargs->dwflmod, cbargs->addrsize, addr, addr);
7710 : 100836 : printf ("\n");
7711 : : }
7712 : 7136116 : break;
7713 : :
7714 : 1314308 : case DW_FORM_indirect:
7715 : : case DW_FORM_strp:
7716 : : case DW_FORM_line_strp:
7717 : : case DW_FORM_strx:
7718 : : case DW_FORM_strx1:
7719 : : case DW_FORM_strx2:
7720 : : case DW_FORM_strx3:
7721 : : case DW_FORM_strx4:
7722 : : case DW_FORM_string:
7723 : : case DW_FORM_GNU_strp_alt:
7724 : : case DW_FORM_GNU_str_index:
7725 [ + + ]: 1314308 : if (cbargs->silent)
7726 : : break;
7727 : 1312798 : const char *str = dwarf_formstring (attrp);
7728 [ - + ]: 1312798 : if (unlikely (str == NULL))
7729 : 0 : goto attrval_out;
7730 : 1312798 : printf (" %*s%-20s (%s) \"%s\"\n",
7731 : : (int) (level * 2), "", dwarf_attr_name (attr),
7732 : : dwarf_form_name (form), str);
7733 : : break;
7734 : :
7735 : 1716310 : case DW_FORM_ref_addr:
7736 : : case DW_FORM_ref_udata:
7737 : : case DW_FORM_ref8:
7738 : : case DW_FORM_ref4:
7739 : : case DW_FORM_ref2:
7740 : : case DW_FORM_ref1:
7741 : : case DW_FORM_GNU_ref_alt:
7742 : : case DW_FORM_ref_sup4:
7743 : : case DW_FORM_ref_sup8:
7744 [ + + ]: 1716310 : if (cbargs->silent)
7745 : : break;
7746 : 1714730 : Dwarf_Die ref;
7747 [ - + ]: 1714730 : if (unlikely (dwarf_formref_die (attrp, &ref) == NULL))
7748 : 0 : goto attrval_out;
7749 : :
7750 : 1714730 : printf (" %*s%-20s (%s) ",
7751 : : (int) (level * 2), "", dwarf_attr_name (attr),
7752 : : dwarf_form_name (form));
7753 [ + + ]: 1714730 : if (is_split)
7754 : 66 : printf ("{%6" PRIxMAX "}\n", (uintmax_t) dwarf_dieoffset (&ref));
7755 : : else
7756 : 1714664 : printf ("[%6" PRIxMAX "]\n", (uintmax_t) dwarf_dieoffset (&ref));
7757 : : break;
7758 : :
7759 : 8 : case DW_FORM_ref_sig8:
7760 [ + - ]: 8 : if (cbargs->silent)
7761 : : break;
7762 : 8 : printf (" %*s%-20s (%s) {%6" PRIx64 "}\n",
7763 : : (int) (level * 2), "", dwarf_attr_name (attr),
7764 : : dwarf_form_name (form),
7765 [ - + ]: 8 : (uint64_t) read_8ubyte_unaligned (attrp->cu->dbg, attrp->valp));
7766 : : break;
7767 : :
7768 : 4407142 : case DW_FORM_sec_offset:
7769 : : case DW_FORM_rnglistx:
7770 : : case DW_FORM_loclistx:
7771 : : case DW_FORM_implicit_const:
7772 : : case DW_FORM_udata:
7773 : : case DW_FORM_sdata:
7774 : : case DW_FORM_data8: /* Note no data16 here, we see that as block. */
7775 : : case DW_FORM_data4:
7776 : : case DW_FORM_data2:
7777 : 4407142 : case DW_FORM_data1:;
7778 : 4407142 : Dwarf_Word num;
7779 [ - + ]: 4407142 : if (unlikely (dwarf_formudata (attrp, &num) != 0))
7780 : 0 : goto attrval_out;
7781 : :
7782 : 4407142 : const char *valuestr = NULL;
7783 : 4407142 : bool as_hex_id = false;
7784 [ + + + + : 4407142 : switch (attr)
+ + + + +
+ - - - -
- - + - +
+ + ]
7785 : : {
7786 : : /* This case can take either a constant or a loclistptr. */
7787 : 546708 : case DW_AT_data_member_location:
7788 [ + - ]: 546708 : if (form != DW_FORM_sec_offset
7789 [ + + ]: 546708 : && (cbargs->version >= 4
7790 [ + - ]: 47612 : || (form != DW_FORM_data4 && form != DW_FORM_data8)))
7791 : : {
7792 [ + - ]: 546708 : if (!cbargs->silent)
7793 : 546708 : printf (" %*s%-20s (%s) %" PRIuMAX "\n",
7794 : : (int) (level * 2), "", dwarf_attr_name (attr),
7795 : : dwarf_form_name (form), (uintmax_t) num);
7796 : 546708 : return DWARF_CB_OK;
7797 : : }
7798 : 217658 : FALLTHROUGH;
7799 : :
7800 : : /* These cases always take a loclist[ptr] and no constant. */
7801 : : case DW_AT_location:
7802 : : case DW_AT_data_location:
7803 : : case DW_AT_vtable_elem_location:
7804 : : case DW_AT_string_length:
7805 : : case DW_AT_use_location:
7806 : : case DW_AT_frame_base:
7807 : : case DW_AT_return_addr:
7808 : : case DW_AT_static_link:
7809 : : case DW_AT_segment:
7810 : : case DW_AT_GNU_call_site_value:
7811 : : case DW_AT_GNU_call_site_data_value:
7812 : : case DW_AT_GNU_call_site_target:
7813 : : case DW_AT_GNU_call_site_target_clobbered:
7814 : : case DW_AT_GNU_locviews:
7815 : : {
7816 : 217658 : bool nlpt;
7817 [ + + ]: 217658 : if (cbargs->cu->version < 5)
7818 : : {
7819 [ + + ]: 324 : if (! cbargs->is_split)
7820 : : {
7821 : 284 : nlpt = notice_listptr (section_loc, &known_locsptr,
7822 : 284 : cbargs->addrsize,
7823 : 284 : cbargs->offset_size,
7824 : : cbargs->cu, num, attr);
7825 : : }
7826 : : else
7827 : : nlpt = true;
7828 : : }
7829 : : else
7830 : : {
7831 : : /* Only register for a real section offset. Otherwise
7832 : : it is a DW_FORM_loclistx which is just an index
7833 : : number and we should already have registered the
7834 : : section offset for the index when we saw the
7835 : : DW_AT_loclists_base CU attribute. */
7836 [ + + ]: 217334 : if (form == DW_FORM_sec_offset)
7837 : 217256 : nlpt = notice_listptr (section_loc, &known_loclistsptr,
7838 : 217256 : cbargs->addrsize, cbargs->offset_size,
7839 : : cbargs->cu, num, attr);
7840 : : else
7841 : : nlpt = true;
7842 : :
7843 : : }
7844 : :
7845 [ + + ]: 217658 : if (!cbargs->silent)
7846 : : {
7847 [ + + + + ]: 217292 : if (cbargs->cu->version < 5 || form == DW_FORM_sec_offset)
7848 [ - + ]: 217278 : printf (" %*s%-20s (%s) location list [%6"
7849 : : PRIxMAX "]%s\n",
7850 : : (int) (level * 2), "", dwarf_attr_name (attr),
7851 : : dwarf_form_name (form), (uintmax_t) num,
7852 : : nlpt ? "" : " <WARNING offset too big>");
7853 : : else
7854 : 14 : printf (" %*s%-20s (%s) location index [%6"
7855 : : PRIxMAX "]\n",
7856 : : (int) (level * 2), "", dwarf_attr_name (attr),
7857 : : dwarf_form_name (form), (uintmax_t) num);
7858 : : }
7859 : : }
7860 : : return DWARF_CB_OK;
7861 : :
7862 : 10 : case DW_AT_loclists_base:
7863 : : {
7864 : 20 : bool nlpt = notice_listptr (section_loc, &known_loclistsptr,
7865 : 10 : cbargs->addrsize, cbargs->offset_size,
7866 : : cbargs->cu, num, attr);
7867 : :
7868 [ + + ]: 10 : if (!cbargs->silent)
7869 [ - + ]: 2 : printf (" %*s%-20s (%s) location list [%6" PRIxMAX "]%s\n",
7870 : : (int) (level * 2), "", dwarf_attr_name (attr),
7871 : : dwarf_form_name (form), (uintmax_t) num,
7872 : : nlpt ? "" : " <WARNING offset too big>");
7873 : : }
7874 : : return DWARF_CB_OK;
7875 : :
7876 : 33386 : case DW_AT_ranges:
7877 : : case DW_AT_start_scope:
7878 : : {
7879 : 33386 : bool nlpt;
7880 [ + + ]: 33386 : if (cbargs->cu->version < 5)
7881 : 106 : nlpt = notice_listptr (section_ranges, &known_rangelistptr,
7882 : 106 : cbargs->addrsize, cbargs->offset_size,
7883 : : cbargs->cu, num, attr);
7884 : : else
7885 : : {
7886 : : /* Only register for a real section offset. Otherwise
7887 : : it is a DW_FORM_rangelistx which is just an index
7888 : : number and we should already have registered the
7889 : : section offset for the index when we saw the
7890 : : DW_AT_rnglists_base CU attribute. */
7891 [ + + ]: 33280 : if (form == DW_FORM_sec_offset)
7892 : 33268 : nlpt = notice_listptr (section_ranges, &known_rnglistptr,
7893 : 33268 : cbargs->addrsize, cbargs->offset_size,
7894 : : cbargs->cu, num, attr);
7895 : : else
7896 : : nlpt = true;
7897 : : }
7898 : :
7899 [ + + ]: 33386 : if (!cbargs->silent)
7900 : : {
7901 [ + + + + ]: 33300 : if (cbargs->cu->version < 5 || form == DW_FORM_sec_offset)
7902 [ - + ]: 33296 : printf (" %*s%-20s (%s) range list [%6"
7903 : : PRIxMAX "]%s\n",
7904 : : (int) (level * 2), "", dwarf_attr_name (attr),
7905 : : dwarf_form_name (form), (uintmax_t) num,
7906 : : nlpt ? "" : " <WARNING offset too big>");
7907 : : else
7908 : 4 : printf (" %*s%-20s (%s) range index [%6"
7909 : : PRIxMAX "]\n",
7910 : : (int) (level * 2), "", dwarf_attr_name (attr),
7911 : : dwarf_form_name (form), (uintmax_t) num);
7912 : : }
7913 : : }
7914 : : return DWARF_CB_OK;
7915 : :
7916 : 8 : case DW_AT_rnglists_base:
7917 : : {
7918 : 16 : bool nlpt = notice_listptr (section_ranges, &known_rnglistptr,
7919 : 8 : cbargs->addrsize, cbargs->offset_size,
7920 : : cbargs->cu, num, attr);
7921 [ + + ]: 8 : if (!cbargs->silent)
7922 [ - + ]: 4 : printf (" %*s%-20s (%s) range list [%6"
7923 : : PRIxMAX "]%s\n",
7924 : : (int) (level * 2), "", dwarf_attr_name (attr),
7925 : : dwarf_form_name (form), (uintmax_t) num,
7926 : : nlpt ? "" : " <WARNING offset too big>");
7927 : : }
7928 : : return DWARF_CB_OK;
7929 : :
7930 : 34 : case DW_AT_addr_base:
7931 : : case DW_AT_GNU_addr_base:
7932 : : {
7933 : 68 : bool addrbase = notice_listptr (section_addr, &known_addrbases,
7934 : 34 : cbargs->addrsize,
7935 : 34 : cbargs->offset_size,
7936 : : cbargs->cu, num, attr);
7937 [ + + ]: 34 : if (!cbargs->silent)
7938 [ - + ]: 18 : printf (" %*s%-20s (%s) address base [%6"
7939 : : PRIxMAX "]%s\n",
7940 : : (int) (level * 2), "", dwarf_attr_name (attr),
7941 : : dwarf_form_name (form), (uintmax_t) num,
7942 : : addrbase ? "" : " <WARNING offset too big>");
7943 : : }
7944 : : return DWARF_CB_OK;
7945 : :
7946 : 8 : case DW_AT_str_offsets_base:
7947 : : {
7948 : 16 : bool stroffbase = notice_listptr (section_str, &known_stroffbases,
7949 : 8 : cbargs->addrsize,
7950 : 8 : cbargs->offset_size,
7951 : : cbargs->cu, num, attr);
7952 [ + - ]: 8 : if (!cbargs->silent)
7953 [ - + ]: 797812 : printf (" %*s%-20s (%s) str offsets base [%6"
7954 : : PRIxMAX "]%s\n",
7955 : : (int) (level * 2), "", dwarf_attr_name (attr),
7956 : : dwarf_form_name (form), (uintmax_t) num,
7957 : : stroffbase ? "" : " <WARNING offset too big>");
7958 : : }
7959 : : return DWARF_CB_OK;
7960 : :
7961 : 3676 : case DW_AT_language:
7962 : 3676 : valuestr = dwarf_lang_name (num);
7963 : 3676 : break;
7964 : 4 : case DW_AT_language_name:
7965 : 4 : valuestr = dwarf_lname_name (num);
7966 : 4 : break;
7967 : 56482 : case DW_AT_encoding:
7968 : 56482 : valuestr = dwarf_encoding_name (num);
7969 : 56482 : break;
7970 : 0 : case DW_AT_accessibility:
7971 : 0 : valuestr = dwarf_access_name (num);
7972 : 0 : break;
7973 : 0 : case DW_AT_defaulted:
7974 : 0 : valuestr = dwarf_defaulted_name (num);
7975 : 0 : break;
7976 : 0 : case DW_AT_visibility:
7977 : 0 : valuestr = dwarf_visibility_name (num);
7978 : 0 : break;
7979 : 0 : case DW_AT_virtuality:
7980 : 0 : valuestr = dwarf_virtuality_name (num);
7981 : 0 : break;
7982 : 0 : case DW_AT_identifier_case:
7983 : 0 : valuestr = dwarf_identifier_case_name (num);
7984 : 0 : break;
7985 : 0 : case DW_AT_calling_convention:
7986 : 0 : valuestr = dwarf_calling_convention_name (num);
7987 : 0 : break;
7988 : 7138 : case DW_AT_inline:
7989 : 7138 : valuestr = dwarf_inline_name (num);
7990 : 7138 : break;
7991 : 0 : case DW_AT_ordering:
7992 : 0 : valuestr = dwarf_ordering_name (num);
7993 : 0 : break;
7994 : 968822 : case DW_AT_decl_file:
7995 : : case DW_AT_call_file:
7996 : : {
7997 [ + + ]: 968822 : if (cbargs->silent)
7998 : : break;
7999 : :
8000 : : /* Try to get the actual file, the current interface only
8001 : : gives us full paths, but we only want to show the file
8002 : : name for now. */
8003 : 967974 : Dwarf_Die cudie;
8004 [ + - ]: 967974 : if (dwarf_cu_die (cbargs->cu, &cudie,
8005 : : NULL, NULL, NULL, NULL, NULL, NULL) != NULL)
8006 : : {
8007 : 967974 : Dwarf_Files *files;
8008 : 967974 : size_t nfiles;
8009 [ + - ]: 967974 : if (dwarf_getsrcfiles (&cudie, &files, &nfiles) == 0)
8010 : : {
8011 : 967974 : valuestr = dwarf_filesrc (files, num, NULL, NULL);
8012 [ + - ]: 967974 : if (valuestr != NULL)
8013 : : {
8014 : 967974 : char *filename = strrchr (valuestr, '/');
8015 [ + - ]: 967974 : if (filename != NULL)
8016 : 967974 : valuestr = filename + 1;
8017 : : }
8018 : : else
8019 : 0 : error (0, 0, _("invalid file (%" PRId64 "): %s"),
8020 : : num, dwarf_errmsg (-1));
8021 : : }
8022 : : else
8023 : 0 : error (0, 0, _("no srcfiles for CU [%" PRIx64 "]"),
8024 : : dwarf_dieoffset (&cudie));
8025 : : }
8026 : : else
8027 : 0 : error (0, 0, _("couldn't get DWARF CU: %s"),
8028 : : dwarf_errmsg (-1));
8029 : 967974 : if (valuestr == NULL)
8030 : : valuestr = "???";
8031 : : }
8032 : 967974 : break;
8033 : 26 : case DW_AT_GNU_dwo_id:
8034 : 26 : as_hex_id = true;
8035 : 26 : break;
8036 : :
8037 : : default:
8038 : : /* Nothing. */
8039 : : break;
8040 : : }
8041 : :
8042 [ + + ]: 3609330 : if (cbargs->silent)
8043 : : break;
8044 : :
8045 : : /* When highpc is in constant form it is relative to lowpc.
8046 : : In that case also show the address. */
8047 : 3605620 : Dwarf_Addr highpc;
8048 [ + + + - ]: 3605620 : if (attr == DW_AT_high_pc && dwarf_highpc (die, &highpc) == 0)
8049 : : {
8050 : 24736 : printf (" %*s%-20s (%s) %" PRIuMAX " (",
8051 : : (int) (level * 2), "", dwarf_attr_name (attr),
8052 : : dwarf_form_name (form), (uintmax_t) num);
8053 : 24736 : print_dwarf_addr (cbargs->dwflmod, cbargs->addrsize, highpc, highpc);
8054 : 24736 : printf (")\n");
8055 : : }
8056 : : else
8057 : : {
8058 [ + + ]: 3580884 : if (as_hex_id)
8059 : : {
8060 : 4 : printf (" %*s%-20s (%s) 0x%.16" PRIx64 "\n",
8061 : : (int) (level * 2), "", dwarf_attr_name (attr),
8062 : : dwarf_form_name (form), num);
8063 : : }
8064 : : else
8065 : : {
8066 : 3580880 : Dwarf_Sword snum = 0;
8067 : 3580880 : bool is_signed;
8068 : 3580880 : int bytes = 0;
8069 [ + + ]: 3580880 : if (attr == DW_AT_const_value)
8070 : 348940 : die_type_sign_bytes (die, &is_signed, &bytes);
8071 : : else
8072 : 3231940 : is_signed = (form == DW_FORM_sdata
8073 : 3231940 : || form == DW_FORM_implicit_const);
8074 : :
8075 [ + + ]: 3580880 : if (is_signed)
8076 [ - + ]: 373898 : if (unlikely (dwarf_formsdata (attrp, &snum) != 0))
8077 : 0 : goto attrval_out;
8078 : :
8079 [ + + ]: 3580880 : if (valuestr == NULL)
8080 : : {
8081 : 2546216 : printf (" %*s%-20s (%s) ",
8082 : : (int) (level * 2), "", dwarf_attr_name (attr),
8083 : : dwarf_form_name (form));
8084 : : }
8085 : : else
8086 : : {
8087 : 1034664 : printf (" %*s%-20s (%s) %s (",
8088 : : (int) (level * 2), "", dwarf_attr_name (attr),
8089 : : dwarf_form_name (form), valuestr);
8090 : : }
8091 : :
8092 [ + + + + : 3580880 : switch (bytes)
+ ]
8093 : : {
8094 : 10 : case 1:
8095 [ + + ]: 10 : if (is_signed)
8096 : 2 : printf ("%" PRId8, (int8_t) snum);
8097 : : else
8098 : 8 : printf ("%" PRIu8, (uint8_t) num);
8099 : : break;
8100 : :
8101 : 4 : case 2:
8102 [ + + ]: 4 : if (is_signed)
8103 : 2 : printf ("%" PRId16, (int16_t) snum);
8104 : : else
8105 : 2 : printf ("%" PRIu16, (uint16_t) num);
8106 : : break;
8107 : :
8108 : 258 : case 4:
8109 [ + + ]: 258 : if (is_signed)
8110 : 246 : printf ("%" PRId32, (int32_t) snum);
8111 : : else
8112 : 12 : printf ("%" PRIu32, (uint32_t) num);
8113 : : break;
8114 : :
8115 : 616 : case 8:
8116 [ + + ]: 616 : if (is_signed)
8117 : 2 : printf ("%" PRId64, (int64_t) snum);
8118 : : else
8119 : 614 : printf ("%" PRIu64, (uint64_t) num);
8120 : : break;
8121 : :
8122 : 3579992 : default:
8123 [ + + ]: 3579992 : if (is_signed)
8124 : 373646 : printf ("%" PRIdMAX, (intmax_t) snum);
8125 : : else
8126 : 3206346 : printf ("%" PRIuMAX, (uintmax_t) num);
8127 : : break;
8128 : : }
8129 : :
8130 : : /* Make clear if we switched from a signed encoding to
8131 : : an unsigned value. */
8132 [ + + ]: 3580880 : if (attr == DW_AT_const_value
8133 [ + + ]: 348940 : && (form == DW_FORM_sdata || form == DW_FORM_implicit_const)
8134 [ + + ]: 2578 : && !is_signed)
8135 : 2560 : printf (" (%" PRIdMAX ")", (intmax_t) num);
8136 : :
8137 [ + + ]: 3580880 : if (valuestr == NULL)
8138 : 2546216 : printf ("\n");
8139 : : else
8140 : 3580880 : printf (")\n");
8141 : : }
8142 : : }
8143 : : break;
8144 : :
8145 : 18244 : case DW_FORM_flag:
8146 [ + + ]: 18244 : if (cbargs->silent)
8147 : : break;
8148 : 18096 : bool flag;
8149 [ - + ]: 18096 : if (unlikely (dwarf_formflag (attrp, &flag) != 0))
8150 : 0 : goto attrval_out;
8151 : :
8152 : 18096 : printf (" %*s%-20s (%s) %s\n",
8153 : : (int) (level * 2), "", dwarf_attr_name (attr),
8154 [ + - ]: 18096 : dwarf_form_name (form), flag ? yes_str : no_str);
8155 : : break;
8156 : :
8157 : 156202 : case DW_FORM_flag_present:
8158 [ + + ]: 156202 : if (cbargs->silent)
8159 : : break;
8160 : 155648 : printf (" %*s%-20s (%s) %s\n",
8161 : : (int) (level * 2), "", dwarf_attr_name (attr),
8162 : : dwarf_form_name (form), yes_str);
8163 : : break;
8164 : :
8165 : 220336 : case DW_FORM_exprloc:
8166 : : case DW_FORM_block4:
8167 : : case DW_FORM_block2:
8168 : : case DW_FORM_block1:
8169 : : case DW_FORM_block:
8170 : : case DW_FORM_data16: /* DWARF5 calls this a constant class. */
8171 [ + + ]: 220336 : if (cbargs->silent)
8172 : : break;
8173 : 220074 : Dwarf_Block block;
8174 [ - + ]: 220074 : if (unlikely (dwarf_formblock (attrp, &block) != 0))
8175 : 0 : goto attrval_out;
8176 : :
8177 : 220074 : printf (" %*s%-20s (%s) ",
8178 : : (int) (level * 2), "", dwarf_attr_name (attr),
8179 : : dwarf_form_name (form));
8180 : :
8181 [ + + + ]: 220074 : switch (attr)
8182 : : {
8183 : 94866 : default:
8184 [ - + ]: 94866 : if (form != DW_FORM_exprloc)
8185 : : {
8186 : 0 : print_block (block.length, block.data);
8187 : 0 : break;
8188 : : }
8189 : 125200 : FALLTHROUGH;
8190 : :
8191 : : case DW_AT_location:
8192 : : case DW_AT_data_location:
8193 : : case DW_AT_data_member_location:
8194 : : case DW_AT_vtable_elem_location:
8195 : : case DW_AT_string_length:
8196 : : case DW_AT_use_location:
8197 : : case DW_AT_frame_base:
8198 : : case DW_AT_return_addr:
8199 : : case DW_AT_static_link:
8200 : : case DW_AT_allocated:
8201 : : case DW_AT_associated:
8202 : : case DW_AT_bit_size:
8203 : : case DW_AT_bit_offset:
8204 : : case DW_AT_bit_stride:
8205 : : case DW_AT_byte_size:
8206 : : case DW_AT_byte_stride:
8207 : : case DW_AT_count:
8208 : : case DW_AT_lower_bound:
8209 : : case DW_AT_upper_bound:
8210 : : case DW_AT_GNU_call_site_value:
8211 : : case DW_AT_GNU_call_site_data_value:
8212 : : case DW_AT_GNU_call_site_target:
8213 : : case DW_AT_GNU_call_site_target_clobbered:
8214 [ + + ]: 125200 : if (form == DW_FORM_exprloc
8215 [ + - ]: 140 : || (form != DW_FORM_data16
8216 [ + - ]: 140 : && attrp->cu->version < 4)) /* blocks were expressions. */
8217 : : {
8218 : 220066 : putchar ('\n');
8219 : 220066 : print_ops (cbargs->dwflmod, cbargs->dbg,
8220 : 220066 : 12 + level * 2, 12 + level * 2,
8221 : : cbargs->version, cbargs->addrsize, cbargs->offset_size,
8222 : 220066 : attrp->cu, block.length, block.data);
8223 : : }
8224 : : else
8225 : 0 : print_block (block.length, block.data);
8226 : : break;
8227 : :
8228 : 8 : case DW_AT_discr_list:
8229 [ - + ]: 8 : if (block.length == 0)
8230 : 0 : puts ("<default>");
8231 [ + - ]: 8 : else if (form != DW_FORM_data16)
8232 : : {
8233 : 8 : const unsigned char *readp = block.data;
8234 : 8 : const unsigned char *readendp = readp + block.length;
8235 : :
8236 : : /* See if we are dealing with a signed or unsigned
8237 : : values. If the parent of this variant DIE is a
8238 : : variant_part then it will either have a discriminant
8239 : : which points to the member which type is the
8240 : : discriminant type. Or the variant_part itself has a
8241 : : type representing the discriminant. */
8242 : 8 : bool is_signed = false;
8243 [ + - ]: 8 : if (level > 0)
8244 : : {
8245 : 8 : Dwarf_Die *parent = &cbargs->dies[level - 1];
8246 [ + - ]: 8 : if (dwarf_tag (die) == DW_TAG_variant
8247 [ + - ]: 8 : && dwarf_tag (parent) == DW_TAG_variant_part)
8248 : : {
8249 : 8 : Dwarf_Die member;
8250 : 8 : Dwarf_Attribute discr_attr;
8251 : 8 : int bytes;
8252 [ + - ]: 8 : if (dwarf_formref_die (dwarf_attr (parent,
8253 : : DW_AT_discr,
8254 : : &discr_attr),
8255 : : &member) != NULL)
8256 : 8 : die_type_sign_bytes (&member, &is_signed, &bytes);
8257 : : else
8258 : 0 : die_type_sign_bytes (parent, &is_signed, &bytes);
8259 : : }
8260 : : }
8261 : 24 : while (readp < readendp)
8262 : : {
8263 : 16 : int d = (int) *readp++;
8264 : 16 : printf ("%s ", dwarf_discr_list_name (d));
8265 [ - + ]: 16 : if (readp >= readendp)
8266 : 0 : goto attrval_out;
8267 : :
8268 : 16 : Dwarf_Word val;
8269 : 16 : Dwarf_Sword sval;
8270 [ + + ]: 16 : if (d == DW_DSC_label)
8271 : : {
8272 [ + + ]: 8 : if (is_signed)
8273 : : {
8274 : 4 : get_sleb128 (sval, readp, readendp);
8275 : 4 : printf ("%" PRId64 "", sval);
8276 : : }
8277 : : else
8278 : : {
8279 : 4 : get_uleb128 (val, readp, readendp);
8280 : 4 : printf ("%" PRIu64 "", val);
8281 : : }
8282 : : }
8283 [ + - ]: 8 : else if (d == DW_DSC_range)
8284 : : {
8285 [ + + ]: 8 : if (is_signed)
8286 : : {
8287 : 6 : get_sleb128 (sval, readp, readendp);
8288 : 6 : printf ("%" PRId64 "..", sval);
8289 [ - + ]: 6 : if (readp >= readendp)
8290 : 0 : goto attrval_out;
8291 : 6 : get_sleb128 (sval, readp, readendp);
8292 : 6 : printf ("%" PRId64 "", sval);
8293 : : }
8294 : : else
8295 : : {
8296 : 2 : get_uleb128 (val, readp, readendp);
8297 : 2 : printf ("%" PRIu64 "..", val);
8298 [ - + ]: 2 : if (readp >= readendp)
8299 : 0 : goto attrval_out;
8300 : 2 : get_uleb128 (val, readp, readendp);
8301 : 2 : printf ("%" PRIu64 "", val);
8302 : : }
8303 : : }
8304 : : else
8305 : : {
8306 : 0 : print_block (readendp - readp, readp);
8307 : 0 : break;
8308 : : }
8309 [ + + ]: 16 : if (readp < readendp)
8310 [ + + ]: 32 : printf (", ");
8311 : : }
8312 : 8 : putchar ('\n');
8313 : : }
8314 : : else
8315 : 0 : print_block (block.length, block.data);
8316 : : break;
8317 : : }
8318 : : break;
8319 : :
8320 : 0 : default:
8321 [ # # ]: 0 : if (cbargs->silent)
8322 : : break;
8323 : 0 : printf (" %*s%-20s (%s) ???\n",
8324 : : (int) (level * 2), "", dwarf_attr_name (attr),
8325 : : dwarf_form_name (form));
8326 : : break;
8327 : : }
8328 : :
8329 : 7136116 : return DWARF_CB_OK;
8330 : : }
8331 : :
8332 : : static void
8333 : 208 : print_debug_units (Dwfl_Module *dwflmod,
8334 : : Ebl *ebl, GElf_Ehdr *ehdr __attribute__ ((unused)),
8335 : : Elf_Scn *scn, GElf_Shdr *shdr,
8336 : : Dwarf *dbg, bool debug_types)
8337 : : {
8338 [ + + + + ]: 208 : const bool silent = !(print_debug_sections & section_info) && !debug_types;
8339 : 208 : const char *secname = section_name (ebl, shdr);
8340 : :
8341 : : /* Check section actually exists. */
8342 [ + + ]: 208 : if (!silent)
8343 [ + - ]: 136 : if (get_debug_elf_data (dbg, ebl,
8344 : : debug_types ? IDX_debug_types : IDX_debug_info,
8345 : : scn) == NULL)
8346 : 0 : return;
8347 : :
8348 : 136 : if (!silent)
8349 : 136 : printf (_("\
8350 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n [Offset]\n"),
8351 : 136 : elf_ndxscn (scn), secname, (uint64_t) shdr->sh_offset);
8352 : :
8353 : : /* If the section is empty we don't have to do anything. */
8354 [ + + + - ]: 208 : if (!silent && shdr->sh_size == 0)
8355 : : return;
8356 : :
8357 : 208 : int maxdies = 20;
8358 : 208 : Dwarf_Die *dies = xmalloc (maxdies * sizeof (Dwarf_Die));
8359 : :
8360 : : /* New compilation unit. */
8361 : 208 : Dwarf_Half version;
8362 : :
8363 : 208 : Dwarf_Die result;
8364 : 208 : Dwarf_Off abbroffset;
8365 : 208 : uint8_t addrsize;
8366 : 208 : uint8_t offsize;
8367 : 208 : uint64_t unit_id;
8368 : 208 : Dwarf_Off subdie_off;
8369 : :
8370 : 208 : int unit_res;
8371 : 208 : Dwarf_CU *cu;
8372 : 208 : Dwarf_CU cu_mem;
8373 : 208 : uint8_t unit_type;
8374 : 208 : Dwarf_Die cudie;
8375 : :
8376 : : /* We cheat a little because we want to see only the CUs from .debug_info
8377 : : or .debug_types. We know the Dwarf_CU struct layout. Set it up at
8378 : : the end of .debug_info if we want .debug_types only. Check the returned
8379 : : Dwarf_CU is still in the expected section. */
8380 [ + + ]: 208 : if (debug_types)
8381 : : {
8382 : 2 : cu_mem.dbg = dbg;
8383 : 2 : cu_mem.end = dbg->sectiondata[IDX_debug_info]->d_size;
8384 : 2 : cu_mem.sec_idx = IDX_debug_info;
8385 : 2 : cu = &cu_mem;
8386 : : }
8387 : : else
8388 : 206 : cu = NULL;
8389 : :
8390 : : next_cu:
8391 : 3902 : unit_res = dwarf_get_units (dbg, cu, &cu, &version, &unit_type,
8392 : : &cudie, NULL);
8393 [ + + ]: 3902 : if (unit_res == 1)
8394 : 206 : goto do_return;
8395 : :
8396 [ - + ]: 3696 : if (unit_res == -1)
8397 : : {
8398 [ # # ]: 0 : if (!silent)
8399 : 0 : error (0, 0, _("cannot get next unit: %s"), dwarf_errmsg (-1));
8400 : 0 : goto do_return;
8401 : : }
8402 : :
8403 [ + + + + ]: 7388 : if (cu->sec_idx != (size_t) (debug_types ? IDX_debug_types : IDX_debug_info))
8404 : 2 : goto do_return;
8405 : :
8406 : 3694 : dwarf_cu_die (cu, &result, NULL, &abbroffset, &addrsize, &offsize,
8407 : : &unit_id, &subdie_off);
8408 : :
8409 [ + + ]: 3694 : if (!silent)
8410 : : {
8411 : 3594 : Dwarf_Off offset = cu->start;
8412 [ + + + - ]: 3594 : if (debug_types && version < 5)
8413 : 4 : {
8414 : 4 : Dwarf_Die typedie;
8415 : 4 : Dwarf_Off dieoffset;
8416 : 4 : dieoffset = dwarf_dieoffset (dwarf_offdie_types (dbg, cu->start
8417 : : + subdie_off,
8418 : : &typedie));
8419 : 4 : printf (_(" Type unit at offset %" PRIu64 ":\n"
8420 : : " Version: %" PRIu16
8421 : : ", Abbreviation section offset: %" PRIu64
8422 : : ", Address size: %" PRIu8
8423 : : ", Offset size: %" PRIu8
8424 : : "\n Type signature: %#" PRIx64
8425 : : ", Type offset: %#" PRIx64 " [%" PRIx64 "]\n"),
8426 : : (uint64_t) offset, version, abbroffset, addrsize, offsize,
8427 : : unit_id, (uint64_t) subdie_off, dieoffset);
8428 : : }
8429 : : else
8430 : : {
8431 : 3590 : printf (_(" Compilation unit at offset %" PRIu64 ":\n"
8432 : : " Version: %" PRIu16
8433 : : ", Abbreviation section offset: %" PRIu64
8434 : : ", Address size: %" PRIu8
8435 : : ", Offset size: %" PRIu8 "\n"),
8436 : : (uint64_t) offset, version, abbroffset, addrsize, offsize);
8437 : :
8438 [ + + ]: 3590 : if (version >= 5 || (unit_type != DW_UT_compile
8439 [ + + ]: 170 : && unit_type != DW_UT_partial))
8440 : : {
8441 : 3422 : printf (_(" Unit type: %s (%" PRIu8 ")"),
8442 : : dwarf_unit_name (unit_type), unit_type);
8443 : 3422 : if (unit_type == DW_UT_type
8444 [ + + ]: 3422 : || unit_type == DW_UT_skeleton
8445 [ + - ]: 3412 : || unit_type == DW_UT_split_compile
8446 [ - + ]: 3412 : || unit_type == DW_UT_split_type)
8447 : 10 : printf (", Unit id: 0x%.16" PRIx64 "", unit_id);
8448 : 3422 : if (unit_type == DW_UT_type
8449 [ - + ]: 3422 : || unit_type == DW_UT_split_type)
8450 : : {
8451 : 0 : Dwarf_Die typedie;
8452 : 0 : Dwarf_Off dieoffset;
8453 : 0 : dwarf_cu_info (cu, NULL, NULL, NULL, &typedie,
8454 : : NULL, NULL, NULL);
8455 : 0 : dieoffset = dwarf_dieoffset (&typedie);
8456 : 0 : printf (", Unit DIE off: %#" PRIx64 " [%" PRIx64 "]",
8457 : : subdie_off, dieoffset);
8458 : : }
8459 : 3422 : printf ("\n");
8460 : : }
8461 : : }
8462 : : }
8463 : :
8464 [ + - ]: 3694 : if (version < 2 || version > 5
8465 [ + - - + ]: 3694 : || unit_type < DW_UT_compile || unit_type > DW_UT_split_type)
8466 : : {
8467 [ # # ]: 0 : if (!silent)
8468 : 0 : error (0, 0, _("unknown version (%d) or unit type (%d)"),
8469 : : version, unit_type);
8470 : 0 : goto next_cu;
8471 : : }
8472 : :
8473 : 3694 : struct attrcb_args args =
8474 : : {
8475 : : .dwflmod = dwflmod,
8476 : : .silent = silent,
8477 : : .version = version,
8478 : : .addrsize = addrsize,
8479 : : .offset_size = offsize
8480 : : };
8481 : :
8482 : 3694 : bool is_split = false;
8483 : 3694 : int level = 0;
8484 : 3694 : dies[0] = cudie;
8485 : 3694 : args.cu = dies[0].cu;
8486 : 3694 : args.dbg = dbg;
8487 : 3694 : args.is_split = is_split;
8488 : :
8489 : : /* We might return here again for the split CU subdie. */
8490 : : do_cu:
8491 : 2084814 : do
8492 : : {
8493 : 2084814 : Dwarf_Off offset = dwarf_dieoffset (&dies[level]);
8494 [ - + ]: 2084814 : if (unlikely (offset == (Dwarf_Off) -1))
8495 : : {
8496 [ # # ]: 0 : if (!silent)
8497 : 0 : error (0, 0, _("cannot get DIE offset: %s"),
8498 : : dwarf_errmsg (-1));
8499 : 0 : goto do_return;
8500 : : }
8501 : :
8502 : 2084814 : int tag = dwarf_tag (&dies[level]);
8503 [ - + ]: 2084814 : if (unlikely (tag == DW_TAG_invalid))
8504 : : {
8505 [ # # ]: 0 : if (!silent)
8506 : 0 : error (0, 0, _("cannot get tag of DIE at offset [%" PRIx64
8507 : : "] in section '%s': %s"),
8508 : : (uint64_t) offset, secname, dwarf_errmsg (-1));
8509 : 0 : goto do_return;
8510 : : }
8511 : :
8512 [ + + ]: 2084814 : if (!silent)
8513 : : {
8514 : 2082828 : unsigned int code = dwarf_getabbrevcode (dies[level].abbrev);
8515 [ + + ]: 2082828 : if (is_split)
8516 : 96 : printf (" {%6" PRIx64 "} ", (uint64_t) offset);
8517 : : else
8518 : 2082732 : printf (" [%6" PRIx64 "] ", (uint64_t) offset);
8519 : 2082828 : printf ("%*s%-20s abbrev: %u\n", (int) (level * 2), "",
8520 : : dwarf_tag_name (tag), code);
8521 : : }
8522 : :
8523 : : /* Print the attribute values. */
8524 : 2084814 : args.level = level;
8525 : 2084814 : args.dies = dies;
8526 : 2084814 : (void) dwarf_getattrs (&dies[level], attr_callback, &args, 0);
8527 : :
8528 : : /* Make room for the next level's DIE. */
8529 [ - + ]: 2084814 : if (level + 1 == maxdies)
8530 : 0 : dies = xrealloc (dies, (maxdies += 10) * sizeof (Dwarf_Die));
8531 : :
8532 : 2084814 : int res = dwarf_child (&dies[level], &dies[level + 1]);
8533 [ + + ]: 2084814 : if (res > 0)
8534 : : {
8535 [ + + ]: 2084814 : while ((res = dwarf_siblingof (&dies[level], &dies[level])) == 1)
8536 [ + + ]: 288812 : if (level-- == 0)
8537 : : break;
8538 : :
8539 [ - + ]: 1799704 : if (unlikely (res == -1))
8540 : : {
8541 [ # # ]: 0 : if (!silent)
8542 : 0 : error (0, 0, _("cannot get next DIE: %s\n"),
8543 : : dwarf_errmsg (-1));
8544 : 0 : goto do_return;
8545 : : }
8546 : : }
8547 [ - + ]: 285110 : else if (unlikely (res < 0))
8548 : : {
8549 [ # # ]: 0 : if (!silent)
8550 : 0 : error (0, 0, _("cannot get next DIE: %s"),
8551 : : dwarf_errmsg (-1));
8552 : 0 : goto do_return;
8553 : : }
8554 : : else
8555 : : ++level;
8556 : : }
8557 [ + + ]: 2084814 : while (level >= 0);
8558 : :
8559 : : /* We might want to show the split compile unit if this was a skeleton.
8560 : : We need to scan it if we are requesting printing .debug_ranges for
8561 : : DWARF4 since GNU DebugFission uses "offsets" into the main ranges
8562 : : section. */
8563 [ + + ]: 3702 : if (unit_type == DW_UT_skeleton
8564 [ + + + + ]: 26 : && ((!silent && show_split_units)
8565 [ + + + + ]: 20 : || (version < 5 && (print_debug_sections & section_ranges) != 0)))
8566 : : {
8567 : 10 : Dwarf_Die subdie;
8568 [ + - ]: 10 : if (dwarf_cu_info (cu, NULL, NULL, NULL, &subdie, NULL, NULL, NULL) != 0
8569 [ + + ]: 10 : || dwarf_tag (&subdie) == DW_TAG_invalid)
8570 : : {
8571 [ + - ]: 2 : if (!silent)
8572 : : {
8573 : 2 : Dwarf_Attribute dwo_at;
8574 : 4 : const char *dwo_name =
8575 : 2 : (dwarf_formstring (dwarf_attr (&cudie, DW_AT_dwo_name,
8576 : : &dwo_at))
8577 [ - + ]: 2 : ?: (dwarf_formstring (dwarf_attr (&cudie, DW_AT_GNU_dwo_name,
8578 : : &dwo_at))
8579 [ # # ]: 0 : ?: "<unknown>"));
8580 : 2 : fprintf (stderr,
8581 : : "Could not find split unit '%s', id: %" PRIx64 "\n",
8582 : : dwo_name, unit_id);
8583 : : }
8584 : : }
8585 : : else
8586 : : {
8587 : 8 : Dwarf_CU *split_cu = subdie.cu;
8588 : 8 : dwarf_cu_die (split_cu, &result, NULL, &abbroffset,
8589 : : &addrsize, &offsize, &unit_id, &subdie_off);
8590 : 8 : Dwarf_Off offset = cu->start;
8591 : :
8592 [ + + ]: 8 : if (!silent)
8593 : : {
8594 : 4 : printf (_(" Split compilation unit at offset %"
8595 : : PRIu64 ":\n"
8596 : : " Version: %" PRIu16
8597 : : ", Abbreviation section offset: %" PRIu64
8598 : : ", Address size: %" PRIu8
8599 : : ", Offset size: %" PRIu8 "\n"),
8600 : : (uint64_t) offset, version, abbroffset,
8601 : : addrsize, offsize);
8602 : 4 : printf (_(" Unit type: %s (%" PRIu8 ")"),
8603 : : dwarf_unit_name (unit_type), unit_type);
8604 : 4 : printf (", Unit id: 0x%.16" PRIx64 "", unit_id);
8605 : 4 : printf ("\n");
8606 : : }
8607 : :
8608 : 8 : unit_type = DW_UT_split_compile;
8609 : 8 : is_split = true;
8610 : 8 : level = 0;
8611 : 8 : dies[0] = subdie;
8612 : 8 : args.cu = dies[0].cu;
8613 : 8 : args.dbg = split_cu->dbg;
8614 : 8 : args.is_split = is_split;
8615 : 8 : goto do_cu;
8616 : : }
8617 : : }
8618 : :
8619 : : /* And again... */
8620 : 3694 : goto next_cu;
8621 : :
8622 : 208 : do_return:
8623 : 208 : free (dies);
8624 : : }
8625 : :
8626 : : static void
8627 : 206 : print_debug_info_section (Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr,
8628 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
8629 : : {
8630 : 42 : print_debug_units (dwflmod, ebl, ehdr, scn, shdr, dbg, false);
8631 : 42 : }
8632 : :
8633 : : static void
8634 : 2 : print_debug_types_section (Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr,
8635 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
8636 : : {
8637 : 2 : print_debug_units (dwflmod, ebl, ehdr, scn, shdr, dbg, true);
8638 : 2 : }
8639 : :
8640 : :
8641 : : static void
8642 : 82 : print_decoded_line_section (Dwfl_Module *dwflmod, Ebl *ebl,
8643 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
8644 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
8645 : : {
8646 : 82 : printf (_("\
8647 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n\n"),
8648 : : elf_ndxscn (scn), section_name (ebl, shdr),
8649 : 82 : (uint64_t) shdr->sh_offset);
8650 : :
8651 : 164 : size_t address_size
8652 [ + + ]: 82 : = elf_getident (ebl->elf, NULL)[EI_CLASS] == ELFCLASS32 ? 4 : 8;
8653 : :
8654 : 82 : Dwarf_Lines *lines;
8655 : 82 : size_t nlines;
8656 : 82 : Dwarf_Off off, next_off = 0;
8657 : 82 : Dwarf_CU *cu = NULL;
8658 : 82 : while (dwarf_next_lines (dbg, off = next_off, &next_off, &cu, NULL, NULL,
8659 [ + + ]: 176 : &lines, &nlines) == 0)
8660 : : {
8661 : 94 : Dwarf_Die cudie;
8662 [ + + + - ]: 94 : if (cu != NULL && dwarf_cu_info (cu, NULL, NULL, &cudie,
8663 : : NULL, NULL, NULL, NULL) == 0)
8664 : 50 : printf (" CU [%" PRIx64 "] %s\n",
8665 : : dwarf_dieoffset (&cudie), dwarf_diename (&cudie));
8666 : : else
8667 : : {
8668 : : /* DWARF5 lines can be independent of any CU, but they probably
8669 : : are used by some CU. Determine the CU this block is for. */
8670 : 44 : Dwarf_Off cuoffset;
8671 : 44 : Dwarf_Off ncuoffset = 0;
8672 : 44 : size_t hsize;
8673 : 44 : while (dwarf_nextcu (dbg, cuoffset = ncuoffset, &ncuoffset, &hsize,
8674 [ + - ]: 46 : NULL, NULL, NULL) == 0)
8675 : : {
8676 [ - + ]: 46 : if (dwarf_offdie (dbg, cuoffset + hsize, &cudie) == NULL)
8677 : 0 : continue;
8678 : 46 : Dwarf_Attribute stmt_list;
8679 [ - + ]: 46 : if (dwarf_attr (&cudie, DW_AT_stmt_list, &stmt_list) == NULL)
8680 : 0 : continue;
8681 : 46 : Dwarf_Word lineoff;
8682 [ - + ]: 46 : if (dwarf_formudata (&stmt_list, &lineoff) != 0)
8683 : 0 : continue;
8684 [ + + ]: 46 : if (lineoff == off)
8685 : : {
8686 : : /* Found the CU. */
8687 : 44 : cu = cudie.cu;
8688 : 44 : break;
8689 : : }
8690 : : }
8691 : :
8692 [ + - ]: 44 : if (cu != NULL)
8693 : 44 : printf (" CU [%" PRIx64 "] %s\n",
8694 : : dwarf_dieoffset (&cudie), dwarf_diename (&cudie));
8695 : : else
8696 : 44 : printf (" No CU\n");
8697 : : }
8698 : :
8699 : 94 : printf (" line:col SBPE* disc isa op address"
8700 : : " (Statement Block Prologue Epilogue *End)\n");
8701 : 94 : const char *last_file = "";
8702 [ + + ]: 720 : for (size_t n = 0; n < nlines; n++)
8703 : : {
8704 : 626 : Dwarf_Line *line = dwarf_onesrcline (lines, n);
8705 [ - + ]: 626 : if (line == NULL)
8706 : : {
8707 : 0 : printf (" dwarf_onesrcline: %s\n", dwarf_errmsg (-1));
8708 : 0 : continue;
8709 : : }
8710 : 626 : Dwarf_Word mtime, length;
8711 : 626 : const char *file = dwarf_linesrc (line, &mtime, &length);
8712 [ - + ]: 626 : if (file == NULL)
8713 : : {
8714 : 0 : printf (" <%s> (mtime: ?, length: ?)\n", dwarf_errmsg (-1));
8715 : 0 : last_file = "";
8716 : : }
8717 [ + + ]: 626 : else if (strcmp (last_file, file) != 0)
8718 : : {
8719 : 114 : printf (" %s (mtime: %" PRIu64 ", length: %" PRIu64 ")\n",
8720 : : file, mtime, length);
8721 : 114 : last_file = file;
8722 : : }
8723 : :
8724 : 626 : int lineno, colno;
8725 : 626 : bool statement, endseq, block, prologue_end, epilogue_begin;
8726 : 626 : unsigned int lineop, isa, disc;
8727 : 626 : Dwarf_Addr address;
8728 : 626 : dwarf_lineaddr (line, &address);
8729 : 626 : dwarf_lineno (line, &lineno);
8730 : 626 : dwarf_linecol (line, &colno);
8731 : 626 : dwarf_lineop_index (line, &lineop);
8732 : 626 : dwarf_linebeginstatement (line, &statement);
8733 : 626 : dwarf_lineendsequence (line, &endseq);
8734 : 626 : dwarf_lineblock (line, &block);
8735 : 626 : dwarf_lineprologueend (line, &prologue_end);
8736 : 626 : dwarf_lineepiloguebegin (line, &epilogue_begin);
8737 : 626 : dwarf_lineisa (line, &isa);
8738 : 626 : dwarf_linediscriminator (line, &disc);
8739 : :
8740 : : /* End sequence is special, it is one byte past. */
8741 : 626 : printf (" %4d:%-3d %c%c%c%c%c %4d %3d %2d ",
8742 : : lineno, colno,
8743 [ + + ]: 626 : (statement ? 'S' : ' '),
8744 [ + - ]: 626 : (block ? 'B' : ' '),
8745 [ + + ]: 626 : (prologue_end ? 'P' : ' '),
8746 [ + - ]: 626 : (epilogue_begin ? 'E' : ' '),
8747 [ + + ]: 626 : (endseq ? '*' : ' '),
8748 : : disc, isa, lineop);
8749 : 626 : print_dwarf_addr (dwflmod, address_size,
8750 [ + + ]: 626 : address - (endseq ? 1 : 0), address);
8751 : 626 : printf ("\n");
8752 : :
8753 [ + + ]: 626 : if (endseq)
8754 : 626 : printf("\n");
8755 : : }
8756 : : }
8757 : 82 : }
8758 : :
8759 : :
8760 : : /* Print the value of a form.
8761 : : Returns new value of readp, or readendp on failure. */
8762 : : static const unsigned char *
8763 : 141246 : print_form_data (Dwarf *dbg, int form, const unsigned char *readp,
8764 : : const unsigned char *readendp, unsigned int offset_len,
8765 : : Dwarf_Off str_offsets_base)
8766 : : {
8767 : 141246 : Dwarf_Word val;
8768 : 141246 : unsigned char *endp;
8769 : 141246 : Elf_Data *data;
8770 : 141246 : char *str;
8771 [ + - - - : 141246 : switch (form)
- + - - -
- + - - +
- + - - -
- - ]
8772 : : {
8773 : 16 : case DW_FORM_data1:
8774 [ - + ]: 16 : if (readendp - readp < 1)
8775 : : {
8776 : 0 : invalid_data:
8777 : 0 : error (0, 0, "invalid data");
8778 : 0 : return readendp;
8779 : : }
8780 : 16 : val = *readp++;
8781 : 16 : printf (" %" PRIx8, (unsigned int) val);
8782 : : break;
8783 : :
8784 : 0 : case DW_FORM_data2:
8785 [ # # ]: 0 : if (readendp - readp < 2)
8786 : 0 : goto invalid_data;
8787 [ # # ]: 0 : val = read_2ubyte_unaligned_inc (dbg, readp);
8788 : 0 : printf(" %" PRIx16, (unsigned int) val);
8789 : : break;
8790 : :
8791 : 0 : case DW_FORM_data4:
8792 [ # # ]: 0 : if (readendp - readp < 4)
8793 : 0 : goto invalid_data;
8794 [ # # ]: 0 : val = read_4ubyte_unaligned_inc (dbg, readp);
8795 : 0 : printf (" %" PRIx32, (unsigned int) val);
8796 : : break;
8797 : :
8798 : 0 : case DW_FORM_data8:
8799 [ # # ]: 0 : if (readendp - readp < 8)
8800 : 0 : goto invalid_data;
8801 [ # # ]: 0 : val = read_8ubyte_unaligned_inc (dbg, readp);
8802 : 0 : printf (" %" PRIx64, val);
8803 : : break;
8804 : :
8805 : 0 : case DW_FORM_sdata:
8806 [ # # ]: 0 : if (readendp - readp < 1)
8807 : 0 : goto invalid_data;
8808 : 0 : get_sleb128 (val, readp, readendp);
8809 : 0 : printf (" %" PRIx64, val);
8810 : : break;
8811 : :
8812 : 56756 : case DW_FORM_udata:
8813 [ - + ]: 56756 : if (readendp - readp < 1)
8814 : 0 : goto invalid_data;
8815 : 56756 : get_uleb128 (val, readp, readendp);
8816 : 56756 : printf (" %" PRIx64, val);
8817 : : break;
8818 : :
8819 : 0 : case DW_FORM_block:
8820 [ # # ]: 0 : if (readendp - readp < 1)
8821 : 0 : goto invalid_data;
8822 : 0 : get_uleb128 (val, readp, readendp);
8823 [ # # ]: 0 : if ((size_t) (readendp - readp) < val)
8824 : 0 : goto invalid_data;
8825 : 0 : print_bytes (val, readp);
8826 : 0 : readp += val;
8827 : 0 : break;
8828 : :
8829 : 0 : case DW_FORM_block1:
8830 [ # # ]: 0 : if (readendp - readp < 1)
8831 : 0 : goto invalid_data;
8832 : 0 : val = *readp++;
8833 [ # # ]: 0 : if ((size_t) (readendp - readp) < val)
8834 : 0 : goto invalid_data;
8835 : 0 : print_bytes (val, readp);
8836 : 0 : readp += val;
8837 : 0 : break;
8838 : :
8839 : 0 : case DW_FORM_block2:
8840 [ # # ]: 0 : if (readendp - readp < 2)
8841 : 0 : goto invalid_data;
8842 [ # # ]: 0 : val = read_2ubyte_unaligned_inc (dbg, readp);
8843 [ # # ]: 0 : if ((size_t) (readendp - readp) < val)
8844 : 0 : goto invalid_data;
8845 : 0 : print_bytes (val, readp);
8846 : 0 : readp += val;
8847 : 0 : break;
8848 : :
8849 : 0 : case DW_FORM_block4:
8850 [ # # ]: 0 : if (readendp - readp < 4)
8851 : 0 : goto invalid_data;
8852 [ # # ]: 0 : val = read_4ubyte_unaligned_inc (dbg, readp);
8853 [ # # ]: 0 : if ((size_t) (readendp - readp) < val)
8854 : 0 : goto invalid_data;
8855 : 0 : print_bytes (val, readp);
8856 : 0 : readp += val;
8857 : 0 : break;
8858 : :
8859 : 24 : case DW_FORM_data16:
8860 [ - + ]: 24 : if (readendp - readp < 16)
8861 : 0 : goto invalid_data;
8862 : 24 : print_bytes (16, readp);
8863 : 24 : readp += 16;
8864 : 24 : break;
8865 : :
8866 : 0 : case DW_FORM_flag:
8867 [ # # ]: 0 : if (readendp - readp < 1)
8868 : 0 : goto invalid_data;
8869 : 0 : val = *readp++;
8870 [ # # ]: 0 : printf ("%s", val != 0 ? yes_str : no_str);
8871 : : break;
8872 : :
8873 : 0 : case DW_FORM_string:
8874 : 0 : endp = memchr (readp, '\0', readendp - readp);
8875 [ # # ]: 0 : if (endp == NULL)
8876 : 0 : goto invalid_data;
8877 : 0 : printf ("%s", readp);
8878 : 0 : readp = endp + 1;
8879 : 0 : break;
8880 : :
8881 : 82894 : case DW_FORM_strp:
8882 : : case DW_FORM_line_strp:
8883 : : case DW_FORM_strp_sup:
8884 [ - + ]: 82894 : if ((size_t) (readendp - readp) < offset_len)
8885 : 0 : goto invalid_data;
8886 [ - + ]: 82894 : if (offset_len == 8)
8887 [ # # ]: 0 : val = read_8ubyte_unaligned_inc (dbg, readp);
8888 : : else
8889 [ - + ]: 82894 : val = read_4ubyte_unaligned_inc (dbg, readp);
8890 [ - + ]: 82894 : if (form == DW_FORM_strp)
8891 : 0 : data = dbg->sectiondata[IDX_debug_str];
8892 [ + - ]: 82894 : else if (form == DW_FORM_line_strp)
8893 : 82894 : data = dbg->sectiondata[IDX_debug_line_str];
8894 : : else /* form == DW_FORM_strp_sup */
8895 : : {
8896 : 0 : Dwarf *alt = dwarf_getalt (dbg);
8897 [ # # ]: 0 : data = alt != NULL ? alt->sectiondata[IDX_debug_str] : NULL;
8898 : : }
8899 [ + - - + ]: 82894 : if (data == NULL || val >= data->d_size
8900 [ - + ]: 82894 : || memchr (data->d_buf + val, '\0', data->d_size - val) == NULL)
8901 : : str = "???";
8902 : : else
8903 : 82894 : str = (char *) data->d_buf + val;
8904 : 82894 : printf ("%s (%" PRIu64 ")", str, val);
8905 : : break;
8906 : :
8907 : 0 : case DW_FORM_sec_offset:
8908 [ # # ]: 0 : if ((size_t) (readendp - readp) < offset_len)
8909 : 0 : goto invalid_data;
8910 [ # # ]: 0 : if (offset_len == 8)
8911 [ # # ]: 0 : val = read_8ubyte_unaligned_inc (dbg, readp);
8912 : : else
8913 [ # # ]: 0 : val = read_4ubyte_unaligned_inc (dbg, readp);
8914 : 0 : printf ("[%" PRIx64 "]", val);
8915 : : break;
8916 : :
8917 : 1556 : case DW_FORM_strx:
8918 : : case DW_FORM_GNU_str_index:
8919 [ - + ]: 1556 : if (readendp - readp < 1)
8920 : 0 : goto invalid_data;
8921 : 1556 : get_uleb128 (val, readp, readendp);
8922 : 1556 : strx_val:
8923 : 1556 : data = dbg->sectiondata[IDX_debug_str_offsets];
8924 [ + - ]: 1556 : if (data == NULL
8925 [ - + ]: 1556 : || data->d_size - str_offsets_base < val * offset_len)
8926 : : str = "???";
8927 : : else
8928 : : {
8929 : 1556 : const unsigned char *strreadp = (data->d_buf + str_offsets_base
8930 : 1556 : + val * offset_len);
8931 : 1556 : const unsigned char *strreadendp = data->d_buf + data->d_size;
8932 [ - + ]: 1556 : if ((size_t) (strreadendp - strreadp) < offset_len)
8933 : : str = "???";
8934 : : else
8935 : : {
8936 : 1556 : Dwarf_Off idx;
8937 [ - + ]: 1556 : if (offset_len == 8)
8938 [ # # ]: 0 : idx = read_8ubyte_unaligned (dbg, strreadp);
8939 : : else
8940 [ - + ]: 1556 : idx = read_4ubyte_unaligned (dbg, strreadp);
8941 : :
8942 : 1556 : data = dbg->sectiondata[IDX_debug_str];
8943 [ - + - + ]: 1556 : if (data == NULL || idx >= data->d_size
8944 [ - + ]: 1556 : || memchr (data->d_buf + idx, '\0',
8945 : : data->d_size - idx) == NULL)
8946 : : str = "???";
8947 : : else
8948 : 1556 : str = (char *) data->d_buf + idx;
8949 : : }
8950 : : }
8951 : 1556 : printf ("%s (%" PRIu64 ")", str, val);
8952 : : break;
8953 : :
8954 : 0 : case DW_FORM_strx1:
8955 [ # # ]: 0 : if (readendp - readp < 1)
8956 : 0 : goto invalid_data;
8957 : 0 : val = *readp++;
8958 : 0 : goto strx_val;
8959 : :
8960 : 0 : case DW_FORM_strx2:
8961 [ # # ]: 0 : if (readendp - readp < 2)
8962 : 0 : goto invalid_data;
8963 [ # # ]: 0 : val = read_2ubyte_unaligned_inc (dbg, readp);
8964 : 0 : goto strx_val;
8965 : :
8966 : 0 : case DW_FORM_strx3:
8967 [ # # ]: 0 : if (readendp - readp < 3)
8968 : 0 : goto invalid_data;
8969 : 0 : val = read_3ubyte_unaligned_inc (dbg, readp);
8970 : 0 : goto strx_val;
8971 : :
8972 : 0 : case DW_FORM_strx4:
8973 [ # # ]: 0 : if (readendp - readp < 4)
8974 : 0 : goto invalid_data;
8975 [ # # ]: 0 : val = read_4ubyte_unaligned_inc (dbg, readp);
8976 : 0 : goto strx_val;
8977 : :
8978 : 0 : default:
8979 : 0 : error (0, 0, _("unknown form: %s"), dwarf_form_name (form));
8980 : 0 : return readendp;
8981 : : }
8982 : :
8983 : 141246 : return readp;
8984 : : }
8985 : :
8986 : : /* Only used via run_advance_pc() macro */
8987 : : static inline void
8988 : 752184 : run_advance_pc (unsigned int op_advance,
8989 : : unsigned int minimum_instr_len,
8990 : : unsigned int max_ops_per_instr,
8991 : : unsigned int *op_addr_advance,
8992 : : Dwarf_Word *address,
8993 : : unsigned int *op_index)
8994 : : {
8995 : 752184 : const unsigned int advanced_op_index = (*op_index) + op_advance;
8996 : :
8997 : 752184 : *op_addr_advance = minimum_instr_len * (advanced_op_index
8998 : 752184 : / max_ops_per_instr);
8999 : 752184 : *address = *address + *op_addr_advance;
9000 : 752184 : *op_index = advanced_op_index % max_ops_per_instr;
9001 : : }
9002 : :
9003 : : static void
9004 : 186 : print_debug_line_section (Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr,
9005 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
9006 : : {
9007 [ + + ]: 186 : if (decodedline)
9008 : : {
9009 : 82 : print_decoded_line_section (dwflmod, ebl, ehdr, scn, shdr, dbg);
9010 : 164 : return;
9011 : : }
9012 : :
9013 : 104 : Elf_Data *data = get_debug_elf_data (dbg, ebl, IDX_debug_line, scn);
9014 [ + - ]: 104 : if (data == NULL)
9015 : : return;
9016 : :
9017 : 104 : printf (_("\
9018 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
9019 : : elf_ndxscn (scn), section_name (ebl, shdr),
9020 : 104 : (uint64_t) shdr->sh_offset);
9021 : :
9022 [ + - ]: 104 : if (shdr->sh_size == 0)
9023 : : return;
9024 : :
9025 : : /* There is no functionality in libdw to read the information in the
9026 : : way it is represented here. Hardcode the decoder. */
9027 : :
9028 : 104 : const unsigned char *linep = (const unsigned char *) data->d_buf;
9029 : 104 : const unsigned char *lineendp;
9030 : :
9031 : 104 : while (linep
9032 [ + + ]: 3666 : < (lineendp = (const unsigned char *) data->d_buf + data->d_size))
9033 : : {
9034 : 3562 : size_t start_offset = linep - (const unsigned char *) data->d_buf;
9035 : :
9036 : 3562 : printf (_("\nTable at offset %zu:\n"), start_offset);
9037 : :
9038 [ - + ]: 3562 : if (unlikely (linep + 4 > lineendp))
9039 : 0 : goto invalid_data;
9040 [ + + ]: 3562 : Dwarf_Word unit_length = read_4ubyte_unaligned_inc (dbg, linep);
9041 : 3562 : unsigned int length = 4;
9042 [ - + ]: 3562 : if (unlikely (unit_length == 0xffffffff))
9043 : : {
9044 [ # # ]: 0 : if (unlikely (linep + 8 > lineendp))
9045 : : {
9046 : 0 : invalid_data:
9047 : 0 : error (0, 0, _("invalid data in section [%zu] '%s'"),
9048 : : elf_ndxscn (scn), section_name (ebl, shdr));
9049 : 0 : return;
9050 : : }
9051 [ # # ]: 0 : unit_length = read_8ubyte_unaligned_inc (dbg, linep);
9052 : 0 : length = 8;
9053 : : }
9054 : :
9055 : : /* Check whether we have enough room in the section. */
9056 [ - + ]: 3562 : if (unlikely (unit_length > (size_t) (lineendp - linep)))
9057 : 0 : goto invalid_data;
9058 : 3562 : lineendp = linep + unit_length;
9059 : :
9060 : : /* The next element of the header is the version identifier. */
9061 [ - + ]: 3562 : if ((size_t) (lineendp - linep) < 2)
9062 : 0 : goto invalid_data;
9063 [ + + ]: 3562 : uint_fast16_t version = read_2ubyte_unaligned_inc (dbg, linep);
9064 : :
9065 : 7124 : size_t address_size
9066 [ + + ]: 3562 : = elf_getident (ebl->elf, NULL)[EI_CLASS] == ELFCLASS32 ? 4 : 8;
9067 : 3562 : unsigned char segment_selector_size = 0;
9068 [ + + ]: 3562 : if (version > 4)
9069 : : {
9070 [ - + ]: 3410 : if ((size_t) (lineendp - linep) < 2)
9071 : 0 : goto invalid_data;
9072 : 3410 : address_size = *linep++;
9073 : 3410 : segment_selector_size = *linep++;
9074 : : }
9075 : :
9076 : : /* Next comes the header length. */
9077 : 3562 : Dwarf_Word header_length;
9078 [ + - ]: 3562 : if (length == 4)
9079 : : {
9080 [ - + ]: 3562 : if ((size_t) (lineendp - linep) < 4)
9081 : 0 : goto invalid_data;
9082 [ + + ]: 3562 : header_length = read_4ubyte_unaligned_inc (dbg, linep);
9083 : : }
9084 : : else
9085 : : {
9086 [ # # ]: 0 : if ((size_t) (lineendp - linep) < 8)
9087 : 0 : goto invalid_data;
9088 [ # # ]: 0 : header_length = read_8ubyte_unaligned_inc (dbg, linep);
9089 : : }
9090 : :
9091 : 3562 : const unsigned char *header_start = linep;
9092 : :
9093 : : /* Next the minimum instruction length. */
9094 [ - + ]: 3562 : if ((size_t) (lineendp - linep) < 1)
9095 : 0 : goto invalid_data;
9096 : 3562 : uint_fast8_t minimum_instr_len = *linep++;
9097 : :
9098 : : /* Next the maximum operations per instruction, in version 4 format. */
9099 : 3562 : uint_fast8_t max_ops_per_instr;
9100 [ + + ]: 3562 : if (version < 4)
9101 : : max_ops_per_instr = 1;
9102 : : else
9103 : : {
9104 [ - + ]: 3430 : if ((size_t) (lineendp - linep) < 1)
9105 : 0 : goto invalid_data;
9106 : 3430 : max_ops_per_instr = *linep++;
9107 : : }
9108 : :
9109 : : /* We need at least 4 more bytes. */
9110 [ - + ]: 3562 : if ((size_t) (lineendp - linep) < 4)
9111 : 0 : goto invalid_data;
9112 : :
9113 : : /* Then the flag determining the default value of the is_stmt
9114 : : register. */
9115 : 3562 : uint_fast8_t default_is_stmt = *linep++;
9116 : :
9117 : : /* Now the line base. */
9118 : 3562 : int_fast8_t line_base = *linep++;
9119 : :
9120 : : /* And the line range. */
9121 : 3562 : uint_fast8_t line_range = *linep++;
9122 : :
9123 : : /* The opcode base. */
9124 : 3562 : uint_fast8_t opcode_base = *linep++;
9125 : :
9126 : : /* Print what we got so far. */
9127 : 3562 : printf (_("\n"
9128 : : " Length: %" PRIu64 "\n"
9129 : : " DWARF version: %" PRIuFAST16 "\n"
9130 : : " Prologue length: %" PRIu64 "\n"
9131 : : " Address size: %zd\n"
9132 : : " Segment selector size: %zd\n"
9133 : : " Min instruction length: %" PRIuFAST8 "\n"
9134 : : " Max operations per instruction: %" PRIuFAST8 "\n"
9135 : : " Initial value if 'is_stmt': %" PRIuFAST8 "\n"
9136 : : " Line base: %" PRIdFAST8 "\n"
9137 : : " Line range: %" PRIuFAST8 "\n"
9138 : : " Opcode base: %" PRIuFAST8 "\n"
9139 : : "\n"
9140 : : "Opcodes:\n"),
9141 : : (uint64_t) unit_length, version, (uint64_t) header_length,
9142 : : address_size, (size_t) segment_selector_size,
9143 : : minimum_instr_len, max_ops_per_instr,
9144 : : default_is_stmt, line_base,
9145 : : line_range, opcode_base);
9146 : :
9147 [ - + ]: 3562 : if (version < 2 || version > 5)
9148 : : {
9149 : 0 : error (0, 0, _("cannot handle .debug_line version: %u\n"),
9150 : : (unsigned int) version);
9151 : 0 : linep = lineendp;
9152 : 0 : continue;
9153 : : }
9154 : :
9155 [ - + ]: 3562 : if (address_size != 4 && address_size != 8)
9156 : : {
9157 : 0 : error (0, 0, _("cannot handle address size: %u\n"),
9158 : : (unsigned int) address_size);
9159 : 0 : linep = lineendp;
9160 : 0 : continue;
9161 : : }
9162 : :
9163 [ - + ]: 3562 : if (segment_selector_size != 0)
9164 : : {
9165 : 0 : error (0, 0, _("cannot handle segment selector size: %u\n"),
9166 : : (unsigned int) segment_selector_size);
9167 : 0 : linep = lineendp;
9168 : 0 : continue;
9169 : : }
9170 : :
9171 [ - + ]: 3562 : if (unlikely (linep + opcode_base - 1 >= lineendp))
9172 : : {
9173 : 0 : invalid_unit:
9174 : 0 : error (0, 0,
9175 : 0 : _("invalid data at offset %tu in section [%zu] '%s'"),
9176 : 0 : linep - (const unsigned char *) data->d_buf,
9177 : : elf_ndxscn (scn), section_name (ebl, shdr));
9178 : 0 : linep = lineendp;
9179 : 0 : continue;
9180 : : }
9181 : 3562 : int opcode_base_l10 = 1;
9182 : 3562 : unsigned int tmp = opcode_base;
9183 [ + + ]: 7118 : while (tmp > 10)
9184 : : {
9185 : 3556 : tmp /= 10;
9186 : 3556 : ++opcode_base_l10;
9187 : : }
9188 : : const uint8_t *standard_opcode_lengths = linep - 1;
9189 [ + + ]: 46288 : for (uint_fast8_t cnt = 1; cnt < opcode_base; ++cnt)
9190 : 42726 : printf (ngettext (" [%*" PRIuFAST8 "] %hhu argument\n",
9191 : : " [%*" PRIuFAST8 "] %hhu arguments\n",
9192 : : (int) linep[cnt - 1]),
9193 : 42726 : opcode_base_l10, cnt, linep[cnt - 1]);
9194 : 3562 : linep += opcode_base - 1;
9195 : :
9196 : 3562 : if (unlikely (linep >= lineendp))
9197 : : goto invalid_unit;
9198 : :
9199 : 3562 : Dwarf_Off str_offsets_base = str_offsets_base_off (dbg, NULL);
9200 : :
9201 : 3562 : puts (_("\nDirectory table:"));
9202 [ + + ]: 3562 : if (version > 4)
9203 : : {
9204 : 3410 : struct encpair { uint16_t desc; uint16_t form; };
9205 : 3410 : struct encpair enc[256];
9206 : :
9207 : 3410 : printf (_(" ["));
9208 [ - + ]: 3410 : if ((size_t) (lineendp - linep) < 1)
9209 : 0 : goto invalid_data;
9210 : 3410 : unsigned char directory_entry_format_count = *linep++;
9211 : 3410 : for (int i = 0; i < directory_entry_format_count; i++)
9212 : : {
9213 : 3410 : uint16_t desc, form;
9214 [ - + ]: 3410 : if ((size_t) (lineendp - linep) < 1)
9215 : 0 : goto invalid_data;
9216 : 3410 : get_uleb128 (desc, linep, lineendp);
9217 [ - + ]: 3410 : if ((size_t) (lineendp - linep) < 1)
9218 : 0 : goto invalid_data;
9219 : 3410 : get_uleb128 (form, linep, lineendp);
9220 : :
9221 : 3410 : enc[i].desc = desc;
9222 : 3410 : enc[i].form = form;
9223 : :
9224 : 3410 : printf ("%s(%s)",
9225 : : dwarf_line_content_description_name (desc),
9226 : : dwarf_form_name (form));
9227 [ - + ]: 3410 : if (i + 1 < directory_entry_format_count)
9228 [ + + ]: 6820 : printf (", ");
9229 : : }
9230 : 3410 : printf ("]\n");
9231 : :
9232 : 3410 : uint64_t directories_count;
9233 [ - + ]: 3410 : if ((size_t) (lineendp - linep) < 1)
9234 : 0 : goto invalid_data;
9235 : 3410 : get_uleb128 (directories_count, linep, lineendp);
9236 : :
9237 : 3410 : if (directory_entry_format_count == 0
9238 [ - + ]: 3410 : && directories_count != 0)
9239 : 0 : goto invalid_data;
9240 : :
9241 [ + + ]: 29532 : for (uint64_t i = 0; i < directories_count; i++)
9242 : : {
9243 : 26122 : printf (" %-5" PRIu64 " ", i);
9244 : 26122 : for (int j = 0; j < directory_entry_format_count; j++)
9245 : : {
9246 : 26122 : linep = print_form_data (dbg, enc[j].form,
9247 : : linep, lineendp, length,
9248 : : str_offsets_base);
9249 [ - + ]: 26122 : if (j + 1 < directory_entry_format_count)
9250 [ + + ]: 52244 : printf (", ");
9251 : : }
9252 : 26122 : printf ("\n");
9253 [ - + ]: 26122 : if (linep >= lineendp)
9254 : 0 : goto invalid_unit;
9255 : : }
9256 : : }
9257 : : else
9258 : : {
9259 [ + - + + ]: 766 : while (linep < lineendp && *linep != 0)
9260 : : {
9261 : 614 : unsigned char *endp = memchr (linep, '\0', lineendp - linep);
9262 [ - + ]: 614 : if (unlikely (endp == NULL))
9263 : 0 : goto invalid_unit;
9264 : :
9265 : 614 : printf (" %s\n", (char *) linep);
9266 : :
9267 : 614 : linep = endp + 1;
9268 : : }
9269 [ + - - + ]: 152 : if (linep >= lineendp || *linep != 0)
9270 : 0 : goto invalid_unit;
9271 : : /* Skip the final NUL byte. */
9272 : 152 : ++linep;
9273 : : }
9274 : :
9275 [ - + ]: 3562 : if (unlikely (linep >= lineendp))
9276 : 0 : goto invalid_unit;
9277 : :
9278 : 3562 : puts (_("\nFile name table:"));
9279 [ + + ]: 3562 : if (version > 4)
9280 : : {
9281 : 3410 : struct encpair { uint16_t desc; uint16_t form; };
9282 : 3410 : struct encpair enc[256];
9283 : :
9284 : 3410 : printf (_(" ["));
9285 [ - + ]: 3410 : if ((size_t) (lineendp - linep) < 1)
9286 : 0 : goto invalid_data;
9287 : 3410 : unsigned char file_name_format_count = *linep++;
9288 : 3410 : for (int i = 0; i < file_name_format_count; i++)
9289 : : {
9290 : 6828 : uint64_t desc, form;
9291 [ - + ]: 6828 : if ((size_t) (lineendp - linep) < 1)
9292 : 0 : goto invalid_data;
9293 : 6828 : get_uleb128 (desc, linep, lineendp);
9294 [ - + ]: 6828 : if ((size_t) (lineendp - linep) < 1)
9295 : 0 : goto invalid_data;
9296 : 6828 : get_uleb128 (form, linep, lineendp);
9297 : :
9298 [ - + ]: 6828 : if (! libdw_valid_user_form (form))
9299 : 0 : goto invalid_data;
9300 : :
9301 : 6828 : enc[i].desc = desc;
9302 : 6828 : enc[i].form = form;
9303 : :
9304 : 6828 : printf ("%s(%s)",
9305 : : dwarf_line_content_description_name (desc),
9306 : : dwarf_form_name (form));
9307 [ + + ]: 6828 : if (i + 1 < file_name_format_count)
9308 [ + + ]: 13656 : printf (", ");
9309 : : }
9310 : 3410 : printf ("]\n");
9311 : :
9312 : 3410 : uint64_t file_name_count;
9313 [ - + ]: 3410 : if ((size_t) (lineendp - linep) < 1)
9314 : 0 : goto invalid_data;
9315 : 3410 : get_uleb128 (file_name_count, linep, lineendp);
9316 : :
9317 : 3410 : if (file_name_format_count == 0
9318 [ - + ]: 3410 : && file_name_count != 0)
9319 : 0 : goto invalid_data;
9320 : :
9321 [ + + ]: 60182 : for (uint64_t i = 0; i < file_name_count; i++)
9322 : : {
9323 : 56772 : printf (" %-5" PRIu64 " ", i);
9324 : 56772 : for (int j = 0; j < file_name_format_count; j++)
9325 : : {
9326 : 113568 : linep = print_form_data (dbg, enc[j].form,
9327 : : linep, lineendp, length,
9328 : : str_offsets_base);
9329 [ + + ]: 113568 : if (j + 1 < file_name_format_count)
9330 [ + + ]: 227136 : printf (", ");
9331 : : }
9332 : 56772 : printf ("\n");
9333 [ - + ]: 56772 : if (linep > lineendp)
9334 : 0 : goto invalid_unit;
9335 : : }
9336 : : }
9337 : : else
9338 : : {
9339 : 152 : puts (_(" Entry Dir Time Size Name"));
9340 [ + - + + ]: 6154 : for (unsigned int cnt = 1; linep < lineendp && *linep != 0; ++cnt)
9341 : : {
9342 : : /* First comes the file name. */
9343 : 6002 : char *fname = (char *) linep;
9344 : 6002 : unsigned char *endp = memchr (fname, '\0', lineendp - linep);
9345 [ - + ]: 6002 : if (unlikely (endp == NULL))
9346 : 0 : goto invalid_unit;
9347 : 6002 : linep = endp + 1;
9348 : :
9349 : : /* Then the index. */
9350 : 6002 : unsigned int diridx;
9351 [ - + ]: 6002 : if (lineendp - linep < 1)
9352 : 0 : goto invalid_unit;
9353 : 6002 : get_uleb128 (diridx, linep, lineendp);
9354 : :
9355 : : /* Next comes the modification time. */
9356 : 6002 : unsigned int mtime;
9357 [ - + ]: 6002 : if (lineendp - linep < 1)
9358 : 0 : goto invalid_unit;
9359 : 6002 : get_uleb128 (mtime, linep, lineendp);
9360 : :
9361 : : /* Finally the length of the file. */
9362 : 6002 : unsigned int fsize;
9363 [ - + ]: 6002 : if (lineendp - linep < 1)
9364 : 0 : goto invalid_unit;
9365 : 6002 : get_uleb128 (fsize, linep, lineendp);
9366 : :
9367 : 6002 : printf (" %-5u %-5u %-9u %-9u %s\n",
9368 : : cnt, diridx, mtime, fsize, fname);
9369 : : }
9370 [ + - - + ]: 152 : if (linep >= lineendp || *linep != '\0')
9371 : 0 : goto invalid_unit;
9372 : : /* Skip the final NUL byte. */
9373 : 152 : ++linep;
9374 : : }
9375 : :
9376 : 3562 : unsigned int debug_str_offset = 0;
9377 [ + + ]: 3562 : if (unlikely (linep == header_start + header_length - 4))
9378 : : {
9379 : : /* CUBINs contain an unsigned 4-byte offset */
9380 [ - + ]: 2 : debug_str_offset = read_4ubyte_unaligned_inc (dbg, linep);
9381 : : }
9382 : :
9383 [ + + ]: 3562 : if (linep == lineendp)
9384 : : {
9385 : 40 : puts (_("\nNo line number statements."));
9386 : 40 : continue;
9387 : : }
9388 : :
9389 : 3522 : puts (_("\nLine number statements:"));
9390 : 3522 : Dwarf_Word address = 0;
9391 : 3522 : unsigned int op_index = 0;
9392 : 3522 : size_t line = 1;
9393 : 3522 : uint_fast8_t is_stmt = default_is_stmt;
9394 : :
9395 : : /* Apply the "operation advance" from a special opcode
9396 : : or DW_LNS_advance_pc (as per DWARF4 6.2.5.1). */
9397 : 3522 : unsigned int op_addr_advance;
9398 : : #define advance_pc(op_advance) run_advance_pc(op_advance, minimum_instr_len, \
9399 : : max_ops_per_instr, &op_addr_advance, &address, &op_index)
9400 : :
9401 [ - + ]: 3522 : if (max_ops_per_instr == 0)
9402 : : {
9403 : 0 : error (0, 0,
9404 : 0 : _("invalid maximum operations per instruction is zero"));
9405 : 0 : linep = lineendp;
9406 : 0 : continue;
9407 : : }
9408 : :
9409 : 2367388 : while (linep < lineendp)
9410 : : {
9411 : 2363866 : size_t offset = linep - (const unsigned char *) data->d_buf;
9412 : 2363866 : unsigned int u128;
9413 : 2363866 : int s128;
9414 : :
9415 : : /* Read the opcode. */
9416 : 2363866 : unsigned int opcode = *linep++;
9417 : :
9418 : 2363866 : printf (" [%6" PRIx64 "]", (uint64_t)offset);
9419 : : /* Is this a special opcode? */
9420 [ + + ]: 2363866 : if (likely (opcode >= opcode_base))
9421 : : {
9422 [ - + ]: 683684 : if (unlikely (line_range == 0))
9423 : 0 : goto invalid_unit;
9424 : :
9425 : : /* Yes. Handling this is quite easy since the opcode value
9426 : : is computed with
9427 : :
9428 : : opcode = (desired line increment - line_base)
9429 : : + (line_range * address advance) + opcode_base
9430 : : */
9431 : 683684 : int line_increment = (line_base
9432 : 683684 : + (opcode - opcode_base) % line_range);
9433 : :
9434 : : /* Perform the increments. */
9435 : 683684 : line += line_increment;
9436 : 683684 : advance_pc ((opcode - opcode_base) / line_range);
9437 : :
9438 : 683684 : printf (_(" special opcode %u: address+%u = "),
9439 : : opcode, op_addr_advance);
9440 : 683684 : print_dwarf_addr (dwflmod, 0, address, address);
9441 [ - + ]: 683684 : if (op_index > 0)
9442 : 0 : printf (_(", op_index = %u, line%+d = %zu\n"),
9443 : : op_index, line_increment, line);
9444 : : else
9445 : 683684 : printf (_(", line%+d = %zu\n"),
9446 : : line_increment, line);
9447 : : }
9448 [ + + ]: 1680182 : else if (opcode == 0)
9449 : : {
9450 : : /* This an extended opcode. */
9451 [ - + ]: 128328 : if (unlikely (linep + 2 > lineendp))
9452 : 0 : goto invalid_unit;
9453 : :
9454 : : /* The length. */
9455 : 128328 : unsigned int len = *linep++;
9456 : :
9457 [ - + ]: 128328 : if (unlikely (linep + len > lineendp))
9458 : 0 : goto invalid_unit;
9459 : :
9460 : : /* The sub-opcode. */
9461 : 128328 : opcode = *linep++;
9462 : :
9463 : 128328 : printf (_(" extended opcode %u: "), opcode);
9464 : :
9465 [ + + - + : 128328 : switch (opcode)
+ - - ]
9466 : : {
9467 : 9812 : case DW_LNE_end_sequence:
9468 : 9812 : puts (_(" end of sequence"));
9469 : :
9470 : : /* Reset the registers we care about. */
9471 : 9812 : address = 0;
9472 : 9812 : op_index = 0;
9473 : 9812 : line = 1;
9474 : 9812 : is_stmt = default_is_stmt;
9475 : 9812 : break;
9476 : :
9477 : 11042 : case DW_LNE_set_address:
9478 : 11042 : op_index = 0;
9479 [ - + ]: 11042 : if (unlikely ((size_t) (lineendp - linep) < address_size))
9480 : 0 : goto invalid_unit;
9481 [ + + ]: 11042 : if (address_size == 4)
9482 [ + + ]: 50 : address = read_4ubyte_unaligned_inc (dbg, linep);
9483 : : else
9484 [ + + ]: 10992 : address = read_8ubyte_unaligned_inc (dbg, linep);
9485 : : {
9486 : 11042 : printf (_(" set address to "));
9487 : 11042 : print_dwarf_addr (dwflmod, 0, address, address);
9488 : 11042 : printf ("\n");
9489 : : }
9490 : : break;
9491 : :
9492 : 0 : case DW_LNE_define_file:
9493 : : {
9494 : 0 : char *fname = (char *) linep;
9495 : 0 : unsigned char *endp = memchr (linep, '\0',
9496 : 0 : lineendp - linep);
9497 [ # # ]: 0 : if (unlikely (endp == NULL))
9498 : 0 : goto invalid_unit;
9499 : 0 : linep = endp + 1;
9500 : :
9501 : 0 : unsigned int diridx;
9502 [ # # ]: 0 : if (lineendp - linep < 1)
9503 : 0 : goto invalid_unit;
9504 : 0 : get_uleb128 (diridx, linep, lineendp);
9505 : 0 : Dwarf_Word mtime;
9506 [ # # ]: 0 : if (lineendp - linep < 1)
9507 : 0 : goto invalid_unit;
9508 : 0 : get_uleb128 (mtime, linep, lineendp);
9509 : 0 : Dwarf_Word filelength;
9510 [ # # ]: 0 : if (lineendp - linep < 1)
9511 : 0 : goto invalid_unit;
9512 : 0 : get_uleb128 (filelength, linep, lineendp);
9513 : :
9514 : 0 : printf (_("\
9515 : : define new file: dir=%u, mtime=%" PRIu64 ", length=%" PRIu64 ", name=%s\n"),
9516 : : diridx, (uint64_t) mtime, (uint64_t) filelength,
9517 : : fname);
9518 : : }
9519 : : break;
9520 : :
9521 : 107460 : case DW_LNE_set_discriminator:
9522 : : /* Takes one ULEB128 parameter, the discriminator. */
9523 [ + - - + ]: 107460 : if (unlikely (standard_opcode_lengths[opcode] != 1
9524 : : || lineendp - linep < 1))
9525 : 0 : goto invalid_unit;
9526 : :
9527 : 107460 : get_uleb128 (u128, linep, lineendp);
9528 : 107460 : printf (_(" set discriminator to %u\n"), u128);
9529 : : break;
9530 : :
9531 : 14 : case DW_LNE_NVIDIA_inlined_call:
9532 : : {
9533 [ - + ]: 14 : if (unlikely (linep >= lineendp))
9534 : 0 : goto invalid_data;
9535 : :
9536 : 14 : unsigned int context;
9537 : 14 : get_uleb128 (context, linep, lineendp);
9538 : :
9539 [ - + ]: 14 : if (unlikely (linep >= lineendp))
9540 : 0 : goto invalid_data;
9541 : :
9542 : 14 : unsigned int function_name;
9543 : 14 : get_uleb128 (function_name, linep, lineendp);
9544 : 14 : function_name += debug_str_offset;
9545 : :
9546 : 14 : Elf_Data *str_data = dbg->sectiondata[IDX_debug_str];
9547 : 14 : char *function_str;
9548 [ + - - + ]: 14 : if (str_data == NULL || function_name >= str_data->d_size
9549 [ - + ]: 14 : || memchr (str_data->d_buf + function_name, '\0',
9550 : : str_data->d_size - function_name) == NULL)
9551 : : function_str = "???";
9552 : : else
9553 : 14 : function_str = (char *) str_data->d_buf + function_name;
9554 : :
9555 : 14 : printf (_(" set inlined context %u,"
9556 : : " function name %s (0x%x)\n"),
9557 : : context, function_str, function_name);
9558 : : break;
9559 : : }
9560 : :
9561 : 0 : case DW_LNE_NVIDIA_set_function_name:
9562 : : {
9563 [ # # ]: 0 : if (unlikely (linep >= lineendp))
9564 : 0 : goto invalid_data;
9565 : :
9566 : 0 : unsigned int function_name;
9567 : 0 : get_uleb128 (function_name, linep, lineendp);
9568 : 0 : function_name += debug_str_offset;
9569 : :
9570 : 0 : Elf_Data *str_data = dbg->sectiondata[IDX_debug_str];
9571 : 0 : char *function_str;
9572 [ # # # # ]: 0 : if (str_data == NULL || function_name >= str_data->d_size
9573 [ # # ]: 0 : || memchr (str_data->d_buf + function_name, '\0',
9574 : : str_data->d_size - function_name) == NULL)
9575 : : function_str = "???";
9576 : : else
9577 : 0 : function_str = (char *) str_data->d_buf + function_name;
9578 : :
9579 : 0 : printf (_(" set function name %s (0x%x)\n"),
9580 : : function_str, function_name);
9581 : : }
9582 : : break;
9583 : :
9584 : 0 : default:
9585 : : /* Unknown, ignore it. */
9586 : 0 : puts (_(" unknown opcode"));
9587 : 0 : linep += len - 1;
9588 : 0 : break;
9589 : : }
9590 : : }
9591 [ + - ]: 1551854 : else if (opcode <= DW_LNS_set_isa)
9592 : : {
9593 : : /* This is a known standard opcode. */
9594 [ + + + + : 1551854 : switch (opcode)
+ + - + +
+ - - ]
9595 : : {
9596 : 235894 : case DW_LNS_copy:
9597 : : /* Takes no argument. */
9598 : 235894 : puts (_(" copy"));
9599 : 235894 : break;
9600 : :
9601 : 15314 : case DW_LNS_advance_pc:
9602 : : /* Takes one uleb128 parameter which is added to the
9603 : : address. */
9604 [ - + ]: 15314 : if (lineendp - linep < 1)
9605 : 0 : goto invalid_unit;
9606 : 15314 : get_uleb128 (u128, linep, lineendp);
9607 : 15314 : advance_pc (u128);
9608 : : {
9609 : 15314 : printf (_(" advance address by %u to "),
9610 : : op_addr_advance);
9611 : 15314 : print_dwarf_addr (dwflmod, 0, address, address);
9612 [ - + ]: 15314 : if (op_index > 0)
9613 : 0 : printf (_(", op_index to %u"), op_index);
9614 : 15314 : printf ("\n");
9615 : : }
9616 : : break;
9617 : :
9618 : 181788 : case DW_LNS_advance_line:
9619 : : /* Takes one sleb128 parameter which is added to the
9620 : : line. */
9621 [ - + ]: 181788 : if (lineendp - linep < 1)
9622 : 0 : goto invalid_unit;
9623 : 181788 : get_sleb128 (s128, linep, lineendp);
9624 : 181788 : line += s128;
9625 : 181788 : printf (_("\
9626 : : advance line by constant %d to %" PRId64 "\n"),
9627 : : s128, (int64_t) line);
9628 : : break;
9629 : :
9630 : 65712 : case DW_LNS_set_file:
9631 : : /* Takes one uleb128 parameter which is stored in file. */
9632 [ - + ]: 65712 : if (lineendp - linep < 1)
9633 : 0 : goto invalid_unit;
9634 : 65712 : get_uleb128 (u128, linep, lineendp);
9635 : 65712 : printf (_(" set file to %" PRIu64 "\n"),
9636 : : (uint64_t) u128);
9637 : : break;
9638 : :
9639 : 588188 : case DW_LNS_set_column:
9640 : : /* Takes one uleb128 parameter which is stored in column. */
9641 [ + - - + ]: 588188 : if (unlikely (standard_opcode_lengths[opcode] != 1
9642 : : || lineendp - linep < 1))
9643 : 0 : goto invalid_unit;
9644 : :
9645 : 588188 : get_uleb128 (u128, linep, lineendp);
9646 : 588188 : printf (_(" set column to %" PRIu64 "\n"),
9647 : : (uint64_t) u128);
9648 : : break;
9649 : :
9650 : 411718 : case DW_LNS_negate_stmt:
9651 : : /* Takes no argument. */
9652 : 411718 : is_stmt = 1 - is_stmt;
9653 : 411718 : printf (_(" set '%s' to %" PRIuFAST8 "\n"),
9654 : : "is_stmt", is_stmt);
9655 : : break;
9656 : :
9657 : 0 : case DW_LNS_set_basic_block:
9658 : : /* Takes no argument. */
9659 : 0 : puts (_(" set basic block flag"));
9660 : 0 : break;
9661 : :
9662 : 53186 : case DW_LNS_const_add_pc:
9663 : : /* Takes no argument. */
9664 : :
9665 [ - + ]: 53186 : if (unlikely (line_range == 0))
9666 : 0 : goto invalid_unit;
9667 : :
9668 : 53186 : advance_pc ((255 - opcode_base) / line_range);
9669 : : {
9670 : 53186 : printf (_(" advance address by constant %u to "),
9671 : : op_addr_advance);
9672 : 53186 : print_dwarf_addr (dwflmod, 0, address, address);
9673 [ - + ]: 53186 : if (op_index > 0)
9674 : 0 : printf (_(", op_index to %u"), op_index);
9675 : 53186 : printf ("\n");
9676 : : }
9677 : : break;
9678 : :
9679 : 44 : case DW_LNS_fixed_advance_pc:
9680 : : /* Takes one 16 bit parameter which is added to the
9681 : : address. */
9682 [ + - - + ]: 44 : if (unlikely (standard_opcode_lengths[opcode] != 1
9683 : : || lineendp - linep < 2))
9684 : 0 : goto invalid_unit;
9685 : :
9686 [ - + ]: 44 : u128 = read_2ubyte_unaligned_inc (dbg, linep);
9687 : 44 : address += u128;
9688 : 44 : op_index = 0;
9689 : : {
9690 : 44 : printf (_("\
9691 : : advance address by fixed value %u to \n"),
9692 : : u128);
9693 : 44 : print_dwarf_addr (dwflmod, 0, address, address);
9694 : 44 : printf ("\n");
9695 : : }
9696 : : break;
9697 : :
9698 : 10 : case DW_LNS_set_prologue_end:
9699 : : /* Takes no argument. */
9700 : 10 : puts (_(" set prologue end flag"));
9701 : 10 : break;
9702 : :
9703 : 0 : case DW_LNS_set_epilogue_begin:
9704 : : /* Takes no argument. */
9705 : 0 : puts (_(" set epilogue begin flag"));
9706 : 0 : break;
9707 : :
9708 : 0 : case DW_LNS_set_isa:
9709 : : /* Takes one uleb128 parameter which is stored in isa. */
9710 [ # # # # ]: 0 : if (unlikely (standard_opcode_lengths[opcode] != 1
9711 : : || lineendp - linep < 1))
9712 : 0 : goto invalid_unit;
9713 : :
9714 : 0 : get_uleb128 (u128, linep, lineendp);
9715 [ + + ]: 2367388 : printf (_(" set isa to %u\n"), u128);
9716 : : break;
9717 : : }
9718 : : }
9719 : : else
9720 : : {
9721 : : /* This is a new opcode the generator but not we know about.
9722 : : Read the parameters associated with it but then discard
9723 : : everything. Read all the parameters for this opcode. */
9724 : 0 : printf (ngettext (" unknown opcode with %" PRIu8 " parameter:",
9725 : : " unknown opcode with %" PRIu8 " parameters:",
9726 : : standard_opcode_lengths[opcode]),
9727 : 0 : standard_opcode_lengths[opcode]);
9728 : 0 : for (int n = standard_opcode_lengths[opcode];
9729 [ # # # # ]: 0 : n > 0 && linep < lineendp; --n)
9730 : : {
9731 : 0 : get_uleb128 (u128, linep, lineendp);
9732 [ # # ]: 0 : if (n != standard_opcode_lengths[opcode])
9733 : 0 : fputc (',', stdout);
9734 : 0 : printf (" %u", u128);
9735 : : }
9736 : :
9737 : : /* Next round, ignore this opcode. */
9738 : 0 : continue;
9739 : : }
9740 : : }
9741 : : }
9742 : :
9743 : : /* There must only be one data block. */
9744 [ - + ]: 104 : assert (elf_getdata (scn, data) == NULL);
9745 : : }
9746 : :
9747 : :
9748 : : static void
9749 : 40 : print_debug_loclists_section (Dwfl_Module *dwflmod,
9750 : : Ebl *ebl,
9751 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
9752 : : Elf_Scn *scn, GElf_Shdr *shdr,
9753 : : Dwarf *dbg)
9754 : : {
9755 : 40 : Elf_Data *data = get_debug_elf_data (dbg, ebl, IDX_debug_loclists, scn);
9756 [ + - ]: 40 : if (data == NULL)
9757 : 0 : return;
9758 : :
9759 : 40 : printf (_("\
9760 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
9761 : : elf_ndxscn (scn), section_name (ebl, shdr),
9762 : 40 : (uint64_t) shdr->sh_offset);
9763 : :
9764 : : /* For the listptr to get the base address/CU. */
9765 : 40 : sort_listptr (&known_loclistsptr, "loclistsptr");
9766 : 40 : size_t listptr_idx = 0;
9767 : :
9768 : 40 : const unsigned char *readp = data->d_buf;
9769 : 40 : const unsigned char *const dataend = ((unsigned char *) data->d_buf
9770 : 40 : + data->d_size);
9771 [ + + ]: 3072 : while (readp < dataend)
9772 : : {
9773 [ - + ]: 3032 : if (unlikely (readp > dataend - 4))
9774 : : {
9775 : 0 : invalid_data:
9776 : 0 : error (0, 0, _("invalid data in section [%zu] '%s'"),
9777 : : elf_ndxscn (scn), section_name (ebl, shdr));
9778 : 0 : return;
9779 : : }
9780 : :
9781 : 3032 : ptrdiff_t offset = readp - (unsigned char *) data->d_buf;
9782 : 3032 : printf (_("Table at Offset 0x%" PRIx64 ":\n\n"),
9783 : : (uint64_t) offset);
9784 : :
9785 [ - + ]: 3032 : uint64_t unit_length = read_4ubyte_unaligned_inc (dbg, readp);
9786 : 3032 : unsigned int offset_size = 4;
9787 [ - + ]: 3032 : if (unlikely (unit_length == 0xffffffff))
9788 : : {
9789 [ # # ]: 0 : if (unlikely (readp > dataend - 8))
9790 : 0 : goto invalid_data;
9791 : :
9792 [ # # ]: 0 : unit_length = read_8ubyte_unaligned_inc (dbg, readp);
9793 : 0 : offset_size = 8;
9794 : : }
9795 : 3032 : printf (_(" Length: %8" PRIu64 "\n"), unit_length);
9796 : :
9797 : : /* We need at least 2-bytes + 1-byte + 1-byte + 4-bytes = 8
9798 : : bytes to complete the header. And this unit cannot go beyond
9799 : : the section data. */
9800 [ + - ]: 3032 : if (readp > dataend - 8
9801 [ + - ]: 3032 : || unit_length < 8
9802 [ - + ]: 3032 : || unit_length > (uint64_t) (dataend - readp))
9803 : 0 : goto invalid_data;
9804 : :
9805 : 3032 : const unsigned char *nexthdr = readp + unit_length;
9806 : :
9807 [ - + ]: 3032 : uint16_t version = read_2ubyte_unaligned_inc (dbg, readp);
9808 : 3032 : printf (_(" DWARF version: %8" PRIu16 "\n"), version);
9809 : :
9810 [ - + ]: 3032 : if (version != 5)
9811 : : {
9812 : 0 : error (0, 0, _("Unknown version"));
9813 : 0 : goto next_table;
9814 : : }
9815 : :
9816 : 3032 : uint8_t address_size = *readp++;
9817 : 3032 : printf (_(" Address size: %8" PRIu64 "\n"),
9818 : : (uint64_t) address_size);
9819 : :
9820 [ - + ]: 3032 : if (address_size != 4 && address_size != 8)
9821 : : {
9822 : 0 : error (0, 0, _("unsupported address size"));
9823 : 0 : goto next_table;
9824 : : }
9825 : :
9826 : 3032 : uint8_t segment_size = *readp++;
9827 : 3032 : printf (_(" Segment size: %8" PRIu64 "\n"),
9828 : : (uint64_t) segment_size);
9829 : :
9830 [ - + ]: 3032 : if (segment_size != 0)
9831 : : {
9832 : 0 : error (0, 0, _("unsupported segment size"));
9833 : 0 : goto next_table;
9834 : : }
9835 : :
9836 [ - + ]: 3032 : uint32_t offset_entry_count = read_4ubyte_unaligned_inc (dbg, readp);
9837 : 3032 : printf (_(" Offset entries: %8" PRIu64 "\n"),
9838 : : (uint64_t) offset_entry_count);
9839 : :
9840 : : /* We need the CU that uses this unit to get the initial base address. */
9841 : 3032 : Dwarf_Addr cu_base = 0;
9842 : 3032 : struct Dwarf_CU *cu = NULL;
9843 [ - + ]: 3032 : if (listptr_cu (&known_loclistsptr, &listptr_idx,
9844 : : (Dwarf_Off) offset,
9845 : 3032 : (Dwarf_Off) (nexthdr - (unsigned char *) data->d_buf),
9846 : : &cu_base, &cu)
9847 [ # # ]: 0 : || split_dwarf_cu_base (dbg, &cu, &cu_base))
9848 : 3032 : {
9849 : 3032 : Dwarf_Die cudie;
9850 [ - + ]: 3032 : if (dwarf_cu_die (cu, &cudie,
9851 : : NULL, NULL, NULL, NULL,
9852 : : NULL, NULL) == NULL)
9853 : 0 : printf (_(" Unknown CU base: "));
9854 : : else
9855 : 3032 : printf (_(" CU [%6" PRIx64 "] base: "),
9856 : : dwarf_dieoffset (&cudie));
9857 : 3032 : print_dwarf_addr (dwflmod, address_size, cu_base, cu_base);
9858 : 3032 : printf ("\n");
9859 : : }
9860 : : else
9861 : 0 : printf (_(" Not associated with a CU.\n"));
9862 : :
9863 : 3032 : printf ("\n");
9864 : :
9865 : 3032 : const unsigned char *offset_array_start = readp;
9866 [ + + ]: 3032 : if (offset_entry_count > 0)
9867 : : {
9868 : 4 : uint64_t max_entries = (unit_length - 8) / offset_size;
9869 [ - + ]: 4 : if (offset_entry_count > max_entries)
9870 : : {
9871 : 0 : error (0, 0,
9872 : 0 : _("too many offset entries for unit length"));
9873 : 0 : offset_entry_count = max_entries;
9874 : : }
9875 : :
9876 : 4 : printf (_(" Offsets starting at 0x%" PRIx64 ":\n"),
9877 : : (uint64_t) (offset_array_start
9878 : 4 : - (unsigned char *) data->d_buf));
9879 [ + + ]: 36 : for (uint32_t idx = 0; idx < offset_entry_count; idx++)
9880 : : {
9881 : 32 : printf (" [%6" PRIu32 "] ", idx);
9882 [ + - ]: 32 : if (offset_size == 4)
9883 : : {
9884 [ - + ]: 32 : uint32_t off = read_4ubyte_unaligned_inc (dbg, readp);
9885 : 32 : printf ("0x%" PRIx32 "\n", off);
9886 : : }
9887 : : else
9888 : : {
9889 [ # # ]: 0 : uint64_t off = read_8ubyte_unaligned_inc (dbg, readp);
9890 : 32 : printf ("0x%" PRIx64 "\n", off);
9891 : : }
9892 : : }
9893 : 4 : printf ("\n");
9894 : : }
9895 : :
9896 : 3032 : Dwarf_Addr base = cu_base;
9897 : 3032 : bool start_of_list = true;
9898 : 3032 : while (readp < nexthdr)
9899 : : {
9900 : 734316 : Dwarf_Off off = (Dwarf_Off) (readp - (unsigned char *) data->d_buf);
9901 [ + + ]: 734316 : if (listptr_attr (&known_loclistsptr, listptr_idx, off,
9902 : : DW_AT_GNU_locviews))
9903 : 107692 : {
9904 : 107692 : Dwarf_Off next_off = next_listptr_offset (&known_loclistsptr,
9905 : : &listptr_idx, off);
9906 : 107692 : const unsigned char *locp = readp;
9907 : 107692 : const unsigned char *locendp;
9908 [ + - ]: 107692 : if (next_off == 0
9909 [ + - ]: 107692 : || next_off > (size_t) (nexthdr - ((const unsigned char *)
9910 : : data->d_buf)))
9911 : : locendp = nexthdr;
9912 : : else
9913 : 107692 : locendp = (const unsigned char *) data->d_buf + next_off;
9914 : :
9915 : 715966 : printf (" Offset: %" PRIx64 ", Index: %" PRIx64 "\n",
9916 : : (uint64_t) (readp - (unsigned char *) data->d_buf),
9917 : 107692 : (uint64_t) (readp - offset_array_start));
9918 : :
9919 : 608274 : while (locp < locendp)
9920 : : {
9921 : 500582 : uint64_t v1, v2;
9922 : 500582 : get_uleb128 (v1, locp, locendp);
9923 [ - + ]: 500582 : if (locp >= locendp)
9924 : : {
9925 : 0 : printf (_(" <INVALID DATA>\n"));
9926 : : break;
9927 : : }
9928 : 500582 : get_uleb128 (v2, locp, locendp);
9929 [ + + ]: 608274 : printf (" view pair %" PRId64 ", %" PRId64 "\n", v1, v2);
9930 : : }
9931 : :
9932 : 107692 : printf ("\n");
9933 : 107692 : readp = (unsigned char *) locendp;
9934 : 107692 : continue;
9935 : : }
9936 : :
9937 : 626624 : uint8_t kind = *readp++;
9938 : 626624 : uint64_t op1, op2, len;
9939 : :
9940 : : /* Skip padding. */
9941 [ - + ]: 626624 : if (start_of_list && kind == DW_LLE_end_of_list)
9942 : 0 : continue;
9943 : :
9944 [ + + ]: 626624 : if (start_of_list)
9945 : : {
9946 : 107756 : base = cu_base;
9947 : 107756 : printf (" Offset: %" PRIx64 ", Index: %" PRIx64 "\n",
9948 : 107756 : (uint64_t) (readp - (unsigned char *) data->d_buf - 1),
9949 : 107756 : (uint64_t) (readp - offset_array_start - 1));
9950 : 107756 : start_of_list = false;
9951 : : }
9952 : :
9953 : 626624 : printf (" %s", dwarf_loc_list_encoding_name (kind));
9954 [ + - - + : 626624 : switch (kind)
+ - + - +
- - ]
9955 : : {
9956 : 107756 : case DW_LLE_end_of_list:
9957 : 107756 : start_of_list = true;
9958 : 107756 : printf ("\n\n");
9959 : : break;
9960 : :
9961 : 0 : case DW_LLE_base_addressx:
9962 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
9963 : : {
9964 : 0 : invalid_entry:
9965 : 0 : error (0, 0, _("invalid loclists data"));
9966 : 0 : goto next_table;
9967 : : }
9968 : 0 : get_uleb128 (op1, readp, nexthdr);
9969 : 0 : printf (" %" PRIx64 "\n", op1);
9970 [ # # ]: 0 : if (! print_unresolved_addresses)
9971 : : {
9972 : 0 : Dwarf_Addr addr;
9973 [ # # ]: 0 : if (get_indexed_addr (cu, op1, &addr) != 0)
9974 : 0 : printf (" ???\n");
9975 : : else
9976 : : {
9977 : 0 : printf (" ");
9978 : 0 : print_dwarf_addr (dwflmod, address_size, addr, addr);
9979 : 0 : printf ("\n");
9980 : : }
9981 : : }
9982 : : break;
9983 : :
9984 : 0 : case DW_LLE_startx_endx:
9985 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
9986 : 0 : goto invalid_entry;
9987 : 0 : get_uleb128 (op1, readp, nexthdr);
9988 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
9989 : 0 : goto invalid_entry;
9990 : 0 : get_uleb128 (op2, readp, nexthdr);
9991 : 0 : printf (" %" PRIx64 ", %" PRIx64 "\n", op1, op2);
9992 [ # # ]: 0 : if (! print_unresolved_addresses)
9993 : : {
9994 : 0 : Dwarf_Addr addr1;
9995 : 0 : Dwarf_Addr addr2;
9996 [ # # ]: 0 : if (get_indexed_addr (cu, op1, &addr1) != 0
9997 [ # # ]: 0 : || get_indexed_addr (cu, op2, &addr2) != 0)
9998 : : {
9999 : 0 : printf (" ???..\n");
10000 : 0 : printf (" ???\n");
10001 : : }
10002 : : else
10003 : : {
10004 : 0 : printf (" ");
10005 : 0 : print_dwarf_addr (dwflmod, address_size, addr1, addr1);
10006 : 0 : printf ("..\n ");
10007 : 0 : print_dwarf_addr (dwflmod, address_size,
10008 : : addr2 - 1, addr2);
10009 : 0 : printf ("\n");
10010 : : }
10011 : : }
10012 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
10013 : 0 : goto invalid_entry;
10014 : 0 : get_uleb128 (len, readp, nexthdr);
10015 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < len)
10016 : 0 : goto invalid_entry;
10017 : 0 : print_ops (dwflmod, dbg, 8, 8, version,
10018 : : address_size, offset_size, cu, len, readp);
10019 : 0 : readp += len;
10020 : 0 : break;
10021 : :
10022 : 52 : case DW_LLE_startx_length:
10023 [ - + ]: 52 : if ((uint64_t) (nexthdr - readp) < 1)
10024 : 0 : goto invalid_entry;
10025 : 52 : get_uleb128 (op1, readp, nexthdr);
10026 [ - + ]: 52 : if ((uint64_t) (nexthdr - readp) < 1)
10027 : 0 : goto invalid_entry;
10028 : 52 : get_uleb128 (op2, readp, nexthdr);
10029 : 52 : printf (" %" PRIx64 ", %" PRIx64 "\n", op1, op2);
10030 [ + - ]: 52 : if (! print_unresolved_addresses)
10031 : : {
10032 : 52 : Dwarf_Addr addr1;
10033 : 52 : Dwarf_Addr addr2;
10034 [ - + ]: 52 : if (get_indexed_addr (cu, op1, &addr1) != 0)
10035 : : {
10036 : 0 : printf (" ???..\n");
10037 : 0 : printf (" ???\n");
10038 : : }
10039 : : else
10040 : : {
10041 : 52 : addr2 = addr1 + op2;
10042 : 52 : printf (" ");
10043 : 52 : print_dwarf_addr (dwflmod, address_size, addr1, addr1);
10044 : 52 : printf ("..\n ");
10045 : 52 : print_dwarf_addr (dwflmod, address_size,
10046 : : addr2 - 1, addr2);
10047 : 52 : printf ("\n");
10048 : : }
10049 : : }
10050 [ - + ]: 52 : if ((uint64_t) (nexthdr - readp) < 1)
10051 : 0 : goto invalid_entry;
10052 : 52 : get_uleb128 (len, readp, nexthdr);
10053 [ - + ]: 52 : if ((uint64_t) (nexthdr - readp) < len)
10054 : 0 : goto invalid_entry;
10055 : 52 : print_ops (dwflmod, dbg, 8, 8, version,
10056 : : address_size, offset_size, cu, len, readp);
10057 : 52 : readp += len;
10058 : 52 : break;
10059 : :
10060 : 488836 : case DW_LLE_offset_pair:
10061 [ - + ]: 488836 : if ((uint64_t) (nexthdr - readp) < 1)
10062 : 0 : goto invalid_entry;
10063 : 488836 : get_uleb128 (op1, readp, nexthdr);
10064 [ - + ]: 488836 : if ((uint64_t) (nexthdr - readp) < 1)
10065 : 0 : goto invalid_entry;
10066 : 488836 : get_uleb128 (op2, readp, nexthdr);
10067 : 488836 : printf (" %" PRIx64 ", %" PRIx64 "\n", op1, op2);
10068 [ + - ]: 488836 : if (! print_unresolved_addresses)
10069 : : {
10070 : 488836 : op1 += base;
10071 : 488836 : op2 += base;
10072 : 488836 : printf (" ");
10073 : 488836 : print_dwarf_addr (dwflmod, address_size, op1, op1);
10074 : 488836 : printf ("..\n ");
10075 : 488836 : print_dwarf_addr (dwflmod, address_size, op2 - 1, op2);
10076 : 488836 : printf ("\n");
10077 : : }
10078 [ - + ]: 488836 : if ((uint64_t) (nexthdr - readp) < 1)
10079 : 0 : goto invalid_entry;
10080 : 488836 : get_uleb128 (len, readp, nexthdr);
10081 [ - + ]: 488836 : if ((uint64_t) (nexthdr - readp) < len)
10082 : 0 : goto invalid_entry;
10083 : 488836 : print_ops (dwflmod, dbg, 8, 8, version,
10084 : : address_size, offset_size, cu, len, readp);
10085 : 488836 : readp += len;
10086 : 488836 : break;
10087 : :
10088 : 0 : case DW_LLE_default_location:
10089 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
10090 : 0 : goto invalid_entry;
10091 : 0 : get_uleb128 (len, readp, nexthdr);
10092 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < len)
10093 : 0 : goto invalid_entry;
10094 : 0 : print_ops (dwflmod, dbg, 8, 8, version,
10095 : : address_size, offset_size, cu, len, readp);
10096 : 0 : readp += len;
10097 : 0 : break;
10098 : :
10099 : 18182 : case DW_LLE_base_address:
10100 [ - + ]: 18182 : if (address_size == 4)
10101 : : {
10102 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 4)
10103 : 0 : goto invalid_entry;
10104 [ # # ]: 0 : op1 = read_4ubyte_unaligned_inc (dbg, readp);
10105 : : }
10106 : : else
10107 : : {
10108 [ - + ]: 18182 : if ((uint64_t) (nexthdr - readp) < 8)
10109 : 0 : goto invalid_entry;
10110 [ - + ]: 18182 : op1 = read_8ubyte_unaligned_inc (dbg, readp);
10111 : : }
10112 : 18182 : base = op1;
10113 : 18182 : printf (" 0x%" PRIx64 "\n", base);
10114 [ - + ]: 18182 : if (! print_unresolved_addresses)
10115 : : {
10116 : 18182 : printf (" ");
10117 : 18182 : print_dwarf_addr (dwflmod, address_size, base, base);
10118 : 18182 : printf ("\n");
10119 : : }
10120 : : break;
10121 : :
10122 : 0 : case DW_LLE_start_end:
10123 [ # # ]: 0 : if (address_size == 4)
10124 : : {
10125 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 8)
10126 : 0 : goto invalid_entry;
10127 [ # # ]: 0 : op1 = read_4ubyte_unaligned_inc (dbg, readp);
10128 [ # # ]: 0 : op2 = read_4ubyte_unaligned_inc (dbg, readp);
10129 : : }
10130 : : else
10131 : : {
10132 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 16)
10133 : 0 : goto invalid_entry;
10134 [ # # ]: 0 : op1 = read_8ubyte_unaligned_inc (dbg, readp);
10135 [ # # ]: 0 : op2 = read_8ubyte_unaligned_inc (dbg, readp);
10136 : : }
10137 : 0 : printf (" 0x%" PRIx64 "..0x%" PRIx64 "\n", op1, op2);
10138 [ # # ]: 0 : if (! print_unresolved_addresses)
10139 : : {
10140 : 0 : printf (" ");
10141 : 0 : print_dwarf_addr (dwflmod, address_size, op1, op1);
10142 : 0 : printf ("..\n ");
10143 : 0 : print_dwarf_addr (dwflmod, address_size, op2 - 1, op2);
10144 : 0 : printf ("\n");
10145 : : }
10146 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
10147 : 0 : goto invalid_entry;
10148 : 0 : get_uleb128 (len, readp, nexthdr);
10149 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < len)
10150 : 0 : goto invalid_entry;
10151 : 0 : print_ops (dwflmod, dbg, 8, 8, version,
10152 : : address_size, offset_size, cu, len, readp);
10153 : 0 : readp += len;
10154 : 0 : break;
10155 : :
10156 : 11798 : case DW_LLE_start_length:
10157 [ - + ]: 11798 : if (address_size == 4)
10158 : : {
10159 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 4)
10160 : 0 : goto invalid_entry;
10161 [ # # ]: 0 : op1 = read_4ubyte_unaligned_inc (dbg, readp);
10162 : : }
10163 : : else
10164 : : {
10165 [ - + ]: 11798 : if ((uint64_t) (nexthdr - readp) < 8)
10166 : 0 : goto invalid_entry;
10167 [ - + ]: 11798 : op1 = read_8ubyte_unaligned_inc (dbg, readp);
10168 : : }
10169 [ - + ]: 11798 : if ((uint64_t) (nexthdr - readp) < 1)
10170 : 0 : goto invalid_entry;
10171 : 11798 : get_uleb128 (op2, readp, nexthdr);
10172 : 11798 : printf (" 0x%" PRIx64 ", %" PRIx64 "\n", op1, op2);
10173 [ + + ]: 11798 : if (! print_unresolved_addresses)
10174 : : {
10175 : 11796 : op2 = op1 + op2;
10176 : 11796 : printf (" ");
10177 : 11796 : print_dwarf_addr (dwflmod, address_size, op1, op1);
10178 : 11796 : printf ("..\n ");
10179 : 11796 : print_dwarf_addr (dwflmod, address_size, op2 - 1, op2);
10180 : 11796 : printf ("\n");
10181 : : }
10182 [ - + ]: 11798 : if ((uint64_t) (nexthdr - readp) < 1)
10183 : 0 : goto invalid_entry;
10184 : 11798 : get_uleb128 (len, readp, nexthdr);
10185 [ - + ]: 11798 : if ((uint64_t) (nexthdr - readp) < len)
10186 : 0 : goto invalid_entry;
10187 : 11798 : print_ops (dwflmod, dbg, 8, 8, version,
10188 : : address_size, offset_size, cu, len, readp);
10189 : 11798 : readp += len;
10190 : 11798 : break;
10191 : :
10192 : 0 : case DW_LLE_GNU_view_pair:
10193 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
10194 : 0 : goto invalid_entry;
10195 : 0 : get_uleb128 (op1, readp, nexthdr);
10196 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
10197 : 0 : goto invalid_entry;
10198 : 0 : get_uleb128 (op2, readp, nexthdr);
10199 [ + + ]: 737348 : printf (" %" PRIx64 ", %" PRIx64 "\n", op1, op2);
10200 : : break;
10201 : :
10202 : 0 : default:
10203 : 0 : goto invalid_entry;
10204 : : }
10205 : : }
10206 : :
10207 : 3032 : next_table:
10208 [ - + ]: 3032 : if (readp != nexthdr)
10209 : : {
10210 : 0 : size_t padding = nexthdr - readp;
10211 : 0 : printf (_(" %zu padding bytes\n\n"), padding);
10212 : 0 : readp = nexthdr;
10213 : : }
10214 : : }
10215 : : }
10216 : :
10217 : :
10218 : : static void
10219 : 52 : print_debug_loc_section (Dwfl_Module *dwflmod,
10220 : : Ebl *ebl, GElf_Ehdr *ehdr,
10221 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
10222 : : {
10223 : 52 : Elf_Data *data = get_debug_elf_data (dbg, ebl, IDX_debug_loc, scn);
10224 [ - + ]: 52 : if (data == NULL)
10225 : 0 : return;
10226 : :
10227 : 52 : printf (_("\
10228 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
10229 : : elf_ndxscn (scn), section_name (ebl, shdr),
10230 : 52 : (uint64_t) shdr->sh_offset);
10231 : :
10232 : 52 : sort_listptr (&known_locsptr, "loclistptr");
10233 : 52 : size_t listptr_idx = 0;
10234 : :
10235 [ + - ]: 52 : uint_fast8_t address_size = ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 4 : 8;
10236 : 52 : uint_fast8_t offset_size = 4;
10237 : :
10238 : 52 : bool first = true;
10239 : 52 : Dwarf_Addr base = 0;
10240 : 52 : unsigned char *readp = data->d_buf;
10241 : 52 : unsigned char *const endp = (unsigned char *) data->d_buf + data->d_size;
10242 : 52 : Dwarf_CU *last_cu = NULL;
10243 [ + + ]: 490 : while (readp < endp)
10244 : : {
10245 : 438 : ptrdiff_t offset = readp - (unsigned char *) data->d_buf;
10246 : 438 : Dwarf_CU *cu = last_cu;
10247 : 438 : unsigned int attr = 0;
10248 : :
10249 [ + + - + ]: 438 : if (first && skip_listptr_hole (&known_locsptr, &listptr_idx,
10250 : : &address_size, &offset_size, &base,
10251 : : &cu, offset, &readp, endp, &attr))
10252 : 0 : continue;
10253 : :
10254 [ + + ]: 438 : if (last_cu != cu)
10255 : : {
10256 : 60 : Dwarf_Die cudie;
10257 [ - + ]: 60 : if (dwarf_cu_die (cu, &cudie,
10258 : : NULL, NULL, NULL, NULL,
10259 : : NULL, NULL) == NULL)
10260 : 0 : printf (_("\n Unknown CU base: "));
10261 : : else
10262 : 60 : printf (_("\n CU [%6" PRIx64 "] base: "),
10263 : : dwarf_dieoffset (&cudie));
10264 : 60 : print_dwarf_addr (dwflmod, address_size, base, base);
10265 : 60 : printf ("\n");
10266 : : }
10267 : 438 : last_cu = cu;
10268 : :
10269 [ - + ]: 438 : if (attr == DW_AT_GNU_locviews)
10270 : 0 : {
10271 : 0 : Dwarf_Off next_off = next_listptr_offset (&known_locsptr,
10272 : : &listptr_idx, offset);
10273 : 0 : const unsigned char *locp = readp;
10274 : 0 : const unsigned char *locendp;
10275 [ # # ]: 0 : if (next_off == 0
10276 : 0 : || next_off > (size_t) (endp
10277 [ # # ]: 0 : - (const unsigned char *) data->d_buf))
10278 : 0 : locendp = endp;
10279 : : else
10280 : 0 : locendp = (const unsigned char *) data->d_buf + next_off;
10281 : :
10282 [ # # ]: 0 : while (locp < locendp)
10283 : : {
10284 : 0 : uint64_t v1, v2;
10285 : 0 : get_uleb128 (v1, locp, locendp);
10286 [ # # ]: 0 : if (locp >= locendp)
10287 : : {
10288 : 0 : printf (_(" [%6tx] <INVALID DATA>\n"), offset);
10289 : : break;
10290 : : }
10291 : 0 : get_uleb128 (v2, locp, locendp);
10292 [ # # ]: 0 : if (first) /* First view pair in a list. */
10293 : 0 : printf (" [%6tx] ", offset);
10294 : : else
10295 : 0 : printf (" ");
10296 : 0 : printf ("view pair %" PRId64 ", %" PRId64 "\n", v1, v2);
10297 : 0 : first = false;
10298 : : }
10299 : :
10300 : 0 : first = true;
10301 : 0 : readp = (unsigned char *) locendp;
10302 : 0 : continue;
10303 : : }
10304 : :
10305 : : /* GNU DebugFission encoded addresses as addrx. */
10306 : 876 : bool is_debugfission = ((cu != NULL
10307 [ # # ]: 0 : || split_dwarf_cu_base (dbg, &cu, &base))
10308 [ - + - + ]: 438 : && (cu->version < 5
10309 [ + + ]: 438 : && cu->unit_type == DW_UT_split_compile));
10310 [ + + ]: 438 : if (!is_debugfission
10311 [ - + ]: 318 : && unlikely (data->d_size - offset < (size_t) address_size * 2))
10312 : : {
10313 : 0 : invalid_data:
10314 : 0 : printf (_(" [%6tx] <INVALID DATA>\n"), offset);
10315 : 0 : break;
10316 : : }
10317 : :
10318 : 318 : Dwarf_Addr begin;
10319 : 318 : Dwarf_Addr end;
10320 : 438 : bool use_base = true;
10321 : 318 : if (is_debugfission)
10322 : : {
10323 : 120 : const unsigned char *locp = readp;
10324 : 120 : const unsigned char *locendp = readp + data->d_size;
10325 [ - + ]: 120 : if (locp >= locendp)
10326 : 0 : goto invalid_data;
10327 : :
10328 : 120 : Dwarf_Word idx;
10329 : 120 : unsigned char code = *locp++;
10330 [ + - - + : 120 : switch (code)
- ]
10331 : : {
10332 : 40 : case DW_LLE_GNU_end_of_list_entry:
10333 : 40 : begin = 0;
10334 : 40 : end = 0;
10335 : 40 : break;
10336 : :
10337 : 0 : case DW_LLE_GNU_base_address_selection_entry:
10338 [ # # ]: 0 : if (locp >= locendp)
10339 : 0 : goto invalid_data;
10340 : 0 : begin = (Dwarf_Addr) -1;
10341 : 0 : get_uleb128 (idx, locp, locendp);
10342 [ # # ]: 0 : if (get_indexed_addr (cu, idx, &end) != 0)
10343 : 0 : end = idx; /* ... */
10344 : : break;
10345 : :
10346 : 0 : case DW_LLE_GNU_start_end_entry:
10347 [ # # ]: 0 : if (locp >= locendp)
10348 : 0 : goto invalid_data;
10349 : 0 : get_uleb128 (idx, locp, locendp);
10350 [ # # ]: 0 : if (get_indexed_addr (cu, idx, &begin) != 0)
10351 : 0 : begin = idx; /* ... */
10352 [ # # ]: 0 : if (locp >= locendp)
10353 : 0 : goto invalid_data;
10354 : 0 : get_uleb128 (idx, locp, locendp);
10355 [ # # ]: 0 : if (get_indexed_addr (cu, idx, &end) != 0)
10356 : 0 : end = idx; /* ... */
10357 : : use_base = false;
10358 : : break;
10359 : :
10360 : 80 : case DW_LLE_GNU_start_length_entry:
10361 [ - + ]: 80 : if (locp >= locendp)
10362 : 0 : goto invalid_data;
10363 : 80 : get_uleb128 (idx, locp, locendp);
10364 [ - + ]: 80 : if (get_indexed_addr (cu, idx, &begin) != 0)
10365 : 0 : begin = idx; /* ... */
10366 [ - + ]: 80 : if (locendp - locp < 4)
10367 : 0 : goto invalid_data;
10368 [ - + ]: 80 : end = read_4ubyte_unaligned_inc (dbg, locp);
10369 : 80 : end += begin;
10370 : 80 : use_base = false;
10371 : 80 : break;
10372 : :
10373 : 0 : default:
10374 : 0 : goto invalid_data;
10375 : : }
10376 : :
10377 : 120 : readp = (unsigned char *) locp;
10378 : : }
10379 [ + - ]: 318 : else if (address_size == 8)
10380 : : {
10381 [ + + ]: 318 : begin = read_8ubyte_unaligned_inc (dbg, readp);
10382 [ + + ]: 318 : end = read_8ubyte_unaligned_inc (dbg, readp);
10383 : : }
10384 : : else
10385 : : {
10386 [ # # ]: 0 : begin = read_4ubyte_unaligned_inc (dbg, readp);
10387 [ # # ]: 0 : end = read_4ubyte_unaligned_inc (dbg, readp);
10388 [ # # ]: 0 : if (begin == (Dwarf_Addr) (uint32_t) -1)
10389 : 0 : begin = (Dwarf_Addr) -1l;
10390 : : }
10391 : :
10392 [ - + ]: 438 : if (begin == (Dwarf_Addr) -1l) /* Base address entry. */
10393 : : {
10394 [ # # ]: 0 : if (first)
10395 : 0 : printf (" [%6tx] ", offset);
10396 : : else
10397 : 0 : printf (" ");
10398 : 0 : puts (_("base address"));
10399 : 0 : printf (" ");
10400 : 0 : print_dwarf_addr (dwflmod, address_size, end, end);
10401 : 0 : printf ("\n");
10402 : 0 : base = end;
10403 : 0 : first = false;
10404 : : }
10405 [ + + + + ]: 438 : else if (begin == 0 && end == 0) /* End of list entry. */
10406 : : {
10407 [ - + ]: 148 : if (first)
10408 : 438 : printf (_(" [%6tx] empty list\n"), offset);
10409 : : first = true;
10410 : : }
10411 : : else
10412 : : {
10413 : : /* We have a location expression entry. */
10414 [ + + ]: 290 : uint_fast16_t len = read_2ubyte_unaligned_inc (dbg, readp);
10415 : :
10416 [ + + ]: 290 : if (first) /* First entry in a list. */
10417 : 148 : printf (" [%6tx] ", offset);
10418 : : else
10419 : 142 : printf (" ");
10420 : :
10421 : 290 : printf ("range %" PRIx64 ", %" PRIx64 "\n", begin, end);
10422 [ + + ]: 290 : if (! print_unresolved_addresses)
10423 : : {
10424 [ + + ]: 230 : Dwarf_Addr dab = use_base ? base + begin : begin;
10425 [ + + ]: 230 : Dwarf_Addr dae = use_base ? base + end : end;
10426 : 230 : printf (" ");
10427 : 230 : print_dwarf_addr (dwflmod, address_size, dab, dab);
10428 : 230 : printf ("..\n ");
10429 : 230 : print_dwarf_addr (dwflmod, address_size, dae - 1, dae);
10430 : 230 : printf ("\n");
10431 : : }
10432 : :
10433 [ - + ]: 290 : if (endp - readp <= (ptrdiff_t) len)
10434 : : {
10435 : 0 : fputs (_(" <INVALID DATA>\n"), stdout);
10436 : 0 : break;
10437 : : }
10438 : :
10439 [ + - ]: 580 : print_ops (dwflmod, dbg, 11, 11,
10440 : 290 : cu != NULL ? cu->version : 3,
10441 : : address_size, offset_size, cu, len, readp);
10442 : :
10443 : 290 : first = false;
10444 : 290 : readp += len;
10445 : : }
10446 : : }
10447 : : }
10448 : :
10449 : : struct mac_culist
10450 : : {
10451 : : Dwarf_Die die;
10452 : : Dwarf_Off offset;
10453 : : Dwarf_Files *files;
10454 : : struct mac_culist *next;
10455 : : };
10456 : :
10457 : :
10458 : : static int
10459 : 0 : mac_compare (const void *p1, const void *p2)
10460 : : {
10461 : 0 : struct mac_culist *m1 = (struct mac_culist *) p1;
10462 : 0 : struct mac_culist *m2 = (struct mac_culist *) p2;
10463 : :
10464 [ # # ]: 0 : if (m1->offset < m2->offset)
10465 : : return -1;
10466 [ # # ]: 0 : if (m1->offset > m2->offset)
10467 : 0 : return 1;
10468 : : return 0;
10469 : : }
10470 : :
10471 : :
10472 : : static void
10473 : 0 : print_debug_macinfo_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
10474 : : Ebl *ebl,
10475 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
10476 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
10477 : : {
10478 : 0 : Elf_Data *data = get_debug_elf_data (dbg, ebl, IDX_debug_macinfo, scn);
10479 [ # # ]: 0 : if (data == NULL)
10480 : 0 : return;
10481 : :
10482 : 0 : printf (_("\
10483 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
10484 : : elf_ndxscn (scn), section_name (ebl, shdr),
10485 : 0 : (uint64_t) shdr->sh_offset);
10486 : 0 : fputc ('\n', stdout);
10487 : :
10488 : : /* There is no function in libdw to iterate over the raw content of
10489 : : the section but it is easy enough to do. */
10490 : :
10491 : : /* Get the source file information for all CUs. */
10492 : 0 : Dwarf_Off offset;
10493 : 0 : Dwarf_Off ncu = 0;
10494 : 0 : size_t hsize;
10495 : 0 : struct mac_culist *culist = NULL;
10496 : 0 : size_t nculist = 0;
10497 [ # # ]: 0 : while (dwarf_nextcu (dbg, offset = ncu, &ncu, &hsize, NULL, NULL, NULL) == 0)
10498 : : {
10499 : 0 : Dwarf_Die cudie;
10500 [ # # ]: 0 : if (dwarf_offdie (dbg, offset + hsize, &cudie) == NULL)
10501 : 0 : continue;
10502 : :
10503 : 0 : Dwarf_Attribute attr;
10504 [ # # ]: 0 : if (dwarf_attr (&cudie, DW_AT_macro_info, &attr) == NULL)
10505 : 0 : continue;
10506 : :
10507 : 0 : Dwarf_Word macoff;
10508 [ # # ]: 0 : if (dwarf_formudata (&attr, &macoff) != 0)
10509 : 0 : continue;
10510 : :
10511 : 0 : struct mac_culist *newp = (struct mac_culist *) alloca (sizeof (*newp));
10512 : 0 : newp->die = cudie;
10513 : 0 : newp->offset = macoff;
10514 : 0 : newp->files = NULL;
10515 : 0 : newp->next = culist;
10516 : 0 : culist = newp;
10517 : 0 : ++nculist;
10518 : : }
10519 : :
10520 : : /* Convert the list into an array for easier consumption. */
10521 : 0 : struct mac_culist *cus = (struct mac_culist *) alloca ((nculist + 1)
10522 : : * sizeof (*cus));
10523 : : /* Add sentinel. */
10524 : 0 : cus[nculist].offset = data->d_size;
10525 : 0 : cus[nculist].files = (Dwarf_Files *) -1l;
10526 [ # # ]: 0 : if (nculist > 0)
10527 : : {
10528 [ # # ]: 0 : for (size_t cnt = nculist - 1; culist != NULL; --cnt)
10529 : : {
10530 [ # # ]: 0 : assert (cnt < nculist);
10531 : 0 : cus[cnt] = *culist;
10532 : 0 : culist = culist->next;
10533 : : }
10534 : :
10535 : : /* Sort the array according to the offset in the .debug_macinfo
10536 : : section. Note we keep the sentinel at the end. */
10537 : 0 : qsort (cus, nculist, sizeof (*cus), mac_compare);
10538 : : }
10539 : :
10540 : 0 : const unsigned char *readp = (const unsigned char *) data->d_buf;
10541 : 0 : const unsigned char *readendp = readp + data->d_size;
10542 : 0 : int level = 1;
10543 : :
10544 : 0 : while (readp < readendp)
10545 : : {
10546 : 0 : unsigned int opcode = *readp++;
10547 : 0 : unsigned int u128;
10548 : 0 : unsigned int u128_2;
10549 : 0 : const unsigned char *endp;
10550 : :
10551 [ # # # # ]: 0 : switch (opcode)
10552 : : {
10553 : 0 : case DW_MACINFO_define:
10554 : : case DW_MACINFO_undef:
10555 : : case DW_MACINFO_vendor_ext:
10556 : : /* For the first two opcodes the parameters are
10557 : : line, string
10558 : : For the latter
10559 : : number, string.
10560 : : We can treat these cases together. */
10561 : 0 : get_uleb128 (u128, readp, readendp);
10562 : :
10563 : 0 : endp = memchr (readp, '\0', readendp - readp);
10564 [ # # ]: 0 : if (unlikely (endp == NULL))
10565 : : {
10566 : 0 : printf (_("\
10567 : : %*s*** non-terminated string at end of section"),
10568 : : level, "");
10569 : 0 : return;
10570 : : }
10571 : :
10572 [ # # ]: 0 : if (opcode == DW_MACINFO_define)
10573 : 0 : printf ("%*s#define %s, line %u\n",
10574 : : level, "", (char *) readp, u128);
10575 [ # # ]: 0 : else if (opcode == DW_MACINFO_undef)
10576 : 0 : printf ("%*s#undef %s, line %u\n",
10577 : : level, "", (char *) readp, u128);
10578 : : else
10579 : 0 : printf (" #vendor-ext %s, number %u\n", (char *) readp, u128);
10580 : :
10581 : 0 : readp = endp + 1;
10582 : 0 : break;
10583 : :
10584 : 0 : case DW_MACINFO_start_file:
10585 : : /* The two parameters are line and file index, in this order. */
10586 : 0 : get_uleb128 (u128, readp, readendp);
10587 [ # # ]: 0 : if (readendp - readp < 1)
10588 : : {
10589 : 0 : printf (_("\
10590 : : %*s*** missing DW_MACINFO_start_file argument at end of section"),
10591 : : level, "");
10592 : 0 : return;
10593 : : }
10594 : 0 : get_uleb128 (u128_2, readp, readendp);
10595 : :
10596 : : /* Find the CU DIE for this file. */
10597 : 0 : size_t macoff = readp - (const unsigned char *) data->d_buf;
10598 : 0 : const char *fname = "???";
10599 [ # # # # ]: 0 : if (macoff >= cus[0].offset && cus[0].offset != data->d_size)
10600 : : {
10601 [ # # # # ]: 0 : while (macoff >= cus[1].offset && cus[1].offset != data->d_size)
10602 : 0 : ++cus;
10603 : :
10604 [ # # ]: 0 : if (cus[0].files == NULL
10605 [ # # ]: 0 : && dwarf_getsrcfiles (&cus[0].die, &cus[0].files, NULL) != 0)
10606 : 0 : cus[0].files = (Dwarf_Files *) -1l;
10607 : :
10608 [ # # ]: 0 : if (cus[0].files != (Dwarf_Files *) -1l)
10609 : 0 : fname = (dwarf_filesrc (cus[0].files, u128_2, NULL, NULL)
10610 [ # # ]: 0 : ?: "???");
10611 : : }
10612 : :
10613 : 0 : printf ("%*sstart_file %u, [%u] %s\n",
10614 : : level, "", u128, u128_2, fname);
10615 : 0 : ++level;
10616 : 0 : break;
10617 : :
10618 : 0 : case DW_MACINFO_end_file:
10619 : 0 : --level;
10620 : 0 : printf ("%*send_file\n", level, "");
10621 : : /* Nothing more to do. */
10622 : : break;
10623 : :
10624 : 0 : default:
10625 : : // XXX gcc seems to generate files with a trailing zero.
10626 [ # # # # ]: 0 : if (unlikely (opcode != 0 || readp != readendp))
10627 [ # # ]: 0 : printf ("%*s*** invalid opcode %u\n", level, "", opcode);
10628 : : break;
10629 : : }
10630 : : }
10631 : : }
10632 : :
10633 : :
10634 : : static void
10635 : 8 : print_debug_macro_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
10636 : : Ebl *ebl,
10637 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
10638 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
10639 : : {
10640 : 8 : Elf_Data *data = get_debug_elf_data (dbg, ebl, IDX_debug_macro, scn);
10641 [ + - ]: 8 : if (data == NULL)
10642 : 0 : return;
10643 : :
10644 : 8 : printf (_("\
10645 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
10646 : : elf_ndxscn (scn), section_name (ebl, shdr),
10647 : 8 : (uint64_t) shdr->sh_offset);
10648 : 8 : fputc ('\n', stdout);
10649 : :
10650 : : /* Get the source file information for all CUs. Uses same
10651 : : datastructure as macinfo. But uses offset field to directly
10652 : : match .debug_line offset. And just stored in a list. */
10653 : 8 : Dwarf_Off offset;
10654 : 8 : Dwarf_Off ncu = 0;
10655 : 8 : size_t hsize;
10656 : 8 : struct mac_culist *culist = NULL;
10657 [ + + ]: 20 : while (dwarf_nextcu (dbg, offset = ncu, &ncu, &hsize, NULL, NULL, NULL) == 0)
10658 : : {
10659 : 12 : Dwarf_Die cudie;
10660 [ - + ]: 12 : if (dwarf_offdie (dbg, offset + hsize, &cudie) == NULL)
10661 : 0 : continue;
10662 : :
10663 : 12 : Dwarf_Attribute attr;
10664 [ - + ]: 12 : if (dwarf_attr (&cudie, DW_AT_stmt_list, &attr) == NULL)
10665 : 0 : continue;
10666 : :
10667 : 12 : Dwarf_Word lineoff;
10668 [ - + ]: 12 : if (dwarf_formudata (&attr, &lineoff) != 0)
10669 : 0 : continue;
10670 : :
10671 : 12 : struct mac_culist *newp = (struct mac_culist *) alloca (sizeof (*newp));
10672 : 12 : newp->die = cudie;
10673 : 12 : newp->offset = lineoff;
10674 : 12 : newp->files = NULL;
10675 : 12 : newp->next = culist;
10676 : 12 : culist = newp;
10677 : : }
10678 : :
10679 : 8 : const unsigned char *readp = (const unsigned char *) data->d_buf;
10680 : 8 : const unsigned char *readendp = readp + data->d_size;
10681 : :
10682 [ + + ]: 28 : while (readp < readendp)
10683 : : {
10684 : 40 : printf (_(" Offset: 0x%" PRIx64 "\n"),
10685 : 20 : (uint64_t) (readp - (const unsigned char *) data->d_buf));
10686 : :
10687 : : // Header, 2 byte version, 1 byte flag, optional .debug_line offset,
10688 : : // optional vendor extension macro entry table.
10689 [ - + ]: 20 : if (readp + 2 > readendp)
10690 : : {
10691 : 0 : invalid_data:
10692 : 0 : error (0, 0, _("invalid data"));
10693 : 0 : return;
10694 : : }
10695 [ - + ]: 20 : const uint16_t vers = read_2ubyte_unaligned_inc (dbg, readp);
10696 : 20 : printf (_(" Version: %" PRIu16 "\n"), vers);
10697 : :
10698 : : // Version 4 is the GNU extension for DWARF4. DWARF5 will use version
10699 : : // 5 when it gets standardized.
10700 [ - + ]: 20 : if (vers != 4 && vers != 5)
10701 : : {
10702 : 0 : printf (_(" unknown version, cannot parse section\n"));
10703 : 0 : return;
10704 : : }
10705 : :
10706 [ - + ]: 20 : if (readp + 1 > readendp)
10707 : 0 : goto invalid_data;
10708 : 20 : const unsigned char flag = *readp++;
10709 : 20 : printf (_(" Flag: 0x%" PRIx8), flag);
10710 [ + + ]: 20 : if (flag != 0)
10711 : : {
10712 : 12 : printf (" (");
10713 [ - + ]: 12 : if ((flag & 0x01) != 0)
10714 : : {
10715 : 0 : printf ("offset_size");
10716 [ # # ]: 0 : if ((flag & 0xFE) != 0)
10717 : 0 : printf (", ");
10718 : : }
10719 [ + - ]: 12 : if ((flag & 0x02) != 0)
10720 : : {
10721 : 12 : printf ("debug_line_offset");
10722 [ - + ]: 12 : if ((flag & 0xFC) != 0)
10723 : 0 : printf (", ");
10724 : : }
10725 [ # # ]: 0 : if ((flag & 0x04) != 0)
10726 : : {
10727 : 0 : printf ("operands_table");
10728 [ # # ]: 0 : if ((flag & 0xF8) != 0)
10729 : 0 : printf (", ");
10730 : : }
10731 [ - + ]: 12 : if ((flag & 0xF8) != 0)
10732 : 0 : printf ("unknown");
10733 : 12 : printf (")");
10734 : : }
10735 : 20 : printf ("\n");
10736 : :
10737 [ + - ]: 20 : unsigned int offset_len = (flag & 0x01) ? 8 : 4;
10738 : 20 : printf (_(" Offset length: %" PRIu8 "\n"), offset_len);
10739 : 20 : Dwarf_Off line_offset = -1;
10740 [ + + ]: 20 : if (flag & 0x02)
10741 : : {
10742 [ - + ]: 12 : if (offset_len == 8)
10743 [ # # ]: 0 : line_offset = read_8ubyte_unaligned_inc (dbg, readp);
10744 : : else
10745 [ - + ]: 12 : line_offset = read_4ubyte_unaligned_inc (dbg, readp);
10746 : 12 : printf (_(" .debug_line offset: 0x%" PRIx64 "\n"),
10747 : : line_offset);
10748 : : }
10749 : :
10750 : 12 : struct mac_culist *cu = NULL;
10751 [ + - ]: 12 : if (line_offset != (Dwarf_Off) -1)
10752 : : {
10753 : : cu = culist;
10754 [ + - + + ]: 16 : while (cu != NULL && line_offset != cu->offset)
10755 : 4 : cu = cu->next;
10756 : : }
10757 : :
10758 [ - + ]: 20 : Dwarf_Off str_offsets_base = str_offsets_base_off (dbg, (cu != NULL
10759 : : ? cu->die.cu
10760 : : : NULL));
10761 : :
10762 : 20 : const unsigned char *vendor[DW_MACRO_hi_user - DW_MACRO_lo_user + 1];
10763 [ - + ]: 20 : memset (vendor, 0, sizeof vendor);
10764 [ - + ]: 20 : if (flag & 0x04)
10765 : : {
10766 : : // 1 byte length, for each item, 1 byte opcode, uleb128 number
10767 : : // of arguments, for each argument 1 byte form code.
10768 [ # # ]: 0 : if (readp + 1 > readendp)
10769 : 0 : goto invalid_data;
10770 : 0 : unsigned int tlen = *readp++;
10771 : 0 : printf (_(" extension opcode table, %" PRIu8 " items:\n"),
10772 : : tlen);
10773 [ # # ]: 0 : for (unsigned int i = 0; i < tlen; i++)
10774 : : {
10775 [ # # ]: 0 : if (readp + 1 > readendp)
10776 : 0 : goto invalid_data;
10777 : 0 : unsigned int opcode = *readp++;
10778 : 0 : printf (_(" [%" PRIx8 "]"), opcode);
10779 : 0 : if (opcode < DW_MACRO_lo_user
10780 [ # # ]: 0 : || opcode > DW_MACRO_hi_user)
10781 : 0 : goto invalid_data;
10782 : : // Record the start of description for this vendor opcode.
10783 : : // uleb128 nr args, 1 byte per arg form.
10784 : 0 : vendor[opcode - DW_MACRO_lo_user] = readp;
10785 [ # # ]: 0 : if (readp + 1 > readendp)
10786 : 0 : goto invalid_data;
10787 : 0 : unsigned int args = *readp++;
10788 [ # # ]: 0 : if (args > 0)
10789 : : {
10790 : 0 : printf (_(" %" PRIu8 " arguments:"), args);
10791 : 0 : while (args > 0)
10792 : : {
10793 : 0 : if (readp + 1 > readendp)
10794 : 0 : goto invalid_data;
10795 : 0 : unsigned int form = *readp++;
10796 : 0 : printf (" %s", dwarf_form_name (form));
10797 [ # # ]: 0 : if (! libdw_valid_user_form (form))
10798 : 0 : goto invalid_data;
10799 : 0 : args--;
10800 [ # # ]: 0 : if (args > 0)
10801 [ # # ]: 0 : putchar (',');
10802 : : }
10803 : : }
10804 : : else
10805 : 0 : printf (_(" no arguments."));
10806 : 0 : putchar ('\n');
10807 : : }
10808 : : }
10809 : 20 : putchar ('\n');
10810 : :
10811 : 20 : int level = 1;
10812 [ - + ]: 20 : if (readp + 1 > readendp)
10813 : 0 : goto invalid_data;
10814 : 20 : unsigned int opcode = *readp++;
10815 : 20 : while (opcode != 0)
10816 : : {
10817 : 3026 : unsigned int u128;
10818 : 3026 : unsigned int u128_2;
10819 : 3026 : const unsigned char *endp;
10820 : 3026 : uint64_t off;
10821 : :
10822 [ + + + - : 3026 : switch (opcode)
+ + + - -
- + + - ]
10823 : : {
10824 : 20 : case DW_MACRO_start_file:
10825 : 20 : get_uleb128 (u128, readp, readendp);
10826 [ - + ]: 20 : if (readp >= readendp)
10827 : 0 : goto invalid_data;
10828 : 20 : get_uleb128 (u128_2, readp, readendp);
10829 : :
10830 : : /* Find the CU DIE that matches this line offset. */
10831 : 20 : const char *fname = "???";
10832 [ - + ]: 20 : if (cu != NULL)
10833 : : {
10834 [ + + ]: 20 : if (cu->files == NULL
10835 [ - + ]: 12 : && dwarf_getsrcfiles (&cu->die, &cu->files,
10836 : : NULL) != 0)
10837 : 0 : cu->files = (Dwarf_Files *) -1l;
10838 : :
10839 [ - + ]: 20 : if (cu->files != (Dwarf_Files *) -1l)
10840 : 20 : fname = (dwarf_filesrc (cu->files, u128_2,
10841 [ - + ]: 20 : NULL, NULL) ?: "???");
10842 : : }
10843 : 20 : printf ("%*sstart_file %u, [%u] %s\n",
10844 : : level, "", u128, u128_2, fname);
10845 : 20 : ++level;
10846 : 20 : break;
10847 : :
10848 : 20 : case DW_MACRO_end_file:
10849 : 20 : --level;
10850 : 20 : printf ("%*send_file\n", level, "");
10851 : : break;
10852 : :
10853 : 2 : case DW_MACRO_define:
10854 : 2 : get_uleb128 (u128, readp, readendp);
10855 : 2 : endp = memchr (readp, '\0', readendp - readp);
10856 [ - + ]: 2 : if (endp == NULL)
10857 : 0 : goto invalid_data;
10858 : 2 : printf ("%*s#define %s, line %u\n",
10859 : : level, "", readp, u128);
10860 : 2 : readp = endp + 1;
10861 : 2 : break;
10862 : :
10863 : 0 : case DW_MACRO_undef:
10864 : 0 : get_uleb128 (u128, readp, readendp);
10865 : 0 : endp = memchr (readp, '\0', readendp - readp);
10866 [ # # ]: 0 : if (endp == NULL)
10867 : 0 : goto invalid_data;
10868 : 0 : printf ("%*s#undef %s, line %u\n",
10869 : : level, "", readp, u128);
10870 : 0 : readp = endp + 1;
10871 : 0 : break;
10872 : :
10873 : 1414 : case DW_MACRO_define_strp:
10874 : 1414 : get_uleb128 (u128, readp, readendp);
10875 [ - + ]: 1414 : if (readp + offset_len > readendp)
10876 : 0 : goto invalid_data;
10877 [ - + ]: 1414 : if (offset_len == 8)
10878 [ # # ]: 0 : off = read_8ubyte_unaligned_inc (dbg, readp);
10879 : : else
10880 [ - + ]: 1414 : off = read_4ubyte_unaligned_inc (dbg, readp);
10881 : 1414 : printf ("%*s#define %s, line %u (indirect)\n",
10882 : : level, "", dwarf_getstring (dbg, off, NULL), u128);
10883 : : break;
10884 : :
10885 : 2 : case DW_MACRO_undef_strp:
10886 : 2 : get_uleb128 (u128, readp, readendp);
10887 [ - + ]: 2 : if (readp + offset_len > readendp)
10888 : 0 : goto invalid_data;
10889 [ - + ]: 2 : if (offset_len == 8)
10890 [ # # ]: 0 : off = read_8ubyte_unaligned_inc (dbg, readp);
10891 : : else
10892 [ - + ]: 2 : off = read_4ubyte_unaligned_inc (dbg, readp);
10893 : 2 : printf ("%*s#undef %s, line %u (indirect)\n",
10894 : : level, "", dwarf_getstring (dbg, off, NULL), u128);
10895 : : break;
10896 : :
10897 : 12 : case DW_MACRO_import:
10898 [ - + ]: 12 : if (readp + offset_len > readendp)
10899 : 0 : goto invalid_data;
10900 [ - + ]: 12 : if (offset_len == 8)
10901 [ # # ]: 0 : off = read_8ubyte_unaligned_inc (dbg, readp);
10902 : : else
10903 [ - + ]: 12 : off = read_4ubyte_unaligned_inc (dbg, readp);
10904 : 12 : printf ("%*s#include offset 0x%" PRIx64 "\n",
10905 : : level, "", off);
10906 : : break;
10907 : :
10908 : 0 : case DW_MACRO_define_sup:
10909 : 0 : get_uleb128 (u128, readp, readendp);
10910 : 0 : printf ("%*s#define ", level, "");
10911 : 0 : readp = print_form_data (dbg, DW_FORM_strp_sup,
10912 : : readp, readendp, offset_len,
10913 : : str_offsets_base);
10914 : 0 : printf (", line %u (sup)\n", u128);
10915 : : break;
10916 : :
10917 : 0 : case DW_MACRO_undef_sup:
10918 : 0 : get_uleb128 (u128, readp, readendp);
10919 : 0 : printf ("%*s#undef ", level, "");
10920 : 0 : readp = print_form_data (dbg, DW_FORM_strp_sup,
10921 : : readp, readendp, offset_len,
10922 : : str_offsets_base);
10923 : 0 : printf (", line %u (sup)\n", u128);
10924 : : break;
10925 : :
10926 : 0 : case DW_MACRO_import_sup:
10927 [ # # ]: 0 : if (readp + offset_len > readendp)
10928 : 0 : goto invalid_data;
10929 [ # # ]: 0 : if (offset_len == 8)
10930 [ # # ]: 0 : off = read_8ubyte_unaligned_inc (dbg, readp);
10931 : : else
10932 [ # # ]: 0 : off = read_4ubyte_unaligned_inc (dbg, readp);
10933 : : // XXX Needs support for reading from supplementary object file.
10934 : 0 : printf ("%*s#include offset 0x%" PRIx64 " (sup)\n",
10935 : : level, "", off);
10936 : : break;
10937 : :
10938 : 1552 : case DW_MACRO_define_strx:
10939 : 1552 : get_uleb128 (u128, readp, readendp);
10940 : 1552 : printf ("%*s#define ", level, "");
10941 : 1552 : readp = print_form_data (dbg, DW_FORM_strx,
10942 : : readp, readendp, offset_len,
10943 : : str_offsets_base);
10944 : 1552 : printf (", line %u (strx)\n", u128);
10945 : : break;
10946 : :
10947 : 4 : case DW_MACRO_undef_strx:
10948 : 4 : get_uleb128 (u128, readp, readendp);
10949 : 4 : printf ("%*s#undef ", level, "");
10950 : 4 : readp = print_form_data (dbg, DW_FORM_strx,
10951 : : readp, readendp, offset_len,
10952 : : str_offsets_base);
10953 : 4 : printf (", line %u (strx)\n", u128);
10954 : : break;
10955 : :
10956 : : default:
10957 : 0 : printf ("%*svendor opcode 0x%" PRIx8, level, "", opcode);
10958 [ # # ]: 0 : if (opcode < DW_MACRO_lo_user
10959 : : || opcode > DW_MACRO_lo_user
10960 [ # # ]: 0 : || vendor[opcode - DW_MACRO_lo_user] == NULL)
10961 : 0 : goto invalid_data;
10962 : :
10963 : 0 : const unsigned char *op_desc;
10964 : 0 : op_desc = vendor[opcode - DW_MACRO_lo_user];
10965 : :
10966 : : // Just skip the arguments, we cannot really interpret them,
10967 : : // but print as much as we can.
10968 : 0 : unsigned int args = *op_desc++;
10969 [ # # ]: 0 : while (args > 0 && readp < readendp)
10970 : : {
10971 : 0 : unsigned int form = *op_desc++;
10972 : 0 : readp = print_form_data (dbg, form, readp, readendp,
10973 : : offset_len, str_offsets_base);
10974 : 0 : args--;
10975 [ # # ]: 0 : if (args > 0)
10976 [ # # ]: 0 : printf (", ");
10977 : : }
10978 : 0 : putchar ('\n');
10979 : : }
10980 : :
10981 [ - + ]: 3026 : if (readp + 1 > readendp)
10982 : 0 : goto invalid_data;
10983 : 3026 : opcode = *readp++;
10984 [ + + ]: 3026 : if (opcode == 0)
10985 [ + + ]: 3066 : putchar ('\n');
10986 : : }
10987 : : }
10988 : : }
10989 : :
10990 : :
10991 : : /* Callback for printing global names. */
10992 : : static int
10993 : 68 : print_pubnames (Dwarf *dbg __attribute__ ((unused)), Dwarf_Global *global,
10994 : : void *arg)
10995 : : {
10996 : 68 : int *np = (int *) arg;
10997 : :
10998 : 136 : printf (_(" [%5d] DIE offset: %6" PRId64
10999 : : ", CU DIE offset: %6" PRId64 ", name: %s\n"),
11000 : 68 : (*np)++, global->die_offset, global->cu_offset, global->name);
11001 : :
11002 : 68 : return 0;
11003 : : }
11004 : :
11005 : :
11006 : : /* Print the known exported symbols in the DWARF section '.debug_pubnames'. */
11007 : : static void
11008 : 16 : print_debug_pubnames_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
11009 : : Ebl *ebl,
11010 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
11011 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
11012 : : {
11013 : : /* Check section actually exists. */
11014 [ - + ]: 16 : if (get_debug_elf_data (dbg, ebl, IDX_debug_pubnames, scn) == NULL)
11015 : 0 : return;
11016 : :
11017 : 16 : printf (_("\nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
11018 : : elf_ndxscn (scn), section_name (ebl, shdr),
11019 : 16 : (uint64_t) shdr->sh_offset);
11020 : :
11021 : 16 : int n = 0;
11022 : 16 : (void) dwarf_getpubnames (dbg, print_pubnames, &n, 0);
11023 : : }
11024 : :
11025 : : /* Print the content of the DWARF string section '.debug_str'
11026 : : or 'debug_line_str'. */
11027 : : static void
11028 : 134 : print_debug_str_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
11029 : : Ebl *ebl,
11030 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
11031 : : Elf_Scn *scn, GElf_Shdr *shdr,
11032 : : Dwarf *dbg __attribute__ ((unused)))
11033 : : {
11034 : 134 : const char *name = section_name (ebl, shdr);
11035 [ + + ]: 134 : int idx = ((name != NULL && strstr (name, "debug_line_str") != NULL)
11036 [ + - ]: 134 : ? IDX_debug_line_str : IDX_debug_str);
11037 : 134 : Elf_Data *data = get_debug_elf_data (dbg, ebl, idx, scn);
11038 [ + - ]: 134 : if (data == NULL)
11039 : : return;
11040 : :
11041 : 134 : const size_t sh_size = data->d_size;
11042 : :
11043 : : /* Compute floor(log16(shdr->sh_size)). */
11044 : 134 : GElf_Addr tmp = sh_size;
11045 : 134 : int digits = 1;
11046 [ + + ]: 466 : while (tmp >= 16)
11047 : : {
11048 : 332 : ++digits;
11049 : 332 : tmp >>= 4;
11050 : : }
11051 : 134 : digits = MAX (4, digits);
11052 : :
11053 : 134 : printf (_("\nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"
11054 : : " %*s String\n"),
11055 : : elf_ndxscn (scn),
11056 : 134 : section_name (ebl, shdr), (uint64_t) shdr->sh_offset,
11057 : : /* TRANS: the debugstr| prefix makes the string unique. */
11058 [ + - ]: 134 : digits + 2, sgettext ("debugstr|Offset"));
11059 : :
11060 : 134 : Dwarf_Off offset = 0;
11061 [ + + ]: 161060 : while (offset < sh_size)
11062 : : {
11063 : 160926 : size_t len;
11064 : 160926 : const char *str = (const char *) data->d_buf + offset;
11065 : 160926 : const char *endp = memchr (str, '\0', sh_size - offset);
11066 [ - + ]: 160926 : if (unlikely (endp == NULL))
11067 : : {
11068 : 0 : printf (_(" *** error, missing string terminator\n"));
11069 : : break;
11070 : : }
11071 : :
11072 : 160926 : printf (" [%*" PRIx64 "] \"%s\"\n", digits, (uint64_t) offset, str);
11073 : 160926 : len = endp - str;
11074 : 160926 : offset += len + 1;
11075 : : }
11076 : : }
11077 : :
11078 : : static void
11079 : 12 : print_debug_str_offsets_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
11080 : : Ebl *ebl,
11081 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
11082 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
11083 : : {
11084 : 12 : Elf_Data *data = get_debug_elf_data (dbg, ebl, IDX_debug_str_offsets, scn);
11085 [ + - ]: 12 : if (data == NULL)
11086 : : return;
11087 : :
11088 : 12 : printf (_("\
11089 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
11090 : : elf_ndxscn (scn), section_name (ebl, shdr),
11091 : 12 : (uint64_t) shdr->sh_offset);
11092 : :
11093 [ + - ]: 12 : if (shdr->sh_size == 0)
11094 : : return;
11095 : :
11096 : 12 : size_t idx = 0;
11097 : 12 : sort_listptr (&known_stroffbases, "str_offsets");
11098 : :
11099 : 12 : const unsigned char *start = (const unsigned char *) data->d_buf;
11100 : 12 : const unsigned char *readp = start;
11101 : 12 : const unsigned char *readendp = ((const unsigned char *) data->d_buf
11102 : 12 : + data->d_size);
11103 : :
11104 : 12 : while (readp < readendp)
11105 : : {
11106 : : /* Most string offset tables will have a header. For split
11107 : : dwarf unit GNU DebugFission didn't add one. But they were
11108 : : also only defined for split units (main or skeleton units
11109 : : didn't have indirect strings). So if we don't have a
11110 : : DW_AT_str_offsets_base at all and this is offset zero, then
11111 : : just start printing offsets immediately, if this is a .dwo
11112 : : section. */
11113 : 16 : Dwarf_Off off = (Dwarf_Off) (readp
11114 : 16 : - (const unsigned char *) data->d_buf);
11115 : :
11116 : 16 : printf ("Table at offset %" PRIx64 " ", off);
11117 : :
11118 [ + + ]: 16 : struct listptr *listptr = get_listptr (&known_stroffbases, idx++);
11119 : 16 : const unsigned char *next_unitp = readendp;
11120 : 8 : uint8_t offset_size;
11121 : 8 : bool has_header;
11122 [ - + ]: 8 : if (listptr == NULL)
11123 : : {
11124 : : /* This can happen for .dwo files. There is only an header
11125 : : in the case this is a version 5 split DWARF file. */
11126 : 8 : Dwarf_CU *cu;
11127 : 8 : uint8_t unit_type;
11128 [ - + ]: 8 : if (dwarf_get_units (dbg, NULL, &cu, NULL, &unit_type,
11129 : : NULL, NULL) != 0)
11130 : : {
11131 : 0 : error (0, 0, "Warning: Cannot find any DWARF unit.");
11132 : : /* Just guess some values. */
11133 : 0 : has_header = false;
11134 : 0 : offset_size = 4;
11135 : : }
11136 [ + - ]: 8 : else if (off == 0
11137 : 8 : && (unit_type == DW_UT_split_type
11138 [ + - ]: 8 : || unit_type == DW_UT_split_compile))
11139 : : {
11140 : 8 : has_header = cu->version > 4;
11141 : 8 : offset_size = cu->offset_size;
11142 : : }
11143 : : else
11144 : : {
11145 : 0 : error (0, 0,
11146 : : "Warning: No CU references .debug_str_offsets after %"
11147 : : PRIx64, off);
11148 : 0 : has_header = cu->version > 4;
11149 : 0 : offset_size = cu->offset_size;
11150 : : }
11151 : 8 : printf ("\n");
11152 : : }
11153 : : else
11154 : : {
11155 : : /* This must be DWARF5, since GNU DebugFission didn't define
11156 : : DW_AT_str_offsets_base. */
11157 : 8 : has_header = true;
11158 : :
11159 : 8 : Dwarf_Die cudie;
11160 [ - + ]: 8 : if (dwarf_cu_die (listptr->cu, &cudie,
11161 : : NULL, NULL, NULL, NULL,
11162 : : NULL, NULL) == NULL)
11163 : 0 : printf ("Unknown CU (%s):\n", dwarf_errmsg (-1));
11164 : : else
11165 : 8 : printf ("for CU [%6" PRIx64 "]:\n", dwarf_dieoffset (&cudie));
11166 : : }
11167 : :
11168 [ + + ]: 16 : if (has_header)
11169 : : {
11170 : 12 : uint64_t unit_length;
11171 : 12 : uint16_t version;
11172 : 12 : uint16_t padding;
11173 : :
11174 [ - + ]: 12 : unit_length = read_4ubyte_unaligned_inc (dbg, readp);
11175 [ - + ]: 12 : if (unlikely (unit_length == 0xffffffff))
11176 : : {
11177 [ # # ]: 0 : if (unlikely (readp > readendp - 8))
11178 : : {
11179 : 0 : invalid_data:
11180 : 0 : error (0, 0, "Invalid data");
11181 : 0 : return;
11182 : : }
11183 [ # # ]: 0 : unit_length = read_8ubyte_unaligned_inc (dbg, readp);
11184 : 0 : offset_size = 8;
11185 : : }
11186 : : else
11187 : : offset_size = 4;
11188 : :
11189 : 12 : printf ("\n");
11190 : 12 : printf (_(" Length: %8" PRIu64 "\n"),
11191 : : unit_length);
11192 : 12 : printf (_(" Offset size: %8" PRIu8 "\n"),
11193 : : offset_size);
11194 : :
11195 : : /* We need at least 2-bytes (version) + 2-bytes (padding) =
11196 : : 4 bytes to complete the header. And this unit cannot go
11197 : : beyond the section data. */
11198 [ + - ]: 12 : if (readp > readendp - 4
11199 [ + - ]: 12 : || unit_length < 4
11200 [ - + ]: 12 : || unit_length > (uint64_t) (readendp - readp))
11201 : 0 : goto invalid_data;
11202 : :
11203 : 12 : next_unitp = readp + unit_length;
11204 : :
11205 [ - + ]: 12 : version = read_2ubyte_unaligned_inc (dbg, readp);
11206 : 12 : printf (_(" DWARF version: %8" PRIu16 "\n"), version);
11207 : :
11208 [ - + ]: 12 : if (version != 5)
11209 : : {
11210 : 0 : error (0, 0, _("Unknown version"));
11211 : 0 : goto next_unit;
11212 : : }
11213 : :
11214 [ - + ]: 12 : padding = read_2ubyte_unaligned_inc (dbg, readp);
11215 : 12 : printf (_(" Padding: %8" PRIx16 "\n"), padding);
11216 : :
11217 [ + + ]: 12 : if (listptr != NULL
11218 [ - + ]: 8 : && listptr->offset != (Dwarf_Off) (readp - start))
11219 : : {
11220 : 0 : error (0, 0, "String offsets index doesn't start after header");
11221 : 0 : goto next_unit;
11222 : : }
11223 : :
11224 : 12 : printf ("\n");
11225 : : }
11226 : :
11227 : 16 : int digits = 1;
11228 : 16 : size_t offsets = (next_unitp - readp) / offset_size;
11229 [ + + ]: 24 : while (offsets >= 10)
11230 : : {
11231 : 8 : ++digits;
11232 : 8 : offsets /= 10;
11233 : : }
11234 : :
11235 : 16 : unsigned int uidx = 0;
11236 : 16 : size_t index_offset = readp - (const unsigned char *) data->d_buf;
11237 : 16 : printf (" Offsets start at 0x%zx:\n", index_offset);
11238 : 204 : while (readp <= next_unitp - offset_size)
11239 : : {
11240 : 188 : Dwarf_Word offset;
11241 [ + - ]: 188 : if (offset_size == 4)
11242 [ - + ]: 188 : offset = read_4ubyte_unaligned_inc (dbg, readp);
11243 : : else
11244 [ # # ]: 0 : offset = read_8ubyte_unaligned_inc (dbg, readp);
11245 : 188 : const char *str = dwarf_getstring (dbg, offset, NULL);
11246 [ - + + + ]: 392 : printf (" [%*u] [%*" PRIx64 "] \"%s\"\n",
11247 : 188 : digits, uidx++, (int) offset_size * 2, offset, str ?: "???");
11248 : : }
11249 : 16 : printf ("\n");
11250 : :
11251 [ + - ]: 16 : if (readp != next_unitp)
11252 [ + + ]: 28 : error (0, 0, "extra %zd bytes at end of unit",
11253 : 0 : (size_t) (next_unitp - readp));
11254 : :
11255 : 16 : next_unit:
11256 : : readp = next_unitp;
11257 : : }
11258 : : }
11259 : :
11260 : :
11261 : : /* Print the content of the call frame search table section
11262 : : '.eh_frame_hdr'. */
11263 : : static void
11264 : 140 : print_debug_frame_hdr_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
11265 : : Ebl *ebl __attribute__ ((unused)),
11266 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
11267 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
11268 : : {
11269 : 140 : printf (_("\
11270 : : \nCall frame search table section [%2zu] '.eh_frame_hdr':\n"),
11271 : : elf_ndxscn (scn));
11272 : :
11273 : 140 : Elf_Data *data = elf_rawdata (scn, NULL);
11274 : :
11275 [ - + ]: 140 : if (unlikely (data == NULL))
11276 : : {
11277 : 0 : error (0, 0, _("cannot get %s content: %s"),
11278 : : ".eh_frame_hdr", elf_errmsg (-1));
11279 : 0 : return;
11280 : : }
11281 : :
11282 : 140 : const unsigned char *readp = data->d_buf;
11283 : 140 : const unsigned char *const dataend = ((unsigned char *) data->d_buf
11284 : 140 : + data->d_size);
11285 : :
11286 [ - + ]: 140 : if (unlikely (readp + 4 > dataend))
11287 : : {
11288 : 0 : invalid_data:
11289 : 0 : error (0, 0, _("invalid data"));
11290 : 0 : return;
11291 : : }
11292 : :
11293 : 140 : unsigned int version = *readp++;
11294 : 140 : unsigned int eh_frame_ptr_enc = *readp++;
11295 : 140 : unsigned int fde_count_enc = *readp++;
11296 : 140 : unsigned int table_enc = *readp++;
11297 : :
11298 : 140 : printf (" version: %u\n"
11299 : : " eh_frame_ptr_enc: %#x ",
11300 : : version, eh_frame_ptr_enc);
11301 : 140 : print_encoding_base ("", eh_frame_ptr_enc);
11302 : 140 : printf (" fde_count_enc: %#x ", fde_count_enc);
11303 : 140 : print_encoding_base ("", fde_count_enc);
11304 : 140 : printf (" table_enc: %#x ", table_enc);
11305 : 140 : print_encoding_base ("", table_enc);
11306 : :
11307 : 140 : uint64_t eh_frame_ptr = 0;
11308 [ + - ]: 140 : if (eh_frame_ptr_enc != DW_EH_PE_omit)
11309 : : {
11310 : 140 : readp = read_encoded (eh_frame_ptr_enc, readp, dataend, &eh_frame_ptr,
11311 : : dbg);
11312 [ - + ]: 140 : if (unlikely (readp == NULL))
11313 : 0 : goto invalid_data;
11314 : :
11315 : 140 : printf (" eh_frame_ptr: %#" PRIx64, eh_frame_ptr);
11316 [ + - ]: 140 : if ((eh_frame_ptr_enc & 0x70) == DW_EH_PE_pcrel)
11317 : 280 : printf (" (offset: %#" PRIx64 ")",
11318 : : /* +4 because of the 4 byte header of the section. */
11319 : 140 : (uint64_t) shdr->sh_offset + 4 + eh_frame_ptr);
11320 : :
11321 : 140 : putchar ('\n');
11322 : : }
11323 : :
11324 : 140 : uint64_t fde_count = 0;
11325 [ + - ]: 140 : if (fde_count_enc != DW_EH_PE_omit)
11326 : : {
11327 : 140 : readp = read_encoded (fde_count_enc, readp, dataend, &fde_count, dbg);
11328 [ - + ]: 140 : if (unlikely (readp == NULL))
11329 : 0 : goto invalid_data;
11330 : :
11331 : 140 : printf (" fde_count: %" PRIu64 "\n", fde_count);
11332 : : }
11333 : :
11334 [ + - + - ]: 140 : if (fde_count == 0 || table_enc == DW_EH_PE_omit)
11335 : : return;
11336 : :
11337 : 140 : puts (" Table:");
11338 : :
11339 : : /* Optimize for the most common case. */
11340 [ + - ]: 140 : if (table_enc == (DW_EH_PE_datarel | DW_EH_PE_sdata4))
11341 : 21912 : while (fde_count > 0 && readp + 8 <= dataend)
11342 : : {
11343 [ - + ]: 21772 : int32_t initial_location = read_4sbyte_unaligned_inc (dbg, readp);
11344 : 21772 : uint64_t initial_offset = ((uint64_t) shdr->sh_offset
11345 : 21772 : + (int64_t) initial_location);
11346 [ - + ]: 21772 : int32_t address = read_4sbyte_unaligned_inc (dbg, readp);
11347 : : // XXX Possibly print symbol name or section offset for initial_offset
11348 [ + + ]: 21912 : printf (" %#" PRIx32 " (offset: %#6" PRIx64 ") -> %#" PRIx32
11349 : : " fde=[%6" PRIx64 "]\n",
11350 : : initial_location, initial_offset,
11351 : 21772 : address, address - (eh_frame_ptr + 4));
11352 : : }
11353 : : else
11354 : 140 : while (0 && readp < dataend)
11355 : : {
11356 : :
11357 : 140 : }
11358 : : }
11359 : :
11360 : :
11361 : : /* Print the content of the exception handling table section
11362 : : '.eh_frame_hdr'. */
11363 : : static void
11364 : 0 : print_debug_exception_table (Dwfl_Module *dwflmod __attribute__ ((unused)),
11365 : : Ebl *ebl __attribute__ ((unused)),
11366 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
11367 : : Elf_Scn *scn,
11368 : : GElf_Shdr *shdr __attribute__ ((unused)),
11369 : : Dwarf *dbg __attribute__ ((unused)))
11370 : : {
11371 : 0 : printf (_("\
11372 : : \nException handling table section [%2zu] '.gcc_except_table':\n"),
11373 : : elf_ndxscn (scn));
11374 : :
11375 : 0 : Elf_Data *data = elf_rawdata (scn, NULL);
11376 : :
11377 [ # # ]: 0 : if (unlikely (data == NULL))
11378 : : {
11379 : 0 : error (0, 0, _("cannot get %s content: %s"),
11380 : : ".gcc_except_table", elf_errmsg (-1));
11381 : 0 : return;
11382 : : }
11383 : :
11384 : 0 : const unsigned char *readp = data->d_buf;
11385 : 0 : const unsigned char *const dataend = readp + data->d_size;
11386 : :
11387 [ # # ]: 0 : if (unlikely (readp + 1 > dataend))
11388 : : {
11389 : 0 : invalid_data:
11390 : 0 : error (0, 0, _("invalid data"));
11391 : 0 : return;
11392 : : }
11393 : 0 : unsigned int lpstart_encoding = *readp++;
11394 : 0 : printf (_(" LPStart encoding: %#x "), lpstart_encoding);
11395 : 0 : print_encoding_base ("", lpstart_encoding);
11396 [ # # ]: 0 : if (lpstart_encoding != DW_EH_PE_omit)
11397 : : {
11398 : 0 : uint64_t lpstart;
11399 : 0 : readp = read_encoded (lpstart_encoding, readp, dataend, &lpstart, dbg);
11400 : 0 : printf (" LPStart: %#" PRIx64 "\n", lpstart);
11401 : : }
11402 : :
11403 [ # # ]: 0 : if (unlikely (readp + 1 > dataend))
11404 : 0 : goto invalid_data;
11405 : 0 : unsigned int ttype_encoding = *readp++;
11406 : 0 : printf (_(" TType encoding: %#x "), ttype_encoding);
11407 : 0 : print_encoding_base ("", ttype_encoding);
11408 : 0 : const unsigned char *ttype_base = NULL;
11409 [ # # ]: 0 : if (ttype_encoding != DW_EH_PE_omit)
11410 : : {
11411 : 0 : unsigned int ttype_base_offset;
11412 [ # # ]: 0 : if (readp >= dataend)
11413 : 0 : goto invalid_data;
11414 : 0 : get_uleb128 (ttype_base_offset, readp, dataend);
11415 : 0 : printf (" TType base offset: %#x\n", ttype_base_offset);
11416 [ # # ]: 0 : if ((size_t) (dataend - readp) > ttype_base_offset)
11417 : 0 : ttype_base = readp + ttype_base_offset;
11418 : : }
11419 : :
11420 [ # # ]: 0 : if (unlikely (readp + 1 > dataend))
11421 : 0 : goto invalid_data;
11422 : 0 : unsigned int call_site_encoding = *readp++;
11423 : 0 : printf (_(" Call site encoding: %#x "), call_site_encoding);
11424 : 0 : print_encoding_base ("", call_site_encoding);
11425 : 0 : unsigned int call_site_table_len;
11426 [ # # ]: 0 : if (readp >= dataend)
11427 : 0 : goto invalid_data;
11428 : 0 : get_uleb128 (call_site_table_len, readp, dataend);
11429 : :
11430 : 0 : const unsigned char *const action_table = readp + call_site_table_len;
11431 [ # # ]: 0 : if (unlikely (action_table > dataend))
11432 : 0 : goto invalid_data;
11433 : : unsigned int u = 0;
11434 : : unsigned int max_action = 0;
11435 [ # # ]: 0 : while (readp < action_table)
11436 : : {
11437 [ # # ]: 0 : if (u == 0)
11438 : 0 : puts (_("\n Call site table:"));
11439 : :
11440 : 0 : uint64_t call_site_start;
11441 : 0 : readp = read_encoded (call_site_encoding, readp, dataend,
11442 : : &call_site_start, dbg);
11443 : 0 : uint64_t call_site_length;
11444 : 0 : readp = read_encoded (call_site_encoding, readp, dataend,
11445 : : &call_site_length, dbg);
11446 : 0 : uint64_t landing_pad;
11447 : 0 : readp = read_encoded (call_site_encoding, readp, dataend,
11448 : : &landing_pad, dbg);
11449 : 0 : unsigned int action;
11450 [ # # ]: 0 : if (readp >= dataend)
11451 : 0 : goto invalid_data;
11452 : 0 : get_uleb128 (action, readp, dataend);
11453 : 0 : max_action = MAX (action, max_action);
11454 : 0 : printf (_(" [%4u] Call site start: %#" PRIx64 "\n"
11455 : : " Call site length: %" PRIu64 "\n"
11456 : : " Landing pad: %#" PRIx64 "\n"
11457 : : " Action: %u\n"),
11458 : : u++, call_site_start, call_site_length, landing_pad, action);
11459 : : }
11460 [ # # ]: 0 : if (readp != action_table)
11461 : 0 : goto invalid_data;
11462 : :
11463 : 0 : unsigned int max_ar_filter = 0;
11464 [ # # ]: 0 : if (max_action > 0)
11465 : : {
11466 : 0 : puts ("\n Action table:");
11467 : :
11468 : 0 : size_t maxdata = (size_t) (dataend - action_table);
11469 [ # # # # ]: 0 : if (max_action > maxdata || maxdata - max_action < 1)
11470 : : {
11471 : 0 : invalid_action_table:
11472 : 0 : fputs (_(" <INVALID DATA>\n"), stdout);
11473 : 0 : return;
11474 : : }
11475 : :
11476 : 0 : const unsigned char *const action_table_end
11477 : 0 : = action_table + max_action + 1;
11478 : :
11479 : 0 : u = 0;
11480 : 0 : do
11481 : : {
11482 : 0 : int ar_filter;
11483 : 0 : get_sleb128 (ar_filter, readp, action_table_end);
11484 [ # # ]: 0 : if (ar_filter > 0 && (unsigned int) ar_filter > max_ar_filter)
11485 : 0 : max_ar_filter = ar_filter;
11486 : 0 : int ar_disp;
11487 [ # # ]: 0 : if (readp >= action_table_end)
11488 : 0 : goto invalid_action_table;
11489 : 0 : get_sleb128 (ar_disp, readp, action_table_end);
11490 : :
11491 : 0 : printf (" [%4u] ar_filter: % d\n"
11492 : : " ar_disp: % -5d",
11493 : : u, ar_filter, ar_disp);
11494 [ # # ]: 0 : if (abs (ar_disp) & 1)
11495 : 0 : printf (" -> [%4u]\n", u + (ar_disp + 1) / 2);
11496 [ # # ]: 0 : else if (ar_disp != 0)
11497 : 0 : puts (" -> ???");
11498 : : else
11499 : 0 : putchar ('\n');
11500 : 0 : ++u;
11501 : : }
11502 [ # # ]: 0 : while (readp < action_table_end);
11503 : : }
11504 : :
11505 [ # # ]: 0 : if (max_ar_filter > 0 && ttype_base != NULL)
11506 : : {
11507 : 0 : unsigned char dsize;
11508 : 0 : puts ("\n TType table:");
11509 : :
11510 : : // XXX Not *4, size of encoding;
11511 [ # # ]: 0 : switch (ttype_encoding & 7)
11512 : : {
11513 : : case DW_EH_PE_udata2:
11514 : : case DW_EH_PE_sdata2:
11515 : : dsize = 2;
11516 : : break;
11517 : : case DW_EH_PE_udata4:
11518 : : case DW_EH_PE_sdata4:
11519 : : dsize = 4;
11520 : : break;
11521 : : case DW_EH_PE_udata8:
11522 : : case DW_EH_PE_sdata8:
11523 : : dsize = 8;
11524 : : break;
11525 : 0 : default:
11526 : 0 : dsize = 0;
11527 : 0 : error (1, 0, _("invalid TType encoding"));
11528 : : }
11529 : :
11530 : 0 : if (max_ar_filter
11531 [ # # ]: 0 : > (size_t) (ttype_base - (const unsigned char *) data->d_buf) / dsize)
11532 : 0 : goto invalid_data;
11533 : :
11534 : 0 : readp = ttype_base - max_ar_filter * dsize;
11535 : 0 : do
11536 : : {
11537 : 0 : uint64_t ttype;
11538 : 0 : readp = read_encoded (ttype_encoding, readp, ttype_base, &ttype,
11539 : : dbg);
11540 : 0 : printf (" [%4u] %#" PRIx64 "\n", max_ar_filter--, ttype);
11541 : : }
11542 [ # # ]: 0 : while (readp < ttype_base);
11543 : : }
11544 : : }
11545 : :
11546 : : /* Print the content of the '.gdb_index' section.
11547 : : http://sourceware.org/gdb/current/onlinedocs/gdb/Index-Section-Format.html
11548 : : */
11549 : : static void
11550 : 8 : print_gdb_index_section (Dwfl_Module *dwflmod, Ebl *ebl,
11551 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
11552 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
11553 : : {
11554 : 8 : printf (_("\nGDB section [%2zu] '%s' at offset %#" PRIx64
11555 : : " contains %" PRId64 " bytes :\n"),
11556 : : elf_ndxscn (scn), section_name (ebl, shdr),
11557 : 8 : (uint64_t) shdr->sh_offset, (uint64_t) shdr->sh_size);
11558 : :
11559 : 8 : Elf_Data *data = elf_rawdata (scn, NULL);
11560 : :
11561 [ - + ]: 8 : if (unlikely (data == NULL))
11562 : : {
11563 : 0 : error (0, 0, _("cannot get %s content: %s"),
11564 : : ".gdb_index", elf_errmsg (-1));
11565 : 4 : return;
11566 : : }
11567 : :
11568 : : // .gdb_index is always in little endian.
11569 : 8 : Dwarf dummy_dbg = { .other_byte_order = MY_ELFDATA != ELFDATA2LSB };
11570 : 8 : dbg = &dummy_dbg;
11571 : :
11572 : 8 : const unsigned char *readp = data->d_buf;
11573 : 8 : const unsigned char *const dataend = readp + data->d_size;
11574 : :
11575 [ - + ]: 8 : if (unlikely (readp + 4 > dataend))
11576 : : {
11577 : 0 : invalid_data:
11578 : 0 : error (0, 0, _("invalid data"));
11579 : 0 : return;
11580 : : }
11581 : :
11582 : 8 : int32_t vers = read_4ubyte_unaligned (dbg, readp);
11583 : 8 : printf (_(" Version: %" PRId32 "\n"), vers);
11584 : :
11585 : : // The only difference between version 4 and version 5 is the
11586 : : // hash used for generating the table. Version 6 contains symbols
11587 : : // for inlined functions, older versions didn't. Version 7 adds
11588 : : // symbol kinds. Version 8 just indicates that it correctly includes
11589 : : // TUs for symbols. Version 9 adds shortcut table for information
11590 : : // regarding the main function.
11591 [ - + ]: 8 : if (vers < 4 || vers > 9)
11592 : : {
11593 : 0 : printf (_(" unknown version, cannot parse section\n"));
11594 : 0 : return;
11595 : : }
11596 : :
11597 : 8 : readp += 4;
11598 [ - + ]: 8 : if (unlikely (readp + 4 > dataend))
11599 : 0 : goto invalid_data;
11600 : :
11601 : 8 : uint32_t cu_off = read_4ubyte_unaligned (dbg, readp);
11602 : 8 : printf (_(" CU offset: %#" PRIx32 "\n"), cu_off);
11603 : :
11604 : 8 : readp += 4;
11605 [ - + ]: 8 : if (unlikely (readp + 4 > dataend))
11606 : 0 : goto invalid_data;
11607 : :
11608 : 8 : uint32_t tu_off = read_4ubyte_unaligned (dbg, readp);
11609 : 8 : printf (_(" TU offset: %#" PRIx32 "\n"), tu_off);
11610 : :
11611 : 8 : readp += 4;
11612 [ - + ]: 8 : if (unlikely (readp + 4 > dataend))
11613 : 0 : goto invalid_data;
11614 : :
11615 : 8 : uint32_t addr_off = read_4ubyte_unaligned (dbg, readp);
11616 : 8 : printf (_(" address offset: %#" PRIx32 "\n"), addr_off);
11617 : :
11618 : 8 : readp += 4;
11619 [ - + ]: 8 : if (unlikely (readp + 4 > dataend))
11620 : 0 : goto invalid_data;
11621 : :
11622 : 8 : uint32_t sym_off = read_4ubyte_unaligned (dbg, readp);
11623 : 8 : printf (_(" symbol offset: %#" PRIx32 "\n"), sym_off);
11624 : :
11625 : 8 : readp += 4;
11626 [ - + ]: 8 : if (unlikely (readp + 4 > dataend))
11627 : 0 : goto invalid_data;
11628 : :
11629 : 8 : uint32_t shortcut_off = 0;
11630 [ + + ]: 8 : if (vers >= 9)
11631 : : {
11632 : 4 : shortcut_off = read_4ubyte_unaligned (dbg, readp);
11633 : 4 : printf (_(" shortcut offset: %#" PRIx32 "\n"), shortcut_off);
11634 : :
11635 : 4 : readp += 4;
11636 [ - + ]: 4 : if (unlikely (readp + 4 > dataend))
11637 : 0 : goto invalid_data;
11638 : : }
11639 : :
11640 : 8 : uint32_t const_off = read_4ubyte_unaligned (dbg, readp);
11641 : 8 : printf (_(" constant offset: %#" PRIx32 "\n"), const_off);
11642 : :
11643 [ - + ]: 8 : if (unlikely ((size_t) (dataend - (const unsigned char *) data->d_buf)
11644 : : < const_off))
11645 : 0 : goto invalid_data;
11646 : :
11647 : 8 : readp = data->d_buf + cu_off;
11648 : :
11649 : 8 : const unsigned char *nextp = data->d_buf + tu_off;
11650 [ - + ]: 8 : if (tu_off >= data->d_size)
11651 : 0 : goto invalid_data;
11652 : :
11653 : 8 : size_t cu_nr = (nextp - readp) / 16;
11654 : :
11655 : 8 : printf (_("\n CU list at offset %#" PRIx32
11656 : : " contains %zu entries:\n"),
11657 : : cu_off, cu_nr);
11658 : :
11659 : 8 : size_t n = 0;
11660 [ + - + + ]: 22 : while (dataend - readp >= 16 && n < cu_nr)
11661 : : {
11662 : 14 : uint64_t off = read_8ubyte_unaligned (dbg, readp);
11663 : 14 : readp += 8;
11664 : :
11665 : 14 : uint64_t len = read_8ubyte_unaligned (dbg, readp);
11666 : 14 : readp += 8;
11667 : :
11668 : 14 : printf (" [%4zu] start: %0#8" PRIx64
11669 : : ", length: %5" PRIu64 "\n", n, off, len);
11670 : 14 : n++;
11671 : : }
11672 : :
11673 : 8 : readp = data->d_buf + tu_off;
11674 : 8 : nextp = data->d_buf + addr_off;
11675 [ - + ]: 8 : if (addr_off >= data->d_size)
11676 : 0 : goto invalid_data;
11677 : :
11678 : 8 : size_t tu_nr = (nextp - readp) / 24;
11679 : :
11680 : 8 : printf (_("\n TU list at offset %#" PRIx32
11681 : : " contains %zu entries:\n"),
11682 : : tu_off, tu_nr);
11683 : :
11684 : 8 : n = 0;
11685 [ + - + + ]: 14 : while (dataend - readp >= 24 && n < tu_nr)
11686 : : {
11687 : 6 : uint64_t off = read_8ubyte_unaligned (dbg, readp);
11688 : 6 : readp += 8;
11689 : :
11690 : 6 : uint64_t type = read_8ubyte_unaligned (dbg, readp);
11691 : 6 : readp += 8;
11692 : :
11693 : 6 : uint64_t sig = read_8ubyte_unaligned (dbg, readp);
11694 : 6 : readp += 8;
11695 : :
11696 : 6 : printf (" [%4zu] CU offset: %5" PRId64
11697 : : ", type offset: %5" PRId64
11698 : : ", signature: %0#8" PRIx64 "\n", n, off, type, sig);
11699 : 6 : n++;
11700 : : }
11701 : :
11702 : 8 : readp = data->d_buf + addr_off;
11703 : 8 : nextp = data->d_buf + sym_off;
11704 [ - + ]: 8 : if (sym_off >= data->d_size)
11705 : 0 : goto invalid_data;
11706 : :
11707 : 8 : size_t addr_nr = (nextp - readp) / 20;
11708 : :
11709 : 8 : printf (_("\n Address list at offset %#" PRIx32
11710 : : " contains %zu entries:\n"),
11711 : : addr_off, addr_nr);
11712 : :
11713 : 8 : n = 0;
11714 [ + - + + ]: 22 : while (dataend - readp >= 20 && n < addr_nr)
11715 : : {
11716 : 14 : uint64_t low = read_8ubyte_unaligned (dbg, readp);
11717 : 14 : readp += 8;
11718 : :
11719 : 14 : uint64_t high = read_8ubyte_unaligned (dbg, readp);
11720 : 14 : readp += 8;
11721 : :
11722 : 14 : uint32_t idx = read_4ubyte_unaligned (dbg, readp);
11723 : 14 : readp += 4;
11724 : :
11725 : 14 : printf (" [%4zu] ", n);
11726 : 14 : print_dwarf_addr (dwflmod, 8, low, low);
11727 : 14 : printf ("..");
11728 : 14 : print_dwarf_addr (dwflmod, 8, high - 1, high);
11729 : 14 : printf (", CU index: %5" PRId32 "\n", idx);
11730 : 14 : n++;
11731 : : }
11732 : :
11733 : 8 : const unsigned char *const_start = data->d_buf + const_off;
11734 [ - + ]: 8 : if (const_off > data->d_size)
11735 : 0 : goto invalid_data;
11736 : :
11737 : 8 : const unsigned char *shortcut_start = NULL;
11738 [ + + ]: 8 : if (vers >= 9)
11739 : : {
11740 [ - + ]: 4 : if (shortcut_off >= data->d_size)
11741 : 0 : goto invalid_data;
11742 : :
11743 : 4 : shortcut_start = data->d_buf + shortcut_off;
11744 : 4 : nextp = shortcut_start;
11745 : : }
11746 : : else
11747 : : nextp = const_start;
11748 : :
11749 : 8 : readp = data->d_buf + sym_off;
11750 : 8 : size_t sym_nr = (nextp - readp) / 8;
11751 : :
11752 : 8 : printf (_("\n Symbol table at offset %#" PRIx32
11753 : : " contains %zu slots:\n"),
11754 : : sym_off, sym_nr);
11755 : :
11756 : 8 : n = 0;
11757 [ + - + + ]: 8200 : while (dataend - readp >= 8 && n < sym_nr)
11758 : : {
11759 : 8192 : uint32_t name = read_4ubyte_unaligned (dbg, readp);
11760 : 8192 : readp += 4;
11761 : :
11762 : 8192 : uint32_t vector = read_4ubyte_unaligned (dbg, readp);
11763 : 8192 : readp += 4;
11764 : :
11765 [ + + ]: 8192 : if (name != 0 || vector != 0)
11766 : : {
11767 : 60 : const unsigned char *sym = const_start + name;
11768 [ + - - + ]: 60 : if (unlikely ((size_t) (dataend - const_start) < name
11769 : : || memchr (sym, '\0', dataend - sym) == NULL))
11770 : 0 : goto invalid_data;
11771 : :
11772 : 60 : printf (" [%4zu] symbol: %s, CUs: ", n, sym);
11773 : :
11774 : 60 : const unsigned char *readcus = const_start + vector;
11775 [ - + ]: 60 : if (unlikely ((size_t) (dataend - const_start) < vector))
11776 : 0 : goto invalid_data;
11777 : 60 : uint32_t cus = read_4ubyte_unaligned (dbg, readcus);
11778 : 60 : while (cus--)
11779 : : {
11780 : 68 : uint32_t cu_kind, cu, kind;
11781 : 68 : bool is_static;
11782 : 68 : readcus += 4;
11783 [ - + ]: 68 : if (unlikely (readcus + 4 > dataend))
11784 : 0 : goto invalid_data;
11785 : 68 : cu_kind = read_4ubyte_unaligned (dbg, readcus);
11786 : 68 : cu = cu_kind & ((1 << 24) - 1);
11787 : 68 : kind = (cu_kind >> 28) & 7;
11788 : 68 : is_static = cu_kind & (1U << 31);
11789 [ + + ]: 68 : if (cu > cu_nr - 1)
11790 : 10 : printf ("%" PRId32 "T", cu - (uint32_t) cu_nr);
11791 : : else
11792 : 58 : printf ("%" PRId32, cu);
11793 [ + + ]: 68 : if (kind != 0)
11794 : : {
11795 : 52 : printf (" (");
11796 [ + + + - : 52 : switch (kind)
- ]
11797 : : {
11798 : : case 1:
11799 : 20 : printf ("type");
11800 : : break;
11801 : : case 2:
11802 : 16 : printf ("var");
11803 : : break;
11804 : : case 3:
11805 : 16 : printf ("func");
11806 : : break;
11807 : : case 4:
11808 : 0 : printf ("other");
11809 : : break;
11810 : : default:
11811 : 0 : printf ("unknown-0x%" PRIx32, kind);
11812 : : break;
11813 : : }
11814 [ + + ]: 52 : printf (":%c)", (is_static ? 'S' : 'G'));
11815 : : }
11816 [ + + ]: 68 : if (cus > 0)
11817 [ + + ]: 136 : printf (", ");
11818 : : }
11819 : 60 : printf ("\n");
11820 : : }
11821 : 8192 : n++;
11822 : : }
11823 : :
11824 [ + + ]: 8 : if (vers < 9)
11825 : : return;
11826 : :
11827 [ - + ]: 4 : if (unlikely (shortcut_start == NULL))
11828 : 0 : goto invalid_data;
11829 : :
11830 : 4 : readp = shortcut_start;
11831 : 4 : nextp = const_start;
11832 : 4 : size_t shortcut_nr = (nextp - readp) / 4;
11833 : :
11834 [ - + ]: 4 : if (unlikely (shortcut_nr != 2))
11835 : 0 : goto invalid_data;
11836 : :
11837 : 4 : printf (_("\nShortcut table at offset %#" PRIx32 " contains %zu slots:\n"),
11838 : : shortcut_off, shortcut_nr);
11839 : :
11840 : 4 : uint32_t lang = read_4ubyte_unaligned (dbg, readp);
11841 : 4 : readp += 4;
11842 : :
11843 : : /* Include the hex number of LANG in the output if the language
11844 : : is unknown. */
11845 : 4 : const char *lang_str = dwarf_lang_string (lang);
11846 : 4 : lang_str = string_or_unknown (lang_str, lang, DW_LANG_lo_user,
11847 : : DW_LANG_hi_user, true);
11848 : :
11849 : 4 : printf (_("Language of main: %s\n"), lang_str);
11850 : 4 : printf (_("Name of main: "));
11851 : :
11852 [ + + ]: 4 : if (lang != 0)
11853 : : {
11854 : 2 : uint32_t name = read_4ubyte_unaligned (dbg, readp);
11855 : 2 : readp += 4;
11856 : 2 : const unsigned char *sym = const_start + name;
11857 : :
11858 [ + - - + ]: 2 : if (unlikely ((size_t) (dataend - const_start) < name
11859 : : || memchr (sym, '\0', dataend - sym) == NULL))
11860 : 0 : goto invalid_data;
11861 : :
11862 : 2 : printf ("%s\n", sym);
11863 : : }
11864 : : else
11865 : 4 : printf ("<unknown>\n");
11866 : : }
11867 : :
11868 : : /* Returns true and sets split DWARF CU id if there is a split compile
11869 : : unit in the given Dwarf, and no non-split units are found (before it). */
11870 : : static bool
11871 : 396 : is_split_dwarf (Dwarf *dbg, uint64_t *id, Dwarf_CU **split_cu)
11872 : : {
11873 : 396 : Dwarf_CU *cu = NULL;
11874 [ + + ]: 396 : while (dwarf_get_units (dbg, cu, &cu, NULL, NULL, NULL, NULL) == 0)
11875 : : {
11876 : 394 : uint8_t unit_type;
11877 [ + - ]: 394 : if (dwarf_cu_info (cu, NULL, &unit_type, NULL, NULL,
11878 : : id, NULL, NULL) != 0)
11879 : 394 : return false;
11880 : :
11881 [ + + ]: 394 : if (unit_type != DW_UT_split_compile && unit_type != DW_UT_split_type)
11882 : : return false;
11883 : :
11884 : : /* We really only care about the split compile unit, the types
11885 : : should be fine and self sufficient. Also they don't have an
11886 : : id that we can match with a skeleton unit. */
11887 [ + - ]: 18 : if (unit_type == DW_UT_split_compile)
11888 : : {
11889 : 18 : *split_cu = cu;
11890 : 18 : return true;
11891 : : }
11892 : : }
11893 : :
11894 : : return false;
11895 : : }
11896 : :
11897 : : /* Check that there is one and only one Dwfl_Module, return in arg. */
11898 : : static int
11899 : 18 : getone_dwflmod (Dwfl_Module *dwflmod,
11900 : : void **userdata __attribute__ ((unused)),
11901 : : const char *name __attribute__ ((unused)),
11902 : : Dwarf_Addr base __attribute__ ((unused)),
11903 : : void *arg)
11904 : : {
11905 : 18 : Dwfl_Module **m = (Dwfl_Module **) arg;
11906 [ + - ]: 18 : if (*m != NULL)
11907 : : return DWARF_CB_ABORT;
11908 : 18 : *m = dwflmod;
11909 : 18 : return DWARF_CB_OK;
11910 : : }
11911 : :
11912 : : static void
11913 : 502 : print_debug (Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr)
11914 : : {
11915 : : /* Used for skeleton file, if necessary for split DWARF. */
11916 : 502 : Dwfl *skel_dwfl = NULL;
11917 : 502 : Dwfl_Module *skel_mod = NULL;
11918 : 502 : char *skel_name = NULL;
11919 : 502 : Dwarf *split_dbg = NULL;
11920 : 502 : Dwarf_CU *split_cu = NULL;
11921 : :
11922 : : /* Before we start the real work get a debug context descriptor. */
11923 : 502 : Dwarf_Addr dwbias;
11924 : 502 : Dwarf *dbg = dwfl_module_getdwarf (dwflmod, &dwbias);
11925 : 502 : Dwarf dummy_dbg =
11926 : : {
11927 : 502 : .elf = ebl->elf,
11928 : 502 : .other_byte_order = MY_ELFDATA != ehdr->e_ident[EI_DATA]
11929 : : };
11930 [ + + ]: 502 : if (dbg == NULL)
11931 : : {
11932 [ - + ]: 106 : if ((print_debug_sections & ~(section_exception|section_frame)) != 0)
11933 : 0 : error (0, 0, _("cannot get debug context descriptor: %s"),
11934 : : dwfl_errmsg (-1));
11935 : : dbg = &dummy_dbg;
11936 : : }
11937 : : else
11938 : : {
11939 : : /* If we are asked about a split dwarf (.dwo) file, use the user
11940 : : provided, or find the corresponding skeleton file. If we got
11941 : : a skeleton file, replace the given dwflmod and dbg, with one
11942 : : derived from the skeleton file to provide enough context. */
11943 : 396 : uint64_t split_id;
11944 [ + + ]: 396 : if (is_split_dwarf (dbg, &split_id, &split_cu))
11945 : : {
11946 [ + - ]: 18 : if (dwarf_skeleton != NULL)
11947 : 18 : skel_name = strdup (dwarf_skeleton);
11948 : : else
11949 : : {
11950 : : /* Replace file.dwo with file.o and see if that matches. */
11951 : 0 : const char *fname;
11952 : 0 : dwfl_module_info (dwflmod, NULL, NULL, NULL, NULL, NULL,
11953 : : &fname, NULL);
11954 [ # # ]: 0 : if (fname != NULL)
11955 : : {
11956 : 0 : size_t flen = strlen (fname);
11957 [ # # # # ]: 0 : if (flen > 4 && strcmp (".dwo", fname + flen - 4) == 0)
11958 : : {
11959 : 0 : skel_name = strdup (fname);
11960 [ # # ]: 0 : if (skel_name != NULL)
11961 : : {
11962 : 0 : skel_name[flen - 3] = 'o';
11963 : 0 : skel_name[flen - 2] = '\0';
11964 : : }
11965 : : }
11966 : : }
11967 : : }
11968 : :
11969 [ - + ]: 18 : if (skel_name != NULL)
11970 : : {
11971 : 18 : int skel_fd = open (skel_name, O_RDONLY);
11972 [ - + ]: 18 : if (skel_fd == -1)
11973 : 0 : fprintf (stderr, "Warning: Couldn't open DWARF skeleton file"
11974 : : " '%s'\n", skel_name);
11975 : : else
11976 : : {
11977 : 18 : skel_dwfl = create_dwfl (skel_fd, skel_name);
11978 : :
11979 : : /* skel_fd was dup'ed by create_dwfl. We can close the
11980 : : original now. */
11981 : 18 : close (skel_fd);
11982 : : }
11983 : :
11984 [ + - ]: 18 : if (skel_dwfl != NULL)
11985 : : {
11986 [ - + ]: 18 : if (dwfl_getmodules (skel_dwfl, &getone_dwflmod,
11987 : : &skel_mod, 0) != 0)
11988 : : {
11989 : 0 : fprintf (stderr, "Warning: Bad DWARF skeleton,"
11990 : : " multiple modules '%s'\n", skel_name);
11991 : 0 : dwfl_end (skel_dwfl);
11992 : 0 : skel_mod = NULL;
11993 : : }
11994 : : }
11995 [ # # ]: 0 : else if (skel_fd != -1)
11996 : 0 : fprintf (stderr, "Warning: Couldn't create skeleton dwfl for"
11997 : : " '%s': %s\n", skel_name, dwfl_errmsg (-1));
11998 : :
11999 [ + - ]: 18 : if (skel_mod != NULL)
12000 : : {
12001 : 18 : Dwarf *skel_dbg = dwfl_module_getdwarf (skel_mod, &dwbias);
12002 [ + - ]: 18 : if (skel_dbg != NULL)
12003 : : {
12004 : : /* First check the skeleton CU DIE, only fetch
12005 : : the split DIE if we know the id matches to
12006 : : not unnecessary search for any split DIEs we
12007 : : don't need. */
12008 : 18 : Dwarf_CU *cu = NULL;
12009 : 18 : while (dwarf_get_units (skel_dbg, cu, &cu,
12010 [ + - ]: 26 : NULL, NULL, NULL, NULL) == 0)
12011 : : {
12012 : 26 : uint8_t unit_type;
12013 : 26 : uint64_t skel_id;
12014 [ + - ]: 26 : if (dwarf_cu_info (cu, NULL, &unit_type, NULL, NULL,
12015 : : &skel_id, NULL, NULL) == 0
12016 [ + - ]: 26 : && unit_type == DW_UT_skeleton
12017 [ + + ]: 26 : && split_id == skel_id)
12018 : : {
12019 : 18 : Dwarf_Die subdie;
12020 [ + - ]: 18 : if (dwarf_cu_info (cu, NULL, NULL, NULL,
12021 : : &subdie,
12022 : : NULL, NULL, NULL) == 0
12023 [ + - ]: 18 : && dwarf_tag (&subdie) != DW_TAG_invalid)
12024 : : {
12025 : 18 : split_dbg = dwarf_cu_getdwarf (subdie.cu);
12026 [ - + ]: 18 : if (split_dbg == NULL)
12027 : 0 : fprintf (stderr,
12028 : : "Warning: Couldn't get split_dbg:"
12029 : : " %s\n", dwarf_errmsg (-1));
12030 : 0 : break;
12031 : : }
12032 : : else
12033 : : {
12034 : : /* Everything matches up, but not
12035 : : according to libdw. Which means
12036 : : the user knew better. So...
12037 : : Terrible hack... We can never
12038 : : destroy the underlying dwfl
12039 : : because it would free the wrong
12040 : : Dwarfs... So we leak memory...*/
12041 [ # # ]: 0 : if (cu->split == NULL
12042 [ # # ]: 0 : && dwarf_skeleton != NULL)
12043 : : {
12044 : 0 : do_not_close_dwfl = true;
12045 : 0 : __libdw_link_skel_split (cu, split_cu);
12046 : 0 : split_dbg = dwarf_cu_getdwarf (split_cu);
12047 : 0 : break;
12048 : : }
12049 : : else
12050 : 0 : fprintf (stderr, "Warning: Couldn't get"
12051 : : " skeleton subdie: %s\n",
12052 : : dwarf_errmsg (-1));
12053 : : }
12054 : : }
12055 : : }
12056 [ - - ]: 18 : if (split_dbg == NULL)
12057 : 0 : fprintf (stderr, "Warning: '%s' didn't contain a skeleton for split id %" PRIx64 "\n", skel_name, split_id);
12058 : : }
12059 : : else
12060 : 0 : fprintf (stderr, "Warning: Couldn't get skeleton DWARF:"
12061 : : " %s\n", dwfl_errmsg (-1));
12062 : : }
12063 : : }
12064 : :
12065 : 0 : if (split_dbg != NULL)
12066 : : {
12067 : 18 : dbg = split_dbg;
12068 : 18 : dwflmod = skel_mod;
12069 : : }
12070 [ # # ]: 0 : else if (skel_name == NULL)
12071 : 0 : fprintf (stderr,
12072 : : "Warning: split DWARF file, but no skeleton found.\n");
12073 : : }
12074 [ + - ]: 378 : else if (dwarf_skeleton != NULL)
12075 : 396 : fprintf (stderr, "Warning: DWARF skeleton given,"
12076 : : " but not a split DWARF file\n");
12077 : : }
12078 : :
12079 : : /* Get the section header string table index. */
12080 : 502 : size_t shstrndx;
12081 [ - + ]: 502 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
12082 : 0 : error_exit (0, _("cannot get section header string table index"));
12083 : :
12084 : : /* If the .debug_info section is listed as implicitly required then
12085 : : we must make sure to handle it before handling any other debug
12086 : : section. Various other sections depend on the CU DIEs being
12087 : : scanned (silently) first. */
12088 : 502 : bool implicit_info = (implicit_debug_sections & section_info) != 0;
12089 : 502 : bool explicit_info = (print_debug_sections & section_info) != 0;
12090 [ + + ]: 502 : if (implicit_info)
12091 : : {
12092 : : Elf_Scn *scn = NULL;
12093 [ + - ]: 3132 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
12094 : : {
12095 : 3132 : GElf_Shdr shdr_mem;
12096 : 3132 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
12097 : :
12098 [ + - + + ]: 3132 : if (shdr != NULL && shdr->sh_type == SHT_PROGBITS)
12099 : : {
12100 : 2448 : const char *name = elf_strptr (ebl->elf, shstrndx,
12101 : 1224 : shdr->sh_name);
12102 [ - + ]: 1224 : if (name == NULL)
12103 : 0 : continue;
12104 : :
12105 [ + + ]: 1224 : if (strcmp (name, ".debug_info") == 0
12106 [ + + ]: 1092 : || strcmp (name, ".debug_info.dwo") == 0
12107 [ + + ]: 1074 : || strcmp (name, ".zdebug_info") == 0
12108 [ + - ]: 1062 : || strcmp (name, ".zdebug_info.dwo") == 0
12109 [ + + ]: 1062 : || strcmp (name, ".gnu.debuglto_.debug_info") == 0)
12110 : : {
12111 : 164 : print_debug_info_section (dwflmod, ebl, ehdr,
12112 : : scn, shdr, dbg);
12113 : 164 : break;
12114 : : }
12115 : : }
12116 : : }
12117 : 164 : print_debug_sections &= ~section_info;
12118 : 164 : implicit_debug_sections &= ~section_info;
12119 : : }
12120 : :
12121 : : /* Look through all the sections for the debugging sections to print. */
12122 : : Elf_Scn *scn = NULL;
12123 [ + + ]: 16590 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
12124 : : {
12125 : 16088 : GElf_Shdr shdr_mem;
12126 : 16088 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
12127 : :
12128 [ + - + + ]: 16088 : if (shdr != NULL && shdr->sh_type == SHT_PROGBITS)
12129 : : {
12130 : 6758 : static const struct
12131 : : {
12132 : : const char *name;
12133 : : enum section_e bitmask;
12134 : : void (*fp) (Dwfl_Module *, Ebl *,
12135 : : GElf_Ehdr *, Elf_Scn *, GElf_Shdr *, Dwarf *);
12136 : : } debug_sections[] =
12137 : : {
12138 : : #define NEW_SECTION(name) \
12139 : : { ".debug_" #name, section_##name, print_debug_##name##_section }
12140 : : NEW_SECTION (abbrev),
12141 : : NEW_SECTION (addr),
12142 : : NEW_SECTION (aranges),
12143 : : NEW_SECTION (frame),
12144 : : NEW_SECTION (info),
12145 : : NEW_SECTION (types),
12146 : : NEW_SECTION (line),
12147 : : NEW_SECTION (loc),
12148 : : /* loclists is loc for DWARF5. */
12149 : : { ".debug_loclists", section_loc,
12150 : : print_debug_loclists_section },
12151 : : NEW_SECTION (pubnames),
12152 : : NEW_SECTION (str),
12153 : : /* A DWARF5 specialised debug string section. */
12154 : : { ".debug_line_str", section_str,
12155 : : print_debug_str_section },
12156 : : /* DWARF5 string offsets table. */
12157 : : { ".debug_str_offsets", section_str,
12158 : : print_debug_str_offsets_section },
12159 : : NEW_SECTION (macinfo),
12160 : : NEW_SECTION (macro),
12161 : : NEW_SECTION (ranges),
12162 : : /* rnglists is ranges for DWARF5. */
12163 : : { ".debug_rnglists", section_ranges,
12164 : : print_debug_rnglists_section },
12165 : : { ".eh_frame", section_frame | section_exception,
12166 : : print_debug_frame_section },
12167 : : { ".eh_frame_hdr", section_frame | section_exception,
12168 : : print_debug_frame_hdr_section },
12169 : : { ".gcc_except_table", section_frame | section_exception,
12170 : : print_debug_exception_table },
12171 : : { ".gdb_index", section_gdb_index, print_gdb_index_section }
12172 : : };
12173 : 6758 : const int ndebug_sections = (sizeof (debug_sections)
12174 : : / sizeof (debug_sections[0]));
12175 : 13516 : const char *name = elf_strptr (ebl->elf, shstrndx,
12176 : 6758 : shdr->sh_name);
12177 [ - + ]: 6758 : if (name == NULL)
12178 : 0 : continue;
12179 : :
12180 : : int n;
12181 [ + + ]: 106506 : for (n = 0; n < ndebug_sections; ++n)
12182 : : {
12183 : 103042 : size_t dbglen = strlen (debug_sections[n].name);
12184 : 103042 : size_t scnlen = strlen (name);
12185 [ + + ]: 103042 : if ((strncmp (name, debug_sections[n].name, dbglen) == 0
12186 [ + + ]: 3558 : && (dbglen == scnlen
12187 [ + + ]: 546 : || (scnlen == dbglen + 4
12188 [ + + ]: 466 : && strstr (name, ".dwo") == name + dbglen)))
12189 [ + + + + ]: 99922 : || (name[0] == '.' && name[1] == 'z'
12190 [ + - ]: 1132 : && debug_sections[n].name[1] == 'd'
12191 [ + + ]: 1132 : && strncmp (&name[2], &debug_sections[n].name[1],
12192 : : dbglen - 1) == 0
12193 [ - + ]: 164 : && (scnlen == dbglen + 1
12194 [ # # ]: 0 : || (scnlen == dbglen + 5
12195 [ # # ]: 0 : && strstr (name, ".dwo") == name + dbglen + 1)))
12196 [ + + ]: 99758 : || (scnlen > 14 /* .gnu.debuglto_ prefix. */
12197 [ + + ]: 10170 : && startswith (name, ".gnu.debuglto_")
12198 [ + + ]: 72 : && strcmp (&name[14], debug_sections[n].name) == 0)
12199 : : )
12200 : : {
12201 : 3294 : if ((print_debug_sections | implicit_debug_sections)
12202 [ + + ]: 3294 : & debug_sections[n].bitmask)
12203 : 1094 : debug_sections[n].fp (dwflmod, ebl, ehdr, scn, shdr, dbg);
12204 : : break;
12205 : : }
12206 : : }
12207 : : }
12208 : : }
12209 : :
12210 : 502 : dwfl_end (skel_dwfl);
12211 : 502 : free (skel_name);
12212 : :
12213 : : /* Turn implicit and/or explicit back on in case we go over another file. */
12214 [ + + ]: 502 : if (implicit_info)
12215 : 164 : implicit_debug_sections |= section_info;
12216 [ + + ]: 502 : if (explicit_info)
12217 : 134 : print_debug_sections |= section_info;
12218 : :
12219 : 502 : reset_listptr (&known_locsptr);
12220 : 502 : reset_listptr (&known_loclistsptr);
12221 : 502 : reset_listptr (&known_rangelistptr);
12222 : 502 : reset_listptr (&known_rnglistptr);
12223 : 502 : reset_listptr (&known_addrbases);
12224 : 502 : reset_listptr (&known_stroffbases);
12225 : 502 : }
12226 : :
12227 : :
12228 : : #define ITEM_INDENT 4
12229 : : #define WRAP_COLUMN 75
12230 : :
12231 : : /* Print "NAME: FORMAT", wrapping when output text would make the line
12232 : : exceed WRAP_COLUMN. Unpadded numbers look better for the core items
12233 : : but this function is also used for registers which should be printed
12234 : : aligned. Fortunately registers output uses fixed fields width (such
12235 : : as %11d) for the alignment.
12236 : :
12237 : : Line breaks should not depend on the particular values although that
12238 : : may happen in some cases of the core items. */
12239 : :
12240 : : static unsigned int
12241 : : __attribute__ ((format (printf, 6, 7)))
12242 : 1642 : print_core_item (unsigned int colno, char sep, unsigned int wrap,
12243 : : size_t name_width, const char *name, const char *format, ...)
12244 : : {
12245 : 1642 : size_t len = strlen (name);
12246 : 1642 : if (name_width < len)
12247 : : name_width = len;
12248 : :
12249 : 1642 : char *out;
12250 : 1642 : va_list ap;
12251 : 1642 : va_start (ap, format);
12252 : 1642 : int out_len = vasprintf (&out, format, ap);
12253 : 1642 : va_end (ap);
12254 [ - + ]: 1642 : if (out_len == -1)
12255 : 0 : error_exit (0, _("memory exhausted"));
12256 : :
12257 : 1642 : size_t n = name_width + sizeof ": " - 1 + out_len;
12258 : :
12259 [ + + ]: 1642 : if (colno == 0)
12260 : : {
12261 : 102 : printf ("%*s", ITEM_INDENT, "");
12262 : 102 : colno = ITEM_INDENT + n;
12263 : : }
12264 [ + + ]: 1540 : else if (colno + 2 + n < wrap)
12265 : : {
12266 : 936 : printf ("%c ", sep);
12267 : 936 : colno += 2 + n;
12268 : : }
12269 : : else
12270 : : {
12271 : 604 : printf ("\n%*s", ITEM_INDENT, "");
12272 : 604 : colno = ITEM_INDENT + n;
12273 : : }
12274 : :
12275 : 1642 : printf ("%s: %*s%s", name, (int) (name_width - len), "", out);
12276 : :
12277 : 1642 : free (out);
12278 : :
12279 : 1642 : return colno;
12280 : : }
12281 : :
12282 : : static const void *
12283 : 1862 : convert (Elf *core, Elf_Type type, uint_fast16_t count,
12284 : : void *value, const void *data, size_t size)
12285 : : {
12286 : 3724 : Elf_Data valuedata =
12287 : : {
12288 : : .d_type = type,
12289 : : .d_buf = value,
12290 [ + + ]: 1862 : .d_size = size ?: gelf_fsize (core, type, count, EV_CURRENT),
12291 : : .d_version = EV_CURRENT,
12292 : : };
12293 : 1862 : Elf_Data indata =
12294 : : {
12295 : : .d_type = type,
12296 : : .d_buf = (void *) data,
12297 : : .d_size = valuedata.d_size,
12298 : : .d_version = EV_CURRENT,
12299 : : };
12300 : :
12301 : 1862 : Elf_Data *d = (gelf_getclass (core) == ELFCLASS32
12302 [ + + ]: 1862 : ? elf32_xlatetom : elf64_xlatetom)
12303 : 1862 : (&valuedata, &indata, elf_getident (core, NULL)[EI_DATA]);
12304 [ - + ]: 1862 : if (d == NULL)
12305 : 0 : error_exit (0, _("cannot convert core note data: %s"),
12306 : : elf_errmsg (-1));
12307 : :
12308 : 1862 : return data + indata.d_size;
12309 : : }
12310 : :
12311 : : typedef uint8_t GElf_Byte;
12312 : :
12313 : : static unsigned int
12314 : 800 : handle_core_item (Elf *core, const Ebl_Core_Item *item, const void *desc,
12315 : : unsigned int colno, size_t *repeated_size)
12316 : : {
12317 [ + + ]: 800 : uint_fast16_t count = item->count ?: 1;
12318 : : /* Ebl_Core_Item count is always a small number.
12319 : : Make sure the backend didn't put in some large bogus value. */
12320 [ - + ]: 124 : assert (count < 128);
12321 : :
12322 : : #define TYPES \
12323 : : DO_TYPE (BYTE, Byte, "0x%.2" PRIx8, "%" PRId8); \
12324 : : DO_TYPE (HALF, Half, "0x%.4" PRIx16, "%" PRId16); \
12325 : : DO_TYPE (WORD, Word, "0x%.8" PRIx32, "%" PRId32); \
12326 : : DO_TYPE (SWORD, Sword, "%" PRId32, "%" PRId32); \
12327 : : DO_TYPE (XWORD, Xword, "0x%.16" PRIx64, "%" PRId64); \
12328 : : DO_TYPE (SXWORD, Sxword, "%" PRId64, "%" PRId64)
12329 : :
12330 : : #define DO_TYPE(NAME, Name, hex, dec) GElf_##Name Name
12331 : 800 : typedef union { TYPES; } value_t;
12332 : 800 : void *data = alloca (count * sizeof (value_t));
12333 : : #undef DO_TYPE
12334 : :
12335 : : #define DO_TYPE(NAME, Name, hex, dec) \
12336 : : GElf_##Name *value_##Name __attribute__((unused)) = data
12337 : 800 : TYPES;
12338 : : #undef DO_TYPE
12339 : :
12340 : 800 : size_t size = gelf_fsize (core, item->type, count, EV_CURRENT);
12341 : 800 : size_t convsize = size;
12342 [ + + ]: 800 : if (repeated_size != NULL)
12343 : : {
12344 [ + - - + ]: 2 : if (*repeated_size > size && (item->format == 'b' || item->format == 'B'))
12345 : : {
12346 : 0 : data = alloca (*repeated_size);
12347 : 0 : count *= *repeated_size / size;
12348 : 0 : convsize = count * size;
12349 : 0 : *repeated_size -= convsize;
12350 : : }
12351 [ + - + - ]: 2 : else if (item->count != 0 || item->format != '\n')
12352 : 0 : *repeated_size -= size;
12353 : : }
12354 : :
12355 : 800 : convert (core, item->type, count, data, desc + item->offset, convsize);
12356 : :
12357 : 800 : Elf_Type type = item->type;
12358 [ + + ]: 800 : if (type == ELF_T_ADDR)
12359 [ + - ]: 2 : type = gelf_getclass (core) == ELFCLASS32 ? ELF_T_WORD : ELF_T_XWORD;
12360 : :
12361 [ + + + + : 800 : switch (item->format)
+ + + +
- ]
12362 : : {
12363 : 386 : case 'd':
12364 [ - + ]: 386 : assert (count == 1);
12365 [ + + + + : 386 : switch (type)
- + - ]
12366 : : {
12367 : : #define DO_TYPE(NAME, Name, hex, dec) \
12368 : : case ELF_T_##NAME: \
12369 : : colno = print_core_item (colno, ',', WRAP_COLUMN, \
12370 : : 0, item->name, dec, value_##Name[0]); \
12371 : : break
12372 : 386 : TYPES;
12373 : : #undef DO_TYPE
12374 : 0 : default:
12375 : 0 : abort ();
12376 : : }
12377 : : break;
12378 : :
12379 : 222 : case 'x':
12380 [ - + ]: 222 : assert (count == 1);
12381 [ - - + - : 222 : switch (type)
+ - - ]
12382 : : {
12383 : : #define DO_TYPE(NAME, Name, hex, dec) \
12384 : : case ELF_T_##NAME: \
12385 : : colno = print_core_item (colno, ',', WRAP_COLUMN, \
12386 : : 0, item->name, hex, value_##Name[0]); \
12387 : : break
12388 : 222 : TYPES;
12389 : : #undef DO_TYPE
12390 : 0 : default:
12391 : 0 : abort ();
12392 : : }
12393 : : break;
12394 : :
12395 : 44 : case 'b':
12396 : : case 'B':
12397 [ - + ]: 44 : assert (size % sizeof (unsigned int) == 0);
12398 : 44 : unsigned int nbits = count * size * 8;
12399 : 44 : unsigned int pop = 0;
12400 [ + + ]: 112 : for (const unsigned int *i = data; (void *) i < data + count * size; ++i)
12401 : 68 : pop += __builtin_popcount (*i);
12402 : 44 : bool negate = pop > nbits / 2;
12403 : 44 : const unsigned int bias = item->format == 'b';
12404 : :
12405 : 44 : {
12406 [ - + ]: 44 : char printed[(negate ? nbits - pop : pop) * 16 + 1];
12407 : 44 : char *p = printed;
12408 : 44 : *p = '\0';
12409 : :
12410 : 44 : if (BYTE_ORDER != LITTLE_ENDIAN && size > sizeof (unsigned int))
12411 : : {
12412 : : assert (size == sizeof (unsigned int) * 2);
12413 : : for (unsigned int *i = data;
12414 : : (void *) i < data + count * size; i += 2)
12415 : : {
12416 : : unsigned int w = i[1];
12417 : : i[1] = i[0];
12418 : : i[0] = w;
12419 : : }
12420 : : }
12421 : :
12422 : 44 : unsigned int lastbit = 0;
12423 : 44 : unsigned int run = 0;
12424 : 44 : for (const unsigned int *i = data;
12425 [ + + ]: 112 : (void *) i < data + count * size; ++i)
12426 : : {
12427 : 68 : unsigned int bit = ((void *) i - data) * 8;
12428 [ - + ]: 68 : unsigned int w = negate ? ~*i : *i;
12429 [ - + ]: 68 : while (w != 0)
12430 : : {
12431 : : /* Note that a right shift equal to (or greater than)
12432 : : the number of bits of w is undefined behaviour. In
12433 : : particular when the least significant bit is bit 32
12434 : : (w = 0x8000000) then w >>= n is undefined. So
12435 : : explicitly handle that case separately. */
12436 : 0 : unsigned int n = ffs (w);
12437 [ # # ]: 0 : if (n < sizeof (w) * 8)
12438 : 0 : w >>= n;
12439 : : else
12440 : : w = 0;
12441 : 0 : bit += n;
12442 : :
12443 [ # # # # ]: 0 : if (lastbit != 0 && lastbit + 1 == bit)
12444 : 0 : ++run;
12445 : : else
12446 : : {
12447 : 0 : if (lastbit == 0)
12448 : 0 : p += sprintf (p, "%u", bit - bias);
12449 [ # # ]: 0 : else if (run == 0)
12450 : 0 : p += sprintf (p, ",%u", bit - bias);
12451 : : else
12452 : 0 : p += sprintf (p, "-%u,%u", lastbit - bias, bit - bias);
12453 : : run = 0;
12454 : : }
12455 : :
12456 : : lastbit = bit;
12457 : : }
12458 : : }
12459 [ - + - - ]: 44 : if (lastbit > 0 && run > 0 && lastbit + 1 != nbits)
12460 : 0 : p += sprintf (p, "-%u", lastbit - bias);
12461 : :
12462 [ + - ]: 88 : colno = print_core_item (colno, ',', WRAP_COLUMN, 0, item->name,
12463 : : negate ? "~<%s>" : "<%s>", printed);
12464 : : }
12465 : 44 : break;
12466 : :
12467 : 88 : case 'T':
12468 : : case (char) ('T'|0x80):
12469 [ - + ]: 88 : assert (count == 2);
12470 : 88 : Dwarf_Word sec;
12471 : 88 : Dwarf_Word usec;
12472 [ - - + - : 88 : switch (type)
+ - - ]
12473 : : {
12474 : : #define DO_TYPE(NAME, Name, hex, dec) \
12475 : : case ELF_T_##NAME: \
12476 : : sec = value_##Name[0]; \
12477 : : usec = value_##Name[1]; \
12478 : : break
12479 : 88 : TYPES;
12480 : : #undef DO_TYPE
12481 : 0 : default:
12482 : 0 : abort ();
12483 : : }
12484 [ - + ]: 88 : if (unlikely (item->format == (char) ('T'|0x80)))
12485 : : {
12486 : : /* This is a hack for an ill-considered 64-bit ABI where
12487 : : tv_usec is actually a 32-bit field with 32 bits of padding
12488 : : rounding out struct timeval. We've already converted it as
12489 : : a 64-bit field. For little-endian, this just means the
12490 : : high half is the padding; it's presumably zero, but should
12491 : : be ignored anyway. For big-endian, it means the 32-bit
12492 : : field went into the high half of USEC. */
12493 : 0 : GElf_Ehdr ehdr_mem;
12494 : 0 : GElf_Ehdr *ehdr = gelf_getehdr (core, &ehdr_mem);
12495 [ # # ]: 0 : if (likely (ehdr->e_ident[EI_DATA] == ELFDATA2MSB))
12496 : 0 : usec >>= 32;
12497 : : else
12498 : 0 : usec &= UINT32_MAX;
12499 : : }
12500 : 88 : colno = print_core_item (colno, ',', WRAP_COLUMN, 0, item->name,
12501 : : "%" PRIu64 ".%.6" PRIu64, sec, usec);
12502 : 88 : break;
12503 : :
12504 : 18 : case 'c':
12505 [ - + ]: 18 : assert (count == 1);
12506 : 18 : colno = print_core_item (colno, ',', WRAP_COLUMN, 0, item->name,
12507 : 18 : "%c", value_Byte[0]);
12508 : 18 : break;
12509 : :
12510 : 36 : case 's':
12511 : 36 : colno = print_core_item (colno, ',', WRAP_COLUMN, 0, item->name,
12512 : : "%.*s", (int) count, value_Byte);
12513 : 36 : break;
12514 : :
12515 : 2 : case '\n':
12516 : : /* This is a list of strings separated by '\n'. */
12517 [ - + ]: 2 : assert (item->count == 0);
12518 [ - + ]: 2 : assert (repeated_size != NULL);
12519 [ - + ]: 2 : assert (item->name == NULL);
12520 [ - + ]: 2 : if (unlikely (item->offset >= *repeated_size))
12521 : : break;
12522 : :
12523 : 2 : const char *s = desc + item->offset;
12524 : 2 : size = *repeated_size - item->offset;
12525 : 2 : *repeated_size = 0;
12526 [ + - ]: 96 : while (size > 0)
12527 : : {
12528 : 96 : const char *eol = memchr (s, '\n', size);
12529 : 96 : int len = size;
12530 [ + + ]: 96 : if (eol != NULL)
12531 : 94 : len = eol - s;
12532 : 96 : printf ("%*s%.*s\n", ITEM_INDENT, "", len, s);
12533 [ + + ]: 96 : if (eol == NULL)
12534 : : break;
12535 : 94 : size -= eol + 1 - s;
12536 : 94 : s = eol + 1;
12537 : : }
12538 : :
12539 : : colno = WRAP_COLUMN;
12540 : : break;
12541 : :
12542 : : case 'h':
12543 : : break;
12544 : :
12545 : 0 : default:
12546 : 800 : error (0, 0, "XXX not handling format '%c' for %s",
12547 : 0 : item->format, item->name);
12548 : : break;
12549 : : }
12550 : :
12551 : : #undef TYPES
12552 : :
12553 : 800 : return colno;
12554 : : }
12555 : :
12556 : :
12557 : : /* Sort items by group, and by layout offset within each group. */
12558 : : static int
12559 : 1790 : compare_core_items (const void *a, const void *b)
12560 : : {
12561 : 1790 : const Ebl_Core_Item *const *p1 = a;
12562 : 1790 : const Ebl_Core_Item *const *p2 = b;
12563 : 1790 : const Ebl_Core_Item *item1 = *p1;
12564 : 1790 : const Ebl_Core_Item *item2 = *p2;
12565 : :
12566 : 1790 : return ((item1->group == item2->group ? 0
12567 [ + + ]: 1790 : : strcmp (item1->group, item2->group))
12568 [ - + ]: 1790 : ?: (int) item1->offset - (int) item2->offset);
12569 : : }
12570 : :
12571 : : /* Sort item groups by layout offset of the first item in the group. */
12572 : : static int
12573 : 278 : compare_core_item_groups (const void *a, const void *b)
12574 : : {
12575 : 278 : const Ebl_Core_Item *const *const *p1 = a;
12576 : 278 : const Ebl_Core_Item *const *const *p2 = b;
12577 : 278 : const Ebl_Core_Item *const *group1 = *p1;
12578 : 278 : const Ebl_Core_Item *const *group2 = *p2;
12579 : 278 : const Ebl_Core_Item *item1 = *group1;
12580 : 278 : const Ebl_Core_Item *item2 = *group2;
12581 : :
12582 : 278 : return (int) item1->offset - (int) item2->offset;
12583 : : }
12584 : :
12585 : : static unsigned int
12586 : 74 : handle_core_items (Elf *core, const void *desc, size_t descsz,
12587 : : const Ebl_Core_Item *items, size_t nitems)
12588 : 74 : {
12589 [ + + ]: 74 : if (nitems == 0)
12590 : : return 0;
12591 : 68 : unsigned int colno = 0;
12592 : :
12593 : : /* FORMAT '\n' makes sense to be present only as a single item as it
12594 : : processes all the data of a note. FORMATs 'b' and 'B' have a special case
12595 : : if present as a single item but they can be also processed with other
12596 : : items below. */
12597 [ + + + + ]: 68 : if (nitems == 1 && (items[0].format == '\n' || items[0].format == 'b'
12598 [ + - ]: 16 : || items[0].format == 'B'))
12599 : : {
12600 [ - + ]: 2 : assert (items[0].offset == 0);
12601 : 2 : size_t size = descsz;
12602 : 2 : colno = handle_core_item (core, items, desc, colno, &size);
12603 : : /* If SIZE is not zero here there is some remaining data. But we do not
12604 : : know how to process it anyway. */
12605 : 2 : return colno;
12606 : : }
12607 [ + + ]: 848 : for (size_t i = 0; i < nitems; ++i)
12608 [ - + ]: 782 : assert (items[i].format != '\n');
12609 : :
12610 : : /* Sort to collect the groups together. */
12611 : 66 : const Ebl_Core_Item *sorted_items[nitems];
12612 [ + + ]: 848 : for (size_t i = 0; i < nitems; ++i)
12613 : 782 : sorted_items[i] = &items[i];
12614 : 66 : qsort (sorted_items, nitems, sizeof sorted_items[0], &compare_core_items);
12615 : :
12616 : : /* Collect the unique groups and sort them. */
12617 : 66 : const Ebl_Core_Item **groups[nitems];
12618 : 66 : groups[0] = &sorted_items[0];
12619 : 66 : size_t ngroups = 1;
12620 [ + + ]: 782 : for (size_t i = 1; i < nitems; ++i)
12621 [ + + ]: 716 : if (sorted_items[i]->group != sorted_items[i - 1]->group
12622 [ + - ]: 150 : && strcmp (sorted_items[i]->group, sorted_items[i - 1]->group))
12623 : 150 : groups[ngroups++] = &sorted_items[i];
12624 : 66 : qsort (groups, ngroups, sizeof groups[0], &compare_core_item_groups);
12625 : :
12626 : : /* Write out all the groups. */
12627 : 66 : const void *last = desc;
12628 : 70 : do
12629 : : {
12630 [ + + ]: 290 : for (size_t i = 0; i < ngroups; ++i)
12631 : : {
12632 : 220 : for (const Ebl_Core_Item **item = groups[i];
12633 : 1018 : (item < &sorted_items[nitems]
12634 [ + + + + ]: 1018 : && ((*item)->group == groups[i][0]->group
12635 [ - + ]: 150 : || !strcmp ((*item)->group, groups[i][0]->group)));
12636 : 798 : ++item)
12637 : 798 : colno = handle_core_item (core, *item, desc, colno, NULL);
12638 : :
12639 : : /* Force a line break at the end of the group. */
12640 : 220 : colno = WRAP_COLUMN;
12641 : : }
12642 : :
12643 [ + + ]: 70 : if (descsz == 0)
12644 : : break;
12645 : :
12646 : : /* This set of items consumed a certain amount of the note's data.
12647 : : If there is more data there, we have another unit of the same size.
12648 : : Loop to print that out too. */
12649 : 40 : const Ebl_Core_Item *item = &items[nitems - 1];
12650 : 80 : size_t eltsz = item->offset + gelf_fsize (core, item->type,
12651 : 40 : item->count ?: 1, EV_CURRENT);
12652 : :
12653 : 40 : int reps = -1;
12654 : 40 : do
12655 : : {
12656 : 40 : ++reps;
12657 : 40 : desc += eltsz;
12658 : 40 : descsz -= eltsz;
12659 : : }
12660 [ + + - + ]: 40 : while (descsz >= eltsz && !memcmp (desc, last, eltsz));
12661 : :
12662 [ + - ]: 40 : if (reps == 1)
12663 : : {
12664 : : /* For just one repeat, print it unabridged twice. */
12665 : 40 : desc -= eltsz;
12666 : 40 : descsz += eltsz;
12667 : : }
12668 [ + - ]: 40 : else if (reps > 1)
12669 : 0 : printf (_("\n%*s... <repeats %u more times> ..."),
12670 : : ITEM_INDENT, "", reps);
12671 : :
12672 : 40 : last = desc;
12673 : : }
12674 [ + + ]: 40 : while (descsz > 0);
12675 : :
12676 : : return colno;
12677 : : }
12678 : :
12679 : : static unsigned int
12680 : 324 : handle_core_register (Ebl *ebl, Elf *core, int maxregname,
12681 : : const Ebl_Register_Location *regloc, const void *desc,
12682 : : unsigned int colno)
12683 : : {
12684 [ - + ]: 324 : if (regloc->bits % 8 != 0)
12685 : : {
12686 : 0 : error (0, 0, "Warning: Cannot handle register with %" PRIu8 "bits\n",
12687 : : regloc->bits);
12688 : 0 : return colno;
12689 : : }
12690 : :
12691 : 324 : desc += regloc->offset;
12692 : :
12693 [ + + ]: 1172 : for (int reg = regloc->regno; reg < regloc->regno + regloc->count; ++reg)
12694 : : {
12695 : 848 : char name[REGNAMESZ];
12696 : 848 : int bits;
12697 : 848 : int type;
12698 : 848 : register_info (ebl, reg, regloc, name, &bits, &type);
12699 : :
12700 : : #define TYPES \
12701 : : BITS (8, BYTE, "%4" PRId8, "0x%.2" PRIx8); \
12702 : : BITS (16, HALF, "%6" PRId16, "0x%.4" PRIx16); \
12703 : : BITS (32, WORD, "%11" PRId32, " 0x%.8" PRIx32); \
12704 : : BITS (64, XWORD, "%20" PRId64, " 0x%.16" PRIx64)
12705 : :
12706 : : #define BITS(bits, xtype, sfmt, ufmt) \
12707 : : uint##bits##_t b##bits; int##bits##_t b##bits##s
12708 : 848 : union { TYPES; uint64_t b128[2]; } value;
12709 : : #undef BITS
12710 : :
12711 [ + + ]: 848 : switch (type)
12712 : : {
12713 : 672 : case DW_ATE_unsigned:
12714 : : case DW_ATE_signed:
12715 : : case DW_ATE_address:
12716 [ - + + + : 672 : switch (bits)
+ - ]
12717 : : {
12718 : : #define BITS(bits, xtype, sfmt, ufmt) \
12719 : : case bits: \
12720 : : desc = convert (core, ELF_T_##xtype, 1, &value, desc, 0); \
12721 : : if (type == DW_ATE_signed) \
12722 : : colno = print_core_item (colno, ' ', WRAP_COLUMN, \
12723 : : maxregname, name, \
12724 : : sfmt, value.b##bits##s); \
12725 : : else \
12726 : : colno = print_core_item (colno, ' ', WRAP_COLUMN, \
12727 : : maxregname, name, \
12728 : : ufmt, value.b##bits); \
12729 : : break
12730 : :
12731 [ - - - + : 576 : TYPES;
+ + + + ]
12732 : :
12733 : 96 : case 128:
12734 [ - + ]: 96 : assert (type == DW_ATE_unsigned);
12735 : 96 : desc = convert (core, ELF_T_XWORD, 2, &value, desc, 0);
12736 : 96 : int be = elf_getident (core, NULL)[EI_DATA] == ELFDATA2MSB;
12737 : 96 : colno = print_core_item (colno, ' ', WRAP_COLUMN,
12738 : : maxregname, name,
12739 : : "0x%.16" PRIx64 "%.16" PRIx64,
12740 : 96 : value.b128[!be], value.b128[be]);
12741 : 96 : break;
12742 : :
12743 : 0 : default:
12744 : 0 : abort ();
12745 : : #undef BITS
12746 : : }
12747 : : break;
12748 : :
12749 : 176 : default:
12750 : : /* Print each byte in hex, the whole thing in native byte order. */
12751 [ - + ]: 176 : assert (bits % 8 == 0);
12752 : 176 : const uint8_t *bytes = desc;
12753 : 176 : desc += bits / 8;
12754 : 176 : char hex[bits / 4 + 1];
12755 : 176 : hex[bits / 4] = '\0';
12756 : 176 : int incr = 1;
12757 [ + + ]: 176 : if (elf_getident (core, NULL)[EI_DATA] == ELFDATA2LSB)
12758 : : {
12759 : 96 : bytes += bits / 8 - 1;
12760 : 96 : incr = -1;
12761 : : }
12762 : 176 : size_t idx = 0;
12763 [ + + ]: 1744 : for (char *h = hex; bits > 0; bits -= 8, idx += incr)
12764 : : {
12765 : 1568 : *h++ = "0123456789abcdef"[bytes[idx] >> 4];
12766 : 1568 : *h++ = "0123456789abcdef"[bytes[idx] & 0xf];
12767 : : }
12768 : 176 : colno = print_core_item (colno, ' ', WRAP_COLUMN,
12769 : : maxregname, name, "0x%s", hex);
12770 : 176 : break;
12771 : : }
12772 : 848 : desc += regloc->pad;
12773 : :
12774 : : #undef TYPES
12775 : : }
12776 : :
12777 : : return colno;
12778 : : }
12779 : :
12780 : :
12781 : : struct register_info
12782 : : {
12783 : : const Ebl_Register_Location *regloc;
12784 : : const char *set;
12785 : : char name[REGNAMESZ];
12786 : : int regno;
12787 : : int bits;
12788 : : int type;
12789 : : };
12790 : :
12791 : : static int
12792 : 4132 : register_bitpos (const struct register_info *r)
12793 : : {
12794 : 4132 : return (r->regloc->offset * 8
12795 : 4132 : + ((r->regno - r->regloc->regno)
12796 : 4132 : * (r->regloc->bits + r->regloc->pad * 8)));
12797 : : }
12798 : :
12799 : : static int
12800 : 2124 : compare_sets_by_info (const struct register_info *r1,
12801 : : const struct register_info *r2)
12802 : : {
12803 : 2124 : return ((int) r2->bits - (int) r1->bits
12804 [ + + ]: 2124 : ?: register_bitpos (r1) - register_bitpos (r2));
12805 : : }
12806 : :
12807 : : /* Sort registers by set, and by size and layout offset within each set. */
12808 : : static int
12809 : 9190 : compare_registers (const void *a, const void *b)
12810 : : {
12811 : 9190 : const struct register_info *r1 = a;
12812 : 9190 : const struct register_info *r2 = b;
12813 : :
12814 : : /* Unused elements sort last. */
12815 [ + + ]: 9190 : if (r1->regloc == NULL)
12816 : 6618 : return r2->regloc == NULL ? 0 : 1;
12817 [ + + ]: 2572 : if (r2->regloc == NULL)
12818 : : return -1;
12819 : :
12820 [ + + ]: 2242 : return ((r1->set == r2->set ? 0 : strcmp (r1->set, r2->set))
12821 [ - + ]: 2242 : ?: compare_sets_by_info (r1, r2));
12822 : : }
12823 : :
12824 : : /* Sort register sets by layout offset of the first register in the set. */
12825 : : static int
12826 : 40 : compare_register_sets (const void *a, const void *b)
12827 : : {
12828 : 40 : const struct register_info *const *p1 = a;
12829 : 40 : const struct register_info *const *p2 = b;
12830 : 40 : return compare_sets_by_info (*p1, *p2);
12831 : : }
12832 : :
12833 : : static inline bool
12834 : 1728 : same_set (const struct register_info *a,
12835 : : const struct register_info *b,
12836 : : const struct register_info *regs,
12837 : : size_t maxnreg)
12838 : : {
12839 [ + - ]: 1728 : return (a < ®s[maxnreg] && a->regloc != NULL
12840 [ + - + + ]: 1728 : && b < ®s[maxnreg] && b->regloc != NULL
12841 [ + + ]: 1692 : && a->bits == b->bits
12842 [ + - + + : 3384 : && (a->set == b->set || !strcmp (a->set, b->set)));
- + ]
12843 : : }
12844 : :
12845 : : static unsigned int
12846 : 74 : handle_core_registers (Ebl *ebl, Elf *core, const void *desc,
12847 : : const Ebl_Register_Location *reglocs, size_t nregloc)
12848 : 74 : {
12849 [ + + ]: 74 : if (nregloc == 0)
12850 : : return 0;
12851 : :
12852 : 36 : ssize_t maxnreg = ebl_register_info (ebl, 0, NULL, 0, NULL, NULL, NULL, NULL);
12853 [ - + ]: 36 : if (maxnreg <= 0)
12854 : : {
12855 [ # # ]: 0 : for (size_t i = 0; i < nregloc; ++i)
12856 : 0 : if (maxnreg < reglocs[i].regno + reglocs[i].count)
12857 : : maxnreg = reglocs[i].regno + reglocs[i].count;
12858 [ # # ]: 0 : assert (maxnreg > 0);
12859 : : }
12860 : :
12861 : 36 : struct register_info regs[maxnreg];
12862 : 36 : memset (regs, 0, sizeof regs);
12863 : :
12864 : : /* Sort to collect the sets together. */
12865 : 36 : int maxreg = 0;
12866 [ + + ]: 360 : for (size_t i = 0; i < nregloc; ++i)
12867 : 324 : for (int reg = reglocs[i].regno;
12868 [ + + ]: 1172 : reg < reglocs[i].regno + reglocs[i].count;
12869 : 848 : ++reg)
12870 : : {
12871 [ - + ]: 848 : assert (reg < maxnreg);
12872 : 848 : if (reg > maxreg)
12873 : : maxreg = reg;
12874 : 848 : struct register_info *info = ®s[reg];
12875 : 848 : info->regloc = ®locs[i];
12876 : 848 : info->regno = reg;
12877 : 848 : info->set = register_info (ebl, reg, ®locs[i],
12878 : 848 : info->name, &info->bits, &info->type);
12879 : : }
12880 : 36 : qsort (regs, maxreg + 1, sizeof regs[0], &compare_registers);
12881 : :
12882 : : /* Collect the unique sets and sort them. */
12883 : 36 : struct register_info *sets[maxreg + 1];
12884 : 36 : sets[0] = ®s[0];
12885 : 36 : size_t nsets = 1;
12886 [ + + ]: 2558 : for (int i = 1; i <= maxreg; ++i)
12887 [ + + ]: 2522 : if (regs[i].regloc != NULL
12888 [ + + ]: 812 : && !same_set (®s[i], ®s[i - 1], regs, maxnreg))
12889 : 32 : sets[nsets++] = ®s[i];
12890 : 36 : qsort (sets, nsets, sizeof sets[0], &compare_register_sets);
12891 : :
12892 : : /* Write out all the sets. */
12893 : 36 : unsigned int colno = 0;
12894 [ + + ]: 104 : for (size_t i = 0; i < nsets; ++i)
12895 : : {
12896 : : /* Find the longest name of a register in this set. */
12897 : 68 : size_t maxname = 0;
12898 : 68 : const struct register_info *end;
12899 [ + + ]: 916 : for (end = sets[i]; same_set (sets[i], end, regs, maxnreg); ++end)
12900 : : {
12901 : 848 : size_t len = strlen (end->name);
12902 : 848 : if (len > maxname)
12903 : : maxname = len;
12904 : : }
12905 : :
12906 : : for (const struct register_info *reg = sets[i];
12907 [ + + ]: 392 : reg < end;
12908 : 324 : reg += reg->regloc->count ?: 1)
12909 [ + - ]: 324 : colno = handle_core_register (ebl, core, maxname,
12910 : 324 : reg->regloc, desc, colno);
12911 : :
12912 : : /* Force a line break at the end of the group. */
12913 : 68 : colno = WRAP_COLUMN;
12914 : : }
12915 : :
12916 : : return colno;
12917 : : }
12918 : :
12919 : : static void
12920 : 18 : handle_auxv_note (Ebl *ebl, Elf *core, GElf_Word descsz, GElf_Off desc_pos)
12921 : : {
12922 : 18 : Elf_Data *data = elf_getdata_rawchunk (core, desc_pos, descsz, ELF_T_AUXV);
12923 [ - + ]: 18 : if (data == NULL)
12924 : 0 : elf_error:
12925 : 0 : error_exit (0, _("cannot convert core note data: %s"), elf_errmsg (-1));
12926 : :
12927 : 18 : const size_t nauxv = descsz / gelf_fsize (core, ELF_T_AUXV, 1, EV_CURRENT);
12928 [ + + ]: 354 : for (size_t i = 0; i < nauxv; ++i)
12929 : : {
12930 : 336 : GElf_auxv_t av_mem;
12931 : 336 : GElf_auxv_t *av = gelf_getauxv (data, i, &av_mem);
12932 [ - + ]: 336 : if (av == NULL)
12933 : 0 : goto elf_error;
12934 : :
12935 : 336 : const char *name;
12936 : 336 : const char *fmt;
12937 [ - + ]: 336 : if (ebl_auxv_info (ebl, av->a_type, &name, &fmt) == 0)
12938 : : {
12939 : : /* Unknown type. */
12940 [ # # ]: 0 : if (av->a_un.a_val == 0)
12941 : 0 : printf (" %" PRIu64 "\n", av->a_type);
12942 : : else
12943 : 0 : printf (" %" PRIu64 ": %#" PRIx64 "\n",
12944 : : av->a_type, av->a_un.a_val);
12945 : : }
12946 : : else
12947 [ + + + - : 336 : switch (fmt[0])
+ - ]
12948 : : {
12949 : 18 : case '\0': /* Normally zero. */
12950 [ + - ]: 18 : if (av->a_un.a_val == 0)
12951 : : {
12952 : 18 : printf (" %s\n", name);
12953 : : break;
12954 : : }
12955 : 148 : FALLTHROUGH;
12956 : : case 'x': /* hex */
12957 : : case 'p': /* address */
12958 : : case 's': /* address of string */
12959 : 148 : printf (" %s: %#" PRIx64 "\n", name, av->a_un.a_val);
12960 : : break;
12961 : 162 : case 'u':
12962 : 162 : printf (" %s: %" PRIu64 "\n", name, av->a_un.a_val);
12963 : : break;
12964 : 0 : case 'd':
12965 : 0 : printf (" %s: %" PRId64 "\n", name, av->a_un.a_val);
12966 : : break;
12967 : :
12968 : 8 : case 'b':
12969 : 8 : printf (" %s: %#" PRIx64 " ", name, av->a_un.a_val);
12970 : 8 : GElf_Xword bit = 1;
12971 : 8 : const char *pfx = "<";
12972 [ + + ]: 220 : for (const char *p = fmt + 1; *p != 0; p = strchr (p, '\0') + 1)
12973 : : {
12974 [ + + ]: 212 : if (av->a_un.a_val & bit)
12975 : : {
12976 : 154 : printf ("%s%s", pfx, p);
12977 : 154 : pfx = " ";
12978 : : }
12979 : 212 : bit <<= 1;
12980 : : }
12981 : 336 : printf (">\n");
12982 : : break;
12983 : :
12984 : 0 : default:
12985 : 0 : abort ();
12986 : : }
12987 : : }
12988 : 18 : }
12989 : :
12990 : : static bool
12991 : 390 : buf_has_data (unsigned char const *ptr, unsigned char const *end, size_t sz)
12992 : : {
12993 [ - + - + ]: 390 : return ptr < end && (size_t) (end - ptr) >= sz;
12994 : : }
12995 : :
12996 : : static bool
12997 : 36 : buf_read_int (Elf *core, unsigned char const **ptrp, unsigned char const *end,
12998 : : int *retp)
12999 : : {
13000 [ + - + - ]: 72 : if (! buf_has_data (*ptrp, end, 4))
13001 : : return false;
13002 : :
13003 : 36 : *ptrp = convert (core, ELF_T_WORD, 1, retp, *ptrp, 4);
13004 : 36 : return true;
13005 : : }
13006 : :
13007 : : static bool
13008 : 354 : buf_read_ulong (Elf *core, unsigned char const **ptrp, unsigned char const *end,
13009 : : uint64_t *retp)
13010 : : {
13011 : 354 : size_t sz = gelf_fsize (core, ELF_T_ADDR, 1, EV_CURRENT);
13012 [ + - + - ]: 708 : if (! buf_has_data (*ptrp, end, sz))
13013 : : return false;
13014 : :
13015 : 354 : union
13016 : : {
13017 : : uint64_t u64;
13018 : : uint32_t u32;
13019 : : } u;
13020 : :
13021 : 354 : *ptrp = convert (core, ELF_T_ADDR, 1, &u, *ptrp, sz);
13022 : :
13023 [ + + ]: 354 : if (sz == 4)
13024 : 186 : *retp = u.u32;
13025 : : else
13026 : 168 : *retp = u.u64;
13027 : : return true;
13028 : : }
13029 : :
13030 : : static void
13031 : 12 : handle_siginfo_note (Elf *core, GElf_Word descsz, GElf_Off desc_pos)
13032 : : {
13033 : 12 : Elf_Data *data = elf_getdata_rawchunk (core, desc_pos, descsz, ELF_T_BYTE);
13034 [ - + ]: 12 : if (data == NULL)
13035 : 0 : error_exit (0, _("cannot convert core note data: %s"), elf_errmsg (-1));
13036 : :
13037 : 12 : unsigned char const *ptr = data->d_buf;
13038 : 12 : unsigned char const *const end = data->d_buf + data->d_size;
13039 : :
13040 : : /* Siginfo head is three ints: signal number, error number, origin
13041 : : code. */
13042 : 12 : int si_signo, si_errno, si_code;
13043 [ - + ]: 12 : if (! buf_read_int (core, &ptr, end, &si_signo)
13044 [ - + ]: 12 : || ! buf_read_int (core, &ptr, end, &si_errno)
13045 [ - + ]: 12 : || ! buf_read_int (core, &ptr, end, &si_code))
13046 : : {
13047 : 0 : fail:
13048 : 0 : printf (" Not enough data in NT_SIGINFO note.\n");
13049 : 0 : return;
13050 : : }
13051 : :
13052 : : /* Next is a pointer-aligned union of structures. On 64-bit
13053 : : machines, that implies a word of padding. */
13054 [ + + ]: 12 : if (gelf_getclass (core) == ELFCLASS64)
13055 : 6 : ptr += 4;
13056 : :
13057 : 12 : printf (" si_signo: %d, si_errno: %d, si_code: %d\n",
13058 : : si_signo, si_errno, si_code);
13059 : :
13060 [ + - ]: 12 : if (si_code > 0)
13061 [ + - ]: 12 : switch (si_signo)
13062 : : {
13063 : 12 : case CORE_SIGILL:
13064 : : case CORE_SIGFPE:
13065 : : case CORE_SIGSEGV:
13066 : : case CORE_SIGBUS:
13067 : : {
13068 : 12 : uint64_t addr;
13069 [ - + ]: 12 : if (! buf_read_ulong (core, &ptr, end, &addr))
13070 : 0 : goto fail;
13071 : 12 : printf (" fault address: %#" PRIx64 "\n", addr);
13072 : 12 : break;
13073 : : }
13074 : : default:
13075 : : ;
13076 : : }
13077 [ # # ]: 0 : else if (si_code == CORE_SI_USER)
13078 : : {
13079 : 0 : int pid, uid;
13080 [ # # ]: 0 : if (! buf_read_int (core, &ptr, end, &pid)
13081 [ # # ]: 0 : || ! buf_read_int (core, &ptr, end, &uid))
13082 : 0 : goto fail;
13083 : 0 : printf (" sender PID: %d, sender UID: %d\n", pid, uid);
13084 : : }
13085 : : }
13086 : :
13087 : : static void
13088 : 12 : handle_file_note (Elf *core, GElf_Word descsz, GElf_Off desc_pos)
13089 : : {
13090 : 12 : Elf_Data *data = elf_getdata_rawchunk (core, desc_pos, descsz, ELF_T_BYTE);
13091 [ - + ]: 12 : if (data == NULL)
13092 : 0 : error_exit (0, _("cannot convert core note data: %s"), elf_errmsg (-1));
13093 : :
13094 : 12 : unsigned char const *ptr = data->d_buf;
13095 : 12 : unsigned char const *const end = data->d_buf + data->d_size;
13096 : :
13097 : 12 : uint64_t count, page_size;
13098 [ - + ]: 12 : if (! buf_read_ulong (core, &ptr, end, &count)
13099 [ - + ]: 12 : || ! buf_read_ulong (core, &ptr, end, &page_size))
13100 : : {
13101 : 0 : fail:
13102 : 0 : printf (" Not enough data in NT_FILE note.\n");
13103 : 0 : return;
13104 : : }
13105 : :
13106 : 12 : size_t addrsize = gelf_fsize (core, ELF_T_ADDR, 1, EV_CURRENT);
13107 : 12 : uint64_t maxcount = (size_t) (end - ptr) / (3 * addrsize);
13108 [ - + ]: 12 : if (count > maxcount)
13109 : 0 : goto fail;
13110 : :
13111 : : /* Where file names are stored. */
13112 : 12 : unsigned char const *const fstart = ptr + 3 * count * addrsize;
13113 : 12 : char const *fptr = (char *) fstart;
13114 : :
13115 : 12 : printf (" %" PRId64 " files:\n", count);
13116 [ + + ]: 118 : for (uint64_t i = 0; i < count; ++i)
13117 : : {
13118 : 106 : uint64_t mstart, mend, moffset;
13119 [ + - ]: 106 : if (! buf_read_ulong (core, &ptr, fstart, &mstart)
13120 [ + - ]: 106 : || ! buf_read_ulong (core, &ptr, fstart, &mend)
13121 [ - + ]: 106 : || ! buf_read_ulong (core, &ptr, fstart, &moffset))
13122 : 0 : goto fail;
13123 : :
13124 : 106 : const char *fnext = memchr (fptr, '\0', (char *) end - fptr);
13125 [ - + ]: 106 : if (fnext == NULL)
13126 : 0 : goto fail;
13127 : :
13128 : 106 : int ct = printf (" %08" PRIx64 "-%08" PRIx64
13129 : : " %08" PRIx64 " %" PRId64,
13130 : : mstart, mend, moffset * page_size, mend - mstart);
13131 [ + - ]: 106 : printf ("%*s%s\n", ct > 50 ? 3 : 53 - ct, "", fptr);
13132 : :
13133 : 106 : fptr = fnext + 1;
13134 : : }
13135 : : }
13136 : :
13137 : : static void
13138 : 76 : handle_core_note (Ebl *ebl, const GElf_Nhdr *nhdr,
13139 : : const char *name, const void *desc)
13140 : : {
13141 : 76 : GElf_Word regs_offset;
13142 : 76 : size_t nregloc;
13143 : 76 : const Ebl_Register_Location *reglocs;
13144 : 76 : size_t nitems;
13145 : 76 : const Ebl_Core_Item *items;
13146 : :
13147 [ + + ]: 76 : if (! ebl_core_note (ebl, nhdr, name, desc,
13148 : : ®s_offset, &nregloc, ®locs, &nitems, &items))
13149 : 2 : return;
13150 : :
13151 : : /* Pass 0 for DESCSZ when there are registers in the note,
13152 : : so that the ITEMS array does not describe the whole thing.
13153 : : For non-register notes, the actual descsz might be a multiple
13154 : : of the unit size, not just exactly the unit size. */
13155 : 74 : unsigned int colno = handle_core_items (ebl->elf, desc,
13156 [ + + ]: 74 : nregloc == 0 ? nhdr->n_descsz : 0,
13157 : : items, nitems);
13158 [ + + ]: 74 : if (colno != 0)
13159 : 68 : putchar ('\n');
13160 : :
13161 : 74 : colno = handle_core_registers (ebl, ebl->elf, desc + regs_offset,
13162 : : reglocs, nregloc);
13163 [ + + ]: 74 : if (colno != 0)
13164 : 74 : putchar ('\n');
13165 : : }
13166 : :
13167 : : static void
13168 : 506 : handle_notes_data (Ebl *ebl, const GElf_Ehdr *ehdr,
13169 : : GElf_Off start, Elf_Data *data)
13170 : : {
13171 : 506 : fputs (_(" Owner Data size Type\n"), stdout);
13172 : :
13173 [ - + ]: 506 : if (data == NULL)
13174 : 0 : goto bad_note;
13175 : :
13176 : : size_t offset = 0;
13177 : : GElf_Nhdr nhdr;
13178 : : size_t name_offset;
13179 : : size_t desc_offset;
13180 : 5120 : while (offset < data->d_size
13181 [ + + + - ]: 5120 : && (offset = gelf_getnote (data, offset,
13182 : : &nhdr, &name_offset, &desc_offset)) > 0)
13183 : : {
13184 [ + - ]: 4614 : const char *name = nhdr.n_namesz == 0 ? "" : data->d_buf + name_offset;
13185 : 4614 : const char *desc = data->d_buf + desc_offset;
13186 : :
13187 : : /* GNU Build Attributes are weird, they store most of their data
13188 : : into the owner name field. Extract just the owner name
13189 : : prefix here, then use the rest later as data. */
13190 : 4614 : bool is_gnu_build_attr
13191 : 4614 : = startswith (name, ELF_NOTE_GNU_BUILD_ATTRIBUTE_PREFIX);
13192 : 9798 : const char *print_name = (is_gnu_build_attr
13193 [ + + ]: 4614 : ? ELF_NOTE_GNU_BUILD_ATTRIBUTE_PREFIX : name);
13194 : 5184 : size_t print_namesz = (is_gnu_build_attr
13195 : 570 : ? strlen (print_name) : nhdr.n_namesz);
13196 : :
13197 : 4614 : char buf[100];
13198 : 4614 : char buf2[100];
13199 : 4614 : printf (_(" %-13.*s %9" PRId32 " %s\n"),
13200 : : (int) print_namesz, print_name, nhdr.n_descsz,
13201 [ + + ]: 4614 : ehdr->e_type == ET_CORE
13202 : 118 : ? ebl_core_note_type_name (ebl, nhdr.n_type,
13203 : : buf, sizeof (buf))
13204 : 4496 : : ebl_object_note_type_name (ebl, name, nhdr.n_type,
13205 : : nhdr.n_descsz,
13206 : : buf2, sizeof (buf2)));
13207 : :
13208 : : /* Filter out invalid entries. */
13209 : 4614 : if (memchr (name, '\0', nhdr.n_namesz) != NULL
13210 : : /* XXX For now help broken Linux kernels. */
13211 : : || 1)
13212 : : {
13213 [ + + ]: 4614 : if (ehdr->e_type == ET_CORE)
13214 : : {
13215 [ + + ]: 118 : if (nhdr.n_type == NT_AUXV
13216 [ + - ]: 18 : && (nhdr.n_namesz == 4 /* Broken old Linux kernels. */
13217 [ + - + - ]: 18 : || (nhdr.n_namesz == 5 && name[4] == '\0'))
13218 [ + - ]: 18 : && !memcmp (name, "CORE", 4))
13219 : 18 : handle_auxv_note (ebl, ebl->elf, nhdr.n_descsz,
13220 : : start + desc_offset);
13221 [ + + + - ]: 100 : else if (nhdr.n_namesz == 5 && strcmp (name, "CORE") == 0)
13222 [ + + + ]: 76 : switch (nhdr.n_type)
13223 : : {
13224 : 12 : case NT_SIGINFO:
13225 : 12 : handle_siginfo_note (ebl->elf, nhdr.n_descsz,
13226 : : start + desc_offset);
13227 : 12 : break;
13228 : :
13229 : 12 : case NT_FILE:
13230 : 12 : handle_file_note (ebl->elf, nhdr.n_descsz,
13231 : : start + desc_offset);
13232 : 12 : break;
13233 : :
13234 : 52 : default:
13235 : 52 : handle_core_note (ebl, &nhdr, name, desc);
13236 : : }
13237 : : else
13238 : 24 : handle_core_note (ebl, &nhdr, name, desc);
13239 : : }
13240 : : else
13241 : 4496 : ebl_object_note (ebl, nhdr.n_namesz, name, nhdr.n_type,
13242 : : nhdr.n_descsz, desc);
13243 : : }
13244 : : }
13245 : :
13246 [ + - ]: 506 : if (offset == data->d_size)
13247 : 506 : return;
13248 : :
13249 : 0 : bad_note:
13250 : 0 : error (0, 0,
13251 : 0 : _("cannot get content of note: %s"),
13252 : 0 : data != NULL ? "garbage data" : elf_errmsg (-1));
13253 : : }
13254 : :
13255 : : static void
13256 : 230 : handle_notes (Ebl *ebl, GElf_Ehdr *ehdr)
13257 : : {
13258 : : /* If we have section headers, just look for SHT_NOTE sections.
13259 : : In a debuginfo file, the program headers are not reliable. */
13260 [ + + ]: 230 : if (shnum != 0)
13261 : : {
13262 : : /* Get the section header string table index. */
13263 : 208 : size_t shstrndx;
13264 [ - + ]: 208 : if (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0)
13265 : 0 : error_exit (0, _("cannot get section header string table index"));
13266 : :
13267 : : Elf_Scn *scn = NULL;
13268 [ + + ]: 6446 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
13269 : : {
13270 : 6238 : GElf_Shdr shdr_mem;
13271 : 6238 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
13272 : :
13273 [ + - + + ]: 6238 : if (shdr == NULL || shdr->sh_type != SHT_NOTE)
13274 : : /* Not what we are looking for. */
13275 : 5756 : continue;
13276 : :
13277 [ - + ]: 482 : if (notes_section != NULL)
13278 : : {
13279 : 0 : char *sname = elf_strptr (ebl->elf, shstrndx, shdr->sh_name);
13280 [ # # # # ]: 0 : if (sname == NULL || strcmp (sname, notes_section) != 0)
13281 : 0 : continue;
13282 : : }
13283 : :
13284 : 482 : printf (_("\
13285 : : \nNote section [%2zu] '%s' of %" PRIu64 " bytes at offset %#0" PRIx64 ":\n"),
13286 : : elf_ndxscn (scn),
13287 : 482 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
13288 : : shdr->sh_size, shdr->sh_offset);
13289 : :
13290 : 482 : handle_notes_data (ebl, ehdr, shdr->sh_offset,
13291 : : elf_getdata (scn, NULL));
13292 : : }
13293 : 208 : return;
13294 : : }
13295 : :
13296 : : /* We have to look through the program header to find the note
13297 : : sections. There can be more than one. */
13298 [ + + ]: 280 : for (size_t cnt = 0; cnt < phnum; ++cnt)
13299 : : {
13300 : 258 : GElf_Phdr mem;
13301 : 258 : GElf_Phdr *phdr = gelf_getphdr (ebl->elf, cnt, &mem);
13302 : :
13303 [ + - + + ]: 258 : if (phdr == NULL || phdr->p_type != PT_NOTE)
13304 : : /* Not what we are looking for. */
13305 : 234 : continue;
13306 : :
13307 : 24 : printf (_("\
13308 : : \nNote segment of %" PRIu64 " bytes at offset %#0" PRIx64 ":\n"),
13309 : : phdr->p_filesz, phdr->p_offset);
13310 : :
13311 : 24 : handle_notes_data (ebl, ehdr, phdr->p_offset,
13312 : : elf_getdata_rawchunk (ebl->elf,
13313 : 24 : phdr->p_offset, phdr->p_filesz,
13314 [ + + ]: 24 : (phdr->p_align == 8
13315 : : ? ELF_T_NHDR8 : ELF_T_NHDR)));
13316 : : }
13317 : : }
13318 : :
13319 : :
13320 : : static void
13321 : 12 : hex_dump (const uint8_t *data, size_t len)
13322 : : {
13323 : 12 : size_t pos = 0;
13324 [ + + ]: 56 : while (pos < len)
13325 : : {
13326 : 44 : printf (" 0x%08zx ", pos);
13327 : :
13328 : 44 : const size_t chunk = MIN (len - pos, 16);
13329 : :
13330 [ + + ]: 688 : for (size_t i = 0; i < chunk; ++i)
13331 [ + + ]: 644 : if (i % 4 == 3)
13332 : 160 : printf ("%02x ", data[pos + i]);
13333 : : else
13334 : 644 : printf ("%02x", data[pos + i]);
13335 : :
13336 [ + + ]: 44 : if (chunk < 16)
13337 : 4 : printf ("%*s", (int) ((16 - chunk) * 2 + (16 - chunk + 3) / 4), "");
13338 : :
13339 [ + + ]: 688 : for (size_t i = 0; i < chunk; ++i)
13340 : : {
13341 : 644 : unsigned char b = data[pos + i];
13342 [ + + ]: 1288 : printf ("%c", isprint (b) ? b : '.');
13343 : : }
13344 : :
13345 : 44 : putchar ('\n');
13346 : 44 : pos += chunk;
13347 : : }
13348 : 12 : }
13349 : :
13350 : : static void
13351 : 12 : dump_data_section (Elf_Scn *scn, const GElf_Shdr *shdr, const char *name)
13352 : : {
13353 [ + - - + ]: 12 : if (shdr->sh_size == 0 || shdr->sh_type == SHT_NOBITS)
13354 : 0 : printf (_("\nSection [%zu] '%s' has no data to dump.\n"),
13355 : : elf_ndxscn (scn), name);
13356 : : else
13357 : : {
13358 [ + + ]: 12 : if (print_decompress)
13359 : : {
13360 : : /* We try to decompress the section, but keep the old shdr around
13361 : : so we can show both the original shdr size and the uncompressed
13362 : : data size. */
13363 [ + + ]: 8 : if ((shdr->sh_flags & SHF_COMPRESSED) != 0)
13364 : : {
13365 [ - + ]: 4 : if (elf_compress (scn, 0, 0) < 0)
13366 : 0 : printf ("WARNING: %s [%zd]\n",
13367 : : _("Couldn't uncompress section"),
13368 : : elf_ndxscn (scn));
13369 : : }
13370 [ + - + - ]: 4 : else if (name && startswith (name, ".zdebug"))
13371 : : {
13372 [ - + ]: 4 : if (elf_compress_gnu (scn, 0, 0) < 0)
13373 : 0 : printf ("WARNING: %s [%zd]\n",
13374 : : _("Couldn't uncompress section"),
13375 : : elf_ndxscn (scn));
13376 : : }
13377 : : }
13378 : :
13379 : 12 : Elf_Data *data = elf_rawdata (scn, NULL);
13380 [ - + ]: 12 : if (data == NULL)
13381 : 0 : error (0, 0, _("cannot get data for section [%zu] '%s': %s"),
13382 : : elf_ndxscn (scn), name, elf_errmsg (-1));
13383 : : else
13384 : : {
13385 [ + + ]: 12 : if (data->d_size == shdr->sh_size)
13386 : 4 : printf (_("\nHex dump of section [%zu] '%s', %" PRIu64
13387 : : " bytes at offset %#0" PRIx64 ":\n"),
13388 : : elf_ndxscn (scn), name,
13389 : 4 : shdr->sh_size, shdr->sh_offset);
13390 : : else
13391 : 8 : printf (_("\nHex dump of section [%zu] '%s', %" PRIu64
13392 : : " bytes (%zd uncompressed) at offset %#0"
13393 : : PRIx64 ":\n"),
13394 : : elf_ndxscn (scn), name,
13395 : 8 : shdr->sh_size, data->d_size, shdr->sh_offset);
13396 : 12 : hex_dump (data->d_buf, data->d_size);
13397 : : }
13398 : : }
13399 : 12 : }
13400 : :
13401 : : static void
13402 : 374 : print_string_section (Elf_Scn *scn, const GElf_Shdr *shdr, const char *name)
13403 : : {
13404 [ + - + + ]: 374 : if (shdr->sh_size == 0 || shdr->sh_type == SHT_NOBITS)
13405 : 42 : printf (_("\nSection [%zu] '%s' has no strings to dump.\n"),
13406 : : elf_ndxscn (scn), name);
13407 : : else
13408 : : {
13409 [ + + ]: 332 : if (print_decompress)
13410 : : {
13411 : : /* We try to decompress the section, but keep the old shdr around
13412 : : so we can show both the original shdr size and the uncompressed
13413 : : data size. */
13414 [ - + ]: 2 : if ((shdr->sh_flags & SHF_COMPRESSED) != 0)
13415 : : {
13416 [ # # ]: 0 : if (elf_compress (scn, 0, 0) < 0)
13417 : 0 : printf ("WARNING: %s [%zd]\n",
13418 : : _("Couldn't uncompress section"),
13419 : : elf_ndxscn (scn));
13420 : : }
13421 [ + - + - ]: 2 : else if (name && startswith (name, ".zdebug"))
13422 : : {
13423 [ - + ]: 2 : if (elf_compress_gnu (scn, 0, 0) < 0)
13424 : 0 : printf ("WARNING: %s [%zd]\n",
13425 : : _("Couldn't uncompress section"),
13426 : : elf_ndxscn (scn));
13427 : : }
13428 : : }
13429 : :
13430 : 332 : Elf_Data *data = elf_rawdata (scn, NULL);
13431 [ - + ]: 332 : if (data == NULL)
13432 : 0 : error (0, 0, _("cannot get data for section [%zu] '%s': %s"),
13433 : : elf_ndxscn (scn), name, elf_errmsg (-1));
13434 : : else
13435 : : {
13436 [ + + ]: 332 : if (data->d_size == shdr->sh_size)
13437 : 330 : printf (_("\nString section [%zu] '%s' contains %" PRIu64
13438 : : " bytes at offset %#0" PRIx64 ":\n"),
13439 : : elf_ndxscn (scn), name,
13440 : 330 : shdr->sh_size, shdr->sh_offset);
13441 : : else
13442 : 2 : printf (_("\nString section [%zu] '%s' contains %" PRIu64
13443 : : " bytes (%zd uncompressed) at offset %#0"
13444 : : PRIx64 ":\n"),
13445 : : elf_ndxscn (scn), name,
13446 : 2 : shdr->sh_size, data->d_size, shdr->sh_offset);
13447 : :
13448 : 332 : const char *start = data->d_buf;
13449 : 332 : const char *const limit = start + data->d_size;
13450 : 44686 : do
13451 : : {
13452 : 44686 : const char *end = memchr (start, '\0', limit - start);
13453 : 44686 : const size_t pos = start - (const char *) data->d_buf;
13454 [ - + ]: 44686 : if (unlikely (end == NULL))
13455 : : {
13456 : 0 : printf (" [%6zx]- %.*s\n",
13457 : : pos, (int) (limit - start), start);
13458 : : break;
13459 : : }
13460 : 44686 : printf (" [%6zx] %s\n", pos, start);
13461 : 44686 : start = end + 1;
13462 [ + + ]: 44686 : } while (start < limit);
13463 : : }
13464 : : }
13465 : 374 : }
13466 : :
13467 : : static void
13468 : 198 : for_each_section_argument (Elf *elf, const struct section_argument *list,
13469 : : void (*dump) (Elf_Scn *scn, const GElf_Shdr *shdr,
13470 : : const char *name))
13471 : : {
13472 : : /* Get the section header string table index. */
13473 : 198 : size_t shstrndx;
13474 [ - + ]: 198 : if (elf_getshdrstrndx (elf, &shstrndx) < 0)
13475 : 0 : error_exit (0, _("cannot get section header string table index"));
13476 : :
13477 [ + + ]: 764 : for (const struct section_argument *a = list; a != NULL; a = a->next)
13478 : : {
13479 : 566 : Elf_Scn *scn;
13480 : 566 : GElf_Shdr shdr_mem;
13481 : 566 : const char *name = NULL;
13482 : :
13483 : 566 : char *endp = NULL;
13484 : 566 : unsigned long int shndx = strtoul (a->arg, &endp, 0);
13485 [ + + - + ]: 566 : if (endp != a->arg && *endp == '\0')
13486 : : {
13487 : 2 : scn = elf_getscn (elf, shndx);
13488 [ - + ]: 2 : if (scn == NULL)
13489 : : {
13490 : 0 : error (0, 0, _("\nsection [%lu] does not exist"), shndx);
13491 : 0 : continue;
13492 : : }
13493 : :
13494 [ - + ]: 2 : if (gelf_getshdr (scn, &shdr_mem) == NULL)
13495 : 0 : error_exit (0, _("cannot get section header: %s"),
13496 : : elf_errmsg (-1));
13497 : 2 : name = elf_strptr (elf, shstrndx, shdr_mem.sh_name);
13498 : 2 : (*dump) (scn, &shdr_mem, name);
13499 : : }
13500 : : else
13501 : : {
13502 : : /* Need to look up the section by name. */
13503 : : scn = NULL;
13504 : : bool found = false;
13505 [ + + ]: 18180 : while ((scn = elf_nextscn (elf, scn)) != NULL)
13506 : : {
13507 [ - + ]: 17616 : if (gelf_getshdr (scn, &shdr_mem) == NULL)
13508 : 0 : continue;
13509 : 17616 : name = elf_strptr (elf, shstrndx, shdr_mem.sh_name);
13510 [ - + ]: 17616 : if (name == NULL)
13511 : 0 : continue;
13512 [ + + ]: 17616 : if (!strcmp (name, a->arg))
13513 : : {
13514 : 384 : found = true;
13515 : 384 : (*dump) (scn, &shdr_mem, name);
13516 : : }
13517 : : }
13518 : :
13519 [ + + - + ]: 564 : if (unlikely (!found) && !a->implicit)
13520 : 566 : error (0, 0, _("\nsection '%s' does not exist"), a->arg);
13521 : : }
13522 : : }
13523 : 198 : }
13524 : :
13525 : : static void
13526 : 12 : dump_data (Ebl *ebl)
13527 : : {
13528 : 12 : for_each_section_argument (ebl->elf, dump_data_sections, &dump_data_section);
13529 : 12 : }
13530 : :
13531 : : static void
13532 : 186 : dump_strings (Ebl *ebl)
13533 : : {
13534 : 186 : for_each_section_argument (ebl->elf, string_sections, &print_string_section);
13535 : 186 : }
13536 : :
13537 : : static void
13538 : 0 : print_strings (Ebl *ebl)
13539 : : {
13540 : : /* Get the section header string table index. */
13541 : 0 : size_t shstrndx;
13542 [ # # ]: 0 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
13543 : 0 : error_exit (0, _("cannot get section header string table index"));
13544 : :
13545 : : Elf_Scn *scn;
13546 : : GElf_Shdr shdr_mem;
13547 : : const char *name;
13548 : : scn = NULL;
13549 [ # # ]: 0 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
13550 : : {
13551 [ # # ]: 0 : if (gelf_getshdr (scn, &shdr_mem) == NULL)
13552 : 0 : continue;
13553 : :
13554 [ # # ]: 0 : if (shdr_mem.sh_type != SHT_PROGBITS
13555 [ # # ]: 0 : || !(shdr_mem.sh_flags & SHF_STRINGS))
13556 : 0 : continue;
13557 : :
13558 : 0 : name = elf_strptr (ebl->elf, shstrndx, shdr_mem.sh_name);
13559 [ # # ]: 0 : if (name == NULL)
13560 : 0 : continue;
13561 : :
13562 : 0 : print_string_section (scn, &shdr_mem, name);
13563 : : }
13564 : 0 : }
13565 : :
13566 : : static void
13567 : 4 : dump_archive_index (Elf *elf, const char *fname)
13568 : : {
13569 : 4 : size_t narsym;
13570 : 4 : const Elf_Arsym *arsym = elf_getarsym (elf, &narsym);
13571 [ - + ]: 4 : if (arsym == NULL)
13572 : : {
13573 : 0 : int result = elf_errno ();
13574 [ # # ]: 0 : if (unlikely (result != ELF_E_NO_INDEX))
13575 : 0 : error_exit (0, _("cannot get symbol index of archive '%s': %s"),
13576 : : fname, elf_errmsg (result));
13577 : : else
13578 : 0 : printf (_("\nArchive '%s' has no symbol index\n"), fname);
13579 : 0 : return;
13580 : : }
13581 : :
13582 : 4 : printf (_("\nIndex of archive '%s' has %zu entries:\n"),
13583 : : fname, narsym);
13584 : :
13585 : 4 : size_t as_off = 0;
13586 [ + + ]: 22 : for (const Elf_Arsym *s = arsym; s < &arsym[narsym - 1]; ++s)
13587 : : {
13588 [ + + ]: 18 : if (s->as_off != as_off)
13589 : : {
13590 : 12 : as_off = s->as_off;
13591 : :
13592 : 12 : Elf *subelf = NULL;
13593 [ + - ]: 12 : if (unlikely (elf_rand (elf, as_off) == 0)
13594 [ - + ]: 12 : || unlikely ((subelf = elf_begin (-1, ELF_C_READ_MMAP, elf))
13595 : : == NULL))
13596 : : #if __GLIBC__ < 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ < 7)
13597 : : while (1)
13598 : : #endif
13599 : 0 : error_exit (0,
13600 : : _("cannot extract member at offset %zu in '%s': %s"),
13601 : : as_off, fname, elf_errmsg (-1));
13602 : :
13603 : 12 : const Elf_Arhdr *h = elf_getarhdr (subelf);
13604 [ + - ]: 12 : if (h != NULL)
13605 : 12 : printf (_("Archive member '%s' contains:\n"), h->ar_name);
13606 : :
13607 : 12 : elf_end (subelf);
13608 : : }
13609 : :
13610 : 18 : printf ("\t%s\n", s->as_name);
13611 : : }
13612 : : }
13613 : :
13614 : : #include "debugpred.h"
|