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 : 76868 : get_visibility_type (int value)
1246 : : {
1247 [ - + - - : 76868 : 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 [ + + ]: 1165914 : for (int cnt = 0; cnt < nentries; ++cnt)
2367 : : {
2368 : 1165632 : GElf_Rela relmem;
2369 : 1165632 : GElf_Rela *rel = gelf_getrela (data, cnt, &relmem);
2370 [ + - ]: 1165632 : if (likely (rel != NULL))
2371 : : {
2372 : 1165632 : char buf[64];
2373 : 1165632 : GElf_Sym symmem;
2374 : 1165632 : Elf32_Word xndx;
2375 : 2331264 : GElf_Sym *sym = gelf_getsymshndx (symdata, xndxdata,
2376 : 1165632 : GELF_R_SYM (rel->r_info),
2377 : : &symmem, &xndx);
2378 : :
2379 [ - + ]: 1165632 : 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 [ + + ]: 1165632 : else if (GELF_ST_TYPE (sym->st_info) != STT_SECTION
2432 [ + + ]: 627726 : && !(GELF_ST_TYPE (sym->st_info) == STT_NOTYPE
2433 [ + + ]: 100986 : && GELF_ST_BIND (sym->st_info) == STB_LOCAL
2434 [ + + ]: 99456 : && sym->st_shndx != SHN_UNDEF
2435 [ + + ]: 716 : && sym->st_value == 0)) // local start section label
2436 [ + - ]: 2421060 : printf ("\
2437 : : %#0*" PRIx64 " %-15s %#0*" PRIx64 " %+6" PRId64 " %s\n",
2438 : : class == ELFCLASS32 ? 10 : 18, rel->r_offset,
2439 [ + - ]: 627714 : 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 : 627714 : ? 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 : 627714 : 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 [ + + ]: 77134 : for (unsigned int cnt = 0; cnt < nsyms; ++cnt)
2663 : : {
2664 : 76868 : char typebuf[64];
2665 : 76868 : char bindbuf[64];
2666 : 76868 : char scnbuf[64];
2667 : 76868 : const char *sym_name;
2668 : 76868 : Elf32_Word xndx;
2669 : 76868 : GElf_Sym sym_mem;
2670 : 76868 : GElf_Sym *sym
2671 : 76868 : = gelf_getsymshndx (symdata, xndx_data, cnt, &sym_mem, &xndx);
2672 : :
2673 [ - + ]: 76868 : if (unlikely (sym == NULL))
2674 : 0 : continue;
2675 : :
2676 : : /* Determine the real section index. */
2677 [ + - ]: 76868 : if (likely (sym->st_shndx != SHN_XINDEX))
2678 : 76868 : xndx = sym->st_shndx;
2679 [ - + ]: 76868 : 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 : 76868 : sym_name = elf_strptr (ebl->elf, idx, sym->st_name);
2689 : :
2690 [ - + ]: 76868 : if (sym_name == NULL)
2691 : : sym_name = "???";
2692 : :
2693 [ + + ]: 152848 : printf (_ ("\
2694 : : %5u: %0*" PRIx64 " %6" PRId64 " %-7s %-6s %-9s %6s %s"),
2695 : 76868 : cnt, gelf_getclass (ebl->elf) == ELFCLASS32 ? 8 : 16,
2696 : : sym->st_value, sym->st_size,
2697 : 76868 : ebl_symbol_type_name (ebl, GELF_ST_TYPE (sym->st_info), typebuf,
2698 : : sizeof (typebuf)),
2699 : 76868 : ebl_symbol_binding_name (ebl, GELF_ST_BIND (sym->st_info),
2700 : : bindbuf, sizeof (bindbuf)),
2701 : 76868 : get_visibility_type (GELF_ST_VISIBILITY (sym->st_other)),
2702 : 76868 : ebl_section_name (ebl, sym->st_shndx, xndx, scnbuf,
2703 : : sizeof (scnbuf), NULL, shnum),
2704 : : sym_name);
2705 : :
2706 [ + + ]: 76868 : 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 : 76868 : 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 : symdata = elf_getdata_rawchunk (
3051 : : ebl->elf, offs[i_symtab],
3052 : : gelf_fsize (ebl->elf, ELF_T_SYM, syments, EV_CURRENT), ELF_T_SYM);
3053 : 0 : symstrdata = elf_getdata_rawchunk (ebl->elf, offs[i_strtab], addrs[i_strsz],
3054 : : ELF_T_BYTE);
3055 : 0 : versym_data = elf_getdata_rawchunk (
3056 : 0 : ebl->elf, offs[i_versym], syments * sizeof (Elf64_Half), ELF_T_HALF);
3057 : :
3058 : : /* Get the verneed_data without vernaux. */
3059 : 0 : verneed_data = elf_getdata_rawchunk (
3060 : 0 : ebl->elf, offs[i_verneed], addrs[i_verneednum] * sizeof (Elf64_Verneed),
3061 : : ELF_T_VNEED);
3062 : 0 : size_t vernauxnum = 0;
3063 : 0 : size_t vn_next_offset = 0;
3064 : :
3065 [ # # ]: 0 : for (size_t i = 0; i < addrs[i_verneednum]; i++)
3066 : : {
3067 : 0 : GElf_Verneed *verneed
3068 : 0 : = (GElf_Verneed *)(verneed_data->d_buf + vn_next_offset);
3069 : 0 : vernauxnum += verneed->vn_cnt;
3070 : 0 : vn_next_offset += verneed->vn_next;
3071 : : }
3072 : :
3073 : : /* Update the verneed_data to include the vernaux. */
3074 : 0 : verneed_data = elf_getdata_rawchunk (
3075 : : ebl->elf, offs[i_verneed],
3076 : 0 : (addrs[i_verneednum] + vernauxnum) * sizeof (GElf_Verneed), ELF_T_VNEED);
3077 : :
3078 : : /* Get the verdef_data without verdaux. */
3079 : 0 : verdef_data = elf_getdata_rawchunk (
3080 : 0 : ebl->elf, offs[i_verdef], addrs[i_verdefnum] * sizeof (Elf64_Verdef),
3081 : : ELF_T_VDEF);
3082 : 0 : size_t verdauxnum = 0;
3083 : 0 : size_t vd_next_offset = 0;
3084 : :
3085 [ # # ]: 0 : for (size_t i = 0; i < addrs[i_verdefnum]; i++)
3086 : : {
3087 : 0 : GElf_Verdef *verdef
3088 : 0 : = (GElf_Verdef *)(verdef_data->d_buf + vd_next_offset);
3089 : 0 : verdauxnum += verdef->vd_cnt;
3090 : 0 : vd_next_offset += verdef->vd_next;
3091 : : }
3092 : :
3093 : : /* Update the verdef_data to include the verdaux. */
3094 : 0 : verdef_data = elf_getdata_rawchunk (
3095 : : ebl->elf, offs[i_verdef],
3096 : 0 : (addrs[i_verdefnum] + verdauxnum) * sizeof (GElf_Verdef), ELF_T_VDEF);
3097 : :
3098 : 0 : unsigned int nsyms = (unsigned int)syments;
3099 : 0 : process_symtab (ebl, nsyms, 0, 0, 0, symdata, versym_data, symstrdata,
3100 : : verneed_data, verdef_data, NULL);
3101 : 0 : return true;
3102 : : }
3103 : :
3104 : :
3105 : : /* Print version information. */
3106 : : static void
3107 : 184 : print_verinfo (Ebl *ebl)
3108 : : {
3109 : : /* Find the version information sections. For this we have to
3110 : : search through the section table. */
3111 : 184 : Elf_Scn *scn = NULL;
3112 : :
3113 [ + + ]: 6004 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
3114 : : {
3115 : : /* Handle the section if it is part of the versioning handling. */
3116 : 5820 : GElf_Shdr shdr_mem;
3117 : 5820 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
3118 : :
3119 [ + - ]: 5820 : if (likely (shdr != NULL))
3120 : : {
3121 [ + + ]: 5820 : if (shdr->sh_type == SHT_GNU_verneed)
3122 : 130 : handle_verneed (ebl, scn, shdr);
3123 [ + + ]: 5690 : else if (shdr->sh_type == SHT_GNU_verdef)
3124 : 4 : handle_verdef (ebl, scn, shdr);
3125 [ + + ]: 5686 : else if (shdr->sh_type == SHT_GNU_versym)
3126 : 130 : handle_versym (ebl, scn, shdr);
3127 : : }
3128 : : }
3129 : 184 : }
3130 : :
3131 : :
3132 : : static const char *
3133 : 374 : get_ver_flags (unsigned int flags)
3134 : : {
3135 : 374 : static char buf[32];
3136 : 374 : char *endp;
3137 : :
3138 [ + + ]: 374 : if (flags == 0)
3139 : 370 : return _("none");
3140 : :
3141 [ + - ]: 4 : if (flags & VER_FLG_BASE)
3142 : 4 : endp = stpcpy (buf, "BASE ");
3143 : : else
3144 : : endp = buf;
3145 : :
3146 [ - + ]: 4 : if (flags & VER_FLG_WEAK)
3147 : : {
3148 [ # # ]: 0 : if (endp != buf)
3149 : 0 : endp = stpcpy (endp, "| ");
3150 : :
3151 : 0 : endp = stpcpy (endp, "WEAK ");
3152 : : }
3153 : :
3154 [ - + ]: 4 : if (unlikely (flags & ~(VER_FLG_BASE | VER_FLG_WEAK)))
3155 : : {
3156 : 0 : strncpy (endp, _("| <unknown>"), buf + sizeof (buf) - endp);
3157 : 0 : buf[sizeof (buf) - 1] = '\0';
3158 : : }
3159 : :
3160 : : return buf;
3161 : : }
3162 : :
3163 : :
3164 : : static void
3165 : 130 : handle_verneed (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr)
3166 : : {
3167 : 130 : int class = gelf_getclass (ebl->elf);
3168 : :
3169 : : /* Get the data of the section. */
3170 : 130 : Elf_Data *data = elf_getdata (scn, NULL);
3171 [ - + ]: 130 : if (data == NULL)
3172 : 0 : return;
3173 : :
3174 : : /* Get the section header string table index. */
3175 : 130 : size_t shstrndx;
3176 [ - + ]: 130 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
3177 : 0 : error_exit (0, _("cannot get section header string table index"));
3178 : :
3179 : 130 : GElf_Shdr glink_mem;
3180 : 130 : GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
3181 : : &glink_mem);
3182 [ - + ]: 130 : if (glink == NULL)
3183 : 0 : error_exit (0, _("invalid sh_link value in section %zu"),
3184 : : elf_ndxscn (scn));
3185 : :
3186 : 130 : printf (ngettext ("\
3187 : : \nVersion needs section [%2u] '%s' contains %d entry:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'\n",
3188 : : "\
3189 : : \nVersion needs section [%2u] '%s' contains %d entries:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'\n",
3190 : : shdr->sh_info),
3191 : 130 : (unsigned int) elf_ndxscn (scn),
3192 : 130 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name), shdr->sh_info,
3193 : : class == ELFCLASS32 ? 10 : 18, shdr->sh_addr,
3194 : : shdr->sh_offset,
3195 [ + + ]: 130 : (unsigned int) shdr->sh_link,
3196 : 130 : elf_strptr (ebl->elf, shstrndx, glink->sh_name));
3197 : :
3198 : 130 : unsigned int offset = 0;
3199 [ + - ]: 184 : for (unsigned int cnt = shdr->sh_info; cnt > 0; cnt--)
3200 : : {
3201 : : /* Get the data at the next offset. */
3202 : 184 : GElf_Verneed needmem;
3203 : 184 : GElf_Verneed *need = gelf_getverneed (data, offset, &needmem);
3204 [ + - ]: 184 : if (unlikely (need == NULL))
3205 : : break;
3206 : :
3207 : 552 : printf (_(" %#06x: Version: %hu File: %s Cnt: %hu\n"),
3208 : 184 : offset, (unsigned short int) need->vn_version,
3209 : 184 : elf_strptr (ebl->elf, shdr->sh_link, need->vn_file),
3210 : 184 : (unsigned short int) need->vn_cnt);
3211 : :
3212 : 184 : unsigned int auxoffset = offset + need->vn_aux;
3213 [ + - ]: 350 : for (unsigned int cnt2 = need->vn_cnt; cnt2 > 0; cnt2--)
3214 : : {
3215 : 350 : GElf_Vernaux auxmem;
3216 : 350 : GElf_Vernaux *aux = gelf_getvernaux (data, auxoffset, &auxmem);
3217 [ + - ]: 350 : if (unlikely (aux == NULL))
3218 : : break;
3219 : :
3220 : 350 : printf (_(" %#06x: Name: %s Flags: %s Version: %hu\n"),
3221 : : auxoffset,
3222 : 350 : elf_strptr (ebl->elf, shdr->sh_link, aux->vna_name),
3223 : 350 : get_ver_flags (aux->vna_flags),
3224 : 350 : (unsigned short int) aux->vna_other);
3225 : :
3226 [ + + ]: 350 : if (aux->vna_next == 0)
3227 : : break;
3228 : :
3229 : 166 : auxoffset += aux->vna_next;
3230 : : }
3231 : :
3232 : : /* Find the next offset. */
3233 [ + + ]: 184 : if (need->vn_next == 0)
3234 : : break;
3235 : :
3236 : 54 : offset += need->vn_next;
3237 : : }
3238 : : }
3239 : :
3240 : :
3241 : : static void
3242 : 4 : handle_verdef (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr)
3243 : : {
3244 : : /* Get the data of the section. */
3245 : 4 : Elf_Data *data = elf_getdata (scn, NULL);
3246 [ - + ]: 4 : if (data == NULL)
3247 : 0 : return;
3248 : :
3249 : : /* Get the section header string table index. */
3250 : 4 : size_t shstrndx;
3251 [ - + ]: 4 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
3252 : 0 : error_exit (0, _("cannot get section header string table index"));
3253 : :
3254 : 4 : GElf_Shdr glink_mem;
3255 : 4 : GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
3256 : : &glink_mem);
3257 [ - + ]: 4 : if (glink == NULL)
3258 : 0 : error_exit (0, _("invalid sh_link value in section %zu"),
3259 : : elf_ndxscn (scn));
3260 : :
3261 : 4 : int class = gelf_getclass (ebl->elf);
3262 : 4 : printf (ngettext ("\
3263 : : \nVersion definition section [%2u] '%s' contains %d entry:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'\n",
3264 : : "\
3265 : : \nVersion definition section [%2u] '%s' contains %d entries:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'\n",
3266 : : shdr->sh_info),
3267 : 4 : (unsigned int) elf_ndxscn (scn),
3268 : 4 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
3269 : : shdr->sh_info,
3270 : : class == ELFCLASS32 ? 10 : 18, shdr->sh_addr,
3271 : : shdr->sh_offset,
3272 [ + - ]: 4 : (unsigned int) shdr->sh_link,
3273 : 4 : elf_strptr (ebl->elf, shstrndx, glink->sh_name));
3274 : :
3275 : 4 : unsigned int offset = 0;
3276 [ + - ]: 24 : for (unsigned int cnt = shdr->sh_info; cnt > 0; cnt--)
3277 : : {
3278 : : /* Get the data at the next offset. */
3279 : 24 : GElf_Verdef defmem;
3280 : 24 : GElf_Verdef *def = gelf_getverdef (data, offset, &defmem);
3281 [ + - ]: 24 : if (unlikely (def == NULL))
3282 : : break;
3283 : :
3284 : 24 : unsigned int auxoffset = offset + def->vd_aux;
3285 : 24 : GElf_Verdaux auxmem;
3286 : 24 : GElf_Verdaux *aux = gelf_getverdaux (data, auxoffset, &auxmem);
3287 [ + - ]: 24 : if (unlikely (aux == NULL))
3288 : : break;
3289 : :
3290 : 96 : printf (_("\
3291 : : %#06x: Version: %hd Flags: %s Index: %hd Cnt: %hd Name: %s\n"),
3292 : 24 : offset, def->vd_version,
3293 : 24 : get_ver_flags (def->vd_flags),
3294 : 24 : def->vd_ndx,
3295 : 24 : def->vd_cnt,
3296 : 24 : elf_strptr (ebl->elf, shdr->sh_link, aux->vda_name));
3297 : :
3298 : 24 : auxoffset += aux->vda_next;
3299 [ + + ]: 24 : for (unsigned int cnt2 = 1; cnt2 < def->vd_cnt; ++cnt2)
3300 : : {
3301 : 16 : aux = gelf_getverdaux (data, auxoffset, &auxmem);
3302 [ + - ]: 16 : if (unlikely (aux == NULL))
3303 : : break;
3304 : :
3305 : 16 : printf (_(" %#06x: Parent %d: %s\n"),
3306 : : auxoffset, cnt2,
3307 : 16 : elf_strptr (ebl->elf, shdr->sh_link, aux->vda_name));
3308 : :
3309 [ - + ]: 16 : if (aux->vda_next == 0)
3310 : : break;
3311 : :
3312 : 0 : auxoffset += aux->vda_next;
3313 : : }
3314 : :
3315 : : /* Find the next offset. */
3316 [ + + ]: 24 : if (def->vd_next == 0)
3317 : : break;
3318 : 20 : offset += def->vd_next;
3319 : : }
3320 : : }
3321 : :
3322 : :
3323 : : static void
3324 : 130 : handle_versym (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr)
3325 : : {
3326 : 130 : int class = gelf_getclass (ebl->elf);
3327 : 130 : const char **vername;
3328 : 130 : const char **filename;
3329 : :
3330 : : /* Get the data of the section. */
3331 : 130 : Elf_Data *data = elf_getdata (scn, NULL);
3332 [ + - ]: 130 : if (data == NULL)
3333 : 0 : return;
3334 : :
3335 : : /* Get the section header string table index. */
3336 : 130 : size_t shstrndx;
3337 [ - + ]: 130 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
3338 : 0 : error_exit (0, _("cannot get section header string table index"));
3339 : :
3340 : : /* We have to find the version definition section and extract the
3341 : : version names. */
3342 : : Elf_Scn *defscn = NULL;
3343 : : Elf_Scn *needscn = NULL;
3344 : :
3345 : : Elf_Scn *verscn = NULL;
3346 [ + + ]: 4046 : while ((verscn = elf_nextscn (ebl->elf, verscn)) != NULL)
3347 : : {
3348 : 3916 : GElf_Shdr vershdr_mem;
3349 : 3916 : GElf_Shdr *vershdr = gelf_getshdr (verscn, &vershdr_mem);
3350 : :
3351 [ - + ]: 3916 : if (likely (vershdr != NULL))
3352 : : {
3353 [ + + ]: 3916 : if (vershdr->sh_type == SHT_GNU_verdef)
3354 : : defscn = verscn;
3355 [ + + ]: 3912 : else if (vershdr->sh_type == SHT_GNU_verneed)
3356 : 3916 : needscn = verscn;
3357 : : }
3358 : : }
3359 : :
3360 : 130 : size_t nvername;
3361 [ + - ]: 130 : if (defscn != NULL || needscn != NULL)
3362 : : {
3363 : : /* We have a version information (better should have). Now get
3364 : : the version names. First find the maximum version number. */
3365 : 130 : nvername = 0;
3366 [ + + ]: 130 : if (defscn != NULL)
3367 : : {
3368 : : /* Run through the version definitions and find the highest
3369 : : index. */
3370 : 4 : unsigned int offset = 0;
3371 : 4 : Elf_Data *defdata;
3372 : 4 : GElf_Shdr defshdrmem;
3373 : 4 : GElf_Shdr *defshdr;
3374 : :
3375 : 4 : defdata = elf_getdata (defscn, NULL);
3376 [ + - ]: 4 : if (unlikely (defdata == NULL))
3377 : 0 : return;
3378 : :
3379 : 4 : defshdr = gelf_getshdr (defscn, &defshdrmem);
3380 [ + - ]: 4 : if (unlikely (defshdr == NULL))
3381 : : return;
3382 : :
3383 [ + - ]: 24 : for (unsigned int cnt = 0; cnt < defshdr->sh_info; ++cnt)
3384 : : {
3385 : 24 : GElf_Verdef defmem;
3386 : 24 : GElf_Verdef *def;
3387 : :
3388 : : /* Get the data at the next offset. */
3389 : 24 : def = gelf_getverdef (defdata, offset, &defmem);
3390 [ + - ]: 24 : if (unlikely (def == NULL))
3391 : : break;
3392 : :
3393 : 24 : nvername = MAX (nvername, (size_t) (def->vd_ndx & 0x7fff));
3394 : :
3395 [ + + ]: 24 : if (def->vd_next == 0)
3396 : : break;
3397 : 20 : offset += def->vd_next;
3398 : : }
3399 : : }
3400 [ + - ]: 130 : if (needscn != NULL)
3401 : : {
3402 : 130 : unsigned int offset = 0;
3403 : 130 : Elf_Data *needdata;
3404 : 130 : GElf_Shdr needshdrmem;
3405 : 130 : GElf_Shdr *needshdr;
3406 : :
3407 : 130 : needdata = elf_getdata (needscn, NULL);
3408 [ + - ]: 130 : if (unlikely (needdata == NULL))
3409 : 0 : return;
3410 : :
3411 : 130 : needshdr = gelf_getshdr (needscn, &needshdrmem);
3412 [ + - ]: 130 : if (unlikely (needshdr == NULL))
3413 : : return;
3414 : :
3415 [ + - ]: 184 : for (unsigned int cnt = 0; cnt < needshdr->sh_info; ++cnt)
3416 : : {
3417 : 184 : GElf_Verneed needmem;
3418 : 184 : GElf_Verneed *need;
3419 : 184 : unsigned int auxoffset;
3420 : 184 : int cnt2;
3421 : :
3422 : : /* Get the data at the next offset. */
3423 : 184 : need = gelf_getverneed (needdata, offset, &needmem);
3424 [ + - ]: 184 : if (unlikely (need == NULL))
3425 : : break;
3426 : :
3427 : : /* Run through the auxiliary entries. */
3428 : 184 : auxoffset = offset + need->vn_aux;
3429 [ + - ]: 350 : for (cnt2 = need->vn_cnt; --cnt2 >= 0; )
3430 : : {
3431 : 350 : GElf_Vernaux auxmem;
3432 : 350 : GElf_Vernaux *aux;
3433 : :
3434 : 350 : aux = gelf_getvernaux (needdata, auxoffset, &auxmem);
3435 [ + - ]: 350 : if (unlikely (aux == NULL))
3436 : : break;
3437 : :
3438 : 350 : nvername = MAX (nvername,
3439 : : (size_t) (aux->vna_other & 0x7fff));
3440 : :
3441 [ + + ]: 350 : if (aux->vna_next == 0)
3442 : : break;
3443 : 166 : auxoffset += aux->vna_next;
3444 : : }
3445 : :
3446 [ + + ]: 184 : if (need->vn_next == 0)
3447 : : break;
3448 : 54 : offset += need->vn_next;
3449 : : }
3450 : : }
3451 : :
3452 : : /* This is the number of versions we know about. */
3453 : 130 : ++nvername;
3454 : :
3455 : : /* Allocate the array. */
3456 : 130 : vername = (const char **) alloca (nvername * sizeof (const char *));
3457 [ + + ]: 130 : memset(vername, 0, nvername * sizeof (const char *));
3458 : 130 : filename = (const char **) alloca (nvername * sizeof (const char *));
3459 : 130 : memset(filename, 0, nvername * sizeof (const char *));
3460 : :
3461 : : /* Run through the data structures again and collect the strings. */
3462 [ + + ]: 130 : if (defscn != NULL)
3463 : : {
3464 : : /* Run through the version definitions and find the highest
3465 : : index. */
3466 : 4 : unsigned int offset = 0;
3467 : 4 : Elf_Data *defdata;
3468 : 4 : GElf_Shdr defshdrmem;
3469 : 4 : GElf_Shdr *defshdr;
3470 : :
3471 : 4 : defdata = elf_getdata (defscn, NULL);
3472 [ + - ]: 4 : if (unlikely (defdata == NULL))
3473 : 0 : return;
3474 : :
3475 : 4 : defshdr = gelf_getshdr (defscn, &defshdrmem);
3476 [ + - ]: 4 : if (unlikely (defshdr == NULL))
3477 : : return;
3478 : :
3479 [ + - ]: 24 : for (unsigned int cnt = 0; cnt < defshdr->sh_info; ++cnt)
3480 : : {
3481 : :
3482 : : /* Get the data at the next offset. */
3483 : 24 : GElf_Verdef defmem;
3484 : 24 : GElf_Verdef *def = gelf_getverdef (defdata, offset, &defmem);
3485 [ + - ]: 24 : if (unlikely (def == NULL))
3486 : : break;
3487 : :
3488 : 24 : GElf_Verdaux auxmem;
3489 : 48 : GElf_Verdaux *aux = gelf_getverdaux (defdata,
3490 : 24 : offset + def->vd_aux,
3491 : : &auxmem);
3492 [ + - ]: 24 : if (unlikely (aux == NULL))
3493 : : break;
3494 : :
3495 : 24 : vername[def->vd_ndx & 0x7fff]
3496 : 24 : = elf_strptr (ebl->elf, defshdr->sh_link, aux->vda_name);
3497 : 24 : filename[def->vd_ndx & 0x7fff] = NULL;
3498 : :
3499 [ + + ]: 24 : if (def->vd_next == 0)
3500 : : break;
3501 : 20 : offset += def->vd_next;
3502 : : }
3503 : : }
3504 [ + - ]: 130 : if (needscn != NULL)
3505 : : {
3506 : 130 : unsigned int offset = 0;
3507 : :
3508 : 130 : Elf_Data *needdata = elf_getdata (needscn, NULL);
3509 : 130 : GElf_Shdr needshdrmem;
3510 : 130 : GElf_Shdr *needshdr = gelf_getshdr (needscn, &needshdrmem);
3511 [ - + ]: 130 : if (unlikely (needdata == NULL || needshdr == NULL))
3512 : 0 : return;
3513 : :
3514 [ + - ]: 184 : for (unsigned int cnt = 0; cnt < needshdr->sh_info; ++cnt)
3515 : : {
3516 : : /* Get the data at the next offset. */
3517 : 184 : GElf_Verneed needmem;
3518 : 184 : GElf_Verneed *need = gelf_getverneed (needdata, offset,
3519 : : &needmem);
3520 [ + - ]: 184 : if (unlikely (need == NULL))
3521 : : break;
3522 : :
3523 : : /* Run through the auxiliary entries. */
3524 : 184 : unsigned int auxoffset = offset + need->vn_aux;
3525 [ + - ]: 350 : for (int cnt2 = need->vn_cnt; --cnt2 >= 0; )
3526 : : {
3527 : 350 : GElf_Vernaux auxmem;
3528 : 350 : GElf_Vernaux *aux = gelf_getvernaux (needdata, auxoffset,
3529 : : &auxmem);
3530 [ + - ]: 350 : if (unlikely (aux == NULL))
3531 : : break;
3532 : :
3533 : 350 : vername[aux->vna_other & 0x7fff]
3534 : 350 : = elf_strptr (ebl->elf, needshdr->sh_link, aux->vna_name);
3535 : 350 : filename[aux->vna_other & 0x7fff]
3536 : 350 : = elf_strptr (ebl->elf, needshdr->sh_link, need->vn_file);
3537 : :
3538 [ + + ]: 350 : if (aux->vna_next == 0)
3539 : : break;
3540 : 166 : auxoffset += aux->vna_next;
3541 : : }
3542 : :
3543 [ + + ]: 184 : if (need->vn_next == 0)
3544 : : break;
3545 : 54 : offset += need->vn_next;
3546 : : }
3547 : : }
3548 : : }
3549 : : else
3550 : : {
3551 : : vername = NULL;
3552 : : nvername = 1;
3553 : : filename = NULL;
3554 : : }
3555 : :
3556 : 130 : GElf_Shdr glink_mem;
3557 : 130 : GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
3558 : : &glink_mem);
3559 : 130 : size_t sh_entsize = gelf_fsize (ebl->elf, ELF_T_HALF, 1, EV_CURRENT);
3560 [ - + ]: 130 : if (glink == NULL)
3561 : 0 : error_exit (0, _("invalid sh_link value in section %zu"),
3562 : : elf_ndxscn (scn));
3563 : :
3564 : : /* Print the header. */
3565 : 130 : printf (ngettext ("\
3566 : : \nVersion symbols section [%2u] '%s' contains %d entry:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'",
3567 : : "\
3568 : : \nVersion symbols section [%2u] '%s' contains %d entries:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'",
3569 : : shdr->sh_size / sh_entsize),
3570 : 130 : (unsigned int) elf_ndxscn (scn),
3571 : 130 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
3572 : 130 : (int) (shdr->sh_size / sh_entsize),
3573 : : class == ELFCLASS32 ? 10 : 18, shdr->sh_addr,
3574 : : shdr->sh_offset,
3575 [ + + ]: 130 : (unsigned int) shdr->sh_link,
3576 : 130 : elf_strptr (ebl->elf, shstrndx, glink->sh_name));
3577 : :
3578 : : /* Now we can finally look at the actual contents of this section. */
3579 [ + + ]: 3292 : for (unsigned int cnt = 0; cnt < shdr->sh_size / sh_entsize; ++cnt)
3580 : : {
3581 [ + + ]: 3162 : if (cnt % 2 == 0)
3582 : 1598 : printf ("\n %4d:", cnt);
3583 : :
3584 : 3162 : GElf_Versym symmem;
3585 : 3162 : GElf_Versym *sym = gelf_getversym (data, cnt, &symmem);
3586 [ + - ]: 3162 : if (sym == NULL)
3587 : : break;
3588 : :
3589 [ + + + ]: 3162 : switch (*sym)
3590 : : {
3591 : 238 : ssize_t n;
3592 : 238 : case 0:
3593 : 238 : fputs (_(" 0 *local* "),
3594 : : stdout);
3595 : 238 : break;
3596 : :
3597 : 502 : case 1:
3598 : 502 : fputs (_(" 1 *global* "),
3599 : : stdout);
3600 : 502 : break;
3601 : :
3602 : 2422 : default:
3603 [ + - ]: 4844 : n = printf ("%4d%c%s",
3604 [ + - ]: 2422 : *sym & 0x7fff, *sym & 0x8000 ? 'h' : ' ',
3605 : : (vername != NULL
3606 [ + - ]: 2422 : && (unsigned int) (*sym & 0x7fff) < nvername)
3607 : 2422 : ? vername[*sym & 0x7fff] : "???");
3608 [ + - ]: 2422 : if ((unsigned int) (*sym & 0x7fff) < nvername
3609 [ + - + + ]: 2422 : && filename != NULL && filename[*sym & 0x7fff] != NULL)
3610 : 2118 : n += printf ("(%s)", filename[*sym & 0x7fff]);
3611 : 3162 : printf ("%*s", MAX (0, 33 - (int) n), " ");
3612 : : break;
3613 : : }
3614 : : }
3615 : 130 : putchar ('\n');
3616 : : }
3617 : :
3618 : :
3619 : : static void
3620 : 130 : print_hash_info (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr, size_t shstrndx,
3621 : : uint_fast32_t maxlength, Elf32_Word nbucket,
3622 : : uint_fast32_t nsyms, uint32_t *lengths, const char *extrastr)
3623 : : {
3624 : 130 : uint32_t *counts = xcalloc (maxlength + 1, sizeof (uint32_t));
3625 : :
3626 [ + + ]: 550 : for (Elf32_Word cnt = 0; cnt < nbucket; ++cnt)
3627 : 420 : ++counts[lengths[cnt]];
3628 : :
3629 : 130 : GElf_Shdr glink_mem;
3630 : 130 : GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf,
3631 : 130 : shdr->sh_link),
3632 : : &glink_mem);
3633 [ - + ]: 130 : if (glink == NULL)
3634 : : {
3635 : 0 : error (0, 0, _("invalid sh_link value in section %zu"),
3636 : : elf_ndxscn (scn));
3637 : 0 : free (counts);
3638 : 0 : return;
3639 : : }
3640 : :
3641 [ + + ]: 260 : printf (ngettext ("\
3642 : : \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",
3643 : : "\
3644 : : \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",
3645 : : nbucket),
3646 : 130 : (unsigned int) elf_ndxscn (scn),
3647 : 130 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
3648 : : (int) nbucket,
3649 : 130 : gelf_getclass (ebl->elf) == ELFCLASS32 ? 10 : 18,
3650 : : shdr->sh_addr,
3651 : : shdr->sh_offset,
3652 : 130 : (unsigned int) shdr->sh_link,
3653 : 130 : elf_strptr (ebl->elf, shstrndx, glink->sh_name));
3654 : :
3655 [ + - ]: 130 : if (extrastr != NULL)
3656 : 130 : fputs (extrastr, stdout);
3657 : :
3658 [ + - ]: 130 : if (likely (nbucket > 0))
3659 : : {
3660 : 130 : uint64_t success = 0;
3661 : :
3662 : : /* xgettext:no-c-format */
3663 : 130 : fputs (_("\
3664 : : Length Number % of total Coverage\n"), stdout);
3665 : 130 : printf (_(" 0 %6" PRIu32 " %5.1f%%\n"),
3666 : 130 : counts[0], (counts[0] * 100.0) / nbucket);
3667 : :
3668 : 130 : uint64_t nzero_counts = 0;
3669 [ + + ]: 228 : for (Elf32_Word cnt = 1; cnt <= maxlength; ++cnt)
3670 : : {
3671 : 98 : nzero_counts += counts[cnt] * cnt;
3672 : 98 : printf (_("\
3673 : : %7d %6" PRIu32 " %5.1f%% %5.1f%%\n"),
3674 : 98 : (int) cnt, counts[cnt], (counts[cnt] * 100.0) / nbucket,
3675 : 98 : (nzero_counts * 100.0) / nsyms);
3676 : : }
3677 : :
3678 : : Elf32_Word acc = 0;
3679 [ + + ]: 228 : for (Elf32_Word cnt = 1; cnt <= maxlength; ++cnt)
3680 : : {
3681 : 98 : acc += cnt;
3682 : 98 : success += counts[cnt] * acc;
3683 : : }
3684 : :
3685 [ + + ]: 130 : if (nzero_counts > 0)
3686 : 192 : printf (_("\
3687 : : Average number of tests: successful lookup: %f\n\
3688 : : unsuccessful lookup: %f\n"),
3689 : 62 : (double) success / (double) nzero_counts,
3690 : 62 : (double) nzero_counts / (double) nbucket);
3691 : : }
3692 : :
3693 : 130 : free (counts);
3694 : : }
3695 : :
3696 : :
3697 : : /* This function handles the traditional System V-style hash table format. */
3698 : : static void
3699 : 0 : handle_sysv_hash (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr, size_t shstrndx)
3700 : : {
3701 : 0 : Elf_Data *data = elf_getdata (scn, NULL);
3702 [ # # ]: 0 : if (unlikely (data == NULL))
3703 : : {
3704 : 0 : error (0, 0, _("cannot get data for section %d: %s"),
3705 : 0 : (int) elf_ndxscn (scn), elf_errmsg (-1));
3706 : 0 : return;
3707 : : }
3708 : :
3709 [ # # ]: 0 : if (unlikely (data->d_size < 2 * sizeof (Elf32_Word)))
3710 : : {
3711 : 0 : invalid_data:
3712 : 0 : error (0, 0, _("invalid data in sysv.hash section %d"),
3713 : 0 : (int) elf_ndxscn (scn));
3714 : 0 : return;
3715 : : }
3716 : :
3717 : 0 : Elf32_Word nbucket = ((Elf32_Word *) data->d_buf)[0];
3718 : 0 : Elf32_Word nchain = ((Elf32_Word *) data->d_buf)[1];
3719 : :
3720 : 0 : uint64_t used_buf = (2ULL + nchain + nbucket) * sizeof (Elf32_Word);
3721 [ # # ]: 0 : if (used_buf > data->d_size)
3722 : 0 : goto invalid_data;
3723 : :
3724 : 0 : Elf32_Word *bucket = &((Elf32_Word *) data->d_buf)[2];
3725 : 0 : Elf32_Word *chain = &((Elf32_Word *) data->d_buf)[2 + nbucket];
3726 : :
3727 : 0 : uint32_t *lengths = xcalloc (nbucket, sizeof (uint32_t));
3728 : :
3729 : 0 : uint_fast32_t maxlength = 0;
3730 : 0 : uint_fast32_t nsyms = 0;
3731 [ # # ]: 0 : for (Elf32_Word cnt = 0; cnt < nbucket; ++cnt)
3732 : : {
3733 : 0 : Elf32_Word inner = bucket[cnt];
3734 : 0 : Elf32_Word chain_len = 0;
3735 [ # # ]: 0 : while (inner > 0 && inner < nchain)
3736 : : {
3737 : 0 : ++nsyms;
3738 : 0 : ++chain_len;
3739 [ # # ]: 0 : if (chain_len > nchain)
3740 : : {
3741 : 0 : error (0, 0, _("invalid chain in sysv.hash section %d"),
3742 : 0 : (int) elf_ndxscn (scn));
3743 : 0 : free (lengths);
3744 : 0 : return;
3745 : : }
3746 [ # # ]: 0 : if (maxlength < ++lengths[cnt])
3747 : 0 : ++maxlength;
3748 : :
3749 : 0 : inner = chain[inner];
3750 : : }
3751 : : }
3752 : :
3753 : 0 : print_hash_info (ebl, scn, shdr, shstrndx, maxlength, nbucket, nsyms,
3754 : : lengths, NULL);
3755 : :
3756 : 0 : free (lengths);
3757 : : }
3758 : :
3759 : :
3760 : : /* This function handles the incorrect, System V-style hash table
3761 : : format some 64-bit architectures use. */
3762 : : static void
3763 : 0 : handle_sysv_hash64 (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr, size_t shstrndx)
3764 : : {
3765 : 0 : Elf_Data *data = elf_getdata (scn, NULL);
3766 [ # # ]: 0 : if (unlikely (data == NULL))
3767 : : {
3768 : 0 : error (0, 0, _("cannot get data for section %d: %s"),
3769 : 0 : (int) elf_ndxscn (scn), elf_errmsg (-1));
3770 : 0 : return;
3771 : : }
3772 : :
3773 [ # # ]: 0 : if (unlikely (data->d_size < 2 * sizeof (Elf64_Xword)))
3774 : : {
3775 : 0 : invalid_data:
3776 : 0 : error (0, 0, _("invalid data in sysv.hash64 section %d"),
3777 : 0 : (int) elf_ndxscn (scn));
3778 : 0 : return;
3779 : : }
3780 : :
3781 : 0 : Elf64_Xword nbucket = ((Elf64_Xword *) data->d_buf)[0];
3782 : 0 : Elf64_Xword nchain = ((Elf64_Xword *) data->d_buf)[1];
3783 : :
3784 : 0 : uint64_t maxwords = data->d_size / sizeof (Elf64_Xword);
3785 [ # # ]: 0 : if (maxwords < 2
3786 [ # # ]: 0 : || maxwords - 2 < nbucket
3787 [ # # ]: 0 : || maxwords - 2 - nbucket < nchain)
3788 : 0 : goto invalid_data;
3789 : :
3790 : 0 : Elf64_Xword *bucket = &((Elf64_Xword *) data->d_buf)[2];
3791 : 0 : Elf64_Xword *chain = &((Elf64_Xword *) data->d_buf)[2 + nbucket];
3792 : :
3793 : 0 : uint32_t *lengths = xcalloc (nbucket, sizeof (uint32_t));
3794 : :
3795 : 0 : uint_fast32_t maxlength = 0;
3796 : 0 : uint_fast32_t nsyms = 0;
3797 [ # # ]: 0 : for (Elf64_Xword cnt = 0; cnt < nbucket; ++cnt)
3798 : : {
3799 : 0 : Elf64_Xword inner = bucket[cnt];
3800 : 0 : Elf64_Xword chain_len = 0;
3801 [ # # ]: 0 : while (inner > 0 && inner < nchain)
3802 : : {
3803 : 0 : ++nsyms;
3804 : 0 : ++chain_len;
3805 [ # # ]: 0 : if (chain_len > nchain)
3806 : : {
3807 : 0 : error (0, 0, _("invalid chain in sysv.hash64 section %d"),
3808 : 0 : (int) elf_ndxscn (scn));
3809 : 0 : free (lengths);
3810 : 0 : return;
3811 : : }
3812 [ # # ]: 0 : if (maxlength < ++lengths[cnt])
3813 : 0 : ++maxlength;
3814 : :
3815 : 0 : inner = chain[inner];
3816 : : }
3817 : : }
3818 : :
3819 : 0 : print_hash_info (ebl, scn, shdr, shstrndx, maxlength, nbucket, nsyms,
3820 : : lengths, NULL);
3821 : :
3822 : 0 : free (lengths);
3823 : : }
3824 : :
3825 : :
3826 : : /* This function handles the GNU-style hash table format. */
3827 : : static void
3828 : 130 : handle_gnu_hash (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr, size_t shstrndx)
3829 : : {
3830 : 130 : uint32_t *lengths = NULL;
3831 : 130 : Elf_Data *data = elf_getdata (scn, NULL);
3832 [ - + ]: 130 : if (unlikely (data == NULL))
3833 : : {
3834 : 0 : error (0, 0, _("cannot get data for section %d: %s"),
3835 : 0 : (int) elf_ndxscn (scn), elf_errmsg (-1));
3836 : 0 : return;
3837 : : }
3838 : :
3839 [ - + ]: 130 : if (unlikely (data->d_size < 4 * sizeof (Elf32_Word)))
3840 : : {
3841 : 0 : invalid_data:
3842 : 0 : free (lengths);
3843 : 0 : error (0, 0, _("invalid data in gnu.hash section %d"),
3844 : 0 : (int) elf_ndxscn (scn));
3845 : 0 : return;
3846 : : }
3847 : :
3848 : 130 : Elf32_Word nbucket = ((Elf32_Word *) data->d_buf)[0];
3849 : 130 : Elf32_Word symbias = ((Elf32_Word *) data->d_buf)[1];
3850 : :
3851 : : /* Next comes the size of the bitmap. It's measured in words for
3852 : : the architecture. It's 32 bits for 32 bit archs, and 64 bits for
3853 : : 64 bit archs. There is always a bloom filter present, so zero is
3854 : : an invalid value. */
3855 : 130 : Elf32_Word bitmask_words = ((Elf32_Word *) data->d_buf)[2];
3856 [ + + ]: 130 : if (gelf_getclass (ebl->elf) == ELFCLASS64)
3857 : 114 : bitmask_words *= 2;
3858 : :
3859 [ - + ]: 130 : if (bitmask_words == 0)
3860 : 0 : goto invalid_data;
3861 : :
3862 : 130 : Elf32_Word shift = ((Elf32_Word *) data->d_buf)[3];
3863 : :
3864 : : /* Is there still room for the sym chain?
3865 : : Use uint64_t calculation to prevent 32bit overflow. */
3866 : 130 : uint64_t used_buf = (4ULL + bitmask_words + nbucket) * sizeof (Elf32_Word);
3867 : 130 : uint32_t max_nsyms = (data->d_size - used_buf) / sizeof (Elf32_Word);
3868 [ - + ]: 130 : if (used_buf > data->d_size)
3869 : 0 : goto invalid_data;
3870 : :
3871 : 130 : lengths = xcalloc (nbucket, sizeof (uint32_t));
3872 : :
3873 : 130 : Elf32_Word *bitmask = &((Elf32_Word *) data->d_buf)[4];
3874 : 130 : Elf32_Word *bucket = &((Elf32_Word *) data->d_buf)[4 + bitmask_words];
3875 : 130 : Elf32_Word *chain = &((Elf32_Word *) data->d_buf)[4 + bitmask_words
3876 : 130 : + nbucket];
3877 : :
3878 : : /* Compute distribution of chain lengths. */
3879 : 130 : uint_fast32_t maxlength = 0;
3880 : 130 : uint_fast32_t nsyms = 0;
3881 [ + + ]: 550 : for (Elf32_Word cnt = 0; cnt < nbucket; ++cnt)
3882 [ + + ]: 420 : if (bucket[cnt] != 0)
3883 : : {
3884 : 232 : Elf32_Word inner = bucket[cnt] - symbias;
3885 : 414 : do
3886 : : {
3887 : 414 : ++nsyms;
3888 [ + + ]: 414 : if (maxlength < ++lengths[cnt])
3889 : 98 : ++maxlength;
3890 [ - + ]: 414 : if (inner >= max_nsyms)
3891 : 0 : goto invalid_data;
3892 : : }
3893 [ + + ]: 414 : while ((chain[inner++] & 1) == 0);
3894 : : }
3895 : :
3896 : : /* Count bits in bitmask. */
3897 : : uint_fast32_t nbits = 0;
3898 [ + + ]: 446 : for (Elf32_Word cnt = 0; cnt < bitmask_words; ++cnt)
3899 : : {
3900 : 316 : uint_fast32_t word = bitmask[cnt];
3901 : :
3902 : 316 : word = (word & 0x55555555) + ((word >> 1) & 0x55555555);
3903 : 316 : word = (word & 0x33333333) + ((word >> 2) & 0x33333333);
3904 : 316 : word = (word & 0x0f0f0f0f) + ((word >> 4) & 0x0f0f0f0f);
3905 : 316 : word = (word & 0x00ff00ff) + ((word >> 8) & 0x00ff00ff);
3906 : 316 : nbits += (word & 0x0000ffff) + ((word >> 16) & 0x0000ffff);
3907 : : }
3908 : :
3909 : 260 : char *str = xasprintf (_("\
3910 : : Symbol Bias: %u\n\
3911 : : Bitmask Size: %zu bytes %" PRIuFAST32 "%% bits set 2nd hash shift: %u\n"),
3912 : : (unsigned int) symbias,
3913 : : bitmask_words * sizeof (Elf32_Word),
3914 : 130 : ((nbits * 100 + 50)
3915 : 130 : / (uint_fast32_t) (bitmask_words
3916 : : * sizeof (Elf32_Word) * 8)),
3917 : : (unsigned int) shift);
3918 : :
3919 : 130 : print_hash_info (ebl, scn, shdr, shstrndx, maxlength, nbucket, nsyms,
3920 : : lengths, str);
3921 : :
3922 : 130 : free (str);
3923 : 130 : free (lengths);
3924 : : }
3925 : :
3926 : :
3927 : : /* Find the symbol table(s). For this we have to search through the
3928 : : section table. */
3929 : : static void
3930 : 184 : handle_hash (Ebl *ebl)
3931 : : {
3932 : : /* Get the section header string table index. */
3933 : 184 : size_t shstrndx;
3934 [ - + ]: 184 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
3935 : 0 : error_exit (0, _("cannot get section header string table index"));
3936 : :
3937 : : Elf_Scn *scn = NULL;
3938 [ + + ]: 6004 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
3939 : : {
3940 : : /* Handle the section if it is a symbol table. */
3941 : 5820 : GElf_Shdr shdr_mem;
3942 : 5820 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
3943 : :
3944 [ + - ]: 5820 : if (likely (shdr != NULL))
3945 : : {
3946 [ + + ]: 5820 : if ((shdr->sh_type == SHT_HASH || shdr->sh_type == SHT_GNU_HASH)
3947 [ - + ]: 130 : && (shdr->sh_flags & SHF_COMPRESSED) != 0)
3948 : : {
3949 [ # # ]: 0 : if (elf_compress (scn, 0, 0) < 0)
3950 : 0 : printf ("WARNING: %s [%zd]\n",
3951 : : _("Couldn't uncompress section"),
3952 : : elf_ndxscn (scn));
3953 : 0 : shdr = gelf_getshdr (scn, &shdr_mem);
3954 [ # # ]: 0 : if (unlikely (shdr == NULL))
3955 : 0 : error_exit (0, _("cannot get section [%zd] header: %s"),
3956 : : elf_ndxscn (scn), elf_errmsg (-1));
3957 : : }
3958 : :
3959 [ - + ]: 5820 : if (shdr->sh_type == SHT_HASH)
3960 : : {
3961 [ # # ]: 0 : if (ebl_sysvhash_entrysize (ebl) == sizeof (Elf64_Xword))
3962 : 0 : handle_sysv_hash64 (ebl, scn, shdr, shstrndx);
3963 : : else
3964 : 0 : handle_sysv_hash (ebl, scn, shdr, shstrndx);
3965 : : }
3966 [ + + ]: 5820 : else if (shdr->sh_type == SHT_GNU_HASH)
3967 : 130 : handle_gnu_hash (ebl, scn, shdr, shstrndx);
3968 : : }
3969 : : }
3970 : 184 : }
3971 : :
3972 : :
3973 : : static void
3974 : 192 : print_liblist (Ebl *ebl)
3975 : : {
3976 : : /* Find the library list sections. For this we have to search
3977 : : through the section table. */
3978 : 192 : Elf_Scn *scn = NULL;
3979 : :
3980 : : /* Get the section header string table index. */
3981 : 192 : size_t shstrndx;
3982 [ - + ]: 192 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
3983 : 0 : error_exit (0, _("cannot get section header string table index"));
3984 : :
3985 [ + + ]: 6130 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
3986 : : {
3987 : 5938 : GElf_Shdr shdr_mem;
3988 : 5938 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
3989 : :
3990 [ + - - + ]: 5938 : if (shdr != NULL && shdr->sh_type == SHT_GNU_LIBLIST)
3991 : : {
3992 : 0 : size_t sh_entsize = gelf_fsize (ebl->elf, ELF_T_LIB, 1, EV_CURRENT);
3993 : 0 : int nentries = shdr->sh_size / sh_entsize;
3994 : 0 : printf (ngettext ("\
3995 : : \nLibrary list section [%2zu] '%s' at offset %#0" PRIx64 " contains %d entry:\n",
3996 : : "\
3997 : : \nLibrary list section [%2zu] '%s' at offset %#0" PRIx64 " contains %d entries:\n",
3998 : : nentries),
3999 : : elf_ndxscn (scn),
4000 : 0 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
4001 : : shdr->sh_offset,
4002 : : nentries);
4003 : :
4004 : 0 : Elf_Data *data = elf_getdata (scn, NULL);
4005 [ # # ]: 0 : if (data == NULL)
4006 : 0 : return;
4007 : :
4008 : 0 : puts (_("\
4009 : : Library Time Stamp Checksum Version Flags"));
4010 : :
4011 [ # # ]: 0 : for (int cnt = 0; cnt < nentries; ++cnt)
4012 : : {
4013 : 0 : GElf_Lib lib_mem;
4014 : 0 : GElf_Lib *lib = gelf_getlib (data, cnt, &lib_mem);
4015 [ # # ]: 0 : if (unlikely (lib == NULL))
4016 : 0 : continue;
4017 : :
4018 : 0 : time_t t = (time_t) lib->l_time_stamp;
4019 : 0 : struct tm *tm = gmtime (&t);
4020 [ # # ]: 0 : if (unlikely (tm == NULL))
4021 : 0 : continue;
4022 : :
4023 : 0 : printf (" [%2d] %-29s %04u-%02u-%02uT%02u:%02u:%02u %08x %-7u %u\n",
4024 : 0 : cnt, elf_strptr (ebl->elf, shdr->sh_link, lib->l_name),
4025 : 0 : tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
4026 : : tm->tm_hour, tm->tm_min, tm->tm_sec,
4027 : 0 : (unsigned int) lib->l_checksum,
4028 : 0 : (unsigned int) lib->l_version,
4029 : 0 : (unsigned int) lib->l_flags);
4030 : : }
4031 : : }
4032 : : }
4033 : : }
4034 : :
4035 : : static inline size_t
4036 : 16 : left (Elf_Data *data,
4037 : : const unsigned char *p)
4038 : : {
4039 : 16 : return (const unsigned char *) data->d_buf + data->d_size - p;
4040 : : }
4041 : :
4042 : : static void
4043 : 192 : print_attributes (Ebl *ebl, const GElf_Ehdr *ehdr)
4044 : : {
4045 : : /* Find the object attributes sections. For this we have to search
4046 : : through the section table. */
4047 : 192 : Elf_Scn *scn = NULL;
4048 : :
4049 : : /* Get the section header string table index. */
4050 : 192 : size_t shstrndx;
4051 [ - + ]: 192 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
4052 : 0 : error_exit (0, _("cannot get section header string table index"));
4053 : :
4054 [ + + ]: 6130 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
4055 : : {
4056 : 5938 : GElf_Shdr shdr_mem;
4057 : 5938 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
4058 : :
4059 [ + - + + ]: 5938 : if (shdr == NULL || (shdr->sh_type != SHT_GNU_ATTRIBUTES
4060 [ + + ]: 5932 : && (shdr->sh_type != SHT_ARM_ATTRIBUTES
4061 [ - + ]: 2 : || ehdr->e_machine != EM_ARM)
4062 [ + + ]: 5930 : && (shdr->sh_type != SHT_CSKY_ATTRIBUTES
4063 [ + - ]: 2 : || ehdr->e_machine != EM_CSKY)
4064 [ - + ]: 5930 : && (shdr->sh_type != SHT_RISCV_ATTRIBUTES
4065 [ # # ]: 0 : || ehdr->e_machine != EM_RISCV)))
4066 : 5930 : continue;
4067 : :
4068 : 8 : printf (_("\
4069 : : \nObject attributes section [%2zu] '%s' of %" PRIu64
4070 : : " bytes at offset %#0" PRIx64 ":\n"),
4071 : : elf_ndxscn (scn),
4072 : 8 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
4073 : : shdr->sh_size, shdr->sh_offset);
4074 : :
4075 : 8 : Elf_Data *data = elf_rawdata (scn, NULL);
4076 [ + - + - ]: 8 : if (unlikely (data == NULL || data->d_size == 0))
4077 : 0 : return;
4078 : :
4079 : 8 : const unsigned char *p = data->d_buf;
4080 : :
4081 : : /* There is only one 'version', A. */
4082 [ + - ]: 8 : if (unlikely (*p++ != 'A'))
4083 : : return;
4084 : :
4085 : 8 : fputs (_(" Owner Size\n"), stdout);
4086 : :
4087 : : /* Loop over the sections. */
4088 [ + + ]: 16 : while (left (data, p) >= 4)
4089 : : {
4090 : : /* Section length. */
4091 : 8 : uint32_t len;
4092 [ + + ]: 8 : memcpy (&len, p, sizeof len);
4093 : :
4094 [ + + ]: 8 : if (MY_ELFDATA != ehdr->e_ident[EI_DATA])
4095 : 4 : CONVERT (len);
4096 : :
4097 [ + - ]: 8 : if (unlikely (len > left (data, p)))
4098 : : break;
4099 : :
4100 : : /* Section vendor name. */
4101 : 8 : const unsigned char *name = p + sizeof len;
4102 : 8 : p += len;
4103 : :
4104 : 8 : unsigned const char *q = memchr (name, '\0', len);
4105 [ + - ]: 8 : if (unlikely (q == NULL))
4106 : : break;
4107 : 8 : ++q;
4108 : :
4109 : 8 : printf (_(" %-13s %4" PRIu32 "\n"), name, len);
4110 : :
4111 : 16 : bool gnu_vendor = (q - name == sizeof "gnu"
4112 [ + + - + ]: 8 : && !memcmp (name, "gnu", sizeof "gnu"));
4113 : :
4114 : : /* Loop over subsections. */
4115 [ + + ]: 8 : if (shdr->sh_type != SHT_GNU_ATTRIBUTES
4116 [ + - ]: 6 : || gnu_vendor)
4117 [ + + ]: 16 : while (q < p)
4118 : : {
4119 : 8 : const unsigned char *const sub = q;
4120 : :
4121 : 8 : unsigned int subsection_tag;
4122 : 8 : get_uleb128 (subsection_tag, q, p);
4123 [ + - ]: 8 : if (unlikely (q >= p))
4124 : : break;
4125 : :
4126 : 8 : uint32_t subsection_len;
4127 [ + - ]: 8 : if (unlikely (p - sub < (ptrdiff_t) sizeof subsection_len))
4128 : : break;
4129 : :
4130 [ + + ]: 8 : memcpy (&subsection_len, q, sizeof subsection_len);
4131 : :
4132 [ + + ]: 8 : if (MY_ELFDATA != ehdr->e_ident[EI_DATA])
4133 : 4 : CONVERT (subsection_len);
4134 : :
4135 : : /* Don't overflow, ptrdiff_t might be 32bits, but signed. */
4136 [ + - + - ]: 8 : if (unlikely (subsection_len == 0
4137 : : || subsection_len >= (uint32_t) PTRDIFF_MAX
4138 : : || p - sub < (ptrdiff_t) subsection_len))
4139 : : break;
4140 : :
4141 : 8 : const unsigned char *r = q + sizeof subsection_len;
4142 : 8 : q = sub + subsection_len;
4143 : :
4144 [ - + ]: 8 : switch (subsection_tag)
4145 : : {
4146 : 0 : default:
4147 : : /* Unknown subsection, print and skip. */
4148 : 8 : printf (_(" %-4u %12" PRIu32 "\n"),
4149 : : subsection_tag, subsection_len);
4150 : : break;
4151 : :
4152 : 8 : case 1: /* Tag_File */
4153 : 8 : printf (_(" File: %11" PRIu32 "\n"),
4154 : : subsection_len);
4155 : :
4156 [ + + ]: 52 : while (r < q)
4157 : : {
4158 : 44 : unsigned int tag;
4159 : 44 : get_uleb128 (tag, r, q);
4160 [ + - ]: 44 : if (unlikely (r >= q))
4161 : : break;
4162 : :
4163 : : /* GNU style tags have either a uleb128 value,
4164 : : when lowest bit is not set, or a string
4165 : : when the lowest bit is set.
4166 : : "compatibility" (32) is special. It has
4167 : : both a string and a uleb128 value. For
4168 : : non-gnu we assume 6 till 31 only take ints.
4169 : : XXX see arm backend, do we need a separate
4170 : : hook? */
4171 : 44 : uint64_t value = 0;
4172 : 44 : const char *string = NULL;
4173 [ + - + + ]: 44 : if (tag == 32 || (tag & 1) == 0
4174 [ + - + + ]: 16 : || (! gnu_vendor && (tag > 5 && tag < 32)))
4175 : : {
4176 : : // Note r >= q check above.
4177 : 42 : get_uleb128 (value, r, q);
4178 [ + - ]: 42 : if (r > q)
4179 : : break;
4180 : : }
4181 [ + - ]: 44 : if (tag == 32
4182 [ + + ]: 44 : || ((tag & 1) != 0
4183 [ + - ]: 16 : && (gnu_vendor
4184 [ + - ]: 16 : || (! gnu_vendor && tag > 32)))
4185 [ + + + + ]: 44 : || (! gnu_vendor && tag > 3 && tag < 6))
4186 : : {
4187 : 2 : string = (const char *) r;
4188 : 2 : r = memchr (r, '\0', q - r);
4189 [ + - ]: 2 : if (r == NULL)
4190 : : break;
4191 : 2 : ++r;
4192 : : }
4193 : :
4194 : 44 : const char *tag_name = NULL;
4195 : 44 : const char *value_name = NULL;
4196 : 44 : ebl_check_object_attribute (ebl, (const char *) name,
4197 : : tag, value,
4198 : : &tag_name, &value_name);
4199 : :
4200 [ + - ]: 44 : if (tag_name != NULL)
4201 : : {
4202 [ - + ]: 44 : if (tag == 32)
4203 : 0 : printf (_(" %s: %" PRId64 ", %s\n"),
4204 : : tag_name, value, string);
4205 [ + + + + ]: 44 : else if (string == NULL && value_name == NULL)
4206 : 2 : printf (_(" %s: %" PRId64 "\n"),
4207 : : tag_name, value);
4208 : : else
4209 : 42 : printf (_(" %s: %s\n"),
4210 : : tag_name, string ?: value_name);
4211 : : }
4212 : : else
4213 : : {
4214 : : /* For "gnu" vendor 32 "compatibility" has
4215 : : already been handled above. */
4216 [ # # # # ]: 0 : assert (tag != 32
4217 : : || strcmp ((const char *) name, "gnu"));
4218 [ # # ]: 0 : if (string == NULL)
4219 : 0 : printf (_(" %u: %" PRId64 "\n"),
4220 : : tag, value);
4221 : : else
4222 : 44 : printf (_(" %u: %s\n"),
4223 : : tag, string);
4224 : : }
4225 : : }
4226 : : }
4227 : : }
4228 : : }
4229 : : }
4230 : : }
4231 : :
4232 : : /* Returns either the (relocated) data from the Dwarf, or tries to get
4233 : : the "raw" (uncompressed) data from the Elf section. Produces a
4234 : : warning if the data cannot be found (or decompressed). */
4235 : : static Elf_Data *
4236 : 810 : get_debug_elf_data (Dwarf *dbg, Ebl *ebl, int idx, Elf_Scn *scn)
4237 : : {
4238 : : /* We prefer to get the section data from the Dwarf because that
4239 : : might have been relocated already. Note this is subtly wrong if
4240 : : there are multiple sections with the same .debug name. */
4241 [ - + ]: 810 : if (dbg->sectiondata[idx] != NULL)
4242 : : return dbg->sectiondata[idx];
4243 : :
4244 : 0 : GElf_Shdr shdr_mem;
4245 : 0 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
4246 [ # # # # ]: 0 : if (shdr != NULL && (shdr->sh_flags & SHF_COMPRESSED) != 0)
4247 : : {
4248 [ # # ]: 0 : if (elf_compress (scn, 0, 0) < 0)
4249 : : {
4250 : 0 : error (0, 0, "%s [%zd] '%s'\n",
4251 : : _("Couldn't uncompress section"),
4252 : : elf_ndxscn (scn), section_name (ebl, shdr));
4253 : 0 : return NULL;
4254 : : }
4255 : : }
4256 : :
4257 : 0 : Elf_Data *data = elf_getdata (scn, NULL);
4258 [ # # ]: 0 : if (data == NULL)
4259 : 0 : error (0, 0, "%s [%zd] '%s': %s\n",
4260 : : _("Couldn't get data from section"),
4261 : : elf_ndxscn (scn), section_name (ebl, shdr), elf_errmsg (-1));
4262 : :
4263 : 0 : return elf_getdata (scn, NULL);
4264 : : }
4265 : :
4266 : : static void
4267 : 2172634 : print_dwarf_addr (Dwfl_Module *dwflmod,
4268 : : int address_size, Dwarf_Addr address, Dwarf_Addr raw)
4269 : : {
4270 : : /* See if there is a name we can give for this address. */
4271 : 2172634 : GElf_Sym sym;
4272 : 2172634 : GElf_Off off = 0;
4273 [ + + ]: 13076 : const char *name = (print_address_names && ! print_unresolved_addresses)
4274 : 12912 : ? dwfl_module_addrinfo (dwflmod, address, &off, &sym, NULL, NULL, NULL)
4275 [ + + ]: 2172634 : : NULL;
4276 : :
4277 : 2172634 : const char *scn;
4278 [ + + ]: 2172634 : if (print_unresolved_addresses)
4279 : : {
4280 : 168 : address = raw;
4281 : 168 : scn = NULL;
4282 : : }
4283 : : else
4284 : : {
4285 : : /* Relativize the address. */
4286 : 2172466 : int n = dwfl_module_relocations (dwflmod);
4287 [ + + ]: 2172466 : int i = n < 1 ? -1 : dwfl_module_relocate_address (dwflmod, &address);
4288 : :
4289 : : /* In an ET_REL file there is a section name to refer to. */
4290 : 2169714 : scn = (i < 0 ? NULL
4291 [ - + ]: 2169714 : : dwfl_module_relocation_info (dwflmod, i, NULL));
4292 : : }
4293 : :
4294 [ - + ]: 2172634 : if ((name != NULL
4295 : 12504 : ? (off != 0
4296 : : ? (scn != NULL
4297 : : ? (address_size == 0
4298 : 2 : ? printf ("%s+%#" PRIx64 " <%s+%#" PRIx64 ">",
4299 : : scn, address, name, off)
4300 : 8 : : printf ("%s+%#0*" PRIx64 " <%s+%#" PRIx64 ">",
4301 : 4 : scn, 2 + address_size * 2, address,
4302 : : name, off))
4303 : : : (address_size == 0
4304 : 364 : ? printf ("%#" PRIx64 " <%s+%#" PRIx64 ">",
4305 : : address, name, off)
4306 : 2156 : : printf ("%#0*" PRIx64 " <%s+%#" PRIx64 ">",
4307 : 1078 : 2 + address_size * 2, address,
4308 : : name, off)))
4309 : : : (scn != NULL
4310 : : ? (address_size == 0
4311 : 2 : ? printf ("%s+%#" PRIx64 " <%s>", scn, address, name)
4312 : 20108 : : printf ("%s+%#0*" PRIx64 " <%s>",
4313 : 10054 : scn, 2 + address_size * 2, address, name))
4314 : : : (address_size == 0
4315 : 160 : ? printf ("%#" PRIx64 " <%s>", address, name)
4316 : 1680 : : printf ("%#0*" PRIx64 " <%s>",
4317 : 840 : 2 + address_size * 2, address, name))))
4318 : : : (scn != NULL
4319 : : ? (address_size == 0
4320 : 788838 : ? printf ("%s+%#" PRIx64, scn, address)
4321 : 1370814 : : printf ("%s+%#0*" PRIx64, scn, 2 + address_size * 2, address))
4322 : : : (address_size == 0
4323 : 70 : ? printf ("%#" PRIx64, address)
4324 [ + + + + : 4344860 : : printf ("%#0*" PRIx64, 2 + address_size * 2, address)))) < 0)
+ + + + +
+ + + + +
+ + + + +
+ + + ]
4325 : 0 : error_exit (0, _("sprintf failure"));
4326 : 2172634 : }
4327 : :
4328 : :
4329 : : static const char *
4330 : 2243838 : dwarf_tag_string (unsigned int tag)
4331 : : {
4332 [ - + + - : 2243838 : switch (tag)
- - - - +
+ + + + -
- - - - -
+ - + - +
- - + + -
+ - - - -
- - - + -
+ - + + +
- - - - -
- + - - +
- - - + -
+ + + + -
- - - - +
+ + + - +
+ + + - -
- ]
4333 : : {
4334 : : #define DWARF_ONE_KNOWN_DW_TAG(NAME, CODE) case CODE: return #NAME;
4335 : 2243838 : DWARF_ALL_KNOWN_DW_TAG
4336 : : #undef DWARF_ONE_KNOWN_DW_TAG
4337 : 0 : default:
4338 : 0 : return NULL;
4339 : : }
4340 : : }
4341 : :
4342 : :
4343 : : static const char *
4344 : 8684718 : dwarf_attr_string (unsigned int attrnum)
4345 : : {
4346 [ + - + - : 8684718 : switch (attrnum)
- - - + -
- + + + -
- - - + +
- + - - +
- + - + -
- - - - -
- - - - -
- - - - -
- - - + -
+ - + - +
- - - + +
- - - + -
+ - + + -
- + + + +
- + + + -
+ - - + -
+ - + + -
+ - - + +
+ + - - -
- - + + +
+ - + - +
- - - - +
+ - + - +
+ - + + +
+ + + + -
- + - - -
+ - + - -
- - + + -
+ - - - -
+ - - - +
+ - + - -
- - + + -
- - - - +
+ - - - -
- - - + ]
4347 : : {
4348 : : #define DWARF_ONE_KNOWN_DW_AT(NAME, CODE) case CODE: return #NAME;
4349 : 8684716 : DWARF_ALL_KNOWN_DW_AT
4350 : : #undef DWARF_ONE_KNOWN_DW_AT
4351 : 0 : default:
4352 : 0 : return NULL;
4353 : : }
4354 : : }
4355 : :
4356 : :
4357 : : static const char *
4358 : 8694956 : dwarf_form_string (unsigned int form)
4359 : : {
4360 [ + + + + : 8694956 : switch (form)
+ - - - -
- + - - +
+ + + + +
+ + + - +
+ - - + -
- + - - +
+ + + + +
- + + - -
- + - + ]
4361 : : {
4362 : : #define DWARF_ONE_KNOWN_DW_FORM(NAME, CODE) case CODE: return #NAME;
4363 : 8694952 : DWARF_ALL_KNOWN_DW_FORM
4364 : : #undef DWARF_ONE_KNOWN_DW_FORM
4365 : 0 : default:
4366 : 0 : return NULL;
4367 : : }
4368 : : }
4369 : :
4370 : :
4371 : : static const char *
4372 : 3680 : dwarf_lang_string (unsigned int lang)
4373 : : {
4374 [ - - + - : 3680 : switch (lang)
- - - + -
- + + - +
- - + - -
- - - - -
- - - + -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - +
- ]
4375 : : {
4376 : : #define DWARF_ONE_KNOWN_DW_LANG(NAME, CODE) case CODE: return #NAME;
4377 : 3678 : DWARF_ALL_KNOWN_DW_LANG
4378 : : #undef DWARF_ONE_KNOWN_DW_LANG
4379 : 2 : default:
4380 : 2 : return NULL;
4381 : : }
4382 : : }
4383 : :
4384 : :
4385 : : static const char *
4386 : 4 : dwarf_lname_string (unsigned int lname)
4387 : : {
4388 [ - - - + : 4 : switch (lname)
- + - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - ]
4389 : : {
4390 : : #define DWARF_ONE_KNOWN_DW_LNAME(NAME, CODE) case CODE: return #NAME;
4391 : 4 : DWARF_ALL_KNOWN_DW_LNAME
4392 : : #undef DWARF_ONE_KNOWN_DW_LNAME
4393 : 0 : default:
4394 : 0 : return NULL;
4395 : : }
4396 : : }
4397 : :
4398 : :
4399 : : static const char *
4400 : 7138 : dwarf_inline_string (unsigned int code)
4401 : : {
4402 : 7138 : static const char *const known[] =
4403 : : {
4404 : : #define DWARF_ONE_KNOWN_DW_INL(NAME, CODE) [CODE] = #NAME,
4405 : : DWARF_ALL_KNOWN_DW_INL
4406 : : #undef DWARF_ONE_KNOWN_DW_INL
4407 : : };
4408 : :
4409 : 7138 : if (likely (code < sizeof (known) / sizeof (known[0])))
4410 : 7138 : return known[code];
4411 : :
4412 : : return NULL;
4413 : : }
4414 : :
4415 : :
4416 : : static const char *
4417 : 56482 : dwarf_encoding_string (unsigned int code)
4418 : : {
4419 : 56482 : static const char *const known[] =
4420 : : {
4421 : : #define DWARF_ONE_KNOWN_DW_ATE(NAME, CODE) [CODE] = #NAME,
4422 : : DWARF_ALL_KNOWN_DW_ATE
4423 : : #undef DWARF_ONE_KNOWN_DW_ATE
4424 : : };
4425 : :
4426 : 56482 : if (likely (code < sizeof (known) / sizeof (known[0])))
4427 : 56482 : return known[code];
4428 : :
4429 : : return NULL;
4430 : : }
4431 : :
4432 : :
4433 : : static const char *
4434 : 0 : dwarf_access_string (unsigned int code)
4435 : : {
4436 : 0 : static const char *const known[] =
4437 : : {
4438 : : #define DWARF_ONE_KNOWN_DW_ACCESS(NAME, CODE) [CODE] = #NAME,
4439 : : DWARF_ALL_KNOWN_DW_ACCESS
4440 : : #undef DWARF_ONE_KNOWN_DW_ACCESS
4441 : : };
4442 : :
4443 : 0 : if (likely (code < sizeof (known) / sizeof (known[0])))
4444 : 0 : return known[code];
4445 : :
4446 : : return NULL;
4447 : : }
4448 : :
4449 : :
4450 : : static const char *
4451 : 0 : dwarf_defaulted_string (unsigned int code)
4452 : : {
4453 : 0 : static const char *const known[] =
4454 : : {
4455 : : #define DWARF_ONE_KNOWN_DW_DEFAULTED(NAME, CODE) [CODE] = #NAME,
4456 : : DWARF_ALL_KNOWN_DW_DEFAULTED
4457 : : #undef DWARF_ONE_KNOWN_DW_DEFAULTED
4458 : : };
4459 : :
4460 : 0 : if (likely (code < sizeof (known) / sizeof (known[0])))
4461 : 0 : return known[code];
4462 : :
4463 : : return NULL;
4464 : : }
4465 : :
4466 : :
4467 : : static const char *
4468 : 0 : dwarf_visibility_string (unsigned int code)
4469 : : {
4470 : 0 : static const char *const known[] =
4471 : : {
4472 : : #define DWARF_ONE_KNOWN_DW_VIS(NAME, CODE) [CODE] = #NAME,
4473 : : DWARF_ALL_KNOWN_DW_VIS
4474 : : #undef DWARF_ONE_KNOWN_DW_VIS
4475 : : };
4476 : :
4477 : 0 : if (likely (code < sizeof (known) / sizeof (known[0])))
4478 : 0 : return known[code];
4479 : :
4480 : : return NULL;
4481 : : }
4482 : :
4483 : :
4484 : : static const char *
4485 : 0 : dwarf_virtuality_string (unsigned int code)
4486 : : {
4487 : 0 : static const char *const known[] =
4488 : : {
4489 : : #define DWARF_ONE_KNOWN_DW_VIRTUALITY(NAME, CODE) [CODE] = #NAME,
4490 : : DWARF_ALL_KNOWN_DW_VIRTUALITY
4491 : : #undef DWARF_ONE_KNOWN_DW_VIRTUALITY
4492 : : };
4493 : :
4494 : 0 : if (likely (code < sizeof (known) / sizeof (known[0])))
4495 : 0 : return known[code];
4496 : :
4497 : : return NULL;
4498 : : }
4499 : :
4500 : :
4501 : : static const char *
4502 : 0 : dwarf_identifier_case_string (unsigned int code)
4503 : : {
4504 : 0 : static const char *const known[] =
4505 : : {
4506 : : #define DWARF_ONE_KNOWN_DW_ID(NAME, CODE) [CODE] = #NAME,
4507 : : DWARF_ALL_KNOWN_DW_ID
4508 : : #undef DWARF_ONE_KNOWN_DW_ID
4509 : : };
4510 : :
4511 : 0 : if (likely (code < sizeof (known) / sizeof (known[0])))
4512 : 0 : return known[code];
4513 : :
4514 : : return NULL;
4515 : : }
4516 : :
4517 : :
4518 : : static const char *
4519 : 0 : dwarf_calling_convention_string (unsigned int code)
4520 : : {
4521 : 0 : static const char *const known[] =
4522 : : {
4523 : : #define DWARF_ONE_KNOWN_DW_CC(NAME, CODE) [CODE] = #NAME,
4524 : : DWARF_ALL_KNOWN_DW_CC
4525 : : #undef DWARF_ONE_KNOWN_DW_CC
4526 : : };
4527 : :
4528 : 0 : if (likely (code < sizeof (known) / sizeof (known[0])))
4529 : 0 : return known[code];
4530 : :
4531 : : return NULL;
4532 : : }
4533 : :
4534 : :
4535 : : static const char *
4536 : 0 : dwarf_ordering_string (unsigned int code)
4537 : : {
4538 : 0 : static const char *const known[] =
4539 : : {
4540 : : #define DWARF_ONE_KNOWN_DW_ORD(NAME, CODE) [CODE] = #NAME,
4541 : : DWARF_ALL_KNOWN_DW_ORD
4542 : : #undef DWARF_ONE_KNOWN_DW_ORD
4543 : : };
4544 : :
4545 : 0 : if (likely (code < sizeof (known) / sizeof (known[0])))
4546 : 0 : return known[code];
4547 : :
4548 : : return NULL;
4549 : : }
4550 : :
4551 : :
4552 : : static const char *
4553 : 16 : dwarf_discr_list_string (unsigned int code)
4554 : : {
4555 : 16 : static const char *const known[] =
4556 : : {
4557 : : #define DWARF_ONE_KNOWN_DW_DSC(NAME, CODE) [CODE] = #NAME,
4558 : : DWARF_ALL_KNOWN_DW_DSC
4559 : : #undef DWARF_ONE_KNOWN_DW_DSC
4560 : : };
4561 : :
4562 : 16 : if (likely (code < sizeof (known) / sizeof (known[0])))
4563 : 16 : return known[code];
4564 : :
4565 : : return NULL;
4566 : : }
4567 : :
4568 : :
4569 : : static const char *
4570 : 1146908 : dwarf_locexpr_opcode_string (unsigned int code)
4571 : : {
4572 : 1146908 : static const char *const known[] =
4573 : : {
4574 : : /* Normally we can't afford building huge table of 64K entries,
4575 : : most of them zero, just because there are a couple defined
4576 : : values at the far end. In case of opcodes, it's OK. */
4577 : : #define DWARF_ONE_KNOWN_DW_OP(NAME, CODE) [CODE] = #NAME,
4578 : : DWARF_ALL_KNOWN_DW_OP
4579 : : #undef DWARF_ONE_KNOWN_DW_OP
4580 : : };
4581 : :
4582 : 1146908 : if (likely (code < sizeof (known) / sizeof (known[0])))
4583 : 1146908 : return known[code];
4584 : :
4585 : : return NULL;
4586 : : }
4587 : :
4588 : :
4589 : : static const char *
4590 : 3426 : dwarf_unit_string (unsigned int type)
4591 : : {
4592 [ - + - - : 3426 : switch (type)
- - + ]
4593 : : {
4594 : : #define DWARF_ONE_KNOWN_DW_UT(NAME, CODE) case CODE: return #NAME;
4595 : 14 : DWARF_ALL_KNOWN_DW_UT
4596 : : #undef DWARF_ONE_KNOWN_DW_UT
4597 : 0 : default:
4598 : 0 : return NULL;
4599 : : }
4600 : : }
4601 : :
4602 : :
4603 : : static const char *
4604 : 132876 : dwarf_range_list_encoding_string (unsigned int kind)
4605 : : {
4606 [ - + + - : 132876 : switch (kind)
+ - - -
+ ]
4607 : : {
4608 : : #define DWARF_ONE_KNOWN_DW_RLE(NAME, CODE) case CODE: return #NAME;
4609 : 124412 : DWARF_ALL_KNOWN_DW_RLE
4610 : : #undef DWARF_ONE_KNOWN_DW_RLE
4611 : 0 : default:
4612 : 0 : return NULL;
4613 : : }
4614 : : }
4615 : :
4616 : :
4617 : : static const char *
4618 : 626744 : dwarf_loc_list_encoding_string (unsigned int kind)
4619 : : {
4620 [ - - + + : 626744 : switch (kind)
- + - + -
- + ]
4621 : : {
4622 : : #define DWARF_ONE_KNOWN_DW_LLE(NAME, CODE) case CODE: return #NAME;
4623 : 608562 : DWARF_ALL_KNOWN_DW_LLE
4624 : : #undef DWARF_ONE_KNOWN_DW_LLE
4625 : : /* DW_LLE_GNU_view_pair is special/incompatible with default codes. */
4626 : 0 : case DW_LLE_GNU_view_pair: return "GNU_view_pair";
4627 : 0 : default:
4628 : 0 : return NULL;
4629 : : }
4630 : : }
4631 : :
4632 : :
4633 : : static const char *
4634 : 10238 : dwarf_line_content_description_string (unsigned int kind)
4635 : : {
4636 [ + + - - : 10238 : switch (kind)
- + ]
4637 : : {
4638 : : #define DWARF_ONE_KNOWN_DW_LNCT(NAME, CODE) case CODE: return #NAME;
4639 : 10230 : DWARF_ALL_KNOWN_DW_LNCT
4640 : : #undef DWARF_ONE_KNOWN_DW_LNCT
4641 : 0 : default:
4642 : 0 : return NULL;
4643 : : }
4644 : : }
4645 : :
4646 : :
4647 : : /* Used by all dwarf_foo_name functions. */
4648 : : static const char *
4649 : 20464116 : string_or_unknown (const char *known, unsigned int code,
4650 : : unsigned int lo_user, unsigned int hi_user,
4651 : : bool print_unknown_num)
4652 : : {
4653 : 20464116 : static char unknown_buf[20];
4654 : :
4655 [ + + ]: 20464116 : if (likely (known != NULL))
4656 : : return known;
4657 : :
4658 [ - + - - ]: 2 : if (lo_user != 0 && code >= lo_user && code <= hi_user)
4659 : : {
4660 : 0 : snprintf (unknown_buf, sizeof unknown_buf, "lo_user+%#x",
4661 : : code - lo_user);
4662 : 0 : return unknown_buf;
4663 : : }
4664 : :
4665 [ + - ]: 2 : if (print_unknown_num)
4666 : : {
4667 : 2 : snprintf (unknown_buf, sizeof unknown_buf, "??? (%#x)", code);
4668 : 2 : return unknown_buf;
4669 : : }
4670 : :
4671 : : return "???";
4672 : : }
4673 : :
4674 : :
4675 : : static const char *
4676 : 2243838 : dwarf_tag_name (unsigned int tag)
4677 : : {
4678 : 2243838 : const char *ret = dwarf_tag_string (tag);
4679 : 2243838 : return string_or_unknown (ret, tag, DW_TAG_lo_user, DW_TAG_hi_user, true);
4680 : : }
4681 : :
4682 : : static const char *
4683 : 8684718 : dwarf_attr_name (unsigned int attr)
4684 : : {
4685 : 8684718 : const char *ret = dwarf_attr_string (attr);
4686 : 8684718 : return string_or_unknown (ret, attr, DW_AT_lo_user, DW_AT_hi_user, true);
4687 : : }
4688 : :
4689 : :
4690 : : static const char *
4691 : 8694956 : dwarf_form_name (unsigned int form)
4692 : : {
4693 : 8694956 : const char *ret = dwarf_form_string (form);
4694 : 8694956 : return string_or_unknown (ret, form, 0, 0, true);
4695 : : }
4696 : :
4697 : :
4698 : : static const char *
4699 : 3676 : dwarf_lang_name (unsigned int lang)
4700 : : {
4701 : 3676 : const char *ret = dwarf_lang_string (lang);
4702 : 3676 : return string_or_unknown (ret, lang, DW_LANG_lo_user, DW_LANG_hi_user, false);
4703 : : }
4704 : :
4705 : :
4706 : : static const char *
4707 : 4 : dwarf_lname_name (unsigned int lname)
4708 : : {
4709 : 4 : const char *ret = dwarf_lname_string (lname);
4710 : 4 : return string_or_unknown (ret, lname, DW_LNAME_lo_user, DW_LNAME_hi_user,
4711 : : false);
4712 : : }
4713 : :
4714 : :
4715 : : static const char *
4716 : 7138 : dwarf_inline_name (unsigned int code)
4717 : : {
4718 [ + - ]: 7138 : const char *ret = dwarf_inline_string (code);
4719 : 7138 : return string_or_unknown (ret, code, 0, 0, false);
4720 : : }
4721 : :
4722 : :
4723 : : static const char *
4724 : 56482 : dwarf_encoding_name (unsigned int code)
4725 : : {
4726 [ + - ]: 56482 : const char *ret = dwarf_encoding_string (code);
4727 : 56482 : return string_or_unknown (ret, code, DW_ATE_lo_user, DW_ATE_hi_user, false);
4728 : : }
4729 : :
4730 : :
4731 : : static const char *
4732 : 0 : dwarf_access_name (unsigned int code)
4733 : : {
4734 [ # # ]: 0 : const char *ret = dwarf_access_string (code);
4735 : 0 : return string_or_unknown (ret, code, 0, 0, false);
4736 : : }
4737 : :
4738 : :
4739 : : static const char *
4740 : 0 : dwarf_defaulted_name (unsigned int code)
4741 : : {
4742 [ # # ]: 0 : const char *ret = dwarf_defaulted_string (code);
4743 : 0 : return string_or_unknown (ret, code, 0, 0, false);
4744 : : }
4745 : :
4746 : :
4747 : : static const char *
4748 : 0 : dwarf_visibility_name (unsigned int code)
4749 : : {
4750 [ # # ]: 0 : const char *ret = dwarf_visibility_string (code);
4751 : 0 : return string_or_unknown (ret, code, 0, 0, false);
4752 : : }
4753 : :
4754 : :
4755 : : static const char *
4756 : 0 : dwarf_virtuality_name (unsigned int code)
4757 : : {
4758 [ # # ]: 0 : const char *ret = dwarf_virtuality_string (code);
4759 : 0 : return string_or_unknown (ret, code, 0, 0, false);
4760 : : }
4761 : :
4762 : :
4763 : : static const char *
4764 : 0 : dwarf_identifier_case_name (unsigned int code)
4765 : : {
4766 [ # # ]: 0 : const char *ret = dwarf_identifier_case_string (code);
4767 : 0 : return string_or_unknown (ret, code, 0, 0, false);
4768 : : }
4769 : :
4770 : :
4771 : : static const char *
4772 : 0 : dwarf_calling_convention_name (unsigned int code)
4773 : : {
4774 [ # # ]: 0 : const char *ret = dwarf_calling_convention_string (code);
4775 : 0 : return string_or_unknown (ret, code, DW_CC_lo_user, DW_CC_hi_user, false);
4776 : : }
4777 : :
4778 : :
4779 : : static const char *
4780 : 0 : dwarf_ordering_name (unsigned int code)
4781 : : {
4782 [ # # ]: 0 : const char *ret = dwarf_ordering_string (code);
4783 : 0 : return string_or_unknown (ret, code, 0, 0, false);
4784 : : }
4785 : :
4786 : :
4787 : : static const char *
4788 : 16 : dwarf_discr_list_name (unsigned int code)
4789 : : {
4790 [ + - ]: 16 : const char *ret = dwarf_discr_list_string (code);
4791 : 16 : return string_or_unknown (ret, code, 0, 0, false);
4792 : : }
4793 : :
4794 : :
4795 : : static const char *
4796 : 3426 : dwarf_unit_name (unsigned int type)
4797 : : {
4798 : 3426 : const char *ret = dwarf_unit_string (type);
4799 : 3426 : return string_or_unknown (ret, type, DW_UT_lo_user, DW_UT_hi_user, true);
4800 : : }
4801 : :
4802 : :
4803 : : static const char *
4804 : 132876 : dwarf_range_list_encoding_name (unsigned int kind)
4805 : : {
4806 : 132876 : const char *ret = dwarf_range_list_encoding_string (kind);
4807 : 132876 : return string_or_unknown (ret, kind, 0, 0, false);
4808 : : }
4809 : :
4810 : :
4811 : : static const char *
4812 : 626744 : dwarf_loc_list_encoding_name (unsigned int kind)
4813 : : {
4814 : 626744 : const char *ret = dwarf_loc_list_encoding_string (kind);
4815 : 626744 : return string_or_unknown (ret, kind, 0, 0, false);
4816 : : }
4817 : :
4818 : :
4819 : : static const char *
4820 : 10238 : dwarf_line_content_description_name (unsigned int kind)
4821 : : {
4822 : 10238 : const char *ret = dwarf_line_content_description_string (kind);
4823 : 10238 : return string_or_unknown (ret, kind, DW_LNCT_lo_user, DW_LNCT_hi_user,
4824 : : false);
4825 : : }
4826 : :
4827 : :
4828 : : static void
4829 : 510 : print_block (size_t n, const void *block)
4830 : : {
4831 [ - + ]: 510 : if (n == 0)
4832 : 0 : puts (_("empty block"));
4833 : : else
4834 : : {
4835 : 510 : printf (_("%zu byte block:"), n);
4836 : 510 : const unsigned char *data = block;
4837 : 2250 : do
4838 : 2250 : printf (" %02x", *data++);
4839 [ + + ]: 2250 : while (--n > 0);
4840 : 510 : putchar ('\n');
4841 : : }
4842 : 510 : }
4843 : :
4844 : : static void
4845 : 24 : print_bytes (size_t n, const unsigned char *bytes)
4846 : : {
4847 : 24 : while (n-- > 0)
4848 : : {
4849 : 384 : printf ("%02x", *bytes++);
4850 [ + + ]: 384 : if (n > 0)
4851 [ + + ]: 768 : printf (" ");
4852 : : }
4853 : 24 : }
4854 : :
4855 : : static int
4856 : 148 : get_indexed_addr (Dwarf_CU *cu, Dwarf_Word idx, Dwarf_Addr *addr)
4857 : : {
4858 [ - + ]: 148 : if (cu == NULL)
4859 : : return -1;
4860 : :
4861 : 148 : Elf_Data *debug_addr = cu->dbg->sectiondata[IDX_debug_addr];
4862 [ - + ]: 148 : if (debug_addr == NULL)
4863 : : return -1;
4864 : :
4865 : 148 : Dwarf_Off base = __libdw_cu_addr_base (cu);
4866 : 148 : Dwarf_Word off = idx * cu->address_size;
4867 [ - + ]: 148 : if (base > debug_addr->d_size
4868 [ - + ]: 148 : || off > debug_addr->d_size - base
4869 [ - + ]: 148 : || cu->address_size > debug_addr->d_size - base - off)
4870 : : return -1;
4871 : :
4872 : 148 : const unsigned char *addrp = debug_addr->d_buf + base + off;
4873 [ + + ]: 148 : if (cu->address_size == 4)
4874 [ - + ]: 12 : *addr = read_4ubyte_unaligned (cu->dbg, addrp);
4875 : : else
4876 [ - + ]: 136 : *addr = read_8ubyte_unaligned (cu->dbg, addrp);
4877 : :
4878 : : return 0;
4879 : : }
4880 : :
4881 : : static void
4882 : 784862 : print_ops (Dwfl_Module *dwflmod, Dwarf *dbg, int indent, int indentrest,
4883 : : unsigned int vers, unsigned int addrsize, unsigned int offset_size,
4884 : : struct Dwarf_CU *cu, Dwarf_Word len, const unsigned char *data)
4885 : : {
4886 [ + + ]: 784862 : const unsigned int ref_size = vers < 3 ? addrsize : offset_size;
4887 : :
4888 [ - + ]: 784862 : if (len == 0)
4889 : : {
4890 : 0 : printf ("%*s(empty)\n", indent, "");
4891 : 0 : return;
4892 : : }
4893 : :
4894 : : #define NEED(n) if (len < (Dwarf_Word) (n)) goto invalid
4895 : : #define CONSUME(n) NEED (n); else len -= (n)
4896 : :
4897 : : Dwarf_Word offset = 0;
4898 [ + + ]: 1931770 : while (len-- > 0)
4899 : 1146908 : {
4900 : 1146908 : uint_fast8_t op = *data++;
4901 : :
4902 [ + - ]: 1146908 : const char *op_name = dwarf_locexpr_opcode_string (op);
4903 [ - + ]: 1146908 : if (unlikely (op_name == NULL))
4904 : : {
4905 : 0 : static char buf[20];
4906 [ # # ]: 0 : if (op >= DW_OP_lo_user)
4907 : 0 : snprintf (buf, sizeof buf, "lo_user+%#x", op - DW_OP_lo_user);
4908 : : else
4909 : 0 : snprintf (buf, sizeof buf, "??? (%#x)", op);
4910 : : op_name = buf;
4911 : : }
4912 : :
4913 [ + - + + : 1146908 : switch (op)
+ + + + -
- + + - +
- - + + +
+ + - - -
- + + + ]
4914 : : {
4915 : 25942 : case DW_OP_addr:;
4916 : : /* Address operand. */
4917 : 25942 : Dwarf_Word addr;
4918 [ - + ]: 25942 : NEED (addrsize);
4919 [ + + ]: 25942 : if (addrsize == 4)
4920 [ + + ]: 108 : addr = read_4ubyte_unaligned (dbg, data);
4921 [ + - ]: 25834 : else if (addrsize == 8)
4922 [ + + ]: 25834 : addr = read_8ubyte_unaligned (dbg, data);
4923 : : else
4924 : 0 : goto invalid;
4925 : 25942 : data += addrsize;
4926 : 25942 : CONSUME (addrsize);
4927 : :
4928 : 25942 : printf ("%*s[%2" PRIuMAX "] %s ",
4929 : : indent, "", (uintmax_t) offset, op_name);
4930 : 25942 : print_dwarf_addr (dwflmod, 0, addr, addr);
4931 : 25942 : printf ("\n");
4932 : :
4933 : 25942 : offset += 1 + addrsize;
4934 : 25942 : break;
4935 : :
4936 : 0 : case DW_OP_call_ref:
4937 : : case DW_OP_GNU_variable_value:
4938 : : /* Offset operand. */
4939 [ # # ]: 0 : if (ref_size != 4 && ref_size != 8)
4940 : 0 : goto invalid; /* Cannot be used in CFA. */
4941 [ # # ]: 0 : NEED (ref_size);
4942 [ # # ]: 0 : if (ref_size == 4)
4943 [ # # ]: 0 : addr = read_4ubyte_unaligned (dbg, data);
4944 : : else
4945 [ # # ]: 0 : addr = read_8ubyte_unaligned (dbg, data);
4946 : 0 : data += ref_size;
4947 : 0 : CONSUME (ref_size);
4948 : : /* addr is a DIE offset, so format it as one. */
4949 : 0 : printf ("%*s[%2" PRIuMAX "] %s [%6" PRIxMAX "]\n",
4950 : : indent, "", (uintmax_t) offset,
4951 : : op_name, (uintmax_t) addr);
4952 : 0 : offset += 1 + ref_size;
4953 : 0 : break;
4954 : :
4955 : 27076 : case DW_OP_deref_size:
4956 : : case DW_OP_xderef_size:
4957 : : case DW_OP_pick:
4958 : : case DW_OP_const1u:
4959 : : // XXX value might be modified by relocation
4960 [ - + ]: 27076 : NEED (1);
4961 : 54152 : printf ("%*s[%2" PRIuMAX "] %s %" PRIu8 "\n",
4962 : : indent, "", (uintmax_t) offset,
4963 : 27076 : op_name, *((uint8_t *) data));
4964 : 27076 : ++data;
4965 : 27076 : --len;
4966 : 27076 : offset += 2;
4967 : 27076 : break;
4968 : :
4969 : 3242 : case DW_OP_const2u:
4970 [ - + ]: 3242 : NEED (2);
4971 : : // XXX value might be modified by relocation
4972 : 0 : printf ("%*s[%2" PRIuMAX "] %s %" PRIu16 "\n",
4973 : : indent, "", (uintmax_t) offset,
4974 [ - + ]: 3242 : op_name, read_2ubyte_unaligned (dbg, data));
4975 : 3242 : CONSUME (2);
4976 : 3242 : data += 2;
4977 : 3242 : offset += 3;
4978 : 3242 : break;
4979 : :
4980 : 2324 : case DW_OP_const4u:
4981 [ - + ]: 2324 : NEED (4);
4982 : : // XXX value might be modified by relocation
4983 : 2324 : printf ("%*s[%2" PRIuMAX "] %s %" PRIu32 "\n",
4984 : : indent, "", (uintmax_t) offset,
4985 [ - + ]: 2324 : op_name, read_4ubyte_unaligned (dbg, data));
4986 : 2324 : CONSUME (4);
4987 : 2324 : data += 4;
4988 : 2324 : offset += 5;
4989 : 2324 : break;
4990 : :
4991 : 112 : case DW_OP_const8u:
4992 [ - + ]: 112 : NEED (8);
4993 : : // XXX value might be modified by relocation
4994 : 112 : printf ("%*s[%2" PRIuMAX "] %s %" PRIu64 "\n",
4995 : : indent, "", (uintmax_t) offset,
4996 [ - + ]: 112 : op_name, (uint64_t) read_8ubyte_unaligned (dbg, data));
4997 : 112 : CONSUME (8);
4998 : 112 : data += 8;
4999 : 112 : offset += 9;
5000 : 112 : break;
5001 : :
5002 : 4910 : case DW_OP_const1s:
5003 [ - + ]: 4910 : NEED (1);
5004 : : // XXX value might be modified by relocation
5005 : 9820 : printf ("%*s[%2" PRIuMAX "] %s %" PRId8 "\n",
5006 : : indent, "", (uintmax_t) offset,
5007 : 4910 : op_name, *((int8_t *) data));
5008 : 4910 : ++data;
5009 : 4910 : --len;
5010 : 4910 : offset += 2;
5011 : 4910 : break;
5012 : :
5013 : 10 : case DW_OP_const2s:
5014 [ - + ]: 10 : NEED (2);
5015 : : // XXX value might be modified by relocation
5016 : 0 : printf ("%*s[%2" PRIuMAX "] %s %" PRId16 "\n",
5017 : : indent, "", (uintmax_t) offset,
5018 [ - + ]: 10 : op_name, read_2sbyte_unaligned (dbg, data));
5019 : 10 : CONSUME (2);
5020 : 10 : data += 2;
5021 : 10 : offset += 3;
5022 : 10 : break;
5023 : :
5024 : 0 : case DW_OP_const4s:
5025 [ # # ]: 0 : NEED (4);
5026 : : // XXX value might be modified by relocation
5027 : 0 : printf ("%*s[%2" PRIuMAX "] %s %" PRId32 "\n",
5028 : : indent, "", (uintmax_t) offset,
5029 [ # # ]: 0 : op_name, read_4sbyte_unaligned (dbg, data));
5030 : 0 : CONSUME (4);
5031 : 0 : data += 4;
5032 : 0 : offset += 5;
5033 : 0 : break;
5034 : :
5035 : 0 : case DW_OP_const8s:
5036 [ # # ]: 0 : NEED (8);
5037 : : // XXX value might be modified by relocation
5038 : 0 : printf ("%*s[%2" PRIuMAX "] %s %" PRId64 "\n",
5039 : : indent, "", (uintmax_t) offset,
5040 [ # # ]: 0 : op_name, read_8sbyte_unaligned (dbg, data));
5041 : 0 : CONSUME (8);
5042 : 0 : data += 8;
5043 : 0 : offset += 9;
5044 : 0 : break;
5045 : :
5046 : 27034 : case DW_OP_piece:
5047 : : case DW_OP_regx:
5048 : : case DW_OP_plus_uconst:
5049 : 27034 : case DW_OP_constu:;
5050 : 27034 : const unsigned char *start = data;
5051 : 27034 : uint64_t uleb;
5052 [ - + ]: 27034 : NEED (1);
5053 : 27034 : get_uleb128 (uleb, data, data + len);
5054 : 27034 : printf ("%*s[%2" PRIuMAX "] %s %" PRIu64 "\n",
5055 : : indent, "", (uintmax_t) offset, op_name, uleb);
5056 [ - + ]: 27034 : CONSUME (data - start);
5057 : 27034 : offset += 1 + (data - start);
5058 : 27034 : break;
5059 : :
5060 : 16 : case DW_OP_addrx:
5061 : : case DW_OP_GNU_addr_index:
5062 : : case DW_OP_constx:
5063 : 16 : case DW_OP_GNU_const_index:;
5064 : 16 : start = data;
5065 [ - + ]: 16 : NEED (1);
5066 : 16 : get_uleb128 (uleb, data, data + len);
5067 : 16 : printf ("%*s[%2" PRIuMAX "] %s [%" PRIu64 "] ",
5068 : : indent, "", (uintmax_t) offset, op_name, uleb);
5069 [ - + ]: 16 : CONSUME (data - start);
5070 : 16 : offset += 1 + (data - start);
5071 [ - + ]: 16 : if (get_indexed_addr (cu, uleb, &addr) != 0)
5072 : 0 : printf ("???\n");
5073 : : else
5074 : : {
5075 : 16 : print_dwarf_addr (dwflmod, 0, addr, addr);
5076 : 16 : printf ("\n");
5077 : : }
5078 : : break;
5079 : :
5080 : 0 : case DW_OP_bit_piece:
5081 : 0 : start = data;
5082 : 0 : uint64_t uleb2;
5083 [ # # ]: 0 : NEED (1);
5084 : 0 : get_uleb128 (uleb, data, data + len);
5085 : 0 : NEED (1);
5086 : 0 : get_uleb128 (uleb2, data, data + len);
5087 : 0 : printf ("%*s[%2" PRIuMAX "] %s %" PRIu64 ", %" PRIu64 "\n",
5088 : : indent, "", (uintmax_t) offset, op_name, uleb, uleb2);
5089 [ # # ]: 0 : CONSUME (data - start);
5090 : 0 : offset += 1 + (data - start);
5091 : 0 : break;
5092 : :
5093 : 201322 : case DW_OP_fbreg:
5094 : : case DW_OP_breg0 ... DW_OP_breg31:
5095 : : case DW_OP_consts:
5096 : 201322 : start = data;
5097 : 201322 : int64_t sleb;
5098 [ - + ]: 201322 : NEED (1);
5099 : 201322 : get_sleb128 (sleb, data, data + len);
5100 : 201322 : printf ("%*s[%2" PRIuMAX "] %s %" PRId64 "\n",
5101 : : indent, "", (uintmax_t) offset, op_name, sleb);
5102 [ - + ]: 201322 : CONSUME (data - start);
5103 : 201322 : offset += 1 + (data - start);
5104 : 201322 : break;
5105 : :
5106 : 0 : case DW_OP_bregx:
5107 : 0 : start = data;
5108 [ # # ]: 0 : NEED (1);
5109 : 0 : get_uleb128 (uleb, data, data + len);
5110 : 0 : NEED (1);
5111 : 0 : get_sleb128 (sleb, data, data + len);
5112 : 0 : printf ("%*s[%2" PRIuMAX "] %s %" PRIu64 " %" PRId64 "\n",
5113 : : indent, "", (uintmax_t) offset, op_name, uleb, sleb);
5114 [ # # ]: 0 : CONSUME (data - start);
5115 : 0 : offset += 1 + (data - start);
5116 : 0 : break;
5117 : :
5118 : 0 : case DW_OP_call2:
5119 [ # # ]: 0 : NEED (2);
5120 : 0 : printf ("%*s[%2" PRIuMAX "] %s [%6" PRIx16 "]\n",
5121 : : indent, "", (uintmax_t) offset, op_name,
5122 [ # # ]: 0 : read_2ubyte_unaligned (dbg, data));
5123 : 0 : CONSUME (2);
5124 : 0 : data += 2;
5125 : 0 : offset += 3;
5126 : 0 : break;
5127 : :
5128 : 8 : case DW_OP_call4:
5129 [ - + ]: 8 : NEED (4);
5130 : 8 : printf ("%*s[%2" PRIuMAX "] %s [%6" PRIx32 "]\n",
5131 : : indent, "", (uintmax_t) offset, op_name,
5132 [ - + ]: 8 : read_4ubyte_unaligned (dbg, data));
5133 : 8 : CONSUME (4);
5134 : 8 : data += 4;
5135 : 8 : offset += 5;
5136 : 8 : break;
5137 : :
5138 : 1128 : case DW_OP_skip:
5139 : : case DW_OP_bra:
5140 [ - + ]: 1128 : NEED (2);
5141 : 2256 : printf ("%*s[%2" PRIuMAX "] %s %" PRIuMAX "\n",
5142 : : indent, "", (uintmax_t) offset, op_name,
5143 [ - + ]: 1128 : (uintmax_t) (offset + read_2sbyte_unaligned (dbg, data) + 3));
5144 : 1128 : CONSUME (2);
5145 : 1128 : data += 2;
5146 : 1128 : offset += 3;
5147 : 1128 : break;
5148 : :
5149 : 510 : case DW_OP_implicit_value:
5150 : 510 : start = data;
5151 [ - + ]: 510 : NEED (1);
5152 : 510 : get_uleb128 (uleb, data, data + len);
5153 : 510 : printf ("%*s[%2" PRIuMAX "] %s: ",
5154 : : indent, "", (uintmax_t) offset, op_name);
5155 [ - + ]: 510 : NEED (uleb);
5156 : 510 : print_block (uleb, data);
5157 : 510 : data += uleb;
5158 [ - + ]: 510 : CONSUME (data - start);
5159 : 510 : offset += 1 + (data - start);
5160 : 510 : break;
5161 : :
5162 : 5024 : case DW_OP_implicit_pointer:
5163 : : case DW_OP_GNU_implicit_pointer:
5164 : : /* DIE offset operand. */
5165 : 5024 : start = data;
5166 [ - + ]: 5024 : NEED (ref_size);
5167 [ - + ]: 5024 : if (ref_size != 4 && ref_size != 8)
5168 : 0 : goto invalid; /* Cannot be used in CFA. */
5169 [ + - ]: 5024 : if (ref_size == 4)
5170 [ - + ]: 5024 : addr = read_4ubyte_unaligned (dbg, data);
5171 : : else
5172 [ # # ]: 0 : addr = read_8ubyte_unaligned (dbg, data);
5173 : 5024 : data += ref_size;
5174 : : /* Byte offset operand. */
5175 [ - + ]: 5024 : NEED (1);
5176 : 5024 : get_sleb128 (sleb, data, data + len);
5177 : :
5178 : 5024 : printf ("%*s[%2" PRIuMAX "] %s [%6" PRIxMAX "] %+" PRId64 "\n",
5179 : : indent, "", (intmax_t) offset,
5180 : : op_name, (uintmax_t) addr, sleb);
5181 [ - + ]: 5024 : CONSUME (data - start);
5182 : 5024 : offset += 1 + (data - start);
5183 : 5024 : break;
5184 : :
5185 : 63582 : case DW_OP_entry_value:
5186 : : case DW_OP_GNU_entry_value:
5187 : : /* Size plus expression block. */
5188 : 63582 : start = data;
5189 [ - + ]: 63582 : NEED (1);
5190 : 63582 : get_uleb128 (uleb, data, data + len);
5191 : 63582 : printf ("%*s[%2" PRIuMAX "] %s:\n",
5192 : : indent, "", (uintmax_t) offset, op_name);
5193 [ - + ]: 63582 : NEED (uleb);
5194 : 63582 : print_ops (dwflmod, dbg, indent + 5, indent + 5, vers,
5195 : : addrsize, offset_size, cu, uleb, data);
5196 : 63582 : data += uleb;
5197 [ - + ]: 63582 : CONSUME (data - start);
5198 : 63582 : offset += 1 + (data - start);
5199 : 63582 : break;
5200 : :
5201 : 0 : case DW_OP_const_type:
5202 : : case DW_OP_GNU_const_type:
5203 : : /* uleb128 CU relative DW_TAG_base_type DIE offset, 1-byte
5204 : : unsigned size plus block. */
5205 : 0 : start = data;
5206 [ # # ]: 0 : NEED (1);
5207 : 0 : get_uleb128 (uleb, data, data + len);
5208 [ # # # # ]: 0 : if (! print_unresolved_addresses && cu != NULL)
5209 : 0 : uleb += cu->start;
5210 : 0 : NEED (1);
5211 : 0 : uint8_t usize = *(uint8_t *) data++;
5212 [ # # ]: 0 : NEED (usize);
5213 : 0 : printf ("%*s[%2" PRIuMAX "] %s [%6" PRIxMAX "] ",
5214 : : indent, "", (uintmax_t) offset, op_name, uleb);
5215 : 0 : print_block (usize, data);
5216 : 0 : data += usize;
5217 [ # # ]: 0 : CONSUME (data - start);
5218 : 0 : offset += 1 + (data - start);
5219 : 0 : break;
5220 : :
5221 : 0 : case DW_OP_regval_type:
5222 : : case DW_OP_GNU_regval_type:
5223 : : /* uleb128 register number, uleb128 CU relative
5224 : : DW_TAG_base_type DIE offset. */
5225 : 0 : start = data;
5226 [ # # ]: 0 : NEED (1);
5227 : 0 : get_uleb128 (uleb, data, data + len);
5228 : 0 : NEED (1);
5229 : 0 : get_uleb128 (uleb2, data, data + len);
5230 [ # # # # ]: 0 : if (! print_unresolved_addresses && cu != NULL)
5231 : 0 : uleb2 += cu->start;
5232 : 0 : printf ("%*s[%2" PRIuMAX "] %s %" PRIu64 " [%6" PRIx64 "]\n",
5233 : : indent, "", (uintmax_t) offset, op_name, uleb, uleb2);
5234 [ # # ]: 0 : CONSUME (data - start);
5235 : 0 : offset += 1 + (data - start);
5236 : 0 : break;
5237 : :
5238 : 0 : case DW_OP_deref_type:
5239 : : case DW_OP_GNU_deref_type:
5240 : : /* 1-byte unsigned size of value, uleb128 CU relative
5241 : : DW_TAG_base_type DIE offset. */
5242 : 0 : start = data;
5243 [ # # ]: 0 : NEED (1);
5244 : 0 : usize = *(uint8_t *) data++;
5245 : 0 : NEED (1);
5246 : 0 : get_uleb128 (uleb, data, data + len);
5247 [ # # # # ]: 0 : if (! print_unresolved_addresses && cu != NULL)
5248 : 0 : uleb += cu->start;
5249 : 0 : printf ("%*s[%2" PRIuMAX "] %s %" PRIu8 " [%6" PRIxMAX "]\n",
5250 : : indent, "", (uintmax_t) offset,
5251 : : op_name, usize, uleb);
5252 [ # # ]: 0 : CONSUME (data - start);
5253 : 0 : offset += 1 + (data - start);
5254 : 0 : break;
5255 : :
5256 : 0 : case DW_OP_xderef_type:
5257 : : /* 1-byte unsigned size of value, uleb128 base_type DIE offset. */
5258 : 0 : start = data;
5259 [ # # ]: 0 : NEED (1);
5260 : 0 : usize = *(uint8_t *) data++;
5261 : 0 : NEED (1);
5262 : 0 : get_uleb128 (uleb, data, data + len);
5263 : 0 : printf ("%*s[%4" PRIuMAX "] %s %" PRIu8 " [%6" PRIxMAX "]\n",
5264 : : indent, "", (uintmax_t) offset,
5265 : : op_name, usize, uleb);
5266 [ # # ]: 0 : CONSUME (data - start);
5267 : 0 : offset += 1 + (data - start);
5268 : 0 : break;
5269 : :
5270 : 810 : case DW_OP_convert:
5271 : : case DW_OP_GNU_convert:
5272 : : case DW_OP_reinterpret:
5273 : : case DW_OP_GNU_reinterpret:
5274 : : /* uleb128 CU relative offset to DW_TAG_base_type, or zero
5275 : : for conversion to untyped. */
5276 : 810 : start = data;
5277 [ - + ]: 810 : NEED (1);
5278 : 810 : get_uleb128 (uleb, data, data + len);
5279 [ + + + - : 810 : if (uleb != 0 && ! print_unresolved_addresses && cu != NULL)
+ - ]
5280 : 540 : uleb += cu->start;
5281 : 810 : printf ("%*s[%2" PRIuMAX "] %s [%6" PRIxMAX "]\n",
5282 : : indent, "", (uintmax_t) offset, op_name, uleb);
5283 [ - + ]: 810 : CONSUME (data - start);
5284 : 810 : offset += 1 + (data - start);
5285 : 810 : break;
5286 : :
5287 : 8 : case DW_OP_GNU_parameter_ref:
5288 : : /* 4 byte CU relative reference to the abstract optimized away
5289 : : DW_TAG_formal_parameter. */
5290 [ - + ]: 8 : NEED (4);
5291 [ - + ]: 8 : uintmax_t param_off = (uintmax_t) read_4ubyte_unaligned (dbg, data);
5292 [ + - + - ]: 8 : if (! print_unresolved_addresses && cu != NULL)
5293 : 8 : param_off += cu->start;
5294 : 8 : printf ("%*s[%2" PRIuMAX "] %s [%6" PRIxMAX "]\n",
5295 : : indent, "", (uintmax_t) offset, op_name, param_off);
5296 : 8 : CONSUME (4);
5297 : 8 : data += 4;
5298 : 8 : offset += 5;
5299 : 8 : break;
5300 : :
5301 : : default:
5302 : : /* No Operand. */
5303 : 783850 : printf ("%*s[%2" PRIuMAX "] %s\n",
5304 : : indent, "", (uintmax_t) offset, op_name);
5305 : 783850 : ++offset;
5306 : 783850 : break;
5307 : : }
5308 : :
5309 : 1146908 : indent = indentrest;
5310 : 1146908 : continue;
5311 : :
5312 : 0 : invalid:
5313 : 0 : printf (_("%*s[%2" PRIuMAX "] %s <TRUNCATED>\n"),
5314 : : indent, "", (uintmax_t) offset, op_name);
5315 : : break;
5316 : : }
5317 : : }
5318 : :
5319 : :
5320 : : /* Turn the addresses into file offsets by using the phdrs. */
5321 : : static void
5322 : 2 : find_offsets(Elf *elf, GElf_Addr main_bias, size_t n,
5323 : : GElf_Addr addrs[n], GElf_Off offs[n])
5324 : : {
5325 : 2 : size_t unsolved = n;
5326 [ + + ]: 18 : for (size_t i = 0; i < phnum; ++i) {
5327 : 16 : GElf_Phdr phdr_mem;
5328 : 16 : GElf_Phdr *phdr = gelf_getphdr(elf, i, &phdr_mem);
5329 [ + - + + : 16 : if (phdr != NULL && phdr->p_type == PT_LOAD && phdr->p_memsz > 0)
+ - ]
5330 [ + + ]: 46 : for (size_t j = 0; j < n; ++j)
5331 [ + + + + ]: 44 : if (offs[j] == 0 && addrs[j] >= phdr->p_vaddr + main_bias &&
5332 [ + - ]: 22 : addrs[j] - (phdr->p_vaddr + main_bias) < phdr->p_filesz) {
5333 : 22 : offs[j] = addrs[j] - (phdr->p_vaddr + main_bias) + phdr->p_offset;
5334 [ + + ]: 22 : if (--unsolved == 0)
5335 : : break;
5336 : : }
5337 : : }
5338 : 2 : }
5339 : :
5340 : : /* The dynamic segment (type PT_DYNAMIC), contains the .dynamic section.
5341 : : And .dynamic section contains an array of the dynamic structures.
5342 : : We use the array to get:
5343 : : DT_STRTAB: the address of the string table
5344 : : DT_SYMTAB: the address of the symbol table
5345 : : DT_STRSZ: the size, in bytes, of the string table
5346 : : ... */
5347 : : static void
5348 : 2 : get_dynscn_addrs(Elf *elf, GElf_Phdr *phdr, GElf_Addr addrs[i_max])
5349 : : {
5350 : 2 : Elf_Data *data = elf_getdata_rawchunk(
5351 : 2 : elf, phdr->p_offset, phdr->p_filesz, ELF_T_DYN);
5352 : :
5353 : 2 : int dyn_idx = 0;
5354 : 44 : for (;; ++dyn_idx) {
5355 : 46 : GElf_Dyn dyn_mem;
5356 : 46 : GElf_Dyn *dyn = gelf_getdyn(data, dyn_idx, &dyn_mem);
5357 : : /* DT_NULL Marks end of dynamic section. */
5358 [ + - + + ]: 46 : if (dyn == NULL || dyn->d_tag == DT_NULL)
5359 : : break;
5360 : :
5361 [ + - + + : 44 : switch (dyn->d_tag) {
+ + + + +
+ - + ]
5362 : 2 : case DT_SYMTAB:
5363 : 2 : addrs[i_symtab] = dyn->d_un.d_ptr;
5364 : 2 : break;
5365 : :
5366 : 0 : case DT_HASH:
5367 : 0 : addrs[i_hash] = dyn->d_un.d_ptr;
5368 : 0 : break;
5369 : :
5370 : 2 : case DT_GNU_HASH:
5371 : 2 : addrs[i_gnu_hash] = dyn->d_un.d_ptr;
5372 : 2 : break;
5373 : :
5374 : 2 : case DT_STRTAB:
5375 : 2 : addrs[i_strtab] = dyn->d_un.d_ptr;
5376 : 2 : break;
5377 : :
5378 : 2 : case DT_VERSYM:
5379 : 2 : addrs[i_versym] = dyn->d_un.d_ptr;
5380 : 2 : break;
5381 : :
5382 : 2 : case DT_VERDEF:
5383 : 2 : addrs[i_verdef] = dyn->d_un.d_ptr;
5384 : 2 : break;
5385 : :
5386 : 2 : case DT_VERDEFNUM:
5387 : 2 : addrs[i_verdefnum] = dyn->d_un.d_val;
5388 : 2 : break;
5389 : :
5390 : 2 : case DT_VERNEED:
5391 : 2 : addrs[i_verneed] = dyn->d_un.d_ptr;
5392 : 2 : break;
5393 : :
5394 : 2 : case DT_VERNEEDNUM:
5395 : 2 : addrs[i_verneednum] = dyn->d_un.d_val;
5396 : 2 : break;
5397 : :
5398 : 2 : case DT_STRSZ:
5399 : 2 : addrs[i_strsz] = dyn->d_un.d_val;
5400 : 2 : break;
5401 : :
5402 : 0 : case DT_SYMTAB_SHNDX:
5403 : 0 : addrs[i_symtab_shndx] = dyn->d_un.d_ptr;
5404 : 0 : break;
5405 : : }
5406 : : }
5407 : 2 : }
5408 : :
5409 : :
5410 : : /* Use dynamic segment to get data for the string table section. */
5411 : : static Elf_Data *
5412 : 2 : get_dynscn_strtab(Elf *elf, GElf_Phdr *phdr)
5413 : : {
5414 : 2 : Elf_Data *strtab_data;
5415 : 2 : GElf_Addr addrs[i_max] = {0,};
5416 : 2 : GElf_Off offs[i_max] = {0,};
5417 : 2 : get_dynscn_addrs(elf, phdr, addrs);
5418 : 2 : find_offsets(elf, 0, i_max, addrs, offs);
5419 : 4 : strtab_data = elf_getdata_rawchunk(
5420 : 2 : elf, offs[i_strtab], addrs[i_strsz], ELF_T_BYTE);
5421 : 2 : return strtab_data;
5422 : : }
5423 : :
5424 : :
5425 : : struct listptr
5426 : : {
5427 : : Dwarf_Off offset:(64 - 3);
5428 : : bool addr64:1;
5429 : : bool dwarf64:1;
5430 : : bool warned:1;
5431 : : struct Dwarf_CU *cu;
5432 : : unsigned int attr;
5433 : : };
5434 : :
5435 : : #define listptr_offset_size(p) ((p)->dwarf64 ? 8 : 4)
5436 : : #define listptr_address_size(p) ((p)->addr64 ? 8 : 4)
5437 : :
5438 : : static Dwarf_Addr
5439 : 21356 : cudie_base (Dwarf_Die *cudie)
5440 : : {
5441 : 21356 : Dwarf_Addr base;
5442 : : /* Find the base address of the compilation unit. It will normally
5443 : : be specified by DW_AT_low_pc. In DWARF-3 draft 4, the base
5444 : : address could be overridden by DW_AT_entry_pc. It's been
5445 : : removed, but GCC emits DW_AT_entry_pc and not DW_AT_lowpc for
5446 : : compilation units with discontinuous ranges. */
5447 [ - + ]: 21356 : if (unlikely (dwarf_lowpc (cudie, &base) != 0))
5448 : : {
5449 : 0 : Dwarf_Attribute attr_mem;
5450 [ # # ]: 0 : if (dwarf_formaddr (dwarf_attr (cudie, DW_AT_entry_pc, &attr_mem),
5451 : : &base) != 0)
5452 : 0 : base = 0;
5453 : : }
5454 : 21356 : return base;
5455 : : }
5456 : :
5457 : : static Dwarf_Addr
5458 : 21356 : listptr_base (struct listptr *p)
5459 : : {
5460 : 21356 : Dwarf_Die cu = CUDIE (p->cu);
5461 : 21356 : return cudie_base (&cu);
5462 : : }
5463 : :
5464 : : /* To store the name used in compare_listptr */
5465 : : static const char *sort_listptr_name;
5466 : :
5467 : : static int
5468 : 1731032 : compare_listptr (const void *a, const void *b)
5469 : : {
5470 : 1731032 : const char *name = sort_listptr_name;
5471 : 1731032 : struct listptr *p1 = (void *) a;
5472 : 1731032 : struct listptr *p2 = (void *) b;
5473 : :
5474 [ + + ]: 1731032 : if (p1->offset < p2->offset)
5475 : : return -1;
5476 [ + + ]: 166406 : if (p1->offset > p2->offset)
5477 : : return 1;
5478 : :
5479 [ + - - + ]: 8104 : if (!p1->warned && !p2->warned)
5480 : : {
5481 [ - + ]: 8104 : if (p1->addr64 != p2->addr64)
5482 : : {
5483 : 0 : p1->warned = p2->warned = true;
5484 : 8104 : error (0, 0,
5485 : 0 : _("%s %#" PRIx64 " used with different address sizes"),
5486 : : name, (uint64_t) p1->offset);
5487 : : }
5488 [ - + ]: 8104 : if (p1->dwarf64 != p2->dwarf64)
5489 : : {
5490 : 0 : p1->warned = p2->warned = true;
5491 : 8104 : error (0, 0,
5492 : 0 : _("%s %#" PRIx64 " used with different offset sizes"),
5493 : 0 : name, (uint64_t) p1->offset);
5494 : : }
5495 [ - + ]: 8104 : if (listptr_base (p1) != listptr_base (p2))
5496 : : {
5497 : 0 : p1->warned = p2->warned = true;
5498 : 8104 : error (0, 0,
5499 : 0 : _("%s %#" PRIx64 " used with different base addresses"),
5500 : 0 : name, (uint64_t) p1->offset);
5501 : : }
5502 [ + - ]: 8104 : if (p1->attr != p2 ->attr)
5503 : : {
5504 : 0 : p1->warned = p2->warned = true;
5505 : 0 : error (0, 0,
5506 : 0 : _("%s %#" PRIx64
5507 : : " used with different attribute %s and %s"),
5508 : 0 : name, (uint64_t) p1->offset, dwarf_attr_name (p1->attr),
5509 : : dwarf_attr_name (p2->attr));
5510 : : }
5511 : : }
5512 : :
5513 : : return 0;
5514 : : }
5515 : :
5516 : : struct listptr_table
5517 : : {
5518 : : size_t n;
5519 : : size_t alloc;
5520 : : struct listptr *table;
5521 : : };
5522 : :
5523 : : static struct listptr_table known_locsptr;
5524 : : static struct listptr_table known_loclistsptr;
5525 : : static struct listptr_table known_rangelistptr;
5526 : : static struct listptr_table known_rnglistptr;
5527 : : static struct listptr_table known_addrbases;
5528 : : static struct listptr_table known_stroffbases;
5529 : :
5530 : : static void
5531 : 502 : reset_listptr (struct listptr_table *table)
5532 : : {
5533 : 502 : free (table->table);
5534 : 502 : table->table = NULL;
5535 : 502 : table->n = table->alloc = 0;
5536 : : }
5537 : :
5538 : : /* Returns false if offset doesn't fit. See struct listptr. */
5539 : : static bool
5540 : 250974 : notice_listptr (enum section_e section, struct listptr_table *table,
5541 : : uint_fast8_t address_size, uint_fast8_t offset_size,
5542 : : struct Dwarf_CU *cu, Dwarf_Off offset, unsigned int attr)
5543 : : {
5544 [ + + ]: 250974 : if (print_debug_sections & section)
5545 : : {
5546 [ + + ]: 250722 : if (table->n == table->alloc)
5547 : : {
5548 [ + + ]: 408 : if (table->alloc == 0)
5549 : 174 : table->alloc = 128;
5550 : : else
5551 : 234 : table->alloc *= 2;
5552 : 408 : table->table = xrealloc (table->table,
5553 : 408 : table->alloc * sizeof table->table[0]);
5554 : : }
5555 : :
5556 : 250722 : struct listptr *p = &table->table[table->n++];
5557 : :
5558 : 250722 : *p = (struct listptr)
5559 : : {
5560 : 250722 : .addr64 = address_size == 8,
5561 : 250722 : .dwarf64 = offset_size == 8,
5562 : : .offset = offset,
5563 : : .cu = cu,
5564 : : .attr = attr
5565 : : };
5566 : :
5567 [ + - ]: 250722 : if (p->offset != offset)
5568 : : {
5569 : 0 : table->n--;
5570 : 0 : return false;
5571 : : }
5572 : : }
5573 : : return true;
5574 : : }
5575 : :
5576 : : static void
5577 : 182 : sort_listptr (struct listptr_table *table, const char *name)
5578 : : {
5579 [ + + ]: 182 : if (table->n > 0)
5580 : : {
5581 : 174 : sort_listptr_name = name;
5582 : 174 : qsort (table->table, table->n, sizeof table->table[0],
5583 : : &compare_listptr);
5584 : : }
5585 : 182 : }
5586 : :
5587 : : static bool
5588 : 208 : skip_listptr_hole (struct listptr_table *table, size_t *idxp,
5589 : : uint_fast8_t *address_sizep, uint_fast8_t *offset_sizep,
5590 : : Dwarf_Addr *base, struct Dwarf_CU **cu, ptrdiff_t offset,
5591 : : unsigned char **readp, unsigned char *endp,
5592 : : unsigned int *attr)
5593 : : {
5594 [ - + ]: 208 : if (table->n == 0)
5595 : : return false;
5596 : :
5597 [ + - + + ]: 336 : while (*idxp < table->n && table->table[*idxp].offset < (Dwarf_Off) offset)
5598 : 128 : ++*idxp;
5599 : :
5600 : 208 : struct listptr *p = &table->table[*idxp];
5601 : :
5602 [ + - ]: 208 : if (*idxp == table->n
5603 [ - + ]: 208 : || p->offset >= (Dwarf_Off) (endp - *readp + offset))
5604 : : {
5605 : 0 : *readp = endp;
5606 : 0 : printf (_(" [%6tx] <UNUSED GARBAGE IN REST OF SECTION>\n"),
5607 : : offset);
5608 : 0 : return true;
5609 : : }
5610 : :
5611 [ - + ]: 208 : if (p->offset != (Dwarf_Off) offset)
5612 : : {
5613 : 0 : *readp += p->offset - offset;
5614 : 0 : printf (_(" [%6tx] <UNUSED GARBAGE> ... %" PRIu64 " bytes ...\n"),
5615 : : offset, (Dwarf_Off) p->offset - offset);
5616 : 0 : return true;
5617 : : }
5618 : :
5619 [ + - ]: 208 : if (address_sizep != NULL)
5620 [ + + ]: 224 : *address_sizep = listptr_address_size (p);
5621 [ + + ]: 208 : if (offset_sizep != NULL)
5622 [ + - ]: 296 : *offset_sizep = listptr_offset_size (p);
5623 [ + - ]: 208 : if (base != NULL)
5624 : 208 : *base = listptr_base (p);
5625 [ + - ]: 208 : if (cu != NULL)
5626 : 208 : *cu = p->cu;
5627 [ + + ]: 208 : if (attr != NULL)
5628 : 148 : *attr = p->attr;
5629 : :
5630 : : return false;
5631 : : }
5632 : :
5633 : : static Dwarf_Off
5634 : 107692 : next_listptr_offset (struct listptr_table *table, size_t *idxp, Dwarf_Off off)
5635 : : {
5636 : : /* Note that multiple attributes could in theory point to the same loclist
5637 : : offset, so make sure we pick one that is bigger than the current one.
5638 : : The table is sorted on offset. */
5639 [ + - ]: 107692 : if (*idxp < table->n)
5640 : : {
5641 [ + - ]: 214168 : while (++*idxp < table->n)
5642 : : {
5643 : 214168 : Dwarf_Off next = table->table[*idxp].offset;
5644 [ + + ]: 214168 : if (next > off)
5645 : 107692 : return next;
5646 : : }
5647 : : }
5648 : : return 0;
5649 : : }
5650 : :
5651 : : /* Returns the listptr associated with the given index, or NULL. */
5652 : : static struct listptr *
5653 : 1469260 : get_listptr (struct listptr_table *table, size_t idx)
5654 : : {
5655 : 1469260 : if (idx >= table->n)
5656 : : return NULL;
5657 : 1469008 : return &table->table[idx];
5658 : : }
5659 : :
5660 : : /* Returns the next index, base address and CU associated with the
5661 : : list unit offsets. If there is none false is returned, otherwise
5662 : : true. Assumes the table has been sorted. */
5663 : : static bool
5664 : 4940 : listptr_cu (struct listptr_table *table, size_t *idxp,
5665 : : Dwarf_Off start, Dwarf_Off end,
5666 : : Dwarf_Addr *base, struct Dwarf_CU **cu)
5667 : : {
5668 : 4940 : while (*idxp < table->n
5669 [ + - + + ]: 38444 : && table->table[*idxp].offset < start)
5670 : 33504 : ++*idxp;
5671 : :
5672 [ + - ]: 4940 : if (*idxp < table->n
5673 [ + - ]: 4940 : && table->table[*idxp].offset >= start
5674 [ + - ]: 4940 : && table->table[*idxp].offset < end)
5675 : : {
5676 : 4940 : struct listptr *p = &table->table[*idxp];
5677 : 4940 : *base = listptr_base (p);
5678 : 4940 : *cu = p->cu;
5679 : 4940 : return true;
5680 : : }
5681 : :
5682 : : return false;
5683 : : }
5684 : :
5685 : : /* Returns the next index with the current CU for the given attribute.
5686 : : If there is none false is returned, otherwise true. Assumes the
5687 : : table has been sorted. */
5688 : : static bool
5689 : 734436 : listptr_attr (struct listptr_table *table, size_t idxp,
5690 : : Dwarf_Off offset, unsigned int attr)
5691 : : {
5692 : 1469224 : struct listptr *listptr;
5693 : 1469224 : do
5694 : : {
5695 [ + + ]: 1469224 : listptr = get_listptr (table, idxp);
5696 [ + - ]: 1468982 : if (listptr == NULL)
5697 : : return false;
5698 : :
5699 [ + + + + ]: 1468982 : if (listptr->offset == offset && listptr->attr == attr)
5700 : : return true;
5701 : :
5702 : 1361290 : idxp++;
5703 : : }
5704 [ + + ]: 1361290 : while (listptr->offset <= offset);
5705 : :
5706 : : return false;
5707 : : }
5708 : :
5709 : : static void
5710 : 88 : print_debug_abbrev_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
5711 : : Ebl *ebl, GElf_Ehdr *ehdr __attribute__ ((unused)),
5712 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
5713 : : {
5714 : 88 : Elf_Data *elf_data = get_debug_elf_data (dbg, ebl, IDX_debug_abbrev, scn);
5715 [ + - ]: 88 : if (elf_data == NULL)
5716 : : return;
5717 : :
5718 : 88 : const size_t sh_size = elf_data->d_size;
5719 : :
5720 : 88 : printf (_("\nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"
5721 : : " [ Code]\n"),
5722 : : elf_ndxscn (scn), section_name (ebl, shdr),
5723 : 88 : (uint64_t) shdr->sh_offset);
5724 : :
5725 : 88 : Dwarf_Off offset = 0;
5726 [ + + ]: 3622 : while (offset < sh_size)
5727 : : {
5728 : 3534 : printf (_("\nAbbreviation section at offset %" PRIu64 ":\n"),
5729 : : offset);
5730 : :
5731 : 325474 : while (1)
5732 : 160970 : {
5733 : 164504 : size_t length;
5734 : 164504 : Dwarf_Abbrev abbrev;
5735 : :
5736 : 164504 : int res = dwarf_offabbrev (dbg, offset, &length, &abbrev);
5737 [ + + ]: 164504 : if (res != 0)
5738 : : {
5739 [ - + ]: 3534 : if (unlikely (res < 0))
5740 : : {
5741 : 0 : printf (_("\
5742 : : *** error while reading abbreviation: %s\n"),
5743 : : dwarf_errmsg (-1));
5744 : 0 : return;
5745 : : }
5746 : :
5747 : : /* This is the NUL byte at the end of the section. */
5748 : 3534 : ++offset;
5749 : 3534 : break;
5750 : : }
5751 : :
5752 : : /* We know these calls can never fail. */
5753 : 160970 : unsigned int code = dwarf_getabbrevcode (&abbrev);
5754 : 160970 : unsigned int tag = dwarf_getabbrevtag (&abbrev);
5755 : 160970 : int has_children = dwarf_abbrevhaschildren (&abbrev);
5756 : :
5757 [ + + ]: 160970 : printf (_(" [%5u] offset: %" PRId64
5758 : : ", children: %s, tag: %s\n"),
5759 : : code, (int64_t) offset,
5760 : : has_children ? yes_str : no_str,
5761 : : dwarf_tag_name (tag));
5762 : :
5763 : 160970 : size_t cnt = 0;
5764 : 160970 : unsigned int name;
5765 : 160970 : unsigned int form;
5766 : 160970 : Dwarf_Sword data;
5767 : 160970 : Dwarf_Off enoffset;
5768 : 160970 : while (dwarf_getabbrevattr_data (&abbrev, cnt, &name, &form,
5769 [ + + ]: 920466 : &data, &enoffset) == 0)
5770 : : {
5771 : 759496 : printf (" attr: %s, form: %s",
5772 : : dwarf_attr_name (name), dwarf_form_name (form));
5773 [ + + ]: 759496 : if (form == DW_FORM_implicit_const)
5774 : 54576 : printf (" (%" PRId64 ")", data);
5775 : 759496 : printf (", offset: %#" PRIx64 "\n", (uint64_t) enoffset);
5776 : 759496 : ++cnt;
5777 : : }
5778 : :
5779 : 160970 : offset += length;
5780 : : }
5781 : : }
5782 : : }
5783 : :
5784 : :
5785 : : static void
5786 : 8 : print_debug_addr_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
5787 : : Ebl *ebl, GElf_Ehdr *ehdr,
5788 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
5789 : : {
5790 : 8 : Elf_Data *data = get_debug_elf_data (dbg, ebl, IDX_debug_addr, scn);
5791 [ + - ]: 8 : if (data == NULL)
5792 : : return;
5793 : :
5794 : 8 : printf (_("\
5795 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
5796 : : elf_ndxscn (scn), section_name (ebl, shdr),
5797 : 8 : (uint64_t) shdr->sh_offset);
5798 : :
5799 [ + - ]: 8 : if (shdr->sh_size == 0)
5800 : : return;
5801 : :
5802 : 8 : size_t idx = 0;
5803 : 8 : sort_listptr (&known_addrbases, "addr_base");
5804 : :
5805 : 8 : const unsigned char *start = (const unsigned char *) data->d_buf;
5806 : 8 : const unsigned char *readp = start;
5807 : 8 : const unsigned char *readendp = ((const unsigned char *) data->d_buf
5808 : 8 : + data->d_size);
5809 : :
5810 : 8 : while (readp < readendp)
5811 : : {
5812 : : /* We cannot really know whether or not there is an header. The
5813 : : DebugFission extension to DWARF4 doesn't add one. The DWARF5
5814 : : .debug_addr variant does. Whether or not we have an header,
5815 : : DW_AT_[GNU_]addr_base points at "index 0". So if the current
5816 : : offset equals the CU addr_base then we can just start
5817 : : printing addresses. If there is no CU with an exact match
5818 : : then we'll try to parse the header first. */
5819 : 16 : Dwarf_Off off = (Dwarf_Off) (readp
5820 : 16 : - (const unsigned char *) data->d_buf);
5821 : :
5822 : 16 : printf ("Table at offset %" PRIx64 " ", off);
5823 : :
5824 [ + - ]: 16 : struct listptr *listptr = get_listptr (&known_addrbases, idx++);
5825 : 16 : const unsigned char *next_unitp;
5826 : :
5827 : 16 : uint64_t unit_length;
5828 : 16 : uint16_t version;
5829 : 16 : uint8_t address_size;
5830 : 16 : uint8_t segment_size;
5831 [ - + ]: 16 : if (listptr == NULL)
5832 : : {
5833 : 0 : error (0, 0, "Warning: No CU references .debug_addr after %" PRIx64,
5834 : : off);
5835 : :
5836 : : /* We will have to assume it is just addresses to the end... */
5837 [ # # ]: 0 : address_size = ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 4 : 8;
5838 : 0 : next_unitp = readendp;
5839 : 0 : printf ("Unknown CU:\n");
5840 : : }
5841 : : else
5842 : : {
5843 : 16 : Dwarf_Die cudie;
5844 [ - + ]: 16 : if (dwarf_cu_die (listptr->cu, &cudie,
5845 : : NULL, NULL, NULL, NULL,
5846 : : NULL, NULL) == NULL)
5847 : 0 : printf ("Unknown CU (%s):\n", dwarf_errmsg (-1));
5848 : : else
5849 : 16 : printf ("for CU [%6" PRIx64 "]:\n", dwarf_dieoffset (&cudie));
5850 : :
5851 [ + + ]: 16 : if (listptr->offset == off)
5852 : : {
5853 [ - + ]: 4 : address_size = listptr_address_size (listptr);
5854 : 4 : segment_size = 0;
5855 : 4 : version = 4;
5856 : :
5857 : : /* The addresses start here, but where do they end? */
5858 [ + + ]: 4 : listptr = get_listptr (&known_addrbases, idx);
5859 [ - + ]: 2 : if (listptr == NULL)
5860 : : next_unitp = readendp;
5861 [ + - ]: 2 : else if (listptr->cu->version < 5)
5862 : : {
5863 : 2 : next_unitp = start + listptr->offset;
5864 [ + - - + ]: 2 : if (listptr->offset < off || listptr->offset > data->d_size)
5865 : : {
5866 : 0 : error (0, 0,
5867 : : "Warning: Bad address base for next unit at %"
5868 : : PRIx64, off);
5869 : 0 : next_unitp = readendp;
5870 : : }
5871 : : }
5872 : : else
5873 : : {
5874 : : /* Tricky, we don't have a header for this unit, but
5875 : : there is one for the next. We will have to
5876 : : "guess" how big it is and subtract it from the
5877 : : offset (because that points after the header). */
5878 [ # # ]: 0 : unsigned int offset_size = listptr_offset_size (listptr);
5879 : 0 : Dwarf_Off next_off = (listptr->offset
5880 : 0 : - (offset_size == 4 ? 4 : 12) /* len */
5881 : : - 2 /* version */
5882 : : - 1 /* address size */
5883 : 0 : - 1); /* segment selector size */
5884 : 0 : next_unitp = start + next_off;
5885 [ # # # # ]: 0 : if (next_off < off || next_off > data->d_size)
5886 : : {
5887 : 0 : error (0, 0,
5888 : : "Warning: Couldn't calculate .debug_addr "
5889 : : " unit length at %" PRIx64, off);
5890 : 0 : next_unitp = readendp;
5891 : : }
5892 : : }
5893 : 4 : unit_length = (uint64_t) (next_unitp - readp);
5894 : :
5895 : : /* Pretend we have a header. */
5896 : 4 : printf ("\n");
5897 : 4 : printf (_(" Length: %8" PRIu64 "\n"),
5898 : : unit_length);
5899 : 4 : printf (_(" DWARF version: %8" PRIu16 "\n"), version);
5900 : 4 : printf (_(" Address size: %8" PRIu64 "\n"),
5901 : : (uint64_t) address_size);
5902 : 4 : printf (_(" Segment size: %8" PRIu64 "\n"),
5903 : : (uint64_t) segment_size);
5904 : 16 : printf ("\n");
5905 : : }
5906 : : else
5907 : : {
5908 : : /* OK, we have to parse an header first. */
5909 [ - + ]: 12 : unit_length = read_4ubyte_unaligned_inc (dbg, readp);
5910 [ - + ]: 12 : if (unlikely (unit_length == 0xffffffff))
5911 : : {
5912 [ # # ]: 0 : if (unlikely (readp > readendp - 8))
5913 : : {
5914 : 0 : invalid_data:
5915 : 0 : error (0, 0, "Invalid data");
5916 : 0 : return;
5917 : : }
5918 [ # # ]: 0 : unit_length = read_8ubyte_unaligned_inc (dbg, readp);
5919 : : }
5920 : 12 : printf ("\n");
5921 : 12 : printf (_(" Length: %8" PRIu64 "\n"),
5922 : : unit_length);
5923 : :
5924 : : /* We need at least 2-bytes (version) + 1-byte
5925 : : (addr_size) + 1-byte (segment_size) = 4 bytes to
5926 : : complete the header. And this unit cannot go beyond
5927 : : the section data. */
5928 [ + - ]: 12 : if (readp > readendp - 4
5929 [ + - ]: 12 : || unit_length < 4
5930 [ - + ]: 12 : || unit_length > (uint64_t) (readendp - readp))
5931 : 0 : goto invalid_data;
5932 : :
5933 : 12 : next_unitp = readp + unit_length;
5934 : :
5935 [ - + ]: 12 : version = read_2ubyte_unaligned_inc (dbg, readp);
5936 : 12 : printf (_(" DWARF version: %8" PRIu16 "\n"), version);
5937 : :
5938 [ - + ]: 12 : if (version != 5)
5939 : : {
5940 : 0 : error (0, 0, _("Unknown version"));
5941 : 0 : goto next_unit;
5942 : : }
5943 : :
5944 : 12 : address_size = *readp++;
5945 : 12 : printf (_(" Address size: %8" PRIu64 "\n"),
5946 : : (uint64_t) address_size);
5947 : :
5948 [ - + ]: 12 : if (address_size != 4 && address_size != 8)
5949 : : {
5950 : 0 : error (0, 0, _("unsupported address size"));
5951 : 0 : goto next_unit;
5952 : : }
5953 : :
5954 : 12 : segment_size = *readp++;
5955 : 12 : printf (_(" Segment size: %8" PRIu64 "\n"),
5956 : : (uint64_t) segment_size);
5957 : 12 : printf ("\n");
5958 : :
5959 [ - + ]: 12 : if (segment_size != 0)
5960 : : {
5961 : 0 : error (0, 0, _("unsupported segment size"));
5962 : 0 : goto next_unit;
5963 : : }
5964 : :
5965 [ - + ]: 12 : if (listptr->offset != (Dwarf_Off) (readp - start))
5966 : : {
5967 : 0 : error (0, 0, "Address index doesn't start after header");
5968 : 0 : goto next_unit;
5969 : : }
5970 : : }
5971 : : }
5972 : :
5973 : 16 : int digits = 1;
5974 : 16 : size_t addresses = (next_unitp - readp) / address_size;
5975 [ + + ]: 24 : while (addresses >= 10)
5976 : : {
5977 : 8 : ++digits;
5978 : 8 : addresses /= 10;
5979 : : }
5980 : :
5981 : 16 : unsigned int uidx = 0;
5982 : 16 : size_t index_offset = readp - (const unsigned char *) data->d_buf;
5983 : 16 : printf (" Addresses start at offset 0x%zx:\n", index_offset);
5984 : 188 : while (readp <= next_unitp - address_size)
5985 : : {
5986 [ - + + + : 172 : Dwarf_Addr addr = read_addr_unaligned_inc (address_size, dbg,
- + - + ]
5987 : : readp);
5988 : 172 : printf (" [%*u] ", digits, uidx++);
5989 : 172 : print_dwarf_addr (dwflmod, address_size, addr, addr);
5990 [ + + ]: 188 : printf ("\n");
5991 : : }
5992 : 16 : printf ("\n");
5993 : :
5994 [ + - ]: 16 : if (readp != next_unitp)
5995 [ + + ]: 24 : error (0, 0, "extra %zd bytes at end of unit",
5996 : 0 : (size_t) (next_unitp - readp));
5997 : :
5998 : 16 : next_unit:
5999 : : readp = next_unitp;
6000 : : }
6001 : : }
6002 : :
6003 : : /* Print content of DWARF .debug_aranges section. We fortunately do
6004 : : not have to know a bit about the structure of the section, libdwarf
6005 : : takes care of it. */
6006 : : static void
6007 : 2 : print_decoded_aranges_section (Ebl *ebl, GElf_Ehdr *ehdr, Elf_Scn *scn,
6008 : : GElf_Shdr *shdr, Dwarf *dbg)
6009 : : {
6010 : 2 : Dwarf_Aranges *aranges;
6011 : 2 : size_t cnt;
6012 [ - + ]: 2 : if (unlikely (dwarf_getaranges (dbg, &aranges, &cnt) != 0))
6013 : : {
6014 : 0 : error (0, 0, _("cannot get .debug_aranges content: %s"),
6015 : : dwarf_errmsg (-1));
6016 : 0 : return;
6017 : : }
6018 : :
6019 : 2 : GElf_Shdr glink_mem;
6020 : 2 : GElf_Shdr *glink;
6021 : 2 : glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link), &glink_mem);
6022 [ - + ]: 2 : if (glink == NULL)
6023 : : {
6024 : 0 : error (0, 0, _("invalid sh_link value in section %zu"),
6025 : : elf_ndxscn (scn));
6026 : 0 : return;
6027 : : }
6028 : :
6029 : 2 : printf (ngettext ("\
6030 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 " contains %zu entry:\n",
6031 : : "\
6032 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 " contains %zu entries:\n",
6033 : : cnt),
6034 : : elf_ndxscn (scn), section_name (ebl, shdr),
6035 : 2 : (uint64_t) shdr->sh_offset, cnt);
6036 : :
6037 : : /* Compute floor(log16(cnt)). */
6038 : 2 : size_t tmp = cnt;
6039 : 2 : int digits = 1;
6040 [ - + ]: 2 : while (tmp >= 16)
6041 : : {
6042 : 0 : ++digits;
6043 : 0 : tmp >>= 4;
6044 : : }
6045 : :
6046 [ + + ]: 12 : for (size_t n = 0; n < cnt; ++n)
6047 : : {
6048 : 10 : Dwarf_Arange *runp = dwarf_onearange (aranges, n);
6049 [ - + ]: 10 : if (unlikely (runp == NULL))
6050 : : {
6051 : 0 : printf ("cannot get arange %zu: %s\n", n, dwarf_errmsg (-1));
6052 : 0 : return;
6053 : : }
6054 : :
6055 : 10 : Dwarf_Addr start;
6056 : 10 : Dwarf_Word length;
6057 : 10 : Dwarf_Off offset;
6058 : :
6059 [ - + ]: 10 : if (unlikely (dwarf_getarangeinfo (runp, &start, &length, &offset) != 0))
6060 : 0 : printf (_(" [%*zu] ???\n"), digits, n);
6061 : : else
6062 : 10 : printf (_(" [%*zu] start: %0#*" PRIx64
6063 : : ", length: %5" PRIu64 ", CU DIE offset: %6"
6064 : : PRId64 "\n"),
6065 [ - + ]: 10 : digits, n, ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 10 : 18,
6066 : : (uint64_t) start, (uint64_t) length, (int64_t) offset);
6067 : : }
6068 : : }
6069 : :
6070 : :
6071 : : /* Print content of DWARF .debug_aranges section. */
6072 : : static void
6073 : 92 : print_debug_aranges_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
6074 : : Ebl *ebl, GElf_Ehdr *ehdr, Elf_Scn *scn,
6075 : : GElf_Shdr *shdr, Dwarf *dbg)
6076 : : {
6077 [ + + ]: 92 : if (decodedaranges)
6078 : : {
6079 : 2 : print_decoded_aranges_section (ebl, ehdr, scn, shdr, dbg);
6080 : 2 : return;
6081 : : }
6082 : :
6083 : 90 : Elf_Data *data = get_debug_elf_data (dbg, ebl, IDX_debug_aranges, scn);
6084 [ + - ]: 90 : if (data == NULL)
6085 : : return;
6086 : :
6087 : 90 : printf (_("\
6088 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
6089 : : elf_ndxscn (scn), section_name (ebl, shdr),
6090 : 90 : (uint64_t) shdr->sh_offset);
6091 : :
6092 : 90 : const unsigned char *readp = data->d_buf;
6093 : 90 : const unsigned char *readendp = readp + data->d_size;
6094 : :
6095 [ + + ]: 3606 : while (readp < readendp)
6096 : : {
6097 : 3516 : const unsigned char *hdrstart = readp;
6098 : 3516 : size_t start_offset = hdrstart - (const unsigned char *) data->d_buf;
6099 : :
6100 : 3516 : printf (_("\nTable at offset %zu:\n"), start_offset);
6101 [ - + ]: 3516 : if (readp + 4 > readendp)
6102 : : {
6103 : 0 : invalid_data:
6104 : 0 : error (0, 0, _("invalid data in section [%zu] '%s'"),
6105 : : elf_ndxscn (scn), section_name (ebl, shdr));
6106 : 0 : return;
6107 : : }
6108 : :
6109 [ + + ]: 3516 : Dwarf_Word length = read_4ubyte_unaligned_inc (dbg, readp);
6110 : 3516 : unsigned int length_bytes = 4;
6111 [ - + ]: 3516 : if (length == DWARF3_LENGTH_64_BIT)
6112 : : {
6113 [ # # ]: 0 : if (readp + 8 > readendp)
6114 : 0 : goto invalid_data;
6115 [ # # ]: 0 : length = read_8ubyte_unaligned_inc (dbg, readp);
6116 : 0 : length_bytes = 8;
6117 : : }
6118 : :
6119 : 3516 : const unsigned char *nexthdr = readp + length;
6120 : 3516 : printf (_("\n Length: %6" PRIu64 "\n"),
6121 : : (uint64_t) length);
6122 : :
6123 [ - + ]: 3516 : if (unlikely (length > (size_t) (readendp - readp)))
6124 : 0 : goto invalid_data;
6125 : :
6126 [ - + ]: 3516 : if (length == 0)
6127 : 0 : continue;
6128 : :
6129 [ - + ]: 3516 : if (readp + 2 > readendp)
6130 : 0 : goto invalid_data;
6131 [ + + ]: 3516 : uint_fast16_t version = read_2ubyte_unaligned_inc (dbg, readp);
6132 : 3516 : printf (_(" DWARF version: %6" PRIuFAST16 "\n"),
6133 : : version);
6134 [ - + ]: 3516 : if (version != 2)
6135 : : {
6136 : 0 : error (0, 0, _("unsupported aranges version"));
6137 : 0 : goto next_table;
6138 : : }
6139 : :
6140 : 3516 : Dwarf_Word offset;
6141 [ - + ]: 3516 : if (readp + length_bytes > readendp)
6142 : 0 : goto invalid_data;
6143 [ - + ]: 3516 : if (length_bytes == 8)
6144 [ # # ]: 0 : offset = read_8ubyte_unaligned_inc (dbg, readp);
6145 : : else
6146 [ + + ]: 3516 : offset = read_4ubyte_unaligned_inc (dbg, readp);
6147 : 3516 : printf (_(" CU offset: %6" PRIx64 "\n"),
6148 : : (uint64_t) offset);
6149 : :
6150 [ - + ]: 3516 : if (readp + 1 > readendp)
6151 : 0 : goto invalid_data;
6152 : 3516 : unsigned int address_size = *readp++;
6153 : 3516 : printf (_(" Address size: %6" PRIu64 "\n"),
6154 : : (uint64_t) address_size);
6155 [ - + ]: 3516 : if (address_size != 4 && address_size != 8)
6156 : : {
6157 : 0 : error (0, 0, _("unsupported address size"));
6158 : 0 : goto next_table;
6159 : : }
6160 : :
6161 [ - + ]: 3516 : if (readp + 1 > readendp)
6162 : 0 : goto invalid_data;
6163 : 3516 : unsigned int segment_size = *readp++;
6164 : 3516 : printf (_(" Segment size: %6" PRIu64 "\n\n"),
6165 : : (uint64_t) segment_size);
6166 [ - + - - ]: 3516 : if (segment_size != 0 && segment_size != 4 && segment_size != 8)
6167 : : {
6168 : 0 : error (0, 0, _("unsupported segment size"));
6169 : 0 : goto next_table;
6170 : : }
6171 : :
6172 : : /* Round the address to the next multiple of 2*address_size. */
6173 : 3516 : readp += ((2 * address_size - ((readp - hdrstart) % (2 * address_size)))
6174 : 3516 : % (2 * address_size));
6175 : :
6176 : 3516 : while (readp < nexthdr)
6177 : : {
6178 : 7306 : Dwarf_Word range_address;
6179 : 7306 : Dwarf_Word range_length;
6180 : 7306 : Dwarf_Word segment = 0;
6181 [ - + ]: 7306 : if (readp + 2 * address_size + segment_size > readendp)
6182 : 0 : goto invalid_data;
6183 [ + + ]: 7306 : if (address_size == 4)
6184 : : {
6185 [ + + ]: 92 : range_address = read_4ubyte_unaligned_inc (dbg, readp);
6186 [ + + ]: 92 : range_length = read_4ubyte_unaligned_inc (dbg, readp);
6187 : : }
6188 : : else
6189 : : {
6190 [ + + ]: 7214 : range_address = read_8ubyte_unaligned_inc (dbg, readp);
6191 [ + + ]: 7214 : range_length = read_8ubyte_unaligned_inc (dbg, readp);
6192 : : }
6193 : :
6194 [ - + ]: 7306 : if (segment_size == 4)
6195 [ # # ]: 0 : segment = read_4ubyte_unaligned_inc (dbg, readp);
6196 [ - + ]: 7306 : else if (segment_size == 8)
6197 [ # # ]: 0 : segment = read_8ubyte_unaligned_inc (dbg, readp);
6198 : :
6199 [ + + ]: 7306 : if (range_address == 0 && range_length == 0 && segment == 0)
6200 : : break;
6201 : :
6202 : 3790 : printf (" ");
6203 : 3790 : print_dwarf_addr (dwflmod, address_size, range_address,
6204 : : range_address);
6205 : 3790 : printf ("..");
6206 : 3790 : print_dwarf_addr (dwflmod, address_size,
6207 : 3790 : range_address + range_length - 1,
6208 : : range_length);
6209 [ - + ]: 3790 : if (segment_size != 0)
6210 : 0 : printf (" (%" PRIx64 ")\n", (uint64_t) segment);
6211 : : else
6212 [ + - ]: 11096 : printf ("\n");
6213 : : }
6214 : :
6215 : 3516 : next_table:
6216 [ - + ]: 3516 : if (readp != nexthdr)
6217 : : {
6218 : 0 : size_t padding = nexthdr - readp;
6219 : 0 : printf (_(" %zu padding bytes\n"), padding);
6220 : 0 : readp = nexthdr;
6221 : : }
6222 : : }
6223 : : }
6224 : :
6225 : :
6226 : : static bool is_split_dwarf (Dwarf *dbg, uint64_t *id, Dwarf_CU **split_cu);
6227 : :
6228 : : /* Returns true and sets cu and cu_base if the given Dwarf is a split
6229 : : DWARF (.dwo) file. */
6230 : : static bool
6231 : 0 : split_dwarf_cu_base (Dwarf *dbg, Dwarf_CU **cu, Dwarf_Addr *cu_base)
6232 : : {
6233 : 0 : uint64_t id;
6234 [ # # ]: 0 : if (is_split_dwarf (dbg, &id, cu))
6235 : : {
6236 : 0 : Dwarf_Die cudie;
6237 [ # # ]: 0 : if (dwarf_cu_info (*cu, NULL, NULL, &cudie, NULL, NULL, NULL, NULL) == 0)
6238 : : {
6239 : 0 : *cu_base = cudie_base (&cudie);
6240 : 0 : return true;
6241 : : }
6242 : : }
6243 : : return false;
6244 : : }
6245 : :
6246 : : /* Print content of DWARF .debug_rnglists section. */
6247 : : static void
6248 : 38 : print_debug_rnglists_section (Dwfl_Module *dwflmod,
6249 : : Ebl *ebl,
6250 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
6251 : : Elf_Scn *scn, GElf_Shdr *shdr,
6252 : : Dwarf *dbg __attribute__((unused)))
6253 : : {
6254 : 38 : Elf_Data *data = get_debug_elf_data (dbg, ebl, IDX_debug_rnglists, scn);
6255 [ + - ]: 38 : if (data == NULL)
6256 : 0 : return;
6257 : :
6258 : 38 : printf (_("\
6259 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
6260 : : elf_ndxscn (scn), section_name (ebl, shdr),
6261 : 38 : (uint64_t) shdr->sh_offset);
6262 : :
6263 : : /* For the listptr to get the base address/CU. */
6264 : 38 : sort_listptr (&known_rnglistptr, "rnglistptr");
6265 : 38 : size_t listptr_idx = 0;
6266 : :
6267 : 38 : const unsigned char *readp = data->d_buf;
6268 : 38 : const unsigned char *const dataend = ((unsigned char *) data->d_buf
6269 : 38 : + data->d_size);
6270 [ + + ]: 1946 : while (readp < dataend)
6271 : : {
6272 [ - + ]: 1908 : if (unlikely (readp > dataend - 4))
6273 : : {
6274 : 0 : invalid_data:
6275 : 0 : error (0, 0, _("invalid data in section [%zu] '%s'"),
6276 : : elf_ndxscn (scn), section_name (ebl, shdr));
6277 : 0 : return;
6278 : : }
6279 : :
6280 : 1908 : ptrdiff_t offset = readp - (unsigned char *) data->d_buf;
6281 : 1908 : printf (_("Table at Offset 0x%" PRIx64 ":\n\n"),
6282 : : (uint64_t) offset);
6283 : :
6284 [ - + ]: 1908 : uint64_t unit_length = read_4ubyte_unaligned_inc (dbg, readp);
6285 : 1908 : unsigned int offset_size = 4;
6286 [ - + ]: 1908 : if (unlikely (unit_length == 0xffffffff))
6287 : : {
6288 [ # # ]: 0 : if (unlikely (readp > dataend - 8))
6289 : 0 : goto invalid_data;
6290 : :
6291 [ # # ]: 0 : unit_length = read_8ubyte_unaligned_inc (dbg, readp);
6292 : 0 : offset_size = 8;
6293 : : }
6294 : 1908 : printf (_(" Length: %8" PRIu64 "\n"), unit_length);
6295 : :
6296 : : /* We need at least 2-bytes + 1-byte + 1-byte + 4-bytes = 8
6297 : : bytes to complete the header. And this unit cannot go beyond
6298 : : the section data. */
6299 [ + - ]: 1908 : if (readp > dataend - 8
6300 [ + - ]: 1908 : || unit_length < 8
6301 [ - + ]: 1908 : || unit_length > (uint64_t) (dataend - readp))
6302 : 0 : goto invalid_data;
6303 : :
6304 : 1908 : const unsigned char *nexthdr = readp + unit_length;
6305 : :
6306 [ - + ]: 1908 : uint16_t version = read_2ubyte_unaligned_inc (dbg, readp);
6307 : 1908 : printf (_(" DWARF version: %8" PRIu16 "\n"), version);
6308 : :
6309 [ - + ]: 1908 : if (version != 5)
6310 : : {
6311 : 0 : error (0, 0, _("Unknown version"));
6312 : 0 : goto next_table;
6313 : : }
6314 : :
6315 : 1908 : uint8_t address_size = *readp++;
6316 : 1908 : printf (_(" Address size: %8" PRIu64 "\n"),
6317 : : (uint64_t) address_size);
6318 : :
6319 [ - + ]: 1908 : if (address_size != 4 && address_size != 8)
6320 : : {
6321 : 0 : error (0, 0, _("unsupported address size"));
6322 : 0 : goto next_table;
6323 : : }
6324 : :
6325 : 1908 : uint8_t segment_size = *readp++;
6326 : 1908 : printf (_(" Segment size: %8" PRIu64 "\n"),
6327 : : (uint64_t) segment_size);
6328 : :
6329 [ - + - - ]: 1908 : if (segment_size != 0 && segment_size != 4 && segment_size != 8)
6330 : : {
6331 : 0 : error (0, 0, _("unsupported segment size"));
6332 : 0 : goto next_table;
6333 : : }
6334 : :
6335 [ - + ]: 1908 : uint32_t offset_entry_count = read_4ubyte_unaligned_inc (dbg, readp);
6336 : 1908 : printf (_(" Offset entries: %8" PRIu64 "\n"),
6337 : : (uint64_t) offset_entry_count);
6338 : :
6339 : : /* We need the CU that uses this unit to get the initial base address. */
6340 : 1908 : Dwarf_Addr cu_base = 0;
6341 : 1908 : struct Dwarf_CU *cu = NULL;
6342 [ - + ]: 1908 : if (listptr_cu (&known_rnglistptr, &listptr_idx,
6343 : : (Dwarf_Off) offset,
6344 : 1908 : (Dwarf_Off) (nexthdr - (unsigned char *) data->d_buf),
6345 : : &cu_base, &cu)
6346 [ # # ]: 0 : || split_dwarf_cu_base (dbg, &cu, &cu_base))
6347 : 1908 : {
6348 : 1908 : Dwarf_Die cudie;
6349 [ - + ]: 1908 : if (dwarf_cu_die (cu, &cudie,
6350 : : NULL, NULL, NULL, NULL,
6351 : : NULL, NULL) == NULL)
6352 : 0 : printf (_(" Unknown CU base: "));
6353 : : else
6354 : 1908 : printf (_(" CU [%6" PRIx64 "] base: "),
6355 : : dwarf_dieoffset (&cudie));
6356 : 1908 : print_dwarf_addr (dwflmod, address_size, cu_base, cu_base);
6357 : 1908 : printf ("\n");
6358 : : }
6359 : : else
6360 : 0 : printf (_(" Not associated with a CU.\n"));
6361 : :
6362 : 1908 : printf ("\n");
6363 : :
6364 : 1908 : const unsigned char *offset_array_start = readp;
6365 [ + + ]: 1908 : if (offset_entry_count > 0)
6366 : : {
6367 : 2 : uint64_t max_entries = (unit_length - 8) / offset_size;
6368 [ - + ]: 2 : if (offset_entry_count > max_entries)
6369 : : {
6370 : 0 : error (0, 0,
6371 : 0 : _("too many offset entries for unit length"));
6372 : 0 : offset_entry_count = max_entries;
6373 : : }
6374 : :
6375 : 2 : printf (_(" Offsets starting at 0x%" PRIx64 ":\n"),
6376 : : (uint64_t) (offset_array_start
6377 : 2 : - (unsigned char *) data->d_buf));
6378 [ + + ]: 6 : for (uint32_t idx = 0; idx < offset_entry_count; idx++)
6379 : : {
6380 : 4 : printf (" [%6" PRIu32 "] ", idx);
6381 [ + - ]: 4 : if (offset_size == 4)
6382 : : {
6383 [ - + ]: 4 : uint32_t off = read_4ubyte_unaligned_inc (dbg, readp);
6384 : 4 : printf ("0x%" PRIx32 "\n", off);
6385 : : }
6386 : : else
6387 : : {
6388 [ # # ]: 0 : uint64_t off = read_8ubyte_unaligned_inc (dbg, readp);
6389 : 4 : printf ("0x%" PRIx64 "\n", off);
6390 : : }
6391 : : }
6392 : 2 : printf ("\n");
6393 : : }
6394 : :
6395 : 1908 : Dwarf_Addr base = cu_base;
6396 : 1908 : bool start_of_list = true;
6397 : 1908 : while (readp < nexthdr)
6398 : : {
6399 : 132876 : uint8_t kind = *readp++;
6400 : 132876 : uint64_t op1, op2;
6401 : :
6402 : : /* Skip padding. */
6403 [ - + ]: 132876 : if (start_of_list && kind == DW_RLE_end_of_list)
6404 : 0 : continue;
6405 : :
6406 [ + + ]: 132876 : if (start_of_list)
6407 : : {
6408 : 27712 : base = cu_base;
6409 : 27712 : printf (" Offset: %" PRIx64 ", Index: %" PRIx64 "\n",
6410 : 27712 : (uint64_t) (readp - (unsigned char *) data->d_buf - 1),
6411 : 27712 : (uint64_t) (readp - offset_array_start - 1));
6412 : 27712 : start_of_list = false;
6413 : : }
6414 : :
6415 : 132876 : printf (" %s", dwarf_range_list_encoding_name (kind));
6416 [ + - - - : 132876 : switch (kind)
+ + - +
- ]
6417 : : {
6418 : 27712 : case DW_RLE_end_of_list:
6419 : 27712 : start_of_list = true;
6420 : 27712 : printf ("\n\n");
6421 : : break;
6422 : :
6423 : 0 : case DW_RLE_base_addressx:
6424 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
6425 : : {
6426 : 0 : invalid_range:
6427 : 0 : error (0, 0, _("invalid range list data"));
6428 : 0 : goto next_table;
6429 : : }
6430 : 0 : get_uleb128 (op1, readp, nexthdr);
6431 : 0 : printf (" %" PRIx64 "\n", op1);
6432 [ # # ]: 0 : if (! print_unresolved_addresses)
6433 : : {
6434 : 0 : Dwarf_Addr addr;
6435 [ # # ]: 0 : if (get_indexed_addr (cu, op1, &addr) != 0)
6436 : 0 : printf (" ???\n");
6437 : : else
6438 : : {
6439 : 0 : printf (" ");
6440 : 0 : print_dwarf_addr (dwflmod, address_size, addr, addr);
6441 : 0 : printf ("\n");
6442 : : }
6443 : : }
6444 : : break;
6445 : :
6446 : 0 : case DW_RLE_startx_endx:
6447 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
6448 : 0 : goto invalid_range;
6449 : 0 : get_uleb128 (op1, readp, nexthdr);
6450 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
6451 : 0 : goto invalid_range;
6452 : 0 : get_uleb128 (op2, readp, nexthdr);
6453 : 0 : printf (" %" PRIx64 ", %" PRIx64 "\n", op1, op2);
6454 [ # # ]: 0 : if (! print_unresolved_addresses)
6455 : : {
6456 : 0 : Dwarf_Addr addr1;
6457 : 0 : Dwarf_Addr addr2;
6458 [ # # ]: 0 : if (get_indexed_addr (cu, op1, &addr1) != 0
6459 [ # # ]: 0 : || get_indexed_addr (cu, op2, &addr2) != 0)
6460 : : {
6461 : 0 : printf (" ???..\n");
6462 : 0 : printf (" ???\n");
6463 : : }
6464 : : else
6465 : : {
6466 : 0 : printf (" ");
6467 : 0 : print_dwarf_addr (dwflmod, address_size, addr1, addr1);
6468 : 0 : printf ("..\n ");
6469 : 0 : print_dwarf_addr (dwflmod, address_size,
6470 : : addr2 - 1, addr2);
6471 : 0 : printf ("\n");
6472 : : }
6473 : : }
6474 : : break;
6475 : :
6476 : 0 : case DW_RLE_startx_length:
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 : : {
6490 : 0 : printf (" ???..\n");
6491 : 0 : printf (" ???\n");
6492 : : }
6493 : : else
6494 : : {
6495 : 0 : addr2 = addr1 + op2;
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 : 95786 : case DW_RLE_offset_pair:
6507 [ - + ]: 95786 : if ((uint64_t) (nexthdr - readp) < 1)
6508 : 0 : goto invalid_range;
6509 : 95786 : get_uleb128 (op1, readp, nexthdr);
6510 [ - + ]: 95786 : if ((uint64_t) (nexthdr - readp) < 1)
6511 : 0 : goto invalid_range;
6512 : 95786 : get_uleb128 (op2, readp, nexthdr);
6513 : 95786 : printf (" %" PRIx64 ", %" PRIx64 "\n", op1, op2);
6514 [ + - ]: 95786 : if (! print_unresolved_addresses)
6515 : : {
6516 : 95786 : op1 += base;
6517 : 95786 : op2 += base;
6518 : 95786 : printf (" ");
6519 : 95786 : print_dwarf_addr (dwflmod, address_size, op1, op1);
6520 : 95786 : printf ("..\n ");
6521 : 95786 : print_dwarf_addr (dwflmod, address_size, op2 - 1, op2);
6522 : 95786 : printf ("\n");
6523 : : }
6524 : : break;
6525 : :
6526 : 8464 : case DW_RLE_base_address:
6527 [ - + ]: 8464 : if (address_size == 4)
6528 : : {
6529 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 4)
6530 : 0 : goto invalid_range;
6531 [ # # ]: 0 : op1 = read_4ubyte_unaligned_inc (dbg, readp);
6532 : : }
6533 : : else
6534 : : {
6535 [ - + ]: 8464 : if ((uint64_t) (nexthdr - readp) < 8)
6536 : 0 : goto invalid_range;
6537 [ - + ]: 8464 : op1 = read_8ubyte_unaligned_inc (dbg, readp);
6538 : : }
6539 : 8464 : base = op1;
6540 : 8464 : printf (" 0x%" PRIx64 "\n", base);
6541 [ - + ]: 8464 : if (! print_unresolved_addresses)
6542 : : {
6543 : 8464 : printf (" ");
6544 : 8464 : print_dwarf_addr (dwflmod, address_size, base, base);
6545 : 8464 : printf ("\n");
6546 : : }
6547 : : break;
6548 : :
6549 : 0 : case DW_RLE_start_end:
6550 [ # # ]: 0 : if (address_size == 4)
6551 : : {
6552 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 8)
6553 : 0 : goto invalid_range;
6554 [ # # ]: 0 : op1 = read_4ubyte_unaligned_inc (dbg, readp);
6555 [ # # ]: 0 : op2 = read_4ubyte_unaligned_inc (dbg, readp);
6556 : : }
6557 : : else
6558 : : {
6559 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 16)
6560 : 0 : goto invalid_range;
6561 [ # # ]: 0 : op1 = read_8ubyte_unaligned_inc (dbg, readp);
6562 [ # # ]: 0 : op2 = read_8ubyte_unaligned_inc (dbg, readp);
6563 : : }
6564 : 0 : printf (" 0x%" PRIx64 "..0x%" PRIx64 "\n", op1, op2);
6565 [ # # ]: 0 : if (! print_unresolved_addresses)
6566 : : {
6567 : 0 : printf (" ");
6568 : 0 : print_dwarf_addr (dwflmod, address_size, op1, op1);
6569 : 0 : printf ("..\n ");
6570 : 0 : print_dwarf_addr (dwflmod, address_size, op2 - 1, op2);
6571 : 0 : printf ("\n");
6572 : : }
6573 : : break;
6574 : :
6575 : 914 : case DW_RLE_start_length:
6576 [ - + ]: 914 : if (address_size == 4)
6577 : : {
6578 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 4)
6579 : 0 : goto invalid_range;
6580 [ # # ]: 0 : op1 = read_4ubyte_unaligned_inc (dbg, readp);
6581 : : }
6582 : : else
6583 : : {
6584 [ - + ]: 914 : if ((uint64_t) (nexthdr - readp) < 8)
6585 : 0 : goto invalid_range;
6586 [ - + ]: 914 : op1 = read_8ubyte_unaligned_inc (dbg, readp);
6587 : : }
6588 [ - + ]: 914 : if ((uint64_t) (nexthdr - readp) < 1)
6589 : 0 : goto invalid_range;
6590 : 914 : get_uleb128 (op2, readp, nexthdr);
6591 : 914 : printf (" 0x%" PRIx64 ", %" PRIx64 "\n", op1, op2);
6592 [ + + ]: 914 : if (! print_unresolved_addresses)
6593 : : {
6594 : 912 : op2 = op1 + op2;
6595 : 912 : printf (" ");
6596 : 912 : print_dwarf_addr (dwflmod, address_size, op1, op1);
6597 : 912 : printf ("..\n ");
6598 : 912 : print_dwarf_addr (dwflmod, address_size, op2 - 1, op2);
6599 [ + + ]: 135696 : printf ("\n");
6600 : : }
6601 : : break;
6602 : :
6603 : 0 : default:
6604 : 0 : goto invalid_range;
6605 : : }
6606 : : }
6607 : :
6608 : 1908 : next_table:
6609 [ - + ]: 1908 : if (readp != nexthdr)
6610 : : {
6611 : 0 : size_t padding = nexthdr - readp;
6612 : 0 : printf (_(" %zu padding bytes\n\n"), padding);
6613 : 0 : readp = nexthdr;
6614 : : }
6615 : : }
6616 : : }
6617 : :
6618 : : /* Print content of DWARF .debug_ranges section. */
6619 : : static void
6620 : 32 : print_debug_ranges_section (Dwfl_Module *dwflmod,
6621 : : Ebl *ebl, GElf_Ehdr *ehdr,
6622 : : Elf_Scn *scn, GElf_Shdr *shdr,
6623 : : Dwarf *dbg)
6624 : : {
6625 : 32 : Elf_Data *data = get_debug_elf_data (dbg, ebl, IDX_debug_ranges, scn);
6626 [ - + ]: 32 : if (data == NULL)
6627 : 0 : return;
6628 : :
6629 : 32 : printf (_("\
6630 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
6631 : : elf_ndxscn (scn), section_name (ebl, shdr),
6632 : 32 : (uint64_t) shdr->sh_offset);
6633 : :
6634 : 32 : sort_listptr (&known_rangelistptr, "rangelistptr");
6635 : 32 : size_t listptr_idx = 0;
6636 : :
6637 [ + + ]: 32 : uint_fast8_t address_size = ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 4 : 8;
6638 : :
6639 : 32 : bool first = true;
6640 : 32 : Dwarf_Addr base = 0;
6641 : 32 : unsigned char *const endp = (unsigned char *) data->d_buf + data->d_size;
6642 : 32 : unsigned char *readp = data->d_buf;
6643 : 32 : Dwarf_CU *last_cu = NULL;
6644 [ + + ]: 184 : while (readp < endp)
6645 : : {
6646 : 152 : ptrdiff_t offset = readp - (unsigned char *) data->d_buf;
6647 : 152 : Dwarf_CU *cu = last_cu;
6648 : :
6649 [ + + - + ]: 152 : if (first && skip_listptr_hole (&known_rangelistptr, &listptr_idx,
6650 : : &address_size, NULL, &base, &cu,
6651 : : offset, &readp, endp, NULL))
6652 : 0 : continue;
6653 : :
6654 [ + + ]: 152 : if (last_cu != cu)
6655 : : {
6656 : 58 : Dwarf_Die cudie;
6657 [ - + ]: 58 : if (dwarf_cu_die (cu, &cudie,
6658 : : NULL, NULL, NULL, NULL,
6659 : : NULL, NULL) == NULL)
6660 : 0 : printf (_("\n Unknown CU base: "));
6661 : : else
6662 : 58 : printf (_("\n CU [%6" PRIx64 "] base: "),
6663 : : dwarf_dieoffset (&cudie));
6664 : 58 : print_dwarf_addr (dwflmod, address_size, base, base);
6665 : 58 : printf ("\n");
6666 : : }
6667 : 152 : last_cu = cu;
6668 : :
6669 [ - + ]: 152 : if (unlikely (data->d_size - offset < (size_t) address_size * 2))
6670 : : {
6671 : 0 : printf (_(" [%6tx] <INVALID DATA>\n"), offset);
6672 : 0 : break;
6673 : : }
6674 : :
6675 : 152 : Dwarf_Addr begin;
6676 : 152 : Dwarf_Addr end;
6677 [ + + ]: 152 : if (address_size == 8)
6678 : : {
6679 [ - + ]: 120 : begin = read_8ubyte_unaligned_inc (dbg, readp);
6680 [ - + ]: 120 : end = read_8ubyte_unaligned_inc (dbg, readp);
6681 : : }
6682 : : else
6683 : : {
6684 [ - + ]: 32 : begin = read_4ubyte_unaligned_inc (dbg, readp);
6685 [ - + ]: 32 : end = read_4ubyte_unaligned_inc (dbg, readp);
6686 [ + - ]: 32 : if (begin == (Dwarf_Addr) (uint32_t) -1)
6687 : : begin = (Dwarf_Addr) -1l;
6688 : : }
6689 : :
6690 [ - + ]: 152 : if (begin == (Dwarf_Addr) -1l) /* Base address entry. */
6691 : : {
6692 [ # # ]: 0 : if (first)
6693 : 0 : printf (" [%6tx] ", offset);
6694 : : else
6695 : 0 : printf (" ");
6696 : 0 : puts (_("base address"));
6697 : 0 : printf (" ");
6698 : 0 : print_dwarf_addr (dwflmod, address_size, end, end);
6699 : 0 : printf ("\n");
6700 : 0 : base = end;
6701 : 0 : first = false;
6702 : : }
6703 [ + + ]: 152 : else if (begin == 0 && end == 0) /* End of list entry. */
6704 : : {
6705 [ - + ]: 60 : if (first)
6706 : 0 : printf (_(" [%6tx] empty list\n"), offset);
6707 : : first = true;
6708 : : }
6709 : : else
6710 : : {
6711 : : /* We have an address range entry. */
6712 [ + + ]: 92 : if (first) /* First address range entry in a list. */
6713 : 60 : printf (" [%6tx] ", offset);
6714 : : else
6715 : 32 : printf (" ");
6716 : :
6717 : 92 : printf ("range %" PRIx64 ", %" PRIx64 "\n", begin, end);
6718 [ + + ]: 92 : if (! print_unresolved_addresses)
6719 : : {
6720 : 72 : printf (" ");
6721 : 72 : print_dwarf_addr (dwflmod, address_size, base + begin,
6722 : : base + begin);
6723 : 72 : printf ("..\n ");
6724 : 72 : print_dwarf_addr (dwflmod, address_size,
6725 : : base + end - 1, base + end);
6726 : 224 : printf ("\n");
6727 : : }
6728 : :
6729 : : first = false;
6730 : : }
6731 : : }
6732 : : }
6733 : :
6734 : : #define REGNAMESZ 16
6735 : : static const char *
6736 : 33118 : register_info (Ebl *ebl, unsigned int regno, const Ebl_Register_Location *loc,
6737 : : char name[REGNAMESZ], int *bits, int *type)
6738 : : {
6739 : 33118 : const char *set;
6740 : 33118 : const char *pfx;
6741 : 33118 : int ignore;
6742 [ + + + + ]: 95962 : ssize_t n = ebl_register_info (ebl, regno, name, REGNAMESZ, &pfx, &set,
6743 : : bits ?: &ignore, type ?: &ignore);
6744 [ - + ]: 33118 : if (n <= 0)
6745 : : {
6746 [ # # ]: 0 : if (loc != NULL)
6747 : 0 : snprintf (name, REGNAMESZ, "reg%u", loc->regno);
6748 : : else
6749 : 0 : snprintf (name, REGNAMESZ, "??? 0x%x", regno);
6750 [ # # ]: 0 : if (bits != NULL)
6751 [ # # ]: 0 : *bits = loc != NULL ? loc->bits : 0;
6752 [ # # ]: 0 : if (type != NULL)
6753 : 0 : *type = DW_ATE_unsigned;
6754 : 0 : set = "??? unrecognized";
6755 : : }
6756 : : else
6757 : : {
6758 [ + + - + ]: 33118 : if (bits != NULL && *bits <= 0)
6759 [ # # ]: 0 : *bits = loc != NULL ? loc->bits : 0;
6760 [ + + - + ]: 33118 : if (type != NULL && *type == DW_ATE_void)
6761 : 0 : *type = DW_ATE_unsigned;
6762 : :
6763 : : }
6764 : 33118 : return set;
6765 : : }
6766 : :
6767 : : static const unsigned char *
6768 : 280 : read_encoded (unsigned int encoding, const unsigned char *readp,
6769 : : const unsigned char *const endp, uint64_t *res, Dwarf *dbg)
6770 : : {
6771 [ - + ]: 280 : if ((encoding & 0xf) == DW_EH_PE_absptr)
6772 : 0 : encoding = gelf_getclass (dbg->elf) == ELFCLASS32
6773 [ # # ]: 0 : ? DW_EH_PE_udata4 : DW_EH_PE_udata8;
6774 : :
6775 [ - - - + : 280 : switch (encoding & 0xf)
- - + -
- ]
6776 : : {
6777 : 0 : case DW_EH_PE_uleb128:
6778 [ # # ]: 0 : if (readp >= endp)
6779 : 0 : goto invalid;
6780 : 0 : get_uleb128 (*res, readp, endp);
6781 : 0 : break;
6782 : 0 : case DW_EH_PE_sleb128:
6783 [ # # ]: 0 : if (readp >= endp)
6784 : 0 : goto invalid;
6785 : 0 : get_sleb128 (*res, readp, endp);
6786 : 0 : break;
6787 : 0 : case DW_EH_PE_udata2:
6788 [ # # ]: 0 : if (readp + 2 > endp)
6789 : 0 : goto invalid;
6790 [ # # ]: 0 : *res = read_2ubyte_unaligned_inc (dbg, readp);
6791 : 0 : break;
6792 : 140 : case DW_EH_PE_udata4:
6793 [ - + ]: 140 : if (readp + 4 > endp)
6794 : 0 : goto invalid;
6795 [ - + ]: 140 : *res = read_4ubyte_unaligned_inc (dbg, readp);
6796 : 140 : break;
6797 : 0 : case DW_EH_PE_udata8:
6798 [ # # ]: 0 : if (readp + 8 > endp)
6799 : 0 : goto invalid;
6800 [ # # ]: 0 : *res = read_8ubyte_unaligned_inc (dbg, readp);
6801 : 0 : break;
6802 : 0 : case DW_EH_PE_sdata2:
6803 [ # # ]: 0 : if (readp + 2 > endp)
6804 : 0 : goto invalid;
6805 [ # # ]: 0 : *res = read_2sbyte_unaligned_inc (dbg, readp);
6806 : 0 : break;
6807 : 140 : case DW_EH_PE_sdata4:
6808 [ - + ]: 140 : if (readp + 4 > endp)
6809 : 0 : goto invalid;
6810 [ - + ]: 140 : *res = read_4sbyte_unaligned_inc (dbg, readp);
6811 : 140 : break;
6812 : 0 : case DW_EH_PE_sdata8:
6813 [ # # ]: 0 : if (readp + 8 > endp)
6814 : 0 : goto invalid;
6815 [ # # ]: 0 : *res = read_8sbyte_unaligned_inc (dbg, readp);
6816 : 0 : break;
6817 : : default:
6818 : 0 : invalid:
6819 : 0 : error (1, 0,
6820 : 0 : _("invalid encoding"));
6821 : : }
6822 : :
6823 : 280 : return readp;
6824 : : }
6825 : :
6826 : : static const char *
6827 : 31422 : regname (Ebl *ebl, unsigned int regno, char *regnamebuf)
6828 : : {
6829 : 31422 : register_info (ebl, regno, NULL, regnamebuf, NULL, NULL);
6830 : :
6831 : 31422 : return regnamebuf;
6832 : : }
6833 : :
6834 : : static void
6835 : 22218 : print_cfa_program (const unsigned char *readp, const unsigned char *const endp,
6836 : : Dwarf_Word vma_base, unsigned int code_align,
6837 : : int data_align,
6838 : : unsigned int version, unsigned int ptr_size,
6839 : : unsigned int encoding,
6840 : : Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr, Dwarf *dbg)
6841 : : {
6842 : 22218 : char regnamebuf[REGNAMESZ];
6843 : :
6844 : 22218 : puts ("\n Program:");
6845 : 22218 : Dwarf_Word pc = vma_base;
6846 : 22218 : while (readp < endp)
6847 : : {
6848 : 303244 : unsigned int opcode = *readp++;
6849 : :
6850 [ + + ]: 303244 : if (opcode < DW_CFA_advance_loc)
6851 : : /* Extended opcode. */
6852 [ + - + + : 182830 : switch (opcode)
+ + - + -
- + + + +
+ + - + -
- - - - -
+ - - ]
6853 : : {
6854 : 63392 : uint64_t op1;
6855 : 63392 : int64_t sop1;
6856 : 63392 : uint64_t op2;
6857 : 63392 : int64_t sop2;
6858 : :
6859 : 63392 : case DW_CFA_nop:
6860 : 63392 : puts (" nop");
6861 : 63392 : break;
6862 : 0 : case DW_CFA_set_loc:
6863 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
6864 : 0 : goto invalid;
6865 : 0 : readp = read_encoded (encoding, readp, endp, &op1, dbg);
6866 : 182830 : printf (" set_loc %#" PRIx64 " to %#" PRIx64 "\n",
6867 : 0 : op1, pc = vma_base + op1);
6868 : : break;
6869 : 6892 : case DW_CFA_advance_loc1:
6870 [ - + ]: 6892 : if ((uint64_t) (endp - readp) < 1)
6871 : 0 : goto invalid;
6872 : 13784 : printf (" advance_loc1 %u to %#" PRIx64 "\n",
6873 : 6892 : *readp, pc += *readp * code_align);
6874 : 6892 : ++readp;
6875 : 6892 : break;
6876 : 2322 : case DW_CFA_advance_loc2:
6877 [ - + ]: 2322 : if ((uint64_t) (endp - readp) < 2)
6878 : 0 : goto invalid;
6879 [ - + ]: 2322 : op1 = read_2ubyte_unaligned_inc (dbg, readp);
6880 : 185152 : printf (" advance_loc2 %" PRIu64 " to %#" PRIx64 "\n",
6881 : 2322 : op1, pc += op1 * code_align);
6882 : : break;
6883 : 36 : case DW_CFA_advance_loc4:
6884 [ - + ]: 36 : if ((uint64_t) (endp - readp) < 4)
6885 : 0 : goto invalid;
6886 [ + - ]: 36 : op1 = read_4ubyte_unaligned_inc (dbg, readp);
6887 : 182866 : printf (" advance_loc4 %" PRIu64 " to %#" PRIx64 "\n",
6888 : 36 : op1, pc += op1 * code_align);
6889 : : break;
6890 : 8 : case DW_CFA_offset_extended:
6891 [ - + ]: 8 : if ((uint64_t) (endp - readp) < 1)
6892 : 0 : goto invalid;
6893 : 8 : get_uleb128 (op1, readp, endp);
6894 [ - + ]: 8 : if ((uint64_t) (endp - readp) < 1)
6895 : 0 : goto invalid;
6896 : 8 : get_uleb128 (op2, readp, endp);
6897 : 8 : printf (" offset_extended r%" PRIu64 " (%s) at cfa%+" PRId64
6898 : : "\n",
6899 : : op1, regname (ebl, op1, regnamebuf), op2 * data_align);
6900 : : break;
6901 : 0 : case DW_CFA_restore_extended:
6902 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
6903 : 0 : goto invalid;
6904 : 0 : get_uleb128 (op1, readp, endp);
6905 : 0 : printf (" restore_extended r%" PRIu64 " (%s)\n",
6906 : : op1, regname (ebl, op1, regnamebuf));
6907 : : break;
6908 : 110 : case DW_CFA_undefined:
6909 [ - + ]: 110 : if ((uint64_t) (endp - readp) < 1)
6910 : 0 : goto invalid;
6911 : 110 : get_uleb128 (op1, readp, endp);
6912 : 110 : printf (" undefined r%" PRIu64 " (%s)\n", op1,
6913 : : regname (ebl, op1, regnamebuf));
6914 : : break;
6915 : 0 : case DW_CFA_same_value:
6916 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
6917 : 0 : goto invalid;
6918 : 0 : get_uleb128 (op1, readp, endp);
6919 : 0 : printf (" same_value r%" PRIu64 " (%s)\n", op1,
6920 : : regname (ebl, op1, regnamebuf));
6921 : : break;
6922 : 0 : case DW_CFA_register:
6923 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
6924 : 0 : goto invalid;
6925 : 0 : get_uleb128 (op1, readp, endp);
6926 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
6927 : 0 : goto invalid;
6928 : 0 : get_uleb128 (op2, readp, endp);
6929 : 0 : printf (" register r%" PRIu64 " (%s) in r%" PRIu64 " (%s)\n",
6930 : : op1, regname (ebl, op1, regnamebuf), op2,
6931 : : regname (ebl, op2, regnamebuf));
6932 : : break;
6933 : 11104 : case DW_CFA_remember_state:
6934 : 11104 : puts (" remember_state");
6935 : 11104 : break;
6936 : 11104 : case DW_CFA_restore_state:
6937 : 11104 : puts (" restore_state");
6938 : 11104 : break;
6939 : 416 : case DW_CFA_def_cfa:
6940 [ - + ]: 416 : if ((uint64_t) (endp - readp) < 1)
6941 : 0 : goto invalid;
6942 : 416 : get_uleb128 (op1, readp, endp);
6943 [ - + ]: 416 : if ((uint64_t) (endp - readp) < 1)
6944 : 0 : goto invalid;
6945 : 416 : get_uleb128 (op2, readp, endp);
6946 : 416 : printf (" def_cfa r%" PRIu64 " (%s) at offset %" PRIu64 "\n",
6947 : : op1, regname (ebl, op1, regnamebuf), op2);
6948 : : break;
6949 : 126 : case DW_CFA_def_cfa_register:
6950 [ - + ]: 126 : if ((uint64_t) (endp - readp) < 1)
6951 : 0 : goto invalid;
6952 : 126 : get_uleb128 (op1, readp, endp);
6953 : 126 : printf (" def_cfa_register r%" PRIu64 " (%s)\n",
6954 : : op1, regname (ebl, op1, regnamebuf));
6955 : : break;
6956 : 87170 : case DW_CFA_def_cfa_offset:
6957 [ - + ]: 87170 : if ((uint64_t) (endp - readp) < 1)
6958 : 0 : goto invalid;
6959 : 87170 : get_uleb128 (op1, readp, endp);
6960 : 87170 : printf (" def_cfa_offset %" PRIu64 "\n", op1);
6961 : : break;
6962 : 118 : case DW_CFA_def_cfa_expression:
6963 [ - + ]: 118 : if ((uint64_t) (endp - readp) < 1)
6964 : 0 : goto invalid;
6965 : 118 : get_uleb128 (op1, readp, endp); /* Length of DW_FORM_block. */
6966 : 118 : printf (" def_cfa_expression %" PRIu64 "\n", op1);
6967 [ - + ]: 118 : if ((uint64_t) (endp - readp) < op1)
6968 : : {
6969 : 0 : invalid:
6970 : 0 : fputs (_(" <INVALID DATA>\n"), stdout);
6971 : 0 : return;
6972 : : }
6973 : 118 : print_ops (dwflmod, dbg, 10, 10, version, ptr_size, 0, NULL,
6974 : : op1, readp);
6975 : 118 : readp += op1;
6976 : 118 : break;
6977 : 0 : case DW_CFA_expression:
6978 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
6979 : 0 : goto invalid;
6980 : 0 : get_uleb128 (op1, readp, endp);
6981 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
6982 : 0 : goto invalid;
6983 : 0 : get_uleb128 (op2, readp, endp); /* Length of DW_FORM_block. */
6984 : 0 : printf (" expression r%" PRIu64 " (%s) \n",
6985 : : op1, regname (ebl, op1, regnamebuf));
6986 [ # # ]: 0 : if ((uint64_t) (endp - readp) < op2)
6987 : 0 : goto invalid;
6988 : 0 : print_ops (dwflmod, dbg, 10, 10, version, ptr_size, 0, NULL,
6989 : : op2, readp);
6990 : 0 : readp += op2;
6991 : 0 : break;
6992 : 24 : case DW_CFA_offset_extended_sf:
6993 [ - + ]: 24 : if ((uint64_t) (endp - readp) < 1)
6994 : 0 : goto invalid;
6995 : 24 : get_uleb128 (op1, readp, endp);
6996 [ - + ]: 24 : if ((uint64_t) (endp - readp) < 1)
6997 : 0 : goto invalid;
6998 : 24 : get_sleb128 (sop2, readp, endp);
6999 : 24 : printf (" offset_extended_sf r%" PRIu64 " (%s) at cfa%+"
7000 : : PRId64 "\n",
7001 : : op1, regname (ebl, op1, regnamebuf), sop2 * data_align);
7002 : : break;
7003 : 0 : case DW_CFA_def_cfa_sf:
7004 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
7005 : 0 : goto invalid;
7006 : 0 : get_uleb128 (op1, readp, endp);
7007 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
7008 : 0 : goto invalid;
7009 : 0 : get_sleb128 (sop2, readp, endp);
7010 : 0 : printf (" def_cfa_sf r%" PRIu64 " (%s) at offset %" PRId64 "\n",
7011 : : op1, regname (ebl, op1, regnamebuf), sop2 * data_align);
7012 : : break;
7013 : 0 : case DW_CFA_def_cfa_offset_sf:
7014 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
7015 : 0 : goto invalid;
7016 : 0 : get_sleb128 (sop1, readp, endp);
7017 : 0 : printf (" def_cfa_offset_sf %" PRId64 "\n", sop1 * data_align);
7018 : : break;
7019 : 0 : case DW_CFA_val_offset:
7020 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
7021 : 0 : goto invalid;
7022 : 0 : get_uleb128 (op1, readp, endp);
7023 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
7024 : 0 : goto invalid;
7025 : 0 : get_uleb128 (op2, readp, endp);
7026 : 0 : printf (" val_offset %" PRIu64 " at offset %" PRIu64 "\n",
7027 : : op1, op2 * data_align);
7028 : : break;
7029 : 0 : case DW_CFA_val_offset_sf:
7030 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
7031 : 0 : goto invalid;
7032 : 0 : get_uleb128 (op1, readp, endp);
7033 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
7034 : 0 : goto invalid;
7035 : 0 : get_sleb128 (sop2, readp, endp);
7036 : 0 : printf (" val_offset_sf %" PRIu64 " at offset %" PRId64 "\n",
7037 : : op1, sop2 * data_align);
7038 : : break;
7039 : 0 : case DW_CFA_val_expression:
7040 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
7041 : 0 : goto invalid;
7042 : 0 : get_uleb128 (op1, readp, endp);
7043 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
7044 : 0 : goto invalid;
7045 : 0 : get_uleb128 (op2, readp, endp); /* Length of DW_FORM_block. */
7046 : 0 : printf (" val_expression r%" PRIu64 " (%s)\n",
7047 : : op1, regname (ebl, op1, regnamebuf));
7048 [ # # ]: 0 : if ((uint64_t) (endp - readp) < op2)
7049 : 0 : goto invalid;
7050 : 0 : print_ops (dwflmod, dbg, 10, 10, version, ptr_size, 0,
7051 : : NULL, op2, readp);
7052 : 0 : readp += op2;
7053 : 0 : break;
7054 : 0 : case DW_CFA_MIPS_advance_loc8:
7055 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 8)
7056 : 0 : goto invalid;
7057 [ # # ]: 0 : op1 = read_8ubyte_unaligned_inc (dbg, readp);
7058 : 182830 : printf (" MIPS_advance_loc8 %" PRIu64 " to %#" PRIx64 "\n",
7059 : 0 : op1, pc += op1 * code_align);
7060 : : break;
7061 : 8 : case DW_CFA_GNU_window_save: /* DW_CFA_AARCH64_negate_ra_state */
7062 [ + - ]: 8 : if (ehdr->e_machine == EM_AARCH64)
7063 : 8 : puts (" AARCH64_negate_ra_state");
7064 : : else
7065 : 0 : puts (" GNU_window_save");
7066 : : break;
7067 : 0 : case DW_CFA_GNU_args_size:
7068 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
7069 : 0 : goto invalid;
7070 : 0 : get_uleb128 (op1, readp, endp);
7071 : 0 : printf (" args_size %" PRIu64 "\n", op1);
7072 : : break;
7073 : : default:
7074 : 182830 : printf (" ??? (%u)\n", opcode);
7075 : : break;
7076 : : }
7077 [ + + ]: 120414 : else if (opcode < DW_CFA_offset)
7078 : 415138 : printf (" advance_loc %u to %#" PRIx64 "\n",
7079 : 89676 : opcode & 0x3f, pc += (opcode & 0x3f) * code_align);
7080 [ + + ]: 30738 : else if (opcode < DW_CFA_restore)
7081 : : {
7082 : 27556 : uint64_t offset;
7083 [ - + ]: 27556 : if ((uint64_t) (endp - readp) < 1)
7084 : 0 : goto invalid;
7085 : 27556 : get_uleb128 (offset, readp, endp);
7086 : 27556 : printf (" offset r%u (%s) at cfa%+" PRId64 "\n",
7087 : : opcode & 0x3f, regname (ebl, opcode & 0x3f, regnamebuf),
7088 : : offset * data_align);
7089 : : }
7090 : : else
7091 [ + + ]: 328644 : printf (" restore r%u (%s)\n",
7092 : : opcode & 0x3f, regname (ebl, opcode & 0x3f, regnamebuf));
7093 : : }
7094 : : }
7095 : :
7096 : :
7097 : : static unsigned int
7098 : 21908 : encoded_ptr_size (int encoding, unsigned int ptr_size)
7099 : : {
7100 [ - + - + ]: 21908 : switch (encoding & 7)
7101 : : {
7102 : : case DW_EH_PE_udata4:
7103 : : return 4;
7104 : 0 : case DW_EH_PE_udata8:
7105 : 0 : return 8;
7106 : : case 0:
7107 : : return ptr_size;
7108 : : }
7109 : :
7110 : 0 : fprintf (stderr, "Unsupported pointer encoding: %#x, "
7111 : : "assuming pointer size of %d.\n", encoding, ptr_size);
7112 : 0 : return ptr_size;
7113 : : }
7114 : :
7115 : :
7116 : : static unsigned int
7117 : 642 : print_encoding (unsigned int val)
7118 : : {
7119 [ - - - + : 642 : switch (val & 0xf)
- - - + -
- ]
7120 : : {
7121 : 0 : case DW_EH_PE_absptr:
7122 : 0 : fputs ("absptr", stdout);
7123 : 0 : break;
7124 : 0 : case DW_EH_PE_uleb128:
7125 : 0 : fputs ("uleb128", stdout);
7126 : 0 : break;
7127 : 0 : case DW_EH_PE_udata2:
7128 : 0 : fputs ("udata2", stdout);
7129 : 0 : break;
7130 : 140 : case DW_EH_PE_udata4:
7131 : 140 : fputs ("udata4", stdout);
7132 : 140 : break;
7133 : 0 : case DW_EH_PE_udata8:
7134 : 0 : fputs ("udata8", stdout);
7135 : 0 : break;
7136 : 0 : case DW_EH_PE_sleb128:
7137 : 0 : fputs ("sleb128", stdout);
7138 : 0 : break;
7139 : 0 : case DW_EH_PE_sdata2:
7140 : 0 : fputs ("sdata2", stdout);
7141 : 0 : break;
7142 : 502 : case DW_EH_PE_sdata4:
7143 : 502 : fputs ("sdata4", stdout);
7144 : 502 : break;
7145 : 0 : case DW_EH_PE_sdata8:
7146 : 0 : fputs ("sdata8", stdout);
7147 : 0 : break;
7148 : : default:
7149 : : /* We did not use any of the bits after all. */
7150 : : return val;
7151 : : }
7152 : :
7153 : 642 : return val & ~0xf;
7154 : : }
7155 : :
7156 : :
7157 : : static unsigned int
7158 : 502 : print_relinfo (unsigned int val)
7159 : : {
7160 [ + - + - : 502 : switch (val & 0x70)
- - ]
7161 : : {
7162 : 362 : case DW_EH_PE_pcrel:
7163 : 362 : fputs ("pcrel", stdout);
7164 : 362 : break;
7165 : 0 : case DW_EH_PE_textrel:
7166 : 0 : fputs ("textrel", stdout);
7167 : 0 : break;
7168 : 140 : case DW_EH_PE_datarel:
7169 : 140 : fputs ("datarel", stdout);
7170 : 140 : break;
7171 : 0 : case DW_EH_PE_funcrel:
7172 : 0 : fputs ("funcrel", stdout);
7173 : 0 : break;
7174 : 0 : case DW_EH_PE_aligned:
7175 : 0 : fputs ("aligned", stdout);
7176 : 0 : break;
7177 : : default:
7178 : : return val;
7179 : : }
7180 : :
7181 : 502 : return val & ~0x70;
7182 : : }
7183 : :
7184 : :
7185 : : static void
7186 : 642 : print_encoding_base (const char *pfx, unsigned int fde_encoding)
7187 : : {
7188 : 642 : printf ("(%s", pfx);
7189 : :
7190 [ - + ]: 642 : if (fde_encoding == DW_EH_PE_omit)
7191 : 0 : puts ("omit)");
7192 : : else
7193 : : {
7194 : 642 : unsigned int w = fde_encoding;
7195 : :
7196 : 642 : w = print_encoding (w);
7197 : :
7198 [ + + ]: 642 : if (w & 0x70)
7199 : : {
7200 [ + - ]: 502 : if (w != fde_encoding)
7201 : 502 : fputc (' ', stdout);
7202 : :
7203 : 502 : w = print_relinfo (w);
7204 : : }
7205 : :
7206 [ - + ]: 642 : if (w != 0)
7207 [ # # ]: 0 : printf ("%s%x", w != fde_encoding ? " " : "", w);
7208 : :
7209 : 642 : puts (")");
7210 : : }
7211 : 642 : }
7212 : :
7213 : :
7214 : : static void
7215 : 196 : print_debug_frame_section (Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr,
7216 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
7217 : : {
7218 : 196 : size_t shstrndx;
7219 : : /* We know this call will succeed since it did in the caller. */
7220 : 196 : (void) elf_getshdrstrndx (ebl->elf, &shstrndx);
7221 : 196 : const char *scnname = section_name (ebl, shdr);
7222 : :
7223 : : /* Needed if we find PC-relative addresses. */
7224 : 196 : GElf_Addr bias;
7225 [ - + ]: 196 : if (dwfl_module_getelf (dwflmod, &bias) == NULL)
7226 : : {
7227 : 0 : error (0, 0, _("cannot get ELF: %s"), dwfl_errmsg (-1));
7228 : 0 : return;
7229 : : }
7230 : :
7231 : 196 : bool is_eh_frame = strcmp (scnname, ".eh_frame") == 0;
7232 : 196 : Elf_Data *data;
7233 [ + + ]: 196 : if (is_eh_frame)
7234 : : {
7235 : 144 : data = elf_rawdata (scn, NULL);
7236 [ - + ]: 144 : if (data == NULL)
7237 : : {
7238 : 0 : error (0, 0, _("cannot get %s content: %s"),
7239 : : scnname, elf_errmsg (-1));
7240 : 0 : return;
7241 : : }
7242 : : }
7243 : : else
7244 : : {
7245 : 52 : data = get_debug_elf_data (dbg, ebl, IDX_debug_frame, scn);
7246 [ + - ]: 52 : if (data == NULL)
7247 : : return;
7248 : : }
7249 : :
7250 [ + + ]: 196 : if (is_eh_frame)
7251 : 144 : printf (_("\
7252 : : \nCall frame information section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
7253 : 144 : elf_ndxscn (scn), scnname, (uint64_t) shdr->sh_offset);
7254 : : else
7255 : 52 : printf (_("\
7256 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
7257 : 52 : elf_ndxscn (scn), scnname, (uint64_t) shdr->sh_offset);
7258 : :
7259 : 196 : struct cieinfo
7260 : : {
7261 : : ptrdiff_t cie_offset;
7262 : : const char *augmentation;
7263 : : unsigned int code_alignment_factor;
7264 : : unsigned int data_alignment_factor;
7265 : : uint8_t address_size;
7266 : : uint8_t fde_encoding;
7267 : : uint8_t lsda_encoding;
7268 : : struct cieinfo *next;
7269 : 196 : } *cies = NULL;
7270 : :
7271 : 196 : const unsigned char *readp = data->d_buf;
7272 : 196 : const unsigned char *const dataend = ((unsigned char *) data->d_buf
7273 : 196 : + data->d_size);
7274 [ + + ]: 22554 : while (readp < dataend)
7275 : : {
7276 [ - + ]: 22358 : if (unlikely (readp + 4 > dataend))
7277 : : {
7278 : 0 : invalid_data:
7279 : 0 : error (0, 0, _("invalid data in section [%zu] '%s'"),
7280 : : elf_ndxscn (scn), scnname);
7281 : 0 : return;
7282 : : }
7283 : :
7284 : : /* At the beginning there must be a CIE. There can be multiple,
7285 : : hence we test tis in a loop. */
7286 : 22358 : ptrdiff_t offset = readp - (unsigned char *) data->d_buf;
7287 : :
7288 [ + + ]: 22358 : Dwarf_Word unit_length = read_4ubyte_unaligned_inc (dbg, readp);
7289 : 22358 : unsigned int length = 4;
7290 [ - + ]: 22358 : if (unlikely (unit_length == 0xffffffff))
7291 : : {
7292 [ # # ]: 0 : if (unlikely (readp + 8 > dataend))
7293 : 0 : goto invalid_data;
7294 : :
7295 [ # # ]: 0 : unit_length = read_8ubyte_unaligned_inc (dbg, readp);
7296 : 0 : length = 8;
7297 : : }
7298 : :
7299 [ + + ]: 22358 : if (unlikely (unit_length == 0))
7300 : : {
7301 : 140 : printf (_("\n [%6tx] Zero terminator\n"), offset);
7302 : 140 : continue;
7303 : : }
7304 : :
7305 : 22218 : Dwarf_Word maxsize = dataend - readp;
7306 [ - + ]: 22218 : if (unlikely (unit_length > maxsize))
7307 : 0 : goto invalid_data;
7308 : :
7309 [ + + ]: 22218 : unsigned int ptr_size = ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 4 : 8;
7310 : :
7311 : 22218 : ptrdiff_t start = readp - (unsigned char *) data->d_buf;
7312 : 22218 : const unsigned char *const cieend = readp + unit_length;
7313 [ - + ]: 22218 : if (unlikely (cieend > dataend))
7314 : 0 : goto invalid_data;
7315 : :
7316 : 22218 : Dwarf_Off cie_id;
7317 [ + - ]: 22218 : if (length == 4)
7318 : : {
7319 [ - + ]: 22218 : if (unlikely (cieend - readp < 4))
7320 : 0 : goto invalid_data;
7321 [ + + ]: 22218 : cie_id = read_4ubyte_unaligned_inc (dbg, readp);
7322 [ + + ]: 22218 : if (!is_eh_frame && cie_id == DW_CIE_ID_32)
7323 : : cie_id = DW_CIE_ID_64;
7324 : : }
7325 : : else
7326 : : {
7327 [ # # ]: 0 : if (unlikely (cieend - readp < 8))
7328 : 0 : goto invalid_data;
7329 [ # # ]: 0 : cie_id = read_8ubyte_unaligned_inc (dbg, readp);
7330 : : }
7331 : :
7332 : 44352 : uint_fast8_t version = 2;
7333 : 22134 : unsigned int code_alignment_factor;
7334 : 22134 : int data_alignment_factor;
7335 : 44352 : unsigned int fde_encoding = 0;
7336 : 44352 : unsigned int lsda_encoding = 0;
7337 : 44352 : Dwarf_Word initial_location = 0;
7338 : 44352 : Dwarf_Word vma_base = 0;
7339 : :
7340 [ + + + + ]: 22310 : if (cie_id == (is_eh_frame ? 0 : DW_CIE_ID_64))
7341 : : {
7342 [ - + ]: 310 : if (unlikely (cieend - readp < 2))
7343 : 0 : goto invalid_data;
7344 : 310 : version = *readp++;
7345 : 310 : const char *const augmentation = (const char *) readp;
7346 : 310 : readp = memchr (readp, '\0', cieend - readp);
7347 [ - + ]: 310 : if (unlikely (readp == NULL))
7348 : 0 : goto invalid_data;
7349 : 310 : ++readp;
7350 : :
7351 : 310 : uint_fast8_t segment_size = 0;
7352 [ + + ]: 310 : if (version >= 4)
7353 : : {
7354 [ - + ]: 8 : if (cieend - readp < 5)
7355 : 0 : goto invalid_data;
7356 : 8 : ptr_size = *readp++;
7357 : 8 : segment_size = *readp++;
7358 : : }
7359 : :
7360 [ - + ]: 310 : if (cieend - readp < 1)
7361 : 0 : goto invalid_data;
7362 : 310 : get_uleb128 (code_alignment_factor, readp, cieend);
7363 [ - + ]: 310 : if (cieend - readp < 1)
7364 : 0 : goto invalid_data;
7365 : 310 : get_sleb128 (data_alignment_factor, readp, cieend);
7366 : :
7367 : : /* In some variant for unwind data there is another field. */
7368 [ - + ]: 310 : if (strcmp (augmentation, "eh") == 0)
7369 [ # # ]: 0 : readp += ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 4 : 8;
7370 : :
7371 : 310 : unsigned int return_address_register;
7372 [ - + ]: 310 : if (cieend - readp < 1)
7373 : 0 : goto invalid_data;
7374 [ + + ]: 310 : if (unlikely (version == 1))
7375 : 286 : return_address_register = *readp++;
7376 : : else
7377 : 24 : get_uleb128 (return_address_register, readp, cieend);
7378 : :
7379 : 310 : printf ("\n [%6tx] CIE length=%" PRIu64 "\n"
7380 : : " CIE_id: %" PRIu64 "\n"
7381 : : " version: %u\n"
7382 : : " augmentation: \"%s\"\n",
7383 : : offset, (uint64_t) unit_length, (uint64_t) cie_id,
7384 : : version, augmentation);
7385 [ + + ]: 310 : if (version >= 4)
7386 : 8 : printf (" address_size: %u\n"
7387 : : " segment_size: %u\n",
7388 : : ptr_size, segment_size);
7389 : 310 : printf (" code_alignment_factor: %u\n"
7390 : : " data_alignment_factor: %d\n"
7391 : : " return_address_register: %u\n",
7392 : : code_alignment_factor,
7393 : : data_alignment_factor, return_address_register);
7394 : :
7395 [ + + ]: 310 : if (augmentation[0] == 'z')
7396 : : {
7397 [ - + ]: 222 : if (cieend - readp < 1)
7398 : 0 : goto invalid_data;
7399 : :
7400 : 222 : unsigned int augmentationlen;
7401 : 222 : get_uleb128 (augmentationlen, readp, cieend);
7402 : :
7403 [ - + ]: 222 : if (augmentationlen > (size_t) (cieend - readp))
7404 : : {
7405 : 0 : error (0, 0, _("invalid augmentation length"));
7406 : 0 : readp = cieend;
7407 : 0 : continue;
7408 : : }
7409 : :
7410 : 222 : const char *hdr = "Augmentation data:";
7411 : 222 : const char *cp = augmentation + 1;
7412 [ + + + - ]: 444 : while (*cp != '\0' && cp < augmentation + augmentationlen + 1)
7413 : : {
7414 : 222 : printf (" %-26s%#x ", hdr, *readp);
7415 : 222 : hdr = "";
7416 : :
7417 [ + - ]: 222 : if (*cp == 'R')
7418 : : {
7419 : 222 : fde_encoding = *readp++;
7420 : 222 : print_encoding_base (_("FDE address encoding: "),
7421 : : fde_encoding);
7422 : : }
7423 [ # # ]: 0 : else if (*cp == 'L')
7424 : : {
7425 : 0 : lsda_encoding = *readp++;
7426 : 0 : print_encoding_base (_("LSDA pointer encoding: "),
7427 : : lsda_encoding);
7428 : : }
7429 [ # # ]: 0 : else if (*cp == 'P')
7430 : : {
7431 : : /* Personality. This field usually has a relocation
7432 : : attached pointing to __gcc_personality_v0. */
7433 : 0 : const unsigned char *startp = readp;
7434 : 0 : unsigned int encoding = *readp++;
7435 : 0 : uint64_t val = 0;
7436 : 0 : readp = read_encoded (encoding, readp,
7437 : : readp - 1 + augmentationlen,
7438 : : &val, dbg);
7439 : :
7440 : 0 : while (++startp < readp)
7441 [ # # ]: 0 : printf ("%#x ", *startp);
7442 : :
7443 : 0 : putchar ('(');
7444 : 0 : print_encoding (encoding);
7445 : 0 : putchar (' ');
7446 [ # # ]: 0 : switch (encoding & 0xf)
7447 : : {
7448 : 0 : case DW_EH_PE_sleb128:
7449 : : case DW_EH_PE_sdata2:
7450 : : case DW_EH_PE_sdata4:
7451 : 0 : printf ("%" PRId64 ")\n", val);
7452 : : break;
7453 : 0 : default:
7454 : 0 : printf ("%#" PRIx64 ")\n", val);
7455 : : break;
7456 : : }
7457 : : }
7458 : : else
7459 : 0 : printf ("(%x)\n", *readp++);
7460 : :
7461 : 222 : ++cp;
7462 : : }
7463 : : }
7464 : :
7465 [ + - ]: 310 : if (likely (ptr_size == 4 || ptr_size == 8))
7466 : : {
7467 : 310 : struct cieinfo *newp = alloca (sizeof (*newp));
7468 : 310 : newp->cie_offset = offset;
7469 : 310 : newp->augmentation = augmentation;
7470 : 310 : newp->fde_encoding = fde_encoding;
7471 : 310 : newp->lsda_encoding = lsda_encoding;
7472 : 310 : newp->address_size = ptr_size;
7473 : 310 : newp->code_alignment_factor = code_alignment_factor;
7474 : 310 : newp->data_alignment_factor = data_alignment_factor;
7475 : 310 : newp->next = cies;
7476 : 310 : cies = newp;
7477 : : }
7478 : : }
7479 : : else
7480 : : {
7481 : : struct cieinfo *cie = cies;
7482 [ + - ]: 21908 : while (cie != NULL)
7483 [ + + - + ]: 43816 : if (is_eh_frame
7484 : 21816 : ? ((Dwarf_Off) start - cie_id) == (Dwarf_Off) cie->cie_offset
7485 : 92 : : cie_id == (Dwarf_Off) cie->cie_offset)
7486 : : break;
7487 : : else
7488 : 0 : cie = cie->next;
7489 [ - + ]: 21908 : if (unlikely (cie == NULL))
7490 : : {
7491 : 0 : puts ("invalid CIE reference in FDE");
7492 : 0 : return;
7493 : : }
7494 : :
7495 : : /* Initialize from CIE data. */
7496 : 21908 : fde_encoding = cie->fde_encoding;
7497 : 21908 : lsda_encoding = cie->lsda_encoding;
7498 : 21908 : ptr_size = encoded_ptr_size (fde_encoding, cie->address_size);
7499 : 21908 : code_alignment_factor = cie->code_alignment_factor;
7500 : 21908 : data_alignment_factor = cie->data_alignment_factor;
7501 : :
7502 : 21908 : const unsigned char *base = readp;
7503 : : // XXX There are sometimes relocations for this value
7504 [ - + + + : 21908 : initial_location = read_addr_unaligned_inc (ptr_size, dbg, readp);
- + + + ]
7505 : 43816 : Dwarf_Word address_range
7506 [ + + - + : 21908 : = read_addr_unaligned_inc (ptr_size, dbg, readp);
+ + ]
7507 : :
7508 : : /* pcrel for an FDE address is relative to the runtime
7509 : : address of the start_address field itself. Sign extend
7510 : : if necessary to make sure the calculation is done on the
7511 : : full 64 bit address even when initial_location only holds
7512 : : the lower 32 bits. */
7513 : 21908 : Dwarf_Addr pc_start = initial_location;
7514 [ + + ]: 21908 : if (ptr_size == 4)
7515 : 21828 : pc_start = (uint64_t) (int32_t) pc_start;
7516 [ + + ]: 21908 : if ((fde_encoding & 0x70) == DW_EH_PE_pcrel)
7517 : 21804 : pc_start += ((uint64_t) shdr->sh_addr
7518 : 21804 : + (base - (const unsigned char *) data->d_buf)
7519 : 21804 : - bias);
7520 : :
7521 : 21908 : printf ("\n [%6tx] FDE length=%" PRIu64 " cie=[%6tx]\n"
7522 : : " CIE_pointer: %" PRIu64 "\n"
7523 : : " initial_location: ",
7524 : : offset, (uint64_t) unit_length,
7525 : : cie->cie_offset, (uint64_t) cie_id);
7526 : 21908 : print_dwarf_addr (dwflmod, cie->address_size,
7527 : : pc_start, initial_location);
7528 [ + + ]: 21908 : if ((fde_encoding & 0x70) == DW_EH_PE_pcrel)
7529 : : {
7530 : 43608 : vma_base = (((uint64_t) shdr->sh_offset
7531 : 21804 : + (base - (const unsigned char *) data->d_buf)
7532 : 21804 : + (uint64_t) initial_location)
7533 : : & (ptr_size == 4
7534 : : ? UINT64_C (0xffffffff)
7535 [ - + ]: 21804 : : UINT64_C (0xffffffffffffffff)));
7536 : 21804 : printf (_(" (offset: %#" PRIx64 ")"),
7537 : : (uint64_t) vma_base);
7538 : : }
7539 : :
7540 : 21908 : printf ("\n address_range: %#" PRIx64,
7541 : : (uint64_t) address_range);
7542 [ + + ]: 21908 : if ((fde_encoding & 0x70) == DW_EH_PE_pcrel)
7543 : 21804 : printf (_(" (end offset: %#" PRIx64 ")"),
7544 : 21804 : ((uint64_t) vma_base + (uint64_t) address_range)
7545 : : & (ptr_size == 4
7546 : : ? UINT64_C (0xffffffff)
7547 [ - + ]: 21804 : : UINT64_C (0xffffffffffffffff)));
7548 : 21908 : putchar ('\n');
7549 : :
7550 [ + + ]: 21908 : if (cie->augmentation[0] == 'z')
7551 : : {
7552 : 21804 : unsigned int augmentationlen;
7553 [ - + ]: 21804 : if (cieend - readp < 1)
7554 : 0 : goto invalid_data;
7555 : 21804 : get_uleb128 (augmentationlen, readp, cieend);
7556 : :
7557 [ - + ]: 21804 : if (augmentationlen > (size_t) (cieend - readp))
7558 : : {
7559 : 0 : error (0, 0, _("invalid augmentation length"));
7560 : 0 : readp = cieend;
7561 : 0 : continue;
7562 : : }
7563 : :
7564 [ - + ]: 21804 : if (augmentationlen > 0)
7565 : : {
7566 : 0 : const char *hdr = "Augmentation data:";
7567 : 0 : const char *cp = cie->augmentation + 1;
7568 : 0 : unsigned int u = 0;
7569 : 0 : while (*cp != '\0'
7570 [ # # # # ]: 0 : && cp < cie->augmentation + augmentationlen + 1)
7571 : : {
7572 [ # # ]: 0 : if (*cp == 'L')
7573 : : {
7574 : 0 : uint64_t lsda_pointer;
7575 : 0 : const unsigned char *p
7576 : 0 : = read_encoded (lsda_encoding, &readp[u],
7577 : : &readp[augmentationlen],
7578 : : &lsda_pointer, dbg);
7579 : 0 : u = p - readp;
7580 : 0 : printf (_("\
7581 : : %-26sLSDA pointer: %#" PRIx64 "\n"),
7582 : : hdr, lsda_pointer);
7583 : 0 : hdr = "";
7584 : : }
7585 : 0 : ++cp;
7586 : : }
7587 : :
7588 [ # # ]: 0 : while (u < augmentationlen)
7589 : : {
7590 : 0 : printf (" %-26s%#x\n", hdr, readp[u++]);
7591 : 0 : hdr = "";
7592 : : }
7593 : : }
7594 : :
7595 : 21804 : readp += augmentationlen;
7596 : : }
7597 : : }
7598 : :
7599 : : /* Handle the initialization instructions. */
7600 [ - + ]: 22218 : if (ptr_size != 4 && ptr_size !=8)
7601 : 0 : printf ("invalid CIE pointer size (%u), must be 4 or 8.\n", ptr_size);
7602 : : else
7603 : 22218 : print_cfa_program (readp, cieend, vma_base, code_alignment_factor,
7604 : : data_alignment_factor, version, ptr_size,
7605 : : fde_encoding, dwflmod, ebl, ehdr, dbg);
7606 : 22218 : readp = cieend;
7607 : : }
7608 : : }
7609 : :
7610 : :
7611 : : /* Returns the signedness (or false if it cannot be determined) and
7612 : : the byte size (or zero if it cannot be gotten) of the given DIE
7613 : : DW_AT_type attribute. Uses dwarf_peel_type and dwarf_aggregate_size. */
7614 : : static void
7615 : 348988 : die_type_sign_bytes (Dwarf_Die *die, bool *is_signed, int *bytes)
7616 : : {
7617 : 348988 : Dwarf_Attribute attr;
7618 : 348988 : Dwarf_Die type;
7619 : :
7620 : 348988 : *bytes = 0;
7621 : 348988 : *is_signed = false;
7622 : :
7623 [ + + ]: 348988 : if (dwarf_peel_type (dwarf_formref_die (dwarf_attr_integrate (die,
7624 : : DW_AT_type,
7625 : : &attr), &type),
7626 : : &type) == 0)
7627 : : {
7628 : 896 : Dwarf_Word val;
7629 : 896 : *is_signed = (dwarf_formudata (dwarf_attr (&type, DW_AT_encoding,
7630 : : &attr), &val) == 0
7631 [ + + + + ]: 896 : && (val == DW_ATE_signed || val == DW_ATE_signed_char));
7632 : :
7633 [ + - ]: 896 : if (dwarf_aggregate_size (&type, &val) == 0)
7634 : 896 : *bytes = val;
7635 : : }
7636 : 348988 : }
7637 : :
7638 : : struct attrcb_args
7639 : : {
7640 : : Dwfl_Module *dwflmod;
7641 : : Dwarf *dbg;
7642 : : Dwarf_Die *dies;
7643 : : int level;
7644 : : bool silent;
7645 : : bool is_split;
7646 : : unsigned int version;
7647 : : unsigned int addrsize;
7648 : : unsigned int offset_size;
7649 : : struct Dwarf_CU *cu;
7650 : : };
7651 : :
7652 : :
7653 : : static int
7654 : 7934008 : attr_callback (Dwarf_Attribute *attrp, void *arg)
7655 : : {
7656 : 7934008 : struct attrcb_args *cbargs = (struct attrcb_args *) arg;
7657 : 7934008 : const int level = cbargs->level;
7658 : 7934008 : Dwarf_Die *die = &cbargs->dies[level];
7659 : 7934008 : bool is_split = cbargs->is_split;
7660 : :
7661 [ + - ]: 7934008 : unsigned int attr = dwarf_whatattr (attrp);
7662 [ - + ]: 7934008 : if (unlikely (attr == 0))
7663 : : {
7664 [ # # ]: 0 : if (!cbargs->silent)
7665 : 0 : error (0, 0, _("DIE [%" PRIx64 "] "
7666 : : "cannot get attribute code: %s"),
7667 : : dwarf_dieoffset (die), dwarf_errmsg (-1));
7668 : 0 : return DWARF_CB_ABORT;
7669 : : }
7670 : :
7671 [ + - ]: 7934008 : unsigned int form = dwarf_whatform (attrp);
7672 [ - + ]: 7934008 : if (unlikely (form == 0))
7673 : : {
7674 [ # # ]: 0 : if (!cbargs->silent)
7675 : 0 : error (0, 0, _("DIE [%" PRIx64 "] "
7676 : : "cannot get attribute form: %s"),
7677 : : dwarf_dieoffset (die), dwarf_errmsg (-1));
7678 : 0 : return DWARF_CB_ABORT;
7679 : : }
7680 : :
7681 [ + + + + : 7934008 : switch (form)
+ + + +
- ]
7682 : : {
7683 : 101378 : case DW_FORM_addr:
7684 : : case DW_FORM_addrx:
7685 : : case DW_FORM_addrx1:
7686 : : case DW_FORM_addrx2:
7687 : : case DW_FORM_addrx3:
7688 : : case DW_FORM_addrx4:
7689 : : case DW_FORM_GNU_addr_index:
7690 [ + + ]: 101378 : if (!cbargs->silent)
7691 : : {
7692 : 100836 : Dwarf_Addr addr;
7693 [ - + ]: 100836 : if (unlikely (dwarf_formaddr (attrp, &addr) != 0))
7694 : : {
7695 : 0 : attrval_out:
7696 [ # # ]: 0 : if (!cbargs->silent)
7697 : 0 : error (0, 0, _("DIE [%" PRIx64 "] "
7698 : : "cannot get attribute '%s' (%s) value: "
7699 : : "%s"),
7700 : : dwarf_dieoffset (die),
7701 : : dwarf_attr_name (attr),
7702 : : dwarf_form_name (form),
7703 : : dwarf_errmsg (-1));
7704 : : /* Don't ABORT, it might be other attributes can be resolved. */
7705 : 0 : return DWARF_CB_OK;
7706 : : }
7707 [ + + ]: 100836 : if (form != DW_FORM_addr )
7708 : : {
7709 : 40 : Dwarf_Word word;
7710 [ - + ]: 40 : if (dwarf_formudata (attrp, &word) != 0)
7711 : 0 : goto attrval_out;
7712 : 40 : printf (" %*s%-20s (%s) [%" PRIx64 "] ",
7713 : : (int) (level * 2), "", dwarf_attr_name (attr),
7714 : : dwarf_form_name (form), word);
7715 : : }
7716 : : else
7717 : 100796 : printf (" %*s%-20s (%s) ",
7718 : : (int) (level * 2), "", dwarf_attr_name (attr),
7719 : : dwarf_form_name (form));
7720 : 100836 : print_dwarf_addr (cbargs->dwflmod, cbargs->addrsize, addr, addr);
7721 : 100836 : printf ("\n");
7722 : : }
7723 : 7136196 : break;
7724 : :
7725 : 1314348 : case DW_FORM_indirect:
7726 : : case DW_FORM_strp:
7727 : : case DW_FORM_line_strp:
7728 : : case DW_FORM_strx:
7729 : : case DW_FORM_strx1:
7730 : : case DW_FORM_strx2:
7731 : : case DW_FORM_strx3:
7732 : : case DW_FORM_strx4:
7733 : : case DW_FORM_string:
7734 : : case DW_FORM_GNU_strp_alt:
7735 : : case DW_FORM_GNU_str_index:
7736 [ + + ]: 1314348 : if (cbargs->silent)
7737 : : break;
7738 : 1312838 : const char *str = dwarf_formstring (attrp);
7739 [ - + ]: 1312838 : if (unlikely (str == NULL))
7740 : 0 : goto attrval_out;
7741 : 1312838 : printf (" %*s%-20s (%s) \"%s\"\n",
7742 : : (int) (level * 2), "", dwarf_attr_name (attr),
7743 : : dwarf_form_name (form), str);
7744 : : break;
7745 : :
7746 : 1716310 : case DW_FORM_ref_addr:
7747 : : case DW_FORM_ref_udata:
7748 : : case DW_FORM_ref8:
7749 : : case DW_FORM_ref4:
7750 : : case DW_FORM_ref2:
7751 : : case DW_FORM_ref1:
7752 : : case DW_FORM_GNU_ref_alt:
7753 : : case DW_FORM_ref_sup4:
7754 : : case DW_FORM_ref_sup8:
7755 [ + + ]: 1716310 : if (cbargs->silent)
7756 : : break;
7757 : 1714730 : Dwarf_Die ref;
7758 [ - + ]: 1714730 : if (unlikely (dwarf_formref_die (attrp, &ref) == NULL))
7759 : 0 : goto attrval_out;
7760 : :
7761 : 1714730 : printf (" %*s%-20s (%s) ",
7762 : : (int) (level * 2), "", dwarf_attr_name (attr),
7763 : : dwarf_form_name (form));
7764 [ + + ]: 1714730 : if (is_split)
7765 : 66 : printf ("{%6" PRIxMAX "}\n", (uintmax_t) dwarf_dieoffset (&ref));
7766 : : else
7767 : 1714664 : printf ("[%6" PRIxMAX "]\n", (uintmax_t) dwarf_dieoffset (&ref));
7768 : : break;
7769 : :
7770 : 8 : case DW_FORM_ref_sig8:
7771 [ + - ]: 8 : if (cbargs->silent)
7772 : : break;
7773 : 8 : printf (" %*s%-20s (%s) {%6" PRIx64 "}\n",
7774 : : (int) (level * 2), "", dwarf_attr_name (attr),
7775 : : dwarf_form_name (form),
7776 [ - + ]: 8 : (uint64_t) read_8ubyte_unaligned (attrp->cu->dbg, attrp->valp));
7777 : : break;
7778 : :
7779 : 4407182 : case DW_FORM_sec_offset:
7780 : : case DW_FORM_rnglistx:
7781 : : case DW_FORM_loclistx:
7782 : : case DW_FORM_implicit_const:
7783 : : case DW_FORM_udata:
7784 : : case DW_FORM_sdata:
7785 : : case DW_FORM_data8: /* Note no data16 here, we see that as block. */
7786 : : case DW_FORM_data4:
7787 : : case DW_FORM_data2:
7788 : 4407182 : case DW_FORM_data1:;
7789 : 4407182 : Dwarf_Word num;
7790 [ - + ]: 4407182 : if (unlikely (dwarf_formudata (attrp, &num) != 0))
7791 : 0 : goto attrval_out;
7792 : :
7793 : 4407182 : const char *valuestr = NULL;
7794 : 4407182 : bool as_hex_id = false;
7795 [ + + + + : 4407182 : switch (attr)
+ + + + +
+ - - - -
- - + - +
+ + ]
7796 : : {
7797 : : /* This case can take either a constant or a loclistptr. */
7798 : 546708 : case DW_AT_data_member_location:
7799 [ + - ]: 546708 : if (form != DW_FORM_sec_offset
7800 [ + + ]: 546708 : && (cbargs->version >= 4
7801 [ + - ]: 47612 : || (form != DW_FORM_data4 && form != DW_FORM_data8)))
7802 : : {
7803 [ + - ]: 546708 : if (!cbargs->silent)
7804 : 546708 : printf (" %*s%-20s (%s) %" PRIuMAX "\n",
7805 : : (int) (level * 2), "", dwarf_attr_name (attr),
7806 : : dwarf_form_name (form), (uintmax_t) num);
7807 : 546708 : return DWARF_CB_OK;
7808 : : }
7809 : 217658 : FALLTHROUGH;
7810 : :
7811 : : /* These cases always take a loclist[ptr] and no constant. */
7812 : : case DW_AT_location:
7813 : : case DW_AT_data_location:
7814 : : case DW_AT_vtable_elem_location:
7815 : : case DW_AT_string_length:
7816 : : case DW_AT_use_location:
7817 : : case DW_AT_frame_base:
7818 : : case DW_AT_return_addr:
7819 : : case DW_AT_static_link:
7820 : : case DW_AT_segment:
7821 : : case DW_AT_GNU_call_site_value:
7822 : : case DW_AT_GNU_call_site_data_value:
7823 : : case DW_AT_GNU_call_site_target:
7824 : : case DW_AT_GNU_call_site_target_clobbered:
7825 : : case DW_AT_GNU_locviews:
7826 : : {
7827 : 217658 : bool nlpt;
7828 [ + + ]: 217658 : if (cbargs->cu->version < 5)
7829 : : {
7830 [ + + ]: 324 : if (! cbargs->is_split)
7831 : : {
7832 : 284 : nlpt = notice_listptr (section_loc, &known_locsptr,
7833 : 284 : cbargs->addrsize,
7834 : 284 : cbargs->offset_size,
7835 : : cbargs->cu, num, attr);
7836 : : }
7837 : : else
7838 : : nlpt = true;
7839 : : }
7840 : : else
7841 : : {
7842 : : /* Only register for a real section offset. Otherwise
7843 : : it is a DW_FORM_loclistx which is just an index
7844 : : number and we should already have registered the
7845 : : section offset for the index when we saw the
7846 : : DW_AT_loclists_base CU attribute. */
7847 [ + + ]: 217334 : if (form == DW_FORM_sec_offset)
7848 : 217256 : nlpt = notice_listptr (section_loc, &known_loclistsptr,
7849 : 217256 : cbargs->addrsize, cbargs->offset_size,
7850 : : cbargs->cu, num, attr);
7851 : : else
7852 : : nlpt = true;
7853 : :
7854 : : }
7855 : :
7856 [ + + ]: 217658 : if (!cbargs->silent)
7857 : : {
7858 [ + + + + ]: 217292 : if (cbargs->cu->version < 5 || form == DW_FORM_sec_offset)
7859 [ - + ]: 217278 : printf (" %*s%-20s (%s) location list [%6"
7860 : : PRIxMAX "]%s\n",
7861 : : (int) (level * 2), "", dwarf_attr_name (attr),
7862 : : dwarf_form_name (form), (uintmax_t) num,
7863 : : nlpt ? "" : " <WARNING offset too big>");
7864 : : else
7865 : 14 : printf (" %*s%-20s (%s) location index [%6"
7866 : : PRIxMAX "]\n",
7867 : : (int) (level * 2), "", dwarf_attr_name (attr),
7868 : : dwarf_form_name (form), (uintmax_t) num);
7869 : : }
7870 : : }
7871 : : return DWARF_CB_OK;
7872 : :
7873 : 10 : case DW_AT_loclists_base:
7874 : : {
7875 : 20 : bool nlpt = notice_listptr (section_loc, &known_loclistsptr,
7876 : 10 : cbargs->addrsize, cbargs->offset_size,
7877 : : cbargs->cu, num, attr);
7878 : :
7879 [ + + ]: 10 : if (!cbargs->silent)
7880 [ - + ]: 2 : printf (" %*s%-20s (%s) location list [%6" PRIxMAX "]%s\n",
7881 : : (int) (level * 2), "", dwarf_attr_name (attr),
7882 : : dwarf_form_name (form), (uintmax_t) num,
7883 : : nlpt ? "" : " <WARNING offset too big>");
7884 : : }
7885 : : return DWARF_CB_OK;
7886 : :
7887 : 33386 : case DW_AT_ranges:
7888 : : case DW_AT_start_scope:
7889 : : {
7890 : 33386 : bool nlpt;
7891 [ + + ]: 33386 : if (cbargs->cu->version < 5)
7892 : 106 : nlpt = notice_listptr (section_ranges, &known_rangelistptr,
7893 : 106 : cbargs->addrsize, cbargs->offset_size,
7894 : : cbargs->cu, num, attr);
7895 : : else
7896 : : {
7897 : : /* Only register for a real section offset. Otherwise
7898 : : it is a DW_FORM_rangelistx which is just an index
7899 : : number and we should already have registered the
7900 : : section offset for the index when we saw the
7901 : : DW_AT_rnglists_base CU attribute. */
7902 [ + + ]: 33280 : if (form == DW_FORM_sec_offset)
7903 : 33268 : nlpt = notice_listptr (section_ranges, &known_rnglistptr,
7904 : 33268 : cbargs->addrsize, cbargs->offset_size,
7905 : : cbargs->cu, num, attr);
7906 : : else
7907 : : nlpt = true;
7908 : : }
7909 : :
7910 [ + + ]: 33386 : if (!cbargs->silent)
7911 : : {
7912 [ + + + + ]: 33300 : if (cbargs->cu->version < 5 || form == DW_FORM_sec_offset)
7913 [ - + ]: 33296 : printf (" %*s%-20s (%s) range list [%6"
7914 : : PRIxMAX "]%s\n",
7915 : : (int) (level * 2), "", dwarf_attr_name (attr),
7916 : : dwarf_form_name (form), (uintmax_t) num,
7917 : : nlpt ? "" : " <WARNING offset too big>");
7918 : : else
7919 : 4 : printf (" %*s%-20s (%s) range index [%6"
7920 : : PRIxMAX "]\n",
7921 : : (int) (level * 2), "", dwarf_attr_name (attr),
7922 : : dwarf_form_name (form), (uintmax_t) num);
7923 : : }
7924 : : }
7925 : : return DWARF_CB_OK;
7926 : :
7927 : 8 : case DW_AT_rnglists_base:
7928 : : {
7929 : 16 : bool nlpt = notice_listptr (section_ranges, &known_rnglistptr,
7930 : 8 : cbargs->addrsize, cbargs->offset_size,
7931 : : cbargs->cu, num, attr);
7932 [ + + ]: 8 : if (!cbargs->silent)
7933 [ - + ]: 4 : printf (" %*s%-20s (%s) range list [%6"
7934 : : PRIxMAX "]%s\n",
7935 : : (int) (level * 2), "", dwarf_attr_name (attr),
7936 : : dwarf_form_name (form), (uintmax_t) num,
7937 : : nlpt ? "" : " <WARNING offset too big>");
7938 : : }
7939 : : return DWARF_CB_OK;
7940 : :
7941 : 34 : case DW_AT_addr_base:
7942 : : case DW_AT_GNU_addr_base:
7943 : : {
7944 : 68 : bool addrbase = notice_listptr (section_addr, &known_addrbases,
7945 : 34 : cbargs->addrsize,
7946 : 34 : cbargs->offset_size,
7947 : : cbargs->cu, num, attr);
7948 [ + + ]: 34 : if (!cbargs->silent)
7949 [ - + ]: 18 : printf (" %*s%-20s (%s) address base [%6"
7950 : : PRIxMAX "]%s\n",
7951 : : (int) (level * 2), "", dwarf_attr_name (attr),
7952 : : dwarf_form_name (form), (uintmax_t) num,
7953 : : addrbase ? "" : " <WARNING offset too big>");
7954 : : }
7955 : : return DWARF_CB_OK;
7956 : :
7957 : 8 : case DW_AT_str_offsets_base:
7958 : : {
7959 : 16 : bool stroffbase = notice_listptr (section_str, &known_stroffbases,
7960 : 8 : cbargs->addrsize,
7961 : 8 : cbargs->offset_size,
7962 : : cbargs->cu, num, attr);
7963 [ + - ]: 8 : if (!cbargs->silent)
7964 [ - + ]: 797812 : printf (" %*s%-20s (%s) str offsets base [%6"
7965 : : PRIxMAX "]%s\n",
7966 : : (int) (level * 2), "", dwarf_attr_name (attr),
7967 : : dwarf_form_name (form), (uintmax_t) num,
7968 : : stroffbase ? "" : " <WARNING offset too big>");
7969 : : }
7970 : : return DWARF_CB_OK;
7971 : :
7972 : 3676 : case DW_AT_language:
7973 : 3676 : valuestr = dwarf_lang_name (num);
7974 : 3676 : break;
7975 : 4 : case DW_AT_language_name:
7976 : 4 : valuestr = dwarf_lname_name (num);
7977 : 4 : break;
7978 : 56482 : case DW_AT_encoding:
7979 : 56482 : valuestr = dwarf_encoding_name (num);
7980 : 56482 : break;
7981 : 0 : case DW_AT_accessibility:
7982 : 0 : valuestr = dwarf_access_name (num);
7983 : 0 : break;
7984 : 0 : case DW_AT_defaulted:
7985 : 0 : valuestr = dwarf_defaulted_name (num);
7986 : 0 : break;
7987 : 0 : case DW_AT_visibility:
7988 : 0 : valuestr = dwarf_visibility_name (num);
7989 : 0 : break;
7990 : 0 : case DW_AT_virtuality:
7991 : 0 : valuestr = dwarf_virtuality_name (num);
7992 : 0 : break;
7993 : 0 : case DW_AT_identifier_case:
7994 : 0 : valuestr = dwarf_identifier_case_name (num);
7995 : 0 : break;
7996 : 0 : case DW_AT_calling_convention:
7997 : 0 : valuestr = dwarf_calling_convention_name (num);
7998 : 0 : break;
7999 : 7138 : case DW_AT_inline:
8000 : 7138 : valuestr = dwarf_inline_name (num);
8001 : 7138 : break;
8002 : 0 : case DW_AT_ordering:
8003 : 0 : valuestr = dwarf_ordering_name (num);
8004 : 0 : break;
8005 : 968822 : case DW_AT_decl_file:
8006 : : case DW_AT_call_file:
8007 : : {
8008 [ + + ]: 968822 : if (cbargs->silent)
8009 : : break;
8010 : :
8011 : : /* Try to get the actual file, the current interface only
8012 : : gives us full paths, but we only want to show the file
8013 : : name for now. */
8014 : 967974 : Dwarf_Die cudie;
8015 [ + - ]: 967974 : if (dwarf_cu_die (cbargs->cu, &cudie,
8016 : : NULL, NULL, NULL, NULL, NULL, NULL) != NULL)
8017 : : {
8018 : 967974 : Dwarf_Files *files;
8019 : 967974 : size_t nfiles;
8020 [ + - ]: 967974 : if (dwarf_getsrcfiles (&cudie, &files, &nfiles) == 0)
8021 : : {
8022 : 967974 : valuestr = dwarf_filesrc (files, num, NULL, NULL);
8023 [ + - ]: 967974 : if (valuestr != NULL)
8024 : : {
8025 : 967974 : char *filename = strrchr (valuestr, '/');
8026 [ + - ]: 967974 : if (filename != NULL)
8027 : 967974 : valuestr = filename + 1;
8028 : : }
8029 : : else
8030 : 0 : error (0, 0, _("invalid file (%" PRId64 "): %s"),
8031 : : num, dwarf_errmsg (-1));
8032 : : }
8033 : : else
8034 : 0 : error (0, 0, _("no srcfiles for CU [%" PRIx64 "]"),
8035 : : dwarf_dieoffset (&cudie));
8036 : : }
8037 : : else
8038 : 0 : error (0, 0, _("couldn't get DWARF CU: %s"),
8039 : : dwarf_errmsg (-1));
8040 : 967974 : if (valuestr == NULL)
8041 : : valuestr = "???";
8042 : : }
8043 : 967974 : break;
8044 : 26 : case DW_AT_GNU_dwo_id:
8045 : 26 : as_hex_id = true;
8046 : 26 : break;
8047 : :
8048 : : default:
8049 : : /* Nothing. */
8050 : : break;
8051 : : }
8052 : :
8053 [ + + ]: 3609370 : if (cbargs->silent)
8054 : : break;
8055 : :
8056 : : /* When highpc is in constant form it is relative to lowpc.
8057 : : In that case also show the address. */
8058 : 3605660 : Dwarf_Addr highpc;
8059 [ + + + - ]: 3605660 : if (attr == DW_AT_high_pc && dwarf_highpc (die, &highpc) == 0)
8060 : : {
8061 : 24736 : printf (" %*s%-20s (%s) %" PRIuMAX " (",
8062 : : (int) (level * 2), "", dwarf_attr_name (attr),
8063 : : dwarf_form_name (form), (uintmax_t) num);
8064 : 24736 : print_dwarf_addr (cbargs->dwflmod, cbargs->addrsize, highpc, highpc);
8065 : 24736 : printf (")\n");
8066 : : }
8067 : : else
8068 : : {
8069 [ + + ]: 3580924 : if (as_hex_id)
8070 : : {
8071 : 4 : printf (" %*s%-20s (%s) 0x%.16" PRIx64 "\n",
8072 : : (int) (level * 2), "", dwarf_attr_name (attr),
8073 : : dwarf_form_name (form), num);
8074 : : }
8075 : : else
8076 : : {
8077 : 3580920 : Dwarf_Sword snum = 0;
8078 : 3580920 : bool is_signed;
8079 : 3580920 : int bytes = 0;
8080 [ + + ]: 3580920 : if (attr == DW_AT_const_value)
8081 : 348980 : die_type_sign_bytes (die, &is_signed, &bytes);
8082 : : else
8083 : 3231940 : is_signed = (form == DW_FORM_sdata
8084 : 3231940 : || form == DW_FORM_implicit_const);
8085 : :
8086 [ + + ]: 3580920 : if (is_signed)
8087 [ - + ]: 373898 : if (unlikely (dwarf_formsdata (attrp, &snum) != 0))
8088 : 0 : goto attrval_out;
8089 : :
8090 [ + + ]: 3580920 : if (valuestr == NULL)
8091 : : {
8092 : 2546256 : printf (" %*s%-20s (%s) ",
8093 : : (int) (level * 2), "", dwarf_attr_name (attr),
8094 : : dwarf_form_name (form));
8095 : : }
8096 : : else
8097 : : {
8098 : 1034664 : printf (" %*s%-20s (%s) %s (",
8099 : : (int) (level * 2), "", dwarf_attr_name (attr),
8100 : : dwarf_form_name (form), valuestr);
8101 : : }
8102 : :
8103 [ + + + + : 3580920 : switch (bytes)
+ ]
8104 : : {
8105 : 10 : case 1:
8106 [ + + ]: 10 : if (is_signed)
8107 : 2 : printf ("%" PRId8, (int8_t) snum);
8108 : : else
8109 : 8 : printf ("%" PRIu8, (uint8_t) num);
8110 : : break;
8111 : :
8112 : 4 : case 2:
8113 [ + + ]: 4 : if (is_signed)
8114 : 2 : printf ("%" PRId16, (int16_t) snum);
8115 : : else
8116 : 2 : printf ("%" PRIu16, (uint16_t) num);
8117 : : break;
8118 : :
8119 : 258 : case 4:
8120 [ + + ]: 258 : if (is_signed)
8121 : 246 : printf ("%" PRId32, (int32_t) snum);
8122 : : else
8123 : 12 : printf ("%" PRIu32, (uint32_t) num);
8124 : : break;
8125 : :
8126 : 616 : case 8:
8127 [ + + ]: 616 : if (is_signed)
8128 : 2 : printf ("%" PRId64, (int64_t) snum);
8129 : : else
8130 : 614 : printf ("%" PRIu64, (uint64_t) num);
8131 : : break;
8132 : :
8133 : 3580032 : default:
8134 [ + + ]: 3580032 : if (is_signed)
8135 : 373646 : printf ("%" PRIdMAX, (intmax_t) snum);
8136 : : else
8137 : 3206386 : printf ("%" PRIuMAX, (uintmax_t) num);
8138 : : break;
8139 : : }
8140 : :
8141 : : /* Make clear if we switched from a signed encoding to
8142 : : an unsigned value. */
8143 [ + + ]: 3580920 : if (attr == DW_AT_const_value
8144 [ + + ]: 348980 : && (form == DW_FORM_sdata || form == DW_FORM_implicit_const)
8145 [ + + ]: 2578 : && !is_signed)
8146 : 2560 : printf (" (%" PRIdMAX ")", (intmax_t) num);
8147 : :
8148 [ + + ]: 3580920 : if (valuestr == NULL)
8149 : 2546256 : printf ("\n");
8150 : : else
8151 : 3580920 : printf (")\n");
8152 : : }
8153 : : }
8154 : : break;
8155 : :
8156 : 18244 : case DW_FORM_flag:
8157 [ + + ]: 18244 : if (cbargs->silent)
8158 : : break;
8159 : 18096 : bool flag;
8160 [ - + ]: 18096 : if (unlikely (dwarf_formflag (attrp, &flag) != 0))
8161 : 0 : goto attrval_out;
8162 : :
8163 : 18096 : printf (" %*s%-20s (%s) %s\n",
8164 : : (int) (level * 2), "", dwarf_attr_name (attr),
8165 [ + - ]: 18096 : dwarf_form_name (form), flag ? yes_str : no_str);
8166 : : break;
8167 : :
8168 : 156202 : case DW_FORM_flag_present:
8169 [ + + ]: 156202 : if (cbargs->silent)
8170 : : break;
8171 : 155648 : printf (" %*s%-20s (%s) %s\n",
8172 : : (int) (level * 2), "", dwarf_attr_name (attr),
8173 : : dwarf_form_name (form), yes_str);
8174 : : break;
8175 : :
8176 : 220336 : case DW_FORM_exprloc:
8177 : : case DW_FORM_block4:
8178 : : case DW_FORM_block2:
8179 : : case DW_FORM_block1:
8180 : : case DW_FORM_block:
8181 : : case DW_FORM_data16: /* DWARF5 calls this a constant class. */
8182 [ + + ]: 220336 : if (cbargs->silent)
8183 : : break;
8184 : 220074 : Dwarf_Block block;
8185 [ - + ]: 220074 : if (unlikely (dwarf_formblock (attrp, &block) != 0))
8186 : 0 : goto attrval_out;
8187 : :
8188 : 220074 : printf (" %*s%-20s (%s) ",
8189 : : (int) (level * 2), "", dwarf_attr_name (attr),
8190 : : dwarf_form_name (form));
8191 : :
8192 [ + + + ]: 220074 : switch (attr)
8193 : : {
8194 : 94866 : default:
8195 [ - + ]: 94866 : if (form != DW_FORM_exprloc)
8196 : : {
8197 : 0 : print_block (block.length, block.data);
8198 : 0 : break;
8199 : : }
8200 : 125200 : FALLTHROUGH;
8201 : :
8202 : : case DW_AT_location:
8203 : : case DW_AT_data_location:
8204 : : case DW_AT_data_member_location:
8205 : : case DW_AT_vtable_elem_location:
8206 : : case DW_AT_string_length:
8207 : : case DW_AT_use_location:
8208 : : case DW_AT_frame_base:
8209 : : case DW_AT_return_addr:
8210 : : case DW_AT_static_link:
8211 : : case DW_AT_allocated:
8212 : : case DW_AT_associated:
8213 : : case DW_AT_bit_size:
8214 : : case DW_AT_bit_offset:
8215 : : case DW_AT_bit_stride:
8216 : : case DW_AT_byte_size:
8217 : : case DW_AT_byte_stride:
8218 : : case DW_AT_count:
8219 : : case DW_AT_lower_bound:
8220 : : case DW_AT_upper_bound:
8221 : : case DW_AT_GNU_call_site_value:
8222 : : case DW_AT_GNU_call_site_data_value:
8223 : : case DW_AT_GNU_call_site_target:
8224 : : case DW_AT_GNU_call_site_target_clobbered:
8225 [ + + ]: 125200 : if (form == DW_FORM_exprloc
8226 [ + - ]: 140 : || (form != DW_FORM_data16
8227 [ + - ]: 140 : && attrp->cu->version < 4)) /* blocks were expressions. */
8228 : : {
8229 : 220066 : putchar ('\n');
8230 : 220066 : print_ops (cbargs->dwflmod, cbargs->dbg,
8231 : 220066 : 12 + level * 2, 12 + level * 2,
8232 : : cbargs->version, cbargs->addrsize, cbargs->offset_size,
8233 : 220066 : attrp->cu, block.length, block.data);
8234 : : }
8235 : : else
8236 : 0 : print_block (block.length, block.data);
8237 : : break;
8238 : :
8239 : 8 : case DW_AT_discr_list:
8240 [ - + ]: 8 : if (block.length == 0)
8241 : 0 : puts ("<default>");
8242 [ + - ]: 8 : else if (form != DW_FORM_data16)
8243 : : {
8244 : 8 : const unsigned char *readp = block.data;
8245 : 8 : const unsigned char *readendp = readp + block.length;
8246 : :
8247 : : /* See if we are dealing with a signed or unsigned
8248 : : values. If the parent of this variant DIE is a
8249 : : variant_part then it will either have a discriminant
8250 : : which points to the member which type is the
8251 : : discriminant type. Or the variant_part itself has a
8252 : : type representing the discriminant. */
8253 : 8 : bool is_signed = false;
8254 [ + - ]: 8 : if (level > 0)
8255 : : {
8256 : 8 : Dwarf_Die *parent = &cbargs->dies[level - 1];
8257 [ + - ]: 8 : if (dwarf_tag (die) == DW_TAG_variant
8258 [ + - ]: 8 : && dwarf_tag (parent) == DW_TAG_variant_part)
8259 : : {
8260 : 8 : Dwarf_Die member;
8261 : 8 : Dwarf_Attribute discr_attr;
8262 : 8 : int bytes;
8263 [ + - ]: 8 : if (dwarf_formref_die (dwarf_attr (parent,
8264 : : DW_AT_discr,
8265 : : &discr_attr),
8266 : : &member) != NULL)
8267 : 8 : die_type_sign_bytes (&member, &is_signed, &bytes);
8268 : : else
8269 : 0 : die_type_sign_bytes (parent, &is_signed, &bytes);
8270 : : }
8271 : : }
8272 : 24 : while (readp < readendp)
8273 : : {
8274 : 16 : int d = (int) *readp++;
8275 : 16 : printf ("%s ", dwarf_discr_list_name (d));
8276 [ - + ]: 16 : if (readp >= readendp)
8277 : 0 : goto attrval_out;
8278 : :
8279 : 16 : Dwarf_Word val;
8280 : 16 : Dwarf_Sword sval;
8281 [ + + ]: 16 : if (d == DW_DSC_label)
8282 : : {
8283 [ + + ]: 8 : if (is_signed)
8284 : : {
8285 : 4 : get_sleb128 (sval, readp, readendp);
8286 : 4 : printf ("%" PRId64 "", sval);
8287 : : }
8288 : : else
8289 : : {
8290 : 4 : get_uleb128 (val, readp, readendp);
8291 : 4 : printf ("%" PRIu64 "", val);
8292 : : }
8293 : : }
8294 [ + - ]: 8 : else if (d == DW_DSC_range)
8295 : : {
8296 [ + + ]: 8 : if (is_signed)
8297 : : {
8298 : 6 : get_sleb128 (sval, readp, readendp);
8299 : 6 : printf ("%" PRId64 "..", sval);
8300 [ - + ]: 6 : if (readp >= readendp)
8301 : 0 : goto attrval_out;
8302 : 6 : get_sleb128 (sval, readp, readendp);
8303 : 6 : printf ("%" PRId64 "", sval);
8304 : : }
8305 : : else
8306 : : {
8307 : 2 : get_uleb128 (val, readp, readendp);
8308 : 2 : printf ("%" PRIu64 "..", val);
8309 [ - + ]: 2 : if (readp >= readendp)
8310 : 0 : goto attrval_out;
8311 : 2 : get_uleb128 (val, readp, readendp);
8312 : 2 : printf ("%" PRIu64 "", val);
8313 : : }
8314 : : }
8315 : : else
8316 : : {
8317 : 0 : print_block (readendp - readp, readp);
8318 : 0 : break;
8319 : : }
8320 [ + + ]: 16 : if (readp < readendp)
8321 [ + + ]: 32 : printf (", ");
8322 : : }
8323 : 8 : putchar ('\n');
8324 : : }
8325 : : else
8326 : 0 : print_block (block.length, block.data);
8327 : : break;
8328 : : }
8329 : : break;
8330 : :
8331 : 0 : default:
8332 [ # # ]: 0 : if (cbargs->silent)
8333 : : break;
8334 : 0 : printf (" %*s%-20s (%s) ???\n",
8335 : : (int) (level * 2), "", dwarf_attr_name (attr),
8336 : : dwarf_form_name (form));
8337 : : break;
8338 : : }
8339 : :
8340 : 7136196 : return DWARF_CB_OK;
8341 : : }
8342 : :
8343 : : static void
8344 : 208 : print_debug_units (Dwfl_Module *dwflmod,
8345 : : Ebl *ebl, GElf_Ehdr *ehdr __attribute__ ((unused)),
8346 : : Elf_Scn *scn, GElf_Shdr *shdr,
8347 : : Dwarf *dbg, bool debug_types)
8348 : : {
8349 [ + + + + ]: 208 : const bool silent = !(print_debug_sections & section_info) && !debug_types;
8350 : 208 : const char *secname = section_name (ebl, shdr);
8351 : :
8352 : : /* Check section actually exists. */
8353 [ + + ]: 208 : if (!silent)
8354 [ + - ]: 136 : if (get_debug_elf_data (dbg, ebl,
8355 : : debug_types ? IDX_debug_types : IDX_debug_info,
8356 : : scn) == NULL)
8357 : 0 : return;
8358 : :
8359 : 136 : if (!silent)
8360 : 136 : printf (_("\
8361 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n [Offset]\n"),
8362 : 136 : elf_ndxscn (scn), secname, (uint64_t) shdr->sh_offset);
8363 : :
8364 : : /* If the section is empty we don't have to do anything. */
8365 [ + + + - ]: 208 : if (!silent && shdr->sh_size == 0)
8366 : : return;
8367 : :
8368 : 208 : int maxdies = 20;
8369 : 208 : Dwarf_Die *dies = xmalloc (maxdies * sizeof (Dwarf_Die));
8370 : :
8371 : : /* New compilation unit. */
8372 : 208 : Dwarf_Half version;
8373 : :
8374 : 208 : Dwarf_Die result;
8375 : 208 : Dwarf_Off abbroffset;
8376 : 208 : uint8_t addrsize;
8377 : 208 : uint8_t offsize;
8378 : 208 : uint64_t unit_id;
8379 : 208 : Dwarf_Off subdie_off;
8380 : :
8381 : 208 : int unit_res;
8382 : 208 : Dwarf_CU *cu;
8383 : 208 : Dwarf_CU cu_mem;
8384 : 208 : uint8_t unit_type;
8385 : 208 : Dwarf_Die cudie;
8386 : :
8387 : : /* We cheat a little because we want to see only the CUs from .debug_info
8388 : : or .debug_types. We know the Dwarf_CU struct layout. Set it up at
8389 : : the end of .debug_info if we want .debug_types only. Check the returned
8390 : : Dwarf_CU is still in the expected section. */
8391 [ + + ]: 208 : if (debug_types)
8392 : : {
8393 : 2 : cu_mem.dbg = dbg;
8394 : 2 : cu_mem.end = dbg->sectiondata[IDX_debug_info]->d_size;
8395 : 2 : cu_mem.sec_idx = IDX_debug_info;
8396 : 2 : cu = &cu_mem;
8397 : : }
8398 : : else
8399 : 206 : cu = NULL;
8400 : :
8401 : : next_cu:
8402 : 3902 : unit_res = dwarf_get_units (dbg, cu, &cu, &version, &unit_type,
8403 : : &cudie, NULL);
8404 [ + + ]: 3902 : if (unit_res == 1)
8405 : 206 : goto do_return;
8406 : :
8407 [ - + ]: 3696 : if (unit_res == -1)
8408 : : {
8409 [ # # ]: 0 : if (!silent)
8410 : 0 : error (0, 0, _("cannot get next unit: %s"), dwarf_errmsg (-1));
8411 : 0 : goto do_return;
8412 : : }
8413 : :
8414 [ + + + + ]: 7388 : if (cu->sec_idx != (size_t) (debug_types ? IDX_debug_types : IDX_debug_info))
8415 : 2 : goto do_return;
8416 : :
8417 : 3694 : dwarf_cu_die (cu, &result, NULL, &abbroffset, &addrsize, &offsize,
8418 : : &unit_id, &subdie_off);
8419 : :
8420 [ + + ]: 3694 : if (!silent)
8421 : : {
8422 : 3594 : Dwarf_Off offset = cu->start;
8423 [ + + + - ]: 3594 : if (debug_types && version < 5)
8424 : 4 : {
8425 : 4 : Dwarf_Die typedie;
8426 : 4 : Dwarf_Off dieoffset;
8427 : 4 : dieoffset = dwarf_dieoffset (dwarf_offdie_types (dbg, cu->start
8428 : : + subdie_off,
8429 : : &typedie));
8430 : 4 : printf (_(" Type unit at offset %" PRIu64 ":\n"
8431 : : " Version: %" PRIu16
8432 : : ", Abbreviation section offset: %" PRIu64
8433 : : ", Address size: %" PRIu8
8434 : : ", Offset size: %" PRIu8
8435 : : "\n Type signature: %#" PRIx64
8436 : : ", Type offset: %#" PRIx64 " [%" PRIx64 "]\n"),
8437 : : (uint64_t) offset, version, abbroffset, addrsize, offsize,
8438 : : unit_id, (uint64_t) subdie_off, dieoffset);
8439 : : }
8440 : : else
8441 : : {
8442 : 3590 : printf (_(" Compilation unit at offset %" PRIu64 ":\n"
8443 : : " Version: %" PRIu16
8444 : : ", Abbreviation section offset: %" PRIu64
8445 : : ", Address size: %" PRIu8
8446 : : ", Offset size: %" PRIu8 "\n"),
8447 : : (uint64_t) offset, version, abbroffset, addrsize, offsize);
8448 : :
8449 [ + + ]: 3590 : if (version >= 5 || (unit_type != DW_UT_compile
8450 [ + + ]: 170 : && unit_type != DW_UT_partial))
8451 : : {
8452 : 3422 : printf (_(" Unit type: %s (%" PRIu8 ")"),
8453 : : dwarf_unit_name (unit_type), unit_type);
8454 : 3422 : if (unit_type == DW_UT_type
8455 [ + + ]: 3422 : || unit_type == DW_UT_skeleton
8456 [ + - ]: 3412 : || unit_type == DW_UT_split_compile
8457 [ - + ]: 3412 : || unit_type == DW_UT_split_type)
8458 : 10 : printf (", Unit id: 0x%.16" PRIx64 "", unit_id);
8459 : 3422 : if (unit_type == DW_UT_type
8460 [ - + ]: 3422 : || unit_type == DW_UT_split_type)
8461 : : {
8462 : 0 : Dwarf_Die typedie;
8463 : 0 : Dwarf_Off dieoffset;
8464 : 0 : dwarf_cu_info (cu, NULL, NULL, NULL, &typedie,
8465 : : NULL, NULL, NULL);
8466 : 0 : dieoffset = dwarf_dieoffset (&typedie);
8467 : 0 : printf (", Unit DIE off: %#" PRIx64 " [%" PRIx64 "]",
8468 : : subdie_off, dieoffset);
8469 : : }
8470 : 3422 : printf ("\n");
8471 : : }
8472 : : }
8473 : : }
8474 : :
8475 [ + - ]: 3694 : if (version < 2 || version > 5
8476 [ + - - + ]: 3694 : || unit_type < DW_UT_compile || unit_type > DW_UT_split_type)
8477 : : {
8478 [ # # ]: 0 : if (!silent)
8479 : 0 : error (0, 0, _("unknown version (%d) or unit type (%d)"),
8480 : : version, unit_type);
8481 : 0 : goto next_cu;
8482 : : }
8483 : :
8484 : 3694 : struct attrcb_args args =
8485 : : {
8486 : : .dwflmod = dwflmod,
8487 : : .silent = silent,
8488 : : .version = version,
8489 : : .addrsize = addrsize,
8490 : : .offset_size = offsize
8491 : : };
8492 : :
8493 : 3694 : bool is_split = false;
8494 : 3694 : int level = 0;
8495 : 3694 : dies[0] = cudie;
8496 : 3694 : args.cu = dies[0].cu;
8497 : 3694 : args.dbg = dbg;
8498 : 3694 : args.is_split = is_split;
8499 : :
8500 : : /* We might return here again for the split CU subdie. */
8501 : : do_cu:
8502 : 2084854 : do
8503 : : {
8504 : 2084854 : Dwarf_Off offset = dwarf_dieoffset (&dies[level]);
8505 [ - + ]: 2084854 : if (unlikely (offset == (Dwarf_Off) -1))
8506 : : {
8507 [ # # ]: 0 : if (!silent)
8508 : 0 : error (0, 0, _("cannot get DIE offset: %s"),
8509 : : dwarf_errmsg (-1));
8510 : 0 : goto do_return;
8511 : : }
8512 : :
8513 : 2084854 : int tag = dwarf_tag (&dies[level]);
8514 [ - + ]: 2084854 : if (unlikely (tag == DW_TAG_invalid))
8515 : : {
8516 [ # # ]: 0 : if (!silent)
8517 : 0 : error (0, 0, _("cannot get tag of DIE at offset [%" PRIx64
8518 : : "] in section '%s': %s"),
8519 : : (uint64_t) offset, secname, dwarf_errmsg (-1));
8520 : 0 : goto do_return;
8521 : : }
8522 : :
8523 [ + + ]: 2084854 : if (!silent)
8524 : : {
8525 : 2082868 : unsigned int code = dwarf_getabbrevcode (dies[level].abbrev);
8526 [ + + ]: 2082868 : if (is_split)
8527 : 96 : printf (" {%6" PRIx64 "} ", (uint64_t) offset);
8528 : : else
8529 : 2082772 : printf (" [%6" PRIx64 "] ", (uint64_t) offset);
8530 : 2082868 : printf ("%*s%-20s abbrev: %u\n", (int) (level * 2), "",
8531 : : dwarf_tag_name (tag), code);
8532 : : }
8533 : :
8534 : : /* Print the attribute values. */
8535 : 2084854 : args.level = level;
8536 : 2084854 : args.dies = dies;
8537 : 2084854 : (void) dwarf_getattrs (&dies[level], attr_callback, &args, 0);
8538 : :
8539 : : /* Make room for the next level's DIE. */
8540 [ - + ]: 2084854 : if (level + 1 == maxdies)
8541 : 0 : dies = xrealloc (dies, (maxdies += 10) * sizeof (Dwarf_Die));
8542 : :
8543 : 2084854 : int res = dwarf_child (&dies[level], &dies[level + 1]);
8544 [ + + ]: 2084854 : if (res > 0)
8545 : : {
8546 [ + + ]: 2084854 : while ((res = dwarf_siblingof (&dies[level], &dies[level])) == 1)
8547 [ + + ]: 288812 : if (level-- == 0)
8548 : : break;
8549 : :
8550 [ - + ]: 1799744 : if (unlikely (res == -1))
8551 : : {
8552 [ # # ]: 0 : if (!silent)
8553 : 0 : error (0, 0, _("cannot get next DIE: %s\n"),
8554 : : dwarf_errmsg (-1));
8555 : 0 : goto do_return;
8556 : : }
8557 : : }
8558 [ - + ]: 285110 : else if (unlikely (res < 0))
8559 : : {
8560 [ # # ]: 0 : if (!silent)
8561 : 0 : error (0, 0, _("cannot get next DIE: %s"),
8562 : : dwarf_errmsg (-1));
8563 : 0 : goto do_return;
8564 : : }
8565 : : else
8566 : : ++level;
8567 : : }
8568 [ + + ]: 2084854 : while (level >= 0);
8569 : :
8570 : : /* We might want to show the split compile unit if this was a skeleton.
8571 : : We need to scan it if we are requesting printing .debug_ranges for
8572 : : DWARF4 since GNU DebugFission uses "offsets" into the main ranges
8573 : : section. */
8574 [ + + ]: 3702 : if (unit_type == DW_UT_skeleton
8575 [ + + + + ]: 26 : && ((!silent && show_split_units)
8576 [ + + + + ]: 20 : || (version < 5 && (print_debug_sections & section_ranges) != 0)))
8577 : : {
8578 : 10 : Dwarf_Die subdie;
8579 [ + - ]: 10 : if (dwarf_cu_info (cu, NULL, NULL, NULL, &subdie, NULL, NULL, NULL) != 0
8580 [ + + ]: 10 : || dwarf_tag (&subdie) == DW_TAG_invalid)
8581 : : {
8582 [ + - ]: 2 : if (!silent)
8583 : : {
8584 : 2 : Dwarf_Attribute dwo_at;
8585 : 4 : const char *dwo_name =
8586 : 2 : (dwarf_formstring (dwarf_attr (&cudie, DW_AT_dwo_name,
8587 : : &dwo_at))
8588 [ - + ]: 2 : ?: (dwarf_formstring (dwarf_attr (&cudie, DW_AT_GNU_dwo_name,
8589 : : &dwo_at))
8590 [ # # ]: 0 : ?: "<unknown>"));
8591 : 2 : fprintf (stderr,
8592 : : "Could not find split unit '%s', id: %" PRIx64 "\n",
8593 : : dwo_name, unit_id);
8594 : : }
8595 : : }
8596 : : else
8597 : : {
8598 : 8 : Dwarf_CU *split_cu = subdie.cu;
8599 : 8 : dwarf_cu_die (split_cu, &result, NULL, &abbroffset,
8600 : : &addrsize, &offsize, &unit_id, &subdie_off);
8601 : 8 : Dwarf_Off offset = cu->start;
8602 : :
8603 [ + + ]: 8 : if (!silent)
8604 : : {
8605 : 4 : printf (_(" Split compilation unit at offset %"
8606 : : PRIu64 ":\n"
8607 : : " Version: %" PRIu16
8608 : : ", Abbreviation section offset: %" PRIu64
8609 : : ", Address size: %" PRIu8
8610 : : ", Offset size: %" PRIu8 "\n"),
8611 : : (uint64_t) offset, version, abbroffset,
8612 : : addrsize, offsize);
8613 : 4 : printf (_(" Unit type: %s (%" PRIu8 ")"),
8614 : : dwarf_unit_name (unit_type), unit_type);
8615 : 4 : printf (", Unit id: 0x%.16" PRIx64 "", unit_id);
8616 : 4 : printf ("\n");
8617 : : }
8618 : :
8619 : 8 : unit_type = DW_UT_split_compile;
8620 : 8 : is_split = true;
8621 : 8 : level = 0;
8622 : 8 : dies[0] = subdie;
8623 : 8 : args.cu = dies[0].cu;
8624 : 8 : args.dbg = split_cu->dbg;
8625 : 8 : args.is_split = is_split;
8626 : 8 : goto do_cu;
8627 : : }
8628 : : }
8629 : :
8630 : : /* And again... */
8631 : 3694 : goto next_cu;
8632 : :
8633 : 208 : do_return:
8634 : 208 : free (dies);
8635 : : }
8636 : :
8637 : : static void
8638 : 206 : print_debug_info_section (Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr,
8639 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
8640 : : {
8641 : 42 : print_debug_units (dwflmod, ebl, ehdr, scn, shdr, dbg, false);
8642 : 42 : }
8643 : :
8644 : : static void
8645 : 2 : print_debug_types_section (Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr,
8646 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
8647 : : {
8648 : 2 : print_debug_units (dwflmod, ebl, ehdr, scn, shdr, dbg, true);
8649 : 2 : }
8650 : :
8651 : :
8652 : : static void
8653 : 82 : print_decoded_line_section (Dwfl_Module *dwflmod, Ebl *ebl,
8654 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
8655 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
8656 : : {
8657 : 82 : printf (_("\
8658 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n\n"),
8659 : : elf_ndxscn (scn), section_name (ebl, shdr),
8660 : 82 : (uint64_t) shdr->sh_offset);
8661 : :
8662 : 164 : size_t address_size
8663 [ + + ]: 82 : = elf_getident (ebl->elf, NULL)[EI_CLASS] == ELFCLASS32 ? 4 : 8;
8664 : :
8665 : 82 : Dwarf_Lines *lines;
8666 : 82 : size_t nlines;
8667 : 82 : Dwarf_Off off, next_off = 0;
8668 : 82 : Dwarf_CU *cu = NULL;
8669 : 82 : while (dwarf_next_lines (dbg, off = next_off, &next_off, &cu, NULL, NULL,
8670 [ + + ]: 176 : &lines, &nlines) == 0)
8671 : : {
8672 : 94 : Dwarf_Die cudie;
8673 [ + + + - ]: 94 : if (cu != NULL && dwarf_cu_info (cu, NULL, NULL, &cudie,
8674 : : NULL, NULL, NULL, NULL) == 0)
8675 : 50 : printf (" CU [%" PRIx64 "] %s\n",
8676 : : dwarf_dieoffset (&cudie), dwarf_diename (&cudie));
8677 : : else
8678 : : {
8679 : : /* DWARF5 lines can be independent of any CU, but they probably
8680 : : are used by some CU. Determine the CU this block is for. */
8681 : 44 : Dwarf_Off cuoffset;
8682 : 44 : Dwarf_Off ncuoffset = 0;
8683 : 44 : size_t hsize;
8684 : 44 : while (dwarf_nextcu (dbg, cuoffset = ncuoffset, &ncuoffset, &hsize,
8685 [ + - ]: 46 : NULL, NULL, NULL) == 0)
8686 : : {
8687 [ - + ]: 46 : if (dwarf_offdie (dbg, cuoffset + hsize, &cudie) == NULL)
8688 : 0 : continue;
8689 : 46 : Dwarf_Attribute stmt_list;
8690 [ - + ]: 46 : if (dwarf_attr (&cudie, DW_AT_stmt_list, &stmt_list) == NULL)
8691 : 0 : continue;
8692 : 46 : Dwarf_Word lineoff;
8693 [ - + ]: 46 : if (dwarf_formudata (&stmt_list, &lineoff) != 0)
8694 : 0 : continue;
8695 [ + + ]: 46 : if (lineoff == off)
8696 : : {
8697 : : /* Found the CU. */
8698 : 44 : cu = cudie.cu;
8699 : 44 : break;
8700 : : }
8701 : : }
8702 : :
8703 [ + - ]: 44 : if (cu != NULL)
8704 : 44 : printf (" CU [%" PRIx64 "] %s\n",
8705 : : dwarf_dieoffset (&cudie), dwarf_diename (&cudie));
8706 : : else
8707 : 44 : printf (" No CU\n");
8708 : : }
8709 : :
8710 : 94 : printf (" line:col SBPE* disc isa op address"
8711 : : " (Statement Block Prologue Epilogue *End)\n");
8712 : 94 : const char *last_file = "";
8713 [ + + ]: 720 : for (size_t n = 0; n < nlines; n++)
8714 : : {
8715 : 626 : Dwarf_Line *line = dwarf_onesrcline (lines, n);
8716 [ - + ]: 626 : if (line == NULL)
8717 : : {
8718 : 0 : printf (" dwarf_onesrcline: %s\n", dwarf_errmsg (-1));
8719 : 0 : continue;
8720 : : }
8721 : 626 : Dwarf_Word mtime, length;
8722 : 626 : const char *file = dwarf_linesrc (line, &mtime, &length);
8723 [ - + ]: 626 : if (file == NULL)
8724 : : {
8725 : 0 : printf (" <%s> (mtime: ?, length: ?)\n", dwarf_errmsg (-1));
8726 : 0 : last_file = "";
8727 : : }
8728 [ + + ]: 626 : else if (strcmp (last_file, file) != 0)
8729 : : {
8730 : 114 : printf (" %s (mtime: %" PRIu64 ", length: %" PRIu64 ")\n",
8731 : : file, mtime, length);
8732 : 114 : last_file = file;
8733 : : }
8734 : :
8735 : 626 : int lineno, colno;
8736 : 626 : bool statement, endseq, block, prologue_end, epilogue_begin;
8737 : 626 : unsigned int lineop, isa, disc;
8738 : 626 : Dwarf_Addr address;
8739 : 626 : dwarf_lineaddr (line, &address);
8740 : 626 : dwarf_lineno (line, &lineno);
8741 : 626 : dwarf_linecol (line, &colno);
8742 : 626 : dwarf_lineop_index (line, &lineop);
8743 : 626 : dwarf_linebeginstatement (line, &statement);
8744 : 626 : dwarf_lineendsequence (line, &endseq);
8745 : 626 : dwarf_lineblock (line, &block);
8746 : 626 : dwarf_lineprologueend (line, &prologue_end);
8747 : 626 : dwarf_lineepiloguebegin (line, &epilogue_begin);
8748 : 626 : dwarf_lineisa (line, &isa);
8749 : 626 : dwarf_linediscriminator (line, &disc);
8750 : :
8751 : : /* End sequence is special, it is one byte past. */
8752 : 626 : printf (" %4d:%-3d %c%c%c%c%c %4d %3d %2d ",
8753 : : lineno, colno,
8754 [ + + ]: 626 : (statement ? 'S' : ' '),
8755 [ + - ]: 626 : (block ? 'B' : ' '),
8756 [ + + ]: 626 : (prologue_end ? 'P' : ' '),
8757 [ + - ]: 626 : (epilogue_begin ? 'E' : ' '),
8758 [ + + ]: 626 : (endseq ? '*' : ' '),
8759 : : disc, isa, lineop);
8760 : 626 : print_dwarf_addr (dwflmod, address_size,
8761 [ + + ]: 626 : address - (endseq ? 1 : 0), address);
8762 : 626 : printf ("\n");
8763 : :
8764 [ + + ]: 626 : if (endseq)
8765 : 626 : printf("\n");
8766 : : }
8767 : : }
8768 : 82 : }
8769 : :
8770 : :
8771 : : /* Print the value of a form.
8772 : : Returns new value of readp, or readendp on failure. */
8773 : : static const unsigned char *
8774 : 141246 : print_form_data (Dwarf *dbg, int form, const unsigned char *readp,
8775 : : const unsigned char *readendp, unsigned int offset_len,
8776 : : Dwarf_Off str_offsets_base)
8777 : : {
8778 : 141246 : Dwarf_Word val;
8779 : 141246 : unsigned char *endp;
8780 : 141246 : Elf_Data *data;
8781 : 141246 : char *str;
8782 [ + - - - : 141246 : switch (form)
- + - - -
- + - - +
- + - - -
- - ]
8783 : : {
8784 : 16 : case DW_FORM_data1:
8785 [ - + ]: 16 : if (readendp - readp < 1)
8786 : : {
8787 : 0 : invalid_data:
8788 : 0 : error (0, 0, "invalid data");
8789 : 0 : return readendp;
8790 : : }
8791 : 16 : val = *readp++;
8792 : 16 : printf (" %" PRIx8, (unsigned int) val);
8793 : : break;
8794 : :
8795 : 0 : case DW_FORM_data2:
8796 [ # # ]: 0 : if (readendp - readp < 2)
8797 : 0 : goto invalid_data;
8798 [ # # ]: 0 : val = read_2ubyte_unaligned_inc (dbg, readp);
8799 : 0 : printf(" %" PRIx16, (unsigned int) val);
8800 : : break;
8801 : :
8802 : 0 : case DW_FORM_data4:
8803 [ # # ]: 0 : if (readendp - readp < 4)
8804 : 0 : goto invalid_data;
8805 [ # # ]: 0 : val = read_4ubyte_unaligned_inc (dbg, readp);
8806 : 0 : printf (" %" PRIx32, (unsigned int) val);
8807 : : break;
8808 : :
8809 : 0 : case DW_FORM_data8:
8810 [ # # ]: 0 : if (readendp - readp < 8)
8811 : 0 : goto invalid_data;
8812 [ # # ]: 0 : val = read_8ubyte_unaligned_inc (dbg, readp);
8813 : 0 : printf (" %" PRIx64, val);
8814 : : break;
8815 : :
8816 : 0 : case DW_FORM_sdata:
8817 [ # # ]: 0 : if (readendp - readp < 1)
8818 : 0 : goto invalid_data;
8819 : 0 : get_sleb128 (val, readp, readendp);
8820 : 0 : printf (" %" PRIx64, val);
8821 : : break;
8822 : :
8823 : 56756 : case DW_FORM_udata:
8824 [ - + ]: 56756 : if (readendp - readp < 1)
8825 : 0 : goto invalid_data;
8826 : 56756 : get_uleb128 (val, readp, readendp);
8827 : 56756 : printf (" %" PRIx64, val);
8828 : : break;
8829 : :
8830 : 0 : case DW_FORM_block:
8831 [ # # ]: 0 : if (readendp - readp < 1)
8832 : 0 : goto invalid_data;
8833 : 0 : get_uleb128 (val, readp, readendp);
8834 [ # # ]: 0 : if ((size_t) (readendp - readp) < val)
8835 : 0 : goto invalid_data;
8836 : 0 : print_bytes (val, readp);
8837 : 0 : readp += val;
8838 : 0 : break;
8839 : :
8840 : 0 : case DW_FORM_block1:
8841 [ # # ]: 0 : if (readendp - readp < 1)
8842 : 0 : goto invalid_data;
8843 : 0 : val = *readp++;
8844 [ # # ]: 0 : if ((size_t) (readendp - readp) < val)
8845 : 0 : goto invalid_data;
8846 : 0 : print_bytes (val, readp);
8847 : 0 : readp += val;
8848 : 0 : break;
8849 : :
8850 : 0 : case DW_FORM_block2:
8851 [ # # ]: 0 : if (readendp - readp < 2)
8852 : 0 : goto invalid_data;
8853 [ # # ]: 0 : val = read_2ubyte_unaligned_inc (dbg, readp);
8854 [ # # ]: 0 : if ((size_t) (readendp - readp) < val)
8855 : 0 : goto invalid_data;
8856 : 0 : print_bytes (val, readp);
8857 : 0 : readp += val;
8858 : 0 : break;
8859 : :
8860 : 0 : case DW_FORM_block4:
8861 [ # # ]: 0 : if (readendp - readp < 4)
8862 : 0 : goto invalid_data;
8863 [ # # ]: 0 : val = read_4ubyte_unaligned_inc (dbg, readp);
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 : 24 : case DW_FORM_data16:
8871 [ - + ]: 24 : if (readendp - readp < 16)
8872 : 0 : goto invalid_data;
8873 : 24 : print_bytes (16, readp);
8874 : 24 : readp += 16;
8875 : 24 : break;
8876 : :
8877 : 0 : case DW_FORM_flag:
8878 [ # # ]: 0 : if (readendp - readp < 1)
8879 : 0 : goto invalid_data;
8880 : 0 : val = *readp++;
8881 [ # # ]: 0 : printf ("%s", val != 0 ? yes_str : no_str);
8882 : : break;
8883 : :
8884 : 0 : case DW_FORM_string:
8885 : 0 : endp = memchr (readp, '\0', readendp - readp);
8886 [ # # ]: 0 : if (endp == NULL)
8887 : 0 : goto invalid_data;
8888 : 0 : printf ("%s", readp);
8889 : 0 : readp = endp + 1;
8890 : 0 : break;
8891 : :
8892 : 82894 : case DW_FORM_strp:
8893 : : case DW_FORM_line_strp:
8894 : : case DW_FORM_strp_sup:
8895 [ - + ]: 82894 : if ((size_t) (readendp - readp) < offset_len)
8896 : 0 : goto invalid_data;
8897 [ - + ]: 82894 : if (offset_len == 8)
8898 [ # # ]: 0 : val = read_8ubyte_unaligned_inc (dbg, readp);
8899 : : else
8900 [ - + ]: 82894 : val = read_4ubyte_unaligned_inc (dbg, readp);
8901 [ - + ]: 82894 : if (form == DW_FORM_strp)
8902 : 0 : data = dbg->sectiondata[IDX_debug_str];
8903 [ + - ]: 82894 : else if (form == DW_FORM_line_strp)
8904 : 82894 : data = dbg->sectiondata[IDX_debug_line_str];
8905 : : else /* form == DW_FORM_strp_sup */
8906 : : {
8907 : 0 : Dwarf *alt = dwarf_getalt (dbg);
8908 [ # # ]: 0 : data = alt != NULL ? alt->sectiondata[IDX_debug_str] : NULL;
8909 : : }
8910 [ + - - + ]: 82894 : if (data == NULL || val >= data->d_size
8911 [ - + ]: 82894 : || memchr (data->d_buf + val, '\0', data->d_size - val) == NULL)
8912 : : str = "???";
8913 : : else
8914 : 82894 : str = (char *) data->d_buf + val;
8915 : 82894 : printf ("%s (%" PRIu64 ")", str, val);
8916 : : break;
8917 : :
8918 : 0 : case DW_FORM_sec_offset:
8919 [ # # ]: 0 : if ((size_t) (readendp - readp) < offset_len)
8920 : 0 : goto invalid_data;
8921 [ # # ]: 0 : if (offset_len == 8)
8922 [ # # ]: 0 : val = read_8ubyte_unaligned_inc (dbg, readp);
8923 : : else
8924 [ # # ]: 0 : val = read_4ubyte_unaligned_inc (dbg, readp);
8925 : 0 : printf ("[%" PRIx64 "]", val);
8926 : : break;
8927 : :
8928 : 1556 : case DW_FORM_strx:
8929 : : case DW_FORM_GNU_str_index:
8930 [ - + ]: 1556 : if (readendp - readp < 1)
8931 : 0 : goto invalid_data;
8932 : 1556 : get_uleb128 (val, readp, readendp);
8933 : 1556 : strx_val:
8934 : 1556 : data = dbg->sectiondata[IDX_debug_str_offsets];
8935 [ + - ]: 1556 : if (data == NULL
8936 [ - + ]: 1556 : || data->d_size - str_offsets_base < val * offset_len)
8937 : : str = "???";
8938 : : else
8939 : : {
8940 : 1556 : const unsigned char *strreadp = (data->d_buf + str_offsets_base
8941 : 1556 : + val * offset_len);
8942 : 1556 : const unsigned char *strreadendp = data->d_buf + data->d_size;
8943 [ - + ]: 1556 : if ((size_t) (strreadendp - strreadp) < offset_len)
8944 : : str = "???";
8945 : : else
8946 : : {
8947 : 1556 : Dwarf_Off idx;
8948 [ - + ]: 1556 : if (offset_len == 8)
8949 [ # # ]: 0 : idx = read_8ubyte_unaligned (dbg, strreadp);
8950 : : else
8951 [ - + ]: 1556 : idx = read_4ubyte_unaligned (dbg, strreadp);
8952 : :
8953 : 1556 : data = dbg->sectiondata[IDX_debug_str];
8954 [ - + - + ]: 1556 : if (data == NULL || idx >= data->d_size
8955 [ - + ]: 1556 : || memchr (data->d_buf + idx, '\0',
8956 : : data->d_size - idx) == NULL)
8957 : : str = "???";
8958 : : else
8959 : 1556 : str = (char *) data->d_buf + idx;
8960 : : }
8961 : : }
8962 : 1556 : printf ("%s (%" PRIu64 ")", str, val);
8963 : : break;
8964 : :
8965 : 0 : case DW_FORM_strx1:
8966 [ # # ]: 0 : if (readendp - readp < 1)
8967 : 0 : goto invalid_data;
8968 : 0 : val = *readp++;
8969 : 0 : goto strx_val;
8970 : :
8971 : 0 : case DW_FORM_strx2:
8972 [ # # ]: 0 : if (readendp - readp < 2)
8973 : 0 : goto invalid_data;
8974 [ # # ]: 0 : val = read_2ubyte_unaligned_inc (dbg, readp);
8975 : 0 : goto strx_val;
8976 : :
8977 : 0 : case DW_FORM_strx3:
8978 [ # # ]: 0 : if (readendp - readp < 3)
8979 : 0 : goto invalid_data;
8980 : 0 : val = read_3ubyte_unaligned_inc (dbg, readp);
8981 : 0 : goto strx_val;
8982 : :
8983 : 0 : case DW_FORM_strx4:
8984 [ # # ]: 0 : if (readendp - readp < 4)
8985 : 0 : goto invalid_data;
8986 [ # # ]: 0 : val = read_4ubyte_unaligned_inc (dbg, readp);
8987 : 0 : goto strx_val;
8988 : :
8989 : 0 : default:
8990 : 0 : error (0, 0, _("unknown form: %s"), dwarf_form_name (form));
8991 : 0 : return readendp;
8992 : : }
8993 : :
8994 : 141246 : return readp;
8995 : : }
8996 : :
8997 : : /* Only used via run_advance_pc() macro */
8998 : : static inline void
8999 : 752392 : run_advance_pc (unsigned int op_advance,
9000 : : unsigned int minimum_instr_len,
9001 : : unsigned int max_ops_per_instr,
9002 : : unsigned int *op_addr_advance,
9003 : : Dwarf_Word *address,
9004 : : unsigned int *op_index)
9005 : : {
9006 : 752392 : const unsigned int advanced_op_index = (*op_index) + op_advance;
9007 : :
9008 : 752392 : *op_addr_advance = minimum_instr_len * (advanced_op_index
9009 : 752392 : / max_ops_per_instr);
9010 : 752392 : *address = *address + *op_addr_advance;
9011 : 752392 : *op_index = advanced_op_index % max_ops_per_instr;
9012 : : }
9013 : :
9014 : : static void
9015 : 186 : print_debug_line_section (Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr,
9016 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
9017 : : {
9018 [ + + ]: 186 : if (decodedline)
9019 : : {
9020 : 82 : print_decoded_line_section (dwflmod, ebl, ehdr, scn, shdr, dbg);
9021 : 164 : return;
9022 : : }
9023 : :
9024 : 104 : Elf_Data *data = get_debug_elf_data (dbg, ebl, IDX_debug_line, scn);
9025 [ + - ]: 104 : if (data == NULL)
9026 : : return;
9027 : :
9028 : 104 : printf (_("\
9029 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
9030 : : elf_ndxscn (scn), section_name (ebl, shdr),
9031 : 104 : (uint64_t) shdr->sh_offset);
9032 : :
9033 [ + - ]: 104 : if (shdr->sh_size == 0)
9034 : : return;
9035 : :
9036 : : /* There is no functionality in libdw to read the information in the
9037 : : way it is represented here. Hardcode the decoder. */
9038 : :
9039 : 104 : const unsigned char *linep = (const unsigned char *) data->d_buf;
9040 : 104 : const unsigned char *lineendp;
9041 : :
9042 : 104 : while (linep
9043 [ + + ]: 3666 : < (lineendp = (const unsigned char *) data->d_buf + data->d_size))
9044 : : {
9045 : 3562 : size_t start_offset = linep - (const unsigned char *) data->d_buf;
9046 : :
9047 : 3562 : printf (_("\nTable at offset %zu:\n"), start_offset);
9048 : :
9049 [ - + ]: 3562 : if (unlikely (linep + 4 > lineendp))
9050 : 0 : goto invalid_data;
9051 [ + + ]: 3562 : Dwarf_Word unit_length = read_4ubyte_unaligned_inc (dbg, linep);
9052 : 3562 : unsigned int length = 4;
9053 [ - + ]: 3562 : if (unlikely (unit_length == 0xffffffff))
9054 : : {
9055 [ # # ]: 0 : if (unlikely (linep + 8 > lineendp))
9056 : : {
9057 : 0 : invalid_data:
9058 : 0 : error (0, 0, _("invalid data in section [%zu] '%s'"),
9059 : : elf_ndxscn (scn), section_name (ebl, shdr));
9060 : 0 : return;
9061 : : }
9062 [ # # ]: 0 : unit_length = read_8ubyte_unaligned_inc (dbg, linep);
9063 : 0 : length = 8;
9064 : : }
9065 : :
9066 : : /* Check whether we have enough room in the section. */
9067 [ - + ]: 3562 : if (unlikely (unit_length > (size_t) (lineendp - linep)))
9068 : 0 : goto invalid_data;
9069 : 3562 : lineendp = linep + unit_length;
9070 : :
9071 : : /* The next element of the header is the version identifier. */
9072 [ - + ]: 3562 : if ((size_t) (lineendp - linep) < 2)
9073 : 0 : goto invalid_data;
9074 [ + + ]: 3562 : uint_fast16_t version = read_2ubyte_unaligned_inc (dbg, linep);
9075 : :
9076 : 7124 : size_t address_size
9077 [ + + ]: 3562 : = elf_getident (ebl->elf, NULL)[EI_CLASS] == ELFCLASS32 ? 4 : 8;
9078 : 3562 : unsigned char segment_selector_size = 0;
9079 [ + + ]: 3562 : if (version > 4)
9080 : : {
9081 [ - + ]: 3410 : if ((size_t) (lineendp - linep) < 2)
9082 : 0 : goto invalid_data;
9083 : 3410 : address_size = *linep++;
9084 : 3410 : segment_selector_size = *linep++;
9085 : : }
9086 : :
9087 : : /* Next comes the header length. */
9088 : 3562 : Dwarf_Word header_length;
9089 [ + - ]: 3562 : if (length == 4)
9090 : : {
9091 [ - + ]: 3562 : if ((size_t) (lineendp - linep) < 4)
9092 : 0 : goto invalid_data;
9093 [ + + ]: 3562 : header_length = read_4ubyte_unaligned_inc (dbg, linep);
9094 : : }
9095 : : else
9096 : : {
9097 [ # # ]: 0 : if ((size_t) (lineendp - linep) < 8)
9098 : 0 : goto invalid_data;
9099 [ # # ]: 0 : header_length = read_8ubyte_unaligned_inc (dbg, linep);
9100 : : }
9101 : :
9102 : 3562 : const unsigned char *header_start = linep;
9103 : :
9104 : : /* Next the minimum instruction length. */
9105 [ - + ]: 3562 : if ((size_t) (lineendp - linep) < 1)
9106 : 0 : goto invalid_data;
9107 : 3562 : uint_fast8_t minimum_instr_len = *linep++;
9108 : :
9109 : : /* Next the maximum operations per instruction, in version 4 format. */
9110 : 3562 : uint_fast8_t max_ops_per_instr;
9111 [ + + ]: 3562 : if (version < 4)
9112 : : max_ops_per_instr = 1;
9113 : : else
9114 : : {
9115 [ - + ]: 3430 : if ((size_t) (lineendp - linep) < 1)
9116 : 0 : goto invalid_data;
9117 : 3430 : max_ops_per_instr = *linep++;
9118 : : }
9119 : :
9120 : : /* We need at least 4 more bytes. */
9121 [ - + ]: 3562 : if ((size_t) (lineendp - linep) < 4)
9122 : 0 : goto invalid_data;
9123 : :
9124 : : /* Then the flag determining the default value of the is_stmt
9125 : : register. */
9126 : 3562 : uint_fast8_t default_is_stmt = *linep++;
9127 : :
9128 : : /* Now the line base. */
9129 : 3562 : int_fast8_t line_base = *linep++;
9130 : :
9131 : : /* And the line range. */
9132 : 3562 : uint_fast8_t line_range = *linep++;
9133 : :
9134 : : /* The opcode base. */
9135 : 3562 : uint_fast8_t opcode_base = *linep++;
9136 : :
9137 : : /* Print what we got so far. */
9138 : 3562 : printf (_("\n"
9139 : : " Length: %" PRIu64 "\n"
9140 : : " DWARF version: %" PRIuFAST16 "\n"
9141 : : " Prologue length: %" PRIu64 "\n"
9142 : : " Address size: %zd\n"
9143 : : " Segment selector size: %zd\n"
9144 : : " Min instruction length: %" PRIuFAST8 "\n"
9145 : : " Max operations per instruction: %" PRIuFAST8 "\n"
9146 : : " Initial value if 'is_stmt': %" PRIuFAST8 "\n"
9147 : : " Line base: %" PRIdFAST8 "\n"
9148 : : " Line range: %" PRIuFAST8 "\n"
9149 : : " Opcode base: %" PRIuFAST8 "\n"
9150 : : "\n"
9151 : : "Opcodes:\n"),
9152 : : (uint64_t) unit_length, version, (uint64_t) header_length,
9153 : : address_size, (size_t) segment_selector_size,
9154 : : minimum_instr_len, max_ops_per_instr,
9155 : : default_is_stmt, line_base,
9156 : : line_range, opcode_base);
9157 : :
9158 [ - + ]: 3562 : if (version < 2 || version > 5)
9159 : : {
9160 : 0 : error (0, 0, _("cannot handle .debug_line version: %u\n"),
9161 : : (unsigned int) version);
9162 : 0 : linep = lineendp;
9163 : 0 : continue;
9164 : : }
9165 : :
9166 [ - + ]: 3562 : if (address_size != 4 && address_size != 8)
9167 : : {
9168 : 0 : error (0, 0, _("cannot handle address size: %u\n"),
9169 : : (unsigned int) address_size);
9170 : 0 : linep = lineendp;
9171 : 0 : continue;
9172 : : }
9173 : :
9174 [ - + ]: 3562 : if (segment_selector_size != 0)
9175 : : {
9176 : 0 : error (0, 0, _("cannot handle segment selector size: %u\n"),
9177 : : (unsigned int) segment_selector_size);
9178 : 0 : linep = lineendp;
9179 : 0 : continue;
9180 : : }
9181 : :
9182 [ - + ]: 3562 : if (unlikely (linep + opcode_base - 1 >= lineendp))
9183 : : {
9184 : 0 : invalid_unit:
9185 : 0 : error (0, 0,
9186 : 0 : _("invalid data at offset %tu in section [%zu] '%s'"),
9187 : 0 : linep - (const unsigned char *) data->d_buf,
9188 : : elf_ndxscn (scn), section_name (ebl, shdr));
9189 : 0 : linep = lineendp;
9190 : 0 : continue;
9191 : : }
9192 : 3562 : int opcode_base_l10 = 1;
9193 : 3562 : unsigned int tmp = opcode_base;
9194 [ + + ]: 7118 : while (tmp > 10)
9195 : : {
9196 : 3556 : tmp /= 10;
9197 : 3556 : ++opcode_base_l10;
9198 : : }
9199 : : const uint8_t *standard_opcode_lengths = linep - 1;
9200 [ + + ]: 46288 : for (uint_fast8_t cnt = 1; cnt < opcode_base; ++cnt)
9201 : 42726 : printf (ngettext (" [%*" PRIuFAST8 "] %hhu argument\n",
9202 : : " [%*" PRIuFAST8 "] %hhu arguments\n",
9203 : : (int) linep[cnt - 1]),
9204 : 42726 : opcode_base_l10, cnt, linep[cnt - 1]);
9205 : 3562 : linep += opcode_base - 1;
9206 : :
9207 : 3562 : if (unlikely (linep >= lineendp))
9208 : : goto invalid_unit;
9209 : :
9210 : 3562 : Dwarf_Off str_offsets_base = str_offsets_base_off (dbg, NULL);
9211 : :
9212 : 3562 : puts (_("\nDirectory table:"));
9213 [ + + ]: 3562 : if (version > 4)
9214 : : {
9215 : 3410 : struct encpair { uint16_t desc; uint16_t form; };
9216 : 3410 : struct encpair enc[256];
9217 : :
9218 : 3410 : printf (_(" ["));
9219 [ - + ]: 3410 : if ((size_t) (lineendp - linep) < 1)
9220 : 0 : goto invalid_data;
9221 : 3410 : unsigned char directory_entry_format_count = *linep++;
9222 : 3410 : for (int i = 0; i < directory_entry_format_count; i++)
9223 : : {
9224 : 3410 : uint16_t desc, form;
9225 [ - + ]: 3410 : if ((size_t) (lineendp - linep) < 1)
9226 : 0 : goto invalid_data;
9227 : 3410 : get_uleb128 (desc, linep, lineendp);
9228 [ - + ]: 3410 : if ((size_t) (lineendp - linep) < 1)
9229 : 0 : goto invalid_data;
9230 : 3410 : get_uleb128 (form, linep, lineendp);
9231 : :
9232 : 3410 : enc[i].desc = desc;
9233 : 3410 : enc[i].form = form;
9234 : :
9235 : 3410 : printf ("%s(%s)",
9236 : : dwarf_line_content_description_name (desc),
9237 : : dwarf_form_name (form));
9238 [ - + ]: 3410 : if (i + 1 < directory_entry_format_count)
9239 [ + + ]: 6820 : printf (", ");
9240 : : }
9241 : 3410 : printf ("]\n");
9242 : :
9243 : 3410 : uint64_t directories_count;
9244 [ - + ]: 3410 : if ((size_t) (lineendp - linep) < 1)
9245 : 0 : goto invalid_data;
9246 : 3410 : get_uleb128 (directories_count, linep, lineendp);
9247 : :
9248 : 3410 : if (directory_entry_format_count == 0
9249 [ - + ]: 3410 : && directories_count != 0)
9250 : 0 : goto invalid_data;
9251 : :
9252 [ + + ]: 29532 : for (uint64_t i = 0; i < directories_count; i++)
9253 : : {
9254 : 26122 : printf (" %-5" PRIu64 " ", i);
9255 : 26122 : for (int j = 0; j < directory_entry_format_count; j++)
9256 : : {
9257 : 26122 : linep = print_form_data (dbg, enc[j].form,
9258 : : linep, lineendp, length,
9259 : : str_offsets_base);
9260 [ - + ]: 26122 : if (j + 1 < directory_entry_format_count)
9261 [ + + ]: 52244 : printf (", ");
9262 : : }
9263 : 26122 : printf ("\n");
9264 [ - + ]: 26122 : if (linep >= lineendp)
9265 : 0 : goto invalid_unit;
9266 : : }
9267 : : }
9268 : : else
9269 : : {
9270 [ + - + + ]: 766 : while (linep < lineendp && *linep != 0)
9271 : : {
9272 : 614 : unsigned char *endp = memchr (linep, '\0', lineendp - linep);
9273 [ - + ]: 614 : if (unlikely (endp == NULL))
9274 : 0 : goto invalid_unit;
9275 : :
9276 : 614 : printf (" %s\n", (char *) linep);
9277 : :
9278 : 614 : linep = endp + 1;
9279 : : }
9280 [ + - - + ]: 152 : if (linep >= lineendp || *linep != 0)
9281 : 0 : goto invalid_unit;
9282 : : /* Skip the final NUL byte. */
9283 : 152 : ++linep;
9284 : : }
9285 : :
9286 [ - + ]: 3562 : if (unlikely (linep >= lineendp))
9287 : 0 : goto invalid_unit;
9288 : :
9289 : 3562 : puts (_("\nFile name table:"));
9290 [ + + ]: 3562 : if (version > 4)
9291 : : {
9292 : 3410 : struct encpair { uint16_t desc; uint16_t form; };
9293 : 3410 : struct encpair enc[256];
9294 : :
9295 : 3410 : printf (_(" ["));
9296 [ - + ]: 3410 : if ((size_t) (lineendp - linep) < 1)
9297 : 0 : goto invalid_data;
9298 : 3410 : unsigned char file_name_format_count = *linep++;
9299 : 3410 : for (int i = 0; i < file_name_format_count; i++)
9300 : : {
9301 : 6828 : uint64_t desc, form;
9302 [ - + ]: 6828 : if ((size_t) (lineendp - linep) < 1)
9303 : 0 : goto invalid_data;
9304 : 6828 : get_uleb128 (desc, linep, lineendp);
9305 [ - + ]: 6828 : if ((size_t) (lineendp - linep) < 1)
9306 : 0 : goto invalid_data;
9307 : 6828 : get_uleb128 (form, linep, lineendp);
9308 : :
9309 [ - + ]: 6828 : if (! libdw_valid_user_form (form))
9310 : 0 : goto invalid_data;
9311 : :
9312 : 6828 : enc[i].desc = desc;
9313 : 6828 : enc[i].form = form;
9314 : :
9315 : 6828 : printf ("%s(%s)",
9316 : : dwarf_line_content_description_name (desc),
9317 : : dwarf_form_name (form));
9318 [ + + ]: 6828 : if (i + 1 < file_name_format_count)
9319 [ + + ]: 13656 : printf (", ");
9320 : : }
9321 : 3410 : printf ("]\n");
9322 : :
9323 : 3410 : uint64_t file_name_count;
9324 [ - + ]: 3410 : if ((size_t) (lineendp - linep) < 1)
9325 : 0 : goto invalid_data;
9326 : 3410 : get_uleb128 (file_name_count, linep, lineendp);
9327 : :
9328 : 3410 : if (file_name_format_count == 0
9329 [ - + ]: 3410 : && file_name_count != 0)
9330 : 0 : goto invalid_data;
9331 : :
9332 [ + + ]: 60182 : for (uint64_t i = 0; i < file_name_count; i++)
9333 : : {
9334 : 56772 : printf (" %-5" PRIu64 " ", i);
9335 : 56772 : for (int j = 0; j < file_name_format_count; j++)
9336 : : {
9337 : 113568 : linep = print_form_data (dbg, enc[j].form,
9338 : : linep, lineendp, length,
9339 : : str_offsets_base);
9340 [ + + ]: 113568 : if (j + 1 < file_name_format_count)
9341 [ + + ]: 227136 : printf (", ");
9342 : : }
9343 : 56772 : printf ("\n");
9344 [ - + ]: 56772 : if (linep > lineendp)
9345 : 0 : goto invalid_unit;
9346 : : }
9347 : : }
9348 : : else
9349 : : {
9350 : 152 : puts (_(" Entry Dir Time Size Name"));
9351 [ + - + + ]: 6154 : for (unsigned int cnt = 1; linep < lineendp && *linep != 0; ++cnt)
9352 : : {
9353 : : /* First comes the file name. */
9354 : 6002 : char *fname = (char *) linep;
9355 : 6002 : unsigned char *endp = memchr (fname, '\0', lineendp - linep);
9356 [ - + ]: 6002 : if (unlikely (endp == NULL))
9357 : 0 : goto invalid_unit;
9358 : 6002 : linep = endp + 1;
9359 : :
9360 : : /* Then the index. */
9361 : 6002 : unsigned int diridx;
9362 [ - + ]: 6002 : if (lineendp - linep < 1)
9363 : 0 : goto invalid_unit;
9364 : 6002 : get_uleb128 (diridx, linep, lineendp);
9365 : :
9366 : : /* Next comes the modification time. */
9367 : 6002 : unsigned int mtime;
9368 [ - + ]: 6002 : if (lineendp - linep < 1)
9369 : 0 : goto invalid_unit;
9370 : 6002 : get_uleb128 (mtime, linep, lineendp);
9371 : :
9372 : : /* Finally the length of the file. */
9373 : 6002 : unsigned int fsize;
9374 [ - + ]: 6002 : if (lineendp - linep < 1)
9375 : 0 : goto invalid_unit;
9376 : 6002 : get_uleb128 (fsize, linep, lineendp);
9377 : :
9378 : 6002 : printf (" %-5u %-5u %-9u %-9u %s\n",
9379 : : cnt, diridx, mtime, fsize, fname);
9380 : : }
9381 [ + - - + ]: 152 : if (linep >= lineendp || *linep != '\0')
9382 : 0 : goto invalid_unit;
9383 : : /* Skip the final NUL byte. */
9384 : 152 : ++linep;
9385 : : }
9386 : :
9387 : 3562 : unsigned int debug_str_offset = 0;
9388 [ + + ]: 3562 : if (unlikely (linep == header_start + header_length - 4))
9389 : : {
9390 : : /* CUBINs contain an unsigned 4-byte offset */
9391 [ - + ]: 2 : debug_str_offset = read_4ubyte_unaligned_inc (dbg, linep);
9392 : : }
9393 : :
9394 [ + + ]: 3562 : if (linep == lineendp)
9395 : : {
9396 : 40 : puts (_("\nNo line number statements."));
9397 : 40 : continue;
9398 : : }
9399 : :
9400 : 3522 : puts (_("\nLine number statements:"));
9401 : 3522 : Dwarf_Word address = 0;
9402 : 3522 : unsigned int op_index = 0;
9403 : 3522 : size_t line = 1;
9404 : 3522 : uint_fast8_t is_stmt = default_is_stmt;
9405 : :
9406 : : /* Apply the "operation advance" from a special opcode
9407 : : or DW_LNS_advance_pc (as per DWARF4 6.2.5.1). */
9408 : 3522 : unsigned int op_addr_advance;
9409 : : #define advance_pc(op_advance) run_advance_pc(op_advance, minimum_instr_len, \
9410 : : max_ops_per_instr, &op_addr_advance, &address, &op_index)
9411 : :
9412 [ - + ]: 3522 : if (max_ops_per_instr == 0)
9413 : : {
9414 : 0 : error (0, 0,
9415 : 0 : _("invalid maximum operations per instruction is zero"));
9416 : 0 : linep = lineendp;
9417 : 0 : continue;
9418 : : }
9419 : :
9420 : 2368028 : while (linep < lineendp)
9421 : : {
9422 : 2364506 : size_t offset = linep - (const unsigned char *) data->d_buf;
9423 : 2364506 : unsigned int u128;
9424 : 2364506 : int s128;
9425 : :
9426 : : /* Read the opcode. */
9427 : 2364506 : unsigned int opcode = *linep++;
9428 : :
9429 : 2364506 : printf (" [%6" PRIx64 "]", (uint64_t)offset);
9430 : : /* Is this a special opcode? */
9431 [ + + ]: 2364506 : if (likely (opcode >= opcode_base))
9432 : : {
9433 [ - + ]: 683902 : if (unlikely (line_range == 0))
9434 : 0 : goto invalid_unit;
9435 : :
9436 : : /* Yes. Handling this is quite easy since the opcode value
9437 : : is computed with
9438 : :
9439 : : opcode = (desired line increment - line_base)
9440 : : + (line_range * address advance) + opcode_base
9441 : : */
9442 : 683902 : int line_increment = (line_base
9443 : 683902 : + (opcode - opcode_base) % line_range);
9444 : :
9445 : : /* Perform the increments. */
9446 : 683902 : line += line_increment;
9447 : 683902 : advance_pc ((opcode - opcode_base) / line_range);
9448 : :
9449 : 683902 : printf (_(" special opcode %u: address+%u = "),
9450 : : opcode, op_addr_advance);
9451 : 683902 : print_dwarf_addr (dwflmod, 0, address, address);
9452 [ - + ]: 683902 : if (op_index > 0)
9453 : 0 : printf (_(", op_index = %u, line%+d = %zu\n"),
9454 : : op_index, line_increment, line);
9455 : : else
9456 : 683902 : printf (_(", line%+d = %zu\n"),
9457 : : line_increment, line);
9458 : : }
9459 [ + + ]: 1680604 : else if (opcode == 0)
9460 : : {
9461 : : /* This an extended opcode. */
9462 [ - + ]: 128328 : if (unlikely (linep + 2 > lineendp))
9463 : 0 : goto invalid_unit;
9464 : :
9465 : : /* The length. */
9466 : 128328 : unsigned int len = *linep++;
9467 : :
9468 [ - + ]: 128328 : if (unlikely (linep + len > lineendp))
9469 : 0 : goto invalid_unit;
9470 : :
9471 : : /* The sub-opcode. */
9472 : 128328 : opcode = *linep++;
9473 : :
9474 : 128328 : printf (_(" extended opcode %u: "), opcode);
9475 : :
9476 [ + + - + : 128328 : switch (opcode)
+ - - ]
9477 : : {
9478 : 9812 : case DW_LNE_end_sequence:
9479 : 9812 : puts (_(" end of sequence"));
9480 : :
9481 : : /* Reset the registers we care about. */
9482 : 9812 : address = 0;
9483 : 9812 : op_index = 0;
9484 : 9812 : line = 1;
9485 : 9812 : is_stmt = default_is_stmt;
9486 : 9812 : break;
9487 : :
9488 : 11042 : case DW_LNE_set_address:
9489 : 11042 : op_index = 0;
9490 [ - + ]: 11042 : if (unlikely ((size_t) (lineendp - linep) < address_size))
9491 : 0 : goto invalid_unit;
9492 [ + + ]: 11042 : if (address_size == 4)
9493 [ + + ]: 50 : address = read_4ubyte_unaligned_inc (dbg, linep);
9494 : : else
9495 [ + + ]: 10992 : address = read_8ubyte_unaligned_inc (dbg, linep);
9496 : : {
9497 : 11042 : printf (_(" set address to "));
9498 : 11042 : print_dwarf_addr (dwflmod, 0, address, address);
9499 : 11042 : printf ("\n");
9500 : : }
9501 : : break;
9502 : :
9503 : 0 : case DW_LNE_define_file:
9504 : : {
9505 : 0 : char *fname = (char *) linep;
9506 : 0 : unsigned char *endp = memchr (linep, '\0',
9507 : 0 : lineendp - linep);
9508 [ # # ]: 0 : if (unlikely (endp == NULL))
9509 : 0 : goto invalid_unit;
9510 : 0 : linep = endp + 1;
9511 : :
9512 : 0 : unsigned int diridx;
9513 [ # # ]: 0 : if (lineendp - linep < 1)
9514 : 0 : goto invalid_unit;
9515 : 0 : get_uleb128 (diridx, linep, lineendp);
9516 : 0 : Dwarf_Word mtime;
9517 [ # # ]: 0 : if (lineendp - linep < 1)
9518 : 0 : goto invalid_unit;
9519 : 0 : get_uleb128 (mtime, linep, lineendp);
9520 : 0 : Dwarf_Word filelength;
9521 [ # # ]: 0 : if (lineendp - linep < 1)
9522 : 0 : goto invalid_unit;
9523 : 0 : get_uleb128 (filelength, linep, lineendp);
9524 : :
9525 : 0 : printf (_("\
9526 : : define new file: dir=%u, mtime=%" PRIu64 ", length=%" PRIu64 ", name=%s\n"),
9527 : : diridx, (uint64_t) mtime, (uint64_t) filelength,
9528 : : fname);
9529 : : }
9530 : : break;
9531 : :
9532 : 107460 : case DW_LNE_set_discriminator:
9533 : : /* Takes one ULEB128 parameter, the discriminator. */
9534 [ + - - + ]: 107460 : if (unlikely (standard_opcode_lengths[opcode] != 1
9535 : : || lineendp - linep < 1))
9536 : 0 : goto invalid_unit;
9537 : :
9538 : 107460 : get_uleb128 (u128, linep, lineendp);
9539 : 107460 : printf (_(" set discriminator to %u\n"), u128);
9540 : : break;
9541 : :
9542 : 14 : case DW_LNE_NVIDIA_inlined_call:
9543 : : {
9544 [ - + ]: 14 : if (unlikely (linep >= lineendp))
9545 : 0 : goto invalid_data;
9546 : :
9547 : 14 : unsigned int context;
9548 : 14 : get_uleb128 (context, linep, lineendp);
9549 : :
9550 [ - + ]: 14 : if (unlikely (linep >= lineendp))
9551 : 0 : goto invalid_data;
9552 : :
9553 : 14 : unsigned int function_name;
9554 : 14 : get_uleb128 (function_name, linep, lineendp);
9555 : 14 : function_name += debug_str_offset;
9556 : :
9557 : 14 : Elf_Data *str_data = dbg->sectiondata[IDX_debug_str];
9558 : 14 : char *function_str;
9559 [ + - - + ]: 14 : if (str_data == NULL || function_name >= str_data->d_size
9560 [ - + ]: 14 : || memchr (str_data->d_buf + function_name, '\0',
9561 : : str_data->d_size - function_name) == NULL)
9562 : : function_str = "???";
9563 : : else
9564 : 14 : function_str = (char *) str_data->d_buf + function_name;
9565 : :
9566 : 14 : printf (_(" set inlined context %u,"
9567 : : " function name %s (0x%x)\n"),
9568 : : context, function_str, function_name);
9569 : : break;
9570 : : }
9571 : :
9572 : 0 : case DW_LNE_NVIDIA_set_function_name:
9573 : : {
9574 [ # # ]: 0 : if (unlikely (linep >= lineendp))
9575 : 0 : goto invalid_data;
9576 : :
9577 : 0 : unsigned int function_name;
9578 : 0 : get_uleb128 (function_name, linep, lineendp);
9579 : 0 : function_name += debug_str_offset;
9580 : :
9581 : 0 : Elf_Data *str_data = dbg->sectiondata[IDX_debug_str];
9582 : 0 : char *function_str;
9583 [ # # # # ]: 0 : if (str_data == NULL || function_name >= str_data->d_size
9584 [ # # ]: 0 : || memchr (str_data->d_buf + function_name, '\0',
9585 : : str_data->d_size - function_name) == NULL)
9586 : : function_str = "???";
9587 : : else
9588 : 0 : function_str = (char *) str_data->d_buf + function_name;
9589 : :
9590 : 0 : printf (_(" set function name %s (0x%x)\n"),
9591 : : function_str, function_name);
9592 : : }
9593 : : break;
9594 : :
9595 : 0 : default:
9596 : : /* Unknown, ignore it. */
9597 : 0 : puts (_(" unknown opcode"));
9598 : 0 : linep += len - 1;
9599 : 0 : break;
9600 : : }
9601 : : }
9602 [ + - ]: 1552276 : else if (opcode <= DW_LNS_set_isa)
9603 : : {
9604 : : /* This is a known standard opcode. */
9605 [ + + + + : 1552276 : switch (opcode)
+ + - + +
+ - - ]
9606 : : {
9607 : 235986 : case DW_LNS_copy:
9608 : : /* Takes no argument. */
9609 : 235986 : puts (_(" copy"));
9610 : 235986 : break;
9611 : :
9612 : 15314 : case DW_LNS_advance_pc:
9613 : : /* Takes one uleb128 parameter which is added to the
9614 : : address. */
9615 [ - + ]: 15314 : if (lineendp - linep < 1)
9616 : 0 : goto invalid_unit;
9617 : 15314 : get_uleb128 (u128, linep, lineendp);
9618 : 15314 : advance_pc (u128);
9619 : : {
9620 : 15314 : printf (_(" advance address by %u to "),
9621 : : op_addr_advance);
9622 : 15314 : print_dwarf_addr (dwflmod, 0, address, address);
9623 [ - + ]: 15314 : if (op_index > 0)
9624 : 0 : printf (_(", op_index to %u"), op_index);
9625 : 15314 : printf ("\n");
9626 : : }
9627 : : break;
9628 : :
9629 : 181868 : case DW_LNS_advance_line:
9630 : : /* Takes one sleb128 parameter which is added to the
9631 : : line. */
9632 [ - + ]: 181868 : if (lineendp - linep < 1)
9633 : 0 : goto invalid_unit;
9634 : 181868 : get_sleb128 (s128, linep, lineendp);
9635 : 181868 : line += s128;
9636 : 181868 : printf (_("\
9637 : : advance line by constant %d to %" PRId64 "\n"),
9638 : : s128, (int64_t) line);
9639 : : break;
9640 : :
9641 : 65712 : case DW_LNS_set_file:
9642 : : /* Takes one uleb128 parameter which is stored in file. */
9643 [ - + ]: 65712 : if (lineendp - linep < 1)
9644 : 0 : goto invalid_unit;
9645 : 65712 : get_uleb128 (u128, linep, lineendp);
9646 : 65712 : printf (_(" set file to %" PRIu64 "\n"),
9647 : : (uint64_t) u128);
9648 : : break;
9649 : :
9650 : 588308 : case DW_LNS_set_column:
9651 : : /* Takes one uleb128 parameter which is stored in column. */
9652 [ + - - + ]: 588308 : if (unlikely (standard_opcode_lengths[opcode] != 1
9653 : : || lineendp - linep < 1))
9654 : 0 : goto invalid_unit;
9655 : :
9656 : 588308 : get_uleb128 (u128, linep, lineendp);
9657 : 588308 : printf (_(" set column to %" PRIu64 "\n"),
9658 : : (uint64_t) u128);
9659 : : break;
9660 : :
9661 : 411858 : case DW_LNS_negate_stmt:
9662 : : /* Takes no argument. */
9663 : 411858 : is_stmt = 1 - is_stmt;
9664 : 411858 : printf (_(" set '%s' to %" PRIuFAST8 "\n"),
9665 : : "is_stmt", is_stmt);
9666 : : break;
9667 : :
9668 : 0 : case DW_LNS_set_basic_block:
9669 : : /* Takes no argument. */
9670 : 0 : puts (_(" set basic block flag"));
9671 : 0 : break;
9672 : :
9673 : 53176 : case DW_LNS_const_add_pc:
9674 : : /* Takes no argument. */
9675 : :
9676 [ - + ]: 53176 : if (unlikely (line_range == 0))
9677 : 0 : goto invalid_unit;
9678 : :
9679 : 53176 : advance_pc ((255 - opcode_base) / line_range);
9680 : : {
9681 : 53176 : printf (_(" advance address by constant %u to "),
9682 : : op_addr_advance);
9683 : 53176 : print_dwarf_addr (dwflmod, 0, address, address);
9684 [ - + ]: 53176 : if (op_index > 0)
9685 : 0 : printf (_(", op_index to %u"), op_index);
9686 : 53176 : printf ("\n");
9687 : : }
9688 : : break;
9689 : :
9690 : 44 : case DW_LNS_fixed_advance_pc:
9691 : : /* Takes one 16 bit parameter which is added to the
9692 : : address. */
9693 [ + - - + ]: 44 : if (unlikely (standard_opcode_lengths[opcode] != 1
9694 : : || lineendp - linep < 2))
9695 : 0 : goto invalid_unit;
9696 : :
9697 [ - + ]: 44 : u128 = read_2ubyte_unaligned_inc (dbg, linep);
9698 : 44 : address += u128;
9699 : 44 : op_index = 0;
9700 : : {
9701 : 44 : printf (_("\
9702 : : advance address by fixed value %u to \n"),
9703 : : u128);
9704 : 44 : print_dwarf_addr (dwflmod, 0, address, address);
9705 : 44 : printf ("\n");
9706 : : }
9707 : : break;
9708 : :
9709 : 10 : case DW_LNS_set_prologue_end:
9710 : : /* Takes no argument. */
9711 : 10 : puts (_(" set prologue end flag"));
9712 : 10 : break;
9713 : :
9714 : 0 : case DW_LNS_set_epilogue_begin:
9715 : : /* Takes no argument. */
9716 : 0 : puts (_(" set epilogue begin flag"));
9717 : 0 : break;
9718 : :
9719 : 0 : case DW_LNS_set_isa:
9720 : : /* Takes one uleb128 parameter which is stored in isa. */
9721 [ # # # # ]: 0 : if (unlikely (standard_opcode_lengths[opcode] != 1
9722 : : || lineendp - linep < 1))
9723 : 0 : goto invalid_unit;
9724 : :
9725 : 0 : get_uleb128 (u128, linep, lineendp);
9726 [ + + ]: 2368028 : printf (_(" set isa to %u\n"), u128);
9727 : : break;
9728 : : }
9729 : : }
9730 : : else
9731 : : {
9732 : : /* This is a new opcode the generator but not we know about.
9733 : : Read the parameters associated with it but then discard
9734 : : everything. Read all the parameters for this opcode. */
9735 : 0 : printf (ngettext (" unknown opcode with %" PRIu8 " parameter:",
9736 : : " unknown opcode with %" PRIu8 " parameters:",
9737 : : standard_opcode_lengths[opcode]),
9738 : 0 : standard_opcode_lengths[opcode]);
9739 : 0 : for (int n = standard_opcode_lengths[opcode];
9740 [ # # # # ]: 0 : n > 0 && linep < lineendp; --n)
9741 : : {
9742 : 0 : get_uleb128 (u128, linep, lineendp);
9743 [ # # ]: 0 : if (n != standard_opcode_lengths[opcode])
9744 : 0 : fputc (',', stdout);
9745 : 0 : printf (" %u", u128);
9746 : : }
9747 : :
9748 : : /* Next round, ignore this opcode. */
9749 : 0 : continue;
9750 : : }
9751 : : }
9752 : : }
9753 : :
9754 : : /* There must only be one data block. */
9755 [ - + ]: 104 : assert (elf_getdata (scn, data) == NULL);
9756 : : }
9757 : :
9758 : :
9759 : : static void
9760 : 40 : print_debug_loclists_section (Dwfl_Module *dwflmod,
9761 : : Ebl *ebl,
9762 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
9763 : : Elf_Scn *scn, GElf_Shdr *shdr,
9764 : : Dwarf *dbg)
9765 : : {
9766 : 40 : Elf_Data *data = get_debug_elf_data (dbg, ebl, IDX_debug_loclists, scn);
9767 [ + - ]: 40 : if (data == NULL)
9768 : 0 : return;
9769 : :
9770 : 40 : printf (_("\
9771 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
9772 : : elf_ndxscn (scn), section_name (ebl, shdr),
9773 : 40 : (uint64_t) shdr->sh_offset);
9774 : :
9775 : : /* For the listptr to get the base address/CU. */
9776 : 40 : sort_listptr (&known_loclistsptr, "loclistsptr");
9777 : 40 : size_t listptr_idx = 0;
9778 : :
9779 : 40 : const unsigned char *readp = data->d_buf;
9780 : 40 : const unsigned char *const dataend = ((unsigned char *) data->d_buf
9781 : 40 : + data->d_size);
9782 [ + + ]: 3072 : while (readp < dataend)
9783 : : {
9784 [ - + ]: 3032 : if (unlikely (readp > dataend - 4))
9785 : : {
9786 : 0 : invalid_data:
9787 : 0 : error (0, 0, _("invalid data in section [%zu] '%s'"),
9788 : : elf_ndxscn (scn), section_name (ebl, shdr));
9789 : 0 : return;
9790 : : }
9791 : :
9792 : 3032 : ptrdiff_t offset = readp - (unsigned char *) data->d_buf;
9793 : 3032 : printf (_("Table at Offset 0x%" PRIx64 ":\n\n"),
9794 : : (uint64_t) offset);
9795 : :
9796 [ - + ]: 3032 : uint64_t unit_length = read_4ubyte_unaligned_inc (dbg, readp);
9797 : 3032 : unsigned int offset_size = 4;
9798 [ - + ]: 3032 : if (unlikely (unit_length == 0xffffffff))
9799 : : {
9800 [ # # ]: 0 : if (unlikely (readp > dataend - 8))
9801 : 0 : goto invalid_data;
9802 : :
9803 [ # # ]: 0 : unit_length = read_8ubyte_unaligned_inc (dbg, readp);
9804 : 0 : offset_size = 8;
9805 : : }
9806 : 3032 : printf (_(" Length: %8" PRIu64 "\n"), unit_length);
9807 : :
9808 : : /* We need at least 2-bytes + 1-byte + 1-byte + 4-bytes = 8
9809 : : bytes to complete the header. And this unit cannot go beyond
9810 : : the section data. */
9811 [ + - ]: 3032 : if (readp > dataend - 8
9812 [ + - ]: 3032 : || unit_length < 8
9813 [ - + ]: 3032 : || unit_length > (uint64_t) (dataend - readp))
9814 : 0 : goto invalid_data;
9815 : :
9816 : 3032 : const unsigned char *nexthdr = readp + unit_length;
9817 : :
9818 [ - + ]: 3032 : uint16_t version = read_2ubyte_unaligned_inc (dbg, readp);
9819 : 3032 : printf (_(" DWARF version: %8" PRIu16 "\n"), version);
9820 : :
9821 [ - + ]: 3032 : if (version != 5)
9822 : : {
9823 : 0 : error (0, 0, _("Unknown version"));
9824 : 0 : goto next_table;
9825 : : }
9826 : :
9827 : 3032 : uint8_t address_size = *readp++;
9828 : 3032 : printf (_(" Address size: %8" PRIu64 "\n"),
9829 : : (uint64_t) address_size);
9830 : :
9831 [ - + ]: 3032 : if (address_size != 4 && address_size != 8)
9832 : : {
9833 : 0 : error (0, 0, _("unsupported address size"));
9834 : 0 : goto next_table;
9835 : : }
9836 : :
9837 : 3032 : uint8_t segment_size = *readp++;
9838 : 3032 : printf (_(" Segment size: %8" PRIu64 "\n"),
9839 : : (uint64_t) segment_size);
9840 : :
9841 [ - + ]: 3032 : if (segment_size != 0)
9842 : : {
9843 : 0 : error (0, 0, _("unsupported segment size"));
9844 : 0 : goto next_table;
9845 : : }
9846 : :
9847 [ - + ]: 3032 : uint32_t offset_entry_count = read_4ubyte_unaligned_inc (dbg, readp);
9848 : 3032 : printf (_(" Offset entries: %8" PRIu64 "\n"),
9849 : : (uint64_t) offset_entry_count);
9850 : :
9851 : : /* We need the CU that uses this unit to get the initial base address. */
9852 : 3032 : Dwarf_Addr cu_base = 0;
9853 : 3032 : struct Dwarf_CU *cu = NULL;
9854 [ - + ]: 3032 : if (listptr_cu (&known_loclistsptr, &listptr_idx,
9855 : : (Dwarf_Off) offset,
9856 : 3032 : (Dwarf_Off) (nexthdr - (unsigned char *) data->d_buf),
9857 : : &cu_base, &cu)
9858 [ # # ]: 0 : || split_dwarf_cu_base (dbg, &cu, &cu_base))
9859 : 3032 : {
9860 : 3032 : Dwarf_Die cudie;
9861 [ - + ]: 3032 : if (dwarf_cu_die (cu, &cudie,
9862 : : NULL, NULL, NULL, NULL,
9863 : : NULL, NULL) == NULL)
9864 : 0 : printf (_(" Unknown CU base: "));
9865 : : else
9866 : 3032 : printf (_(" CU [%6" PRIx64 "] base: "),
9867 : : dwarf_dieoffset (&cudie));
9868 : 3032 : print_dwarf_addr (dwflmod, address_size, cu_base, cu_base);
9869 : 3032 : printf ("\n");
9870 : : }
9871 : : else
9872 : 0 : printf (_(" Not associated with a CU.\n"));
9873 : :
9874 : 3032 : printf ("\n");
9875 : :
9876 : 3032 : const unsigned char *offset_array_start = readp;
9877 [ + + ]: 3032 : if (offset_entry_count > 0)
9878 : : {
9879 : 4 : uint64_t max_entries = (unit_length - 8) / offset_size;
9880 [ - + ]: 4 : if (offset_entry_count > max_entries)
9881 : : {
9882 : 0 : error (0, 0,
9883 : 0 : _("too many offset entries for unit length"));
9884 : 0 : offset_entry_count = max_entries;
9885 : : }
9886 : :
9887 : 4 : printf (_(" Offsets starting at 0x%" PRIx64 ":\n"),
9888 : : (uint64_t) (offset_array_start
9889 : 4 : - (unsigned char *) data->d_buf));
9890 [ + + ]: 36 : for (uint32_t idx = 0; idx < offset_entry_count; idx++)
9891 : : {
9892 : 32 : printf (" [%6" PRIu32 "] ", idx);
9893 [ + - ]: 32 : if (offset_size == 4)
9894 : : {
9895 [ - + ]: 32 : uint32_t off = read_4ubyte_unaligned_inc (dbg, readp);
9896 : 32 : printf ("0x%" PRIx32 "\n", off);
9897 : : }
9898 : : else
9899 : : {
9900 [ # # ]: 0 : uint64_t off = read_8ubyte_unaligned_inc (dbg, readp);
9901 : 32 : printf ("0x%" PRIx64 "\n", off);
9902 : : }
9903 : : }
9904 : 4 : printf ("\n");
9905 : : }
9906 : :
9907 : 3032 : Dwarf_Addr base = cu_base;
9908 : 3032 : bool start_of_list = true;
9909 : 3032 : while (readp < nexthdr)
9910 : : {
9911 : 734436 : Dwarf_Off off = (Dwarf_Off) (readp - (unsigned char *) data->d_buf);
9912 [ + + ]: 734436 : if (listptr_attr (&known_loclistsptr, listptr_idx, off,
9913 : : DW_AT_GNU_locviews))
9914 : 107692 : {
9915 : 107692 : Dwarf_Off next_off = next_listptr_offset (&known_loclistsptr,
9916 : : &listptr_idx, off);
9917 : 107692 : const unsigned char *locp = readp;
9918 : 107692 : const unsigned char *locendp;
9919 [ + - ]: 107692 : if (next_off == 0
9920 [ + - ]: 107692 : || next_off > (size_t) (nexthdr - ((const unsigned char *)
9921 : : data->d_buf)))
9922 : : locendp = nexthdr;
9923 : : else
9924 : 107692 : locendp = (const unsigned char *) data->d_buf + next_off;
9925 : :
9926 : 716086 : printf (" Offset: %" PRIx64 ", Index: %" PRIx64 "\n",
9927 : : (uint64_t) (readp - (unsigned char *) data->d_buf),
9928 : 107692 : (uint64_t) (readp - offset_array_start));
9929 : :
9930 : 608394 : while (locp < locendp)
9931 : : {
9932 : 500702 : uint64_t v1, v2;
9933 : 500702 : get_uleb128 (v1, locp, locendp);
9934 [ - + ]: 500702 : if (locp >= locendp)
9935 : : {
9936 : 0 : printf (_(" <INVALID DATA>\n"));
9937 : : break;
9938 : : }
9939 : 500702 : get_uleb128 (v2, locp, locendp);
9940 [ + + ]: 608394 : printf (" view pair %" PRId64 ", %" PRId64 "\n", v1, v2);
9941 : : }
9942 : :
9943 : 107692 : printf ("\n");
9944 : 107692 : readp = (unsigned char *) locendp;
9945 : 107692 : continue;
9946 : : }
9947 : :
9948 : 626744 : uint8_t kind = *readp++;
9949 : 626744 : uint64_t op1, op2, len;
9950 : :
9951 : : /* Skip padding. */
9952 [ - + ]: 626744 : if (start_of_list && kind == DW_LLE_end_of_list)
9953 : 0 : continue;
9954 : :
9955 [ + + ]: 626744 : if (start_of_list)
9956 : : {
9957 : 107756 : base = cu_base;
9958 : 107756 : printf (" Offset: %" PRIx64 ", Index: %" PRIx64 "\n",
9959 : 107756 : (uint64_t) (readp - (unsigned char *) data->d_buf - 1),
9960 : 107756 : (uint64_t) (readp - offset_array_start - 1));
9961 : 107756 : start_of_list = false;
9962 : : }
9963 : :
9964 : 626744 : printf (" %s", dwarf_loc_list_encoding_name (kind));
9965 [ + - - + : 626744 : switch (kind)
+ - + - +
- - ]
9966 : : {
9967 : 107756 : case DW_LLE_end_of_list:
9968 : 107756 : start_of_list = true;
9969 : 107756 : printf ("\n\n");
9970 : : break;
9971 : :
9972 : 0 : case DW_LLE_base_addressx:
9973 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
9974 : : {
9975 : 0 : invalid_entry:
9976 : 0 : error (0, 0, _("invalid loclists data"));
9977 : 0 : goto next_table;
9978 : : }
9979 : 0 : get_uleb128 (op1, readp, nexthdr);
9980 : 0 : printf (" %" PRIx64 "\n", op1);
9981 [ # # ]: 0 : if (! print_unresolved_addresses)
9982 : : {
9983 : 0 : Dwarf_Addr addr;
9984 [ # # ]: 0 : if (get_indexed_addr (cu, op1, &addr) != 0)
9985 : 0 : printf (" ???\n");
9986 : : else
9987 : : {
9988 : 0 : printf (" ");
9989 : 0 : print_dwarf_addr (dwflmod, address_size, addr, addr);
9990 : 0 : printf ("\n");
9991 : : }
9992 : : }
9993 : : break;
9994 : :
9995 : 0 : case DW_LLE_startx_endx:
9996 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
9997 : 0 : goto invalid_entry;
9998 : 0 : get_uleb128 (op1, readp, nexthdr);
9999 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
10000 : 0 : goto invalid_entry;
10001 : 0 : get_uleb128 (op2, readp, nexthdr);
10002 : 0 : printf (" %" PRIx64 ", %" PRIx64 "\n", op1, op2);
10003 [ # # ]: 0 : if (! print_unresolved_addresses)
10004 : : {
10005 : 0 : Dwarf_Addr addr1;
10006 : 0 : Dwarf_Addr addr2;
10007 [ # # ]: 0 : if (get_indexed_addr (cu, op1, &addr1) != 0
10008 [ # # ]: 0 : || get_indexed_addr (cu, op2, &addr2) != 0)
10009 : : {
10010 : 0 : printf (" ???..\n");
10011 : 0 : printf (" ???\n");
10012 : : }
10013 : : else
10014 : : {
10015 : 0 : printf (" ");
10016 : 0 : print_dwarf_addr (dwflmod, address_size, addr1, addr1);
10017 : 0 : printf ("..\n ");
10018 : 0 : print_dwarf_addr (dwflmod, address_size,
10019 : : addr2 - 1, addr2);
10020 : 0 : printf ("\n");
10021 : : }
10022 : : }
10023 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
10024 : 0 : goto invalid_entry;
10025 : 0 : get_uleb128 (len, readp, nexthdr);
10026 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < len)
10027 : 0 : goto invalid_entry;
10028 : 0 : print_ops (dwflmod, dbg, 8, 8, version,
10029 : : address_size, offset_size, cu, len, readp);
10030 : 0 : readp += len;
10031 : 0 : break;
10032 : :
10033 : 52 : case DW_LLE_startx_length:
10034 [ - + ]: 52 : if ((uint64_t) (nexthdr - readp) < 1)
10035 : 0 : goto invalid_entry;
10036 : 52 : get_uleb128 (op1, readp, nexthdr);
10037 [ - + ]: 52 : if ((uint64_t) (nexthdr - readp) < 1)
10038 : 0 : goto invalid_entry;
10039 : 52 : get_uleb128 (op2, readp, nexthdr);
10040 : 52 : printf (" %" PRIx64 ", %" PRIx64 "\n", op1, op2);
10041 [ + - ]: 52 : if (! print_unresolved_addresses)
10042 : : {
10043 : 52 : Dwarf_Addr addr1;
10044 : 52 : Dwarf_Addr addr2;
10045 [ - + ]: 52 : if (get_indexed_addr (cu, op1, &addr1) != 0)
10046 : : {
10047 : 0 : printf (" ???..\n");
10048 : 0 : printf (" ???\n");
10049 : : }
10050 : : else
10051 : : {
10052 : 52 : addr2 = addr1 + op2;
10053 : 52 : printf (" ");
10054 : 52 : print_dwarf_addr (dwflmod, address_size, addr1, addr1);
10055 : 52 : printf ("..\n ");
10056 : 52 : print_dwarf_addr (dwflmod, address_size,
10057 : : addr2 - 1, addr2);
10058 : 52 : printf ("\n");
10059 : : }
10060 : : }
10061 [ - + ]: 52 : if ((uint64_t) (nexthdr - readp) < 1)
10062 : 0 : goto invalid_entry;
10063 : 52 : get_uleb128 (len, readp, nexthdr);
10064 [ - + ]: 52 : if ((uint64_t) (nexthdr - readp) < len)
10065 : 0 : goto invalid_entry;
10066 : 52 : print_ops (dwflmod, dbg, 8, 8, version,
10067 : : address_size, offset_size, cu, len, readp);
10068 : 52 : readp += len;
10069 : 52 : break;
10070 : :
10071 : 488956 : case DW_LLE_offset_pair:
10072 [ - + ]: 488956 : if ((uint64_t) (nexthdr - readp) < 1)
10073 : 0 : goto invalid_entry;
10074 : 488956 : get_uleb128 (op1, readp, nexthdr);
10075 [ - + ]: 488956 : if ((uint64_t) (nexthdr - readp) < 1)
10076 : 0 : goto invalid_entry;
10077 : 488956 : get_uleb128 (op2, readp, nexthdr);
10078 : 488956 : printf (" %" PRIx64 ", %" PRIx64 "\n", op1, op2);
10079 [ + - ]: 488956 : if (! print_unresolved_addresses)
10080 : : {
10081 : 488956 : op1 += base;
10082 : 488956 : op2 += base;
10083 : 488956 : printf (" ");
10084 : 488956 : print_dwarf_addr (dwflmod, address_size, op1, op1);
10085 : 488956 : printf ("..\n ");
10086 : 488956 : print_dwarf_addr (dwflmod, address_size, op2 - 1, op2);
10087 : 488956 : printf ("\n");
10088 : : }
10089 [ - + ]: 488956 : if ((uint64_t) (nexthdr - readp) < 1)
10090 : 0 : goto invalid_entry;
10091 : 488956 : get_uleb128 (len, readp, nexthdr);
10092 [ - + ]: 488956 : if ((uint64_t) (nexthdr - readp) < len)
10093 : 0 : goto invalid_entry;
10094 : 488956 : print_ops (dwflmod, dbg, 8, 8, version,
10095 : : address_size, offset_size, cu, len, readp);
10096 : 488956 : readp += len;
10097 : 488956 : break;
10098 : :
10099 : 0 : case DW_LLE_default_location:
10100 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
10101 : 0 : goto invalid_entry;
10102 : 0 : get_uleb128 (len, readp, nexthdr);
10103 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < len)
10104 : 0 : goto invalid_entry;
10105 : 0 : print_ops (dwflmod, dbg, 8, 8, version,
10106 : : address_size, offset_size, cu, len, readp);
10107 : 0 : readp += len;
10108 : 0 : break;
10109 : :
10110 : 18182 : case DW_LLE_base_address:
10111 [ - + ]: 18182 : if (address_size == 4)
10112 : : {
10113 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 4)
10114 : 0 : goto invalid_entry;
10115 [ # # ]: 0 : op1 = read_4ubyte_unaligned_inc (dbg, readp);
10116 : : }
10117 : : else
10118 : : {
10119 [ - + ]: 18182 : if ((uint64_t) (nexthdr - readp) < 8)
10120 : 0 : goto invalid_entry;
10121 [ - + ]: 18182 : op1 = read_8ubyte_unaligned_inc (dbg, readp);
10122 : : }
10123 : 18182 : base = op1;
10124 : 18182 : printf (" 0x%" PRIx64 "\n", base);
10125 [ - + ]: 18182 : if (! print_unresolved_addresses)
10126 : : {
10127 : 18182 : printf (" ");
10128 : 18182 : print_dwarf_addr (dwflmod, address_size, base, base);
10129 : 18182 : printf ("\n");
10130 : : }
10131 : : break;
10132 : :
10133 : 0 : case DW_LLE_start_end:
10134 [ # # ]: 0 : if (address_size == 4)
10135 : : {
10136 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 8)
10137 : 0 : goto invalid_entry;
10138 [ # # ]: 0 : op1 = read_4ubyte_unaligned_inc (dbg, readp);
10139 [ # # ]: 0 : op2 = read_4ubyte_unaligned_inc (dbg, readp);
10140 : : }
10141 : : else
10142 : : {
10143 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 16)
10144 : 0 : goto invalid_entry;
10145 [ # # ]: 0 : op1 = read_8ubyte_unaligned_inc (dbg, readp);
10146 [ # # ]: 0 : op2 = read_8ubyte_unaligned_inc (dbg, readp);
10147 : : }
10148 : 0 : printf (" 0x%" PRIx64 "..0x%" PRIx64 "\n", op1, op2);
10149 [ # # ]: 0 : if (! print_unresolved_addresses)
10150 : : {
10151 : 0 : printf (" ");
10152 : 0 : print_dwarf_addr (dwflmod, address_size, op1, op1);
10153 : 0 : printf ("..\n ");
10154 : 0 : print_dwarf_addr (dwflmod, address_size, op2 - 1, op2);
10155 : 0 : printf ("\n");
10156 : : }
10157 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
10158 : 0 : goto invalid_entry;
10159 : 0 : get_uleb128 (len, readp, nexthdr);
10160 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < len)
10161 : 0 : goto invalid_entry;
10162 : 0 : print_ops (dwflmod, dbg, 8, 8, version,
10163 : : address_size, offset_size, cu, len, readp);
10164 : 0 : readp += len;
10165 : 0 : break;
10166 : :
10167 : 11798 : case DW_LLE_start_length:
10168 [ - + ]: 11798 : if (address_size == 4)
10169 : : {
10170 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 4)
10171 : 0 : goto invalid_entry;
10172 [ # # ]: 0 : op1 = read_4ubyte_unaligned_inc (dbg, readp);
10173 : : }
10174 : : else
10175 : : {
10176 [ - + ]: 11798 : if ((uint64_t) (nexthdr - readp) < 8)
10177 : 0 : goto invalid_entry;
10178 [ - + ]: 11798 : op1 = read_8ubyte_unaligned_inc (dbg, readp);
10179 : : }
10180 [ - + ]: 11798 : if ((uint64_t) (nexthdr - readp) < 1)
10181 : 0 : goto invalid_entry;
10182 : 11798 : get_uleb128 (op2, readp, nexthdr);
10183 : 11798 : printf (" 0x%" PRIx64 ", %" PRIx64 "\n", op1, op2);
10184 [ + + ]: 11798 : if (! print_unresolved_addresses)
10185 : : {
10186 : 11796 : op2 = op1 + op2;
10187 : 11796 : printf (" ");
10188 : 11796 : print_dwarf_addr (dwflmod, address_size, op1, op1);
10189 : 11796 : printf ("..\n ");
10190 : 11796 : print_dwarf_addr (dwflmod, address_size, op2 - 1, op2);
10191 : 11796 : printf ("\n");
10192 : : }
10193 [ - + ]: 11798 : if ((uint64_t) (nexthdr - readp) < 1)
10194 : 0 : goto invalid_entry;
10195 : 11798 : get_uleb128 (len, readp, nexthdr);
10196 [ - + ]: 11798 : if ((uint64_t) (nexthdr - readp) < len)
10197 : 0 : goto invalid_entry;
10198 : 11798 : print_ops (dwflmod, dbg, 8, 8, version,
10199 : : address_size, offset_size, cu, len, readp);
10200 : 11798 : readp += len;
10201 : 11798 : break;
10202 : :
10203 : 0 : case DW_LLE_GNU_view_pair:
10204 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
10205 : 0 : goto invalid_entry;
10206 : 0 : get_uleb128 (op1, readp, nexthdr);
10207 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
10208 : 0 : goto invalid_entry;
10209 : 0 : get_uleb128 (op2, readp, nexthdr);
10210 [ + + ]: 737468 : printf (" %" PRIx64 ", %" PRIx64 "\n", op1, op2);
10211 : : break;
10212 : :
10213 : 0 : default:
10214 : 0 : goto invalid_entry;
10215 : : }
10216 : : }
10217 : :
10218 : 3032 : next_table:
10219 [ - + ]: 3032 : if (readp != nexthdr)
10220 : : {
10221 : 0 : size_t padding = nexthdr - readp;
10222 : 0 : printf (_(" %zu padding bytes\n\n"), padding);
10223 : 0 : readp = nexthdr;
10224 : : }
10225 : : }
10226 : : }
10227 : :
10228 : :
10229 : : static void
10230 : 52 : print_debug_loc_section (Dwfl_Module *dwflmod,
10231 : : Ebl *ebl, GElf_Ehdr *ehdr,
10232 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
10233 : : {
10234 : 52 : Elf_Data *data = get_debug_elf_data (dbg, ebl, IDX_debug_loc, scn);
10235 [ - + ]: 52 : if (data == NULL)
10236 : 0 : return;
10237 : :
10238 : 52 : printf (_("\
10239 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
10240 : : elf_ndxscn (scn), section_name (ebl, shdr),
10241 : 52 : (uint64_t) shdr->sh_offset);
10242 : :
10243 : 52 : sort_listptr (&known_locsptr, "loclistptr");
10244 : 52 : size_t listptr_idx = 0;
10245 : :
10246 [ + - ]: 52 : uint_fast8_t address_size = ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 4 : 8;
10247 : 52 : uint_fast8_t offset_size = 4;
10248 : :
10249 : 52 : bool first = true;
10250 : 52 : Dwarf_Addr base = 0;
10251 : 52 : unsigned char *readp = data->d_buf;
10252 : 52 : unsigned char *const endp = (unsigned char *) data->d_buf + data->d_size;
10253 : 52 : Dwarf_CU *last_cu = NULL;
10254 [ + + ]: 490 : while (readp < endp)
10255 : : {
10256 : 438 : ptrdiff_t offset = readp - (unsigned char *) data->d_buf;
10257 : 438 : Dwarf_CU *cu = last_cu;
10258 : 438 : unsigned int attr = 0;
10259 : :
10260 [ + + - + ]: 438 : if (first && skip_listptr_hole (&known_locsptr, &listptr_idx,
10261 : : &address_size, &offset_size, &base,
10262 : : &cu, offset, &readp, endp, &attr))
10263 : 0 : continue;
10264 : :
10265 [ + + ]: 438 : if (last_cu != cu)
10266 : : {
10267 : 60 : Dwarf_Die cudie;
10268 [ - + ]: 60 : if (dwarf_cu_die (cu, &cudie,
10269 : : NULL, NULL, NULL, NULL,
10270 : : NULL, NULL) == NULL)
10271 : 0 : printf (_("\n Unknown CU base: "));
10272 : : else
10273 : 60 : printf (_("\n CU [%6" PRIx64 "] base: "),
10274 : : dwarf_dieoffset (&cudie));
10275 : 60 : print_dwarf_addr (dwflmod, address_size, base, base);
10276 : 60 : printf ("\n");
10277 : : }
10278 : 438 : last_cu = cu;
10279 : :
10280 [ - + ]: 438 : if (attr == DW_AT_GNU_locviews)
10281 : 0 : {
10282 : 0 : Dwarf_Off next_off = next_listptr_offset (&known_locsptr,
10283 : : &listptr_idx, offset);
10284 : 0 : const unsigned char *locp = readp;
10285 : 0 : const unsigned char *locendp;
10286 [ # # ]: 0 : if (next_off == 0
10287 : 0 : || next_off > (size_t) (endp
10288 [ # # ]: 0 : - (const unsigned char *) data->d_buf))
10289 : 0 : locendp = endp;
10290 : : else
10291 : 0 : locendp = (const unsigned char *) data->d_buf + next_off;
10292 : :
10293 [ # # ]: 0 : while (locp < locendp)
10294 : : {
10295 : 0 : uint64_t v1, v2;
10296 : 0 : get_uleb128 (v1, locp, locendp);
10297 [ # # ]: 0 : if (locp >= locendp)
10298 : : {
10299 : 0 : printf (_(" [%6tx] <INVALID DATA>\n"), offset);
10300 : : break;
10301 : : }
10302 : 0 : get_uleb128 (v2, locp, locendp);
10303 [ # # ]: 0 : if (first) /* First view pair in a list. */
10304 : 0 : printf (" [%6tx] ", offset);
10305 : : else
10306 : 0 : printf (" ");
10307 : 0 : printf ("view pair %" PRId64 ", %" PRId64 "\n", v1, v2);
10308 : 0 : first = false;
10309 : : }
10310 : :
10311 : 0 : first = true;
10312 : 0 : readp = (unsigned char *) locendp;
10313 : 0 : continue;
10314 : : }
10315 : :
10316 : : /* GNU DebugFission encoded addresses as addrx. */
10317 : 876 : bool is_debugfission = ((cu != NULL
10318 [ # # ]: 0 : || split_dwarf_cu_base (dbg, &cu, &base))
10319 [ - + - + ]: 438 : && (cu->version < 5
10320 [ + + ]: 438 : && cu->unit_type == DW_UT_split_compile));
10321 [ + + ]: 438 : if (!is_debugfission
10322 [ - + ]: 318 : && unlikely (data->d_size - offset < (size_t) address_size * 2))
10323 : : {
10324 : 0 : invalid_data:
10325 : 0 : printf (_(" [%6tx] <INVALID DATA>\n"), offset);
10326 : 0 : break;
10327 : : }
10328 : :
10329 : 318 : Dwarf_Addr begin;
10330 : 318 : Dwarf_Addr end;
10331 : 438 : bool use_base = true;
10332 : 318 : if (is_debugfission)
10333 : : {
10334 : 120 : const unsigned char *locp = readp;
10335 : 120 : const unsigned char *locendp = readp + data->d_size;
10336 [ - + ]: 120 : if (locp >= locendp)
10337 : 0 : goto invalid_data;
10338 : :
10339 : 120 : Dwarf_Word idx;
10340 : 120 : unsigned char code = *locp++;
10341 [ + - - + : 120 : switch (code)
- ]
10342 : : {
10343 : 40 : case DW_LLE_GNU_end_of_list_entry:
10344 : 40 : begin = 0;
10345 : 40 : end = 0;
10346 : 40 : break;
10347 : :
10348 : 0 : case DW_LLE_GNU_base_address_selection_entry:
10349 [ # # ]: 0 : if (locp >= locendp)
10350 : 0 : goto invalid_data;
10351 : 0 : begin = (Dwarf_Addr) -1;
10352 : 0 : get_uleb128 (idx, locp, locendp);
10353 [ # # ]: 0 : if (get_indexed_addr (cu, idx, &end) != 0)
10354 : 0 : end = idx; /* ... */
10355 : : break;
10356 : :
10357 : 0 : case DW_LLE_GNU_start_end_entry:
10358 [ # # ]: 0 : if (locp >= locendp)
10359 : 0 : goto invalid_data;
10360 : 0 : get_uleb128 (idx, locp, locendp);
10361 [ # # ]: 0 : if (get_indexed_addr (cu, idx, &begin) != 0)
10362 : 0 : begin = idx; /* ... */
10363 [ # # ]: 0 : if (locp >= locendp)
10364 : 0 : goto invalid_data;
10365 : 0 : get_uleb128 (idx, locp, locendp);
10366 [ # # ]: 0 : if (get_indexed_addr (cu, idx, &end) != 0)
10367 : 0 : end = idx; /* ... */
10368 : : use_base = false;
10369 : : break;
10370 : :
10371 : 80 : case DW_LLE_GNU_start_length_entry:
10372 [ - + ]: 80 : if (locp >= locendp)
10373 : 0 : goto invalid_data;
10374 : 80 : get_uleb128 (idx, locp, locendp);
10375 [ - + ]: 80 : if (get_indexed_addr (cu, idx, &begin) != 0)
10376 : 0 : begin = idx; /* ... */
10377 [ - + ]: 80 : if (locendp - locp < 4)
10378 : 0 : goto invalid_data;
10379 [ - + ]: 80 : end = read_4ubyte_unaligned_inc (dbg, locp);
10380 : 80 : end += begin;
10381 : 80 : use_base = false;
10382 : 80 : break;
10383 : :
10384 : 0 : default:
10385 : 0 : goto invalid_data;
10386 : : }
10387 : :
10388 : 120 : readp = (unsigned char *) locp;
10389 : : }
10390 [ + - ]: 318 : else if (address_size == 8)
10391 : : {
10392 [ + + ]: 318 : begin = read_8ubyte_unaligned_inc (dbg, readp);
10393 [ + + ]: 318 : end = read_8ubyte_unaligned_inc (dbg, readp);
10394 : : }
10395 : : else
10396 : : {
10397 [ # # ]: 0 : begin = read_4ubyte_unaligned_inc (dbg, readp);
10398 [ # # ]: 0 : end = read_4ubyte_unaligned_inc (dbg, readp);
10399 [ # # ]: 0 : if (begin == (Dwarf_Addr) (uint32_t) -1)
10400 : 0 : begin = (Dwarf_Addr) -1l;
10401 : : }
10402 : :
10403 [ - + ]: 438 : if (begin == (Dwarf_Addr) -1l) /* Base address entry. */
10404 : : {
10405 [ # # ]: 0 : if (first)
10406 : 0 : printf (" [%6tx] ", offset);
10407 : : else
10408 : 0 : printf (" ");
10409 : 0 : puts (_("base address"));
10410 : 0 : printf (" ");
10411 : 0 : print_dwarf_addr (dwflmod, address_size, end, end);
10412 : 0 : printf ("\n");
10413 : 0 : base = end;
10414 : 0 : first = false;
10415 : : }
10416 [ + + + + ]: 438 : else if (begin == 0 && end == 0) /* End of list entry. */
10417 : : {
10418 [ - + ]: 148 : if (first)
10419 : 438 : printf (_(" [%6tx] empty list\n"), offset);
10420 : : first = true;
10421 : : }
10422 : : else
10423 : : {
10424 : : /* We have a location expression entry. */
10425 [ + + ]: 290 : uint_fast16_t len = read_2ubyte_unaligned_inc (dbg, readp);
10426 : :
10427 [ + + ]: 290 : if (first) /* First entry in a list. */
10428 : 148 : printf (" [%6tx] ", offset);
10429 : : else
10430 : 142 : printf (" ");
10431 : :
10432 : 290 : printf ("range %" PRIx64 ", %" PRIx64 "\n", begin, end);
10433 [ + + ]: 290 : if (! print_unresolved_addresses)
10434 : : {
10435 [ + + ]: 230 : Dwarf_Addr dab = use_base ? base + begin : begin;
10436 [ + + ]: 230 : Dwarf_Addr dae = use_base ? base + end : end;
10437 : 230 : printf (" ");
10438 : 230 : print_dwarf_addr (dwflmod, address_size, dab, dab);
10439 : 230 : printf ("..\n ");
10440 : 230 : print_dwarf_addr (dwflmod, address_size, dae - 1, dae);
10441 : 230 : printf ("\n");
10442 : : }
10443 : :
10444 [ - + ]: 290 : if (endp - readp <= (ptrdiff_t) len)
10445 : : {
10446 : 0 : fputs (_(" <INVALID DATA>\n"), stdout);
10447 : 0 : break;
10448 : : }
10449 : :
10450 [ + - ]: 580 : print_ops (dwflmod, dbg, 11, 11,
10451 : 290 : cu != NULL ? cu->version : 3,
10452 : : address_size, offset_size, cu, len, readp);
10453 : :
10454 : 290 : first = false;
10455 : 290 : readp += len;
10456 : : }
10457 : : }
10458 : : }
10459 : :
10460 : : struct mac_culist
10461 : : {
10462 : : Dwarf_Die die;
10463 : : Dwarf_Off offset;
10464 : : Dwarf_Files *files;
10465 : : struct mac_culist *next;
10466 : : };
10467 : :
10468 : :
10469 : : static int
10470 : 0 : mac_compare (const void *p1, const void *p2)
10471 : : {
10472 : 0 : struct mac_culist *m1 = (struct mac_culist *) p1;
10473 : 0 : struct mac_culist *m2 = (struct mac_culist *) p2;
10474 : :
10475 [ # # ]: 0 : if (m1->offset < m2->offset)
10476 : : return -1;
10477 [ # # ]: 0 : if (m1->offset > m2->offset)
10478 : 0 : return 1;
10479 : : return 0;
10480 : : }
10481 : :
10482 : :
10483 : : static void
10484 : 0 : print_debug_macinfo_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
10485 : : Ebl *ebl,
10486 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
10487 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
10488 : : {
10489 : 0 : Elf_Data *data = get_debug_elf_data (dbg, ebl, IDX_debug_macinfo, scn);
10490 [ # # ]: 0 : if (data == NULL)
10491 : 0 : return;
10492 : :
10493 : 0 : printf (_("\
10494 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
10495 : : elf_ndxscn (scn), section_name (ebl, shdr),
10496 : 0 : (uint64_t) shdr->sh_offset);
10497 : 0 : fputc ('\n', stdout);
10498 : :
10499 : : /* There is no function in libdw to iterate over the raw content of
10500 : : the section but it is easy enough to do. */
10501 : :
10502 : : /* Get the source file information for all CUs. */
10503 : 0 : Dwarf_Off offset;
10504 : 0 : Dwarf_Off ncu = 0;
10505 : 0 : size_t hsize;
10506 : 0 : struct mac_culist *culist = NULL;
10507 : 0 : size_t nculist = 0;
10508 [ # # ]: 0 : while (dwarf_nextcu (dbg, offset = ncu, &ncu, &hsize, NULL, NULL, NULL) == 0)
10509 : : {
10510 : 0 : Dwarf_Die cudie;
10511 [ # # ]: 0 : if (dwarf_offdie (dbg, offset + hsize, &cudie) == NULL)
10512 : 0 : continue;
10513 : :
10514 : 0 : Dwarf_Attribute attr;
10515 [ # # ]: 0 : if (dwarf_attr (&cudie, DW_AT_macro_info, &attr) == NULL)
10516 : 0 : continue;
10517 : :
10518 : 0 : Dwarf_Word macoff;
10519 [ # # ]: 0 : if (dwarf_formudata (&attr, &macoff) != 0)
10520 : 0 : continue;
10521 : :
10522 : 0 : struct mac_culist *newp = (struct mac_culist *) alloca (sizeof (*newp));
10523 : 0 : newp->die = cudie;
10524 : 0 : newp->offset = macoff;
10525 : 0 : newp->files = NULL;
10526 : 0 : newp->next = culist;
10527 : 0 : culist = newp;
10528 : 0 : ++nculist;
10529 : : }
10530 : :
10531 : : /* Convert the list into an array for easier consumption. */
10532 : 0 : struct mac_culist *cus = (struct mac_culist *) alloca ((nculist + 1)
10533 : : * sizeof (*cus));
10534 : : /* Add sentinel. */
10535 : 0 : cus[nculist].offset = data->d_size;
10536 : 0 : cus[nculist].files = (Dwarf_Files *) -1l;
10537 [ # # ]: 0 : if (nculist > 0)
10538 : : {
10539 [ # # ]: 0 : for (size_t cnt = nculist - 1; culist != NULL; --cnt)
10540 : : {
10541 [ # # ]: 0 : assert (cnt < nculist);
10542 : 0 : cus[cnt] = *culist;
10543 : 0 : culist = culist->next;
10544 : : }
10545 : :
10546 : : /* Sort the array according to the offset in the .debug_macinfo
10547 : : section. Note we keep the sentinel at the end. */
10548 : 0 : qsort (cus, nculist, sizeof (*cus), mac_compare);
10549 : : }
10550 : :
10551 : 0 : const unsigned char *readp = (const unsigned char *) data->d_buf;
10552 : 0 : const unsigned char *readendp = readp + data->d_size;
10553 : 0 : int level = 1;
10554 : :
10555 : 0 : while (readp < readendp)
10556 : : {
10557 : 0 : unsigned int opcode = *readp++;
10558 : 0 : unsigned int u128;
10559 : 0 : unsigned int u128_2;
10560 : 0 : const unsigned char *endp;
10561 : :
10562 [ # # # # ]: 0 : switch (opcode)
10563 : : {
10564 : 0 : case DW_MACINFO_define:
10565 : : case DW_MACINFO_undef:
10566 : : case DW_MACINFO_vendor_ext:
10567 : : /* For the first two opcodes the parameters are
10568 : : line, string
10569 : : For the latter
10570 : : number, string.
10571 : : We can treat these cases together. */
10572 : 0 : get_uleb128 (u128, readp, readendp);
10573 : :
10574 : 0 : endp = memchr (readp, '\0', readendp - readp);
10575 [ # # ]: 0 : if (unlikely (endp == NULL))
10576 : : {
10577 : 0 : printf (_("\
10578 : : %*s*** non-terminated string at end of section"),
10579 : : level, "");
10580 : 0 : return;
10581 : : }
10582 : :
10583 [ # # ]: 0 : if (opcode == DW_MACINFO_define)
10584 : 0 : printf ("%*s#define %s, line %u\n",
10585 : : level, "", (char *) readp, u128);
10586 [ # # ]: 0 : else if (opcode == DW_MACINFO_undef)
10587 : 0 : printf ("%*s#undef %s, line %u\n",
10588 : : level, "", (char *) readp, u128);
10589 : : else
10590 : 0 : printf (" #vendor-ext %s, number %u\n", (char *) readp, u128);
10591 : :
10592 : 0 : readp = endp + 1;
10593 : 0 : break;
10594 : :
10595 : 0 : case DW_MACINFO_start_file:
10596 : : /* The two parameters are line and file index, in this order. */
10597 : 0 : get_uleb128 (u128, readp, readendp);
10598 [ # # ]: 0 : if (readendp - readp < 1)
10599 : : {
10600 : 0 : printf (_("\
10601 : : %*s*** missing DW_MACINFO_start_file argument at end of section"),
10602 : : level, "");
10603 : 0 : return;
10604 : : }
10605 : 0 : get_uleb128 (u128_2, readp, readendp);
10606 : :
10607 : : /* Find the CU DIE for this file. */
10608 : 0 : size_t macoff = readp - (const unsigned char *) data->d_buf;
10609 : 0 : const char *fname = "???";
10610 [ # # # # ]: 0 : if (macoff >= cus[0].offset && cus[0].offset != data->d_size)
10611 : : {
10612 [ # # # # ]: 0 : while (macoff >= cus[1].offset && cus[1].offset != data->d_size)
10613 : 0 : ++cus;
10614 : :
10615 [ # # ]: 0 : if (cus[0].files == NULL
10616 [ # # ]: 0 : && dwarf_getsrcfiles (&cus[0].die, &cus[0].files, NULL) != 0)
10617 : 0 : cus[0].files = (Dwarf_Files *) -1l;
10618 : :
10619 [ # # ]: 0 : if (cus[0].files != (Dwarf_Files *) -1l)
10620 : 0 : fname = (dwarf_filesrc (cus[0].files, u128_2, NULL, NULL)
10621 [ # # ]: 0 : ?: "???");
10622 : : }
10623 : :
10624 : 0 : printf ("%*sstart_file %u, [%u] %s\n",
10625 : : level, "", u128, u128_2, fname);
10626 : 0 : ++level;
10627 : 0 : break;
10628 : :
10629 : 0 : case DW_MACINFO_end_file:
10630 : 0 : --level;
10631 : 0 : printf ("%*send_file\n", level, "");
10632 : : /* Nothing more to do. */
10633 : : break;
10634 : :
10635 : 0 : default:
10636 : : // XXX gcc seems to generate files with a trailing zero.
10637 [ # # # # ]: 0 : if (unlikely (opcode != 0 || readp != readendp))
10638 [ # # ]: 0 : printf ("%*s*** invalid opcode %u\n", level, "", opcode);
10639 : : break;
10640 : : }
10641 : : }
10642 : : }
10643 : :
10644 : :
10645 : : static void
10646 : 8 : print_debug_macro_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
10647 : : Ebl *ebl,
10648 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
10649 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
10650 : : {
10651 : 8 : Elf_Data *data = get_debug_elf_data (dbg, ebl, IDX_debug_macro, scn);
10652 [ + - ]: 8 : if (data == NULL)
10653 : 0 : return;
10654 : :
10655 : 8 : printf (_("\
10656 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
10657 : : elf_ndxscn (scn), section_name (ebl, shdr),
10658 : 8 : (uint64_t) shdr->sh_offset);
10659 : 8 : fputc ('\n', stdout);
10660 : :
10661 : : /* Get the source file information for all CUs. Uses same
10662 : : datastructure as macinfo. But uses offset field to directly
10663 : : match .debug_line offset. And just stored in a list. */
10664 : 8 : Dwarf_Off offset;
10665 : 8 : Dwarf_Off ncu = 0;
10666 : 8 : size_t hsize;
10667 : 8 : struct mac_culist *culist = NULL;
10668 [ + + ]: 20 : while (dwarf_nextcu (dbg, offset = ncu, &ncu, &hsize, NULL, NULL, NULL) == 0)
10669 : : {
10670 : 12 : Dwarf_Die cudie;
10671 [ - + ]: 12 : if (dwarf_offdie (dbg, offset + hsize, &cudie) == NULL)
10672 : 0 : continue;
10673 : :
10674 : 12 : Dwarf_Attribute attr;
10675 [ - + ]: 12 : if (dwarf_attr (&cudie, DW_AT_stmt_list, &attr) == NULL)
10676 : 0 : continue;
10677 : :
10678 : 12 : Dwarf_Word lineoff;
10679 [ - + ]: 12 : if (dwarf_formudata (&attr, &lineoff) != 0)
10680 : 0 : continue;
10681 : :
10682 : 12 : struct mac_culist *newp = (struct mac_culist *) alloca (sizeof (*newp));
10683 : 12 : newp->die = cudie;
10684 : 12 : newp->offset = lineoff;
10685 : 12 : newp->files = NULL;
10686 : 12 : newp->next = culist;
10687 : 12 : culist = newp;
10688 : : }
10689 : :
10690 : 8 : const unsigned char *readp = (const unsigned char *) data->d_buf;
10691 : 8 : const unsigned char *readendp = readp + data->d_size;
10692 : :
10693 [ + + ]: 28 : while (readp < readendp)
10694 : : {
10695 : 40 : printf (_(" Offset: 0x%" PRIx64 "\n"),
10696 : 20 : (uint64_t) (readp - (const unsigned char *) data->d_buf));
10697 : :
10698 : : // Header, 2 byte version, 1 byte flag, optional .debug_line offset,
10699 : : // optional vendor extension macro entry table.
10700 [ - + ]: 20 : if (readp + 2 > readendp)
10701 : : {
10702 : 0 : invalid_data:
10703 : 0 : error (0, 0, _("invalid data"));
10704 : 0 : return;
10705 : : }
10706 [ - + ]: 20 : const uint16_t vers = read_2ubyte_unaligned_inc (dbg, readp);
10707 : 20 : printf (_(" Version: %" PRIu16 "\n"), vers);
10708 : :
10709 : : // Version 4 is the GNU extension for DWARF4. DWARF5 will use version
10710 : : // 5 when it gets standardized.
10711 [ - + ]: 20 : if (vers != 4 && vers != 5)
10712 : : {
10713 : 0 : printf (_(" unknown version, cannot parse section\n"));
10714 : 0 : return;
10715 : : }
10716 : :
10717 [ - + ]: 20 : if (readp + 1 > readendp)
10718 : 0 : goto invalid_data;
10719 : 20 : const unsigned char flag = *readp++;
10720 : 20 : printf (_(" Flag: 0x%" PRIx8), flag);
10721 [ + + ]: 20 : if (flag != 0)
10722 : : {
10723 : 12 : printf (" (");
10724 [ - + ]: 12 : if ((flag & 0x01) != 0)
10725 : : {
10726 : 0 : printf ("offset_size");
10727 [ # # ]: 0 : if ((flag & 0xFE) != 0)
10728 : 0 : printf (", ");
10729 : : }
10730 [ + - ]: 12 : if ((flag & 0x02) != 0)
10731 : : {
10732 : 12 : printf ("debug_line_offset");
10733 [ - + ]: 12 : if ((flag & 0xFC) != 0)
10734 : 0 : printf (", ");
10735 : : }
10736 [ # # ]: 0 : if ((flag & 0x04) != 0)
10737 : : {
10738 : 0 : printf ("operands_table");
10739 [ # # ]: 0 : if ((flag & 0xF8) != 0)
10740 : 0 : printf (", ");
10741 : : }
10742 [ - + ]: 12 : if ((flag & 0xF8) != 0)
10743 : 0 : printf ("unknown");
10744 : 12 : printf (")");
10745 : : }
10746 : 20 : printf ("\n");
10747 : :
10748 [ + - ]: 20 : unsigned int offset_len = (flag & 0x01) ? 8 : 4;
10749 : 20 : printf (_(" Offset length: %" PRIu8 "\n"), offset_len);
10750 : 20 : Dwarf_Off line_offset = -1;
10751 [ + + ]: 20 : if (flag & 0x02)
10752 : : {
10753 [ - + ]: 12 : if (offset_len == 8)
10754 [ # # ]: 0 : line_offset = read_8ubyte_unaligned_inc (dbg, readp);
10755 : : else
10756 [ - + ]: 12 : line_offset = read_4ubyte_unaligned_inc (dbg, readp);
10757 : 12 : printf (_(" .debug_line offset: 0x%" PRIx64 "\n"),
10758 : : line_offset);
10759 : : }
10760 : :
10761 : 12 : struct mac_culist *cu = NULL;
10762 [ + - ]: 12 : if (line_offset != (Dwarf_Off) -1)
10763 : : {
10764 : : cu = culist;
10765 [ + - + + ]: 16 : while (cu != NULL && line_offset != cu->offset)
10766 : 4 : cu = cu->next;
10767 : : }
10768 : :
10769 [ - + ]: 20 : Dwarf_Off str_offsets_base = str_offsets_base_off (dbg, (cu != NULL
10770 : : ? cu->die.cu
10771 : : : NULL));
10772 : :
10773 : 20 : const unsigned char *vendor[DW_MACRO_hi_user - DW_MACRO_lo_user + 1];
10774 [ - + ]: 20 : memset (vendor, 0, sizeof vendor);
10775 [ - + ]: 20 : if (flag & 0x04)
10776 : : {
10777 : : // 1 byte length, for each item, 1 byte opcode, uleb128 number
10778 : : // of arguments, for each argument 1 byte form code.
10779 [ # # ]: 0 : if (readp + 1 > readendp)
10780 : 0 : goto invalid_data;
10781 : 0 : unsigned int tlen = *readp++;
10782 : 0 : printf (_(" extension opcode table, %" PRIu8 " items:\n"),
10783 : : tlen);
10784 [ # # ]: 0 : for (unsigned int i = 0; i < tlen; i++)
10785 : : {
10786 [ # # ]: 0 : if (readp + 1 > readendp)
10787 : 0 : goto invalid_data;
10788 : 0 : unsigned int opcode = *readp++;
10789 : 0 : printf (_(" [%" PRIx8 "]"), opcode);
10790 : 0 : if (opcode < DW_MACRO_lo_user
10791 [ # # ]: 0 : || opcode > DW_MACRO_hi_user)
10792 : 0 : goto invalid_data;
10793 : : // Record the start of description for this vendor opcode.
10794 : : // uleb128 nr args, 1 byte per arg form.
10795 : 0 : vendor[opcode - DW_MACRO_lo_user] = readp;
10796 [ # # ]: 0 : if (readp + 1 > readendp)
10797 : 0 : goto invalid_data;
10798 : 0 : unsigned int args = *readp++;
10799 [ # # ]: 0 : if (args > 0)
10800 : : {
10801 : 0 : printf (_(" %" PRIu8 " arguments:"), args);
10802 : 0 : while (args > 0)
10803 : : {
10804 : 0 : if (readp + 1 > readendp)
10805 : 0 : goto invalid_data;
10806 : 0 : unsigned int form = *readp++;
10807 : 0 : printf (" %s", dwarf_form_name (form));
10808 [ # # ]: 0 : if (! libdw_valid_user_form (form))
10809 : 0 : goto invalid_data;
10810 : 0 : args--;
10811 [ # # ]: 0 : if (args > 0)
10812 [ # # ]: 0 : putchar (',');
10813 : : }
10814 : : }
10815 : : else
10816 : 0 : printf (_(" no arguments."));
10817 : 0 : putchar ('\n');
10818 : : }
10819 : : }
10820 : 20 : putchar ('\n');
10821 : :
10822 : 20 : int level = 1;
10823 [ - + ]: 20 : if (readp + 1 > readendp)
10824 : 0 : goto invalid_data;
10825 : 20 : unsigned int opcode = *readp++;
10826 : 20 : while (opcode != 0)
10827 : : {
10828 : 3026 : unsigned int u128;
10829 : 3026 : unsigned int u128_2;
10830 : 3026 : const unsigned char *endp;
10831 : 3026 : uint64_t off;
10832 : :
10833 [ + + + - : 3026 : switch (opcode)
+ + + - -
- + + - ]
10834 : : {
10835 : 20 : case DW_MACRO_start_file:
10836 : 20 : get_uleb128 (u128, readp, readendp);
10837 [ - + ]: 20 : if (readp >= readendp)
10838 : 0 : goto invalid_data;
10839 : 20 : get_uleb128 (u128_2, readp, readendp);
10840 : :
10841 : : /* Find the CU DIE that matches this line offset. */
10842 : 20 : const char *fname = "???";
10843 [ - + ]: 20 : if (cu != NULL)
10844 : : {
10845 [ + + ]: 20 : if (cu->files == NULL
10846 [ - + ]: 12 : && dwarf_getsrcfiles (&cu->die, &cu->files,
10847 : : NULL) != 0)
10848 : 0 : cu->files = (Dwarf_Files *) -1l;
10849 : :
10850 [ - + ]: 20 : if (cu->files != (Dwarf_Files *) -1l)
10851 : 20 : fname = (dwarf_filesrc (cu->files, u128_2,
10852 [ - + ]: 20 : NULL, NULL) ?: "???");
10853 : : }
10854 : 20 : printf ("%*sstart_file %u, [%u] %s\n",
10855 : : level, "", u128, u128_2, fname);
10856 : 20 : ++level;
10857 : 20 : break;
10858 : :
10859 : 20 : case DW_MACRO_end_file:
10860 : 20 : --level;
10861 : 20 : printf ("%*send_file\n", level, "");
10862 : : break;
10863 : :
10864 : 2 : case DW_MACRO_define:
10865 : 2 : get_uleb128 (u128, readp, readendp);
10866 : 2 : endp = memchr (readp, '\0', readendp - readp);
10867 [ - + ]: 2 : if (endp == NULL)
10868 : 0 : goto invalid_data;
10869 : 2 : printf ("%*s#define %s, line %u\n",
10870 : : level, "", readp, u128);
10871 : 2 : readp = endp + 1;
10872 : 2 : break;
10873 : :
10874 : 0 : case DW_MACRO_undef:
10875 : 0 : get_uleb128 (u128, readp, readendp);
10876 : 0 : endp = memchr (readp, '\0', readendp - readp);
10877 [ # # ]: 0 : if (endp == NULL)
10878 : 0 : goto invalid_data;
10879 : 0 : printf ("%*s#undef %s, line %u\n",
10880 : : level, "", readp, u128);
10881 : 0 : readp = endp + 1;
10882 : 0 : break;
10883 : :
10884 : 1414 : case DW_MACRO_define_strp:
10885 : 1414 : get_uleb128 (u128, readp, readendp);
10886 [ - + ]: 1414 : if (readp + offset_len > readendp)
10887 : 0 : goto invalid_data;
10888 [ - + ]: 1414 : if (offset_len == 8)
10889 [ # # ]: 0 : off = read_8ubyte_unaligned_inc (dbg, readp);
10890 : : else
10891 [ - + ]: 1414 : off = read_4ubyte_unaligned_inc (dbg, readp);
10892 : 1414 : printf ("%*s#define %s, line %u (indirect)\n",
10893 : : level, "", dwarf_getstring (dbg, off, NULL), u128);
10894 : : break;
10895 : :
10896 : 2 : case DW_MACRO_undef_strp:
10897 : 2 : get_uleb128 (u128, readp, readendp);
10898 [ - + ]: 2 : if (readp + offset_len > readendp)
10899 : 0 : goto invalid_data;
10900 [ - + ]: 2 : if (offset_len == 8)
10901 [ # # ]: 0 : off = read_8ubyte_unaligned_inc (dbg, readp);
10902 : : else
10903 [ - + ]: 2 : off = read_4ubyte_unaligned_inc (dbg, readp);
10904 : 2 : printf ("%*s#undef %s, line %u (indirect)\n",
10905 : : level, "", dwarf_getstring (dbg, off, NULL), u128);
10906 : : break;
10907 : :
10908 : 12 : case DW_MACRO_import:
10909 [ - + ]: 12 : if (readp + offset_len > readendp)
10910 : 0 : goto invalid_data;
10911 [ - + ]: 12 : if (offset_len == 8)
10912 [ # # ]: 0 : off = read_8ubyte_unaligned_inc (dbg, readp);
10913 : : else
10914 [ - + ]: 12 : off = read_4ubyte_unaligned_inc (dbg, readp);
10915 : 12 : printf ("%*s#include offset 0x%" PRIx64 "\n",
10916 : : level, "", off);
10917 : : break;
10918 : :
10919 : 0 : case DW_MACRO_define_sup:
10920 : 0 : get_uleb128 (u128, readp, readendp);
10921 : 0 : printf ("%*s#define ", level, "");
10922 : 0 : readp = print_form_data (dbg, DW_FORM_strp_sup,
10923 : : readp, readendp, offset_len,
10924 : : str_offsets_base);
10925 : 0 : printf (", line %u (sup)\n", u128);
10926 : : break;
10927 : :
10928 : 0 : case DW_MACRO_undef_sup:
10929 : 0 : get_uleb128 (u128, readp, readendp);
10930 : 0 : printf ("%*s#undef ", level, "");
10931 : 0 : readp = print_form_data (dbg, DW_FORM_strp_sup,
10932 : : readp, readendp, offset_len,
10933 : : str_offsets_base);
10934 : 0 : printf (", line %u (sup)\n", u128);
10935 : : break;
10936 : :
10937 : 0 : case DW_MACRO_import_sup:
10938 [ # # ]: 0 : if (readp + offset_len > readendp)
10939 : 0 : goto invalid_data;
10940 [ # # ]: 0 : if (offset_len == 8)
10941 [ # # ]: 0 : off = read_8ubyte_unaligned_inc (dbg, readp);
10942 : : else
10943 [ # # ]: 0 : off = read_4ubyte_unaligned_inc (dbg, readp);
10944 : : // XXX Needs support for reading from supplementary object file.
10945 : 0 : printf ("%*s#include offset 0x%" PRIx64 " (sup)\n",
10946 : : level, "", off);
10947 : : break;
10948 : :
10949 : 1552 : case DW_MACRO_define_strx:
10950 : 1552 : get_uleb128 (u128, readp, readendp);
10951 : 1552 : printf ("%*s#define ", level, "");
10952 : 1552 : readp = print_form_data (dbg, DW_FORM_strx,
10953 : : readp, readendp, offset_len,
10954 : : str_offsets_base);
10955 : 1552 : printf (", line %u (strx)\n", u128);
10956 : : break;
10957 : :
10958 : 4 : case DW_MACRO_undef_strx:
10959 : 4 : get_uleb128 (u128, readp, readendp);
10960 : 4 : printf ("%*s#undef ", level, "");
10961 : 4 : readp = print_form_data (dbg, DW_FORM_strx,
10962 : : readp, readendp, offset_len,
10963 : : str_offsets_base);
10964 : 4 : printf (", line %u (strx)\n", u128);
10965 : : break;
10966 : :
10967 : : default:
10968 : 0 : printf ("%*svendor opcode 0x%" PRIx8, level, "", opcode);
10969 [ # # ]: 0 : if (opcode < DW_MACRO_lo_user
10970 : : || opcode > DW_MACRO_lo_user
10971 [ # # ]: 0 : || vendor[opcode - DW_MACRO_lo_user] == NULL)
10972 : 0 : goto invalid_data;
10973 : :
10974 : 0 : const unsigned char *op_desc;
10975 : 0 : op_desc = vendor[opcode - DW_MACRO_lo_user];
10976 : :
10977 : : // Just skip the arguments, we cannot really interpret them,
10978 : : // but print as much as we can.
10979 : 0 : unsigned int args = *op_desc++;
10980 [ # # ]: 0 : while (args > 0 && readp < readendp)
10981 : : {
10982 : 0 : unsigned int form = *op_desc++;
10983 : 0 : readp = print_form_data (dbg, form, readp, readendp,
10984 : : offset_len, str_offsets_base);
10985 : 0 : args--;
10986 [ # # ]: 0 : if (args > 0)
10987 [ # # ]: 0 : printf (", ");
10988 : : }
10989 : 0 : putchar ('\n');
10990 : : }
10991 : :
10992 [ - + ]: 3026 : if (readp + 1 > readendp)
10993 : 0 : goto invalid_data;
10994 : 3026 : opcode = *readp++;
10995 [ + + ]: 3026 : if (opcode == 0)
10996 [ + + ]: 3066 : putchar ('\n');
10997 : : }
10998 : : }
10999 : : }
11000 : :
11001 : :
11002 : : /* Callback for printing global names. */
11003 : : static int
11004 : 68 : print_pubnames (Dwarf *dbg __attribute__ ((unused)), Dwarf_Global *global,
11005 : : void *arg)
11006 : : {
11007 : 68 : int *np = (int *) arg;
11008 : :
11009 : 136 : printf (_(" [%5d] DIE offset: %6" PRId64
11010 : : ", CU DIE offset: %6" PRId64 ", name: %s\n"),
11011 : 68 : (*np)++, global->die_offset, global->cu_offset, global->name);
11012 : :
11013 : 68 : return 0;
11014 : : }
11015 : :
11016 : :
11017 : : /* Print the known exported symbols in the DWARF section '.debug_pubnames'. */
11018 : : static void
11019 : 16 : print_debug_pubnames_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
11020 : : Ebl *ebl,
11021 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
11022 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
11023 : : {
11024 : : /* Check section actually exists. */
11025 [ - + ]: 16 : if (get_debug_elf_data (dbg, ebl, IDX_debug_pubnames, scn) == NULL)
11026 : 0 : return;
11027 : :
11028 : 16 : printf (_("\nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
11029 : : elf_ndxscn (scn), section_name (ebl, shdr),
11030 : 16 : (uint64_t) shdr->sh_offset);
11031 : :
11032 : 16 : int n = 0;
11033 : 16 : (void) dwarf_getpubnames (dbg, print_pubnames, &n, 0);
11034 : : }
11035 : :
11036 : : /* Print the content of the DWARF string section '.debug_str'
11037 : : or 'debug_line_str'. */
11038 : : static void
11039 : 134 : print_debug_str_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
11040 : : Ebl *ebl,
11041 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
11042 : : Elf_Scn *scn, GElf_Shdr *shdr,
11043 : : Dwarf *dbg __attribute__ ((unused)))
11044 : : {
11045 : 134 : const char *name = section_name (ebl, shdr);
11046 [ + + ]: 134 : int idx = ((name != NULL && strstr (name, "debug_line_str") != NULL)
11047 [ + - ]: 134 : ? IDX_debug_line_str : IDX_debug_str);
11048 : 134 : Elf_Data *data = get_debug_elf_data (dbg, ebl, idx, scn);
11049 [ + - ]: 134 : if (data == NULL)
11050 : : return;
11051 : :
11052 : 134 : const size_t sh_size = data->d_size;
11053 : :
11054 : : /* Compute floor(log16(shdr->sh_size)). */
11055 : 134 : GElf_Addr tmp = sh_size;
11056 : 134 : int digits = 1;
11057 [ + + ]: 466 : while (tmp >= 16)
11058 : : {
11059 : 332 : ++digits;
11060 : 332 : tmp >>= 4;
11061 : : }
11062 : 134 : digits = MAX (4, digits);
11063 : :
11064 : 134 : printf (_("\nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"
11065 : : " %*s String\n"),
11066 : : elf_ndxscn (scn),
11067 : 134 : section_name (ebl, shdr), (uint64_t) shdr->sh_offset,
11068 : : /* TRANS: the debugstr| prefix makes the string unique. */
11069 [ + - ]: 134 : digits + 2, sgettext ("debugstr|Offset"));
11070 : :
11071 : 134 : Dwarf_Off offset = 0;
11072 [ + + ]: 161080 : while (offset < sh_size)
11073 : : {
11074 : 160946 : size_t len;
11075 : 160946 : const char *str = (const char *) data->d_buf + offset;
11076 : 160946 : const char *endp = memchr (str, '\0', sh_size - offset);
11077 [ - + ]: 160946 : if (unlikely (endp == NULL))
11078 : : {
11079 : 0 : printf (_(" *** error, missing string terminator\n"));
11080 : : break;
11081 : : }
11082 : :
11083 : 160946 : printf (" [%*" PRIx64 "] \"%s\"\n", digits, (uint64_t) offset, str);
11084 : 160946 : len = endp - str;
11085 : 160946 : offset += len + 1;
11086 : : }
11087 : : }
11088 : :
11089 : : static void
11090 : 12 : print_debug_str_offsets_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
11091 : : Ebl *ebl,
11092 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
11093 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
11094 : : {
11095 : 12 : Elf_Data *data = get_debug_elf_data (dbg, ebl, IDX_debug_str_offsets, scn);
11096 [ + - ]: 12 : if (data == NULL)
11097 : : return;
11098 : :
11099 : 12 : printf (_("\
11100 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
11101 : : elf_ndxscn (scn), section_name (ebl, shdr),
11102 : 12 : (uint64_t) shdr->sh_offset);
11103 : :
11104 [ + - ]: 12 : if (shdr->sh_size == 0)
11105 : : return;
11106 : :
11107 : 12 : size_t idx = 0;
11108 : 12 : sort_listptr (&known_stroffbases, "str_offsets");
11109 : :
11110 : 12 : const unsigned char *start = (const unsigned char *) data->d_buf;
11111 : 12 : const unsigned char *readp = start;
11112 : 12 : const unsigned char *readendp = ((const unsigned char *) data->d_buf
11113 : 12 : + data->d_size);
11114 : :
11115 : 12 : while (readp < readendp)
11116 : : {
11117 : : /* Most string offset tables will have a header. For split
11118 : : dwarf unit GNU DebugFission didn't add one. But they were
11119 : : also only defined for split units (main or skeleton units
11120 : : didn't have indirect strings). So if we don't have a
11121 : : DW_AT_str_offsets_base at all and this is offset zero, then
11122 : : just start printing offsets immediately, if this is a .dwo
11123 : : section. */
11124 : 16 : Dwarf_Off off = (Dwarf_Off) (readp
11125 : 16 : - (const unsigned char *) data->d_buf);
11126 : :
11127 : 16 : printf ("Table at offset %" PRIx64 " ", off);
11128 : :
11129 [ + + ]: 16 : struct listptr *listptr = get_listptr (&known_stroffbases, idx++);
11130 : 16 : const unsigned char *next_unitp = readendp;
11131 : 8 : uint8_t offset_size;
11132 : 8 : bool has_header;
11133 [ - + ]: 8 : if (listptr == NULL)
11134 : : {
11135 : : /* This can happen for .dwo files. There is only an header
11136 : : in the case this is a version 5 split DWARF file. */
11137 : 8 : Dwarf_CU *cu;
11138 : 8 : uint8_t unit_type;
11139 [ - + ]: 8 : if (dwarf_get_units (dbg, NULL, &cu, NULL, &unit_type,
11140 : : NULL, NULL) != 0)
11141 : : {
11142 : 0 : error (0, 0, "Warning: Cannot find any DWARF unit.");
11143 : : /* Just guess some values. */
11144 : 0 : has_header = false;
11145 : 0 : offset_size = 4;
11146 : : }
11147 [ + - ]: 8 : else if (off == 0
11148 : 8 : && (unit_type == DW_UT_split_type
11149 [ + - ]: 8 : || unit_type == DW_UT_split_compile))
11150 : : {
11151 : 8 : has_header = cu->version > 4;
11152 : 8 : offset_size = cu->offset_size;
11153 : : }
11154 : : else
11155 : : {
11156 : 0 : error (0, 0,
11157 : : "Warning: No CU references .debug_str_offsets after %"
11158 : : PRIx64, off);
11159 : 0 : has_header = cu->version > 4;
11160 : 0 : offset_size = cu->offset_size;
11161 : : }
11162 : 8 : printf ("\n");
11163 : : }
11164 : : else
11165 : : {
11166 : : /* This must be DWARF5, since GNU DebugFission didn't define
11167 : : DW_AT_str_offsets_base. */
11168 : 8 : has_header = true;
11169 : :
11170 : 8 : Dwarf_Die cudie;
11171 [ - + ]: 8 : if (dwarf_cu_die (listptr->cu, &cudie,
11172 : : NULL, NULL, NULL, NULL,
11173 : : NULL, NULL) == NULL)
11174 : 0 : printf ("Unknown CU (%s):\n", dwarf_errmsg (-1));
11175 : : else
11176 : 8 : printf ("for CU [%6" PRIx64 "]:\n", dwarf_dieoffset (&cudie));
11177 : : }
11178 : :
11179 [ + + ]: 16 : if (has_header)
11180 : : {
11181 : 12 : uint64_t unit_length;
11182 : 12 : uint16_t version;
11183 : 12 : uint16_t padding;
11184 : :
11185 [ - + ]: 12 : unit_length = read_4ubyte_unaligned_inc (dbg, readp);
11186 [ - + ]: 12 : if (unlikely (unit_length == 0xffffffff))
11187 : : {
11188 [ # # ]: 0 : if (unlikely (readp > readendp - 8))
11189 : : {
11190 : 0 : invalid_data:
11191 : 0 : error (0, 0, "Invalid data");
11192 : 0 : return;
11193 : : }
11194 [ # # ]: 0 : unit_length = read_8ubyte_unaligned_inc (dbg, readp);
11195 : 0 : offset_size = 8;
11196 : : }
11197 : : else
11198 : : offset_size = 4;
11199 : :
11200 : 12 : printf ("\n");
11201 : 12 : printf (_(" Length: %8" PRIu64 "\n"),
11202 : : unit_length);
11203 : 12 : printf (_(" Offset size: %8" PRIu8 "\n"),
11204 : : offset_size);
11205 : :
11206 : : /* We need at least 2-bytes (version) + 2-bytes (padding) =
11207 : : 4 bytes to complete the header. And this unit cannot go
11208 : : beyond the section data. */
11209 [ + - ]: 12 : if (readp > readendp - 4
11210 [ + - ]: 12 : || unit_length < 4
11211 [ - + ]: 12 : || unit_length > (uint64_t) (readendp - readp))
11212 : 0 : goto invalid_data;
11213 : :
11214 : 12 : next_unitp = readp + unit_length;
11215 : :
11216 [ - + ]: 12 : version = read_2ubyte_unaligned_inc (dbg, readp);
11217 : 12 : printf (_(" DWARF version: %8" PRIu16 "\n"), version);
11218 : :
11219 [ - + ]: 12 : if (version != 5)
11220 : : {
11221 : 0 : error (0, 0, _("Unknown version"));
11222 : 0 : goto next_unit;
11223 : : }
11224 : :
11225 [ - + ]: 12 : padding = read_2ubyte_unaligned_inc (dbg, readp);
11226 : 12 : printf (_(" Padding: %8" PRIx16 "\n"), padding);
11227 : :
11228 [ + + ]: 12 : if (listptr != NULL
11229 [ - + ]: 8 : && listptr->offset != (Dwarf_Off) (readp - start))
11230 : : {
11231 : 0 : error (0, 0, "String offsets index doesn't start after header");
11232 : 0 : goto next_unit;
11233 : : }
11234 : :
11235 : 12 : printf ("\n");
11236 : : }
11237 : :
11238 : 16 : int digits = 1;
11239 : 16 : size_t offsets = (next_unitp - readp) / offset_size;
11240 [ + + ]: 24 : while (offsets >= 10)
11241 : : {
11242 : 8 : ++digits;
11243 : 8 : offsets /= 10;
11244 : : }
11245 : :
11246 : 16 : unsigned int uidx = 0;
11247 : 16 : size_t index_offset = readp - (const unsigned char *) data->d_buf;
11248 : 16 : printf (" Offsets start at 0x%zx:\n", index_offset);
11249 : 204 : while (readp <= next_unitp - offset_size)
11250 : : {
11251 : 188 : Dwarf_Word offset;
11252 [ + - ]: 188 : if (offset_size == 4)
11253 [ - + ]: 188 : offset = read_4ubyte_unaligned_inc (dbg, readp);
11254 : : else
11255 [ # # ]: 0 : offset = read_8ubyte_unaligned_inc (dbg, readp);
11256 : 188 : const char *str = dwarf_getstring (dbg, offset, NULL);
11257 [ - + + + ]: 392 : printf (" [%*u] [%*" PRIx64 "] \"%s\"\n",
11258 : 188 : digits, uidx++, (int) offset_size * 2, offset, str ?: "???");
11259 : : }
11260 : 16 : printf ("\n");
11261 : :
11262 [ + - ]: 16 : if (readp != next_unitp)
11263 [ + + ]: 28 : error (0, 0, "extra %zd bytes at end of unit",
11264 : 0 : (size_t) (next_unitp - readp));
11265 : :
11266 : 16 : next_unit:
11267 : : readp = next_unitp;
11268 : : }
11269 : : }
11270 : :
11271 : :
11272 : : /* Print the content of the call frame search table section
11273 : : '.eh_frame_hdr'. */
11274 : : static void
11275 : 140 : print_debug_frame_hdr_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
11276 : : Ebl *ebl __attribute__ ((unused)),
11277 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
11278 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
11279 : : {
11280 : 140 : printf (_("\
11281 : : \nCall frame search table section [%2zu] '.eh_frame_hdr':\n"),
11282 : : elf_ndxscn (scn));
11283 : :
11284 : 140 : Elf_Data *data = elf_rawdata (scn, NULL);
11285 : :
11286 [ - + ]: 140 : if (unlikely (data == NULL))
11287 : : {
11288 : 0 : error (0, 0, _("cannot get %s content: %s"),
11289 : : ".eh_frame_hdr", elf_errmsg (-1));
11290 : 0 : return;
11291 : : }
11292 : :
11293 : 140 : const unsigned char *readp = data->d_buf;
11294 : 140 : const unsigned char *const dataend = ((unsigned char *) data->d_buf
11295 : 140 : + data->d_size);
11296 : :
11297 [ - + ]: 140 : if (unlikely (readp + 4 > dataend))
11298 : : {
11299 : 0 : invalid_data:
11300 : 0 : error (0, 0, _("invalid data"));
11301 : 0 : return;
11302 : : }
11303 : :
11304 : 140 : unsigned int version = *readp++;
11305 : 140 : unsigned int eh_frame_ptr_enc = *readp++;
11306 : 140 : unsigned int fde_count_enc = *readp++;
11307 : 140 : unsigned int table_enc = *readp++;
11308 : :
11309 : 140 : printf (" version: %u\n"
11310 : : " eh_frame_ptr_enc: %#x ",
11311 : : version, eh_frame_ptr_enc);
11312 : 140 : print_encoding_base ("", eh_frame_ptr_enc);
11313 : 140 : printf (" fde_count_enc: %#x ", fde_count_enc);
11314 : 140 : print_encoding_base ("", fde_count_enc);
11315 : 140 : printf (" table_enc: %#x ", table_enc);
11316 : 140 : print_encoding_base ("", table_enc);
11317 : :
11318 : 140 : uint64_t eh_frame_ptr = 0;
11319 [ + - ]: 140 : if (eh_frame_ptr_enc != DW_EH_PE_omit)
11320 : : {
11321 : 140 : readp = read_encoded (eh_frame_ptr_enc, readp, dataend, &eh_frame_ptr,
11322 : : dbg);
11323 [ - + ]: 140 : if (unlikely (readp == NULL))
11324 : 0 : goto invalid_data;
11325 : :
11326 : 140 : printf (" eh_frame_ptr: %#" PRIx64, eh_frame_ptr);
11327 [ + - ]: 140 : if ((eh_frame_ptr_enc & 0x70) == DW_EH_PE_pcrel)
11328 : 280 : printf (" (offset: %#" PRIx64 ")",
11329 : : /* +4 because of the 4 byte header of the section. */
11330 : 140 : (uint64_t) shdr->sh_offset + 4 + eh_frame_ptr);
11331 : :
11332 : 140 : putchar ('\n');
11333 : : }
11334 : :
11335 : 140 : uint64_t fde_count = 0;
11336 [ + - ]: 140 : if (fde_count_enc != DW_EH_PE_omit)
11337 : : {
11338 : 140 : readp = read_encoded (fde_count_enc, readp, dataend, &fde_count, dbg);
11339 [ - + ]: 140 : if (unlikely (readp == NULL))
11340 : 0 : goto invalid_data;
11341 : :
11342 : 140 : printf (" fde_count: %" PRIu64 "\n", fde_count);
11343 : : }
11344 : :
11345 [ + - + - ]: 140 : if (fde_count == 0 || table_enc == DW_EH_PE_omit)
11346 : : return;
11347 : :
11348 : 140 : puts (" Table:");
11349 : :
11350 : : /* Optimize for the most common case. */
11351 [ + - ]: 140 : if (table_enc == (DW_EH_PE_datarel | DW_EH_PE_sdata4))
11352 : 21912 : while (fde_count > 0 && readp + 8 <= dataend)
11353 : : {
11354 [ - + ]: 21772 : int32_t initial_location = read_4sbyte_unaligned_inc (dbg, readp);
11355 : 21772 : uint64_t initial_offset = ((uint64_t) shdr->sh_offset
11356 : 21772 : + (int64_t) initial_location);
11357 [ - + ]: 21772 : int32_t address = read_4sbyte_unaligned_inc (dbg, readp);
11358 : : // XXX Possibly print symbol name or section offset for initial_offset
11359 [ + + ]: 21912 : printf (" %#" PRIx32 " (offset: %#6" PRIx64 ") -> %#" PRIx32
11360 : : " fde=[%6" PRIx64 "]\n",
11361 : : initial_location, initial_offset,
11362 : 21772 : address, address - (eh_frame_ptr + 4));
11363 : : }
11364 : : else
11365 : 140 : while (0 && readp < dataend)
11366 : : {
11367 : :
11368 : 140 : }
11369 : : }
11370 : :
11371 : :
11372 : : /* Print the content of the exception handling table section
11373 : : '.eh_frame_hdr'. */
11374 : : static void
11375 : 0 : print_debug_exception_table (Dwfl_Module *dwflmod __attribute__ ((unused)),
11376 : : Ebl *ebl __attribute__ ((unused)),
11377 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
11378 : : Elf_Scn *scn,
11379 : : GElf_Shdr *shdr __attribute__ ((unused)),
11380 : : Dwarf *dbg __attribute__ ((unused)))
11381 : : {
11382 : 0 : printf (_("\
11383 : : \nException handling table section [%2zu] '.gcc_except_table':\n"),
11384 : : elf_ndxscn (scn));
11385 : :
11386 : 0 : Elf_Data *data = elf_rawdata (scn, NULL);
11387 : :
11388 [ # # ]: 0 : if (unlikely (data == NULL))
11389 : : {
11390 : 0 : error (0, 0, _("cannot get %s content: %s"),
11391 : : ".gcc_except_table", elf_errmsg (-1));
11392 : 0 : return;
11393 : : }
11394 : :
11395 : 0 : const unsigned char *readp = data->d_buf;
11396 : 0 : const unsigned char *const dataend = readp + data->d_size;
11397 : :
11398 [ # # ]: 0 : if (unlikely (readp + 1 > dataend))
11399 : : {
11400 : 0 : invalid_data:
11401 : 0 : error (0, 0, _("invalid data"));
11402 : 0 : return;
11403 : : }
11404 : 0 : unsigned int lpstart_encoding = *readp++;
11405 : 0 : printf (_(" LPStart encoding: %#x "), lpstart_encoding);
11406 : 0 : print_encoding_base ("", lpstart_encoding);
11407 [ # # ]: 0 : if (lpstart_encoding != DW_EH_PE_omit)
11408 : : {
11409 : 0 : uint64_t lpstart;
11410 : 0 : readp = read_encoded (lpstart_encoding, readp, dataend, &lpstart, dbg);
11411 : 0 : printf (" LPStart: %#" PRIx64 "\n", lpstart);
11412 : : }
11413 : :
11414 [ # # ]: 0 : if (unlikely (readp + 1 > dataend))
11415 : 0 : goto invalid_data;
11416 : 0 : unsigned int ttype_encoding = *readp++;
11417 : 0 : printf (_(" TType encoding: %#x "), ttype_encoding);
11418 : 0 : print_encoding_base ("", ttype_encoding);
11419 : 0 : const unsigned char *ttype_base = NULL;
11420 [ # # ]: 0 : if (ttype_encoding != DW_EH_PE_omit)
11421 : : {
11422 : 0 : unsigned int ttype_base_offset;
11423 [ # # ]: 0 : if (readp >= dataend)
11424 : 0 : goto invalid_data;
11425 : 0 : get_uleb128 (ttype_base_offset, readp, dataend);
11426 : 0 : printf (" TType base offset: %#x\n", ttype_base_offset);
11427 [ # # ]: 0 : if ((size_t) (dataend - readp) > ttype_base_offset)
11428 : 0 : ttype_base = readp + ttype_base_offset;
11429 : : }
11430 : :
11431 [ # # ]: 0 : if (unlikely (readp + 1 > dataend))
11432 : 0 : goto invalid_data;
11433 : 0 : unsigned int call_site_encoding = *readp++;
11434 : 0 : printf (_(" Call site encoding: %#x "), call_site_encoding);
11435 : 0 : print_encoding_base ("", call_site_encoding);
11436 : 0 : unsigned int call_site_table_len;
11437 [ # # ]: 0 : if (readp >= dataend)
11438 : 0 : goto invalid_data;
11439 : 0 : get_uleb128 (call_site_table_len, readp, dataend);
11440 : :
11441 : 0 : const unsigned char *const action_table = readp + call_site_table_len;
11442 [ # # ]: 0 : if (unlikely (action_table > dataend))
11443 : 0 : goto invalid_data;
11444 : : unsigned int u = 0;
11445 : : unsigned int max_action = 0;
11446 [ # # ]: 0 : while (readp < action_table)
11447 : : {
11448 [ # # ]: 0 : if (u == 0)
11449 : 0 : puts (_("\n Call site table:"));
11450 : :
11451 : 0 : uint64_t call_site_start;
11452 : 0 : readp = read_encoded (call_site_encoding, readp, dataend,
11453 : : &call_site_start, dbg);
11454 : 0 : uint64_t call_site_length;
11455 : 0 : readp = read_encoded (call_site_encoding, readp, dataend,
11456 : : &call_site_length, dbg);
11457 : 0 : uint64_t landing_pad;
11458 : 0 : readp = read_encoded (call_site_encoding, readp, dataend,
11459 : : &landing_pad, dbg);
11460 : 0 : unsigned int action;
11461 [ # # ]: 0 : if (readp >= dataend)
11462 : 0 : goto invalid_data;
11463 : 0 : get_uleb128 (action, readp, dataend);
11464 : 0 : max_action = MAX (action, max_action);
11465 : 0 : printf (_(" [%4u] Call site start: %#" PRIx64 "\n"
11466 : : " Call site length: %" PRIu64 "\n"
11467 : : " Landing pad: %#" PRIx64 "\n"
11468 : : " Action: %u\n"),
11469 : : u++, call_site_start, call_site_length, landing_pad, action);
11470 : : }
11471 [ # # ]: 0 : if (readp != action_table)
11472 : 0 : goto invalid_data;
11473 : :
11474 : 0 : unsigned int max_ar_filter = 0;
11475 [ # # ]: 0 : if (max_action > 0)
11476 : : {
11477 : 0 : puts ("\n Action table:");
11478 : :
11479 : 0 : size_t maxdata = (size_t) (dataend - action_table);
11480 [ # # # # ]: 0 : if (max_action > maxdata || maxdata - max_action < 1)
11481 : : {
11482 : 0 : invalid_action_table:
11483 : 0 : fputs (_(" <INVALID DATA>\n"), stdout);
11484 : 0 : return;
11485 : : }
11486 : :
11487 : 0 : const unsigned char *const action_table_end
11488 : 0 : = action_table + max_action + 1;
11489 : :
11490 : 0 : u = 0;
11491 : 0 : do
11492 : : {
11493 : 0 : int ar_filter;
11494 : 0 : get_sleb128 (ar_filter, readp, action_table_end);
11495 [ # # ]: 0 : if (ar_filter > 0 && (unsigned int) ar_filter > max_ar_filter)
11496 : 0 : max_ar_filter = ar_filter;
11497 : 0 : int ar_disp;
11498 [ # # ]: 0 : if (readp >= action_table_end)
11499 : 0 : goto invalid_action_table;
11500 : 0 : get_sleb128 (ar_disp, readp, action_table_end);
11501 : :
11502 : 0 : printf (" [%4u] ar_filter: % d\n"
11503 : : " ar_disp: % -5d",
11504 : : u, ar_filter, ar_disp);
11505 [ # # ]: 0 : if (abs (ar_disp) & 1)
11506 : 0 : printf (" -> [%4u]\n", u + (ar_disp + 1) / 2);
11507 [ # # ]: 0 : else if (ar_disp != 0)
11508 : 0 : puts (" -> ???");
11509 : : else
11510 : 0 : putchar ('\n');
11511 : 0 : ++u;
11512 : : }
11513 [ # # ]: 0 : while (readp < action_table_end);
11514 : : }
11515 : :
11516 [ # # ]: 0 : if (max_ar_filter > 0 && ttype_base != NULL)
11517 : : {
11518 : 0 : unsigned char dsize;
11519 : 0 : puts ("\n TType table:");
11520 : :
11521 : : // XXX Not *4, size of encoding;
11522 [ # # ]: 0 : switch (ttype_encoding & 7)
11523 : : {
11524 : : case DW_EH_PE_udata2:
11525 : : case DW_EH_PE_sdata2:
11526 : : dsize = 2;
11527 : : break;
11528 : : case DW_EH_PE_udata4:
11529 : : case DW_EH_PE_sdata4:
11530 : : dsize = 4;
11531 : : break;
11532 : : case DW_EH_PE_udata8:
11533 : : case DW_EH_PE_sdata8:
11534 : : dsize = 8;
11535 : : break;
11536 : 0 : default:
11537 : 0 : dsize = 0;
11538 : 0 : error (1, 0, _("invalid TType encoding"));
11539 : : }
11540 : :
11541 : 0 : if (max_ar_filter
11542 [ # # ]: 0 : > (size_t) (ttype_base - (const unsigned char *) data->d_buf) / dsize)
11543 : 0 : goto invalid_data;
11544 : :
11545 : 0 : readp = ttype_base - max_ar_filter * dsize;
11546 : 0 : do
11547 : : {
11548 : 0 : uint64_t ttype;
11549 : 0 : readp = read_encoded (ttype_encoding, readp, ttype_base, &ttype,
11550 : : dbg);
11551 : 0 : printf (" [%4u] %#" PRIx64 "\n", max_ar_filter--, ttype);
11552 : : }
11553 [ # # ]: 0 : while (readp < ttype_base);
11554 : : }
11555 : : }
11556 : :
11557 : : /* Print the content of the '.gdb_index' section.
11558 : : http://sourceware.org/gdb/current/onlinedocs/gdb/Index-Section-Format.html
11559 : : */
11560 : : static void
11561 : 8 : print_gdb_index_section (Dwfl_Module *dwflmod, Ebl *ebl,
11562 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
11563 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
11564 : : {
11565 : 8 : printf (_("\nGDB section [%2zu] '%s' at offset %#" PRIx64
11566 : : " contains %" PRId64 " bytes :\n"),
11567 : : elf_ndxscn (scn), section_name (ebl, shdr),
11568 : 8 : (uint64_t) shdr->sh_offset, (uint64_t) shdr->sh_size);
11569 : :
11570 : 8 : Elf_Data *data = elf_rawdata (scn, NULL);
11571 : :
11572 [ - + ]: 8 : if (unlikely (data == NULL))
11573 : : {
11574 : 0 : error (0, 0, _("cannot get %s content: %s"),
11575 : : ".gdb_index", elf_errmsg (-1));
11576 : 4 : return;
11577 : : }
11578 : :
11579 : : // .gdb_index is always in little endian.
11580 : 8 : Dwarf dummy_dbg = { .other_byte_order = MY_ELFDATA != ELFDATA2LSB };
11581 : 8 : dbg = &dummy_dbg;
11582 : :
11583 : 8 : const unsigned char *readp = data->d_buf;
11584 : 8 : const unsigned char *const dataend = readp + data->d_size;
11585 : :
11586 [ - + ]: 8 : if (unlikely (readp + 4 > dataend))
11587 : : {
11588 : 0 : invalid_data:
11589 : 0 : error (0, 0, _("invalid data"));
11590 : 0 : return;
11591 : : }
11592 : :
11593 : 8 : int32_t vers = read_4ubyte_unaligned (dbg, readp);
11594 : 8 : printf (_(" Version: %" PRId32 "\n"), vers);
11595 : :
11596 : : // The only difference between version 4 and version 5 is the
11597 : : // hash used for generating the table. Version 6 contains symbols
11598 : : // for inlined functions, older versions didn't. Version 7 adds
11599 : : // symbol kinds. Version 8 just indicates that it correctly includes
11600 : : // TUs for symbols. Version 9 adds shortcut table for information
11601 : : // regarding the main function.
11602 [ - + ]: 8 : if (vers < 4 || vers > 9)
11603 : : {
11604 : 0 : printf (_(" unknown version, cannot parse section\n"));
11605 : 0 : return;
11606 : : }
11607 : :
11608 : 8 : readp += 4;
11609 [ - + ]: 8 : if (unlikely (readp + 4 > dataend))
11610 : 0 : goto invalid_data;
11611 : :
11612 : 8 : uint32_t cu_off = read_4ubyte_unaligned (dbg, readp);
11613 : 8 : printf (_(" CU offset: %#" PRIx32 "\n"), cu_off);
11614 : :
11615 : 8 : readp += 4;
11616 [ - + ]: 8 : if (unlikely (readp + 4 > dataend))
11617 : 0 : goto invalid_data;
11618 : :
11619 : 8 : uint32_t tu_off = read_4ubyte_unaligned (dbg, readp);
11620 : 8 : printf (_(" TU offset: %#" PRIx32 "\n"), tu_off);
11621 : :
11622 : 8 : readp += 4;
11623 [ - + ]: 8 : if (unlikely (readp + 4 > dataend))
11624 : 0 : goto invalid_data;
11625 : :
11626 : 8 : uint32_t addr_off = read_4ubyte_unaligned (dbg, readp);
11627 : 8 : printf (_(" address offset: %#" PRIx32 "\n"), addr_off);
11628 : :
11629 : 8 : readp += 4;
11630 [ - + ]: 8 : if (unlikely (readp + 4 > dataend))
11631 : 0 : goto invalid_data;
11632 : :
11633 : 8 : uint32_t sym_off = read_4ubyte_unaligned (dbg, readp);
11634 : 8 : printf (_(" symbol offset: %#" PRIx32 "\n"), sym_off);
11635 : :
11636 : 8 : readp += 4;
11637 [ - + ]: 8 : if (unlikely (readp + 4 > dataend))
11638 : 0 : goto invalid_data;
11639 : :
11640 : 8 : uint32_t shortcut_off = 0;
11641 [ + + ]: 8 : if (vers >= 9)
11642 : : {
11643 : 4 : shortcut_off = read_4ubyte_unaligned (dbg, readp);
11644 : 4 : printf (_(" shortcut offset: %#" PRIx32 "\n"), shortcut_off);
11645 : :
11646 : 4 : readp += 4;
11647 [ - + ]: 4 : if (unlikely (readp + 4 > dataend))
11648 : 0 : goto invalid_data;
11649 : : }
11650 : :
11651 : 8 : uint32_t const_off = read_4ubyte_unaligned (dbg, readp);
11652 : 8 : printf (_(" constant offset: %#" PRIx32 "\n"), const_off);
11653 : :
11654 [ - + ]: 8 : if (unlikely ((size_t) (dataend - (const unsigned char *) data->d_buf)
11655 : : < const_off))
11656 : 0 : goto invalid_data;
11657 : :
11658 : 8 : readp = data->d_buf + cu_off;
11659 : :
11660 : 8 : const unsigned char *nextp = data->d_buf + tu_off;
11661 [ - + ]: 8 : if (tu_off >= data->d_size)
11662 : 0 : goto invalid_data;
11663 : :
11664 : 8 : size_t cu_nr = (nextp - readp) / 16;
11665 : :
11666 : 8 : printf (_("\n CU list at offset %#" PRIx32
11667 : : " contains %zu entries:\n"),
11668 : : cu_off, cu_nr);
11669 : :
11670 : 8 : size_t n = 0;
11671 [ + - + + ]: 22 : while (dataend - readp >= 16 && n < cu_nr)
11672 : : {
11673 : 14 : uint64_t off = read_8ubyte_unaligned (dbg, readp);
11674 : 14 : readp += 8;
11675 : :
11676 : 14 : uint64_t len = read_8ubyte_unaligned (dbg, readp);
11677 : 14 : readp += 8;
11678 : :
11679 : 14 : printf (" [%4zu] start: %0#8" PRIx64
11680 : : ", length: %5" PRIu64 "\n", n, off, len);
11681 : 14 : n++;
11682 : : }
11683 : :
11684 : 8 : readp = data->d_buf + tu_off;
11685 : 8 : nextp = data->d_buf + addr_off;
11686 [ - + ]: 8 : if (addr_off >= data->d_size)
11687 : 0 : goto invalid_data;
11688 : :
11689 : 8 : size_t tu_nr = (nextp - readp) / 24;
11690 : :
11691 : 8 : printf (_("\n TU list at offset %#" PRIx32
11692 : : " contains %zu entries:\n"),
11693 : : tu_off, tu_nr);
11694 : :
11695 : 8 : n = 0;
11696 [ + - + + ]: 14 : while (dataend - readp >= 24 && n < tu_nr)
11697 : : {
11698 : 6 : uint64_t off = read_8ubyte_unaligned (dbg, readp);
11699 : 6 : readp += 8;
11700 : :
11701 : 6 : uint64_t type = read_8ubyte_unaligned (dbg, readp);
11702 : 6 : readp += 8;
11703 : :
11704 : 6 : uint64_t sig = read_8ubyte_unaligned (dbg, readp);
11705 : 6 : readp += 8;
11706 : :
11707 : 6 : printf (" [%4zu] CU offset: %5" PRId64
11708 : : ", type offset: %5" PRId64
11709 : : ", signature: %0#8" PRIx64 "\n", n, off, type, sig);
11710 : 6 : n++;
11711 : : }
11712 : :
11713 : 8 : readp = data->d_buf + addr_off;
11714 : 8 : nextp = data->d_buf + sym_off;
11715 [ - + ]: 8 : if (sym_off >= data->d_size)
11716 : 0 : goto invalid_data;
11717 : :
11718 : 8 : size_t addr_nr = (nextp - readp) / 20;
11719 : :
11720 : 8 : printf (_("\n Address list at offset %#" PRIx32
11721 : : " contains %zu entries:\n"),
11722 : : addr_off, addr_nr);
11723 : :
11724 : 8 : n = 0;
11725 [ + - + + ]: 22 : while (dataend - readp >= 20 && n < addr_nr)
11726 : : {
11727 : 14 : uint64_t low = read_8ubyte_unaligned (dbg, readp);
11728 : 14 : readp += 8;
11729 : :
11730 : 14 : uint64_t high = read_8ubyte_unaligned (dbg, readp);
11731 : 14 : readp += 8;
11732 : :
11733 : 14 : uint32_t idx = read_4ubyte_unaligned (dbg, readp);
11734 : 14 : readp += 4;
11735 : :
11736 : 14 : printf (" [%4zu] ", n);
11737 : 14 : print_dwarf_addr (dwflmod, 8, low, low);
11738 : 14 : printf ("..");
11739 : 14 : print_dwarf_addr (dwflmod, 8, high - 1, high);
11740 : 14 : printf (", CU index: %5" PRId32 "\n", idx);
11741 : 14 : n++;
11742 : : }
11743 : :
11744 : 8 : const unsigned char *const_start = data->d_buf + const_off;
11745 [ - + ]: 8 : if (const_off > data->d_size)
11746 : 0 : goto invalid_data;
11747 : :
11748 : 8 : const unsigned char *shortcut_start = NULL;
11749 [ + + ]: 8 : if (vers >= 9)
11750 : : {
11751 [ - + ]: 4 : if (shortcut_off >= data->d_size)
11752 : 0 : goto invalid_data;
11753 : :
11754 : 4 : shortcut_start = data->d_buf + shortcut_off;
11755 : 4 : nextp = shortcut_start;
11756 : : }
11757 : : else
11758 : : nextp = const_start;
11759 : :
11760 : 8 : readp = data->d_buf + sym_off;
11761 : 8 : size_t sym_nr = (nextp - readp) / 8;
11762 : :
11763 : 8 : printf (_("\n Symbol table at offset %#" PRIx32
11764 : : " contains %zu slots:\n"),
11765 : : sym_off, sym_nr);
11766 : :
11767 : 8 : n = 0;
11768 [ + - + + ]: 8200 : while (dataend - readp >= 8 && n < sym_nr)
11769 : : {
11770 : 8192 : uint32_t name = read_4ubyte_unaligned (dbg, readp);
11771 : 8192 : readp += 4;
11772 : :
11773 : 8192 : uint32_t vector = read_4ubyte_unaligned (dbg, readp);
11774 : 8192 : readp += 4;
11775 : :
11776 [ + + ]: 8192 : if (name != 0 || vector != 0)
11777 : : {
11778 : 60 : const unsigned char *sym = const_start + name;
11779 [ + - - + ]: 60 : if (unlikely ((size_t) (dataend - const_start) < name
11780 : : || memchr (sym, '\0', dataend - sym) == NULL))
11781 : 0 : goto invalid_data;
11782 : :
11783 : 60 : printf (" [%4zu] symbol: %s, CUs: ", n, sym);
11784 : :
11785 : 60 : const unsigned char *readcus = const_start + vector;
11786 [ - + ]: 60 : if (unlikely ((size_t) (dataend - const_start) < vector))
11787 : 0 : goto invalid_data;
11788 : 60 : uint32_t cus = read_4ubyte_unaligned (dbg, readcus);
11789 : 60 : while (cus--)
11790 : : {
11791 : 68 : uint32_t cu_kind, cu, kind;
11792 : 68 : bool is_static;
11793 : 68 : readcus += 4;
11794 [ - + ]: 68 : if (unlikely (readcus + 4 > dataend))
11795 : 0 : goto invalid_data;
11796 : 68 : cu_kind = read_4ubyte_unaligned (dbg, readcus);
11797 : 68 : cu = cu_kind & ((1 << 24) - 1);
11798 : 68 : kind = (cu_kind >> 28) & 7;
11799 : 68 : is_static = cu_kind & (1U << 31);
11800 [ + + ]: 68 : if (cu > cu_nr - 1)
11801 : 10 : printf ("%" PRId32 "T", cu - (uint32_t) cu_nr);
11802 : : else
11803 : 58 : printf ("%" PRId32, cu);
11804 [ + + ]: 68 : if (kind != 0)
11805 : : {
11806 : 52 : printf (" (");
11807 [ + + + - : 52 : switch (kind)
- ]
11808 : : {
11809 : : case 1:
11810 : 20 : printf ("type");
11811 : : break;
11812 : : case 2:
11813 : 16 : printf ("var");
11814 : : break;
11815 : : case 3:
11816 : 16 : printf ("func");
11817 : : break;
11818 : : case 4:
11819 : 0 : printf ("other");
11820 : : break;
11821 : : default:
11822 : 0 : printf ("unknown-0x%" PRIx32, kind);
11823 : : break;
11824 : : }
11825 [ + + ]: 52 : printf (":%c)", (is_static ? 'S' : 'G'));
11826 : : }
11827 [ + + ]: 68 : if (cus > 0)
11828 [ + + ]: 136 : printf (", ");
11829 : : }
11830 : 60 : printf ("\n");
11831 : : }
11832 : 8192 : n++;
11833 : : }
11834 : :
11835 [ + + ]: 8 : if (vers < 9)
11836 : : return;
11837 : :
11838 [ - + ]: 4 : if (unlikely (shortcut_start == NULL))
11839 : 0 : goto invalid_data;
11840 : :
11841 : 4 : readp = shortcut_start;
11842 : 4 : nextp = const_start;
11843 : 4 : size_t shortcut_nr = (nextp - readp) / 4;
11844 : :
11845 [ - + ]: 4 : if (unlikely (shortcut_nr != 2))
11846 : 0 : goto invalid_data;
11847 : :
11848 : 4 : printf (_("\nShortcut table at offset %#" PRIx32 " contains %zu slots:\n"),
11849 : : shortcut_off, shortcut_nr);
11850 : :
11851 : 4 : uint32_t lang = read_4ubyte_unaligned (dbg, readp);
11852 : 4 : readp += 4;
11853 : :
11854 : : /* Include the hex number of LANG in the output if the language
11855 : : is unknown. */
11856 : 4 : const char *lang_str = dwarf_lang_string (lang);
11857 : 4 : lang_str = string_or_unknown (lang_str, lang, DW_LANG_lo_user,
11858 : : DW_LANG_hi_user, true);
11859 : :
11860 : 4 : printf (_("Language of main: %s\n"), lang_str);
11861 : 4 : printf (_("Name of main: "));
11862 : :
11863 [ + + ]: 4 : if (lang != 0)
11864 : : {
11865 : 2 : uint32_t name = read_4ubyte_unaligned (dbg, readp);
11866 : 2 : readp += 4;
11867 : 2 : const unsigned char *sym = const_start + name;
11868 : :
11869 [ + - - + ]: 2 : if (unlikely ((size_t) (dataend - const_start) < name
11870 : : || memchr (sym, '\0', dataend - sym) == NULL))
11871 : 0 : goto invalid_data;
11872 : :
11873 : 2 : printf ("%s\n", sym);
11874 : : }
11875 : : else
11876 : 4 : printf ("<unknown>\n");
11877 : : }
11878 : :
11879 : : /* Returns true and sets split DWARF CU id if there is a split compile
11880 : : unit in the given Dwarf, and no non-split units are found (before it). */
11881 : : static bool
11882 : 396 : is_split_dwarf (Dwarf *dbg, uint64_t *id, Dwarf_CU **split_cu)
11883 : : {
11884 : 396 : Dwarf_CU *cu = NULL;
11885 [ + + ]: 396 : while (dwarf_get_units (dbg, cu, &cu, NULL, NULL, NULL, NULL) == 0)
11886 : : {
11887 : 394 : uint8_t unit_type;
11888 [ + - ]: 394 : if (dwarf_cu_info (cu, NULL, &unit_type, NULL, NULL,
11889 : : id, NULL, NULL) != 0)
11890 : 394 : return false;
11891 : :
11892 [ + + ]: 394 : if (unit_type != DW_UT_split_compile && unit_type != DW_UT_split_type)
11893 : : return false;
11894 : :
11895 : : /* We really only care about the split compile unit, the types
11896 : : should be fine and self sufficient. Also they don't have an
11897 : : id that we can match with a skeleton unit. */
11898 [ + - ]: 18 : if (unit_type == DW_UT_split_compile)
11899 : : {
11900 : 18 : *split_cu = cu;
11901 : 18 : return true;
11902 : : }
11903 : : }
11904 : :
11905 : : return false;
11906 : : }
11907 : :
11908 : : /* Check that there is one and only one Dwfl_Module, return in arg. */
11909 : : static int
11910 : 18 : getone_dwflmod (Dwfl_Module *dwflmod,
11911 : : void **userdata __attribute__ ((unused)),
11912 : : const char *name __attribute__ ((unused)),
11913 : : Dwarf_Addr base __attribute__ ((unused)),
11914 : : void *arg)
11915 : : {
11916 : 18 : Dwfl_Module **m = (Dwfl_Module **) arg;
11917 [ + - ]: 18 : if (*m != NULL)
11918 : : return DWARF_CB_ABORT;
11919 : 18 : *m = dwflmod;
11920 : 18 : return DWARF_CB_OK;
11921 : : }
11922 : :
11923 : : static void
11924 : 502 : print_debug (Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr)
11925 : : {
11926 : : /* Used for skeleton file, if necessary for split DWARF. */
11927 : 502 : Dwfl *skel_dwfl = NULL;
11928 : 502 : Dwfl_Module *skel_mod = NULL;
11929 : 502 : char *skel_name = NULL;
11930 : 502 : Dwarf *split_dbg = NULL;
11931 : 502 : Dwarf_CU *split_cu = NULL;
11932 : :
11933 : : /* Before we start the real work get a debug context descriptor. */
11934 : 502 : Dwarf_Addr dwbias;
11935 : 502 : Dwarf *dbg = dwfl_module_getdwarf (dwflmod, &dwbias);
11936 : 502 : Dwarf dummy_dbg =
11937 : : {
11938 : 502 : .elf = ebl->elf,
11939 : 502 : .other_byte_order = MY_ELFDATA != ehdr->e_ident[EI_DATA]
11940 : : };
11941 [ + + ]: 502 : if (dbg == NULL)
11942 : : {
11943 [ - + ]: 106 : if ((print_debug_sections & ~(section_exception|section_frame)) != 0)
11944 : 0 : error (0, 0, _("cannot get debug context descriptor: %s"),
11945 : : dwfl_errmsg (-1));
11946 : : dbg = &dummy_dbg;
11947 : : }
11948 : : else
11949 : : {
11950 : : /* If we are asked about a split dwarf (.dwo) file, use the user
11951 : : provided, or find the corresponding skeleton file. If we got
11952 : : a skeleton file, replace the given dwflmod and dbg, with one
11953 : : derived from the skeleton file to provide enough context. */
11954 : 396 : uint64_t split_id;
11955 [ + + ]: 396 : if (is_split_dwarf (dbg, &split_id, &split_cu))
11956 : : {
11957 [ + - ]: 18 : if (dwarf_skeleton != NULL)
11958 : 18 : skel_name = strdup (dwarf_skeleton);
11959 : : else
11960 : : {
11961 : : /* Replace file.dwo with file.o and see if that matches. */
11962 : 0 : const char *fname;
11963 : 0 : dwfl_module_info (dwflmod, NULL, NULL, NULL, NULL, NULL,
11964 : : &fname, NULL);
11965 [ # # ]: 0 : if (fname != NULL)
11966 : : {
11967 : 0 : size_t flen = strlen (fname);
11968 [ # # # # ]: 0 : if (flen > 4 && strcmp (".dwo", fname + flen - 4) == 0)
11969 : : {
11970 : 0 : skel_name = strdup (fname);
11971 [ # # ]: 0 : if (skel_name != NULL)
11972 : : {
11973 : 0 : skel_name[flen - 3] = 'o';
11974 : 0 : skel_name[flen - 2] = '\0';
11975 : : }
11976 : : }
11977 : : }
11978 : : }
11979 : :
11980 [ - + ]: 18 : if (skel_name != NULL)
11981 : : {
11982 : 18 : int skel_fd = open (skel_name, O_RDONLY);
11983 [ - + ]: 18 : if (skel_fd == -1)
11984 : 0 : fprintf (stderr, "Warning: Couldn't open DWARF skeleton file"
11985 : : " '%s'\n", skel_name);
11986 : : else
11987 : : {
11988 : 18 : skel_dwfl = create_dwfl (skel_fd, skel_name);
11989 : :
11990 : : /* skel_fd was dup'ed by create_dwfl. We can close the
11991 : : original now. */
11992 : 18 : close (skel_fd);
11993 : : }
11994 : :
11995 [ + - ]: 18 : if (skel_dwfl != NULL)
11996 : : {
11997 [ - + ]: 18 : if (dwfl_getmodules (skel_dwfl, &getone_dwflmod,
11998 : : &skel_mod, 0) != 0)
11999 : : {
12000 : 0 : fprintf (stderr, "Warning: Bad DWARF skeleton,"
12001 : : " multiple modules '%s'\n", skel_name);
12002 : 0 : dwfl_end (skel_dwfl);
12003 : 0 : skel_mod = NULL;
12004 : : }
12005 : : }
12006 [ # # ]: 0 : else if (skel_fd != -1)
12007 : 0 : fprintf (stderr, "Warning: Couldn't create skeleton dwfl for"
12008 : : " '%s': %s\n", skel_name, dwfl_errmsg (-1));
12009 : :
12010 [ + - ]: 18 : if (skel_mod != NULL)
12011 : : {
12012 : 18 : Dwarf *skel_dbg = dwfl_module_getdwarf (skel_mod, &dwbias);
12013 [ + - ]: 18 : if (skel_dbg != NULL)
12014 : : {
12015 : : /* First check the skeleton CU DIE, only fetch
12016 : : the split DIE if we know the id matches to
12017 : : not unnecessary search for any split DIEs we
12018 : : don't need. */
12019 : 18 : Dwarf_CU *cu = NULL;
12020 : 18 : while (dwarf_get_units (skel_dbg, cu, &cu,
12021 [ + - ]: 26 : NULL, NULL, NULL, NULL) == 0)
12022 : : {
12023 : 26 : uint8_t unit_type;
12024 : 26 : uint64_t skel_id;
12025 [ + - ]: 26 : if (dwarf_cu_info (cu, NULL, &unit_type, NULL, NULL,
12026 : : &skel_id, NULL, NULL) == 0
12027 [ + - ]: 26 : && unit_type == DW_UT_skeleton
12028 [ + + ]: 26 : && split_id == skel_id)
12029 : : {
12030 : 18 : Dwarf_Die subdie;
12031 [ + - ]: 18 : if (dwarf_cu_info (cu, NULL, NULL, NULL,
12032 : : &subdie,
12033 : : NULL, NULL, NULL) == 0
12034 [ + - ]: 18 : && dwarf_tag (&subdie) != DW_TAG_invalid)
12035 : : {
12036 : 18 : split_dbg = dwarf_cu_getdwarf (subdie.cu);
12037 [ - + ]: 18 : if (split_dbg == NULL)
12038 : 0 : fprintf (stderr,
12039 : : "Warning: Couldn't get split_dbg:"
12040 : : " %s\n", dwarf_errmsg (-1));
12041 : 0 : break;
12042 : : }
12043 : : else
12044 : : {
12045 : : /* Everything matches up, but not
12046 : : according to libdw. Which means
12047 : : the user knew better. So...
12048 : : Terrible hack... We can never
12049 : : destroy the underlying dwfl
12050 : : because it would free the wrong
12051 : : Dwarfs... So we leak memory...*/
12052 [ # # ]: 0 : if (cu->split == NULL
12053 [ # # ]: 0 : && dwarf_skeleton != NULL)
12054 : : {
12055 : 0 : do_not_close_dwfl = true;
12056 : 0 : __libdw_link_skel_split (cu, split_cu);
12057 : 0 : split_dbg = dwarf_cu_getdwarf (split_cu);
12058 : 0 : break;
12059 : : }
12060 : : else
12061 : 0 : fprintf (stderr, "Warning: Couldn't get"
12062 : : " skeleton subdie: %s\n",
12063 : : dwarf_errmsg (-1));
12064 : : }
12065 : : }
12066 : : }
12067 [ - - ]: 18 : if (split_dbg == NULL)
12068 : 0 : fprintf (stderr, "Warning: '%s' didn't contain a skeleton for split id %" PRIx64 "\n", skel_name, split_id);
12069 : : }
12070 : : else
12071 : 0 : fprintf (stderr, "Warning: Couldn't get skeleton DWARF:"
12072 : : " %s\n", dwfl_errmsg (-1));
12073 : : }
12074 : : }
12075 : :
12076 : 0 : if (split_dbg != NULL)
12077 : : {
12078 : 18 : dbg = split_dbg;
12079 : 18 : dwflmod = skel_mod;
12080 : : }
12081 [ # # ]: 0 : else if (skel_name == NULL)
12082 : 0 : fprintf (stderr,
12083 : : "Warning: split DWARF file, but no skeleton found.\n");
12084 : : }
12085 [ + - ]: 378 : else if (dwarf_skeleton != NULL)
12086 : 396 : fprintf (stderr, "Warning: DWARF skeleton given,"
12087 : : " but not a split DWARF file\n");
12088 : : }
12089 : :
12090 : : /* Get the section header string table index. */
12091 : 502 : size_t shstrndx;
12092 [ - + ]: 502 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
12093 : 0 : error_exit (0, _("cannot get section header string table index"));
12094 : :
12095 : : /* If the .debug_info section is listed as implicitly required then
12096 : : we must make sure to handle it before handling any other debug
12097 : : section. Various other sections depend on the CU DIEs being
12098 : : scanned (silently) first. */
12099 : 502 : bool implicit_info = (implicit_debug_sections & section_info) != 0;
12100 : 502 : bool explicit_info = (print_debug_sections & section_info) != 0;
12101 [ + + ]: 502 : if (implicit_info)
12102 : : {
12103 : : Elf_Scn *scn = NULL;
12104 [ + - ]: 3132 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
12105 : : {
12106 : 3132 : GElf_Shdr shdr_mem;
12107 : 3132 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
12108 : :
12109 [ + - + + ]: 3132 : if (shdr != NULL && shdr->sh_type == SHT_PROGBITS)
12110 : : {
12111 : 2448 : const char *name = elf_strptr (ebl->elf, shstrndx,
12112 : 1224 : shdr->sh_name);
12113 [ - + ]: 1224 : if (name == NULL)
12114 : 0 : continue;
12115 : :
12116 [ + + ]: 1224 : if (strcmp (name, ".debug_info") == 0
12117 [ + + ]: 1092 : || strcmp (name, ".debug_info.dwo") == 0
12118 [ + + ]: 1074 : || strcmp (name, ".zdebug_info") == 0
12119 [ + - ]: 1062 : || strcmp (name, ".zdebug_info.dwo") == 0
12120 [ + + ]: 1062 : || strcmp (name, ".gnu.debuglto_.debug_info") == 0)
12121 : : {
12122 : 164 : print_debug_info_section (dwflmod, ebl, ehdr,
12123 : : scn, shdr, dbg);
12124 : 164 : break;
12125 : : }
12126 : : }
12127 : : }
12128 : 164 : print_debug_sections &= ~section_info;
12129 : 164 : implicit_debug_sections &= ~section_info;
12130 : : }
12131 : :
12132 : : /* Look through all the sections for the debugging sections to print. */
12133 : : Elf_Scn *scn = NULL;
12134 [ + + ]: 16590 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
12135 : : {
12136 : 16088 : GElf_Shdr shdr_mem;
12137 : 16088 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
12138 : :
12139 [ + - + + ]: 16088 : if (shdr != NULL && shdr->sh_type == SHT_PROGBITS)
12140 : : {
12141 : 6758 : static const struct
12142 : : {
12143 : : const char *name;
12144 : : enum section_e bitmask;
12145 : : void (*fp) (Dwfl_Module *, Ebl *,
12146 : : GElf_Ehdr *, Elf_Scn *, GElf_Shdr *, Dwarf *);
12147 : : } debug_sections[] =
12148 : : {
12149 : : #define NEW_SECTION(name) \
12150 : : { ".debug_" #name, section_##name, print_debug_##name##_section }
12151 : : NEW_SECTION (abbrev),
12152 : : NEW_SECTION (addr),
12153 : : NEW_SECTION (aranges),
12154 : : NEW_SECTION (frame),
12155 : : NEW_SECTION (info),
12156 : : NEW_SECTION (types),
12157 : : NEW_SECTION (line),
12158 : : NEW_SECTION (loc),
12159 : : /* loclists is loc for DWARF5. */
12160 : : { ".debug_loclists", section_loc,
12161 : : print_debug_loclists_section },
12162 : : NEW_SECTION (pubnames),
12163 : : NEW_SECTION (str),
12164 : : /* A DWARF5 specialised debug string section. */
12165 : : { ".debug_line_str", section_str,
12166 : : print_debug_str_section },
12167 : : /* DWARF5 string offsets table. */
12168 : : { ".debug_str_offsets", section_str,
12169 : : print_debug_str_offsets_section },
12170 : : NEW_SECTION (macinfo),
12171 : : NEW_SECTION (macro),
12172 : : NEW_SECTION (ranges),
12173 : : /* rnglists is ranges for DWARF5. */
12174 : : { ".debug_rnglists", section_ranges,
12175 : : print_debug_rnglists_section },
12176 : : { ".eh_frame", section_frame | section_exception,
12177 : : print_debug_frame_section },
12178 : : { ".eh_frame_hdr", section_frame | section_exception,
12179 : : print_debug_frame_hdr_section },
12180 : : { ".gcc_except_table", section_frame | section_exception,
12181 : : print_debug_exception_table },
12182 : : { ".gdb_index", section_gdb_index, print_gdb_index_section }
12183 : : };
12184 : 6758 : const int ndebug_sections = (sizeof (debug_sections)
12185 : : / sizeof (debug_sections[0]));
12186 : 13516 : const char *name = elf_strptr (ebl->elf, shstrndx,
12187 : 6758 : shdr->sh_name);
12188 [ - + ]: 6758 : if (name == NULL)
12189 : 0 : continue;
12190 : :
12191 : : int n;
12192 [ + + ]: 106506 : for (n = 0; n < ndebug_sections; ++n)
12193 : : {
12194 : 103042 : size_t dbglen = strlen (debug_sections[n].name);
12195 : 103042 : size_t scnlen = strlen (name);
12196 [ + + ]: 103042 : if ((strncmp (name, debug_sections[n].name, dbglen) == 0
12197 [ + + ]: 3558 : && (dbglen == scnlen
12198 [ + + ]: 546 : || (scnlen == dbglen + 4
12199 [ + + ]: 466 : && strstr (name, ".dwo") == name + dbglen)))
12200 [ + + + + ]: 99922 : || (name[0] == '.' && name[1] == 'z'
12201 [ + - ]: 1132 : && debug_sections[n].name[1] == 'd'
12202 [ + + ]: 1132 : && strncmp (&name[2], &debug_sections[n].name[1],
12203 : : dbglen - 1) == 0
12204 [ - + ]: 164 : && (scnlen == dbglen + 1
12205 [ # # ]: 0 : || (scnlen == dbglen + 5
12206 [ # # ]: 0 : && strstr (name, ".dwo") == name + dbglen + 1)))
12207 [ + + ]: 99758 : || (scnlen > 14 /* .gnu.debuglto_ prefix. */
12208 [ + + ]: 10170 : && startswith (name, ".gnu.debuglto_")
12209 [ + + ]: 72 : && strcmp (&name[14], debug_sections[n].name) == 0)
12210 : : )
12211 : : {
12212 : 3294 : if ((print_debug_sections | implicit_debug_sections)
12213 [ + + ]: 3294 : & debug_sections[n].bitmask)
12214 : 1094 : debug_sections[n].fp (dwflmod, ebl, ehdr, scn, shdr, dbg);
12215 : : break;
12216 : : }
12217 : : }
12218 : : }
12219 : : }
12220 : :
12221 : 502 : dwfl_end (skel_dwfl);
12222 : 502 : free (skel_name);
12223 : :
12224 : : /* Turn implicit and/or explicit back on in case we go over another file. */
12225 [ + + ]: 502 : if (implicit_info)
12226 : 164 : implicit_debug_sections |= section_info;
12227 [ + + ]: 502 : if (explicit_info)
12228 : 134 : print_debug_sections |= section_info;
12229 : :
12230 : 502 : reset_listptr (&known_locsptr);
12231 : 502 : reset_listptr (&known_loclistsptr);
12232 : 502 : reset_listptr (&known_rangelistptr);
12233 : 502 : reset_listptr (&known_rnglistptr);
12234 : 502 : reset_listptr (&known_addrbases);
12235 : 502 : reset_listptr (&known_stroffbases);
12236 : 502 : }
12237 : :
12238 : :
12239 : : #define ITEM_INDENT 4
12240 : : #define WRAP_COLUMN 75
12241 : :
12242 : : /* Print "NAME: FORMAT", wrapping when output text would make the line
12243 : : exceed WRAP_COLUMN. Unpadded numbers look better for the core items
12244 : : but this function is also used for registers which should be printed
12245 : : aligned. Fortunately registers output uses fixed fields width (such
12246 : : as %11d) for the alignment.
12247 : :
12248 : : Line breaks should not depend on the particular values although that
12249 : : may happen in some cases of the core items. */
12250 : :
12251 : : static unsigned int
12252 : : __attribute__ ((format (printf, 6, 7)))
12253 : 1642 : print_core_item (unsigned int colno, char sep, unsigned int wrap,
12254 : : size_t name_width, const char *name, const char *format, ...)
12255 : : {
12256 : 1642 : size_t len = strlen (name);
12257 : 1642 : if (name_width < len)
12258 : : name_width = len;
12259 : :
12260 : 1642 : char *out;
12261 : 1642 : va_list ap;
12262 : 1642 : va_start (ap, format);
12263 : 1642 : int out_len = vasprintf (&out, format, ap);
12264 : 1642 : va_end (ap);
12265 [ - + ]: 1642 : if (out_len == -1)
12266 : 0 : error_exit (0, _("memory exhausted"));
12267 : :
12268 : 1642 : size_t n = name_width + sizeof ": " - 1 + out_len;
12269 : :
12270 [ + + ]: 1642 : if (colno == 0)
12271 : : {
12272 : 102 : printf ("%*s", ITEM_INDENT, "");
12273 : 102 : colno = ITEM_INDENT + n;
12274 : : }
12275 [ + + ]: 1540 : else if (colno + 2 + n < wrap)
12276 : : {
12277 : 936 : printf ("%c ", sep);
12278 : 936 : colno += 2 + n;
12279 : : }
12280 : : else
12281 : : {
12282 : 604 : printf ("\n%*s", ITEM_INDENT, "");
12283 : 604 : colno = ITEM_INDENT + n;
12284 : : }
12285 : :
12286 : 1642 : printf ("%s: %*s%s", name, (int) (name_width - len), "", out);
12287 : :
12288 : 1642 : free (out);
12289 : :
12290 : 1642 : return colno;
12291 : : }
12292 : :
12293 : : static const void *
12294 : 1862 : convert (Elf *core, Elf_Type type, uint_fast16_t count,
12295 : : void *value, const void *data, size_t size)
12296 : : {
12297 : 3724 : Elf_Data valuedata =
12298 : : {
12299 : : .d_type = type,
12300 : : .d_buf = value,
12301 [ + + ]: 1862 : .d_size = size ?: gelf_fsize (core, type, count, EV_CURRENT),
12302 : : .d_version = EV_CURRENT,
12303 : : };
12304 : 1862 : Elf_Data indata =
12305 : : {
12306 : : .d_type = type,
12307 : : .d_buf = (void *) data,
12308 : : .d_size = valuedata.d_size,
12309 : : .d_version = EV_CURRENT,
12310 : : };
12311 : :
12312 : 1862 : Elf_Data *d = (gelf_getclass (core) == ELFCLASS32
12313 [ + + ]: 1862 : ? elf32_xlatetom : elf64_xlatetom)
12314 : 1862 : (&valuedata, &indata, elf_getident (core, NULL)[EI_DATA]);
12315 [ - + ]: 1862 : if (d == NULL)
12316 : 0 : error_exit (0, _("cannot convert core note data: %s"),
12317 : : elf_errmsg (-1));
12318 : :
12319 : 1862 : return data + indata.d_size;
12320 : : }
12321 : :
12322 : : typedef uint8_t GElf_Byte;
12323 : :
12324 : : static unsigned int
12325 : 800 : handle_core_item (Elf *core, const Ebl_Core_Item *item, const void *desc,
12326 : : unsigned int colno, size_t *repeated_size)
12327 : : {
12328 [ + + ]: 800 : uint_fast16_t count = item->count ?: 1;
12329 : : /* Ebl_Core_Item count is always a small number.
12330 : : Make sure the backend didn't put in some large bogus value. */
12331 [ - + ]: 124 : assert (count < 128);
12332 : :
12333 : : #define TYPES \
12334 : : DO_TYPE (BYTE, Byte, "0x%.2" PRIx8, "%" PRId8); \
12335 : : DO_TYPE (HALF, Half, "0x%.4" PRIx16, "%" PRId16); \
12336 : : DO_TYPE (WORD, Word, "0x%.8" PRIx32, "%" PRId32); \
12337 : : DO_TYPE (SWORD, Sword, "%" PRId32, "%" PRId32); \
12338 : : DO_TYPE (XWORD, Xword, "0x%.16" PRIx64, "%" PRId64); \
12339 : : DO_TYPE (SXWORD, Sxword, "%" PRId64, "%" PRId64)
12340 : :
12341 : : #define DO_TYPE(NAME, Name, hex, dec) GElf_##Name Name
12342 : 800 : typedef union { TYPES; } value_t;
12343 : 800 : void *data = alloca (count * sizeof (value_t));
12344 : : #undef DO_TYPE
12345 : :
12346 : : #define DO_TYPE(NAME, Name, hex, dec) \
12347 : : GElf_##Name *value_##Name __attribute__((unused)) = data
12348 : 800 : TYPES;
12349 : : #undef DO_TYPE
12350 : :
12351 : 800 : size_t size = gelf_fsize (core, item->type, count, EV_CURRENT);
12352 : 800 : size_t convsize = size;
12353 [ + + ]: 800 : if (repeated_size != NULL)
12354 : : {
12355 [ + - - + ]: 2 : if (*repeated_size > size && (item->format == 'b' || item->format == 'B'))
12356 : : {
12357 : 0 : data = alloca (*repeated_size);
12358 : 0 : count *= *repeated_size / size;
12359 : 0 : convsize = count * size;
12360 : 0 : *repeated_size -= convsize;
12361 : : }
12362 [ + - + - ]: 2 : else if (item->count != 0 || item->format != '\n')
12363 : 0 : *repeated_size -= size;
12364 : : }
12365 : :
12366 : 800 : convert (core, item->type, count, data, desc + item->offset, convsize);
12367 : :
12368 : 800 : Elf_Type type = item->type;
12369 [ + + ]: 800 : if (type == ELF_T_ADDR)
12370 [ + - ]: 2 : type = gelf_getclass (core) == ELFCLASS32 ? ELF_T_WORD : ELF_T_XWORD;
12371 : :
12372 [ + + + + : 800 : switch (item->format)
+ + + +
- ]
12373 : : {
12374 : 386 : case 'd':
12375 [ - + ]: 386 : assert (count == 1);
12376 [ + + + + : 386 : switch (type)
- + - ]
12377 : : {
12378 : : #define DO_TYPE(NAME, Name, hex, dec) \
12379 : : case ELF_T_##NAME: \
12380 : : colno = print_core_item (colno, ',', WRAP_COLUMN, \
12381 : : 0, item->name, dec, value_##Name[0]); \
12382 : : break
12383 : 386 : TYPES;
12384 : : #undef DO_TYPE
12385 : 0 : default:
12386 : 0 : abort ();
12387 : : }
12388 : : break;
12389 : :
12390 : 222 : case 'x':
12391 [ - + ]: 222 : assert (count == 1);
12392 [ - - + - : 222 : switch (type)
+ - - ]
12393 : : {
12394 : : #define DO_TYPE(NAME, Name, hex, dec) \
12395 : : case ELF_T_##NAME: \
12396 : : colno = print_core_item (colno, ',', WRAP_COLUMN, \
12397 : : 0, item->name, hex, value_##Name[0]); \
12398 : : break
12399 : 222 : TYPES;
12400 : : #undef DO_TYPE
12401 : 0 : default:
12402 : 0 : abort ();
12403 : : }
12404 : : break;
12405 : :
12406 : 44 : case 'b':
12407 : : case 'B':
12408 [ - + ]: 44 : assert (size % sizeof (unsigned int) == 0);
12409 : 44 : unsigned int nbits = count * size * 8;
12410 : 44 : unsigned int pop = 0;
12411 [ + + ]: 112 : for (const unsigned int *i = data; (void *) i < data + count * size; ++i)
12412 : 68 : pop += __builtin_popcount (*i);
12413 : 44 : bool negate = pop > nbits / 2;
12414 : 44 : const unsigned int bias = item->format == 'b';
12415 : :
12416 : 44 : {
12417 [ - + ]: 44 : char printed[(negate ? nbits - pop : pop) * 16 + 1];
12418 : 44 : char *p = printed;
12419 : 44 : *p = '\0';
12420 : :
12421 : 44 : if (BYTE_ORDER != LITTLE_ENDIAN && size > sizeof (unsigned int))
12422 : : {
12423 : : assert (size == sizeof (unsigned int) * 2);
12424 : : for (unsigned int *i = data;
12425 : : (void *) i < data + count * size; i += 2)
12426 : : {
12427 : : unsigned int w = i[1];
12428 : : i[1] = i[0];
12429 : : i[0] = w;
12430 : : }
12431 : : }
12432 : :
12433 : 44 : unsigned int lastbit = 0;
12434 : 44 : unsigned int run = 0;
12435 : 44 : for (const unsigned int *i = data;
12436 [ + + ]: 112 : (void *) i < data + count * size; ++i)
12437 : : {
12438 : 68 : unsigned int bit = ((void *) i - data) * 8;
12439 [ - + ]: 68 : unsigned int w = negate ? ~*i : *i;
12440 [ - + ]: 68 : while (w != 0)
12441 : : {
12442 : : /* Note that a right shift equal to (or greater than)
12443 : : the number of bits of w is undefined behaviour. In
12444 : : particular when the least significant bit is bit 32
12445 : : (w = 0x8000000) then w >>= n is undefined. So
12446 : : explicitly handle that case separately. */
12447 : 0 : unsigned int n = ffs (w);
12448 [ # # ]: 0 : if (n < sizeof (w) * 8)
12449 : 0 : w >>= n;
12450 : : else
12451 : : w = 0;
12452 : 0 : bit += n;
12453 : :
12454 [ # # # # ]: 0 : if (lastbit != 0 && lastbit + 1 == bit)
12455 : 0 : ++run;
12456 : : else
12457 : : {
12458 : 0 : if (lastbit == 0)
12459 : 0 : p += sprintf (p, "%u", bit - bias);
12460 [ # # ]: 0 : else if (run == 0)
12461 : 0 : p += sprintf (p, ",%u", bit - bias);
12462 : : else
12463 : 0 : p += sprintf (p, "-%u,%u", lastbit - bias, bit - bias);
12464 : : run = 0;
12465 : : }
12466 : :
12467 : : lastbit = bit;
12468 : : }
12469 : : }
12470 [ - + - - ]: 44 : if (lastbit > 0 && run > 0 && lastbit + 1 != nbits)
12471 : 0 : p += sprintf (p, "-%u", lastbit - bias);
12472 : :
12473 [ + - ]: 88 : colno = print_core_item (colno, ',', WRAP_COLUMN, 0, item->name,
12474 : : negate ? "~<%s>" : "<%s>", printed);
12475 : : }
12476 : 44 : break;
12477 : :
12478 : 88 : case 'T':
12479 : : case (char) ('T'|0x80):
12480 [ - + ]: 88 : assert (count == 2);
12481 : 88 : Dwarf_Word sec;
12482 : 88 : Dwarf_Word usec;
12483 [ - - + - : 88 : switch (type)
+ - - ]
12484 : : {
12485 : : #define DO_TYPE(NAME, Name, hex, dec) \
12486 : : case ELF_T_##NAME: \
12487 : : sec = value_##Name[0]; \
12488 : : usec = value_##Name[1]; \
12489 : : break
12490 : 88 : TYPES;
12491 : : #undef DO_TYPE
12492 : 0 : default:
12493 : 0 : abort ();
12494 : : }
12495 [ - + ]: 88 : if (unlikely (item->format == (char) ('T'|0x80)))
12496 : : {
12497 : : /* This is a hack for an ill-considered 64-bit ABI where
12498 : : tv_usec is actually a 32-bit field with 32 bits of padding
12499 : : rounding out struct timeval. We've already converted it as
12500 : : a 64-bit field. For little-endian, this just means the
12501 : : high half is the padding; it's presumably zero, but should
12502 : : be ignored anyway. For big-endian, it means the 32-bit
12503 : : field went into the high half of USEC. */
12504 : 0 : GElf_Ehdr ehdr_mem;
12505 : 0 : GElf_Ehdr *ehdr = gelf_getehdr (core, &ehdr_mem);
12506 [ # # ]: 0 : if (likely (ehdr->e_ident[EI_DATA] == ELFDATA2MSB))
12507 : 0 : usec >>= 32;
12508 : : else
12509 : 0 : usec &= UINT32_MAX;
12510 : : }
12511 : 88 : colno = print_core_item (colno, ',', WRAP_COLUMN, 0, item->name,
12512 : : "%" PRIu64 ".%.6" PRIu64, sec, usec);
12513 : 88 : break;
12514 : :
12515 : 18 : case 'c':
12516 [ - + ]: 18 : assert (count == 1);
12517 : 18 : colno = print_core_item (colno, ',', WRAP_COLUMN, 0, item->name,
12518 : 18 : "%c", value_Byte[0]);
12519 : 18 : break;
12520 : :
12521 : 36 : case 's':
12522 : 36 : colno = print_core_item (colno, ',', WRAP_COLUMN, 0, item->name,
12523 : : "%.*s", (int) count, value_Byte);
12524 : 36 : break;
12525 : :
12526 : 2 : case '\n':
12527 : : /* This is a list of strings separated by '\n'. */
12528 [ - + ]: 2 : assert (item->count == 0);
12529 [ - + ]: 2 : assert (repeated_size != NULL);
12530 [ - + ]: 2 : assert (item->name == NULL);
12531 [ - + ]: 2 : if (unlikely (item->offset >= *repeated_size))
12532 : : break;
12533 : :
12534 : 2 : const char *s = desc + item->offset;
12535 : 2 : size = *repeated_size - item->offset;
12536 : 2 : *repeated_size = 0;
12537 [ + - ]: 96 : while (size > 0)
12538 : : {
12539 : 96 : const char *eol = memchr (s, '\n', size);
12540 : 96 : int len = size;
12541 [ + + ]: 96 : if (eol != NULL)
12542 : 94 : len = eol - s;
12543 : 96 : printf ("%*s%.*s\n", ITEM_INDENT, "", len, s);
12544 [ + + ]: 96 : if (eol == NULL)
12545 : : break;
12546 : 94 : size -= eol + 1 - s;
12547 : 94 : s = eol + 1;
12548 : : }
12549 : :
12550 : : colno = WRAP_COLUMN;
12551 : : break;
12552 : :
12553 : : case 'h':
12554 : : break;
12555 : :
12556 : 0 : default:
12557 : 800 : error (0, 0, "XXX not handling format '%c' for %s",
12558 : 0 : item->format, item->name);
12559 : : break;
12560 : : }
12561 : :
12562 : : #undef TYPES
12563 : :
12564 : 800 : return colno;
12565 : : }
12566 : :
12567 : :
12568 : : /* Sort items by group, and by layout offset within each group. */
12569 : : static int
12570 : 1790 : compare_core_items (const void *a, const void *b)
12571 : : {
12572 : 1790 : const Ebl_Core_Item *const *p1 = a;
12573 : 1790 : const Ebl_Core_Item *const *p2 = b;
12574 : 1790 : const Ebl_Core_Item *item1 = *p1;
12575 : 1790 : const Ebl_Core_Item *item2 = *p2;
12576 : :
12577 : 1790 : return ((item1->group == item2->group ? 0
12578 [ + + ]: 1790 : : strcmp (item1->group, item2->group))
12579 [ - + ]: 1790 : ?: (int) item1->offset - (int) item2->offset);
12580 : : }
12581 : :
12582 : : /* Sort item groups by layout offset of the first item in the group. */
12583 : : static int
12584 : 278 : compare_core_item_groups (const void *a, const void *b)
12585 : : {
12586 : 278 : const Ebl_Core_Item *const *const *p1 = a;
12587 : 278 : const Ebl_Core_Item *const *const *p2 = b;
12588 : 278 : const Ebl_Core_Item *const *group1 = *p1;
12589 : 278 : const Ebl_Core_Item *const *group2 = *p2;
12590 : 278 : const Ebl_Core_Item *item1 = *group1;
12591 : 278 : const Ebl_Core_Item *item2 = *group2;
12592 : :
12593 : 278 : return (int) item1->offset - (int) item2->offset;
12594 : : }
12595 : :
12596 : : static unsigned int
12597 : 74 : handle_core_items (Elf *core, const void *desc, size_t descsz,
12598 : : const Ebl_Core_Item *items, size_t nitems)
12599 : 74 : {
12600 [ + + ]: 74 : if (nitems == 0)
12601 : : return 0;
12602 : 68 : unsigned int colno = 0;
12603 : :
12604 : : /* FORMAT '\n' makes sense to be present only as a single item as it
12605 : : processes all the data of a note. FORMATs 'b' and 'B' have a special case
12606 : : if present as a single item but they can be also processed with other
12607 : : items below. */
12608 [ + + + + ]: 68 : if (nitems == 1 && (items[0].format == '\n' || items[0].format == 'b'
12609 [ + - ]: 16 : || items[0].format == 'B'))
12610 : : {
12611 [ - + ]: 2 : assert (items[0].offset == 0);
12612 : 2 : size_t size = descsz;
12613 : 2 : colno = handle_core_item (core, items, desc, colno, &size);
12614 : : /* If SIZE is not zero here there is some remaining data. But we do not
12615 : : know how to process it anyway. */
12616 : 2 : return colno;
12617 : : }
12618 [ + + ]: 848 : for (size_t i = 0; i < nitems; ++i)
12619 [ - + ]: 782 : assert (items[i].format != '\n');
12620 : :
12621 : : /* Sort to collect the groups together. */
12622 : 66 : const Ebl_Core_Item *sorted_items[nitems];
12623 [ + + ]: 848 : for (size_t i = 0; i < nitems; ++i)
12624 : 782 : sorted_items[i] = &items[i];
12625 : 66 : qsort (sorted_items, nitems, sizeof sorted_items[0], &compare_core_items);
12626 : :
12627 : : /* Collect the unique groups and sort them. */
12628 : 66 : const Ebl_Core_Item **groups[nitems];
12629 : 66 : groups[0] = &sorted_items[0];
12630 : 66 : size_t ngroups = 1;
12631 [ + + ]: 782 : for (size_t i = 1; i < nitems; ++i)
12632 [ + + ]: 716 : if (sorted_items[i]->group != sorted_items[i - 1]->group
12633 [ + - ]: 150 : && strcmp (sorted_items[i]->group, sorted_items[i - 1]->group))
12634 : 150 : groups[ngroups++] = &sorted_items[i];
12635 : 66 : qsort (groups, ngroups, sizeof groups[0], &compare_core_item_groups);
12636 : :
12637 : : /* Write out all the groups. */
12638 : 66 : const void *last = desc;
12639 : 70 : do
12640 : : {
12641 [ + + ]: 290 : for (size_t i = 0; i < ngroups; ++i)
12642 : : {
12643 : 220 : for (const Ebl_Core_Item **item = groups[i];
12644 : 1018 : (item < &sorted_items[nitems]
12645 [ + + + + ]: 1018 : && ((*item)->group == groups[i][0]->group
12646 [ - + ]: 150 : || !strcmp ((*item)->group, groups[i][0]->group)));
12647 : 798 : ++item)
12648 : 798 : colno = handle_core_item (core, *item, desc, colno, NULL);
12649 : :
12650 : : /* Force a line break at the end of the group. */
12651 : 220 : colno = WRAP_COLUMN;
12652 : : }
12653 : :
12654 [ + + ]: 70 : if (descsz == 0)
12655 : : break;
12656 : :
12657 : : /* This set of items consumed a certain amount of the note's data.
12658 : : If there is more data there, we have another unit of the same size.
12659 : : Loop to print that out too. */
12660 : 40 : const Ebl_Core_Item *item = &items[nitems - 1];
12661 : 80 : size_t eltsz = item->offset + gelf_fsize (core, item->type,
12662 : 40 : item->count ?: 1, EV_CURRENT);
12663 : :
12664 : 40 : int reps = -1;
12665 : 40 : do
12666 : : {
12667 : 40 : ++reps;
12668 : 40 : desc += eltsz;
12669 : 40 : descsz -= eltsz;
12670 : : }
12671 [ + + - + ]: 40 : while (descsz >= eltsz && !memcmp (desc, last, eltsz));
12672 : :
12673 [ + - ]: 40 : if (reps == 1)
12674 : : {
12675 : : /* For just one repeat, print it unabridged twice. */
12676 : 40 : desc -= eltsz;
12677 : 40 : descsz += eltsz;
12678 : : }
12679 [ + - ]: 40 : else if (reps > 1)
12680 : 0 : printf (_("\n%*s... <repeats %u more times> ..."),
12681 : : ITEM_INDENT, "", reps);
12682 : :
12683 : 40 : last = desc;
12684 : : }
12685 [ + + ]: 40 : while (descsz > 0);
12686 : :
12687 : : return colno;
12688 : : }
12689 : :
12690 : : static unsigned int
12691 : 324 : handle_core_register (Ebl *ebl, Elf *core, int maxregname,
12692 : : const Ebl_Register_Location *regloc, const void *desc,
12693 : : unsigned int colno)
12694 : : {
12695 [ - + ]: 324 : if (regloc->bits % 8 != 0)
12696 : : {
12697 : 0 : error (0, 0, "Warning: Cannot handle register with %" PRIu8 "bits\n",
12698 : : regloc->bits);
12699 : 0 : return colno;
12700 : : }
12701 : :
12702 : 324 : desc += regloc->offset;
12703 : :
12704 [ + + ]: 1172 : for (int reg = regloc->regno; reg < regloc->regno + regloc->count; ++reg)
12705 : : {
12706 : 848 : char name[REGNAMESZ];
12707 : 848 : int bits;
12708 : 848 : int type;
12709 : 848 : register_info (ebl, reg, regloc, name, &bits, &type);
12710 : :
12711 : : #define TYPES \
12712 : : BITS (8, BYTE, "%4" PRId8, "0x%.2" PRIx8); \
12713 : : BITS (16, HALF, "%6" PRId16, "0x%.4" PRIx16); \
12714 : : BITS (32, WORD, "%11" PRId32, " 0x%.8" PRIx32); \
12715 : : BITS (64, XWORD, "%20" PRId64, " 0x%.16" PRIx64)
12716 : :
12717 : : #define BITS(bits, xtype, sfmt, ufmt) \
12718 : : uint##bits##_t b##bits; int##bits##_t b##bits##s
12719 : 848 : union { TYPES; uint64_t b128[2]; } value;
12720 : : #undef BITS
12721 : :
12722 [ + + ]: 848 : switch (type)
12723 : : {
12724 : 672 : case DW_ATE_unsigned:
12725 : : case DW_ATE_signed:
12726 : : case DW_ATE_address:
12727 [ - + + + : 672 : switch (bits)
+ - ]
12728 : : {
12729 : : #define BITS(bits, xtype, sfmt, ufmt) \
12730 : : case bits: \
12731 : : desc = convert (core, ELF_T_##xtype, 1, &value, desc, 0); \
12732 : : if (type == DW_ATE_signed) \
12733 : : colno = print_core_item (colno, ' ', WRAP_COLUMN, \
12734 : : maxregname, name, \
12735 : : sfmt, value.b##bits##s); \
12736 : : else \
12737 : : colno = print_core_item (colno, ' ', WRAP_COLUMN, \
12738 : : maxregname, name, \
12739 : : ufmt, value.b##bits); \
12740 : : break
12741 : :
12742 [ - - - + : 576 : TYPES;
+ + + + ]
12743 : :
12744 : 96 : case 128:
12745 [ - + ]: 96 : assert (type == DW_ATE_unsigned);
12746 : 96 : desc = convert (core, ELF_T_XWORD, 2, &value, desc, 0);
12747 : 96 : int be = elf_getident (core, NULL)[EI_DATA] == ELFDATA2MSB;
12748 : 96 : colno = print_core_item (colno, ' ', WRAP_COLUMN,
12749 : : maxregname, name,
12750 : : "0x%.16" PRIx64 "%.16" PRIx64,
12751 : 96 : value.b128[!be], value.b128[be]);
12752 : 96 : break;
12753 : :
12754 : 0 : default:
12755 : 0 : abort ();
12756 : : #undef BITS
12757 : : }
12758 : : break;
12759 : :
12760 : 176 : default:
12761 : : /* Print each byte in hex, the whole thing in native byte order. */
12762 [ - + ]: 176 : assert (bits % 8 == 0);
12763 : 176 : const uint8_t *bytes = desc;
12764 : 176 : desc += bits / 8;
12765 : 176 : char hex[bits / 4 + 1];
12766 : 176 : hex[bits / 4] = '\0';
12767 : 176 : int incr = 1;
12768 [ + + ]: 176 : if (elf_getident (core, NULL)[EI_DATA] == ELFDATA2LSB)
12769 : : {
12770 : 96 : bytes += bits / 8 - 1;
12771 : 96 : incr = -1;
12772 : : }
12773 : 176 : size_t idx = 0;
12774 [ + + ]: 1744 : for (char *h = hex; bits > 0; bits -= 8, idx += incr)
12775 : : {
12776 : 1568 : *h++ = "0123456789abcdef"[bytes[idx] >> 4];
12777 : 1568 : *h++ = "0123456789abcdef"[bytes[idx] & 0xf];
12778 : : }
12779 : 176 : colno = print_core_item (colno, ' ', WRAP_COLUMN,
12780 : : maxregname, name, "0x%s", hex);
12781 : 176 : break;
12782 : : }
12783 : 848 : desc += regloc->pad;
12784 : :
12785 : : #undef TYPES
12786 : : }
12787 : :
12788 : : return colno;
12789 : : }
12790 : :
12791 : :
12792 : : struct register_info
12793 : : {
12794 : : const Ebl_Register_Location *regloc;
12795 : : const char *set;
12796 : : char name[REGNAMESZ];
12797 : : int regno;
12798 : : int bits;
12799 : : int type;
12800 : : };
12801 : :
12802 : : static int
12803 : 4132 : register_bitpos (const struct register_info *r)
12804 : : {
12805 : 4132 : return (r->regloc->offset * 8
12806 : 4132 : + ((r->regno - r->regloc->regno)
12807 : 4132 : * (r->regloc->bits + r->regloc->pad * 8)));
12808 : : }
12809 : :
12810 : : static int
12811 : 2124 : compare_sets_by_info (const struct register_info *r1,
12812 : : const struct register_info *r2)
12813 : : {
12814 : 2124 : return ((int) r2->bits - (int) r1->bits
12815 [ + + ]: 2124 : ?: register_bitpos (r1) - register_bitpos (r2));
12816 : : }
12817 : :
12818 : : /* Sort registers by set, and by size and layout offset within each set. */
12819 : : static int
12820 : 9190 : compare_registers (const void *a, const void *b)
12821 : : {
12822 : 9190 : const struct register_info *r1 = a;
12823 : 9190 : const struct register_info *r2 = b;
12824 : :
12825 : : /* Unused elements sort last. */
12826 [ + + ]: 9190 : if (r1->regloc == NULL)
12827 : 6618 : return r2->regloc == NULL ? 0 : 1;
12828 [ + + ]: 2572 : if (r2->regloc == NULL)
12829 : : return -1;
12830 : :
12831 [ + + ]: 2242 : return ((r1->set == r2->set ? 0 : strcmp (r1->set, r2->set))
12832 [ - + ]: 2242 : ?: compare_sets_by_info (r1, r2));
12833 : : }
12834 : :
12835 : : /* Sort register sets by layout offset of the first register in the set. */
12836 : : static int
12837 : 40 : compare_register_sets (const void *a, const void *b)
12838 : : {
12839 : 40 : const struct register_info *const *p1 = a;
12840 : 40 : const struct register_info *const *p2 = b;
12841 : 40 : return compare_sets_by_info (*p1, *p2);
12842 : : }
12843 : :
12844 : : static inline bool
12845 : 1728 : same_set (const struct register_info *a,
12846 : : const struct register_info *b,
12847 : : const struct register_info *regs,
12848 : : size_t maxnreg)
12849 : : {
12850 [ + - ]: 1728 : return (a < ®s[maxnreg] && a->regloc != NULL
12851 [ + - + + ]: 1728 : && b < ®s[maxnreg] && b->regloc != NULL
12852 [ + + ]: 1692 : && a->bits == b->bits
12853 [ + - + + : 3384 : && (a->set == b->set || !strcmp (a->set, b->set)));
- + ]
12854 : : }
12855 : :
12856 : : static unsigned int
12857 : 74 : handle_core_registers (Ebl *ebl, Elf *core, const void *desc,
12858 : : const Ebl_Register_Location *reglocs, size_t nregloc)
12859 : 74 : {
12860 [ + + ]: 74 : if (nregloc == 0)
12861 : : return 0;
12862 : :
12863 : 36 : ssize_t maxnreg = ebl_register_info (ebl, 0, NULL, 0, NULL, NULL, NULL, NULL);
12864 [ - + ]: 36 : if (maxnreg <= 0)
12865 : : {
12866 [ # # ]: 0 : for (size_t i = 0; i < nregloc; ++i)
12867 : 0 : if (maxnreg < reglocs[i].regno + reglocs[i].count)
12868 : : maxnreg = reglocs[i].regno + reglocs[i].count;
12869 [ # # ]: 0 : assert (maxnreg > 0);
12870 : : }
12871 : :
12872 : 36 : struct register_info regs[maxnreg];
12873 : 36 : memset (regs, 0, sizeof regs);
12874 : :
12875 : : /* Sort to collect the sets together. */
12876 : 36 : int maxreg = 0;
12877 [ + + ]: 360 : for (size_t i = 0; i < nregloc; ++i)
12878 : 324 : for (int reg = reglocs[i].regno;
12879 [ + + ]: 1172 : reg < reglocs[i].regno + reglocs[i].count;
12880 : 848 : ++reg)
12881 : : {
12882 [ - + ]: 848 : assert (reg < maxnreg);
12883 : 848 : if (reg > maxreg)
12884 : : maxreg = reg;
12885 : 848 : struct register_info *info = ®s[reg];
12886 : 848 : info->regloc = ®locs[i];
12887 : 848 : info->regno = reg;
12888 : 848 : info->set = register_info (ebl, reg, ®locs[i],
12889 : 848 : info->name, &info->bits, &info->type);
12890 : : }
12891 : 36 : qsort (regs, maxreg + 1, sizeof regs[0], &compare_registers);
12892 : :
12893 : : /* Collect the unique sets and sort them. */
12894 : 36 : struct register_info *sets[maxreg + 1];
12895 : 36 : sets[0] = ®s[0];
12896 : 36 : size_t nsets = 1;
12897 [ + + ]: 2558 : for (int i = 1; i <= maxreg; ++i)
12898 [ + + ]: 2522 : if (regs[i].regloc != NULL
12899 [ + + ]: 812 : && !same_set (®s[i], ®s[i - 1], regs, maxnreg))
12900 : 32 : sets[nsets++] = ®s[i];
12901 : 36 : qsort (sets, nsets, sizeof sets[0], &compare_register_sets);
12902 : :
12903 : : /* Write out all the sets. */
12904 : 36 : unsigned int colno = 0;
12905 [ + + ]: 104 : for (size_t i = 0; i < nsets; ++i)
12906 : : {
12907 : : /* Find the longest name of a register in this set. */
12908 : 68 : size_t maxname = 0;
12909 : 68 : const struct register_info *end;
12910 [ + + ]: 916 : for (end = sets[i]; same_set (sets[i], end, regs, maxnreg); ++end)
12911 : : {
12912 : 848 : size_t len = strlen (end->name);
12913 : 848 : if (len > maxname)
12914 : : maxname = len;
12915 : : }
12916 : :
12917 : : for (const struct register_info *reg = sets[i];
12918 [ + + ]: 392 : reg < end;
12919 : 324 : reg += reg->regloc->count ?: 1)
12920 [ + - ]: 324 : colno = handle_core_register (ebl, core, maxname,
12921 : 324 : reg->regloc, desc, colno);
12922 : :
12923 : : /* Force a line break at the end of the group. */
12924 : 68 : colno = WRAP_COLUMN;
12925 : : }
12926 : :
12927 : : return colno;
12928 : : }
12929 : :
12930 : : static void
12931 : 18 : handle_auxv_note (Ebl *ebl, Elf *core, GElf_Word descsz, GElf_Off desc_pos)
12932 : : {
12933 : 18 : Elf_Data *data = elf_getdata_rawchunk (core, desc_pos, descsz, ELF_T_AUXV);
12934 [ - + ]: 18 : if (data == NULL)
12935 : 0 : elf_error:
12936 : 0 : error_exit (0, _("cannot convert core note data: %s"), elf_errmsg (-1));
12937 : :
12938 : 18 : const size_t nauxv = descsz / gelf_fsize (core, ELF_T_AUXV, 1, EV_CURRENT);
12939 [ + + ]: 354 : for (size_t i = 0; i < nauxv; ++i)
12940 : : {
12941 : 336 : GElf_auxv_t av_mem;
12942 : 336 : GElf_auxv_t *av = gelf_getauxv (data, i, &av_mem);
12943 [ - + ]: 336 : if (av == NULL)
12944 : 0 : goto elf_error;
12945 : :
12946 : 336 : const char *name;
12947 : 336 : const char *fmt;
12948 [ - + ]: 336 : if (ebl_auxv_info (ebl, av->a_type, &name, &fmt) == 0)
12949 : : {
12950 : : /* Unknown type. */
12951 [ # # ]: 0 : if (av->a_un.a_val == 0)
12952 : 0 : printf (" %" PRIu64 "\n", av->a_type);
12953 : : else
12954 : 0 : printf (" %" PRIu64 ": %#" PRIx64 "\n",
12955 : : av->a_type, av->a_un.a_val);
12956 : : }
12957 : : else
12958 [ + + + - : 336 : switch (fmt[0])
+ - ]
12959 : : {
12960 : 18 : case '\0': /* Normally zero. */
12961 [ + - ]: 18 : if (av->a_un.a_val == 0)
12962 : : {
12963 : 18 : printf (" %s\n", name);
12964 : : break;
12965 : : }
12966 : 148 : FALLTHROUGH;
12967 : : case 'x': /* hex */
12968 : : case 'p': /* address */
12969 : : case 's': /* address of string */
12970 : 148 : printf (" %s: %#" PRIx64 "\n", name, av->a_un.a_val);
12971 : : break;
12972 : 162 : case 'u':
12973 : 162 : printf (" %s: %" PRIu64 "\n", name, av->a_un.a_val);
12974 : : break;
12975 : 0 : case 'd':
12976 : 0 : printf (" %s: %" PRId64 "\n", name, av->a_un.a_val);
12977 : : break;
12978 : :
12979 : 8 : case 'b':
12980 : 8 : printf (" %s: %#" PRIx64 " ", name, av->a_un.a_val);
12981 : 8 : GElf_Xword bit = 1;
12982 : 8 : const char *pfx = "<";
12983 [ + + ]: 220 : for (const char *p = fmt + 1; *p != 0; p = strchr (p, '\0') + 1)
12984 : : {
12985 [ + + ]: 212 : if (av->a_un.a_val & bit)
12986 : : {
12987 : 154 : printf ("%s%s", pfx, p);
12988 : 154 : pfx = " ";
12989 : : }
12990 : 212 : bit <<= 1;
12991 : : }
12992 : 336 : printf (">\n");
12993 : : break;
12994 : :
12995 : 0 : default:
12996 : 0 : abort ();
12997 : : }
12998 : : }
12999 : 18 : }
13000 : :
13001 : : static bool
13002 : 390 : buf_has_data (unsigned char const *ptr, unsigned char const *end, size_t sz)
13003 : : {
13004 [ - + - + ]: 390 : return ptr < end && (size_t) (end - ptr) >= sz;
13005 : : }
13006 : :
13007 : : static bool
13008 : 36 : buf_read_int (Elf *core, unsigned char const **ptrp, unsigned char const *end,
13009 : : int *retp)
13010 : : {
13011 [ + - + - ]: 72 : if (! buf_has_data (*ptrp, end, 4))
13012 : : return false;
13013 : :
13014 : 36 : *ptrp = convert (core, ELF_T_WORD, 1, retp, *ptrp, 4);
13015 : 36 : return true;
13016 : : }
13017 : :
13018 : : static bool
13019 : 354 : buf_read_ulong (Elf *core, unsigned char const **ptrp, unsigned char const *end,
13020 : : uint64_t *retp)
13021 : : {
13022 : 354 : size_t sz = gelf_fsize (core, ELF_T_ADDR, 1, EV_CURRENT);
13023 [ + - + - ]: 708 : if (! buf_has_data (*ptrp, end, sz))
13024 : : return false;
13025 : :
13026 : 354 : union
13027 : : {
13028 : : uint64_t u64;
13029 : : uint32_t u32;
13030 : : } u;
13031 : :
13032 : 354 : *ptrp = convert (core, ELF_T_ADDR, 1, &u, *ptrp, sz);
13033 : :
13034 [ + + ]: 354 : if (sz == 4)
13035 : 186 : *retp = u.u32;
13036 : : else
13037 : 168 : *retp = u.u64;
13038 : : return true;
13039 : : }
13040 : :
13041 : : static void
13042 : 12 : handle_siginfo_note (Elf *core, GElf_Word descsz, GElf_Off desc_pos)
13043 : : {
13044 : 12 : Elf_Data *data = elf_getdata_rawchunk (core, desc_pos, descsz, ELF_T_BYTE);
13045 [ - + ]: 12 : if (data == NULL)
13046 : 0 : error_exit (0, _("cannot convert core note data: %s"), elf_errmsg (-1));
13047 : :
13048 : 12 : unsigned char const *ptr = data->d_buf;
13049 : 12 : unsigned char const *const end = data->d_buf + data->d_size;
13050 : :
13051 : : /* Siginfo head is three ints: signal number, error number, origin
13052 : : code. */
13053 : 12 : int si_signo, si_errno, si_code;
13054 [ - + ]: 12 : if (! buf_read_int (core, &ptr, end, &si_signo)
13055 [ - + ]: 12 : || ! buf_read_int (core, &ptr, end, &si_errno)
13056 [ - + ]: 12 : || ! buf_read_int (core, &ptr, end, &si_code))
13057 : : {
13058 : 0 : fail:
13059 : 0 : printf (" Not enough data in NT_SIGINFO note.\n");
13060 : 0 : return;
13061 : : }
13062 : :
13063 : : /* Next is a pointer-aligned union of structures. On 64-bit
13064 : : machines, that implies a word of padding. */
13065 [ + + ]: 12 : if (gelf_getclass (core) == ELFCLASS64)
13066 : 6 : ptr += 4;
13067 : :
13068 : 12 : printf (" si_signo: %d, si_errno: %d, si_code: %d\n",
13069 : : si_signo, si_errno, si_code);
13070 : :
13071 [ + - ]: 12 : if (si_code > 0)
13072 [ + - ]: 12 : switch (si_signo)
13073 : : {
13074 : 12 : case CORE_SIGILL:
13075 : : case CORE_SIGFPE:
13076 : : case CORE_SIGSEGV:
13077 : : case CORE_SIGBUS:
13078 : : {
13079 : 12 : uint64_t addr;
13080 [ - + ]: 12 : if (! buf_read_ulong (core, &ptr, end, &addr))
13081 : 0 : goto fail;
13082 : 12 : printf (" fault address: %#" PRIx64 "\n", addr);
13083 : 12 : break;
13084 : : }
13085 : : default:
13086 : : ;
13087 : : }
13088 [ # # ]: 0 : else if (si_code == CORE_SI_USER)
13089 : : {
13090 : 0 : int pid, uid;
13091 [ # # ]: 0 : if (! buf_read_int (core, &ptr, end, &pid)
13092 [ # # ]: 0 : || ! buf_read_int (core, &ptr, end, &uid))
13093 : 0 : goto fail;
13094 : 0 : printf (" sender PID: %d, sender UID: %d\n", pid, uid);
13095 : : }
13096 : : }
13097 : :
13098 : : static void
13099 : 12 : handle_file_note (Elf *core, GElf_Word descsz, GElf_Off desc_pos)
13100 : : {
13101 : 12 : Elf_Data *data = elf_getdata_rawchunk (core, desc_pos, descsz, ELF_T_BYTE);
13102 [ - + ]: 12 : if (data == NULL)
13103 : 0 : error_exit (0, _("cannot convert core note data: %s"), elf_errmsg (-1));
13104 : :
13105 : 12 : unsigned char const *ptr = data->d_buf;
13106 : 12 : unsigned char const *const end = data->d_buf + data->d_size;
13107 : :
13108 : 12 : uint64_t count, page_size;
13109 [ - + ]: 12 : if (! buf_read_ulong (core, &ptr, end, &count)
13110 [ - + ]: 12 : || ! buf_read_ulong (core, &ptr, end, &page_size))
13111 : : {
13112 : 0 : fail:
13113 : 0 : printf (" Not enough data in NT_FILE note.\n");
13114 : 0 : return;
13115 : : }
13116 : :
13117 : 12 : size_t addrsize = gelf_fsize (core, ELF_T_ADDR, 1, EV_CURRENT);
13118 : 12 : uint64_t maxcount = (size_t) (end - ptr) / (3 * addrsize);
13119 [ - + ]: 12 : if (count > maxcount)
13120 : 0 : goto fail;
13121 : :
13122 : : /* Where file names are stored. */
13123 : 12 : unsigned char const *const fstart = ptr + 3 * count * addrsize;
13124 : 12 : char const *fptr = (char *) fstart;
13125 : :
13126 : 12 : printf (" %" PRId64 " files:\n", count);
13127 [ + + ]: 118 : for (uint64_t i = 0; i < count; ++i)
13128 : : {
13129 : 106 : uint64_t mstart, mend, moffset;
13130 [ + - ]: 106 : if (! buf_read_ulong (core, &ptr, fstart, &mstart)
13131 [ + - ]: 106 : || ! buf_read_ulong (core, &ptr, fstart, &mend)
13132 [ - + ]: 106 : || ! buf_read_ulong (core, &ptr, fstart, &moffset))
13133 : 0 : goto fail;
13134 : :
13135 : 106 : const char *fnext = memchr (fptr, '\0', (char *) end - fptr);
13136 [ - + ]: 106 : if (fnext == NULL)
13137 : 0 : goto fail;
13138 : :
13139 : 106 : int ct = printf (" %08" PRIx64 "-%08" PRIx64
13140 : : " %08" PRIx64 " %" PRId64,
13141 : : mstart, mend, moffset * page_size, mend - mstart);
13142 [ + - ]: 106 : printf ("%*s%s\n", ct > 50 ? 3 : 53 - ct, "", fptr);
13143 : :
13144 : 106 : fptr = fnext + 1;
13145 : : }
13146 : : }
13147 : :
13148 : : static void
13149 : 76 : handle_core_note (Ebl *ebl, const GElf_Nhdr *nhdr,
13150 : : const char *name, const void *desc)
13151 : : {
13152 : 76 : GElf_Word regs_offset;
13153 : 76 : size_t nregloc;
13154 : 76 : const Ebl_Register_Location *reglocs;
13155 : 76 : size_t nitems;
13156 : 76 : const Ebl_Core_Item *items;
13157 : :
13158 [ + + ]: 76 : if (! ebl_core_note (ebl, nhdr, name, desc,
13159 : : ®s_offset, &nregloc, ®locs, &nitems, &items))
13160 : 2 : return;
13161 : :
13162 : : /* Pass 0 for DESCSZ when there are registers in the note,
13163 : : so that the ITEMS array does not describe the whole thing.
13164 : : For non-register notes, the actual descsz might be a multiple
13165 : : of the unit size, not just exactly the unit size. */
13166 : 74 : unsigned int colno = handle_core_items (ebl->elf, desc,
13167 [ + + ]: 74 : nregloc == 0 ? nhdr->n_descsz : 0,
13168 : : items, nitems);
13169 [ + + ]: 74 : if (colno != 0)
13170 : 68 : putchar ('\n');
13171 : :
13172 : 74 : colno = handle_core_registers (ebl, ebl->elf, desc + regs_offset,
13173 : : reglocs, nregloc);
13174 [ + + ]: 74 : if (colno != 0)
13175 : 74 : putchar ('\n');
13176 : : }
13177 : :
13178 : : static void
13179 : 506 : handle_notes_data (Ebl *ebl, const GElf_Ehdr *ehdr,
13180 : : GElf_Off start, Elf_Data *data)
13181 : : {
13182 : 506 : fputs (_(" Owner Data size Type\n"), stdout);
13183 : :
13184 [ - + ]: 506 : if (data == NULL)
13185 : 0 : goto bad_note;
13186 : :
13187 : : size_t offset = 0;
13188 : : GElf_Nhdr nhdr;
13189 : : size_t name_offset;
13190 : : size_t desc_offset;
13191 : 5120 : while (offset < data->d_size
13192 [ + + + - ]: 5120 : && (offset = gelf_getnote (data, offset,
13193 : : &nhdr, &name_offset, &desc_offset)) > 0)
13194 : : {
13195 [ + - ]: 4614 : const char *name = nhdr.n_namesz == 0 ? "" : data->d_buf + name_offset;
13196 : 4614 : const char *desc = data->d_buf + desc_offset;
13197 : :
13198 : : /* GNU Build Attributes are weird, they store most of their data
13199 : : into the owner name field. Extract just the owner name
13200 : : prefix here, then use the rest later as data. */
13201 : 4614 : bool is_gnu_build_attr
13202 : 4614 : = startswith (name, ELF_NOTE_GNU_BUILD_ATTRIBUTE_PREFIX);
13203 : 9798 : const char *print_name = (is_gnu_build_attr
13204 [ + + ]: 4614 : ? ELF_NOTE_GNU_BUILD_ATTRIBUTE_PREFIX : name);
13205 : 5184 : size_t print_namesz = (is_gnu_build_attr
13206 : 570 : ? strlen (print_name) : nhdr.n_namesz);
13207 : :
13208 : 4614 : char buf[100];
13209 : 4614 : char buf2[100];
13210 : 4614 : printf (_(" %-13.*s %9" PRId32 " %s\n"),
13211 : : (int) print_namesz, print_name, nhdr.n_descsz,
13212 [ + + ]: 4614 : ehdr->e_type == ET_CORE
13213 : 118 : ? ebl_core_note_type_name (ebl, nhdr.n_type,
13214 : : buf, sizeof (buf))
13215 : 4496 : : ebl_object_note_type_name (ebl, name, nhdr.n_type,
13216 : : nhdr.n_descsz,
13217 : : buf2, sizeof (buf2)));
13218 : :
13219 : : /* Filter out invalid entries. */
13220 : 4614 : if (memchr (name, '\0', nhdr.n_namesz) != NULL
13221 : : /* XXX For now help broken Linux kernels. */
13222 : : || 1)
13223 : : {
13224 [ + + ]: 4614 : if (ehdr->e_type == ET_CORE)
13225 : : {
13226 [ + + ]: 118 : if (nhdr.n_type == NT_AUXV
13227 [ + - ]: 18 : && (nhdr.n_namesz == 4 /* Broken old Linux kernels. */
13228 [ + - + - ]: 18 : || (nhdr.n_namesz == 5 && name[4] == '\0'))
13229 [ + - ]: 18 : && !memcmp (name, "CORE", 4))
13230 : 18 : handle_auxv_note (ebl, ebl->elf, nhdr.n_descsz,
13231 : : start + desc_offset);
13232 [ + + + - ]: 100 : else if (nhdr.n_namesz == 5 && strcmp (name, "CORE") == 0)
13233 [ + + + ]: 76 : switch (nhdr.n_type)
13234 : : {
13235 : 12 : case NT_SIGINFO:
13236 : 12 : handle_siginfo_note (ebl->elf, nhdr.n_descsz,
13237 : : start + desc_offset);
13238 : 12 : break;
13239 : :
13240 : 12 : case NT_FILE:
13241 : 12 : handle_file_note (ebl->elf, nhdr.n_descsz,
13242 : : start + desc_offset);
13243 : 12 : break;
13244 : :
13245 : 52 : default:
13246 : 52 : handle_core_note (ebl, &nhdr, name, desc);
13247 : : }
13248 : : else
13249 : 24 : handle_core_note (ebl, &nhdr, name, desc);
13250 : : }
13251 : : else
13252 : 4496 : ebl_object_note (ebl, nhdr.n_namesz, name, nhdr.n_type,
13253 : : nhdr.n_descsz, desc);
13254 : : }
13255 : : }
13256 : :
13257 [ + - ]: 506 : if (offset == data->d_size)
13258 : 506 : return;
13259 : :
13260 : 0 : bad_note:
13261 : 0 : error (0, 0,
13262 : 0 : _("cannot get content of note: %s"),
13263 : 0 : data != NULL ? "garbage data" : elf_errmsg (-1));
13264 : : }
13265 : :
13266 : : static void
13267 : 230 : handle_notes (Ebl *ebl, GElf_Ehdr *ehdr)
13268 : : {
13269 : : /* If we have section headers, just look for SHT_NOTE sections.
13270 : : In a debuginfo file, the program headers are not reliable. */
13271 [ + + ]: 230 : if (shnum != 0)
13272 : : {
13273 : : /* Get the section header string table index. */
13274 : 208 : size_t shstrndx;
13275 [ - + ]: 208 : if (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0)
13276 : 0 : error_exit (0, _("cannot get section header string table index"));
13277 : :
13278 : : Elf_Scn *scn = NULL;
13279 [ + + ]: 6446 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
13280 : : {
13281 : 6238 : GElf_Shdr shdr_mem;
13282 : 6238 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
13283 : :
13284 [ + - + + ]: 6238 : if (shdr == NULL || shdr->sh_type != SHT_NOTE)
13285 : : /* Not what we are looking for. */
13286 : 5756 : continue;
13287 : :
13288 [ - + ]: 482 : if (notes_section != NULL)
13289 : : {
13290 : 0 : char *sname = elf_strptr (ebl->elf, shstrndx, shdr->sh_name);
13291 [ # # # # ]: 0 : if (sname == NULL || strcmp (sname, notes_section) != 0)
13292 : 0 : continue;
13293 : : }
13294 : :
13295 : 482 : printf (_("\
13296 : : \nNote section [%2zu] '%s' of %" PRIu64 " bytes at offset %#0" PRIx64 ":\n"),
13297 : : elf_ndxscn (scn),
13298 : 482 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
13299 : : shdr->sh_size, shdr->sh_offset);
13300 : :
13301 : 482 : handle_notes_data (ebl, ehdr, shdr->sh_offset,
13302 : : elf_getdata (scn, NULL));
13303 : : }
13304 : 208 : return;
13305 : : }
13306 : :
13307 : : /* We have to look through the program header to find the note
13308 : : sections. There can be more than one. */
13309 [ + + ]: 280 : for (size_t cnt = 0; cnt < phnum; ++cnt)
13310 : : {
13311 : 258 : GElf_Phdr mem;
13312 : 258 : GElf_Phdr *phdr = gelf_getphdr (ebl->elf, cnt, &mem);
13313 : :
13314 [ + - + + ]: 258 : if (phdr == NULL || phdr->p_type != PT_NOTE)
13315 : : /* Not what we are looking for. */
13316 : 234 : continue;
13317 : :
13318 : 24 : printf (_("\
13319 : : \nNote segment of %" PRIu64 " bytes at offset %#0" PRIx64 ":\n"),
13320 : : phdr->p_filesz, phdr->p_offset);
13321 : :
13322 : 24 : handle_notes_data (ebl, ehdr, phdr->p_offset,
13323 : : elf_getdata_rawchunk (ebl->elf,
13324 : 24 : phdr->p_offset, phdr->p_filesz,
13325 [ + + ]: 24 : (phdr->p_align == 8
13326 : : ? ELF_T_NHDR8 : ELF_T_NHDR)));
13327 : : }
13328 : : }
13329 : :
13330 : :
13331 : : static void
13332 : 12 : hex_dump (const uint8_t *data, size_t len)
13333 : : {
13334 : 12 : size_t pos = 0;
13335 [ + + ]: 56 : while (pos < len)
13336 : : {
13337 : 44 : printf (" 0x%08zx ", pos);
13338 : :
13339 : 44 : const size_t chunk = MIN (len - pos, 16);
13340 : :
13341 [ + + ]: 688 : for (size_t i = 0; i < chunk; ++i)
13342 [ + + ]: 644 : if (i % 4 == 3)
13343 : 160 : printf ("%02x ", data[pos + i]);
13344 : : else
13345 : 644 : printf ("%02x", data[pos + i]);
13346 : :
13347 [ + + ]: 44 : if (chunk < 16)
13348 : 4 : printf ("%*s", (int) ((16 - chunk) * 2 + (16 - chunk + 3) / 4), "");
13349 : :
13350 [ + + ]: 688 : for (size_t i = 0; i < chunk; ++i)
13351 : : {
13352 : 644 : unsigned char b = data[pos + i];
13353 [ + + ]: 1288 : printf ("%c", isprint (b) ? b : '.');
13354 : : }
13355 : :
13356 : 44 : putchar ('\n');
13357 : 44 : pos += chunk;
13358 : : }
13359 : 12 : }
13360 : :
13361 : : static void
13362 : 12 : dump_data_section (Elf_Scn *scn, const GElf_Shdr *shdr, const char *name)
13363 : : {
13364 [ + - - + ]: 12 : if (shdr->sh_size == 0 || shdr->sh_type == SHT_NOBITS)
13365 : 0 : printf (_("\nSection [%zu] '%s' has no data to dump.\n"),
13366 : : elf_ndxscn (scn), name);
13367 : : else
13368 : : {
13369 [ + + ]: 12 : if (print_decompress)
13370 : : {
13371 : : /* We try to decompress the section, but keep the old shdr around
13372 : : so we can show both the original shdr size and the uncompressed
13373 : : data size. */
13374 [ + + ]: 8 : if ((shdr->sh_flags & SHF_COMPRESSED) != 0)
13375 : : {
13376 [ - + ]: 4 : if (elf_compress (scn, 0, 0) < 0)
13377 : 0 : printf ("WARNING: %s [%zd]\n",
13378 : : _("Couldn't uncompress section"),
13379 : : elf_ndxscn (scn));
13380 : : }
13381 [ + - + - ]: 4 : else if (name && startswith (name, ".zdebug"))
13382 : : {
13383 [ - + ]: 4 : if (elf_compress_gnu (scn, 0, 0) < 0)
13384 : 0 : printf ("WARNING: %s [%zd]\n",
13385 : : _("Couldn't uncompress section"),
13386 : : elf_ndxscn (scn));
13387 : : }
13388 : : }
13389 : :
13390 : 12 : Elf_Data *data = elf_rawdata (scn, NULL);
13391 [ - + ]: 12 : if (data == NULL)
13392 : 0 : error (0, 0, _("cannot get data for section [%zu] '%s': %s"),
13393 : : elf_ndxscn (scn), name, elf_errmsg (-1));
13394 : : else
13395 : : {
13396 [ + + ]: 12 : if (data->d_size == shdr->sh_size)
13397 : 4 : printf (_("\nHex dump of section [%zu] '%s', %" PRIu64
13398 : : " bytes at offset %#0" PRIx64 ":\n"),
13399 : : elf_ndxscn (scn), name,
13400 : 4 : shdr->sh_size, shdr->sh_offset);
13401 : : else
13402 : 8 : printf (_("\nHex dump of section [%zu] '%s', %" PRIu64
13403 : : " bytes (%zd uncompressed) at offset %#0"
13404 : : PRIx64 ":\n"),
13405 : : elf_ndxscn (scn), name,
13406 : 8 : shdr->sh_size, data->d_size, shdr->sh_offset);
13407 : 12 : hex_dump (data->d_buf, data->d_size);
13408 : : }
13409 : : }
13410 : 12 : }
13411 : :
13412 : : static void
13413 : 374 : print_string_section (Elf_Scn *scn, const GElf_Shdr *shdr, const char *name)
13414 : : {
13415 [ + - + + ]: 374 : if (shdr->sh_size == 0 || shdr->sh_type == SHT_NOBITS)
13416 : 42 : printf (_("\nSection [%zu] '%s' has no strings to dump.\n"),
13417 : : elf_ndxscn (scn), name);
13418 : : else
13419 : : {
13420 [ + + ]: 332 : if (print_decompress)
13421 : : {
13422 : : /* We try to decompress the section, but keep the old shdr around
13423 : : so we can show both the original shdr size and the uncompressed
13424 : : data size. */
13425 [ - + ]: 2 : if ((shdr->sh_flags & SHF_COMPRESSED) != 0)
13426 : : {
13427 [ # # ]: 0 : if (elf_compress (scn, 0, 0) < 0)
13428 : 0 : printf ("WARNING: %s [%zd]\n",
13429 : : _("Couldn't uncompress section"),
13430 : : elf_ndxscn (scn));
13431 : : }
13432 [ + - + - ]: 2 : else if (name && startswith (name, ".zdebug"))
13433 : : {
13434 [ - + ]: 2 : if (elf_compress_gnu (scn, 0, 0) < 0)
13435 : 0 : printf ("WARNING: %s [%zd]\n",
13436 : : _("Couldn't uncompress section"),
13437 : : elf_ndxscn (scn));
13438 : : }
13439 : : }
13440 : :
13441 : 332 : Elf_Data *data = elf_rawdata (scn, NULL);
13442 [ - + ]: 332 : if (data == NULL)
13443 : 0 : error (0, 0, _("cannot get data for section [%zu] '%s': %s"),
13444 : : elf_ndxscn (scn), name, elf_errmsg (-1));
13445 : : else
13446 : : {
13447 [ + + ]: 332 : if (data->d_size == shdr->sh_size)
13448 : 330 : printf (_("\nString section [%zu] '%s' contains %" PRIu64
13449 : : " bytes at offset %#0" PRIx64 ":\n"),
13450 : : elf_ndxscn (scn), name,
13451 : 330 : shdr->sh_size, shdr->sh_offset);
13452 : : else
13453 : 2 : printf (_("\nString section [%zu] '%s' contains %" PRIu64
13454 : : " bytes (%zd uncompressed) at offset %#0"
13455 : : PRIx64 ":\n"),
13456 : : elf_ndxscn (scn), name,
13457 : 2 : shdr->sh_size, data->d_size, shdr->sh_offset);
13458 : :
13459 : 332 : const char *start = data->d_buf;
13460 : 332 : const char *const limit = start + data->d_size;
13461 : 44686 : do
13462 : : {
13463 : 44686 : const char *end = memchr (start, '\0', limit - start);
13464 : 44686 : const size_t pos = start - (const char *) data->d_buf;
13465 [ - + ]: 44686 : if (unlikely (end == NULL))
13466 : : {
13467 : 0 : printf (" [%6zx]- %.*s\n",
13468 : : pos, (int) (limit - start), start);
13469 : : break;
13470 : : }
13471 : 44686 : printf (" [%6zx] %s\n", pos, start);
13472 : 44686 : start = end + 1;
13473 [ + + ]: 44686 : } while (start < limit);
13474 : : }
13475 : : }
13476 : 374 : }
13477 : :
13478 : : static void
13479 : 198 : for_each_section_argument (Elf *elf, const struct section_argument *list,
13480 : : void (*dump) (Elf_Scn *scn, const GElf_Shdr *shdr,
13481 : : const char *name))
13482 : : {
13483 : : /* Get the section header string table index. */
13484 : 198 : size_t shstrndx;
13485 [ - + ]: 198 : if (elf_getshdrstrndx (elf, &shstrndx) < 0)
13486 : 0 : error_exit (0, _("cannot get section header string table index"));
13487 : :
13488 [ + + ]: 764 : for (const struct section_argument *a = list; a != NULL; a = a->next)
13489 : : {
13490 : 566 : Elf_Scn *scn;
13491 : 566 : GElf_Shdr shdr_mem;
13492 : 566 : const char *name = NULL;
13493 : :
13494 : 566 : char *endp = NULL;
13495 : 566 : unsigned long int shndx = strtoul (a->arg, &endp, 0);
13496 [ + + - + ]: 566 : if (endp != a->arg && *endp == '\0')
13497 : : {
13498 : 2 : scn = elf_getscn (elf, shndx);
13499 [ - + ]: 2 : if (scn == NULL)
13500 : : {
13501 : 0 : error (0, 0, _("\nsection [%lu] does not exist"), shndx);
13502 : 0 : continue;
13503 : : }
13504 : :
13505 [ - + ]: 2 : if (gelf_getshdr (scn, &shdr_mem) == NULL)
13506 : 0 : error_exit (0, _("cannot get section header: %s"),
13507 : : elf_errmsg (-1));
13508 : 2 : name = elf_strptr (elf, shstrndx, shdr_mem.sh_name);
13509 : 2 : (*dump) (scn, &shdr_mem, name);
13510 : : }
13511 : : else
13512 : : {
13513 : : /* Need to look up the section by name. */
13514 : : scn = NULL;
13515 : : bool found = false;
13516 [ + + ]: 18180 : while ((scn = elf_nextscn (elf, scn)) != NULL)
13517 : : {
13518 [ - + ]: 17616 : if (gelf_getshdr (scn, &shdr_mem) == NULL)
13519 : 0 : continue;
13520 : 17616 : name = elf_strptr (elf, shstrndx, shdr_mem.sh_name);
13521 [ - + ]: 17616 : if (name == NULL)
13522 : 0 : continue;
13523 [ + + ]: 17616 : if (!strcmp (name, a->arg))
13524 : : {
13525 : 384 : found = true;
13526 : 384 : (*dump) (scn, &shdr_mem, name);
13527 : : }
13528 : : }
13529 : :
13530 [ + + - + ]: 564 : if (unlikely (!found) && !a->implicit)
13531 : 566 : error (0, 0, _("\nsection '%s' does not exist"), a->arg);
13532 : : }
13533 : : }
13534 : 198 : }
13535 : :
13536 : : static void
13537 : 12 : dump_data (Ebl *ebl)
13538 : : {
13539 : 12 : for_each_section_argument (ebl->elf, dump_data_sections, &dump_data_section);
13540 : 12 : }
13541 : :
13542 : : static void
13543 : 186 : dump_strings (Ebl *ebl)
13544 : : {
13545 : 186 : for_each_section_argument (ebl->elf, string_sections, &print_string_section);
13546 : 186 : }
13547 : :
13548 : : static void
13549 : 0 : print_strings (Ebl *ebl)
13550 : : {
13551 : : /* Get the section header string table index. */
13552 : 0 : size_t shstrndx;
13553 [ # # ]: 0 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
13554 : 0 : error_exit (0, _("cannot get section header string table index"));
13555 : :
13556 : : Elf_Scn *scn;
13557 : : GElf_Shdr shdr_mem;
13558 : : const char *name;
13559 : : scn = NULL;
13560 [ # # ]: 0 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
13561 : : {
13562 [ # # ]: 0 : if (gelf_getshdr (scn, &shdr_mem) == NULL)
13563 : 0 : continue;
13564 : :
13565 [ # # ]: 0 : if (shdr_mem.sh_type != SHT_PROGBITS
13566 [ # # ]: 0 : || !(shdr_mem.sh_flags & SHF_STRINGS))
13567 : 0 : continue;
13568 : :
13569 : 0 : name = elf_strptr (ebl->elf, shstrndx, shdr_mem.sh_name);
13570 [ # # ]: 0 : if (name == NULL)
13571 : 0 : continue;
13572 : :
13573 : 0 : print_string_section (scn, &shdr_mem, name);
13574 : : }
13575 : 0 : }
13576 : :
13577 : : static void
13578 : 4 : dump_archive_index (Elf *elf, const char *fname)
13579 : : {
13580 : 4 : size_t narsym;
13581 : 4 : const Elf_Arsym *arsym = elf_getarsym (elf, &narsym);
13582 [ - + ]: 4 : if (arsym == NULL)
13583 : : {
13584 : 0 : int result = elf_errno ();
13585 [ # # ]: 0 : if (unlikely (result != ELF_E_NO_INDEX))
13586 : 0 : error_exit (0, _("cannot get symbol index of archive '%s': %s"),
13587 : : fname, elf_errmsg (result));
13588 : : else
13589 : 0 : printf (_("\nArchive '%s' has no symbol index\n"), fname);
13590 : 0 : return;
13591 : : }
13592 : :
13593 : 4 : printf (_("\nIndex of archive '%s' has %zu entries:\n"),
13594 : : fname, narsym);
13595 : :
13596 : 4 : size_t as_off = 0;
13597 [ + + ]: 22 : for (const Elf_Arsym *s = arsym; s < &arsym[narsym - 1]; ++s)
13598 : : {
13599 [ + + ]: 18 : if (s->as_off != as_off)
13600 : : {
13601 : 12 : as_off = s->as_off;
13602 : :
13603 : 12 : Elf *subelf = NULL;
13604 [ + - ]: 12 : if (unlikely (elf_rand (elf, as_off) == 0)
13605 [ - + ]: 12 : || unlikely ((subelf = elf_begin (-1, ELF_C_READ_MMAP, elf))
13606 : : == NULL))
13607 : : #if __GLIBC__ < 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ < 7)
13608 : : while (1)
13609 : : #endif
13610 : 0 : error_exit (0,
13611 : : _("cannot extract member at offset %zu in '%s': %s"),
13612 : : as_off, fname, elf_errmsg (-1));
13613 : :
13614 : 12 : const Elf_Arhdr *h = elf_getarhdr (subelf);
13615 [ + - ]: 12 : if (h != NULL)
13616 : 12 : printf (_("Archive member '%s' contains:\n"), h->ar_name);
13617 : :
13618 : 12 : elf_end (subelf);
13619 : : }
13620 : :
13621 : 18 : printf ("\t%s\n", s->as_name);
13622 : : }
13623 : : }
13624 : :
13625 : : #include "debugpred.h"
|