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 USE_LOCKS
61 : : #include "threadlib.h"
62 : : #endif
63 : :
64 : : #ifdef HAVE_SCHED_H
65 : : #include <sched.h>
66 : : #endif
67 : :
68 : : #ifdef HAVE_SYS_RESOURCE_H
69 : : #include <sys/resource.h>
70 : : #endif
71 : :
72 : : #ifdef __linux__
73 : : #define CORE_SIGILL SIGILL
74 : : #define CORE_SIGBUS SIGBUS
75 : : #define CORE_SIGFPE SIGFPE
76 : : #define CORE_SIGSEGV SIGSEGV
77 : : #define CORE_SI_USER SI_USER
78 : : #else
79 : : /* We want the linux version of those as that is what shows up in the core files. */
80 : : #define CORE_SIGILL 4 /* Illegal instruction (ANSI). */
81 : : #define CORE_SIGBUS 7 /* BUS error (4.2 BSD). */
82 : : #define CORE_SIGFPE 8 /* Floating-point exception (ANSI). */
83 : : #define CORE_SIGSEGV 11 /* Segmentation violation (ANSI). */
84 : : #define CORE_SI_USER 0 /* Sent by kill, sigsend. */
85 : : #endif
86 : :
87 : : /* Name and version of program. */
88 : : ARGP_PROGRAM_VERSION_HOOK_DEF = print_version;
89 : :
90 : : /* Bug report address. */
91 : : ARGP_PROGRAM_BUG_ADDRESS_DEF = PACKAGE_BUGREPORT;
92 : :
93 : : /* argp key value for --elf-section, non-ascii. */
94 : : #define ELF_INPUT_SECTION 256
95 : :
96 : : /* argp key value for --dwarf-skeleton, non-ascii. */
97 : : #define DWARF_SKELETON 257
98 : :
99 : : /* argp key value for --dyn-syms, non-ascii. */
100 : : #define PRINT_DYNSYM_TABLE 258
101 : :
102 : : /* Terrible hack for hooking unrelated skeleton/split compile units,
103 : : see __libdw_link_skel_split in print_debug. */
104 : : static bool do_not_close_dwfl = false;
105 : :
106 : : /* Definitions of arguments for argp functions. */
107 : : static const struct argp_option options[] =
108 : : {
109 : : { NULL, 0, NULL, 0, N_("ELF input selection:"), 0 },
110 : : { "elf-section", ELF_INPUT_SECTION, "SECTION", OPTION_ARG_OPTIONAL,
111 : : N_("Use the named SECTION (default .gnu_debugdata) as (compressed) ELF "
112 : : "input data"), 0 },
113 : : { "dwarf-skeleton", DWARF_SKELETON, "FILE", 0,
114 : : N_("Used with -w to find the skeleton Compile Units in FILE associated "
115 : : "with the Split Compile units in a .dwo input file"), 0 },
116 : : { NULL, 0, NULL, 0, N_("ELF output selection:"), 0 },
117 : : { "all", 'a', NULL, 0,
118 : : N_("All these plus -p .strtab -p .dynstr -p .comment"), 0 },
119 : : { "dynamic", 'd', NULL, 0, N_("Display the dynamic segment"), 0 },
120 : : { "file-header", 'h', NULL, 0, N_("Display the ELF file header"), 0 },
121 : : { "histogram", 'I', NULL, 0,
122 : : N_("Display histogram of bucket list lengths"), 0 },
123 : : { "program-headers", 'l', NULL, 0, N_("Display the program headers"), 0 },
124 : : { "segments", 'l', NULL, OPTION_ALIAS | OPTION_HIDDEN, NULL, 0 },
125 : : { "relocs", 'r', NULL, 0, N_("Display relocations"), 0 },
126 : : { "section-groups", 'g', NULL, 0, N_("Display the section groups"), 0 },
127 : : { "section-headers", 'S', NULL, 0, N_("Display the sections' headers"), 0 },
128 : : { "sections", 'S', NULL, OPTION_ALIAS | OPTION_HIDDEN, NULL, 0 },
129 : : { "symbols", 's', "SECTION", OPTION_ARG_OPTIONAL,
130 : : N_("Display the symbol table sections"), 0 },
131 : : { "syms", 's', NULL, OPTION_ALIAS | OPTION_HIDDEN, NULL, 0 },
132 : : { "dyn-syms", PRINT_DYNSYM_TABLE, NULL, 0,
133 : : N_("Display (only) the dynamic symbol table"), 0 },
134 : : { "version-info", 'V', NULL, 0, N_("Display versioning information"), 0 },
135 : : { "notes", 'n', "SECTION", OPTION_ARG_OPTIONAL, N_("Display the ELF notes"), 0 },
136 : : { "arch-specific", 'A', NULL, 0,
137 : : N_("Display architecture specific information, if any"), 0 },
138 : : { "exception", 'e', NULL, 0,
139 : : N_("Display sections for exception handling"), 0 },
140 : :
141 : : { NULL, 0, NULL, 0, N_("Additional output selection:"), 0 },
142 : : { "debug-dump", 'w', "SECTION", OPTION_ARG_OPTIONAL,
143 : : N_("Display DWARF section content. SECTION can be one of abbrev, addr, "
144 : : "aranges, decodedaranges, frame, gdb_index, info, info+, loc, line, "
145 : : "decodedline, ranges, pubnames, str, macinfo, macro or exception"), 0 },
146 : : { "hex-dump", 'x', "SECTION", 0,
147 : : N_("Dump the uninterpreted contents of SECTION, by number or name"), 0 },
148 : : { "strings", 'p', "SECTION", OPTION_ARG_OPTIONAL,
149 : : N_("Print string contents of sections"), 0 },
150 : : { "string-dump", 'p', NULL, OPTION_ALIAS | OPTION_HIDDEN, NULL, 0 },
151 : : { "archive-index", 'c', NULL, 0,
152 : : N_("Display the symbol index of an archive"), 0 },
153 : : { "use-dynamic", 'D', NULL, 0,
154 : : N_("Use the dynamic segment when possible for displaying info"), 0 },
155 : :
156 : : { NULL, 0, NULL, 0, N_("Output control:"), 0 },
157 : : { "numeric-addresses", 'N', NULL, 0,
158 : : N_("Do not find symbol names for addresses in DWARF data"), 0 },
159 : : { "unresolved-address-offsets", 'U', NULL, 0,
160 : : N_("Display just offsets instead of resolving values to addresses in DWARF data"), 0 },
161 : : { "wide", 'W', NULL, 0,
162 : : N_("Ignored for compatibility (lines always wide)"), 0 },
163 : : { "decompress", 'z', NULL, 0,
164 : : N_("Show compression information for compressed sections (when used with -S); decompress section before dumping data (when used with -p or -x)"), 0 },
165 : : #ifdef USE_LOCKS
166 : : { "concurrency", 'C', "NUM", 0,
167 : : N_("Set maximum number of threads. Defaults to the number of CPUs."), 0 },
168 : : #endif
169 : : { NULL, 0, NULL, 0, NULL, 0 }
170 : : };
171 : :
172 : : /* Short description of program. */
173 : : static const char doc[] = N_("\
174 : : Print information from ELF file in human-readable form.");
175 : :
176 : : /* Strings for arguments in help texts. */
177 : : static const char args_doc[] = N_("FILE...");
178 : :
179 : : /* Prototype for option handler. */
180 : : static error_t parse_opt (int key, char *arg, struct argp_state *state);
181 : :
182 : : /* Data structure to communicate with argp functions. */
183 : : static struct argp argp =
184 : : {
185 : : options, parse_opt, args_doc, doc, NULL, NULL, NULL
186 : : };
187 : :
188 : : /* If non-null, the section from which we should read to (compressed) ELF. */
189 : : static const char *elf_input_section = NULL;
190 : :
191 : : /* If non-null, the file that contains the skeleton CUs. */
192 : : static const char *dwarf_skeleton = NULL;
193 : :
194 : : /* Flags set by the option controlling the output. */
195 : :
196 : : /* True if dynamic segment should be printed. */
197 : : static bool print_dynamic_table;
198 : :
199 : : /* True if the file header should be printed. */
200 : : static bool print_file_header;
201 : :
202 : : /* True if the program headers should be printed. */
203 : : static bool print_program_header;
204 : :
205 : : /* True if relocations should be printed. */
206 : : static bool print_relocations;
207 : :
208 : : /* True if the section headers should be printed. */
209 : : static bool print_section_header;
210 : :
211 : : /* True if the symbol table should be printed. */
212 : : static bool print_symbol_table;
213 : :
214 : : /* True if (only) the dynsym table should be printed. */
215 : : static bool print_dynsym_table;
216 : :
217 : : /* True if reconstruct dynamic symbol table from the PT_DYNAMIC segment. */
218 : : static bool use_dynamic_segment;
219 : :
220 : : /* A specific section name, or NULL to print all symbol tables. */
221 : : static char *symbol_table_section;
222 : :
223 : : /* A specific section name, or NULL to print all ELF notes. */
224 : : static char *notes_section;
225 : :
226 : : /* True if the version information should be printed. */
227 : : static bool print_version_info;
228 : :
229 : : /* True if section groups should be printed. */
230 : : static bool print_section_groups;
231 : :
232 : : /* True if bucket list length histogram should be printed. */
233 : : static bool print_histogram;
234 : :
235 : : /* True if the architecture specific data should be printed. */
236 : : static bool print_arch;
237 : :
238 : : /* True if note section content should be printed. */
239 : : static bool print_notes;
240 : :
241 : : /* True if SHF_STRINGS section content should be printed. */
242 : : static bool print_string_sections;
243 : :
244 : : /* True if archive index should be printed. */
245 : : static bool print_archive_index;
246 : :
247 : : /* True if any of the control options except print_archive_index is set. */
248 : : static bool any_control_option;
249 : :
250 : : /* True if we should print addresses from DWARF in symbolic form. */
251 : : static bool print_address_names = true;
252 : :
253 : : /* True if we should print raw values instead of relativized addresses. */
254 : : static bool print_unresolved_addresses = false;
255 : :
256 : : /* True if we should print the .debug_aranges section using libdw. */
257 : : static bool decodedaranges = false;
258 : :
259 : : /* True if we should print the .debug_aranges section using libdw. */
260 : : static bool decodedline = false;
261 : :
262 : : /* True if we want to show more information about compressed sections. */
263 : : static bool print_decompress = false;
264 : :
265 : : /* True if we want to show split compile units for debug_info skeletons. */
266 : : static bool show_split_units = false;
267 : :
268 : : #if USE_LOCKS
269 : : /* Maximum number of threads. */
270 : : static int max_threads = 0;
271 : : #endif
272 : :
273 : : /* Select printing of debugging sections. */
274 : : static enum section_e
275 : : {
276 : : section_abbrev = 1, /* .debug_abbrev */
277 : : section_aranges = 2, /* .debug_aranges */
278 : : section_frame = 4, /* .debug_frame or .eh_frame & al. */
279 : : section_info = 8, /* .debug_info, (implies .debug_types) */
280 : : section_line = 16, /* .debug_line */
281 : : section_loc = 32, /* .debug_loc */
282 : : section_pubnames = 64, /* .debug_pubnames */
283 : : section_str = 128, /* .debug_str */
284 : : section_macinfo = 256, /* .debug_macinfo */
285 : : section_ranges = 512, /* .debug_ranges */
286 : : section_exception = 1024, /* .eh_frame & al. */
287 : : section_gdb_index = 2048, /* .gdb_index */
288 : : section_macro = 4096, /* .debug_macro */
289 : : section_addr = 8192, /* .debug_addr */
290 : : section_types = 16384, /* .debug_types (implied by .debug_info) */
291 : : section_all = (section_abbrev | section_aranges | section_frame
292 : : | section_info | section_line | section_loc
293 : : | section_pubnames | section_str | section_macinfo
294 : : | section_ranges | section_exception | section_gdb_index
295 : : | section_macro | section_addr | section_types)
296 : : } print_debug_sections, implicit_debug_sections;
297 : :
298 : : /* Select hex dumping of sections. */
299 : : static struct section_argument *dump_data_sections;
300 : : static struct section_argument **dump_data_sections_tail = &dump_data_sections;
301 : :
302 : : /* Select string dumping of sections. */
303 : : static struct section_argument *string_sections;
304 : : static struct section_argument **string_sections_tail = &string_sections;
305 : :
306 : : struct section_argument
307 : : {
308 : : struct section_argument *next;
309 : : const char *arg;
310 : : bool implicit;
311 : : };
312 : :
313 : : /* Numbers of sections and program headers in the file. */
314 : : static size_t shnum;
315 : : static size_t phnum;
316 : :
317 : :
318 : : /* Declarations of local functions. */
319 : : static void process_file (int fd, const char *fname, bool only_one);
320 : : static void process_elf_file (Dwfl_Module *dwflmod, int fd);
321 : : static void print_ehdr (Ebl *ebl, GElf_Ehdr *ehdr);
322 : : static void print_shdr (Ebl *ebl, GElf_Ehdr *ehdr);
323 : : static void print_phdr (Ebl *ebl, GElf_Ehdr *ehdr);
324 : : static void print_scngrp (Ebl *ebl);
325 : : static void print_dynamic (Ebl *ebl);
326 : : static void print_relocs (Ebl *ebl, Dwfl_Module *mod, GElf_Ehdr *ehdr);
327 : : static void handle_relocs_rel (Ebl *ebl, GElf_Ehdr *ehdr, Elf_Scn *scn,
328 : : GElf_Shdr *shdr);
329 : : static void handle_relocs_rela (Ebl *ebl, GElf_Ehdr *ehdr, Elf_Scn *scn,
330 : : GElf_Shdr *shdr);
331 : : static void handle_relocs_relr (Ebl *ebl, Dwfl_Module *mod, Elf_Scn *scn,
332 : : GElf_Shdr *shdr);
333 : : static bool print_symtab (Ebl *ebl, int type);
334 : : static bool handle_symtab (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr);
335 : : static bool handle_dynamic_symtab (Ebl *ebl);
336 : : static void
337 : : process_symtab(
338 : : Ebl * ebl,
339 : : unsigned int nsyms,
340 : : Elf64_Word idx,
341 : : Elf32_Word verneed_stridx,
342 : : Elf32_Word verdef_stridx,
343 : : Elf_Data * symdata,
344 : : Elf_Data * versym_data,
345 : : Elf_Data * symstr_data,
346 : : Elf_Data * verneed_data,
347 : : Elf_Data * verdef_data,
348 : : Elf_Data * xndx_data);
349 : : static void print_verinfo (Ebl *ebl);
350 : : static void handle_verneed (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr);
351 : : static void handle_verdef (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr);
352 : : static void handle_versym (Ebl *ebl, Elf_Scn *scn,
353 : : GElf_Shdr *shdr);
354 : : static void print_debug (Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr);
355 : : static void handle_hash (Ebl *ebl);
356 : : static void handle_notes (Ebl *ebl, GElf_Ehdr *ehdr);
357 : : static void print_liblist (Ebl *ebl);
358 : : static void print_attributes (Ebl *ebl, const GElf_Ehdr *ehdr);
359 : : static void dump_data (Ebl *ebl);
360 : : static void dump_strings (Ebl *ebl);
361 : : static void print_strings (Ebl *ebl);
362 : : static void dump_archive_index (Elf *, const char *);
363 : : static void print_dwarf_addr (Dwfl_Module *dwflmod, int address_size,
364 : : Dwarf_Addr address, Dwarf_Addr raw, FILE *out);
365 : : static void print_flag_info(void);
366 : :
367 : : enum dyn_idx
368 : : {
369 : : i_symtab_shndx,
370 : : i_strsz,
371 : : i_verneed,
372 : : i_verneednum,
373 : : i_verdef,
374 : : i_verdefnum,
375 : : i_versym,
376 : : i_symtab,
377 : : i_strtab,
378 : : i_hash,
379 : : i_gnu_hash,
380 : : i_max
381 : : };
382 : :
383 : : /* Declarations of local functions for use-dynamic. */
384 : : static Elf_Data *get_dynscn_strtab (Elf *elf, GElf_Phdr *phdr);
385 : : static void get_dynscn_addrs (Elf *elf, GElf_Phdr *phdr, GElf_Addr addrs[i_max]);
386 : : static void find_offsets (Elf *elf, GElf_Addr main_bias, size_t n,
387 : : GElf_Addr addrs[n], GElf_Off offs[n]);
388 : :
389 : : /* Looked up once with gettext in main. */
390 : : static char *yes_str;
391 : : static char *no_str;
392 : :
393 : : static void
394 : 1780 : cleanup_list (struct section_argument *list)
395 : : {
396 [ + + + + ]: 2346 : while (list != NULL)
397 : : {
398 : 566 : struct section_argument *a = list;
399 : 566 : list = a->next;
400 : 566 : free (a);
401 : : }
402 : : }
403 : :
404 : : #ifdef USE_LOCKS
405 : : /* Estimate the maximum number of threads. This is normally
406 : : #CPU. Return value is guaranteed to be at least 1. */
407 : : static int
408 : : default_concurrency (void)
409 : : {
410 : : unsigned aff = 0;
411 : : #ifdef HAVE_SCHED_GETAFFINITY
412 : : {
413 : : int ret;
414 : : cpu_set_t mask;
415 : : CPU_ZERO (&mask);
416 : : ret = sched_getaffinity (0, sizeof(mask), &mask);
417 : : if (ret == 0)
418 : : aff = CPU_COUNT (&mask);
419 : : }
420 : : #endif
421 : :
422 : : unsigned fn = 0;
423 : : #ifdef HAVE_GETRLIMIT
424 : : {
425 : : struct rlimit rlim;
426 : : int rc = getrlimit (RLIMIT_NOFILE, &rlim);
427 : : if (rc == 0)
428 : : fn = MAX ((rlim_t) 1, (rlim.rlim_cur - 100) / 2);
429 : : /* Conservatively estimate that at least 2 fds are used
430 : : by each thread. */
431 : : }
432 : : #endif
433 : :
434 : : unsigned d = MIN (MAX (aff, 1U),
435 : : MAX (fn, 1U));
436 : :
437 : : return d;
438 : : }
439 : : #endif
440 : :
441 : : int
442 : 890 : main (int argc, char *argv[])
443 : : {
444 : : /* We use no threads here which can interfere with handling a stream. */
445 : 890 : (void) __fsetlocking (stdout, FSETLOCKING_BYCALLER);
446 : :
447 : : /* Set locale. */
448 : 890 : setlocale (LC_ALL, "");
449 : :
450 : : /* Initialize the message catalog. */
451 : 890 : textdomain (PACKAGE_TARNAME);
452 : :
453 : : /* Look up once. */
454 : 890 : yes_str = _("yes");
455 : 890 : no_str = _("no");
456 : :
457 : : /* Parse and process arguments. */
458 : 890 : int remaining;
459 : 890 : argp_parse (&argp, argc, argv, 0, &remaining, NULL);
460 : :
461 : : /* Before we start tell the ELF library which version we are using. */
462 : 890 : elf_version (EV_CURRENT);
463 : :
464 : : #ifdef USE_LOCKS
465 : : /* If concurrency wasn't set by argp_parse, then set a default value. */
466 : : if (max_threads == 0)
467 : : max_threads = default_concurrency ();
468 : : #endif
469 : :
470 : : /* Now process all the files given at the command line. */
471 : 890 : bool only_one = remaining + 1 == argc;
472 : 972 : do
473 : : {
474 : : /* Open the file. */
475 : 972 : int fd = open (argv[remaining], O_RDONLY);
476 [ - + ]: 972 : if (fd == -1)
477 : : {
478 : 0 : error (0, errno, _("cannot open input file '%s'"), argv[remaining]);
479 : 0 : continue;
480 : : }
481 : :
482 : 972 : process_file (fd, argv[remaining], only_one);
483 : :
484 : 972 : close (fd);
485 : : }
486 [ + + ]: 972 : while (++remaining < argc);
487 : :
488 : 890 : cleanup_list (dump_data_sections);
489 : 890 : cleanup_list (string_sections);
490 : :
491 : 890 : return error_message_count != 0;
492 : : }
493 : :
494 : : static void
495 : 566 : add_dump_section (const char *name,
496 : : int key,
497 : : bool implicit)
498 : : {
499 : 566 : struct section_argument *a = xmalloc (sizeof *a);
500 : 566 : a->arg = name;
501 : 566 : a->next = NULL;
502 : 566 : a->implicit = implicit;
503 : 1132 : struct section_argument ***tailp
504 [ + + ]: 566 : = key == 'x' ? &dump_data_sections_tail : &string_sections_tail;
505 : 566 : **tailp = a;
506 : 566 : *tailp = &a->next;
507 : 566 : }
508 : :
509 : : /* Handle program arguments. */
510 : : static error_t
511 : 5662 : parse_opt (int key, char *arg,
512 : : struct argp_state *state __attribute__ ((unused)))
513 : : {
514 [ + + - + : 5662 : switch (key)
+ - + + -
+ + + + +
+ - + + +
+ + + - +
+ + + + ]
515 : : {
516 : 184 : case 'a':
517 : 184 : print_file_header = true;
518 : 184 : print_program_header = true;
519 : 184 : print_relocations = true;
520 : 184 : print_section_header = true;
521 : 184 : print_symbol_table = true;
522 : 184 : print_version_info = true;
523 : 184 : print_dynamic_table = true;
524 : 184 : print_section_groups = true;
525 : 184 : print_histogram = true;
526 : 184 : print_arch = true;
527 : 184 : print_notes = true;
528 : 184 : implicit_debug_sections |= section_exception;
529 : 184 : add_dump_section (".strtab", key, true);
530 : 184 : add_dump_section (".dynstr", key, true);
531 : 184 : add_dump_section (".comment", key, true);
532 : 184 : any_control_option = true;
533 : 184 : break;
534 : 8 : case 'A':
535 : 8 : print_arch = true;
536 : 8 : any_control_option = true;
537 : 8 : break;
538 : 6 : case 'd':
539 : 6 : print_dynamic_table = true;
540 : 6 : any_control_option = true;
541 : 6 : break;
542 : 2 : case 'D':
543 : 2 : use_dynamic_segment = true;
544 : 2 : break;
545 : 0 : case 'e':
546 : 0 : print_debug_sections |= section_exception;
547 : 0 : any_control_option = true;
548 : 0 : break;
549 : 18 : case 'g':
550 : 18 : print_section_groups = true;
551 : 18 : any_control_option = true;
552 : 18 : break;
553 : 6 : case 'h':
554 : 6 : print_file_header = true;
555 : 6 : any_control_option = true;
556 : 6 : break;
557 : 0 : case 'I':
558 : 0 : print_histogram = true;
559 : 0 : any_control_option = true;
560 : 0 : break;
561 : 4 : case 'l':
562 : 4 : print_program_header = true;
563 : 4 : any_control_option = true;
564 : 4 : break;
565 : 46 : case 'n':
566 : 46 : print_notes = true;
567 : 46 : any_control_option = true;
568 : 46 : notes_section = arg;
569 : 46 : break;
570 : 10 : case 'r':
571 : 10 : print_relocations = true;
572 : 10 : any_control_option = true;
573 : 10 : break;
574 : 322 : case 'S':
575 : 322 : print_section_header = true;
576 : 322 : any_control_option = true;
577 : 322 : break;
578 : 34 : case 's':
579 : 34 : print_symbol_table = true;
580 : 34 : any_control_option = true;
581 : 34 : symbol_table_section = arg;
582 : 34 : break;
583 : 2 : case PRINT_DYNSYM_TABLE:
584 : 2 : print_dynsym_table = true;
585 : 2 : any_control_option = true;
586 : 2 : break;
587 : 0 : case 'V':
588 : 0 : print_version_info = true;
589 : 0 : any_control_option = true;
590 : 0 : break;
591 : 4 : case 'c':
592 : 4 : print_archive_index = true;
593 : 4 : break;
594 : : #if USE_LOCKS
595 : : case 'C':
596 : : if (arg != NULL)
597 : : {
598 : : max_threads = atoi (arg);
599 : : if (max_threads < 1)
600 : : {
601 : : argp_error (state, _("-C NUM minimum 1"));
602 : : return EINVAL;
603 : : }
604 : : }
605 : : break;
606 : : #endif
607 : 264 : case 'w':
608 [ + + ]: 264 : if (arg == NULL)
609 : : {
610 : 86 : print_debug_sections = section_all;
611 : 86 : implicit_debug_sections = section_info;
612 : 86 : show_split_units = true;
613 : : }
614 [ - + ]: 178 : else if (strcmp (arg, "abbrev") == 0)
615 : 0 : print_debug_sections |= section_abbrev;
616 [ + + ]: 178 : else if (strcmp (arg, "addr") == 0)
617 : : {
618 : 4 : print_debug_sections |= section_addr;
619 : 4 : implicit_debug_sections |= section_info;
620 : : }
621 [ + + ]: 174 : else if (strcmp (arg, "aranges") == 0)
622 : 6 : print_debug_sections |= section_aranges;
623 [ + + ]: 168 : else if (strcmp (arg, "decodedaranges") == 0)
624 : : {
625 : 2 : print_debug_sections |= section_aranges;
626 : 2 : decodedaranges = true;
627 : : }
628 [ + + ]: 166 : else if (strcmp (arg, "ranges") == 0)
629 : : {
630 : 26 : print_debug_sections |= section_ranges;
631 : 26 : implicit_debug_sections |= section_info;
632 : : }
633 [ + + + + ]: 140 : else if (strcmp (arg, "frame") == 0 || strcmp (arg, "frames") == 0)
634 : 6 : print_debug_sections |= section_frame;
635 [ + + ]: 134 : else if (strcmp (arg, "info") == 0)
636 : : {
637 : 40 : print_debug_sections |= section_info;
638 : 40 : print_debug_sections |= section_types;
639 : : }
640 [ + + ]: 94 : else if (strcmp (arg, "info+") == 0)
641 : : {
642 : 4 : print_debug_sections |= section_info;
643 : 4 : print_debug_sections |= section_types;
644 : 4 : show_split_units = true;
645 : : }
646 [ + + ]: 90 : else if (strcmp (arg, "loc") == 0)
647 : : {
648 : 38 : print_debug_sections |= section_loc;
649 : 38 : implicit_debug_sections |= section_info;
650 : : }
651 [ + + ]: 52 : else if (strcmp (arg, "line") == 0)
652 : 16 : print_debug_sections |= section_line;
653 [ + + ]: 36 : else if (strcmp (arg, "decodedline") == 0)
654 : : {
655 : 14 : print_debug_sections |= section_line;
656 : 14 : decodedline = true;
657 : : }
658 [ - + ]: 22 : else if (strcmp (arg, "pubnames") == 0)
659 : 0 : print_debug_sections |= section_pubnames;
660 [ + + ]: 22 : else if (strcmp (arg, "str") == 0)
661 : : {
662 : 6 : print_debug_sections |= section_str;
663 : : /* For mapping string offset tables to CUs. */
664 : 6 : implicit_debug_sections |= section_info;
665 : : }
666 [ - + ]: 16 : else if (strcmp (arg, "macinfo") == 0)
667 : 0 : print_debug_sections |= section_macinfo;
668 [ + + ]: 16 : else if (strcmp (arg, "macro") == 0)
669 : 8 : print_debug_sections |= section_macro;
670 [ - + ]: 8 : else if (strcmp (arg, "exception") == 0)
671 : 0 : print_debug_sections |= section_exception;
672 [ + - ]: 8 : else if (strcmp (arg, "gdb_index") == 0)
673 : 8 : print_debug_sections |= section_gdb_index;
674 : : else
675 : : {
676 : 0 : fprintf (stderr, _("Unknown DWARF debug section `%s'.\n"),
677 : : arg);
678 : 0 : argp_help (&argp, stderr, ARGP_HELP_SEE,
679 : : program_invocation_short_name);
680 : 0 : exit (1);
681 : : }
682 : 264 : any_control_option = true;
683 : 264 : break;
684 : 2 : case 'p':
685 : 2 : any_control_option = true;
686 [ - + ]: 2 : if (arg == NULL)
687 : : {
688 : 0 : print_string_sections = true;
689 : 0 : break;
690 : : }
691 : 14 : FALLTHROUGH;
692 : : case 'x':
693 : 14 : add_dump_section (arg, key, false);
694 : 14 : any_control_option = true;
695 : 14 : break;
696 : 90 : case 'N':
697 : 90 : print_address_names = false;
698 : 90 : break;
699 : 56 : case 'U':
700 : 56 : print_unresolved_addresses = true;
701 : 56 : break;
702 : 0 : case ARGP_KEY_NO_ARGS:
703 : 0 : fputs (_("Missing file name.\n"), stderr);
704 : 0 : goto do_argp_help;
705 : 890 : case ARGP_KEY_FINI:
706 [ + + + - ]: 890 : if (! any_control_option && ! print_archive_index)
707 : : {
708 : 0 : fputs (_("No operation specified.\n"), stderr);
709 : 0 : do_argp_help:
710 : 0 : argp_help (&argp, stderr, ARGP_HELP_SEE,
711 : : program_invocation_short_name);
712 : 0 : exit (EXIT_FAILURE);
713 : : }
714 : : break;
715 : : case 'W': /* Ignored. */
716 : : break;
717 : 122 : case 'z':
718 : 122 : print_decompress = true;
719 : 122 : break;
720 : 10 : case ELF_INPUT_SECTION:
721 [ + - ]: 10 : if (arg == NULL)
722 : 10 : elf_input_section = ".gnu_debugdata";
723 : : else
724 : 0 : elf_input_section = arg;
725 : : break;
726 : 10 : case DWARF_SKELETON:
727 : 10 : dwarf_skeleton = arg;
728 : 10 : break;
729 : : default:
730 : : return ARGP_ERR_UNKNOWN;
731 : : }
732 : : return 0;
733 : : }
734 : :
735 : :
736 : : /* Create a file descriptor to read the data from the
737 : : elf_input_section given a file descriptor to an ELF file. */
738 : : static int
739 : 10 : open_input_section (int fd)
740 : : {
741 : 10 : size_t shnums;
742 : 10 : size_t cnt;
743 : 10 : size_t shstrndx;
744 : 10 : Elf *elf = elf_begin (fd, ELF_C_READ_MMAP, NULL);
745 [ - + ]: 10 : if (elf == NULL)
746 : : {
747 : 0 : error (0, 0, _("cannot generate Elf descriptor: %s"),
748 : : elf_errmsg (-1));
749 : 0 : return -1;
750 : : }
751 : :
752 [ - + ]: 10 : if (elf_getshdrnum (elf, &shnums) < 0)
753 : : {
754 : 0 : error (0, 0, _("cannot determine number of sections: %s"),
755 : : elf_errmsg (-1));
756 : 0 : open_error:
757 : 0 : elf_end (elf);
758 : 0 : return -1;
759 : : }
760 : :
761 [ - + ]: 10 : if (elf_getshdrstrndx (elf, &shstrndx) < 0)
762 : : {
763 : 0 : error (0, 0, _("cannot get section header string table index"));
764 : 0 : goto open_error;
765 : : }
766 : :
767 [ + - ]: 236 : for (cnt = 0; cnt < shnums; ++cnt)
768 : : {
769 : 236 : Elf_Scn *scn = elf_getscn (elf, cnt);
770 [ - + ]: 236 : if (scn == NULL)
771 : : {
772 : 0 : error (0, 0, _("cannot get section: %s"),
773 : : elf_errmsg (-1));
774 : 0 : goto open_error;
775 : : }
776 : :
777 : 236 : GElf_Shdr shdr_mem;
778 : 236 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
779 [ - + ]: 236 : if (unlikely (shdr == NULL))
780 : : {
781 : 0 : error (0, 0, _("cannot get section header: %s"),
782 : : elf_errmsg (-1));
783 : 0 : goto open_error;
784 : : }
785 : :
786 : 236 : const char *sname = elf_strptr (elf, shstrndx, shdr->sh_name);
787 [ - + ]: 236 : if (sname == NULL)
788 : : {
789 : 0 : error (0, 0, _("cannot get section name"));
790 : 0 : goto open_error;
791 : : }
792 : :
793 [ + + ]: 236 : if (strcmp (sname, elf_input_section) == 0)
794 : : {
795 : 10 : Elf_Data *data = elf_rawdata (scn, NULL);
796 [ - + ]: 10 : if (data == NULL)
797 : : {
798 : 0 : error (0, 0, _("cannot get %s content: %s"),
799 : : sname, elf_errmsg (-1));
800 : 0 : goto open_error;
801 : : }
802 : :
803 : : /* Create (and immediately unlink) a temporary file to store
804 : : section data in to create a file descriptor for it. */
805 [ + - ]: 10 : const char *tmpdir = getenv ("TMPDIR") ?: P_tmpdir;
806 : 10 : static const char suffix[] = "/readelfXXXXXX";
807 : 10 : int tmplen = strlen (tmpdir) + sizeof (suffix);
808 : 10 : char *tempname = alloca (tmplen);
809 : 10 : sprintf (tempname, "%s%s", tmpdir, suffix);
810 : :
811 : 10 : int sfd = mkstemp (tempname);
812 [ - + ]: 10 : if (sfd == -1)
813 : : {
814 : 0 : error (0, 0, _("cannot create temp file '%s'"),
815 : : tempname);
816 : 0 : goto open_error;
817 : : }
818 : 10 : unlink (tempname);
819 : :
820 : 10 : ssize_t size = data->d_size;
821 [ - + ]: 10 : if (write_retry (sfd, data->d_buf, size) != size)
822 : : {
823 : 0 : error (0, 0, _("cannot write section data"));
824 : 0 : goto open_error;
825 : : }
826 : :
827 [ - + ]: 10 : if (elf_end (elf) != 0)
828 : : {
829 : 0 : error (0, 0, _("error while closing Elf descriptor: %s"),
830 : : elf_errmsg (-1));
831 : 10 : return -1;
832 : : }
833 : :
834 [ - + ]: 10 : if (lseek (sfd, 0, SEEK_SET) == -1)
835 : : {
836 : 0 : error (0, 0, _("error while rewinding file descriptor"));
837 : 0 : return -1;
838 : : }
839 : :
840 : : return sfd;
841 : : }
842 : : }
843 : :
844 : : /* Named section not found. */
845 [ # # ]: 0 : if (elf_end (elf) != 0)
846 : 0 : error (0, 0, _("error while closing Elf descriptor: %s"),
847 : : elf_errmsg (-1));
848 : : return -1;
849 : : }
850 : :
851 : : /* Check if the file is an archive, and if so dump its index. */
852 : : static void
853 : 4 : check_archive_index (int fd, const char *fname, bool only_one)
854 : : {
855 : : /* Create an `Elf' descriptor. */
856 : 4 : Elf *elf = elf_begin (fd, ELF_C_READ_MMAP, NULL);
857 [ - + ]: 4 : if (elf == NULL)
858 : 0 : error (0, 0, _("cannot generate Elf descriptor: %s"),
859 : : elf_errmsg (-1));
860 : : else
861 : : {
862 [ + - ]: 4 : if (elf_kind (elf) == ELF_K_AR)
863 : : {
864 [ - + ]: 4 : if (!only_one)
865 : 0 : printf ("\n%s:\n\n", fname);
866 : 4 : dump_archive_index (elf, fname);
867 : : }
868 : : else
869 : 0 : error (0, 0,
870 : 0 : _("'%s' is not an archive, cannot print archive index"),
871 : : fname);
872 : :
873 : : /* Now we can close the descriptor. */
874 [ - + ]: 4 : if (elf_end (elf) != 0)
875 : 0 : error (0, 0, _("error while closing Elf descriptor: %s"),
876 : : elf_errmsg (-1));
877 : : }
878 : 4 : }
879 : :
880 : : /* Trivial callback used for checking if we opened an archive. */
881 : : static int
882 : 868 : count_dwflmod (Dwfl_Module *dwflmod __attribute__ ((unused)),
883 : : void **userdata __attribute__ ((unused)),
884 : : const char *name __attribute__ ((unused)),
885 : : Dwarf_Addr base __attribute__ ((unused)),
886 : : void *arg)
887 : : {
888 [ + - ]: 868 : if (*(bool *) arg)
889 : : return DWARF_CB_ABORT;
890 : 868 : *(bool *) arg = true;
891 : 868 : return DWARF_CB_OK;
892 : : }
893 : :
894 : : struct process_dwflmod_args
895 : : {
896 : : int fd;
897 : : bool only_one;
898 : : };
899 : :
900 : : static int
901 : 968 : process_dwflmod (Dwfl_Module *dwflmod,
902 : : void **userdata __attribute__ ((unused)),
903 : : const char *name __attribute__ ((unused)),
904 : : Dwarf_Addr base __attribute__ ((unused)),
905 : : void *arg)
906 : : {
907 : 968 : const struct process_dwflmod_args *a = arg;
908 : :
909 : : /* Print the file name. */
910 [ + + ]: 968 : if (!a->only_one)
911 : : {
912 : 100 : const char *fname;
913 : 100 : dwfl_module_info (dwflmod, NULL, NULL, NULL, NULL, NULL, &fname, NULL);
914 : :
915 : 100 : printf ("\n%s:\n\n", fname);
916 : : }
917 : :
918 : 968 : process_elf_file (dwflmod, a->fd);
919 : :
920 : 968 : return DWARF_CB_OK;
921 : : }
922 : :
923 : : /* Stub libdwfl callback, only the ELF handle already open is ever used.
924 : : Only used for finding the alternate debug file if the Dwarf comes from
925 : : the main file. We are not interested in separate debuginfo. */
926 : : static int
927 : 262 : find_no_debuginfo (Dwfl_Module *mod,
928 : : void **userdata,
929 : : const char *modname,
930 : : Dwarf_Addr base,
931 : : const char *file_name,
932 : : const char *debuglink_file,
933 : : GElf_Word debuglink_crc,
934 : : char **debuginfo_file_name)
935 : : {
936 : 262 : Dwarf_Addr dwbias;
937 : 262 : dwfl_module_info (mod, NULL, NULL, NULL, &dwbias, NULL, NULL, NULL);
938 : :
939 : : /* We are only interested if the Dwarf has been setup on the main
940 : : elf file but is only missing the alternate debug link. If dwbias
941 : : hasn't even been setup, this is searching for separate debuginfo
942 : : for the main elf. We don't care in that case. */
943 [ + + ]: 262 : if (dwbias == (Dwarf_Addr) -1)
944 : : return -1;
945 : :
946 : 70 : return dwfl_standard_find_debuginfo (mod, userdata, modname, base,
947 : : file_name, debuglink_file,
948 : : debuglink_crc, debuginfo_file_name);
949 : : }
950 : :
951 : : static Dwfl *
952 : 986 : create_dwfl (int fd, const char *fname)
953 : : {
954 : : /* Duplicate an fd for dwfl_report_offline to swallow. */
955 : 986 : int dwfl_fd = dup (fd);
956 [ - + ]: 986 : if (unlikely (dwfl_fd < 0))
957 : 0 : error_exit (errno, "dup");
958 : :
959 : : /* Use libdwfl in a trivial way to open the libdw handle for us.
960 : : This takes care of applying relocations to DWARF data in ET_REL files. */
961 : 986 : static const Dwfl_Callbacks callbacks =
962 : : {
963 : : .section_address = dwfl_offline_section_address,
964 : : .find_debuginfo = find_no_debuginfo
965 : : };
966 : 986 : Dwfl *dwfl = dwfl_begin (&callbacks);
967 [ + - ]: 986 : if (likely (dwfl != NULL))
968 : : /* Let 0 be the logical address of the file (or first in archive). */
969 : 986 : dwfl->offline_next_address = 0;
970 [ - + ]: 986 : if (dwfl_report_offline (dwfl, fname, fname, dwfl_fd) == NULL)
971 : : {
972 : 0 : struct stat st;
973 [ # # ]: 0 : if (fstat (dwfl_fd, &st) != 0)
974 : 0 : error (0, errno, _("cannot stat input file"));
975 [ # # ]: 0 : else if (unlikely (st.st_size == 0))
976 : 0 : error (0, 0, _("input file is empty"));
977 : : else
978 : 0 : error (0, 0, _("failed reading '%s': %s"),
979 : : fname, dwfl_errmsg (-1));
980 : 0 : close (dwfl_fd); /* Consumed on success, not on failure. */
981 : 0 : dwfl_end (dwfl);
982 : 0 : dwfl = NULL;
983 : : }
984 : : else
985 : 986 : dwfl_report_end (dwfl, NULL, NULL);
986 : :
987 : 986 : return dwfl;
988 : : }
989 : :
990 : : /* Process one input file. */
991 : : static void
992 : 972 : process_file (int fd, const char *fname, bool only_one)
993 : : {
994 [ + + ]: 972 : if (print_archive_index)
995 : 4 : check_archive_index (fd, fname, only_one);
996 : :
997 [ + + ]: 972 : if (!any_control_option)
998 : : return;
999 : :
1000 [ + + ]: 968 : if (elf_input_section != NULL)
1001 : : {
1002 : : /* Replace fname and fd with section content. */
1003 : 10 : char *fnname = alloca (strlen (fname) + strlen (elf_input_section) + 2);
1004 : 10 : sprintf (fnname, "%s:%s", fname, elf_input_section);
1005 : 10 : fd = open_input_section (fd);
1006 [ - + ]: 10 : if (fd == -1)
1007 : : {
1008 : 0 : error (0, 0, _("No such section '%s' in '%s'"),
1009 : : elf_input_section, fname);
1010 : 0 : return;
1011 : : }
1012 : : fname = fnname;
1013 : : }
1014 : :
1015 : 968 : Dwfl *dwfl = create_dwfl (fd, fname);
1016 [ + - ]: 968 : if (dwfl != NULL)
1017 : : {
1018 [ + + ]: 968 : if (only_one)
1019 : : {
1020 : : /* Clear ONLY_ONE if we have multiple modules, from an archive. */
1021 : 868 : bool seen = false;
1022 : 868 : only_one = dwfl_getmodules (dwfl, &count_dwflmod, &seen, 0) == 0;
1023 : : }
1024 : :
1025 : : /* Process the one or more modules gleaned from this file. */
1026 : 968 : struct process_dwflmod_args a = { .fd = fd, .only_one = only_one };
1027 : 968 : dwfl_getmodules (dwfl, &process_dwflmod, &a, 0);
1028 : : }
1029 : : /* Terrible hack for hooking unrelated skeleton/split compile units,
1030 : : see __libdw_link_skel_split in print_debug. */
1031 [ + - ]: 968 : if (! do_not_close_dwfl)
1032 : 968 : dwfl_end (dwfl);
1033 : :
1034 : : /* Need to close the replaced fd if we created it. Caller takes
1035 : : care of original. */
1036 [ + + ]: 968 : if (elf_input_section != NULL)
1037 : 10 : close (fd);
1038 : : }
1039 : :
1040 : : /* Check whether there are any compressed sections in the ELF file. */
1041 : : static bool
1042 : 492 : elf_contains_chdrs (Elf *elf)
1043 : : {
1044 : 492 : Elf_Scn *scn = NULL;
1045 [ + + ]: 797906 : while ((scn = elf_nextscn (elf, scn)) != NULL)
1046 : : {
1047 : 797528 : GElf_Shdr shdr_mem;
1048 : 797528 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
1049 [ + - + + ]: 797528 : if (shdr != NULL && (shdr->sh_flags & SHF_COMPRESSED) != 0)
1050 : 114 : return true;
1051 : : }
1052 : : return false;
1053 : : }
1054 : :
1055 : : /* Process one ELF file. */
1056 : : static void
1057 : 968 : process_elf_file (Dwfl_Module *dwflmod, int fd)
1058 : : {
1059 : 968 : GElf_Addr dwflbias;
1060 : 968 : Elf *elf = dwfl_module_getelf (dwflmod, &dwflbias);
1061 : :
1062 : 968 : GElf_Ehdr ehdr_mem;
1063 : 968 : GElf_Ehdr *ehdr = gelf_getehdr (elf, &ehdr_mem);
1064 : :
1065 [ - + ]: 968 : if (ehdr == NULL)
1066 : : {
1067 : 0 : error (0, 0, _("cannot read ELF header: %s"), elf_errmsg (-1));
1068 : 0 : return;
1069 : : }
1070 : :
1071 : 968 : Ebl *ebl = ebl_openbackend (elf);
1072 [ - + ]: 968 : if (unlikely (ebl == NULL))
1073 : : {
1074 : 0 : ebl_error:
1075 : 0 : error (0, errno, _("cannot create EBL handle"));
1076 : 0 : return;
1077 : : }
1078 : :
1079 : : /* Determine the number of sections. */
1080 [ - + ]: 968 : if (unlikely (elf_getshdrnum (ebl->elf, &shnum) < 0))
1081 : 0 : error_exit (0, _("cannot determine number of sections: %s"),
1082 : : elf_errmsg (-1));
1083 : :
1084 : : /* Determine the number of phdrs. */
1085 [ - + ]: 968 : if (unlikely (elf_getphdrnum (ebl->elf, &phnum) < 0))
1086 : 0 : error_exit (0, _("cannot determine number of program headers: %s"),
1087 : : elf_errmsg (-1));
1088 : :
1089 : : /* For an ET_REL file, libdwfl has adjusted the in-core shdrs and
1090 : : may have applied relocation to some sections. If there are any
1091 : : compressed sections, any pass (or libdw/libdwfl) might have
1092 : : uncompressed them. So we need to get a fresh Elf handle on the
1093 : : file to display those. */
1094 : 2132 : bool print_unchanged = ((print_section_header
1095 [ + + ]: 462 : || print_relocations
1096 [ + + ]: 452 : || dump_data_sections != NULL
1097 [ + + ]: 440 : || print_notes)
1098 [ + + + + ]: 1036 : && (ehdr->e_type == ET_REL
1099 [ + + ]: 492 : || elf_contains_chdrs (ebl->elf)));
1100 : :
1101 : 196 : Elf *pure_elf = NULL;
1102 : 196 : Ebl *pure_ebl = ebl;
1103 : 196 : if (print_unchanged)
1104 : : {
1105 : : /* Read the file afresh. */
1106 : 196 : off_t aroff = elf_getaroff (elf);
1107 : 196 : pure_elf = dwelf_elf_begin (fd);
1108 [ - + ]: 196 : if (aroff > 0)
1109 : : {
1110 : : /* Archive member. */
1111 : 0 : (void) elf_rand (pure_elf, aroff);
1112 : 0 : Elf *armem = elf_begin (-1, ELF_C_READ_MMAP, pure_elf);
1113 : 0 : elf_end (pure_elf);
1114 : 0 : pure_elf = armem;
1115 : : }
1116 [ - + ]: 196 : if (pure_elf == NULL)
1117 : : {
1118 : 0 : error (0, 0, _("cannot read ELF: %s"), elf_errmsg (-1));
1119 : 0 : return;
1120 : : }
1121 : 196 : pure_ebl = ebl_openbackend (pure_elf);
1122 [ - + ]: 196 : if (pure_ebl == NULL)
1123 : 0 : goto ebl_error;
1124 : : }
1125 : :
1126 : 968 : bool symtab_printed = false;
1127 : :
1128 [ + + ]: 968 : if (print_file_header)
1129 : 190 : print_ehdr (ebl, ehdr);
1130 [ + + ]: 968 : if (print_section_header)
1131 : 506 : print_shdr (pure_ebl, ehdr);
1132 [ + + ]: 968 : if (print_program_header)
1133 : 188 : print_phdr (ebl, ehdr);
1134 [ + + ]: 968 : if (print_section_groups)
1135 : 202 : print_scngrp (ebl);
1136 [ + + ]: 968 : if (print_dynamic_table)
1137 : 190 : print_dynamic (ebl);
1138 [ + + ]: 968 : if (print_relocations)
1139 : 194 : print_relocs (pure_ebl, dwflmod, ehdr);
1140 [ + + ]: 968 : if (print_histogram)
1141 : 184 : handle_hash (ebl);
1142 [ + + + + ]: 968 : if (print_symbol_table || print_dynsym_table)
1143 : 220 : symtab_printed |= print_symtab (ebl, SHT_DYNSYM);
1144 [ + + ]: 968 : if (print_version_info)
1145 : 184 : print_verinfo (ebl);
1146 [ + + + - ]: 968 : if (print_symbol_table && !use_dynamic_segment)
1147 : 218 : symtab_printed |= print_symtab (ebl, SHT_SYMTAB);
1148 : :
1149 [ + + + + ]: 968 : if ((print_symbol_table || print_dynsym_table)
1150 [ + + + + ]: 220 : && !symtab_printed && symbol_table_section != NULL)
1151 : 2 : printf ("WARNING: %s: '%s'\n", _("cannot find section"),
1152 : : symbol_table_section);
1153 : :
1154 [ + + ]: 968 : if (print_arch)
1155 : 192 : print_liblist (ebl);
1156 [ + + ]: 968 : if (print_arch)
1157 : 192 : print_attributes (ebl, ehdr);
1158 [ + + ]: 968 : if (dump_data_sections != NULL)
1159 : 12 : dump_data (pure_ebl);
1160 [ + + ]: 968 : if (string_sections != NULL)
1161 : 186 : dump_strings (ebl);
1162 [ + + ]: 968 : if ((print_debug_sections | implicit_debug_sections) != 0)
1163 : 502 : print_debug (dwflmod, ebl, ehdr);
1164 [ + + ]: 968 : if (print_notes)
1165 : 230 : handle_notes (pure_ebl, ehdr);
1166 [ - + ]: 968 : if (print_string_sections)
1167 : 0 : print_strings (ebl);
1168 : :
1169 [ + + ]: 968 : if (pure_ebl != ebl)
1170 : : {
1171 : 196 : ebl_closebackend (ebl);
1172 : 196 : ebl_closebackend (pure_ebl);
1173 : 196 : elf_end (pure_elf);
1174 : : }
1175 : : else
1176 : 772 : ebl_closebackend (ebl);
1177 : : }
1178 : :
1179 : :
1180 : : /* Print file type. */
1181 : : static void
1182 : 190 : print_file_type (unsigned short int e_type)
1183 : : {
1184 [ + - ]: 190 : if (likely (e_type <= ET_CORE))
1185 : : {
1186 : 190 : static const char *const knowntypes[] =
1187 : : {
1188 : : N_("NONE (None)"),
1189 : : N_("REL (Relocatable file)"),
1190 : : N_("EXEC (Executable file)"),
1191 : : N_("DYN (Shared object file)"),
1192 : : N_("CORE (Core file)")
1193 : : };
1194 : 190 : puts (_(knowntypes[e_type]));
1195 : : }
1196 [ # # ]: 0 : else if (e_type >= ET_LOOS && e_type <= ET_HIOS)
1197 : 0 : printf (_("OS Specific: (%x)\n"), e_type);
1198 [ # # ]: 0 : else if (e_type >= ET_LOPROC /* && e_type <= ET_HIPROC always true */)
1199 : 0 : printf (_("Processor Specific: (%x)\n"), e_type);
1200 : : else
1201 : 0 : puts ("???");
1202 : 190 : }
1203 : :
1204 : :
1205 : : /* Print ELF header. */
1206 : : static void
1207 : 190 : print_ehdr (Ebl *ebl, GElf_Ehdr *ehdr)
1208 : : {
1209 : 190 : fputs (_("ELF Header:\n Magic: "), stdout);
1210 [ + + ]: 3230 : for (size_t cnt = 0; cnt < EI_NIDENT; ++cnt)
1211 : 3040 : printf (" %02hhx", ehdr->e_ident[cnt]);
1212 : :
1213 : 190 : printf (_("\n Class: %s\n"),
1214 [ + + ]: 190 : ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? "ELF32"
1215 : : : ehdr->e_ident[EI_CLASS] == ELFCLASS64 ? "ELF64"
1216 [ - + ]: 156 : : "\?\?\?");
1217 : :
1218 : 190 : printf (_(" Data: %s\n"),
1219 [ - + ]: 190 : ehdr->e_ident[EI_DATA] == ELFDATA2LSB
1220 : : ? "2's complement, little endian"
1221 : : : ehdr->e_ident[EI_DATA] == ELFDATA2MSB
1222 [ # # ]: 0 : ? "2's complement, big endian" : "\?\?\?");
1223 : :
1224 : 570 : printf (_(" Ident Version: %hhd %s\n"),
1225 : 190 : ehdr->e_ident[EI_VERSION],
1226 [ + - ]: 190 : ehdr->e_ident[EI_VERSION] == EV_CURRENT ? _("(current)")
1227 : : : "(\?\?\?)");
1228 : :
1229 : 190 : char buf[512];
1230 : 190 : printf (_(" OS/ABI: %s\n"),
1231 : 190 : ebl_osabi_name (ebl, ehdr->e_ident[EI_OSABI], buf, sizeof (buf)));
1232 : :
1233 : 380 : printf (_(" ABI Version: %hhd\n"),
1234 : 190 : ehdr->e_ident[EI_ABIVERSION]);
1235 : :
1236 : 190 : fputs (_(" Type: "), stdout);
1237 : 190 : print_file_type (ehdr->e_type);
1238 : :
1239 : 190 : const char *machine = dwelf_elf_e_machine_string (ehdr->e_machine);
1240 [ + - ]: 190 : if (machine != NULL)
1241 : 190 : printf (_(" Machine: %s\n"), machine);
1242 : : else
1243 : 0 : printf (_(" Machine: <unknown>: 0x%x\n"),
1244 : 0 : ehdr->e_machine);
1245 : :
1246 : 190 : printf (_(" Version: %d %s\n"),
1247 : : ehdr->e_version,
1248 [ + - ]: 190 : ehdr->e_version == EV_CURRENT ? _("(current)") : "(\?\?\?)");
1249 : :
1250 : 190 : printf (_(" Entry point address: %#" PRIx64 "\n"),
1251 : : ehdr->e_entry);
1252 : :
1253 : 190 : printf (_(" Start of program headers: %" PRId64 " %s\n"),
1254 : : ehdr->e_phoff, _("(bytes into file)"));
1255 : :
1256 : 190 : printf (_(" Start of section headers: %" PRId64 " %s\n"),
1257 : : ehdr->e_shoff, _("(bytes into file)"));
1258 : :
1259 : 190 : printf (_(" Flags: %s\n"),
1260 : : ebl_machine_flag_name (ebl, ehdr->e_flags, buf, sizeof (buf)));
1261 : :
1262 : 380 : printf (_(" Size of this header: %" PRId16 " %s\n"),
1263 : 190 : ehdr->e_ehsize, _("(bytes)"));
1264 : :
1265 : 380 : printf (_(" Size of program header entries: %" PRId16 " %s\n"),
1266 : 190 : ehdr->e_phentsize, _("(bytes)"));
1267 : :
1268 : 380 : printf (_(" Number of program headers entries: %" PRId16),
1269 : 190 : ehdr->e_phnum);
1270 [ + + ]: 190 : if (ehdr->e_phnum == PN_XNUM)
1271 : : {
1272 : 2 : GElf_Shdr shdr_mem;
1273 : 2 : GElf_Shdr *shdr = gelf_getshdr (elf_getscn (ebl->elf, 0), &shdr_mem);
1274 [ + - ]: 2 : if (shdr != NULL)
1275 : 2 : printf (_(" (%" PRIu32 " in [0].sh_info)"),
1276 : 2 : (uint32_t) shdr->sh_info);
1277 : : else
1278 : 0 : fputs (_(" ([0] not available)"), stdout);
1279 : : }
1280 : 190 : fputc ('\n', stdout);
1281 : :
1282 : 380 : printf (_(" Size of section header entries: %" PRId16 " %s\n"),
1283 : 190 : ehdr->e_shentsize, _("(bytes)"));
1284 : :
1285 : 380 : printf (_(" Number of section headers entries: %" PRId16),
1286 : 190 : ehdr->e_shnum);
1287 [ - + ]: 190 : if (ehdr->e_shnum == 0)
1288 : : {
1289 : 0 : GElf_Shdr shdr_mem;
1290 : 0 : GElf_Shdr *shdr = gelf_getshdr (elf_getscn (ebl->elf, 0), &shdr_mem);
1291 [ # # ]: 0 : if (shdr != NULL)
1292 : 0 : printf (_(" (%" PRIu32 " in [0].sh_size)"),
1293 : 0 : (uint32_t) shdr->sh_size);
1294 : : else
1295 : 0 : fputs (_(" ([0] not available)"), stdout);
1296 : : }
1297 : 190 : fputc ('\n', stdout);
1298 : :
1299 [ - + ]: 190 : if (unlikely (ehdr->e_shstrndx == SHN_XINDEX))
1300 : : {
1301 : 0 : GElf_Shdr shdr_mem;
1302 : 0 : GElf_Shdr *shdr = gelf_getshdr (elf_getscn (ebl->elf, 0), &shdr_mem);
1303 [ # # ]: 0 : if (shdr != NULL)
1304 : : /* We managed to get the zeroth section. */
1305 : 0 : snprintf (buf, sizeof (buf), _(" (%" PRIu32 " in [0].sh_link)"),
1306 : 0 : (uint32_t) shdr->sh_link);
1307 : : else
1308 : : {
1309 : 0 : strncpy (buf, _(" ([0] not available)"), sizeof (buf) - 1);
1310 : 0 : buf[sizeof (buf) - 1] = '\0';
1311 : : }
1312 : :
1313 : 0 : printf (_(" Section header string table index: XINDEX%s\n\n"),
1314 : : buf);
1315 : : }
1316 : : else
1317 : 190 : printf (_(" Section header string table index: %" PRId16 "\n\n"),
1318 : : ehdr->e_shstrndx);
1319 : 190 : }
1320 : :
1321 : :
1322 : : static const char *
1323 : 80578 : get_visibility_type (int value)
1324 : : {
1325 [ - + - - : 80578 : switch (value)
+ ]
1326 : : {
1327 : : case STV_DEFAULT:
1328 : : return "DEFAULT";
1329 : 0 : case STV_INTERNAL:
1330 : 0 : return "INTERNAL";
1331 : 956 : case STV_HIDDEN:
1332 : 956 : return "HIDDEN";
1333 : 0 : case STV_PROTECTED:
1334 : 0 : return "PROTECTED";
1335 : 0 : default:
1336 : 0 : return "???";
1337 : : }
1338 : : }
1339 : :
1340 : : static const char *
1341 : 384 : elf_ch_type_name (unsigned int code)
1342 : : {
1343 : 384 : switch (code)
1344 : : {
1345 : : case 0:
1346 : : return "NONE";
1347 : 24 : case ELFCOMPRESS_ZLIB:
1348 : 24 : return "ZLIB";
1349 : 360 : case ELFCOMPRESS_ZSTD:
1350 : 360 : return "ZSTD";
1351 : 0 : default:
1352 : 0 : return "UNKNOWN";
1353 : : }
1354 : : }
1355 : :
1356 : : /* Print the section headers. */
1357 : : static void
1358 : 506 : print_shdr (Ebl *ebl, GElf_Ehdr *ehdr)
1359 : : {
1360 : 506 : size_t cnt;
1361 : 506 : size_t shstrndx;
1362 : :
1363 [ + + ]: 506 : if (! print_file_header)
1364 : : {
1365 : 322 : size_t sections;
1366 [ - + ]: 322 : if (unlikely (elf_getshdrnum (ebl->elf, §ions) < 0))
1367 : 0 : error_exit (0, _("cannot get number of sections: %s"),
1368 : : elf_errmsg (-1));
1369 : :
1370 : 322 : printf (_("\
1371 : : There are %zd section headers, starting at offset %#" PRIx64 ":\n\
1372 : : \n"),
1373 : : sections, ehdr->e_shoff);
1374 : : }
1375 : :
1376 : : /* Get the section header string table index. */
1377 [ - + ]: 506 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
1378 : 0 : error_exit (0, _("cannot get section header string table index: %s"),
1379 : : elf_errmsg (-1));
1380 : :
1381 : 506 : puts (_("Section Headers:"));
1382 : :
1383 [ + + ]: 506 : if (ehdr->e_ident[EI_CLASS] == ELFCLASS32)
1384 : 196 : puts (_("[Nr] Name Type Addr Off Size ES Flags Lk Inf Al"));
1385 : : else
1386 : 310 : puts (_("[Nr] Name Type Addr Off Size ES Flags Lk Inf Al"));
1387 : :
1388 [ + + ]: 506 : if (print_decompress)
1389 : : {
1390 [ + + ]: 112 : if (ehdr->e_ident[EI_CLASS] == ELFCLASS32)
1391 : 56 : puts (_(" [Compression Size Al]"));
1392 : : else
1393 : 56 : puts (_(" [Compression Size Al]"));
1394 : : }
1395 : :
1396 [ + + ]: 1586316 : for (cnt = 0; cnt < shnum; ++cnt)
1397 : : {
1398 : 1585810 : Elf_Scn *scn = elf_getscn (ebl->elf, cnt);
1399 : :
1400 [ - + ]: 1585810 : if (unlikely (scn == NULL))
1401 : 0 : error_exit (0, _("cannot get section: %s"),
1402 : : elf_errmsg (-1));
1403 : :
1404 : : /* Get the section header. */
1405 : 1585810 : GElf_Shdr shdr_mem;
1406 : 1585810 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
1407 [ - + ]: 1585810 : if (unlikely (shdr == NULL))
1408 : 0 : error_exit (0, _("cannot get section header: %s"),
1409 : : elf_errmsg (-1));
1410 : :
1411 : 1585810 : char flagbuf[20];
1412 : 1585810 : char *cp = flagbuf;
1413 [ + + ]: 1585810 : if (shdr->sh_flags & SHF_WRITE)
1414 : 2764 : *cp++ = 'W';
1415 [ + + ]: 1585810 : if (shdr->sh_flags & SHF_ALLOC)
1416 : 8520 : *cp++ = 'A';
1417 [ + + ]: 1585810 : if (shdr->sh_flags & SHF_EXECINSTR)
1418 : 1564 : *cp++ = 'X';
1419 [ + + ]: 1585810 : if (shdr->sh_flags & SHF_MERGE)
1420 : 420 : *cp++ = 'M';
1421 [ + + ]: 1585810 : if (shdr->sh_flags & SHF_STRINGS)
1422 : 380 : *cp++ = 'S';
1423 [ + + ]: 1585810 : if (shdr->sh_flags & SHF_INFO_LINK)
1424 : 196 : *cp++ = 'I';
1425 [ + + ]: 1585810 : if (shdr->sh_flags & SHF_LINK_ORDER)
1426 : 4 : *cp++ = 'L';
1427 [ - + ]: 1585810 : if (shdr->sh_flags & SHF_OS_NONCONFORMING)
1428 : 0 : *cp++ = 'N';
1429 [ - + ]: 1585810 : if (shdr->sh_flags & SHF_GROUP)
1430 : 0 : *cp++ = 'G';
1431 [ + + ]: 1585810 : if (shdr->sh_flags & SHF_TLS)
1432 : 30 : *cp++ = 'T';
1433 [ + + ]: 1585810 : if (shdr->sh_flags & SHF_COMPRESSED)
1434 : 414 : *cp++ = 'C';
1435 [ - + ]: 1585810 : if (shdr->sh_flags & SHF_ORDERED)
1436 : 0 : *cp++ = 'O';
1437 [ - + ]: 1585810 : if (shdr->sh_flags & SHF_EXCLUDE)
1438 : 0 : *cp++ = 'E';
1439 [ + + ]: 1585810 : if (shdr->sh_flags & SHF_GNU_RETAIN)
1440 : 2 : *cp++ = 'R';
1441 : 1585810 : *cp = '\0';
1442 : :
1443 : 1585810 : const char *sname;
1444 : 1585810 : char buf[128];
1445 [ - + ]: 1585810 : sname = elf_strptr (ebl->elf, shstrndx, shdr->sh_name) ?: "<corrupt>";
1446 [ + + ]: 3171620 : printf ("[%2zu] %-20s %-12s %0*" PRIx64 " %0*" PRIx64 " %0*" PRIx64
1447 : : " %2" PRId64 " %-5s %2" PRId32 " %3" PRId32
1448 : : " %2" PRId64 "\n",
1449 : : cnt, sname,
1450 : 1585810 : ebl_section_type_name (ebl, shdr->sh_type, buf, sizeof (buf)),
1451 : : ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 8 : 16, shdr->sh_addr,
1452 : : ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 6 : 8, shdr->sh_offset,
1453 [ + + ]: 1585810 : ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 6 : 8, shdr->sh_size,
1454 : : shdr->sh_entsize, flagbuf, shdr->sh_link, shdr->sh_info,
1455 : : shdr->sh_addralign);
1456 : :
1457 [ + + ]: 1585810 : if (print_decompress)
1458 : : {
1459 [ + + ]: 1880 : if ((shdr->sh_flags & SHF_COMPRESSED) != 0)
1460 : : {
1461 : 384 : GElf_Chdr chdr;
1462 [ + - ]: 384 : if (gelf_getchdr (scn, &chdr) != NULL)
1463 [ + + - - ]: 768 : printf (" [ELF %s (%" PRId32 ") %0*" PRIx64
1464 : : " %2" PRId64 "]\n",
1465 : : elf_ch_type_name (chdr.ch_type),
1466 : : chdr.ch_type,
1467 [ + + ]: 384 : ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 6 : 8,
1468 : : chdr.ch_size, chdr.ch_addralign);
1469 : : else
1470 : 0 : error (0, 0,
1471 : 0 : _("bad compression header for section %zd: %s"),
1472 : : elf_ndxscn (scn), elf_errmsg (-1));
1473 : : }
1474 [ + + ]: 1496 : else if (startswith (sname, ".zdebug"))
1475 : : {
1476 : 24 : ssize_t size;
1477 [ + - ]: 24 : if ((size = dwelf_scn_gnu_compressed_size (scn)) >= 0)
1478 : 24 : printf (" [GNU ZLIB %0*zx ]\n",
1479 [ + + ]: 24 : ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 6 : 8, size);
1480 : : else
1481 : 0 : error (0, 0,
1482 : 0 : _("bad gnu compressed size for section %zd: %s"),
1483 : : elf_ndxscn (scn), elf_errmsg (-1));
1484 : : }
1485 : : }
1486 : : }
1487 : :
1488 : 506 : print_flag_info();
1489 : 506 : fputc ('\n', stdout);
1490 : 506 : }
1491 : :
1492 : : /* Print flag information. */
1493 : : static void
1494 : 506 : print_flag_info (void)
1495 : : {
1496 : 506 : puts ("Key to Flags:");
1497 : 506 : puts (" W (write), A (alloc), X (execute), M (merge), S (strings), I (info),");
1498 : 506 : puts (" L (link order), N (extra OS processing required), G (group), T (TLS),");
1499 : 506 : puts (" C (compressed), O (ordered), R (GNU retain), E (exclude)");
1500 : 506 : }
1501 : :
1502 : : /* Print the program header. */
1503 : : static void
1504 : 188 : print_phdr (Ebl *ebl, GElf_Ehdr *ehdr)
1505 : : {
1506 [ + + ]: 188 : if (phnum == 0)
1507 : : /* No program header, this is OK in relocatable objects. */
1508 : 12 : return;
1509 : :
1510 : 176 : puts (_("Program Headers:"));
1511 [ + + ]: 176 : if (ehdr->e_ident[EI_CLASS] == ELFCLASS32)
1512 : 26 : puts (_("\
1513 : : Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align"));
1514 : : else
1515 : 150 : puts (_("\
1516 : : Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align"));
1517 : :
1518 : : /* Process all program headers. */
1519 : : bool has_relro = false;
1520 : : GElf_Addr relro_from = 0;
1521 : : GElf_Addr relro_to = 0;
1522 [ + + ]: 2074 : for (size_t cnt = 0; cnt < phnum; ++cnt)
1523 : : {
1524 : 1898 : char buf[128];
1525 : 1898 : GElf_Phdr mem;
1526 : 1898 : GElf_Phdr *phdr = gelf_getphdr (ebl->elf, cnt, &mem);
1527 : :
1528 : : /* If for some reason the header cannot be returned show this. */
1529 [ - + ]: 1898 : if (unlikely (phdr == NULL))
1530 : : {
1531 : 0 : puts (" ???");
1532 : 0 : continue;
1533 : : }
1534 : :
1535 : 1898 : printf (" %-14s 0x%06" PRIx64 " 0x%0*" PRIx64 " 0x%0*" PRIx64
1536 : : " 0x%06" PRIx64 " 0x%06" PRIx64 " %c%c%c 0x%" PRIx64 "\n",
1537 : 1898 : ebl_segment_type_name (ebl, phdr->p_type, buf, sizeof (buf)),
1538 : : phdr->p_offset,
1539 : : ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 8 : 16, phdr->p_vaddr,
1540 [ + + ]: 1898 : ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 8 : 16, phdr->p_paddr,
1541 : : phdr->p_filesz,
1542 : : phdr->p_memsz,
1543 [ - + ]: 1898 : phdr->p_flags & PF_R ? 'R' : ' ',
1544 [ + + ]: 1898 : phdr->p_flags & PF_W ? 'W' : ' ',
1545 [ + + ]: 1898 : phdr->p_flags & PF_X ? 'E' : ' ',
1546 : : phdr->p_align);
1547 : :
1548 [ + + ]: 1898 : if (phdr->p_type == PT_INTERP)
1549 : : {
1550 : : /* If we are sure the file offset is valid then we can show
1551 : : the user the name of the interpreter. We check whether
1552 : : there is a section at the file offset. Normally there
1553 : : would be a section called ".interp". But in separate
1554 : : .debug files it is a NOBITS section (and so doesn't match
1555 : : with gelf_offscn). Which probably means the offset is
1556 : : not valid another reason could be because the ELF file
1557 : : just doesn't contain any section headers, in that case
1558 : : just play it safe and don't display anything. */
1559 : :
1560 : 172 : Elf_Scn *scn = gelf_offscn (ebl->elf, phdr->p_offset);
1561 : 172 : GElf_Shdr shdr_mem;
1562 : 172 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
1563 : :
1564 : 172 : size_t maxsize;
1565 : 172 : char *filedata = elf_rawfile (ebl->elf, &maxsize);
1566 : :
1567 [ + - + + ]: 172 : if (shdr != NULL && shdr->sh_type == SHT_PROGBITS
1568 [ + - + - ]: 130 : && filedata != NULL && phdr->p_offset < maxsize
1569 [ + - ]: 130 : && phdr->p_filesz <= maxsize - phdr->p_offset
1570 [ + - ]: 130 : && memchr (filedata + phdr->p_offset, '\0',
1571 : : phdr->p_filesz) != NULL)
1572 : 130 : printf (_("\t[Requesting program interpreter: %s]\n"),
1573 : : filedata + phdr->p_offset);
1574 : : }
1575 [ + + ]: 1726 : else if (phdr->p_type == PT_GNU_RELRO)
1576 : : {
1577 : 152 : has_relro = true;
1578 : 152 : relro_from = phdr->p_vaddr;
1579 : 152 : relro_to = relro_from + phdr->p_memsz;
1580 : : }
1581 : : }
1582 : :
1583 : 176 : size_t sections;
1584 [ - + ]: 176 : if (unlikely (elf_getshdrnum (ebl->elf, §ions) < 0))
1585 : 0 : error_exit (0, _("cannot get number of sections: %s"),
1586 : : elf_errmsg (-1));
1587 : :
1588 [ + - ]: 176 : if (sections == 0)
1589 : : /* No sections in the file. Punt. */
1590 : : return;
1591 : :
1592 : : /* Get the section header string table index. */
1593 : 176 : size_t shstrndx;
1594 [ - + ]: 176 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
1595 : 0 : error_exit (0, _("cannot get section header string table index"));
1596 : :
1597 : 176 : puts (_("\n Section to Segment mapping:\n Segment Sections..."));
1598 : :
1599 [ + + ]: 2074 : for (size_t cnt = 0; cnt < phnum; ++cnt)
1600 : : {
1601 : : /* Print the segment number. */
1602 : 1898 : printf (" %2.2zu ", cnt);
1603 : :
1604 : 1898 : GElf_Phdr phdr_mem;
1605 : 1898 : GElf_Phdr *phdr = gelf_getphdr (ebl->elf, cnt, &phdr_mem);
1606 : : /* This must not happen. */
1607 [ - + ]: 1898 : if (unlikely (phdr == NULL))
1608 : 0 : error_exit (0, _("cannot get program header: %s"),
1609 : : elf_errmsg (-1));
1610 : :
1611 : : /* Iterate over the sections. */
1612 : : bool in_relro = false;
1613 : : bool in_ro = false;
1614 [ + + ]: 62206 : for (size_t inner = 1; inner < shnum; ++inner)
1615 : : {
1616 : 60308 : Elf_Scn *scn = elf_getscn (ebl->elf, inner);
1617 : : /* This should not happen. */
1618 [ - + ]: 60308 : if (unlikely (scn == NULL))
1619 : 0 : error_exit (0, _("cannot get section: %s"),
1620 : : elf_errmsg (-1));
1621 : :
1622 : : /* Get the section header. */
1623 : 60308 : GElf_Shdr shdr_mem;
1624 : 60308 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
1625 [ - + ]: 60308 : if (unlikely (shdr == NULL))
1626 : 0 : error_exit (0, _("cannot get section header: %s"),
1627 : : elf_errmsg (-1));
1628 : :
1629 [ + - ]: 60308 : if (shdr->sh_size > 0
1630 : : /* Compare allocated sections by VMA, unallocated
1631 : : sections by file offset. */
1632 [ + + ]: 60308 : && (shdr->sh_flags & SHF_ALLOC
1633 : 47582 : ? (shdr->sh_addr >= phdr->p_vaddr
1634 [ + + ]: 47582 : && (shdr->sh_addr + shdr->sh_size
1635 [ + + ]: 31986 : <= phdr->p_vaddr + phdr->p_memsz))
1636 : 12726 : : (shdr->sh_offset >= phdr->p_offset
1637 [ + + ]: 12726 : && (shdr->sh_offset + shdr->sh_size
1638 [ + + ]: 11200 : <= phdr->p_offset + phdr->p_filesz))))
1639 : : {
1640 [ + + ]: 6262 : if (has_relro && !in_relro
1641 [ + + ]: 4178 : && shdr->sh_addr >= relro_from
1642 [ + + ]: 732 : && shdr->sh_addr + shdr->sh_size <= relro_to)
1643 : : {
1644 : 474 : fputs (" [RELRO:", stdout);
1645 : 474 : in_relro = true;
1646 : : }
1647 [ + + + + ]: 5788 : else if (has_relro && in_relro && shdr->sh_addr >= relro_to)
1648 : : {
1649 : 134 : fputs ("]", stdout);
1650 : 134 : in_relro = false;
1651 : : }
1652 [ + + ]: 5654 : else if (has_relro && in_relro
1653 [ + + ]: 1166 : && shdr->sh_addr + shdr->sh_size > relro_to)
1654 : 18 : fputs ("] <RELRO:", stdout);
1655 [ + + ]: 5636 : else if (phdr->p_type == PT_LOAD && (phdr->p_flags & PF_W) == 0)
1656 : : {
1657 [ + + ]: 3226 : if (!in_ro)
1658 : : {
1659 : 372 : fputs (" [RO:", stdout);
1660 : 372 : in_ro = true;
1661 : : }
1662 : : }
1663 : : else
1664 : : {
1665 : : /* Determine the segment this section is part of. */
1666 : : size_t cnt2;
1667 : : GElf_Phdr phdr2_mem;
1668 : : GElf_Phdr *phdr2 = NULL;
1669 [ + - ]: 10860 : for (cnt2 = 0; cnt2 < phnum; ++cnt2)
1670 : : {
1671 : 10860 : phdr2 = gelf_getphdr (ebl->elf, cnt2, &phdr2_mem);
1672 : :
1673 [ + - + + ]: 10860 : if (phdr2 != NULL && phdr2->p_type == PT_LOAD
1674 [ + - ]: 6152 : && shdr->sh_addr >= phdr2->p_vaddr
1675 : 6152 : && (shdr->sh_addr + shdr->sh_size
1676 [ + + ]: 6152 : <= phdr2->p_vaddr + phdr2->p_memsz))
1677 : : break;
1678 : : }
1679 : :
1680 [ + - ]: 2410 : if (cnt2 < phnum)
1681 : : {
1682 [ + + + + ]: 2410 : if ((phdr2->p_flags & PF_W) == 0 && !in_ro)
1683 : : {
1684 : 656 : fputs (" [RO:", stdout);
1685 : 656 : in_ro = true;
1686 : : }
1687 [ + + + - ]: 1754 : else if ((phdr2->p_flags & PF_W) != 0 && in_ro)
1688 : : {
1689 : 0 : fputs ("]", stdout);
1690 : 0 : in_ro = false;
1691 : : }
1692 : : }
1693 : : }
1694 : :
1695 : 6262 : printf (" %s",
1696 : 6262 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name));
1697 : :
1698 : : /* Signal that this section is only partially covered. */
1699 [ + + ]: 6262 : if (has_relro && in_relro
1700 [ + + ]: 1640 : && shdr->sh_addr + shdr->sh_size > relro_to)
1701 : : {
1702 : 18 : fputs (">", stdout);
1703 : 18 : in_relro = false;
1704 : : }
1705 : : }
1706 : : }
1707 [ + + ]: 1898 : if (in_relro || in_ro)
1708 : 1350 : fputs ("]", stdout);
1709 : :
1710 : : /* Finish the line. */
1711 : 1898 : fputc ('\n', stdout);
1712 : : }
1713 : : }
1714 : :
1715 : :
1716 : : static const char *
1717 : 1252 : section_name (Ebl *ebl, GElf_Shdr *shdr)
1718 : : {
1719 : 1252 : size_t shstrndx;
1720 [ + - - + ]: 1252 : if (shdr == NULL || elf_getshdrstrndx (ebl->elf, &shstrndx) < 0)
1721 : 0 : return "???";
1722 [ - + ]: 1252 : return elf_strptr (ebl->elf, shstrndx, shdr->sh_name) ?: "???";
1723 : : }
1724 : :
1725 : :
1726 : : static void
1727 : 32 : handle_scngrp (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr)
1728 : : {
1729 : : /* Get the data of the section. */
1730 : 32 : Elf_Data *data = elf_getdata (scn, NULL);
1731 : :
1732 : 32 : Elf_Scn *symscn = elf_getscn (ebl->elf, shdr->sh_link);
1733 : 32 : GElf_Shdr symshdr_mem;
1734 : 32 : GElf_Shdr *symshdr = gelf_getshdr (symscn, &symshdr_mem);
1735 : 32 : Elf_Data *symdata = elf_getdata (symscn, NULL);
1736 : :
1737 [ + - + - ]: 32 : if (data == NULL || data->d_size < sizeof (Elf32_Word) || symshdr == NULL
1738 [ - + ]: 32 : || symdata == NULL)
1739 : 0 : return;
1740 : :
1741 : : /* Get the section header string table index. */
1742 : 32 : size_t shstrndx;
1743 [ - + ]: 32 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
1744 : 0 : error_exit (0, _("cannot get section header string table index"));
1745 : :
1746 : 32 : Elf32_Word *grpref = (Elf32_Word *) data->d_buf;
1747 : :
1748 : 32 : GElf_Sym sym_mem;
1749 : 32 : GElf_Sym *sym = gelf_getsym (symdata, shdr->sh_info, &sym_mem);
1750 : :
1751 [ - + + + ]: 64 : printf ((grpref[0] & GRP_COMDAT)
1752 : 8 : ? ngettext ("\
1753 : : \nCOMDAT section group [%2zu] '%s' with signature '%s' contains %zu entry:\n",
1754 : : "\
1755 : : \nCOMDAT section group [%2zu] '%s' with signature '%s' contains %zu entries:\n",
1756 : : data->d_size / sizeof (Elf32_Word) - 1)
1757 : 24 : : ngettext ("\
1758 : : \nSection group [%2zu] '%s' with signature '%s' contains %zu entry:\n", "\
1759 : : \nSection group [%2zu] '%s' with signature '%s' contains %zu entries:\n",
1760 : : data->d_size / sizeof (Elf32_Word) - 1),
1761 : : elf_ndxscn (scn),
1762 : 32 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
1763 : : (sym == NULL ? NULL
1764 : 32 : : elf_strptr (ebl->elf, symshdr->sh_link, sym->st_name))
1765 : 0 : ?: _("<INVALID SYMBOL>"),
1766 [ + - ]: 32 : data->d_size / sizeof (Elf32_Word) - 1);
1767 : :
1768 [ + + ]: 104 : for (size_t cnt = 1; cnt < data->d_size / sizeof (Elf32_Word); ++cnt)
1769 : : {
1770 : 72 : GElf_Shdr grpshdr_mem;
1771 : 72 : GElf_Shdr *grpshdr = gelf_getshdr (elf_getscn (ebl->elf, grpref[cnt]),
1772 : : &grpshdr_mem);
1773 : :
1774 : 72 : const char *str;
1775 [ + - ]: 144 : printf (" [%2u] %s\n",
1776 : : grpref[cnt],
1777 : : grpshdr != NULL
1778 [ - + ]: 72 : && (str = elf_strptr (ebl->elf, shstrndx, grpshdr->sh_name))
1779 : 0 : ? str : _("<INVALID SECTION>"));
1780 : : }
1781 : : }
1782 : :
1783 : :
1784 : : static void
1785 : 202 : print_scngrp (Ebl *ebl)
1786 : : {
1787 : : /* Find all relocation sections and handle them. */
1788 : 202 : Elf_Scn *scn = NULL;
1789 : :
1790 [ + + ]: 6522 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
1791 : : {
1792 : : /* Handle the section if it is a symbol table. */
1793 : 6320 : GElf_Shdr shdr_mem;
1794 : 6320 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
1795 : :
1796 [ + - + + ]: 6320 : if (shdr != NULL && shdr->sh_type == SHT_GROUP)
1797 : : {
1798 [ - + ]: 32 : if ((shdr->sh_flags & SHF_COMPRESSED) != 0)
1799 : : {
1800 [ # # ]: 0 : if (elf_compress (scn, 0, 0) < 0)
1801 : 0 : printf ("WARNING: %s [%zd]\n",
1802 : : _("Couldn't uncompress section"),
1803 : : elf_ndxscn (scn));
1804 : 0 : shdr = gelf_getshdr (scn, &shdr_mem);
1805 [ # # ]: 0 : if (unlikely (shdr == NULL))
1806 : 0 : error_exit (0, _("cannot get section [%zd] header: %s"),
1807 : : elf_ndxscn (scn),
1808 : : elf_errmsg (-1));
1809 : : }
1810 : 32 : handle_scngrp (ebl, scn, shdr);
1811 : : }
1812 : : }
1813 : 202 : }
1814 : :
1815 : :
1816 : : static const struct flags
1817 : : {
1818 : : int mask;
1819 : : const char *str;
1820 : : } dt_flags[] =
1821 : : {
1822 : : { DF_ORIGIN, "ORIGIN" },
1823 : : { DF_SYMBOLIC, "SYMBOLIC" },
1824 : : { DF_TEXTREL, "TEXTREL" },
1825 : : { DF_BIND_NOW, "BIND_NOW" },
1826 : : { DF_STATIC_TLS, "STATIC_TLS" }
1827 : : };
1828 : : static const int ndt_flags = sizeof (dt_flags) / sizeof (dt_flags[0]);
1829 : :
1830 : : static const struct flags dt_flags_1[] =
1831 : : {
1832 : : { DF_1_NOW, "NOW" },
1833 : : { DF_1_GLOBAL, "GLOBAL" },
1834 : : { DF_1_GROUP, "GROUP" },
1835 : : { DF_1_NODELETE, "NODELETE" },
1836 : : { DF_1_LOADFLTR, "LOADFLTR" },
1837 : : { DF_1_INITFIRST, "INITFIRST" },
1838 : : { DF_1_NOOPEN, "NOOPEN" },
1839 : : { DF_1_ORIGIN, "ORIGIN" },
1840 : : { DF_1_DIRECT, "DIRECT" },
1841 : : { DF_1_TRANS, "TRANS" },
1842 : : { DF_1_INTERPOSE, "INTERPOSE" },
1843 : : { DF_1_NODEFLIB, "NODEFLIB" },
1844 : : { DF_1_NODUMP, "NODUMP" },
1845 : : { DF_1_CONFALT, "CONFALT" },
1846 : : { DF_1_ENDFILTEE, "ENDFILTEE" },
1847 : : { DF_1_DISPRELDNE, "DISPRELDNE" },
1848 : : { DF_1_DISPRELPND, "DISPRELPND" },
1849 : : };
1850 : : static const int ndt_flags_1 = sizeof (dt_flags_1) / sizeof (dt_flags_1[0]);
1851 : :
1852 : : static const struct flags dt_feature_1[] =
1853 : : {
1854 : : { DTF_1_PARINIT, "PARINIT" },
1855 : : { DTF_1_CONFEXP, "CONFEXP" }
1856 : : };
1857 : : static const int ndt_feature_1 = (sizeof (dt_feature_1)
1858 : : / sizeof (dt_feature_1[0]));
1859 : :
1860 : : static const struct flags dt_posflag_1[] =
1861 : : {
1862 : : { DF_P1_LAZYLOAD, "LAZYLOAD" },
1863 : : { DF_P1_GROUPPERM, "GROUPPERM" }
1864 : : };
1865 : : static const int ndt_posflag_1 = (sizeof (dt_posflag_1)
1866 : : / sizeof (dt_posflag_1[0]));
1867 : :
1868 : :
1869 : : static void
1870 : 74 : print_flags (int class, GElf_Xword d_val, const struct flags *flags,
1871 : : int nflags)
1872 : : {
1873 : 74 : bool first = true;
1874 : 74 : int cnt;
1875 : :
1876 [ + + ]: 1104 : for (cnt = 0; cnt < nflags; ++cnt)
1877 [ + + ]: 1030 : if (d_val & flags[cnt].mask)
1878 : : {
1879 [ + + ]: 68 : if (!first)
1880 : 36 : putchar (' ');
1881 : 68 : fputs (flags[cnt].str, stdout);
1882 : 68 : d_val &= ~flags[cnt].mask;
1883 : 68 : first = false;
1884 : : }
1885 : :
1886 [ + + ]: 74 : if (d_val != 0)
1887 : : {
1888 [ + + ]: 62 : if (!first)
1889 : 20 : putchar (' ');
1890 [ + + ]: 116 : printf ("%#0*" PRIx64, class == ELFCLASS32 ? 10 : 18, d_val);
1891 : : }
1892 : :
1893 : 74 : putchar ('\n');
1894 : 74 : }
1895 : :
1896 : :
1897 : : static void
1898 : 14 : print_dt_flags (int class, GElf_Xword d_val)
1899 : : {
1900 : 14 : print_flags (class, d_val, dt_flags, ndt_flags);
1901 : 14 : }
1902 : :
1903 : :
1904 : : static void
1905 : 56 : print_dt_flags_1 (int class, GElf_Xword d_val)
1906 : : {
1907 : 56 : print_flags (class, d_val, dt_flags_1, ndt_flags_1);
1908 : 56 : }
1909 : :
1910 : :
1911 : : static void
1912 : 2 : print_dt_feature_1 (int class, GElf_Xword d_val)
1913 : : {
1914 : 2 : print_flags (class, d_val, dt_feature_1, ndt_feature_1);
1915 : 2 : }
1916 : :
1917 : :
1918 : : static void
1919 : 2 : print_dt_posflag_1 (int class, GElf_Xword d_val)
1920 : : {
1921 : 2 : print_flags (class, d_val, dt_posflag_1, ndt_posflag_1);
1922 : 2 : }
1923 : :
1924 : :
1925 : : static size_t
1926 : 136 : get_dyn_ents (Elf_Data * dyn_data)
1927 : : {
1928 : 136 : GElf_Dyn *dyn;
1929 : 136 : GElf_Dyn dyn_mem;
1930 : 136 : size_t dyn_idx = 0;
1931 : 3414 : do
1932 : : {
1933 : 3414 : dyn = gelf_getdyn(dyn_data, dyn_idx, &dyn_mem);
1934 [ + - ]: 3414 : if (dyn != NULL)
1935 : 3414 : ++dyn_idx;
1936 : : }
1937 [ + + ]: 3414 : while (dyn != NULL && dyn->d_tag != DT_NULL);
1938 : :
1939 : 136 : return dyn_idx;
1940 : : }
1941 : :
1942 : :
1943 : : static void
1944 : 136 : handle_dynamic (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr, GElf_Phdr *phdr)
1945 : : {
1946 : 136 : int class = gelf_getclass (ebl->elf);
1947 : 136 : GElf_Shdr glink_mem;
1948 : 136 : GElf_Shdr *glink;
1949 : 136 : Elf_Data *data;
1950 : 136 : size_t cnt;
1951 : 136 : size_t shstrndx;
1952 : 136 : size_t dyn_ents;
1953 : :
1954 : : /* Get the data of the section. */
1955 [ + + + - ]: 136 : if (use_dynamic_segment && phdr != NULL)
1956 : 2 : data = elf_getdata_rawchunk(ebl->elf, phdr->p_offset,
1957 : : phdr->p_filesz, ELF_T_DYN);
1958 : : else
1959 : 134 : data = elf_getdata (scn, NULL);
1960 : :
1961 [ - + ]: 136 : if (data == NULL)
1962 : 0 : return;
1963 : :
1964 : : /* Get the dynamic section entry number */
1965 : 136 : dyn_ents = get_dyn_ents (data);
1966 : :
1967 [ + + + - ]: 136 : if (!use_dynamic_segment && shdr != NULL)
1968 : : {
1969 : : /* Get the section header string table index. */
1970 [ - + ]: 134 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
1971 : 0 : error_exit (0, _("cannot get section header string table index"));
1972 : :
1973 : 134 : glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link), &glink_mem);
1974 [ - + ]: 134 : if (glink == NULL)
1975 : 0 : error_exit (0, _("invalid sh_link value in section %zu"),
1976 : : elf_ndxscn (scn));
1977 : :
1978 : 134 : printf (ngettext ("\
1979 : : \nDynamic segment contains %lu entry:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'\n",
1980 : : "\
1981 : : \nDynamic segment contains %lu entries:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'\n",
1982 : : dyn_ents),
1983 : : (unsigned long int) dyn_ents,
1984 : : class == ELFCLASS32 ? 10 : 18, shdr->sh_addr,
1985 : : shdr->sh_offset,
1986 [ + + ]: 134 : (int) shdr->sh_link,
1987 : 134 : elf_strptr (ebl->elf, shstrndx, glink->sh_name));
1988 : : }
1989 [ + - ]: 2 : else if (phdr != NULL)
1990 : : {
1991 [ + - ]: 4 : printf (ngettext ("\
1992 : : \nDynamic segment contains %lu entry:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 "\n",
1993 : : "\
1994 : : \nDynamic segment contains %lu entries:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 "\n",
1995 : : dyn_ents),
1996 : : (unsigned long int) dyn_ents,
1997 : : class == ELFCLASS32 ? 10 : 18, phdr->p_paddr,
1998 : : phdr->p_offset);
1999 : : }
2000 : :
2001 : 136 : fputs (_(" Type Value\n"), stdout);
2002 : :
2003 : : /* if --use-dynamic option is enabled,
2004 : : use the string table to get the related library info. */
2005 : 136 : Elf_Data *strtab_data = NULL;
2006 [ + + + - ]: 136 : if (use_dynamic_segment && phdr != NULL)
2007 : : {
2008 : 2 : strtab_data = get_dynscn_strtab(ebl->elf, phdr);
2009 [ - + ]: 2 : if (strtab_data == NULL)
2010 : 0 : error_exit (0, _("cannot get string table by using dynamic segment"));
2011 : : }
2012 : :
2013 [ + + ]: 3550 : for (cnt = 0; cnt < dyn_ents; ++cnt)
2014 : : {
2015 : 3414 : GElf_Dyn dynmem;
2016 : 3414 : GElf_Dyn *dyn = gelf_getdyn (data, cnt, &dynmem);
2017 [ + - ]: 3414 : if (dyn == NULL)
2018 : : break;
2019 : :
2020 : 3414 : char buf[64];
2021 : 3414 : printf (" %-17s ",
2022 : : ebl_dynamic_tag_name (ebl, dyn->d_tag, buf, sizeof (buf)));
2023 : :
2024 : 3414 : char *name = NULL;
2025 : 3414 : if (dyn->d_tag == DT_NEEDED
2026 [ + + ]: 3414 : || dyn->d_tag == DT_SONAME
2027 : : || dyn->d_tag == DT_RPATH
2028 : : || dyn->d_tag == DT_RUNPATH)
2029 : : {
2030 [ + + + - ]: 236 : if (! use_dynamic_segment && shdr != NULL)
2031 : 232 : name = elf_strptr (ebl->elf, shdr->sh_link, dyn->d_un.d_val);
2032 [ - + ]: 4 : else if (dyn->d_un.d_val < strtab_data->d_size
2033 : 4 : && memrchr (strtab_data->d_buf + dyn->d_un.d_val, '\0',
2034 [ - + ]: 4 : strtab_data->d_size - 1 - dyn->d_un.d_val) != NULL)
2035 : 3414 : name = ((char *) strtab_data->d_buf) + dyn->d_un.d_val;
2036 : : }
2037 : :
2038 [ + + + + : 3414 : switch (dyn->d_tag)
+ + + + +
+ + + + ]
2039 : : {
2040 : 268 : case DT_NULL:
2041 : : case DT_DEBUG:
2042 : : case DT_BIND_NOW:
2043 : : case DT_TEXTREL:
2044 : : /* No further output. */
2045 : 268 : fputc ('\n', stdout);
2046 : 268 : break;
2047 : :
2048 : 226 : case DT_NEEDED:
2049 : 226 : printf (_("Shared library: [%s]\n"), name);
2050 : 226 : break;
2051 : :
2052 : 6 : case DT_SONAME:
2053 : 6 : printf (_("Library soname: [%s]\n"), name);
2054 : 6 : break;
2055 : :
2056 : 2 : case DT_RPATH:
2057 : 2 : printf (_("Library rpath: [%s]\n"), name);
2058 : 2 : break;
2059 : :
2060 : 2 : case DT_RUNPATH:
2061 : 2 : printf (_("Library runpath: [%s]\n"), name);
2062 : 2 : break;
2063 : :
2064 : 902 : case DT_PLTRELSZ:
2065 : : case DT_RELASZ:
2066 : : case DT_STRSZ:
2067 : : case DT_RELSZ:
2068 : : case DT_RELRSZ:
2069 : : case DT_RELAENT:
2070 : : case DT_SYMENT:
2071 : : case DT_RELENT:
2072 : : case DT_RELRENT:
2073 : : case DT_PLTPADSZ:
2074 : : case DT_MOVEENT:
2075 : : case DT_MOVESZ:
2076 : : case DT_INIT_ARRAYSZ:
2077 : : case DT_FINI_ARRAYSZ:
2078 : : case DT_SYMINSZ:
2079 : : case DT_SYMINENT:
2080 : : case DT_GNU_CONFLICTSZ:
2081 : : case DT_GNU_LIBLISTSZ:
2082 : 902 : printf (_("%" PRId64 " (bytes)\n"), dyn->d_un.d_val);
2083 : 902 : break;
2084 : :
2085 : 212 : case DT_VERDEFNUM:
2086 : : case DT_VERNEEDNUM:
2087 : : case DT_RELACOUNT:
2088 : : case DT_RELCOUNT:
2089 : 212 : printf ("%" PRId64 "\n", dyn->d_un.d_val);
2090 : 212 : break;
2091 : :
2092 : 108 : case DT_PLTREL:;
2093 : 108 : const char *tagname = ebl_dynamic_tag_name (ebl, dyn->d_un.d_val,
2094 : : NULL, 0);
2095 [ + + ]: 108 : puts (tagname ?: "???");
2096 : 108 : break;
2097 : :
2098 : 14 : case DT_FLAGS:
2099 : 14 : print_dt_flags (class, dyn->d_un.d_val);
2100 : 14 : break;
2101 : :
2102 : 56 : case DT_FLAGS_1:
2103 : 56 : print_dt_flags_1 (class, dyn->d_un.d_val);
2104 : 56 : break;
2105 : :
2106 : 2 : case DT_FEATURE_1:
2107 : 2 : print_dt_feature_1 (class, dyn->d_un.d_val);
2108 : 2 : break;
2109 : :
2110 : 2 : case DT_POSFLAG_1:
2111 : 2 : print_dt_posflag_1 (class, dyn->d_un.d_val);
2112 : 2 : break;
2113 : :
2114 : 1614 : default:
2115 [ + + ]: 1614 : printf ("%#0*" PRIx64 "\n",
2116 : : class == ELFCLASS32 ? 10 : 18, dyn->d_un.d_val);
2117 : 1614 : break;
2118 : : }
2119 : : }
2120 : : }
2121 : :
2122 : :
2123 : : /* Print the dynamic segment. */
2124 : : static void
2125 : 190 : print_dynamic (Ebl *ebl)
2126 : : {
2127 [ + + ]: 1072 : for (size_t i = 0; i < phnum; ++i)
2128 : : {
2129 : 1060 : GElf_Phdr phdr_mem;
2130 : 1060 : GElf_Phdr *phdr = gelf_getphdr (ebl->elf, i, &phdr_mem);
2131 : :
2132 [ + - + + ]: 1060 : if (phdr != NULL && phdr->p_type == PT_DYNAMIC)
2133 : : {
2134 : 178 : Elf_Scn *scn = gelf_offscn (ebl->elf, phdr->p_offset);
2135 : 178 : GElf_Shdr shdr_mem;
2136 : 178 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
2137 [ + + ]: 178 : if ((use_dynamic_segment && phdr != NULL)
2138 [ + + + + ]: 176 : || (shdr != NULL && shdr->sh_type == SHT_DYNAMIC))
2139 : 136 : handle_dynamic (ebl, scn, shdr, phdr);
2140 : 178 : break;
2141 : : }
2142 : : }
2143 : 190 : }
2144 : :
2145 : :
2146 : : /* Print relocations. */
2147 : : static void
2148 : 194 : print_relocs (Ebl *ebl, Dwfl_Module *mod, GElf_Ehdr *ehdr)
2149 : : {
2150 : : /* Find all relocation sections and handle them. */
2151 : 194 : Elf_Scn *scn = NULL;
2152 : :
2153 [ + + ]: 530466 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
2154 : : {
2155 : : /* Handle the section if it is a symbol table. */
2156 : 530272 : GElf_Shdr shdr_mem;
2157 : 530272 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
2158 : :
2159 [ + - ]: 530272 : if (likely (shdr != NULL))
2160 : : {
2161 [ + + ]: 530272 : if (shdr->sh_type == SHT_REL)
2162 : 92 : handle_relocs_rel (ebl, ehdr, scn, shdr);
2163 [ + + ]: 530180 : else if (shdr->sh_type == SHT_RELA)
2164 : 282 : handle_relocs_rela (ebl, ehdr, scn, shdr);
2165 [ - + ]: 529898 : else if (shdr->sh_type == SHT_RELR)
2166 : 0 : handle_relocs_relr (ebl, mod, scn, shdr);
2167 : : }
2168 : : }
2169 : 194 : }
2170 : :
2171 : :
2172 : : /* Handle a relocation section. */
2173 : : static void
2174 : 92 : handle_relocs_rel (Ebl *ebl, GElf_Ehdr *ehdr, Elf_Scn *scn, GElf_Shdr *shdr)
2175 : : {
2176 : 92 : int class = gelf_getclass (ebl->elf);
2177 : 92 : size_t sh_entsize = gelf_fsize (ebl->elf, ELF_T_REL, 1, EV_CURRENT);
2178 : 92 : int nentries = shdr->sh_size / sh_entsize;
2179 : :
2180 : : /* Get the data of the section. */
2181 : 92 : Elf_Data *data = elf_getdata (scn, NULL);
2182 [ + - ]: 92 : if (data == NULL)
2183 : 0 : return;
2184 : :
2185 : : /* Get the symbol table information. */
2186 : 92 : Elf_Scn *symscn = elf_getscn (ebl->elf, shdr->sh_link);
2187 : 92 : GElf_Shdr symshdr_mem;
2188 : 92 : GElf_Shdr *symshdr = gelf_getshdr (symscn, &symshdr_mem);
2189 : 92 : Elf_Data *symdata = elf_getdata (symscn, NULL);
2190 : :
2191 : : /* Get the section header of the section the relocations are for. */
2192 : 92 : GElf_Shdr destshdr_mem;
2193 : 92 : GElf_Shdr *destshdr = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_info),
2194 : : &destshdr_mem);
2195 : :
2196 [ + - - + ]: 92 : if (unlikely (symshdr == NULL || symdata == NULL || destshdr == NULL))
2197 : : {
2198 : 0 : printf (_("\nInvalid symbol table at offset %#0" PRIx64 "\n"),
2199 : : shdr->sh_offset);
2200 : 0 : return;
2201 : : }
2202 : :
2203 : : /* Search for the optional extended section index table if there are
2204 : : more than 64k sections. */
2205 : 92 : Elf_Data *xndxdata = NULL;
2206 [ - + ]: 92 : int xndxscnidx = shnum >= SHN_LORESERVE ? elf_scnshndx (symscn) : 0;
2207 [ # # ]: 0 : if (unlikely (xndxscnidx > 0))
2208 : 0 : xndxdata = elf_getdata (elf_getscn (ebl->elf, xndxscnidx), NULL);
2209 : :
2210 : : /* Get the section header string table index. */
2211 : 92 : size_t shstrndx;
2212 [ - + ]: 92 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
2213 : 0 : error_exit (0, _("cannot get section header string table index"));
2214 : :
2215 [ + + ]: 92 : if (shdr->sh_info != 0)
2216 : 76 : printf (ngettext ("\
2217 : : \nRelocation section [%2zu] '%s' for section [%2u] '%s' at offset %#0" PRIx64 " contains %d entry:\n",
2218 : : "\
2219 : : \nRelocation section [%2zu] '%s' for section [%2u] '%s' at offset %#0" PRIx64 " contains %d entries:\n",
2220 : : nentries),
2221 : : elf_ndxscn (scn),
2222 : 76 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
2223 : 76 : (unsigned int) shdr->sh_info,
2224 : 76 : elf_strptr (ebl->elf, shstrndx, destshdr->sh_name),
2225 : : shdr->sh_offset,
2226 : : nentries);
2227 : : else
2228 : : /* The .rel.dyn section does not refer to a specific section but
2229 : : instead of section index zero. Do not try to print a section
2230 : : name. */
2231 : 16 : printf (ngettext ("\
2232 : : \nRelocation section [%2u] '%s' at offset %#0" PRIx64 " contains %d entry:\n",
2233 : : "\
2234 : : \nRelocation section [%2u] '%s' at offset %#0" PRIx64 " contains %d entries:\n",
2235 : : nentries),
2236 : 16 : (unsigned int) elf_ndxscn (scn),
2237 : 16 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
2238 : : shdr->sh_offset,
2239 : : nentries);
2240 [ + - ]: 92 : fputs (class == ELFCLASS32
2241 : 92 : ? _("\
2242 : : Offset Type Value Name\n")
2243 : 0 : : _("\
2244 : : Offset Type Value Name\n"),
2245 : : stdout);
2246 : :
2247 : 92 : int is_statically_linked = 0;
2248 [ + + ]: 25942 : for (int cnt = 0; cnt < nentries; ++cnt)
2249 : : {
2250 : 25850 : GElf_Rel relmem;
2251 : 25850 : GElf_Rel *rel = gelf_getrel (data, cnt, &relmem);
2252 [ + - ]: 25850 : if (likely (rel != NULL))
2253 : : {
2254 : 25850 : char buf[128];
2255 : 25850 : GElf_Sym symmem;
2256 : 25850 : Elf32_Word xndx;
2257 : 51700 : GElf_Sym *sym = gelf_getsymshndx (symdata, xndxdata,
2258 : 25850 : GELF_R_SYM (rel->r_info),
2259 : : &symmem, &xndx);
2260 [ - + ]: 25850 : if (unlikely (sym == NULL))
2261 : : {
2262 : : /* As a special case we have to handle relocations in static
2263 : : executables. This only happens for IRELATIVE relocations
2264 : : (so far). There is no symbol table. */
2265 [ # # ]: 0 : if (is_statically_linked == 0)
2266 : : {
2267 : : /* Find the program header and look for a PT_INTERP entry. */
2268 : 0 : is_statically_linked = -1;
2269 [ # # ]: 0 : if (ehdr->e_type == ET_EXEC)
2270 : : {
2271 : 0 : is_statically_linked = 1;
2272 : :
2273 [ # # ]: 0 : for (size_t inner = 0; inner < phnum; ++inner)
2274 : : {
2275 : 0 : GElf_Phdr phdr_mem;
2276 : 0 : GElf_Phdr *phdr = gelf_getphdr (ebl->elf, inner,
2277 : : &phdr_mem);
2278 [ # # # # ]: 0 : if (phdr != NULL && phdr->p_type == PT_INTERP)
2279 : : {
2280 : 0 : is_statically_linked = -1;
2281 : 0 : break;
2282 : : }
2283 : : }
2284 : : }
2285 : : }
2286 : :
2287 [ # # # # ]: 0 : if (is_statically_linked > 0 && shdr->sh_link == 0)
2288 [ # # # # ]: 0 : printf ("\
2289 : : %#0*" PRIx64 " %-20s %*s %s\n",
2290 : : class == ELFCLASS32 ? 10 : 18, rel->r_offset,
2291 : 0 : ebl_reloc_type_check (ebl, GELF_R_TYPE (rel->r_info))
2292 : : /* Avoid the leading R_ which isn't carrying any
2293 : : information. */
2294 : 0 : ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info),
2295 : : buf, sizeof (buf)) + 2
2296 : 0 : : _("<INVALID RELOC>"),
2297 : : class == ELFCLASS32 ? 10 : 18, "",
2298 : 0 : elf_strptr (ebl->elf, shstrndx, destshdr->sh_name));
2299 : : else
2300 [ # # # # ]: 0 : printf (" %#0*" PRIx64 " %-20s <%s %ld>\n",
2301 : : class == ELFCLASS32 ? 10 : 18, rel->r_offset,
2302 : 0 : ebl_reloc_type_check (ebl, GELF_R_TYPE (rel->r_info))
2303 : : /* Avoid the leading R_ which isn't carrying any
2304 : : information. */
2305 : 0 : ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info),
2306 : : buf, sizeof (buf)) + 2
2307 : 0 : : _("<INVALID RELOC>"),
2308 : : _("INVALID SYMBOL"),
2309 : 0 : (long int) GELF_R_SYM (rel->r_info));
2310 : : }
2311 [ + + ]: 25850 : else if (GELF_ST_TYPE (sym->st_info) != STT_SECTION
2312 [ + + ]: 130 : && !(GELF_ST_TYPE (sym->st_info) == STT_NOTYPE
2313 [ - + ]: 66 : && GELF_ST_BIND (sym->st_info) == STB_LOCAL
2314 [ # # ]: 0 : && sym->st_shndx != SHN_UNDEF
2315 [ # # ]: 0 : && sym->st_value == 0)) // local start section label
2316 [ - + ]: 260 : printf (" %#0*" PRIx64 " %-20s %#0*" PRIx64 " %s\n",
2317 : : class == ELFCLASS32 ? 10 : 18, rel->r_offset,
2318 [ + - ]: 130 : likely (ebl_reloc_type_check (ebl,
2319 : : GELF_R_TYPE (rel->r_info)))
2320 : : /* Avoid the leading R_ which isn't carrying any
2321 : : information. */
2322 : 130 : ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info),
2323 : : buf, sizeof (buf)) + 2
2324 : 0 : : _("<INVALID RELOC>"),
2325 : : class == ELFCLASS32 ? 10 : 18, sym->st_value,
2326 : 130 : elf_strptr (ebl->elf, symshdr->sh_link, sym->st_name));
2327 : : else
2328 : : {
2329 : : /* This is a relocation against a STT_SECTION symbol
2330 : : or a local start section label for which we print
2331 : : section name. */
2332 : 25720 : GElf_Shdr secshdr_mem;
2333 : 25720 : GElf_Shdr *secshdr;
2334 : 25720 : secshdr = gelf_getshdr (elf_getscn (ebl->elf,
2335 [ + - ]: 25720 : sym->st_shndx == SHN_XINDEX
2336 : 0 : ? xndx : sym->st_shndx),
2337 : : &secshdr_mem);
2338 : :
2339 [ - + ]: 25720 : if (unlikely (secshdr == NULL))
2340 [ # # # # ]: 0 : printf (" %#0*" PRIx64 " %-20s <%s %ld>\n",
2341 : : class == ELFCLASS32 ? 10 : 18, rel->r_offset,
2342 : 0 : ebl_reloc_type_check (ebl, GELF_R_TYPE (rel->r_info))
2343 : : /* Avoid the leading R_ which isn't carrying any
2344 : : information. */
2345 : 0 : ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info),
2346 : : buf, sizeof (buf)) + 2
2347 : 0 : : _("<INVALID RELOC>"),
2348 : : _("INVALID SECTION"),
2349 [ # # ]: 0 : (long int) (sym->st_shndx == SHN_XINDEX
2350 : 0 : ? xndx : sym->st_shndx));
2351 : : else
2352 [ - + + - ]: 77160 : printf (" %#0*" PRIx64 " %-20s %#0*" PRIx64 " %s\n",
2353 : : class == ELFCLASS32 ? 10 : 18, rel->r_offset,
2354 : 25720 : ebl_reloc_type_check (ebl, GELF_R_TYPE (rel->r_info))
2355 : : /* Avoid the leading R_ which isn't carrying any
2356 : : information. */
2357 : 25720 : ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info),
2358 : : buf, sizeof (buf)) + 2
2359 : 0 : : _("<INVALID RELOC>"),
2360 : : class == ELFCLASS32 ? 10 : 18, sym->st_value,
2361 : 25720 : elf_strptr (ebl->elf, shstrndx, secshdr->sh_name));
2362 : : }
2363 : : }
2364 : : }
2365 : : }
2366 : :
2367 : :
2368 : : /* Handle a relocation section. */
2369 : : static void
2370 : 282 : handle_relocs_rela (Ebl *ebl, GElf_Ehdr *ehdr, Elf_Scn *scn, GElf_Shdr *shdr)
2371 : : {
2372 : 282 : int class = gelf_getclass (ebl->elf);
2373 : 282 : size_t sh_entsize = gelf_fsize (ebl->elf, ELF_T_RELA, 1, EV_CURRENT);
2374 : 282 : int nentries = shdr->sh_size / sh_entsize;
2375 : :
2376 : : /* Get the data of the section. */
2377 : 282 : Elf_Data *data = elf_getdata (scn, NULL);
2378 [ + - ]: 282 : if (data == NULL)
2379 : 0 : return;
2380 : :
2381 : : /* Get the symbol table information. */
2382 : 282 : Elf_Scn *symscn = elf_getscn (ebl->elf, shdr->sh_link);
2383 : 282 : GElf_Shdr symshdr_mem;
2384 : 282 : GElf_Shdr *symshdr = gelf_getshdr (symscn, &symshdr_mem);
2385 : 282 : Elf_Data *symdata = elf_getdata (symscn, NULL);
2386 : :
2387 : : /* Get the section header of the section the relocations are for. */
2388 : 282 : GElf_Shdr destshdr_mem;
2389 : 282 : GElf_Shdr *destshdr = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_info),
2390 : : &destshdr_mem);
2391 : :
2392 [ + - - + ]: 282 : if (unlikely (symshdr == NULL || symdata == NULL || destshdr == NULL))
2393 : : {
2394 : 0 : printf (_("\nInvalid symbol table at offset %#0" PRIx64 "\n"),
2395 : : shdr->sh_offset);
2396 : 0 : return;
2397 : : }
2398 : :
2399 : : /* Search for the optional extended section index table if there are
2400 : : more than 64k sections. */
2401 : 282 : Elf_Data *xndxdata = NULL;
2402 [ + + ]: 282 : int xndxscnidx = shnum >= SHN_LORESERVE ? elf_scnshndx (symscn) : 0;
2403 [ - + ]: 16 : if (unlikely (xndxscnidx > 0))
2404 : 16 : xndxdata = elf_getdata (elf_getscn (ebl->elf, xndxscnidx), NULL);
2405 : :
2406 : : /* Get the section header string table index. */
2407 : 282 : size_t shstrndx;
2408 [ - + ]: 282 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
2409 : 0 : error_exit (0, _("cannot get section header string table index"));
2410 : :
2411 [ + + ]: 282 : if (shdr->sh_info != 0)
2412 : 168 : printf (ngettext ("\
2413 : : \nRelocation section [%2zu] '%s' for section [%2u] '%s' at offset %#0" PRIx64 " contains %d entry:\n",
2414 : : "\
2415 : : \nRelocation section [%2zu] '%s' for section [%2u] '%s' at offset %#0" PRIx64 " contains %d entries:\n",
2416 : : nentries),
2417 : : elf_ndxscn (scn),
2418 : 168 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
2419 : 168 : (unsigned int) shdr->sh_info,
2420 : 168 : elf_strptr (ebl->elf, shstrndx, destshdr->sh_name),
2421 : : shdr->sh_offset,
2422 : : nentries);
2423 : : else
2424 : : /* The .rela.dyn section does not refer to a specific section but
2425 : : instead of section index zero. Do not try to print a section
2426 : : name. */
2427 : 114 : printf (ngettext ("\
2428 : : \nRelocation section [%2u] '%s' at offset %#0" PRIx64 " contains %d entry:\n",
2429 : : "\
2430 : : \nRelocation section [%2u] '%s' at offset %#0" PRIx64 " contains %d entries:\n",
2431 : : nentries),
2432 : 114 : (unsigned int) elf_ndxscn (scn),
2433 : 114 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
2434 : : shdr->sh_offset,
2435 : : nentries);
2436 [ - + ]: 282 : fputs (class == ELFCLASS32
2437 : 0 : ? _("\
2438 : : Offset Type Value Addend Name\n")
2439 : 282 : : _("\
2440 : : Offset Type Value Addend Name\n"),
2441 : : stdout);
2442 : :
2443 : 282 : int is_statically_linked = 0;
2444 [ + + ]: 1168710 : for (int cnt = 0; cnt < nentries; ++cnt)
2445 : : {
2446 : 1168428 : GElf_Rela relmem;
2447 : 1168428 : GElf_Rela *rel = gelf_getrela (data, cnt, &relmem);
2448 [ + - ]: 1168428 : if (likely (rel != NULL))
2449 : : {
2450 : 1168428 : char buf[64];
2451 : 1168428 : GElf_Sym symmem;
2452 : 1168428 : Elf32_Word xndx;
2453 : 2336856 : GElf_Sym *sym = gelf_getsymshndx (symdata, xndxdata,
2454 : 1168428 : GELF_R_SYM (rel->r_info),
2455 : : &symmem, &xndx);
2456 : :
2457 [ - + ]: 1168428 : if (unlikely (sym == NULL))
2458 : : {
2459 : : /* As a special case we have to handle relocations in static
2460 : : executables. This only happens for IRELATIVE relocations
2461 : : (so far). There is no symbol table. */
2462 [ # # ]: 0 : if (is_statically_linked == 0)
2463 : : {
2464 : : /* Find the program header and look for a PT_INTERP entry. */
2465 : 0 : is_statically_linked = -1;
2466 [ # # ]: 0 : if (ehdr->e_type == ET_EXEC)
2467 : : {
2468 : 0 : is_statically_linked = 1;
2469 : :
2470 [ # # ]: 0 : for (size_t inner = 0; inner < phnum; ++inner)
2471 : : {
2472 : 0 : GElf_Phdr phdr_mem;
2473 : 0 : GElf_Phdr *phdr = gelf_getphdr (ebl->elf, inner,
2474 : : &phdr_mem);
2475 [ # # # # ]: 0 : if (phdr != NULL && phdr->p_type == PT_INTERP)
2476 : : {
2477 : 0 : is_statically_linked = -1;
2478 : 0 : break;
2479 : : }
2480 : : }
2481 : : }
2482 : : }
2483 : :
2484 [ # # # # ]: 0 : if (is_statically_linked > 0 && shdr->sh_link == 0)
2485 [ # # # # ]: 0 : printf ("\
2486 : : %#0*" PRIx64 " %-15s %*s %#6" PRIx64 " %s\n",
2487 : : class == ELFCLASS32 ? 10 : 18, rel->r_offset,
2488 : 0 : ebl_reloc_type_check (ebl, GELF_R_TYPE (rel->r_info))
2489 : : /* Avoid the leading R_ which isn't carrying any
2490 : : information. */
2491 : 0 : ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info),
2492 : : buf, sizeof (buf)) + 2
2493 : 0 : : _("<INVALID RELOC>"),
2494 : : class == ELFCLASS32 ? 10 : 18, "",
2495 : : rel->r_addend,
2496 : 0 : elf_strptr (ebl->elf, shstrndx, destshdr->sh_name));
2497 : : else
2498 [ # # # # ]: 0 : printf (" %#0*" PRIx64 " %-15s <%s %ld>\n",
2499 : : class == ELFCLASS32 ? 10 : 18, rel->r_offset,
2500 : 0 : ebl_reloc_type_check (ebl, GELF_R_TYPE (rel->r_info))
2501 : : /* Avoid the leading R_ which isn't carrying any
2502 : : information. */
2503 : 0 : ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info),
2504 : : buf, sizeof (buf)) + 2
2505 : 0 : : _("<INVALID RELOC>"),
2506 : : _("INVALID SYMBOL"),
2507 : 0 : (long int) GELF_R_SYM (rel->r_info));
2508 : : }
2509 [ + + ]: 1168428 : else if (GELF_ST_TYPE (sym->st_info) != STT_SECTION
2510 [ + + ]: 630544 : && !(GELF_ST_TYPE (sym->st_info) == STT_NOTYPE
2511 [ + + ]: 103788 : && GELF_ST_BIND (sym->st_info) == STB_LOCAL
2512 [ + + ]: 102250 : && sym->st_shndx != SHN_UNDEF
2513 [ + + ]: 716 : && sym->st_value == 0)) // local start section label
2514 [ + - ]: 1261064 : printf ("\
2515 : : %#0*" PRIx64 " %-15s %#0*" PRIx64 " %+6" PRId64 " %s\n",
2516 : : class == ELFCLASS32 ? 10 : 18, rel->r_offset,
2517 [ + - ]: 630532 : likely (ebl_reloc_type_check (ebl,
2518 : : GELF_R_TYPE (rel->r_info)))
2519 : : /* Avoid the leading R_ which isn't carrying any
2520 : : information. */
2521 : 630532 : ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info),
2522 : : buf, sizeof (buf)) + 2
2523 : 0 : : _("<INVALID RELOC>"),
2524 : : class == ELFCLASS32 ? 10 : 18, sym->st_value,
2525 : : rel->r_addend,
2526 : 630532 : elf_strptr (ebl->elf, symshdr->sh_link, sym->st_name));
2527 : : else
2528 : : {
2529 : : /* This is a relocation against a STT_SECTION symbol
2530 : : or a local start section label for which we print
2531 : : section name. */
2532 : 537896 : GElf_Shdr secshdr_mem;
2533 : 537896 : GElf_Shdr *secshdr;
2534 : 537896 : secshdr = gelf_getshdr (elf_getscn (ebl->elf,
2535 [ + + ]: 537896 : sym->st_shndx == SHN_XINDEX
2536 : 2088 : ? xndx : sym->st_shndx),
2537 : : &secshdr_mem);
2538 : :
2539 [ - + ]: 537896 : if (unlikely (secshdr == NULL))
2540 [ # # # # ]: 0 : printf (" %#0*" PRIx64 " %-15s <%s %ld>\n",
2541 : : class == ELFCLASS32 ? 10 : 18, rel->r_offset,
2542 : 0 : ebl_reloc_type_check (ebl, GELF_R_TYPE (rel->r_info))
2543 : : /* Avoid the leading R_ which isn't carrying any
2544 : : information. */
2545 : 0 : ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info),
2546 : : buf, sizeof (buf)) + 2
2547 : 0 : : _("<INVALID RELOC>"),
2548 : : _("INVALID SECTION"),
2549 [ # # ]: 0 : (long int) (sym->st_shndx == SHN_XINDEX
2550 : 0 : ? xndx : sym->st_shndx));
2551 : : else
2552 [ + - + - ]: 1613688 : printf ("\
2553 : : %#0*" PRIx64 " %-15s %#0*" PRIx64 " %+6" PRId64 " %s\n",
2554 : : class == ELFCLASS32 ? 10 : 18, rel->r_offset,
2555 : 537896 : ebl_reloc_type_check (ebl, GELF_R_TYPE (rel->r_info))
2556 : : /* Avoid the leading R_ which isn't carrying any
2557 : : information. */
2558 : 537896 : ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info),
2559 : : buf, sizeof (buf)) + 2
2560 : 0 : : _("<INVALID RELOC>"),
2561 : : class == ELFCLASS32 ? 10 : 18, sym->st_value,
2562 : : rel->r_addend,
2563 : 537896 : elf_strptr (ebl->elf, shstrndx, secshdr->sh_name));
2564 : : }
2565 : : }
2566 : : }
2567 : : }
2568 : :
2569 : : /* Handle a relocation section. */
2570 : : static void
2571 : 0 : handle_relocs_relr (Ebl *ebl, Dwfl_Module *mod, Elf_Scn *scn, GElf_Shdr *shdr)
2572 : : {
2573 : 0 : int class = gelf_getclass (ebl->elf);
2574 : 0 : size_t sh_entsize = gelf_fsize (ebl->elf, ELF_T_RELR, 1, EV_CURRENT);
2575 : 0 : int nentries = shdr->sh_size / sh_entsize;
2576 : :
2577 : : /* Get the data of the section. */
2578 : 0 : Elf_Data *data = elf_getdata (scn, NULL);
2579 [ # # ]: 0 : if (data == NULL)
2580 : 0 : return;
2581 : :
2582 : : /* Get the section header string table index. */
2583 : 0 : size_t shstrndx;
2584 [ # # ]: 0 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
2585 : 0 : error_exit (0, _("cannot get section header string table index"));
2586 : :
2587 : : /* A .relr.dyn section does not refer to a specific section. */
2588 : 0 : printf (ngettext ("\
2589 : : \nRelocation section [%2u] '%s' at offset %#0" PRIx64 " contains %d entry:\n",
2590 : : "\
2591 : : \nRelocation section [%2u] '%s' at offset %#0" PRIx64 " contains %d entries:\n",
2592 : : nentries),
2593 : 0 : (unsigned int) elf_ndxscn (scn),
2594 : 0 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
2595 : : shdr->sh_offset,
2596 : : nentries);
2597 : :
2598 [ # # ]: 0 : if (class == ELFCLASS32)
2599 : : {
2600 : : uint32_t base = 0;
2601 [ # # ]: 0 : for (int cnt = 0; cnt < nentries; ++cnt)
2602 : : {
2603 : 0 : Elf32_Word *words = data->d_buf;
2604 : 0 : Elf32_Word entry = words[cnt];
2605 : :
2606 : : /* Just the raw entries? */
2607 [ # # ]: 0 : if (print_unresolved_addresses)
2608 : 0 : printf (" %#010" PRIx32 "%s\n", entry,
2609 [ # # ]: 0 : (entry & 1) == 0 ? " *" : "");
2610 : : else
2611 : : {
2612 : : /* A real address, also sets base. */
2613 [ # # ]: 0 : if ((entry & 1) == 0)
2614 : : {
2615 : 0 : printf (" ");
2616 : 0 : print_dwarf_addr (mod, 4, entry, entry, stdout);
2617 : 0 : printf (" *\n");
2618 : :
2619 : 0 : base = entry + 4;
2620 : : }
2621 : : else
2622 : : {
2623 : : /* Untangle address from base and bits. */
2624 : : uint32_t addr;
2625 [ # # ]: 0 : for (addr = base; (entry >>= 1) != 0; addr += 4)
2626 [ # # ]: 0 : if ((entry & 1) != 0)
2627 : : {
2628 : 0 : printf (" ");
2629 : 0 : print_dwarf_addr (mod, 4, addr, addr, stdout);
2630 : 0 : printf ("\n");
2631 : : }
2632 : 0 : base += 4 * (4 * 8 - 1);
2633 : : }
2634 : : }
2635 : : }
2636 : : }
2637 : : else
2638 : : {
2639 : : uint64_t base = 0;
2640 [ # # ]: 0 : for (int cnt = 0; cnt < nentries; ++cnt)
2641 : : {
2642 : 0 : Elf64_Xword *xwords = data->d_buf;
2643 : 0 : Elf64_Xword entry = xwords[cnt];
2644 : :
2645 : : /* Just the raw entries? */
2646 [ # # ]: 0 : if (print_unresolved_addresses)
2647 : 0 : printf (" %#018" PRIx64 "%s\n", entry,
2648 [ # # ]: 0 : (entry & 1) == 0 ? " *" : "");
2649 : : else
2650 : : {
2651 : : /* A real address, also sets base. */
2652 [ # # ]: 0 : if ((entry & 1) == 0)
2653 : : {
2654 : 0 : printf (" ");
2655 : 0 : print_dwarf_addr (mod, 8, entry, entry, stdout);
2656 : 0 : printf (" *\n");
2657 : :
2658 : 0 : base = entry + 8;
2659 : : }
2660 : : else
2661 : : {
2662 : : /* Untangle address from base and bits. */
2663 : : uint64_t addr;
2664 [ # # ]: 0 : for (addr = base; (entry >>= 1) != 0; addr += 8)
2665 [ # # ]: 0 : if ((entry & 1) != 0)
2666 : : {
2667 : 0 : printf (" ");
2668 : 0 : print_dwarf_addr (mod, 8, addr, addr, stdout);
2669 : 0 : printf ("\n");
2670 : : }
2671 : 0 : base += 8 * (8 * 8 - 1);
2672 : : }
2673 : : }
2674 : : }
2675 : : }
2676 : : }
2677 : :
2678 : : /* Print the program header. Return true if a symtab is printed,
2679 : : false otherwise. */
2680 : : static bool
2681 : 438 : print_symtab (Ebl *ebl, int type)
2682 : : {
2683 : : /* Use the dynamic section info to display symbol tables. */
2684 [ + - - - ]: 438 : if (use_dynamic_segment && type == SHT_DYNSYM)
2685 : 0 : return handle_dynamic_symtab(ebl);
2686 : :
2687 : : /* Find the symbol table(s). For this we have to search through the
2688 : : section table. */
2689 : : Elf_Scn *scn = NULL;
2690 : : bool symtab_printed = false;
2691 : :
2692 [ + + ]: 14056 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
2693 : : {
2694 : : /* Handle the section if it is a symbol table. */
2695 : 13618 : GElf_Shdr shdr_mem;
2696 : 13618 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
2697 : :
2698 [ + - + + ]: 13618 : if (shdr != NULL && shdr->sh_type == (GElf_Word) type)
2699 : : {
2700 [ + + ]: 274 : if (symbol_table_section != NULL)
2701 : : {
2702 : : /* Get the section header string table index. */
2703 : 14 : size_t shstrndx;
2704 : 14 : const char *sname;
2705 [ - + ]: 14 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
2706 : 0 : error_exit (0,
2707 : : _("cannot get section header string table index"));
2708 : 14 : sname = elf_strptr (ebl->elf, shstrndx, shdr->sh_name);
2709 [ + - + + ]: 14 : if (sname == NULL || strcmp (sname, symbol_table_section) != 0)
2710 : 8 : continue;
2711 : : }
2712 : :
2713 [ - + ]: 266 : if ((shdr->sh_flags & SHF_COMPRESSED) != 0)
2714 : : {
2715 [ # # ]: 0 : if (elf_compress (scn, 0, 0) < 0)
2716 : 0 : printf ("WARNING: %s [%zd]\n",
2717 : : _("Couldn't uncompress section"),
2718 : : elf_ndxscn (scn));
2719 : 0 : shdr = gelf_getshdr (scn, &shdr_mem);
2720 [ # # ]: 0 : if (unlikely (shdr == NULL))
2721 : 0 : error_exit (0,
2722 : : _("cannot get section [%zd] header: %s"),
2723 : : elf_ndxscn (scn), elf_errmsg (-1));
2724 : : }
2725 : 266 : symtab_printed = handle_symtab (ebl, scn, shdr);
2726 : : }
2727 : : }
2728 : :
2729 : : return symtab_printed;
2730 : : }
2731 : :
2732 : :
2733 : : static void
2734 : 266 : process_symtab (Ebl *ebl, unsigned int nsyms, Elf64_Word idx,
2735 : : Elf32_Word verneed_stridx, Elf32_Word verdef_stridx,
2736 : : Elf_Data *symdata, Elf_Data *versym_data,
2737 : : Elf_Data *symstr_data, Elf_Data *verneed_data,
2738 : : Elf_Data *verdef_data, Elf_Data *xndx_data)
2739 : : {
2740 [ + + ]: 80844 : for (unsigned int cnt = 0; cnt < nsyms; ++cnt)
2741 : : {
2742 : 80578 : char typebuf[64];
2743 : 80578 : char bindbuf[64];
2744 : 80578 : char scnbuf[64];
2745 : 80578 : const char *sym_name;
2746 : 80578 : Elf32_Word xndx;
2747 : 80578 : GElf_Sym sym_mem;
2748 : 80578 : GElf_Sym *sym
2749 : 80578 : = gelf_getsymshndx (symdata, xndx_data, cnt, &sym_mem, &xndx);
2750 : :
2751 [ - + ]: 80578 : if (unlikely (sym == NULL))
2752 : 0 : continue;
2753 : :
2754 : : /* Determine the real section index. */
2755 [ + - ]: 80578 : if (likely (sym->st_shndx != SHN_XINDEX))
2756 : 80578 : xndx = sym->st_shndx;
2757 [ - + ]: 80578 : if (use_dynamic_segment == true)
2758 : : {
2759 [ # # ]: 0 : if (validate_str (symstr_data->d_buf, sym->st_name,
2760 : : symstr_data->d_size))
2761 : 0 : sym_name = (char *)symstr_data->d_buf + sym->st_name;
2762 : : else
2763 : : sym_name = NULL;
2764 : : }
2765 : : else
2766 : 80578 : sym_name = elf_strptr (ebl->elf, idx, sym->st_name);
2767 : :
2768 [ - + ]: 80578 : if (sym_name == NULL)
2769 : : sym_name = "???";
2770 : :
2771 [ + + ]: 160268 : printf (_ ("\
2772 : : %5u: %0*" PRIx64 " %6" PRId64 " %-7s %-6s %-9s %6s %s"),
2773 : 80578 : cnt, gelf_getclass (ebl->elf) == ELFCLASS32 ? 8 : 16,
2774 : : sym->st_value, sym->st_size,
2775 : 80578 : ebl_symbol_type_name (ebl, GELF_ST_TYPE (sym->st_info), typebuf,
2776 : : sizeof (typebuf)),
2777 : 80578 : ebl_symbol_binding_name (ebl, GELF_ST_BIND (sym->st_info),
2778 : : bindbuf, sizeof (bindbuf)),
2779 : 80578 : get_visibility_type (GELF_ST_VISIBILITY (sym->st_other)),
2780 : 80578 : ebl_section_name (ebl, sym->st_shndx, xndx, scnbuf,
2781 : : sizeof (scnbuf), NULL, shnum),
2782 : : sym_name);
2783 : :
2784 [ + + ]: 80578 : if (versym_data != NULL)
2785 : : {
2786 : : /* Get the version information. */
2787 : 3436 : GElf_Versym versym_mem;
2788 : 3436 : GElf_Versym *versym = gelf_getversym (versym_data, cnt, &versym_mem);
2789 : :
2790 [ + - + + ]: 3436 : if (versym != NULL && ((*versym & 0x8000) != 0 || *versym > 1))
2791 : : {
2792 : 2476 : bool is_nobits = false;
2793 : 2476 : bool check_def = xndx != SHN_UNDEF;
2794 : :
2795 [ + + - + ]: 2476 : if (xndx < SHN_LORESERVE || sym->st_shndx == SHN_XINDEX)
2796 : : {
2797 : 2456 : GElf_Shdr symshdr_mem;
2798 : 2456 : GElf_Shdr *symshdr = gelf_getshdr (
2799 : : elf_getscn (ebl->elf, xndx), &symshdr_mem);
2800 : :
2801 : 2456 : is_nobits
2802 [ + - + + ]: 4888 : = (symshdr != NULL && symshdr->sh_type == SHT_NOBITS);
2803 : : }
2804 : :
2805 [ + + ]: 2476 : if (is_nobits || !check_def)
2806 : : {
2807 : : /* We must test both. */
2808 : 2172 : GElf_Vernaux vernaux_mem;
2809 : 2172 : GElf_Vernaux *vernaux = NULL;
2810 : 2172 : size_t vn_offset = 0;
2811 : :
2812 : 2172 : GElf_Verneed verneed_mem;
2813 : 2172 : GElf_Verneed *verneed
2814 : 2172 : = gelf_getverneed (verneed_data, 0, &verneed_mem);
2815 [ + - ]: 7902 : while (verneed != NULL)
2816 : : {
2817 : 7902 : size_t vna_offset = vn_offset;
2818 : :
2819 : 7902 : vernaux = gelf_getvernaux (verneed_data,
2820 : 7902 : vna_offset += verneed->vn_aux,
2821 : : &vernaux_mem);
2822 [ + + ]: 28762 : while (vernaux != NULL && vernaux->vna_other != *versym
2823 [ + + ]: 18688 : && vernaux->vna_next != 0
2824 [ + - ]: 20860 : && (verneed_data->d_size - vna_offset
2825 [ + - ]: 12958 : >= vernaux->vna_next))
2826 : : {
2827 : : /* Update the offset. */
2828 : 12958 : vna_offset += vernaux->vna_next;
2829 : :
2830 : 12958 : vernaux = (vernaux->vna_next == 0
2831 : : ? NULL
2832 : 12958 : : gelf_getvernaux (verneed_data,
2833 : : vna_offset,
2834 : : &vernaux_mem));
2835 : : }
2836 : :
2837 : : /* Check whether we found the version. */
2838 [ + - + + ]: 7902 : if (vernaux != NULL && vernaux->vna_other == *versym)
2839 : : /* Found it. */
2840 : : break;
2841 : :
2842 [ + - ]: 5730 : if (verneed_data->d_size - vn_offset < verneed->vn_next)
2843 : : break;
2844 : :
2845 : 5730 : vn_offset += verneed->vn_next;
2846 : 5730 : verneed
2847 : : = (verneed->vn_next == 0
2848 : : ? NULL
2849 [ + - ]: 5730 : : gelf_getverneed (verneed_data, vn_offset,
2850 : : &verneed_mem));
2851 : : }
2852 : :
2853 [ + - + - ]: 2172 : if (vernaux != NULL && vernaux->vna_other == *versym)
2854 : : {
2855 : 4344 : printf ("@%s (%u)",
2856 [ - + ]: 2172 : use_dynamic_segment == true
2857 : 0 : ? (char *)symstr_data->d_buf
2858 : 0 : + vernaux->vna_name
2859 : 2172 : : elf_strptr (ebl->elf, verneed_stridx,
2860 : 2172 : vernaux->vna_name),
2861 : : (unsigned int)vernaux->vna_other);
2862 : 2172 : check_def = 0;
2863 : : }
2864 [ # # ]: 0 : else if (unlikely (!is_nobits))
2865 : 0 : error (0, 0, _ ("bad dynamic symbol"));
2866 : : else
2867 : : check_def = 1;
2868 : : }
2869 : :
2870 [ + - + - ]: 2476 : if (check_def && *versym != 0x8001)
2871 : : {
2872 : : /* We must test both. */
2873 : 304 : size_t vd_offset = 0;
2874 : :
2875 : 304 : GElf_Verdef verdef_mem;
2876 : 304 : GElf_Verdef *verdef
2877 : 304 : = gelf_getverdef (verdef_data, 0, &verdef_mem);
2878 [ + - ]: 866 : while (verdef != NULL)
2879 : : {
2880 [ + + ]: 866 : if (verdef->vd_ndx == (*versym & 0x7fff))
2881 : : /* Found the definition. */
2882 : : break;
2883 : :
2884 [ + - ]: 562 : if (verdef_data->d_size - vd_offset < verdef->vd_next)
2885 : : break;
2886 : :
2887 : 562 : vd_offset += verdef->vd_next;
2888 : 562 : verdef = (verdef->vd_next == 0
2889 : : ? NULL
2890 [ + - ]: 562 : : gelf_getverdef (verdef_data, vd_offset,
2891 : : &verdef_mem));
2892 : : }
2893 : :
2894 [ + - ]: 304 : if (verdef != NULL)
2895 : : {
2896 : 304 : GElf_Verdaux verdaux_mem;
2897 : 608 : GElf_Verdaux *verdaux = gelf_getverdaux (
2898 : 304 : verdef_data, vd_offset + verdef->vd_aux,
2899 : : &verdaux_mem);
2900 : :
2901 [ + - ]: 304 : if (verdaux != NULL)
2902 [ + - ]: 608 : printf ((*versym & 0x8000) ? "@%s" : "@@%s",
2903 [ - + ]: 304 : use_dynamic_segment == true
2904 : 0 : ? (char *)symstr_data->d_buf
2905 : 0 : + verdaux->vda_name
2906 : 304 : : elf_strptr (ebl->elf, verdef_stridx,
2907 : 304 : verdaux->vda_name));
2908 : : }
2909 : : }
2910 : : }
2911 : : }
2912 : :
2913 : 80578 : putchar ('\n');
2914 : : }
2915 : 266 : }
2916 : :
2917 : :
2918 : : static bool
2919 : 266 : handle_symtab (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr)
2920 : : {
2921 : 266 : Elf_Data *versym_data = NULL;
2922 : 266 : Elf_Data *verneed_data = NULL;
2923 : 266 : Elf_Data *verdef_data = NULL;
2924 : 266 : Elf_Data *xndx_data = NULL;
2925 : 266 : int class = gelf_getclass (ebl->elf);
2926 : 266 : Elf32_Word verneed_stridx = 0;
2927 : 266 : Elf32_Word verdef_stridx = 0;
2928 : :
2929 : : /* Get the data of the section. */
2930 : 266 : Elf_Data *data = elf_getdata (scn, NULL);
2931 [ + - ]: 266 : if (data == NULL)
2932 : : return false;
2933 : :
2934 : : /* Find out whether we have other sections we might need. */
2935 : : Elf_Scn *runscn = NULL;
2936 [ + + ]: 8784 : while ((runscn = elf_nextscn (ebl->elf, runscn)) != NULL)
2937 : : {
2938 : 8518 : GElf_Shdr runshdr_mem;
2939 : 8518 : GElf_Shdr *runshdr = gelf_getshdr (runscn, &runshdr_mem);
2940 : :
2941 [ + - ]: 8518 : if (likely (runshdr != NULL))
2942 : : {
2943 [ + + ]: 8518 : if (runshdr->sh_type == SHT_GNU_versym
2944 [ + + ]: 202 : && runshdr->sh_link == elf_ndxscn (scn))
2945 : : /* Bingo, found the version information. Now get the data. */
2946 : 150 : versym_data = elf_getdata (runscn, NULL);
2947 [ + + ]: 8368 : else if (runshdr->sh_type == SHT_GNU_verneed)
2948 : : {
2949 : : /* This is the information about the needed versions. */
2950 : 202 : verneed_data = elf_getdata (runscn, NULL);
2951 : 202 : verneed_stridx = runshdr->sh_link;
2952 : : }
2953 [ + + ]: 8166 : else if (runshdr->sh_type == SHT_GNU_verdef)
2954 : : {
2955 : : /* This is the information about the defined versions. */
2956 : 8 : verdef_data = elf_getdata (runscn, NULL);
2957 : 8 : verdef_stridx = runshdr->sh_link;
2958 : : }
2959 [ + - ]: 8158 : else if (runshdr->sh_type == SHT_SYMTAB_SHNDX
2960 [ # # ]: 0 : && runshdr->sh_link == elf_ndxscn (scn))
2961 : : /* Extended section index. */
2962 : 0 : xndx_data = elf_getdata (runscn, NULL);
2963 : : }
2964 : : }
2965 : :
2966 : : /* Get the section header string table index. */
2967 : 266 : size_t shstrndx;
2968 [ - + ]: 266 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
2969 : 0 : error_exit (0, _("cannot get section header string table index"));
2970 : :
2971 : 266 : GElf_Shdr glink_mem;
2972 : 266 : GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
2973 : : &glink_mem);
2974 [ - + ]: 266 : if (glink == NULL)
2975 : 0 : error_exit (0, _("invalid sh_link value in section %zu"),
2976 : : elf_ndxscn (scn));
2977 : :
2978 : : /* Now we can compute the number of entries in the section. */
2979 : 532 : unsigned int nsyms = data->d_size / (class == ELFCLASS32
2980 : : ? sizeof (Elf32_Sym)
2981 [ + + ]: 266 : : sizeof (Elf64_Sym));
2982 : :
2983 : 266 : printf (ngettext ("\nSymbol table [%2u] '%s' contains %u entry:\n",
2984 : : "\nSymbol table [%2u] '%s' contains %u entries:\n",
2985 : : nsyms),
2986 : 266 : (unsigned int) elf_ndxscn (scn),
2987 : 266 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name), nsyms);
2988 : 266 : printf (ngettext (" %lu local symbol String table: [%2u] '%s'\n",
2989 : : " %lu local symbols String table: [%2u] '%s'\n",
2990 : : shdr->sh_info),
2991 : 266 : (unsigned long int) shdr->sh_info,
2992 : 266 : (unsigned int) shdr->sh_link,
2993 : 266 : elf_strptr (ebl->elf, shstrndx, glink->sh_name));
2994 : :
2995 [ + + ]: 532 : fputs (class == ELFCLASS32
2996 : 32 : ? _("\
2997 : : Num: Value Size Type Bind Vis Ndx Name\n")
2998 : 234 : : _("\
2999 : : Num: Value Size Type Bind Vis Ndx Name\n"),
3000 : : stdout);
3001 : :
3002 : 266 : process_symtab(ebl, nsyms, shdr->sh_link, verneed_stridx, verdef_stridx,
3003 : : data, versym_data, NULL, verneed_data, verdef_data, xndx_data);
3004 : 266 : return true;
3005 : : }
3006 : :
3007 : :
3008 : : static bool
3009 : 0 : handle_dynamic_symtab (Ebl *ebl)
3010 : : {
3011 : 0 : GElf_Phdr phdr_mem;
3012 : 0 : GElf_Phdr *phdr = NULL;
3013 : : /* phnum is a static variable which was already fetched in function
3014 : : process_elf_file. */
3015 [ # # ]: 0 : for (size_t i = 0; i < phnum; ++i)
3016 : : {
3017 : 0 : phdr = gelf_getphdr (ebl->elf, i, &phdr_mem);
3018 [ # # # # ]: 0 : if (phdr == NULL || phdr->p_type == PT_DYNAMIC)
3019 : : break;
3020 : : }
3021 [ # # ]: 0 : if (phdr == NULL)
3022 : : return false;
3023 : :
3024 : 0 : GElf_Addr addrs[i_max] = {
3025 : : 0,
3026 : : };
3027 : 0 : GElf_Off offs[i_max] = {
3028 : : 0,
3029 : : };
3030 : 0 : get_dynscn_addrs (ebl->elf, phdr, addrs);
3031 : 0 : find_offsets (ebl->elf, 0, i_max, addrs, offs);
3032 : :
3033 : 0 : size_t syments = 0;
3034 : :
3035 : 0 : GElf_Ehdr ehdr_mem;
3036 : 0 : GElf_Ehdr *ehdr = gelf_getehdr (ebl->elf, &ehdr_mem);
3037 : :
3038 [ # # ]: 0 : if (offs[i_hash] != 0)
3039 : : {
3040 : : /* In the original format, .hash says the size of .dynsym. */
3041 : :
3042 [ # # # # : 0 : size_t entsz = SH_ENTSIZE_HASH (ehdr);
# # ]
3043 : : Elf_Data *data
3044 : 0 : = elf_getdata_rawchunk (ebl->elf, offs[i_hash] + entsz, entsz,
3045 : : (entsz == 4 ? ELF_T_WORD : ELF_T_XWORD));
3046 [ # # ]: 0 : if (data != NULL)
3047 : 0 : syments = (entsz == 4 ? *(const GElf_Word *)data->d_buf
3048 [ # # ]: 0 : : *(const GElf_Xword *)data->d_buf);
3049 : : }
3050 [ # # # # ]: 0 : if (offs[i_gnu_hash] != 0 && syments == 0)
3051 : : {
3052 : : /* In the new format, we can derive it with some work. */
3053 : :
3054 : 0 : const struct
3055 : : {
3056 : : Elf32_Word nbuckets;
3057 : : Elf32_Word symndx;
3058 : : Elf32_Word maskwords;
3059 : : Elf32_Word shift2;
3060 : : } * header;
3061 : :
3062 : 0 : Elf_Data *data = elf_getdata_rawchunk (ebl->elf, offs[i_gnu_hash],
3063 : : sizeof *header, ELF_T_WORD);
3064 [ # # ]: 0 : if (data != NULL)
3065 : : {
3066 : 0 : header = data->d_buf;
3067 : 0 : Elf32_Word nbuckets = header->nbuckets;
3068 : 0 : Elf32_Word symndx = header->symndx;
3069 : 0 : GElf_Off buckets_at
3070 : : = (offs[i_gnu_hash] + sizeof *header
3071 : 0 : + (gelf_getclass (ebl->elf) * sizeof (Elf32_Word)
3072 : 0 : * header->maskwords));
3073 : :
3074 : : // elf_getdata_rawchunk takes a size_t, make sure it
3075 : : // doesn't overflow.
3076 : : #if SIZE_MAX <= UINT32_MAX
3077 : : if (nbuckets > SIZE_MAX / sizeof (Elf32_Word))
3078 : : data = NULL;
3079 : : else
3080 : : #endif
3081 : 0 : data = elf_getdata_rawchunk (ebl->elf, buckets_at,
3082 : : nbuckets * sizeof (Elf32_Word),
3083 : : ELF_T_WORD);
3084 [ # # ]: 0 : if (data != NULL && symndx < nbuckets)
3085 : : {
3086 : 0 : const Elf32_Word *const buckets = data->d_buf;
3087 : 0 : Elf32_Word maxndx = symndx;
3088 [ # # ]: 0 : for (Elf32_Word bucket = 0; bucket < nbuckets; ++bucket)
3089 : 0 : if (buckets[bucket] > maxndx)
3090 : : maxndx = buckets[bucket];
3091 : :
3092 : 0 : GElf_Off hasharr_at
3093 : : = (buckets_at + nbuckets * sizeof (Elf32_Word));
3094 : 0 : hasharr_at += (maxndx - symndx) * sizeof (Elf32_Word);
3095 : 0 : do
3096 : : {
3097 : 0 : data = elf_getdata_rawchunk (
3098 : : ebl->elf, hasharr_at, sizeof (Elf32_Word), ELF_T_WORD);
3099 [ # # # # ]: 0 : if (data != NULL && (*(const Elf32_Word *)data->d_buf & 1u))
3100 : : {
3101 : 0 : syments = maxndx + 1;
3102 : 0 : break;
3103 : : }
3104 : 0 : ++maxndx;
3105 : 0 : hasharr_at += sizeof (Elf32_Word);
3106 : : }
3107 [ # # ]: 0 : while (data != NULL);
3108 : : }
3109 : : }
3110 : : }
3111 [ # # # # ]: 0 : if (offs[i_strtab] > offs[i_symtab] && syments == 0)
3112 : 0 : syments = ((offs[i_strtab] - offs[i_symtab])
3113 : 0 : / gelf_fsize (ebl->elf, ELF_T_SYM, 1, EV_CURRENT));
3114 : :
3115 [ # # # # : 0 : if (syments <= 0 || offs[i_strtab] == 0 || offs[i_symtab] == 0)
# # ]
3116 : : {
3117 : 0 : error_exit (0, _ ("Dynamic symbol information is not available for "
3118 : : "displaying symbols."));
3119 : : }
3120 : :
3121 : : /* All the data chunk initializaion. */
3122 : 0 : Elf_Data *symdata = NULL;
3123 : 0 : Elf_Data *symstrdata = NULL;
3124 : 0 : Elf_Data *versym_data = NULL;
3125 : 0 : Elf_Data *verdef_data = NULL;
3126 : 0 : Elf_Data *verneed_data = NULL;
3127 : :
3128 : 0 : if (offs[i_symtab] != 0)
3129 : 0 : symdata = elf_getdata_rawchunk (
3130 : : ebl->elf, offs[i_symtab],
3131 : : gelf_fsize (ebl->elf, ELF_T_SYM, syments, EV_CURRENT), ELF_T_SYM);
3132 : :
3133 [ # # ]: 0 : if (offs[i_strtab] != 0 && addrs[i_strsz] != 0)
3134 : 0 : symstrdata = elf_getdata_rawchunk (ebl->elf, offs[i_strtab], addrs[i_strsz],
3135 : : ELF_T_BYTE);
3136 : :
3137 [ # # ]: 0 : if (offs[i_versym] != 0)
3138 : 0 : versym_data = elf_getdata_rawchunk (
3139 : : ebl->elf, offs[i_versym], syments * sizeof (Elf64_Half), ELF_T_HALF);
3140 : :
3141 : : /* Get the verneed_data without vernaux. */
3142 [ # # # # ]: 0 : if (offs[i_verneed] != 0 && addrs[i_verneednum] != 0)
3143 : : {
3144 : 0 : verneed_data = elf_getdata_rawchunk (
3145 : : ebl->elf, offs[i_verneed], addrs[i_verneednum] * sizeof (Elf64_Verneed),
3146 : : ELF_T_VNEED);
3147 : :
3148 [ # # ]: 0 : if (verneed_data->d_size < sizeof (GElf_Verneed))
3149 : 0 : error_exit (0, _("malformed SHT_GNU_verneed data"));
3150 : : }
3151 : :
3152 : 0 : size_t vernauxnum = 0;
3153 : 0 : size_t vn_next_offset = 0;
3154 : :
3155 [ # # ]: 0 : if (verneed_data != NULL && verneed_data->d_buf != NULL)
3156 [ # # ]: 0 : for (size_t i = 0; i < addrs[i_verneednum]; i++)
3157 : : {
3158 [ # # ]: 0 : if (vn_next_offset > (verneed_data->d_size - sizeof (GElf_Verneed)))
3159 : 0 : error_exit (0, _("invalid SHT_GNU_verneed data"));
3160 : :
3161 : 0 : GElf_Verneed *verneed
3162 : : = (GElf_Verneed *)(verneed_data->d_buf + vn_next_offset);
3163 : 0 : vernauxnum += verneed->vn_cnt;
3164 : 0 : vn_next_offset += verneed->vn_next;
3165 : : }
3166 : :
3167 : : /* Update the verneed_data to include the vernaux. */
3168 [ # # # # ]: 0 : if (offs[i_verneed] != 0 && addrs[i_verneednum] != 0)
3169 : 0 : verneed_data = elf_getdata_rawchunk (
3170 : : ebl->elf, offs[i_verneed],
3171 : 0 : (addrs[i_verneednum] + vernauxnum) * sizeof (GElf_Verneed),
3172 : : ELF_T_VNEED);
3173 : :
3174 : : /* Get the verdef_data without verdaux. */
3175 [ # # # # ]: 0 : if (offs[i_verdef] != 0 && addrs[i_verdefnum] != 0)
3176 : : {
3177 : 0 : verdef_data = elf_getdata_rawchunk (
3178 : : ebl->elf, offs[i_verdef], addrs[i_verdefnum] * sizeof (Elf64_Verdef),
3179 : : ELF_T_VDEF);
3180 : :
3181 [ # # ]: 0 : if (verdef_data->d_size < sizeof (GElf_Verdef))
3182 : 0 : error_exit (0, _("malformed SHT_GNU_verdef data"));
3183 : : }
3184 : :
3185 : 0 : size_t verdauxnum = 0;
3186 : 0 : size_t vd_next_offset = 0;
3187 : :
3188 [ # # ]: 0 : if (verdef_data != NULL && verdef_data->d_buf != NULL)
3189 [ # # ]: 0 : for (size_t i = 0; i < addrs[i_verdefnum]; i++)
3190 : : {
3191 [ # # ]: 0 : if (vd_next_offset > (verdef_data->d_size - sizeof (GElf_Verdef)))
3192 : 0 : error_exit (0, _("invalid SHT_GNU_verdef data"));
3193 : :
3194 : 0 : GElf_Verdef *verdef
3195 : : = (GElf_Verdef *)(verdef_data->d_buf + vd_next_offset);
3196 : 0 : verdauxnum += verdef->vd_cnt;
3197 : 0 : vd_next_offset += verdef->vd_next;
3198 : : }
3199 : :
3200 : : /* Update the verdef_data to include the verdaux. */
3201 [ # # # # ]: 0 : if (offs[i_verdef] != 0 && addrs[i_verdefnum] != 0)
3202 : 0 : verdef_data = elf_getdata_rawchunk (
3203 : : ebl->elf, offs[i_verdef],
3204 : 0 : (addrs[i_verdefnum] + verdauxnum) * sizeof (GElf_Verdef), ELF_T_VDEF);
3205 : :
3206 : 0 : unsigned int nsyms = (unsigned int)syments;
3207 : 0 : process_symtab (ebl, nsyms, 0, 0, 0, symdata, versym_data, symstrdata,
3208 : : verneed_data, verdef_data, NULL);
3209 : 0 : return true;
3210 : : }
3211 : :
3212 : :
3213 : : /* Print version information. */
3214 : : static void
3215 : 184 : print_verinfo (Ebl *ebl)
3216 : : {
3217 : : /* Find the version information sections. For this we have to
3218 : : search through the section table. */
3219 : 184 : Elf_Scn *scn = NULL;
3220 : :
3221 [ + + ]: 6004 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
3222 : : {
3223 : : /* Handle the section if it is part of the versioning handling. */
3224 : 5820 : GElf_Shdr shdr_mem;
3225 : 5820 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
3226 : :
3227 [ + - ]: 5820 : if (likely (shdr != NULL))
3228 : : {
3229 [ + + ]: 5820 : if (shdr->sh_type == SHT_GNU_verneed)
3230 : 130 : handle_verneed (ebl, scn, shdr);
3231 [ + + ]: 5690 : else if (shdr->sh_type == SHT_GNU_verdef)
3232 : 4 : handle_verdef (ebl, scn, shdr);
3233 [ + + ]: 5686 : else if (shdr->sh_type == SHT_GNU_versym)
3234 : 130 : handle_versym (ebl, scn, shdr);
3235 : : }
3236 : : }
3237 : 184 : }
3238 : :
3239 : :
3240 : : static const char *
3241 : 380 : get_ver_flags (unsigned int flags)
3242 : : {
3243 : 380 : static char buf[32];
3244 : 380 : char *endp;
3245 : :
3246 [ + + ]: 380 : if (flags == 0)
3247 : 376 : return _("none");
3248 : :
3249 [ + - ]: 4 : if (flags & VER_FLG_BASE)
3250 : 4 : endp = stpcpy (buf, "BASE ");
3251 : : else
3252 : : endp = buf;
3253 : :
3254 [ - + ]: 4 : if (flags & VER_FLG_WEAK)
3255 : : {
3256 [ # # ]: 0 : if (endp != buf)
3257 : 0 : endp = stpcpy (endp, "| ");
3258 : :
3259 : 0 : endp = stpcpy (endp, "WEAK ");
3260 : : }
3261 : :
3262 [ - + ]: 4 : if (unlikely (flags & ~(VER_FLG_BASE | VER_FLG_WEAK)))
3263 : : {
3264 : 0 : strncpy (endp, _("| <unknown>"), buf + sizeof (buf) - endp);
3265 : 0 : buf[sizeof (buf) - 1] = '\0';
3266 : : }
3267 : :
3268 : : return buf;
3269 : : }
3270 : :
3271 : :
3272 : : static void
3273 : 130 : handle_verneed (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr)
3274 : : {
3275 : 130 : int class = gelf_getclass (ebl->elf);
3276 : :
3277 : : /* Get the data of the section. */
3278 : 130 : Elf_Data *data = elf_getdata (scn, NULL);
3279 [ - + ]: 130 : if (data == NULL)
3280 : 0 : return;
3281 : :
3282 : : /* Get the section header string table index. */
3283 : 130 : size_t shstrndx;
3284 [ - + ]: 130 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
3285 : 0 : error_exit (0, _("cannot get section header string table index"));
3286 : :
3287 : 130 : GElf_Shdr glink_mem;
3288 : 130 : GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
3289 : : &glink_mem);
3290 [ - + ]: 130 : if (glink == NULL)
3291 : 0 : error_exit (0, _("invalid sh_link value in section %zu"),
3292 : : elf_ndxscn (scn));
3293 : :
3294 : 130 : printf (ngettext ("\
3295 : : \nVersion needs section [%2u] '%s' contains %d entry:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'\n",
3296 : : "\
3297 : : \nVersion needs section [%2u] '%s' contains %d entries:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'\n",
3298 : : shdr->sh_info),
3299 : 130 : (unsigned int) elf_ndxscn (scn),
3300 : 130 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name), shdr->sh_info,
3301 : : class == ELFCLASS32 ? 10 : 18, shdr->sh_addr,
3302 : : shdr->sh_offset,
3303 [ + + ]: 130 : (unsigned int) shdr->sh_link,
3304 : 130 : elf_strptr (ebl->elf, shstrndx, glink->sh_name));
3305 : :
3306 : 130 : unsigned int offset = 0;
3307 [ + - ]: 184 : for (unsigned int cnt = shdr->sh_info; cnt > 0; cnt--)
3308 : : {
3309 : : /* Get the data at the next offset. */
3310 : 184 : GElf_Verneed needmem;
3311 : 184 : GElf_Verneed *need = gelf_getverneed (data, offset, &needmem);
3312 [ + - ]: 184 : if (unlikely (need == NULL))
3313 : : break;
3314 : :
3315 : 552 : printf (_(" %#06x: Version: %hu File: %s Cnt: %hu\n"),
3316 : 184 : offset, (unsigned short int) need->vn_version,
3317 : 184 : elf_strptr (ebl->elf, shdr->sh_link, need->vn_file),
3318 : 184 : (unsigned short int) need->vn_cnt);
3319 : :
3320 : 184 : unsigned int auxoffset = offset + need->vn_aux;
3321 [ + - ]: 356 : for (unsigned int cnt2 = need->vn_cnt; cnt2 > 0; cnt2--)
3322 : : {
3323 : 356 : GElf_Vernaux auxmem;
3324 : 356 : GElf_Vernaux *aux = gelf_getvernaux (data, auxoffset, &auxmem);
3325 [ + - ]: 356 : if (unlikely (aux == NULL))
3326 : : break;
3327 : :
3328 : 356 : printf (_(" %#06x: Name: %s Flags: %s Version: %hu\n"),
3329 : : auxoffset,
3330 : 356 : elf_strptr (ebl->elf, shdr->sh_link, aux->vna_name),
3331 : 356 : get_ver_flags (aux->vna_flags),
3332 : 356 : (unsigned short int) aux->vna_other);
3333 : :
3334 [ + + ]: 356 : if (aux->vna_next == 0)
3335 : : break;
3336 : :
3337 : 172 : auxoffset += aux->vna_next;
3338 : : }
3339 : :
3340 : : /* Find the next offset. */
3341 [ + + ]: 184 : if (need->vn_next == 0)
3342 : : break;
3343 : :
3344 : 54 : offset += need->vn_next;
3345 : : }
3346 : : }
3347 : :
3348 : :
3349 : : static void
3350 : 4 : handle_verdef (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr)
3351 : : {
3352 : : /* Get the data of the section. */
3353 : 4 : Elf_Data *data = elf_getdata (scn, NULL);
3354 [ - + ]: 4 : if (data == NULL)
3355 : 0 : return;
3356 : :
3357 : : /* Get the section header string table index. */
3358 : 4 : size_t shstrndx;
3359 [ - + ]: 4 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
3360 : 0 : error_exit (0, _("cannot get section header string table index"));
3361 : :
3362 : 4 : GElf_Shdr glink_mem;
3363 : 4 : GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
3364 : : &glink_mem);
3365 [ - + ]: 4 : if (glink == NULL)
3366 : 0 : error_exit (0, _("invalid sh_link value in section %zu"),
3367 : : elf_ndxscn (scn));
3368 : :
3369 : 4 : int class = gelf_getclass (ebl->elf);
3370 : 4 : printf (ngettext ("\
3371 : : \nVersion definition section [%2u] '%s' contains %d entry:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'\n",
3372 : : "\
3373 : : \nVersion definition section [%2u] '%s' contains %d entries:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'\n",
3374 : : shdr->sh_info),
3375 : 4 : (unsigned int) elf_ndxscn (scn),
3376 : 4 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
3377 : : shdr->sh_info,
3378 : : class == ELFCLASS32 ? 10 : 18, shdr->sh_addr,
3379 : : shdr->sh_offset,
3380 [ + - ]: 4 : (unsigned int) shdr->sh_link,
3381 : 4 : elf_strptr (ebl->elf, shstrndx, glink->sh_name));
3382 : :
3383 : 4 : unsigned int offset = 0;
3384 [ + - ]: 24 : for (unsigned int cnt = shdr->sh_info; cnt > 0; cnt--)
3385 : : {
3386 : : /* Get the data at the next offset. */
3387 : 24 : GElf_Verdef defmem;
3388 : 24 : GElf_Verdef *def = gelf_getverdef (data, offset, &defmem);
3389 [ + - ]: 24 : if (unlikely (def == NULL))
3390 : : break;
3391 : :
3392 : 24 : unsigned int auxoffset = offset + def->vd_aux;
3393 : 24 : GElf_Verdaux auxmem;
3394 : 24 : GElf_Verdaux *aux = gelf_getverdaux (data, auxoffset, &auxmem);
3395 [ + - ]: 24 : if (unlikely (aux == NULL))
3396 : : break;
3397 : :
3398 : 96 : printf (_("\
3399 : : %#06x: Version: %hd Flags: %s Index: %hd Cnt: %hd Name: %s\n"),
3400 : 24 : offset, def->vd_version,
3401 : 24 : get_ver_flags (def->vd_flags),
3402 : 24 : def->vd_ndx,
3403 : 24 : def->vd_cnt,
3404 : 24 : elf_strptr (ebl->elf, shdr->sh_link, aux->vda_name));
3405 : :
3406 : 24 : auxoffset += aux->vda_next;
3407 [ + + ]: 24 : for (unsigned int cnt2 = 1; cnt2 < def->vd_cnt; ++cnt2)
3408 : : {
3409 : 16 : aux = gelf_getverdaux (data, auxoffset, &auxmem);
3410 [ + - ]: 16 : if (unlikely (aux == NULL))
3411 : : break;
3412 : :
3413 : 16 : printf (_(" %#06x: Parent %d: %s\n"),
3414 : : auxoffset, cnt2,
3415 : 16 : elf_strptr (ebl->elf, shdr->sh_link, aux->vda_name));
3416 : :
3417 [ - + ]: 16 : if (aux->vda_next == 0)
3418 : : break;
3419 : :
3420 : 0 : auxoffset += aux->vda_next;
3421 : : }
3422 : :
3423 : : /* Find the next offset. */
3424 [ + + ]: 24 : if (def->vd_next == 0)
3425 : : break;
3426 : 20 : offset += def->vd_next;
3427 : : }
3428 : : }
3429 : :
3430 : :
3431 : : static void
3432 : 130 : handle_versym (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr)
3433 : : {
3434 : 130 : int class = gelf_getclass (ebl->elf);
3435 : 130 : const char **vername;
3436 : 130 : const char **filename;
3437 : :
3438 : : /* Get the data of the section. */
3439 : 130 : Elf_Data *data = elf_getdata (scn, NULL);
3440 [ + - ]: 130 : if (data == NULL)
3441 : 0 : return;
3442 : :
3443 : : /* Get the section header string table index. */
3444 : 130 : size_t shstrndx;
3445 [ - + ]: 130 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
3446 : 0 : error_exit (0, _("cannot get section header string table index"));
3447 : :
3448 : : /* We have to find the version definition section and extract the
3449 : : version names. */
3450 : : Elf_Scn *defscn = NULL;
3451 : : Elf_Scn *needscn = NULL;
3452 : :
3453 : : Elf_Scn *verscn = NULL;
3454 [ + + ]: 4046 : while ((verscn = elf_nextscn (ebl->elf, verscn)) != NULL)
3455 : : {
3456 : 3916 : GElf_Shdr vershdr_mem;
3457 : 3916 : GElf_Shdr *vershdr = gelf_getshdr (verscn, &vershdr_mem);
3458 : :
3459 [ - + ]: 3916 : if (likely (vershdr != NULL))
3460 : : {
3461 [ + + ]: 3916 : if (vershdr->sh_type == SHT_GNU_verdef)
3462 : : defscn = verscn;
3463 [ + + ]: 3912 : else if (vershdr->sh_type == SHT_GNU_verneed)
3464 : 3916 : needscn = verscn;
3465 : : }
3466 : : }
3467 : :
3468 : 130 : size_t nvername;
3469 [ + - ]: 130 : if (defscn != NULL || needscn != NULL)
3470 : : {
3471 : : /* We have a version information (better should have). Now get
3472 : : the version names. First find the maximum version number. */
3473 : 130 : nvername = 0;
3474 [ + + ]: 130 : if (defscn != NULL)
3475 : : {
3476 : : /* Run through the version definitions and find the highest
3477 : : index. */
3478 : 4 : unsigned int offset = 0;
3479 : 4 : Elf_Data *defdata;
3480 : 4 : GElf_Shdr defshdrmem;
3481 : 4 : GElf_Shdr *defshdr;
3482 : :
3483 : 4 : defdata = elf_getdata (defscn, NULL);
3484 [ + - ]: 4 : if (unlikely (defdata == NULL))
3485 : 0 : return;
3486 : :
3487 : 4 : defshdr = gelf_getshdr (defscn, &defshdrmem);
3488 [ + - ]: 4 : if (unlikely (defshdr == NULL))
3489 : : return;
3490 : :
3491 [ + - ]: 24 : for (unsigned int cnt = 0; cnt < defshdr->sh_info; ++cnt)
3492 : : {
3493 : 24 : GElf_Verdef defmem;
3494 : 24 : GElf_Verdef *def;
3495 : :
3496 : : /* Get the data at the next offset. */
3497 : 24 : def = gelf_getverdef (defdata, offset, &defmem);
3498 [ + - ]: 24 : if (unlikely (def == NULL))
3499 : : break;
3500 : :
3501 : 24 : nvername = MAX (nvername, (size_t) (def->vd_ndx & 0x7fff));
3502 : :
3503 [ + + ]: 24 : if (def->vd_next == 0)
3504 : : break;
3505 : 20 : offset += def->vd_next;
3506 : : }
3507 : : }
3508 [ + - ]: 130 : if (needscn != NULL)
3509 : : {
3510 : 130 : unsigned int offset = 0;
3511 : 130 : Elf_Data *needdata;
3512 : 130 : GElf_Shdr needshdrmem;
3513 : 130 : GElf_Shdr *needshdr;
3514 : :
3515 : 130 : needdata = elf_getdata (needscn, NULL);
3516 [ + - ]: 130 : if (unlikely (needdata == NULL))
3517 : 0 : return;
3518 : :
3519 : 130 : needshdr = gelf_getshdr (needscn, &needshdrmem);
3520 [ + - ]: 130 : if (unlikely (needshdr == NULL))
3521 : : return;
3522 : :
3523 [ + - ]: 184 : for (unsigned int cnt = 0; cnt < needshdr->sh_info; ++cnt)
3524 : : {
3525 : 184 : GElf_Verneed needmem;
3526 : 184 : GElf_Verneed *need;
3527 : 184 : unsigned int auxoffset;
3528 : 184 : int cnt2;
3529 : :
3530 : : /* Get the data at the next offset. */
3531 : 184 : need = gelf_getverneed (needdata, offset, &needmem);
3532 [ + - ]: 184 : if (unlikely (need == NULL))
3533 : : break;
3534 : :
3535 : : /* Run through the auxiliary entries. */
3536 : 184 : auxoffset = offset + need->vn_aux;
3537 [ + - ]: 356 : for (cnt2 = need->vn_cnt; --cnt2 >= 0; )
3538 : : {
3539 : 356 : GElf_Vernaux auxmem;
3540 : 356 : GElf_Vernaux *aux;
3541 : :
3542 : 356 : aux = gelf_getvernaux (needdata, auxoffset, &auxmem);
3543 [ + - ]: 356 : if (unlikely (aux == NULL))
3544 : : break;
3545 : :
3546 : 356 : nvername = MAX (nvername,
3547 : : (size_t) (aux->vna_other & 0x7fff));
3548 : :
3549 [ + + ]: 356 : if (aux->vna_next == 0)
3550 : : break;
3551 : 172 : auxoffset += aux->vna_next;
3552 : : }
3553 : :
3554 [ + + ]: 184 : if (need->vn_next == 0)
3555 : : break;
3556 : 54 : offset += need->vn_next;
3557 : : }
3558 : : }
3559 : :
3560 : : /* This is the number of versions we know about. */
3561 : 130 : ++nvername;
3562 : :
3563 : : /* Allocate the array. */
3564 : 130 : vername = (const char **) alloca (nvername * sizeof (const char *));
3565 [ + + ]: 130 : memset(vername, 0, nvername * sizeof (const char *));
3566 : 130 : filename = (const char **) alloca (nvername * sizeof (const char *));
3567 : 130 : memset(filename, 0, nvername * sizeof (const char *));
3568 : :
3569 : : /* Run through the data structures again and collect the strings. */
3570 [ + + ]: 130 : if (defscn != NULL)
3571 : : {
3572 : : /* Run through the version definitions and find the highest
3573 : : index. */
3574 : 4 : unsigned int offset = 0;
3575 : 4 : Elf_Data *defdata;
3576 : 4 : GElf_Shdr defshdrmem;
3577 : 4 : GElf_Shdr *defshdr;
3578 : :
3579 : 4 : defdata = elf_getdata (defscn, NULL);
3580 [ + - ]: 4 : if (unlikely (defdata == NULL))
3581 : 0 : return;
3582 : :
3583 : 4 : defshdr = gelf_getshdr (defscn, &defshdrmem);
3584 [ + - ]: 4 : if (unlikely (defshdr == NULL))
3585 : : return;
3586 : :
3587 [ + - ]: 24 : for (unsigned int cnt = 0; cnt < defshdr->sh_info; ++cnt)
3588 : : {
3589 : :
3590 : : /* Get the data at the next offset. */
3591 : 24 : GElf_Verdef defmem;
3592 : 24 : GElf_Verdef *def = gelf_getverdef (defdata, offset, &defmem);
3593 [ + - ]: 24 : if (unlikely (def == NULL))
3594 : : break;
3595 : :
3596 : 24 : GElf_Verdaux auxmem;
3597 : 48 : GElf_Verdaux *aux = gelf_getverdaux (defdata,
3598 : 24 : offset + def->vd_aux,
3599 : : &auxmem);
3600 [ + - ]: 24 : if (unlikely (aux == NULL))
3601 : : break;
3602 : :
3603 : 24 : vername[def->vd_ndx & 0x7fff]
3604 : 24 : = elf_strptr (ebl->elf, defshdr->sh_link, aux->vda_name);
3605 : 24 : filename[def->vd_ndx & 0x7fff] = NULL;
3606 : :
3607 [ + + ]: 24 : if (def->vd_next == 0)
3608 : : break;
3609 : 20 : offset += def->vd_next;
3610 : : }
3611 : : }
3612 [ + - ]: 130 : if (needscn != NULL)
3613 : : {
3614 : 130 : unsigned int offset = 0;
3615 : :
3616 : 130 : Elf_Data *needdata = elf_getdata (needscn, NULL);
3617 : 130 : GElf_Shdr needshdrmem;
3618 : 130 : GElf_Shdr *needshdr = gelf_getshdr (needscn, &needshdrmem);
3619 [ - + ]: 130 : if (unlikely (needdata == NULL || needshdr == NULL))
3620 : 0 : return;
3621 : :
3622 [ + - ]: 184 : for (unsigned int cnt = 0; cnt < needshdr->sh_info; ++cnt)
3623 : : {
3624 : : /* Get the data at the next offset. */
3625 : 184 : GElf_Verneed needmem;
3626 : 184 : GElf_Verneed *need = gelf_getverneed (needdata, offset,
3627 : : &needmem);
3628 [ + - ]: 184 : if (unlikely (need == NULL))
3629 : : break;
3630 : :
3631 : : /* Run through the auxiliary entries. */
3632 : 184 : unsigned int auxoffset = offset + need->vn_aux;
3633 [ + - ]: 356 : for (int cnt2 = need->vn_cnt; --cnt2 >= 0; )
3634 : : {
3635 : 356 : GElf_Vernaux auxmem;
3636 : 356 : GElf_Vernaux *aux = gelf_getvernaux (needdata, auxoffset,
3637 : : &auxmem);
3638 [ + - ]: 356 : if (unlikely (aux == NULL))
3639 : : break;
3640 : :
3641 : 356 : vername[aux->vna_other & 0x7fff]
3642 : 356 : = elf_strptr (ebl->elf, needshdr->sh_link, aux->vna_name);
3643 : 356 : filename[aux->vna_other & 0x7fff]
3644 : 356 : = elf_strptr (ebl->elf, needshdr->sh_link, need->vn_file);
3645 : :
3646 [ + + ]: 356 : if (aux->vna_next == 0)
3647 : : break;
3648 : 172 : auxoffset += aux->vna_next;
3649 : : }
3650 : :
3651 [ + + ]: 184 : if (need->vn_next == 0)
3652 : : break;
3653 : 54 : offset += need->vn_next;
3654 : : }
3655 : : }
3656 : : }
3657 : : else
3658 : : {
3659 : : vername = NULL;
3660 : : nvername = 1;
3661 : : filename = NULL;
3662 : : }
3663 : :
3664 : 130 : GElf_Shdr glink_mem;
3665 : 130 : GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
3666 : : &glink_mem);
3667 : 130 : size_t sh_entsize = gelf_fsize (ebl->elf, ELF_T_HALF, 1, EV_CURRENT);
3668 [ - + ]: 130 : if (glink == NULL)
3669 : 0 : error_exit (0, _("invalid sh_link value in section %zu"),
3670 : : elf_ndxscn (scn));
3671 : :
3672 : : /* Print the header. */
3673 : 130 : printf (ngettext ("\
3674 : : \nVersion symbols section [%2u] '%s' contains %d entry:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'",
3675 : : "\
3676 : : \nVersion symbols section [%2u] '%s' contains %d entries:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'",
3677 : : shdr->sh_size / sh_entsize),
3678 : 130 : (unsigned int) elf_ndxscn (scn),
3679 : 130 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
3680 : 130 : (int) (shdr->sh_size / sh_entsize),
3681 : : class == ELFCLASS32 ? 10 : 18, shdr->sh_addr,
3682 : : shdr->sh_offset,
3683 [ + + ]: 130 : (unsigned int) shdr->sh_link,
3684 : 130 : elf_strptr (ebl->elf, shstrndx, glink->sh_name));
3685 : :
3686 : : /* Now we can finally look at the actual contents of this section. */
3687 [ + + ]: 3308 : for (unsigned int cnt = 0; cnt < shdr->sh_size / sh_entsize; ++cnt)
3688 : : {
3689 [ + + ]: 3178 : if (cnt % 2 == 0)
3690 : 1608 : printf ("\n %4d:", cnt);
3691 : :
3692 : 3178 : GElf_Versym symmem;
3693 : 3178 : GElf_Versym *sym = gelf_getversym (data, cnt, &symmem);
3694 [ + - ]: 3178 : if (sym == NULL)
3695 : : break;
3696 : :
3697 [ + + + ]: 3178 : switch (*sym)
3698 : : {
3699 : 238 : ssize_t n;
3700 : 238 : case 0:
3701 : 238 : fputs (_(" 0 *local* "),
3702 : : stdout);
3703 : 238 : break;
3704 : :
3705 : 502 : case 1:
3706 : 502 : fputs (_(" 1 *global* "),
3707 : : stdout);
3708 : 502 : break;
3709 : :
3710 : 2438 : default:
3711 [ + - ]: 4876 : n = printf ("%4d%c%s",
3712 [ + - ]: 2438 : *sym & 0x7fff, *sym & 0x8000 ? 'h' : ' ',
3713 : : (vername != NULL
3714 [ + - ]: 2438 : && (unsigned int) (*sym & 0x7fff) < nvername)
3715 : 2438 : ? vername[*sym & 0x7fff] : "???");
3716 [ + - ]: 2438 : if ((unsigned int) (*sym & 0x7fff) < nvername
3717 [ + - + + ]: 2438 : && filename != NULL && filename[*sym & 0x7fff] != NULL)
3718 : 2134 : n += printf ("(%s)", filename[*sym & 0x7fff]);
3719 : 2438 : printf ("%*s", MAX (0, 33 - (int) n), " ");
3720 : 2438 : break;
3721 : : }
3722 : : }
3723 : 130 : putchar ('\n');
3724 : : }
3725 : :
3726 : :
3727 : : static void
3728 : 130 : print_hash_info (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr, size_t shstrndx,
3729 : : uint_fast32_t maxlength, Elf32_Word nbucket,
3730 : : uint_fast32_t nsyms, uint32_t *lengths, const char *extrastr)
3731 : : {
3732 : 130 : uint32_t *counts = xcalloc (maxlength + 1, sizeof (uint32_t));
3733 : :
3734 [ + + ]: 550 : for (Elf32_Word cnt = 0; cnt < nbucket; ++cnt)
3735 : 420 : ++counts[lengths[cnt]];
3736 : :
3737 : 130 : GElf_Shdr glink_mem;
3738 : 130 : GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf,
3739 : 130 : shdr->sh_link),
3740 : : &glink_mem);
3741 [ - + ]: 130 : if (glink == NULL)
3742 : : {
3743 : 0 : error (0, 0, _("invalid sh_link value in section %zu"),
3744 : : elf_ndxscn (scn));
3745 : 0 : free (counts);
3746 : 0 : return;
3747 : : }
3748 : :
3749 [ + + ]: 260 : printf (ngettext ("\
3750 : : \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",
3751 : : "\
3752 : : \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",
3753 : : nbucket),
3754 : 130 : (unsigned int) elf_ndxscn (scn),
3755 : 130 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
3756 : : (int) nbucket,
3757 : 130 : gelf_getclass (ebl->elf) == ELFCLASS32 ? 10 : 18,
3758 : : shdr->sh_addr,
3759 : : shdr->sh_offset,
3760 : 130 : (unsigned int) shdr->sh_link,
3761 : 130 : elf_strptr (ebl->elf, shstrndx, glink->sh_name));
3762 : :
3763 [ + - ]: 130 : if (extrastr != NULL)
3764 : 130 : fputs (extrastr, stdout);
3765 : :
3766 [ + - ]: 130 : if (likely (nbucket > 0))
3767 : : {
3768 : 130 : uint64_t success = 0;
3769 : :
3770 : : /* xgettext:no-c-format */
3771 : 130 : fputs (_("\
3772 : : Length Number % of total Coverage\n"), stdout);
3773 : 130 : printf (_(" 0 %6" PRIu32 " %5.1f%%\n"),
3774 : 130 : counts[0], (counts[0] * 100.0) / nbucket);
3775 : :
3776 : 130 : uint64_t nzero_counts = 0;
3777 [ + + ]: 228 : for (Elf32_Word cnt = 1; cnt <= maxlength; ++cnt)
3778 : : {
3779 : 98 : nzero_counts += counts[cnt] * cnt;
3780 : 98 : printf (_("\
3781 : : %7d %6" PRIu32 " %5.1f%% %5.1f%%\n"),
3782 : 98 : (int) cnt, counts[cnt], (counts[cnt] * 100.0) / nbucket,
3783 : 98 : (nzero_counts * 100.0) / nsyms);
3784 : : }
3785 : :
3786 : : Elf32_Word acc = 0;
3787 [ + + ]: 228 : for (Elf32_Word cnt = 1; cnt <= maxlength; ++cnt)
3788 : : {
3789 : 98 : acc += cnt;
3790 : 98 : success += counts[cnt] * acc;
3791 : : }
3792 : :
3793 [ + + ]: 130 : if (nzero_counts > 0)
3794 : 62 : printf (_("\
3795 : : Average number of tests: successful lookup: %f\n\
3796 : : unsuccessful lookup: %f\n"),
3797 : 62 : (double) success / (double) nzero_counts,
3798 : 62 : (double) nzero_counts / (double) nbucket);
3799 : : }
3800 : :
3801 : 130 : free (counts);
3802 : : }
3803 : :
3804 : :
3805 : : /* This function handles the traditional System V-style hash table format. */
3806 : : static void
3807 : 0 : handle_sysv_hash (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr, size_t shstrndx)
3808 : : {
3809 : 0 : Elf_Data *data = elf_getdata (scn, NULL);
3810 [ # # ]: 0 : if (unlikely (data == NULL))
3811 : : {
3812 : 0 : error (0, 0, _("cannot get data for section %d: %s"),
3813 : 0 : (int) elf_ndxscn (scn), elf_errmsg (-1));
3814 : 0 : return;
3815 : : }
3816 : :
3817 [ # # ]: 0 : if (unlikely (data->d_size < 2 * sizeof (Elf32_Word)))
3818 : : {
3819 : 0 : invalid_data:
3820 : 0 : error (0, 0, _("invalid data in sysv.hash section %d"),
3821 : 0 : (int) elf_ndxscn (scn));
3822 : 0 : return;
3823 : : }
3824 : :
3825 : 0 : Elf32_Word nbucket = ((Elf32_Word *) data->d_buf)[0];
3826 : 0 : Elf32_Word nchain = ((Elf32_Word *) data->d_buf)[1];
3827 : :
3828 : 0 : uint64_t used_buf = (2ULL + nchain + nbucket) * sizeof (Elf32_Word);
3829 [ # # ]: 0 : if (used_buf > data->d_size)
3830 : 0 : goto invalid_data;
3831 : :
3832 : 0 : Elf32_Word *bucket = &((Elf32_Word *) data->d_buf)[2];
3833 : 0 : Elf32_Word *chain = &((Elf32_Word *) data->d_buf)[2 + nbucket];
3834 : :
3835 : 0 : uint32_t *lengths = xcalloc (nbucket, sizeof (uint32_t));
3836 : :
3837 : 0 : uint_fast32_t maxlength = 0;
3838 : 0 : uint_fast32_t nsyms = 0;
3839 [ # # ]: 0 : for (Elf32_Word cnt = 0; cnt < nbucket; ++cnt)
3840 : : {
3841 : 0 : Elf32_Word inner = bucket[cnt];
3842 : 0 : Elf32_Word chain_len = 0;
3843 [ # # ]: 0 : while (inner > 0 && inner < nchain)
3844 : : {
3845 : 0 : ++nsyms;
3846 : 0 : ++chain_len;
3847 [ # # ]: 0 : if (chain_len > nchain)
3848 : : {
3849 : 0 : error (0, 0, _("invalid chain in sysv.hash section %d"),
3850 : 0 : (int) elf_ndxscn (scn));
3851 : 0 : free (lengths);
3852 : 0 : return;
3853 : : }
3854 [ # # ]: 0 : if (maxlength < ++lengths[cnt])
3855 : 0 : ++maxlength;
3856 : :
3857 : 0 : inner = chain[inner];
3858 : : }
3859 : : }
3860 : :
3861 : 0 : print_hash_info (ebl, scn, shdr, shstrndx, maxlength, nbucket, nsyms,
3862 : : lengths, NULL);
3863 : :
3864 : 0 : free (lengths);
3865 : : }
3866 : :
3867 : :
3868 : : /* This function handles the incorrect, System V-style hash table
3869 : : format some 64-bit architectures use. */
3870 : : static void
3871 : 0 : handle_sysv_hash64 (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr, size_t shstrndx)
3872 : : {
3873 : 0 : Elf_Data *data = elf_getdata (scn, NULL);
3874 [ # # ]: 0 : if (unlikely (data == NULL))
3875 : : {
3876 : 0 : error (0, 0, _("cannot get data for section %d: %s"),
3877 : 0 : (int) elf_ndxscn (scn), elf_errmsg (-1));
3878 : 0 : return;
3879 : : }
3880 : :
3881 [ # # ]: 0 : if (unlikely (data->d_size < 2 * sizeof (Elf64_Xword)))
3882 : : {
3883 : 0 : invalid_data:
3884 : 0 : error (0, 0, _("invalid data in sysv.hash64 section %d"),
3885 : 0 : (int) elf_ndxscn (scn));
3886 : 0 : return;
3887 : : }
3888 : :
3889 : 0 : Elf64_Xword nbucket = ((Elf64_Xword *) data->d_buf)[0];
3890 : 0 : Elf64_Xword nchain = ((Elf64_Xword *) data->d_buf)[1];
3891 : :
3892 : 0 : uint64_t maxwords = data->d_size / sizeof (Elf64_Xword);
3893 : 0 : if (maxwords < 2
3894 [ # # ]: 0 : || maxwords - 2 < nbucket
3895 [ # # ]: 0 : || maxwords - 2 - nbucket < nchain)
3896 : 0 : goto invalid_data;
3897 : :
3898 : 0 : Elf64_Xword *bucket = &((Elf64_Xword *) data->d_buf)[2];
3899 : 0 : Elf64_Xword *chain = &((Elf64_Xword *) data->d_buf)[2 + nbucket];
3900 : :
3901 : 0 : uint32_t *lengths = xcalloc (nbucket, sizeof (uint32_t));
3902 : :
3903 : 0 : uint_fast32_t maxlength = 0;
3904 : 0 : uint_fast32_t nsyms = 0;
3905 [ # # ]: 0 : for (Elf64_Xword cnt = 0; cnt < nbucket; ++cnt)
3906 : : {
3907 : 0 : Elf64_Xword inner = bucket[cnt];
3908 : 0 : Elf64_Xword chain_len = 0;
3909 [ # # ]: 0 : while (inner > 0 && inner < nchain)
3910 : : {
3911 : 0 : ++nsyms;
3912 : 0 : ++chain_len;
3913 [ # # ]: 0 : if (chain_len > nchain)
3914 : : {
3915 : 0 : error (0, 0, _("invalid chain in sysv.hash64 section %d"),
3916 : 0 : (int) elf_ndxscn (scn));
3917 : 0 : free (lengths);
3918 : 0 : return;
3919 : : }
3920 [ # # ]: 0 : if (maxlength < ++lengths[cnt])
3921 : 0 : ++maxlength;
3922 : :
3923 : 0 : inner = chain[inner];
3924 : : }
3925 : : }
3926 : :
3927 : 0 : print_hash_info (ebl, scn, shdr, shstrndx, maxlength, nbucket, nsyms,
3928 : : lengths, NULL);
3929 : :
3930 : 0 : free (lengths);
3931 : : }
3932 : :
3933 : :
3934 : : /* This function handles the GNU-style hash table format. */
3935 : : static void
3936 : 130 : handle_gnu_hash (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr, size_t shstrndx)
3937 : : {
3938 : 130 : uint32_t *lengths = NULL;
3939 : 130 : Elf_Data *data = elf_getdata (scn, NULL);
3940 [ - + ]: 130 : if (unlikely (data == NULL))
3941 : : {
3942 : 0 : error (0, 0, _("cannot get data for section %d: %s"),
3943 : 0 : (int) elf_ndxscn (scn), elf_errmsg (-1));
3944 : 0 : return;
3945 : : }
3946 : :
3947 [ - + ]: 130 : if (unlikely (data->d_size < 4 * sizeof (Elf32_Word)))
3948 : : {
3949 : 0 : invalid_data:
3950 : 0 : free (lengths);
3951 : 0 : error (0, 0, _("invalid data in gnu.hash section %d"),
3952 : 0 : (int) elf_ndxscn (scn));
3953 : 0 : return;
3954 : : }
3955 : :
3956 : 130 : Elf32_Word nbucket = ((Elf32_Word *) data->d_buf)[0];
3957 : 130 : Elf32_Word symbias = ((Elf32_Word *) data->d_buf)[1];
3958 : :
3959 : : /* Next comes the size of the bitmap. It's measured in words for
3960 : : the architecture. It's 32 bits for 32 bit archs, and 64 bits for
3961 : : 64 bit archs. There is always a bloom filter present, so zero is
3962 : : an invalid value. */
3963 : 130 : Elf32_Word bitmask_words = ((Elf32_Word *) data->d_buf)[2];
3964 [ + + ]: 130 : if (gelf_getclass (ebl->elf) == ELFCLASS64)
3965 : 114 : bitmask_words *= 2;
3966 : :
3967 [ - + ]: 130 : if (bitmask_words == 0)
3968 : 0 : goto invalid_data;
3969 : :
3970 : 130 : Elf32_Word shift = ((Elf32_Word *) data->d_buf)[3];
3971 : :
3972 : : /* Is there still room for the sym chain?
3973 : : Use uint64_t calculation to prevent 32bit overflow. */
3974 : 130 : uint64_t used_buf = (4ULL + bitmask_words + nbucket) * sizeof (Elf32_Word);
3975 : 130 : uint32_t max_nsyms = (data->d_size - used_buf) / sizeof (Elf32_Word);
3976 [ - + ]: 130 : if (used_buf > data->d_size)
3977 : 0 : goto invalid_data;
3978 : :
3979 : 130 : lengths = xcalloc (nbucket, sizeof (uint32_t));
3980 : :
3981 : 130 : Elf32_Word *bitmask = &((Elf32_Word *) data->d_buf)[4];
3982 : 130 : Elf32_Word *bucket = &((Elf32_Word *) data->d_buf)[4 + bitmask_words];
3983 : 130 : Elf32_Word *chain = &((Elf32_Word *) data->d_buf)[4 + bitmask_words
3984 : 130 : + nbucket];
3985 : :
3986 : : /* Compute distribution of chain lengths. */
3987 : 130 : uint_fast32_t maxlength = 0;
3988 : 130 : uint_fast32_t nsyms = 0;
3989 [ + + ]: 550 : for (Elf32_Word cnt = 0; cnt < nbucket; ++cnt)
3990 [ + + ]: 420 : if (bucket[cnt] != 0)
3991 : : {
3992 : 232 : Elf32_Word inner = bucket[cnt] - symbias;
3993 : 414 : do
3994 : : {
3995 : 414 : ++nsyms;
3996 [ + + ]: 414 : if (maxlength < ++lengths[cnt])
3997 : 98 : ++maxlength;
3998 [ - + ]: 414 : if (inner >= max_nsyms)
3999 : 0 : goto invalid_data;
4000 : : }
4001 [ + + ]: 414 : while ((chain[inner++] & 1) == 0);
4002 : : }
4003 : :
4004 : : /* Count bits in bitmask. */
4005 : : uint_fast32_t nbits = 0;
4006 [ + + ]: 446 : for (Elf32_Word cnt = 0; cnt < bitmask_words; ++cnt)
4007 : : {
4008 : 316 : uint_fast32_t word = bitmask[cnt];
4009 : :
4010 : 316 : word = (word & 0x55555555) + ((word >> 1) & 0x55555555);
4011 : 316 : word = (word & 0x33333333) + ((word >> 2) & 0x33333333);
4012 : 316 : word = (word & 0x0f0f0f0f) + ((word >> 4) & 0x0f0f0f0f);
4013 : 316 : word = (word & 0x00ff00ff) + ((word >> 8) & 0x00ff00ff);
4014 : 316 : nbits += (word & 0x0000ffff) + ((word >> 16) & 0x0000ffff);
4015 : : }
4016 : :
4017 : 260 : char *str = xasprintf (_("\
4018 : : Symbol Bias: %u\n\
4019 : : Bitmask Size: %zu bytes %" PRIuFAST32 "%% bits set 2nd hash shift: %u\n"),
4020 : : (unsigned int) symbias,
4021 : : bitmask_words * sizeof (Elf32_Word),
4022 : 130 : ((nbits * 100 + 50)
4023 : 130 : / (uint_fast32_t) (bitmask_words
4024 : : * sizeof (Elf32_Word) * 8)),
4025 : : (unsigned int) shift);
4026 : :
4027 : 130 : print_hash_info (ebl, scn, shdr, shstrndx, maxlength, nbucket, nsyms,
4028 : : lengths, str);
4029 : :
4030 : 130 : free (str);
4031 : 130 : free (lengths);
4032 : : }
4033 : :
4034 : :
4035 : : /* Find the symbol table(s). For this we have to search through the
4036 : : section table. */
4037 : : static void
4038 : 184 : handle_hash (Ebl *ebl)
4039 : : {
4040 : : /* Get the section header string table index. */
4041 : 184 : size_t shstrndx;
4042 [ - + ]: 184 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
4043 : 0 : error_exit (0, _("cannot get section header string table index"));
4044 : :
4045 : : Elf_Scn *scn = NULL;
4046 [ + + ]: 6004 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
4047 : : {
4048 : : /* Handle the section if it is a symbol table. */
4049 : 5820 : GElf_Shdr shdr_mem;
4050 : 5820 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
4051 : :
4052 [ + - ]: 5820 : if (likely (shdr != NULL))
4053 : : {
4054 [ + + ]: 5820 : if ((shdr->sh_type == SHT_HASH || shdr->sh_type == SHT_GNU_HASH)
4055 [ - + ]: 130 : && (shdr->sh_flags & SHF_COMPRESSED) != 0)
4056 : : {
4057 [ # # ]: 0 : if (elf_compress (scn, 0, 0) < 0)
4058 : 0 : printf ("WARNING: %s [%zd]\n",
4059 : : _("Couldn't uncompress section"),
4060 : : elf_ndxscn (scn));
4061 : 0 : shdr = gelf_getshdr (scn, &shdr_mem);
4062 [ # # ]: 0 : if (unlikely (shdr == NULL))
4063 : 0 : error_exit (0, _("cannot get section [%zd] header: %s"),
4064 : : elf_ndxscn (scn), elf_errmsg (-1));
4065 : : }
4066 : :
4067 [ - + ]: 5820 : if (shdr->sh_type == SHT_HASH)
4068 : : {
4069 [ # # ]: 0 : if (ebl_sysvhash_entrysize (ebl) == sizeof (Elf64_Xword))
4070 : 0 : handle_sysv_hash64 (ebl, scn, shdr, shstrndx);
4071 : : else
4072 : 0 : handle_sysv_hash (ebl, scn, shdr, shstrndx);
4073 : : }
4074 [ + + ]: 5820 : else if (shdr->sh_type == SHT_GNU_HASH)
4075 : 130 : handle_gnu_hash (ebl, scn, shdr, shstrndx);
4076 : : }
4077 : : }
4078 : 184 : }
4079 : :
4080 : :
4081 : : static void
4082 : 192 : print_liblist (Ebl *ebl)
4083 : : {
4084 : : /* Find the library list sections. For this we have to search
4085 : : through the section table. */
4086 : 192 : Elf_Scn *scn = NULL;
4087 : :
4088 : : /* Get the section header string table index. */
4089 : 192 : size_t shstrndx;
4090 [ - + ]: 192 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
4091 : 0 : error_exit (0, _("cannot get section header string table index"));
4092 : :
4093 [ + + ]: 6130 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
4094 : : {
4095 : 5938 : GElf_Shdr shdr_mem;
4096 : 5938 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
4097 : :
4098 [ + - - + ]: 5938 : if (shdr != NULL && shdr->sh_type == SHT_GNU_LIBLIST)
4099 : : {
4100 : 0 : size_t sh_entsize = gelf_fsize (ebl->elf, ELF_T_LIB, 1, EV_CURRENT);
4101 : 0 : int nentries = shdr->sh_size / sh_entsize;
4102 : 0 : printf (ngettext ("\
4103 : : \nLibrary list section [%2zu] '%s' at offset %#0" PRIx64 " contains %d entry:\n",
4104 : : "\
4105 : : \nLibrary list section [%2zu] '%s' at offset %#0" PRIx64 " contains %d entries:\n",
4106 : : nentries),
4107 : : elf_ndxscn (scn),
4108 : 0 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
4109 : : shdr->sh_offset,
4110 : : nentries);
4111 : :
4112 : 0 : Elf_Data *data = elf_getdata (scn, NULL);
4113 [ # # ]: 0 : if (data == NULL)
4114 : 0 : return;
4115 : :
4116 : 0 : puts (_("\
4117 : : Library Time Stamp Checksum Version Flags"));
4118 : :
4119 [ # # ]: 0 : for (int cnt = 0; cnt < nentries; ++cnt)
4120 : : {
4121 : 0 : GElf_Lib lib_mem;
4122 : 0 : GElf_Lib *lib = gelf_getlib (data, cnt, &lib_mem);
4123 [ # # ]: 0 : if (unlikely (lib == NULL))
4124 : 0 : continue;
4125 : :
4126 : 0 : time_t t = (time_t) lib->l_time_stamp;
4127 : 0 : struct tm *tm = gmtime (&t);
4128 [ # # ]: 0 : if (unlikely (tm == NULL))
4129 : 0 : continue;
4130 : :
4131 : 0 : printf (" [%2d] %-29s %04u-%02u-%02uT%02u:%02u:%02u %08x %-7u %u\n",
4132 : 0 : cnt, elf_strptr (ebl->elf, shdr->sh_link, lib->l_name),
4133 : 0 : tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
4134 : : tm->tm_hour, tm->tm_min, tm->tm_sec,
4135 : 0 : (unsigned int) lib->l_checksum,
4136 : 0 : (unsigned int) lib->l_version,
4137 : 0 : (unsigned int) lib->l_flags);
4138 : : }
4139 : : }
4140 : : }
4141 : : }
4142 : :
4143 : : static inline size_t
4144 : 16 : left (Elf_Data *data,
4145 : : const unsigned char *p)
4146 : : {
4147 : 16 : return (const unsigned char *) data->d_buf + data->d_size - p;
4148 : : }
4149 : :
4150 : : static void
4151 : 192 : print_attributes (Ebl *ebl, const GElf_Ehdr *ehdr)
4152 : : {
4153 : : /* Find the object attributes sections. For this we have to search
4154 : : through the section table. */
4155 : 192 : Elf_Scn *scn = NULL;
4156 : :
4157 : : /* Get the section header string table index. */
4158 : 192 : size_t shstrndx;
4159 [ - + ]: 192 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
4160 : 0 : error_exit (0, _("cannot get section header string table index"));
4161 : :
4162 [ + + ]: 6130 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
4163 : : {
4164 : 5938 : GElf_Shdr shdr_mem;
4165 : 5938 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
4166 : :
4167 [ + - + + ]: 5938 : if (shdr == NULL || (shdr->sh_type != SHT_GNU_ATTRIBUTES
4168 [ + + ]: 5932 : && (shdr->sh_type != SHT_ARM_ATTRIBUTES
4169 [ - + ]: 2 : || ehdr->e_machine != EM_ARM)
4170 [ + + ]: 5930 : && (shdr->sh_type != SHT_CSKY_ATTRIBUTES
4171 [ + - ]: 2 : || ehdr->e_machine != EM_CSKY)
4172 [ - + ]: 5930 : && (shdr->sh_type != SHT_RISCV_ATTRIBUTES
4173 [ # # ]: 0 : || ehdr->e_machine != EM_RISCV)))
4174 : 5930 : continue;
4175 : :
4176 : 8 : printf (_("\
4177 : : \nObject attributes section [%2zu] '%s' of %" PRIu64
4178 : : " bytes at offset %#0" PRIx64 ":\n"),
4179 : : elf_ndxscn (scn),
4180 : 8 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
4181 : : shdr->sh_size, shdr->sh_offset);
4182 : :
4183 : 8 : Elf_Data *data = elf_rawdata (scn, NULL);
4184 [ + - + - ]: 8 : if (unlikely (data == NULL || data->d_size == 0))
4185 : 0 : return;
4186 : :
4187 : 8 : const unsigned char *p = data->d_buf;
4188 : :
4189 : : /* There is only one 'version', A. */
4190 [ + - ]: 8 : if (unlikely (*p++ != 'A'))
4191 : : return;
4192 : :
4193 : 8 : fputs (_(" Owner Size\n"), stdout);
4194 : :
4195 : : /* Loop over the sections. */
4196 [ + + ]: 16 : while (left (data, p) >= 4)
4197 : : {
4198 : : /* Section length. */
4199 : 8 : uint32_t len;
4200 [ + + ]: 8 : memcpy (&len, p, sizeof len);
4201 : :
4202 [ + + ]: 8 : if (MY_ELFDATA != ehdr->e_ident[EI_DATA])
4203 : 4 : CONVERT (len);
4204 : :
4205 [ + - ]: 8 : if (unlikely (len > left (data, p)))
4206 : : break;
4207 : :
4208 : : /* Section vendor name. */
4209 : 8 : const unsigned char *name = p + sizeof len;
4210 : 8 : p += len;
4211 : :
4212 : 8 : unsigned const char *q = memchr (name, '\0', len);
4213 [ + - ]: 8 : if (unlikely (q == NULL))
4214 : : break;
4215 : 8 : ++q;
4216 : :
4217 : 8 : printf (_(" %-13s %4" PRIu32 "\n"), name, len);
4218 : :
4219 : 16 : bool gnu_vendor = (q - name == sizeof "gnu"
4220 [ + + - + ]: 8 : && !memcmp (name, "gnu", sizeof "gnu"));
4221 : :
4222 : : /* Loop over subsections. */
4223 [ + + ]: 8 : if (shdr->sh_type != SHT_GNU_ATTRIBUTES
4224 [ + - ]: 6 : || gnu_vendor)
4225 [ + + ]: 16 : while (q < p)
4226 : : {
4227 : 8 : const unsigned char *const sub = q;
4228 : :
4229 : 8 : unsigned int subsection_tag;
4230 : 8 : get_uleb128 (subsection_tag, q, p);
4231 [ + - ]: 8 : if (unlikely (q >= p))
4232 : : break;
4233 : :
4234 : 8 : uint32_t subsection_len;
4235 [ + - ]: 8 : if (unlikely (p - sub < (ptrdiff_t) sizeof subsection_len))
4236 : : break;
4237 : :
4238 [ + + ]: 8 : memcpy (&subsection_len, q, sizeof subsection_len);
4239 : :
4240 [ + + ]: 8 : if (MY_ELFDATA != ehdr->e_ident[EI_DATA])
4241 : 4 : CONVERT (subsection_len);
4242 : :
4243 : : /* Don't overflow, ptrdiff_t might be 32bits, but signed. */
4244 [ + - + - ]: 8 : if (unlikely (subsection_len == 0
4245 : : || subsection_len >= (uint32_t) PTRDIFF_MAX
4246 : : || p - sub < (ptrdiff_t) subsection_len))
4247 : : break;
4248 : :
4249 : 8 : const unsigned char *r = q + sizeof subsection_len;
4250 : 8 : q = sub + subsection_len;
4251 : :
4252 [ - + ]: 8 : switch (subsection_tag)
4253 : : {
4254 : 0 : default:
4255 : : /* Unknown subsection, print and skip. */
4256 : 0 : printf (_(" %-4u %12" PRIu32 "\n"),
4257 : : subsection_tag, subsection_len);
4258 : 0 : break;
4259 : :
4260 : 8 : case 1: /* Tag_File */
4261 : 8 : printf (_(" File: %11" PRIu32 "\n"),
4262 : : subsection_len);
4263 : :
4264 [ + + ]: 52 : while (r < q)
4265 : : {
4266 : 44 : unsigned int tag;
4267 : 44 : get_uleb128 (tag, r, q);
4268 [ + - ]: 44 : if (unlikely (r >= q))
4269 : : break;
4270 : :
4271 : : /* GNU style tags have either a uleb128 value,
4272 : : when lowest bit is not set, or a string
4273 : : when the lowest bit is set.
4274 : : "compatibility" (32) is special. It has
4275 : : both a string and a uleb128 value. For
4276 : : non-gnu we assume 6 till 31 only take ints.
4277 : : XXX see arm backend, do we need a separate
4278 : : hook? */
4279 : 44 : uint64_t value = 0;
4280 : 44 : const char *string = NULL;
4281 [ + - + + ]: 44 : if (tag == 32 || (tag & 1) == 0
4282 [ + - + + ]: 16 : || (! gnu_vendor && (tag > 5 && tag < 32)))
4283 : : {
4284 : : // Note r >= q check above.
4285 : 42 : get_uleb128 (value, r, q);
4286 [ + - ]: 42 : if (r > q)
4287 : : break;
4288 : : }
4289 [ + - ]: 44 : if (tag == 32
4290 [ + + ]: 44 : || ((tag & 1) != 0
4291 [ + - ]: 16 : && (gnu_vendor
4292 [ + - ]: 16 : || (! gnu_vendor && tag > 32)))
4293 [ + + + + ]: 44 : || (! gnu_vendor && tag > 3 && tag < 6))
4294 : : {
4295 : 2 : string = (const char *) r;
4296 : 2 : r = memchr (r, '\0', q - r);
4297 [ + - ]: 2 : if (r == NULL)
4298 : : break;
4299 : 2 : ++r;
4300 : : }
4301 : :
4302 : 44 : const char *tag_name = NULL;
4303 : 44 : const char *value_name = NULL;
4304 : 44 : ebl_check_object_attribute (ebl, (const char *) name,
4305 : : tag, value,
4306 : : &tag_name, &value_name);
4307 : :
4308 [ + - ]: 44 : if (tag_name != NULL)
4309 : : {
4310 [ - + ]: 44 : if (tag == 32)
4311 : 0 : printf (_(" %s: %" PRId64 ", %s\n"),
4312 : : tag_name, value, string);
4313 [ + + + + ]: 44 : else if (string == NULL && value_name == NULL)
4314 : 2 : printf (_(" %s: %" PRId64 "\n"),
4315 : : tag_name, value);
4316 : : else
4317 : 42 : printf (_(" %s: %s\n"),
4318 : : tag_name, string ?: value_name);
4319 : : }
4320 : : else
4321 : : {
4322 : : /* For "gnu" vendor 32 "compatibility" has
4323 : : already been handled above. */
4324 [ # # # # ]: 0 : assert (tag != 32
4325 : : || strcmp ((const char *) name, "gnu"));
4326 [ # # ]: 0 : if (string == NULL)
4327 : 0 : printf (_(" %u: %" PRId64 "\n"),
4328 : : tag, value);
4329 : : else
4330 : 0 : printf (_(" %u: %s\n"),
4331 : : tag, string);
4332 : : }
4333 : : }
4334 : : }
4335 : : }
4336 : : }
4337 : : }
4338 : : }
4339 : :
4340 : : /* Returns either the (relocated) data from the Dwarf, or tries to get
4341 : : the "raw" (uncompressed) data from the Elf section. Produces a
4342 : : warning if the data cannot be found (or decompressed). */
4343 : : static Elf_Data *
4344 : 810 : get_debug_elf_data (Dwarf *dbg, Ebl *ebl, int idx, Elf_Scn *scn)
4345 : : {
4346 : : /* We prefer to get the section data from the Dwarf because that
4347 : : might have been relocated already. Note this is subtly wrong if
4348 : : there are multiple sections with the same .debug name. */
4349 [ - + ]: 810 : if (dbg->sectiondata[idx] != NULL)
4350 : : return dbg->sectiondata[idx];
4351 : :
4352 : 0 : GElf_Shdr shdr_mem;
4353 : 0 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
4354 [ # # # # ]: 0 : if (shdr != NULL && (shdr->sh_flags & SHF_COMPRESSED) != 0)
4355 : : {
4356 [ # # ]: 0 : if (elf_compress (scn, 0, 0) < 0)
4357 : : {
4358 : 0 : error (0, 0, "%s [%zd] '%s'\n",
4359 : : _("Couldn't uncompress section"),
4360 : : elf_ndxscn (scn), section_name (ebl, shdr));
4361 : 0 : return NULL;
4362 : : }
4363 : : }
4364 : :
4365 : 0 : Elf_Data *data = elf_getdata (scn, NULL);
4366 [ # # ]: 0 : if (data == NULL)
4367 : 0 : error (0, 0, "%s [%zd] '%s': %s\n",
4368 : : _("Couldn't get data from section"),
4369 : : elf_ndxscn (scn), section_name (ebl, shdr), elf_errmsg (-1));
4370 : :
4371 : 0 : return elf_getdata (scn, NULL);
4372 : : }
4373 : :
4374 : : static void
4375 : 2253782 : print_dwarf_addr (Dwfl_Module *dwflmod,
4376 : : int address_size, Dwarf_Addr address, Dwarf_Addr raw,
4377 : : FILE *out)
4378 : : {
4379 : : /* See if there is a name we can give for this address. */
4380 : 2253782 : GElf_Sym sym;
4381 : 2253782 : GElf_Off off = 0;
4382 [ + + ]: 14168 : const char *name = (print_address_names && ! print_unresolved_addresses)
4383 : 14004 : ? dwfl_module_addrinfo (dwflmod, address, &off, &sym, NULL, NULL, NULL)
4384 [ + + ]: 2253782 : : NULL;
4385 : :
4386 : 2253782 : const char *scn;
4387 [ + + ]: 2253782 : if (print_unresolved_addresses)
4388 : : {
4389 : 168 : address = raw;
4390 : 168 : scn = NULL;
4391 : : }
4392 : : else
4393 : : {
4394 : : /* Relativize the address. */
4395 : 2253614 : int n = dwfl_module_relocations (dwflmod);
4396 [ + + ]: 2253614 : int i = n < 1 ? -1 : dwfl_module_relocate_address (dwflmod, &address);
4397 : :
4398 : : /* In an ET_REL file there is a section name to refer to. */
4399 : 2250862 : scn = (i < 0 ? NULL
4400 [ - + ]: 2250862 : : dwfl_module_relocation_info (dwflmod, i, NULL));
4401 : : }
4402 : :
4403 [ - + ]: 2253782 : if ((name != NULL
4404 : 13596 : ? (off != 0
4405 : : ? (scn != NULL
4406 : : ? (address_size == 0
4407 : 2 : ? fprintf (out, "%s+%#" PRIx64 " <%s+%#" PRIx64 ">",
4408 : : scn, address, name, off)
4409 : 8 : : fprintf (out, "%s+%#0*" PRIx64 " <%s+%#" PRIx64 ">",
4410 : 4 : scn, 2 + address_size * 2, address,
4411 : : name, off))
4412 : : : (address_size == 0
4413 : 364 : ? fprintf (out, "%#" PRIx64 " <%s+%#" PRIx64 ">",
4414 : : address, name, off)
4415 : 2156 : : fprintf (out, "%#0*" PRIx64 " <%s+%#" PRIx64 ">",
4416 : 1078 : 2 + address_size * 2, address,
4417 : : name, off)))
4418 : : : (scn != NULL
4419 : : ? (address_size == 0
4420 : 2 : ? fprintf (out, "%s+%#" PRIx64 " <%s>", scn, address, name)
4421 : 22292 : : fprintf (out, "%s+%#0*" PRIx64 " <%s>",
4422 : 11146 : scn, 2 + address_size * 2, address, name))
4423 : : : (address_size == 0
4424 : 160 : ? fprintf (out, "%#" PRIx64 " <%s>", address, name)
4425 : 1680 : : fprintf (out, "%#0*" PRIx64 " <%s>",
4426 : 840 : 2 + address_size * 2, address, name))))
4427 : : : (scn != NULL
4428 : : ? (address_size == 0
4429 : 822800 : ? fprintf (out, "%s+%#" PRIx64, scn, address)
4430 : 2833816 : : fprintf (out, "%s+%#0*" PRIx64,
4431 : 1416908 : scn, 2 + address_size * 2, address))
4432 : : : (address_size == 0
4433 : 70 : ? fprintf (out, "%#" PRIx64, address)
4434 : 816 : : fprintf (out, "%#0*" PRIx64,
4435 [ + + + + : 4507156 : 2 + address_size * 2, address)))) < 0)
+ + + + +
+ + + + +
+ + + + +
+ + + ]
4436 : 0 : error_exit (0, _("sprintf failure"));
4437 : 2253782 : }
4438 : :
4439 : :
4440 : : static const char *
4441 : 2290364 : dwarf_tag_string (unsigned int tag)
4442 : : {
4443 [ - + + - : 2290364 : switch (tag)
- - - - +
+ + + + -
- - - - -
+ - + - +
- - + + -
+ - - - -
- - - + -
+ - + + +
- - - - -
- + - - +
- - - + -
+ + + + -
- - - - +
+ + + - +
+ + + - -
- ]
4444 : : {
4445 : : #define DWARF_ONE_KNOWN_DW_TAG(NAME, CODE) case CODE: return #NAME;
4446 : 2290364 : DWARF_ALL_KNOWN_DW_TAG
4447 : : #undef DWARF_ONE_KNOWN_DW_TAG
4448 : 0 : default:
4449 : 0 : return NULL;
4450 : : }
4451 : : }
4452 : :
4453 : :
4454 : : static const char *
4455 : 8888240 : dwarf_attr_string (unsigned int attrnum)
4456 : : {
4457 [ + - + - : 8888240 : switch (attrnum)
- - - + -
- + + + -
- - - + +
- + - - +
- + - + -
- - - - -
- - - - -
- - - - -
- - - + -
+ - + - +
- - - + +
- - - + -
+ - + + -
- + + + +
- + + + -
+ - - + -
+ - + + -
+ - - + +
+ + - - -
- - + + +
+ - + - +
- - + - +
+ - + - +
+ - + + +
+ + + + -
- + - - -
+ - + - -
- - + + -
+ - - - -
+ - - - +
+ - + - -
- - + + -
- - - - +
+ - - - -
- - - + ]
4458 : : {
4459 : : #define DWARF_ONE_KNOWN_DW_AT(NAME, CODE) case CODE: return #NAME;
4460 : 8888238 : DWARF_ALL_KNOWN_DW_AT
4461 : : #undef DWARF_ONE_KNOWN_DW_AT
4462 : 0 : default:
4463 : 0 : return NULL;
4464 : : }
4465 : : }
4466 : :
4467 : :
4468 : : static const char *
4469 : 8898658 : dwarf_form_string (unsigned int form)
4470 : : {
4471 [ + + + + : 8898658 : switch (form)
+ - - - -
- + - - +
+ + + + +
+ + + - +
+ - - + -
- + - - +
+ + + + +
- + + - -
- + - + ]
4472 : : {
4473 : : #define DWARF_ONE_KNOWN_DW_FORM(NAME, CODE) case CODE: return #NAME;
4474 : 8898654 : DWARF_ALL_KNOWN_DW_FORM
4475 : : #undef DWARF_ONE_KNOWN_DW_FORM
4476 : 0 : default:
4477 : 0 : return NULL;
4478 : : }
4479 : : }
4480 : :
4481 : :
4482 : : static const char *
4483 : 3740 : dwarf_lang_string (unsigned int lang)
4484 : : {
4485 [ - - + - : 3740 : switch (lang)
- - - + -
- + + - +
- - + - -
- - - - -
- - - + -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - +
- ]
4486 : : {
4487 : : #define DWARF_ONE_KNOWN_DW_LANG(NAME, CODE) case CODE: return #NAME;
4488 : 3738 : DWARF_ALL_KNOWN_DW_LANG
4489 : : #undef DWARF_ONE_KNOWN_DW_LANG
4490 : 2 : default:
4491 : 2 : return NULL;
4492 : : }
4493 : : }
4494 : :
4495 : :
4496 : : static const char *
4497 : 4 : dwarf_lname_string (unsigned int lname)
4498 : : {
4499 [ - - - + : 4 : switch (lname)
- + - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - ]
4500 : : {
4501 : : #define DWARF_ONE_KNOWN_DW_LNAME(NAME, CODE) case CODE: return #NAME;
4502 : 4 : DWARF_ALL_KNOWN_DW_LNAME
4503 : : #undef DWARF_ONE_KNOWN_DW_LNAME
4504 : 0 : default:
4505 : 0 : return NULL;
4506 : : }
4507 : : }
4508 : :
4509 : :
4510 : : static const char *
4511 : 7280 : dwarf_inline_string (unsigned int code)
4512 : : {
4513 : 7280 : static const char *const known[] =
4514 : : {
4515 : : #define DWARF_ONE_KNOWN_DW_INL(NAME, CODE) [CODE] = #NAME,
4516 : : DWARF_ALL_KNOWN_DW_INL
4517 : : #undef DWARF_ONE_KNOWN_DW_INL
4518 : : };
4519 : :
4520 : 7280 : if (likely (code < sizeof (known) / sizeof (known[0])))
4521 : 7280 : return known[code];
4522 : :
4523 : : return NULL;
4524 : : }
4525 : :
4526 : :
4527 : : static const char *
4528 : 59402 : dwarf_encoding_string (unsigned int code)
4529 : : {
4530 : 59402 : static const char *const known[] =
4531 : : {
4532 : : #define DWARF_ONE_KNOWN_DW_ATE(NAME, CODE) [CODE] = #NAME,
4533 : : DWARF_ALL_KNOWN_DW_ATE
4534 : : #undef DWARF_ONE_KNOWN_DW_ATE
4535 : : };
4536 : :
4537 : 59402 : if (likely (code < sizeof (known) / sizeof (known[0])))
4538 : 59402 : return known[code];
4539 : :
4540 : : return NULL;
4541 : : }
4542 : :
4543 : :
4544 : : static const char *
4545 : 0 : dwarf_access_string (unsigned int code)
4546 : : {
4547 : 0 : static const char *const known[] =
4548 : : {
4549 : : #define DWARF_ONE_KNOWN_DW_ACCESS(NAME, CODE) [CODE] = #NAME,
4550 : : DWARF_ALL_KNOWN_DW_ACCESS
4551 : : #undef DWARF_ONE_KNOWN_DW_ACCESS
4552 : : };
4553 : :
4554 : 0 : if (likely (code < sizeof (known) / sizeof (known[0])))
4555 : 0 : return known[code];
4556 : :
4557 : : return NULL;
4558 : : }
4559 : :
4560 : :
4561 : : static const char *
4562 : 0 : dwarf_defaulted_string (unsigned int code)
4563 : : {
4564 : 0 : static const char *const known[] =
4565 : : {
4566 : : #define DWARF_ONE_KNOWN_DW_DEFAULTED(NAME, CODE) [CODE] = #NAME,
4567 : : DWARF_ALL_KNOWN_DW_DEFAULTED
4568 : : #undef DWARF_ONE_KNOWN_DW_DEFAULTED
4569 : : };
4570 : :
4571 : 0 : if (likely (code < sizeof (known) / sizeof (known[0])))
4572 : 0 : return known[code];
4573 : :
4574 : : return NULL;
4575 : : }
4576 : :
4577 : :
4578 : : static const char *
4579 : 0 : dwarf_visibility_string (unsigned int code)
4580 : : {
4581 : 0 : static const char *const known[] =
4582 : : {
4583 : : #define DWARF_ONE_KNOWN_DW_VIS(NAME, CODE) [CODE] = #NAME,
4584 : : DWARF_ALL_KNOWN_DW_VIS
4585 : : #undef DWARF_ONE_KNOWN_DW_VIS
4586 : : };
4587 : :
4588 : 0 : if (likely (code < sizeof (known) / sizeof (known[0])))
4589 : 0 : return known[code];
4590 : :
4591 : : return NULL;
4592 : : }
4593 : :
4594 : :
4595 : : static const char *
4596 : 0 : dwarf_virtuality_string (unsigned int code)
4597 : : {
4598 : 0 : static const char *const known[] =
4599 : : {
4600 : : #define DWARF_ONE_KNOWN_DW_VIRTUALITY(NAME, CODE) [CODE] = #NAME,
4601 : : DWARF_ALL_KNOWN_DW_VIRTUALITY
4602 : : #undef DWARF_ONE_KNOWN_DW_VIRTUALITY
4603 : : };
4604 : :
4605 : 0 : if (likely (code < sizeof (known) / sizeof (known[0])))
4606 : 0 : return known[code];
4607 : :
4608 : : return NULL;
4609 : : }
4610 : :
4611 : :
4612 : : static const char *
4613 : 0 : dwarf_identifier_case_string (unsigned int code)
4614 : : {
4615 : 0 : static const char *const known[] =
4616 : : {
4617 : : #define DWARF_ONE_KNOWN_DW_ID(NAME, CODE) [CODE] = #NAME,
4618 : : DWARF_ALL_KNOWN_DW_ID
4619 : : #undef DWARF_ONE_KNOWN_DW_ID
4620 : : };
4621 : :
4622 : 0 : if (likely (code < sizeof (known) / sizeof (known[0])))
4623 : 0 : return known[code];
4624 : :
4625 : : return NULL;
4626 : : }
4627 : :
4628 : :
4629 : : static const char *
4630 : 0 : dwarf_calling_convention_string (unsigned int code)
4631 : : {
4632 : 0 : static const char *const known[] =
4633 : : {
4634 : : #define DWARF_ONE_KNOWN_DW_CC(NAME, CODE) [CODE] = #NAME,
4635 : : DWARF_ALL_KNOWN_DW_CC
4636 : : #undef DWARF_ONE_KNOWN_DW_CC
4637 : : };
4638 : :
4639 : 0 : if (likely (code < sizeof (known) / sizeof (known[0])))
4640 : 0 : return known[code];
4641 : :
4642 : : return NULL;
4643 : : }
4644 : :
4645 : :
4646 : : static const char *
4647 : 0 : dwarf_ordering_string (unsigned int code)
4648 : : {
4649 : 0 : static const char *const known[] =
4650 : : {
4651 : : #define DWARF_ONE_KNOWN_DW_ORD(NAME, CODE) [CODE] = #NAME,
4652 : : DWARF_ALL_KNOWN_DW_ORD
4653 : : #undef DWARF_ONE_KNOWN_DW_ORD
4654 : : };
4655 : :
4656 : 0 : if (likely (code < sizeof (known) / sizeof (known[0])))
4657 : 0 : return known[code];
4658 : :
4659 : : return NULL;
4660 : : }
4661 : :
4662 : :
4663 : : static const char *
4664 : 16 : dwarf_discr_list_string (unsigned int code)
4665 : : {
4666 : 16 : static const char *const known[] =
4667 : : {
4668 : : #define DWARF_ONE_KNOWN_DW_DSC(NAME, CODE) [CODE] = #NAME,
4669 : : DWARF_ALL_KNOWN_DW_DSC
4670 : : #undef DWARF_ONE_KNOWN_DW_DSC
4671 : : };
4672 : :
4673 : 16 : if (likely (code < sizeof (known) / sizeof (known[0])))
4674 : 16 : return known[code];
4675 : :
4676 : : return NULL;
4677 : : }
4678 : :
4679 : :
4680 : : static const char *
4681 : 1115868 : dwarf_locexpr_opcode_string (unsigned int code)
4682 : : {
4683 : 1115868 : static const char *const known[] =
4684 : : {
4685 : : /* Normally we can't afford building huge table of 64K entries,
4686 : : most of them zero, just because there are a couple defined
4687 : : values at the far end. In case of opcodes, it's OK. */
4688 : : #define DWARF_ONE_KNOWN_DW_OP(NAME, CODE) [CODE] = #NAME,
4689 : : DWARF_ALL_KNOWN_DW_OP
4690 : : #undef DWARF_ONE_KNOWN_DW_OP
4691 : : };
4692 : :
4693 : 1115868 : if (likely (code < sizeof (known) / sizeof (known[0])))
4694 : 1115868 : return known[code];
4695 : :
4696 : : return NULL;
4697 : : }
4698 : :
4699 : :
4700 : : static const char *
4701 : 3486 : dwarf_unit_string (unsigned int type)
4702 : : {
4703 [ - + - - : 3486 : switch (type)
- - + ]
4704 : : {
4705 : : #define DWARF_ONE_KNOWN_DW_UT(NAME, CODE) case CODE: return #NAME;
4706 : 14 : DWARF_ALL_KNOWN_DW_UT
4707 : : #undef DWARF_ONE_KNOWN_DW_UT
4708 : 0 : default:
4709 : 0 : return NULL;
4710 : : }
4711 : : }
4712 : :
4713 : :
4714 : : static const char *
4715 : 148364 : dwarf_range_list_encoding_string (unsigned int kind)
4716 : : {
4717 [ - + + - : 148364 : switch (kind)
+ - - -
+ ]
4718 : : {
4719 : : #define DWARF_ONE_KNOWN_DW_RLE(NAME, CODE) case CODE: return #NAME;
4720 : 131346 : DWARF_ALL_KNOWN_DW_RLE
4721 : : #undef DWARF_ONE_KNOWN_DW_RLE
4722 : 0 : default:
4723 : 0 : return NULL;
4724 : : }
4725 : : }
4726 : :
4727 : :
4728 : : static const char *
4729 : 653748 : dwarf_loc_list_encoding_string (unsigned int kind)
4730 : : {
4731 [ - - + + : 653748 : switch (kind)
- + - + -
- + ]
4732 : : {
4733 : : #define DWARF_ONE_KNOWN_DW_LLE(NAME, CODE) case CODE: return #NAME;
4734 : 608128 : DWARF_ALL_KNOWN_DW_LLE
4735 : : #undef DWARF_ONE_KNOWN_DW_LLE
4736 : : /* DW_LLE_GNU_view_pair is special/incompatible with default codes. */
4737 : 0 : case DW_LLE_GNU_view_pair: return "GNU_view_pair";
4738 : 0 : default:
4739 : 0 : return NULL;
4740 : : }
4741 : : }
4742 : :
4743 : :
4744 : : static const char *
4745 : 10418 : dwarf_line_content_description_string (unsigned int kind)
4746 : : {
4747 [ + + - - : 10418 : switch (kind)
- + ]
4748 : : {
4749 : : #define DWARF_ONE_KNOWN_DW_LNCT(NAME, CODE) case CODE: return #NAME;
4750 : 10410 : DWARF_ALL_KNOWN_DW_LNCT
4751 : : #undef DWARF_ONE_KNOWN_DW_LNCT
4752 : 0 : default:
4753 : 0 : return NULL;
4754 : : }
4755 : : }
4756 : :
4757 : :
4758 : : /* Used by all dwarf_foo_name functions. */
4759 : : static const char *
4760 : 20963720 : string_or_unknown (const char *known, unsigned int code,
4761 : : unsigned int lo_user, unsigned int hi_user,
4762 : : bool print_unknown_num)
4763 : : {
4764 : 20963720 : static char unknown_buf[20];
4765 : :
4766 [ + + ]: 20963720 : if (likely (known != NULL))
4767 : : return known;
4768 : :
4769 [ - + - - ]: 2 : if (lo_user != 0 && code >= lo_user && code <= hi_user)
4770 : : {
4771 : 0 : snprintf (unknown_buf, sizeof unknown_buf, "lo_user+%#x",
4772 : : code - lo_user);
4773 : 0 : return unknown_buf;
4774 : : }
4775 : :
4776 [ + - ]: 2 : if (print_unknown_num)
4777 : : {
4778 : 2 : snprintf (unknown_buf, sizeof unknown_buf, "??? (%#x)", code);
4779 : 2 : return unknown_buf;
4780 : : }
4781 : :
4782 : : return "???";
4783 : : }
4784 : :
4785 : :
4786 : : static const char *
4787 : 2290364 : dwarf_tag_name (unsigned int tag)
4788 : : {
4789 : 2290364 : const char *ret = dwarf_tag_string (tag);
4790 : 2290364 : return string_or_unknown (ret, tag, DW_TAG_lo_user, DW_TAG_hi_user, true);
4791 : : }
4792 : :
4793 : : static const char *
4794 : 8888240 : dwarf_attr_name (unsigned int attr)
4795 : : {
4796 : 8888240 : const char *ret = dwarf_attr_string (attr);
4797 : 8888240 : return string_or_unknown (ret, attr, DW_AT_lo_user, DW_AT_hi_user, true);
4798 : : }
4799 : :
4800 : :
4801 : : static const char *
4802 : 8898658 : dwarf_form_name (unsigned int form)
4803 : : {
4804 : 8898658 : const char *ret = dwarf_form_string (form);
4805 : 8898658 : return string_or_unknown (ret, form, 0, 0, true);
4806 : : }
4807 : :
4808 : :
4809 : : static const char *
4810 : 3736 : dwarf_lang_name (unsigned int lang)
4811 : : {
4812 : 3736 : const char *ret = dwarf_lang_string (lang);
4813 : 3736 : return string_or_unknown (ret, lang, DW_LANG_lo_user, DW_LANG_hi_user, false);
4814 : : }
4815 : :
4816 : :
4817 : : static const char *
4818 : 4 : dwarf_lname_name (unsigned int lname)
4819 : : {
4820 : 4 : const char *ret = dwarf_lname_string (lname);
4821 : 4 : return string_or_unknown (ret, lname, DW_LNAME_lo_user, DW_LNAME_hi_user,
4822 : : false);
4823 : : }
4824 : :
4825 : :
4826 : : static const char *
4827 : 7280 : dwarf_inline_name (unsigned int code)
4828 : : {
4829 [ + - ]: 7280 : const char *ret = dwarf_inline_string (code);
4830 : 7280 : return string_or_unknown (ret, code, 0, 0, false);
4831 : : }
4832 : :
4833 : :
4834 : : static const char *
4835 : 59402 : dwarf_encoding_name (unsigned int code)
4836 : : {
4837 [ + - ]: 59402 : const char *ret = dwarf_encoding_string (code);
4838 : 59402 : return string_or_unknown (ret, code, DW_ATE_lo_user, DW_ATE_hi_user, false);
4839 : : }
4840 : :
4841 : :
4842 : : static const char *
4843 : 0 : dwarf_access_name (unsigned int code)
4844 : : {
4845 [ # # ]: 0 : const char *ret = dwarf_access_string (code);
4846 : 0 : return string_or_unknown (ret, code, 0, 0, false);
4847 : : }
4848 : :
4849 : :
4850 : : static const char *
4851 : 0 : dwarf_defaulted_name (unsigned int code)
4852 : : {
4853 [ # # ]: 0 : const char *ret = dwarf_defaulted_string (code);
4854 : 0 : return string_or_unknown (ret, code, 0, 0, false);
4855 : : }
4856 : :
4857 : :
4858 : : static const char *
4859 : 0 : dwarf_visibility_name (unsigned int code)
4860 : : {
4861 [ # # ]: 0 : const char *ret = dwarf_visibility_string (code);
4862 : 0 : return string_or_unknown (ret, code, 0, 0, false);
4863 : : }
4864 : :
4865 : :
4866 : : static const char *
4867 : 0 : dwarf_virtuality_name (unsigned int code)
4868 : : {
4869 [ # # ]: 0 : const char *ret = dwarf_virtuality_string (code);
4870 : 0 : return string_or_unknown (ret, code, 0, 0, false);
4871 : : }
4872 : :
4873 : :
4874 : : static const char *
4875 : 0 : dwarf_identifier_case_name (unsigned int code)
4876 : : {
4877 [ # # ]: 0 : const char *ret = dwarf_identifier_case_string (code);
4878 : 0 : return string_or_unknown (ret, code, 0, 0, false);
4879 : : }
4880 : :
4881 : :
4882 : : static const char *
4883 : 0 : dwarf_calling_convention_name (unsigned int code)
4884 : : {
4885 [ # # ]: 0 : const char *ret = dwarf_calling_convention_string (code);
4886 : 0 : return string_or_unknown (ret, code, DW_CC_lo_user, DW_CC_hi_user, false);
4887 : : }
4888 : :
4889 : :
4890 : : static const char *
4891 : 0 : dwarf_ordering_name (unsigned int code)
4892 : : {
4893 [ # # ]: 0 : const char *ret = dwarf_ordering_string (code);
4894 : 0 : return string_or_unknown (ret, code, 0, 0, false);
4895 : : }
4896 : :
4897 : :
4898 : : static const char *
4899 : 16 : dwarf_discr_list_name (unsigned int code)
4900 : : {
4901 [ + - ]: 16 : const char *ret = dwarf_discr_list_string (code);
4902 : 16 : return string_or_unknown (ret, code, 0, 0, false);
4903 : : }
4904 : :
4905 : :
4906 : : static const char *
4907 : 3486 : dwarf_unit_name (unsigned int type)
4908 : : {
4909 : 3486 : const char *ret = dwarf_unit_string (type);
4910 : 3486 : return string_or_unknown (ret, type, DW_UT_lo_user, DW_UT_hi_user, true);
4911 : : }
4912 : :
4913 : :
4914 : : static const char *
4915 : 148364 : dwarf_range_list_encoding_name (unsigned int kind)
4916 : : {
4917 : 148364 : const char *ret = dwarf_range_list_encoding_string (kind);
4918 : 148364 : return string_or_unknown (ret, kind, 0, 0, false);
4919 : : }
4920 : :
4921 : :
4922 : : static const char *
4923 : 653748 : dwarf_loc_list_encoding_name (unsigned int kind)
4924 : : {
4925 : 653748 : const char *ret = dwarf_loc_list_encoding_string (kind);
4926 : 653748 : return string_or_unknown (ret, kind, 0, 0, false);
4927 : : }
4928 : :
4929 : :
4930 : : static const char *
4931 : 10418 : dwarf_line_content_description_name (unsigned int kind)
4932 : : {
4933 : 10418 : const char *ret = dwarf_line_content_description_string (kind);
4934 : 10418 : return string_or_unknown (ret, kind, DW_LNCT_lo_user, DW_LNCT_hi_user,
4935 : : false);
4936 : : }
4937 : :
4938 : :
4939 : : static void
4940 : 510 : print_block (size_t n, const void *block, FILE *out)
4941 : : {
4942 [ - + ]: 510 : if (n == 0)
4943 : 0 : fputs (_("empty block\n"), out);
4944 : : else
4945 : : {
4946 : 510 : fprintf (out, _("%zu byte block:"), n);
4947 : 510 : const unsigned char *data = block;
4948 : 2250 : do
4949 : 2250 : fprintf (out, " %02x", *data++);
4950 [ + + ]: 2250 : while (--n > 0);
4951 : 510 : fputc ('\n', out);
4952 : : }
4953 : 510 : }
4954 : :
4955 : : static void
4956 : 24 : print_bytes (size_t n, const unsigned char *bytes, FILE *out)
4957 : : {
4958 [ + + ]: 408 : while (n-- > 0)
4959 : : {
4960 : 384 : fprintf (out, "%02x", *bytes++);
4961 [ + + ]: 384 : if (n > 0)
4962 : 360 : fprintf (out, " ");
4963 : : }
4964 : 24 : }
4965 : :
4966 : : static int
4967 : 148 : get_indexed_addr (Dwarf_CU *cu, Dwarf_Word idx, Dwarf_Addr *addr)
4968 : : {
4969 [ - + ]: 148 : if (cu == NULL)
4970 : : return -1;
4971 : :
4972 : 148 : Elf_Data *debug_addr = cu->dbg->sectiondata[IDX_debug_addr];
4973 [ - + ]: 148 : if (debug_addr == NULL)
4974 : : return -1;
4975 : :
4976 : 148 : Dwarf_Off base = __libdw_cu_addr_base (cu);
4977 : 148 : Dwarf_Word off = idx * cu->address_size;
4978 [ - + ]: 148 : if (base > debug_addr->d_size
4979 [ - + ]: 148 : || off > debug_addr->d_size - base
4980 [ - + ]: 148 : || cu->address_size > debug_addr->d_size - base - off)
4981 : : return -1;
4982 : :
4983 : 148 : const unsigned char *addrp = debug_addr->d_buf + base + off;
4984 [ + + ]: 148 : if (cu->address_size == 4)
4985 [ - + ]: 12 : *addr = read_4ubyte_unaligned (cu->dbg, addrp);
4986 : : else
4987 [ - + ]: 136 : *addr = read_8ubyte_unaligned (cu->dbg, addrp);
4988 : :
4989 : : return 0;
4990 : : }
4991 : :
4992 : : static void
4993 : 784658 : print_ops (Dwfl_Module *dwflmod, Dwarf *dbg, int indent, int indentrest,
4994 : : unsigned int vers, unsigned int addrsize, unsigned int offset_size,
4995 : : struct Dwarf_CU *cu, Dwarf_Word len, const unsigned char *data,
4996 : : FILE *out)
4997 : : {
4998 [ + + ]: 784658 : const unsigned int ref_size = vers < 3 ? addrsize : offset_size;
4999 : :
5000 [ - + ]: 784658 : if (len == 0)
5001 : : {
5002 : 0 : fprintf (out, "%*s(empty)\n", indent, "");
5003 : 0 : return;
5004 : : }
5005 : :
5006 : : #define NEED(n) if (len < (Dwarf_Word) (n)) goto invalid
5007 : : #define CONSUME(n) NEED (n); else len -= (n)
5008 : :
5009 : : Dwarf_Word offset = 0;
5010 [ + + ]: 1900526 : while (len-- > 0)
5011 : 1115868 : {
5012 : 1115868 : uint_fast8_t op = *data++;
5013 : :
5014 [ + - ]: 1115868 : const char *op_name = dwarf_locexpr_opcode_string (op);
5015 [ - + ]: 1115868 : if (unlikely (op_name == NULL))
5016 : : {
5017 : 0 : static char buf[20];
5018 [ # # ]: 0 : if (op >= DW_OP_lo_user)
5019 : 0 : snprintf (buf, sizeof buf, "lo_user+%#x", op - DW_OP_lo_user);
5020 : : else
5021 : 0 : snprintf (buf, sizeof buf, "??? (%#x)", op);
5022 : : op_name = buf;
5023 : : }
5024 : :
5025 [ + - + + : 1115868 : switch (op)
+ + + + -
- + + - +
- - + + +
+ + - - -
- + + + ]
5026 : : {
5027 : 27000 : case DW_OP_addr:;
5028 : : /* Address operand. */
5029 : 27000 : Dwarf_Word addr;
5030 [ - + ]: 27000 : NEED (addrsize);
5031 [ + + ]: 27000 : if (addrsize == 4)
5032 [ + + ]: 108 : addr = read_4ubyte_unaligned (dbg, data);
5033 [ + - ]: 26892 : else if (addrsize == 8)
5034 [ + + ]: 26892 : addr = read_8ubyte_unaligned (dbg, data);
5035 : : else
5036 : 0 : goto invalid;
5037 : 27000 : data += addrsize;
5038 : 27000 : CONSUME (addrsize);
5039 : :
5040 : 27000 : fprintf (out, "%*s[%2" PRIuMAX "] %s ",
5041 : : indent, "", (uintmax_t) offset, op_name);
5042 : 27000 : print_dwarf_addr (dwflmod, 0, addr, addr, out);
5043 : 27000 : fprintf (out, "\n");
5044 : :
5045 : 27000 : offset += 1 + addrsize;
5046 : 27000 : break;
5047 : :
5048 : 0 : case DW_OP_call_ref:
5049 : : case DW_OP_GNU_variable_value:
5050 : : /* Offset operand. */
5051 [ # # ]: 0 : if (ref_size != 4 && ref_size != 8)
5052 : 0 : goto invalid; /* Cannot be used in CFA. */
5053 [ # # ]: 0 : NEED (ref_size);
5054 [ # # ]: 0 : if (ref_size == 4)
5055 [ # # ]: 0 : addr = read_4ubyte_unaligned (dbg, data);
5056 : : else
5057 [ # # ]: 0 : addr = read_8ubyte_unaligned (dbg, data);
5058 : 0 : data += ref_size;
5059 : 0 : CONSUME (ref_size);
5060 : : /* addr is a DIE offset, so format it as one. */
5061 : 0 : fprintf (out, "%*s[%2" PRIuMAX "] %s [%6" PRIxMAX "]\n",
5062 : : indent, "", (uintmax_t) offset,
5063 : : op_name, (uintmax_t) addr);
5064 : 0 : offset += 1 + ref_size;
5065 : 0 : break;
5066 : :
5067 : 24770 : case DW_OP_deref_size:
5068 : : case DW_OP_xderef_size:
5069 : : case DW_OP_pick:
5070 : : case DW_OP_const1u:
5071 : : // XXX value might be modified by relocation
5072 [ - + ]: 24770 : NEED (1);
5073 : 49540 : fprintf (out, "%*s[%2" PRIuMAX "] %s %" PRIu8 "\n",
5074 : : indent, "", (uintmax_t) offset,
5075 : 24770 : op_name, *((uint8_t *) data));
5076 : 24770 : ++data;
5077 : 24770 : --len;
5078 : 24770 : offset += 2;
5079 : 24770 : break;
5080 : :
5081 : 3318 : case DW_OP_const2u:
5082 [ - + ]: 3318 : NEED (2);
5083 : : // XXX value might be modified by relocation
5084 : 6636 : fprintf (out, "%*s[%2" PRIuMAX "] %s %" PRIu16 "\n",
5085 : : indent, "", (uintmax_t) offset,
5086 [ - + ]: 3318 : op_name, read_2ubyte_unaligned (dbg, data));
5087 : 3318 : CONSUME (2);
5088 : 3318 : data += 2;
5089 : 3318 : offset += 3;
5090 : 3318 : break;
5091 : :
5092 : 2906 : case DW_OP_const4u:
5093 [ - + ]: 2906 : NEED (4);
5094 : : // XXX value might be modified by relocation
5095 : 2906 : fprintf (out, "%*s[%2" PRIuMAX "] %s %" PRIu32 "\n",
5096 : : indent, "", (uintmax_t) offset,
5097 [ - + ]: 2906 : op_name, read_4ubyte_unaligned (dbg, data));
5098 : 2906 : CONSUME (4);
5099 : 2906 : data += 4;
5100 : 2906 : offset += 5;
5101 : 2906 : break;
5102 : :
5103 : 126 : case DW_OP_const8u:
5104 [ - + ]: 126 : NEED (8);
5105 : : // XXX value might be modified by relocation
5106 : 126 : fprintf (out, "%*s[%2" PRIuMAX "] %s %" PRIu64 "\n",
5107 : : indent, "", (uintmax_t) offset,
5108 [ - + ]: 126 : op_name, (uint64_t) read_8ubyte_unaligned (dbg, data));
5109 : 126 : CONSUME (8);
5110 : 126 : data += 8;
5111 : 126 : offset += 9;
5112 : 126 : break;
5113 : :
5114 : 5132 : case DW_OP_const1s:
5115 [ - + ]: 5132 : NEED (1);
5116 : : // XXX value might be modified by relocation
5117 : 10264 : fprintf (out, "%*s[%2" PRIuMAX "] %s %" PRId8 "\n",
5118 : : indent, "", (uintmax_t) offset,
5119 : 5132 : op_name, *((int8_t *) data));
5120 : 5132 : ++data;
5121 : 5132 : --len;
5122 : 5132 : offset += 2;
5123 : 5132 : break;
5124 : :
5125 : 10 : case DW_OP_const2s:
5126 [ - + ]: 10 : NEED (2);
5127 : : // XXX value might be modified by relocation
5128 : 20 : fprintf (out, "%*s[%2" PRIuMAX "] %s %" PRId16 "\n",
5129 : : indent, "", (uintmax_t) offset,
5130 [ - + ]: 10 : op_name, read_2sbyte_unaligned (dbg, data));
5131 : 10 : CONSUME (2);
5132 : 10 : data += 2;
5133 : 10 : offset += 3;
5134 : 10 : break;
5135 : :
5136 : 0 : case DW_OP_const4s:
5137 [ # # ]: 0 : NEED (4);
5138 : : // XXX value might be modified by relocation
5139 : 0 : fprintf (out, "%*s[%2" PRIuMAX "] %s %" PRId32 "\n",
5140 : : indent, "", (uintmax_t) offset,
5141 [ # # ]: 0 : op_name, read_4sbyte_unaligned (dbg, data));
5142 : 0 : CONSUME (4);
5143 : 0 : data += 4;
5144 : 0 : offset += 5;
5145 : 0 : break;
5146 : :
5147 : 0 : case DW_OP_const8s:
5148 [ # # ]: 0 : NEED (8);
5149 : : // XXX value might be modified by relocation
5150 : 0 : fprintf (out, "%*s[%2" PRIuMAX "] %s %" PRId64 "\n",
5151 : : indent, "", (uintmax_t) offset,
5152 [ # # ]: 0 : op_name, read_8sbyte_unaligned (dbg, data));
5153 : 0 : CONSUME (8);
5154 : 0 : data += 8;
5155 : 0 : offset += 9;
5156 : 0 : break;
5157 : :
5158 : 25366 : case DW_OP_piece:
5159 : : case DW_OP_regx:
5160 : : case DW_OP_plus_uconst:
5161 : 25366 : case DW_OP_constu:;
5162 : 25366 : const unsigned char *start = data;
5163 : 25366 : uint64_t uleb;
5164 [ - + ]: 25366 : NEED (1);
5165 : 25366 : get_uleb128 (uleb, data, data + len);
5166 : 25366 : fprintf (out, "%*s[%2" PRIuMAX "] %s %" PRIu64 "\n",
5167 : : indent, "", (uintmax_t) offset, op_name, uleb);
5168 [ - + ]: 25366 : CONSUME (data - start);
5169 : 25366 : offset += 1 + (data - start);
5170 : 25366 : break;
5171 : :
5172 : 16 : case DW_OP_addrx:
5173 : : case DW_OP_GNU_addr_index:
5174 : : case DW_OP_constx:
5175 : 16 : case DW_OP_GNU_const_index:;
5176 : 16 : start = data;
5177 [ - + ]: 16 : NEED (1);
5178 : 16 : get_uleb128 (uleb, data, data + len);
5179 : 16 : fprintf (out, "%*s[%2" PRIuMAX "] %s [%" PRIu64 "] ",
5180 : : indent, "", (uintmax_t) offset, op_name, uleb);
5181 [ - + ]: 16 : CONSUME (data - start);
5182 : 16 : offset += 1 + (data - start);
5183 [ - + ]: 16 : if (get_indexed_addr (cu, uleb, &addr) != 0)
5184 : 0 : fprintf (out, "???\n");
5185 : : else
5186 : : {
5187 : 16 : print_dwarf_addr (dwflmod, 0, addr, addr, out);
5188 : 16 : fprintf (out, "\n");
5189 : : }
5190 : : break;
5191 : :
5192 : 0 : case DW_OP_bit_piece:
5193 : 0 : start = data;
5194 : 0 : uint64_t uleb2;
5195 [ # # ]: 0 : NEED (1);
5196 : 0 : get_uleb128 (uleb, data, data + len);
5197 : 0 : NEED (1);
5198 : 0 : get_uleb128 (uleb2, data, data + len);
5199 : 0 : fprintf (out, "%*s[%2" PRIuMAX "] %s %" PRIu64 ", %" PRIu64 "\n",
5200 : : indent, "", (uintmax_t) offset, op_name, uleb, uleb2);
5201 [ # # ]: 0 : CONSUME (data - start);
5202 : 0 : offset += 1 + (data - start);
5203 : 0 : break;
5204 : :
5205 : 188808 : case DW_OP_fbreg:
5206 : : case DW_OP_breg0 ... DW_OP_breg31:
5207 : : case DW_OP_consts:
5208 : 188808 : start = data;
5209 : 188808 : int64_t sleb;
5210 [ - + ]: 188808 : NEED (1);
5211 : 188808 : get_sleb128 (sleb, data, data + len);
5212 : 188808 : fprintf (out, "%*s[%2" PRIuMAX "] %s %" PRId64 "\n",
5213 : : indent, "", (uintmax_t) offset, op_name, sleb);
5214 [ - + ]: 188808 : CONSUME (data - start);
5215 : 188808 : offset += 1 + (data - start);
5216 : 188808 : break;
5217 : :
5218 : 0 : case DW_OP_bregx:
5219 : 0 : start = data;
5220 [ # # ]: 0 : NEED (1);
5221 : 0 : get_uleb128 (uleb, data, data + len);
5222 : 0 : NEED (1);
5223 : 0 : get_sleb128 (sleb, data, data + len);
5224 : 0 : fprintf (out, "%*s[%2" PRIuMAX "] %s %" PRIu64 " %" PRId64 "\n",
5225 : : indent, "", (uintmax_t) offset, op_name, uleb, sleb);
5226 [ # # ]: 0 : CONSUME (data - start);
5227 : 0 : offset += 1 + (data - start);
5228 : 0 : break;
5229 : :
5230 : 0 : case DW_OP_call2:
5231 [ # # ]: 0 : NEED (2);
5232 : 0 : fprintf (out, "%*s[%2" PRIuMAX "] %s [%6" PRIx16 "]\n",
5233 : : indent, "", (uintmax_t) offset, op_name,
5234 [ # # ]: 0 : read_2ubyte_unaligned (dbg, data));
5235 : 0 : CONSUME (2);
5236 : 0 : data += 2;
5237 : 0 : offset += 3;
5238 : 0 : break;
5239 : :
5240 : 8 : case DW_OP_call4:
5241 [ - + ]: 8 : NEED (4);
5242 : 8 : fprintf (out, "%*s[%2" PRIuMAX "] %s [%6" PRIx32 "]\n",
5243 : : indent, "", (uintmax_t) offset, op_name,
5244 [ - + ]: 8 : read_4ubyte_unaligned (dbg, data));
5245 : 8 : CONSUME (4);
5246 : 8 : data += 4;
5247 : 8 : offset += 5;
5248 : 8 : break;
5249 : :
5250 : 1120 : case DW_OP_skip:
5251 : : case DW_OP_bra:
5252 [ - + ]: 1120 : NEED (2);
5253 : 2240 : fprintf (out, "%*s[%2" PRIuMAX "] %s %" PRIuMAX "\n",
5254 : : indent, "", (uintmax_t) offset, op_name,
5255 [ - + ]: 1120 : (uintmax_t) (offset + read_2sbyte_unaligned (dbg, data) + 3));
5256 : 1120 : CONSUME (2);
5257 : 1120 : data += 2;
5258 : 1120 : offset += 3;
5259 : 1120 : break;
5260 : :
5261 : 510 : case DW_OP_implicit_value:
5262 : 510 : start = data;
5263 [ - + ]: 510 : NEED (1);
5264 : 510 : get_uleb128 (uleb, data, data + len);
5265 : 510 : fprintf (out, "%*s[%2" PRIuMAX "] %s: ",
5266 : : indent, "", (uintmax_t) offset, op_name);
5267 [ - + ]: 510 : NEED (uleb);
5268 : 510 : print_block (uleb, data, out);
5269 : 510 : data += uleb;
5270 [ - + ]: 510 : CONSUME (data - start);
5271 : 510 : offset += 1 + (data - start);
5272 : 510 : break;
5273 : :
5274 : 5912 : case DW_OP_implicit_pointer:
5275 : : case DW_OP_GNU_implicit_pointer:
5276 : : /* DIE offset operand. */
5277 : 5912 : start = data;
5278 [ - + ]: 5912 : NEED (ref_size);
5279 [ - + ]: 5912 : if (ref_size != 4 && ref_size != 8)
5280 : 0 : goto invalid; /* Cannot be used in CFA. */
5281 [ + - ]: 5912 : if (ref_size == 4)
5282 [ - + ]: 5912 : addr = read_4ubyte_unaligned (dbg, data);
5283 : : else
5284 [ # # ]: 0 : addr = read_8ubyte_unaligned (dbg, data);
5285 : 5912 : data += ref_size;
5286 : : /* Byte offset operand. */
5287 [ - + ]: 5912 : NEED (1);
5288 : 5912 : get_sleb128 (sleb, data, data + len);
5289 : :
5290 : 5912 : fprintf (out, "%*s[%2" PRIuMAX "] %s [%6" PRIxMAX "] %+" PRId64 "\n",
5291 : : indent, "", (intmax_t) offset,
5292 : : op_name, (uintmax_t) addr, sleb);
5293 [ - + ]: 5912 : CONSUME (data - start);
5294 : 5912 : offset += 1 + (data - start);
5295 : 5912 : break;
5296 : :
5297 : 62514 : case DW_OP_entry_value:
5298 : : case DW_OP_GNU_entry_value:
5299 : : /* Size plus expression block. */
5300 : 62514 : start = data;
5301 [ - + ]: 62514 : NEED (1);
5302 : 62514 : get_uleb128 (uleb, data, data + len);
5303 : 62514 : fprintf (out, "%*s[%2" PRIuMAX "] %s:\n",
5304 : : indent, "", (uintmax_t) offset, op_name);
5305 [ - + ]: 62514 : NEED (uleb);
5306 : 62514 : print_ops (dwflmod, dbg, indent + 5, indent + 5, vers,
5307 : : addrsize, offset_size, cu, uleb, data, out);
5308 : 62514 : data += uleb;
5309 [ - + ]: 62514 : CONSUME (data - start);
5310 : 62514 : offset += 1 + (data - start);
5311 : 62514 : break;
5312 : :
5313 : 0 : case DW_OP_const_type:
5314 : : case DW_OP_GNU_const_type:
5315 : : /* uleb128 CU relative DW_TAG_base_type DIE offset, 1-byte
5316 : : unsigned size plus block. */
5317 : 0 : start = data;
5318 [ # # ]: 0 : NEED (1);
5319 : 0 : get_uleb128 (uleb, data, data + len);
5320 [ # # # # ]: 0 : if (! print_unresolved_addresses && cu != NULL)
5321 : 0 : uleb += cu->start;
5322 : 0 : NEED (1);
5323 : 0 : uint8_t usize = *(uint8_t *) data++;
5324 [ # # ]: 0 : NEED (usize);
5325 : 0 : fprintf (out, "%*s[%2" PRIuMAX "] %s [%6" PRIxMAX "] ",
5326 : : indent, "", (uintmax_t) offset, op_name, uleb);
5327 : 0 : print_block (usize, data, out);
5328 : 0 : data += usize;
5329 [ # # ]: 0 : CONSUME (data - start);
5330 : 0 : offset += 1 + (data - start);
5331 : 0 : break;
5332 : :
5333 : 0 : case DW_OP_regval_type:
5334 : : case DW_OP_GNU_regval_type:
5335 : : /* uleb128 register number, uleb128 CU relative
5336 : : DW_TAG_base_type DIE offset. */
5337 : 0 : start = data;
5338 [ # # ]: 0 : NEED (1);
5339 : 0 : get_uleb128 (uleb, data, data + len);
5340 : 0 : NEED (1);
5341 : 0 : get_uleb128 (uleb2, data, data + len);
5342 [ # # # # ]: 0 : if (! print_unresolved_addresses && cu != NULL)
5343 : 0 : uleb2 += cu->start;
5344 : 0 : fprintf (out, "%*s[%2" PRIuMAX "] %s %" PRIu64 " [%6" PRIx64 "]\n",
5345 : : indent, "", (uintmax_t) offset, op_name, uleb, uleb2);
5346 [ # # ]: 0 : CONSUME (data - start);
5347 : 0 : offset += 1 + (data - start);
5348 : 0 : break;
5349 : :
5350 : 0 : case DW_OP_deref_type:
5351 : : case DW_OP_GNU_deref_type:
5352 : : /* 1-byte unsigned size of value, uleb128 CU relative
5353 : : DW_TAG_base_type DIE offset. */
5354 : 0 : start = data;
5355 [ # # ]: 0 : NEED (1);
5356 : 0 : usize = *(uint8_t *) data++;
5357 : 0 : NEED (1);
5358 : 0 : get_uleb128 (uleb, data, data + len);
5359 [ # # # # ]: 0 : if (! print_unresolved_addresses && cu != NULL)
5360 : 0 : uleb += cu->start;
5361 : 0 : fprintf (out, "%*s[%2" PRIuMAX "] %s %" PRIu8 " [%6" PRIxMAX "]\n",
5362 : : indent, "", (uintmax_t) offset,
5363 : : op_name, usize, uleb);
5364 [ # # ]: 0 : CONSUME (data - start);
5365 : 0 : offset += 1 + (data - start);
5366 : 0 : break;
5367 : :
5368 : 0 : case DW_OP_xderef_type:
5369 : : /* 1-byte unsigned size of value, uleb128 base_type DIE offset. */
5370 : 0 : start = data;
5371 [ # # ]: 0 : NEED (1);
5372 : 0 : usize = *(uint8_t *) data++;
5373 : 0 : NEED (1);
5374 : 0 : get_uleb128 (uleb, data, data + len);
5375 : 0 : fprintf (out, "%*s[%4" PRIuMAX "] %s %" PRIu8 " [%6" PRIxMAX "]\n",
5376 : : indent, "", (uintmax_t) offset,
5377 : : op_name, usize, uleb);
5378 [ # # ]: 0 : CONSUME (data - start);
5379 : 0 : offset += 1 + (data - start);
5380 : 0 : break;
5381 : :
5382 : 786 : case DW_OP_convert:
5383 : : case DW_OP_GNU_convert:
5384 : : case DW_OP_reinterpret:
5385 : : case DW_OP_GNU_reinterpret:
5386 : : /* uleb128 CU relative offset to DW_TAG_base_type, or zero
5387 : : for conversion to untyped. */
5388 : 786 : start = data;
5389 [ - + ]: 786 : NEED (1);
5390 : 786 : get_uleb128 (uleb, data, data + len);
5391 [ + + + - : 786 : if (uleb != 0 && ! print_unresolved_addresses && cu != NULL)
+ - ]
5392 : 524 : uleb += cu->start;
5393 : 786 : fprintf (out, "%*s[%2" PRIuMAX "] %s [%6" PRIxMAX "]\n",
5394 : : indent, "", (uintmax_t) offset, op_name, uleb);
5395 [ - + ]: 786 : CONSUME (data - start);
5396 : 786 : offset += 1 + (data - start);
5397 : 786 : break;
5398 : :
5399 : 8 : case DW_OP_GNU_parameter_ref:
5400 : : /* 4 byte CU relative reference to the abstract optimized away
5401 : : DW_TAG_formal_parameter. */
5402 [ - + ]: 8 : NEED (4);
5403 [ - + ]: 8 : uintmax_t param_off = (uintmax_t) read_4ubyte_unaligned (dbg, data);
5404 [ + - + - ]: 8 : if (! print_unresolved_addresses && cu != NULL)
5405 : 8 : param_off += cu->start;
5406 : 8 : fprintf (out, "%*s[%2" PRIuMAX "] %s [%6" PRIxMAX "]\n",
5407 : : indent, "", (uintmax_t) offset, op_name, param_off);
5408 : 8 : CONSUME (4);
5409 : 8 : data += 4;
5410 : 8 : offset += 5;
5411 : 8 : break;
5412 : :
5413 : : default:
5414 : : /* No Operand. */
5415 : 767558 : fprintf (out, "%*s[%2" PRIuMAX "] %s\n",
5416 : : indent, "", (uintmax_t) offset, op_name);
5417 : 767558 : ++offset;
5418 : 767558 : break;
5419 : : }
5420 : :
5421 : 1115868 : indent = indentrest;
5422 : 1115868 : continue;
5423 : :
5424 : 0 : invalid:
5425 : 0 : fprintf (out, _("%*s[%2" PRIuMAX "] %s <TRUNCATED>\n"),
5426 : : indent, "", (uintmax_t) offset, op_name);
5427 : 0 : break;
5428 : : }
5429 : : }
5430 : :
5431 : :
5432 : : /* Turn the addresses into file offsets by using the phdrs. */
5433 : : static void
5434 : 2 : find_offsets(Elf *elf, GElf_Addr main_bias, size_t n,
5435 : : GElf_Addr addrs[n], GElf_Off offs[n])
5436 : : {
5437 : 2 : size_t unsolved = n;
5438 [ + + ]: 18 : for (size_t i = 0; i < phnum; ++i) {
5439 : 16 : GElf_Phdr phdr_mem;
5440 : 16 : GElf_Phdr *phdr = gelf_getphdr(elf, i, &phdr_mem);
5441 [ + - + + : 16 : if (phdr != NULL && phdr->p_type == PT_LOAD && phdr->p_memsz > 0)
+ - ]
5442 [ + + ]: 46 : for (size_t j = 0; j < n; ++j)
5443 [ + + + + ]: 44 : if (offs[j] == 0 && addrs[j] >= phdr->p_vaddr + main_bias &&
5444 [ + - ]: 22 : addrs[j] - (phdr->p_vaddr + main_bias) < phdr->p_filesz) {
5445 : 22 : offs[j] = addrs[j] - (phdr->p_vaddr + main_bias) + phdr->p_offset;
5446 [ + + ]: 22 : if (--unsolved == 0)
5447 : : break;
5448 : : }
5449 : : }
5450 : 2 : }
5451 : :
5452 : : /* The dynamic segment (type PT_DYNAMIC), contains the .dynamic section.
5453 : : And .dynamic section contains an array of the dynamic structures.
5454 : : We use the array to get:
5455 : : DT_STRTAB: the address of the string table
5456 : : DT_SYMTAB: the address of the symbol table
5457 : : DT_STRSZ: the size, in bytes, of the string table
5458 : : ... */
5459 : : static void
5460 : 2 : get_dynscn_addrs(Elf *elf, GElf_Phdr *phdr, GElf_Addr addrs[i_max])
5461 : : {
5462 : 2 : Elf_Data *data = elf_getdata_rawchunk(
5463 : 2 : elf, phdr->p_offset, phdr->p_filesz, ELF_T_DYN);
5464 : :
5465 : 2 : int dyn_idx = 0;
5466 : 44 : for (;; ++dyn_idx) {
5467 : 46 : GElf_Dyn dyn_mem;
5468 : 46 : GElf_Dyn *dyn = gelf_getdyn(data, dyn_idx, &dyn_mem);
5469 : : /* DT_NULL Marks end of dynamic section. */
5470 [ + - + + ]: 46 : if (dyn == NULL || dyn->d_tag == DT_NULL)
5471 : : break;
5472 : :
5473 [ + - + + : 44 : switch (dyn->d_tag) {
+ + + + +
+ - + ]
5474 : 2 : case DT_SYMTAB:
5475 : 2 : addrs[i_symtab] = dyn->d_un.d_ptr;
5476 : 2 : break;
5477 : :
5478 : 0 : case DT_HASH:
5479 : 0 : addrs[i_hash] = dyn->d_un.d_ptr;
5480 : 0 : break;
5481 : :
5482 : 2 : case DT_GNU_HASH:
5483 : 2 : addrs[i_gnu_hash] = dyn->d_un.d_ptr;
5484 : 2 : break;
5485 : :
5486 : 2 : case DT_STRTAB:
5487 : 2 : addrs[i_strtab] = dyn->d_un.d_ptr;
5488 : 2 : break;
5489 : :
5490 : 2 : case DT_VERSYM:
5491 : 2 : addrs[i_versym] = dyn->d_un.d_ptr;
5492 : 2 : break;
5493 : :
5494 : 2 : case DT_VERDEF:
5495 : 2 : addrs[i_verdef] = dyn->d_un.d_ptr;
5496 : 2 : break;
5497 : :
5498 : 2 : case DT_VERDEFNUM:
5499 : 2 : addrs[i_verdefnum] = dyn->d_un.d_val;
5500 : 2 : break;
5501 : :
5502 : 2 : case DT_VERNEED:
5503 : 2 : addrs[i_verneed] = dyn->d_un.d_ptr;
5504 : 2 : break;
5505 : :
5506 : 2 : case DT_VERNEEDNUM:
5507 : 2 : addrs[i_verneednum] = dyn->d_un.d_val;
5508 : 2 : break;
5509 : :
5510 : 2 : case DT_STRSZ:
5511 : 2 : addrs[i_strsz] = dyn->d_un.d_val;
5512 : 2 : break;
5513 : :
5514 : 0 : case DT_SYMTAB_SHNDX:
5515 : 0 : addrs[i_symtab_shndx] = dyn->d_un.d_ptr;
5516 : 0 : break;
5517 : : }
5518 : : }
5519 : 2 : }
5520 : :
5521 : :
5522 : : /* Use dynamic segment to get data for the string table section. */
5523 : : static Elf_Data *
5524 : 2 : get_dynscn_strtab(Elf *elf, GElf_Phdr *phdr)
5525 : : {
5526 : 2 : Elf_Data *strtab_data;
5527 : 2 : GElf_Addr addrs[i_max] = {0,};
5528 : 2 : GElf_Off offs[i_max] = {0,};
5529 : 2 : get_dynscn_addrs(elf, phdr, addrs);
5530 : 2 : find_offsets(elf, 0, i_max, addrs, offs);
5531 : 4 : strtab_data = elf_getdata_rawchunk(
5532 : 2 : elf, offs[i_strtab], addrs[i_strsz], ELF_T_BYTE);
5533 : 2 : return strtab_data;
5534 : : }
5535 : :
5536 : :
5537 : : struct listptr
5538 : : {
5539 : : Dwarf_Off offset:(64 - 3);
5540 : : bool addr64:1;
5541 : : bool dwarf64:1;
5542 : : bool warned:1;
5543 : : struct Dwarf_CU *cu;
5544 : : unsigned int attr;
5545 : : };
5546 : :
5547 : : #define listptr_offset_size(p) ((p)->dwarf64 ? 8 : 4)
5548 : : #define listptr_address_size(p) ((p)->addr64 ? 8 : 4)
5549 : :
5550 : : static Dwarf_Addr
5551 : 21758 : cudie_base (Dwarf_Die *cudie)
5552 : : {
5553 : 21758 : Dwarf_Addr base;
5554 : : /* Find the base address of the compilation unit. It will normally
5555 : : be specified by DW_AT_low_pc. In DWARF-3 draft 4, the base
5556 : : address could be overridden by DW_AT_entry_pc. It's been
5557 : : removed, but GCC emits DW_AT_entry_pc and not DW_AT_lowpc for
5558 : : compilation units with discontinuous ranges. */
5559 [ - + ]: 21758 : if (unlikely (dwarf_lowpc (cudie, &base) != 0))
5560 : : {
5561 : 0 : Dwarf_Attribute attr_mem;
5562 [ # # ]: 0 : if (dwarf_formaddr (dwarf_attr (cudie, DW_AT_entry_pc, &attr_mem),
5563 : : &base) != 0)
5564 : 0 : base = 0;
5565 : : }
5566 : 21758 : return base;
5567 : : }
5568 : :
5569 : : static Dwarf_Addr
5570 : 21758 : listptr_base (struct listptr *p)
5571 : : {
5572 : 21758 : Dwarf_Die cu = CUDIE (p->cu);
5573 : 21758 : return cudie_base (&cu);
5574 : : }
5575 : :
5576 : : /* To store the name used in compare_listptr */
5577 : : static _Thread_local const char *sort_listptr_name;
5578 : :
5579 : : static int
5580 : 1788800 : compare_listptr (const void *a, const void *b)
5581 : : {
5582 : 1788800 : const char *name = sort_listptr_name;
5583 : 1788800 : struct listptr *p1 = (void *) a;
5584 : 1788800 : struct listptr *p2 = (void *) b;
5585 : :
5586 [ + + ]: 1788800 : if (p1->offset < p2->offset)
5587 : : return -1;
5588 [ + + ]: 176082 : if (p1->offset > p2->offset)
5589 : : return 1;
5590 : :
5591 [ + - - + ]: 8210 : if (!p1->warned && !p2->warned)
5592 : : {
5593 [ - + ]: 8210 : if (p1->addr64 != p2->addr64)
5594 : : {
5595 : 0 : p1->warned = p2->warned = true;
5596 : 0 : error (0, 0,
5597 : 0 : _("%s %#" PRIx64 " used with different address sizes"),
5598 : : name, (uint64_t) p1->offset);
5599 : : }
5600 [ - + ]: 8210 : if (p1->dwarf64 != p2->dwarf64)
5601 : : {
5602 : 0 : p1->warned = p2->warned = true;
5603 : 0 : error (0, 0,
5604 : 0 : _("%s %#" PRIx64 " used with different offset sizes"),
5605 : 0 : name, (uint64_t) p1->offset);
5606 : : }
5607 [ - + ]: 8210 : if (listptr_base (p1) != listptr_base (p2))
5608 : : {
5609 : 0 : p1->warned = p2->warned = true;
5610 : 0 : error (0, 0,
5611 : 0 : _("%s %#" PRIx64 " used with different base addresses"),
5612 : 0 : name, (uint64_t) p1->offset);
5613 : : }
5614 [ + - ]: 8210 : if (p1->attr != p2 ->attr)
5615 : : {
5616 : 0 : p1->warned = p2->warned = true;
5617 : 0 : error (0, 0,
5618 : 0 : _("%s %#" PRIx64
5619 : : " used with different attribute %s and %s"),
5620 : 0 : name, (uint64_t) p1->offset, dwarf_attr_name (p1->attr),
5621 : : dwarf_attr_name (p2->attr));
5622 : : }
5623 : : }
5624 : :
5625 : : return 0;
5626 : : }
5627 : :
5628 : : struct listptr_table
5629 : : {
5630 : : size_t n;
5631 : : size_t alloc;
5632 : : struct listptr *table;
5633 : : };
5634 : :
5635 : : static struct listptr_table known_locsptr;
5636 : : static struct listptr_table known_loclistsptr;
5637 : : static struct listptr_table known_rangelistptr;
5638 : : static struct listptr_table known_rnglistptr;
5639 : : static struct listptr_table known_addrbases;
5640 : : static struct listptr_table known_stroffbases;
5641 : :
5642 : : static void
5643 : 502 : reset_listptr (struct listptr_table *table)
5644 : : {
5645 : 502 : free (table->table);
5646 : 502 : table->table = NULL;
5647 : 502 : table->n = table->alloc = 0;
5648 : : }
5649 : :
5650 : : /* Returns false if offset doesn't fit. See struct listptr. */
5651 : : static bool
5652 : 257548 : notice_listptr (enum section_e section, struct listptr_table *table,
5653 : : uint_fast8_t address_size, uint_fast8_t offset_size,
5654 : : struct Dwarf_CU *cu, Dwarf_Off offset, unsigned int attr)
5655 : : {
5656 [ + + ]: 257548 : if (print_debug_sections & section)
5657 : : {
5658 [ + + ]: 257296 : if (table->n == table->alloc)
5659 : : {
5660 [ + + ]: 410 : if (table->alloc == 0)
5661 : 174 : table->alloc = 128;
5662 : : else
5663 : 236 : table->alloc *= 2;
5664 : 410 : table->table = xrealloc (table->table,
5665 : 410 : table->alloc * sizeof table->table[0]);
5666 : : }
5667 : :
5668 : 257296 : struct listptr *p = &table->table[table->n++];
5669 : :
5670 : 257296 : *p = (struct listptr)
5671 : : {
5672 : 257296 : .addr64 = address_size == 8,
5673 : 257296 : .dwarf64 = offset_size == 8,
5674 : : .offset = offset,
5675 : : .cu = cu,
5676 : : .attr = attr
5677 : : };
5678 : :
5679 [ + - ]: 257296 : if (p->offset != offset)
5680 : : {
5681 : 0 : table->n--;
5682 : 0 : return false;
5683 : : }
5684 : : }
5685 : : return true;
5686 : : }
5687 : :
5688 : : static void
5689 : 182 : sort_listptr (struct listptr_table *table, const char *name)
5690 : : {
5691 [ + + ]: 182 : if (table->n > 0)
5692 : : {
5693 : 174 : sort_listptr_name = name;
5694 : 174 : qsort (table->table, table->n, sizeof table->table[0],
5695 : : &compare_listptr);
5696 : : }
5697 : 182 : }
5698 : :
5699 : : static bool
5700 : 208 : skip_listptr_hole (struct listptr_table *table, size_t *idxp,
5701 : : uint_fast8_t *address_sizep, uint_fast8_t *offset_sizep,
5702 : : Dwarf_Addr *base, struct Dwarf_CU **cu, ptrdiff_t offset,
5703 : : unsigned char **readp, unsigned char *endp,
5704 : : unsigned int *attr)
5705 : : {
5706 [ - + ]: 208 : if (table->n == 0)
5707 : : return false;
5708 : :
5709 [ + - + + ]: 336 : while (*idxp < table->n && table->table[*idxp].offset < (Dwarf_Off) offset)
5710 : 128 : ++*idxp;
5711 : :
5712 : 208 : struct listptr *p = &table->table[*idxp];
5713 : :
5714 [ + - ]: 208 : if (*idxp == table->n
5715 [ - + ]: 208 : || p->offset >= (Dwarf_Off) (endp - *readp + offset))
5716 : : {
5717 : 0 : *readp = endp;
5718 : 0 : printf (_(" [%6tx] <UNUSED GARBAGE IN REST OF SECTION>\n"),
5719 : : offset);
5720 : 0 : return true;
5721 : : }
5722 : :
5723 [ - + ]: 208 : if (p->offset != (Dwarf_Off) offset)
5724 : : {
5725 : 0 : *readp += p->offset - offset;
5726 : 0 : printf (_(" [%6tx] <UNUSED GARBAGE> ... %" PRIu64 " bytes ...\n"),
5727 : : offset, (Dwarf_Off) p->offset - offset);
5728 : 0 : return true;
5729 : : }
5730 : :
5731 [ + - ]: 208 : if (address_sizep != NULL)
5732 [ + + ]: 224 : *address_sizep = listptr_address_size (p);
5733 [ + + ]: 208 : if (offset_sizep != NULL)
5734 [ + - ]: 296 : *offset_sizep = listptr_offset_size (p);
5735 [ + - ]: 208 : if (base != NULL)
5736 : 208 : *base = listptr_base (p);
5737 [ + - ]: 208 : if (cu != NULL)
5738 : 208 : *cu = p->cu;
5739 [ + + ]: 208 : if (attr != NULL)
5740 : 148 : *attr = p->attr;
5741 : :
5742 : : return false;
5743 : : }
5744 : :
5745 : : static Dwarf_Off
5746 : 109748 : next_listptr_offset (struct listptr_table *table, size_t *idxp, Dwarf_Off off)
5747 : : {
5748 : : /* Note that multiple attributes could in theory point to the same loclist
5749 : : offset, so make sure we pick one that is bigger than the current one.
5750 : : The table is sorted on offset. */
5751 [ + - ]: 109748 : if (*idxp < table->n)
5752 : : {
5753 [ + - ]: 218320 : while (++*idxp < table->n)
5754 : : {
5755 : 218320 : Dwarf_Off next = table->table[*idxp].offset;
5756 [ + + ]: 218320 : if (next > off)
5757 : : return next;
5758 : : }
5759 : : }
5760 : : return 0;
5761 : : }
5762 : :
5763 : : /* Returns the listptr associated with the given index, or NULL. */
5764 : : static struct listptr *
5765 : 1527696 : get_listptr (struct listptr_table *table, size_t idx)
5766 : : {
5767 : 1527696 : if (idx >= table->n)
5768 : : return NULL;
5769 : 1527444 : return &table->table[idx];
5770 : : }
5771 : :
5772 : : /* Returns the next index, base address and CU associated with the
5773 : : list unit offsets. If there is none false is returned, otherwise
5774 : : true. Assumes the table has been sorted. */
5775 : : static bool
5776 : 5130 : listptr_cu (struct listptr_table *table, size_t *idxp,
5777 : : Dwarf_Off start, Dwarf_Off end,
5778 : : Dwarf_Addr *base, struct Dwarf_CU **cu)
5779 : : {
5780 : 5130 : while (*idxp < table->n
5781 [ + - + + ]: 41080 : && table->table[*idxp].offset < start)
5782 : 35950 : ++*idxp;
5783 : :
5784 [ + - ]: 5130 : if (*idxp < table->n
5785 [ + - ]: 5130 : && table->table[*idxp].offset >= start
5786 [ + - ]: 5130 : && table->table[*idxp].offset < end)
5787 : : {
5788 : 5130 : struct listptr *p = &table->table[*idxp];
5789 : 5130 : *base = listptr_base (p);
5790 : 5130 : *cu = p->cu;
5791 : 5130 : return true;
5792 : : }
5793 : :
5794 : : return false;
5795 : : }
5796 : :
5797 : : /* Returns the next index with the current CU for the given attribute.
5798 : : If there is none false is returned, otherwise true. Assumes the
5799 : : table has been sorted. */
5800 : : static bool
5801 : 763496 : listptr_attr (struct listptr_table *table, size_t idxp,
5802 : : Dwarf_Off offset, unsigned int attr)
5803 : : {
5804 : 1527660 : struct listptr *listptr;
5805 : 1527660 : do
5806 : : {
5807 [ + + ]: 1527660 : listptr = get_listptr (table, idxp);
5808 [ + - ]: 1527418 : if (listptr == NULL)
5809 : : return false;
5810 : :
5811 [ + + + + ]: 1527418 : if (listptr->offset == offset && listptr->attr == attr)
5812 : : return true;
5813 : :
5814 : 1417670 : idxp++;
5815 : : }
5816 [ + + ]: 1417670 : while (listptr->offset <= offset);
5817 : :
5818 : : return false;
5819 : : }
5820 : :
5821 : : static void
5822 : 88 : print_debug_abbrev_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
5823 : : Ebl *ebl, GElf_Ehdr *ehdr __attribute__ ((unused)),
5824 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg,
5825 : : FILE *out)
5826 : : {
5827 : 88 : Elf_Data *elf_data = get_debug_elf_data (dbg, ebl, IDX_debug_abbrev, scn);
5828 [ + - ]: 88 : if (elf_data == NULL)
5829 : : return;
5830 : :
5831 : 88 : const size_t sh_size = elf_data->d_size;
5832 : :
5833 : 88 : fprintf (out, _("\nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"
5834 : : " [ Code]\n"),
5835 : : elf_ndxscn (scn), section_name (ebl, shdr),
5836 : 88 : (uint64_t) shdr->sh_offset);
5837 : :
5838 : 88 : Dwarf_Off offset = 0;
5839 [ + + ]: 3682 : while (offset < sh_size)
5840 : : {
5841 : 3594 : fprintf (out, _("\nAbbreviation section at offset %" PRIu64 ":\n"),
5842 : : offset);
5843 : :
5844 : 332238 : while (1)
5845 : 164322 : {
5846 : 167916 : size_t length;
5847 : 167916 : Dwarf_Abbrev abbrev;
5848 : :
5849 : 167916 : int res = dwarf_offabbrev (dbg, offset, &length, &abbrev);
5850 [ + + ]: 167916 : if (res != 0)
5851 : : {
5852 [ - + ]: 3594 : if (unlikely (res < 0))
5853 : : {
5854 : 0 : fprintf (out, _("\
5855 : : *** error while reading abbreviation: %s\n"),
5856 : : dwarf_errmsg (-1));
5857 : 0 : return;
5858 : : }
5859 : :
5860 : : /* This is the NUL byte at the end of the section. */
5861 : 3594 : ++offset;
5862 : 3594 : break;
5863 : : }
5864 : :
5865 : : /* We know these calls can never fail. */
5866 : 164322 : unsigned int code = dwarf_getabbrevcode (&abbrev);
5867 : 164322 : unsigned int tag = dwarf_getabbrevtag (&abbrev);
5868 : 164322 : int has_children = dwarf_abbrevhaschildren (&abbrev);
5869 : :
5870 [ + + ]: 164322 : fprintf (out, _(" [%5u] offset: %" PRId64
5871 : : ", children: %s, tag: %s\n"),
5872 : : code, (int64_t) offset,
5873 : : has_children ? yes_str : no_str,
5874 : : dwarf_tag_name (tag));
5875 : :
5876 : 164322 : size_t cnt = 0;
5877 : 164322 : unsigned int name;
5878 : 164322 : unsigned int form;
5879 : 164322 : Dwarf_Sword data;
5880 : 164322 : Dwarf_Off enoffset;
5881 : 164322 : while (dwarf_getabbrevattr_data (&abbrev, cnt, &name, &form,
5882 [ + + ]: 942204 : &data, &enoffset) == 0)
5883 : : {
5884 : 777882 : fprintf (out, " attr: %s, form: %s",
5885 : : dwarf_attr_name (name), dwarf_form_name (form));
5886 [ + + ]: 777882 : if (form == DW_FORM_implicit_const)
5887 : 55064 : fprintf (out, " (%" PRId64 ")", data);
5888 : 777882 : fprintf (out, ", offset: %#" PRIx64 "\n", (uint64_t) enoffset);
5889 : 777882 : ++cnt;
5890 : : }
5891 : :
5892 : 164322 : offset += length;
5893 : : }
5894 : : }
5895 : : }
5896 : :
5897 : :
5898 : : static void
5899 : 8 : print_debug_addr_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
5900 : : Ebl *ebl, GElf_Ehdr *ehdr,
5901 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg,
5902 : : FILE *out)
5903 : : {
5904 : 8 : Elf_Data *data = get_debug_elf_data (dbg, ebl, IDX_debug_addr, scn);
5905 [ + - ]: 8 : if (data == NULL)
5906 : : return;
5907 : :
5908 : 8 : fprintf (out, _("\
5909 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
5910 : : elf_ndxscn (scn), section_name (ebl, shdr),
5911 : 8 : (uint64_t) shdr->sh_offset);
5912 : :
5913 [ + - ]: 8 : if (shdr->sh_size == 0)
5914 : : return;
5915 : :
5916 : 8 : size_t idx = 0;
5917 : 8 : sort_listptr (&known_addrbases, "addr_base");
5918 : :
5919 : 8 : const unsigned char *start = (const unsigned char *) data->d_buf;
5920 : 8 : const unsigned char *readp = start;
5921 : 8 : const unsigned char *readendp = ((const unsigned char *) data->d_buf
5922 : 8 : + data->d_size);
5923 : :
5924 [ + + ]: 24 : while (readp < readendp)
5925 : : {
5926 : : /* We cannot really know whether or not there is an header. The
5927 : : DebugFission extension to DWARF4 doesn't add one. The DWARF5
5928 : : .debug_addr variant does. Whether or not we have an header,
5929 : : DW_AT_[GNU_]addr_base points at "index 0". So if the current
5930 : : offset equals the CU addr_base then we can just start
5931 : : printing addresses. If there is no CU with an exact match
5932 : : then we'll try to parse the header first. */
5933 : 16 : Dwarf_Off off = (Dwarf_Off) (readp
5934 : 16 : - (const unsigned char *) data->d_buf);
5935 : :
5936 : 16 : fprintf (out, "Table at offset %" PRIx64 " ", off);
5937 : :
5938 [ + - ]: 16 : struct listptr *listptr = get_listptr (&known_addrbases, idx++);
5939 : 16 : const unsigned char *next_unitp;
5940 : :
5941 : 16 : uint64_t unit_length;
5942 : 16 : uint16_t version;
5943 : 16 : uint8_t address_size;
5944 : 16 : uint8_t segment_size;
5945 [ - + ]: 16 : if (listptr == NULL)
5946 : : {
5947 : 0 : error (0, 0, "Warning: No CU references .debug_addr after %" PRIx64,
5948 : : off);
5949 : :
5950 : : /* We will have to assume it is just addresses to the end... */
5951 [ # # ]: 0 : address_size = ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 4 : 8;
5952 : 0 : next_unitp = readendp;
5953 : 0 : fprintf (out, "Unknown CU:\n");
5954 : : }
5955 : : else
5956 : : {
5957 : 16 : Dwarf_Die cudie;
5958 [ - + ]: 16 : if (dwarf_cu_die (listptr->cu, &cudie,
5959 : : NULL, NULL, NULL, NULL,
5960 : : NULL, NULL) == NULL)
5961 : 0 : fprintf (out, "Unknown CU (%s):\n", dwarf_errmsg (-1));
5962 : : else
5963 : 16 : fprintf (out, "for CU [%6" PRIx64 "]:\n", dwarf_dieoffset (&cudie));
5964 : :
5965 [ + + ]: 16 : if (listptr->offset == off)
5966 : : {
5967 [ - + ]: 4 : address_size = listptr_address_size (listptr);
5968 : 4 : segment_size = 0;
5969 : 4 : version = 4;
5970 : :
5971 : : /* The addresses start here, but where do they end? */
5972 [ + + ]: 4 : listptr = get_listptr (&known_addrbases, idx);
5973 [ - + ]: 2 : if (listptr == NULL)
5974 : : next_unitp = readendp;
5975 [ + - ]: 2 : else if (listptr->cu->version < 5)
5976 : : {
5977 : 2 : next_unitp = start + listptr->offset;
5978 [ + - - + ]: 2 : if (listptr->offset < off || listptr->offset > data->d_size)
5979 : : {
5980 : 0 : error (0, 0,
5981 : : "Warning: Bad address base for next unit at %"
5982 : : PRIx64, off);
5983 : 0 : next_unitp = readendp;
5984 : : }
5985 : : }
5986 : : else
5987 : : {
5988 : : /* Tricky, we don't have a header for this unit, but
5989 : : there is one for the next. We will have to
5990 : : "guess" how big it is and subtract it from the
5991 : : offset (because that points after the header). */
5992 [ # # ]: 0 : unsigned int offset_size = listptr_offset_size (listptr);
5993 : 0 : Dwarf_Off next_off = (listptr->offset
5994 : 0 : - (offset_size == 4 ? 4 : 12) /* len */
5995 : : - 2 /* version */
5996 : : - 1 /* address size */
5997 : 0 : - 1); /* segment selector size */
5998 : 0 : next_unitp = start + next_off;
5999 [ # # # # ]: 0 : if (next_off < off || next_off > data->d_size)
6000 : : {
6001 : 0 : error (0, 0,
6002 : : "Warning: Couldn't calculate .debug_addr "
6003 : : " unit length at %" PRIx64, off);
6004 : 0 : next_unitp = readendp;
6005 : : }
6006 : : }
6007 : 4 : unit_length = (uint64_t) (next_unitp - readp);
6008 : :
6009 : : /* Pretend we have a header. */
6010 : 4 : fprintf (out, "\n");
6011 : 4 : fprintf (out, _(" Length: %8" PRIu64 "\n"),
6012 : : unit_length);
6013 : 4 : fprintf (out, _(" DWARF version: %8" PRIu16 "\n"), version);
6014 : 4 : fprintf (out, _(" Address size: %8" PRIu64 "\n"),
6015 : : (uint64_t) address_size);
6016 : 4 : fprintf (out, _(" Segment size: %8" PRIu64 "\n"),
6017 : : (uint64_t) segment_size);
6018 : 4 : fprintf (out, "\n");
6019 : : }
6020 : : else
6021 : : {
6022 : : /* OK, we have to parse an header first. */
6023 [ - + ]: 12 : unit_length = read_4ubyte_unaligned_inc (dbg, readp);
6024 [ - + ]: 12 : if (unlikely (unit_length == 0xffffffff))
6025 : : {
6026 [ # # ]: 0 : if (unlikely (readp > readendp - 8))
6027 : : {
6028 : 0 : invalid_data:
6029 : 0 : error (0, 0, "Invalid data");
6030 : 0 : return;
6031 : : }
6032 [ # # ]: 0 : unit_length = read_8ubyte_unaligned_inc (dbg, readp);
6033 : : }
6034 : 12 : fprintf (out, "\n");
6035 : 12 : fprintf (out, _(" Length: %8" PRIu64 "\n"),
6036 : : unit_length);
6037 : :
6038 : : /* We need at least 2-bytes (version) + 1-byte
6039 : : (addr_size) + 1-byte (segment_size) = 4 bytes to
6040 : : complete the header. And this unit cannot go beyond
6041 : : the section data. */
6042 [ + - ]: 12 : if (readp > readendp - 4
6043 [ + - ]: 12 : || unit_length < 4
6044 [ - + ]: 12 : || unit_length > (uint64_t) (readendp - readp))
6045 : 0 : goto invalid_data;
6046 : :
6047 : 12 : next_unitp = readp + unit_length;
6048 : :
6049 [ - + ]: 12 : version = read_2ubyte_unaligned_inc (dbg, readp);
6050 : 12 : fprintf (out, _(" DWARF version: %8" PRIu16 "\n"), version);
6051 : :
6052 [ - + ]: 12 : if (version != 5)
6053 : : {
6054 : 0 : error (0, 0, _("Unknown version"));
6055 : 0 : goto next_unit;
6056 : : }
6057 : :
6058 : 12 : address_size = *readp++;
6059 : 12 : fprintf (out, _(" Address size: %8" PRIu64 "\n"),
6060 : : (uint64_t) address_size);
6061 : :
6062 [ - + ]: 12 : if (address_size != 4 && address_size != 8)
6063 : : {
6064 : 0 : error (0, 0, _("unsupported address size"));
6065 : 0 : goto next_unit;
6066 : : }
6067 : :
6068 : 12 : segment_size = *readp++;
6069 : 12 : fprintf (out, _(" Segment size: %8" PRIu64 "\n"),
6070 : : (uint64_t) segment_size);
6071 : 12 : fprintf (out, "\n");
6072 : :
6073 [ - + ]: 12 : if (segment_size != 0)
6074 : : {
6075 : 0 : error (0, 0, _("unsupported segment size"));
6076 : 0 : goto next_unit;
6077 : : }
6078 : :
6079 [ - + ]: 12 : if (listptr->offset != (Dwarf_Off) (readp - start))
6080 : : {
6081 : 0 : error (0, 0, "Address index doesn't start after header");
6082 : 0 : goto next_unit;
6083 : : }
6084 : : }
6085 : : }
6086 : :
6087 : 16 : int digits = 1;
6088 : 16 : size_t addresses = (next_unitp - readp) / address_size;
6089 [ + + ]: 24 : while (addresses >= 10)
6090 : : {
6091 : 8 : ++digits;
6092 : 8 : addresses /= 10;
6093 : : }
6094 : :
6095 : 16 : unsigned int uidx = 0;
6096 : 16 : size_t index_offset = readp - (const unsigned char *) data->d_buf;
6097 : 16 : fprintf (out, " Addresses start at offset 0x%zx:\n", index_offset);
6098 [ + + ]: 188 : while (readp <= next_unitp - address_size)
6099 : : {
6100 [ - + + + : 172 : Dwarf_Addr addr = read_addr_unaligned_inc (address_size, dbg,
- + - + ]
6101 : : readp);
6102 : 172 : fprintf (out, " [%*u] ", digits, uidx++);
6103 : 172 : print_dwarf_addr (dwflmod, address_size, addr, addr, out);
6104 : 172 : fprintf (out, "\n");
6105 : : }
6106 : 16 : fprintf (out, "\n");
6107 : :
6108 [ + - ]: 16 : if (readp != next_unitp)
6109 : 0 : error (0, 0, "extra %zd bytes at end of unit",
6110 : 0 : (size_t) (next_unitp - readp));
6111 : :
6112 : 16 : next_unit:
6113 : : readp = next_unitp;
6114 : : }
6115 : : }
6116 : :
6117 : : /* Print content of DWARF .debug_aranges section. We fortunately do
6118 : : not have to know a bit about the structure of the section, libdwarf
6119 : : takes care of it. */
6120 : : static void
6121 : 2 : print_decoded_aranges_section (Ebl *ebl, GElf_Ehdr *ehdr, Elf_Scn *scn,
6122 : : GElf_Shdr *shdr, Dwarf *dbg, FILE *out)
6123 : : {
6124 : 2 : Dwarf_Aranges *aranges;
6125 : 2 : size_t cnt;
6126 [ - + ]: 2 : if (unlikely (dwarf_getaranges (dbg, &aranges, &cnt) != 0))
6127 : : {
6128 : 0 : error (0, 0, _("cannot get .debug_aranges content: %s"),
6129 : : dwarf_errmsg (-1));
6130 : 0 : return;
6131 : : }
6132 : :
6133 : 2 : GElf_Shdr glink_mem;
6134 : 2 : GElf_Shdr *glink;
6135 : 2 : glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link), &glink_mem);
6136 [ - + ]: 2 : if (glink == NULL)
6137 : : {
6138 : 0 : error (0, 0, _("invalid sh_link value in section %zu"),
6139 : : elf_ndxscn (scn));
6140 : 0 : return;
6141 : : }
6142 : :
6143 : 2 : fprintf (out, ngettext ("\
6144 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 " contains %zu entry:\n",
6145 : : "\
6146 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 " contains %zu entries:\n",
6147 : : cnt),
6148 : : elf_ndxscn (scn), section_name (ebl, shdr),
6149 : 2 : (uint64_t) shdr->sh_offset, cnt);
6150 : :
6151 : : /* Compute floor(log16(cnt)). */
6152 : 2 : size_t tmp = cnt;
6153 : 2 : int digits = 1;
6154 [ - + ]: 2 : while (tmp >= 16)
6155 : : {
6156 : 0 : ++digits;
6157 : 0 : tmp >>= 4;
6158 : : }
6159 : :
6160 [ + + ]: 12 : for (size_t n = 0; n < cnt; ++n)
6161 : : {
6162 : 10 : Dwarf_Arange *runp = dwarf_onearange (aranges, n);
6163 [ - + ]: 10 : if (unlikely (runp == NULL))
6164 : : {
6165 : 0 : fprintf (out, "cannot get arange %zu: %s\n", n, dwarf_errmsg (-1));
6166 : 0 : return;
6167 : : }
6168 : :
6169 : 10 : Dwarf_Addr start;
6170 : 10 : Dwarf_Word length;
6171 : 10 : Dwarf_Off offset;
6172 : :
6173 [ - + ]: 10 : if (unlikely (dwarf_getarangeinfo (runp, &start, &length, &offset) != 0))
6174 : 0 : fprintf (out, _(" [%*zu] ???\n"), digits, n);
6175 : : else
6176 : 10 : fprintf (out, _(" [%*zu] start: %0#*" PRIx64
6177 : : ", length: %5" PRIu64 ", CU DIE offset: %6"
6178 : : PRId64 "\n"),
6179 [ - + ]: 10 : digits, n, ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 10 : 18,
6180 : : (uint64_t) start, (uint64_t) length, (int64_t) offset);
6181 : : }
6182 : : }
6183 : :
6184 : :
6185 : : /* Print content of DWARF .debug_aranges section. */
6186 : : static void
6187 : 92 : print_debug_aranges_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
6188 : : Ebl *ebl, GElf_Ehdr *ehdr, Elf_Scn *scn,
6189 : : GElf_Shdr *shdr, Dwarf *dbg, FILE *out)
6190 : : {
6191 [ + + ]: 92 : if (decodedaranges)
6192 : : {
6193 : 2 : print_decoded_aranges_section (ebl, ehdr, scn, shdr, dbg, out);
6194 : 2 : return;
6195 : : }
6196 : :
6197 : 90 : Elf_Data *data = get_debug_elf_data (dbg, ebl, IDX_debug_aranges, scn);
6198 [ + - ]: 90 : if (data == NULL)
6199 : : return;
6200 : :
6201 : 90 : fprintf (out, _("\
6202 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
6203 : : elf_ndxscn (scn), section_name (ebl, shdr),
6204 : 90 : (uint64_t) shdr->sh_offset);
6205 : :
6206 : 90 : const unsigned char *readp = data->d_buf;
6207 : 90 : const unsigned char *readendp = readp + data->d_size;
6208 : :
6209 [ + + ]: 3666 : while (readp < readendp)
6210 : : {
6211 : 3576 : const unsigned char *hdrstart = readp;
6212 : 3576 : size_t start_offset = hdrstart - (const unsigned char *) data->d_buf;
6213 : :
6214 : 3576 : fprintf (out, _("\nTable at offset %zu:\n"), start_offset);
6215 [ - + ]: 3576 : if (readp + 4 > readendp)
6216 : : {
6217 : 0 : invalid_data:
6218 : 0 : error (0, 0, _("invalid data in section [%zu] '%s'"),
6219 : : elf_ndxscn (scn), section_name (ebl, shdr));
6220 : 0 : return;
6221 : : }
6222 : :
6223 [ + + ]: 3576 : Dwarf_Word length = read_4ubyte_unaligned_inc (dbg, readp);
6224 : 3576 : unsigned int length_bytes = 4;
6225 [ - + ]: 3576 : if (length == DWARF3_LENGTH_64_BIT)
6226 : : {
6227 [ # # ]: 0 : if (readp + 8 > readendp)
6228 : 0 : goto invalid_data;
6229 [ # # ]: 0 : length = read_8ubyte_unaligned_inc (dbg, readp);
6230 : 0 : length_bytes = 8;
6231 : : }
6232 : :
6233 : 3576 : const unsigned char *nexthdr = readp + length;
6234 : 3576 : fprintf (out, _("\n Length: %6" PRIu64 "\n"),
6235 : : (uint64_t) length);
6236 : :
6237 [ - + ]: 3576 : if (unlikely (length > (size_t) (readendp - readp)))
6238 : 0 : goto invalid_data;
6239 : :
6240 [ - + ]: 3576 : if (length == 0)
6241 : 0 : continue;
6242 : :
6243 [ - + ]: 3576 : if (readp + 2 > readendp)
6244 : 0 : goto invalid_data;
6245 [ + + ]: 3576 : uint_fast16_t version = read_2ubyte_unaligned_inc (dbg, readp);
6246 : 3576 : fprintf (out, _(" DWARF version: %6" PRIuFAST16 "\n"),
6247 : : version);
6248 [ - + ]: 3576 : if (version != 2)
6249 : : {
6250 : 0 : error (0, 0, _("unsupported aranges version"));
6251 : 0 : goto next_table;
6252 : : }
6253 : :
6254 : 3576 : Dwarf_Word offset;
6255 [ - + ]: 3576 : if (readp + length_bytes > readendp)
6256 : 0 : goto invalid_data;
6257 [ - + ]: 3576 : if (length_bytes == 8)
6258 [ # # ]: 0 : offset = read_8ubyte_unaligned_inc (dbg, readp);
6259 : : else
6260 [ + + ]: 3576 : offset = read_4ubyte_unaligned_inc (dbg, readp);
6261 : 3576 : fprintf (out, _(" CU offset: %6" PRIx64 "\n"),
6262 : : (uint64_t) offset);
6263 : :
6264 [ - + ]: 3576 : if (readp + 1 > readendp)
6265 : 0 : goto invalid_data;
6266 : 3576 : unsigned int address_size = *readp++;
6267 : 3576 : fprintf (out, _(" Address size: %6" PRIu64 "\n"),
6268 : : (uint64_t) address_size);
6269 [ - + ]: 3576 : if (address_size != 4 && address_size != 8)
6270 : : {
6271 : 0 : error (0, 0, _("unsupported address size"));
6272 : 0 : goto next_table;
6273 : : }
6274 : :
6275 [ - + ]: 3576 : if (readp + 1 > readendp)
6276 : 0 : goto invalid_data;
6277 : 3576 : unsigned int segment_size = *readp++;
6278 : 3576 : fprintf (out, _(" Segment size: %6" PRIu64 "\n\n"),
6279 : : (uint64_t) segment_size);
6280 [ - + - - ]: 3576 : if (segment_size != 0 && segment_size != 4 && segment_size != 8)
6281 : : {
6282 : 0 : error (0, 0, _("unsupported segment size"));
6283 : 0 : goto next_table;
6284 : : }
6285 : :
6286 : : /* Round the address to the next multiple of 2*address_size. */
6287 : 3576 : readp += ((2 * address_size - ((readp - hdrstart) % (2 * address_size)))
6288 : 3576 : % (2 * address_size));
6289 : :
6290 [ + - ]: 9170 : while (readp < nexthdr)
6291 : : {
6292 : 9170 : Dwarf_Word range_address;
6293 : 9170 : Dwarf_Word range_length;
6294 : 9170 : Dwarf_Word segment = 0;
6295 [ - + ]: 9170 : if (readp + 2 * address_size + segment_size > readendp)
6296 : 0 : goto invalid_data;
6297 [ + + ]: 9170 : if (address_size == 4)
6298 : : {
6299 [ + + ]: 92 : range_address = read_4ubyte_unaligned_inc (dbg, readp);
6300 [ + + ]: 92 : range_length = read_4ubyte_unaligned_inc (dbg, readp);
6301 : : }
6302 : : else
6303 : : {
6304 [ + + ]: 9078 : range_address = read_8ubyte_unaligned_inc (dbg, readp);
6305 [ + + ]: 9078 : range_length = read_8ubyte_unaligned_inc (dbg, readp);
6306 : : }
6307 : :
6308 [ - + ]: 9170 : if (segment_size == 4)
6309 [ # # ]: 0 : segment = read_4ubyte_unaligned_inc (dbg, readp);
6310 [ - + ]: 9170 : else if (segment_size == 8)
6311 [ # # ]: 0 : segment = read_8ubyte_unaligned_inc (dbg, readp);
6312 : :
6313 [ + + ]: 9170 : if (range_address == 0 && range_length == 0 && segment == 0)
6314 : : break;
6315 : :
6316 : 5594 : fprintf (out, " ");
6317 : 5594 : print_dwarf_addr (dwflmod, address_size, range_address,
6318 : : range_address, out);
6319 : 5594 : fprintf (out, "..");
6320 : 5594 : print_dwarf_addr (dwflmod, address_size,
6321 : 5594 : range_address + range_length - 1,
6322 : : range_length, out);
6323 [ - + ]: 5594 : if (segment_size != 0)
6324 : 0 : fprintf (out, " (%" PRIx64 ")\n", (uint64_t) segment);
6325 : : else
6326 : 5594 : fprintf (out, "\n");
6327 : : }
6328 : :
6329 : 3576 : next_table:
6330 [ - + ]: 3576 : if (readp != nexthdr)
6331 : : {
6332 : 0 : size_t padding = nexthdr - readp;
6333 : 0 : fprintf (out, _(" %zu padding bytes\n"), padding);
6334 : 0 : readp = nexthdr;
6335 : : }
6336 : : }
6337 : : }
6338 : :
6339 : :
6340 : : static bool is_split_dwarf (Dwarf *dbg, uint64_t *id, Dwarf_CU **split_cu);
6341 : :
6342 : : /* Returns true and sets cu and cu_base if the given Dwarf is a split
6343 : : DWARF (.dwo) file. */
6344 : : static bool
6345 : 0 : split_dwarf_cu_base (Dwarf *dbg, Dwarf_CU **cu, Dwarf_Addr *cu_base)
6346 : : {
6347 : 0 : uint64_t id;
6348 [ # # ]: 0 : if (is_split_dwarf (dbg, &id, cu))
6349 : : {
6350 : 0 : Dwarf_Die cudie;
6351 [ # # ]: 0 : if (dwarf_cu_info (*cu, NULL, NULL, &cudie, NULL, NULL, NULL, NULL) == 0)
6352 : : {
6353 : 0 : *cu_base = cudie_base (&cudie);
6354 : 0 : return true;
6355 : : }
6356 : : }
6357 : : return false;
6358 : : }
6359 : :
6360 : : /* Print content of DWARF .debug_rnglists section. */
6361 : : static void
6362 : 38 : print_debug_rnglists_section (Dwfl_Module *dwflmod,
6363 : : Ebl *ebl,
6364 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
6365 : : Elf_Scn *scn, GElf_Shdr *shdr,
6366 : : Dwarf *dbg __attribute__((unused)),
6367 : : FILE *out)
6368 : : {
6369 : 38 : Elf_Data *data = get_debug_elf_data (dbg, ebl, IDX_debug_rnglists, scn);
6370 [ + - ]: 38 : if (data == NULL)
6371 : 0 : return;
6372 : :
6373 : 38 : fprintf (out, _("\
6374 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
6375 : : elf_ndxscn (scn), section_name (ebl, shdr),
6376 : 38 : (uint64_t) shdr->sh_offset);
6377 : :
6378 : : /* For the listptr to get the base address/CU. */
6379 : 38 : sort_listptr (&known_rnglistptr, "rnglistptr");
6380 : 38 : size_t listptr_idx = 0;
6381 : :
6382 : 38 : const unsigned char *readp = data->d_buf;
6383 : 38 : const unsigned char *const dataend = ((unsigned char *) data->d_buf
6384 : 38 : + data->d_size);
6385 [ + + ]: 2076 : while (readp < dataend)
6386 : : {
6387 [ - + ]: 2038 : if (unlikely (readp > dataend - 4))
6388 : : {
6389 : 0 : invalid_data:
6390 : 0 : error (0, 0, _("invalid data in section [%zu] '%s'"),
6391 : : elf_ndxscn (scn), section_name (ebl, shdr));
6392 : 0 : return;
6393 : : }
6394 : :
6395 : 2038 : ptrdiff_t offset = readp - (unsigned char *) data->d_buf;
6396 : 2038 : fprintf (out, _("Table at Offset 0x%" PRIx64 ":\n\n"),
6397 : : (uint64_t) offset);
6398 : :
6399 [ - + ]: 2038 : uint64_t unit_length = read_4ubyte_unaligned_inc (dbg, readp);
6400 : 2038 : unsigned int offset_size = 4;
6401 [ - + ]: 2038 : if (unlikely (unit_length == 0xffffffff))
6402 : : {
6403 [ # # ]: 0 : if (unlikely (readp > dataend - 8))
6404 : 0 : goto invalid_data;
6405 : :
6406 [ # # ]: 0 : unit_length = read_8ubyte_unaligned_inc (dbg, readp);
6407 : 0 : offset_size = 8;
6408 : : }
6409 : 2038 : fprintf (out, _(" Length: %8" PRIu64 "\n"), unit_length);
6410 : :
6411 : : /* We need at least 2-bytes + 1-byte + 1-byte + 4-bytes = 8
6412 : : bytes to complete the header. And this unit cannot go beyond
6413 : : the section data. */
6414 [ + - ]: 2038 : if (readp > dataend - 8
6415 [ + - ]: 2038 : || unit_length < 8
6416 [ - + ]: 2038 : || unit_length > (uint64_t) (dataend - readp))
6417 : 0 : goto invalid_data;
6418 : :
6419 : 2038 : const unsigned char *nexthdr = readp + unit_length;
6420 : :
6421 [ - + ]: 2038 : uint16_t version = read_2ubyte_unaligned_inc (dbg, readp);
6422 : 2038 : fprintf (out, _(" DWARF version: %8" PRIu16 "\n"), version);
6423 : :
6424 [ - + ]: 2038 : if (version != 5)
6425 : : {
6426 : 0 : error (0, 0, _("Unknown version"));
6427 : 0 : goto next_table;
6428 : : }
6429 : :
6430 : 2038 : uint8_t address_size = *readp++;
6431 : 2038 : fprintf (out, _(" Address size: %8" PRIu64 "\n"),
6432 : : (uint64_t) address_size);
6433 : :
6434 [ - + ]: 2038 : if (address_size != 4 && address_size != 8)
6435 : : {
6436 : 0 : error (0, 0, _("unsupported address size"));
6437 : 0 : goto next_table;
6438 : : }
6439 : :
6440 : 2038 : uint8_t segment_size = *readp++;
6441 : 2038 : fprintf (out, _(" Segment size: %8" PRIu64 "\n"),
6442 : : (uint64_t) segment_size);
6443 : :
6444 [ - + - - ]: 2038 : if (segment_size != 0 && segment_size != 4 && segment_size != 8)
6445 : : {
6446 : 0 : error (0, 0, _("unsupported segment size"));
6447 : 0 : goto next_table;
6448 : : }
6449 : :
6450 [ - + ]: 2038 : uint32_t offset_entry_count = read_4ubyte_unaligned_inc (dbg, readp);
6451 : 2038 : fprintf (out, _(" Offset entries: %8" PRIu64 "\n"),
6452 : : (uint64_t) offset_entry_count);
6453 : :
6454 : : /* We need the CU that uses this unit to get the initial base address. */
6455 : 2038 : Dwarf_Addr cu_base = 0;
6456 : 2038 : struct Dwarf_CU *cu = NULL;
6457 [ - + ]: 2038 : if (listptr_cu (&known_rnglistptr, &listptr_idx,
6458 : : (Dwarf_Off) offset,
6459 : 2038 : (Dwarf_Off) (nexthdr - (unsigned char *) data->d_buf),
6460 : : &cu_base, &cu)
6461 [ # # ]: 0 : || split_dwarf_cu_base (dbg, &cu, &cu_base))
6462 : 2038 : {
6463 : 2038 : Dwarf_Die cudie;
6464 [ - + ]: 2038 : if (dwarf_cu_die (cu, &cudie,
6465 : : NULL, NULL, NULL, NULL,
6466 : : NULL, NULL) == NULL)
6467 : 0 : fprintf (out, _(" Unknown CU base: "));
6468 : : else
6469 : 2038 : fprintf (out, _(" CU [%6" PRIx64 "] base: "),
6470 : : dwarf_dieoffset (&cudie));
6471 : 2038 : print_dwarf_addr (dwflmod, address_size, cu_base, cu_base, out);
6472 : 2038 : fprintf (out, "\n");
6473 : : }
6474 : : else
6475 : 0 : fprintf (out, _(" Not associated with a CU.\n"));
6476 : :
6477 : 2038 : fprintf (out, "\n");
6478 : :
6479 : 2038 : const unsigned char *offset_array_start = readp;
6480 [ + + ]: 2038 : if (offset_entry_count > 0)
6481 : : {
6482 : 2 : uint64_t max_entries = (unit_length - 8) / offset_size;
6483 [ - + ]: 2 : if (offset_entry_count > max_entries)
6484 : : {
6485 : 0 : error (0, 0,
6486 : 0 : _("too many offset entries for unit length"));
6487 : 0 : offset_entry_count = max_entries;
6488 : : }
6489 : :
6490 : 2 : fprintf (out, _(" Offsets starting at 0x%" PRIx64 ":\n"),
6491 : : (uint64_t) (offset_array_start
6492 : 2 : - (unsigned char *) data->d_buf));
6493 [ + + ]: 6 : for (uint32_t idx = 0; idx < offset_entry_count; idx++)
6494 : : {
6495 : 4 : fprintf (out, " [%6" PRIu32 "] ", idx);
6496 [ + - ]: 4 : if (offset_size == 4)
6497 : : {
6498 [ - + ]: 4 : uint32_t off = read_4ubyte_unaligned_inc (dbg, readp);
6499 : 4 : fprintf (out, "0x%" PRIx32 "\n", off);
6500 : : }
6501 : : else
6502 : : {
6503 [ # # ]: 0 : uint64_t off = read_8ubyte_unaligned_inc (dbg, readp);
6504 : 0 : fprintf (out, "0x%" PRIx64 "\n", off);
6505 : : }
6506 : : }
6507 : 2 : fprintf (out, "\n");
6508 : : }
6509 : :
6510 : 2038 : Dwarf_Addr base = cu_base;
6511 : 2038 : bool start_of_list = true;
6512 [ + + ]: 150402 : while (readp < nexthdr)
6513 : : {
6514 : 148364 : uint8_t kind = *readp++;
6515 : 148364 : uint64_t op1, op2;
6516 : :
6517 : : /* Skip padding. */
6518 [ - + ]: 148364 : if (start_of_list && kind == DW_RLE_end_of_list)
6519 : 0 : continue;
6520 : :
6521 [ + + ]: 148364 : if (start_of_list)
6522 : : {
6523 : 30110 : base = cu_base;
6524 : 30110 : fprintf (out, " Offset: %" PRIx64 ", Index: %" PRIx64 "\n",
6525 : 30110 : (uint64_t) (readp - (unsigned char *) data->d_buf - 1),
6526 : 30110 : (uint64_t) (readp - offset_array_start - 1));
6527 : 30110 : start_of_list = false;
6528 : : }
6529 : :
6530 : 148364 : fprintf (out, " %s", dwarf_range_list_encoding_name (kind));
6531 [ + - - - : 148364 : switch (kind)
+ + - +
- ]
6532 : : {
6533 : 30110 : case DW_RLE_end_of_list:
6534 : 30110 : start_of_list = true;
6535 : 30110 : fprintf (out, "\n\n");
6536 : 30110 : break;
6537 : :
6538 : 0 : case DW_RLE_base_addressx:
6539 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
6540 : : {
6541 : 0 : invalid_range:
6542 : 0 : error (0, 0, _("invalid range list data"));
6543 : 0 : goto next_table;
6544 : : }
6545 : 0 : get_uleb128 (op1, readp, nexthdr);
6546 : 0 : fprintf (out, " %" PRIx64 "\n", op1);
6547 [ # # ]: 0 : if (! print_unresolved_addresses)
6548 : : {
6549 : 0 : Dwarf_Addr addr;
6550 [ # # ]: 0 : if (get_indexed_addr (cu, op1, &addr) != 0)
6551 : 0 : fprintf (out, " ???\n");
6552 : : else
6553 : : {
6554 : 0 : fprintf (out, " ");
6555 : 0 : print_dwarf_addr (dwflmod, address_size, addr,
6556 : : addr, out);
6557 : 0 : fprintf (out, "\n");
6558 : : }
6559 : : }
6560 : : break;
6561 : :
6562 : 0 : case DW_RLE_startx_endx:
6563 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
6564 : 0 : goto invalid_range;
6565 : 0 : get_uleb128 (op1, readp, nexthdr);
6566 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
6567 : 0 : goto invalid_range;
6568 : 0 : get_uleb128 (op2, readp, nexthdr);
6569 : 0 : fprintf (out, " %" PRIx64 ", %" PRIx64 "\n", op1, op2);
6570 [ # # ]: 0 : if (! print_unresolved_addresses)
6571 : : {
6572 : 0 : Dwarf_Addr addr1;
6573 : 0 : Dwarf_Addr addr2;
6574 [ # # ]: 0 : if (get_indexed_addr (cu, op1, &addr1) != 0
6575 [ # # ]: 0 : || get_indexed_addr (cu, op2, &addr2) != 0)
6576 : : {
6577 : 0 : fprintf (out, " ???..\n");
6578 : 0 : fprintf (out, " ???\n");
6579 : : }
6580 : : else
6581 : : {
6582 : 0 : fprintf (out, " ");
6583 : 0 : print_dwarf_addr (dwflmod, address_size,
6584 : : addr1, addr1, out);
6585 : 0 : fprintf (out, "..\n ");
6586 : 0 : print_dwarf_addr (dwflmod, address_size,
6587 : : addr2 - 1, addr2, out);
6588 : 0 : fprintf (out, "\n");
6589 : : }
6590 : : }
6591 : : break;
6592 : :
6593 : 0 : case DW_RLE_startx_length:
6594 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
6595 : 0 : goto invalid_range;
6596 : 0 : get_uleb128 (op1, readp, nexthdr);
6597 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
6598 : 0 : goto invalid_range;
6599 : 0 : get_uleb128 (op2, readp, nexthdr);
6600 : 0 : fprintf (out, " %" PRIx64 ", %" PRIx64 "\n", op1, op2);
6601 [ # # ]: 0 : if (! print_unresolved_addresses)
6602 : : {
6603 : 0 : Dwarf_Addr addr1;
6604 : 0 : Dwarf_Addr addr2;
6605 [ # # ]: 0 : if (get_indexed_addr (cu, op1, &addr1) != 0)
6606 : : {
6607 : 0 : fprintf (out, " ???..\n");
6608 : 0 : fprintf (out, " ???\n");
6609 : : }
6610 : : else
6611 : : {
6612 : 0 : addr2 = addr1 + op2;
6613 : 0 : fprintf (out, " ");
6614 : 0 : print_dwarf_addr (dwflmod, address_size, addr1, addr1, out);
6615 : 0 : fprintf (out, "..\n ");
6616 : 0 : print_dwarf_addr (dwflmod, address_size,
6617 : : addr2 - 1, addr2, out);
6618 : 0 : fprintf (out, "\n");
6619 : : }
6620 : : }
6621 : : break;
6622 : :
6623 : 93868 : case DW_RLE_offset_pair:
6624 [ - + ]: 93868 : if ((uint64_t) (nexthdr - readp) < 1)
6625 : 0 : goto invalid_range;
6626 : 93868 : get_uleb128 (op1, readp, nexthdr);
6627 [ - + ]: 93868 : if ((uint64_t) (nexthdr - readp) < 1)
6628 : 0 : goto invalid_range;
6629 : 93868 : get_uleb128 (op2, readp, nexthdr);
6630 : 93868 : fprintf (out, " %" PRIx64 ", %" PRIx64 "\n", op1, op2);
6631 [ + - ]: 93868 : if (! print_unresolved_addresses)
6632 : : {
6633 : 93868 : op1 += base;
6634 : 93868 : op2 += base;
6635 : 93868 : fprintf (out, " ");
6636 : 93868 : print_dwarf_addr (dwflmod, address_size, op1, op1, out);
6637 : 93868 : fprintf (out, "..\n ");
6638 : 93868 : print_dwarf_addr (dwflmod, address_size, op2 - 1, op2, out);
6639 : 93868 : fprintf (out, "\n");
6640 : : }
6641 : : break;
6642 : :
6643 : 17018 : case DW_RLE_base_address:
6644 [ - + ]: 17018 : if (address_size == 4)
6645 : : {
6646 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 4)
6647 : 0 : goto invalid_range;
6648 [ # # ]: 0 : op1 = read_4ubyte_unaligned_inc (dbg, readp);
6649 : : }
6650 : : else
6651 : : {
6652 [ - + ]: 17018 : if ((uint64_t) (nexthdr - readp) < 8)
6653 : 0 : goto invalid_range;
6654 [ - + ]: 17018 : op1 = read_8ubyte_unaligned_inc (dbg, readp);
6655 : : }
6656 : 17018 : base = op1;
6657 : 17018 : fprintf (out, " 0x%" PRIx64 "\n", base);
6658 [ + - ]: 17018 : if (! print_unresolved_addresses)
6659 : : {
6660 : 17018 : fprintf (out, " ");
6661 : 17018 : print_dwarf_addr (dwflmod, address_size, base, base, out);
6662 : 17018 : fprintf (out, "\n");
6663 : : }
6664 : : break;
6665 : :
6666 : 0 : case DW_RLE_start_end:
6667 [ # # ]: 0 : if (address_size == 4)
6668 : : {
6669 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 8)
6670 : 0 : goto invalid_range;
6671 [ # # ]: 0 : op1 = read_4ubyte_unaligned_inc (dbg, readp);
6672 [ # # ]: 0 : op2 = read_4ubyte_unaligned_inc (dbg, readp);
6673 : : }
6674 : : else
6675 : : {
6676 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 16)
6677 : 0 : goto invalid_range;
6678 [ # # ]: 0 : op1 = read_8ubyte_unaligned_inc (dbg, readp);
6679 [ # # ]: 0 : op2 = read_8ubyte_unaligned_inc (dbg, readp);
6680 : : }
6681 : 0 : fprintf (out, " 0x%" PRIx64 "..0x%" PRIx64 "\n", op1, op2);
6682 [ # # ]: 0 : if (! print_unresolved_addresses)
6683 : : {
6684 : 0 : fprintf (out, " ");
6685 : 0 : print_dwarf_addr (dwflmod, address_size, op1, op1, out);
6686 : 0 : fprintf (out, "..\n ");
6687 : 0 : print_dwarf_addr (dwflmod, address_size, op2 - 1, op2, out);
6688 : 0 : fprintf (out, "\n");
6689 : : }
6690 : : break;
6691 : :
6692 : 7368 : case DW_RLE_start_length:
6693 [ - + ]: 7368 : if (address_size == 4)
6694 : : {
6695 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 4)
6696 : 0 : goto invalid_range;
6697 [ # # ]: 0 : op1 = read_4ubyte_unaligned_inc (dbg, readp);
6698 : : }
6699 : : else
6700 : : {
6701 [ - + ]: 7368 : if ((uint64_t) (nexthdr - readp) < 8)
6702 : 0 : goto invalid_range;
6703 [ - + ]: 7368 : op1 = read_8ubyte_unaligned_inc (dbg, readp);
6704 : : }
6705 [ - + ]: 7368 : if ((uint64_t) (nexthdr - readp) < 1)
6706 : 0 : goto invalid_range;
6707 : 7368 : get_uleb128 (op2, readp, nexthdr);
6708 : 7368 : fprintf (out, " 0x%" PRIx64 ", %" PRIx64 "\n", op1, op2);
6709 [ + + ]: 7368 : if (! print_unresolved_addresses)
6710 : : {
6711 : 7366 : op2 = op1 + op2;
6712 : 7366 : fprintf (out, " ");
6713 : 7366 : print_dwarf_addr (dwflmod, address_size, op1, op1, out);
6714 : 7366 : fprintf (out, "..\n ");
6715 : 7366 : print_dwarf_addr (dwflmod, address_size, op2 - 1, op2, out);
6716 : 7366 : fprintf (out, "\n");
6717 : : }
6718 : : break;
6719 : :
6720 : 0 : default:
6721 : 0 : goto invalid_range;
6722 : : }
6723 : : }
6724 : :
6725 : 2038 : next_table:
6726 [ - + ]: 2038 : if (readp != nexthdr)
6727 : : {
6728 : 0 : size_t padding = nexthdr - readp;
6729 : 0 : fprintf (out, _(" %zu padding bytes\n\n"), padding);
6730 : 0 : readp = nexthdr;
6731 : : }
6732 : : }
6733 : : }
6734 : :
6735 : : /* Print content of DWARF .debug_ranges section. */
6736 : : static void
6737 : 32 : print_debug_ranges_section (Dwfl_Module *dwflmod,
6738 : : Ebl *ebl, GElf_Ehdr *ehdr,
6739 : : Elf_Scn *scn, GElf_Shdr *shdr,
6740 : : Dwarf *dbg, FILE *out)
6741 : : {
6742 : 32 : Elf_Data *data = get_debug_elf_data (dbg, ebl, IDX_debug_ranges, scn);
6743 [ - + ]: 32 : if (data == NULL)
6744 : 0 : return;
6745 : :
6746 : 32 : fprintf (out, _("\
6747 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
6748 : : elf_ndxscn (scn), section_name (ebl, shdr),
6749 : 32 : (uint64_t) shdr->sh_offset);
6750 : :
6751 : 32 : sort_listptr (&known_rangelistptr, "rangelistptr");
6752 : 32 : size_t listptr_idx = 0;
6753 : :
6754 [ + + ]: 32 : uint_fast8_t address_size = ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 4 : 8;
6755 : :
6756 : 32 : bool first = true;
6757 : 32 : Dwarf_Addr base = 0;
6758 : 32 : unsigned char *const endp = (unsigned char *) data->d_buf + data->d_size;
6759 : 32 : unsigned char *readp = data->d_buf;
6760 : 32 : Dwarf_CU *last_cu = NULL;
6761 [ + + ]: 184 : while (readp < endp)
6762 : : {
6763 : 152 : ptrdiff_t offset = readp - (unsigned char *) data->d_buf;
6764 : 152 : Dwarf_CU *cu = last_cu;
6765 : :
6766 [ + + - + ]: 152 : if (first && skip_listptr_hole (&known_rangelistptr, &listptr_idx,
6767 : : &address_size, NULL, &base, &cu,
6768 : : offset, &readp, endp, NULL))
6769 : 0 : continue;
6770 : :
6771 [ + + ]: 152 : if (last_cu != cu)
6772 : : {
6773 : 58 : Dwarf_Die cudie;
6774 [ - + ]: 58 : if (dwarf_cu_die (cu, &cudie,
6775 : : NULL, NULL, NULL, NULL,
6776 : : NULL, NULL) == NULL)
6777 : 0 : fprintf (out, _("\n Unknown CU base: "));
6778 : : else
6779 : 58 : fprintf (out, _("\n CU [%6" PRIx64 "] base: "),
6780 : : dwarf_dieoffset (&cudie));
6781 : 58 : print_dwarf_addr (dwflmod, address_size, base, base, out);
6782 : 58 : fprintf (out, "\n");
6783 : : }
6784 : 152 : last_cu = cu;
6785 : :
6786 [ - + ]: 152 : if (unlikely (data->d_size - offset < (size_t) address_size * 2))
6787 : : {
6788 : 0 : fprintf (out, _(" [%6tx] <INVALID DATA>\n"), offset);
6789 : 0 : break;
6790 : : }
6791 : :
6792 : 152 : Dwarf_Addr begin;
6793 : 152 : Dwarf_Addr end;
6794 [ + + ]: 152 : if (address_size == 8)
6795 : : {
6796 [ - + ]: 120 : begin = read_8ubyte_unaligned_inc (dbg, readp);
6797 [ - + ]: 120 : end = read_8ubyte_unaligned_inc (dbg, readp);
6798 : : }
6799 : : else
6800 : : {
6801 [ - + ]: 32 : begin = read_4ubyte_unaligned_inc (dbg, readp);
6802 [ - + ]: 32 : end = read_4ubyte_unaligned_inc (dbg, readp);
6803 [ + - ]: 32 : if (begin == (Dwarf_Addr) (uint32_t) -1)
6804 : : begin = (Dwarf_Addr) -1l;
6805 : : }
6806 : :
6807 [ - + ]: 152 : if (begin == (Dwarf_Addr) -1l) /* Base address entry. */
6808 : : {
6809 [ # # ]: 0 : if (first)
6810 : 0 : fprintf (out, " [%6tx] ", offset);
6811 : : else
6812 : 0 : fprintf (out, " ");
6813 : 0 : fputs (_("base address\n"), out);
6814 : 0 : fprintf (out, " ");
6815 : 0 : print_dwarf_addr (dwflmod, address_size, end, end, out);
6816 : 0 : fprintf (out, "\n");
6817 : 0 : base = end;
6818 : 0 : first = false;
6819 : : }
6820 [ + + ]: 152 : else if (begin == 0 && end == 0) /* End of list entry. */
6821 : : {
6822 [ - + ]: 60 : if (first)
6823 : 0 : fprintf (out, _(" [%6tx] empty list\n"), offset);
6824 : : first = true;
6825 : : }
6826 : : else
6827 : : {
6828 : : /* We have an address range entry. */
6829 [ + + ]: 92 : if (first) /* First address range entry in a list. */
6830 : 60 : fprintf (out, " [%6tx] ", offset);
6831 : : else
6832 : 32 : fprintf (out, " ");
6833 : :
6834 : 92 : fprintf (out, "range %" PRIx64 ", %" PRIx64 "\n", begin, end);
6835 [ + + ]: 92 : if (! print_unresolved_addresses)
6836 : : {
6837 : 72 : fprintf (out, " ");
6838 : 72 : print_dwarf_addr (dwflmod, address_size, base + begin,
6839 : : base + begin, out);
6840 : 72 : fprintf (out, "..\n ");
6841 : 72 : print_dwarf_addr (dwflmod, address_size,
6842 : : base + end - 1, base + end, out);
6843 : 72 : fprintf (out, "\n");
6844 : : }
6845 : :
6846 : : first = false;
6847 : : }
6848 : : }
6849 : : }
6850 : :
6851 : : #define REGNAMESZ 16
6852 : : static const char *
6853 : 39598 : register_info (Ebl *ebl, unsigned int regno, const Ebl_Register_Location *loc,
6854 : : char name[REGNAMESZ], int *bits, int *type)
6855 : : {
6856 : 39598 : const char *set;
6857 : 39598 : const char *pfx;
6858 : 39598 : int ignore;
6859 [ + + + + ]: 115402 : ssize_t n = ebl_register_info (ebl, regno, name, REGNAMESZ, &pfx, &set,
6860 : : bits ?: &ignore, type ?: &ignore);
6861 [ - + ]: 39598 : if (n <= 0)
6862 : : {
6863 [ # # ]: 0 : if (loc != NULL)
6864 : 0 : snprintf (name, REGNAMESZ, "reg%u", loc->regno);
6865 : : else
6866 : 0 : snprintf (name, REGNAMESZ, "??? 0x%x", regno);
6867 [ # # ]: 0 : if (bits != NULL)
6868 [ # # ]: 0 : *bits = loc != NULL ? loc->bits : 0;
6869 [ # # ]: 0 : if (type != NULL)
6870 : 0 : *type = DW_ATE_unsigned;
6871 : 0 : set = "??? unrecognized";
6872 : : }
6873 : : else
6874 : : {
6875 [ + + - + ]: 39598 : if (bits != NULL && *bits <= 0)
6876 [ # # ]: 0 : *bits = loc != NULL ? loc->bits : 0;
6877 [ + + - + ]: 39598 : if (type != NULL && *type == DW_ATE_void)
6878 : 0 : *type = DW_ATE_unsigned;
6879 : :
6880 : : }
6881 : 39598 : return set;
6882 : : }
6883 : :
6884 : : static const unsigned char *
6885 : 280 : read_encoded (unsigned int encoding, const unsigned char *readp,
6886 : : const unsigned char *const endp, uint64_t *res, Dwarf *dbg)
6887 : : {
6888 [ - + ]: 280 : if ((encoding & 0xf) == DW_EH_PE_absptr)
6889 : 0 : encoding = gelf_getclass (dbg->elf) == ELFCLASS32
6890 [ # # ]: 0 : ? DW_EH_PE_udata4 : DW_EH_PE_udata8;
6891 : :
6892 [ - - - + : 280 : switch (encoding & 0xf)
- - + -
- ]
6893 : : {
6894 : 0 : case DW_EH_PE_uleb128:
6895 [ # # ]: 0 : if (readp >= endp)
6896 : 0 : goto invalid;
6897 : 0 : get_uleb128 (*res, readp, endp);
6898 : 0 : break;
6899 : 0 : case DW_EH_PE_sleb128:
6900 [ # # ]: 0 : if (readp >= endp)
6901 : 0 : goto invalid;
6902 : 0 : get_sleb128 (*res, readp, endp);
6903 : 0 : break;
6904 : 0 : case DW_EH_PE_udata2:
6905 [ # # ]: 0 : if (readp + 2 > endp)
6906 : 0 : goto invalid;
6907 [ # # ]: 0 : *res = read_2ubyte_unaligned_inc (dbg, readp);
6908 : 0 : break;
6909 : 140 : case DW_EH_PE_udata4:
6910 [ - + ]: 140 : if (readp + 4 > endp)
6911 : 0 : goto invalid;
6912 [ - + ]: 140 : *res = read_4ubyte_unaligned_inc (dbg, readp);
6913 : 140 : break;
6914 : 0 : case DW_EH_PE_udata8:
6915 [ # # ]: 0 : if (readp + 8 > endp)
6916 : 0 : goto invalid;
6917 [ # # ]: 0 : *res = read_8ubyte_unaligned_inc (dbg, readp);
6918 : 0 : break;
6919 : 0 : case DW_EH_PE_sdata2:
6920 [ # # ]: 0 : if (readp + 2 > endp)
6921 : 0 : goto invalid;
6922 [ # # ]: 0 : *res = read_2sbyte_unaligned_inc (dbg, readp);
6923 : 0 : break;
6924 : 140 : case DW_EH_PE_sdata4:
6925 [ - + ]: 140 : if (readp + 4 > endp)
6926 : 0 : goto invalid;
6927 [ - + ]: 140 : *res = read_4sbyte_unaligned_inc (dbg, readp);
6928 : 140 : break;
6929 : 0 : case DW_EH_PE_sdata8:
6930 [ # # ]: 0 : if (readp + 8 > endp)
6931 : 0 : goto invalid;
6932 [ # # ]: 0 : *res = read_8sbyte_unaligned_inc (dbg, readp);
6933 : 0 : break;
6934 : : default:
6935 : 0 : invalid:
6936 : 0 : error (1, 0,
6937 : 0 : _("invalid encoding"));
6938 : : }
6939 : :
6940 : 280 : return readp;
6941 : : }
6942 : :
6943 : : static const char *
6944 : 37902 : regname (Ebl *ebl, unsigned int regno, char *regnamebuf)
6945 : : {
6946 : 37902 : register_info (ebl, regno, NULL, regnamebuf, NULL, NULL);
6947 : :
6948 : 37902 : return regnamebuf;
6949 : : }
6950 : :
6951 : : static void
6952 : 24520 : print_cfa_program (const unsigned char *readp, const unsigned char *const endp,
6953 : : Dwarf_Word vma_base, unsigned int code_align,
6954 : : int data_align,
6955 : : unsigned int version, unsigned int ptr_size,
6956 : : unsigned int encoding,
6957 : : Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr, Dwarf *dbg,
6958 : : FILE *out)
6959 : : {
6960 : 24520 : char regnamebuf[REGNAMESZ];
6961 : :
6962 : 24520 : fputs ("\n Program:\n", out);
6963 : 24520 : Dwarf_Word pc = vma_base;
6964 [ + + ]: 357224 : while (readp < endp)
6965 : : {
6966 : 332704 : unsigned int opcode = *readp++;
6967 : :
6968 [ + + ]: 332704 : if (opcode < DW_CFA_advance_loc)
6969 : : /* Extended opcode. */
6970 [ + - + + : 198352 : switch (opcode)
+ + - + -
- + + + +
+ + - + -
- - - - -
+ - - ]
6971 : : {
6972 : 68098 : uint64_t op1;
6973 : 68098 : int64_t sop1;
6974 : 68098 : uint64_t op2;
6975 : 68098 : int64_t sop2;
6976 : :
6977 : 68098 : case DW_CFA_nop:
6978 : 68098 : fputs (" nop\n", out);
6979 : 266450 : break;
6980 : 0 : case DW_CFA_set_loc:
6981 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
6982 : 0 : goto invalid;
6983 : 0 : readp = read_encoded (encoding, readp, endp, &op1, dbg);
6984 : 0 : fprintf (out, " set_loc %#" PRIx64 " to %#" PRIx64 "\n",
6985 : 0 : op1, pc = vma_base + op1);
6986 : 0 : break;
6987 : 6830 : case DW_CFA_advance_loc1:
6988 [ - + ]: 6830 : if ((uint64_t) (endp - readp) < 1)
6989 : 0 : goto invalid;
6990 : 13660 : fprintf (out, " advance_loc1 %u to %#" PRIx64 "\n",
6991 : 6830 : *readp, pc += *readp * code_align);
6992 : 6830 : ++readp;
6993 : 6830 : break;
6994 : 2884 : case DW_CFA_advance_loc2:
6995 [ - + ]: 2884 : if ((uint64_t) (endp - readp) < 2)
6996 : 0 : goto invalid;
6997 [ - + ]: 2884 : op1 = read_2ubyte_unaligned_inc (dbg, readp);
6998 : 2884 : fprintf (out, " advance_loc2 %" PRIu64 " to %#" PRIx64 "\n",
6999 : 2884 : op1, pc += op1 * code_align);
7000 : 2884 : break;
7001 : 36 : case DW_CFA_advance_loc4:
7002 [ - + ]: 36 : if ((uint64_t) (endp - readp) < 4)
7003 : 0 : goto invalid;
7004 [ + - ]: 36 : op1 = read_4ubyte_unaligned_inc (dbg, readp);
7005 : 36 : fprintf (out, " advance_loc4 %" PRIu64 " to %#" PRIx64 "\n",
7006 : 36 : op1, pc += op1 * code_align);
7007 : 36 : break;
7008 : 8 : case DW_CFA_offset_extended:
7009 [ - + ]: 8 : if ((uint64_t) (endp - readp) < 1)
7010 : 0 : goto invalid;
7011 : 8 : get_uleb128 (op1, readp, endp);
7012 [ - + ]: 8 : if ((uint64_t) (endp - readp) < 1)
7013 : 0 : goto invalid;
7014 : 8 : get_uleb128 (op2, readp, endp);
7015 : 8 : fprintf (out, " offset_extended r%" PRIu64 " (%s) at cfa%+" PRId64
7016 : : "\n",
7017 : : op1, regname (ebl, op1, regnamebuf), op2 * data_align);
7018 : 8 : break;
7019 : 0 : case DW_CFA_restore_extended:
7020 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
7021 : 0 : goto invalid;
7022 : 0 : get_uleb128 (op1, readp, endp);
7023 : 0 : fprintf (out, " restore_extended r%" PRIu64 " (%s)\n",
7024 : : op1, regname (ebl, op1, regnamebuf));
7025 : 0 : break;
7026 : 110 : case DW_CFA_undefined:
7027 [ - + ]: 110 : if ((uint64_t) (endp - readp) < 1)
7028 : 0 : goto invalid;
7029 : 110 : get_uleb128 (op1, readp, endp);
7030 : 110 : fprintf (out, " undefined r%" PRIu64 " (%s)\n", op1,
7031 : : regname (ebl, op1, regnamebuf));
7032 : 110 : break;
7033 : 0 : case DW_CFA_same_value:
7034 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
7035 : 0 : goto invalid;
7036 : 0 : get_uleb128 (op1, readp, endp);
7037 : 0 : fprintf (out, " same_value r%" PRIu64 " (%s)\n", op1,
7038 : : regname (ebl, op1, regnamebuf));
7039 : 0 : break;
7040 : 0 : case DW_CFA_register:
7041 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
7042 : 0 : goto invalid;
7043 : 0 : get_uleb128 (op1, readp, endp);
7044 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
7045 : 0 : goto invalid;
7046 : 0 : get_uleb128 (op2, readp, endp);
7047 : 0 : fprintf (out,
7048 : : " register r%" PRIu64 " (%s) in r%" PRIu64 " (%s)\n",
7049 : : op1, regname (ebl, op1, regnamebuf), op2,
7050 : : regname (ebl, op2, regnamebuf));
7051 : 0 : break;
7052 : 11934 : case DW_CFA_remember_state:
7053 : 11934 : fputs (" remember_state\n", out);
7054 : 11934 : break;
7055 : 11934 : case DW_CFA_restore_state:
7056 : 11934 : fputs (" restore_state\n", out);
7057 : 11934 : break;
7058 : 418 : case DW_CFA_def_cfa:
7059 [ - + ]: 418 : if ((uint64_t) (endp - readp) < 1)
7060 : 0 : goto invalid;
7061 : 418 : get_uleb128 (op1, readp, endp);
7062 [ - + ]: 418 : if ((uint64_t) (endp - readp) < 1)
7063 : 0 : goto invalid;
7064 : 418 : get_uleb128 (op2, readp, endp);
7065 : 418 : fprintf (out,
7066 : : " def_cfa r%" PRIu64 " (%s) at offset %" PRIu64 "\n",
7067 : : op1, regname (ebl, op1, regnamebuf), op2);
7068 : 418 : break;
7069 : 126 : case DW_CFA_def_cfa_register:
7070 [ - + ]: 126 : if ((uint64_t) (endp - readp) < 1)
7071 : 0 : goto invalid;
7072 : 126 : get_uleb128 (op1, readp, endp);
7073 : 126 : fprintf (out, " def_cfa_register r%" PRIu64 " (%s)\n",
7074 : : op1, regname (ebl, op1, regnamebuf));
7075 : 126 : break;
7076 : 95824 : case DW_CFA_def_cfa_offset:
7077 [ - + ]: 95824 : if ((uint64_t) (endp - readp) < 1)
7078 : 0 : goto invalid;
7079 : 95824 : get_uleb128 (op1, readp, endp);
7080 : 95824 : fprintf (out, " def_cfa_offset %" PRIu64 "\n", op1);
7081 : 95824 : break;
7082 : 118 : case DW_CFA_def_cfa_expression:
7083 [ - + ]: 118 : if ((uint64_t) (endp - readp) < 1)
7084 : 0 : goto invalid;
7085 : 118 : get_uleb128 (op1, readp, endp); /* Length of DW_FORM_block. */
7086 : 118 : fprintf (out, " def_cfa_expression %" PRIu64 "\n", op1);
7087 [ - + ]: 118 : if ((uint64_t) (endp - readp) < op1)
7088 : : {
7089 : 0 : invalid:
7090 : 0 : fputs (_(" <INVALID DATA>\n"), out);
7091 : 0 : return;
7092 : : }
7093 : 118 : print_ops (dwflmod, dbg, 10, 10, version, ptr_size, 0, NULL,
7094 : : op1, readp, out);
7095 : 118 : readp += op1;
7096 : 118 : break;
7097 : 0 : case DW_CFA_expression:
7098 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
7099 : 0 : goto invalid;
7100 : 0 : get_uleb128 (op1, readp, endp);
7101 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
7102 : 0 : goto invalid;
7103 : 0 : get_uleb128 (op2, readp, endp); /* Length of DW_FORM_block. */
7104 : 0 : fprintf (out, " expression r%" PRIu64 " (%s) \n",
7105 : : op1, regname (ebl, op1, regnamebuf));
7106 [ # # ]: 0 : if ((uint64_t) (endp - readp) < op2)
7107 : 0 : goto invalid;
7108 : 0 : print_ops (dwflmod, dbg, 10, 10, version, ptr_size, 0, NULL,
7109 : : op2, readp, out);
7110 : 0 : readp += op2;
7111 : 0 : break;
7112 : 24 : case DW_CFA_offset_extended_sf:
7113 [ - + ]: 24 : if ((uint64_t) (endp - readp) < 1)
7114 : 0 : goto invalid;
7115 : 24 : get_uleb128 (op1, readp, endp);
7116 [ - + ]: 24 : if ((uint64_t) (endp - readp) < 1)
7117 : 0 : goto invalid;
7118 : 24 : get_sleb128 (sop2, readp, endp);
7119 : 24 : fprintf (out, " offset_extended_sf r%" PRIu64 " (%s) at cfa%+"
7120 : : PRId64 "\n",
7121 : : op1, regname (ebl, op1, regnamebuf), sop2 * data_align);
7122 : 24 : break;
7123 : 0 : case DW_CFA_def_cfa_sf:
7124 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
7125 : 0 : goto invalid;
7126 : 0 : get_uleb128 (op1, readp, endp);
7127 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
7128 : 0 : goto invalid;
7129 : 0 : get_sleb128 (sop2, readp, endp);
7130 : 0 : fprintf (out,
7131 : : " def_cfa_sf r%" PRIu64 " (%s) at offset %" PRId64 "\n",
7132 : : op1, regname (ebl, op1, regnamebuf), sop2 * data_align);
7133 : 0 : break;
7134 : 0 : case DW_CFA_def_cfa_offset_sf:
7135 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
7136 : 0 : goto invalid;
7137 : 0 : get_sleb128 (sop1, readp, endp);
7138 : 0 : fprintf (out,
7139 : : " def_cfa_offset_sf %" PRId64 "\n", sop1 * data_align);
7140 : 0 : break;
7141 : 0 : case DW_CFA_val_offset:
7142 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
7143 : 0 : goto invalid;
7144 : 0 : get_uleb128 (op1, readp, endp);
7145 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
7146 : 0 : goto invalid;
7147 : 0 : get_uleb128 (op2, readp, endp);
7148 : 0 : fprintf (out, " val_offset %" PRIu64 " at offset %" PRIu64 "\n",
7149 : : op1, op2 * data_align);
7150 : 0 : break;
7151 : 0 : case DW_CFA_val_offset_sf:
7152 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
7153 : 0 : goto invalid;
7154 : 0 : get_uleb128 (op1, readp, endp);
7155 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
7156 : 0 : goto invalid;
7157 : 0 : get_sleb128 (sop2, readp, endp);
7158 : 0 : fprintf (out,
7159 : : " val_offset_sf %" PRIu64 " at offset %" PRId64 "\n",
7160 : : op1, sop2 * data_align);
7161 : 0 : break;
7162 : 0 : case DW_CFA_val_expression:
7163 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
7164 : 0 : goto invalid;
7165 : 0 : get_uleb128 (op1, readp, endp);
7166 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
7167 : 0 : goto invalid;
7168 : 0 : get_uleb128 (op2, readp, endp); /* Length of DW_FORM_block. */
7169 : 0 : fprintf (out, " val_expression r%" PRIu64 " (%s)\n",
7170 : : op1, regname (ebl, op1, regnamebuf));
7171 [ # # ]: 0 : if ((uint64_t) (endp - readp) < op2)
7172 : 0 : goto invalid;
7173 : 0 : print_ops (dwflmod, dbg, 10, 10, version, ptr_size, 0,
7174 : : NULL, op2, readp, out);
7175 : 0 : readp += op2;
7176 : 0 : break;
7177 : 0 : case DW_CFA_MIPS_advance_loc8:
7178 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 8)
7179 : 0 : goto invalid;
7180 [ # # ]: 0 : op1 = read_8ubyte_unaligned_inc (dbg, readp);
7181 : 0 : fprintf (out,
7182 : : " MIPS_advance_loc8 %" PRIu64 " to %#" PRIx64 "\n",
7183 : 0 : op1, pc += op1 * code_align);
7184 : 0 : break;
7185 : 8 : case DW_CFA_GNU_window_save: /* DW_CFA_AARCH64_negate_ra_state */
7186 [ + - ]: 8 : if (ehdr->e_machine == EM_AARCH64)
7187 : 8 : fputs (" AARCH64_negate_ra_state\n", out);
7188 : : else
7189 : 0 : fputs (" GNU_window_save\n", out);
7190 : : break;
7191 : 0 : case DW_CFA_GNU_args_size:
7192 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
7193 : 0 : goto invalid;
7194 : 0 : get_uleb128 (op1, readp, endp);
7195 : 0 : fprintf (out, " args_size %" PRIu64 "\n", op1);
7196 : 0 : break;
7197 : : default:
7198 : 0 : fprintf (out, " ??? (%u)\n", opcode);
7199 : 0 : break;
7200 : : }
7201 [ + + ]: 134352 : else if (opcode < DW_CFA_offset)
7202 : 97136 : fprintf (out, " advance_loc %u to %#" PRIx64 "\n",
7203 : 97136 : opcode & 0x3f, pc += (opcode & 0x3f) * code_align);
7204 [ + + ]: 37216 : else if (opcode < DW_CFA_restore)
7205 : : {
7206 : 33898 : uint64_t offset;
7207 [ - + ]: 33898 : if ((uint64_t) (endp - readp) < 1)
7208 : 0 : goto invalid;
7209 : 33898 : get_uleb128 (offset, readp, endp);
7210 : 33898 : fprintf (out, " offset r%u (%s) at cfa%+" PRId64 "\n",
7211 : : opcode & 0x3f, regname (ebl, opcode & 0x3f, regnamebuf),
7212 : : offset * data_align);
7213 : : }
7214 : : else
7215 : 3318 : fprintf (out, " restore r%u (%s)\n",
7216 : : opcode & 0x3f, regname (ebl, opcode & 0x3f, regnamebuf));
7217 : : }
7218 : : }
7219 : :
7220 : :
7221 : : static unsigned int
7222 : 24210 : encoded_ptr_size (int encoding, unsigned int ptr_size)
7223 : : {
7224 [ - + - + ]: 24210 : switch (encoding & 7)
7225 : : {
7226 : : case DW_EH_PE_udata4:
7227 : : return 4;
7228 : 0 : case DW_EH_PE_udata8:
7229 : 0 : return 8;
7230 : : case 0:
7231 : : return ptr_size;
7232 : : }
7233 : :
7234 : 0 : fprintf (stderr, "Unsupported pointer encoding: %#x, "
7235 : : "assuming pointer size of %d.\n", encoding, ptr_size);
7236 : 0 : return ptr_size;
7237 : : }
7238 : :
7239 : :
7240 : : static unsigned int
7241 : 642 : print_encoding (unsigned int val, FILE *out)
7242 : : {
7243 [ - - - + : 642 : switch (val & 0xf)
- - - + -
- ]
7244 : : {
7245 : 0 : case DW_EH_PE_absptr:
7246 : 0 : fputs ("absptr", out);
7247 : 0 : break;
7248 : 0 : case DW_EH_PE_uleb128:
7249 : 0 : fputs ("uleb128", out);
7250 : 0 : break;
7251 : 0 : case DW_EH_PE_udata2:
7252 : 0 : fputs ("udata2", out);
7253 : 0 : break;
7254 : 140 : case DW_EH_PE_udata4:
7255 : 140 : fputs ("udata4", out);
7256 : 140 : break;
7257 : 0 : case DW_EH_PE_udata8:
7258 : 0 : fputs ("udata8", out);
7259 : 0 : break;
7260 : 0 : case DW_EH_PE_sleb128:
7261 : 0 : fputs ("sleb128", out);
7262 : 0 : break;
7263 : 0 : case DW_EH_PE_sdata2:
7264 : 0 : fputs ("sdata2", out);
7265 : 0 : break;
7266 : 502 : case DW_EH_PE_sdata4:
7267 : 502 : fputs ("sdata4", out);
7268 : 502 : break;
7269 : 0 : case DW_EH_PE_sdata8:
7270 : 0 : fputs ("sdata8", out);
7271 : 0 : break;
7272 : : default:
7273 : : /* We did not use any of the bits after all. */
7274 : : return val;
7275 : : }
7276 : :
7277 : 642 : return val & ~0xf;
7278 : : }
7279 : :
7280 : :
7281 : : static unsigned int
7282 : 502 : print_relinfo (unsigned int val, FILE *out)
7283 : : {
7284 [ + - + - : 502 : switch (val & 0x70)
- - ]
7285 : : {
7286 : 362 : case DW_EH_PE_pcrel:
7287 : 362 : fputs ("pcrel", out);
7288 : 362 : break;
7289 : 0 : case DW_EH_PE_textrel:
7290 : 0 : fputs ("textrel", out);
7291 : 0 : break;
7292 : 140 : case DW_EH_PE_datarel:
7293 : 140 : fputs ("datarel", out);
7294 : 140 : break;
7295 : 0 : case DW_EH_PE_funcrel:
7296 : 0 : fputs ("funcrel", out);
7297 : 0 : break;
7298 : 0 : case DW_EH_PE_aligned:
7299 : 0 : fputs ("aligned", out);
7300 : 0 : break;
7301 : : default:
7302 : : return val;
7303 : : }
7304 : :
7305 : 502 : return val & ~0x70;
7306 : : }
7307 : :
7308 : :
7309 : : static void
7310 : 642 : print_encoding_base (const char *pfx, unsigned int fde_encoding,
7311 : : FILE *out)
7312 : : {
7313 : 642 : fprintf (out, "(%s", pfx);
7314 : :
7315 [ - + ]: 642 : if (fde_encoding == DW_EH_PE_omit)
7316 : 0 : fputs ("omit)\n", out);
7317 : : else
7318 : : {
7319 : 642 : unsigned int w = fde_encoding;
7320 : :
7321 : 642 : w = print_encoding (w, out);
7322 : :
7323 [ + + ]: 642 : if (w & 0x70)
7324 : : {
7325 [ + - ]: 502 : if (w != fde_encoding)
7326 : 502 : fputc (' ', out);
7327 : :
7328 : 502 : w = print_relinfo (w, out);
7329 : : }
7330 : :
7331 [ - + ]: 642 : if (w != 0)
7332 [ # # ]: 0 : fprintf (out, "%s%x", w != fde_encoding ? " " : "", w);
7333 : :
7334 : 642 : fputs (")\n", out);
7335 : : }
7336 : 642 : }
7337 : :
7338 : :
7339 : : static void
7340 : 196 : print_debug_frame_section (Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr,
7341 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg,
7342 : : FILE *out)
7343 : : {
7344 : 196 : size_t shstrndx;
7345 : : /* We know this call will succeed since it did in the caller. */
7346 : 196 : (void) elf_getshdrstrndx (ebl->elf, &shstrndx);
7347 : 196 : const char *scnname = section_name (ebl, shdr);
7348 : :
7349 : : /* Needed if we find PC-relative addresses. */
7350 : 196 : GElf_Addr bias;
7351 [ - + ]: 196 : if (dwfl_module_getelf (dwflmod, &bias) == NULL)
7352 : : {
7353 : 0 : error (0, 0, _("cannot get ELF: %s"), dwfl_errmsg (-1));
7354 : 0 : return;
7355 : : }
7356 : :
7357 : 196 : bool is_eh_frame = strcmp (scnname, ".eh_frame") == 0;
7358 : 196 : Elf_Data *data;
7359 [ + + ]: 196 : if (is_eh_frame)
7360 : : {
7361 : 144 : data = elf_rawdata (scn, NULL);
7362 [ - + ]: 144 : if (data == NULL)
7363 : : {
7364 : 0 : error (0, 0, _("cannot get %s content: %s"),
7365 : : scnname, elf_errmsg (-1));
7366 : 0 : return;
7367 : : }
7368 : : }
7369 : : else
7370 : : {
7371 : 52 : data = get_debug_elf_data (dbg, ebl, IDX_debug_frame, scn);
7372 [ + - ]: 52 : if (data == NULL)
7373 : : return;
7374 : : }
7375 : :
7376 [ + + ]: 196 : if (is_eh_frame)
7377 : 144 : fprintf (out, _("\
7378 : : \nCall frame information section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
7379 : 144 : elf_ndxscn (scn), scnname, (uint64_t) shdr->sh_offset);
7380 : : else
7381 : 52 : fprintf (out, _("\
7382 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
7383 : 52 : elf_ndxscn (scn), scnname, (uint64_t) shdr->sh_offset);
7384 : :
7385 : 196 : struct cieinfo
7386 : : {
7387 : : ptrdiff_t cie_offset;
7388 : : const char *augmentation;
7389 : : unsigned int code_alignment_factor;
7390 : : unsigned int data_alignment_factor;
7391 : : uint8_t address_size;
7392 : : uint8_t fde_encoding;
7393 : : uint8_t lsda_encoding;
7394 : : struct cieinfo *next;
7395 : 196 : } *cies = NULL;
7396 : :
7397 : 196 : const unsigned char *readp = data->d_buf;
7398 : 196 : const unsigned char *const dataend = ((unsigned char *) data->d_buf
7399 : 196 : + data->d_size);
7400 [ + + ]: 24856 : while (readp < dataend)
7401 : : {
7402 [ - + ]: 24660 : if (unlikely (readp + 4 > dataend))
7403 : : {
7404 : 0 : invalid_data:
7405 : 0 : error (0, 0, _("invalid data in section [%zu] '%s'"),
7406 : : elf_ndxscn (scn), scnname);
7407 : 0 : return;
7408 : : }
7409 : :
7410 : : /* At the beginning there must be a CIE. There can be multiple,
7411 : : hence we test tis in a loop. */
7412 : 24660 : ptrdiff_t offset = readp - (unsigned char *) data->d_buf;
7413 : :
7414 [ + + ]: 24660 : Dwarf_Word unit_length = read_4ubyte_unaligned_inc (dbg, readp);
7415 : 24660 : unsigned int length = 4;
7416 [ - + ]: 24660 : if (unlikely (unit_length == 0xffffffff))
7417 : : {
7418 [ # # ]: 0 : if (unlikely (readp + 8 > dataend))
7419 : 0 : goto invalid_data;
7420 : :
7421 [ # # ]: 0 : unit_length = read_8ubyte_unaligned_inc (dbg, readp);
7422 : 0 : length = 8;
7423 : : }
7424 : :
7425 [ + + ]: 24660 : if (unlikely (unit_length == 0))
7426 : : {
7427 : 140 : fprintf (out, _("\n [%6tx] Zero terminator\n"), offset);
7428 : 140 : continue;
7429 : : }
7430 : :
7431 : 24520 : Dwarf_Word maxsize = dataend - readp;
7432 [ - + ]: 24520 : if (unlikely (unit_length > maxsize))
7433 : 0 : goto invalid_data;
7434 : :
7435 [ + + ]: 24520 : unsigned int ptr_size = ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 4 : 8;
7436 : :
7437 : 24520 : ptrdiff_t start = readp - (unsigned char *) data->d_buf;
7438 : 24520 : const unsigned char *const cieend = readp + unit_length;
7439 [ - + ]: 24520 : if (unlikely (cieend > dataend))
7440 : 0 : goto invalid_data;
7441 : :
7442 : 24520 : Dwarf_Off cie_id;
7443 [ + - ]: 24520 : if (length == 4)
7444 : : {
7445 [ - + ]: 24520 : if (unlikely (cieend - readp < 4))
7446 : 0 : goto invalid_data;
7447 [ + + ]: 24520 : cie_id = read_4ubyte_unaligned_inc (dbg, readp);
7448 [ + + ]: 24520 : if (!is_eh_frame && cie_id == DW_CIE_ID_32)
7449 : : cie_id = DW_CIE_ID_64;
7450 : : }
7451 : : else
7452 : : {
7453 [ # # ]: 0 : if (unlikely (cieend - readp < 8))
7454 : 0 : goto invalid_data;
7455 [ # # ]: 0 : cie_id = read_8ubyte_unaligned_inc (dbg, readp);
7456 : : }
7457 : :
7458 : 48956 : uint_fast8_t version = 2;
7459 : 24436 : unsigned int code_alignment_factor;
7460 : 24436 : int data_alignment_factor;
7461 : 48956 : unsigned int fde_encoding = 0;
7462 : 48956 : unsigned int lsda_encoding = 0;
7463 : 48956 : Dwarf_Word initial_location = 0;
7464 : 48956 : Dwarf_Word vma_base = 0;
7465 : :
7466 [ + + + + ]: 24612 : if (cie_id == (is_eh_frame ? 0 : DW_CIE_ID_64))
7467 : : {
7468 [ - + ]: 310 : if (unlikely (cieend - readp < 2))
7469 : 0 : goto invalid_data;
7470 : 310 : version = *readp++;
7471 : 310 : const char *const augmentation = (const char *) readp;
7472 : 310 : readp = memchr (readp, '\0', cieend - readp);
7473 [ - + ]: 310 : if (unlikely (readp == NULL))
7474 : 0 : goto invalid_data;
7475 : 310 : ++readp;
7476 : :
7477 : 310 : uint_fast8_t segment_size = 0;
7478 [ + + ]: 310 : if (version >= 4)
7479 : : {
7480 [ - + ]: 8 : if (cieend - readp < 5)
7481 : 0 : goto invalid_data;
7482 : 8 : ptr_size = *readp++;
7483 : 8 : segment_size = *readp++;
7484 : : }
7485 : :
7486 [ - + ]: 310 : if (cieend - readp < 1)
7487 : 0 : goto invalid_data;
7488 : 310 : get_uleb128 (code_alignment_factor, readp, cieend);
7489 [ - + ]: 310 : if (cieend - readp < 1)
7490 : 0 : goto invalid_data;
7491 : 310 : get_sleb128 (data_alignment_factor, readp, cieend);
7492 : :
7493 : : /* In some variant for unwind data there is another field. */
7494 [ - + ]: 310 : if (strcmp (augmentation, "eh") == 0)
7495 [ # # ]: 0 : readp += ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 4 : 8;
7496 : :
7497 : 310 : unsigned int return_address_register;
7498 [ - + ]: 310 : if (cieend - readp < 1)
7499 : 0 : goto invalid_data;
7500 [ + + ]: 310 : if (unlikely (version == 1))
7501 : 286 : return_address_register = *readp++;
7502 : : else
7503 : 24 : get_uleb128 (return_address_register, readp, cieend);
7504 : :
7505 : 310 : fprintf (out, "\n [%6tx] CIE length=%" PRIu64 "\n"
7506 : : " CIE_id: %" PRIu64 "\n"
7507 : : " version: %u\n"
7508 : : " augmentation: \"%s\"\n",
7509 : : offset, (uint64_t) unit_length, (uint64_t) cie_id,
7510 : : version, augmentation);
7511 [ + + ]: 310 : if (version >= 4)
7512 : 8 : fprintf (out, " address_size: %u\n"
7513 : : " segment_size: %u\n",
7514 : : ptr_size, segment_size);
7515 : 310 : fprintf (out, " code_alignment_factor: %u\n"
7516 : : " data_alignment_factor: %d\n"
7517 : : " return_address_register: %u\n",
7518 : : code_alignment_factor,
7519 : : data_alignment_factor, return_address_register);
7520 : :
7521 [ + + ]: 310 : if (augmentation[0] == 'z')
7522 : : {
7523 [ - + ]: 222 : if (cieend - readp < 1)
7524 : 0 : goto invalid_data;
7525 : :
7526 : 222 : unsigned int augmentationlen;
7527 : 222 : get_uleb128 (augmentationlen, readp, cieend);
7528 : :
7529 [ - + ]: 222 : if (augmentationlen > (size_t) (cieend - readp))
7530 : : {
7531 : 0 : error (0, 0, _("invalid augmentation length"));
7532 : 0 : readp = cieend;
7533 : 0 : continue;
7534 : : }
7535 : :
7536 : 222 : const char *hdr = "Augmentation data:";
7537 : 222 : const char *cp = augmentation + 1;
7538 [ + + + - ]: 444 : while (*cp != '\0' && cp < augmentation + augmentationlen + 1)
7539 : : {
7540 : 222 : fprintf (out, " %-26s%#x ", hdr, *readp);
7541 : 222 : hdr = "";
7542 : :
7543 [ + - ]: 222 : if (*cp == 'R')
7544 : : {
7545 : 222 : fde_encoding = *readp++;
7546 : 222 : print_encoding_base (_("FDE address encoding: "),
7547 : : fde_encoding, out);
7548 : : }
7549 [ # # ]: 0 : else if (*cp == 'L')
7550 : : {
7551 : 0 : lsda_encoding = *readp++;
7552 : 0 : print_encoding_base (_("LSDA pointer encoding: "),
7553 : : lsda_encoding, out);
7554 : : }
7555 [ # # ]: 0 : else if (*cp == 'P')
7556 : : {
7557 : : /* Personality. This field usually has a relocation
7558 : : attached pointing to __gcc_personality_v0. */
7559 : 0 : const unsigned char *startp = readp;
7560 : 0 : unsigned int encoding = *readp++;
7561 : 0 : uint64_t val = 0;
7562 : 0 : readp = read_encoded (encoding, readp,
7563 : : readp - 1 + augmentationlen,
7564 : : &val, dbg);
7565 : :
7566 [ # # ]: 0 : while (++startp < readp)
7567 : 0 : fprintf (out, "%#x ", *startp);
7568 : :
7569 : 0 : fputc ('(', out);
7570 : 0 : print_encoding (encoding, out);
7571 : 0 : fputc (' ', out);
7572 [ # # ]: 0 : switch (encoding & 0xf)
7573 : : {
7574 : 0 : case DW_EH_PE_sleb128:
7575 : : case DW_EH_PE_sdata2:
7576 : : case DW_EH_PE_sdata4:
7577 : 0 : fprintf (out, "%" PRId64 ")\n", val);
7578 : 0 : break;
7579 : 0 : default:
7580 : 0 : fprintf (out, "%#" PRIx64 ")\n", val);
7581 : 0 : break;
7582 : : }
7583 : : }
7584 : : else
7585 : 0 : fprintf (out, "(%x)\n", *readp++);
7586 : :
7587 : 222 : ++cp;
7588 : : }
7589 : : }
7590 : :
7591 [ + - ]: 310 : if (likely (ptr_size == 4 || ptr_size == 8))
7592 : : {
7593 : 310 : struct cieinfo *newp = alloca (sizeof (*newp));
7594 : 310 : newp->cie_offset = offset;
7595 : 310 : newp->augmentation = augmentation;
7596 : 310 : newp->fde_encoding = fde_encoding;
7597 : 310 : newp->lsda_encoding = lsda_encoding;
7598 : 310 : newp->address_size = ptr_size;
7599 : 310 : newp->code_alignment_factor = code_alignment_factor;
7600 : 310 : newp->data_alignment_factor = data_alignment_factor;
7601 : 310 : newp->next = cies;
7602 : 310 : cies = newp;
7603 : : }
7604 : : }
7605 : : else
7606 : : {
7607 : : struct cieinfo *cie = cies;
7608 [ + - ]: 24210 : while (cie != NULL)
7609 [ + + - + ]: 48420 : if (is_eh_frame
7610 : 24118 : ? ((Dwarf_Off) start - cie_id) == (Dwarf_Off) cie->cie_offset
7611 : 92 : : cie_id == (Dwarf_Off) cie->cie_offset)
7612 : : break;
7613 : : else
7614 : 0 : cie = cie->next;
7615 : 24210 : if (unlikely (cie == NULL))
7616 : : {
7617 : 0 : fputs ("invalid CIE reference in FDE\n", out);
7618 : 0 : return;
7619 : : }
7620 : :
7621 : : /* Initialize from CIE data. */
7622 : 24210 : fde_encoding = cie->fde_encoding;
7623 : 24210 : lsda_encoding = cie->lsda_encoding;
7624 : 24210 : ptr_size = encoded_ptr_size (fde_encoding, cie->address_size);
7625 : 24210 : code_alignment_factor = cie->code_alignment_factor;
7626 : 24210 : data_alignment_factor = cie->data_alignment_factor;
7627 : :
7628 : 24210 : const unsigned char *base = readp;
7629 : : // XXX There are sometimes relocations for this value
7630 [ - + + + : 24210 : initial_location = read_addr_unaligned_inc (ptr_size, dbg, readp);
- + + + ]
7631 : 48420 : Dwarf_Word address_range
7632 [ + + - + : 24210 : = read_addr_unaligned_inc (ptr_size, dbg, readp);
+ + ]
7633 : :
7634 : : /* pcrel for an FDE address is relative to the runtime
7635 : : address of the start_address field itself. Sign extend
7636 : : if necessary to make sure the calculation is done on the
7637 : : full 64 bit address even when initial_location only holds
7638 : : the lower 32 bits. */
7639 : 24210 : Dwarf_Addr pc_start = initial_location;
7640 [ + + ]: 24210 : if (ptr_size == 4)
7641 : 24130 : pc_start = (uint64_t) (int32_t) pc_start;
7642 [ + + ]: 24210 : if ((fde_encoding & 0x70) == DW_EH_PE_pcrel)
7643 : 24106 : pc_start += ((uint64_t) shdr->sh_addr
7644 : 24106 : + (base - (const unsigned char *) data->d_buf)
7645 : 24106 : - bias);
7646 : :
7647 : 24210 : fprintf (out, "\n [%6tx] FDE length=%" PRIu64 " cie=[%6tx]\n"
7648 : : " CIE_pointer: %" PRIu64 "\n"
7649 : : " initial_location: ",
7650 : : offset, (uint64_t) unit_length,
7651 : : cie->cie_offset, (uint64_t) cie_id);
7652 : 24210 : print_dwarf_addr (dwflmod, cie->address_size,
7653 : : pc_start, initial_location, out);
7654 [ + + ]: 24210 : if ((fde_encoding & 0x70) == DW_EH_PE_pcrel)
7655 : : {
7656 : 48212 : vma_base = (((uint64_t) shdr->sh_offset
7657 : 24106 : + (base - (const unsigned char *) data->d_buf)
7658 : 24106 : + (uint64_t) initial_location)
7659 : : & (ptr_size == 4
7660 : : ? UINT64_C (0xffffffff)
7661 [ - + ]: 24106 : : UINT64_C (0xffffffffffffffff)));
7662 : 24106 : fprintf (out, _(" (offset: %#" PRIx64 ")"),
7663 : : (uint64_t) vma_base);
7664 : : }
7665 : :
7666 : 24210 : fprintf (out, "\n address_range: %#" PRIx64,
7667 : : (uint64_t) address_range);
7668 [ + + ]: 24210 : if ((fde_encoding & 0x70) == DW_EH_PE_pcrel)
7669 : 24106 : fprintf (out, _(" (end offset: %#" PRIx64 ")"),
7670 : 24106 : ((uint64_t) vma_base + (uint64_t) address_range)
7671 : : & (ptr_size == 4
7672 : : ? UINT64_C (0xffffffff)
7673 [ - + ]: 24106 : : UINT64_C (0xffffffffffffffff)));
7674 : 24210 : fputc ('\n', out);
7675 : :
7676 [ + + ]: 24210 : if (cie->augmentation[0] == 'z')
7677 : : {
7678 : 24106 : unsigned int augmentationlen;
7679 [ - + ]: 24106 : if (cieend - readp < 1)
7680 : 0 : goto invalid_data;
7681 : 24106 : get_uleb128 (augmentationlen, readp, cieend);
7682 : :
7683 [ - + ]: 24106 : if (augmentationlen > (size_t) (cieend - readp))
7684 : : {
7685 : 0 : error (0, 0, _("invalid augmentation length"));
7686 : 0 : readp = cieend;
7687 : 0 : continue;
7688 : : }
7689 : :
7690 [ - + ]: 24106 : if (augmentationlen > 0)
7691 : : {
7692 : 0 : const char *hdr = "Augmentation data:";
7693 : 0 : const char *cp = cie->augmentation + 1;
7694 : 0 : unsigned int u = 0;
7695 : 0 : while (*cp != '\0'
7696 [ # # # # ]: 0 : && cp < cie->augmentation + augmentationlen + 1)
7697 : : {
7698 [ # # ]: 0 : if (*cp == 'L')
7699 : : {
7700 : 0 : uint64_t lsda_pointer;
7701 : 0 : const unsigned char *p
7702 : 0 : = read_encoded (lsda_encoding, &readp[u],
7703 : : &readp[augmentationlen],
7704 : : &lsda_pointer, dbg);
7705 : 0 : u = p - readp;
7706 : 0 : fprintf (out, _("\
7707 : : %-26sLSDA pointer: %#" PRIx64 "\n"),
7708 : : hdr, lsda_pointer);
7709 : 0 : hdr = "";
7710 : : }
7711 : 0 : ++cp;
7712 : : }
7713 : :
7714 [ # # ]: 0 : while (u < augmentationlen)
7715 : : {
7716 : 0 : fprintf (out, " %-26s%#x\n", hdr, readp[u++]);
7717 : 0 : hdr = "";
7718 : : }
7719 : : }
7720 : :
7721 : 24106 : readp += augmentationlen;
7722 : : }
7723 : : }
7724 : :
7725 : : /* Handle the initialization instructions. */
7726 [ - + ]: 24520 : if (ptr_size != 4 && ptr_size !=8)
7727 : 0 : fprintf (out, "invalid CIE pointer size (%u), must be 4 or 8.\n",
7728 : : ptr_size);
7729 : : else
7730 : 24520 : print_cfa_program (readp, cieend, vma_base, code_alignment_factor,
7731 : : data_alignment_factor, version, ptr_size,
7732 : : fde_encoding, dwflmod, ebl, ehdr, dbg, out);
7733 : 24520 : readp = cieend;
7734 : : }
7735 : : }
7736 : :
7737 : :
7738 : : /* Returns the signedness (or false if it cannot be determined) and
7739 : : the byte size (or zero if it cannot be gotten) of the given DIE
7740 : : DW_AT_type attribute. Uses dwarf_peel_type and dwarf_aggregate_size. */
7741 : : static void
7742 : 353004 : die_type_sign_bytes (Dwarf_Die *die, bool *is_signed, int *bytes)
7743 : : {
7744 : 353004 : Dwarf_Attribute attr;
7745 : 353004 : Dwarf_Die type;
7746 : :
7747 : 353004 : *bytes = 0;
7748 : 353004 : *is_signed = false;
7749 : :
7750 [ + + ]: 353004 : if (dwarf_peel_type (dwarf_formref_die (dwarf_attr_integrate (die,
7751 : : DW_AT_type,
7752 : : &attr), &type),
7753 : : &type) == 0)
7754 : : {
7755 : 974 : Dwarf_Word val;
7756 : 974 : *is_signed = (dwarf_formudata (dwarf_attr (&type, DW_AT_encoding,
7757 : : &attr), &val) == 0
7758 [ + + + + ]: 974 : && (val == DW_ATE_signed || val == DW_ATE_signed_char));
7759 : :
7760 [ + - ]: 974 : if (dwarf_aggregate_size (&type, &val) == 0)
7761 : 974 : *bytes = val;
7762 : : }
7763 : 353004 : }
7764 : :
7765 : : struct attrcb_args
7766 : : {
7767 : : Dwfl_Module *dwflmod;
7768 : : Dwarf *dbg;
7769 : : Dwarf_Die *dies;
7770 : : int level;
7771 : : bool silent;
7772 : : bool is_split;
7773 : : unsigned int version;
7774 : : unsigned int addrsize;
7775 : : unsigned int offset_size;
7776 : : struct Dwarf_CU *cu;
7777 : : FILE *out;
7778 : : };
7779 : :
7780 : :
7781 : : static int
7782 : 8119144 : attr_callback (Dwarf_Attribute *attrp, void *arg)
7783 : : {
7784 : 8119144 : struct attrcb_args *cbargs = (struct attrcb_args *) arg;
7785 : 8119144 : const int level = cbargs->level;
7786 : 8119144 : Dwarf_Die *die = &cbargs->dies[level];
7787 : 8119144 : bool is_split = cbargs->is_split;
7788 : 8119144 : FILE *out = cbargs->out;
7789 : :
7790 [ + - ]: 8119144 : unsigned int attr = dwarf_whatattr (attrp);
7791 [ - + ]: 8119144 : if (unlikely (attr == 0))
7792 : : {
7793 [ # # ]: 0 : if (!cbargs->silent)
7794 : 0 : error (0, 0, _("DIE [%" PRIx64 "] "
7795 : : "cannot get attribute code: %s"),
7796 : : dwarf_dieoffset (die), dwarf_errmsg (-1));
7797 : 0 : return DWARF_CB_ABORT;
7798 : : }
7799 : :
7800 [ - + ]: 8119144 : unsigned int form = dwarf_whatform (attrp);
7801 [ - + ]: 8119144 : if (unlikely (form == 0))
7802 : : {
7803 [ # # ]: 0 : if (!cbargs->silent)
7804 : 0 : error (0, 0, _("DIE [%" PRIx64 "] "
7805 : : "cannot get attribute form: %s"),
7806 : : dwarf_dieoffset (die), dwarf_errmsg (-1));
7807 : 0 : return DWARF_CB_ABORT;
7808 : : }
7809 : :
7810 [ + + + + : 8119144 : switch (form)
+ + + +
- ]
7811 : : {
7812 : 103118 : case DW_FORM_addr:
7813 : : case DW_FORM_addrx:
7814 : : case DW_FORM_addrx1:
7815 : : case DW_FORM_addrx2:
7816 : : case DW_FORM_addrx3:
7817 : : case DW_FORM_addrx4:
7818 : : case DW_FORM_GNU_addr_index:
7819 [ + + ]: 103118 : if (!cbargs->silent)
7820 : : {
7821 : 102576 : Dwarf_Addr addr;
7822 [ - + ]: 102576 : if (unlikely (dwarf_formaddr (attrp, &addr) != 0))
7823 : : {
7824 : 0 : attrval_out:
7825 [ # # ]: 0 : if (!cbargs->silent)
7826 : 0 : error (0, 0, _("DIE [%" PRIx64 "] "
7827 : : "cannot get attribute '%s' (%s) value: "
7828 : : "%s"),
7829 : : dwarf_dieoffset (die),
7830 : : dwarf_attr_name (attr),
7831 : : dwarf_form_name (form),
7832 : : dwarf_errmsg (-1));
7833 : : /* Don't ABORT, it might be other attributes can be resolved. */
7834 : 0 : return DWARF_CB_OK;
7835 : : }
7836 [ + + ]: 102576 : if (form != DW_FORM_addr )
7837 : : {
7838 : 40 : Dwarf_Word word;
7839 [ - + ]: 40 : if (dwarf_formudata (attrp, &word) != 0)
7840 : 0 : goto attrval_out;
7841 : 40 : fprintf (out, " %*s%-20s (%s) [%" PRIx64 "] ",
7842 : : (int) (level * 2), "", dwarf_attr_name (attr),
7843 : : dwarf_form_name (form), word);
7844 : : }
7845 : : else
7846 : 102536 : fprintf (out, " %*s%-20s (%s) ",
7847 : : (int) (level * 2), "", dwarf_attr_name (attr),
7848 : : dwarf_form_name (form));
7849 : 102576 : print_dwarf_addr (cbargs->dwflmod, cbargs->addrsize, addr, addr, out);
7850 : 102576 : fprintf (out, "\n");
7851 : : }
7852 : 7299362 : break;
7853 : :
7854 : 1344224 : case DW_FORM_indirect:
7855 : : case DW_FORM_strp:
7856 : : case DW_FORM_line_strp:
7857 : : case DW_FORM_strx:
7858 : : case DW_FORM_strx1:
7859 : : case DW_FORM_strx2:
7860 : : case DW_FORM_strx3:
7861 : : case DW_FORM_strx4:
7862 : : case DW_FORM_string:
7863 : : case DW_FORM_GNU_strp_alt:
7864 : : case DW_FORM_GNU_str_index:
7865 [ + + ]: 1344224 : if (cbargs->silent)
7866 : : break;
7867 : 1342714 : const char *str = dwarf_formstring (attrp);
7868 [ - + ]: 1342714 : if (unlikely (str == NULL))
7869 : 0 : goto attrval_out;
7870 : 1342714 : fprintf (out, " %*s%-20s (%s) \"%s\"\n",
7871 : : (int) (level * 2), "", dwarf_attr_name (attr),
7872 : : dwarf_form_name (form), str);
7873 : 1342714 : break;
7874 : :
7875 : 1755222 : case DW_FORM_ref_addr:
7876 : : case DW_FORM_ref_udata:
7877 : : case DW_FORM_ref8:
7878 : : case DW_FORM_ref4:
7879 : : case DW_FORM_ref2:
7880 : : case DW_FORM_ref1:
7881 : : case DW_FORM_GNU_ref_alt:
7882 : : case DW_FORM_ref_sup4:
7883 : : case DW_FORM_ref_sup8:
7884 [ + + ]: 1755222 : if (cbargs->silent)
7885 : : break;
7886 : 1753642 : Dwarf_Die ref;
7887 [ - + ]: 1753642 : if (unlikely (dwarf_formref_die (attrp, &ref) == NULL))
7888 : 0 : goto attrval_out;
7889 : :
7890 : 1753642 : fprintf (out, " %*s%-20s (%s) ",
7891 : : (int) (level * 2), "", dwarf_attr_name (attr),
7892 : : dwarf_form_name (form));
7893 [ + + ]: 1753642 : if (is_split)
7894 : 66 : fprintf (out, "{%6" PRIxMAX "}\n", (uintmax_t) dwarf_dieoffset (&ref));
7895 : : else
7896 : 1753576 : fprintf (out, "[%6" PRIxMAX "]\n", (uintmax_t) dwarf_dieoffset (&ref));
7897 : : break;
7898 : :
7899 : 8 : case DW_FORM_ref_sig8:
7900 [ + - ]: 8 : if (cbargs->silent)
7901 : : break;
7902 : 8 : fprintf (out, " %*s%-20s (%s) {%6" PRIx64 "}\n",
7903 : : (int) (level * 2), "", dwarf_attr_name (attr),
7904 : : dwarf_form_name (form),
7905 [ - + ]: 8 : (uint64_t) read_8ubyte_unaligned (attrp->cu->dbg, attrp->valp));
7906 : 8 : break;
7907 : :
7908 : 4513904 : case DW_FORM_sec_offset:
7909 : : case DW_FORM_rnglistx:
7910 : : case DW_FORM_loclistx:
7911 : : case DW_FORM_implicit_const:
7912 : : case DW_FORM_udata:
7913 : : case DW_FORM_sdata:
7914 : : case DW_FORM_data8: /* Note no data16 here, we see that as block. */
7915 : : case DW_FORM_data4:
7916 : : case DW_FORM_data2:
7917 : 4513904 : case DW_FORM_data1:;
7918 : 4513904 : Dwarf_Word num;
7919 [ - + ]: 4513904 : if (unlikely (dwarf_formudata (attrp, &num) != 0))
7920 : 0 : goto attrval_out;
7921 : :
7922 : 4513904 : const char *valuestr = NULL;
7923 : 4513904 : bool as_hex_id = false;
7924 [ + + + + : 4513904 : switch (attr)
+ + + + +
+ - - - -
- - + - +
+ + ]
7925 : : {
7926 : : /* This case can take either a constant or a loclistptr. */
7927 : 562104 : case DW_AT_data_member_location:
7928 [ + - ]: 562104 : if (form != DW_FORM_sec_offset
7929 [ + + ]: 562104 : && (cbargs->version >= 4
7930 [ + - ]: 47612 : || (form != DW_FORM_data4 && form != DW_FORM_data8)))
7931 : : {
7932 [ + - ]: 562104 : if (!cbargs->silent)
7933 : 562104 : fprintf (out, " %*s%-20s (%s) %" PRIuMAX "\n",
7934 : : (int) (level * 2), "", dwarf_attr_name (attr),
7935 : : dwarf_form_name (form), (uintmax_t) num);
7936 : 562104 : return DWARF_CB_OK;
7937 : : }
7938 : 221870 : FALLTHROUGH;
7939 : :
7940 : : /* These cases always take a loclist[ptr] and no constant. */
7941 : : case DW_AT_location:
7942 : : case DW_AT_data_location:
7943 : : case DW_AT_vtable_elem_location:
7944 : : case DW_AT_string_length:
7945 : : case DW_AT_use_location:
7946 : : case DW_AT_frame_base:
7947 : : case DW_AT_return_addr:
7948 : : case DW_AT_static_link:
7949 : : case DW_AT_segment:
7950 : : case DW_AT_GNU_call_site_value:
7951 : : case DW_AT_GNU_call_site_data_value:
7952 : : case DW_AT_GNU_call_site_target:
7953 : : case DW_AT_GNU_call_site_target_clobbered:
7954 : : case DW_AT_GNU_locviews:
7955 : : {
7956 : 221870 : bool nlpt;
7957 [ + + ]: 221870 : if (cbargs->cu->version < 5)
7958 : : {
7959 [ + + ]: 324 : if (! cbargs->is_split)
7960 : : {
7961 : 284 : nlpt = notice_listptr (section_loc, &known_locsptr,
7962 : 284 : cbargs->addrsize,
7963 : 284 : cbargs->offset_size,
7964 : : cbargs->cu, num, attr);
7965 : : }
7966 : : else
7967 : : nlpt = true;
7968 : : }
7969 : : else
7970 : : {
7971 : : /* Only register for a real section offset. Otherwise
7972 : : it is a DW_FORM_loclistx which is just an index
7973 : : number and we should already have registered the
7974 : : section offset for the index when we saw the
7975 : : DW_AT_loclists_base CU attribute. */
7976 [ + + ]: 221546 : if (form == DW_FORM_sec_offset)
7977 : 221468 : nlpt = notice_listptr (section_loc, &known_loclistsptr,
7978 : 221468 : cbargs->addrsize, cbargs->offset_size,
7979 : : cbargs->cu, num, attr);
7980 : : else
7981 : : nlpt = true;
7982 : :
7983 : : }
7984 : :
7985 [ + + ]: 221870 : if (!cbargs->silent)
7986 : : {
7987 [ + + + + ]: 221504 : if (cbargs->cu->version < 5 || form == DW_FORM_sec_offset)
7988 [ - + ]: 221490 : fprintf (out, " %*s%-20s (%s) location list [%6"
7989 : : PRIxMAX "]%s\n",
7990 : : (int) (level * 2), "", dwarf_attr_name (attr),
7991 : : dwarf_form_name (form), (uintmax_t) num,
7992 : : nlpt ? "" : " <WARNING offset too big>");
7993 : : else
7994 : 14 : fprintf (out, " %*s%-20s (%s) location index [%6"
7995 : : PRIxMAX "]\n",
7996 : : (int) (level * 2), "", dwarf_attr_name (attr),
7997 : : dwarf_form_name (form), (uintmax_t) num);
7998 : : }
7999 : : }
8000 : : return DWARF_CB_OK;
8001 : :
8002 : 10 : case DW_AT_loclists_base:
8003 : : {
8004 : 20 : bool nlpt = notice_listptr (section_loc, &known_loclistsptr,
8005 : 10 : cbargs->addrsize, cbargs->offset_size,
8006 : : cbargs->cu, num, attr);
8007 : :
8008 [ + + ]: 10 : if (!cbargs->silent)
8009 [ - + ]: 2 : fprintf (out, " %*s%-20s (%s) location list [%6" PRIxMAX "]%s\n",
8010 : : (int) (level * 2), "", dwarf_attr_name (attr),
8011 : : dwarf_form_name (form), (uintmax_t) num,
8012 : : nlpt ? "" : " <WARNING offset too big>");
8013 : : }
8014 : : return DWARF_CB_OK;
8015 : :
8016 : 35748 : case DW_AT_ranges:
8017 : : case DW_AT_start_scope:
8018 : : {
8019 : 35748 : bool nlpt;
8020 [ + + ]: 35748 : if (cbargs->cu->version < 5)
8021 : 106 : nlpt = notice_listptr (section_ranges, &known_rangelistptr,
8022 : 106 : cbargs->addrsize, cbargs->offset_size,
8023 : : cbargs->cu, num, attr);
8024 : : else
8025 : : {
8026 : : /* Only register for a real section offset. Otherwise
8027 : : it is a DW_FORM_rangelistx which is just an index
8028 : : number and we should already have registered the
8029 : : section offset for the index when we saw the
8030 : : DW_AT_rnglists_base CU attribute. */
8031 [ + + ]: 35642 : if (form == DW_FORM_sec_offset)
8032 : 35630 : nlpt = notice_listptr (section_ranges, &known_rnglistptr,
8033 : 35630 : cbargs->addrsize, cbargs->offset_size,
8034 : : cbargs->cu, num, attr);
8035 : : else
8036 : : nlpt = true;
8037 : : }
8038 : :
8039 [ + + ]: 35748 : if (!cbargs->silent)
8040 : : {
8041 [ + + + + ]: 35662 : if (cbargs->cu->version < 5 || form == DW_FORM_sec_offset)
8042 [ - + ]: 35658 : fprintf (out, " %*s%-20s (%s) range list [%6"
8043 : : PRIxMAX "]%s\n",
8044 : : (int) (level * 2), "", dwarf_attr_name (attr),
8045 : : dwarf_form_name (form), (uintmax_t) num,
8046 : : nlpt ? "" : " <WARNING offset too big>");
8047 : : else
8048 : 4 : fprintf (out, " %*s%-20s (%s) range index [%6"
8049 : : PRIxMAX "]\n",
8050 : : (int) (level * 2), "", dwarf_attr_name (attr),
8051 : : dwarf_form_name (form), (uintmax_t) num);
8052 : : }
8053 : : }
8054 : : return DWARF_CB_OK;
8055 : :
8056 : 8 : case DW_AT_rnglists_base:
8057 : : {
8058 : 16 : bool nlpt = notice_listptr (section_ranges, &known_rnglistptr,
8059 : 8 : cbargs->addrsize, cbargs->offset_size,
8060 : : cbargs->cu, num, attr);
8061 [ + + ]: 8 : if (!cbargs->silent)
8062 [ - + ]: 4 : fprintf (out, " %*s%-20s (%s) range list [%6"
8063 : : PRIxMAX "]%s\n",
8064 : : (int) (level * 2), "", dwarf_attr_name (attr),
8065 : : dwarf_form_name (form), (uintmax_t) num,
8066 : : nlpt ? "" : " <WARNING offset too big>");
8067 : : }
8068 : : return DWARF_CB_OK;
8069 : :
8070 : 34 : case DW_AT_addr_base:
8071 : : case DW_AT_GNU_addr_base:
8072 : : {
8073 : 68 : bool addrbase = notice_listptr (section_addr, &known_addrbases,
8074 : 34 : cbargs->addrsize,
8075 : 34 : cbargs->offset_size,
8076 : : cbargs->cu, num, attr);
8077 [ + + ]: 34 : if (!cbargs->silent)
8078 [ - + ]: 18 : fprintf (out, " %*s%-20s (%s) address base [%6"
8079 : : PRIxMAX "]%s\n",
8080 : : (int) (level * 2), "", dwarf_attr_name (attr),
8081 : : dwarf_form_name (form), (uintmax_t) num,
8082 : : addrbase ? "" : " <WARNING offset too big>");
8083 : : }
8084 : : return DWARF_CB_OK;
8085 : :
8086 : 8 : case DW_AT_str_offsets_base:
8087 : : {
8088 : 16 : bool stroffbase = notice_listptr (section_str, &known_stroffbases,
8089 : 8 : cbargs->addrsize,
8090 : 8 : cbargs->offset_size,
8091 : : cbargs->cu, num, attr);
8092 [ + - ]: 8 : if (!cbargs->silent)
8093 [ - + ]: 8 : fprintf (out, " %*s%-20s (%s) str offsets base [%6"
8094 : : PRIxMAX "]%s\n",
8095 : : (int) (level * 2), "", dwarf_attr_name (attr),
8096 : : dwarf_form_name (form), (uintmax_t) num,
8097 : : stroffbase ? "" : " <WARNING offset too big>");
8098 : : }
8099 : : return DWARF_CB_OK;
8100 : :
8101 : 3736 : case DW_AT_language:
8102 : 3736 : valuestr = dwarf_lang_name (num);
8103 : 3736 : break;
8104 : 4 : case DW_AT_language_name:
8105 : 4 : valuestr = dwarf_lname_name (num);
8106 : 4 : break;
8107 : 59402 : case DW_AT_encoding:
8108 : 59402 : valuestr = dwarf_encoding_name (num);
8109 : 59402 : break;
8110 : 0 : case DW_AT_accessibility:
8111 : 0 : valuestr = dwarf_access_name (num);
8112 : 0 : break;
8113 : 0 : case DW_AT_defaulted:
8114 : 0 : valuestr = dwarf_defaulted_name (num);
8115 : 0 : break;
8116 : 0 : case DW_AT_visibility:
8117 : 0 : valuestr = dwarf_visibility_name (num);
8118 : 0 : break;
8119 : 0 : case DW_AT_virtuality:
8120 : 0 : valuestr = dwarf_virtuality_name (num);
8121 : 0 : break;
8122 : 0 : case DW_AT_identifier_case:
8123 : 0 : valuestr = dwarf_identifier_case_name (num);
8124 : 0 : break;
8125 : 0 : case DW_AT_calling_convention:
8126 : 0 : valuestr = dwarf_calling_convention_name (num);
8127 : 0 : break;
8128 : 7280 : case DW_AT_inline:
8129 : 7280 : valuestr = dwarf_inline_name (num);
8130 : 7280 : break;
8131 : 0 : case DW_AT_ordering:
8132 : 0 : valuestr = dwarf_ordering_name (num);
8133 : 0 : break;
8134 : 991928 : case DW_AT_decl_file:
8135 : : case DW_AT_call_file:
8136 : : {
8137 [ + + ]: 991928 : if (cbargs->silent)
8138 : : break;
8139 : :
8140 : : /* Try to get the actual file, the current interface only
8141 : : gives us full paths, but we only want to show the file
8142 : : name for now. */
8143 : 991080 : Dwarf_Die cudie;
8144 [ + - ]: 991080 : if (dwarf_cu_die (cbargs->cu, &cudie,
8145 : : NULL, NULL, NULL, NULL, NULL, NULL) != NULL)
8146 : : {
8147 : 991080 : Dwarf_Files *files;
8148 : 991080 : size_t nfiles;
8149 [ + - ]: 991080 : if (dwarf_getsrcfiles (&cudie, &files, &nfiles) == 0)
8150 : : {
8151 : 991080 : valuestr = dwarf_filesrc (files, num, NULL, NULL);
8152 [ + - ]: 991080 : if (valuestr != NULL)
8153 : : {
8154 : 991080 : char *filename = strrchr (valuestr, '/');
8155 [ - + ]: 991080 : if (filename != NULL)
8156 : 991080 : valuestr = filename + 1;
8157 : : }
8158 : : else
8159 : 0 : error (0, 0, _("invalid file (%" PRId64 "): %s"),
8160 : : num, dwarf_errmsg (-1));
8161 : : }
8162 : : else
8163 : 0 : error (0, 0, _("no srcfiles for CU [%" PRIx64 "]"),
8164 : : dwarf_dieoffset (&cudie));
8165 : : }
8166 : : else
8167 : 0 : error (0, 0, _("couldn't get DWARF CU: %s"),
8168 : : dwarf_errmsg (-1));
8169 : 991080 : if (valuestr == NULL)
8170 : : valuestr = "???";
8171 : : }
8172 : 991080 : break;
8173 : 26 : case DW_AT_GNU_dwo_id:
8174 : 26 : as_hex_id = true;
8175 : 26 : break;
8176 : :
8177 : : default:
8178 : : /* Nothing. */
8179 : : break;
8180 : : }
8181 : :
8182 [ + + ]: 3694122 : if (cbargs->silent)
8183 : : break;
8184 : :
8185 : : /* When highpc is in constant form it is relative to lowpc.
8186 : : In that case also show the address. */
8187 : 3690412 : Dwarf_Addr highpc;
8188 [ + + + - ]: 3690412 : if (attr == DW_AT_high_pc && dwarf_highpc (die, &highpc) == 0)
8189 : : {
8190 : 23998 : fprintf (out, " %*s%-20s (%s) %" PRIuMAX " (",
8191 : : (int) (level * 2), "", dwarf_attr_name (attr),
8192 : : dwarf_form_name (form), (uintmax_t) num);
8193 : 23998 : print_dwarf_addr (cbargs->dwflmod, cbargs->addrsize,
8194 : : highpc, highpc, out);
8195 : 23998 : fprintf (out, ")\n");
8196 : : }
8197 : : else
8198 : : {
8199 [ + + ]: 3666414 : if (as_hex_id)
8200 : : {
8201 : 4 : fprintf (out, " %*s%-20s (%s) 0x%.16" PRIx64 "\n",
8202 : : (int) (level * 2), "", dwarf_attr_name (attr),
8203 : : dwarf_form_name (form), num);
8204 : : }
8205 : : else
8206 : : {
8207 : 3666410 : Dwarf_Sword snum = 0;
8208 : 3666410 : bool is_signed;
8209 : 3666410 : int bytes = 0;
8210 [ + + ]: 3666410 : if (attr == DW_AT_const_value)
8211 : 352996 : die_type_sign_bytes (die, &is_signed, &bytes);
8212 : : else
8213 : 3313414 : is_signed = (form == DW_FORM_sdata
8214 : 3313414 : || form == DW_FORM_implicit_const);
8215 : :
8216 [ + + ]: 3666410 : if (is_signed)
8217 [ - + ]: 381668 : if (unlikely (dwarf_formsdata (attrp, &snum) != 0))
8218 : 0 : goto attrval_out;
8219 : :
8220 [ + + ]: 3666410 : if (valuestr == NULL)
8221 : : {
8222 : 2605518 : fprintf (out, " %*s%-20s (%s) ",
8223 : : (int) (level * 2), "", dwarf_attr_name (attr),
8224 : : dwarf_form_name (form));
8225 : : }
8226 : : else
8227 : : {
8228 : 1060892 : fprintf (out, " %*s%-20s (%s) %s (",
8229 : : (int) (level * 2), "", dwarf_attr_name (attr),
8230 : : dwarf_form_name (form), valuestr);
8231 : : }
8232 : :
8233 [ + + + + : 3666410 : switch (bytes)
+ ]
8234 : : {
8235 : 10 : case 1:
8236 [ + + ]: 10 : if (is_signed)
8237 : 2 : fprintf (out, "%" PRId8, (int8_t) snum);
8238 : : else
8239 : 8 : fprintf (out, "%" PRIu8, (uint8_t) num);
8240 : : break;
8241 : :
8242 : 4 : case 2:
8243 [ + + ]: 4 : if (is_signed)
8244 : 2 : fprintf (out, "%" PRId16, (int16_t) snum);
8245 : : else
8246 : 2 : fprintf (out, "%" PRIu16, (uint16_t) num);
8247 : : break;
8248 : :
8249 : 234 : case 4:
8250 [ + + ]: 234 : if (is_signed)
8251 : 210 : fprintf (out, "%" PRId32, (int32_t) snum);
8252 : : else
8253 : 24 : fprintf (out, "%" PRIu32, (uint32_t) num);
8254 : : break;
8255 : :
8256 : 718 : case 8:
8257 [ + + ]: 718 : if (is_signed)
8258 : 2 : fprintf (out, "%" PRId64, (int64_t) snum);
8259 : : else
8260 : 716 : fprintf (out, "%" PRIu64, (uint64_t) num);
8261 : : break;
8262 : :
8263 : 3665444 : default:
8264 [ + + ]: 3665444 : if (is_signed)
8265 : 381452 : fprintf (out, "%" PRIdMAX, (intmax_t) snum);
8266 : : else
8267 : 3283992 : fprintf (out, "%" PRIuMAX, (uintmax_t) num);
8268 : : break;
8269 : : }
8270 : :
8271 : : /* Make clear if we switched from a signed encoding to
8272 : : an unsigned value. */
8273 [ + + ]: 3666410 : if (attr == DW_AT_const_value
8274 [ + + ]: 352996 : && (form == DW_FORM_sdata || form == DW_FORM_implicit_const)
8275 [ + + ]: 2680 : && !is_signed)
8276 : 2662 : fprintf (out, " (%" PRIdMAX ")", (intmax_t) num);
8277 : :
8278 [ + + ]: 3666410 : if (valuestr == NULL)
8279 : 2605518 : fprintf (out, "\n");
8280 : : else
8281 : 1060892 : fprintf (out, ")\n");
8282 : : }
8283 : : }
8284 : : break;
8285 : :
8286 : 18244 : case DW_FORM_flag:
8287 [ + + ]: 18244 : if (cbargs->silent)
8288 : : break;
8289 : 18096 : bool flag;
8290 [ - + ]: 18096 : if (unlikely (dwarf_formflag (attrp, &flag) != 0))
8291 : 0 : goto attrval_out;
8292 : :
8293 : 18096 : fprintf (out, " %*s%-20s (%s) %s\n",
8294 : : (int) (level * 2), "", dwarf_attr_name (attr),
8295 [ + - ]: 18096 : dwarf_form_name (form), flag ? yes_str : no_str);
8296 : 18096 : break;
8297 : :
8298 : 160734 : case DW_FORM_flag_present:
8299 [ + + ]: 160734 : if (cbargs->silent)
8300 : : break;
8301 : 160180 : fprintf (out, " %*s%-20s (%s) %s\n",
8302 : : (int) (level * 2), "", dwarf_attr_name (attr),
8303 : : dwarf_form_name (form), yes_str);
8304 : 160180 : break;
8305 : :
8306 : 223690 : case DW_FORM_exprloc:
8307 : : case DW_FORM_block4:
8308 : : case DW_FORM_block2:
8309 : : case DW_FORM_block1:
8310 : : case DW_FORM_block:
8311 : : case DW_FORM_data16: /* DWARF5 calls this a constant class. */
8312 [ + + ]: 223690 : if (cbargs->silent)
8313 : : break;
8314 : 223428 : Dwarf_Block block;
8315 [ - + ]: 223428 : if (unlikely (dwarf_formblock (attrp, &block) != 0))
8316 : 0 : goto attrval_out;
8317 : :
8318 : 223428 : fprintf (out, " %*s%-20s (%s) ",
8319 : : (int) (level * 2), "", dwarf_attr_name (attr),
8320 : : dwarf_form_name (form));
8321 : :
8322 [ + + + ]: 223428 : switch (attr)
8323 : : {
8324 : 96350 : default:
8325 [ - + ]: 96350 : if (form != DW_FORM_exprloc)
8326 : : {
8327 : 0 : print_block (block.length, block.data, out);
8328 : 0 : break;
8329 : : }
8330 : 127070 : FALLTHROUGH;
8331 : :
8332 : : case DW_AT_location:
8333 : : case DW_AT_data_location:
8334 : : case DW_AT_data_member_location:
8335 : : case DW_AT_vtable_elem_location:
8336 : : case DW_AT_string_length:
8337 : : case DW_AT_use_location:
8338 : : case DW_AT_frame_base:
8339 : : case DW_AT_return_addr:
8340 : : case DW_AT_static_link:
8341 : : case DW_AT_allocated:
8342 : : case DW_AT_associated:
8343 : : case DW_AT_bit_size:
8344 : : case DW_AT_bit_offset:
8345 : : case DW_AT_bit_stride:
8346 : : case DW_AT_byte_size:
8347 : : case DW_AT_byte_stride:
8348 : : case DW_AT_count:
8349 : : case DW_AT_lower_bound:
8350 : : case DW_AT_upper_bound:
8351 : : case DW_AT_GNU_call_site_value:
8352 : : case DW_AT_GNU_call_site_data_value:
8353 : : case DW_AT_GNU_call_site_target:
8354 : : case DW_AT_GNU_call_site_target_clobbered:
8355 [ + + ]: 127070 : if (form == DW_FORM_exprloc
8356 [ + - ]: 140 : || (form != DW_FORM_data16
8357 [ + - ]: 140 : && attrp->cu->version < 4)) /* blocks were expressions. */
8358 : : {
8359 : 223420 : fputc ('\n', out);
8360 : 223420 : print_ops (cbargs->dwflmod, cbargs->dbg,
8361 : 223420 : 12 + level * 2, 12 + level * 2,
8362 : : cbargs->version, cbargs->addrsize, cbargs->offset_size,
8363 : 223420 : attrp->cu, block.length, block.data, out);
8364 : : }
8365 : : else
8366 : 0 : print_block (block.length, block.data, out);
8367 : : break;
8368 : :
8369 : 8 : case DW_AT_discr_list:
8370 [ - + ]: 8 : if (block.length == 0)
8371 : 0 : fputs ("<default>\n", out);
8372 [ + - ]: 8 : else if (form != DW_FORM_data16)
8373 : : {
8374 : 8 : const unsigned char *readp = block.data;
8375 : 8 : const unsigned char *readendp = readp + block.length;
8376 : :
8377 : : /* See if we are dealing with a signed or unsigned
8378 : : values. If the parent of this variant DIE is a
8379 : : variant_part then it will either have a discriminant
8380 : : which points to the member which type is the
8381 : : discriminant type. Or the variant_part itself has a
8382 : : type representing the discriminant. */
8383 : 8 : bool is_signed = false;
8384 [ + - ]: 8 : if (level > 0)
8385 : : {
8386 : 8 : Dwarf_Die *parent = &cbargs->dies[level - 1];
8387 [ + - ]: 8 : if (dwarf_tag (die) == DW_TAG_variant
8388 [ + - ]: 8 : && dwarf_tag (parent) == DW_TAG_variant_part)
8389 : : {
8390 : 8 : Dwarf_Die member;
8391 : 8 : Dwarf_Attribute discr_attr;
8392 : 8 : int bytes;
8393 [ + - ]: 8 : if (dwarf_formref_die (dwarf_attr (parent,
8394 : : DW_AT_discr,
8395 : : &discr_attr),
8396 : : &member) != NULL)
8397 : 8 : die_type_sign_bytes (&member, &is_signed, &bytes);
8398 : : else
8399 : 0 : die_type_sign_bytes (parent, &is_signed, &bytes);
8400 : : }
8401 : : }
8402 [ + + ]: 24 : while (readp < readendp)
8403 : : {
8404 : 16 : int d = (int) *readp++;
8405 : 16 : fprintf (out, "%s ", dwarf_discr_list_name (d));
8406 [ - + ]: 16 : if (readp >= readendp)
8407 : 0 : goto attrval_out;
8408 : :
8409 : 16 : Dwarf_Word val;
8410 : 16 : Dwarf_Sword sval;
8411 [ + + ]: 16 : if (d == DW_DSC_label)
8412 : : {
8413 [ + + ]: 8 : if (is_signed)
8414 : : {
8415 : 4 : get_sleb128 (sval, readp, readendp);
8416 : 4 : fprintf (out, "%" PRId64 "", sval);
8417 : : }
8418 : : else
8419 : : {
8420 : 4 : get_uleb128 (val, readp, readendp);
8421 : 4 : fprintf (out, "%" PRIu64 "", val);
8422 : : }
8423 : : }
8424 [ + - ]: 8 : else if (d == DW_DSC_range)
8425 : : {
8426 [ + + ]: 8 : if (is_signed)
8427 : : {
8428 : 6 : get_sleb128 (sval, readp, readendp);
8429 : 6 : fprintf (out, "%" PRId64 "..", sval);
8430 [ - + ]: 6 : if (readp >= readendp)
8431 : 0 : goto attrval_out;
8432 : 6 : get_sleb128 (sval, readp, readendp);
8433 : 6 : fprintf (out, "%" PRId64 "", sval);
8434 : : }
8435 : : else
8436 : : {
8437 : 2 : get_uleb128 (val, readp, readendp);
8438 : 2 : fprintf (out, "%" PRIu64 "..", val);
8439 [ - + ]: 2 : if (readp >= readendp)
8440 : 0 : goto attrval_out;
8441 : 2 : get_uleb128 (val, readp, readendp);
8442 : 2 : fprintf (out, "%" PRIu64 "", val);
8443 : : }
8444 : : }
8445 : : else
8446 : : {
8447 : 0 : print_block (readendp - readp, readp, out);
8448 : 0 : break;
8449 : : }
8450 [ + + ]: 16 : if (readp < readendp)
8451 : 8 : fprintf (out, ", ");
8452 : : }
8453 : 8 : fputc ('\n', out);
8454 : : }
8455 : : else
8456 : 0 : print_block (block.length, block.data, out);
8457 : : break;
8458 : : }
8459 : : break;
8460 : :
8461 : 0 : default:
8462 [ # # ]: 0 : if (cbargs->silent)
8463 : : break;
8464 : 0 : fprintf (out, " %*s%-20s (%s) ???\n",
8465 : : (int) (level * 2), "", dwarf_attr_name (attr),
8466 : : dwarf_form_name (form));
8467 : 0 : break;
8468 : : }
8469 : :
8470 : 7299362 : return DWARF_CB_OK;
8471 : : }
8472 : :
8473 : : static void
8474 : 208 : print_debug_units (Dwfl_Module *dwflmod,
8475 : : Ebl *ebl, GElf_Ehdr *ehdr __attribute__ ((unused)),
8476 : : Elf_Scn *scn, GElf_Shdr *shdr,
8477 : : Dwarf *dbg, bool debug_types, FILE *out)
8478 : : {
8479 [ + + + + ]: 208 : const bool silent = !(print_debug_sections & section_info) && !debug_types;
8480 : 208 : const char *secname = section_name (ebl, shdr);
8481 : :
8482 : : /* Check section actually exists. */
8483 [ + + ]: 208 : if (!silent)
8484 [ + - ]: 136 : if (get_debug_elf_data (dbg, ebl,
8485 : : debug_types ? IDX_debug_types : IDX_debug_info,
8486 : : scn) == NULL)
8487 : 0 : return;
8488 : :
8489 : 136 : if (!silent)
8490 : 136 : fprintf (out, _("\
8491 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n [Offset]\n"),
8492 : 136 : elf_ndxscn (scn), secname, (uint64_t) shdr->sh_offset);
8493 : :
8494 : : /* If the section is empty we don't have to do anything. */
8495 [ + + + - ]: 208 : if (!silent && shdr->sh_size == 0)
8496 : : return;
8497 : :
8498 : 208 : int maxdies = 20;
8499 : 208 : Dwarf_Die *dies = xmalloc (maxdies * sizeof (Dwarf_Die));
8500 : :
8501 : : /* New compilation unit. */
8502 : 208 : Dwarf_Half version;
8503 : :
8504 : 208 : Dwarf_Die result;
8505 : 208 : Dwarf_Off abbroffset;
8506 : 208 : uint8_t addrsize;
8507 : 208 : uint8_t offsize;
8508 : 208 : uint64_t unit_id;
8509 : 208 : Dwarf_Off subdie_off;
8510 : :
8511 : 208 : int unit_res;
8512 : 208 : Dwarf_CU *cu;
8513 : 208 : Dwarf_CU cu_mem;
8514 : 208 : uint8_t unit_type;
8515 : 208 : Dwarf_Die cudie;
8516 : :
8517 : : /* We cheat a little because we want to see only the CUs from .debug_info
8518 : : or .debug_types. We know the Dwarf_CU struct layout. Set it up at
8519 : : the end of .debug_info if we want .debug_types only. Check the returned
8520 : : Dwarf_CU is still in the expected section. */
8521 [ + + ]: 208 : if (debug_types)
8522 : : {
8523 : 2 : cu_mem.dbg = dbg;
8524 : 2 : cu_mem.end = dbg->sectiondata[IDX_debug_info]->d_size;
8525 : 2 : cu_mem.sec_idx = IDX_debug_info;
8526 : 2 : cu = &cu_mem;
8527 : : }
8528 : : else
8529 : 206 : cu = NULL;
8530 : :
8531 : : next_cu:
8532 : 3962 : unit_res = dwarf_get_units (dbg, cu, &cu, &version, &unit_type,
8533 : : &cudie, NULL);
8534 [ + + ]: 3962 : if (unit_res == 1)
8535 : 206 : goto do_return;
8536 : :
8537 [ - + ]: 3756 : if (unit_res == -1)
8538 : : {
8539 [ # # ]: 0 : if (!silent)
8540 : 0 : error (0, 0, _("cannot get next unit: %s"), dwarf_errmsg (-1));
8541 : 0 : goto do_return;
8542 : : }
8543 : :
8544 [ + + + + ]: 7508 : if (cu->sec_idx != (size_t) (debug_types ? IDX_debug_types : IDX_debug_info))
8545 : 2 : goto do_return;
8546 : :
8547 : 3754 : dwarf_cu_die (cu, &result, NULL, &abbroffset, &addrsize, &offsize,
8548 : : &unit_id, &subdie_off);
8549 : :
8550 [ + + ]: 3754 : if (!silent)
8551 : : {
8552 : 3654 : Dwarf_Off offset = cu->start;
8553 [ + + + - ]: 3654 : if (debug_types && version < 5)
8554 : 4 : {
8555 : 4 : Dwarf_Die typedie;
8556 : 4 : Dwarf_Off dieoffset;
8557 : 4 : dieoffset = dwarf_dieoffset (dwarf_offdie_types (dbg, cu->start
8558 : : + subdie_off,
8559 : : &typedie));
8560 : 4 : fprintf (out, _(" Type unit at offset %" PRIu64 ":\n"
8561 : : " Version: %" PRIu16
8562 : : ", Abbreviation section offset: %" PRIu64
8563 : : ", Address size: %" PRIu8
8564 : : ", Offset size: %" PRIu8
8565 : : "\n Type signature: %#" PRIx64
8566 : : ", Type offset: %#" PRIx64 " [%" PRIx64 "]\n"),
8567 : : (uint64_t) offset, version, abbroffset, addrsize, offsize,
8568 : : unit_id, (uint64_t) subdie_off, dieoffset);
8569 : : }
8570 : : else
8571 : : {
8572 : 3650 : fprintf (out, _(" Compilation unit at offset %" PRIu64 ":\n"
8573 : : " Version: %" PRIu16
8574 : : ", Abbreviation section offset: %" PRIu64
8575 : : ", Address size: %" PRIu8
8576 : : ", Offset size: %" PRIu8 "\n"),
8577 : : (uint64_t) offset, version, abbroffset, addrsize, offsize);
8578 : :
8579 [ + + ]: 3650 : if (version >= 5 || (unit_type != DW_UT_compile
8580 [ + + ]: 170 : && unit_type != DW_UT_partial))
8581 : : {
8582 : 3482 : fprintf (out, _(" Unit type: %s (%" PRIu8 ")"),
8583 : : dwarf_unit_name (unit_type), unit_type);
8584 : 3482 : if (unit_type == DW_UT_type
8585 [ + + ]: 3482 : || unit_type == DW_UT_skeleton
8586 [ + - ]: 3472 : || unit_type == DW_UT_split_compile
8587 [ - + ]: 3472 : || unit_type == DW_UT_split_type)
8588 : 10 : fprintf (out, ", Unit id: 0x%.16" PRIx64 "", unit_id);
8589 : 3482 : if (unit_type == DW_UT_type
8590 [ - + ]: 3482 : || unit_type == DW_UT_split_type)
8591 : : {
8592 : 0 : Dwarf_Die typedie;
8593 : 0 : Dwarf_Off dieoffset;
8594 : 0 : dwarf_cu_info (cu, NULL, NULL, NULL, &typedie,
8595 : : NULL, NULL, NULL);
8596 : 0 : dieoffset = dwarf_dieoffset (&typedie);
8597 : 0 : fprintf (out, ", Unit DIE off: %#" PRIx64 " [%" PRIx64 "]",
8598 : : subdie_off, dieoffset);
8599 : : }
8600 : 3482 : fprintf (out, "\n");
8601 : : }
8602 : : }
8603 : : }
8604 : :
8605 [ + - ]: 3754 : if (version < 2 || version > 5
8606 [ + - - + ]: 3754 : || unit_type < DW_UT_compile || unit_type > DW_UT_split_type)
8607 : : {
8608 [ # # ]: 0 : if (!silent)
8609 : 0 : error (0, 0, _("unknown version (%d) or unit type (%d)"),
8610 : : version, unit_type);
8611 : 0 : goto next_cu;
8612 : : }
8613 : :
8614 : 3754 : struct attrcb_args args =
8615 : : {
8616 : : .dwflmod = dwflmod,
8617 : : .silent = silent,
8618 : : .version = version,
8619 : : .addrsize = addrsize,
8620 : : .offset_size = offsize
8621 : : };
8622 : :
8623 : 3754 : bool is_split = false;
8624 : 3754 : int level = 0;
8625 : 3754 : dies[0] = cudie;
8626 : 3754 : args.cu = dies[0].cu;
8627 : 3754 : args.dbg = dbg;
8628 : 3754 : args.is_split = is_split;
8629 : 3754 : args.out = out;
8630 : :
8631 : : /* We might return here again for the split CU subdie. */
8632 : : do_cu:
8633 : 2128028 : do
8634 : : {
8635 : 2128028 : Dwarf_Off offset = dwarf_dieoffset (&dies[level]);
8636 [ - + ]: 2128028 : if (unlikely (offset == (Dwarf_Off) -1))
8637 : : {
8638 [ # # ]: 0 : if (!silent)
8639 : 0 : error (0, 0, _("cannot get DIE offset: %s"),
8640 : : dwarf_errmsg (-1));
8641 : 0 : goto do_return;
8642 : : }
8643 : :
8644 : 2128028 : int tag = dwarf_tag (&dies[level]);
8645 [ - + ]: 2128028 : if (unlikely (tag == DW_TAG_invalid))
8646 : : {
8647 [ # # ]: 0 : if (!silent)
8648 : 0 : error (0, 0, _("cannot get tag of DIE at offset [%" PRIx64
8649 : : "] in section '%s': %s"),
8650 : : (uint64_t) offset, secname, dwarf_errmsg (-1));
8651 : 0 : goto do_return;
8652 : : }
8653 : :
8654 [ + + ]: 2128028 : if (!silent)
8655 : : {
8656 : 2126042 : unsigned int code = dwarf_getabbrevcode (dies[level].abbrev);
8657 [ + + ]: 2126042 : if (is_split)
8658 : 96 : fprintf (out, " {%6" PRIx64 "} ", (uint64_t) offset);
8659 : : else
8660 : 2125946 : fprintf (out, " [%6" PRIx64 "] ", (uint64_t) offset);
8661 : 2126042 : fprintf (out, "%*s%-20s abbrev: %u\n", (int) (level * 2), "",
8662 : : dwarf_tag_name (tag), code);
8663 : : }
8664 : :
8665 : : /* Print the attribute values. */
8666 : 2128028 : args.level = level;
8667 : 2128028 : args.dies = dies;
8668 : 2128028 : (void) dwarf_getattrs (&dies[level], attr_callback, &args, 0);
8669 : :
8670 : : /* Make room for the next level's DIE. */
8671 [ - + ]: 2128028 : if (level + 1 == maxdies)
8672 : 0 : dies = xrealloc (dies, (maxdies += 10) * sizeof (Dwarf_Die));
8673 : :
8674 : 2128028 : int res = dwarf_child (&dies[level], &dies[level + 1]);
8675 [ + + ]: 2128028 : if (res > 0)
8676 : : {
8677 [ + + ]: 2128028 : while ((res = dwarf_siblingof (&dies[level], &dies[level])) == 1)
8678 [ + + ]: 295360 : if (level-- == 0)
8679 : : break;
8680 : :
8681 [ - + ]: 1836430 : if (unlikely (res == -1))
8682 : : {
8683 [ # # ]: 0 : if (!silent)
8684 : 0 : error (0, 0, _("cannot get next DIE: %s\n"),
8685 : : dwarf_errmsg (-1));
8686 : 0 : goto do_return;
8687 : : }
8688 : : }
8689 [ - + ]: 291598 : else if (unlikely (res < 0))
8690 : : {
8691 [ # # ]: 0 : if (!silent)
8692 : 0 : error (0, 0, _("cannot get next DIE: %s"),
8693 : : dwarf_errmsg (-1));
8694 : 0 : goto do_return;
8695 : : }
8696 : : else
8697 : : ++level;
8698 : : }
8699 [ + + ]: 2128028 : while (level >= 0);
8700 : :
8701 : : /* We might want to show the split compile unit if this was a skeleton.
8702 : : We need to scan it if we are requesting printing .debug_ranges for
8703 : : DWARF4 since GNU DebugFission uses "offsets" into the main ranges
8704 : : section. */
8705 [ + + ]: 3762 : if (unit_type == DW_UT_skeleton
8706 [ + + + + ]: 26 : && ((!silent && show_split_units)
8707 [ + + + + ]: 20 : || (version < 5 && (print_debug_sections & section_ranges) != 0)))
8708 : : {
8709 : 10 : Dwarf_Die subdie;
8710 [ + - ]: 10 : if (dwarf_cu_info (cu, NULL, NULL, NULL, &subdie, NULL, NULL, NULL) != 0
8711 [ + + ]: 10 : || dwarf_tag (&subdie) == DW_TAG_invalid)
8712 : : {
8713 [ + - ]: 2 : if (!silent)
8714 : : {
8715 : 2 : Dwarf_Attribute dwo_at;
8716 : 4 : const char *dwo_name =
8717 : 2 : (dwarf_formstring (dwarf_attr (&cudie, DW_AT_dwo_name,
8718 : : &dwo_at))
8719 [ - + ]: 2 : ?: (dwarf_formstring (dwarf_attr (&cudie, DW_AT_GNU_dwo_name,
8720 : : &dwo_at))
8721 [ # # ]: 0 : ?: "<unknown>"));
8722 : 2 : fprintf (stderr,
8723 : : "Could not find split unit '%s', id: %" PRIx64 "\n",
8724 : : dwo_name, unit_id);
8725 : : }
8726 : : }
8727 : : else
8728 : : {
8729 : 8 : Dwarf_CU *split_cu = subdie.cu;
8730 : 8 : dwarf_cu_die (split_cu, &result, NULL, &abbroffset,
8731 : : &addrsize, &offsize, &unit_id, &subdie_off);
8732 : 8 : Dwarf_Off offset = cu->start;
8733 : :
8734 [ + + ]: 8 : if (!silent)
8735 : : {
8736 : 4 : fprintf (out, _(" Split compilation unit at offset %"
8737 : : PRIu64 ":\n"
8738 : : " Version: %" PRIu16
8739 : : ", Abbreviation section offset: %" PRIu64
8740 : : ", Address size: %" PRIu8
8741 : : ", Offset size: %" PRIu8 "\n"),
8742 : : (uint64_t) offset, version, abbroffset,
8743 : : addrsize, offsize);
8744 : 4 : fprintf (out, _(" Unit type: %s (%" PRIu8 ")"),
8745 : : dwarf_unit_name (unit_type), unit_type);
8746 : 4 : fprintf (out, ", Unit id: 0x%.16" PRIx64 "", unit_id);
8747 : 4 : fprintf (out, "\n");
8748 : : }
8749 : :
8750 : 8 : unit_type = DW_UT_split_compile;
8751 : 8 : is_split = true;
8752 : 8 : level = 0;
8753 : 8 : dies[0] = subdie;
8754 : 8 : args.cu = dies[0].cu;
8755 : 8 : args.dbg = split_cu->dbg;
8756 : 8 : args.is_split = is_split;
8757 : 8 : goto do_cu;
8758 : : }
8759 : : }
8760 : :
8761 : : /* And again... */
8762 : 3754 : goto next_cu;
8763 : :
8764 : 208 : do_return:
8765 : 208 : free (dies);
8766 : : }
8767 : :
8768 : : static void
8769 : 206 : print_debug_info_section (Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr,
8770 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg,
8771 : : FILE *out)
8772 : : {
8773 : 42 : print_debug_units (dwflmod, ebl, ehdr, scn, shdr, dbg, false, out);
8774 : 42 : }
8775 : :
8776 : : static void
8777 : 2 : print_debug_types_section (Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr,
8778 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg,
8779 : : FILE *out)
8780 : : {
8781 : 2 : print_debug_units (dwflmod, ebl, ehdr, scn, shdr, dbg, true, out);
8782 : 2 : }
8783 : :
8784 : :
8785 : : static void
8786 : 82 : print_decoded_line_section (Dwfl_Module *dwflmod, Ebl *ebl,
8787 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
8788 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg,
8789 : : FILE *out)
8790 : : {
8791 : 82 : fprintf (out, _("\
8792 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n\n"),
8793 : : elf_ndxscn (scn), section_name (ebl, shdr),
8794 : 82 : (uint64_t) shdr->sh_offset);
8795 : :
8796 : 164 : size_t address_size
8797 [ + + ]: 82 : = elf_getident (ebl->elf, NULL)[EI_CLASS] == ELFCLASS32 ? 4 : 8;
8798 : :
8799 : 82 : Dwarf_Lines *lines;
8800 : 82 : size_t nlines;
8801 : 82 : Dwarf_Off off, next_off = 0;
8802 : 82 : Dwarf_CU *cu = NULL;
8803 : 82 : while (dwarf_next_lines (dbg, off = next_off, &next_off, &cu, NULL, NULL,
8804 [ + + ]: 176 : &lines, &nlines) == 0)
8805 : : {
8806 : 94 : Dwarf_Die cudie;
8807 [ + + + - ]: 94 : if (cu != NULL && dwarf_cu_info (cu, NULL, NULL, &cudie,
8808 : : NULL, NULL, NULL, NULL) == 0)
8809 : 50 : fprintf (out, " CU [%" PRIx64 "] %s\n",
8810 : : dwarf_dieoffset (&cudie), dwarf_diename (&cudie));
8811 : : else
8812 : : {
8813 : : /* DWARF5 lines can be independent of any CU, but they probably
8814 : : are used by some CU. Determine the CU this block is for. */
8815 : 44 : Dwarf_Off cuoffset;
8816 : 44 : Dwarf_Off ncuoffset = 0;
8817 : 44 : size_t hsize;
8818 : 44 : while (dwarf_nextcu (dbg, cuoffset = ncuoffset, &ncuoffset, &hsize,
8819 [ + - ]: 46 : NULL, NULL, NULL) == 0)
8820 : : {
8821 [ - + ]: 46 : if (dwarf_offdie (dbg, cuoffset + hsize, &cudie) == NULL)
8822 : 0 : continue;
8823 : 46 : Dwarf_Attribute stmt_list;
8824 [ - + ]: 46 : if (dwarf_attr (&cudie, DW_AT_stmt_list, &stmt_list) == NULL)
8825 : 0 : continue;
8826 : 46 : Dwarf_Word lineoff;
8827 [ - + ]: 46 : if (dwarf_formudata (&stmt_list, &lineoff) != 0)
8828 : 0 : continue;
8829 [ + + ]: 46 : if (lineoff == off)
8830 : : {
8831 : : /* Found the CU. */
8832 : 44 : cu = cudie.cu;
8833 : 44 : break;
8834 : : }
8835 : : }
8836 : :
8837 [ + - ]: 44 : if (cu != NULL)
8838 : 44 : fprintf (out, " CU [%" PRIx64 "] %s\n",
8839 : : dwarf_dieoffset (&cudie), dwarf_diename (&cudie));
8840 : : else
8841 : 0 : fprintf (out, " No CU\n");
8842 : : }
8843 : :
8844 : 94 : fprintf (out, " line:col SBPE* disc isa op address"
8845 : : " (Statement Block Prologue Epilogue *End)\n");
8846 : 94 : const char *last_file = "";
8847 [ + + ]: 720 : for (size_t n = 0; n < nlines; n++)
8848 : : {
8849 : 626 : Dwarf_Line *line = dwarf_onesrcline (lines, n);
8850 [ - + ]: 626 : if (line == NULL)
8851 : : {
8852 : 0 : fprintf (out, " dwarf_onesrcline: %s\n", dwarf_errmsg (-1));
8853 : 0 : continue;
8854 : : }
8855 : 626 : Dwarf_Word mtime, length;
8856 : 626 : const char *file = dwarf_linesrc (line, &mtime, &length);
8857 [ - + ]: 626 : if (file == NULL)
8858 : : {
8859 : 0 : fprintf (out, " <%s> (mtime: ?, length: ?)\n",
8860 : : dwarf_errmsg (-1));
8861 : 0 : last_file = "";
8862 : : }
8863 [ + + ]: 626 : else if (strcmp (last_file, file) != 0)
8864 : : {
8865 : 114 : fprintf (out, " %s (mtime: %" PRIu64 ", length: %" PRIu64 ")\n",
8866 : : file, mtime, length);
8867 : 114 : last_file = file;
8868 : : }
8869 : :
8870 : 626 : int lineno, colno;
8871 : 626 : bool statement, endseq, block, prologue_end, epilogue_begin;
8872 : 626 : unsigned int lineop, isa, disc;
8873 : 626 : Dwarf_Addr address;
8874 : 626 : dwarf_lineaddr (line, &address);
8875 : 626 : dwarf_lineno (line, &lineno);
8876 : 626 : dwarf_linecol (line, &colno);
8877 : 626 : dwarf_lineop_index (line, &lineop);
8878 : 626 : dwarf_linebeginstatement (line, &statement);
8879 : 626 : dwarf_lineendsequence (line, &endseq);
8880 : 626 : dwarf_lineblock (line, &block);
8881 : 626 : dwarf_lineprologueend (line, &prologue_end);
8882 : 626 : dwarf_lineepiloguebegin (line, &epilogue_begin);
8883 : 626 : dwarf_lineisa (line, &isa);
8884 : 626 : dwarf_linediscriminator (line, &disc);
8885 : :
8886 : : /* End sequence is special, it is one byte past. */
8887 : 626 : fprintf (out, " %4d:%-3d %c%c%c%c%c %4d %3d %2d ",
8888 : : lineno, colno,
8889 [ + + ]: 626 : (statement ? 'S' : ' '),
8890 [ + - ]: 626 : (block ? 'B' : ' '),
8891 [ + + ]: 626 : (prologue_end ? 'P' : ' '),
8892 [ + - ]: 626 : (epilogue_begin ? 'E' : ' '),
8893 [ + + ]: 626 : (endseq ? '*' : ' '),
8894 : : disc, isa, lineop);
8895 : 626 : print_dwarf_addr (dwflmod, address_size,
8896 [ + + ]: 626 : address - (endseq ? 1 : 0), address, out);
8897 : 626 : fprintf (out, "\n");
8898 : :
8899 [ + + ]: 626 : if (endseq)
8900 : 98 : fprintf(out, "\n");
8901 : : }
8902 : : }
8903 : 82 : }
8904 : :
8905 : :
8906 : : /* Print the value of a form.
8907 : : Returns new value of readp, or readendp on failure. */
8908 : : static const unsigned char *
8909 : 143610 : print_form_data (Dwarf *dbg, int form, const unsigned char *readp,
8910 : : const unsigned char *readendp, unsigned int offset_len,
8911 : : Dwarf_Off str_offsets_base, FILE *out)
8912 : : {
8913 : 143610 : Dwarf_Word val;
8914 : 143610 : unsigned char *endp;
8915 : 143610 : Elf_Data *data;
8916 : 143610 : char *str;
8917 [ + - - - : 143610 : switch (form)
- + - - -
- + - - +
- + - - -
- - ]
8918 : : {
8919 : 16 : case DW_FORM_data1:
8920 [ - + ]: 16 : if (readendp - readp < 1)
8921 : : {
8922 : 0 : invalid_data:
8923 : 0 : error (0, 0, "invalid data");
8924 : 0 : return readendp;
8925 : : }
8926 : 16 : val = *readp++;
8927 : 16 : fprintf (out, " %" PRIx8, (unsigned int) val);
8928 : 16 : break;
8929 : :
8930 : 0 : case DW_FORM_data2:
8931 [ # # ]: 0 : if (readendp - readp < 2)
8932 : 0 : goto invalid_data;
8933 [ # # ]: 0 : val = read_2ubyte_unaligned_inc (dbg, readp);
8934 : 0 : fprintf(out, " %" PRIx16, (unsigned int) val);
8935 : 0 : break;
8936 : :
8937 : 0 : case DW_FORM_data4:
8938 [ # # ]: 0 : if (readendp - readp < 4)
8939 : 0 : goto invalid_data;
8940 [ # # ]: 0 : val = read_4ubyte_unaligned_inc (dbg, readp);
8941 : 0 : fprintf (out, " %" PRIx32, (unsigned int) val);
8942 : 0 : break;
8943 : :
8944 : 0 : case DW_FORM_data8:
8945 [ # # ]: 0 : if (readendp - readp < 8)
8946 : 0 : goto invalid_data;
8947 [ # # ]: 0 : val = read_8ubyte_unaligned_inc (dbg, readp);
8948 : 0 : fprintf (out, " %" PRIx64, val);
8949 : 0 : break;
8950 : :
8951 : 0 : case DW_FORM_sdata:
8952 [ # # ]: 0 : if (readendp - readp < 1)
8953 : 0 : goto invalid_data;
8954 : 0 : get_sleb128 (val, readp, readendp);
8955 : 0 : fprintf (out, " %" PRIx64, val);
8956 : 0 : break;
8957 : :
8958 : 57842 : case DW_FORM_udata:
8959 [ - + ]: 57842 : if (readendp - readp < 1)
8960 : 0 : goto invalid_data;
8961 : 57842 : get_uleb128 (val, readp, readendp);
8962 : 57842 : fprintf (out, " %" PRIx64, val);
8963 : 57842 : break;
8964 : :
8965 : 0 : case DW_FORM_block:
8966 [ # # ]: 0 : if (readendp - readp < 1)
8967 : 0 : goto invalid_data;
8968 : 0 : get_uleb128 (val, readp, readendp);
8969 [ # # ]: 0 : if ((size_t) (readendp - readp) < val)
8970 : 0 : goto invalid_data;
8971 : 0 : print_bytes (val, readp, out);
8972 : 0 : readp += val;
8973 : 0 : break;
8974 : :
8975 : 0 : case DW_FORM_block1:
8976 [ # # ]: 0 : if (readendp - readp < 1)
8977 : 0 : goto invalid_data;
8978 : 0 : val = *readp++;
8979 [ # # ]: 0 : if ((size_t) (readendp - readp) < val)
8980 : 0 : goto invalid_data;
8981 : 0 : print_bytes (val, readp, out);
8982 : 0 : readp += val;
8983 : 0 : break;
8984 : :
8985 : 0 : case DW_FORM_block2:
8986 [ # # ]: 0 : if (readendp - readp < 2)
8987 : 0 : goto invalid_data;
8988 [ # # ]: 0 : val = read_2ubyte_unaligned_inc (dbg, readp);
8989 [ # # ]: 0 : if ((size_t) (readendp - readp) < val)
8990 : 0 : goto invalid_data;
8991 : 0 : print_bytes (val, readp, out);
8992 : 0 : readp += val;
8993 : 0 : break;
8994 : :
8995 : 0 : case DW_FORM_block4:
8996 [ # # ]: 0 : if (readendp - readp < 4)
8997 : 0 : goto invalid_data;
8998 [ # # ]: 0 : val = read_4ubyte_unaligned_inc (dbg, readp);
8999 [ # # ]: 0 : if ((size_t) (readendp - readp) < val)
9000 : 0 : goto invalid_data;
9001 : 0 : print_bytes (val, readp, out);
9002 : 0 : readp += val;
9003 : 0 : break;
9004 : :
9005 : 24 : case DW_FORM_data16:
9006 [ - + ]: 24 : if (readendp - readp < 16)
9007 : 0 : goto invalid_data;
9008 : 24 : print_bytes (16, readp, out);
9009 : 24 : readp += 16;
9010 : 24 : break;
9011 : :
9012 : 0 : case DW_FORM_flag:
9013 [ # # ]: 0 : if (readendp - readp < 1)
9014 : 0 : goto invalid_data;
9015 : 0 : val = *readp++;
9016 [ # # ]: 0 : fprintf (out, "%s", val != 0 ? yes_str : no_str);
9017 : 0 : break;
9018 : :
9019 : 0 : case DW_FORM_string:
9020 : 0 : endp = memchr (readp, '\0', readendp - readp);
9021 [ # # ]: 0 : if (endp == NULL)
9022 : 0 : goto invalid_data;
9023 : 0 : fprintf (out, "%s", readp);
9024 : 0 : readp = endp + 1;
9025 : 0 : break;
9026 : :
9027 : 84172 : case DW_FORM_strp:
9028 : : case DW_FORM_line_strp:
9029 : : case DW_FORM_strp_sup:
9030 [ - + ]: 84172 : if ((size_t) (readendp - readp) < offset_len)
9031 : 0 : goto invalid_data;
9032 [ - + ]: 84172 : if (offset_len == 8)
9033 [ # # ]: 0 : val = read_8ubyte_unaligned_inc (dbg, readp);
9034 : : else
9035 [ - + ]: 84172 : val = read_4ubyte_unaligned_inc (dbg, readp);
9036 [ - + ]: 84172 : if (form == DW_FORM_strp)
9037 : 0 : data = dbg->sectiondata[IDX_debug_str];
9038 [ + - ]: 84172 : else if (form == DW_FORM_line_strp)
9039 : 84172 : data = dbg->sectiondata[IDX_debug_line_str];
9040 : : else /* form == DW_FORM_strp_sup */
9041 : : {
9042 : 0 : Dwarf *alt = dwarf_getalt (dbg);
9043 [ # # ]: 0 : data = alt != NULL ? alt->sectiondata[IDX_debug_str] : NULL;
9044 : : }
9045 [ + - - + ]: 84172 : if (data == NULL || val >= data->d_size
9046 [ - + ]: 84172 : || memchr (data->d_buf + val, '\0', data->d_size - val) == NULL)
9047 : : str = "???";
9048 : : else
9049 : 84172 : str = (char *) data->d_buf + val;
9050 : 84172 : fprintf (out, "%s (%" PRIu64 ")", str, val);
9051 : 84172 : break;
9052 : :
9053 : 0 : case DW_FORM_sec_offset:
9054 [ # # ]: 0 : if ((size_t) (readendp - readp) < offset_len)
9055 : 0 : goto invalid_data;
9056 [ # # ]: 0 : if (offset_len == 8)
9057 [ # # ]: 0 : val = read_8ubyte_unaligned_inc (dbg, readp);
9058 : : else
9059 [ # # ]: 0 : val = read_4ubyte_unaligned_inc (dbg, readp);
9060 : 0 : fprintf (out, "[%" PRIx64 "]", val);
9061 : 0 : break;
9062 : :
9063 : 1556 : case DW_FORM_strx:
9064 : : case DW_FORM_GNU_str_index:
9065 [ - + ]: 1556 : if (readendp - readp < 1)
9066 : 0 : goto invalid_data;
9067 : 1556 : get_uleb128 (val, readp, readendp);
9068 : 1556 : strx_val:
9069 : 1556 : data = dbg->sectiondata[IDX_debug_str_offsets];
9070 [ + - ]: 1556 : if (data == NULL
9071 [ - + ]: 1556 : || data->d_size - str_offsets_base < val * offset_len)
9072 : : str = "???";
9073 : : else
9074 : : {
9075 : 1556 : const unsigned char *strreadp = (data->d_buf + str_offsets_base
9076 : 1556 : + val * offset_len);
9077 : 1556 : const unsigned char *strreadendp = data->d_buf + data->d_size;
9078 [ - + ]: 1556 : if ((size_t) (strreadendp - strreadp) < offset_len)
9079 : : str = "???";
9080 : : else
9081 : : {
9082 : 1556 : Dwarf_Off idx;
9083 [ - + ]: 1556 : if (offset_len == 8)
9084 [ # # ]: 0 : idx = read_8ubyte_unaligned (dbg, strreadp);
9085 : : else
9086 [ - + ]: 1556 : idx = read_4ubyte_unaligned (dbg, strreadp);
9087 : :
9088 : 1556 : data = dbg->sectiondata[IDX_debug_str];
9089 [ - + - + ]: 1556 : if (data == NULL || idx >= data->d_size
9090 [ - + ]: 1556 : || memchr (data->d_buf + idx, '\0',
9091 : : data->d_size - idx) == NULL)
9092 : : str = "???";
9093 : : else
9094 : 1556 : str = (char *) data->d_buf + idx;
9095 : : }
9096 : : }
9097 : 1556 : fprintf (out, "%s (%" PRIu64 ")", str, val);
9098 : 1556 : break;
9099 : :
9100 : 0 : case DW_FORM_strx1:
9101 [ # # ]: 0 : if (readendp - readp < 1)
9102 : 0 : goto invalid_data;
9103 : 0 : val = *readp++;
9104 : 0 : goto strx_val;
9105 : :
9106 : 0 : case DW_FORM_strx2:
9107 [ # # ]: 0 : if (readendp - readp < 2)
9108 : 0 : goto invalid_data;
9109 [ # # ]: 0 : val = read_2ubyte_unaligned_inc (dbg, readp);
9110 : 0 : goto strx_val;
9111 : :
9112 : 0 : case DW_FORM_strx3:
9113 [ # # ]: 0 : if (readendp - readp < 3)
9114 : 0 : goto invalid_data;
9115 : 0 : val = read_3ubyte_unaligned_inc (dbg, readp);
9116 : 0 : goto strx_val;
9117 : :
9118 : 0 : case DW_FORM_strx4:
9119 [ # # ]: 0 : if (readendp - readp < 4)
9120 : 0 : goto invalid_data;
9121 [ # # ]: 0 : val = read_4ubyte_unaligned_inc (dbg, readp);
9122 : 0 : goto strx_val;
9123 : :
9124 : 0 : default:
9125 : 0 : error (0, 0, _("unknown form: %s"), dwarf_form_name (form));
9126 : 0 : return readendp;
9127 : : }
9128 : :
9129 : 143610 : return readp;
9130 : : }
9131 : :
9132 : : /* Only used via run_advance_pc() macro */
9133 : : static inline void
9134 : 783966 : run_advance_pc (unsigned int op_advance,
9135 : : unsigned int minimum_instr_len,
9136 : : unsigned int max_ops_per_instr,
9137 : : unsigned int *op_addr_advance,
9138 : : Dwarf_Word *address,
9139 : : unsigned int *op_index)
9140 : : {
9141 : 783966 : const unsigned int advanced_op_index = (*op_index) + op_advance;
9142 : :
9143 : 783966 : *op_addr_advance = minimum_instr_len * (advanced_op_index
9144 : 783966 : / max_ops_per_instr);
9145 : 783966 : *address = *address + *op_addr_advance;
9146 : 783966 : *op_index = advanced_op_index % max_ops_per_instr;
9147 : : }
9148 : :
9149 : : static void
9150 : 186 : print_debug_line_section (Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr,
9151 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg,
9152 : : FILE *out)
9153 : : {
9154 [ + + ]: 186 : if (decodedline)
9155 : : {
9156 : 82 : print_decoded_line_section (dwflmod, ebl, ehdr, scn, shdr, dbg, out);
9157 : 164 : return;
9158 : : }
9159 : :
9160 : 104 : Elf_Data *data = get_debug_elf_data (dbg, ebl, IDX_debug_line, scn);
9161 [ + - ]: 104 : if (data == NULL)
9162 : : return;
9163 : :
9164 : 104 : fprintf (out, _("\
9165 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
9166 : : elf_ndxscn (scn), section_name (ebl, shdr),
9167 : 104 : (uint64_t) shdr->sh_offset);
9168 : :
9169 [ + - ]: 104 : if (shdr->sh_size == 0)
9170 : : return;
9171 : :
9172 : : /* There is no functionality in libdw to read the information in the
9173 : : way it is represented here. Hardcode the decoder. */
9174 : :
9175 : 104 : const unsigned char *linep = (const unsigned char *) data->d_buf;
9176 : 104 : const unsigned char *lineendp;
9177 : :
9178 : 104 : while (linep
9179 [ + + ]: 3726 : < (lineendp = (const unsigned char *) data->d_buf + data->d_size))
9180 : : {
9181 : 3622 : size_t start_offset = linep - (const unsigned char *) data->d_buf;
9182 : :
9183 : 3622 : fprintf (out, _("\nTable at offset %zu:\n"), start_offset);
9184 : :
9185 [ - + ]: 3622 : if (unlikely (linep + 4 > lineendp))
9186 : 0 : goto invalid_data;
9187 [ + + ]: 3622 : Dwarf_Word unit_length = read_4ubyte_unaligned_inc (dbg, linep);
9188 : 3622 : unsigned int length = 4;
9189 [ - + ]: 3622 : if (unlikely (unit_length == 0xffffffff))
9190 : : {
9191 [ # # ]: 0 : if (unlikely (linep + 8 > lineendp))
9192 : : {
9193 : 0 : invalid_data:
9194 : 0 : error (0, 0, _("invalid data in section [%zu] '%s'"),
9195 : : elf_ndxscn (scn), section_name (ebl, shdr));
9196 : 0 : return;
9197 : : }
9198 [ # # ]: 0 : unit_length = read_8ubyte_unaligned_inc (dbg, linep);
9199 : 0 : length = 8;
9200 : : }
9201 : :
9202 : : /* Check whether we have enough room in the section. */
9203 [ - + ]: 3622 : if (unlikely (unit_length > (size_t) (lineendp - linep)))
9204 : 0 : goto invalid_data;
9205 : 3622 : lineendp = linep + unit_length;
9206 : :
9207 : : /* The next element of the header is the version identifier. */
9208 [ - + ]: 3622 : if ((size_t) (lineendp - linep) < 2)
9209 : 0 : goto invalid_data;
9210 [ + + ]: 3622 : uint_fast16_t version = read_2ubyte_unaligned_inc (dbg, linep);
9211 : :
9212 : 7244 : size_t address_size
9213 [ + + ]: 3622 : = elf_getident (ebl->elf, NULL)[EI_CLASS] == ELFCLASS32 ? 4 : 8;
9214 : 3622 : unsigned char segment_selector_size = 0;
9215 [ + + ]: 3622 : if (version > 4)
9216 : : {
9217 [ - + ]: 3470 : if ((size_t) (lineendp - linep) < 2)
9218 : 0 : goto invalid_data;
9219 : 3470 : address_size = *linep++;
9220 : 3470 : segment_selector_size = *linep++;
9221 : : }
9222 : :
9223 : : /* Next comes the header length. */
9224 : 3622 : Dwarf_Word header_length;
9225 [ + - ]: 3622 : if (length == 4)
9226 : : {
9227 [ - + ]: 3622 : if ((size_t) (lineendp - linep) < 4)
9228 : 0 : goto invalid_data;
9229 [ + + ]: 3622 : header_length = read_4ubyte_unaligned_inc (dbg, linep);
9230 : : }
9231 : : else
9232 : : {
9233 [ # # ]: 0 : if ((size_t) (lineendp - linep) < 8)
9234 : 0 : goto invalid_data;
9235 [ # # ]: 0 : header_length = read_8ubyte_unaligned_inc (dbg, linep);
9236 : : }
9237 : :
9238 : 3622 : const unsigned char *header_start = linep;
9239 : :
9240 : : /* Next the minimum instruction length. */
9241 [ - + ]: 3622 : if ((size_t) (lineendp - linep) < 1)
9242 : 0 : goto invalid_data;
9243 : 3622 : uint_fast8_t minimum_instr_len = *linep++;
9244 : :
9245 : : /* Next the maximum operations per instruction, in version 4 format. */
9246 : 3622 : uint_fast8_t max_ops_per_instr;
9247 [ + + ]: 3622 : if (version < 4)
9248 : : max_ops_per_instr = 1;
9249 : : else
9250 : : {
9251 [ - + ]: 3490 : if ((size_t) (lineendp - linep) < 1)
9252 : 0 : goto invalid_data;
9253 : 3490 : max_ops_per_instr = *linep++;
9254 : : }
9255 : :
9256 : : /* We need at least 4 more bytes. */
9257 [ - + ]: 3622 : if ((size_t) (lineendp - linep) < 4)
9258 : 0 : goto invalid_data;
9259 : :
9260 : : /* Then the flag determining the default value of the is_stmt
9261 : : register. */
9262 : 3622 : uint_fast8_t default_is_stmt = *linep++;
9263 : :
9264 : : /* Now the line base. */
9265 : 3622 : int_fast8_t line_base = *linep++;
9266 : :
9267 : : /* And the line range. */
9268 : 3622 : uint_fast8_t line_range = *linep++;
9269 : :
9270 : : /* The opcode base. */
9271 : 3622 : uint_fast8_t opcode_base = *linep++;
9272 : :
9273 : : /* Print what we got so far. */
9274 : 3622 : fprintf (out, _("\n"
9275 : : " Length: %" PRIu64 "\n"
9276 : : " DWARF version: %" PRIuFAST16 "\n"
9277 : : " Prologue length: %" PRIu64 "\n"
9278 : : " Address size: %zd\n"
9279 : : " Segment selector size: %zd\n"
9280 : : " Min instruction length: %" PRIuFAST8 "\n"
9281 : : " Max operations per instruction: %" PRIuFAST8 "\n"
9282 : : " Initial value if 'is_stmt': %" PRIuFAST8 "\n"
9283 : : " Line base: %" PRIdFAST8 "\n"
9284 : : " Line range: %" PRIuFAST8 "\n"
9285 : : " Opcode base: %" PRIuFAST8 "\n"
9286 : : "\n"
9287 : : "Opcodes:\n"),
9288 : : (uint64_t) unit_length, version, (uint64_t) header_length,
9289 : : address_size, (size_t) segment_selector_size,
9290 : : minimum_instr_len, max_ops_per_instr,
9291 : : default_is_stmt, line_base,
9292 : : line_range, opcode_base);
9293 : :
9294 [ - + ]: 3622 : if (version < 2 || version > 5)
9295 : : {
9296 : 0 : error (0, 0, _("cannot handle .debug_line version: %u\n"),
9297 : : (unsigned int) version);
9298 : 0 : linep = lineendp;
9299 : 0 : continue;
9300 : : }
9301 : :
9302 [ - + ]: 3622 : if (address_size != 4 && address_size != 8)
9303 : : {
9304 : 0 : error (0, 0, _("cannot handle address size: %u\n"),
9305 : : (unsigned int) address_size);
9306 : 0 : linep = lineendp;
9307 : 0 : continue;
9308 : : }
9309 : :
9310 [ - + ]: 3622 : if (segment_selector_size != 0)
9311 : : {
9312 : 0 : error (0, 0, _("cannot handle segment selector size: %u\n"),
9313 : : (unsigned int) segment_selector_size);
9314 : 0 : linep = lineendp;
9315 : 0 : continue;
9316 : : }
9317 : :
9318 [ - + ]: 3622 : if (unlikely (linep + opcode_base - 1 >= lineendp))
9319 : : {
9320 : 0 : invalid_unit:
9321 : 0 : error (0, 0,
9322 : 0 : _("invalid data at offset %tu in section [%zu] '%s'"),
9323 : 0 : linep - (const unsigned char *) data->d_buf,
9324 : : elf_ndxscn (scn), section_name (ebl, shdr));
9325 : 0 : linep = lineendp;
9326 : 0 : continue;
9327 : : }
9328 : 3622 : int opcode_base_l10 = 1;
9329 : 3622 : unsigned int tmp = opcode_base;
9330 [ + + ]: 7238 : while (tmp > 10)
9331 : : {
9332 : 3616 : tmp /= 10;
9333 : 3616 : ++opcode_base_l10;
9334 : : }
9335 : : const uint8_t *standard_opcode_lengths = linep - 1;
9336 [ + + ]: 47068 : for (uint_fast8_t cnt = 1; cnt < opcode_base; ++cnt)
9337 : 43446 : fprintf (out, ngettext (" [%*" PRIuFAST8 "] %hhu argument\n",
9338 : : " [%*" PRIuFAST8 "] %hhu arguments\n",
9339 : : (int) linep[cnt - 1]),
9340 : 43446 : opcode_base_l10, cnt, linep[cnt - 1]);
9341 : 3622 : linep += opcode_base - 1;
9342 : :
9343 : 3622 : if (unlikely (linep >= lineendp))
9344 : : goto invalid_unit;
9345 : :
9346 : 3622 : Dwarf_Off str_offsets_base = str_offsets_base_off (dbg, NULL);
9347 : :
9348 : 3622 : fputs (_("\nDirectory table:\n"), out);
9349 [ + + ]: 3622 : if (version > 4)
9350 : : {
9351 : 3470 : struct encpair { uint16_t desc; uint16_t form; };
9352 : 3470 : struct encpair enc[256];
9353 : :
9354 : 3470 : fprintf (out, _(" ["));
9355 : 3470 : if ((size_t) (lineendp - linep) < 1)
9356 : 0 : goto invalid_data;
9357 : 3470 : unsigned char directory_entry_format_count = *linep++;
9358 [ + + ]: 6940 : for (int i = 0; i < directory_entry_format_count; i++)
9359 : : {
9360 : 3470 : uint16_t desc, form;
9361 [ - + ]: 3470 : if ((size_t) (lineendp - linep) < 1)
9362 : 0 : goto invalid_data;
9363 : 3470 : get_uleb128 (desc, linep, lineendp);
9364 [ - + ]: 3470 : if ((size_t) (lineendp - linep) < 1)
9365 : 0 : goto invalid_data;
9366 : 3470 : get_uleb128 (form, linep, lineendp);
9367 : :
9368 : 3470 : enc[i].desc = desc;
9369 : 3470 : enc[i].form = form;
9370 : :
9371 : 3470 : fprintf (out, "%s(%s)",
9372 : : dwarf_line_content_description_name (desc),
9373 : : dwarf_form_name (form));
9374 [ - + ]: 3470 : if (i + 1 < directory_entry_format_count)
9375 : 0 : fprintf (out, ", ");
9376 : : }
9377 : 3470 : fprintf (out, "]\n");
9378 : :
9379 : 3470 : uint64_t directories_count;
9380 [ - + ]: 3470 : if ((size_t) (lineendp - linep) < 1)
9381 : 0 : goto invalid_data;
9382 : 3470 : get_uleb128 (directories_count, linep, lineendp);
9383 : :
9384 : 3470 : if (directory_entry_format_count == 0
9385 [ - + ]: 3470 : && directories_count != 0)
9386 : 0 : goto invalid_data;
9387 : :
9388 [ + + ]: 29784 : for (uint64_t i = 0; i < directories_count; i++)
9389 : : {
9390 : 26314 : fprintf (out, " %-5" PRIu64 " ", i);
9391 [ + + ]: 52628 : for (int j = 0; j < directory_entry_format_count; j++)
9392 : : {
9393 : 26314 : linep = print_form_data (dbg, enc[j].form,
9394 : : linep, lineendp, length,
9395 : : str_offsets_base, out);
9396 [ - + ]: 26314 : if (j + 1 < directory_entry_format_count)
9397 : 0 : fprintf (out, ", ");
9398 : : }
9399 : 26314 : fprintf (out, "\n");
9400 [ - + ]: 26314 : if (linep >= lineendp)
9401 : 0 : goto invalid_unit;
9402 : : }
9403 : : }
9404 : : else
9405 : : {
9406 [ + - + + ]: 766 : while (linep < lineendp && *linep != 0)
9407 : : {
9408 : 614 : unsigned char *endp = memchr (linep, '\0', lineendp - linep);
9409 [ - + ]: 614 : if (unlikely (endp == NULL))
9410 : 0 : goto invalid_unit;
9411 : :
9412 : 614 : fprintf (out, " %s\n", (char *) linep);
9413 : :
9414 : 614 : linep = endp + 1;
9415 : : }
9416 [ + - - + ]: 152 : if (linep >= lineendp || *linep != 0)
9417 : 0 : goto invalid_unit;
9418 : : /* Skip the final NUL byte. */
9419 : 152 : ++linep;
9420 : : }
9421 : :
9422 [ - + ]: 3622 : if (unlikely (linep >= lineendp))
9423 : 0 : goto invalid_unit;
9424 : :
9425 : 3622 : fputs (_("\nFile name table:\n"), out);
9426 [ + + ]: 3622 : if (version > 4)
9427 : : {
9428 : 3470 : struct encpair { uint16_t desc; uint16_t form; };
9429 : 3470 : struct encpair enc[256];
9430 : :
9431 : 3470 : fprintf (out, _(" ["));
9432 : 3470 : if ((size_t) (lineendp - linep) < 1)
9433 : 0 : goto invalid_data;
9434 : 3470 : unsigned char file_name_format_count = *linep++;
9435 [ + + ]: 10418 : for (int i = 0; i < file_name_format_count; i++)
9436 : : {
9437 : 6948 : uint64_t desc, form;
9438 [ - + ]: 6948 : if ((size_t) (lineendp - linep) < 1)
9439 : 0 : goto invalid_data;
9440 : 6948 : get_uleb128 (desc, linep, lineendp);
9441 [ - + ]: 6948 : if ((size_t) (lineendp - linep) < 1)
9442 : 0 : goto invalid_data;
9443 : 6948 : get_uleb128 (form, linep, lineendp);
9444 : :
9445 [ - + ]: 6948 : if (! libdw_valid_user_form (form))
9446 : 0 : goto invalid_data;
9447 : :
9448 : 6948 : enc[i].desc = desc;
9449 : 6948 : enc[i].form = form;
9450 : :
9451 : 6948 : fprintf (out, "%s(%s)",
9452 : : dwarf_line_content_description_name (desc),
9453 : : dwarf_form_name (form));
9454 [ + + ]: 6948 : if (i + 1 < file_name_format_count)
9455 : 3478 : fprintf (out, ", ");
9456 : : }
9457 : 3470 : fprintf (out, "]\n");
9458 : :
9459 : 3470 : uint64_t file_name_count;
9460 [ - + ]: 3470 : if ((size_t) (lineendp - linep) < 1)
9461 : 0 : goto invalid_data;
9462 : 3470 : get_uleb128 (file_name_count, linep, lineendp);
9463 : :
9464 : 3470 : if (file_name_format_count == 0
9465 [ - + ]: 3470 : && file_name_count != 0)
9466 : 0 : goto invalid_data;
9467 : :
9468 [ + + ]: 61328 : for (uint64_t i = 0; i < file_name_count; i++)
9469 : : {
9470 : 57858 : fprintf (out, " %-5" PRIu64 " ", i);
9471 [ + + ]: 173598 : for (int j = 0; j < file_name_format_count; j++)
9472 : : {
9473 : 115740 : linep = print_form_data (dbg, enc[j].form,
9474 : : linep, lineendp, length,
9475 : : str_offsets_base, out);
9476 [ + + ]: 115740 : if (j + 1 < file_name_format_count)
9477 : 57882 : fprintf (out, ", ");
9478 : : }
9479 : 57858 : fprintf (out, "\n");
9480 [ - + ]: 57858 : if (linep > lineendp)
9481 : 0 : goto invalid_unit;
9482 : : }
9483 : : }
9484 : : else
9485 : : {
9486 : 152 : fputs (_(" Entry Dir Time Size Name\n"), out);
9487 [ + - + + ]: 6154 : for (unsigned int cnt = 1; linep < lineendp && *linep != 0; ++cnt)
9488 : : {
9489 : : /* First comes the file name. */
9490 : 6002 : char *fname = (char *) linep;
9491 : 6002 : unsigned char *endp = memchr (fname, '\0', lineendp - linep);
9492 [ - + ]: 6002 : if (unlikely (endp == NULL))
9493 : 0 : goto invalid_unit;
9494 : 6002 : linep = endp + 1;
9495 : :
9496 : : /* Then the index. */
9497 : 6002 : unsigned int diridx;
9498 [ - + ]: 6002 : if (lineendp - linep < 1)
9499 : 0 : goto invalid_unit;
9500 : 6002 : get_uleb128 (diridx, linep, lineendp);
9501 : :
9502 : : /* Next comes the modification time. */
9503 : 6002 : unsigned int mtime;
9504 [ - + ]: 6002 : if (lineendp - linep < 1)
9505 : 0 : goto invalid_unit;
9506 : 6002 : get_uleb128 (mtime, linep, lineendp);
9507 : :
9508 : : /* Finally the length of the file. */
9509 : 6002 : unsigned int fsize;
9510 [ - + ]: 6002 : if (lineendp - linep < 1)
9511 : 0 : goto invalid_unit;
9512 : 6002 : get_uleb128 (fsize, linep, lineendp);
9513 : :
9514 : 6002 : fprintf (out, " %-5u %-5u %-9u %-9u %s\n",
9515 : : cnt, diridx, mtime, fsize, fname);
9516 : : }
9517 [ + - - + ]: 152 : if (linep >= lineendp || *linep != '\0')
9518 : 0 : goto invalid_unit;
9519 : : /* Skip the final NUL byte. */
9520 : 152 : ++linep;
9521 : : }
9522 : :
9523 : 3622 : unsigned int debug_str_offset = 0;
9524 [ + + ]: 3622 : if (unlikely (linep == header_start + header_length - 4))
9525 : : {
9526 : : /* CUBINs contain an unsigned 4-byte offset */
9527 [ - + ]: 2 : debug_str_offset = read_4ubyte_unaligned_inc (dbg, linep);
9528 : : }
9529 : :
9530 [ + + ]: 3622 : if (linep == lineendp)
9531 : : {
9532 : 40 : fputs (_("\nNo line number statements.\n"), out);
9533 : 40 : continue;
9534 : : }
9535 : :
9536 : 3582 : fputs (_("\nLine number statements:\n"), out);
9537 : 3582 : Dwarf_Word address = 0;
9538 : 3582 : unsigned int op_index = 0;
9539 : 3582 : size_t line = 1;
9540 : 3582 : uint_fast8_t is_stmt = default_is_stmt;
9541 : :
9542 : : /* Apply the "operation advance" from a special opcode
9543 : : or DW_LNS_advance_pc (as per DWARF4 6.2.5.1). */
9544 : 3582 : unsigned int op_addr_advance;
9545 : : #define advance_pc(op_advance) run_advance_pc(op_advance, minimum_instr_len, \
9546 : : max_ops_per_instr, &op_addr_advance, &address, &op_index)
9547 : :
9548 [ - + ]: 3582 : if (max_ops_per_instr == 0)
9549 : : {
9550 : 0 : error (0, 0,
9551 : 0 : _("invalid maximum operations per instruction is zero"));
9552 : 0 : linep = lineendp;
9553 : 0 : continue;
9554 : : }
9555 : :
9556 [ + + ]: 2523004 : while (linep < lineendp)
9557 : : {
9558 : 2519422 : size_t offset = linep - (const unsigned char *) data->d_buf;
9559 : 2519422 : unsigned int u128;
9560 : 2519422 : int s128;
9561 : :
9562 : : /* Read the opcode. */
9563 : 2519422 : unsigned int opcode = *linep++;
9564 : :
9565 : 2519422 : fprintf (out, " [%6" PRIx64 "]", (uint64_t)offset);
9566 : : /* Is this a special opcode? */
9567 [ + + ]: 2519422 : if (likely (opcode >= opcode_base))
9568 : : {
9569 [ - + ]: 715000 : if (unlikely (line_range == 0))
9570 : 0 : goto invalid_unit;
9571 : :
9572 : : /* Yes. Handling this is quite easy since the opcode value
9573 : : is computed with
9574 : :
9575 : : opcode = (desired line increment - line_base)
9576 : : + (line_range * address advance) + opcode_base
9577 : : */
9578 : 715000 : int line_increment = (line_base
9579 : 715000 : + (opcode - opcode_base) % line_range);
9580 : :
9581 : : /* Perform the increments. */
9582 : 715000 : line += line_increment;
9583 : 715000 : advance_pc ((opcode - opcode_base) / line_range);
9584 : :
9585 : 715000 : fprintf (out, _(" special opcode %u: address+%u = "),
9586 : : opcode, op_addr_advance);
9587 : 715000 : print_dwarf_addr (dwflmod, 0, address, address, out);
9588 [ - + ]: 715000 : if (op_index > 0)
9589 : 0 : fprintf (out, _(", op_index = %u, line%+d = %zu\n"),
9590 : : op_index, line_increment, line);
9591 : : else
9592 : 715000 : fprintf (out, _(", line%+d = %zu\n"),
9593 : : line_increment, line);
9594 : : }
9595 [ + + ]: 1804422 : else if (opcode == 0)
9596 : : {
9597 : : /* This an extended opcode. */
9598 [ - + ]: 190686 : if (unlikely (linep + 2 > lineendp))
9599 : 0 : goto invalid_unit;
9600 : :
9601 : : /* The length. */
9602 : 190686 : unsigned int len = *linep++;
9603 : :
9604 [ - + ]: 190686 : if (unlikely (linep + len > lineendp))
9605 : 0 : goto invalid_unit;
9606 : :
9607 : : /* The sub-opcode. */
9608 : 190686 : opcode = *linep++;
9609 : :
9610 : 190686 : fprintf (out, _(" extended opcode %u: "), opcode);
9611 : :
9612 [ + + - + : 190686 : switch (opcode)
+ - - ]
9613 : : {
9614 : 10608 : case DW_LNE_end_sequence:
9615 : 10608 : fputs (_(" end of sequence\n"), out);
9616 : :
9617 : : /* Reset the registers we care about. */
9618 : 10608 : address = 0;
9619 : 10608 : op_index = 0;
9620 : 10608 : line = 1;
9621 : 10608 : is_stmt = default_is_stmt;
9622 : 10608 : break;
9623 : :
9624 : 12372 : case DW_LNE_set_address:
9625 : 12372 : op_index = 0;
9626 [ - + ]: 12372 : if (unlikely ((size_t) (lineendp - linep) < address_size))
9627 : 0 : goto invalid_unit;
9628 [ + + ]: 12372 : if (address_size == 4)
9629 [ + + ]: 50 : address = read_4ubyte_unaligned_inc (dbg, linep);
9630 : : else
9631 [ + + ]: 12322 : address = read_8ubyte_unaligned_inc (dbg, linep);
9632 : : {
9633 : 12372 : fprintf (out, _(" set address to "));
9634 : 12372 : print_dwarf_addr (dwflmod, 0, address, address, out);
9635 : 12372 : fprintf (out, "\n");
9636 : : }
9637 : 12372 : break;
9638 : :
9639 : 0 : case DW_LNE_define_file:
9640 : : {
9641 : 0 : char *fname = (char *) linep;
9642 : 0 : unsigned char *endp = memchr (linep, '\0',
9643 : 0 : lineendp - linep);
9644 [ # # ]: 0 : if (unlikely (endp == NULL))
9645 : 0 : goto invalid_unit;
9646 : 0 : linep = endp + 1;
9647 : :
9648 : 0 : unsigned int diridx;
9649 [ # # ]: 0 : if (lineendp - linep < 1)
9650 : 0 : goto invalid_unit;
9651 : 0 : get_uleb128 (diridx, linep, lineendp);
9652 : 0 : Dwarf_Word mtime;
9653 [ # # ]: 0 : if (lineendp - linep < 1)
9654 : 0 : goto invalid_unit;
9655 : 0 : get_uleb128 (mtime, linep, lineendp);
9656 : 0 : Dwarf_Word filelength;
9657 [ # # ]: 0 : if (lineendp - linep < 1)
9658 : 0 : goto invalid_unit;
9659 : 0 : get_uleb128 (filelength, linep, lineendp);
9660 : :
9661 : 0 : fprintf (out, _("\
9662 : : define new file: dir=%u, mtime=%" PRIu64 ", length=%" PRIu64 ", name=%s\n"),
9663 : : diridx, (uint64_t) mtime, (uint64_t) filelength,
9664 : : fname);
9665 : : }
9666 : 0 : break;
9667 : :
9668 : 167692 : case DW_LNE_set_discriminator:
9669 : : /* Takes one ULEB128 parameter, the discriminator. */
9670 [ + - - + ]: 167692 : if (unlikely (standard_opcode_lengths[opcode] != 1
9671 : : || lineendp - linep < 1))
9672 : 0 : goto invalid_unit;
9673 : :
9674 : 167692 : get_uleb128 (u128, linep, lineendp);
9675 : 167692 : fprintf (out, _(" set discriminator to %u\n"), u128);
9676 : 167692 : break;
9677 : :
9678 : 14 : case DW_LNE_NVIDIA_inlined_call:
9679 : : {
9680 [ - + ]: 14 : if (unlikely (linep >= lineendp))
9681 : 0 : goto invalid_data;
9682 : :
9683 : 14 : unsigned int context;
9684 : 14 : get_uleb128 (context, linep, lineendp);
9685 : :
9686 [ - + ]: 14 : if (unlikely (linep >= lineendp))
9687 : 0 : goto invalid_data;
9688 : :
9689 : 14 : unsigned int function_name;
9690 : 14 : get_uleb128 (function_name, linep, lineendp);
9691 : 14 : function_name += debug_str_offset;
9692 : :
9693 : 14 : Elf_Data *str_data = dbg->sectiondata[IDX_debug_str];
9694 : 14 : char *function_str;
9695 [ + - - + ]: 14 : if (str_data == NULL || function_name >= str_data->d_size
9696 [ - + ]: 14 : || memchr (str_data->d_buf + function_name, '\0',
9697 : : str_data->d_size - function_name) == NULL)
9698 : : function_str = "???";
9699 : : else
9700 : 14 : function_str = (char *) str_data->d_buf + function_name;
9701 : :
9702 : 14 : fprintf (out, _(" set inlined context %u,"
9703 : : " function name %s (0x%x)\n"),
9704 : : context, function_str, function_name);
9705 : 14 : break;
9706 : : }
9707 : :
9708 : 0 : case DW_LNE_NVIDIA_set_function_name:
9709 : : {
9710 [ # # ]: 0 : if (unlikely (linep >= lineendp))
9711 : 0 : goto invalid_data;
9712 : :
9713 : 0 : unsigned int function_name;
9714 : 0 : get_uleb128 (function_name, linep, lineendp);
9715 : 0 : function_name += debug_str_offset;
9716 : :
9717 : 0 : Elf_Data *str_data = dbg->sectiondata[IDX_debug_str];
9718 : 0 : char *function_str;
9719 [ # # # # ]: 0 : if (str_data == NULL || function_name >= str_data->d_size
9720 [ # # ]: 0 : || memchr (str_data->d_buf + function_name, '\0',
9721 : : str_data->d_size - function_name) == NULL)
9722 : : function_str = "???";
9723 : : else
9724 : 0 : function_str = (char *) str_data->d_buf + function_name;
9725 : :
9726 : 0 : fprintf (out, _(" set function name %s (0x%x)\n"),
9727 : : function_str, function_name);
9728 : : }
9729 : 0 : break;
9730 : :
9731 : 0 : default:
9732 : : /* Unknown, ignore it. */
9733 : 0 : fputs (_(" unknown opcode\n"), out);
9734 : 0 : linep += len - 1;
9735 : 0 : break;
9736 : : }
9737 : : }
9738 [ + - ]: 1613736 : else if (opcode <= DW_LNS_set_isa)
9739 : : {
9740 : : /* This is a known standard opcode. */
9741 [ + + + + : 1613736 : switch (opcode)
+ + - + +
+ - - ]
9742 : : {
9743 : 251532 : case DW_LNS_copy:
9744 : : /* Takes no argument. */
9745 : 251532 : fputs (_(" copy\n"), out);
9746 : 251532 : break;
9747 : :
9748 : 15378 : case DW_LNS_advance_pc:
9749 : : /* Takes one uleb128 parameter which is added to the
9750 : : address. */
9751 [ - + ]: 15378 : if (lineendp - linep < 1)
9752 : 0 : goto invalid_unit;
9753 : 15378 : get_uleb128 (u128, linep, lineendp);
9754 : 15378 : advance_pc (u128);
9755 : : {
9756 : 15378 : fprintf (out, _(" advance address by %u to "),
9757 : : op_addr_advance);
9758 : 15378 : print_dwarf_addr (dwflmod, 0, address, address, out);
9759 [ - + ]: 15378 : if (op_index > 0)
9760 : 0 : fprintf (out, _(", op_index to %u"), op_index);
9761 : 15378 : fprintf (out, "\n");
9762 : : }
9763 : 15378 : break;
9764 : :
9765 : 186198 : case DW_LNS_advance_line:
9766 : : /* Takes one sleb128 parameter which is added to the
9767 : : line. */
9768 [ - + ]: 186198 : if (lineendp - linep < 1)
9769 : 0 : goto invalid_unit;
9770 : 186198 : get_sleb128 (s128, linep, lineendp);
9771 : 186198 : line += s128;
9772 : 186198 : fprintf (out, _("\
9773 : : advance line by constant %d to %" PRId64 "\n"),
9774 : : s128, (int64_t) line);
9775 : 186198 : break;
9776 : :
9777 : 64988 : case DW_LNS_set_file:
9778 : : /* Takes one uleb128 parameter which is stored in file. */
9779 [ - + ]: 64988 : if (lineendp - linep < 1)
9780 : 0 : goto invalid_unit;
9781 : 64988 : get_uleb128 (u128, linep, lineendp);
9782 : 64988 : fprintf (out, _(" set file to %" PRIu64 "\n"),
9783 : : (uint64_t) u128);
9784 : 64988 : break;
9785 : :
9786 : 610036 : case DW_LNS_set_column:
9787 : : /* Takes one uleb128 parameter which is stored in column. */
9788 [ + - - + ]: 610036 : if (unlikely (standard_opcode_lengths[opcode] != 1
9789 : : || lineendp - linep < 1))
9790 : 0 : goto invalid_unit;
9791 : :
9792 : 610036 : get_uleb128 (u128, linep, lineendp);
9793 : 610036 : fprintf (out, _(" set column to %" PRIu64 "\n"),
9794 : : (uint64_t) u128);
9795 : 610036 : break;
9796 : :
9797 : 431962 : case DW_LNS_negate_stmt:
9798 : : /* Takes no argument. */
9799 : 431962 : is_stmt = 1 - is_stmt;
9800 : 431962 : fprintf (out, _(" set '%s' to %" PRIuFAST8 "\n"),
9801 : : "is_stmt", is_stmt);
9802 : 431962 : break;
9803 : :
9804 : 0 : case DW_LNS_set_basic_block:
9805 : : /* Takes no argument. */
9806 : 0 : fputs (_(" set basic block flag\n"), out);
9807 : 0 : break;
9808 : :
9809 : 53588 : case DW_LNS_const_add_pc:
9810 : : /* Takes no argument. */
9811 : :
9812 [ - + ]: 53588 : if (unlikely (line_range == 0))
9813 : 0 : goto invalid_unit;
9814 : :
9815 : 53588 : advance_pc ((255 - opcode_base) / line_range);
9816 : : {
9817 : 53588 : fprintf (out, _(" advance address by constant %u to "),
9818 : : op_addr_advance);
9819 : 53588 : print_dwarf_addr (dwflmod, 0, address, address, out);
9820 [ - + ]: 53588 : if (op_index > 0)
9821 : 0 : fprintf (out, _(", op_index to %u"), op_index);
9822 : 53588 : fprintf (out, "\n");
9823 : : }
9824 : 53588 : break;
9825 : :
9826 : 44 : case DW_LNS_fixed_advance_pc:
9827 : : /* Takes one 16 bit parameter which is added to the
9828 : : address. */
9829 [ + - - + ]: 44 : if (unlikely (standard_opcode_lengths[opcode] != 1
9830 : : || lineendp - linep < 2))
9831 : 0 : goto invalid_unit;
9832 : :
9833 [ - + ]: 44 : u128 = read_2ubyte_unaligned_inc (dbg, linep);
9834 : 44 : address += u128;
9835 : 44 : op_index = 0;
9836 : : {
9837 : 44 : fprintf (out, _("\
9838 : : advance address by fixed value %u to \n"),
9839 : : u128);
9840 : 44 : print_dwarf_addr (dwflmod, 0, address, address, out);
9841 : 44 : fprintf (out, "\n");
9842 : : }
9843 : 44 : break;
9844 : :
9845 : 10 : case DW_LNS_set_prologue_end:
9846 : : /* Takes no argument. */
9847 : 10 : fputs (_(" set prologue end flag\n"), out);
9848 : 10 : break;
9849 : :
9850 : 0 : case DW_LNS_set_epilogue_begin:
9851 : : /* Takes no argument. */
9852 : 0 : fputs (_(" set epilogue begin flag\n"), out);
9853 : 0 : break;
9854 : :
9855 : 0 : case DW_LNS_set_isa:
9856 : : /* Takes one uleb128 parameter which is stored in isa. */
9857 [ # # # # ]: 0 : if (unlikely (standard_opcode_lengths[opcode] != 1
9858 : : || lineendp - linep < 1))
9859 : 0 : goto invalid_unit;
9860 : :
9861 : 0 : get_uleb128 (u128, linep, lineendp);
9862 : 0 : fprintf (out, _(" set isa to %u\n"), u128);
9863 : 0 : break;
9864 : : }
9865 : : }
9866 : : else
9867 : : {
9868 : : /* This is a new opcode the generator but not we know about.
9869 : : Read the parameters associated with it but then discard
9870 : : everything. Read all the parameters for this opcode. */
9871 : 0 : fprintf (out,
9872 : 0 : ngettext (" unknown opcode with %" PRIu8 " parameter:",
9873 : : " unknown opcode with %" PRIu8 " parameters:",
9874 : : standard_opcode_lengths[opcode]),
9875 : 0 : standard_opcode_lengths[opcode]);
9876 : 0 : for (int n = standard_opcode_lengths[opcode];
9877 [ # # # # ]: 0 : n > 0 && linep < lineendp; --n)
9878 : : {
9879 : 0 : get_uleb128 (u128, linep, lineendp);
9880 [ # # ]: 0 : if (n != standard_opcode_lengths[opcode])
9881 : 0 : fputc (',', out);
9882 : 0 : fprintf (out, " %u", u128);
9883 : : }
9884 : :
9885 : : /* Next round, ignore this opcode. */
9886 : 0 : continue;
9887 : : }
9888 : : }
9889 : : }
9890 : :
9891 : : /* There must only be one data block. */
9892 [ - + ]: 104 : assert (elf_getdata (scn, data) == NULL);
9893 : : }
9894 : :
9895 : :
9896 : : static void
9897 : 40 : print_debug_loclists_section (Dwfl_Module *dwflmod,
9898 : : Ebl *ebl,
9899 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
9900 : : Elf_Scn *scn, GElf_Shdr *shdr,
9901 : : Dwarf *dbg, FILE *out)
9902 : : {
9903 : 40 : Elf_Data *data = get_debug_elf_data (dbg, ebl, IDX_debug_loclists, scn);
9904 [ + - ]: 40 : if (data == NULL)
9905 : 0 : return;
9906 : :
9907 : 40 : fprintf (out, _("\
9908 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
9909 : : elf_ndxscn (scn), section_name (ebl, shdr),
9910 : 40 : (uint64_t) shdr->sh_offset);
9911 : :
9912 : : /* For the listptr to get the base address/CU. */
9913 : 40 : sort_listptr (&known_loclistsptr, "loclistsptr");
9914 : 40 : size_t listptr_idx = 0;
9915 : :
9916 : 40 : const unsigned char *readp = data->d_buf;
9917 : 40 : const unsigned char *const dataend = ((unsigned char *) data->d_buf
9918 : 40 : + data->d_size);
9919 [ + + ]: 3132 : while (readp < dataend)
9920 : : {
9921 [ - + ]: 3092 : if (unlikely (readp > dataend - 4))
9922 : : {
9923 : 0 : invalid_data:
9924 : 0 : error (0, 0, _("invalid data in section [%zu] '%s'"),
9925 : : elf_ndxscn (scn), section_name (ebl, shdr));
9926 : 0 : return;
9927 : : }
9928 : :
9929 : 3092 : ptrdiff_t offset = readp - (unsigned char *) data->d_buf;
9930 : 3092 : fprintf (out, _("Table at Offset 0x%" PRIx64 ":\n\n"),
9931 : : (uint64_t) offset);
9932 : :
9933 [ - + ]: 3092 : uint64_t unit_length = read_4ubyte_unaligned_inc (dbg, readp);
9934 : 3092 : unsigned int offset_size = 4;
9935 [ - + ]: 3092 : if (unlikely (unit_length == 0xffffffff))
9936 : : {
9937 [ # # ]: 0 : if (unlikely (readp > dataend - 8))
9938 : 0 : goto invalid_data;
9939 : :
9940 [ # # ]: 0 : unit_length = read_8ubyte_unaligned_inc (dbg, readp);
9941 : 0 : offset_size = 8;
9942 : : }
9943 : 3092 : fprintf (out, _(" Length: %8" PRIu64 "\n"), unit_length);
9944 : :
9945 : : /* We need at least 2-bytes + 1-byte + 1-byte + 4-bytes = 8
9946 : : bytes to complete the header. And this unit cannot go beyond
9947 : : the section data. */
9948 [ + - ]: 3092 : if (readp > dataend - 8
9949 [ + - ]: 3092 : || unit_length < 8
9950 [ - + ]: 3092 : || unit_length > (uint64_t) (dataend - readp))
9951 : 0 : goto invalid_data;
9952 : :
9953 : 3092 : const unsigned char *nexthdr = readp + unit_length;
9954 : :
9955 [ - + ]: 3092 : uint16_t version = read_2ubyte_unaligned_inc (dbg, readp);
9956 : 3092 : fprintf (out, _(" DWARF version: %8" PRIu16 "\n"), version);
9957 : :
9958 [ - + ]: 3092 : if (version != 5)
9959 : : {
9960 : 0 : error (0, 0, _("Unknown version"));
9961 : 0 : goto next_table;
9962 : : }
9963 : :
9964 : 3092 : uint8_t address_size = *readp++;
9965 : 3092 : fprintf (out, _(" Address size: %8" PRIu64 "\n"),
9966 : : (uint64_t) address_size);
9967 : :
9968 [ - + ]: 3092 : if (address_size != 4 && address_size != 8)
9969 : : {
9970 : 0 : error (0, 0, _("unsupported address size"));
9971 : 0 : goto next_table;
9972 : : }
9973 : :
9974 : 3092 : uint8_t segment_size = *readp++;
9975 : 3092 : fprintf (out, _(" Segment size: %8" PRIu64 "\n"),
9976 : : (uint64_t) segment_size);
9977 : :
9978 [ - + ]: 3092 : if (segment_size != 0)
9979 : : {
9980 : 0 : error (0, 0, _("unsupported segment size"));
9981 : 0 : goto next_table;
9982 : : }
9983 : :
9984 [ - + ]: 3092 : uint32_t offset_entry_count = read_4ubyte_unaligned_inc (dbg, readp);
9985 : 3092 : fprintf (out, _(" Offset entries: %8" PRIu64 "\n"),
9986 : : (uint64_t) offset_entry_count);
9987 : :
9988 : : /* We need the CU that uses this unit to get the initial base address. */
9989 : 3092 : Dwarf_Addr cu_base = 0;
9990 : 3092 : struct Dwarf_CU *cu = NULL;
9991 [ - + ]: 3092 : if (listptr_cu (&known_loclistsptr, &listptr_idx,
9992 : : (Dwarf_Off) offset,
9993 : 3092 : (Dwarf_Off) (nexthdr - (unsigned char *) data->d_buf),
9994 : : &cu_base, &cu)
9995 [ # # ]: 0 : || split_dwarf_cu_base (dbg, &cu, &cu_base))
9996 : 3092 : {
9997 : 3092 : Dwarf_Die cudie;
9998 [ - + ]: 3092 : if (dwarf_cu_die (cu, &cudie,
9999 : : NULL, NULL, NULL, NULL,
10000 : : NULL, NULL) == NULL)
10001 : 0 : fprintf (out, _(" Unknown CU base: "));
10002 : : else
10003 : 3092 : fprintf (out, _(" CU [%6" PRIx64 "] base: "),
10004 : : dwarf_dieoffset (&cudie));
10005 : 3092 : print_dwarf_addr (dwflmod, address_size, cu_base, cu_base, out);
10006 : 3092 : fprintf (out, "\n");
10007 : : }
10008 : : else
10009 : 0 : fprintf (out, _(" Not associated with a CU.\n"));
10010 : :
10011 : 3092 : fprintf (out, "\n");
10012 : :
10013 : 3092 : const unsigned char *offset_array_start = readp;
10014 [ + + ]: 3092 : if (offset_entry_count > 0)
10015 : : {
10016 : 4 : uint64_t max_entries = (unit_length - 8) / offset_size;
10017 [ - + ]: 4 : if (offset_entry_count > max_entries)
10018 : : {
10019 : 0 : error (0, 0,
10020 : 0 : _("too many offset entries for unit length"));
10021 : 0 : offset_entry_count = max_entries;
10022 : : }
10023 : :
10024 : 4 : fprintf (out, _(" Offsets starting at 0x%" PRIx64 ":\n"),
10025 : : (uint64_t) (offset_array_start
10026 : 4 : - (unsigned char *) data->d_buf));
10027 [ + + ]: 36 : for (uint32_t idx = 0; idx < offset_entry_count; idx++)
10028 : : {
10029 : 32 : fprintf (out, " [%6" PRIu32 "] ", idx);
10030 [ + - ]: 32 : if (offset_size == 4)
10031 : : {
10032 [ - + ]: 32 : uint32_t off = read_4ubyte_unaligned_inc (dbg, readp);
10033 : 32 : fprintf (out, "0x%" PRIx32 "\n", off);
10034 : : }
10035 : : else
10036 : : {
10037 [ # # ]: 0 : uint64_t off = read_8ubyte_unaligned_inc (dbg, readp);
10038 : 0 : fprintf (out, "0x%" PRIx64 "\n", off);
10039 : : }
10040 : : }
10041 : 4 : fprintf (out, "\n");
10042 : : }
10043 : :
10044 : 3092 : Dwarf_Addr base = cu_base;
10045 : 3092 : bool start_of_list = true;
10046 [ + + ]: 766588 : while (readp < nexthdr)
10047 : : {
10048 : 763496 : Dwarf_Off off = (Dwarf_Off) (readp - (unsigned char *) data->d_buf);
10049 [ + + ]: 763496 : if (listptr_attr (&known_loclistsptr, listptr_idx, off,
10050 : : DW_AT_GNU_locviews))
10051 : 109748 : {
10052 : 109748 : Dwarf_Off next_off = next_listptr_offset (&known_loclistsptr,
10053 : : &listptr_idx, off);
10054 : 109748 : const unsigned char *locp = readp;
10055 : 109748 : const unsigned char *locendp;
10056 [ + - ]: 109748 : if (next_off == 0
10057 [ + - ]: 109748 : || next_off > (size_t) (nexthdr - ((const unsigned char *)
10058 : : data->d_buf)))
10059 : : locendp = nexthdr;
10060 : : else
10061 : 109748 : locendp = (const unsigned char *) data->d_buf + next_off;
10062 : :
10063 : 109748 : fprintf (out, " Offset: %" PRIx64 ", Index: %" PRIx64 "\n",
10064 : : (uint64_t) (readp - (unsigned char *) data->d_buf),
10065 : 109748 : (uint64_t) (readp - offset_array_start));
10066 : :
10067 [ + + ]: 607960 : while (locp < locendp)
10068 : : {
10069 : 498212 : uint64_t v1, v2;
10070 : 498212 : get_uleb128 (v1, locp, locendp);
10071 [ - + ]: 498212 : if (locp >= locendp)
10072 : : {
10073 : 0 : fprintf (out, _(" <INVALID DATA>\n"));
10074 : 0 : break;
10075 : : }
10076 : 498212 : get_uleb128 (v2, locp, locendp);
10077 : 498212 : fprintf (out,
10078 : : " view pair %" PRId64 ", %" PRId64 "\n", v1, v2);
10079 : : }
10080 : :
10081 : 109748 : fprintf (out, "\n");
10082 : 109748 : readp = (unsigned char *) locendp;
10083 : 109748 : continue;
10084 : : }
10085 : :
10086 : 653748 : uint8_t kind = *readp++;
10087 : 653748 : uint64_t op1, op2, len;
10088 : :
10089 : : /* Skip padding. */
10090 [ - + ]: 653748 : if (start_of_list && kind == DW_LLE_end_of_list)
10091 : 0 : continue;
10092 : :
10093 [ + + ]: 653748 : if (start_of_list)
10094 : : {
10095 : 109812 : base = cu_base;
10096 : 109812 : fprintf (out, " Offset: %" PRIx64 ", Index: %" PRIx64 "\n",
10097 : 109812 : (uint64_t) (readp - (unsigned char *) data->d_buf - 1),
10098 : 109812 : (uint64_t) (readp - offset_array_start - 1));
10099 : 109812 : start_of_list = false;
10100 : : }
10101 : :
10102 : 653748 : fprintf (out, " %s", dwarf_loc_list_encoding_name (kind));
10103 [ + - - + : 653748 : switch (kind)
+ - + - +
- - ]
10104 : : {
10105 : 109812 : case DW_LLE_end_of_list:
10106 : 109812 : start_of_list = true;
10107 : 109812 : fprintf (out, "\n\n");
10108 : 109812 : break;
10109 : :
10110 : 0 : case DW_LLE_base_addressx:
10111 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
10112 : : {
10113 : 0 : invalid_entry:
10114 : 0 : error (0, 0, _("invalid loclists data"));
10115 : 0 : goto next_table;
10116 : : }
10117 : 0 : get_uleb128 (op1, readp, nexthdr);
10118 : 0 : fprintf (out, " %" PRIx64 "\n", op1);
10119 [ # # ]: 0 : if (! print_unresolved_addresses)
10120 : : {
10121 : 0 : Dwarf_Addr addr;
10122 [ # # ]: 0 : if (get_indexed_addr (cu, op1, &addr) != 0)
10123 : 0 : fprintf (out, " ???\n");
10124 : : else
10125 : : {
10126 : 0 : fprintf (out, " ");
10127 : 0 : print_dwarf_addr (dwflmod, address_size, addr, addr, out);
10128 : 0 : fprintf (out, "\n");
10129 : : }
10130 : : }
10131 : : break;
10132 : :
10133 : 0 : case DW_LLE_startx_endx:
10134 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
10135 : 0 : goto invalid_entry;
10136 : 0 : get_uleb128 (op1, readp, nexthdr);
10137 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
10138 : 0 : goto invalid_entry;
10139 : 0 : get_uleb128 (op2, readp, nexthdr);
10140 : 0 : fprintf (out, " %" PRIx64 ", %" PRIx64 "\n", op1, op2);
10141 [ # # ]: 0 : if (! print_unresolved_addresses)
10142 : : {
10143 : 0 : Dwarf_Addr addr1;
10144 : 0 : Dwarf_Addr addr2;
10145 [ # # ]: 0 : if (get_indexed_addr (cu, op1, &addr1) != 0
10146 [ # # ]: 0 : || get_indexed_addr (cu, op2, &addr2) != 0)
10147 : : {
10148 : 0 : fprintf (out, " ???..\n");
10149 : 0 : fprintf (out, " ???\n");
10150 : : }
10151 : : else
10152 : : {
10153 : 0 : fprintf (out, " ");
10154 : 0 : print_dwarf_addr (dwflmod, address_size,
10155 : : addr1, addr1, out);
10156 : 0 : fprintf (out, "..\n ");
10157 : 0 : print_dwarf_addr (dwflmod, address_size,
10158 : : addr2 - 1, addr2, out);
10159 : 0 : fprintf (out, "\n");
10160 : : }
10161 : : }
10162 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
10163 : 0 : goto invalid_entry;
10164 : 0 : get_uleb128 (len, readp, nexthdr);
10165 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < len)
10166 : 0 : goto invalid_entry;
10167 : 0 : print_ops (dwflmod, dbg, 8, 8, version,
10168 : : address_size, offset_size, cu, len, readp, out);
10169 : 0 : readp += len;
10170 : 0 : break;
10171 : :
10172 : 52 : case DW_LLE_startx_length:
10173 [ - + ]: 52 : if ((uint64_t) (nexthdr - readp) < 1)
10174 : 0 : goto invalid_entry;
10175 : 52 : get_uleb128 (op1, readp, nexthdr);
10176 [ - + ]: 52 : if ((uint64_t) (nexthdr - readp) < 1)
10177 : 0 : goto invalid_entry;
10178 : 52 : get_uleb128 (op2, readp, nexthdr);
10179 : 52 : fprintf (out, " %" PRIx64 ", %" PRIx64 "\n", op1, op2);
10180 [ + - ]: 52 : if (! print_unresolved_addresses)
10181 : : {
10182 : 52 : Dwarf_Addr addr1;
10183 : 52 : Dwarf_Addr addr2;
10184 [ - + ]: 52 : if (get_indexed_addr (cu, op1, &addr1) != 0)
10185 : : {
10186 : 0 : fprintf (out, " ???..\n");
10187 : 0 : fprintf (out, " ???\n");
10188 : : }
10189 : : else
10190 : : {
10191 : 52 : addr2 = addr1 + op2;
10192 : 52 : fprintf (out, " ");
10193 : 52 : print_dwarf_addr (dwflmod, address_size, addr1, addr1, out);
10194 : 52 : fprintf (out, "..\n ");
10195 : 52 : print_dwarf_addr (dwflmod, address_size,
10196 : : addr2 - 1, addr2, out);
10197 : 52 : fprintf (out, "\n");
10198 : : }
10199 : : }
10200 [ - + ]: 52 : if ((uint64_t) (nexthdr - readp) < 1)
10201 : 0 : goto invalid_entry;
10202 : 52 : get_uleb128 (len, readp, nexthdr);
10203 [ - + ]: 52 : if ((uint64_t) (nexthdr - readp) < len)
10204 : 0 : goto invalid_entry;
10205 : 52 : print_ops (dwflmod, dbg, 8, 8, version,
10206 : : address_size, offset_size, cu, len, readp, out);
10207 : 52 : readp += len;
10208 : 52 : break;
10209 : :
10210 : 476122 : case DW_LLE_offset_pair:
10211 [ - + ]: 476122 : if ((uint64_t) (nexthdr - readp) < 1)
10212 : 0 : goto invalid_entry;
10213 : 476122 : get_uleb128 (op1, readp, nexthdr);
10214 [ - + ]: 476122 : if ((uint64_t) (nexthdr - readp) < 1)
10215 : 0 : goto invalid_entry;
10216 : 476122 : get_uleb128 (op2, readp, nexthdr);
10217 : 476122 : fprintf (out, " %" PRIx64 ", %" PRIx64 "\n", op1, op2);
10218 [ + - ]: 476122 : if (! print_unresolved_addresses)
10219 : : {
10220 : 476122 : op1 += base;
10221 : 476122 : op2 += base;
10222 : 476122 : fprintf (out, " ");
10223 : 476122 : print_dwarf_addr (dwflmod, address_size, op1, op1, out);
10224 : 476122 : fprintf (out, "..\n ");
10225 : 476122 : print_dwarf_addr (dwflmod, address_size, op2 - 1, op2, out);
10226 : 476122 : fprintf (out, "\n");
10227 : : }
10228 [ - + ]: 476122 : if ((uint64_t) (nexthdr - readp) < 1)
10229 : 0 : goto invalid_entry;
10230 : 476122 : get_uleb128 (len, readp, nexthdr);
10231 [ - + ]: 476122 : if ((uint64_t) (nexthdr - readp) < len)
10232 : 0 : goto invalid_entry;
10233 : 476122 : print_ops (dwflmod, dbg, 8, 8, version,
10234 : : address_size, offset_size, cu, len, readp, out);
10235 : 476122 : readp += len;
10236 : 476122 : break;
10237 : :
10238 : 0 : case DW_LLE_default_location:
10239 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
10240 : 0 : goto invalid_entry;
10241 : 0 : get_uleb128 (len, readp, nexthdr);
10242 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < len)
10243 : 0 : goto invalid_entry;
10244 : 0 : print_ops (dwflmod, dbg, 8, 8, version,
10245 : : address_size, offset_size, cu, len, readp, out);
10246 : 0 : readp += len;
10247 : 0 : break;
10248 : :
10249 : 45620 : case DW_LLE_base_address:
10250 [ - + ]: 45620 : if (address_size == 4)
10251 : : {
10252 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 4)
10253 : 0 : goto invalid_entry;
10254 [ # # ]: 0 : op1 = read_4ubyte_unaligned_inc (dbg, readp);
10255 : : }
10256 : : else
10257 : : {
10258 [ - + ]: 45620 : if ((uint64_t) (nexthdr - readp) < 8)
10259 : 0 : goto invalid_entry;
10260 [ - + ]: 45620 : op1 = read_8ubyte_unaligned_inc (dbg, readp);
10261 : : }
10262 : 45620 : base = op1;
10263 : 45620 : fprintf (out, " 0x%" PRIx64 "\n", base);
10264 [ + - ]: 45620 : if (! print_unresolved_addresses)
10265 : : {
10266 : 45620 : fprintf (out, " ");
10267 : 45620 : print_dwarf_addr (dwflmod, address_size, base, base, out);
10268 : 45620 : fprintf (out, "\n");
10269 : : }
10270 : : break;
10271 : :
10272 : 0 : case DW_LLE_start_end:
10273 [ # # ]: 0 : if (address_size == 4)
10274 : : {
10275 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 8)
10276 : 0 : goto invalid_entry;
10277 [ # # ]: 0 : op1 = read_4ubyte_unaligned_inc (dbg, readp);
10278 [ # # ]: 0 : op2 = read_4ubyte_unaligned_inc (dbg, readp);
10279 : : }
10280 : : else
10281 : : {
10282 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 16)
10283 : 0 : goto invalid_entry;
10284 [ # # ]: 0 : op1 = read_8ubyte_unaligned_inc (dbg, readp);
10285 [ # # ]: 0 : op2 = read_8ubyte_unaligned_inc (dbg, readp);
10286 : : }
10287 : 0 : fprintf (out, " 0x%" PRIx64 "..0x%" PRIx64 "\n", op1, op2);
10288 [ # # ]: 0 : if (! print_unresolved_addresses)
10289 : : {
10290 : 0 : fprintf (out, " ");
10291 : 0 : print_dwarf_addr (dwflmod, address_size, op1, op1, out);
10292 : 0 : fprintf (out, "..\n ");
10293 : 0 : print_dwarf_addr (dwflmod, address_size, op2 - 1, op2, out);
10294 : 0 : fprintf (out, "\n");
10295 : : }
10296 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
10297 : 0 : goto invalid_entry;
10298 : 0 : get_uleb128 (len, readp, nexthdr);
10299 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < len)
10300 : 0 : goto invalid_entry;
10301 : 0 : print_ops (dwflmod, dbg, 8, 8, version,
10302 : : address_size, offset_size, cu, len, readp, out);
10303 : 0 : readp += len;
10304 : 0 : break;
10305 : :
10306 : 22142 : case DW_LLE_start_length:
10307 [ - + ]: 22142 : if (address_size == 4)
10308 : : {
10309 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 4)
10310 : 0 : goto invalid_entry;
10311 [ # # ]: 0 : op1 = read_4ubyte_unaligned_inc (dbg, readp);
10312 : : }
10313 : : else
10314 : : {
10315 [ - + ]: 22142 : if ((uint64_t) (nexthdr - readp) < 8)
10316 : 0 : goto invalid_entry;
10317 [ - + ]: 22142 : op1 = read_8ubyte_unaligned_inc (dbg, readp);
10318 : : }
10319 [ - + ]: 22142 : if ((uint64_t) (nexthdr - readp) < 1)
10320 : 0 : goto invalid_entry;
10321 : 22142 : get_uleb128 (op2, readp, nexthdr);
10322 : 22142 : fprintf (out, " 0x%" PRIx64 ", %" PRIx64 "\n", op1, op2);
10323 [ + + ]: 22142 : if (! print_unresolved_addresses)
10324 : : {
10325 : 22140 : op2 = op1 + op2;
10326 : 22140 : fprintf (out, " ");
10327 : 22140 : print_dwarf_addr (dwflmod, address_size, op1, op1, out);
10328 : 22140 : fprintf (out, "..\n ");
10329 : 22140 : print_dwarf_addr (dwflmod, address_size, op2 - 1, op2, out);
10330 : 22140 : fprintf (out, "\n");
10331 : : }
10332 [ - + ]: 22142 : if ((uint64_t) (nexthdr - readp) < 1)
10333 : 0 : goto invalid_entry;
10334 : 22142 : get_uleb128 (len, readp, nexthdr);
10335 [ - + ]: 22142 : if ((uint64_t) (nexthdr - readp) < len)
10336 : 0 : goto invalid_entry;
10337 : 22142 : print_ops (dwflmod, dbg, 8, 8, version,
10338 : : address_size, offset_size, cu, len, readp, out);
10339 : 22142 : readp += len;
10340 : 22142 : break;
10341 : :
10342 : 0 : case DW_LLE_GNU_view_pair:
10343 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
10344 : 0 : goto invalid_entry;
10345 : 0 : get_uleb128 (op1, readp, nexthdr);
10346 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
10347 : 0 : goto invalid_entry;
10348 : 0 : get_uleb128 (op2, readp, nexthdr);
10349 : 0 : fprintf (out, " %" PRIx64 ", %" PRIx64 "\n", op1, op2);
10350 : 0 : break;
10351 : :
10352 : 0 : default:
10353 : 0 : goto invalid_entry;
10354 : : }
10355 : : }
10356 : :
10357 : 3092 : next_table:
10358 [ - + ]: 3092 : if (readp != nexthdr)
10359 : : {
10360 : 0 : size_t padding = nexthdr - readp;
10361 : 0 : fprintf (out, _(" %zu padding bytes\n\n"), padding);
10362 : 0 : readp = nexthdr;
10363 : : }
10364 : : }
10365 : : }
10366 : :
10367 : :
10368 : : static void
10369 : 52 : print_debug_loc_section (Dwfl_Module *dwflmod,
10370 : : Ebl *ebl, GElf_Ehdr *ehdr,
10371 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg,
10372 : : FILE *out)
10373 : : {
10374 : 52 : Elf_Data *data = get_debug_elf_data (dbg, ebl, IDX_debug_loc, scn);
10375 [ - + ]: 52 : if (data == NULL)
10376 : 0 : return;
10377 : :
10378 : 52 : fprintf (out, _("\
10379 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
10380 : : elf_ndxscn (scn), section_name (ebl, shdr),
10381 : 52 : (uint64_t) shdr->sh_offset);
10382 : :
10383 : 52 : sort_listptr (&known_locsptr, "loclistptr");
10384 : 52 : size_t listptr_idx = 0;
10385 : :
10386 [ + - ]: 52 : uint_fast8_t address_size = ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 4 : 8;
10387 : 52 : uint_fast8_t offset_size = 4;
10388 : :
10389 : 52 : bool first = true;
10390 : 52 : Dwarf_Addr base = 0;
10391 : 52 : unsigned char *readp = data->d_buf;
10392 : 52 : unsigned char *const endp = (unsigned char *) data->d_buf + data->d_size;
10393 : 52 : Dwarf_CU *last_cu = NULL;
10394 [ + + ]: 490 : while (readp < endp)
10395 : : {
10396 : 438 : ptrdiff_t offset = readp - (unsigned char *) data->d_buf;
10397 : 438 : Dwarf_CU *cu = last_cu;
10398 : 438 : unsigned int attr = 0;
10399 : :
10400 [ + + - + ]: 438 : if (first && skip_listptr_hole (&known_locsptr, &listptr_idx,
10401 : : &address_size, &offset_size, &base,
10402 : : &cu, offset, &readp, endp, &attr))
10403 : 0 : continue;
10404 : :
10405 [ + + ]: 438 : if (last_cu != cu)
10406 : : {
10407 : 60 : Dwarf_Die cudie;
10408 [ - + ]: 60 : if (dwarf_cu_die (cu, &cudie,
10409 : : NULL, NULL, NULL, NULL,
10410 : : NULL, NULL) == NULL)
10411 : 0 : fprintf (out, _("\n Unknown CU base: "));
10412 : : else
10413 : 60 : fprintf (out, _("\n CU [%6" PRIx64 "] base: "),
10414 : : dwarf_dieoffset (&cudie));
10415 : 60 : print_dwarf_addr (dwflmod, address_size, base, base, out);
10416 : 60 : fprintf (out, "\n");
10417 : : }
10418 : 438 : last_cu = cu;
10419 : :
10420 [ - + ]: 438 : if (attr == DW_AT_GNU_locviews)
10421 : 0 : {
10422 : 0 : Dwarf_Off next_off = next_listptr_offset (&known_locsptr,
10423 : : &listptr_idx, offset);
10424 : 0 : const unsigned char *locp = readp;
10425 : 0 : const unsigned char *locendp;
10426 [ # # ]: 0 : if (next_off == 0
10427 : 0 : || next_off > (size_t) (endp
10428 [ # # ]: 0 : - (const unsigned char *) data->d_buf))
10429 : 0 : locendp = endp;
10430 : : else
10431 : 0 : locendp = (const unsigned char *) data->d_buf + next_off;
10432 : :
10433 [ # # ]: 0 : while (locp < locendp)
10434 : : {
10435 : 0 : uint64_t v1, v2;
10436 : 0 : get_uleb128 (v1, locp, locendp);
10437 [ # # ]: 0 : if (locp >= locendp)
10438 : : {
10439 : 0 : fprintf (out, _(" [%6tx] <INVALID DATA>\n"), offset);
10440 : 0 : break;
10441 : : }
10442 : 0 : get_uleb128 (v2, locp, locendp);
10443 [ # # ]: 0 : if (first) /* First view pair in a list. */
10444 : 0 : fprintf (out, " [%6tx] ", offset);
10445 : : else
10446 : 0 : fprintf (out, " ");
10447 : 0 : fprintf (out, "view pair %" PRId64 ", %" PRId64 "\n", v1, v2);
10448 : 0 : first = false;
10449 : : }
10450 : :
10451 : 0 : first = true;
10452 : 0 : readp = (unsigned char *) locendp;
10453 : 0 : continue;
10454 : : }
10455 : :
10456 : : /* GNU DebugFission encoded addresses as addrx. */
10457 : 1194 : bool is_debugfission = ((cu != NULL
10458 [ # # ]: 0 : || split_dwarf_cu_base (dbg, &cu, &base))
10459 [ - + + - ]: 438 : && (cu->version < 5
10460 [ + + ]: 438 : && cu->unit_type == DW_UT_split_compile));
10461 : 318 : if (!is_debugfission
10462 [ - + ]: 318 : && unlikely (data->d_size - offset < (size_t) address_size * 2))
10463 : : {
10464 : 0 : invalid_data:
10465 : 0 : fprintf (out, _(" [%6tx] <INVALID DATA>\n"), offset);
10466 : 0 : break;
10467 : : }
10468 : :
10469 : 438 : Dwarf_Addr begin;
10470 : 438 : Dwarf_Addr end;
10471 : 438 : bool use_base = true;
10472 [ + + ]: 438 : if (is_debugfission)
10473 : : {
10474 : 120 : const unsigned char *locp = readp;
10475 : 120 : const unsigned char *locendp = readp + data->d_size;
10476 [ - + ]: 120 : if (locp >= locendp)
10477 : 0 : goto invalid_data;
10478 : :
10479 : 120 : Dwarf_Word idx;
10480 : 120 : unsigned char code = *locp++;
10481 [ + - - + : 120 : switch (code)
- ]
10482 : : {
10483 : 40 : case DW_LLE_GNU_end_of_list_entry:
10484 : 40 : begin = 0;
10485 : 40 : end = 0;
10486 : 40 : break;
10487 : :
10488 : 0 : case DW_LLE_GNU_base_address_selection_entry:
10489 [ # # ]: 0 : if (locp >= locendp)
10490 : 0 : goto invalid_data;
10491 : 0 : begin = (Dwarf_Addr) -1;
10492 : 0 : get_uleb128 (idx, locp, locendp);
10493 [ # # ]: 0 : if (get_indexed_addr (cu, idx, &end) != 0)
10494 : 0 : end = idx; /* ... */
10495 : : break;
10496 : :
10497 : 0 : case DW_LLE_GNU_start_end_entry:
10498 [ # # ]: 0 : if (locp >= locendp)
10499 : 0 : goto invalid_data;
10500 : 0 : get_uleb128 (idx, locp, locendp);
10501 [ # # ]: 0 : if (get_indexed_addr (cu, idx, &begin) != 0)
10502 : 0 : begin = idx; /* ... */
10503 [ # # ]: 0 : if (locp >= locendp)
10504 : 0 : goto invalid_data;
10505 : 0 : get_uleb128 (idx, locp, locendp);
10506 [ # # ]: 0 : if (get_indexed_addr (cu, idx, &end) != 0)
10507 : 0 : end = idx; /* ... */
10508 : : use_base = false;
10509 : : break;
10510 : :
10511 : 80 : case DW_LLE_GNU_start_length_entry:
10512 [ - + ]: 80 : if (locp >= locendp)
10513 : 0 : goto invalid_data;
10514 : 80 : get_uleb128 (idx, locp, locendp);
10515 [ - + ]: 80 : if (get_indexed_addr (cu, idx, &begin) != 0)
10516 : 0 : begin = idx; /* ... */
10517 [ - + ]: 80 : if (locendp - locp < 4)
10518 : 0 : goto invalid_data;
10519 [ - + ]: 80 : end = read_4ubyte_unaligned_inc (dbg, locp);
10520 : 80 : end += begin;
10521 : 80 : use_base = false;
10522 : 80 : break;
10523 : :
10524 : 0 : default:
10525 : 0 : goto invalid_data;
10526 : : }
10527 : :
10528 : 120 : readp = (unsigned char *) locp;
10529 : : }
10530 [ + - ]: 318 : else if (address_size == 8)
10531 : : {
10532 [ + + ]: 318 : begin = read_8ubyte_unaligned_inc (dbg, readp);
10533 [ + + ]: 318 : end = read_8ubyte_unaligned_inc (dbg, readp);
10534 : : }
10535 : : else
10536 : : {
10537 [ # # ]: 0 : begin = read_4ubyte_unaligned_inc (dbg, readp);
10538 [ # # ]: 0 : end = read_4ubyte_unaligned_inc (dbg, readp);
10539 [ # # ]: 0 : if (begin == (Dwarf_Addr) (uint32_t) -1)
10540 : 0 : begin = (Dwarf_Addr) -1l;
10541 : : }
10542 : :
10543 [ - + ]: 438 : if (begin == (Dwarf_Addr) -1l) /* Base address entry. */
10544 : : {
10545 [ # # ]: 0 : if (first)
10546 : 0 : fprintf (out, " [%6tx] ", offset);
10547 : : else
10548 : 0 : fprintf (out, " ");
10549 : 0 : fputs (_("base address\n"), out);
10550 : 0 : fprintf (out, " ");
10551 : 0 : print_dwarf_addr (dwflmod, address_size, end, end, out);
10552 : 0 : fprintf (out, "\n");
10553 : 0 : base = end;
10554 : 0 : first = false;
10555 : : }
10556 [ + + + + ]: 438 : else if (begin == 0 && end == 0) /* End of list entry. */
10557 : : {
10558 [ - + ]: 148 : if (first)
10559 : 0 : fprintf (out, _(" [%6tx] empty list\n"), offset);
10560 : : first = true;
10561 : : }
10562 : : else
10563 : : {
10564 : : /* We have a location expression entry. */
10565 [ + + ]: 290 : uint_fast16_t len = read_2ubyte_unaligned_inc (dbg, readp);
10566 : :
10567 [ + + ]: 290 : if (first) /* First entry in a list. */
10568 : 148 : fprintf (out, " [%6tx] ", offset);
10569 : : else
10570 : 142 : fprintf (out, " ");
10571 : :
10572 : 290 : fprintf (out, "range %" PRIx64 ", %" PRIx64 "\n", begin, end);
10573 [ + + ]: 290 : if (! print_unresolved_addresses)
10574 : : {
10575 : 230 : Dwarf_Addr dab = use_base ? base + begin : begin;
10576 : 230 : Dwarf_Addr dae = use_base ? base + end : end;
10577 : 230 : fprintf (out, " ");
10578 : 230 : print_dwarf_addr (dwflmod, address_size, dab, dab, out);
10579 : 230 : fprintf (out, "..\n ");
10580 : 230 : print_dwarf_addr (dwflmod, address_size, dae - 1, dae, out);
10581 : 230 : fprintf (out, "\n");
10582 : : }
10583 : :
10584 [ - + ]: 290 : if (endp - readp <= (ptrdiff_t) len)
10585 : : {
10586 : 0 : fputs (_(" <INVALID DATA>\n"), out);
10587 : 0 : break;
10588 : : }
10589 : :
10590 [ + - ]: 580 : print_ops (dwflmod, dbg, 11, 11,
10591 : 290 : cu != NULL ? cu->version : 3,
10592 : : address_size, offset_size, cu, len, readp, out);
10593 : :
10594 : 290 : first = false;
10595 : 290 : readp += len;
10596 : : }
10597 : : }
10598 : : }
10599 : :
10600 : : struct mac_culist
10601 : : {
10602 : : Dwarf_Die die;
10603 : : Dwarf_Off offset;
10604 : : Dwarf_Files *files;
10605 : : struct mac_culist *next;
10606 : : };
10607 : :
10608 : :
10609 : : static int
10610 : 0 : mac_compare (const void *p1, const void *p2)
10611 : : {
10612 : 0 : struct mac_culist *m1 = (struct mac_culist *) p1;
10613 : 0 : struct mac_culist *m2 = (struct mac_culist *) p2;
10614 : :
10615 [ # # ]: 0 : if (m1->offset < m2->offset)
10616 : : return -1;
10617 [ # # ]: 0 : if (m1->offset > m2->offset)
10618 : 0 : return 1;
10619 : : return 0;
10620 : : }
10621 : :
10622 : :
10623 : : static void
10624 : 0 : print_debug_macinfo_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
10625 : : Ebl *ebl,
10626 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
10627 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg,
10628 : : FILE *out)
10629 : : {
10630 : 0 : Elf_Data *data = get_debug_elf_data (dbg, ebl, IDX_debug_macinfo, scn);
10631 [ # # ]: 0 : if (data == NULL)
10632 : 0 : return;
10633 : :
10634 : 0 : fprintf (out, _("\
10635 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
10636 : : elf_ndxscn (scn), section_name (ebl, shdr),
10637 : 0 : (uint64_t) shdr->sh_offset);
10638 : 0 : fputc ('\n', out);
10639 : :
10640 : : /* There is no function in libdw to iterate over the raw content of
10641 : : the section but it is easy enough to do. */
10642 : :
10643 : : /* Get the source file information for all CUs. */
10644 : 0 : Dwarf_Off offset;
10645 : 0 : Dwarf_Off ncu = 0;
10646 : 0 : size_t hsize;
10647 : 0 : struct mac_culist *culist = NULL;
10648 : 0 : size_t nculist = 0;
10649 [ # # ]: 0 : while (dwarf_nextcu (dbg, offset = ncu, &ncu, &hsize, NULL, NULL, NULL) == 0)
10650 : : {
10651 : 0 : Dwarf_Die cudie;
10652 [ # # ]: 0 : if (dwarf_offdie (dbg, offset + hsize, &cudie) == NULL)
10653 : 0 : continue;
10654 : :
10655 : 0 : Dwarf_Attribute attr;
10656 [ # # ]: 0 : if (dwarf_attr (&cudie, DW_AT_macro_info, &attr) == NULL)
10657 : 0 : continue;
10658 : :
10659 : 0 : Dwarf_Word macoff;
10660 [ # # ]: 0 : if (dwarf_formudata (&attr, &macoff) != 0)
10661 : 0 : continue;
10662 : :
10663 : 0 : struct mac_culist *newp = (struct mac_culist *) alloca (sizeof (*newp));
10664 : 0 : newp->die = cudie;
10665 : 0 : newp->offset = macoff;
10666 : 0 : newp->files = NULL;
10667 : 0 : newp->next = culist;
10668 : 0 : culist = newp;
10669 : 0 : ++nculist;
10670 : : }
10671 : :
10672 : : /* Convert the list into an array for easier consumption. */
10673 : 0 : struct mac_culist *cus = (struct mac_culist *) alloca ((nculist + 1)
10674 : : * sizeof (*cus));
10675 : : /* Add sentinel. */
10676 : 0 : cus[nculist].offset = data->d_size;
10677 : 0 : cus[nculist].files = (Dwarf_Files *) -1l;
10678 [ # # ]: 0 : if (nculist > 0)
10679 : : {
10680 [ # # ]: 0 : for (size_t cnt = nculist - 1; culist != NULL; --cnt)
10681 : : {
10682 [ # # ]: 0 : assert (cnt < nculist);
10683 : 0 : cus[cnt] = *culist;
10684 : 0 : culist = culist->next;
10685 : : }
10686 : :
10687 : : /* Sort the array according to the offset in the .debug_macinfo
10688 : : section. Note we keep the sentinel at the end. */
10689 : 0 : qsort (cus, nculist, sizeof (*cus), mac_compare);
10690 : : }
10691 : :
10692 : 0 : const unsigned char *readp = (const unsigned char *) data->d_buf;
10693 : 0 : const unsigned char *readendp = readp + data->d_size;
10694 : 0 : int level = 1;
10695 : :
10696 [ # # ]: 0 : while (readp < readendp)
10697 : : {
10698 : 0 : unsigned int opcode = *readp++;
10699 : 0 : unsigned int u128;
10700 : 0 : unsigned int u128_2;
10701 : 0 : const unsigned char *endp;
10702 : :
10703 [ # # # # ]: 0 : switch (opcode)
10704 : : {
10705 : 0 : case DW_MACINFO_define:
10706 : : case DW_MACINFO_undef:
10707 : : case DW_MACINFO_vendor_ext:
10708 : : /* For the first two opcodes the parameters are
10709 : : line, string
10710 : : For the latter
10711 : : number, string.
10712 : : We can treat these cases together. */
10713 : 0 : get_uleb128 (u128, readp, readendp);
10714 : :
10715 : 0 : endp = memchr (readp, '\0', readendp - readp);
10716 [ # # ]: 0 : if (unlikely (endp == NULL))
10717 : : {
10718 : 0 : fprintf (out, _("\
10719 : : %*s*** non-terminated string at end of section"),
10720 : : level, "");
10721 : 0 : return;
10722 : : }
10723 : :
10724 [ # # ]: 0 : if (opcode == DW_MACINFO_define)
10725 : 0 : fprintf (out, "%*s#define %s, line %u\n",
10726 : : level, "", (char *) readp, u128);
10727 [ # # ]: 0 : else if (opcode == DW_MACINFO_undef)
10728 : 0 : fprintf (out, "%*s#undef %s, line %u\n",
10729 : : level, "", (char *) readp, u128);
10730 : : else
10731 : 0 : fprintf (out, " #vendor-ext %s, number %u\n", (char *) readp, u128);
10732 : :
10733 : 0 : readp = endp + 1;
10734 : 0 : break;
10735 : :
10736 : 0 : case DW_MACINFO_start_file:
10737 : : /* The two parameters are line and file index, in this order. */
10738 : 0 : get_uleb128 (u128, readp, readendp);
10739 [ # # ]: 0 : if (readendp - readp < 1)
10740 : : {
10741 : 0 : fprintf (out, _("\
10742 : : %*s*** missing DW_MACINFO_start_file argument at end of section"),
10743 : : level, "");
10744 : 0 : return;
10745 : : }
10746 : 0 : get_uleb128 (u128_2, readp, readendp);
10747 : :
10748 : : /* Find the CU DIE for this file. */
10749 : 0 : size_t macoff = readp - (const unsigned char *) data->d_buf;
10750 : 0 : const char *fname = "???";
10751 [ # # # # ]: 0 : if (macoff >= cus[0].offset && cus[0].offset != data->d_size)
10752 : : {
10753 [ # # # # ]: 0 : while (macoff >= cus[1].offset && cus[1].offset != data->d_size)
10754 : 0 : ++cus;
10755 : :
10756 [ # # ]: 0 : if (cus[0].files == NULL
10757 [ # # ]: 0 : && dwarf_getsrcfiles (&cus[0].die, &cus[0].files, NULL) != 0)
10758 : 0 : cus[0].files = (Dwarf_Files *) -1l;
10759 : :
10760 [ # # ]: 0 : if (cus[0].files != (Dwarf_Files *) -1l)
10761 : 0 : fname = (dwarf_filesrc (cus[0].files, u128_2, NULL, NULL)
10762 [ # # ]: 0 : ?: "???");
10763 : : }
10764 : :
10765 : 0 : fprintf (out, "%*sstart_file %u, [%u] %s\n",
10766 : : level, "", u128, u128_2, fname);
10767 : 0 : ++level;
10768 : 0 : break;
10769 : :
10770 : 0 : case DW_MACINFO_end_file:
10771 : 0 : --level;
10772 : 0 : fprintf (out, "%*send_file\n", level, "");
10773 : : /* Nothing more to do. */
10774 : 0 : break;
10775 : :
10776 : 0 : default:
10777 : : // XXX gcc seems to generate files with a trailing zero.
10778 [ # # # # ]: 0 : if (unlikely (opcode != 0 || readp != readendp))
10779 : 0 : fprintf (out, "%*s*** invalid opcode %u\n", level, "", opcode);
10780 : : break;
10781 : : }
10782 : : }
10783 : : }
10784 : :
10785 : :
10786 : : static void
10787 : 8 : print_debug_macro_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
10788 : : Ebl *ebl,
10789 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
10790 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg,
10791 : : FILE *out)
10792 : : {
10793 : 8 : Elf_Data *data = get_debug_elf_data (dbg, ebl, IDX_debug_macro, scn);
10794 [ + - ]: 8 : if (data == NULL)
10795 : 0 : return;
10796 : :
10797 : 8 : fprintf (out, _("\
10798 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
10799 : : elf_ndxscn (scn), section_name (ebl, shdr),
10800 : 8 : (uint64_t) shdr->sh_offset);
10801 : 8 : fputc ('\n', out);
10802 : :
10803 : : /* Get the source file information for all CUs. Uses same
10804 : : datastructure as macinfo. But uses offset field to directly
10805 : : match .debug_line offset. And just stored in a list. */
10806 : 8 : Dwarf_Off offset;
10807 : 8 : Dwarf_Off ncu = 0;
10808 : 8 : size_t hsize;
10809 : 8 : struct mac_culist *culist = NULL;
10810 [ + + ]: 20 : while (dwarf_nextcu (dbg, offset = ncu, &ncu, &hsize, NULL, NULL, NULL) == 0)
10811 : : {
10812 : 12 : Dwarf_Die cudie;
10813 [ - + ]: 12 : if (dwarf_offdie (dbg, offset + hsize, &cudie) == NULL)
10814 : 0 : continue;
10815 : :
10816 : 12 : Dwarf_Attribute attr;
10817 [ - + ]: 12 : if (dwarf_attr (&cudie, DW_AT_stmt_list, &attr) == NULL)
10818 : 0 : continue;
10819 : :
10820 : 12 : Dwarf_Word lineoff;
10821 [ - + ]: 12 : if (dwarf_formudata (&attr, &lineoff) != 0)
10822 : 0 : continue;
10823 : :
10824 : 12 : struct mac_culist *newp = (struct mac_culist *) alloca (sizeof (*newp));
10825 : 12 : newp->die = cudie;
10826 : 12 : newp->offset = lineoff;
10827 : 12 : newp->files = NULL;
10828 : 12 : newp->next = culist;
10829 : 12 : culist = newp;
10830 : : }
10831 : :
10832 : 8 : const unsigned char *readp = (const unsigned char *) data->d_buf;
10833 : 8 : const unsigned char *readendp = readp + data->d_size;
10834 : :
10835 [ + + ]: 28 : while (readp < readendp)
10836 : : {
10837 : 40 : fprintf (out, _(" Offset: 0x%" PRIx64 "\n"),
10838 : 20 : (uint64_t) (readp - (const unsigned char *) data->d_buf));
10839 : :
10840 : : // Header, 2 byte version, 1 byte flag, optional .debug_line offset,
10841 : : // optional vendor extension macro entry table.
10842 [ - + ]: 20 : if (readp + 2 > readendp)
10843 : : {
10844 : 0 : invalid_data:
10845 : 0 : error (0, 0, _("invalid data"));
10846 : 0 : return;
10847 : : }
10848 [ - + ]: 20 : const uint16_t vers = read_2ubyte_unaligned_inc (dbg, readp);
10849 : 20 : fprintf (out, _(" Version: %" PRIu16 "\n"), vers);
10850 : :
10851 : : // Version 4 is the GNU extension for DWARF4. DWARF5 will use version
10852 : : // 5 when it gets standardized.
10853 [ - + ]: 20 : if (vers != 4 && vers != 5)
10854 : : {
10855 : 0 : fprintf (out, _(" unknown version, cannot parse section\n"));
10856 : 0 : return;
10857 : : }
10858 : :
10859 [ - + ]: 20 : if (readp + 1 > readendp)
10860 : 0 : goto invalid_data;
10861 : 20 : const unsigned char flag = *readp++;
10862 : 20 : fprintf (out, _(" Flag: 0x%" PRIx8), flag);
10863 [ + + ]: 20 : if (flag != 0)
10864 : : {
10865 : 12 : fprintf (out, " (");
10866 [ - + ]: 12 : if ((flag & 0x01) != 0)
10867 : : {
10868 : 0 : fprintf (out, "offset_size");
10869 [ # # ]: 0 : if ((flag & 0xFE) != 0)
10870 : 0 : fprintf (out, ", ");
10871 : : }
10872 [ + - ]: 12 : if ((flag & 0x02) != 0)
10873 : : {
10874 : 12 : fprintf (out, "debug_line_offset");
10875 [ - + ]: 12 : if ((flag & 0xFC) != 0)
10876 : 0 : fprintf (out, ", ");
10877 : : }
10878 [ # # ]: 0 : if ((flag & 0x04) != 0)
10879 : : {
10880 : 0 : fprintf (out, "operands_table");
10881 [ # # ]: 0 : if ((flag & 0xF8) != 0)
10882 : 0 : fprintf (out, ", ");
10883 : : }
10884 [ - + ]: 12 : if ((flag & 0xF8) != 0)
10885 : 0 : fprintf (out, "unknown");
10886 : 12 : fprintf (out, ")");
10887 : : }
10888 : 20 : fprintf (out, "\n");
10889 : :
10890 [ + - ]: 20 : unsigned int offset_len = (flag & 0x01) ? 8 : 4;
10891 : 20 : fprintf (out, _(" Offset length: %" PRIu8 "\n"), offset_len);
10892 : 20 : Dwarf_Off line_offset = -1;
10893 [ + + ]: 20 : if (flag & 0x02)
10894 : : {
10895 [ - + ]: 12 : if (offset_len == 8)
10896 [ # # ]: 0 : line_offset = read_8ubyte_unaligned_inc (dbg, readp);
10897 : : else
10898 [ - + ]: 12 : line_offset = read_4ubyte_unaligned_inc (dbg, readp);
10899 : 12 : fprintf (out, _(" .debug_line offset: 0x%" PRIx64 "\n"),
10900 : : line_offset);
10901 : : }
10902 : :
10903 : 12 : struct mac_culist *cu = NULL;
10904 [ + - ]: 12 : if (line_offset != (Dwarf_Off) -1)
10905 : : {
10906 : : cu = culist;
10907 [ + - + + ]: 16 : while (cu != NULL && line_offset != cu->offset)
10908 : 4 : cu = cu->next;
10909 : : }
10910 : :
10911 [ - + ]: 20 : Dwarf_Off str_offsets_base = str_offsets_base_off (dbg, (cu != NULL
10912 : : ? cu->die.cu
10913 : : : NULL));
10914 : :
10915 : 20 : const unsigned char *vendor[DW_MACRO_hi_user - DW_MACRO_lo_user + 1];
10916 [ - + ]: 20 : memset (vendor, 0, sizeof vendor);
10917 [ - + ]: 20 : if (flag & 0x04)
10918 : : {
10919 : : // 1 byte length, for each item, 1 byte opcode, uleb128 number
10920 : : // of arguments, for each argument 1 byte form code.
10921 [ # # ]: 0 : if (readp + 1 > readendp)
10922 : 0 : goto invalid_data;
10923 : 0 : unsigned int tlen = *readp++;
10924 : 0 : fprintf (out, _(" extension opcode table, %" PRIu8 " items:\n"),
10925 : : tlen);
10926 [ # # ]: 0 : for (unsigned int i = 0; i < tlen; i++)
10927 : : {
10928 [ # # ]: 0 : if (readp + 1 > readendp)
10929 : 0 : goto invalid_data;
10930 : 0 : unsigned int opcode = *readp++;
10931 : 0 : fprintf (out, _(" [%" PRIx8 "]"), opcode);
10932 : 0 : if (opcode < DW_MACRO_lo_user
10933 [ # # ]: 0 : || opcode > DW_MACRO_hi_user)
10934 : 0 : goto invalid_data;
10935 : : // Record the start of description for this vendor opcode.
10936 : : // uleb128 nr args, 1 byte per arg form.
10937 : 0 : vendor[opcode - DW_MACRO_lo_user] = readp;
10938 [ # # ]: 0 : if (readp + 1 > readendp)
10939 : 0 : goto invalid_data;
10940 : 0 : unsigned int args = *readp++;
10941 [ # # ]: 0 : if (args > 0)
10942 : : {
10943 : 0 : fprintf (out, _(" %" PRIu8 " arguments:"), args);
10944 : 0 : while (args > 0)
10945 : : {
10946 [ # # ]: 0 : if (readp + 1 > readendp)
10947 : 0 : goto invalid_data;
10948 : 0 : unsigned int form = *readp++;
10949 : 0 : fprintf (out, " %s", dwarf_form_name (form));
10950 [ # # ]: 0 : if (! libdw_valid_user_form (form))
10951 : 0 : goto invalid_data;
10952 : 0 : args--;
10953 [ # # ]: 0 : if (args > 0)
10954 : 0 : fputc (',', out);
10955 : : }
10956 : : }
10957 : : else
10958 : 0 : fprintf (out, _(" no arguments."));
10959 : 0 : fputc ('\n', out);
10960 : : }
10961 : : }
10962 : 20 : fputc ('\n', out);
10963 : :
10964 : 20 : int level = 1;
10965 [ - + ]: 20 : if (readp + 1 > readendp)
10966 : 0 : goto invalid_data;
10967 : 20 : unsigned int opcode = *readp++;
10968 [ + + ]: 3046 : while (opcode != 0)
10969 : : {
10970 : 3026 : unsigned int u128;
10971 : 3026 : unsigned int u128_2;
10972 : 3026 : const unsigned char *endp;
10973 : 3026 : uint64_t off;
10974 : :
10975 [ + + + - : 3026 : switch (opcode)
+ + + - -
- + + - ]
10976 : : {
10977 : 20 : case DW_MACRO_start_file:
10978 : 20 : get_uleb128 (u128, readp, readendp);
10979 [ - + ]: 20 : if (readp >= readendp)
10980 : 0 : goto invalid_data;
10981 : 20 : get_uleb128 (u128_2, readp, readendp);
10982 : :
10983 : : /* Find the CU DIE that matches this line offset. */
10984 : 20 : const char *fname = "???";
10985 [ - + ]: 20 : if (cu != NULL)
10986 : : {
10987 [ + + ]: 20 : if (cu->files == NULL
10988 [ - + ]: 12 : && dwarf_getsrcfiles (&cu->die, &cu->files,
10989 : : NULL) != 0)
10990 : 0 : cu->files = (Dwarf_Files *) -1l;
10991 : :
10992 [ - + ]: 20 : if (cu->files != (Dwarf_Files *) -1l)
10993 : 20 : fname = (dwarf_filesrc (cu->files, u128_2,
10994 [ - + ]: 20 : NULL, NULL) ?: "???");
10995 : : }
10996 : 20 : fprintf (out, "%*sstart_file %u, [%u] %s\n",
10997 : : level, "", u128, u128_2, fname);
10998 : 20 : ++level;
10999 : 20 : break;
11000 : :
11001 : 20 : case DW_MACRO_end_file:
11002 : 20 : --level;
11003 : 20 : fprintf (out, "%*send_file\n", level, "");
11004 : 20 : break;
11005 : :
11006 : 2 : case DW_MACRO_define:
11007 : 2 : get_uleb128 (u128, readp, readendp);
11008 : 2 : endp = memchr (readp, '\0', readendp - readp);
11009 [ - + ]: 2 : if (endp == NULL)
11010 : 0 : goto invalid_data;
11011 : 2 : fprintf (out, "%*s#define %s, line %u\n",
11012 : : level, "", readp, u128);
11013 : 2 : readp = endp + 1;
11014 : 2 : break;
11015 : :
11016 : 0 : case DW_MACRO_undef:
11017 : 0 : get_uleb128 (u128, readp, readendp);
11018 : 0 : endp = memchr (readp, '\0', readendp - readp);
11019 [ # # ]: 0 : if (endp == NULL)
11020 : 0 : goto invalid_data;
11021 : 0 : fprintf (out, "%*s#undef %s, line %u\n",
11022 : : level, "", readp, u128);
11023 : 0 : readp = endp + 1;
11024 : 0 : break;
11025 : :
11026 : 1414 : case DW_MACRO_define_strp:
11027 : 1414 : get_uleb128 (u128, readp, readendp);
11028 [ - + ]: 1414 : if (readp + offset_len > readendp)
11029 : 0 : goto invalid_data;
11030 [ - + ]: 1414 : if (offset_len == 8)
11031 [ # # ]: 0 : off = read_8ubyte_unaligned_inc (dbg, readp);
11032 : : else
11033 [ - + ]: 1414 : off = read_4ubyte_unaligned_inc (dbg, readp);
11034 : 1414 : fprintf (out, "%*s#define %s, line %u (indirect)\n",
11035 : : level, "", dwarf_getstring (dbg, off, NULL), u128);
11036 : 1414 : break;
11037 : :
11038 : 2 : case DW_MACRO_undef_strp:
11039 : 2 : get_uleb128 (u128, readp, readendp);
11040 [ - + ]: 2 : if (readp + offset_len > readendp)
11041 : 0 : goto invalid_data;
11042 [ - + ]: 2 : if (offset_len == 8)
11043 [ # # ]: 0 : off = read_8ubyte_unaligned_inc (dbg, readp);
11044 : : else
11045 [ - + ]: 2 : off = read_4ubyte_unaligned_inc (dbg, readp);
11046 : 2 : fprintf (out, "%*s#undef %s, line %u (indirect)\n",
11047 : : level, "", dwarf_getstring (dbg, off, NULL), u128);
11048 : 2 : break;
11049 : :
11050 : 12 : case DW_MACRO_import:
11051 [ - + ]: 12 : if (readp + offset_len > readendp)
11052 : 0 : goto invalid_data;
11053 [ - + ]: 12 : if (offset_len == 8)
11054 [ # # ]: 0 : off = read_8ubyte_unaligned_inc (dbg, readp);
11055 : : else
11056 [ - + ]: 12 : off = read_4ubyte_unaligned_inc (dbg, readp);
11057 : 12 : fprintf (out, "%*s#include offset 0x%" PRIx64 "\n",
11058 : : level, "", off);
11059 : 12 : break;
11060 : :
11061 : 0 : case DW_MACRO_define_sup:
11062 : 0 : get_uleb128 (u128, readp, readendp);
11063 : 0 : fprintf (out, "%*s#define ", level, "");
11064 : 0 : readp = print_form_data (dbg, DW_FORM_strp_sup,
11065 : : readp, readendp, offset_len,
11066 : : str_offsets_base, out);
11067 : 0 : fprintf (out, ", line %u (sup)\n", u128);
11068 : 0 : break;
11069 : :
11070 : 0 : case DW_MACRO_undef_sup:
11071 : 0 : get_uleb128 (u128, readp, readendp);
11072 : 0 : fprintf (out, "%*s#undef ", level, "");
11073 : 0 : readp = print_form_data (dbg, DW_FORM_strp_sup,
11074 : : readp, readendp, offset_len,
11075 : : str_offsets_base, out);
11076 : 0 : fprintf (out, ", line %u (sup)\n", u128);
11077 : 0 : break;
11078 : :
11079 : 0 : case DW_MACRO_import_sup:
11080 [ # # ]: 0 : if (readp + offset_len > readendp)
11081 : 0 : goto invalid_data;
11082 [ # # ]: 0 : if (offset_len == 8)
11083 [ # # ]: 0 : off = read_8ubyte_unaligned_inc (dbg, readp);
11084 : : else
11085 [ # # ]: 0 : off = read_4ubyte_unaligned_inc (dbg, readp);
11086 : : // XXX Needs support for reading from supplementary object file.
11087 : 0 : fprintf (out, "%*s#include offset 0x%" PRIx64 " (sup)\n",
11088 : : level, "", off);
11089 : 0 : break;
11090 : :
11091 : 1552 : case DW_MACRO_define_strx:
11092 : 1552 : get_uleb128 (u128, readp, readendp);
11093 : 1552 : fprintf (out, "%*s#define ", level, "");
11094 : 1552 : readp = print_form_data (dbg, DW_FORM_strx,
11095 : : readp, readendp, offset_len,
11096 : : str_offsets_base, out);
11097 : 1552 : fprintf (out, ", line %u (strx)\n", u128);
11098 : 1552 : break;
11099 : :
11100 : 4 : case DW_MACRO_undef_strx:
11101 : 4 : get_uleb128 (u128, readp, readendp);
11102 : 4 : fprintf (out, "%*s#undef ", level, "");
11103 : 4 : readp = print_form_data (dbg, DW_FORM_strx,
11104 : : readp, readendp, offset_len,
11105 : : str_offsets_base, out);
11106 : 4 : fprintf (out, ", line %u (strx)\n", u128);
11107 : 4 : break;
11108 : :
11109 : : default:
11110 : 0 : fprintf (out, "%*svendor opcode 0x%" PRIx8, level, "", opcode);
11111 [ # # ]: 0 : if (opcode < DW_MACRO_lo_user
11112 : : || opcode > DW_MACRO_lo_user
11113 [ # # ]: 0 : || vendor[opcode - DW_MACRO_lo_user] == NULL)
11114 : 0 : goto invalid_data;
11115 : :
11116 : 0 : const unsigned char *op_desc;
11117 : 0 : op_desc = vendor[opcode - DW_MACRO_lo_user];
11118 : :
11119 : : // Just skip the arguments, we cannot really interpret them,
11120 : : // but print as much as we can.
11121 : 0 : unsigned int args = *op_desc++;
11122 [ # # # # ]: 0 : while (args > 0 && readp < readendp)
11123 : : {
11124 : 0 : unsigned int form = *op_desc++;
11125 : 0 : readp = print_form_data (dbg, form, readp, readendp,
11126 : : offset_len, str_offsets_base, out);
11127 : 0 : args--;
11128 [ # # ]: 0 : if (args > 0)
11129 : 0 : fprintf (out, ", ");
11130 : : }
11131 : 0 : fputc ('\n', out);
11132 : : }
11133 : :
11134 [ - + ]: 3026 : if (readp + 1 > readendp)
11135 : 0 : goto invalid_data;
11136 : 3026 : opcode = *readp++;
11137 [ + + ]: 3026 : if (opcode == 0)
11138 : 20 : fputc ('\n', out);
11139 : : }
11140 : : }
11141 : : }
11142 : :
11143 : :
11144 : : typedef struct {
11145 : : int n;
11146 : : FILE *out;
11147 : : } pubnames_arg;
11148 : :
11149 : : /* Callback for printing global names. */
11150 : : static int
11151 : 68 : print_pubnames (Dwarf *dbg __attribute__ ((unused)), Dwarf_Global *global,
11152 : : void *arg)
11153 : : {
11154 : 68 : pubnames_arg *p = (pubnames_arg *) arg;
11155 : :
11156 : 136 : fprintf (p->out, _(" [%5d] DIE offset: %6" PRId64
11157 : : ", CU DIE offset: %6" PRId64 ", name: %s\n"),
11158 : 68 : (p->n)++, global->die_offset, global->cu_offset, global->name);
11159 : :
11160 : 68 : return 0;
11161 : : }
11162 : :
11163 : :
11164 : : /* Print the known exported symbols in the DWARF section '.debug_pubnames'. */
11165 : : static void
11166 : 16 : print_debug_pubnames_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
11167 : : Ebl *ebl,
11168 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
11169 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg,
11170 : : FILE *out)
11171 : : {
11172 : : /* Check section actually exists. */
11173 [ - + ]: 16 : if (get_debug_elf_data (dbg, ebl, IDX_debug_pubnames, scn) == NULL)
11174 : 0 : return;
11175 : :
11176 : 16 : fprintf (out, _("\nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
11177 : : elf_ndxscn (scn), section_name (ebl, shdr),
11178 : 16 : (uint64_t) shdr->sh_offset);
11179 : :
11180 : 16 : pubnames_arg arg = { 0, out };
11181 : 16 : (void) dwarf_getpubnames (dbg, print_pubnames, &arg, 0);
11182 : : }
11183 : :
11184 : : /* Print the content of the DWARF string section '.debug_str'
11185 : : or 'debug_line_str'. */
11186 : : static void
11187 : 134 : print_debug_str_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
11188 : : Ebl *ebl,
11189 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
11190 : : Elf_Scn *scn, GElf_Shdr *shdr,
11191 : : Dwarf *dbg __attribute__ ((unused)),
11192 : : FILE *out)
11193 : : {
11194 : 134 : const char *name = section_name (ebl, shdr);
11195 [ + + ]: 134 : int idx = ((name != NULL && strstr (name, "debug_line_str") != NULL)
11196 : : ? IDX_debug_line_str : IDX_debug_str);
11197 : 134 : Elf_Data *data = get_debug_elf_data (dbg, ebl, idx, scn);
11198 [ + - ]: 134 : if (data == NULL)
11199 : : return;
11200 : :
11201 : 134 : const size_t sh_size = data->d_size;
11202 : :
11203 : : /* Compute floor(log16(shdr->sh_size)). */
11204 : 134 : GElf_Addr tmp = sh_size;
11205 : 134 : int digits = 1;
11206 [ + + ]: 466 : while (tmp >= 16)
11207 : : {
11208 : 332 : ++digits;
11209 : 332 : tmp >>= 4;
11210 : : }
11211 : 134 : digits = MAX (4, digits);
11212 : :
11213 : 134 : fprintf (out, _("\nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"
11214 : : " %*s String\n"),
11215 : : elf_ndxscn (scn),
11216 : 134 : section_name (ebl, shdr), (uint64_t) shdr->sh_offset,
11217 : : /* TRANS: the debugstr| prefix makes the string unique. */
11218 [ + - ]: 134 : digits + 2, sgettext ("debugstr|Offset"));
11219 : :
11220 : 134 : Dwarf_Off offset = 0;
11221 [ + + ]: 162348 : while (offset < sh_size)
11222 : : {
11223 : 162214 : size_t len;
11224 : 162214 : const char *str = (const char *) data->d_buf + offset;
11225 : 162214 : const char *endp = memchr (str, '\0', sh_size - offset);
11226 [ - + ]: 162214 : if (unlikely (endp == NULL))
11227 : : {
11228 : 0 : fprintf (out, _(" *** error, missing string terminator\n"));
11229 : 0 : break;
11230 : : }
11231 : :
11232 : 162214 : fprintf (out, " [%*" PRIx64 "] \"%s\"\n",
11233 : : digits, (uint64_t) offset, str);
11234 : 162214 : len = endp - str;
11235 : 162214 : offset += len + 1;
11236 : : }
11237 : : }
11238 : :
11239 : : static void
11240 : 12 : print_debug_str_offsets_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
11241 : : Ebl *ebl,
11242 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
11243 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg,
11244 : : FILE *out)
11245 : : {
11246 : 12 : Elf_Data *data = get_debug_elf_data (dbg, ebl, IDX_debug_str_offsets, scn);
11247 [ + - ]: 12 : if (data == NULL)
11248 : : return;
11249 : :
11250 : 12 : fprintf (out, _("\
11251 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
11252 : : elf_ndxscn (scn), section_name (ebl, shdr),
11253 : 12 : (uint64_t) shdr->sh_offset);
11254 : :
11255 [ + - ]: 12 : if (shdr->sh_size == 0)
11256 : : return;
11257 : :
11258 : 12 : size_t idx = 0;
11259 : 12 : sort_listptr (&known_stroffbases, "str_offsets");
11260 : :
11261 : 12 : const unsigned char *start = (const unsigned char *) data->d_buf;
11262 : 12 : const unsigned char *readp = start;
11263 : 12 : const unsigned char *readendp = ((const unsigned char *) data->d_buf
11264 : 12 : + data->d_size);
11265 : :
11266 [ + + ]: 28 : while (readp < readendp)
11267 : : {
11268 : : /* Most string offset tables will have a header. For split
11269 : : dwarf unit GNU DebugFission didn't add one. But they were
11270 : : also only defined for split units (main or skeleton units
11271 : : didn't have indirect strings). So if we don't have a
11272 : : DW_AT_str_offsets_base at all and this is offset zero, then
11273 : : just start printing offsets immediately, if this is a .dwo
11274 : : section. */
11275 : 16 : Dwarf_Off off = (Dwarf_Off) (readp
11276 : 16 : - (const unsigned char *) data->d_buf);
11277 : :
11278 : 16 : fprintf (out, "Table at offset %" PRIx64 " ", off);
11279 : :
11280 [ + + ]: 16 : struct listptr *listptr = get_listptr (&known_stroffbases, idx++);
11281 : 16 : const unsigned char *next_unitp = readendp;
11282 : 8 : uint8_t offset_size;
11283 : 8 : bool has_header;
11284 [ - + ]: 8 : if (listptr == NULL)
11285 : : {
11286 : : /* This can happen for .dwo files. There is only an header
11287 : : in the case this is a version 5 split DWARF file. */
11288 : 8 : Dwarf_CU *cu;
11289 : 8 : uint8_t unit_type;
11290 [ - + ]: 8 : if (dwarf_get_units (dbg, NULL, &cu, NULL, &unit_type,
11291 : : NULL, NULL) != 0)
11292 : : {
11293 : 0 : error (0, 0, "Warning: Cannot find any DWARF unit.");
11294 : : /* Just guess some values. */
11295 : 0 : has_header = false;
11296 : 0 : offset_size = 4;
11297 : : }
11298 [ + - ]: 8 : else if (off == 0
11299 : 8 : && (unit_type == DW_UT_split_type
11300 [ + - ]: 8 : || unit_type == DW_UT_split_compile))
11301 : : {
11302 : 8 : has_header = cu->version > 4;
11303 : 8 : offset_size = cu->offset_size;
11304 : : }
11305 : : else
11306 : : {
11307 : 0 : error (0, 0,
11308 : : "Warning: No CU references .debug_str_offsets after %"
11309 : : PRIx64, off);
11310 : 0 : has_header = cu->version > 4;
11311 : 0 : offset_size = cu->offset_size;
11312 : : }
11313 : 8 : fprintf (out, "\n");
11314 : : }
11315 : : else
11316 : : {
11317 : : /* This must be DWARF5, since GNU DebugFission didn't define
11318 : : DW_AT_str_offsets_base. */
11319 : 8 : has_header = true;
11320 : :
11321 : 8 : Dwarf_Die cudie;
11322 [ - + ]: 8 : if (dwarf_cu_die (listptr->cu, &cudie,
11323 : : NULL, NULL, NULL, NULL,
11324 : : NULL, NULL) == NULL)
11325 : 0 : fprintf (out, "Unknown CU (%s):\n", dwarf_errmsg (-1));
11326 : : else
11327 : 8 : fprintf (out, "for CU [%6" PRIx64 "]:\n", dwarf_dieoffset (&cudie));
11328 : : }
11329 : :
11330 [ + + ]: 16 : if (has_header)
11331 : : {
11332 : 12 : uint64_t unit_length;
11333 : 12 : uint16_t version;
11334 : 12 : uint16_t padding;
11335 : :
11336 [ - + ]: 12 : unit_length = read_4ubyte_unaligned_inc (dbg, readp);
11337 [ - + ]: 12 : if (unlikely (unit_length == 0xffffffff))
11338 : : {
11339 [ # # ]: 0 : if (unlikely (readp > readendp - 8))
11340 : : {
11341 : 0 : invalid_data:
11342 : 0 : error (0, 0, "Invalid data");
11343 : 0 : return;
11344 : : }
11345 [ # # ]: 0 : unit_length = read_8ubyte_unaligned_inc (dbg, readp);
11346 : 0 : offset_size = 8;
11347 : : }
11348 : : else
11349 : : offset_size = 4;
11350 : :
11351 : 12 : fprintf (out, "\n");
11352 : 12 : fprintf (out, _(" Length: %8" PRIu64 "\n"),
11353 : : unit_length);
11354 : 12 : fprintf (out, _(" Offset size: %8" PRIu8 "\n"),
11355 : : offset_size);
11356 : :
11357 : : /* We need at least 2-bytes (version) + 2-bytes (padding) =
11358 : : 4 bytes to complete the header. And this unit cannot go
11359 : : beyond the section data. */
11360 [ + - ]: 12 : if (readp > readendp - 4
11361 [ + - ]: 12 : || unit_length < 4
11362 [ - + ]: 12 : || unit_length > (uint64_t) (readendp - readp))
11363 : 0 : goto invalid_data;
11364 : :
11365 : 12 : next_unitp = readp + unit_length;
11366 : :
11367 [ - + ]: 12 : version = read_2ubyte_unaligned_inc (dbg, readp);
11368 : 12 : fprintf (out, _(" DWARF version: %8" PRIu16 "\n"), version);
11369 : :
11370 [ - + ]: 12 : if (version != 5)
11371 : : {
11372 : 0 : error (0, 0, _("Unknown version"));
11373 : 0 : goto next_unit;
11374 : : }
11375 : :
11376 [ - + ]: 12 : padding = read_2ubyte_unaligned_inc (dbg, readp);
11377 : 12 : fprintf (out, _(" Padding: %8" PRIx16 "\n"), padding);
11378 : :
11379 [ + + ]: 12 : if (listptr != NULL
11380 [ - + ]: 8 : && listptr->offset != (Dwarf_Off) (readp - start))
11381 : : {
11382 : 0 : error (0, 0, "String offsets index doesn't start after header");
11383 : 0 : goto next_unit;
11384 : : }
11385 : :
11386 : 12 : fprintf (out, "\n");
11387 : : }
11388 : :
11389 : 16 : int digits = 1;
11390 : 16 : size_t offsets = (next_unitp - readp) / offset_size;
11391 [ + + ]: 24 : while (offsets >= 10)
11392 : : {
11393 : 8 : ++digits;
11394 : 8 : offsets /= 10;
11395 : : }
11396 : :
11397 : 16 : unsigned int uidx = 0;
11398 : 16 : size_t index_offset = readp - (const unsigned char *) data->d_buf;
11399 : 16 : fprintf (out, " Offsets start at 0x%zx:\n", index_offset);
11400 [ + + ]: 204 : while (readp <= next_unitp - offset_size)
11401 : : {
11402 : 188 : Dwarf_Word offset;
11403 [ + - ]: 188 : if (offset_size == 4)
11404 [ - + ]: 188 : offset = read_4ubyte_unaligned_inc (dbg, readp);
11405 : : else
11406 [ # # ]: 0 : offset = read_8ubyte_unaligned_inc (dbg, readp);
11407 : 188 : const char *str = dwarf_getstring (dbg, offset, NULL);
11408 [ - + ]: 188 : fprintf (out, " [%*u] [%*" PRIx64 "] \"%s\"\n",
11409 : 188 : digits, uidx++, (int) offset_size * 2, offset, str ?: "???");
11410 : : }
11411 : 16 : fprintf (out, "\n");
11412 : :
11413 [ + - ]: 16 : if (readp != next_unitp)
11414 : 0 : error (0, 0, "extra %zd bytes at end of unit",
11415 : 0 : (size_t) (next_unitp - readp));
11416 : :
11417 : 16 : next_unit:
11418 : : readp = next_unitp;
11419 : : }
11420 : : }
11421 : :
11422 : :
11423 : : /* Print the content of the call frame search table section
11424 : : '.eh_frame_hdr'. */
11425 : : static void
11426 : 140 : print_debug_frame_hdr_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
11427 : : Ebl *ebl __attribute__ ((unused)),
11428 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
11429 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg,
11430 : : FILE *out)
11431 : : {
11432 : 140 : fprintf (out, _("\
11433 : : \nCall frame search table section [%2zu] '.eh_frame_hdr':\n"),
11434 : : elf_ndxscn (scn));
11435 : :
11436 : 140 : Elf_Data *data = elf_rawdata (scn, NULL);
11437 : :
11438 [ - + ]: 140 : if (unlikely (data == NULL))
11439 : : {
11440 : 0 : error (0, 0, _("cannot get %s content: %s"),
11441 : : ".eh_frame_hdr", elf_errmsg (-1));
11442 : 0 : return;
11443 : : }
11444 : :
11445 : 140 : const unsigned char *readp = data->d_buf;
11446 : 140 : const unsigned char *const dataend = ((unsigned char *) data->d_buf
11447 : 140 : + data->d_size);
11448 : :
11449 [ - + ]: 140 : if (unlikely (readp + 4 > dataend))
11450 : : {
11451 : 0 : invalid_data:
11452 : 0 : error (0, 0, _("invalid data"));
11453 : 0 : return;
11454 : : }
11455 : :
11456 : 140 : unsigned int version = *readp++;
11457 : 140 : unsigned int eh_frame_ptr_enc = *readp++;
11458 : 140 : unsigned int fde_count_enc = *readp++;
11459 : 140 : unsigned int table_enc = *readp++;
11460 : :
11461 : 140 : fprintf (out, " version: %u\n"
11462 : : " eh_frame_ptr_enc: %#x ",
11463 : : version, eh_frame_ptr_enc);
11464 : 140 : print_encoding_base ("", eh_frame_ptr_enc, out);
11465 : 140 : fprintf (out, " fde_count_enc: %#x ", fde_count_enc);
11466 : 140 : print_encoding_base ("", fde_count_enc, out);
11467 : 140 : fprintf (out, " table_enc: %#x ", table_enc);
11468 : 140 : print_encoding_base ("", table_enc, out);
11469 : :
11470 : 140 : uint64_t eh_frame_ptr = 0;
11471 [ + - ]: 140 : if (eh_frame_ptr_enc != DW_EH_PE_omit)
11472 : : {
11473 : 140 : readp = read_encoded (eh_frame_ptr_enc, readp, dataend, &eh_frame_ptr,
11474 : : dbg);
11475 [ - + ]: 140 : if (unlikely (readp == NULL))
11476 : 0 : goto invalid_data;
11477 : :
11478 : 140 : fprintf (out, " eh_frame_ptr: %#" PRIx64, eh_frame_ptr);
11479 [ + - ]: 140 : if ((eh_frame_ptr_enc & 0x70) == DW_EH_PE_pcrel)
11480 : 140 : fprintf (out, " (offset: %#" PRIx64 ")",
11481 : : /* +4 because of the 4 byte header of the section. */
11482 : 140 : (uint64_t) shdr->sh_offset + 4 + eh_frame_ptr);
11483 : :
11484 : 140 : fputc ('\n', out);
11485 : : }
11486 : :
11487 : 140 : uint64_t fde_count = 0;
11488 [ + - ]: 140 : if (fde_count_enc != DW_EH_PE_omit)
11489 : : {
11490 : 140 : readp = read_encoded (fde_count_enc, readp, dataend, &fde_count, dbg);
11491 [ - + ]: 140 : if (unlikely (readp == NULL))
11492 : 0 : goto invalid_data;
11493 : :
11494 : 140 : fprintf (out, " fde_count: %" PRIu64 "\n", fde_count);
11495 : : }
11496 : :
11497 [ + - + - ]: 140 : if (fde_count == 0 || table_enc == DW_EH_PE_omit)
11498 : : return;
11499 : :
11500 : 140 : fputs (" Table:\n", out);
11501 : :
11502 : : /* Optimize for the most common case. */
11503 [ + - ]: 140 : if (table_enc == (DW_EH_PE_datarel | DW_EH_PE_sdata4))
11504 [ + + ]: 24214 : while (fde_count > 0 && readp + 8 <= dataend)
11505 : : {
11506 [ - + ]: 24074 : int32_t initial_location = read_4sbyte_unaligned_inc (dbg, readp);
11507 : 24074 : uint64_t initial_offset = ((uint64_t) shdr->sh_offset
11508 : 24074 : + (int64_t) initial_location);
11509 [ - + ]: 24074 : int32_t address = read_4sbyte_unaligned_inc (dbg, readp);
11510 : : // XXX Possibly print symbol name or section offset for initial_offset
11511 : 24074 : fprintf (out, " %#" PRIx32 " (offset: %#6" PRIx64 ") -> %#" PRIx32
11512 : : " fde=[%6" PRIx64 "]\n",
11513 : : initial_location, initial_offset,
11514 : 24074 : address, address - (eh_frame_ptr + 4));
11515 : : }
11516 : : else
11517 : 140 : while (0 && readp < dataend)
11518 : : {
11519 : :
11520 : 140 : }
11521 : : }
11522 : :
11523 : :
11524 : : /* Print the content of the exception handling table section
11525 : : '.eh_frame_hdr'. */
11526 : : static void
11527 : 0 : print_debug_exception_table (Dwfl_Module *dwflmod __attribute__ ((unused)),
11528 : : Ebl *ebl __attribute__ ((unused)),
11529 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
11530 : : Elf_Scn *scn,
11531 : : GElf_Shdr *shdr __attribute__ ((unused)),
11532 : : Dwarf *dbg __attribute__ ((unused)),
11533 : : FILE *out)
11534 : : {
11535 : 0 : fprintf (out, _("\
11536 : : \nException handling table section [%2zu] '.gcc_except_table':\n"),
11537 : : elf_ndxscn (scn));
11538 : :
11539 : 0 : Elf_Data *data = elf_rawdata (scn, NULL);
11540 : :
11541 [ # # ]: 0 : if (unlikely (data == NULL))
11542 : : {
11543 : 0 : error (0, 0, _("cannot get %s content: %s"),
11544 : : ".gcc_except_table", elf_errmsg (-1));
11545 : 0 : return;
11546 : : }
11547 : :
11548 : 0 : const unsigned char *readp = data->d_buf;
11549 : 0 : const unsigned char *const dataend = readp + data->d_size;
11550 : :
11551 [ # # ]: 0 : if (unlikely (readp + 1 > dataend))
11552 : : {
11553 : 0 : invalid_data:
11554 : 0 : error (0, 0, _("invalid data"));
11555 : 0 : return;
11556 : : }
11557 : 0 : unsigned int lpstart_encoding = *readp++;
11558 : 0 : fprintf (out, _(" LPStart encoding: %#x "), lpstart_encoding);
11559 : 0 : print_encoding_base ("", lpstart_encoding, out);
11560 [ # # ]: 0 : if (lpstart_encoding != DW_EH_PE_omit)
11561 : : {
11562 : 0 : uint64_t lpstart;
11563 : 0 : readp = read_encoded (lpstart_encoding, readp, dataend, &lpstart, dbg);
11564 : 0 : fprintf (out, " LPStart: %#" PRIx64 "\n", lpstart);
11565 : : }
11566 : :
11567 [ # # ]: 0 : if (unlikely (readp + 1 > dataend))
11568 : 0 : goto invalid_data;
11569 : 0 : unsigned int ttype_encoding = *readp++;
11570 : 0 : fprintf (out, _(" TType encoding: %#x "), ttype_encoding);
11571 : 0 : print_encoding_base ("", ttype_encoding, out);
11572 : 0 : const unsigned char *ttype_base = NULL;
11573 [ # # ]: 0 : if (ttype_encoding != DW_EH_PE_omit)
11574 : : {
11575 : 0 : unsigned int ttype_base_offset;
11576 [ # # ]: 0 : if (readp >= dataend)
11577 : 0 : goto invalid_data;
11578 : 0 : get_uleb128 (ttype_base_offset, readp, dataend);
11579 : 0 : fprintf (out, " TType base offset: %#x\n", ttype_base_offset);
11580 [ # # ]: 0 : if ((size_t) (dataend - readp) > ttype_base_offset)
11581 : 0 : ttype_base = readp + ttype_base_offset;
11582 : : }
11583 : :
11584 [ # # ]: 0 : if (unlikely (readp + 1 > dataend))
11585 : 0 : goto invalid_data;
11586 : 0 : unsigned int call_site_encoding = *readp++;
11587 : 0 : fprintf (out, _(" Call site encoding: %#x "), call_site_encoding);
11588 : 0 : print_encoding_base ("", call_site_encoding, out);
11589 : 0 : unsigned int call_site_table_len;
11590 [ # # ]: 0 : if (readp >= dataend)
11591 : 0 : goto invalid_data;
11592 : 0 : get_uleb128 (call_site_table_len, readp, dataend);
11593 : :
11594 : 0 : const unsigned char *const action_table = readp + call_site_table_len;
11595 [ # # ]: 0 : if (unlikely (action_table > dataend))
11596 : 0 : goto invalid_data;
11597 : : unsigned int u = 0;
11598 : : unsigned int max_action = 0;
11599 [ # # ]: 0 : while (readp < action_table)
11600 : : {
11601 [ # # ]: 0 : if (u == 0)
11602 : 0 : fputs (_("\n Call site table:"), out);
11603 : :
11604 : 0 : uint64_t call_site_start;
11605 : 0 : readp = read_encoded (call_site_encoding, readp, dataend,
11606 : : &call_site_start, dbg);
11607 : 0 : uint64_t call_site_length;
11608 : 0 : readp = read_encoded (call_site_encoding, readp, dataend,
11609 : : &call_site_length, dbg);
11610 : 0 : uint64_t landing_pad;
11611 : 0 : readp = read_encoded (call_site_encoding, readp, dataend,
11612 : : &landing_pad, dbg);
11613 : 0 : unsigned int action;
11614 [ # # ]: 0 : if (readp >= dataend)
11615 : 0 : goto invalid_data;
11616 : 0 : get_uleb128 (action, readp, dataend);
11617 : 0 : max_action = MAX (action, max_action);
11618 : 0 : fprintf (out, _(" [%4u] Call site start: %#" PRIx64 "\n"
11619 : : " Call site length: %" PRIu64 "\n"
11620 : : " Landing pad: %#" PRIx64 "\n"
11621 : : " Action: %u\n"),
11622 : : u++, call_site_start, call_site_length, landing_pad, action);
11623 : : }
11624 [ # # ]: 0 : if (readp != action_table)
11625 : 0 : goto invalid_data;
11626 : :
11627 : 0 : unsigned int max_ar_filter = 0;
11628 [ # # ]: 0 : if (max_action > 0)
11629 : : {
11630 : 0 : fputs ("\n Action table:", out);
11631 : :
11632 : 0 : size_t maxdata = (size_t) (dataend - action_table);
11633 [ # # # # ]: 0 : if (max_action > maxdata || maxdata - max_action < 1)
11634 : : {
11635 : 0 : invalid_action_table:
11636 : 0 : fputs (_(" <INVALID DATA>\n"), stdout);
11637 : 0 : return;
11638 : : }
11639 : :
11640 : 0 : const unsigned char *const action_table_end
11641 : 0 : = action_table + max_action + 1;
11642 : :
11643 : 0 : u = 0;
11644 : 0 : do
11645 : : {
11646 : 0 : int ar_filter;
11647 : 0 : get_sleb128 (ar_filter, readp, action_table_end);
11648 [ # # ]: 0 : if (ar_filter > 0 && (unsigned int) ar_filter > max_ar_filter)
11649 : 0 : max_ar_filter = ar_filter;
11650 : 0 : int ar_disp;
11651 [ # # ]: 0 : if (readp >= action_table_end)
11652 : 0 : goto invalid_action_table;
11653 : 0 : get_sleb128 (ar_disp, readp, action_table_end);
11654 : :
11655 : 0 : fprintf (out, " [%4u] ar_filter: % d\n"
11656 : : " ar_disp: % -5d",
11657 : : u, ar_filter, ar_disp);
11658 [ # # ]: 0 : if (abs (ar_disp) & 1)
11659 : 0 : fprintf (out, " -> [%4u]\n", u + (ar_disp + 1) / 2);
11660 [ # # ]: 0 : else if (ar_disp != 0)
11661 : 0 : fputs (" -> ???", out);
11662 : : else
11663 : 0 : fputc ('\n', out);
11664 : 0 : ++u;
11665 : : }
11666 [ # # ]: 0 : while (readp < action_table_end);
11667 : : }
11668 : :
11669 [ # # ]: 0 : if (max_ar_filter > 0 && ttype_base != NULL)
11670 : : {
11671 : 0 : unsigned char dsize;
11672 : 0 : fputs ("\n TType table:", out);
11673 : :
11674 : : // XXX Not *4, size of encoding;
11675 [ # # ]: 0 : switch (ttype_encoding & 7)
11676 : : {
11677 : : case DW_EH_PE_udata2:
11678 : : case DW_EH_PE_sdata2:
11679 : : dsize = 2;
11680 : : break;
11681 : : case DW_EH_PE_udata4:
11682 : : case DW_EH_PE_sdata4:
11683 : : dsize = 4;
11684 : : break;
11685 : : case DW_EH_PE_udata8:
11686 : : case DW_EH_PE_sdata8:
11687 : : dsize = 8;
11688 : : break;
11689 : 0 : default:
11690 : 0 : dsize = 0;
11691 : 0 : error (1, 0, _("invalid TType encoding"));
11692 : : }
11693 : :
11694 : 0 : if (max_ar_filter
11695 [ # # ]: 0 : > (size_t) (ttype_base - (const unsigned char *) data->d_buf) / dsize)
11696 : 0 : goto invalid_data;
11697 : :
11698 : 0 : readp = ttype_base - max_ar_filter * dsize;
11699 : 0 : do
11700 : : {
11701 : 0 : uint64_t ttype;
11702 : 0 : readp = read_encoded (ttype_encoding, readp, ttype_base, &ttype,
11703 : : dbg);
11704 : 0 : fprintf (out, " [%4u] %#" PRIx64 "\n", max_ar_filter--, ttype);
11705 : : }
11706 [ # # ]: 0 : while (readp < ttype_base);
11707 : : }
11708 : : }
11709 : :
11710 : : /* Print the content of the '.gdb_index' section.
11711 : : http://sourceware.org/gdb/current/onlinedocs/gdb/Index-Section-Format.html
11712 : : */
11713 : : static void
11714 : 8 : print_gdb_index_section (Dwfl_Module *dwflmod, Ebl *ebl,
11715 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
11716 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg,
11717 : : FILE *out)
11718 : : {
11719 : 8 : fprintf (out, _("\nGDB section [%2zu] '%s' at offset %#" PRIx64
11720 : : " contains %" PRId64 " bytes :\n"),
11721 : : elf_ndxscn (scn), section_name (ebl, shdr),
11722 : 8 : (uint64_t) shdr->sh_offset, (uint64_t) shdr->sh_size);
11723 : :
11724 : 8 : Elf_Data *data = elf_rawdata (scn, NULL);
11725 : :
11726 [ - + ]: 8 : if (unlikely (data == NULL))
11727 : : {
11728 : 0 : error (0, 0, _("cannot get %s content: %s"),
11729 : : ".gdb_index", elf_errmsg (-1));
11730 : 4 : return;
11731 : : }
11732 : :
11733 : : // .gdb_index is always in little endian.
11734 : 8 : Dwarf dummy_dbg = { .other_byte_order = MY_ELFDATA != ELFDATA2LSB };
11735 : 8 : dbg = &dummy_dbg;
11736 : :
11737 : 8 : const unsigned char *readp = data->d_buf;
11738 : 8 : const unsigned char *const dataend = readp + data->d_size;
11739 : :
11740 [ - + ]: 8 : if (unlikely (readp + 4 > dataend))
11741 : : {
11742 : 0 : invalid_data:
11743 : 0 : error (0, 0, _("invalid data"));
11744 : 0 : return;
11745 : : }
11746 : :
11747 : 8 : int32_t vers = read_4ubyte_unaligned (dbg, readp);
11748 : 8 : fprintf (out, _(" Version: %" PRId32 "\n"), vers);
11749 : :
11750 : : // The only difference between version 4 and version 5 is the
11751 : : // hash used for generating the table. Version 6 contains symbols
11752 : : // for inlined functions, older versions didn't. Version 7 adds
11753 : : // symbol kinds. Version 8 just indicates that it correctly includes
11754 : : // TUs for symbols. Version 9 adds shortcut table for information
11755 : : // regarding the main function.
11756 [ - + ]: 8 : if (vers < 4 || vers > 9)
11757 : : {
11758 : 0 : fprintf (out, _(" unknown version, cannot parse section\n"));
11759 : 0 : return;
11760 : : }
11761 : :
11762 : 8 : readp += 4;
11763 [ - + ]: 8 : if (unlikely (readp + 4 > dataend))
11764 : 0 : goto invalid_data;
11765 : :
11766 : 8 : uint32_t cu_off = read_4ubyte_unaligned (dbg, readp);
11767 : 8 : fprintf (out, _(" CU offset: %#" PRIx32 "\n"), cu_off);
11768 : :
11769 : 8 : readp += 4;
11770 [ - + ]: 8 : if (unlikely (readp + 4 > dataend))
11771 : 0 : goto invalid_data;
11772 : :
11773 : 8 : uint32_t tu_off = read_4ubyte_unaligned (dbg, readp);
11774 : 8 : fprintf (out, _(" TU offset: %#" PRIx32 "\n"), tu_off);
11775 : :
11776 : 8 : readp += 4;
11777 [ - + ]: 8 : if (unlikely (readp + 4 > dataend))
11778 : 0 : goto invalid_data;
11779 : :
11780 : 8 : uint32_t addr_off = read_4ubyte_unaligned (dbg, readp);
11781 : 8 : fprintf (out, _(" address offset: %#" PRIx32 "\n"), addr_off);
11782 : :
11783 : 8 : readp += 4;
11784 [ - + ]: 8 : if (unlikely (readp + 4 > dataend))
11785 : 0 : goto invalid_data;
11786 : :
11787 : 8 : uint32_t sym_off = read_4ubyte_unaligned (dbg, readp);
11788 : 8 : fprintf (out, _(" symbol offset: %#" PRIx32 "\n"), sym_off);
11789 : :
11790 : 8 : readp += 4;
11791 [ - + ]: 8 : if (unlikely (readp + 4 > dataend))
11792 : 0 : goto invalid_data;
11793 : :
11794 : 8 : uint32_t shortcut_off = 0;
11795 [ + + ]: 8 : if (vers >= 9)
11796 : : {
11797 : 4 : shortcut_off = read_4ubyte_unaligned (dbg, readp);
11798 : 4 : fprintf (out, _(" shortcut offset: %#" PRIx32 "\n"), shortcut_off);
11799 : :
11800 : 4 : readp += 4;
11801 [ - + ]: 4 : if (unlikely (readp + 4 > dataend))
11802 : 0 : goto invalid_data;
11803 : : }
11804 : :
11805 : 8 : uint32_t const_off = read_4ubyte_unaligned (dbg, readp);
11806 : 8 : fprintf (out, _(" constant offset: %#" PRIx32 "\n"), const_off);
11807 : :
11808 [ - + ]: 8 : if (unlikely ((size_t) (dataend - (const unsigned char *) data->d_buf)
11809 : : < const_off))
11810 : 0 : goto invalid_data;
11811 : :
11812 : 8 : readp = data->d_buf + cu_off;
11813 : :
11814 : 8 : const unsigned char *nextp = data->d_buf + tu_off;
11815 [ - + ]: 8 : if (tu_off >= data->d_size)
11816 : 0 : goto invalid_data;
11817 : :
11818 : 8 : size_t cu_nr = (nextp - readp) / 16;
11819 : :
11820 : 8 : fprintf (out, _("\n CU list at offset %#" PRIx32
11821 : : " contains %zu entries:\n"),
11822 : : cu_off, cu_nr);
11823 : :
11824 : 8 : size_t n = 0;
11825 [ + - + + ]: 22 : while (dataend - readp >= 16 && n < cu_nr)
11826 : : {
11827 : 14 : uint64_t off = read_8ubyte_unaligned (dbg, readp);
11828 : 14 : readp += 8;
11829 : :
11830 : 14 : uint64_t len = read_8ubyte_unaligned (dbg, readp);
11831 : 14 : readp += 8;
11832 : :
11833 : 14 : fprintf (out, " [%4zu] start: %0#8" PRIx64
11834 : : ", length: %5" PRIu64 "\n", n, off, len);
11835 : 14 : n++;
11836 : : }
11837 : :
11838 : 8 : readp = data->d_buf + tu_off;
11839 : 8 : nextp = data->d_buf + addr_off;
11840 [ - + ]: 8 : if (addr_off >= data->d_size)
11841 : 0 : goto invalid_data;
11842 : :
11843 : 8 : size_t tu_nr = (nextp - readp) / 24;
11844 : :
11845 : 8 : fprintf (out, _("\n TU list at offset %#" PRIx32
11846 : : " contains %zu entries:\n"),
11847 : : tu_off, tu_nr);
11848 : :
11849 : 8 : n = 0;
11850 [ + - + + ]: 14 : while (dataend - readp >= 24 && n < tu_nr)
11851 : : {
11852 : 6 : uint64_t off = read_8ubyte_unaligned (dbg, readp);
11853 : 6 : readp += 8;
11854 : :
11855 : 6 : uint64_t type = read_8ubyte_unaligned (dbg, readp);
11856 : 6 : readp += 8;
11857 : :
11858 : 6 : uint64_t sig = read_8ubyte_unaligned (dbg, readp);
11859 : 6 : readp += 8;
11860 : :
11861 : 6 : fprintf (out, " [%4zu] CU offset: %5" PRId64
11862 : : ", type offset: %5" PRId64
11863 : : ", signature: %0#8" PRIx64 "\n", n, off, type, sig);
11864 : 6 : n++;
11865 : : }
11866 : :
11867 : 8 : readp = data->d_buf + addr_off;
11868 : 8 : nextp = data->d_buf + sym_off;
11869 [ - + ]: 8 : if (sym_off >= data->d_size)
11870 : 0 : goto invalid_data;
11871 : :
11872 : 8 : size_t addr_nr = (nextp - readp) / 20;
11873 : :
11874 : 8 : fprintf (out, _("\n Address list at offset %#" PRIx32
11875 : : " contains %zu entries:\n"),
11876 : : addr_off, addr_nr);
11877 : :
11878 : 8 : n = 0;
11879 [ + - + + ]: 22 : while (dataend - readp >= 20 && n < addr_nr)
11880 : : {
11881 : 14 : uint64_t low = read_8ubyte_unaligned (dbg, readp);
11882 : 14 : readp += 8;
11883 : :
11884 : 14 : uint64_t high = read_8ubyte_unaligned (dbg, readp);
11885 : 14 : readp += 8;
11886 : :
11887 : 14 : uint32_t idx = read_4ubyte_unaligned (dbg, readp);
11888 : 14 : readp += 4;
11889 : :
11890 : 14 : fprintf (out, " [%4zu] ", n);
11891 : 14 : print_dwarf_addr (dwflmod, 8, low, low, out);
11892 : 14 : fprintf (out, "..");
11893 : 14 : print_dwarf_addr (dwflmod, 8, high - 1, high, out);
11894 : 14 : fprintf (out, ", CU index: %5" PRId32 "\n", idx);
11895 : 14 : n++;
11896 : : }
11897 : :
11898 : 8 : const unsigned char *const_start = data->d_buf + const_off;
11899 [ - + ]: 8 : if (const_off > data->d_size)
11900 : 0 : goto invalid_data;
11901 : :
11902 : 8 : const unsigned char *shortcut_start = NULL;
11903 [ + + ]: 8 : if (vers >= 9)
11904 : : {
11905 [ - + ]: 4 : if (shortcut_off >= data->d_size)
11906 : 0 : goto invalid_data;
11907 : :
11908 : 4 : shortcut_start = data->d_buf + shortcut_off;
11909 : 4 : nextp = shortcut_start;
11910 : : }
11911 : : else
11912 : : nextp = const_start;
11913 : :
11914 : 8 : readp = data->d_buf + sym_off;
11915 : 8 : size_t sym_nr = (nextp - readp) / 8;
11916 : :
11917 : 8 : fprintf (out, _("\n Symbol table at offset %#" PRIx32
11918 : : " contains %zu slots:\n"),
11919 : : sym_off, sym_nr);
11920 : :
11921 : 8 : n = 0;
11922 [ + - + + ]: 8200 : while (dataend - readp >= 8 && n < sym_nr)
11923 : : {
11924 : 8192 : uint32_t name = read_4ubyte_unaligned (dbg, readp);
11925 : 8192 : readp += 4;
11926 : :
11927 : 8192 : uint32_t vector = read_4ubyte_unaligned (dbg, readp);
11928 : 8192 : readp += 4;
11929 : :
11930 [ + + ]: 8192 : if (name != 0 || vector != 0)
11931 : : {
11932 : 60 : const unsigned char *sym = const_start + name;
11933 [ + - - + ]: 60 : if (unlikely ((size_t) (dataend - const_start) < name
11934 : : || memchr (sym, '\0', dataend - sym) == NULL))
11935 : 0 : goto invalid_data;
11936 : :
11937 : 60 : fprintf (out, " [%4zu] symbol: %s, CUs: ", n, sym);
11938 : :
11939 : 60 : const unsigned char *readcus = const_start + vector;
11940 [ - + ]: 60 : if (unlikely ((size_t) (dataend - const_start) < vector))
11941 : 0 : goto invalid_data;
11942 : 60 : uint32_t cus = read_4ubyte_unaligned (dbg, readcus);
11943 [ + + ]: 128 : while (cus--)
11944 : : {
11945 : 68 : uint32_t cu_kind, cu, kind;
11946 : 68 : bool is_static;
11947 : 68 : readcus += 4;
11948 [ - + ]: 68 : if (unlikely (readcus + 4 > dataend))
11949 : 0 : goto invalid_data;
11950 : 68 : cu_kind = read_4ubyte_unaligned (dbg, readcus);
11951 : 68 : cu = cu_kind & ((1 << 24) - 1);
11952 : 68 : kind = (cu_kind >> 28) & 7;
11953 : 68 : is_static = cu_kind & (1U << 31);
11954 [ + + ]: 68 : if (cu > cu_nr - 1)
11955 : 10 : fprintf (out, "%" PRId32 "T", cu - (uint32_t) cu_nr);
11956 : : else
11957 : 58 : fprintf (out, "%" PRId32, cu);
11958 [ + + ]: 68 : if (kind != 0)
11959 : : {
11960 : 52 : fprintf (out, " (");
11961 [ + + + - : 52 : switch (kind)
- ]
11962 : : {
11963 : : case 1:
11964 : 20 : fprintf (out, "type");
11965 : 20 : break;
11966 : : case 2:
11967 : 16 : fprintf (out, "var");
11968 : 16 : break;
11969 : : case 3:
11970 : 16 : fprintf (out, "func");
11971 : 16 : break;
11972 : : case 4:
11973 : 0 : fprintf (out, "other");
11974 : 0 : break;
11975 : : default:
11976 : 0 : fprintf (out, "unknown-0x%" PRIx32, kind);
11977 : 0 : break;
11978 : : }
11979 [ + + ]: 68 : fprintf (out, ":%c)", (is_static ? 'S' : 'G'));
11980 : : }
11981 [ + + ]: 68 : if (cus > 0)
11982 : 8 : fprintf (out, ", ");
11983 : : }
11984 : 60 : fprintf (out, "\n");
11985 : : }
11986 : 8192 : n++;
11987 : : }
11988 : :
11989 [ + + ]: 8 : if (vers < 9)
11990 : : return;
11991 : :
11992 [ - + ]: 4 : if (unlikely (shortcut_start == NULL))
11993 : 0 : goto invalid_data;
11994 : :
11995 : 4 : readp = shortcut_start;
11996 : 4 : nextp = const_start;
11997 : 4 : size_t shortcut_nr = (nextp - readp) / 4;
11998 : :
11999 [ - + ]: 4 : if (unlikely (shortcut_nr != 2))
12000 : 0 : goto invalid_data;
12001 : :
12002 : 8 : fprintf (out,
12003 : 4 : _("\nShortcut table at offset %#" PRIx32 " contains %zu slots:\n"),
12004 : : shortcut_off, shortcut_nr);
12005 : :
12006 : 4 : uint32_t lang = read_4ubyte_unaligned (dbg, readp);
12007 : 4 : readp += 4;
12008 : :
12009 : : /* Include the hex number of LANG in the output if the language
12010 : : is unknown. */
12011 : 4 : const char *lang_str = dwarf_lang_string (lang);
12012 : 4 : lang_str = string_or_unknown (lang_str, lang, DW_LANG_lo_user,
12013 : : DW_LANG_hi_user, true);
12014 : :
12015 : 4 : fprintf (out, _("Language of main: %s\n"), lang_str);
12016 : 4 : fprintf (out, _("Name of main: "));
12017 : :
12018 [ + + ]: 4 : if (lang != 0)
12019 : : {
12020 : 2 : uint32_t name = read_4ubyte_unaligned (dbg, readp);
12021 : 2 : readp += 4;
12022 : 2 : const unsigned char *sym = const_start + name;
12023 : :
12024 [ + - - + ]: 2 : if (unlikely ((size_t) (dataend - const_start) < name
12025 : : || memchr (sym, '\0', dataend - sym) == NULL))
12026 : 0 : goto invalid_data;
12027 : :
12028 : 2 : fprintf (out, "%s\n", sym);
12029 : : }
12030 : : else
12031 : 2 : fprintf (out, "<unknown>\n");
12032 : : }
12033 : :
12034 : : /* Returns true and sets split DWARF CU id if there is a split compile
12035 : : unit in the given Dwarf, and no non-split units are found (before it). */
12036 : : static bool
12037 : 396 : is_split_dwarf (Dwarf *dbg, uint64_t *id, Dwarf_CU **split_cu)
12038 : : {
12039 : 396 : Dwarf_CU *cu = NULL;
12040 [ + + ]: 396 : while (dwarf_get_units (dbg, cu, &cu, NULL, NULL, NULL, NULL) == 0)
12041 : : {
12042 : 394 : uint8_t unit_type;
12043 [ + - ]: 394 : if (dwarf_cu_info (cu, NULL, &unit_type, NULL, NULL,
12044 : : id, NULL, NULL) != 0)
12045 : 394 : return false;
12046 : :
12047 [ + + ]: 394 : if (unit_type != DW_UT_split_compile && unit_type != DW_UT_split_type)
12048 : : return false;
12049 : :
12050 : : /* We really only care about the split compile unit, the types
12051 : : should be fine and self sufficient. Also they don't have an
12052 : : id that we can match with a skeleton unit. */
12053 [ + - ]: 18 : if (unit_type == DW_UT_split_compile)
12054 : : {
12055 : 18 : *split_cu = cu;
12056 : 18 : return true;
12057 : : }
12058 : : }
12059 : :
12060 : : return false;
12061 : : }
12062 : :
12063 : : /* Check that there is one and only one Dwfl_Module, return in arg. */
12064 : : static int
12065 : 18 : getone_dwflmod (Dwfl_Module *dwflmod,
12066 : : void **userdata __attribute__ ((unused)),
12067 : : const char *name __attribute__ ((unused)),
12068 : : Dwarf_Addr base __attribute__ ((unused)),
12069 : : void *arg)
12070 : : {
12071 : 18 : Dwfl_Module **m = (Dwfl_Module **) arg;
12072 [ + - ]: 18 : if (*m != NULL)
12073 : : return DWARF_CB_ABORT;
12074 : 18 : *m = dwflmod;
12075 : 18 : return DWARF_CB_OK;
12076 : : }
12077 : :
12078 : : typedef struct {
12079 : : Dwfl_Module *dwflmod;
12080 : : Ebl *ebl;
12081 : : GElf_Ehdr *ehdr;
12082 : : Elf_Scn scn;
12083 : : GElf_Shdr shdr;
12084 : : Dwarf *dbg;
12085 : : FILE *out;
12086 : : void (*fp) (Dwfl_Module *, Ebl *, GElf_Ehdr *,
12087 : : Elf_Scn *, GElf_Shdr *, Dwarf *, FILE *);
12088 : : } job_data;
12089 : :
12090 : : #ifdef USE_LOCKS
12091 : :
12092 : : /* Thread entry point. */
12093 : : static void *
12094 : : do_job (void *data, FILE *out)
12095 : : {
12096 : : job_data *d = (job_data *) data;
12097 : : d->fp (d->dwflmod, d->ebl, d->ehdr, &d->scn, &d->shdr, d->dbg, out);
12098 : : return NULL;
12099 : : }
12100 : : #endif
12101 : :
12102 : : /* If readelf is built with thread safety, then set up JDATA at index IDX
12103 : : and add it to the job queue.
12104 : :
12105 : : If thread safety is not supported or the maximum number of threads is set
12106 : : to 1, then immediately call START_ROUTINE with the given arguments. */
12107 : : static void
12108 : 1094 : schedule_job (job_data jdata[], size_t idx,
12109 : : void (*start_routine) (Dwfl_Module *, Ebl *, GElf_Ehdr *,
12110 : : Elf_Scn *, GElf_Shdr *, Dwarf *, FILE *),
12111 : : Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr, Elf_Scn *scn,
12112 : : GElf_Shdr *shdr, Dwarf *dbg)
12113 : : {
12114 : : #ifdef USE_LOCKS
12115 : : if (max_threads > 1)
12116 : : {
12117 : : /* Add to the job queue. */
12118 : : jdata[idx].dwflmod = dwflmod;
12119 : : jdata[idx].ebl = ebl;
12120 : : jdata[idx].ehdr = ehdr;
12121 : : jdata[idx].scn = *scn;
12122 : : jdata[idx].shdr = *shdr;
12123 : : jdata[idx].dbg = dbg;
12124 : : jdata[idx].fp = start_routine;
12125 : :
12126 : : add_job (do_job, (void *) &jdata[idx]);
12127 : : }
12128 : : else
12129 : : start_routine (dwflmod, ebl, ehdr, scn, shdr, dbg, stdout);
12130 : : #else
12131 : 1094 : (void) jdata; (void) idx;
12132 : :
12133 : 1094 : start_routine (dwflmod, ebl, ehdr, scn, shdr, dbg, stdout);
12134 : : #endif
12135 : 1094 : }
12136 : :
12137 : : static void
12138 : 502 : print_debug (Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr)
12139 : : {
12140 : : /* Used for skeleton file, if necessary for split DWARF. */
12141 : 502 : Dwfl *skel_dwfl = NULL;
12142 : 502 : Dwfl_Module *skel_mod = NULL;
12143 : 502 : char *skel_name = NULL;
12144 : 502 : Dwarf *split_dbg = NULL;
12145 : 502 : Dwarf_CU *split_cu = NULL;
12146 : :
12147 : : /* Before we start the real work get a debug context descriptor. */
12148 : 502 : Dwarf_Addr dwbias;
12149 : 502 : Dwarf *dbg = dwfl_module_getdwarf (dwflmod, &dwbias);
12150 : 502 : Dwarf dummy_dbg =
12151 : : {
12152 : 502 : .elf = ebl->elf,
12153 : 502 : .other_byte_order = MY_ELFDATA != ehdr->e_ident[EI_DATA]
12154 : 502 : };
12155 [ + + ]: 502 : if (dbg == NULL)
12156 : : {
12157 [ - + ]: 106 : if ((print_debug_sections & ~(section_exception|section_frame)) != 0)
12158 : 0 : error (0, 0, _("cannot get debug context descriptor: %s"),
12159 : : dwfl_errmsg (-1));
12160 : : dbg = &dummy_dbg;
12161 : : }
12162 : : else
12163 : : {
12164 : : /* If we are asked about a split dwarf (.dwo) file, use the user
12165 : : provided, or find the corresponding skeleton file. If we got
12166 : : a skeleton file, replace the given dwflmod and dbg, with one
12167 : : derived from the skeleton file to provide enough context. */
12168 : 396 : uint64_t split_id;
12169 [ + + ]: 396 : if (is_split_dwarf (dbg, &split_id, &split_cu))
12170 : : {
12171 [ + - ]: 18 : if (dwarf_skeleton != NULL)
12172 : 18 : skel_name = strdup (dwarf_skeleton);
12173 : : else
12174 : : {
12175 : : /* Replace file.dwo with file.o and see if that matches. */
12176 : 0 : const char *fname;
12177 : 0 : dwfl_module_info (dwflmod, NULL, NULL, NULL, NULL, NULL,
12178 : : &fname, NULL);
12179 [ # # ]: 0 : if (fname != NULL)
12180 : : {
12181 : 0 : size_t flen = strlen (fname);
12182 [ # # # # ]: 0 : if (flen > 4 && strcmp (".dwo", fname + flen - 4) == 0)
12183 : : {
12184 : 0 : skel_name = strdup (fname);
12185 [ # # ]: 0 : if (skel_name != NULL)
12186 : : {
12187 : 0 : skel_name[flen - 3] = 'o';
12188 : 0 : skel_name[flen - 2] = '\0';
12189 : : }
12190 : : }
12191 : : }
12192 : : }
12193 : :
12194 [ - + ]: 18 : if (skel_name != NULL)
12195 : : {
12196 : 18 : int skel_fd = open (skel_name, O_RDONLY);
12197 [ - + ]: 18 : if (skel_fd == -1)
12198 : 0 : fprintf (stderr, "Warning: Couldn't open DWARF skeleton file"
12199 : : " '%s'\n", skel_name);
12200 : : else
12201 : : {
12202 : 18 : skel_dwfl = create_dwfl (skel_fd, skel_name);
12203 : :
12204 : : /* skel_fd was dup'ed by create_dwfl. We can close the
12205 : : original now. */
12206 : 18 : close (skel_fd);
12207 : : }
12208 : :
12209 [ + - ]: 18 : if (skel_dwfl != NULL)
12210 : : {
12211 [ - + ]: 18 : if (dwfl_getmodules (skel_dwfl, &getone_dwflmod,
12212 : : &skel_mod, 0) != 0)
12213 : : {
12214 : 0 : fprintf (stderr, "Warning: Bad DWARF skeleton,"
12215 : : " multiple modules '%s'\n", skel_name);
12216 : 0 : dwfl_end (skel_dwfl);
12217 : 0 : skel_mod = NULL;
12218 : : }
12219 : : }
12220 [ # # ]: 0 : else if (skel_fd != -1)
12221 : 0 : fprintf (stderr, "Warning: Couldn't create skeleton dwfl for"
12222 : : " '%s': %s\n", skel_name, dwfl_errmsg (-1));
12223 : :
12224 [ + - ]: 18 : if (skel_mod != NULL)
12225 : : {
12226 : 18 : Dwarf *skel_dbg = dwfl_module_getdwarf (skel_mod, &dwbias);
12227 [ + - ]: 18 : if (skel_dbg != NULL)
12228 : : {
12229 : : /* First check the skeleton CU DIE, only fetch
12230 : : the split DIE if we know the id matches to
12231 : : not unnecessary search for any split DIEs we
12232 : : don't need. */
12233 : 18 : Dwarf_CU *cu = NULL;
12234 : 18 : while (dwarf_get_units (skel_dbg, cu, &cu,
12235 [ + - ]: 26 : NULL, NULL, NULL, NULL) == 0)
12236 : : {
12237 : 26 : uint8_t unit_type;
12238 : 26 : uint64_t skel_id;
12239 [ + - ]: 26 : if (dwarf_cu_info (cu, NULL, &unit_type, NULL, NULL,
12240 : : &skel_id, NULL, NULL) == 0
12241 [ + - ]: 26 : && unit_type == DW_UT_skeleton
12242 [ + + ]: 26 : && split_id == skel_id)
12243 : : {
12244 : 18 : Dwarf_Die subdie;
12245 [ + - ]: 18 : if (dwarf_cu_info (cu, NULL, NULL, NULL,
12246 : : &subdie,
12247 : : NULL, NULL, NULL) == 0
12248 [ + - ]: 18 : && dwarf_tag (&subdie) != DW_TAG_invalid)
12249 : : {
12250 : 18 : split_dbg = dwarf_cu_getdwarf (subdie.cu);
12251 [ - + ]: 18 : if (split_dbg == NULL)
12252 : 0 : fprintf (stderr,
12253 : : "Warning: Couldn't get split_dbg:"
12254 : : " %s\n", dwarf_errmsg (-1));
12255 : 0 : break;
12256 : : }
12257 : : else
12258 : : {
12259 : : /* Everything matches up, but not
12260 : : according to libdw. Which means
12261 : : the user knew better. So...
12262 : : Terrible hack... We can never
12263 : : destroy the underlying dwfl
12264 : : because it would free the wrong
12265 : : Dwarfs... So we leak memory...*/
12266 [ # # ]: 0 : if (cu->split == NULL
12267 [ # # ]: 0 : && dwarf_skeleton != NULL)
12268 : : {
12269 : 0 : do_not_close_dwfl = true;
12270 : 0 : __libdw_link_skel_split (cu, split_cu);
12271 : 0 : split_dbg = dwarf_cu_getdwarf (split_cu);
12272 : 0 : break;
12273 : : }
12274 : : else
12275 : 0 : fprintf (stderr, "Warning: Couldn't get"
12276 : : " skeleton subdie: %s\n",
12277 : : dwarf_errmsg (-1));
12278 : : }
12279 : : }
12280 : : }
12281 [ - - ]: 18 : if (split_dbg == NULL)
12282 : 0 : fprintf (stderr, "Warning: '%s' didn't contain a skeleton for split id %" PRIx64 "\n", skel_name, split_id);
12283 : : }
12284 : : else
12285 : 0 : fprintf (stderr, "Warning: Couldn't get skeleton DWARF:"
12286 : : " %s\n", dwfl_errmsg (-1));
12287 : : }
12288 : : }
12289 : :
12290 : 0 : if (split_dbg != NULL)
12291 : : {
12292 : 18 : dbg = split_dbg;
12293 : 18 : dwflmod = skel_mod;
12294 : : }
12295 [ # # ]: 0 : else if (skel_name == NULL)
12296 : 0 : fprintf (stderr,
12297 : : "Warning: split DWARF file, but no skeleton found.\n");
12298 : : }
12299 [ + - ]: 378 : else if (dwarf_skeleton != NULL)
12300 : 0 : fprintf (stderr, "Warning: DWARF skeleton given,"
12301 : : " but not a split DWARF file\n");
12302 : : }
12303 : :
12304 : : /* Get the section header string table index. */
12305 : 502 : size_t shstrndx;
12306 [ - + ]: 502 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
12307 : 0 : error_exit (0, _("cannot get section header string table index"));
12308 : :
12309 : 502 : ssize_t num_jobs = 0;
12310 : 502 : job_data *jdata = NULL;
12311 : :
12312 : : /* If the .debug_info section is listed as implicitly required then
12313 : : we must make sure to handle it before handling any other debug
12314 : : section. Various other sections depend on the CU DIEs being
12315 : : scanned (silently) first. */
12316 : 502 : bool implicit_info = (implicit_debug_sections & section_info) != 0;
12317 : 502 : bool explicit_info = (print_debug_sections & section_info) != 0;
12318 [ + + ]: 502 : if (implicit_info)
12319 : : {
12320 : : Elf_Scn *scn = NULL;
12321 [ + - ]: 3132 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
12322 : : {
12323 : 3132 : GElf_Shdr shdr_mem;
12324 : 3132 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
12325 : :
12326 [ + - + + ]: 3132 : if (shdr != NULL && shdr->sh_type == SHT_PROGBITS)
12327 : : {
12328 : 2448 : const char *name = elf_strptr (ebl->elf, shstrndx,
12329 : 1224 : shdr->sh_name);
12330 [ - + ]: 1224 : if (name == NULL)
12331 : 0 : continue;
12332 : :
12333 [ + + ]: 1224 : if (strcmp (name, ".debug_info") == 0
12334 [ + + ]: 1092 : || strcmp (name, ".debug_info.dwo") == 0
12335 [ + + ]: 1074 : || strcmp (name, ".zdebug_info") == 0
12336 [ + - ]: 1062 : || strcmp (name, ".zdebug_info.dwo") == 0
12337 [ + + ]: 1062 : || strcmp (name, ".gnu.debuglto_.debug_info") == 0)
12338 : : {
12339 : 164 : print_debug_info_section (dwflmod, ebl, ehdr,
12340 : : scn, shdr, dbg, stdout);
12341 : 164 : break;
12342 : : }
12343 : : }
12344 : : }
12345 : :
12346 : 164 : print_debug_sections &= ~section_info;
12347 : 164 : implicit_debug_sections &= ~section_info;
12348 : : }
12349 : :
12350 : : /* Look through all the sections for the debugging sections to print. */
12351 : : Elf_Scn *scn = NULL;
12352 [ + + ]: 16590 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
12353 : : {
12354 : 16088 : GElf_Shdr shdr_mem;
12355 : 16088 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
12356 : :
12357 [ + - + + ]: 16088 : if (shdr != NULL && shdr->sh_type == SHT_PROGBITS)
12358 : : {
12359 : 6758 : static const struct
12360 : : {
12361 : : const char *name;
12362 : : enum section_e bitmask;
12363 : : void (*fp) (Dwfl_Module *, Ebl *,
12364 : : GElf_Ehdr *, Elf_Scn *, GElf_Shdr *, Dwarf *, FILE *);
12365 : : } debug_sections[] =
12366 : : {
12367 : : #define NEW_SECTION(name) \
12368 : : { ".debug_" #name, section_##name, print_debug_##name##_section }
12369 : : NEW_SECTION (abbrev),
12370 : : NEW_SECTION (addr),
12371 : : NEW_SECTION (aranges),
12372 : : NEW_SECTION (frame),
12373 : : NEW_SECTION (info),
12374 : : NEW_SECTION (types),
12375 : : NEW_SECTION (line),
12376 : : NEW_SECTION (loc),
12377 : : /* loclists is loc for DWARF5. */
12378 : : { ".debug_loclists", section_loc,
12379 : : print_debug_loclists_section },
12380 : : NEW_SECTION (pubnames),
12381 : : NEW_SECTION (str),
12382 : : /* A DWARF5 specialised debug string section. */
12383 : : { ".debug_line_str", section_str,
12384 : : print_debug_str_section },
12385 : : /* DWARF5 string offsets table. */
12386 : : { ".debug_str_offsets", section_str,
12387 : : print_debug_str_offsets_section },
12388 : : NEW_SECTION (macinfo),
12389 : : NEW_SECTION (macro),
12390 : : NEW_SECTION (ranges),
12391 : : /* rnglists is ranges for DWARF5. */
12392 : : { ".debug_rnglists", section_ranges,
12393 : : print_debug_rnglists_section },
12394 : : { ".eh_frame", section_frame | section_exception,
12395 : : print_debug_frame_section },
12396 : : { ".eh_frame_hdr", section_frame | section_exception,
12397 : : print_debug_frame_hdr_section },
12398 : : { ".gcc_except_table", section_frame | section_exception,
12399 : : print_debug_exception_table },
12400 : : { ".gdb_index", section_gdb_index, print_gdb_index_section }
12401 : : };
12402 : 6758 : const int ndebug_sections = (sizeof (debug_sections)
12403 : : / sizeof (debug_sections[0]));
12404 : 13516 : const char *name = elf_strptr (ebl->elf, shstrndx,
12405 : 6758 : shdr->sh_name);
12406 [ - + ]: 6758 : if (name == NULL)
12407 : 0 : continue;
12408 : :
12409 [ + + ]: 6758 : if (jdata == NULL)
12410 : : {
12411 : 502 : jdata = calloc (ndebug_sections, sizeof (*jdata));
12412 [ - + ]: 502 : if (jdata == NULL)
12413 : 0 : error_exit (0, _("failed to allocate job data"));
12414 : : }
12415 : :
12416 : 6758 : int n;
12417 [ + + ]: 106506 : for (n = 0; n < ndebug_sections; ++n)
12418 : : {
12419 : 103042 : size_t dbglen = strlen (debug_sections[n].name);
12420 : 103042 : size_t scnlen = strlen (name);
12421 [ + + ]: 103042 : if ((strncmp (name, debug_sections[n].name, dbglen) == 0
12422 [ + + ]: 3558 : && (dbglen == scnlen
12423 [ + + ]: 546 : || (scnlen == dbglen + 4
12424 [ + + ]: 466 : && strstr (name, ".dwo") == name + dbglen)))
12425 [ + + + + ]: 99922 : || (name[0] == '.' && name[1] == 'z'
12426 [ + - ]: 1132 : && debug_sections[n].name[1] == 'd'
12427 [ + + ]: 1132 : && strncmp (&name[2], &debug_sections[n].name[1],
12428 : : dbglen - 1) == 0
12429 [ - + ]: 164 : && (scnlen == dbglen + 1
12430 [ # # ]: 0 : || (scnlen == dbglen + 5
12431 [ # # ]: 0 : && strstr (name, ".dwo") == name + dbglen + 1)))
12432 [ + + ]: 99758 : || (scnlen > 14 /* .gnu.debuglto_ prefix. */
12433 [ + + ]: 10170 : && startswith (name, ".gnu.debuglto_")
12434 [ + + ]: 72 : && strcmp (&name[14], debug_sections[n].name) == 0)
12435 : : )
12436 : : {
12437 : 3294 : if (((print_debug_sections | implicit_debug_sections)
12438 [ + + ]: 3294 : & debug_sections[n].bitmask))
12439 : 1094 : schedule_job (jdata, num_jobs++, debug_sections[n].fp,
12440 : : dwflmod, ebl, ehdr, scn, shdr, dbg);
12441 : :
12442 [ - + ]: 3294 : assert (num_jobs <= ndebug_sections);
12443 : : break;
12444 : : }
12445 : : }
12446 : : }
12447 : : }
12448 : :
12449 : : #ifdef USE_LOCKS
12450 : : /* If max_threads <= 1, then jobs were immediately run in schedule_job. */
12451 : : if (max_threads > 1)
12452 : : run_jobs (max_threads);
12453 : : #endif
12454 : :
12455 : 502 : dwfl_end (skel_dwfl);
12456 : 502 : free (skel_name);
12457 : 502 : free (jdata);
12458 : :
12459 : : /* Turn implicit and/or explicit back on in case we go over another file. */
12460 [ + + ]: 502 : if (implicit_info)
12461 : 164 : implicit_debug_sections |= section_info;
12462 [ + + ]: 502 : if (explicit_info)
12463 : 134 : print_debug_sections |= section_info;
12464 : :
12465 : 502 : reset_listptr (&known_locsptr);
12466 : 502 : reset_listptr (&known_loclistsptr);
12467 : 502 : reset_listptr (&known_rangelistptr);
12468 : 502 : reset_listptr (&known_rnglistptr);
12469 : 502 : reset_listptr (&known_addrbases);
12470 : 502 : reset_listptr (&known_stroffbases);
12471 : 502 : }
12472 : :
12473 : :
12474 : : #define ITEM_INDENT 4
12475 : : #define WRAP_COLUMN 75
12476 : :
12477 : : /* Print "NAME: FORMAT", wrapping when output text would make the line
12478 : : exceed WRAP_COLUMN. Unpadded numbers look better for the core items
12479 : : but this function is also used for registers which should be printed
12480 : : aligned. Fortunately registers output uses fixed fields width (such
12481 : : as %11d) for the alignment.
12482 : :
12483 : : Line breaks should not depend on the particular values although that
12484 : : may happen in some cases of the core items. */
12485 : :
12486 : : static unsigned int
12487 : : __attribute__ ((format (printf, 6, 7)))
12488 : 1642 : print_core_item (unsigned int colno, char sep, unsigned int wrap,
12489 : : size_t name_width, const char *name, const char *format, ...)
12490 : : {
12491 : 1642 : size_t len = strlen (name);
12492 : 1642 : if (name_width < len)
12493 : : name_width = len;
12494 : :
12495 : 1642 : char *out;
12496 : 1642 : va_list ap;
12497 : 1642 : va_start (ap, format);
12498 : 1642 : int out_len = vasprintf (&out, format, ap);
12499 : 1642 : va_end (ap);
12500 [ - + ]: 1642 : if (out_len == -1)
12501 : 0 : error_exit (0, _("memory exhausted"));
12502 : :
12503 : 1642 : size_t n = name_width + sizeof ": " - 1 + out_len;
12504 : :
12505 [ + + ]: 1642 : if (colno == 0)
12506 : : {
12507 : 102 : printf ("%*s", ITEM_INDENT, "");
12508 : 102 : colno = ITEM_INDENT + n;
12509 : : }
12510 [ + + ]: 1540 : else if (colno + 2 + n < wrap)
12511 : : {
12512 : 936 : printf ("%c ", sep);
12513 : 936 : colno += 2 + n;
12514 : : }
12515 : : else
12516 : : {
12517 : 604 : printf ("\n%*s", ITEM_INDENT, "");
12518 : 604 : colno = ITEM_INDENT + n;
12519 : : }
12520 : :
12521 : 1642 : printf ("%s: %*s%s", name, (int) (name_width - len), "", out);
12522 : :
12523 : 1642 : free (out);
12524 : :
12525 : 1642 : return colno;
12526 : : }
12527 : :
12528 : : static const void *
12529 : 1862 : convert (Elf *core, Elf_Type type, uint_fast16_t count,
12530 : : void *value, const void *data, size_t size)
12531 : : {
12532 : 3724 : Elf_Data valuedata =
12533 : : {
12534 : : .d_type = type,
12535 : : .d_buf = value,
12536 [ + + ]: 1862 : .d_size = size ?: gelf_fsize (core, type, count, EV_CURRENT),
12537 : : .d_version = EV_CURRENT,
12538 : : };
12539 : 1862 : Elf_Data indata =
12540 : : {
12541 : : .d_type = type,
12542 : : .d_buf = (void *) data,
12543 : : .d_size = valuedata.d_size,
12544 : : .d_version = EV_CURRENT,
12545 : : };
12546 : :
12547 : 1862 : Elf_Data *d = (gelf_getclass (core) == ELFCLASS32
12548 [ + + ]: 1862 : ? elf32_xlatetom : elf64_xlatetom)
12549 : 1862 : (&valuedata, &indata, elf_getident (core, NULL)[EI_DATA]);
12550 [ - + ]: 1862 : if (d == NULL)
12551 : 0 : error_exit (0, _("cannot convert core note data: %s"),
12552 : : elf_errmsg (-1));
12553 : :
12554 : 1862 : return data + indata.d_size;
12555 : : }
12556 : :
12557 : : typedef uint8_t GElf_Byte;
12558 : :
12559 : : static unsigned int
12560 : 800 : handle_core_item (Elf *core, const GElf_Ehdr *ehdr,
12561 : : const Ebl_Core_Item *item, const void *desc,
12562 : : unsigned int colno, size_t *repeated_size)
12563 : : {
12564 [ + + ]: 800 : uint_fast16_t count = item->count ?: 1;
12565 : : /* Ebl_Core_Item count is always a small number.
12566 : : Make sure the backend didn't put in some large bogus value. */
12567 [ - + ]: 124 : assert (count < 128);
12568 : :
12569 : : #define TYPES \
12570 : : DO_TYPE (BYTE, Byte, "0x%.2" PRIx8, "%" PRId8); \
12571 : : DO_TYPE (HALF, Half, "0x%.4" PRIx16, "%" PRId16); \
12572 : : DO_TYPE (WORD, Word, "0x%.8" PRIx32, "%" PRId32); \
12573 : : DO_TYPE (SWORD, Sword, "%" PRId32, "%" PRId32); \
12574 : : DO_TYPE (XWORD, Xword, "0x%.16" PRIx64, "%" PRId64); \
12575 : : DO_TYPE (SXWORD, Sxword, "%" PRId64, "%" PRId64)
12576 : :
12577 : : #define DO_TYPE(NAME, Name, hex, dec) GElf_##Name Name
12578 : 800 : typedef union { TYPES; } value_t;
12579 : 800 : void *data = alloca (count * sizeof (value_t));
12580 : : #undef DO_TYPE
12581 : :
12582 : : #define DO_TYPE(NAME, Name, hex, dec) \
12583 : : GElf_##Name *value_##Name __attribute__((unused)) = data
12584 : 800 : TYPES;
12585 : : #undef DO_TYPE
12586 : :
12587 : 800 : size_t size = gelf_fsize (core, item->type, count, EV_CURRENT);
12588 : 800 : size_t convsize = size;
12589 [ + + ]: 800 : if (repeated_size != NULL)
12590 : : {
12591 [ + - - + ]: 2 : if (*repeated_size > size && (item->format == 'b' || item->format == 'B'))
12592 : : {
12593 : 0 : data = alloca (*repeated_size);
12594 : 0 : count *= *repeated_size / size;
12595 : 0 : convsize = count * size;
12596 : 0 : *repeated_size -= convsize;
12597 : : }
12598 [ + - + - ]: 2 : else if (item->count != 0 || item->format != '\n')
12599 : 0 : *repeated_size -= size;
12600 : : }
12601 : :
12602 : 800 : convert (core, item->type, count, data, desc + item->offset, convsize);
12603 : :
12604 : 800 : Elf_Type type = item->type;
12605 [ + + ]: 800 : if (type == ELF_T_ADDR)
12606 [ + - ]: 2 : type = gelf_getclass (core) == ELFCLASS32 ? ELF_T_WORD : ELF_T_XWORD;
12607 : :
12608 [ + + + + : 800 : switch (item->format)
+ + + +
- ]
12609 : : {
12610 : 386 : case 'd':
12611 [ - + ]: 386 : assert (count == 1);
12612 [ + + + + : 386 : switch (type)
- + - ]
12613 : : {
12614 : : #define DO_TYPE(NAME, Name, hex, dec) \
12615 : : case ELF_T_##NAME: \
12616 : : colno = print_core_item (colno, ',', WRAP_COLUMN, \
12617 : : 0, item->name, dec, value_##Name[0]); \
12618 : : break
12619 : 386 : TYPES;
12620 : : #undef DO_TYPE
12621 : 0 : default:
12622 : 0 : abort ();
12623 : : }
12624 : : break;
12625 : :
12626 : 222 : case 'x':
12627 [ - + ]: 222 : assert (count == 1);
12628 [ - - + - : 222 : switch (type)
+ - - ]
12629 : : {
12630 : : #define DO_TYPE(NAME, Name, hex, dec) \
12631 : : case ELF_T_##NAME: \
12632 : : colno = print_core_item (colno, ',', WRAP_COLUMN, \
12633 : : 0, item->name, hex, value_##Name[0]); \
12634 : : break
12635 : 222 : TYPES;
12636 : : #undef DO_TYPE
12637 : 0 : default:
12638 : 0 : abort ();
12639 : : }
12640 : : break;
12641 : :
12642 : 44 : case 'b':
12643 : : case 'B':
12644 [ - + ]: 44 : assert (size % sizeof (unsigned int) == 0);
12645 : 44 : unsigned int nbits = count * size * 8;
12646 : 44 : unsigned int pop = 0;
12647 [ + + ]: 112 : for (const unsigned int *i = data; (void *) i < data + count * size; ++i)
12648 : 68 : pop += __builtin_popcount (*i);
12649 : 44 : bool negate = pop > nbits / 2;
12650 : 44 : const unsigned int bias = item->format == 'b';
12651 : :
12652 : 44 : {
12653 [ - + ]: 44 : char printed[(negate ? nbits - pop : pop) * 16 + 1];
12654 : 44 : char *p = printed;
12655 : 44 : *p = '\0';
12656 : :
12657 : 44 : if (BYTE_ORDER != LITTLE_ENDIAN && size > sizeof (unsigned int))
12658 : : {
12659 : : assert (size == sizeof (unsigned int) * 2);
12660 : : for (unsigned int *i = data;
12661 : : (void *) i < data + count * size; i += 2)
12662 : : {
12663 : : unsigned int w = i[1];
12664 : : i[1] = i[0];
12665 : : i[0] = w;
12666 : : }
12667 : : }
12668 : :
12669 : 44 : unsigned int lastbit = 0;
12670 : 44 : unsigned int run = 0;
12671 : 44 : for (const unsigned int *i = data;
12672 [ + + ]: 112 : (void *) i < data + count * size; ++i)
12673 : : {
12674 : 68 : unsigned int bit = ((void *) i - data) * 8;
12675 [ - + ]: 68 : unsigned int w = negate ? ~*i : *i;
12676 [ - + ]: 68 : while (w != 0)
12677 : : {
12678 : : /* Note that a right shift equal to (or greater than)
12679 : : the number of bits of w is undefined behaviour. In
12680 : : particular when the least significant bit is bit 32
12681 : : (w = 0x8000000) then w >>= n is undefined. So
12682 : : explicitly handle that case separately. */
12683 : 0 : unsigned int n = ffs (w);
12684 [ # # ]: 0 : if (n < sizeof (w) * 8)
12685 : 0 : w >>= n;
12686 : : else
12687 : : w = 0;
12688 : 0 : bit += n;
12689 : :
12690 [ # # # # ]: 0 : if (lastbit != 0 && lastbit + 1 == bit)
12691 : 0 : ++run;
12692 : : else
12693 : : {
12694 : 0 : if (lastbit == 0)
12695 : 0 : p += sprintf (p, "%u", bit - bias);
12696 [ # # ]: 0 : else if (run == 0)
12697 : 0 : p += sprintf (p, ",%u", bit - bias);
12698 : : else
12699 : 0 : p += sprintf (p, "-%u,%u", lastbit - bias, bit - bias);
12700 : : run = 0;
12701 : : }
12702 : :
12703 : : lastbit = bit;
12704 : : }
12705 : : }
12706 [ - + - - ]: 44 : if (lastbit > 0 && run > 0 && lastbit + 1 != nbits)
12707 : 0 : p += sprintf (p, "-%u", lastbit - bias);
12708 : :
12709 [ + - ]: 88 : colno = print_core_item (colno, ',', WRAP_COLUMN, 0, item->name,
12710 : : negate ? "~<%s>" : "<%s>", printed);
12711 : : }
12712 : 44 : break;
12713 : :
12714 : 88 : case 'T':
12715 : : case (char) ('T'|0x80):
12716 [ - + ]: 88 : assert (count == 2);
12717 : 88 : Dwarf_Word sec;
12718 : 88 : Dwarf_Word usec;
12719 [ - - + - : 88 : switch (type)
+ - - ]
12720 : : {
12721 : : #define DO_TYPE(NAME, Name, hex, dec) \
12722 : : case ELF_T_##NAME: \
12723 : : sec = value_##Name[0]; \
12724 : : usec = value_##Name[1]; \
12725 : : break
12726 : 88 : TYPES;
12727 : : #undef DO_TYPE
12728 : 0 : default:
12729 : 0 : abort ();
12730 : : }
12731 [ - + ]: 88 : if (unlikely (item->format == (char) ('T'|0x80)))
12732 : : {
12733 : : /* This is a hack for an ill-considered 64-bit ABI where
12734 : : tv_usec is actually a 32-bit field with 32 bits of padding
12735 : : rounding out struct timeval. We've already converted it as
12736 : : a 64-bit field. For little-endian, this just means the
12737 : : high half is the padding; it's presumably zero, but should
12738 : : be ignored anyway. For big-endian, it means the 32-bit
12739 : : field went into the high half of USEC. */
12740 [ # # ]: 0 : if (likely (ehdr->e_ident[EI_DATA] == ELFDATA2MSB))
12741 : 0 : usec >>= 32;
12742 : : else
12743 : 0 : usec &= UINT32_MAX;
12744 : : }
12745 : 88 : colno = print_core_item (colno, ',', WRAP_COLUMN, 0, item->name,
12746 : : "%" PRIu64 ".%.6" PRIu64, sec, usec);
12747 : 88 : break;
12748 : :
12749 : 18 : case 'c':
12750 [ - + ]: 18 : assert (count == 1);
12751 : 18 : colno = print_core_item (colno, ',', WRAP_COLUMN, 0, item->name,
12752 : 18 : "%c", value_Byte[0]);
12753 : 18 : break;
12754 : :
12755 : 36 : case 's':
12756 : 36 : colno = print_core_item (colno, ',', WRAP_COLUMN, 0, item->name,
12757 : : "%.*s", (int) count, value_Byte);
12758 : 36 : break;
12759 : :
12760 : 2 : case '\n':
12761 : : /* This is a list of strings separated by '\n'. */
12762 [ - + ]: 2 : assert (item->count == 0);
12763 [ - + ]: 2 : assert (repeated_size != NULL);
12764 [ - + ]: 2 : assert (item->name == NULL);
12765 [ - + ]: 2 : if (unlikely (item->offset >= *repeated_size))
12766 : : break;
12767 : :
12768 : 2 : const char *s = desc + item->offset;
12769 : 2 : size = *repeated_size - item->offset;
12770 : 2 : *repeated_size = 0;
12771 [ + - ]: 96 : while (size > 0)
12772 : : {
12773 : 96 : const char *eol = memchr (s, '\n', size);
12774 : 96 : int len = size;
12775 [ + + ]: 96 : if (eol != NULL)
12776 : 94 : len = eol - s;
12777 : 96 : printf ("%*s%.*s\n", ITEM_INDENT, "", len, s);
12778 [ + + ]: 96 : if (eol == NULL)
12779 : : break;
12780 : 94 : size -= eol + 1 - s;
12781 : 94 : s = eol + 1;
12782 : : }
12783 : :
12784 : : colno = WRAP_COLUMN;
12785 : : break;
12786 : :
12787 : : case 'h':
12788 : : break;
12789 : :
12790 : 0 : default:
12791 : 0 : error (0, 0, "XXX not handling format '%c' for %s",
12792 : 0 : item->format, item->name);
12793 : 0 : break;
12794 : : }
12795 : :
12796 : : #undef TYPES
12797 : :
12798 : 800 : return colno;
12799 : : }
12800 : :
12801 : :
12802 : : /* Sort items by group, and by layout offset within each group. */
12803 : : static int
12804 : 1790 : compare_core_items (const void *a, const void *b)
12805 : : {
12806 : 1790 : const Ebl_Core_Item *const *p1 = a;
12807 : 1790 : const Ebl_Core_Item *const *p2 = b;
12808 : 1790 : const Ebl_Core_Item *item1 = *p1;
12809 : 1790 : const Ebl_Core_Item *item2 = *p2;
12810 : :
12811 : 1790 : return ((item1->group == item2->group ? 0
12812 [ + + ]: 1790 : : strcmp (item1->group, item2->group))
12813 [ - + ]: 1790 : ?: (int) item1->offset - (int) item2->offset);
12814 : : }
12815 : :
12816 : : /* Sort item groups by layout offset of the first item in the group. */
12817 : : static int
12818 : 278 : compare_core_item_groups (const void *a, const void *b)
12819 : : {
12820 : 278 : const Ebl_Core_Item *const *const *p1 = a;
12821 : 278 : const Ebl_Core_Item *const *const *p2 = b;
12822 : 278 : const Ebl_Core_Item *const *group1 = *p1;
12823 : 278 : const Ebl_Core_Item *const *group2 = *p2;
12824 : 278 : const Ebl_Core_Item *item1 = *group1;
12825 : 278 : const Ebl_Core_Item *item2 = *group2;
12826 : :
12827 : 278 : return (int) item1->offset - (int) item2->offset;
12828 : : }
12829 : :
12830 : : static unsigned int
12831 : 74 : handle_core_items (Elf *core, const GElf_Ehdr *ehdr,
12832 : : const void *desc, size_t descsz,
12833 : : const Ebl_Core_Item *items, size_t nitems)
12834 : 74 : {
12835 [ + + ]: 74 : if (nitems == 0)
12836 : : return 0;
12837 : 68 : unsigned int colno = 0;
12838 : :
12839 : : /* FORMAT '\n' makes sense to be present only as a single item as it
12840 : : processes all the data of a note. FORMATs 'b' and 'B' have a special case
12841 : : if present as a single item but they can be also processed with other
12842 : : items below. */
12843 [ + + + + ]: 68 : if (nitems == 1 && (items[0].format == '\n' || items[0].format == 'b'
12844 [ + - ]: 16 : || items[0].format == 'B'))
12845 : : {
12846 [ - + ]: 2 : assert (items[0].offset == 0);
12847 : 2 : size_t size = descsz;
12848 : 2 : colno = handle_core_item (core, ehdr, items, desc, colno, &size);
12849 : : /* If SIZE is not zero here there is some remaining data. But we do not
12850 : : know how to process it anyway. */
12851 : 2 : return colno;
12852 : : }
12853 [ + + ]: 848 : for (size_t i = 0; i < nitems; ++i)
12854 [ - + ]: 782 : assert (items[i].format != '\n');
12855 : :
12856 : : /* Sort to collect the groups together. */
12857 : 66 : const Ebl_Core_Item *sorted_items[nitems];
12858 [ + + ]: 848 : for (size_t i = 0; i < nitems; ++i)
12859 : 782 : sorted_items[i] = &items[i];
12860 : 66 : qsort (sorted_items, nitems, sizeof sorted_items[0], &compare_core_items);
12861 : :
12862 : : /* Collect the unique groups and sort them. */
12863 : 66 : const Ebl_Core_Item **groups[nitems];
12864 : 66 : groups[0] = &sorted_items[0];
12865 : 66 : size_t ngroups = 1;
12866 [ + + ]: 782 : for (size_t i = 1; i < nitems; ++i)
12867 [ + + ]: 716 : if (sorted_items[i]->group != sorted_items[i - 1]->group
12868 [ + - ]: 150 : && strcmp (sorted_items[i]->group, sorted_items[i - 1]->group))
12869 : 150 : groups[ngroups++] = &sorted_items[i];
12870 : 66 : qsort (groups, ngroups, sizeof groups[0], &compare_core_item_groups);
12871 : :
12872 : : /* Write out all the groups. */
12873 : 66 : const void *last = desc;
12874 : 70 : do
12875 : : {
12876 [ + + ]: 290 : for (size_t i = 0; i < ngroups; ++i)
12877 : : {
12878 : 220 : for (const Ebl_Core_Item **item = groups[i];
12879 : 1018 : (item < &sorted_items[nitems]
12880 [ + + + + ]: 1018 : && ((*item)->group == groups[i][0]->group
12881 [ - + ]: 150 : || !strcmp ((*item)->group, groups[i][0]->group)));
12882 : 798 : ++item)
12883 : 798 : colno = handle_core_item (core, ehdr, *item, desc, colno, NULL);
12884 : :
12885 : : /* Force a line break at the end of the group. */
12886 : 220 : colno = WRAP_COLUMN;
12887 : : }
12888 : :
12889 [ + + ]: 70 : if (descsz == 0)
12890 : : break;
12891 : :
12892 : : /* This set of items consumed a certain amount of the note's data.
12893 : : If there is more data there, we have another unit of the same size.
12894 : : Loop to print that out too. */
12895 : 40 : const Ebl_Core_Item *item = &items[nitems - 1];
12896 : 80 : size_t eltsz = item->offset + gelf_fsize (core, item->type,
12897 : 40 : item->count ?: 1, EV_CURRENT);
12898 : :
12899 : 40 : int reps = -1;
12900 : 40 : do
12901 : : {
12902 : 40 : ++reps;
12903 : 40 : desc += eltsz;
12904 : 40 : descsz -= eltsz;
12905 : : }
12906 [ + + - + ]: 40 : while (descsz >= eltsz && !memcmp (desc, last, eltsz));
12907 : :
12908 [ + - ]: 40 : if (reps == 1)
12909 : : {
12910 : : /* For just one repeat, print it unabridged twice. */
12911 : : desc -= eltsz;
12912 : : descsz += eltsz;
12913 : : }
12914 [ + - ]: 40 : else if (reps > 1)
12915 : 0 : printf (_("\n%*s... <repeats %u more times> ..."),
12916 : : ITEM_INDENT, "", reps);
12917 : :
12918 : 40 : last = desc;
12919 : : }
12920 [ + + ]: 40 : while (descsz > 0);
12921 : :
12922 : : return colno;
12923 : : }
12924 : :
12925 : : static unsigned int
12926 : 324 : handle_core_register (Ebl *ebl, Elf *core, int maxregname,
12927 : : const Ebl_Register_Location *regloc, const void *desc,
12928 : : unsigned int colno)
12929 : : {
12930 [ - + ]: 324 : if (regloc->bits % 8 != 0)
12931 : : {
12932 : 0 : error (0, 0, "Warning: Cannot handle register with %" PRIu8 "bits\n",
12933 : : regloc->bits);
12934 : 0 : return colno;
12935 : : }
12936 : :
12937 : 324 : desc += regloc->offset;
12938 : :
12939 [ + + ]: 1172 : for (int reg = regloc->regno; reg < regloc->regno + regloc->count; ++reg)
12940 : : {
12941 : 848 : char name[REGNAMESZ];
12942 : 848 : int bits;
12943 : 848 : int type;
12944 : 848 : register_info (ebl, reg, regloc, name, &bits, &type);
12945 : :
12946 : : #define TYPES \
12947 : : BITS (8, BYTE, "%4" PRId8, "0x%.2" PRIx8); \
12948 : : BITS (16, HALF, "%6" PRId16, "0x%.4" PRIx16); \
12949 : : BITS (32, WORD, "%11" PRId32, " 0x%.8" PRIx32); \
12950 : : BITS (64, XWORD, "%20" PRId64, " 0x%.16" PRIx64)
12951 : :
12952 : : #define BITS(bits, xtype, sfmt, ufmt) \
12953 : : uint##bits##_t b##bits; int##bits##_t b##bits##s
12954 : 848 : union { TYPES; uint64_t b128[2]; } value;
12955 : : #undef BITS
12956 : :
12957 [ + + ]: 848 : switch (type)
12958 : : {
12959 : 672 : case DW_ATE_unsigned:
12960 : : case DW_ATE_signed:
12961 : : case DW_ATE_address:
12962 [ - + + + : 672 : switch (bits)
+ - ]
12963 : : {
12964 : : #define BITS(bits, xtype, sfmt, ufmt) \
12965 : : case bits: \
12966 : : desc = convert (core, ELF_T_##xtype, 1, &value, desc, 0); \
12967 : : if (type == DW_ATE_signed) \
12968 : : colno = print_core_item (colno, ' ', WRAP_COLUMN, \
12969 : : maxregname, name, \
12970 : : sfmt, value.b##bits##s); \
12971 : : else \
12972 : : colno = print_core_item (colno, ' ', WRAP_COLUMN, \
12973 : : maxregname, name, \
12974 : : ufmt, value.b##bits); \
12975 : : break
12976 : :
12977 [ - - - + : 576 : TYPES;
+ + + + ]
12978 : :
12979 : 96 : case 128:
12980 [ - + ]: 96 : assert (type == DW_ATE_unsigned);
12981 : 96 : desc = convert (core, ELF_T_XWORD, 2, &value, desc, 0);
12982 : 96 : int be = elf_getident (core, NULL)[EI_DATA] == ELFDATA2MSB;
12983 : 96 : colno = print_core_item (colno, ' ', WRAP_COLUMN,
12984 : : maxregname, name,
12985 : : "0x%.16" PRIx64 "%.16" PRIx64,
12986 : 96 : value.b128[!be], value.b128[be]);
12987 : 96 : break;
12988 : :
12989 : 0 : default:
12990 : 0 : abort ();
12991 : : #undef BITS
12992 : : }
12993 : : break;
12994 : :
12995 : 176 : default:
12996 : : /* Print each byte in hex, the whole thing in native byte order. */
12997 [ - + ]: 176 : assert (bits % 8 == 0);
12998 : 176 : const uint8_t *bytes = desc;
12999 : 176 : desc += bits / 8;
13000 : 176 : char hex[bits / 4 + 1];
13001 : 176 : hex[bits / 4] = '\0';
13002 : 176 : int incr = 1;
13003 [ + + ]: 176 : if (elf_getident (core, NULL)[EI_DATA] == ELFDATA2LSB)
13004 : : {
13005 : 96 : bytes += bits / 8 - 1;
13006 : 96 : incr = -1;
13007 : : }
13008 : 176 : size_t idx = 0;
13009 [ + + ]: 1744 : for (char *h = hex; bits > 0; bits -= 8, idx += incr)
13010 : : {
13011 : 1568 : *h++ = "0123456789abcdef"[bytes[idx] >> 4];
13012 : 1568 : *h++ = "0123456789abcdef"[bytes[idx] & 0xf];
13013 : : }
13014 : 176 : colno = print_core_item (colno, ' ', WRAP_COLUMN,
13015 : : maxregname, name, "0x%s", hex);
13016 : 176 : break;
13017 : : }
13018 : 848 : desc += regloc->pad;
13019 : :
13020 : : #undef TYPES
13021 : : }
13022 : :
13023 : : return colno;
13024 : : }
13025 : :
13026 : :
13027 : : struct register_info
13028 : : {
13029 : : const Ebl_Register_Location *regloc;
13030 : : const char *set;
13031 : : char name[REGNAMESZ];
13032 : : int regno;
13033 : : int bits;
13034 : : int type;
13035 : : };
13036 : :
13037 : : static int
13038 : 4132 : register_bitpos (const struct register_info *r)
13039 : : {
13040 : 4132 : return (r->regloc->offset * 8
13041 : 4132 : + ((r->regno - r->regloc->regno)
13042 : 4132 : * (r->regloc->bits + r->regloc->pad * 8)));
13043 : : }
13044 : :
13045 : : static int
13046 : 2124 : compare_sets_by_info (const struct register_info *r1,
13047 : : const struct register_info *r2)
13048 : : {
13049 : 2124 : return ((int) r2->bits - (int) r1->bits
13050 [ + + ]: 2124 : ?: register_bitpos (r1) - register_bitpos (r2));
13051 : : }
13052 : :
13053 : : /* Sort registers by set, and by size and layout offset within each set. */
13054 : : static int
13055 : 9190 : compare_registers (const void *a, const void *b)
13056 : : {
13057 : 9190 : const struct register_info *r1 = a;
13058 : 9190 : const struct register_info *r2 = b;
13059 : :
13060 : : /* Unused elements sort last. */
13061 [ + + ]: 9190 : if (r1->regloc == NULL)
13062 : 6618 : return r2->regloc == NULL ? 0 : 1;
13063 [ + + ]: 2572 : if (r2->regloc == NULL)
13064 : : return -1;
13065 : :
13066 [ + + ]: 2242 : return ((r1->set == r2->set ? 0 : strcmp (r1->set, r2->set))
13067 [ - + ]: 2242 : ?: compare_sets_by_info (r1, r2));
13068 : : }
13069 : :
13070 : : /* Sort register sets by layout offset of the first register in the set. */
13071 : : static int
13072 : 40 : compare_register_sets (const void *a, const void *b)
13073 : : {
13074 : 40 : const struct register_info *const *p1 = a;
13075 : 40 : const struct register_info *const *p2 = b;
13076 : 40 : return compare_sets_by_info (*p1, *p2);
13077 : : }
13078 : :
13079 : : static inline bool
13080 : 1728 : same_set (const struct register_info *a,
13081 : : const struct register_info *b,
13082 : : const struct register_info *regs,
13083 : : size_t maxnreg)
13084 : : {
13085 [ + - ]: 1728 : return (a < ®s[maxnreg] && a->regloc != NULL
13086 [ + - + + ]: 1728 : && b < ®s[maxnreg] && b->regloc != NULL
13087 [ + + ]: 1692 : && a->bits == b->bits
13088 [ + - + + : 3384 : && (a->set == b->set || !strcmp (a->set, b->set)));
- + ]
13089 : : }
13090 : :
13091 : : static unsigned int
13092 : 74 : handle_core_registers (Ebl *ebl, Elf *core, const void *desc,
13093 : : const Ebl_Register_Location *reglocs, size_t nregloc)
13094 : 74 : {
13095 [ + + ]: 74 : if (nregloc == 0)
13096 : : return 0;
13097 : :
13098 : 36 : ssize_t maxnreg = ebl_register_info (ebl, 0, NULL, 0, NULL, NULL, NULL, NULL);
13099 [ - + ]: 36 : if (maxnreg <= 0)
13100 : : {
13101 [ # # ]: 0 : for (size_t i = 0; i < nregloc; ++i)
13102 : 0 : if (maxnreg < reglocs[i].regno + reglocs[i].count)
13103 : : maxnreg = reglocs[i].regno + reglocs[i].count;
13104 [ # # ]: 0 : assert (maxnreg > 0);
13105 : : }
13106 : :
13107 : 36 : struct register_info regs[maxnreg];
13108 : 36 : memset (regs, 0, sizeof regs);
13109 : :
13110 : : /* Sort to collect the sets together. */
13111 : 36 : int maxreg = 0;
13112 [ + + ]: 360 : for (size_t i = 0; i < nregloc; ++i)
13113 : 324 : for (int reg = reglocs[i].regno;
13114 [ + + ]: 1172 : reg < reglocs[i].regno + reglocs[i].count;
13115 : 848 : ++reg)
13116 : : {
13117 [ - + ]: 848 : assert (reg < maxnreg);
13118 : 848 : if (reg > maxreg)
13119 : : maxreg = reg;
13120 : 848 : struct register_info *info = ®s[reg];
13121 : 848 : info->regloc = ®locs[i];
13122 : 848 : info->regno = reg;
13123 : 848 : info->set = register_info (ebl, reg, ®locs[i],
13124 : 848 : info->name, &info->bits, &info->type);
13125 : : }
13126 : 36 : qsort (regs, maxreg + 1, sizeof regs[0], &compare_registers);
13127 : :
13128 : : /* Collect the unique sets and sort them. */
13129 : 36 : struct register_info *sets[maxreg + 1];
13130 : 36 : sets[0] = ®s[0];
13131 : 36 : size_t nsets = 1;
13132 [ + + ]: 2558 : for (int i = 1; i <= maxreg; ++i)
13133 [ + + ]: 2522 : if (regs[i].regloc != NULL
13134 [ + + ]: 812 : && !same_set (®s[i], ®s[i - 1], regs, maxnreg))
13135 : 32 : sets[nsets++] = ®s[i];
13136 : 36 : qsort (sets, nsets, sizeof sets[0], &compare_register_sets);
13137 : :
13138 : : /* Write out all the sets. */
13139 : 36 : unsigned int colno = 0;
13140 [ + + ]: 104 : for (size_t i = 0; i < nsets; ++i)
13141 : : {
13142 : : /* Find the longest name of a register in this set. */
13143 : 68 : size_t maxname = 0;
13144 : 68 : const struct register_info *end;
13145 [ + + ]: 916 : for (end = sets[i]; same_set (sets[i], end, regs, maxnreg); ++end)
13146 : : {
13147 : 848 : size_t len = strlen (end->name);
13148 : 848 : if (len > maxname)
13149 : : maxname = len;
13150 : : }
13151 : :
13152 : : for (const struct register_info *reg = sets[i];
13153 [ + + ]: 392 : reg < end;
13154 : 324 : reg += reg->regloc->count ?: 1)
13155 [ + - ]: 324 : colno = handle_core_register (ebl, core, maxname,
13156 : 324 : reg->regloc, desc, colno);
13157 : :
13158 : : /* Force a line break at the end of the group. */
13159 : 68 : colno = WRAP_COLUMN;
13160 : : }
13161 : :
13162 : : return colno;
13163 : : }
13164 : :
13165 : : static void
13166 : 18 : handle_auxv_note (Ebl *ebl, Elf *core, GElf_Word descsz, GElf_Off desc_pos)
13167 : : {
13168 : 18 : Elf_Data *data = elf_getdata_rawchunk (core, desc_pos, descsz, ELF_T_AUXV);
13169 [ - + ]: 18 : if (data == NULL)
13170 : 0 : elf_error:
13171 : 0 : error_exit (0, _("cannot convert core note data: %s"), elf_errmsg (-1));
13172 : :
13173 : 18 : const size_t nauxv = descsz / gelf_fsize (core, ELF_T_AUXV, 1, EV_CURRENT);
13174 [ + + ]: 354 : for (size_t i = 0; i < nauxv; ++i)
13175 : : {
13176 : 336 : GElf_auxv_t av_mem;
13177 : 336 : GElf_auxv_t *av = gelf_getauxv (data, i, &av_mem);
13178 [ - + ]: 336 : if (av == NULL)
13179 : 0 : goto elf_error;
13180 : :
13181 : 336 : const char *name;
13182 : 336 : const char *fmt;
13183 [ - + ]: 336 : if (ebl_auxv_info (ebl, av->a_type, &name, &fmt) == 0)
13184 : : {
13185 : : /* Unknown type. */
13186 [ # # ]: 0 : if (av->a_un.a_val == 0)
13187 : 0 : printf (" %" PRIu64 "\n", av->a_type);
13188 : : else
13189 : 0 : printf (" %" PRIu64 ": %#" PRIx64 "\n",
13190 : : av->a_type, av->a_un.a_val);
13191 : : }
13192 : : else
13193 [ + + + - : 336 : switch (fmt[0])
+ - ]
13194 : : {
13195 : 18 : case '\0': /* Normally zero. */
13196 [ + - ]: 18 : if (av->a_un.a_val == 0)
13197 : : {
13198 : 18 : printf (" %s\n", name);
13199 : 18 : break;
13200 : : }
13201 : 148 : FALLTHROUGH;
13202 : : case 'x': /* hex */
13203 : : case 'p': /* address */
13204 : : case 's': /* address of string */
13205 : 148 : printf (" %s: %#" PRIx64 "\n", name, av->a_un.a_val);
13206 : 148 : break;
13207 : 162 : case 'u':
13208 : 162 : printf (" %s: %" PRIu64 "\n", name, av->a_un.a_val);
13209 : 162 : break;
13210 : 0 : case 'd':
13211 : 0 : printf (" %s: %" PRId64 "\n", name, av->a_un.a_val);
13212 : 0 : break;
13213 : :
13214 : 8 : case 'b':
13215 : 8 : printf (" %s: %#" PRIx64 " ", name, av->a_un.a_val);
13216 : 8 : GElf_Xword bit = 1;
13217 : 8 : const char *pfx = "<";
13218 [ + + ]: 220 : for (const char *p = fmt + 1; *p != 0; p = strchr (p, '\0') + 1)
13219 : : {
13220 [ + + ]: 212 : if (av->a_un.a_val & bit)
13221 : : {
13222 : 154 : printf ("%s%s", pfx, p);
13223 : 154 : pfx = " ";
13224 : : }
13225 : 212 : bit <<= 1;
13226 : : }
13227 : 8 : printf (">\n");
13228 : 8 : break;
13229 : :
13230 : 0 : default:
13231 : 0 : abort ();
13232 : : }
13233 : : }
13234 : 18 : }
13235 : :
13236 : : static bool
13237 : 390 : buf_has_data (unsigned char const *ptr, unsigned char const *end, size_t sz)
13238 : : {
13239 [ + - + - ]: 390 : return ptr < end && (size_t) (end - ptr) >= sz;
13240 : : }
13241 : :
13242 : : static bool
13243 : 36 : buf_read_int (Elf *core, unsigned char const **ptrp, unsigned char const *end,
13244 : : int *retp)
13245 : : {
13246 [ + - ]: 36 : if (! buf_has_data (*ptrp, end, 4))
13247 : : return false;
13248 : :
13249 : 36 : *ptrp = convert (core, ELF_T_WORD, 1, retp, *ptrp, 4);
13250 : 36 : return true;
13251 : : }
13252 : :
13253 : : static bool
13254 : 354 : buf_read_ulong (Elf *core, unsigned char const **ptrp, unsigned char const *end,
13255 : : uint64_t *retp)
13256 : : {
13257 : 354 : size_t sz = gelf_fsize (core, ELF_T_ADDR, 1, EV_CURRENT);
13258 [ + - ]: 354 : if (! buf_has_data (*ptrp, end, sz))
13259 : : return false;
13260 : :
13261 : 354 : union
13262 : : {
13263 : : uint64_t u64;
13264 : : uint32_t u32;
13265 : : } u;
13266 : :
13267 : 354 : *ptrp = convert (core, ELF_T_ADDR, 1, &u, *ptrp, sz);
13268 : :
13269 [ + + ]: 354 : if (sz == 4)
13270 : 186 : *retp = u.u32;
13271 : : else
13272 : 168 : *retp = u.u64;
13273 : : return true;
13274 : : }
13275 : :
13276 : : static void
13277 : 12 : handle_siginfo_note (Elf *core, GElf_Word descsz, GElf_Off desc_pos)
13278 : : {
13279 : 12 : Elf_Data *data = elf_getdata_rawchunk (core, desc_pos, descsz, ELF_T_BYTE);
13280 [ - + ]: 12 : if (data == NULL)
13281 : 0 : error_exit (0, _("cannot convert core note data: %s"), elf_errmsg (-1));
13282 : :
13283 : 12 : unsigned char const *ptr = data->d_buf;
13284 : 12 : unsigned char const *const end = data->d_buf + data->d_size;
13285 : :
13286 : : /* Siginfo head is three ints: signal number, error number, origin
13287 : : code. */
13288 : 12 : int si_signo, si_errno, si_code;
13289 [ - + ]: 12 : if (! buf_read_int (core, &ptr, end, &si_signo)
13290 [ - + ]: 12 : || ! buf_read_int (core, &ptr, end, &si_errno)
13291 [ - + ]: 12 : || ! buf_read_int (core, &ptr, end, &si_code))
13292 : : {
13293 : 0 : fail:
13294 : 0 : printf (" Not enough data in NT_SIGINFO note.\n");
13295 : 0 : return;
13296 : : }
13297 : :
13298 : : /* Next is a pointer-aligned union of structures. On 64-bit
13299 : : machines, that implies a word of padding. */
13300 [ + + ]: 12 : if (gelf_getclass (core) == ELFCLASS64)
13301 : 6 : ptr += 4;
13302 : :
13303 : 12 : printf (" si_signo: %d, si_errno: %d, si_code: %d\n",
13304 : : si_signo, si_errno, si_code);
13305 : :
13306 [ + - ]: 12 : if (si_code > 0)
13307 [ + - ]: 12 : switch (si_signo)
13308 : : {
13309 : 12 : case CORE_SIGILL:
13310 : : case CORE_SIGFPE:
13311 : : case CORE_SIGSEGV:
13312 : : case CORE_SIGBUS:
13313 : : {
13314 : 12 : uint64_t addr;
13315 [ - + ]: 12 : if (! buf_read_ulong (core, &ptr, end, &addr))
13316 : 0 : goto fail;
13317 : 12 : printf (" fault address: %#" PRIx64 "\n", addr);
13318 : 12 : break;
13319 : : }
13320 : : default:
13321 : : ;
13322 : : }
13323 [ # # ]: 0 : else if (si_code == CORE_SI_USER)
13324 : : {
13325 : 0 : int pid, uid;
13326 [ # # ]: 0 : if (! buf_read_int (core, &ptr, end, &pid)
13327 [ # # ]: 0 : || ! buf_read_int (core, &ptr, end, &uid))
13328 : 0 : goto fail;
13329 : 0 : printf (" sender PID: %d, sender UID: %d\n", pid, uid);
13330 : : }
13331 : : }
13332 : :
13333 : : static void
13334 : 12 : handle_file_note (Elf *core, GElf_Word descsz, GElf_Off desc_pos)
13335 : : {
13336 : 12 : Elf_Data *data = elf_getdata_rawchunk (core, desc_pos, descsz, ELF_T_BYTE);
13337 [ - + ]: 12 : if (data == NULL)
13338 : 0 : error_exit (0, _("cannot convert core note data: %s"), elf_errmsg (-1));
13339 : :
13340 : 12 : unsigned char const *ptr = data->d_buf;
13341 : 12 : unsigned char const *const end = data->d_buf + data->d_size;
13342 : :
13343 : 12 : uint64_t count, page_size;
13344 [ - + ]: 12 : if (! buf_read_ulong (core, &ptr, end, &count)
13345 [ - + ]: 12 : || ! buf_read_ulong (core, &ptr, end, &page_size))
13346 : : {
13347 : 0 : fail:
13348 : 0 : printf (" Not enough data in NT_FILE note.\n");
13349 : 0 : return;
13350 : : }
13351 : :
13352 : 12 : size_t addrsize = gelf_fsize (core, ELF_T_ADDR, 1, EV_CURRENT);
13353 : 12 : uint64_t maxcount = (size_t) (end - ptr) / (3 * addrsize);
13354 [ - + ]: 12 : if (count > maxcount)
13355 : 0 : goto fail;
13356 : :
13357 : : /* Where file names are stored. */
13358 : 12 : unsigned char const *const fstart = ptr + 3 * count * addrsize;
13359 : 12 : char const *fptr = (char *) fstart;
13360 : :
13361 : 12 : printf (" %" PRId64 " files:\n", count);
13362 [ + + ]: 118 : for (uint64_t i = 0; i < count; ++i)
13363 : : {
13364 : 106 : uint64_t mstart, mend, moffset;
13365 [ + - ]: 106 : if (! buf_read_ulong (core, &ptr, fstart, &mstart)
13366 [ + - ]: 106 : || ! buf_read_ulong (core, &ptr, fstart, &mend)
13367 [ - + ]: 106 : || ! buf_read_ulong (core, &ptr, fstart, &moffset))
13368 : 0 : goto fail;
13369 : :
13370 : 106 : const char *fnext = memchr (fptr, '\0', (char *) end - fptr);
13371 [ - + ]: 106 : if (fnext == NULL)
13372 : 0 : goto fail;
13373 : :
13374 : 106 : int ct = printf (" %08" PRIx64 "-%08" PRIx64
13375 : : " %08" PRIx64 " %" PRId64,
13376 : : mstart, mend, moffset * page_size, mend - mstart);
13377 [ + - ]: 106 : printf ("%*s%s\n", ct > 50 ? 3 : 53 - ct, "", fptr);
13378 : :
13379 : 106 : fptr = fnext + 1;
13380 : : }
13381 : : }
13382 : :
13383 : : static void
13384 : 76 : handle_core_note (Ebl *ebl, const GElf_Ehdr *ehdr, const GElf_Nhdr *nhdr,
13385 : : const char *name, const void *desc)
13386 : : {
13387 : 76 : GElf_Word regs_offset;
13388 : 76 : size_t nregloc;
13389 : 76 : const Ebl_Register_Location *reglocs;
13390 : 76 : size_t nitems;
13391 : 76 : const Ebl_Core_Item *items;
13392 : :
13393 [ + + ]: 76 : if (! ebl_core_note (ebl, nhdr, name, desc,
13394 : : ®s_offset, &nregloc, ®locs, &nitems, &items))
13395 : 2 : return;
13396 : :
13397 : : /* Pass 0 for DESCSZ when there are registers in the note,
13398 : : so that the ITEMS array does not describe the whole thing.
13399 : : For non-register notes, the actual descsz might be a multiple
13400 : : of the unit size, not just exactly the unit size. */
13401 : 74 : unsigned int colno = handle_core_items (ebl->elf, ehdr, desc,
13402 [ + + ]: 74 : nregloc == 0 ? nhdr->n_descsz : 0,
13403 : : items, nitems);
13404 [ + + ]: 74 : if (colno != 0)
13405 : 68 : putchar ('\n');
13406 : :
13407 : 74 : colno = handle_core_registers (ebl, ebl->elf, desc + regs_offset,
13408 : : reglocs, nregloc);
13409 [ + + ]: 74 : if (colno != 0)
13410 : 36 : putchar ('\n');
13411 : : }
13412 : :
13413 : : static void
13414 : 506 : handle_notes_data (Ebl *ebl, const GElf_Ehdr *ehdr,
13415 : : GElf_Off start, Elf_Data *data)
13416 : : {
13417 : 506 : fputs (_(" Owner Data size Type\n"), stdout);
13418 : :
13419 [ - + ]: 506 : if (data == NULL)
13420 : 0 : goto bad_note;
13421 : :
13422 : : size_t offset = 0;
13423 : : GElf_Nhdr nhdr;
13424 : : size_t name_offset;
13425 : : size_t desc_offset;
13426 : 5120 : while (offset < data->d_size
13427 [ + + + - ]: 5120 : && (offset = gelf_getnote (data, offset,
13428 : : &nhdr, &name_offset, &desc_offset)) > 0)
13429 : : {
13430 [ + - ]: 4614 : const char *name = nhdr.n_namesz == 0 ? "" : data->d_buf + name_offset;
13431 : 4614 : const char *desc = data->d_buf + desc_offset;
13432 : :
13433 : : /* GNU Build Attributes are weird, they store most of their data
13434 : : into the owner name field. Extract just the owner name
13435 : : prefix here, then use the rest later as data. */
13436 : 4614 : bool is_gnu_build_attr
13437 : 4614 : = startswith (name, ELF_NOTE_GNU_BUILD_ATTRIBUTE_PREFIX);
13438 : 9798 : const char *print_name = (is_gnu_build_attr
13439 [ + + ]: 4614 : ? ELF_NOTE_GNU_BUILD_ATTRIBUTE_PREFIX : name);
13440 : 5184 : size_t print_namesz = (is_gnu_build_attr
13441 : 570 : ? strlen (print_name) : nhdr.n_namesz);
13442 : :
13443 : 4614 : char buf[100];
13444 : 4614 : char buf2[100];
13445 : 4614 : printf (_(" %-13.*s %9" PRId32 " %s\n"),
13446 : : (int) print_namesz, print_name, nhdr.n_descsz,
13447 [ + + ]: 4614 : ehdr->e_type == ET_CORE
13448 : 118 : ? ebl_core_note_type_name (ebl, nhdr.n_type,
13449 : : buf, sizeof (buf))
13450 : 4496 : : ebl_object_note_type_name (ebl, name, nhdr.n_type,
13451 : : nhdr.n_descsz,
13452 : : buf2, sizeof (buf2)));
13453 : :
13454 : : /* Filter out invalid entries. */
13455 : 4614 : if (memchr (name, '\0', nhdr.n_namesz) != NULL
13456 : : /* XXX For now help broken Linux kernels. */
13457 : : || 1)
13458 : : {
13459 [ + + ]: 4614 : if (ehdr->e_type == ET_CORE)
13460 : : {
13461 [ + + ]: 118 : if (nhdr.n_type == NT_AUXV
13462 [ + - ]: 18 : && (nhdr.n_namesz == 4 /* Broken old Linux kernels. */
13463 [ + - + - ]: 18 : || (nhdr.n_namesz == 5 && name[4] == '\0'))
13464 [ + - ]: 18 : && !memcmp (name, "CORE", 4))
13465 : 18 : handle_auxv_note (ebl, ebl->elf, nhdr.n_descsz,
13466 : : start + desc_offset);
13467 [ + + + - ]: 100 : else if (nhdr.n_namesz == 5 && strcmp (name, "CORE") == 0)
13468 [ + + + ]: 76 : switch (nhdr.n_type)
13469 : : {
13470 : 12 : case NT_SIGINFO:
13471 : 12 : handle_siginfo_note (ebl->elf, nhdr.n_descsz,
13472 : : start + desc_offset);
13473 : 12 : break;
13474 : :
13475 : 12 : case NT_FILE:
13476 : 12 : handle_file_note (ebl->elf, nhdr.n_descsz,
13477 : : start + desc_offset);
13478 : 12 : break;
13479 : :
13480 : 52 : default:
13481 : 52 : handle_core_note (ebl, ehdr, &nhdr, name, desc);
13482 : : }
13483 : : else
13484 : 24 : handle_core_note (ebl, ehdr, &nhdr, name, desc);
13485 : : }
13486 : : else
13487 : 4496 : ebl_object_note (ebl, nhdr.n_namesz, name, nhdr.n_type,
13488 : : nhdr.n_descsz, desc);
13489 : : }
13490 : : }
13491 : :
13492 [ + - ]: 506 : if (offset == data->d_size)
13493 : 506 : return;
13494 : :
13495 : 0 : bad_note:
13496 : 0 : error (0, 0,
13497 : 0 : _("cannot get content of note: %s"),
13498 : 0 : data != NULL ? "garbage data" : elf_errmsg (-1));
13499 : : }
13500 : :
13501 : : static void
13502 : 230 : handle_notes (Ebl *ebl, GElf_Ehdr *ehdr)
13503 : : {
13504 : : /* If we have section headers, just look for SHT_NOTE sections.
13505 : : In a debuginfo file, the program headers are not reliable. */
13506 [ + + ]: 230 : if (shnum != 0)
13507 : : {
13508 : : /* Get the section header string table index. */
13509 : 208 : size_t shstrndx;
13510 [ - + ]: 208 : if (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0)
13511 : 0 : error_exit (0, _("cannot get section header string table index"));
13512 : :
13513 : : Elf_Scn *scn = NULL;
13514 [ + + ]: 6446 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
13515 : : {
13516 : 6238 : GElf_Shdr shdr_mem;
13517 : 6238 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
13518 : :
13519 [ + - + + ]: 6238 : if (shdr == NULL || shdr->sh_type != SHT_NOTE)
13520 : : /* Not what we are looking for. */
13521 : 5756 : continue;
13522 : :
13523 [ - + ]: 482 : if (notes_section != NULL)
13524 : : {
13525 : 0 : char *sname = elf_strptr (ebl->elf, shstrndx, shdr->sh_name);
13526 [ # # # # ]: 0 : if (sname == NULL || strcmp (sname, notes_section) != 0)
13527 : 0 : continue;
13528 : : }
13529 : :
13530 : 482 : printf (_("\
13531 : : \nNote section [%2zu] '%s' of %" PRIu64 " bytes at offset %#0" PRIx64 ":\n"),
13532 : : elf_ndxscn (scn),
13533 : 482 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
13534 : : shdr->sh_size, shdr->sh_offset);
13535 : :
13536 : 482 : handle_notes_data (ebl, ehdr, shdr->sh_offset,
13537 : : elf_getdata (scn, NULL));
13538 : : }
13539 : 208 : return;
13540 : : }
13541 : :
13542 : : /* We have to look through the program header to find the note
13543 : : sections. There can be more than one. */
13544 [ + + ]: 280 : for (size_t cnt = 0; cnt < phnum; ++cnt)
13545 : : {
13546 : 258 : GElf_Phdr mem;
13547 : 258 : GElf_Phdr *phdr = gelf_getphdr (ebl->elf, cnt, &mem);
13548 : :
13549 [ + - + + ]: 258 : if (phdr == NULL || phdr->p_type != PT_NOTE)
13550 : : /* Not what we are looking for. */
13551 : 234 : continue;
13552 : :
13553 : 24 : printf (_("\
13554 : : \nNote segment of %" PRIu64 " bytes at offset %#0" PRIx64 ":\n"),
13555 : : phdr->p_filesz, phdr->p_offset);
13556 : :
13557 : 24 : handle_notes_data (ebl, ehdr, phdr->p_offset,
13558 : : elf_getdata_rawchunk (ebl->elf,
13559 : 24 : phdr->p_offset, phdr->p_filesz,
13560 [ + + ]: 24 : (phdr->p_align == 8
13561 : : ? ELF_T_NHDR8 : ELF_T_NHDR)));
13562 : : }
13563 : : }
13564 : :
13565 : :
13566 : : static void
13567 : 12 : hex_dump (const uint8_t *data, size_t len)
13568 : : {
13569 : 12 : size_t pos = 0;
13570 [ + + ]: 56 : while (pos < len)
13571 : : {
13572 : 44 : printf (" 0x%08zx ", pos);
13573 : :
13574 : 44 : const size_t chunk = MIN (len - pos, 16);
13575 : :
13576 [ + + ]: 688 : for (size_t i = 0; i < chunk; ++i)
13577 [ + + ]: 644 : if (i % 4 == 3)
13578 : 160 : printf ("%02x ", data[pos + i]);
13579 : : else
13580 : 484 : printf ("%02x", data[pos + i]);
13581 : :
13582 [ + + ]: 44 : if (chunk < 16)
13583 : 4 : printf ("%*s", (int) ((16 - chunk) * 2 + (16 - chunk + 3) / 4), "");
13584 : :
13585 [ + + ]: 688 : for (size_t i = 0; i < chunk; ++i)
13586 : : {
13587 : 644 : unsigned char b = data[pos + i];
13588 [ + + ]: 1288 : printf ("%c", isprint (b) ? b : '.');
13589 : : }
13590 : :
13591 : 44 : putchar ('\n');
13592 : 44 : pos += chunk;
13593 : : }
13594 : 12 : }
13595 : :
13596 : : static void
13597 : 12 : dump_data_section (Elf_Scn *scn, const GElf_Shdr *shdr, const char *name)
13598 : : {
13599 [ + - - + ]: 12 : if (shdr->sh_size == 0 || shdr->sh_type == SHT_NOBITS)
13600 : 0 : printf (_("\nSection [%zu] '%s' has no data to dump.\n"),
13601 : : elf_ndxscn (scn), name);
13602 : : else
13603 : : {
13604 [ + + ]: 12 : if (print_decompress)
13605 : : {
13606 : : /* We try to decompress the section, but keep the old shdr around
13607 : : so we can show both the original shdr size and the uncompressed
13608 : : data size. */
13609 [ + + ]: 8 : if ((shdr->sh_flags & SHF_COMPRESSED) != 0)
13610 : : {
13611 [ - + ]: 4 : if (elf_compress (scn, 0, 0) < 0)
13612 : 0 : printf ("WARNING: %s [%zd]\n",
13613 : : _("Couldn't uncompress section"),
13614 : : elf_ndxscn (scn));
13615 : : }
13616 [ + - + - ]: 4 : else if (name && startswith (name, ".zdebug"))
13617 : : {
13618 [ - + ]: 4 : if (elf_compress_gnu (scn, 0, 0) < 0)
13619 : 0 : printf ("WARNING: %s [%zd]\n",
13620 : : _("Couldn't uncompress section"),
13621 : : elf_ndxscn (scn));
13622 : : }
13623 : : }
13624 : :
13625 : 12 : Elf_Data *data = elf_rawdata (scn, NULL);
13626 [ - + ]: 12 : if (data == NULL)
13627 : 0 : error (0, 0, _("cannot get data for section [%zu] '%s': %s"),
13628 : : elf_ndxscn (scn), name, elf_errmsg (-1));
13629 : : else
13630 : : {
13631 [ + + ]: 12 : if (data->d_size == shdr->sh_size)
13632 : 4 : printf (_("\nHex dump of section [%zu] '%s', %" PRIu64
13633 : : " bytes at offset %#0" PRIx64 ":\n"),
13634 : : elf_ndxscn (scn), name,
13635 : 4 : shdr->sh_size, shdr->sh_offset);
13636 : : else
13637 : 8 : printf (_("\nHex dump of section [%zu] '%s', %" PRIu64
13638 : : " bytes (%zd uncompressed) at offset %#0"
13639 : : PRIx64 ":\n"),
13640 : : elf_ndxscn (scn), name,
13641 : 8 : shdr->sh_size, data->d_size, shdr->sh_offset);
13642 : 12 : hex_dump (data->d_buf, data->d_size);
13643 : : }
13644 : : }
13645 : 12 : }
13646 : :
13647 : : static void
13648 : 374 : print_string_section (Elf_Scn *scn, const GElf_Shdr *shdr, const char *name)
13649 : : {
13650 [ + - + + ]: 374 : if (shdr->sh_size == 0 || shdr->sh_type == SHT_NOBITS)
13651 : 42 : printf (_("\nSection [%zu] '%s' has no strings to dump.\n"),
13652 : : elf_ndxscn (scn), name);
13653 : : else
13654 : : {
13655 [ + + ]: 332 : if (print_decompress)
13656 : : {
13657 : : /* We try to decompress the section, but keep the old shdr around
13658 : : so we can show both the original shdr size and the uncompressed
13659 : : data size. */
13660 [ - + ]: 2 : if ((shdr->sh_flags & SHF_COMPRESSED) != 0)
13661 : : {
13662 [ # # ]: 0 : if (elf_compress (scn, 0, 0) < 0)
13663 : 0 : printf ("WARNING: %s [%zd]\n",
13664 : : _("Couldn't uncompress section"),
13665 : : elf_ndxscn (scn));
13666 : : }
13667 [ + - + - ]: 2 : else if (name && startswith (name, ".zdebug"))
13668 : : {
13669 [ - + ]: 2 : if (elf_compress_gnu (scn, 0, 0) < 0)
13670 : 0 : printf ("WARNING: %s [%zd]\n",
13671 : : _("Couldn't uncompress section"),
13672 : : elf_ndxscn (scn));
13673 : : }
13674 : : }
13675 : :
13676 : 332 : Elf_Data *data = elf_rawdata (scn, NULL);
13677 [ - + ]: 332 : if (data == NULL)
13678 : 0 : error (0, 0, _("cannot get data for section [%zu] '%s': %s"),
13679 : : elf_ndxscn (scn), name, elf_errmsg (-1));
13680 : : else
13681 : : {
13682 [ + + ]: 332 : if (data->d_size == shdr->sh_size)
13683 : 330 : printf (_("\nString section [%zu] '%s' contains %" PRIu64
13684 : : " bytes at offset %#0" PRIx64 ":\n"),
13685 : : elf_ndxscn (scn), name,
13686 : 330 : shdr->sh_size, shdr->sh_offset);
13687 : : else
13688 : 2 : printf (_("\nString section [%zu] '%s' contains %" PRIu64
13689 : : " bytes (%zd uncompressed) at offset %#0"
13690 : : PRIx64 ":\n"),
13691 : : elf_ndxscn (scn), name,
13692 : 2 : shdr->sh_size, data->d_size, shdr->sh_offset);
13693 : :
13694 : 332 : const char *start = data->d_buf;
13695 : 332 : const char *const limit = start + data->d_size;
13696 : 47014 : do
13697 : : {
13698 : 47014 : const char *end = memchr (start, '\0', limit - start);
13699 : 47014 : const size_t pos = start - (const char *) data->d_buf;
13700 [ - + ]: 47014 : if (unlikely (end == NULL))
13701 : : {
13702 : 0 : printf (" [%6zx]- %.*s\n",
13703 : : pos, (int) (limit - start), start);
13704 : 0 : break;
13705 : : }
13706 : 47014 : printf (" [%6zx] %s\n", pos, start);
13707 : 47014 : start = end + 1;
13708 [ + + ]: 47014 : } while (start < limit);
13709 : : }
13710 : : }
13711 : 374 : }
13712 : :
13713 : : static void
13714 : 198 : for_each_section_argument (Elf *elf, const struct section_argument *list,
13715 : : void (*dump) (Elf_Scn *scn, const GElf_Shdr *shdr,
13716 : : const char *name))
13717 : : {
13718 : : /* Get the section header string table index. */
13719 : 198 : size_t shstrndx;
13720 [ - + ]: 198 : if (elf_getshdrstrndx (elf, &shstrndx) < 0)
13721 : 0 : error_exit (0, _("cannot get section header string table index"));
13722 : :
13723 [ + + ]: 764 : for (const struct section_argument *a = list; a != NULL; a = a->next)
13724 : : {
13725 : 566 : Elf_Scn *scn;
13726 : 566 : GElf_Shdr shdr_mem;
13727 : 566 : const char *name = NULL;
13728 : :
13729 : 566 : char *endp = NULL;
13730 : 566 : unsigned long int shndx = strtoul (a->arg, &endp, 0);
13731 [ + + - + ]: 566 : if (endp != a->arg && *endp == '\0')
13732 : : {
13733 : 2 : scn = elf_getscn (elf, shndx);
13734 [ - + ]: 2 : if (scn == NULL)
13735 : : {
13736 : 0 : error (0, 0, _("\nsection [%lu] does not exist"), shndx);
13737 : 0 : continue;
13738 : : }
13739 : :
13740 [ - + ]: 2 : if (gelf_getshdr (scn, &shdr_mem) == NULL)
13741 : 0 : error_exit (0, _("cannot get section header: %s"),
13742 : : elf_errmsg (-1));
13743 : 2 : name = elf_strptr (elf, shstrndx, shdr_mem.sh_name);
13744 : 2 : (*dump) (scn, &shdr_mem, name);
13745 : : }
13746 : : else
13747 : : {
13748 : : /* Need to look up the section by name. */
13749 : : scn = NULL;
13750 : : bool found = false;
13751 [ + + ]: 18180 : while ((scn = elf_nextscn (elf, scn)) != NULL)
13752 : : {
13753 [ - + ]: 17616 : if (gelf_getshdr (scn, &shdr_mem) == NULL)
13754 : 0 : continue;
13755 : 17616 : name = elf_strptr (elf, shstrndx, shdr_mem.sh_name);
13756 [ - + ]: 17616 : if (name == NULL)
13757 : 0 : continue;
13758 [ + + ]: 17616 : if (!strcmp (name, a->arg))
13759 : : {
13760 : 384 : found = true;
13761 : 384 : (*dump) (scn, &shdr_mem, name);
13762 : : }
13763 : : }
13764 : :
13765 [ + + - + ]: 564 : if (unlikely (!found) && !a->implicit)
13766 : 0 : error (0, 0, _("\nsection '%s' does not exist"), a->arg);
13767 : : }
13768 : : }
13769 : 198 : }
13770 : :
13771 : : static void
13772 : 12 : dump_data (Ebl *ebl)
13773 : : {
13774 : 12 : for_each_section_argument (ebl->elf, dump_data_sections, &dump_data_section);
13775 : 12 : }
13776 : :
13777 : : static void
13778 : 186 : dump_strings (Ebl *ebl)
13779 : : {
13780 : 186 : for_each_section_argument (ebl->elf, string_sections, &print_string_section);
13781 : 186 : }
13782 : :
13783 : : static void
13784 : 0 : print_strings (Ebl *ebl)
13785 : : {
13786 : : /* Get the section header string table index. */
13787 : 0 : size_t shstrndx;
13788 [ # # ]: 0 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
13789 : 0 : error_exit (0, _("cannot get section header string table index"));
13790 : :
13791 : : Elf_Scn *scn;
13792 : : GElf_Shdr shdr_mem;
13793 : : const char *name;
13794 : : scn = NULL;
13795 [ # # ]: 0 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
13796 : : {
13797 [ # # ]: 0 : if (gelf_getshdr (scn, &shdr_mem) == NULL)
13798 : 0 : continue;
13799 : :
13800 [ # # ]: 0 : if (shdr_mem.sh_type != SHT_PROGBITS
13801 [ # # ]: 0 : || !(shdr_mem.sh_flags & SHF_STRINGS))
13802 : 0 : continue;
13803 : :
13804 : 0 : name = elf_strptr (ebl->elf, shstrndx, shdr_mem.sh_name);
13805 [ # # ]: 0 : if (name == NULL)
13806 : 0 : continue;
13807 : :
13808 : 0 : print_string_section (scn, &shdr_mem, name);
13809 : : }
13810 : 0 : }
13811 : :
13812 : : static void
13813 : 4 : dump_archive_index (Elf *elf, const char *fname)
13814 : : {
13815 : 4 : size_t narsym;
13816 : 4 : const Elf_Arsym *arsym = elf_getarsym (elf, &narsym);
13817 [ - + ]: 4 : if (arsym == NULL)
13818 : : {
13819 : 0 : int result = elf_errno ();
13820 [ # # ]: 0 : if (unlikely (result != ELF_E_NO_INDEX))
13821 : 0 : error_exit (0, _("cannot get symbol index of archive '%s': %s"),
13822 : : fname, elf_errmsg (result));
13823 : : else
13824 : 0 : printf (_("\nArchive '%s' has no symbol index\n"), fname);
13825 : 0 : return;
13826 : : }
13827 : :
13828 : 4 : printf (_("\nIndex of archive '%s' has %zu entries:\n"),
13829 : : fname, narsym);
13830 : :
13831 : 4 : size_t as_off = 0;
13832 [ + + ]: 22 : for (const Elf_Arsym *s = arsym; s < &arsym[narsym - 1]; ++s)
13833 : : {
13834 [ + + ]: 18 : if (s->as_off != as_off)
13835 : : {
13836 : 12 : as_off = s->as_off;
13837 : :
13838 : 12 : Elf *subelf = NULL;
13839 [ + - ]: 12 : if (unlikely (elf_rand (elf, as_off) == 0)
13840 [ - + ]: 12 : || unlikely ((subelf = elf_begin (-1, ELF_C_READ_MMAP, elf))
13841 : : == NULL))
13842 : : #if __GLIBC__ < 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ < 7)
13843 : : while (1)
13844 : : #endif
13845 : 0 : error_exit (0,
13846 : : _("cannot extract member at offset %zu in '%s': %s"),
13847 : : as_off, fname, elf_errmsg (-1));
13848 : :
13849 : 12 : const Elf_Arhdr *h = elf_getarhdr (subelf);
13850 [ + - ]: 12 : if (h != NULL)
13851 : 12 : printf (_("Archive member '%s' contains:\n"), h->ar_name);
13852 : :
13853 : 12 : elf_end (subelf);
13854 : : }
13855 : :
13856 : 18 : printf ("\t%s\n", s->as_name);
13857 : : }
13858 : : }
13859 : :
13860 : : #include "debugpred.h"
|