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 : : static char *empty_block_str;
393 : : static char *byte_block_str;
394 : : static char *offset_str;
395 : : static char *children_str;
396 : : static char *tag_str;
397 : : static char *length_str;
398 : : static char *dwarf_ver_str;
399 : : static char *cu_offset_str;
400 : : static char *seg_size_str;
401 : : static char *table_offset_lower_str;
402 : : static char *table_offset_upper_str;
403 : : static char *addr_size_str;
404 : : static char *offset_ent_str;
405 : : static char *cu_str;
406 : : static char *base_str;
407 : : static char *type_unit_str;
408 : : static char *version_str;
409 : : static char *abbrev_offset_str;
410 : : static char *addr_size_str;
411 : : static char *offset_size_str;
412 : : static char *type_sig_str;
413 : : static char *type_offset_str;
414 : : static char *cu_offset_at_str;
415 : : static char *unit_type_str;
416 : : static char *unit_id_str;
417 : : static char *unit_die_off_str;
418 : : static char *prologue_len_str;
419 : : static char *seg_selector_str;
420 : : static char *min_inst_len_str;
421 : : static char *max_op_per_inst_str;
422 : : static char *initial_value_if_str;
423 : : static char *line_base_str;
424 : : static char *line_range_str;
425 : : static char *opcodes_base_str;
426 : : static char *opcodes_str;
427 : : static char *special_opcode_str;
428 : : static char *address_str;
429 : : static char *line_str;
430 : : static char *ext_opcode_str;
431 : : static char *set_disc_str;
432 : : static char *adv_addr_str;
433 : : static char *to_str;
434 : : static char *set_file_str;
435 : : static char *set_col_str;
436 : : static char *set_str;
437 : : static char *adv_line_by_str;
438 : : static char *adv_addr_by_str;
439 : : static char *unknown_base_str;
440 : : static char *unknown_base_str;
441 : : static char *dir_table_str;
442 : : static char *file_table_str;
443 : : static char *no_line_num_stmts_str;
444 : : static char *line_num_stmts_str;
445 : : static char *end_of_seq_str;
446 : : static char *set_addr_to_str;
447 : : static char *copy_str;
448 : :
449 : :
450 : : static void
451 : 890 : init_gettext_strs (void)
452 : : {
453 : 890 : yes_str = _("yes");
454 : 890 : no_str = _("no");
455 : 890 : empty_block_str = _("empty block");
456 : 890 : byte_block_str = _("byte block");
457 : 890 : offset_str = _("offset");
458 : 890 : children_str = _("children");
459 : 890 : tag_str = _("tag");
460 : 890 : table_offset_lower_str = _("Table at offset");
461 : 890 : table_offset_upper_str = _("Table at Offset");
462 : 890 : length_str = _("Length");
463 : 890 : dwarf_ver_str = _("DWARF version");
464 : 890 : cu_offset_str = _("CU offset");
465 : 890 : addr_size_str = _("Address size");
466 : 890 : seg_size_str = _("Segment size");
467 : 890 : offset_ent_str = _("Offset entries");
468 : 890 : cu_str = _("CU");
469 : 890 : base_str = _("base");
470 : 890 : type_unit_str = _("Type unit at offset");
471 : 890 : version_str = _("Version");
472 : 890 : abbrev_offset_str = _("Abbreviation section offset");
473 : 890 : offset_size_str = _("Offset size");
474 : 890 : type_sig_str = _("Type signature");
475 : 890 : type_offset_str = _("Type offset");
476 : 890 : cu_offset_at_str = _("Compilation unit at offset");
477 : 890 : unit_type_str = _("Unit type");
478 : 890 : unit_id_str = _("Unit id");
479 : 890 : unit_die_off_str = _("Unit DIE off");
480 : 890 : prologue_len_str = _("Prologue length");
481 : 890 : seg_selector_str = _("Segment selector size");
482 : 890 : min_inst_len_str = _("Min instruction length");
483 : 890 : max_op_per_inst_str = _("Max operations per instruction");
484 : 890 : initial_value_if_str = _("Initial value if");
485 : 890 : line_base_str = _("Line base");
486 : 890 : line_range_str = _("Line range");
487 : 890 : opcodes_base_str = _("Opcode base");
488 : 890 : opcodes_str = _("Opcodes");
489 : 890 : special_opcode_str = _("special opcode");
490 : 890 : address_str = _("address");
491 : 890 : line_str = _("line");
492 : 890 : ext_opcode_str = _("extended opcode");
493 : 890 : set_disc_str = _("set discriminator to");
494 : 890 : adv_addr_str = _("advance address by");
495 : 890 : to_str = _("to");
496 : 890 : set_file_str = _("set file to");
497 : 890 : set_col_str = _("set column to");
498 : 890 : set_str = _("set");
499 : 890 : adv_addr_by_str = _("advance address by constant");
500 : 890 : adv_line_by_str = _("advance line by constant");
501 : 890 : unknown_base_str = _("Unknown CU base");
502 : 890 : dir_table_str = _("Directory table");
503 : 890 : file_table_str = _("File name table");
504 : 890 : no_line_num_stmts_str = _("No line number statements");
505 : 890 : line_num_stmts_str = _("Line number statements");
506 : 890 : end_of_seq_str = _("end of sequence");
507 : 890 : set_addr_to_str = _("set address to");
508 : 890 : copy_str = _("copy");
509 : 890 : }
510 : :
511 : :
512 : : static void
513 : 1780 : cleanup_list (struct section_argument *list)
514 : : {
515 [ + + + + ]: 2346 : while (list != NULL)
516 : : {
517 : 566 : struct section_argument *a = list;
518 : 566 : list = a->next;
519 : 566 : free (a);
520 : : }
521 : : }
522 : :
523 : : #ifdef USE_LOCKS
524 : : /* Estimate the maximum number of threads. This is normally
525 : : #CPU. Return value is guaranteed to be at least 1. */
526 : : static int
527 : : default_concurrency (void)
528 : : {
529 : : unsigned aff = 0;
530 : : #ifdef HAVE_SCHED_GETAFFINITY
531 : : {
532 : : int ret;
533 : : cpu_set_t mask;
534 : : CPU_ZERO (&mask);
535 : : ret = sched_getaffinity (0, sizeof(mask), &mask);
536 : : if (ret == 0)
537 : : aff = CPU_COUNT (&mask);
538 : : }
539 : : #endif
540 : :
541 : : unsigned fn = 0;
542 : : #ifdef HAVE_GETRLIMIT
543 : : {
544 : : struct rlimit rlim;
545 : : int rc = getrlimit (RLIMIT_NOFILE, &rlim);
546 : : if (rc == 0)
547 : : fn = MAX ((rlim_t) 1, (rlim.rlim_cur - 100) / 2);
548 : : /* Conservatively estimate that at least 2 fds are used
549 : : by each thread. */
550 : : }
551 : : #endif
552 : :
553 : : unsigned d = MIN (MAX (aff, 1U),
554 : : MAX (fn, 1U));
555 : :
556 : : return d;
557 : : }
558 : : #endif
559 : :
560 : : int
561 : 890 : main (int argc, char *argv[])
562 : : {
563 : : /* We use no threads here which can interfere with handling a stream. */
564 : 890 : (void) __fsetlocking (stdout, FSETLOCKING_BYCALLER);
565 : :
566 : : /* Set locale. */
567 : 890 : setlocale (LC_ALL, "");
568 : :
569 : : /* Initialize the message catalog. */
570 : 890 : textdomain (PACKAGE_TARNAME);
571 : :
572 : : /* Look up once. */
573 : 890 : init_gettext_strs ();
574 : :
575 : : /* Parse and process arguments. */
576 : 890 : int remaining;
577 : 890 : argp_parse (&argp, argc, argv, 0, &remaining, NULL);
578 : :
579 : : /* Before we start tell the ELF library which version we are using. */
580 : 890 : elf_version (EV_CURRENT);
581 : :
582 : : #ifdef USE_LOCKS
583 : : /* If concurrency wasn't set by argp_parse, then set a default value. */
584 : : if (max_threads == 0)
585 : : max_threads = default_concurrency ();
586 : : #endif
587 : :
588 : : /* Now process all the files given at the command line. */
589 : 890 : bool only_one = remaining + 1 == argc;
590 : 972 : do
591 : : {
592 : : /* Open the file. */
593 : 972 : int fd = open (argv[remaining], O_RDONLY);
594 [ - + ]: 972 : if (fd == -1)
595 : : {
596 : 0 : error (0, errno, _("cannot open input file '%s'"), argv[remaining]);
597 : 0 : continue;
598 : : }
599 : :
600 : 972 : process_file (fd, argv[remaining], only_one);
601 : :
602 : 972 : close (fd);
603 : : }
604 [ + + ]: 972 : while (++remaining < argc);
605 : :
606 : 890 : cleanup_list (dump_data_sections);
607 : 890 : cleanup_list (string_sections);
608 : :
609 : 890 : return error_message_count != 0;
610 : : }
611 : :
612 : : static void
613 : 566 : add_dump_section (const char *name,
614 : : int key,
615 : : bool implicit)
616 : : {
617 : 566 : struct section_argument *a = xmalloc (sizeof *a);
618 : 566 : a->arg = name;
619 : 566 : a->next = NULL;
620 : 566 : a->implicit = implicit;
621 : 1132 : struct section_argument ***tailp
622 [ + + ]: 566 : = key == 'x' ? &dump_data_sections_tail : &string_sections_tail;
623 : 566 : **tailp = a;
624 : 566 : *tailp = &a->next;
625 : 566 : }
626 : :
627 : : /* Handle program arguments. */
628 : : static error_t
629 : 5662 : parse_opt (int key, char *arg,
630 : : struct argp_state *state __attribute__ ((unused)))
631 : : {
632 [ + + - + : 5662 : switch (key)
+ - + + -
+ + + + +
+ - + + +
+ + + - +
+ + + + ]
633 : : {
634 : 184 : case 'a':
635 : 184 : print_file_header = true;
636 : 184 : print_program_header = true;
637 : 184 : print_relocations = true;
638 : 184 : print_section_header = true;
639 : 184 : print_symbol_table = true;
640 : 184 : print_version_info = true;
641 : 184 : print_dynamic_table = true;
642 : 184 : print_section_groups = true;
643 : 184 : print_histogram = true;
644 : 184 : print_arch = true;
645 : 184 : print_notes = true;
646 : 184 : implicit_debug_sections |= section_exception;
647 : 184 : add_dump_section (".strtab", key, true);
648 : 184 : add_dump_section (".dynstr", key, true);
649 : 184 : add_dump_section (".comment", key, true);
650 : 184 : any_control_option = true;
651 : 184 : break;
652 : 8 : case 'A':
653 : 8 : print_arch = true;
654 : 8 : any_control_option = true;
655 : 8 : break;
656 : 6 : case 'd':
657 : 6 : print_dynamic_table = true;
658 : 6 : any_control_option = true;
659 : 6 : break;
660 : 2 : case 'D':
661 : 2 : use_dynamic_segment = true;
662 : 2 : break;
663 : 0 : case 'e':
664 : 0 : print_debug_sections |= section_exception;
665 : 0 : any_control_option = true;
666 : 0 : break;
667 : 18 : case 'g':
668 : 18 : print_section_groups = true;
669 : 18 : any_control_option = true;
670 : 18 : break;
671 : 6 : case 'h':
672 : 6 : print_file_header = true;
673 : 6 : any_control_option = true;
674 : 6 : break;
675 : 0 : case 'I':
676 : 0 : print_histogram = true;
677 : 0 : any_control_option = true;
678 : 0 : break;
679 : 4 : case 'l':
680 : 4 : print_program_header = true;
681 : 4 : any_control_option = true;
682 : 4 : break;
683 : 46 : case 'n':
684 : 46 : print_notes = true;
685 : 46 : any_control_option = true;
686 : 46 : notes_section = arg;
687 : 46 : break;
688 : 10 : case 'r':
689 : 10 : print_relocations = true;
690 : 10 : any_control_option = true;
691 : 10 : break;
692 : 322 : case 'S':
693 : 322 : print_section_header = true;
694 : 322 : any_control_option = true;
695 : 322 : break;
696 : 34 : case 's':
697 : 34 : print_symbol_table = true;
698 : 34 : any_control_option = true;
699 : 34 : symbol_table_section = arg;
700 : 34 : break;
701 : 2 : case PRINT_DYNSYM_TABLE:
702 : 2 : print_dynsym_table = true;
703 : 2 : any_control_option = true;
704 : 2 : break;
705 : 0 : case 'V':
706 : 0 : print_version_info = true;
707 : 0 : any_control_option = true;
708 : 0 : break;
709 : 4 : case 'c':
710 : 4 : print_archive_index = true;
711 : 4 : break;
712 : : #if USE_LOCKS
713 : : case 'C':
714 : : if (arg != NULL)
715 : : {
716 : : max_threads = atoi (arg);
717 : : if (max_threads < 1)
718 : : {
719 : : argp_error (state, _("-C NUM minimum 1"));
720 : : return EINVAL;
721 : : }
722 : : }
723 : : break;
724 : : #endif
725 : 264 : case 'w':
726 [ + + ]: 264 : if (arg == NULL)
727 : : {
728 : 86 : print_debug_sections = section_all;
729 : 86 : implicit_debug_sections = section_info;
730 : 86 : show_split_units = true;
731 : : }
732 [ - + ]: 178 : else if (strcmp (arg, "abbrev") == 0)
733 : 0 : print_debug_sections |= section_abbrev;
734 [ + + ]: 178 : else if (strcmp (arg, "addr") == 0)
735 : : {
736 : 4 : print_debug_sections |= section_addr;
737 : 4 : implicit_debug_sections |= section_info;
738 : : }
739 [ + + ]: 174 : else if (strcmp (arg, "aranges") == 0)
740 : 6 : print_debug_sections |= section_aranges;
741 [ + + ]: 168 : else if (strcmp (arg, "decodedaranges") == 0)
742 : : {
743 : 2 : print_debug_sections |= section_aranges;
744 : 2 : decodedaranges = true;
745 : : }
746 [ + + ]: 166 : else if (strcmp (arg, "ranges") == 0)
747 : : {
748 : 26 : print_debug_sections |= section_ranges;
749 : 26 : implicit_debug_sections |= section_info;
750 : : }
751 [ + + + + ]: 140 : else if (strcmp (arg, "frame") == 0 || strcmp (arg, "frames") == 0)
752 : 6 : print_debug_sections |= section_frame;
753 [ + + ]: 134 : else if (strcmp (arg, "info") == 0)
754 : : {
755 : 40 : print_debug_sections |= section_info;
756 : 40 : print_debug_sections |= section_types;
757 : : }
758 [ + + ]: 94 : else if (strcmp (arg, "info+") == 0)
759 : : {
760 : 4 : print_debug_sections |= section_info;
761 : 4 : print_debug_sections |= section_types;
762 : 4 : show_split_units = true;
763 : : }
764 [ + + ]: 90 : else if (strcmp (arg, "loc") == 0)
765 : : {
766 : 38 : print_debug_sections |= section_loc;
767 : 38 : implicit_debug_sections |= section_info;
768 : : }
769 [ + + ]: 52 : else if (strcmp (arg, "line") == 0)
770 : 16 : print_debug_sections |= section_line;
771 [ + + ]: 36 : else if (strcmp (arg, "decodedline") == 0)
772 : : {
773 : 14 : print_debug_sections |= section_line;
774 : 14 : decodedline = true;
775 : : }
776 [ - + ]: 22 : else if (strcmp (arg, "pubnames") == 0)
777 : 0 : print_debug_sections |= section_pubnames;
778 [ + + ]: 22 : else if (strcmp (arg, "str") == 0)
779 : : {
780 : 6 : print_debug_sections |= section_str;
781 : : /* For mapping string offset tables to CUs. */
782 : 6 : implicit_debug_sections |= section_info;
783 : : }
784 [ - + ]: 16 : else if (strcmp (arg, "macinfo") == 0)
785 : 0 : print_debug_sections |= section_macinfo;
786 [ + + ]: 16 : else if (strcmp (arg, "macro") == 0)
787 : 8 : print_debug_sections |= section_macro;
788 [ - + ]: 8 : else if (strcmp (arg, "exception") == 0)
789 : 0 : print_debug_sections |= section_exception;
790 [ + - ]: 8 : else if (strcmp (arg, "gdb_index") == 0)
791 : 8 : print_debug_sections |= section_gdb_index;
792 : : else
793 : : {
794 : 0 : fprintf (stderr, _("Unknown DWARF debug section `%s'.\n"),
795 : : arg);
796 : 0 : argp_help (&argp, stderr, ARGP_HELP_SEE,
797 : : program_invocation_short_name);
798 : 0 : exit (1);
799 : : }
800 : 264 : any_control_option = true;
801 : 264 : break;
802 : 2 : case 'p':
803 : 2 : any_control_option = true;
804 [ - + ]: 2 : if (arg == NULL)
805 : : {
806 : 0 : print_string_sections = true;
807 : 0 : break;
808 : : }
809 : 14 : FALLTHROUGH;
810 : : case 'x':
811 : 14 : add_dump_section (arg, key, false);
812 : 14 : any_control_option = true;
813 : 14 : break;
814 : 90 : case 'N':
815 : 90 : print_address_names = false;
816 : 90 : break;
817 : 56 : case 'U':
818 : 56 : print_unresolved_addresses = true;
819 : 56 : break;
820 : 0 : case ARGP_KEY_NO_ARGS:
821 : 0 : fputs (_("Missing file name.\n"), stderr);
822 : 0 : goto do_argp_help;
823 : 890 : case ARGP_KEY_FINI:
824 [ + + + - ]: 890 : if (! any_control_option && ! print_archive_index)
825 : : {
826 : 0 : fputs (_("No operation specified.\n"), stderr);
827 : 0 : do_argp_help:
828 : 0 : argp_help (&argp, stderr, ARGP_HELP_SEE,
829 : : program_invocation_short_name);
830 : 0 : exit (EXIT_FAILURE);
831 : : }
832 : : break;
833 : : case 'W': /* Ignored. */
834 : : break;
835 : 122 : case 'z':
836 : 122 : print_decompress = true;
837 : 122 : break;
838 : 10 : case ELF_INPUT_SECTION:
839 [ + - ]: 10 : if (arg == NULL)
840 : 10 : elf_input_section = ".gnu_debugdata";
841 : : else
842 : 0 : elf_input_section = arg;
843 : : break;
844 : 10 : case DWARF_SKELETON:
845 : 10 : dwarf_skeleton = arg;
846 : 10 : break;
847 : : default:
848 : : return ARGP_ERR_UNKNOWN;
849 : : }
850 : : return 0;
851 : : }
852 : :
853 : :
854 : : /* Create a file descriptor to read the data from the
855 : : elf_input_section given a file descriptor to an ELF file. */
856 : : static int
857 : 10 : open_input_section (int fd)
858 : : {
859 : 10 : size_t shnums;
860 : 10 : size_t cnt;
861 : 10 : size_t shstrndx;
862 : 10 : Elf *elf = elf_begin (fd, ELF_C_READ_MMAP, NULL);
863 [ - + ]: 10 : if (elf == NULL)
864 : : {
865 : 0 : error (0, 0, _("cannot generate Elf descriptor: %s"),
866 : : elf_errmsg (-1));
867 : 0 : return -1;
868 : : }
869 : :
870 [ - + ]: 10 : if (elf_getshdrnum (elf, &shnums) < 0)
871 : : {
872 : 0 : error (0, 0, _("cannot determine number of sections: %s"),
873 : : elf_errmsg (-1));
874 : 0 : open_error:
875 : 0 : elf_end (elf);
876 : 0 : return -1;
877 : : }
878 : :
879 [ - + ]: 10 : if (elf_getshdrstrndx (elf, &shstrndx) < 0)
880 : : {
881 : 0 : error (0, 0, _("cannot get section header string table index"));
882 : 0 : goto open_error;
883 : : }
884 : :
885 [ + - ]: 236 : for (cnt = 0; cnt < shnums; ++cnt)
886 : : {
887 : 236 : Elf_Scn *scn = elf_getscn (elf, cnt);
888 [ - + ]: 236 : if (scn == NULL)
889 : : {
890 : 0 : error (0, 0, _("cannot get section: %s"),
891 : : elf_errmsg (-1));
892 : 0 : goto open_error;
893 : : }
894 : :
895 : 236 : GElf_Shdr shdr_mem;
896 : 236 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
897 [ - + ]: 236 : if (unlikely (shdr == NULL))
898 : : {
899 : 0 : error (0, 0, _("cannot get section header: %s"),
900 : : elf_errmsg (-1));
901 : 0 : goto open_error;
902 : : }
903 : :
904 : 236 : const char *sname = elf_strptr (elf, shstrndx, shdr->sh_name);
905 [ - + ]: 236 : if (sname == NULL)
906 : : {
907 : 0 : error (0, 0, _("cannot get section name"));
908 : 0 : goto open_error;
909 : : }
910 : :
911 [ + + ]: 236 : if (strcmp (sname, elf_input_section) == 0)
912 : : {
913 : 10 : Elf_Data *data = elf_rawdata (scn, NULL);
914 [ - + ]: 10 : if (data == NULL)
915 : : {
916 : 0 : error (0, 0, _("cannot get %s content: %s"),
917 : : sname, elf_errmsg (-1));
918 : 0 : goto open_error;
919 : : }
920 : :
921 : : /* Create (and immediately unlink) a temporary file to store
922 : : section data in to create a file descriptor for it. */
923 [ + - ]: 10 : const char *tmpdir = getenv ("TMPDIR") ?: P_tmpdir;
924 : 10 : static const char suffix[] = "/readelfXXXXXX";
925 : 10 : int tmplen = strlen (tmpdir) + sizeof (suffix);
926 : 10 : char *tempname = alloca (tmplen);
927 : 10 : sprintf (tempname, "%s%s", tmpdir, suffix);
928 : :
929 : 10 : int sfd = mkstemp (tempname);
930 [ - + ]: 10 : if (sfd == -1)
931 : : {
932 : 0 : error (0, 0, _("cannot create temp file '%s'"),
933 : : tempname);
934 : 0 : goto open_error;
935 : : }
936 : 10 : unlink (tempname);
937 : :
938 : 10 : ssize_t size = data->d_size;
939 [ - + ]: 10 : if (write_retry (sfd, data->d_buf, size) != size)
940 : : {
941 : 0 : error (0, 0, _("cannot write section data"));
942 : 0 : goto open_error;
943 : : }
944 : :
945 [ - + ]: 10 : if (elf_end (elf) != 0)
946 : : {
947 : 0 : error (0, 0, _("error while closing Elf descriptor: %s"),
948 : : elf_errmsg (-1));
949 : 10 : return -1;
950 : : }
951 : :
952 [ - + ]: 10 : if (lseek (sfd, 0, SEEK_SET) == -1)
953 : : {
954 : 0 : error (0, 0, _("error while rewinding file descriptor"));
955 : 0 : return -1;
956 : : }
957 : :
958 : : return sfd;
959 : : }
960 : : }
961 : :
962 : : /* Named section not found. */
963 [ # # ]: 0 : if (elf_end (elf) != 0)
964 : 0 : error (0, 0, _("error while closing Elf descriptor: %s"),
965 : : elf_errmsg (-1));
966 : : return -1;
967 : : }
968 : :
969 : : /* Check if the file is an archive, and if so dump its index. */
970 : : static void
971 : 4 : check_archive_index (int fd, const char *fname, bool only_one)
972 : : {
973 : : /* Create an `Elf' descriptor. */
974 : 4 : Elf *elf = elf_begin (fd, ELF_C_READ_MMAP, NULL);
975 [ - + ]: 4 : if (elf == NULL)
976 : 0 : error (0, 0, _("cannot generate Elf descriptor: %s"),
977 : : elf_errmsg (-1));
978 : : else
979 : : {
980 [ + - ]: 4 : if (elf_kind (elf) == ELF_K_AR)
981 : : {
982 [ - + ]: 4 : if (!only_one)
983 : 0 : printf ("\n%s:\n\n", fname);
984 : 4 : dump_archive_index (elf, fname);
985 : : }
986 : : else
987 : 0 : error (0, 0,
988 : 0 : _("'%s' is not an archive, cannot print archive index"),
989 : : fname);
990 : :
991 : : /* Now we can close the descriptor. */
992 [ - + ]: 4 : if (elf_end (elf) != 0)
993 : 0 : error (0, 0, _("error while closing Elf descriptor: %s"),
994 : : elf_errmsg (-1));
995 : : }
996 : 4 : }
997 : :
998 : : /* Trivial callback used for checking if we opened an archive. */
999 : : static int
1000 : 868 : count_dwflmod (Dwfl_Module *dwflmod __attribute__ ((unused)),
1001 : : void **userdata __attribute__ ((unused)),
1002 : : const char *name __attribute__ ((unused)),
1003 : : Dwarf_Addr base __attribute__ ((unused)),
1004 : : void *arg)
1005 : : {
1006 [ + - ]: 868 : if (*(bool *) arg)
1007 : : return DWARF_CB_ABORT;
1008 : 868 : *(bool *) arg = true;
1009 : 868 : return DWARF_CB_OK;
1010 : : }
1011 : :
1012 : : struct process_dwflmod_args
1013 : : {
1014 : : int fd;
1015 : : bool only_one;
1016 : : };
1017 : :
1018 : : static int
1019 : 968 : process_dwflmod (Dwfl_Module *dwflmod,
1020 : : void **userdata __attribute__ ((unused)),
1021 : : const char *name __attribute__ ((unused)),
1022 : : Dwarf_Addr base __attribute__ ((unused)),
1023 : : void *arg)
1024 : : {
1025 : 968 : const struct process_dwflmod_args *a = arg;
1026 : :
1027 : : /* Print the file name. */
1028 [ + + ]: 968 : if (!a->only_one)
1029 : : {
1030 : 100 : const char *fname;
1031 : 100 : dwfl_module_info (dwflmod, NULL, NULL, NULL, NULL, NULL, &fname, NULL);
1032 : :
1033 : 100 : printf ("\n%s:\n\n", fname);
1034 : : }
1035 : :
1036 : 968 : process_elf_file (dwflmod, a->fd);
1037 : :
1038 : 968 : return DWARF_CB_OK;
1039 : : }
1040 : :
1041 : : /* Stub libdwfl callback, only the ELF handle already open is ever used.
1042 : : Only used for finding the alternate debug file if the Dwarf comes from
1043 : : the main file. We are not interested in separate debuginfo. */
1044 : : static int
1045 : 262 : find_no_debuginfo (Dwfl_Module *mod,
1046 : : void **userdata,
1047 : : const char *modname,
1048 : : Dwarf_Addr base,
1049 : : const char *file_name,
1050 : : const char *debuglink_file,
1051 : : GElf_Word debuglink_crc,
1052 : : char **debuginfo_file_name)
1053 : : {
1054 : 262 : Dwarf_Addr dwbias;
1055 : 262 : dwfl_module_info (mod, NULL, NULL, NULL, &dwbias, NULL, NULL, NULL);
1056 : :
1057 : : /* We are only interested if the Dwarf has been setup on the main
1058 : : elf file but is only missing the alternate debug link. If dwbias
1059 : : hasn't even been setup, this is searching for separate debuginfo
1060 : : for the main elf. We don't care in that case. */
1061 [ + + ]: 262 : if (dwbias == (Dwarf_Addr) -1)
1062 : : return -1;
1063 : :
1064 : 70 : return dwfl_standard_find_debuginfo (mod, userdata, modname, base,
1065 : : file_name, debuglink_file,
1066 : : debuglink_crc, debuginfo_file_name);
1067 : : }
1068 : :
1069 : : static Dwfl *
1070 : 986 : create_dwfl (int fd, const char *fname)
1071 : : {
1072 : : /* Duplicate an fd for dwfl_report_offline to swallow. */
1073 : 986 : int dwfl_fd = dup (fd);
1074 [ - + ]: 986 : if (unlikely (dwfl_fd < 0))
1075 : 0 : error_exit (errno, "dup");
1076 : :
1077 : : /* Use libdwfl in a trivial way to open the libdw handle for us.
1078 : : This takes care of applying relocations to DWARF data in ET_REL files. */
1079 : 986 : static const Dwfl_Callbacks callbacks =
1080 : : {
1081 : : .section_address = dwfl_offline_section_address,
1082 : : .find_debuginfo = find_no_debuginfo
1083 : : };
1084 : 986 : Dwfl *dwfl = dwfl_begin (&callbacks);
1085 [ + - ]: 986 : if (likely (dwfl != NULL))
1086 : : /* Let 0 be the logical address of the file (or first in archive). */
1087 : 986 : dwfl->offline_next_address = 0;
1088 [ - + ]: 986 : if (dwfl_report_offline (dwfl, fname, fname, dwfl_fd) == NULL)
1089 : : {
1090 : 0 : struct stat st;
1091 [ # # ]: 0 : if (fstat (dwfl_fd, &st) != 0)
1092 : 0 : error (0, errno, _("cannot stat input file"));
1093 [ # # ]: 0 : else if (unlikely (st.st_size == 0))
1094 : 0 : error (0, 0, _("input file is empty"));
1095 : : else
1096 : 0 : error (0, 0, _("failed reading '%s': %s"),
1097 : : fname, dwfl_errmsg (-1));
1098 : 0 : close (dwfl_fd); /* Consumed on success, not on failure. */
1099 : 0 : dwfl_end (dwfl);
1100 : 0 : dwfl = NULL;
1101 : : }
1102 : : else
1103 : 986 : dwfl_report_end (dwfl, NULL, NULL);
1104 : :
1105 : 986 : return dwfl;
1106 : : }
1107 : :
1108 : : /* Process one input file. */
1109 : : static void
1110 : 972 : process_file (int fd, const char *fname, bool only_one)
1111 : : {
1112 [ + + ]: 972 : if (print_archive_index)
1113 : 4 : check_archive_index (fd, fname, only_one);
1114 : :
1115 [ + + ]: 972 : if (!any_control_option)
1116 : : return;
1117 : :
1118 [ + + ]: 968 : if (elf_input_section != NULL)
1119 : : {
1120 : : /* Replace fname and fd with section content. */
1121 : 10 : char *fnname = alloca (strlen (fname) + strlen (elf_input_section) + 2);
1122 : 10 : sprintf (fnname, "%s:%s", fname, elf_input_section);
1123 : 10 : fd = open_input_section (fd);
1124 [ - + ]: 10 : if (fd == -1)
1125 : : {
1126 : 0 : error (0, 0, _("No such section '%s' in '%s'"),
1127 : : elf_input_section, fname);
1128 : 0 : return;
1129 : : }
1130 : : fname = fnname;
1131 : : }
1132 : :
1133 : 968 : Dwfl *dwfl = create_dwfl (fd, fname);
1134 [ + - ]: 968 : if (dwfl != NULL)
1135 : : {
1136 [ + + ]: 968 : if (only_one)
1137 : : {
1138 : : /* Clear ONLY_ONE if we have multiple modules, from an archive. */
1139 : 868 : bool seen = false;
1140 : 868 : only_one = dwfl_getmodules (dwfl, &count_dwflmod, &seen, 0) == 0;
1141 : : }
1142 : :
1143 : : /* Process the one or more modules gleaned from this file. */
1144 : 968 : struct process_dwflmod_args a = { .fd = fd, .only_one = only_one };
1145 : 968 : dwfl_getmodules (dwfl, &process_dwflmod, &a, 0);
1146 : : }
1147 : : /* Terrible hack for hooking unrelated skeleton/split compile units,
1148 : : see __libdw_link_skel_split in print_debug. */
1149 [ + - ]: 968 : if (! do_not_close_dwfl)
1150 : 968 : dwfl_end (dwfl);
1151 : :
1152 : : /* Need to close the replaced fd if we created it. Caller takes
1153 : : care of original. */
1154 [ + + ]: 968 : if (elf_input_section != NULL)
1155 : 10 : close (fd);
1156 : : }
1157 : :
1158 : : /* Check whether there are any compressed sections in the ELF file. */
1159 : : static bool
1160 : 492 : elf_contains_chdrs (Elf *elf)
1161 : : {
1162 : 492 : Elf_Scn *scn = NULL;
1163 [ + + ]: 797906 : while ((scn = elf_nextscn (elf, scn)) != NULL)
1164 : : {
1165 : 797528 : GElf_Shdr shdr_mem;
1166 : 797528 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
1167 [ + - + + ]: 797528 : if (shdr != NULL && (shdr->sh_flags & SHF_COMPRESSED) != 0)
1168 : 114 : return true;
1169 : : }
1170 : : return false;
1171 : : }
1172 : :
1173 : : /* Process one ELF file. */
1174 : : static void
1175 : 968 : process_elf_file (Dwfl_Module *dwflmod, int fd)
1176 : : {
1177 : 968 : GElf_Addr dwflbias;
1178 : 968 : Elf *elf = dwfl_module_getelf (dwflmod, &dwflbias);
1179 : :
1180 : 968 : GElf_Ehdr ehdr_mem;
1181 : 968 : GElf_Ehdr *ehdr = gelf_getehdr (elf, &ehdr_mem);
1182 : :
1183 [ - + ]: 968 : if (ehdr == NULL)
1184 : : {
1185 : 0 : error (0, 0, _("cannot read ELF header: %s"), elf_errmsg (-1));
1186 : 0 : return;
1187 : : }
1188 : :
1189 : 968 : Ebl *ebl = ebl_openbackend (elf);
1190 [ - + ]: 968 : if (unlikely (ebl == NULL))
1191 : : {
1192 : 0 : ebl_error:
1193 : 0 : error (0, errno, _("cannot create EBL handle"));
1194 : 0 : return;
1195 : : }
1196 : :
1197 : : /* Determine the number of sections. */
1198 [ - + ]: 968 : if (unlikely (elf_getshdrnum (ebl->elf, &shnum) < 0))
1199 : 0 : error_exit (0, _("cannot determine number of sections: %s"),
1200 : : elf_errmsg (-1));
1201 : :
1202 : : /* Determine the number of phdrs. */
1203 [ - + ]: 968 : if (unlikely (elf_getphdrnum (ebl->elf, &phnum) < 0))
1204 : 0 : error_exit (0, _("cannot determine number of program headers: %s"),
1205 : : elf_errmsg (-1));
1206 : :
1207 : : /* For an ET_REL file, libdwfl has adjusted the in-core shdrs and
1208 : : may have applied relocation to some sections. If there are any
1209 : : compressed sections, any pass (or libdw/libdwfl) might have
1210 : : uncompressed them. So we need to get a fresh Elf handle on the
1211 : : file to display those. */
1212 : 2132 : bool print_unchanged = ((print_section_header
1213 [ + + ]: 462 : || print_relocations
1214 [ + + ]: 452 : || dump_data_sections != NULL
1215 [ + + ]: 440 : || print_notes)
1216 [ + + + + ]: 1036 : && (ehdr->e_type == ET_REL
1217 [ + + ]: 492 : || elf_contains_chdrs (ebl->elf)));
1218 : :
1219 : 196 : Elf *pure_elf = NULL;
1220 : 196 : Ebl *pure_ebl = ebl;
1221 : 196 : if (print_unchanged)
1222 : : {
1223 : : /* Read the file afresh. */
1224 : 196 : off_t aroff = elf_getaroff (elf);
1225 : 196 : pure_elf = dwelf_elf_begin (fd);
1226 [ - + ]: 196 : if (aroff > 0)
1227 : : {
1228 : : /* Archive member. */
1229 : 0 : (void) elf_rand (pure_elf, aroff);
1230 : 0 : Elf *armem = elf_begin (-1, ELF_C_READ_MMAP, pure_elf);
1231 : 0 : elf_end (pure_elf);
1232 : 0 : pure_elf = armem;
1233 : : }
1234 [ - + ]: 196 : if (pure_elf == NULL)
1235 : : {
1236 : 0 : error (0, 0, _("cannot read ELF: %s"), elf_errmsg (-1));
1237 : 0 : return;
1238 : : }
1239 : 196 : pure_ebl = ebl_openbackend (pure_elf);
1240 [ - + ]: 196 : if (pure_ebl == NULL)
1241 : 0 : goto ebl_error;
1242 : : }
1243 : :
1244 : 968 : bool symtab_printed = false;
1245 : :
1246 [ + + ]: 968 : if (print_file_header)
1247 : 190 : print_ehdr (ebl, ehdr);
1248 [ + + ]: 968 : if (print_section_header)
1249 : 506 : print_shdr (pure_ebl, ehdr);
1250 [ + + ]: 968 : if (print_program_header)
1251 : 188 : print_phdr (ebl, ehdr);
1252 [ + + ]: 968 : if (print_section_groups)
1253 : 202 : print_scngrp (ebl);
1254 [ + + ]: 968 : if (print_dynamic_table)
1255 : 190 : print_dynamic (ebl);
1256 [ + + ]: 968 : if (print_relocations)
1257 : 194 : print_relocs (pure_ebl, dwflmod, ehdr);
1258 [ + + ]: 968 : if (print_histogram)
1259 : 184 : handle_hash (ebl);
1260 [ + + + + ]: 968 : if (print_symbol_table || print_dynsym_table)
1261 : 220 : symtab_printed |= print_symtab (ebl, SHT_DYNSYM);
1262 [ + + ]: 968 : if (print_version_info)
1263 : 184 : print_verinfo (ebl);
1264 [ + + + - ]: 968 : if (print_symbol_table && !use_dynamic_segment)
1265 : 218 : symtab_printed |= print_symtab (ebl, SHT_SYMTAB);
1266 : :
1267 [ + + + + ]: 968 : if ((print_symbol_table || print_dynsym_table)
1268 [ + + + + ]: 220 : && !symtab_printed && symbol_table_section != NULL)
1269 : 2 : printf ("WARNING: %s: '%s'\n", _("cannot find section"),
1270 : : symbol_table_section);
1271 : :
1272 [ + + ]: 968 : if (print_arch)
1273 : 192 : print_liblist (ebl);
1274 [ + + ]: 968 : if (print_arch)
1275 : 192 : print_attributes (ebl, ehdr);
1276 [ + + ]: 968 : if (dump_data_sections != NULL)
1277 : 12 : dump_data (pure_ebl);
1278 [ + + ]: 968 : if (string_sections != NULL)
1279 : 186 : dump_strings (ebl);
1280 [ + + ]: 968 : if ((print_debug_sections | implicit_debug_sections) != 0)
1281 : 502 : print_debug (dwflmod, ebl, ehdr);
1282 [ + + ]: 968 : if (print_notes)
1283 : 230 : handle_notes (pure_ebl, ehdr);
1284 [ - + ]: 968 : if (print_string_sections)
1285 : 0 : print_strings (ebl);
1286 : :
1287 [ + + ]: 968 : if (pure_ebl != ebl)
1288 : : {
1289 : 196 : ebl_closebackend (ebl);
1290 : 196 : ebl_closebackend (pure_ebl);
1291 : 196 : elf_end (pure_elf);
1292 : : }
1293 : : else
1294 : 772 : ebl_closebackend (ebl);
1295 : : }
1296 : :
1297 : :
1298 : : /* Print file type. */
1299 : : static void
1300 : 190 : print_file_type (unsigned short int e_type)
1301 : : {
1302 [ + - ]: 190 : if (likely (e_type <= ET_CORE))
1303 : : {
1304 : 190 : static const char *const knowntypes[] =
1305 : : {
1306 : : N_("NONE (None)"),
1307 : : N_("REL (Relocatable file)"),
1308 : : N_("EXEC (Executable file)"),
1309 : : N_("DYN (Shared object file)"),
1310 : : N_("CORE (Core file)")
1311 : : };
1312 : 190 : puts (_(knowntypes[e_type]));
1313 : : }
1314 [ # # ]: 0 : else if (e_type >= ET_LOOS && e_type <= ET_HIOS)
1315 : 0 : printf (_("OS Specific: (%x)\n"), e_type);
1316 [ # # ]: 0 : else if (e_type >= ET_LOPROC /* && e_type <= ET_HIPROC always true */)
1317 : 0 : printf (_("Processor Specific: (%x)\n"), e_type);
1318 : : else
1319 : 0 : puts ("???");
1320 : 190 : }
1321 : :
1322 : :
1323 : : /* Print ELF header. */
1324 : : static void
1325 : 190 : print_ehdr (Ebl *ebl, GElf_Ehdr *ehdr)
1326 : : {
1327 : 190 : fputs (_("ELF Header:\n Magic: "), stdout);
1328 [ + + ]: 3230 : for (size_t cnt = 0; cnt < EI_NIDENT; ++cnt)
1329 : 3040 : printf (" %02hhx", ehdr->e_ident[cnt]);
1330 : :
1331 : 190 : printf (_("\n Class: %s\n"),
1332 [ + + ]: 190 : ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? "ELF32"
1333 : : : ehdr->e_ident[EI_CLASS] == ELFCLASS64 ? "ELF64"
1334 [ - + ]: 156 : : "\?\?\?");
1335 : :
1336 : 190 : printf (_(" Data: %s\n"),
1337 [ - + ]: 190 : ehdr->e_ident[EI_DATA] == ELFDATA2LSB
1338 : : ? "2's complement, little endian"
1339 : : : ehdr->e_ident[EI_DATA] == ELFDATA2MSB
1340 [ # # ]: 0 : ? "2's complement, big endian" : "\?\?\?");
1341 : :
1342 : 570 : printf (_(" Ident Version: %hhd %s\n"),
1343 : 190 : ehdr->e_ident[EI_VERSION],
1344 [ + - ]: 190 : ehdr->e_ident[EI_VERSION] == EV_CURRENT ? _("(current)")
1345 : : : "(\?\?\?)");
1346 : :
1347 : 190 : char buf[512];
1348 : 190 : printf (_(" OS/ABI: %s\n"),
1349 : 190 : ebl_osabi_name (ebl, ehdr->e_ident[EI_OSABI], buf, sizeof (buf)));
1350 : :
1351 : 380 : printf (_(" ABI Version: %hhd\n"),
1352 : 190 : ehdr->e_ident[EI_ABIVERSION]);
1353 : :
1354 : 190 : fputs (_(" Type: "), stdout);
1355 : 190 : print_file_type (ehdr->e_type);
1356 : :
1357 : 190 : const char *machine = dwelf_elf_e_machine_string (ehdr->e_machine);
1358 [ + - ]: 190 : if (machine != NULL)
1359 : 190 : printf (_(" Machine: %s\n"), machine);
1360 : : else
1361 : 0 : printf (_(" Machine: <unknown>: 0x%x\n"),
1362 : 0 : ehdr->e_machine);
1363 : :
1364 : 190 : printf (_(" Version: %d %s\n"),
1365 : : ehdr->e_version,
1366 [ + - ]: 190 : ehdr->e_version == EV_CURRENT ? _("(current)") : "(\?\?\?)");
1367 : :
1368 : 190 : printf (_(" Entry point address: %#" PRIx64 "\n"),
1369 : : ehdr->e_entry);
1370 : :
1371 : 190 : printf (_(" Start of program headers: %" PRId64 " %s\n"),
1372 : : ehdr->e_phoff, _("(bytes into file)"));
1373 : :
1374 : 190 : printf (_(" Start of section headers: %" PRId64 " %s\n"),
1375 : : ehdr->e_shoff, _("(bytes into file)"));
1376 : :
1377 : 190 : printf (_(" Flags: %s\n"),
1378 : : ebl_machine_flag_name (ebl, ehdr->e_flags, buf, sizeof (buf)));
1379 : :
1380 : 380 : printf (_(" Size of this header: %" PRId16 " %s\n"),
1381 : 190 : ehdr->e_ehsize, _("(bytes)"));
1382 : :
1383 : 380 : printf (_(" Size of program header entries: %" PRId16 " %s\n"),
1384 : 190 : ehdr->e_phentsize, _("(bytes)"));
1385 : :
1386 : 380 : printf (_(" Number of program headers entries: %" PRId16),
1387 : 190 : ehdr->e_phnum);
1388 [ + + ]: 190 : if (ehdr->e_phnum == PN_XNUM)
1389 : : {
1390 : 2 : GElf_Shdr shdr_mem;
1391 : 2 : GElf_Shdr *shdr = gelf_getshdr (elf_getscn (ebl->elf, 0), &shdr_mem);
1392 [ + - ]: 2 : if (shdr != NULL)
1393 : 2 : printf (_(" (%" PRIu32 " in [0].sh_info)"),
1394 : 2 : (uint32_t) shdr->sh_info);
1395 : : else
1396 : 0 : fputs (_(" ([0] not available)"), stdout);
1397 : : }
1398 : 190 : fputc ('\n', stdout);
1399 : :
1400 : 380 : printf (_(" Size of section header entries: %" PRId16 " %s\n"),
1401 : 190 : ehdr->e_shentsize, _("(bytes)"));
1402 : :
1403 : 380 : printf (_(" Number of section headers entries: %" PRId16),
1404 : 190 : ehdr->e_shnum);
1405 [ - + ]: 190 : if (ehdr->e_shnum == 0)
1406 : : {
1407 : 0 : GElf_Shdr shdr_mem;
1408 : 0 : GElf_Shdr *shdr = gelf_getshdr (elf_getscn (ebl->elf, 0), &shdr_mem);
1409 [ # # ]: 0 : if (shdr != NULL)
1410 : 0 : printf (_(" (%" PRIu32 " in [0].sh_size)"),
1411 : 0 : (uint32_t) shdr->sh_size);
1412 : : else
1413 : 0 : fputs (_(" ([0] not available)"), stdout);
1414 : : }
1415 : 190 : fputc ('\n', stdout);
1416 : :
1417 [ - + ]: 190 : if (unlikely (ehdr->e_shstrndx == SHN_XINDEX))
1418 : : {
1419 : 0 : GElf_Shdr shdr_mem;
1420 : 0 : GElf_Shdr *shdr = gelf_getshdr (elf_getscn (ebl->elf, 0), &shdr_mem);
1421 [ # # ]: 0 : if (shdr != NULL)
1422 : : /* We managed to get the zeroth section. */
1423 : 0 : snprintf (buf, sizeof (buf), _(" (%" PRIu32 " in [0].sh_link)"),
1424 : 0 : (uint32_t) shdr->sh_link);
1425 : : else
1426 : : {
1427 : 0 : strncpy (buf, _(" ([0] not available)"), sizeof (buf) - 1);
1428 : 0 : buf[sizeof (buf) - 1] = '\0';
1429 : : }
1430 : :
1431 : 0 : printf (_(" Section header string table index: XINDEX%s\n\n"),
1432 : : buf);
1433 : : }
1434 : : else
1435 : 190 : printf (_(" Section header string table index: %" PRId16 "\n\n"),
1436 : : ehdr->e_shstrndx);
1437 : 190 : }
1438 : :
1439 : :
1440 : : static const char *
1441 : 80578 : get_visibility_type (int value)
1442 : : {
1443 [ - + - - : 80578 : switch (value)
+ ]
1444 : : {
1445 : : case STV_DEFAULT:
1446 : : return "DEFAULT";
1447 : 0 : case STV_INTERNAL:
1448 : 0 : return "INTERNAL";
1449 : 956 : case STV_HIDDEN:
1450 : 956 : return "HIDDEN";
1451 : 0 : case STV_PROTECTED:
1452 : 0 : return "PROTECTED";
1453 : 0 : default:
1454 : 0 : return "???";
1455 : : }
1456 : : }
1457 : :
1458 : : static const char *
1459 : 384 : elf_ch_type_name (unsigned int code)
1460 : : {
1461 : 384 : switch (code)
1462 : : {
1463 : : case 0:
1464 : : return "NONE";
1465 : 24 : case ELFCOMPRESS_ZLIB:
1466 : 24 : return "ZLIB";
1467 : 360 : case ELFCOMPRESS_ZSTD:
1468 : 360 : return "ZSTD";
1469 : 0 : default:
1470 : 0 : return "UNKNOWN";
1471 : : }
1472 : : }
1473 : :
1474 : : /* Print the section headers. */
1475 : : static void
1476 : 506 : print_shdr (Ebl *ebl, GElf_Ehdr *ehdr)
1477 : : {
1478 : 506 : size_t cnt;
1479 : 506 : size_t shstrndx;
1480 : :
1481 [ + + ]: 506 : if (! print_file_header)
1482 : : {
1483 : 322 : size_t sections;
1484 [ - + ]: 322 : if (unlikely (elf_getshdrnum (ebl->elf, §ions) < 0))
1485 : 0 : error_exit (0, _("cannot get number of sections: %s"),
1486 : : elf_errmsg (-1));
1487 : :
1488 : 322 : printf (_("\
1489 : : There are %zd section headers, starting at offset %#" PRIx64 ":\n\
1490 : : \n"),
1491 : : sections, ehdr->e_shoff);
1492 : : }
1493 : :
1494 : : /* Get the section header string table index. */
1495 [ - + ]: 506 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
1496 : 0 : error_exit (0, _("cannot get section header string table index: %s"),
1497 : : elf_errmsg (-1));
1498 : :
1499 : 506 : puts (_("Section Headers:"));
1500 : :
1501 [ + + ]: 506 : if (ehdr->e_ident[EI_CLASS] == ELFCLASS32)
1502 : 196 : puts (_("[Nr] Name Type Addr Off Size ES Flags Lk Inf Al"));
1503 : : else
1504 : 310 : puts (_("[Nr] Name Type Addr Off Size ES Flags Lk Inf Al"));
1505 : :
1506 [ + + ]: 506 : if (print_decompress)
1507 : : {
1508 [ + + ]: 112 : if (ehdr->e_ident[EI_CLASS] == ELFCLASS32)
1509 : 56 : puts (_(" [Compression Size Al]"));
1510 : : else
1511 : 56 : puts (_(" [Compression Size Al]"));
1512 : : }
1513 : :
1514 [ + + ]: 1586316 : for (cnt = 0; cnt < shnum; ++cnt)
1515 : : {
1516 : 1585810 : Elf_Scn *scn = elf_getscn (ebl->elf, cnt);
1517 : :
1518 [ - + ]: 1585810 : if (unlikely (scn == NULL))
1519 : 0 : error_exit (0, _("cannot get section: %s"),
1520 : : elf_errmsg (-1));
1521 : :
1522 : : /* Get the section header. */
1523 : 1585810 : GElf_Shdr shdr_mem;
1524 : 1585810 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
1525 [ - + ]: 1585810 : if (unlikely (shdr == NULL))
1526 : 0 : error_exit (0, _("cannot get section header: %s"),
1527 : : elf_errmsg (-1));
1528 : :
1529 : 1585810 : char flagbuf[20];
1530 : 1585810 : char *cp = flagbuf;
1531 [ + + ]: 1585810 : if (shdr->sh_flags & SHF_WRITE)
1532 : 2764 : *cp++ = 'W';
1533 [ + + ]: 1585810 : if (shdr->sh_flags & SHF_ALLOC)
1534 : 8520 : *cp++ = 'A';
1535 [ + + ]: 1585810 : if (shdr->sh_flags & SHF_EXECINSTR)
1536 : 1564 : *cp++ = 'X';
1537 [ + + ]: 1585810 : if (shdr->sh_flags & SHF_MERGE)
1538 : 420 : *cp++ = 'M';
1539 [ + + ]: 1585810 : if (shdr->sh_flags & SHF_STRINGS)
1540 : 380 : *cp++ = 'S';
1541 [ + + ]: 1585810 : if (shdr->sh_flags & SHF_INFO_LINK)
1542 : 196 : *cp++ = 'I';
1543 [ + + ]: 1585810 : if (shdr->sh_flags & SHF_LINK_ORDER)
1544 : 4 : *cp++ = 'L';
1545 [ - + ]: 1585810 : if (shdr->sh_flags & SHF_OS_NONCONFORMING)
1546 : 0 : *cp++ = 'N';
1547 [ - + ]: 1585810 : if (shdr->sh_flags & SHF_GROUP)
1548 : 0 : *cp++ = 'G';
1549 [ + + ]: 1585810 : if (shdr->sh_flags & SHF_TLS)
1550 : 30 : *cp++ = 'T';
1551 [ + + ]: 1585810 : if (shdr->sh_flags & SHF_COMPRESSED)
1552 : 414 : *cp++ = 'C';
1553 [ - + ]: 1585810 : if (shdr->sh_flags & SHF_ORDERED)
1554 : 0 : *cp++ = 'O';
1555 [ - + ]: 1585810 : if (shdr->sh_flags & SHF_EXCLUDE)
1556 : 0 : *cp++ = 'E';
1557 [ + + ]: 1585810 : if (shdr->sh_flags & SHF_GNU_RETAIN)
1558 : 2 : *cp++ = 'R';
1559 : 1585810 : *cp = '\0';
1560 : :
1561 : 1585810 : const char *sname;
1562 : 1585810 : char buf[128];
1563 [ - + ]: 1585810 : sname = elf_strptr (ebl->elf, shstrndx, shdr->sh_name) ?: "<corrupt>";
1564 [ + + ]: 3171620 : printf ("[%2zu] %-20s %-12s %0*" PRIx64 " %0*" PRIx64 " %0*" PRIx64
1565 : : " %2" PRId64 " %-5s %2" PRId32 " %3" PRId32
1566 : : " %2" PRId64 "\n",
1567 : : cnt, sname,
1568 : 1585810 : ebl_section_type_name (ebl, shdr->sh_type, buf, sizeof (buf)),
1569 : : ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 8 : 16, shdr->sh_addr,
1570 : : ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 6 : 8, shdr->sh_offset,
1571 [ + + ]: 1585810 : ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 6 : 8, shdr->sh_size,
1572 : : shdr->sh_entsize, flagbuf, shdr->sh_link, shdr->sh_info,
1573 : : shdr->sh_addralign);
1574 : :
1575 [ + + ]: 1585810 : if (print_decompress)
1576 : : {
1577 [ + + ]: 1880 : if ((shdr->sh_flags & SHF_COMPRESSED) != 0)
1578 : : {
1579 : 384 : GElf_Chdr chdr;
1580 [ + - ]: 384 : if (gelf_getchdr (scn, &chdr) != NULL)
1581 [ + + - - ]: 768 : printf (" [ELF %s (%" PRId32 ") %0*" PRIx64
1582 : : " %2" PRId64 "]\n",
1583 : : elf_ch_type_name (chdr.ch_type),
1584 : : chdr.ch_type,
1585 [ + + ]: 384 : ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 6 : 8,
1586 : : chdr.ch_size, chdr.ch_addralign);
1587 : : else
1588 : 0 : error (0, 0,
1589 : 0 : _("bad compression header for section %zd: %s"),
1590 : : elf_ndxscn (scn), elf_errmsg (-1));
1591 : : }
1592 [ + + ]: 1496 : else if (startswith (sname, ".zdebug"))
1593 : : {
1594 : 24 : ssize_t size;
1595 [ + - ]: 24 : if ((size = dwelf_scn_gnu_compressed_size (scn)) >= 0)
1596 : 24 : printf (" [GNU ZLIB %0*zx ]\n",
1597 [ + + ]: 24 : ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 6 : 8, size);
1598 : : else
1599 : 0 : error (0, 0,
1600 : 0 : _("bad gnu compressed size for section %zd: %s"),
1601 : : elf_ndxscn (scn), elf_errmsg (-1));
1602 : : }
1603 : : }
1604 : : }
1605 : :
1606 : 506 : print_flag_info();
1607 : 506 : fputc ('\n', stdout);
1608 : 506 : }
1609 : :
1610 : : /* Print flag information. */
1611 : : static void
1612 : 506 : print_flag_info (void)
1613 : : {
1614 : 506 : puts ("Key to Flags:");
1615 : 506 : puts (" W (write), A (alloc), X (execute), M (merge), S (strings), I (info),");
1616 : 506 : puts (" L (link order), N (extra OS processing required), G (group), T (TLS),");
1617 : 506 : puts (" C (compressed), O (ordered), R (GNU retain), E (exclude)");
1618 : 506 : }
1619 : :
1620 : : /* Print the program header. */
1621 : : static void
1622 : 188 : print_phdr (Ebl *ebl, GElf_Ehdr *ehdr)
1623 : : {
1624 [ + + ]: 188 : if (phnum == 0)
1625 : : /* No program header, this is OK in relocatable objects. */
1626 : 12 : return;
1627 : :
1628 : 176 : puts (_("Program Headers:"));
1629 [ + + ]: 176 : if (ehdr->e_ident[EI_CLASS] == ELFCLASS32)
1630 : 26 : puts (_("\
1631 : : Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align"));
1632 : : else
1633 : 150 : puts (_("\
1634 : : Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align"));
1635 : :
1636 : : /* Process all program headers. */
1637 : : bool has_relro = false;
1638 : : GElf_Addr relro_from = 0;
1639 : : GElf_Addr relro_to = 0;
1640 [ + + ]: 2074 : for (size_t cnt = 0; cnt < phnum; ++cnt)
1641 : : {
1642 : 1898 : char buf[128];
1643 : 1898 : GElf_Phdr mem;
1644 : 1898 : GElf_Phdr *phdr = gelf_getphdr (ebl->elf, cnt, &mem);
1645 : :
1646 : : /* If for some reason the header cannot be returned show this. */
1647 [ - + ]: 1898 : if (unlikely (phdr == NULL))
1648 : : {
1649 : 0 : puts (" ???");
1650 : 0 : continue;
1651 : : }
1652 : :
1653 : 1898 : printf (" %-14s 0x%06" PRIx64 " 0x%0*" PRIx64 " 0x%0*" PRIx64
1654 : : " 0x%06" PRIx64 " 0x%06" PRIx64 " %c%c%c 0x%" PRIx64 "\n",
1655 : 1898 : ebl_segment_type_name (ebl, phdr->p_type, buf, sizeof (buf)),
1656 : : phdr->p_offset,
1657 : : ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 8 : 16, phdr->p_vaddr,
1658 [ + + ]: 1898 : ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 8 : 16, phdr->p_paddr,
1659 : : phdr->p_filesz,
1660 : : phdr->p_memsz,
1661 [ - + ]: 1898 : phdr->p_flags & PF_R ? 'R' : ' ',
1662 [ + + ]: 1898 : phdr->p_flags & PF_W ? 'W' : ' ',
1663 [ + + ]: 1898 : phdr->p_flags & PF_X ? 'E' : ' ',
1664 : : phdr->p_align);
1665 : :
1666 [ + + ]: 1898 : if (phdr->p_type == PT_INTERP)
1667 : : {
1668 : : /* If we are sure the file offset is valid then we can show
1669 : : the user the name of the interpreter. We check whether
1670 : : there is a section at the file offset. Normally there
1671 : : would be a section called ".interp". But in separate
1672 : : .debug files it is a NOBITS section (and so doesn't match
1673 : : with gelf_offscn). Which probably means the offset is
1674 : : not valid another reason could be because the ELF file
1675 : : just doesn't contain any section headers, in that case
1676 : : just play it safe and don't display anything. */
1677 : :
1678 : 172 : Elf_Scn *scn = gelf_offscn (ebl->elf, phdr->p_offset);
1679 : 172 : GElf_Shdr shdr_mem;
1680 : 172 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
1681 : :
1682 : 172 : size_t maxsize;
1683 : 172 : char *filedata = elf_rawfile (ebl->elf, &maxsize);
1684 : :
1685 [ + - + + ]: 172 : if (shdr != NULL && shdr->sh_type == SHT_PROGBITS
1686 [ + - + - ]: 130 : && filedata != NULL && phdr->p_offset < maxsize
1687 [ + - ]: 130 : && phdr->p_filesz <= maxsize - phdr->p_offset
1688 [ + - ]: 130 : && memchr (filedata + phdr->p_offset, '\0',
1689 : : phdr->p_filesz) != NULL)
1690 : 130 : printf (_("\t[Requesting program interpreter: %s]\n"),
1691 : : filedata + phdr->p_offset);
1692 : : }
1693 [ + + ]: 1726 : else if (phdr->p_type == PT_GNU_RELRO)
1694 : : {
1695 : 152 : has_relro = true;
1696 : 152 : relro_from = phdr->p_vaddr;
1697 : 152 : relro_to = relro_from + phdr->p_memsz;
1698 : : }
1699 : : }
1700 : :
1701 : 176 : size_t sections;
1702 [ - + ]: 176 : if (unlikely (elf_getshdrnum (ebl->elf, §ions) < 0))
1703 : 0 : error_exit (0, _("cannot get number of sections: %s"),
1704 : : elf_errmsg (-1));
1705 : :
1706 [ + - ]: 176 : if (sections == 0)
1707 : : /* No sections in the file. Punt. */
1708 : : return;
1709 : :
1710 : : /* Get the section header string table index. */
1711 : 176 : size_t shstrndx;
1712 [ - + ]: 176 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
1713 : 0 : error_exit (0, _("cannot get section header string table index"));
1714 : :
1715 : 176 : puts (_("\n Section to Segment mapping:\n Segment Sections..."));
1716 : :
1717 [ + + ]: 2074 : for (size_t cnt = 0; cnt < phnum; ++cnt)
1718 : : {
1719 : : /* Print the segment number. */
1720 : 1898 : printf (" %2.2zu ", cnt);
1721 : :
1722 : 1898 : GElf_Phdr phdr_mem;
1723 : 1898 : GElf_Phdr *phdr = gelf_getphdr (ebl->elf, cnt, &phdr_mem);
1724 : : /* This must not happen. */
1725 [ - + ]: 1898 : if (unlikely (phdr == NULL))
1726 : 0 : error_exit (0, _("cannot get program header: %s"),
1727 : : elf_errmsg (-1));
1728 : :
1729 : : /* Iterate over the sections. */
1730 : : bool in_relro = false;
1731 : : bool in_ro = false;
1732 [ + + ]: 62206 : for (size_t inner = 1; inner < shnum; ++inner)
1733 : : {
1734 : 60308 : Elf_Scn *scn = elf_getscn (ebl->elf, inner);
1735 : : /* This should not happen. */
1736 [ - + ]: 60308 : if (unlikely (scn == NULL))
1737 : 0 : error_exit (0, _("cannot get section: %s"),
1738 : : elf_errmsg (-1));
1739 : :
1740 : : /* Get the section header. */
1741 : 60308 : GElf_Shdr shdr_mem;
1742 : 60308 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
1743 [ - + ]: 60308 : if (unlikely (shdr == NULL))
1744 : 0 : error_exit (0, _("cannot get section header: %s"),
1745 : : elf_errmsg (-1));
1746 : :
1747 [ + - ]: 60308 : if (shdr->sh_size > 0
1748 : : /* Compare allocated sections by VMA, unallocated
1749 : : sections by file offset. */
1750 [ + + ]: 60308 : && (shdr->sh_flags & SHF_ALLOC
1751 : 47582 : ? (shdr->sh_addr >= phdr->p_vaddr
1752 [ + + ]: 47582 : && (shdr->sh_addr + shdr->sh_size
1753 [ + + ]: 31986 : <= phdr->p_vaddr + phdr->p_memsz))
1754 : 12726 : : (shdr->sh_offset >= phdr->p_offset
1755 [ + + ]: 12726 : && (shdr->sh_offset + shdr->sh_size
1756 [ + + ]: 11200 : <= phdr->p_offset + phdr->p_filesz))))
1757 : : {
1758 [ + + ]: 6262 : if (has_relro && !in_relro
1759 [ + + ]: 4178 : && shdr->sh_addr >= relro_from
1760 [ + + ]: 732 : && shdr->sh_addr + shdr->sh_size <= relro_to)
1761 : : {
1762 : 474 : fputs (" [RELRO:", stdout);
1763 : 474 : in_relro = true;
1764 : : }
1765 [ + + + + ]: 5788 : else if (has_relro && in_relro && shdr->sh_addr >= relro_to)
1766 : : {
1767 : 134 : fputs ("]", stdout);
1768 : 134 : in_relro = false;
1769 : : }
1770 [ + + ]: 5654 : else if (has_relro && in_relro
1771 [ + + ]: 1166 : && shdr->sh_addr + shdr->sh_size > relro_to)
1772 : 18 : fputs ("] <RELRO:", stdout);
1773 [ + + ]: 5636 : else if (phdr->p_type == PT_LOAD && (phdr->p_flags & PF_W) == 0)
1774 : : {
1775 [ + + ]: 3226 : if (!in_ro)
1776 : : {
1777 : 372 : fputs (" [RO:", stdout);
1778 : 372 : in_ro = true;
1779 : : }
1780 : : }
1781 : : else
1782 : : {
1783 : : /* Determine the segment this section is part of. */
1784 : : size_t cnt2;
1785 : : GElf_Phdr phdr2_mem;
1786 : : GElf_Phdr *phdr2 = NULL;
1787 [ + - ]: 10860 : for (cnt2 = 0; cnt2 < phnum; ++cnt2)
1788 : : {
1789 : 10860 : phdr2 = gelf_getphdr (ebl->elf, cnt2, &phdr2_mem);
1790 : :
1791 [ + - + + ]: 10860 : if (phdr2 != NULL && phdr2->p_type == PT_LOAD
1792 [ + - ]: 6152 : && shdr->sh_addr >= phdr2->p_vaddr
1793 : 6152 : && (shdr->sh_addr + shdr->sh_size
1794 [ + + ]: 6152 : <= phdr2->p_vaddr + phdr2->p_memsz))
1795 : : break;
1796 : : }
1797 : :
1798 [ + - ]: 2410 : if (cnt2 < phnum)
1799 : : {
1800 [ + + + + ]: 2410 : if ((phdr2->p_flags & PF_W) == 0 && !in_ro)
1801 : : {
1802 : 656 : fputs (" [RO:", stdout);
1803 : 656 : in_ro = true;
1804 : : }
1805 [ + + + - ]: 1754 : else if ((phdr2->p_flags & PF_W) != 0 && in_ro)
1806 : : {
1807 : 0 : fputs ("]", stdout);
1808 : 0 : in_ro = false;
1809 : : }
1810 : : }
1811 : : }
1812 : :
1813 : 6262 : printf (" %s",
1814 : 6262 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name));
1815 : :
1816 : : /* Signal that this section is only partially covered. */
1817 [ + + ]: 6262 : if (has_relro && in_relro
1818 [ + + ]: 1640 : && shdr->sh_addr + shdr->sh_size > relro_to)
1819 : : {
1820 : 18 : fputs (">", stdout);
1821 : 18 : in_relro = false;
1822 : : }
1823 : : }
1824 : : }
1825 [ + + ]: 1898 : if (in_relro || in_ro)
1826 : 1350 : fputs ("]", stdout);
1827 : :
1828 : : /* Finish the line. */
1829 : 1898 : fputc ('\n', stdout);
1830 : : }
1831 : : }
1832 : :
1833 : :
1834 : : static const char *
1835 : 1252 : section_name (Ebl *ebl, GElf_Shdr *shdr)
1836 : : {
1837 : 1252 : size_t shstrndx;
1838 [ + - - + ]: 1252 : if (shdr == NULL || elf_getshdrstrndx (ebl->elf, &shstrndx) < 0)
1839 : 0 : return "???";
1840 [ - + ]: 1252 : return elf_strptr (ebl->elf, shstrndx, shdr->sh_name) ?: "???";
1841 : : }
1842 : :
1843 : :
1844 : : static void
1845 : 32 : handle_scngrp (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr)
1846 : : {
1847 : : /* Get the data of the section. */
1848 : 32 : Elf_Data *data = elf_getdata (scn, NULL);
1849 : :
1850 : 32 : Elf_Scn *symscn = elf_getscn (ebl->elf, shdr->sh_link);
1851 : 32 : GElf_Shdr symshdr_mem;
1852 : 32 : GElf_Shdr *symshdr = gelf_getshdr (symscn, &symshdr_mem);
1853 : 32 : Elf_Data *symdata = elf_getdata (symscn, NULL);
1854 : :
1855 [ + - + - ]: 32 : if (data == NULL || data->d_size < sizeof (Elf32_Word) || symshdr == NULL
1856 [ - + ]: 32 : || symdata == NULL)
1857 : 0 : return;
1858 : :
1859 : : /* Get the section header string table index. */
1860 : 32 : size_t shstrndx;
1861 [ - + ]: 32 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
1862 : 0 : error_exit (0, _("cannot get section header string table index"));
1863 : :
1864 : 32 : Elf32_Word *grpref = (Elf32_Word *) data->d_buf;
1865 : :
1866 : 32 : GElf_Sym sym_mem;
1867 : 32 : GElf_Sym *sym = gelf_getsym (symdata, shdr->sh_info, &sym_mem);
1868 : :
1869 [ - + + + ]: 64 : printf ((grpref[0] & GRP_COMDAT)
1870 : 8 : ? ngettext ("\
1871 : : \nCOMDAT section group [%2zu] '%s' with signature '%s' contains %zu entry:\n",
1872 : : "\
1873 : : \nCOMDAT section group [%2zu] '%s' with signature '%s' contains %zu entries:\n",
1874 : : data->d_size / sizeof (Elf32_Word) - 1)
1875 : 24 : : ngettext ("\
1876 : : \nSection group [%2zu] '%s' with signature '%s' contains %zu entry:\n", "\
1877 : : \nSection group [%2zu] '%s' with signature '%s' contains %zu entries:\n",
1878 : : data->d_size / sizeof (Elf32_Word) - 1),
1879 : : elf_ndxscn (scn),
1880 : 32 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
1881 : : (sym == NULL ? NULL
1882 : 32 : : elf_strptr (ebl->elf, symshdr->sh_link, sym->st_name))
1883 : 0 : ?: _("<INVALID SYMBOL>"),
1884 [ + - ]: 32 : data->d_size / sizeof (Elf32_Word) - 1);
1885 : :
1886 [ + + ]: 104 : for (size_t cnt = 1; cnt < data->d_size / sizeof (Elf32_Word); ++cnt)
1887 : : {
1888 : 72 : GElf_Shdr grpshdr_mem;
1889 : 72 : GElf_Shdr *grpshdr = gelf_getshdr (elf_getscn (ebl->elf, grpref[cnt]),
1890 : : &grpshdr_mem);
1891 : :
1892 : 72 : const char *str;
1893 [ + - ]: 144 : printf (" [%2u] %s\n",
1894 : : grpref[cnt],
1895 : : grpshdr != NULL
1896 [ - + ]: 72 : && (str = elf_strptr (ebl->elf, shstrndx, grpshdr->sh_name))
1897 : 0 : ? str : _("<INVALID SECTION>"));
1898 : : }
1899 : : }
1900 : :
1901 : :
1902 : : static void
1903 : 202 : print_scngrp (Ebl *ebl)
1904 : : {
1905 : : /* Find all relocation sections and handle them. */
1906 : 202 : Elf_Scn *scn = NULL;
1907 : :
1908 [ + + ]: 6522 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
1909 : : {
1910 : : /* Handle the section if it is a symbol table. */
1911 : 6320 : GElf_Shdr shdr_mem;
1912 : 6320 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
1913 : :
1914 [ + - + + ]: 6320 : if (shdr != NULL && shdr->sh_type == SHT_GROUP)
1915 : : {
1916 [ - + ]: 32 : if ((shdr->sh_flags & SHF_COMPRESSED) != 0)
1917 : : {
1918 [ # # ]: 0 : if (elf_compress (scn, 0, 0) < 0)
1919 : 0 : printf ("WARNING: %s [%zd]\n",
1920 : : _("Couldn't uncompress section"),
1921 : : elf_ndxscn (scn));
1922 : 0 : shdr = gelf_getshdr (scn, &shdr_mem);
1923 [ # # ]: 0 : if (unlikely (shdr == NULL))
1924 : 0 : error_exit (0, _("cannot get section [%zd] header: %s"),
1925 : : elf_ndxscn (scn),
1926 : : elf_errmsg (-1));
1927 : : }
1928 : 32 : handle_scngrp (ebl, scn, shdr);
1929 : : }
1930 : : }
1931 : 202 : }
1932 : :
1933 : :
1934 : : static const struct flags
1935 : : {
1936 : : int mask;
1937 : : const char *str;
1938 : : } dt_flags[] =
1939 : : {
1940 : : { DF_ORIGIN, "ORIGIN" },
1941 : : { DF_SYMBOLIC, "SYMBOLIC" },
1942 : : { DF_TEXTREL, "TEXTREL" },
1943 : : { DF_BIND_NOW, "BIND_NOW" },
1944 : : { DF_STATIC_TLS, "STATIC_TLS" }
1945 : : };
1946 : : static const int ndt_flags = sizeof (dt_flags) / sizeof (dt_flags[0]);
1947 : :
1948 : : static const struct flags dt_flags_1[] =
1949 : : {
1950 : : { DF_1_NOW, "NOW" },
1951 : : { DF_1_GLOBAL, "GLOBAL" },
1952 : : { DF_1_GROUP, "GROUP" },
1953 : : { DF_1_NODELETE, "NODELETE" },
1954 : : { DF_1_LOADFLTR, "LOADFLTR" },
1955 : : { DF_1_INITFIRST, "INITFIRST" },
1956 : : { DF_1_NOOPEN, "NOOPEN" },
1957 : : { DF_1_ORIGIN, "ORIGIN" },
1958 : : { DF_1_DIRECT, "DIRECT" },
1959 : : { DF_1_TRANS, "TRANS" },
1960 : : { DF_1_INTERPOSE, "INTERPOSE" },
1961 : : { DF_1_NODEFLIB, "NODEFLIB" },
1962 : : { DF_1_NODUMP, "NODUMP" },
1963 : : { DF_1_CONFALT, "CONFALT" },
1964 : : { DF_1_ENDFILTEE, "ENDFILTEE" },
1965 : : { DF_1_DISPRELDNE, "DISPRELDNE" },
1966 : : { DF_1_DISPRELPND, "DISPRELPND" },
1967 : : };
1968 : : static const int ndt_flags_1 = sizeof (dt_flags_1) / sizeof (dt_flags_1[0]);
1969 : :
1970 : : static const struct flags dt_feature_1[] =
1971 : : {
1972 : : { DTF_1_PARINIT, "PARINIT" },
1973 : : { DTF_1_CONFEXP, "CONFEXP" }
1974 : : };
1975 : : static const int ndt_feature_1 = (sizeof (dt_feature_1)
1976 : : / sizeof (dt_feature_1[0]));
1977 : :
1978 : : static const struct flags dt_posflag_1[] =
1979 : : {
1980 : : { DF_P1_LAZYLOAD, "LAZYLOAD" },
1981 : : { DF_P1_GROUPPERM, "GROUPPERM" }
1982 : : };
1983 : : static const int ndt_posflag_1 = (sizeof (dt_posflag_1)
1984 : : / sizeof (dt_posflag_1[0]));
1985 : :
1986 : :
1987 : : static void
1988 : 74 : print_flags (int class, GElf_Xword d_val, const struct flags *flags,
1989 : : int nflags)
1990 : : {
1991 : 74 : bool first = true;
1992 : 74 : int cnt;
1993 : :
1994 [ + + ]: 1104 : for (cnt = 0; cnt < nflags; ++cnt)
1995 [ + + ]: 1030 : if (d_val & flags[cnt].mask)
1996 : : {
1997 [ + + ]: 68 : if (!first)
1998 : 36 : putchar (' ');
1999 : 68 : fputs (flags[cnt].str, stdout);
2000 : 68 : d_val &= ~flags[cnt].mask;
2001 : 68 : first = false;
2002 : : }
2003 : :
2004 [ + + ]: 74 : if (d_val != 0)
2005 : : {
2006 [ + + ]: 62 : if (!first)
2007 : 20 : putchar (' ');
2008 [ + + ]: 116 : printf ("%#0*" PRIx64, class == ELFCLASS32 ? 10 : 18, d_val);
2009 : : }
2010 : :
2011 : 74 : putchar ('\n');
2012 : 74 : }
2013 : :
2014 : :
2015 : : static void
2016 : 14 : print_dt_flags (int class, GElf_Xword d_val)
2017 : : {
2018 : 14 : print_flags (class, d_val, dt_flags, ndt_flags);
2019 : 14 : }
2020 : :
2021 : :
2022 : : static void
2023 : 56 : print_dt_flags_1 (int class, GElf_Xword d_val)
2024 : : {
2025 : 56 : print_flags (class, d_val, dt_flags_1, ndt_flags_1);
2026 : 56 : }
2027 : :
2028 : :
2029 : : static void
2030 : 2 : print_dt_feature_1 (int class, GElf_Xword d_val)
2031 : : {
2032 : 2 : print_flags (class, d_val, dt_feature_1, ndt_feature_1);
2033 : 2 : }
2034 : :
2035 : :
2036 : : static void
2037 : 2 : print_dt_posflag_1 (int class, GElf_Xword d_val)
2038 : : {
2039 : 2 : print_flags (class, d_val, dt_posflag_1, ndt_posflag_1);
2040 : 2 : }
2041 : :
2042 : :
2043 : : static size_t
2044 : 136 : get_dyn_ents (Elf_Data * dyn_data)
2045 : : {
2046 : 136 : GElf_Dyn *dyn;
2047 : 136 : GElf_Dyn dyn_mem;
2048 : 136 : size_t dyn_idx = 0;
2049 : 3414 : do
2050 : : {
2051 : 3414 : dyn = gelf_getdyn(dyn_data, dyn_idx, &dyn_mem);
2052 [ + - ]: 3414 : if (dyn != NULL)
2053 : 3414 : ++dyn_idx;
2054 : : }
2055 [ + + ]: 3414 : while (dyn != NULL && dyn->d_tag != DT_NULL);
2056 : :
2057 : 136 : return dyn_idx;
2058 : : }
2059 : :
2060 : :
2061 : : static void
2062 : 136 : handle_dynamic (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr, GElf_Phdr *phdr)
2063 : : {
2064 : 136 : int class = gelf_getclass (ebl->elf);
2065 : 136 : GElf_Shdr glink_mem;
2066 : 136 : GElf_Shdr *glink;
2067 : 136 : Elf_Data *data;
2068 : 136 : size_t cnt;
2069 : 136 : size_t shstrndx;
2070 : 136 : size_t dyn_ents;
2071 : :
2072 : : /* Get the data of the section. */
2073 [ + + + - ]: 136 : if (use_dynamic_segment && phdr != NULL)
2074 : 2 : data = elf_getdata_rawchunk(ebl->elf, phdr->p_offset,
2075 : : phdr->p_filesz, ELF_T_DYN);
2076 : : else
2077 : 134 : data = elf_getdata (scn, NULL);
2078 : :
2079 [ - + ]: 136 : if (data == NULL)
2080 : 0 : return;
2081 : :
2082 : : /* Get the dynamic section entry number */
2083 : 136 : dyn_ents = get_dyn_ents (data);
2084 : :
2085 [ + + + - ]: 136 : if (!use_dynamic_segment && shdr != NULL)
2086 : : {
2087 : : /* Get the section header string table index. */
2088 [ - + ]: 134 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
2089 : 0 : error_exit (0, _("cannot get section header string table index"));
2090 : :
2091 : 134 : glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link), &glink_mem);
2092 [ - + ]: 134 : if (glink == NULL)
2093 : 0 : error_exit (0, _("invalid sh_link value in section %zu"),
2094 : : elf_ndxscn (scn));
2095 : :
2096 : 134 : printf (ngettext ("\
2097 : : \nDynamic segment contains %lu entry:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'\n",
2098 : : "\
2099 : : \nDynamic segment contains %lu entries:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'\n",
2100 : : dyn_ents),
2101 : : (unsigned long int) dyn_ents,
2102 : : class == ELFCLASS32 ? 10 : 18, shdr->sh_addr,
2103 : : shdr->sh_offset,
2104 [ + + ]: 134 : (int) shdr->sh_link,
2105 : 134 : elf_strptr (ebl->elf, shstrndx, glink->sh_name));
2106 : : }
2107 [ + - ]: 2 : else if (phdr != NULL)
2108 : : {
2109 [ + - ]: 4 : printf (ngettext ("\
2110 : : \nDynamic segment contains %lu entry:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 "\n",
2111 : : "\
2112 : : \nDynamic segment contains %lu entries:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 "\n",
2113 : : dyn_ents),
2114 : : (unsigned long int) dyn_ents,
2115 : : class == ELFCLASS32 ? 10 : 18, phdr->p_paddr,
2116 : : phdr->p_offset);
2117 : : }
2118 : :
2119 : 136 : fputs (_(" Type Value\n"), stdout);
2120 : :
2121 : : /* if --use-dynamic option is enabled,
2122 : : use the string table to get the related library info. */
2123 : 136 : Elf_Data *strtab_data = NULL;
2124 [ + + + - ]: 136 : if (use_dynamic_segment && phdr != NULL)
2125 : : {
2126 : 2 : strtab_data = get_dynscn_strtab(ebl->elf, phdr);
2127 [ - + ]: 2 : if (strtab_data == NULL)
2128 : 0 : error_exit (0, _("cannot get string table by using dynamic segment"));
2129 : : }
2130 : :
2131 [ + + ]: 3550 : for (cnt = 0; cnt < dyn_ents; ++cnt)
2132 : : {
2133 : 3414 : GElf_Dyn dynmem;
2134 : 3414 : GElf_Dyn *dyn = gelf_getdyn (data, cnt, &dynmem);
2135 [ + - ]: 3414 : if (dyn == NULL)
2136 : : break;
2137 : :
2138 : 3414 : char buf[64];
2139 : 3414 : printf (" %-17s ",
2140 : : ebl_dynamic_tag_name (ebl, dyn->d_tag, buf, sizeof (buf)));
2141 : :
2142 : 3414 : char *name = NULL;
2143 : 3414 : if (dyn->d_tag == DT_NEEDED
2144 [ + + ]: 3414 : || dyn->d_tag == DT_SONAME
2145 : : || dyn->d_tag == DT_RPATH
2146 : : || dyn->d_tag == DT_RUNPATH)
2147 : : {
2148 [ + + + - ]: 236 : if (! use_dynamic_segment && shdr != NULL)
2149 : 232 : name = elf_strptr (ebl->elf, shdr->sh_link, dyn->d_un.d_val);
2150 [ - + ]: 4 : else if (dyn->d_un.d_val < strtab_data->d_size
2151 : 4 : && memrchr (strtab_data->d_buf + dyn->d_un.d_val, '\0',
2152 [ - + ]: 4 : strtab_data->d_size - 1 - dyn->d_un.d_val) != NULL)
2153 : 3414 : name = ((char *) strtab_data->d_buf) + dyn->d_un.d_val;
2154 : : }
2155 : :
2156 [ + + + + : 3414 : switch (dyn->d_tag)
+ + + + +
+ + + + ]
2157 : : {
2158 : 268 : case DT_NULL:
2159 : : case DT_DEBUG:
2160 : : case DT_BIND_NOW:
2161 : : case DT_TEXTREL:
2162 : : /* No further output. */
2163 : 268 : fputc ('\n', stdout);
2164 : 268 : break;
2165 : :
2166 : 226 : case DT_NEEDED:
2167 : 226 : printf (_("Shared library: [%s]\n"), name);
2168 : 226 : break;
2169 : :
2170 : 6 : case DT_SONAME:
2171 : 6 : printf (_("Library soname: [%s]\n"), name);
2172 : 6 : break;
2173 : :
2174 : 2 : case DT_RPATH:
2175 : 2 : printf (_("Library rpath: [%s]\n"), name);
2176 : 2 : break;
2177 : :
2178 : 2 : case DT_RUNPATH:
2179 : 2 : printf (_("Library runpath: [%s]\n"), name);
2180 : 2 : break;
2181 : :
2182 : 902 : case DT_PLTRELSZ:
2183 : : case DT_RELASZ:
2184 : : case DT_STRSZ:
2185 : : case DT_RELSZ:
2186 : : case DT_RELRSZ:
2187 : : case DT_RELAENT:
2188 : : case DT_SYMENT:
2189 : : case DT_RELENT:
2190 : : case DT_RELRENT:
2191 : : case DT_PLTPADSZ:
2192 : : case DT_MOVEENT:
2193 : : case DT_MOVESZ:
2194 : : case DT_INIT_ARRAYSZ:
2195 : : case DT_FINI_ARRAYSZ:
2196 : : case DT_SYMINSZ:
2197 : : case DT_SYMINENT:
2198 : : case DT_GNU_CONFLICTSZ:
2199 : : case DT_GNU_LIBLISTSZ:
2200 : 902 : printf (_("%" PRId64 " (bytes)\n"), dyn->d_un.d_val);
2201 : 902 : break;
2202 : :
2203 : 212 : case DT_VERDEFNUM:
2204 : : case DT_VERNEEDNUM:
2205 : : case DT_RELACOUNT:
2206 : : case DT_RELCOUNT:
2207 : 212 : printf ("%" PRId64 "\n", dyn->d_un.d_val);
2208 : 212 : break;
2209 : :
2210 : 108 : case DT_PLTREL:;
2211 : 108 : const char *tagname = ebl_dynamic_tag_name (ebl, dyn->d_un.d_val,
2212 : : NULL, 0);
2213 [ + + ]: 108 : puts (tagname ?: "???");
2214 : 108 : break;
2215 : :
2216 : 14 : case DT_FLAGS:
2217 : 14 : print_dt_flags (class, dyn->d_un.d_val);
2218 : 14 : break;
2219 : :
2220 : 56 : case DT_FLAGS_1:
2221 : 56 : print_dt_flags_1 (class, dyn->d_un.d_val);
2222 : 56 : break;
2223 : :
2224 : 2 : case DT_FEATURE_1:
2225 : 2 : print_dt_feature_1 (class, dyn->d_un.d_val);
2226 : 2 : break;
2227 : :
2228 : 2 : case DT_POSFLAG_1:
2229 : 2 : print_dt_posflag_1 (class, dyn->d_un.d_val);
2230 : 2 : break;
2231 : :
2232 : 1614 : default:
2233 [ + + ]: 1614 : printf ("%#0*" PRIx64 "\n",
2234 : : class == ELFCLASS32 ? 10 : 18, dyn->d_un.d_val);
2235 : 1614 : break;
2236 : : }
2237 : : }
2238 : : }
2239 : :
2240 : :
2241 : : /* Print the dynamic segment. */
2242 : : static void
2243 : 190 : print_dynamic (Ebl *ebl)
2244 : : {
2245 [ + + ]: 1072 : for (size_t i = 0; i < phnum; ++i)
2246 : : {
2247 : 1060 : GElf_Phdr phdr_mem;
2248 : 1060 : GElf_Phdr *phdr = gelf_getphdr (ebl->elf, i, &phdr_mem);
2249 : :
2250 [ + - + + ]: 1060 : if (phdr != NULL && phdr->p_type == PT_DYNAMIC)
2251 : : {
2252 : 178 : Elf_Scn *scn = gelf_offscn (ebl->elf, phdr->p_offset);
2253 : 178 : GElf_Shdr shdr_mem;
2254 : 178 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
2255 [ + + ]: 178 : if ((use_dynamic_segment && phdr != NULL)
2256 [ + + + + ]: 176 : || (shdr != NULL && shdr->sh_type == SHT_DYNAMIC))
2257 : 136 : handle_dynamic (ebl, scn, shdr, phdr);
2258 : 178 : break;
2259 : : }
2260 : : }
2261 : 190 : }
2262 : :
2263 : :
2264 : : /* Print relocations. */
2265 : : static void
2266 : 194 : print_relocs (Ebl *ebl, Dwfl_Module *mod, GElf_Ehdr *ehdr)
2267 : : {
2268 : : /* Find all relocation sections and handle them. */
2269 : 194 : Elf_Scn *scn = NULL;
2270 : :
2271 [ + + ]: 530466 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
2272 : : {
2273 : : /* Handle the section if it is a symbol table. */
2274 : 530272 : GElf_Shdr shdr_mem;
2275 : 530272 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
2276 : :
2277 [ + - ]: 530272 : if (likely (shdr != NULL))
2278 : : {
2279 [ + + ]: 530272 : if (shdr->sh_type == SHT_REL)
2280 : 92 : handle_relocs_rel (ebl, ehdr, scn, shdr);
2281 [ + + ]: 530180 : else if (shdr->sh_type == SHT_RELA)
2282 : 282 : handle_relocs_rela (ebl, ehdr, scn, shdr);
2283 [ - + ]: 529898 : else if (shdr->sh_type == SHT_RELR)
2284 : 0 : handle_relocs_relr (ebl, mod, scn, shdr);
2285 : : }
2286 : : }
2287 : 194 : }
2288 : :
2289 : :
2290 : : /* Handle a relocation section. */
2291 : : static void
2292 : 92 : handle_relocs_rel (Ebl *ebl, GElf_Ehdr *ehdr, Elf_Scn *scn, GElf_Shdr *shdr)
2293 : : {
2294 : 92 : int class = gelf_getclass (ebl->elf);
2295 : 92 : size_t sh_entsize = gelf_fsize (ebl->elf, ELF_T_REL, 1, EV_CURRENT);
2296 : 92 : int nentries = shdr->sh_size / sh_entsize;
2297 : :
2298 : : /* Get the data of the section. */
2299 : 92 : Elf_Data *data = elf_getdata (scn, NULL);
2300 [ + - ]: 92 : if (data == NULL)
2301 : 0 : return;
2302 : :
2303 : : /* Get the symbol table information. */
2304 : 92 : Elf_Scn *symscn = elf_getscn (ebl->elf, shdr->sh_link);
2305 : 92 : GElf_Shdr symshdr_mem;
2306 : 92 : GElf_Shdr *symshdr = gelf_getshdr (symscn, &symshdr_mem);
2307 : 92 : Elf_Data *symdata = elf_getdata (symscn, NULL);
2308 : :
2309 : : /* Get the section header of the section the relocations are for. */
2310 : 92 : GElf_Shdr destshdr_mem;
2311 : 92 : GElf_Shdr *destshdr = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_info),
2312 : : &destshdr_mem);
2313 : :
2314 [ + - - + ]: 92 : if (unlikely (symshdr == NULL || symdata == NULL || destshdr == NULL))
2315 : : {
2316 : 0 : printf (_("\nInvalid symbol table at offset %#0" PRIx64 "\n"),
2317 : : shdr->sh_offset);
2318 : 0 : return;
2319 : : }
2320 : :
2321 : : /* Search for the optional extended section index table if there are
2322 : : more than 64k sections. */
2323 : 92 : Elf_Data *xndxdata = NULL;
2324 [ - + ]: 92 : int xndxscnidx = shnum >= SHN_LORESERVE ? elf_scnshndx (symscn) : 0;
2325 [ # # ]: 0 : if (unlikely (xndxscnidx > 0))
2326 : 0 : xndxdata = elf_getdata (elf_getscn (ebl->elf, xndxscnidx), NULL);
2327 : :
2328 : : /* Get the section header string table index. */
2329 : 92 : size_t shstrndx;
2330 [ - + ]: 92 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
2331 : 0 : error_exit (0, _("cannot get section header string table index"));
2332 : :
2333 [ + + ]: 92 : if (shdr->sh_info != 0)
2334 : 76 : printf (ngettext ("\
2335 : : \nRelocation section [%2zu] '%s' for section [%2u] '%s' at offset %#0" PRIx64 " contains %d entry:\n",
2336 : : "\
2337 : : \nRelocation section [%2zu] '%s' for section [%2u] '%s' at offset %#0" PRIx64 " contains %d entries:\n",
2338 : : nentries),
2339 : : elf_ndxscn (scn),
2340 : 76 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
2341 : 76 : (unsigned int) shdr->sh_info,
2342 : 76 : elf_strptr (ebl->elf, shstrndx, destshdr->sh_name),
2343 : : shdr->sh_offset,
2344 : : nentries);
2345 : : else
2346 : : /* The .rel.dyn section does not refer to a specific section but
2347 : : instead of section index zero. Do not try to print a section
2348 : : name. */
2349 : 16 : printf (ngettext ("\
2350 : : \nRelocation section [%2u] '%s' at offset %#0" PRIx64 " contains %d entry:\n",
2351 : : "\
2352 : : \nRelocation section [%2u] '%s' at offset %#0" PRIx64 " contains %d entries:\n",
2353 : : nentries),
2354 : 16 : (unsigned int) elf_ndxscn (scn),
2355 : 16 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
2356 : : shdr->sh_offset,
2357 : : nentries);
2358 [ + - ]: 92 : fputs (class == ELFCLASS32
2359 : 92 : ? _("\
2360 : : Offset Type Value Name\n")
2361 : 0 : : _("\
2362 : : Offset Type Value Name\n"),
2363 : : stdout);
2364 : :
2365 : 92 : int is_statically_linked = 0;
2366 [ + + ]: 25942 : for (int cnt = 0; cnt < nentries; ++cnt)
2367 : : {
2368 : 25850 : GElf_Rel relmem;
2369 : 25850 : GElf_Rel *rel = gelf_getrel (data, cnt, &relmem);
2370 [ + - ]: 25850 : if (likely (rel != NULL))
2371 : : {
2372 : 25850 : char buf[128];
2373 : 25850 : GElf_Sym symmem;
2374 : 25850 : Elf32_Word xndx;
2375 : 51700 : GElf_Sym *sym = gelf_getsymshndx (symdata, xndxdata,
2376 : 25850 : GELF_R_SYM (rel->r_info),
2377 : : &symmem, &xndx);
2378 [ - + ]: 25850 : if (unlikely (sym == NULL))
2379 : : {
2380 : : /* As a special case we have to handle relocations in static
2381 : : executables. This only happens for IRELATIVE relocations
2382 : : (so far). There is no symbol table. */
2383 [ # # ]: 0 : if (is_statically_linked == 0)
2384 : : {
2385 : : /* Find the program header and look for a PT_INTERP entry. */
2386 : 0 : is_statically_linked = -1;
2387 [ # # ]: 0 : if (ehdr->e_type == ET_EXEC)
2388 : : {
2389 : 0 : is_statically_linked = 1;
2390 : :
2391 [ # # ]: 0 : for (size_t inner = 0; inner < phnum; ++inner)
2392 : : {
2393 : 0 : GElf_Phdr phdr_mem;
2394 : 0 : GElf_Phdr *phdr = gelf_getphdr (ebl->elf, inner,
2395 : : &phdr_mem);
2396 [ # # # # ]: 0 : if (phdr != NULL && phdr->p_type == PT_INTERP)
2397 : : {
2398 : 0 : is_statically_linked = -1;
2399 : 0 : break;
2400 : : }
2401 : : }
2402 : : }
2403 : : }
2404 : :
2405 [ # # # # ]: 0 : if (is_statically_linked > 0 && shdr->sh_link == 0)
2406 [ # # # # ]: 0 : printf ("\
2407 : : %#0*" PRIx64 " %-20s %*s %s\n",
2408 : : class == ELFCLASS32 ? 10 : 18, rel->r_offset,
2409 : 0 : ebl_reloc_type_check (ebl, GELF_R_TYPE (rel->r_info))
2410 : : /* Avoid the leading R_ which isn't carrying any
2411 : : information. */
2412 : 0 : ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info),
2413 : : buf, sizeof (buf)) + 2
2414 : 0 : : _("<INVALID RELOC>"),
2415 : : class == ELFCLASS32 ? 10 : 18, "",
2416 : 0 : elf_strptr (ebl->elf, shstrndx, destshdr->sh_name));
2417 : : else
2418 [ # # # # ]: 0 : printf (" %#0*" PRIx64 " %-20s <%s %ld>\n",
2419 : : class == ELFCLASS32 ? 10 : 18, rel->r_offset,
2420 : 0 : ebl_reloc_type_check (ebl, GELF_R_TYPE (rel->r_info))
2421 : : /* Avoid the leading R_ which isn't carrying any
2422 : : information. */
2423 : 0 : ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info),
2424 : : buf, sizeof (buf)) + 2
2425 : 0 : : _("<INVALID RELOC>"),
2426 : : _("INVALID SYMBOL"),
2427 : 0 : (long int) GELF_R_SYM (rel->r_info));
2428 : : }
2429 [ + + ]: 25850 : else if (GELF_ST_TYPE (sym->st_info) != STT_SECTION
2430 [ + + ]: 130 : && !(GELF_ST_TYPE (sym->st_info) == STT_NOTYPE
2431 [ - + ]: 66 : && GELF_ST_BIND (sym->st_info) == STB_LOCAL
2432 [ # # ]: 0 : && sym->st_shndx != SHN_UNDEF
2433 [ # # ]: 0 : && sym->st_value == 0)) // local start section label
2434 [ - + ]: 260 : printf (" %#0*" PRIx64 " %-20s %#0*" PRIx64 " %s\n",
2435 : : class == ELFCLASS32 ? 10 : 18, rel->r_offset,
2436 [ + - ]: 130 : likely (ebl_reloc_type_check (ebl,
2437 : : GELF_R_TYPE (rel->r_info)))
2438 : : /* Avoid the leading R_ which isn't carrying any
2439 : : information. */
2440 : 130 : ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info),
2441 : : buf, sizeof (buf)) + 2
2442 : 0 : : _("<INVALID RELOC>"),
2443 : : class == ELFCLASS32 ? 10 : 18, sym->st_value,
2444 : 130 : elf_strptr (ebl->elf, symshdr->sh_link, sym->st_name));
2445 : : else
2446 : : {
2447 : : /* This is a relocation against a STT_SECTION symbol
2448 : : or a local start section label for which we print
2449 : : section name. */
2450 : 25720 : GElf_Shdr secshdr_mem;
2451 : 25720 : GElf_Shdr *secshdr;
2452 : 25720 : secshdr = gelf_getshdr (elf_getscn (ebl->elf,
2453 [ + - ]: 25720 : sym->st_shndx == SHN_XINDEX
2454 : 0 : ? xndx : sym->st_shndx),
2455 : : &secshdr_mem);
2456 : :
2457 [ - + ]: 25720 : if (unlikely (secshdr == NULL))
2458 [ # # # # ]: 0 : printf (" %#0*" PRIx64 " %-20s <%s %ld>\n",
2459 : : class == ELFCLASS32 ? 10 : 18, rel->r_offset,
2460 : 0 : ebl_reloc_type_check (ebl, GELF_R_TYPE (rel->r_info))
2461 : : /* Avoid the leading R_ which isn't carrying any
2462 : : information. */
2463 : 0 : ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info),
2464 : : buf, sizeof (buf)) + 2
2465 : 0 : : _("<INVALID RELOC>"),
2466 : : _("INVALID SECTION"),
2467 [ # # ]: 0 : (long int) (sym->st_shndx == SHN_XINDEX
2468 : 0 : ? xndx : sym->st_shndx));
2469 : : else
2470 [ - + + - ]: 77160 : printf (" %#0*" PRIx64 " %-20s %#0*" PRIx64 " %s\n",
2471 : : class == ELFCLASS32 ? 10 : 18, rel->r_offset,
2472 : 25720 : ebl_reloc_type_check (ebl, GELF_R_TYPE (rel->r_info))
2473 : : /* Avoid the leading R_ which isn't carrying any
2474 : : information. */
2475 : 25720 : ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info),
2476 : : buf, sizeof (buf)) + 2
2477 : 0 : : _("<INVALID RELOC>"),
2478 : : class == ELFCLASS32 ? 10 : 18, sym->st_value,
2479 : 25720 : elf_strptr (ebl->elf, shstrndx, secshdr->sh_name));
2480 : : }
2481 : : }
2482 : : }
2483 : : }
2484 : :
2485 : :
2486 : : /* Handle a relocation section. */
2487 : : static void
2488 : 282 : handle_relocs_rela (Ebl *ebl, GElf_Ehdr *ehdr, Elf_Scn *scn, GElf_Shdr *shdr)
2489 : : {
2490 : 282 : int class = gelf_getclass (ebl->elf);
2491 : 282 : size_t sh_entsize = gelf_fsize (ebl->elf, ELF_T_RELA, 1, EV_CURRENT);
2492 : 282 : int nentries = shdr->sh_size / sh_entsize;
2493 : :
2494 : : /* Get the data of the section. */
2495 : 282 : Elf_Data *data = elf_getdata (scn, NULL);
2496 [ + - ]: 282 : if (data == NULL)
2497 : 0 : return;
2498 : :
2499 : : /* Get the symbol table information. */
2500 : 282 : Elf_Scn *symscn = elf_getscn (ebl->elf, shdr->sh_link);
2501 : 282 : GElf_Shdr symshdr_mem;
2502 : 282 : GElf_Shdr *symshdr = gelf_getshdr (symscn, &symshdr_mem);
2503 : 282 : Elf_Data *symdata = elf_getdata (symscn, NULL);
2504 : :
2505 : : /* Get the section header of the section the relocations are for. */
2506 : 282 : GElf_Shdr destshdr_mem;
2507 : 282 : GElf_Shdr *destshdr = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_info),
2508 : : &destshdr_mem);
2509 : :
2510 [ + - - + ]: 282 : if (unlikely (symshdr == NULL || symdata == NULL || destshdr == NULL))
2511 : : {
2512 : 0 : printf (_("\nInvalid symbol table at offset %#0" PRIx64 "\n"),
2513 : : shdr->sh_offset);
2514 : 0 : return;
2515 : : }
2516 : :
2517 : : /* Search for the optional extended section index table if there are
2518 : : more than 64k sections. */
2519 : 282 : Elf_Data *xndxdata = NULL;
2520 [ + + ]: 282 : int xndxscnidx = shnum >= SHN_LORESERVE ? elf_scnshndx (symscn) : 0;
2521 [ - + ]: 16 : if (unlikely (xndxscnidx > 0))
2522 : 16 : xndxdata = elf_getdata (elf_getscn (ebl->elf, xndxscnidx), NULL);
2523 : :
2524 : : /* Get the section header string table index. */
2525 : 282 : size_t shstrndx;
2526 [ - + ]: 282 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
2527 : 0 : error_exit (0, _("cannot get section header string table index"));
2528 : :
2529 [ + + ]: 282 : if (shdr->sh_info != 0)
2530 : 168 : printf (ngettext ("\
2531 : : \nRelocation section [%2zu] '%s' for section [%2u] '%s' at offset %#0" PRIx64 " contains %d entry:\n",
2532 : : "\
2533 : : \nRelocation section [%2zu] '%s' for section [%2u] '%s' at offset %#0" PRIx64 " contains %d entries:\n",
2534 : : nentries),
2535 : : elf_ndxscn (scn),
2536 : 168 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
2537 : 168 : (unsigned int) shdr->sh_info,
2538 : 168 : elf_strptr (ebl->elf, shstrndx, destshdr->sh_name),
2539 : : shdr->sh_offset,
2540 : : nentries);
2541 : : else
2542 : : /* The .rela.dyn section does not refer to a specific section but
2543 : : instead of section index zero. Do not try to print a section
2544 : : name. */
2545 : 114 : printf (ngettext ("\
2546 : : \nRelocation section [%2u] '%s' at offset %#0" PRIx64 " contains %d entry:\n",
2547 : : "\
2548 : : \nRelocation section [%2u] '%s' at offset %#0" PRIx64 " contains %d entries:\n",
2549 : : nentries),
2550 : 114 : (unsigned int) elf_ndxscn (scn),
2551 : 114 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
2552 : : shdr->sh_offset,
2553 : : nentries);
2554 [ - + ]: 282 : fputs (class == ELFCLASS32
2555 : 0 : ? _("\
2556 : : Offset Type Value Addend Name\n")
2557 : 282 : : _("\
2558 : : Offset Type Value Addend Name\n"),
2559 : : stdout);
2560 : :
2561 : 282 : int is_statically_linked = 0;
2562 [ + + ]: 1168710 : for (int cnt = 0; cnt < nentries; ++cnt)
2563 : : {
2564 : 1168428 : GElf_Rela relmem;
2565 : 1168428 : GElf_Rela *rel = gelf_getrela (data, cnt, &relmem);
2566 [ + - ]: 1168428 : if (likely (rel != NULL))
2567 : : {
2568 : 1168428 : char buf[64];
2569 : 1168428 : GElf_Sym symmem;
2570 : 1168428 : Elf32_Word xndx;
2571 : 2336856 : GElf_Sym *sym = gelf_getsymshndx (symdata, xndxdata,
2572 : 1168428 : GELF_R_SYM (rel->r_info),
2573 : : &symmem, &xndx);
2574 : :
2575 [ - + ]: 1168428 : if (unlikely (sym == NULL))
2576 : : {
2577 : : /* As a special case we have to handle relocations in static
2578 : : executables. This only happens for IRELATIVE relocations
2579 : : (so far). There is no symbol table. */
2580 [ # # ]: 0 : if (is_statically_linked == 0)
2581 : : {
2582 : : /* Find the program header and look for a PT_INTERP entry. */
2583 : 0 : is_statically_linked = -1;
2584 [ # # ]: 0 : if (ehdr->e_type == ET_EXEC)
2585 : : {
2586 : 0 : is_statically_linked = 1;
2587 : :
2588 [ # # ]: 0 : for (size_t inner = 0; inner < phnum; ++inner)
2589 : : {
2590 : 0 : GElf_Phdr phdr_mem;
2591 : 0 : GElf_Phdr *phdr = gelf_getphdr (ebl->elf, inner,
2592 : : &phdr_mem);
2593 [ # # # # ]: 0 : if (phdr != NULL && phdr->p_type == PT_INTERP)
2594 : : {
2595 : 0 : is_statically_linked = -1;
2596 : 0 : break;
2597 : : }
2598 : : }
2599 : : }
2600 : : }
2601 : :
2602 [ # # # # ]: 0 : if (is_statically_linked > 0 && shdr->sh_link == 0)
2603 [ # # # # ]: 0 : printf ("\
2604 : : %#0*" PRIx64 " %-15s %*s %#6" PRIx64 " %s\n",
2605 : : class == ELFCLASS32 ? 10 : 18, rel->r_offset,
2606 : 0 : ebl_reloc_type_check (ebl, GELF_R_TYPE (rel->r_info))
2607 : : /* Avoid the leading R_ which isn't carrying any
2608 : : information. */
2609 : 0 : ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info),
2610 : : buf, sizeof (buf)) + 2
2611 : 0 : : _("<INVALID RELOC>"),
2612 : : class == ELFCLASS32 ? 10 : 18, "",
2613 : : rel->r_addend,
2614 : 0 : elf_strptr (ebl->elf, shstrndx, destshdr->sh_name));
2615 : : else
2616 [ # # # # ]: 0 : printf (" %#0*" PRIx64 " %-15s <%s %ld>\n",
2617 : : class == ELFCLASS32 ? 10 : 18, rel->r_offset,
2618 : 0 : ebl_reloc_type_check (ebl, GELF_R_TYPE (rel->r_info))
2619 : : /* Avoid the leading R_ which isn't carrying any
2620 : : information. */
2621 : 0 : ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info),
2622 : : buf, sizeof (buf)) + 2
2623 : 0 : : _("<INVALID RELOC>"),
2624 : : _("INVALID SYMBOL"),
2625 : 0 : (long int) GELF_R_SYM (rel->r_info));
2626 : : }
2627 [ + + ]: 1168428 : else if (GELF_ST_TYPE (sym->st_info) != STT_SECTION
2628 [ + + ]: 630544 : && !(GELF_ST_TYPE (sym->st_info) == STT_NOTYPE
2629 [ + + ]: 103788 : && GELF_ST_BIND (sym->st_info) == STB_LOCAL
2630 [ + + ]: 102250 : && sym->st_shndx != SHN_UNDEF
2631 [ + + ]: 716 : && sym->st_value == 0)) // local start section label
2632 [ + - ]: 1261064 : printf ("\
2633 : : %#0*" PRIx64 " %-15s %#0*" PRIx64 " %+6" PRId64 " %s\n",
2634 : : class == ELFCLASS32 ? 10 : 18, rel->r_offset,
2635 [ + - ]: 630532 : likely (ebl_reloc_type_check (ebl,
2636 : : GELF_R_TYPE (rel->r_info)))
2637 : : /* Avoid the leading R_ which isn't carrying any
2638 : : information. */
2639 : 630532 : ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info),
2640 : : buf, sizeof (buf)) + 2
2641 : 0 : : _("<INVALID RELOC>"),
2642 : : class == ELFCLASS32 ? 10 : 18, sym->st_value,
2643 : : rel->r_addend,
2644 : 630532 : elf_strptr (ebl->elf, symshdr->sh_link, sym->st_name));
2645 : : else
2646 : : {
2647 : : /* This is a relocation against a STT_SECTION symbol
2648 : : or a local start section label for which we print
2649 : : section name. */
2650 : 537896 : GElf_Shdr secshdr_mem;
2651 : 537896 : GElf_Shdr *secshdr;
2652 : 537896 : secshdr = gelf_getshdr (elf_getscn (ebl->elf,
2653 [ + + ]: 537896 : sym->st_shndx == SHN_XINDEX
2654 : 2088 : ? xndx : sym->st_shndx),
2655 : : &secshdr_mem);
2656 : :
2657 [ - + ]: 537896 : if (unlikely (secshdr == NULL))
2658 [ # # # # ]: 0 : printf (" %#0*" PRIx64 " %-15s <%s %ld>\n",
2659 : : class == ELFCLASS32 ? 10 : 18, rel->r_offset,
2660 : 0 : ebl_reloc_type_check (ebl, GELF_R_TYPE (rel->r_info))
2661 : : /* Avoid the leading R_ which isn't carrying any
2662 : : information. */
2663 : 0 : ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info),
2664 : : buf, sizeof (buf)) + 2
2665 : 0 : : _("<INVALID RELOC>"),
2666 : : _("INVALID SECTION"),
2667 [ # # ]: 0 : (long int) (sym->st_shndx == SHN_XINDEX
2668 : 0 : ? xndx : sym->st_shndx));
2669 : : else
2670 [ + - + - ]: 1613688 : printf ("\
2671 : : %#0*" PRIx64 " %-15s %#0*" PRIx64 " %+6" PRId64 " %s\n",
2672 : : class == ELFCLASS32 ? 10 : 18, rel->r_offset,
2673 : 537896 : ebl_reloc_type_check (ebl, GELF_R_TYPE (rel->r_info))
2674 : : /* Avoid the leading R_ which isn't carrying any
2675 : : information. */
2676 : 537896 : ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info),
2677 : : buf, sizeof (buf)) + 2
2678 : 0 : : _("<INVALID RELOC>"),
2679 : : class == ELFCLASS32 ? 10 : 18, sym->st_value,
2680 : : rel->r_addend,
2681 : 537896 : elf_strptr (ebl->elf, shstrndx, secshdr->sh_name));
2682 : : }
2683 : : }
2684 : : }
2685 : : }
2686 : :
2687 : : /* Handle a relocation section. */
2688 : : static void
2689 : 0 : handle_relocs_relr (Ebl *ebl, Dwfl_Module *mod, Elf_Scn *scn, GElf_Shdr *shdr)
2690 : : {
2691 : 0 : int class = gelf_getclass (ebl->elf);
2692 : 0 : size_t sh_entsize = gelf_fsize (ebl->elf, ELF_T_RELR, 1, EV_CURRENT);
2693 : 0 : int nentries = shdr->sh_size / sh_entsize;
2694 : :
2695 : : /* Get the data of the section. */
2696 : 0 : Elf_Data *data = elf_getdata (scn, NULL);
2697 [ # # ]: 0 : if (data == NULL)
2698 : 0 : return;
2699 : :
2700 : : /* Get the section header string table index. */
2701 : 0 : size_t shstrndx;
2702 [ # # ]: 0 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
2703 : 0 : error_exit (0, _("cannot get section header string table index"));
2704 : :
2705 : : /* A .relr.dyn section does not refer to a specific section. */
2706 : 0 : printf (ngettext ("\
2707 : : \nRelocation section [%2u] '%s' at offset %#0" PRIx64 " contains %d entry:\n",
2708 : : "\
2709 : : \nRelocation section [%2u] '%s' at offset %#0" PRIx64 " contains %d entries:\n",
2710 : : nentries),
2711 : 0 : (unsigned int) elf_ndxscn (scn),
2712 : 0 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
2713 : : shdr->sh_offset,
2714 : : nentries);
2715 : :
2716 [ # # ]: 0 : if (class == ELFCLASS32)
2717 : : {
2718 : : uint32_t base = 0;
2719 [ # # ]: 0 : for (int cnt = 0; cnt < nentries; ++cnt)
2720 : : {
2721 : 0 : Elf32_Word *words = data->d_buf;
2722 : 0 : Elf32_Word entry = words[cnt];
2723 : :
2724 : : /* Just the raw entries? */
2725 [ # # ]: 0 : if (print_unresolved_addresses)
2726 : 0 : printf (" %#010" PRIx32 "%s\n", entry,
2727 [ # # ]: 0 : (entry & 1) == 0 ? " *" : "");
2728 : : else
2729 : : {
2730 : : /* A real address, also sets base. */
2731 [ # # ]: 0 : if ((entry & 1) == 0)
2732 : : {
2733 : 0 : printf (" ");
2734 : 0 : print_dwarf_addr (mod, 4, entry, entry, stdout);
2735 : 0 : printf (" *\n");
2736 : :
2737 : 0 : base = entry + 4;
2738 : : }
2739 : : else
2740 : : {
2741 : : /* Untangle address from base and bits. */
2742 : : uint32_t addr;
2743 [ # # ]: 0 : for (addr = base; (entry >>= 1) != 0; addr += 4)
2744 [ # # ]: 0 : if ((entry & 1) != 0)
2745 : : {
2746 : 0 : printf (" ");
2747 : 0 : print_dwarf_addr (mod, 4, addr, addr, stdout);
2748 : 0 : printf ("\n");
2749 : : }
2750 : 0 : base += 4 * (4 * 8 - 1);
2751 : : }
2752 : : }
2753 : : }
2754 : : }
2755 : : else
2756 : : {
2757 : : uint64_t base = 0;
2758 [ # # ]: 0 : for (int cnt = 0; cnt < nentries; ++cnt)
2759 : : {
2760 : 0 : Elf64_Xword *xwords = data->d_buf;
2761 : 0 : Elf64_Xword entry = xwords[cnt];
2762 : :
2763 : : /* Just the raw entries? */
2764 [ # # ]: 0 : if (print_unresolved_addresses)
2765 : 0 : printf (" %#018" PRIx64 "%s\n", entry,
2766 [ # # ]: 0 : (entry & 1) == 0 ? " *" : "");
2767 : : else
2768 : : {
2769 : : /* A real address, also sets base. */
2770 [ # # ]: 0 : if ((entry & 1) == 0)
2771 : : {
2772 : 0 : printf (" ");
2773 : 0 : print_dwarf_addr (mod, 8, entry, entry, stdout);
2774 : 0 : printf (" *\n");
2775 : :
2776 : 0 : base = entry + 8;
2777 : : }
2778 : : else
2779 : : {
2780 : : /* Untangle address from base and bits. */
2781 : : uint64_t addr;
2782 [ # # ]: 0 : for (addr = base; (entry >>= 1) != 0; addr += 8)
2783 [ # # ]: 0 : if ((entry & 1) != 0)
2784 : : {
2785 : 0 : printf (" ");
2786 : 0 : print_dwarf_addr (mod, 8, addr, addr, stdout);
2787 : 0 : printf ("\n");
2788 : : }
2789 : 0 : base += 8 * (8 * 8 - 1);
2790 : : }
2791 : : }
2792 : : }
2793 : : }
2794 : : }
2795 : :
2796 : : /* Print the program header. Return true if a symtab is printed,
2797 : : false otherwise. */
2798 : : static bool
2799 : 438 : print_symtab (Ebl *ebl, int type)
2800 : : {
2801 : : /* Use the dynamic section info to display symbol tables. */
2802 [ + - - - ]: 438 : if (use_dynamic_segment && type == SHT_DYNSYM)
2803 : 0 : return handle_dynamic_symtab(ebl);
2804 : :
2805 : : /* Find the symbol table(s). For this we have to search through the
2806 : : section table. */
2807 : : Elf_Scn *scn = NULL;
2808 : : bool symtab_printed = false;
2809 : :
2810 [ + + ]: 14056 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
2811 : : {
2812 : : /* Handle the section if it is a symbol table. */
2813 : 13618 : GElf_Shdr shdr_mem;
2814 : 13618 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
2815 : :
2816 [ + - + + ]: 13618 : if (shdr != NULL && shdr->sh_type == (GElf_Word) type)
2817 : : {
2818 [ + + ]: 274 : if (symbol_table_section != NULL)
2819 : : {
2820 : : /* Get the section header string table index. */
2821 : 14 : size_t shstrndx;
2822 : 14 : const char *sname;
2823 [ - + ]: 14 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
2824 : 0 : error_exit (0,
2825 : : _("cannot get section header string table index"));
2826 : 14 : sname = elf_strptr (ebl->elf, shstrndx, shdr->sh_name);
2827 [ + - + + ]: 14 : if (sname == NULL || strcmp (sname, symbol_table_section) != 0)
2828 : 8 : continue;
2829 : : }
2830 : :
2831 [ - + ]: 266 : if ((shdr->sh_flags & SHF_COMPRESSED) != 0)
2832 : : {
2833 [ # # ]: 0 : if (elf_compress (scn, 0, 0) < 0)
2834 : 0 : printf ("WARNING: %s [%zd]\n",
2835 : : _("Couldn't uncompress section"),
2836 : : elf_ndxscn (scn));
2837 : 0 : shdr = gelf_getshdr (scn, &shdr_mem);
2838 [ # # ]: 0 : if (unlikely (shdr == NULL))
2839 : 0 : error_exit (0,
2840 : : _("cannot get section [%zd] header: %s"),
2841 : : elf_ndxscn (scn), elf_errmsg (-1));
2842 : : }
2843 : 266 : symtab_printed = handle_symtab (ebl, scn, shdr);
2844 : : }
2845 : : }
2846 : :
2847 : : return symtab_printed;
2848 : : }
2849 : :
2850 : :
2851 : : static void
2852 : 266 : process_symtab (Ebl *ebl, unsigned int nsyms, Elf64_Word idx,
2853 : : Elf32_Word verneed_stridx, Elf32_Word verdef_stridx,
2854 : : Elf_Data *symdata, Elf_Data *versym_data,
2855 : : Elf_Data *symstr_data, Elf_Data *verneed_data,
2856 : : Elf_Data *verdef_data, Elf_Data *xndx_data)
2857 : : {
2858 [ + + ]: 80844 : for (unsigned int cnt = 0; cnt < nsyms; ++cnt)
2859 : : {
2860 : 80578 : char typebuf[64];
2861 : 80578 : char bindbuf[64];
2862 : 80578 : char scnbuf[64];
2863 : 80578 : const char *sym_name;
2864 : 80578 : Elf32_Word xndx;
2865 : 80578 : GElf_Sym sym_mem;
2866 : 80578 : GElf_Sym *sym
2867 : 80578 : = gelf_getsymshndx (symdata, xndx_data, cnt, &sym_mem, &xndx);
2868 : :
2869 [ - + ]: 80578 : if (unlikely (sym == NULL))
2870 : 0 : continue;
2871 : :
2872 : : /* Determine the real section index. */
2873 [ + - ]: 80578 : if (likely (sym->st_shndx != SHN_XINDEX))
2874 : 80578 : xndx = sym->st_shndx;
2875 [ - + ]: 80578 : if (use_dynamic_segment == true)
2876 : : {
2877 [ # # ]: 0 : if (validate_str (symstr_data->d_buf, sym->st_name,
2878 : : symstr_data->d_size))
2879 : 0 : sym_name = (char *)symstr_data->d_buf + sym->st_name;
2880 : : else
2881 : : sym_name = NULL;
2882 : : }
2883 : : else
2884 : 80578 : sym_name = elf_strptr (ebl->elf, idx, sym->st_name);
2885 : :
2886 [ - + ]: 80578 : if (sym_name == NULL)
2887 : : sym_name = "???";
2888 : :
2889 [ + + ]: 160268 : printf (_ ("\
2890 : : %5u: %0*" PRIx64 " %6" PRId64 " %-7s %-6s %-9s %6s %s"),
2891 : 80578 : cnt, gelf_getclass (ebl->elf) == ELFCLASS32 ? 8 : 16,
2892 : : sym->st_value, sym->st_size,
2893 : 80578 : ebl_symbol_type_name (ebl, GELF_ST_TYPE (sym->st_info), typebuf,
2894 : : sizeof (typebuf)),
2895 : 80578 : ebl_symbol_binding_name (ebl, GELF_ST_BIND (sym->st_info),
2896 : : bindbuf, sizeof (bindbuf)),
2897 : 80578 : get_visibility_type (GELF_ST_VISIBILITY (sym->st_other)),
2898 : 80578 : ebl_section_name (ebl, sym->st_shndx, xndx, scnbuf,
2899 : : sizeof (scnbuf), NULL, shnum),
2900 : : sym_name);
2901 : :
2902 [ + + ]: 80578 : if (versym_data != NULL)
2903 : : {
2904 : : /* Get the version information. */
2905 : 3436 : GElf_Versym versym_mem;
2906 : 3436 : GElf_Versym *versym = gelf_getversym (versym_data, cnt, &versym_mem);
2907 : :
2908 [ + - + + ]: 3436 : if (versym != NULL && ((*versym & 0x8000) != 0 || *versym > 1))
2909 : : {
2910 : 2476 : bool is_nobits = false;
2911 : 2476 : bool check_def = xndx != SHN_UNDEF;
2912 : :
2913 [ + + - + ]: 2476 : if (xndx < SHN_LORESERVE || sym->st_shndx == SHN_XINDEX)
2914 : : {
2915 : 2456 : GElf_Shdr symshdr_mem;
2916 : 2456 : GElf_Shdr *symshdr = gelf_getshdr (
2917 : : elf_getscn (ebl->elf, xndx), &symshdr_mem);
2918 : :
2919 : 2456 : is_nobits
2920 [ + - + + ]: 4888 : = (symshdr != NULL && symshdr->sh_type == SHT_NOBITS);
2921 : : }
2922 : :
2923 [ + + ]: 2476 : if (is_nobits || !check_def)
2924 : : {
2925 : : /* We must test both. */
2926 : 2172 : GElf_Vernaux vernaux_mem;
2927 : 2172 : GElf_Vernaux *vernaux = NULL;
2928 : 2172 : size_t vn_offset = 0;
2929 : :
2930 : 2172 : GElf_Verneed verneed_mem;
2931 : 2172 : GElf_Verneed *verneed
2932 : 2172 : = gelf_getverneed (verneed_data, 0, &verneed_mem);
2933 [ + - ]: 7902 : while (verneed != NULL)
2934 : : {
2935 : 7902 : size_t vna_offset = vn_offset;
2936 : :
2937 : 7902 : vernaux = gelf_getvernaux (verneed_data,
2938 : 7902 : vna_offset += verneed->vn_aux,
2939 : : &vernaux_mem);
2940 [ + + ]: 28762 : while (vernaux != NULL && vernaux->vna_other != *versym
2941 [ + + ]: 18688 : && vernaux->vna_next != 0
2942 [ + - ]: 20860 : && (verneed_data->d_size - vna_offset
2943 [ + - ]: 12958 : >= vernaux->vna_next))
2944 : : {
2945 : : /* Update the offset. */
2946 : 12958 : vna_offset += vernaux->vna_next;
2947 : :
2948 : 12958 : vernaux = (vernaux->vna_next == 0
2949 : : ? NULL
2950 : 12958 : : gelf_getvernaux (verneed_data,
2951 : : vna_offset,
2952 : : &vernaux_mem));
2953 : : }
2954 : :
2955 : : /* Check whether we found the version. */
2956 [ + - + + ]: 7902 : if (vernaux != NULL && vernaux->vna_other == *versym)
2957 : : /* Found it. */
2958 : : break;
2959 : :
2960 [ + - ]: 5730 : if (verneed_data->d_size - vn_offset < verneed->vn_next)
2961 : : break;
2962 : :
2963 : 5730 : vn_offset += verneed->vn_next;
2964 : 5730 : verneed
2965 : : = (verneed->vn_next == 0
2966 : : ? NULL
2967 [ + - ]: 5730 : : gelf_getverneed (verneed_data, vn_offset,
2968 : : &verneed_mem));
2969 : : }
2970 : :
2971 [ + - + - ]: 2172 : if (vernaux != NULL && vernaux->vna_other == *versym)
2972 : : {
2973 : 4344 : printf ("@%s (%u)",
2974 [ - + ]: 2172 : use_dynamic_segment == true
2975 : 0 : ? (char *)symstr_data->d_buf
2976 : 0 : + vernaux->vna_name
2977 : 2172 : : elf_strptr (ebl->elf, verneed_stridx,
2978 : 2172 : vernaux->vna_name),
2979 : : (unsigned int)vernaux->vna_other);
2980 : 2172 : check_def = 0;
2981 : : }
2982 [ # # ]: 0 : else if (unlikely (!is_nobits))
2983 : 0 : error (0, 0, _ ("bad dynamic symbol"));
2984 : : else
2985 : : check_def = 1;
2986 : : }
2987 : :
2988 [ + - + - ]: 2476 : if (check_def && *versym != 0x8001)
2989 : : {
2990 : : /* We must test both. */
2991 : 304 : size_t vd_offset = 0;
2992 : :
2993 : 304 : GElf_Verdef verdef_mem;
2994 : 304 : GElf_Verdef *verdef
2995 : 304 : = gelf_getverdef (verdef_data, 0, &verdef_mem);
2996 [ + - ]: 866 : while (verdef != NULL)
2997 : : {
2998 [ + + ]: 866 : if (verdef->vd_ndx == (*versym & 0x7fff))
2999 : : /* Found the definition. */
3000 : : break;
3001 : :
3002 [ + - ]: 562 : if (verdef_data->d_size - vd_offset < verdef->vd_next)
3003 : : break;
3004 : :
3005 : 562 : vd_offset += verdef->vd_next;
3006 : 562 : verdef = (verdef->vd_next == 0
3007 : : ? NULL
3008 [ + - ]: 562 : : gelf_getverdef (verdef_data, vd_offset,
3009 : : &verdef_mem));
3010 : : }
3011 : :
3012 [ + - ]: 304 : if (verdef != NULL)
3013 : : {
3014 : 304 : GElf_Verdaux verdaux_mem;
3015 : 608 : GElf_Verdaux *verdaux = gelf_getverdaux (
3016 : 304 : verdef_data, vd_offset + verdef->vd_aux,
3017 : : &verdaux_mem);
3018 : :
3019 [ + - ]: 304 : if (verdaux != NULL)
3020 [ + - ]: 608 : printf ((*versym & 0x8000) ? "@%s" : "@@%s",
3021 [ - + ]: 304 : use_dynamic_segment == true
3022 : 0 : ? (char *)symstr_data->d_buf
3023 : 0 : + verdaux->vda_name
3024 : 304 : : elf_strptr (ebl->elf, verdef_stridx,
3025 : 304 : verdaux->vda_name));
3026 : : }
3027 : : }
3028 : : }
3029 : : }
3030 : :
3031 : 80578 : putchar ('\n');
3032 : : }
3033 : 266 : }
3034 : :
3035 : :
3036 : : static bool
3037 : 266 : handle_symtab (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr)
3038 : : {
3039 : 266 : Elf_Data *versym_data = NULL;
3040 : 266 : Elf_Data *verneed_data = NULL;
3041 : 266 : Elf_Data *verdef_data = NULL;
3042 : 266 : Elf_Data *xndx_data = NULL;
3043 : 266 : int class = gelf_getclass (ebl->elf);
3044 : 266 : Elf32_Word verneed_stridx = 0;
3045 : 266 : Elf32_Word verdef_stridx = 0;
3046 : :
3047 : : /* Get the data of the section. */
3048 : 266 : Elf_Data *data = elf_getdata (scn, NULL);
3049 [ + - ]: 266 : if (data == NULL)
3050 : : return false;
3051 : :
3052 : : /* Find out whether we have other sections we might need. */
3053 : : Elf_Scn *runscn = NULL;
3054 [ + + ]: 8784 : while ((runscn = elf_nextscn (ebl->elf, runscn)) != NULL)
3055 : : {
3056 : 8518 : GElf_Shdr runshdr_mem;
3057 : 8518 : GElf_Shdr *runshdr = gelf_getshdr (runscn, &runshdr_mem);
3058 : :
3059 [ + - ]: 8518 : if (likely (runshdr != NULL))
3060 : : {
3061 [ + + ]: 8518 : if (runshdr->sh_type == SHT_GNU_versym
3062 [ + + ]: 202 : && runshdr->sh_link == elf_ndxscn (scn))
3063 : : /* Bingo, found the version information. Now get the data. */
3064 : 150 : versym_data = elf_getdata (runscn, NULL);
3065 [ + + ]: 8368 : else if (runshdr->sh_type == SHT_GNU_verneed)
3066 : : {
3067 : : /* This is the information about the needed versions. */
3068 : 202 : verneed_data = elf_getdata (runscn, NULL);
3069 : 202 : verneed_stridx = runshdr->sh_link;
3070 : : }
3071 [ + + ]: 8166 : else if (runshdr->sh_type == SHT_GNU_verdef)
3072 : : {
3073 : : /* This is the information about the defined versions. */
3074 : 8 : verdef_data = elf_getdata (runscn, NULL);
3075 : 8 : verdef_stridx = runshdr->sh_link;
3076 : : }
3077 [ + - ]: 8158 : else if (runshdr->sh_type == SHT_SYMTAB_SHNDX
3078 [ # # ]: 0 : && runshdr->sh_link == elf_ndxscn (scn))
3079 : : /* Extended section index. */
3080 : 0 : xndx_data = elf_getdata (runscn, NULL);
3081 : : }
3082 : : }
3083 : :
3084 : : /* Get the section header string table index. */
3085 : 266 : size_t shstrndx;
3086 [ - + ]: 266 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
3087 : 0 : error_exit (0, _("cannot get section header string table index"));
3088 : :
3089 : 266 : GElf_Shdr glink_mem;
3090 : 266 : GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
3091 : : &glink_mem);
3092 [ - + ]: 266 : if (glink == NULL)
3093 : 0 : error_exit (0, _("invalid sh_link value in section %zu"),
3094 : : elf_ndxscn (scn));
3095 : :
3096 : : /* Now we can compute the number of entries in the section. */
3097 : 532 : unsigned int nsyms = data->d_size / (class == ELFCLASS32
3098 : : ? sizeof (Elf32_Sym)
3099 [ + + ]: 266 : : sizeof (Elf64_Sym));
3100 : :
3101 : 266 : printf (ngettext ("\nSymbol table [%2u] '%s' contains %u entry:\n",
3102 : : "\nSymbol table [%2u] '%s' contains %u entries:\n",
3103 : : nsyms),
3104 : 266 : (unsigned int) elf_ndxscn (scn),
3105 : 266 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name), nsyms);
3106 : 266 : printf (ngettext (" %lu local symbol String table: [%2u] '%s'\n",
3107 : : " %lu local symbols String table: [%2u] '%s'\n",
3108 : : shdr->sh_info),
3109 : 266 : (unsigned long int) shdr->sh_info,
3110 : 266 : (unsigned int) shdr->sh_link,
3111 : 266 : elf_strptr (ebl->elf, shstrndx, glink->sh_name));
3112 : :
3113 [ + + ]: 532 : fputs (class == ELFCLASS32
3114 : 32 : ? _("\
3115 : : Num: Value Size Type Bind Vis Ndx Name\n")
3116 : 234 : : _("\
3117 : : Num: Value Size Type Bind Vis Ndx Name\n"),
3118 : : stdout);
3119 : :
3120 : 266 : process_symtab(ebl, nsyms, shdr->sh_link, verneed_stridx, verdef_stridx,
3121 : : data, versym_data, NULL, verneed_data, verdef_data, xndx_data);
3122 : 266 : return true;
3123 : : }
3124 : :
3125 : :
3126 : : static bool
3127 : 0 : handle_dynamic_symtab (Ebl *ebl)
3128 : : {
3129 : 0 : GElf_Phdr phdr_mem;
3130 : 0 : GElf_Phdr *phdr = NULL;
3131 : : /* phnum is a static variable which was already fetched in function
3132 : : process_elf_file. */
3133 [ # # ]: 0 : for (size_t i = 0; i < phnum; ++i)
3134 : : {
3135 : 0 : phdr = gelf_getphdr (ebl->elf, i, &phdr_mem);
3136 [ # # # # ]: 0 : if (phdr == NULL || phdr->p_type == PT_DYNAMIC)
3137 : : break;
3138 : : }
3139 [ # # ]: 0 : if (phdr == NULL)
3140 : : return false;
3141 : :
3142 : 0 : GElf_Addr addrs[i_max] = {
3143 : : 0,
3144 : : };
3145 : 0 : GElf_Off offs[i_max] = {
3146 : : 0,
3147 : : };
3148 : 0 : get_dynscn_addrs (ebl->elf, phdr, addrs);
3149 : 0 : find_offsets (ebl->elf, 0, i_max, addrs, offs);
3150 : :
3151 : 0 : size_t syments = 0;
3152 : :
3153 : 0 : GElf_Ehdr ehdr_mem;
3154 : 0 : GElf_Ehdr *ehdr = gelf_getehdr (ebl->elf, &ehdr_mem);
3155 : :
3156 [ # # ]: 0 : if (offs[i_hash] != 0)
3157 : : {
3158 : : /* In the original format, .hash says the size of .dynsym. */
3159 : :
3160 [ # # # # : 0 : size_t entsz = SH_ENTSIZE_HASH (ehdr);
# # ]
3161 : : Elf_Data *data
3162 : 0 : = elf_getdata_rawchunk (ebl->elf, offs[i_hash] + entsz, entsz,
3163 : : (entsz == 4 ? ELF_T_WORD : ELF_T_XWORD));
3164 [ # # ]: 0 : if (data != NULL)
3165 : 0 : syments = (entsz == 4 ? *(const GElf_Word *)data->d_buf
3166 [ # # ]: 0 : : *(const GElf_Xword *)data->d_buf);
3167 : : }
3168 [ # # # # ]: 0 : if (offs[i_gnu_hash] != 0 && syments == 0)
3169 : : {
3170 : : /* In the new format, we can derive it with some work. */
3171 : :
3172 : 0 : const struct
3173 : : {
3174 : : Elf32_Word nbuckets;
3175 : : Elf32_Word symndx;
3176 : : Elf32_Word maskwords;
3177 : : Elf32_Word shift2;
3178 : : } * header;
3179 : :
3180 : 0 : Elf_Data *data = elf_getdata_rawchunk (ebl->elf, offs[i_gnu_hash],
3181 : : sizeof *header, ELF_T_WORD);
3182 [ # # ]: 0 : if (data != NULL)
3183 : : {
3184 : 0 : header = data->d_buf;
3185 : 0 : Elf32_Word nbuckets = header->nbuckets;
3186 : 0 : Elf32_Word symndx = header->symndx;
3187 : 0 : GElf_Off buckets_at
3188 : : = (offs[i_gnu_hash] + sizeof *header
3189 : 0 : + (gelf_getclass (ebl->elf) * sizeof (Elf32_Word)
3190 : 0 : * header->maskwords));
3191 : :
3192 : : // elf_getdata_rawchunk takes a size_t, make sure it
3193 : : // doesn't overflow.
3194 : : #if SIZE_MAX <= UINT32_MAX
3195 : : if (nbuckets > SIZE_MAX / sizeof (Elf32_Word))
3196 : : data = NULL;
3197 : : else
3198 : : #endif
3199 : 0 : data = elf_getdata_rawchunk (ebl->elf, buckets_at,
3200 : : nbuckets * sizeof (Elf32_Word),
3201 : : ELF_T_WORD);
3202 [ # # ]: 0 : if (data != NULL && symndx < nbuckets)
3203 : : {
3204 : 0 : const Elf32_Word *const buckets = data->d_buf;
3205 : 0 : Elf32_Word maxndx = symndx;
3206 [ # # ]: 0 : for (Elf32_Word bucket = 0; bucket < nbuckets; ++bucket)
3207 : 0 : if (buckets[bucket] > maxndx)
3208 : : maxndx = buckets[bucket];
3209 : :
3210 : 0 : GElf_Off hasharr_at
3211 : : = (buckets_at + nbuckets * sizeof (Elf32_Word));
3212 : 0 : hasharr_at += (maxndx - symndx) * sizeof (Elf32_Word);
3213 : 0 : do
3214 : : {
3215 : 0 : data = elf_getdata_rawchunk (
3216 : : ebl->elf, hasharr_at, sizeof (Elf32_Word), ELF_T_WORD);
3217 [ # # # # ]: 0 : if (data != NULL && (*(const Elf32_Word *)data->d_buf & 1u))
3218 : : {
3219 : 0 : syments = maxndx + 1;
3220 : 0 : break;
3221 : : }
3222 : 0 : ++maxndx;
3223 : 0 : hasharr_at += sizeof (Elf32_Word);
3224 : : }
3225 [ # # ]: 0 : while (data != NULL);
3226 : : }
3227 : : }
3228 : : }
3229 [ # # # # ]: 0 : if (offs[i_strtab] > offs[i_symtab] && syments == 0)
3230 : 0 : syments = ((offs[i_strtab] - offs[i_symtab])
3231 : 0 : / gelf_fsize (ebl->elf, ELF_T_SYM, 1, EV_CURRENT));
3232 : :
3233 [ # # # # : 0 : if (syments <= 0 || offs[i_strtab] == 0 || offs[i_symtab] == 0)
# # ]
3234 : : {
3235 : 0 : error_exit (0, _ ("Dynamic symbol information is not available for "
3236 : : "displaying symbols."));
3237 : : }
3238 : :
3239 : : /* All the data chunk initializaion. */
3240 : 0 : Elf_Data *symdata = NULL;
3241 : 0 : Elf_Data *symstrdata = NULL;
3242 : 0 : Elf_Data *versym_data = NULL;
3243 : 0 : Elf_Data *verdef_data = NULL;
3244 : 0 : Elf_Data *verneed_data = NULL;
3245 : :
3246 : 0 : if (offs[i_symtab] != 0)
3247 : 0 : symdata = elf_getdata_rawchunk (
3248 : : ebl->elf, offs[i_symtab],
3249 : : gelf_fsize (ebl->elf, ELF_T_SYM, syments, EV_CURRENT), ELF_T_SYM);
3250 : :
3251 [ # # ]: 0 : if (offs[i_strtab] != 0 && addrs[i_strsz] != 0)
3252 : 0 : symstrdata = elf_getdata_rawchunk (ebl->elf, offs[i_strtab], addrs[i_strsz],
3253 : : ELF_T_BYTE);
3254 : :
3255 [ # # ]: 0 : if (offs[i_versym] != 0)
3256 : 0 : versym_data = elf_getdata_rawchunk (
3257 : : ebl->elf, offs[i_versym], syments * sizeof (Elf64_Half), ELF_T_HALF);
3258 : :
3259 : : /* Get the verneed_data without vernaux. */
3260 [ # # # # ]: 0 : if (offs[i_verneed] != 0 && addrs[i_verneednum] != 0)
3261 : : {
3262 : 0 : verneed_data = elf_getdata_rawchunk (
3263 : : ebl->elf, offs[i_verneed], addrs[i_verneednum] * sizeof (Elf64_Verneed),
3264 : : ELF_T_VNEED);
3265 : :
3266 [ # # ]: 0 : if (verneed_data->d_size < sizeof (GElf_Verneed))
3267 : 0 : error_exit (0, _("malformed SHT_GNU_verneed data"));
3268 : : }
3269 : :
3270 : 0 : size_t vernauxnum = 0;
3271 : 0 : size_t vn_next_offset = 0;
3272 : :
3273 [ # # ]: 0 : if (verneed_data != NULL && verneed_data->d_buf != NULL)
3274 [ # # ]: 0 : for (size_t i = 0; i < addrs[i_verneednum]; i++)
3275 : : {
3276 [ # # ]: 0 : if (vn_next_offset > (verneed_data->d_size - sizeof (GElf_Verneed)))
3277 : 0 : error_exit (0, _("invalid SHT_GNU_verneed data"));
3278 : :
3279 : 0 : GElf_Verneed *verneed
3280 : : = (GElf_Verneed *)(verneed_data->d_buf + vn_next_offset);
3281 : 0 : vernauxnum += verneed->vn_cnt;
3282 : 0 : vn_next_offset += verneed->vn_next;
3283 : : }
3284 : :
3285 : : /* Update the verneed_data to include the vernaux. */
3286 [ # # # # ]: 0 : if (offs[i_verneed] != 0 && addrs[i_verneednum] != 0)
3287 : 0 : verneed_data = elf_getdata_rawchunk (
3288 : : ebl->elf, offs[i_verneed],
3289 : 0 : (addrs[i_verneednum] + vernauxnum) * sizeof (GElf_Verneed),
3290 : : ELF_T_VNEED);
3291 : :
3292 : : /* Get the verdef_data without verdaux. */
3293 [ # # # # ]: 0 : if (offs[i_verdef] != 0 && addrs[i_verdefnum] != 0)
3294 : : {
3295 : 0 : verdef_data = elf_getdata_rawchunk (
3296 : : ebl->elf, offs[i_verdef], addrs[i_verdefnum] * sizeof (Elf64_Verdef),
3297 : : ELF_T_VDEF);
3298 : :
3299 [ # # ]: 0 : if (verdef_data->d_size < sizeof (GElf_Verdef))
3300 : 0 : error_exit (0, _("malformed SHT_GNU_verdef data"));
3301 : : }
3302 : :
3303 : 0 : size_t verdauxnum = 0;
3304 : 0 : size_t vd_next_offset = 0;
3305 : :
3306 [ # # ]: 0 : if (verdef_data != NULL && verdef_data->d_buf != NULL)
3307 [ # # ]: 0 : for (size_t i = 0; i < addrs[i_verdefnum]; i++)
3308 : : {
3309 [ # # ]: 0 : if (vd_next_offset > (verdef_data->d_size - sizeof (GElf_Verdef)))
3310 : 0 : error_exit (0, _("invalid SHT_GNU_verdef data"));
3311 : :
3312 : 0 : GElf_Verdef *verdef
3313 : : = (GElf_Verdef *)(verdef_data->d_buf + vd_next_offset);
3314 : 0 : verdauxnum += verdef->vd_cnt;
3315 : 0 : vd_next_offset += verdef->vd_next;
3316 : : }
3317 : :
3318 : : /* Update the verdef_data to include the verdaux. */
3319 [ # # # # ]: 0 : if (offs[i_verdef] != 0 && addrs[i_verdefnum] != 0)
3320 : 0 : verdef_data = elf_getdata_rawchunk (
3321 : : ebl->elf, offs[i_verdef],
3322 : 0 : (addrs[i_verdefnum] + verdauxnum) * sizeof (GElf_Verdef), ELF_T_VDEF);
3323 : :
3324 : 0 : unsigned int nsyms = (unsigned int)syments;
3325 : 0 : process_symtab (ebl, nsyms, 0, 0, 0, symdata, versym_data, symstrdata,
3326 : : verneed_data, verdef_data, NULL);
3327 : 0 : return true;
3328 : : }
3329 : :
3330 : :
3331 : : /* Print version information. */
3332 : : static void
3333 : 184 : print_verinfo (Ebl *ebl)
3334 : : {
3335 : : /* Find the version information sections. For this we have to
3336 : : search through the section table. */
3337 : 184 : Elf_Scn *scn = NULL;
3338 : :
3339 [ + + ]: 6004 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
3340 : : {
3341 : : /* Handle the section if it is part of the versioning handling. */
3342 : 5820 : GElf_Shdr shdr_mem;
3343 : 5820 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
3344 : :
3345 [ + - ]: 5820 : if (likely (shdr != NULL))
3346 : : {
3347 [ + + ]: 5820 : if (shdr->sh_type == SHT_GNU_verneed)
3348 : 130 : handle_verneed (ebl, scn, shdr);
3349 [ + + ]: 5690 : else if (shdr->sh_type == SHT_GNU_verdef)
3350 : 4 : handle_verdef (ebl, scn, shdr);
3351 [ + + ]: 5686 : else if (shdr->sh_type == SHT_GNU_versym)
3352 : 130 : handle_versym (ebl, scn, shdr);
3353 : : }
3354 : : }
3355 : 184 : }
3356 : :
3357 : :
3358 : : static const char *
3359 : 380 : get_ver_flags (unsigned int flags)
3360 : : {
3361 : 380 : static char buf[32];
3362 : 380 : char *endp;
3363 : :
3364 [ + + ]: 380 : if (flags == 0)
3365 : 376 : return _("none");
3366 : :
3367 [ + - ]: 4 : if (flags & VER_FLG_BASE)
3368 : 4 : endp = stpcpy (buf, "BASE ");
3369 : : else
3370 : : endp = buf;
3371 : :
3372 [ - + ]: 4 : if (flags & VER_FLG_WEAK)
3373 : : {
3374 [ # # ]: 0 : if (endp != buf)
3375 : 0 : endp = stpcpy (endp, "| ");
3376 : :
3377 : 0 : endp = stpcpy (endp, "WEAK ");
3378 : : }
3379 : :
3380 [ - + ]: 4 : if (unlikely (flags & ~(VER_FLG_BASE | VER_FLG_WEAK)))
3381 : : {
3382 : 0 : strncpy (endp, _("| <unknown>"), buf + sizeof (buf) - endp);
3383 : 0 : buf[sizeof (buf) - 1] = '\0';
3384 : : }
3385 : :
3386 : : return buf;
3387 : : }
3388 : :
3389 : :
3390 : : static void
3391 : 130 : handle_verneed (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr)
3392 : : {
3393 : 130 : int class = gelf_getclass (ebl->elf);
3394 : :
3395 : : /* Get the data of the section. */
3396 : 130 : Elf_Data *data = elf_getdata (scn, NULL);
3397 [ - + ]: 130 : if (data == NULL)
3398 : 0 : return;
3399 : :
3400 : : /* Get the section header string table index. */
3401 : 130 : size_t shstrndx;
3402 [ - + ]: 130 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
3403 : 0 : error_exit (0, _("cannot get section header string table index"));
3404 : :
3405 : 130 : GElf_Shdr glink_mem;
3406 : 130 : GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
3407 : : &glink_mem);
3408 [ - + ]: 130 : if (glink == NULL)
3409 : 0 : error_exit (0, _("invalid sh_link value in section %zu"),
3410 : : elf_ndxscn (scn));
3411 : :
3412 : 130 : printf (ngettext ("\
3413 : : \nVersion needs section [%2u] '%s' contains %d entry:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'\n",
3414 : : "\
3415 : : \nVersion needs section [%2u] '%s' contains %d entries:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'\n",
3416 : : shdr->sh_info),
3417 : 130 : (unsigned int) elf_ndxscn (scn),
3418 : 130 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name), shdr->sh_info,
3419 : : class == ELFCLASS32 ? 10 : 18, shdr->sh_addr,
3420 : : shdr->sh_offset,
3421 [ + + ]: 130 : (unsigned int) shdr->sh_link,
3422 : 130 : elf_strptr (ebl->elf, shstrndx, glink->sh_name));
3423 : :
3424 : 130 : unsigned int offset = 0;
3425 [ + - ]: 184 : for (unsigned int cnt = shdr->sh_info; cnt > 0; cnt--)
3426 : : {
3427 : : /* Get the data at the next offset. */
3428 : 184 : GElf_Verneed needmem;
3429 : 184 : GElf_Verneed *need = gelf_getverneed (data, offset, &needmem);
3430 [ + - ]: 184 : if (unlikely (need == NULL))
3431 : : break;
3432 : :
3433 : 552 : printf (_(" %#06x: Version: %hu File: %s Cnt: %hu\n"),
3434 : 184 : offset, (unsigned short int) need->vn_version,
3435 : 184 : elf_strptr (ebl->elf, shdr->sh_link, need->vn_file),
3436 : 184 : (unsigned short int) need->vn_cnt);
3437 : :
3438 : 184 : unsigned int auxoffset = offset + need->vn_aux;
3439 [ + - ]: 356 : for (unsigned int cnt2 = need->vn_cnt; cnt2 > 0; cnt2--)
3440 : : {
3441 : 356 : GElf_Vernaux auxmem;
3442 : 356 : GElf_Vernaux *aux = gelf_getvernaux (data, auxoffset, &auxmem);
3443 [ + - ]: 356 : if (unlikely (aux == NULL))
3444 : : break;
3445 : :
3446 : 356 : printf (_(" %#06x: Name: %s Flags: %s Version: %hu\n"),
3447 : : auxoffset,
3448 : 356 : elf_strptr (ebl->elf, shdr->sh_link, aux->vna_name),
3449 : 356 : get_ver_flags (aux->vna_flags),
3450 : 356 : (unsigned short int) aux->vna_other);
3451 : :
3452 [ + + ]: 356 : if (aux->vna_next == 0)
3453 : : break;
3454 : :
3455 : 172 : auxoffset += aux->vna_next;
3456 : : }
3457 : :
3458 : : /* Find the next offset. */
3459 [ + + ]: 184 : if (need->vn_next == 0)
3460 : : break;
3461 : :
3462 : 54 : offset += need->vn_next;
3463 : : }
3464 : : }
3465 : :
3466 : :
3467 : : static void
3468 : 4 : handle_verdef (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr)
3469 : : {
3470 : : /* Get the data of the section. */
3471 : 4 : Elf_Data *data = elf_getdata (scn, NULL);
3472 [ - + ]: 4 : if (data == NULL)
3473 : 0 : return;
3474 : :
3475 : : /* Get the section header string table index. */
3476 : 4 : size_t shstrndx;
3477 [ - + ]: 4 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
3478 : 0 : error_exit (0, _("cannot get section header string table index"));
3479 : :
3480 : 4 : GElf_Shdr glink_mem;
3481 : 4 : GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
3482 : : &glink_mem);
3483 [ - + ]: 4 : if (glink == NULL)
3484 : 0 : error_exit (0, _("invalid sh_link value in section %zu"),
3485 : : elf_ndxscn (scn));
3486 : :
3487 : 4 : int class = gelf_getclass (ebl->elf);
3488 : 4 : printf (ngettext ("\
3489 : : \nVersion definition section [%2u] '%s' contains %d entry:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'\n",
3490 : : "\
3491 : : \nVersion definition section [%2u] '%s' contains %d entries:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'\n",
3492 : : shdr->sh_info),
3493 : 4 : (unsigned int) elf_ndxscn (scn),
3494 : 4 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
3495 : : shdr->sh_info,
3496 : : class == ELFCLASS32 ? 10 : 18, shdr->sh_addr,
3497 : : shdr->sh_offset,
3498 [ + - ]: 4 : (unsigned int) shdr->sh_link,
3499 : 4 : elf_strptr (ebl->elf, shstrndx, glink->sh_name));
3500 : :
3501 : 4 : unsigned int offset = 0;
3502 [ + - ]: 24 : for (unsigned int cnt = shdr->sh_info; cnt > 0; cnt--)
3503 : : {
3504 : : /* Get the data at the next offset. */
3505 : 24 : GElf_Verdef defmem;
3506 : 24 : GElf_Verdef *def = gelf_getverdef (data, offset, &defmem);
3507 [ + - ]: 24 : if (unlikely (def == NULL))
3508 : : break;
3509 : :
3510 : 24 : unsigned int auxoffset = offset + def->vd_aux;
3511 : 24 : GElf_Verdaux auxmem;
3512 : 24 : GElf_Verdaux *aux = gelf_getverdaux (data, auxoffset, &auxmem);
3513 [ + - ]: 24 : if (unlikely (aux == NULL))
3514 : : break;
3515 : :
3516 : 96 : printf (_("\
3517 : : %#06x: Version: %hd Flags: %s Index: %hd Cnt: %hd Name: %s\n"),
3518 : 24 : offset, def->vd_version,
3519 : 24 : get_ver_flags (def->vd_flags),
3520 : 24 : def->vd_ndx,
3521 : 24 : def->vd_cnt,
3522 : 24 : elf_strptr (ebl->elf, shdr->sh_link, aux->vda_name));
3523 : :
3524 : 24 : auxoffset += aux->vda_next;
3525 [ + + ]: 24 : for (unsigned int cnt2 = 1; cnt2 < def->vd_cnt; ++cnt2)
3526 : : {
3527 : 16 : aux = gelf_getverdaux (data, auxoffset, &auxmem);
3528 [ + - ]: 16 : if (unlikely (aux == NULL))
3529 : : break;
3530 : :
3531 : 16 : printf (_(" %#06x: Parent %d: %s\n"),
3532 : : auxoffset, cnt2,
3533 : 16 : elf_strptr (ebl->elf, shdr->sh_link, aux->vda_name));
3534 : :
3535 [ - + ]: 16 : if (aux->vda_next == 0)
3536 : : break;
3537 : :
3538 : 0 : auxoffset += aux->vda_next;
3539 : : }
3540 : :
3541 : : /* Find the next offset. */
3542 [ + + ]: 24 : if (def->vd_next == 0)
3543 : : break;
3544 : 20 : offset += def->vd_next;
3545 : : }
3546 : : }
3547 : :
3548 : :
3549 : : static void
3550 : 130 : handle_versym (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr)
3551 : : {
3552 : 130 : int class = gelf_getclass (ebl->elf);
3553 : 130 : const char **vername;
3554 : 130 : const char **filename;
3555 : :
3556 : : /* Get the data of the section. */
3557 : 130 : Elf_Data *data = elf_getdata (scn, NULL);
3558 [ + - ]: 130 : if (data == NULL)
3559 : 0 : return;
3560 : :
3561 : : /* Get the section header string table index. */
3562 : 130 : size_t shstrndx;
3563 [ - + ]: 130 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
3564 : 0 : error_exit (0, _("cannot get section header string table index"));
3565 : :
3566 : : /* We have to find the version definition section and extract the
3567 : : version names. */
3568 : : Elf_Scn *defscn = NULL;
3569 : : Elf_Scn *needscn = NULL;
3570 : :
3571 : : Elf_Scn *verscn = NULL;
3572 [ + + ]: 4046 : while ((verscn = elf_nextscn (ebl->elf, verscn)) != NULL)
3573 : : {
3574 : 3916 : GElf_Shdr vershdr_mem;
3575 : 3916 : GElf_Shdr *vershdr = gelf_getshdr (verscn, &vershdr_mem);
3576 : :
3577 [ - + ]: 3916 : if (likely (vershdr != NULL))
3578 : : {
3579 [ + + ]: 3916 : if (vershdr->sh_type == SHT_GNU_verdef)
3580 : : defscn = verscn;
3581 [ + + ]: 3912 : else if (vershdr->sh_type == SHT_GNU_verneed)
3582 : 3916 : needscn = verscn;
3583 : : }
3584 : : }
3585 : :
3586 : 130 : size_t nvername;
3587 [ + - ]: 130 : if (defscn != NULL || needscn != NULL)
3588 : : {
3589 : : /* We have a version information (better should have). Now get
3590 : : the version names. First find the maximum version number. */
3591 : 130 : nvername = 0;
3592 [ + + ]: 130 : if (defscn != NULL)
3593 : : {
3594 : : /* Run through the version definitions and find the highest
3595 : : index. */
3596 : 4 : unsigned int offset = 0;
3597 : 4 : Elf_Data *defdata;
3598 : 4 : GElf_Shdr defshdrmem;
3599 : 4 : GElf_Shdr *defshdr;
3600 : :
3601 : 4 : defdata = elf_getdata (defscn, NULL);
3602 [ + - ]: 4 : if (unlikely (defdata == NULL))
3603 : 0 : return;
3604 : :
3605 : 4 : defshdr = gelf_getshdr (defscn, &defshdrmem);
3606 [ + - ]: 4 : if (unlikely (defshdr == NULL))
3607 : : return;
3608 : :
3609 [ + - ]: 24 : for (unsigned int cnt = 0; cnt < defshdr->sh_info; ++cnt)
3610 : : {
3611 : 24 : GElf_Verdef defmem;
3612 : 24 : GElf_Verdef *def;
3613 : :
3614 : : /* Get the data at the next offset. */
3615 : 24 : def = gelf_getverdef (defdata, offset, &defmem);
3616 [ + - ]: 24 : if (unlikely (def == NULL))
3617 : : break;
3618 : :
3619 : 24 : nvername = MAX (nvername, (size_t) (def->vd_ndx & 0x7fff));
3620 : :
3621 [ + + ]: 24 : if (def->vd_next == 0)
3622 : : break;
3623 : 20 : offset += def->vd_next;
3624 : : }
3625 : : }
3626 [ + - ]: 130 : if (needscn != NULL)
3627 : : {
3628 : 130 : unsigned int offset = 0;
3629 : 130 : Elf_Data *needdata;
3630 : 130 : GElf_Shdr needshdrmem;
3631 : 130 : GElf_Shdr *needshdr;
3632 : :
3633 : 130 : needdata = elf_getdata (needscn, NULL);
3634 [ + - ]: 130 : if (unlikely (needdata == NULL))
3635 : 0 : return;
3636 : :
3637 : 130 : needshdr = gelf_getshdr (needscn, &needshdrmem);
3638 [ + - ]: 130 : if (unlikely (needshdr == NULL))
3639 : : return;
3640 : :
3641 [ + - ]: 184 : for (unsigned int cnt = 0; cnt < needshdr->sh_info; ++cnt)
3642 : : {
3643 : 184 : GElf_Verneed needmem;
3644 : 184 : GElf_Verneed *need;
3645 : 184 : unsigned int auxoffset;
3646 : 184 : int cnt2;
3647 : :
3648 : : /* Get the data at the next offset. */
3649 : 184 : need = gelf_getverneed (needdata, offset, &needmem);
3650 [ + - ]: 184 : if (unlikely (need == NULL))
3651 : : break;
3652 : :
3653 : : /* Run through the auxiliary entries. */
3654 : 184 : auxoffset = offset + need->vn_aux;
3655 [ + - ]: 356 : for (cnt2 = need->vn_cnt; --cnt2 >= 0; )
3656 : : {
3657 : 356 : GElf_Vernaux auxmem;
3658 : 356 : GElf_Vernaux *aux;
3659 : :
3660 : 356 : aux = gelf_getvernaux (needdata, auxoffset, &auxmem);
3661 [ + - ]: 356 : if (unlikely (aux == NULL))
3662 : : break;
3663 : :
3664 : 356 : nvername = MAX (nvername,
3665 : : (size_t) (aux->vna_other & 0x7fff));
3666 : :
3667 [ + + ]: 356 : if (aux->vna_next == 0)
3668 : : break;
3669 : 172 : auxoffset += aux->vna_next;
3670 : : }
3671 : :
3672 [ + + ]: 184 : if (need->vn_next == 0)
3673 : : break;
3674 : 54 : offset += need->vn_next;
3675 : : }
3676 : : }
3677 : :
3678 : : /* This is the number of versions we know about. */
3679 : 130 : ++nvername;
3680 : :
3681 : : /* Allocate the array. */
3682 : 130 : vername = (const char **) alloca (nvername * sizeof (const char *));
3683 [ + + ]: 130 : memset(vername, 0, nvername * sizeof (const char *));
3684 : 130 : filename = (const char **) alloca (nvername * sizeof (const char *));
3685 : 130 : memset(filename, 0, nvername * sizeof (const char *));
3686 : :
3687 : : /* Run through the data structures again and collect the strings. */
3688 [ + + ]: 130 : if (defscn != NULL)
3689 : : {
3690 : : /* Run through the version definitions and find the highest
3691 : : index. */
3692 : 4 : unsigned int offset = 0;
3693 : 4 : Elf_Data *defdata;
3694 : 4 : GElf_Shdr defshdrmem;
3695 : 4 : GElf_Shdr *defshdr;
3696 : :
3697 : 4 : defdata = elf_getdata (defscn, NULL);
3698 [ + - ]: 4 : if (unlikely (defdata == NULL))
3699 : 0 : return;
3700 : :
3701 : 4 : defshdr = gelf_getshdr (defscn, &defshdrmem);
3702 [ + - ]: 4 : if (unlikely (defshdr == NULL))
3703 : : return;
3704 : :
3705 [ + - ]: 24 : for (unsigned int cnt = 0; cnt < defshdr->sh_info; ++cnt)
3706 : : {
3707 : :
3708 : : /* Get the data at the next offset. */
3709 : 24 : GElf_Verdef defmem;
3710 : 24 : GElf_Verdef *def = gelf_getverdef (defdata, offset, &defmem);
3711 [ + - ]: 24 : if (unlikely (def == NULL))
3712 : : break;
3713 : :
3714 : 24 : GElf_Verdaux auxmem;
3715 : 48 : GElf_Verdaux *aux = gelf_getverdaux (defdata,
3716 : 24 : offset + def->vd_aux,
3717 : : &auxmem);
3718 [ + - ]: 24 : if (unlikely (aux == NULL))
3719 : : break;
3720 : :
3721 : 24 : vername[def->vd_ndx & 0x7fff]
3722 : 24 : = elf_strptr (ebl->elf, defshdr->sh_link, aux->vda_name);
3723 : 24 : filename[def->vd_ndx & 0x7fff] = NULL;
3724 : :
3725 [ + + ]: 24 : if (def->vd_next == 0)
3726 : : break;
3727 : 20 : offset += def->vd_next;
3728 : : }
3729 : : }
3730 [ + - ]: 130 : if (needscn != NULL)
3731 : : {
3732 : 130 : unsigned int offset = 0;
3733 : :
3734 : 130 : Elf_Data *needdata = elf_getdata (needscn, NULL);
3735 : 130 : GElf_Shdr needshdrmem;
3736 : 130 : GElf_Shdr *needshdr = gelf_getshdr (needscn, &needshdrmem);
3737 [ - + ]: 130 : if (unlikely (needdata == NULL || needshdr == NULL))
3738 : 0 : return;
3739 : :
3740 [ + - ]: 184 : for (unsigned int cnt = 0; cnt < needshdr->sh_info; ++cnt)
3741 : : {
3742 : : /* Get the data at the next offset. */
3743 : 184 : GElf_Verneed needmem;
3744 : 184 : GElf_Verneed *need = gelf_getverneed (needdata, offset,
3745 : : &needmem);
3746 [ + - ]: 184 : if (unlikely (need == NULL))
3747 : : break;
3748 : :
3749 : : /* Run through the auxiliary entries. */
3750 : 184 : unsigned int auxoffset = offset + need->vn_aux;
3751 [ + - ]: 356 : for (int cnt2 = need->vn_cnt; --cnt2 >= 0; )
3752 : : {
3753 : 356 : GElf_Vernaux auxmem;
3754 : 356 : GElf_Vernaux *aux = gelf_getvernaux (needdata, auxoffset,
3755 : : &auxmem);
3756 [ + - ]: 356 : if (unlikely (aux == NULL))
3757 : : break;
3758 : :
3759 : 356 : vername[aux->vna_other & 0x7fff]
3760 : 356 : = elf_strptr (ebl->elf, needshdr->sh_link, aux->vna_name);
3761 : 356 : filename[aux->vna_other & 0x7fff]
3762 : 356 : = elf_strptr (ebl->elf, needshdr->sh_link, need->vn_file);
3763 : :
3764 [ + + ]: 356 : if (aux->vna_next == 0)
3765 : : break;
3766 : 172 : auxoffset += aux->vna_next;
3767 : : }
3768 : :
3769 [ + + ]: 184 : if (need->vn_next == 0)
3770 : : break;
3771 : 54 : offset += need->vn_next;
3772 : : }
3773 : : }
3774 : : }
3775 : : else
3776 : : {
3777 : : vername = NULL;
3778 : : nvername = 1;
3779 : : filename = NULL;
3780 : : }
3781 : :
3782 : 130 : GElf_Shdr glink_mem;
3783 : 130 : GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
3784 : : &glink_mem);
3785 : 130 : size_t sh_entsize = gelf_fsize (ebl->elf, ELF_T_HALF, 1, EV_CURRENT);
3786 [ - + ]: 130 : if (glink == NULL)
3787 : 0 : error_exit (0, _("invalid sh_link value in section %zu"),
3788 : : elf_ndxscn (scn));
3789 : :
3790 : : /* Print the header. */
3791 : 130 : printf (ngettext ("\
3792 : : \nVersion symbols section [%2u] '%s' contains %d entry:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'",
3793 : : "\
3794 : : \nVersion symbols section [%2u] '%s' contains %d entries:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'",
3795 : : shdr->sh_size / sh_entsize),
3796 : 130 : (unsigned int) elf_ndxscn (scn),
3797 : 130 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
3798 : 130 : (int) (shdr->sh_size / sh_entsize),
3799 : : class == ELFCLASS32 ? 10 : 18, shdr->sh_addr,
3800 : : shdr->sh_offset,
3801 [ + + ]: 130 : (unsigned int) shdr->sh_link,
3802 : 130 : elf_strptr (ebl->elf, shstrndx, glink->sh_name));
3803 : :
3804 : : /* Now we can finally look at the actual contents of this section. */
3805 [ + + ]: 3308 : for (unsigned int cnt = 0; cnt < shdr->sh_size / sh_entsize; ++cnt)
3806 : : {
3807 [ + + ]: 3178 : if (cnt % 2 == 0)
3808 : 1608 : printf ("\n %4d:", cnt);
3809 : :
3810 : 3178 : GElf_Versym symmem;
3811 : 3178 : GElf_Versym *sym = gelf_getversym (data, cnt, &symmem);
3812 [ + - ]: 3178 : if (sym == NULL)
3813 : : break;
3814 : :
3815 [ + + + ]: 3178 : switch (*sym)
3816 : : {
3817 : 238 : ssize_t n;
3818 : 238 : case 0:
3819 : 238 : fputs (_(" 0 *local* "),
3820 : : stdout);
3821 : 238 : break;
3822 : :
3823 : 502 : case 1:
3824 : 502 : fputs (_(" 1 *global* "),
3825 : : stdout);
3826 : 502 : break;
3827 : :
3828 : 2438 : default:
3829 [ + - ]: 4876 : n = printf ("%4d%c%s",
3830 [ + - ]: 2438 : *sym & 0x7fff, *sym & 0x8000 ? 'h' : ' ',
3831 : : (vername != NULL
3832 [ + - ]: 2438 : && (unsigned int) (*sym & 0x7fff) < nvername)
3833 : 2438 : ? vername[*sym & 0x7fff] : "???");
3834 [ + - ]: 2438 : if ((unsigned int) (*sym & 0x7fff) < nvername
3835 [ + - + + ]: 2438 : && filename != NULL && filename[*sym & 0x7fff] != NULL)
3836 : 2134 : n += printf ("(%s)", filename[*sym & 0x7fff]);
3837 : 2438 : printf ("%*s", MAX (0, 33 - (int) n), " ");
3838 : 2438 : break;
3839 : : }
3840 : : }
3841 : 130 : putchar ('\n');
3842 : : }
3843 : :
3844 : :
3845 : : static void
3846 : 130 : print_hash_info (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr, size_t shstrndx,
3847 : : uint_fast32_t maxlength, Elf32_Word nbucket,
3848 : : uint_fast32_t nsyms, uint32_t *lengths, const char *extrastr)
3849 : : {
3850 : 130 : uint32_t *counts = xcalloc (maxlength + 1, sizeof (uint32_t));
3851 : :
3852 [ + + ]: 550 : for (Elf32_Word cnt = 0; cnt < nbucket; ++cnt)
3853 : 420 : ++counts[lengths[cnt]];
3854 : :
3855 : 130 : GElf_Shdr glink_mem;
3856 : 130 : GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf,
3857 : 130 : shdr->sh_link),
3858 : : &glink_mem);
3859 [ - + ]: 130 : if (glink == NULL)
3860 : : {
3861 : 0 : error (0, 0, _("invalid sh_link value in section %zu"),
3862 : : elf_ndxscn (scn));
3863 : 0 : free (counts);
3864 : 0 : return;
3865 : : }
3866 : :
3867 [ + + ]: 260 : printf (ngettext ("\
3868 : : \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",
3869 : : "\
3870 : : \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",
3871 : : nbucket),
3872 : 130 : (unsigned int) elf_ndxscn (scn),
3873 : 130 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
3874 : : (int) nbucket,
3875 : 130 : gelf_getclass (ebl->elf) == ELFCLASS32 ? 10 : 18,
3876 : : shdr->sh_addr,
3877 : : shdr->sh_offset,
3878 : 130 : (unsigned int) shdr->sh_link,
3879 : 130 : elf_strptr (ebl->elf, shstrndx, glink->sh_name));
3880 : :
3881 [ + - ]: 130 : if (extrastr != NULL)
3882 : 130 : fputs (extrastr, stdout);
3883 : :
3884 [ + - ]: 130 : if (likely (nbucket > 0))
3885 : : {
3886 : 130 : uint64_t success = 0;
3887 : :
3888 : : /* xgettext:no-c-format */
3889 : 130 : fputs (_("\
3890 : : Length Number % of total Coverage\n"), stdout);
3891 : 130 : printf (_(" 0 %6" PRIu32 " %5.1f%%\n"),
3892 : 130 : counts[0], (counts[0] * 100.0) / nbucket);
3893 : :
3894 : 130 : uint64_t nzero_counts = 0;
3895 [ + + ]: 228 : for (Elf32_Word cnt = 1; cnt <= maxlength; ++cnt)
3896 : : {
3897 : 98 : nzero_counts += counts[cnt] * cnt;
3898 : 98 : printf (_("\
3899 : : %7d %6" PRIu32 " %5.1f%% %5.1f%%\n"),
3900 : 98 : (int) cnt, counts[cnt], (counts[cnt] * 100.0) / nbucket,
3901 : 98 : (nzero_counts * 100.0) / nsyms);
3902 : : }
3903 : :
3904 : : Elf32_Word acc = 0;
3905 [ + + ]: 228 : for (Elf32_Word cnt = 1; cnt <= maxlength; ++cnt)
3906 : : {
3907 : 98 : acc += cnt;
3908 : 98 : success += counts[cnt] * acc;
3909 : : }
3910 : :
3911 [ + + ]: 130 : if (nzero_counts > 0)
3912 : 62 : printf (_("\
3913 : : Average number of tests: successful lookup: %f\n\
3914 : : unsuccessful lookup: %f\n"),
3915 : 62 : (double) success / (double) nzero_counts,
3916 : 62 : (double) nzero_counts / (double) nbucket);
3917 : : }
3918 : :
3919 : 130 : free (counts);
3920 : : }
3921 : :
3922 : :
3923 : : /* This function handles the traditional System V-style hash table format. */
3924 : : static void
3925 : 0 : handle_sysv_hash (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr, size_t shstrndx)
3926 : : {
3927 : 0 : Elf_Data *data = elf_getdata (scn, NULL);
3928 [ # # ]: 0 : if (unlikely (data == NULL))
3929 : : {
3930 : 0 : error (0, 0, _("cannot get data for section %d: %s"),
3931 : 0 : (int) elf_ndxscn (scn), elf_errmsg (-1));
3932 : 0 : return;
3933 : : }
3934 : :
3935 [ # # ]: 0 : if (unlikely (data->d_size < 2 * sizeof (Elf32_Word)))
3936 : : {
3937 : 0 : invalid_data:
3938 : 0 : error (0, 0, _("invalid data in sysv.hash section %d"),
3939 : 0 : (int) elf_ndxscn (scn));
3940 : 0 : return;
3941 : : }
3942 : :
3943 : 0 : Elf32_Word nbucket = ((Elf32_Word *) data->d_buf)[0];
3944 : 0 : Elf32_Word nchain = ((Elf32_Word *) data->d_buf)[1];
3945 : :
3946 : 0 : uint64_t used_buf = (2ULL + nchain + nbucket) * sizeof (Elf32_Word);
3947 [ # # ]: 0 : if (used_buf > data->d_size)
3948 : 0 : goto invalid_data;
3949 : :
3950 : 0 : Elf32_Word *bucket = &((Elf32_Word *) data->d_buf)[2];
3951 : 0 : Elf32_Word *chain = &((Elf32_Word *) data->d_buf)[2 + nbucket];
3952 : :
3953 : 0 : uint32_t *lengths = xcalloc (nbucket, sizeof (uint32_t));
3954 : :
3955 : 0 : uint_fast32_t maxlength = 0;
3956 : 0 : uint_fast32_t nsyms = 0;
3957 [ # # ]: 0 : for (Elf32_Word cnt = 0; cnt < nbucket; ++cnt)
3958 : : {
3959 : 0 : Elf32_Word inner = bucket[cnt];
3960 : 0 : Elf32_Word chain_len = 0;
3961 [ # # ]: 0 : while (inner > 0 && inner < nchain)
3962 : : {
3963 : 0 : ++nsyms;
3964 : 0 : ++chain_len;
3965 [ # # ]: 0 : if (chain_len > nchain)
3966 : : {
3967 : 0 : error (0, 0, _("invalid chain in sysv.hash section %d"),
3968 : 0 : (int) elf_ndxscn (scn));
3969 : 0 : free (lengths);
3970 : 0 : return;
3971 : : }
3972 [ # # ]: 0 : if (maxlength < ++lengths[cnt])
3973 : 0 : ++maxlength;
3974 : :
3975 : 0 : inner = chain[inner];
3976 : : }
3977 : : }
3978 : :
3979 : 0 : print_hash_info (ebl, scn, shdr, shstrndx, maxlength, nbucket, nsyms,
3980 : : lengths, NULL);
3981 : :
3982 : 0 : free (lengths);
3983 : : }
3984 : :
3985 : :
3986 : : /* This function handles the incorrect, System V-style hash table
3987 : : format some 64-bit architectures use. */
3988 : : static void
3989 : 0 : handle_sysv_hash64 (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr, size_t shstrndx)
3990 : : {
3991 : 0 : Elf_Data *data = elf_getdata (scn, NULL);
3992 [ # # ]: 0 : if (unlikely (data == NULL))
3993 : : {
3994 : 0 : error (0, 0, _("cannot get data for section %d: %s"),
3995 : 0 : (int) elf_ndxscn (scn), elf_errmsg (-1));
3996 : 0 : return;
3997 : : }
3998 : :
3999 [ # # ]: 0 : if (unlikely (data->d_size < 2 * sizeof (Elf64_Xword)))
4000 : : {
4001 : 0 : invalid_data:
4002 : 0 : error (0, 0, _("invalid data in sysv.hash64 section %d"),
4003 : 0 : (int) elf_ndxscn (scn));
4004 : 0 : return;
4005 : : }
4006 : :
4007 : 0 : Elf64_Xword nbucket = ((Elf64_Xword *) data->d_buf)[0];
4008 : 0 : Elf64_Xword nchain = ((Elf64_Xword *) data->d_buf)[1];
4009 : :
4010 : 0 : uint64_t maxwords = data->d_size / sizeof (Elf64_Xword);
4011 : 0 : if (maxwords < 2
4012 [ # # ]: 0 : || maxwords - 2 < nbucket
4013 [ # # ]: 0 : || maxwords - 2 - nbucket < nchain)
4014 : 0 : goto invalid_data;
4015 : :
4016 : 0 : Elf64_Xword *bucket = &((Elf64_Xword *) data->d_buf)[2];
4017 : 0 : Elf64_Xword *chain = &((Elf64_Xword *) data->d_buf)[2 + nbucket];
4018 : :
4019 : 0 : uint32_t *lengths = xcalloc (nbucket, sizeof (uint32_t));
4020 : :
4021 : 0 : uint_fast32_t maxlength = 0;
4022 : 0 : uint_fast32_t nsyms = 0;
4023 [ # # ]: 0 : for (Elf64_Xword cnt = 0; cnt < nbucket; ++cnt)
4024 : : {
4025 : 0 : Elf64_Xword inner = bucket[cnt];
4026 : 0 : Elf64_Xword chain_len = 0;
4027 [ # # ]: 0 : while (inner > 0 && inner < nchain)
4028 : : {
4029 : 0 : ++nsyms;
4030 : 0 : ++chain_len;
4031 [ # # ]: 0 : if (chain_len > nchain)
4032 : : {
4033 : 0 : error (0, 0, _("invalid chain in sysv.hash64 section %d"),
4034 : 0 : (int) elf_ndxscn (scn));
4035 : 0 : free (lengths);
4036 : 0 : return;
4037 : : }
4038 [ # # ]: 0 : if (maxlength < ++lengths[cnt])
4039 : 0 : ++maxlength;
4040 : :
4041 : 0 : inner = chain[inner];
4042 : : }
4043 : : }
4044 : :
4045 : 0 : print_hash_info (ebl, scn, shdr, shstrndx, maxlength, nbucket, nsyms,
4046 : : lengths, NULL);
4047 : :
4048 : 0 : free (lengths);
4049 : : }
4050 : :
4051 : :
4052 : : /* This function handles the GNU-style hash table format. */
4053 : : static void
4054 : 130 : handle_gnu_hash (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr, size_t shstrndx)
4055 : : {
4056 : 130 : uint32_t *lengths = NULL;
4057 : 130 : Elf_Data *data = elf_getdata (scn, NULL);
4058 [ - + ]: 130 : if (unlikely (data == NULL))
4059 : : {
4060 : 0 : error (0, 0, _("cannot get data for section %d: %s"),
4061 : 0 : (int) elf_ndxscn (scn), elf_errmsg (-1));
4062 : 0 : return;
4063 : : }
4064 : :
4065 [ - + ]: 130 : if (unlikely (data->d_size < 4 * sizeof (Elf32_Word)))
4066 : : {
4067 : 0 : invalid_data:
4068 : 0 : free (lengths);
4069 : 0 : error (0, 0, _("invalid data in gnu.hash section %d"),
4070 : 0 : (int) elf_ndxscn (scn));
4071 : 0 : return;
4072 : : }
4073 : :
4074 : 130 : Elf32_Word nbucket = ((Elf32_Word *) data->d_buf)[0];
4075 : 130 : Elf32_Word symbias = ((Elf32_Word *) data->d_buf)[1];
4076 : :
4077 : : /* Next comes the size of the bitmap. It's measured in words for
4078 : : the architecture. It's 32 bits for 32 bit archs, and 64 bits for
4079 : : 64 bit archs. There is always a bloom filter present, so zero is
4080 : : an invalid value. */
4081 : 130 : Elf32_Word bitmask_words = ((Elf32_Word *) data->d_buf)[2];
4082 [ + + ]: 130 : if (gelf_getclass (ebl->elf) == ELFCLASS64)
4083 : 114 : bitmask_words *= 2;
4084 : :
4085 [ - + ]: 130 : if (bitmask_words == 0)
4086 : 0 : goto invalid_data;
4087 : :
4088 : 130 : Elf32_Word shift = ((Elf32_Word *) data->d_buf)[3];
4089 : :
4090 : : /* Is there still room for the sym chain?
4091 : : Use uint64_t calculation to prevent 32bit overflow. */
4092 : 130 : uint64_t used_buf = (4ULL + bitmask_words + nbucket) * sizeof (Elf32_Word);
4093 : 130 : uint32_t max_nsyms = (data->d_size - used_buf) / sizeof (Elf32_Word);
4094 [ - + ]: 130 : if (used_buf > data->d_size)
4095 : 0 : goto invalid_data;
4096 : :
4097 : 130 : lengths = xcalloc (nbucket, sizeof (uint32_t));
4098 : :
4099 : 130 : Elf32_Word *bitmask = &((Elf32_Word *) data->d_buf)[4];
4100 : 130 : Elf32_Word *bucket = &((Elf32_Word *) data->d_buf)[4 + bitmask_words];
4101 : 130 : Elf32_Word *chain = &((Elf32_Word *) data->d_buf)[4 + bitmask_words
4102 : 130 : + nbucket];
4103 : :
4104 : : /* Compute distribution of chain lengths. */
4105 : 130 : uint_fast32_t maxlength = 0;
4106 : 130 : uint_fast32_t nsyms = 0;
4107 [ + + ]: 550 : for (Elf32_Word cnt = 0; cnt < nbucket; ++cnt)
4108 [ + + ]: 420 : if (bucket[cnt] != 0)
4109 : : {
4110 : 232 : Elf32_Word inner = bucket[cnt] - symbias;
4111 : 414 : do
4112 : : {
4113 : 414 : ++nsyms;
4114 [ + + ]: 414 : if (maxlength < ++lengths[cnt])
4115 : 98 : ++maxlength;
4116 [ - + ]: 414 : if (inner >= max_nsyms)
4117 : 0 : goto invalid_data;
4118 : : }
4119 [ + + ]: 414 : while ((chain[inner++] & 1) == 0);
4120 : : }
4121 : :
4122 : : /* Count bits in bitmask. */
4123 : : uint_fast32_t nbits = 0;
4124 [ + + ]: 446 : for (Elf32_Word cnt = 0; cnt < bitmask_words; ++cnt)
4125 : : {
4126 : 316 : uint_fast32_t word = bitmask[cnt];
4127 : :
4128 : 316 : word = (word & 0x55555555) + ((word >> 1) & 0x55555555);
4129 : 316 : word = (word & 0x33333333) + ((word >> 2) & 0x33333333);
4130 : 316 : word = (word & 0x0f0f0f0f) + ((word >> 4) & 0x0f0f0f0f);
4131 : 316 : word = (word & 0x00ff00ff) + ((word >> 8) & 0x00ff00ff);
4132 : 316 : nbits += (word & 0x0000ffff) + ((word >> 16) & 0x0000ffff);
4133 : : }
4134 : :
4135 : 260 : char *str = xasprintf (_("\
4136 : : Symbol Bias: %u\n\
4137 : : Bitmask Size: %zu bytes %" PRIuFAST32 "%% bits set 2nd hash shift: %u\n"),
4138 : : (unsigned int) symbias,
4139 : : bitmask_words * sizeof (Elf32_Word),
4140 : 130 : ((nbits * 100 + 50)
4141 : 130 : / (uint_fast32_t) (bitmask_words
4142 : : * sizeof (Elf32_Word) * 8)),
4143 : : (unsigned int) shift);
4144 : :
4145 : 130 : print_hash_info (ebl, scn, shdr, shstrndx, maxlength, nbucket, nsyms,
4146 : : lengths, str);
4147 : :
4148 : 130 : free (str);
4149 : 130 : free (lengths);
4150 : : }
4151 : :
4152 : :
4153 : : /* Find the symbol table(s). For this we have to search through the
4154 : : section table. */
4155 : : static void
4156 : 184 : handle_hash (Ebl *ebl)
4157 : : {
4158 : : /* Get the section header string table index. */
4159 : 184 : size_t shstrndx;
4160 [ - + ]: 184 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
4161 : 0 : error_exit (0, _("cannot get section header string table index"));
4162 : :
4163 : : Elf_Scn *scn = NULL;
4164 [ + + ]: 6004 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
4165 : : {
4166 : : /* Handle the section if it is a symbol table. */
4167 : 5820 : GElf_Shdr shdr_mem;
4168 : 5820 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
4169 : :
4170 [ + - ]: 5820 : if (likely (shdr != NULL))
4171 : : {
4172 [ + + ]: 5820 : if ((shdr->sh_type == SHT_HASH || shdr->sh_type == SHT_GNU_HASH)
4173 [ - + ]: 130 : && (shdr->sh_flags & SHF_COMPRESSED) != 0)
4174 : : {
4175 [ # # ]: 0 : if (elf_compress (scn, 0, 0) < 0)
4176 : 0 : printf ("WARNING: %s [%zd]\n",
4177 : : _("Couldn't uncompress section"),
4178 : : elf_ndxscn (scn));
4179 : 0 : shdr = gelf_getshdr (scn, &shdr_mem);
4180 [ # # ]: 0 : if (unlikely (shdr == NULL))
4181 : 0 : error_exit (0, _("cannot get section [%zd] header: %s"),
4182 : : elf_ndxscn (scn), elf_errmsg (-1));
4183 : : }
4184 : :
4185 [ - + ]: 5820 : if (shdr->sh_type == SHT_HASH)
4186 : : {
4187 [ # # ]: 0 : if (ebl_sysvhash_entrysize (ebl) == sizeof (Elf64_Xword))
4188 : 0 : handle_sysv_hash64 (ebl, scn, shdr, shstrndx);
4189 : : else
4190 : 0 : handle_sysv_hash (ebl, scn, shdr, shstrndx);
4191 : : }
4192 [ + + ]: 5820 : else if (shdr->sh_type == SHT_GNU_HASH)
4193 : 130 : handle_gnu_hash (ebl, scn, shdr, shstrndx);
4194 : : }
4195 : : }
4196 : 184 : }
4197 : :
4198 : :
4199 : : static void
4200 : 192 : print_liblist (Ebl *ebl)
4201 : : {
4202 : : /* Find the library list sections. For this we have to search
4203 : : through the section table. */
4204 : 192 : Elf_Scn *scn = NULL;
4205 : :
4206 : : /* Get the section header string table index. */
4207 : 192 : size_t shstrndx;
4208 [ - + ]: 192 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
4209 : 0 : error_exit (0, _("cannot get section header string table index"));
4210 : :
4211 [ + + ]: 6130 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
4212 : : {
4213 : 5938 : GElf_Shdr shdr_mem;
4214 : 5938 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
4215 : :
4216 [ + - - + ]: 5938 : if (shdr != NULL && shdr->sh_type == SHT_GNU_LIBLIST)
4217 : : {
4218 : 0 : size_t sh_entsize = gelf_fsize (ebl->elf, ELF_T_LIB, 1, EV_CURRENT);
4219 : 0 : int nentries = shdr->sh_size / sh_entsize;
4220 : 0 : printf (ngettext ("\
4221 : : \nLibrary list section [%2zu] '%s' at offset %#0" PRIx64 " contains %d entry:\n",
4222 : : "\
4223 : : \nLibrary list section [%2zu] '%s' at offset %#0" PRIx64 " contains %d entries:\n",
4224 : : nentries),
4225 : : elf_ndxscn (scn),
4226 : 0 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
4227 : : shdr->sh_offset,
4228 : : nentries);
4229 : :
4230 : 0 : Elf_Data *data = elf_getdata (scn, NULL);
4231 [ # # ]: 0 : if (data == NULL)
4232 : 0 : return;
4233 : :
4234 : 0 : puts (_("\
4235 : : Library Time Stamp Checksum Version Flags"));
4236 : :
4237 [ # # ]: 0 : for (int cnt = 0; cnt < nentries; ++cnt)
4238 : : {
4239 : 0 : GElf_Lib lib_mem;
4240 : 0 : GElf_Lib *lib = gelf_getlib (data, cnt, &lib_mem);
4241 [ # # ]: 0 : if (unlikely (lib == NULL))
4242 : 0 : continue;
4243 : :
4244 : 0 : time_t t = (time_t) lib->l_time_stamp;
4245 : 0 : struct tm *tm = gmtime (&t);
4246 [ # # ]: 0 : if (unlikely (tm == NULL))
4247 : 0 : continue;
4248 : :
4249 : 0 : printf (" [%2d] %-29s %04u-%02u-%02uT%02u:%02u:%02u %08x %-7u %u\n",
4250 : 0 : cnt, elf_strptr (ebl->elf, shdr->sh_link, lib->l_name),
4251 : 0 : tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
4252 : : tm->tm_hour, tm->tm_min, tm->tm_sec,
4253 : 0 : (unsigned int) lib->l_checksum,
4254 : 0 : (unsigned int) lib->l_version,
4255 : 0 : (unsigned int) lib->l_flags);
4256 : : }
4257 : : }
4258 : : }
4259 : : }
4260 : :
4261 : : static inline size_t
4262 : 16 : left (Elf_Data *data,
4263 : : const unsigned char *p)
4264 : : {
4265 : 16 : return (const unsigned char *) data->d_buf + data->d_size - p;
4266 : : }
4267 : :
4268 : : static void
4269 : 192 : print_attributes (Ebl *ebl, const GElf_Ehdr *ehdr)
4270 : : {
4271 : : /* Find the object attributes sections. For this we have to search
4272 : : through the section table. */
4273 : 192 : Elf_Scn *scn = NULL;
4274 : :
4275 : : /* Get the section header string table index. */
4276 : 192 : size_t shstrndx;
4277 [ - + ]: 192 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
4278 : 0 : error_exit (0, _("cannot get section header string table index"));
4279 : :
4280 [ + + ]: 6130 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
4281 : : {
4282 : 5938 : GElf_Shdr shdr_mem;
4283 : 5938 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
4284 : :
4285 [ + - + + ]: 5938 : if (shdr == NULL || (shdr->sh_type != SHT_GNU_ATTRIBUTES
4286 [ + + ]: 5932 : && (shdr->sh_type != SHT_ARM_ATTRIBUTES
4287 [ - + ]: 2 : || ehdr->e_machine != EM_ARM)
4288 [ + + ]: 5930 : && (shdr->sh_type != SHT_CSKY_ATTRIBUTES
4289 [ + - ]: 2 : || ehdr->e_machine != EM_CSKY)
4290 [ - + ]: 5930 : && (shdr->sh_type != SHT_RISCV_ATTRIBUTES
4291 [ # # ]: 0 : || ehdr->e_machine != EM_RISCV)))
4292 : 5930 : continue;
4293 : :
4294 : 8 : printf (_("\
4295 : : \nObject attributes section [%2zu] '%s' of %" PRIu64
4296 : : " bytes at offset %#0" PRIx64 ":\n"),
4297 : : elf_ndxscn (scn),
4298 : 8 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
4299 : : shdr->sh_size, shdr->sh_offset);
4300 : :
4301 : 8 : Elf_Data *data = elf_rawdata (scn, NULL);
4302 [ + - + - ]: 8 : if (unlikely (data == NULL || data->d_size == 0))
4303 : 0 : return;
4304 : :
4305 : 8 : const unsigned char *p = data->d_buf;
4306 : :
4307 : : /* There is only one 'version', A. */
4308 [ + - ]: 8 : if (unlikely (*p++ != 'A'))
4309 : : return;
4310 : :
4311 : 8 : fputs (_(" Owner Size\n"), stdout);
4312 : :
4313 : : /* Loop over the sections. */
4314 [ + + ]: 16 : while (left (data, p) >= 4)
4315 : : {
4316 : : /* Section length. */
4317 : 8 : uint32_t len;
4318 [ + + ]: 8 : memcpy (&len, p, sizeof len);
4319 : :
4320 [ + + ]: 8 : if (MY_ELFDATA != ehdr->e_ident[EI_DATA])
4321 : 4 : CONVERT (len);
4322 : :
4323 [ + - ]: 8 : if (unlikely (len > left (data, p)))
4324 : : break;
4325 : :
4326 : : /* Section vendor name. */
4327 : 8 : const unsigned char *name = p + sizeof len;
4328 : 8 : p += len;
4329 : :
4330 : 8 : unsigned const char *q = memchr (name, '\0', len);
4331 [ + - ]: 8 : if (unlikely (q == NULL))
4332 : : break;
4333 : 8 : ++q;
4334 : :
4335 : 8 : printf (_(" %-13s %4" PRIu32 "\n"), name, len);
4336 : :
4337 : 16 : bool gnu_vendor = (q - name == sizeof "gnu"
4338 [ + + - + ]: 8 : && !memcmp (name, "gnu", sizeof "gnu"));
4339 : :
4340 : : /* Loop over subsections. */
4341 [ + + ]: 8 : if (shdr->sh_type != SHT_GNU_ATTRIBUTES
4342 [ + - ]: 6 : || gnu_vendor)
4343 [ + + ]: 16 : while (q < p)
4344 : : {
4345 : 8 : const unsigned char *const sub = q;
4346 : :
4347 : 8 : unsigned int subsection_tag;
4348 : 8 : get_uleb128 (subsection_tag, q, p);
4349 [ + - ]: 8 : if (unlikely (q >= p))
4350 : : break;
4351 : :
4352 : 8 : uint32_t subsection_len;
4353 [ + - ]: 8 : if (unlikely (p - sub < (ptrdiff_t) sizeof subsection_len))
4354 : : break;
4355 : :
4356 [ + + ]: 8 : memcpy (&subsection_len, q, sizeof subsection_len);
4357 : :
4358 [ + + ]: 8 : if (MY_ELFDATA != ehdr->e_ident[EI_DATA])
4359 : 4 : CONVERT (subsection_len);
4360 : :
4361 : : /* Don't overflow, ptrdiff_t might be 32bits, but signed. */
4362 [ + - + - ]: 8 : if (unlikely (subsection_len == 0
4363 : : || subsection_len >= (uint32_t) PTRDIFF_MAX
4364 : : || p - sub < (ptrdiff_t) subsection_len))
4365 : : break;
4366 : :
4367 : 8 : const unsigned char *r = q + sizeof subsection_len;
4368 : 8 : q = sub + subsection_len;
4369 : :
4370 [ - + ]: 8 : switch (subsection_tag)
4371 : : {
4372 : 0 : default:
4373 : : /* Unknown subsection, print and skip. */
4374 : 0 : printf (_(" %-4u %12" PRIu32 "\n"),
4375 : : subsection_tag, subsection_len);
4376 : 0 : break;
4377 : :
4378 : 8 : case 1: /* Tag_File */
4379 : 8 : printf (_(" File: %11" PRIu32 "\n"),
4380 : : subsection_len);
4381 : :
4382 [ + + ]: 52 : while (r < q)
4383 : : {
4384 : 44 : unsigned int tag;
4385 : 44 : get_uleb128 (tag, r, q);
4386 [ + - ]: 44 : if (unlikely (r >= q))
4387 : : break;
4388 : :
4389 : : /* GNU style tags have either a uleb128 value,
4390 : : when lowest bit is not set, or a string
4391 : : when the lowest bit is set.
4392 : : "compatibility" (32) is special. It has
4393 : : both a string and a uleb128 value. For
4394 : : non-gnu we assume 6 till 31 only take ints.
4395 : : XXX see arm backend, do we need a separate
4396 : : hook? */
4397 : 44 : uint64_t value = 0;
4398 : 44 : const char *string = NULL;
4399 [ + - + + ]: 44 : if (tag == 32 || (tag & 1) == 0
4400 [ + - + + ]: 16 : || (! gnu_vendor && (tag > 5 && tag < 32)))
4401 : : {
4402 : : // Note r >= q check above.
4403 : 42 : get_uleb128 (value, r, q);
4404 [ + - ]: 42 : if (r > q)
4405 : : break;
4406 : : }
4407 [ + - ]: 44 : if (tag == 32
4408 [ + + ]: 44 : || ((tag & 1) != 0
4409 [ + - ]: 16 : && (gnu_vendor
4410 [ + - ]: 16 : || (! gnu_vendor && tag > 32)))
4411 [ + + + + ]: 44 : || (! gnu_vendor && tag > 3 && tag < 6))
4412 : : {
4413 : 2 : string = (const char *) r;
4414 : 2 : r = memchr (r, '\0', q - r);
4415 [ + - ]: 2 : if (r == NULL)
4416 : : break;
4417 : 2 : ++r;
4418 : : }
4419 : :
4420 : 44 : const char *tag_name = NULL;
4421 : 44 : const char *value_name = NULL;
4422 : 44 : ebl_check_object_attribute (ebl, (const char *) name,
4423 : : tag, value,
4424 : : &tag_name, &value_name);
4425 : :
4426 [ + - ]: 44 : if (tag_name != NULL)
4427 : : {
4428 [ - + ]: 44 : if (tag == 32)
4429 : 0 : printf (_(" %s: %" PRId64 ", %s\n"),
4430 : : tag_name, value, string);
4431 [ + + + + ]: 44 : else if (string == NULL && value_name == NULL)
4432 : 2 : printf (_(" %s: %" PRId64 "\n"),
4433 : : tag_name, value);
4434 : : else
4435 : 42 : printf (_(" %s: %s\n"),
4436 : : tag_name, string ?: value_name);
4437 : : }
4438 : : else
4439 : : {
4440 : : /* For "gnu" vendor 32 "compatibility" has
4441 : : already been handled above. */
4442 [ # # # # ]: 0 : assert (tag != 32
4443 : : || strcmp ((const char *) name, "gnu"));
4444 [ # # ]: 0 : if (string == NULL)
4445 : 0 : printf (_(" %u: %" PRId64 "\n"),
4446 : : tag, value);
4447 : : else
4448 : 0 : printf (_(" %u: %s\n"),
4449 : : tag, string);
4450 : : }
4451 : : }
4452 : : }
4453 : : }
4454 : : }
4455 : : }
4456 : : }
4457 : :
4458 : : /* Returns either the (relocated) data from the Dwarf, or tries to get
4459 : : the "raw" (uncompressed) data from the Elf section. Produces a
4460 : : warning if the data cannot be found (or decompressed). */
4461 : : static Elf_Data *
4462 : 810 : get_debug_elf_data (Dwarf *dbg, Ebl *ebl, int idx, Elf_Scn *scn)
4463 : : {
4464 : : /* We prefer to get the section data from the Dwarf because that
4465 : : might have been relocated already. Note this is subtly wrong if
4466 : : there are multiple sections with the same .debug name. */
4467 [ - + ]: 810 : if (dbg->sectiondata[idx] != NULL)
4468 : : return dbg->sectiondata[idx];
4469 : :
4470 : 0 : GElf_Shdr shdr_mem;
4471 : 0 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
4472 [ # # # # ]: 0 : if (shdr != NULL && (shdr->sh_flags & SHF_COMPRESSED) != 0)
4473 : : {
4474 [ # # ]: 0 : if (elf_compress (scn, 0, 0) < 0)
4475 : : {
4476 : 0 : error (0, 0, "%s [%zd] '%s'\n",
4477 : : _("Couldn't uncompress section"),
4478 : : elf_ndxscn (scn), section_name (ebl, shdr));
4479 : 0 : return NULL;
4480 : : }
4481 : : }
4482 : :
4483 : 0 : Elf_Data *data = elf_getdata (scn, NULL);
4484 [ # # ]: 0 : if (data == NULL)
4485 : 0 : error (0, 0, "%s [%zd] '%s': %s\n",
4486 : : _("Couldn't get data from section"),
4487 : : elf_ndxscn (scn), section_name (ebl, shdr), elf_errmsg (-1));
4488 : :
4489 : 0 : return elf_getdata (scn, NULL);
4490 : : }
4491 : :
4492 : : static void
4493 : 2254230 : print_dwarf_addr (Dwfl_Module *dwflmod,
4494 : : int address_size, Dwarf_Addr address, Dwarf_Addr raw,
4495 : : FILE *out)
4496 : : {
4497 : : /* See if there is a name we can give for this address. */
4498 : 2254230 : GElf_Sym sym;
4499 : 2254230 : GElf_Off off = 0;
4500 [ + + ]: 14168 : const char *name = (print_address_names && ! print_unresolved_addresses)
4501 : 14004 : ? dwfl_module_addrinfo (dwflmod, address, &off, &sym, NULL, NULL, NULL)
4502 [ + + ]: 2254230 : : NULL;
4503 : :
4504 : 2254230 : const char *scn;
4505 [ + + ]: 2254230 : if (print_unresolved_addresses)
4506 : : {
4507 : 168 : address = raw;
4508 : 168 : scn = NULL;
4509 : : }
4510 : : else
4511 : : {
4512 : : /* Relativize the address. */
4513 : 2254062 : int n = dwfl_module_relocations (dwflmod);
4514 [ + + ]: 2254062 : int i = n < 1 ? -1 : dwfl_module_relocate_address (dwflmod, &address);
4515 : :
4516 : : /* In an ET_REL file there is a section name to refer to. */
4517 : 2251310 : scn = (i < 0 ? NULL
4518 [ - + ]: 2251310 : : dwfl_module_relocation_info (dwflmod, i, NULL));
4519 : : }
4520 : :
4521 [ - + ]: 2254230 : if ((name != NULL
4522 : 13596 : ? (off != 0
4523 : : ? (scn != NULL
4524 : : ? (address_size == 0
4525 : 2 : ? fprintf (out, "%s+%#" PRIx64 " <%s+%#" PRIx64 ">",
4526 : : scn, address, name, off)
4527 : 8 : : fprintf (out, "%s+%#0*" PRIx64 " <%s+%#" PRIx64 ">",
4528 : 4 : scn, 2 + address_size * 2, address,
4529 : : name, off))
4530 : : : (address_size == 0
4531 : 364 : ? fprintf (out, "%#" PRIx64 " <%s+%#" PRIx64 ">",
4532 : : address, name, off)
4533 : 2156 : : fprintf (out, "%#0*" PRIx64 " <%s+%#" PRIx64 ">",
4534 : 1078 : 2 + address_size * 2, address,
4535 : : name, off)))
4536 : : : (scn != NULL
4537 : : ? (address_size == 0
4538 : 2 : ? fprintf (out, "%s+%#" PRIx64 " <%s>", scn, address, name)
4539 : 22292 : : fprintf (out, "%s+%#0*" PRIx64 " <%s>",
4540 : 11146 : scn, 2 + address_size * 2, address, name))
4541 : : : (address_size == 0
4542 : 160 : ? fprintf (out, "%#" PRIx64 " <%s>", address, name)
4543 : 1680 : : fprintf (out, "%#0*" PRIx64 " <%s>",
4544 : 840 : 2 + address_size * 2, address, name))))
4545 : : : (scn != NULL
4546 : : ? (address_size == 0
4547 : 822822 : ? fprintf (out, "%s+%#" PRIx64, scn, address)
4548 : 2834668 : : fprintf (out, "%s+%#0*" PRIx64,
4549 : 1417334 : scn, 2 + address_size * 2, address))
4550 : : : (address_size == 0
4551 : 70 : ? fprintf (out, "%#" PRIx64, address)
4552 : 816 : : fprintf (out, "%#0*" PRIx64,
4553 [ + + + + : 4508052 : 2 + address_size * 2, address)))) < 0)
+ + + + +
+ + + + +
+ + + + +
+ + + ]
4554 : 0 : error_exit (0, _("sprintf failure"));
4555 : 2254230 : }
4556 : :
4557 : :
4558 : : static const char *
4559 : 2290364 : dwarf_tag_string (unsigned int tag)
4560 : : {
4561 [ - + + - : 2290364 : switch (tag)
- - - - +
+ + + + -
- - - - -
+ - + - +
- - + + -
+ - - - -
- - - + -
+ - + + +
- - - - -
- + - - +
- - - + -
+ + + + -
- - - - +
+ + + - +
+ + + - -
- ]
4562 : : {
4563 : : #define DWARF_ONE_KNOWN_DW_TAG(NAME, CODE) case CODE: return #NAME;
4564 : 2290364 : DWARF_ALL_KNOWN_DW_TAG
4565 : : #undef DWARF_ONE_KNOWN_DW_TAG
4566 : 0 : default:
4567 : 0 : return NULL;
4568 : : }
4569 : : }
4570 : :
4571 : :
4572 : : static const char *
4573 : 8888240 : dwarf_attr_string (unsigned int attrnum)
4574 : : {
4575 [ + - + - : 8888240 : switch (attrnum)
- - - + -
- + + + -
- - - + +
- + - - +
- + - + -
- - - - -
- - - - -
- - - - -
- - - + -
+ - + - +
- - - + +
- - - + -
+ - + + -
- + + + +
- + + + -
+ - - + -
+ - + + -
+ - - + +
+ + - - -
- - + + +
+ - + - +
- - + - +
+ - + - +
+ - + + +
+ + + + -
- + - - -
+ - + - -
- - + + -
+ - - - -
+ - - - +
+ - + - -
- - + + -
- - - - +
+ - - - -
- - - + ]
4576 : : {
4577 : : #define DWARF_ONE_KNOWN_DW_AT(NAME, CODE) case CODE: return #NAME;
4578 : 8888238 : DWARF_ALL_KNOWN_DW_AT
4579 : : #undef DWARF_ONE_KNOWN_DW_AT
4580 : 0 : default:
4581 : 0 : return NULL;
4582 : : }
4583 : : }
4584 : :
4585 : :
4586 : : static const char *
4587 : 8898658 : dwarf_form_string (unsigned int form)
4588 : : {
4589 [ + + + + : 8898658 : switch (form)
+ - - - -
- + - - +
+ + + + +
+ + + - +
+ - - + -
- + - - +
+ + + + +
- + + - -
- + - + ]
4590 : : {
4591 : : #define DWARF_ONE_KNOWN_DW_FORM(NAME, CODE) case CODE: return #NAME;
4592 : 8898654 : DWARF_ALL_KNOWN_DW_FORM
4593 : : #undef DWARF_ONE_KNOWN_DW_FORM
4594 : 0 : default:
4595 : 0 : return NULL;
4596 : : }
4597 : : }
4598 : :
4599 : :
4600 : : static const char *
4601 : 3740 : dwarf_lang_string (unsigned int lang)
4602 : : {
4603 [ - - + - : 3740 : switch (lang)
- - - + -
- + + - +
- - + - -
- - - - -
- - - + -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - +
- ]
4604 : : {
4605 : : #define DWARF_ONE_KNOWN_DW_LANG(NAME, CODE) case CODE: return #NAME;
4606 : 3738 : DWARF_ALL_KNOWN_DW_LANG
4607 : : #undef DWARF_ONE_KNOWN_DW_LANG
4608 : 2 : default:
4609 : 2 : return NULL;
4610 : : }
4611 : : }
4612 : :
4613 : :
4614 : : static const char *
4615 : 4 : dwarf_lname_string (unsigned int lname)
4616 : : {
4617 [ - - - + : 4 : switch (lname)
- + - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - ]
4618 : : {
4619 : : #define DWARF_ONE_KNOWN_DW_LNAME(NAME, CODE) case CODE: return #NAME;
4620 : 4 : DWARF_ALL_KNOWN_DW_LNAME
4621 : : #undef DWARF_ONE_KNOWN_DW_LNAME
4622 : 0 : default:
4623 : 0 : return NULL;
4624 : : }
4625 : : }
4626 : :
4627 : :
4628 : : static const char *
4629 : 7280 : dwarf_inline_string (unsigned int code)
4630 : : {
4631 : 7280 : static const char *const known[] =
4632 : : {
4633 : : #define DWARF_ONE_KNOWN_DW_INL(NAME, CODE) [CODE] = #NAME,
4634 : : DWARF_ALL_KNOWN_DW_INL
4635 : : #undef DWARF_ONE_KNOWN_DW_INL
4636 : : };
4637 : :
4638 : 7280 : if (likely (code < sizeof (known) / sizeof (known[0])))
4639 : 7280 : return known[code];
4640 : :
4641 : : return NULL;
4642 : : }
4643 : :
4644 : :
4645 : : static const char *
4646 : 59402 : dwarf_encoding_string (unsigned int code)
4647 : : {
4648 : 59402 : static const char *const known[] =
4649 : : {
4650 : : #define DWARF_ONE_KNOWN_DW_ATE(NAME, CODE) [CODE] = #NAME,
4651 : : DWARF_ALL_KNOWN_DW_ATE
4652 : : #undef DWARF_ONE_KNOWN_DW_ATE
4653 : : };
4654 : :
4655 : 59402 : if (likely (code < sizeof (known) / sizeof (known[0])))
4656 : 59402 : return known[code];
4657 : :
4658 : : return NULL;
4659 : : }
4660 : :
4661 : :
4662 : : static const char *
4663 : 0 : dwarf_access_string (unsigned int code)
4664 : : {
4665 : 0 : static const char *const known[] =
4666 : : {
4667 : : #define DWARF_ONE_KNOWN_DW_ACCESS(NAME, CODE) [CODE] = #NAME,
4668 : : DWARF_ALL_KNOWN_DW_ACCESS
4669 : : #undef DWARF_ONE_KNOWN_DW_ACCESS
4670 : : };
4671 : :
4672 : 0 : if (likely (code < sizeof (known) / sizeof (known[0])))
4673 : 0 : return known[code];
4674 : :
4675 : : return NULL;
4676 : : }
4677 : :
4678 : :
4679 : : static const char *
4680 : 0 : dwarf_defaulted_string (unsigned int code)
4681 : : {
4682 : 0 : static const char *const known[] =
4683 : : {
4684 : : #define DWARF_ONE_KNOWN_DW_DEFAULTED(NAME, CODE) [CODE] = #NAME,
4685 : : DWARF_ALL_KNOWN_DW_DEFAULTED
4686 : : #undef DWARF_ONE_KNOWN_DW_DEFAULTED
4687 : : };
4688 : :
4689 : 0 : if (likely (code < sizeof (known) / sizeof (known[0])))
4690 : 0 : return known[code];
4691 : :
4692 : : return NULL;
4693 : : }
4694 : :
4695 : :
4696 : : static const char *
4697 : 0 : dwarf_visibility_string (unsigned int code)
4698 : : {
4699 : 0 : static const char *const known[] =
4700 : : {
4701 : : #define DWARF_ONE_KNOWN_DW_VIS(NAME, CODE) [CODE] = #NAME,
4702 : : DWARF_ALL_KNOWN_DW_VIS
4703 : : #undef DWARF_ONE_KNOWN_DW_VIS
4704 : : };
4705 : :
4706 : 0 : if (likely (code < sizeof (known) / sizeof (known[0])))
4707 : 0 : return known[code];
4708 : :
4709 : : return NULL;
4710 : : }
4711 : :
4712 : :
4713 : : static const char *
4714 : 0 : dwarf_virtuality_string (unsigned int code)
4715 : : {
4716 : 0 : static const char *const known[] =
4717 : : {
4718 : : #define DWARF_ONE_KNOWN_DW_VIRTUALITY(NAME, CODE) [CODE] = #NAME,
4719 : : DWARF_ALL_KNOWN_DW_VIRTUALITY
4720 : : #undef DWARF_ONE_KNOWN_DW_VIRTUALITY
4721 : : };
4722 : :
4723 : 0 : if (likely (code < sizeof (known) / sizeof (known[0])))
4724 : 0 : return known[code];
4725 : :
4726 : : return NULL;
4727 : : }
4728 : :
4729 : :
4730 : : static const char *
4731 : 0 : dwarf_identifier_case_string (unsigned int code)
4732 : : {
4733 : 0 : static const char *const known[] =
4734 : : {
4735 : : #define DWARF_ONE_KNOWN_DW_ID(NAME, CODE) [CODE] = #NAME,
4736 : : DWARF_ALL_KNOWN_DW_ID
4737 : : #undef DWARF_ONE_KNOWN_DW_ID
4738 : : };
4739 : :
4740 : 0 : if (likely (code < sizeof (known) / sizeof (known[0])))
4741 : 0 : return known[code];
4742 : :
4743 : : return NULL;
4744 : : }
4745 : :
4746 : :
4747 : : static const char *
4748 : 0 : dwarf_calling_convention_string (unsigned int code)
4749 : : {
4750 : 0 : static const char *const known[] =
4751 : : {
4752 : : #define DWARF_ONE_KNOWN_DW_CC(NAME, CODE) [CODE] = #NAME,
4753 : : DWARF_ALL_KNOWN_DW_CC
4754 : : #undef DWARF_ONE_KNOWN_DW_CC
4755 : : };
4756 : :
4757 : 0 : if (likely (code < sizeof (known) / sizeof (known[0])))
4758 : 0 : return known[code];
4759 : :
4760 : : return NULL;
4761 : : }
4762 : :
4763 : :
4764 : : static const char *
4765 : 0 : dwarf_ordering_string (unsigned int code)
4766 : : {
4767 : 0 : static const char *const known[] =
4768 : : {
4769 : : #define DWARF_ONE_KNOWN_DW_ORD(NAME, CODE) [CODE] = #NAME,
4770 : : DWARF_ALL_KNOWN_DW_ORD
4771 : : #undef DWARF_ONE_KNOWN_DW_ORD
4772 : : };
4773 : :
4774 : 0 : if (likely (code < sizeof (known) / sizeof (known[0])))
4775 : 0 : return known[code];
4776 : :
4777 : : return NULL;
4778 : : }
4779 : :
4780 : :
4781 : : static const char *
4782 : 16 : dwarf_discr_list_string (unsigned int code)
4783 : : {
4784 : 16 : static const char *const known[] =
4785 : : {
4786 : : #define DWARF_ONE_KNOWN_DW_DSC(NAME, CODE) [CODE] = #NAME,
4787 : : DWARF_ALL_KNOWN_DW_DSC
4788 : : #undef DWARF_ONE_KNOWN_DW_DSC
4789 : : };
4790 : :
4791 : 16 : if (likely (code < sizeof (known) / sizeof (known[0])))
4792 : 16 : return known[code];
4793 : :
4794 : : return NULL;
4795 : : }
4796 : :
4797 : :
4798 : : static const char *
4799 : 1116230 : dwarf_locexpr_opcode_string (unsigned int code)
4800 : : {
4801 : 1116230 : static const char *const known[] =
4802 : : {
4803 : : /* Normally we can't afford building huge table of 64K entries,
4804 : : most of them zero, just because there are a couple defined
4805 : : values at the far end. In case of opcodes, it's OK. */
4806 : : #define DWARF_ONE_KNOWN_DW_OP(NAME, CODE) [CODE] = #NAME,
4807 : : DWARF_ALL_KNOWN_DW_OP
4808 : : #undef DWARF_ONE_KNOWN_DW_OP
4809 : : };
4810 : :
4811 : 1116230 : if (likely (code < sizeof (known) / sizeof (known[0])))
4812 : 1116230 : return known[code];
4813 : :
4814 : : return NULL;
4815 : : }
4816 : :
4817 : :
4818 : : static const char *
4819 : 3486 : dwarf_unit_string (unsigned int type)
4820 : : {
4821 [ - + - - : 3486 : switch (type)
- - + ]
4822 : : {
4823 : : #define DWARF_ONE_KNOWN_DW_UT(NAME, CODE) case CODE: return #NAME;
4824 : 14 : DWARF_ALL_KNOWN_DW_UT
4825 : : #undef DWARF_ONE_KNOWN_DW_UT
4826 : 0 : default:
4827 : 0 : return NULL;
4828 : : }
4829 : : }
4830 : :
4831 : :
4832 : : static const char *
4833 : 148348 : dwarf_range_list_encoding_string (unsigned int kind)
4834 : : {
4835 [ - + + - : 148348 : switch (kind)
+ - - -
+ ]
4836 : : {
4837 : : #define DWARF_ONE_KNOWN_DW_RLE(NAME, CODE) case CODE: return #NAME;
4838 : 131330 : DWARF_ALL_KNOWN_DW_RLE
4839 : : #undef DWARF_ONE_KNOWN_DW_RLE
4840 : 0 : default:
4841 : 0 : return NULL;
4842 : : }
4843 : : }
4844 : :
4845 : :
4846 : : static const char *
4847 : 653976 : dwarf_loc_list_encoding_string (unsigned int kind)
4848 : : {
4849 [ - - + + : 653976 : switch (kind)
- + - + -
- + ]
4850 : : {
4851 : : #define DWARF_ONE_KNOWN_DW_LLE(NAME, CODE) case CODE: return #NAME;
4852 : 608358 : DWARF_ALL_KNOWN_DW_LLE
4853 : : #undef DWARF_ONE_KNOWN_DW_LLE
4854 : : /* DW_LLE_GNU_view_pair is special/incompatible with default codes. */
4855 : 0 : case DW_LLE_GNU_view_pair: return "GNU_view_pair";
4856 : 0 : default:
4857 : 0 : return NULL;
4858 : : }
4859 : : }
4860 : :
4861 : :
4862 : : static const char *
4863 : 10418 : dwarf_line_content_description_string (unsigned int kind)
4864 : : {
4865 [ + + - - : 10418 : switch (kind)
- + ]
4866 : : {
4867 : : #define DWARF_ONE_KNOWN_DW_LNCT(NAME, CODE) case CODE: return #NAME;
4868 : 10410 : DWARF_ALL_KNOWN_DW_LNCT
4869 : : #undef DWARF_ONE_KNOWN_DW_LNCT
4870 : 0 : default:
4871 : 0 : return NULL;
4872 : : }
4873 : : }
4874 : :
4875 : :
4876 : : /* Used by all dwarf_foo_name functions. */
4877 : : static const char *
4878 : 20963932 : string_or_unknown (const char *known, unsigned int code,
4879 : : unsigned int lo_user, unsigned int hi_user,
4880 : : bool print_unknown_num)
4881 : : {
4882 : 20963932 : static char unknown_buf[20];
4883 : :
4884 [ + + ]: 20963932 : if (likely (known != NULL))
4885 : : return known;
4886 : :
4887 [ - + - - ]: 2 : if (lo_user != 0 && code >= lo_user && code <= hi_user)
4888 : : {
4889 : 0 : snprintf (unknown_buf, sizeof unknown_buf, "lo_user+%#x",
4890 : : code - lo_user);
4891 : 0 : return unknown_buf;
4892 : : }
4893 : :
4894 [ + - ]: 2 : if (print_unknown_num)
4895 : : {
4896 : 2 : snprintf (unknown_buf, sizeof unknown_buf, "??? (%#x)", code);
4897 : 2 : return unknown_buf;
4898 : : }
4899 : :
4900 : : return "???";
4901 : : }
4902 : :
4903 : :
4904 : : static const char *
4905 : 2290364 : dwarf_tag_name (unsigned int tag)
4906 : : {
4907 : 2290364 : const char *ret = dwarf_tag_string (tag);
4908 : 2290364 : return string_or_unknown (ret, tag, DW_TAG_lo_user, DW_TAG_hi_user, true);
4909 : : }
4910 : :
4911 : : static const char *
4912 : 8888240 : dwarf_attr_name (unsigned int attr)
4913 : : {
4914 : 8888240 : const char *ret = dwarf_attr_string (attr);
4915 : 8888240 : return string_or_unknown (ret, attr, DW_AT_lo_user, DW_AT_hi_user, true);
4916 : : }
4917 : :
4918 : :
4919 : : static const char *
4920 : 8898658 : dwarf_form_name (unsigned int form)
4921 : : {
4922 : 8898658 : const char *ret = dwarf_form_string (form);
4923 : 8898658 : return string_or_unknown (ret, form, 0, 0, true);
4924 : : }
4925 : :
4926 : :
4927 : : static const char *
4928 : 3736 : dwarf_lang_name (unsigned int lang)
4929 : : {
4930 : 3736 : const char *ret = dwarf_lang_string (lang);
4931 : 3736 : return string_or_unknown (ret, lang, DW_LANG_lo_user, DW_LANG_hi_user, false);
4932 : : }
4933 : :
4934 : :
4935 : : static const char *
4936 : 4 : dwarf_lname_name (unsigned int lname)
4937 : : {
4938 : 4 : const char *ret = dwarf_lname_string (lname);
4939 : 4 : return string_or_unknown (ret, lname, DW_LNAME_lo_user, DW_LNAME_hi_user,
4940 : : false);
4941 : : }
4942 : :
4943 : :
4944 : : static const char *
4945 : 7280 : dwarf_inline_name (unsigned int code)
4946 : : {
4947 [ + - ]: 7280 : const char *ret = dwarf_inline_string (code);
4948 : 7280 : return string_or_unknown (ret, code, 0, 0, false);
4949 : : }
4950 : :
4951 : :
4952 : : static const char *
4953 : 59402 : dwarf_encoding_name (unsigned int code)
4954 : : {
4955 [ + - ]: 59402 : const char *ret = dwarf_encoding_string (code);
4956 : 59402 : return string_or_unknown (ret, code, DW_ATE_lo_user, DW_ATE_hi_user, false);
4957 : : }
4958 : :
4959 : :
4960 : : static const char *
4961 : 0 : dwarf_access_name (unsigned int code)
4962 : : {
4963 [ # # ]: 0 : const char *ret = dwarf_access_string (code);
4964 : 0 : return string_or_unknown (ret, code, 0, 0, false);
4965 : : }
4966 : :
4967 : :
4968 : : static const char *
4969 : 0 : dwarf_defaulted_name (unsigned int code)
4970 : : {
4971 [ # # ]: 0 : const char *ret = dwarf_defaulted_string (code);
4972 : 0 : return string_or_unknown (ret, code, 0, 0, false);
4973 : : }
4974 : :
4975 : :
4976 : : static const char *
4977 : 0 : dwarf_visibility_name (unsigned int code)
4978 : : {
4979 [ # # ]: 0 : const char *ret = dwarf_visibility_string (code);
4980 : 0 : return string_or_unknown (ret, code, 0, 0, false);
4981 : : }
4982 : :
4983 : :
4984 : : static const char *
4985 : 0 : dwarf_virtuality_name (unsigned int code)
4986 : : {
4987 [ # # ]: 0 : const char *ret = dwarf_virtuality_string (code);
4988 : 0 : return string_or_unknown (ret, code, 0, 0, false);
4989 : : }
4990 : :
4991 : :
4992 : : static const char *
4993 : 0 : dwarf_identifier_case_name (unsigned int code)
4994 : : {
4995 [ # # ]: 0 : const char *ret = dwarf_identifier_case_string (code);
4996 : 0 : return string_or_unknown (ret, code, 0, 0, false);
4997 : : }
4998 : :
4999 : :
5000 : : static const char *
5001 : 0 : dwarf_calling_convention_name (unsigned int code)
5002 : : {
5003 [ # # ]: 0 : const char *ret = dwarf_calling_convention_string (code);
5004 : 0 : return string_or_unknown (ret, code, DW_CC_lo_user, DW_CC_hi_user, false);
5005 : : }
5006 : :
5007 : :
5008 : : static const char *
5009 : 0 : dwarf_ordering_name (unsigned int code)
5010 : : {
5011 [ # # ]: 0 : const char *ret = dwarf_ordering_string (code);
5012 : 0 : return string_or_unknown (ret, code, 0, 0, false);
5013 : : }
5014 : :
5015 : :
5016 : : static const char *
5017 : 16 : dwarf_discr_list_name (unsigned int code)
5018 : : {
5019 [ + - ]: 16 : const char *ret = dwarf_discr_list_string (code);
5020 : 16 : return string_or_unknown (ret, code, 0, 0, false);
5021 : : }
5022 : :
5023 : :
5024 : : static const char *
5025 : 3486 : dwarf_unit_name (unsigned int type)
5026 : : {
5027 : 3486 : const char *ret = dwarf_unit_string (type);
5028 : 3486 : return string_or_unknown (ret, type, DW_UT_lo_user, DW_UT_hi_user, true);
5029 : : }
5030 : :
5031 : :
5032 : : static const char *
5033 : 148348 : dwarf_range_list_encoding_name (unsigned int kind)
5034 : : {
5035 : 148348 : const char *ret = dwarf_range_list_encoding_string (kind);
5036 : 148348 : return string_or_unknown (ret, kind, 0, 0, false);
5037 : : }
5038 : :
5039 : :
5040 : : static const char *
5041 : 653976 : dwarf_loc_list_encoding_name (unsigned int kind)
5042 : : {
5043 : 653976 : const char *ret = dwarf_loc_list_encoding_string (kind);
5044 : 653976 : return string_or_unknown (ret, kind, 0, 0, false);
5045 : : }
5046 : :
5047 : :
5048 : : static const char *
5049 : 10418 : dwarf_line_content_description_name (unsigned int kind)
5050 : : {
5051 : 10418 : const char *ret = dwarf_line_content_description_string (kind);
5052 : 10418 : return string_or_unknown (ret, kind, DW_LNCT_lo_user, DW_LNCT_hi_user,
5053 : : false);
5054 : : }
5055 : :
5056 : :
5057 : : static void
5058 : 510 : print_block (size_t n, const void *block, FILE *out)
5059 : : {
5060 [ - + ]: 510 : if (n == 0)
5061 : 0 : fputs (empty_block_str, out);
5062 : : else
5063 : : {
5064 : 510 : fprintf (out, "%zu %s:", n, byte_block_str);
5065 : 510 : const unsigned char *data = block;
5066 : 2250 : do
5067 : 2250 : fprintf (out, " %02x", *data++);
5068 [ + + ]: 2250 : while (--n > 0);
5069 : 510 : fputc ('\n', out);
5070 : : }
5071 : 510 : }
5072 : :
5073 : : static void
5074 : 24 : print_bytes (size_t n, const unsigned char *bytes, FILE *out)
5075 : : {
5076 [ + + ]: 408 : while (n-- > 0)
5077 : : {
5078 : 384 : fprintf (out, "%02x", *bytes++);
5079 [ + + ]: 384 : if (n > 0)
5080 : 360 : fprintf (out, " ");
5081 : : }
5082 : 24 : }
5083 : :
5084 : : static int
5085 : 148 : get_indexed_addr (Dwarf_CU *cu, Dwarf_Word idx, Dwarf_Addr *addr)
5086 : : {
5087 [ - + ]: 148 : if (cu == NULL)
5088 : : return -1;
5089 : :
5090 : 148 : Elf_Data *debug_addr = cu->dbg->sectiondata[IDX_debug_addr];
5091 [ - + ]: 148 : if (debug_addr == NULL)
5092 : : return -1;
5093 : :
5094 : 148 : Dwarf_Off base = __libdw_cu_addr_base (cu);
5095 : 148 : Dwarf_Word off = idx * cu->address_size;
5096 [ - + ]: 148 : if (base > debug_addr->d_size
5097 [ - + ]: 148 : || off > debug_addr->d_size - base
5098 [ - + ]: 148 : || cu->address_size > debug_addr->d_size - base - off)
5099 : : return -1;
5100 : :
5101 : 148 : const unsigned char *addrp = debug_addr->d_buf + base + off;
5102 [ + + ]: 148 : if (cu->address_size == 4)
5103 [ - + ]: 12 : *addr = read_4ubyte_unaligned (cu->dbg, addrp);
5104 : : else
5105 [ - + ]: 136 : *addr = read_8ubyte_unaligned (cu->dbg, addrp);
5106 : :
5107 : : return 0;
5108 : : }
5109 : :
5110 : : static void
5111 : 784886 : print_ops (Dwfl_Module *dwflmod, Dwarf *dbg, int indent, int indentrest,
5112 : : unsigned int vers, unsigned int addrsize, unsigned int offset_size,
5113 : : struct Dwarf_CU *cu, Dwarf_Word len, const unsigned char *data,
5114 : : FILE *out)
5115 : : {
5116 [ + + ]: 784886 : const unsigned int ref_size = vers < 3 ? addrsize : offset_size;
5117 : :
5118 [ - + ]: 784886 : if (len == 0)
5119 : : {
5120 : 0 : fprintf (out, "%*s(empty)\n", indent, "");
5121 : 0 : return;
5122 : : }
5123 : :
5124 : : #define NEED(n) if (len < (Dwarf_Word) (n)) goto invalid
5125 : : #define CONSUME(n) NEED (n); else len -= (n)
5126 : :
5127 : : Dwarf_Word offset = 0;
5128 [ + + ]: 1901116 : while (len-- > 0)
5129 : 1116230 : {
5130 : 1116230 : uint_fast8_t op = *data++;
5131 : :
5132 [ + - ]: 1116230 : const char *op_name = dwarf_locexpr_opcode_string (op);
5133 [ - + ]: 1116230 : if (unlikely (op_name == NULL))
5134 : : {
5135 : 0 : static char buf[20];
5136 [ # # ]: 0 : if (op >= DW_OP_lo_user)
5137 : 0 : snprintf (buf, sizeof buf, "lo_user+%#x", op - DW_OP_lo_user);
5138 : : else
5139 : 0 : snprintf (buf, sizeof buf, "??? (%#x)", op);
5140 : : op_name = buf;
5141 : : }
5142 : :
5143 [ + - + + : 1116230 : switch (op)
+ + + + -
- + + - +
- - + + +
+ + - - -
- + + + ]
5144 : : {
5145 : 27000 : case DW_OP_addr:;
5146 : : /* Address operand. */
5147 : 27000 : Dwarf_Word addr;
5148 [ - + ]: 27000 : NEED (addrsize);
5149 [ + + ]: 27000 : if (addrsize == 4)
5150 [ + + ]: 108 : addr = read_4ubyte_unaligned (dbg, data);
5151 [ + - ]: 26892 : else if (addrsize == 8)
5152 [ + + ]: 26892 : addr = read_8ubyte_unaligned (dbg, data);
5153 : : else
5154 : 0 : goto invalid;
5155 : 27000 : data += addrsize;
5156 : 27000 : CONSUME (addrsize);
5157 : :
5158 : 27000 : fprintf (out, "%*s[%2" PRIuMAX "] %s ",
5159 : : indent, "", (uintmax_t) offset, op_name);
5160 : 27000 : print_dwarf_addr (dwflmod, 0, addr, addr, out);
5161 : 27000 : fprintf (out, "\n");
5162 : :
5163 : 27000 : offset += 1 + addrsize;
5164 : 27000 : break;
5165 : :
5166 : 0 : case DW_OP_call_ref:
5167 : : case DW_OP_GNU_variable_value:
5168 : : /* Offset operand. */
5169 [ # # ]: 0 : if (ref_size != 4 && ref_size != 8)
5170 : 0 : goto invalid; /* Cannot be used in CFA. */
5171 [ # # ]: 0 : NEED (ref_size);
5172 [ # # ]: 0 : if (ref_size == 4)
5173 [ # # ]: 0 : addr = read_4ubyte_unaligned (dbg, data);
5174 : : else
5175 [ # # ]: 0 : addr = read_8ubyte_unaligned (dbg, data);
5176 : 0 : data += ref_size;
5177 : 0 : CONSUME (ref_size);
5178 : : /* addr is a DIE offset, so format it as one. */
5179 : 0 : fprintf (out, "%*s[%2" PRIuMAX "] %s [%6" PRIxMAX "]\n",
5180 : : indent, "", (uintmax_t) offset,
5181 : : op_name, (uintmax_t) addr);
5182 : 0 : offset += 1 + ref_size;
5183 : 0 : break;
5184 : :
5185 : 24770 : case DW_OP_deref_size:
5186 : : case DW_OP_xderef_size:
5187 : : case DW_OP_pick:
5188 : : case DW_OP_const1u:
5189 : : // XXX value might be modified by relocation
5190 [ - + ]: 24770 : NEED (1);
5191 : 49540 : fprintf (out, "%*s[%2" PRIuMAX "] %s %" PRIu8 "\n",
5192 : : indent, "", (uintmax_t) offset,
5193 : 24770 : op_name, *((uint8_t *) data));
5194 : 24770 : ++data;
5195 : 24770 : --len;
5196 : 24770 : offset += 2;
5197 : 24770 : break;
5198 : :
5199 : 3318 : case DW_OP_const2u:
5200 [ - + ]: 3318 : NEED (2);
5201 : : // XXX value might be modified by relocation
5202 : 6636 : fprintf (out, "%*s[%2" PRIuMAX "] %s %" PRIu16 "\n",
5203 : : indent, "", (uintmax_t) offset,
5204 [ - + ]: 3318 : op_name, read_2ubyte_unaligned (dbg, data));
5205 : 3318 : CONSUME (2);
5206 : 3318 : data += 2;
5207 : 3318 : offset += 3;
5208 : 3318 : break;
5209 : :
5210 : 2906 : case DW_OP_const4u:
5211 [ - + ]: 2906 : NEED (4);
5212 : : // XXX value might be modified by relocation
5213 : 2906 : fprintf (out, "%*s[%2" PRIuMAX "] %s %" PRIu32 "\n",
5214 : : indent, "", (uintmax_t) offset,
5215 [ - + ]: 2906 : op_name, read_4ubyte_unaligned (dbg, data));
5216 : 2906 : CONSUME (4);
5217 : 2906 : data += 4;
5218 : 2906 : offset += 5;
5219 : 2906 : break;
5220 : :
5221 : 126 : case DW_OP_const8u:
5222 [ - + ]: 126 : NEED (8);
5223 : : // XXX value might be modified by relocation
5224 : 126 : fprintf (out, "%*s[%2" PRIuMAX "] %s %" PRIu64 "\n",
5225 : : indent, "", (uintmax_t) offset,
5226 [ - + ]: 126 : op_name, (uint64_t) read_8ubyte_unaligned (dbg, data));
5227 : 126 : CONSUME (8);
5228 : 126 : data += 8;
5229 : 126 : offset += 9;
5230 : 126 : break;
5231 : :
5232 : 5132 : case DW_OP_const1s:
5233 [ - + ]: 5132 : NEED (1);
5234 : : // XXX value might be modified by relocation
5235 : 10264 : fprintf (out, "%*s[%2" PRIuMAX "] %s %" PRId8 "\n",
5236 : : indent, "", (uintmax_t) offset,
5237 : 5132 : op_name, *((int8_t *) data));
5238 : 5132 : ++data;
5239 : 5132 : --len;
5240 : 5132 : offset += 2;
5241 : 5132 : break;
5242 : :
5243 : 10 : case DW_OP_const2s:
5244 [ - + ]: 10 : NEED (2);
5245 : : // XXX value might be modified by relocation
5246 : 20 : fprintf (out, "%*s[%2" PRIuMAX "] %s %" PRId16 "\n",
5247 : : indent, "", (uintmax_t) offset,
5248 [ - + ]: 10 : op_name, read_2sbyte_unaligned (dbg, data));
5249 : 10 : CONSUME (2);
5250 : 10 : data += 2;
5251 : 10 : offset += 3;
5252 : 10 : break;
5253 : :
5254 : 0 : case DW_OP_const4s:
5255 [ # # ]: 0 : NEED (4);
5256 : : // XXX value might be modified by relocation
5257 : 0 : fprintf (out, "%*s[%2" PRIuMAX "] %s %" PRId32 "\n",
5258 : : indent, "", (uintmax_t) offset,
5259 [ # # ]: 0 : op_name, read_4sbyte_unaligned (dbg, data));
5260 : 0 : CONSUME (4);
5261 : 0 : data += 4;
5262 : 0 : offset += 5;
5263 : 0 : break;
5264 : :
5265 : 0 : case DW_OP_const8s:
5266 [ # # ]: 0 : NEED (8);
5267 : : // XXX value might be modified by relocation
5268 : 0 : fprintf (out, "%*s[%2" PRIuMAX "] %s %" PRId64 "\n",
5269 : : indent, "", (uintmax_t) offset,
5270 [ # # ]: 0 : op_name, read_8sbyte_unaligned (dbg, data));
5271 : 0 : CONSUME (8);
5272 : 0 : data += 8;
5273 : 0 : offset += 9;
5274 : 0 : break;
5275 : :
5276 : 25378 : case DW_OP_piece:
5277 : : case DW_OP_regx:
5278 : : case DW_OP_plus_uconst:
5279 : 25378 : case DW_OP_constu:;
5280 : 25378 : const unsigned char *start = data;
5281 : 25378 : uint64_t uleb;
5282 [ - + ]: 25378 : NEED (1);
5283 : 25378 : get_uleb128 (uleb, data, data + len);
5284 : 25378 : fprintf (out, "%*s[%2" PRIuMAX "] %s %" PRIu64 "\n",
5285 : : indent, "", (uintmax_t) offset, op_name, uleb);
5286 [ - + ]: 25378 : CONSUME (data - start);
5287 : 25378 : offset += 1 + (data - start);
5288 : 25378 : break;
5289 : :
5290 : 16 : case DW_OP_addrx:
5291 : : case DW_OP_GNU_addr_index:
5292 : : case DW_OP_constx:
5293 : 16 : case DW_OP_GNU_const_index:;
5294 : 16 : start = data;
5295 [ - + ]: 16 : NEED (1);
5296 : 16 : get_uleb128 (uleb, data, data + len);
5297 : 16 : fprintf (out, "%*s[%2" PRIuMAX "] %s [%" PRIu64 "] ",
5298 : : indent, "", (uintmax_t) offset, op_name, uleb);
5299 [ - + ]: 16 : CONSUME (data - start);
5300 : 16 : offset += 1 + (data - start);
5301 [ - + ]: 16 : if (get_indexed_addr (cu, uleb, &addr) != 0)
5302 : 0 : fprintf (out, "???\n");
5303 : : else
5304 : : {
5305 : 16 : print_dwarf_addr (dwflmod, 0, addr, addr, out);
5306 : 16 : fprintf (out, "\n");
5307 : : }
5308 : : break;
5309 : :
5310 : 0 : case DW_OP_bit_piece:
5311 : 0 : start = data;
5312 : 0 : uint64_t uleb2;
5313 [ # # ]: 0 : NEED (1);
5314 : 0 : get_uleb128 (uleb, data, data + len);
5315 : 0 : NEED (1);
5316 : 0 : get_uleb128 (uleb2, data, data + len);
5317 : 0 : fprintf (out, "%*s[%2" PRIuMAX "] %s %" PRIu64 ", %" PRIu64 "\n",
5318 : : indent, "", (uintmax_t) offset, op_name, uleb, uleb2);
5319 [ # # ]: 0 : CONSUME (data - start);
5320 : 0 : offset += 1 + (data - start);
5321 : 0 : break;
5322 : :
5323 : 188856 : case DW_OP_fbreg:
5324 : : case DW_OP_breg0 ... DW_OP_breg31:
5325 : : case DW_OP_consts:
5326 : 188856 : start = data;
5327 : 188856 : int64_t sleb;
5328 [ - + ]: 188856 : NEED (1);
5329 : 188856 : get_sleb128 (sleb, data, data + len);
5330 : 188856 : fprintf (out, "%*s[%2" PRIuMAX "] %s %" PRId64 "\n",
5331 : : indent, "", (uintmax_t) offset, op_name, sleb);
5332 [ - + ]: 188856 : CONSUME (data - start);
5333 : 188856 : offset += 1 + (data - start);
5334 : 188856 : break;
5335 : :
5336 : 0 : case DW_OP_bregx:
5337 : 0 : start = data;
5338 [ # # ]: 0 : NEED (1);
5339 : 0 : get_uleb128 (uleb, data, data + len);
5340 : 0 : NEED (1);
5341 : 0 : get_sleb128 (sleb, data, data + len);
5342 : 0 : fprintf (out, "%*s[%2" PRIuMAX "] %s %" PRIu64 " %" PRId64 "\n",
5343 : : indent, "", (uintmax_t) offset, op_name, uleb, sleb);
5344 [ # # ]: 0 : CONSUME (data - start);
5345 : 0 : offset += 1 + (data - start);
5346 : 0 : break;
5347 : :
5348 : 0 : case DW_OP_call2:
5349 [ # # ]: 0 : NEED (2);
5350 : 0 : fprintf (out, "%*s[%2" PRIuMAX "] %s [%6" PRIx16 "]\n",
5351 : : indent, "", (uintmax_t) offset, op_name,
5352 [ # # ]: 0 : read_2ubyte_unaligned (dbg, data));
5353 : 0 : CONSUME (2);
5354 : 0 : data += 2;
5355 : 0 : offset += 3;
5356 : 0 : break;
5357 : :
5358 : 8 : case DW_OP_call4:
5359 [ - + ]: 8 : NEED (4);
5360 : 8 : fprintf (out, "%*s[%2" PRIuMAX "] %s [%6" PRIx32 "]\n",
5361 : : indent, "", (uintmax_t) offset, op_name,
5362 [ - + ]: 8 : read_4ubyte_unaligned (dbg, data));
5363 : 8 : CONSUME (4);
5364 : 8 : data += 4;
5365 : 8 : offset += 5;
5366 : 8 : break;
5367 : :
5368 : 1120 : case DW_OP_skip:
5369 : : case DW_OP_bra:
5370 [ - + ]: 1120 : NEED (2);
5371 : 2240 : fprintf (out, "%*s[%2" PRIuMAX "] %s %" PRIuMAX "\n",
5372 : : indent, "", (uintmax_t) offset, op_name,
5373 [ - + ]: 1120 : (uintmax_t) (offset + read_2sbyte_unaligned (dbg, data) + 3));
5374 : 1120 : CONSUME (2);
5375 : 1120 : data += 2;
5376 : 1120 : offset += 3;
5377 : 1120 : break;
5378 : :
5379 : 510 : case DW_OP_implicit_value:
5380 : 510 : start = data;
5381 [ - + ]: 510 : NEED (1);
5382 : 510 : get_uleb128 (uleb, data, data + len);
5383 : 510 : fprintf (out, "%*s[%2" PRIuMAX "] %s: ",
5384 : : indent, "", (uintmax_t) offset, op_name);
5385 [ - + ]: 510 : NEED (uleb);
5386 : 510 : print_block (uleb, data, out);
5387 : 510 : data += uleb;
5388 [ - + ]: 510 : CONSUME (data - start);
5389 : 510 : offset += 1 + (data - start);
5390 : 510 : break;
5391 : :
5392 : 5912 : case DW_OP_implicit_pointer:
5393 : : case DW_OP_GNU_implicit_pointer:
5394 : : /* DIE offset operand. */
5395 : 5912 : start = data;
5396 [ - + ]: 5912 : NEED (ref_size);
5397 [ - + ]: 5912 : if (ref_size != 4 && ref_size != 8)
5398 : 0 : goto invalid; /* Cannot be used in CFA. */
5399 [ + - ]: 5912 : if (ref_size == 4)
5400 [ - + ]: 5912 : addr = read_4ubyte_unaligned (dbg, data);
5401 : : else
5402 [ # # ]: 0 : addr = read_8ubyte_unaligned (dbg, data);
5403 : 5912 : data += ref_size;
5404 : : /* Byte offset operand. */
5405 [ - + ]: 5912 : NEED (1);
5406 : 5912 : get_sleb128 (sleb, data, data + len);
5407 : :
5408 : 5912 : fprintf (out, "%*s[%2" PRIuMAX "] %s [%6" PRIxMAX "] %+" PRId64 "\n",
5409 : : indent, "", (intmax_t) offset,
5410 : : op_name, (uintmax_t) addr, sleb);
5411 [ - + ]: 5912 : CONSUME (data - start);
5412 : 5912 : offset += 1 + (data - start);
5413 : 5912 : break;
5414 : :
5415 : 62512 : case DW_OP_entry_value:
5416 : : case DW_OP_GNU_entry_value:
5417 : : /* Size plus expression block. */
5418 : 62512 : start = data;
5419 [ - + ]: 62512 : NEED (1);
5420 : 62512 : get_uleb128 (uleb, data, data + len);
5421 : 62512 : fprintf (out, "%*s[%2" PRIuMAX "] %s:\n",
5422 : : indent, "", (uintmax_t) offset, op_name);
5423 [ - + ]: 62512 : NEED (uleb);
5424 : 62512 : print_ops (dwflmod, dbg, indent + 5, indent + 5, vers,
5425 : : addrsize, offset_size, cu, uleb, data, out);
5426 : 62512 : data += uleb;
5427 [ - + ]: 62512 : CONSUME (data - start);
5428 : 62512 : offset += 1 + (data - start);
5429 : 62512 : break;
5430 : :
5431 : 0 : case DW_OP_const_type:
5432 : : case DW_OP_GNU_const_type:
5433 : : /* uleb128 CU relative DW_TAG_base_type DIE offset, 1-byte
5434 : : unsigned size plus block. */
5435 : 0 : start = data;
5436 [ # # ]: 0 : NEED (1);
5437 : 0 : get_uleb128 (uleb, data, data + len);
5438 [ # # # # ]: 0 : if (! print_unresolved_addresses && cu != NULL)
5439 : 0 : uleb += cu->start;
5440 : 0 : NEED (1);
5441 : 0 : uint8_t usize = *(uint8_t *) data++;
5442 [ # # ]: 0 : NEED (usize);
5443 : 0 : fprintf (out, "%*s[%2" PRIuMAX "] %s [%6" PRIxMAX "] ",
5444 : : indent, "", (uintmax_t) offset, op_name, uleb);
5445 : 0 : print_block (usize, data, out);
5446 : 0 : data += usize;
5447 [ # # ]: 0 : CONSUME (data - start);
5448 : 0 : offset += 1 + (data - start);
5449 : 0 : break;
5450 : :
5451 : 0 : case DW_OP_regval_type:
5452 : : case DW_OP_GNU_regval_type:
5453 : : /* uleb128 register number, uleb128 CU relative
5454 : : DW_TAG_base_type DIE offset. */
5455 : 0 : start = data;
5456 [ # # ]: 0 : NEED (1);
5457 : 0 : get_uleb128 (uleb, data, data + len);
5458 : 0 : NEED (1);
5459 : 0 : get_uleb128 (uleb2, data, data + len);
5460 [ # # # # ]: 0 : if (! print_unresolved_addresses && cu != NULL)
5461 : 0 : uleb2 += cu->start;
5462 : 0 : fprintf (out, "%*s[%2" PRIuMAX "] %s %" PRIu64 " [%6" PRIx64 "]\n",
5463 : : indent, "", (uintmax_t) offset, op_name, uleb, uleb2);
5464 [ # # ]: 0 : CONSUME (data - start);
5465 : 0 : offset += 1 + (data - start);
5466 : 0 : break;
5467 : :
5468 : 0 : case DW_OP_deref_type:
5469 : : case DW_OP_GNU_deref_type:
5470 : : /* 1-byte unsigned size of value, uleb128 CU relative
5471 : : DW_TAG_base_type DIE offset. */
5472 : 0 : start = data;
5473 [ # # ]: 0 : NEED (1);
5474 : 0 : usize = *(uint8_t *) data++;
5475 : 0 : NEED (1);
5476 : 0 : get_uleb128 (uleb, data, data + len);
5477 [ # # # # ]: 0 : if (! print_unresolved_addresses && cu != NULL)
5478 : 0 : uleb += cu->start;
5479 : 0 : fprintf (out, "%*s[%2" PRIuMAX "] %s %" PRIu8 " [%6" PRIxMAX "]\n",
5480 : : indent, "", (uintmax_t) offset,
5481 : : op_name, usize, uleb);
5482 [ # # ]: 0 : CONSUME (data - start);
5483 : 0 : offset += 1 + (data - start);
5484 : 0 : break;
5485 : :
5486 : 0 : case DW_OP_xderef_type:
5487 : : /* 1-byte unsigned size of value, uleb128 base_type DIE offset. */
5488 : 0 : start = data;
5489 [ # # ]: 0 : NEED (1);
5490 : 0 : usize = *(uint8_t *) data++;
5491 : 0 : NEED (1);
5492 : 0 : get_uleb128 (uleb, data, data + len);
5493 : 0 : fprintf (out, "%*s[%4" PRIuMAX "] %s %" PRIu8 " [%6" PRIxMAX "]\n",
5494 : : indent, "", (uintmax_t) offset,
5495 : : op_name, usize, uleb);
5496 [ # # ]: 0 : CONSUME (data - start);
5497 : 0 : offset += 1 + (data - start);
5498 : 0 : break;
5499 : :
5500 : 786 : case DW_OP_convert:
5501 : : case DW_OP_GNU_convert:
5502 : : case DW_OP_reinterpret:
5503 : : case DW_OP_GNU_reinterpret:
5504 : : /* uleb128 CU relative offset to DW_TAG_base_type, or zero
5505 : : for conversion to untyped. */
5506 : 786 : start = data;
5507 [ - + ]: 786 : NEED (1);
5508 : 786 : get_uleb128 (uleb, data, data + len);
5509 [ + + + - : 786 : if (uleb != 0 && ! print_unresolved_addresses && cu != NULL)
+ - ]
5510 : 524 : uleb += cu->start;
5511 : 786 : fprintf (out, "%*s[%2" PRIuMAX "] %s [%6" PRIxMAX "]\n",
5512 : : indent, "", (uintmax_t) offset, op_name, uleb);
5513 [ - + ]: 786 : CONSUME (data - start);
5514 : 786 : offset += 1 + (data - start);
5515 : 786 : break;
5516 : :
5517 : 8 : case DW_OP_GNU_parameter_ref:
5518 : : /* 4 byte CU relative reference to the abstract optimized away
5519 : : DW_TAG_formal_parameter. */
5520 [ - + ]: 8 : NEED (4);
5521 [ - + ]: 8 : uintmax_t param_off = (uintmax_t) read_4ubyte_unaligned (dbg, data);
5522 [ + - + - ]: 8 : if (! print_unresolved_addresses && cu != NULL)
5523 : 8 : param_off += cu->start;
5524 : 8 : fprintf (out, "%*s[%2" PRIuMAX "] %s [%6" PRIxMAX "]\n",
5525 : : indent, "", (uintmax_t) offset, op_name, param_off);
5526 : 8 : CONSUME (4);
5527 : 8 : data += 4;
5528 : 8 : offset += 5;
5529 : 8 : break;
5530 : :
5531 : : default:
5532 : : /* No Operand. */
5533 : 767862 : fprintf (out, "%*s[%2" PRIuMAX "] %s\n",
5534 : : indent, "", (uintmax_t) offset, op_name);
5535 : 767862 : ++offset;
5536 : 767862 : break;
5537 : : }
5538 : :
5539 : 1116230 : indent = indentrest;
5540 : 1116230 : continue;
5541 : :
5542 : 0 : invalid:
5543 : 0 : fprintf (out, _("%*s[%2" PRIuMAX "] %s <TRUNCATED>\n"),
5544 : : indent, "", (uintmax_t) offset, op_name);
5545 : 0 : break;
5546 : : }
5547 : : }
5548 : :
5549 : :
5550 : : /* Turn the addresses into file offsets by using the phdrs. */
5551 : : static void
5552 : 2 : find_offsets(Elf *elf, GElf_Addr main_bias, size_t n,
5553 : : GElf_Addr addrs[n], GElf_Off offs[n])
5554 : : {
5555 : 2 : size_t unsolved = n;
5556 [ + + ]: 18 : for (size_t i = 0; i < phnum; ++i) {
5557 : 16 : GElf_Phdr phdr_mem;
5558 : 16 : GElf_Phdr *phdr = gelf_getphdr(elf, i, &phdr_mem);
5559 [ + - + + : 16 : if (phdr != NULL && phdr->p_type == PT_LOAD && phdr->p_memsz > 0)
+ - ]
5560 [ + + ]: 46 : for (size_t j = 0; j < n; ++j)
5561 [ + + + + ]: 44 : if (offs[j] == 0 && addrs[j] >= phdr->p_vaddr + main_bias &&
5562 [ + - ]: 22 : addrs[j] - (phdr->p_vaddr + main_bias) < phdr->p_filesz) {
5563 : 22 : offs[j] = addrs[j] - (phdr->p_vaddr + main_bias) + phdr->p_offset;
5564 [ + + ]: 22 : if (--unsolved == 0)
5565 : : break;
5566 : : }
5567 : : }
5568 : 2 : }
5569 : :
5570 : : /* The dynamic segment (type PT_DYNAMIC), contains the .dynamic section.
5571 : : And .dynamic section contains an array of the dynamic structures.
5572 : : We use the array to get:
5573 : : DT_STRTAB: the address of the string table
5574 : : DT_SYMTAB: the address of the symbol table
5575 : : DT_STRSZ: the size, in bytes, of the string table
5576 : : ... */
5577 : : static void
5578 : 2 : get_dynscn_addrs(Elf *elf, GElf_Phdr *phdr, GElf_Addr addrs[i_max])
5579 : : {
5580 : 2 : Elf_Data *data = elf_getdata_rawchunk(
5581 : 2 : elf, phdr->p_offset, phdr->p_filesz, ELF_T_DYN);
5582 : :
5583 : 2 : int dyn_idx = 0;
5584 : 44 : for (;; ++dyn_idx) {
5585 : 46 : GElf_Dyn dyn_mem;
5586 : 46 : GElf_Dyn *dyn = gelf_getdyn(data, dyn_idx, &dyn_mem);
5587 : : /* DT_NULL Marks end of dynamic section. */
5588 [ + - + + ]: 46 : if (dyn == NULL || dyn->d_tag == DT_NULL)
5589 : : break;
5590 : :
5591 [ + - + + : 44 : switch (dyn->d_tag) {
+ + + + +
+ - + ]
5592 : 2 : case DT_SYMTAB:
5593 : 2 : addrs[i_symtab] = dyn->d_un.d_ptr;
5594 : 2 : break;
5595 : :
5596 : 0 : case DT_HASH:
5597 : 0 : addrs[i_hash] = dyn->d_un.d_ptr;
5598 : 0 : break;
5599 : :
5600 : 2 : case DT_GNU_HASH:
5601 : 2 : addrs[i_gnu_hash] = dyn->d_un.d_ptr;
5602 : 2 : break;
5603 : :
5604 : 2 : case DT_STRTAB:
5605 : 2 : addrs[i_strtab] = dyn->d_un.d_ptr;
5606 : 2 : break;
5607 : :
5608 : 2 : case DT_VERSYM:
5609 : 2 : addrs[i_versym] = dyn->d_un.d_ptr;
5610 : 2 : break;
5611 : :
5612 : 2 : case DT_VERDEF:
5613 : 2 : addrs[i_verdef] = dyn->d_un.d_ptr;
5614 : 2 : break;
5615 : :
5616 : 2 : case DT_VERDEFNUM:
5617 : 2 : addrs[i_verdefnum] = dyn->d_un.d_val;
5618 : 2 : break;
5619 : :
5620 : 2 : case DT_VERNEED:
5621 : 2 : addrs[i_verneed] = dyn->d_un.d_ptr;
5622 : 2 : break;
5623 : :
5624 : 2 : case DT_VERNEEDNUM:
5625 : 2 : addrs[i_verneednum] = dyn->d_un.d_val;
5626 : 2 : break;
5627 : :
5628 : 2 : case DT_STRSZ:
5629 : 2 : addrs[i_strsz] = dyn->d_un.d_val;
5630 : 2 : break;
5631 : :
5632 : 0 : case DT_SYMTAB_SHNDX:
5633 : 0 : addrs[i_symtab_shndx] = dyn->d_un.d_ptr;
5634 : 0 : break;
5635 : : }
5636 : : }
5637 : 2 : }
5638 : :
5639 : :
5640 : : /* Use dynamic segment to get data for the string table section. */
5641 : : static Elf_Data *
5642 : 2 : get_dynscn_strtab(Elf *elf, GElf_Phdr *phdr)
5643 : : {
5644 : 2 : Elf_Data *strtab_data;
5645 : 2 : GElf_Addr addrs[i_max] = {0,};
5646 : 2 : GElf_Off offs[i_max] = {0,};
5647 : 2 : get_dynscn_addrs(elf, phdr, addrs);
5648 : 2 : find_offsets(elf, 0, i_max, addrs, offs);
5649 : 4 : strtab_data = elf_getdata_rawchunk(
5650 : 2 : elf, offs[i_strtab], addrs[i_strsz], ELF_T_BYTE);
5651 : 2 : return strtab_data;
5652 : : }
5653 : :
5654 : :
5655 : : struct listptr
5656 : : {
5657 : : Dwarf_Off offset:(64 - 3);
5658 : : bool addr64:1;
5659 : : bool dwarf64:1;
5660 : : bool warned:1;
5661 : : struct Dwarf_CU *cu;
5662 : : unsigned int attr;
5663 : : };
5664 : :
5665 : : #define listptr_offset_size(p) ((p)->dwarf64 ? 8 : 4)
5666 : : #define listptr_address_size(p) ((p)->addr64 ? 8 : 4)
5667 : :
5668 : : static Dwarf_Addr
5669 : 21758 : cudie_base (Dwarf_Die *cudie)
5670 : : {
5671 : 21758 : Dwarf_Addr base;
5672 : : /* Find the base address of the compilation unit. It will normally
5673 : : be specified by DW_AT_low_pc. In DWARF-3 draft 4, the base
5674 : : address could be overridden by DW_AT_entry_pc. It's been
5675 : : removed, but GCC emits DW_AT_entry_pc and not DW_AT_lowpc for
5676 : : compilation units with discontinuous ranges. */
5677 [ - + ]: 21758 : if (unlikely (dwarf_lowpc (cudie, &base) != 0))
5678 : : {
5679 : 0 : Dwarf_Attribute attr_mem;
5680 [ # # ]: 0 : if (dwarf_formaddr (dwarf_attr (cudie, DW_AT_entry_pc, &attr_mem),
5681 : : &base) != 0)
5682 : 0 : base = 0;
5683 : : }
5684 : 21758 : return base;
5685 : : }
5686 : :
5687 : : static Dwarf_Addr
5688 : 21758 : listptr_base (struct listptr *p)
5689 : : {
5690 : 21758 : Dwarf_Die cu = CUDIE (p->cu);
5691 : 21758 : return cudie_base (&cu);
5692 : : }
5693 : :
5694 : : /* To store the name used in compare_listptr */
5695 : : static _Thread_local const char *sort_listptr_name;
5696 : :
5697 : : static int
5698 : 1788800 : compare_listptr (const void *a, const void *b)
5699 : : {
5700 : 1788800 : const char *name = sort_listptr_name;
5701 : 1788800 : struct listptr *p1 = (void *) a;
5702 : 1788800 : struct listptr *p2 = (void *) b;
5703 : :
5704 [ + + ]: 1788800 : if (p1->offset < p2->offset)
5705 : : return -1;
5706 [ + + ]: 176082 : if (p1->offset > p2->offset)
5707 : : return 1;
5708 : :
5709 [ + - - + ]: 8210 : if (!p1->warned && !p2->warned)
5710 : : {
5711 [ - + ]: 8210 : if (p1->addr64 != p2->addr64)
5712 : : {
5713 : 0 : p1->warned = p2->warned = true;
5714 : 0 : error (0, 0,
5715 : 0 : _("%s %#" PRIx64 " used with different address sizes"),
5716 : : name, (uint64_t) p1->offset);
5717 : : }
5718 [ - + ]: 8210 : if (p1->dwarf64 != p2->dwarf64)
5719 : : {
5720 : 0 : p1->warned = p2->warned = true;
5721 : 0 : error (0, 0,
5722 : 0 : _("%s %#" PRIx64 " used with different offset sizes"),
5723 : 0 : name, (uint64_t) p1->offset);
5724 : : }
5725 [ - + ]: 8210 : if (listptr_base (p1) != listptr_base (p2))
5726 : : {
5727 : 0 : p1->warned = p2->warned = true;
5728 : 0 : error (0, 0,
5729 : 0 : _("%s %#" PRIx64 " used with different base addresses"),
5730 : 0 : name, (uint64_t) p1->offset);
5731 : : }
5732 [ + - ]: 8210 : if (p1->attr != p2 ->attr)
5733 : : {
5734 : 0 : p1->warned = p2->warned = true;
5735 : 0 : error (0, 0,
5736 : 0 : _("%s %#" PRIx64
5737 : : " used with different attribute %s and %s"),
5738 : 0 : name, (uint64_t) p1->offset, dwarf_attr_name (p1->attr),
5739 : : dwarf_attr_name (p2->attr));
5740 : : }
5741 : : }
5742 : :
5743 : : return 0;
5744 : : }
5745 : :
5746 : : struct listptr_table
5747 : : {
5748 : : size_t n;
5749 : : size_t alloc;
5750 : : struct listptr *table;
5751 : : };
5752 : :
5753 : : static struct listptr_table known_locsptr;
5754 : : static struct listptr_table known_loclistsptr;
5755 : : static struct listptr_table known_rangelistptr;
5756 : : static struct listptr_table known_rnglistptr;
5757 : : static struct listptr_table known_addrbases;
5758 : : static struct listptr_table known_stroffbases;
5759 : :
5760 : : static void
5761 : 502 : reset_listptr (struct listptr_table *table)
5762 : : {
5763 : 502 : free (table->table);
5764 : 502 : table->table = NULL;
5765 : 502 : table->n = table->alloc = 0;
5766 : : }
5767 : :
5768 : : /* Returns false if offset doesn't fit. See struct listptr. */
5769 : : static bool
5770 : 257548 : notice_listptr (enum section_e section, struct listptr_table *table,
5771 : : uint_fast8_t address_size, uint_fast8_t offset_size,
5772 : : struct Dwarf_CU *cu, Dwarf_Off offset, unsigned int attr)
5773 : : {
5774 [ + + ]: 257548 : if (print_debug_sections & section)
5775 : : {
5776 [ + + ]: 257296 : if (table->n == table->alloc)
5777 : : {
5778 [ + + ]: 410 : if (table->alloc == 0)
5779 : 174 : table->alloc = 128;
5780 : : else
5781 : 236 : table->alloc *= 2;
5782 : 410 : table->table = xrealloc (table->table,
5783 : 410 : table->alloc * sizeof table->table[0]);
5784 : : }
5785 : :
5786 : 257296 : struct listptr *p = &table->table[table->n++];
5787 : :
5788 : 257296 : *p = (struct listptr)
5789 : : {
5790 : 257296 : .addr64 = address_size == 8,
5791 : 257296 : .dwarf64 = offset_size == 8,
5792 : : .offset = offset,
5793 : : .cu = cu,
5794 : : .attr = attr
5795 : : };
5796 : :
5797 [ + - ]: 257296 : if (p->offset != offset)
5798 : : {
5799 : 0 : table->n--;
5800 : 0 : return false;
5801 : : }
5802 : : }
5803 : : return true;
5804 : : }
5805 : :
5806 : : static void
5807 : 182 : sort_listptr (struct listptr_table *table, const char *name)
5808 : : {
5809 [ + + ]: 182 : if (table->n > 0)
5810 : : {
5811 : 174 : sort_listptr_name = name;
5812 : 174 : qsort (table->table, table->n, sizeof table->table[0],
5813 : : &compare_listptr);
5814 : : }
5815 : 182 : }
5816 : :
5817 : : static bool
5818 : 208 : skip_listptr_hole (struct listptr_table *table, size_t *idxp,
5819 : : uint_fast8_t *address_sizep, uint_fast8_t *offset_sizep,
5820 : : Dwarf_Addr *base, struct Dwarf_CU **cu, ptrdiff_t offset,
5821 : : unsigned char **readp, unsigned char *endp,
5822 : : unsigned int *attr)
5823 : : {
5824 [ - + ]: 208 : if (table->n == 0)
5825 : : return false;
5826 : :
5827 [ + - + + ]: 336 : while (*idxp < table->n && table->table[*idxp].offset < (Dwarf_Off) offset)
5828 : 128 : ++*idxp;
5829 : :
5830 : 208 : struct listptr *p = &table->table[*idxp];
5831 : :
5832 [ + - ]: 208 : if (*idxp == table->n
5833 [ - + ]: 208 : || p->offset >= (Dwarf_Off) (endp - *readp + offset))
5834 : : {
5835 : 0 : *readp = endp;
5836 : 0 : printf (_(" [%6tx] <UNUSED GARBAGE IN REST OF SECTION>\n"),
5837 : : offset);
5838 : 0 : return true;
5839 : : }
5840 : :
5841 [ - + ]: 208 : if (p->offset != (Dwarf_Off) offset)
5842 : : {
5843 : 0 : *readp += p->offset - offset;
5844 : 0 : printf (_(" [%6tx] <UNUSED GARBAGE> ... %" PRIu64 " bytes ...\n"),
5845 : : offset, (Dwarf_Off) p->offset - offset);
5846 : 0 : return true;
5847 : : }
5848 : :
5849 [ + - ]: 208 : if (address_sizep != NULL)
5850 [ + + ]: 224 : *address_sizep = listptr_address_size (p);
5851 [ + + ]: 208 : if (offset_sizep != NULL)
5852 [ + - ]: 296 : *offset_sizep = listptr_offset_size (p);
5853 [ + - ]: 208 : if (base != NULL)
5854 : 208 : *base = listptr_base (p);
5855 [ + - ]: 208 : if (cu != NULL)
5856 : 208 : *cu = p->cu;
5857 [ + + ]: 208 : if (attr != NULL)
5858 : 148 : *attr = p->attr;
5859 : :
5860 : : return false;
5861 : : }
5862 : :
5863 : : static Dwarf_Off
5864 : 109748 : next_listptr_offset (struct listptr_table *table, size_t *idxp, Dwarf_Off off)
5865 : : {
5866 : : /* Note that multiple attributes could in theory point to the same loclist
5867 : : offset, so make sure we pick one that is bigger than the current one.
5868 : : The table is sorted on offset. */
5869 [ + - ]: 109748 : if (*idxp < table->n)
5870 : : {
5871 [ + - ]: 218320 : while (++*idxp < table->n)
5872 : : {
5873 : 218320 : Dwarf_Off next = table->table[*idxp].offset;
5874 [ + + ]: 218320 : if (next > off)
5875 : : return next;
5876 : : }
5877 : : }
5878 : : return 0;
5879 : : }
5880 : :
5881 : : /* Returns the listptr associated with the given index, or NULL. */
5882 : : static struct listptr *
5883 : 1528152 : get_listptr (struct listptr_table *table, size_t idx)
5884 : : {
5885 : 1528152 : if (idx >= table->n)
5886 : : return NULL;
5887 : 1527900 : return &table->table[idx];
5888 : : }
5889 : :
5890 : : /* Returns the next index, base address and CU associated with the
5891 : : list unit offsets. If there is none false is returned, otherwise
5892 : : true. Assumes the table has been sorted. */
5893 : : static bool
5894 : 5130 : listptr_cu (struct listptr_table *table, size_t *idxp,
5895 : : Dwarf_Off start, Dwarf_Off end,
5896 : : Dwarf_Addr *base, struct Dwarf_CU **cu)
5897 : : {
5898 : 5130 : while (*idxp < table->n
5899 [ + - + + ]: 41080 : && table->table[*idxp].offset < start)
5900 : 35950 : ++*idxp;
5901 : :
5902 [ + - ]: 5130 : if (*idxp < table->n
5903 [ + - ]: 5130 : && table->table[*idxp].offset >= start
5904 [ + - ]: 5130 : && table->table[*idxp].offset < end)
5905 : : {
5906 : 5130 : struct listptr *p = &table->table[*idxp];
5907 : 5130 : *base = listptr_base (p);
5908 : 5130 : *cu = p->cu;
5909 : 5130 : return true;
5910 : : }
5911 : :
5912 : : return false;
5913 : : }
5914 : :
5915 : : /* Returns the next index with the current CU for the given attribute.
5916 : : If there is none false is returned, otherwise true. Assumes the
5917 : : table has been sorted. */
5918 : : static bool
5919 : 763724 : listptr_attr (struct listptr_table *table, size_t idxp,
5920 : : Dwarf_Off offset, unsigned int attr)
5921 : : {
5922 : 1528116 : struct listptr *listptr;
5923 : 1528116 : do
5924 : : {
5925 [ + + ]: 1528116 : listptr = get_listptr (table, idxp);
5926 [ + - ]: 1527874 : if (listptr == NULL)
5927 : : return false;
5928 : :
5929 [ + + + + ]: 1527874 : if (listptr->offset == offset && listptr->attr == attr)
5930 : : return true;
5931 : :
5932 : 1418126 : idxp++;
5933 : : }
5934 [ + + ]: 1418126 : while (listptr->offset <= offset);
5935 : :
5936 : : return false;
5937 : : }
5938 : :
5939 : : static void
5940 : 88 : print_debug_abbrev_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
5941 : : Ebl *ebl, GElf_Ehdr *ehdr __attribute__ ((unused)),
5942 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg,
5943 : : FILE *out)
5944 : : {
5945 : 88 : Elf_Data *elf_data = get_debug_elf_data (dbg, ebl, IDX_debug_abbrev, scn);
5946 [ + - ]: 88 : if (elf_data == NULL)
5947 : : return;
5948 : :
5949 : 88 : const size_t sh_size = elf_data->d_size;
5950 : :
5951 : 88 : fprintf (out, _("\nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"
5952 : : " [ Code]\n"),
5953 : : elf_ndxscn (scn), section_name (ebl, shdr),
5954 : 88 : (uint64_t) shdr->sh_offset);
5955 : :
5956 : 88 : Dwarf_Off offset = 0;
5957 [ + + ]: 3682 : while (offset < sh_size)
5958 : : {
5959 : 3594 : fprintf (out, _("\nAbbreviation section at offset %" PRIu64 ":\n"),
5960 : : offset);
5961 : :
5962 : 332238 : while (1)
5963 : 164322 : {
5964 : 167916 : size_t length;
5965 : 167916 : Dwarf_Abbrev abbrev;
5966 : :
5967 : 167916 : int res = dwarf_offabbrev (dbg, offset, &length, &abbrev);
5968 [ + + ]: 167916 : if (res != 0)
5969 : : {
5970 [ - + ]: 3594 : if (unlikely (res < 0))
5971 : : {
5972 : 0 : fprintf (out, _("\
5973 : : *** error while reading abbreviation: %s\n"),
5974 : : dwarf_errmsg (-1));
5975 : 0 : return;
5976 : : }
5977 : :
5978 : : /* This is the NUL byte at the end of the section. */
5979 : 3594 : ++offset;
5980 : 3594 : break;
5981 : : }
5982 : :
5983 : : /* We know these calls can never fail. */
5984 : 164322 : unsigned int code = dwarf_getabbrevcode (&abbrev);
5985 : 164322 : unsigned int tag = dwarf_getabbrevtag (&abbrev);
5986 : 164322 : int has_children = dwarf_abbrevhaschildren (&abbrev);
5987 : :
5988 [ + + ]: 164322 : fprintf (out, " [%5u] %s: %" PRId64
5989 : : ", %s: %s, %s: %s\n",
5990 : : code, offset_str, (int64_t) offset,
5991 : : children_str, has_children ? yes_str : no_str,
5992 : : tag_str, dwarf_tag_name (tag));
5993 : :
5994 : 164322 : size_t cnt = 0;
5995 : 164322 : unsigned int name;
5996 : 164322 : unsigned int form;
5997 : 164322 : Dwarf_Sword data;
5998 : 164322 : Dwarf_Off enoffset;
5999 : 164322 : while (dwarf_getabbrevattr_data (&abbrev, cnt, &name, &form,
6000 [ + + ]: 942204 : &data, &enoffset) == 0)
6001 : : {
6002 : 777882 : fprintf (out, " attr: %s, form: %s",
6003 : : dwarf_attr_name (name), dwarf_form_name (form));
6004 [ + + ]: 777882 : if (form == DW_FORM_implicit_const)
6005 : 55052 : fprintf (out, " (%" PRId64 ")", data);
6006 : 777882 : fprintf (out, ", offset: %#" PRIx64 "\n", (uint64_t) enoffset);
6007 : 777882 : ++cnt;
6008 : : }
6009 : :
6010 : 164322 : offset += length;
6011 : : }
6012 : : }
6013 : : }
6014 : :
6015 : :
6016 : : static void
6017 : 8 : print_debug_addr_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
6018 : : Ebl *ebl, GElf_Ehdr *ehdr,
6019 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg,
6020 : : FILE *out)
6021 : : {
6022 : 8 : Elf_Data *data = get_debug_elf_data (dbg, ebl, IDX_debug_addr, scn);
6023 [ + - ]: 8 : if (data == NULL)
6024 : : return;
6025 : :
6026 : 8 : fprintf (out, _("\
6027 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
6028 : : elf_ndxscn (scn), section_name (ebl, shdr),
6029 : 8 : (uint64_t) shdr->sh_offset);
6030 : :
6031 [ + - ]: 8 : if (shdr->sh_size == 0)
6032 : : return;
6033 : :
6034 : 8 : size_t idx = 0;
6035 : 8 : sort_listptr (&known_addrbases, "addr_base");
6036 : :
6037 : 8 : const unsigned char *start = (const unsigned char *) data->d_buf;
6038 : 8 : const unsigned char *readp = start;
6039 : 8 : const unsigned char *readendp = ((const unsigned char *) data->d_buf
6040 : 8 : + data->d_size);
6041 : :
6042 [ + + ]: 24 : while (readp < readendp)
6043 : : {
6044 : : /* We cannot really know whether or not there is an header. The
6045 : : DebugFission extension to DWARF4 doesn't add one. The DWARF5
6046 : : .debug_addr variant does. Whether or not we have an header,
6047 : : DW_AT_[GNU_]addr_base points at "index 0". So if the current
6048 : : offset equals the CU addr_base then we can just start
6049 : : printing addresses. If there is no CU with an exact match
6050 : : then we'll try to parse the header first. */
6051 : 16 : Dwarf_Off off = (Dwarf_Off) (readp
6052 : 16 : - (const unsigned char *) data->d_buf);
6053 : :
6054 : 16 : fprintf (out, "Table at offset %" PRIx64 " ", off);
6055 : :
6056 [ + - ]: 16 : struct listptr *listptr = get_listptr (&known_addrbases, idx++);
6057 : 16 : const unsigned char *next_unitp;
6058 : :
6059 : 16 : uint64_t unit_length;
6060 : 16 : uint16_t version;
6061 : 16 : uint8_t address_size;
6062 : 16 : uint8_t segment_size;
6063 [ - + ]: 16 : if (listptr == NULL)
6064 : : {
6065 : 0 : error (0, 0, "Warning: No CU references .debug_addr after %" PRIx64,
6066 : : off);
6067 : :
6068 : : /* We will have to assume it is just addresses to the end... */
6069 [ # # ]: 0 : address_size = ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 4 : 8;
6070 : 0 : next_unitp = readendp;
6071 : 0 : fprintf (out, "Unknown CU:\n");
6072 : : }
6073 : : else
6074 : : {
6075 : 16 : Dwarf_Die cudie;
6076 [ - + ]: 16 : if (dwarf_cu_die (listptr->cu, &cudie,
6077 : : NULL, NULL, NULL, NULL,
6078 : : NULL, NULL) == NULL)
6079 : 0 : fprintf (out, "Unknown CU (%s):\n", dwarf_errmsg (-1));
6080 : : else
6081 : 16 : fprintf (out, "for CU [%6" PRIx64 "]:\n", dwarf_dieoffset (&cudie));
6082 : :
6083 [ + + ]: 16 : if (listptr->offset == off)
6084 : : {
6085 [ - + ]: 4 : address_size = listptr_address_size (listptr);
6086 : 4 : segment_size = 0;
6087 : 4 : version = 4;
6088 : :
6089 : : /* The addresses start here, but where do they end? */
6090 [ + + ]: 4 : listptr = get_listptr (&known_addrbases, idx);
6091 [ - + ]: 2 : if (listptr == NULL)
6092 : : next_unitp = readendp;
6093 [ + - ]: 2 : else if (listptr->cu->version < 5)
6094 : : {
6095 : 2 : next_unitp = start + listptr->offset;
6096 [ + - - + ]: 2 : if (listptr->offset < off || listptr->offset > data->d_size)
6097 : : {
6098 : 0 : error (0, 0,
6099 : : "Warning: Bad address base for next unit at %"
6100 : : PRIx64, off);
6101 : 0 : next_unitp = readendp;
6102 : : }
6103 : : }
6104 : : else
6105 : : {
6106 : : /* Tricky, we don't have a header for this unit, but
6107 : : there is one for the next. We will have to
6108 : : "guess" how big it is and subtract it from the
6109 : : offset (because that points after the header). */
6110 [ # # ]: 0 : unsigned int offset_size = listptr_offset_size (listptr);
6111 : 0 : Dwarf_Off next_off = (listptr->offset
6112 : 0 : - (offset_size == 4 ? 4 : 12) /* len */
6113 : : - 2 /* version */
6114 : : - 1 /* address size */
6115 : 0 : - 1); /* segment selector size */
6116 : 0 : next_unitp = start + next_off;
6117 [ # # # # ]: 0 : if (next_off < off || next_off > data->d_size)
6118 : : {
6119 : 0 : error (0, 0,
6120 : : "Warning: Couldn't calculate .debug_addr "
6121 : : " unit length at %" PRIx64, off);
6122 : 0 : next_unitp = readendp;
6123 : : }
6124 : : }
6125 : 4 : unit_length = (uint64_t) (next_unitp - readp);
6126 : :
6127 : : /* Pretend we have a header. */
6128 : 4 : fprintf (out, "\n");
6129 : 4 : fprintf (out, _(" Length: %8" PRIu64 "\n"),
6130 : : unit_length);
6131 : 4 : fprintf (out, _(" DWARF version: %8" PRIu16 "\n"), version);
6132 : 4 : fprintf (out, _(" Address size: %8" PRIu64 "\n"),
6133 : : (uint64_t) address_size);
6134 : 4 : fprintf (out, _(" Segment size: %8" PRIu64 "\n"),
6135 : : (uint64_t) segment_size);
6136 : 4 : fprintf (out, "\n");
6137 : : }
6138 : : else
6139 : : {
6140 : : /* OK, we have to parse an header first. */
6141 [ - + ]: 12 : unit_length = read_4ubyte_unaligned_inc (dbg, readp);
6142 [ - + ]: 12 : if (unlikely (unit_length == 0xffffffff))
6143 : : {
6144 [ # # ]: 0 : if (unlikely (readp > readendp - 8))
6145 : : {
6146 : 0 : invalid_data:
6147 : 0 : error (0, 0, "Invalid data");
6148 : 0 : return;
6149 : : }
6150 [ # # ]: 0 : unit_length = read_8ubyte_unaligned_inc (dbg, readp);
6151 : : }
6152 : 12 : fprintf (out, "\n");
6153 : 12 : fprintf (out, _(" Length: %8" PRIu64 "\n"),
6154 : : unit_length);
6155 : :
6156 : : /* We need at least 2-bytes (version) + 1-byte
6157 : : (addr_size) + 1-byte (segment_size) = 4 bytes to
6158 : : complete the header. And this unit cannot go beyond
6159 : : the section data. */
6160 [ + - ]: 12 : if (readp > readendp - 4
6161 [ + - ]: 12 : || unit_length < 4
6162 [ - + ]: 12 : || unit_length > (uint64_t) (readendp - readp))
6163 : 0 : goto invalid_data;
6164 : :
6165 : 12 : next_unitp = readp + unit_length;
6166 : :
6167 [ - + ]: 12 : version = read_2ubyte_unaligned_inc (dbg, readp);
6168 : 12 : fprintf (out, _(" DWARF version: %8" PRIu16 "\n"), version);
6169 : :
6170 [ - + ]: 12 : if (version != 5)
6171 : : {
6172 : 0 : error (0, 0, _("Unknown version"));
6173 : 0 : goto next_unit;
6174 : : }
6175 : :
6176 : 12 : address_size = *readp++;
6177 : 12 : fprintf (out, _(" Address size: %8" PRIu64 "\n"),
6178 : : (uint64_t) address_size);
6179 : :
6180 [ - + ]: 12 : if (address_size != 4 && address_size != 8)
6181 : : {
6182 : 0 : error (0, 0, _("unsupported address size"));
6183 : 0 : goto next_unit;
6184 : : }
6185 : :
6186 : 12 : segment_size = *readp++;
6187 : 12 : fprintf (out, _(" Segment size: %8" PRIu64 "\n"),
6188 : : (uint64_t) segment_size);
6189 : 12 : fprintf (out, "\n");
6190 : :
6191 [ - + ]: 12 : if (segment_size != 0)
6192 : : {
6193 : 0 : error (0, 0, _("unsupported segment size"));
6194 : 0 : goto next_unit;
6195 : : }
6196 : :
6197 [ - + ]: 12 : if (listptr->offset != (Dwarf_Off) (readp - start))
6198 : : {
6199 : 0 : error (0, 0, "Address index doesn't start after header");
6200 : 0 : goto next_unit;
6201 : : }
6202 : : }
6203 : : }
6204 : :
6205 : 16 : int digits = 1;
6206 : 16 : size_t addresses = (next_unitp - readp) / address_size;
6207 [ + + ]: 24 : while (addresses >= 10)
6208 : : {
6209 : 8 : ++digits;
6210 : 8 : addresses /= 10;
6211 : : }
6212 : :
6213 : 16 : unsigned int uidx = 0;
6214 : 16 : size_t index_offset = readp - (const unsigned char *) data->d_buf;
6215 : 16 : fprintf (out, " Addresses start at offset 0x%zx:\n", index_offset);
6216 [ + + ]: 188 : while (readp <= next_unitp - address_size)
6217 : : {
6218 [ - + + + : 172 : Dwarf_Addr addr = read_addr_unaligned_inc (address_size, dbg,
- + - + ]
6219 : : readp);
6220 : 172 : fprintf (out, " [%*u] ", digits, uidx++);
6221 : 172 : print_dwarf_addr (dwflmod, address_size, addr, addr, out);
6222 : 172 : fprintf (out, "\n");
6223 : : }
6224 : 16 : fprintf (out, "\n");
6225 : :
6226 [ + - ]: 16 : if (readp != next_unitp)
6227 : 0 : error (0, 0, "extra %zd bytes at end of unit",
6228 : 0 : (size_t) (next_unitp - readp));
6229 : :
6230 : 16 : next_unit:
6231 : : readp = next_unitp;
6232 : : }
6233 : : }
6234 : :
6235 : : /* Print content of DWARF .debug_aranges section. We fortunately do
6236 : : not have to know a bit about the structure of the section, libdwarf
6237 : : takes care of it. */
6238 : : static void
6239 : 2 : print_decoded_aranges_section (Ebl *ebl, GElf_Ehdr *ehdr, Elf_Scn *scn,
6240 : : GElf_Shdr *shdr, Dwarf *dbg, FILE *out)
6241 : : {
6242 : 2 : Dwarf_Aranges *aranges;
6243 : 2 : size_t cnt;
6244 [ - + ]: 2 : if (unlikely (dwarf_getaranges (dbg, &aranges, &cnt) != 0))
6245 : : {
6246 : 0 : error (0, 0, _("cannot get .debug_aranges content: %s"),
6247 : : dwarf_errmsg (-1));
6248 : 0 : return;
6249 : : }
6250 : :
6251 : 2 : GElf_Shdr glink_mem;
6252 : 2 : GElf_Shdr *glink;
6253 : 2 : glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link), &glink_mem);
6254 [ - + ]: 2 : if (glink == NULL)
6255 : : {
6256 : 0 : error (0, 0, _("invalid sh_link value in section %zu"),
6257 : : elf_ndxscn (scn));
6258 : 0 : return;
6259 : : }
6260 : :
6261 : 2 : fprintf (out, ngettext ("\
6262 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 " contains %zu entry:\n",
6263 : : "\
6264 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 " contains %zu entries:\n",
6265 : : cnt),
6266 : : elf_ndxscn (scn), section_name (ebl, shdr),
6267 : 2 : (uint64_t) shdr->sh_offset, cnt);
6268 : :
6269 : : /* Compute floor(log16(cnt)). */
6270 : 2 : size_t tmp = cnt;
6271 : 2 : int digits = 1;
6272 [ - + ]: 2 : while (tmp >= 16)
6273 : : {
6274 : 0 : ++digits;
6275 : 0 : tmp >>= 4;
6276 : : }
6277 : :
6278 [ + + ]: 12 : for (size_t n = 0; n < cnt; ++n)
6279 : : {
6280 : 10 : Dwarf_Arange *runp = dwarf_onearange (aranges, n);
6281 [ - + ]: 10 : if (unlikely (runp == NULL))
6282 : : {
6283 : 0 : fprintf (out, "cannot get arange %zu: %s\n", n, dwarf_errmsg (-1));
6284 : 0 : return;
6285 : : }
6286 : :
6287 : 10 : Dwarf_Addr start;
6288 : 10 : Dwarf_Word length;
6289 : 10 : Dwarf_Off offset;
6290 : :
6291 [ - + ]: 10 : if (unlikely (dwarf_getarangeinfo (runp, &start, &length, &offset) != 0))
6292 : 0 : fprintf (out, _(" [%*zu] ???\n"), digits, n);
6293 : : else
6294 : 10 : fprintf (out, _(" [%*zu] start: %0#*" PRIx64
6295 : : ", length: %5" PRIu64 ", CU DIE offset: %6"
6296 : : PRId64 "\n"),
6297 [ - + ]: 10 : digits, n, ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 10 : 18,
6298 : : (uint64_t) start, (uint64_t) length, (int64_t) offset);
6299 : : }
6300 : : }
6301 : :
6302 : :
6303 : : /* Print content of DWARF .debug_aranges section. */
6304 : : static void
6305 : 92 : print_debug_aranges_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
6306 : : Ebl *ebl, GElf_Ehdr *ehdr, Elf_Scn *scn,
6307 : : GElf_Shdr *shdr, Dwarf *dbg, FILE *out)
6308 : : {
6309 [ + + ]: 92 : if (decodedaranges)
6310 : : {
6311 : 2 : print_decoded_aranges_section (ebl, ehdr, scn, shdr, dbg, out);
6312 : 2 : return;
6313 : : }
6314 : :
6315 : 90 : Elf_Data *data = get_debug_elf_data (dbg, ebl, IDX_debug_aranges, scn);
6316 [ + - ]: 90 : if (data == NULL)
6317 : : return;
6318 : :
6319 : 90 : fprintf (out, _("\
6320 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
6321 : : elf_ndxscn (scn), section_name (ebl, shdr),
6322 : 90 : (uint64_t) shdr->sh_offset);
6323 : :
6324 : 90 : const unsigned char *readp = data->d_buf;
6325 : 90 : const unsigned char *readendp = readp + data->d_size;
6326 : :
6327 [ + + ]: 3666 : while (readp < readendp)
6328 : : {
6329 : 3576 : const unsigned char *hdrstart = readp;
6330 : 3576 : size_t start_offset = hdrstart - (const unsigned char *) data->d_buf;
6331 : :
6332 : 3576 : fprintf (out, "\n%s %zu:\n", table_offset_lower_str, start_offset);
6333 [ - + ]: 3576 : if (readp + 4 > readendp)
6334 : : {
6335 : 0 : invalid_data:
6336 : 0 : error (0, 0, _("invalid data in section [%zu] '%s'"),
6337 : : elf_ndxscn (scn), section_name (ebl, shdr));
6338 : 0 : return;
6339 : : }
6340 : :
6341 [ + + ]: 3576 : Dwarf_Word length = read_4ubyte_unaligned_inc (dbg, readp);
6342 : 3576 : unsigned int length_bytes = 4;
6343 [ - + ]: 3576 : if (length == DWARF3_LENGTH_64_BIT)
6344 : : {
6345 [ # # ]: 0 : if (readp + 8 > readendp)
6346 : 0 : goto invalid_data;
6347 [ # # ]: 0 : length = read_8ubyte_unaligned_inc (dbg, readp);
6348 : 0 : length_bytes = 8;
6349 : : }
6350 : :
6351 : 3576 : const unsigned char *nexthdr = readp + length;
6352 : 3576 : fprintf (out, "\n %s: %6" PRIu64 "\n",
6353 : : length_str, (uint64_t) length);
6354 : :
6355 [ - + ]: 3576 : if (unlikely (length > (size_t) (readendp - readp)))
6356 : 0 : goto invalid_data;
6357 : :
6358 [ - + ]: 3576 : if (length == 0)
6359 : 0 : continue;
6360 : :
6361 [ - + ]: 3576 : if (readp + 2 > readendp)
6362 : 0 : goto invalid_data;
6363 [ + + ]: 3576 : uint_fast16_t version = read_2ubyte_unaligned_inc (dbg, readp);
6364 : 3576 : fprintf (out, " %s: %6" PRIuFAST16 "\n",
6365 : : dwarf_ver_str, version);
6366 [ - + ]: 3576 : if (version != 2)
6367 : : {
6368 : 0 : error (0, 0, _("unsupported aranges version"));
6369 : 0 : goto next_table;
6370 : : }
6371 : :
6372 : 3576 : Dwarf_Word offset;
6373 [ - + ]: 3576 : if (readp + length_bytes > readendp)
6374 : 0 : goto invalid_data;
6375 [ - + ]: 3576 : if (length_bytes == 8)
6376 [ # # ]: 0 : offset = read_8ubyte_unaligned_inc (dbg, readp);
6377 : : else
6378 [ + + ]: 3576 : offset = read_4ubyte_unaligned_inc (dbg, readp);
6379 : 3576 : fprintf (out, " %s: %6" PRIx64 "\n",
6380 : : cu_offset_str, (uint64_t) offset);
6381 : :
6382 [ - + ]: 3576 : if (readp + 1 > readendp)
6383 : 0 : goto invalid_data;
6384 : 3576 : unsigned int address_size = *readp++;
6385 : 3576 : fprintf (out, " %s: %6" PRIu64 "\n",
6386 : : addr_size_str, (uint64_t) address_size);
6387 [ - + ]: 3576 : if (address_size != 4 && address_size != 8)
6388 : : {
6389 : 0 : error (0, 0, _("unsupported address size"));
6390 : 0 : goto next_table;
6391 : : }
6392 : :
6393 [ - + ]: 3576 : if (readp + 1 > readendp)
6394 : 0 : goto invalid_data;
6395 : 3576 : unsigned int segment_size = *readp++;
6396 : 3576 : fprintf (out, " %s: %6" PRIu64 "\n\n",
6397 : : seg_size_str, (uint64_t) segment_size);
6398 [ - + - - ]: 3576 : if (segment_size != 0 && segment_size != 4 && segment_size != 8)
6399 : : {
6400 : 0 : error (0, 0, _("unsupported segment size"));
6401 : 0 : goto next_table;
6402 : : }
6403 : :
6404 : : /* Round the address to the next multiple of 2*address_size. */
6405 : 3576 : readp += ((2 * address_size - ((readp - hdrstart) % (2 * address_size)))
6406 : 3576 : % (2 * address_size));
6407 : :
6408 [ + - ]: 9170 : while (readp < nexthdr)
6409 : : {
6410 : 9170 : Dwarf_Word range_address;
6411 : 9170 : Dwarf_Word range_length;
6412 : 9170 : Dwarf_Word segment = 0;
6413 [ - + ]: 9170 : if (readp + 2 * address_size + segment_size > readendp)
6414 : 0 : goto invalid_data;
6415 [ + + ]: 9170 : if (address_size == 4)
6416 : : {
6417 [ + + ]: 92 : range_address = read_4ubyte_unaligned_inc (dbg, readp);
6418 [ + + ]: 92 : range_length = read_4ubyte_unaligned_inc (dbg, readp);
6419 : : }
6420 : : else
6421 : : {
6422 [ + + ]: 9078 : range_address = read_8ubyte_unaligned_inc (dbg, readp);
6423 [ + + ]: 9078 : range_length = read_8ubyte_unaligned_inc (dbg, readp);
6424 : : }
6425 : :
6426 [ - + ]: 9170 : if (segment_size == 4)
6427 [ # # ]: 0 : segment = read_4ubyte_unaligned_inc (dbg, readp);
6428 [ - + ]: 9170 : else if (segment_size == 8)
6429 [ # # ]: 0 : segment = read_8ubyte_unaligned_inc (dbg, readp);
6430 : :
6431 [ + + ]: 9170 : if (range_address == 0 && range_length == 0 && segment == 0)
6432 : : break;
6433 : :
6434 : 5594 : fprintf (out, " ");
6435 : 5594 : print_dwarf_addr (dwflmod, address_size, range_address,
6436 : : range_address, out);
6437 : 5594 : fprintf (out, "..");
6438 : 5594 : print_dwarf_addr (dwflmod, address_size,
6439 : 5594 : range_address + range_length - 1,
6440 : : range_length, out);
6441 [ - + ]: 5594 : if (segment_size != 0)
6442 : 0 : fprintf (out, " (%" PRIx64 ")\n", (uint64_t) segment);
6443 : : else
6444 : 5594 : fprintf (out, "\n");
6445 : : }
6446 : :
6447 : 3576 : next_table:
6448 [ - + ]: 3576 : if (readp != nexthdr)
6449 : : {
6450 : 0 : size_t padding = nexthdr - readp;
6451 : 0 : fprintf (out, _(" %zu padding bytes\n"), padding);
6452 : 0 : readp = nexthdr;
6453 : : }
6454 : : }
6455 : : }
6456 : :
6457 : :
6458 : : static bool is_split_dwarf (Dwarf *dbg, uint64_t *id, Dwarf_CU **split_cu);
6459 : :
6460 : : /* Returns true and sets cu and cu_base if the given Dwarf is a split
6461 : : DWARF (.dwo) file. */
6462 : : static bool
6463 : 0 : split_dwarf_cu_base (Dwarf *dbg, Dwarf_CU **cu, Dwarf_Addr *cu_base)
6464 : : {
6465 : 0 : uint64_t id;
6466 [ # # ]: 0 : if (is_split_dwarf (dbg, &id, cu))
6467 : : {
6468 : 0 : Dwarf_Die cudie;
6469 [ # # ]: 0 : if (dwarf_cu_info (*cu, NULL, NULL, &cudie, NULL, NULL, NULL, NULL) == 0)
6470 : : {
6471 : 0 : *cu_base = cudie_base (&cudie);
6472 : 0 : return true;
6473 : : }
6474 : : }
6475 : : return false;
6476 : : }
6477 : :
6478 : : /* Print content of DWARF .debug_rnglists section. */
6479 : : static void
6480 : 38 : print_debug_rnglists_section (Dwfl_Module *dwflmod,
6481 : : Ebl *ebl,
6482 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
6483 : : Elf_Scn *scn, GElf_Shdr *shdr,
6484 : : Dwarf *dbg __attribute__((unused)),
6485 : : FILE *out)
6486 : : {
6487 : 38 : Elf_Data *data = get_debug_elf_data (dbg, ebl, IDX_debug_rnglists, scn);
6488 [ + - ]: 38 : if (data == NULL)
6489 : 0 : return;
6490 : :
6491 : 38 : fprintf (out, _("\
6492 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
6493 : : elf_ndxscn (scn), section_name (ebl, shdr),
6494 : 38 : (uint64_t) shdr->sh_offset);
6495 : :
6496 : : /* For the listptr to get the base address/CU. */
6497 : 38 : sort_listptr (&known_rnglistptr, "rnglistptr");
6498 : 38 : size_t listptr_idx = 0;
6499 : :
6500 : 38 : const unsigned char *readp = data->d_buf;
6501 : 38 : const unsigned char *const dataend = ((unsigned char *) data->d_buf
6502 : 38 : + data->d_size);
6503 [ + + ]: 2076 : while (readp < dataend)
6504 : : {
6505 [ - + ]: 2038 : if (unlikely (readp > dataend - 4))
6506 : : {
6507 : 0 : invalid_data:
6508 : 0 : error (0, 0, _("invalid data in section [%zu] '%s'"),
6509 : : elf_ndxscn (scn), section_name (ebl, shdr));
6510 : 0 : return;
6511 : : }
6512 : :
6513 : 2038 : ptrdiff_t offset = readp - (unsigned char *) data->d_buf;
6514 : 2038 : fprintf (out, "%s 0x%" PRIx64 ":\n\n",
6515 : : table_offset_upper_str, (uint64_t) offset);
6516 : :
6517 [ - + ]: 2038 : uint64_t unit_length = read_4ubyte_unaligned_inc (dbg, readp);
6518 : 2038 : unsigned int offset_size = 4;
6519 [ - + ]: 2038 : if (unlikely (unit_length == 0xffffffff))
6520 : : {
6521 [ # # ]: 0 : if (unlikely (readp > dataend - 8))
6522 : 0 : goto invalid_data;
6523 : :
6524 [ # # ]: 0 : unit_length = read_8ubyte_unaligned_inc (dbg, readp);
6525 : 0 : offset_size = 8;
6526 : : }
6527 : 2038 : fprintf (out, " %s: %8" PRIu64 "\n", length_str, unit_length);
6528 : :
6529 : : /* We need at least 2-bytes + 1-byte + 1-byte + 4-bytes = 8
6530 : : bytes to complete the header. And this unit cannot go beyond
6531 : : the section data. */
6532 [ + - ]: 2038 : if (readp > dataend - 8
6533 [ + - ]: 2038 : || unit_length < 8
6534 [ - + ]: 2038 : || unit_length > (uint64_t) (dataend - readp))
6535 : 0 : goto invalid_data;
6536 : :
6537 : 2038 : const unsigned char *nexthdr = readp + unit_length;
6538 : :
6539 [ - + ]: 2038 : uint16_t version = read_2ubyte_unaligned_inc (dbg, readp);
6540 : 2038 : fprintf (out, " %s: %8" PRIu16 "\n", dwarf_ver_str, version);
6541 : :
6542 [ - + ]: 2038 : if (version != 5)
6543 : : {
6544 : 0 : error (0, 0, _("Unknown version"));
6545 : 0 : goto next_table;
6546 : : }
6547 : :
6548 : 2038 : uint8_t address_size = *readp++;
6549 : 2038 : fprintf (out, " %s: %8" PRIu64 "\n",
6550 : : addr_size_str, (uint64_t) address_size);
6551 : :
6552 [ - + ]: 2038 : if (address_size != 4 && address_size != 8)
6553 : : {
6554 : 0 : error (0, 0, _("unsupported address size"));
6555 : 0 : goto next_table;
6556 : : }
6557 : :
6558 : 2038 : uint8_t segment_size = *readp++;
6559 : 2038 : fprintf (out, _(" Segment size: %8" PRIu64 "\n"),
6560 : : (uint64_t) segment_size);
6561 : :
6562 [ - + - - ]: 2038 : if (segment_size != 0 && segment_size != 4 && segment_size != 8)
6563 : : {
6564 : 0 : error (0, 0, _("unsupported segment size"));
6565 : 0 : goto next_table;
6566 : : }
6567 : :
6568 [ - + ]: 2038 : uint32_t offset_entry_count = read_4ubyte_unaligned_inc (dbg, readp);
6569 : 2038 : fprintf (out, " %s: %8" PRIu64 "\n",
6570 : : offset_ent_str, (uint64_t) offset_entry_count);
6571 : :
6572 : : /* We need the CU that uses this unit to get the initial base address. */
6573 : 2038 : Dwarf_Addr cu_base = 0;
6574 : 2038 : struct Dwarf_CU *cu = NULL;
6575 [ - + ]: 2038 : if (listptr_cu (&known_rnglistptr, &listptr_idx,
6576 : : (Dwarf_Off) offset,
6577 : 2038 : (Dwarf_Off) (nexthdr - (unsigned char *) data->d_buf),
6578 : : &cu_base, &cu)
6579 [ # # ]: 0 : || split_dwarf_cu_base (dbg, &cu, &cu_base))
6580 : 2038 : {
6581 : 2038 : Dwarf_Die cudie;
6582 [ - + ]: 2038 : if (dwarf_cu_die (cu, &cudie,
6583 : : NULL, NULL, NULL, NULL,
6584 : : NULL, NULL) == NULL)
6585 : 0 : fprintf (out, " %s: ", unknown_base_str);
6586 : : else
6587 : 2038 : fprintf (out, " %s [%6" PRIx64 "] %s: ",
6588 : : cu_str, dwarf_dieoffset (&cudie), base_str);
6589 : 2038 : print_dwarf_addr (dwflmod, address_size, cu_base, cu_base, out);
6590 : 2038 : fprintf (out, "\n");
6591 : : }
6592 : : else
6593 : 0 : fprintf (out, _(" Not associated with a CU.\n"));
6594 : :
6595 : 2038 : fprintf (out, "\n");
6596 : :
6597 : 2038 : const unsigned char *offset_array_start = readp;
6598 [ + + ]: 2038 : if (offset_entry_count > 0)
6599 : : {
6600 : 2 : uint64_t max_entries = (unit_length - 8) / offset_size;
6601 [ - + ]: 2 : if (offset_entry_count > max_entries)
6602 : : {
6603 : 0 : error (0, 0,
6604 : 0 : _("too many offset entries for unit length"));
6605 : 0 : offset_entry_count = max_entries;
6606 : : }
6607 : :
6608 : 2 : fprintf (out, _(" Offsets starting at 0x%" PRIx64 ":\n"),
6609 : : (uint64_t) (offset_array_start
6610 : 2 : - (unsigned char *) data->d_buf));
6611 [ + + ]: 6 : for (uint32_t idx = 0; idx < offset_entry_count; idx++)
6612 : : {
6613 : 4 : fprintf (out, " [%6" PRIu32 "] ", idx);
6614 [ + - ]: 4 : if (offset_size == 4)
6615 : : {
6616 [ - + ]: 4 : uint32_t off = read_4ubyte_unaligned_inc (dbg, readp);
6617 : 4 : fprintf (out, "0x%" PRIx32 "\n", off);
6618 : : }
6619 : : else
6620 : : {
6621 [ # # ]: 0 : uint64_t off = read_8ubyte_unaligned_inc (dbg, readp);
6622 : 0 : fprintf (out, "0x%" PRIx64 "\n", off);
6623 : : }
6624 : : }
6625 : 2 : fprintf (out, "\n");
6626 : : }
6627 : :
6628 : 2038 : Dwarf_Addr base = cu_base;
6629 : 2038 : bool start_of_list = true;
6630 [ + + ]: 150386 : while (readp < nexthdr)
6631 : : {
6632 : 148348 : uint8_t kind = *readp++;
6633 : 148348 : uint64_t op1, op2;
6634 : :
6635 : : /* Skip padding. */
6636 [ - + ]: 148348 : if (start_of_list && kind == DW_RLE_end_of_list)
6637 : 0 : continue;
6638 : :
6639 [ + + ]: 148348 : if (start_of_list)
6640 : : {
6641 : 30110 : base = cu_base;
6642 : 30110 : fprintf (out, " Offset: %" PRIx64 ", Index: %" PRIx64 "\n",
6643 : 30110 : (uint64_t) (readp - (unsigned char *) data->d_buf - 1),
6644 : 30110 : (uint64_t) (readp - offset_array_start - 1));
6645 : 30110 : start_of_list = false;
6646 : : }
6647 : :
6648 : 148348 : fprintf (out, " %s", dwarf_range_list_encoding_name (kind));
6649 [ + - - - : 148348 : switch (kind)
+ + - +
- ]
6650 : : {
6651 : 30110 : case DW_RLE_end_of_list:
6652 : 30110 : start_of_list = true;
6653 : 30110 : fprintf (out, "\n\n");
6654 : 30110 : break;
6655 : :
6656 : 0 : case DW_RLE_base_addressx:
6657 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
6658 : : {
6659 : 0 : invalid_range:
6660 : 0 : error (0, 0, _("invalid range list data"));
6661 : 0 : goto next_table;
6662 : : }
6663 : 0 : get_uleb128 (op1, readp, nexthdr);
6664 : 0 : fprintf (out, " %" PRIx64 "\n", op1);
6665 [ # # ]: 0 : if (! print_unresolved_addresses)
6666 : : {
6667 : 0 : Dwarf_Addr addr;
6668 [ # # ]: 0 : if (get_indexed_addr (cu, op1, &addr) != 0)
6669 : 0 : fprintf (out, " ???\n");
6670 : : else
6671 : : {
6672 : 0 : fprintf (out, " ");
6673 : 0 : print_dwarf_addr (dwflmod, address_size, addr,
6674 : : addr, out);
6675 : 0 : fprintf (out, "\n");
6676 : : }
6677 : : }
6678 : : break;
6679 : :
6680 : 0 : case DW_RLE_startx_endx:
6681 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
6682 : 0 : goto invalid_range;
6683 : 0 : get_uleb128 (op1, readp, nexthdr);
6684 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
6685 : 0 : goto invalid_range;
6686 : 0 : get_uleb128 (op2, readp, nexthdr);
6687 : 0 : fprintf (out, " %" PRIx64 ", %" PRIx64 "\n", op1, op2);
6688 [ # # ]: 0 : if (! print_unresolved_addresses)
6689 : : {
6690 : 0 : Dwarf_Addr addr1;
6691 : 0 : Dwarf_Addr addr2;
6692 [ # # ]: 0 : if (get_indexed_addr (cu, op1, &addr1) != 0
6693 [ # # ]: 0 : || get_indexed_addr (cu, op2, &addr2) != 0)
6694 : : {
6695 : 0 : fprintf (out, " ???..\n");
6696 : 0 : fprintf (out, " ???\n");
6697 : : }
6698 : : else
6699 : : {
6700 : 0 : fprintf (out, " ");
6701 : 0 : print_dwarf_addr (dwflmod, address_size,
6702 : : addr1, addr1, out);
6703 : 0 : fprintf (out, "..\n ");
6704 : 0 : print_dwarf_addr (dwflmod, address_size,
6705 : : addr2 - 1, addr2, out);
6706 : 0 : fprintf (out, "\n");
6707 : : }
6708 : : }
6709 : : break;
6710 : :
6711 : 0 : case DW_RLE_startx_length:
6712 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
6713 : 0 : goto invalid_range;
6714 : 0 : get_uleb128 (op1, readp, nexthdr);
6715 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
6716 : 0 : goto invalid_range;
6717 : 0 : get_uleb128 (op2, readp, nexthdr);
6718 : 0 : fprintf (out, " %" PRIx64 ", %" PRIx64 "\n", op1, op2);
6719 [ # # ]: 0 : if (! print_unresolved_addresses)
6720 : : {
6721 : 0 : Dwarf_Addr addr1;
6722 : 0 : Dwarf_Addr addr2;
6723 [ # # ]: 0 : if (get_indexed_addr (cu, op1, &addr1) != 0)
6724 : : {
6725 : 0 : fprintf (out, " ???..\n");
6726 : 0 : fprintf (out, " ???\n");
6727 : : }
6728 : : else
6729 : : {
6730 : 0 : addr2 = addr1 + op2;
6731 : 0 : fprintf (out, " ");
6732 : 0 : print_dwarf_addr (dwflmod, address_size, addr1, addr1, out);
6733 : 0 : fprintf (out, "..\n ");
6734 : 0 : print_dwarf_addr (dwflmod, address_size,
6735 : : addr2 - 1, addr2, out);
6736 : 0 : fprintf (out, "\n");
6737 : : }
6738 : : }
6739 : : break;
6740 : :
6741 : 93852 : case DW_RLE_offset_pair:
6742 [ - + ]: 93852 : if ((uint64_t) (nexthdr - readp) < 1)
6743 : 0 : goto invalid_range;
6744 : 93852 : get_uleb128 (op1, readp, nexthdr);
6745 [ - + ]: 93852 : if ((uint64_t) (nexthdr - readp) < 1)
6746 : 0 : goto invalid_range;
6747 : 93852 : get_uleb128 (op2, readp, nexthdr);
6748 : 93852 : fprintf (out, " %" PRIx64 ", %" PRIx64 "\n", op1, op2);
6749 [ + - ]: 93852 : if (! print_unresolved_addresses)
6750 : : {
6751 : 93852 : op1 += base;
6752 : 93852 : op2 += base;
6753 : 93852 : fprintf (out, " ");
6754 : 93852 : print_dwarf_addr (dwflmod, address_size, op1, op1, out);
6755 : 93852 : fprintf (out, "..\n ");
6756 : 93852 : print_dwarf_addr (dwflmod, address_size, op2 - 1, op2, out);
6757 : 93852 : fprintf (out, "\n");
6758 : : }
6759 : : break;
6760 : :
6761 : 17018 : case DW_RLE_base_address:
6762 [ - + ]: 17018 : if (address_size == 4)
6763 : : {
6764 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 4)
6765 : 0 : goto invalid_range;
6766 [ # # ]: 0 : op1 = read_4ubyte_unaligned_inc (dbg, readp);
6767 : : }
6768 : : else
6769 : : {
6770 [ - + ]: 17018 : if ((uint64_t) (nexthdr - readp) < 8)
6771 : 0 : goto invalid_range;
6772 [ - + ]: 17018 : op1 = read_8ubyte_unaligned_inc (dbg, readp);
6773 : : }
6774 : 17018 : base = op1;
6775 : 17018 : fprintf (out, " 0x%" PRIx64 "\n", base);
6776 [ + - ]: 17018 : if (! print_unresolved_addresses)
6777 : : {
6778 : 17018 : fprintf (out, " ");
6779 : 17018 : print_dwarf_addr (dwflmod, address_size, base, base, out);
6780 : 17018 : fprintf (out, "\n");
6781 : : }
6782 : : break;
6783 : :
6784 : 0 : case DW_RLE_start_end:
6785 [ # # ]: 0 : if (address_size == 4)
6786 : : {
6787 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 8)
6788 : 0 : goto invalid_range;
6789 [ # # ]: 0 : op1 = read_4ubyte_unaligned_inc (dbg, readp);
6790 [ # # ]: 0 : op2 = read_4ubyte_unaligned_inc (dbg, readp);
6791 : : }
6792 : : else
6793 : : {
6794 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 16)
6795 : 0 : goto invalid_range;
6796 [ # # ]: 0 : op1 = read_8ubyte_unaligned_inc (dbg, readp);
6797 [ # # ]: 0 : op2 = read_8ubyte_unaligned_inc (dbg, readp);
6798 : : }
6799 : 0 : fprintf (out, " 0x%" PRIx64 "..0x%" PRIx64 "\n", op1, op2);
6800 [ # # ]: 0 : if (! print_unresolved_addresses)
6801 : : {
6802 : 0 : fprintf (out, " ");
6803 : 0 : print_dwarf_addr (dwflmod, address_size, op1, op1, out);
6804 : 0 : fprintf (out, "..\n ");
6805 : 0 : print_dwarf_addr (dwflmod, address_size, op2 - 1, op2, out);
6806 : 0 : fprintf (out, "\n");
6807 : : }
6808 : : break;
6809 : :
6810 : 7368 : case DW_RLE_start_length:
6811 [ - + ]: 7368 : if (address_size == 4)
6812 : : {
6813 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 4)
6814 : 0 : goto invalid_range;
6815 [ # # ]: 0 : op1 = read_4ubyte_unaligned_inc (dbg, readp);
6816 : : }
6817 : : else
6818 : : {
6819 [ - + ]: 7368 : if ((uint64_t) (nexthdr - readp) < 8)
6820 : 0 : goto invalid_range;
6821 [ - + ]: 7368 : op1 = read_8ubyte_unaligned_inc (dbg, readp);
6822 : : }
6823 [ - + ]: 7368 : if ((uint64_t) (nexthdr - readp) < 1)
6824 : 0 : goto invalid_range;
6825 : 7368 : get_uleb128 (op2, readp, nexthdr);
6826 : 7368 : fprintf (out, " 0x%" PRIx64 ", %" PRIx64 "\n", op1, op2);
6827 [ + + ]: 7368 : if (! print_unresolved_addresses)
6828 : : {
6829 : 7366 : op2 = op1 + op2;
6830 : 7366 : fprintf (out, " ");
6831 : 7366 : print_dwarf_addr (dwflmod, address_size, op1, op1, out);
6832 : 7366 : fprintf (out, "..\n ");
6833 : 7366 : print_dwarf_addr (dwflmod, address_size, op2 - 1, op2, out);
6834 : 7366 : fprintf (out, "\n");
6835 : : }
6836 : : break;
6837 : :
6838 : 0 : default:
6839 : 0 : goto invalid_range;
6840 : : }
6841 : : }
6842 : :
6843 : 2038 : next_table:
6844 [ - + ]: 2038 : if (readp != nexthdr)
6845 : : {
6846 : 0 : size_t padding = nexthdr - readp;
6847 : 0 : fprintf (out, _(" %zu padding bytes\n\n"), padding);
6848 : 0 : readp = nexthdr;
6849 : : }
6850 : : }
6851 : : }
6852 : :
6853 : : /* Print content of DWARF .debug_ranges section. */
6854 : : static void
6855 : 32 : print_debug_ranges_section (Dwfl_Module *dwflmod,
6856 : : Ebl *ebl, GElf_Ehdr *ehdr,
6857 : : Elf_Scn *scn, GElf_Shdr *shdr,
6858 : : Dwarf *dbg, FILE *out)
6859 : : {
6860 : 32 : Elf_Data *data = get_debug_elf_data (dbg, ebl, IDX_debug_ranges, scn);
6861 [ - + ]: 32 : if (data == NULL)
6862 : 0 : return;
6863 : :
6864 : 32 : fprintf (out, _("\
6865 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
6866 : : elf_ndxscn (scn), section_name (ebl, shdr),
6867 : 32 : (uint64_t) shdr->sh_offset);
6868 : :
6869 : 32 : sort_listptr (&known_rangelistptr, "rangelistptr");
6870 : 32 : size_t listptr_idx = 0;
6871 : :
6872 [ + + ]: 32 : uint_fast8_t address_size = ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 4 : 8;
6873 : :
6874 : 32 : bool first = true;
6875 : 32 : Dwarf_Addr base = 0;
6876 : 32 : unsigned char *const endp = (unsigned char *) data->d_buf + data->d_size;
6877 : 32 : unsigned char *readp = data->d_buf;
6878 : 32 : Dwarf_CU *last_cu = NULL;
6879 [ + + ]: 184 : while (readp < endp)
6880 : : {
6881 : 152 : ptrdiff_t offset = readp - (unsigned char *) data->d_buf;
6882 : 152 : Dwarf_CU *cu = last_cu;
6883 : :
6884 [ + + - + ]: 152 : if (first && skip_listptr_hole (&known_rangelistptr, &listptr_idx,
6885 : : &address_size, NULL, &base, &cu,
6886 : : offset, &readp, endp, NULL))
6887 : 0 : continue;
6888 : :
6889 [ + + ]: 152 : if (last_cu != cu)
6890 : : {
6891 : 58 : Dwarf_Die cudie;
6892 [ - + ]: 58 : if (dwarf_cu_die (cu, &cudie,
6893 : : NULL, NULL, NULL, NULL,
6894 : : NULL, NULL) == NULL)
6895 : 0 : fprintf (out, _("\n Unknown CU base: "));
6896 : : else
6897 : 58 : fprintf (out, _("\n CU [%6" PRIx64 "] base: "),
6898 : : dwarf_dieoffset (&cudie));
6899 : 58 : print_dwarf_addr (dwflmod, address_size, base, base, out);
6900 : 58 : fprintf (out, "\n");
6901 : : }
6902 : 152 : last_cu = cu;
6903 : :
6904 [ - + ]: 152 : if (unlikely (data->d_size - offset < (size_t) address_size * 2))
6905 : : {
6906 : 0 : fprintf (out, _(" [%6tx] <INVALID DATA>\n"), offset);
6907 : 0 : break;
6908 : : }
6909 : :
6910 : 152 : Dwarf_Addr begin;
6911 : 152 : Dwarf_Addr end;
6912 [ + + ]: 152 : if (address_size == 8)
6913 : : {
6914 [ - + ]: 120 : begin = read_8ubyte_unaligned_inc (dbg, readp);
6915 [ - + ]: 120 : end = read_8ubyte_unaligned_inc (dbg, readp);
6916 : : }
6917 : : else
6918 : : {
6919 [ - + ]: 32 : begin = read_4ubyte_unaligned_inc (dbg, readp);
6920 [ - + ]: 32 : end = read_4ubyte_unaligned_inc (dbg, readp);
6921 [ + - ]: 32 : if (begin == (Dwarf_Addr) (uint32_t) -1)
6922 : : begin = (Dwarf_Addr) -1l;
6923 : : }
6924 : :
6925 [ - + ]: 152 : if (begin == (Dwarf_Addr) -1l) /* Base address entry. */
6926 : : {
6927 [ # # ]: 0 : if (first)
6928 : 0 : fprintf (out, " [%6tx] ", offset);
6929 : : else
6930 : 0 : fprintf (out, " ");
6931 : 0 : fputs (_("base address\n"), out);
6932 : 0 : fprintf (out, " ");
6933 : 0 : print_dwarf_addr (dwflmod, address_size, end, end, out);
6934 : 0 : fprintf (out, "\n");
6935 : 0 : base = end;
6936 : 0 : first = false;
6937 : : }
6938 [ + + ]: 152 : else if (begin == 0 && end == 0) /* End of list entry. */
6939 : : {
6940 [ - + ]: 60 : if (first)
6941 : 0 : fprintf (out, _(" [%6tx] empty list\n"), offset);
6942 : : first = true;
6943 : : }
6944 : : else
6945 : : {
6946 : : /* We have an address range entry. */
6947 [ + + ]: 92 : if (first) /* First address range entry in a list. */
6948 : 60 : fprintf (out, " [%6tx] ", offset);
6949 : : else
6950 : 32 : fprintf (out, " ");
6951 : :
6952 : 92 : fprintf (out, "range %" PRIx64 ", %" PRIx64 "\n", begin, end);
6953 [ + + ]: 92 : if (! print_unresolved_addresses)
6954 : : {
6955 : 72 : fprintf (out, " ");
6956 : 72 : print_dwarf_addr (dwflmod, address_size, base + begin,
6957 : : base + begin, out);
6958 : 72 : fprintf (out, "..\n ");
6959 : 72 : print_dwarf_addr (dwflmod, address_size,
6960 : : base + end - 1, base + end, out);
6961 : 72 : fprintf (out, "\n");
6962 : : }
6963 : :
6964 : : first = false;
6965 : : }
6966 : : }
6967 : : }
6968 : :
6969 : : #define REGNAMESZ 16
6970 : : static const char *
6971 : 39598 : register_info (Ebl *ebl, unsigned int regno, const Ebl_Register_Location *loc,
6972 : : char name[REGNAMESZ], int *bits, int *type)
6973 : : {
6974 : 39598 : const char *set;
6975 : 39598 : const char *pfx;
6976 : 39598 : int ignore;
6977 [ + + + + ]: 115402 : ssize_t n = ebl_register_info (ebl, regno, name, REGNAMESZ, &pfx, &set,
6978 : : bits ?: &ignore, type ?: &ignore);
6979 [ - + ]: 39598 : if (n <= 0)
6980 : : {
6981 [ # # ]: 0 : if (loc != NULL)
6982 : 0 : snprintf (name, REGNAMESZ, "reg%u", loc->regno);
6983 : : else
6984 : 0 : snprintf (name, REGNAMESZ, "??? 0x%x", regno);
6985 [ # # ]: 0 : if (bits != NULL)
6986 [ # # ]: 0 : *bits = loc != NULL ? loc->bits : 0;
6987 [ # # ]: 0 : if (type != NULL)
6988 : 0 : *type = DW_ATE_unsigned;
6989 : 0 : set = "??? unrecognized";
6990 : : }
6991 : : else
6992 : : {
6993 [ + + - + ]: 39598 : if (bits != NULL && *bits <= 0)
6994 [ # # ]: 0 : *bits = loc != NULL ? loc->bits : 0;
6995 [ + + - + ]: 39598 : if (type != NULL && *type == DW_ATE_void)
6996 : 0 : *type = DW_ATE_unsigned;
6997 : :
6998 : : }
6999 : 39598 : return set;
7000 : : }
7001 : :
7002 : : static const unsigned char *
7003 : 280 : read_encoded (unsigned int encoding, const unsigned char *readp,
7004 : : const unsigned char *const endp, uint64_t *res, Dwarf *dbg)
7005 : : {
7006 [ - + ]: 280 : if ((encoding & 0xf) == DW_EH_PE_absptr)
7007 : 0 : encoding = gelf_getclass (dbg->elf) == ELFCLASS32
7008 [ # # ]: 0 : ? DW_EH_PE_udata4 : DW_EH_PE_udata8;
7009 : :
7010 [ - - - + : 280 : switch (encoding & 0xf)
- - + -
- ]
7011 : : {
7012 : 0 : case DW_EH_PE_uleb128:
7013 [ # # ]: 0 : if (readp >= endp)
7014 : 0 : goto invalid;
7015 : 0 : get_uleb128 (*res, readp, endp);
7016 : 0 : break;
7017 : 0 : case DW_EH_PE_sleb128:
7018 [ # # ]: 0 : if (readp >= endp)
7019 : 0 : goto invalid;
7020 : 0 : get_sleb128 (*res, readp, endp);
7021 : 0 : break;
7022 : 0 : case DW_EH_PE_udata2:
7023 [ # # ]: 0 : if (readp + 2 > endp)
7024 : 0 : goto invalid;
7025 [ # # ]: 0 : *res = read_2ubyte_unaligned_inc (dbg, readp);
7026 : 0 : break;
7027 : 140 : case DW_EH_PE_udata4:
7028 [ - + ]: 140 : if (readp + 4 > endp)
7029 : 0 : goto invalid;
7030 [ - + ]: 140 : *res = read_4ubyte_unaligned_inc (dbg, readp);
7031 : 140 : break;
7032 : 0 : case DW_EH_PE_udata8:
7033 [ # # ]: 0 : if (readp + 8 > endp)
7034 : 0 : goto invalid;
7035 [ # # ]: 0 : *res = read_8ubyte_unaligned_inc (dbg, readp);
7036 : 0 : break;
7037 : 0 : case DW_EH_PE_sdata2:
7038 [ # # ]: 0 : if (readp + 2 > endp)
7039 : 0 : goto invalid;
7040 [ # # ]: 0 : *res = read_2sbyte_unaligned_inc (dbg, readp);
7041 : 0 : break;
7042 : 140 : case DW_EH_PE_sdata4:
7043 [ - + ]: 140 : if (readp + 4 > endp)
7044 : 0 : goto invalid;
7045 [ - + ]: 140 : *res = read_4sbyte_unaligned_inc (dbg, readp);
7046 : 140 : break;
7047 : 0 : case DW_EH_PE_sdata8:
7048 [ # # ]: 0 : if (readp + 8 > endp)
7049 : 0 : goto invalid;
7050 [ # # ]: 0 : *res = read_8sbyte_unaligned_inc (dbg, readp);
7051 : 0 : break;
7052 : : default:
7053 : 0 : invalid:
7054 : 0 : error (1, 0,
7055 : 0 : _("invalid encoding"));
7056 : : }
7057 : :
7058 : 280 : return readp;
7059 : : }
7060 : :
7061 : : static const char *
7062 : 37902 : regname (Ebl *ebl, unsigned int regno, char *regnamebuf)
7063 : : {
7064 : 37902 : register_info (ebl, regno, NULL, regnamebuf, NULL, NULL);
7065 : :
7066 : 37902 : return regnamebuf;
7067 : : }
7068 : :
7069 : : static void
7070 : 24520 : print_cfa_program (const unsigned char *readp, const unsigned char *const endp,
7071 : : Dwarf_Word vma_base, unsigned int code_align,
7072 : : int data_align,
7073 : : unsigned int version, unsigned int ptr_size,
7074 : : unsigned int encoding,
7075 : : Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr, Dwarf *dbg,
7076 : : FILE *out)
7077 : : {
7078 : 24520 : char regnamebuf[REGNAMESZ];
7079 : :
7080 : 24520 : fputs ("\n Program:\n", out);
7081 : 24520 : Dwarf_Word pc = vma_base;
7082 [ + + ]: 357224 : while (readp < endp)
7083 : : {
7084 : 332704 : unsigned int opcode = *readp++;
7085 : :
7086 [ + + ]: 332704 : if (opcode < DW_CFA_advance_loc)
7087 : : /* Extended opcode. */
7088 [ + - + + : 198352 : switch (opcode)
+ + - + -
- + + + +
+ + - + -
- - - - -
+ - - ]
7089 : : {
7090 : 68098 : uint64_t op1;
7091 : 68098 : int64_t sop1;
7092 : 68098 : uint64_t op2;
7093 : 68098 : int64_t sop2;
7094 : :
7095 : 68098 : case DW_CFA_nop:
7096 : 68098 : fputs (" nop\n", out);
7097 : 266450 : break;
7098 : 0 : case DW_CFA_set_loc:
7099 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
7100 : 0 : goto invalid;
7101 : 0 : readp = read_encoded (encoding, readp, endp, &op1, dbg);
7102 : 0 : fprintf (out, " set_loc %#" PRIx64 " to %#" PRIx64 "\n",
7103 : 0 : op1, pc = vma_base + op1);
7104 : 0 : break;
7105 : 6830 : case DW_CFA_advance_loc1:
7106 [ - + ]: 6830 : if ((uint64_t) (endp - readp) < 1)
7107 : 0 : goto invalid;
7108 : 13660 : fprintf (out, " advance_loc1 %u to %#" PRIx64 "\n",
7109 : 6830 : *readp, pc += *readp * code_align);
7110 : 6830 : ++readp;
7111 : 6830 : break;
7112 : 2884 : case DW_CFA_advance_loc2:
7113 [ - + ]: 2884 : if ((uint64_t) (endp - readp) < 2)
7114 : 0 : goto invalid;
7115 [ - + ]: 2884 : op1 = read_2ubyte_unaligned_inc (dbg, readp);
7116 : 2884 : fprintf (out, " advance_loc2 %" PRIu64 " to %#" PRIx64 "\n",
7117 : 2884 : op1, pc += op1 * code_align);
7118 : 2884 : break;
7119 : 36 : case DW_CFA_advance_loc4:
7120 [ - + ]: 36 : if ((uint64_t) (endp - readp) < 4)
7121 : 0 : goto invalid;
7122 [ + - ]: 36 : op1 = read_4ubyte_unaligned_inc (dbg, readp);
7123 : 36 : fprintf (out, " advance_loc4 %" PRIu64 " to %#" PRIx64 "\n",
7124 : 36 : op1, pc += op1 * code_align);
7125 : 36 : break;
7126 : 8 : case DW_CFA_offset_extended:
7127 [ - + ]: 8 : if ((uint64_t) (endp - readp) < 1)
7128 : 0 : goto invalid;
7129 : 8 : get_uleb128 (op1, readp, endp);
7130 [ - + ]: 8 : if ((uint64_t) (endp - readp) < 1)
7131 : 0 : goto invalid;
7132 : 8 : get_uleb128 (op2, readp, endp);
7133 : 8 : fprintf (out, " offset_extended r%" PRIu64 " (%s) at cfa%+" PRId64
7134 : : "\n",
7135 : : op1, regname (ebl, op1, regnamebuf), op2 * data_align);
7136 : 8 : break;
7137 : 0 : case DW_CFA_restore_extended:
7138 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
7139 : 0 : goto invalid;
7140 : 0 : get_uleb128 (op1, readp, endp);
7141 : 0 : fprintf (out, " restore_extended r%" PRIu64 " (%s)\n",
7142 : : op1, regname (ebl, op1, regnamebuf));
7143 : 0 : break;
7144 : 110 : case DW_CFA_undefined:
7145 [ - + ]: 110 : if ((uint64_t) (endp - readp) < 1)
7146 : 0 : goto invalid;
7147 : 110 : get_uleb128 (op1, readp, endp);
7148 : 110 : fprintf (out, " undefined r%" PRIu64 " (%s)\n", op1,
7149 : : regname (ebl, op1, regnamebuf));
7150 : 110 : break;
7151 : 0 : case DW_CFA_same_value:
7152 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
7153 : 0 : goto invalid;
7154 : 0 : get_uleb128 (op1, readp, endp);
7155 : 0 : fprintf (out, " same_value r%" PRIu64 " (%s)\n", op1,
7156 : : regname (ebl, op1, regnamebuf));
7157 : 0 : break;
7158 : 0 : case DW_CFA_register:
7159 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
7160 : 0 : goto invalid;
7161 : 0 : get_uleb128 (op1, readp, endp);
7162 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
7163 : 0 : goto invalid;
7164 : 0 : get_uleb128 (op2, readp, endp);
7165 : 0 : fprintf (out,
7166 : : " register r%" PRIu64 " (%s) in r%" PRIu64 " (%s)\n",
7167 : : op1, regname (ebl, op1, regnamebuf), op2,
7168 : : regname (ebl, op2, regnamebuf));
7169 : 0 : break;
7170 : 11934 : case DW_CFA_remember_state:
7171 : 11934 : fputs (" remember_state\n", out);
7172 : 11934 : break;
7173 : 11934 : case DW_CFA_restore_state:
7174 : 11934 : fputs (" restore_state\n", out);
7175 : 11934 : break;
7176 : 418 : case DW_CFA_def_cfa:
7177 [ - + ]: 418 : if ((uint64_t) (endp - readp) < 1)
7178 : 0 : goto invalid;
7179 : 418 : get_uleb128 (op1, readp, endp);
7180 [ - + ]: 418 : if ((uint64_t) (endp - readp) < 1)
7181 : 0 : goto invalid;
7182 : 418 : get_uleb128 (op2, readp, endp);
7183 : 418 : fprintf (out,
7184 : : " def_cfa r%" PRIu64 " (%s) at offset %" PRIu64 "\n",
7185 : : op1, regname (ebl, op1, regnamebuf), op2);
7186 : 418 : break;
7187 : 126 : case DW_CFA_def_cfa_register:
7188 [ - + ]: 126 : if ((uint64_t) (endp - readp) < 1)
7189 : 0 : goto invalid;
7190 : 126 : get_uleb128 (op1, readp, endp);
7191 : 126 : fprintf (out, " def_cfa_register r%" PRIu64 " (%s)\n",
7192 : : op1, regname (ebl, op1, regnamebuf));
7193 : 126 : break;
7194 : 95824 : case DW_CFA_def_cfa_offset:
7195 [ - + ]: 95824 : if ((uint64_t) (endp - readp) < 1)
7196 : 0 : goto invalid;
7197 : 95824 : get_uleb128 (op1, readp, endp);
7198 : 95824 : fprintf (out, " def_cfa_offset %" PRIu64 "\n", op1);
7199 : 95824 : break;
7200 : 118 : case DW_CFA_def_cfa_expression:
7201 [ - + ]: 118 : if ((uint64_t) (endp - readp) < 1)
7202 : 0 : goto invalid;
7203 : 118 : get_uleb128 (op1, readp, endp); /* Length of DW_FORM_block. */
7204 : 118 : fprintf (out, " def_cfa_expression %" PRIu64 "\n", op1);
7205 [ - + ]: 118 : if ((uint64_t) (endp - readp) < op1)
7206 : : {
7207 : 0 : invalid:
7208 : 0 : fputs (_(" <INVALID DATA>\n"), out);
7209 : 0 : return;
7210 : : }
7211 : 118 : print_ops (dwflmod, dbg, 10, 10, version, ptr_size, 0, NULL,
7212 : : op1, readp, out);
7213 : 118 : readp += op1;
7214 : 118 : break;
7215 : 0 : case DW_CFA_expression:
7216 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
7217 : 0 : goto invalid;
7218 : 0 : get_uleb128 (op1, readp, endp);
7219 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
7220 : 0 : goto invalid;
7221 : 0 : get_uleb128 (op2, readp, endp); /* Length of DW_FORM_block. */
7222 : 0 : fprintf (out, " expression r%" PRIu64 " (%s) \n",
7223 : : op1, regname (ebl, op1, regnamebuf));
7224 [ # # ]: 0 : if ((uint64_t) (endp - readp) < op2)
7225 : 0 : goto invalid;
7226 : 0 : print_ops (dwflmod, dbg, 10, 10, version, ptr_size, 0, NULL,
7227 : : op2, readp, out);
7228 : 0 : readp += op2;
7229 : 0 : break;
7230 : 24 : case DW_CFA_offset_extended_sf:
7231 [ - + ]: 24 : if ((uint64_t) (endp - readp) < 1)
7232 : 0 : goto invalid;
7233 : 24 : get_uleb128 (op1, readp, endp);
7234 [ - + ]: 24 : if ((uint64_t) (endp - readp) < 1)
7235 : 0 : goto invalid;
7236 : 24 : get_sleb128 (sop2, readp, endp);
7237 : 24 : fprintf (out, " offset_extended_sf r%" PRIu64 " (%s) at cfa%+"
7238 : : PRId64 "\n",
7239 : : op1, regname (ebl, op1, regnamebuf), sop2 * data_align);
7240 : 24 : break;
7241 : 0 : case DW_CFA_def_cfa_sf:
7242 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
7243 : 0 : goto invalid;
7244 : 0 : get_uleb128 (op1, readp, endp);
7245 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
7246 : 0 : goto invalid;
7247 : 0 : get_sleb128 (sop2, readp, endp);
7248 : 0 : fprintf (out,
7249 : : " def_cfa_sf r%" PRIu64 " (%s) at offset %" PRId64 "\n",
7250 : : op1, regname (ebl, op1, regnamebuf), sop2 * data_align);
7251 : 0 : break;
7252 : 0 : case DW_CFA_def_cfa_offset_sf:
7253 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
7254 : 0 : goto invalid;
7255 : 0 : get_sleb128 (sop1, readp, endp);
7256 : 0 : fprintf (out,
7257 : : " def_cfa_offset_sf %" PRId64 "\n", sop1 * data_align);
7258 : 0 : break;
7259 : 0 : case DW_CFA_val_offset:
7260 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
7261 : 0 : goto invalid;
7262 : 0 : get_uleb128 (op1, readp, endp);
7263 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
7264 : 0 : goto invalid;
7265 : 0 : get_uleb128 (op2, readp, endp);
7266 : 0 : fprintf (out, " val_offset %" PRIu64 " at offset %" PRIu64 "\n",
7267 : : op1, op2 * data_align);
7268 : 0 : break;
7269 : 0 : case DW_CFA_val_offset_sf:
7270 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
7271 : 0 : goto invalid;
7272 : 0 : get_uleb128 (op1, readp, endp);
7273 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
7274 : 0 : goto invalid;
7275 : 0 : get_sleb128 (sop2, readp, endp);
7276 : 0 : fprintf (out,
7277 : : " val_offset_sf %" PRIu64 " at offset %" PRId64 "\n",
7278 : : op1, sop2 * data_align);
7279 : 0 : break;
7280 : 0 : case DW_CFA_val_expression:
7281 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
7282 : 0 : goto invalid;
7283 : 0 : get_uleb128 (op1, readp, endp);
7284 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
7285 : 0 : goto invalid;
7286 : 0 : get_uleb128 (op2, readp, endp); /* Length of DW_FORM_block. */
7287 : 0 : fprintf (out, " val_expression r%" PRIu64 " (%s)\n",
7288 : : op1, regname (ebl, op1, regnamebuf));
7289 [ # # ]: 0 : if ((uint64_t) (endp - readp) < op2)
7290 : 0 : goto invalid;
7291 : 0 : print_ops (dwflmod, dbg, 10, 10, version, ptr_size, 0,
7292 : : NULL, op2, readp, out);
7293 : 0 : readp += op2;
7294 : 0 : break;
7295 : 0 : case DW_CFA_MIPS_advance_loc8:
7296 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 8)
7297 : 0 : goto invalid;
7298 [ # # ]: 0 : op1 = read_8ubyte_unaligned_inc (dbg, readp);
7299 : 0 : fprintf (out,
7300 : : " MIPS_advance_loc8 %" PRIu64 " to %#" PRIx64 "\n",
7301 : 0 : op1, pc += op1 * code_align);
7302 : 0 : break;
7303 : 8 : case DW_CFA_GNU_window_save: /* DW_CFA_AARCH64_negate_ra_state */
7304 [ + - ]: 8 : if (ehdr->e_machine == EM_AARCH64)
7305 : 8 : fputs (" AARCH64_negate_ra_state\n", out);
7306 : : else
7307 : 0 : fputs (" GNU_window_save\n", out);
7308 : : break;
7309 : 0 : case DW_CFA_GNU_args_size:
7310 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
7311 : 0 : goto invalid;
7312 : 0 : get_uleb128 (op1, readp, endp);
7313 : 0 : fprintf (out, " args_size %" PRIu64 "\n", op1);
7314 : 0 : break;
7315 : : default:
7316 : 0 : fprintf (out, " ??? (%u)\n", opcode);
7317 : 0 : break;
7318 : : }
7319 [ + + ]: 134352 : else if (opcode < DW_CFA_offset)
7320 : 97136 : fprintf (out, " advance_loc %u to %#" PRIx64 "\n",
7321 : 97136 : opcode & 0x3f, pc += (opcode & 0x3f) * code_align);
7322 [ + + ]: 37216 : else if (opcode < DW_CFA_restore)
7323 : : {
7324 : 33898 : uint64_t offset;
7325 [ - + ]: 33898 : if ((uint64_t) (endp - readp) < 1)
7326 : 0 : goto invalid;
7327 : 33898 : get_uleb128 (offset, readp, endp);
7328 : 33898 : fprintf (out, " offset r%u (%s) at cfa%+" PRId64 "\n",
7329 : : opcode & 0x3f, regname (ebl, opcode & 0x3f, regnamebuf),
7330 : : offset * data_align);
7331 : : }
7332 : : else
7333 : 3318 : fprintf (out, " restore r%u (%s)\n",
7334 : : opcode & 0x3f, regname (ebl, opcode & 0x3f, regnamebuf));
7335 : : }
7336 : : }
7337 : :
7338 : :
7339 : : static unsigned int
7340 : 24210 : encoded_ptr_size (int encoding, unsigned int ptr_size)
7341 : : {
7342 [ - + - + ]: 24210 : switch (encoding & 7)
7343 : : {
7344 : : case DW_EH_PE_udata4:
7345 : : return 4;
7346 : 0 : case DW_EH_PE_udata8:
7347 : 0 : return 8;
7348 : : case 0:
7349 : : return ptr_size;
7350 : : }
7351 : :
7352 : 0 : fprintf (stderr, "Unsupported pointer encoding: %#x, "
7353 : : "assuming pointer size of %d.\n", encoding, ptr_size);
7354 : 0 : return ptr_size;
7355 : : }
7356 : :
7357 : :
7358 : : static unsigned int
7359 : 642 : print_encoding (unsigned int val, FILE *out)
7360 : : {
7361 [ - - - + : 642 : switch (val & 0xf)
- - - + -
- ]
7362 : : {
7363 : 0 : case DW_EH_PE_absptr:
7364 : 0 : fputs ("absptr", out);
7365 : 0 : break;
7366 : 0 : case DW_EH_PE_uleb128:
7367 : 0 : fputs ("uleb128", out);
7368 : 0 : break;
7369 : 0 : case DW_EH_PE_udata2:
7370 : 0 : fputs ("udata2", out);
7371 : 0 : break;
7372 : 140 : case DW_EH_PE_udata4:
7373 : 140 : fputs ("udata4", out);
7374 : 140 : break;
7375 : 0 : case DW_EH_PE_udata8:
7376 : 0 : fputs ("udata8", out);
7377 : 0 : break;
7378 : 0 : case DW_EH_PE_sleb128:
7379 : 0 : fputs ("sleb128", out);
7380 : 0 : break;
7381 : 0 : case DW_EH_PE_sdata2:
7382 : 0 : fputs ("sdata2", out);
7383 : 0 : break;
7384 : 502 : case DW_EH_PE_sdata4:
7385 : 502 : fputs ("sdata4", out);
7386 : 502 : break;
7387 : 0 : case DW_EH_PE_sdata8:
7388 : 0 : fputs ("sdata8", out);
7389 : 0 : break;
7390 : : default:
7391 : : /* We did not use any of the bits after all. */
7392 : : return val;
7393 : : }
7394 : :
7395 : 642 : return val & ~0xf;
7396 : : }
7397 : :
7398 : :
7399 : : static unsigned int
7400 : 502 : print_relinfo (unsigned int val, FILE *out)
7401 : : {
7402 [ + - + - : 502 : switch (val & 0x70)
- - ]
7403 : : {
7404 : 362 : case DW_EH_PE_pcrel:
7405 : 362 : fputs ("pcrel", out);
7406 : 362 : break;
7407 : 0 : case DW_EH_PE_textrel:
7408 : 0 : fputs ("textrel", out);
7409 : 0 : break;
7410 : 140 : case DW_EH_PE_datarel:
7411 : 140 : fputs ("datarel", out);
7412 : 140 : break;
7413 : 0 : case DW_EH_PE_funcrel:
7414 : 0 : fputs ("funcrel", out);
7415 : 0 : break;
7416 : 0 : case DW_EH_PE_aligned:
7417 : 0 : fputs ("aligned", out);
7418 : 0 : break;
7419 : : default:
7420 : : return val;
7421 : : }
7422 : :
7423 : 502 : return val & ~0x70;
7424 : : }
7425 : :
7426 : :
7427 : : static void
7428 : 642 : print_encoding_base (const char *pfx, unsigned int fde_encoding,
7429 : : FILE *out)
7430 : : {
7431 : 642 : fprintf (out, "(%s", pfx);
7432 : :
7433 [ - + ]: 642 : if (fde_encoding == DW_EH_PE_omit)
7434 : 0 : fputs ("omit)\n", out);
7435 : : else
7436 : : {
7437 : 642 : unsigned int w = fde_encoding;
7438 : :
7439 : 642 : w = print_encoding (w, out);
7440 : :
7441 [ + + ]: 642 : if (w & 0x70)
7442 : : {
7443 [ + - ]: 502 : if (w != fde_encoding)
7444 : 502 : fputc (' ', out);
7445 : :
7446 : 502 : w = print_relinfo (w, out);
7447 : : }
7448 : :
7449 [ - + ]: 642 : if (w != 0)
7450 [ # # ]: 0 : fprintf (out, "%s%x", w != fde_encoding ? " " : "", w);
7451 : :
7452 : 642 : fputs (")\n", out);
7453 : : }
7454 : 642 : }
7455 : :
7456 : :
7457 : : static void
7458 : 196 : print_debug_frame_section (Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr,
7459 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg,
7460 : : FILE *out)
7461 : : {
7462 : 196 : size_t shstrndx;
7463 : : /* We know this call will succeed since it did in the caller. */
7464 : 196 : (void) elf_getshdrstrndx (ebl->elf, &shstrndx);
7465 : 196 : const char *scnname = section_name (ebl, shdr);
7466 : :
7467 : : /* Needed if we find PC-relative addresses. */
7468 : 196 : GElf_Addr bias;
7469 [ - + ]: 196 : if (dwfl_module_getelf (dwflmod, &bias) == NULL)
7470 : : {
7471 : 0 : error (0, 0, _("cannot get ELF: %s"), dwfl_errmsg (-1));
7472 : 0 : return;
7473 : : }
7474 : :
7475 : 196 : bool is_eh_frame = strcmp (scnname, ".eh_frame") == 0;
7476 : 196 : Elf_Data *data;
7477 [ + + ]: 196 : if (is_eh_frame)
7478 : : {
7479 : 144 : data = elf_rawdata (scn, NULL);
7480 [ - + ]: 144 : if (data == NULL)
7481 : : {
7482 : 0 : error (0, 0, _("cannot get %s content: %s"),
7483 : : scnname, elf_errmsg (-1));
7484 : 0 : return;
7485 : : }
7486 : : }
7487 : : else
7488 : : {
7489 : 52 : data = get_debug_elf_data (dbg, ebl, IDX_debug_frame, scn);
7490 [ + - ]: 52 : if (data == NULL)
7491 : : return;
7492 : : }
7493 : :
7494 [ + + ]: 196 : if (is_eh_frame)
7495 : 144 : fprintf (out, _("\
7496 : : \nCall frame information section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
7497 : 144 : elf_ndxscn (scn), scnname, (uint64_t) shdr->sh_offset);
7498 : : else
7499 : 52 : fprintf (out, _("\
7500 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
7501 : 52 : elf_ndxscn (scn), scnname, (uint64_t) shdr->sh_offset);
7502 : :
7503 : 196 : struct cieinfo
7504 : : {
7505 : : ptrdiff_t cie_offset;
7506 : : const char *augmentation;
7507 : : unsigned int code_alignment_factor;
7508 : : unsigned int data_alignment_factor;
7509 : : uint8_t address_size;
7510 : : uint8_t fde_encoding;
7511 : : uint8_t lsda_encoding;
7512 : : struct cieinfo *next;
7513 : 196 : } *cies = NULL;
7514 : :
7515 : 196 : const unsigned char *readp = data->d_buf;
7516 : 196 : const unsigned char *const dataend = ((unsigned char *) data->d_buf
7517 : 196 : + data->d_size);
7518 [ + + ]: 24856 : while (readp < dataend)
7519 : : {
7520 [ - + ]: 24660 : if (unlikely (readp + 4 > dataend))
7521 : : {
7522 : 0 : invalid_data:
7523 : 0 : error (0, 0, _("invalid data in section [%zu] '%s'"),
7524 : : elf_ndxscn (scn), scnname);
7525 : 0 : return;
7526 : : }
7527 : :
7528 : : /* At the beginning there must be a CIE. There can be multiple,
7529 : : hence we test tis in a loop. */
7530 : 24660 : ptrdiff_t offset = readp - (unsigned char *) data->d_buf;
7531 : :
7532 [ + + ]: 24660 : Dwarf_Word unit_length = read_4ubyte_unaligned_inc (dbg, readp);
7533 : 24660 : unsigned int length = 4;
7534 [ - + ]: 24660 : if (unlikely (unit_length == 0xffffffff))
7535 : : {
7536 [ # # ]: 0 : if (unlikely (readp + 8 > dataend))
7537 : 0 : goto invalid_data;
7538 : :
7539 [ # # ]: 0 : unit_length = read_8ubyte_unaligned_inc (dbg, readp);
7540 : 0 : length = 8;
7541 : : }
7542 : :
7543 [ + + ]: 24660 : if (unlikely (unit_length == 0))
7544 : : {
7545 : 140 : fprintf (out, _("\n [%6tx] Zero terminator\n"), offset);
7546 : 140 : continue;
7547 : : }
7548 : :
7549 : 24520 : Dwarf_Word maxsize = dataend - readp;
7550 [ - + ]: 24520 : if (unlikely (unit_length > maxsize))
7551 : 0 : goto invalid_data;
7552 : :
7553 [ + + ]: 24520 : unsigned int ptr_size = ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 4 : 8;
7554 : :
7555 : 24520 : ptrdiff_t start = readp - (unsigned char *) data->d_buf;
7556 : 24520 : const unsigned char *const cieend = readp + unit_length;
7557 [ - + ]: 24520 : if (unlikely (cieend > dataend))
7558 : 0 : goto invalid_data;
7559 : :
7560 : 24520 : Dwarf_Off cie_id;
7561 [ + - ]: 24520 : if (length == 4)
7562 : : {
7563 [ - + ]: 24520 : if (unlikely (cieend - readp < 4))
7564 : 0 : goto invalid_data;
7565 [ + + ]: 24520 : cie_id = read_4ubyte_unaligned_inc (dbg, readp);
7566 [ + + ]: 24520 : if (!is_eh_frame && cie_id == DW_CIE_ID_32)
7567 : : cie_id = DW_CIE_ID_64;
7568 : : }
7569 : : else
7570 : : {
7571 [ # # ]: 0 : if (unlikely (cieend - readp < 8))
7572 : 0 : goto invalid_data;
7573 [ # # ]: 0 : cie_id = read_8ubyte_unaligned_inc (dbg, readp);
7574 : : }
7575 : :
7576 : 48956 : uint_fast8_t version = 2;
7577 : 24436 : unsigned int code_alignment_factor;
7578 : 24436 : int data_alignment_factor;
7579 : 48956 : unsigned int fde_encoding = 0;
7580 : 48956 : unsigned int lsda_encoding = 0;
7581 : 48956 : Dwarf_Word initial_location = 0;
7582 : 48956 : Dwarf_Word vma_base = 0;
7583 : :
7584 [ + + + + ]: 24612 : if (cie_id == (is_eh_frame ? 0 : DW_CIE_ID_64))
7585 : : {
7586 [ - + ]: 310 : if (unlikely (cieend - readp < 2))
7587 : 0 : goto invalid_data;
7588 : 310 : version = *readp++;
7589 : 310 : const char *const augmentation = (const char *) readp;
7590 : 310 : readp = memchr (readp, '\0', cieend - readp);
7591 [ - + ]: 310 : if (unlikely (readp == NULL))
7592 : 0 : goto invalid_data;
7593 : 310 : ++readp;
7594 : :
7595 : 310 : uint_fast8_t segment_size = 0;
7596 [ + + ]: 310 : if (version >= 4)
7597 : : {
7598 [ - + ]: 8 : if (cieend - readp < 5)
7599 : 0 : goto invalid_data;
7600 : 8 : ptr_size = *readp++;
7601 : 8 : segment_size = *readp++;
7602 : : }
7603 : :
7604 [ - + ]: 310 : if (cieend - readp < 1)
7605 : 0 : goto invalid_data;
7606 : 310 : get_uleb128 (code_alignment_factor, readp, cieend);
7607 [ - + ]: 310 : if (cieend - readp < 1)
7608 : 0 : goto invalid_data;
7609 : 310 : get_sleb128 (data_alignment_factor, readp, cieend);
7610 : :
7611 : : /* In some variant for unwind data there is another field. */
7612 [ - + ]: 310 : if (strcmp (augmentation, "eh") == 0)
7613 [ # # ]: 0 : readp += ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 4 : 8;
7614 : :
7615 : 310 : unsigned int return_address_register;
7616 [ - + ]: 310 : if (cieend - readp < 1)
7617 : 0 : goto invalid_data;
7618 [ + + ]: 310 : if (unlikely (version == 1))
7619 : 286 : return_address_register = *readp++;
7620 : : else
7621 : 24 : get_uleb128 (return_address_register, readp, cieend);
7622 : :
7623 : 310 : fprintf (out, "\n [%6tx] CIE length=%" PRIu64 "\n"
7624 : : " CIE_id: %" PRIu64 "\n"
7625 : : " version: %u\n"
7626 : : " augmentation: \"%s\"\n",
7627 : : offset, (uint64_t) unit_length, (uint64_t) cie_id,
7628 : : version, augmentation);
7629 [ + + ]: 310 : if (version >= 4)
7630 : 8 : fprintf (out, " address_size: %u\n"
7631 : : " segment_size: %u\n",
7632 : : ptr_size, segment_size);
7633 : 310 : fprintf (out, " code_alignment_factor: %u\n"
7634 : : " data_alignment_factor: %d\n"
7635 : : " return_address_register: %u\n",
7636 : : code_alignment_factor,
7637 : : data_alignment_factor, return_address_register);
7638 : :
7639 [ + + ]: 310 : if (augmentation[0] == 'z')
7640 : : {
7641 [ - + ]: 222 : if (cieend - readp < 1)
7642 : 0 : goto invalid_data;
7643 : :
7644 : 222 : unsigned int augmentationlen;
7645 : 222 : get_uleb128 (augmentationlen, readp, cieend);
7646 : :
7647 [ - + ]: 222 : if (augmentationlen > (size_t) (cieend - readp))
7648 : : {
7649 : 0 : error (0, 0, _("invalid augmentation length"));
7650 : 0 : readp = cieend;
7651 : 0 : continue;
7652 : : }
7653 : :
7654 : 222 : const char *hdr = "Augmentation data:";
7655 : 222 : const char *cp = augmentation + 1;
7656 [ + + + - ]: 444 : while (*cp != '\0' && cp < augmentation + augmentationlen + 1)
7657 : : {
7658 : 222 : fprintf (out, " %-26s%#x ", hdr, *readp);
7659 : 222 : hdr = "";
7660 : :
7661 [ + - ]: 222 : if (*cp == 'R')
7662 : : {
7663 : 222 : fde_encoding = *readp++;
7664 : 222 : print_encoding_base (_("FDE address encoding: "),
7665 : : fde_encoding, out);
7666 : : }
7667 [ # # ]: 0 : else if (*cp == 'L')
7668 : : {
7669 : 0 : lsda_encoding = *readp++;
7670 : 0 : print_encoding_base (_("LSDA pointer encoding: "),
7671 : : lsda_encoding, out);
7672 : : }
7673 [ # # ]: 0 : else if (*cp == 'P')
7674 : : {
7675 : : /* Personality. This field usually has a relocation
7676 : : attached pointing to __gcc_personality_v0. */
7677 : 0 : const unsigned char *startp = readp;
7678 : 0 : unsigned int encoding = *readp++;
7679 : 0 : uint64_t val = 0;
7680 : 0 : readp = read_encoded (encoding, readp,
7681 : : readp - 1 + augmentationlen,
7682 : : &val, dbg);
7683 : :
7684 [ # # ]: 0 : while (++startp < readp)
7685 : 0 : fprintf (out, "%#x ", *startp);
7686 : :
7687 : 0 : fputc ('(', out);
7688 : 0 : print_encoding (encoding, out);
7689 : 0 : fputc (' ', out);
7690 [ # # ]: 0 : switch (encoding & 0xf)
7691 : : {
7692 : 0 : case DW_EH_PE_sleb128:
7693 : : case DW_EH_PE_sdata2:
7694 : : case DW_EH_PE_sdata4:
7695 : 0 : fprintf (out, "%" PRId64 ")\n", val);
7696 : 0 : break;
7697 : 0 : default:
7698 : 0 : fprintf (out, "%#" PRIx64 ")\n", val);
7699 : 0 : break;
7700 : : }
7701 : : }
7702 : : else
7703 : 0 : fprintf (out, "(%x)\n", *readp++);
7704 : :
7705 : 222 : ++cp;
7706 : : }
7707 : : }
7708 : :
7709 [ + - ]: 310 : if (likely (ptr_size == 4 || ptr_size == 8))
7710 : : {
7711 : 310 : struct cieinfo *newp = alloca (sizeof (*newp));
7712 : 310 : newp->cie_offset = offset;
7713 : 310 : newp->augmentation = augmentation;
7714 : 310 : newp->fde_encoding = fde_encoding;
7715 : 310 : newp->lsda_encoding = lsda_encoding;
7716 : 310 : newp->address_size = ptr_size;
7717 : 310 : newp->code_alignment_factor = code_alignment_factor;
7718 : 310 : newp->data_alignment_factor = data_alignment_factor;
7719 : 310 : newp->next = cies;
7720 : 310 : cies = newp;
7721 : : }
7722 : : }
7723 : : else
7724 : : {
7725 : : struct cieinfo *cie = cies;
7726 [ + - ]: 24210 : while (cie != NULL)
7727 [ + + - + ]: 48420 : if (is_eh_frame
7728 : 24118 : ? ((Dwarf_Off) start - cie_id) == (Dwarf_Off) cie->cie_offset
7729 : 92 : : cie_id == (Dwarf_Off) cie->cie_offset)
7730 : : break;
7731 : : else
7732 : 0 : cie = cie->next;
7733 : 24210 : if (unlikely (cie == NULL))
7734 : : {
7735 : 0 : fputs ("invalid CIE reference in FDE\n", out);
7736 : 0 : return;
7737 : : }
7738 : :
7739 : : /* Initialize from CIE data. */
7740 : 24210 : fde_encoding = cie->fde_encoding;
7741 : 24210 : lsda_encoding = cie->lsda_encoding;
7742 : 24210 : ptr_size = encoded_ptr_size (fde_encoding, cie->address_size);
7743 : 24210 : code_alignment_factor = cie->code_alignment_factor;
7744 : 24210 : data_alignment_factor = cie->data_alignment_factor;
7745 : :
7746 : 24210 : const unsigned char *base = readp;
7747 : : // XXX There are sometimes relocations for this value
7748 [ - + + + : 24210 : initial_location = read_addr_unaligned_inc (ptr_size, dbg, readp);
- + + + ]
7749 : 48420 : Dwarf_Word address_range
7750 [ + + - + : 24210 : = read_addr_unaligned_inc (ptr_size, dbg, readp);
+ + ]
7751 : :
7752 : : /* pcrel for an FDE address is relative to the runtime
7753 : : address of the start_address field itself. Sign extend
7754 : : if necessary to make sure the calculation is done on the
7755 : : full 64 bit address even when initial_location only holds
7756 : : the lower 32 bits. */
7757 : 24210 : Dwarf_Addr pc_start = initial_location;
7758 [ + + ]: 24210 : if (ptr_size == 4)
7759 : 24130 : pc_start = (uint64_t) (int32_t) pc_start;
7760 [ + + ]: 24210 : if ((fde_encoding & 0x70) == DW_EH_PE_pcrel)
7761 : 24106 : pc_start += ((uint64_t) shdr->sh_addr
7762 : 24106 : + (base - (const unsigned char *) data->d_buf)
7763 : 24106 : - bias);
7764 : :
7765 : 24210 : fprintf (out, "\n [%6tx] FDE length=%" PRIu64 " cie=[%6tx]\n"
7766 : : " CIE_pointer: %" PRIu64 "\n"
7767 : : " initial_location: ",
7768 : : offset, (uint64_t) unit_length,
7769 : : cie->cie_offset, (uint64_t) cie_id);
7770 : 24210 : print_dwarf_addr (dwflmod, cie->address_size,
7771 : : pc_start, initial_location, out);
7772 [ + + ]: 24210 : if ((fde_encoding & 0x70) == DW_EH_PE_pcrel)
7773 : : {
7774 : 48212 : vma_base = (((uint64_t) shdr->sh_offset
7775 : 24106 : + (base - (const unsigned char *) data->d_buf)
7776 : 24106 : + (uint64_t) initial_location)
7777 : : & (ptr_size == 4
7778 : : ? UINT64_C (0xffffffff)
7779 [ - + ]: 24106 : : UINT64_C (0xffffffffffffffff)));
7780 : 24106 : fprintf (out, _(" (offset: %#" PRIx64 ")"),
7781 : : (uint64_t) vma_base);
7782 : : }
7783 : :
7784 : 24210 : fprintf (out, "\n address_range: %#" PRIx64,
7785 : : (uint64_t) address_range);
7786 [ + + ]: 24210 : if ((fde_encoding & 0x70) == DW_EH_PE_pcrel)
7787 : 24106 : fprintf (out, _(" (end offset: %#" PRIx64 ")"),
7788 : 24106 : ((uint64_t) vma_base + (uint64_t) address_range)
7789 : : & (ptr_size == 4
7790 : : ? UINT64_C (0xffffffff)
7791 [ - + ]: 24106 : : UINT64_C (0xffffffffffffffff)));
7792 : 24210 : fputc ('\n', out);
7793 : :
7794 [ + + ]: 24210 : if (cie->augmentation[0] == 'z')
7795 : : {
7796 : 24106 : unsigned int augmentationlen;
7797 [ - + ]: 24106 : if (cieend - readp < 1)
7798 : 0 : goto invalid_data;
7799 : 24106 : get_uleb128 (augmentationlen, readp, cieend);
7800 : :
7801 [ - + ]: 24106 : if (augmentationlen > (size_t) (cieend - readp))
7802 : : {
7803 : 0 : error (0, 0, _("invalid augmentation length"));
7804 : 0 : readp = cieend;
7805 : 0 : continue;
7806 : : }
7807 : :
7808 [ - + ]: 24106 : if (augmentationlen > 0)
7809 : : {
7810 : 0 : const char *hdr = "Augmentation data:";
7811 : 0 : const char *cp = cie->augmentation + 1;
7812 : 0 : unsigned int u = 0;
7813 : 0 : while (*cp != '\0'
7814 [ # # # # ]: 0 : && cp < cie->augmentation + augmentationlen + 1)
7815 : : {
7816 [ # # ]: 0 : if (*cp == 'L')
7817 : : {
7818 : 0 : uint64_t lsda_pointer;
7819 : 0 : const unsigned char *p
7820 : 0 : = read_encoded (lsda_encoding, &readp[u],
7821 : : &readp[augmentationlen],
7822 : : &lsda_pointer, dbg);
7823 : 0 : u = p - readp;
7824 : 0 : fprintf (out, _("\
7825 : : %-26sLSDA pointer: %#" PRIx64 "\n"),
7826 : : hdr, lsda_pointer);
7827 : 0 : hdr = "";
7828 : : }
7829 : 0 : ++cp;
7830 : : }
7831 : :
7832 [ # # ]: 0 : while (u < augmentationlen)
7833 : : {
7834 : 0 : fprintf (out, " %-26s%#x\n", hdr, readp[u++]);
7835 : 0 : hdr = "";
7836 : : }
7837 : : }
7838 : :
7839 : 24106 : readp += augmentationlen;
7840 : : }
7841 : : }
7842 : :
7843 : : /* Handle the initialization instructions. */
7844 [ - + ]: 24520 : if (ptr_size != 4 && ptr_size !=8)
7845 : 0 : fprintf (out, "invalid CIE pointer size (%u), must be 4 or 8.\n",
7846 : : ptr_size);
7847 : : else
7848 : 24520 : print_cfa_program (readp, cieend, vma_base, code_alignment_factor,
7849 : : data_alignment_factor, version, ptr_size,
7850 : : fde_encoding, dwflmod, ebl, ehdr, dbg, out);
7851 : 24520 : readp = cieend;
7852 : : }
7853 : : }
7854 : :
7855 : :
7856 : : /* Returns the signedness (or false if it cannot be determined) and
7857 : : the byte size (or zero if it cannot be gotten) of the given DIE
7858 : : DW_AT_type attribute. Uses dwarf_peel_type and dwarf_aggregate_size. */
7859 : : static void
7860 : 353004 : die_type_sign_bytes (Dwarf_Die *die, bool *is_signed, int *bytes)
7861 : : {
7862 : 353004 : Dwarf_Attribute attr;
7863 : 353004 : Dwarf_Die type;
7864 : :
7865 : 353004 : *bytes = 0;
7866 : 353004 : *is_signed = false;
7867 : :
7868 [ + + ]: 353004 : if (dwarf_peel_type (dwarf_formref_die (dwarf_attr_integrate (die,
7869 : : DW_AT_type,
7870 : : &attr), &type),
7871 : : &type) == 0)
7872 : : {
7873 : 974 : Dwarf_Word val;
7874 : 974 : *is_signed = (dwarf_formudata (dwarf_attr (&type, DW_AT_encoding,
7875 : : &attr), &val) == 0
7876 [ + + + + ]: 974 : && (val == DW_ATE_signed || val == DW_ATE_signed_char));
7877 : :
7878 [ + - ]: 974 : if (dwarf_aggregate_size (&type, &val) == 0)
7879 : 974 : *bytes = val;
7880 : : }
7881 : 353004 : }
7882 : :
7883 : : struct attrcb_args
7884 : : {
7885 : : Dwfl_Module *dwflmod;
7886 : : Dwarf *dbg;
7887 : : Dwarf_Die *dies;
7888 : : int level;
7889 : : bool silent;
7890 : : bool is_split;
7891 : : unsigned int version;
7892 : : unsigned int addrsize;
7893 : : unsigned int offset_size;
7894 : : struct Dwarf_CU *cu;
7895 : : FILE *out;
7896 : : };
7897 : :
7898 : :
7899 : : static int
7900 : 8119144 : attr_callback (Dwarf_Attribute *attrp, void *arg)
7901 : : {
7902 : 8119144 : struct attrcb_args *cbargs = (struct attrcb_args *) arg;
7903 : 8119144 : const int level = cbargs->level;
7904 : 8119144 : Dwarf_Die *die = &cbargs->dies[level];
7905 : 8119144 : bool is_split = cbargs->is_split;
7906 : 8119144 : FILE *out = cbargs->out;
7907 : :
7908 [ + - ]: 8119144 : unsigned int attr = dwarf_whatattr (attrp);
7909 [ - + ]: 8119144 : if (unlikely (attr == 0))
7910 : : {
7911 [ # # ]: 0 : if (!cbargs->silent)
7912 : 0 : error (0, 0, _("DIE [%" PRIx64 "] "
7913 : : "cannot get attribute code: %s"),
7914 : : dwarf_dieoffset (die), dwarf_errmsg (-1));
7915 : 0 : return DWARF_CB_ABORT;
7916 : : }
7917 : :
7918 [ - + ]: 8119144 : unsigned int form = dwarf_whatform (attrp);
7919 [ - + ]: 8119144 : if (unlikely (form == 0))
7920 : : {
7921 [ # # ]: 0 : if (!cbargs->silent)
7922 : 0 : error (0, 0, _("DIE [%" PRIx64 "] "
7923 : : "cannot get attribute form: %s"),
7924 : : dwarf_dieoffset (die), dwarf_errmsg (-1));
7925 : 0 : return DWARF_CB_ABORT;
7926 : : }
7927 : :
7928 [ + + + + : 8119144 : switch (form)
+ + + +
- ]
7929 : : {
7930 : 103118 : case DW_FORM_addr:
7931 : : case DW_FORM_addrx:
7932 : : case DW_FORM_addrx1:
7933 : : case DW_FORM_addrx2:
7934 : : case DW_FORM_addrx3:
7935 : : case DW_FORM_addrx4:
7936 : : case DW_FORM_GNU_addr_index:
7937 [ + + ]: 103118 : if (!cbargs->silent)
7938 : : {
7939 : 102576 : Dwarf_Addr addr;
7940 [ - + ]: 102576 : if (unlikely (dwarf_formaddr (attrp, &addr) != 0))
7941 : : {
7942 : 0 : attrval_out:
7943 [ # # ]: 0 : if (!cbargs->silent)
7944 : 0 : error (0, 0, _("DIE [%" PRIx64 "] "
7945 : : "cannot get attribute '%s' (%s) value: "
7946 : : "%s"),
7947 : : dwarf_dieoffset (die),
7948 : : dwarf_attr_name (attr),
7949 : : dwarf_form_name (form),
7950 : : dwarf_errmsg (-1));
7951 : : /* Don't ABORT, it might be other attributes can be resolved. */
7952 : 0 : return DWARF_CB_OK;
7953 : : }
7954 [ + + ]: 102576 : if (form != DW_FORM_addr )
7955 : : {
7956 : 40 : Dwarf_Word word;
7957 [ - + ]: 40 : if (dwarf_formudata (attrp, &word) != 0)
7958 : 0 : goto attrval_out;
7959 : 40 : fprintf (out, " %*s%-20s (%s) [%" PRIx64 "] ",
7960 : : (int) (level * 2), "", dwarf_attr_name (attr),
7961 : : dwarf_form_name (form), word);
7962 : : }
7963 : : else
7964 : 102536 : fprintf (out, " %*s%-20s (%s) ",
7965 : : (int) (level * 2), "", dwarf_attr_name (attr),
7966 : : dwarf_form_name (form));
7967 : 102576 : print_dwarf_addr (cbargs->dwflmod, cbargs->addrsize, addr, addr, out);
7968 : 102576 : fprintf (out, "\n");
7969 : : }
7970 : 7299362 : break;
7971 : :
7972 : 1344224 : case DW_FORM_indirect:
7973 : : case DW_FORM_strp:
7974 : : case DW_FORM_line_strp:
7975 : : case DW_FORM_strx:
7976 : : case DW_FORM_strx1:
7977 : : case DW_FORM_strx2:
7978 : : case DW_FORM_strx3:
7979 : : case DW_FORM_strx4:
7980 : : case DW_FORM_string:
7981 : : case DW_FORM_GNU_strp_alt:
7982 : : case DW_FORM_GNU_str_index:
7983 [ + + ]: 1344224 : if (cbargs->silent)
7984 : : break;
7985 : 1342714 : const char *str = dwarf_formstring (attrp);
7986 [ - + ]: 1342714 : if (unlikely (str == NULL))
7987 : 0 : goto attrval_out;
7988 : 1342714 : fprintf (out, " %*s%-20s (%s) \"%s\"\n",
7989 : : (int) (level * 2), "", dwarf_attr_name (attr),
7990 : : dwarf_form_name (form), str);
7991 : 1342714 : break;
7992 : :
7993 : 1755222 : case DW_FORM_ref_addr:
7994 : : case DW_FORM_ref_udata:
7995 : : case DW_FORM_ref8:
7996 : : case DW_FORM_ref4:
7997 : : case DW_FORM_ref2:
7998 : : case DW_FORM_ref1:
7999 : : case DW_FORM_GNU_ref_alt:
8000 : : case DW_FORM_ref_sup4:
8001 : : case DW_FORM_ref_sup8:
8002 [ + + ]: 1755222 : if (cbargs->silent)
8003 : : break;
8004 : 1753642 : Dwarf_Die ref;
8005 [ - + ]: 1753642 : if (unlikely (dwarf_formref_die (attrp, &ref) == NULL))
8006 : 0 : goto attrval_out;
8007 : :
8008 : 1753642 : fprintf (out, " %*s%-20s (%s) ",
8009 : : (int) (level * 2), "", dwarf_attr_name (attr),
8010 : : dwarf_form_name (form));
8011 [ + + ]: 1753642 : if (is_split)
8012 : 66 : fprintf (out, "{%6" PRIxMAX "}\n", (uintmax_t) dwarf_dieoffset (&ref));
8013 : : else
8014 : 1753576 : fprintf (out, "[%6" PRIxMAX "]\n", (uintmax_t) dwarf_dieoffset (&ref));
8015 : : break;
8016 : :
8017 : 8 : case DW_FORM_ref_sig8:
8018 [ + - ]: 8 : if (cbargs->silent)
8019 : : break;
8020 : 8 : fprintf (out, " %*s%-20s (%s) {%6" PRIx64 "}\n",
8021 : : (int) (level * 2), "", dwarf_attr_name (attr),
8022 : : dwarf_form_name (form),
8023 [ - + ]: 8 : (uint64_t) read_8ubyte_unaligned (attrp->cu->dbg, attrp->valp));
8024 : 8 : break;
8025 : :
8026 : 4513904 : case DW_FORM_sec_offset:
8027 : : case DW_FORM_rnglistx:
8028 : : case DW_FORM_loclistx:
8029 : : case DW_FORM_implicit_const:
8030 : : case DW_FORM_udata:
8031 : : case DW_FORM_sdata:
8032 : : case DW_FORM_data8: /* Note no data16 here, we see that as block. */
8033 : : case DW_FORM_data4:
8034 : : case DW_FORM_data2:
8035 : 4513904 : case DW_FORM_data1:;
8036 : 4513904 : Dwarf_Word num;
8037 [ - + ]: 4513904 : if (unlikely (dwarf_formudata (attrp, &num) != 0))
8038 : 0 : goto attrval_out;
8039 : :
8040 : 4513904 : const char *valuestr = NULL;
8041 : 4513904 : bool as_hex_id = false;
8042 [ + + + + : 4513904 : switch (attr)
+ + + + +
+ - - - -
- - + - +
+ + ]
8043 : : {
8044 : : /* This case can take either a constant or a loclistptr. */
8045 : 562104 : case DW_AT_data_member_location:
8046 [ + - ]: 562104 : if (form != DW_FORM_sec_offset
8047 [ + + ]: 562104 : && (cbargs->version >= 4
8048 [ + - ]: 47612 : || (form != DW_FORM_data4 && form != DW_FORM_data8)))
8049 : : {
8050 [ + - ]: 562104 : if (!cbargs->silent)
8051 : 562104 : fprintf (out, " %*s%-20s (%s) %" PRIuMAX "\n",
8052 : : (int) (level * 2), "", dwarf_attr_name (attr),
8053 : : dwarf_form_name (form), (uintmax_t) num);
8054 : 562104 : return DWARF_CB_OK;
8055 : : }
8056 : 221870 : FALLTHROUGH;
8057 : :
8058 : : /* These cases always take a loclist[ptr] and no constant. */
8059 : : case DW_AT_location:
8060 : : case DW_AT_data_location:
8061 : : case DW_AT_vtable_elem_location:
8062 : : case DW_AT_string_length:
8063 : : case DW_AT_use_location:
8064 : : case DW_AT_frame_base:
8065 : : case DW_AT_return_addr:
8066 : : case DW_AT_static_link:
8067 : : case DW_AT_segment:
8068 : : case DW_AT_GNU_call_site_value:
8069 : : case DW_AT_GNU_call_site_data_value:
8070 : : case DW_AT_GNU_call_site_target:
8071 : : case DW_AT_GNU_call_site_target_clobbered:
8072 : : case DW_AT_GNU_locviews:
8073 : : {
8074 : 221870 : bool nlpt;
8075 [ + + ]: 221870 : if (cbargs->cu->version < 5)
8076 : : {
8077 [ + + ]: 324 : if (! cbargs->is_split)
8078 : : {
8079 : 284 : nlpt = notice_listptr (section_loc, &known_locsptr,
8080 : 284 : cbargs->addrsize,
8081 : 284 : cbargs->offset_size,
8082 : : cbargs->cu, num, attr);
8083 : : }
8084 : : else
8085 : : nlpt = true;
8086 : : }
8087 : : else
8088 : : {
8089 : : /* Only register for a real section offset. Otherwise
8090 : : it is a DW_FORM_loclistx which is just an index
8091 : : number and we should already have registered the
8092 : : section offset for the index when we saw the
8093 : : DW_AT_loclists_base CU attribute. */
8094 [ + + ]: 221546 : if (form == DW_FORM_sec_offset)
8095 : 221468 : nlpt = notice_listptr (section_loc, &known_loclistsptr,
8096 : 221468 : cbargs->addrsize, cbargs->offset_size,
8097 : : cbargs->cu, num, attr);
8098 : : else
8099 : : nlpt = true;
8100 : :
8101 : : }
8102 : :
8103 [ + + ]: 221870 : if (!cbargs->silent)
8104 : : {
8105 [ + + + + ]: 221504 : if (cbargs->cu->version < 5 || form == DW_FORM_sec_offset)
8106 [ - + ]: 221490 : fprintf (out, " %*s%-20s (%s) location list [%6"
8107 : : PRIxMAX "]%s\n",
8108 : : (int) (level * 2), "", dwarf_attr_name (attr),
8109 : : dwarf_form_name (form), (uintmax_t) num,
8110 : : nlpt ? "" : " <WARNING offset too big>");
8111 : : else
8112 : 14 : fprintf (out, " %*s%-20s (%s) location index [%6"
8113 : : PRIxMAX "]\n",
8114 : : (int) (level * 2), "", dwarf_attr_name (attr),
8115 : : dwarf_form_name (form), (uintmax_t) num);
8116 : : }
8117 : : }
8118 : : return DWARF_CB_OK;
8119 : :
8120 : 10 : case DW_AT_loclists_base:
8121 : : {
8122 : 20 : bool nlpt = notice_listptr (section_loc, &known_loclistsptr,
8123 : 10 : cbargs->addrsize, cbargs->offset_size,
8124 : : cbargs->cu, num, attr);
8125 : :
8126 [ + + ]: 10 : if (!cbargs->silent)
8127 [ - + ]: 2 : fprintf (out, " %*s%-20s (%s) location list [%6" PRIxMAX "]%s\n",
8128 : : (int) (level * 2), "", dwarf_attr_name (attr),
8129 : : dwarf_form_name (form), (uintmax_t) num,
8130 : : nlpt ? "" : " <WARNING offset too big>");
8131 : : }
8132 : : return DWARF_CB_OK;
8133 : :
8134 : 35748 : case DW_AT_ranges:
8135 : : case DW_AT_start_scope:
8136 : : {
8137 : 35748 : bool nlpt;
8138 [ + + ]: 35748 : if (cbargs->cu->version < 5)
8139 : 106 : nlpt = notice_listptr (section_ranges, &known_rangelistptr,
8140 : 106 : cbargs->addrsize, cbargs->offset_size,
8141 : : cbargs->cu, num, attr);
8142 : : else
8143 : : {
8144 : : /* Only register for a real section offset. Otherwise
8145 : : it is a DW_FORM_rangelistx which is just an index
8146 : : number and we should already have registered the
8147 : : section offset for the index when we saw the
8148 : : DW_AT_rnglists_base CU attribute. */
8149 [ + + ]: 35642 : if (form == DW_FORM_sec_offset)
8150 : 35630 : nlpt = notice_listptr (section_ranges, &known_rnglistptr,
8151 : 35630 : cbargs->addrsize, cbargs->offset_size,
8152 : : cbargs->cu, num, attr);
8153 : : else
8154 : : nlpt = true;
8155 : : }
8156 : :
8157 [ + + ]: 35748 : if (!cbargs->silent)
8158 : : {
8159 [ + + + + ]: 35662 : if (cbargs->cu->version < 5 || form == DW_FORM_sec_offset)
8160 [ - + ]: 35658 : fprintf (out, " %*s%-20s (%s) range list [%6"
8161 : : PRIxMAX "]%s\n",
8162 : : (int) (level * 2), "", dwarf_attr_name (attr),
8163 : : dwarf_form_name (form), (uintmax_t) num,
8164 : : nlpt ? "" : " <WARNING offset too big>");
8165 : : else
8166 : 4 : fprintf (out, " %*s%-20s (%s) range index [%6"
8167 : : PRIxMAX "]\n",
8168 : : (int) (level * 2), "", dwarf_attr_name (attr),
8169 : : dwarf_form_name (form), (uintmax_t) num);
8170 : : }
8171 : : }
8172 : : return DWARF_CB_OK;
8173 : :
8174 : 8 : case DW_AT_rnglists_base:
8175 : : {
8176 : 16 : bool nlpt = notice_listptr (section_ranges, &known_rnglistptr,
8177 : 8 : cbargs->addrsize, cbargs->offset_size,
8178 : : cbargs->cu, num, attr);
8179 [ + + ]: 8 : if (!cbargs->silent)
8180 [ - + ]: 4 : fprintf (out, " %*s%-20s (%s) range list [%6"
8181 : : PRIxMAX "]%s\n",
8182 : : (int) (level * 2), "", dwarf_attr_name (attr),
8183 : : dwarf_form_name (form), (uintmax_t) num,
8184 : : nlpt ? "" : " <WARNING offset too big>");
8185 : : }
8186 : : return DWARF_CB_OK;
8187 : :
8188 : 34 : case DW_AT_addr_base:
8189 : : case DW_AT_GNU_addr_base:
8190 : : {
8191 : 68 : bool addrbase = notice_listptr (section_addr, &known_addrbases,
8192 : 34 : cbargs->addrsize,
8193 : 34 : cbargs->offset_size,
8194 : : cbargs->cu, num, attr);
8195 [ + + ]: 34 : if (!cbargs->silent)
8196 [ - + ]: 18 : fprintf (out, " %*s%-20s (%s) address base [%6"
8197 : : PRIxMAX "]%s\n",
8198 : : (int) (level * 2), "", dwarf_attr_name (attr),
8199 : : dwarf_form_name (form), (uintmax_t) num,
8200 : : addrbase ? "" : " <WARNING offset too big>");
8201 : : }
8202 : : return DWARF_CB_OK;
8203 : :
8204 : 8 : case DW_AT_str_offsets_base:
8205 : : {
8206 : 16 : bool stroffbase = notice_listptr (section_str, &known_stroffbases,
8207 : 8 : cbargs->addrsize,
8208 : 8 : cbargs->offset_size,
8209 : : cbargs->cu, num, attr);
8210 [ + - ]: 8 : if (!cbargs->silent)
8211 [ - + ]: 8 : fprintf (out, " %*s%-20s (%s) str offsets base [%6"
8212 : : PRIxMAX "]%s\n",
8213 : : (int) (level * 2), "", dwarf_attr_name (attr),
8214 : : dwarf_form_name (form), (uintmax_t) num,
8215 : : stroffbase ? "" : " <WARNING offset too big>");
8216 : : }
8217 : : return DWARF_CB_OK;
8218 : :
8219 : 3736 : case DW_AT_language:
8220 : 3736 : valuestr = dwarf_lang_name (num);
8221 : 3736 : break;
8222 : 4 : case DW_AT_language_name:
8223 : 4 : valuestr = dwarf_lname_name (num);
8224 : 4 : break;
8225 : 59402 : case DW_AT_encoding:
8226 : 59402 : valuestr = dwarf_encoding_name (num);
8227 : 59402 : break;
8228 : 0 : case DW_AT_accessibility:
8229 : 0 : valuestr = dwarf_access_name (num);
8230 : 0 : break;
8231 : 0 : case DW_AT_defaulted:
8232 : 0 : valuestr = dwarf_defaulted_name (num);
8233 : 0 : break;
8234 : 0 : case DW_AT_visibility:
8235 : 0 : valuestr = dwarf_visibility_name (num);
8236 : 0 : break;
8237 : 0 : case DW_AT_virtuality:
8238 : 0 : valuestr = dwarf_virtuality_name (num);
8239 : 0 : break;
8240 : 0 : case DW_AT_identifier_case:
8241 : 0 : valuestr = dwarf_identifier_case_name (num);
8242 : 0 : break;
8243 : 0 : case DW_AT_calling_convention:
8244 : 0 : valuestr = dwarf_calling_convention_name (num);
8245 : 0 : break;
8246 : 7280 : case DW_AT_inline:
8247 : 7280 : valuestr = dwarf_inline_name (num);
8248 : 7280 : break;
8249 : 0 : case DW_AT_ordering:
8250 : 0 : valuestr = dwarf_ordering_name (num);
8251 : 0 : break;
8252 : 991928 : case DW_AT_decl_file:
8253 : : case DW_AT_call_file:
8254 : : {
8255 [ + + ]: 991928 : if (cbargs->silent)
8256 : : break;
8257 : :
8258 : : /* Try to get the actual file, the current interface only
8259 : : gives us full paths, but we only want to show the file
8260 : : name for now. */
8261 : 991080 : Dwarf_Die cudie;
8262 [ + - ]: 991080 : if (dwarf_cu_die (cbargs->cu, &cudie,
8263 : : NULL, NULL, NULL, NULL, NULL, NULL) != NULL)
8264 : : {
8265 : 991080 : Dwarf_Files *files;
8266 : 991080 : size_t nfiles;
8267 [ + - ]: 991080 : if (dwarf_getsrcfiles (&cudie, &files, &nfiles) == 0)
8268 : : {
8269 : 991080 : valuestr = dwarf_filesrc (files, num, NULL, NULL);
8270 [ + - ]: 991080 : if (valuestr != NULL)
8271 : : {
8272 : 991080 : char *filename = strrchr (valuestr, '/');
8273 [ - + ]: 991080 : if (filename != NULL)
8274 : 991080 : valuestr = filename + 1;
8275 : : }
8276 : : else
8277 : 0 : error (0, 0, _("invalid file (%" PRId64 "): %s"),
8278 : : num, dwarf_errmsg (-1));
8279 : : }
8280 : : else
8281 : 0 : error (0, 0, _("no srcfiles for CU [%" PRIx64 "]"),
8282 : : dwarf_dieoffset (&cudie));
8283 : : }
8284 : : else
8285 : 0 : error (0, 0, _("couldn't get DWARF CU: %s"),
8286 : : dwarf_errmsg (-1));
8287 : 991080 : if (valuestr == NULL)
8288 : : valuestr = "???";
8289 : : }
8290 : 991080 : break;
8291 : 26 : case DW_AT_GNU_dwo_id:
8292 : 26 : as_hex_id = true;
8293 : 26 : break;
8294 : :
8295 : : default:
8296 : : /* Nothing. */
8297 : : break;
8298 : : }
8299 : :
8300 [ + + ]: 3694122 : if (cbargs->silent)
8301 : : break;
8302 : :
8303 : : /* When highpc is in constant form it is relative to lowpc.
8304 : : In that case also show the address. */
8305 : 3690412 : Dwarf_Addr highpc;
8306 [ + + + - ]: 3690412 : if (attr == DW_AT_high_pc && dwarf_highpc (die, &highpc) == 0)
8307 : : {
8308 : 23998 : fprintf (out, " %*s%-20s (%s) %" PRIuMAX " (",
8309 : : (int) (level * 2), "", dwarf_attr_name (attr),
8310 : : dwarf_form_name (form), (uintmax_t) num);
8311 : 23998 : print_dwarf_addr (cbargs->dwflmod, cbargs->addrsize,
8312 : : highpc, highpc, out);
8313 : 23998 : fprintf (out, ")\n");
8314 : : }
8315 : : else
8316 : : {
8317 [ + + ]: 3666414 : if (as_hex_id)
8318 : : {
8319 : 4 : fprintf (out, " %*s%-20s (%s) 0x%.16" PRIx64 "\n",
8320 : : (int) (level * 2), "", dwarf_attr_name (attr),
8321 : : dwarf_form_name (form), num);
8322 : : }
8323 : : else
8324 : : {
8325 : 3666410 : Dwarf_Sword snum = 0;
8326 : 3666410 : bool is_signed;
8327 : 3666410 : int bytes = 0;
8328 [ + + ]: 3666410 : if (attr == DW_AT_const_value)
8329 : 352996 : die_type_sign_bytes (die, &is_signed, &bytes);
8330 : : else
8331 : 3313414 : is_signed = (form == DW_FORM_sdata
8332 : 3313414 : || form == DW_FORM_implicit_const);
8333 : :
8334 [ + + ]: 3666410 : if (is_signed)
8335 [ - + ]: 381644 : if (unlikely (dwarf_formsdata (attrp, &snum) != 0))
8336 : 0 : goto attrval_out;
8337 : :
8338 [ + + ]: 3666410 : if (valuestr == NULL)
8339 : : {
8340 : 2605518 : fprintf (out, " %*s%-20s (%s) ",
8341 : : (int) (level * 2), "", dwarf_attr_name (attr),
8342 : : dwarf_form_name (form));
8343 : : }
8344 : : else
8345 : : {
8346 : 1060892 : fprintf (out, " %*s%-20s (%s) %s (",
8347 : : (int) (level * 2), "", dwarf_attr_name (attr),
8348 : : dwarf_form_name (form), valuestr);
8349 : : }
8350 : :
8351 [ + + + + : 3666410 : switch (bytes)
+ ]
8352 : : {
8353 : 10 : case 1:
8354 [ + + ]: 10 : if (is_signed)
8355 : 2 : fprintf (out, "%" PRId8, (int8_t) snum);
8356 : : else
8357 : 8 : fprintf (out, "%" PRIu8, (uint8_t) num);
8358 : : break;
8359 : :
8360 : 4 : case 2:
8361 [ + + ]: 4 : if (is_signed)
8362 : 2 : fprintf (out, "%" PRId16, (int16_t) snum);
8363 : : else
8364 : 2 : fprintf (out, "%" PRIu16, (uint16_t) num);
8365 : : break;
8366 : :
8367 : 234 : case 4:
8368 [ + + ]: 234 : if (is_signed)
8369 : 210 : fprintf (out, "%" PRId32, (int32_t) snum);
8370 : : else
8371 : 24 : fprintf (out, "%" PRIu32, (uint32_t) num);
8372 : : break;
8373 : :
8374 : 718 : case 8:
8375 [ + + ]: 718 : if (is_signed)
8376 : 2 : fprintf (out, "%" PRId64, (int64_t) snum);
8377 : : else
8378 : 716 : fprintf (out, "%" PRIu64, (uint64_t) num);
8379 : : break;
8380 : :
8381 : 3665444 : default:
8382 [ + + ]: 3665444 : if (is_signed)
8383 : 381428 : fprintf (out, "%" PRIdMAX, (intmax_t) snum);
8384 : : else
8385 : 3284016 : fprintf (out, "%" PRIuMAX, (uintmax_t) num);
8386 : : break;
8387 : : }
8388 : :
8389 : : /* Make clear if we switched from a signed encoding to
8390 : : an unsigned value. */
8391 [ + + ]: 3666410 : if (attr == DW_AT_const_value
8392 [ + + ]: 352996 : && (form == DW_FORM_sdata || form == DW_FORM_implicit_const)
8393 [ + + ]: 2680 : && !is_signed)
8394 : 2662 : fprintf (out, " (%" PRIdMAX ")", (intmax_t) num);
8395 : :
8396 [ + + ]: 3666410 : if (valuestr == NULL)
8397 : 2605518 : fprintf (out, "\n");
8398 : : else
8399 : 1060892 : fprintf (out, ")\n");
8400 : : }
8401 : : }
8402 : : break;
8403 : :
8404 : 18244 : case DW_FORM_flag:
8405 [ + + ]: 18244 : if (cbargs->silent)
8406 : : break;
8407 : 18096 : bool flag;
8408 [ - + ]: 18096 : if (unlikely (dwarf_formflag (attrp, &flag) != 0))
8409 : 0 : goto attrval_out;
8410 : :
8411 : 18096 : fprintf (out, " %*s%-20s (%s) %s\n",
8412 : : (int) (level * 2), "", dwarf_attr_name (attr),
8413 [ + - ]: 18096 : dwarf_form_name (form), flag ? yes_str : no_str);
8414 : 18096 : break;
8415 : :
8416 : 160734 : case DW_FORM_flag_present:
8417 [ + + ]: 160734 : if (cbargs->silent)
8418 : : break;
8419 : 160180 : fprintf (out, " %*s%-20s (%s) %s\n",
8420 : : (int) (level * 2), "", dwarf_attr_name (attr),
8421 : : dwarf_form_name (form), yes_str);
8422 : 160180 : break;
8423 : :
8424 : 223690 : case DW_FORM_exprloc:
8425 : : case DW_FORM_block4:
8426 : : case DW_FORM_block2:
8427 : : case DW_FORM_block1:
8428 : : case DW_FORM_block:
8429 : : case DW_FORM_data16: /* DWARF5 calls this a constant class. */
8430 [ + + ]: 223690 : if (cbargs->silent)
8431 : : break;
8432 : 223428 : Dwarf_Block block;
8433 [ - + ]: 223428 : if (unlikely (dwarf_formblock (attrp, &block) != 0))
8434 : 0 : goto attrval_out;
8435 : :
8436 : 223428 : fprintf (out, " %*s%-20s (%s) ",
8437 : : (int) (level * 2), "", dwarf_attr_name (attr),
8438 : : dwarf_form_name (form));
8439 : :
8440 [ + + + ]: 223428 : switch (attr)
8441 : : {
8442 : 96350 : default:
8443 [ - + ]: 96350 : if (form != DW_FORM_exprloc)
8444 : : {
8445 : 0 : print_block (block.length, block.data, out);
8446 : 0 : break;
8447 : : }
8448 : 127070 : FALLTHROUGH;
8449 : :
8450 : : case DW_AT_location:
8451 : : case DW_AT_data_location:
8452 : : case DW_AT_data_member_location:
8453 : : case DW_AT_vtable_elem_location:
8454 : : case DW_AT_string_length:
8455 : : case DW_AT_use_location:
8456 : : case DW_AT_frame_base:
8457 : : case DW_AT_return_addr:
8458 : : case DW_AT_static_link:
8459 : : case DW_AT_allocated:
8460 : : case DW_AT_associated:
8461 : : case DW_AT_bit_size:
8462 : : case DW_AT_bit_offset:
8463 : : case DW_AT_bit_stride:
8464 : : case DW_AT_byte_size:
8465 : : case DW_AT_byte_stride:
8466 : : case DW_AT_count:
8467 : : case DW_AT_lower_bound:
8468 : : case DW_AT_upper_bound:
8469 : : case DW_AT_GNU_call_site_value:
8470 : : case DW_AT_GNU_call_site_data_value:
8471 : : case DW_AT_GNU_call_site_target:
8472 : : case DW_AT_GNU_call_site_target_clobbered:
8473 [ + + ]: 127070 : if (form == DW_FORM_exprloc
8474 [ + - ]: 140 : || (form != DW_FORM_data16
8475 [ + - ]: 140 : && attrp->cu->version < 4)) /* blocks were expressions. */
8476 : : {
8477 : 223420 : fputc ('\n', out);
8478 : 223420 : print_ops (cbargs->dwflmod, cbargs->dbg,
8479 : 223420 : 12 + level * 2, 12 + level * 2,
8480 : : cbargs->version, cbargs->addrsize, cbargs->offset_size,
8481 : 223420 : attrp->cu, block.length, block.data, out);
8482 : : }
8483 : : else
8484 : 0 : print_block (block.length, block.data, out);
8485 : : break;
8486 : :
8487 : 8 : case DW_AT_discr_list:
8488 [ - + ]: 8 : if (block.length == 0)
8489 : 0 : fputs ("<default>\n", out);
8490 [ + - ]: 8 : else if (form != DW_FORM_data16)
8491 : : {
8492 : 8 : const unsigned char *readp = block.data;
8493 : 8 : const unsigned char *readendp = readp + block.length;
8494 : :
8495 : : /* See if we are dealing with a signed or unsigned
8496 : : values. If the parent of this variant DIE is a
8497 : : variant_part then it will either have a discriminant
8498 : : which points to the member which type is the
8499 : : discriminant type. Or the variant_part itself has a
8500 : : type representing the discriminant. */
8501 : 8 : bool is_signed = false;
8502 [ + - ]: 8 : if (level > 0)
8503 : : {
8504 : 8 : Dwarf_Die *parent = &cbargs->dies[level - 1];
8505 [ + - ]: 8 : if (dwarf_tag (die) == DW_TAG_variant
8506 [ + - ]: 8 : && dwarf_tag (parent) == DW_TAG_variant_part)
8507 : : {
8508 : 8 : Dwarf_Die member;
8509 : 8 : Dwarf_Attribute discr_attr;
8510 : 8 : int bytes;
8511 [ + - ]: 8 : if (dwarf_formref_die (dwarf_attr (parent,
8512 : : DW_AT_discr,
8513 : : &discr_attr),
8514 : : &member) != NULL)
8515 : 8 : die_type_sign_bytes (&member, &is_signed, &bytes);
8516 : : else
8517 : 0 : die_type_sign_bytes (parent, &is_signed, &bytes);
8518 : : }
8519 : : }
8520 [ + + ]: 24 : while (readp < readendp)
8521 : : {
8522 : 16 : int d = (int) *readp++;
8523 : 16 : fprintf (out, "%s ", dwarf_discr_list_name (d));
8524 [ - + ]: 16 : if (readp >= readendp)
8525 : 0 : goto attrval_out;
8526 : :
8527 : 16 : Dwarf_Word val;
8528 : 16 : Dwarf_Sword sval;
8529 [ + + ]: 16 : if (d == DW_DSC_label)
8530 : : {
8531 [ + + ]: 8 : if (is_signed)
8532 : : {
8533 : 4 : get_sleb128 (sval, readp, readendp);
8534 : 4 : fprintf (out, "%" PRId64 "", sval);
8535 : : }
8536 : : else
8537 : : {
8538 : 4 : get_uleb128 (val, readp, readendp);
8539 : 4 : fprintf (out, "%" PRIu64 "", val);
8540 : : }
8541 : : }
8542 [ + - ]: 8 : else if (d == DW_DSC_range)
8543 : : {
8544 [ + + ]: 8 : if (is_signed)
8545 : : {
8546 : 6 : get_sleb128 (sval, readp, readendp);
8547 : 6 : fprintf (out, "%" PRId64 "..", sval);
8548 [ - + ]: 6 : if (readp >= readendp)
8549 : 0 : goto attrval_out;
8550 : 6 : get_sleb128 (sval, readp, readendp);
8551 : 6 : fprintf (out, "%" PRId64 "", sval);
8552 : : }
8553 : : else
8554 : : {
8555 : 2 : get_uleb128 (val, readp, readendp);
8556 : 2 : fprintf (out, "%" PRIu64 "..", val);
8557 [ - + ]: 2 : if (readp >= readendp)
8558 : 0 : goto attrval_out;
8559 : 2 : get_uleb128 (val, readp, readendp);
8560 : 2 : fprintf (out, "%" PRIu64 "", val);
8561 : : }
8562 : : }
8563 : : else
8564 : : {
8565 : 0 : print_block (readendp - readp, readp, out);
8566 : 0 : break;
8567 : : }
8568 [ + + ]: 16 : if (readp < readendp)
8569 : 8 : fprintf (out, ", ");
8570 : : }
8571 : 8 : fputc ('\n', out);
8572 : : }
8573 : : else
8574 : 0 : print_block (block.length, block.data, out);
8575 : : break;
8576 : : }
8577 : : break;
8578 : :
8579 : 0 : default:
8580 [ # # ]: 0 : if (cbargs->silent)
8581 : : break;
8582 : 0 : fprintf (out, " %*s%-20s (%s) ???\n",
8583 : : (int) (level * 2), "", dwarf_attr_name (attr),
8584 : : dwarf_form_name (form));
8585 : 0 : break;
8586 : : }
8587 : :
8588 : 7299362 : return DWARF_CB_OK;
8589 : : }
8590 : :
8591 : : static void
8592 : 208 : print_debug_units (Dwfl_Module *dwflmod,
8593 : : Ebl *ebl, GElf_Ehdr *ehdr __attribute__ ((unused)),
8594 : : Elf_Scn *scn, GElf_Shdr *shdr,
8595 : : Dwarf *dbg, bool debug_types, FILE *out)
8596 : : {
8597 [ + + + + ]: 208 : const bool silent = !(print_debug_sections & section_info) && !debug_types;
8598 : 208 : const char *secname = section_name (ebl, shdr);
8599 : :
8600 : : /* Check section actually exists. */
8601 [ + + ]: 208 : if (!silent)
8602 [ + - ]: 136 : if (get_debug_elf_data (dbg, ebl,
8603 : : debug_types ? IDX_debug_types : IDX_debug_info,
8604 : : scn) == NULL)
8605 : 0 : return;
8606 : :
8607 : 136 : if (!silent)
8608 : 136 : fprintf (out, _("\
8609 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n [Offset]\n"),
8610 : 136 : elf_ndxscn (scn), secname, (uint64_t) shdr->sh_offset);
8611 : :
8612 : : /* If the section is empty we don't have to do anything. */
8613 [ + + + - ]: 208 : if (!silent && shdr->sh_size == 0)
8614 : : return;
8615 : :
8616 : 208 : int maxdies = 20;
8617 : 208 : Dwarf_Die *dies = xmalloc (maxdies * sizeof (Dwarf_Die));
8618 : :
8619 : : /* New compilation unit. */
8620 : 208 : Dwarf_Half version;
8621 : :
8622 : 208 : Dwarf_Die result;
8623 : 208 : Dwarf_Off abbroffset;
8624 : 208 : uint8_t addrsize;
8625 : 208 : uint8_t offsize;
8626 : 208 : uint64_t unit_id;
8627 : 208 : Dwarf_Off subdie_off;
8628 : :
8629 : 208 : int unit_res;
8630 : 208 : Dwarf_CU *cu;
8631 : 208 : Dwarf_CU cu_mem;
8632 : 208 : uint8_t unit_type;
8633 : 208 : Dwarf_Die cudie;
8634 : :
8635 : : /* We cheat a little because we want to see only the CUs from .debug_info
8636 : : or .debug_types. We know the Dwarf_CU struct layout. Set it up at
8637 : : the end of .debug_info if we want .debug_types only. Check the returned
8638 : : Dwarf_CU is still in the expected section. */
8639 [ + + ]: 208 : if (debug_types)
8640 : : {
8641 : 2 : cu_mem.dbg = dbg;
8642 : 2 : cu_mem.end = dbg->sectiondata[IDX_debug_info]->d_size;
8643 : 2 : cu_mem.sec_idx = IDX_debug_info;
8644 : 2 : cu = &cu_mem;
8645 : : }
8646 : : else
8647 : 206 : cu = NULL;
8648 : :
8649 : : next_cu:
8650 : 3962 : unit_res = dwarf_get_units (dbg, cu, &cu, &version, &unit_type,
8651 : : &cudie, NULL);
8652 [ + + ]: 3962 : if (unit_res == 1)
8653 : 206 : goto do_return;
8654 : :
8655 [ - + ]: 3756 : if (unit_res == -1)
8656 : : {
8657 [ # # ]: 0 : if (!silent)
8658 : 0 : error (0, 0, _("cannot get next unit: %s"), dwarf_errmsg (-1));
8659 : 0 : goto do_return;
8660 : : }
8661 : :
8662 [ + + + + ]: 7508 : if (cu->sec_idx != (size_t) (debug_types ? IDX_debug_types : IDX_debug_info))
8663 : 2 : goto do_return;
8664 : :
8665 : 3754 : dwarf_cu_die (cu, &result, NULL, &abbroffset, &addrsize, &offsize,
8666 : : &unit_id, &subdie_off);
8667 : :
8668 [ + + ]: 3754 : if (!silent)
8669 : : {
8670 : 3654 : Dwarf_Off offset = cu->start;
8671 [ + + + - ]: 3654 : if (debug_types && version < 5)
8672 : 4 : {
8673 : 4 : Dwarf_Die typedie;
8674 : 4 : Dwarf_Off dieoffset;
8675 : 4 : dieoffset = dwarf_dieoffset (dwarf_offdie_types (dbg, cu->start
8676 : : + subdie_off,
8677 : : &typedie));
8678 : 4 : fprintf (out, " %s %" PRIu64 ":\n"
8679 : : " %s: %" PRIu16
8680 : : ", %s: %" PRIu64
8681 : : ", %s: %" PRIu8
8682 : : ", %s: %" PRIu8
8683 : : "\n %s: %#" PRIx64
8684 : : ", %s: %#" PRIx64 " [%" PRIx64 "]\n",
8685 : : type_unit_str, (uint64_t) offset, version_str, version,
8686 : : abbrev_offset_str, abbroffset, addr_size_str, addrsize,
8687 : : offset_size_str, offsize, type_sig_str, unit_id,
8688 : : type_offset_str, (uint64_t) subdie_off, dieoffset);
8689 : : }
8690 : : else
8691 : : {
8692 : 3650 : fprintf (out, " %s %" PRIu64 ":\n"
8693 : : " %s: %" PRIu16
8694 : : ", %s: %" PRIu64
8695 : : ", %s: %" PRIu8
8696 : : ", %s: %" PRIu8 "\n",
8697 : : cu_offset_at_str, (uint64_t) offset, version_str, version,
8698 : : abbrev_offset_str, abbroffset, addr_size_str, addrsize,
8699 : : offset_size_str, offsize);
8700 : :
8701 [ + + ]: 3650 : if (version >= 5 || (unit_type != DW_UT_compile
8702 [ + + ]: 170 : && unit_type != DW_UT_partial))
8703 : : {
8704 : 3482 : fprintf (out, " %s: %s (%" PRIu8 ")",
8705 : : unit_type_str, dwarf_unit_name (unit_type), unit_type);
8706 : 3482 : if (unit_type == DW_UT_type
8707 [ + + ]: 3482 : || unit_type == DW_UT_skeleton
8708 [ + - ]: 3472 : || unit_type == DW_UT_split_compile
8709 [ - + ]: 3472 : || unit_type == DW_UT_split_type)
8710 : 10 : fprintf (out, ", %s: 0x%.16" PRIx64 "", unit_id_str, unit_id);
8711 : 3482 : if (unit_type == DW_UT_type
8712 [ - + ]: 3482 : || unit_type == DW_UT_split_type)
8713 : : {
8714 : 0 : Dwarf_Die typedie;
8715 : 0 : Dwarf_Off dieoffset;
8716 : 0 : dwarf_cu_info (cu, NULL, NULL, NULL, &typedie,
8717 : : NULL, NULL, NULL);
8718 : 0 : dieoffset = dwarf_dieoffset (&typedie);
8719 : 0 : fprintf (out, ", %s: %#" PRIx64 " [%" PRIx64 "]",
8720 : : unit_die_off_str, subdie_off, dieoffset);
8721 : : }
8722 : 3482 : fprintf (out, "\n");
8723 : : }
8724 : : }
8725 : : }
8726 : :
8727 [ + - ]: 3754 : if (version < 2 || version > 5
8728 [ + - - + ]: 3754 : || unit_type < DW_UT_compile || unit_type > DW_UT_split_type)
8729 : : {
8730 [ # # ]: 0 : if (!silent)
8731 : 0 : error (0, 0, _("unknown version (%d) or unit type (%d)"),
8732 : : version, unit_type);
8733 : 0 : goto next_cu;
8734 : : }
8735 : :
8736 : 3754 : struct attrcb_args args =
8737 : : {
8738 : : .dwflmod = dwflmod,
8739 : : .silent = silent,
8740 : : .version = version,
8741 : : .addrsize = addrsize,
8742 : : .offset_size = offsize
8743 : : };
8744 : :
8745 : 3754 : bool is_split = false;
8746 : 3754 : int level = 0;
8747 : 3754 : dies[0] = cudie;
8748 : 3754 : args.cu = dies[0].cu;
8749 : 3754 : args.dbg = dbg;
8750 : 3754 : args.is_split = is_split;
8751 : 3754 : args.out = out;
8752 : :
8753 : : /* We might return here again for the split CU subdie. */
8754 : : do_cu:
8755 : 2128028 : do
8756 : : {
8757 : 2128028 : Dwarf_Off offset = dwarf_dieoffset (&dies[level]);
8758 [ - + ]: 2128028 : if (unlikely (offset == (Dwarf_Off) -1))
8759 : : {
8760 [ # # ]: 0 : if (!silent)
8761 : 0 : error (0, 0, _("cannot get DIE offset: %s"),
8762 : : dwarf_errmsg (-1));
8763 : 0 : goto do_return;
8764 : : }
8765 : :
8766 : 2128028 : int tag = dwarf_tag (&dies[level]);
8767 [ - + ]: 2128028 : if (unlikely (tag == DW_TAG_invalid))
8768 : : {
8769 [ # # ]: 0 : if (!silent)
8770 : 0 : error (0, 0, _("cannot get tag of DIE at offset [%" PRIx64
8771 : : "] in section '%s': %s"),
8772 : : (uint64_t) offset, secname, dwarf_errmsg (-1));
8773 : 0 : goto do_return;
8774 : : }
8775 : :
8776 [ + + ]: 2128028 : if (!silent)
8777 : : {
8778 : 2126042 : unsigned int code = dwarf_getabbrevcode (dies[level].abbrev);
8779 [ + + ]: 2126042 : if (is_split)
8780 : 96 : fprintf (out, " {%6" PRIx64 "} ", (uint64_t) offset);
8781 : : else
8782 : 2125946 : fprintf (out, " [%6" PRIx64 "] ", (uint64_t) offset);
8783 : 2126042 : fprintf (out, "%*s%-20s abbrev: %u\n", (int) (level * 2), "",
8784 : : dwarf_tag_name (tag), code);
8785 : : }
8786 : :
8787 : : /* Print the attribute values. */
8788 : 2128028 : args.level = level;
8789 : 2128028 : args.dies = dies;
8790 : 2128028 : (void) dwarf_getattrs (&dies[level], attr_callback, &args, 0);
8791 : :
8792 : : /* Make room for the next level's DIE. */
8793 [ - + ]: 2128028 : if (level + 1 == maxdies)
8794 : 0 : dies = xrealloc (dies, (maxdies += 10) * sizeof (Dwarf_Die));
8795 : :
8796 : 2128028 : int res = dwarf_child (&dies[level], &dies[level + 1]);
8797 [ + + ]: 2128028 : if (res > 0)
8798 : : {
8799 [ + + ]: 2128028 : while ((res = dwarf_siblingof (&dies[level], &dies[level])) == 1)
8800 [ + + ]: 295360 : if (level-- == 0)
8801 : : break;
8802 : :
8803 [ - + ]: 1836430 : if (unlikely (res == -1))
8804 : : {
8805 [ # # ]: 0 : if (!silent)
8806 : 0 : error (0, 0, _("cannot get next DIE: %s\n"),
8807 : : dwarf_errmsg (-1));
8808 : 0 : goto do_return;
8809 : : }
8810 : : }
8811 [ - + ]: 291598 : else if (unlikely (res < 0))
8812 : : {
8813 [ # # ]: 0 : if (!silent)
8814 : 0 : error (0, 0, _("cannot get next DIE: %s"),
8815 : : dwarf_errmsg (-1));
8816 : 0 : goto do_return;
8817 : : }
8818 : : else
8819 : : ++level;
8820 : : }
8821 [ + + ]: 2128028 : while (level >= 0);
8822 : :
8823 : : /* We might want to show the split compile unit if this was a skeleton.
8824 : : We need to scan it if we are requesting printing .debug_ranges for
8825 : : DWARF4 since GNU DebugFission uses "offsets" into the main ranges
8826 : : section. */
8827 [ + + ]: 3762 : if (unit_type == DW_UT_skeleton
8828 [ + + + + ]: 26 : && ((!silent && show_split_units)
8829 [ + + + + ]: 20 : || (version < 5 && (print_debug_sections & section_ranges) != 0)))
8830 : : {
8831 : 10 : Dwarf_Die subdie;
8832 [ + - ]: 10 : if (dwarf_cu_info (cu, NULL, NULL, NULL, &subdie, NULL, NULL, NULL) != 0
8833 [ + + ]: 10 : || dwarf_tag (&subdie) == DW_TAG_invalid)
8834 : : {
8835 [ + - ]: 2 : if (!silent)
8836 : : {
8837 : 2 : Dwarf_Attribute dwo_at;
8838 : 4 : const char *dwo_name =
8839 : 2 : (dwarf_formstring (dwarf_attr (&cudie, DW_AT_dwo_name,
8840 : : &dwo_at))
8841 [ - + ]: 2 : ?: (dwarf_formstring (dwarf_attr (&cudie, DW_AT_GNU_dwo_name,
8842 : : &dwo_at))
8843 [ # # ]: 0 : ?: "<unknown>"));
8844 : 2 : fprintf (stderr,
8845 : : "Could not find split unit '%s', id: %" PRIx64 "\n",
8846 : : dwo_name, unit_id);
8847 : : }
8848 : : }
8849 : : else
8850 : : {
8851 : 8 : Dwarf_CU *split_cu = subdie.cu;
8852 : 8 : dwarf_cu_die (split_cu, &result, NULL, &abbroffset,
8853 : : &addrsize, &offsize, &unit_id, &subdie_off);
8854 : 8 : Dwarf_Off offset = cu->start;
8855 : :
8856 [ + + ]: 8 : if (!silent)
8857 : : {
8858 : 4 : fprintf (out, _(" Split compilation unit at offset %"
8859 : : PRIu64 ":\n"
8860 : : " Version: %" PRIu16
8861 : : ", Abbreviation section offset: %" PRIu64
8862 : : ", Address size: %" PRIu8
8863 : : ", Offset size: %" PRIu8 "\n"),
8864 : : (uint64_t) offset, version, abbroffset,
8865 : : addrsize, offsize);
8866 : 4 : fprintf (out, _(" Unit type: %s (%" PRIu8 ")"),
8867 : : dwarf_unit_name (unit_type), unit_type);
8868 : 4 : fprintf (out, ", Unit id: 0x%.16" PRIx64 "", unit_id);
8869 : 4 : fprintf (out, "\n");
8870 : : }
8871 : :
8872 : 8 : unit_type = DW_UT_split_compile;
8873 : 8 : is_split = true;
8874 : 8 : level = 0;
8875 : 8 : dies[0] = subdie;
8876 : 8 : args.cu = dies[0].cu;
8877 : 8 : args.dbg = split_cu->dbg;
8878 : 8 : args.is_split = is_split;
8879 : 8 : goto do_cu;
8880 : : }
8881 : : }
8882 : :
8883 : : /* And again... */
8884 : 3754 : goto next_cu;
8885 : :
8886 : 208 : do_return:
8887 : 208 : free (dies);
8888 : : }
8889 : :
8890 : : static void
8891 : 206 : print_debug_info_section (Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr,
8892 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg,
8893 : : FILE *out)
8894 : : {
8895 : 42 : print_debug_units (dwflmod, ebl, ehdr, scn, shdr, dbg, false, out);
8896 : 42 : }
8897 : :
8898 : : static void
8899 : 2 : print_debug_types_section (Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr,
8900 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg,
8901 : : FILE *out)
8902 : : {
8903 : 2 : print_debug_units (dwflmod, ebl, ehdr, scn, shdr, dbg, true, out);
8904 : 2 : }
8905 : :
8906 : :
8907 : : static void
8908 : 82 : print_decoded_line_section (Dwfl_Module *dwflmod, Ebl *ebl,
8909 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
8910 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg,
8911 : : FILE *out)
8912 : : {
8913 : 82 : fprintf (out, _("\
8914 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n\n"),
8915 : : elf_ndxscn (scn), section_name (ebl, shdr),
8916 : 82 : (uint64_t) shdr->sh_offset);
8917 : :
8918 : 164 : size_t address_size
8919 [ + + ]: 82 : = elf_getident (ebl->elf, NULL)[EI_CLASS] == ELFCLASS32 ? 4 : 8;
8920 : :
8921 : 82 : Dwarf_Lines *lines;
8922 : 82 : size_t nlines;
8923 : 82 : Dwarf_Off off, next_off = 0;
8924 : 82 : Dwarf_CU *cu = NULL;
8925 : 82 : while (dwarf_next_lines (dbg, off = next_off, &next_off, &cu, NULL, NULL,
8926 [ + + ]: 176 : &lines, &nlines) == 0)
8927 : : {
8928 : 94 : Dwarf_Die cudie;
8929 [ + + + - ]: 94 : if (cu != NULL && dwarf_cu_info (cu, NULL, NULL, &cudie,
8930 : : NULL, NULL, NULL, NULL) == 0)
8931 : 50 : fprintf (out, " CU [%" PRIx64 "] %s\n",
8932 : : dwarf_dieoffset (&cudie), dwarf_diename (&cudie));
8933 : : else
8934 : : {
8935 : : /* DWARF5 lines can be independent of any CU, but they probably
8936 : : are used by some CU. Determine the CU this block is for. */
8937 : 44 : Dwarf_Off cuoffset;
8938 : 44 : Dwarf_Off ncuoffset = 0;
8939 : 44 : size_t hsize;
8940 : 44 : while (dwarf_nextcu (dbg, cuoffset = ncuoffset, &ncuoffset, &hsize,
8941 [ + - ]: 46 : NULL, NULL, NULL) == 0)
8942 : : {
8943 [ - + ]: 46 : if (dwarf_offdie (dbg, cuoffset + hsize, &cudie) == NULL)
8944 : 0 : continue;
8945 : 46 : Dwarf_Attribute stmt_list;
8946 [ - + ]: 46 : if (dwarf_attr (&cudie, DW_AT_stmt_list, &stmt_list) == NULL)
8947 : 0 : continue;
8948 : 46 : Dwarf_Word lineoff;
8949 [ - + ]: 46 : if (dwarf_formudata (&stmt_list, &lineoff) != 0)
8950 : 0 : continue;
8951 [ + + ]: 46 : if (lineoff == off)
8952 : : {
8953 : : /* Found the CU. */
8954 : 44 : cu = cudie.cu;
8955 : 44 : break;
8956 : : }
8957 : : }
8958 : :
8959 [ + - ]: 44 : if (cu != NULL)
8960 : 44 : fprintf (out, " CU [%" PRIx64 "] %s\n",
8961 : : dwarf_dieoffset (&cudie), dwarf_diename (&cudie));
8962 : : else
8963 : 0 : fprintf (out, " No CU\n");
8964 : : }
8965 : :
8966 : 94 : fprintf (out, " line:col SBPE* disc isa op address"
8967 : : " (Statement Block Prologue Epilogue *End)\n");
8968 : 94 : const char *last_file = "";
8969 [ + + ]: 720 : for (size_t n = 0; n < nlines; n++)
8970 : : {
8971 : 626 : Dwarf_Line *line = dwarf_onesrcline (lines, n);
8972 [ - + ]: 626 : if (line == NULL)
8973 : : {
8974 : 0 : fprintf (out, " dwarf_onesrcline: %s\n", dwarf_errmsg (-1));
8975 : 0 : continue;
8976 : : }
8977 : 626 : Dwarf_Word mtime, length;
8978 : 626 : const char *file = dwarf_linesrc (line, &mtime, &length);
8979 [ - + ]: 626 : if (file == NULL)
8980 : : {
8981 : 0 : fprintf (out, " <%s> (mtime: ?, length: ?)\n",
8982 : : dwarf_errmsg (-1));
8983 : 0 : last_file = "";
8984 : : }
8985 [ + + ]: 626 : else if (strcmp (last_file, file) != 0)
8986 : : {
8987 : 114 : fprintf (out, " %s (mtime: %" PRIu64 ", length: %" PRIu64 ")\n",
8988 : : file, mtime, length);
8989 : 114 : last_file = file;
8990 : : }
8991 : :
8992 : 626 : int lineno, colno;
8993 : 626 : bool statement, endseq, block, prologue_end, epilogue_begin;
8994 : 626 : unsigned int lineop, isa, disc;
8995 : 626 : Dwarf_Addr address;
8996 : 626 : dwarf_lineaddr (line, &address);
8997 : 626 : dwarf_lineno (line, &lineno);
8998 : 626 : dwarf_linecol (line, &colno);
8999 : 626 : dwarf_lineop_index (line, &lineop);
9000 : 626 : dwarf_linebeginstatement (line, &statement);
9001 : 626 : dwarf_lineendsequence (line, &endseq);
9002 : 626 : dwarf_lineblock (line, &block);
9003 : 626 : dwarf_lineprologueend (line, &prologue_end);
9004 : 626 : dwarf_lineepiloguebegin (line, &epilogue_begin);
9005 : 626 : dwarf_lineisa (line, &isa);
9006 : 626 : dwarf_linediscriminator (line, &disc);
9007 : :
9008 : : /* End sequence is special, it is one byte past. */
9009 : 626 : fprintf (out, " %4d:%-3d %c%c%c%c%c %4d %3d %2d ",
9010 : : lineno, colno,
9011 [ + + ]: 626 : (statement ? 'S' : ' '),
9012 [ + - ]: 626 : (block ? 'B' : ' '),
9013 [ + + ]: 626 : (prologue_end ? 'P' : ' '),
9014 [ + - ]: 626 : (epilogue_begin ? 'E' : ' '),
9015 [ + + ]: 626 : (endseq ? '*' : ' '),
9016 : : disc, isa, lineop);
9017 : 626 : print_dwarf_addr (dwflmod, address_size,
9018 [ + + ]: 626 : address - (endseq ? 1 : 0), address, out);
9019 : 626 : fprintf (out, "\n");
9020 : :
9021 [ + + ]: 626 : if (endseq)
9022 : 98 : fprintf(out, "\n");
9023 : : }
9024 : : }
9025 : 82 : }
9026 : :
9027 : :
9028 : : /* Print the value of a form.
9029 : : Returns new value of readp, or readendp on failure. */
9030 : : static const unsigned char *
9031 : 143610 : print_form_data (Dwarf *dbg, int form, const unsigned char *readp,
9032 : : const unsigned char *readendp, unsigned int offset_len,
9033 : : Dwarf_Off str_offsets_base, FILE *out)
9034 : : {
9035 : 143610 : Dwarf_Word val;
9036 : 143610 : unsigned char *endp;
9037 : 143610 : Elf_Data *data;
9038 : 143610 : char *str;
9039 [ + - - - : 143610 : switch (form)
- + - - -
- + - - +
- + - - -
- - ]
9040 : : {
9041 : 16 : case DW_FORM_data1:
9042 [ - + ]: 16 : if (readendp - readp < 1)
9043 : : {
9044 : 0 : invalid_data:
9045 : 0 : error (0, 0, "invalid data");
9046 : 0 : return readendp;
9047 : : }
9048 : 16 : val = *readp++;
9049 : 16 : fprintf (out, " %" PRIx8, (unsigned int) val);
9050 : 16 : break;
9051 : :
9052 : 0 : case DW_FORM_data2:
9053 [ # # ]: 0 : if (readendp - readp < 2)
9054 : 0 : goto invalid_data;
9055 [ # # ]: 0 : val = read_2ubyte_unaligned_inc (dbg, readp);
9056 : 0 : fprintf(out, " %" PRIx16, (unsigned int) val);
9057 : 0 : break;
9058 : :
9059 : 0 : case DW_FORM_data4:
9060 [ # # ]: 0 : if (readendp - readp < 4)
9061 : 0 : goto invalid_data;
9062 [ # # ]: 0 : val = read_4ubyte_unaligned_inc (dbg, readp);
9063 : 0 : fprintf (out, " %" PRIx32, (unsigned int) val);
9064 : 0 : break;
9065 : :
9066 : 0 : case DW_FORM_data8:
9067 [ # # ]: 0 : if (readendp - readp < 8)
9068 : 0 : goto invalid_data;
9069 [ # # ]: 0 : val = read_8ubyte_unaligned_inc (dbg, readp);
9070 : 0 : fprintf (out, " %" PRIx64, val);
9071 : 0 : break;
9072 : :
9073 : 0 : case DW_FORM_sdata:
9074 [ # # ]: 0 : if (readendp - readp < 1)
9075 : 0 : goto invalid_data;
9076 : 0 : get_sleb128 (val, readp, readendp);
9077 : 0 : fprintf (out, " %" PRIx64, val);
9078 : 0 : break;
9079 : :
9080 : 57842 : case DW_FORM_udata:
9081 [ - + ]: 57842 : if (readendp - readp < 1)
9082 : 0 : goto invalid_data;
9083 : 57842 : get_uleb128 (val, readp, readendp);
9084 : 57842 : fprintf (out, " %" PRIx64, val);
9085 : 57842 : break;
9086 : :
9087 : 0 : case DW_FORM_block:
9088 [ # # ]: 0 : if (readendp - readp < 1)
9089 : 0 : goto invalid_data;
9090 : 0 : get_uleb128 (val, readp, readendp);
9091 [ # # ]: 0 : if ((size_t) (readendp - readp) < val)
9092 : 0 : goto invalid_data;
9093 : 0 : print_bytes (val, readp, out);
9094 : 0 : readp += val;
9095 : 0 : break;
9096 : :
9097 : 0 : case DW_FORM_block1:
9098 [ # # ]: 0 : if (readendp - readp < 1)
9099 : 0 : goto invalid_data;
9100 : 0 : val = *readp++;
9101 [ # # ]: 0 : if ((size_t) (readendp - readp) < val)
9102 : 0 : goto invalid_data;
9103 : 0 : print_bytes (val, readp, out);
9104 : 0 : readp += val;
9105 : 0 : break;
9106 : :
9107 : 0 : case DW_FORM_block2:
9108 [ # # ]: 0 : if (readendp - readp < 2)
9109 : 0 : goto invalid_data;
9110 [ # # ]: 0 : val = read_2ubyte_unaligned_inc (dbg, readp);
9111 [ # # ]: 0 : if ((size_t) (readendp - readp) < val)
9112 : 0 : goto invalid_data;
9113 : 0 : print_bytes (val, readp, out);
9114 : 0 : readp += val;
9115 : 0 : break;
9116 : :
9117 : 0 : case DW_FORM_block4:
9118 [ # # ]: 0 : if (readendp - readp < 4)
9119 : 0 : goto invalid_data;
9120 [ # # ]: 0 : val = read_4ubyte_unaligned_inc (dbg, readp);
9121 [ # # ]: 0 : if ((size_t) (readendp - readp) < val)
9122 : 0 : goto invalid_data;
9123 : 0 : print_bytes (val, readp, out);
9124 : 0 : readp += val;
9125 : 0 : break;
9126 : :
9127 : 24 : case DW_FORM_data16:
9128 [ - + ]: 24 : if (readendp - readp < 16)
9129 : 0 : goto invalid_data;
9130 : 24 : print_bytes (16, readp, out);
9131 : 24 : readp += 16;
9132 : 24 : break;
9133 : :
9134 : 0 : case DW_FORM_flag:
9135 [ # # ]: 0 : if (readendp - readp < 1)
9136 : 0 : goto invalid_data;
9137 : 0 : val = *readp++;
9138 [ # # ]: 0 : fprintf (out, "%s", val != 0 ? yes_str : no_str);
9139 : 0 : break;
9140 : :
9141 : 0 : case DW_FORM_string:
9142 : 0 : endp = memchr (readp, '\0', readendp - readp);
9143 [ # # ]: 0 : if (endp == NULL)
9144 : 0 : goto invalid_data;
9145 : 0 : fprintf (out, "%s", readp);
9146 : 0 : readp = endp + 1;
9147 : 0 : break;
9148 : :
9149 : 84172 : case DW_FORM_strp:
9150 : : case DW_FORM_line_strp:
9151 : : case DW_FORM_strp_sup:
9152 [ - + ]: 84172 : if ((size_t) (readendp - readp) < offset_len)
9153 : 0 : goto invalid_data;
9154 [ - + ]: 84172 : if (offset_len == 8)
9155 [ # # ]: 0 : val = read_8ubyte_unaligned_inc (dbg, readp);
9156 : : else
9157 [ - + ]: 84172 : val = read_4ubyte_unaligned_inc (dbg, readp);
9158 [ - + ]: 84172 : if (form == DW_FORM_strp)
9159 : 0 : data = dbg->sectiondata[IDX_debug_str];
9160 [ + - ]: 84172 : else if (form == DW_FORM_line_strp)
9161 : 84172 : data = dbg->sectiondata[IDX_debug_line_str];
9162 : : else /* form == DW_FORM_strp_sup */
9163 : : {
9164 : 0 : Dwarf *alt = dwarf_getalt (dbg);
9165 [ # # ]: 0 : data = alt != NULL ? alt->sectiondata[IDX_debug_str] : NULL;
9166 : : }
9167 [ + - - + ]: 84172 : if (data == NULL || val >= data->d_size
9168 [ - + ]: 84172 : || memchr (data->d_buf + val, '\0', data->d_size - val) == NULL)
9169 : : str = "???";
9170 : : else
9171 : 84172 : str = (char *) data->d_buf + val;
9172 : 84172 : fprintf (out, "%s (%" PRIu64 ")", str, val);
9173 : 84172 : break;
9174 : :
9175 : 0 : case DW_FORM_sec_offset:
9176 [ # # ]: 0 : if ((size_t) (readendp - readp) < offset_len)
9177 : 0 : goto invalid_data;
9178 [ # # ]: 0 : if (offset_len == 8)
9179 [ # # ]: 0 : val = read_8ubyte_unaligned_inc (dbg, readp);
9180 : : else
9181 [ # # ]: 0 : val = read_4ubyte_unaligned_inc (dbg, readp);
9182 : 0 : fprintf (out, "[%" PRIx64 "]", val);
9183 : 0 : break;
9184 : :
9185 : 1556 : case DW_FORM_strx:
9186 : : case DW_FORM_GNU_str_index:
9187 [ - + ]: 1556 : if (readendp - readp < 1)
9188 : 0 : goto invalid_data;
9189 : 1556 : get_uleb128 (val, readp, readendp);
9190 : 1556 : strx_val:
9191 : 1556 : data = dbg->sectiondata[IDX_debug_str_offsets];
9192 [ + - ]: 1556 : if (data == NULL
9193 [ - + ]: 1556 : || data->d_size - str_offsets_base < val * offset_len)
9194 : : str = "???";
9195 : : else
9196 : : {
9197 : 1556 : const unsigned char *strreadp = (data->d_buf + str_offsets_base
9198 : 1556 : + val * offset_len);
9199 : 1556 : const unsigned char *strreadendp = data->d_buf + data->d_size;
9200 [ - + ]: 1556 : if ((size_t) (strreadendp - strreadp) < offset_len)
9201 : : str = "???";
9202 : : else
9203 : : {
9204 : 1556 : Dwarf_Off idx;
9205 [ - + ]: 1556 : if (offset_len == 8)
9206 [ # # ]: 0 : idx = read_8ubyte_unaligned (dbg, strreadp);
9207 : : else
9208 [ - + ]: 1556 : idx = read_4ubyte_unaligned (dbg, strreadp);
9209 : :
9210 : 1556 : data = dbg->sectiondata[IDX_debug_str];
9211 [ - + - + ]: 1556 : if (data == NULL || idx >= data->d_size
9212 [ - + ]: 1556 : || memchr (data->d_buf + idx, '\0',
9213 : : data->d_size - idx) == NULL)
9214 : : str = "???";
9215 : : else
9216 : 1556 : str = (char *) data->d_buf + idx;
9217 : : }
9218 : : }
9219 : 1556 : fprintf (out, "%s (%" PRIu64 ")", str, val);
9220 : 1556 : break;
9221 : :
9222 : 0 : case DW_FORM_strx1:
9223 [ # # ]: 0 : if (readendp - readp < 1)
9224 : 0 : goto invalid_data;
9225 : 0 : val = *readp++;
9226 : 0 : goto strx_val;
9227 : :
9228 : 0 : case DW_FORM_strx2:
9229 [ # # ]: 0 : if (readendp - readp < 2)
9230 : 0 : goto invalid_data;
9231 [ # # ]: 0 : val = read_2ubyte_unaligned_inc (dbg, readp);
9232 : 0 : goto strx_val;
9233 : :
9234 : 0 : case DW_FORM_strx3:
9235 [ # # ]: 0 : if (readendp - readp < 3)
9236 : 0 : goto invalid_data;
9237 : 0 : val = read_3ubyte_unaligned_inc (dbg, readp);
9238 : 0 : goto strx_val;
9239 : :
9240 : 0 : case DW_FORM_strx4:
9241 [ # # ]: 0 : if (readendp - readp < 4)
9242 : 0 : goto invalid_data;
9243 [ # # ]: 0 : val = read_4ubyte_unaligned_inc (dbg, readp);
9244 : 0 : goto strx_val;
9245 : :
9246 : 0 : default:
9247 : 0 : error (0, 0, _("unknown form: %s"), dwarf_form_name (form));
9248 : 0 : return readendp;
9249 : : }
9250 : :
9251 : 143610 : return readp;
9252 : : }
9253 : :
9254 : : /* Only used via run_advance_pc() macro */
9255 : : static inline void
9256 : 783988 : run_advance_pc (unsigned int op_advance,
9257 : : unsigned int minimum_instr_len,
9258 : : unsigned int max_ops_per_instr,
9259 : : unsigned int *op_addr_advance,
9260 : : Dwarf_Word *address,
9261 : : unsigned int *op_index)
9262 : : {
9263 : 783988 : const unsigned int advanced_op_index = (*op_index) + op_advance;
9264 : :
9265 : 783988 : *op_addr_advance = minimum_instr_len * (advanced_op_index
9266 : 783988 : / max_ops_per_instr);
9267 : 783988 : *address = *address + *op_addr_advance;
9268 : 783988 : *op_index = advanced_op_index % max_ops_per_instr;
9269 : : }
9270 : :
9271 : : static void
9272 : 186 : print_debug_line_section (Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr,
9273 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg,
9274 : : FILE *out)
9275 : : {
9276 [ + + ]: 186 : if (decodedline)
9277 : : {
9278 : 82 : print_decoded_line_section (dwflmod, ebl, ehdr, scn, shdr, dbg, out);
9279 : 164 : return;
9280 : : }
9281 : :
9282 : 104 : Elf_Data *data = get_debug_elf_data (dbg, ebl, IDX_debug_line, scn);
9283 [ + - ]: 104 : if (data == NULL)
9284 : : return;
9285 : :
9286 : 104 : fprintf (out, _("\
9287 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
9288 : : elf_ndxscn (scn), section_name (ebl, shdr),
9289 : 104 : (uint64_t) shdr->sh_offset);
9290 : :
9291 [ + - ]: 104 : if (shdr->sh_size == 0)
9292 : : return;
9293 : :
9294 : : /* There is no functionality in libdw to read the information in the
9295 : : way it is represented here. Hardcode the decoder. */
9296 : :
9297 : 104 : const unsigned char *linep = (const unsigned char *) data->d_buf;
9298 : 104 : const unsigned char *lineendp;
9299 : :
9300 : 104 : while (linep
9301 [ + + ]: 3726 : < (lineendp = (const unsigned char *) data->d_buf + data->d_size))
9302 : : {
9303 : 3622 : size_t start_offset = linep - (const unsigned char *) data->d_buf;
9304 : :
9305 : 3622 : fprintf (out, "\n%s %zu:\n",
9306 : : table_offset_lower_str, start_offset);
9307 : :
9308 [ - + ]: 3622 : if (unlikely (linep + 4 > lineendp))
9309 : 0 : goto invalid_data;
9310 [ + + ]: 3622 : Dwarf_Word unit_length = read_4ubyte_unaligned_inc (dbg, linep);
9311 : 3622 : unsigned int length = 4;
9312 [ - + ]: 3622 : if (unlikely (unit_length == 0xffffffff))
9313 : : {
9314 [ # # ]: 0 : if (unlikely (linep + 8 > lineendp))
9315 : : {
9316 : 0 : invalid_data:
9317 : 0 : error (0, 0, _("invalid data in section [%zu] '%s'"),
9318 : : elf_ndxscn (scn), section_name (ebl, shdr));
9319 : 0 : return;
9320 : : }
9321 [ # # ]: 0 : unit_length = read_8ubyte_unaligned_inc (dbg, linep);
9322 : 0 : length = 8;
9323 : : }
9324 : :
9325 : : /* Check whether we have enough room in the section. */
9326 [ - + ]: 3622 : if (unlikely (unit_length > (size_t) (lineendp - linep)))
9327 : 0 : goto invalid_data;
9328 : 3622 : lineendp = linep + unit_length;
9329 : :
9330 : : /* The next element of the header is the version identifier. */
9331 [ - + ]: 3622 : if ((size_t) (lineendp - linep) < 2)
9332 : 0 : goto invalid_data;
9333 [ + + ]: 3622 : uint_fast16_t version = read_2ubyte_unaligned_inc (dbg, linep);
9334 : :
9335 : 7244 : size_t address_size
9336 [ + + ]: 3622 : = elf_getident (ebl->elf, NULL)[EI_CLASS] == ELFCLASS32 ? 4 : 8;
9337 : 3622 : unsigned char segment_selector_size = 0;
9338 [ + + ]: 3622 : if (version > 4)
9339 : : {
9340 [ - + ]: 3470 : if ((size_t) (lineendp - linep) < 2)
9341 : 0 : goto invalid_data;
9342 : 3470 : address_size = *linep++;
9343 : 3470 : segment_selector_size = *linep++;
9344 : : }
9345 : :
9346 : : /* Next comes the header length. */
9347 : 3622 : Dwarf_Word header_length;
9348 [ + - ]: 3622 : if (length == 4)
9349 : : {
9350 [ - + ]: 3622 : if ((size_t) (lineendp - linep) < 4)
9351 : 0 : goto invalid_data;
9352 [ + + ]: 3622 : header_length = read_4ubyte_unaligned_inc (dbg, linep);
9353 : : }
9354 : : else
9355 : : {
9356 [ # # ]: 0 : if ((size_t) (lineendp - linep) < 8)
9357 : 0 : goto invalid_data;
9358 [ # # ]: 0 : header_length = read_8ubyte_unaligned_inc (dbg, linep);
9359 : : }
9360 : :
9361 : 3622 : const unsigned char *header_start = linep;
9362 : :
9363 : : /* Next the minimum instruction length. */
9364 [ - + ]: 3622 : if ((size_t) (lineendp - linep) < 1)
9365 : 0 : goto invalid_data;
9366 : 3622 : uint_fast8_t minimum_instr_len = *linep++;
9367 : :
9368 : : /* Next the maximum operations per instruction, in version 4 format. */
9369 : 3622 : uint_fast8_t max_ops_per_instr;
9370 [ + + ]: 3622 : if (version < 4)
9371 : : max_ops_per_instr = 1;
9372 : : else
9373 : : {
9374 [ - + ]: 3490 : if ((size_t) (lineendp - linep) < 1)
9375 : 0 : goto invalid_data;
9376 : 3490 : max_ops_per_instr = *linep++;
9377 : : }
9378 : :
9379 : : /* We need at least 4 more bytes. */
9380 [ - + ]: 3622 : if ((size_t) (lineendp - linep) < 4)
9381 : 0 : goto invalid_data;
9382 : :
9383 : : /* Then the flag determining the default value of the is_stmt
9384 : : register. */
9385 : 3622 : uint_fast8_t default_is_stmt = *linep++;
9386 : :
9387 : : /* Now the line base. */
9388 : 3622 : int_fast8_t line_base = *linep++;
9389 : :
9390 : : /* And the line range. */
9391 : 3622 : uint_fast8_t line_range = *linep++;
9392 : :
9393 : : /* The opcode base. */
9394 : 3622 : uint_fast8_t opcode_base = *linep++;
9395 : :
9396 : : /* Print what we got so far. */
9397 : 3622 : fprintf (out, "\n"
9398 : : " %s: %" PRIu64 "\n"
9399 : : " %s: %" PRIuFAST16 "\n"
9400 : : " %s: %" PRIu64 "\n"
9401 : : " %s: %zd\n"
9402 : : " %s: %zd\n"
9403 : : " %s: %" PRIuFAST8 "\n"
9404 : : " %s: %" PRIuFAST8 "\n"
9405 : : " %s 'is_stmt': %" PRIuFAST8 "\n"
9406 : : " %s: %" PRIdFAST8 "\n"
9407 : : " %s: %" PRIuFAST8 "\n"
9408 : : " %s: %" PRIuFAST8 "\n"
9409 : : "\n"
9410 : : "%s:\n",
9411 : : length_str, (uint64_t) unit_length, dwarf_ver_str, version,
9412 : : prologue_len_str, (uint64_t) header_length, addr_size_str,
9413 : : address_size, seg_selector_str, (size_t) segment_selector_size,
9414 : : min_inst_len_str, minimum_instr_len, max_op_per_inst_str,
9415 : : max_ops_per_instr, initial_value_if_str, default_is_stmt,
9416 : : line_base_str, line_base, line_range_str, line_range,
9417 : : opcodes_base_str, opcode_base, opcodes_str);
9418 : :
9419 [ - + ]: 3622 : if (version < 2 || version > 5)
9420 : : {
9421 : 0 : error (0, 0, _("cannot handle .debug_line version: %u\n"),
9422 : : (unsigned int) version);
9423 : 0 : linep = lineendp;
9424 : 0 : continue;
9425 : : }
9426 : :
9427 [ - + ]: 3622 : if (address_size != 4 && address_size != 8)
9428 : : {
9429 : 0 : error (0, 0, _("cannot handle address size: %u\n"),
9430 : : (unsigned int) address_size);
9431 : 0 : linep = lineendp;
9432 : 0 : continue;
9433 : : }
9434 : :
9435 [ - + ]: 3622 : if (segment_selector_size != 0)
9436 : : {
9437 : 0 : error (0, 0, _("cannot handle segment selector size: %u\n"),
9438 : : (unsigned int) segment_selector_size);
9439 : 0 : linep = lineendp;
9440 : 0 : continue;
9441 : : }
9442 : :
9443 [ - + ]: 3622 : if (unlikely (linep + opcode_base - 1 >= lineendp))
9444 : : {
9445 : 0 : invalid_unit:
9446 : 0 : error (0, 0,
9447 : 0 : _("invalid data at offset %tu in section [%zu] '%s'"),
9448 : 0 : linep - (const unsigned char *) data->d_buf,
9449 : : elf_ndxscn (scn), section_name (ebl, shdr));
9450 : 0 : linep = lineendp;
9451 : 0 : continue;
9452 : : }
9453 : 3622 : int opcode_base_l10 = 1;
9454 : 3622 : unsigned int tmp = opcode_base;
9455 [ + + ]: 7238 : while (tmp > 10)
9456 : : {
9457 : 3616 : tmp /= 10;
9458 : 3616 : ++opcode_base_l10;
9459 : : }
9460 : : const uint8_t *standard_opcode_lengths = linep - 1;
9461 [ + + ]: 47068 : for (uint_fast8_t cnt = 1; cnt < opcode_base; ++cnt)
9462 : 43446 : fprintf (out, ngettext (" [%*" PRIuFAST8 "] %hhu argument\n",
9463 : : " [%*" PRIuFAST8 "] %hhu arguments\n",
9464 : : (int) linep[cnt - 1]),
9465 : 43446 : opcode_base_l10, cnt, linep[cnt - 1]);
9466 : 3622 : linep += opcode_base - 1;
9467 : :
9468 : 3622 : if (unlikely (linep >= lineendp))
9469 : : goto invalid_unit;
9470 : :
9471 : 3622 : Dwarf_Off str_offsets_base = str_offsets_base_off (dbg, NULL);
9472 : :
9473 : 3622 : fprintf (out, "\n%s:\n", dir_table_str);
9474 [ + + ]: 3622 : if (version > 4)
9475 : : {
9476 : 3470 : struct encpair { uint16_t desc; uint16_t form; };
9477 : 3470 : struct encpair enc[256];
9478 : :
9479 : 3470 : fprintf (out, " [");
9480 : 3470 : if ((size_t) (lineendp - linep) < 1)
9481 : 0 : goto invalid_data;
9482 : 3470 : unsigned char directory_entry_format_count = *linep++;
9483 [ + + ]: 6940 : for (int i = 0; i < directory_entry_format_count; i++)
9484 : : {
9485 : 3470 : uint16_t desc, form;
9486 [ - + ]: 3470 : if ((size_t) (lineendp - linep) < 1)
9487 : 0 : goto invalid_data;
9488 : 3470 : get_uleb128 (desc, linep, lineendp);
9489 [ - + ]: 3470 : if ((size_t) (lineendp - linep) < 1)
9490 : 0 : goto invalid_data;
9491 : 3470 : get_uleb128 (form, linep, lineendp);
9492 : :
9493 : 3470 : enc[i].desc = desc;
9494 : 3470 : enc[i].form = form;
9495 : :
9496 : 3470 : fprintf (out, "%s(%s)",
9497 : : dwarf_line_content_description_name (desc),
9498 : : dwarf_form_name (form));
9499 [ - + ]: 3470 : if (i + 1 < directory_entry_format_count)
9500 : 0 : fprintf (out, ", ");
9501 : : }
9502 : 3470 : fprintf (out, "]\n");
9503 : :
9504 : 3470 : uint64_t directories_count;
9505 [ - + ]: 3470 : if ((size_t) (lineendp - linep) < 1)
9506 : 0 : goto invalid_data;
9507 : 3470 : get_uleb128 (directories_count, linep, lineendp);
9508 : :
9509 : 3470 : if (directory_entry_format_count == 0
9510 [ - + ]: 3470 : && directories_count != 0)
9511 : 0 : goto invalid_data;
9512 : :
9513 [ + + ]: 29784 : for (uint64_t i = 0; i < directories_count; i++)
9514 : : {
9515 : 26314 : fprintf (out, " %-5" PRIu64 " ", i);
9516 [ + + ]: 52628 : for (int j = 0; j < directory_entry_format_count; j++)
9517 : : {
9518 : 26314 : linep = print_form_data (dbg, enc[j].form,
9519 : : linep, lineendp, length,
9520 : : str_offsets_base, out);
9521 [ - + ]: 26314 : if (j + 1 < directory_entry_format_count)
9522 : 0 : fprintf (out, ", ");
9523 : : }
9524 : 26314 : fprintf (out, "\n");
9525 [ - + ]: 26314 : if (linep >= lineendp)
9526 : 0 : goto invalid_unit;
9527 : : }
9528 : : }
9529 : : else
9530 : : {
9531 [ + - + + ]: 766 : while (linep < lineendp && *linep != 0)
9532 : : {
9533 : 614 : unsigned char *endp = memchr (linep, '\0', lineendp - linep);
9534 [ - + ]: 614 : if (unlikely (endp == NULL))
9535 : 0 : goto invalid_unit;
9536 : :
9537 : 614 : fprintf (out, " %s\n", (char *) linep);
9538 : :
9539 : 614 : linep = endp + 1;
9540 : : }
9541 [ + - - + ]: 152 : if (linep >= lineendp || *linep != 0)
9542 : 0 : goto invalid_unit;
9543 : : /* Skip the final NUL byte. */
9544 : 152 : ++linep;
9545 : : }
9546 : :
9547 [ - + ]: 3622 : if (unlikely (linep >= lineendp))
9548 : 0 : goto invalid_unit;
9549 : :
9550 : 3622 : fprintf (out, "\n%s:\n", file_table_str);
9551 [ + + ]: 3622 : if (version > 4)
9552 : : {
9553 : 3470 : struct encpair { uint16_t desc; uint16_t form; };
9554 : 3470 : struct encpair enc[256];
9555 : :
9556 : 3470 : fprintf (out, " [");
9557 : 3470 : if ((size_t) (lineendp - linep) < 1)
9558 : 0 : goto invalid_data;
9559 : 3470 : unsigned char file_name_format_count = *linep++;
9560 [ + + ]: 10418 : for (int i = 0; i < file_name_format_count; i++)
9561 : : {
9562 : 6948 : uint64_t desc, form;
9563 [ - + ]: 6948 : if ((size_t) (lineendp - linep) < 1)
9564 : 0 : goto invalid_data;
9565 : 6948 : get_uleb128 (desc, linep, lineendp);
9566 [ - + ]: 6948 : if ((size_t) (lineendp - linep) < 1)
9567 : 0 : goto invalid_data;
9568 : 6948 : get_uleb128 (form, linep, lineendp);
9569 : :
9570 [ - + ]: 6948 : if (! libdw_valid_user_form (form))
9571 : 0 : goto invalid_data;
9572 : :
9573 : 6948 : enc[i].desc = desc;
9574 : 6948 : enc[i].form = form;
9575 : :
9576 : 6948 : fprintf (out, "%s(%s)",
9577 : : dwarf_line_content_description_name (desc),
9578 : : dwarf_form_name (form));
9579 [ + + ]: 6948 : if (i + 1 < file_name_format_count)
9580 : 3478 : fprintf (out, ", ");
9581 : : }
9582 : 3470 : fprintf (out, "]\n");
9583 : :
9584 : 3470 : uint64_t file_name_count;
9585 [ - + ]: 3470 : if ((size_t) (lineendp - linep) < 1)
9586 : 0 : goto invalid_data;
9587 : 3470 : get_uleb128 (file_name_count, linep, lineendp);
9588 : :
9589 : 3470 : if (file_name_format_count == 0
9590 [ - + ]: 3470 : && file_name_count != 0)
9591 : 0 : goto invalid_data;
9592 : :
9593 [ + + ]: 61328 : for (uint64_t i = 0; i < file_name_count; i++)
9594 : : {
9595 : 57858 : fprintf (out, " %-5" PRIu64 " ", i);
9596 [ + + ]: 173598 : for (int j = 0; j < file_name_format_count; j++)
9597 : : {
9598 : 115740 : linep = print_form_data (dbg, enc[j].form,
9599 : : linep, lineendp, length,
9600 : : str_offsets_base, out);
9601 [ + + ]: 115740 : if (j + 1 < file_name_format_count)
9602 : 57882 : fprintf (out, ", ");
9603 : : }
9604 : 57858 : fprintf (out, "\n");
9605 [ - + ]: 57858 : if (linep > lineendp)
9606 : 0 : goto invalid_unit;
9607 : : }
9608 : : }
9609 : : else
9610 : : {
9611 : 152 : fputs (_(" Entry Dir Time Size Name\n"), out);
9612 [ + - + + ]: 6154 : for (unsigned int cnt = 1; linep < lineendp && *linep != 0; ++cnt)
9613 : : {
9614 : : /* First comes the file name. */
9615 : 6002 : char *fname = (char *) linep;
9616 : 6002 : unsigned char *endp = memchr (fname, '\0', lineendp - linep);
9617 [ - + ]: 6002 : if (unlikely (endp == NULL))
9618 : 0 : goto invalid_unit;
9619 : 6002 : linep = endp + 1;
9620 : :
9621 : : /* Then the index. */
9622 : 6002 : unsigned int diridx;
9623 [ - + ]: 6002 : if (lineendp - linep < 1)
9624 : 0 : goto invalid_unit;
9625 : 6002 : get_uleb128 (diridx, linep, lineendp);
9626 : :
9627 : : /* Next comes the modification time. */
9628 : 6002 : unsigned int mtime;
9629 [ - + ]: 6002 : if (lineendp - linep < 1)
9630 : 0 : goto invalid_unit;
9631 : 6002 : get_uleb128 (mtime, linep, lineendp);
9632 : :
9633 : : /* Finally the length of the file. */
9634 : 6002 : unsigned int fsize;
9635 [ - + ]: 6002 : if (lineendp - linep < 1)
9636 : 0 : goto invalid_unit;
9637 : 6002 : get_uleb128 (fsize, linep, lineendp);
9638 : :
9639 : 6002 : fprintf (out, " %-5u %-5u %-9u %-9u %s\n",
9640 : : cnt, diridx, mtime, fsize, fname);
9641 : : }
9642 [ + - - + ]: 152 : if (linep >= lineendp || *linep != '\0')
9643 : 0 : goto invalid_unit;
9644 : : /* Skip the final NUL byte. */
9645 : 152 : ++linep;
9646 : : }
9647 : :
9648 : 3622 : unsigned int debug_str_offset = 0;
9649 [ + + ]: 3622 : if (unlikely (linep == header_start + header_length - 4))
9650 : : {
9651 : : /* CUBINs contain an unsigned 4-byte offset */
9652 [ - + ]: 2 : debug_str_offset = read_4ubyte_unaligned_inc (dbg, linep);
9653 : : }
9654 : :
9655 [ + + ]: 3622 : if (linep == lineendp)
9656 : : {
9657 : 40 : fprintf (out, "\n%s.\n", no_line_num_stmts_str);
9658 : 40 : continue;
9659 : : }
9660 : :
9661 : 3582 : fprintf (out, "\n%s:\n", line_num_stmts_str);
9662 : 3582 : Dwarf_Word address = 0;
9663 : 3582 : unsigned int op_index = 0;
9664 : 3582 : size_t line = 1;
9665 : 3582 : uint_fast8_t is_stmt = default_is_stmt;
9666 : :
9667 : : /* Apply the "operation advance" from a special opcode
9668 : : or DW_LNS_advance_pc (as per DWARF4 6.2.5.1). */
9669 : 3582 : unsigned int op_addr_advance;
9670 : : #define advance_pc(op_advance) run_advance_pc(op_advance, minimum_instr_len, \
9671 : : max_ops_per_instr, &op_addr_advance, &address, &op_index)
9672 : :
9673 [ - + ]: 3582 : if (max_ops_per_instr == 0)
9674 : : {
9675 : 0 : error (0, 0,
9676 : 0 : _("invalid maximum operations per instruction is zero"));
9677 : 0 : linep = lineendp;
9678 : 0 : continue;
9679 : : }
9680 : :
9681 [ + + ]: 2523098 : while (linep < lineendp)
9682 : : {
9683 : 2519516 : size_t offset = linep - (const unsigned char *) data->d_buf;
9684 : 2519516 : unsigned int u128;
9685 : 2519516 : int s128;
9686 : :
9687 : : /* Read the opcode. */
9688 : 2519516 : unsigned int opcode = *linep++;
9689 : :
9690 : 2519516 : fprintf (out, " [%6" PRIx64 "]", (uint64_t)offset);
9691 : : /* Is this a special opcode? */
9692 [ + + ]: 2519516 : if (likely (opcode >= opcode_base))
9693 : : {
9694 [ - + ]: 715014 : if (unlikely (line_range == 0))
9695 : 0 : goto invalid_unit;
9696 : :
9697 : : /* Yes. Handling this is quite easy since the opcode value
9698 : : is computed with
9699 : :
9700 : : opcode = (desired line increment - line_base)
9701 : : + (line_range * address advance) + opcode_base
9702 : : */
9703 : 715014 : int line_increment = (line_base
9704 : 715014 : + (opcode - opcode_base) % line_range);
9705 : :
9706 : : /* Perform the increments. */
9707 : 715014 : line += line_increment;
9708 : 715014 : advance_pc ((opcode - opcode_base) / line_range);
9709 : :
9710 : 715014 : fprintf (out, " %s %u: %s+%u = ",
9711 : : special_opcode_str, opcode, address_str, op_addr_advance);
9712 : 715014 : print_dwarf_addr (dwflmod, 0, address, address, out);
9713 [ - + ]: 715014 : if (op_index > 0)
9714 : 0 : fprintf (out, ", op_index = %u, %s%+d = %zu\n",
9715 : : op_index, line_str, line_increment, line);
9716 : : else
9717 : 715014 : fprintf (out, ", %s%+d = %zu\n",
9718 : : line_str, line_increment, line);
9719 : : }
9720 [ + + ]: 1804502 : else if (opcode == 0)
9721 : : {
9722 : : /* This an extended opcode. */
9723 [ - + ]: 190684 : if (unlikely (linep + 2 > lineendp))
9724 : 0 : goto invalid_unit;
9725 : :
9726 : : /* The length. */
9727 : 190684 : unsigned int len = *linep++;
9728 : :
9729 [ - + ]: 190684 : if (unlikely (linep + len > lineendp))
9730 : 0 : goto invalid_unit;
9731 : :
9732 : : /* The sub-opcode. */
9733 : 190684 : opcode = *linep++;
9734 : :
9735 : 190684 : fprintf (out, " %s %u: ", ext_opcode_str, opcode);
9736 : :
9737 [ + + - + : 190684 : switch (opcode)
+ - - ]
9738 : : {
9739 : 10608 : case DW_LNE_end_sequence:
9740 : 10608 : fprintf (out, " %s\n", end_of_seq_str);
9741 : :
9742 : : /* Reset the registers we care about. */
9743 : 10608 : address = 0;
9744 : 10608 : op_index = 0;
9745 : 10608 : line = 1;
9746 : 10608 : is_stmt = default_is_stmt;
9747 : 10608 : break;
9748 : :
9749 : 12372 : case DW_LNE_set_address:
9750 : 12372 : op_index = 0;
9751 [ - + ]: 12372 : if (unlikely ((size_t) (lineendp - linep) < address_size))
9752 : 0 : goto invalid_unit;
9753 [ + + ]: 12372 : if (address_size == 4)
9754 [ + + ]: 50 : address = read_4ubyte_unaligned_inc (dbg, linep);
9755 : : else
9756 [ + + ]: 12322 : address = read_8ubyte_unaligned_inc (dbg, linep);
9757 : : {
9758 : 12372 : fprintf (out, " %s ", set_addr_to_str);
9759 : 12372 : print_dwarf_addr (dwflmod, 0, address, address, out);
9760 : 12372 : fprintf (out, "\n");
9761 : : }
9762 : 12372 : break;
9763 : :
9764 : 0 : case DW_LNE_define_file:
9765 : : {
9766 : 0 : char *fname = (char *) linep;
9767 : 0 : unsigned char *endp = memchr (linep, '\0',
9768 : 0 : lineendp - linep);
9769 [ # # ]: 0 : if (unlikely (endp == NULL))
9770 : 0 : goto invalid_unit;
9771 : 0 : linep = endp + 1;
9772 : :
9773 : 0 : unsigned int diridx;
9774 [ # # ]: 0 : if (lineendp - linep < 1)
9775 : 0 : goto invalid_unit;
9776 : 0 : get_uleb128 (diridx, linep, lineendp);
9777 : 0 : Dwarf_Word mtime;
9778 [ # # ]: 0 : if (lineendp - linep < 1)
9779 : 0 : goto invalid_unit;
9780 : 0 : get_uleb128 (mtime, linep, lineendp);
9781 : 0 : Dwarf_Word filelength;
9782 [ # # ]: 0 : if (lineendp - linep < 1)
9783 : 0 : goto invalid_unit;
9784 : 0 : get_uleb128 (filelength, linep, lineendp);
9785 : :
9786 : 0 : fprintf (out, _("\
9787 : : define new file: dir=%u, mtime=%" PRIu64 ", length=%" PRIu64 ", name=%s\n"),
9788 : : diridx, (uint64_t) mtime, (uint64_t) filelength,
9789 : : fname);
9790 : : }
9791 : 0 : break;
9792 : :
9793 : 167690 : case DW_LNE_set_discriminator:
9794 : : /* Takes one ULEB128 parameter, the discriminator. */
9795 [ + - - + ]: 167690 : if (unlikely (standard_opcode_lengths[opcode] != 1
9796 : : || lineendp - linep < 1))
9797 : 0 : goto invalid_unit;
9798 : :
9799 : 167690 : get_uleb128 (u128, linep, lineendp);
9800 : 167690 : fprintf (out, " %s %u\n", set_disc_str, u128);
9801 : 167690 : break;
9802 : :
9803 : 14 : case DW_LNE_NVIDIA_inlined_call:
9804 : : {
9805 [ - + ]: 14 : if (unlikely (linep >= lineendp))
9806 : 0 : goto invalid_data;
9807 : :
9808 : 14 : unsigned int context;
9809 : 14 : get_uleb128 (context, linep, lineendp);
9810 : :
9811 [ - + ]: 14 : if (unlikely (linep >= lineendp))
9812 : 0 : goto invalid_data;
9813 : :
9814 : 14 : unsigned int function_name;
9815 : 14 : get_uleb128 (function_name, linep, lineendp);
9816 : 14 : function_name += debug_str_offset;
9817 : :
9818 : 14 : Elf_Data *str_data = dbg->sectiondata[IDX_debug_str];
9819 : 14 : char *function_str;
9820 [ + - - + ]: 14 : if (str_data == NULL || function_name >= str_data->d_size
9821 [ - + ]: 14 : || memchr (str_data->d_buf + function_name, '\0',
9822 : : str_data->d_size - function_name) == NULL)
9823 : : function_str = "???";
9824 : : else
9825 : 14 : function_str = (char *) str_data->d_buf + function_name;
9826 : :
9827 : 14 : fprintf (out, _(" set inlined context %u,"
9828 : : " function name %s (0x%x)\n"),
9829 : : context, function_str, function_name);
9830 : 14 : break;
9831 : : }
9832 : :
9833 : 0 : case DW_LNE_NVIDIA_set_function_name:
9834 : : {
9835 [ # # ]: 0 : if (unlikely (linep >= lineendp))
9836 : 0 : goto invalid_data;
9837 : :
9838 : 0 : unsigned int function_name;
9839 : 0 : get_uleb128 (function_name, linep, lineendp);
9840 : 0 : function_name += debug_str_offset;
9841 : :
9842 : 0 : Elf_Data *str_data = dbg->sectiondata[IDX_debug_str];
9843 : 0 : char *function_str;
9844 [ # # # # ]: 0 : if (str_data == NULL || function_name >= str_data->d_size
9845 [ # # ]: 0 : || memchr (str_data->d_buf + function_name, '\0',
9846 : : str_data->d_size - function_name) == NULL)
9847 : : function_str = "???";
9848 : : else
9849 : 0 : function_str = (char *) str_data->d_buf + function_name;
9850 : :
9851 : 0 : fprintf (out, _(" set function name %s (0x%x)\n"),
9852 : : function_str, function_name);
9853 : : }
9854 : 0 : break;
9855 : :
9856 : 0 : default:
9857 : : /* Unknown, ignore it. */
9858 : 0 : fputs (_(" unknown opcode\n"), out);
9859 : 0 : linep += len - 1;
9860 : 0 : break;
9861 : : }
9862 : : }
9863 [ + - ]: 1613818 : else if (opcode <= DW_LNS_set_isa)
9864 : : {
9865 : : /* This is a known standard opcode. */
9866 [ + + + + : 1613818 : switch (opcode)
+ + - + +
+ - - ]
9867 : : {
9868 : 251538 : case DW_LNS_copy:
9869 : : /* Takes no argument. */
9870 : 251538 : fprintf (out, " %s\n", copy_str);
9871 : 251538 : break;
9872 : :
9873 : 15378 : case DW_LNS_advance_pc:
9874 : : /* Takes one uleb128 parameter which is added to the
9875 : : address. */
9876 [ - + ]: 15378 : if (lineendp - linep < 1)
9877 : 0 : goto invalid_unit;
9878 : 15378 : get_uleb128 (u128, linep, lineendp);
9879 : 15378 : advance_pc (u128);
9880 : : {
9881 : 15378 : fprintf (out, " %s %u %s ",
9882 : : adv_addr_str, op_addr_advance, to_str);
9883 : 15378 : print_dwarf_addr (dwflmod, 0, address, address, out);
9884 [ - + ]: 15378 : if (op_index > 0)
9885 : 0 : fprintf (out, ", op_index %s %u", to_str, op_index);
9886 : 15378 : fprintf (out, "\n");
9887 : : }
9888 : 15378 : break;
9889 : :
9890 : 186190 : case DW_LNS_advance_line:
9891 : : /* Takes one sleb128 parameter which is added to the
9892 : : line. */
9893 [ - + ]: 186190 : if (lineendp - linep < 1)
9894 : 0 : goto invalid_unit;
9895 : 186190 : get_sleb128 (s128, linep, lineendp);
9896 : 186190 : line += s128;
9897 : 186190 : fprintf (out, "\
9898 : : %s %d to %" PRId64 "\n",
9899 : : adv_line_by_str, s128, (int64_t) line);
9900 : 186190 : break;
9901 : :
9902 : 64988 : case DW_LNS_set_file:
9903 : : /* Takes one uleb128 parameter which is stored in file. */
9904 [ - + ]: 64988 : if (lineendp - linep < 1)
9905 : 0 : goto invalid_unit;
9906 : 64988 : get_uleb128 (u128, linep, lineendp);
9907 : 64988 : fprintf (out, " %s %" PRIu64 "\n",
9908 : : set_file_str, (uint64_t) u128);
9909 : 64988 : break;
9910 : :
9911 : 610056 : case DW_LNS_set_column:
9912 : : /* Takes one uleb128 parameter which is stored in column. */
9913 [ + - - + ]: 610056 : if (unlikely (standard_opcode_lengths[opcode] != 1
9914 : : || lineendp - linep < 1))
9915 : 0 : goto invalid_unit;
9916 : :
9917 : 610056 : get_uleb128 (u128, linep, lineendp);
9918 : 610056 : fprintf (out, " %s %" PRIu64 "\n",
9919 : : set_col_str, (uint64_t) u128);
9920 : 610056 : break;
9921 : :
9922 : 432018 : case DW_LNS_negate_stmt:
9923 : : /* Takes no argument. */
9924 : 432018 : is_stmt = 1 - is_stmt;
9925 : 432018 : fprintf (out, " %s '%s' %s %" PRIuFAST8 "\n",
9926 : : set_str, "is_stmt", to_str, is_stmt);
9927 : 432018 : break;
9928 : :
9929 : 0 : case DW_LNS_set_basic_block:
9930 : : /* Takes no argument. */
9931 : 0 : fputs (_(" set basic block flag\n"), out);
9932 : 0 : break;
9933 : :
9934 : 53596 : case DW_LNS_const_add_pc:
9935 : : /* Takes no argument. */
9936 : :
9937 [ - + ]: 53596 : if (unlikely (line_range == 0))
9938 : 0 : goto invalid_unit;
9939 : :
9940 : 53596 : advance_pc ((255 - opcode_base) / line_range);
9941 : : {
9942 : 53596 : fprintf (out, " %s %u to ",
9943 : : adv_addr_by_str, op_addr_advance);
9944 : 53596 : print_dwarf_addr (dwflmod, 0, address, address, out);
9945 [ - + ]: 53596 : if (op_index > 0)
9946 : 0 : fprintf (out, _(", op_index to %u"), op_index);
9947 : 53596 : fprintf (out, "\n");
9948 : : }
9949 : 53596 : break;
9950 : :
9951 : 44 : case DW_LNS_fixed_advance_pc:
9952 : : /* Takes one 16 bit parameter which is added to the
9953 : : address. */
9954 [ + - - + ]: 44 : if (unlikely (standard_opcode_lengths[opcode] != 1
9955 : : || lineendp - linep < 2))
9956 : 0 : goto invalid_unit;
9957 : :
9958 [ - + ]: 44 : u128 = read_2ubyte_unaligned_inc (dbg, linep);
9959 : 44 : address += u128;
9960 : 44 : op_index = 0;
9961 : : {
9962 : 44 : fprintf (out, _("\
9963 : : advance address by fixed value %u to \n"),
9964 : : u128);
9965 : 44 : print_dwarf_addr (dwflmod, 0, address, address, out);
9966 : 44 : fprintf (out, "\n");
9967 : : }
9968 : 44 : break;
9969 : :
9970 : 10 : case DW_LNS_set_prologue_end:
9971 : : /* Takes no argument. */
9972 : 10 : fputs (_(" set prologue end flag\n"), out);
9973 : 10 : break;
9974 : :
9975 : 0 : case DW_LNS_set_epilogue_begin:
9976 : : /* Takes no argument. */
9977 : 0 : fputs (_(" set epilogue begin flag\n"), out);
9978 : 0 : break;
9979 : :
9980 : 0 : case DW_LNS_set_isa:
9981 : : /* Takes one uleb128 parameter which is stored in isa. */
9982 [ # # # # ]: 0 : if (unlikely (standard_opcode_lengths[opcode] != 1
9983 : : || lineendp - linep < 1))
9984 : 0 : goto invalid_unit;
9985 : :
9986 : 0 : get_uleb128 (u128, linep, lineendp);
9987 : 0 : fprintf (out, _(" set isa to %u\n"), u128);
9988 : 0 : break;
9989 : : }
9990 : : }
9991 : : else
9992 : : {
9993 : : /* This is a new opcode the generator but not we know about.
9994 : : Read the parameters associated with it but then discard
9995 : : everything. Read all the parameters for this opcode. */
9996 : 0 : fprintf (out,
9997 : 0 : ngettext (" unknown opcode with %" PRIu8 " parameter:",
9998 : : " unknown opcode with %" PRIu8 " parameters:",
9999 : : standard_opcode_lengths[opcode]),
10000 : 0 : standard_opcode_lengths[opcode]);
10001 : 0 : for (int n = standard_opcode_lengths[opcode];
10002 [ # # # # ]: 0 : n > 0 && linep < lineendp; --n)
10003 : : {
10004 : 0 : get_uleb128 (u128, linep, lineendp);
10005 [ # # ]: 0 : if (n != standard_opcode_lengths[opcode])
10006 : 0 : fputc (',', out);
10007 : 0 : fprintf (out, " %u", u128);
10008 : : }
10009 : :
10010 : : /* Next round, ignore this opcode. */
10011 : 0 : continue;
10012 : : }
10013 : : }
10014 : : }
10015 : :
10016 : : /* There must only be one data block. */
10017 [ - + ]: 104 : assert (elf_getdata (scn, data) == NULL);
10018 : : }
10019 : :
10020 : :
10021 : : static void
10022 : 40 : print_debug_loclists_section (Dwfl_Module *dwflmod,
10023 : : Ebl *ebl,
10024 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
10025 : : Elf_Scn *scn, GElf_Shdr *shdr,
10026 : : Dwarf *dbg, FILE *out)
10027 : : {
10028 : 40 : Elf_Data *data = get_debug_elf_data (dbg, ebl, IDX_debug_loclists, scn);
10029 [ + - ]: 40 : if (data == NULL)
10030 : 0 : return;
10031 : :
10032 : 40 : fprintf (out, _("\
10033 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
10034 : : elf_ndxscn (scn), section_name (ebl, shdr),
10035 : 40 : (uint64_t) shdr->sh_offset);
10036 : :
10037 : : /* For the listptr to get the base address/CU. */
10038 : 40 : sort_listptr (&known_loclistsptr, "loclistsptr");
10039 : 40 : size_t listptr_idx = 0;
10040 : :
10041 : 40 : const unsigned char *readp = data->d_buf;
10042 : 40 : const unsigned char *const dataend = ((unsigned char *) data->d_buf
10043 : 40 : + data->d_size);
10044 [ + + ]: 3132 : while (readp < dataend)
10045 : : {
10046 [ - + ]: 3092 : if (unlikely (readp > dataend - 4))
10047 : : {
10048 : 0 : invalid_data:
10049 : 0 : error (0, 0, _("invalid data in section [%zu] '%s'"),
10050 : : elf_ndxscn (scn), section_name (ebl, shdr));
10051 : 0 : return;
10052 : : }
10053 : :
10054 : 3092 : ptrdiff_t offset = readp - (unsigned char *) data->d_buf;
10055 : 3092 : fprintf (out, "%s 0x%" PRIx64 ":\n\n",
10056 : : table_offset_upper_str, (uint64_t) offset);
10057 : :
10058 [ - + ]: 3092 : uint64_t unit_length = read_4ubyte_unaligned_inc (dbg, readp);
10059 : 3092 : unsigned int offset_size = 4;
10060 [ - + ]: 3092 : if (unlikely (unit_length == 0xffffffff))
10061 : : {
10062 [ # # ]: 0 : if (unlikely (readp > dataend - 8))
10063 : 0 : goto invalid_data;
10064 : :
10065 [ # # ]: 0 : unit_length = read_8ubyte_unaligned_inc (dbg, readp);
10066 : 0 : offset_size = 8;
10067 : : }
10068 : 3092 : fprintf (out, " %s: %8" PRIu64 "\n", length_str, unit_length);
10069 : :
10070 : : /* We need at least 2-bytes + 1-byte + 1-byte + 4-bytes = 8
10071 : : bytes to complete the header. And this unit cannot go beyond
10072 : : the section data. */
10073 [ + - ]: 3092 : if (readp > dataend - 8
10074 [ + - ]: 3092 : || unit_length < 8
10075 [ - + ]: 3092 : || unit_length > (uint64_t) (dataend - readp))
10076 : 0 : goto invalid_data;
10077 : :
10078 : 3092 : const unsigned char *nexthdr = readp + unit_length;
10079 : :
10080 [ - + ]: 3092 : uint16_t version = read_2ubyte_unaligned_inc (dbg, readp);
10081 : 3092 : fprintf (out, " %s: %8" PRIu16 "\n", dwarf_ver_str, version);
10082 : :
10083 [ - + ]: 3092 : if (version != 5)
10084 : : {
10085 : 0 : error (0, 0, _("Unknown version"));
10086 : 0 : goto next_table;
10087 : : }
10088 : :
10089 : 3092 : uint8_t address_size = *readp++;
10090 : 3092 : fprintf (out, " %s: %8" PRIu64 "\n",
10091 : : addr_size_str, (uint64_t) address_size);
10092 : :
10093 [ - + ]: 3092 : if (address_size != 4 && address_size != 8)
10094 : : {
10095 : 0 : error (0, 0, _("unsupported address size"));
10096 : 0 : goto next_table;
10097 : : }
10098 : :
10099 : 3092 : uint8_t segment_size = *readp++;
10100 : 3092 : fprintf (out, " %s: %8" PRIu64 "\n",
10101 : : seg_size_str, (uint64_t) segment_size);
10102 : :
10103 [ - + ]: 3092 : if (segment_size != 0)
10104 : : {
10105 : 0 : error (0, 0, _("unsupported segment size"));
10106 : 0 : goto next_table;
10107 : : }
10108 : :
10109 [ - + ]: 3092 : uint32_t offset_entry_count = read_4ubyte_unaligned_inc (dbg, readp);
10110 : 3092 : fprintf (out, " %s: %8" PRIu64 "\n",
10111 : : offset_ent_str, (uint64_t) offset_entry_count);
10112 : :
10113 : : /* We need the CU that uses this unit to get the initial base address. */
10114 : 3092 : Dwarf_Addr cu_base = 0;
10115 : 3092 : struct Dwarf_CU *cu = NULL;
10116 [ - + ]: 3092 : if (listptr_cu (&known_loclistsptr, &listptr_idx,
10117 : : (Dwarf_Off) offset,
10118 : 3092 : (Dwarf_Off) (nexthdr - (unsigned char *) data->d_buf),
10119 : : &cu_base, &cu)
10120 [ # # ]: 0 : || split_dwarf_cu_base (dbg, &cu, &cu_base))
10121 : 3092 : {
10122 : 3092 : Dwarf_Die cudie;
10123 [ - + ]: 3092 : if (dwarf_cu_die (cu, &cudie,
10124 : : NULL, NULL, NULL, NULL,
10125 : : NULL, NULL) == NULL)
10126 : 0 : fprintf (out, " %s: ", unknown_base_str);
10127 : : else
10128 : 3092 : fprintf (out, " %s [%6" PRIx64 "] %s: ",
10129 : : cu_str, dwarf_dieoffset (&cudie), base_str);
10130 : 3092 : print_dwarf_addr (dwflmod, address_size, cu_base, cu_base, out);
10131 : 3092 : fprintf (out, "\n");
10132 : : }
10133 : : else
10134 : 0 : fprintf (out, _(" Not associated with a CU.\n"));
10135 : :
10136 : 3092 : fprintf (out, "\n");
10137 : :
10138 : 3092 : const unsigned char *offset_array_start = readp;
10139 [ + + ]: 3092 : if (offset_entry_count > 0)
10140 : : {
10141 : 4 : uint64_t max_entries = (unit_length - 8) / offset_size;
10142 [ - + ]: 4 : if (offset_entry_count > max_entries)
10143 : : {
10144 : 0 : error (0, 0,
10145 : 0 : _("too many offset entries for unit length"));
10146 : 0 : offset_entry_count = max_entries;
10147 : : }
10148 : :
10149 : 4 : fprintf (out, _(" Offsets starting at 0x%" PRIx64 ":\n"),
10150 : : (uint64_t) (offset_array_start
10151 : 4 : - (unsigned char *) data->d_buf));
10152 [ + + ]: 36 : for (uint32_t idx = 0; idx < offset_entry_count; idx++)
10153 : : {
10154 : 32 : fprintf (out, " [%6" PRIu32 "] ", idx);
10155 [ + - ]: 32 : if (offset_size == 4)
10156 : : {
10157 [ - + ]: 32 : uint32_t off = read_4ubyte_unaligned_inc (dbg, readp);
10158 : 32 : fprintf (out, "0x%" PRIx32 "\n", off);
10159 : : }
10160 : : else
10161 : : {
10162 [ # # ]: 0 : uint64_t off = read_8ubyte_unaligned_inc (dbg, readp);
10163 : 0 : fprintf (out, "0x%" PRIx64 "\n", off);
10164 : : }
10165 : : }
10166 : 4 : fprintf (out, "\n");
10167 : : }
10168 : :
10169 : 3092 : Dwarf_Addr base = cu_base;
10170 : 3092 : bool start_of_list = true;
10171 [ + + ]: 766816 : while (readp < nexthdr)
10172 : : {
10173 : 763724 : Dwarf_Off off = (Dwarf_Off) (readp - (unsigned char *) data->d_buf);
10174 [ + + ]: 763724 : if (listptr_attr (&known_loclistsptr, listptr_idx, off,
10175 : : DW_AT_GNU_locviews))
10176 : 109748 : {
10177 : 109748 : Dwarf_Off next_off = next_listptr_offset (&known_loclistsptr,
10178 : : &listptr_idx, off);
10179 : 109748 : const unsigned char *locp = readp;
10180 : 109748 : const unsigned char *locendp;
10181 [ + - ]: 109748 : if (next_off == 0
10182 [ + - ]: 109748 : || next_off > (size_t) (nexthdr - ((const unsigned char *)
10183 : : data->d_buf)))
10184 : : locendp = nexthdr;
10185 : : else
10186 : 109748 : locendp = (const unsigned char *) data->d_buf + next_off;
10187 : :
10188 : 109748 : fprintf (out, " Offset: %" PRIx64 ", Index: %" PRIx64 "\n",
10189 : : (uint64_t) (readp - (unsigned char *) data->d_buf),
10190 : 109748 : (uint64_t) (readp - offset_array_start));
10191 : :
10192 [ + + ]: 608190 : while (locp < locendp)
10193 : : {
10194 : 498442 : uint64_t v1, v2;
10195 : 498442 : get_uleb128 (v1, locp, locendp);
10196 [ - + ]: 498442 : if (locp >= locendp)
10197 : : {
10198 : 0 : fprintf (out, _(" <INVALID DATA>\n"));
10199 : 0 : break;
10200 : : }
10201 : 498442 : get_uleb128 (v2, locp, locendp);
10202 : 498442 : fprintf (out,
10203 : : " view pair %" PRId64 ", %" PRId64 "\n", v1, v2);
10204 : : }
10205 : :
10206 : 109748 : fprintf (out, "\n");
10207 : 109748 : readp = (unsigned char *) locendp;
10208 : 109748 : continue;
10209 : : }
10210 : :
10211 : 653976 : uint8_t kind = *readp++;
10212 : 653976 : uint64_t op1, op2, len;
10213 : :
10214 : : /* Skip padding. */
10215 [ - + ]: 653976 : if (start_of_list && kind == DW_LLE_end_of_list)
10216 : 0 : continue;
10217 : :
10218 [ + + ]: 653976 : if (start_of_list)
10219 : : {
10220 : 109812 : base = cu_base;
10221 : 109812 : fprintf (out, " Offset: %" PRIx64 ", Index: %" PRIx64 "\n",
10222 : 109812 : (uint64_t) (readp - (unsigned char *) data->d_buf - 1),
10223 : 109812 : (uint64_t) (readp - offset_array_start - 1));
10224 : 109812 : start_of_list = false;
10225 : : }
10226 : :
10227 : 653976 : fprintf (out, " %s", dwarf_loc_list_encoding_name (kind));
10228 [ + - - + : 653976 : switch (kind)
+ - + - +
- - ]
10229 : : {
10230 : 109812 : case DW_LLE_end_of_list:
10231 : 109812 : start_of_list = true;
10232 : 109812 : fprintf (out, "\n\n");
10233 : 109812 : break;
10234 : :
10235 : 0 : case DW_LLE_base_addressx:
10236 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
10237 : : {
10238 : 0 : invalid_entry:
10239 : 0 : error (0, 0, _("invalid loclists data"));
10240 : 0 : goto next_table;
10241 : : }
10242 : 0 : get_uleb128 (op1, readp, nexthdr);
10243 : 0 : fprintf (out, " %" PRIx64 "\n", op1);
10244 [ # # ]: 0 : if (! print_unresolved_addresses)
10245 : : {
10246 : 0 : Dwarf_Addr addr;
10247 [ # # ]: 0 : if (get_indexed_addr (cu, op1, &addr) != 0)
10248 : 0 : fprintf (out, " ???\n");
10249 : : else
10250 : : {
10251 : 0 : fprintf (out, " ");
10252 : 0 : print_dwarf_addr (dwflmod, address_size, addr, addr, out);
10253 : 0 : fprintf (out, "\n");
10254 : : }
10255 : : }
10256 : : break;
10257 : :
10258 : 0 : case DW_LLE_startx_endx:
10259 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
10260 : 0 : goto invalid_entry;
10261 : 0 : get_uleb128 (op1, readp, nexthdr);
10262 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
10263 : 0 : goto invalid_entry;
10264 : 0 : get_uleb128 (op2, readp, nexthdr);
10265 : 0 : fprintf (out, " %" PRIx64 ", %" PRIx64 "\n", op1, op2);
10266 [ # # ]: 0 : if (! print_unresolved_addresses)
10267 : : {
10268 : 0 : Dwarf_Addr addr1;
10269 : 0 : Dwarf_Addr addr2;
10270 [ # # ]: 0 : if (get_indexed_addr (cu, op1, &addr1) != 0
10271 [ # # ]: 0 : || get_indexed_addr (cu, op2, &addr2) != 0)
10272 : : {
10273 : 0 : fprintf (out, " ???..\n");
10274 : 0 : fprintf (out, " ???\n");
10275 : : }
10276 : : else
10277 : : {
10278 : 0 : fprintf (out, " ");
10279 : 0 : print_dwarf_addr (dwflmod, address_size,
10280 : : addr1, addr1, out);
10281 : 0 : fprintf (out, "..\n ");
10282 : 0 : print_dwarf_addr (dwflmod, address_size,
10283 : : addr2 - 1, addr2, out);
10284 : 0 : fprintf (out, "\n");
10285 : : }
10286 : : }
10287 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
10288 : 0 : goto invalid_entry;
10289 : 0 : get_uleb128 (len, readp, nexthdr);
10290 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < len)
10291 : 0 : goto invalid_entry;
10292 : 0 : print_ops (dwflmod, dbg, 8, 8, version,
10293 : : address_size, offset_size, cu, len, readp, out);
10294 : 0 : readp += len;
10295 : 0 : break;
10296 : :
10297 : 52 : case DW_LLE_startx_length:
10298 [ - + ]: 52 : if ((uint64_t) (nexthdr - readp) < 1)
10299 : 0 : goto invalid_entry;
10300 : 52 : get_uleb128 (op1, readp, nexthdr);
10301 [ - + ]: 52 : if ((uint64_t) (nexthdr - readp) < 1)
10302 : 0 : goto invalid_entry;
10303 : 52 : get_uleb128 (op2, readp, nexthdr);
10304 : 52 : fprintf (out, " %" PRIx64 ", %" PRIx64 "\n", op1, op2);
10305 [ + - ]: 52 : if (! print_unresolved_addresses)
10306 : : {
10307 : 52 : Dwarf_Addr addr1;
10308 : 52 : Dwarf_Addr addr2;
10309 [ - + ]: 52 : if (get_indexed_addr (cu, op1, &addr1) != 0)
10310 : : {
10311 : 0 : fprintf (out, " ???..\n");
10312 : 0 : fprintf (out, " ???\n");
10313 : : }
10314 : : else
10315 : : {
10316 : 52 : addr2 = addr1 + op2;
10317 : 52 : fprintf (out, " ");
10318 : 52 : print_dwarf_addr (dwflmod, address_size, addr1, addr1, out);
10319 : 52 : fprintf (out, "..\n ");
10320 : 52 : print_dwarf_addr (dwflmod, address_size,
10321 : : addr2 - 1, addr2, out);
10322 : 52 : fprintf (out, "\n");
10323 : : }
10324 : : }
10325 [ - + ]: 52 : if ((uint64_t) (nexthdr - readp) < 1)
10326 : 0 : goto invalid_entry;
10327 : 52 : get_uleb128 (len, readp, nexthdr);
10328 [ - + ]: 52 : if ((uint64_t) (nexthdr - readp) < len)
10329 : 0 : goto invalid_entry;
10330 : 52 : print_ops (dwflmod, dbg, 8, 8, version,
10331 : : address_size, offset_size, cu, len, readp, out);
10332 : 52 : readp += len;
10333 : 52 : break;
10334 : :
10335 : 476350 : case DW_LLE_offset_pair:
10336 [ - + ]: 476350 : if ((uint64_t) (nexthdr - readp) < 1)
10337 : 0 : goto invalid_entry;
10338 : 476350 : get_uleb128 (op1, readp, nexthdr);
10339 [ - + ]: 476350 : if ((uint64_t) (nexthdr - readp) < 1)
10340 : 0 : goto invalid_entry;
10341 : 476350 : get_uleb128 (op2, readp, nexthdr);
10342 : 476350 : fprintf (out, " %" PRIx64 ", %" PRIx64 "\n", op1, op2);
10343 [ + - ]: 476350 : if (! print_unresolved_addresses)
10344 : : {
10345 : 476350 : op1 += base;
10346 : 476350 : op2 += base;
10347 : 476350 : fprintf (out, " ");
10348 : 476350 : print_dwarf_addr (dwflmod, address_size, op1, op1, out);
10349 : 476350 : fprintf (out, "..\n ");
10350 : 476350 : print_dwarf_addr (dwflmod, address_size, op2 - 1, op2, out);
10351 : 476350 : fprintf (out, "\n");
10352 : : }
10353 [ - + ]: 476350 : if ((uint64_t) (nexthdr - readp) < 1)
10354 : 0 : goto invalid_entry;
10355 : 476350 : get_uleb128 (len, readp, nexthdr);
10356 [ - + ]: 476350 : if ((uint64_t) (nexthdr - readp) < len)
10357 : 0 : goto invalid_entry;
10358 : 476350 : print_ops (dwflmod, dbg, 8, 8, version,
10359 : : address_size, offset_size, cu, len, readp, out);
10360 : 476350 : readp += len;
10361 : 476350 : break;
10362 : :
10363 : 0 : case DW_LLE_default_location:
10364 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
10365 : 0 : goto invalid_entry;
10366 : 0 : get_uleb128 (len, readp, nexthdr);
10367 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < len)
10368 : 0 : goto invalid_entry;
10369 : 0 : print_ops (dwflmod, dbg, 8, 8, version,
10370 : : address_size, offset_size, cu, len, readp, out);
10371 : 0 : readp += len;
10372 : 0 : break;
10373 : :
10374 : 45618 : case DW_LLE_base_address:
10375 [ - + ]: 45618 : if (address_size == 4)
10376 : : {
10377 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 4)
10378 : 0 : goto invalid_entry;
10379 [ # # ]: 0 : op1 = read_4ubyte_unaligned_inc (dbg, readp);
10380 : : }
10381 : : else
10382 : : {
10383 [ - + ]: 45618 : if ((uint64_t) (nexthdr - readp) < 8)
10384 : 0 : goto invalid_entry;
10385 [ - + ]: 45618 : op1 = read_8ubyte_unaligned_inc (dbg, readp);
10386 : : }
10387 : 45618 : base = op1;
10388 : 45618 : fprintf (out, " 0x%" PRIx64 "\n", base);
10389 [ + - ]: 45618 : if (! print_unresolved_addresses)
10390 : : {
10391 : 45618 : fprintf (out, " ");
10392 : 45618 : print_dwarf_addr (dwflmod, address_size, base, base, out);
10393 : 45618 : fprintf (out, "\n");
10394 : : }
10395 : : break;
10396 : :
10397 : 0 : case DW_LLE_start_end:
10398 [ # # ]: 0 : if (address_size == 4)
10399 : : {
10400 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 8)
10401 : 0 : goto invalid_entry;
10402 [ # # ]: 0 : op1 = read_4ubyte_unaligned_inc (dbg, readp);
10403 [ # # ]: 0 : op2 = read_4ubyte_unaligned_inc (dbg, readp);
10404 : : }
10405 : : else
10406 : : {
10407 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 16)
10408 : 0 : goto invalid_entry;
10409 [ # # ]: 0 : op1 = read_8ubyte_unaligned_inc (dbg, readp);
10410 [ # # ]: 0 : op2 = read_8ubyte_unaligned_inc (dbg, readp);
10411 : : }
10412 : 0 : fprintf (out, " 0x%" PRIx64 "..0x%" PRIx64 "\n", op1, op2);
10413 [ # # ]: 0 : if (! print_unresolved_addresses)
10414 : : {
10415 : 0 : fprintf (out, " ");
10416 : 0 : print_dwarf_addr (dwflmod, address_size, op1, op1, out);
10417 : 0 : fprintf (out, "..\n ");
10418 : 0 : print_dwarf_addr (dwflmod, address_size, op2 - 1, op2, out);
10419 : 0 : fprintf (out, "\n");
10420 : : }
10421 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
10422 : 0 : goto invalid_entry;
10423 : 0 : get_uleb128 (len, readp, nexthdr);
10424 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < len)
10425 : 0 : goto invalid_entry;
10426 : 0 : print_ops (dwflmod, dbg, 8, 8, version,
10427 : : address_size, offset_size, cu, len, readp, out);
10428 : 0 : readp += len;
10429 : 0 : break;
10430 : :
10431 : 22144 : case DW_LLE_start_length:
10432 [ - + ]: 22144 : if (address_size == 4)
10433 : : {
10434 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 4)
10435 : 0 : goto invalid_entry;
10436 [ # # ]: 0 : op1 = read_4ubyte_unaligned_inc (dbg, readp);
10437 : : }
10438 : : else
10439 : : {
10440 [ - + ]: 22144 : if ((uint64_t) (nexthdr - readp) < 8)
10441 : 0 : goto invalid_entry;
10442 [ - + ]: 22144 : op1 = read_8ubyte_unaligned_inc (dbg, readp);
10443 : : }
10444 [ - + ]: 22144 : if ((uint64_t) (nexthdr - readp) < 1)
10445 : 0 : goto invalid_entry;
10446 : 22144 : get_uleb128 (op2, readp, nexthdr);
10447 : 22144 : fprintf (out, " 0x%" PRIx64 ", %" PRIx64 "\n", op1, op2);
10448 [ + + ]: 22144 : if (! print_unresolved_addresses)
10449 : : {
10450 : 22142 : op2 = op1 + op2;
10451 : 22142 : fprintf (out, " ");
10452 : 22142 : print_dwarf_addr (dwflmod, address_size, op1, op1, out);
10453 : 22142 : fprintf (out, "..\n ");
10454 : 22142 : print_dwarf_addr (dwflmod, address_size, op2 - 1, op2, out);
10455 : 22142 : fprintf (out, "\n");
10456 : : }
10457 [ - + ]: 22144 : if ((uint64_t) (nexthdr - readp) < 1)
10458 : 0 : goto invalid_entry;
10459 : 22144 : get_uleb128 (len, readp, nexthdr);
10460 [ - + ]: 22144 : if ((uint64_t) (nexthdr - readp) < len)
10461 : 0 : goto invalid_entry;
10462 : 22144 : print_ops (dwflmod, dbg, 8, 8, version,
10463 : : address_size, offset_size, cu, len, readp, out);
10464 : 22144 : readp += len;
10465 : 22144 : break;
10466 : :
10467 : 0 : case DW_LLE_GNU_view_pair:
10468 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
10469 : 0 : goto invalid_entry;
10470 : 0 : get_uleb128 (op1, readp, nexthdr);
10471 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
10472 : 0 : goto invalid_entry;
10473 : 0 : get_uleb128 (op2, readp, nexthdr);
10474 : 0 : fprintf (out, " %" PRIx64 ", %" PRIx64 "\n", op1, op2);
10475 : 0 : break;
10476 : :
10477 : 0 : default:
10478 : 0 : goto invalid_entry;
10479 : : }
10480 : : }
10481 : :
10482 : 3092 : next_table:
10483 [ - + ]: 3092 : if (readp != nexthdr)
10484 : : {
10485 : 0 : size_t padding = nexthdr - readp;
10486 : 0 : fprintf (out, _(" %zu padding bytes\n\n"), padding);
10487 : 0 : readp = nexthdr;
10488 : : }
10489 : : }
10490 : : }
10491 : :
10492 : :
10493 : : static void
10494 : 52 : print_debug_loc_section (Dwfl_Module *dwflmod,
10495 : : Ebl *ebl, GElf_Ehdr *ehdr,
10496 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg,
10497 : : FILE *out)
10498 : : {
10499 : 52 : Elf_Data *data = get_debug_elf_data (dbg, ebl, IDX_debug_loc, scn);
10500 [ - + ]: 52 : if (data == NULL)
10501 : 0 : return;
10502 : :
10503 : 52 : fprintf (out, _("\
10504 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
10505 : : elf_ndxscn (scn), section_name (ebl, shdr),
10506 : 52 : (uint64_t) shdr->sh_offset);
10507 : :
10508 : 52 : sort_listptr (&known_locsptr, "loclistptr");
10509 : 52 : size_t listptr_idx = 0;
10510 : :
10511 [ + - ]: 52 : uint_fast8_t address_size = ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 4 : 8;
10512 : 52 : uint_fast8_t offset_size = 4;
10513 : :
10514 : 52 : bool first = true;
10515 : 52 : Dwarf_Addr base = 0;
10516 : 52 : unsigned char *readp = data->d_buf;
10517 : 52 : unsigned char *const endp = (unsigned char *) data->d_buf + data->d_size;
10518 : 52 : Dwarf_CU *last_cu = NULL;
10519 [ + + ]: 490 : while (readp < endp)
10520 : : {
10521 : 438 : ptrdiff_t offset = readp - (unsigned char *) data->d_buf;
10522 : 438 : Dwarf_CU *cu = last_cu;
10523 : 438 : unsigned int attr = 0;
10524 : :
10525 [ + + - + ]: 438 : if (first && skip_listptr_hole (&known_locsptr, &listptr_idx,
10526 : : &address_size, &offset_size, &base,
10527 : : &cu, offset, &readp, endp, &attr))
10528 : 0 : continue;
10529 : :
10530 [ + + ]: 438 : if (last_cu != cu)
10531 : : {
10532 : 60 : Dwarf_Die cudie;
10533 [ - + ]: 60 : if (dwarf_cu_die (cu, &cudie,
10534 : : NULL, NULL, NULL, NULL,
10535 : : NULL, NULL) == NULL)
10536 : 0 : fprintf (out, _("\n Unknown CU base: "));
10537 : : else
10538 : 60 : fprintf (out, _("\n CU [%6" PRIx64 "] base: "),
10539 : : dwarf_dieoffset (&cudie));
10540 : 60 : print_dwarf_addr (dwflmod, address_size, base, base, out);
10541 : 60 : fprintf (out, "\n");
10542 : : }
10543 : 438 : last_cu = cu;
10544 : :
10545 [ - + ]: 438 : if (attr == DW_AT_GNU_locviews)
10546 : 0 : {
10547 : 0 : Dwarf_Off next_off = next_listptr_offset (&known_locsptr,
10548 : : &listptr_idx, offset);
10549 : 0 : const unsigned char *locp = readp;
10550 : 0 : const unsigned char *locendp;
10551 [ # # ]: 0 : if (next_off == 0
10552 : 0 : || next_off > (size_t) (endp
10553 [ # # ]: 0 : - (const unsigned char *) data->d_buf))
10554 : 0 : locendp = endp;
10555 : : else
10556 : 0 : locendp = (const unsigned char *) data->d_buf + next_off;
10557 : :
10558 [ # # ]: 0 : while (locp < locendp)
10559 : : {
10560 : 0 : uint64_t v1, v2;
10561 : 0 : get_uleb128 (v1, locp, locendp);
10562 [ # # ]: 0 : if (locp >= locendp)
10563 : : {
10564 : 0 : fprintf (out, _(" [%6tx] <INVALID DATA>\n"), offset);
10565 : 0 : break;
10566 : : }
10567 : 0 : get_uleb128 (v2, locp, locendp);
10568 [ # # ]: 0 : if (first) /* First view pair in a list. */
10569 : 0 : fprintf (out, " [%6tx] ", offset);
10570 : : else
10571 : 0 : fprintf (out, " ");
10572 : 0 : fprintf (out, "view pair %" PRId64 ", %" PRId64 "\n", v1, v2);
10573 : 0 : first = false;
10574 : : }
10575 : :
10576 : 0 : first = true;
10577 : 0 : readp = (unsigned char *) locendp;
10578 : 0 : continue;
10579 : : }
10580 : :
10581 : : /* GNU DebugFission encoded addresses as addrx. */
10582 : 1194 : bool is_debugfission = ((cu != NULL
10583 [ # # ]: 0 : || split_dwarf_cu_base (dbg, &cu, &base))
10584 [ - + + - ]: 438 : && (cu->version < 5
10585 [ + + ]: 438 : && cu->unit_type == DW_UT_split_compile));
10586 : 318 : if (!is_debugfission
10587 [ - + ]: 318 : && unlikely (data->d_size - offset < (size_t) address_size * 2))
10588 : : {
10589 : 0 : invalid_data:
10590 : 0 : fprintf (out, _(" [%6tx] <INVALID DATA>\n"), offset);
10591 : 0 : break;
10592 : : }
10593 : :
10594 : 438 : Dwarf_Addr begin;
10595 : 438 : Dwarf_Addr end;
10596 : 438 : bool use_base = true;
10597 [ + + ]: 438 : if (is_debugfission)
10598 : : {
10599 : 120 : const unsigned char *locp = readp;
10600 : 120 : const unsigned char *locendp = readp + data->d_size;
10601 [ - + ]: 120 : if (locp >= locendp)
10602 : 0 : goto invalid_data;
10603 : :
10604 : 120 : Dwarf_Word idx;
10605 : 120 : unsigned char code = *locp++;
10606 [ + - - + : 120 : switch (code)
- ]
10607 : : {
10608 : 40 : case DW_LLE_GNU_end_of_list_entry:
10609 : 40 : begin = 0;
10610 : 40 : end = 0;
10611 : 40 : break;
10612 : :
10613 : 0 : case DW_LLE_GNU_base_address_selection_entry:
10614 [ # # ]: 0 : if (locp >= locendp)
10615 : 0 : goto invalid_data;
10616 : 0 : begin = (Dwarf_Addr) -1;
10617 : 0 : get_uleb128 (idx, locp, locendp);
10618 [ # # ]: 0 : if (get_indexed_addr (cu, idx, &end) != 0)
10619 : 0 : end = idx; /* ... */
10620 : : break;
10621 : :
10622 : 0 : case DW_LLE_GNU_start_end_entry:
10623 [ # # ]: 0 : if (locp >= locendp)
10624 : 0 : goto invalid_data;
10625 : 0 : get_uleb128 (idx, locp, locendp);
10626 [ # # ]: 0 : if (get_indexed_addr (cu, idx, &begin) != 0)
10627 : 0 : begin = idx; /* ... */
10628 [ # # ]: 0 : if (locp >= locendp)
10629 : 0 : goto invalid_data;
10630 : 0 : get_uleb128 (idx, locp, locendp);
10631 [ # # ]: 0 : if (get_indexed_addr (cu, idx, &end) != 0)
10632 : 0 : end = idx; /* ... */
10633 : : use_base = false;
10634 : : break;
10635 : :
10636 : 80 : case DW_LLE_GNU_start_length_entry:
10637 [ - + ]: 80 : if (locp >= locendp)
10638 : 0 : goto invalid_data;
10639 : 80 : get_uleb128 (idx, locp, locendp);
10640 [ - + ]: 80 : if (get_indexed_addr (cu, idx, &begin) != 0)
10641 : 0 : begin = idx; /* ... */
10642 [ - + ]: 80 : if (locendp - locp < 4)
10643 : 0 : goto invalid_data;
10644 [ - + ]: 80 : end = read_4ubyte_unaligned_inc (dbg, locp);
10645 : 80 : end += begin;
10646 : 80 : use_base = false;
10647 : 80 : break;
10648 : :
10649 : 0 : default:
10650 : 0 : goto invalid_data;
10651 : : }
10652 : :
10653 : 120 : readp = (unsigned char *) locp;
10654 : : }
10655 [ + - ]: 318 : else if (address_size == 8)
10656 : : {
10657 [ + + ]: 318 : begin = read_8ubyte_unaligned_inc (dbg, readp);
10658 [ + + ]: 318 : end = read_8ubyte_unaligned_inc (dbg, readp);
10659 : : }
10660 : : else
10661 : : {
10662 [ # # ]: 0 : begin = read_4ubyte_unaligned_inc (dbg, readp);
10663 [ # # ]: 0 : end = read_4ubyte_unaligned_inc (dbg, readp);
10664 [ # # ]: 0 : if (begin == (Dwarf_Addr) (uint32_t) -1)
10665 : 0 : begin = (Dwarf_Addr) -1l;
10666 : : }
10667 : :
10668 [ - + ]: 438 : if (begin == (Dwarf_Addr) -1l) /* Base address entry. */
10669 : : {
10670 [ # # ]: 0 : if (first)
10671 : 0 : fprintf (out, " [%6tx] ", offset);
10672 : : else
10673 : 0 : fprintf (out, " ");
10674 : 0 : fputs (_("base address\n"), out);
10675 : 0 : fprintf (out, " ");
10676 : 0 : print_dwarf_addr (dwflmod, address_size, end, end, out);
10677 : 0 : fprintf (out, "\n");
10678 : 0 : base = end;
10679 : 0 : first = false;
10680 : : }
10681 [ + + + + ]: 438 : else if (begin == 0 && end == 0) /* End of list entry. */
10682 : : {
10683 [ - + ]: 148 : if (first)
10684 : 0 : fprintf (out, _(" [%6tx] empty list\n"), offset);
10685 : : first = true;
10686 : : }
10687 : : else
10688 : : {
10689 : : /* We have a location expression entry. */
10690 [ + + ]: 290 : uint_fast16_t len = read_2ubyte_unaligned_inc (dbg, readp);
10691 : :
10692 [ + + ]: 290 : if (first) /* First entry in a list. */
10693 : 148 : fprintf (out, " [%6tx] ", offset);
10694 : : else
10695 : 142 : fprintf (out, " ");
10696 : :
10697 : 290 : fprintf (out, "range %" PRIx64 ", %" PRIx64 "\n", begin, end);
10698 [ + + ]: 290 : if (! print_unresolved_addresses)
10699 : : {
10700 : 230 : Dwarf_Addr dab = use_base ? base + begin : begin;
10701 : 230 : Dwarf_Addr dae = use_base ? base + end : end;
10702 : 230 : fprintf (out, " ");
10703 : 230 : print_dwarf_addr (dwflmod, address_size, dab, dab, out);
10704 : 230 : fprintf (out, "..\n ");
10705 : 230 : print_dwarf_addr (dwflmod, address_size, dae - 1, dae, out);
10706 : 230 : fprintf (out, "\n");
10707 : : }
10708 : :
10709 [ - + ]: 290 : if (endp - readp <= (ptrdiff_t) len)
10710 : : {
10711 : 0 : fputs (_(" <INVALID DATA>\n"), out);
10712 : 0 : break;
10713 : : }
10714 : :
10715 [ + - ]: 580 : print_ops (dwflmod, dbg, 11, 11,
10716 : 290 : cu != NULL ? cu->version : 3,
10717 : : address_size, offset_size, cu, len, readp, out);
10718 : :
10719 : 290 : first = false;
10720 : 290 : readp += len;
10721 : : }
10722 : : }
10723 : : }
10724 : :
10725 : : struct mac_culist
10726 : : {
10727 : : Dwarf_Die die;
10728 : : Dwarf_Off offset;
10729 : : Dwarf_Files *files;
10730 : : struct mac_culist *next;
10731 : : };
10732 : :
10733 : :
10734 : : static int
10735 : 0 : mac_compare (const void *p1, const void *p2)
10736 : : {
10737 : 0 : struct mac_culist *m1 = (struct mac_culist *) p1;
10738 : 0 : struct mac_culist *m2 = (struct mac_culist *) p2;
10739 : :
10740 [ # # ]: 0 : if (m1->offset < m2->offset)
10741 : : return -1;
10742 [ # # ]: 0 : if (m1->offset > m2->offset)
10743 : 0 : return 1;
10744 : : return 0;
10745 : : }
10746 : :
10747 : :
10748 : : static void
10749 : 0 : print_debug_macinfo_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
10750 : : Ebl *ebl,
10751 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
10752 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg,
10753 : : FILE *out)
10754 : : {
10755 : 0 : Elf_Data *data = get_debug_elf_data (dbg, ebl, IDX_debug_macinfo, scn);
10756 [ # # ]: 0 : if (data == NULL)
10757 : 0 : return;
10758 : :
10759 : 0 : fprintf (out, _("\
10760 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
10761 : : elf_ndxscn (scn), section_name (ebl, shdr),
10762 : 0 : (uint64_t) shdr->sh_offset);
10763 : 0 : fputc ('\n', out);
10764 : :
10765 : : /* There is no function in libdw to iterate over the raw content of
10766 : : the section but it is easy enough to do. */
10767 : :
10768 : : /* Get the source file information for all CUs. */
10769 : 0 : Dwarf_Off offset;
10770 : 0 : Dwarf_Off ncu = 0;
10771 : 0 : size_t hsize;
10772 : 0 : struct mac_culist *culist = NULL;
10773 : 0 : size_t nculist = 0;
10774 [ # # ]: 0 : while (dwarf_nextcu (dbg, offset = ncu, &ncu, &hsize, NULL, NULL, NULL) == 0)
10775 : : {
10776 : 0 : Dwarf_Die cudie;
10777 [ # # ]: 0 : if (dwarf_offdie (dbg, offset + hsize, &cudie) == NULL)
10778 : 0 : continue;
10779 : :
10780 : 0 : Dwarf_Attribute attr;
10781 [ # # ]: 0 : if (dwarf_attr (&cudie, DW_AT_macro_info, &attr) == NULL)
10782 : 0 : continue;
10783 : :
10784 : 0 : Dwarf_Word macoff;
10785 [ # # ]: 0 : if (dwarf_formudata (&attr, &macoff) != 0)
10786 : 0 : continue;
10787 : :
10788 : 0 : struct mac_culist *newp = (struct mac_culist *) alloca (sizeof (*newp));
10789 : 0 : newp->die = cudie;
10790 : 0 : newp->offset = macoff;
10791 : 0 : newp->files = NULL;
10792 : 0 : newp->next = culist;
10793 : 0 : culist = newp;
10794 : 0 : ++nculist;
10795 : : }
10796 : :
10797 : : /* Convert the list into an array for easier consumption. */
10798 : 0 : struct mac_culist *cus = (struct mac_culist *) alloca ((nculist + 1)
10799 : : * sizeof (*cus));
10800 : : /* Add sentinel. */
10801 : 0 : cus[nculist].offset = data->d_size;
10802 : 0 : cus[nculist].files = (Dwarf_Files *) -1l;
10803 [ # # ]: 0 : if (nculist > 0)
10804 : : {
10805 [ # # ]: 0 : for (size_t cnt = nculist - 1; culist != NULL; --cnt)
10806 : : {
10807 [ # # ]: 0 : assert (cnt < nculist);
10808 : 0 : cus[cnt] = *culist;
10809 : 0 : culist = culist->next;
10810 : : }
10811 : :
10812 : : /* Sort the array according to the offset in the .debug_macinfo
10813 : : section. Note we keep the sentinel at the end. */
10814 : 0 : qsort (cus, nculist, sizeof (*cus), mac_compare);
10815 : : }
10816 : :
10817 : 0 : const unsigned char *readp = (const unsigned char *) data->d_buf;
10818 : 0 : const unsigned char *readendp = readp + data->d_size;
10819 : 0 : int level = 1;
10820 : :
10821 [ # # ]: 0 : while (readp < readendp)
10822 : : {
10823 : 0 : unsigned int opcode = *readp++;
10824 : 0 : unsigned int u128;
10825 : 0 : unsigned int u128_2;
10826 : 0 : const unsigned char *endp;
10827 : :
10828 [ # # # # ]: 0 : switch (opcode)
10829 : : {
10830 : 0 : case DW_MACINFO_define:
10831 : : case DW_MACINFO_undef:
10832 : : case DW_MACINFO_vendor_ext:
10833 : : /* For the first two opcodes the parameters are
10834 : : line, string
10835 : : For the latter
10836 : : number, string.
10837 : : We can treat these cases together. */
10838 : 0 : get_uleb128 (u128, readp, readendp);
10839 : :
10840 : 0 : endp = memchr (readp, '\0', readendp - readp);
10841 [ # # ]: 0 : if (unlikely (endp == NULL))
10842 : : {
10843 : 0 : fprintf (out, _("\
10844 : : %*s*** non-terminated string at end of section"),
10845 : : level, "");
10846 : 0 : return;
10847 : : }
10848 : :
10849 [ # # ]: 0 : if (opcode == DW_MACINFO_define)
10850 : 0 : fprintf (out, "%*s#define %s, line %u\n",
10851 : : level, "", (char *) readp, u128);
10852 [ # # ]: 0 : else if (opcode == DW_MACINFO_undef)
10853 : 0 : fprintf (out, "%*s#undef %s, line %u\n",
10854 : : level, "", (char *) readp, u128);
10855 : : else
10856 : 0 : fprintf (out, " #vendor-ext %s, number %u\n", (char *) readp, u128);
10857 : :
10858 : 0 : readp = endp + 1;
10859 : 0 : break;
10860 : :
10861 : 0 : case DW_MACINFO_start_file:
10862 : : /* The two parameters are line and file index, in this order. */
10863 : 0 : get_uleb128 (u128, readp, readendp);
10864 [ # # ]: 0 : if (readendp - readp < 1)
10865 : : {
10866 : 0 : fprintf (out, _("\
10867 : : %*s*** missing DW_MACINFO_start_file argument at end of section"),
10868 : : level, "");
10869 : 0 : return;
10870 : : }
10871 : 0 : get_uleb128 (u128_2, readp, readendp);
10872 : :
10873 : : /* Find the CU DIE for this file. */
10874 : 0 : size_t macoff = readp - (const unsigned char *) data->d_buf;
10875 : 0 : const char *fname = "???";
10876 [ # # # # ]: 0 : if (macoff >= cus[0].offset && cus[0].offset != data->d_size)
10877 : : {
10878 [ # # # # ]: 0 : while (macoff >= cus[1].offset && cus[1].offset != data->d_size)
10879 : 0 : ++cus;
10880 : :
10881 [ # # ]: 0 : if (cus[0].files == NULL
10882 [ # # ]: 0 : && dwarf_getsrcfiles (&cus[0].die, &cus[0].files, NULL) != 0)
10883 : 0 : cus[0].files = (Dwarf_Files *) -1l;
10884 : :
10885 [ # # ]: 0 : if (cus[0].files != (Dwarf_Files *) -1l)
10886 : 0 : fname = (dwarf_filesrc (cus[0].files, u128_2, NULL, NULL)
10887 [ # # ]: 0 : ?: "???");
10888 : : }
10889 : :
10890 : 0 : fprintf (out, "%*sstart_file %u, [%u] %s\n",
10891 : : level, "", u128, u128_2, fname);
10892 : 0 : ++level;
10893 : 0 : break;
10894 : :
10895 : 0 : case DW_MACINFO_end_file:
10896 : 0 : --level;
10897 : 0 : fprintf (out, "%*send_file\n", level, "");
10898 : : /* Nothing more to do. */
10899 : 0 : break;
10900 : :
10901 : 0 : default:
10902 : : // XXX gcc seems to generate files with a trailing zero.
10903 [ # # # # ]: 0 : if (unlikely (opcode != 0 || readp != readendp))
10904 : 0 : fprintf (out, "%*s*** invalid opcode %u\n", level, "", opcode);
10905 : : break;
10906 : : }
10907 : : }
10908 : : }
10909 : :
10910 : :
10911 : : static void
10912 : 8 : print_debug_macro_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
10913 : : Ebl *ebl,
10914 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
10915 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg,
10916 : : FILE *out)
10917 : : {
10918 : 8 : Elf_Data *data = get_debug_elf_data (dbg, ebl, IDX_debug_macro, scn);
10919 [ + - ]: 8 : if (data == NULL)
10920 : 0 : return;
10921 : :
10922 : 8 : fprintf (out, _("\
10923 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
10924 : : elf_ndxscn (scn), section_name (ebl, shdr),
10925 : 8 : (uint64_t) shdr->sh_offset);
10926 : 8 : fputc ('\n', out);
10927 : :
10928 : : /* Get the source file information for all CUs. Uses same
10929 : : datastructure as macinfo. But uses offset field to directly
10930 : : match .debug_line offset. And just stored in a list. */
10931 : 8 : Dwarf_Off offset;
10932 : 8 : Dwarf_Off ncu = 0;
10933 : 8 : size_t hsize;
10934 : 8 : struct mac_culist *culist = NULL;
10935 [ + + ]: 20 : while (dwarf_nextcu (dbg, offset = ncu, &ncu, &hsize, NULL, NULL, NULL) == 0)
10936 : : {
10937 : 12 : Dwarf_Die cudie;
10938 [ - + ]: 12 : if (dwarf_offdie (dbg, offset + hsize, &cudie) == NULL)
10939 : 0 : continue;
10940 : :
10941 : 12 : Dwarf_Attribute attr;
10942 [ - + ]: 12 : if (dwarf_attr (&cudie, DW_AT_stmt_list, &attr) == NULL)
10943 : 0 : continue;
10944 : :
10945 : 12 : Dwarf_Word lineoff;
10946 [ - + ]: 12 : if (dwarf_formudata (&attr, &lineoff) != 0)
10947 : 0 : continue;
10948 : :
10949 : 12 : struct mac_culist *newp = (struct mac_culist *) alloca (sizeof (*newp));
10950 : 12 : newp->die = cudie;
10951 : 12 : newp->offset = lineoff;
10952 : 12 : newp->files = NULL;
10953 : 12 : newp->next = culist;
10954 : 12 : culist = newp;
10955 : : }
10956 : :
10957 : 8 : const unsigned char *readp = (const unsigned char *) data->d_buf;
10958 : 8 : const unsigned char *readendp = readp + data->d_size;
10959 : :
10960 [ + + ]: 28 : while (readp < readendp)
10961 : : {
10962 : 40 : fprintf (out, _(" Offset: 0x%" PRIx64 "\n"),
10963 : 20 : (uint64_t) (readp - (const unsigned char *) data->d_buf));
10964 : :
10965 : : // Header, 2 byte version, 1 byte flag, optional .debug_line offset,
10966 : : // optional vendor extension macro entry table.
10967 [ - + ]: 20 : if (readp + 2 > readendp)
10968 : : {
10969 : 0 : invalid_data:
10970 : 0 : error (0, 0, _("invalid data"));
10971 : 0 : return;
10972 : : }
10973 [ - + ]: 20 : const uint16_t vers = read_2ubyte_unaligned_inc (dbg, readp);
10974 : 20 : fprintf (out, _(" Version: %" PRIu16 "\n"), vers);
10975 : :
10976 : : // Version 4 is the GNU extension for DWARF4. DWARF5 will use version
10977 : : // 5 when it gets standardized.
10978 [ - + ]: 20 : if (vers != 4 && vers != 5)
10979 : : {
10980 : 0 : fprintf (out, _(" unknown version, cannot parse section\n"));
10981 : 0 : return;
10982 : : }
10983 : :
10984 [ - + ]: 20 : if (readp + 1 > readendp)
10985 : 0 : goto invalid_data;
10986 : 20 : const unsigned char flag = *readp++;
10987 : 20 : fprintf (out, _(" Flag: 0x%" PRIx8), flag);
10988 [ + + ]: 20 : if (flag != 0)
10989 : : {
10990 : 12 : fprintf (out, " (");
10991 [ - + ]: 12 : if ((flag & 0x01) != 0)
10992 : : {
10993 : 0 : fprintf (out, "offset_size");
10994 [ # # ]: 0 : if ((flag & 0xFE) != 0)
10995 : 0 : fprintf (out, ", ");
10996 : : }
10997 [ + - ]: 12 : if ((flag & 0x02) != 0)
10998 : : {
10999 : 12 : fprintf (out, "debug_line_offset");
11000 [ - + ]: 12 : if ((flag & 0xFC) != 0)
11001 : 0 : fprintf (out, ", ");
11002 : : }
11003 [ # # ]: 0 : if ((flag & 0x04) != 0)
11004 : : {
11005 : 0 : fprintf (out, "operands_table");
11006 [ # # ]: 0 : if ((flag & 0xF8) != 0)
11007 : 0 : fprintf (out, ", ");
11008 : : }
11009 [ - + ]: 12 : if ((flag & 0xF8) != 0)
11010 : 0 : fprintf (out, "unknown");
11011 : 12 : fprintf (out, ")");
11012 : : }
11013 : 20 : fprintf (out, "\n");
11014 : :
11015 [ + - ]: 20 : unsigned int offset_len = (flag & 0x01) ? 8 : 4;
11016 : 20 : fprintf (out, _(" Offset length: %" PRIu8 "\n"), offset_len);
11017 : 20 : Dwarf_Off line_offset = -1;
11018 [ + + ]: 20 : if (flag & 0x02)
11019 : : {
11020 [ - + ]: 12 : if (offset_len == 8)
11021 [ # # ]: 0 : line_offset = read_8ubyte_unaligned_inc (dbg, readp);
11022 : : else
11023 [ - + ]: 12 : line_offset = read_4ubyte_unaligned_inc (dbg, readp);
11024 : 12 : fprintf (out, _(" .debug_line offset: 0x%" PRIx64 "\n"),
11025 : : line_offset);
11026 : : }
11027 : :
11028 : 12 : struct mac_culist *cu = NULL;
11029 [ + - ]: 12 : if (line_offset != (Dwarf_Off) -1)
11030 : : {
11031 : : cu = culist;
11032 [ + - + + ]: 16 : while (cu != NULL && line_offset != cu->offset)
11033 : 4 : cu = cu->next;
11034 : : }
11035 : :
11036 [ - + ]: 20 : Dwarf_Off str_offsets_base = str_offsets_base_off (dbg, (cu != NULL
11037 : : ? cu->die.cu
11038 : : : NULL));
11039 : :
11040 : 20 : const unsigned char *vendor[DW_MACRO_hi_user - DW_MACRO_lo_user + 1];
11041 [ - + ]: 20 : memset (vendor, 0, sizeof vendor);
11042 [ - + ]: 20 : if (flag & 0x04)
11043 : : {
11044 : : // 1 byte length, for each item, 1 byte opcode, uleb128 number
11045 : : // of arguments, for each argument 1 byte form code.
11046 [ # # ]: 0 : if (readp + 1 > readendp)
11047 : 0 : goto invalid_data;
11048 : 0 : unsigned int tlen = *readp++;
11049 : 0 : fprintf (out, _(" extension opcode table, %" PRIu8 " items:\n"),
11050 : : tlen);
11051 [ # # ]: 0 : for (unsigned int i = 0; i < tlen; i++)
11052 : : {
11053 [ # # ]: 0 : if (readp + 1 > readendp)
11054 : 0 : goto invalid_data;
11055 : 0 : unsigned int opcode = *readp++;
11056 : 0 : fprintf (out, _(" [%" PRIx8 "]"), opcode);
11057 : 0 : if (opcode < DW_MACRO_lo_user
11058 [ # # ]: 0 : || opcode > DW_MACRO_hi_user)
11059 : 0 : goto invalid_data;
11060 : : // Record the start of description for this vendor opcode.
11061 : : // uleb128 nr args, 1 byte per arg form.
11062 : 0 : vendor[opcode - DW_MACRO_lo_user] = readp;
11063 [ # # ]: 0 : if (readp + 1 > readendp)
11064 : 0 : goto invalid_data;
11065 : 0 : unsigned int args = *readp++;
11066 [ # # ]: 0 : if (args > 0)
11067 : : {
11068 : 0 : fprintf (out, _(" %" PRIu8 " arguments:"), args);
11069 : 0 : while (args > 0)
11070 : : {
11071 [ # # ]: 0 : if (readp + 1 > readendp)
11072 : 0 : goto invalid_data;
11073 : 0 : unsigned int form = *readp++;
11074 : 0 : fprintf (out, " %s", dwarf_form_name (form));
11075 [ # # ]: 0 : if (! libdw_valid_user_form (form))
11076 : 0 : goto invalid_data;
11077 : 0 : args--;
11078 [ # # ]: 0 : if (args > 0)
11079 : 0 : fputc (',', out);
11080 : : }
11081 : : }
11082 : : else
11083 : 0 : fprintf (out, _(" no arguments."));
11084 : 0 : fputc ('\n', out);
11085 : : }
11086 : : }
11087 : 20 : fputc ('\n', out);
11088 : :
11089 : 20 : int level = 1;
11090 [ - + ]: 20 : if (readp + 1 > readendp)
11091 : 0 : goto invalid_data;
11092 : 20 : unsigned int opcode = *readp++;
11093 [ + + ]: 3046 : while (opcode != 0)
11094 : : {
11095 : 3026 : unsigned int u128;
11096 : 3026 : unsigned int u128_2;
11097 : 3026 : const unsigned char *endp;
11098 : 3026 : uint64_t off;
11099 : :
11100 [ + + + - : 3026 : switch (opcode)
+ + + - -
- + + - ]
11101 : : {
11102 : 20 : case DW_MACRO_start_file:
11103 : 20 : get_uleb128 (u128, readp, readendp);
11104 [ - + ]: 20 : if (readp >= readendp)
11105 : 0 : goto invalid_data;
11106 : 20 : get_uleb128 (u128_2, readp, readendp);
11107 : :
11108 : : /* Find the CU DIE that matches this line offset. */
11109 : 20 : const char *fname = "???";
11110 [ - + ]: 20 : if (cu != NULL)
11111 : : {
11112 [ + + ]: 20 : if (cu->files == NULL
11113 [ - + ]: 12 : && dwarf_getsrcfiles (&cu->die, &cu->files,
11114 : : NULL) != 0)
11115 : 0 : cu->files = (Dwarf_Files *) -1l;
11116 : :
11117 [ - + ]: 20 : if (cu->files != (Dwarf_Files *) -1l)
11118 : 20 : fname = (dwarf_filesrc (cu->files, u128_2,
11119 [ - + ]: 20 : NULL, NULL) ?: "???");
11120 : : }
11121 : 20 : fprintf (out, "%*sstart_file %u, [%u] %s\n",
11122 : : level, "", u128, u128_2, fname);
11123 : 20 : ++level;
11124 : 20 : break;
11125 : :
11126 : 20 : case DW_MACRO_end_file:
11127 : 20 : --level;
11128 : 20 : fprintf (out, "%*send_file\n", level, "");
11129 : 20 : break;
11130 : :
11131 : 2 : case DW_MACRO_define:
11132 : 2 : get_uleb128 (u128, readp, readendp);
11133 : 2 : endp = memchr (readp, '\0', readendp - readp);
11134 [ - + ]: 2 : if (endp == NULL)
11135 : 0 : goto invalid_data;
11136 : 2 : fprintf (out, "%*s#define %s, line %u\n",
11137 : : level, "", readp, u128);
11138 : 2 : readp = endp + 1;
11139 : 2 : break;
11140 : :
11141 : 0 : case DW_MACRO_undef:
11142 : 0 : get_uleb128 (u128, readp, readendp);
11143 : 0 : endp = memchr (readp, '\0', readendp - readp);
11144 [ # # ]: 0 : if (endp == NULL)
11145 : 0 : goto invalid_data;
11146 : 0 : fprintf (out, "%*s#undef %s, line %u\n",
11147 : : level, "", readp, u128);
11148 : 0 : readp = endp + 1;
11149 : 0 : break;
11150 : :
11151 : 1414 : case DW_MACRO_define_strp:
11152 : 1414 : get_uleb128 (u128, readp, readendp);
11153 [ - + ]: 1414 : if (readp + offset_len > readendp)
11154 : 0 : goto invalid_data;
11155 [ - + ]: 1414 : if (offset_len == 8)
11156 [ # # ]: 0 : off = read_8ubyte_unaligned_inc (dbg, readp);
11157 : : else
11158 [ - + ]: 1414 : off = read_4ubyte_unaligned_inc (dbg, readp);
11159 : 1414 : fprintf (out, "%*s#define %s, line %u (indirect)\n",
11160 : : level, "", dwarf_getstring (dbg, off, NULL), u128);
11161 : 1414 : break;
11162 : :
11163 : 2 : case DW_MACRO_undef_strp:
11164 : 2 : get_uleb128 (u128, readp, readendp);
11165 [ - + ]: 2 : if (readp + offset_len > readendp)
11166 : 0 : goto invalid_data;
11167 [ - + ]: 2 : if (offset_len == 8)
11168 [ # # ]: 0 : off = read_8ubyte_unaligned_inc (dbg, readp);
11169 : : else
11170 [ - + ]: 2 : off = read_4ubyte_unaligned_inc (dbg, readp);
11171 : 2 : fprintf (out, "%*s#undef %s, line %u (indirect)\n",
11172 : : level, "", dwarf_getstring (dbg, off, NULL), u128);
11173 : 2 : break;
11174 : :
11175 : 12 : case DW_MACRO_import:
11176 [ - + ]: 12 : if (readp + offset_len > readendp)
11177 : 0 : goto invalid_data;
11178 [ - + ]: 12 : if (offset_len == 8)
11179 [ # # ]: 0 : off = read_8ubyte_unaligned_inc (dbg, readp);
11180 : : else
11181 [ - + ]: 12 : off = read_4ubyte_unaligned_inc (dbg, readp);
11182 : 12 : fprintf (out, "%*s#include offset 0x%" PRIx64 "\n",
11183 : : level, "", off);
11184 : 12 : break;
11185 : :
11186 : 0 : case DW_MACRO_define_sup:
11187 : 0 : get_uleb128 (u128, readp, readendp);
11188 : 0 : fprintf (out, "%*s#define ", level, "");
11189 : 0 : readp = print_form_data (dbg, DW_FORM_strp_sup,
11190 : : readp, readendp, offset_len,
11191 : : str_offsets_base, out);
11192 : 0 : fprintf (out, ", line %u (sup)\n", u128);
11193 : 0 : break;
11194 : :
11195 : 0 : case DW_MACRO_undef_sup:
11196 : 0 : get_uleb128 (u128, readp, readendp);
11197 : 0 : fprintf (out, "%*s#undef ", level, "");
11198 : 0 : readp = print_form_data (dbg, DW_FORM_strp_sup,
11199 : : readp, readendp, offset_len,
11200 : : str_offsets_base, out);
11201 : 0 : fprintf (out, ", line %u (sup)\n", u128);
11202 : 0 : break;
11203 : :
11204 : 0 : case DW_MACRO_import_sup:
11205 [ # # ]: 0 : if (readp + offset_len > readendp)
11206 : 0 : goto invalid_data;
11207 [ # # ]: 0 : if (offset_len == 8)
11208 [ # # ]: 0 : off = read_8ubyte_unaligned_inc (dbg, readp);
11209 : : else
11210 [ # # ]: 0 : off = read_4ubyte_unaligned_inc (dbg, readp);
11211 : : // XXX Needs support for reading from supplementary object file.
11212 : 0 : fprintf (out, "%*s#include offset 0x%" PRIx64 " (sup)\n",
11213 : : level, "", off);
11214 : 0 : break;
11215 : :
11216 : 1552 : case DW_MACRO_define_strx:
11217 : 1552 : get_uleb128 (u128, readp, readendp);
11218 : 1552 : fprintf (out, "%*s#define ", level, "");
11219 : 1552 : readp = print_form_data (dbg, DW_FORM_strx,
11220 : : readp, readendp, offset_len,
11221 : : str_offsets_base, out);
11222 : 1552 : fprintf (out, ", line %u (strx)\n", u128);
11223 : 1552 : break;
11224 : :
11225 : 4 : case DW_MACRO_undef_strx:
11226 : 4 : get_uleb128 (u128, readp, readendp);
11227 : 4 : fprintf (out, "%*s#undef ", level, "");
11228 : 4 : readp = print_form_data (dbg, DW_FORM_strx,
11229 : : readp, readendp, offset_len,
11230 : : str_offsets_base, out);
11231 : 4 : fprintf (out, ", line %u (strx)\n", u128);
11232 : 4 : break;
11233 : :
11234 : : default:
11235 : 0 : fprintf (out, "%*svendor opcode 0x%" PRIx8, level, "", opcode);
11236 [ # # ]: 0 : if (opcode < DW_MACRO_lo_user
11237 : : || opcode > DW_MACRO_lo_user
11238 [ # # ]: 0 : || vendor[opcode - DW_MACRO_lo_user] == NULL)
11239 : 0 : goto invalid_data;
11240 : :
11241 : 0 : const unsigned char *op_desc;
11242 : 0 : op_desc = vendor[opcode - DW_MACRO_lo_user];
11243 : :
11244 : : // Just skip the arguments, we cannot really interpret them,
11245 : : // but print as much as we can.
11246 : 0 : unsigned int args = *op_desc++;
11247 [ # # # # ]: 0 : while (args > 0 && readp < readendp)
11248 : : {
11249 : 0 : unsigned int form = *op_desc++;
11250 : 0 : readp = print_form_data (dbg, form, readp, readendp,
11251 : : offset_len, str_offsets_base, out);
11252 : 0 : args--;
11253 [ # # ]: 0 : if (args > 0)
11254 : 0 : fprintf (out, ", ");
11255 : : }
11256 : 0 : fputc ('\n', out);
11257 : : }
11258 : :
11259 [ - + ]: 3026 : if (readp + 1 > readendp)
11260 : 0 : goto invalid_data;
11261 : 3026 : opcode = *readp++;
11262 [ + + ]: 3026 : if (opcode == 0)
11263 : 20 : fputc ('\n', out);
11264 : : }
11265 : : }
11266 : : }
11267 : :
11268 : :
11269 : : typedef struct {
11270 : : int n;
11271 : : FILE *out;
11272 : : } pubnames_arg;
11273 : :
11274 : : /* Callback for printing global names. */
11275 : : static int
11276 : 68 : print_pubnames (Dwarf *dbg __attribute__ ((unused)), Dwarf_Global *global,
11277 : : void *arg)
11278 : : {
11279 : 68 : pubnames_arg *p = (pubnames_arg *) arg;
11280 : :
11281 : 136 : fprintf (p->out, _(" [%5d] DIE offset: %6" PRId64
11282 : : ", CU DIE offset: %6" PRId64 ", name: %s\n"),
11283 : 68 : (p->n)++, global->die_offset, global->cu_offset, global->name);
11284 : :
11285 : 68 : return 0;
11286 : : }
11287 : :
11288 : :
11289 : : /* Print the known exported symbols in the DWARF section '.debug_pubnames'. */
11290 : : static void
11291 : 16 : print_debug_pubnames_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
11292 : : Ebl *ebl,
11293 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
11294 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg,
11295 : : FILE *out)
11296 : : {
11297 : : /* Check section actually exists. */
11298 [ - + ]: 16 : if (get_debug_elf_data (dbg, ebl, IDX_debug_pubnames, scn) == NULL)
11299 : 0 : return;
11300 : :
11301 : 16 : fprintf (out, _("\nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
11302 : : elf_ndxscn (scn), section_name (ebl, shdr),
11303 : 16 : (uint64_t) shdr->sh_offset);
11304 : :
11305 : 16 : pubnames_arg arg = { 0, out };
11306 : 16 : (void) dwarf_getpubnames (dbg, print_pubnames, &arg, 0);
11307 : : }
11308 : :
11309 : : /* Print the content of the DWARF string section '.debug_str'
11310 : : or 'debug_line_str'. */
11311 : : static void
11312 : 134 : print_debug_str_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
11313 : : Ebl *ebl,
11314 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
11315 : : Elf_Scn *scn, GElf_Shdr *shdr,
11316 : : Dwarf *dbg __attribute__ ((unused)),
11317 : : FILE *out)
11318 : : {
11319 : 134 : const char *name = section_name (ebl, shdr);
11320 [ + + ]: 134 : int idx = ((name != NULL && strstr (name, "debug_line_str") != NULL)
11321 : : ? IDX_debug_line_str : IDX_debug_str);
11322 : 134 : Elf_Data *data = get_debug_elf_data (dbg, ebl, idx, scn);
11323 [ + - ]: 134 : if (data == NULL)
11324 : : return;
11325 : :
11326 : 134 : const size_t sh_size = data->d_size;
11327 : :
11328 : : /* Compute floor(log16(shdr->sh_size)). */
11329 : 134 : GElf_Addr tmp = sh_size;
11330 : 134 : int digits = 1;
11331 [ + + ]: 466 : while (tmp >= 16)
11332 : : {
11333 : 332 : ++digits;
11334 : 332 : tmp >>= 4;
11335 : : }
11336 : 134 : digits = MAX (4, digits);
11337 : :
11338 : 134 : fprintf (out, _("\nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"
11339 : : " %*s String\n"),
11340 : : elf_ndxscn (scn),
11341 : 134 : section_name (ebl, shdr), (uint64_t) shdr->sh_offset,
11342 : : /* TRANS: the debugstr| prefix makes the string unique. */
11343 [ + - ]: 134 : digits + 2, sgettext ("debugstr|Offset"));
11344 : :
11345 : 134 : Dwarf_Off offset = 0;
11346 [ + + ]: 162348 : while (offset < sh_size)
11347 : : {
11348 : 162214 : size_t len;
11349 : 162214 : const char *str = (const char *) data->d_buf + offset;
11350 : 162214 : const char *endp = memchr (str, '\0', sh_size - offset);
11351 [ - + ]: 162214 : if (unlikely (endp == NULL))
11352 : : {
11353 : 0 : fprintf (out, _(" *** error, missing string terminator\n"));
11354 : 0 : break;
11355 : : }
11356 : :
11357 : 162214 : fprintf (out, " [%*" PRIx64 "] \"%s\"\n",
11358 : : digits, (uint64_t) offset, str);
11359 : 162214 : len = endp - str;
11360 : 162214 : offset += len + 1;
11361 : : }
11362 : : }
11363 : :
11364 : : static void
11365 : 12 : print_debug_str_offsets_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
11366 : : Ebl *ebl,
11367 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
11368 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg,
11369 : : FILE *out)
11370 : : {
11371 : 12 : Elf_Data *data = get_debug_elf_data (dbg, ebl, IDX_debug_str_offsets, scn);
11372 [ + - ]: 12 : if (data == NULL)
11373 : : return;
11374 : :
11375 : 12 : fprintf (out, _("\
11376 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
11377 : : elf_ndxscn (scn), section_name (ebl, shdr),
11378 : 12 : (uint64_t) shdr->sh_offset);
11379 : :
11380 [ + - ]: 12 : if (shdr->sh_size == 0)
11381 : : return;
11382 : :
11383 : 12 : size_t idx = 0;
11384 : 12 : sort_listptr (&known_stroffbases, "str_offsets");
11385 : :
11386 : 12 : const unsigned char *start = (const unsigned char *) data->d_buf;
11387 : 12 : const unsigned char *readp = start;
11388 : 12 : const unsigned char *readendp = ((const unsigned char *) data->d_buf
11389 : 12 : + data->d_size);
11390 : :
11391 [ + + ]: 28 : while (readp < readendp)
11392 : : {
11393 : : /* Most string offset tables will have a header. For split
11394 : : dwarf unit GNU DebugFission didn't add one. But they were
11395 : : also only defined for split units (main or skeleton units
11396 : : didn't have indirect strings). So if we don't have a
11397 : : DW_AT_str_offsets_base at all and this is offset zero, then
11398 : : just start printing offsets immediately, if this is a .dwo
11399 : : section. */
11400 : 16 : Dwarf_Off off = (Dwarf_Off) (readp
11401 : 16 : - (const unsigned char *) data->d_buf);
11402 : :
11403 : 16 : fprintf (out, "Table at offset %" PRIx64 " ", off);
11404 : :
11405 [ + + ]: 16 : struct listptr *listptr = get_listptr (&known_stroffbases, idx++);
11406 : 16 : const unsigned char *next_unitp = readendp;
11407 : 8 : uint8_t offset_size;
11408 : 8 : bool has_header;
11409 [ - + ]: 8 : if (listptr == NULL)
11410 : : {
11411 : : /* This can happen for .dwo files. There is only an header
11412 : : in the case this is a version 5 split DWARF file. */
11413 : 8 : Dwarf_CU *cu;
11414 : 8 : uint8_t unit_type;
11415 [ - + ]: 8 : if (dwarf_get_units (dbg, NULL, &cu, NULL, &unit_type,
11416 : : NULL, NULL) != 0)
11417 : : {
11418 : 0 : error (0, 0, "Warning: Cannot find any DWARF unit.");
11419 : : /* Just guess some values. */
11420 : 0 : has_header = false;
11421 : 0 : offset_size = 4;
11422 : : }
11423 [ + - ]: 8 : else if (off == 0
11424 : 8 : && (unit_type == DW_UT_split_type
11425 [ + - ]: 8 : || unit_type == DW_UT_split_compile))
11426 : : {
11427 : 8 : has_header = cu->version > 4;
11428 : 8 : offset_size = cu->offset_size;
11429 : : }
11430 : : else
11431 : : {
11432 : 0 : error (0, 0,
11433 : : "Warning: No CU references .debug_str_offsets after %"
11434 : : PRIx64, off);
11435 : 0 : has_header = cu->version > 4;
11436 : 0 : offset_size = cu->offset_size;
11437 : : }
11438 : 8 : fprintf (out, "\n");
11439 : : }
11440 : : else
11441 : : {
11442 : : /* This must be DWARF5, since GNU DebugFission didn't define
11443 : : DW_AT_str_offsets_base. */
11444 : 8 : has_header = true;
11445 : :
11446 : 8 : Dwarf_Die cudie;
11447 [ - + ]: 8 : if (dwarf_cu_die (listptr->cu, &cudie,
11448 : : NULL, NULL, NULL, NULL,
11449 : : NULL, NULL) == NULL)
11450 : 0 : fprintf (out, "Unknown CU (%s):\n", dwarf_errmsg (-1));
11451 : : else
11452 : 8 : fprintf (out, "for CU [%6" PRIx64 "]:\n", dwarf_dieoffset (&cudie));
11453 : : }
11454 : :
11455 [ + + ]: 16 : if (has_header)
11456 : : {
11457 : 12 : uint64_t unit_length;
11458 : 12 : uint16_t version;
11459 : 12 : uint16_t padding;
11460 : :
11461 [ - + ]: 12 : unit_length = read_4ubyte_unaligned_inc (dbg, readp);
11462 [ - + ]: 12 : if (unlikely (unit_length == 0xffffffff))
11463 : : {
11464 [ # # ]: 0 : if (unlikely (readp > readendp - 8))
11465 : : {
11466 : 0 : invalid_data:
11467 : 0 : error (0, 0, "Invalid data");
11468 : 0 : return;
11469 : : }
11470 [ # # ]: 0 : unit_length = read_8ubyte_unaligned_inc (dbg, readp);
11471 : 0 : offset_size = 8;
11472 : : }
11473 : : else
11474 : : offset_size = 4;
11475 : :
11476 : 12 : fprintf (out, "\n");
11477 : 12 : fprintf (out, _(" Length: %8" PRIu64 "\n"),
11478 : : unit_length);
11479 : 12 : fprintf (out, _(" Offset size: %8" PRIu8 "\n"),
11480 : : offset_size);
11481 : :
11482 : : /* We need at least 2-bytes (version) + 2-bytes (padding) =
11483 : : 4 bytes to complete the header. And this unit cannot go
11484 : : beyond the section data. */
11485 [ + - ]: 12 : if (readp > readendp - 4
11486 [ + - ]: 12 : || unit_length < 4
11487 [ - + ]: 12 : || unit_length > (uint64_t) (readendp - readp))
11488 : 0 : goto invalid_data;
11489 : :
11490 : 12 : next_unitp = readp + unit_length;
11491 : :
11492 [ - + ]: 12 : version = read_2ubyte_unaligned_inc (dbg, readp);
11493 : 12 : fprintf (out, _(" DWARF version: %8" PRIu16 "\n"), version);
11494 : :
11495 [ - + ]: 12 : if (version != 5)
11496 : : {
11497 : 0 : error (0, 0, _("Unknown version"));
11498 : 0 : goto next_unit;
11499 : : }
11500 : :
11501 [ - + ]: 12 : padding = read_2ubyte_unaligned_inc (dbg, readp);
11502 : 12 : fprintf (out, _(" Padding: %8" PRIx16 "\n"), padding);
11503 : :
11504 [ + + ]: 12 : if (listptr != NULL
11505 [ - + ]: 8 : && listptr->offset != (Dwarf_Off) (readp - start))
11506 : : {
11507 : 0 : error (0, 0, "String offsets index doesn't start after header");
11508 : 0 : goto next_unit;
11509 : : }
11510 : :
11511 : 12 : fprintf (out, "\n");
11512 : : }
11513 : :
11514 : 16 : int digits = 1;
11515 : 16 : size_t offsets = (next_unitp - readp) / offset_size;
11516 [ + + ]: 24 : while (offsets >= 10)
11517 : : {
11518 : 8 : ++digits;
11519 : 8 : offsets /= 10;
11520 : : }
11521 : :
11522 : 16 : unsigned int uidx = 0;
11523 : 16 : size_t index_offset = readp - (const unsigned char *) data->d_buf;
11524 : 16 : fprintf (out, " Offsets start at 0x%zx:\n", index_offset);
11525 [ + + ]: 204 : while (readp <= next_unitp - offset_size)
11526 : : {
11527 : 188 : Dwarf_Word offset;
11528 [ + - ]: 188 : if (offset_size == 4)
11529 [ - + ]: 188 : offset = read_4ubyte_unaligned_inc (dbg, readp);
11530 : : else
11531 [ # # ]: 0 : offset = read_8ubyte_unaligned_inc (dbg, readp);
11532 : 188 : const char *str = dwarf_getstring (dbg, offset, NULL);
11533 [ - + ]: 188 : fprintf (out, " [%*u] [%*" PRIx64 "] \"%s\"\n",
11534 : 188 : digits, uidx++, (int) offset_size * 2, offset, str ?: "???");
11535 : : }
11536 : 16 : fprintf (out, "\n");
11537 : :
11538 [ + - ]: 16 : if (readp != next_unitp)
11539 : 0 : error (0, 0, "extra %zd bytes at end of unit",
11540 : 0 : (size_t) (next_unitp - readp));
11541 : :
11542 : 16 : next_unit:
11543 : : readp = next_unitp;
11544 : : }
11545 : : }
11546 : :
11547 : :
11548 : : /* Print the content of the call frame search table section
11549 : : '.eh_frame_hdr'. */
11550 : : static void
11551 : 140 : print_debug_frame_hdr_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
11552 : : Ebl *ebl __attribute__ ((unused)),
11553 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
11554 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg,
11555 : : FILE *out)
11556 : : {
11557 : 140 : fprintf (out, _("\
11558 : : \nCall frame search table section [%2zu] '.eh_frame_hdr':\n"),
11559 : : elf_ndxscn (scn));
11560 : :
11561 : 140 : Elf_Data *data = elf_rawdata (scn, NULL);
11562 : :
11563 [ - + ]: 140 : if (unlikely (data == NULL))
11564 : : {
11565 : 0 : error (0, 0, _("cannot get %s content: %s"),
11566 : : ".eh_frame_hdr", elf_errmsg (-1));
11567 : 0 : return;
11568 : : }
11569 : :
11570 : 140 : const unsigned char *readp = data->d_buf;
11571 : 140 : const unsigned char *const dataend = ((unsigned char *) data->d_buf
11572 : 140 : + data->d_size);
11573 : :
11574 [ - + ]: 140 : if (unlikely (readp + 4 > dataend))
11575 : : {
11576 : 0 : invalid_data:
11577 : 0 : error (0, 0, _("invalid data"));
11578 : 0 : return;
11579 : : }
11580 : :
11581 : 140 : unsigned int version = *readp++;
11582 : 140 : unsigned int eh_frame_ptr_enc = *readp++;
11583 : 140 : unsigned int fde_count_enc = *readp++;
11584 : 140 : unsigned int table_enc = *readp++;
11585 : :
11586 : 140 : fprintf (out, " version: %u\n"
11587 : : " eh_frame_ptr_enc: %#x ",
11588 : : version, eh_frame_ptr_enc);
11589 : 140 : print_encoding_base ("", eh_frame_ptr_enc, out);
11590 : 140 : fprintf (out, " fde_count_enc: %#x ", fde_count_enc);
11591 : 140 : print_encoding_base ("", fde_count_enc, out);
11592 : 140 : fprintf (out, " table_enc: %#x ", table_enc);
11593 : 140 : print_encoding_base ("", table_enc, out);
11594 : :
11595 : 140 : uint64_t eh_frame_ptr = 0;
11596 [ + - ]: 140 : if (eh_frame_ptr_enc != DW_EH_PE_omit)
11597 : : {
11598 : 140 : readp = read_encoded (eh_frame_ptr_enc, readp, dataend, &eh_frame_ptr,
11599 : : dbg);
11600 [ - + ]: 140 : if (unlikely (readp == NULL))
11601 : 0 : goto invalid_data;
11602 : :
11603 : 140 : fprintf (out, " eh_frame_ptr: %#" PRIx64, eh_frame_ptr);
11604 [ + - ]: 140 : if ((eh_frame_ptr_enc & 0x70) == DW_EH_PE_pcrel)
11605 : 140 : fprintf (out, " (offset: %#" PRIx64 ")",
11606 : : /* +4 because of the 4 byte header of the section. */
11607 : 140 : (uint64_t) shdr->sh_offset + 4 + eh_frame_ptr);
11608 : :
11609 : 140 : fputc ('\n', out);
11610 : : }
11611 : :
11612 : 140 : uint64_t fde_count = 0;
11613 [ + - ]: 140 : if (fde_count_enc != DW_EH_PE_omit)
11614 : : {
11615 : 140 : readp = read_encoded (fde_count_enc, readp, dataend, &fde_count, dbg);
11616 [ - + ]: 140 : if (unlikely (readp == NULL))
11617 : 0 : goto invalid_data;
11618 : :
11619 : 140 : fprintf (out, " fde_count: %" PRIu64 "\n", fde_count);
11620 : : }
11621 : :
11622 [ + - + - ]: 140 : if (fde_count == 0 || table_enc == DW_EH_PE_omit)
11623 : : return;
11624 : :
11625 : 140 : fputs (" Table:\n", out);
11626 : :
11627 : : /* Optimize for the most common case. */
11628 [ + - ]: 140 : if (table_enc == (DW_EH_PE_datarel | DW_EH_PE_sdata4))
11629 [ + + ]: 24214 : while (fde_count > 0 && readp + 8 <= dataend)
11630 : : {
11631 [ - + ]: 24074 : int32_t initial_location = read_4sbyte_unaligned_inc (dbg, readp);
11632 : 24074 : uint64_t initial_offset = ((uint64_t) shdr->sh_offset
11633 : 24074 : + (int64_t) initial_location);
11634 [ - + ]: 24074 : int32_t address = read_4sbyte_unaligned_inc (dbg, readp);
11635 : : // XXX Possibly print symbol name or section offset for initial_offset
11636 : 24074 : fprintf (out, " %#" PRIx32 " (offset: %#6" PRIx64 ") -> %#" PRIx32
11637 : : " fde=[%6" PRIx64 "]\n",
11638 : : initial_location, initial_offset,
11639 : 24074 : address, address - (eh_frame_ptr + 4));
11640 : : }
11641 : : else
11642 : 140 : while (0 && readp < dataend)
11643 : : {
11644 : :
11645 : 140 : }
11646 : : }
11647 : :
11648 : :
11649 : : /* Print the content of the exception handling table section
11650 : : '.eh_frame_hdr'. */
11651 : : static void
11652 : 0 : print_debug_exception_table (Dwfl_Module *dwflmod __attribute__ ((unused)),
11653 : : Ebl *ebl __attribute__ ((unused)),
11654 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
11655 : : Elf_Scn *scn,
11656 : : GElf_Shdr *shdr __attribute__ ((unused)),
11657 : : Dwarf *dbg __attribute__ ((unused)),
11658 : : FILE *out)
11659 : : {
11660 : 0 : fprintf (out, _("\
11661 : : \nException handling table section [%2zu] '.gcc_except_table':\n"),
11662 : : elf_ndxscn (scn));
11663 : :
11664 : 0 : Elf_Data *data = elf_rawdata (scn, NULL);
11665 : :
11666 [ # # ]: 0 : if (unlikely (data == NULL))
11667 : : {
11668 : 0 : error (0, 0, _("cannot get %s content: %s"),
11669 : : ".gcc_except_table", elf_errmsg (-1));
11670 : 0 : return;
11671 : : }
11672 : :
11673 : 0 : const unsigned char *readp = data->d_buf;
11674 : 0 : const unsigned char *const dataend = readp + data->d_size;
11675 : :
11676 [ # # ]: 0 : if (unlikely (readp + 1 > dataend))
11677 : : {
11678 : 0 : invalid_data:
11679 : 0 : error (0, 0, _("invalid data"));
11680 : 0 : return;
11681 : : }
11682 : 0 : unsigned int lpstart_encoding = *readp++;
11683 : 0 : fprintf (out, _(" LPStart encoding: %#x "), lpstart_encoding);
11684 : 0 : print_encoding_base ("", lpstart_encoding, out);
11685 [ # # ]: 0 : if (lpstart_encoding != DW_EH_PE_omit)
11686 : : {
11687 : 0 : uint64_t lpstart;
11688 : 0 : readp = read_encoded (lpstart_encoding, readp, dataend, &lpstart, dbg);
11689 : 0 : fprintf (out, " LPStart: %#" PRIx64 "\n", lpstart);
11690 : : }
11691 : :
11692 [ # # ]: 0 : if (unlikely (readp + 1 > dataend))
11693 : 0 : goto invalid_data;
11694 : 0 : unsigned int ttype_encoding = *readp++;
11695 : 0 : fprintf (out, _(" TType encoding: %#x "), ttype_encoding);
11696 : 0 : print_encoding_base ("", ttype_encoding, out);
11697 : 0 : const unsigned char *ttype_base = NULL;
11698 [ # # ]: 0 : if (ttype_encoding != DW_EH_PE_omit)
11699 : : {
11700 : 0 : unsigned int ttype_base_offset;
11701 [ # # ]: 0 : if (readp >= dataend)
11702 : 0 : goto invalid_data;
11703 : 0 : get_uleb128 (ttype_base_offset, readp, dataend);
11704 : 0 : fprintf (out, " TType base offset: %#x\n", ttype_base_offset);
11705 [ # # ]: 0 : if ((size_t) (dataend - readp) > ttype_base_offset)
11706 : 0 : ttype_base = readp + ttype_base_offset;
11707 : : }
11708 : :
11709 [ # # ]: 0 : if (unlikely (readp + 1 > dataend))
11710 : 0 : goto invalid_data;
11711 : 0 : unsigned int call_site_encoding = *readp++;
11712 : 0 : fprintf (out, _(" Call site encoding: %#x "), call_site_encoding);
11713 : 0 : print_encoding_base ("", call_site_encoding, out);
11714 : 0 : unsigned int call_site_table_len;
11715 [ # # ]: 0 : if (readp >= dataend)
11716 : 0 : goto invalid_data;
11717 : 0 : get_uleb128 (call_site_table_len, readp, dataend);
11718 : :
11719 : 0 : const unsigned char *const action_table = readp + call_site_table_len;
11720 [ # # ]: 0 : if (unlikely (action_table > dataend))
11721 : 0 : goto invalid_data;
11722 : : unsigned int u = 0;
11723 : : unsigned int max_action = 0;
11724 [ # # ]: 0 : while (readp < action_table)
11725 : : {
11726 [ # # ]: 0 : if (u == 0)
11727 : 0 : fputs (_("\n Call site table:"), out);
11728 : :
11729 : 0 : uint64_t call_site_start;
11730 : 0 : readp = read_encoded (call_site_encoding, readp, dataend,
11731 : : &call_site_start, dbg);
11732 : 0 : uint64_t call_site_length;
11733 : 0 : readp = read_encoded (call_site_encoding, readp, dataend,
11734 : : &call_site_length, dbg);
11735 : 0 : uint64_t landing_pad;
11736 : 0 : readp = read_encoded (call_site_encoding, readp, dataend,
11737 : : &landing_pad, dbg);
11738 : 0 : unsigned int action;
11739 [ # # ]: 0 : if (readp >= dataend)
11740 : 0 : goto invalid_data;
11741 : 0 : get_uleb128 (action, readp, dataend);
11742 : 0 : max_action = MAX (action, max_action);
11743 : 0 : fprintf (out, _(" [%4u] Call site start: %#" PRIx64 "\n"
11744 : : " Call site length: %" PRIu64 "\n"
11745 : : " Landing pad: %#" PRIx64 "\n"
11746 : : " Action: %u\n"),
11747 : : u++, call_site_start, call_site_length, landing_pad, action);
11748 : : }
11749 [ # # ]: 0 : if (readp != action_table)
11750 : 0 : goto invalid_data;
11751 : :
11752 : 0 : unsigned int max_ar_filter = 0;
11753 [ # # ]: 0 : if (max_action > 0)
11754 : : {
11755 : 0 : fputs ("\n Action table:", out);
11756 : :
11757 : 0 : size_t maxdata = (size_t) (dataend - action_table);
11758 [ # # # # ]: 0 : if (max_action > maxdata || maxdata - max_action < 1)
11759 : : {
11760 : 0 : invalid_action_table:
11761 : 0 : fputs (_(" <INVALID DATA>\n"), stdout);
11762 : 0 : return;
11763 : : }
11764 : :
11765 : 0 : const unsigned char *const action_table_end
11766 : 0 : = action_table + max_action + 1;
11767 : :
11768 : 0 : u = 0;
11769 : 0 : do
11770 : : {
11771 : 0 : int ar_filter;
11772 : 0 : get_sleb128 (ar_filter, readp, action_table_end);
11773 [ # # ]: 0 : if (ar_filter > 0 && (unsigned int) ar_filter > max_ar_filter)
11774 : 0 : max_ar_filter = ar_filter;
11775 : 0 : int ar_disp;
11776 [ # # ]: 0 : if (readp >= action_table_end)
11777 : 0 : goto invalid_action_table;
11778 : 0 : get_sleb128 (ar_disp, readp, action_table_end);
11779 : :
11780 : 0 : fprintf (out, " [%4u] ar_filter: % d\n"
11781 : : " ar_disp: % -5d",
11782 : : u, ar_filter, ar_disp);
11783 [ # # ]: 0 : if (abs (ar_disp) & 1)
11784 : 0 : fprintf (out, " -> [%4u]\n", u + (ar_disp + 1) / 2);
11785 [ # # ]: 0 : else if (ar_disp != 0)
11786 : 0 : fputs (" -> ???", out);
11787 : : else
11788 : 0 : fputc ('\n', out);
11789 : 0 : ++u;
11790 : : }
11791 [ # # ]: 0 : while (readp < action_table_end);
11792 : : }
11793 : :
11794 [ # # ]: 0 : if (max_ar_filter > 0 && ttype_base != NULL)
11795 : : {
11796 : 0 : unsigned char dsize;
11797 : 0 : fputs ("\n TType table:", out);
11798 : :
11799 : : // XXX Not *4, size of encoding;
11800 [ # # ]: 0 : switch (ttype_encoding & 7)
11801 : : {
11802 : : case DW_EH_PE_udata2:
11803 : : case DW_EH_PE_sdata2:
11804 : : dsize = 2;
11805 : : break;
11806 : : case DW_EH_PE_udata4:
11807 : : case DW_EH_PE_sdata4:
11808 : : dsize = 4;
11809 : : break;
11810 : : case DW_EH_PE_udata8:
11811 : : case DW_EH_PE_sdata8:
11812 : : dsize = 8;
11813 : : break;
11814 : 0 : default:
11815 : 0 : dsize = 0;
11816 : 0 : error (1, 0, _("invalid TType encoding"));
11817 : : }
11818 : :
11819 : 0 : if (max_ar_filter
11820 [ # # ]: 0 : > (size_t) (ttype_base - (const unsigned char *) data->d_buf) / dsize)
11821 : 0 : goto invalid_data;
11822 : :
11823 : 0 : readp = ttype_base - max_ar_filter * dsize;
11824 : 0 : do
11825 : : {
11826 : 0 : uint64_t ttype;
11827 : 0 : readp = read_encoded (ttype_encoding, readp, ttype_base, &ttype,
11828 : : dbg);
11829 : 0 : fprintf (out, " [%4u] %#" PRIx64 "\n", max_ar_filter--, ttype);
11830 : : }
11831 [ # # ]: 0 : while (readp < ttype_base);
11832 : : }
11833 : : }
11834 : :
11835 : : /* Print the content of the '.gdb_index' section.
11836 : : http://sourceware.org/gdb/current/onlinedocs/gdb/Index-Section-Format.html
11837 : : */
11838 : : static void
11839 : 8 : print_gdb_index_section (Dwfl_Module *dwflmod, Ebl *ebl,
11840 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
11841 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg,
11842 : : FILE *out)
11843 : : {
11844 : 8 : fprintf (out, _("\nGDB section [%2zu] '%s' at offset %#" PRIx64
11845 : : " contains %" PRId64 " bytes :\n"),
11846 : : elf_ndxscn (scn), section_name (ebl, shdr),
11847 : 8 : (uint64_t) shdr->sh_offset, (uint64_t) shdr->sh_size);
11848 : :
11849 : 8 : Elf_Data *data = elf_rawdata (scn, NULL);
11850 : :
11851 [ - + ]: 8 : if (unlikely (data == NULL))
11852 : : {
11853 : 0 : error (0, 0, _("cannot get %s content: %s"),
11854 : : ".gdb_index", elf_errmsg (-1));
11855 : 4 : return;
11856 : : }
11857 : :
11858 : : // .gdb_index is always in little endian.
11859 : 8 : Dwarf dummy_dbg = { .other_byte_order = MY_ELFDATA != ELFDATA2LSB };
11860 : 8 : dbg = &dummy_dbg;
11861 : :
11862 : 8 : const unsigned char *readp = data->d_buf;
11863 : 8 : const unsigned char *const dataend = readp + data->d_size;
11864 : :
11865 [ - + ]: 8 : if (unlikely (readp + 4 > dataend))
11866 : : {
11867 : 0 : invalid_data:
11868 : 0 : error (0, 0, _("invalid data"));
11869 : 0 : return;
11870 : : }
11871 : :
11872 : 8 : int32_t vers = read_4ubyte_unaligned (dbg, readp);
11873 : 8 : fprintf (out, _(" Version: %" PRId32 "\n"), vers);
11874 : :
11875 : : // The only difference between version 4 and version 5 is the
11876 : : // hash used for generating the table. Version 6 contains symbols
11877 : : // for inlined functions, older versions didn't. Version 7 adds
11878 : : // symbol kinds. Version 8 just indicates that it correctly includes
11879 : : // TUs for symbols. Version 9 adds shortcut table for information
11880 : : // regarding the main function.
11881 [ - + ]: 8 : if (vers < 4 || vers > 9)
11882 : : {
11883 : 0 : fprintf (out, _(" unknown version, cannot parse section\n"));
11884 : 0 : return;
11885 : : }
11886 : :
11887 : 8 : readp += 4;
11888 [ - + ]: 8 : if (unlikely (readp + 4 > dataend))
11889 : 0 : goto invalid_data;
11890 : :
11891 : 8 : uint32_t cu_off = read_4ubyte_unaligned (dbg, readp);
11892 : 8 : fprintf (out, _(" CU offset: %#" PRIx32 "\n"), cu_off);
11893 : :
11894 : 8 : readp += 4;
11895 [ - + ]: 8 : if (unlikely (readp + 4 > dataend))
11896 : 0 : goto invalid_data;
11897 : :
11898 : 8 : uint32_t tu_off = read_4ubyte_unaligned (dbg, readp);
11899 : 8 : fprintf (out, _(" TU offset: %#" PRIx32 "\n"), tu_off);
11900 : :
11901 : 8 : readp += 4;
11902 [ - + ]: 8 : if (unlikely (readp + 4 > dataend))
11903 : 0 : goto invalid_data;
11904 : :
11905 : 8 : uint32_t addr_off = read_4ubyte_unaligned (dbg, readp);
11906 : 8 : fprintf (out, _(" address offset: %#" PRIx32 "\n"), addr_off);
11907 : :
11908 : 8 : readp += 4;
11909 [ - + ]: 8 : if (unlikely (readp + 4 > dataend))
11910 : 0 : goto invalid_data;
11911 : :
11912 : 8 : uint32_t sym_off = read_4ubyte_unaligned (dbg, readp);
11913 : 8 : fprintf (out, _(" symbol offset: %#" PRIx32 "\n"), sym_off);
11914 : :
11915 : 8 : readp += 4;
11916 [ - + ]: 8 : if (unlikely (readp + 4 > dataend))
11917 : 0 : goto invalid_data;
11918 : :
11919 : 8 : uint32_t shortcut_off = 0;
11920 [ + + ]: 8 : if (vers >= 9)
11921 : : {
11922 : 4 : shortcut_off = read_4ubyte_unaligned (dbg, readp);
11923 : 4 : fprintf (out, _(" shortcut offset: %#" PRIx32 "\n"), shortcut_off);
11924 : :
11925 : 4 : readp += 4;
11926 [ - + ]: 4 : if (unlikely (readp + 4 > dataend))
11927 : 0 : goto invalid_data;
11928 : : }
11929 : :
11930 : 8 : uint32_t const_off = read_4ubyte_unaligned (dbg, readp);
11931 : 8 : fprintf (out, _(" constant offset: %#" PRIx32 "\n"), const_off);
11932 : :
11933 [ - + ]: 8 : if (unlikely ((size_t) (dataend - (const unsigned char *) data->d_buf)
11934 : : < const_off))
11935 : 0 : goto invalid_data;
11936 : :
11937 : 8 : readp = data->d_buf + cu_off;
11938 : :
11939 : 8 : const unsigned char *nextp = data->d_buf + tu_off;
11940 [ - + ]: 8 : if (tu_off >= data->d_size)
11941 : 0 : goto invalid_data;
11942 : :
11943 : 8 : size_t cu_nr = (nextp - readp) / 16;
11944 : :
11945 : 8 : fprintf (out, _("\n CU list at offset %#" PRIx32
11946 : : " contains %zu entries:\n"),
11947 : : cu_off, cu_nr);
11948 : :
11949 : 8 : size_t n = 0;
11950 [ + - + + ]: 22 : while (dataend - readp >= 16 && n < cu_nr)
11951 : : {
11952 : 14 : uint64_t off = read_8ubyte_unaligned (dbg, readp);
11953 : 14 : readp += 8;
11954 : :
11955 : 14 : uint64_t len = read_8ubyte_unaligned (dbg, readp);
11956 : 14 : readp += 8;
11957 : :
11958 : 14 : fprintf (out, " [%4zu] start: %0#8" PRIx64
11959 : : ", length: %5" PRIu64 "\n", n, off, len);
11960 : 14 : n++;
11961 : : }
11962 : :
11963 : 8 : readp = data->d_buf + tu_off;
11964 : 8 : nextp = data->d_buf + addr_off;
11965 [ - + ]: 8 : if (addr_off >= data->d_size)
11966 : 0 : goto invalid_data;
11967 : :
11968 : 8 : size_t tu_nr = (nextp - readp) / 24;
11969 : :
11970 : 8 : fprintf (out, _("\n TU list at offset %#" PRIx32
11971 : : " contains %zu entries:\n"),
11972 : : tu_off, tu_nr);
11973 : :
11974 : 8 : n = 0;
11975 [ + - + + ]: 14 : while (dataend - readp >= 24 && n < tu_nr)
11976 : : {
11977 : 6 : uint64_t off = read_8ubyte_unaligned (dbg, readp);
11978 : 6 : readp += 8;
11979 : :
11980 : 6 : uint64_t type = read_8ubyte_unaligned (dbg, readp);
11981 : 6 : readp += 8;
11982 : :
11983 : 6 : uint64_t sig = read_8ubyte_unaligned (dbg, readp);
11984 : 6 : readp += 8;
11985 : :
11986 : 6 : fprintf (out, " [%4zu] CU offset: %5" PRId64
11987 : : ", type offset: %5" PRId64
11988 : : ", signature: %0#8" PRIx64 "\n", n, off, type, sig);
11989 : 6 : n++;
11990 : : }
11991 : :
11992 : 8 : readp = data->d_buf + addr_off;
11993 : 8 : nextp = data->d_buf + sym_off;
11994 [ - + ]: 8 : if (sym_off >= data->d_size)
11995 : 0 : goto invalid_data;
11996 : :
11997 : 8 : size_t addr_nr = (nextp - readp) / 20;
11998 : :
11999 : 8 : fprintf (out, _("\n Address list at offset %#" PRIx32
12000 : : " contains %zu entries:\n"),
12001 : : addr_off, addr_nr);
12002 : :
12003 : 8 : n = 0;
12004 [ + - + + ]: 22 : while (dataend - readp >= 20 && n < addr_nr)
12005 : : {
12006 : 14 : uint64_t low = read_8ubyte_unaligned (dbg, readp);
12007 : 14 : readp += 8;
12008 : :
12009 : 14 : uint64_t high = read_8ubyte_unaligned (dbg, readp);
12010 : 14 : readp += 8;
12011 : :
12012 : 14 : uint32_t idx = read_4ubyte_unaligned (dbg, readp);
12013 : 14 : readp += 4;
12014 : :
12015 : 14 : fprintf (out, " [%4zu] ", n);
12016 : 14 : print_dwarf_addr (dwflmod, 8, low, low, out);
12017 : 14 : fprintf (out, "..");
12018 : 14 : print_dwarf_addr (dwflmod, 8, high - 1, high, out);
12019 : 14 : fprintf (out, ", CU index: %5" PRId32 "\n", idx);
12020 : 14 : n++;
12021 : : }
12022 : :
12023 : 8 : const unsigned char *const_start = data->d_buf + const_off;
12024 [ - + ]: 8 : if (const_off > data->d_size)
12025 : 0 : goto invalid_data;
12026 : :
12027 : 8 : const unsigned char *shortcut_start = NULL;
12028 [ + + ]: 8 : if (vers >= 9)
12029 : : {
12030 [ - + ]: 4 : if (shortcut_off >= data->d_size)
12031 : 0 : goto invalid_data;
12032 : :
12033 : 4 : shortcut_start = data->d_buf + shortcut_off;
12034 : 4 : nextp = shortcut_start;
12035 : : }
12036 : : else
12037 : : nextp = const_start;
12038 : :
12039 : 8 : readp = data->d_buf + sym_off;
12040 : 8 : size_t sym_nr = (nextp - readp) / 8;
12041 : :
12042 : 8 : fprintf (out, _("\n Symbol table at offset %#" PRIx32
12043 : : " contains %zu slots:\n"),
12044 : : sym_off, sym_nr);
12045 : :
12046 : 8 : n = 0;
12047 [ + - + + ]: 8200 : while (dataend - readp >= 8 && n < sym_nr)
12048 : : {
12049 : 8192 : uint32_t name = read_4ubyte_unaligned (dbg, readp);
12050 : 8192 : readp += 4;
12051 : :
12052 : 8192 : uint32_t vector = read_4ubyte_unaligned (dbg, readp);
12053 : 8192 : readp += 4;
12054 : :
12055 [ + + ]: 8192 : if (name != 0 || vector != 0)
12056 : : {
12057 : 60 : const unsigned char *sym = const_start + name;
12058 [ + - - + ]: 60 : if (unlikely ((size_t) (dataend - const_start) < name
12059 : : || memchr (sym, '\0', dataend - sym) == NULL))
12060 : 0 : goto invalid_data;
12061 : :
12062 : 60 : fprintf (out, " [%4zu] symbol: %s, CUs: ", n, sym);
12063 : :
12064 : 60 : const unsigned char *readcus = const_start + vector;
12065 [ - + ]: 60 : if (unlikely ((size_t) (dataend - const_start) < vector))
12066 : 0 : goto invalid_data;
12067 : 60 : uint32_t cus = read_4ubyte_unaligned (dbg, readcus);
12068 [ + + ]: 128 : while (cus--)
12069 : : {
12070 : 68 : uint32_t cu_kind, cu, kind;
12071 : 68 : bool is_static;
12072 : 68 : readcus += 4;
12073 [ - + ]: 68 : if (unlikely (readcus + 4 > dataend))
12074 : 0 : goto invalid_data;
12075 : 68 : cu_kind = read_4ubyte_unaligned (dbg, readcus);
12076 : 68 : cu = cu_kind & ((1 << 24) - 1);
12077 : 68 : kind = (cu_kind >> 28) & 7;
12078 : 68 : is_static = cu_kind & (1U << 31);
12079 [ + + ]: 68 : if (cu > cu_nr - 1)
12080 : 10 : fprintf (out, "%" PRId32 "T", cu - (uint32_t) cu_nr);
12081 : : else
12082 : 58 : fprintf (out, "%" PRId32, cu);
12083 [ + + ]: 68 : if (kind != 0)
12084 : : {
12085 : 52 : fprintf (out, " (");
12086 [ + + + - : 52 : switch (kind)
- ]
12087 : : {
12088 : : case 1:
12089 : 20 : fprintf (out, "type");
12090 : 20 : break;
12091 : : case 2:
12092 : 16 : fprintf (out, "var");
12093 : 16 : break;
12094 : : case 3:
12095 : 16 : fprintf (out, "func");
12096 : 16 : break;
12097 : : case 4:
12098 : 0 : fprintf (out, "other");
12099 : 0 : break;
12100 : : default:
12101 : 0 : fprintf (out, "unknown-0x%" PRIx32, kind);
12102 : 0 : break;
12103 : : }
12104 [ + + ]: 68 : fprintf (out, ":%c)", (is_static ? 'S' : 'G'));
12105 : : }
12106 [ + + ]: 68 : if (cus > 0)
12107 : 8 : fprintf (out, ", ");
12108 : : }
12109 : 60 : fprintf (out, "\n");
12110 : : }
12111 : 8192 : n++;
12112 : : }
12113 : :
12114 [ + + ]: 8 : if (vers < 9)
12115 : : return;
12116 : :
12117 [ - + ]: 4 : if (unlikely (shortcut_start == NULL))
12118 : 0 : goto invalid_data;
12119 : :
12120 : 4 : readp = shortcut_start;
12121 : 4 : nextp = const_start;
12122 : 4 : size_t shortcut_nr = (nextp - readp) / 4;
12123 : :
12124 [ - + ]: 4 : if (unlikely (shortcut_nr != 2))
12125 : 0 : goto invalid_data;
12126 : :
12127 : 8 : fprintf (out,
12128 : 4 : _("\nShortcut table at offset %#" PRIx32 " contains %zu slots:\n"),
12129 : : shortcut_off, shortcut_nr);
12130 : :
12131 : 4 : uint32_t lang = read_4ubyte_unaligned (dbg, readp);
12132 : 4 : readp += 4;
12133 : :
12134 : : /* Include the hex number of LANG in the output if the language
12135 : : is unknown. */
12136 : 4 : const char *lang_str = dwarf_lang_string (lang);
12137 : 4 : lang_str = string_or_unknown (lang_str, lang, DW_LANG_lo_user,
12138 : : DW_LANG_hi_user, true);
12139 : :
12140 : 4 : fprintf (out, _("Language of main: %s\n"), lang_str);
12141 : 4 : fprintf (out, _("Name of main: "));
12142 : :
12143 [ + + ]: 4 : if (lang != 0)
12144 : : {
12145 : 2 : uint32_t name = read_4ubyte_unaligned (dbg, readp);
12146 : 2 : readp += 4;
12147 : 2 : const unsigned char *sym = const_start + name;
12148 : :
12149 [ + - - + ]: 2 : if (unlikely ((size_t) (dataend - const_start) < name
12150 : : || memchr (sym, '\0', dataend - sym) == NULL))
12151 : 0 : goto invalid_data;
12152 : :
12153 : 2 : fprintf (out, "%s\n", sym);
12154 : : }
12155 : : else
12156 : 2 : fprintf (out, "<unknown>\n");
12157 : : }
12158 : :
12159 : : /* Returns true and sets split DWARF CU id if there is a split compile
12160 : : unit in the given Dwarf, and no non-split units are found (before it). */
12161 : : static bool
12162 : 396 : is_split_dwarf (Dwarf *dbg, uint64_t *id, Dwarf_CU **split_cu)
12163 : : {
12164 : 396 : Dwarf_CU *cu = NULL;
12165 [ + + ]: 396 : while (dwarf_get_units (dbg, cu, &cu, NULL, NULL, NULL, NULL) == 0)
12166 : : {
12167 : 394 : uint8_t unit_type;
12168 [ + - ]: 394 : if (dwarf_cu_info (cu, NULL, &unit_type, NULL, NULL,
12169 : : id, NULL, NULL) != 0)
12170 : 394 : return false;
12171 : :
12172 [ + + ]: 394 : if (unit_type != DW_UT_split_compile && unit_type != DW_UT_split_type)
12173 : : return false;
12174 : :
12175 : : /* We really only care about the split compile unit, the types
12176 : : should be fine and self sufficient. Also they don't have an
12177 : : id that we can match with a skeleton unit. */
12178 [ + - ]: 18 : if (unit_type == DW_UT_split_compile)
12179 : : {
12180 : 18 : *split_cu = cu;
12181 : 18 : return true;
12182 : : }
12183 : : }
12184 : :
12185 : : return false;
12186 : : }
12187 : :
12188 : : /* Check that there is one and only one Dwfl_Module, return in arg. */
12189 : : static int
12190 : 18 : getone_dwflmod (Dwfl_Module *dwflmod,
12191 : : void **userdata __attribute__ ((unused)),
12192 : : const char *name __attribute__ ((unused)),
12193 : : Dwarf_Addr base __attribute__ ((unused)),
12194 : : void *arg)
12195 : : {
12196 : 18 : Dwfl_Module **m = (Dwfl_Module **) arg;
12197 [ + - ]: 18 : if (*m != NULL)
12198 : : return DWARF_CB_ABORT;
12199 : 18 : *m = dwflmod;
12200 : 18 : return DWARF_CB_OK;
12201 : : }
12202 : :
12203 : : typedef struct {
12204 : : Dwfl_Module *dwflmod;
12205 : : Ebl *ebl;
12206 : : GElf_Ehdr *ehdr;
12207 : : Elf_Scn scn;
12208 : : GElf_Shdr shdr;
12209 : : Dwarf *dbg;
12210 : : FILE *out;
12211 : : void (*fp) (Dwfl_Module *, Ebl *, GElf_Ehdr *,
12212 : : Elf_Scn *, GElf_Shdr *, Dwarf *, FILE *);
12213 : : } job_data;
12214 : :
12215 : : #ifdef USE_LOCKS
12216 : :
12217 : : /* Thread entry point. */
12218 : : static void *
12219 : : do_job (void *data, FILE *out)
12220 : : {
12221 : : job_data *d = (job_data *) data;
12222 : : d->fp (d->dwflmod, d->ebl, d->ehdr, &d->scn, &d->shdr, d->dbg, out);
12223 : : return NULL;
12224 : : }
12225 : : #endif
12226 : :
12227 : : /* If readelf is built with thread safety, then set up JDATA at index IDX
12228 : : and add it to the job queue.
12229 : :
12230 : : If thread safety is not supported or the maximum number of threads is set
12231 : : to 1, then immediately call START_ROUTINE with the given arguments. */
12232 : : static void
12233 : 1094 : schedule_job (job_data jdata[], size_t idx,
12234 : : void (*start_routine) (Dwfl_Module *, Ebl *, GElf_Ehdr *,
12235 : : Elf_Scn *, GElf_Shdr *, Dwarf *, FILE *),
12236 : : Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr, Elf_Scn *scn,
12237 : : GElf_Shdr *shdr, Dwarf *dbg)
12238 : : {
12239 : : #ifdef USE_LOCKS
12240 : : if (max_threads > 1)
12241 : : {
12242 : : /* Add to the job queue. */
12243 : : jdata[idx].dwflmod = dwflmod;
12244 : : jdata[idx].ebl = ebl;
12245 : : jdata[idx].ehdr = ehdr;
12246 : : jdata[idx].scn = *scn;
12247 : : jdata[idx].shdr = *shdr;
12248 : : jdata[idx].dbg = dbg;
12249 : : jdata[idx].fp = start_routine;
12250 : :
12251 : : add_job (do_job, (void *) &jdata[idx]);
12252 : : }
12253 : : else
12254 : : start_routine (dwflmod, ebl, ehdr, scn, shdr, dbg, stdout);
12255 : : #else
12256 : 1094 : (void) jdata; (void) idx;
12257 : :
12258 : 1094 : start_routine (dwflmod, ebl, ehdr, scn, shdr, dbg, stdout);
12259 : : #endif
12260 : 1094 : }
12261 : :
12262 : : static void
12263 : 502 : print_debug (Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr)
12264 : : {
12265 : : /* Used for skeleton file, if necessary for split DWARF. */
12266 : 502 : Dwfl *skel_dwfl = NULL;
12267 : 502 : Dwfl_Module *skel_mod = NULL;
12268 : 502 : char *skel_name = NULL;
12269 : 502 : Dwarf *split_dbg = NULL;
12270 : 502 : Dwarf_CU *split_cu = NULL;
12271 : :
12272 : : /* Before we start the real work get a debug context descriptor. */
12273 : 502 : Dwarf_Addr dwbias;
12274 : 502 : Dwarf *dbg = dwfl_module_getdwarf (dwflmod, &dwbias);
12275 : 502 : Dwarf dummy_dbg =
12276 : : {
12277 : 502 : .elf = ebl->elf,
12278 : 502 : .other_byte_order = MY_ELFDATA != ehdr->e_ident[EI_DATA]
12279 : 502 : };
12280 [ + + ]: 502 : if (dbg == NULL)
12281 : : {
12282 [ - + ]: 106 : if ((print_debug_sections & ~(section_exception|section_frame)) != 0)
12283 : 0 : error (0, 0, _("cannot get debug context descriptor: %s"),
12284 : : dwfl_errmsg (-1));
12285 : : dbg = &dummy_dbg;
12286 : : }
12287 : : else
12288 : : {
12289 : : /* If we are asked about a split dwarf (.dwo) file, use the user
12290 : : provided, or find the corresponding skeleton file. If we got
12291 : : a skeleton file, replace the given dwflmod and dbg, with one
12292 : : derived from the skeleton file to provide enough context. */
12293 : 396 : uint64_t split_id;
12294 [ + + ]: 396 : if (is_split_dwarf (dbg, &split_id, &split_cu))
12295 : : {
12296 [ + - ]: 18 : if (dwarf_skeleton != NULL)
12297 : 18 : skel_name = strdup (dwarf_skeleton);
12298 : : else
12299 : : {
12300 : : /* Replace file.dwo with file.o and see if that matches. */
12301 : 0 : const char *fname;
12302 : 0 : dwfl_module_info (dwflmod, NULL, NULL, NULL, NULL, NULL,
12303 : : &fname, NULL);
12304 [ # # ]: 0 : if (fname != NULL)
12305 : : {
12306 : 0 : size_t flen = strlen (fname);
12307 [ # # # # ]: 0 : if (flen > 4 && strcmp (".dwo", fname + flen - 4) == 0)
12308 : : {
12309 : 0 : skel_name = strdup (fname);
12310 [ # # ]: 0 : if (skel_name != NULL)
12311 : : {
12312 : 0 : skel_name[flen - 3] = 'o';
12313 : 0 : skel_name[flen - 2] = '\0';
12314 : : }
12315 : : }
12316 : : }
12317 : : }
12318 : :
12319 [ - + ]: 18 : if (skel_name != NULL)
12320 : : {
12321 : 18 : int skel_fd = open (skel_name, O_RDONLY);
12322 [ - + ]: 18 : if (skel_fd == -1)
12323 : 0 : fprintf (stderr, "Warning: Couldn't open DWARF skeleton file"
12324 : : " '%s'\n", skel_name);
12325 : : else
12326 : : {
12327 : 18 : skel_dwfl = create_dwfl (skel_fd, skel_name);
12328 : :
12329 : : /* skel_fd was dup'ed by create_dwfl. We can close the
12330 : : original now. */
12331 : 18 : close (skel_fd);
12332 : : }
12333 : :
12334 [ + - ]: 18 : if (skel_dwfl != NULL)
12335 : : {
12336 [ - + ]: 18 : if (dwfl_getmodules (skel_dwfl, &getone_dwflmod,
12337 : : &skel_mod, 0) != 0)
12338 : : {
12339 : 0 : fprintf (stderr, "Warning: Bad DWARF skeleton,"
12340 : : " multiple modules '%s'\n", skel_name);
12341 : 0 : dwfl_end (skel_dwfl);
12342 : 0 : skel_mod = NULL;
12343 : : }
12344 : : }
12345 [ # # ]: 0 : else if (skel_fd != -1)
12346 : 0 : fprintf (stderr, "Warning: Couldn't create skeleton dwfl for"
12347 : : " '%s': %s\n", skel_name, dwfl_errmsg (-1));
12348 : :
12349 [ + - ]: 18 : if (skel_mod != NULL)
12350 : : {
12351 : 18 : Dwarf *skel_dbg = dwfl_module_getdwarf (skel_mod, &dwbias);
12352 [ + - ]: 18 : if (skel_dbg != NULL)
12353 : : {
12354 : : /* First check the skeleton CU DIE, only fetch
12355 : : the split DIE if we know the id matches to
12356 : : not unnecessary search for any split DIEs we
12357 : : don't need. */
12358 : 18 : Dwarf_CU *cu = NULL;
12359 : 18 : while (dwarf_get_units (skel_dbg, cu, &cu,
12360 [ + - ]: 26 : NULL, NULL, NULL, NULL) == 0)
12361 : : {
12362 : 26 : uint8_t unit_type;
12363 : 26 : uint64_t skel_id;
12364 [ + - ]: 26 : if (dwarf_cu_info (cu, NULL, &unit_type, NULL, NULL,
12365 : : &skel_id, NULL, NULL) == 0
12366 [ + - ]: 26 : && unit_type == DW_UT_skeleton
12367 [ + + ]: 26 : && split_id == skel_id)
12368 : : {
12369 : 18 : Dwarf_Die subdie;
12370 [ + - ]: 18 : if (dwarf_cu_info (cu, NULL, NULL, NULL,
12371 : : &subdie,
12372 : : NULL, NULL, NULL) == 0
12373 [ + - ]: 18 : && dwarf_tag (&subdie) != DW_TAG_invalid)
12374 : : {
12375 : 18 : split_dbg = dwarf_cu_getdwarf (subdie.cu);
12376 [ - + ]: 18 : if (split_dbg == NULL)
12377 : 0 : fprintf (stderr,
12378 : : "Warning: Couldn't get split_dbg:"
12379 : : " %s\n", dwarf_errmsg (-1));
12380 : 0 : break;
12381 : : }
12382 : : else
12383 : : {
12384 : : /* Everything matches up, but not
12385 : : according to libdw. Which means
12386 : : the user knew better. So...
12387 : : Terrible hack... We can never
12388 : : destroy the underlying dwfl
12389 : : because it would free the wrong
12390 : : Dwarfs... So we leak memory...*/
12391 [ # # ]: 0 : if (cu->split == NULL
12392 [ # # ]: 0 : && dwarf_skeleton != NULL)
12393 : : {
12394 : 0 : do_not_close_dwfl = true;
12395 : 0 : __libdw_link_skel_split (cu, split_cu);
12396 : 0 : split_dbg = dwarf_cu_getdwarf (split_cu);
12397 : 0 : break;
12398 : : }
12399 : : else
12400 : 0 : fprintf (stderr, "Warning: Couldn't get"
12401 : : " skeleton subdie: %s\n",
12402 : : dwarf_errmsg (-1));
12403 : : }
12404 : : }
12405 : : }
12406 [ - - ]: 18 : if (split_dbg == NULL)
12407 : 0 : fprintf (stderr, "Warning: '%s' didn't contain a skeleton for split id %" PRIx64 "\n", skel_name, split_id);
12408 : : }
12409 : : else
12410 : 0 : fprintf (stderr, "Warning: Couldn't get skeleton DWARF:"
12411 : : " %s\n", dwfl_errmsg (-1));
12412 : : }
12413 : : }
12414 : :
12415 : 0 : if (split_dbg != NULL)
12416 : : {
12417 : 18 : dbg = split_dbg;
12418 : 18 : dwflmod = skel_mod;
12419 : : }
12420 [ # # ]: 0 : else if (skel_name == NULL)
12421 : 0 : fprintf (stderr,
12422 : : "Warning: split DWARF file, but no skeleton found.\n");
12423 : : }
12424 [ + - ]: 378 : else if (dwarf_skeleton != NULL)
12425 : 0 : fprintf (stderr, "Warning: DWARF skeleton given,"
12426 : : " but not a split DWARF file\n");
12427 : : }
12428 : :
12429 : : /* Get the section header string table index. */
12430 : 502 : size_t shstrndx;
12431 [ - + ]: 502 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
12432 : 0 : error_exit (0, _("cannot get section header string table index"));
12433 : :
12434 : 502 : ssize_t num_jobs = 0;
12435 : 502 : job_data *jdata = NULL;
12436 : :
12437 : : /* If the .debug_info section is listed as implicitly required then
12438 : : we must make sure to handle it before handling any other debug
12439 : : section. Various other sections depend on the CU DIEs being
12440 : : scanned (silently) first. */
12441 : 502 : bool implicit_info = (implicit_debug_sections & section_info) != 0;
12442 : 502 : bool explicit_info = (print_debug_sections & section_info) != 0;
12443 [ + + ]: 502 : if (implicit_info)
12444 : : {
12445 : : Elf_Scn *scn = NULL;
12446 [ + - ]: 3132 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
12447 : : {
12448 : 3132 : GElf_Shdr shdr_mem;
12449 : 3132 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
12450 : :
12451 [ + - + + ]: 3132 : if (shdr != NULL && shdr->sh_type == SHT_PROGBITS)
12452 : : {
12453 : 2448 : const char *name = elf_strptr (ebl->elf, shstrndx,
12454 : 1224 : shdr->sh_name);
12455 [ - + ]: 1224 : if (name == NULL)
12456 : 0 : continue;
12457 : :
12458 [ + + ]: 1224 : if (strcmp (name, ".debug_info") == 0
12459 [ + + ]: 1092 : || strcmp (name, ".debug_info.dwo") == 0
12460 [ + + ]: 1074 : || strcmp (name, ".zdebug_info") == 0
12461 [ + - ]: 1062 : || strcmp (name, ".zdebug_info.dwo") == 0
12462 [ + + ]: 1062 : || strcmp (name, ".gnu.debuglto_.debug_info") == 0)
12463 : : {
12464 : 164 : print_debug_info_section (dwflmod, ebl, ehdr,
12465 : : scn, shdr, dbg, stdout);
12466 : 164 : break;
12467 : : }
12468 : : }
12469 : : }
12470 : :
12471 : 164 : print_debug_sections &= ~section_info;
12472 : 164 : implicit_debug_sections &= ~section_info;
12473 : : }
12474 : :
12475 : : /* Look through all the sections for the debugging sections to print. */
12476 : : Elf_Scn *scn = NULL;
12477 [ + + ]: 16590 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
12478 : : {
12479 : 16088 : GElf_Shdr shdr_mem;
12480 : 16088 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
12481 : :
12482 [ + - + + ]: 16088 : if (shdr != NULL && shdr->sh_type == SHT_PROGBITS)
12483 : : {
12484 : 6758 : static const struct
12485 : : {
12486 : : const char *name;
12487 : : enum section_e bitmask;
12488 : : void (*fp) (Dwfl_Module *, Ebl *,
12489 : : GElf_Ehdr *, Elf_Scn *, GElf_Shdr *, Dwarf *, FILE *);
12490 : : } debug_sections[] =
12491 : : {
12492 : : #define NEW_SECTION(name) \
12493 : : { ".debug_" #name, section_##name, print_debug_##name##_section }
12494 : : NEW_SECTION (abbrev),
12495 : : NEW_SECTION (addr),
12496 : : NEW_SECTION (aranges),
12497 : : NEW_SECTION (frame),
12498 : : NEW_SECTION (info),
12499 : : NEW_SECTION (types),
12500 : : NEW_SECTION (line),
12501 : : NEW_SECTION (loc),
12502 : : /* loclists is loc for DWARF5. */
12503 : : { ".debug_loclists", section_loc,
12504 : : print_debug_loclists_section },
12505 : : NEW_SECTION (pubnames),
12506 : : NEW_SECTION (str),
12507 : : /* A DWARF5 specialised debug string section. */
12508 : : { ".debug_line_str", section_str,
12509 : : print_debug_str_section },
12510 : : /* DWARF5 string offsets table. */
12511 : : { ".debug_str_offsets", section_str,
12512 : : print_debug_str_offsets_section },
12513 : : NEW_SECTION (macinfo),
12514 : : NEW_SECTION (macro),
12515 : : NEW_SECTION (ranges),
12516 : : /* rnglists is ranges for DWARF5. */
12517 : : { ".debug_rnglists", section_ranges,
12518 : : print_debug_rnglists_section },
12519 : : { ".eh_frame", section_frame | section_exception,
12520 : : print_debug_frame_section },
12521 : : { ".eh_frame_hdr", section_frame | section_exception,
12522 : : print_debug_frame_hdr_section },
12523 : : { ".gcc_except_table", section_frame | section_exception,
12524 : : print_debug_exception_table },
12525 : : { ".gdb_index", section_gdb_index, print_gdb_index_section }
12526 : : };
12527 : 6758 : const int ndebug_sections = (sizeof (debug_sections)
12528 : : / sizeof (debug_sections[0]));
12529 : 13516 : const char *name = elf_strptr (ebl->elf, shstrndx,
12530 : 6758 : shdr->sh_name);
12531 [ - + ]: 6758 : if (name == NULL)
12532 : 0 : continue;
12533 : :
12534 [ + + ]: 6758 : if (jdata == NULL)
12535 : : {
12536 : 502 : jdata = calloc (ndebug_sections, sizeof (*jdata));
12537 [ - + ]: 502 : if (jdata == NULL)
12538 : 0 : error_exit (0, _("failed to allocate job data"));
12539 : : }
12540 : :
12541 : 6758 : int n;
12542 [ + + ]: 106506 : for (n = 0; n < ndebug_sections; ++n)
12543 : : {
12544 : 103042 : size_t dbglen = strlen (debug_sections[n].name);
12545 : 103042 : size_t scnlen = strlen (name);
12546 [ + + ]: 103042 : if ((strncmp (name, debug_sections[n].name, dbglen) == 0
12547 [ + + ]: 3558 : && (dbglen == scnlen
12548 [ + + ]: 546 : || (scnlen == dbglen + 4
12549 [ + + ]: 466 : && strstr (name, ".dwo") == name + dbglen)))
12550 [ + + + + ]: 99922 : || (name[0] == '.' && name[1] == 'z'
12551 [ + - ]: 1132 : && debug_sections[n].name[1] == 'd'
12552 [ + + ]: 1132 : && strncmp (&name[2], &debug_sections[n].name[1],
12553 : : dbglen - 1) == 0
12554 [ - + ]: 164 : && (scnlen == dbglen + 1
12555 [ # # ]: 0 : || (scnlen == dbglen + 5
12556 [ # # ]: 0 : && strstr (name, ".dwo") == name + dbglen + 1)))
12557 [ + + ]: 99758 : || (scnlen > 14 /* .gnu.debuglto_ prefix. */
12558 [ + + ]: 10170 : && startswith (name, ".gnu.debuglto_")
12559 [ + + ]: 72 : && strcmp (&name[14], debug_sections[n].name) == 0)
12560 : : )
12561 : : {
12562 : 3294 : if (((print_debug_sections | implicit_debug_sections)
12563 [ + + ]: 3294 : & debug_sections[n].bitmask))
12564 : 1094 : schedule_job (jdata, num_jobs++, debug_sections[n].fp,
12565 : : dwflmod, ebl, ehdr, scn, shdr, dbg);
12566 : :
12567 [ - + ]: 3294 : assert (num_jobs <= ndebug_sections);
12568 : : break;
12569 : : }
12570 : : }
12571 : : }
12572 : : }
12573 : :
12574 : : #ifdef USE_LOCKS
12575 : : /* If max_threads <= 1, then jobs were immediately run in schedule_job. */
12576 : : if (max_threads > 1)
12577 : : run_jobs (max_threads);
12578 : : #endif
12579 : :
12580 : 502 : dwfl_end (skel_dwfl);
12581 : 502 : free (skel_name);
12582 : 502 : free (jdata);
12583 : :
12584 : : /* Turn implicit and/or explicit back on in case we go over another file. */
12585 [ + + ]: 502 : if (implicit_info)
12586 : 164 : implicit_debug_sections |= section_info;
12587 [ + + ]: 502 : if (explicit_info)
12588 : 134 : print_debug_sections |= section_info;
12589 : :
12590 : 502 : reset_listptr (&known_locsptr);
12591 : 502 : reset_listptr (&known_loclistsptr);
12592 : 502 : reset_listptr (&known_rangelistptr);
12593 : 502 : reset_listptr (&known_rnglistptr);
12594 : 502 : reset_listptr (&known_addrbases);
12595 : 502 : reset_listptr (&known_stroffbases);
12596 : 502 : }
12597 : :
12598 : :
12599 : : #define ITEM_INDENT 4
12600 : : #define WRAP_COLUMN 75
12601 : :
12602 : : /* Print "NAME: FORMAT", wrapping when output text would make the line
12603 : : exceed WRAP_COLUMN. Unpadded numbers look better for the core items
12604 : : but this function is also used for registers which should be printed
12605 : : aligned. Fortunately registers output uses fixed fields width (such
12606 : : as %11d) for the alignment.
12607 : :
12608 : : Line breaks should not depend on the particular values although that
12609 : : may happen in some cases of the core items. */
12610 : :
12611 : : static unsigned int
12612 : : __attribute__ ((format (printf, 6, 7)))
12613 : 1642 : print_core_item (unsigned int colno, char sep, unsigned int wrap,
12614 : : size_t name_width, const char *name, const char *format, ...)
12615 : : {
12616 : 1642 : size_t len = strlen (name);
12617 : 1642 : if (name_width < len)
12618 : : name_width = len;
12619 : :
12620 : 1642 : char *out;
12621 : 1642 : va_list ap;
12622 : 1642 : va_start (ap, format);
12623 : 1642 : int out_len = vasprintf (&out, format, ap);
12624 : 1642 : va_end (ap);
12625 [ - + ]: 1642 : if (out_len == -1)
12626 : 0 : error_exit (0, _("memory exhausted"));
12627 : :
12628 : 1642 : size_t n = name_width + sizeof ": " - 1 + out_len;
12629 : :
12630 [ + + ]: 1642 : if (colno == 0)
12631 : : {
12632 : 102 : printf ("%*s", ITEM_INDENT, "");
12633 : 102 : colno = ITEM_INDENT + n;
12634 : : }
12635 [ + + ]: 1540 : else if (colno + 2 + n < wrap)
12636 : : {
12637 : 936 : printf ("%c ", sep);
12638 : 936 : colno += 2 + n;
12639 : : }
12640 : : else
12641 : : {
12642 : 604 : printf ("\n%*s", ITEM_INDENT, "");
12643 : 604 : colno = ITEM_INDENT + n;
12644 : : }
12645 : :
12646 : 1642 : printf ("%s: %*s%s", name, (int) (name_width - len), "", out);
12647 : :
12648 : 1642 : free (out);
12649 : :
12650 : 1642 : return colno;
12651 : : }
12652 : :
12653 : : static const void *
12654 : 1862 : convert (Elf *core, Elf_Type type, uint_fast16_t count,
12655 : : void *value, const void *data, size_t size)
12656 : : {
12657 : 3724 : Elf_Data valuedata =
12658 : : {
12659 : : .d_type = type,
12660 : : .d_buf = value,
12661 [ + + ]: 1862 : .d_size = size ?: gelf_fsize (core, type, count, EV_CURRENT),
12662 : : .d_version = EV_CURRENT,
12663 : : };
12664 : 1862 : Elf_Data indata =
12665 : : {
12666 : : .d_type = type,
12667 : : .d_buf = (void *) data,
12668 : : .d_size = valuedata.d_size,
12669 : : .d_version = EV_CURRENT,
12670 : : };
12671 : :
12672 : 1862 : Elf_Data *d = (gelf_getclass (core) == ELFCLASS32
12673 [ + + ]: 1862 : ? elf32_xlatetom : elf64_xlatetom)
12674 : 1862 : (&valuedata, &indata, elf_getident (core, NULL)[EI_DATA]);
12675 [ - + ]: 1862 : if (d == NULL)
12676 : 0 : error_exit (0, _("cannot convert core note data: %s"),
12677 : : elf_errmsg (-1));
12678 : :
12679 : 1862 : return data + indata.d_size;
12680 : : }
12681 : :
12682 : : typedef uint8_t GElf_Byte;
12683 : :
12684 : : static unsigned int
12685 : 800 : handle_core_item (Elf *core, const GElf_Ehdr *ehdr,
12686 : : const Ebl_Core_Item *item, const void *desc,
12687 : : unsigned int colno, size_t *repeated_size)
12688 : : {
12689 [ + + ]: 800 : uint_fast16_t count = item->count ?: 1;
12690 : : /* Ebl_Core_Item count is always a small number.
12691 : : Make sure the backend didn't put in some large bogus value. */
12692 [ - + ]: 124 : assert (count < 128);
12693 : :
12694 : : #define TYPES \
12695 : : DO_TYPE (BYTE, Byte, "0x%.2" PRIx8, "%" PRId8); \
12696 : : DO_TYPE (HALF, Half, "0x%.4" PRIx16, "%" PRId16); \
12697 : : DO_TYPE (WORD, Word, "0x%.8" PRIx32, "%" PRId32); \
12698 : : DO_TYPE (SWORD, Sword, "%" PRId32, "%" PRId32); \
12699 : : DO_TYPE (XWORD, Xword, "0x%.16" PRIx64, "%" PRId64); \
12700 : : DO_TYPE (SXWORD, Sxword, "%" PRId64, "%" PRId64)
12701 : :
12702 : : #define DO_TYPE(NAME, Name, hex, dec) GElf_##Name Name
12703 : 800 : typedef union { TYPES; } value_t;
12704 : 800 : void *data = alloca (count * sizeof (value_t));
12705 : : #undef DO_TYPE
12706 : :
12707 : : #define DO_TYPE(NAME, Name, hex, dec) \
12708 : : GElf_##Name *value_##Name __attribute__((unused)) = data
12709 : 800 : TYPES;
12710 : : #undef DO_TYPE
12711 : :
12712 : 800 : size_t size = gelf_fsize (core, item->type, count, EV_CURRENT);
12713 : 800 : size_t convsize = size;
12714 [ + + ]: 800 : if (repeated_size != NULL)
12715 : : {
12716 [ + - - + ]: 2 : if (*repeated_size > size && (item->format == 'b' || item->format == 'B'))
12717 : : {
12718 : 0 : data = alloca (*repeated_size);
12719 : 0 : count *= *repeated_size / size;
12720 : 0 : convsize = count * size;
12721 : 0 : *repeated_size -= convsize;
12722 : : }
12723 [ + - + - ]: 2 : else if (item->count != 0 || item->format != '\n')
12724 : 0 : *repeated_size -= size;
12725 : : }
12726 : :
12727 : 800 : convert (core, item->type, count, data, desc + item->offset, convsize);
12728 : :
12729 : 800 : Elf_Type type = item->type;
12730 [ + + ]: 800 : if (type == ELF_T_ADDR)
12731 [ + - ]: 2 : type = gelf_getclass (core) == ELFCLASS32 ? ELF_T_WORD : ELF_T_XWORD;
12732 : :
12733 [ + + + + : 800 : switch (item->format)
+ + + +
- ]
12734 : : {
12735 : 386 : case 'd':
12736 [ - + ]: 386 : assert (count == 1);
12737 [ + + + + : 386 : switch (type)
- + - ]
12738 : : {
12739 : : #define DO_TYPE(NAME, Name, hex, dec) \
12740 : : case ELF_T_##NAME: \
12741 : : colno = print_core_item (colno, ',', WRAP_COLUMN, \
12742 : : 0, item->name, dec, value_##Name[0]); \
12743 : : break
12744 : 386 : TYPES;
12745 : : #undef DO_TYPE
12746 : 0 : default:
12747 : 0 : abort ();
12748 : : }
12749 : : break;
12750 : :
12751 : 222 : case 'x':
12752 [ - + ]: 222 : assert (count == 1);
12753 [ - - + - : 222 : switch (type)
+ - - ]
12754 : : {
12755 : : #define DO_TYPE(NAME, Name, hex, dec) \
12756 : : case ELF_T_##NAME: \
12757 : : colno = print_core_item (colno, ',', WRAP_COLUMN, \
12758 : : 0, item->name, hex, value_##Name[0]); \
12759 : : break
12760 : 222 : TYPES;
12761 : : #undef DO_TYPE
12762 : 0 : default:
12763 : 0 : abort ();
12764 : : }
12765 : : break;
12766 : :
12767 : 44 : case 'b':
12768 : : case 'B':
12769 [ - + ]: 44 : assert (size % sizeof (unsigned int) == 0);
12770 : 44 : unsigned int nbits = count * size * 8;
12771 : 44 : unsigned int pop = 0;
12772 [ + + ]: 112 : for (const unsigned int *i = data; (void *) i < data + count * size; ++i)
12773 : 68 : pop += __builtin_popcount (*i);
12774 : 44 : bool negate = pop > nbits / 2;
12775 : 44 : const unsigned int bias = item->format == 'b';
12776 : :
12777 : 44 : {
12778 [ - + ]: 44 : char printed[(negate ? nbits - pop : pop) * 16 + 1];
12779 : 44 : char *p = printed;
12780 : 44 : *p = '\0';
12781 : :
12782 : 44 : if (BYTE_ORDER != LITTLE_ENDIAN && size > sizeof (unsigned int))
12783 : : {
12784 : : assert (size == sizeof (unsigned int) * 2);
12785 : : for (unsigned int *i = data;
12786 : : (void *) i < data + count * size; i += 2)
12787 : : {
12788 : : unsigned int w = i[1];
12789 : : i[1] = i[0];
12790 : : i[0] = w;
12791 : : }
12792 : : }
12793 : :
12794 : 44 : unsigned int lastbit = 0;
12795 : 44 : unsigned int run = 0;
12796 : 44 : for (const unsigned int *i = data;
12797 [ + + ]: 112 : (void *) i < data + count * size; ++i)
12798 : : {
12799 : 68 : unsigned int bit = ((void *) i - data) * 8;
12800 [ - + ]: 68 : unsigned int w = negate ? ~*i : *i;
12801 [ - + ]: 68 : while (w != 0)
12802 : : {
12803 : : /* Note that a right shift equal to (or greater than)
12804 : : the number of bits of w is undefined behaviour. In
12805 : : particular when the least significant bit is bit 32
12806 : : (w = 0x8000000) then w >>= n is undefined. So
12807 : : explicitly handle that case separately. */
12808 : 0 : unsigned int n = ffs (w);
12809 [ # # ]: 0 : if (n < sizeof (w) * 8)
12810 : 0 : w >>= n;
12811 : : else
12812 : : w = 0;
12813 : 0 : bit += n;
12814 : :
12815 [ # # # # ]: 0 : if (lastbit != 0 && lastbit + 1 == bit)
12816 : 0 : ++run;
12817 : : else
12818 : : {
12819 : 0 : if (lastbit == 0)
12820 : 0 : p += sprintf (p, "%u", bit - bias);
12821 [ # # ]: 0 : else if (run == 0)
12822 : 0 : p += sprintf (p, ",%u", bit - bias);
12823 : : else
12824 : 0 : p += sprintf (p, "-%u,%u", lastbit - bias, bit - bias);
12825 : : run = 0;
12826 : : }
12827 : :
12828 : : lastbit = bit;
12829 : : }
12830 : : }
12831 [ - + - - ]: 44 : if (lastbit > 0 && run > 0 && lastbit + 1 != nbits)
12832 : 0 : p += sprintf (p, "-%u", lastbit - bias);
12833 : :
12834 [ + - ]: 88 : colno = print_core_item (colno, ',', WRAP_COLUMN, 0, item->name,
12835 : : negate ? "~<%s>" : "<%s>", printed);
12836 : : }
12837 : 44 : break;
12838 : :
12839 : 88 : case 'T':
12840 : : case (char) ('T'|0x80):
12841 [ - + ]: 88 : assert (count == 2);
12842 : 88 : Dwarf_Word sec;
12843 : 88 : Dwarf_Word usec;
12844 [ - - + - : 88 : switch (type)
+ - - ]
12845 : : {
12846 : : #define DO_TYPE(NAME, Name, hex, dec) \
12847 : : case ELF_T_##NAME: \
12848 : : sec = value_##Name[0]; \
12849 : : usec = value_##Name[1]; \
12850 : : break
12851 : 88 : TYPES;
12852 : : #undef DO_TYPE
12853 : 0 : default:
12854 : 0 : abort ();
12855 : : }
12856 [ - + ]: 88 : if (unlikely (item->format == (char) ('T'|0x80)))
12857 : : {
12858 : : /* This is a hack for an ill-considered 64-bit ABI where
12859 : : tv_usec is actually a 32-bit field with 32 bits of padding
12860 : : rounding out struct timeval. We've already converted it as
12861 : : a 64-bit field. For little-endian, this just means the
12862 : : high half is the padding; it's presumably zero, but should
12863 : : be ignored anyway. For big-endian, it means the 32-bit
12864 : : field went into the high half of USEC. */
12865 [ # # ]: 0 : if (likely (ehdr->e_ident[EI_DATA] == ELFDATA2MSB))
12866 : 0 : usec >>= 32;
12867 : : else
12868 : 0 : usec &= UINT32_MAX;
12869 : : }
12870 : 88 : colno = print_core_item (colno, ',', WRAP_COLUMN, 0, item->name,
12871 : : "%" PRIu64 ".%.6" PRIu64, sec, usec);
12872 : 88 : break;
12873 : :
12874 : 18 : case 'c':
12875 [ - + ]: 18 : assert (count == 1);
12876 : 18 : colno = print_core_item (colno, ',', WRAP_COLUMN, 0, item->name,
12877 : 18 : "%c", value_Byte[0]);
12878 : 18 : break;
12879 : :
12880 : 36 : case 's':
12881 : 36 : colno = print_core_item (colno, ',', WRAP_COLUMN, 0, item->name,
12882 : : "%.*s", (int) count, value_Byte);
12883 : 36 : break;
12884 : :
12885 : 2 : case '\n':
12886 : : /* This is a list of strings separated by '\n'. */
12887 [ - + ]: 2 : assert (item->count == 0);
12888 [ - + ]: 2 : assert (repeated_size != NULL);
12889 [ - + ]: 2 : assert (item->name == NULL);
12890 [ - + ]: 2 : if (unlikely (item->offset >= *repeated_size))
12891 : : break;
12892 : :
12893 : 2 : const char *s = desc + item->offset;
12894 : 2 : size = *repeated_size - item->offset;
12895 : 2 : *repeated_size = 0;
12896 [ + - ]: 96 : while (size > 0)
12897 : : {
12898 : 96 : const char *eol = memchr (s, '\n', size);
12899 : 96 : int len = size;
12900 [ + + ]: 96 : if (eol != NULL)
12901 : 94 : len = eol - s;
12902 : 96 : printf ("%*s%.*s\n", ITEM_INDENT, "", len, s);
12903 [ + + ]: 96 : if (eol == NULL)
12904 : : break;
12905 : 94 : size -= eol + 1 - s;
12906 : 94 : s = eol + 1;
12907 : : }
12908 : :
12909 : : colno = WRAP_COLUMN;
12910 : : break;
12911 : :
12912 : : case 'h':
12913 : : break;
12914 : :
12915 : 0 : default:
12916 : 0 : error (0, 0, "XXX not handling format '%c' for %s",
12917 : 0 : item->format, item->name);
12918 : 0 : break;
12919 : : }
12920 : :
12921 : : #undef TYPES
12922 : :
12923 : 800 : return colno;
12924 : : }
12925 : :
12926 : :
12927 : : /* Sort items by group, and by layout offset within each group. */
12928 : : static int
12929 : 1790 : compare_core_items (const void *a, const void *b)
12930 : : {
12931 : 1790 : const Ebl_Core_Item *const *p1 = a;
12932 : 1790 : const Ebl_Core_Item *const *p2 = b;
12933 : 1790 : const Ebl_Core_Item *item1 = *p1;
12934 : 1790 : const Ebl_Core_Item *item2 = *p2;
12935 : :
12936 : 1790 : return ((item1->group == item2->group ? 0
12937 [ + + ]: 1790 : : strcmp (item1->group, item2->group))
12938 [ - + ]: 1790 : ?: (int) item1->offset - (int) item2->offset);
12939 : : }
12940 : :
12941 : : /* Sort item groups by layout offset of the first item in the group. */
12942 : : static int
12943 : 278 : compare_core_item_groups (const void *a, const void *b)
12944 : : {
12945 : 278 : const Ebl_Core_Item *const *const *p1 = a;
12946 : 278 : const Ebl_Core_Item *const *const *p2 = b;
12947 : 278 : const Ebl_Core_Item *const *group1 = *p1;
12948 : 278 : const Ebl_Core_Item *const *group2 = *p2;
12949 : 278 : const Ebl_Core_Item *item1 = *group1;
12950 : 278 : const Ebl_Core_Item *item2 = *group2;
12951 : :
12952 : 278 : return (int) item1->offset - (int) item2->offset;
12953 : : }
12954 : :
12955 : : static unsigned int
12956 : 74 : handle_core_items (Elf *core, const GElf_Ehdr *ehdr,
12957 : : const void *desc, size_t descsz,
12958 : : const Ebl_Core_Item *items, size_t nitems)
12959 : 74 : {
12960 [ + + ]: 74 : if (nitems == 0)
12961 : : return 0;
12962 : 68 : unsigned int colno = 0;
12963 : :
12964 : : /* FORMAT '\n' makes sense to be present only as a single item as it
12965 : : processes all the data of a note. FORMATs 'b' and 'B' have a special case
12966 : : if present as a single item but they can be also processed with other
12967 : : items below. */
12968 [ + + + + ]: 68 : if (nitems == 1 && (items[0].format == '\n' || items[0].format == 'b'
12969 [ + - ]: 16 : || items[0].format == 'B'))
12970 : : {
12971 [ - + ]: 2 : assert (items[0].offset == 0);
12972 : 2 : size_t size = descsz;
12973 : 2 : colno = handle_core_item (core, ehdr, items, desc, colno, &size);
12974 : : /* If SIZE is not zero here there is some remaining data. But we do not
12975 : : know how to process it anyway. */
12976 : 2 : return colno;
12977 : : }
12978 [ + + ]: 848 : for (size_t i = 0; i < nitems; ++i)
12979 [ - + ]: 782 : assert (items[i].format != '\n');
12980 : :
12981 : : /* Sort to collect the groups together. */
12982 : 66 : const Ebl_Core_Item *sorted_items[nitems];
12983 [ + + ]: 848 : for (size_t i = 0; i < nitems; ++i)
12984 : 782 : sorted_items[i] = &items[i];
12985 : 66 : qsort (sorted_items, nitems, sizeof sorted_items[0], &compare_core_items);
12986 : :
12987 : : /* Collect the unique groups and sort them. */
12988 : 66 : const Ebl_Core_Item **groups[nitems];
12989 : 66 : groups[0] = &sorted_items[0];
12990 : 66 : size_t ngroups = 1;
12991 [ + + ]: 782 : for (size_t i = 1; i < nitems; ++i)
12992 [ + + ]: 716 : if (sorted_items[i]->group != sorted_items[i - 1]->group
12993 [ + - ]: 150 : && strcmp (sorted_items[i]->group, sorted_items[i - 1]->group))
12994 : 150 : groups[ngroups++] = &sorted_items[i];
12995 : 66 : qsort (groups, ngroups, sizeof groups[0], &compare_core_item_groups);
12996 : :
12997 : : /* Write out all the groups. */
12998 : 66 : const void *last = desc;
12999 : 70 : do
13000 : : {
13001 [ + + ]: 290 : for (size_t i = 0; i < ngroups; ++i)
13002 : : {
13003 : 220 : for (const Ebl_Core_Item **item = groups[i];
13004 : 1018 : (item < &sorted_items[nitems]
13005 [ + + + + ]: 1018 : && ((*item)->group == groups[i][0]->group
13006 [ - + ]: 150 : || !strcmp ((*item)->group, groups[i][0]->group)));
13007 : 798 : ++item)
13008 : 798 : colno = handle_core_item (core, ehdr, *item, desc, colno, NULL);
13009 : :
13010 : : /* Force a line break at the end of the group. */
13011 : 220 : colno = WRAP_COLUMN;
13012 : : }
13013 : :
13014 [ + + ]: 70 : if (descsz == 0)
13015 : : break;
13016 : :
13017 : : /* This set of items consumed a certain amount of the note's data.
13018 : : If there is more data there, we have another unit of the same size.
13019 : : Loop to print that out too. */
13020 : 40 : const Ebl_Core_Item *item = &items[nitems - 1];
13021 : 80 : size_t eltsz = item->offset + gelf_fsize (core, item->type,
13022 : 40 : item->count ?: 1, EV_CURRENT);
13023 : :
13024 : 40 : int reps = -1;
13025 : 40 : do
13026 : : {
13027 : 40 : ++reps;
13028 : 40 : desc += eltsz;
13029 : 40 : descsz -= eltsz;
13030 : : }
13031 [ + + - + ]: 40 : while (descsz >= eltsz && !memcmp (desc, last, eltsz));
13032 : :
13033 [ + - ]: 40 : if (reps == 1)
13034 : : {
13035 : : /* For just one repeat, print it unabridged twice. */
13036 : : desc -= eltsz;
13037 : : descsz += eltsz;
13038 : : }
13039 [ + - ]: 40 : else if (reps > 1)
13040 : 0 : printf (_("\n%*s... <repeats %u more times> ..."),
13041 : : ITEM_INDENT, "", reps);
13042 : :
13043 : 40 : last = desc;
13044 : : }
13045 [ + + ]: 40 : while (descsz > 0);
13046 : :
13047 : : return colno;
13048 : : }
13049 : :
13050 : : static unsigned int
13051 : 324 : handle_core_register (Ebl *ebl, Elf *core, int maxregname,
13052 : : const Ebl_Register_Location *regloc, const void *desc,
13053 : : unsigned int colno)
13054 : : {
13055 [ - + ]: 324 : if (regloc->bits % 8 != 0)
13056 : : {
13057 : 0 : error (0, 0, "Warning: Cannot handle register with %" PRIu8 "bits\n",
13058 : : regloc->bits);
13059 : 0 : return colno;
13060 : : }
13061 : :
13062 : 324 : desc += regloc->offset;
13063 : :
13064 [ + + ]: 1172 : for (int reg = regloc->regno; reg < regloc->regno + regloc->count; ++reg)
13065 : : {
13066 : 848 : char name[REGNAMESZ];
13067 : 848 : int bits;
13068 : 848 : int type;
13069 : 848 : register_info (ebl, reg, regloc, name, &bits, &type);
13070 : :
13071 : : #define TYPES \
13072 : : BITS (8, BYTE, "%4" PRId8, "0x%.2" PRIx8); \
13073 : : BITS (16, HALF, "%6" PRId16, "0x%.4" PRIx16); \
13074 : : BITS (32, WORD, "%11" PRId32, " 0x%.8" PRIx32); \
13075 : : BITS (64, XWORD, "%20" PRId64, " 0x%.16" PRIx64)
13076 : :
13077 : : #define BITS(bits, xtype, sfmt, ufmt) \
13078 : : uint##bits##_t b##bits; int##bits##_t b##bits##s
13079 : 848 : union { TYPES; uint64_t b128[2]; } value;
13080 : : #undef BITS
13081 : :
13082 [ + + ]: 848 : switch (type)
13083 : : {
13084 : 672 : case DW_ATE_unsigned:
13085 : : case DW_ATE_signed:
13086 : : case DW_ATE_address:
13087 [ - + + + : 672 : switch (bits)
+ - ]
13088 : : {
13089 : : #define BITS(bits, xtype, sfmt, ufmt) \
13090 : : case bits: \
13091 : : desc = convert (core, ELF_T_##xtype, 1, &value, desc, 0); \
13092 : : if (type == DW_ATE_signed) \
13093 : : colno = print_core_item (colno, ' ', WRAP_COLUMN, \
13094 : : maxregname, name, \
13095 : : sfmt, value.b##bits##s); \
13096 : : else \
13097 : : colno = print_core_item (colno, ' ', WRAP_COLUMN, \
13098 : : maxregname, name, \
13099 : : ufmt, value.b##bits); \
13100 : : break
13101 : :
13102 [ - - - + : 576 : TYPES;
+ + + + ]
13103 : :
13104 : 96 : case 128:
13105 [ - + ]: 96 : assert (type == DW_ATE_unsigned);
13106 : 96 : desc = convert (core, ELF_T_XWORD, 2, &value, desc, 0);
13107 : 96 : int be = elf_getident (core, NULL)[EI_DATA] == ELFDATA2MSB;
13108 : 96 : colno = print_core_item (colno, ' ', WRAP_COLUMN,
13109 : : maxregname, name,
13110 : : "0x%.16" PRIx64 "%.16" PRIx64,
13111 : 96 : value.b128[!be], value.b128[be]);
13112 : 96 : break;
13113 : :
13114 : 0 : default:
13115 : 0 : abort ();
13116 : : #undef BITS
13117 : : }
13118 : : break;
13119 : :
13120 : 176 : default:
13121 : : /* Print each byte in hex, the whole thing in native byte order. */
13122 [ - + ]: 176 : assert (bits % 8 == 0);
13123 : 176 : const uint8_t *bytes = desc;
13124 : 176 : desc += bits / 8;
13125 : 176 : char hex[bits / 4 + 1];
13126 : 176 : hex[bits / 4] = '\0';
13127 : 176 : int incr = 1;
13128 [ + + ]: 176 : if (elf_getident (core, NULL)[EI_DATA] == ELFDATA2LSB)
13129 : : {
13130 : 96 : bytes += bits / 8 - 1;
13131 : 96 : incr = -1;
13132 : : }
13133 : 176 : size_t idx = 0;
13134 [ + + ]: 1744 : for (char *h = hex; bits > 0; bits -= 8, idx += incr)
13135 : : {
13136 : 1568 : *h++ = "0123456789abcdef"[bytes[idx] >> 4];
13137 : 1568 : *h++ = "0123456789abcdef"[bytes[idx] & 0xf];
13138 : : }
13139 : 176 : colno = print_core_item (colno, ' ', WRAP_COLUMN,
13140 : : maxregname, name, "0x%s", hex);
13141 : 176 : break;
13142 : : }
13143 : 848 : desc += regloc->pad;
13144 : :
13145 : : #undef TYPES
13146 : : }
13147 : :
13148 : : return colno;
13149 : : }
13150 : :
13151 : :
13152 : : struct register_info
13153 : : {
13154 : : const Ebl_Register_Location *regloc;
13155 : : const char *set;
13156 : : char name[REGNAMESZ];
13157 : : int regno;
13158 : : int bits;
13159 : : int type;
13160 : : };
13161 : :
13162 : : static int
13163 : 4132 : register_bitpos (const struct register_info *r)
13164 : : {
13165 : 4132 : return (r->regloc->offset * 8
13166 : 4132 : + ((r->regno - r->regloc->regno)
13167 : 4132 : * (r->regloc->bits + r->regloc->pad * 8)));
13168 : : }
13169 : :
13170 : : static int
13171 : 2124 : compare_sets_by_info (const struct register_info *r1,
13172 : : const struct register_info *r2)
13173 : : {
13174 : 2124 : return ((int) r2->bits - (int) r1->bits
13175 [ + + ]: 2124 : ?: register_bitpos (r1) - register_bitpos (r2));
13176 : : }
13177 : :
13178 : : /* Sort registers by set, and by size and layout offset within each set. */
13179 : : static int
13180 : 9190 : compare_registers (const void *a, const void *b)
13181 : : {
13182 : 9190 : const struct register_info *r1 = a;
13183 : 9190 : const struct register_info *r2 = b;
13184 : :
13185 : : /* Unused elements sort last. */
13186 [ + + ]: 9190 : if (r1->regloc == NULL)
13187 : 6618 : return r2->regloc == NULL ? 0 : 1;
13188 [ + + ]: 2572 : if (r2->regloc == NULL)
13189 : : return -1;
13190 : :
13191 [ + + ]: 2242 : return ((r1->set == r2->set ? 0 : strcmp (r1->set, r2->set))
13192 [ - + ]: 2242 : ?: compare_sets_by_info (r1, r2));
13193 : : }
13194 : :
13195 : : /* Sort register sets by layout offset of the first register in the set. */
13196 : : static int
13197 : 40 : compare_register_sets (const void *a, const void *b)
13198 : : {
13199 : 40 : const struct register_info *const *p1 = a;
13200 : 40 : const struct register_info *const *p2 = b;
13201 : 40 : return compare_sets_by_info (*p1, *p2);
13202 : : }
13203 : :
13204 : : static inline bool
13205 : 1728 : same_set (const struct register_info *a,
13206 : : const struct register_info *b,
13207 : : const struct register_info *regs,
13208 : : size_t maxnreg)
13209 : : {
13210 [ + - ]: 1728 : return (a < ®s[maxnreg] && a->regloc != NULL
13211 [ + - + + ]: 1728 : && b < ®s[maxnreg] && b->regloc != NULL
13212 [ + + ]: 1692 : && a->bits == b->bits
13213 [ + - + + : 3384 : && (a->set == b->set || !strcmp (a->set, b->set)));
- + ]
13214 : : }
13215 : :
13216 : : static unsigned int
13217 : 74 : handle_core_registers (Ebl *ebl, Elf *core, const void *desc,
13218 : : const Ebl_Register_Location *reglocs, size_t nregloc)
13219 : 74 : {
13220 [ + + ]: 74 : if (nregloc == 0)
13221 : : return 0;
13222 : :
13223 : 36 : ssize_t maxnreg = ebl_register_info (ebl, 0, NULL, 0, NULL, NULL, NULL, NULL);
13224 [ - + ]: 36 : if (maxnreg <= 0)
13225 : : {
13226 [ # # ]: 0 : for (size_t i = 0; i < nregloc; ++i)
13227 : 0 : if (maxnreg < reglocs[i].regno + reglocs[i].count)
13228 : : maxnreg = reglocs[i].regno + reglocs[i].count;
13229 [ # # ]: 0 : assert (maxnreg > 0);
13230 : : }
13231 : :
13232 : 36 : struct register_info regs[maxnreg];
13233 : 36 : memset (regs, 0, sizeof regs);
13234 : :
13235 : : /* Sort to collect the sets together. */
13236 : 36 : int maxreg = 0;
13237 [ + + ]: 360 : for (size_t i = 0; i < nregloc; ++i)
13238 : 324 : for (int reg = reglocs[i].regno;
13239 [ + + ]: 1172 : reg < reglocs[i].regno + reglocs[i].count;
13240 : 848 : ++reg)
13241 : : {
13242 [ - + ]: 848 : assert (reg < maxnreg);
13243 : 848 : if (reg > maxreg)
13244 : : maxreg = reg;
13245 : 848 : struct register_info *info = ®s[reg];
13246 : 848 : info->regloc = ®locs[i];
13247 : 848 : info->regno = reg;
13248 : 848 : info->set = register_info (ebl, reg, ®locs[i],
13249 : 848 : info->name, &info->bits, &info->type);
13250 : : }
13251 : 36 : qsort (regs, maxreg + 1, sizeof regs[0], &compare_registers);
13252 : :
13253 : : /* Collect the unique sets and sort them. */
13254 : 36 : struct register_info *sets[maxreg + 1];
13255 : 36 : sets[0] = ®s[0];
13256 : 36 : size_t nsets = 1;
13257 [ + + ]: 2558 : for (int i = 1; i <= maxreg; ++i)
13258 [ + + ]: 2522 : if (regs[i].regloc != NULL
13259 [ + + ]: 812 : && !same_set (®s[i], ®s[i - 1], regs, maxnreg))
13260 : 32 : sets[nsets++] = ®s[i];
13261 : 36 : qsort (sets, nsets, sizeof sets[0], &compare_register_sets);
13262 : :
13263 : : /* Write out all the sets. */
13264 : 36 : unsigned int colno = 0;
13265 [ + + ]: 104 : for (size_t i = 0; i < nsets; ++i)
13266 : : {
13267 : : /* Find the longest name of a register in this set. */
13268 : 68 : size_t maxname = 0;
13269 : 68 : const struct register_info *end;
13270 [ + + ]: 916 : for (end = sets[i]; same_set (sets[i], end, regs, maxnreg); ++end)
13271 : : {
13272 : 848 : size_t len = strlen (end->name);
13273 : 848 : if (len > maxname)
13274 : : maxname = len;
13275 : : }
13276 : :
13277 : : for (const struct register_info *reg = sets[i];
13278 [ + + ]: 392 : reg < end;
13279 : 324 : reg += reg->regloc->count ?: 1)
13280 [ + - ]: 324 : colno = handle_core_register (ebl, core, maxname,
13281 : 324 : reg->regloc, desc, colno);
13282 : :
13283 : : /* Force a line break at the end of the group. */
13284 : 68 : colno = WRAP_COLUMN;
13285 : : }
13286 : :
13287 : : return colno;
13288 : : }
13289 : :
13290 : : static void
13291 : 18 : handle_auxv_note (Ebl *ebl, Elf *core, GElf_Word descsz, GElf_Off desc_pos)
13292 : : {
13293 : 18 : Elf_Data *data = elf_getdata_rawchunk (core, desc_pos, descsz, ELF_T_AUXV);
13294 [ - + ]: 18 : if (data == NULL)
13295 : 0 : elf_error:
13296 : 0 : error_exit (0, _("cannot convert core note data: %s"), elf_errmsg (-1));
13297 : :
13298 : 18 : const size_t nauxv = descsz / gelf_fsize (core, ELF_T_AUXV, 1, EV_CURRENT);
13299 [ + + ]: 354 : for (size_t i = 0; i < nauxv; ++i)
13300 : : {
13301 : 336 : GElf_auxv_t av_mem;
13302 : 336 : GElf_auxv_t *av = gelf_getauxv (data, i, &av_mem);
13303 [ - + ]: 336 : if (av == NULL)
13304 : 0 : goto elf_error;
13305 : :
13306 : 336 : const char *name;
13307 : 336 : const char *fmt;
13308 [ - + ]: 336 : if (ebl_auxv_info (ebl, av->a_type, &name, &fmt) == 0)
13309 : : {
13310 : : /* Unknown type. */
13311 [ # # ]: 0 : if (av->a_un.a_val == 0)
13312 : 0 : printf (" %" PRIu64 "\n", av->a_type);
13313 : : else
13314 : 0 : printf (" %" PRIu64 ": %#" PRIx64 "\n",
13315 : : av->a_type, av->a_un.a_val);
13316 : : }
13317 : : else
13318 [ + + + - : 336 : switch (fmt[0])
+ - ]
13319 : : {
13320 : 18 : case '\0': /* Normally zero. */
13321 [ + - ]: 18 : if (av->a_un.a_val == 0)
13322 : : {
13323 : 18 : printf (" %s\n", name);
13324 : 18 : break;
13325 : : }
13326 : 148 : FALLTHROUGH;
13327 : : case 'x': /* hex */
13328 : : case 'p': /* address */
13329 : : case 's': /* address of string */
13330 : 148 : printf (" %s: %#" PRIx64 "\n", name, av->a_un.a_val);
13331 : 148 : break;
13332 : 162 : case 'u':
13333 : 162 : printf (" %s: %" PRIu64 "\n", name, av->a_un.a_val);
13334 : 162 : break;
13335 : 0 : case 'd':
13336 : 0 : printf (" %s: %" PRId64 "\n", name, av->a_un.a_val);
13337 : 0 : break;
13338 : :
13339 : 8 : case 'b':
13340 : 8 : printf (" %s: %#" PRIx64 " ", name, av->a_un.a_val);
13341 : 8 : GElf_Xword bit = 1;
13342 : 8 : const char *pfx = "<";
13343 [ + + ]: 220 : for (const char *p = fmt + 1; *p != 0; p = strchr (p, '\0') + 1)
13344 : : {
13345 [ + + ]: 212 : if (av->a_un.a_val & bit)
13346 : : {
13347 : 154 : printf ("%s%s", pfx, p);
13348 : 154 : pfx = " ";
13349 : : }
13350 : 212 : bit <<= 1;
13351 : : }
13352 : 8 : printf (">\n");
13353 : 8 : break;
13354 : :
13355 : 0 : default:
13356 : 0 : abort ();
13357 : : }
13358 : : }
13359 : 18 : }
13360 : :
13361 : : static bool
13362 : 390 : buf_has_data (unsigned char const *ptr, unsigned char const *end, size_t sz)
13363 : : {
13364 [ + - + - ]: 390 : return ptr < end && (size_t) (end - ptr) >= sz;
13365 : : }
13366 : :
13367 : : static bool
13368 : 36 : buf_read_int (Elf *core, unsigned char const **ptrp, unsigned char const *end,
13369 : : int *retp)
13370 : : {
13371 [ + - ]: 36 : if (! buf_has_data (*ptrp, end, 4))
13372 : : return false;
13373 : :
13374 : 36 : *ptrp = convert (core, ELF_T_WORD, 1, retp, *ptrp, 4);
13375 : 36 : return true;
13376 : : }
13377 : :
13378 : : static bool
13379 : 354 : buf_read_ulong (Elf *core, unsigned char const **ptrp, unsigned char const *end,
13380 : : uint64_t *retp)
13381 : : {
13382 : 354 : size_t sz = gelf_fsize (core, ELF_T_ADDR, 1, EV_CURRENT);
13383 [ + - ]: 354 : if (! buf_has_data (*ptrp, end, sz))
13384 : : return false;
13385 : :
13386 : 354 : union
13387 : : {
13388 : : uint64_t u64;
13389 : : uint32_t u32;
13390 : : } u;
13391 : :
13392 : 354 : *ptrp = convert (core, ELF_T_ADDR, 1, &u, *ptrp, sz);
13393 : :
13394 [ + + ]: 354 : if (sz == 4)
13395 : 186 : *retp = u.u32;
13396 : : else
13397 : 168 : *retp = u.u64;
13398 : : return true;
13399 : : }
13400 : :
13401 : : static void
13402 : 12 : handle_siginfo_note (Elf *core, GElf_Word descsz, GElf_Off desc_pos)
13403 : : {
13404 : 12 : Elf_Data *data = elf_getdata_rawchunk (core, desc_pos, descsz, ELF_T_BYTE);
13405 [ - + ]: 12 : if (data == NULL)
13406 : 0 : error_exit (0, _("cannot convert core note data: %s"), elf_errmsg (-1));
13407 : :
13408 : 12 : unsigned char const *ptr = data->d_buf;
13409 : 12 : unsigned char const *const end = data->d_buf + data->d_size;
13410 : :
13411 : : /* Siginfo head is three ints: signal number, error number, origin
13412 : : code. */
13413 : 12 : int si_signo, si_errno, si_code;
13414 [ - + ]: 12 : if (! buf_read_int (core, &ptr, end, &si_signo)
13415 [ - + ]: 12 : || ! buf_read_int (core, &ptr, end, &si_errno)
13416 [ - + ]: 12 : || ! buf_read_int (core, &ptr, end, &si_code))
13417 : : {
13418 : 0 : fail:
13419 : 0 : printf (" Not enough data in NT_SIGINFO note.\n");
13420 : 0 : return;
13421 : : }
13422 : :
13423 : : /* Next is a pointer-aligned union of structures. On 64-bit
13424 : : machines, that implies a word of padding. */
13425 [ + + ]: 12 : if (gelf_getclass (core) == ELFCLASS64)
13426 : 6 : ptr += 4;
13427 : :
13428 : 12 : printf (" si_signo: %d, si_errno: %d, si_code: %d\n",
13429 : : si_signo, si_errno, si_code);
13430 : :
13431 [ + - ]: 12 : if (si_code > 0)
13432 [ + - ]: 12 : switch (si_signo)
13433 : : {
13434 : 12 : case CORE_SIGILL:
13435 : : case CORE_SIGFPE:
13436 : : case CORE_SIGSEGV:
13437 : : case CORE_SIGBUS:
13438 : : {
13439 : 12 : uint64_t addr;
13440 [ - + ]: 12 : if (! buf_read_ulong (core, &ptr, end, &addr))
13441 : 0 : goto fail;
13442 : 12 : printf (" fault address: %#" PRIx64 "\n", addr);
13443 : 12 : break;
13444 : : }
13445 : : default:
13446 : : ;
13447 : : }
13448 [ # # ]: 0 : else if (si_code == CORE_SI_USER)
13449 : : {
13450 : 0 : int pid, uid;
13451 [ # # ]: 0 : if (! buf_read_int (core, &ptr, end, &pid)
13452 [ # # ]: 0 : || ! buf_read_int (core, &ptr, end, &uid))
13453 : 0 : goto fail;
13454 : 0 : printf (" sender PID: %d, sender UID: %d\n", pid, uid);
13455 : : }
13456 : : }
13457 : :
13458 : : static void
13459 : 12 : handle_file_note (Elf *core, GElf_Word descsz, GElf_Off desc_pos)
13460 : : {
13461 : 12 : Elf_Data *data = elf_getdata_rawchunk (core, desc_pos, descsz, ELF_T_BYTE);
13462 [ - + ]: 12 : if (data == NULL)
13463 : 0 : error_exit (0, _("cannot convert core note data: %s"), elf_errmsg (-1));
13464 : :
13465 : 12 : unsigned char const *ptr = data->d_buf;
13466 : 12 : unsigned char const *const end = data->d_buf + data->d_size;
13467 : :
13468 : 12 : uint64_t count, page_size;
13469 [ - + ]: 12 : if (! buf_read_ulong (core, &ptr, end, &count)
13470 [ - + ]: 12 : || ! buf_read_ulong (core, &ptr, end, &page_size))
13471 : : {
13472 : 0 : fail:
13473 : 0 : printf (" Not enough data in NT_FILE note.\n");
13474 : 0 : return;
13475 : : }
13476 : :
13477 : 12 : size_t addrsize = gelf_fsize (core, ELF_T_ADDR, 1, EV_CURRENT);
13478 : 12 : uint64_t maxcount = (size_t) (end - ptr) / (3 * addrsize);
13479 [ - + ]: 12 : if (count > maxcount)
13480 : 0 : goto fail;
13481 : :
13482 : : /* Where file names are stored. */
13483 : 12 : unsigned char const *const fstart = ptr + 3 * count * addrsize;
13484 : 12 : char const *fptr = (char *) fstart;
13485 : :
13486 : 12 : printf (" %" PRId64 " files:\n", count);
13487 [ + + ]: 118 : for (uint64_t i = 0; i < count; ++i)
13488 : : {
13489 : 106 : uint64_t mstart, mend, moffset;
13490 [ + - ]: 106 : if (! buf_read_ulong (core, &ptr, fstart, &mstart)
13491 [ + - ]: 106 : || ! buf_read_ulong (core, &ptr, fstart, &mend)
13492 [ - + ]: 106 : || ! buf_read_ulong (core, &ptr, fstart, &moffset))
13493 : 0 : goto fail;
13494 : :
13495 : 106 : const char *fnext = memchr (fptr, '\0', (char *) end - fptr);
13496 [ - + ]: 106 : if (fnext == NULL)
13497 : 0 : goto fail;
13498 : :
13499 : 106 : int ct = printf (" %08" PRIx64 "-%08" PRIx64
13500 : : " %08" PRIx64 " %" PRId64,
13501 : : mstart, mend, moffset * page_size, mend - mstart);
13502 [ + - ]: 106 : printf ("%*s%s\n", ct > 50 ? 3 : 53 - ct, "", fptr);
13503 : :
13504 : 106 : fptr = fnext + 1;
13505 : : }
13506 : : }
13507 : :
13508 : : static void
13509 : 76 : handle_core_note (Ebl *ebl, const GElf_Ehdr *ehdr, const GElf_Nhdr *nhdr,
13510 : : const char *name, const void *desc)
13511 : : {
13512 : 76 : GElf_Word regs_offset;
13513 : 76 : size_t nregloc;
13514 : 76 : const Ebl_Register_Location *reglocs;
13515 : 76 : size_t nitems;
13516 : 76 : const Ebl_Core_Item *items;
13517 : :
13518 [ + + ]: 76 : if (! ebl_core_note (ebl, nhdr, name, desc,
13519 : : ®s_offset, &nregloc, ®locs, &nitems, &items))
13520 : 2 : return;
13521 : :
13522 : : /* Pass 0 for DESCSZ when there are registers in the note,
13523 : : so that the ITEMS array does not describe the whole thing.
13524 : : For non-register notes, the actual descsz might be a multiple
13525 : : of the unit size, not just exactly the unit size. */
13526 : 74 : unsigned int colno = handle_core_items (ebl->elf, ehdr, desc,
13527 [ + + ]: 74 : nregloc == 0 ? nhdr->n_descsz : 0,
13528 : : items, nitems);
13529 [ + + ]: 74 : if (colno != 0)
13530 : 68 : putchar ('\n');
13531 : :
13532 : 74 : colno = handle_core_registers (ebl, ebl->elf, desc + regs_offset,
13533 : : reglocs, nregloc);
13534 [ + + ]: 74 : if (colno != 0)
13535 : 36 : putchar ('\n');
13536 : : }
13537 : :
13538 : : static void
13539 : 506 : handle_notes_data (Ebl *ebl, const GElf_Ehdr *ehdr,
13540 : : GElf_Off start, Elf_Data *data)
13541 : : {
13542 : 506 : fputs (_(" Owner Data size Type\n"), stdout);
13543 : :
13544 [ - + ]: 506 : if (data == NULL)
13545 : 0 : goto bad_note;
13546 : :
13547 : : size_t offset = 0;
13548 : : GElf_Nhdr nhdr;
13549 : : size_t name_offset;
13550 : : size_t desc_offset;
13551 : 5120 : while (offset < data->d_size
13552 [ + + + - ]: 5120 : && (offset = gelf_getnote (data, offset,
13553 : : &nhdr, &name_offset, &desc_offset)) > 0)
13554 : : {
13555 [ + - ]: 4614 : const char *name = nhdr.n_namesz == 0 ? "" : data->d_buf + name_offset;
13556 : 4614 : const char *desc = data->d_buf + desc_offset;
13557 : :
13558 : : /* GNU Build Attributes are weird, they store most of their data
13559 : : into the owner name field. Extract just the owner name
13560 : : prefix here, then use the rest later as data. */
13561 : 4614 : bool is_gnu_build_attr
13562 : 4614 : = startswith (name, ELF_NOTE_GNU_BUILD_ATTRIBUTE_PREFIX);
13563 : 9798 : const char *print_name = (is_gnu_build_attr
13564 [ + + ]: 4614 : ? ELF_NOTE_GNU_BUILD_ATTRIBUTE_PREFIX : name);
13565 : 5184 : size_t print_namesz = (is_gnu_build_attr
13566 : 570 : ? strlen (print_name) : nhdr.n_namesz);
13567 : :
13568 : 4614 : char buf[100];
13569 : 4614 : char buf2[100];
13570 : 4614 : printf (_(" %-13.*s %9" PRId32 " %s\n"),
13571 : : (int) print_namesz, print_name, nhdr.n_descsz,
13572 [ + + ]: 4614 : ehdr->e_type == ET_CORE
13573 : 118 : ? ebl_core_note_type_name (ebl, nhdr.n_type,
13574 : : buf, sizeof (buf))
13575 : 4496 : : ebl_object_note_type_name (ebl, name, nhdr.n_type,
13576 : : nhdr.n_descsz,
13577 : : buf2, sizeof (buf2)));
13578 : :
13579 : : /* Filter out invalid entries. */
13580 : 4614 : if (memchr (name, '\0', nhdr.n_namesz) != NULL
13581 : : /* XXX For now help broken Linux kernels. */
13582 : : || 1)
13583 : : {
13584 [ + + ]: 4614 : if (ehdr->e_type == ET_CORE)
13585 : : {
13586 [ + + ]: 118 : if (nhdr.n_type == NT_AUXV
13587 [ + - ]: 18 : && (nhdr.n_namesz == 4 /* Broken old Linux kernels. */
13588 [ + - + - ]: 18 : || (nhdr.n_namesz == 5 && name[4] == '\0'))
13589 [ + - ]: 18 : && !memcmp (name, "CORE", 4))
13590 : 18 : handle_auxv_note (ebl, ebl->elf, nhdr.n_descsz,
13591 : : start + desc_offset);
13592 [ + + + - ]: 100 : else if (nhdr.n_namesz == 5 && strcmp (name, "CORE") == 0)
13593 [ + + + ]: 76 : switch (nhdr.n_type)
13594 : : {
13595 : 12 : case NT_SIGINFO:
13596 : 12 : handle_siginfo_note (ebl->elf, nhdr.n_descsz,
13597 : : start + desc_offset);
13598 : 12 : break;
13599 : :
13600 : 12 : case NT_FILE:
13601 : 12 : handle_file_note (ebl->elf, nhdr.n_descsz,
13602 : : start + desc_offset);
13603 : 12 : break;
13604 : :
13605 : 52 : default:
13606 : 52 : handle_core_note (ebl, ehdr, &nhdr, name, desc);
13607 : : }
13608 : : else
13609 : 24 : handle_core_note (ebl, ehdr, &nhdr, name, desc);
13610 : : }
13611 : : else
13612 : 4496 : ebl_object_note (ebl, nhdr.n_namesz, name, nhdr.n_type,
13613 : : nhdr.n_descsz, desc);
13614 : : }
13615 : : }
13616 : :
13617 [ + - ]: 506 : if (offset == data->d_size)
13618 : 506 : return;
13619 : :
13620 : 0 : bad_note:
13621 : 0 : error (0, 0,
13622 : 0 : _("cannot get content of note: %s"),
13623 : 0 : data != NULL ? "garbage data" : elf_errmsg (-1));
13624 : : }
13625 : :
13626 : : static void
13627 : 230 : handle_notes (Ebl *ebl, GElf_Ehdr *ehdr)
13628 : : {
13629 : : /* If we have section headers, just look for SHT_NOTE sections.
13630 : : In a debuginfo file, the program headers are not reliable. */
13631 [ + + ]: 230 : if (shnum != 0)
13632 : : {
13633 : : /* Get the section header string table index. */
13634 : 208 : size_t shstrndx;
13635 [ - + ]: 208 : if (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0)
13636 : 0 : error_exit (0, _("cannot get section header string table index"));
13637 : :
13638 : : Elf_Scn *scn = NULL;
13639 [ + + ]: 6446 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
13640 : : {
13641 : 6238 : GElf_Shdr shdr_mem;
13642 : 6238 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
13643 : :
13644 [ + - + + ]: 6238 : if (shdr == NULL || shdr->sh_type != SHT_NOTE)
13645 : : /* Not what we are looking for. */
13646 : 5756 : continue;
13647 : :
13648 [ - + ]: 482 : if (notes_section != NULL)
13649 : : {
13650 : 0 : char *sname = elf_strptr (ebl->elf, shstrndx, shdr->sh_name);
13651 [ # # # # ]: 0 : if (sname == NULL || strcmp (sname, notes_section) != 0)
13652 : 0 : continue;
13653 : : }
13654 : :
13655 : 482 : printf (_("\
13656 : : \nNote section [%2zu] '%s' of %" PRIu64 " bytes at offset %#0" PRIx64 ":\n"),
13657 : : elf_ndxscn (scn),
13658 : 482 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
13659 : : shdr->sh_size, shdr->sh_offset);
13660 : :
13661 : 482 : handle_notes_data (ebl, ehdr, shdr->sh_offset,
13662 : : elf_getdata (scn, NULL));
13663 : : }
13664 : 208 : return;
13665 : : }
13666 : :
13667 : : /* We have to look through the program header to find the note
13668 : : sections. There can be more than one. */
13669 [ + + ]: 280 : for (size_t cnt = 0; cnt < phnum; ++cnt)
13670 : : {
13671 : 258 : GElf_Phdr mem;
13672 : 258 : GElf_Phdr *phdr = gelf_getphdr (ebl->elf, cnt, &mem);
13673 : :
13674 [ + - + + ]: 258 : if (phdr == NULL || phdr->p_type != PT_NOTE)
13675 : : /* Not what we are looking for. */
13676 : 234 : continue;
13677 : :
13678 : 24 : printf (_("\
13679 : : \nNote segment of %" PRIu64 " bytes at offset %#0" PRIx64 ":\n"),
13680 : : phdr->p_filesz, phdr->p_offset);
13681 : :
13682 : 24 : handle_notes_data (ebl, ehdr, phdr->p_offset,
13683 : : elf_getdata_rawchunk (ebl->elf,
13684 : 24 : phdr->p_offset, phdr->p_filesz,
13685 [ + + ]: 24 : (phdr->p_align == 8
13686 : : ? ELF_T_NHDR8 : ELF_T_NHDR)));
13687 : : }
13688 : : }
13689 : :
13690 : :
13691 : : static void
13692 : 12 : hex_dump (const uint8_t *data, size_t len)
13693 : : {
13694 : 12 : size_t pos = 0;
13695 [ + + ]: 56 : while (pos < len)
13696 : : {
13697 : 44 : printf (" 0x%08zx ", pos);
13698 : :
13699 : 44 : const size_t chunk = MIN (len - pos, 16);
13700 : :
13701 [ + + ]: 688 : for (size_t i = 0; i < chunk; ++i)
13702 [ + + ]: 644 : if (i % 4 == 3)
13703 : 160 : printf ("%02x ", data[pos + i]);
13704 : : else
13705 : 484 : printf ("%02x", data[pos + i]);
13706 : :
13707 [ + + ]: 44 : if (chunk < 16)
13708 : 4 : printf ("%*s", (int) ((16 - chunk) * 2 + (16 - chunk + 3) / 4), "");
13709 : :
13710 [ + + ]: 688 : for (size_t i = 0; i < chunk; ++i)
13711 : : {
13712 : 644 : unsigned char b = data[pos + i];
13713 [ + + ]: 1288 : printf ("%c", isprint (b) ? b : '.');
13714 : : }
13715 : :
13716 : 44 : putchar ('\n');
13717 : 44 : pos += chunk;
13718 : : }
13719 : 12 : }
13720 : :
13721 : : static void
13722 : 12 : dump_data_section (Elf_Scn *scn, const GElf_Shdr *shdr, const char *name)
13723 : : {
13724 [ + - - + ]: 12 : if (shdr->sh_size == 0 || shdr->sh_type == SHT_NOBITS)
13725 : 0 : printf (_("\nSection [%zu] '%s' has no data to dump.\n"),
13726 : : elf_ndxscn (scn), name);
13727 : : else
13728 : : {
13729 [ + + ]: 12 : if (print_decompress)
13730 : : {
13731 : : /* We try to decompress the section, but keep the old shdr around
13732 : : so we can show both the original shdr size and the uncompressed
13733 : : data size. */
13734 [ + + ]: 8 : if ((shdr->sh_flags & SHF_COMPRESSED) != 0)
13735 : : {
13736 [ - + ]: 4 : if (elf_compress (scn, 0, 0) < 0)
13737 : 0 : printf ("WARNING: %s [%zd]\n",
13738 : : _("Couldn't uncompress section"),
13739 : : elf_ndxscn (scn));
13740 : : }
13741 [ + - + - ]: 4 : else if (name && startswith (name, ".zdebug"))
13742 : : {
13743 [ - + ]: 4 : if (elf_compress_gnu (scn, 0, 0) < 0)
13744 : 0 : printf ("WARNING: %s [%zd]\n",
13745 : : _("Couldn't uncompress section"),
13746 : : elf_ndxscn (scn));
13747 : : }
13748 : : }
13749 : :
13750 : 12 : Elf_Data *data = elf_rawdata (scn, NULL);
13751 [ - + ]: 12 : if (data == NULL)
13752 : 0 : error (0, 0, _("cannot get data for section [%zu] '%s': %s"),
13753 : : elf_ndxscn (scn), name, elf_errmsg (-1));
13754 : : else
13755 : : {
13756 [ + + ]: 12 : if (data->d_size == shdr->sh_size)
13757 : 4 : printf (_("\nHex dump of section [%zu] '%s', %" PRIu64
13758 : : " bytes at offset %#0" PRIx64 ":\n"),
13759 : : elf_ndxscn (scn), name,
13760 : 4 : shdr->sh_size, shdr->sh_offset);
13761 : : else
13762 : 8 : printf (_("\nHex dump of section [%zu] '%s', %" PRIu64
13763 : : " bytes (%zd uncompressed) at offset %#0"
13764 : : PRIx64 ":\n"),
13765 : : elf_ndxscn (scn), name,
13766 : 8 : shdr->sh_size, data->d_size, shdr->sh_offset);
13767 : 12 : hex_dump (data->d_buf, data->d_size);
13768 : : }
13769 : : }
13770 : 12 : }
13771 : :
13772 : : static void
13773 : 374 : print_string_section (Elf_Scn *scn, const GElf_Shdr *shdr, const char *name)
13774 : : {
13775 [ + - + + ]: 374 : if (shdr->sh_size == 0 || shdr->sh_type == SHT_NOBITS)
13776 : 42 : printf (_("\nSection [%zu] '%s' has no strings to dump.\n"),
13777 : : elf_ndxscn (scn), name);
13778 : : else
13779 : : {
13780 [ + + ]: 332 : if (print_decompress)
13781 : : {
13782 : : /* We try to decompress the section, but keep the old shdr around
13783 : : so we can show both the original shdr size and the uncompressed
13784 : : data size. */
13785 [ - + ]: 2 : if ((shdr->sh_flags & SHF_COMPRESSED) != 0)
13786 : : {
13787 [ # # ]: 0 : if (elf_compress (scn, 0, 0) < 0)
13788 : 0 : printf ("WARNING: %s [%zd]\n",
13789 : : _("Couldn't uncompress section"),
13790 : : elf_ndxscn (scn));
13791 : : }
13792 [ + - + - ]: 2 : else if (name && startswith (name, ".zdebug"))
13793 : : {
13794 [ - + ]: 2 : if (elf_compress_gnu (scn, 0, 0) < 0)
13795 : 0 : printf ("WARNING: %s [%zd]\n",
13796 : : _("Couldn't uncompress section"),
13797 : : elf_ndxscn (scn));
13798 : : }
13799 : : }
13800 : :
13801 : 332 : Elf_Data *data = elf_rawdata (scn, NULL);
13802 [ - + ]: 332 : if (data == NULL)
13803 : 0 : error (0, 0, _("cannot get data for section [%zu] '%s': %s"),
13804 : : elf_ndxscn (scn), name, elf_errmsg (-1));
13805 : : else
13806 : : {
13807 [ + + ]: 332 : if (data->d_size == shdr->sh_size)
13808 : 330 : printf (_("\nString section [%zu] '%s' contains %" PRIu64
13809 : : " bytes at offset %#0" PRIx64 ":\n"),
13810 : : elf_ndxscn (scn), name,
13811 : 330 : shdr->sh_size, shdr->sh_offset);
13812 : : else
13813 : 2 : printf (_("\nString section [%zu] '%s' contains %" PRIu64
13814 : : " bytes (%zd uncompressed) at offset %#0"
13815 : : PRIx64 ":\n"),
13816 : : elf_ndxscn (scn), name,
13817 : 2 : shdr->sh_size, data->d_size, shdr->sh_offset);
13818 : :
13819 : 332 : const char *start = data->d_buf;
13820 : 332 : const char *const limit = start + data->d_size;
13821 : 47014 : do
13822 : : {
13823 : 47014 : const char *end = memchr (start, '\0', limit - start);
13824 : 47014 : const size_t pos = start - (const char *) data->d_buf;
13825 [ - + ]: 47014 : if (unlikely (end == NULL))
13826 : : {
13827 : 0 : printf (" [%6zx]- %.*s\n",
13828 : : pos, (int) (limit - start), start);
13829 : 0 : break;
13830 : : }
13831 : 47014 : printf (" [%6zx] %s\n", pos, start);
13832 : 47014 : start = end + 1;
13833 [ + + ]: 47014 : } while (start < limit);
13834 : : }
13835 : : }
13836 : 374 : }
13837 : :
13838 : : static void
13839 : 198 : for_each_section_argument (Elf *elf, const struct section_argument *list,
13840 : : void (*dump) (Elf_Scn *scn, const GElf_Shdr *shdr,
13841 : : const char *name))
13842 : : {
13843 : : /* Get the section header string table index. */
13844 : 198 : size_t shstrndx;
13845 [ - + ]: 198 : if (elf_getshdrstrndx (elf, &shstrndx) < 0)
13846 : 0 : error_exit (0, _("cannot get section header string table index"));
13847 : :
13848 [ + + ]: 764 : for (const struct section_argument *a = list; a != NULL; a = a->next)
13849 : : {
13850 : 566 : Elf_Scn *scn;
13851 : 566 : GElf_Shdr shdr_mem;
13852 : 566 : const char *name = NULL;
13853 : :
13854 : 566 : char *endp = NULL;
13855 : 566 : unsigned long int shndx = strtoul (a->arg, &endp, 0);
13856 [ + + - + ]: 566 : if (endp != a->arg && *endp == '\0')
13857 : : {
13858 : 2 : scn = elf_getscn (elf, shndx);
13859 [ - + ]: 2 : if (scn == NULL)
13860 : : {
13861 : 0 : error (0, 0, _("\nsection [%lu] does not exist"), shndx);
13862 : 0 : continue;
13863 : : }
13864 : :
13865 [ - + ]: 2 : if (gelf_getshdr (scn, &shdr_mem) == NULL)
13866 : 0 : error_exit (0, _("cannot get section header: %s"),
13867 : : elf_errmsg (-1));
13868 : 2 : name = elf_strptr (elf, shstrndx, shdr_mem.sh_name);
13869 : 2 : (*dump) (scn, &shdr_mem, name);
13870 : : }
13871 : : else
13872 : : {
13873 : : /* Need to look up the section by name. */
13874 : : scn = NULL;
13875 : : bool found = false;
13876 [ + + ]: 18180 : while ((scn = elf_nextscn (elf, scn)) != NULL)
13877 : : {
13878 [ - + ]: 17616 : if (gelf_getshdr (scn, &shdr_mem) == NULL)
13879 : 0 : continue;
13880 : 17616 : name = elf_strptr (elf, shstrndx, shdr_mem.sh_name);
13881 [ - + ]: 17616 : if (name == NULL)
13882 : 0 : continue;
13883 [ + + ]: 17616 : if (!strcmp (name, a->arg))
13884 : : {
13885 : 384 : found = true;
13886 : 384 : (*dump) (scn, &shdr_mem, name);
13887 : : }
13888 : : }
13889 : :
13890 [ + + - + ]: 564 : if (unlikely (!found) && !a->implicit)
13891 : 0 : error (0, 0, _("\nsection '%s' does not exist"), a->arg);
13892 : : }
13893 : : }
13894 : 198 : }
13895 : :
13896 : : static void
13897 : 12 : dump_data (Ebl *ebl)
13898 : : {
13899 : 12 : for_each_section_argument (ebl->elf, dump_data_sections, &dump_data_section);
13900 : 12 : }
13901 : :
13902 : : static void
13903 : 186 : dump_strings (Ebl *ebl)
13904 : : {
13905 : 186 : for_each_section_argument (ebl->elf, string_sections, &print_string_section);
13906 : 186 : }
13907 : :
13908 : : static void
13909 : 0 : print_strings (Ebl *ebl)
13910 : : {
13911 : : /* Get the section header string table index. */
13912 : 0 : size_t shstrndx;
13913 [ # # ]: 0 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
13914 : 0 : error_exit (0, _("cannot get section header string table index"));
13915 : :
13916 : : Elf_Scn *scn;
13917 : : GElf_Shdr shdr_mem;
13918 : : const char *name;
13919 : : scn = NULL;
13920 [ # # ]: 0 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
13921 : : {
13922 [ # # ]: 0 : if (gelf_getshdr (scn, &shdr_mem) == NULL)
13923 : 0 : continue;
13924 : :
13925 [ # # ]: 0 : if (shdr_mem.sh_type != SHT_PROGBITS
13926 [ # # ]: 0 : || !(shdr_mem.sh_flags & SHF_STRINGS))
13927 : 0 : continue;
13928 : :
13929 : 0 : name = elf_strptr (ebl->elf, shstrndx, shdr_mem.sh_name);
13930 [ # # ]: 0 : if (name == NULL)
13931 : 0 : continue;
13932 : :
13933 : 0 : print_string_section (scn, &shdr_mem, name);
13934 : : }
13935 : 0 : }
13936 : :
13937 : : static void
13938 : 4 : dump_archive_index (Elf *elf, const char *fname)
13939 : : {
13940 : 4 : size_t narsym;
13941 : 4 : const Elf_Arsym *arsym = elf_getarsym (elf, &narsym);
13942 [ - + ]: 4 : if (arsym == NULL)
13943 : : {
13944 : 0 : int result = elf_errno ();
13945 [ # # ]: 0 : if (unlikely (result != ELF_E_NO_INDEX))
13946 : 0 : error_exit (0, _("cannot get symbol index of archive '%s': %s"),
13947 : : fname, elf_errmsg (result));
13948 : : else
13949 : 0 : printf (_("\nArchive '%s' has no symbol index\n"), fname);
13950 : 0 : return;
13951 : : }
13952 : :
13953 : 4 : printf (_("\nIndex of archive '%s' has %zu entries:\n"),
13954 : : fname, narsym);
13955 : :
13956 : 4 : size_t as_off = 0;
13957 [ + + ]: 22 : for (const Elf_Arsym *s = arsym; s < &arsym[narsym - 1]; ++s)
13958 : : {
13959 [ + + ]: 18 : if (s->as_off != as_off)
13960 : : {
13961 : 12 : as_off = s->as_off;
13962 : :
13963 : 12 : Elf *subelf = NULL;
13964 [ + - ]: 12 : if (unlikely (elf_rand (elf, as_off) == 0)
13965 [ - + ]: 12 : || unlikely ((subelf = elf_begin (-1, ELF_C_READ_MMAP, elf))
13966 : : == NULL))
13967 : : #if __GLIBC__ < 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ < 7)
13968 : : while (1)
13969 : : #endif
13970 : 0 : error_exit (0,
13971 : : _("cannot extract member at offset %zu in '%s': %s"),
13972 : : as_off, fname, elf_errmsg (-1));
13973 : :
13974 : 12 : const Elf_Arhdr *h = elf_getarhdr (subelf);
13975 [ + - ]: 12 : if (h != NULL)
13976 : 12 : printf (_("Archive member '%s' contains:\n"), h->ar_name);
13977 : :
13978 : 12 : elf_end (subelf);
13979 : : }
13980 : :
13981 : 18 : printf ("\t%s\n", s->as_name);
13982 : : }
13983 : : }
13984 : :
13985 : : #include "debugpred.h"
|