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 : : static void print_flag_info(void);
345 : :
346 : : enum dyn_idx
347 : : {
348 : : i_symtab_shndx,
349 : : i_strsz,
350 : : i_verneed,
351 : : i_verneednum,
352 : : i_verdef,
353 : : i_verdefnum,
354 : : i_versym,
355 : : i_symtab,
356 : : i_strtab,
357 : : i_hash,
358 : : i_gnu_hash,
359 : : i_max
360 : : };
361 : :
362 : : /* Declarations of local functions for use-dynamic. */
363 : : static Elf_Data *get_dynscn_strtab (Elf *elf, GElf_Phdr *phdr);
364 : : static void get_dynscn_addrs (Elf *elf, GElf_Phdr *phdr, GElf_Addr addrs[i_max]);
365 : : static void find_offsets (Elf *elf, GElf_Addr main_bias, size_t n,
366 : : GElf_Addr addrs[n], GElf_Off offs[n]);
367 : :
368 : : /* Looked up once with gettext in main. */
369 : : static char *yes_str;
370 : : static char *no_str;
371 : :
372 : : static void
373 : 1780 : cleanup_list (struct section_argument *list)
374 : : {
375 [ + + + + ]: 2346 : while (list != NULL)
376 : : {
377 : 566 : struct section_argument *a = list;
378 : 566 : list = a->next;
379 : 566 : free (a);
380 : : }
381 : : }
382 : :
383 : : int
384 : 890 : main (int argc, char *argv[])
385 : : {
386 : : /* We use no threads here which can interfere with handling a stream. */
387 : 890 : (void) __fsetlocking (stdout, FSETLOCKING_BYCALLER);
388 : :
389 : : /* Set locale. */
390 : 890 : setlocale (LC_ALL, "");
391 : :
392 : : /* Initialize the message catalog. */
393 : 890 : textdomain (PACKAGE_TARNAME);
394 : :
395 : : /* Look up once. */
396 : 890 : yes_str = _("yes");
397 : 890 : no_str = _("no");
398 : :
399 : : /* Parse and process arguments. */
400 : 890 : int remaining;
401 : 890 : argp_parse (&argp, argc, argv, 0, &remaining, NULL);
402 : :
403 : : /* Before we start tell the ELF library which version we are using. */
404 : 890 : elf_version (EV_CURRENT);
405 : :
406 : : /* Now process all the files given at the command line. */
407 : 890 : bool only_one = remaining + 1 == argc;
408 : 972 : do
409 : : {
410 : : /* Open the file. */
411 : 972 : int fd = open (argv[remaining], O_RDONLY);
412 [ - + ]: 972 : if (fd == -1)
413 : : {
414 : 0 : error (0, errno, _("cannot open input file '%s'"), argv[remaining]);
415 : 0 : continue;
416 : : }
417 : :
418 : 972 : process_file (fd, argv[remaining], only_one);
419 : :
420 : 972 : close (fd);
421 : : }
422 [ + + ]: 972 : while (++remaining < argc);
423 : :
424 : 890 : cleanup_list (dump_data_sections);
425 : 890 : cleanup_list (string_sections);
426 : :
427 : 890 : return error_message_count != 0;
428 : : }
429 : :
430 : : static void
431 : 566 : add_dump_section (const char *name,
432 : : int key,
433 : : bool implicit)
434 : : {
435 : 566 : struct section_argument *a = xmalloc (sizeof *a);
436 : 566 : a->arg = name;
437 : 566 : a->next = NULL;
438 : 566 : a->implicit = implicit;
439 : 1132 : struct section_argument ***tailp
440 [ + + ]: 566 : = key == 'x' ? &dump_data_sections_tail : &string_sections_tail;
441 : 566 : **tailp = a;
442 : 566 : *tailp = &a->next;
443 : 566 : }
444 : :
445 : : /* Handle program arguments. */
446 : : static error_t
447 : 5662 : parse_opt (int key, char *arg,
448 : : struct argp_state *state __attribute__ ((unused)))
449 : : {
450 [ + + - + : 5662 : switch (key)
+ - + + -
+ + + + +
+ - + + +
+ + + - +
+ + + + ]
451 : : {
452 : 184 : case 'a':
453 : 184 : print_file_header = true;
454 : 184 : print_program_header = true;
455 : 184 : print_relocations = true;
456 : 184 : print_section_header = true;
457 : 184 : print_symbol_table = true;
458 : 184 : print_version_info = true;
459 : 184 : print_dynamic_table = true;
460 : 184 : print_section_groups = true;
461 : 184 : print_histogram = true;
462 : 184 : print_arch = true;
463 : 184 : print_notes = true;
464 : 184 : implicit_debug_sections |= section_exception;
465 : 184 : add_dump_section (".strtab", key, true);
466 : 184 : add_dump_section (".dynstr", key, true);
467 : 184 : add_dump_section (".comment", key, true);
468 : 184 : any_control_option = true;
469 : 184 : break;
470 : 8 : case 'A':
471 : 8 : print_arch = true;
472 : 8 : any_control_option = true;
473 : 8 : break;
474 : 6 : case 'd':
475 : 6 : print_dynamic_table = true;
476 : 6 : any_control_option = true;
477 : 6 : break;
478 : 2 : case 'D':
479 : 2 : use_dynamic_segment = true;
480 : 2 : break;
481 : 0 : case 'e':
482 : 0 : print_debug_sections |= section_exception;
483 : 0 : any_control_option = true;
484 : 0 : break;
485 : 18 : case 'g':
486 : 18 : print_section_groups = true;
487 : 18 : any_control_option = true;
488 : 18 : break;
489 : 6 : case 'h':
490 : 6 : print_file_header = true;
491 : 6 : any_control_option = true;
492 : 6 : break;
493 : 0 : case 'I':
494 : 0 : print_histogram = true;
495 : 0 : any_control_option = true;
496 : 0 : break;
497 : 4 : case 'l':
498 : 4 : print_program_header = true;
499 : 4 : any_control_option = true;
500 : 4 : break;
501 : 46 : case 'n':
502 : 46 : print_notes = true;
503 : 46 : any_control_option = true;
504 : 46 : notes_section = arg;
505 : 46 : break;
506 : 10 : case 'r':
507 : 10 : print_relocations = true;
508 : 10 : any_control_option = true;
509 : 10 : break;
510 : 322 : case 'S':
511 : 322 : print_section_header = true;
512 : 322 : any_control_option = true;
513 : 322 : break;
514 : 34 : case 's':
515 : 34 : print_symbol_table = true;
516 : 34 : any_control_option = true;
517 : 34 : symbol_table_section = arg;
518 : 34 : break;
519 : 2 : case PRINT_DYNSYM_TABLE:
520 : 2 : print_dynsym_table = true;
521 : 2 : any_control_option = true;
522 : 2 : break;
523 : 0 : case 'V':
524 : 0 : print_version_info = true;
525 : 0 : any_control_option = true;
526 : 0 : break;
527 : 4 : case 'c':
528 : 4 : print_archive_index = true;
529 : 4 : break;
530 : 264 : case 'w':
531 [ + + ]: 264 : if (arg == NULL)
532 : : {
533 : 86 : print_debug_sections = section_all;
534 : 86 : implicit_debug_sections = section_info;
535 : 86 : show_split_units = true;
536 : : }
537 [ - + ]: 178 : else if (strcmp (arg, "abbrev") == 0)
538 : 0 : print_debug_sections |= section_abbrev;
539 [ + + ]: 178 : else if (strcmp (arg, "addr") == 0)
540 : : {
541 : 4 : print_debug_sections |= section_addr;
542 : 4 : implicit_debug_sections |= section_info;
543 : : }
544 [ + + ]: 174 : else if (strcmp (arg, "aranges") == 0)
545 : 6 : print_debug_sections |= section_aranges;
546 [ + + ]: 168 : else if (strcmp (arg, "decodedaranges") == 0)
547 : : {
548 : 2 : print_debug_sections |= section_aranges;
549 : 2 : decodedaranges = true;
550 : : }
551 [ + + ]: 166 : else if (strcmp (arg, "ranges") == 0)
552 : : {
553 : 26 : print_debug_sections |= section_ranges;
554 : 26 : implicit_debug_sections |= section_info;
555 : : }
556 [ + + + + ]: 140 : else if (strcmp (arg, "frame") == 0 || strcmp (arg, "frames") == 0)
557 : 6 : print_debug_sections |= section_frame;
558 [ + + ]: 134 : else if (strcmp (arg, "info") == 0)
559 : : {
560 : 40 : print_debug_sections |= section_info;
561 : 40 : print_debug_sections |= section_types;
562 : : }
563 [ + + ]: 94 : else if (strcmp (arg, "info+") == 0)
564 : : {
565 : 4 : print_debug_sections |= section_info;
566 : 4 : print_debug_sections |= section_types;
567 : 4 : show_split_units = true;
568 : : }
569 [ + + ]: 90 : else if (strcmp (arg, "loc") == 0)
570 : : {
571 : 38 : print_debug_sections |= section_loc;
572 : 38 : implicit_debug_sections |= section_info;
573 : : }
574 [ + + ]: 52 : else if (strcmp (arg, "line") == 0)
575 : 16 : print_debug_sections |= section_line;
576 [ + + ]: 36 : else if (strcmp (arg, "decodedline") == 0)
577 : : {
578 : 14 : print_debug_sections |= section_line;
579 : 14 : decodedline = true;
580 : : }
581 [ - + ]: 22 : else if (strcmp (arg, "pubnames") == 0)
582 : 0 : print_debug_sections |= section_pubnames;
583 [ + + ]: 22 : else if (strcmp (arg, "str") == 0)
584 : : {
585 : 6 : print_debug_sections |= section_str;
586 : : /* For mapping string offset tables to CUs. */
587 : 6 : implicit_debug_sections |= section_info;
588 : : }
589 [ - + ]: 16 : else if (strcmp (arg, "macinfo") == 0)
590 : 0 : print_debug_sections |= section_macinfo;
591 [ + + ]: 16 : else if (strcmp (arg, "macro") == 0)
592 : 8 : print_debug_sections |= section_macro;
593 [ - + ]: 8 : else if (strcmp (arg, "exception") == 0)
594 : 0 : print_debug_sections |= section_exception;
595 [ + - ]: 8 : else if (strcmp (arg, "gdb_index") == 0)
596 : 8 : print_debug_sections |= section_gdb_index;
597 : : else
598 : : {
599 : 0 : fprintf (stderr, _("Unknown DWARF debug section `%s'.\n"),
600 : : arg);
601 : 0 : argp_help (&argp, stderr, ARGP_HELP_SEE,
602 : : program_invocation_short_name);
603 : 0 : exit (1);
604 : : }
605 : 264 : any_control_option = true;
606 : 264 : break;
607 : 2 : case 'p':
608 : 2 : any_control_option = true;
609 [ - + ]: 2 : if (arg == NULL)
610 : : {
611 : 0 : print_string_sections = true;
612 : 0 : break;
613 : : }
614 : 14 : FALLTHROUGH;
615 : : case 'x':
616 : 14 : add_dump_section (arg, key, false);
617 : 14 : any_control_option = true;
618 : 14 : break;
619 : 90 : case 'N':
620 : 90 : print_address_names = false;
621 : 90 : break;
622 : 56 : case 'U':
623 : 56 : print_unresolved_addresses = true;
624 : 56 : break;
625 : 0 : case ARGP_KEY_NO_ARGS:
626 : 0 : fputs (_("Missing file name.\n"), stderr);
627 : 0 : goto do_argp_help;
628 : 890 : case ARGP_KEY_FINI:
629 [ + + + - ]: 890 : if (! any_control_option && ! print_archive_index)
630 : : {
631 : 0 : fputs (_("No operation specified.\n"), stderr);
632 : 0 : do_argp_help:
633 : 0 : argp_help (&argp, stderr, ARGP_HELP_SEE,
634 : : program_invocation_short_name);
635 : 0 : exit (EXIT_FAILURE);
636 : : }
637 : : break;
638 : : case 'W': /* Ignored. */
639 : : break;
640 : 122 : case 'z':
641 : 122 : print_decompress = true;
642 : 122 : break;
643 : 10 : case ELF_INPUT_SECTION:
644 [ + - ]: 10 : if (arg == NULL)
645 : 10 : elf_input_section = ".gnu_debugdata";
646 : : else
647 : 0 : elf_input_section = arg;
648 : : break;
649 : 10 : case DWARF_SKELETON:
650 : 10 : dwarf_skeleton = arg;
651 : 10 : break;
652 : : default:
653 : : return ARGP_ERR_UNKNOWN;
654 : : }
655 : : return 0;
656 : : }
657 : :
658 : :
659 : : /* Create a file descriptor to read the data from the
660 : : elf_input_section given a file descriptor to an ELF file. */
661 : : static int
662 : 10 : open_input_section (int fd)
663 : : {
664 : 10 : size_t shnums;
665 : 10 : size_t cnt;
666 : 10 : size_t shstrndx;
667 : 10 : Elf *elf = elf_begin (fd, ELF_C_READ_MMAP, NULL);
668 [ - + ]: 10 : if (elf == NULL)
669 : : {
670 : 0 : error (0, 0, _("cannot generate Elf descriptor: %s"),
671 : : elf_errmsg (-1));
672 : 0 : return -1;
673 : : }
674 : :
675 [ - + ]: 10 : if (elf_getshdrnum (elf, &shnums) < 0)
676 : : {
677 : 0 : error (0, 0, _("cannot determine number of sections: %s"),
678 : : elf_errmsg (-1));
679 : 0 : open_error:
680 : 0 : elf_end (elf);
681 : 0 : return -1;
682 : : }
683 : :
684 [ - + ]: 10 : if (elf_getshdrstrndx (elf, &shstrndx) < 0)
685 : : {
686 : 0 : error (0, 0, _("cannot get section header string table index"));
687 : 0 : goto open_error;
688 : : }
689 : :
690 [ + - ]: 236 : for (cnt = 0; cnt < shnums; ++cnt)
691 : : {
692 : 236 : Elf_Scn *scn = elf_getscn (elf, cnt);
693 [ - + ]: 236 : if (scn == NULL)
694 : : {
695 : 0 : error (0, 0, _("cannot get section: %s"),
696 : : elf_errmsg (-1));
697 : 0 : goto open_error;
698 : : }
699 : :
700 : 236 : GElf_Shdr shdr_mem;
701 : 236 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
702 [ - + ]: 236 : if (unlikely (shdr == NULL))
703 : : {
704 : 0 : error (0, 0, _("cannot get section header: %s"),
705 : : elf_errmsg (-1));
706 : 0 : goto open_error;
707 : : }
708 : :
709 : 236 : const char *sname = elf_strptr (elf, shstrndx, shdr->sh_name);
710 [ - + ]: 236 : if (sname == NULL)
711 : : {
712 : 0 : error (0, 0, _("cannot get section name"));
713 : 0 : goto open_error;
714 : : }
715 : :
716 [ + + ]: 236 : if (strcmp (sname, elf_input_section) == 0)
717 : : {
718 : 10 : Elf_Data *data = elf_rawdata (scn, NULL);
719 [ - + ]: 10 : if (data == NULL)
720 : : {
721 : 0 : error (0, 0, _("cannot get %s content: %s"),
722 : : sname, elf_errmsg (-1));
723 : 0 : goto open_error;
724 : : }
725 : :
726 : : /* Create (and immediately unlink) a temporary file to store
727 : : section data in to create a file descriptor for it. */
728 [ + - ]: 10 : const char *tmpdir = getenv ("TMPDIR") ?: P_tmpdir;
729 : 10 : static const char suffix[] = "/readelfXXXXXX";
730 : 10 : int tmplen = strlen (tmpdir) + sizeof (suffix);
731 : 10 : char *tempname = alloca (tmplen);
732 : 10 : sprintf (tempname, "%s%s", tmpdir, suffix);
733 : :
734 : 10 : int sfd = mkstemp (tempname);
735 [ - + ]: 10 : if (sfd == -1)
736 : : {
737 : 0 : error (0, 0, _("cannot create temp file '%s'"),
738 : : tempname);
739 : 0 : goto open_error;
740 : : }
741 : 10 : unlink (tempname);
742 : :
743 : 10 : ssize_t size = data->d_size;
744 [ - + ]: 10 : if (write_retry (sfd, data->d_buf, size) != size)
745 : : {
746 : 0 : error (0, 0, _("cannot write section data"));
747 : 0 : goto open_error;
748 : : }
749 : :
750 [ - + ]: 10 : if (elf_end (elf) != 0)
751 : : {
752 : 0 : error (0, 0, _("error while closing Elf descriptor: %s"),
753 : : elf_errmsg (-1));
754 : 10 : return -1;
755 : : }
756 : :
757 [ - + ]: 10 : if (lseek (sfd, 0, SEEK_SET) == -1)
758 : : {
759 : 0 : error (0, 0, _("error while rewinding file descriptor"));
760 : 0 : return -1;
761 : : }
762 : :
763 : : return sfd;
764 : : }
765 : : }
766 : :
767 : : /* Named section not found. */
768 [ # # ]: 0 : if (elf_end (elf) != 0)
769 : 0 : error (0, 0, _("error while closing Elf descriptor: %s"),
770 : : elf_errmsg (-1));
771 : : return -1;
772 : : }
773 : :
774 : : /* Check if the file is an archive, and if so dump its index. */
775 : : static void
776 : 4 : check_archive_index (int fd, const char *fname, bool only_one)
777 : : {
778 : : /* Create an `Elf' descriptor. */
779 : 4 : Elf *elf = elf_begin (fd, ELF_C_READ_MMAP, NULL);
780 [ - + ]: 4 : if (elf == NULL)
781 : 0 : error (0, 0, _("cannot generate Elf descriptor: %s"),
782 : : elf_errmsg (-1));
783 : : else
784 : : {
785 [ + - ]: 4 : if (elf_kind (elf) == ELF_K_AR)
786 : : {
787 [ - + ]: 4 : if (!only_one)
788 : 0 : printf ("\n%s:\n\n", fname);
789 : 4 : dump_archive_index (elf, fname);
790 : : }
791 : : else
792 : 4 : error (0, 0,
793 : 0 : _("'%s' is not an archive, cannot print archive index"),
794 : : fname);
795 : :
796 : : /* Now we can close the descriptor. */
797 [ - + ]: 4 : if (elf_end (elf) != 0)
798 : 0 : error (0, 0, _("error while closing Elf descriptor: %s"),
799 : : elf_errmsg (-1));
800 : : }
801 : 4 : }
802 : :
803 : : /* Trivial callback used for checking if we opened an archive. */
804 : : static int
805 : 868 : count_dwflmod (Dwfl_Module *dwflmod __attribute__ ((unused)),
806 : : void **userdata __attribute__ ((unused)),
807 : : const char *name __attribute__ ((unused)),
808 : : Dwarf_Addr base __attribute__ ((unused)),
809 : : void *arg)
810 : : {
811 [ + - ]: 868 : if (*(bool *) arg)
812 : : return DWARF_CB_ABORT;
813 : 868 : *(bool *) arg = true;
814 : 868 : return DWARF_CB_OK;
815 : : }
816 : :
817 : : struct process_dwflmod_args
818 : : {
819 : : int fd;
820 : : bool only_one;
821 : : };
822 : :
823 : : static int
824 : 968 : process_dwflmod (Dwfl_Module *dwflmod,
825 : : void **userdata __attribute__ ((unused)),
826 : : const char *name __attribute__ ((unused)),
827 : : Dwarf_Addr base __attribute__ ((unused)),
828 : : void *arg)
829 : : {
830 : 968 : const struct process_dwflmod_args *a = arg;
831 : :
832 : : /* Print the file name. */
833 [ + + ]: 968 : if (!a->only_one)
834 : : {
835 : 100 : const char *fname;
836 : 100 : dwfl_module_info (dwflmod, NULL, NULL, NULL, NULL, NULL, &fname, NULL);
837 : :
838 : 100 : printf ("\n%s:\n\n", fname);
839 : : }
840 : :
841 : 968 : process_elf_file (dwflmod, a->fd);
842 : :
843 : 968 : return DWARF_CB_OK;
844 : : }
845 : :
846 : : /* Stub libdwfl callback, only the ELF handle already open is ever used.
847 : : Only used for finding the alternate debug file if the Dwarf comes from
848 : : the main file. We are not interested in separate debuginfo. */
849 : : static int
850 : 262 : find_no_debuginfo (Dwfl_Module *mod,
851 : : void **userdata,
852 : : const char *modname,
853 : : Dwarf_Addr base,
854 : : const char *file_name,
855 : : const char *debuglink_file,
856 : : GElf_Word debuglink_crc,
857 : : char **debuginfo_file_name)
858 : : {
859 : 262 : Dwarf_Addr dwbias;
860 : 262 : dwfl_module_info (mod, NULL, NULL, NULL, &dwbias, NULL, NULL, NULL);
861 : :
862 : : /* We are only interested if the Dwarf has been setup on the main
863 : : elf file but is only missing the alternate debug link. If dwbias
864 : : hasn't even been setup, this is searching for separate debuginfo
865 : : for the main elf. We don't care in that case. */
866 [ + + ]: 262 : if (dwbias == (Dwarf_Addr) -1)
867 : : return -1;
868 : :
869 : 70 : return dwfl_standard_find_debuginfo (mod, userdata, modname, base,
870 : : file_name, debuglink_file,
871 : : debuglink_crc, debuginfo_file_name);
872 : : }
873 : :
874 : : static Dwfl *
875 : 986 : create_dwfl (int fd, const char *fname)
876 : : {
877 : : /* Duplicate an fd for dwfl_report_offline to swallow. */
878 : 986 : int dwfl_fd = dup (fd);
879 [ - + ]: 986 : if (unlikely (dwfl_fd < 0))
880 : 0 : error_exit (errno, "dup");
881 : :
882 : : /* Use libdwfl in a trivial way to open the libdw handle for us.
883 : : This takes care of applying relocations to DWARF data in ET_REL files. */
884 : 986 : static const Dwfl_Callbacks callbacks =
885 : : {
886 : : .section_address = dwfl_offline_section_address,
887 : : .find_debuginfo = find_no_debuginfo
888 : : };
889 : 986 : Dwfl *dwfl = dwfl_begin (&callbacks);
890 [ + - ]: 986 : if (likely (dwfl != NULL))
891 : : /* Let 0 be the logical address of the file (or first in archive). */
892 : 986 : dwfl->offline_next_address = 0;
893 [ - + ]: 986 : if (dwfl_report_offline (dwfl, fname, fname, dwfl_fd) == NULL)
894 : : {
895 : 0 : struct stat st;
896 [ # # ]: 0 : if (fstat (dwfl_fd, &st) != 0)
897 : 0 : error (0, errno, _("cannot stat input file"));
898 [ # # ]: 0 : else if (unlikely (st.st_size == 0))
899 : 0 : error (0, 0, _("input file is empty"));
900 : : else
901 : 0 : error (0, 0, _("failed reading '%s': %s"),
902 : : fname, dwfl_errmsg (-1));
903 : 0 : close (dwfl_fd); /* Consumed on success, not on failure. */
904 : 0 : dwfl = NULL;
905 : : }
906 : : else
907 : 986 : dwfl_report_end (dwfl, NULL, NULL);
908 : :
909 : 986 : return dwfl;
910 : : }
911 : :
912 : : /* Process one input file. */
913 : : static void
914 : 972 : process_file (int fd, const char *fname, bool only_one)
915 : : {
916 [ + + ]: 972 : if (print_archive_index)
917 : 4 : check_archive_index (fd, fname, only_one);
918 : :
919 [ + + ]: 972 : if (!any_control_option)
920 : : return;
921 : :
922 [ + + ]: 968 : if (elf_input_section != NULL)
923 : : {
924 : : /* Replace fname and fd with section content. */
925 : 10 : char *fnname = alloca (strlen (fname) + strlen (elf_input_section) + 2);
926 : 10 : sprintf (fnname, "%s:%s", fname, elf_input_section);
927 : 10 : fd = open_input_section (fd);
928 [ - + ]: 10 : if (fd == -1)
929 : : {
930 : 0 : error (0, 0, _("No such section '%s' in '%s'"),
931 : : elf_input_section, fname);
932 : 0 : return;
933 : : }
934 : : fname = fnname;
935 : : }
936 : :
937 : 968 : Dwfl *dwfl = create_dwfl (fd, fname);
938 [ + - ]: 968 : if (dwfl != NULL)
939 : : {
940 [ + + ]: 968 : if (only_one)
941 : : {
942 : : /* Clear ONLY_ONE if we have multiple modules, from an archive. */
943 : 868 : bool seen = false;
944 : 868 : only_one = dwfl_getmodules (dwfl, &count_dwflmod, &seen, 0) == 0;
945 : : }
946 : :
947 : : /* Process the one or more modules gleaned from this file. */
948 : 968 : struct process_dwflmod_args a = { .fd = fd, .only_one = only_one };
949 : 968 : dwfl_getmodules (dwfl, &process_dwflmod, &a, 0);
950 : : }
951 : : /* Terrible hack for hooking unrelated skeleton/split compile units,
952 : : see __libdw_link_skel_split in print_debug. */
953 [ + - ]: 968 : if (! do_not_close_dwfl)
954 : 968 : dwfl_end (dwfl);
955 : :
956 : : /* Need to close the replaced fd if we created it. Caller takes
957 : : care of original. */
958 [ + + ]: 968 : if (elf_input_section != NULL)
959 : 10 : close (fd);
960 : : }
961 : :
962 : : /* Check whether there are any compressed sections in the ELF file. */
963 : : static bool
964 : 492 : elf_contains_chdrs (Elf *elf)
965 : : {
966 : 492 : Elf_Scn *scn = NULL;
967 [ + + ]: 797906 : while ((scn = elf_nextscn (elf, scn)) != NULL)
968 : : {
969 : 797528 : GElf_Shdr shdr_mem;
970 : 797528 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
971 [ + - + + ]: 797528 : if (shdr != NULL && (shdr->sh_flags & SHF_COMPRESSED) != 0)
972 : 114 : return true;
973 : : }
974 : : return false;
975 : : }
976 : :
977 : : /* Process one ELF file. */
978 : : static void
979 : 968 : process_elf_file (Dwfl_Module *dwflmod, int fd)
980 : : {
981 : 968 : GElf_Addr dwflbias;
982 : 968 : Elf *elf = dwfl_module_getelf (dwflmod, &dwflbias);
983 : :
984 : 968 : GElf_Ehdr ehdr_mem;
985 : 968 : GElf_Ehdr *ehdr = gelf_getehdr (elf, &ehdr_mem);
986 : :
987 [ - + ]: 968 : if (ehdr == NULL)
988 : : {
989 : 0 : error (0, 0, _("cannot read ELF header: %s"), elf_errmsg (-1));
990 : 0 : return;
991 : : }
992 : :
993 : 968 : Ebl *ebl = ebl_openbackend (elf);
994 [ - + ]: 968 : if (unlikely (ebl == NULL))
995 : : {
996 : 0 : ebl_error:
997 : 0 : error (0, errno, _("cannot create EBL handle"));
998 : 0 : return;
999 : : }
1000 : :
1001 : : /* Determine the number of sections. */
1002 [ - + ]: 968 : if (unlikely (elf_getshdrnum (ebl->elf, &shnum) < 0))
1003 : 0 : error_exit (0, _("cannot determine number of sections: %s"),
1004 : : elf_errmsg (-1));
1005 : :
1006 : : /* Determine the number of phdrs. */
1007 [ - + ]: 968 : if (unlikely (elf_getphdrnum (ebl->elf, &phnum) < 0))
1008 : 0 : error_exit (0, _("cannot determine number of program headers: %s"),
1009 : : elf_errmsg (-1));
1010 : :
1011 : : /* For an ET_REL file, libdwfl has adjusted the in-core shdrs and
1012 : : may have applied relocation to some sections. If there are any
1013 : : compressed sections, any pass (or libdw/libdwfl) might have
1014 : : uncompressed them. So we need to get a fresh Elf handle on the
1015 : : file to display those. */
1016 : 2132 : bool print_unchanged = ((print_section_header
1017 [ + + ]: 462 : || print_relocations
1018 [ + + ]: 452 : || dump_data_sections != NULL
1019 [ + + ]: 440 : || print_notes)
1020 [ + + + + ]: 1036 : && (ehdr->e_type == ET_REL
1021 [ + + ]: 492 : || elf_contains_chdrs (ebl->elf)));
1022 : :
1023 : 196 : Elf *pure_elf = NULL;
1024 : 196 : Ebl *pure_ebl = ebl;
1025 : 196 : if (print_unchanged)
1026 : : {
1027 : : /* Read the file afresh. */
1028 : 196 : off_t aroff = elf_getaroff (elf);
1029 : 196 : pure_elf = dwelf_elf_begin (fd);
1030 [ - + ]: 196 : if (aroff > 0)
1031 : : {
1032 : : /* Archive member. */
1033 : 0 : (void) elf_rand (pure_elf, aroff);
1034 : 0 : Elf *armem = elf_begin (-1, ELF_C_READ_MMAP, pure_elf);
1035 : 0 : elf_end (pure_elf);
1036 : 0 : pure_elf = armem;
1037 : : }
1038 [ - + ]: 196 : if (pure_elf == NULL)
1039 : : {
1040 : 0 : error (0, 0, _("cannot read ELF: %s"), elf_errmsg (-1));
1041 : 0 : return;
1042 : : }
1043 : 196 : pure_ebl = ebl_openbackend (pure_elf);
1044 [ - + ]: 196 : if (pure_ebl == NULL)
1045 : 0 : goto ebl_error;
1046 : : }
1047 : :
1048 : 968 : bool symtab_printed = false;
1049 : :
1050 [ + + ]: 968 : if (print_file_header)
1051 : 190 : print_ehdr (ebl, ehdr);
1052 [ + + ]: 968 : if (print_section_header)
1053 : 506 : print_shdr (pure_ebl, ehdr);
1054 [ + + ]: 968 : if (print_program_header)
1055 : 188 : print_phdr (ebl, ehdr);
1056 [ + + ]: 968 : if (print_section_groups)
1057 : 202 : print_scngrp (ebl);
1058 [ + + ]: 968 : if (print_dynamic_table)
1059 : 190 : print_dynamic (ebl);
1060 [ + + ]: 968 : if (print_relocations)
1061 : 194 : print_relocs (pure_ebl, dwflmod, ehdr);
1062 [ + + ]: 968 : if (print_histogram)
1063 : 184 : handle_hash (ebl);
1064 [ + + + + ]: 968 : if (print_symbol_table || print_dynsym_table)
1065 : 220 : symtab_printed |= print_symtab (ebl, SHT_DYNSYM);
1066 [ + + ]: 968 : if (print_version_info)
1067 : 184 : print_verinfo (ebl);
1068 [ + + + - ]: 968 : if (print_symbol_table && !use_dynamic_segment)
1069 : 218 : symtab_printed |= print_symtab (ebl, SHT_SYMTAB);
1070 : :
1071 [ + + + + ]: 968 : if ((print_symbol_table || print_dynsym_table)
1072 [ + + + + ]: 220 : && !symtab_printed && symbol_table_section != NULL)
1073 : 2 : printf ("WARNING: %s: '%s'\n", _("cannot find section"),
1074 : : symbol_table_section);
1075 : :
1076 [ + + ]: 968 : if (print_arch)
1077 : 192 : print_liblist (ebl);
1078 [ + + ]: 968 : if (print_arch)
1079 : 192 : print_attributes (ebl, ehdr);
1080 [ + + ]: 968 : if (dump_data_sections != NULL)
1081 : 12 : dump_data (pure_ebl);
1082 [ + + ]: 968 : if (string_sections != NULL)
1083 : 186 : dump_strings (ebl);
1084 [ + + ]: 968 : if ((print_debug_sections | implicit_debug_sections) != 0)
1085 : 502 : print_debug (dwflmod, ebl, ehdr);
1086 [ + + ]: 968 : if (print_notes)
1087 : 230 : handle_notes (pure_ebl, ehdr);
1088 [ - + ]: 968 : if (print_string_sections)
1089 : 0 : print_strings (ebl);
1090 : :
1091 [ + + ]: 968 : if (pure_ebl != ebl)
1092 : : {
1093 : 196 : ebl_closebackend (ebl);
1094 : 196 : ebl_closebackend (pure_ebl);
1095 : 196 : elf_end (pure_elf);
1096 : : }
1097 : : else
1098 : 772 : ebl_closebackend (ebl);
1099 : : }
1100 : :
1101 : :
1102 : : /* Print file type. */
1103 : : static void
1104 : 190 : print_file_type (unsigned short int e_type)
1105 : : {
1106 [ + - ]: 190 : if (likely (e_type <= ET_CORE))
1107 : : {
1108 : 190 : static const char *const knowntypes[] =
1109 : : {
1110 : : N_("NONE (None)"),
1111 : : N_("REL (Relocatable file)"),
1112 : : N_("EXEC (Executable file)"),
1113 : : N_("DYN (Shared object file)"),
1114 : : N_("CORE (Core file)")
1115 : : };
1116 : 190 : puts (_(knowntypes[e_type]));
1117 : : }
1118 [ # # ]: 0 : else if (e_type >= ET_LOOS && e_type <= ET_HIOS)
1119 : 0 : printf (_("OS Specific: (%x)\n"), e_type);
1120 [ # # ]: 0 : else if (e_type >= ET_LOPROC /* && e_type <= ET_HIPROC always true */)
1121 : 0 : printf (_("Processor Specific: (%x)\n"), e_type);
1122 : : else
1123 : 0 : puts ("???");
1124 : 190 : }
1125 : :
1126 : :
1127 : : /* Print ELF header. */
1128 : : static void
1129 : 190 : print_ehdr (Ebl *ebl, GElf_Ehdr *ehdr)
1130 : : {
1131 : 190 : fputs (_("ELF Header:\n Magic: "), stdout);
1132 [ + + ]: 3230 : for (size_t cnt = 0; cnt < EI_NIDENT; ++cnt)
1133 : 3040 : printf (" %02hhx", ehdr->e_ident[cnt]);
1134 : :
1135 : 190 : printf (_("\n Class: %s\n"),
1136 [ + + ]: 190 : ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? "ELF32"
1137 : : : ehdr->e_ident[EI_CLASS] == ELFCLASS64 ? "ELF64"
1138 [ - + ]: 156 : : "\?\?\?");
1139 : :
1140 : 190 : printf (_(" Data: %s\n"),
1141 [ - + ]: 190 : ehdr->e_ident[EI_DATA] == ELFDATA2LSB
1142 : : ? "2's complement, little endian"
1143 : : : ehdr->e_ident[EI_DATA] == ELFDATA2MSB
1144 [ # # ]: 0 : ? "2's complement, big endian" : "\?\?\?");
1145 : :
1146 : 570 : printf (_(" Ident Version: %hhd %s\n"),
1147 : 190 : ehdr->e_ident[EI_VERSION],
1148 [ + - ]: 190 : ehdr->e_ident[EI_VERSION] == EV_CURRENT ? _("(current)")
1149 : : : "(\?\?\?)");
1150 : :
1151 : 190 : char buf[512];
1152 : 190 : printf (_(" OS/ABI: %s\n"),
1153 : 190 : ebl_osabi_name (ebl, ehdr->e_ident[EI_OSABI], buf, sizeof (buf)));
1154 : :
1155 : 380 : printf (_(" ABI Version: %hhd\n"),
1156 : 190 : ehdr->e_ident[EI_ABIVERSION]);
1157 : :
1158 : 190 : fputs (_(" Type: "), stdout);
1159 : 190 : print_file_type (ehdr->e_type);
1160 : :
1161 : 190 : const char *machine = dwelf_elf_e_machine_string (ehdr->e_machine);
1162 [ + - ]: 190 : if (machine != NULL)
1163 : 190 : printf (_(" Machine: %s\n"), machine);
1164 : : else
1165 : 190 : printf (_(" Machine: <unknown>: 0x%x\n"),
1166 : 0 : ehdr->e_machine);
1167 : :
1168 : 190 : printf (_(" Version: %d %s\n"),
1169 : : ehdr->e_version,
1170 [ + - ]: 190 : ehdr->e_version == EV_CURRENT ? _("(current)") : "(\?\?\?)");
1171 : :
1172 : 190 : printf (_(" Entry point address: %#" PRIx64 "\n"),
1173 : : ehdr->e_entry);
1174 : :
1175 : 190 : printf (_(" Start of program headers: %" PRId64 " %s\n"),
1176 : : ehdr->e_phoff, _("(bytes into file)"));
1177 : :
1178 : 190 : printf (_(" Start of section headers: %" PRId64 " %s\n"),
1179 : : ehdr->e_shoff, _("(bytes into file)"));
1180 : :
1181 : 190 : printf (_(" Flags: %s\n"),
1182 : : ebl_machine_flag_name (ebl, ehdr->e_flags, buf, sizeof (buf)));
1183 : :
1184 : 380 : printf (_(" Size of this header: %" PRId16 " %s\n"),
1185 : 190 : ehdr->e_ehsize, _("(bytes)"));
1186 : :
1187 : 380 : printf (_(" Size of program header entries: %" PRId16 " %s\n"),
1188 : 190 : ehdr->e_phentsize, _("(bytes)"));
1189 : :
1190 : 380 : printf (_(" Number of program headers entries: %" PRId16),
1191 : 190 : ehdr->e_phnum);
1192 [ + + ]: 190 : if (ehdr->e_phnum == PN_XNUM)
1193 : : {
1194 : 2 : GElf_Shdr shdr_mem;
1195 : 2 : GElf_Shdr *shdr = gelf_getshdr (elf_getscn (ebl->elf, 0), &shdr_mem);
1196 [ + - ]: 2 : if (shdr != NULL)
1197 : 2 : printf (_(" (%" PRIu32 " in [0].sh_info)"),
1198 : 2 : (uint32_t) shdr->sh_info);
1199 : : else
1200 : 0 : fputs (_(" ([0] not available)"), stdout);
1201 : : }
1202 : 190 : fputc ('\n', stdout);
1203 : :
1204 : 380 : printf (_(" Size of section header entries: %" PRId16 " %s\n"),
1205 : 190 : ehdr->e_shentsize, _("(bytes)"));
1206 : :
1207 : 380 : printf (_(" Number of section headers entries: %" PRId16),
1208 : 190 : ehdr->e_shnum);
1209 [ - + ]: 190 : if (ehdr->e_shnum == 0)
1210 : : {
1211 : 0 : GElf_Shdr shdr_mem;
1212 : 0 : GElf_Shdr *shdr = gelf_getshdr (elf_getscn (ebl->elf, 0), &shdr_mem);
1213 [ # # ]: 0 : if (shdr != NULL)
1214 : 0 : printf (_(" (%" PRIu32 " in [0].sh_size)"),
1215 : 0 : (uint32_t) shdr->sh_size);
1216 : : else
1217 : 0 : fputs (_(" ([0] not available)"), stdout);
1218 : : }
1219 : 190 : fputc ('\n', stdout);
1220 : :
1221 [ - + ]: 190 : if (unlikely (ehdr->e_shstrndx == SHN_XINDEX))
1222 : : {
1223 : 0 : GElf_Shdr shdr_mem;
1224 : 0 : GElf_Shdr *shdr = gelf_getshdr (elf_getscn (ebl->elf, 0), &shdr_mem);
1225 [ # # ]: 0 : if (shdr != NULL)
1226 : : /* We managed to get the zeroth section. */
1227 : 0 : snprintf (buf, sizeof (buf), _(" (%" PRIu32 " in [0].sh_link)"),
1228 : 0 : (uint32_t) shdr->sh_link);
1229 : : else
1230 : : {
1231 : 0 : strncpy (buf, _(" ([0] not available)"), sizeof (buf) - 1);
1232 : 0 : buf[sizeof (buf) - 1] = '\0';
1233 : : }
1234 : :
1235 : 0 : printf (_(" Section header string table index: XINDEX%s\n\n"),
1236 : : buf);
1237 : : }
1238 : : else
1239 : 190 : printf (_(" Section header string table index: %" PRId16 "\n\n"),
1240 : : ehdr->e_shstrndx);
1241 : 190 : }
1242 : :
1243 : :
1244 : : static const char *
1245 : 78800 : get_visibility_type (int value)
1246 : : {
1247 [ - + - - : 78800 : switch (value)
+ ]
1248 : : {
1249 : : case STV_DEFAULT:
1250 : : return "DEFAULT";
1251 : 0 : case STV_INTERNAL:
1252 : 0 : return "INTERNAL";
1253 : 984 : case STV_HIDDEN:
1254 : 984 : return "HIDDEN";
1255 : 0 : case STV_PROTECTED:
1256 : 0 : return "PROTECTED";
1257 : 0 : default:
1258 : 0 : return "???";
1259 : : }
1260 : : }
1261 : :
1262 : : static const char *
1263 : 384 : elf_ch_type_name (unsigned int code)
1264 : : {
1265 : 384 : switch (code)
1266 : : {
1267 : : case 0:
1268 : : return "NONE";
1269 : 24 : case ELFCOMPRESS_ZLIB:
1270 : 24 : return "ZLIB";
1271 : 360 : case ELFCOMPRESS_ZSTD:
1272 : 360 : return "ZSTD";
1273 : 0 : default:
1274 : 0 : return "UNKNOWN";
1275 : : }
1276 : : }
1277 : :
1278 : : /* Print the section headers. */
1279 : : static void
1280 : 506 : print_shdr (Ebl *ebl, GElf_Ehdr *ehdr)
1281 : : {
1282 : 506 : size_t cnt;
1283 : 506 : size_t shstrndx;
1284 : :
1285 [ + + ]: 506 : if (! print_file_header)
1286 : : {
1287 : 322 : size_t sections;
1288 [ - + ]: 322 : if (unlikely (elf_getshdrnum (ebl->elf, §ions) < 0))
1289 : 0 : error_exit (0, _("cannot get number of sections: %s"),
1290 : : elf_errmsg (-1));
1291 : :
1292 : 322 : printf (_("\
1293 : : There are %zd section headers, starting at offset %#" PRIx64 ":\n\
1294 : : \n"),
1295 : : sections, ehdr->e_shoff);
1296 : : }
1297 : :
1298 : : /* Get the section header string table index. */
1299 [ - + ]: 506 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
1300 : 0 : error_exit (0, _("cannot get section header string table index: %s"),
1301 : : elf_errmsg (-1));
1302 : :
1303 : 506 : puts (_("Section Headers:"));
1304 : :
1305 [ + + ]: 506 : if (ehdr->e_ident[EI_CLASS] == ELFCLASS32)
1306 : 196 : puts (_("[Nr] Name Type Addr Off Size ES Flags Lk Inf Al"));
1307 : : else
1308 : 310 : puts (_("[Nr] Name Type Addr Off Size ES Flags Lk Inf Al"));
1309 : :
1310 [ + + ]: 506 : if (print_decompress)
1311 : : {
1312 [ + + ]: 112 : if (ehdr->e_ident[EI_CLASS] == ELFCLASS32)
1313 : 56 : puts (_(" [Compression Size Al]"));
1314 : : else
1315 : 56 : puts (_(" [Compression Size Al]"));
1316 : : }
1317 : :
1318 [ + + ]: 1586316 : for (cnt = 0; cnt < shnum; ++cnt)
1319 : : {
1320 : 1585810 : Elf_Scn *scn = elf_getscn (ebl->elf, cnt);
1321 : :
1322 [ - + ]: 1585810 : if (unlikely (scn == NULL))
1323 : 0 : error_exit (0, _("cannot get section: %s"),
1324 : : elf_errmsg (-1));
1325 : :
1326 : : /* Get the section header. */
1327 : 1585810 : GElf_Shdr shdr_mem;
1328 : 1585810 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
1329 [ - + ]: 1585810 : if (unlikely (shdr == NULL))
1330 : 0 : error_exit (0, _("cannot get section header: %s"),
1331 : : elf_errmsg (-1));
1332 : :
1333 : 1585810 : char flagbuf[20];
1334 : 1585810 : char *cp = flagbuf;
1335 [ + + ]: 1585810 : if (shdr->sh_flags & SHF_WRITE)
1336 : 2764 : *cp++ = 'W';
1337 [ + + ]: 1585810 : if (shdr->sh_flags & SHF_ALLOC)
1338 : 8520 : *cp++ = 'A';
1339 [ + + ]: 1585810 : if (shdr->sh_flags & SHF_EXECINSTR)
1340 : 1564 : *cp++ = 'X';
1341 [ + + ]: 1585810 : if (shdr->sh_flags & SHF_MERGE)
1342 : 420 : *cp++ = 'M';
1343 [ + + ]: 1585810 : if (shdr->sh_flags & SHF_STRINGS)
1344 : 380 : *cp++ = 'S';
1345 [ + + ]: 1585810 : if (shdr->sh_flags & SHF_INFO_LINK)
1346 : 196 : *cp++ = 'I';
1347 [ + + ]: 1585810 : if (shdr->sh_flags & SHF_LINK_ORDER)
1348 : 4 : *cp++ = 'L';
1349 [ - + ]: 1585810 : if (shdr->sh_flags & SHF_OS_NONCONFORMING)
1350 : 0 : *cp++ = 'N';
1351 [ - + ]: 1585810 : if (shdr->sh_flags & SHF_GROUP)
1352 : 0 : *cp++ = 'G';
1353 [ + + ]: 1585810 : if (shdr->sh_flags & SHF_TLS)
1354 : 30 : *cp++ = 'T';
1355 [ + + ]: 1585810 : if (shdr->sh_flags & SHF_COMPRESSED)
1356 : 414 : *cp++ = 'C';
1357 [ - + ]: 1585810 : if (shdr->sh_flags & SHF_ORDERED)
1358 : 0 : *cp++ = 'O';
1359 [ - + ]: 1585810 : if (shdr->sh_flags & SHF_EXCLUDE)
1360 : 0 : *cp++ = 'E';
1361 [ + + ]: 1585810 : if (shdr->sh_flags & SHF_GNU_RETAIN)
1362 : 2 : *cp++ = 'R';
1363 : 1585810 : *cp = '\0';
1364 : :
1365 : 1585810 : const char *sname;
1366 : 1585810 : char buf[128];
1367 [ - + ]: 1585810 : sname = elf_strptr (ebl->elf, shstrndx, shdr->sh_name) ?: "<corrupt>";
1368 [ + + ]: 3171620 : printf ("[%2zu] %-20s %-12s %0*" PRIx64 " %0*" PRIx64 " %0*" PRIx64
1369 : : " %2" PRId64 " %-5s %2" PRId32 " %3" PRId32
1370 : : " %2" PRId64 "\n",
1371 : : cnt, sname,
1372 : 1585810 : ebl_section_type_name (ebl, shdr->sh_type, buf, sizeof (buf)),
1373 : : ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 8 : 16, shdr->sh_addr,
1374 : : ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 6 : 8, shdr->sh_offset,
1375 [ + + ]: 1585810 : ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 6 : 8, shdr->sh_size,
1376 : : shdr->sh_entsize, flagbuf, shdr->sh_link, shdr->sh_info,
1377 : : shdr->sh_addralign);
1378 : :
1379 [ + + ]: 1585810 : if (print_decompress)
1380 : : {
1381 [ + + ]: 1880 : if ((shdr->sh_flags & SHF_COMPRESSED) != 0)
1382 : : {
1383 : 384 : GElf_Chdr chdr;
1384 [ + - ]: 384 : if (gelf_getchdr (scn, &chdr) != NULL)
1385 [ + + - - ]: 768 : printf (" [ELF %s (%" PRId32 ") %0*" PRIx64
1386 : : " %2" PRId64 "]\n",
1387 : : elf_ch_type_name (chdr.ch_type),
1388 : : chdr.ch_type,
1389 [ + + ]: 384 : ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 6 : 8,
1390 : : chdr.ch_size, chdr.ch_addralign);
1391 : : else
1392 : 384 : error (0, 0,
1393 : 0 : _("bad compression header for section %zd: %s"),
1394 : : elf_ndxscn (scn), elf_errmsg (-1));
1395 : : }
1396 [ + + ]: 1496 : else if (startswith (sname, ".zdebug"))
1397 : : {
1398 : 24 : ssize_t size;
1399 [ + - ]: 24 : if ((size = dwelf_scn_gnu_compressed_size (scn)) >= 0)
1400 : 1585834 : printf (" [GNU ZLIB %0*zx ]\n",
1401 [ + + ]: 24 : ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 6 : 8, size);
1402 : : else
1403 : 1585810 : error (0, 0,
1404 : 0 : _("bad gnu compressed size for section %zd: %s"),
1405 : : elf_ndxscn (scn), elf_errmsg (-1));
1406 : : }
1407 : : }
1408 : : }
1409 : :
1410 : 506 : print_flag_info();
1411 : 506 : fputc ('\n', stdout);
1412 : 506 : }
1413 : :
1414 : : /* Print flag information. */
1415 : : static void
1416 : 506 : print_flag_info (void)
1417 : : {
1418 : 506 : puts ("Key to Flags:");
1419 : 506 : puts (" W (write), A (alloc), X (execute), M (merge), S (strings), I (info),");
1420 : 506 : puts (" L (link order), N (extra OS processing required), G (group), T (TLS),");
1421 : 506 : puts (" C (compressed), O (ordered), R (GNU retain), E (exclude)");
1422 : 506 : }
1423 : :
1424 : : /* Print the program header. */
1425 : : static void
1426 : 188 : print_phdr (Ebl *ebl, GElf_Ehdr *ehdr)
1427 : : {
1428 [ + + ]: 188 : if (phnum == 0)
1429 : : /* No program header, this is OK in relocatable objects. */
1430 : 12 : return;
1431 : :
1432 : 176 : puts (_("Program Headers:"));
1433 [ + + ]: 176 : if (ehdr->e_ident[EI_CLASS] == ELFCLASS32)
1434 : 26 : puts (_("\
1435 : : Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align"));
1436 : : else
1437 : 150 : puts (_("\
1438 : : Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align"));
1439 : :
1440 : : /* Process all program headers. */
1441 : : bool has_relro = false;
1442 : : GElf_Addr relro_from = 0;
1443 : : GElf_Addr relro_to = 0;
1444 [ + + ]: 2032 : for (size_t cnt = 0; cnt < phnum; ++cnt)
1445 : : {
1446 : 1856 : char buf[128];
1447 : 1856 : GElf_Phdr mem;
1448 : 1856 : GElf_Phdr *phdr = gelf_getphdr (ebl->elf, cnt, &mem);
1449 : :
1450 : : /* If for some reason the header cannot be returned show this. */
1451 [ - + ]: 1856 : if (unlikely (phdr == NULL))
1452 : : {
1453 : 0 : puts (" ???");
1454 : 0 : continue;
1455 : : }
1456 : :
1457 : 1856 : printf (" %-14s 0x%06" PRIx64 " 0x%0*" PRIx64 " 0x%0*" PRIx64
1458 : : " 0x%06" PRIx64 " 0x%06" PRIx64 " %c%c%c 0x%" PRIx64 "\n",
1459 : 1856 : ebl_segment_type_name (ebl, phdr->p_type, buf, sizeof (buf)),
1460 : : phdr->p_offset,
1461 : : ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 8 : 16, phdr->p_vaddr,
1462 [ + + ]: 1856 : ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 8 : 16, phdr->p_paddr,
1463 : : phdr->p_filesz,
1464 : : phdr->p_memsz,
1465 [ - + ]: 1856 : phdr->p_flags & PF_R ? 'R' : ' ',
1466 [ + + ]: 1856 : phdr->p_flags & PF_W ? 'W' : ' ',
1467 [ + + ]: 1856 : phdr->p_flags & PF_X ? 'E' : ' ',
1468 : : phdr->p_align);
1469 : :
1470 [ + + ]: 1856 : if (phdr->p_type == PT_INTERP)
1471 : : {
1472 : : /* If we are sure the file offset is valid then we can show
1473 : : the user the name of the interpreter. We check whether
1474 : : there is a section at the file offset. Normally there
1475 : : would be a section called ".interp". But in separate
1476 : : .debug files it is a NOBITS section (and so doesn't match
1477 : : with gelf_offscn). Which probably means the offset is
1478 : : not valid another reason could be because the ELF file
1479 : : just doesn't contain any section headers, in that case
1480 : : just play it safe and don't display anything. */
1481 : :
1482 : 172 : Elf_Scn *scn = gelf_offscn (ebl->elf, phdr->p_offset);
1483 : 172 : GElf_Shdr shdr_mem;
1484 : 172 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
1485 : :
1486 : 172 : size_t maxsize;
1487 : 172 : char *filedata = elf_rawfile (ebl->elf, &maxsize);
1488 : :
1489 [ + - + + ]: 172 : if (shdr != NULL && shdr->sh_type == SHT_PROGBITS
1490 [ + - + - ]: 130 : && filedata != NULL && phdr->p_offset < maxsize
1491 [ + - ]: 130 : && phdr->p_filesz <= maxsize - phdr->p_offset
1492 [ + - ]: 130 : && memchr (filedata + phdr->p_offset, '\0',
1493 : : phdr->p_filesz) != NULL)
1494 : 172 : printf (_("\t[Requesting program interpreter: %s]\n"),
1495 : : filedata + phdr->p_offset);
1496 : : }
1497 [ + + ]: 1684 : else if (phdr->p_type == PT_GNU_RELRO)
1498 : : {
1499 : 152 : has_relro = true;
1500 : 152 : relro_from = phdr->p_vaddr;
1501 : 152 : relro_to = relro_from + phdr->p_memsz;
1502 : : }
1503 : : }
1504 : :
1505 : 176 : size_t sections;
1506 [ - + ]: 176 : if (unlikely (elf_getshdrnum (ebl->elf, §ions) < 0))
1507 : 0 : error_exit (0, _("cannot get number of sections: %s"),
1508 : : elf_errmsg (-1));
1509 : :
1510 [ + - ]: 176 : if (sections == 0)
1511 : : /* No sections in the file. Punt. */
1512 : : return;
1513 : :
1514 : : /* Get the section header string table index. */
1515 : 176 : size_t shstrndx;
1516 [ - + ]: 176 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
1517 : 0 : error_exit (0, _("cannot get section header string table index"));
1518 : :
1519 : 176 : puts (_("\n Section to Segment mapping:\n Segment Sections..."));
1520 : :
1521 [ + + ]: 2032 : for (size_t cnt = 0; cnt < phnum; ++cnt)
1522 : : {
1523 : : /* Print the segment number. */
1524 : 1856 : printf (" %2.2zu ", cnt);
1525 : :
1526 : 1856 : GElf_Phdr phdr_mem;
1527 : 1856 : GElf_Phdr *phdr = gelf_getphdr (ebl->elf, cnt, &phdr_mem);
1528 : : /* This must not happen. */
1529 [ - + ]: 1856 : if (unlikely (phdr == NULL))
1530 : 0 : error_exit (0, _("cannot get program header: %s"),
1531 : : elf_errmsg (-1));
1532 : :
1533 : : /* Iterate over the sections. */
1534 : : bool in_relro = false;
1535 : : bool in_ro = false;
1536 [ + + ]: 60712 : for (size_t inner = 1; inner < shnum; ++inner)
1537 : : {
1538 : 58856 : Elf_Scn *scn = elf_getscn (ebl->elf, inner);
1539 : : /* This should not happen. */
1540 [ - + ]: 58856 : if (unlikely (scn == NULL))
1541 : 0 : error_exit (0, _("cannot get section: %s"),
1542 : : elf_errmsg (-1));
1543 : :
1544 : : /* Get the section header. */
1545 : 58856 : GElf_Shdr shdr_mem;
1546 : 58856 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
1547 [ - + ]: 58856 : if (unlikely (shdr == NULL))
1548 : 0 : error_exit (0, _("cannot get section header: %s"),
1549 : : elf_errmsg (-1));
1550 : :
1551 [ + - ]: 58856 : if (shdr->sh_size > 0
1552 : : /* Compare allocated sections by VMA, unallocated
1553 : : sections by file offset. */
1554 [ + + ]: 58856 : && (shdr->sh_flags & SHF_ALLOC
1555 : 46478 : ? (shdr->sh_addr >= phdr->p_vaddr
1556 [ + + ]: 46478 : && (shdr->sh_addr + shdr->sh_size
1557 [ + + ]: 31442 : <= phdr->p_vaddr + phdr->p_memsz))
1558 : 12378 : : (shdr->sh_offset >= phdr->p_offset
1559 [ + + ]: 12378 : && (shdr->sh_offset + shdr->sh_size
1560 [ + + ]: 10852 : <= phdr->p_offset + phdr->p_filesz))))
1561 : : {
1562 [ + + ]: 6262 : if (has_relro && !in_relro
1563 [ + + ]: 4178 : && shdr->sh_addr >= relro_from
1564 [ + + ]: 732 : && shdr->sh_addr + shdr->sh_size <= relro_to)
1565 : : {
1566 : 474 : fputs (" [RELRO:", stdout);
1567 : 474 : in_relro = true;
1568 : : }
1569 [ + + + + ]: 5788 : else if (has_relro && in_relro && shdr->sh_addr >= relro_to)
1570 : : {
1571 : 134 : fputs ("]", stdout);
1572 : 134 : in_relro = false;
1573 : : }
1574 [ + + ]: 5654 : else if (has_relro && in_relro
1575 [ + + ]: 1166 : && shdr->sh_addr + shdr->sh_size > relro_to)
1576 : 18 : fputs ("] <RELRO:", stdout);
1577 [ + + ]: 5636 : else if (phdr->p_type == PT_LOAD && (phdr->p_flags & PF_W) == 0)
1578 : : {
1579 [ + + ]: 3226 : if (!in_ro)
1580 : : {
1581 : 372 : fputs (" [RO:", stdout);
1582 : 372 : in_ro = true;
1583 : : }
1584 : : }
1585 : : else
1586 : : {
1587 : : /* Determine the segment this section is part of. */
1588 : : size_t cnt2;
1589 : : GElf_Phdr phdr2_mem;
1590 : : GElf_Phdr *phdr2 = NULL;
1591 [ + - ]: 10776 : for (cnt2 = 0; cnt2 < phnum; ++cnt2)
1592 : : {
1593 : 10776 : phdr2 = gelf_getphdr (ebl->elf, cnt2, &phdr2_mem);
1594 : :
1595 [ + - + + ]: 10776 : if (phdr2 != NULL && phdr2->p_type == PT_LOAD
1596 [ + - ]: 6068 : && shdr->sh_addr >= phdr2->p_vaddr
1597 : 6068 : && (shdr->sh_addr + shdr->sh_size
1598 [ + + ]: 6068 : <= phdr2->p_vaddr + phdr2->p_memsz))
1599 : : break;
1600 : : }
1601 : :
1602 [ + - ]: 2410 : if (cnt2 < phnum)
1603 : : {
1604 [ + + + + ]: 2410 : if ((phdr2->p_flags & PF_W) == 0 && !in_ro)
1605 : : {
1606 : 614 : fputs (" [RO:", stdout);
1607 : 614 : in_ro = true;
1608 : : }
1609 [ + + + - ]: 1796 : else if ((phdr2->p_flags & PF_W) != 0 && in_ro)
1610 : : {
1611 : 0 : fputs ("]", stdout);
1612 : 0 : in_ro = false;
1613 : : }
1614 : : }
1615 : : }
1616 : :
1617 : 6262 : printf (" %s",
1618 : 6262 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name));
1619 : :
1620 : : /* Signal that this section is only partially covered. */
1621 [ + + ]: 6262 : if (has_relro && in_relro
1622 [ + + ]: 1640 : && shdr->sh_addr + shdr->sh_size > relro_to)
1623 : : {
1624 : 18 : fputs (">", stdout);
1625 : 18 : in_relro = false;
1626 : : }
1627 : : }
1628 : : }
1629 [ + + ]: 1856 : if (in_relro || in_ro)
1630 : 1308 : fputs ("]", stdout);
1631 : :
1632 : : /* Finish the line. */
1633 : 1856 : fputc ('\n', stdout);
1634 : : }
1635 : : }
1636 : :
1637 : :
1638 : : static const char *
1639 : 1252 : section_name (Ebl *ebl, GElf_Shdr *shdr)
1640 : : {
1641 : 1252 : size_t shstrndx;
1642 [ + - - + ]: 1252 : if (shdr == NULL || elf_getshdrstrndx (ebl->elf, &shstrndx) < 0)
1643 : 0 : return "???";
1644 [ - + ]: 1252 : return elf_strptr (ebl->elf, shstrndx, shdr->sh_name) ?: "???";
1645 : : }
1646 : :
1647 : :
1648 : : static void
1649 : 32 : handle_scngrp (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr)
1650 : : {
1651 : : /* Get the data of the section. */
1652 : 32 : Elf_Data *data = elf_getdata (scn, NULL);
1653 : :
1654 : 32 : Elf_Scn *symscn = elf_getscn (ebl->elf, shdr->sh_link);
1655 : 32 : GElf_Shdr symshdr_mem;
1656 : 32 : GElf_Shdr *symshdr = gelf_getshdr (symscn, &symshdr_mem);
1657 : 32 : Elf_Data *symdata = elf_getdata (symscn, NULL);
1658 : :
1659 [ + - + - ]: 32 : if (data == NULL || data->d_size < sizeof (Elf32_Word) || symshdr == NULL
1660 [ - + ]: 32 : || symdata == NULL)
1661 : 0 : return;
1662 : :
1663 : : /* Get the section header string table index. */
1664 : 32 : size_t shstrndx;
1665 [ - + ]: 32 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
1666 : 0 : error_exit (0, _("cannot get section header string table index"));
1667 : :
1668 : 32 : Elf32_Word *grpref = (Elf32_Word *) data->d_buf;
1669 : :
1670 : 32 : GElf_Sym sym_mem;
1671 : 32 : GElf_Sym *sym = gelf_getsym (symdata, shdr->sh_info, &sym_mem);
1672 : :
1673 [ - + + + ]: 64 : printf ((grpref[0] & GRP_COMDAT)
1674 : 8 : ? ngettext ("\
1675 : : \nCOMDAT section group [%2zu] '%s' with signature '%s' contains %zu entry:\n",
1676 : : "\
1677 : : \nCOMDAT section group [%2zu] '%s' with signature '%s' contains %zu entries:\n",
1678 : : data->d_size / sizeof (Elf32_Word) - 1)
1679 : 24 : : ngettext ("\
1680 : : \nSection group [%2zu] '%s' with signature '%s' contains %zu entry:\n", "\
1681 : : \nSection group [%2zu] '%s' with signature '%s' contains %zu entries:\n",
1682 : : data->d_size / sizeof (Elf32_Word) - 1),
1683 : : elf_ndxscn (scn),
1684 : 32 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
1685 : : (sym == NULL ? NULL
1686 : 32 : : elf_strptr (ebl->elf, symshdr->sh_link, sym->st_name))
1687 : 0 : ?: _("<INVALID SYMBOL>"),
1688 [ + - ]: 32 : data->d_size / sizeof (Elf32_Word) - 1);
1689 : :
1690 [ + + ]: 104 : for (size_t cnt = 1; cnt < data->d_size / sizeof (Elf32_Word); ++cnt)
1691 : : {
1692 : 72 : GElf_Shdr grpshdr_mem;
1693 : 72 : GElf_Shdr *grpshdr = gelf_getshdr (elf_getscn (ebl->elf, grpref[cnt]),
1694 : : &grpshdr_mem);
1695 : :
1696 : 72 : const char *str;
1697 [ + - ]: 144 : printf (" [%2u] %s\n",
1698 : : grpref[cnt],
1699 : : grpshdr != NULL
1700 [ - + ]: 72 : && (str = elf_strptr (ebl->elf, shstrndx, grpshdr->sh_name))
1701 : 0 : ? str : _("<INVALID SECTION>"));
1702 : : }
1703 : : }
1704 : :
1705 : :
1706 : : static void
1707 : 202 : print_scngrp (Ebl *ebl)
1708 : : {
1709 : : /* Find all relocation sections and handle them. */
1710 : 202 : Elf_Scn *scn = NULL;
1711 : :
1712 [ + + ]: 6522 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
1713 : : {
1714 : : /* Handle the section if it is a symbol table. */
1715 : 6320 : GElf_Shdr shdr_mem;
1716 : 6320 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
1717 : :
1718 [ + - + + ]: 6320 : if (shdr != NULL && shdr->sh_type == SHT_GROUP)
1719 : : {
1720 [ - + ]: 32 : if ((shdr->sh_flags & SHF_COMPRESSED) != 0)
1721 : : {
1722 [ # # ]: 0 : if (elf_compress (scn, 0, 0) < 0)
1723 : 0 : printf ("WARNING: %s [%zd]\n",
1724 : : _("Couldn't uncompress section"),
1725 : : elf_ndxscn (scn));
1726 : 0 : shdr = gelf_getshdr (scn, &shdr_mem);
1727 [ # # ]: 0 : if (unlikely (shdr == NULL))
1728 : 0 : error_exit (0, _("cannot get section [%zd] header: %s"),
1729 : : elf_ndxscn (scn),
1730 : : elf_errmsg (-1));
1731 : : }
1732 : 32 : handle_scngrp (ebl, scn, shdr);
1733 : : }
1734 : : }
1735 : 202 : }
1736 : :
1737 : :
1738 : : static const struct flags
1739 : : {
1740 : : int mask;
1741 : : const char *str;
1742 : : } dt_flags[] =
1743 : : {
1744 : : { DF_ORIGIN, "ORIGIN" },
1745 : : { DF_SYMBOLIC, "SYMBOLIC" },
1746 : : { DF_TEXTREL, "TEXTREL" },
1747 : : { DF_BIND_NOW, "BIND_NOW" },
1748 : : { DF_STATIC_TLS, "STATIC_TLS" }
1749 : : };
1750 : : static const int ndt_flags = sizeof (dt_flags) / sizeof (dt_flags[0]);
1751 : :
1752 : : static const struct flags dt_flags_1[] =
1753 : : {
1754 : : { DF_1_NOW, "NOW" },
1755 : : { DF_1_GLOBAL, "GLOBAL" },
1756 : : { DF_1_GROUP, "GROUP" },
1757 : : { DF_1_NODELETE, "NODELETE" },
1758 : : { DF_1_LOADFLTR, "LOADFLTR" },
1759 : : { DF_1_INITFIRST, "INITFIRST" },
1760 : : { DF_1_NOOPEN, "NOOPEN" },
1761 : : { DF_1_ORIGIN, "ORIGIN" },
1762 : : { DF_1_DIRECT, "DIRECT" },
1763 : : { DF_1_TRANS, "TRANS" },
1764 : : { DF_1_INTERPOSE, "INTERPOSE" },
1765 : : { DF_1_NODEFLIB, "NODEFLIB" },
1766 : : { DF_1_NODUMP, "NODUMP" },
1767 : : { DF_1_CONFALT, "CONFALT" },
1768 : : { DF_1_ENDFILTEE, "ENDFILTEE" },
1769 : : { DF_1_DISPRELDNE, "DISPRELDNE" },
1770 : : { DF_1_DISPRELPND, "DISPRELPND" },
1771 : : };
1772 : : static const int ndt_flags_1 = sizeof (dt_flags_1) / sizeof (dt_flags_1[0]);
1773 : :
1774 : : static const struct flags dt_feature_1[] =
1775 : : {
1776 : : { DTF_1_PARINIT, "PARINIT" },
1777 : : { DTF_1_CONFEXP, "CONFEXP" }
1778 : : };
1779 : : static const int ndt_feature_1 = (sizeof (dt_feature_1)
1780 : : / sizeof (dt_feature_1[0]));
1781 : :
1782 : : static const struct flags dt_posflag_1[] =
1783 : : {
1784 : : { DF_P1_LAZYLOAD, "LAZYLOAD" },
1785 : : { DF_P1_GROUPPERM, "GROUPPERM" }
1786 : : };
1787 : : static const int ndt_posflag_1 = (sizeof (dt_posflag_1)
1788 : : / sizeof (dt_posflag_1[0]));
1789 : :
1790 : :
1791 : : static void
1792 : 74 : print_flags (int class, GElf_Xword d_val, const struct flags *flags,
1793 : : int nflags)
1794 : : {
1795 : 74 : bool first = true;
1796 : 74 : int cnt;
1797 : :
1798 [ + + ]: 1104 : for (cnt = 0; cnt < nflags; ++cnt)
1799 [ + + ]: 1030 : if (d_val & flags[cnt].mask)
1800 : : {
1801 [ + + ]: 68 : if (!first)
1802 : 36 : putchar (' ');
1803 : 68 : fputs (flags[cnt].str, stdout);
1804 : 68 : d_val &= ~flags[cnt].mask;
1805 : 68 : first = false;
1806 : : }
1807 : :
1808 [ + + ]: 74 : if (d_val != 0)
1809 : : {
1810 [ + + ]: 62 : if (!first)
1811 : 20 : putchar (' ');
1812 [ + + ]: 62 : printf ("%#0*" PRIx64, class == ELFCLASS32 ? 10 : 18, d_val);
1813 : : }
1814 : :
1815 : 74 : putchar ('\n');
1816 : 74 : }
1817 : :
1818 : :
1819 : : static void
1820 : 14 : print_dt_flags (int class, GElf_Xword d_val)
1821 : : {
1822 : 14 : print_flags (class, d_val, dt_flags, ndt_flags);
1823 : 14 : }
1824 : :
1825 : :
1826 : : static void
1827 : 56 : print_dt_flags_1 (int class, GElf_Xword d_val)
1828 : : {
1829 : 56 : print_flags (class, d_val, dt_flags_1, ndt_flags_1);
1830 : 56 : }
1831 : :
1832 : :
1833 : : static void
1834 : 2 : print_dt_feature_1 (int class, GElf_Xword d_val)
1835 : : {
1836 : 2 : print_flags (class, d_val, dt_feature_1, ndt_feature_1);
1837 : 2 : }
1838 : :
1839 : :
1840 : : static void
1841 : 2 : print_dt_posflag_1 (int class, GElf_Xword d_val)
1842 : : {
1843 : 2 : print_flags (class, d_val, dt_posflag_1, ndt_posflag_1);
1844 : 2 : }
1845 : :
1846 : :
1847 : : static size_t
1848 : 136 : get_dyn_ents (Elf_Data * dyn_data)
1849 : : {
1850 : 136 : GElf_Dyn *dyn;
1851 : 136 : GElf_Dyn dyn_mem;
1852 : 136 : size_t dyn_idx = 0;
1853 : 3414 : do
1854 : : {
1855 : 3414 : dyn = gelf_getdyn(dyn_data, dyn_idx, &dyn_mem);
1856 [ + - ]: 3414 : if (dyn != NULL)
1857 : 3414 : ++dyn_idx;
1858 : : }
1859 [ + + ]: 3414 : while (dyn != NULL && dyn->d_tag != DT_NULL);
1860 : :
1861 : 136 : return dyn_idx;
1862 : : }
1863 : :
1864 : :
1865 : : static void
1866 : 136 : handle_dynamic (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr, GElf_Phdr *phdr)
1867 : : {
1868 : 136 : int class = gelf_getclass (ebl->elf);
1869 : 136 : GElf_Shdr glink_mem;
1870 : 136 : GElf_Shdr *glink;
1871 : 136 : Elf_Data *data;
1872 : 136 : size_t cnt;
1873 : 136 : size_t shstrndx;
1874 : 136 : size_t dyn_ents;
1875 : :
1876 : : /* Get the data of the section. */
1877 [ + + + - ]: 136 : if (use_dynamic_segment && phdr != NULL)
1878 : 2 : data = elf_getdata_rawchunk(ebl->elf, phdr->p_offset,
1879 : : phdr->p_filesz, ELF_T_DYN);
1880 : : else
1881 : 134 : data = elf_getdata (scn, NULL);
1882 : :
1883 [ - + ]: 136 : if (data == NULL)
1884 : 0 : return;
1885 : :
1886 : : /* Get the dynamic section entry number */
1887 : 136 : dyn_ents = get_dyn_ents (data);
1888 : :
1889 [ + + + - ]: 136 : if (!use_dynamic_segment && shdr != NULL)
1890 : : {
1891 : : /* Get the section header string table index. */
1892 [ - + ]: 134 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
1893 : 0 : error_exit (0, _("cannot get section header string table index"));
1894 : :
1895 : 134 : glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link), &glink_mem);
1896 [ - + ]: 134 : if (glink == NULL)
1897 : 0 : error_exit (0, _("invalid sh_link value in section %zu"),
1898 : : elf_ndxscn (scn));
1899 : :
1900 : 134 : printf (ngettext ("\
1901 : : \nDynamic segment contains %lu entry:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'\n",
1902 : : "\
1903 : : \nDynamic segment contains %lu entries:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'\n",
1904 : : dyn_ents),
1905 : : (unsigned long int) dyn_ents,
1906 : : class == ELFCLASS32 ? 10 : 18, shdr->sh_addr,
1907 : : shdr->sh_offset,
1908 [ + + ]: 134 : (int) shdr->sh_link,
1909 : 134 : elf_strptr (ebl->elf, shstrndx, glink->sh_name));
1910 : : }
1911 [ + - ]: 2 : else if (phdr != NULL)
1912 : : {
1913 [ + - ]: 4 : printf (ngettext ("\
1914 : : \nDynamic segment contains %lu entry:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 "\n",
1915 : : "\
1916 : : \nDynamic segment contains %lu entries:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 "\n",
1917 : : dyn_ents),
1918 : : (unsigned long int) dyn_ents,
1919 : : class == ELFCLASS32 ? 10 : 18, phdr->p_paddr,
1920 : : phdr->p_offset);
1921 : : }
1922 : :
1923 : 136 : fputs (_(" Type Value\n"), stdout);
1924 : :
1925 : : /* if --use-dynamic option is enabled,
1926 : : use the string table to get the related library info. */
1927 : 136 : Elf_Data *strtab_data = NULL;
1928 [ + + + - ]: 136 : if (use_dynamic_segment && phdr != NULL)
1929 : : {
1930 : 2 : strtab_data = get_dynscn_strtab(ebl->elf, phdr);
1931 [ - + ]: 2 : if (strtab_data == NULL)
1932 : 0 : error_exit (0, _("cannot get string table by using dynamic segment"));
1933 : : }
1934 : :
1935 [ + + ]: 3550 : for (cnt = 0; cnt < dyn_ents; ++cnt)
1936 : : {
1937 : 3414 : GElf_Dyn dynmem;
1938 : 3414 : GElf_Dyn *dyn = gelf_getdyn (data, cnt, &dynmem);
1939 [ + - ]: 3414 : if (dyn == NULL)
1940 : : break;
1941 : :
1942 : 3414 : char buf[64];
1943 : 3414 : printf (" %-17s ",
1944 : : ebl_dynamic_tag_name (ebl, dyn->d_tag, buf, sizeof (buf)));
1945 : :
1946 : 3414 : char *name = NULL;
1947 : 3414 : if (dyn->d_tag == DT_NEEDED
1948 [ + + ]: 3414 : || dyn->d_tag == DT_SONAME
1949 [ + + ]: 3182 : || dyn->d_tag == DT_RPATH
1950 [ + + ]: 3180 : || dyn->d_tag == DT_RUNPATH)
1951 : : {
1952 [ + + + - ]: 236 : if (! use_dynamic_segment && shdr != NULL)
1953 : 232 : name = elf_strptr (ebl->elf, shdr->sh_link, dyn->d_un.d_val);
1954 [ - + ]: 4 : else if (dyn->d_un.d_val < strtab_data->d_size
1955 : 4 : && memrchr (strtab_data->d_buf + dyn->d_un.d_val, '\0',
1956 [ - + ]: 4 : strtab_data->d_size - 1 - dyn->d_un.d_val) != NULL)
1957 : 3414 : name = ((char *) strtab_data->d_buf) + dyn->d_un.d_val;
1958 : : }
1959 : :
1960 [ + + + + : 3414 : switch (dyn->d_tag)
+ + + + +
+ + + + ]
1961 : : {
1962 : 268 : case DT_NULL:
1963 : : case DT_DEBUG:
1964 : : case DT_BIND_NOW:
1965 : : case DT_TEXTREL:
1966 : : /* No further output. */
1967 : 268 : fputc ('\n', stdout);
1968 : 268 : break;
1969 : :
1970 : 226 : case DT_NEEDED:
1971 : 226 : printf (_("Shared library: [%s]\n"), name);
1972 : : break;
1973 : :
1974 : 6 : case DT_SONAME:
1975 : 6 : printf (_("Library soname: [%s]\n"), name);
1976 : : break;
1977 : :
1978 : 2 : case DT_RPATH:
1979 : 2 : printf (_("Library rpath: [%s]\n"), name);
1980 : : break;
1981 : :
1982 : 2 : case DT_RUNPATH:
1983 : 2 : printf (_("Library runpath: [%s]\n"), name);
1984 : : break;
1985 : :
1986 : 902 : case DT_PLTRELSZ:
1987 : : case DT_RELASZ:
1988 : : case DT_STRSZ:
1989 : : case DT_RELSZ:
1990 : : case DT_RELRSZ:
1991 : : case DT_RELAENT:
1992 : : case DT_SYMENT:
1993 : : case DT_RELENT:
1994 : : case DT_RELRENT:
1995 : : case DT_PLTPADSZ:
1996 : : case DT_MOVEENT:
1997 : : case DT_MOVESZ:
1998 : : case DT_INIT_ARRAYSZ:
1999 : : case DT_FINI_ARRAYSZ:
2000 : : case DT_SYMINSZ:
2001 : : case DT_SYMINENT:
2002 : : case DT_GNU_CONFLICTSZ:
2003 : : case DT_GNU_LIBLISTSZ:
2004 : 902 : printf (_("%" PRId64 " (bytes)\n"), dyn->d_un.d_val);
2005 : : break;
2006 : :
2007 : 212 : case DT_VERDEFNUM:
2008 : : case DT_VERNEEDNUM:
2009 : : case DT_RELACOUNT:
2010 : : case DT_RELCOUNT:
2011 : 212 : printf ("%" PRId64 "\n", dyn->d_un.d_val);
2012 : : break;
2013 : :
2014 : 108 : case DT_PLTREL:;
2015 : 108 : const char *tagname = ebl_dynamic_tag_name (ebl, dyn->d_un.d_val,
2016 : : NULL, 0);
2017 [ + + ]: 108 : puts (tagname ?: "???");
2018 : 108 : break;
2019 : :
2020 : 14 : case DT_FLAGS:
2021 : 14 : print_dt_flags (class, dyn->d_un.d_val);
2022 : : break;
2023 : :
2024 : 56 : case DT_FLAGS_1:
2025 : 56 : print_dt_flags_1 (class, dyn->d_un.d_val);
2026 : : break;
2027 : :
2028 : 2 : case DT_FEATURE_1:
2029 : 2 : print_dt_feature_1 (class, dyn->d_un.d_val);
2030 : : break;
2031 : :
2032 : 2 : case DT_POSFLAG_1:
2033 : 2 : print_dt_posflag_1 (class, dyn->d_un.d_val);
2034 : : break;
2035 : :
2036 : 1614 : default:
2037 [ + + ]: 5028 : printf ("%#0*" PRIx64 "\n",
2038 : : class == ELFCLASS32 ? 10 : 18, dyn->d_un.d_val);
2039 : : break;
2040 : : }
2041 : : }
2042 : : }
2043 : :
2044 : :
2045 : : /* Print the dynamic segment. */
2046 : : static void
2047 : 190 : print_dynamic (Ebl *ebl)
2048 : : {
2049 [ + + ]: 1072 : for (size_t i = 0; i < phnum; ++i)
2050 : : {
2051 : 1060 : GElf_Phdr phdr_mem;
2052 : 1060 : GElf_Phdr *phdr = gelf_getphdr (ebl->elf, i, &phdr_mem);
2053 : :
2054 [ + - + + ]: 1060 : if (phdr != NULL && phdr->p_type == PT_DYNAMIC)
2055 : : {
2056 : 178 : Elf_Scn *scn = gelf_offscn (ebl->elf, phdr->p_offset);
2057 : 178 : GElf_Shdr shdr_mem;
2058 : 178 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
2059 [ + + ]: 178 : if ((use_dynamic_segment && phdr != NULL)
2060 [ + + + + ]: 176 : || (shdr != NULL && shdr->sh_type == SHT_DYNAMIC))
2061 : 136 : handle_dynamic (ebl, scn, shdr, phdr);
2062 : 178 : break;
2063 : : }
2064 : : }
2065 : 190 : }
2066 : :
2067 : :
2068 : : /* Print relocations. */
2069 : : static void
2070 : 194 : print_relocs (Ebl *ebl, Dwfl_Module *mod, GElf_Ehdr *ehdr)
2071 : : {
2072 : : /* Find all relocation sections and handle them. */
2073 : 194 : Elf_Scn *scn = NULL;
2074 : :
2075 [ + + ]: 530466 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
2076 : : {
2077 : : /* Handle the section if it is a symbol table. */
2078 : 530272 : GElf_Shdr shdr_mem;
2079 : 530272 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
2080 : :
2081 [ + - ]: 530272 : if (likely (shdr != NULL))
2082 : : {
2083 [ + + ]: 530272 : if (shdr->sh_type == SHT_REL)
2084 : 92 : handle_relocs_rel (ebl, ehdr, scn, shdr);
2085 [ + + ]: 530180 : else if (shdr->sh_type == SHT_RELA)
2086 : 282 : handle_relocs_rela (ebl, ehdr, scn, shdr);
2087 [ - + ]: 529898 : else if (shdr->sh_type == SHT_RELR)
2088 : 0 : handle_relocs_relr (ebl, mod, scn, shdr);
2089 : : }
2090 : : }
2091 : 194 : }
2092 : :
2093 : :
2094 : : /* Handle a relocation section. */
2095 : : static void
2096 : 92 : handle_relocs_rel (Ebl *ebl, GElf_Ehdr *ehdr, Elf_Scn *scn, GElf_Shdr *shdr)
2097 : : {
2098 : 92 : int class = gelf_getclass (ebl->elf);
2099 : 92 : size_t sh_entsize = gelf_fsize (ebl->elf, ELF_T_REL, 1, EV_CURRENT);
2100 : 92 : int nentries = shdr->sh_size / sh_entsize;
2101 : :
2102 : : /* Get the data of the section. */
2103 : 92 : Elf_Data *data = elf_getdata (scn, NULL);
2104 [ + - ]: 92 : if (data == NULL)
2105 : 0 : return;
2106 : :
2107 : : /* Get the symbol table information. */
2108 : 92 : Elf_Scn *symscn = elf_getscn (ebl->elf, shdr->sh_link);
2109 : 92 : GElf_Shdr symshdr_mem;
2110 : 92 : GElf_Shdr *symshdr = gelf_getshdr (symscn, &symshdr_mem);
2111 : 92 : Elf_Data *symdata = elf_getdata (symscn, NULL);
2112 : :
2113 : : /* Get the section header of the section the relocations are for. */
2114 : 92 : GElf_Shdr destshdr_mem;
2115 : 92 : GElf_Shdr *destshdr = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_info),
2116 : : &destshdr_mem);
2117 : :
2118 [ + - - + ]: 92 : if (unlikely (symshdr == NULL || symdata == NULL || destshdr == NULL))
2119 : : {
2120 : 0 : printf (_("\nInvalid symbol table at offset %#0" PRIx64 "\n"),
2121 : : shdr->sh_offset);
2122 : 0 : return;
2123 : : }
2124 : :
2125 : : /* Search for the optional extended section index table if there are
2126 : : more than 64k sections. */
2127 : 92 : Elf_Data *xndxdata = NULL;
2128 [ - + ]: 92 : int xndxscnidx = shnum >= SHN_LORESERVE ? elf_scnshndx (symscn) : 0;
2129 [ - + ]: 92 : if (unlikely (xndxscnidx > 0))
2130 : 0 : xndxdata = elf_getdata (elf_getscn (ebl->elf, xndxscnidx), NULL);
2131 : :
2132 : : /* Get the section header string table index. */
2133 : 92 : size_t shstrndx;
2134 [ - + ]: 92 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
2135 : 0 : error_exit (0, _("cannot get section header string table index"));
2136 : :
2137 [ + + ]: 92 : if (shdr->sh_info != 0)
2138 : 76 : printf (ngettext ("\
2139 : : \nRelocation section [%2zu] '%s' for section [%2u] '%s' at offset %#0" PRIx64 " contains %d entry:\n",
2140 : : "\
2141 : : \nRelocation section [%2zu] '%s' for section [%2u] '%s' at offset %#0" PRIx64 " contains %d entries:\n",
2142 : : nentries),
2143 : : elf_ndxscn (scn),
2144 : 76 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
2145 : 76 : (unsigned int) shdr->sh_info,
2146 : 76 : elf_strptr (ebl->elf, shstrndx, destshdr->sh_name),
2147 : : shdr->sh_offset,
2148 : : nentries);
2149 : : else
2150 : : /* The .rel.dyn section does not refer to a specific section but
2151 : : instead of section index zero. Do not try to print a section
2152 : : name. */
2153 : 16 : printf (ngettext ("\
2154 : : \nRelocation section [%2u] '%s' at offset %#0" PRIx64 " contains %d entry:\n",
2155 : : "\
2156 : : \nRelocation section [%2u] '%s' at offset %#0" PRIx64 " contains %d entries:\n",
2157 : : nentries),
2158 : 16 : (unsigned int) elf_ndxscn (scn),
2159 : 16 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
2160 : : shdr->sh_offset,
2161 : : nentries);
2162 [ + - ]: 92 : fputs (class == ELFCLASS32
2163 : 92 : ? _("\
2164 : : Offset Type Value Name\n")
2165 : 0 : : _("\
2166 : : Offset Type Value Name\n"),
2167 : : stdout);
2168 : :
2169 : 92 : int is_statically_linked = 0;
2170 [ + + ]: 25942 : for (int cnt = 0; cnt < nentries; ++cnt)
2171 : : {
2172 : 25850 : GElf_Rel relmem;
2173 : 25850 : GElf_Rel *rel = gelf_getrel (data, cnt, &relmem);
2174 [ + - ]: 25850 : if (likely (rel != NULL))
2175 : : {
2176 : 25850 : char buf[128];
2177 : 25850 : GElf_Sym symmem;
2178 : 25850 : Elf32_Word xndx;
2179 : 51700 : GElf_Sym *sym = gelf_getsymshndx (symdata, xndxdata,
2180 : 25850 : GELF_R_SYM (rel->r_info),
2181 : : &symmem, &xndx);
2182 [ - + ]: 25850 : if (unlikely (sym == NULL))
2183 : : {
2184 : : /* As a special case we have to handle relocations in static
2185 : : executables. This only happens for IRELATIVE relocations
2186 : : (so far). There is no symbol table. */
2187 [ # # ]: 0 : if (is_statically_linked == 0)
2188 : : {
2189 : : /* Find the program header and look for a PT_INTERP entry. */
2190 : 0 : is_statically_linked = -1;
2191 [ # # ]: 0 : if (ehdr->e_type == ET_EXEC)
2192 : : {
2193 : 0 : is_statically_linked = 1;
2194 : :
2195 [ # # ]: 0 : for (size_t inner = 0; inner < phnum; ++inner)
2196 : : {
2197 : 0 : GElf_Phdr phdr_mem;
2198 : 0 : GElf_Phdr *phdr = gelf_getphdr (ebl->elf, inner,
2199 : : &phdr_mem);
2200 [ # # # # ]: 0 : if (phdr != NULL && phdr->p_type == PT_INTERP)
2201 : : {
2202 : 0 : is_statically_linked = -1;
2203 : 0 : break;
2204 : : }
2205 : : }
2206 : : }
2207 : : }
2208 : :
2209 [ # # # # ]: 0 : if (is_statically_linked > 0 && shdr->sh_link == 0)
2210 [ # # # # ]: 0 : printf ("\
2211 : : %#0*" PRIx64 " %-20s %*s %s\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 : : class == ELFCLASS32 ? 10 : 18, "",
2220 : 0 : elf_strptr (ebl->elf, shstrndx, destshdr->sh_name));
2221 : : else
2222 [ # # # # ]: 0 : printf (" %#0*" PRIx64 " %-20s <%s %ld>\n",
2223 : : class == ELFCLASS32 ? 10 : 18, rel->r_offset,
2224 : 0 : ebl_reloc_type_check (ebl, GELF_R_TYPE (rel->r_info))
2225 : : /* Avoid the leading R_ which isn't carrying any
2226 : : information. */
2227 : 0 : ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info),
2228 : : buf, sizeof (buf)) + 2
2229 : 0 : : _("<INVALID RELOC>"),
2230 : : _("INVALID SYMBOL"),
2231 : 0 : (long int) GELF_R_SYM (rel->r_info));
2232 : : }
2233 [ + + ]: 25850 : else if (GELF_ST_TYPE (sym->st_info) != STT_SECTION
2234 [ + + ]: 130 : && !(GELF_ST_TYPE (sym->st_info) == STT_NOTYPE
2235 [ - + ]: 66 : && GELF_ST_BIND (sym->st_info) == STB_LOCAL
2236 [ # # ]: 0 : && sym->st_shndx != SHN_UNDEF
2237 [ # # ]: 0 : && sym->st_value == 0)) // local start section label
2238 [ - + ]: 26110 : printf (" %#0*" PRIx64 " %-20s %#0*" PRIx64 " %s\n",
2239 : : class == ELFCLASS32 ? 10 : 18, rel->r_offset,
2240 [ + - ]: 130 : likely (ebl_reloc_type_check (ebl,
2241 : : GELF_R_TYPE (rel->r_info)))
2242 : : /* Avoid the leading R_ which isn't carrying any
2243 : : information. */
2244 : 130 : ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info),
2245 : : buf, sizeof (buf)) + 2
2246 : 0 : : _("<INVALID RELOC>"),
2247 : : class == ELFCLASS32 ? 10 : 18, sym->st_value,
2248 : 130 : elf_strptr (ebl->elf, symshdr->sh_link, sym->st_name));
2249 : : else
2250 : : {
2251 : : /* This is a relocation against a STT_SECTION symbol
2252 : : or a local start section label for which we print
2253 : : section name. */
2254 : 25720 : GElf_Shdr secshdr_mem;
2255 : 25720 : GElf_Shdr *secshdr;
2256 : 25720 : secshdr = gelf_getshdr (elf_getscn (ebl->elf,
2257 [ + - ]: 25720 : sym->st_shndx == SHN_XINDEX
2258 : 0 : ? xndx : sym->st_shndx),
2259 : : &secshdr_mem);
2260 : :
2261 [ - + ]: 25720 : if (unlikely (secshdr == NULL))
2262 [ # # # # ]: 0 : printf (" %#0*" PRIx64 " %-20s <%s %ld>\n",
2263 : : class == ELFCLASS32 ? 10 : 18, rel->r_offset,
2264 : 0 : ebl_reloc_type_check (ebl, GELF_R_TYPE (rel->r_info))
2265 : : /* Avoid the leading R_ which isn't carrying any
2266 : : information. */
2267 : 0 : ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info),
2268 : : buf, sizeof (buf)) + 2
2269 : 0 : : _("<INVALID RELOC>"),
2270 : : _("INVALID SECTION"),
2271 [ # # ]: 0 : (long int) (sym->st_shndx == SHN_XINDEX
2272 : 0 : ? xndx : sym->st_shndx));
2273 : : else
2274 [ - + + - ]: 77160 : printf (" %#0*" PRIx64 " %-20s %#0*" PRIx64 " %s\n",
2275 : : class == ELFCLASS32 ? 10 : 18, rel->r_offset,
2276 : 25720 : ebl_reloc_type_check (ebl, GELF_R_TYPE (rel->r_info))
2277 : : /* Avoid the leading R_ which isn't carrying any
2278 : : information. */
2279 : 25720 : ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info),
2280 : : buf, sizeof (buf)) + 2
2281 : 0 : : _("<INVALID RELOC>"),
2282 : : class == ELFCLASS32 ? 10 : 18, sym->st_value,
2283 : 25720 : elf_strptr (ebl->elf, shstrndx, secshdr->sh_name));
2284 : : }
2285 : : }
2286 : : }
2287 : : }
2288 : :
2289 : :
2290 : : /* Handle a relocation section. */
2291 : : static void
2292 : 282 : handle_relocs_rela (Ebl *ebl, GElf_Ehdr *ehdr, Elf_Scn *scn, GElf_Shdr *shdr)
2293 : : {
2294 : 282 : int class = gelf_getclass (ebl->elf);
2295 : 282 : size_t sh_entsize = gelf_fsize (ebl->elf, ELF_T_RELA, 1, EV_CURRENT);
2296 : 282 : int nentries = shdr->sh_size / sh_entsize;
2297 : :
2298 : : /* Get the data of the section. */
2299 : 282 : Elf_Data *data = elf_getdata (scn, NULL);
2300 [ + - ]: 282 : if (data == NULL)
2301 : 0 : return;
2302 : :
2303 : : /* Get the symbol table information. */
2304 : 282 : Elf_Scn *symscn = elf_getscn (ebl->elf, shdr->sh_link);
2305 : 282 : GElf_Shdr symshdr_mem;
2306 : 282 : GElf_Shdr *symshdr = gelf_getshdr (symscn, &symshdr_mem);
2307 : 282 : Elf_Data *symdata = elf_getdata (symscn, NULL);
2308 : :
2309 : : /* Get the section header of the section the relocations are for. */
2310 : 282 : GElf_Shdr destshdr_mem;
2311 : 282 : GElf_Shdr *destshdr = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_info),
2312 : : &destshdr_mem);
2313 : :
2314 [ + - - + ]: 282 : if (unlikely (symshdr == NULL || symdata == NULL || destshdr == NULL))
2315 : : {
2316 : 0 : printf (_("\nInvalid symbol table at offset %#0" PRIx64 "\n"),
2317 : : shdr->sh_offset);
2318 : 0 : return;
2319 : : }
2320 : :
2321 : : /* Search for the optional extended section index table if there are
2322 : : more than 64k sections. */
2323 : 282 : Elf_Data *xndxdata = NULL;
2324 [ + + ]: 282 : int xndxscnidx = shnum >= SHN_LORESERVE ? elf_scnshndx (symscn) : 0;
2325 [ + + ]: 282 : if (unlikely (xndxscnidx > 0))
2326 : 16 : xndxdata = elf_getdata (elf_getscn (ebl->elf, xndxscnidx), NULL);
2327 : :
2328 : : /* Get the section header string table index. */
2329 : 282 : size_t shstrndx;
2330 [ - + ]: 282 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
2331 : 0 : error_exit (0, _("cannot get section header string table index"));
2332 : :
2333 [ + + ]: 282 : if (shdr->sh_info != 0)
2334 : 168 : printf (ngettext ("\
2335 : : \nRelocation section [%2zu] '%s' for section [%2u] '%s' at offset %#0" PRIx64 " contains %d entry:\n",
2336 : : "\
2337 : : \nRelocation section [%2zu] '%s' for section [%2u] '%s' at offset %#0" PRIx64 " contains %d entries:\n",
2338 : : nentries),
2339 : : elf_ndxscn (scn),
2340 : 168 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
2341 : 168 : (unsigned int) shdr->sh_info,
2342 : 168 : elf_strptr (ebl->elf, shstrndx, destshdr->sh_name),
2343 : : shdr->sh_offset,
2344 : : nentries);
2345 : : else
2346 : : /* The .rela.dyn section does not refer to a specific section but
2347 : : instead of section index zero. Do not try to print a section
2348 : : name. */
2349 : 114 : printf (ngettext ("\
2350 : : \nRelocation section [%2u] '%s' at offset %#0" PRIx64 " contains %d entry:\n",
2351 : : "\
2352 : : \nRelocation section [%2u] '%s' at offset %#0" PRIx64 " contains %d entries:\n",
2353 : : nentries),
2354 : 114 : (unsigned int) elf_ndxscn (scn),
2355 : 114 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
2356 : : shdr->sh_offset,
2357 : : nentries);
2358 [ - + ]: 282 : fputs (class == ELFCLASS32
2359 : 0 : ? _("\
2360 : : Offset Type Value Addend Name\n")
2361 : 282 : : _("\
2362 : : Offset Type Value Addend Name\n"),
2363 : : stdout);
2364 : :
2365 : 282 : int is_statically_linked = 0;
2366 [ + + ]: 1168074 : for (int cnt = 0; cnt < nentries; ++cnt)
2367 : : {
2368 : 1167792 : GElf_Rela relmem;
2369 : 1167792 : GElf_Rela *rel = gelf_getrela (data, cnt, &relmem);
2370 [ + - ]: 1167792 : if (likely (rel != NULL))
2371 : : {
2372 : 1167792 : char buf[64];
2373 : 1167792 : GElf_Sym symmem;
2374 : 1167792 : Elf32_Word xndx;
2375 : 2335584 : GElf_Sym *sym = gelf_getsymshndx (symdata, xndxdata,
2376 : 1167792 : GELF_R_SYM (rel->r_info),
2377 : : &symmem, &xndx);
2378 : :
2379 [ - + ]: 1167792 : if (unlikely (sym == NULL))
2380 : : {
2381 : : /* As a special case we have to handle relocations in static
2382 : : executables. This only happens for IRELATIVE relocations
2383 : : (so far). There is no symbol table. */
2384 [ # # ]: 0 : if (is_statically_linked == 0)
2385 : : {
2386 : : /* Find the program header and look for a PT_INTERP entry. */
2387 : 0 : is_statically_linked = -1;
2388 [ # # ]: 0 : if (ehdr->e_type == ET_EXEC)
2389 : : {
2390 : 0 : is_statically_linked = 1;
2391 : :
2392 [ # # ]: 0 : for (size_t inner = 0; inner < phnum; ++inner)
2393 : : {
2394 : 0 : GElf_Phdr phdr_mem;
2395 : 0 : GElf_Phdr *phdr = gelf_getphdr (ebl->elf, inner,
2396 : : &phdr_mem);
2397 [ # # # # ]: 0 : if (phdr != NULL && phdr->p_type == PT_INTERP)
2398 : : {
2399 : 0 : is_statically_linked = -1;
2400 : 0 : break;
2401 : : }
2402 : : }
2403 : : }
2404 : : }
2405 : :
2406 [ # # # # ]: 0 : if (is_statically_linked > 0 && shdr->sh_link == 0)
2407 [ # # # # ]: 0 : printf ("\
2408 : : %#0*" PRIx64 " %-15s %*s %#6" PRIx64 " %s\n",
2409 : : class == ELFCLASS32 ? 10 : 18, rel->r_offset,
2410 : 0 : ebl_reloc_type_check (ebl, GELF_R_TYPE (rel->r_info))
2411 : : /* Avoid the leading R_ which isn't carrying any
2412 : : information. */
2413 : 0 : ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info),
2414 : : buf, sizeof (buf)) + 2
2415 : 0 : : _("<INVALID RELOC>"),
2416 : : class == ELFCLASS32 ? 10 : 18, "",
2417 : : rel->r_addend,
2418 : 0 : elf_strptr (ebl->elf, shstrndx, destshdr->sh_name));
2419 : : else
2420 [ # # # # ]: 0 : printf (" %#0*" PRIx64 " %-15s <%s %ld>\n",
2421 : : class == ELFCLASS32 ? 10 : 18, rel->r_offset,
2422 : 0 : ebl_reloc_type_check (ebl, GELF_R_TYPE (rel->r_info))
2423 : : /* Avoid the leading R_ which isn't carrying any
2424 : : information. */
2425 : 0 : ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info),
2426 : : buf, sizeof (buf)) + 2
2427 : 0 : : _("<INVALID RELOC>"),
2428 : : _("INVALID SYMBOL"),
2429 : 0 : (long int) GELF_R_SYM (rel->r_info));
2430 : : }
2431 [ + + ]: 1167792 : else if (GELF_ST_TYPE (sym->st_info) != STT_SECTION
2432 [ + + ]: 629886 : && !(GELF_ST_TYPE (sym->st_info) == STT_NOTYPE
2433 [ + + ]: 103146 : && GELF_ST_BIND (sym->st_info) == STB_LOCAL
2434 [ + + ]: 101616 : && sym->st_shndx != SHN_UNDEF
2435 [ + + ]: 716 : && sym->st_value == 0)) // local start section label
2436 [ + - ]: 2427540 : printf ("\
2437 : : %#0*" PRIx64 " %-15s %#0*" PRIx64 " %+6" PRId64 " %s\n",
2438 : : class == ELFCLASS32 ? 10 : 18, rel->r_offset,
2439 [ + - ]: 629874 : likely (ebl_reloc_type_check (ebl,
2440 : : GELF_R_TYPE (rel->r_info)))
2441 : : /* Avoid the leading R_ which isn't carrying any
2442 : : information. */
2443 : 629874 : ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info),
2444 : : buf, sizeof (buf)) + 2
2445 : 0 : : _("<INVALID RELOC>"),
2446 : : class == ELFCLASS32 ? 10 : 18, sym->st_value,
2447 : : rel->r_addend,
2448 : 629874 : elf_strptr (ebl->elf, symshdr->sh_link, sym->st_name));
2449 : : else
2450 : : {
2451 : : /* This is a relocation against a STT_SECTION symbol
2452 : : or a local start section label for which we print
2453 : : section name. */
2454 : 537918 : GElf_Shdr secshdr_mem;
2455 : 537918 : GElf_Shdr *secshdr;
2456 : 537918 : secshdr = gelf_getshdr (elf_getscn (ebl->elf,
2457 [ + + ]: 537918 : sym->st_shndx == SHN_XINDEX
2458 : 2088 : ? xndx : sym->st_shndx),
2459 : : &secshdr_mem);
2460 : :
2461 [ - + ]: 537918 : if (unlikely (secshdr == NULL))
2462 [ # # # # ]: 0 : printf (" %#0*" PRIx64 " %-15s <%s %ld>\n",
2463 : : class == ELFCLASS32 ? 10 : 18, rel->r_offset,
2464 : 0 : ebl_reloc_type_check (ebl, GELF_R_TYPE (rel->r_info))
2465 : : /* Avoid the leading R_ which isn't carrying any
2466 : : information. */
2467 : 0 : ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info),
2468 : : buf, sizeof (buf)) + 2
2469 : 0 : : _("<INVALID RELOC>"),
2470 : : _("INVALID SECTION"),
2471 [ # # ]: 0 : (long int) (sym->st_shndx == SHN_XINDEX
2472 : 0 : ? xndx : sym->st_shndx));
2473 : : else
2474 [ + - + - ]: 1613754 : printf ("\
2475 : : %#0*" PRIx64 " %-15s %#0*" PRIx64 " %+6" PRId64 " %s\n",
2476 : : class == ELFCLASS32 ? 10 : 18, rel->r_offset,
2477 : 537918 : ebl_reloc_type_check (ebl, GELF_R_TYPE (rel->r_info))
2478 : : /* Avoid the leading R_ which isn't carrying any
2479 : : information. */
2480 : 537918 : ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info),
2481 : : buf, sizeof (buf)) + 2
2482 : 0 : : _("<INVALID RELOC>"),
2483 : : class == ELFCLASS32 ? 10 : 18, sym->st_value,
2484 : : rel->r_addend,
2485 : 537918 : elf_strptr (ebl->elf, shstrndx, secshdr->sh_name));
2486 : : }
2487 : : }
2488 : : }
2489 : : }
2490 : :
2491 : : /* Handle a relocation section. */
2492 : : static void
2493 : 0 : handle_relocs_relr (Ebl *ebl, Dwfl_Module *mod, Elf_Scn *scn, GElf_Shdr *shdr)
2494 : : {
2495 : 0 : int class = gelf_getclass (ebl->elf);
2496 : 0 : size_t sh_entsize = gelf_fsize (ebl->elf, ELF_T_RELR, 1, EV_CURRENT);
2497 : 0 : int nentries = shdr->sh_size / sh_entsize;
2498 : :
2499 : : /* Get the data of the section. */
2500 : 0 : Elf_Data *data = elf_getdata (scn, NULL);
2501 [ # # ]: 0 : if (data == NULL)
2502 : 0 : return;
2503 : :
2504 : : /* Get the section header string table index. */
2505 : 0 : size_t shstrndx;
2506 [ # # ]: 0 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
2507 : 0 : error_exit (0, _("cannot get section header string table index"));
2508 : :
2509 : : /* A .relr.dyn section does not refer to a specific section. */
2510 : 0 : printf (ngettext ("\
2511 : : \nRelocation section [%2u] '%s' at offset %#0" PRIx64 " contains %d entry:\n",
2512 : : "\
2513 : : \nRelocation section [%2u] '%s' at offset %#0" PRIx64 " contains %d entries:\n",
2514 : : nentries),
2515 : 0 : (unsigned int) elf_ndxscn (scn),
2516 : 0 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
2517 : : shdr->sh_offset,
2518 : : nentries);
2519 : :
2520 [ # # ]: 0 : if (class == ELFCLASS32)
2521 : : {
2522 : : uint32_t base = 0;
2523 [ # # ]: 0 : for (int cnt = 0; cnt < nentries; ++cnt)
2524 : : {
2525 : 0 : Elf32_Word *words = data->d_buf;
2526 : 0 : Elf32_Word entry = words[cnt];
2527 : :
2528 : : /* Just the raw entries? */
2529 [ # # ]: 0 : if (print_unresolved_addresses)
2530 : 0 : printf (" %#010" PRIx32 "%s\n", entry,
2531 [ # # ]: 0 : (entry & 1) == 0 ? " *" : "");
2532 : : else
2533 : : {
2534 : : /* A real address, also sets base. */
2535 [ # # ]: 0 : if ((entry & 1) == 0)
2536 : : {
2537 : 0 : printf (" ");
2538 : 0 : print_dwarf_addr (mod, 4, entry, entry);
2539 : 0 : printf (" *\n");
2540 : :
2541 : 0 : base = entry + 4;
2542 : : }
2543 : : else
2544 : : {
2545 : : /* Untangle address from base and bits. */
2546 : : uint32_t addr;
2547 [ # # ]: 0 : for (addr = base; (entry >>= 1) != 0; addr += 4)
2548 [ # # ]: 0 : if ((entry & 1) != 0)
2549 : : {
2550 : 0 : printf (" ");
2551 : 0 : print_dwarf_addr (mod, 4, addr, addr);
2552 : 0 : printf ("\n");
2553 : : }
2554 : 0 : base += 4 * (4 * 8 - 1);
2555 : : }
2556 : : }
2557 : : }
2558 : : }
2559 : : else
2560 : : {
2561 : : uint64_t base = 0;
2562 [ # # ]: 0 : for (int cnt = 0; cnt < nentries; ++cnt)
2563 : : {
2564 : 0 : Elf64_Xword *xwords = data->d_buf;
2565 : 0 : Elf64_Xword entry = xwords[cnt];
2566 : :
2567 : : /* Just the raw entries? */
2568 [ # # ]: 0 : if (print_unresolved_addresses)
2569 : 0 : printf (" %#018" PRIx64 "%s\n", entry,
2570 [ # # ]: 0 : (entry & 1) == 0 ? " *" : "");
2571 : : else
2572 : : {
2573 : : /* A real address, also sets base. */
2574 [ # # ]: 0 : if ((entry & 1) == 0)
2575 : : {
2576 : 0 : printf (" ");
2577 : 0 : print_dwarf_addr (mod, 8, entry, entry);
2578 : 0 : printf (" *\n");
2579 : :
2580 : 0 : base = entry + 8;
2581 : : }
2582 : : else
2583 : : {
2584 : : /* Untangle address from base and bits. */
2585 : : uint64_t addr;
2586 [ # # ]: 0 : for (addr = base; (entry >>= 1) != 0; addr += 8)
2587 [ # # ]: 0 : if ((entry & 1) != 0)
2588 : : {
2589 : 0 : printf (" ");
2590 : 0 : print_dwarf_addr (mod, 8, addr, addr);
2591 : 0 : printf ("\n");
2592 : : }
2593 : 0 : base += 8 * (8 * 8 - 1);
2594 : : }
2595 : : }
2596 : : }
2597 : : }
2598 : : }
2599 : :
2600 : : /* Print the program header. Return true if a symtab is printed,
2601 : : false otherwise. */
2602 : : static bool
2603 : 438 : print_symtab (Ebl *ebl, int type)
2604 : : {
2605 : : /* Use the dynamic section info to display symbol tables. */
2606 [ - + - - ]: 438 : if (use_dynamic_segment && type == SHT_DYNSYM)
2607 : 0 : return handle_dynamic_symtab(ebl);
2608 : :
2609 : : /* Find the symbol table(s). For this we have to search through the
2610 : : section table. */
2611 : : Elf_Scn *scn = NULL;
2612 : : bool symtab_printed = false;
2613 : :
2614 [ + + ]: 14056 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
2615 : : {
2616 : : /* Handle the section if it is a symbol table. */
2617 : 13618 : GElf_Shdr shdr_mem;
2618 : 13618 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
2619 : :
2620 [ + - + + ]: 13618 : if (shdr != NULL && shdr->sh_type == (GElf_Word) type)
2621 : : {
2622 [ + + ]: 274 : if (symbol_table_section != NULL)
2623 : : {
2624 : : /* Get the section header string table index. */
2625 : 14 : size_t shstrndx;
2626 : 14 : const char *sname;
2627 [ - + ]: 14 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
2628 : 0 : error_exit (0,
2629 : : _("cannot get section header string table index"));
2630 : 14 : sname = elf_strptr (ebl->elf, shstrndx, shdr->sh_name);
2631 [ + - + + ]: 14 : if (sname == NULL || strcmp (sname, symbol_table_section) != 0)
2632 : 8 : continue;
2633 : : }
2634 : :
2635 [ - + ]: 266 : if ((shdr->sh_flags & SHF_COMPRESSED) != 0)
2636 : : {
2637 [ # # ]: 0 : if (elf_compress (scn, 0, 0) < 0)
2638 : 0 : printf ("WARNING: %s [%zd]\n",
2639 : : _("Couldn't uncompress section"),
2640 : : elf_ndxscn (scn));
2641 : 0 : shdr = gelf_getshdr (scn, &shdr_mem);
2642 [ # # ]: 0 : if (unlikely (shdr == NULL))
2643 : 0 : error_exit (0,
2644 : : _("cannot get section [%zd] header: %s"),
2645 : : elf_ndxscn (scn), elf_errmsg (-1));
2646 : : }
2647 : 266 : symtab_printed = handle_symtab (ebl, scn, shdr);
2648 : : }
2649 : : }
2650 : :
2651 : : return symtab_printed;
2652 : : }
2653 : :
2654 : :
2655 : : static void
2656 : 266 : process_symtab (Ebl *ebl, unsigned int nsyms, Elf64_Word idx,
2657 : : Elf32_Word verneed_stridx, Elf32_Word verdef_stridx,
2658 : : Elf_Data *symdata, Elf_Data *versym_data,
2659 : : Elf_Data *symstr_data, Elf_Data *verneed_data,
2660 : : Elf_Data *verdef_data, Elf_Data *xndx_data)
2661 : : {
2662 [ + + ]: 79066 : for (unsigned int cnt = 0; cnt < nsyms; ++cnt)
2663 : : {
2664 : 78800 : char typebuf[64];
2665 : 78800 : char bindbuf[64];
2666 : 78800 : char scnbuf[64];
2667 : 78800 : const char *sym_name;
2668 : 78800 : Elf32_Word xndx;
2669 : 78800 : GElf_Sym sym_mem;
2670 : 78800 : GElf_Sym *sym
2671 : 78800 : = gelf_getsymshndx (symdata, xndx_data, cnt, &sym_mem, &xndx);
2672 : :
2673 [ - + ]: 78800 : if (unlikely (sym == NULL))
2674 : 0 : continue;
2675 : :
2676 : : /* Determine the real section index. */
2677 [ + - ]: 78800 : if (likely (sym->st_shndx != SHN_XINDEX))
2678 : 78800 : xndx = sym->st_shndx;
2679 [ - + ]: 78800 : if (use_dynamic_segment == true)
2680 : : {
2681 [ # # ]: 0 : if (validate_str (symstr_data->d_buf, sym->st_name,
2682 : : symstr_data->d_size))
2683 : 0 : sym_name = (char *)symstr_data->d_buf + sym->st_name;
2684 : : else
2685 : : sym_name = NULL;
2686 : : }
2687 : : else
2688 : 78800 : sym_name = elf_strptr (ebl->elf, idx, sym->st_name);
2689 : :
2690 [ - + ]: 78800 : if (sym_name == NULL)
2691 : : sym_name = "???";
2692 : :
2693 [ + + ]: 156712 : printf (_ ("\
2694 : : %5u: %0*" PRIx64 " %6" PRId64 " %-7s %-6s %-9s %6s %s"),
2695 : 78800 : cnt, gelf_getclass (ebl->elf) == ELFCLASS32 ? 8 : 16,
2696 : : sym->st_value, sym->st_size,
2697 : 78800 : ebl_symbol_type_name (ebl, GELF_ST_TYPE (sym->st_info), typebuf,
2698 : : sizeof (typebuf)),
2699 : 78800 : ebl_symbol_binding_name (ebl, GELF_ST_BIND (sym->st_info),
2700 : : bindbuf, sizeof (bindbuf)),
2701 : 78800 : get_visibility_type (GELF_ST_VISIBILITY (sym->st_other)),
2702 : 78800 : ebl_section_name (ebl, sym->st_shndx, xndx, scnbuf,
2703 : : sizeof (scnbuf), NULL, shnum),
2704 : : sym_name);
2705 : :
2706 [ + + ]: 78800 : if (versym_data != NULL)
2707 : : {
2708 : : /* Get the version information. */
2709 : 3420 : GElf_Versym versym_mem;
2710 : 3420 : GElf_Versym *versym = gelf_getversym (versym_data, cnt, &versym_mem);
2711 : :
2712 [ + - + + ]: 3420 : if (versym != NULL && ((*versym & 0x8000) != 0 || *versym > 1))
2713 : : {
2714 : 2460 : bool is_nobits = false;
2715 : 2460 : bool check_def = xndx != SHN_UNDEF;
2716 : :
2717 [ + + - + ]: 2460 : if (xndx < SHN_LORESERVE || sym->st_shndx == SHN_XINDEX)
2718 : : {
2719 : 2440 : GElf_Shdr symshdr_mem;
2720 : 2440 : GElf_Shdr *symshdr = gelf_getshdr (
2721 : : elf_getscn (ebl->elf, xndx), &symshdr_mem);
2722 : :
2723 : 2440 : is_nobits
2724 [ + - + + ]: 4856 : = (symshdr != NULL && symshdr->sh_type == SHT_NOBITS);
2725 : : }
2726 : :
2727 [ + + ]: 2460 : if (is_nobits || !check_def)
2728 : : {
2729 : : /* We must test both. */
2730 : 2156 : GElf_Vernaux vernaux_mem;
2731 : 2156 : GElf_Vernaux *vernaux = NULL;
2732 : 2156 : size_t vn_offset = 0;
2733 : :
2734 : 2156 : GElf_Verneed verneed_mem;
2735 : 2156 : GElf_Verneed *verneed
2736 : 2156 : = gelf_getverneed (verneed_data, 0, &verneed_mem);
2737 [ + - ]: 7838 : while (verneed != NULL)
2738 : : {
2739 : 7838 : size_t vna_offset = vn_offset;
2740 : :
2741 : 7838 : vernaux = gelf_getvernaux (verneed_data,
2742 : 7838 : vna_offset += verneed->vn_aux,
2743 : : &vernaux_mem);
2744 [ + + ]: 28170 : while (vernaux != NULL && vernaux->vna_other != *versym
2745 [ + + ]: 18176 : && vernaux->vna_next != 0
2746 [ + - ]: 20332 : && (verneed_data->d_size - vna_offset
2747 [ + - ]: 12494 : >= vernaux->vna_next))
2748 : : {
2749 : : /* Update the offset. */
2750 : 12494 : vna_offset += vernaux->vna_next;
2751 : :
2752 : 12494 : vernaux = (vernaux->vna_next == 0
2753 : : ? NULL
2754 : 12494 : : gelf_getvernaux (verneed_data,
2755 : : vna_offset,
2756 : : &vernaux_mem));
2757 : : }
2758 : :
2759 : : /* Check whether we found the version. */
2760 [ + - + + ]: 7838 : if (vernaux != NULL && vernaux->vna_other == *versym)
2761 : : /* Found it. */
2762 : : break;
2763 : :
2764 [ + - ]: 5682 : if (verneed_data->d_size - vn_offset < verneed->vn_next)
2765 : : break;
2766 : :
2767 : 5682 : vn_offset += verneed->vn_next;
2768 : 5682 : verneed
2769 : : = (verneed->vn_next == 0
2770 : : ? NULL
2771 [ + - ]: 5682 : : gelf_getverneed (verneed_data, vn_offset,
2772 : : &verneed_mem));
2773 : : }
2774 : :
2775 [ + - + - ]: 2156 : if (vernaux != NULL && vernaux->vna_other == *versym)
2776 : : {
2777 : 4312 : printf ("@%s (%u)",
2778 [ - + ]: 2156 : use_dynamic_segment == true
2779 : 0 : ? (char *)symstr_data->d_buf
2780 : 0 : + vernaux->vna_name
2781 : 2156 : : elf_strptr (ebl->elf, verneed_stridx,
2782 : 2156 : vernaux->vna_name),
2783 : : (unsigned int)vernaux->vna_other);
2784 : 2156 : check_def = 0;
2785 : : }
2786 [ # # ]: 0 : else if (unlikely (!is_nobits))
2787 : 0 : error (0, 0, _ ("bad dynamic symbol"));
2788 : : else
2789 : : check_def = 1;
2790 : : }
2791 : :
2792 [ + - + - ]: 2460 : if (check_def && *versym != 0x8001)
2793 : : {
2794 : : /* We must test both. */
2795 : 304 : size_t vd_offset = 0;
2796 : :
2797 : 304 : GElf_Verdef verdef_mem;
2798 : 304 : GElf_Verdef *verdef
2799 : 304 : = gelf_getverdef (verdef_data, 0, &verdef_mem);
2800 [ + - ]: 866 : while (verdef != NULL)
2801 : : {
2802 [ + + ]: 866 : if (verdef->vd_ndx == (*versym & 0x7fff))
2803 : : /* Found the definition. */
2804 : : break;
2805 : :
2806 [ + - ]: 562 : if (verdef_data->d_size - vd_offset < verdef->vd_next)
2807 : : break;
2808 : :
2809 : 562 : vd_offset += verdef->vd_next;
2810 : 562 : verdef = (verdef->vd_next == 0
2811 : : ? NULL
2812 [ + - ]: 562 : : gelf_getverdef (verdef_data, vd_offset,
2813 : : &verdef_mem));
2814 : : }
2815 : :
2816 [ + - ]: 304 : if (verdef != NULL)
2817 : : {
2818 : 304 : GElf_Verdaux verdaux_mem;
2819 : 608 : GElf_Verdaux *verdaux = gelf_getverdaux (
2820 : 304 : verdef_data, vd_offset + verdef->vd_aux,
2821 : : &verdaux_mem);
2822 : :
2823 [ + - ]: 304 : if (verdaux != NULL)
2824 [ + - ]: 608 : printf ((*versym & 0x8000) ? "@%s" : "@@%s",
2825 [ - + ]: 304 : use_dynamic_segment == true
2826 : 0 : ? (char *)symstr_data->d_buf
2827 : 0 : + verdaux->vda_name
2828 : 304 : : elf_strptr (ebl->elf, verdef_stridx,
2829 : 304 : verdaux->vda_name));
2830 : : }
2831 : : }
2832 : : }
2833 : : }
2834 : :
2835 : 78800 : putchar ('\n');
2836 : : }
2837 : 266 : }
2838 : :
2839 : :
2840 : : static bool
2841 : 266 : handle_symtab (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr)
2842 : : {
2843 : 266 : Elf_Data *versym_data = NULL;
2844 : 266 : Elf_Data *verneed_data = NULL;
2845 : 266 : Elf_Data *verdef_data = NULL;
2846 : 266 : Elf_Data *xndx_data = NULL;
2847 : 266 : int class = gelf_getclass (ebl->elf);
2848 : 266 : Elf32_Word verneed_stridx = 0;
2849 : 266 : Elf32_Word verdef_stridx = 0;
2850 : :
2851 : : /* Get the data of the section. */
2852 : 266 : Elf_Data *data = elf_getdata (scn, NULL);
2853 [ + - ]: 266 : if (data == NULL)
2854 : : return false;
2855 : :
2856 : : /* Find out whether we have other sections we might need. */
2857 : : Elf_Scn *runscn = NULL;
2858 [ + + ]: 8784 : while ((runscn = elf_nextscn (ebl->elf, runscn)) != NULL)
2859 : : {
2860 : 8518 : GElf_Shdr runshdr_mem;
2861 : 8518 : GElf_Shdr *runshdr = gelf_getshdr (runscn, &runshdr_mem);
2862 : :
2863 [ + - ]: 8518 : if (likely (runshdr != NULL))
2864 : : {
2865 [ + + ]: 8518 : if (runshdr->sh_type == SHT_GNU_versym
2866 [ + + ]: 202 : && runshdr->sh_link == elf_ndxscn (scn))
2867 : : /* Bingo, found the version information. Now get the data. */
2868 : 150 : versym_data = elf_getdata (runscn, NULL);
2869 [ + + ]: 8368 : else if (runshdr->sh_type == SHT_GNU_verneed)
2870 : : {
2871 : : /* This is the information about the needed versions. */
2872 : 202 : verneed_data = elf_getdata (runscn, NULL);
2873 : 202 : verneed_stridx = runshdr->sh_link;
2874 : : }
2875 [ + + ]: 8166 : else if (runshdr->sh_type == SHT_GNU_verdef)
2876 : : {
2877 : : /* This is the information about the defined versions. */
2878 : 8 : verdef_data = elf_getdata (runscn, NULL);
2879 : 8 : verdef_stridx = runshdr->sh_link;
2880 : : }
2881 [ + - ]: 8158 : else if (runshdr->sh_type == SHT_SYMTAB_SHNDX
2882 [ # # ]: 0 : && runshdr->sh_link == elf_ndxscn (scn))
2883 : : /* Extended section index. */
2884 : 0 : xndx_data = elf_getdata (runscn, NULL);
2885 : : }
2886 : : }
2887 : :
2888 : : /* Get the section header string table index. */
2889 : 266 : size_t shstrndx;
2890 [ - + ]: 266 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
2891 : 0 : error_exit (0, _("cannot get section header string table index"));
2892 : :
2893 : 266 : GElf_Shdr glink_mem;
2894 : 266 : GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
2895 : : &glink_mem);
2896 [ - + ]: 266 : if (glink == NULL)
2897 : 0 : error_exit (0, _("invalid sh_link value in section %zu"),
2898 : : elf_ndxscn (scn));
2899 : :
2900 : : /* Now we can compute the number of entries in the section. */
2901 : 532 : unsigned int nsyms = data->d_size / (class == ELFCLASS32
2902 : : ? sizeof (Elf32_Sym)
2903 [ + + ]: 266 : : sizeof (Elf64_Sym));
2904 : :
2905 : 266 : printf (ngettext ("\nSymbol table [%2u] '%s' contains %u entry:\n",
2906 : : "\nSymbol table [%2u] '%s' contains %u entries:\n",
2907 : : nsyms),
2908 : 266 : (unsigned int) elf_ndxscn (scn),
2909 : 266 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name), nsyms);
2910 : 266 : printf (ngettext (" %lu local symbol String table: [%2u] '%s'\n",
2911 : : " %lu local symbols String table: [%2u] '%s'\n",
2912 : : shdr->sh_info),
2913 : 266 : (unsigned long int) shdr->sh_info,
2914 : 266 : (unsigned int) shdr->sh_link,
2915 : 266 : elf_strptr (ebl->elf, shstrndx, glink->sh_name));
2916 : :
2917 [ + + ]: 532 : fputs (class == ELFCLASS32
2918 : 32 : ? _("\
2919 : : Num: Value Size Type Bind Vis Ndx Name\n")
2920 : 234 : : _("\
2921 : : Num: Value Size Type Bind Vis Ndx Name\n"),
2922 : : stdout);
2923 : :
2924 : 266 : process_symtab(ebl, nsyms, shdr->sh_link, verneed_stridx, verdef_stridx,
2925 : : data, versym_data, NULL, verneed_data, verdef_data, xndx_data);
2926 : 266 : return true;
2927 : : }
2928 : :
2929 : :
2930 : : static bool
2931 : 0 : handle_dynamic_symtab (Ebl *ebl)
2932 : : {
2933 : 0 : GElf_Phdr phdr_mem;
2934 : 0 : GElf_Phdr *phdr = NULL;
2935 : : /* phnum is a static variable which was already fetched in function
2936 : : process_elf_file. */
2937 [ # # ]: 0 : for (size_t i = 0; i < phnum; ++i)
2938 : : {
2939 : 0 : phdr = gelf_getphdr (ebl->elf, i, &phdr_mem);
2940 [ # # # # ]: 0 : if (phdr == NULL || phdr->p_type == PT_DYNAMIC)
2941 : : break;
2942 : : }
2943 [ # # ]: 0 : if (phdr == NULL)
2944 : : return false;
2945 : :
2946 : 0 : GElf_Addr addrs[i_max] = {
2947 : : 0,
2948 : : };
2949 : 0 : GElf_Off offs[i_max] = {
2950 : : 0,
2951 : : };
2952 : 0 : get_dynscn_addrs (ebl->elf, phdr, addrs);
2953 : 0 : find_offsets (ebl->elf, 0, i_max, addrs, offs);
2954 : :
2955 : 0 : size_t syments = 0;
2956 : :
2957 : 0 : GElf_Ehdr ehdr_mem;
2958 : 0 : GElf_Ehdr *ehdr = gelf_getehdr (ebl->elf, &ehdr_mem);
2959 : :
2960 [ # # ]: 0 : if (offs[i_hash] != 0)
2961 : : {
2962 : : /* In the original format, .hash says the size of .dynsym. */
2963 : :
2964 [ # # # # : 0 : size_t entsz = SH_ENTSIZE_HASH (ehdr);
# # ]
2965 : : Elf_Data *data
2966 : 0 : = elf_getdata_rawchunk (ebl->elf, offs[i_hash] + entsz, entsz,
2967 : : (entsz == 4 ? ELF_T_WORD : ELF_T_XWORD));
2968 [ # # ]: 0 : if (data != NULL)
2969 : 0 : syments = (entsz == 4 ? *(const GElf_Word *)data->d_buf
2970 [ # # ]: 0 : : *(const GElf_Xword *)data->d_buf);
2971 : : }
2972 [ # # # # ]: 0 : if (offs[i_gnu_hash] != 0 && syments == 0)
2973 : : {
2974 : : /* In the new format, we can derive it with some work. */
2975 : :
2976 : 0 : const struct
2977 : : {
2978 : : Elf32_Word nbuckets;
2979 : : Elf32_Word symndx;
2980 : : Elf32_Word maskwords;
2981 : : Elf32_Word shift2;
2982 : : } * header;
2983 : :
2984 : 0 : Elf_Data *data = elf_getdata_rawchunk (ebl->elf, offs[i_gnu_hash],
2985 : : sizeof *header, ELF_T_WORD);
2986 [ # # ]: 0 : if (data != NULL)
2987 : : {
2988 : 0 : header = data->d_buf;
2989 : 0 : Elf32_Word nbuckets = header->nbuckets;
2990 : 0 : Elf32_Word symndx = header->symndx;
2991 : 0 : GElf_Off buckets_at
2992 : : = (offs[i_gnu_hash] + sizeof *header
2993 : 0 : + (gelf_getclass (ebl->elf) * sizeof (Elf32_Word)
2994 : 0 : * header->maskwords));
2995 : :
2996 : : // elf_getdata_rawchunk takes a size_t, make sure it
2997 : : // doesn't overflow.
2998 : : #if SIZE_MAX <= UINT32_MAX
2999 : : if (nbuckets > SIZE_MAX / sizeof (Elf32_Word))
3000 : : data = NULL;
3001 : : else
3002 : : #endif
3003 : 0 : data = elf_getdata_rawchunk (ebl->elf, buckets_at,
3004 : : nbuckets * sizeof (Elf32_Word),
3005 : : ELF_T_WORD);
3006 [ # # ]: 0 : if (data != NULL && symndx < nbuckets)
3007 : : {
3008 : 0 : const Elf32_Word *const buckets = data->d_buf;
3009 : 0 : Elf32_Word maxndx = symndx;
3010 [ # # ]: 0 : for (Elf32_Word bucket = 0; bucket < nbuckets; ++bucket)
3011 : 0 : if (buckets[bucket] > maxndx)
3012 : : maxndx = buckets[bucket];
3013 : :
3014 : 0 : GElf_Off hasharr_at
3015 : : = (buckets_at + nbuckets * sizeof (Elf32_Word));
3016 : 0 : hasharr_at += (maxndx - symndx) * sizeof (Elf32_Word);
3017 : 0 : do
3018 : : {
3019 : 0 : data = elf_getdata_rawchunk (
3020 : : ebl->elf, hasharr_at, sizeof (Elf32_Word), ELF_T_WORD);
3021 [ # # # # ]: 0 : if (data != NULL && (*(const Elf32_Word *)data->d_buf & 1u))
3022 : : {
3023 : 0 : syments = maxndx + 1;
3024 : 0 : break;
3025 : : }
3026 : 0 : ++maxndx;
3027 : 0 : hasharr_at += sizeof (Elf32_Word);
3028 : : }
3029 [ # # ]: 0 : while (data != NULL);
3030 : : }
3031 : : }
3032 : : }
3033 [ # # # # ]: 0 : if (offs[i_strtab] > offs[i_symtab] && syments == 0)
3034 : 0 : syments = ((offs[i_strtab] - offs[i_symtab])
3035 : 0 : / gelf_fsize (ebl->elf, ELF_T_SYM, 1, EV_CURRENT));
3036 : :
3037 [ # # # # : 0 : if (syments <= 0 || offs[i_strtab] == 0 || offs[i_symtab] == 0)
# # ]
3038 : : {
3039 : 0 : error_exit (0, _ ("Dynamic symbol information is not available for "
3040 : : "displaying symbols."));
3041 : : }
3042 : :
3043 : : /* All the data chunk initializaion. */
3044 : 0 : Elf_Data *symdata = NULL;
3045 : 0 : Elf_Data *symstrdata = NULL;
3046 : 0 : Elf_Data *versym_data = NULL;
3047 : 0 : Elf_Data *verdef_data = NULL;
3048 : 0 : Elf_Data *verneed_data = NULL;
3049 : :
3050 : 0 : if (offs[i_symtab] != 0)
3051 : 0 : symdata = elf_getdata_rawchunk (
3052 : : ebl->elf, offs[i_symtab],
3053 : : gelf_fsize (ebl->elf, ELF_T_SYM, syments, EV_CURRENT), ELF_T_SYM);
3054 : :
3055 [ # # ]: 0 : if (offs[i_strtab] != 0 && addrs[i_strsz] != 0)
3056 : 0 : symstrdata = elf_getdata_rawchunk (ebl->elf, offs[i_strtab], addrs[i_strsz],
3057 : : ELF_T_BYTE);
3058 : :
3059 [ # # ]: 0 : if (offs[i_versym] != 0)
3060 : 0 : versym_data = elf_getdata_rawchunk (
3061 : : ebl->elf, offs[i_versym], syments * sizeof (Elf64_Half), ELF_T_HALF);
3062 : :
3063 : : /* Get the verneed_data without vernaux. */
3064 [ # # # # ]: 0 : if (offs[i_verneed] != 0 && addrs[i_verneednum] != 0)
3065 : : {
3066 : 0 : verneed_data = elf_getdata_rawchunk (
3067 : : ebl->elf, offs[i_verneed], addrs[i_verneednum] * sizeof (Elf64_Verneed),
3068 : : ELF_T_VNEED);
3069 : :
3070 [ # # ]: 0 : if (verneed_data->d_size < sizeof (GElf_Verneed))
3071 : 0 : error_exit (0, _("malformed SHT_GNU_verneed data"));
3072 : : }
3073 : :
3074 : 0 : size_t vernauxnum = 0;
3075 : 0 : size_t vn_next_offset = 0;
3076 : :
3077 [ # # ]: 0 : if (verneed_data != NULL && verneed_data->d_buf != NULL)
3078 [ # # ]: 0 : for (size_t i = 0; i < addrs[i_verneednum]; i++)
3079 : : {
3080 [ # # ]: 0 : if (vn_next_offset > (verneed_data->d_size - sizeof (GElf_Verneed)))
3081 : 0 : error_exit (0, _("invalid SHT_GNU_verneed data"));
3082 : :
3083 : 0 : GElf_Verneed *verneed
3084 : : = (GElf_Verneed *)(verneed_data->d_buf + vn_next_offset);
3085 : 0 : vernauxnum += verneed->vn_cnt;
3086 : 0 : vn_next_offset += verneed->vn_next;
3087 : : }
3088 : :
3089 : : /* Update the verneed_data to include the vernaux. */
3090 [ # # # # ]: 0 : if (offs[i_verneed] != 0 && addrs[i_verneednum] != 0)
3091 : 0 : verneed_data = elf_getdata_rawchunk (
3092 : : ebl->elf, offs[i_verneed],
3093 : 0 : (addrs[i_verneednum] + vernauxnum) * sizeof (GElf_Verneed),
3094 : : ELF_T_VNEED);
3095 : :
3096 : : /* Get the verdef_data without verdaux. */
3097 [ # # # # ]: 0 : if (offs[i_verdef] != 0 && addrs[i_verdefnum] != 0)
3098 : : {
3099 : 0 : verdef_data = elf_getdata_rawchunk (
3100 : : ebl->elf, offs[i_verdef], addrs[i_verdefnum] * sizeof (Elf64_Verdef),
3101 : : ELF_T_VDEF);
3102 : :
3103 [ # # ]: 0 : if (verdef_data->d_size < sizeof (GElf_Verdef))
3104 : 0 : error_exit (0, _("malformed SHT_GNU_verdef data"));
3105 : : }
3106 : :
3107 : 0 : size_t verdauxnum = 0;
3108 : 0 : size_t vd_next_offset = 0;
3109 : :
3110 [ # # ]: 0 : if (verdef_data != NULL && verdef_data->d_buf != NULL)
3111 [ # # ]: 0 : for (size_t i = 0; i < addrs[i_verdefnum]; i++)
3112 : : {
3113 [ # # ]: 0 : if (vd_next_offset > (verdef_data->d_size - sizeof (GElf_Verdef)))
3114 : 0 : error_exit (0, _("invalid SHT_GNU_verdef data"));
3115 : :
3116 : 0 : GElf_Verdef *verdef
3117 : : = (GElf_Verdef *)(verdef_data->d_buf + vd_next_offset);
3118 : 0 : verdauxnum += verdef->vd_cnt;
3119 : 0 : vd_next_offset += verdef->vd_next;
3120 : : }
3121 : :
3122 : : /* Update the verdef_data to include the verdaux. */
3123 [ # # # # ]: 0 : if (offs[i_verdef] != 0 && addrs[i_verdefnum] != 0)
3124 : 0 : verdef_data = elf_getdata_rawchunk (
3125 : : ebl->elf, offs[i_verdef],
3126 : 0 : (addrs[i_verdefnum] + verdauxnum) * sizeof (GElf_Verdef), ELF_T_VDEF);
3127 : :
3128 : 0 : unsigned int nsyms = (unsigned int)syments;
3129 : 0 : process_symtab (ebl, nsyms, 0, 0, 0, symdata, versym_data, symstrdata,
3130 : : verneed_data, verdef_data, NULL);
3131 : 0 : return true;
3132 : : }
3133 : :
3134 : :
3135 : : /* Print version information. */
3136 : : static void
3137 : 184 : print_verinfo (Ebl *ebl)
3138 : : {
3139 : : /* Find the version information sections. For this we have to
3140 : : search through the section table. */
3141 : 184 : Elf_Scn *scn = NULL;
3142 : :
3143 [ + + ]: 6004 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
3144 : : {
3145 : : /* Handle the section if it is part of the versioning handling. */
3146 : 5820 : GElf_Shdr shdr_mem;
3147 : 5820 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
3148 : :
3149 [ + - ]: 5820 : if (likely (shdr != NULL))
3150 : : {
3151 [ + + ]: 5820 : if (shdr->sh_type == SHT_GNU_verneed)
3152 : 130 : handle_verneed (ebl, scn, shdr);
3153 [ + + ]: 5690 : else if (shdr->sh_type == SHT_GNU_verdef)
3154 : 4 : handle_verdef (ebl, scn, shdr);
3155 [ + + ]: 5686 : else if (shdr->sh_type == SHT_GNU_versym)
3156 : 130 : handle_versym (ebl, scn, shdr);
3157 : : }
3158 : : }
3159 : 184 : }
3160 : :
3161 : :
3162 : : static const char *
3163 : 374 : get_ver_flags (unsigned int flags)
3164 : : {
3165 : 374 : static char buf[32];
3166 : 374 : char *endp;
3167 : :
3168 [ + + ]: 374 : if (flags == 0)
3169 : 370 : return _("none");
3170 : :
3171 [ + - ]: 4 : if (flags & VER_FLG_BASE)
3172 : 4 : endp = stpcpy (buf, "BASE ");
3173 : : else
3174 : : endp = buf;
3175 : :
3176 [ - + ]: 4 : if (flags & VER_FLG_WEAK)
3177 : : {
3178 [ # # ]: 0 : if (endp != buf)
3179 : 0 : endp = stpcpy (endp, "| ");
3180 : :
3181 : 0 : endp = stpcpy (endp, "WEAK ");
3182 : : }
3183 : :
3184 [ - + ]: 4 : if (unlikely (flags & ~(VER_FLG_BASE | VER_FLG_WEAK)))
3185 : : {
3186 : 0 : strncpy (endp, _("| <unknown>"), buf + sizeof (buf) - endp);
3187 : 0 : buf[sizeof (buf) - 1] = '\0';
3188 : : }
3189 : :
3190 : : return buf;
3191 : : }
3192 : :
3193 : :
3194 : : static void
3195 : 130 : handle_verneed (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr)
3196 : : {
3197 : 130 : int class = gelf_getclass (ebl->elf);
3198 : :
3199 : : /* Get the data of the section. */
3200 : 130 : Elf_Data *data = elf_getdata (scn, NULL);
3201 [ - + ]: 130 : if (data == NULL)
3202 : 0 : return;
3203 : :
3204 : : /* Get the section header string table index. */
3205 : 130 : size_t shstrndx;
3206 [ - + ]: 130 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
3207 : 0 : error_exit (0, _("cannot get section header string table index"));
3208 : :
3209 : 130 : GElf_Shdr glink_mem;
3210 : 130 : GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
3211 : : &glink_mem);
3212 [ - + ]: 130 : if (glink == NULL)
3213 : 0 : error_exit (0, _("invalid sh_link value in section %zu"),
3214 : : elf_ndxscn (scn));
3215 : :
3216 : 130 : printf (ngettext ("\
3217 : : \nVersion needs section [%2u] '%s' contains %d entry:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'\n",
3218 : : "\
3219 : : \nVersion needs section [%2u] '%s' contains %d entries:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'\n",
3220 : : shdr->sh_info),
3221 : 130 : (unsigned int) elf_ndxscn (scn),
3222 : 130 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name), shdr->sh_info,
3223 : : class == ELFCLASS32 ? 10 : 18, shdr->sh_addr,
3224 : : shdr->sh_offset,
3225 [ + + ]: 130 : (unsigned int) shdr->sh_link,
3226 : 130 : elf_strptr (ebl->elf, shstrndx, glink->sh_name));
3227 : :
3228 : 130 : unsigned int offset = 0;
3229 [ + - ]: 184 : for (unsigned int cnt = shdr->sh_info; cnt > 0; cnt--)
3230 : : {
3231 : : /* Get the data at the next offset. */
3232 : 184 : GElf_Verneed needmem;
3233 : 184 : GElf_Verneed *need = gelf_getverneed (data, offset, &needmem);
3234 [ + - ]: 184 : if (unlikely (need == NULL))
3235 : : break;
3236 : :
3237 : 552 : printf (_(" %#06x: Version: %hu File: %s Cnt: %hu\n"),
3238 : 184 : offset, (unsigned short int) need->vn_version,
3239 : 184 : elf_strptr (ebl->elf, shdr->sh_link, need->vn_file),
3240 : 184 : (unsigned short int) need->vn_cnt);
3241 : :
3242 : 184 : unsigned int auxoffset = offset + need->vn_aux;
3243 [ + - ]: 350 : for (unsigned int cnt2 = need->vn_cnt; cnt2 > 0; cnt2--)
3244 : : {
3245 : 350 : GElf_Vernaux auxmem;
3246 : 350 : GElf_Vernaux *aux = gelf_getvernaux (data, auxoffset, &auxmem);
3247 [ + - ]: 350 : if (unlikely (aux == NULL))
3248 : : break;
3249 : :
3250 : 350 : printf (_(" %#06x: Name: %s Flags: %s Version: %hu\n"),
3251 : : auxoffset,
3252 : 350 : elf_strptr (ebl->elf, shdr->sh_link, aux->vna_name),
3253 : 350 : get_ver_flags (aux->vna_flags),
3254 : 350 : (unsigned short int) aux->vna_other);
3255 : :
3256 [ + + ]: 350 : if (aux->vna_next == 0)
3257 : : break;
3258 : :
3259 : 166 : auxoffset += aux->vna_next;
3260 : : }
3261 : :
3262 : : /* Find the next offset. */
3263 [ + + ]: 184 : if (need->vn_next == 0)
3264 : : break;
3265 : :
3266 : 54 : offset += need->vn_next;
3267 : : }
3268 : : }
3269 : :
3270 : :
3271 : : static void
3272 : 4 : handle_verdef (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr)
3273 : : {
3274 : : /* Get the data of the section. */
3275 : 4 : Elf_Data *data = elf_getdata (scn, NULL);
3276 [ - + ]: 4 : if (data == NULL)
3277 : 0 : return;
3278 : :
3279 : : /* Get the section header string table index. */
3280 : 4 : size_t shstrndx;
3281 [ - + ]: 4 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
3282 : 0 : error_exit (0, _("cannot get section header string table index"));
3283 : :
3284 : 4 : GElf_Shdr glink_mem;
3285 : 4 : GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
3286 : : &glink_mem);
3287 [ - + ]: 4 : if (glink == NULL)
3288 : 0 : error_exit (0, _("invalid sh_link value in section %zu"),
3289 : : elf_ndxscn (scn));
3290 : :
3291 : 4 : int class = gelf_getclass (ebl->elf);
3292 : 4 : printf (ngettext ("\
3293 : : \nVersion definition section [%2u] '%s' contains %d entry:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'\n",
3294 : : "\
3295 : : \nVersion definition section [%2u] '%s' contains %d entries:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'\n",
3296 : : shdr->sh_info),
3297 : 4 : (unsigned int) elf_ndxscn (scn),
3298 : 4 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
3299 : : shdr->sh_info,
3300 : : class == ELFCLASS32 ? 10 : 18, shdr->sh_addr,
3301 : : shdr->sh_offset,
3302 [ + - ]: 4 : (unsigned int) shdr->sh_link,
3303 : 4 : elf_strptr (ebl->elf, shstrndx, glink->sh_name));
3304 : :
3305 : 4 : unsigned int offset = 0;
3306 [ + - ]: 24 : for (unsigned int cnt = shdr->sh_info; cnt > 0; cnt--)
3307 : : {
3308 : : /* Get the data at the next offset. */
3309 : 24 : GElf_Verdef defmem;
3310 : 24 : GElf_Verdef *def = gelf_getverdef (data, offset, &defmem);
3311 [ + - ]: 24 : if (unlikely (def == NULL))
3312 : : break;
3313 : :
3314 : 24 : unsigned int auxoffset = offset + def->vd_aux;
3315 : 24 : GElf_Verdaux auxmem;
3316 : 24 : GElf_Verdaux *aux = gelf_getverdaux (data, auxoffset, &auxmem);
3317 [ + - ]: 24 : if (unlikely (aux == NULL))
3318 : : break;
3319 : :
3320 : 96 : printf (_("\
3321 : : %#06x: Version: %hd Flags: %s Index: %hd Cnt: %hd Name: %s\n"),
3322 : 24 : offset, def->vd_version,
3323 : 24 : get_ver_flags (def->vd_flags),
3324 : 24 : def->vd_ndx,
3325 : 24 : def->vd_cnt,
3326 : 24 : elf_strptr (ebl->elf, shdr->sh_link, aux->vda_name));
3327 : :
3328 : 24 : auxoffset += aux->vda_next;
3329 [ + + ]: 24 : for (unsigned int cnt2 = 1; cnt2 < def->vd_cnt; ++cnt2)
3330 : : {
3331 : 16 : aux = gelf_getverdaux (data, auxoffset, &auxmem);
3332 [ + - ]: 16 : if (unlikely (aux == NULL))
3333 : : break;
3334 : :
3335 : 16 : printf (_(" %#06x: Parent %d: %s\n"),
3336 : : auxoffset, cnt2,
3337 : 16 : elf_strptr (ebl->elf, shdr->sh_link, aux->vda_name));
3338 : :
3339 [ - + ]: 16 : if (aux->vda_next == 0)
3340 : : break;
3341 : :
3342 : 0 : auxoffset += aux->vda_next;
3343 : : }
3344 : :
3345 : : /* Find the next offset. */
3346 [ + + ]: 24 : if (def->vd_next == 0)
3347 : : break;
3348 : 20 : offset += def->vd_next;
3349 : : }
3350 : : }
3351 : :
3352 : :
3353 : : static void
3354 : 130 : handle_versym (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr)
3355 : : {
3356 : 130 : int class = gelf_getclass (ebl->elf);
3357 : 130 : const char **vername;
3358 : 130 : const char **filename;
3359 : :
3360 : : /* Get the data of the section. */
3361 : 130 : Elf_Data *data = elf_getdata (scn, NULL);
3362 [ + - ]: 130 : if (data == NULL)
3363 : 0 : return;
3364 : :
3365 : : /* Get the section header string table index. */
3366 : 130 : size_t shstrndx;
3367 [ - + ]: 130 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
3368 : 0 : error_exit (0, _("cannot get section header string table index"));
3369 : :
3370 : : /* We have to find the version definition section and extract the
3371 : : version names. */
3372 : : Elf_Scn *defscn = NULL;
3373 : : Elf_Scn *needscn = NULL;
3374 : :
3375 : : Elf_Scn *verscn = NULL;
3376 [ + + ]: 4046 : while ((verscn = elf_nextscn (ebl->elf, verscn)) != NULL)
3377 : : {
3378 : 3916 : GElf_Shdr vershdr_mem;
3379 : 3916 : GElf_Shdr *vershdr = gelf_getshdr (verscn, &vershdr_mem);
3380 : :
3381 [ - + ]: 3916 : if (likely (vershdr != NULL))
3382 : : {
3383 [ + + ]: 3916 : if (vershdr->sh_type == SHT_GNU_verdef)
3384 : : defscn = verscn;
3385 [ + + ]: 3912 : else if (vershdr->sh_type == SHT_GNU_verneed)
3386 : 3916 : needscn = verscn;
3387 : : }
3388 : : }
3389 : :
3390 : 130 : size_t nvername;
3391 [ + - ]: 130 : if (defscn != NULL || needscn != NULL)
3392 : : {
3393 : : /* We have a version information (better should have). Now get
3394 : : the version names. First find the maximum version number. */
3395 : 130 : nvername = 0;
3396 [ + + ]: 130 : if (defscn != NULL)
3397 : : {
3398 : : /* Run through the version definitions and find the highest
3399 : : index. */
3400 : 4 : unsigned int offset = 0;
3401 : 4 : Elf_Data *defdata;
3402 : 4 : GElf_Shdr defshdrmem;
3403 : 4 : GElf_Shdr *defshdr;
3404 : :
3405 : 4 : defdata = elf_getdata (defscn, NULL);
3406 [ + - ]: 4 : if (unlikely (defdata == NULL))
3407 : 0 : return;
3408 : :
3409 : 4 : defshdr = gelf_getshdr (defscn, &defshdrmem);
3410 [ + - ]: 4 : if (unlikely (defshdr == NULL))
3411 : : return;
3412 : :
3413 [ + - ]: 24 : for (unsigned int cnt = 0; cnt < defshdr->sh_info; ++cnt)
3414 : : {
3415 : 24 : GElf_Verdef defmem;
3416 : 24 : GElf_Verdef *def;
3417 : :
3418 : : /* Get the data at the next offset. */
3419 : 24 : def = gelf_getverdef (defdata, offset, &defmem);
3420 [ + - ]: 24 : if (unlikely (def == NULL))
3421 : : break;
3422 : :
3423 : 24 : nvername = MAX (nvername, (size_t) (def->vd_ndx & 0x7fff));
3424 : :
3425 [ + + ]: 24 : if (def->vd_next == 0)
3426 : : break;
3427 : 20 : offset += def->vd_next;
3428 : : }
3429 : : }
3430 [ + - ]: 130 : if (needscn != NULL)
3431 : : {
3432 : 130 : unsigned int offset = 0;
3433 : 130 : Elf_Data *needdata;
3434 : 130 : GElf_Shdr needshdrmem;
3435 : 130 : GElf_Shdr *needshdr;
3436 : :
3437 : 130 : needdata = elf_getdata (needscn, NULL);
3438 [ + - ]: 130 : if (unlikely (needdata == NULL))
3439 : 0 : return;
3440 : :
3441 : 130 : needshdr = gelf_getshdr (needscn, &needshdrmem);
3442 [ + - ]: 130 : if (unlikely (needshdr == NULL))
3443 : : return;
3444 : :
3445 [ + - ]: 184 : for (unsigned int cnt = 0; cnt < needshdr->sh_info; ++cnt)
3446 : : {
3447 : 184 : GElf_Verneed needmem;
3448 : 184 : GElf_Verneed *need;
3449 : 184 : unsigned int auxoffset;
3450 : 184 : int cnt2;
3451 : :
3452 : : /* Get the data at the next offset. */
3453 : 184 : need = gelf_getverneed (needdata, offset, &needmem);
3454 [ + - ]: 184 : if (unlikely (need == NULL))
3455 : : break;
3456 : :
3457 : : /* Run through the auxiliary entries. */
3458 : 184 : auxoffset = offset + need->vn_aux;
3459 [ + - ]: 350 : for (cnt2 = need->vn_cnt; --cnt2 >= 0; )
3460 : : {
3461 : 350 : GElf_Vernaux auxmem;
3462 : 350 : GElf_Vernaux *aux;
3463 : :
3464 : 350 : aux = gelf_getvernaux (needdata, auxoffset, &auxmem);
3465 [ + - ]: 350 : if (unlikely (aux == NULL))
3466 : : break;
3467 : :
3468 : 350 : nvername = MAX (nvername,
3469 : : (size_t) (aux->vna_other & 0x7fff));
3470 : :
3471 [ + + ]: 350 : if (aux->vna_next == 0)
3472 : : break;
3473 : 166 : auxoffset += aux->vna_next;
3474 : : }
3475 : :
3476 [ + + ]: 184 : if (need->vn_next == 0)
3477 : : break;
3478 : 54 : offset += need->vn_next;
3479 : : }
3480 : : }
3481 : :
3482 : : /* This is the number of versions we know about. */
3483 : 130 : ++nvername;
3484 : :
3485 : : /* Allocate the array. */
3486 : 130 : vername = (const char **) alloca (nvername * sizeof (const char *));
3487 [ + + ]: 130 : memset(vername, 0, nvername * sizeof (const char *));
3488 : 130 : filename = (const char **) alloca (nvername * sizeof (const char *));
3489 : 130 : memset(filename, 0, nvername * sizeof (const char *));
3490 : :
3491 : : /* Run through the data structures again and collect the strings. */
3492 [ + + ]: 130 : if (defscn != NULL)
3493 : : {
3494 : : /* Run through the version definitions and find the highest
3495 : : index. */
3496 : 4 : unsigned int offset = 0;
3497 : 4 : Elf_Data *defdata;
3498 : 4 : GElf_Shdr defshdrmem;
3499 : 4 : GElf_Shdr *defshdr;
3500 : :
3501 : 4 : defdata = elf_getdata (defscn, NULL);
3502 [ + - ]: 4 : if (unlikely (defdata == NULL))
3503 : 0 : return;
3504 : :
3505 : 4 : defshdr = gelf_getshdr (defscn, &defshdrmem);
3506 [ + - ]: 4 : if (unlikely (defshdr == NULL))
3507 : : return;
3508 : :
3509 [ + - ]: 24 : for (unsigned int cnt = 0; cnt < defshdr->sh_info; ++cnt)
3510 : : {
3511 : :
3512 : : /* Get the data at the next offset. */
3513 : 24 : GElf_Verdef defmem;
3514 : 24 : GElf_Verdef *def = gelf_getverdef (defdata, offset, &defmem);
3515 [ + - ]: 24 : if (unlikely (def == NULL))
3516 : : break;
3517 : :
3518 : 24 : GElf_Verdaux auxmem;
3519 : 48 : GElf_Verdaux *aux = gelf_getverdaux (defdata,
3520 : 24 : offset + def->vd_aux,
3521 : : &auxmem);
3522 [ + - ]: 24 : if (unlikely (aux == NULL))
3523 : : break;
3524 : :
3525 : 24 : vername[def->vd_ndx & 0x7fff]
3526 : 24 : = elf_strptr (ebl->elf, defshdr->sh_link, aux->vda_name);
3527 : 24 : filename[def->vd_ndx & 0x7fff] = NULL;
3528 : :
3529 [ + + ]: 24 : if (def->vd_next == 0)
3530 : : break;
3531 : 20 : offset += def->vd_next;
3532 : : }
3533 : : }
3534 [ + - ]: 130 : if (needscn != NULL)
3535 : : {
3536 : 130 : unsigned int offset = 0;
3537 : :
3538 : 130 : Elf_Data *needdata = elf_getdata (needscn, NULL);
3539 : 130 : GElf_Shdr needshdrmem;
3540 : 130 : GElf_Shdr *needshdr = gelf_getshdr (needscn, &needshdrmem);
3541 [ - + ]: 130 : if (unlikely (needdata == NULL || needshdr == NULL))
3542 : 0 : return;
3543 : :
3544 [ + - ]: 184 : for (unsigned int cnt = 0; cnt < needshdr->sh_info; ++cnt)
3545 : : {
3546 : : /* Get the data at the next offset. */
3547 : 184 : GElf_Verneed needmem;
3548 : 184 : GElf_Verneed *need = gelf_getverneed (needdata, offset,
3549 : : &needmem);
3550 [ + - ]: 184 : if (unlikely (need == NULL))
3551 : : break;
3552 : :
3553 : : /* Run through the auxiliary entries. */
3554 : 184 : unsigned int auxoffset = offset + need->vn_aux;
3555 [ + - ]: 350 : for (int cnt2 = need->vn_cnt; --cnt2 >= 0; )
3556 : : {
3557 : 350 : GElf_Vernaux auxmem;
3558 : 350 : GElf_Vernaux *aux = gelf_getvernaux (needdata, auxoffset,
3559 : : &auxmem);
3560 [ + - ]: 350 : if (unlikely (aux == NULL))
3561 : : break;
3562 : :
3563 : 350 : vername[aux->vna_other & 0x7fff]
3564 : 350 : = elf_strptr (ebl->elf, needshdr->sh_link, aux->vna_name);
3565 : 350 : filename[aux->vna_other & 0x7fff]
3566 : 350 : = elf_strptr (ebl->elf, needshdr->sh_link, need->vn_file);
3567 : :
3568 [ + + ]: 350 : if (aux->vna_next == 0)
3569 : : break;
3570 : 166 : auxoffset += aux->vna_next;
3571 : : }
3572 : :
3573 [ + + ]: 184 : if (need->vn_next == 0)
3574 : : break;
3575 : 54 : offset += need->vn_next;
3576 : : }
3577 : : }
3578 : : }
3579 : : else
3580 : : {
3581 : : vername = NULL;
3582 : : nvername = 1;
3583 : : filename = NULL;
3584 : : }
3585 : :
3586 : 130 : GElf_Shdr glink_mem;
3587 : 130 : GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
3588 : : &glink_mem);
3589 : 130 : size_t sh_entsize = gelf_fsize (ebl->elf, ELF_T_HALF, 1, EV_CURRENT);
3590 [ - + ]: 130 : if (glink == NULL)
3591 : 0 : error_exit (0, _("invalid sh_link value in section %zu"),
3592 : : elf_ndxscn (scn));
3593 : :
3594 : : /* Print the header. */
3595 : 130 : printf (ngettext ("\
3596 : : \nVersion symbols section [%2u] '%s' contains %d entry:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'",
3597 : : "\
3598 : : \nVersion symbols section [%2u] '%s' contains %d entries:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'",
3599 : : shdr->sh_size / sh_entsize),
3600 : 130 : (unsigned int) elf_ndxscn (scn),
3601 : 130 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
3602 : 130 : (int) (shdr->sh_size / sh_entsize),
3603 : : class == ELFCLASS32 ? 10 : 18, shdr->sh_addr,
3604 : : shdr->sh_offset,
3605 [ + + ]: 130 : (unsigned int) shdr->sh_link,
3606 : 130 : elf_strptr (ebl->elf, shstrndx, glink->sh_name));
3607 : :
3608 : : /* Now we can finally look at the actual contents of this section. */
3609 [ + + ]: 3292 : for (unsigned int cnt = 0; cnt < shdr->sh_size / sh_entsize; ++cnt)
3610 : : {
3611 [ + + ]: 3162 : if (cnt % 2 == 0)
3612 : 1598 : printf ("\n %4d:", cnt);
3613 : :
3614 : 3162 : GElf_Versym symmem;
3615 : 3162 : GElf_Versym *sym = gelf_getversym (data, cnt, &symmem);
3616 [ + - ]: 3162 : if (sym == NULL)
3617 : : break;
3618 : :
3619 [ + + + ]: 3162 : switch (*sym)
3620 : : {
3621 : 238 : ssize_t n;
3622 : 238 : case 0:
3623 : 238 : fputs (_(" 0 *local* "),
3624 : : stdout);
3625 : 238 : break;
3626 : :
3627 : 502 : case 1:
3628 : 502 : fputs (_(" 1 *global* "),
3629 : : stdout);
3630 : 502 : break;
3631 : :
3632 : 2422 : default:
3633 [ + - ]: 4844 : n = printf ("%4d%c%s",
3634 [ + - ]: 2422 : *sym & 0x7fff, *sym & 0x8000 ? 'h' : ' ',
3635 : : (vername != NULL
3636 [ + - ]: 2422 : && (unsigned int) (*sym & 0x7fff) < nvername)
3637 : 2422 : ? vername[*sym & 0x7fff] : "???");
3638 [ + - ]: 2422 : if ((unsigned int) (*sym & 0x7fff) < nvername
3639 [ + - + + ]: 2422 : && filename != NULL && filename[*sym & 0x7fff] != NULL)
3640 : 2118 : n += printf ("(%s)", filename[*sym & 0x7fff]);
3641 : 3162 : printf ("%*s", MAX (0, 33 - (int) n), " ");
3642 : : break;
3643 : : }
3644 : : }
3645 : 130 : putchar ('\n');
3646 : : }
3647 : :
3648 : :
3649 : : static void
3650 : 130 : print_hash_info (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr, size_t shstrndx,
3651 : : uint_fast32_t maxlength, Elf32_Word nbucket,
3652 : : uint_fast32_t nsyms, uint32_t *lengths, const char *extrastr)
3653 : : {
3654 : 130 : uint32_t *counts = xcalloc (maxlength + 1, sizeof (uint32_t));
3655 : :
3656 [ + + ]: 550 : for (Elf32_Word cnt = 0; cnt < nbucket; ++cnt)
3657 : 420 : ++counts[lengths[cnt]];
3658 : :
3659 : 130 : GElf_Shdr glink_mem;
3660 : 130 : GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf,
3661 : 130 : shdr->sh_link),
3662 : : &glink_mem);
3663 [ - + ]: 130 : if (glink == NULL)
3664 : : {
3665 : 0 : error (0, 0, _("invalid sh_link value in section %zu"),
3666 : : elf_ndxscn (scn));
3667 : 0 : free (counts);
3668 : 0 : return;
3669 : : }
3670 : :
3671 [ + + ]: 260 : printf (ngettext ("\
3672 : : \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",
3673 : : "\
3674 : : \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",
3675 : : nbucket),
3676 : 130 : (unsigned int) elf_ndxscn (scn),
3677 : 130 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
3678 : : (int) nbucket,
3679 : 130 : gelf_getclass (ebl->elf) == ELFCLASS32 ? 10 : 18,
3680 : : shdr->sh_addr,
3681 : : shdr->sh_offset,
3682 : 130 : (unsigned int) shdr->sh_link,
3683 : 130 : elf_strptr (ebl->elf, shstrndx, glink->sh_name));
3684 : :
3685 [ + - ]: 130 : if (extrastr != NULL)
3686 : 130 : fputs (extrastr, stdout);
3687 : :
3688 [ + - ]: 130 : if (likely (nbucket > 0))
3689 : : {
3690 : 130 : uint64_t success = 0;
3691 : :
3692 : : /* xgettext:no-c-format */
3693 : 130 : fputs (_("\
3694 : : Length Number % of total Coverage\n"), stdout);
3695 : 130 : printf (_(" 0 %6" PRIu32 " %5.1f%%\n"),
3696 : 130 : counts[0], (counts[0] * 100.0) / nbucket);
3697 : :
3698 : 130 : uint64_t nzero_counts = 0;
3699 [ + + ]: 228 : for (Elf32_Word cnt = 1; cnt <= maxlength; ++cnt)
3700 : : {
3701 : 98 : nzero_counts += counts[cnt] * cnt;
3702 : 98 : printf (_("\
3703 : : %7d %6" PRIu32 " %5.1f%% %5.1f%%\n"),
3704 : 98 : (int) cnt, counts[cnt], (counts[cnt] * 100.0) / nbucket,
3705 : 98 : (nzero_counts * 100.0) / nsyms);
3706 : : }
3707 : :
3708 : : Elf32_Word acc = 0;
3709 [ + + ]: 228 : for (Elf32_Word cnt = 1; cnt <= maxlength; ++cnt)
3710 : : {
3711 : 98 : acc += cnt;
3712 : 98 : success += counts[cnt] * acc;
3713 : : }
3714 : :
3715 [ + + ]: 130 : if (nzero_counts > 0)
3716 : 192 : printf (_("\
3717 : : Average number of tests: successful lookup: %f\n\
3718 : : unsuccessful lookup: %f\n"),
3719 : 62 : (double) success / (double) nzero_counts,
3720 : 62 : (double) nzero_counts / (double) nbucket);
3721 : : }
3722 : :
3723 : 130 : free (counts);
3724 : : }
3725 : :
3726 : :
3727 : : /* This function handles the traditional System V-style hash table format. */
3728 : : static void
3729 : 0 : handle_sysv_hash (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr, size_t shstrndx)
3730 : : {
3731 : 0 : Elf_Data *data = elf_getdata (scn, NULL);
3732 [ # # ]: 0 : if (unlikely (data == NULL))
3733 : : {
3734 : 0 : error (0, 0, _("cannot get data for section %d: %s"),
3735 : 0 : (int) elf_ndxscn (scn), elf_errmsg (-1));
3736 : 0 : return;
3737 : : }
3738 : :
3739 [ # # ]: 0 : if (unlikely (data->d_size < 2 * sizeof (Elf32_Word)))
3740 : : {
3741 : 0 : invalid_data:
3742 : 0 : error (0, 0, _("invalid data in sysv.hash section %d"),
3743 : 0 : (int) elf_ndxscn (scn));
3744 : 0 : return;
3745 : : }
3746 : :
3747 : 0 : Elf32_Word nbucket = ((Elf32_Word *) data->d_buf)[0];
3748 : 0 : Elf32_Word nchain = ((Elf32_Word *) data->d_buf)[1];
3749 : :
3750 : 0 : uint64_t used_buf = (2ULL + nchain + nbucket) * sizeof (Elf32_Word);
3751 [ # # ]: 0 : if (used_buf > data->d_size)
3752 : 0 : goto invalid_data;
3753 : :
3754 : 0 : Elf32_Word *bucket = &((Elf32_Word *) data->d_buf)[2];
3755 : 0 : Elf32_Word *chain = &((Elf32_Word *) data->d_buf)[2 + nbucket];
3756 : :
3757 : 0 : uint32_t *lengths = xcalloc (nbucket, sizeof (uint32_t));
3758 : :
3759 : 0 : uint_fast32_t maxlength = 0;
3760 : 0 : uint_fast32_t nsyms = 0;
3761 [ # # ]: 0 : for (Elf32_Word cnt = 0; cnt < nbucket; ++cnt)
3762 : : {
3763 : 0 : Elf32_Word inner = bucket[cnt];
3764 : 0 : Elf32_Word chain_len = 0;
3765 [ # # ]: 0 : while (inner > 0 && inner < nchain)
3766 : : {
3767 : 0 : ++nsyms;
3768 : 0 : ++chain_len;
3769 [ # # ]: 0 : if (chain_len > nchain)
3770 : : {
3771 : 0 : error (0, 0, _("invalid chain in sysv.hash section %d"),
3772 : 0 : (int) elf_ndxscn (scn));
3773 : 0 : free (lengths);
3774 : 0 : return;
3775 : : }
3776 [ # # ]: 0 : if (maxlength < ++lengths[cnt])
3777 : 0 : ++maxlength;
3778 : :
3779 : 0 : inner = chain[inner];
3780 : : }
3781 : : }
3782 : :
3783 : 0 : print_hash_info (ebl, scn, shdr, shstrndx, maxlength, nbucket, nsyms,
3784 : : lengths, NULL);
3785 : :
3786 : 0 : free (lengths);
3787 : : }
3788 : :
3789 : :
3790 : : /* This function handles the incorrect, System V-style hash table
3791 : : format some 64-bit architectures use. */
3792 : : static void
3793 : 0 : handle_sysv_hash64 (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr, size_t shstrndx)
3794 : : {
3795 : 0 : Elf_Data *data = elf_getdata (scn, NULL);
3796 [ # # ]: 0 : if (unlikely (data == NULL))
3797 : : {
3798 : 0 : error (0, 0, _("cannot get data for section %d: %s"),
3799 : 0 : (int) elf_ndxscn (scn), elf_errmsg (-1));
3800 : 0 : return;
3801 : : }
3802 : :
3803 [ # # ]: 0 : if (unlikely (data->d_size < 2 * sizeof (Elf64_Xword)))
3804 : : {
3805 : 0 : invalid_data:
3806 : 0 : error (0, 0, _("invalid data in sysv.hash64 section %d"),
3807 : 0 : (int) elf_ndxscn (scn));
3808 : 0 : return;
3809 : : }
3810 : :
3811 : 0 : Elf64_Xword nbucket = ((Elf64_Xword *) data->d_buf)[0];
3812 : 0 : Elf64_Xword nchain = ((Elf64_Xword *) data->d_buf)[1];
3813 : :
3814 : 0 : uint64_t maxwords = data->d_size / sizeof (Elf64_Xword);
3815 [ # # ]: 0 : if (maxwords < 2
3816 [ # # ]: 0 : || maxwords - 2 < nbucket
3817 [ # # ]: 0 : || maxwords - 2 - nbucket < nchain)
3818 : 0 : goto invalid_data;
3819 : :
3820 : 0 : Elf64_Xword *bucket = &((Elf64_Xword *) data->d_buf)[2];
3821 : 0 : Elf64_Xword *chain = &((Elf64_Xword *) data->d_buf)[2 + nbucket];
3822 : :
3823 : 0 : uint32_t *lengths = xcalloc (nbucket, sizeof (uint32_t));
3824 : :
3825 : 0 : uint_fast32_t maxlength = 0;
3826 : 0 : uint_fast32_t nsyms = 0;
3827 [ # # ]: 0 : for (Elf64_Xword cnt = 0; cnt < nbucket; ++cnt)
3828 : : {
3829 : 0 : Elf64_Xword inner = bucket[cnt];
3830 : 0 : Elf64_Xword chain_len = 0;
3831 [ # # ]: 0 : while (inner > 0 && inner < nchain)
3832 : : {
3833 : 0 : ++nsyms;
3834 : 0 : ++chain_len;
3835 [ # # ]: 0 : if (chain_len > nchain)
3836 : : {
3837 : 0 : error (0, 0, _("invalid chain in sysv.hash64 section %d"),
3838 : 0 : (int) elf_ndxscn (scn));
3839 : 0 : free (lengths);
3840 : 0 : return;
3841 : : }
3842 [ # # ]: 0 : if (maxlength < ++lengths[cnt])
3843 : 0 : ++maxlength;
3844 : :
3845 : 0 : inner = chain[inner];
3846 : : }
3847 : : }
3848 : :
3849 : 0 : print_hash_info (ebl, scn, shdr, shstrndx, maxlength, nbucket, nsyms,
3850 : : lengths, NULL);
3851 : :
3852 : 0 : free (lengths);
3853 : : }
3854 : :
3855 : :
3856 : : /* This function handles the GNU-style hash table format. */
3857 : : static void
3858 : 130 : handle_gnu_hash (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr, size_t shstrndx)
3859 : : {
3860 : 130 : uint32_t *lengths = NULL;
3861 : 130 : Elf_Data *data = elf_getdata (scn, NULL);
3862 [ - + ]: 130 : if (unlikely (data == NULL))
3863 : : {
3864 : 0 : error (0, 0, _("cannot get data for section %d: %s"),
3865 : 0 : (int) elf_ndxscn (scn), elf_errmsg (-1));
3866 : 0 : return;
3867 : : }
3868 : :
3869 [ - + ]: 130 : if (unlikely (data->d_size < 4 * sizeof (Elf32_Word)))
3870 : : {
3871 : 0 : invalid_data:
3872 : 0 : free (lengths);
3873 : 0 : error (0, 0, _("invalid data in gnu.hash section %d"),
3874 : 0 : (int) elf_ndxscn (scn));
3875 : 0 : return;
3876 : : }
3877 : :
3878 : 130 : Elf32_Word nbucket = ((Elf32_Word *) data->d_buf)[0];
3879 : 130 : Elf32_Word symbias = ((Elf32_Word *) data->d_buf)[1];
3880 : :
3881 : : /* Next comes the size of the bitmap. It's measured in words for
3882 : : the architecture. It's 32 bits for 32 bit archs, and 64 bits for
3883 : : 64 bit archs. There is always a bloom filter present, so zero is
3884 : : an invalid value. */
3885 : 130 : Elf32_Word bitmask_words = ((Elf32_Word *) data->d_buf)[2];
3886 [ + + ]: 130 : if (gelf_getclass (ebl->elf) == ELFCLASS64)
3887 : 114 : bitmask_words *= 2;
3888 : :
3889 [ - + ]: 130 : if (bitmask_words == 0)
3890 : 0 : goto invalid_data;
3891 : :
3892 : 130 : Elf32_Word shift = ((Elf32_Word *) data->d_buf)[3];
3893 : :
3894 : : /* Is there still room for the sym chain?
3895 : : Use uint64_t calculation to prevent 32bit overflow. */
3896 : 130 : uint64_t used_buf = (4ULL + bitmask_words + nbucket) * sizeof (Elf32_Word);
3897 : 130 : uint32_t max_nsyms = (data->d_size - used_buf) / sizeof (Elf32_Word);
3898 [ - + ]: 130 : if (used_buf > data->d_size)
3899 : 0 : goto invalid_data;
3900 : :
3901 : 130 : lengths = xcalloc (nbucket, sizeof (uint32_t));
3902 : :
3903 : 130 : Elf32_Word *bitmask = &((Elf32_Word *) data->d_buf)[4];
3904 : 130 : Elf32_Word *bucket = &((Elf32_Word *) data->d_buf)[4 + bitmask_words];
3905 : 130 : Elf32_Word *chain = &((Elf32_Word *) data->d_buf)[4 + bitmask_words
3906 : 130 : + nbucket];
3907 : :
3908 : : /* Compute distribution of chain lengths. */
3909 : 130 : uint_fast32_t maxlength = 0;
3910 : 130 : uint_fast32_t nsyms = 0;
3911 [ + + ]: 550 : for (Elf32_Word cnt = 0; cnt < nbucket; ++cnt)
3912 [ + + ]: 420 : if (bucket[cnt] != 0)
3913 : : {
3914 : 232 : Elf32_Word inner = bucket[cnt] - symbias;
3915 : 414 : do
3916 : : {
3917 : 414 : ++nsyms;
3918 [ + + ]: 414 : if (maxlength < ++lengths[cnt])
3919 : 98 : ++maxlength;
3920 [ - + ]: 414 : if (inner >= max_nsyms)
3921 : 0 : goto invalid_data;
3922 : : }
3923 [ + + ]: 414 : while ((chain[inner++] & 1) == 0);
3924 : : }
3925 : :
3926 : : /* Count bits in bitmask. */
3927 : : uint_fast32_t nbits = 0;
3928 [ + + ]: 446 : for (Elf32_Word cnt = 0; cnt < bitmask_words; ++cnt)
3929 : : {
3930 : 316 : uint_fast32_t word = bitmask[cnt];
3931 : :
3932 : 316 : word = (word & 0x55555555) + ((word >> 1) & 0x55555555);
3933 : 316 : word = (word & 0x33333333) + ((word >> 2) & 0x33333333);
3934 : 316 : word = (word & 0x0f0f0f0f) + ((word >> 4) & 0x0f0f0f0f);
3935 : 316 : word = (word & 0x00ff00ff) + ((word >> 8) & 0x00ff00ff);
3936 : 316 : nbits += (word & 0x0000ffff) + ((word >> 16) & 0x0000ffff);
3937 : : }
3938 : :
3939 : 260 : char *str = xasprintf (_("\
3940 : : Symbol Bias: %u\n\
3941 : : Bitmask Size: %zu bytes %" PRIuFAST32 "%% bits set 2nd hash shift: %u\n"),
3942 : : (unsigned int) symbias,
3943 : : bitmask_words * sizeof (Elf32_Word),
3944 : 130 : ((nbits * 100 + 50)
3945 : 130 : / (uint_fast32_t) (bitmask_words
3946 : : * sizeof (Elf32_Word) * 8)),
3947 : : (unsigned int) shift);
3948 : :
3949 : 130 : print_hash_info (ebl, scn, shdr, shstrndx, maxlength, nbucket, nsyms,
3950 : : lengths, str);
3951 : :
3952 : 130 : free (str);
3953 : 130 : free (lengths);
3954 : : }
3955 : :
3956 : :
3957 : : /* Find the symbol table(s). For this we have to search through the
3958 : : section table. */
3959 : : static void
3960 : 184 : handle_hash (Ebl *ebl)
3961 : : {
3962 : : /* Get the section header string table index. */
3963 : 184 : size_t shstrndx;
3964 [ - + ]: 184 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
3965 : 0 : error_exit (0, _("cannot get section header string table index"));
3966 : :
3967 : : Elf_Scn *scn = NULL;
3968 [ + + ]: 6004 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
3969 : : {
3970 : : /* Handle the section if it is a symbol table. */
3971 : 5820 : GElf_Shdr shdr_mem;
3972 : 5820 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
3973 : :
3974 [ + - ]: 5820 : if (likely (shdr != NULL))
3975 : : {
3976 [ + + ]: 5820 : if ((shdr->sh_type == SHT_HASH || shdr->sh_type == SHT_GNU_HASH)
3977 [ - + ]: 130 : && (shdr->sh_flags & SHF_COMPRESSED) != 0)
3978 : : {
3979 [ # # ]: 0 : if (elf_compress (scn, 0, 0) < 0)
3980 : 0 : printf ("WARNING: %s [%zd]\n",
3981 : : _("Couldn't uncompress section"),
3982 : : elf_ndxscn (scn));
3983 : 0 : shdr = gelf_getshdr (scn, &shdr_mem);
3984 [ # # ]: 0 : if (unlikely (shdr == NULL))
3985 : 0 : error_exit (0, _("cannot get section [%zd] header: %s"),
3986 : : elf_ndxscn (scn), elf_errmsg (-1));
3987 : : }
3988 : :
3989 [ - + ]: 5820 : if (shdr->sh_type == SHT_HASH)
3990 : : {
3991 [ # # ]: 0 : if (ebl_sysvhash_entrysize (ebl) == sizeof (Elf64_Xword))
3992 : 0 : handle_sysv_hash64 (ebl, scn, shdr, shstrndx);
3993 : : else
3994 : 0 : handle_sysv_hash (ebl, scn, shdr, shstrndx);
3995 : : }
3996 [ + + ]: 5820 : else if (shdr->sh_type == SHT_GNU_HASH)
3997 : 130 : handle_gnu_hash (ebl, scn, shdr, shstrndx);
3998 : : }
3999 : : }
4000 : 184 : }
4001 : :
4002 : :
4003 : : static void
4004 : 192 : print_liblist (Ebl *ebl)
4005 : : {
4006 : : /* Find the library list sections. For this we have to search
4007 : : through the section table. */
4008 : 192 : Elf_Scn *scn = NULL;
4009 : :
4010 : : /* Get the section header string table index. */
4011 : 192 : size_t shstrndx;
4012 [ - + ]: 192 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
4013 : 0 : error_exit (0, _("cannot get section header string table index"));
4014 : :
4015 [ + + ]: 6130 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
4016 : : {
4017 : 5938 : GElf_Shdr shdr_mem;
4018 : 5938 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
4019 : :
4020 [ + - - + ]: 5938 : if (shdr != NULL && shdr->sh_type == SHT_GNU_LIBLIST)
4021 : : {
4022 : 0 : size_t sh_entsize = gelf_fsize (ebl->elf, ELF_T_LIB, 1, EV_CURRENT);
4023 : 0 : int nentries = shdr->sh_size / sh_entsize;
4024 : 0 : printf (ngettext ("\
4025 : : \nLibrary list section [%2zu] '%s' at offset %#0" PRIx64 " contains %d entry:\n",
4026 : : "\
4027 : : \nLibrary list section [%2zu] '%s' at offset %#0" PRIx64 " contains %d entries:\n",
4028 : : nentries),
4029 : : elf_ndxscn (scn),
4030 : 0 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
4031 : : shdr->sh_offset,
4032 : : nentries);
4033 : :
4034 : 0 : Elf_Data *data = elf_getdata (scn, NULL);
4035 [ # # ]: 0 : if (data == NULL)
4036 : 0 : return;
4037 : :
4038 : 0 : puts (_("\
4039 : : Library Time Stamp Checksum Version Flags"));
4040 : :
4041 [ # # ]: 0 : for (int cnt = 0; cnt < nentries; ++cnt)
4042 : : {
4043 : 0 : GElf_Lib lib_mem;
4044 : 0 : GElf_Lib *lib = gelf_getlib (data, cnt, &lib_mem);
4045 [ # # ]: 0 : if (unlikely (lib == NULL))
4046 : 0 : continue;
4047 : :
4048 : 0 : time_t t = (time_t) lib->l_time_stamp;
4049 : 0 : struct tm *tm = gmtime (&t);
4050 [ # # ]: 0 : if (unlikely (tm == NULL))
4051 : 0 : continue;
4052 : :
4053 : 0 : printf (" [%2d] %-29s %04u-%02u-%02uT%02u:%02u:%02u %08x %-7u %u\n",
4054 : 0 : cnt, elf_strptr (ebl->elf, shdr->sh_link, lib->l_name),
4055 : 0 : tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
4056 : : tm->tm_hour, tm->tm_min, tm->tm_sec,
4057 : 0 : (unsigned int) lib->l_checksum,
4058 : 0 : (unsigned int) lib->l_version,
4059 : 0 : (unsigned int) lib->l_flags);
4060 : : }
4061 : : }
4062 : : }
4063 : : }
4064 : :
4065 : : static inline size_t
4066 : 16 : left (Elf_Data *data,
4067 : : const unsigned char *p)
4068 : : {
4069 : 16 : return (const unsigned char *) data->d_buf + data->d_size - p;
4070 : : }
4071 : :
4072 : : static void
4073 : 192 : print_attributes (Ebl *ebl, const GElf_Ehdr *ehdr)
4074 : : {
4075 : : /* Find the object attributes sections. For this we have to search
4076 : : through the section table. */
4077 : 192 : Elf_Scn *scn = NULL;
4078 : :
4079 : : /* Get the section header string table index. */
4080 : 192 : size_t shstrndx;
4081 [ - + ]: 192 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
4082 : 0 : error_exit (0, _("cannot get section header string table index"));
4083 : :
4084 [ + + ]: 6130 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
4085 : : {
4086 : 5938 : GElf_Shdr shdr_mem;
4087 : 5938 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
4088 : :
4089 [ + - + + ]: 5938 : if (shdr == NULL || (shdr->sh_type != SHT_GNU_ATTRIBUTES
4090 [ + + ]: 5932 : && (shdr->sh_type != SHT_ARM_ATTRIBUTES
4091 [ - + ]: 2 : || ehdr->e_machine != EM_ARM)
4092 [ + + ]: 5930 : && (shdr->sh_type != SHT_CSKY_ATTRIBUTES
4093 [ + - ]: 2 : || ehdr->e_machine != EM_CSKY)
4094 [ - + ]: 5930 : && (shdr->sh_type != SHT_RISCV_ATTRIBUTES
4095 [ # # ]: 0 : || ehdr->e_machine != EM_RISCV)))
4096 : 5930 : continue;
4097 : :
4098 : 8 : printf (_("\
4099 : : \nObject attributes section [%2zu] '%s' of %" PRIu64
4100 : : " bytes at offset %#0" PRIx64 ":\n"),
4101 : : elf_ndxscn (scn),
4102 : 8 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
4103 : : shdr->sh_size, shdr->sh_offset);
4104 : :
4105 : 8 : Elf_Data *data = elf_rawdata (scn, NULL);
4106 [ + - + - ]: 8 : if (unlikely (data == NULL || data->d_size == 0))
4107 : 0 : return;
4108 : :
4109 : 8 : const unsigned char *p = data->d_buf;
4110 : :
4111 : : /* There is only one 'version', A. */
4112 [ + - ]: 8 : if (unlikely (*p++ != 'A'))
4113 : : return;
4114 : :
4115 : 8 : fputs (_(" Owner Size\n"), stdout);
4116 : :
4117 : : /* Loop over the sections. */
4118 [ + + ]: 16 : while (left (data, p) >= 4)
4119 : : {
4120 : : /* Section length. */
4121 : 8 : uint32_t len;
4122 [ + + ]: 8 : memcpy (&len, p, sizeof len);
4123 : :
4124 [ + + ]: 8 : if (MY_ELFDATA != ehdr->e_ident[EI_DATA])
4125 : 4 : CONVERT (len);
4126 : :
4127 [ + - ]: 8 : if (unlikely (len > left (data, p)))
4128 : : break;
4129 : :
4130 : : /* Section vendor name. */
4131 : 8 : const unsigned char *name = p + sizeof len;
4132 : 8 : p += len;
4133 : :
4134 : 8 : unsigned const char *q = memchr (name, '\0', len);
4135 [ + - ]: 8 : if (unlikely (q == NULL))
4136 : : break;
4137 : 8 : ++q;
4138 : :
4139 : 8 : printf (_(" %-13s %4" PRIu32 "\n"), name, len);
4140 : :
4141 : 16 : bool gnu_vendor = (q - name == sizeof "gnu"
4142 [ + + - + ]: 8 : && !memcmp (name, "gnu", sizeof "gnu"));
4143 : :
4144 : : /* Loop over subsections. */
4145 [ + + ]: 8 : if (shdr->sh_type != SHT_GNU_ATTRIBUTES
4146 [ + - ]: 6 : || gnu_vendor)
4147 [ + + ]: 16 : while (q < p)
4148 : : {
4149 : 8 : const unsigned char *const sub = q;
4150 : :
4151 : 8 : unsigned int subsection_tag;
4152 : 8 : get_uleb128 (subsection_tag, q, p);
4153 [ + - ]: 8 : if (unlikely (q >= p))
4154 : : break;
4155 : :
4156 : 8 : uint32_t subsection_len;
4157 [ + - ]: 8 : if (unlikely (p - sub < (ptrdiff_t) sizeof subsection_len))
4158 : : break;
4159 : :
4160 [ + + ]: 8 : memcpy (&subsection_len, q, sizeof subsection_len);
4161 : :
4162 [ + + ]: 8 : if (MY_ELFDATA != ehdr->e_ident[EI_DATA])
4163 : 4 : CONVERT (subsection_len);
4164 : :
4165 : : /* Don't overflow, ptrdiff_t might be 32bits, but signed. */
4166 [ + - + - ]: 8 : if (unlikely (subsection_len == 0
4167 : : || subsection_len >= (uint32_t) PTRDIFF_MAX
4168 : : || p - sub < (ptrdiff_t) subsection_len))
4169 : : break;
4170 : :
4171 : 8 : const unsigned char *r = q + sizeof subsection_len;
4172 : 8 : q = sub + subsection_len;
4173 : :
4174 [ - + ]: 8 : switch (subsection_tag)
4175 : : {
4176 : 0 : default:
4177 : : /* Unknown subsection, print and skip. */
4178 : 8 : printf (_(" %-4u %12" PRIu32 "\n"),
4179 : : subsection_tag, subsection_len);
4180 : : break;
4181 : :
4182 : 8 : case 1: /* Tag_File */
4183 : 8 : printf (_(" File: %11" PRIu32 "\n"),
4184 : : subsection_len);
4185 : :
4186 [ + + ]: 52 : while (r < q)
4187 : : {
4188 : 44 : unsigned int tag;
4189 : 44 : get_uleb128 (tag, r, q);
4190 [ + - ]: 44 : if (unlikely (r >= q))
4191 : : break;
4192 : :
4193 : : /* GNU style tags have either a uleb128 value,
4194 : : when lowest bit is not set, or a string
4195 : : when the lowest bit is set.
4196 : : "compatibility" (32) is special. It has
4197 : : both a string and a uleb128 value. For
4198 : : non-gnu we assume 6 till 31 only take ints.
4199 : : XXX see arm backend, do we need a separate
4200 : : hook? */
4201 : 44 : uint64_t value = 0;
4202 : 44 : const char *string = NULL;
4203 [ + - + + ]: 44 : if (tag == 32 || (tag & 1) == 0
4204 [ + - + + ]: 16 : || (! gnu_vendor && (tag > 5 && tag < 32)))
4205 : : {
4206 : : // Note r >= q check above.
4207 : 42 : get_uleb128 (value, r, q);
4208 [ + - ]: 42 : if (r > q)
4209 : : break;
4210 : : }
4211 [ + - ]: 44 : if (tag == 32
4212 [ + + ]: 44 : || ((tag & 1) != 0
4213 [ + - ]: 16 : && (gnu_vendor
4214 [ + - ]: 16 : || (! gnu_vendor && tag > 32)))
4215 [ + + + + ]: 44 : || (! gnu_vendor && tag > 3 && tag < 6))
4216 : : {
4217 : 2 : string = (const char *) r;
4218 : 2 : r = memchr (r, '\0', q - r);
4219 [ + - ]: 2 : if (r == NULL)
4220 : : break;
4221 : 2 : ++r;
4222 : : }
4223 : :
4224 : 44 : const char *tag_name = NULL;
4225 : 44 : const char *value_name = NULL;
4226 : 44 : ebl_check_object_attribute (ebl, (const char *) name,
4227 : : tag, value,
4228 : : &tag_name, &value_name);
4229 : :
4230 [ + - ]: 44 : if (tag_name != NULL)
4231 : : {
4232 [ - + ]: 44 : if (tag == 32)
4233 : 0 : printf (_(" %s: %" PRId64 ", %s\n"),
4234 : : tag_name, value, string);
4235 [ + + + + ]: 44 : else if (string == NULL && value_name == NULL)
4236 : 2 : printf (_(" %s: %" PRId64 "\n"),
4237 : : tag_name, value);
4238 : : else
4239 : 42 : printf (_(" %s: %s\n"),
4240 : : tag_name, string ?: value_name);
4241 : : }
4242 : : else
4243 : : {
4244 : : /* For "gnu" vendor 32 "compatibility" has
4245 : : already been handled above. */
4246 [ # # # # ]: 0 : assert (tag != 32
4247 : : || strcmp ((const char *) name, "gnu"));
4248 [ # # ]: 0 : if (string == NULL)
4249 : 0 : printf (_(" %u: %" PRId64 "\n"),
4250 : : tag, value);
4251 : : else
4252 : 44 : printf (_(" %u: %s\n"),
4253 : : tag, string);
4254 : : }
4255 : : }
4256 : : }
4257 : : }
4258 : : }
4259 : : }
4260 : : }
4261 : :
4262 : : /* Returns either the (relocated) data from the Dwarf, or tries to get
4263 : : the "raw" (uncompressed) data from the Elf section. Produces a
4264 : : warning if the data cannot be found (or decompressed). */
4265 : : static Elf_Data *
4266 : 810 : get_debug_elf_data (Dwarf *dbg, Ebl *ebl, int idx, Elf_Scn *scn)
4267 : : {
4268 : : /* We prefer to get the section data from the Dwarf because that
4269 : : might have been relocated already. Note this is subtly wrong if
4270 : : there are multiple sections with the same .debug name. */
4271 [ - + ]: 810 : if (dbg->sectiondata[idx] != NULL)
4272 : : return dbg->sectiondata[idx];
4273 : :
4274 : 0 : GElf_Shdr shdr_mem;
4275 : 0 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
4276 [ # # # # ]: 0 : if (shdr != NULL && (shdr->sh_flags & SHF_COMPRESSED) != 0)
4277 : : {
4278 [ # # ]: 0 : if (elf_compress (scn, 0, 0) < 0)
4279 : : {
4280 : 0 : error (0, 0, "%s [%zd] '%s'\n",
4281 : : _("Couldn't uncompress section"),
4282 : : elf_ndxscn (scn), section_name (ebl, shdr));
4283 : 0 : return NULL;
4284 : : }
4285 : : }
4286 : :
4287 : 0 : Elf_Data *data = elf_getdata (scn, NULL);
4288 [ # # ]: 0 : if (data == NULL)
4289 : 0 : error (0, 0, "%s [%zd] '%s': %s\n",
4290 : : _("Couldn't get data from section"),
4291 : : elf_ndxscn (scn), section_name (ebl, shdr), elf_errmsg (-1));
4292 : :
4293 : 0 : return elf_getdata (scn, NULL);
4294 : : }
4295 : :
4296 : : static void
4297 : 2205948 : print_dwarf_addr (Dwfl_Module *dwflmod,
4298 : : int address_size, Dwarf_Addr address, Dwarf_Addr raw)
4299 : : {
4300 : : /* See if there is a name we can give for this address. */
4301 : 2205948 : GElf_Sym sym;
4302 : 2205948 : GElf_Off off = 0;
4303 [ + + ]: 13388 : const char *name = (print_address_names && ! print_unresolved_addresses)
4304 : 13224 : ? dwfl_module_addrinfo (dwflmod, address, &off, &sym, NULL, NULL, NULL)
4305 [ + + ]: 2205948 : : NULL;
4306 : :
4307 : 2205948 : const char *scn;
4308 [ + + ]: 2205948 : if (print_unresolved_addresses)
4309 : : {
4310 : 168 : address = raw;
4311 : 168 : scn = NULL;
4312 : : }
4313 : : else
4314 : : {
4315 : : /* Relativize the address. */
4316 : 2205780 : int n = dwfl_module_relocations (dwflmod);
4317 [ + + ]: 2205780 : int i = n < 1 ? -1 : dwfl_module_relocate_address (dwflmod, &address);
4318 : :
4319 : : /* In an ET_REL file there is a section name to refer to. */
4320 : 2203028 : scn = (i < 0 ? NULL
4321 [ - + ]: 2203028 : : dwfl_module_relocation_info (dwflmod, i, NULL));
4322 : : }
4323 : :
4324 [ - + ]: 2205948 : if ((name != NULL
4325 : 12816 : ? (off != 0
4326 : : ? (scn != NULL
4327 : : ? (address_size == 0
4328 : 2 : ? printf ("%s+%#" PRIx64 " <%s+%#" PRIx64 ">",
4329 : : scn, address, name, off)
4330 : 8 : : printf ("%s+%#0*" PRIx64 " <%s+%#" PRIx64 ">",
4331 : 4 : scn, 2 + address_size * 2, address,
4332 : : name, off))
4333 : : : (address_size == 0
4334 : 364 : ? printf ("%#" PRIx64 " <%s+%#" PRIx64 ">",
4335 : : address, name, off)
4336 : 2156 : : printf ("%#0*" PRIx64 " <%s+%#" PRIx64 ">",
4337 : 1078 : 2 + address_size * 2, address,
4338 : : name, off)))
4339 : : : (scn != NULL
4340 : : ? (address_size == 0
4341 : 2 : ? printf ("%s+%#" PRIx64 " <%s>", scn, address, name)
4342 : 20732 : : printf ("%s+%#0*" PRIx64 " <%s>",
4343 : 10366 : scn, 2 + address_size * 2, address, name))
4344 : : : (address_size == 0
4345 : 160 : ? printf ("%#" PRIx64 " <%s>", address, name)
4346 : 1680 : : printf ("%#0*" PRIx64 " <%s>",
4347 : 840 : 2 + address_size * 2, address, name))))
4348 : : : (scn != NULL
4349 : : ? (address_size == 0
4350 : 800862 : ? printf ("%s+%#" PRIx64, scn, address)
4351 : 1391792 : : printf ("%s+%#0*" PRIx64, scn, 2 + address_size * 2, address))
4352 : : : (address_size == 0
4353 : 70 : ? printf ("%#" PRIx64, address)
4354 [ + + + + : 4411488 : : printf ("%#0*" PRIx64, 2 + address_size * 2, address)))) < 0)
+ + + + +
+ + + + +
+ + + + +
+ + + ]
4355 : 0 : error_exit (0, _("sprintf failure"));
4356 : 2205948 : }
4357 : :
4358 : :
4359 : : static const char *
4360 : 2291264 : dwarf_tag_string (unsigned int tag)
4361 : : {
4362 [ - + + - : 2291264 : switch (tag)
- - - - +
+ + + + -
- - - - -
+ - + - +
- - + + -
+ - - - -
- - - + -
+ - + + +
- - - - -
- + - - +
- - - + -
+ + + + -
- - - - +
+ + + - +
+ + + - -
- ]
4363 : : {
4364 : : #define DWARF_ONE_KNOWN_DW_TAG(NAME, CODE) case CODE: return #NAME;
4365 : 2291264 : DWARF_ALL_KNOWN_DW_TAG
4366 : : #undef DWARF_ONE_KNOWN_DW_TAG
4367 : 0 : default:
4368 : 0 : return NULL;
4369 : : }
4370 : : }
4371 : :
4372 : :
4373 : : static const char *
4374 : 8854376 : dwarf_attr_string (unsigned int attrnum)
4375 : : {
4376 [ + - + - : 8854376 : switch (attrnum)
- - - + -
- + + + -
- - - + +
- + - - +
- + - + -
- - - - -
- - - - -
- - - - -
- - - + -
+ - + - +
- - - + +
- - - + -
+ - + + -
- + + + +
- + + + -
+ - - + -
+ - + + -
+ - - + +
+ + - - -
- - + + +
+ - + - +
- - - - +
+ - + - +
+ - + + +
+ + + + -
- + - - -
+ - + - -
- - + + -
+ - - - -
+ - - - +
+ - + - -
- - + + -
- - - - +
+ - - - -
- - - + ]
4377 : : {
4378 : : #define DWARF_ONE_KNOWN_DW_AT(NAME, CODE) case CODE: return #NAME;
4379 : 8854374 : DWARF_ALL_KNOWN_DW_AT
4380 : : #undef DWARF_ONE_KNOWN_DW_AT
4381 : 0 : default:
4382 : 0 : return NULL;
4383 : : }
4384 : : }
4385 : :
4386 : :
4387 : : static const char *
4388 : 8864782 : dwarf_form_string (unsigned int form)
4389 : : {
4390 [ + + + + : 8864782 : switch (form)
+ - - - -
- + - - +
+ + + + +
+ + + - +
+ - - + -
- + - - +
+ + + + +
- + + - -
- + - + ]
4391 : : {
4392 : : #define DWARF_ONE_KNOWN_DW_FORM(NAME, CODE) case CODE: return #NAME;
4393 : 8864778 : DWARF_ALL_KNOWN_DW_FORM
4394 : : #undef DWARF_ONE_KNOWN_DW_FORM
4395 : 0 : default:
4396 : 0 : return NULL;
4397 : : }
4398 : : }
4399 : :
4400 : :
4401 : : static const char *
4402 : 3736 : dwarf_lang_string (unsigned int lang)
4403 : : {
4404 [ - - + - : 3736 : switch (lang)
- - - + -
- + + - +
- - + - -
- - - - -
- - - + -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - +
- ]
4405 : : {
4406 : : #define DWARF_ONE_KNOWN_DW_LANG(NAME, CODE) case CODE: return #NAME;
4407 : 3734 : DWARF_ALL_KNOWN_DW_LANG
4408 : : #undef DWARF_ONE_KNOWN_DW_LANG
4409 : 2 : default:
4410 : 2 : return NULL;
4411 : : }
4412 : : }
4413 : :
4414 : :
4415 : : static const char *
4416 : 4 : dwarf_lname_string (unsigned int lname)
4417 : : {
4418 [ - - - + : 4 : switch (lname)
- + - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - ]
4419 : : {
4420 : : #define DWARF_ONE_KNOWN_DW_LNAME(NAME, CODE) case CODE: return #NAME;
4421 : 4 : DWARF_ALL_KNOWN_DW_LNAME
4422 : : #undef DWARF_ONE_KNOWN_DW_LNAME
4423 : 0 : default:
4424 : 0 : return NULL;
4425 : : }
4426 : : }
4427 : :
4428 : :
4429 : : static const char *
4430 : 7206 : dwarf_inline_string (unsigned int code)
4431 : : {
4432 : 7206 : static const char *const known[] =
4433 : : {
4434 : : #define DWARF_ONE_KNOWN_DW_INL(NAME, CODE) [CODE] = #NAME,
4435 : : DWARF_ALL_KNOWN_DW_INL
4436 : : #undef DWARF_ONE_KNOWN_DW_INL
4437 : : };
4438 : :
4439 : 7206 : if (likely (code < sizeof (known) / sizeof (known[0])))
4440 : 7206 : return known[code];
4441 : :
4442 : : return NULL;
4443 : : }
4444 : :
4445 : :
4446 : : static const char *
4447 : 57348 : dwarf_encoding_string (unsigned int code)
4448 : : {
4449 : 57348 : static const char *const known[] =
4450 : : {
4451 : : #define DWARF_ONE_KNOWN_DW_ATE(NAME, CODE) [CODE] = #NAME,
4452 : : DWARF_ALL_KNOWN_DW_ATE
4453 : : #undef DWARF_ONE_KNOWN_DW_ATE
4454 : : };
4455 : :
4456 : 57348 : if (likely (code < sizeof (known) / sizeof (known[0])))
4457 : 57348 : return known[code];
4458 : :
4459 : : return NULL;
4460 : : }
4461 : :
4462 : :
4463 : : static const char *
4464 : 0 : dwarf_access_string (unsigned int code)
4465 : : {
4466 : 0 : static const char *const known[] =
4467 : : {
4468 : : #define DWARF_ONE_KNOWN_DW_ACCESS(NAME, CODE) [CODE] = #NAME,
4469 : : DWARF_ALL_KNOWN_DW_ACCESS
4470 : : #undef DWARF_ONE_KNOWN_DW_ACCESS
4471 : : };
4472 : :
4473 : 0 : if (likely (code < sizeof (known) / sizeof (known[0])))
4474 : 0 : return known[code];
4475 : :
4476 : : return NULL;
4477 : : }
4478 : :
4479 : :
4480 : : static const char *
4481 : 0 : dwarf_defaulted_string (unsigned int code)
4482 : : {
4483 : 0 : static const char *const known[] =
4484 : : {
4485 : : #define DWARF_ONE_KNOWN_DW_DEFAULTED(NAME, CODE) [CODE] = #NAME,
4486 : : DWARF_ALL_KNOWN_DW_DEFAULTED
4487 : : #undef DWARF_ONE_KNOWN_DW_DEFAULTED
4488 : : };
4489 : :
4490 : 0 : if (likely (code < sizeof (known) / sizeof (known[0])))
4491 : 0 : return known[code];
4492 : :
4493 : : return NULL;
4494 : : }
4495 : :
4496 : :
4497 : : static const char *
4498 : 0 : dwarf_visibility_string (unsigned int code)
4499 : : {
4500 : 0 : static const char *const known[] =
4501 : : {
4502 : : #define DWARF_ONE_KNOWN_DW_VIS(NAME, CODE) [CODE] = #NAME,
4503 : : DWARF_ALL_KNOWN_DW_VIS
4504 : : #undef DWARF_ONE_KNOWN_DW_VIS
4505 : : };
4506 : :
4507 : 0 : if (likely (code < sizeof (known) / sizeof (known[0])))
4508 : 0 : return known[code];
4509 : :
4510 : : return NULL;
4511 : : }
4512 : :
4513 : :
4514 : : static const char *
4515 : 0 : dwarf_virtuality_string (unsigned int code)
4516 : : {
4517 : 0 : static const char *const known[] =
4518 : : {
4519 : : #define DWARF_ONE_KNOWN_DW_VIRTUALITY(NAME, CODE) [CODE] = #NAME,
4520 : : DWARF_ALL_KNOWN_DW_VIRTUALITY
4521 : : #undef DWARF_ONE_KNOWN_DW_VIRTUALITY
4522 : : };
4523 : :
4524 : 0 : if (likely (code < sizeof (known) / sizeof (known[0])))
4525 : 0 : return known[code];
4526 : :
4527 : : return NULL;
4528 : : }
4529 : :
4530 : :
4531 : : static const char *
4532 : 0 : dwarf_identifier_case_string (unsigned int code)
4533 : : {
4534 : 0 : static const char *const known[] =
4535 : : {
4536 : : #define DWARF_ONE_KNOWN_DW_ID(NAME, CODE) [CODE] = #NAME,
4537 : : DWARF_ALL_KNOWN_DW_ID
4538 : : #undef DWARF_ONE_KNOWN_DW_ID
4539 : : };
4540 : :
4541 : 0 : if (likely (code < sizeof (known) / sizeof (known[0])))
4542 : 0 : return known[code];
4543 : :
4544 : : return NULL;
4545 : : }
4546 : :
4547 : :
4548 : : static const char *
4549 : 0 : dwarf_calling_convention_string (unsigned int code)
4550 : : {
4551 : 0 : static const char *const known[] =
4552 : : {
4553 : : #define DWARF_ONE_KNOWN_DW_CC(NAME, CODE) [CODE] = #NAME,
4554 : : DWARF_ALL_KNOWN_DW_CC
4555 : : #undef DWARF_ONE_KNOWN_DW_CC
4556 : : };
4557 : :
4558 : 0 : if (likely (code < sizeof (known) / sizeof (known[0])))
4559 : 0 : return known[code];
4560 : :
4561 : : return NULL;
4562 : : }
4563 : :
4564 : :
4565 : : static const char *
4566 : 0 : dwarf_ordering_string (unsigned int code)
4567 : : {
4568 : 0 : static const char *const known[] =
4569 : : {
4570 : : #define DWARF_ONE_KNOWN_DW_ORD(NAME, CODE) [CODE] = #NAME,
4571 : : DWARF_ALL_KNOWN_DW_ORD
4572 : : #undef DWARF_ONE_KNOWN_DW_ORD
4573 : : };
4574 : :
4575 : 0 : if (likely (code < sizeof (known) / sizeof (known[0])))
4576 : 0 : return known[code];
4577 : :
4578 : : return NULL;
4579 : : }
4580 : :
4581 : :
4582 : : static const char *
4583 : 16 : dwarf_discr_list_string (unsigned int code)
4584 : : {
4585 : 16 : static const char *const known[] =
4586 : : {
4587 : : #define DWARF_ONE_KNOWN_DW_DSC(NAME, CODE) [CODE] = #NAME,
4588 : : DWARF_ALL_KNOWN_DW_DSC
4589 : : #undef DWARF_ONE_KNOWN_DW_DSC
4590 : : };
4591 : :
4592 : 16 : if (likely (code < sizeof (known) / sizeof (known[0])))
4593 : 16 : return known[code];
4594 : :
4595 : : return NULL;
4596 : : }
4597 : :
4598 : :
4599 : : static const char *
4600 : 1167366 : dwarf_locexpr_opcode_string (unsigned int code)
4601 : : {
4602 : 1167366 : static const char *const known[] =
4603 : : {
4604 : : /* Normally we can't afford building huge table of 64K entries,
4605 : : most of them zero, just because there are a couple defined
4606 : : values at the far end. In case of opcodes, it's OK. */
4607 : : #define DWARF_ONE_KNOWN_DW_OP(NAME, CODE) [CODE] = #NAME,
4608 : : DWARF_ALL_KNOWN_DW_OP
4609 : : #undef DWARF_ONE_KNOWN_DW_OP
4610 : : };
4611 : :
4612 : 1167366 : if (likely (code < sizeof (known) / sizeof (known[0])))
4613 : 1167366 : return known[code];
4614 : :
4615 : : return NULL;
4616 : : }
4617 : :
4618 : :
4619 : : static const char *
4620 : 3482 : dwarf_unit_string (unsigned int type)
4621 : : {
4622 [ - + - - : 3482 : switch (type)
- - + ]
4623 : : {
4624 : : #define DWARF_ONE_KNOWN_DW_UT(NAME, CODE) case CODE: return #NAME;
4625 : 14 : DWARF_ALL_KNOWN_DW_UT
4626 : : #undef DWARF_ONE_KNOWN_DW_UT
4627 : 0 : default:
4628 : 0 : return NULL;
4629 : : }
4630 : : }
4631 : :
4632 : :
4633 : : static const char *
4634 : 133822 : dwarf_range_list_encoding_string (unsigned int kind)
4635 : : {
4636 [ - + + - : 133822 : switch (kind)
+ - - -
+ ]
4637 : : {
4638 : : #define DWARF_ONE_KNOWN_DW_RLE(NAME, CODE) case CODE: return #NAME;
4639 : 125336 : DWARF_ALL_KNOWN_DW_RLE
4640 : : #undef DWARF_ONE_KNOWN_DW_RLE
4641 : 0 : default:
4642 : 0 : return NULL;
4643 : : }
4644 : : }
4645 : :
4646 : :
4647 : : static const char *
4648 : 637572 : dwarf_loc_list_encoding_string (unsigned int kind)
4649 : : {
4650 [ - - + + : 637572 : switch (kind)
- + - + -
- + ]
4651 : : {
4652 : : #define DWARF_ONE_KNOWN_DW_LLE(NAME, CODE) case CODE: return #NAME;
4653 : 619172 : DWARF_ALL_KNOWN_DW_LLE
4654 : : #undef DWARF_ONE_KNOWN_DW_LLE
4655 : : /* DW_LLE_GNU_view_pair is special/incompatible with default codes. */
4656 : 0 : case DW_LLE_GNU_view_pair: return "GNU_view_pair";
4657 : 0 : default:
4658 : 0 : return NULL;
4659 : : }
4660 : : }
4661 : :
4662 : :
4663 : : static const char *
4664 : 10406 : dwarf_line_content_description_string (unsigned int kind)
4665 : : {
4666 [ + + - - : 10406 : switch (kind)
- + ]
4667 : : {
4668 : : #define DWARF_ONE_KNOWN_DW_LNCT(NAME, CODE) case CODE: return #NAME;
4669 : 10398 : DWARF_ALL_KNOWN_DW_LNCT
4670 : : #undef DWARF_ONE_KNOWN_DW_LNCT
4671 : 0 : default:
4672 : 0 : return NULL;
4673 : : }
4674 : : }
4675 : :
4676 : :
4677 : : /* Used by all dwarf_foo_name functions. */
4678 : : static const char *
4679 : 20864014 : string_or_unknown (const char *known, unsigned int code,
4680 : : unsigned int lo_user, unsigned int hi_user,
4681 : : bool print_unknown_num)
4682 : : {
4683 : 20864014 : static char unknown_buf[20];
4684 : :
4685 [ + + ]: 20864014 : if (likely (known != NULL))
4686 : : return known;
4687 : :
4688 [ - + - - ]: 2 : if (lo_user != 0 && code >= lo_user && code <= hi_user)
4689 : : {
4690 : 0 : snprintf (unknown_buf, sizeof unknown_buf, "lo_user+%#x",
4691 : : code - lo_user);
4692 : 0 : return unknown_buf;
4693 : : }
4694 : :
4695 [ + - ]: 2 : if (print_unknown_num)
4696 : : {
4697 : 2 : snprintf (unknown_buf, sizeof unknown_buf, "??? (%#x)", code);
4698 : 2 : return unknown_buf;
4699 : : }
4700 : :
4701 : : return "???";
4702 : : }
4703 : :
4704 : :
4705 : : static const char *
4706 : 2291264 : dwarf_tag_name (unsigned int tag)
4707 : : {
4708 : 2291264 : const char *ret = dwarf_tag_string (tag);
4709 : 2291264 : return string_or_unknown (ret, tag, DW_TAG_lo_user, DW_TAG_hi_user, true);
4710 : : }
4711 : :
4712 : : static const char *
4713 : 8854376 : dwarf_attr_name (unsigned int attr)
4714 : : {
4715 : 8854376 : const char *ret = dwarf_attr_string (attr);
4716 : 8854376 : return string_or_unknown (ret, attr, DW_AT_lo_user, DW_AT_hi_user, true);
4717 : : }
4718 : :
4719 : :
4720 : : static const char *
4721 : 8864782 : dwarf_form_name (unsigned int form)
4722 : : {
4723 : 8864782 : const char *ret = dwarf_form_string (form);
4724 : 8864782 : return string_or_unknown (ret, form, 0, 0, true);
4725 : : }
4726 : :
4727 : :
4728 : : static const char *
4729 : 3732 : dwarf_lang_name (unsigned int lang)
4730 : : {
4731 : 3732 : const char *ret = dwarf_lang_string (lang);
4732 : 3732 : return string_or_unknown (ret, lang, DW_LANG_lo_user, DW_LANG_hi_user, false);
4733 : : }
4734 : :
4735 : :
4736 : : static const char *
4737 : 4 : dwarf_lname_name (unsigned int lname)
4738 : : {
4739 : 4 : const char *ret = dwarf_lname_string (lname);
4740 : 4 : return string_or_unknown (ret, lname, DW_LNAME_lo_user, DW_LNAME_hi_user,
4741 : : false);
4742 : : }
4743 : :
4744 : :
4745 : : static const char *
4746 : 7206 : dwarf_inline_name (unsigned int code)
4747 : : {
4748 [ + - ]: 7206 : const char *ret = dwarf_inline_string (code);
4749 : 7206 : return string_or_unknown (ret, code, 0, 0, false);
4750 : : }
4751 : :
4752 : :
4753 : : static const char *
4754 : 57348 : dwarf_encoding_name (unsigned int code)
4755 : : {
4756 [ + - ]: 57348 : const char *ret = dwarf_encoding_string (code);
4757 : 57348 : return string_or_unknown (ret, code, DW_ATE_lo_user, DW_ATE_hi_user, false);
4758 : : }
4759 : :
4760 : :
4761 : : static const char *
4762 : 0 : dwarf_access_name (unsigned int code)
4763 : : {
4764 [ # # ]: 0 : const char *ret = dwarf_access_string (code);
4765 : 0 : return string_or_unknown (ret, code, 0, 0, false);
4766 : : }
4767 : :
4768 : :
4769 : : static const char *
4770 : 0 : dwarf_defaulted_name (unsigned int code)
4771 : : {
4772 [ # # ]: 0 : const char *ret = dwarf_defaulted_string (code);
4773 : 0 : return string_or_unknown (ret, code, 0, 0, false);
4774 : : }
4775 : :
4776 : :
4777 : : static const char *
4778 : 0 : dwarf_visibility_name (unsigned int code)
4779 : : {
4780 [ # # ]: 0 : const char *ret = dwarf_visibility_string (code);
4781 : 0 : return string_or_unknown (ret, code, 0, 0, false);
4782 : : }
4783 : :
4784 : :
4785 : : static const char *
4786 : 0 : dwarf_virtuality_name (unsigned int code)
4787 : : {
4788 [ # # ]: 0 : const char *ret = dwarf_virtuality_string (code);
4789 : 0 : return string_or_unknown (ret, code, 0, 0, false);
4790 : : }
4791 : :
4792 : :
4793 : : static const char *
4794 : 0 : dwarf_identifier_case_name (unsigned int code)
4795 : : {
4796 [ # # ]: 0 : const char *ret = dwarf_identifier_case_string (code);
4797 : 0 : return string_or_unknown (ret, code, 0, 0, false);
4798 : : }
4799 : :
4800 : :
4801 : : static const char *
4802 : 0 : dwarf_calling_convention_name (unsigned int code)
4803 : : {
4804 [ # # ]: 0 : const char *ret = dwarf_calling_convention_string (code);
4805 : 0 : return string_or_unknown (ret, code, DW_CC_lo_user, DW_CC_hi_user, false);
4806 : : }
4807 : :
4808 : :
4809 : : static const char *
4810 : 0 : dwarf_ordering_name (unsigned int code)
4811 : : {
4812 [ # # ]: 0 : const char *ret = dwarf_ordering_string (code);
4813 : 0 : return string_or_unknown (ret, code, 0, 0, false);
4814 : : }
4815 : :
4816 : :
4817 : : static const char *
4818 : 16 : dwarf_discr_list_name (unsigned int code)
4819 : : {
4820 [ + - ]: 16 : const char *ret = dwarf_discr_list_string (code);
4821 : 16 : return string_or_unknown (ret, code, 0, 0, false);
4822 : : }
4823 : :
4824 : :
4825 : : static const char *
4826 : 3482 : dwarf_unit_name (unsigned int type)
4827 : : {
4828 : 3482 : const char *ret = dwarf_unit_string (type);
4829 : 3482 : return string_or_unknown (ret, type, DW_UT_lo_user, DW_UT_hi_user, true);
4830 : : }
4831 : :
4832 : :
4833 : : static const char *
4834 : 133822 : dwarf_range_list_encoding_name (unsigned int kind)
4835 : : {
4836 : 133822 : const char *ret = dwarf_range_list_encoding_string (kind);
4837 : 133822 : return string_or_unknown (ret, kind, 0, 0, false);
4838 : : }
4839 : :
4840 : :
4841 : : static const char *
4842 : 637572 : dwarf_loc_list_encoding_name (unsigned int kind)
4843 : : {
4844 : 637572 : const char *ret = dwarf_loc_list_encoding_string (kind);
4845 : 637572 : return string_or_unknown (ret, kind, 0, 0, false);
4846 : : }
4847 : :
4848 : :
4849 : : static const char *
4850 : 10406 : dwarf_line_content_description_name (unsigned int kind)
4851 : : {
4852 : 10406 : const char *ret = dwarf_line_content_description_string (kind);
4853 : 10406 : return string_or_unknown (ret, kind, DW_LNCT_lo_user, DW_LNCT_hi_user,
4854 : : false);
4855 : : }
4856 : :
4857 : :
4858 : : static void
4859 : 510 : print_block (size_t n, const void *block)
4860 : : {
4861 [ - + ]: 510 : if (n == 0)
4862 : 0 : puts (_("empty block"));
4863 : : else
4864 : : {
4865 : 510 : printf (_("%zu byte block:"), n);
4866 : 510 : const unsigned char *data = block;
4867 : 2250 : do
4868 : 2250 : printf (" %02x", *data++);
4869 [ + + ]: 2250 : while (--n > 0);
4870 : 510 : putchar ('\n');
4871 : : }
4872 : 510 : }
4873 : :
4874 : : static void
4875 : 24 : print_bytes (size_t n, const unsigned char *bytes)
4876 : : {
4877 : 24 : while (n-- > 0)
4878 : : {
4879 : 384 : printf ("%02x", *bytes++);
4880 [ + + ]: 384 : if (n > 0)
4881 [ + + ]: 768 : printf (" ");
4882 : : }
4883 : 24 : }
4884 : :
4885 : : static int
4886 : 148 : get_indexed_addr (Dwarf_CU *cu, Dwarf_Word idx, Dwarf_Addr *addr)
4887 : : {
4888 [ - + ]: 148 : if (cu == NULL)
4889 : : return -1;
4890 : :
4891 : 148 : Elf_Data *debug_addr = cu->dbg->sectiondata[IDX_debug_addr];
4892 [ - + ]: 148 : if (debug_addr == NULL)
4893 : : return -1;
4894 : :
4895 : 148 : Dwarf_Off base = __libdw_cu_addr_base (cu);
4896 : 148 : Dwarf_Word off = idx * cu->address_size;
4897 [ - + ]: 148 : if (base > debug_addr->d_size
4898 [ - + ]: 148 : || off > debug_addr->d_size - base
4899 [ - + ]: 148 : || cu->address_size > debug_addr->d_size - base - off)
4900 : : return -1;
4901 : :
4902 : 148 : const unsigned char *addrp = debug_addr->d_buf + base + off;
4903 [ + + ]: 148 : if (cu->address_size == 4)
4904 [ - + ]: 12 : *addr = read_4ubyte_unaligned (cu->dbg, addrp);
4905 : : else
4906 [ - + ]: 136 : *addr = read_8ubyte_unaligned (cu->dbg, addrp);
4907 : :
4908 : : return 0;
4909 : : }
4910 : :
4911 : : static void
4912 : 797224 : print_ops (Dwfl_Module *dwflmod, Dwarf *dbg, int indent, int indentrest,
4913 : : unsigned int vers, unsigned int addrsize, unsigned int offset_size,
4914 : : struct Dwarf_CU *cu, Dwarf_Word len, const unsigned char *data)
4915 : : {
4916 [ + + ]: 797224 : const unsigned int ref_size = vers < 3 ? addrsize : offset_size;
4917 : :
4918 [ - + ]: 797224 : if (len == 0)
4919 : : {
4920 : 0 : printf ("%*s(empty)\n", indent, "");
4921 : 0 : return;
4922 : : }
4923 : :
4924 : : #define NEED(n) if (len < (Dwarf_Word) (n)) goto invalid
4925 : : #define CONSUME(n) NEED (n); else len -= (n)
4926 : :
4927 : : Dwarf_Word offset = 0;
4928 [ + + ]: 1964590 : while (len-- > 0)
4929 : 1167366 : {
4930 : 1167366 : uint_fast8_t op = *data++;
4931 : :
4932 [ + - ]: 1167366 : const char *op_name = dwarf_locexpr_opcode_string (op);
4933 [ - + ]: 1167366 : if (unlikely (op_name == NULL))
4934 : : {
4935 : 0 : static char buf[20];
4936 [ # # ]: 0 : if (op >= DW_OP_lo_user)
4937 : 0 : snprintf (buf, sizeof buf, "lo_user+%#x", op - DW_OP_lo_user);
4938 : : else
4939 : 0 : snprintf (buf, sizeof buf, "??? (%#x)", op);
4940 : : op_name = buf;
4941 : : }
4942 : :
4943 [ + - + + : 1167366 : switch (op)
+ + + + -
- + + - +
- - + + +
+ + - - -
- + + + ]
4944 : : {
4945 : 26262 : case DW_OP_addr:;
4946 : : /* Address operand. */
4947 : 26262 : Dwarf_Word addr;
4948 [ - + ]: 26262 : NEED (addrsize);
4949 [ + + ]: 26262 : if (addrsize == 4)
4950 [ + + ]: 108 : addr = read_4ubyte_unaligned (dbg, data);
4951 [ + - ]: 26154 : else if (addrsize == 8)
4952 [ + + ]: 26154 : addr = read_8ubyte_unaligned (dbg, data);
4953 : : else
4954 : 0 : goto invalid;
4955 : 26262 : data += addrsize;
4956 : 26262 : CONSUME (addrsize);
4957 : :
4958 : 26262 : printf ("%*s[%2" PRIuMAX "] %s ",
4959 : : indent, "", (uintmax_t) offset, op_name);
4960 : 26262 : print_dwarf_addr (dwflmod, 0, addr, addr);
4961 : 26262 : printf ("\n");
4962 : :
4963 : 26262 : offset += 1 + addrsize;
4964 : 26262 : break;
4965 : :
4966 : 0 : case DW_OP_call_ref:
4967 : : case DW_OP_GNU_variable_value:
4968 : : /* Offset operand. */
4969 [ # # ]: 0 : if (ref_size != 4 && ref_size != 8)
4970 : 0 : goto invalid; /* Cannot be used in CFA. */
4971 [ # # ]: 0 : NEED (ref_size);
4972 [ # # ]: 0 : if (ref_size == 4)
4973 [ # # ]: 0 : addr = read_4ubyte_unaligned (dbg, data);
4974 : : else
4975 [ # # ]: 0 : addr = read_8ubyte_unaligned (dbg, data);
4976 : 0 : data += ref_size;
4977 : 0 : CONSUME (ref_size);
4978 : : /* addr is a DIE offset, so format it as one. */
4979 : 0 : printf ("%*s[%2" PRIuMAX "] %s [%6" PRIxMAX "]\n",
4980 : : indent, "", (uintmax_t) offset,
4981 : : op_name, (uintmax_t) addr);
4982 : 0 : offset += 1 + ref_size;
4983 : 0 : break;
4984 : :
4985 : 27438 : case DW_OP_deref_size:
4986 : : case DW_OP_xderef_size:
4987 : : case DW_OP_pick:
4988 : : case DW_OP_const1u:
4989 : : // XXX value might be modified by relocation
4990 [ - + ]: 27438 : NEED (1);
4991 : 54876 : printf ("%*s[%2" PRIuMAX "] %s %" PRIu8 "\n",
4992 : : indent, "", (uintmax_t) offset,
4993 : 27438 : op_name, *((uint8_t *) data));
4994 : 27438 : ++data;
4995 : 27438 : --len;
4996 : 27438 : offset += 2;
4997 : 27438 : break;
4998 : :
4999 : 3304 : case DW_OP_const2u:
5000 [ - + ]: 3304 : NEED (2);
5001 : : // XXX value might be modified by relocation
5002 : 0 : printf ("%*s[%2" PRIuMAX "] %s %" PRIu16 "\n",
5003 : : indent, "", (uintmax_t) offset,
5004 [ - + ]: 3304 : op_name, read_2ubyte_unaligned (dbg, data));
5005 : 3304 : CONSUME (2);
5006 : 3304 : data += 2;
5007 : 3304 : offset += 3;
5008 : 3304 : break;
5009 : :
5010 : 2324 : case DW_OP_const4u:
5011 [ - + ]: 2324 : NEED (4);
5012 : : // XXX value might be modified by relocation
5013 : 2324 : printf ("%*s[%2" PRIuMAX "] %s %" PRIu32 "\n",
5014 : : indent, "", (uintmax_t) offset,
5015 [ - + ]: 2324 : op_name, read_4ubyte_unaligned (dbg, data));
5016 : 2324 : CONSUME (4);
5017 : 2324 : data += 4;
5018 : 2324 : offset += 5;
5019 : 2324 : break;
5020 : :
5021 : 112 : case DW_OP_const8u:
5022 [ - + ]: 112 : NEED (8);
5023 : : // XXX value might be modified by relocation
5024 : 112 : printf ("%*s[%2" PRIuMAX "] %s %" PRIu64 "\n",
5025 : : indent, "", (uintmax_t) offset,
5026 [ - + ]: 112 : op_name, (uint64_t) read_8ubyte_unaligned (dbg, data));
5027 : 112 : CONSUME (8);
5028 : 112 : data += 8;
5029 : 112 : offset += 9;
5030 : 112 : break;
5031 : :
5032 : 4962 : case DW_OP_const1s:
5033 [ - + ]: 4962 : NEED (1);
5034 : : // XXX value might be modified by relocation
5035 : 9924 : printf ("%*s[%2" PRIuMAX "] %s %" PRId8 "\n",
5036 : : indent, "", (uintmax_t) offset,
5037 : 4962 : op_name, *((int8_t *) data));
5038 : 4962 : ++data;
5039 : 4962 : --len;
5040 : 4962 : offset += 2;
5041 : 4962 : break;
5042 : :
5043 : 10 : case DW_OP_const2s:
5044 [ - + ]: 10 : NEED (2);
5045 : : // XXX value might be modified by relocation
5046 : 0 : printf ("%*s[%2" PRIuMAX "] %s %" PRId16 "\n",
5047 : : indent, "", (uintmax_t) offset,
5048 [ - + ]: 10 : op_name, read_2sbyte_unaligned (dbg, data));
5049 : 10 : CONSUME (2);
5050 : 10 : data += 2;
5051 : 10 : offset += 3;
5052 : 10 : break;
5053 : :
5054 : 0 : case DW_OP_const4s:
5055 [ # # ]: 0 : NEED (4);
5056 : : // XXX value might be modified by relocation
5057 : 0 : printf ("%*s[%2" PRIuMAX "] %s %" PRId32 "\n",
5058 : : indent, "", (uintmax_t) offset,
5059 [ # # ]: 0 : op_name, read_4sbyte_unaligned (dbg, data));
5060 : 0 : CONSUME (4);
5061 : 0 : data += 4;
5062 : 0 : offset += 5;
5063 : 0 : break;
5064 : :
5065 : 0 : case DW_OP_const8s:
5066 [ # # ]: 0 : NEED (8);
5067 : : // XXX value might be modified by relocation
5068 : 0 : printf ("%*s[%2" PRIuMAX "] %s %" PRId64 "\n",
5069 : : indent, "", (uintmax_t) offset,
5070 [ # # ]: 0 : op_name, read_8sbyte_unaligned (dbg, data));
5071 : 0 : CONSUME (8);
5072 : 0 : data += 8;
5073 : 0 : offset += 9;
5074 : 0 : break;
5075 : :
5076 : 27556 : case DW_OP_piece:
5077 : : case DW_OP_regx:
5078 : : case DW_OP_plus_uconst:
5079 : 27556 : case DW_OP_constu:;
5080 : 27556 : const unsigned char *start = data;
5081 : 27556 : uint64_t uleb;
5082 [ - + ]: 27556 : NEED (1);
5083 : 27556 : get_uleb128 (uleb, data, data + len);
5084 : 27556 : printf ("%*s[%2" PRIuMAX "] %s %" PRIu64 "\n",
5085 : : indent, "", (uintmax_t) offset, op_name, uleb);
5086 [ - + ]: 27556 : CONSUME (data - start);
5087 : 27556 : offset += 1 + (data - start);
5088 : 27556 : break;
5089 : :
5090 : 16 : case DW_OP_addrx:
5091 : : case DW_OP_GNU_addr_index:
5092 : : case DW_OP_constx:
5093 : 16 : case DW_OP_GNU_const_index:;
5094 : 16 : start = data;
5095 [ - + ]: 16 : NEED (1);
5096 : 16 : get_uleb128 (uleb, data, data + len);
5097 : 16 : printf ("%*s[%2" PRIuMAX "] %s [%" PRIu64 "] ",
5098 : : indent, "", (uintmax_t) offset, op_name, uleb);
5099 [ - + ]: 16 : CONSUME (data - start);
5100 : 16 : offset += 1 + (data - start);
5101 [ - + ]: 16 : if (get_indexed_addr (cu, uleb, &addr) != 0)
5102 : 0 : printf ("???\n");
5103 : : else
5104 : : {
5105 : 16 : print_dwarf_addr (dwflmod, 0, addr, addr);
5106 : 16 : printf ("\n");
5107 : : }
5108 : : break;
5109 : :
5110 : 0 : case DW_OP_bit_piece:
5111 : 0 : start = data;
5112 : 0 : uint64_t uleb2;
5113 [ # # ]: 0 : NEED (1);
5114 : 0 : get_uleb128 (uleb, data, data + len);
5115 : 0 : NEED (1);
5116 : 0 : get_uleb128 (uleb2, data, data + len);
5117 : 0 : printf ("%*s[%2" PRIuMAX "] %s %" PRIu64 ", %" PRIu64 "\n",
5118 : : indent, "", (uintmax_t) offset, op_name, uleb, uleb2);
5119 [ # # ]: 0 : CONSUME (data - start);
5120 : 0 : offset += 1 + (data - start);
5121 : 0 : break;
5122 : :
5123 : 205012 : case DW_OP_fbreg:
5124 : : case DW_OP_breg0 ... DW_OP_breg31:
5125 : : case DW_OP_consts:
5126 : 205012 : start = data;
5127 : 205012 : int64_t sleb;
5128 [ - + ]: 205012 : NEED (1);
5129 : 205012 : get_sleb128 (sleb, data, data + len);
5130 : 205012 : printf ("%*s[%2" PRIuMAX "] %s %" PRId64 "\n",
5131 : : indent, "", (uintmax_t) offset, op_name, sleb);
5132 [ - + ]: 205012 : CONSUME (data - start);
5133 : 205012 : offset += 1 + (data - start);
5134 : 205012 : break;
5135 : :
5136 : 0 : case DW_OP_bregx:
5137 : 0 : start = data;
5138 [ # # ]: 0 : NEED (1);
5139 : 0 : get_uleb128 (uleb, data, data + len);
5140 : 0 : NEED (1);
5141 : 0 : get_sleb128 (sleb, data, data + len);
5142 : 0 : printf ("%*s[%2" PRIuMAX "] %s %" PRIu64 " %" PRId64 "\n",
5143 : : indent, "", (uintmax_t) offset, op_name, uleb, sleb);
5144 [ # # ]: 0 : CONSUME (data - start);
5145 : 0 : offset += 1 + (data - start);
5146 : 0 : break;
5147 : :
5148 : 0 : case DW_OP_call2:
5149 [ # # ]: 0 : NEED (2);
5150 : 0 : printf ("%*s[%2" PRIuMAX "] %s [%6" PRIx16 "]\n",
5151 : : indent, "", (uintmax_t) offset, op_name,
5152 [ # # ]: 0 : read_2ubyte_unaligned (dbg, data));
5153 : 0 : CONSUME (2);
5154 : 0 : data += 2;
5155 : 0 : offset += 3;
5156 : 0 : break;
5157 : :
5158 : 8 : case DW_OP_call4:
5159 [ - + ]: 8 : NEED (4);
5160 : 8 : printf ("%*s[%2" PRIuMAX "] %s [%6" PRIx32 "]\n",
5161 : : indent, "", (uintmax_t) offset, op_name,
5162 [ - + ]: 8 : read_4ubyte_unaligned (dbg, data));
5163 : 8 : CONSUME (4);
5164 : 8 : data += 4;
5165 : 8 : offset += 5;
5166 : 8 : break;
5167 : :
5168 : 1152 : case DW_OP_skip:
5169 : : case DW_OP_bra:
5170 [ - + ]: 1152 : NEED (2);
5171 : 2304 : printf ("%*s[%2" PRIuMAX "] %s %" PRIuMAX "\n",
5172 : : indent, "", (uintmax_t) offset, op_name,
5173 [ - + ]: 1152 : (uintmax_t) (offset + read_2sbyte_unaligned (dbg, data) + 3));
5174 : 1152 : CONSUME (2);
5175 : 1152 : data += 2;
5176 : 1152 : offset += 3;
5177 : 1152 : break;
5178 : :
5179 : 510 : case DW_OP_implicit_value:
5180 : 510 : start = data;
5181 [ - + ]: 510 : NEED (1);
5182 : 510 : get_uleb128 (uleb, data, data + len);
5183 : 510 : printf ("%*s[%2" PRIuMAX "] %s: ",
5184 : : indent, "", (uintmax_t) offset, op_name);
5185 [ - + ]: 510 : NEED (uleb);
5186 : 510 : print_block (uleb, data);
5187 : 510 : data += uleb;
5188 [ - + ]: 510 : CONSUME (data - start);
5189 : 510 : offset += 1 + (data - start);
5190 : 510 : break;
5191 : :
5192 : 5024 : case DW_OP_implicit_pointer:
5193 : : case DW_OP_GNU_implicit_pointer:
5194 : : /* DIE offset operand. */
5195 : 5024 : start = data;
5196 [ - + ]: 5024 : NEED (ref_size);
5197 [ - + ]: 5024 : if (ref_size != 4 && ref_size != 8)
5198 : 0 : goto invalid; /* Cannot be used in CFA. */
5199 [ + - ]: 5024 : if (ref_size == 4)
5200 [ - + ]: 5024 : addr = read_4ubyte_unaligned (dbg, data);
5201 : : else
5202 [ # # ]: 0 : addr = read_8ubyte_unaligned (dbg, data);
5203 : 5024 : data += ref_size;
5204 : : /* Byte offset operand. */
5205 [ - + ]: 5024 : NEED (1);
5206 : 5024 : get_sleb128 (sleb, data, data + len);
5207 : :
5208 : 5024 : printf ("%*s[%2" PRIuMAX "] %s [%6" PRIxMAX "] %+" PRId64 "\n",
5209 : : indent, "", (intmax_t) offset,
5210 : : op_name, (uintmax_t) addr, sleb);
5211 [ - + ]: 5024 : CONSUME (data - start);
5212 : 5024 : offset += 1 + (data - start);
5213 : 5024 : break;
5214 : :
5215 : 64566 : case DW_OP_entry_value:
5216 : : case DW_OP_GNU_entry_value:
5217 : : /* Size plus expression block. */
5218 : 64566 : start = data;
5219 [ - + ]: 64566 : NEED (1);
5220 : 64566 : get_uleb128 (uleb, data, data + len);
5221 : 64566 : printf ("%*s[%2" PRIuMAX "] %s:\n",
5222 : : indent, "", (uintmax_t) offset, op_name);
5223 [ - + ]: 64566 : NEED (uleb);
5224 : 64566 : print_ops (dwflmod, dbg, indent + 5, indent + 5, vers,
5225 : : addrsize, offset_size, cu, uleb, data);
5226 : 64566 : data += uleb;
5227 [ - + ]: 64566 : CONSUME (data - start);
5228 : 64566 : offset += 1 + (data - start);
5229 : 64566 : break;
5230 : :
5231 : 0 : case DW_OP_const_type:
5232 : : case DW_OP_GNU_const_type:
5233 : : /* uleb128 CU relative DW_TAG_base_type DIE offset, 1-byte
5234 : : unsigned size plus block. */
5235 : 0 : start = data;
5236 [ # # ]: 0 : NEED (1);
5237 : 0 : get_uleb128 (uleb, data, data + len);
5238 [ # # # # ]: 0 : if (! print_unresolved_addresses && cu != NULL)
5239 : 0 : uleb += cu->start;
5240 : 0 : NEED (1);
5241 : 0 : uint8_t usize = *(uint8_t *) data++;
5242 [ # # ]: 0 : NEED (usize);
5243 : 0 : printf ("%*s[%2" PRIuMAX "] %s [%6" PRIxMAX "] ",
5244 : : indent, "", (uintmax_t) offset, op_name, uleb);
5245 : 0 : print_block (usize, data);
5246 : 0 : data += usize;
5247 [ # # ]: 0 : CONSUME (data - start);
5248 : 0 : offset += 1 + (data - start);
5249 : 0 : break;
5250 : :
5251 : 0 : case DW_OP_regval_type:
5252 : : case DW_OP_GNU_regval_type:
5253 : : /* uleb128 register number, uleb128 CU relative
5254 : : DW_TAG_base_type DIE offset. */
5255 : 0 : start = data;
5256 [ # # ]: 0 : NEED (1);
5257 : 0 : get_uleb128 (uleb, data, data + len);
5258 : 0 : NEED (1);
5259 : 0 : get_uleb128 (uleb2, data, data + len);
5260 [ # # # # ]: 0 : if (! print_unresolved_addresses && cu != NULL)
5261 : 0 : uleb2 += cu->start;
5262 : 0 : printf ("%*s[%2" PRIuMAX "] %s %" PRIu64 " [%6" PRIx64 "]\n",
5263 : : indent, "", (uintmax_t) offset, op_name, uleb, uleb2);
5264 [ # # ]: 0 : CONSUME (data - start);
5265 : 0 : offset += 1 + (data - start);
5266 : 0 : break;
5267 : :
5268 : 0 : case DW_OP_deref_type:
5269 : : case DW_OP_GNU_deref_type:
5270 : : /* 1-byte unsigned size of value, uleb128 CU relative
5271 : : DW_TAG_base_type DIE offset. */
5272 : 0 : start = data;
5273 [ # # ]: 0 : NEED (1);
5274 : 0 : usize = *(uint8_t *) data++;
5275 : 0 : NEED (1);
5276 : 0 : get_uleb128 (uleb, data, data + len);
5277 [ # # # # ]: 0 : if (! print_unresolved_addresses && cu != NULL)
5278 : 0 : uleb += cu->start;
5279 : 0 : printf ("%*s[%2" PRIuMAX "] %s %" PRIu8 " [%6" PRIxMAX "]\n",
5280 : : indent, "", (uintmax_t) offset,
5281 : : op_name, usize, uleb);
5282 [ # # ]: 0 : CONSUME (data - start);
5283 : 0 : offset += 1 + (data - start);
5284 : 0 : break;
5285 : :
5286 : 0 : case DW_OP_xderef_type:
5287 : : /* 1-byte unsigned size of value, uleb128 base_type DIE offset. */
5288 : 0 : start = data;
5289 [ # # ]: 0 : NEED (1);
5290 : 0 : usize = *(uint8_t *) data++;
5291 : 0 : NEED (1);
5292 : 0 : get_uleb128 (uleb, data, data + len);
5293 : 0 : printf ("%*s[%4" PRIuMAX "] %s %" PRIu8 " [%6" PRIxMAX "]\n",
5294 : : indent, "", (uintmax_t) offset,
5295 : : op_name, usize, uleb);
5296 [ # # ]: 0 : CONSUME (data - start);
5297 : 0 : offset += 1 + (data - start);
5298 : 0 : break;
5299 : :
5300 : 810 : case DW_OP_convert:
5301 : : case DW_OP_GNU_convert:
5302 : : case DW_OP_reinterpret:
5303 : : case DW_OP_GNU_reinterpret:
5304 : : /* uleb128 CU relative offset to DW_TAG_base_type, or zero
5305 : : for conversion to untyped. */
5306 : 810 : start = data;
5307 [ - + ]: 810 : NEED (1);
5308 : 810 : get_uleb128 (uleb, data, data + len);
5309 [ + + + - : 810 : if (uleb != 0 && ! print_unresolved_addresses && cu != NULL)
+ - ]
5310 : 540 : uleb += cu->start;
5311 : 810 : printf ("%*s[%2" PRIuMAX "] %s [%6" PRIxMAX "]\n",
5312 : : indent, "", (uintmax_t) offset, op_name, uleb);
5313 [ - + ]: 810 : CONSUME (data - start);
5314 : 810 : offset += 1 + (data - start);
5315 : 810 : break;
5316 : :
5317 : 8 : case DW_OP_GNU_parameter_ref:
5318 : : /* 4 byte CU relative reference to the abstract optimized away
5319 : : DW_TAG_formal_parameter. */
5320 [ - + ]: 8 : NEED (4);
5321 [ - + ]: 8 : uintmax_t param_off = (uintmax_t) read_4ubyte_unaligned (dbg, data);
5322 [ + - + - ]: 8 : if (! print_unresolved_addresses && cu != NULL)
5323 : 8 : param_off += cu->start;
5324 : 8 : printf ("%*s[%2" PRIuMAX "] %s [%6" PRIxMAX "]\n",
5325 : : indent, "", (uintmax_t) offset, op_name, param_off);
5326 : 8 : CONSUME (4);
5327 : 8 : data += 4;
5328 : 8 : offset += 5;
5329 : 8 : break;
5330 : :
5331 : : default:
5332 : : /* No Operand. */
5333 : 798292 : printf ("%*s[%2" PRIuMAX "] %s\n",
5334 : : indent, "", (uintmax_t) offset, op_name);
5335 : 798292 : ++offset;
5336 : 798292 : break;
5337 : : }
5338 : :
5339 : 1167366 : indent = indentrest;
5340 : 1167366 : continue;
5341 : :
5342 : 0 : invalid:
5343 : 0 : printf (_("%*s[%2" PRIuMAX "] %s <TRUNCATED>\n"),
5344 : : indent, "", (uintmax_t) offset, op_name);
5345 : : break;
5346 : : }
5347 : : }
5348 : :
5349 : :
5350 : : /* Turn the addresses into file offsets by using the phdrs. */
5351 : : static void
5352 : 2 : find_offsets(Elf *elf, GElf_Addr main_bias, size_t n,
5353 : : GElf_Addr addrs[n], GElf_Off offs[n])
5354 : : {
5355 : 2 : size_t unsolved = n;
5356 [ + + ]: 18 : for (size_t i = 0; i < phnum; ++i) {
5357 : 16 : GElf_Phdr phdr_mem;
5358 : 16 : GElf_Phdr *phdr = gelf_getphdr(elf, i, &phdr_mem);
5359 [ + - + + : 16 : if (phdr != NULL && phdr->p_type == PT_LOAD && phdr->p_memsz > 0)
+ - ]
5360 [ + + ]: 46 : for (size_t j = 0; j < n; ++j)
5361 [ + + + + ]: 44 : if (offs[j] == 0 && addrs[j] >= phdr->p_vaddr + main_bias &&
5362 [ + - ]: 22 : addrs[j] - (phdr->p_vaddr + main_bias) < phdr->p_filesz) {
5363 : 22 : offs[j] = addrs[j] - (phdr->p_vaddr + main_bias) + phdr->p_offset;
5364 [ + + ]: 22 : if (--unsolved == 0)
5365 : : break;
5366 : : }
5367 : : }
5368 : 2 : }
5369 : :
5370 : : /* The dynamic segment (type PT_DYNAMIC), contains the .dynamic section.
5371 : : And .dynamic section contains an array of the dynamic structures.
5372 : : We use the array to get:
5373 : : DT_STRTAB: the address of the string table
5374 : : DT_SYMTAB: the address of the symbol table
5375 : : DT_STRSZ: the size, in bytes, of the string table
5376 : : ... */
5377 : : static void
5378 : 2 : get_dynscn_addrs(Elf *elf, GElf_Phdr *phdr, GElf_Addr addrs[i_max])
5379 : : {
5380 : 2 : Elf_Data *data = elf_getdata_rawchunk(
5381 : 2 : elf, phdr->p_offset, phdr->p_filesz, ELF_T_DYN);
5382 : :
5383 : 2 : int dyn_idx = 0;
5384 : 44 : for (;; ++dyn_idx) {
5385 : 46 : GElf_Dyn dyn_mem;
5386 : 46 : GElf_Dyn *dyn = gelf_getdyn(data, dyn_idx, &dyn_mem);
5387 : : /* DT_NULL Marks end of dynamic section. */
5388 [ + - + + ]: 46 : if (dyn == NULL || dyn->d_tag == DT_NULL)
5389 : : break;
5390 : :
5391 [ + - + + : 44 : switch (dyn->d_tag) {
+ + + + +
+ - + ]
5392 : 2 : case DT_SYMTAB:
5393 : 2 : addrs[i_symtab] = dyn->d_un.d_ptr;
5394 : 2 : break;
5395 : :
5396 : 0 : case DT_HASH:
5397 : 0 : addrs[i_hash] = dyn->d_un.d_ptr;
5398 : 0 : break;
5399 : :
5400 : 2 : case DT_GNU_HASH:
5401 : 2 : addrs[i_gnu_hash] = dyn->d_un.d_ptr;
5402 : 2 : break;
5403 : :
5404 : 2 : case DT_STRTAB:
5405 : 2 : addrs[i_strtab] = dyn->d_un.d_ptr;
5406 : 2 : break;
5407 : :
5408 : 2 : case DT_VERSYM:
5409 : 2 : addrs[i_versym] = dyn->d_un.d_ptr;
5410 : 2 : break;
5411 : :
5412 : 2 : case DT_VERDEF:
5413 : 2 : addrs[i_verdef] = dyn->d_un.d_ptr;
5414 : 2 : break;
5415 : :
5416 : 2 : case DT_VERDEFNUM:
5417 : 2 : addrs[i_verdefnum] = dyn->d_un.d_val;
5418 : 2 : break;
5419 : :
5420 : 2 : case DT_VERNEED:
5421 : 2 : addrs[i_verneed] = dyn->d_un.d_ptr;
5422 : 2 : break;
5423 : :
5424 : 2 : case DT_VERNEEDNUM:
5425 : 2 : addrs[i_verneednum] = dyn->d_un.d_val;
5426 : 2 : break;
5427 : :
5428 : 2 : case DT_STRSZ:
5429 : 2 : addrs[i_strsz] = dyn->d_un.d_val;
5430 : 2 : break;
5431 : :
5432 : 0 : case DT_SYMTAB_SHNDX:
5433 : 0 : addrs[i_symtab_shndx] = dyn->d_un.d_ptr;
5434 : 0 : break;
5435 : : }
5436 : : }
5437 : 2 : }
5438 : :
5439 : :
5440 : : /* Use dynamic segment to get data for the string table section. */
5441 : : static Elf_Data *
5442 : 2 : get_dynscn_strtab(Elf *elf, GElf_Phdr *phdr)
5443 : : {
5444 : 2 : Elf_Data *strtab_data;
5445 : 2 : GElf_Addr addrs[i_max] = {0,};
5446 : 2 : GElf_Off offs[i_max] = {0,};
5447 : 2 : get_dynscn_addrs(elf, phdr, addrs);
5448 : 2 : find_offsets(elf, 0, i_max, addrs, offs);
5449 : 4 : strtab_data = elf_getdata_rawchunk(
5450 : 2 : elf, offs[i_strtab], addrs[i_strsz], ELF_T_BYTE);
5451 : 2 : return strtab_data;
5452 : : }
5453 : :
5454 : :
5455 : : struct listptr
5456 : : {
5457 : : Dwarf_Off offset:(64 - 3);
5458 : : bool addr64:1;
5459 : : bool dwarf64:1;
5460 : : bool warned:1;
5461 : : struct Dwarf_CU *cu;
5462 : : unsigned int attr;
5463 : : };
5464 : :
5465 : : #define listptr_offset_size(p) ((p)->dwarf64 ? 8 : 4)
5466 : : #define listptr_address_size(p) ((p)->addr64 ? 8 : 4)
5467 : :
5468 : : static Dwarf_Addr
5469 : 21780 : cudie_base (Dwarf_Die *cudie)
5470 : : {
5471 : 21780 : Dwarf_Addr base;
5472 : : /* Find the base address of the compilation unit. It will normally
5473 : : be specified by DW_AT_low_pc. In DWARF-3 draft 4, the base
5474 : : address could be overridden by DW_AT_entry_pc. It's been
5475 : : removed, but GCC emits DW_AT_entry_pc and not DW_AT_lowpc for
5476 : : compilation units with discontinuous ranges. */
5477 [ - + ]: 21780 : if (unlikely (dwarf_lowpc (cudie, &base) != 0))
5478 : : {
5479 : 0 : Dwarf_Attribute attr_mem;
5480 [ # # ]: 0 : if (dwarf_formaddr (dwarf_attr (cudie, DW_AT_entry_pc, &attr_mem),
5481 : : &base) != 0)
5482 : 0 : base = 0;
5483 : : }
5484 : 21780 : return base;
5485 : : }
5486 : :
5487 : : static Dwarf_Addr
5488 : 21780 : listptr_base (struct listptr *p)
5489 : : {
5490 : 21780 : Dwarf_Die cu = CUDIE (p->cu);
5491 : 21780 : return cudie_base (&cu);
5492 : : }
5493 : :
5494 : : /* To store the name used in compare_listptr */
5495 : : static const char *sort_listptr_name;
5496 : :
5497 : : static int
5498 : 1772982 : compare_listptr (const void *a, const void *b)
5499 : : {
5500 : 1772982 : const char *name = sort_listptr_name;
5501 : 1772982 : struct listptr *p1 = (void *) a;
5502 : 1772982 : struct listptr *p2 = (void *) b;
5503 : :
5504 [ + + ]: 1772982 : if (p1->offset < p2->offset)
5505 : : return -1;
5506 [ + + ]: 169258 : if (p1->offset > p2->offset)
5507 : : return 1;
5508 : :
5509 [ + - - + ]: 8260 : if (!p1->warned && !p2->warned)
5510 : : {
5511 [ - + ]: 8260 : if (p1->addr64 != p2->addr64)
5512 : : {
5513 : 0 : p1->warned = p2->warned = true;
5514 : 8260 : error (0, 0,
5515 : 0 : _("%s %#" PRIx64 " used with different address sizes"),
5516 : : name, (uint64_t) p1->offset);
5517 : : }
5518 [ - + ]: 8260 : if (p1->dwarf64 != p2->dwarf64)
5519 : : {
5520 : 0 : p1->warned = p2->warned = true;
5521 : 8260 : error (0, 0,
5522 : 0 : _("%s %#" PRIx64 " used with different offset sizes"),
5523 : 0 : name, (uint64_t) p1->offset);
5524 : : }
5525 [ - + ]: 8260 : if (listptr_base (p1) != listptr_base (p2))
5526 : : {
5527 : 0 : p1->warned = p2->warned = true;
5528 : 8260 : error (0, 0,
5529 : 0 : _("%s %#" PRIx64 " used with different base addresses"),
5530 : 0 : name, (uint64_t) p1->offset);
5531 : : }
5532 [ + - ]: 8260 : if (p1->attr != p2 ->attr)
5533 : : {
5534 : 0 : p1->warned = p2->warned = true;
5535 : 0 : error (0, 0,
5536 : 0 : _("%s %#" PRIx64
5537 : : " used with different attribute %s and %s"),
5538 : 0 : name, (uint64_t) p1->offset, dwarf_attr_name (p1->attr),
5539 : : dwarf_attr_name (p2->attr));
5540 : : }
5541 : : }
5542 : :
5543 : : return 0;
5544 : : }
5545 : :
5546 : : struct listptr_table
5547 : : {
5548 : : size_t n;
5549 : : size_t alloc;
5550 : : struct listptr *table;
5551 : : };
5552 : :
5553 : : static struct listptr_table known_locsptr;
5554 : : static struct listptr_table known_loclistsptr;
5555 : : static struct listptr_table known_rangelistptr;
5556 : : static struct listptr_table known_rnglistptr;
5557 : : static struct listptr_table known_addrbases;
5558 : : static struct listptr_table known_stroffbases;
5559 : :
5560 : : static void
5561 : 502 : reset_listptr (struct listptr_table *table)
5562 : : {
5563 : 502 : free (table->table);
5564 : 502 : table->table = NULL;
5565 : 502 : table->n = table->alloc = 0;
5566 : : }
5567 : :
5568 : : /* Returns false if offset doesn't fit. See struct listptr. */
5569 : : static bool
5570 : 255910 : notice_listptr (enum section_e section, struct listptr_table *table,
5571 : : uint_fast8_t address_size, uint_fast8_t offset_size,
5572 : : struct Dwarf_CU *cu, Dwarf_Off offset, unsigned int attr)
5573 : : {
5574 [ + + ]: 255910 : if (print_debug_sections & section)
5575 : : {
5576 [ + + ]: 255658 : if (table->n == table->alloc)
5577 : : {
5578 [ + + ]: 408 : if (table->alloc == 0)
5579 : 174 : table->alloc = 128;
5580 : : else
5581 : 234 : table->alloc *= 2;
5582 : 408 : table->table = xrealloc (table->table,
5583 : 408 : table->alloc * sizeof table->table[0]);
5584 : : }
5585 : :
5586 : 255658 : struct listptr *p = &table->table[table->n++];
5587 : :
5588 : 255658 : *p = (struct listptr)
5589 : : {
5590 : 255658 : .addr64 = address_size == 8,
5591 : 255658 : .dwarf64 = offset_size == 8,
5592 : : .offset = offset,
5593 : : .cu = cu,
5594 : : .attr = attr
5595 : : };
5596 : :
5597 [ + - ]: 255658 : if (p->offset != offset)
5598 : : {
5599 : 0 : table->n--;
5600 : 0 : return false;
5601 : : }
5602 : : }
5603 : : return true;
5604 : : }
5605 : :
5606 : : static void
5607 : 182 : sort_listptr (struct listptr_table *table, const char *name)
5608 : : {
5609 [ + + ]: 182 : if (table->n > 0)
5610 : : {
5611 : 174 : sort_listptr_name = name;
5612 : 174 : qsort (table->table, table->n, sizeof table->table[0],
5613 : : &compare_listptr);
5614 : : }
5615 : 182 : }
5616 : :
5617 : : static bool
5618 : 208 : skip_listptr_hole (struct listptr_table *table, size_t *idxp,
5619 : : uint_fast8_t *address_sizep, uint_fast8_t *offset_sizep,
5620 : : Dwarf_Addr *base, struct Dwarf_CU **cu, ptrdiff_t offset,
5621 : : unsigned char **readp, unsigned char *endp,
5622 : : unsigned int *attr)
5623 : : {
5624 [ - + ]: 208 : if (table->n == 0)
5625 : : return false;
5626 : :
5627 [ + - + + ]: 336 : while (*idxp < table->n && table->table[*idxp].offset < (Dwarf_Off) offset)
5628 : 128 : ++*idxp;
5629 : :
5630 : 208 : struct listptr *p = &table->table[*idxp];
5631 : :
5632 [ + - ]: 208 : if (*idxp == table->n
5633 [ - + ]: 208 : || p->offset >= (Dwarf_Off) (endp - *readp + offset))
5634 : : {
5635 : 0 : *readp = endp;
5636 : 0 : printf (_(" [%6tx] <UNUSED GARBAGE IN REST OF SECTION>\n"),
5637 : : offset);
5638 : 0 : return true;
5639 : : }
5640 : :
5641 [ - + ]: 208 : if (p->offset != (Dwarf_Off) offset)
5642 : : {
5643 : 0 : *readp += p->offset - offset;
5644 : 0 : printf (_(" [%6tx] <UNUSED GARBAGE> ... %" PRIu64 " bytes ...\n"),
5645 : : offset, (Dwarf_Off) p->offset - offset);
5646 : 0 : return true;
5647 : : }
5648 : :
5649 [ + - ]: 208 : if (address_sizep != NULL)
5650 [ + + ]: 224 : *address_sizep = listptr_address_size (p);
5651 [ + + ]: 208 : if (offset_sizep != NULL)
5652 [ + - ]: 296 : *offset_sizep = listptr_offset_size (p);
5653 [ + - ]: 208 : if (base != NULL)
5654 : 208 : *base = listptr_base (p);
5655 [ + - ]: 208 : if (cu != NULL)
5656 : 208 : *cu = p->cu;
5657 [ + + ]: 208 : if (attr != NULL)
5658 : 148 : *attr = p->attr;
5659 : :
5660 : : return false;
5661 : : }
5662 : :
5663 : : static Dwarf_Off
5664 : 109978 : next_listptr_offset (struct listptr_table *table, size_t *idxp, Dwarf_Off off)
5665 : : {
5666 : : /* Note that multiple attributes could in theory point to the same loclist
5667 : : offset, so make sure we pick one that is bigger than the current one.
5668 : : The table is sorted on offset. */
5669 [ + - ]: 109978 : if (*idxp < table->n)
5670 : : {
5671 [ + - ]: 218732 : while (++*idxp < table->n)
5672 : : {
5673 : 218732 : Dwarf_Off next = table->table[*idxp].offset;
5674 [ + + ]: 218732 : if (next > off)
5675 : 109978 : return next;
5676 : : }
5677 : : }
5678 : : return 0;
5679 : : }
5680 : :
5681 : : /* Returns the listptr associated with the given index, or NULL. */
5682 : : static struct listptr *
5683 : 1495528 : get_listptr (struct listptr_table *table, size_t idx)
5684 : : {
5685 : 1495528 : if (idx >= table->n)
5686 : : return NULL;
5687 : 1495276 : return &table->table[idx];
5688 : : }
5689 : :
5690 : : /* Returns the next index, base address and CU associated with the
5691 : : list unit offsets. If there is none false is returned, otherwise
5692 : : true. Assumes the table has been sorted. */
5693 : : static bool
5694 : 5052 : listptr_cu (struct listptr_table *table, size_t *idxp,
5695 : : Dwarf_Off start, Dwarf_Off end,
5696 : : Dwarf_Addr *base, struct Dwarf_CU **cu)
5697 : : {
5698 : 5052 : while (*idxp < table->n
5699 [ + - + + ]: 38928 : && table->table[*idxp].offset < start)
5700 : 33876 : ++*idxp;
5701 : :
5702 [ + - ]: 5052 : if (*idxp < table->n
5703 [ + - ]: 5052 : && table->table[*idxp].offset >= start
5704 [ + - ]: 5052 : && table->table[*idxp].offset < end)
5705 : : {
5706 : 5052 : struct listptr *p = &table->table[*idxp];
5707 : 5052 : *base = listptr_base (p);
5708 : 5052 : *cu = p->cu;
5709 : 5052 : return true;
5710 : : }
5711 : :
5712 : : return false;
5713 : : }
5714 : :
5715 : : /* Returns the next index with the current CU for the given attribute.
5716 : : If there is none false is returned, otherwise true. Assumes the
5717 : : table has been sorted. */
5718 : : static bool
5719 : 747550 : listptr_attr (struct listptr_table *table, size_t idxp,
5720 : : Dwarf_Off offset, unsigned int attr)
5721 : : {
5722 : 1495492 : struct listptr *listptr;
5723 : 1495492 : do
5724 : : {
5725 [ + + ]: 1495492 : listptr = get_listptr (table, idxp);
5726 [ + - ]: 1495250 : if (listptr == NULL)
5727 : : return false;
5728 : :
5729 [ + + + + ]: 1495250 : if (listptr->offset == offset && listptr->attr == attr)
5730 : : return true;
5731 : :
5732 : 1385272 : idxp++;
5733 : : }
5734 [ + + ]: 1385272 : while (listptr->offset <= offset);
5735 : :
5736 : : return false;
5737 : : }
5738 : :
5739 : : static void
5740 : 88 : print_debug_abbrev_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
5741 : : Ebl *ebl, GElf_Ehdr *ehdr __attribute__ ((unused)),
5742 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
5743 : : {
5744 : 88 : Elf_Data *elf_data = get_debug_elf_data (dbg, ebl, IDX_debug_abbrev, scn);
5745 [ + - ]: 88 : if (elf_data == NULL)
5746 : : return;
5747 : :
5748 : 88 : const size_t sh_size = elf_data->d_size;
5749 : :
5750 : 88 : printf (_("\nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"
5751 : : " [ Code]\n"),
5752 : : elf_ndxscn (scn), section_name (ebl, shdr),
5753 : 88 : (uint64_t) shdr->sh_offset);
5754 : :
5755 : 88 : Dwarf_Off offset = 0;
5756 [ + + ]: 3678 : while (offset < sh_size)
5757 : : {
5758 : 3590 : printf (_("\nAbbreviation section at offset %" PRIu64 ":\n"),
5759 : : offset);
5760 : :
5761 : 330866 : while (1)
5762 : 163638 : {
5763 : 167228 : size_t length;
5764 : 167228 : Dwarf_Abbrev abbrev;
5765 : :
5766 : 167228 : int res = dwarf_offabbrev (dbg, offset, &length, &abbrev);
5767 [ + + ]: 167228 : if (res != 0)
5768 : : {
5769 [ - + ]: 3590 : if (unlikely (res < 0))
5770 : : {
5771 : 0 : printf (_("\
5772 : : *** error while reading abbreviation: %s\n"),
5773 : : dwarf_errmsg (-1));
5774 : 0 : return;
5775 : : }
5776 : :
5777 : : /* This is the NUL byte at the end of the section. */
5778 : 3590 : ++offset;
5779 : 3590 : break;
5780 : : }
5781 : :
5782 : : /* We know these calls can never fail. */
5783 : 163638 : unsigned int code = dwarf_getabbrevcode (&abbrev);
5784 : 163638 : unsigned int tag = dwarf_getabbrevtag (&abbrev);
5785 : 163638 : int has_children = dwarf_abbrevhaschildren (&abbrev);
5786 : :
5787 [ + + ]: 163638 : printf (_(" [%5u] offset: %" PRId64
5788 : : ", children: %s, tag: %s\n"),
5789 : : code, (int64_t) offset,
5790 : : has_children ? yes_str : no_str,
5791 : : dwarf_tag_name (tag));
5792 : :
5793 : 163638 : size_t cnt = 0;
5794 : 163638 : unsigned int name;
5795 : 163638 : unsigned int form;
5796 : 163638 : Dwarf_Sword data;
5797 : 163638 : Dwarf_Off enoffset;
5798 : 163638 : while (dwarf_getabbrevattr_data (&abbrev, cnt, &name, &form,
5799 [ + + ]: 935666 : &data, &enoffset) == 0)
5800 : : {
5801 : 772028 : printf (" attr: %s, form: %s",
5802 : : dwarf_attr_name (name), dwarf_form_name (form));
5803 [ + + ]: 772028 : if (form == DW_FORM_implicit_const)
5804 : 55350 : printf (" (%" PRId64 ")", data);
5805 : 772028 : printf (", offset: %#" PRIx64 "\n", (uint64_t) enoffset);
5806 : 772028 : ++cnt;
5807 : : }
5808 : :
5809 : 163638 : offset += length;
5810 : : }
5811 : : }
5812 : : }
5813 : :
5814 : :
5815 : : static void
5816 : 8 : print_debug_addr_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
5817 : : Ebl *ebl, GElf_Ehdr *ehdr,
5818 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
5819 : : {
5820 : 8 : Elf_Data *data = get_debug_elf_data (dbg, ebl, IDX_debug_addr, scn);
5821 [ + - ]: 8 : if (data == NULL)
5822 : : return;
5823 : :
5824 : 8 : printf (_("\
5825 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
5826 : : elf_ndxscn (scn), section_name (ebl, shdr),
5827 : 8 : (uint64_t) shdr->sh_offset);
5828 : :
5829 [ + - ]: 8 : if (shdr->sh_size == 0)
5830 : : return;
5831 : :
5832 : 8 : size_t idx = 0;
5833 : 8 : sort_listptr (&known_addrbases, "addr_base");
5834 : :
5835 : 8 : const unsigned char *start = (const unsigned char *) data->d_buf;
5836 : 8 : const unsigned char *readp = start;
5837 : 8 : const unsigned char *readendp = ((const unsigned char *) data->d_buf
5838 : 8 : + data->d_size);
5839 : :
5840 : 8 : while (readp < readendp)
5841 : : {
5842 : : /* We cannot really know whether or not there is an header. The
5843 : : DebugFission extension to DWARF4 doesn't add one. The DWARF5
5844 : : .debug_addr variant does. Whether or not we have an header,
5845 : : DW_AT_[GNU_]addr_base points at "index 0". So if the current
5846 : : offset equals the CU addr_base then we can just start
5847 : : printing addresses. If there is no CU with an exact match
5848 : : then we'll try to parse the header first. */
5849 : 16 : Dwarf_Off off = (Dwarf_Off) (readp
5850 : 16 : - (const unsigned char *) data->d_buf);
5851 : :
5852 : 16 : printf ("Table at offset %" PRIx64 " ", off);
5853 : :
5854 [ + - ]: 16 : struct listptr *listptr = get_listptr (&known_addrbases, idx++);
5855 : 16 : const unsigned char *next_unitp;
5856 : :
5857 : 16 : uint64_t unit_length;
5858 : 16 : uint16_t version;
5859 : 16 : uint8_t address_size;
5860 : 16 : uint8_t segment_size;
5861 [ - + ]: 16 : if (listptr == NULL)
5862 : : {
5863 : 0 : error (0, 0, "Warning: No CU references .debug_addr after %" PRIx64,
5864 : : off);
5865 : :
5866 : : /* We will have to assume it is just addresses to the end... */
5867 [ # # ]: 0 : address_size = ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 4 : 8;
5868 : 0 : next_unitp = readendp;
5869 : 0 : printf ("Unknown CU:\n");
5870 : : }
5871 : : else
5872 : : {
5873 : 16 : Dwarf_Die cudie;
5874 [ - + ]: 16 : if (dwarf_cu_die (listptr->cu, &cudie,
5875 : : NULL, NULL, NULL, NULL,
5876 : : NULL, NULL) == NULL)
5877 : 0 : printf ("Unknown CU (%s):\n", dwarf_errmsg (-1));
5878 : : else
5879 : 16 : printf ("for CU [%6" PRIx64 "]:\n", dwarf_dieoffset (&cudie));
5880 : :
5881 [ + + ]: 16 : if (listptr->offset == off)
5882 : : {
5883 [ - + ]: 4 : address_size = listptr_address_size (listptr);
5884 : 4 : segment_size = 0;
5885 : 4 : version = 4;
5886 : :
5887 : : /* The addresses start here, but where do they end? */
5888 [ + + ]: 4 : listptr = get_listptr (&known_addrbases, idx);
5889 [ - + ]: 2 : if (listptr == NULL)
5890 : : next_unitp = readendp;
5891 [ + - ]: 2 : else if (listptr->cu->version < 5)
5892 : : {
5893 : 2 : next_unitp = start + listptr->offset;
5894 [ + - - + ]: 2 : if (listptr->offset < off || listptr->offset > data->d_size)
5895 : : {
5896 : 0 : error (0, 0,
5897 : : "Warning: Bad address base for next unit at %"
5898 : : PRIx64, off);
5899 : 0 : next_unitp = readendp;
5900 : : }
5901 : : }
5902 : : else
5903 : : {
5904 : : /* Tricky, we don't have a header for this unit, but
5905 : : there is one for the next. We will have to
5906 : : "guess" how big it is and subtract it from the
5907 : : offset (because that points after the header). */
5908 [ # # ]: 0 : unsigned int offset_size = listptr_offset_size (listptr);
5909 : 0 : Dwarf_Off next_off = (listptr->offset
5910 : 0 : - (offset_size == 4 ? 4 : 12) /* len */
5911 : : - 2 /* version */
5912 : : - 1 /* address size */
5913 : 0 : - 1); /* segment selector size */
5914 : 0 : next_unitp = start + next_off;
5915 [ # # # # ]: 0 : if (next_off < off || next_off > data->d_size)
5916 : : {
5917 : 0 : error (0, 0,
5918 : : "Warning: Couldn't calculate .debug_addr "
5919 : : " unit length at %" PRIx64, off);
5920 : 0 : next_unitp = readendp;
5921 : : }
5922 : : }
5923 : 4 : unit_length = (uint64_t) (next_unitp - readp);
5924 : :
5925 : : /* Pretend we have a header. */
5926 : 4 : printf ("\n");
5927 : 4 : printf (_(" Length: %8" PRIu64 "\n"),
5928 : : unit_length);
5929 : 4 : printf (_(" DWARF version: %8" PRIu16 "\n"), version);
5930 : 4 : printf (_(" Address size: %8" PRIu64 "\n"),
5931 : : (uint64_t) address_size);
5932 : 4 : printf (_(" Segment size: %8" PRIu64 "\n"),
5933 : : (uint64_t) segment_size);
5934 : 16 : printf ("\n");
5935 : : }
5936 : : else
5937 : : {
5938 : : /* OK, we have to parse an header first. */
5939 [ - + ]: 12 : unit_length = read_4ubyte_unaligned_inc (dbg, readp);
5940 [ - + ]: 12 : if (unlikely (unit_length == 0xffffffff))
5941 : : {
5942 [ # # ]: 0 : if (unlikely (readp > readendp - 8))
5943 : : {
5944 : 0 : invalid_data:
5945 : 0 : error (0, 0, "Invalid data");
5946 : 0 : return;
5947 : : }
5948 [ # # ]: 0 : unit_length = read_8ubyte_unaligned_inc (dbg, readp);
5949 : : }
5950 : 12 : printf ("\n");
5951 : 12 : printf (_(" Length: %8" PRIu64 "\n"),
5952 : : unit_length);
5953 : :
5954 : : /* We need at least 2-bytes (version) + 1-byte
5955 : : (addr_size) + 1-byte (segment_size) = 4 bytes to
5956 : : complete the header. And this unit cannot go beyond
5957 : : the section data. */
5958 [ + - ]: 12 : if (readp > readendp - 4
5959 [ + - ]: 12 : || unit_length < 4
5960 [ - + ]: 12 : || unit_length > (uint64_t) (readendp - readp))
5961 : 0 : goto invalid_data;
5962 : :
5963 : 12 : next_unitp = readp + unit_length;
5964 : :
5965 [ - + ]: 12 : version = read_2ubyte_unaligned_inc (dbg, readp);
5966 : 12 : printf (_(" DWARF version: %8" PRIu16 "\n"), version);
5967 : :
5968 [ - + ]: 12 : if (version != 5)
5969 : : {
5970 : 0 : error (0, 0, _("Unknown version"));
5971 : 0 : goto next_unit;
5972 : : }
5973 : :
5974 : 12 : address_size = *readp++;
5975 : 12 : printf (_(" Address size: %8" PRIu64 "\n"),
5976 : : (uint64_t) address_size);
5977 : :
5978 [ - + ]: 12 : if (address_size != 4 && address_size != 8)
5979 : : {
5980 : 0 : error (0, 0, _("unsupported address size"));
5981 : 0 : goto next_unit;
5982 : : }
5983 : :
5984 : 12 : segment_size = *readp++;
5985 : 12 : printf (_(" Segment size: %8" PRIu64 "\n"),
5986 : : (uint64_t) segment_size);
5987 : 12 : printf ("\n");
5988 : :
5989 [ - + ]: 12 : if (segment_size != 0)
5990 : : {
5991 : 0 : error (0, 0, _("unsupported segment size"));
5992 : 0 : goto next_unit;
5993 : : }
5994 : :
5995 [ - + ]: 12 : if (listptr->offset != (Dwarf_Off) (readp - start))
5996 : : {
5997 : 0 : error (0, 0, "Address index doesn't start after header");
5998 : 0 : goto next_unit;
5999 : : }
6000 : : }
6001 : : }
6002 : :
6003 : 16 : int digits = 1;
6004 : 16 : size_t addresses = (next_unitp - readp) / address_size;
6005 [ + + ]: 24 : while (addresses >= 10)
6006 : : {
6007 : 8 : ++digits;
6008 : 8 : addresses /= 10;
6009 : : }
6010 : :
6011 : 16 : unsigned int uidx = 0;
6012 : 16 : size_t index_offset = readp - (const unsigned char *) data->d_buf;
6013 : 16 : printf (" Addresses start at offset 0x%zx:\n", index_offset);
6014 : 188 : while (readp <= next_unitp - address_size)
6015 : : {
6016 [ - + + + : 172 : Dwarf_Addr addr = read_addr_unaligned_inc (address_size, dbg,
- + - + ]
6017 : : readp);
6018 : 172 : printf (" [%*u] ", digits, uidx++);
6019 : 172 : print_dwarf_addr (dwflmod, address_size, addr, addr);
6020 [ + + ]: 188 : printf ("\n");
6021 : : }
6022 : 16 : printf ("\n");
6023 : :
6024 [ + - ]: 16 : if (readp != next_unitp)
6025 [ + + ]: 24 : error (0, 0, "extra %zd bytes at end of unit",
6026 : 0 : (size_t) (next_unitp - readp));
6027 : :
6028 : 16 : next_unit:
6029 : : readp = next_unitp;
6030 : : }
6031 : : }
6032 : :
6033 : : /* Print content of DWARF .debug_aranges section. We fortunately do
6034 : : not have to know a bit about the structure of the section, libdwarf
6035 : : takes care of it. */
6036 : : static void
6037 : 2 : print_decoded_aranges_section (Ebl *ebl, GElf_Ehdr *ehdr, Elf_Scn *scn,
6038 : : GElf_Shdr *shdr, Dwarf *dbg)
6039 : : {
6040 : 2 : Dwarf_Aranges *aranges;
6041 : 2 : size_t cnt;
6042 [ - + ]: 2 : if (unlikely (dwarf_getaranges (dbg, &aranges, &cnt) != 0))
6043 : : {
6044 : 0 : error (0, 0, _("cannot get .debug_aranges content: %s"),
6045 : : dwarf_errmsg (-1));
6046 : 0 : return;
6047 : : }
6048 : :
6049 : 2 : GElf_Shdr glink_mem;
6050 : 2 : GElf_Shdr *glink;
6051 : 2 : glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link), &glink_mem);
6052 [ - + ]: 2 : if (glink == NULL)
6053 : : {
6054 : 0 : error (0, 0, _("invalid sh_link value in section %zu"),
6055 : : elf_ndxscn (scn));
6056 : 0 : return;
6057 : : }
6058 : :
6059 : 2 : printf (ngettext ("\
6060 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 " contains %zu entry:\n",
6061 : : "\
6062 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 " contains %zu entries:\n",
6063 : : cnt),
6064 : : elf_ndxscn (scn), section_name (ebl, shdr),
6065 : 2 : (uint64_t) shdr->sh_offset, cnt);
6066 : :
6067 : : /* Compute floor(log16(cnt)). */
6068 : 2 : size_t tmp = cnt;
6069 : 2 : int digits = 1;
6070 [ - + ]: 2 : while (tmp >= 16)
6071 : : {
6072 : 0 : ++digits;
6073 : 0 : tmp >>= 4;
6074 : : }
6075 : :
6076 [ + + ]: 12 : for (size_t n = 0; n < cnt; ++n)
6077 : : {
6078 : 10 : Dwarf_Arange *runp = dwarf_onearange (aranges, n);
6079 [ - + ]: 10 : if (unlikely (runp == NULL))
6080 : : {
6081 : 0 : printf ("cannot get arange %zu: %s\n", n, dwarf_errmsg (-1));
6082 : 0 : return;
6083 : : }
6084 : :
6085 : 10 : Dwarf_Addr start;
6086 : 10 : Dwarf_Word length;
6087 : 10 : Dwarf_Off offset;
6088 : :
6089 [ - + ]: 10 : if (unlikely (dwarf_getarangeinfo (runp, &start, &length, &offset) != 0))
6090 : 0 : printf (_(" [%*zu] ???\n"), digits, n);
6091 : : else
6092 : 10 : printf (_(" [%*zu] start: %0#*" PRIx64
6093 : : ", length: %5" PRIu64 ", CU DIE offset: %6"
6094 : : PRId64 "\n"),
6095 [ - + ]: 10 : digits, n, ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 10 : 18,
6096 : : (uint64_t) start, (uint64_t) length, (int64_t) offset);
6097 : : }
6098 : : }
6099 : :
6100 : :
6101 : : /* Print content of DWARF .debug_aranges section. */
6102 : : static void
6103 : 92 : print_debug_aranges_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
6104 : : Ebl *ebl, GElf_Ehdr *ehdr, Elf_Scn *scn,
6105 : : GElf_Shdr *shdr, Dwarf *dbg)
6106 : : {
6107 [ + + ]: 92 : if (decodedaranges)
6108 : : {
6109 : 2 : print_decoded_aranges_section (ebl, ehdr, scn, shdr, dbg);
6110 : 2 : return;
6111 : : }
6112 : :
6113 : 90 : Elf_Data *data = get_debug_elf_data (dbg, ebl, IDX_debug_aranges, scn);
6114 [ + - ]: 90 : if (data == NULL)
6115 : : return;
6116 : :
6117 : 90 : printf (_("\
6118 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
6119 : : elf_ndxscn (scn), section_name (ebl, shdr),
6120 : 90 : (uint64_t) shdr->sh_offset);
6121 : :
6122 : 90 : const unsigned char *readp = data->d_buf;
6123 : 90 : const unsigned char *readendp = readp + data->d_size;
6124 : :
6125 [ + + ]: 3662 : while (readp < readendp)
6126 : : {
6127 : 3572 : const unsigned char *hdrstart = readp;
6128 : 3572 : size_t start_offset = hdrstart - (const unsigned char *) data->d_buf;
6129 : :
6130 : 3572 : printf (_("\nTable at offset %zu:\n"), start_offset);
6131 [ - + ]: 3572 : if (readp + 4 > readendp)
6132 : : {
6133 : 0 : invalid_data:
6134 : 0 : error (0, 0, _("invalid data in section [%zu] '%s'"),
6135 : : elf_ndxscn (scn), section_name (ebl, shdr));
6136 : 0 : return;
6137 : : }
6138 : :
6139 [ + + ]: 3572 : Dwarf_Word length = read_4ubyte_unaligned_inc (dbg, readp);
6140 : 3572 : unsigned int length_bytes = 4;
6141 [ - + ]: 3572 : if (length == DWARF3_LENGTH_64_BIT)
6142 : : {
6143 [ # # ]: 0 : if (readp + 8 > readendp)
6144 : 0 : goto invalid_data;
6145 [ # # ]: 0 : length = read_8ubyte_unaligned_inc (dbg, readp);
6146 : 0 : length_bytes = 8;
6147 : : }
6148 : :
6149 : 3572 : const unsigned char *nexthdr = readp + length;
6150 : 3572 : printf (_("\n Length: %6" PRIu64 "\n"),
6151 : : (uint64_t) length);
6152 : :
6153 [ - + ]: 3572 : if (unlikely (length > (size_t) (readendp - readp)))
6154 : 0 : goto invalid_data;
6155 : :
6156 [ - + ]: 3572 : if (length == 0)
6157 : 0 : continue;
6158 : :
6159 [ - + ]: 3572 : if (readp + 2 > readendp)
6160 : 0 : goto invalid_data;
6161 [ + + ]: 3572 : uint_fast16_t version = read_2ubyte_unaligned_inc (dbg, readp);
6162 : 3572 : printf (_(" DWARF version: %6" PRIuFAST16 "\n"),
6163 : : version);
6164 [ - + ]: 3572 : if (version != 2)
6165 : : {
6166 : 0 : error (0, 0, _("unsupported aranges version"));
6167 : 0 : goto next_table;
6168 : : }
6169 : :
6170 : 3572 : Dwarf_Word offset;
6171 [ - + ]: 3572 : if (readp + length_bytes > readendp)
6172 : 0 : goto invalid_data;
6173 [ - + ]: 3572 : if (length_bytes == 8)
6174 [ # # ]: 0 : offset = read_8ubyte_unaligned_inc (dbg, readp);
6175 : : else
6176 [ + + ]: 3572 : offset = read_4ubyte_unaligned_inc (dbg, readp);
6177 : 3572 : printf (_(" CU offset: %6" PRIx64 "\n"),
6178 : : (uint64_t) offset);
6179 : :
6180 [ - + ]: 3572 : if (readp + 1 > readendp)
6181 : 0 : goto invalid_data;
6182 : 3572 : unsigned int address_size = *readp++;
6183 : 3572 : printf (_(" Address size: %6" PRIu64 "\n"),
6184 : : (uint64_t) address_size);
6185 [ - + ]: 3572 : if (address_size != 4 && address_size != 8)
6186 : : {
6187 : 0 : error (0, 0, _("unsupported address size"));
6188 : 0 : goto next_table;
6189 : : }
6190 : :
6191 [ - + ]: 3572 : if (readp + 1 > readendp)
6192 : 0 : goto invalid_data;
6193 : 3572 : unsigned int segment_size = *readp++;
6194 : 3572 : printf (_(" Segment size: %6" PRIu64 "\n\n"),
6195 : : (uint64_t) segment_size);
6196 [ - + - - ]: 3572 : if (segment_size != 0 && segment_size != 4 && segment_size != 8)
6197 : : {
6198 : 0 : error (0, 0, _("unsupported segment size"));
6199 : 0 : goto next_table;
6200 : : }
6201 : :
6202 : : /* Round the address to the next multiple of 2*address_size. */
6203 : 3572 : readp += ((2 * address_size - ((readp - hdrstart) % (2 * address_size)))
6204 : 3572 : % (2 * address_size));
6205 : :
6206 : 3572 : while (readp < nexthdr)
6207 : : {
6208 : 7430 : Dwarf_Word range_address;
6209 : 7430 : Dwarf_Word range_length;
6210 : 7430 : Dwarf_Word segment = 0;
6211 [ - + ]: 7430 : if (readp + 2 * address_size + segment_size > readendp)
6212 : 0 : goto invalid_data;
6213 [ + + ]: 7430 : if (address_size == 4)
6214 : : {
6215 [ + + ]: 92 : range_address = read_4ubyte_unaligned_inc (dbg, readp);
6216 [ + + ]: 92 : range_length = read_4ubyte_unaligned_inc (dbg, readp);
6217 : : }
6218 : : else
6219 : : {
6220 [ + + ]: 7338 : range_address = read_8ubyte_unaligned_inc (dbg, readp);
6221 [ + + ]: 7338 : range_length = read_8ubyte_unaligned_inc (dbg, readp);
6222 : : }
6223 : :
6224 [ - + ]: 7430 : if (segment_size == 4)
6225 [ # # ]: 0 : segment = read_4ubyte_unaligned_inc (dbg, readp);
6226 [ - + ]: 7430 : else if (segment_size == 8)
6227 [ # # ]: 0 : segment = read_8ubyte_unaligned_inc (dbg, readp);
6228 : :
6229 [ + + ]: 7430 : if (range_address == 0 && range_length == 0 && segment == 0)
6230 : : break;
6231 : :
6232 : 3858 : printf (" ");
6233 : 3858 : print_dwarf_addr (dwflmod, address_size, range_address,
6234 : : range_address);
6235 : 3858 : printf ("..");
6236 : 3858 : print_dwarf_addr (dwflmod, address_size,
6237 : 3858 : range_address + range_length - 1,
6238 : : range_length);
6239 [ - + ]: 3858 : if (segment_size != 0)
6240 : 0 : printf (" (%" PRIx64 ")\n", (uint64_t) segment);
6241 : : else
6242 [ + - ]: 11288 : printf ("\n");
6243 : : }
6244 : :
6245 : 3572 : next_table:
6246 [ - + ]: 3572 : if (readp != nexthdr)
6247 : : {
6248 : 0 : size_t padding = nexthdr - readp;
6249 : 0 : printf (_(" %zu padding bytes\n"), padding);
6250 : 0 : readp = nexthdr;
6251 : : }
6252 : : }
6253 : : }
6254 : :
6255 : :
6256 : : static bool is_split_dwarf (Dwarf *dbg, uint64_t *id, Dwarf_CU **split_cu);
6257 : :
6258 : : /* Returns true and sets cu and cu_base if the given Dwarf is a split
6259 : : DWARF (.dwo) file. */
6260 : : static bool
6261 : 0 : split_dwarf_cu_base (Dwarf *dbg, Dwarf_CU **cu, Dwarf_Addr *cu_base)
6262 : : {
6263 : 0 : uint64_t id;
6264 [ # # ]: 0 : if (is_split_dwarf (dbg, &id, cu))
6265 : : {
6266 : 0 : Dwarf_Die cudie;
6267 [ # # ]: 0 : if (dwarf_cu_info (*cu, NULL, NULL, &cudie, NULL, NULL, NULL, NULL) == 0)
6268 : : {
6269 : 0 : *cu_base = cudie_base (&cudie);
6270 : 0 : return true;
6271 : : }
6272 : : }
6273 : : return false;
6274 : : }
6275 : :
6276 : : /* Print content of DWARF .debug_rnglists section. */
6277 : : static void
6278 : 38 : print_debug_rnglists_section (Dwfl_Module *dwflmod,
6279 : : Ebl *ebl,
6280 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
6281 : : Elf_Scn *scn, GElf_Shdr *shdr,
6282 : : Dwarf *dbg __attribute__((unused)))
6283 : : {
6284 : 38 : Elf_Data *data = get_debug_elf_data (dbg, ebl, IDX_debug_rnglists, scn);
6285 [ + - ]: 38 : if (data == NULL)
6286 : 0 : return;
6287 : :
6288 : 38 : printf (_("\
6289 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
6290 : : elf_ndxscn (scn), section_name (ebl, shdr),
6291 : 38 : (uint64_t) shdr->sh_offset);
6292 : :
6293 : : /* For the listptr to get the base address/CU. */
6294 : 38 : sort_listptr (&known_rnglistptr, "rnglistptr");
6295 : 38 : size_t listptr_idx = 0;
6296 : :
6297 : 38 : const unsigned char *readp = data->d_buf;
6298 : 38 : const unsigned char *const dataend = ((unsigned char *) data->d_buf
6299 : 38 : + data->d_size);
6300 [ + + ]: 2002 : while (readp < dataend)
6301 : : {
6302 [ - + ]: 1964 : if (unlikely (readp > dataend - 4))
6303 : : {
6304 : 0 : invalid_data:
6305 : 0 : error (0, 0, _("invalid data in section [%zu] '%s'"),
6306 : : elf_ndxscn (scn), section_name (ebl, shdr));
6307 : 0 : return;
6308 : : }
6309 : :
6310 : 1964 : ptrdiff_t offset = readp - (unsigned char *) data->d_buf;
6311 : 1964 : printf (_("Table at Offset 0x%" PRIx64 ":\n\n"),
6312 : : (uint64_t) offset);
6313 : :
6314 [ - + ]: 1964 : uint64_t unit_length = read_4ubyte_unaligned_inc (dbg, readp);
6315 : 1964 : unsigned int offset_size = 4;
6316 [ - + ]: 1964 : if (unlikely (unit_length == 0xffffffff))
6317 : : {
6318 [ # # ]: 0 : if (unlikely (readp > dataend - 8))
6319 : 0 : goto invalid_data;
6320 : :
6321 [ # # ]: 0 : unit_length = read_8ubyte_unaligned_inc (dbg, readp);
6322 : 0 : offset_size = 8;
6323 : : }
6324 : 1964 : printf (_(" Length: %8" PRIu64 "\n"), unit_length);
6325 : :
6326 : : /* We need at least 2-bytes + 1-byte + 1-byte + 4-bytes = 8
6327 : : bytes to complete the header. And this unit cannot go beyond
6328 : : the section data. */
6329 [ + - ]: 1964 : if (readp > dataend - 8
6330 [ + - ]: 1964 : || unit_length < 8
6331 [ - + ]: 1964 : || unit_length > (uint64_t) (dataend - readp))
6332 : 0 : goto invalid_data;
6333 : :
6334 : 1964 : const unsigned char *nexthdr = readp + unit_length;
6335 : :
6336 [ - + ]: 1964 : uint16_t version = read_2ubyte_unaligned_inc (dbg, readp);
6337 : 1964 : printf (_(" DWARF version: %8" PRIu16 "\n"), version);
6338 : :
6339 [ - + ]: 1964 : if (version != 5)
6340 : : {
6341 : 0 : error (0, 0, _("Unknown version"));
6342 : 0 : goto next_table;
6343 : : }
6344 : :
6345 : 1964 : uint8_t address_size = *readp++;
6346 : 1964 : printf (_(" Address size: %8" PRIu64 "\n"),
6347 : : (uint64_t) address_size);
6348 : :
6349 [ - + ]: 1964 : if (address_size != 4 && address_size != 8)
6350 : : {
6351 : 0 : error (0, 0, _("unsupported address size"));
6352 : 0 : goto next_table;
6353 : : }
6354 : :
6355 : 1964 : uint8_t segment_size = *readp++;
6356 : 1964 : printf (_(" Segment size: %8" PRIu64 "\n"),
6357 : : (uint64_t) segment_size);
6358 : :
6359 [ - + - - ]: 1964 : if (segment_size != 0 && segment_size != 4 && segment_size != 8)
6360 : : {
6361 : 0 : error (0, 0, _("unsupported segment size"));
6362 : 0 : goto next_table;
6363 : : }
6364 : :
6365 [ - + ]: 1964 : uint32_t offset_entry_count = read_4ubyte_unaligned_inc (dbg, readp);
6366 : 1964 : printf (_(" Offset entries: %8" PRIu64 "\n"),
6367 : : (uint64_t) offset_entry_count);
6368 : :
6369 : : /* We need the CU that uses this unit to get the initial base address. */
6370 : 1964 : Dwarf_Addr cu_base = 0;
6371 : 1964 : struct Dwarf_CU *cu = NULL;
6372 [ - + ]: 1964 : if (listptr_cu (&known_rnglistptr, &listptr_idx,
6373 : : (Dwarf_Off) offset,
6374 : 1964 : (Dwarf_Off) (nexthdr - (unsigned char *) data->d_buf),
6375 : : &cu_base, &cu)
6376 [ # # ]: 0 : || split_dwarf_cu_base (dbg, &cu, &cu_base))
6377 : 1964 : {
6378 : 1964 : Dwarf_Die cudie;
6379 [ - + ]: 1964 : if (dwarf_cu_die (cu, &cudie,
6380 : : NULL, NULL, NULL, NULL,
6381 : : NULL, NULL) == NULL)
6382 : 0 : printf (_(" Unknown CU base: "));
6383 : : else
6384 : 1964 : printf (_(" CU [%6" PRIx64 "] base: "),
6385 : : dwarf_dieoffset (&cudie));
6386 : 1964 : print_dwarf_addr (dwflmod, address_size, cu_base, cu_base);
6387 : 1964 : printf ("\n");
6388 : : }
6389 : : else
6390 : 0 : printf (_(" Not associated with a CU.\n"));
6391 : :
6392 : 1964 : printf ("\n");
6393 : :
6394 : 1964 : const unsigned char *offset_array_start = readp;
6395 [ + + ]: 1964 : if (offset_entry_count > 0)
6396 : : {
6397 : 2 : uint64_t max_entries = (unit_length - 8) / offset_size;
6398 [ - + ]: 2 : if (offset_entry_count > max_entries)
6399 : : {
6400 : 0 : error (0, 0,
6401 : 0 : _("too many offset entries for unit length"));
6402 : 0 : offset_entry_count = max_entries;
6403 : : }
6404 : :
6405 : 2 : printf (_(" Offsets starting at 0x%" PRIx64 ":\n"),
6406 : : (uint64_t) (offset_array_start
6407 : 2 : - (unsigned char *) data->d_buf));
6408 [ + + ]: 6 : for (uint32_t idx = 0; idx < offset_entry_count; idx++)
6409 : : {
6410 : 4 : printf (" [%6" PRIu32 "] ", idx);
6411 [ + - ]: 4 : if (offset_size == 4)
6412 : : {
6413 [ - + ]: 4 : uint32_t off = read_4ubyte_unaligned_inc (dbg, readp);
6414 : 4 : printf ("0x%" PRIx32 "\n", off);
6415 : : }
6416 : : else
6417 : : {
6418 [ # # ]: 0 : uint64_t off = read_8ubyte_unaligned_inc (dbg, readp);
6419 : 4 : printf ("0x%" PRIx64 "\n", off);
6420 : : }
6421 : : }
6422 : 2 : printf ("\n");
6423 : : }
6424 : :
6425 : 1964 : Dwarf_Addr base = cu_base;
6426 : 1964 : bool start_of_list = true;
6427 : 1964 : while (readp < nexthdr)
6428 : : {
6429 : 133822 : uint8_t kind = *readp++;
6430 : 133822 : uint64_t op1, op2;
6431 : :
6432 : : /* Skip padding. */
6433 [ - + ]: 133822 : if (start_of_list && kind == DW_RLE_end_of_list)
6434 : 0 : continue;
6435 : :
6436 [ + + ]: 133822 : if (start_of_list)
6437 : : {
6438 : 27968 : base = cu_base;
6439 : 27968 : printf (" Offset: %" PRIx64 ", Index: %" PRIx64 "\n",
6440 : 27968 : (uint64_t) (readp - (unsigned char *) data->d_buf - 1),
6441 : 27968 : (uint64_t) (readp - offset_array_start - 1));
6442 : 27968 : start_of_list = false;
6443 : : }
6444 : :
6445 : 133822 : printf (" %s", dwarf_range_list_encoding_name (kind));
6446 [ + - - - : 133822 : switch (kind)
+ + - +
- ]
6447 : : {
6448 : 27968 : case DW_RLE_end_of_list:
6449 : 27968 : start_of_list = true;
6450 : 27968 : printf ("\n\n");
6451 : : break;
6452 : :
6453 : 0 : case DW_RLE_base_addressx:
6454 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
6455 : : {
6456 : 0 : invalid_range:
6457 : 0 : error (0, 0, _("invalid range list data"));
6458 : 0 : goto next_table;
6459 : : }
6460 : 0 : get_uleb128 (op1, readp, nexthdr);
6461 : 0 : printf (" %" PRIx64 "\n", op1);
6462 [ # # ]: 0 : if (! print_unresolved_addresses)
6463 : : {
6464 : 0 : Dwarf_Addr addr;
6465 [ # # ]: 0 : if (get_indexed_addr (cu, op1, &addr) != 0)
6466 : 0 : printf (" ???\n");
6467 : : else
6468 : : {
6469 : 0 : printf (" ");
6470 : 0 : print_dwarf_addr (dwflmod, address_size, addr, addr);
6471 : 0 : printf ("\n");
6472 : : }
6473 : : }
6474 : : break;
6475 : :
6476 : 0 : case DW_RLE_startx_endx:
6477 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
6478 : 0 : goto invalid_range;
6479 : 0 : get_uleb128 (op1, readp, nexthdr);
6480 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
6481 : 0 : goto invalid_range;
6482 : 0 : get_uleb128 (op2, readp, nexthdr);
6483 : 0 : printf (" %" PRIx64 ", %" PRIx64 "\n", op1, op2);
6484 [ # # ]: 0 : if (! print_unresolved_addresses)
6485 : : {
6486 : 0 : Dwarf_Addr addr1;
6487 : 0 : Dwarf_Addr addr2;
6488 [ # # ]: 0 : if (get_indexed_addr (cu, op1, &addr1) != 0
6489 [ # # ]: 0 : || get_indexed_addr (cu, op2, &addr2) != 0)
6490 : : {
6491 : 0 : printf (" ???..\n");
6492 : 0 : printf (" ???\n");
6493 : : }
6494 : : else
6495 : : {
6496 : 0 : printf (" ");
6497 : 0 : print_dwarf_addr (dwflmod, address_size, addr1, addr1);
6498 : 0 : printf ("..\n ");
6499 : 0 : print_dwarf_addr (dwflmod, address_size,
6500 : : addr2 - 1, addr2);
6501 : 0 : printf ("\n");
6502 : : }
6503 : : }
6504 : : break;
6505 : :
6506 : 0 : case DW_RLE_startx_length:
6507 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
6508 : 0 : goto invalid_range;
6509 : 0 : get_uleb128 (op1, readp, nexthdr);
6510 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
6511 : 0 : goto invalid_range;
6512 : 0 : get_uleb128 (op2, readp, nexthdr);
6513 : 0 : printf (" %" PRIx64 ", %" PRIx64 "\n", op1, op2);
6514 [ # # ]: 0 : if (! print_unresolved_addresses)
6515 : : {
6516 : 0 : Dwarf_Addr addr1;
6517 : 0 : Dwarf_Addr addr2;
6518 [ # # ]: 0 : if (get_indexed_addr (cu, op1, &addr1) != 0)
6519 : : {
6520 : 0 : printf (" ???..\n");
6521 : 0 : printf (" ???\n");
6522 : : }
6523 : : else
6524 : : {
6525 : 0 : addr2 = addr1 + op2;
6526 : 0 : printf (" ");
6527 : 0 : print_dwarf_addr (dwflmod, address_size, addr1, addr1);
6528 : 0 : printf ("..\n ");
6529 : 0 : print_dwarf_addr (dwflmod, address_size,
6530 : : addr2 - 1, addr2);
6531 : 0 : printf ("\n");
6532 : : }
6533 : : }
6534 : : break;
6535 : :
6536 : 96436 : case DW_RLE_offset_pair:
6537 [ - + ]: 96436 : if ((uint64_t) (nexthdr - readp) < 1)
6538 : 0 : goto invalid_range;
6539 : 96436 : get_uleb128 (op1, readp, nexthdr);
6540 [ - + ]: 96436 : if ((uint64_t) (nexthdr - readp) < 1)
6541 : 0 : goto invalid_range;
6542 : 96436 : get_uleb128 (op2, readp, nexthdr);
6543 : 96436 : printf (" %" PRIx64 ", %" PRIx64 "\n", op1, op2);
6544 [ + - ]: 96436 : if (! print_unresolved_addresses)
6545 : : {
6546 : 96436 : op1 += base;
6547 : 96436 : op2 += base;
6548 : 96436 : printf (" ");
6549 : 96436 : print_dwarf_addr (dwflmod, address_size, op1, op1);
6550 : 96436 : printf ("..\n ");
6551 : 96436 : print_dwarf_addr (dwflmod, address_size, op2 - 1, op2);
6552 : 96436 : printf ("\n");
6553 : : }
6554 : : break;
6555 : :
6556 : 8486 : case DW_RLE_base_address:
6557 [ - + ]: 8486 : if (address_size == 4)
6558 : : {
6559 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 4)
6560 : 0 : goto invalid_range;
6561 [ # # ]: 0 : op1 = read_4ubyte_unaligned_inc (dbg, readp);
6562 : : }
6563 : : else
6564 : : {
6565 [ - + ]: 8486 : if ((uint64_t) (nexthdr - readp) < 8)
6566 : 0 : goto invalid_range;
6567 [ - + ]: 8486 : op1 = read_8ubyte_unaligned_inc (dbg, readp);
6568 : : }
6569 : 8486 : base = op1;
6570 : 8486 : printf (" 0x%" PRIx64 "\n", base);
6571 [ - + ]: 8486 : if (! print_unresolved_addresses)
6572 : : {
6573 : 8486 : printf (" ");
6574 : 8486 : print_dwarf_addr (dwflmod, address_size, base, base);
6575 : 8486 : printf ("\n");
6576 : : }
6577 : : break;
6578 : :
6579 : 0 : case DW_RLE_start_end:
6580 [ # # ]: 0 : if (address_size == 4)
6581 : : {
6582 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 8)
6583 : 0 : goto invalid_range;
6584 [ # # ]: 0 : op1 = read_4ubyte_unaligned_inc (dbg, readp);
6585 [ # # ]: 0 : op2 = read_4ubyte_unaligned_inc (dbg, readp);
6586 : : }
6587 : : else
6588 : : {
6589 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 16)
6590 : 0 : goto invalid_range;
6591 [ # # ]: 0 : op1 = read_8ubyte_unaligned_inc (dbg, readp);
6592 [ # # ]: 0 : op2 = read_8ubyte_unaligned_inc (dbg, readp);
6593 : : }
6594 : 0 : printf (" 0x%" PRIx64 "..0x%" PRIx64 "\n", op1, op2);
6595 [ # # ]: 0 : if (! print_unresolved_addresses)
6596 : : {
6597 : 0 : printf (" ");
6598 : 0 : print_dwarf_addr (dwflmod, address_size, op1, op1);
6599 : 0 : printf ("..\n ");
6600 : 0 : print_dwarf_addr (dwflmod, address_size, op2 - 1, op2);
6601 : 0 : printf ("\n");
6602 : : }
6603 : : break;
6604 : :
6605 : 932 : case DW_RLE_start_length:
6606 [ - + ]: 932 : if (address_size == 4)
6607 : : {
6608 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 4)
6609 : 0 : goto invalid_range;
6610 [ # # ]: 0 : op1 = read_4ubyte_unaligned_inc (dbg, readp);
6611 : : }
6612 : : else
6613 : : {
6614 [ - + ]: 932 : if ((uint64_t) (nexthdr - readp) < 8)
6615 : 0 : goto invalid_range;
6616 [ - + ]: 932 : op1 = read_8ubyte_unaligned_inc (dbg, readp);
6617 : : }
6618 [ - + ]: 932 : if ((uint64_t) (nexthdr - readp) < 1)
6619 : 0 : goto invalid_range;
6620 : 932 : get_uleb128 (op2, readp, nexthdr);
6621 : 932 : printf (" 0x%" PRIx64 ", %" PRIx64 "\n", op1, op2);
6622 [ + + ]: 932 : if (! print_unresolved_addresses)
6623 : : {
6624 : 930 : op2 = op1 + op2;
6625 : 930 : printf (" ");
6626 : 930 : print_dwarf_addr (dwflmod, address_size, op1, op1);
6627 : 930 : printf ("..\n ");
6628 : 930 : print_dwarf_addr (dwflmod, address_size, op2 - 1, op2);
6629 [ + + ]: 136716 : printf ("\n");
6630 : : }
6631 : : break;
6632 : :
6633 : 0 : default:
6634 : 0 : goto invalid_range;
6635 : : }
6636 : : }
6637 : :
6638 : 1964 : next_table:
6639 [ - + ]: 1964 : if (readp != nexthdr)
6640 : : {
6641 : 0 : size_t padding = nexthdr - readp;
6642 : 0 : printf (_(" %zu padding bytes\n\n"), padding);
6643 : 0 : readp = nexthdr;
6644 : : }
6645 : : }
6646 : : }
6647 : :
6648 : : /* Print content of DWARF .debug_ranges section. */
6649 : : static void
6650 : 32 : print_debug_ranges_section (Dwfl_Module *dwflmod,
6651 : : Ebl *ebl, GElf_Ehdr *ehdr,
6652 : : Elf_Scn *scn, GElf_Shdr *shdr,
6653 : : Dwarf *dbg)
6654 : : {
6655 : 32 : Elf_Data *data = get_debug_elf_data (dbg, ebl, IDX_debug_ranges, scn);
6656 [ - + ]: 32 : if (data == NULL)
6657 : 0 : return;
6658 : :
6659 : 32 : printf (_("\
6660 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
6661 : : elf_ndxscn (scn), section_name (ebl, shdr),
6662 : 32 : (uint64_t) shdr->sh_offset);
6663 : :
6664 : 32 : sort_listptr (&known_rangelistptr, "rangelistptr");
6665 : 32 : size_t listptr_idx = 0;
6666 : :
6667 [ + + ]: 32 : uint_fast8_t address_size = ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 4 : 8;
6668 : :
6669 : 32 : bool first = true;
6670 : 32 : Dwarf_Addr base = 0;
6671 : 32 : unsigned char *const endp = (unsigned char *) data->d_buf + data->d_size;
6672 : 32 : unsigned char *readp = data->d_buf;
6673 : 32 : Dwarf_CU *last_cu = NULL;
6674 [ + + ]: 184 : while (readp < endp)
6675 : : {
6676 : 152 : ptrdiff_t offset = readp - (unsigned char *) data->d_buf;
6677 : 152 : Dwarf_CU *cu = last_cu;
6678 : :
6679 [ + + - + ]: 152 : if (first && skip_listptr_hole (&known_rangelistptr, &listptr_idx,
6680 : : &address_size, NULL, &base, &cu,
6681 : : offset, &readp, endp, NULL))
6682 : 0 : continue;
6683 : :
6684 [ + + ]: 152 : if (last_cu != cu)
6685 : : {
6686 : 58 : Dwarf_Die cudie;
6687 [ - + ]: 58 : if (dwarf_cu_die (cu, &cudie,
6688 : : NULL, NULL, NULL, NULL,
6689 : : NULL, NULL) == NULL)
6690 : 0 : printf (_("\n Unknown CU base: "));
6691 : : else
6692 : 58 : printf (_("\n CU [%6" PRIx64 "] base: "),
6693 : : dwarf_dieoffset (&cudie));
6694 : 58 : print_dwarf_addr (dwflmod, address_size, base, base);
6695 : 58 : printf ("\n");
6696 : : }
6697 : 152 : last_cu = cu;
6698 : :
6699 [ - + ]: 152 : if (unlikely (data->d_size - offset < (size_t) address_size * 2))
6700 : : {
6701 : 0 : printf (_(" [%6tx] <INVALID DATA>\n"), offset);
6702 : 0 : break;
6703 : : }
6704 : :
6705 : 152 : Dwarf_Addr begin;
6706 : 152 : Dwarf_Addr end;
6707 [ + + ]: 152 : if (address_size == 8)
6708 : : {
6709 [ - + ]: 120 : begin = read_8ubyte_unaligned_inc (dbg, readp);
6710 [ - + ]: 120 : end = read_8ubyte_unaligned_inc (dbg, readp);
6711 : : }
6712 : : else
6713 : : {
6714 [ - + ]: 32 : begin = read_4ubyte_unaligned_inc (dbg, readp);
6715 [ - + ]: 32 : end = read_4ubyte_unaligned_inc (dbg, readp);
6716 [ + - ]: 32 : if (begin == (Dwarf_Addr) (uint32_t) -1)
6717 : : begin = (Dwarf_Addr) -1l;
6718 : : }
6719 : :
6720 [ - + ]: 152 : if (begin == (Dwarf_Addr) -1l) /* Base address entry. */
6721 : : {
6722 [ # # ]: 0 : if (first)
6723 : 0 : printf (" [%6tx] ", offset);
6724 : : else
6725 : 0 : printf (" ");
6726 : 0 : puts (_("base address"));
6727 : 0 : printf (" ");
6728 : 0 : print_dwarf_addr (dwflmod, address_size, end, end);
6729 : 0 : printf ("\n");
6730 : 0 : base = end;
6731 : 0 : first = false;
6732 : : }
6733 [ + + ]: 152 : else if (begin == 0 && end == 0) /* End of list entry. */
6734 : : {
6735 [ - + ]: 60 : if (first)
6736 : 0 : printf (_(" [%6tx] empty list\n"), offset);
6737 : : first = true;
6738 : : }
6739 : : else
6740 : : {
6741 : : /* We have an address range entry. */
6742 [ + + ]: 92 : if (first) /* First address range entry in a list. */
6743 : 60 : printf (" [%6tx] ", offset);
6744 : : else
6745 : 32 : printf (" ");
6746 : :
6747 : 92 : printf ("range %" PRIx64 ", %" PRIx64 "\n", begin, end);
6748 [ + + ]: 92 : if (! print_unresolved_addresses)
6749 : : {
6750 : 72 : printf (" ");
6751 : 72 : print_dwarf_addr (dwflmod, address_size, base + begin,
6752 : : base + begin);
6753 : 72 : printf ("..\n ");
6754 : 72 : print_dwarf_addr (dwflmod, address_size,
6755 : : base + end - 1, base + end);
6756 : 224 : printf ("\n");
6757 : : }
6758 : :
6759 : : first = false;
6760 : : }
6761 : : }
6762 : : }
6763 : :
6764 : : #define REGNAMESZ 16
6765 : : static const char *
6766 : 34462 : register_info (Ebl *ebl, unsigned int regno, const Ebl_Register_Location *loc,
6767 : : char name[REGNAMESZ], int *bits, int *type)
6768 : : {
6769 : 34462 : const char *set;
6770 : 34462 : const char *pfx;
6771 : 34462 : int ignore;
6772 [ + + + + ]: 99994 : ssize_t n = ebl_register_info (ebl, regno, name, REGNAMESZ, &pfx, &set,
6773 : : bits ?: &ignore, type ?: &ignore);
6774 [ - + ]: 34462 : if (n <= 0)
6775 : : {
6776 [ # # ]: 0 : if (loc != NULL)
6777 : 0 : snprintf (name, REGNAMESZ, "reg%u", loc->regno);
6778 : : else
6779 : 0 : snprintf (name, REGNAMESZ, "??? 0x%x", regno);
6780 [ # # ]: 0 : if (bits != NULL)
6781 [ # # ]: 0 : *bits = loc != NULL ? loc->bits : 0;
6782 [ # # ]: 0 : if (type != NULL)
6783 : 0 : *type = DW_ATE_unsigned;
6784 : 0 : set = "??? unrecognized";
6785 : : }
6786 : : else
6787 : : {
6788 [ + + - + ]: 34462 : if (bits != NULL && *bits <= 0)
6789 [ # # ]: 0 : *bits = loc != NULL ? loc->bits : 0;
6790 [ + + - + ]: 34462 : if (type != NULL && *type == DW_ATE_void)
6791 : 0 : *type = DW_ATE_unsigned;
6792 : :
6793 : : }
6794 : 34462 : return set;
6795 : : }
6796 : :
6797 : : static const unsigned char *
6798 : 280 : read_encoded (unsigned int encoding, const unsigned char *readp,
6799 : : const unsigned char *const endp, uint64_t *res, Dwarf *dbg)
6800 : : {
6801 [ - + ]: 280 : if ((encoding & 0xf) == DW_EH_PE_absptr)
6802 : 0 : encoding = gelf_getclass (dbg->elf) == ELFCLASS32
6803 [ # # ]: 0 : ? DW_EH_PE_udata4 : DW_EH_PE_udata8;
6804 : :
6805 [ - - - + : 280 : switch (encoding & 0xf)
- - + -
- ]
6806 : : {
6807 : 0 : case DW_EH_PE_uleb128:
6808 [ # # ]: 0 : if (readp >= endp)
6809 : 0 : goto invalid;
6810 : 0 : get_uleb128 (*res, readp, endp);
6811 : 0 : break;
6812 : 0 : case DW_EH_PE_sleb128:
6813 [ # # ]: 0 : if (readp >= endp)
6814 : 0 : goto invalid;
6815 : 0 : get_sleb128 (*res, readp, endp);
6816 : 0 : break;
6817 : 0 : case DW_EH_PE_udata2:
6818 [ # # ]: 0 : if (readp + 2 > endp)
6819 : 0 : goto invalid;
6820 [ # # ]: 0 : *res = read_2ubyte_unaligned_inc (dbg, readp);
6821 : 0 : break;
6822 : 140 : case DW_EH_PE_udata4:
6823 [ - + ]: 140 : if (readp + 4 > endp)
6824 : 0 : goto invalid;
6825 [ - + ]: 140 : *res = read_4ubyte_unaligned_inc (dbg, readp);
6826 : 140 : break;
6827 : 0 : case DW_EH_PE_udata8:
6828 [ # # ]: 0 : if (readp + 8 > endp)
6829 : 0 : goto invalid;
6830 [ # # ]: 0 : *res = read_8ubyte_unaligned_inc (dbg, readp);
6831 : 0 : break;
6832 : 0 : case DW_EH_PE_sdata2:
6833 [ # # ]: 0 : if (readp + 2 > endp)
6834 : 0 : goto invalid;
6835 [ # # ]: 0 : *res = read_2sbyte_unaligned_inc (dbg, readp);
6836 : 0 : break;
6837 : 140 : case DW_EH_PE_sdata4:
6838 [ - + ]: 140 : if (readp + 4 > endp)
6839 : 0 : goto invalid;
6840 [ - + ]: 140 : *res = read_4sbyte_unaligned_inc (dbg, readp);
6841 : 140 : break;
6842 : 0 : case DW_EH_PE_sdata8:
6843 [ # # ]: 0 : if (readp + 8 > endp)
6844 : 0 : goto invalid;
6845 [ # # ]: 0 : *res = read_8sbyte_unaligned_inc (dbg, readp);
6846 : 0 : break;
6847 : : default:
6848 : 0 : invalid:
6849 : 0 : error (1, 0,
6850 : 0 : _("invalid encoding"));
6851 : : }
6852 : :
6853 : 280 : return readp;
6854 : : }
6855 : :
6856 : : static const char *
6857 : 32766 : regname (Ebl *ebl, unsigned int regno, char *regnamebuf)
6858 : : {
6859 : 32766 : register_info (ebl, regno, NULL, regnamebuf, NULL, NULL);
6860 : :
6861 : 32766 : return regnamebuf;
6862 : : }
6863 : :
6864 : : static void
6865 : 22842 : print_cfa_program (const unsigned char *readp, const unsigned char *const endp,
6866 : : Dwarf_Word vma_base, unsigned int code_align,
6867 : : int data_align,
6868 : : unsigned int version, unsigned int ptr_size,
6869 : : unsigned int encoding,
6870 : : Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr, Dwarf *dbg)
6871 : : {
6872 : 22842 : char regnamebuf[REGNAMESZ];
6873 : :
6874 : 22842 : puts ("\n Program:");
6875 : 22842 : Dwarf_Word pc = vma_base;
6876 : 22842 : while (readp < endp)
6877 : : {
6878 : 313480 : unsigned int opcode = *readp++;
6879 : :
6880 [ + + ]: 313480 : if (opcode < DW_CFA_advance_loc)
6881 : : /* Extended opcode. */
6882 [ + - + + : 188614 : switch (opcode)
+ + - + -
- + + + +
+ + - + -
- - - - -
+ - - ]
6883 : : {
6884 : 64988 : uint64_t op1;
6885 : 64988 : int64_t sop1;
6886 : 64988 : uint64_t op2;
6887 : 64988 : int64_t sop2;
6888 : :
6889 : 64988 : case DW_CFA_nop:
6890 : 64988 : puts (" nop");
6891 : 64988 : break;
6892 : 0 : case DW_CFA_set_loc:
6893 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
6894 : 0 : goto invalid;
6895 : 0 : readp = read_encoded (encoding, readp, endp, &op1, dbg);
6896 : 188614 : printf (" set_loc %#" PRIx64 " to %#" PRIx64 "\n",
6897 : 0 : op1, pc = vma_base + op1);
6898 : : break;
6899 : 7096 : case DW_CFA_advance_loc1:
6900 [ - + ]: 7096 : if ((uint64_t) (endp - readp) < 1)
6901 : 0 : goto invalid;
6902 : 14192 : printf (" advance_loc1 %u to %#" PRIx64 "\n",
6903 : 7096 : *readp, pc += *readp * code_align);
6904 : 7096 : ++readp;
6905 : 7096 : break;
6906 : 2442 : case DW_CFA_advance_loc2:
6907 [ - + ]: 2442 : if ((uint64_t) (endp - readp) < 2)
6908 : 0 : goto invalid;
6909 [ - + ]: 2442 : op1 = read_2ubyte_unaligned_inc (dbg, readp);
6910 : 191056 : printf (" advance_loc2 %" PRIu64 " to %#" PRIx64 "\n",
6911 : 2442 : op1, pc += op1 * code_align);
6912 : : break;
6913 : 36 : case DW_CFA_advance_loc4:
6914 [ - + ]: 36 : if ((uint64_t) (endp - readp) < 4)
6915 : 0 : goto invalid;
6916 [ + - ]: 36 : op1 = read_4ubyte_unaligned_inc (dbg, readp);
6917 : 188650 : printf (" advance_loc4 %" PRIu64 " to %#" PRIx64 "\n",
6918 : 36 : op1, pc += op1 * code_align);
6919 : : break;
6920 : 8 : case DW_CFA_offset_extended:
6921 [ - + ]: 8 : if ((uint64_t) (endp - readp) < 1)
6922 : 0 : goto invalid;
6923 : 8 : get_uleb128 (op1, readp, endp);
6924 [ - + ]: 8 : if ((uint64_t) (endp - readp) < 1)
6925 : 0 : goto invalid;
6926 : 8 : get_uleb128 (op2, readp, endp);
6927 : 8 : printf (" offset_extended r%" PRIu64 " (%s) at cfa%+" PRId64
6928 : : "\n",
6929 : : op1, regname (ebl, op1, regnamebuf), op2 * data_align);
6930 : : break;
6931 : 0 : case DW_CFA_restore_extended:
6932 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
6933 : 0 : goto invalid;
6934 : 0 : get_uleb128 (op1, readp, endp);
6935 : 0 : printf (" restore_extended r%" PRIu64 " (%s)\n",
6936 : : op1, regname (ebl, op1, regnamebuf));
6937 : : break;
6938 : 110 : case DW_CFA_undefined:
6939 [ - + ]: 110 : if ((uint64_t) (endp - readp) < 1)
6940 : 0 : goto invalid;
6941 : 110 : get_uleb128 (op1, readp, endp);
6942 : 110 : printf (" undefined r%" PRIu64 " (%s)\n", op1,
6943 : : regname (ebl, op1, regnamebuf));
6944 : : break;
6945 : 0 : case DW_CFA_same_value:
6946 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
6947 : 0 : goto invalid;
6948 : 0 : get_uleb128 (op1, readp, endp);
6949 : 0 : printf (" same_value r%" PRIu64 " (%s)\n", op1,
6950 : : regname (ebl, op1, regnamebuf));
6951 : : break;
6952 : 0 : case DW_CFA_register:
6953 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
6954 : 0 : goto invalid;
6955 : 0 : get_uleb128 (op1, readp, endp);
6956 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
6957 : 0 : goto invalid;
6958 : 0 : get_uleb128 (op2, readp, endp);
6959 : 0 : printf (" register r%" PRIu64 " (%s) in r%" PRIu64 " (%s)\n",
6960 : : op1, regname (ebl, op1, regnamebuf), op2,
6961 : : regname (ebl, op2, regnamebuf));
6962 : : break;
6963 : 11548 : case DW_CFA_remember_state:
6964 : 11548 : puts (" remember_state");
6965 : 11548 : break;
6966 : 11548 : case DW_CFA_restore_state:
6967 : 11548 : puts (" restore_state");
6968 : 11548 : break;
6969 : 416 : case DW_CFA_def_cfa:
6970 [ - + ]: 416 : if ((uint64_t) (endp - readp) < 1)
6971 : 0 : goto invalid;
6972 : 416 : get_uleb128 (op1, readp, endp);
6973 [ - + ]: 416 : if ((uint64_t) (endp - readp) < 1)
6974 : 0 : goto invalid;
6975 : 416 : get_uleb128 (op2, readp, endp);
6976 : 416 : printf (" def_cfa r%" PRIu64 " (%s) at offset %" PRIu64 "\n",
6977 : : op1, regname (ebl, op1, regnamebuf), op2);
6978 : : break;
6979 : 126 : case DW_CFA_def_cfa_register:
6980 [ - + ]: 126 : if ((uint64_t) (endp - readp) < 1)
6981 : 0 : goto invalid;
6982 : 126 : get_uleb128 (op1, readp, endp);
6983 : 126 : printf (" def_cfa_register r%" PRIu64 " (%s)\n",
6984 : : op1, regname (ebl, op1, regnamebuf));
6985 : : break;
6986 : 90146 : case DW_CFA_def_cfa_offset:
6987 [ - + ]: 90146 : if ((uint64_t) (endp - readp) < 1)
6988 : 0 : goto invalid;
6989 : 90146 : get_uleb128 (op1, readp, endp);
6990 : 90146 : printf (" def_cfa_offset %" PRIu64 "\n", op1);
6991 : : break;
6992 : 118 : case DW_CFA_def_cfa_expression:
6993 [ - + ]: 118 : if ((uint64_t) (endp - readp) < 1)
6994 : 0 : goto invalid;
6995 : 118 : get_uleb128 (op1, readp, endp); /* Length of DW_FORM_block. */
6996 : 118 : printf (" def_cfa_expression %" PRIu64 "\n", op1);
6997 [ - + ]: 118 : if ((uint64_t) (endp - readp) < op1)
6998 : : {
6999 : 0 : invalid:
7000 : 0 : fputs (_(" <INVALID DATA>\n"), stdout);
7001 : 0 : return;
7002 : : }
7003 : 118 : print_ops (dwflmod, dbg, 10, 10, version, ptr_size, 0, NULL,
7004 : : op1, readp);
7005 : 118 : readp += op1;
7006 : 118 : break;
7007 : 0 : case DW_CFA_expression:
7008 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
7009 : 0 : goto invalid;
7010 : 0 : get_uleb128 (op1, readp, endp);
7011 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
7012 : 0 : goto invalid;
7013 : 0 : get_uleb128 (op2, readp, endp); /* Length of DW_FORM_block. */
7014 : 0 : printf (" expression r%" PRIu64 " (%s) \n",
7015 : : op1, regname (ebl, op1, regnamebuf));
7016 [ # # ]: 0 : if ((uint64_t) (endp - readp) < op2)
7017 : 0 : goto invalid;
7018 : 0 : print_ops (dwflmod, dbg, 10, 10, version, ptr_size, 0, NULL,
7019 : : op2, readp);
7020 : 0 : readp += op2;
7021 : 0 : break;
7022 : 24 : case DW_CFA_offset_extended_sf:
7023 [ - + ]: 24 : if ((uint64_t) (endp - readp) < 1)
7024 : 0 : goto invalid;
7025 : 24 : get_uleb128 (op1, readp, endp);
7026 [ - + ]: 24 : if ((uint64_t) (endp - readp) < 1)
7027 : 0 : goto invalid;
7028 : 24 : get_sleb128 (sop2, readp, endp);
7029 : 24 : printf (" offset_extended_sf r%" PRIu64 " (%s) at cfa%+"
7030 : : PRId64 "\n",
7031 : : op1, regname (ebl, op1, regnamebuf), sop2 * data_align);
7032 : : break;
7033 : 0 : case DW_CFA_def_cfa_sf:
7034 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
7035 : 0 : goto invalid;
7036 : 0 : get_uleb128 (op1, readp, endp);
7037 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
7038 : 0 : goto invalid;
7039 : 0 : get_sleb128 (sop2, readp, endp);
7040 : 0 : printf (" def_cfa_sf r%" PRIu64 " (%s) at offset %" PRId64 "\n",
7041 : : op1, regname (ebl, op1, regnamebuf), sop2 * data_align);
7042 : : break;
7043 : 0 : case DW_CFA_def_cfa_offset_sf:
7044 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
7045 : 0 : goto invalid;
7046 : 0 : get_sleb128 (sop1, readp, endp);
7047 : 0 : printf (" def_cfa_offset_sf %" PRId64 "\n", sop1 * data_align);
7048 : : break;
7049 : 0 : case DW_CFA_val_offset:
7050 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
7051 : 0 : goto invalid;
7052 : 0 : get_uleb128 (op1, readp, endp);
7053 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
7054 : 0 : goto invalid;
7055 : 0 : get_uleb128 (op2, readp, endp);
7056 : 0 : printf (" val_offset %" PRIu64 " at offset %" PRIu64 "\n",
7057 : : op1, op2 * data_align);
7058 : : break;
7059 : 0 : case DW_CFA_val_offset_sf:
7060 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
7061 : 0 : goto invalid;
7062 : 0 : get_uleb128 (op1, readp, endp);
7063 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
7064 : 0 : goto invalid;
7065 : 0 : get_sleb128 (sop2, readp, endp);
7066 : 0 : printf (" val_offset_sf %" PRIu64 " at offset %" PRId64 "\n",
7067 : : op1, sop2 * data_align);
7068 : : break;
7069 : 0 : case DW_CFA_val_expression:
7070 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
7071 : 0 : goto invalid;
7072 : 0 : get_uleb128 (op1, readp, endp);
7073 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
7074 : 0 : goto invalid;
7075 : 0 : get_uleb128 (op2, readp, endp); /* Length of DW_FORM_block. */
7076 : 0 : printf (" val_expression r%" PRIu64 " (%s)\n",
7077 : : op1, regname (ebl, op1, regnamebuf));
7078 [ # # ]: 0 : if ((uint64_t) (endp - readp) < op2)
7079 : 0 : goto invalid;
7080 : 0 : print_ops (dwflmod, dbg, 10, 10, version, ptr_size, 0,
7081 : : NULL, op2, readp);
7082 : 0 : readp += op2;
7083 : 0 : break;
7084 : 0 : case DW_CFA_MIPS_advance_loc8:
7085 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 8)
7086 : 0 : goto invalid;
7087 [ # # ]: 0 : op1 = read_8ubyte_unaligned_inc (dbg, readp);
7088 : 188614 : printf (" MIPS_advance_loc8 %" PRIu64 " to %#" PRIx64 "\n",
7089 : 0 : op1, pc += op1 * code_align);
7090 : : break;
7091 : 8 : case DW_CFA_GNU_window_save: /* DW_CFA_AARCH64_negate_ra_state */
7092 [ + - ]: 8 : if (ehdr->e_machine == EM_AARCH64)
7093 : 8 : puts (" AARCH64_negate_ra_state");
7094 : : else
7095 : 0 : puts (" GNU_window_save");
7096 : : break;
7097 : 0 : case DW_CFA_GNU_args_size:
7098 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
7099 : 0 : goto invalid;
7100 : 0 : get_uleb128 (op1, readp, endp);
7101 : 0 : printf (" args_size %" PRIu64 "\n", op1);
7102 : : break;
7103 : : default:
7104 : 188614 : printf (" ??? (%u)\n", opcode);
7105 : : break;
7106 : : }
7107 [ + + ]: 124866 : else if (opcode < DW_CFA_offset)
7108 : 429106 : printf (" advance_loc %u to %#" PRIx64 "\n",
7109 : 92784 : opcode & 0x3f, pc += (opcode & 0x3f) * code_align);
7110 [ + + ]: 32082 : else if (opcode < DW_CFA_restore)
7111 : : {
7112 : 28756 : uint64_t offset;
7113 [ - + ]: 28756 : if ((uint64_t) (endp - readp) < 1)
7114 : 0 : goto invalid;
7115 : 28756 : get_uleb128 (offset, readp, endp);
7116 : 28756 : printf (" offset r%u (%s) at cfa%+" PRId64 "\n",
7117 : : opcode & 0x3f, regname (ebl, opcode & 0x3f, regnamebuf),
7118 : : offset * data_align);
7119 : : }
7120 : : else
7121 [ + + ]: 339648 : printf (" restore r%u (%s)\n",
7122 : : opcode & 0x3f, regname (ebl, opcode & 0x3f, regnamebuf));
7123 : : }
7124 : : }
7125 : :
7126 : :
7127 : : static unsigned int
7128 : 22532 : encoded_ptr_size (int encoding, unsigned int ptr_size)
7129 : : {
7130 [ - + - + ]: 22532 : switch (encoding & 7)
7131 : : {
7132 : : case DW_EH_PE_udata4:
7133 : : return 4;
7134 : 0 : case DW_EH_PE_udata8:
7135 : 0 : return 8;
7136 : : case 0:
7137 : : return ptr_size;
7138 : : }
7139 : :
7140 : 0 : fprintf (stderr, "Unsupported pointer encoding: %#x, "
7141 : : "assuming pointer size of %d.\n", encoding, ptr_size);
7142 : 0 : return ptr_size;
7143 : : }
7144 : :
7145 : :
7146 : : static unsigned int
7147 : 642 : print_encoding (unsigned int val)
7148 : : {
7149 [ - - - + : 642 : switch (val & 0xf)
- - - + -
- ]
7150 : : {
7151 : 0 : case DW_EH_PE_absptr:
7152 : 0 : fputs ("absptr", stdout);
7153 : 0 : break;
7154 : 0 : case DW_EH_PE_uleb128:
7155 : 0 : fputs ("uleb128", stdout);
7156 : 0 : break;
7157 : 0 : case DW_EH_PE_udata2:
7158 : 0 : fputs ("udata2", stdout);
7159 : 0 : break;
7160 : 140 : case DW_EH_PE_udata4:
7161 : 140 : fputs ("udata4", stdout);
7162 : 140 : break;
7163 : 0 : case DW_EH_PE_udata8:
7164 : 0 : fputs ("udata8", stdout);
7165 : 0 : break;
7166 : 0 : case DW_EH_PE_sleb128:
7167 : 0 : fputs ("sleb128", stdout);
7168 : 0 : break;
7169 : 0 : case DW_EH_PE_sdata2:
7170 : 0 : fputs ("sdata2", stdout);
7171 : 0 : break;
7172 : 502 : case DW_EH_PE_sdata4:
7173 : 502 : fputs ("sdata4", stdout);
7174 : 502 : break;
7175 : 0 : case DW_EH_PE_sdata8:
7176 : 0 : fputs ("sdata8", stdout);
7177 : 0 : break;
7178 : : default:
7179 : : /* We did not use any of the bits after all. */
7180 : : return val;
7181 : : }
7182 : :
7183 : 642 : return val & ~0xf;
7184 : : }
7185 : :
7186 : :
7187 : : static unsigned int
7188 : 502 : print_relinfo (unsigned int val)
7189 : : {
7190 [ + - + - : 502 : switch (val & 0x70)
- - ]
7191 : : {
7192 : 362 : case DW_EH_PE_pcrel:
7193 : 362 : fputs ("pcrel", stdout);
7194 : 362 : break;
7195 : 0 : case DW_EH_PE_textrel:
7196 : 0 : fputs ("textrel", stdout);
7197 : 0 : break;
7198 : 140 : case DW_EH_PE_datarel:
7199 : 140 : fputs ("datarel", stdout);
7200 : 140 : break;
7201 : 0 : case DW_EH_PE_funcrel:
7202 : 0 : fputs ("funcrel", stdout);
7203 : 0 : break;
7204 : 0 : case DW_EH_PE_aligned:
7205 : 0 : fputs ("aligned", stdout);
7206 : 0 : break;
7207 : : default:
7208 : : return val;
7209 : : }
7210 : :
7211 : 502 : return val & ~0x70;
7212 : : }
7213 : :
7214 : :
7215 : : static void
7216 : 642 : print_encoding_base (const char *pfx, unsigned int fde_encoding)
7217 : : {
7218 : 642 : printf ("(%s", pfx);
7219 : :
7220 [ - + ]: 642 : if (fde_encoding == DW_EH_PE_omit)
7221 : 0 : puts ("omit)");
7222 : : else
7223 : : {
7224 : 642 : unsigned int w = fde_encoding;
7225 : :
7226 : 642 : w = print_encoding (w);
7227 : :
7228 [ + + ]: 642 : if (w & 0x70)
7229 : : {
7230 [ + - ]: 502 : if (w != fde_encoding)
7231 : 502 : fputc (' ', stdout);
7232 : :
7233 : 502 : w = print_relinfo (w);
7234 : : }
7235 : :
7236 [ - + ]: 642 : if (w != 0)
7237 [ # # ]: 0 : printf ("%s%x", w != fde_encoding ? " " : "", w);
7238 : :
7239 : 642 : puts (")");
7240 : : }
7241 : 642 : }
7242 : :
7243 : :
7244 : : static void
7245 : 196 : print_debug_frame_section (Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr,
7246 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
7247 : : {
7248 : 196 : size_t shstrndx;
7249 : : /* We know this call will succeed since it did in the caller. */
7250 : 196 : (void) elf_getshdrstrndx (ebl->elf, &shstrndx);
7251 : 196 : const char *scnname = section_name (ebl, shdr);
7252 : :
7253 : : /* Needed if we find PC-relative addresses. */
7254 : 196 : GElf_Addr bias;
7255 [ - + ]: 196 : if (dwfl_module_getelf (dwflmod, &bias) == NULL)
7256 : : {
7257 : 0 : error (0, 0, _("cannot get ELF: %s"), dwfl_errmsg (-1));
7258 : 0 : return;
7259 : : }
7260 : :
7261 : 196 : bool is_eh_frame = strcmp (scnname, ".eh_frame") == 0;
7262 : 196 : Elf_Data *data;
7263 [ + + ]: 196 : if (is_eh_frame)
7264 : : {
7265 : 144 : data = elf_rawdata (scn, NULL);
7266 [ - + ]: 144 : if (data == NULL)
7267 : : {
7268 : 0 : error (0, 0, _("cannot get %s content: %s"),
7269 : : scnname, elf_errmsg (-1));
7270 : 0 : return;
7271 : : }
7272 : : }
7273 : : else
7274 : : {
7275 : 52 : data = get_debug_elf_data (dbg, ebl, IDX_debug_frame, scn);
7276 [ + - ]: 52 : if (data == NULL)
7277 : : return;
7278 : : }
7279 : :
7280 [ + + ]: 196 : if (is_eh_frame)
7281 : 144 : printf (_("\
7282 : : \nCall frame information section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
7283 : 144 : elf_ndxscn (scn), scnname, (uint64_t) shdr->sh_offset);
7284 : : else
7285 : 52 : printf (_("\
7286 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
7287 : 52 : elf_ndxscn (scn), scnname, (uint64_t) shdr->sh_offset);
7288 : :
7289 : 196 : struct cieinfo
7290 : : {
7291 : : ptrdiff_t cie_offset;
7292 : : const char *augmentation;
7293 : : unsigned int code_alignment_factor;
7294 : : unsigned int data_alignment_factor;
7295 : : uint8_t address_size;
7296 : : uint8_t fde_encoding;
7297 : : uint8_t lsda_encoding;
7298 : : struct cieinfo *next;
7299 : 196 : } *cies = NULL;
7300 : :
7301 : 196 : const unsigned char *readp = data->d_buf;
7302 : 196 : const unsigned char *const dataend = ((unsigned char *) data->d_buf
7303 : 196 : + data->d_size);
7304 [ + + ]: 23178 : while (readp < dataend)
7305 : : {
7306 [ - + ]: 22982 : if (unlikely (readp + 4 > dataend))
7307 : : {
7308 : 0 : invalid_data:
7309 : 0 : error (0, 0, _("invalid data in section [%zu] '%s'"),
7310 : : elf_ndxscn (scn), scnname);
7311 : 0 : return;
7312 : : }
7313 : :
7314 : : /* At the beginning there must be a CIE. There can be multiple,
7315 : : hence we test tis in a loop. */
7316 : 22982 : ptrdiff_t offset = readp - (unsigned char *) data->d_buf;
7317 : :
7318 [ + + ]: 22982 : Dwarf_Word unit_length = read_4ubyte_unaligned_inc (dbg, readp);
7319 : 22982 : unsigned int length = 4;
7320 [ - + ]: 22982 : if (unlikely (unit_length == 0xffffffff))
7321 : : {
7322 [ # # ]: 0 : if (unlikely (readp + 8 > dataend))
7323 : 0 : goto invalid_data;
7324 : :
7325 [ # # ]: 0 : unit_length = read_8ubyte_unaligned_inc (dbg, readp);
7326 : 0 : length = 8;
7327 : : }
7328 : :
7329 [ + + ]: 22982 : if (unlikely (unit_length == 0))
7330 : : {
7331 : 140 : printf (_("\n [%6tx] Zero terminator\n"), offset);
7332 : 140 : continue;
7333 : : }
7334 : :
7335 : 22842 : Dwarf_Word maxsize = dataend - readp;
7336 [ - + ]: 22842 : if (unlikely (unit_length > maxsize))
7337 : 0 : goto invalid_data;
7338 : :
7339 [ + + ]: 22842 : unsigned int ptr_size = ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 4 : 8;
7340 : :
7341 : 22842 : ptrdiff_t start = readp - (unsigned char *) data->d_buf;
7342 : 22842 : const unsigned char *const cieend = readp + unit_length;
7343 [ - + ]: 22842 : if (unlikely (cieend > dataend))
7344 : 0 : goto invalid_data;
7345 : :
7346 : 22842 : Dwarf_Off cie_id;
7347 [ + - ]: 22842 : if (length == 4)
7348 : : {
7349 [ - + ]: 22842 : if (unlikely (cieend - readp < 4))
7350 : 0 : goto invalid_data;
7351 [ + + ]: 22842 : cie_id = read_4ubyte_unaligned_inc (dbg, readp);
7352 [ + + ]: 22842 : if (!is_eh_frame && cie_id == DW_CIE_ID_32)
7353 : : cie_id = DW_CIE_ID_64;
7354 : : }
7355 : : else
7356 : : {
7357 [ # # ]: 0 : if (unlikely (cieend - readp < 8))
7358 : 0 : goto invalid_data;
7359 [ # # ]: 0 : cie_id = read_8ubyte_unaligned_inc (dbg, readp);
7360 : : }
7361 : :
7362 : 45600 : uint_fast8_t version = 2;
7363 : 22758 : unsigned int code_alignment_factor;
7364 : 22758 : int data_alignment_factor;
7365 : 45600 : unsigned int fde_encoding = 0;
7366 : 45600 : unsigned int lsda_encoding = 0;
7367 : 45600 : Dwarf_Word initial_location = 0;
7368 : 45600 : Dwarf_Word vma_base = 0;
7369 : :
7370 [ + + + + ]: 22934 : if (cie_id == (is_eh_frame ? 0 : DW_CIE_ID_64))
7371 : : {
7372 [ - + ]: 310 : if (unlikely (cieend - readp < 2))
7373 : 0 : goto invalid_data;
7374 : 310 : version = *readp++;
7375 : 310 : const char *const augmentation = (const char *) readp;
7376 : 310 : readp = memchr (readp, '\0', cieend - readp);
7377 [ - + ]: 310 : if (unlikely (readp == NULL))
7378 : 0 : goto invalid_data;
7379 : 310 : ++readp;
7380 : :
7381 : 310 : uint_fast8_t segment_size = 0;
7382 [ + + ]: 310 : if (version >= 4)
7383 : : {
7384 [ - + ]: 8 : if (cieend - readp < 5)
7385 : 0 : goto invalid_data;
7386 : 8 : ptr_size = *readp++;
7387 : 8 : segment_size = *readp++;
7388 : : }
7389 : :
7390 [ - + ]: 310 : if (cieend - readp < 1)
7391 : 0 : goto invalid_data;
7392 : 310 : get_uleb128 (code_alignment_factor, readp, cieend);
7393 [ - + ]: 310 : if (cieend - readp < 1)
7394 : 0 : goto invalid_data;
7395 : 310 : get_sleb128 (data_alignment_factor, readp, cieend);
7396 : :
7397 : : /* In some variant for unwind data there is another field. */
7398 [ - + ]: 310 : if (strcmp (augmentation, "eh") == 0)
7399 [ # # ]: 0 : readp += ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 4 : 8;
7400 : :
7401 : 310 : unsigned int return_address_register;
7402 [ - + ]: 310 : if (cieend - readp < 1)
7403 : 0 : goto invalid_data;
7404 [ + + ]: 310 : if (unlikely (version == 1))
7405 : 286 : return_address_register = *readp++;
7406 : : else
7407 : 24 : get_uleb128 (return_address_register, readp, cieend);
7408 : :
7409 : 310 : printf ("\n [%6tx] CIE length=%" PRIu64 "\n"
7410 : : " CIE_id: %" PRIu64 "\n"
7411 : : " version: %u\n"
7412 : : " augmentation: \"%s\"\n",
7413 : : offset, (uint64_t) unit_length, (uint64_t) cie_id,
7414 : : version, augmentation);
7415 [ + + ]: 310 : if (version >= 4)
7416 : 8 : printf (" address_size: %u\n"
7417 : : " segment_size: %u\n",
7418 : : ptr_size, segment_size);
7419 : 310 : printf (" code_alignment_factor: %u\n"
7420 : : " data_alignment_factor: %d\n"
7421 : : " return_address_register: %u\n",
7422 : : code_alignment_factor,
7423 : : data_alignment_factor, return_address_register);
7424 : :
7425 [ + + ]: 310 : if (augmentation[0] == 'z')
7426 : : {
7427 [ - + ]: 222 : if (cieend - readp < 1)
7428 : 0 : goto invalid_data;
7429 : :
7430 : 222 : unsigned int augmentationlen;
7431 : 222 : get_uleb128 (augmentationlen, readp, cieend);
7432 : :
7433 [ - + ]: 222 : if (augmentationlen > (size_t) (cieend - readp))
7434 : : {
7435 : 0 : error (0, 0, _("invalid augmentation length"));
7436 : 0 : readp = cieend;
7437 : 0 : continue;
7438 : : }
7439 : :
7440 : 222 : const char *hdr = "Augmentation data:";
7441 : 222 : const char *cp = augmentation + 1;
7442 [ + + + - ]: 444 : while (*cp != '\0' && cp < augmentation + augmentationlen + 1)
7443 : : {
7444 : 222 : printf (" %-26s%#x ", hdr, *readp);
7445 : 222 : hdr = "";
7446 : :
7447 [ + - ]: 222 : if (*cp == 'R')
7448 : : {
7449 : 222 : fde_encoding = *readp++;
7450 : 222 : print_encoding_base (_("FDE address encoding: "),
7451 : : fde_encoding);
7452 : : }
7453 [ # # ]: 0 : else if (*cp == 'L')
7454 : : {
7455 : 0 : lsda_encoding = *readp++;
7456 : 0 : print_encoding_base (_("LSDA pointer encoding: "),
7457 : : lsda_encoding);
7458 : : }
7459 [ # # ]: 0 : else if (*cp == 'P')
7460 : : {
7461 : : /* Personality. This field usually has a relocation
7462 : : attached pointing to __gcc_personality_v0. */
7463 : 0 : const unsigned char *startp = readp;
7464 : 0 : unsigned int encoding = *readp++;
7465 : 0 : uint64_t val = 0;
7466 : 0 : readp = read_encoded (encoding, readp,
7467 : : readp - 1 + augmentationlen,
7468 : : &val, dbg);
7469 : :
7470 : 0 : while (++startp < readp)
7471 [ # # ]: 0 : printf ("%#x ", *startp);
7472 : :
7473 : 0 : putchar ('(');
7474 : 0 : print_encoding (encoding);
7475 : 0 : putchar (' ');
7476 [ # # ]: 0 : switch (encoding & 0xf)
7477 : : {
7478 : 0 : case DW_EH_PE_sleb128:
7479 : : case DW_EH_PE_sdata2:
7480 : : case DW_EH_PE_sdata4:
7481 : 0 : printf ("%" PRId64 ")\n", val);
7482 : : break;
7483 : 0 : default:
7484 : 0 : printf ("%#" PRIx64 ")\n", val);
7485 : : break;
7486 : : }
7487 : : }
7488 : : else
7489 : 0 : printf ("(%x)\n", *readp++);
7490 : :
7491 : 222 : ++cp;
7492 : : }
7493 : : }
7494 : :
7495 [ + - ]: 310 : if (likely (ptr_size == 4 || ptr_size == 8))
7496 : : {
7497 : 310 : struct cieinfo *newp = alloca (sizeof (*newp));
7498 : 310 : newp->cie_offset = offset;
7499 : 310 : newp->augmentation = augmentation;
7500 : 310 : newp->fde_encoding = fde_encoding;
7501 : 310 : newp->lsda_encoding = lsda_encoding;
7502 : 310 : newp->address_size = ptr_size;
7503 : 310 : newp->code_alignment_factor = code_alignment_factor;
7504 : 310 : newp->data_alignment_factor = data_alignment_factor;
7505 : 310 : newp->next = cies;
7506 : 310 : cies = newp;
7507 : : }
7508 : : }
7509 : : else
7510 : : {
7511 : : struct cieinfo *cie = cies;
7512 [ + - ]: 22532 : while (cie != NULL)
7513 [ + + - + ]: 45064 : if (is_eh_frame
7514 : 22440 : ? ((Dwarf_Off) start - cie_id) == (Dwarf_Off) cie->cie_offset
7515 : 92 : : cie_id == (Dwarf_Off) cie->cie_offset)
7516 : : break;
7517 : : else
7518 : 0 : cie = cie->next;
7519 [ - + ]: 22532 : if (unlikely (cie == NULL))
7520 : : {
7521 : 0 : puts ("invalid CIE reference in FDE");
7522 : 0 : return;
7523 : : }
7524 : :
7525 : : /* Initialize from CIE data. */
7526 : 22532 : fde_encoding = cie->fde_encoding;
7527 : 22532 : lsda_encoding = cie->lsda_encoding;
7528 : 22532 : ptr_size = encoded_ptr_size (fde_encoding, cie->address_size);
7529 : 22532 : code_alignment_factor = cie->code_alignment_factor;
7530 : 22532 : data_alignment_factor = cie->data_alignment_factor;
7531 : :
7532 : 22532 : const unsigned char *base = readp;
7533 : : // XXX There are sometimes relocations for this value
7534 [ - + + + : 22532 : initial_location = read_addr_unaligned_inc (ptr_size, dbg, readp);
- + + + ]
7535 : 45064 : Dwarf_Word address_range
7536 [ + + - + : 22532 : = read_addr_unaligned_inc (ptr_size, dbg, readp);
+ + ]
7537 : :
7538 : : /* pcrel for an FDE address is relative to the runtime
7539 : : address of the start_address field itself. Sign extend
7540 : : if necessary to make sure the calculation is done on the
7541 : : full 64 bit address even when initial_location only holds
7542 : : the lower 32 bits. */
7543 : 22532 : Dwarf_Addr pc_start = initial_location;
7544 [ + + ]: 22532 : if (ptr_size == 4)
7545 : 22452 : pc_start = (uint64_t) (int32_t) pc_start;
7546 [ + + ]: 22532 : if ((fde_encoding & 0x70) == DW_EH_PE_pcrel)
7547 : 22428 : pc_start += ((uint64_t) shdr->sh_addr
7548 : 22428 : + (base - (const unsigned char *) data->d_buf)
7549 : 22428 : - bias);
7550 : :
7551 : 22532 : printf ("\n [%6tx] FDE length=%" PRIu64 " cie=[%6tx]\n"
7552 : : " CIE_pointer: %" PRIu64 "\n"
7553 : : " initial_location: ",
7554 : : offset, (uint64_t) unit_length,
7555 : : cie->cie_offset, (uint64_t) cie_id);
7556 : 22532 : print_dwarf_addr (dwflmod, cie->address_size,
7557 : : pc_start, initial_location);
7558 [ + + ]: 22532 : if ((fde_encoding & 0x70) == DW_EH_PE_pcrel)
7559 : : {
7560 : 44856 : vma_base = (((uint64_t) shdr->sh_offset
7561 : 22428 : + (base - (const unsigned char *) data->d_buf)
7562 : 22428 : + (uint64_t) initial_location)
7563 : : & (ptr_size == 4
7564 : : ? UINT64_C (0xffffffff)
7565 [ - + ]: 22428 : : UINT64_C (0xffffffffffffffff)));
7566 : 22428 : printf (_(" (offset: %#" PRIx64 ")"),
7567 : : (uint64_t) vma_base);
7568 : : }
7569 : :
7570 : 22532 : printf ("\n address_range: %#" PRIx64,
7571 : : (uint64_t) address_range);
7572 [ + + ]: 22532 : if ((fde_encoding & 0x70) == DW_EH_PE_pcrel)
7573 : 22428 : printf (_(" (end offset: %#" PRIx64 ")"),
7574 : 22428 : ((uint64_t) vma_base + (uint64_t) address_range)
7575 : : & (ptr_size == 4
7576 : : ? UINT64_C (0xffffffff)
7577 [ - + ]: 22428 : : UINT64_C (0xffffffffffffffff)));
7578 : 22532 : putchar ('\n');
7579 : :
7580 [ + + ]: 22532 : if (cie->augmentation[0] == 'z')
7581 : : {
7582 : 22428 : unsigned int augmentationlen;
7583 [ - + ]: 22428 : if (cieend - readp < 1)
7584 : 0 : goto invalid_data;
7585 : 22428 : get_uleb128 (augmentationlen, readp, cieend);
7586 : :
7587 [ - + ]: 22428 : if (augmentationlen > (size_t) (cieend - readp))
7588 : : {
7589 : 0 : error (0, 0, _("invalid augmentation length"));
7590 : 0 : readp = cieend;
7591 : 0 : continue;
7592 : : }
7593 : :
7594 [ - + ]: 22428 : if (augmentationlen > 0)
7595 : : {
7596 : 0 : const char *hdr = "Augmentation data:";
7597 : 0 : const char *cp = cie->augmentation + 1;
7598 : 0 : unsigned int u = 0;
7599 : 0 : while (*cp != '\0'
7600 [ # # # # ]: 0 : && cp < cie->augmentation + augmentationlen + 1)
7601 : : {
7602 [ # # ]: 0 : if (*cp == 'L')
7603 : : {
7604 : 0 : uint64_t lsda_pointer;
7605 : 0 : const unsigned char *p
7606 : 0 : = read_encoded (lsda_encoding, &readp[u],
7607 : : &readp[augmentationlen],
7608 : : &lsda_pointer, dbg);
7609 : 0 : u = p - readp;
7610 : 0 : printf (_("\
7611 : : %-26sLSDA pointer: %#" PRIx64 "\n"),
7612 : : hdr, lsda_pointer);
7613 : 0 : hdr = "";
7614 : : }
7615 : 0 : ++cp;
7616 : : }
7617 : :
7618 [ # # ]: 0 : while (u < augmentationlen)
7619 : : {
7620 : 0 : printf (" %-26s%#x\n", hdr, readp[u++]);
7621 : 0 : hdr = "";
7622 : : }
7623 : : }
7624 : :
7625 : 22428 : readp += augmentationlen;
7626 : : }
7627 : : }
7628 : :
7629 : : /* Handle the initialization instructions. */
7630 [ - + ]: 22842 : if (ptr_size != 4 && ptr_size !=8)
7631 : 0 : printf ("invalid CIE pointer size (%u), must be 4 or 8.\n", ptr_size);
7632 : : else
7633 : 22842 : print_cfa_program (readp, cieend, vma_base, code_alignment_factor,
7634 : : data_alignment_factor, version, ptr_size,
7635 : : fde_encoding, dwflmod, ebl, ehdr, dbg);
7636 : 22842 : readp = cieend;
7637 : : }
7638 : : }
7639 : :
7640 : :
7641 : : /* Returns the signedness (or false if it cannot be determined) and
7642 : : the byte size (or zero if it cannot be gotten) of the given DIE
7643 : : DW_AT_type attribute. Uses dwarf_peel_type and dwarf_aggregate_size. */
7644 : : static void
7645 : 352646 : die_type_sign_bytes (Dwarf_Die *die, bool *is_signed, int *bytes)
7646 : : {
7647 : 352646 : Dwarf_Attribute attr;
7648 : 352646 : Dwarf_Die type;
7649 : :
7650 : 352646 : *bytes = 0;
7651 : 352646 : *is_signed = false;
7652 : :
7653 [ + + ]: 352646 : if (dwarf_peel_type (dwarf_formref_die (dwarf_attr_integrate (die,
7654 : : DW_AT_type,
7655 : : &attr), &type),
7656 : : &type) == 0)
7657 : : {
7658 : 968 : Dwarf_Word val;
7659 : 968 : *is_signed = (dwarf_formudata (dwarf_attr (&type, DW_AT_encoding,
7660 : : &attr), &val) == 0
7661 [ + + + + ]: 968 : && (val == DW_ATE_signed || val == DW_ATE_signed_char));
7662 : :
7663 [ + - ]: 968 : if (dwarf_aggregate_size (&type, &val) == 0)
7664 : 968 : *bytes = val;
7665 : : }
7666 : 352646 : }
7667 : :
7668 : : struct attrcb_args
7669 : : {
7670 : : Dwfl_Module *dwflmod;
7671 : : Dwarf *dbg;
7672 : : Dwarf_Die *dies;
7673 : : int level;
7674 : : bool silent;
7675 : : bool is_split;
7676 : : unsigned int version;
7677 : : unsigned int addrsize;
7678 : : unsigned int offset_size;
7679 : : struct Dwarf_CU *cu;
7680 : : };
7681 : :
7682 : :
7683 : : static int
7684 : 8091134 : attr_callback (Dwarf_Attribute *attrp, void *arg)
7685 : : {
7686 : 8091134 : struct attrcb_args *cbargs = (struct attrcb_args *) arg;
7687 : 8091134 : const int level = cbargs->level;
7688 : 8091134 : Dwarf_Die *die = &cbargs->dies[level];
7689 : 8091134 : bool is_split = cbargs->is_split;
7690 : :
7691 [ + - ]: 8091134 : unsigned int attr = dwarf_whatattr (attrp);
7692 [ - + ]: 8091134 : if (unlikely (attr == 0))
7693 : : {
7694 [ # # ]: 0 : if (!cbargs->silent)
7695 : 0 : error (0, 0, _("DIE [%" PRIx64 "] "
7696 : : "cannot get attribute code: %s"),
7697 : : dwarf_dieoffset (die), dwarf_errmsg (-1));
7698 : 0 : return DWARF_CB_ABORT;
7699 : : }
7700 : :
7701 [ + - ]: 8091134 : unsigned int form = dwarf_whatform (attrp);
7702 [ - + ]: 8091134 : if (unlikely (form == 0))
7703 : : {
7704 [ # # ]: 0 : if (!cbargs->silent)
7705 : 0 : error (0, 0, _("DIE [%" PRIx64 "] "
7706 : : "cannot get attribute form: %s"),
7707 : : dwarf_dieoffset (die), dwarf_errmsg (-1));
7708 : 0 : return DWARF_CB_ABORT;
7709 : : }
7710 : :
7711 [ + + + + : 8091134 : switch (form)
+ + + +
- ]
7712 : : {
7713 : 102890 : case DW_FORM_addr:
7714 : : case DW_FORM_addrx:
7715 : : case DW_FORM_addrx1:
7716 : : case DW_FORM_addrx2:
7717 : : case DW_FORM_addrx3:
7718 : : case DW_FORM_addrx4:
7719 : : case DW_FORM_GNU_addr_index:
7720 [ + + ]: 102890 : if (!cbargs->silent)
7721 : : {
7722 : 102348 : Dwarf_Addr addr;
7723 [ - + ]: 102348 : if (unlikely (dwarf_formaddr (attrp, &addr) != 0))
7724 : : {
7725 : 0 : attrval_out:
7726 [ # # ]: 0 : if (!cbargs->silent)
7727 : 0 : error (0, 0, _("DIE [%" PRIx64 "] "
7728 : : "cannot get attribute '%s' (%s) value: "
7729 : : "%s"),
7730 : : dwarf_dieoffset (die),
7731 : : dwarf_attr_name (attr),
7732 : : dwarf_form_name (form),
7733 : : dwarf_errmsg (-1));
7734 : : /* Don't ABORT, it might be other attributes can be resolved. */
7735 : 0 : return DWARF_CB_OK;
7736 : : }
7737 [ + + ]: 102348 : if (form != DW_FORM_addr )
7738 : : {
7739 : 40 : Dwarf_Word word;
7740 [ - + ]: 40 : if (dwarf_formudata (attrp, &word) != 0)
7741 : 0 : goto attrval_out;
7742 : 40 : printf (" %*s%-20s (%s) [%" PRIx64 "] ",
7743 : : (int) (level * 2), "", dwarf_attr_name (attr),
7744 : : dwarf_form_name (form), word);
7745 : : }
7746 : : else
7747 : 102308 : printf (" %*s%-20s (%s) ",
7748 : : (int) (level * 2), "", dwarf_attr_name (attr),
7749 : : dwarf_form_name (form));
7750 : 102348 : print_dwarf_addr (cbargs->dwflmod, cbargs->addrsize, addr, addr);
7751 : 102348 : printf ("\n");
7752 : : }
7753 : 7276888 : break;
7754 : :
7755 : 1336910 : case DW_FORM_indirect:
7756 : : case DW_FORM_strp:
7757 : : case DW_FORM_line_strp:
7758 : : case DW_FORM_strx:
7759 : : case DW_FORM_strx1:
7760 : : case DW_FORM_strx2:
7761 : : case DW_FORM_strx3:
7762 : : case DW_FORM_strx4:
7763 : : case DW_FORM_string:
7764 : : case DW_FORM_GNU_strp_alt:
7765 : : case DW_FORM_GNU_str_index:
7766 [ + + ]: 1336910 : if (cbargs->silent)
7767 : : break;
7768 : 1335400 : const char *str = dwarf_formstring (attrp);
7769 [ - + ]: 1335400 : if (unlikely (str == NULL))
7770 : 0 : goto attrval_out;
7771 : 1335400 : printf (" %*s%-20s (%s) \"%s\"\n",
7772 : : (int) (level * 2), "", dwarf_attr_name (attr),
7773 : : dwarf_form_name (form), str);
7774 : : break;
7775 : :
7776 : 1758580 : case DW_FORM_ref_addr:
7777 : : case DW_FORM_ref_udata:
7778 : : case DW_FORM_ref8:
7779 : : case DW_FORM_ref4:
7780 : : case DW_FORM_ref2:
7781 : : case DW_FORM_ref1:
7782 : : case DW_FORM_GNU_ref_alt:
7783 : : case DW_FORM_ref_sup4:
7784 : : case DW_FORM_ref_sup8:
7785 [ + + ]: 1758580 : if (cbargs->silent)
7786 : : break;
7787 : 1757000 : Dwarf_Die ref;
7788 [ - + ]: 1757000 : if (unlikely (dwarf_formref_die (attrp, &ref) == NULL))
7789 : 0 : goto attrval_out;
7790 : :
7791 : 1757000 : printf (" %*s%-20s (%s) ",
7792 : : (int) (level * 2), "", dwarf_attr_name (attr),
7793 : : dwarf_form_name (form));
7794 [ + + ]: 1757000 : if (is_split)
7795 : 66 : printf ("{%6" PRIxMAX "}\n", (uintmax_t) dwarf_dieoffset (&ref));
7796 : : else
7797 : 1756934 : printf ("[%6" PRIxMAX "]\n", (uintmax_t) dwarf_dieoffset (&ref));
7798 : : break;
7799 : :
7800 : 8 : case DW_FORM_ref_sig8:
7801 [ + - ]: 8 : if (cbargs->silent)
7802 : : break;
7803 : 8 : printf (" %*s%-20s (%s) {%6" PRIx64 "}\n",
7804 : : (int) (level * 2), "", dwarf_attr_name (attr),
7805 : : dwarf_form_name (form),
7806 [ - + ]: 8 : (uint64_t) read_8ubyte_unaligned (attrp->cu->dbg, attrp->valp));
7807 : : break;
7808 : :
7809 : 4490598 : case DW_FORM_sec_offset:
7810 : : case DW_FORM_rnglistx:
7811 : : case DW_FORM_loclistx:
7812 : : case DW_FORM_implicit_const:
7813 : : case DW_FORM_udata:
7814 : : case DW_FORM_sdata:
7815 : : case DW_FORM_data8: /* Note no data16 here, we see that as block. */
7816 : : case DW_FORM_data4:
7817 : : case DW_FORM_data2:
7818 : 4490598 : case DW_FORM_data1:;
7819 : 4490598 : Dwarf_Word num;
7820 [ - + ]: 4490598 : if (unlikely (dwarf_formudata (attrp, &num) != 0))
7821 : 0 : goto attrval_out;
7822 : :
7823 : 4490598 : const char *valuestr = NULL;
7824 : 4490598 : bool as_hex_id = false;
7825 [ + + + + : 4490598 : switch (attr)
+ + + + +
+ - - - -
- - + - +
+ + ]
7826 : : {
7827 : : /* This case can take either a constant or a loclistptr. */
7828 : 558206 : case DW_AT_data_member_location:
7829 [ + - ]: 558206 : if (form != DW_FORM_sec_offset
7830 [ + + ]: 558206 : && (cbargs->version >= 4
7831 [ + - ]: 47612 : || (form != DW_FORM_data4 && form != DW_FORM_data8)))
7832 : : {
7833 [ + - ]: 558206 : if (!cbargs->silent)
7834 : 558206 : printf (" %*s%-20s (%s) %" PRIuMAX "\n",
7835 : : (int) (level * 2), "", dwarf_attr_name (attr),
7836 : : dwarf_form_name (form), (uintmax_t) num);
7837 : 558206 : return DWARF_CB_OK;
7838 : : }
7839 : 222278 : FALLTHROUGH;
7840 : :
7841 : : /* These cases always take a loclist[ptr] and no constant. */
7842 : : case DW_AT_location:
7843 : : case DW_AT_data_location:
7844 : : case DW_AT_vtable_elem_location:
7845 : : case DW_AT_string_length:
7846 : : case DW_AT_use_location:
7847 : : case DW_AT_frame_base:
7848 : : case DW_AT_return_addr:
7849 : : case DW_AT_static_link:
7850 : : case DW_AT_segment:
7851 : : case DW_AT_GNU_call_site_value:
7852 : : case DW_AT_GNU_call_site_data_value:
7853 : : case DW_AT_GNU_call_site_target:
7854 : : case DW_AT_GNU_call_site_target_clobbered:
7855 : : case DW_AT_GNU_locviews:
7856 : : {
7857 : 222278 : bool nlpt;
7858 [ + + ]: 222278 : if (cbargs->cu->version < 5)
7859 : : {
7860 [ + + ]: 324 : if (! cbargs->is_split)
7861 : : {
7862 : 284 : nlpt = notice_listptr (section_loc, &known_locsptr,
7863 : 284 : cbargs->addrsize,
7864 : 284 : cbargs->offset_size,
7865 : : cbargs->cu, num, attr);
7866 : : }
7867 : : else
7868 : : nlpt = true;
7869 : : }
7870 : : else
7871 : : {
7872 : : /* Only register for a real section offset. Otherwise
7873 : : it is a DW_FORM_loclistx which is just an index
7874 : : number and we should already have registered the
7875 : : section offset for the index when we saw the
7876 : : DW_AT_loclists_base CU attribute. */
7877 [ + + ]: 221954 : if (form == DW_FORM_sec_offset)
7878 : 221876 : nlpt = notice_listptr (section_loc, &known_loclistsptr,
7879 : 221876 : cbargs->addrsize, cbargs->offset_size,
7880 : : cbargs->cu, num, attr);
7881 : : else
7882 : : nlpt = true;
7883 : :
7884 : : }
7885 : :
7886 [ + + ]: 222278 : if (!cbargs->silent)
7887 : : {
7888 [ + + + + ]: 221912 : if (cbargs->cu->version < 5 || form == DW_FORM_sec_offset)
7889 [ - + ]: 221898 : printf (" %*s%-20s (%s) location list [%6"
7890 : : PRIxMAX "]%s\n",
7891 : : (int) (level * 2), "", dwarf_attr_name (attr),
7892 : : dwarf_form_name (form), (uintmax_t) num,
7893 : : nlpt ? "" : " <WARNING offset too big>");
7894 : : else
7895 : 14 : printf (" %*s%-20s (%s) location index [%6"
7896 : : PRIxMAX "]\n",
7897 : : (int) (level * 2), "", dwarf_attr_name (attr),
7898 : : dwarf_form_name (form), (uintmax_t) num);
7899 : : }
7900 : : }
7901 : : return DWARF_CB_OK;
7902 : :
7903 : 10 : case DW_AT_loclists_base:
7904 : : {
7905 : 20 : bool nlpt = notice_listptr (section_loc, &known_loclistsptr,
7906 : 10 : cbargs->addrsize, cbargs->offset_size,
7907 : : cbargs->cu, num, attr);
7908 : :
7909 [ + + ]: 10 : if (!cbargs->silent)
7910 [ - + ]: 2 : printf (" %*s%-20s (%s) location list [%6" PRIxMAX "]%s\n",
7911 : : (int) (level * 2), "", dwarf_attr_name (attr),
7912 : : dwarf_form_name (form), (uintmax_t) num,
7913 : : nlpt ? "" : " <WARNING offset too big>");
7914 : : }
7915 : : return DWARF_CB_OK;
7916 : :
7917 : 33702 : case DW_AT_ranges:
7918 : : case DW_AT_start_scope:
7919 : : {
7920 : 33702 : bool nlpt;
7921 [ + + ]: 33702 : if (cbargs->cu->version < 5)
7922 : 106 : nlpt = notice_listptr (section_ranges, &known_rangelistptr,
7923 : 106 : cbargs->addrsize, cbargs->offset_size,
7924 : : cbargs->cu, num, attr);
7925 : : else
7926 : : {
7927 : : /* Only register for a real section offset. Otherwise
7928 : : it is a DW_FORM_rangelistx which is just an index
7929 : : number and we should already have registered the
7930 : : section offset for the index when we saw the
7931 : : DW_AT_rnglists_base CU attribute. */
7932 [ + + ]: 33596 : if (form == DW_FORM_sec_offset)
7933 : 33584 : nlpt = notice_listptr (section_ranges, &known_rnglistptr,
7934 : 33584 : cbargs->addrsize, cbargs->offset_size,
7935 : : cbargs->cu, num, attr);
7936 : : else
7937 : : nlpt = true;
7938 : : }
7939 : :
7940 [ + + ]: 33702 : if (!cbargs->silent)
7941 : : {
7942 [ + + + + ]: 33616 : if (cbargs->cu->version < 5 || form == DW_FORM_sec_offset)
7943 [ - + ]: 33612 : printf (" %*s%-20s (%s) range list [%6"
7944 : : PRIxMAX "]%s\n",
7945 : : (int) (level * 2), "", dwarf_attr_name (attr),
7946 : : dwarf_form_name (form), (uintmax_t) num,
7947 : : nlpt ? "" : " <WARNING offset too big>");
7948 : : else
7949 : 4 : printf (" %*s%-20s (%s) range index [%6"
7950 : : PRIxMAX "]\n",
7951 : : (int) (level * 2), "", dwarf_attr_name (attr),
7952 : : dwarf_form_name (form), (uintmax_t) num);
7953 : : }
7954 : : }
7955 : : return DWARF_CB_OK;
7956 : :
7957 : 8 : case DW_AT_rnglists_base:
7958 : : {
7959 : 16 : bool nlpt = notice_listptr (section_ranges, &known_rnglistptr,
7960 : 8 : cbargs->addrsize, cbargs->offset_size,
7961 : : cbargs->cu, num, attr);
7962 [ + + ]: 8 : if (!cbargs->silent)
7963 [ - + ]: 4 : printf (" %*s%-20s (%s) range list [%6"
7964 : : PRIxMAX "]%s\n",
7965 : : (int) (level * 2), "", dwarf_attr_name (attr),
7966 : : dwarf_form_name (form), (uintmax_t) num,
7967 : : nlpt ? "" : " <WARNING offset too big>");
7968 : : }
7969 : : return DWARF_CB_OK;
7970 : :
7971 : 34 : case DW_AT_addr_base:
7972 : : case DW_AT_GNU_addr_base:
7973 : : {
7974 : 68 : bool addrbase = notice_listptr (section_addr, &known_addrbases,
7975 : 34 : cbargs->addrsize,
7976 : 34 : cbargs->offset_size,
7977 : : cbargs->cu, num, attr);
7978 [ + + ]: 34 : if (!cbargs->silent)
7979 [ - + ]: 18 : printf (" %*s%-20s (%s) address base [%6"
7980 : : PRIxMAX "]%s\n",
7981 : : (int) (level * 2), "", dwarf_attr_name (attr),
7982 : : dwarf_form_name (form), (uintmax_t) num,
7983 : : addrbase ? "" : " <WARNING offset too big>");
7984 : : }
7985 : : return DWARF_CB_OK;
7986 : :
7987 : 8 : case DW_AT_str_offsets_base:
7988 : : {
7989 : 16 : bool stroffbase = notice_listptr (section_str, &known_stroffbases,
7990 : 8 : cbargs->addrsize,
7991 : 8 : cbargs->offset_size,
7992 : : cbargs->cu, num, attr);
7993 [ + - ]: 8 : if (!cbargs->silent)
7994 [ - + ]: 814246 : printf (" %*s%-20s (%s) str offsets base [%6"
7995 : : PRIxMAX "]%s\n",
7996 : : (int) (level * 2), "", dwarf_attr_name (attr),
7997 : : dwarf_form_name (form), (uintmax_t) num,
7998 : : stroffbase ? "" : " <WARNING offset too big>");
7999 : : }
8000 : : return DWARF_CB_OK;
8001 : :
8002 : 3732 : case DW_AT_language:
8003 : 3732 : valuestr = dwarf_lang_name (num);
8004 : 3732 : break;
8005 : 4 : case DW_AT_language_name:
8006 : 4 : valuestr = dwarf_lname_name (num);
8007 : 4 : break;
8008 : 57348 : case DW_AT_encoding:
8009 : 57348 : valuestr = dwarf_encoding_name (num);
8010 : 57348 : break;
8011 : 0 : case DW_AT_accessibility:
8012 : 0 : valuestr = dwarf_access_name (num);
8013 : 0 : break;
8014 : 0 : case DW_AT_defaulted:
8015 : 0 : valuestr = dwarf_defaulted_name (num);
8016 : 0 : break;
8017 : 0 : case DW_AT_visibility:
8018 : 0 : valuestr = dwarf_visibility_name (num);
8019 : 0 : break;
8020 : 0 : case DW_AT_virtuality:
8021 : 0 : valuestr = dwarf_virtuality_name (num);
8022 : 0 : break;
8023 : 0 : case DW_AT_identifier_case:
8024 : 0 : valuestr = dwarf_identifier_case_name (num);
8025 : 0 : break;
8026 : 0 : case DW_AT_calling_convention:
8027 : 0 : valuestr = dwarf_calling_convention_name (num);
8028 : 0 : break;
8029 : 7206 : case DW_AT_inline:
8030 : 7206 : valuestr = dwarf_inline_name (num);
8031 : 7206 : break;
8032 : 0 : case DW_AT_ordering:
8033 : 0 : valuestr = dwarf_ordering_name (num);
8034 : 0 : break;
8035 : 987178 : case DW_AT_decl_file:
8036 : : case DW_AT_call_file:
8037 : : {
8038 [ + + ]: 987178 : if (cbargs->silent)
8039 : : break;
8040 : :
8041 : : /* Try to get the actual file, the current interface only
8042 : : gives us full paths, but we only want to show the file
8043 : : name for now. */
8044 : 986330 : Dwarf_Die cudie;
8045 [ + - ]: 986330 : if (dwarf_cu_die (cbargs->cu, &cudie,
8046 : : NULL, NULL, NULL, NULL, NULL, NULL) != NULL)
8047 : : {
8048 : 986330 : Dwarf_Files *files;
8049 : 986330 : size_t nfiles;
8050 [ + - ]: 986330 : if (dwarf_getsrcfiles (&cudie, &files, &nfiles) == 0)
8051 : : {
8052 : 986330 : valuestr = dwarf_filesrc (files, num, NULL, NULL);
8053 [ + - ]: 986330 : if (valuestr != NULL)
8054 : : {
8055 : 986330 : char *filename = strrchr (valuestr, '/');
8056 [ + - ]: 986330 : if (filename != NULL)
8057 : 986330 : valuestr = filename + 1;
8058 : : }
8059 : : else
8060 : 0 : error (0, 0, _("invalid file (%" PRId64 "): %s"),
8061 : : num, dwarf_errmsg (-1));
8062 : : }
8063 : : else
8064 : 0 : error (0, 0, _("no srcfiles for CU [%" PRIx64 "]"),
8065 : : dwarf_dieoffset (&cudie));
8066 : : }
8067 : : else
8068 : 0 : error (0, 0, _("couldn't get DWARF CU: %s"),
8069 : : dwarf_errmsg (-1));
8070 : 986330 : if (valuestr == NULL)
8071 : : valuestr = "???";
8072 : : }
8073 : 986330 : break;
8074 : 26 : case DW_AT_GNU_dwo_id:
8075 : 26 : as_hex_id = true;
8076 : 26 : break;
8077 : :
8078 : : default:
8079 : : /* Nothing. */
8080 : : break;
8081 : : }
8082 : :
8083 [ + + ]: 3676352 : if (cbargs->silent)
8084 : : break;
8085 : :
8086 : : /* When highpc is in constant form it is relative to lowpc.
8087 : : In that case also show the address. */
8088 : 3672642 : Dwarf_Addr highpc;
8089 [ + + + - ]: 3672642 : if (attr == DW_AT_high_pc && dwarf_highpc (die, &highpc) == 0)
8090 : : {
8091 : 25418 : printf (" %*s%-20s (%s) %" PRIuMAX " (",
8092 : : (int) (level * 2), "", dwarf_attr_name (attr),
8093 : : dwarf_form_name (form), (uintmax_t) num);
8094 : 25418 : print_dwarf_addr (cbargs->dwflmod, cbargs->addrsize, highpc, highpc);
8095 : 25418 : printf (")\n");
8096 : : }
8097 : : else
8098 : : {
8099 [ + + ]: 3647224 : if (as_hex_id)
8100 : : {
8101 : 4 : printf (" %*s%-20s (%s) 0x%.16" PRIx64 "\n",
8102 : : (int) (level * 2), "", dwarf_attr_name (attr),
8103 : : dwarf_form_name (form), num);
8104 : : }
8105 : : else
8106 : : {
8107 : 3647220 : Dwarf_Sword snum = 0;
8108 : 3647220 : bool is_signed;
8109 : 3647220 : int bytes = 0;
8110 [ + + ]: 3647220 : if (attr == DW_AT_const_value)
8111 : 352638 : die_type_sign_bytes (die, &is_signed, &bytes);
8112 : : else
8113 : 3294582 : is_signed = (form == DW_FORM_sdata
8114 : 3294582 : || form == DW_FORM_implicit_const);
8115 : :
8116 [ + + ]: 3647220 : if (is_signed)
8117 [ - + ]: 381286 : if (unlikely (dwarf_formsdata (attrp, &snum) != 0))
8118 : 0 : goto attrval_out;
8119 : :
8120 [ + + ]: 3647220 : if (valuestr == NULL)
8121 : : {
8122 : 2593210 : printf (" %*s%-20s (%s) ",
8123 : : (int) (level * 2), "", dwarf_attr_name (attr),
8124 : : dwarf_form_name (form));
8125 : : }
8126 : : else
8127 : : {
8128 : 1054010 : printf (" %*s%-20s (%s) %s (",
8129 : : (int) (level * 2), "", dwarf_attr_name (attr),
8130 : : dwarf_form_name (form), valuestr);
8131 : : }
8132 : :
8133 [ + + + + : 3647220 : switch (bytes)
+ ]
8134 : : {
8135 : 10 : case 1:
8136 [ + + ]: 10 : if (is_signed)
8137 : 2 : printf ("%" PRId8, (int8_t) snum);
8138 : : else
8139 : 8 : printf ("%" PRIu8, (uint8_t) num);
8140 : : break;
8141 : :
8142 : 4 : case 2:
8143 [ + + ]: 4 : if (is_signed)
8144 : 2 : printf ("%" PRId16, (int16_t) snum);
8145 : : else
8146 : 2 : printf ("%" PRIu16, (uint16_t) num);
8147 : : break;
8148 : :
8149 : 258 : case 4:
8150 [ + + ]: 258 : if (is_signed)
8151 : 246 : printf ("%" PRId32, (int32_t) snum);
8152 : : else
8153 : 12 : printf ("%" PRIu32, (uint32_t) num);
8154 : : break;
8155 : :
8156 : 688 : case 8:
8157 [ + + ]: 688 : if (is_signed)
8158 : 2 : printf ("%" PRId64, (int64_t) snum);
8159 : : else
8160 : 686 : printf ("%" PRIu64, (uint64_t) num);
8161 : : break;
8162 : :
8163 : 3646260 : default:
8164 [ + + ]: 3646260 : if (is_signed)
8165 : 381034 : printf ("%" PRIdMAX, (intmax_t) snum);
8166 : : else
8167 : 3265226 : printf ("%" PRIuMAX, (uintmax_t) num);
8168 : : break;
8169 : : }
8170 : :
8171 : : /* Make clear if we switched from a signed encoding to
8172 : : an unsigned value. */
8173 [ + + ]: 3647220 : if (attr == DW_AT_const_value
8174 [ + + ]: 352638 : && (form == DW_FORM_sdata || form == DW_FORM_implicit_const)
8175 [ + + ]: 2650 : && !is_signed)
8176 : 2632 : printf (" (%" PRIdMAX ")", (intmax_t) num);
8177 : :
8178 [ + + ]: 3647220 : if (valuestr == NULL)
8179 : 2593210 : printf ("\n");
8180 : : else
8181 : 3647220 : printf (")\n");
8182 : : }
8183 : : }
8184 : : break;
8185 : :
8186 : 18244 : case DW_FORM_flag:
8187 [ + + ]: 18244 : if (cbargs->silent)
8188 : : break;
8189 : 18096 : bool flag;
8190 [ - + ]: 18096 : if (unlikely (dwarf_formflag (attrp, &flag) != 0))
8191 : 0 : goto attrval_out;
8192 : :
8193 : 18096 : printf (" %*s%-20s (%s) %s\n",
8194 : : (int) (level * 2), "", dwarf_attr_name (attr),
8195 [ + - ]: 18096 : dwarf_form_name (form), flag ? yes_str : no_str);
8196 : : break;
8197 : :
8198 : 160514 : case DW_FORM_flag_present:
8199 [ + + ]: 160514 : if (cbargs->silent)
8200 : : break;
8201 : 159960 : printf (" %*s%-20s (%s) %s\n",
8202 : : (int) (level * 2), "", dwarf_attr_name (attr),
8203 : : dwarf_form_name (form), yes_str);
8204 : : break;
8205 : :
8206 : 223390 : case DW_FORM_exprloc:
8207 : : case DW_FORM_block4:
8208 : : case DW_FORM_block2:
8209 : : case DW_FORM_block1:
8210 : : case DW_FORM_block:
8211 : : case DW_FORM_data16: /* DWARF5 calls this a constant class. */
8212 [ + + ]: 223390 : if (cbargs->silent)
8213 : : break;
8214 : 223128 : Dwarf_Block block;
8215 [ - + ]: 223128 : if (unlikely (dwarf_formblock (attrp, &block) != 0))
8216 : 0 : goto attrval_out;
8217 : :
8218 : 223128 : printf (" %*s%-20s (%s) ",
8219 : : (int) (level * 2), "", dwarf_attr_name (attr),
8220 : : dwarf_form_name (form));
8221 : :
8222 [ + + + ]: 223128 : switch (attr)
8223 : : {
8224 : 96130 : default:
8225 [ - + ]: 96130 : if (form != DW_FORM_exprloc)
8226 : : {
8227 : 0 : print_block (block.length, block.data);
8228 : 0 : break;
8229 : : }
8230 : 126990 : FALLTHROUGH;
8231 : :
8232 : : case DW_AT_location:
8233 : : case DW_AT_data_location:
8234 : : case DW_AT_data_member_location:
8235 : : case DW_AT_vtable_elem_location:
8236 : : case DW_AT_string_length:
8237 : : case DW_AT_use_location:
8238 : : case DW_AT_frame_base:
8239 : : case DW_AT_return_addr:
8240 : : case DW_AT_static_link:
8241 : : case DW_AT_allocated:
8242 : : case DW_AT_associated:
8243 : : case DW_AT_bit_size:
8244 : : case DW_AT_bit_offset:
8245 : : case DW_AT_bit_stride:
8246 : : case DW_AT_byte_size:
8247 : : case DW_AT_byte_stride:
8248 : : case DW_AT_count:
8249 : : case DW_AT_lower_bound:
8250 : : case DW_AT_upper_bound:
8251 : : case DW_AT_GNU_call_site_value:
8252 : : case DW_AT_GNU_call_site_data_value:
8253 : : case DW_AT_GNU_call_site_target:
8254 : : case DW_AT_GNU_call_site_target_clobbered:
8255 [ + + ]: 126990 : if (form == DW_FORM_exprloc
8256 [ + - ]: 140 : || (form != DW_FORM_data16
8257 [ + - ]: 140 : && attrp->cu->version < 4)) /* blocks were expressions. */
8258 : : {
8259 : 223120 : putchar ('\n');
8260 : 223120 : print_ops (cbargs->dwflmod, cbargs->dbg,
8261 : 223120 : 12 + level * 2, 12 + level * 2,
8262 : : cbargs->version, cbargs->addrsize, cbargs->offset_size,
8263 : 223120 : attrp->cu, block.length, block.data);
8264 : : }
8265 : : else
8266 : 0 : print_block (block.length, block.data);
8267 : : break;
8268 : :
8269 : 8 : case DW_AT_discr_list:
8270 [ - + ]: 8 : if (block.length == 0)
8271 : 0 : puts ("<default>");
8272 [ + - ]: 8 : else if (form != DW_FORM_data16)
8273 : : {
8274 : 8 : const unsigned char *readp = block.data;
8275 : 8 : const unsigned char *readendp = readp + block.length;
8276 : :
8277 : : /* See if we are dealing with a signed or unsigned
8278 : : values. If the parent of this variant DIE is a
8279 : : variant_part then it will either have a discriminant
8280 : : which points to the member which type is the
8281 : : discriminant type. Or the variant_part itself has a
8282 : : type representing the discriminant. */
8283 : 8 : bool is_signed = false;
8284 [ + - ]: 8 : if (level > 0)
8285 : : {
8286 : 8 : Dwarf_Die *parent = &cbargs->dies[level - 1];
8287 [ + - ]: 8 : if (dwarf_tag (die) == DW_TAG_variant
8288 [ + - ]: 8 : && dwarf_tag (parent) == DW_TAG_variant_part)
8289 : : {
8290 : 8 : Dwarf_Die member;
8291 : 8 : Dwarf_Attribute discr_attr;
8292 : 8 : int bytes;
8293 [ + - ]: 8 : if (dwarf_formref_die (dwarf_attr (parent,
8294 : : DW_AT_discr,
8295 : : &discr_attr),
8296 : : &member) != NULL)
8297 : 8 : die_type_sign_bytes (&member, &is_signed, &bytes);
8298 : : else
8299 : 0 : die_type_sign_bytes (parent, &is_signed, &bytes);
8300 : : }
8301 : : }
8302 : 24 : while (readp < readendp)
8303 : : {
8304 : 16 : int d = (int) *readp++;
8305 : 16 : printf ("%s ", dwarf_discr_list_name (d));
8306 [ - + ]: 16 : if (readp >= readendp)
8307 : 0 : goto attrval_out;
8308 : :
8309 : 16 : Dwarf_Word val;
8310 : 16 : Dwarf_Sword sval;
8311 [ + + ]: 16 : if (d == DW_DSC_label)
8312 : : {
8313 [ + + ]: 8 : if (is_signed)
8314 : : {
8315 : 4 : get_sleb128 (sval, readp, readendp);
8316 : 4 : printf ("%" PRId64 "", sval);
8317 : : }
8318 : : else
8319 : : {
8320 : 4 : get_uleb128 (val, readp, readendp);
8321 : 4 : printf ("%" PRIu64 "", val);
8322 : : }
8323 : : }
8324 [ + - ]: 8 : else if (d == DW_DSC_range)
8325 : : {
8326 [ + + ]: 8 : if (is_signed)
8327 : : {
8328 : 6 : get_sleb128 (sval, readp, readendp);
8329 : 6 : printf ("%" PRId64 "..", sval);
8330 [ - + ]: 6 : if (readp >= readendp)
8331 : 0 : goto attrval_out;
8332 : 6 : get_sleb128 (sval, readp, readendp);
8333 : 6 : printf ("%" PRId64 "", sval);
8334 : : }
8335 : : else
8336 : : {
8337 : 2 : get_uleb128 (val, readp, readendp);
8338 : 2 : printf ("%" PRIu64 "..", val);
8339 [ - + ]: 2 : if (readp >= readendp)
8340 : 0 : goto attrval_out;
8341 : 2 : get_uleb128 (val, readp, readendp);
8342 : 2 : printf ("%" PRIu64 "", val);
8343 : : }
8344 : : }
8345 : : else
8346 : : {
8347 : 0 : print_block (readendp - readp, readp);
8348 : 0 : break;
8349 : : }
8350 [ + + ]: 16 : if (readp < readendp)
8351 [ + + ]: 32 : printf (", ");
8352 : : }
8353 : 8 : putchar ('\n');
8354 : : }
8355 : : else
8356 : 0 : print_block (block.length, block.data);
8357 : : break;
8358 : : }
8359 : : break;
8360 : :
8361 : 0 : default:
8362 [ # # ]: 0 : if (cbargs->silent)
8363 : : break;
8364 : 0 : printf (" %*s%-20s (%s) ???\n",
8365 : : (int) (level * 2), "", dwarf_attr_name (attr),
8366 : : dwarf_form_name (form));
8367 : : break;
8368 : : }
8369 : :
8370 : 7276888 : return DWARF_CB_OK;
8371 : : }
8372 : :
8373 : : static void
8374 : 208 : print_debug_units (Dwfl_Module *dwflmod,
8375 : : Ebl *ebl, GElf_Ehdr *ehdr __attribute__ ((unused)),
8376 : : Elf_Scn *scn, GElf_Shdr *shdr,
8377 : : Dwarf *dbg, bool debug_types)
8378 : : {
8379 [ + + + + ]: 208 : const bool silent = !(print_debug_sections & section_info) && !debug_types;
8380 : 208 : const char *secname = section_name (ebl, shdr);
8381 : :
8382 : : /* Check section actually exists. */
8383 [ + + ]: 208 : if (!silent)
8384 [ + - ]: 136 : if (get_debug_elf_data (dbg, ebl,
8385 : : debug_types ? IDX_debug_types : IDX_debug_info,
8386 : : scn) == NULL)
8387 : 0 : return;
8388 : :
8389 : 136 : if (!silent)
8390 : 136 : printf (_("\
8391 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n [Offset]\n"),
8392 : 136 : elf_ndxscn (scn), secname, (uint64_t) shdr->sh_offset);
8393 : :
8394 : : /* If the section is empty we don't have to do anything. */
8395 [ + + + - ]: 208 : if (!silent && shdr->sh_size == 0)
8396 : : return;
8397 : :
8398 : 208 : int maxdies = 20;
8399 : 208 : Dwarf_Die *dies = xmalloc (maxdies * sizeof (Dwarf_Die));
8400 : :
8401 : : /* New compilation unit. */
8402 : 208 : Dwarf_Half version;
8403 : :
8404 : 208 : Dwarf_Die result;
8405 : 208 : Dwarf_Off abbroffset;
8406 : 208 : uint8_t addrsize;
8407 : 208 : uint8_t offsize;
8408 : 208 : uint64_t unit_id;
8409 : 208 : Dwarf_Off subdie_off;
8410 : :
8411 : 208 : int unit_res;
8412 : 208 : Dwarf_CU *cu;
8413 : 208 : Dwarf_CU cu_mem;
8414 : 208 : uint8_t unit_type;
8415 : 208 : Dwarf_Die cudie;
8416 : :
8417 : : /* We cheat a little because we want to see only the CUs from .debug_info
8418 : : or .debug_types. We know the Dwarf_CU struct layout. Set it up at
8419 : : the end of .debug_info if we want .debug_types only. Check the returned
8420 : : Dwarf_CU is still in the expected section. */
8421 [ + + ]: 208 : if (debug_types)
8422 : : {
8423 : 2 : cu_mem.dbg = dbg;
8424 : 2 : cu_mem.end = dbg->sectiondata[IDX_debug_info]->d_size;
8425 : 2 : cu_mem.sec_idx = IDX_debug_info;
8426 : 2 : cu = &cu_mem;
8427 : : }
8428 : : else
8429 : 206 : cu = NULL;
8430 : :
8431 : : next_cu:
8432 : 3958 : unit_res = dwarf_get_units (dbg, cu, &cu, &version, &unit_type,
8433 : : &cudie, NULL);
8434 [ + + ]: 3958 : if (unit_res == 1)
8435 : 206 : goto do_return;
8436 : :
8437 [ - + ]: 3752 : if (unit_res == -1)
8438 : : {
8439 [ # # ]: 0 : if (!silent)
8440 : 0 : error (0, 0, _("cannot get next unit: %s"), dwarf_errmsg (-1));
8441 : 0 : goto do_return;
8442 : : }
8443 : :
8444 [ + + + + ]: 7500 : if (cu->sec_idx != (size_t) (debug_types ? IDX_debug_types : IDX_debug_info))
8445 : 2 : goto do_return;
8446 : :
8447 : 3750 : dwarf_cu_die (cu, &result, NULL, &abbroffset, &addrsize, &offsize,
8448 : : &unit_id, &subdie_off);
8449 : :
8450 [ + + ]: 3750 : if (!silent)
8451 : : {
8452 : 3650 : Dwarf_Off offset = cu->start;
8453 [ + + + - ]: 3650 : if (debug_types && version < 5)
8454 : 4 : {
8455 : 4 : Dwarf_Die typedie;
8456 : 4 : Dwarf_Off dieoffset;
8457 : 4 : dieoffset = dwarf_dieoffset (dwarf_offdie_types (dbg, cu->start
8458 : : + subdie_off,
8459 : : &typedie));
8460 : 4 : printf (_(" Type unit at offset %" PRIu64 ":\n"
8461 : : " Version: %" PRIu16
8462 : : ", Abbreviation section offset: %" PRIu64
8463 : : ", Address size: %" PRIu8
8464 : : ", Offset size: %" PRIu8
8465 : : "\n Type signature: %#" PRIx64
8466 : : ", Type offset: %#" PRIx64 " [%" PRIx64 "]\n"),
8467 : : (uint64_t) offset, version, abbroffset, addrsize, offsize,
8468 : : unit_id, (uint64_t) subdie_off, dieoffset);
8469 : : }
8470 : : else
8471 : : {
8472 : 3646 : printf (_(" Compilation unit at offset %" PRIu64 ":\n"
8473 : : " Version: %" PRIu16
8474 : : ", Abbreviation section offset: %" PRIu64
8475 : : ", Address size: %" PRIu8
8476 : : ", Offset size: %" PRIu8 "\n"),
8477 : : (uint64_t) offset, version, abbroffset, addrsize, offsize);
8478 : :
8479 [ + + ]: 3646 : if (version >= 5 || (unit_type != DW_UT_compile
8480 [ + + ]: 170 : && unit_type != DW_UT_partial))
8481 : : {
8482 : 3478 : printf (_(" Unit type: %s (%" PRIu8 ")"),
8483 : : dwarf_unit_name (unit_type), unit_type);
8484 : 3478 : if (unit_type == DW_UT_type
8485 [ + + ]: 3478 : || unit_type == DW_UT_skeleton
8486 [ + - ]: 3468 : || unit_type == DW_UT_split_compile
8487 [ - + ]: 3468 : || unit_type == DW_UT_split_type)
8488 : 10 : printf (", Unit id: 0x%.16" PRIx64 "", unit_id);
8489 : 3478 : if (unit_type == DW_UT_type
8490 [ - + ]: 3478 : || unit_type == DW_UT_split_type)
8491 : : {
8492 : 0 : Dwarf_Die typedie;
8493 : 0 : Dwarf_Off dieoffset;
8494 : 0 : dwarf_cu_info (cu, NULL, NULL, NULL, &typedie,
8495 : : NULL, NULL, NULL);
8496 : 0 : dieoffset = dwarf_dieoffset (&typedie);
8497 : 0 : printf (", Unit DIE off: %#" PRIx64 " [%" PRIx64 "]",
8498 : : subdie_off, dieoffset);
8499 : : }
8500 : 3478 : printf ("\n");
8501 : : }
8502 : : }
8503 : : }
8504 : :
8505 [ + - ]: 3750 : if (version < 2 || version > 5
8506 [ + - - + ]: 3750 : || unit_type < DW_UT_compile || unit_type > DW_UT_split_type)
8507 : : {
8508 [ # # ]: 0 : if (!silent)
8509 : 0 : error (0, 0, _("unknown version (%d) or unit type (%d)"),
8510 : : version, unit_type);
8511 : 0 : goto next_cu;
8512 : : }
8513 : :
8514 : 3750 : struct attrcb_args args =
8515 : : {
8516 : : .dwflmod = dwflmod,
8517 : : .silent = silent,
8518 : : .version = version,
8519 : : .addrsize = addrsize,
8520 : : .offset_size = offsize
8521 : : };
8522 : :
8523 : 3750 : bool is_split = false;
8524 : 3750 : int level = 0;
8525 : 3750 : dies[0] = cudie;
8526 : 3750 : args.cu = dies[0].cu;
8527 : 3750 : args.dbg = dbg;
8528 : 3750 : args.is_split = is_split;
8529 : :
8530 : : /* We might return here again for the split CU subdie. */
8531 : : do_cu:
8532 : 2129612 : do
8533 : : {
8534 : 2129612 : Dwarf_Off offset = dwarf_dieoffset (&dies[level]);
8535 [ - + ]: 2129612 : if (unlikely (offset == (Dwarf_Off) -1))
8536 : : {
8537 [ # # ]: 0 : if (!silent)
8538 : 0 : error (0, 0, _("cannot get DIE offset: %s"),
8539 : : dwarf_errmsg (-1));
8540 : 0 : goto do_return;
8541 : : }
8542 : :
8543 : 2129612 : int tag = dwarf_tag (&dies[level]);
8544 [ - + ]: 2129612 : if (unlikely (tag == DW_TAG_invalid))
8545 : : {
8546 [ # # ]: 0 : if (!silent)
8547 : 0 : error (0, 0, _("cannot get tag of DIE at offset [%" PRIx64
8548 : : "] in section '%s': %s"),
8549 : : (uint64_t) offset, secname, dwarf_errmsg (-1));
8550 : 0 : goto do_return;
8551 : : }
8552 : :
8553 [ + + ]: 2129612 : if (!silent)
8554 : : {
8555 : 2127626 : unsigned int code = dwarf_getabbrevcode (dies[level].abbrev);
8556 [ + + ]: 2127626 : if (is_split)
8557 : 96 : printf (" {%6" PRIx64 "} ", (uint64_t) offset);
8558 : : else
8559 : 2127530 : printf (" [%6" PRIx64 "] ", (uint64_t) offset);
8560 : 2127626 : printf ("%*s%-20s abbrev: %u\n", (int) (level * 2), "",
8561 : : dwarf_tag_name (tag), code);
8562 : : }
8563 : :
8564 : : /* Print the attribute values. */
8565 : 2129612 : args.level = level;
8566 : 2129612 : args.dies = dies;
8567 : 2129612 : (void) dwarf_getattrs (&dies[level], attr_callback, &args, 0);
8568 : :
8569 : : /* Make room for the next level's DIE. */
8570 [ - + ]: 2129612 : if (level + 1 == maxdies)
8571 : 0 : dies = xrealloc (dies, (maxdies += 10) * sizeof (Dwarf_Die));
8572 : :
8573 : 2129612 : int res = dwarf_child (&dies[level], &dies[level + 1]);
8574 [ + + ]: 2129612 : if (res > 0)
8575 : : {
8576 [ + + ]: 2129612 : while ((res = dwarf_siblingof (&dies[level], &dies[level])) == 1)
8577 [ + + ]: 294758 : if (level-- == 0)
8578 : : break;
8579 : :
8580 [ - + ]: 1838612 : if (unlikely (res == -1))
8581 : : {
8582 [ # # ]: 0 : if (!silent)
8583 : 0 : error (0, 0, _("cannot get next DIE: %s\n"),
8584 : : dwarf_errmsg (-1));
8585 : 0 : goto do_return;
8586 : : }
8587 : : }
8588 [ - + ]: 291000 : else if (unlikely (res < 0))
8589 : : {
8590 [ # # ]: 0 : if (!silent)
8591 : 0 : error (0, 0, _("cannot get next DIE: %s"),
8592 : : dwarf_errmsg (-1));
8593 : 0 : goto do_return;
8594 : : }
8595 : : else
8596 : : ++level;
8597 : : }
8598 [ + + ]: 2129612 : while (level >= 0);
8599 : :
8600 : : /* We might want to show the split compile unit if this was a skeleton.
8601 : : We need to scan it if we are requesting printing .debug_ranges for
8602 : : DWARF4 since GNU DebugFission uses "offsets" into the main ranges
8603 : : section. */
8604 [ + + ]: 3758 : if (unit_type == DW_UT_skeleton
8605 [ + + + + ]: 26 : && ((!silent && show_split_units)
8606 [ + + + + ]: 20 : || (version < 5 && (print_debug_sections & section_ranges) != 0)))
8607 : : {
8608 : 10 : Dwarf_Die subdie;
8609 [ + - ]: 10 : if (dwarf_cu_info (cu, NULL, NULL, NULL, &subdie, NULL, NULL, NULL) != 0
8610 [ + + ]: 10 : || dwarf_tag (&subdie) == DW_TAG_invalid)
8611 : : {
8612 [ + - ]: 2 : if (!silent)
8613 : : {
8614 : 2 : Dwarf_Attribute dwo_at;
8615 : 4 : const char *dwo_name =
8616 : 2 : (dwarf_formstring (dwarf_attr (&cudie, DW_AT_dwo_name,
8617 : : &dwo_at))
8618 [ - + ]: 2 : ?: (dwarf_formstring (dwarf_attr (&cudie, DW_AT_GNU_dwo_name,
8619 : : &dwo_at))
8620 [ # # ]: 0 : ?: "<unknown>"));
8621 : 2 : fprintf (stderr,
8622 : : "Could not find split unit '%s', id: %" PRIx64 "\n",
8623 : : dwo_name, unit_id);
8624 : : }
8625 : : }
8626 : : else
8627 : : {
8628 : 8 : Dwarf_CU *split_cu = subdie.cu;
8629 : 8 : dwarf_cu_die (split_cu, &result, NULL, &abbroffset,
8630 : : &addrsize, &offsize, &unit_id, &subdie_off);
8631 : 8 : Dwarf_Off offset = cu->start;
8632 : :
8633 [ + + ]: 8 : if (!silent)
8634 : : {
8635 : 4 : printf (_(" Split compilation unit at offset %"
8636 : : PRIu64 ":\n"
8637 : : " Version: %" PRIu16
8638 : : ", Abbreviation section offset: %" PRIu64
8639 : : ", Address size: %" PRIu8
8640 : : ", Offset size: %" PRIu8 "\n"),
8641 : : (uint64_t) offset, version, abbroffset,
8642 : : addrsize, offsize);
8643 : 4 : printf (_(" Unit type: %s (%" PRIu8 ")"),
8644 : : dwarf_unit_name (unit_type), unit_type);
8645 : 4 : printf (", Unit id: 0x%.16" PRIx64 "", unit_id);
8646 : 4 : printf ("\n");
8647 : : }
8648 : :
8649 : 8 : unit_type = DW_UT_split_compile;
8650 : 8 : is_split = true;
8651 : 8 : level = 0;
8652 : 8 : dies[0] = subdie;
8653 : 8 : args.cu = dies[0].cu;
8654 : 8 : args.dbg = split_cu->dbg;
8655 : 8 : args.is_split = is_split;
8656 : 8 : goto do_cu;
8657 : : }
8658 : : }
8659 : :
8660 : : /* And again... */
8661 : 3750 : goto next_cu;
8662 : :
8663 : 208 : do_return:
8664 : 208 : free (dies);
8665 : : }
8666 : :
8667 : : static void
8668 : 206 : print_debug_info_section (Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr,
8669 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
8670 : : {
8671 : 42 : print_debug_units (dwflmod, ebl, ehdr, scn, shdr, dbg, false);
8672 : 42 : }
8673 : :
8674 : : static void
8675 : 2 : print_debug_types_section (Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr,
8676 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
8677 : : {
8678 : 2 : print_debug_units (dwflmod, ebl, ehdr, scn, shdr, dbg, true);
8679 : 2 : }
8680 : :
8681 : :
8682 : : static void
8683 : 82 : print_decoded_line_section (Dwfl_Module *dwflmod, Ebl *ebl,
8684 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
8685 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
8686 : : {
8687 : 82 : printf (_("\
8688 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n\n"),
8689 : : elf_ndxscn (scn), section_name (ebl, shdr),
8690 : 82 : (uint64_t) shdr->sh_offset);
8691 : :
8692 : 164 : size_t address_size
8693 [ + + ]: 82 : = elf_getident (ebl->elf, NULL)[EI_CLASS] == ELFCLASS32 ? 4 : 8;
8694 : :
8695 : 82 : Dwarf_Lines *lines;
8696 : 82 : size_t nlines;
8697 : 82 : Dwarf_Off off, next_off = 0;
8698 : 82 : Dwarf_CU *cu = NULL;
8699 : 82 : while (dwarf_next_lines (dbg, off = next_off, &next_off, &cu, NULL, NULL,
8700 [ + + ]: 176 : &lines, &nlines) == 0)
8701 : : {
8702 : 94 : Dwarf_Die cudie;
8703 [ + + + - ]: 94 : if (cu != NULL && dwarf_cu_info (cu, NULL, NULL, &cudie,
8704 : : NULL, NULL, NULL, NULL) == 0)
8705 : 50 : printf (" CU [%" PRIx64 "] %s\n",
8706 : : dwarf_dieoffset (&cudie), dwarf_diename (&cudie));
8707 : : else
8708 : : {
8709 : : /* DWARF5 lines can be independent of any CU, but they probably
8710 : : are used by some CU. Determine the CU this block is for. */
8711 : 44 : Dwarf_Off cuoffset;
8712 : 44 : Dwarf_Off ncuoffset = 0;
8713 : 44 : size_t hsize;
8714 : 44 : while (dwarf_nextcu (dbg, cuoffset = ncuoffset, &ncuoffset, &hsize,
8715 [ + - ]: 46 : NULL, NULL, NULL) == 0)
8716 : : {
8717 [ - + ]: 46 : if (dwarf_offdie (dbg, cuoffset + hsize, &cudie) == NULL)
8718 : 0 : continue;
8719 : 46 : Dwarf_Attribute stmt_list;
8720 [ - + ]: 46 : if (dwarf_attr (&cudie, DW_AT_stmt_list, &stmt_list) == NULL)
8721 : 0 : continue;
8722 : 46 : Dwarf_Word lineoff;
8723 [ - + ]: 46 : if (dwarf_formudata (&stmt_list, &lineoff) != 0)
8724 : 0 : continue;
8725 [ + + ]: 46 : if (lineoff == off)
8726 : : {
8727 : : /* Found the CU. */
8728 : 44 : cu = cudie.cu;
8729 : 44 : break;
8730 : : }
8731 : : }
8732 : :
8733 [ + - ]: 44 : if (cu != NULL)
8734 : 44 : printf (" CU [%" PRIx64 "] %s\n",
8735 : : dwarf_dieoffset (&cudie), dwarf_diename (&cudie));
8736 : : else
8737 : 44 : printf (" No CU\n");
8738 : : }
8739 : :
8740 : 94 : printf (" line:col SBPE* disc isa op address"
8741 : : " (Statement Block Prologue Epilogue *End)\n");
8742 : 94 : const char *last_file = "";
8743 [ + + ]: 720 : for (size_t n = 0; n < nlines; n++)
8744 : : {
8745 : 626 : Dwarf_Line *line = dwarf_onesrcline (lines, n);
8746 [ - + ]: 626 : if (line == NULL)
8747 : : {
8748 : 0 : printf (" dwarf_onesrcline: %s\n", dwarf_errmsg (-1));
8749 : 0 : continue;
8750 : : }
8751 : 626 : Dwarf_Word mtime, length;
8752 : 626 : const char *file = dwarf_linesrc (line, &mtime, &length);
8753 [ - + ]: 626 : if (file == NULL)
8754 : : {
8755 : 0 : printf (" <%s> (mtime: ?, length: ?)\n", dwarf_errmsg (-1));
8756 : 0 : last_file = "";
8757 : : }
8758 [ + + ]: 626 : else if (strcmp (last_file, file) != 0)
8759 : : {
8760 : 114 : printf (" %s (mtime: %" PRIu64 ", length: %" PRIu64 ")\n",
8761 : : file, mtime, length);
8762 : 114 : last_file = file;
8763 : : }
8764 : :
8765 : 626 : int lineno, colno;
8766 : 626 : bool statement, endseq, block, prologue_end, epilogue_begin;
8767 : 626 : unsigned int lineop, isa, disc;
8768 : 626 : Dwarf_Addr address;
8769 : 626 : dwarf_lineaddr (line, &address);
8770 : 626 : dwarf_lineno (line, &lineno);
8771 : 626 : dwarf_linecol (line, &colno);
8772 : 626 : dwarf_lineop_index (line, &lineop);
8773 : 626 : dwarf_linebeginstatement (line, &statement);
8774 : 626 : dwarf_lineendsequence (line, &endseq);
8775 : 626 : dwarf_lineblock (line, &block);
8776 : 626 : dwarf_lineprologueend (line, &prologue_end);
8777 : 626 : dwarf_lineepiloguebegin (line, &epilogue_begin);
8778 : 626 : dwarf_lineisa (line, &isa);
8779 : 626 : dwarf_linediscriminator (line, &disc);
8780 : :
8781 : : /* End sequence is special, it is one byte past. */
8782 : 626 : printf (" %4d:%-3d %c%c%c%c%c %4d %3d %2d ",
8783 : : lineno, colno,
8784 [ + + ]: 626 : (statement ? 'S' : ' '),
8785 [ + - ]: 626 : (block ? 'B' : ' '),
8786 [ + + ]: 626 : (prologue_end ? 'P' : ' '),
8787 [ + - ]: 626 : (epilogue_begin ? 'E' : ' '),
8788 [ + + ]: 626 : (endseq ? '*' : ' '),
8789 : : disc, isa, lineop);
8790 : 626 : print_dwarf_addr (dwflmod, address_size,
8791 [ + + ]: 626 : address - (endseq ? 1 : 0), address);
8792 : 626 : printf ("\n");
8793 : :
8794 [ + + ]: 626 : if (endseq)
8795 : 626 : printf("\n");
8796 : : }
8797 : : }
8798 : 82 : }
8799 : :
8800 : :
8801 : : /* Print the value of a form.
8802 : : Returns new value of readp, or readendp on failure. */
8803 : : static const unsigned char *
8804 : 143582 : print_form_data (Dwarf *dbg, int form, const unsigned char *readp,
8805 : : const unsigned char *readendp, unsigned int offset_len,
8806 : : Dwarf_Off str_offsets_base)
8807 : : {
8808 : 143582 : Dwarf_Word val;
8809 : 143582 : unsigned char *endp;
8810 : 143582 : Elf_Data *data;
8811 : 143582 : char *str;
8812 [ + - - - : 143582 : switch (form)
- + - - -
- + - - +
- + - - -
- - ]
8813 : : {
8814 : 16 : case DW_FORM_data1:
8815 [ - + ]: 16 : if (readendp - readp < 1)
8816 : : {
8817 : 0 : invalid_data:
8818 : 0 : error (0, 0, "invalid data");
8819 : 0 : return readendp;
8820 : : }
8821 : 16 : val = *readp++;
8822 : 16 : printf (" %" PRIx8, (unsigned int) val);
8823 : : break;
8824 : :
8825 : 0 : case DW_FORM_data2:
8826 [ # # ]: 0 : if (readendp - readp < 2)
8827 : 0 : goto invalid_data;
8828 [ # # ]: 0 : val = read_2ubyte_unaligned_inc (dbg, readp);
8829 : 0 : printf(" %" PRIx16, (unsigned int) val);
8830 : : break;
8831 : :
8832 : 0 : case DW_FORM_data4:
8833 [ # # ]: 0 : if (readendp - readp < 4)
8834 : 0 : goto invalid_data;
8835 [ # # ]: 0 : val = read_4ubyte_unaligned_inc (dbg, readp);
8836 : 0 : printf (" %" PRIx32, (unsigned int) val);
8837 : : break;
8838 : :
8839 : 0 : case DW_FORM_data8:
8840 [ # # ]: 0 : if (readendp - readp < 8)
8841 : 0 : goto invalid_data;
8842 [ # # ]: 0 : val = read_8ubyte_unaligned_inc (dbg, readp);
8843 : 0 : printf (" %" PRIx64, val);
8844 : : break;
8845 : :
8846 : 0 : case DW_FORM_sdata:
8847 [ # # ]: 0 : if (readendp - readp < 1)
8848 : 0 : goto invalid_data;
8849 : 0 : get_sleb128 (val, readp, readendp);
8850 : 0 : printf (" %" PRIx64, val);
8851 : : break;
8852 : :
8853 : 57688 : case DW_FORM_udata:
8854 [ - + ]: 57688 : if (readendp - readp < 1)
8855 : 0 : goto invalid_data;
8856 : 57688 : get_uleb128 (val, readp, readendp);
8857 : 57688 : printf (" %" PRIx64, val);
8858 : : break;
8859 : :
8860 : 0 : case DW_FORM_block:
8861 [ # # ]: 0 : if (readendp - readp < 1)
8862 : 0 : goto invalid_data;
8863 : 0 : get_uleb128 (val, readp, readendp);
8864 [ # # ]: 0 : if ((size_t) (readendp - readp) < val)
8865 : 0 : goto invalid_data;
8866 : 0 : print_bytes (val, readp);
8867 : 0 : readp += val;
8868 : 0 : break;
8869 : :
8870 : 0 : case DW_FORM_block1:
8871 [ # # ]: 0 : if (readendp - readp < 1)
8872 : 0 : goto invalid_data;
8873 : 0 : val = *readp++;
8874 [ # # ]: 0 : if ((size_t) (readendp - readp) < val)
8875 : 0 : goto invalid_data;
8876 : 0 : print_bytes (val, readp);
8877 : 0 : readp += val;
8878 : 0 : break;
8879 : :
8880 : 0 : case DW_FORM_block2:
8881 [ # # ]: 0 : if (readendp - readp < 2)
8882 : 0 : goto invalid_data;
8883 [ # # ]: 0 : val = read_2ubyte_unaligned_inc (dbg, readp);
8884 [ # # ]: 0 : if ((size_t) (readendp - readp) < val)
8885 : 0 : goto invalid_data;
8886 : 0 : print_bytes (val, readp);
8887 : 0 : readp += val;
8888 : 0 : break;
8889 : :
8890 : 0 : case DW_FORM_block4:
8891 [ # # ]: 0 : if (readendp - readp < 4)
8892 : 0 : goto invalid_data;
8893 [ # # ]: 0 : val = read_4ubyte_unaligned_inc (dbg, readp);
8894 [ # # ]: 0 : if ((size_t) (readendp - readp) < val)
8895 : 0 : goto invalid_data;
8896 : 0 : print_bytes (val, readp);
8897 : 0 : readp += val;
8898 : 0 : break;
8899 : :
8900 : 24 : case DW_FORM_data16:
8901 [ - + ]: 24 : if (readendp - readp < 16)
8902 : 0 : goto invalid_data;
8903 : 24 : print_bytes (16, readp);
8904 : 24 : readp += 16;
8905 : 24 : break;
8906 : :
8907 : 0 : case DW_FORM_flag:
8908 [ # # ]: 0 : if (readendp - readp < 1)
8909 : 0 : goto invalid_data;
8910 : 0 : val = *readp++;
8911 [ # # ]: 0 : printf ("%s", val != 0 ? yes_str : no_str);
8912 : : break;
8913 : :
8914 : 0 : case DW_FORM_string:
8915 : 0 : endp = memchr (readp, '\0', readendp - readp);
8916 [ # # ]: 0 : if (endp == NULL)
8917 : 0 : goto invalid_data;
8918 : 0 : printf ("%s", readp);
8919 : 0 : readp = endp + 1;
8920 : 0 : break;
8921 : :
8922 : 84298 : case DW_FORM_strp:
8923 : : case DW_FORM_line_strp:
8924 : : case DW_FORM_strp_sup:
8925 [ - + ]: 84298 : if ((size_t) (readendp - readp) < offset_len)
8926 : 0 : goto invalid_data;
8927 [ - + ]: 84298 : if (offset_len == 8)
8928 [ # # ]: 0 : val = read_8ubyte_unaligned_inc (dbg, readp);
8929 : : else
8930 [ - + ]: 84298 : val = read_4ubyte_unaligned_inc (dbg, readp);
8931 [ - + ]: 84298 : if (form == DW_FORM_strp)
8932 : 0 : data = dbg->sectiondata[IDX_debug_str];
8933 [ + - ]: 84298 : else if (form == DW_FORM_line_strp)
8934 : 84298 : data = dbg->sectiondata[IDX_debug_line_str];
8935 : : else /* form == DW_FORM_strp_sup */
8936 : : {
8937 : 0 : Dwarf *alt = dwarf_getalt (dbg);
8938 [ # # ]: 0 : data = alt != NULL ? alt->sectiondata[IDX_debug_str] : NULL;
8939 : : }
8940 [ + - - + ]: 84298 : if (data == NULL || val >= data->d_size
8941 [ - + ]: 84298 : || memchr (data->d_buf + val, '\0', data->d_size - val) == NULL)
8942 : : str = "???";
8943 : : else
8944 : 84298 : str = (char *) data->d_buf + val;
8945 : 84298 : printf ("%s (%" PRIu64 ")", str, val);
8946 : : break;
8947 : :
8948 : 0 : case DW_FORM_sec_offset:
8949 [ # # ]: 0 : if ((size_t) (readendp - readp) < offset_len)
8950 : 0 : goto invalid_data;
8951 [ # # ]: 0 : if (offset_len == 8)
8952 [ # # ]: 0 : val = read_8ubyte_unaligned_inc (dbg, readp);
8953 : : else
8954 [ # # ]: 0 : val = read_4ubyte_unaligned_inc (dbg, readp);
8955 : 0 : printf ("[%" PRIx64 "]", val);
8956 : : break;
8957 : :
8958 : 1556 : case DW_FORM_strx:
8959 : : case DW_FORM_GNU_str_index:
8960 [ - + ]: 1556 : if (readendp - readp < 1)
8961 : 0 : goto invalid_data;
8962 : 1556 : get_uleb128 (val, readp, readendp);
8963 : 1556 : strx_val:
8964 : 1556 : data = dbg->sectiondata[IDX_debug_str_offsets];
8965 [ + - ]: 1556 : if (data == NULL
8966 [ - + ]: 1556 : || data->d_size - str_offsets_base < val * offset_len)
8967 : : str = "???";
8968 : : else
8969 : : {
8970 : 1556 : const unsigned char *strreadp = (data->d_buf + str_offsets_base
8971 : 1556 : + val * offset_len);
8972 : 1556 : const unsigned char *strreadendp = data->d_buf + data->d_size;
8973 [ - + ]: 1556 : if ((size_t) (strreadendp - strreadp) < offset_len)
8974 : : str = "???";
8975 : : else
8976 : : {
8977 : 1556 : Dwarf_Off idx;
8978 [ - + ]: 1556 : if (offset_len == 8)
8979 [ # # ]: 0 : idx = read_8ubyte_unaligned (dbg, strreadp);
8980 : : else
8981 [ - + ]: 1556 : idx = read_4ubyte_unaligned (dbg, strreadp);
8982 : :
8983 : 1556 : data = dbg->sectiondata[IDX_debug_str];
8984 [ - + - + ]: 1556 : if (data == NULL || idx >= data->d_size
8985 [ - + ]: 1556 : || memchr (data->d_buf + idx, '\0',
8986 : : data->d_size - idx) == NULL)
8987 : : str = "???";
8988 : : else
8989 : 1556 : str = (char *) data->d_buf + idx;
8990 : : }
8991 : : }
8992 : 1556 : printf ("%s (%" PRIu64 ")", str, val);
8993 : : break;
8994 : :
8995 : 0 : case DW_FORM_strx1:
8996 [ # # ]: 0 : if (readendp - readp < 1)
8997 : 0 : goto invalid_data;
8998 : 0 : val = *readp++;
8999 : 0 : goto strx_val;
9000 : :
9001 : 0 : case DW_FORM_strx2:
9002 [ # # ]: 0 : if (readendp - readp < 2)
9003 : 0 : goto invalid_data;
9004 [ # # ]: 0 : val = read_2ubyte_unaligned_inc (dbg, readp);
9005 : 0 : goto strx_val;
9006 : :
9007 : 0 : case DW_FORM_strx3:
9008 [ # # ]: 0 : if (readendp - readp < 3)
9009 : 0 : goto invalid_data;
9010 : 0 : val = read_3ubyte_unaligned_inc (dbg, readp);
9011 : 0 : goto strx_val;
9012 : :
9013 : 0 : case DW_FORM_strx4:
9014 [ # # ]: 0 : if (readendp - readp < 4)
9015 : 0 : goto invalid_data;
9016 [ # # ]: 0 : val = read_4ubyte_unaligned_inc (dbg, readp);
9017 : 0 : goto strx_val;
9018 : :
9019 : 0 : default:
9020 : 0 : error (0, 0, _("unknown form: %s"), dwarf_form_name (form));
9021 : 0 : return readendp;
9022 : : }
9023 : :
9024 : 143582 : return readp;
9025 : : }
9026 : :
9027 : : /* Only used via run_advance_pc() macro */
9028 : : static inline void
9029 : 763870 : run_advance_pc (unsigned int op_advance,
9030 : : unsigned int minimum_instr_len,
9031 : : unsigned int max_ops_per_instr,
9032 : : unsigned int *op_addr_advance,
9033 : : Dwarf_Word *address,
9034 : : unsigned int *op_index)
9035 : : {
9036 : 763870 : const unsigned int advanced_op_index = (*op_index) + op_advance;
9037 : :
9038 : 763870 : *op_addr_advance = minimum_instr_len * (advanced_op_index
9039 : 763870 : / max_ops_per_instr);
9040 : 763870 : *address = *address + *op_addr_advance;
9041 : 763870 : *op_index = advanced_op_index % max_ops_per_instr;
9042 : : }
9043 : :
9044 : : static void
9045 : 186 : print_debug_line_section (Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr,
9046 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
9047 : : {
9048 [ + + ]: 186 : if (decodedline)
9049 : : {
9050 : 82 : print_decoded_line_section (dwflmod, ebl, ehdr, scn, shdr, dbg);
9051 : 164 : return;
9052 : : }
9053 : :
9054 : 104 : Elf_Data *data = get_debug_elf_data (dbg, ebl, IDX_debug_line, scn);
9055 [ + - ]: 104 : if (data == NULL)
9056 : : return;
9057 : :
9058 : 104 : printf (_("\
9059 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
9060 : : elf_ndxscn (scn), section_name (ebl, shdr),
9061 : 104 : (uint64_t) shdr->sh_offset);
9062 : :
9063 [ + - ]: 104 : if (shdr->sh_size == 0)
9064 : : return;
9065 : :
9066 : : /* There is no functionality in libdw to read the information in the
9067 : : way it is represented here. Hardcode the decoder. */
9068 : :
9069 : 104 : const unsigned char *linep = (const unsigned char *) data->d_buf;
9070 : 104 : const unsigned char *lineendp;
9071 : :
9072 : 104 : while (linep
9073 [ + + ]: 3722 : < (lineendp = (const unsigned char *) data->d_buf + data->d_size))
9074 : : {
9075 : 3618 : size_t start_offset = linep - (const unsigned char *) data->d_buf;
9076 : :
9077 : 3618 : printf (_("\nTable at offset %zu:\n"), start_offset);
9078 : :
9079 [ - + ]: 3618 : if (unlikely (linep + 4 > lineendp))
9080 : 0 : goto invalid_data;
9081 [ + + ]: 3618 : Dwarf_Word unit_length = read_4ubyte_unaligned_inc (dbg, linep);
9082 : 3618 : unsigned int length = 4;
9083 [ - + ]: 3618 : if (unlikely (unit_length == 0xffffffff))
9084 : : {
9085 [ # # ]: 0 : if (unlikely (linep + 8 > lineendp))
9086 : : {
9087 : 0 : invalid_data:
9088 : 0 : error (0, 0, _("invalid data in section [%zu] '%s'"),
9089 : : elf_ndxscn (scn), section_name (ebl, shdr));
9090 : 0 : return;
9091 : : }
9092 [ # # ]: 0 : unit_length = read_8ubyte_unaligned_inc (dbg, linep);
9093 : 0 : length = 8;
9094 : : }
9095 : :
9096 : : /* Check whether we have enough room in the section. */
9097 [ - + ]: 3618 : if (unlikely (unit_length > (size_t) (lineendp - linep)))
9098 : 0 : goto invalid_data;
9099 : 3618 : lineendp = linep + unit_length;
9100 : :
9101 : : /* The next element of the header is the version identifier. */
9102 [ - + ]: 3618 : if ((size_t) (lineendp - linep) < 2)
9103 : 0 : goto invalid_data;
9104 [ + + ]: 3618 : uint_fast16_t version = read_2ubyte_unaligned_inc (dbg, linep);
9105 : :
9106 : 7236 : size_t address_size
9107 [ + + ]: 3618 : = elf_getident (ebl->elf, NULL)[EI_CLASS] == ELFCLASS32 ? 4 : 8;
9108 : 3618 : unsigned char segment_selector_size = 0;
9109 [ + + ]: 3618 : if (version > 4)
9110 : : {
9111 [ - + ]: 3466 : if ((size_t) (lineendp - linep) < 2)
9112 : 0 : goto invalid_data;
9113 : 3466 : address_size = *linep++;
9114 : 3466 : segment_selector_size = *linep++;
9115 : : }
9116 : :
9117 : : /* Next comes the header length. */
9118 : 3618 : Dwarf_Word header_length;
9119 [ + - ]: 3618 : if (length == 4)
9120 : : {
9121 [ - + ]: 3618 : if ((size_t) (lineendp - linep) < 4)
9122 : 0 : goto invalid_data;
9123 [ + + ]: 3618 : header_length = read_4ubyte_unaligned_inc (dbg, linep);
9124 : : }
9125 : : else
9126 : : {
9127 [ # # ]: 0 : if ((size_t) (lineendp - linep) < 8)
9128 : 0 : goto invalid_data;
9129 [ # # ]: 0 : header_length = read_8ubyte_unaligned_inc (dbg, linep);
9130 : : }
9131 : :
9132 : 3618 : const unsigned char *header_start = linep;
9133 : :
9134 : : /* Next the minimum instruction length. */
9135 [ - + ]: 3618 : if ((size_t) (lineendp - linep) < 1)
9136 : 0 : goto invalid_data;
9137 : 3618 : uint_fast8_t minimum_instr_len = *linep++;
9138 : :
9139 : : /* Next the maximum operations per instruction, in version 4 format. */
9140 : 3618 : uint_fast8_t max_ops_per_instr;
9141 [ + + ]: 3618 : if (version < 4)
9142 : : max_ops_per_instr = 1;
9143 : : else
9144 : : {
9145 [ - + ]: 3486 : if ((size_t) (lineendp - linep) < 1)
9146 : 0 : goto invalid_data;
9147 : 3486 : max_ops_per_instr = *linep++;
9148 : : }
9149 : :
9150 : : /* We need at least 4 more bytes. */
9151 [ - + ]: 3618 : if ((size_t) (lineendp - linep) < 4)
9152 : 0 : goto invalid_data;
9153 : :
9154 : : /* Then the flag determining the default value of the is_stmt
9155 : : register. */
9156 : 3618 : uint_fast8_t default_is_stmt = *linep++;
9157 : :
9158 : : /* Now the line base. */
9159 : 3618 : int_fast8_t line_base = *linep++;
9160 : :
9161 : : /* And the line range. */
9162 : 3618 : uint_fast8_t line_range = *linep++;
9163 : :
9164 : : /* The opcode base. */
9165 : 3618 : uint_fast8_t opcode_base = *linep++;
9166 : :
9167 : : /* Print what we got so far. */
9168 : 3618 : printf (_("\n"
9169 : : " Length: %" PRIu64 "\n"
9170 : : " DWARF version: %" PRIuFAST16 "\n"
9171 : : " Prologue length: %" PRIu64 "\n"
9172 : : " Address size: %zd\n"
9173 : : " Segment selector size: %zd\n"
9174 : : " Min instruction length: %" PRIuFAST8 "\n"
9175 : : " Max operations per instruction: %" PRIuFAST8 "\n"
9176 : : " Initial value if 'is_stmt': %" PRIuFAST8 "\n"
9177 : : " Line base: %" PRIdFAST8 "\n"
9178 : : " Line range: %" PRIuFAST8 "\n"
9179 : : " Opcode base: %" PRIuFAST8 "\n"
9180 : : "\n"
9181 : : "Opcodes:\n"),
9182 : : (uint64_t) unit_length, version, (uint64_t) header_length,
9183 : : address_size, (size_t) segment_selector_size,
9184 : : minimum_instr_len, max_ops_per_instr,
9185 : : default_is_stmt, line_base,
9186 : : line_range, opcode_base);
9187 : :
9188 [ - + ]: 3618 : if (version < 2 || version > 5)
9189 : : {
9190 : 0 : error (0, 0, _("cannot handle .debug_line version: %u\n"),
9191 : : (unsigned int) version);
9192 : 0 : linep = lineendp;
9193 : 0 : continue;
9194 : : }
9195 : :
9196 [ - + ]: 3618 : if (address_size != 4 && address_size != 8)
9197 : : {
9198 : 0 : error (0, 0, _("cannot handle address size: %u\n"),
9199 : : (unsigned int) address_size);
9200 : 0 : linep = lineendp;
9201 : 0 : continue;
9202 : : }
9203 : :
9204 [ - + ]: 3618 : if (segment_selector_size != 0)
9205 : : {
9206 : 0 : error (0, 0, _("cannot handle segment selector size: %u\n"),
9207 : : (unsigned int) segment_selector_size);
9208 : 0 : linep = lineendp;
9209 : 0 : continue;
9210 : : }
9211 : :
9212 [ - + ]: 3618 : if (unlikely (linep + opcode_base - 1 >= lineendp))
9213 : : {
9214 : 0 : invalid_unit:
9215 : 0 : error (0, 0,
9216 : 0 : _("invalid data at offset %tu in section [%zu] '%s'"),
9217 : 0 : linep - (const unsigned char *) data->d_buf,
9218 : : elf_ndxscn (scn), section_name (ebl, shdr));
9219 : 0 : linep = lineendp;
9220 : 0 : continue;
9221 : : }
9222 : 3618 : int opcode_base_l10 = 1;
9223 : 3618 : unsigned int tmp = opcode_base;
9224 [ + + ]: 7230 : while (tmp > 10)
9225 : : {
9226 : 3612 : tmp /= 10;
9227 : 3612 : ++opcode_base_l10;
9228 : : }
9229 : : const uint8_t *standard_opcode_lengths = linep - 1;
9230 [ + + ]: 47016 : for (uint_fast8_t cnt = 1; cnt < opcode_base; ++cnt)
9231 : 43398 : printf (ngettext (" [%*" PRIuFAST8 "] %hhu argument\n",
9232 : : " [%*" PRIuFAST8 "] %hhu arguments\n",
9233 : : (int) linep[cnt - 1]),
9234 : 43398 : opcode_base_l10, cnt, linep[cnt - 1]);
9235 : 3618 : linep += opcode_base - 1;
9236 : :
9237 : 3618 : if (unlikely (linep >= lineendp))
9238 : : goto invalid_unit;
9239 : :
9240 : 3618 : Dwarf_Off str_offsets_base = str_offsets_base_off (dbg, NULL);
9241 : :
9242 : 3618 : puts (_("\nDirectory table:"));
9243 [ + + ]: 3618 : if (version > 4)
9244 : : {
9245 : 3466 : struct encpair { uint16_t desc; uint16_t form; };
9246 : 3466 : struct encpair enc[256];
9247 : :
9248 : 3466 : printf (_(" ["));
9249 [ - + ]: 3466 : if ((size_t) (lineendp - linep) < 1)
9250 : 0 : goto invalid_data;
9251 : 3466 : unsigned char directory_entry_format_count = *linep++;
9252 : 3466 : for (int i = 0; i < directory_entry_format_count; i++)
9253 : : {
9254 : 3466 : uint16_t desc, form;
9255 [ - + ]: 3466 : if ((size_t) (lineendp - linep) < 1)
9256 : 0 : goto invalid_data;
9257 : 3466 : get_uleb128 (desc, linep, lineendp);
9258 [ - + ]: 3466 : if ((size_t) (lineendp - linep) < 1)
9259 : 0 : goto invalid_data;
9260 : 3466 : get_uleb128 (form, linep, lineendp);
9261 : :
9262 : 3466 : enc[i].desc = desc;
9263 : 3466 : enc[i].form = form;
9264 : :
9265 : 3466 : printf ("%s(%s)",
9266 : : dwarf_line_content_description_name (desc),
9267 : : dwarf_form_name (form));
9268 [ - + ]: 3466 : if (i + 1 < directory_entry_format_count)
9269 [ + + ]: 6932 : printf (", ");
9270 : : }
9271 : 3466 : printf ("]\n");
9272 : :
9273 : 3466 : uint64_t directories_count;
9274 [ - + ]: 3466 : if ((size_t) (lineendp - linep) < 1)
9275 : 0 : goto invalid_data;
9276 : 3466 : get_uleb128 (directories_count, linep, lineendp);
9277 : :
9278 : 3466 : if (directory_entry_format_count == 0
9279 [ - + ]: 3466 : && directories_count != 0)
9280 : 0 : goto invalid_data;
9281 : :
9282 [ + + ]: 30060 : for (uint64_t i = 0; i < directories_count; i++)
9283 : : {
9284 : 26594 : printf (" %-5" PRIu64 " ", i);
9285 : 26594 : for (int j = 0; j < directory_entry_format_count; j++)
9286 : : {
9287 : 26594 : linep = print_form_data (dbg, enc[j].form,
9288 : : linep, lineendp, length,
9289 : : str_offsets_base);
9290 [ - + ]: 26594 : if (j + 1 < directory_entry_format_count)
9291 [ + + ]: 53188 : printf (", ");
9292 : : }
9293 : 26594 : printf ("\n");
9294 [ - + ]: 26594 : if (linep >= lineendp)
9295 : 0 : goto invalid_unit;
9296 : : }
9297 : : }
9298 : : else
9299 : : {
9300 [ + - + + ]: 766 : while (linep < lineendp && *linep != 0)
9301 : : {
9302 : 614 : unsigned char *endp = memchr (linep, '\0', lineendp - linep);
9303 [ - + ]: 614 : if (unlikely (endp == NULL))
9304 : 0 : goto invalid_unit;
9305 : :
9306 : 614 : printf (" %s\n", (char *) linep);
9307 : :
9308 : 614 : linep = endp + 1;
9309 : : }
9310 [ + - - + ]: 152 : if (linep >= lineendp || *linep != 0)
9311 : 0 : goto invalid_unit;
9312 : : /* Skip the final NUL byte. */
9313 : 152 : ++linep;
9314 : : }
9315 : :
9316 [ - + ]: 3618 : if (unlikely (linep >= lineendp))
9317 : 0 : goto invalid_unit;
9318 : :
9319 : 3618 : puts (_("\nFile name table:"));
9320 [ + + ]: 3618 : if (version > 4)
9321 : : {
9322 : 3466 : struct encpair { uint16_t desc; uint16_t form; };
9323 : 3466 : struct encpair enc[256];
9324 : :
9325 : 3466 : printf (_(" ["));
9326 [ - + ]: 3466 : if ((size_t) (lineendp - linep) < 1)
9327 : 0 : goto invalid_data;
9328 : 3466 : unsigned char file_name_format_count = *linep++;
9329 : 3466 : for (int i = 0; i < file_name_format_count; i++)
9330 : : {
9331 : 6940 : uint64_t desc, form;
9332 [ - + ]: 6940 : if ((size_t) (lineendp - linep) < 1)
9333 : 0 : goto invalid_data;
9334 : 6940 : get_uleb128 (desc, linep, lineendp);
9335 [ - + ]: 6940 : if ((size_t) (lineendp - linep) < 1)
9336 : 0 : goto invalid_data;
9337 : 6940 : get_uleb128 (form, linep, lineendp);
9338 : :
9339 [ - + ]: 6940 : if (! libdw_valid_user_form (form))
9340 : 0 : goto invalid_data;
9341 : :
9342 : 6940 : enc[i].desc = desc;
9343 : 6940 : enc[i].form = form;
9344 : :
9345 : 6940 : printf ("%s(%s)",
9346 : : dwarf_line_content_description_name (desc),
9347 : : dwarf_form_name (form));
9348 [ + + ]: 6940 : if (i + 1 < file_name_format_count)
9349 [ + + ]: 13880 : printf (", ");
9350 : : }
9351 : 3466 : printf ("]\n");
9352 : :
9353 : 3466 : uint64_t file_name_count;
9354 [ - + ]: 3466 : if ((size_t) (lineendp - linep) < 1)
9355 : 0 : goto invalid_data;
9356 : 3466 : get_uleb128 (file_name_count, linep, lineendp);
9357 : :
9358 : 3466 : if (file_name_format_count == 0
9359 [ - + ]: 3466 : && file_name_count != 0)
9360 : 0 : goto invalid_data;
9361 : :
9362 [ + + ]: 61170 : for (uint64_t i = 0; i < file_name_count; i++)
9363 : : {
9364 : 57704 : printf (" %-5" PRIu64 " ", i);
9365 : 57704 : for (int j = 0; j < file_name_format_count; j++)
9366 : : {
9367 : 115432 : linep = print_form_data (dbg, enc[j].form,
9368 : : linep, lineendp, length,
9369 : : str_offsets_base);
9370 [ + + ]: 115432 : if (j + 1 < file_name_format_count)
9371 [ + + ]: 230864 : printf (", ");
9372 : : }
9373 : 57704 : printf ("\n");
9374 [ - + ]: 57704 : if (linep > lineendp)
9375 : 0 : goto invalid_unit;
9376 : : }
9377 : : }
9378 : : else
9379 : : {
9380 : 152 : puts (_(" Entry Dir Time Size Name"));
9381 [ + - + + ]: 6154 : for (unsigned int cnt = 1; linep < lineendp && *linep != 0; ++cnt)
9382 : : {
9383 : : /* First comes the file name. */
9384 : 6002 : char *fname = (char *) linep;
9385 : 6002 : unsigned char *endp = memchr (fname, '\0', lineendp - linep);
9386 [ - + ]: 6002 : if (unlikely (endp == NULL))
9387 : 0 : goto invalid_unit;
9388 : 6002 : linep = endp + 1;
9389 : :
9390 : : /* Then the index. */
9391 : 6002 : unsigned int diridx;
9392 [ - + ]: 6002 : if (lineendp - linep < 1)
9393 : 0 : goto invalid_unit;
9394 : 6002 : get_uleb128 (diridx, linep, lineendp);
9395 : :
9396 : : /* Next comes the modification time. */
9397 : 6002 : unsigned int mtime;
9398 [ - + ]: 6002 : if (lineendp - linep < 1)
9399 : 0 : goto invalid_unit;
9400 : 6002 : get_uleb128 (mtime, linep, lineendp);
9401 : :
9402 : : /* Finally the length of the file. */
9403 : 6002 : unsigned int fsize;
9404 [ - + ]: 6002 : if (lineendp - linep < 1)
9405 : 0 : goto invalid_unit;
9406 : 6002 : get_uleb128 (fsize, linep, lineendp);
9407 : :
9408 : 6002 : printf (" %-5u %-5u %-9u %-9u %s\n",
9409 : : cnt, diridx, mtime, fsize, fname);
9410 : : }
9411 [ + - - + ]: 152 : if (linep >= lineendp || *linep != '\0')
9412 : 0 : goto invalid_unit;
9413 : : /* Skip the final NUL byte. */
9414 : 152 : ++linep;
9415 : : }
9416 : :
9417 : 3618 : unsigned int debug_str_offset = 0;
9418 [ + + ]: 3618 : if (unlikely (linep == header_start + header_length - 4))
9419 : : {
9420 : : /* CUBINs contain an unsigned 4-byte offset */
9421 [ - + ]: 2 : debug_str_offset = read_4ubyte_unaligned_inc (dbg, linep);
9422 : : }
9423 : :
9424 [ + + ]: 3618 : if (linep == lineendp)
9425 : : {
9426 : 40 : puts (_("\nNo line number statements."));
9427 : 40 : continue;
9428 : : }
9429 : :
9430 : 3578 : puts (_("\nLine number statements:"));
9431 : 3578 : Dwarf_Word address = 0;
9432 : 3578 : unsigned int op_index = 0;
9433 : 3578 : size_t line = 1;
9434 : 3578 : uint_fast8_t is_stmt = default_is_stmt;
9435 : :
9436 : : /* Apply the "operation advance" from a special opcode
9437 : : or DW_LNS_advance_pc (as per DWARF4 6.2.5.1). */
9438 : 3578 : unsigned int op_addr_advance;
9439 : : #define advance_pc(op_advance) run_advance_pc(op_advance, minimum_instr_len, \
9440 : : max_ops_per_instr, &op_addr_advance, &address, &op_index)
9441 : :
9442 [ - + ]: 3578 : if (max_ops_per_instr == 0)
9443 : : {
9444 : 0 : error (0, 0,
9445 : 0 : _("invalid maximum operations per instruction is zero"));
9446 : 0 : linep = lineendp;
9447 : 0 : continue;
9448 : : }
9449 : :
9450 : 2402654 : while (linep < lineendp)
9451 : : {
9452 : 2399076 : size_t offset = linep - (const unsigned char *) data->d_buf;
9453 : 2399076 : unsigned int u128;
9454 : 2399076 : int s128;
9455 : :
9456 : : /* Read the opcode. */
9457 : 2399076 : unsigned int opcode = *linep++;
9458 : :
9459 : 2399076 : printf (" [%6" PRIx64 "]", (uint64_t)offset);
9460 : : /* Is this a special opcode? */
9461 [ + + ]: 2399076 : if (likely (opcode >= opcode_base))
9462 : : {
9463 [ - + ]: 694606 : if (unlikely (line_range == 0))
9464 : 0 : goto invalid_unit;
9465 : :
9466 : : /* Yes. Handling this is quite easy since the opcode value
9467 : : is computed with
9468 : :
9469 : : opcode = (desired line increment - line_base)
9470 : : + (line_range * address advance) + opcode_base
9471 : : */
9472 : 694606 : int line_increment = (line_base
9473 : 694606 : + (opcode - opcode_base) % line_range);
9474 : :
9475 : : /* Perform the increments. */
9476 : 694606 : line += line_increment;
9477 : 694606 : advance_pc ((opcode - opcode_base) / line_range);
9478 : :
9479 : 694606 : printf (_(" special opcode %u: address+%u = "),
9480 : : opcode, op_addr_advance);
9481 : 694606 : print_dwarf_addr (dwflmod, 0, address, address);
9482 [ - + ]: 694606 : if (op_index > 0)
9483 : 0 : printf (_(", op_index = %u, line%+d = %zu\n"),
9484 : : op_index, line_increment, line);
9485 : : else
9486 : 694606 : printf (_(", line%+d = %zu\n"),
9487 : : line_increment, line);
9488 : : }
9489 [ + + ]: 1704470 : else if (opcode == 0)
9490 : : {
9491 : : /* This an extended opcode. */
9492 [ - + ]: 129910 : if (unlikely (linep + 2 > lineendp))
9493 : 0 : goto invalid_unit;
9494 : :
9495 : : /* The length. */
9496 : 129910 : unsigned int len = *linep++;
9497 : :
9498 [ - + ]: 129910 : if (unlikely (linep + len > lineendp))
9499 : 0 : goto invalid_unit;
9500 : :
9501 : : /* The sub-opcode. */
9502 : 129910 : opcode = *linep++;
9503 : :
9504 : 129910 : printf (_(" extended opcode %u: "), opcode);
9505 : :
9506 [ + + - + : 129910 : switch (opcode)
+ - - ]
9507 : : {
9508 : 9980 : case DW_LNE_end_sequence:
9509 : 9980 : puts (_(" end of sequence"));
9510 : :
9511 : : /* Reset the registers we care about. */
9512 : 9980 : address = 0;
9513 : 9980 : op_index = 0;
9514 : 9980 : line = 1;
9515 : 9980 : is_stmt = default_is_stmt;
9516 : 9980 : break;
9517 : :
9518 : 11268 : case DW_LNE_set_address:
9519 : 11268 : op_index = 0;
9520 [ - + ]: 11268 : if (unlikely ((size_t) (lineendp - linep) < address_size))
9521 : 0 : goto invalid_unit;
9522 [ + + ]: 11268 : if (address_size == 4)
9523 [ + + ]: 50 : address = read_4ubyte_unaligned_inc (dbg, linep);
9524 : : else
9525 [ + + ]: 11218 : address = read_8ubyte_unaligned_inc (dbg, linep);
9526 : : {
9527 : 11268 : printf (_(" set address to "));
9528 : 11268 : print_dwarf_addr (dwflmod, 0, address, address);
9529 : 11268 : printf ("\n");
9530 : : }
9531 : : break;
9532 : :
9533 : 0 : case DW_LNE_define_file:
9534 : : {
9535 : 0 : char *fname = (char *) linep;
9536 : 0 : unsigned char *endp = memchr (linep, '\0',
9537 : 0 : lineendp - linep);
9538 [ # # ]: 0 : if (unlikely (endp == NULL))
9539 : 0 : goto invalid_unit;
9540 : 0 : linep = endp + 1;
9541 : :
9542 : 0 : unsigned int diridx;
9543 [ # # ]: 0 : if (lineendp - linep < 1)
9544 : 0 : goto invalid_unit;
9545 : 0 : get_uleb128 (diridx, linep, lineendp);
9546 : 0 : Dwarf_Word mtime;
9547 [ # # ]: 0 : if (lineendp - linep < 1)
9548 : 0 : goto invalid_unit;
9549 : 0 : get_uleb128 (mtime, linep, lineendp);
9550 : 0 : Dwarf_Word filelength;
9551 [ # # ]: 0 : if (lineendp - linep < 1)
9552 : 0 : goto invalid_unit;
9553 : 0 : get_uleb128 (filelength, linep, lineendp);
9554 : :
9555 : 0 : printf (_("\
9556 : : define new file: dir=%u, mtime=%" PRIu64 ", length=%" PRIu64 ", name=%s\n"),
9557 : : diridx, (uint64_t) mtime, (uint64_t) filelength,
9558 : : fname);
9559 : : }
9560 : : break;
9561 : :
9562 : 108648 : case DW_LNE_set_discriminator:
9563 : : /* Takes one ULEB128 parameter, the discriminator. */
9564 [ + - - + ]: 108648 : if (unlikely (standard_opcode_lengths[opcode] != 1
9565 : : || lineendp - linep < 1))
9566 : 0 : goto invalid_unit;
9567 : :
9568 : 108648 : get_uleb128 (u128, linep, lineendp);
9569 : 108648 : printf (_(" set discriminator to %u\n"), u128);
9570 : : break;
9571 : :
9572 : 14 : case DW_LNE_NVIDIA_inlined_call:
9573 : : {
9574 [ - + ]: 14 : if (unlikely (linep >= lineendp))
9575 : 0 : goto invalid_data;
9576 : :
9577 : 14 : unsigned int context;
9578 : 14 : get_uleb128 (context, linep, lineendp);
9579 : :
9580 [ - + ]: 14 : if (unlikely (linep >= lineendp))
9581 : 0 : goto invalid_data;
9582 : :
9583 : 14 : unsigned int function_name;
9584 : 14 : get_uleb128 (function_name, linep, lineendp);
9585 : 14 : function_name += debug_str_offset;
9586 : :
9587 : 14 : Elf_Data *str_data = dbg->sectiondata[IDX_debug_str];
9588 : 14 : char *function_str;
9589 [ + - - + ]: 14 : if (str_data == NULL || function_name >= str_data->d_size
9590 [ - + ]: 14 : || memchr (str_data->d_buf + function_name, '\0',
9591 : : str_data->d_size - function_name) == NULL)
9592 : : function_str = "???";
9593 : : else
9594 : 14 : function_str = (char *) str_data->d_buf + function_name;
9595 : :
9596 : 14 : printf (_(" set inlined context %u,"
9597 : : " function name %s (0x%x)\n"),
9598 : : context, function_str, function_name);
9599 : : break;
9600 : : }
9601 : :
9602 : 0 : case DW_LNE_NVIDIA_set_function_name:
9603 : : {
9604 [ # # ]: 0 : if (unlikely (linep >= lineendp))
9605 : 0 : goto invalid_data;
9606 : :
9607 : 0 : unsigned int function_name;
9608 : 0 : get_uleb128 (function_name, linep, lineendp);
9609 : 0 : function_name += debug_str_offset;
9610 : :
9611 : 0 : Elf_Data *str_data = dbg->sectiondata[IDX_debug_str];
9612 : 0 : char *function_str;
9613 [ # # # # ]: 0 : if (str_data == NULL || function_name >= str_data->d_size
9614 [ # # ]: 0 : || memchr (str_data->d_buf + function_name, '\0',
9615 : : str_data->d_size - function_name) == NULL)
9616 : : function_str = "???";
9617 : : else
9618 : 0 : function_str = (char *) str_data->d_buf + function_name;
9619 : :
9620 : 0 : printf (_(" set function name %s (0x%x)\n"),
9621 : : function_str, function_name);
9622 : : }
9623 : : break;
9624 : :
9625 : 0 : default:
9626 : : /* Unknown, ignore it. */
9627 : 0 : puts (_(" unknown opcode"));
9628 : 0 : linep += len - 1;
9629 : 0 : break;
9630 : : }
9631 : : }
9632 [ + - ]: 1574560 : else if (opcode <= DW_LNS_set_isa)
9633 : : {
9634 : : /* This is a known standard opcode. */
9635 [ + + + + : 1574560 : switch (opcode)
+ + - + +
+ - - ]
9636 : : {
9637 : 240190 : case DW_LNS_copy:
9638 : : /* Takes no argument. */
9639 : 240190 : puts (_(" copy"));
9640 : 240190 : break;
9641 : :
9642 : 15570 : case DW_LNS_advance_pc:
9643 : : /* Takes one uleb128 parameter which is added to the
9644 : : address. */
9645 [ - + ]: 15570 : if (lineendp - linep < 1)
9646 : 0 : goto invalid_unit;
9647 : 15570 : get_uleb128 (u128, linep, lineendp);
9648 : 15570 : advance_pc (u128);
9649 : : {
9650 : 15570 : printf (_(" advance address by %u to "),
9651 : : op_addr_advance);
9652 : 15570 : print_dwarf_addr (dwflmod, 0, address, address);
9653 [ - + ]: 15570 : if (op_index > 0)
9654 : 0 : printf (_(", op_index to %u"), op_index);
9655 : 15570 : printf ("\n");
9656 : : }
9657 : : break;
9658 : :
9659 : 183618 : case DW_LNS_advance_line:
9660 : : /* Takes one sleb128 parameter which is added to the
9661 : : line. */
9662 [ - + ]: 183618 : if (lineendp - linep < 1)
9663 : 0 : goto invalid_unit;
9664 : 183618 : get_sleb128 (s128, linep, lineendp);
9665 : 183618 : line += s128;
9666 : 183618 : printf (_("\
9667 : : advance line by constant %d to %" PRId64 "\n"),
9668 : : s128, (int64_t) line);
9669 : : break;
9670 : :
9671 : 65988 : case DW_LNS_set_file:
9672 : : /* Takes one uleb128 parameter which is stored in file. */
9673 [ - + ]: 65988 : if (lineendp - linep < 1)
9674 : 0 : goto invalid_unit;
9675 : 65988 : get_uleb128 (u128, linep, lineendp);
9676 : 65988 : printf (_(" set file to %" PRIu64 "\n"),
9677 : : (uint64_t) u128);
9678 : : break;
9679 : :
9680 : 597020 : case DW_LNS_set_column:
9681 : : /* Takes one uleb128 parameter which is stored in column. */
9682 [ + - - + ]: 597020 : if (unlikely (standard_opcode_lengths[opcode] != 1
9683 : : || lineendp - linep < 1))
9684 : 0 : goto invalid_unit;
9685 : :
9686 : 597020 : get_uleb128 (u128, linep, lineendp);
9687 : 597020 : printf (_(" set column to %" PRIu64 "\n"),
9688 : : (uint64_t) u128);
9689 : : break;
9690 : :
9691 : 418426 : case DW_LNS_negate_stmt:
9692 : : /* Takes no argument. */
9693 : 418426 : is_stmt = 1 - is_stmt;
9694 : 418426 : printf (_(" set '%s' to %" PRIuFAST8 "\n"),
9695 : : "is_stmt", is_stmt);
9696 : : break;
9697 : :
9698 : 0 : case DW_LNS_set_basic_block:
9699 : : /* Takes no argument. */
9700 : 0 : puts (_(" set basic block flag"));
9701 : 0 : break;
9702 : :
9703 : 53694 : case DW_LNS_const_add_pc:
9704 : : /* Takes no argument. */
9705 : :
9706 [ - + ]: 53694 : if (unlikely (line_range == 0))
9707 : 0 : goto invalid_unit;
9708 : :
9709 : 53694 : advance_pc ((255 - opcode_base) / line_range);
9710 : : {
9711 : 53694 : printf (_(" advance address by constant %u to "),
9712 : : op_addr_advance);
9713 : 53694 : print_dwarf_addr (dwflmod, 0, address, address);
9714 [ - + ]: 53694 : if (op_index > 0)
9715 : 0 : printf (_(", op_index to %u"), op_index);
9716 : 53694 : printf ("\n");
9717 : : }
9718 : : break;
9719 : :
9720 : 44 : case DW_LNS_fixed_advance_pc:
9721 : : /* Takes one 16 bit parameter which is added to the
9722 : : address. */
9723 [ + - - + ]: 44 : if (unlikely (standard_opcode_lengths[opcode] != 1
9724 : : || lineendp - linep < 2))
9725 : 0 : goto invalid_unit;
9726 : :
9727 [ - + ]: 44 : u128 = read_2ubyte_unaligned_inc (dbg, linep);
9728 : 44 : address += u128;
9729 : 44 : op_index = 0;
9730 : : {
9731 : 44 : printf (_("\
9732 : : advance address by fixed value %u to \n"),
9733 : : u128);
9734 : 44 : print_dwarf_addr (dwflmod, 0, address, address);
9735 : 44 : printf ("\n");
9736 : : }
9737 : : break;
9738 : :
9739 : 10 : case DW_LNS_set_prologue_end:
9740 : : /* Takes no argument. */
9741 : 10 : puts (_(" set prologue end flag"));
9742 : 10 : break;
9743 : :
9744 : 0 : case DW_LNS_set_epilogue_begin:
9745 : : /* Takes no argument. */
9746 : 0 : puts (_(" set epilogue begin flag"));
9747 : 0 : break;
9748 : :
9749 : 0 : case DW_LNS_set_isa:
9750 : : /* Takes one uleb128 parameter which is stored in isa. */
9751 [ # # # # ]: 0 : if (unlikely (standard_opcode_lengths[opcode] != 1
9752 : : || lineendp - linep < 1))
9753 : 0 : goto invalid_unit;
9754 : :
9755 : 0 : get_uleb128 (u128, linep, lineendp);
9756 [ + + ]: 2402654 : printf (_(" set isa to %u\n"), u128);
9757 : : break;
9758 : : }
9759 : : }
9760 : : else
9761 : : {
9762 : : /* This is a new opcode the generator but not we know about.
9763 : : Read the parameters associated with it but then discard
9764 : : everything. Read all the parameters for this opcode. */
9765 : 0 : printf (ngettext (" unknown opcode with %" PRIu8 " parameter:",
9766 : : " unknown opcode with %" PRIu8 " parameters:",
9767 : : standard_opcode_lengths[opcode]),
9768 : 0 : standard_opcode_lengths[opcode]);
9769 : 0 : for (int n = standard_opcode_lengths[opcode];
9770 [ # # # # ]: 0 : n > 0 && linep < lineendp; --n)
9771 : : {
9772 : 0 : get_uleb128 (u128, linep, lineendp);
9773 [ # # ]: 0 : if (n != standard_opcode_lengths[opcode])
9774 : 0 : fputc (',', stdout);
9775 : 0 : printf (" %u", u128);
9776 : : }
9777 : :
9778 : : /* Next round, ignore this opcode. */
9779 : 0 : continue;
9780 : : }
9781 : : }
9782 : : }
9783 : :
9784 : : /* There must only be one data block. */
9785 [ - + ]: 104 : assert (elf_getdata (scn, data) == NULL);
9786 : : }
9787 : :
9788 : :
9789 : : static void
9790 : 40 : print_debug_loclists_section (Dwfl_Module *dwflmod,
9791 : : Ebl *ebl,
9792 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
9793 : : Elf_Scn *scn, GElf_Shdr *shdr,
9794 : : Dwarf *dbg)
9795 : : {
9796 : 40 : Elf_Data *data = get_debug_elf_data (dbg, ebl, IDX_debug_loclists, scn);
9797 [ + - ]: 40 : if (data == NULL)
9798 : 0 : return;
9799 : :
9800 : 40 : printf (_("\
9801 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
9802 : : elf_ndxscn (scn), section_name (ebl, shdr),
9803 : 40 : (uint64_t) shdr->sh_offset);
9804 : :
9805 : : /* For the listptr to get the base address/CU. */
9806 : 40 : sort_listptr (&known_loclistsptr, "loclistsptr");
9807 : 40 : size_t listptr_idx = 0;
9808 : :
9809 : 40 : const unsigned char *readp = data->d_buf;
9810 : 40 : const unsigned char *const dataend = ((unsigned char *) data->d_buf
9811 : 40 : + data->d_size);
9812 [ + + ]: 3128 : while (readp < dataend)
9813 : : {
9814 [ - + ]: 3088 : if (unlikely (readp > dataend - 4))
9815 : : {
9816 : 0 : invalid_data:
9817 : 0 : error (0, 0, _("invalid data in section [%zu] '%s'"),
9818 : : elf_ndxscn (scn), section_name (ebl, shdr));
9819 : 0 : return;
9820 : : }
9821 : :
9822 : 3088 : ptrdiff_t offset = readp - (unsigned char *) data->d_buf;
9823 : 3088 : printf (_("Table at Offset 0x%" PRIx64 ":\n\n"),
9824 : : (uint64_t) offset);
9825 : :
9826 [ - + ]: 3088 : uint64_t unit_length = read_4ubyte_unaligned_inc (dbg, readp);
9827 : 3088 : unsigned int offset_size = 4;
9828 [ - + ]: 3088 : if (unlikely (unit_length == 0xffffffff))
9829 : : {
9830 [ # # ]: 0 : if (unlikely (readp > dataend - 8))
9831 : 0 : goto invalid_data;
9832 : :
9833 [ # # ]: 0 : unit_length = read_8ubyte_unaligned_inc (dbg, readp);
9834 : 0 : offset_size = 8;
9835 : : }
9836 : 3088 : printf (_(" Length: %8" PRIu64 "\n"), unit_length);
9837 : :
9838 : : /* We need at least 2-bytes + 1-byte + 1-byte + 4-bytes = 8
9839 : : bytes to complete the header. And this unit cannot go beyond
9840 : : the section data. */
9841 [ + - ]: 3088 : if (readp > dataend - 8
9842 [ + - ]: 3088 : || unit_length < 8
9843 [ - + ]: 3088 : || unit_length > (uint64_t) (dataend - readp))
9844 : 0 : goto invalid_data;
9845 : :
9846 : 3088 : const unsigned char *nexthdr = readp + unit_length;
9847 : :
9848 [ - + ]: 3088 : uint16_t version = read_2ubyte_unaligned_inc (dbg, readp);
9849 : 3088 : printf (_(" DWARF version: %8" PRIu16 "\n"), version);
9850 : :
9851 [ - + ]: 3088 : if (version != 5)
9852 : : {
9853 : 0 : error (0, 0, _("Unknown version"));
9854 : 0 : goto next_table;
9855 : : }
9856 : :
9857 : 3088 : uint8_t address_size = *readp++;
9858 : 3088 : printf (_(" Address size: %8" PRIu64 "\n"),
9859 : : (uint64_t) address_size);
9860 : :
9861 [ - + ]: 3088 : if (address_size != 4 && address_size != 8)
9862 : : {
9863 : 0 : error (0, 0, _("unsupported address size"));
9864 : 0 : goto next_table;
9865 : : }
9866 : :
9867 : 3088 : uint8_t segment_size = *readp++;
9868 : 3088 : printf (_(" Segment size: %8" PRIu64 "\n"),
9869 : : (uint64_t) segment_size);
9870 : :
9871 [ - + ]: 3088 : if (segment_size != 0)
9872 : : {
9873 : 0 : error (0, 0, _("unsupported segment size"));
9874 : 0 : goto next_table;
9875 : : }
9876 : :
9877 [ - + ]: 3088 : uint32_t offset_entry_count = read_4ubyte_unaligned_inc (dbg, readp);
9878 : 3088 : printf (_(" Offset entries: %8" PRIu64 "\n"),
9879 : : (uint64_t) offset_entry_count);
9880 : :
9881 : : /* We need the CU that uses this unit to get the initial base address. */
9882 : 3088 : Dwarf_Addr cu_base = 0;
9883 : 3088 : struct Dwarf_CU *cu = NULL;
9884 [ - + ]: 3088 : if (listptr_cu (&known_loclistsptr, &listptr_idx,
9885 : : (Dwarf_Off) offset,
9886 : 3088 : (Dwarf_Off) (nexthdr - (unsigned char *) data->d_buf),
9887 : : &cu_base, &cu)
9888 [ # # ]: 0 : || split_dwarf_cu_base (dbg, &cu, &cu_base))
9889 : 3088 : {
9890 : 3088 : Dwarf_Die cudie;
9891 [ - + ]: 3088 : if (dwarf_cu_die (cu, &cudie,
9892 : : NULL, NULL, NULL, NULL,
9893 : : NULL, NULL) == NULL)
9894 : 0 : printf (_(" Unknown CU base: "));
9895 : : else
9896 : 3088 : printf (_(" CU [%6" PRIx64 "] base: "),
9897 : : dwarf_dieoffset (&cudie));
9898 : 3088 : print_dwarf_addr (dwflmod, address_size, cu_base, cu_base);
9899 : 3088 : printf ("\n");
9900 : : }
9901 : : else
9902 : 0 : printf (_(" Not associated with a CU.\n"));
9903 : :
9904 : 3088 : printf ("\n");
9905 : :
9906 : 3088 : const unsigned char *offset_array_start = readp;
9907 [ + + ]: 3088 : if (offset_entry_count > 0)
9908 : : {
9909 : 4 : uint64_t max_entries = (unit_length - 8) / offset_size;
9910 [ - + ]: 4 : if (offset_entry_count > max_entries)
9911 : : {
9912 : 0 : error (0, 0,
9913 : 0 : _("too many offset entries for unit length"));
9914 : 0 : offset_entry_count = max_entries;
9915 : : }
9916 : :
9917 : 4 : printf (_(" Offsets starting at 0x%" PRIx64 ":\n"),
9918 : : (uint64_t) (offset_array_start
9919 : 4 : - (unsigned char *) data->d_buf));
9920 [ + + ]: 36 : for (uint32_t idx = 0; idx < offset_entry_count; idx++)
9921 : : {
9922 : 32 : printf (" [%6" PRIu32 "] ", idx);
9923 [ + - ]: 32 : if (offset_size == 4)
9924 : : {
9925 [ - + ]: 32 : uint32_t off = read_4ubyte_unaligned_inc (dbg, readp);
9926 : 32 : printf ("0x%" PRIx32 "\n", off);
9927 : : }
9928 : : else
9929 : : {
9930 [ # # ]: 0 : uint64_t off = read_8ubyte_unaligned_inc (dbg, readp);
9931 : 32 : printf ("0x%" PRIx64 "\n", off);
9932 : : }
9933 : : }
9934 : 4 : printf ("\n");
9935 : : }
9936 : :
9937 : 3088 : Dwarf_Addr base = cu_base;
9938 : 3088 : bool start_of_list = true;
9939 : 3088 : while (readp < nexthdr)
9940 : : {
9941 : 747550 : Dwarf_Off off = (Dwarf_Off) (readp - (unsigned char *) data->d_buf);
9942 [ + + ]: 747550 : if (listptr_attr (&known_loclistsptr, listptr_idx, off,
9943 : : DW_AT_GNU_locviews))
9944 : 109978 : {
9945 : 109978 : Dwarf_Off next_off = next_listptr_offset (&known_loclistsptr,
9946 : : &listptr_idx, off);
9947 : 109978 : const unsigned char *locp = readp;
9948 : 109978 : const unsigned char *locendp;
9949 [ + - ]: 109978 : if (next_off == 0
9950 [ + - ]: 109978 : || next_off > (size_t) (nexthdr - ((const unsigned char *)
9951 : : data->d_buf)))
9952 : : locendp = nexthdr;
9953 : : else
9954 : 109978 : locendp = (const unsigned char *) data->d_buf + next_off;
9955 : :
9956 : 728982 : printf (" Offset: %" PRIx64 ", Index: %" PRIx64 "\n",
9957 : : (uint64_t) (readp - (unsigned char *) data->d_buf),
9958 : 109978 : (uint64_t) (readp - offset_array_start));
9959 : :
9960 : 619004 : while (locp < locendp)
9961 : : {
9962 : 509026 : uint64_t v1, v2;
9963 : 509026 : get_uleb128 (v1, locp, locendp);
9964 [ - + ]: 509026 : if (locp >= locendp)
9965 : : {
9966 : 0 : printf (_(" <INVALID DATA>\n"));
9967 : : break;
9968 : : }
9969 : 509026 : get_uleb128 (v2, locp, locendp);
9970 [ + + ]: 619004 : printf (" view pair %" PRId64 ", %" PRId64 "\n", v1, v2);
9971 : : }
9972 : :
9973 : 109978 : printf ("\n");
9974 : 109978 : readp = (unsigned char *) locendp;
9975 : 109978 : continue;
9976 : : }
9977 : :
9978 : 637572 : uint8_t kind = *readp++;
9979 : 637572 : uint64_t op1, op2, len;
9980 : :
9981 : : /* Skip padding. */
9982 [ - + ]: 637572 : if (start_of_list && kind == DW_LLE_end_of_list)
9983 : 0 : continue;
9984 : :
9985 [ + + ]: 637572 : if (start_of_list)
9986 : : {
9987 : 110042 : base = cu_base;
9988 : 110042 : printf (" Offset: %" PRIx64 ", Index: %" PRIx64 "\n",
9989 : 110042 : (uint64_t) (readp - (unsigned char *) data->d_buf - 1),
9990 : 110042 : (uint64_t) (readp - offset_array_start - 1));
9991 : 110042 : start_of_list = false;
9992 : : }
9993 : :
9994 : 637572 : printf (" %s", dwarf_loc_list_encoding_name (kind));
9995 [ + - - + : 637572 : switch (kind)
+ - + - +
- - ]
9996 : : {
9997 : 110042 : case DW_LLE_end_of_list:
9998 : 110042 : start_of_list = true;
9999 : 110042 : printf ("\n\n");
10000 : : break;
10001 : :
10002 : 0 : case DW_LLE_base_addressx:
10003 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
10004 : : {
10005 : 0 : invalid_entry:
10006 : 0 : error (0, 0, _("invalid loclists data"));
10007 : 0 : goto next_table;
10008 : : }
10009 : 0 : get_uleb128 (op1, readp, nexthdr);
10010 : 0 : printf (" %" PRIx64 "\n", op1);
10011 [ # # ]: 0 : if (! print_unresolved_addresses)
10012 : : {
10013 : 0 : Dwarf_Addr addr;
10014 [ # # ]: 0 : if (get_indexed_addr (cu, op1, &addr) != 0)
10015 : 0 : printf (" ???\n");
10016 : : else
10017 : : {
10018 : 0 : printf (" ");
10019 : 0 : print_dwarf_addr (dwflmod, address_size, addr, addr);
10020 : 0 : printf ("\n");
10021 : : }
10022 : : }
10023 : : break;
10024 : :
10025 : 0 : case DW_LLE_startx_endx:
10026 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
10027 : 0 : goto invalid_entry;
10028 : 0 : get_uleb128 (op1, readp, nexthdr);
10029 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
10030 : 0 : goto invalid_entry;
10031 : 0 : get_uleb128 (op2, readp, nexthdr);
10032 : 0 : printf (" %" PRIx64 ", %" PRIx64 "\n", op1, op2);
10033 [ # # ]: 0 : if (! print_unresolved_addresses)
10034 : : {
10035 : 0 : Dwarf_Addr addr1;
10036 : 0 : Dwarf_Addr addr2;
10037 [ # # ]: 0 : if (get_indexed_addr (cu, op1, &addr1) != 0
10038 [ # # ]: 0 : || get_indexed_addr (cu, op2, &addr2) != 0)
10039 : : {
10040 : 0 : printf (" ???..\n");
10041 : 0 : printf (" ???\n");
10042 : : }
10043 : : else
10044 : : {
10045 : 0 : printf (" ");
10046 : 0 : print_dwarf_addr (dwflmod, address_size, addr1, addr1);
10047 : 0 : printf ("..\n ");
10048 : 0 : print_dwarf_addr (dwflmod, address_size,
10049 : : addr2 - 1, addr2);
10050 : 0 : printf ("\n");
10051 : : }
10052 : : }
10053 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
10054 : 0 : goto invalid_entry;
10055 : 0 : get_uleb128 (len, readp, nexthdr);
10056 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < len)
10057 : 0 : goto invalid_entry;
10058 : 0 : print_ops (dwflmod, dbg, 8, 8, version,
10059 : : address_size, offset_size, cu, len, readp);
10060 : 0 : readp += len;
10061 : 0 : break;
10062 : :
10063 : 52 : case DW_LLE_startx_length:
10064 [ - + ]: 52 : if ((uint64_t) (nexthdr - readp) < 1)
10065 : 0 : goto invalid_entry;
10066 : 52 : get_uleb128 (op1, readp, nexthdr);
10067 [ - + ]: 52 : if ((uint64_t) (nexthdr - readp) < 1)
10068 : 0 : goto invalid_entry;
10069 : 52 : get_uleb128 (op2, readp, nexthdr);
10070 : 52 : printf (" %" PRIx64 ", %" PRIx64 "\n", op1, op2);
10071 [ + - ]: 52 : if (! print_unresolved_addresses)
10072 : : {
10073 : 52 : Dwarf_Addr addr1;
10074 : 52 : Dwarf_Addr addr2;
10075 [ - + ]: 52 : if (get_indexed_addr (cu, op1, &addr1) != 0)
10076 : : {
10077 : 0 : printf (" ???..\n");
10078 : 0 : printf (" ???\n");
10079 : : }
10080 : : else
10081 : : {
10082 : 52 : addr2 = addr1 + op2;
10083 : 52 : printf (" ");
10084 : 52 : print_dwarf_addr (dwflmod, address_size, addr1, addr1);
10085 : 52 : printf ("..\n ");
10086 : 52 : print_dwarf_addr (dwflmod, address_size,
10087 : : addr2 - 1, addr2);
10088 : 52 : printf ("\n");
10089 : : }
10090 : : }
10091 [ - + ]: 52 : if ((uint64_t) (nexthdr - readp) < 1)
10092 : 0 : goto invalid_entry;
10093 : 52 : get_uleb128 (len, readp, nexthdr);
10094 [ - + ]: 52 : if ((uint64_t) (nexthdr - readp) < len)
10095 : 0 : goto invalid_entry;
10096 : 52 : print_ops (dwflmod, dbg, 8, 8, version,
10097 : : address_size, offset_size, cu, len, readp);
10098 : 52 : readp += len;
10099 : 52 : break;
10100 : :
10101 : 497240 : case DW_LLE_offset_pair:
10102 [ - + ]: 497240 : if ((uint64_t) (nexthdr - readp) < 1)
10103 : 0 : goto invalid_entry;
10104 : 497240 : get_uleb128 (op1, readp, nexthdr);
10105 [ - + ]: 497240 : if ((uint64_t) (nexthdr - readp) < 1)
10106 : 0 : goto invalid_entry;
10107 : 497240 : get_uleb128 (op2, readp, nexthdr);
10108 : 497240 : printf (" %" PRIx64 ", %" PRIx64 "\n", op1, op2);
10109 [ + - ]: 497240 : if (! print_unresolved_addresses)
10110 : : {
10111 : 497240 : op1 += base;
10112 : 497240 : op2 += base;
10113 : 497240 : printf (" ");
10114 : 497240 : print_dwarf_addr (dwflmod, address_size, op1, op1);
10115 : 497240 : printf ("..\n ");
10116 : 497240 : print_dwarf_addr (dwflmod, address_size, op2 - 1, op2);
10117 : 497240 : printf ("\n");
10118 : : }
10119 [ - + ]: 497240 : if ((uint64_t) (nexthdr - readp) < 1)
10120 : 0 : goto invalid_entry;
10121 : 497240 : get_uleb128 (len, readp, nexthdr);
10122 [ - + ]: 497240 : if ((uint64_t) (nexthdr - readp) < len)
10123 : 0 : goto invalid_entry;
10124 : 497240 : print_ops (dwflmod, dbg, 8, 8, version,
10125 : : address_size, offset_size, cu, len, readp);
10126 : 497240 : readp += len;
10127 : 497240 : break;
10128 : :
10129 : 0 : case DW_LLE_default_location:
10130 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
10131 : 0 : goto invalid_entry;
10132 : 0 : get_uleb128 (len, readp, nexthdr);
10133 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < len)
10134 : 0 : goto invalid_entry;
10135 : 0 : print_ops (dwflmod, dbg, 8, 8, version,
10136 : : address_size, offset_size, cu, len, readp);
10137 : 0 : readp += len;
10138 : 0 : break;
10139 : :
10140 : 18400 : case DW_LLE_base_address:
10141 [ - + ]: 18400 : if (address_size == 4)
10142 : : {
10143 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 4)
10144 : 0 : goto invalid_entry;
10145 [ # # ]: 0 : op1 = read_4ubyte_unaligned_inc (dbg, readp);
10146 : : }
10147 : : else
10148 : : {
10149 [ - + ]: 18400 : if ((uint64_t) (nexthdr - readp) < 8)
10150 : 0 : goto invalid_entry;
10151 [ - + ]: 18400 : op1 = read_8ubyte_unaligned_inc (dbg, readp);
10152 : : }
10153 : 18400 : base = op1;
10154 : 18400 : printf (" 0x%" PRIx64 "\n", base);
10155 [ - + ]: 18400 : if (! print_unresolved_addresses)
10156 : : {
10157 : 18400 : printf (" ");
10158 : 18400 : print_dwarf_addr (dwflmod, address_size, base, base);
10159 : 18400 : printf ("\n");
10160 : : }
10161 : : break;
10162 : :
10163 : 0 : case DW_LLE_start_end:
10164 [ # # ]: 0 : if (address_size == 4)
10165 : : {
10166 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 8)
10167 : 0 : goto invalid_entry;
10168 [ # # ]: 0 : op1 = read_4ubyte_unaligned_inc (dbg, readp);
10169 [ # # ]: 0 : op2 = read_4ubyte_unaligned_inc (dbg, readp);
10170 : : }
10171 : : else
10172 : : {
10173 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 16)
10174 : 0 : goto invalid_entry;
10175 [ # # ]: 0 : op1 = read_8ubyte_unaligned_inc (dbg, readp);
10176 [ # # ]: 0 : op2 = read_8ubyte_unaligned_inc (dbg, readp);
10177 : : }
10178 : 0 : printf (" 0x%" PRIx64 "..0x%" PRIx64 "\n", op1, op2);
10179 [ # # ]: 0 : if (! print_unresolved_addresses)
10180 : : {
10181 : 0 : printf (" ");
10182 : 0 : print_dwarf_addr (dwflmod, address_size, op1, op1);
10183 : 0 : printf ("..\n ");
10184 : 0 : print_dwarf_addr (dwflmod, address_size, op2 - 1, op2);
10185 : 0 : printf ("\n");
10186 : : }
10187 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
10188 : 0 : goto invalid_entry;
10189 : 0 : get_uleb128 (len, readp, nexthdr);
10190 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < len)
10191 : 0 : goto invalid_entry;
10192 : 0 : print_ops (dwflmod, dbg, 8, 8, version,
10193 : : address_size, offset_size, cu, len, readp);
10194 : 0 : readp += len;
10195 : 0 : break;
10196 : :
10197 : 11838 : case DW_LLE_start_length:
10198 [ - + ]: 11838 : if (address_size == 4)
10199 : : {
10200 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 4)
10201 : 0 : goto invalid_entry;
10202 [ # # ]: 0 : op1 = read_4ubyte_unaligned_inc (dbg, readp);
10203 : : }
10204 : : else
10205 : : {
10206 [ - + ]: 11838 : if ((uint64_t) (nexthdr - readp) < 8)
10207 : 0 : goto invalid_entry;
10208 [ - + ]: 11838 : op1 = read_8ubyte_unaligned_inc (dbg, readp);
10209 : : }
10210 [ - + ]: 11838 : if ((uint64_t) (nexthdr - readp) < 1)
10211 : 0 : goto invalid_entry;
10212 : 11838 : get_uleb128 (op2, readp, nexthdr);
10213 : 11838 : printf (" 0x%" PRIx64 ", %" PRIx64 "\n", op1, op2);
10214 [ + + ]: 11838 : if (! print_unresolved_addresses)
10215 : : {
10216 : 11836 : op2 = op1 + op2;
10217 : 11836 : printf (" ");
10218 : 11836 : print_dwarf_addr (dwflmod, address_size, op1, op1);
10219 : 11836 : printf ("..\n ");
10220 : 11836 : print_dwarf_addr (dwflmod, address_size, op2 - 1, op2);
10221 : 11836 : printf ("\n");
10222 : : }
10223 [ - + ]: 11838 : if ((uint64_t) (nexthdr - readp) < 1)
10224 : 0 : goto invalid_entry;
10225 : 11838 : get_uleb128 (len, readp, nexthdr);
10226 [ - + ]: 11838 : if ((uint64_t) (nexthdr - readp) < len)
10227 : 0 : goto invalid_entry;
10228 : 11838 : print_ops (dwflmod, dbg, 8, 8, version,
10229 : : address_size, offset_size, cu, len, readp);
10230 : 11838 : readp += len;
10231 : 11838 : break;
10232 : :
10233 : 0 : case DW_LLE_GNU_view_pair:
10234 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
10235 : 0 : goto invalid_entry;
10236 : 0 : get_uleb128 (op1, readp, nexthdr);
10237 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
10238 : 0 : goto invalid_entry;
10239 : 0 : get_uleb128 (op2, readp, nexthdr);
10240 [ + + ]: 750638 : printf (" %" PRIx64 ", %" PRIx64 "\n", op1, op2);
10241 : : break;
10242 : :
10243 : 0 : default:
10244 : 0 : goto invalid_entry;
10245 : : }
10246 : : }
10247 : :
10248 : 3088 : next_table:
10249 [ - + ]: 3088 : if (readp != nexthdr)
10250 : : {
10251 : 0 : size_t padding = nexthdr - readp;
10252 : 0 : printf (_(" %zu padding bytes\n\n"), padding);
10253 : 0 : readp = nexthdr;
10254 : : }
10255 : : }
10256 : : }
10257 : :
10258 : :
10259 : : static void
10260 : 52 : print_debug_loc_section (Dwfl_Module *dwflmod,
10261 : : Ebl *ebl, GElf_Ehdr *ehdr,
10262 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
10263 : : {
10264 : 52 : Elf_Data *data = get_debug_elf_data (dbg, ebl, IDX_debug_loc, scn);
10265 [ - + ]: 52 : if (data == NULL)
10266 : 0 : return;
10267 : :
10268 : 52 : printf (_("\
10269 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
10270 : : elf_ndxscn (scn), section_name (ebl, shdr),
10271 : 52 : (uint64_t) shdr->sh_offset);
10272 : :
10273 : 52 : sort_listptr (&known_locsptr, "loclistptr");
10274 : 52 : size_t listptr_idx = 0;
10275 : :
10276 [ + - ]: 52 : uint_fast8_t address_size = ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 4 : 8;
10277 : 52 : uint_fast8_t offset_size = 4;
10278 : :
10279 : 52 : bool first = true;
10280 : 52 : Dwarf_Addr base = 0;
10281 : 52 : unsigned char *readp = data->d_buf;
10282 : 52 : unsigned char *const endp = (unsigned char *) data->d_buf + data->d_size;
10283 : 52 : Dwarf_CU *last_cu = NULL;
10284 [ + + ]: 490 : while (readp < endp)
10285 : : {
10286 : 438 : ptrdiff_t offset = readp - (unsigned char *) data->d_buf;
10287 : 438 : Dwarf_CU *cu = last_cu;
10288 : 438 : unsigned int attr = 0;
10289 : :
10290 [ + + - + ]: 438 : if (first && skip_listptr_hole (&known_locsptr, &listptr_idx,
10291 : : &address_size, &offset_size, &base,
10292 : : &cu, offset, &readp, endp, &attr))
10293 : 0 : continue;
10294 : :
10295 [ + + ]: 438 : if (last_cu != cu)
10296 : : {
10297 : 60 : Dwarf_Die cudie;
10298 [ - + ]: 60 : if (dwarf_cu_die (cu, &cudie,
10299 : : NULL, NULL, NULL, NULL,
10300 : : NULL, NULL) == NULL)
10301 : 0 : printf (_("\n Unknown CU base: "));
10302 : : else
10303 : 60 : printf (_("\n CU [%6" PRIx64 "] base: "),
10304 : : dwarf_dieoffset (&cudie));
10305 : 60 : print_dwarf_addr (dwflmod, address_size, base, base);
10306 : 60 : printf ("\n");
10307 : : }
10308 : 438 : last_cu = cu;
10309 : :
10310 [ - + ]: 438 : if (attr == DW_AT_GNU_locviews)
10311 : 0 : {
10312 : 0 : Dwarf_Off next_off = next_listptr_offset (&known_locsptr,
10313 : : &listptr_idx, offset);
10314 : 0 : const unsigned char *locp = readp;
10315 : 0 : const unsigned char *locendp;
10316 [ # # ]: 0 : if (next_off == 0
10317 : 0 : || next_off > (size_t) (endp
10318 [ # # ]: 0 : - (const unsigned char *) data->d_buf))
10319 : 0 : locendp = endp;
10320 : : else
10321 : 0 : locendp = (const unsigned char *) data->d_buf + next_off;
10322 : :
10323 [ # # ]: 0 : while (locp < locendp)
10324 : : {
10325 : 0 : uint64_t v1, v2;
10326 : 0 : get_uleb128 (v1, locp, locendp);
10327 [ # # ]: 0 : if (locp >= locendp)
10328 : : {
10329 : 0 : printf (_(" [%6tx] <INVALID DATA>\n"), offset);
10330 : : break;
10331 : : }
10332 : 0 : get_uleb128 (v2, locp, locendp);
10333 [ # # ]: 0 : if (first) /* First view pair in a list. */
10334 : 0 : printf (" [%6tx] ", offset);
10335 : : else
10336 : 0 : printf (" ");
10337 : 0 : printf ("view pair %" PRId64 ", %" PRId64 "\n", v1, v2);
10338 : 0 : first = false;
10339 : : }
10340 : :
10341 : 0 : first = true;
10342 : 0 : readp = (unsigned char *) locendp;
10343 : 0 : continue;
10344 : : }
10345 : :
10346 : : /* GNU DebugFission encoded addresses as addrx. */
10347 : 876 : bool is_debugfission = ((cu != NULL
10348 [ # # ]: 0 : || split_dwarf_cu_base (dbg, &cu, &base))
10349 [ - + - + ]: 438 : && (cu->version < 5
10350 [ + + ]: 438 : && cu->unit_type == DW_UT_split_compile));
10351 [ + + ]: 438 : if (!is_debugfission
10352 [ - + ]: 318 : && unlikely (data->d_size - offset < (size_t) address_size * 2))
10353 : : {
10354 : 0 : invalid_data:
10355 : 0 : printf (_(" [%6tx] <INVALID DATA>\n"), offset);
10356 : 0 : break;
10357 : : }
10358 : :
10359 : 318 : Dwarf_Addr begin;
10360 : 318 : Dwarf_Addr end;
10361 : 438 : bool use_base = true;
10362 : 318 : if (is_debugfission)
10363 : : {
10364 : 120 : const unsigned char *locp = readp;
10365 : 120 : const unsigned char *locendp = readp + data->d_size;
10366 [ - + ]: 120 : if (locp >= locendp)
10367 : 0 : goto invalid_data;
10368 : :
10369 : 120 : Dwarf_Word idx;
10370 : 120 : unsigned char code = *locp++;
10371 [ + - - + : 120 : switch (code)
- ]
10372 : : {
10373 : 40 : case DW_LLE_GNU_end_of_list_entry:
10374 : 40 : begin = 0;
10375 : 40 : end = 0;
10376 : 40 : break;
10377 : :
10378 : 0 : case DW_LLE_GNU_base_address_selection_entry:
10379 [ # # ]: 0 : if (locp >= locendp)
10380 : 0 : goto invalid_data;
10381 : 0 : begin = (Dwarf_Addr) -1;
10382 : 0 : get_uleb128 (idx, locp, locendp);
10383 [ # # ]: 0 : if (get_indexed_addr (cu, idx, &end) != 0)
10384 : 0 : end = idx; /* ... */
10385 : : break;
10386 : :
10387 : 0 : case DW_LLE_GNU_start_end_entry:
10388 [ # # ]: 0 : if (locp >= locendp)
10389 : 0 : goto invalid_data;
10390 : 0 : get_uleb128 (idx, locp, locendp);
10391 [ # # ]: 0 : if (get_indexed_addr (cu, idx, &begin) != 0)
10392 : 0 : begin = idx; /* ... */
10393 [ # # ]: 0 : if (locp >= locendp)
10394 : 0 : goto invalid_data;
10395 : 0 : get_uleb128 (idx, locp, locendp);
10396 [ # # ]: 0 : if (get_indexed_addr (cu, idx, &end) != 0)
10397 : 0 : end = idx; /* ... */
10398 : : use_base = false;
10399 : : break;
10400 : :
10401 : 80 : case DW_LLE_GNU_start_length_entry:
10402 [ - + ]: 80 : if (locp >= locendp)
10403 : 0 : goto invalid_data;
10404 : 80 : get_uleb128 (idx, locp, locendp);
10405 [ - + ]: 80 : if (get_indexed_addr (cu, idx, &begin) != 0)
10406 : 0 : begin = idx; /* ... */
10407 [ - + ]: 80 : if (locendp - locp < 4)
10408 : 0 : goto invalid_data;
10409 [ - + ]: 80 : end = read_4ubyte_unaligned_inc (dbg, locp);
10410 : 80 : end += begin;
10411 : 80 : use_base = false;
10412 : 80 : break;
10413 : :
10414 : 0 : default:
10415 : 0 : goto invalid_data;
10416 : : }
10417 : :
10418 : 120 : readp = (unsigned char *) locp;
10419 : : }
10420 [ + - ]: 318 : else if (address_size == 8)
10421 : : {
10422 [ + + ]: 318 : begin = read_8ubyte_unaligned_inc (dbg, readp);
10423 [ + + ]: 318 : end = read_8ubyte_unaligned_inc (dbg, readp);
10424 : : }
10425 : : else
10426 : : {
10427 [ # # ]: 0 : begin = read_4ubyte_unaligned_inc (dbg, readp);
10428 [ # # ]: 0 : end = read_4ubyte_unaligned_inc (dbg, readp);
10429 [ # # ]: 0 : if (begin == (Dwarf_Addr) (uint32_t) -1)
10430 : 0 : begin = (Dwarf_Addr) -1l;
10431 : : }
10432 : :
10433 [ - + ]: 438 : if (begin == (Dwarf_Addr) -1l) /* Base address entry. */
10434 : : {
10435 [ # # ]: 0 : if (first)
10436 : 0 : printf (" [%6tx] ", offset);
10437 : : else
10438 : 0 : printf (" ");
10439 : 0 : puts (_("base address"));
10440 : 0 : printf (" ");
10441 : 0 : print_dwarf_addr (dwflmod, address_size, end, end);
10442 : 0 : printf ("\n");
10443 : 0 : base = end;
10444 : 0 : first = false;
10445 : : }
10446 [ + + + + ]: 438 : else if (begin == 0 && end == 0) /* End of list entry. */
10447 : : {
10448 [ - + ]: 148 : if (first)
10449 : 438 : printf (_(" [%6tx] empty list\n"), offset);
10450 : : first = true;
10451 : : }
10452 : : else
10453 : : {
10454 : : /* We have a location expression entry. */
10455 [ + + ]: 290 : uint_fast16_t len = read_2ubyte_unaligned_inc (dbg, readp);
10456 : :
10457 [ + + ]: 290 : if (first) /* First entry in a list. */
10458 : 148 : printf (" [%6tx] ", offset);
10459 : : else
10460 : 142 : printf (" ");
10461 : :
10462 : 290 : printf ("range %" PRIx64 ", %" PRIx64 "\n", begin, end);
10463 [ + + ]: 290 : if (! print_unresolved_addresses)
10464 : : {
10465 [ + + ]: 230 : Dwarf_Addr dab = use_base ? base + begin : begin;
10466 [ + + ]: 230 : Dwarf_Addr dae = use_base ? base + end : end;
10467 : 230 : printf (" ");
10468 : 230 : print_dwarf_addr (dwflmod, address_size, dab, dab);
10469 : 230 : printf ("..\n ");
10470 : 230 : print_dwarf_addr (dwflmod, address_size, dae - 1, dae);
10471 : 230 : printf ("\n");
10472 : : }
10473 : :
10474 [ - + ]: 290 : if (endp - readp <= (ptrdiff_t) len)
10475 : : {
10476 : 0 : fputs (_(" <INVALID DATA>\n"), stdout);
10477 : 0 : break;
10478 : : }
10479 : :
10480 [ + - ]: 580 : print_ops (dwflmod, dbg, 11, 11,
10481 : 290 : cu != NULL ? cu->version : 3,
10482 : : address_size, offset_size, cu, len, readp);
10483 : :
10484 : 290 : first = false;
10485 : 290 : readp += len;
10486 : : }
10487 : : }
10488 : : }
10489 : :
10490 : : struct mac_culist
10491 : : {
10492 : : Dwarf_Die die;
10493 : : Dwarf_Off offset;
10494 : : Dwarf_Files *files;
10495 : : struct mac_culist *next;
10496 : : };
10497 : :
10498 : :
10499 : : static int
10500 : 0 : mac_compare (const void *p1, const void *p2)
10501 : : {
10502 : 0 : struct mac_culist *m1 = (struct mac_culist *) p1;
10503 : 0 : struct mac_culist *m2 = (struct mac_culist *) p2;
10504 : :
10505 [ # # ]: 0 : if (m1->offset < m2->offset)
10506 : : return -1;
10507 [ # # ]: 0 : if (m1->offset > m2->offset)
10508 : 0 : return 1;
10509 : : return 0;
10510 : : }
10511 : :
10512 : :
10513 : : static void
10514 : 0 : print_debug_macinfo_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
10515 : : Ebl *ebl,
10516 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
10517 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
10518 : : {
10519 : 0 : Elf_Data *data = get_debug_elf_data (dbg, ebl, IDX_debug_macinfo, scn);
10520 [ # # ]: 0 : if (data == NULL)
10521 : 0 : return;
10522 : :
10523 : 0 : printf (_("\
10524 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
10525 : : elf_ndxscn (scn), section_name (ebl, shdr),
10526 : 0 : (uint64_t) shdr->sh_offset);
10527 : 0 : fputc ('\n', stdout);
10528 : :
10529 : : /* There is no function in libdw to iterate over the raw content of
10530 : : the section but it is easy enough to do. */
10531 : :
10532 : : /* Get the source file information for all CUs. */
10533 : 0 : Dwarf_Off offset;
10534 : 0 : Dwarf_Off ncu = 0;
10535 : 0 : size_t hsize;
10536 : 0 : struct mac_culist *culist = NULL;
10537 : 0 : size_t nculist = 0;
10538 [ # # ]: 0 : while (dwarf_nextcu (dbg, offset = ncu, &ncu, &hsize, NULL, NULL, NULL) == 0)
10539 : : {
10540 : 0 : Dwarf_Die cudie;
10541 [ # # ]: 0 : if (dwarf_offdie (dbg, offset + hsize, &cudie) == NULL)
10542 : 0 : continue;
10543 : :
10544 : 0 : Dwarf_Attribute attr;
10545 [ # # ]: 0 : if (dwarf_attr (&cudie, DW_AT_macro_info, &attr) == NULL)
10546 : 0 : continue;
10547 : :
10548 : 0 : Dwarf_Word macoff;
10549 [ # # ]: 0 : if (dwarf_formudata (&attr, &macoff) != 0)
10550 : 0 : continue;
10551 : :
10552 : 0 : struct mac_culist *newp = (struct mac_culist *) alloca (sizeof (*newp));
10553 : 0 : newp->die = cudie;
10554 : 0 : newp->offset = macoff;
10555 : 0 : newp->files = NULL;
10556 : 0 : newp->next = culist;
10557 : 0 : culist = newp;
10558 : 0 : ++nculist;
10559 : : }
10560 : :
10561 : : /* Convert the list into an array for easier consumption. */
10562 : 0 : struct mac_culist *cus = (struct mac_culist *) alloca ((nculist + 1)
10563 : : * sizeof (*cus));
10564 : : /* Add sentinel. */
10565 : 0 : cus[nculist].offset = data->d_size;
10566 : 0 : cus[nculist].files = (Dwarf_Files *) -1l;
10567 [ # # ]: 0 : if (nculist > 0)
10568 : : {
10569 [ # # ]: 0 : for (size_t cnt = nculist - 1; culist != NULL; --cnt)
10570 : : {
10571 [ # # ]: 0 : assert (cnt < nculist);
10572 : 0 : cus[cnt] = *culist;
10573 : 0 : culist = culist->next;
10574 : : }
10575 : :
10576 : : /* Sort the array according to the offset in the .debug_macinfo
10577 : : section. Note we keep the sentinel at the end. */
10578 : 0 : qsort (cus, nculist, sizeof (*cus), mac_compare);
10579 : : }
10580 : :
10581 : 0 : const unsigned char *readp = (const unsigned char *) data->d_buf;
10582 : 0 : const unsigned char *readendp = readp + data->d_size;
10583 : 0 : int level = 1;
10584 : :
10585 : 0 : while (readp < readendp)
10586 : : {
10587 : 0 : unsigned int opcode = *readp++;
10588 : 0 : unsigned int u128;
10589 : 0 : unsigned int u128_2;
10590 : 0 : const unsigned char *endp;
10591 : :
10592 [ # # # # ]: 0 : switch (opcode)
10593 : : {
10594 : 0 : case DW_MACINFO_define:
10595 : : case DW_MACINFO_undef:
10596 : : case DW_MACINFO_vendor_ext:
10597 : : /* For the first two opcodes the parameters are
10598 : : line, string
10599 : : For the latter
10600 : : number, string.
10601 : : We can treat these cases together. */
10602 : 0 : get_uleb128 (u128, readp, readendp);
10603 : :
10604 : 0 : endp = memchr (readp, '\0', readendp - readp);
10605 [ # # ]: 0 : if (unlikely (endp == NULL))
10606 : : {
10607 : 0 : printf (_("\
10608 : : %*s*** non-terminated string at end of section"),
10609 : : level, "");
10610 : 0 : return;
10611 : : }
10612 : :
10613 [ # # ]: 0 : if (opcode == DW_MACINFO_define)
10614 : 0 : printf ("%*s#define %s, line %u\n",
10615 : : level, "", (char *) readp, u128);
10616 [ # # ]: 0 : else if (opcode == DW_MACINFO_undef)
10617 : 0 : printf ("%*s#undef %s, line %u\n",
10618 : : level, "", (char *) readp, u128);
10619 : : else
10620 : 0 : printf (" #vendor-ext %s, number %u\n", (char *) readp, u128);
10621 : :
10622 : 0 : readp = endp + 1;
10623 : 0 : break;
10624 : :
10625 : 0 : case DW_MACINFO_start_file:
10626 : : /* The two parameters are line and file index, in this order. */
10627 : 0 : get_uleb128 (u128, readp, readendp);
10628 [ # # ]: 0 : if (readendp - readp < 1)
10629 : : {
10630 : 0 : printf (_("\
10631 : : %*s*** missing DW_MACINFO_start_file argument at end of section"),
10632 : : level, "");
10633 : 0 : return;
10634 : : }
10635 : 0 : get_uleb128 (u128_2, readp, readendp);
10636 : :
10637 : : /* Find the CU DIE for this file. */
10638 : 0 : size_t macoff = readp - (const unsigned char *) data->d_buf;
10639 : 0 : const char *fname = "???";
10640 [ # # # # ]: 0 : if (macoff >= cus[0].offset && cus[0].offset != data->d_size)
10641 : : {
10642 [ # # # # ]: 0 : while (macoff >= cus[1].offset && cus[1].offset != data->d_size)
10643 : 0 : ++cus;
10644 : :
10645 [ # # ]: 0 : if (cus[0].files == NULL
10646 [ # # ]: 0 : && dwarf_getsrcfiles (&cus[0].die, &cus[0].files, NULL) != 0)
10647 : 0 : cus[0].files = (Dwarf_Files *) -1l;
10648 : :
10649 [ # # ]: 0 : if (cus[0].files != (Dwarf_Files *) -1l)
10650 : 0 : fname = (dwarf_filesrc (cus[0].files, u128_2, NULL, NULL)
10651 [ # # ]: 0 : ?: "???");
10652 : : }
10653 : :
10654 : 0 : printf ("%*sstart_file %u, [%u] %s\n",
10655 : : level, "", u128, u128_2, fname);
10656 : 0 : ++level;
10657 : 0 : break;
10658 : :
10659 : 0 : case DW_MACINFO_end_file:
10660 : 0 : --level;
10661 : 0 : printf ("%*send_file\n", level, "");
10662 : : /* Nothing more to do. */
10663 : : break;
10664 : :
10665 : 0 : default:
10666 : : // XXX gcc seems to generate files with a trailing zero.
10667 [ # # # # ]: 0 : if (unlikely (opcode != 0 || readp != readendp))
10668 [ # # ]: 0 : printf ("%*s*** invalid opcode %u\n", level, "", opcode);
10669 : : break;
10670 : : }
10671 : : }
10672 : : }
10673 : :
10674 : :
10675 : : static void
10676 : 8 : print_debug_macro_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
10677 : : Ebl *ebl,
10678 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
10679 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
10680 : : {
10681 : 8 : Elf_Data *data = get_debug_elf_data (dbg, ebl, IDX_debug_macro, scn);
10682 [ + - ]: 8 : if (data == NULL)
10683 : 0 : return;
10684 : :
10685 : 8 : printf (_("\
10686 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
10687 : : elf_ndxscn (scn), section_name (ebl, shdr),
10688 : 8 : (uint64_t) shdr->sh_offset);
10689 : 8 : fputc ('\n', stdout);
10690 : :
10691 : : /* Get the source file information for all CUs. Uses same
10692 : : datastructure as macinfo. But uses offset field to directly
10693 : : match .debug_line offset. And just stored in a list. */
10694 : 8 : Dwarf_Off offset;
10695 : 8 : Dwarf_Off ncu = 0;
10696 : 8 : size_t hsize;
10697 : 8 : struct mac_culist *culist = NULL;
10698 [ + + ]: 20 : while (dwarf_nextcu (dbg, offset = ncu, &ncu, &hsize, NULL, NULL, NULL) == 0)
10699 : : {
10700 : 12 : Dwarf_Die cudie;
10701 [ - + ]: 12 : if (dwarf_offdie (dbg, offset + hsize, &cudie) == NULL)
10702 : 0 : continue;
10703 : :
10704 : 12 : Dwarf_Attribute attr;
10705 [ - + ]: 12 : if (dwarf_attr (&cudie, DW_AT_stmt_list, &attr) == NULL)
10706 : 0 : continue;
10707 : :
10708 : 12 : Dwarf_Word lineoff;
10709 [ - + ]: 12 : if (dwarf_formudata (&attr, &lineoff) != 0)
10710 : 0 : continue;
10711 : :
10712 : 12 : struct mac_culist *newp = (struct mac_culist *) alloca (sizeof (*newp));
10713 : 12 : newp->die = cudie;
10714 : 12 : newp->offset = lineoff;
10715 : 12 : newp->files = NULL;
10716 : 12 : newp->next = culist;
10717 : 12 : culist = newp;
10718 : : }
10719 : :
10720 : 8 : const unsigned char *readp = (const unsigned char *) data->d_buf;
10721 : 8 : const unsigned char *readendp = readp + data->d_size;
10722 : :
10723 [ + + ]: 28 : while (readp < readendp)
10724 : : {
10725 : 40 : printf (_(" Offset: 0x%" PRIx64 "\n"),
10726 : 20 : (uint64_t) (readp - (const unsigned char *) data->d_buf));
10727 : :
10728 : : // Header, 2 byte version, 1 byte flag, optional .debug_line offset,
10729 : : // optional vendor extension macro entry table.
10730 [ - + ]: 20 : if (readp + 2 > readendp)
10731 : : {
10732 : 0 : invalid_data:
10733 : 0 : error (0, 0, _("invalid data"));
10734 : 0 : return;
10735 : : }
10736 [ - + ]: 20 : const uint16_t vers = read_2ubyte_unaligned_inc (dbg, readp);
10737 : 20 : printf (_(" Version: %" PRIu16 "\n"), vers);
10738 : :
10739 : : // Version 4 is the GNU extension for DWARF4. DWARF5 will use version
10740 : : // 5 when it gets standardized.
10741 [ - + ]: 20 : if (vers != 4 && vers != 5)
10742 : : {
10743 : 0 : printf (_(" unknown version, cannot parse section\n"));
10744 : 0 : return;
10745 : : }
10746 : :
10747 [ - + ]: 20 : if (readp + 1 > readendp)
10748 : 0 : goto invalid_data;
10749 : 20 : const unsigned char flag = *readp++;
10750 : 20 : printf (_(" Flag: 0x%" PRIx8), flag);
10751 [ + + ]: 20 : if (flag != 0)
10752 : : {
10753 : 12 : printf (" (");
10754 [ - + ]: 12 : if ((flag & 0x01) != 0)
10755 : : {
10756 : 0 : printf ("offset_size");
10757 [ # # ]: 0 : if ((flag & 0xFE) != 0)
10758 : 0 : printf (", ");
10759 : : }
10760 [ + - ]: 12 : if ((flag & 0x02) != 0)
10761 : : {
10762 : 12 : printf ("debug_line_offset");
10763 [ - + ]: 12 : if ((flag & 0xFC) != 0)
10764 : 0 : printf (", ");
10765 : : }
10766 [ # # ]: 0 : if ((flag & 0x04) != 0)
10767 : : {
10768 : 0 : printf ("operands_table");
10769 [ # # ]: 0 : if ((flag & 0xF8) != 0)
10770 : 0 : printf (", ");
10771 : : }
10772 [ - + ]: 12 : if ((flag & 0xF8) != 0)
10773 : 0 : printf ("unknown");
10774 : 12 : printf (")");
10775 : : }
10776 : 20 : printf ("\n");
10777 : :
10778 [ + - ]: 20 : unsigned int offset_len = (flag & 0x01) ? 8 : 4;
10779 : 20 : printf (_(" Offset length: %" PRIu8 "\n"), offset_len);
10780 : 20 : Dwarf_Off line_offset = -1;
10781 [ + + ]: 20 : if (flag & 0x02)
10782 : : {
10783 [ - + ]: 12 : if (offset_len == 8)
10784 [ # # ]: 0 : line_offset = read_8ubyte_unaligned_inc (dbg, readp);
10785 : : else
10786 [ - + ]: 12 : line_offset = read_4ubyte_unaligned_inc (dbg, readp);
10787 : 12 : printf (_(" .debug_line offset: 0x%" PRIx64 "\n"),
10788 : : line_offset);
10789 : : }
10790 : :
10791 : 12 : struct mac_culist *cu = NULL;
10792 [ + - ]: 12 : if (line_offset != (Dwarf_Off) -1)
10793 : : {
10794 : : cu = culist;
10795 [ + - + + ]: 16 : while (cu != NULL && line_offset != cu->offset)
10796 : 4 : cu = cu->next;
10797 : : }
10798 : :
10799 [ - + ]: 20 : Dwarf_Off str_offsets_base = str_offsets_base_off (dbg, (cu != NULL
10800 : : ? cu->die.cu
10801 : : : NULL));
10802 : :
10803 : 20 : const unsigned char *vendor[DW_MACRO_hi_user - DW_MACRO_lo_user + 1];
10804 [ - + ]: 20 : memset (vendor, 0, sizeof vendor);
10805 [ - + ]: 20 : if (flag & 0x04)
10806 : : {
10807 : : // 1 byte length, for each item, 1 byte opcode, uleb128 number
10808 : : // of arguments, for each argument 1 byte form code.
10809 [ # # ]: 0 : if (readp + 1 > readendp)
10810 : 0 : goto invalid_data;
10811 : 0 : unsigned int tlen = *readp++;
10812 : 0 : printf (_(" extension opcode table, %" PRIu8 " items:\n"),
10813 : : tlen);
10814 [ # # ]: 0 : for (unsigned int i = 0; i < tlen; i++)
10815 : : {
10816 [ # # ]: 0 : if (readp + 1 > readendp)
10817 : 0 : goto invalid_data;
10818 : 0 : unsigned int opcode = *readp++;
10819 : 0 : printf (_(" [%" PRIx8 "]"), opcode);
10820 : 0 : if (opcode < DW_MACRO_lo_user
10821 [ # # ]: 0 : || opcode > DW_MACRO_hi_user)
10822 : 0 : goto invalid_data;
10823 : : // Record the start of description for this vendor opcode.
10824 : : // uleb128 nr args, 1 byte per arg form.
10825 : 0 : vendor[opcode - DW_MACRO_lo_user] = readp;
10826 [ # # ]: 0 : if (readp + 1 > readendp)
10827 : 0 : goto invalid_data;
10828 : 0 : unsigned int args = *readp++;
10829 [ # # ]: 0 : if (args > 0)
10830 : : {
10831 : 0 : printf (_(" %" PRIu8 " arguments:"), args);
10832 : 0 : while (args > 0)
10833 : : {
10834 : 0 : if (readp + 1 > readendp)
10835 : 0 : goto invalid_data;
10836 : 0 : unsigned int form = *readp++;
10837 : 0 : printf (" %s", dwarf_form_name (form));
10838 [ # # ]: 0 : if (! libdw_valid_user_form (form))
10839 : 0 : goto invalid_data;
10840 : 0 : args--;
10841 [ # # ]: 0 : if (args > 0)
10842 [ # # ]: 0 : putchar (',');
10843 : : }
10844 : : }
10845 : : else
10846 : 0 : printf (_(" no arguments."));
10847 : 0 : putchar ('\n');
10848 : : }
10849 : : }
10850 : 20 : putchar ('\n');
10851 : :
10852 : 20 : int level = 1;
10853 [ - + ]: 20 : if (readp + 1 > readendp)
10854 : 0 : goto invalid_data;
10855 : 20 : unsigned int opcode = *readp++;
10856 : 20 : while (opcode != 0)
10857 : : {
10858 : 3026 : unsigned int u128;
10859 : 3026 : unsigned int u128_2;
10860 : 3026 : const unsigned char *endp;
10861 : 3026 : uint64_t off;
10862 : :
10863 [ + + + - : 3026 : switch (opcode)
+ + + - -
- + + - ]
10864 : : {
10865 : 20 : case DW_MACRO_start_file:
10866 : 20 : get_uleb128 (u128, readp, readendp);
10867 [ - + ]: 20 : if (readp >= readendp)
10868 : 0 : goto invalid_data;
10869 : 20 : get_uleb128 (u128_2, readp, readendp);
10870 : :
10871 : : /* Find the CU DIE that matches this line offset. */
10872 : 20 : const char *fname = "???";
10873 [ - + ]: 20 : if (cu != NULL)
10874 : : {
10875 [ + + ]: 20 : if (cu->files == NULL
10876 [ - + ]: 12 : && dwarf_getsrcfiles (&cu->die, &cu->files,
10877 : : NULL) != 0)
10878 : 0 : cu->files = (Dwarf_Files *) -1l;
10879 : :
10880 [ - + ]: 20 : if (cu->files != (Dwarf_Files *) -1l)
10881 : 20 : fname = (dwarf_filesrc (cu->files, u128_2,
10882 [ - + ]: 20 : NULL, NULL) ?: "???");
10883 : : }
10884 : 20 : printf ("%*sstart_file %u, [%u] %s\n",
10885 : : level, "", u128, u128_2, fname);
10886 : 20 : ++level;
10887 : 20 : break;
10888 : :
10889 : 20 : case DW_MACRO_end_file:
10890 : 20 : --level;
10891 : 20 : printf ("%*send_file\n", level, "");
10892 : : break;
10893 : :
10894 : 2 : case DW_MACRO_define:
10895 : 2 : get_uleb128 (u128, readp, readendp);
10896 : 2 : endp = memchr (readp, '\0', readendp - readp);
10897 [ - + ]: 2 : if (endp == NULL)
10898 : 0 : goto invalid_data;
10899 : 2 : printf ("%*s#define %s, line %u\n",
10900 : : level, "", readp, u128);
10901 : 2 : readp = endp + 1;
10902 : 2 : break;
10903 : :
10904 : 0 : case DW_MACRO_undef:
10905 : 0 : get_uleb128 (u128, readp, readendp);
10906 : 0 : endp = memchr (readp, '\0', readendp - readp);
10907 [ # # ]: 0 : if (endp == NULL)
10908 : 0 : goto invalid_data;
10909 : 0 : printf ("%*s#undef %s, line %u\n",
10910 : : level, "", readp, u128);
10911 : 0 : readp = endp + 1;
10912 : 0 : break;
10913 : :
10914 : 1414 : case DW_MACRO_define_strp:
10915 : 1414 : get_uleb128 (u128, readp, readendp);
10916 [ - + ]: 1414 : if (readp + offset_len > readendp)
10917 : 0 : goto invalid_data;
10918 [ - + ]: 1414 : if (offset_len == 8)
10919 [ # # ]: 0 : off = read_8ubyte_unaligned_inc (dbg, readp);
10920 : : else
10921 [ - + ]: 1414 : off = read_4ubyte_unaligned_inc (dbg, readp);
10922 : 1414 : printf ("%*s#define %s, line %u (indirect)\n",
10923 : : level, "", dwarf_getstring (dbg, off, NULL), u128);
10924 : : break;
10925 : :
10926 : 2 : case DW_MACRO_undef_strp:
10927 : 2 : get_uleb128 (u128, readp, readendp);
10928 [ - + ]: 2 : if (readp + offset_len > readendp)
10929 : 0 : goto invalid_data;
10930 [ - + ]: 2 : if (offset_len == 8)
10931 [ # # ]: 0 : off = read_8ubyte_unaligned_inc (dbg, readp);
10932 : : else
10933 [ - + ]: 2 : off = read_4ubyte_unaligned_inc (dbg, readp);
10934 : 2 : printf ("%*s#undef %s, line %u (indirect)\n",
10935 : : level, "", dwarf_getstring (dbg, off, NULL), u128);
10936 : : break;
10937 : :
10938 : 12 : case DW_MACRO_import:
10939 [ - + ]: 12 : if (readp + offset_len > readendp)
10940 : 0 : goto invalid_data;
10941 [ - + ]: 12 : if (offset_len == 8)
10942 [ # # ]: 0 : off = read_8ubyte_unaligned_inc (dbg, readp);
10943 : : else
10944 [ - + ]: 12 : off = read_4ubyte_unaligned_inc (dbg, readp);
10945 : 12 : printf ("%*s#include offset 0x%" PRIx64 "\n",
10946 : : level, "", off);
10947 : : break;
10948 : :
10949 : 0 : case DW_MACRO_define_sup:
10950 : 0 : get_uleb128 (u128, readp, readendp);
10951 : 0 : printf ("%*s#define ", level, "");
10952 : 0 : readp = print_form_data (dbg, DW_FORM_strp_sup,
10953 : : readp, readendp, offset_len,
10954 : : str_offsets_base);
10955 : 0 : printf (", line %u (sup)\n", u128);
10956 : : break;
10957 : :
10958 : 0 : case DW_MACRO_undef_sup:
10959 : 0 : get_uleb128 (u128, readp, readendp);
10960 : 0 : printf ("%*s#undef ", level, "");
10961 : 0 : readp = print_form_data (dbg, DW_FORM_strp_sup,
10962 : : readp, readendp, offset_len,
10963 : : str_offsets_base);
10964 : 0 : printf (", line %u (sup)\n", u128);
10965 : : break;
10966 : :
10967 : 0 : case DW_MACRO_import_sup:
10968 [ # # ]: 0 : if (readp + offset_len > readendp)
10969 : 0 : goto invalid_data;
10970 [ # # ]: 0 : if (offset_len == 8)
10971 [ # # ]: 0 : off = read_8ubyte_unaligned_inc (dbg, readp);
10972 : : else
10973 [ # # ]: 0 : off = read_4ubyte_unaligned_inc (dbg, readp);
10974 : : // XXX Needs support for reading from supplementary object file.
10975 : 0 : printf ("%*s#include offset 0x%" PRIx64 " (sup)\n",
10976 : : level, "", off);
10977 : : break;
10978 : :
10979 : 1552 : case DW_MACRO_define_strx:
10980 : 1552 : get_uleb128 (u128, readp, readendp);
10981 : 1552 : printf ("%*s#define ", level, "");
10982 : 1552 : readp = print_form_data (dbg, DW_FORM_strx,
10983 : : readp, readendp, offset_len,
10984 : : str_offsets_base);
10985 : 1552 : printf (", line %u (strx)\n", u128);
10986 : : break;
10987 : :
10988 : 4 : case DW_MACRO_undef_strx:
10989 : 4 : get_uleb128 (u128, readp, readendp);
10990 : 4 : printf ("%*s#undef ", level, "");
10991 : 4 : readp = print_form_data (dbg, DW_FORM_strx,
10992 : : readp, readendp, offset_len,
10993 : : str_offsets_base);
10994 : 4 : printf (", line %u (strx)\n", u128);
10995 : : break;
10996 : :
10997 : : default:
10998 : 0 : printf ("%*svendor opcode 0x%" PRIx8, level, "", opcode);
10999 [ # # ]: 0 : if (opcode < DW_MACRO_lo_user
11000 : : || opcode > DW_MACRO_lo_user
11001 [ # # ]: 0 : || vendor[opcode - DW_MACRO_lo_user] == NULL)
11002 : 0 : goto invalid_data;
11003 : :
11004 : 0 : const unsigned char *op_desc;
11005 : 0 : op_desc = vendor[opcode - DW_MACRO_lo_user];
11006 : :
11007 : : // Just skip the arguments, we cannot really interpret them,
11008 : : // but print as much as we can.
11009 : 0 : unsigned int args = *op_desc++;
11010 [ # # ]: 0 : while (args > 0 && readp < readendp)
11011 : : {
11012 : 0 : unsigned int form = *op_desc++;
11013 : 0 : readp = print_form_data (dbg, form, readp, readendp,
11014 : : offset_len, str_offsets_base);
11015 : 0 : args--;
11016 [ # # ]: 0 : if (args > 0)
11017 [ # # ]: 0 : printf (", ");
11018 : : }
11019 : 0 : putchar ('\n');
11020 : : }
11021 : :
11022 [ - + ]: 3026 : if (readp + 1 > readendp)
11023 : 0 : goto invalid_data;
11024 : 3026 : opcode = *readp++;
11025 [ + + ]: 3026 : if (opcode == 0)
11026 [ + + ]: 3066 : putchar ('\n');
11027 : : }
11028 : : }
11029 : : }
11030 : :
11031 : :
11032 : : /* Callback for printing global names. */
11033 : : static int
11034 : 68 : print_pubnames (Dwarf *dbg __attribute__ ((unused)), Dwarf_Global *global,
11035 : : void *arg)
11036 : : {
11037 : 68 : int *np = (int *) arg;
11038 : :
11039 : 136 : printf (_(" [%5d] DIE offset: %6" PRId64
11040 : : ", CU DIE offset: %6" PRId64 ", name: %s\n"),
11041 : 68 : (*np)++, global->die_offset, global->cu_offset, global->name);
11042 : :
11043 : 68 : return 0;
11044 : : }
11045 : :
11046 : :
11047 : : /* Print the known exported symbols in the DWARF section '.debug_pubnames'. */
11048 : : static void
11049 : 16 : print_debug_pubnames_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
11050 : : Ebl *ebl,
11051 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
11052 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
11053 : : {
11054 : : /* Check section actually exists. */
11055 [ - + ]: 16 : if (get_debug_elf_data (dbg, ebl, IDX_debug_pubnames, scn) == NULL)
11056 : 0 : return;
11057 : :
11058 : 16 : printf (_("\nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
11059 : : elf_ndxscn (scn), section_name (ebl, shdr),
11060 : 16 : (uint64_t) shdr->sh_offset);
11061 : :
11062 : 16 : int n = 0;
11063 : 16 : (void) dwarf_getpubnames (dbg, print_pubnames, &n, 0);
11064 : : }
11065 : :
11066 : : /* Print the content of the DWARF string section '.debug_str'
11067 : : or 'debug_line_str'. */
11068 : : static void
11069 : 134 : print_debug_str_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
11070 : : Ebl *ebl,
11071 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
11072 : : Elf_Scn *scn, GElf_Shdr *shdr,
11073 : : Dwarf *dbg __attribute__ ((unused)))
11074 : : {
11075 : 134 : const char *name = section_name (ebl, shdr);
11076 [ + + ]: 134 : int idx = ((name != NULL && strstr (name, "debug_line_str") != NULL)
11077 [ + - ]: 134 : ? IDX_debug_line_str : IDX_debug_str);
11078 : 134 : Elf_Data *data = get_debug_elf_data (dbg, ebl, idx, scn);
11079 [ + - ]: 134 : if (data == NULL)
11080 : : return;
11081 : :
11082 : 134 : const size_t sh_size = data->d_size;
11083 : :
11084 : : /* Compute floor(log16(shdr->sh_size)). */
11085 : 134 : GElf_Addr tmp = sh_size;
11086 : 134 : int digits = 1;
11087 [ + + ]: 466 : while (tmp >= 16)
11088 : : {
11089 : 332 : ++digits;
11090 : 332 : tmp >>= 4;
11091 : : }
11092 : 134 : digits = MAX (4, digits);
11093 : :
11094 : 134 : printf (_("\nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"
11095 : : " %*s String\n"),
11096 : : elf_ndxscn (scn),
11097 : 134 : section_name (ebl, shdr), (uint64_t) shdr->sh_offset,
11098 : : /* TRANS: the debugstr| prefix makes the string unique. */
11099 [ + - ]: 134 : digits + 2, sgettext ("debugstr|Offset"));
11100 : :
11101 : 134 : Dwarf_Off offset = 0;
11102 [ + + ]: 162064 : while (offset < sh_size)
11103 : : {
11104 : 161930 : size_t len;
11105 : 161930 : const char *str = (const char *) data->d_buf + offset;
11106 : 161930 : const char *endp = memchr (str, '\0', sh_size - offset);
11107 [ - + ]: 161930 : if (unlikely (endp == NULL))
11108 : : {
11109 : 0 : printf (_(" *** error, missing string terminator\n"));
11110 : : break;
11111 : : }
11112 : :
11113 : 161930 : printf (" [%*" PRIx64 "] \"%s\"\n", digits, (uint64_t) offset, str);
11114 : 161930 : len = endp - str;
11115 : 161930 : offset += len + 1;
11116 : : }
11117 : : }
11118 : :
11119 : : static void
11120 : 12 : print_debug_str_offsets_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
11121 : : Ebl *ebl,
11122 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
11123 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
11124 : : {
11125 : 12 : Elf_Data *data = get_debug_elf_data (dbg, ebl, IDX_debug_str_offsets, scn);
11126 [ + - ]: 12 : if (data == NULL)
11127 : : return;
11128 : :
11129 : 12 : printf (_("\
11130 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
11131 : : elf_ndxscn (scn), section_name (ebl, shdr),
11132 : 12 : (uint64_t) shdr->sh_offset);
11133 : :
11134 [ + - ]: 12 : if (shdr->sh_size == 0)
11135 : : return;
11136 : :
11137 : 12 : size_t idx = 0;
11138 : 12 : sort_listptr (&known_stroffbases, "str_offsets");
11139 : :
11140 : 12 : const unsigned char *start = (const unsigned char *) data->d_buf;
11141 : 12 : const unsigned char *readp = start;
11142 : 12 : const unsigned char *readendp = ((const unsigned char *) data->d_buf
11143 : 12 : + data->d_size);
11144 : :
11145 : 12 : while (readp < readendp)
11146 : : {
11147 : : /* Most string offset tables will have a header. For split
11148 : : dwarf unit GNU DebugFission didn't add one. But they were
11149 : : also only defined for split units (main or skeleton units
11150 : : didn't have indirect strings). So if we don't have a
11151 : : DW_AT_str_offsets_base at all and this is offset zero, then
11152 : : just start printing offsets immediately, if this is a .dwo
11153 : : section. */
11154 : 16 : Dwarf_Off off = (Dwarf_Off) (readp
11155 : 16 : - (const unsigned char *) data->d_buf);
11156 : :
11157 : 16 : printf ("Table at offset %" PRIx64 " ", off);
11158 : :
11159 [ + + ]: 16 : struct listptr *listptr = get_listptr (&known_stroffbases, idx++);
11160 : 16 : const unsigned char *next_unitp = readendp;
11161 : 8 : uint8_t offset_size;
11162 : 8 : bool has_header;
11163 [ - + ]: 8 : if (listptr == NULL)
11164 : : {
11165 : : /* This can happen for .dwo files. There is only an header
11166 : : in the case this is a version 5 split DWARF file. */
11167 : 8 : Dwarf_CU *cu;
11168 : 8 : uint8_t unit_type;
11169 [ - + ]: 8 : if (dwarf_get_units (dbg, NULL, &cu, NULL, &unit_type,
11170 : : NULL, NULL) != 0)
11171 : : {
11172 : 0 : error (0, 0, "Warning: Cannot find any DWARF unit.");
11173 : : /* Just guess some values. */
11174 : 0 : has_header = false;
11175 : 0 : offset_size = 4;
11176 : : }
11177 [ + - ]: 8 : else if (off == 0
11178 : 8 : && (unit_type == DW_UT_split_type
11179 [ + - ]: 8 : || unit_type == DW_UT_split_compile))
11180 : : {
11181 : 8 : has_header = cu->version > 4;
11182 : 8 : offset_size = cu->offset_size;
11183 : : }
11184 : : else
11185 : : {
11186 : 0 : error (0, 0,
11187 : : "Warning: No CU references .debug_str_offsets after %"
11188 : : PRIx64, off);
11189 : 0 : has_header = cu->version > 4;
11190 : 0 : offset_size = cu->offset_size;
11191 : : }
11192 : 8 : printf ("\n");
11193 : : }
11194 : : else
11195 : : {
11196 : : /* This must be DWARF5, since GNU DebugFission didn't define
11197 : : DW_AT_str_offsets_base. */
11198 : 8 : has_header = true;
11199 : :
11200 : 8 : Dwarf_Die cudie;
11201 [ - + ]: 8 : if (dwarf_cu_die (listptr->cu, &cudie,
11202 : : NULL, NULL, NULL, NULL,
11203 : : NULL, NULL) == NULL)
11204 : 0 : printf ("Unknown CU (%s):\n", dwarf_errmsg (-1));
11205 : : else
11206 : 8 : printf ("for CU [%6" PRIx64 "]:\n", dwarf_dieoffset (&cudie));
11207 : : }
11208 : :
11209 [ + + ]: 16 : if (has_header)
11210 : : {
11211 : 12 : uint64_t unit_length;
11212 : 12 : uint16_t version;
11213 : 12 : uint16_t padding;
11214 : :
11215 [ - + ]: 12 : unit_length = read_4ubyte_unaligned_inc (dbg, readp);
11216 [ - + ]: 12 : if (unlikely (unit_length == 0xffffffff))
11217 : : {
11218 [ # # ]: 0 : if (unlikely (readp > readendp - 8))
11219 : : {
11220 : 0 : invalid_data:
11221 : 0 : error (0, 0, "Invalid data");
11222 : 0 : return;
11223 : : }
11224 [ # # ]: 0 : unit_length = read_8ubyte_unaligned_inc (dbg, readp);
11225 : 0 : offset_size = 8;
11226 : : }
11227 : : else
11228 : : offset_size = 4;
11229 : :
11230 : 12 : printf ("\n");
11231 : 12 : printf (_(" Length: %8" PRIu64 "\n"),
11232 : : unit_length);
11233 : 12 : printf (_(" Offset size: %8" PRIu8 "\n"),
11234 : : offset_size);
11235 : :
11236 : : /* We need at least 2-bytes (version) + 2-bytes (padding) =
11237 : : 4 bytes to complete the header. And this unit cannot go
11238 : : beyond the section data. */
11239 [ + - ]: 12 : if (readp > readendp - 4
11240 [ + - ]: 12 : || unit_length < 4
11241 [ - + ]: 12 : || unit_length > (uint64_t) (readendp - readp))
11242 : 0 : goto invalid_data;
11243 : :
11244 : 12 : next_unitp = readp + unit_length;
11245 : :
11246 [ - + ]: 12 : version = read_2ubyte_unaligned_inc (dbg, readp);
11247 : 12 : printf (_(" DWARF version: %8" PRIu16 "\n"), version);
11248 : :
11249 [ - + ]: 12 : if (version != 5)
11250 : : {
11251 : 0 : error (0, 0, _("Unknown version"));
11252 : 0 : goto next_unit;
11253 : : }
11254 : :
11255 [ - + ]: 12 : padding = read_2ubyte_unaligned_inc (dbg, readp);
11256 : 12 : printf (_(" Padding: %8" PRIx16 "\n"), padding);
11257 : :
11258 [ + + ]: 12 : if (listptr != NULL
11259 [ - + ]: 8 : && listptr->offset != (Dwarf_Off) (readp - start))
11260 : : {
11261 : 0 : error (0, 0, "String offsets index doesn't start after header");
11262 : 0 : goto next_unit;
11263 : : }
11264 : :
11265 : 12 : printf ("\n");
11266 : : }
11267 : :
11268 : 16 : int digits = 1;
11269 : 16 : size_t offsets = (next_unitp - readp) / offset_size;
11270 [ + + ]: 24 : while (offsets >= 10)
11271 : : {
11272 : 8 : ++digits;
11273 : 8 : offsets /= 10;
11274 : : }
11275 : :
11276 : 16 : unsigned int uidx = 0;
11277 : 16 : size_t index_offset = readp - (const unsigned char *) data->d_buf;
11278 : 16 : printf (" Offsets start at 0x%zx:\n", index_offset);
11279 : 204 : while (readp <= next_unitp - offset_size)
11280 : : {
11281 : 188 : Dwarf_Word offset;
11282 [ + - ]: 188 : if (offset_size == 4)
11283 [ - + ]: 188 : offset = read_4ubyte_unaligned_inc (dbg, readp);
11284 : : else
11285 [ # # ]: 0 : offset = read_8ubyte_unaligned_inc (dbg, readp);
11286 : 188 : const char *str = dwarf_getstring (dbg, offset, NULL);
11287 [ - + + + ]: 392 : printf (" [%*u] [%*" PRIx64 "] \"%s\"\n",
11288 : 188 : digits, uidx++, (int) offset_size * 2, offset, str ?: "???");
11289 : : }
11290 : 16 : printf ("\n");
11291 : :
11292 [ + - ]: 16 : if (readp != next_unitp)
11293 [ + + ]: 28 : error (0, 0, "extra %zd bytes at end of unit",
11294 : 0 : (size_t) (next_unitp - readp));
11295 : :
11296 : 16 : next_unit:
11297 : : readp = next_unitp;
11298 : : }
11299 : : }
11300 : :
11301 : :
11302 : : /* Print the content of the call frame search table section
11303 : : '.eh_frame_hdr'. */
11304 : : static void
11305 : 140 : print_debug_frame_hdr_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
11306 : : Ebl *ebl __attribute__ ((unused)),
11307 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
11308 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
11309 : : {
11310 : 140 : printf (_("\
11311 : : \nCall frame search table section [%2zu] '.eh_frame_hdr':\n"),
11312 : : elf_ndxscn (scn));
11313 : :
11314 : 140 : Elf_Data *data = elf_rawdata (scn, NULL);
11315 : :
11316 [ - + ]: 140 : if (unlikely (data == NULL))
11317 : : {
11318 : 0 : error (0, 0, _("cannot get %s content: %s"),
11319 : : ".eh_frame_hdr", elf_errmsg (-1));
11320 : 0 : return;
11321 : : }
11322 : :
11323 : 140 : const unsigned char *readp = data->d_buf;
11324 : 140 : const unsigned char *const dataend = ((unsigned char *) data->d_buf
11325 : 140 : + data->d_size);
11326 : :
11327 [ - + ]: 140 : if (unlikely (readp + 4 > dataend))
11328 : : {
11329 : 0 : invalid_data:
11330 : 0 : error (0, 0, _("invalid data"));
11331 : 0 : return;
11332 : : }
11333 : :
11334 : 140 : unsigned int version = *readp++;
11335 : 140 : unsigned int eh_frame_ptr_enc = *readp++;
11336 : 140 : unsigned int fde_count_enc = *readp++;
11337 : 140 : unsigned int table_enc = *readp++;
11338 : :
11339 : 140 : printf (" version: %u\n"
11340 : : " eh_frame_ptr_enc: %#x ",
11341 : : version, eh_frame_ptr_enc);
11342 : 140 : print_encoding_base ("", eh_frame_ptr_enc);
11343 : 140 : printf (" fde_count_enc: %#x ", fde_count_enc);
11344 : 140 : print_encoding_base ("", fde_count_enc);
11345 : 140 : printf (" table_enc: %#x ", table_enc);
11346 : 140 : print_encoding_base ("", table_enc);
11347 : :
11348 : 140 : uint64_t eh_frame_ptr = 0;
11349 [ + - ]: 140 : if (eh_frame_ptr_enc != DW_EH_PE_omit)
11350 : : {
11351 : 140 : readp = read_encoded (eh_frame_ptr_enc, readp, dataend, &eh_frame_ptr,
11352 : : dbg);
11353 [ - + ]: 140 : if (unlikely (readp == NULL))
11354 : 0 : goto invalid_data;
11355 : :
11356 : 140 : printf (" eh_frame_ptr: %#" PRIx64, eh_frame_ptr);
11357 [ + - ]: 140 : if ((eh_frame_ptr_enc & 0x70) == DW_EH_PE_pcrel)
11358 : 280 : printf (" (offset: %#" PRIx64 ")",
11359 : : /* +4 because of the 4 byte header of the section. */
11360 : 140 : (uint64_t) shdr->sh_offset + 4 + eh_frame_ptr);
11361 : :
11362 : 140 : putchar ('\n');
11363 : : }
11364 : :
11365 : 140 : uint64_t fde_count = 0;
11366 [ + - ]: 140 : if (fde_count_enc != DW_EH_PE_omit)
11367 : : {
11368 : 140 : readp = read_encoded (fde_count_enc, readp, dataend, &fde_count, dbg);
11369 [ - + ]: 140 : if (unlikely (readp == NULL))
11370 : 0 : goto invalid_data;
11371 : :
11372 : 140 : printf (" fde_count: %" PRIu64 "\n", fde_count);
11373 : : }
11374 : :
11375 [ + - + - ]: 140 : if (fde_count == 0 || table_enc == DW_EH_PE_omit)
11376 : : return;
11377 : :
11378 : 140 : puts (" Table:");
11379 : :
11380 : : /* Optimize for the most common case. */
11381 [ + - ]: 140 : if (table_enc == (DW_EH_PE_datarel | DW_EH_PE_sdata4))
11382 : 22536 : while (fde_count > 0 && readp + 8 <= dataend)
11383 : : {
11384 [ - + ]: 22396 : int32_t initial_location = read_4sbyte_unaligned_inc (dbg, readp);
11385 : 22396 : uint64_t initial_offset = ((uint64_t) shdr->sh_offset
11386 : 22396 : + (int64_t) initial_location);
11387 [ - + ]: 22396 : int32_t address = read_4sbyte_unaligned_inc (dbg, readp);
11388 : : // XXX Possibly print symbol name or section offset for initial_offset
11389 [ + + ]: 22536 : printf (" %#" PRIx32 " (offset: %#6" PRIx64 ") -> %#" PRIx32
11390 : : " fde=[%6" PRIx64 "]\n",
11391 : : initial_location, initial_offset,
11392 : 22396 : address, address - (eh_frame_ptr + 4));
11393 : : }
11394 : : else
11395 : 140 : while (0 && readp < dataend)
11396 : : {
11397 : :
11398 : 140 : }
11399 : : }
11400 : :
11401 : :
11402 : : /* Print the content of the exception handling table section
11403 : : '.eh_frame_hdr'. */
11404 : : static void
11405 : 0 : print_debug_exception_table (Dwfl_Module *dwflmod __attribute__ ((unused)),
11406 : : Ebl *ebl __attribute__ ((unused)),
11407 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
11408 : : Elf_Scn *scn,
11409 : : GElf_Shdr *shdr __attribute__ ((unused)),
11410 : : Dwarf *dbg __attribute__ ((unused)))
11411 : : {
11412 : 0 : printf (_("\
11413 : : \nException handling table section [%2zu] '.gcc_except_table':\n"),
11414 : : elf_ndxscn (scn));
11415 : :
11416 : 0 : Elf_Data *data = elf_rawdata (scn, NULL);
11417 : :
11418 [ # # ]: 0 : if (unlikely (data == NULL))
11419 : : {
11420 : 0 : error (0, 0, _("cannot get %s content: %s"),
11421 : : ".gcc_except_table", elf_errmsg (-1));
11422 : 0 : return;
11423 : : }
11424 : :
11425 : 0 : const unsigned char *readp = data->d_buf;
11426 : 0 : const unsigned char *const dataend = readp + data->d_size;
11427 : :
11428 [ # # ]: 0 : if (unlikely (readp + 1 > dataend))
11429 : : {
11430 : 0 : invalid_data:
11431 : 0 : error (0, 0, _("invalid data"));
11432 : 0 : return;
11433 : : }
11434 : 0 : unsigned int lpstart_encoding = *readp++;
11435 : 0 : printf (_(" LPStart encoding: %#x "), lpstart_encoding);
11436 : 0 : print_encoding_base ("", lpstart_encoding);
11437 [ # # ]: 0 : if (lpstart_encoding != DW_EH_PE_omit)
11438 : : {
11439 : 0 : uint64_t lpstart;
11440 : 0 : readp = read_encoded (lpstart_encoding, readp, dataend, &lpstart, dbg);
11441 : 0 : printf (" LPStart: %#" PRIx64 "\n", lpstart);
11442 : : }
11443 : :
11444 [ # # ]: 0 : if (unlikely (readp + 1 > dataend))
11445 : 0 : goto invalid_data;
11446 : 0 : unsigned int ttype_encoding = *readp++;
11447 : 0 : printf (_(" TType encoding: %#x "), ttype_encoding);
11448 : 0 : print_encoding_base ("", ttype_encoding);
11449 : 0 : const unsigned char *ttype_base = NULL;
11450 [ # # ]: 0 : if (ttype_encoding != DW_EH_PE_omit)
11451 : : {
11452 : 0 : unsigned int ttype_base_offset;
11453 [ # # ]: 0 : if (readp >= dataend)
11454 : 0 : goto invalid_data;
11455 : 0 : get_uleb128 (ttype_base_offset, readp, dataend);
11456 : 0 : printf (" TType base offset: %#x\n", ttype_base_offset);
11457 [ # # ]: 0 : if ((size_t) (dataend - readp) > ttype_base_offset)
11458 : 0 : ttype_base = readp + ttype_base_offset;
11459 : : }
11460 : :
11461 [ # # ]: 0 : if (unlikely (readp + 1 > dataend))
11462 : 0 : goto invalid_data;
11463 : 0 : unsigned int call_site_encoding = *readp++;
11464 : 0 : printf (_(" Call site encoding: %#x "), call_site_encoding);
11465 : 0 : print_encoding_base ("", call_site_encoding);
11466 : 0 : unsigned int call_site_table_len;
11467 [ # # ]: 0 : if (readp >= dataend)
11468 : 0 : goto invalid_data;
11469 : 0 : get_uleb128 (call_site_table_len, readp, dataend);
11470 : :
11471 : 0 : const unsigned char *const action_table = readp + call_site_table_len;
11472 [ # # ]: 0 : if (unlikely (action_table > dataend))
11473 : 0 : goto invalid_data;
11474 : : unsigned int u = 0;
11475 : : unsigned int max_action = 0;
11476 [ # # ]: 0 : while (readp < action_table)
11477 : : {
11478 [ # # ]: 0 : if (u == 0)
11479 : 0 : puts (_("\n Call site table:"));
11480 : :
11481 : 0 : uint64_t call_site_start;
11482 : 0 : readp = read_encoded (call_site_encoding, readp, dataend,
11483 : : &call_site_start, dbg);
11484 : 0 : uint64_t call_site_length;
11485 : 0 : readp = read_encoded (call_site_encoding, readp, dataend,
11486 : : &call_site_length, dbg);
11487 : 0 : uint64_t landing_pad;
11488 : 0 : readp = read_encoded (call_site_encoding, readp, dataend,
11489 : : &landing_pad, dbg);
11490 : 0 : unsigned int action;
11491 [ # # ]: 0 : if (readp >= dataend)
11492 : 0 : goto invalid_data;
11493 : 0 : get_uleb128 (action, readp, dataend);
11494 : 0 : max_action = MAX (action, max_action);
11495 : 0 : printf (_(" [%4u] Call site start: %#" PRIx64 "\n"
11496 : : " Call site length: %" PRIu64 "\n"
11497 : : " Landing pad: %#" PRIx64 "\n"
11498 : : " Action: %u\n"),
11499 : : u++, call_site_start, call_site_length, landing_pad, action);
11500 : : }
11501 [ # # ]: 0 : if (readp != action_table)
11502 : 0 : goto invalid_data;
11503 : :
11504 : 0 : unsigned int max_ar_filter = 0;
11505 [ # # ]: 0 : if (max_action > 0)
11506 : : {
11507 : 0 : puts ("\n Action table:");
11508 : :
11509 : 0 : size_t maxdata = (size_t) (dataend - action_table);
11510 [ # # # # ]: 0 : if (max_action > maxdata || maxdata - max_action < 1)
11511 : : {
11512 : 0 : invalid_action_table:
11513 : 0 : fputs (_(" <INVALID DATA>\n"), stdout);
11514 : 0 : return;
11515 : : }
11516 : :
11517 : 0 : const unsigned char *const action_table_end
11518 : 0 : = action_table + max_action + 1;
11519 : :
11520 : 0 : u = 0;
11521 : 0 : do
11522 : : {
11523 : 0 : int ar_filter;
11524 : 0 : get_sleb128 (ar_filter, readp, action_table_end);
11525 [ # # ]: 0 : if (ar_filter > 0 && (unsigned int) ar_filter > max_ar_filter)
11526 : 0 : max_ar_filter = ar_filter;
11527 : 0 : int ar_disp;
11528 [ # # ]: 0 : if (readp >= action_table_end)
11529 : 0 : goto invalid_action_table;
11530 : 0 : get_sleb128 (ar_disp, readp, action_table_end);
11531 : :
11532 : 0 : printf (" [%4u] ar_filter: % d\n"
11533 : : " ar_disp: % -5d",
11534 : : u, ar_filter, ar_disp);
11535 [ # # ]: 0 : if (abs (ar_disp) & 1)
11536 : 0 : printf (" -> [%4u]\n", u + (ar_disp + 1) / 2);
11537 [ # # ]: 0 : else if (ar_disp != 0)
11538 : 0 : puts (" -> ???");
11539 : : else
11540 : 0 : putchar ('\n');
11541 : 0 : ++u;
11542 : : }
11543 [ # # ]: 0 : while (readp < action_table_end);
11544 : : }
11545 : :
11546 [ # # ]: 0 : if (max_ar_filter > 0 && ttype_base != NULL)
11547 : : {
11548 : 0 : unsigned char dsize;
11549 : 0 : puts ("\n TType table:");
11550 : :
11551 : : // XXX Not *4, size of encoding;
11552 [ # # ]: 0 : switch (ttype_encoding & 7)
11553 : : {
11554 : : case DW_EH_PE_udata2:
11555 : : case DW_EH_PE_sdata2:
11556 : : dsize = 2;
11557 : : break;
11558 : : case DW_EH_PE_udata4:
11559 : : case DW_EH_PE_sdata4:
11560 : : dsize = 4;
11561 : : break;
11562 : : case DW_EH_PE_udata8:
11563 : : case DW_EH_PE_sdata8:
11564 : : dsize = 8;
11565 : : break;
11566 : 0 : default:
11567 : 0 : dsize = 0;
11568 : 0 : error (1, 0, _("invalid TType encoding"));
11569 : : }
11570 : :
11571 : 0 : if (max_ar_filter
11572 [ # # ]: 0 : > (size_t) (ttype_base - (const unsigned char *) data->d_buf) / dsize)
11573 : 0 : goto invalid_data;
11574 : :
11575 : 0 : readp = ttype_base - max_ar_filter * dsize;
11576 : 0 : do
11577 : : {
11578 : 0 : uint64_t ttype;
11579 : 0 : readp = read_encoded (ttype_encoding, readp, ttype_base, &ttype,
11580 : : dbg);
11581 : 0 : printf (" [%4u] %#" PRIx64 "\n", max_ar_filter--, ttype);
11582 : : }
11583 [ # # ]: 0 : while (readp < ttype_base);
11584 : : }
11585 : : }
11586 : :
11587 : : /* Print the content of the '.gdb_index' section.
11588 : : http://sourceware.org/gdb/current/onlinedocs/gdb/Index-Section-Format.html
11589 : : */
11590 : : static void
11591 : 8 : print_gdb_index_section (Dwfl_Module *dwflmod, Ebl *ebl,
11592 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
11593 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
11594 : : {
11595 : 8 : printf (_("\nGDB section [%2zu] '%s' at offset %#" PRIx64
11596 : : " contains %" PRId64 " bytes :\n"),
11597 : : elf_ndxscn (scn), section_name (ebl, shdr),
11598 : 8 : (uint64_t) shdr->sh_offset, (uint64_t) shdr->sh_size);
11599 : :
11600 : 8 : Elf_Data *data = elf_rawdata (scn, NULL);
11601 : :
11602 [ - + ]: 8 : if (unlikely (data == NULL))
11603 : : {
11604 : 0 : error (0, 0, _("cannot get %s content: %s"),
11605 : : ".gdb_index", elf_errmsg (-1));
11606 : 4 : return;
11607 : : }
11608 : :
11609 : : // .gdb_index is always in little endian.
11610 : 8 : Dwarf dummy_dbg = { .other_byte_order = MY_ELFDATA != ELFDATA2LSB };
11611 : 8 : dbg = &dummy_dbg;
11612 : :
11613 : 8 : const unsigned char *readp = data->d_buf;
11614 : 8 : const unsigned char *const dataend = readp + data->d_size;
11615 : :
11616 [ - + ]: 8 : if (unlikely (readp + 4 > dataend))
11617 : : {
11618 : 0 : invalid_data:
11619 : 0 : error (0, 0, _("invalid data"));
11620 : 0 : return;
11621 : : }
11622 : :
11623 : 8 : int32_t vers = read_4ubyte_unaligned (dbg, readp);
11624 : 8 : printf (_(" Version: %" PRId32 "\n"), vers);
11625 : :
11626 : : // The only difference between version 4 and version 5 is the
11627 : : // hash used for generating the table. Version 6 contains symbols
11628 : : // for inlined functions, older versions didn't. Version 7 adds
11629 : : // symbol kinds. Version 8 just indicates that it correctly includes
11630 : : // TUs for symbols. Version 9 adds shortcut table for information
11631 : : // regarding the main function.
11632 [ - + ]: 8 : if (vers < 4 || vers > 9)
11633 : : {
11634 : 0 : printf (_(" unknown version, cannot parse section\n"));
11635 : 0 : return;
11636 : : }
11637 : :
11638 : 8 : readp += 4;
11639 [ - + ]: 8 : if (unlikely (readp + 4 > dataend))
11640 : 0 : goto invalid_data;
11641 : :
11642 : 8 : uint32_t cu_off = read_4ubyte_unaligned (dbg, readp);
11643 : 8 : printf (_(" CU offset: %#" PRIx32 "\n"), cu_off);
11644 : :
11645 : 8 : readp += 4;
11646 [ - + ]: 8 : if (unlikely (readp + 4 > dataend))
11647 : 0 : goto invalid_data;
11648 : :
11649 : 8 : uint32_t tu_off = read_4ubyte_unaligned (dbg, readp);
11650 : 8 : printf (_(" TU offset: %#" PRIx32 "\n"), tu_off);
11651 : :
11652 : 8 : readp += 4;
11653 [ - + ]: 8 : if (unlikely (readp + 4 > dataend))
11654 : 0 : goto invalid_data;
11655 : :
11656 : 8 : uint32_t addr_off = read_4ubyte_unaligned (dbg, readp);
11657 : 8 : printf (_(" address offset: %#" PRIx32 "\n"), addr_off);
11658 : :
11659 : 8 : readp += 4;
11660 [ - + ]: 8 : if (unlikely (readp + 4 > dataend))
11661 : 0 : goto invalid_data;
11662 : :
11663 : 8 : uint32_t sym_off = read_4ubyte_unaligned (dbg, readp);
11664 : 8 : printf (_(" symbol offset: %#" PRIx32 "\n"), sym_off);
11665 : :
11666 : 8 : readp += 4;
11667 [ - + ]: 8 : if (unlikely (readp + 4 > dataend))
11668 : 0 : goto invalid_data;
11669 : :
11670 : 8 : uint32_t shortcut_off = 0;
11671 [ + + ]: 8 : if (vers >= 9)
11672 : : {
11673 : 4 : shortcut_off = read_4ubyte_unaligned (dbg, readp);
11674 : 4 : printf (_(" shortcut offset: %#" PRIx32 "\n"), shortcut_off);
11675 : :
11676 : 4 : readp += 4;
11677 [ - + ]: 4 : if (unlikely (readp + 4 > dataend))
11678 : 0 : goto invalid_data;
11679 : : }
11680 : :
11681 : 8 : uint32_t const_off = read_4ubyte_unaligned (dbg, readp);
11682 : 8 : printf (_(" constant offset: %#" PRIx32 "\n"), const_off);
11683 : :
11684 [ - + ]: 8 : if (unlikely ((size_t) (dataend - (const unsigned char *) data->d_buf)
11685 : : < const_off))
11686 : 0 : goto invalid_data;
11687 : :
11688 : 8 : readp = data->d_buf + cu_off;
11689 : :
11690 : 8 : const unsigned char *nextp = data->d_buf + tu_off;
11691 [ - + ]: 8 : if (tu_off >= data->d_size)
11692 : 0 : goto invalid_data;
11693 : :
11694 : 8 : size_t cu_nr = (nextp - readp) / 16;
11695 : :
11696 : 8 : printf (_("\n CU list at offset %#" PRIx32
11697 : : " contains %zu entries:\n"),
11698 : : cu_off, cu_nr);
11699 : :
11700 : 8 : size_t n = 0;
11701 [ + - + + ]: 22 : while (dataend - readp >= 16 && n < cu_nr)
11702 : : {
11703 : 14 : uint64_t off = read_8ubyte_unaligned (dbg, readp);
11704 : 14 : readp += 8;
11705 : :
11706 : 14 : uint64_t len = read_8ubyte_unaligned (dbg, readp);
11707 : 14 : readp += 8;
11708 : :
11709 : 14 : printf (" [%4zu] start: %0#8" PRIx64
11710 : : ", length: %5" PRIu64 "\n", n, off, len);
11711 : 14 : n++;
11712 : : }
11713 : :
11714 : 8 : readp = data->d_buf + tu_off;
11715 : 8 : nextp = data->d_buf + addr_off;
11716 [ - + ]: 8 : if (addr_off >= data->d_size)
11717 : 0 : goto invalid_data;
11718 : :
11719 : 8 : size_t tu_nr = (nextp - readp) / 24;
11720 : :
11721 : 8 : printf (_("\n TU list at offset %#" PRIx32
11722 : : " contains %zu entries:\n"),
11723 : : tu_off, tu_nr);
11724 : :
11725 : 8 : n = 0;
11726 [ + - + + ]: 14 : while (dataend - readp >= 24 && n < tu_nr)
11727 : : {
11728 : 6 : uint64_t off = read_8ubyte_unaligned (dbg, readp);
11729 : 6 : readp += 8;
11730 : :
11731 : 6 : uint64_t type = read_8ubyte_unaligned (dbg, readp);
11732 : 6 : readp += 8;
11733 : :
11734 : 6 : uint64_t sig = read_8ubyte_unaligned (dbg, readp);
11735 : 6 : readp += 8;
11736 : :
11737 : 6 : printf (" [%4zu] CU offset: %5" PRId64
11738 : : ", type offset: %5" PRId64
11739 : : ", signature: %0#8" PRIx64 "\n", n, off, type, sig);
11740 : 6 : n++;
11741 : : }
11742 : :
11743 : 8 : readp = data->d_buf + addr_off;
11744 : 8 : nextp = data->d_buf + sym_off;
11745 [ - + ]: 8 : if (sym_off >= data->d_size)
11746 : 0 : goto invalid_data;
11747 : :
11748 : 8 : size_t addr_nr = (nextp - readp) / 20;
11749 : :
11750 : 8 : printf (_("\n Address list at offset %#" PRIx32
11751 : : " contains %zu entries:\n"),
11752 : : addr_off, addr_nr);
11753 : :
11754 : 8 : n = 0;
11755 [ + - + + ]: 22 : while (dataend - readp >= 20 && n < addr_nr)
11756 : : {
11757 : 14 : uint64_t low = read_8ubyte_unaligned (dbg, readp);
11758 : 14 : readp += 8;
11759 : :
11760 : 14 : uint64_t high = read_8ubyte_unaligned (dbg, readp);
11761 : 14 : readp += 8;
11762 : :
11763 : 14 : uint32_t idx = read_4ubyte_unaligned (dbg, readp);
11764 : 14 : readp += 4;
11765 : :
11766 : 14 : printf (" [%4zu] ", n);
11767 : 14 : print_dwarf_addr (dwflmod, 8, low, low);
11768 : 14 : printf ("..");
11769 : 14 : print_dwarf_addr (dwflmod, 8, high - 1, high);
11770 : 14 : printf (", CU index: %5" PRId32 "\n", idx);
11771 : 14 : n++;
11772 : : }
11773 : :
11774 : 8 : const unsigned char *const_start = data->d_buf + const_off;
11775 [ - + ]: 8 : if (const_off > data->d_size)
11776 : 0 : goto invalid_data;
11777 : :
11778 : 8 : const unsigned char *shortcut_start = NULL;
11779 [ + + ]: 8 : if (vers >= 9)
11780 : : {
11781 [ - + ]: 4 : if (shortcut_off >= data->d_size)
11782 : 0 : goto invalid_data;
11783 : :
11784 : 4 : shortcut_start = data->d_buf + shortcut_off;
11785 : 4 : nextp = shortcut_start;
11786 : : }
11787 : : else
11788 : : nextp = const_start;
11789 : :
11790 : 8 : readp = data->d_buf + sym_off;
11791 : 8 : size_t sym_nr = (nextp - readp) / 8;
11792 : :
11793 : 8 : printf (_("\n Symbol table at offset %#" PRIx32
11794 : : " contains %zu slots:\n"),
11795 : : sym_off, sym_nr);
11796 : :
11797 : 8 : n = 0;
11798 [ + - + + ]: 8200 : while (dataend - readp >= 8 && n < sym_nr)
11799 : : {
11800 : 8192 : uint32_t name = read_4ubyte_unaligned (dbg, readp);
11801 : 8192 : readp += 4;
11802 : :
11803 : 8192 : uint32_t vector = read_4ubyte_unaligned (dbg, readp);
11804 : 8192 : readp += 4;
11805 : :
11806 [ + + ]: 8192 : if (name != 0 || vector != 0)
11807 : : {
11808 : 60 : const unsigned char *sym = const_start + name;
11809 [ + - - + ]: 60 : if (unlikely ((size_t) (dataend - const_start) < name
11810 : : || memchr (sym, '\0', dataend - sym) == NULL))
11811 : 0 : goto invalid_data;
11812 : :
11813 : 60 : printf (" [%4zu] symbol: %s, CUs: ", n, sym);
11814 : :
11815 : 60 : const unsigned char *readcus = const_start + vector;
11816 [ - + ]: 60 : if (unlikely ((size_t) (dataend - const_start) < vector))
11817 : 0 : goto invalid_data;
11818 : 60 : uint32_t cus = read_4ubyte_unaligned (dbg, readcus);
11819 : 60 : while (cus--)
11820 : : {
11821 : 68 : uint32_t cu_kind, cu, kind;
11822 : 68 : bool is_static;
11823 : 68 : readcus += 4;
11824 [ - + ]: 68 : if (unlikely (readcus + 4 > dataend))
11825 : 0 : goto invalid_data;
11826 : 68 : cu_kind = read_4ubyte_unaligned (dbg, readcus);
11827 : 68 : cu = cu_kind & ((1 << 24) - 1);
11828 : 68 : kind = (cu_kind >> 28) & 7;
11829 : 68 : is_static = cu_kind & (1U << 31);
11830 [ + + ]: 68 : if (cu > cu_nr - 1)
11831 : 10 : printf ("%" PRId32 "T", cu - (uint32_t) cu_nr);
11832 : : else
11833 : 58 : printf ("%" PRId32, cu);
11834 [ + + ]: 68 : if (kind != 0)
11835 : : {
11836 : 52 : printf (" (");
11837 [ + + + - : 52 : switch (kind)
- ]
11838 : : {
11839 : : case 1:
11840 : 20 : printf ("type");
11841 : : break;
11842 : : case 2:
11843 : 16 : printf ("var");
11844 : : break;
11845 : : case 3:
11846 : 16 : printf ("func");
11847 : : break;
11848 : : case 4:
11849 : 0 : printf ("other");
11850 : : break;
11851 : : default:
11852 : 0 : printf ("unknown-0x%" PRIx32, kind);
11853 : : break;
11854 : : }
11855 [ + + ]: 52 : printf (":%c)", (is_static ? 'S' : 'G'));
11856 : : }
11857 [ + + ]: 68 : if (cus > 0)
11858 [ + + ]: 136 : printf (", ");
11859 : : }
11860 : 60 : printf ("\n");
11861 : : }
11862 : 8192 : n++;
11863 : : }
11864 : :
11865 [ + + ]: 8 : if (vers < 9)
11866 : : return;
11867 : :
11868 [ - + ]: 4 : if (unlikely (shortcut_start == NULL))
11869 : 0 : goto invalid_data;
11870 : :
11871 : 4 : readp = shortcut_start;
11872 : 4 : nextp = const_start;
11873 : 4 : size_t shortcut_nr = (nextp - readp) / 4;
11874 : :
11875 [ - + ]: 4 : if (unlikely (shortcut_nr != 2))
11876 : 0 : goto invalid_data;
11877 : :
11878 : 4 : printf (_("\nShortcut table at offset %#" PRIx32 " contains %zu slots:\n"),
11879 : : shortcut_off, shortcut_nr);
11880 : :
11881 : 4 : uint32_t lang = read_4ubyte_unaligned (dbg, readp);
11882 : 4 : readp += 4;
11883 : :
11884 : : /* Include the hex number of LANG in the output if the language
11885 : : is unknown. */
11886 : 4 : const char *lang_str = dwarf_lang_string (lang);
11887 : 4 : lang_str = string_or_unknown (lang_str, lang, DW_LANG_lo_user,
11888 : : DW_LANG_hi_user, true);
11889 : :
11890 : 4 : printf (_("Language of main: %s\n"), lang_str);
11891 : 4 : printf (_("Name of main: "));
11892 : :
11893 [ + + ]: 4 : if (lang != 0)
11894 : : {
11895 : 2 : uint32_t name = read_4ubyte_unaligned (dbg, readp);
11896 : 2 : readp += 4;
11897 : 2 : const unsigned char *sym = const_start + name;
11898 : :
11899 [ + - - + ]: 2 : if (unlikely ((size_t) (dataend - const_start) < name
11900 : : || memchr (sym, '\0', dataend - sym) == NULL))
11901 : 0 : goto invalid_data;
11902 : :
11903 : 2 : printf ("%s\n", sym);
11904 : : }
11905 : : else
11906 : 4 : printf ("<unknown>\n");
11907 : : }
11908 : :
11909 : : /* Returns true and sets split DWARF CU id if there is a split compile
11910 : : unit in the given Dwarf, and no non-split units are found (before it). */
11911 : : static bool
11912 : 396 : is_split_dwarf (Dwarf *dbg, uint64_t *id, Dwarf_CU **split_cu)
11913 : : {
11914 : 396 : Dwarf_CU *cu = NULL;
11915 [ + + ]: 396 : while (dwarf_get_units (dbg, cu, &cu, NULL, NULL, NULL, NULL) == 0)
11916 : : {
11917 : 394 : uint8_t unit_type;
11918 [ + - ]: 394 : if (dwarf_cu_info (cu, NULL, &unit_type, NULL, NULL,
11919 : : id, NULL, NULL) != 0)
11920 : 394 : return false;
11921 : :
11922 [ + + ]: 394 : if (unit_type != DW_UT_split_compile && unit_type != DW_UT_split_type)
11923 : : return false;
11924 : :
11925 : : /* We really only care about the split compile unit, the types
11926 : : should be fine and self sufficient. Also they don't have an
11927 : : id that we can match with a skeleton unit. */
11928 [ + - ]: 18 : if (unit_type == DW_UT_split_compile)
11929 : : {
11930 : 18 : *split_cu = cu;
11931 : 18 : return true;
11932 : : }
11933 : : }
11934 : :
11935 : : return false;
11936 : : }
11937 : :
11938 : : /* Check that there is one and only one Dwfl_Module, return in arg. */
11939 : : static int
11940 : 18 : getone_dwflmod (Dwfl_Module *dwflmod,
11941 : : void **userdata __attribute__ ((unused)),
11942 : : const char *name __attribute__ ((unused)),
11943 : : Dwarf_Addr base __attribute__ ((unused)),
11944 : : void *arg)
11945 : : {
11946 : 18 : Dwfl_Module **m = (Dwfl_Module **) arg;
11947 [ + - ]: 18 : if (*m != NULL)
11948 : : return DWARF_CB_ABORT;
11949 : 18 : *m = dwflmod;
11950 : 18 : return DWARF_CB_OK;
11951 : : }
11952 : :
11953 : : static void
11954 : 502 : print_debug (Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr)
11955 : : {
11956 : : /* Used for skeleton file, if necessary for split DWARF. */
11957 : 502 : Dwfl *skel_dwfl = NULL;
11958 : 502 : Dwfl_Module *skel_mod = NULL;
11959 : 502 : char *skel_name = NULL;
11960 : 502 : Dwarf *split_dbg = NULL;
11961 : 502 : Dwarf_CU *split_cu = NULL;
11962 : :
11963 : : /* Before we start the real work get a debug context descriptor. */
11964 : 502 : Dwarf_Addr dwbias;
11965 : 502 : Dwarf *dbg = dwfl_module_getdwarf (dwflmod, &dwbias);
11966 : 502 : Dwarf dummy_dbg =
11967 : : {
11968 : 502 : .elf = ebl->elf,
11969 : 502 : .other_byte_order = MY_ELFDATA != ehdr->e_ident[EI_DATA]
11970 : : };
11971 [ + + ]: 502 : if (dbg == NULL)
11972 : : {
11973 [ - + ]: 106 : if ((print_debug_sections & ~(section_exception|section_frame)) != 0)
11974 : 0 : error (0, 0, _("cannot get debug context descriptor: %s"),
11975 : : dwfl_errmsg (-1));
11976 : : dbg = &dummy_dbg;
11977 : : }
11978 : : else
11979 : : {
11980 : : /* If we are asked about a split dwarf (.dwo) file, use the user
11981 : : provided, or find the corresponding skeleton file. If we got
11982 : : a skeleton file, replace the given dwflmod and dbg, with one
11983 : : derived from the skeleton file to provide enough context. */
11984 : 396 : uint64_t split_id;
11985 [ + + ]: 396 : if (is_split_dwarf (dbg, &split_id, &split_cu))
11986 : : {
11987 [ + - ]: 18 : if (dwarf_skeleton != NULL)
11988 : 18 : skel_name = strdup (dwarf_skeleton);
11989 : : else
11990 : : {
11991 : : /* Replace file.dwo with file.o and see if that matches. */
11992 : 0 : const char *fname;
11993 : 0 : dwfl_module_info (dwflmod, NULL, NULL, NULL, NULL, NULL,
11994 : : &fname, NULL);
11995 [ # # ]: 0 : if (fname != NULL)
11996 : : {
11997 : 0 : size_t flen = strlen (fname);
11998 [ # # # # ]: 0 : if (flen > 4 && strcmp (".dwo", fname + flen - 4) == 0)
11999 : : {
12000 : 0 : skel_name = strdup (fname);
12001 [ # # ]: 0 : if (skel_name != NULL)
12002 : : {
12003 : 0 : skel_name[flen - 3] = 'o';
12004 : 0 : skel_name[flen - 2] = '\0';
12005 : : }
12006 : : }
12007 : : }
12008 : : }
12009 : :
12010 [ - + ]: 18 : if (skel_name != NULL)
12011 : : {
12012 : 18 : int skel_fd = open (skel_name, O_RDONLY);
12013 [ - + ]: 18 : if (skel_fd == -1)
12014 : 0 : fprintf (stderr, "Warning: Couldn't open DWARF skeleton file"
12015 : : " '%s'\n", skel_name);
12016 : : else
12017 : : {
12018 : 18 : skel_dwfl = create_dwfl (skel_fd, skel_name);
12019 : :
12020 : : /* skel_fd was dup'ed by create_dwfl. We can close the
12021 : : original now. */
12022 : 18 : close (skel_fd);
12023 : : }
12024 : :
12025 [ + - ]: 18 : if (skel_dwfl != NULL)
12026 : : {
12027 [ - + ]: 18 : if (dwfl_getmodules (skel_dwfl, &getone_dwflmod,
12028 : : &skel_mod, 0) != 0)
12029 : : {
12030 : 0 : fprintf (stderr, "Warning: Bad DWARF skeleton,"
12031 : : " multiple modules '%s'\n", skel_name);
12032 : 0 : dwfl_end (skel_dwfl);
12033 : 0 : skel_mod = NULL;
12034 : : }
12035 : : }
12036 [ # # ]: 0 : else if (skel_fd != -1)
12037 : 0 : fprintf (stderr, "Warning: Couldn't create skeleton dwfl for"
12038 : : " '%s': %s\n", skel_name, dwfl_errmsg (-1));
12039 : :
12040 [ + - ]: 18 : if (skel_mod != NULL)
12041 : : {
12042 : 18 : Dwarf *skel_dbg = dwfl_module_getdwarf (skel_mod, &dwbias);
12043 [ + - ]: 18 : if (skel_dbg != NULL)
12044 : : {
12045 : : /* First check the skeleton CU DIE, only fetch
12046 : : the split DIE if we know the id matches to
12047 : : not unnecessary search for any split DIEs we
12048 : : don't need. */
12049 : 18 : Dwarf_CU *cu = NULL;
12050 : 18 : while (dwarf_get_units (skel_dbg, cu, &cu,
12051 [ + - ]: 26 : NULL, NULL, NULL, NULL) == 0)
12052 : : {
12053 : 26 : uint8_t unit_type;
12054 : 26 : uint64_t skel_id;
12055 [ + - ]: 26 : if (dwarf_cu_info (cu, NULL, &unit_type, NULL, NULL,
12056 : : &skel_id, NULL, NULL) == 0
12057 [ + - ]: 26 : && unit_type == DW_UT_skeleton
12058 [ + + ]: 26 : && split_id == skel_id)
12059 : : {
12060 : 18 : Dwarf_Die subdie;
12061 [ + - ]: 18 : if (dwarf_cu_info (cu, NULL, NULL, NULL,
12062 : : &subdie,
12063 : : NULL, NULL, NULL) == 0
12064 [ + - ]: 18 : && dwarf_tag (&subdie) != DW_TAG_invalid)
12065 : : {
12066 : 18 : split_dbg = dwarf_cu_getdwarf (subdie.cu);
12067 [ - + ]: 18 : if (split_dbg == NULL)
12068 : 0 : fprintf (stderr,
12069 : : "Warning: Couldn't get split_dbg:"
12070 : : " %s\n", dwarf_errmsg (-1));
12071 : 0 : break;
12072 : : }
12073 : : else
12074 : : {
12075 : : /* Everything matches up, but not
12076 : : according to libdw. Which means
12077 : : the user knew better. So...
12078 : : Terrible hack... We can never
12079 : : destroy the underlying dwfl
12080 : : because it would free the wrong
12081 : : Dwarfs... So we leak memory...*/
12082 [ # # ]: 0 : if (cu->split == NULL
12083 [ # # ]: 0 : && dwarf_skeleton != NULL)
12084 : : {
12085 : 0 : do_not_close_dwfl = true;
12086 : 0 : __libdw_link_skel_split (cu, split_cu);
12087 : 0 : split_dbg = dwarf_cu_getdwarf (split_cu);
12088 : 0 : break;
12089 : : }
12090 : : else
12091 : 0 : fprintf (stderr, "Warning: Couldn't get"
12092 : : " skeleton subdie: %s\n",
12093 : : dwarf_errmsg (-1));
12094 : : }
12095 : : }
12096 : : }
12097 [ - - ]: 18 : if (split_dbg == NULL)
12098 : 0 : fprintf (stderr, "Warning: '%s' didn't contain a skeleton for split id %" PRIx64 "\n", skel_name, split_id);
12099 : : }
12100 : : else
12101 : 0 : fprintf (stderr, "Warning: Couldn't get skeleton DWARF:"
12102 : : " %s\n", dwfl_errmsg (-1));
12103 : : }
12104 : : }
12105 : :
12106 : 0 : if (split_dbg != NULL)
12107 : : {
12108 : 18 : dbg = split_dbg;
12109 : 18 : dwflmod = skel_mod;
12110 : : }
12111 [ # # ]: 0 : else if (skel_name == NULL)
12112 : 0 : fprintf (stderr,
12113 : : "Warning: split DWARF file, but no skeleton found.\n");
12114 : : }
12115 [ + - ]: 378 : else if (dwarf_skeleton != NULL)
12116 : 396 : fprintf (stderr, "Warning: DWARF skeleton given,"
12117 : : " but not a split DWARF file\n");
12118 : : }
12119 : :
12120 : : /* Get the section header string table index. */
12121 : 502 : size_t shstrndx;
12122 [ - + ]: 502 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
12123 : 0 : error_exit (0, _("cannot get section header string table index"));
12124 : :
12125 : : /* If the .debug_info section is listed as implicitly required then
12126 : : we must make sure to handle it before handling any other debug
12127 : : section. Various other sections depend on the CU DIEs being
12128 : : scanned (silently) first. */
12129 : 502 : bool implicit_info = (implicit_debug_sections & section_info) != 0;
12130 : 502 : bool explicit_info = (print_debug_sections & section_info) != 0;
12131 [ + + ]: 502 : if (implicit_info)
12132 : : {
12133 : : Elf_Scn *scn = NULL;
12134 [ + - ]: 3132 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
12135 : : {
12136 : 3132 : GElf_Shdr shdr_mem;
12137 : 3132 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
12138 : :
12139 [ + - + + ]: 3132 : if (shdr != NULL && shdr->sh_type == SHT_PROGBITS)
12140 : : {
12141 : 2448 : const char *name = elf_strptr (ebl->elf, shstrndx,
12142 : 1224 : shdr->sh_name);
12143 [ - + ]: 1224 : if (name == NULL)
12144 : 0 : continue;
12145 : :
12146 [ + + ]: 1224 : if (strcmp (name, ".debug_info") == 0
12147 [ + + ]: 1092 : || strcmp (name, ".debug_info.dwo") == 0
12148 [ + + ]: 1074 : || strcmp (name, ".zdebug_info") == 0
12149 [ + - ]: 1062 : || strcmp (name, ".zdebug_info.dwo") == 0
12150 [ + + ]: 1062 : || strcmp (name, ".gnu.debuglto_.debug_info") == 0)
12151 : : {
12152 : 164 : print_debug_info_section (dwflmod, ebl, ehdr,
12153 : : scn, shdr, dbg);
12154 : 164 : break;
12155 : : }
12156 : : }
12157 : : }
12158 : 164 : print_debug_sections &= ~section_info;
12159 : 164 : implicit_debug_sections &= ~section_info;
12160 : : }
12161 : :
12162 : : /* Look through all the sections for the debugging sections to print. */
12163 : : Elf_Scn *scn = NULL;
12164 [ + + ]: 16590 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
12165 : : {
12166 : 16088 : GElf_Shdr shdr_mem;
12167 : 16088 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
12168 : :
12169 [ + - + + ]: 16088 : if (shdr != NULL && shdr->sh_type == SHT_PROGBITS)
12170 : : {
12171 : 6758 : static const struct
12172 : : {
12173 : : const char *name;
12174 : : enum section_e bitmask;
12175 : : void (*fp) (Dwfl_Module *, Ebl *,
12176 : : GElf_Ehdr *, Elf_Scn *, GElf_Shdr *, Dwarf *);
12177 : : } debug_sections[] =
12178 : : {
12179 : : #define NEW_SECTION(name) \
12180 : : { ".debug_" #name, section_##name, print_debug_##name##_section }
12181 : : NEW_SECTION (abbrev),
12182 : : NEW_SECTION (addr),
12183 : : NEW_SECTION (aranges),
12184 : : NEW_SECTION (frame),
12185 : : NEW_SECTION (info),
12186 : : NEW_SECTION (types),
12187 : : NEW_SECTION (line),
12188 : : NEW_SECTION (loc),
12189 : : /* loclists is loc for DWARF5. */
12190 : : { ".debug_loclists", section_loc,
12191 : : print_debug_loclists_section },
12192 : : NEW_SECTION (pubnames),
12193 : : NEW_SECTION (str),
12194 : : /* A DWARF5 specialised debug string section. */
12195 : : { ".debug_line_str", section_str,
12196 : : print_debug_str_section },
12197 : : /* DWARF5 string offsets table. */
12198 : : { ".debug_str_offsets", section_str,
12199 : : print_debug_str_offsets_section },
12200 : : NEW_SECTION (macinfo),
12201 : : NEW_SECTION (macro),
12202 : : NEW_SECTION (ranges),
12203 : : /* rnglists is ranges for DWARF5. */
12204 : : { ".debug_rnglists", section_ranges,
12205 : : print_debug_rnglists_section },
12206 : : { ".eh_frame", section_frame | section_exception,
12207 : : print_debug_frame_section },
12208 : : { ".eh_frame_hdr", section_frame | section_exception,
12209 : : print_debug_frame_hdr_section },
12210 : : { ".gcc_except_table", section_frame | section_exception,
12211 : : print_debug_exception_table },
12212 : : { ".gdb_index", section_gdb_index, print_gdb_index_section }
12213 : : };
12214 : 6758 : const int ndebug_sections = (sizeof (debug_sections)
12215 : : / sizeof (debug_sections[0]));
12216 : 13516 : const char *name = elf_strptr (ebl->elf, shstrndx,
12217 : 6758 : shdr->sh_name);
12218 [ - + ]: 6758 : if (name == NULL)
12219 : 0 : continue;
12220 : :
12221 : : int n;
12222 [ + + ]: 106506 : for (n = 0; n < ndebug_sections; ++n)
12223 : : {
12224 : 103042 : size_t dbglen = strlen (debug_sections[n].name);
12225 : 103042 : size_t scnlen = strlen (name);
12226 [ + + ]: 103042 : if ((strncmp (name, debug_sections[n].name, dbglen) == 0
12227 [ + + ]: 3558 : && (dbglen == scnlen
12228 [ + + ]: 546 : || (scnlen == dbglen + 4
12229 [ + + ]: 466 : && strstr (name, ".dwo") == name + dbglen)))
12230 [ + + + + ]: 99922 : || (name[0] == '.' && name[1] == 'z'
12231 [ + - ]: 1132 : && debug_sections[n].name[1] == 'd'
12232 [ + + ]: 1132 : && strncmp (&name[2], &debug_sections[n].name[1],
12233 : : dbglen - 1) == 0
12234 [ - + ]: 164 : && (scnlen == dbglen + 1
12235 [ # # ]: 0 : || (scnlen == dbglen + 5
12236 [ # # ]: 0 : && strstr (name, ".dwo") == name + dbglen + 1)))
12237 [ + + ]: 99758 : || (scnlen > 14 /* .gnu.debuglto_ prefix. */
12238 [ + + ]: 10170 : && startswith (name, ".gnu.debuglto_")
12239 [ + + ]: 72 : && strcmp (&name[14], debug_sections[n].name) == 0)
12240 : : )
12241 : : {
12242 : 3294 : if ((print_debug_sections | implicit_debug_sections)
12243 [ + + ]: 3294 : & debug_sections[n].bitmask)
12244 : 1094 : debug_sections[n].fp (dwflmod, ebl, ehdr, scn, shdr, dbg);
12245 : : break;
12246 : : }
12247 : : }
12248 : : }
12249 : : }
12250 : :
12251 : 502 : dwfl_end (skel_dwfl);
12252 : 502 : free (skel_name);
12253 : :
12254 : : /* Turn implicit and/or explicit back on in case we go over another file. */
12255 [ + + ]: 502 : if (implicit_info)
12256 : 164 : implicit_debug_sections |= section_info;
12257 [ + + ]: 502 : if (explicit_info)
12258 : 134 : print_debug_sections |= section_info;
12259 : :
12260 : 502 : reset_listptr (&known_locsptr);
12261 : 502 : reset_listptr (&known_loclistsptr);
12262 : 502 : reset_listptr (&known_rangelistptr);
12263 : 502 : reset_listptr (&known_rnglistptr);
12264 : 502 : reset_listptr (&known_addrbases);
12265 : 502 : reset_listptr (&known_stroffbases);
12266 : 502 : }
12267 : :
12268 : :
12269 : : #define ITEM_INDENT 4
12270 : : #define WRAP_COLUMN 75
12271 : :
12272 : : /* Print "NAME: FORMAT", wrapping when output text would make the line
12273 : : exceed WRAP_COLUMN. Unpadded numbers look better for the core items
12274 : : but this function is also used for registers which should be printed
12275 : : aligned. Fortunately registers output uses fixed fields width (such
12276 : : as %11d) for the alignment.
12277 : :
12278 : : Line breaks should not depend on the particular values although that
12279 : : may happen in some cases of the core items. */
12280 : :
12281 : : static unsigned int
12282 : : __attribute__ ((format (printf, 6, 7)))
12283 : 1642 : print_core_item (unsigned int colno, char sep, unsigned int wrap,
12284 : : size_t name_width, const char *name, const char *format, ...)
12285 : : {
12286 : 1642 : size_t len = strlen (name);
12287 : 1642 : if (name_width < len)
12288 : : name_width = len;
12289 : :
12290 : 1642 : char *out;
12291 : 1642 : va_list ap;
12292 : 1642 : va_start (ap, format);
12293 : 1642 : int out_len = vasprintf (&out, format, ap);
12294 : 1642 : va_end (ap);
12295 [ - + ]: 1642 : if (out_len == -1)
12296 : 0 : error_exit (0, _("memory exhausted"));
12297 : :
12298 : 1642 : size_t n = name_width + sizeof ": " - 1 + out_len;
12299 : :
12300 [ + + ]: 1642 : if (colno == 0)
12301 : : {
12302 : 102 : printf ("%*s", ITEM_INDENT, "");
12303 : 102 : colno = ITEM_INDENT + n;
12304 : : }
12305 [ + + ]: 1540 : else if (colno + 2 + n < wrap)
12306 : : {
12307 : 936 : printf ("%c ", sep);
12308 : 936 : colno += 2 + n;
12309 : : }
12310 : : else
12311 : : {
12312 : 604 : printf ("\n%*s", ITEM_INDENT, "");
12313 : 604 : colno = ITEM_INDENT + n;
12314 : : }
12315 : :
12316 : 1642 : printf ("%s: %*s%s", name, (int) (name_width - len), "", out);
12317 : :
12318 : 1642 : free (out);
12319 : :
12320 : 1642 : return colno;
12321 : : }
12322 : :
12323 : : static const void *
12324 : 1862 : convert (Elf *core, Elf_Type type, uint_fast16_t count,
12325 : : void *value, const void *data, size_t size)
12326 : : {
12327 : 3724 : Elf_Data valuedata =
12328 : : {
12329 : : .d_type = type,
12330 : : .d_buf = value,
12331 [ + + ]: 1862 : .d_size = size ?: gelf_fsize (core, type, count, EV_CURRENT),
12332 : : .d_version = EV_CURRENT,
12333 : : };
12334 : 1862 : Elf_Data indata =
12335 : : {
12336 : : .d_type = type,
12337 : : .d_buf = (void *) data,
12338 : : .d_size = valuedata.d_size,
12339 : : .d_version = EV_CURRENT,
12340 : : };
12341 : :
12342 : 1862 : Elf_Data *d = (gelf_getclass (core) == ELFCLASS32
12343 [ + + ]: 1862 : ? elf32_xlatetom : elf64_xlatetom)
12344 : 1862 : (&valuedata, &indata, elf_getident (core, NULL)[EI_DATA]);
12345 [ - + ]: 1862 : if (d == NULL)
12346 : 0 : error_exit (0, _("cannot convert core note data: %s"),
12347 : : elf_errmsg (-1));
12348 : :
12349 : 1862 : return data + indata.d_size;
12350 : : }
12351 : :
12352 : : typedef uint8_t GElf_Byte;
12353 : :
12354 : : static unsigned int
12355 : 800 : handle_core_item (Elf *core, const GElf_Ehdr *ehdr,
12356 : : const Ebl_Core_Item *item, const void *desc,
12357 : : unsigned int colno, size_t *repeated_size)
12358 : : {
12359 [ + + ]: 800 : uint_fast16_t count = item->count ?: 1;
12360 : : /* Ebl_Core_Item count is always a small number.
12361 : : Make sure the backend didn't put in some large bogus value. */
12362 [ - + ]: 124 : assert (count < 128);
12363 : :
12364 : : #define TYPES \
12365 : : DO_TYPE (BYTE, Byte, "0x%.2" PRIx8, "%" PRId8); \
12366 : : DO_TYPE (HALF, Half, "0x%.4" PRIx16, "%" PRId16); \
12367 : : DO_TYPE (WORD, Word, "0x%.8" PRIx32, "%" PRId32); \
12368 : : DO_TYPE (SWORD, Sword, "%" PRId32, "%" PRId32); \
12369 : : DO_TYPE (XWORD, Xword, "0x%.16" PRIx64, "%" PRId64); \
12370 : : DO_TYPE (SXWORD, Sxword, "%" PRId64, "%" PRId64)
12371 : :
12372 : : #define DO_TYPE(NAME, Name, hex, dec) GElf_##Name Name
12373 : 800 : typedef union { TYPES; } value_t;
12374 : 800 : void *data = alloca (count * sizeof (value_t));
12375 : : #undef DO_TYPE
12376 : :
12377 : : #define DO_TYPE(NAME, Name, hex, dec) \
12378 : : GElf_##Name *value_##Name __attribute__((unused)) = data
12379 : 800 : TYPES;
12380 : : #undef DO_TYPE
12381 : :
12382 : 800 : size_t size = gelf_fsize (core, item->type, count, EV_CURRENT);
12383 : 800 : size_t convsize = size;
12384 [ + + ]: 800 : if (repeated_size != NULL)
12385 : : {
12386 [ + - - + ]: 2 : if (*repeated_size > size && (item->format == 'b' || item->format == 'B'))
12387 : : {
12388 : 0 : data = alloca (*repeated_size);
12389 : 0 : count *= *repeated_size / size;
12390 : 0 : convsize = count * size;
12391 : 0 : *repeated_size -= convsize;
12392 : : }
12393 [ + - + - ]: 2 : else if (item->count != 0 || item->format != '\n')
12394 : 0 : *repeated_size -= size;
12395 : : }
12396 : :
12397 : 800 : convert (core, item->type, count, data, desc + item->offset, convsize);
12398 : :
12399 : 800 : Elf_Type type = item->type;
12400 [ + + ]: 800 : if (type == ELF_T_ADDR)
12401 [ + - ]: 2 : type = gelf_getclass (core) == ELFCLASS32 ? ELF_T_WORD : ELF_T_XWORD;
12402 : :
12403 [ + + + + : 800 : switch (item->format)
+ + + +
- ]
12404 : : {
12405 : 386 : case 'd':
12406 [ - + ]: 386 : assert (count == 1);
12407 [ + + + + : 386 : switch (type)
- + - ]
12408 : : {
12409 : : #define DO_TYPE(NAME, Name, hex, dec) \
12410 : : case ELF_T_##NAME: \
12411 : : colno = print_core_item (colno, ',', WRAP_COLUMN, \
12412 : : 0, item->name, dec, value_##Name[0]); \
12413 : : break
12414 : 386 : TYPES;
12415 : : #undef DO_TYPE
12416 : 0 : default:
12417 : 0 : abort ();
12418 : : }
12419 : : break;
12420 : :
12421 : 222 : case 'x':
12422 [ - + ]: 222 : assert (count == 1);
12423 [ - - + - : 222 : switch (type)
+ - - ]
12424 : : {
12425 : : #define DO_TYPE(NAME, Name, hex, dec) \
12426 : : case ELF_T_##NAME: \
12427 : : colno = print_core_item (colno, ',', WRAP_COLUMN, \
12428 : : 0, item->name, hex, value_##Name[0]); \
12429 : : break
12430 : 222 : TYPES;
12431 : : #undef DO_TYPE
12432 : 0 : default:
12433 : 0 : abort ();
12434 : : }
12435 : : break;
12436 : :
12437 : 44 : case 'b':
12438 : : case 'B':
12439 [ - + ]: 44 : assert (size % sizeof (unsigned int) == 0);
12440 : 44 : unsigned int nbits = count * size * 8;
12441 : 44 : unsigned int pop = 0;
12442 [ + + ]: 112 : for (const unsigned int *i = data; (void *) i < data + count * size; ++i)
12443 : 68 : pop += __builtin_popcount (*i);
12444 : 44 : bool negate = pop > nbits / 2;
12445 : 44 : const unsigned int bias = item->format == 'b';
12446 : :
12447 : 44 : {
12448 [ - + ]: 44 : char printed[(negate ? nbits - pop : pop) * 16 + 1];
12449 : 44 : char *p = printed;
12450 : 44 : *p = '\0';
12451 : :
12452 : 44 : if (BYTE_ORDER != LITTLE_ENDIAN && size > sizeof (unsigned int))
12453 : : {
12454 : : assert (size == sizeof (unsigned int) * 2);
12455 : : for (unsigned int *i = data;
12456 : : (void *) i < data + count * size; i += 2)
12457 : : {
12458 : : unsigned int w = i[1];
12459 : : i[1] = i[0];
12460 : : i[0] = w;
12461 : : }
12462 : : }
12463 : :
12464 : 44 : unsigned int lastbit = 0;
12465 : 44 : unsigned int run = 0;
12466 : 44 : for (const unsigned int *i = data;
12467 [ + + ]: 112 : (void *) i < data + count * size; ++i)
12468 : : {
12469 : 68 : unsigned int bit = ((void *) i - data) * 8;
12470 [ - + ]: 68 : unsigned int w = negate ? ~*i : *i;
12471 [ - + ]: 68 : while (w != 0)
12472 : : {
12473 : : /* Note that a right shift equal to (or greater than)
12474 : : the number of bits of w is undefined behaviour. In
12475 : : particular when the least significant bit is bit 32
12476 : : (w = 0x8000000) then w >>= n is undefined. So
12477 : : explicitly handle that case separately. */
12478 : 0 : unsigned int n = ffs (w);
12479 [ # # ]: 0 : if (n < sizeof (w) * 8)
12480 : 0 : w >>= n;
12481 : : else
12482 : : w = 0;
12483 : 0 : bit += n;
12484 : :
12485 [ # # # # ]: 0 : if (lastbit != 0 && lastbit + 1 == bit)
12486 : 0 : ++run;
12487 : : else
12488 : : {
12489 : 0 : if (lastbit == 0)
12490 : 0 : p += sprintf (p, "%u", bit - bias);
12491 [ # # ]: 0 : else if (run == 0)
12492 : 0 : p += sprintf (p, ",%u", bit - bias);
12493 : : else
12494 : 0 : p += sprintf (p, "-%u,%u", lastbit - bias, bit - bias);
12495 : : run = 0;
12496 : : }
12497 : :
12498 : : lastbit = bit;
12499 : : }
12500 : : }
12501 [ - + - - ]: 44 : if (lastbit > 0 && run > 0 && lastbit + 1 != nbits)
12502 : 0 : p += sprintf (p, "-%u", lastbit - bias);
12503 : :
12504 [ + - ]: 88 : colno = print_core_item (colno, ',', WRAP_COLUMN, 0, item->name,
12505 : : negate ? "~<%s>" : "<%s>", printed);
12506 : : }
12507 : 44 : break;
12508 : :
12509 : 88 : case 'T':
12510 : : case (char) ('T'|0x80):
12511 [ - + ]: 88 : assert (count == 2);
12512 : 88 : Dwarf_Word sec;
12513 : 88 : Dwarf_Word usec;
12514 [ - - + - : 88 : switch (type)
+ - - ]
12515 : : {
12516 : : #define DO_TYPE(NAME, Name, hex, dec) \
12517 : : case ELF_T_##NAME: \
12518 : : sec = value_##Name[0]; \
12519 : : usec = value_##Name[1]; \
12520 : : break
12521 : 88 : TYPES;
12522 : : #undef DO_TYPE
12523 : 0 : default:
12524 : 0 : abort ();
12525 : : }
12526 [ - + ]: 88 : if (unlikely (item->format == (char) ('T'|0x80)))
12527 : : {
12528 : : /* This is a hack for an ill-considered 64-bit ABI where
12529 : : tv_usec is actually a 32-bit field with 32 bits of padding
12530 : : rounding out struct timeval. We've already converted it as
12531 : : a 64-bit field. For little-endian, this just means the
12532 : : high half is the padding; it's presumably zero, but should
12533 : : be ignored anyway. For big-endian, it means the 32-bit
12534 : : field went into the high half of USEC. */
12535 [ # # ]: 0 : if (likely (ehdr->e_ident[EI_DATA] == ELFDATA2MSB))
12536 : 0 : usec >>= 32;
12537 : : else
12538 : 0 : usec &= UINT32_MAX;
12539 : : }
12540 : 88 : colno = print_core_item (colno, ',', WRAP_COLUMN, 0, item->name,
12541 : : "%" PRIu64 ".%.6" PRIu64, sec, usec);
12542 : 88 : break;
12543 : :
12544 : 18 : case 'c':
12545 [ - + ]: 18 : assert (count == 1);
12546 : 18 : colno = print_core_item (colno, ',', WRAP_COLUMN, 0, item->name,
12547 : 18 : "%c", value_Byte[0]);
12548 : 18 : break;
12549 : :
12550 : 36 : case 's':
12551 : 36 : colno = print_core_item (colno, ',', WRAP_COLUMN, 0, item->name,
12552 : : "%.*s", (int) count, value_Byte);
12553 : 36 : break;
12554 : :
12555 : 2 : case '\n':
12556 : : /* This is a list of strings separated by '\n'. */
12557 [ - + ]: 2 : assert (item->count == 0);
12558 [ - + ]: 2 : assert (repeated_size != NULL);
12559 [ - + ]: 2 : assert (item->name == NULL);
12560 [ - + ]: 2 : if (unlikely (item->offset >= *repeated_size))
12561 : : break;
12562 : :
12563 : 2 : const char *s = desc + item->offset;
12564 : 2 : size = *repeated_size - item->offset;
12565 : 2 : *repeated_size = 0;
12566 [ + - ]: 96 : while (size > 0)
12567 : : {
12568 : 96 : const char *eol = memchr (s, '\n', size);
12569 : 96 : int len = size;
12570 [ + + ]: 96 : if (eol != NULL)
12571 : 94 : len = eol - s;
12572 : 96 : printf ("%*s%.*s\n", ITEM_INDENT, "", len, s);
12573 [ + + ]: 96 : if (eol == NULL)
12574 : : break;
12575 : 94 : size -= eol + 1 - s;
12576 : 94 : s = eol + 1;
12577 : : }
12578 : :
12579 : : colno = WRAP_COLUMN;
12580 : : break;
12581 : :
12582 : : case 'h':
12583 : : break;
12584 : :
12585 : 0 : default:
12586 : 800 : error (0, 0, "XXX not handling format '%c' for %s",
12587 : 0 : item->format, item->name);
12588 : : break;
12589 : : }
12590 : :
12591 : : #undef TYPES
12592 : :
12593 : 800 : return colno;
12594 : : }
12595 : :
12596 : :
12597 : : /* Sort items by group, and by layout offset within each group. */
12598 : : static int
12599 : 1790 : compare_core_items (const void *a, const void *b)
12600 : : {
12601 : 1790 : const Ebl_Core_Item *const *p1 = a;
12602 : 1790 : const Ebl_Core_Item *const *p2 = b;
12603 : 1790 : const Ebl_Core_Item *item1 = *p1;
12604 : 1790 : const Ebl_Core_Item *item2 = *p2;
12605 : :
12606 : 1790 : return ((item1->group == item2->group ? 0
12607 [ + + ]: 1790 : : strcmp (item1->group, item2->group))
12608 [ - + ]: 1790 : ?: (int) item1->offset - (int) item2->offset);
12609 : : }
12610 : :
12611 : : /* Sort item groups by layout offset of the first item in the group. */
12612 : : static int
12613 : 278 : compare_core_item_groups (const void *a, const void *b)
12614 : : {
12615 : 278 : const Ebl_Core_Item *const *const *p1 = a;
12616 : 278 : const Ebl_Core_Item *const *const *p2 = b;
12617 : 278 : const Ebl_Core_Item *const *group1 = *p1;
12618 : 278 : const Ebl_Core_Item *const *group2 = *p2;
12619 : 278 : const Ebl_Core_Item *item1 = *group1;
12620 : 278 : const Ebl_Core_Item *item2 = *group2;
12621 : :
12622 : 278 : return (int) item1->offset - (int) item2->offset;
12623 : : }
12624 : :
12625 : : static unsigned int
12626 : 74 : handle_core_items (Elf *core, const GElf_Ehdr *ehdr,
12627 : : const void *desc, size_t descsz,
12628 : : const Ebl_Core_Item *items, size_t nitems)
12629 : 74 : {
12630 [ + + ]: 74 : if (nitems == 0)
12631 : : return 0;
12632 : 68 : unsigned int colno = 0;
12633 : :
12634 : : /* FORMAT '\n' makes sense to be present only as a single item as it
12635 : : processes all the data of a note. FORMATs 'b' and 'B' have a special case
12636 : : if present as a single item but they can be also processed with other
12637 : : items below. */
12638 [ + + + + ]: 68 : if (nitems == 1 && (items[0].format == '\n' || items[0].format == 'b'
12639 [ + - ]: 16 : || items[0].format == 'B'))
12640 : : {
12641 [ - + ]: 2 : assert (items[0].offset == 0);
12642 : 2 : size_t size = descsz;
12643 : 2 : colno = handle_core_item (core, ehdr, items, desc, colno, &size);
12644 : : /* If SIZE is not zero here there is some remaining data. But we do not
12645 : : know how to process it anyway. */
12646 : 2 : return colno;
12647 : : }
12648 [ + + ]: 848 : for (size_t i = 0; i < nitems; ++i)
12649 [ - + ]: 782 : assert (items[i].format != '\n');
12650 : :
12651 : : /* Sort to collect the groups together. */
12652 : 66 : const Ebl_Core_Item *sorted_items[nitems];
12653 [ + + ]: 848 : for (size_t i = 0; i < nitems; ++i)
12654 : 782 : sorted_items[i] = &items[i];
12655 : 66 : qsort (sorted_items, nitems, sizeof sorted_items[0], &compare_core_items);
12656 : :
12657 : : /* Collect the unique groups and sort them. */
12658 : 66 : const Ebl_Core_Item **groups[nitems];
12659 : 66 : groups[0] = &sorted_items[0];
12660 : 66 : size_t ngroups = 1;
12661 [ + + ]: 782 : for (size_t i = 1; i < nitems; ++i)
12662 [ + + ]: 716 : if (sorted_items[i]->group != sorted_items[i - 1]->group
12663 [ + - ]: 150 : && strcmp (sorted_items[i]->group, sorted_items[i - 1]->group))
12664 : 150 : groups[ngroups++] = &sorted_items[i];
12665 : 66 : qsort (groups, ngroups, sizeof groups[0], &compare_core_item_groups);
12666 : :
12667 : : /* Write out all the groups. */
12668 : 66 : const void *last = desc;
12669 : 70 : do
12670 : : {
12671 [ + + ]: 290 : for (size_t i = 0; i < ngroups; ++i)
12672 : : {
12673 : 220 : for (const Ebl_Core_Item **item = groups[i];
12674 : 1018 : (item < &sorted_items[nitems]
12675 [ + + + + ]: 1018 : && ((*item)->group == groups[i][0]->group
12676 [ - + ]: 150 : || !strcmp ((*item)->group, groups[i][0]->group)));
12677 : 798 : ++item)
12678 : 798 : colno = handle_core_item (core, ehdr, *item, desc, colno, NULL);
12679 : :
12680 : : /* Force a line break at the end of the group. */
12681 : 220 : colno = WRAP_COLUMN;
12682 : : }
12683 : :
12684 [ + + ]: 70 : if (descsz == 0)
12685 : : break;
12686 : :
12687 : : /* This set of items consumed a certain amount of the note's data.
12688 : : If there is more data there, we have another unit of the same size.
12689 : : Loop to print that out too. */
12690 : 40 : const Ebl_Core_Item *item = &items[nitems - 1];
12691 : 80 : size_t eltsz = item->offset + gelf_fsize (core, item->type,
12692 : 40 : item->count ?: 1, EV_CURRENT);
12693 : :
12694 : 40 : int reps = -1;
12695 : 40 : do
12696 : : {
12697 : 40 : ++reps;
12698 : 40 : desc += eltsz;
12699 : 40 : descsz -= eltsz;
12700 : : }
12701 [ + + - + ]: 40 : while (descsz >= eltsz && !memcmp (desc, last, eltsz));
12702 : :
12703 [ + - ]: 40 : if (reps == 1)
12704 : : {
12705 : : /* For just one repeat, print it unabridged twice. */
12706 : 40 : desc -= eltsz;
12707 : 40 : descsz += eltsz;
12708 : : }
12709 [ + - ]: 40 : else if (reps > 1)
12710 : 0 : printf (_("\n%*s... <repeats %u more times> ..."),
12711 : : ITEM_INDENT, "", reps);
12712 : :
12713 : 40 : last = desc;
12714 : : }
12715 [ + + ]: 40 : while (descsz > 0);
12716 : :
12717 : : return colno;
12718 : : }
12719 : :
12720 : : static unsigned int
12721 : 324 : handle_core_register (Ebl *ebl, Elf *core, int maxregname,
12722 : : const Ebl_Register_Location *regloc, const void *desc,
12723 : : unsigned int colno)
12724 : : {
12725 [ - + ]: 324 : if (regloc->bits % 8 != 0)
12726 : : {
12727 : 0 : error (0, 0, "Warning: Cannot handle register with %" PRIu8 "bits\n",
12728 : : regloc->bits);
12729 : 0 : return colno;
12730 : : }
12731 : :
12732 : 324 : desc += regloc->offset;
12733 : :
12734 [ + + ]: 1172 : for (int reg = regloc->regno; reg < regloc->regno + regloc->count; ++reg)
12735 : : {
12736 : 848 : char name[REGNAMESZ];
12737 : 848 : int bits;
12738 : 848 : int type;
12739 : 848 : register_info (ebl, reg, regloc, name, &bits, &type);
12740 : :
12741 : : #define TYPES \
12742 : : BITS (8, BYTE, "%4" PRId8, "0x%.2" PRIx8); \
12743 : : BITS (16, HALF, "%6" PRId16, "0x%.4" PRIx16); \
12744 : : BITS (32, WORD, "%11" PRId32, " 0x%.8" PRIx32); \
12745 : : BITS (64, XWORD, "%20" PRId64, " 0x%.16" PRIx64)
12746 : :
12747 : : #define BITS(bits, xtype, sfmt, ufmt) \
12748 : : uint##bits##_t b##bits; int##bits##_t b##bits##s
12749 : 848 : union { TYPES; uint64_t b128[2]; } value;
12750 : : #undef BITS
12751 : :
12752 [ + + ]: 848 : switch (type)
12753 : : {
12754 : 672 : case DW_ATE_unsigned:
12755 : : case DW_ATE_signed:
12756 : : case DW_ATE_address:
12757 [ - + + + : 672 : switch (bits)
+ - ]
12758 : : {
12759 : : #define BITS(bits, xtype, sfmt, ufmt) \
12760 : : case bits: \
12761 : : desc = convert (core, ELF_T_##xtype, 1, &value, desc, 0); \
12762 : : if (type == DW_ATE_signed) \
12763 : : colno = print_core_item (colno, ' ', WRAP_COLUMN, \
12764 : : maxregname, name, \
12765 : : sfmt, value.b##bits##s); \
12766 : : else \
12767 : : colno = print_core_item (colno, ' ', WRAP_COLUMN, \
12768 : : maxregname, name, \
12769 : : ufmt, value.b##bits); \
12770 : : break
12771 : :
12772 [ - - - + : 576 : TYPES;
+ + + + ]
12773 : :
12774 : 96 : case 128:
12775 [ - + ]: 96 : assert (type == DW_ATE_unsigned);
12776 : 96 : desc = convert (core, ELF_T_XWORD, 2, &value, desc, 0);
12777 : 96 : int be = elf_getident (core, NULL)[EI_DATA] == ELFDATA2MSB;
12778 : 96 : colno = print_core_item (colno, ' ', WRAP_COLUMN,
12779 : : maxregname, name,
12780 : : "0x%.16" PRIx64 "%.16" PRIx64,
12781 : 96 : value.b128[!be], value.b128[be]);
12782 : 96 : break;
12783 : :
12784 : 0 : default:
12785 : 0 : abort ();
12786 : : #undef BITS
12787 : : }
12788 : : break;
12789 : :
12790 : 176 : default:
12791 : : /* Print each byte in hex, the whole thing in native byte order. */
12792 [ - + ]: 176 : assert (bits % 8 == 0);
12793 : 176 : const uint8_t *bytes = desc;
12794 : 176 : desc += bits / 8;
12795 : 176 : char hex[bits / 4 + 1];
12796 : 176 : hex[bits / 4] = '\0';
12797 : 176 : int incr = 1;
12798 [ + + ]: 176 : if (elf_getident (core, NULL)[EI_DATA] == ELFDATA2LSB)
12799 : : {
12800 : 96 : bytes += bits / 8 - 1;
12801 : 96 : incr = -1;
12802 : : }
12803 : 176 : size_t idx = 0;
12804 [ + + ]: 1744 : for (char *h = hex; bits > 0; bits -= 8, idx += incr)
12805 : : {
12806 : 1568 : *h++ = "0123456789abcdef"[bytes[idx] >> 4];
12807 : 1568 : *h++ = "0123456789abcdef"[bytes[idx] & 0xf];
12808 : : }
12809 : 176 : colno = print_core_item (colno, ' ', WRAP_COLUMN,
12810 : : maxregname, name, "0x%s", hex);
12811 : 176 : break;
12812 : : }
12813 : 848 : desc += regloc->pad;
12814 : :
12815 : : #undef TYPES
12816 : : }
12817 : :
12818 : : return colno;
12819 : : }
12820 : :
12821 : :
12822 : : struct register_info
12823 : : {
12824 : : const Ebl_Register_Location *regloc;
12825 : : const char *set;
12826 : : char name[REGNAMESZ];
12827 : : int regno;
12828 : : int bits;
12829 : : int type;
12830 : : };
12831 : :
12832 : : static int
12833 : 4132 : register_bitpos (const struct register_info *r)
12834 : : {
12835 : 4132 : return (r->regloc->offset * 8
12836 : 4132 : + ((r->regno - r->regloc->regno)
12837 : 4132 : * (r->regloc->bits + r->regloc->pad * 8)));
12838 : : }
12839 : :
12840 : : static int
12841 : 2124 : compare_sets_by_info (const struct register_info *r1,
12842 : : const struct register_info *r2)
12843 : : {
12844 : 2124 : return ((int) r2->bits - (int) r1->bits
12845 [ + + ]: 2124 : ?: register_bitpos (r1) - register_bitpos (r2));
12846 : : }
12847 : :
12848 : : /* Sort registers by set, and by size and layout offset within each set. */
12849 : : static int
12850 : 9190 : compare_registers (const void *a, const void *b)
12851 : : {
12852 : 9190 : const struct register_info *r1 = a;
12853 : 9190 : const struct register_info *r2 = b;
12854 : :
12855 : : /* Unused elements sort last. */
12856 [ + + ]: 9190 : if (r1->regloc == NULL)
12857 : 6618 : return r2->regloc == NULL ? 0 : 1;
12858 [ + + ]: 2572 : if (r2->regloc == NULL)
12859 : : return -1;
12860 : :
12861 [ + + ]: 2242 : return ((r1->set == r2->set ? 0 : strcmp (r1->set, r2->set))
12862 [ - + ]: 2242 : ?: compare_sets_by_info (r1, r2));
12863 : : }
12864 : :
12865 : : /* Sort register sets by layout offset of the first register in the set. */
12866 : : static int
12867 : 40 : compare_register_sets (const void *a, const void *b)
12868 : : {
12869 : 40 : const struct register_info *const *p1 = a;
12870 : 40 : const struct register_info *const *p2 = b;
12871 : 40 : return compare_sets_by_info (*p1, *p2);
12872 : : }
12873 : :
12874 : : static inline bool
12875 : 1728 : same_set (const struct register_info *a,
12876 : : const struct register_info *b,
12877 : : const struct register_info *regs,
12878 : : size_t maxnreg)
12879 : : {
12880 [ + - ]: 1728 : return (a < ®s[maxnreg] && a->regloc != NULL
12881 [ + - + + ]: 1728 : && b < ®s[maxnreg] && b->regloc != NULL
12882 [ + + ]: 1692 : && a->bits == b->bits
12883 [ + - + + : 3384 : && (a->set == b->set || !strcmp (a->set, b->set)));
- + ]
12884 : : }
12885 : :
12886 : : static unsigned int
12887 : 74 : handle_core_registers (Ebl *ebl, Elf *core, const void *desc,
12888 : : const Ebl_Register_Location *reglocs, size_t nregloc)
12889 : 74 : {
12890 [ + + ]: 74 : if (nregloc == 0)
12891 : : return 0;
12892 : :
12893 : 36 : ssize_t maxnreg = ebl_register_info (ebl, 0, NULL, 0, NULL, NULL, NULL, NULL);
12894 [ - + ]: 36 : if (maxnreg <= 0)
12895 : : {
12896 [ # # ]: 0 : for (size_t i = 0; i < nregloc; ++i)
12897 : 0 : if (maxnreg < reglocs[i].regno + reglocs[i].count)
12898 : : maxnreg = reglocs[i].regno + reglocs[i].count;
12899 [ # # ]: 0 : assert (maxnreg > 0);
12900 : : }
12901 : :
12902 : 36 : struct register_info regs[maxnreg];
12903 : 36 : memset (regs, 0, sizeof regs);
12904 : :
12905 : : /* Sort to collect the sets together. */
12906 : 36 : int maxreg = 0;
12907 [ + + ]: 360 : for (size_t i = 0; i < nregloc; ++i)
12908 : 324 : for (int reg = reglocs[i].regno;
12909 [ + + ]: 1172 : reg < reglocs[i].regno + reglocs[i].count;
12910 : 848 : ++reg)
12911 : : {
12912 [ - + ]: 848 : assert (reg < maxnreg);
12913 : 848 : if (reg > maxreg)
12914 : : maxreg = reg;
12915 : 848 : struct register_info *info = ®s[reg];
12916 : 848 : info->regloc = ®locs[i];
12917 : 848 : info->regno = reg;
12918 : 848 : info->set = register_info (ebl, reg, ®locs[i],
12919 : 848 : info->name, &info->bits, &info->type);
12920 : : }
12921 : 36 : qsort (regs, maxreg + 1, sizeof regs[0], &compare_registers);
12922 : :
12923 : : /* Collect the unique sets and sort them. */
12924 : 36 : struct register_info *sets[maxreg + 1];
12925 : 36 : sets[0] = ®s[0];
12926 : 36 : size_t nsets = 1;
12927 [ + + ]: 2558 : for (int i = 1; i <= maxreg; ++i)
12928 [ + + ]: 2522 : if (regs[i].regloc != NULL
12929 [ + + ]: 812 : && !same_set (®s[i], ®s[i - 1], regs, maxnreg))
12930 : 32 : sets[nsets++] = ®s[i];
12931 : 36 : qsort (sets, nsets, sizeof sets[0], &compare_register_sets);
12932 : :
12933 : : /* Write out all the sets. */
12934 : 36 : unsigned int colno = 0;
12935 [ + + ]: 104 : for (size_t i = 0; i < nsets; ++i)
12936 : : {
12937 : : /* Find the longest name of a register in this set. */
12938 : 68 : size_t maxname = 0;
12939 : 68 : const struct register_info *end;
12940 [ + + ]: 916 : for (end = sets[i]; same_set (sets[i], end, regs, maxnreg); ++end)
12941 : : {
12942 : 848 : size_t len = strlen (end->name);
12943 : 848 : if (len > maxname)
12944 : : maxname = len;
12945 : : }
12946 : :
12947 : : for (const struct register_info *reg = sets[i];
12948 [ + + ]: 392 : reg < end;
12949 : 324 : reg += reg->regloc->count ?: 1)
12950 [ + - ]: 324 : colno = handle_core_register (ebl, core, maxname,
12951 : 324 : reg->regloc, desc, colno);
12952 : :
12953 : : /* Force a line break at the end of the group. */
12954 : 68 : colno = WRAP_COLUMN;
12955 : : }
12956 : :
12957 : : return colno;
12958 : : }
12959 : :
12960 : : static void
12961 : 18 : handle_auxv_note (Ebl *ebl, Elf *core, GElf_Word descsz, GElf_Off desc_pos)
12962 : : {
12963 : 18 : Elf_Data *data = elf_getdata_rawchunk (core, desc_pos, descsz, ELF_T_AUXV);
12964 [ - + ]: 18 : if (data == NULL)
12965 : 0 : elf_error:
12966 : 0 : error_exit (0, _("cannot convert core note data: %s"), elf_errmsg (-1));
12967 : :
12968 : 18 : const size_t nauxv = descsz / gelf_fsize (core, ELF_T_AUXV, 1, EV_CURRENT);
12969 [ + + ]: 354 : for (size_t i = 0; i < nauxv; ++i)
12970 : : {
12971 : 336 : GElf_auxv_t av_mem;
12972 : 336 : GElf_auxv_t *av = gelf_getauxv (data, i, &av_mem);
12973 [ - + ]: 336 : if (av == NULL)
12974 : 0 : goto elf_error;
12975 : :
12976 : 336 : const char *name;
12977 : 336 : const char *fmt;
12978 [ - + ]: 336 : if (ebl_auxv_info (ebl, av->a_type, &name, &fmt) == 0)
12979 : : {
12980 : : /* Unknown type. */
12981 [ # # ]: 0 : if (av->a_un.a_val == 0)
12982 : 0 : printf (" %" PRIu64 "\n", av->a_type);
12983 : : else
12984 : 0 : printf (" %" PRIu64 ": %#" PRIx64 "\n",
12985 : : av->a_type, av->a_un.a_val);
12986 : : }
12987 : : else
12988 [ + + + - : 336 : switch (fmt[0])
+ - ]
12989 : : {
12990 : 18 : case '\0': /* Normally zero. */
12991 [ + - ]: 18 : if (av->a_un.a_val == 0)
12992 : : {
12993 : 18 : printf (" %s\n", name);
12994 : : break;
12995 : : }
12996 : 148 : FALLTHROUGH;
12997 : : case 'x': /* hex */
12998 : : case 'p': /* address */
12999 : : case 's': /* address of string */
13000 : 148 : printf (" %s: %#" PRIx64 "\n", name, av->a_un.a_val);
13001 : : break;
13002 : 162 : case 'u':
13003 : 162 : printf (" %s: %" PRIu64 "\n", name, av->a_un.a_val);
13004 : : break;
13005 : 0 : case 'd':
13006 : 0 : printf (" %s: %" PRId64 "\n", name, av->a_un.a_val);
13007 : : break;
13008 : :
13009 : 8 : case 'b':
13010 : 8 : printf (" %s: %#" PRIx64 " ", name, av->a_un.a_val);
13011 : 8 : GElf_Xword bit = 1;
13012 : 8 : const char *pfx = "<";
13013 [ + + ]: 220 : for (const char *p = fmt + 1; *p != 0; p = strchr (p, '\0') + 1)
13014 : : {
13015 [ + + ]: 212 : if (av->a_un.a_val & bit)
13016 : : {
13017 : 154 : printf ("%s%s", pfx, p);
13018 : 154 : pfx = " ";
13019 : : }
13020 : 212 : bit <<= 1;
13021 : : }
13022 : 336 : printf (">\n");
13023 : : break;
13024 : :
13025 : 0 : default:
13026 : 0 : abort ();
13027 : : }
13028 : : }
13029 : 18 : }
13030 : :
13031 : : static bool
13032 : 390 : buf_has_data (unsigned char const *ptr, unsigned char const *end, size_t sz)
13033 : : {
13034 [ - + - + ]: 390 : return ptr < end && (size_t) (end - ptr) >= sz;
13035 : : }
13036 : :
13037 : : static bool
13038 : 36 : buf_read_int (Elf *core, unsigned char const **ptrp, unsigned char const *end,
13039 : : int *retp)
13040 : : {
13041 [ + - + - ]: 72 : if (! buf_has_data (*ptrp, end, 4))
13042 : : return false;
13043 : :
13044 : 36 : *ptrp = convert (core, ELF_T_WORD, 1, retp, *ptrp, 4);
13045 : 36 : return true;
13046 : : }
13047 : :
13048 : : static bool
13049 : 354 : buf_read_ulong (Elf *core, unsigned char const **ptrp, unsigned char const *end,
13050 : : uint64_t *retp)
13051 : : {
13052 : 354 : size_t sz = gelf_fsize (core, ELF_T_ADDR, 1, EV_CURRENT);
13053 [ + - + - ]: 708 : if (! buf_has_data (*ptrp, end, sz))
13054 : : return false;
13055 : :
13056 : 354 : union
13057 : : {
13058 : : uint64_t u64;
13059 : : uint32_t u32;
13060 : : } u;
13061 : :
13062 : 354 : *ptrp = convert (core, ELF_T_ADDR, 1, &u, *ptrp, sz);
13063 : :
13064 [ + + ]: 354 : if (sz == 4)
13065 : 186 : *retp = u.u32;
13066 : : else
13067 : 168 : *retp = u.u64;
13068 : : return true;
13069 : : }
13070 : :
13071 : : static void
13072 : 12 : handle_siginfo_note (Elf *core, GElf_Word descsz, GElf_Off desc_pos)
13073 : : {
13074 : 12 : Elf_Data *data = elf_getdata_rawchunk (core, desc_pos, descsz, ELF_T_BYTE);
13075 [ - + ]: 12 : if (data == NULL)
13076 : 0 : error_exit (0, _("cannot convert core note data: %s"), elf_errmsg (-1));
13077 : :
13078 : 12 : unsigned char const *ptr = data->d_buf;
13079 : 12 : unsigned char const *const end = data->d_buf + data->d_size;
13080 : :
13081 : : /* Siginfo head is three ints: signal number, error number, origin
13082 : : code. */
13083 : 12 : int si_signo, si_errno, si_code;
13084 [ - + ]: 12 : if (! buf_read_int (core, &ptr, end, &si_signo)
13085 [ - + ]: 12 : || ! buf_read_int (core, &ptr, end, &si_errno)
13086 [ - + ]: 12 : || ! buf_read_int (core, &ptr, end, &si_code))
13087 : : {
13088 : 0 : fail:
13089 : 0 : printf (" Not enough data in NT_SIGINFO note.\n");
13090 : 0 : return;
13091 : : }
13092 : :
13093 : : /* Next is a pointer-aligned union of structures. On 64-bit
13094 : : machines, that implies a word of padding. */
13095 [ + + ]: 12 : if (gelf_getclass (core) == ELFCLASS64)
13096 : 6 : ptr += 4;
13097 : :
13098 : 12 : printf (" si_signo: %d, si_errno: %d, si_code: %d\n",
13099 : : si_signo, si_errno, si_code);
13100 : :
13101 [ + - ]: 12 : if (si_code > 0)
13102 [ + - ]: 12 : switch (si_signo)
13103 : : {
13104 : 12 : case CORE_SIGILL:
13105 : : case CORE_SIGFPE:
13106 : : case CORE_SIGSEGV:
13107 : : case CORE_SIGBUS:
13108 : : {
13109 : 12 : uint64_t addr;
13110 [ - + ]: 12 : if (! buf_read_ulong (core, &ptr, end, &addr))
13111 : 0 : goto fail;
13112 : 12 : printf (" fault address: %#" PRIx64 "\n", addr);
13113 : 12 : break;
13114 : : }
13115 : : default:
13116 : : ;
13117 : : }
13118 [ # # ]: 0 : else if (si_code == CORE_SI_USER)
13119 : : {
13120 : 0 : int pid, uid;
13121 [ # # ]: 0 : if (! buf_read_int (core, &ptr, end, &pid)
13122 [ # # ]: 0 : || ! buf_read_int (core, &ptr, end, &uid))
13123 : 0 : goto fail;
13124 : 0 : printf (" sender PID: %d, sender UID: %d\n", pid, uid);
13125 : : }
13126 : : }
13127 : :
13128 : : static void
13129 : 12 : handle_file_note (Elf *core, GElf_Word descsz, GElf_Off desc_pos)
13130 : : {
13131 : 12 : Elf_Data *data = elf_getdata_rawchunk (core, desc_pos, descsz, ELF_T_BYTE);
13132 [ - + ]: 12 : if (data == NULL)
13133 : 0 : error_exit (0, _("cannot convert core note data: %s"), elf_errmsg (-1));
13134 : :
13135 : 12 : unsigned char const *ptr = data->d_buf;
13136 : 12 : unsigned char const *const end = data->d_buf + data->d_size;
13137 : :
13138 : 12 : uint64_t count, page_size;
13139 [ - + ]: 12 : if (! buf_read_ulong (core, &ptr, end, &count)
13140 [ - + ]: 12 : || ! buf_read_ulong (core, &ptr, end, &page_size))
13141 : : {
13142 : 0 : fail:
13143 : 0 : printf (" Not enough data in NT_FILE note.\n");
13144 : 0 : return;
13145 : : }
13146 : :
13147 : 12 : size_t addrsize = gelf_fsize (core, ELF_T_ADDR, 1, EV_CURRENT);
13148 : 12 : uint64_t maxcount = (size_t) (end - ptr) / (3 * addrsize);
13149 [ - + ]: 12 : if (count > maxcount)
13150 : 0 : goto fail;
13151 : :
13152 : : /* Where file names are stored. */
13153 : 12 : unsigned char const *const fstart = ptr + 3 * count * addrsize;
13154 : 12 : char const *fptr = (char *) fstart;
13155 : :
13156 : 12 : printf (" %" PRId64 " files:\n", count);
13157 [ + + ]: 118 : for (uint64_t i = 0; i < count; ++i)
13158 : : {
13159 : 106 : uint64_t mstart, mend, moffset;
13160 [ + - ]: 106 : if (! buf_read_ulong (core, &ptr, fstart, &mstart)
13161 [ + - ]: 106 : || ! buf_read_ulong (core, &ptr, fstart, &mend)
13162 [ - + ]: 106 : || ! buf_read_ulong (core, &ptr, fstart, &moffset))
13163 : 0 : goto fail;
13164 : :
13165 : 106 : const char *fnext = memchr (fptr, '\0', (char *) end - fptr);
13166 [ - + ]: 106 : if (fnext == NULL)
13167 : 0 : goto fail;
13168 : :
13169 : 106 : int ct = printf (" %08" PRIx64 "-%08" PRIx64
13170 : : " %08" PRIx64 " %" PRId64,
13171 : : mstart, mend, moffset * page_size, mend - mstart);
13172 [ + - ]: 106 : printf ("%*s%s\n", ct > 50 ? 3 : 53 - ct, "", fptr);
13173 : :
13174 : 106 : fptr = fnext + 1;
13175 : : }
13176 : : }
13177 : :
13178 : : static void
13179 : 76 : handle_core_note (Ebl *ebl, const GElf_Ehdr *ehdr, const GElf_Nhdr *nhdr,
13180 : : const char *name, const void *desc)
13181 : : {
13182 : 76 : GElf_Word regs_offset;
13183 : 76 : size_t nregloc;
13184 : 76 : const Ebl_Register_Location *reglocs;
13185 : 76 : size_t nitems;
13186 : 76 : const Ebl_Core_Item *items;
13187 : :
13188 [ + + ]: 76 : if (! ebl_core_note (ebl, nhdr, name, desc,
13189 : : ®s_offset, &nregloc, ®locs, &nitems, &items))
13190 : 2 : return;
13191 : :
13192 : : /* Pass 0 for DESCSZ when there are registers in the note,
13193 : : so that the ITEMS array does not describe the whole thing.
13194 : : For non-register notes, the actual descsz might be a multiple
13195 : : of the unit size, not just exactly the unit size. */
13196 : 74 : unsigned int colno = handle_core_items (ebl->elf, ehdr, desc,
13197 [ + + ]: 74 : nregloc == 0 ? nhdr->n_descsz : 0,
13198 : : items, nitems);
13199 [ + + ]: 74 : if (colno != 0)
13200 : 68 : putchar ('\n');
13201 : :
13202 : 74 : colno = handle_core_registers (ebl, ebl->elf, desc + regs_offset,
13203 : : reglocs, nregloc);
13204 [ + + ]: 74 : if (colno != 0)
13205 : 74 : putchar ('\n');
13206 : : }
13207 : :
13208 : : static void
13209 : 506 : handle_notes_data (Ebl *ebl, const GElf_Ehdr *ehdr,
13210 : : GElf_Off start, Elf_Data *data)
13211 : : {
13212 : 506 : fputs (_(" Owner Data size Type\n"), stdout);
13213 : :
13214 [ - + ]: 506 : if (data == NULL)
13215 : 0 : goto bad_note;
13216 : :
13217 : : size_t offset = 0;
13218 : : GElf_Nhdr nhdr;
13219 : : size_t name_offset;
13220 : : size_t desc_offset;
13221 : 5120 : while (offset < data->d_size
13222 [ + + + - ]: 5120 : && (offset = gelf_getnote (data, offset,
13223 : : &nhdr, &name_offset, &desc_offset)) > 0)
13224 : : {
13225 [ + - ]: 4614 : const char *name = nhdr.n_namesz == 0 ? "" : data->d_buf + name_offset;
13226 : 4614 : const char *desc = data->d_buf + desc_offset;
13227 : :
13228 : : /* GNU Build Attributes are weird, they store most of their data
13229 : : into the owner name field. Extract just the owner name
13230 : : prefix here, then use the rest later as data. */
13231 : 4614 : bool is_gnu_build_attr
13232 : 4614 : = startswith (name, ELF_NOTE_GNU_BUILD_ATTRIBUTE_PREFIX);
13233 : 9798 : const char *print_name = (is_gnu_build_attr
13234 [ + + ]: 4614 : ? ELF_NOTE_GNU_BUILD_ATTRIBUTE_PREFIX : name);
13235 : 5184 : size_t print_namesz = (is_gnu_build_attr
13236 : 570 : ? strlen (print_name) : nhdr.n_namesz);
13237 : :
13238 : 4614 : char buf[100];
13239 : 4614 : char buf2[100];
13240 : 4614 : printf (_(" %-13.*s %9" PRId32 " %s\n"),
13241 : : (int) print_namesz, print_name, nhdr.n_descsz,
13242 [ + + ]: 4614 : ehdr->e_type == ET_CORE
13243 : 118 : ? ebl_core_note_type_name (ebl, nhdr.n_type,
13244 : : buf, sizeof (buf))
13245 : 4496 : : ebl_object_note_type_name (ebl, name, nhdr.n_type,
13246 : : nhdr.n_descsz,
13247 : : buf2, sizeof (buf2)));
13248 : :
13249 : : /* Filter out invalid entries. */
13250 : 4614 : if (memchr (name, '\0', nhdr.n_namesz) != NULL
13251 : : /* XXX For now help broken Linux kernels. */
13252 : : || 1)
13253 : : {
13254 [ + + ]: 4614 : if (ehdr->e_type == ET_CORE)
13255 : : {
13256 [ + + ]: 118 : if (nhdr.n_type == NT_AUXV
13257 [ + - ]: 18 : && (nhdr.n_namesz == 4 /* Broken old Linux kernels. */
13258 [ + - + - ]: 18 : || (nhdr.n_namesz == 5 && name[4] == '\0'))
13259 [ + - ]: 18 : && !memcmp (name, "CORE", 4))
13260 : 18 : handle_auxv_note (ebl, ebl->elf, nhdr.n_descsz,
13261 : : start + desc_offset);
13262 [ + + + - ]: 100 : else if (nhdr.n_namesz == 5 && strcmp (name, "CORE") == 0)
13263 [ + + + ]: 76 : switch (nhdr.n_type)
13264 : : {
13265 : 12 : case NT_SIGINFO:
13266 : 12 : handle_siginfo_note (ebl->elf, nhdr.n_descsz,
13267 : : start + desc_offset);
13268 : 12 : break;
13269 : :
13270 : 12 : case NT_FILE:
13271 : 12 : handle_file_note (ebl->elf, nhdr.n_descsz,
13272 : : start + desc_offset);
13273 : 12 : break;
13274 : :
13275 : 52 : default:
13276 : 52 : handle_core_note (ebl, ehdr, &nhdr, name, desc);
13277 : : }
13278 : : else
13279 : 24 : handle_core_note (ebl, ehdr, &nhdr, name, desc);
13280 : : }
13281 : : else
13282 : 4496 : ebl_object_note (ebl, nhdr.n_namesz, name, nhdr.n_type,
13283 : : nhdr.n_descsz, desc);
13284 : : }
13285 : : }
13286 : :
13287 [ + - ]: 506 : if (offset == data->d_size)
13288 : 506 : return;
13289 : :
13290 : 0 : bad_note:
13291 : 0 : error (0, 0,
13292 : 0 : _("cannot get content of note: %s"),
13293 : 0 : data != NULL ? "garbage data" : elf_errmsg (-1));
13294 : : }
13295 : :
13296 : : static void
13297 : 230 : handle_notes (Ebl *ebl, GElf_Ehdr *ehdr)
13298 : : {
13299 : : /* If we have section headers, just look for SHT_NOTE sections.
13300 : : In a debuginfo file, the program headers are not reliable. */
13301 [ + + ]: 230 : if (shnum != 0)
13302 : : {
13303 : : /* Get the section header string table index. */
13304 : 208 : size_t shstrndx;
13305 [ - + ]: 208 : if (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0)
13306 : 0 : error_exit (0, _("cannot get section header string table index"));
13307 : :
13308 : : Elf_Scn *scn = NULL;
13309 [ + + ]: 6446 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
13310 : : {
13311 : 6238 : GElf_Shdr shdr_mem;
13312 : 6238 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
13313 : :
13314 [ + - + + ]: 6238 : if (shdr == NULL || shdr->sh_type != SHT_NOTE)
13315 : : /* Not what we are looking for. */
13316 : 5756 : continue;
13317 : :
13318 [ - + ]: 482 : if (notes_section != NULL)
13319 : : {
13320 : 0 : char *sname = elf_strptr (ebl->elf, shstrndx, shdr->sh_name);
13321 [ # # # # ]: 0 : if (sname == NULL || strcmp (sname, notes_section) != 0)
13322 : 0 : continue;
13323 : : }
13324 : :
13325 : 482 : printf (_("\
13326 : : \nNote section [%2zu] '%s' of %" PRIu64 " bytes at offset %#0" PRIx64 ":\n"),
13327 : : elf_ndxscn (scn),
13328 : 482 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
13329 : : shdr->sh_size, shdr->sh_offset);
13330 : :
13331 : 482 : handle_notes_data (ebl, ehdr, shdr->sh_offset,
13332 : : elf_getdata (scn, NULL));
13333 : : }
13334 : 208 : return;
13335 : : }
13336 : :
13337 : : /* We have to look through the program header to find the note
13338 : : sections. There can be more than one. */
13339 [ + + ]: 280 : for (size_t cnt = 0; cnt < phnum; ++cnt)
13340 : : {
13341 : 258 : GElf_Phdr mem;
13342 : 258 : GElf_Phdr *phdr = gelf_getphdr (ebl->elf, cnt, &mem);
13343 : :
13344 [ + - + + ]: 258 : if (phdr == NULL || phdr->p_type != PT_NOTE)
13345 : : /* Not what we are looking for. */
13346 : 234 : continue;
13347 : :
13348 : 24 : printf (_("\
13349 : : \nNote segment of %" PRIu64 " bytes at offset %#0" PRIx64 ":\n"),
13350 : : phdr->p_filesz, phdr->p_offset);
13351 : :
13352 : 24 : handle_notes_data (ebl, ehdr, phdr->p_offset,
13353 : : elf_getdata_rawchunk (ebl->elf,
13354 : 24 : phdr->p_offset, phdr->p_filesz,
13355 [ + + ]: 24 : (phdr->p_align == 8
13356 : : ? ELF_T_NHDR8 : ELF_T_NHDR)));
13357 : : }
13358 : : }
13359 : :
13360 : :
13361 : : static void
13362 : 12 : hex_dump (const uint8_t *data, size_t len)
13363 : : {
13364 : 12 : size_t pos = 0;
13365 [ + + ]: 56 : while (pos < len)
13366 : : {
13367 : 44 : printf (" 0x%08zx ", pos);
13368 : :
13369 : 44 : const size_t chunk = MIN (len - pos, 16);
13370 : :
13371 [ + + ]: 688 : for (size_t i = 0; i < chunk; ++i)
13372 [ + + ]: 644 : if (i % 4 == 3)
13373 : 160 : printf ("%02x ", data[pos + i]);
13374 : : else
13375 : 644 : printf ("%02x", data[pos + i]);
13376 : :
13377 [ + + ]: 44 : if (chunk < 16)
13378 : 4 : printf ("%*s", (int) ((16 - chunk) * 2 + (16 - chunk + 3) / 4), "");
13379 : :
13380 [ + + ]: 688 : for (size_t i = 0; i < chunk; ++i)
13381 : : {
13382 : 644 : unsigned char b = data[pos + i];
13383 [ + + ]: 1288 : printf ("%c", isprint (b) ? b : '.');
13384 : : }
13385 : :
13386 : 44 : putchar ('\n');
13387 : 44 : pos += chunk;
13388 : : }
13389 : 12 : }
13390 : :
13391 : : static void
13392 : 12 : dump_data_section (Elf_Scn *scn, const GElf_Shdr *shdr, const char *name)
13393 : : {
13394 [ + - - + ]: 12 : if (shdr->sh_size == 0 || shdr->sh_type == SHT_NOBITS)
13395 : 0 : printf (_("\nSection [%zu] '%s' has no data to dump.\n"),
13396 : : elf_ndxscn (scn), name);
13397 : : else
13398 : : {
13399 [ + + ]: 12 : if (print_decompress)
13400 : : {
13401 : : /* We try to decompress the section, but keep the old shdr around
13402 : : so we can show both the original shdr size and the uncompressed
13403 : : data size. */
13404 [ + + ]: 8 : if ((shdr->sh_flags & SHF_COMPRESSED) != 0)
13405 : : {
13406 [ - + ]: 4 : if (elf_compress (scn, 0, 0) < 0)
13407 : 0 : printf ("WARNING: %s [%zd]\n",
13408 : : _("Couldn't uncompress section"),
13409 : : elf_ndxscn (scn));
13410 : : }
13411 [ + - + - ]: 4 : else if (name && startswith (name, ".zdebug"))
13412 : : {
13413 [ - + ]: 4 : if (elf_compress_gnu (scn, 0, 0) < 0)
13414 : 0 : printf ("WARNING: %s [%zd]\n",
13415 : : _("Couldn't uncompress section"),
13416 : : elf_ndxscn (scn));
13417 : : }
13418 : : }
13419 : :
13420 : 12 : Elf_Data *data = elf_rawdata (scn, NULL);
13421 [ - + ]: 12 : if (data == NULL)
13422 : 0 : error (0, 0, _("cannot get data for section [%zu] '%s': %s"),
13423 : : elf_ndxscn (scn), name, elf_errmsg (-1));
13424 : : else
13425 : : {
13426 [ + + ]: 12 : if (data->d_size == shdr->sh_size)
13427 : 4 : printf (_("\nHex dump of section [%zu] '%s', %" PRIu64
13428 : : " bytes at offset %#0" PRIx64 ":\n"),
13429 : : elf_ndxscn (scn), name,
13430 : 4 : shdr->sh_size, shdr->sh_offset);
13431 : : else
13432 : 8 : printf (_("\nHex dump of section [%zu] '%s', %" PRIu64
13433 : : " bytes (%zd uncompressed) at offset %#0"
13434 : : PRIx64 ":\n"),
13435 : : elf_ndxscn (scn), name,
13436 : 8 : shdr->sh_size, data->d_size, shdr->sh_offset);
13437 : 12 : hex_dump (data->d_buf, data->d_size);
13438 : : }
13439 : : }
13440 : 12 : }
13441 : :
13442 : : static void
13443 : 374 : print_string_section (Elf_Scn *scn, const GElf_Shdr *shdr, const char *name)
13444 : : {
13445 [ + - + + ]: 374 : if (shdr->sh_size == 0 || shdr->sh_type == SHT_NOBITS)
13446 : 42 : printf (_("\nSection [%zu] '%s' has no strings to dump.\n"),
13447 : : elf_ndxscn (scn), name);
13448 : : else
13449 : : {
13450 [ + + ]: 332 : if (print_decompress)
13451 : : {
13452 : : /* We try to decompress the section, but keep the old shdr around
13453 : : so we can show both the original shdr size and the uncompressed
13454 : : data size. */
13455 [ - + ]: 2 : if ((shdr->sh_flags & SHF_COMPRESSED) != 0)
13456 : : {
13457 [ # # ]: 0 : if (elf_compress (scn, 0, 0) < 0)
13458 : 0 : printf ("WARNING: %s [%zd]\n",
13459 : : _("Couldn't uncompress section"),
13460 : : elf_ndxscn (scn));
13461 : : }
13462 [ + - + - ]: 2 : else if (name && startswith (name, ".zdebug"))
13463 : : {
13464 [ - + ]: 2 : if (elf_compress_gnu (scn, 0, 0) < 0)
13465 : 0 : printf ("WARNING: %s [%zd]\n",
13466 : : _("Couldn't uncompress section"),
13467 : : elf_ndxscn (scn));
13468 : : }
13469 : : }
13470 : :
13471 : 332 : Elf_Data *data = elf_rawdata (scn, NULL);
13472 [ - + ]: 332 : if (data == NULL)
13473 : 0 : error (0, 0, _("cannot get data for section [%zu] '%s': %s"),
13474 : : elf_ndxscn (scn), name, elf_errmsg (-1));
13475 : : else
13476 : : {
13477 [ + + ]: 332 : if (data->d_size == shdr->sh_size)
13478 : 330 : printf (_("\nString section [%zu] '%s' contains %" PRIu64
13479 : : " bytes at offset %#0" PRIx64 ":\n"),
13480 : : elf_ndxscn (scn), name,
13481 : 330 : shdr->sh_size, shdr->sh_offset);
13482 : : else
13483 : 2 : printf (_("\nString section [%zu] '%s' contains %" PRIu64
13484 : : " bytes (%zd uncompressed) at offset %#0"
13485 : : PRIx64 ":\n"),
13486 : : elf_ndxscn (scn), name,
13487 : 2 : shdr->sh_size, data->d_size, shdr->sh_offset);
13488 : :
13489 : 332 : const char *start = data->d_buf;
13490 : 332 : const char *const limit = start + data->d_size;
13491 : 45550 : do
13492 : : {
13493 : 45550 : const char *end = memchr (start, '\0', limit - start);
13494 : 45550 : const size_t pos = start - (const char *) data->d_buf;
13495 [ - + ]: 45550 : if (unlikely (end == NULL))
13496 : : {
13497 : 0 : printf (" [%6zx]- %.*s\n",
13498 : : pos, (int) (limit - start), start);
13499 : : break;
13500 : : }
13501 : 45550 : printf (" [%6zx] %s\n", pos, start);
13502 : 45550 : start = end + 1;
13503 [ + + ]: 45550 : } while (start < limit);
13504 : : }
13505 : : }
13506 : 374 : }
13507 : :
13508 : : static void
13509 : 198 : for_each_section_argument (Elf *elf, const struct section_argument *list,
13510 : : void (*dump) (Elf_Scn *scn, const GElf_Shdr *shdr,
13511 : : const char *name))
13512 : : {
13513 : : /* Get the section header string table index. */
13514 : 198 : size_t shstrndx;
13515 [ - + ]: 198 : if (elf_getshdrstrndx (elf, &shstrndx) < 0)
13516 : 0 : error_exit (0, _("cannot get section header string table index"));
13517 : :
13518 [ + + ]: 764 : for (const struct section_argument *a = list; a != NULL; a = a->next)
13519 : : {
13520 : 566 : Elf_Scn *scn;
13521 : 566 : GElf_Shdr shdr_mem;
13522 : 566 : const char *name = NULL;
13523 : :
13524 : 566 : char *endp = NULL;
13525 : 566 : unsigned long int shndx = strtoul (a->arg, &endp, 0);
13526 [ + + - + ]: 566 : if (endp != a->arg && *endp == '\0')
13527 : : {
13528 : 2 : scn = elf_getscn (elf, shndx);
13529 [ - + ]: 2 : if (scn == NULL)
13530 : : {
13531 : 0 : error (0, 0, _("\nsection [%lu] does not exist"), shndx);
13532 : 0 : continue;
13533 : : }
13534 : :
13535 [ - + ]: 2 : if (gelf_getshdr (scn, &shdr_mem) == NULL)
13536 : 0 : error_exit (0, _("cannot get section header: %s"),
13537 : : elf_errmsg (-1));
13538 : 2 : name = elf_strptr (elf, shstrndx, shdr_mem.sh_name);
13539 : 2 : (*dump) (scn, &shdr_mem, name);
13540 : : }
13541 : : else
13542 : : {
13543 : : /* Need to look up the section by name. */
13544 : : scn = NULL;
13545 : : bool found = false;
13546 [ + + ]: 18180 : while ((scn = elf_nextscn (elf, scn)) != NULL)
13547 : : {
13548 [ - + ]: 17616 : if (gelf_getshdr (scn, &shdr_mem) == NULL)
13549 : 0 : continue;
13550 : 17616 : name = elf_strptr (elf, shstrndx, shdr_mem.sh_name);
13551 [ - + ]: 17616 : if (name == NULL)
13552 : 0 : continue;
13553 [ + + ]: 17616 : if (!strcmp (name, a->arg))
13554 : : {
13555 : 384 : found = true;
13556 : 384 : (*dump) (scn, &shdr_mem, name);
13557 : : }
13558 : : }
13559 : :
13560 [ + + - + ]: 564 : if (unlikely (!found) && !a->implicit)
13561 : 566 : error (0, 0, _("\nsection '%s' does not exist"), a->arg);
13562 : : }
13563 : : }
13564 : 198 : }
13565 : :
13566 : : static void
13567 : 12 : dump_data (Ebl *ebl)
13568 : : {
13569 : 12 : for_each_section_argument (ebl->elf, dump_data_sections, &dump_data_section);
13570 : 12 : }
13571 : :
13572 : : static void
13573 : 186 : dump_strings (Ebl *ebl)
13574 : : {
13575 : 186 : for_each_section_argument (ebl->elf, string_sections, &print_string_section);
13576 : 186 : }
13577 : :
13578 : : static void
13579 : 0 : print_strings (Ebl *ebl)
13580 : : {
13581 : : /* Get the section header string table index. */
13582 : 0 : size_t shstrndx;
13583 [ # # ]: 0 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
13584 : 0 : error_exit (0, _("cannot get section header string table index"));
13585 : :
13586 : : Elf_Scn *scn;
13587 : : GElf_Shdr shdr_mem;
13588 : : const char *name;
13589 : : scn = NULL;
13590 [ # # ]: 0 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
13591 : : {
13592 [ # # ]: 0 : if (gelf_getshdr (scn, &shdr_mem) == NULL)
13593 : 0 : continue;
13594 : :
13595 [ # # ]: 0 : if (shdr_mem.sh_type != SHT_PROGBITS
13596 [ # # ]: 0 : || !(shdr_mem.sh_flags & SHF_STRINGS))
13597 : 0 : continue;
13598 : :
13599 : 0 : name = elf_strptr (ebl->elf, shstrndx, shdr_mem.sh_name);
13600 [ # # ]: 0 : if (name == NULL)
13601 : 0 : continue;
13602 : :
13603 : 0 : print_string_section (scn, &shdr_mem, name);
13604 : : }
13605 : 0 : }
13606 : :
13607 : : static void
13608 : 4 : dump_archive_index (Elf *elf, const char *fname)
13609 : : {
13610 : 4 : size_t narsym;
13611 : 4 : const Elf_Arsym *arsym = elf_getarsym (elf, &narsym);
13612 [ - + ]: 4 : if (arsym == NULL)
13613 : : {
13614 : 0 : int result = elf_errno ();
13615 [ # # ]: 0 : if (unlikely (result != ELF_E_NO_INDEX))
13616 : 0 : error_exit (0, _("cannot get symbol index of archive '%s': %s"),
13617 : : fname, elf_errmsg (result));
13618 : : else
13619 : 0 : printf (_("\nArchive '%s' has no symbol index\n"), fname);
13620 : 0 : return;
13621 : : }
13622 : :
13623 : 4 : printf (_("\nIndex of archive '%s' has %zu entries:\n"),
13624 : : fname, narsym);
13625 : :
13626 : 4 : size_t as_off = 0;
13627 [ + + ]: 22 : for (const Elf_Arsym *s = arsym; s < &arsym[narsym - 1]; ++s)
13628 : : {
13629 [ + + ]: 18 : if (s->as_off != as_off)
13630 : : {
13631 : 12 : as_off = s->as_off;
13632 : :
13633 : 12 : Elf *subelf = NULL;
13634 [ + - ]: 12 : if (unlikely (elf_rand (elf, as_off) == 0)
13635 [ - + ]: 12 : || unlikely ((subelf = elf_begin (-1, ELF_C_READ_MMAP, elf))
13636 : : == NULL))
13637 : : #if __GLIBC__ < 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ < 7)
13638 : : while (1)
13639 : : #endif
13640 : 0 : error_exit (0,
13641 : : _("cannot extract member at offset %zu in '%s': %s"),
13642 : : as_off, fname, elf_errmsg (-1));
13643 : :
13644 : 12 : const Elf_Arhdr *h = elf_getarhdr (subelf);
13645 [ + - ]: 12 : if (h != NULL)
13646 : 12 : printf (_("Archive member '%s' contains:\n"), h->ar_name);
13647 : :
13648 : 12 : elf_end (subelf);
13649 : : }
13650 : :
13651 : 18 : printf ("\t%s\n", s->as_name);
13652 : : }
13653 : : }
13654 : :
13655 : : #include "debugpred.h"
|