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: %" PRIu16 " %s\n"),
1381 : 190 : ehdr->e_ehsize, _("(bytes)"));
1382 : :
1383 : 380 : printf (_(" Size of program header entries: %" PRIu16 " %s\n"),
1384 : 190 : ehdr->e_phentsize, _("(bytes)"));
1385 : :
1386 : 380 : printf (_(" Number of program headers entries: %" PRIu16),
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: %" PRIu16 " %s\n"),
1401 : 190 : ehdr->e_shentsize, _("(bytes)"));
1402 : :
1403 : 380 : printf (_(" Number of section headers entries: %" PRIu16),
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: %" PRIu16 "\n\n"),
1436 : : ehdr->e_shstrndx);
1437 : 190 : }
1438 : :
1439 : :
1440 : : static const char *
1441 : 80490 : get_visibility_type (int value)
1442 : : {
1443 [ - + - - : 80490 : 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 [ + + ]: 1168590 : for (int cnt = 0; cnt < nentries; ++cnt)
2563 : : {
2564 : 1168308 : GElf_Rela relmem;
2565 : 1168308 : GElf_Rela *rel = gelf_getrela (data, cnt, &relmem);
2566 [ + - ]: 1168308 : if (likely (rel != NULL))
2567 : : {
2568 : 1168308 : char buf[64];
2569 : 1168308 : GElf_Sym symmem;
2570 : 1168308 : Elf32_Word xndx;
2571 : 2336616 : GElf_Sym *sym = gelf_getsymshndx (symdata, xndxdata,
2572 : 1168308 : GELF_R_SYM (rel->r_info),
2573 : : &symmem, &xndx);
2574 : :
2575 [ - + ]: 1168308 : 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 [ + + ]: 1168308 : else if (GELF_ST_TYPE (sym->st_info) != STT_SECTION
2628 [ + + ]: 630424 : && !(GELF_ST_TYPE (sym->st_info) == STT_NOTYPE
2629 [ + + ]: 103668 : && GELF_ST_BIND (sym->st_info) == STB_LOCAL
2630 [ + + ]: 102130 : && sym->st_shndx != SHN_UNDEF
2631 [ + + ]: 716 : && sym->st_value == 0)) // local start section label
2632 [ + - ]: 1260824 : printf ("\
2633 : : %#0*" PRIx64 " %-15s %#0*" PRIx64 " %+6" PRId64 " %s\n",
2634 : : class == ELFCLASS32 ? 10 : 18, rel->r_offset,
2635 [ + - ]: 630412 : 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 : 630412 : ? 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 : 630412 : 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 [ + + ]: 80756 : for (unsigned int cnt = 0; cnt < nsyms; ++cnt)
2859 : : {
2860 : 80490 : char typebuf[64];
2861 : 80490 : char bindbuf[64];
2862 : 80490 : char scnbuf[64];
2863 : 80490 : const char *sym_name;
2864 : 80490 : Elf32_Word xndx;
2865 : 80490 : GElf_Sym sym_mem;
2866 : 80490 : GElf_Sym *sym
2867 : 80490 : = gelf_getsymshndx (symdata, xndx_data, cnt, &sym_mem, &xndx);
2868 : :
2869 [ - + ]: 80490 : if (unlikely (sym == NULL))
2870 : 0 : continue;
2871 : :
2872 : : /* Determine the real section index. */
2873 [ + - ]: 80490 : if (likely (sym->st_shndx != SHN_XINDEX))
2874 : 80490 : xndx = sym->st_shndx;
2875 [ - + ]: 80490 : if (use_dynamic_segment == true)
2876 : : {
2877 [ # # ]: 0 : if (symstr_data != NULL
2878 [ # # ]: 0 : && validate_str (symstr_data->d_buf, sym->st_name,
2879 : : symstr_data->d_size))
2880 : 0 : sym_name = (char *)symstr_data->d_buf + sym->st_name;
2881 : : else
2882 : : sym_name = NULL;
2883 : : }
2884 : : else
2885 : 80490 : sym_name = elf_strptr (ebl->elf, idx, sym->st_name);
2886 : :
2887 [ - + ]: 80490 : if (sym_name == NULL)
2888 : : sym_name = "???";
2889 : :
2890 [ + + ]: 160092 : printf (_ ("\
2891 : : %5u: %0*" PRIx64 " %6" PRId64 " %-7s %-6s %-9s %6s %s"),
2892 : 80490 : cnt, gelf_getclass (ebl->elf) == ELFCLASS32 ? 8 : 16,
2893 : : sym->st_value, sym->st_size,
2894 : 80490 : ebl_symbol_type_name (ebl, GELF_ST_TYPE (sym->st_info), typebuf,
2895 : : sizeof (typebuf)),
2896 : 80490 : ebl_symbol_binding_name (ebl, GELF_ST_BIND (sym->st_info),
2897 : : bindbuf, sizeof (bindbuf)),
2898 : 80490 : get_visibility_type (GELF_ST_VISIBILITY (sym->st_other)),
2899 : 80490 : ebl_section_name (ebl, sym->st_shndx, xndx, scnbuf,
2900 : : sizeof (scnbuf), NULL, shnum),
2901 : : sym_name);
2902 : :
2903 [ + + ]: 80490 : if (versym_data != NULL)
2904 : : {
2905 : : /* Get the version information. */
2906 : 3436 : GElf_Versym versym_mem;
2907 : 3436 : GElf_Versym *versym = gelf_getversym (versym_data, cnt, &versym_mem);
2908 : :
2909 [ + - + + ]: 3436 : if (versym != NULL && ((*versym & 0x8000) != 0 || *versym > 1))
2910 : : {
2911 : 2476 : bool is_nobits = false;
2912 : 2476 : bool check_def = xndx != SHN_UNDEF;
2913 : :
2914 [ + + - + ]: 2476 : if (xndx < SHN_LORESERVE || sym->st_shndx == SHN_XINDEX)
2915 : : {
2916 : 2456 : GElf_Shdr symshdr_mem;
2917 : 2456 : GElf_Shdr *symshdr = gelf_getshdr (
2918 : : elf_getscn (ebl->elf, xndx), &symshdr_mem);
2919 : :
2920 : 2456 : is_nobits
2921 [ + - + + ]: 4888 : = (symshdr != NULL && symshdr->sh_type == SHT_NOBITS);
2922 : : }
2923 : :
2924 [ + + ]: 2476 : if (is_nobits || !check_def)
2925 : : {
2926 : : /* We must test both. */
2927 : 2172 : GElf_Vernaux vernaux_mem;
2928 : 2172 : GElf_Vernaux *vernaux = NULL;
2929 : 2172 : size_t vn_offset = 0;
2930 : :
2931 : 2172 : GElf_Verneed verneed_mem;
2932 : 2172 : GElf_Verneed *verneed
2933 : 2172 : = gelf_getverneed (verneed_data, 0, &verneed_mem);
2934 [ + - ]: 7902 : while (verneed != NULL)
2935 : : {
2936 : 7902 : size_t vna_offset = vn_offset;
2937 : :
2938 : 7902 : vernaux = gelf_getvernaux (verneed_data,
2939 : 7902 : vna_offset += verneed->vn_aux,
2940 : : &vernaux_mem);
2941 [ + + ]: 28762 : while (vernaux != NULL && vernaux->vna_other != *versym
2942 [ + + ]: 18688 : && vernaux->vna_next != 0
2943 [ + - ]: 20860 : && (verneed_data->d_size - vna_offset
2944 [ + - ]: 12958 : >= vernaux->vna_next))
2945 : : {
2946 : : /* Update the offset. */
2947 : 12958 : vna_offset += vernaux->vna_next;
2948 : :
2949 : 12958 : vernaux = (vernaux->vna_next == 0
2950 : : ? NULL
2951 : 12958 : : gelf_getvernaux (verneed_data,
2952 : : vna_offset,
2953 : : &vernaux_mem));
2954 : : }
2955 : :
2956 : : /* Check whether we found the version. */
2957 [ + - + + ]: 7902 : if (vernaux != NULL && vernaux->vna_other == *versym)
2958 : : /* Found it. */
2959 : : break;
2960 : :
2961 [ + - ]: 5730 : if (verneed_data->d_size - vn_offset < verneed->vn_next)
2962 : : break;
2963 : :
2964 : 5730 : vn_offset += verneed->vn_next;
2965 : 5730 : verneed
2966 : : = (verneed->vn_next == 0
2967 : : ? NULL
2968 [ + - ]: 5730 : : gelf_getverneed (verneed_data, vn_offset,
2969 : : &verneed_mem));
2970 : : }
2971 : :
2972 [ + - + - ]: 2172 : if (vernaux != NULL && vernaux->vna_other == *versym)
2973 : : {
2974 : 4344 : printf ("@%s (%u)",
2975 [ - + ]: 2172 : use_dynamic_segment == true
2976 : 0 : ? (char *)symstr_data->d_buf
2977 : 0 : + vernaux->vna_name
2978 : 2172 : : elf_strptr (ebl->elf, verneed_stridx,
2979 : 2172 : vernaux->vna_name),
2980 : : (unsigned int)vernaux->vna_other);
2981 : 2172 : check_def = 0;
2982 : : }
2983 [ # # ]: 0 : else if (unlikely (!is_nobits))
2984 : 0 : error (0, 0, _ ("bad dynamic symbol"));
2985 : : else
2986 : : check_def = 1;
2987 : : }
2988 : :
2989 [ + - + - ]: 2476 : if (check_def && *versym != 0x8001)
2990 : : {
2991 : : /* We must test both. */
2992 : 304 : size_t vd_offset = 0;
2993 : :
2994 : 304 : GElf_Verdef verdef_mem;
2995 : 304 : GElf_Verdef *verdef
2996 : 304 : = gelf_getverdef (verdef_data, 0, &verdef_mem);
2997 [ + - ]: 866 : while (verdef != NULL)
2998 : : {
2999 [ + + ]: 866 : if (verdef->vd_ndx == (*versym & 0x7fff))
3000 : : /* Found the definition. */
3001 : : break;
3002 : :
3003 [ + - ]: 562 : if (verdef_data->d_size - vd_offset < verdef->vd_next)
3004 : : break;
3005 : :
3006 : 562 : vd_offset += verdef->vd_next;
3007 : 562 : verdef = (verdef->vd_next == 0
3008 : : ? NULL
3009 [ + - ]: 562 : : gelf_getverdef (verdef_data, vd_offset,
3010 : : &verdef_mem));
3011 : : }
3012 : :
3013 [ + - ]: 304 : if (verdef != NULL)
3014 : : {
3015 : 304 : GElf_Verdaux verdaux_mem;
3016 : 608 : GElf_Verdaux *verdaux = gelf_getverdaux (
3017 : 304 : verdef_data, vd_offset + verdef->vd_aux,
3018 : : &verdaux_mem);
3019 : :
3020 [ + - ]: 304 : if (verdaux != NULL)
3021 [ + - ]: 608 : printf ((*versym & 0x8000) ? "@%s" : "@@%s",
3022 [ - + ]: 304 : use_dynamic_segment == true
3023 : 0 : ? (char *)symstr_data->d_buf
3024 : 0 : + verdaux->vda_name
3025 : 304 : : elf_strptr (ebl->elf, verdef_stridx,
3026 : 304 : verdaux->vda_name));
3027 : : }
3028 : : }
3029 : : }
3030 : : }
3031 : :
3032 : 80490 : putchar ('\n');
3033 : : }
3034 : 266 : }
3035 : :
3036 : :
3037 : : static bool
3038 : 266 : handle_symtab (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr)
3039 : : {
3040 : 266 : Elf_Data *versym_data = NULL;
3041 : 266 : Elf_Data *verneed_data = NULL;
3042 : 266 : Elf_Data *verdef_data = NULL;
3043 : 266 : Elf_Data *xndx_data = NULL;
3044 : 266 : int class = gelf_getclass (ebl->elf);
3045 : 266 : Elf32_Word verneed_stridx = 0;
3046 : 266 : Elf32_Word verdef_stridx = 0;
3047 : :
3048 : : /* Get the data of the section. */
3049 : 266 : Elf_Data *data = elf_getdata (scn, NULL);
3050 [ + - ]: 266 : if (data == NULL)
3051 : : return false;
3052 : :
3053 : : /* Find out whether we have other sections we might need. */
3054 : : Elf_Scn *runscn = NULL;
3055 [ + + ]: 8784 : while ((runscn = elf_nextscn (ebl->elf, runscn)) != NULL)
3056 : : {
3057 : 8518 : GElf_Shdr runshdr_mem;
3058 : 8518 : GElf_Shdr *runshdr = gelf_getshdr (runscn, &runshdr_mem);
3059 : :
3060 [ + - ]: 8518 : if (likely (runshdr != NULL))
3061 : : {
3062 [ + + ]: 8518 : if (runshdr->sh_type == SHT_GNU_versym
3063 [ + + ]: 202 : && runshdr->sh_link == elf_ndxscn (scn))
3064 : : /* Bingo, found the version information. Now get the data. */
3065 : 150 : versym_data = elf_getdata (runscn, NULL);
3066 [ + + ]: 8368 : else if (runshdr->sh_type == SHT_GNU_verneed)
3067 : : {
3068 : : /* This is the information about the needed versions. */
3069 : 202 : verneed_data = elf_getdata (runscn, NULL);
3070 : 202 : verneed_stridx = runshdr->sh_link;
3071 : : }
3072 [ + + ]: 8166 : else if (runshdr->sh_type == SHT_GNU_verdef)
3073 : : {
3074 : : /* This is the information about the defined versions. */
3075 : 8 : verdef_data = elf_getdata (runscn, NULL);
3076 : 8 : verdef_stridx = runshdr->sh_link;
3077 : : }
3078 [ + - ]: 8158 : else if (runshdr->sh_type == SHT_SYMTAB_SHNDX
3079 [ # # ]: 0 : && runshdr->sh_link == elf_ndxscn (scn))
3080 : : /* Extended section index. */
3081 : 0 : xndx_data = elf_getdata (runscn, NULL);
3082 : : }
3083 : : }
3084 : :
3085 : : /* Get the section header string table index. */
3086 : 266 : size_t shstrndx;
3087 [ - + ]: 266 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
3088 : 0 : error_exit (0, _("cannot get section header string table index"));
3089 : :
3090 : 266 : GElf_Shdr glink_mem;
3091 : 266 : GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
3092 : : &glink_mem);
3093 [ - + ]: 266 : if (glink == NULL)
3094 : 0 : error_exit (0, _("invalid sh_link value in section %zu"),
3095 : : elf_ndxscn (scn));
3096 : :
3097 : : /* Now we can compute the number of entries in the section. */
3098 : 532 : unsigned int nsyms = data->d_size / (class == ELFCLASS32
3099 : : ? sizeof (Elf32_Sym)
3100 [ + + ]: 266 : : sizeof (Elf64_Sym));
3101 : :
3102 : 266 : printf (ngettext ("\nSymbol table [%2u] '%s' contains %u entry:\n",
3103 : : "\nSymbol table [%2u] '%s' contains %u entries:\n",
3104 : : nsyms),
3105 : 266 : (unsigned int) elf_ndxscn (scn),
3106 : 266 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name), nsyms);
3107 : 266 : printf (ngettext (" %lu local symbol String table: [%2u] '%s'\n",
3108 : : " %lu local symbols String table: [%2u] '%s'\n",
3109 : : shdr->sh_info),
3110 : 266 : (unsigned long int) shdr->sh_info,
3111 : 266 : (unsigned int) shdr->sh_link,
3112 : 266 : elf_strptr (ebl->elf, shstrndx, glink->sh_name));
3113 : :
3114 [ + + ]: 532 : fputs (class == ELFCLASS32
3115 : 32 : ? _("\
3116 : : Num: Value Size Type Bind Vis Ndx Name\n")
3117 : 234 : : _("\
3118 : : Num: Value Size Type Bind Vis Ndx Name\n"),
3119 : : stdout);
3120 : :
3121 : 266 : process_symtab(ebl, nsyms, shdr->sh_link, verneed_stridx, verdef_stridx,
3122 : : data, versym_data, NULL, verneed_data, verdef_data, xndx_data);
3123 : 266 : return true;
3124 : : }
3125 : :
3126 : :
3127 : : static bool
3128 : 0 : handle_dynamic_symtab (Ebl *ebl)
3129 : : {
3130 : 0 : GElf_Phdr phdr_mem;
3131 : 0 : GElf_Phdr *phdr = NULL;
3132 : : /* phnum is a static variable which was already fetched in function
3133 : : process_elf_file. */
3134 [ # # ]: 0 : for (size_t i = 0; i < phnum; ++i)
3135 : : {
3136 : 0 : phdr = gelf_getphdr (ebl->elf, i, &phdr_mem);
3137 [ # # # # ]: 0 : if (phdr == NULL || phdr->p_type == PT_DYNAMIC)
3138 : : break;
3139 : : }
3140 [ # # ]: 0 : if (phdr == NULL)
3141 : : return false;
3142 : :
3143 : 0 : GElf_Addr addrs[i_max] = {
3144 : : 0,
3145 : : };
3146 : 0 : GElf_Off offs[i_max] = {
3147 : : 0,
3148 : : };
3149 : 0 : get_dynscn_addrs (ebl->elf, phdr, addrs);
3150 : 0 : find_offsets (ebl->elf, 0, i_max, addrs, offs);
3151 : :
3152 : 0 : size_t syments = 0;
3153 : :
3154 : 0 : GElf_Ehdr ehdr_mem;
3155 : 0 : GElf_Ehdr *ehdr = gelf_getehdr (ebl->elf, &ehdr_mem);
3156 : :
3157 [ # # ]: 0 : if (offs[i_hash] != 0)
3158 : : {
3159 : : /* In the original format, .hash says the size of .dynsym. */
3160 : :
3161 [ # # # # : 0 : size_t entsz = SH_ENTSIZE_HASH (ehdr);
# # ]
3162 : : Elf_Data *data
3163 : 0 : = elf_getdata_rawchunk (ebl->elf, offs[i_hash] + entsz, entsz,
3164 : : (entsz == 4 ? ELF_T_WORD : ELF_T_XWORD));
3165 [ # # ]: 0 : if (data != NULL)
3166 : 0 : syments = (entsz == 4 ? *(const GElf_Word *)data->d_buf
3167 [ # # ]: 0 : : *(const GElf_Xword *)data->d_buf);
3168 : : }
3169 [ # # # # ]: 0 : if (offs[i_gnu_hash] != 0 && syments == 0)
3170 : : {
3171 : : /* In the new format, we can derive it with some work. */
3172 : :
3173 : 0 : const struct
3174 : : {
3175 : : Elf32_Word nbuckets;
3176 : : Elf32_Word symndx;
3177 : : Elf32_Word maskwords;
3178 : : Elf32_Word shift2;
3179 : : } * header;
3180 : :
3181 : 0 : Elf_Data *data = elf_getdata_rawchunk (ebl->elf, offs[i_gnu_hash],
3182 : : sizeof *header, ELF_T_WORD);
3183 [ # # ]: 0 : if (data != NULL)
3184 : : {
3185 : 0 : header = data->d_buf;
3186 : 0 : Elf32_Word nbuckets = header->nbuckets;
3187 : 0 : Elf32_Word symndx = header->symndx;
3188 : 0 : GElf_Off buckets_at
3189 : : = (offs[i_gnu_hash] + sizeof *header
3190 : 0 : + (gelf_getclass (ebl->elf) * sizeof (Elf32_Word)
3191 : 0 : * header->maskwords));
3192 : :
3193 : : // elf_getdata_rawchunk takes a size_t, make sure it
3194 : : // doesn't overflow.
3195 : : #if SIZE_MAX <= UINT32_MAX
3196 : : if (nbuckets > SIZE_MAX / sizeof (Elf32_Word))
3197 : : data = NULL;
3198 : : else
3199 : : #endif
3200 : 0 : data = elf_getdata_rawchunk (ebl->elf, buckets_at,
3201 : : nbuckets * sizeof (Elf32_Word),
3202 : : ELF_T_WORD);
3203 [ # # ]: 0 : if (data != NULL && symndx < nbuckets)
3204 : : {
3205 : 0 : const Elf32_Word *const buckets = data->d_buf;
3206 : 0 : Elf32_Word maxndx = symndx;
3207 [ # # ]: 0 : for (Elf32_Word bucket = 0; bucket < nbuckets; ++bucket)
3208 : 0 : if (buckets[bucket] > maxndx)
3209 : : maxndx = buckets[bucket];
3210 : :
3211 : 0 : GElf_Off hasharr_at
3212 : : = (buckets_at + nbuckets * sizeof (Elf32_Word));
3213 : 0 : hasharr_at += (maxndx - symndx) * sizeof (Elf32_Word);
3214 : 0 : do
3215 : : {
3216 : 0 : data = elf_getdata_rawchunk (
3217 : : ebl->elf, hasharr_at, sizeof (Elf32_Word), ELF_T_WORD);
3218 [ # # # # ]: 0 : if (data != NULL && (*(const Elf32_Word *)data->d_buf & 1u))
3219 : : {
3220 : 0 : syments = maxndx + 1;
3221 : 0 : break;
3222 : : }
3223 : 0 : ++maxndx;
3224 : 0 : hasharr_at += sizeof (Elf32_Word);
3225 : : }
3226 [ # # ]: 0 : while (data != NULL);
3227 : : }
3228 : : }
3229 : : }
3230 [ # # # # ]: 0 : if (offs[i_strtab] > offs[i_symtab] && syments == 0)
3231 : 0 : syments = ((offs[i_strtab] - offs[i_symtab])
3232 : 0 : / gelf_fsize (ebl->elf, ELF_T_SYM, 1, EV_CURRENT));
3233 : :
3234 [ # # # # : 0 : if (syments <= 0 || offs[i_strtab] == 0 || offs[i_symtab] == 0)
# # ]
3235 : : {
3236 : 0 : error_exit (0, _ ("Dynamic symbol information is not available for "
3237 : : "displaying symbols."));
3238 : : }
3239 : :
3240 : : /* All the data chunk initializaion. */
3241 : 0 : Elf_Data *symdata = NULL;
3242 : 0 : Elf_Data *symstrdata = NULL;
3243 : 0 : Elf_Data *versym_data = NULL;
3244 : 0 : Elf_Data *verdef_data = NULL;
3245 : 0 : Elf_Data *verneed_data = NULL;
3246 : :
3247 : 0 : if (offs[i_symtab] != 0)
3248 : 0 : symdata = elf_getdata_rawchunk (
3249 : : ebl->elf, offs[i_symtab],
3250 : : gelf_fsize (ebl->elf, ELF_T_SYM, syments, EV_CURRENT), ELF_T_SYM);
3251 : :
3252 [ # # ]: 0 : if (offs[i_strtab] != 0 && addrs[i_strsz] != 0)
3253 : 0 : symstrdata = elf_getdata_rawchunk (ebl->elf, offs[i_strtab], addrs[i_strsz],
3254 : : ELF_T_BYTE);
3255 : :
3256 [ # # ]: 0 : if (offs[i_versym] != 0)
3257 : 0 : versym_data = elf_getdata_rawchunk (
3258 : : ebl->elf, offs[i_versym], syments * sizeof (Elf64_Half), ELF_T_HALF);
3259 : :
3260 : : /* Get the verneed_data without vernaux. */
3261 [ # # # # ]: 0 : if (offs[i_verneed] != 0 && addrs[i_verneednum] != 0)
3262 : : {
3263 : 0 : verneed_data = elf_getdata_rawchunk (
3264 : : ebl->elf, offs[i_verneed], addrs[i_verneednum] * sizeof (Elf64_Verneed),
3265 : : ELF_T_VNEED);
3266 : :
3267 [ # # ]: 0 : if (verneed_data->d_size < sizeof (GElf_Verneed))
3268 : 0 : error_exit (0, _("malformed SHT_GNU_verneed data"));
3269 : : }
3270 : :
3271 : 0 : size_t vernauxnum = 0;
3272 : 0 : size_t vn_next_offset = 0;
3273 : :
3274 [ # # ]: 0 : if (verneed_data != NULL && verneed_data->d_buf != NULL)
3275 [ # # ]: 0 : for (size_t i = 0; i < addrs[i_verneednum]; i++)
3276 : : {
3277 [ # # ]: 0 : if (vn_next_offset > (verneed_data->d_size - sizeof (GElf_Verneed)))
3278 : 0 : error_exit (0, _("invalid SHT_GNU_verneed data"));
3279 : :
3280 : 0 : GElf_Verneed *verneed
3281 : : = (GElf_Verneed *)(verneed_data->d_buf + vn_next_offset);
3282 : 0 : vernauxnum += verneed->vn_cnt;
3283 : 0 : vn_next_offset += verneed->vn_next;
3284 : : }
3285 : :
3286 : : /* Update the verneed_data to include the vernaux. */
3287 [ # # # # ]: 0 : if (offs[i_verneed] != 0 && addrs[i_verneednum] != 0)
3288 : 0 : verneed_data = elf_getdata_rawchunk (
3289 : : ebl->elf, offs[i_verneed],
3290 : 0 : (addrs[i_verneednum] + vernauxnum) * sizeof (GElf_Verneed),
3291 : : ELF_T_VNEED);
3292 : :
3293 : : /* Get the verdef_data without verdaux. */
3294 [ # # # # ]: 0 : if (offs[i_verdef] != 0 && addrs[i_verdefnum] != 0)
3295 : : {
3296 : 0 : verdef_data = elf_getdata_rawchunk (
3297 : : ebl->elf, offs[i_verdef], addrs[i_verdefnum] * sizeof (Elf64_Verdef),
3298 : : ELF_T_VDEF);
3299 : :
3300 [ # # ]: 0 : if (verdef_data->d_size < sizeof (GElf_Verdef))
3301 : 0 : error_exit (0, _("malformed SHT_GNU_verdef data"));
3302 : : }
3303 : :
3304 : 0 : size_t verdauxnum = 0;
3305 : 0 : size_t vd_next_offset = 0;
3306 : :
3307 [ # # ]: 0 : if (verdef_data != NULL && verdef_data->d_buf != NULL)
3308 [ # # ]: 0 : for (size_t i = 0; i < addrs[i_verdefnum]; i++)
3309 : : {
3310 [ # # ]: 0 : if (vd_next_offset > (verdef_data->d_size - sizeof (GElf_Verdef)))
3311 : 0 : error_exit (0, _("invalid SHT_GNU_verdef data"));
3312 : :
3313 : 0 : GElf_Verdef *verdef
3314 : : = (GElf_Verdef *)(verdef_data->d_buf + vd_next_offset);
3315 : 0 : verdauxnum += verdef->vd_cnt;
3316 : 0 : vd_next_offset += verdef->vd_next;
3317 : : }
3318 : :
3319 : : /* Update the verdef_data to include the verdaux. */
3320 [ # # # # ]: 0 : if (offs[i_verdef] != 0 && addrs[i_verdefnum] != 0)
3321 : 0 : verdef_data = elf_getdata_rawchunk (
3322 : : ebl->elf, offs[i_verdef],
3323 : 0 : (addrs[i_verdefnum] + verdauxnum) * sizeof (GElf_Verdef), ELF_T_VDEF);
3324 : :
3325 : 0 : unsigned int nsyms = (unsigned int)syments;
3326 : 0 : process_symtab (ebl, nsyms, 0, 0, 0, symdata, versym_data, symstrdata,
3327 : : verneed_data, verdef_data, NULL);
3328 : 0 : return true;
3329 : : }
3330 : :
3331 : :
3332 : : /* Print version information. */
3333 : : static void
3334 : 184 : print_verinfo (Ebl *ebl)
3335 : : {
3336 : : /* Find the version information sections. For this we have to
3337 : : search through the section table. */
3338 : 184 : Elf_Scn *scn = NULL;
3339 : :
3340 [ + + ]: 6004 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
3341 : : {
3342 : : /* Handle the section if it is part of the versioning handling. */
3343 : 5820 : GElf_Shdr shdr_mem;
3344 : 5820 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
3345 : :
3346 [ + - ]: 5820 : if (likely (shdr != NULL))
3347 : : {
3348 [ + + ]: 5820 : if (shdr->sh_type == SHT_GNU_verneed)
3349 : 130 : handle_verneed (ebl, scn, shdr);
3350 [ + + ]: 5690 : else if (shdr->sh_type == SHT_GNU_verdef)
3351 : 4 : handle_verdef (ebl, scn, shdr);
3352 [ + + ]: 5686 : else if (shdr->sh_type == SHT_GNU_versym)
3353 : 130 : handle_versym (ebl, scn, shdr);
3354 : : }
3355 : : }
3356 : 184 : }
3357 : :
3358 : :
3359 : : static const char *
3360 : 380 : get_ver_flags (unsigned int flags)
3361 : : {
3362 : 380 : static char buf[32];
3363 : 380 : char *endp;
3364 : :
3365 [ + + ]: 380 : if (flags == 0)
3366 : 376 : return _("none");
3367 : :
3368 [ + - ]: 4 : if (flags & VER_FLG_BASE)
3369 : 4 : endp = stpcpy (buf, "BASE ");
3370 : : else
3371 : : endp = buf;
3372 : :
3373 [ - + ]: 4 : if (flags & VER_FLG_WEAK)
3374 : : {
3375 [ # # ]: 0 : if (endp != buf)
3376 : 0 : endp = stpcpy (endp, "| ");
3377 : :
3378 : 0 : endp = stpcpy (endp, "WEAK ");
3379 : : }
3380 : :
3381 [ - + ]: 4 : if (unlikely (flags & ~(VER_FLG_BASE | VER_FLG_WEAK)))
3382 : : {
3383 : 0 : strncpy (endp, _("| <unknown>"), buf + sizeof (buf) - endp);
3384 : 0 : buf[sizeof (buf) - 1] = '\0';
3385 : : }
3386 : :
3387 : : return buf;
3388 : : }
3389 : :
3390 : :
3391 : : static void
3392 : 130 : handle_verneed (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr)
3393 : : {
3394 : 130 : int class = gelf_getclass (ebl->elf);
3395 : :
3396 : : /* Get the data of the section. */
3397 : 130 : Elf_Data *data = elf_getdata (scn, NULL);
3398 [ - + ]: 130 : if (data == NULL)
3399 : 0 : return;
3400 : :
3401 : : /* Get the section header string table index. */
3402 : 130 : size_t shstrndx;
3403 [ - + ]: 130 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
3404 : 0 : error_exit (0, _("cannot get section header string table index"));
3405 : :
3406 : 130 : GElf_Shdr glink_mem;
3407 : 130 : GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
3408 : : &glink_mem);
3409 [ - + ]: 130 : if (glink == NULL)
3410 : 0 : error_exit (0, _("invalid sh_link value in section %zu"),
3411 : : elf_ndxscn (scn));
3412 : :
3413 : 130 : printf (ngettext ("\
3414 : : \nVersion needs section [%2u] '%s' contains %d entry:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'\n",
3415 : : "\
3416 : : \nVersion needs section [%2u] '%s' contains %d entries:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'\n",
3417 : : shdr->sh_info),
3418 : 130 : (unsigned int) elf_ndxscn (scn),
3419 : 130 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name), shdr->sh_info,
3420 : : class == ELFCLASS32 ? 10 : 18, shdr->sh_addr,
3421 : : shdr->sh_offset,
3422 [ + + ]: 130 : (unsigned int) shdr->sh_link,
3423 : 130 : elf_strptr (ebl->elf, shstrndx, glink->sh_name));
3424 : :
3425 : 130 : unsigned int offset = 0;
3426 [ + - ]: 184 : for (unsigned int cnt = shdr->sh_info; cnt > 0; cnt--)
3427 : : {
3428 : : /* Get the data at the next offset. */
3429 : 184 : GElf_Verneed needmem;
3430 : 184 : GElf_Verneed *need = gelf_getverneed (data, offset, &needmem);
3431 [ + - ]: 184 : if (unlikely (need == NULL))
3432 : : break;
3433 : :
3434 : 552 : printf (_(" %#06x: Version: %hu File: %s Cnt: %hu\n"),
3435 : 184 : offset, (unsigned short int) need->vn_version,
3436 : 184 : elf_strptr (ebl->elf, shdr->sh_link, need->vn_file),
3437 : 184 : (unsigned short int) need->vn_cnt);
3438 : :
3439 : 184 : unsigned int auxoffset = offset + need->vn_aux;
3440 [ + - ]: 356 : for (unsigned int cnt2 = need->vn_cnt; cnt2 > 0; cnt2--)
3441 : : {
3442 : 356 : GElf_Vernaux auxmem;
3443 : 356 : GElf_Vernaux *aux = gelf_getvernaux (data, auxoffset, &auxmem);
3444 [ + - ]: 356 : if (unlikely (aux == NULL))
3445 : : break;
3446 : :
3447 : 356 : printf (_(" %#06x: Name: %s Flags: %s Version: %hu\n"),
3448 : : auxoffset,
3449 : 356 : elf_strptr (ebl->elf, shdr->sh_link, aux->vna_name),
3450 : 356 : get_ver_flags (aux->vna_flags),
3451 : 356 : (unsigned short int) aux->vna_other);
3452 : :
3453 [ + + ]: 356 : if (aux->vna_next == 0)
3454 : : break;
3455 : :
3456 : 172 : auxoffset += aux->vna_next;
3457 : : }
3458 : :
3459 : : /* Find the next offset. */
3460 [ + + ]: 184 : if (need->vn_next == 0)
3461 : : break;
3462 : :
3463 : 54 : offset += need->vn_next;
3464 : : }
3465 : : }
3466 : :
3467 : :
3468 : : static void
3469 : 4 : handle_verdef (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr)
3470 : : {
3471 : : /* Get the data of the section. */
3472 : 4 : Elf_Data *data = elf_getdata (scn, NULL);
3473 [ - + ]: 4 : if (data == NULL)
3474 : 0 : return;
3475 : :
3476 : : /* Get the section header string table index. */
3477 : 4 : size_t shstrndx;
3478 [ - + ]: 4 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
3479 : 0 : error_exit (0, _("cannot get section header string table index"));
3480 : :
3481 : 4 : GElf_Shdr glink_mem;
3482 : 4 : GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
3483 : : &glink_mem);
3484 [ - + ]: 4 : if (glink == NULL)
3485 : 0 : error_exit (0, _("invalid sh_link value in section %zu"),
3486 : : elf_ndxscn (scn));
3487 : :
3488 : 4 : int class = gelf_getclass (ebl->elf);
3489 : 4 : printf (ngettext ("\
3490 : : \nVersion definition section [%2u] '%s' contains %d entry:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'\n",
3491 : : "\
3492 : : \nVersion definition section [%2u] '%s' contains %d entries:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'\n",
3493 : : shdr->sh_info),
3494 : 4 : (unsigned int) elf_ndxscn (scn),
3495 : 4 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
3496 : : shdr->sh_info,
3497 : : class == ELFCLASS32 ? 10 : 18, shdr->sh_addr,
3498 : : shdr->sh_offset,
3499 [ + - ]: 4 : (unsigned int) shdr->sh_link,
3500 : 4 : elf_strptr (ebl->elf, shstrndx, glink->sh_name));
3501 : :
3502 : 4 : unsigned int offset = 0;
3503 [ + - ]: 24 : for (unsigned int cnt = shdr->sh_info; cnt > 0; cnt--)
3504 : : {
3505 : : /* Get the data at the next offset. */
3506 : 24 : GElf_Verdef defmem;
3507 : 24 : GElf_Verdef *def = gelf_getverdef (data, offset, &defmem);
3508 [ + - ]: 24 : if (unlikely (def == NULL))
3509 : : break;
3510 : :
3511 : 24 : unsigned int auxoffset = offset + def->vd_aux;
3512 : 24 : GElf_Verdaux auxmem;
3513 : 24 : GElf_Verdaux *aux = gelf_getverdaux (data, auxoffset, &auxmem);
3514 [ + - ]: 24 : if (unlikely (aux == NULL))
3515 : : break;
3516 : :
3517 : 96 : printf (_("\
3518 : : %#06x: Version: %hd Flags: %s Index: %hd Cnt: %hd Name: %s\n"),
3519 : 24 : offset, def->vd_version,
3520 : 24 : get_ver_flags (def->vd_flags),
3521 : 24 : def->vd_ndx,
3522 : 24 : def->vd_cnt,
3523 : 24 : elf_strptr (ebl->elf, shdr->sh_link, aux->vda_name));
3524 : :
3525 : 24 : auxoffset += aux->vda_next;
3526 [ + + ]: 24 : for (unsigned int cnt2 = 1; cnt2 < def->vd_cnt; ++cnt2)
3527 : : {
3528 : 16 : aux = gelf_getverdaux (data, auxoffset, &auxmem);
3529 [ + - ]: 16 : if (unlikely (aux == NULL))
3530 : : break;
3531 : :
3532 : 16 : printf (_(" %#06x: Parent %d: %s\n"),
3533 : : auxoffset, cnt2,
3534 : 16 : elf_strptr (ebl->elf, shdr->sh_link, aux->vda_name));
3535 : :
3536 [ - + ]: 16 : if (aux->vda_next == 0)
3537 : : break;
3538 : :
3539 : 0 : auxoffset += aux->vda_next;
3540 : : }
3541 : :
3542 : : /* Find the next offset. */
3543 [ + + ]: 24 : if (def->vd_next == 0)
3544 : : break;
3545 : 20 : offset += def->vd_next;
3546 : : }
3547 : : }
3548 : :
3549 : :
3550 : : static void
3551 : 130 : handle_versym (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr)
3552 : : {
3553 : 130 : int class = gelf_getclass (ebl->elf);
3554 : 130 : const char **vername;
3555 : 130 : const char **filename;
3556 : :
3557 : : /* Get the data of the section. */
3558 : 130 : Elf_Data *data = elf_getdata (scn, NULL);
3559 [ + - ]: 130 : if (data == NULL)
3560 : 0 : return;
3561 : :
3562 : : /* Get the section header string table index. */
3563 : 130 : size_t shstrndx;
3564 [ - + ]: 130 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
3565 : 0 : error_exit (0, _("cannot get section header string table index"));
3566 : :
3567 : : /* We have to find the version definition section and extract the
3568 : : version names. */
3569 : : Elf_Scn *defscn = NULL;
3570 : : Elf_Scn *needscn = NULL;
3571 : :
3572 : : Elf_Scn *verscn = NULL;
3573 [ + + ]: 4046 : while ((verscn = elf_nextscn (ebl->elf, verscn)) != NULL)
3574 : : {
3575 : 3916 : GElf_Shdr vershdr_mem;
3576 : 3916 : GElf_Shdr *vershdr = gelf_getshdr (verscn, &vershdr_mem);
3577 : :
3578 [ - + ]: 3916 : if (likely (vershdr != NULL))
3579 : : {
3580 [ + + ]: 3916 : if (vershdr->sh_type == SHT_GNU_verdef)
3581 : : defscn = verscn;
3582 [ + + ]: 3912 : else if (vershdr->sh_type == SHT_GNU_verneed)
3583 : 3916 : needscn = verscn;
3584 : : }
3585 : : }
3586 : :
3587 : 130 : size_t nvername;
3588 [ + - ]: 130 : if (defscn != NULL || needscn != NULL)
3589 : : {
3590 : : /* We have a version information (better should have). Now get
3591 : : the version names. First find the maximum version number. */
3592 : 130 : nvername = 0;
3593 [ + + ]: 130 : if (defscn != NULL)
3594 : : {
3595 : : /* Run through the version definitions and find the highest
3596 : : index. */
3597 : 4 : unsigned int offset = 0;
3598 : 4 : Elf_Data *defdata;
3599 : 4 : GElf_Shdr defshdrmem;
3600 : 4 : GElf_Shdr *defshdr;
3601 : :
3602 : 4 : defdata = elf_getdata (defscn, NULL);
3603 [ + - ]: 4 : if (unlikely (defdata == NULL))
3604 : 0 : return;
3605 : :
3606 : 4 : defshdr = gelf_getshdr (defscn, &defshdrmem);
3607 [ + - ]: 4 : if (unlikely (defshdr == NULL))
3608 : : return;
3609 : :
3610 [ + - ]: 24 : for (unsigned int cnt = 0; cnt < defshdr->sh_info; ++cnt)
3611 : : {
3612 : 24 : GElf_Verdef defmem;
3613 : 24 : GElf_Verdef *def;
3614 : :
3615 : : /* Get the data at the next offset. */
3616 : 24 : def = gelf_getverdef (defdata, offset, &defmem);
3617 [ + - ]: 24 : if (unlikely (def == NULL))
3618 : : break;
3619 : :
3620 : 24 : nvername = MAX (nvername, (size_t) (def->vd_ndx & 0x7fff));
3621 : :
3622 [ + + ]: 24 : if (def->vd_next == 0)
3623 : : break;
3624 : 20 : offset += def->vd_next;
3625 : : }
3626 : : }
3627 [ + - ]: 130 : if (needscn != NULL)
3628 : : {
3629 : 130 : unsigned int offset = 0;
3630 : 130 : Elf_Data *needdata;
3631 : 130 : GElf_Shdr needshdrmem;
3632 : 130 : GElf_Shdr *needshdr;
3633 : :
3634 : 130 : needdata = elf_getdata (needscn, NULL);
3635 [ + - ]: 130 : if (unlikely (needdata == NULL))
3636 : 0 : return;
3637 : :
3638 : 130 : needshdr = gelf_getshdr (needscn, &needshdrmem);
3639 [ + - ]: 130 : if (unlikely (needshdr == NULL))
3640 : : return;
3641 : :
3642 [ + - ]: 184 : for (unsigned int cnt = 0; cnt < needshdr->sh_info; ++cnt)
3643 : : {
3644 : 184 : GElf_Verneed needmem;
3645 : 184 : GElf_Verneed *need;
3646 : 184 : unsigned int auxoffset;
3647 : 184 : int cnt2;
3648 : :
3649 : : /* Get the data at the next offset. */
3650 : 184 : need = gelf_getverneed (needdata, offset, &needmem);
3651 [ + - ]: 184 : if (unlikely (need == NULL))
3652 : : break;
3653 : :
3654 : : /* Run through the auxiliary entries. */
3655 : 184 : auxoffset = offset + need->vn_aux;
3656 [ + - ]: 356 : for (cnt2 = need->vn_cnt; --cnt2 >= 0; )
3657 : : {
3658 : 356 : GElf_Vernaux auxmem;
3659 : 356 : GElf_Vernaux *aux;
3660 : :
3661 : 356 : aux = gelf_getvernaux (needdata, auxoffset, &auxmem);
3662 [ + - ]: 356 : if (unlikely (aux == NULL))
3663 : : break;
3664 : :
3665 : 356 : nvername = MAX (nvername,
3666 : : (size_t) (aux->vna_other & 0x7fff));
3667 : :
3668 [ + + ]: 356 : if (aux->vna_next == 0)
3669 : : break;
3670 : 172 : auxoffset += aux->vna_next;
3671 : : }
3672 : :
3673 [ + + ]: 184 : if (need->vn_next == 0)
3674 : : break;
3675 : 54 : offset += need->vn_next;
3676 : : }
3677 : : }
3678 : :
3679 : : /* This is the number of versions we know about. */
3680 : 130 : ++nvername;
3681 : :
3682 : : /* Allocate the array. */
3683 : 130 : vername = (const char **) alloca (nvername * sizeof (const char *));
3684 [ + + ]: 130 : memset(vername, 0, nvername * sizeof (const char *));
3685 : 130 : filename = (const char **) alloca (nvername * sizeof (const char *));
3686 : 130 : memset(filename, 0, nvername * sizeof (const char *));
3687 : :
3688 : : /* Run through the data structures again and collect the strings. */
3689 [ + + ]: 130 : if (defscn != NULL)
3690 : : {
3691 : : /* Run through the version definitions and find the highest
3692 : : index. */
3693 : 4 : unsigned int offset = 0;
3694 : 4 : Elf_Data *defdata;
3695 : 4 : GElf_Shdr defshdrmem;
3696 : 4 : GElf_Shdr *defshdr;
3697 : :
3698 : 4 : defdata = elf_getdata (defscn, NULL);
3699 [ + - ]: 4 : if (unlikely (defdata == NULL))
3700 : 0 : return;
3701 : :
3702 : 4 : defshdr = gelf_getshdr (defscn, &defshdrmem);
3703 [ + - ]: 4 : if (unlikely (defshdr == NULL))
3704 : : return;
3705 : :
3706 [ + - ]: 24 : for (unsigned int cnt = 0; cnt < defshdr->sh_info; ++cnt)
3707 : : {
3708 : :
3709 : : /* Get the data at the next offset. */
3710 : 24 : GElf_Verdef defmem;
3711 : 24 : GElf_Verdef *def = gelf_getverdef (defdata, offset, &defmem);
3712 [ + - ]: 24 : if (unlikely (def == NULL))
3713 : : break;
3714 : :
3715 : 24 : GElf_Verdaux auxmem;
3716 : 48 : GElf_Verdaux *aux = gelf_getverdaux (defdata,
3717 : 24 : offset + def->vd_aux,
3718 : : &auxmem);
3719 [ + - ]: 24 : if (unlikely (aux == NULL))
3720 : : break;
3721 : :
3722 : 24 : vername[def->vd_ndx & 0x7fff]
3723 : 24 : = elf_strptr (ebl->elf, defshdr->sh_link, aux->vda_name);
3724 : 24 : filename[def->vd_ndx & 0x7fff] = NULL;
3725 : :
3726 [ + + ]: 24 : if (def->vd_next == 0)
3727 : : break;
3728 : 20 : offset += def->vd_next;
3729 : : }
3730 : : }
3731 [ + - ]: 130 : if (needscn != NULL)
3732 : : {
3733 : 130 : unsigned int offset = 0;
3734 : :
3735 : 130 : Elf_Data *needdata = elf_getdata (needscn, NULL);
3736 : 130 : GElf_Shdr needshdrmem;
3737 : 130 : GElf_Shdr *needshdr = gelf_getshdr (needscn, &needshdrmem);
3738 [ - + ]: 130 : if (unlikely (needdata == NULL || needshdr == NULL))
3739 : 0 : return;
3740 : :
3741 [ + - ]: 184 : for (unsigned int cnt = 0; cnt < needshdr->sh_info; ++cnt)
3742 : : {
3743 : : /* Get the data at the next offset. */
3744 : 184 : GElf_Verneed needmem;
3745 : 184 : GElf_Verneed *need = gelf_getverneed (needdata, offset,
3746 : : &needmem);
3747 [ + - ]: 184 : if (unlikely (need == NULL))
3748 : : break;
3749 : :
3750 : : /* Run through the auxiliary entries. */
3751 : 184 : unsigned int auxoffset = offset + need->vn_aux;
3752 [ + - ]: 356 : for (int cnt2 = need->vn_cnt; --cnt2 >= 0; )
3753 : : {
3754 : 356 : GElf_Vernaux auxmem;
3755 : 356 : GElf_Vernaux *aux = gelf_getvernaux (needdata, auxoffset,
3756 : : &auxmem);
3757 [ + - ]: 356 : if (unlikely (aux == NULL))
3758 : : break;
3759 : :
3760 : 356 : vername[aux->vna_other & 0x7fff]
3761 : 356 : = elf_strptr (ebl->elf, needshdr->sh_link, aux->vna_name);
3762 : 356 : filename[aux->vna_other & 0x7fff]
3763 : 356 : = elf_strptr (ebl->elf, needshdr->sh_link, need->vn_file);
3764 : :
3765 [ + + ]: 356 : if (aux->vna_next == 0)
3766 : : break;
3767 : 172 : auxoffset += aux->vna_next;
3768 : : }
3769 : :
3770 [ + + ]: 184 : if (need->vn_next == 0)
3771 : : break;
3772 : 54 : offset += need->vn_next;
3773 : : }
3774 : : }
3775 : : }
3776 : : else
3777 : : {
3778 : : vername = NULL;
3779 : : nvername = 1;
3780 : : filename = NULL;
3781 : : }
3782 : :
3783 : 130 : GElf_Shdr glink_mem;
3784 : 130 : GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
3785 : : &glink_mem);
3786 : 130 : size_t sh_entsize = gelf_fsize (ebl->elf, ELF_T_HALF, 1, EV_CURRENT);
3787 [ - + ]: 130 : if (glink == NULL)
3788 : 0 : error_exit (0, _("invalid sh_link value in section %zu"),
3789 : : elf_ndxscn (scn));
3790 : :
3791 : : /* Print the header. */
3792 : 130 : printf (ngettext ("\
3793 : : \nVersion symbols section [%2u] '%s' contains %d entry:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'",
3794 : : "\
3795 : : \nVersion symbols section [%2u] '%s' contains %d entries:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'",
3796 : : shdr->sh_size / sh_entsize),
3797 : 130 : (unsigned int) elf_ndxscn (scn),
3798 : 130 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
3799 : 130 : (int) (shdr->sh_size / sh_entsize),
3800 : : class == ELFCLASS32 ? 10 : 18, shdr->sh_addr,
3801 : : shdr->sh_offset,
3802 [ + + ]: 130 : (unsigned int) shdr->sh_link,
3803 : 130 : elf_strptr (ebl->elf, shstrndx, glink->sh_name));
3804 : :
3805 : : /* Now we can finally look at the actual contents of this section. */
3806 [ + + ]: 3308 : for (unsigned int cnt = 0; cnt < shdr->sh_size / sh_entsize; ++cnt)
3807 : : {
3808 [ + + ]: 3178 : if (cnt % 2 == 0)
3809 : 1608 : printf ("\n %4d:", cnt);
3810 : :
3811 : 3178 : GElf_Versym symmem;
3812 : 3178 : GElf_Versym *sym = gelf_getversym (data, cnt, &symmem);
3813 [ + - ]: 3178 : if (sym == NULL)
3814 : : break;
3815 : :
3816 [ + + + ]: 3178 : switch (*sym)
3817 : : {
3818 : 238 : ssize_t n;
3819 : 238 : case 0:
3820 : 238 : fputs (_(" 0 *local* "),
3821 : : stdout);
3822 : 238 : break;
3823 : :
3824 : 502 : case 1:
3825 : 502 : fputs (_(" 1 *global* "),
3826 : : stdout);
3827 : 502 : break;
3828 : :
3829 : 2438 : default:
3830 [ + - ]: 4876 : n = printf ("%4d%c%s",
3831 [ + - ]: 2438 : *sym & 0x7fff, *sym & 0x8000 ? 'h' : ' ',
3832 : : (vername != NULL
3833 [ + - ]: 2438 : && (unsigned int) (*sym & 0x7fff) < nvername)
3834 : 2438 : ? vername[*sym & 0x7fff] : "???");
3835 [ + - ]: 2438 : if ((unsigned int) (*sym & 0x7fff) < nvername
3836 [ + - + + ]: 2438 : && filename != NULL && filename[*sym & 0x7fff] != NULL)
3837 : 2134 : n += printf ("(%s)", filename[*sym & 0x7fff]);
3838 : 2438 : printf ("%*s", MAX (0, 33 - (int) n), " ");
3839 : 2438 : break;
3840 : : }
3841 : : }
3842 : 130 : putchar ('\n');
3843 : : }
3844 : :
3845 : :
3846 : : static void
3847 : 130 : print_hash_info (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr, size_t shstrndx,
3848 : : uint_fast32_t maxlength, Elf32_Word nbucket,
3849 : : uint_fast32_t nsyms, uint32_t *lengths, const char *extrastr)
3850 : : {
3851 : 130 : uint32_t *counts = xcalloc (maxlength + 1, sizeof (uint32_t));
3852 : :
3853 [ + + ]: 550 : for (Elf32_Word cnt = 0; cnt < nbucket; ++cnt)
3854 : 420 : ++counts[lengths[cnt]];
3855 : :
3856 : 130 : GElf_Shdr glink_mem;
3857 : 130 : GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf,
3858 : 130 : shdr->sh_link),
3859 : : &glink_mem);
3860 [ - + ]: 130 : if (glink == NULL)
3861 : : {
3862 : 0 : error (0, 0, _("invalid sh_link value in section %zu"),
3863 : : elf_ndxscn (scn));
3864 : 0 : free (counts);
3865 : 0 : return;
3866 : : }
3867 : :
3868 [ + + ]: 260 : printf (ngettext ("\
3869 : : \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",
3870 : : "\
3871 : : \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",
3872 : : nbucket),
3873 : 130 : (unsigned int) elf_ndxscn (scn),
3874 : 130 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
3875 : : (int) nbucket,
3876 : 130 : gelf_getclass (ebl->elf) == ELFCLASS32 ? 10 : 18,
3877 : : shdr->sh_addr,
3878 : : shdr->sh_offset,
3879 : 130 : (unsigned int) shdr->sh_link,
3880 : 130 : elf_strptr (ebl->elf, shstrndx, glink->sh_name));
3881 : :
3882 [ + - ]: 130 : if (extrastr != NULL)
3883 : 130 : fputs (extrastr, stdout);
3884 : :
3885 [ + - ]: 130 : if (likely (nbucket > 0))
3886 : : {
3887 : 130 : uint64_t success = 0;
3888 : :
3889 : : /* xgettext:no-c-format */
3890 : 130 : fputs (_("\
3891 : : Length Number % of total Coverage\n"), stdout);
3892 : 130 : printf (_(" 0 %6" PRIu32 " %5.1f%%\n"),
3893 : 130 : counts[0], (counts[0] * 100.0) / nbucket);
3894 : :
3895 : 130 : uint64_t nzero_counts = 0;
3896 [ + + ]: 228 : for (Elf32_Word cnt = 1; cnt <= maxlength; ++cnt)
3897 : : {
3898 : 98 : nzero_counts += counts[cnt] * cnt;
3899 : 98 : printf (_("\
3900 : : %7d %6" PRIu32 " %5.1f%% %5.1f%%\n"),
3901 : 98 : (int) cnt, counts[cnt], (counts[cnt] * 100.0) / nbucket,
3902 : 98 : (nzero_counts * 100.0) / nsyms);
3903 : : }
3904 : :
3905 : : Elf32_Word acc = 0;
3906 [ + + ]: 228 : for (Elf32_Word cnt = 1; cnt <= maxlength; ++cnt)
3907 : : {
3908 : 98 : acc += cnt;
3909 : 98 : success += counts[cnt] * acc;
3910 : : }
3911 : :
3912 [ + + ]: 130 : if (nzero_counts > 0)
3913 : 62 : printf (_("\
3914 : : Average number of tests: successful lookup: %f\n\
3915 : : unsuccessful lookup: %f\n"),
3916 : 62 : (double) success / (double) nzero_counts,
3917 : 62 : (double) nzero_counts / (double) nbucket);
3918 : : }
3919 : :
3920 : 130 : free (counts);
3921 : : }
3922 : :
3923 : :
3924 : : /* This function handles the traditional System V-style hash table format. */
3925 : : static void
3926 : 0 : handle_sysv_hash (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr, size_t shstrndx)
3927 : : {
3928 : 0 : Elf_Data *data = elf_getdata (scn, NULL);
3929 [ # # ]: 0 : if (unlikely (data == NULL))
3930 : : {
3931 : 0 : error (0, 0, _("cannot get data for section %d: %s"),
3932 : 0 : (int) elf_ndxscn (scn), elf_errmsg (-1));
3933 : 0 : return;
3934 : : }
3935 : :
3936 [ # # ]: 0 : if (unlikely (data->d_size < 2 * sizeof (Elf32_Word)))
3937 : : {
3938 : 0 : invalid_data:
3939 : 0 : error (0, 0, _("invalid data in sysv.hash section %d"),
3940 : 0 : (int) elf_ndxscn (scn));
3941 : 0 : return;
3942 : : }
3943 : :
3944 : 0 : Elf32_Word nbucket = ((Elf32_Word *) data->d_buf)[0];
3945 : 0 : Elf32_Word nchain = ((Elf32_Word *) data->d_buf)[1];
3946 : :
3947 : 0 : uint64_t used_buf = (2ULL + nchain + nbucket) * sizeof (Elf32_Word);
3948 [ # # ]: 0 : if (used_buf > data->d_size)
3949 : 0 : goto invalid_data;
3950 : :
3951 : 0 : Elf32_Word *bucket = &((Elf32_Word *) data->d_buf)[2];
3952 : 0 : Elf32_Word *chain = &((Elf32_Word *) data->d_buf)[2 + nbucket];
3953 : :
3954 : 0 : uint32_t *lengths = xcalloc (nbucket, sizeof (uint32_t));
3955 : :
3956 : 0 : uint_fast32_t maxlength = 0;
3957 : 0 : uint_fast32_t nsyms = 0;
3958 [ # # ]: 0 : for (Elf32_Word cnt = 0; cnt < nbucket; ++cnt)
3959 : : {
3960 : 0 : Elf32_Word inner = bucket[cnt];
3961 : 0 : Elf32_Word chain_len = 0;
3962 [ # # ]: 0 : while (inner > 0 && inner < nchain)
3963 : : {
3964 : 0 : ++nsyms;
3965 : 0 : ++chain_len;
3966 [ # # ]: 0 : if (chain_len > nchain)
3967 : : {
3968 : 0 : error (0, 0, _("invalid chain in sysv.hash section %d"),
3969 : 0 : (int) elf_ndxscn (scn));
3970 : 0 : free (lengths);
3971 : 0 : return;
3972 : : }
3973 [ # # ]: 0 : if (maxlength < ++lengths[cnt])
3974 : 0 : ++maxlength;
3975 : :
3976 : 0 : inner = chain[inner];
3977 : : }
3978 : : }
3979 : :
3980 : 0 : print_hash_info (ebl, scn, shdr, shstrndx, maxlength, nbucket, nsyms,
3981 : : lengths, NULL);
3982 : :
3983 : 0 : free (lengths);
3984 : : }
3985 : :
3986 : :
3987 : : /* This function handles the incorrect, System V-style hash table
3988 : : format some 64-bit architectures use. */
3989 : : static void
3990 : 0 : handle_sysv_hash64 (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr, size_t shstrndx)
3991 : : {
3992 : 0 : Elf_Data *data = elf_getdata (scn, NULL);
3993 [ # # ]: 0 : if (unlikely (data == NULL))
3994 : : {
3995 : 0 : error (0, 0, _("cannot get data for section %d: %s"),
3996 : 0 : (int) elf_ndxscn (scn), elf_errmsg (-1));
3997 : 0 : return;
3998 : : }
3999 : :
4000 [ # # ]: 0 : if (unlikely (data->d_size < 2 * sizeof (Elf64_Xword)))
4001 : : {
4002 : 0 : invalid_data:
4003 : 0 : error (0, 0, _("invalid data in sysv.hash64 section %d"),
4004 : 0 : (int) elf_ndxscn (scn));
4005 : 0 : return;
4006 : : }
4007 : :
4008 : 0 : Elf64_Xword nbucket = ((Elf64_Xword *) data->d_buf)[0];
4009 : 0 : Elf64_Xword nchain = ((Elf64_Xword *) data->d_buf)[1];
4010 : :
4011 : 0 : uint64_t maxwords = data->d_size / sizeof (Elf64_Xword);
4012 : 0 : if (maxwords < 2
4013 [ # # ]: 0 : || maxwords - 2 < nbucket
4014 [ # # ]: 0 : || maxwords - 2 - nbucket < nchain)
4015 : 0 : goto invalid_data;
4016 : :
4017 : 0 : Elf64_Xword *bucket = &((Elf64_Xword *) data->d_buf)[2];
4018 : 0 : Elf64_Xword *chain = &((Elf64_Xword *) data->d_buf)[2 + nbucket];
4019 : :
4020 : 0 : uint32_t *lengths = xcalloc (nbucket, sizeof (uint32_t));
4021 : :
4022 : 0 : uint_fast32_t maxlength = 0;
4023 : 0 : uint_fast32_t nsyms = 0;
4024 [ # # ]: 0 : for (Elf64_Xword cnt = 0; cnt < nbucket; ++cnt)
4025 : : {
4026 : 0 : Elf64_Xword inner = bucket[cnt];
4027 : 0 : Elf64_Xword chain_len = 0;
4028 [ # # ]: 0 : while (inner > 0 && inner < nchain)
4029 : : {
4030 : 0 : ++nsyms;
4031 : 0 : ++chain_len;
4032 [ # # ]: 0 : if (chain_len > nchain)
4033 : : {
4034 : 0 : error (0, 0, _("invalid chain in sysv.hash64 section %d"),
4035 : 0 : (int) elf_ndxscn (scn));
4036 : 0 : free (lengths);
4037 : 0 : return;
4038 : : }
4039 [ # # ]: 0 : if (maxlength < ++lengths[cnt])
4040 : 0 : ++maxlength;
4041 : :
4042 : 0 : inner = chain[inner];
4043 : : }
4044 : : }
4045 : :
4046 : 0 : print_hash_info (ebl, scn, shdr, shstrndx, maxlength, nbucket, nsyms,
4047 : : lengths, NULL);
4048 : :
4049 : 0 : free (lengths);
4050 : : }
4051 : :
4052 : :
4053 : : /* This function handles the GNU-style hash table format. */
4054 : : static void
4055 : 130 : handle_gnu_hash (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr, size_t shstrndx)
4056 : : {
4057 : 130 : uint32_t *lengths = NULL;
4058 : 130 : Elf_Data *data = elf_getdata (scn, NULL);
4059 [ - + ]: 130 : if (unlikely (data == NULL))
4060 : : {
4061 : 0 : error (0, 0, _("cannot get data for section %d: %s"),
4062 : 0 : (int) elf_ndxscn (scn), elf_errmsg (-1));
4063 : 0 : return;
4064 : : }
4065 : :
4066 [ - + ]: 130 : if (unlikely (data->d_size < 4 * sizeof (Elf32_Word)))
4067 : : {
4068 : 0 : invalid_data:
4069 : 0 : free (lengths);
4070 : 0 : error (0, 0, _("invalid data in gnu.hash section %d"),
4071 : 0 : (int) elf_ndxscn (scn));
4072 : 0 : return;
4073 : : }
4074 : :
4075 : 130 : Elf32_Word nbucket = ((Elf32_Word *) data->d_buf)[0];
4076 : 130 : Elf32_Word symbias = ((Elf32_Word *) data->d_buf)[1];
4077 : :
4078 : : /* Next comes the size of the bitmap. It's measured in words for
4079 : : the architecture. It's 32 bits for 32 bit archs, and 64 bits for
4080 : : 64 bit archs. There is always a bloom filter present, so zero is
4081 : : an invalid value. */
4082 : 130 : Elf32_Word bitmask_words = ((Elf32_Word *) data->d_buf)[2];
4083 [ + + ]: 130 : if (gelf_getclass (ebl->elf) == ELFCLASS64)
4084 : 114 : bitmask_words *= 2;
4085 : :
4086 [ - + ]: 130 : if (bitmask_words == 0)
4087 : 0 : goto invalid_data;
4088 : :
4089 : 130 : Elf32_Word shift = ((Elf32_Word *) data->d_buf)[3];
4090 : :
4091 : : /* Is there still room for the sym chain?
4092 : : Use uint64_t calculation to prevent 32bit overflow. */
4093 : 130 : uint64_t used_buf = (4ULL + bitmask_words + nbucket) * sizeof (Elf32_Word);
4094 : 130 : uint32_t max_nsyms = (data->d_size - used_buf) / sizeof (Elf32_Word);
4095 [ - + ]: 130 : if (used_buf > data->d_size)
4096 : 0 : goto invalid_data;
4097 : :
4098 : 130 : lengths = xcalloc (nbucket, sizeof (uint32_t));
4099 : :
4100 : 130 : Elf32_Word *bitmask = &((Elf32_Word *) data->d_buf)[4];
4101 : 130 : Elf32_Word *bucket = &((Elf32_Word *) data->d_buf)[4 + bitmask_words];
4102 : 130 : Elf32_Word *chain = &((Elf32_Word *) data->d_buf)[4 + bitmask_words
4103 : 130 : + nbucket];
4104 : :
4105 : : /* Compute distribution of chain lengths. */
4106 : 130 : uint_fast32_t maxlength = 0;
4107 : 130 : uint_fast32_t nsyms = 0;
4108 [ + + ]: 550 : for (Elf32_Word cnt = 0; cnt < nbucket; ++cnt)
4109 [ + + ]: 420 : if (bucket[cnt] != 0)
4110 : : {
4111 : 232 : Elf32_Word inner = bucket[cnt] - symbias;
4112 : 414 : do
4113 : : {
4114 : 414 : ++nsyms;
4115 [ + + ]: 414 : if (maxlength < ++lengths[cnt])
4116 : 98 : ++maxlength;
4117 [ - + ]: 414 : if (inner >= max_nsyms)
4118 : 0 : goto invalid_data;
4119 : : }
4120 [ + + ]: 414 : while ((chain[inner++] & 1) == 0);
4121 : : }
4122 : :
4123 : : /* Count bits in bitmask. */
4124 : : uint_fast32_t nbits = 0;
4125 [ + + ]: 446 : for (Elf32_Word cnt = 0; cnt < bitmask_words; ++cnt)
4126 : : {
4127 : 316 : uint_fast32_t word = bitmask[cnt];
4128 : :
4129 : 316 : word = (word & 0x55555555) + ((word >> 1) & 0x55555555);
4130 : 316 : word = (word & 0x33333333) + ((word >> 2) & 0x33333333);
4131 : 316 : word = (word & 0x0f0f0f0f) + ((word >> 4) & 0x0f0f0f0f);
4132 : 316 : word = (word & 0x00ff00ff) + ((word >> 8) & 0x00ff00ff);
4133 : 316 : nbits += (word & 0x0000ffff) + ((word >> 16) & 0x0000ffff);
4134 : : }
4135 : :
4136 : 260 : char *str = xasprintf (_("\
4137 : : Symbol Bias: %u\n\
4138 : : Bitmask Size: %zu bytes %" PRIuFAST32 "%% bits set 2nd hash shift: %u\n"),
4139 : : (unsigned int) symbias,
4140 : : bitmask_words * sizeof (Elf32_Word),
4141 : 130 : ((nbits * 100 + 50)
4142 : 130 : / (uint_fast32_t) (bitmask_words
4143 : : * sizeof (Elf32_Word) * 8)),
4144 : : (unsigned int) shift);
4145 : :
4146 : 130 : print_hash_info (ebl, scn, shdr, shstrndx, maxlength, nbucket, nsyms,
4147 : : lengths, str);
4148 : :
4149 : 130 : free (str);
4150 : 130 : free (lengths);
4151 : : }
4152 : :
4153 : :
4154 : : /* Find the symbol table(s). For this we have to search through the
4155 : : section table. */
4156 : : static void
4157 : 184 : handle_hash (Ebl *ebl)
4158 : : {
4159 : : /* Get the section header string table index. */
4160 : 184 : size_t shstrndx;
4161 [ - + ]: 184 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
4162 : 0 : error_exit (0, _("cannot get section header string table index"));
4163 : :
4164 : : Elf_Scn *scn = NULL;
4165 [ + + ]: 6004 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
4166 : : {
4167 : : /* Handle the section if it is a symbol table. */
4168 : 5820 : GElf_Shdr shdr_mem;
4169 : 5820 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
4170 : :
4171 [ + - ]: 5820 : if (likely (shdr != NULL))
4172 : : {
4173 [ + + ]: 5820 : if ((shdr->sh_type == SHT_HASH || shdr->sh_type == SHT_GNU_HASH)
4174 [ - + ]: 130 : && (shdr->sh_flags & SHF_COMPRESSED) != 0)
4175 : : {
4176 [ # # ]: 0 : if (elf_compress (scn, 0, 0) < 0)
4177 : 0 : printf ("WARNING: %s [%zd]\n",
4178 : : _("Couldn't uncompress section"),
4179 : : elf_ndxscn (scn));
4180 : 0 : shdr = gelf_getshdr (scn, &shdr_mem);
4181 [ # # ]: 0 : if (unlikely (shdr == NULL))
4182 : 0 : error_exit (0, _("cannot get section [%zd] header: %s"),
4183 : : elf_ndxscn (scn), elf_errmsg (-1));
4184 : : }
4185 : :
4186 [ - + ]: 5820 : if (shdr->sh_type == SHT_HASH)
4187 : : {
4188 [ # # ]: 0 : if (ebl_sysvhash_entrysize (ebl) == sizeof (Elf64_Xword))
4189 : 0 : handle_sysv_hash64 (ebl, scn, shdr, shstrndx);
4190 : : else
4191 : 0 : handle_sysv_hash (ebl, scn, shdr, shstrndx);
4192 : : }
4193 [ + + ]: 5820 : else if (shdr->sh_type == SHT_GNU_HASH)
4194 : 130 : handle_gnu_hash (ebl, scn, shdr, shstrndx);
4195 : : }
4196 : : }
4197 : 184 : }
4198 : :
4199 : :
4200 : : static void
4201 : 192 : print_liblist (Ebl *ebl)
4202 : : {
4203 : : /* Find the library list sections. For this we have to search
4204 : : through the section table. */
4205 : 192 : Elf_Scn *scn = NULL;
4206 : :
4207 : : /* Get the section header string table index. */
4208 : 192 : size_t shstrndx;
4209 [ - + ]: 192 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
4210 : 0 : error_exit (0, _("cannot get section header string table index"));
4211 : :
4212 [ + + ]: 6130 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
4213 : : {
4214 : 5938 : GElf_Shdr shdr_mem;
4215 : 5938 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
4216 : :
4217 [ + - - + ]: 5938 : if (shdr != NULL && shdr->sh_type == SHT_GNU_LIBLIST)
4218 : : {
4219 : 0 : size_t sh_entsize = gelf_fsize (ebl->elf, ELF_T_LIB, 1, EV_CURRENT);
4220 : 0 : int nentries = shdr->sh_size / sh_entsize;
4221 : 0 : printf (ngettext ("\
4222 : : \nLibrary list section [%2zu] '%s' at offset %#0" PRIx64 " contains %d entry:\n",
4223 : : "\
4224 : : \nLibrary list section [%2zu] '%s' at offset %#0" PRIx64 " contains %d entries:\n",
4225 : : nentries),
4226 : : elf_ndxscn (scn),
4227 : 0 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
4228 : : shdr->sh_offset,
4229 : : nentries);
4230 : :
4231 : 0 : Elf_Data *data = elf_getdata (scn, NULL);
4232 [ # # ]: 0 : if (data == NULL)
4233 : 0 : return;
4234 : :
4235 : 0 : puts (_("\
4236 : : Library Time Stamp Checksum Version Flags"));
4237 : :
4238 [ # # ]: 0 : for (int cnt = 0; cnt < nentries; ++cnt)
4239 : : {
4240 : 0 : GElf_Lib lib_mem;
4241 : 0 : GElf_Lib *lib = gelf_getlib (data, cnt, &lib_mem);
4242 [ # # ]: 0 : if (unlikely (lib == NULL))
4243 : 0 : continue;
4244 : :
4245 : 0 : time_t t = (time_t) lib->l_time_stamp;
4246 : 0 : struct tm *tm = gmtime (&t);
4247 [ # # ]: 0 : if (unlikely (tm == NULL))
4248 : 0 : continue;
4249 : :
4250 : 0 : printf (" [%2d] %-29s %04u-%02u-%02uT%02u:%02u:%02u %08x %-7u %u\n",
4251 : 0 : cnt, elf_strptr (ebl->elf, shdr->sh_link, lib->l_name),
4252 : 0 : tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
4253 : : tm->tm_hour, tm->tm_min, tm->tm_sec,
4254 : 0 : (unsigned int) lib->l_checksum,
4255 : 0 : (unsigned int) lib->l_version,
4256 : 0 : (unsigned int) lib->l_flags);
4257 : : }
4258 : : }
4259 : : }
4260 : : }
4261 : :
4262 : : static inline size_t
4263 : 16 : left (Elf_Data *data,
4264 : : const unsigned char *p)
4265 : : {
4266 : 16 : return (const unsigned char *) data->d_buf + data->d_size - p;
4267 : : }
4268 : :
4269 : : static void
4270 : 192 : print_attributes (Ebl *ebl, const GElf_Ehdr *ehdr)
4271 : : {
4272 : : /* Find the object attributes sections. For this we have to search
4273 : : through the section table. */
4274 : 192 : Elf_Scn *scn = NULL;
4275 : :
4276 : : /* Get the section header string table index. */
4277 : 192 : size_t shstrndx;
4278 [ - + ]: 192 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
4279 : 0 : error_exit (0, _("cannot get section header string table index"));
4280 : :
4281 [ + + ]: 6130 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
4282 : : {
4283 : 5938 : GElf_Shdr shdr_mem;
4284 : 5938 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
4285 : :
4286 [ + - + + ]: 5938 : if (shdr == NULL || (shdr->sh_type != SHT_GNU_ATTRIBUTES
4287 [ + + ]: 5932 : && (shdr->sh_type != SHT_ARM_ATTRIBUTES
4288 [ - + ]: 2 : || ehdr->e_machine != EM_ARM)
4289 [ + + ]: 5930 : && (shdr->sh_type != SHT_CSKY_ATTRIBUTES
4290 [ + - ]: 2 : || ehdr->e_machine != EM_CSKY)
4291 [ - + ]: 5930 : && (shdr->sh_type != SHT_RISCV_ATTRIBUTES
4292 [ # # ]: 0 : || ehdr->e_machine != EM_RISCV)))
4293 : 5930 : continue;
4294 : :
4295 : 8 : printf (_("\
4296 : : \nObject attributes section [%2zu] '%s' of %" PRIu64
4297 : : " bytes at offset %#0" PRIx64 ":\n"),
4298 : : elf_ndxscn (scn),
4299 : 8 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
4300 : : shdr->sh_size, shdr->sh_offset);
4301 : :
4302 : 8 : Elf_Data *data = elf_rawdata (scn, NULL);
4303 [ + - + - ]: 8 : if (unlikely (data == NULL || data->d_size == 0))
4304 : 0 : return;
4305 : :
4306 : 8 : const unsigned char *p = data->d_buf;
4307 : :
4308 : : /* There is only one 'version', A. */
4309 [ + - ]: 8 : if (unlikely (*p++ != 'A'))
4310 : : return;
4311 : :
4312 : 8 : fputs (_(" Owner Size\n"), stdout);
4313 : :
4314 : : /* Loop over the sections. */
4315 [ + + ]: 16 : while (left (data, p) >= 4)
4316 : : {
4317 : : /* Section length. */
4318 : 8 : uint32_t len;
4319 [ + + ]: 8 : memcpy (&len, p, sizeof len);
4320 : :
4321 [ + + ]: 8 : if (MY_ELFDATA != ehdr->e_ident[EI_DATA])
4322 : 4 : CONVERT (len);
4323 : :
4324 [ + - ]: 8 : if (unlikely (len > left (data, p)))
4325 : : break;
4326 : :
4327 : : /* Section vendor name. */
4328 : 8 : const unsigned char *name = p + sizeof len;
4329 : 8 : p += len;
4330 : :
4331 : 8 : unsigned const char *q = memchr (name, '\0', len);
4332 [ + - ]: 8 : if (unlikely (q == NULL))
4333 : : break;
4334 : 8 : ++q;
4335 : :
4336 : 8 : printf (_(" %-13s %4" PRIu32 "\n"), name, len);
4337 : :
4338 : 16 : bool gnu_vendor = (q - name == sizeof "gnu"
4339 [ + + - + ]: 8 : && !memcmp (name, "gnu", sizeof "gnu"));
4340 : :
4341 : : /* Loop over subsections. */
4342 [ + + ]: 8 : if (shdr->sh_type != SHT_GNU_ATTRIBUTES
4343 [ + - ]: 6 : || gnu_vendor)
4344 [ + + ]: 16 : while (q < p)
4345 : : {
4346 : 8 : const unsigned char *const sub = q;
4347 : :
4348 : 8 : unsigned int subsection_tag;
4349 : 8 : get_uleb128 (subsection_tag, q, p);
4350 [ + - ]: 8 : if (unlikely (q >= p))
4351 : : break;
4352 : :
4353 : 8 : uint32_t subsection_len;
4354 [ + - ]: 8 : if (unlikely (p - sub < (ptrdiff_t) sizeof subsection_len))
4355 : : break;
4356 : :
4357 [ + + ]: 8 : memcpy (&subsection_len, q, sizeof subsection_len);
4358 : :
4359 [ + + ]: 8 : if (MY_ELFDATA != ehdr->e_ident[EI_DATA])
4360 : 4 : CONVERT (subsection_len);
4361 : :
4362 : : /* Don't overflow, ptrdiff_t might be 32bits, but signed. */
4363 [ + - + - ]: 8 : if (unlikely (subsection_len == 0
4364 : : || subsection_len >= (uint32_t) PTRDIFF_MAX
4365 : : || p - sub < (ptrdiff_t) subsection_len))
4366 : : break;
4367 : :
4368 : 8 : const unsigned char *r = q + sizeof subsection_len;
4369 : 8 : q = sub + subsection_len;
4370 : :
4371 [ - + ]: 8 : switch (subsection_tag)
4372 : : {
4373 : 0 : default:
4374 : : /* Unknown subsection, print and skip. */
4375 : 0 : printf (_(" %-4u %12" PRIu32 "\n"),
4376 : : subsection_tag, subsection_len);
4377 : 0 : break;
4378 : :
4379 : 8 : case 1: /* Tag_File */
4380 : 8 : printf (_(" File: %11" PRIu32 "\n"),
4381 : : subsection_len);
4382 : :
4383 [ + + ]: 52 : while (r < q)
4384 : : {
4385 : 44 : unsigned int tag;
4386 : 44 : get_uleb128 (tag, r, q);
4387 [ + - ]: 44 : if (unlikely (r >= q))
4388 : : break;
4389 : :
4390 : : /* GNU style tags have either a uleb128 value,
4391 : : when lowest bit is not set, or a string
4392 : : when the lowest bit is set.
4393 : : "compatibility" (32) is special. It has
4394 : : both a string and a uleb128 value. For
4395 : : non-gnu we assume 6 till 31 only take ints.
4396 : : XXX see arm backend, do we need a separate
4397 : : hook? */
4398 : 44 : uint64_t value = 0;
4399 : 44 : const char *string = NULL;
4400 [ + - + + ]: 44 : if (tag == 32 || (tag & 1) == 0
4401 [ + - + + ]: 16 : || (! gnu_vendor && (tag > 5 && tag < 32)))
4402 : : {
4403 : : // Note r >= q check above.
4404 : 42 : get_uleb128 (value, r, q);
4405 [ + - ]: 42 : if (r > q)
4406 : : break;
4407 : : }
4408 [ + - ]: 44 : if (tag == 32
4409 [ + + ]: 44 : || ((tag & 1) != 0
4410 [ + - ]: 16 : && (gnu_vendor
4411 [ + - ]: 16 : || (! gnu_vendor && tag > 32)))
4412 [ + + + + ]: 44 : || (! gnu_vendor && tag > 3 && tag < 6))
4413 : : {
4414 : 2 : string = (const char *) r;
4415 : 2 : r = memchr (r, '\0', q - r);
4416 [ + - ]: 2 : if (r == NULL)
4417 : : break;
4418 : 2 : ++r;
4419 : : }
4420 : :
4421 : 44 : const char *tag_name = NULL;
4422 : 44 : const char *value_name = NULL;
4423 : 44 : ebl_check_object_attribute (ebl, (const char *) name,
4424 : : tag, value,
4425 : : &tag_name, &value_name);
4426 : :
4427 [ + - ]: 44 : if (tag_name != NULL)
4428 : : {
4429 [ - + ]: 44 : if (tag == 32)
4430 : 0 : printf (_(" %s: %" PRId64 ", %s\n"),
4431 : : tag_name, value, string);
4432 [ + + + + ]: 44 : else if (string == NULL && value_name == NULL)
4433 : 2 : printf (_(" %s: %" PRId64 "\n"),
4434 : : tag_name, value);
4435 : : else
4436 : 42 : printf (_(" %s: %s\n"),
4437 : : tag_name, string ?: value_name);
4438 : : }
4439 : : else
4440 : : {
4441 : : /* For "gnu" vendor 32 "compatibility" has
4442 : : already been handled above. */
4443 [ # # # # ]: 0 : assert (tag != 32
4444 : : || strcmp ((const char *) name, "gnu"));
4445 [ # # ]: 0 : if (string == NULL)
4446 : 0 : printf (_(" %u: %" PRId64 "\n"),
4447 : : tag, value);
4448 : : else
4449 : 0 : printf (_(" %u: %s\n"),
4450 : : tag, string);
4451 : : }
4452 : : }
4453 : : }
4454 : : }
4455 : : }
4456 : : }
4457 : : }
4458 : :
4459 : : /* Returns either the (relocated) data from the Dwarf, or tries to get
4460 : : the "raw" (uncompressed) data from the Elf section. Produces a
4461 : : warning if the data cannot be found (or decompressed). */
4462 : : static Elf_Data *
4463 : 810 : get_debug_elf_data (Dwarf *dbg, Ebl *ebl, int idx, Elf_Scn *scn)
4464 : : {
4465 : : /* We prefer to get the section data from the Dwarf because that
4466 : : might have been relocated already. Note this is subtly wrong if
4467 : : there are multiple sections with the same .debug name. */
4468 [ - + ]: 810 : if (dbg->sectiondata[idx] != NULL)
4469 : : return dbg->sectiondata[idx];
4470 : :
4471 : 0 : GElf_Shdr shdr_mem;
4472 : 0 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
4473 [ # # # # ]: 0 : if (shdr != NULL && (shdr->sh_flags & SHF_COMPRESSED) != 0)
4474 : : {
4475 [ # # ]: 0 : if (elf_compress (scn, 0, 0) < 0)
4476 : : {
4477 : 0 : error (0, 0, "%s [%zd] '%s'\n",
4478 : : _("Couldn't uncompress section"),
4479 : : elf_ndxscn (scn), section_name (ebl, shdr));
4480 : 0 : return NULL;
4481 : : }
4482 : : }
4483 : :
4484 : 0 : Elf_Data *data = elf_getdata (scn, NULL);
4485 [ # # ]: 0 : if (data == NULL)
4486 : 0 : error (0, 0, "%s [%zd] '%s': %s\n",
4487 : : _("Couldn't get data from section"),
4488 : : elf_ndxscn (scn), section_name (ebl, shdr), elf_errmsg (-1));
4489 : :
4490 : 0 : return elf_getdata (scn, NULL);
4491 : : }
4492 : :
4493 : : static void
4494 : 2259466 : print_dwarf_addr (Dwfl_Module *dwflmod,
4495 : : int address_size, Dwarf_Addr address, Dwarf_Addr raw,
4496 : : FILE *out)
4497 : : {
4498 : : /* See if there is a name we can give for this address. */
4499 : 2259466 : GElf_Sym sym;
4500 : 2259466 : GElf_Off off = 0;
4501 [ + + ]: 14150 : const char *name = (print_address_names && ! print_unresolved_addresses)
4502 : 13986 : ? dwfl_module_addrinfo (dwflmod, address, &off, &sym, NULL, NULL, NULL)
4503 [ + + ]: 2259466 : : NULL;
4504 : :
4505 : 2259466 : const char *scn;
4506 [ + + ]: 2259466 : if (print_unresolved_addresses)
4507 : : {
4508 : 168 : address = raw;
4509 : 168 : scn = NULL;
4510 : : }
4511 : : else
4512 : : {
4513 : : /* Relativize the address. */
4514 : 2259298 : int n = dwfl_module_relocations (dwflmod);
4515 [ + + ]: 2259298 : int i = n < 1 ? -1 : dwfl_module_relocate_address (dwflmod, &address);
4516 : :
4517 : : /* In an ET_REL file there is a section name to refer to. */
4518 : 2256546 : scn = (i < 0 ? NULL
4519 [ - + ]: 2256546 : : dwfl_module_relocation_info (dwflmod, i, NULL));
4520 : : }
4521 : :
4522 [ - + ]: 2259466 : if ((name != NULL
4523 : 13578 : ? (off != 0
4524 : : ? (scn != NULL
4525 : : ? (address_size == 0
4526 : 2 : ? fprintf (out, "%s+%#" PRIx64 " <%s+%#" PRIx64 ">",
4527 : : scn, address, name, off)
4528 : 8 : : fprintf (out, "%s+%#0*" PRIx64 " <%s+%#" PRIx64 ">",
4529 : 4 : scn, 2 + address_size * 2, address,
4530 : : name, off))
4531 : : : (address_size == 0
4532 : 364 : ? fprintf (out, "%#" PRIx64 " <%s+%#" PRIx64 ">",
4533 : : address, name, off)
4534 : 2156 : : fprintf (out, "%#0*" PRIx64 " <%s+%#" PRIx64 ">",
4535 : 1078 : 2 + address_size * 2, address,
4536 : : name, off)))
4537 : : : (scn != NULL
4538 : : ? (address_size == 0
4539 : 2 : ? fprintf (out, "%s+%#" PRIx64 " <%s>", scn, address, name)
4540 : 22256 : : fprintf (out, "%s+%#0*" PRIx64 " <%s>",
4541 : 11128 : scn, 2 + address_size * 2, address, name))
4542 : : : (address_size == 0
4543 : 160 : ? fprintf (out, "%#" PRIx64 " <%s>", address, name)
4544 : 1680 : : fprintf (out, "%#0*" PRIx64 " <%s>",
4545 : 840 : 2 + address_size * 2, address, name))))
4546 : : : (scn != NULL
4547 : : ? (address_size == 0
4548 : 825120 : ? fprintf (out, "%s+%#" PRIx64, scn, address)
4549 : 2840580 : : fprintf (out, "%s+%#0*" PRIx64,
4550 : 1420290 : scn, 2 + address_size * 2, address))
4551 : : : (address_size == 0
4552 : 70 : ? fprintf (out, "%#" PRIx64, address)
4553 : 816 : : fprintf (out, "%#0*" PRIx64,
4554 [ + + + + : 4518524 : 2 + address_size * 2, address)))) < 0)
+ + + + +
+ + + + +
+ + + + +
+ + + ]
4555 : 0 : error_exit (0, _("sprintf failure"));
4556 : 2259466 : }
4557 : :
4558 : :
4559 : : static const char *
4560 : 2312416 : dwarf_tag_string (unsigned int tag)
4561 : : {
4562 [ - + + - : 2312416 : switch (tag)
- - - - +
+ + + + -
- - - - -
+ - + - +
- - + + -
+ - - - -
- - - + -
+ - + + +
- - - - -
- + - - +
- - - + -
+ + + + -
- - - - +
+ + + - +
+ + + - -
- ]
4563 : : {
4564 : : #define DWARF_ONE_KNOWN_DW_TAG(NAME, CODE) case CODE: return #NAME;
4565 : 2312416 : DWARF_ALL_KNOWN_DW_TAG
4566 : : #undef DWARF_ONE_KNOWN_DW_TAG
4567 : 0 : default:
4568 : 0 : return NULL;
4569 : : }
4570 : : }
4571 : :
4572 : :
4573 : : static const char *
4574 : 8941410 : dwarf_attr_string (unsigned int attrnum)
4575 : : {
4576 [ + - + - : 8941410 : switch (attrnum)
- - - + -
- + + + -
- - - + +
- + - - +
- + - + -
- - - - -
- - - - -
- - - - -
- - - + -
+ - + - +
- - - + +
- - - + -
+ - + + -
- + + + +
- + + + -
+ - - + -
+ - + + -
+ - - + +
+ + - - -
- - + + +
+ - + - +
- - + - +
+ - + - +
+ - + + +
+ + + + -
- + - - -
+ - + - -
- - + + -
+ - - - -
+ - - - +
+ - + - -
- - + + -
- - - - +
+ - - - -
- - - + ]
4577 : : {
4578 : : #define DWARF_ONE_KNOWN_DW_AT(NAME, CODE) case CODE: return #NAME;
4579 : 8941408 : DWARF_ALL_KNOWN_DW_AT
4580 : : #undef DWARF_ONE_KNOWN_DW_AT
4581 : 0 : default:
4582 : 0 : return NULL;
4583 : : }
4584 : : }
4585 : :
4586 : :
4587 : : static const char *
4588 : 8951828 : dwarf_form_string (unsigned int form)
4589 : : {
4590 [ + + + + : 8951828 : switch (form)
+ - - - -
- + - - +
+ + + + +
+ + + - +
+ - - + -
- + - - +
+ + + + +
- + + - -
- + - + ]
4591 : : {
4592 : : #define DWARF_ONE_KNOWN_DW_FORM(NAME, CODE) case CODE: return #NAME;
4593 : 8951824 : DWARF_ALL_KNOWN_DW_FORM
4594 : : #undef DWARF_ONE_KNOWN_DW_FORM
4595 : 0 : default:
4596 : 0 : return NULL;
4597 : : }
4598 : : }
4599 : :
4600 : :
4601 : : static const char *
4602 : 3740 : dwarf_lang_string (unsigned int lang)
4603 : : {
4604 [ - - + - : 3740 : switch (lang)
- - - + -
- + + - +
- - + - -
- - - - -
- - - - -
+ - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - + - ]
4605 : : {
4606 : : #define DWARF_ONE_KNOWN_DW_LANG(NAME, CODE) case CODE: return #NAME;
4607 : 3738 : DWARF_ALL_KNOWN_DW_LANG
4608 : : #undef DWARF_ONE_KNOWN_DW_LANG
4609 : 2 : default:
4610 : 2 : return NULL;
4611 : : }
4612 : : }
4613 : :
4614 : :
4615 : : static const char *
4616 : 4 : dwarf_lname_string (unsigned int lname)
4617 : : {
4618 [ - - - + : 4 : switch (lname)
- + - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - ]
4619 : : {
4620 : : #define DWARF_ONE_KNOWN_DW_LNAME(NAME, CODE) case CODE: return #NAME;
4621 : 4 : DWARF_ALL_KNOWN_DW_LNAME
4622 : : #undef DWARF_ONE_KNOWN_DW_LNAME
4623 : 0 : default:
4624 : 0 : return NULL;
4625 : : }
4626 : : }
4627 : :
4628 : :
4629 : : static const char *
4630 : 7294 : dwarf_inline_string (unsigned int code)
4631 : : {
4632 : 7294 : static const char *const known[] =
4633 : : {
4634 : : #define DWARF_ONE_KNOWN_DW_INL(NAME, CODE) [CODE] = #NAME,
4635 : : DWARF_ALL_KNOWN_DW_INL
4636 : : #undef DWARF_ONE_KNOWN_DW_INL
4637 : : };
4638 : :
4639 : 7294 : if (likely (code < sizeof (known) / sizeof (known[0])))
4640 : 7294 : return known[code];
4641 : :
4642 : : return NULL;
4643 : : }
4644 : :
4645 : :
4646 : : static const char *
4647 : 59422 : dwarf_encoding_string (unsigned int code)
4648 : : {
4649 : 59422 : static const char *const known[] =
4650 : : {
4651 : : #define DWARF_ONE_KNOWN_DW_ATE(NAME, CODE) [CODE] = #NAME,
4652 : : DWARF_ALL_KNOWN_DW_ATE
4653 : : #undef DWARF_ONE_KNOWN_DW_ATE
4654 : : };
4655 : :
4656 : 59422 : if (likely (code < sizeof (known) / sizeof (known[0])))
4657 : 59422 : return known[code];
4658 : :
4659 : : return NULL;
4660 : : }
4661 : :
4662 : :
4663 : : static const char *
4664 : 0 : dwarf_access_string (unsigned int code)
4665 : : {
4666 : 0 : static const char *const known[] =
4667 : : {
4668 : : #define DWARF_ONE_KNOWN_DW_ACCESS(NAME, CODE) [CODE] = #NAME,
4669 : : DWARF_ALL_KNOWN_DW_ACCESS
4670 : : #undef DWARF_ONE_KNOWN_DW_ACCESS
4671 : : };
4672 : :
4673 : 0 : if (likely (code < sizeof (known) / sizeof (known[0])))
4674 : 0 : return known[code];
4675 : :
4676 : : return NULL;
4677 : : }
4678 : :
4679 : :
4680 : : static const char *
4681 : 0 : dwarf_defaulted_string (unsigned int code)
4682 : : {
4683 : 0 : static const char *const known[] =
4684 : : {
4685 : : #define DWARF_ONE_KNOWN_DW_DEFAULTED(NAME, CODE) [CODE] = #NAME,
4686 : : DWARF_ALL_KNOWN_DW_DEFAULTED
4687 : : #undef DWARF_ONE_KNOWN_DW_DEFAULTED
4688 : : };
4689 : :
4690 : 0 : if (likely (code < sizeof (known) / sizeof (known[0])))
4691 : 0 : return known[code];
4692 : :
4693 : : return NULL;
4694 : : }
4695 : :
4696 : :
4697 : : static const char *
4698 : 0 : dwarf_visibility_string (unsigned int code)
4699 : : {
4700 : 0 : static const char *const known[] =
4701 : : {
4702 : : #define DWARF_ONE_KNOWN_DW_VIS(NAME, CODE) [CODE] = #NAME,
4703 : : DWARF_ALL_KNOWN_DW_VIS
4704 : : #undef DWARF_ONE_KNOWN_DW_VIS
4705 : : };
4706 : :
4707 : 0 : if (likely (code < sizeof (known) / sizeof (known[0])))
4708 : 0 : return known[code];
4709 : :
4710 : : return NULL;
4711 : : }
4712 : :
4713 : :
4714 : : static const char *
4715 : 0 : dwarf_virtuality_string (unsigned int code)
4716 : : {
4717 : 0 : static const char *const known[] =
4718 : : {
4719 : : #define DWARF_ONE_KNOWN_DW_VIRTUALITY(NAME, CODE) [CODE] = #NAME,
4720 : : DWARF_ALL_KNOWN_DW_VIRTUALITY
4721 : : #undef DWARF_ONE_KNOWN_DW_VIRTUALITY
4722 : : };
4723 : :
4724 : 0 : if (likely (code < sizeof (known) / sizeof (known[0])))
4725 : 0 : return known[code];
4726 : :
4727 : : return NULL;
4728 : : }
4729 : :
4730 : :
4731 : : static const char *
4732 : 0 : dwarf_identifier_case_string (unsigned int code)
4733 : : {
4734 : 0 : static const char *const known[] =
4735 : : {
4736 : : #define DWARF_ONE_KNOWN_DW_ID(NAME, CODE) [CODE] = #NAME,
4737 : : DWARF_ALL_KNOWN_DW_ID
4738 : : #undef DWARF_ONE_KNOWN_DW_ID
4739 : : };
4740 : :
4741 : 0 : if (likely (code < sizeof (known) / sizeof (known[0])))
4742 : 0 : return known[code];
4743 : :
4744 : : return NULL;
4745 : : }
4746 : :
4747 : :
4748 : : static const char *
4749 : 0 : dwarf_calling_convention_string (unsigned int code)
4750 : : {
4751 : 0 : static const char *const known[] =
4752 : : {
4753 : : #define DWARF_ONE_KNOWN_DW_CC(NAME, CODE) [CODE] = #NAME,
4754 : : DWARF_ALL_KNOWN_DW_CC
4755 : : #undef DWARF_ONE_KNOWN_DW_CC
4756 : : };
4757 : :
4758 : 0 : if (likely (code < sizeof (known) / sizeof (known[0])))
4759 : 0 : return known[code];
4760 : :
4761 : : return NULL;
4762 : : }
4763 : :
4764 : :
4765 : : static const char *
4766 : 0 : dwarf_ordering_string (unsigned int code)
4767 : : {
4768 : 0 : static const char *const known[] =
4769 : : {
4770 : : #define DWARF_ONE_KNOWN_DW_ORD(NAME, CODE) [CODE] = #NAME,
4771 : : DWARF_ALL_KNOWN_DW_ORD
4772 : : #undef DWARF_ONE_KNOWN_DW_ORD
4773 : : };
4774 : :
4775 : 0 : if (likely (code < sizeof (known) / sizeof (known[0])))
4776 : 0 : return known[code];
4777 : :
4778 : : return NULL;
4779 : : }
4780 : :
4781 : :
4782 : : static const char *
4783 : 16 : dwarf_discr_list_string (unsigned int code)
4784 : : {
4785 : 16 : static const char *const known[] =
4786 : : {
4787 : : #define DWARF_ONE_KNOWN_DW_DSC(NAME, CODE) [CODE] = #NAME,
4788 : : DWARF_ALL_KNOWN_DW_DSC
4789 : : #undef DWARF_ONE_KNOWN_DW_DSC
4790 : : };
4791 : :
4792 : 16 : if (likely (code < sizeof (known) / sizeof (known[0])))
4793 : 16 : return known[code];
4794 : :
4795 : : return NULL;
4796 : : }
4797 : :
4798 : :
4799 : : static const char *
4800 : 1117674 : dwarf_locexpr_opcode_string (unsigned int code)
4801 : : {
4802 : 1117674 : static const char *const known[] =
4803 : : {
4804 : : /* Normally we can't afford building huge table of 64K entries,
4805 : : most of them zero, just because there are a couple defined
4806 : : values at the far end. In case of opcodes, it's OK. */
4807 : : #define DWARF_ONE_KNOWN_DW_OP(NAME, CODE) [CODE] = #NAME,
4808 : : DWARF_ALL_KNOWN_DW_OP
4809 : : #undef DWARF_ONE_KNOWN_DW_OP
4810 : : };
4811 : :
4812 : 1117674 : if (likely (code < sizeof (known) / sizeof (known[0])))
4813 : 1117674 : return known[code];
4814 : :
4815 : : return NULL;
4816 : : }
4817 : :
4818 : :
4819 : : static const char *
4820 : 3486 : dwarf_unit_string (unsigned int type)
4821 : : {
4822 [ - + - - : 3486 : switch (type)
- - + ]
4823 : : {
4824 : : #define DWARF_ONE_KNOWN_DW_UT(NAME, CODE) case CODE: return #NAME;
4825 : 14 : DWARF_ALL_KNOWN_DW_UT
4826 : : #undef DWARF_ONE_KNOWN_DW_UT
4827 : 0 : default:
4828 : 0 : return NULL;
4829 : : }
4830 : : }
4831 : :
4832 : :
4833 : : static const char *
4834 : 148322 : dwarf_range_list_encoding_string (unsigned int kind)
4835 : : {
4836 [ - + + - : 148322 : switch (kind)
+ - - -
+ ]
4837 : : {
4838 : : #define DWARF_ONE_KNOWN_DW_RLE(NAME, CODE) case CODE: return #NAME;
4839 : 131306 : DWARF_ALL_KNOWN_DW_RLE
4840 : : #undef DWARF_ONE_KNOWN_DW_RLE
4841 : 0 : default:
4842 : 0 : return NULL;
4843 : : }
4844 : : }
4845 : :
4846 : :
4847 : : static const char *
4848 : 655664 : dwarf_loc_list_encoding_string (unsigned int kind)
4849 : : {
4850 [ - - + + : 655664 : switch (kind)
- + - + -
- + ]
4851 : : {
4852 : : #define DWARF_ONE_KNOWN_DW_LLE(NAME, CODE) case CODE: return #NAME;
4853 : 610034 : DWARF_ALL_KNOWN_DW_LLE
4854 : : #undef DWARF_ONE_KNOWN_DW_LLE
4855 : : /* DW_LLE_GNU_view_pair is special/incompatible with default codes. */
4856 : 0 : case DW_LLE_GNU_view_pair: return "GNU_view_pair";
4857 : 0 : default:
4858 : 0 : return NULL;
4859 : : }
4860 : : }
4861 : :
4862 : :
4863 : : static const char *
4864 : 10418 : dwarf_line_content_description_string (unsigned int kind)
4865 : : {
4866 [ + + - - : 10418 : switch (kind)
- + ]
4867 : : {
4868 : : #define DWARF_ONE_KNOWN_DW_LNCT(NAME, CODE) case CODE: return #NAME;
4869 : 10410 : DWARF_ALL_KNOWN_DW_LNCT
4870 : : #undef DWARF_ONE_KNOWN_DW_LNCT
4871 : 0 : default:
4872 : 0 : return NULL;
4873 : : }
4874 : : }
4875 : :
4876 : :
4877 : : /* Used by all dwarf_foo_name functions. */
4878 : : static const char *
4879 : 21094020 : string_or_unknown (const char *known, unsigned int code,
4880 : : unsigned int lo_user, unsigned int hi_user,
4881 : : bool print_unknown_num)
4882 : : {
4883 : 21094020 : static char unknown_buf[20];
4884 : :
4885 [ + + ]: 21094020 : if (likely (known != NULL))
4886 : : return known;
4887 : :
4888 [ - + - - ]: 2 : if (lo_user != 0 && code >= lo_user && code <= hi_user)
4889 : : {
4890 : 0 : snprintf (unknown_buf, sizeof unknown_buf, "lo_user+%#x",
4891 : : code - lo_user);
4892 : 0 : return unknown_buf;
4893 : : }
4894 : :
4895 [ + - ]: 2 : if (print_unknown_num)
4896 : : {
4897 : 2 : snprintf (unknown_buf, sizeof unknown_buf, "??? (%#x)", code);
4898 : 2 : return unknown_buf;
4899 : : }
4900 : :
4901 : : return "???";
4902 : : }
4903 : :
4904 : :
4905 : : static const char *
4906 : 2312416 : dwarf_tag_name (unsigned int tag)
4907 : : {
4908 : 2312416 : const char *ret = dwarf_tag_string (tag);
4909 : 2312416 : return string_or_unknown (ret, tag, DW_TAG_lo_user, DW_TAG_hi_user, true);
4910 : : }
4911 : :
4912 : : static const char *
4913 : 8941410 : dwarf_attr_name (unsigned int attr)
4914 : : {
4915 : 8941410 : const char *ret = dwarf_attr_string (attr);
4916 : 8941410 : return string_or_unknown (ret, attr, DW_AT_lo_user, DW_AT_hi_user, true);
4917 : : }
4918 : :
4919 : :
4920 : : static const char *
4921 : 8951828 : dwarf_form_name (unsigned int form)
4922 : : {
4923 : 8951828 : const char *ret = dwarf_form_string (form);
4924 : 8951828 : return string_or_unknown (ret, form, 0, 0, true);
4925 : : }
4926 : :
4927 : :
4928 : : static const char *
4929 : 3736 : dwarf_lang_name (unsigned int lang)
4930 : : {
4931 : 3736 : const char *ret = dwarf_lang_string (lang);
4932 : 3736 : return string_or_unknown (ret, lang, DW_LANG_lo_user, DW_LANG_hi_user, false);
4933 : : }
4934 : :
4935 : :
4936 : : static const char *
4937 : 4 : dwarf_lname_name (unsigned int lname)
4938 : : {
4939 : 4 : const char *ret = dwarf_lname_string (lname);
4940 : 4 : return string_or_unknown (ret, lname, DW_LNAME_lo_user, DW_LNAME_hi_user,
4941 : : false);
4942 : : }
4943 : :
4944 : :
4945 : : static const char *
4946 : 7294 : dwarf_inline_name (unsigned int code)
4947 : : {
4948 [ + - ]: 7294 : const char *ret = dwarf_inline_string (code);
4949 : 7294 : return string_or_unknown (ret, code, 0, 0, false);
4950 : : }
4951 : :
4952 : :
4953 : : static const char *
4954 : 59422 : dwarf_encoding_name (unsigned int code)
4955 : : {
4956 [ + - ]: 59422 : const char *ret = dwarf_encoding_string (code);
4957 : 59422 : return string_or_unknown (ret, code, DW_ATE_lo_user, DW_ATE_hi_user, false);
4958 : : }
4959 : :
4960 : :
4961 : : static const char *
4962 : 0 : dwarf_access_name (unsigned int code)
4963 : : {
4964 [ # # ]: 0 : const char *ret = dwarf_access_string (code);
4965 : 0 : return string_or_unknown (ret, code, 0, 0, false);
4966 : : }
4967 : :
4968 : :
4969 : : static const char *
4970 : 0 : dwarf_defaulted_name (unsigned int code)
4971 : : {
4972 [ # # ]: 0 : const char *ret = dwarf_defaulted_string (code);
4973 : 0 : return string_or_unknown (ret, code, 0, 0, false);
4974 : : }
4975 : :
4976 : :
4977 : : static const char *
4978 : 0 : dwarf_visibility_name (unsigned int code)
4979 : : {
4980 [ # # ]: 0 : const char *ret = dwarf_visibility_string (code);
4981 : 0 : return string_or_unknown (ret, code, 0, 0, false);
4982 : : }
4983 : :
4984 : :
4985 : : static const char *
4986 : 0 : dwarf_virtuality_name (unsigned int code)
4987 : : {
4988 [ # # ]: 0 : const char *ret = dwarf_virtuality_string (code);
4989 : 0 : return string_or_unknown (ret, code, 0, 0, false);
4990 : : }
4991 : :
4992 : :
4993 : : static const char *
4994 : 0 : dwarf_identifier_case_name (unsigned int code)
4995 : : {
4996 [ # # ]: 0 : const char *ret = dwarf_identifier_case_string (code);
4997 : 0 : return string_or_unknown (ret, code, 0, 0, false);
4998 : : }
4999 : :
5000 : :
5001 : : static const char *
5002 : 0 : dwarf_calling_convention_name (unsigned int code)
5003 : : {
5004 [ # # ]: 0 : const char *ret = dwarf_calling_convention_string (code);
5005 : 0 : return string_or_unknown (ret, code, DW_CC_lo_user, DW_CC_hi_user, false);
5006 : : }
5007 : :
5008 : :
5009 : : static const char *
5010 : 0 : dwarf_ordering_name (unsigned int code)
5011 : : {
5012 [ # # ]: 0 : const char *ret = dwarf_ordering_string (code);
5013 : 0 : return string_or_unknown (ret, code, 0, 0, false);
5014 : : }
5015 : :
5016 : :
5017 : : static const char *
5018 : 16 : dwarf_discr_list_name (unsigned int code)
5019 : : {
5020 [ + - ]: 16 : const char *ret = dwarf_discr_list_string (code);
5021 : 16 : return string_or_unknown (ret, code, 0, 0, false);
5022 : : }
5023 : :
5024 : :
5025 : : static const char *
5026 : 3486 : dwarf_unit_name (unsigned int type)
5027 : : {
5028 : 3486 : const char *ret = dwarf_unit_string (type);
5029 : 3486 : return string_or_unknown (ret, type, DW_UT_lo_user, DW_UT_hi_user, true);
5030 : : }
5031 : :
5032 : :
5033 : : static const char *
5034 : 148322 : dwarf_range_list_encoding_name (unsigned int kind)
5035 : : {
5036 : 148322 : const char *ret = dwarf_range_list_encoding_string (kind);
5037 : 148322 : return string_or_unknown (ret, kind, 0, 0, false);
5038 : : }
5039 : :
5040 : :
5041 : : static const char *
5042 : 655664 : dwarf_loc_list_encoding_name (unsigned int kind)
5043 : : {
5044 : 655664 : const char *ret = dwarf_loc_list_encoding_string (kind);
5045 : 655664 : return string_or_unknown (ret, kind, 0, 0, false);
5046 : : }
5047 : :
5048 : :
5049 : : static const char *
5050 : 10418 : dwarf_line_content_description_name (unsigned int kind)
5051 : : {
5052 : 10418 : const char *ret = dwarf_line_content_description_string (kind);
5053 : 10418 : return string_or_unknown (ret, kind, DW_LNCT_lo_user, DW_LNCT_hi_user,
5054 : : false);
5055 : : }
5056 : :
5057 : :
5058 : : static void
5059 : 510 : print_block (size_t n, const void *block, FILE *out)
5060 : : {
5061 [ - + ]: 510 : if (n == 0)
5062 : 0 : fputs (empty_block_str, out);
5063 : : else
5064 : : {
5065 : 510 : fprintf (out, "%zu %s:", n, byte_block_str);
5066 : 510 : const unsigned char *data = block;
5067 : 2250 : do
5068 : 2250 : fprintf (out, " %02x", *data++);
5069 [ + + ]: 2250 : while (--n > 0);
5070 : 510 : fputc ('\n', out);
5071 : : }
5072 : 510 : }
5073 : :
5074 : : static void
5075 : 24 : print_bytes (size_t n, const unsigned char *bytes, FILE *out)
5076 : : {
5077 [ + + ]: 408 : while (n-- > 0)
5078 : : {
5079 : 384 : fprintf (out, "%02x", *bytes++);
5080 [ + + ]: 384 : if (n > 0)
5081 : 360 : fprintf (out, " ");
5082 : : }
5083 : 24 : }
5084 : :
5085 : : static int
5086 : 148 : get_indexed_addr (Dwarf_CU *cu, Dwarf_Word idx, Dwarf_Addr *addr)
5087 : : {
5088 [ - + ]: 148 : if (cu == NULL)
5089 : : return -1;
5090 : :
5091 : 148 : Elf_Data *debug_addr = cu->dbg->sectiondata[IDX_debug_addr];
5092 [ - + ]: 148 : if (debug_addr == NULL)
5093 : : return -1;
5094 : :
5095 : 148 : Dwarf_Off base = __libdw_cu_addr_base (cu);
5096 : 148 : Dwarf_Word off = idx * cu->address_size;
5097 [ - + ]: 148 : if (base > debug_addr->d_size
5098 [ - + ]: 148 : || off > debug_addr->d_size - base
5099 [ - + ]: 148 : || cu->address_size > debug_addr->d_size - base - off)
5100 : : return -1;
5101 : :
5102 : 148 : const unsigned char *addrp = debug_addr->d_buf + base + off;
5103 [ + + ]: 148 : if (cu->address_size == 4)
5104 [ - + ]: 12 : *addr = read_4ubyte_unaligned (cu->dbg, addrp);
5105 : : else
5106 [ - + ]: 136 : *addr = read_8ubyte_unaligned (cu->dbg, addrp);
5107 : :
5108 : : return 0;
5109 : : }
5110 : :
5111 : : static void
5112 : 786070 : print_ops (Dwfl_Module *dwflmod, Dwarf *dbg, int indent, int indentrest,
5113 : : unsigned int vers, unsigned int addrsize, unsigned int offset_size,
5114 : : struct Dwarf_CU *cu, Dwarf_Word len, const unsigned char *data,
5115 : : FILE *out)
5116 : : {
5117 [ + + ]: 786070 : const unsigned int ref_size = vers < 3 ? addrsize : offset_size;
5118 : :
5119 [ - + ]: 786070 : if (len == 0)
5120 : : {
5121 : 0 : fprintf (out, "%*s(empty)\n", indent, "");
5122 : 0 : return;
5123 : : }
5124 : :
5125 : : #define NEED(n) if (len < (Dwarf_Word) (n)) goto invalid
5126 : : #define CONSUME(n) NEED (n); else len -= (n)
5127 : :
5128 : : Dwarf_Word offset = 0;
5129 [ + + ]: 1903744 : while (len-- > 0)
5130 : 1117674 : {
5131 : 1117674 : uint_fast8_t op = *data++;
5132 : :
5133 [ + - ]: 1117674 : const char *op_name = dwarf_locexpr_opcode_string (op);
5134 [ - + ]: 1117674 : if (unlikely (op_name == NULL))
5135 : : {
5136 : 0 : static char buf[20];
5137 [ # # ]: 0 : if (op >= DW_OP_lo_user)
5138 : 0 : snprintf (buf, sizeof buf, "lo_user+%#x", op - DW_OP_lo_user);
5139 : : else
5140 : 0 : snprintf (buf, sizeof buf, "??? (%#x)", op);
5141 : : op_name = buf;
5142 : : }
5143 : :
5144 [ + - + + : 1117674 : switch (op)
+ + + + -
- + + - +
- - + + +
+ + - - -
- + + + ]
5145 : : {
5146 : 26972 : case DW_OP_addr:;
5147 : : /* Address operand. */
5148 : 26972 : Dwarf_Word addr;
5149 [ - + ]: 26972 : NEED (addrsize);
5150 [ + + ]: 26972 : if (addrsize == 4)
5151 [ + + ]: 108 : addr = read_4ubyte_unaligned (dbg, data);
5152 [ + - ]: 26864 : else if (addrsize == 8)
5153 [ + + ]: 26864 : addr = read_8ubyte_unaligned (dbg, data);
5154 : : else
5155 : 0 : goto invalid;
5156 : 26972 : data += addrsize;
5157 : 26972 : CONSUME (addrsize);
5158 : :
5159 : 26972 : fprintf (out, "%*s[%2" PRIuMAX "] %s ",
5160 : : indent, "", (uintmax_t) offset, op_name);
5161 : 26972 : print_dwarf_addr (dwflmod, 0, addr, addr, out);
5162 : 26972 : fprintf (out, "\n");
5163 : :
5164 : 26972 : offset += 1 + addrsize;
5165 : 26972 : break;
5166 : :
5167 : 0 : case DW_OP_call_ref:
5168 : : case DW_OP_GNU_variable_value:
5169 : : /* Offset operand. */
5170 [ # # ]: 0 : if (ref_size != 4 && ref_size != 8)
5171 : 0 : goto invalid; /* Cannot be used in CFA. */
5172 [ # # ]: 0 : NEED (ref_size);
5173 [ # # ]: 0 : if (ref_size == 4)
5174 [ # # ]: 0 : addr = read_4ubyte_unaligned (dbg, data);
5175 : : else
5176 [ # # ]: 0 : addr = read_8ubyte_unaligned (dbg, data);
5177 : 0 : data += ref_size;
5178 : 0 : CONSUME (ref_size);
5179 : : /* addr is a DIE offset, so format it as one. */
5180 : 0 : fprintf (out, "%*s[%2" PRIuMAX "] %s [%6" PRIxMAX "]\n",
5181 : : indent, "", (uintmax_t) offset,
5182 : : op_name, (uintmax_t) addr);
5183 : 0 : offset += 1 + ref_size;
5184 : 0 : break;
5185 : :
5186 : 24688 : case DW_OP_deref_size:
5187 : : case DW_OP_xderef_size:
5188 : : case DW_OP_pick:
5189 : : case DW_OP_const1u:
5190 : : // XXX value might be modified by relocation
5191 [ - + ]: 24688 : NEED (1);
5192 : 49376 : fprintf (out, "%*s[%2" PRIuMAX "] %s %" PRIu8 "\n",
5193 : : indent, "", (uintmax_t) offset,
5194 : 24688 : op_name, *((uint8_t *) data));
5195 : 24688 : ++data;
5196 : 24688 : --len;
5197 : 24688 : offset += 2;
5198 : 24688 : break;
5199 : :
5200 : 3298 : case DW_OP_const2u:
5201 [ - + ]: 3298 : NEED (2);
5202 : : // XXX value might be modified by relocation
5203 : 6596 : fprintf (out, "%*s[%2" PRIuMAX "] %s %" PRIu16 "\n",
5204 : : indent, "", (uintmax_t) offset,
5205 [ - + ]: 3298 : op_name, read_2ubyte_unaligned (dbg, data));
5206 : 3298 : CONSUME (2);
5207 : 3298 : data += 2;
5208 : 3298 : offset += 3;
5209 : 3298 : break;
5210 : :
5211 : 2906 : case DW_OP_const4u:
5212 [ - + ]: 2906 : NEED (4);
5213 : : // XXX value might be modified by relocation
5214 : 2906 : fprintf (out, "%*s[%2" PRIuMAX "] %s %" PRIu32 "\n",
5215 : : indent, "", (uintmax_t) offset,
5216 [ - + ]: 2906 : op_name, read_4ubyte_unaligned (dbg, data));
5217 : 2906 : CONSUME (4);
5218 : 2906 : data += 4;
5219 : 2906 : offset += 5;
5220 : 2906 : break;
5221 : :
5222 : 126 : case DW_OP_const8u:
5223 [ - + ]: 126 : NEED (8);
5224 : : // XXX value might be modified by relocation
5225 : 126 : fprintf (out, "%*s[%2" PRIuMAX "] %s %" PRIu64 "\n",
5226 : : indent, "", (uintmax_t) offset,
5227 [ - + ]: 126 : op_name, (uint64_t) read_8ubyte_unaligned (dbg, data));
5228 : 126 : CONSUME (8);
5229 : 126 : data += 8;
5230 : 126 : offset += 9;
5231 : 126 : break;
5232 : :
5233 : 5152 : case DW_OP_const1s:
5234 [ - + ]: 5152 : NEED (1);
5235 : : // XXX value might be modified by relocation
5236 : 10304 : fprintf (out, "%*s[%2" PRIuMAX "] %s %" PRId8 "\n",
5237 : : indent, "", (uintmax_t) offset,
5238 : 5152 : op_name, *((int8_t *) data));
5239 : 5152 : ++data;
5240 : 5152 : --len;
5241 : 5152 : offset += 2;
5242 : 5152 : break;
5243 : :
5244 : 10 : case DW_OP_const2s:
5245 [ - + ]: 10 : NEED (2);
5246 : : // XXX value might be modified by relocation
5247 : 20 : fprintf (out, "%*s[%2" PRIuMAX "] %s %" PRId16 "\n",
5248 : : indent, "", (uintmax_t) offset,
5249 [ - + ]: 10 : op_name, read_2sbyte_unaligned (dbg, data));
5250 : 10 : CONSUME (2);
5251 : 10 : data += 2;
5252 : 10 : offset += 3;
5253 : 10 : break;
5254 : :
5255 : 0 : case DW_OP_const4s:
5256 [ # # ]: 0 : NEED (4);
5257 : : // XXX value might be modified by relocation
5258 : 0 : fprintf (out, "%*s[%2" PRIuMAX "] %s %" PRId32 "\n",
5259 : : indent, "", (uintmax_t) offset,
5260 [ # # ]: 0 : op_name, read_4sbyte_unaligned (dbg, data));
5261 : 0 : CONSUME (4);
5262 : 0 : data += 4;
5263 : 0 : offset += 5;
5264 : 0 : break;
5265 : :
5266 : 0 : case DW_OP_const8s:
5267 [ # # ]: 0 : NEED (8);
5268 : : // XXX value might be modified by relocation
5269 : 0 : fprintf (out, "%*s[%2" PRIuMAX "] %s %" PRId64 "\n",
5270 : : indent, "", (uintmax_t) offset,
5271 [ # # ]: 0 : op_name, read_8sbyte_unaligned (dbg, data));
5272 : 0 : CONSUME (8);
5273 : 0 : data += 8;
5274 : 0 : offset += 9;
5275 : 0 : break;
5276 : :
5277 : 25492 : case DW_OP_piece:
5278 : : case DW_OP_regx:
5279 : : case DW_OP_plus_uconst:
5280 : 25492 : case DW_OP_constu:;
5281 : 25492 : const unsigned char *start = data;
5282 : 25492 : uint64_t uleb;
5283 [ - + ]: 25492 : NEED (1);
5284 : 25492 : get_uleb128 (uleb, data, data + len);
5285 : 25492 : fprintf (out, "%*s[%2" PRIuMAX "] %s %" PRIu64 "\n",
5286 : : indent, "", (uintmax_t) offset, op_name, uleb);
5287 [ - + ]: 25492 : CONSUME (data - start);
5288 : 25492 : offset += 1 + (data - start);
5289 : 25492 : break;
5290 : :
5291 : 16 : case DW_OP_addrx:
5292 : : case DW_OP_GNU_addr_index:
5293 : : case DW_OP_constx:
5294 : 16 : case DW_OP_GNU_const_index:;
5295 : 16 : start = data;
5296 [ - + ]: 16 : NEED (1);
5297 : 16 : get_uleb128 (uleb, data, data + len);
5298 : 16 : fprintf (out, "%*s[%2" PRIuMAX "] %s [%" PRIu64 "] ",
5299 : : indent, "", (uintmax_t) offset, op_name, uleb);
5300 [ - + ]: 16 : CONSUME (data - start);
5301 : 16 : offset += 1 + (data - start);
5302 [ - + ]: 16 : if (get_indexed_addr (cu, uleb, &addr) != 0)
5303 : 0 : fprintf (out, "???\n");
5304 : : else
5305 : : {
5306 : 16 : print_dwarf_addr (dwflmod, 0, addr, addr, out);
5307 : 16 : fprintf (out, "\n");
5308 : : }
5309 : : break;
5310 : :
5311 : 0 : case DW_OP_bit_piece:
5312 : 0 : start = data;
5313 : 0 : uint64_t uleb2;
5314 [ # # ]: 0 : NEED (1);
5315 : 0 : get_uleb128 (uleb, data, data + len);
5316 : 0 : NEED (1);
5317 : 0 : get_uleb128 (uleb2, data, data + len);
5318 : 0 : fprintf (out, "%*s[%2" PRIuMAX "] %s %" PRIu64 ", %" PRIu64 "\n",
5319 : : indent, "", (uintmax_t) offset, op_name, uleb, uleb2);
5320 [ # # ]: 0 : CONSUME (data - start);
5321 : 0 : offset += 1 + (data - start);
5322 : 0 : break;
5323 : :
5324 : 188766 : case DW_OP_fbreg:
5325 : : case DW_OP_breg0 ... DW_OP_breg31:
5326 : : case DW_OP_consts:
5327 : 188766 : start = data;
5328 : 188766 : int64_t sleb;
5329 [ - + ]: 188766 : NEED (1);
5330 : 188766 : get_sleb128 (sleb, data, data + len);
5331 : 188766 : fprintf (out, "%*s[%2" PRIuMAX "] %s %" PRId64 "\n",
5332 : : indent, "", (uintmax_t) offset, op_name, sleb);
5333 [ - + ]: 188766 : CONSUME (data - start);
5334 : 188766 : offset += 1 + (data - start);
5335 : 188766 : break;
5336 : :
5337 : 0 : case DW_OP_bregx:
5338 : 0 : start = data;
5339 [ # # ]: 0 : NEED (1);
5340 : 0 : get_uleb128 (uleb, data, data + len);
5341 : 0 : NEED (1);
5342 : 0 : get_sleb128 (sleb, data, data + len);
5343 : 0 : fprintf (out, "%*s[%2" PRIuMAX "] %s %" PRIu64 " %" PRId64 "\n",
5344 : : indent, "", (uintmax_t) offset, op_name, uleb, sleb);
5345 [ # # ]: 0 : CONSUME (data - start);
5346 : 0 : offset += 1 + (data - start);
5347 : 0 : break;
5348 : :
5349 : 0 : case DW_OP_call2:
5350 [ # # ]: 0 : NEED (2);
5351 : 0 : fprintf (out, "%*s[%2" PRIuMAX "] %s [%6" PRIx16 "]\n",
5352 : : indent, "", (uintmax_t) offset, op_name,
5353 [ # # ]: 0 : read_2ubyte_unaligned (dbg, data));
5354 : 0 : CONSUME (2);
5355 : 0 : data += 2;
5356 : 0 : offset += 3;
5357 : 0 : break;
5358 : :
5359 : 8 : case DW_OP_call4:
5360 [ - + ]: 8 : NEED (4);
5361 : 8 : fprintf (out, "%*s[%2" PRIuMAX "] %s [%6" PRIx32 "]\n",
5362 : : indent, "", (uintmax_t) offset, op_name,
5363 [ - + ]: 8 : read_4ubyte_unaligned (dbg, data));
5364 : 8 : CONSUME (4);
5365 : 8 : data += 4;
5366 : 8 : offset += 5;
5367 : 8 : break;
5368 : :
5369 : 1120 : case DW_OP_skip:
5370 : : case DW_OP_bra:
5371 [ - + ]: 1120 : NEED (2);
5372 : 2240 : fprintf (out, "%*s[%2" PRIuMAX "] %s %" PRIuMAX "\n",
5373 : : indent, "", (uintmax_t) offset, op_name,
5374 [ - + ]: 1120 : (uintmax_t) (offset + read_2sbyte_unaligned (dbg, data) + 3));
5375 : 1120 : CONSUME (2);
5376 : 1120 : data += 2;
5377 : 1120 : offset += 3;
5378 : 1120 : break;
5379 : :
5380 : 510 : case DW_OP_implicit_value:
5381 : 510 : start = data;
5382 [ - + ]: 510 : NEED (1);
5383 : 510 : get_uleb128 (uleb, data, data + len);
5384 : 510 : fprintf (out, "%*s[%2" PRIuMAX "] %s: ",
5385 : : indent, "", (uintmax_t) offset, op_name);
5386 [ - + ]: 510 : NEED (uleb);
5387 : 510 : print_block (uleb, data, out);
5388 : 510 : data += uleb;
5389 [ - + ]: 510 : CONSUME (data - start);
5390 : 510 : offset += 1 + (data - start);
5391 : 510 : break;
5392 : :
5393 : 5912 : case DW_OP_implicit_pointer:
5394 : : case DW_OP_GNU_implicit_pointer:
5395 : : /* DIE offset operand. */
5396 : 5912 : start = data;
5397 [ - + ]: 5912 : NEED (ref_size);
5398 [ - + ]: 5912 : if (ref_size != 4 && ref_size != 8)
5399 : 0 : goto invalid; /* Cannot be used in CFA. */
5400 [ + - ]: 5912 : if (ref_size == 4)
5401 [ - + ]: 5912 : addr = read_4ubyte_unaligned (dbg, data);
5402 : : else
5403 [ # # ]: 0 : addr = read_8ubyte_unaligned (dbg, data);
5404 : 5912 : data += ref_size;
5405 : : /* Byte offset operand. */
5406 [ - + ]: 5912 : NEED (1);
5407 : 5912 : get_sleb128 (sleb, data, data + len);
5408 : :
5409 : 5912 : fprintf (out, "%*s[%2" PRIuMAX "] %s [%6" PRIxMAX "] %+" PRId64 "\n",
5410 : : indent, "", (intmax_t) offset,
5411 : : op_name, (uintmax_t) addr, sleb);
5412 [ - + ]: 5912 : CONSUME (data - start);
5413 : 5912 : offset += 1 + (data - start);
5414 : 5912 : break;
5415 : :
5416 : 62480 : case DW_OP_entry_value:
5417 : : case DW_OP_GNU_entry_value:
5418 : : /* Size plus expression block. */
5419 : 62480 : start = data;
5420 [ - + ]: 62480 : NEED (1);
5421 : 62480 : get_uleb128 (uleb, data, data + len);
5422 : 62480 : fprintf (out, "%*s[%2" PRIuMAX "] %s:\n",
5423 : : indent, "", (uintmax_t) offset, op_name);
5424 [ - + ]: 62480 : NEED (uleb);
5425 : 62480 : print_ops (dwflmod, dbg, indent + 5, indent + 5, vers,
5426 : : addrsize, offset_size, cu, uleb, data, out);
5427 : 62480 : data += uleb;
5428 [ - + ]: 62480 : CONSUME (data - start);
5429 : 62480 : offset += 1 + (data - start);
5430 : 62480 : break;
5431 : :
5432 : 0 : case DW_OP_const_type:
5433 : : case DW_OP_GNU_const_type:
5434 : : /* uleb128 CU relative DW_TAG_base_type DIE offset, 1-byte
5435 : : unsigned size plus block. */
5436 : 0 : start = data;
5437 [ # # ]: 0 : NEED (1);
5438 : 0 : get_uleb128 (uleb, data, data + len);
5439 [ # # # # ]: 0 : if (! print_unresolved_addresses && cu != NULL)
5440 : 0 : uleb += cu->start;
5441 : 0 : NEED (1);
5442 : 0 : uint8_t usize = *(uint8_t *) data++;
5443 [ # # ]: 0 : NEED (usize);
5444 : 0 : fprintf (out, "%*s[%2" PRIuMAX "] %s [%6" PRIxMAX "] ",
5445 : : indent, "", (uintmax_t) offset, op_name, uleb);
5446 : 0 : print_block (usize, data, out);
5447 : 0 : data += usize;
5448 [ # # ]: 0 : CONSUME (data - start);
5449 : 0 : offset += 1 + (data - start);
5450 : 0 : break;
5451 : :
5452 : 0 : case DW_OP_regval_type:
5453 : : case DW_OP_GNU_regval_type:
5454 : : /* uleb128 register number, uleb128 CU relative
5455 : : DW_TAG_base_type DIE offset. */
5456 : 0 : start = data;
5457 [ # # ]: 0 : NEED (1);
5458 : 0 : get_uleb128 (uleb, data, data + len);
5459 : 0 : NEED (1);
5460 : 0 : get_uleb128 (uleb2, data, data + len);
5461 [ # # # # ]: 0 : if (! print_unresolved_addresses && cu != NULL)
5462 : 0 : uleb2 += cu->start;
5463 : 0 : fprintf (out, "%*s[%2" PRIuMAX "] %s %" PRIu64 " [%6" PRIx64 "]\n",
5464 : : indent, "", (uintmax_t) offset, op_name, uleb, uleb2);
5465 [ # # ]: 0 : CONSUME (data - start);
5466 : 0 : offset += 1 + (data - start);
5467 : 0 : break;
5468 : :
5469 : 0 : case DW_OP_deref_type:
5470 : : case DW_OP_GNU_deref_type:
5471 : : /* 1-byte unsigned size of value, uleb128 CU relative
5472 : : DW_TAG_base_type DIE offset. */
5473 : 0 : start = data;
5474 [ # # ]: 0 : NEED (1);
5475 : 0 : usize = *(uint8_t *) data++;
5476 : 0 : NEED (1);
5477 : 0 : get_uleb128 (uleb, data, data + len);
5478 [ # # # # ]: 0 : if (! print_unresolved_addresses && cu != NULL)
5479 : 0 : uleb += cu->start;
5480 : 0 : fprintf (out, "%*s[%2" PRIuMAX "] %s %" PRIu8 " [%6" PRIxMAX "]\n",
5481 : : indent, "", (uintmax_t) offset,
5482 : : op_name, usize, uleb);
5483 [ # # ]: 0 : CONSUME (data - start);
5484 : 0 : offset += 1 + (data - start);
5485 : 0 : break;
5486 : :
5487 : 0 : case DW_OP_xderef_type:
5488 : : /* 1-byte unsigned size of value, uleb128 base_type DIE offset. */
5489 : 0 : start = data;
5490 [ # # ]: 0 : NEED (1);
5491 : 0 : usize = *(uint8_t *) data++;
5492 : 0 : NEED (1);
5493 : 0 : get_uleb128 (uleb, data, data + len);
5494 : 0 : fprintf (out, "%*s[%4" PRIuMAX "] %s %" PRIu8 " [%6" PRIxMAX "]\n",
5495 : : indent, "", (uintmax_t) offset,
5496 : : op_name, usize, uleb);
5497 [ # # ]: 0 : CONSUME (data - start);
5498 : 0 : offset += 1 + (data - start);
5499 : 0 : break;
5500 : :
5501 : 786 : case DW_OP_convert:
5502 : : case DW_OP_GNU_convert:
5503 : : case DW_OP_reinterpret:
5504 : : case DW_OP_GNU_reinterpret:
5505 : : /* uleb128 CU relative offset to DW_TAG_base_type, or zero
5506 : : for conversion to untyped. */
5507 : 786 : start = data;
5508 [ - + ]: 786 : NEED (1);
5509 : 786 : get_uleb128 (uleb, data, data + len);
5510 [ + + + - : 786 : if (uleb != 0 && ! print_unresolved_addresses && cu != NULL)
+ - ]
5511 : 524 : uleb += cu->start;
5512 : 786 : fprintf (out, "%*s[%2" PRIuMAX "] %s [%6" PRIxMAX "]\n",
5513 : : indent, "", (uintmax_t) offset, op_name, uleb);
5514 [ - + ]: 786 : CONSUME (data - start);
5515 : 786 : offset += 1 + (data - start);
5516 : 786 : break;
5517 : :
5518 : 8 : case DW_OP_GNU_parameter_ref:
5519 : : /* 4 byte CU relative reference to the abstract optimized away
5520 : : DW_TAG_formal_parameter. */
5521 [ - + ]: 8 : NEED (4);
5522 [ - + ]: 8 : uintmax_t param_off = (uintmax_t) read_4ubyte_unaligned (dbg, data);
5523 [ + - + - ]: 8 : if (! print_unresolved_addresses && cu != NULL)
5524 : 8 : param_off += cu->start;
5525 : 8 : fprintf (out, "%*s[%2" PRIuMAX "] %s [%6" PRIxMAX "]\n",
5526 : : indent, "", (uintmax_t) offset, op_name, param_off);
5527 : 8 : CONSUME (4);
5528 : 8 : data += 4;
5529 : 8 : offset += 5;
5530 : 8 : break;
5531 : :
5532 : : default:
5533 : : /* No Operand. */
5534 : 769424 : fprintf (out, "%*s[%2" PRIuMAX "] %s\n",
5535 : : indent, "", (uintmax_t) offset, op_name);
5536 : 769424 : ++offset;
5537 : 769424 : break;
5538 : : }
5539 : :
5540 : 1117674 : indent = indentrest;
5541 : 1117674 : continue;
5542 : :
5543 : 0 : invalid:
5544 : 0 : fprintf (out, _("%*s[%2" PRIuMAX "] %s <TRUNCATED>\n"),
5545 : : indent, "", (uintmax_t) offset, op_name);
5546 : 0 : break;
5547 : : }
5548 : : }
5549 : :
5550 : :
5551 : : /* Turn the addresses into file offsets by using the phdrs. */
5552 : : static void
5553 : 2 : find_offsets(Elf *elf, GElf_Addr main_bias, size_t n,
5554 : : GElf_Addr addrs[n], GElf_Off offs[n])
5555 : : {
5556 : 2 : size_t unsolved = n;
5557 [ + + ]: 18 : for (size_t i = 0; i < phnum; ++i) {
5558 : 16 : GElf_Phdr phdr_mem;
5559 : 16 : GElf_Phdr *phdr = gelf_getphdr(elf, i, &phdr_mem);
5560 [ + - + + : 16 : if (phdr != NULL && phdr->p_type == PT_LOAD && phdr->p_memsz > 0)
+ - ]
5561 [ + + ]: 46 : for (size_t j = 0; j < n; ++j)
5562 [ + + + + ]: 44 : if (offs[j] == 0 && addrs[j] >= phdr->p_vaddr + main_bias &&
5563 [ + - ]: 22 : addrs[j] - (phdr->p_vaddr + main_bias) < phdr->p_filesz) {
5564 : 22 : offs[j] = addrs[j] - (phdr->p_vaddr + main_bias) + phdr->p_offset;
5565 [ + + ]: 22 : if (--unsolved == 0)
5566 : : break;
5567 : : }
5568 : : }
5569 : 2 : }
5570 : :
5571 : : /* The dynamic segment (type PT_DYNAMIC), contains the .dynamic section.
5572 : : And .dynamic section contains an array of the dynamic structures.
5573 : : We use the array to get:
5574 : : DT_STRTAB: the address of the string table
5575 : : DT_SYMTAB: the address of the symbol table
5576 : : DT_STRSZ: the size, in bytes, of the string table
5577 : : ... */
5578 : : static void
5579 : 2 : get_dynscn_addrs(Elf *elf, GElf_Phdr *phdr, GElf_Addr addrs[i_max])
5580 : : {
5581 : 2 : Elf_Data *data = elf_getdata_rawchunk(
5582 : 2 : elf, phdr->p_offset, phdr->p_filesz, ELF_T_DYN);
5583 : :
5584 : 2 : int dyn_idx = 0;
5585 : 44 : for (;; ++dyn_idx) {
5586 : 46 : GElf_Dyn dyn_mem;
5587 : 46 : GElf_Dyn *dyn = gelf_getdyn(data, dyn_idx, &dyn_mem);
5588 : : /* DT_NULL Marks end of dynamic section. */
5589 [ + - + + ]: 46 : if (dyn == NULL || dyn->d_tag == DT_NULL)
5590 : : break;
5591 : :
5592 [ + - + + : 44 : switch (dyn->d_tag) {
+ + + + +
+ - + ]
5593 : 2 : case DT_SYMTAB:
5594 : 2 : addrs[i_symtab] = dyn->d_un.d_ptr;
5595 : 2 : break;
5596 : :
5597 : 0 : case DT_HASH:
5598 : 0 : addrs[i_hash] = dyn->d_un.d_ptr;
5599 : 0 : break;
5600 : :
5601 : 2 : case DT_GNU_HASH:
5602 : 2 : addrs[i_gnu_hash] = dyn->d_un.d_ptr;
5603 : 2 : break;
5604 : :
5605 : 2 : case DT_STRTAB:
5606 : 2 : addrs[i_strtab] = dyn->d_un.d_ptr;
5607 : 2 : break;
5608 : :
5609 : 2 : case DT_VERSYM:
5610 : 2 : addrs[i_versym] = dyn->d_un.d_ptr;
5611 : 2 : break;
5612 : :
5613 : 2 : case DT_VERDEF:
5614 : 2 : addrs[i_verdef] = dyn->d_un.d_ptr;
5615 : 2 : break;
5616 : :
5617 : 2 : case DT_VERDEFNUM:
5618 : 2 : addrs[i_verdefnum] = dyn->d_un.d_val;
5619 : 2 : break;
5620 : :
5621 : 2 : case DT_VERNEED:
5622 : 2 : addrs[i_verneed] = dyn->d_un.d_ptr;
5623 : 2 : break;
5624 : :
5625 : 2 : case DT_VERNEEDNUM:
5626 : 2 : addrs[i_verneednum] = dyn->d_un.d_val;
5627 : 2 : break;
5628 : :
5629 : 2 : case DT_STRSZ:
5630 : 2 : addrs[i_strsz] = dyn->d_un.d_val;
5631 : 2 : break;
5632 : :
5633 : 0 : case DT_SYMTAB_SHNDX:
5634 : 0 : addrs[i_symtab_shndx] = dyn->d_un.d_ptr;
5635 : 0 : break;
5636 : : }
5637 : : }
5638 : 2 : }
5639 : :
5640 : :
5641 : : /* Use dynamic segment to get data for the string table section. */
5642 : : static Elf_Data *
5643 : 2 : get_dynscn_strtab(Elf *elf, GElf_Phdr *phdr)
5644 : : {
5645 : 2 : Elf_Data *strtab_data;
5646 : 2 : GElf_Addr addrs[i_max] = {0,};
5647 : 2 : GElf_Off offs[i_max] = {0,};
5648 : 2 : get_dynscn_addrs(elf, phdr, addrs);
5649 : 2 : find_offsets(elf, 0, i_max, addrs, offs);
5650 : 4 : strtab_data = elf_getdata_rawchunk(
5651 : 2 : elf, offs[i_strtab], addrs[i_strsz], ELF_T_BYTE);
5652 : 2 : return strtab_data;
5653 : : }
5654 : :
5655 : :
5656 : : struct listptr
5657 : : {
5658 : : Dwarf_Off offset:(64 - 3);
5659 : : bool addr64:1;
5660 : : bool dwarf64:1;
5661 : : bool warned:1;
5662 : : struct Dwarf_CU *cu;
5663 : : unsigned int attr;
5664 : : };
5665 : :
5666 : : #define listptr_offset_size(p) ((p)->dwarf64 ? 8 : 4)
5667 : : #define listptr_address_size(p) ((p)->addr64 ? 8 : 4)
5668 : :
5669 : : static Dwarf_Addr
5670 : 5342 : cudie_base (Dwarf_Die *cudie)
5671 : : {
5672 : 5342 : Dwarf_Addr base;
5673 : : /* Find the base address of the compilation unit. It will normally
5674 : : be specified by DW_AT_low_pc. In DWARF-3 draft 4, the base
5675 : : address could be overridden by DW_AT_entry_pc. It's been
5676 : : removed, but GCC emits DW_AT_entry_pc and not DW_AT_lowpc for
5677 : : compilation units with discontinuous ranges. */
5678 [ - + ]: 5342 : if (unlikely (dwarf_lowpc (cudie, &base) != 0))
5679 : : {
5680 : 0 : Dwarf_Attribute attr_mem;
5681 [ # # ]: 0 : if (dwarf_formaddr (dwarf_attr (cudie, DW_AT_entry_pc, &attr_mem),
5682 : : &base) != 0)
5683 : 0 : base = 0;
5684 : : }
5685 : 5342 : return base;
5686 : : }
5687 : :
5688 : : static Dwarf_Addr
5689 : 5342 : listptr_base (struct listptr *p)
5690 : : {
5691 : 5342 : Dwarf_Die cu = CUDIE (p->cu);
5692 : 5342 : return cudie_base (&cu);
5693 : : }
5694 : :
5695 : : /* To store the name used in compare_listptr */
5696 : : static _Thread_local const char *sort_listptr_name;
5697 : :
5698 : : static int
5699 : 1790640 : compare_listptr (const void *a, const void *b)
5700 : : {
5701 : 1790640 : const char *name = sort_listptr_name;
5702 : 1790640 : struct listptr *p1 = (void *) a;
5703 : 1790640 : struct listptr *p2 = (void *) b;
5704 : :
5705 [ + + ]: 1790640 : if (p1->offset < p2->offset)
5706 : : return -1;
5707 [ + + ]: 175570 : if (p1->offset > p2->offset)
5708 : : return 1;
5709 : :
5710 [ + - - + ]: 8174 : if (!p1->warned && !p2->warned)
5711 : : {
5712 [ - + ]: 8174 : if (p1->addr64 != p2->addr64)
5713 : : {
5714 : 0 : p1->warned = p2->warned = true;
5715 : 0 : error (0, 0,
5716 : 0 : _("%s %#" PRIx64 " used with different address sizes"),
5717 : : name, (uint64_t) p1->offset);
5718 : : }
5719 [ - + ]: 8174 : if (p1->dwarf64 != p2->dwarf64)
5720 : : {
5721 : 0 : p1->warned = p2->warned = true;
5722 : 0 : error (0, 0,
5723 : 0 : _("%s %#" PRIx64 " used with different offset sizes"),
5724 : 0 : name, (uint64_t) p1->offset);
5725 : : }
5726 : :
5727 : : /* Note: CUs can share the same listptr_base. So we don't check
5728 : : (listptr_base (p1) != listptr_base (p2)) */
5729 : :
5730 [ + - ]: 8174 : if (p1->attr != p2 ->attr)
5731 : : {
5732 : 0 : p1->warned = p2->warned = true;
5733 : 0 : error (0, 0,
5734 : 0 : _("%s %#" PRIx64
5735 : : " used with different attribute %s and %s"),
5736 : 0 : name, (uint64_t) p1->offset, dwarf_attr_name (p1->attr),
5737 : : dwarf_attr_name (p2->attr));
5738 : : }
5739 : : }
5740 : :
5741 : : return 0;
5742 : : }
5743 : :
5744 : : struct listptr_table
5745 : : {
5746 : : size_t n;
5747 : : size_t alloc;
5748 : : struct listptr *table;
5749 : : };
5750 : :
5751 : : static struct listptr_table known_locsptr;
5752 : : static struct listptr_table known_loclistsptr;
5753 : : static struct listptr_table known_rangelistptr;
5754 : : static struct listptr_table known_rnglistptr;
5755 : : static struct listptr_table known_addrbases;
5756 : : static struct listptr_table known_stroffbases;
5757 : :
5758 : : static void
5759 : 502 : reset_listptr (struct listptr_table *table)
5760 : : {
5761 : 502 : free (table->table);
5762 : 502 : table->table = NULL;
5763 : 502 : table->n = table->alloc = 0;
5764 : : }
5765 : :
5766 : : /* Returns false if offset doesn't fit. See struct listptr. */
5767 : : static bool
5768 : 257896 : notice_listptr (enum section_e section, struct listptr_table *table,
5769 : : uint_fast8_t address_size, uint_fast8_t offset_size,
5770 : : struct Dwarf_CU *cu, Dwarf_Off offset, unsigned int attr)
5771 : : {
5772 [ + + ]: 257896 : if (print_debug_sections & section)
5773 : : {
5774 [ + + ]: 257644 : if (table->n == table->alloc)
5775 : : {
5776 [ + + ]: 410 : if (table->alloc == 0)
5777 : 174 : table->alloc = 128;
5778 : : else
5779 : 236 : table->alloc *= 2;
5780 : 410 : table->table = xrealloc (table->table,
5781 : 410 : table->alloc * sizeof table->table[0]);
5782 : : }
5783 : :
5784 : 257644 : struct listptr *p = &table->table[table->n++];
5785 : :
5786 : 257644 : *p = (struct listptr)
5787 : : {
5788 : 257644 : .addr64 = address_size == 8,
5789 : 257644 : .dwarf64 = offset_size == 8,
5790 : : .offset = offset,
5791 : : .cu = cu,
5792 : : .attr = attr
5793 : : };
5794 : :
5795 [ + - ]: 257644 : if (p->offset != offset)
5796 : : {
5797 : 0 : table->n--;
5798 : 0 : return false;
5799 : : }
5800 : : }
5801 : : return true;
5802 : : }
5803 : :
5804 : : static void
5805 : 182 : sort_listptr (struct listptr_table *table, const char *name)
5806 : : {
5807 [ + + ]: 182 : if (table->n > 0)
5808 : : {
5809 : 174 : sort_listptr_name = name;
5810 : 174 : qsort (table->table, table->n, sizeof table->table[0],
5811 : : &compare_listptr);
5812 : : }
5813 : 182 : }
5814 : :
5815 : : static bool
5816 : 208 : skip_listptr_hole (struct listptr_table *table, size_t *idxp,
5817 : : uint_fast8_t *address_sizep, uint_fast8_t *offset_sizep,
5818 : : Dwarf_Addr *base, struct Dwarf_CU **cu, ptrdiff_t offset,
5819 : : unsigned char **readp, unsigned char *endp,
5820 : : unsigned int *attr)
5821 : : {
5822 [ - + ]: 208 : if (table->n == 0)
5823 : : return false;
5824 : :
5825 [ + - + + ]: 336 : while (*idxp < table->n && table->table[*idxp].offset < (Dwarf_Off) offset)
5826 : 128 : ++*idxp;
5827 : :
5828 : 208 : struct listptr *p = &table->table[*idxp];
5829 : :
5830 [ + - ]: 208 : if (*idxp == table->n
5831 [ - + ]: 208 : || p->offset >= (Dwarf_Off) (endp - *readp + offset))
5832 : : {
5833 : 0 : *readp = endp;
5834 : 0 : printf (_(" [%6tx] <UNUSED GARBAGE IN REST OF SECTION>\n"),
5835 : : offset);
5836 : 0 : return true;
5837 : : }
5838 : :
5839 [ - + ]: 208 : if (p->offset != (Dwarf_Off) offset)
5840 : : {
5841 : 0 : *readp += p->offset - offset;
5842 : 0 : printf (_(" [%6tx] <UNUSED GARBAGE> ... %" PRIu64 " bytes ...\n"),
5843 : : offset, (Dwarf_Off) p->offset - offset);
5844 : 0 : return true;
5845 : : }
5846 : :
5847 [ + - ]: 208 : if (address_sizep != NULL)
5848 [ + + ]: 224 : *address_sizep = listptr_address_size (p);
5849 [ + + ]: 208 : if (offset_sizep != NULL)
5850 [ + - ]: 296 : *offset_sizep = listptr_offset_size (p);
5851 [ + - ]: 208 : if (base != NULL)
5852 : 208 : *base = listptr_base (p);
5853 [ + - ]: 208 : if (cu != NULL)
5854 : 208 : *cu = p->cu;
5855 [ + + ]: 208 : if (attr != NULL)
5856 : 148 : *attr = p->attr;
5857 : :
5858 : : return false;
5859 : : }
5860 : :
5861 : : static Dwarf_Off
5862 : 109918 : next_listptr_offset (struct listptr_table *table, size_t *idxp, Dwarf_Off off)
5863 : : {
5864 : : /* Note that multiple attributes could in theory point to the same loclist
5865 : : offset, so make sure we pick one that is bigger than the current one.
5866 : : The table is sorted on offset. */
5867 [ + - ]: 109918 : if (*idxp < table->n)
5868 : : {
5869 [ + - ]: 218660 : while (++*idxp < table->n)
5870 : : {
5871 : 218660 : Dwarf_Off next = table->table[*idxp].offset;
5872 [ + + ]: 218660 : if (next > off)
5873 : : return next;
5874 : : }
5875 : : }
5876 : : return 0;
5877 : : }
5878 : :
5879 : : /* Returns the listptr associated with the given index, or NULL. */
5880 : : static struct listptr *
5881 : 1531880 : get_listptr (struct listptr_table *table, size_t idx)
5882 : : {
5883 : 1531880 : if (idx >= table->n)
5884 : : return NULL;
5885 : 36 : return &table->table[idx];
5886 : : }
5887 : :
5888 : : /* Returns the next index, base address and CU associated with the
5889 : : list unit offsets. If there is none false is returned, otherwise
5890 : : true. Assumes the table has been sorted. */
5891 : : static bool
5892 : 5134 : listptr_cu (struct listptr_table *table, size_t *idxp,
5893 : : Dwarf_Off start, Dwarf_Off end,
5894 : : Dwarf_Addr *base, struct Dwarf_CU **cu)
5895 : : {
5896 : 5134 : while (*idxp < table->n
5897 [ + - + + ]: 41092 : && table->table[*idxp].offset < start)
5898 : 35958 : ++*idxp;
5899 : :
5900 [ + - ]: 5134 : if (*idxp < table->n
5901 [ + - ]: 5134 : && table->table[*idxp].offset >= start
5902 [ + - ]: 5134 : && table->table[*idxp].offset < end)
5903 : : {
5904 : 5134 : struct listptr *p = &table->table[*idxp];
5905 : 5134 : *base = listptr_base (p);
5906 : 5134 : *cu = p->cu;
5907 : 5134 : return true;
5908 : : }
5909 : :
5910 : : return false;
5911 : : }
5912 : :
5913 : : /* Returns the next index with the current CU for the given attribute.
5914 : : If there is none false is returned, otherwise true. Assumes the
5915 : : table has been sorted. */
5916 : : static bool
5917 : 765582 : listptr_attr (struct listptr_table *table, size_t idxp,
5918 : : Dwarf_Off offset, unsigned int attr)
5919 : : {
5920 : 1531832 : struct listptr *listptr;
5921 : 1531832 : do
5922 : : {
5923 [ + + ]: 1531832 : listptr = get_listptr (table, idxp);
5924 [ + - ]: 1531592 : if (listptr == NULL)
5925 : : return false;
5926 : :
5927 [ + + + + ]: 1531592 : if (listptr->offset == offset && listptr->attr == attr)
5928 : : return true;
5929 : :
5930 : 1421674 : idxp++;
5931 : : }
5932 [ + + ]: 1421674 : while (listptr->offset <= offset);
5933 : :
5934 : : return false;
5935 : : }
5936 : :
5937 : : static void
5938 : 88 : print_debug_abbrev_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
5939 : : Ebl *ebl, GElf_Ehdr *ehdr __attribute__ ((unused)),
5940 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg,
5941 : : FILE *out)
5942 : : {
5943 : 88 : Elf_Data *elf_data = get_debug_elf_data (dbg, ebl, IDX_debug_abbrev, scn);
5944 [ + - ]: 88 : if (elf_data == NULL)
5945 : : return;
5946 : :
5947 : 88 : const size_t sh_size = elf_data->d_size;
5948 : :
5949 : 88 : fprintf (out, _("\nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"
5950 : : " [ Code]\n"),
5951 : : elf_ndxscn (scn), section_name (ebl, shdr),
5952 : 88 : (uint64_t) shdr->sh_offset);
5953 : :
5954 : 88 : Dwarf_Off offset = 0;
5955 [ + + ]: 3682 : while (offset < sh_size)
5956 : : {
5957 : 3594 : fprintf (out, _("\nAbbreviation section at offset %" PRIu64 ":\n"),
5958 : : offset);
5959 : :
5960 : 332758 : while (1)
5961 : 164582 : {
5962 : 168176 : size_t length;
5963 : 168176 : Dwarf_Abbrev abbrev;
5964 : :
5965 : 168176 : int res = dwarf_offabbrev (dbg, offset, &length, &abbrev);
5966 [ + + ]: 168176 : if (res != 0)
5967 : : {
5968 [ - + ]: 3594 : if (unlikely (res < 0))
5969 : : {
5970 : 0 : fprintf (out, _("\
5971 : : *** error while reading abbreviation: %s\n"),
5972 : : dwarf_errmsg (-1));
5973 : 0 : return;
5974 : : }
5975 : :
5976 : : /* This is the NUL byte at the end of the section. */
5977 : 3594 : ++offset;
5978 : 3594 : break;
5979 : : }
5980 : :
5981 : : /* We know these calls can never fail. */
5982 : 164582 : unsigned int code = dwarf_getabbrevcode (&abbrev);
5983 : 164582 : unsigned int tag = dwarf_getabbrevtag (&abbrev);
5984 : 164582 : int has_children = dwarf_abbrevhaschildren (&abbrev);
5985 : :
5986 [ + + ]: 164582 : fprintf (out, " [%5u] %s: %" PRId64
5987 : : ", %s: %s, %s: %s\n",
5988 : : code, offset_str, (int64_t) offset,
5989 : : children_str, has_children ? yes_str : no_str,
5990 : : tag_str, dwarf_tag_name (tag));
5991 : :
5992 : 164582 : size_t cnt = 0;
5993 : 164582 : unsigned int name;
5994 : 164582 : unsigned int form;
5995 : 164582 : Dwarf_Sword data;
5996 : 164582 : Dwarf_Off enoffset;
5997 : 164582 : while (dwarf_getabbrevattr_data (&abbrev, cnt, &name, &form,
5998 [ + + ]: 943770 : &data, &enoffset) == 0)
5999 : : {
6000 : 779188 : fprintf (out, " attr: %s, form: %s",
6001 : : dwarf_attr_name (name), dwarf_form_name (form));
6002 [ + + ]: 779188 : if (form == DW_FORM_implicit_const)
6003 : 55010 : fprintf (out, " (%" PRId64 ")", data);
6004 : 779188 : fprintf (out, ", offset: %#" PRIx64 "\n", (uint64_t) enoffset);
6005 : 779188 : ++cnt;
6006 : : }
6007 : :
6008 : 164582 : offset += length;
6009 : : }
6010 : : }
6011 : : }
6012 : :
6013 : :
6014 : : static void
6015 : 8 : print_debug_addr_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
6016 : : Ebl *ebl, GElf_Ehdr *ehdr,
6017 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg,
6018 : : FILE *out)
6019 : : {
6020 : 8 : Elf_Data *data = get_debug_elf_data (dbg, ebl, IDX_debug_addr, scn);
6021 [ + - ]: 8 : if (data == NULL)
6022 : : return;
6023 : :
6024 : 8 : fprintf (out, _("\
6025 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
6026 : : elf_ndxscn (scn), section_name (ebl, shdr),
6027 : 8 : (uint64_t) shdr->sh_offset);
6028 : :
6029 [ + - ]: 8 : if (shdr->sh_size == 0)
6030 : : return;
6031 : :
6032 : 8 : size_t idx = 0;
6033 : 8 : sort_listptr (&known_addrbases, "addr_base");
6034 : :
6035 : 8 : const unsigned char *start = (const unsigned char *) data->d_buf;
6036 : 8 : const unsigned char *readp = start;
6037 : 8 : const unsigned char *readendp = ((const unsigned char *) data->d_buf
6038 : 8 : + data->d_size);
6039 : :
6040 [ + + ]: 24 : while (readp < readendp)
6041 : : {
6042 : : /* We cannot really know whether or not there is an header. The
6043 : : DebugFission extension to DWARF4 doesn't add one. The DWARF5
6044 : : .debug_addr variant does. Whether or not we have an header,
6045 : : DW_AT_[GNU_]addr_base points at "index 0". So if the current
6046 : : offset equals the CU addr_base then we can just start
6047 : : printing addresses. If there is no CU with an exact match
6048 : : then we'll try to parse the header first. */
6049 : 16 : Dwarf_Off off = (Dwarf_Off) (readp
6050 : 16 : - (const unsigned char *) data->d_buf);
6051 : :
6052 : 16 : fprintf (out, "Table at offset %" PRIx64 " ", off);
6053 : :
6054 : : /* Find the first CU that could plausibly be associated with
6055 : : this address base offset. Skip CUs that point their addr_base
6056 : : before this table. */
6057 [ + - ]: 16 : struct listptr *listptr = get_listptr (&known_addrbases, idx);
6058 [ + + ]: 24 : while (listptr != NULL && listptr->offset < off)
6059 [ + - + - ]: 32 : listptr = get_listptr (&known_addrbases, ++idx);
6060 : :
6061 : 16 : const unsigned char *next_unitp;
6062 : :
6063 : 16 : uint64_t unit_length;
6064 : 16 : uint16_t version;
6065 : 16 : uint8_t address_size;
6066 : 16 : uint8_t segment_size;
6067 [ - + ]: 16 : if (listptr == NULL)
6068 : : {
6069 : 0 : error (0, 0, "Warning: No CU references .debug_addr after %" PRIx64,
6070 : : off);
6071 : :
6072 : : /* We will have to assume it is just addresses to the end... */
6073 [ # # ]: 0 : address_size = ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 4 : 8;
6074 : 0 : next_unitp = readendp;
6075 : 0 : fprintf (out, "Unknown CU:\n");
6076 : : }
6077 : : else
6078 : : {
6079 : 16 : Dwarf_Die cudie;
6080 [ - + ]: 16 : if (dwarf_cu_die (listptr->cu, &cudie,
6081 : : NULL, NULL, NULL, NULL,
6082 : : NULL, NULL) == NULL)
6083 : 0 : fprintf (out, "Unknown CU (%s):\n", dwarf_errmsg (-1));
6084 : : else
6085 : 16 : fprintf (out, "for CU [%6" PRIx64 "]:\n", dwarf_dieoffset (&cudie));
6086 : :
6087 [ + + ]: 16 : if (listptr->offset == off)
6088 : : {
6089 [ - + ]: 4 : address_size = listptr_address_size (listptr);
6090 : 4 : segment_size = 0;
6091 : 4 : version = 4;
6092 : :
6093 : : /* The addresses start here, but where do they end? */
6094 [ + + ]: 4 : listptr = get_listptr (&known_addrbases, idx + 1);
6095 [ - + ]: 2 : if (listptr == NULL)
6096 : : next_unitp = readendp;
6097 [ + - ]: 2 : else if (listptr->cu->version < 5)
6098 : : {
6099 : 2 : next_unitp = start + listptr->offset;
6100 [ + - - + ]: 2 : if (listptr->offset < off || listptr->offset > data->d_size)
6101 : : {
6102 : 0 : error (0, 0,
6103 : : "Warning: Bad address base for next unit at %"
6104 : : PRIx64, off);
6105 : 0 : next_unitp = readendp;
6106 : : }
6107 : : }
6108 : : else
6109 : : {
6110 : : /* Tricky, we don't have a header for this unit, but
6111 : : there is one for the next. We will have to
6112 : : "guess" how big it is and subtract it from the
6113 : : offset (because that points after the header). */
6114 [ # # ]: 0 : unsigned int offset_size = listptr_offset_size (listptr);
6115 : 0 : Dwarf_Off next_off = (listptr->offset
6116 : 0 : - (offset_size == 4 ? 4 : 12) /* len */
6117 : : - 2 /* version */
6118 : : - 1 /* address size */
6119 : 0 : - 1); /* segment selector size */
6120 : 0 : next_unitp = start + next_off;
6121 [ # # # # ]: 0 : if (next_off < off || next_off > data->d_size)
6122 : : {
6123 : 0 : error (0, 0,
6124 : : "Warning: Couldn't calculate .debug_addr "
6125 : : " unit length at %" PRIx64, off);
6126 : 0 : next_unitp = readendp;
6127 : : }
6128 : : }
6129 : 4 : unit_length = (uint64_t) (next_unitp - readp);
6130 : :
6131 : : /* Pretend we have a header. */
6132 : 4 : fprintf (out, "\n");
6133 : 4 : fprintf (out, _(" Length: %8" PRIu64 "\n"),
6134 : : unit_length);
6135 : 4 : fprintf (out, _(" DWARF version: %8" PRIu16 "\n"), version);
6136 : 4 : fprintf (out, _(" Address size: %8" PRIu64 "\n"),
6137 : : (uint64_t) address_size);
6138 : 4 : fprintf (out, _(" Segment size: %8" PRIu64 "\n"),
6139 : : (uint64_t) segment_size);
6140 : 4 : fprintf (out, "\n");
6141 : : }
6142 : : else
6143 : : {
6144 : : /* OK, we have to parse an header first. */
6145 [ - + ]: 12 : unit_length = read_4ubyte_unaligned_inc (dbg, readp);
6146 [ - + ]: 12 : if (unlikely (unit_length == 0xffffffff))
6147 : : {
6148 [ # # ]: 0 : if (unlikely (readp > readendp - 8))
6149 : : {
6150 : 0 : invalid_data:
6151 : 0 : error (0, 0, "Invalid data");
6152 : 0 : return;
6153 : : }
6154 [ # # ]: 0 : unit_length = read_8ubyte_unaligned_inc (dbg, readp);
6155 : : }
6156 : 12 : fprintf (out, "\n");
6157 : 12 : fprintf (out, _(" Length: %8" PRIu64 "\n"),
6158 : : unit_length);
6159 : :
6160 : : /* We need at least 2-bytes (version) + 1-byte
6161 : : (addr_size) + 1-byte (segment_size) = 4 bytes to
6162 : : complete the header. And this unit cannot go beyond
6163 : : the section data. */
6164 [ + - ]: 12 : if (readp > readendp - 4
6165 [ + - ]: 12 : || unit_length < 4
6166 [ - + ]: 12 : || unit_length > (uint64_t) (readendp - readp))
6167 : 0 : goto invalid_data;
6168 : :
6169 : 12 : next_unitp = readp + unit_length;
6170 : :
6171 [ - + ]: 12 : version = read_2ubyte_unaligned_inc (dbg, readp);
6172 : 12 : fprintf (out, _(" DWARF version: %8" PRIu16 "\n"), version);
6173 : :
6174 [ - + ]: 12 : if (version != 5)
6175 : : {
6176 : 0 : error (0, 0, _("Unknown version"));
6177 : 0 : goto next_unit;
6178 : : }
6179 : :
6180 : 12 : address_size = *readp++;
6181 : 12 : fprintf (out, _(" Address size: %8" PRIu64 "\n"),
6182 : : (uint64_t) address_size);
6183 : :
6184 [ - + ]: 12 : if (address_size != 4 && address_size != 8)
6185 : : {
6186 : 0 : error (0, 0, _("unsupported address size"));
6187 : 0 : goto next_unit;
6188 : : }
6189 : :
6190 : 12 : segment_size = *readp++;
6191 : 12 : fprintf (out, _(" Segment size: %8" PRIu64 "\n"),
6192 : : (uint64_t) segment_size);
6193 : 12 : fprintf (out, "\n");
6194 : :
6195 [ - + ]: 12 : if (segment_size != 0)
6196 : : {
6197 : 0 : error (0, 0, _("unsupported segment size"));
6198 : 0 : goto next_unit;
6199 : : }
6200 : :
6201 [ - + ]: 12 : if (listptr->offset != (Dwarf_Off) (readp - start))
6202 : : {
6203 : 0 : error (0, 0, "Address index doesn't start after header");
6204 : 0 : goto next_unit;
6205 : : }
6206 : : }
6207 : : }
6208 : :
6209 : 16 : int digits = 1;
6210 : 16 : size_t addresses = (next_unitp - readp) / address_size;
6211 [ + + ]: 24 : while (addresses >= 10)
6212 : : {
6213 : 8 : ++digits;
6214 : 8 : addresses /= 10;
6215 : : }
6216 : :
6217 : 16 : unsigned int uidx = 0;
6218 : 16 : size_t index_offset = readp - (const unsigned char *) data->d_buf;
6219 : 16 : fprintf (out, " Addresses start at offset 0x%zx:\n", index_offset);
6220 [ + + ]: 188 : while (readp <= next_unitp - address_size)
6221 : : {
6222 [ - + + + : 172 : Dwarf_Addr addr = read_addr_unaligned_inc (address_size, dbg,
- + - + ]
6223 : : readp);
6224 : 172 : fprintf (out, " [%*u] ", digits, uidx++);
6225 : 172 : print_dwarf_addr (dwflmod, address_size, addr, addr, out);
6226 : 172 : fprintf (out, "\n");
6227 : : }
6228 : 16 : fprintf (out, "\n");
6229 : :
6230 [ + - ]: 16 : if (readp != next_unitp)
6231 : 0 : error (0, 0, "extra %zd bytes at end of unit",
6232 : 0 : (size_t) (next_unitp - readp));
6233 : :
6234 : 16 : next_unit:
6235 : : readp = next_unitp;
6236 : : }
6237 : : }
6238 : :
6239 : : /* Print content of DWARF .debug_aranges section. We fortunately do
6240 : : not have to know a bit about the structure of the section, libdwarf
6241 : : takes care of it. */
6242 : : static void
6243 : 2 : print_decoded_aranges_section (Ebl *ebl, GElf_Ehdr *ehdr, Elf_Scn *scn,
6244 : : GElf_Shdr *shdr, Dwarf *dbg, FILE *out)
6245 : : {
6246 : 2 : Dwarf_Aranges *aranges;
6247 : 2 : size_t cnt;
6248 [ - + ]: 2 : if (unlikely (dwarf_getaranges (dbg, &aranges, &cnt) != 0))
6249 : : {
6250 : 0 : error (0, 0, _("cannot get .debug_aranges content: %s"),
6251 : : dwarf_errmsg (-1));
6252 : 0 : return;
6253 : : }
6254 : :
6255 : 2 : GElf_Shdr glink_mem;
6256 : 2 : GElf_Shdr *glink;
6257 : 2 : glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link), &glink_mem);
6258 [ - + ]: 2 : if (glink == NULL)
6259 : : {
6260 : 0 : error (0, 0, _("invalid sh_link value in section %zu"),
6261 : : elf_ndxscn (scn));
6262 : 0 : return;
6263 : : }
6264 : :
6265 : 2 : fprintf (out, ngettext ("\
6266 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 " contains %zu entry:\n",
6267 : : "\
6268 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 " contains %zu entries:\n",
6269 : : cnt),
6270 : : elf_ndxscn (scn), section_name (ebl, shdr),
6271 : 2 : (uint64_t) shdr->sh_offset, cnt);
6272 : :
6273 : : /* Compute floor(log16(cnt)). */
6274 : 2 : size_t tmp = cnt;
6275 : 2 : int digits = 1;
6276 [ - + ]: 2 : while (tmp >= 16)
6277 : : {
6278 : 0 : ++digits;
6279 : 0 : tmp >>= 4;
6280 : : }
6281 : :
6282 [ + + ]: 12 : for (size_t n = 0; n < cnt; ++n)
6283 : : {
6284 : 10 : Dwarf_Arange *runp = dwarf_onearange (aranges, n);
6285 [ - + ]: 10 : if (unlikely (runp == NULL))
6286 : : {
6287 : 0 : fprintf (out, "cannot get arange %zu: %s\n", n, dwarf_errmsg (-1));
6288 : 0 : return;
6289 : : }
6290 : :
6291 : 10 : Dwarf_Addr start;
6292 : 10 : Dwarf_Word length;
6293 : 10 : Dwarf_Off offset;
6294 : :
6295 [ - + ]: 10 : if (unlikely (dwarf_getarangeinfo (runp, &start, &length, &offset) != 0))
6296 : 0 : fprintf (out, _(" [%*zu] ???\n"), digits, n);
6297 : : else
6298 : 10 : fprintf (out, _(" [%*zu] start: %0#*" PRIx64
6299 : : ", length: %5" PRIu64 ", CU DIE offset: %6"
6300 : : PRId64 "\n"),
6301 [ - + ]: 10 : digits, n, ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 10 : 18,
6302 : : (uint64_t) start, (uint64_t) length, (int64_t) offset);
6303 : : }
6304 : : }
6305 : :
6306 : :
6307 : : /* Print content of DWARF .debug_aranges section. */
6308 : : static void
6309 : 92 : print_debug_aranges_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
6310 : : Ebl *ebl, GElf_Ehdr *ehdr, Elf_Scn *scn,
6311 : : GElf_Shdr *shdr, Dwarf *dbg, FILE *out)
6312 : : {
6313 [ + + ]: 92 : if (decodedaranges)
6314 : : {
6315 : 2 : print_decoded_aranges_section (ebl, ehdr, scn, shdr, dbg, out);
6316 : 2 : return;
6317 : : }
6318 : :
6319 : 90 : Elf_Data *data = get_debug_elf_data (dbg, ebl, IDX_debug_aranges, scn);
6320 [ + - ]: 90 : if (data == NULL)
6321 : : return;
6322 : :
6323 : 90 : fprintf (out, _("\
6324 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
6325 : : elf_ndxscn (scn), section_name (ebl, shdr),
6326 : 90 : (uint64_t) shdr->sh_offset);
6327 : :
6328 : 90 : const unsigned char *readp = data->d_buf;
6329 : 90 : const unsigned char *readendp = readp + data->d_size;
6330 : :
6331 [ + + ]: 3666 : while (readp < readendp)
6332 : : {
6333 : 3576 : const unsigned char *hdrstart = readp;
6334 : 3576 : size_t start_offset = hdrstart - (const unsigned char *) data->d_buf;
6335 : :
6336 : 3576 : fprintf (out, "\n%s %zu:\n", table_offset_lower_str, start_offset);
6337 [ - + ]: 3576 : if (readp + 4 > readendp)
6338 : : {
6339 : 0 : invalid_data:
6340 : 0 : error (0, 0, _("invalid data in section [%zu] '%s'"),
6341 : : elf_ndxscn (scn), section_name (ebl, shdr));
6342 : 0 : return;
6343 : : }
6344 : :
6345 [ + + ]: 3576 : Dwarf_Word length = read_4ubyte_unaligned_inc (dbg, readp);
6346 : 3576 : unsigned int length_bytes = 4;
6347 [ - + ]: 3576 : if (length == DWARF3_LENGTH_64_BIT)
6348 : : {
6349 [ # # ]: 0 : if (readp + 8 > readendp)
6350 : 0 : goto invalid_data;
6351 [ # # ]: 0 : length = read_8ubyte_unaligned_inc (dbg, readp);
6352 : 0 : length_bytes = 8;
6353 : : }
6354 : :
6355 : 3576 : const unsigned char *nexthdr = readp + length;
6356 : 3576 : fprintf (out, "\n %s: %6" PRIu64 "\n",
6357 : : length_str, (uint64_t) length);
6358 : :
6359 [ - + ]: 3576 : if (unlikely (length > (size_t) (readendp - readp)))
6360 : 0 : goto invalid_data;
6361 : :
6362 [ - + ]: 3576 : if (length == 0)
6363 : 0 : continue;
6364 : :
6365 [ - + ]: 3576 : if (readp + 2 > readendp)
6366 : 0 : goto invalid_data;
6367 [ + + ]: 3576 : uint_fast16_t version = read_2ubyte_unaligned_inc (dbg, readp);
6368 : 3576 : fprintf (out, " %s: %6" PRIuFAST16 "\n",
6369 : : dwarf_ver_str, version);
6370 [ - + ]: 3576 : if (version != 2)
6371 : : {
6372 : 0 : error (0, 0, _("unsupported aranges version"));
6373 : 0 : goto next_table;
6374 : : }
6375 : :
6376 : 3576 : Dwarf_Word offset;
6377 [ - + ]: 3576 : if (readp + length_bytes > readendp)
6378 : 0 : goto invalid_data;
6379 [ - + ]: 3576 : if (length_bytes == 8)
6380 [ # # ]: 0 : offset = read_8ubyte_unaligned_inc (dbg, readp);
6381 : : else
6382 [ + + ]: 3576 : offset = read_4ubyte_unaligned_inc (dbg, readp);
6383 : 3576 : fprintf (out, " %s: %6" PRIx64 "\n",
6384 : : cu_offset_str, (uint64_t) offset);
6385 : :
6386 [ - + ]: 3576 : if (readp + 1 > readendp)
6387 : 0 : goto invalid_data;
6388 : 3576 : unsigned int address_size = *readp++;
6389 : 3576 : fprintf (out, " %s: %6" PRIu64 "\n",
6390 : : addr_size_str, (uint64_t) address_size);
6391 [ - + ]: 3576 : if (address_size != 4 && address_size != 8)
6392 : : {
6393 : 0 : error (0, 0, _("unsupported address size"));
6394 : 0 : goto next_table;
6395 : : }
6396 : :
6397 [ - + ]: 3576 : if (readp + 1 > readendp)
6398 : 0 : goto invalid_data;
6399 : 3576 : unsigned int segment_size = *readp++;
6400 : 3576 : fprintf (out, " %s: %6" PRIu64 "\n\n",
6401 : : seg_size_str, (uint64_t) segment_size);
6402 [ - + - - ]: 3576 : if (segment_size != 0 && segment_size != 4 && segment_size != 8)
6403 : : {
6404 : 0 : error (0, 0, _("unsupported segment size"));
6405 : 0 : goto next_table;
6406 : : }
6407 : :
6408 : : /* Round the address to the next multiple of 2*address_size. */
6409 : 3576 : readp += ((2 * address_size - ((readp - hdrstart) % (2 * address_size)))
6410 : 3576 : % (2 * address_size));
6411 : :
6412 [ + - ]: 9168 : while (readp < nexthdr)
6413 : : {
6414 : 9168 : Dwarf_Word range_address;
6415 : 9168 : Dwarf_Word range_length;
6416 : 9168 : Dwarf_Word segment = 0;
6417 [ - + ]: 9168 : if (readp + 2 * address_size + segment_size > readendp)
6418 : 0 : goto invalid_data;
6419 [ + + ]: 9168 : if (address_size == 4)
6420 : : {
6421 [ + + ]: 92 : range_address = read_4ubyte_unaligned_inc (dbg, readp);
6422 [ + + ]: 92 : range_length = read_4ubyte_unaligned_inc (dbg, readp);
6423 : : }
6424 : : else
6425 : : {
6426 [ + + ]: 9076 : range_address = read_8ubyte_unaligned_inc (dbg, readp);
6427 [ + + ]: 9076 : range_length = read_8ubyte_unaligned_inc (dbg, readp);
6428 : : }
6429 : :
6430 [ - + ]: 9168 : if (segment_size == 4)
6431 [ # # ]: 0 : segment = read_4ubyte_unaligned_inc (dbg, readp);
6432 [ - + ]: 9168 : else if (segment_size == 8)
6433 [ # # ]: 0 : segment = read_8ubyte_unaligned_inc (dbg, readp);
6434 : :
6435 [ + + ]: 9168 : if (range_address == 0 && range_length == 0 && segment == 0)
6436 : : break;
6437 : :
6438 : 5592 : fprintf (out, " ");
6439 : 5592 : print_dwarf_addr (dwflmod, address_size, range_address,
6440 : : range_address, out);
6441 : 5592 : fprintf (out, "..");
6442 : 5592 : print_dwarf_addr (dwflmod, address_size,
6443 : 5592 : range_address + range_length - 1,
6444 : : range_length, out);
6445 [ - + ]: 5592 : if (segment_size != 0)
6446 : 0 : fprintf (out, " (%" PRIx64 ")\n", (uint64_t) segment);
6447 : : else
6448 : 5592 : fprintf (out, "\n");
6449 : : }
6450 : :
6451 : 3576 : next_table:
6452 [ - + ]: 3576 : if (readp != nexthdr)
6453 : : {
6454 : 0 : size_t padding = nexthdr - readp;
6455 : 0 : fprintf (out, _(" %zu padding bytes\n"), padding);
6456 : 0 : readp = nexthdr;
6457 : : }
6458 : : }
6459 : : }
6460 : :
6461 : :
6462 : : static bool is_split_dwarf (Dwarf *dbg, uint64_t *id, Dwarf_CU **split_cu);
6463 : :
6464 : : /* Returns true and sets cu and cu_base if the given Dwarf is a split
6465 : : DWARF (.dwo) file. */
6466 : : static bool
6467 : 0 : split_dwarf_cu_base (Dwarf *dbg, Dwarf_CU **cu, Dwarf_Addr *cu_base)
6468 : : {
6469 : 0 : uint64_t id;
6470 [ # # ]: 0 : if (is_split_dwarf (dbg, &id, cu))
6471 : : {
6472 : 0 : Dwarf_Die cudie;
6473 [ # # ]: 0 : if (dwarf_cu_info (*cu, NULL, NULL, &cudie, NULL, NULL, NULL, NULL) == 0)
6474 : : {
6475 : 0 : *cu_base = cudie_base (&cudie);
6476 : 0 : return true;
6477 : : }
6478 : : }
6479 : : return false;
6480 : : }
6481 : :
6482 : : /* Print content of DWARF .debug_rnglists section. */
6483 : : static void
6484 : 38 : print_debug_rnglists_section (Dwfl_Module *dwflmod,
6485 : : Ebl *ebl,
6486 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
6487 : : Elf_Scn *scn, GElf_Shdr *shdr,
6488 : : Dwarf *dbg __attribute__((unused)),
6489 : : FILE *out)
6490 : : {
6491 : 38 : Elf_Data *data = get_debug_elf_data (dbg, ebl, IDX_debug_rnglists, scn);
6492 [ + - ]: 38 : if (data == NULL)
6493 : 0 : return;
6494 : :
6495 : 38 : fprintf (out, _("\
6496 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
6497 : : elf_ndxscn (scn), section_name (ebl, shdr),
6498 : 38 : (uint64_t) shdr->sh_offset);
6499 : :
6500 : : /* For the listptr to get the base address/CU. */
6501 : 38 : sort_listptr (&known_rnglistptr, "rnglistptr");
6502 : 38 : size_t listptr_idx = 0;
6503 : :
6504 : 38 : const unsigned char *readp = data->d_buf;
6505 : 38 : const unsigned char *const dataend = ((unsigned char *) data->d_buf
6506 : 38 : + data->d_size);
6507 [ + + ]: 2080 : while (readp < dataend)
6508 : : {
6509 [ - + ]: 2042 : if (unlikely (readp > dataend - 4))
6510 : : {
6511 : 0 : invalid_data:
6512 : 0 : error (0, 0, _("invalid data in section [%zu] '%s'"),
6513 : : elf_ndxscn (scn), section_name (ebl, shdr));
6514 : 0 : return;
6515 : : }
6516 : :
6517 : 2042 : ptrdiff_t offset = readp - (unsigned char *) data->d_buf;
6518 : 2042 : fprintf (out, "%s 0x%" PRIx64 ":\n\n",
6519 : : table_offset_upper_str, (uint64_t) offset);
6520 : :
6521 [ - + ]: 2042 : uint64_t unit_length = read_4ubyte_unaligned_inc (dbg, readp);
6522 : 2042 : unsigned int offset_size = 4;
6523 [ - + ]: 2042 : if (unlikely (unit_length == 0xffffffff))
6524 : : {
6525 [ # # ]: 0 : if (unlikely (readp > dataend - 8))
6526 : 0 : goto invalid_data;
6527 : :
6528 [ # # ]: 0 : unit_length = read_8ubyte_unaligned_inc (dbg, readp);
6529 : 0 : offset_size = 8;
6530 : : }
6531 : 2042 : fprintf (out, " %s: %8" PRIu64 "\n", length_str, unit_length);
6532 : :
6533 : : /* We need at least 2-bytes + 1-byte + 1-byte + 4-bytes = 8
6534 : : bytes to complete the header. And this unit cannot go beyond
6535 : : the section data. */
6536 [ + - ]: 2042 : if (readp > dataend - 8
6537 [ + - ]: 2042 : || unit_length < 8
6538 [ - + ]: 2042 : || unit_length > (uint64_t) (dataend - readp))
6539 : 0 : goto invalid_data;
6540 : :
6541 : 2042 : const unsigned char *nexthdr = readp + unit_length;
6542 : :
6543 [ - + ]: 2042 : uint16_t version = read_2ubyte_unaligned_inc (dbg, readp);
6544 : 2042 : fprintf (out, " %s: %8" PRIu16 "\n", dwarf_ver_str, version);
6545 : :
6546 [ - + ]: 2042 : if (version != 5)
6547 : : {
6548 : 0 : error (0, 0, _("Unknown version"));
6549 : 0 : goto next_table;
6550 : : }
6551 : :
6552 : 2042 : uint8_t address_size = *readp++;
6553 : 2042 : fprintf (out, " %s: %8" PRIu64 "\n",
6554 : : addr_size_str, (uint64_t) address_size);
6555 : :
6556 [ - + ]: 2042 : if (address_size != 4 && address_size != 8)
6557 : : {
6558 : 0 : error (0, 0, _("unsupported address size"));
6559 : 0 : goto next_table;
6560 : : }
6561 : :
6562 : 2042 : uint8_t segment_size = *readp++;
6563 : 2042 : fprintf (out, _(" Segment size: %8" PRIu64 "\n"),
6564 : : (uint64_t) segment_size);
6565 : :
6566 [ - + - - ]: 2042 : if (segment_size != 0 && segment_size != 4 && segment_size != 8)
6567 : : {
6568 : 0 : error (0, 0, _("unsupported segment size"));
6569 : 0 : goto next_table;
6570 : : }
6571 : :
6572 [ - + ]: 2042 : uint32_t offset_entry_count = read_4ubyte_unaligned_inc (dbg, readp);
6573 : 2042 : fprintf (out, " %s: %8" PRIu64 "\n",
6574 : : offset_ent_str, (uint64_t) offset_entry_count);
6575 : :
6576 : : /* We need the CU that uses this unit to get the initial base address. */
6577 : 2042 : Dwarf_Addr cu_base = 0;
6578 : 2042 : struct Dwarf_CU *cu = NULL;
6579 [ - + ]: 2042 : if (listptr_cu (&known_rnglistptr, &listptr_idx,
6580 : : (Dwarf_Off) offset,
6581 : 2042 : (Dwarf_Off) (nexthdr - (unsigned char *) data->d_buf),
6582 : : &cu_base, &cu)
6583 [ # # ]: 0 : || split_dwarf_cu_base (dbg, &cu, &cu_base))
6584 : 2042 : {
6585 : 2042 : Dwarf_Die cudie;
6586 [ - + ]: 2042 : if (dwarf_cu_die (cu, &cudie,
6587 : : NULL, NULL, NULL, NULL,
6588 : : NULL, NULL) == NULL)
6589 : 0 : fprintf (out, " %s: ", unknown_base_str);
6590 : : else
6591 : 2042 : fprintf (out, " %s [%6" PRIx64 "] %s: ",
6592 : : cu_str, dwarf_dieoffset (&cudie), base_str);
6593 : 2042 : print_dwarf_addr (dwflmod, address_size, cu_base, cu_base, out);
6594 : 2042 : fprintf (out, "\n");
6595 : : }
6596 : : else
6597 : 0 : fprintf (out, _(" Not associated with a CU.\n"));
6598 : :
6599 : 2042 : fprintf (out, "\n");
6600 : :
6601 : 2042 : const unsigned char *offset_array_start = readp;
6602 [ + + ]: 2042 : if (offset_entry_count > 0)
6603 : : {
6604 : 2 : uint64_t max_entries = (unit_length - 8) / offset_size;
6605 [ - + ]: 2 : if (offset_entry_count > max_entries)
6606 : : {
6607 : 0 : error (0, 0,
6608 : 0 : _("too many offset entries for unit length"));
6609 : 0 : offset_entry_count = max_entries;
6610 : : }
6611 : :
6612 : 2 : fprintf (out, _(" Offsets starting at 0x%" PRIx64 ":\n"),
6613 : : (uint64_t) (offset_array_start
6614 : 2 : - (unsigned char *) data->d_buf));
6615 [ + + ]: 6 : for (uint32_t idx = 0; idx < offset_entry_count; idx++)
6616 : : {
6617 : 4 : fprintf (out, " [%6" PRIu32 "] ", idx);
6618 [ + - ]: 4 : if (offset_size == 4)
6619 : : {
6620 [ - + ]: 4 : uint32_t off = read_4ubyte_unaligned_inc (dbg, readp);
6621 : 4 : fprintf (out, "0x%" PRIx32 "\n", off);
6622 : : }
6623 : : else
6624 : : {
6625 [ # # ]: 0 : uint64_t off = read_8ubyte_unaligned_inc (dbg, readp);
6626 : 0 : fprintf (out, "0x%" PRIx64 "\n", off);
6627 : : }
6628 : : }
6629 : 2 : fprintf (out, "\n");
6630 : : }
6631 : :
6632 : 2042 : Dwarf_Addr base = cu_base;
6633 : 2042 : bool start_of_list = true;
6634 [ + + ]: 150364 : while (readp < nexthdr)
6635 : : {
6636 : 148322 : uint8_t kind = *readp++;
6637 : 148322 : uint64_t op1, op2;
6638 : :
6639 : : /* Skip padding. */
6640 [ - + ]: 148322 : if (start_of_list && kind == DW_RLE_end_of_list)
6641 : 0 : continue;
6642 : :
6643 [ + + ]: 148322 : if (start_of_list)
6644 : : {
6645 : 30104 : base = cu_base;
6646 : 30104 : fprintf (out, " Offset: %" PRIx64 ", Index: %" PRIx64 "\n",
6647 : 30104 : (uint64_t) (readp - (unsigned char *) data->d_buf - 1),
6648 : 30104 : (uint64_t) (readp - offset_array_start - 1));
6649 : 30104 : start_of_list = false;
6650 : : }
6651 : :
6652 : 148322 : fprintf (out, " %s", dwarf_range_list_encoding_name (kind));
6653 [ + - - - : 148322 : switch (kind)
+ + - +
- ]
6654 : : {
6655 : 30104 : case DW_RLE_end_of_list:
6656 : 30104 : start_of_list = true;
6657 : 30104 : fprintf (out, "\n\n");
6658 : 30104 : break;
6659 : :
6660 : 0 : case DW_RLE_base_addressx:
6661 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
6662 : : {
6663 : 0 : invalid_range:
6664 : 0 : error (0, 0, _("invalid range list data"));
6665 : 0 : goto next_table;
6666 : : }
6667 : 0 : get_uleb128 (op1, readp, nexthdr);
6668 : 0 : fprintf (out, " %" PRIx64 "\n", op1);
6669 [ # # ]: 0 : if (! print_unresolved_addresses)
6670 : : {
6671 : 0 : Dwarf_Addr addr;
6672 [ # # ]: 0 : if (get_indexed_addr (cu, op1, &addr) != 0)
6673 : 0 : fprintf (out, " ???\n");
6674 : : else
6675 : : {
6676 : 0 : fprintf (out, " ");
6677 : 0 : print_dwarf_addr (dwflmod, address_size, addr,
6678 : : addr, out);
6679 : 0 : fprintf (out, "\n");
6680 : : }
6681 : : }
6682 : : break;
6683 : :
6684 : 0 : case DW_RLE_startx_endx:
6685 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
6686 : 0 : goto invalid_range;
6687 : 0 : get_uleb128 (op1, readp, nexthdr);
6688 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
6689 : 0 : goto invalid_range;
6690 : 0 : get_uleb128 (op2, readp, nexthdr);
6691 : 0 : fprintf (out, " %" PRIx64 ", %" PRIx64 "\n", op1, op2);
6692 [ # # ]: 0 : if (! print_unresolved_addresses)
6693 : : {
6694 : 0 : Dwarf_Addr addr1;
6695 : 0 : Dwarf_Addr addr2;
6696 [ # # ]: 0 : if (get_indexed_addr (cu, op1, &addr1) != 0
6697 [ # # ]: 0 : || get_indexed_addr (cu, op2, &addr2) != 0)
6698 : : {
6699 : 0 : fprintf (out, " ???..\n");
6700 : 0 : fprintf (out, " ???\n");
6701 : : }
6702 : : else
6703 : : {
6704 : 0 : fprintf (out, " ");
6705 : 0 : print_dwarf_addr (dwflmod, address_size,
6706 : : addr1, addr1, out);
6707 : 0 : fprintf (out, "..\n ");
6708 : 0 : print_dwarf_addr (dwflmod, address_size,
6709 : : addr2 - 1, addr2, out);
6710 : 0 : fprintf (out, "\n");
6711 : : }
6712 : : }
6713 : : break;
6714 : :
6715 : 0 : case DW_RLE_startx_length:
6716 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
6717 : 0 : goto invalid_range;
6718 : 0 : get_uleb128 (op1, readp, nexthdr);
6719 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
6720 : 0 : goto invalid_range;
6721 : 0 : get_uleb128 (op2, readp, nexthdr);
6722 : 0 : fprintf (out, " %" PRIx64 ", %" PRIx64 "\n", op1, op2);
6723 [ # # ]: 0 : if (! print_unresolved_addresses)
6724 : : {
6725 : 0 : Dwarf_Addr addr1;
6726 : 0 : Dwarf_Addr addr2;
6727 [ # # ]: 0 : if (get_indexed_addr (cu, op1, &addr1) != 0)
6728 : : {
6729 : 0 : fprintf (out, " ???..\n");
6730 : 0 : fprintf (out, " ???\n");
6731 : : }
6732 : : else
6733 : : {
6734 : 0 : addr2 = addr1 + op2;
6735 : 0 : fprintf (out, " ");
6736 : 0 : print_dwarf_addr (dwflmod, address_size, addr1, addr1, out);
6737 : 0 : fprintf (out, "..\n ");
6738 : 0 : print_dwarf_addr (dwflmod, address_size,
6739 : : addr2 - 1, addr2, out);
6740 : 0 : fprintf (out, "\n");
6741 : : }
6742 : : }
6743 : : break;
6744 : :
6745 : 93842 : case DW_RLE_offset_pair:
6746 [ - + ]: 93842 : if ((uint64_t) (nexthdr - readp) < 1)
6747 : 0 : goto invalid_range;
6748 : 93842 : get_uleb128 (op1, readp, nexthdr);
6749 [ - + ]: 93842 : if ((uint64_t) (nexthdr - readp) < 1)
6750 : 0 : goto invalid_range;
6751 : 93842 : get_uleb128 (op2, readp, nexthdr);
6752 : 93842 : fprintf (out, " %" PRIx64 ", %" PRIx64 "\n", op1, op2);
6753 [ + - ]: 93842 : if (! print_unresolved_addresses)
6754 : : {
6755 : 93842 : op1 += base;
6756 : 93842 : op2 += base;
6757 : 93842 : fprintf (out, " ");
6758 : 93842 : print_dwarf_addr (dwflmod, address_size, op1, op1, out);
6759 : 93842 : fprintf (out, "..\n ");
6760 : 93842 : print_dwarf_addr (dwflmod, address_size, op2 - 1, op2, out);
6761 : 93842 : fprintf (out, "\n");
6762 : : }
6763 : : break;
6764 : :
6765 : 17016 : case DW_RLE_base_address:
6766 [ - + ]: 17016 : if (address_size == 4)
6767 : : {
6768 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 4)
6769 : 0 : goto invalid_range;
6770 [ # # ]: 0 : op1 = read_4ubyte_unaligned_inc (dbg, readp);
6771 : : }
6772 : : else
6773 : : {
6774 [ - + ]: 17016 : if ((uint64_t) (nexthdr - readp) < 8)
6775 : 0 : goto invalid_range;
6776 [ - + ]: 17016 : op1 = read_8ubyte_unaligned_inc (dbg, readp);
6777 : : }
6778 : 17016 : base = op1;
6779 : 17016 : fprintf (out, " 0x%" PRIx64 "\n", base);
6780 [ + - ]: 17016 : if (! print_unresolved_addresses)
6781 : : {
6782 : 17016 : fprintf (out, " ");
6783 : 17016 : print_dwarf_addr (dwflmod, address_size, base, base, out);
6784 : 17016 : fprintf (out, "\n");
6785 : : }
6786 : : break;
6787 : :
6788 : 0 : case DW_RLE_start_end:
6789 [ # # ]: 0 : if (address_size == 4)
6790 : : {
6791 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 8)
6792 : 0 : goto invalid_range;
6793 [ # # ]: 0 : op1 = read_4ubyte_unaligned_inc (dbg, readp);
6794 [ # # ]: 0 : op2 = read_4ubyte_unaligned_inc (dbg, readp);
6795 : : }
6796 : : else
6797 : : {
6798 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 16)
6799 : 0 : goto invalid_range;
6800 [ # # ]: 0 : op1 = read_8ubyte_unaligned_inc (dbg, readp);
6801 [ # # ]: 0 : op2 = read_8ubyte_unaligned_inc (dbg, readp);
6802 : : }
6803 : 0 : fprintf (out, " 0x%" PRIx64 "..0x%" PRIx64 "\n", op1, op2);
6804 [ # # ]: 0 : if (! print_unresolved_addresses)
6805 : : {
6806 : 0 : fprintf (out, " ");
6807 : 0 : print_dwarf_addr (dwflmod, address_size, op1, op1, out);
6808 : 0 : fprintf (out, "..\n ");
6809 : 0 : print_dwarf_addr (dwflmod, address_size, op2 - 1, op2, out);
6810 : 0 : fprintf (out, "\n");
6811 : : }
6812 : : break;
6813 : :
6814 : 7360 : case DW_RLE_start_length:
6815 [ - + ]: 7360 : if (address_size == 4)
6816 : : {
6817 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 4)
6818 : 0 : goto invalid_range;
6819 [ # # ]: 0 : op1 = read_4ubyte_unaligned_inc (dbg, readp);
6820 : : }
6821 : : else
6822 : : {
6823 [ - + ]: 7360 : if ((uint64_t) (nexthdr - readp) < 8)
6824 : 0 : goto invalid_range;
6825 [ - + ]: 7360 : op1 = read_8ubyte_unaligned_inc (dbg, readp);
6826 : : }
6827 [ - + ]: 7360 : if ((uint64_t) (nexthdr - readp) < 1)
6828 : 0 : goto invalid_range;
6829 : 7360 : get_uleb128 (op2, readp, nexthdr);
6830 : 7360 : fprintf (out, " 0x%" PRIx64 ", %" PRIx64 "\n", op1, op2);
6831 [ + + ]: 7360 : if (! print_unresolved_addresses)
6832 : : {
6833 : 7358 : op2 = op1 + op2;
6834 : 7358 : fprintf (out, " ");
6835 : 7358 : print_dwarf_addr (dwflmod, address_size, op1, op1, out);
6836 : 7358 : fprintf (out, "..\n ");
6837 : 7358 : print_dwarf_addr (dwflmod, address_size, op2 - 1, op2, out);
6838 : 7358 : fprintf (out, "\n");
6839 : : }
6840 : : break;
6841 : :
6842 : 0 : default:
6843 : 0 : goto invalid_range;
6844 : : }
6845 : : }
6846 : :
6847 : 2042 : next_table:
6848 [ - + ]: 2042 : if (readp != nexthdr)
6849 : : {
6850 : 0 : size_t padding = nexthdr - readp;
6851 : 0 : fprintf (out, _(" %zu padding bytes\n\n"), padding);
6852 : 0 : readp = nexthdr;
6853 : : }
6854 : : }
6855 : : }
6856 : :
6857 : : /* Print content of DWARF .debug_ranges section. */
6858 : : static void
6859 : 32 : print_debug_ranges_section (Dwfl_Module *dwflmod,
6860 : : Ebl *ebl, GElf_Ehdr *ehdr,
6861 : : Elf_Scn *scn, GElf_Shdr *shdr,
6862 : : Dwarf *dbg, FILE *out)
6863 : : {
6864 : 32 : Elf_Data *data = get_debug_elf_data (dbg, ebl, IDX_debug_ranges, scn);
6865 [ - + ]: 32 : if (data == NULL)
6866 : 0 : return;
6867 : :
6868 : 32 : fprintf (out, _("\
6869 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
6870 : : elf_ndxscn (scn), section_name (ebl, shdr),
6871 : 32 : (uint64_t) shdr->sh_offset);
6872 : :
6873 : 32 : sort_listptr (&known_rangelistptr, "rangelistptr");
6874 : 32 : size_t listptr_idx = 0;
6875 : :
6876 [ + + ]: 32 : uint_fast8_t address_size = ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 4 : 8;
6877 : :
6878 : 32 : bool first = true;
6879 : 32 : Dwarf_Addr base = 0;
6880 : 32 : unsigned char *const endp = (unsigned char *) data->d_buf + data->d_size;
6881 : 32 : unsigned char *readp = data->d_buf;
6882 : 32 : Dwarf_CU *last_cu = NULL;
6883 [ + + ]: 184 : while (readp < endp)
6884 : : {
6885 : 152 : ptrdiff_t offset = readp - (unsigned char *) data->d_buf;
6886 : 152 : Dwarf_CU *cu = last_cu;
6887 : :
6888 [ + + - + ]: 152 : if (first && skip_listptr_hole (&known_rangelistptr, &listptr_idx,
6889 : : &address_size, NULL, &base, &cu,
6890 : : offset, &readp, endp, NULL))
6891 : 0 : continue;
6892 : :
6893 [ + + ]: 152 : if (last_cu != cu)
6894 : : {
6895 : 58 : Dwarf_Die cudie;
6896 [ - + ]: 58 : if (dwarf_cu_die (cu, &cudie,
6897 : : NULL, NULL, NULL, NULL,
6898 : : NULL, NULL) == NULL)
6899 : 0 : fprintf (out, _("\n Unknown CU base: "));
6900 : : else
6901 : 58 : fprintf (out, _("\n CU [%6" PRIx64 "] base: "),
6902 : : dwarf_dieoffset (&cudie));
6903 : 58 : print_dwarf_addr (dwflmod, address_size, base, base, out);
6904 : 58 : fprintf (out, "\n");
6905 : : }
6906 : 152 : last_cu = cu;
6907 : :
6908 [ - + ]: 152 : if (unlikely (data->d_size - offset < (size_t) address_size * 2))
6909 : : {
6910 : 0 : fprintf (out, _(" [%6tx] <INVALID DATA>\n"), offset);
6911 : 0 : break;
6912 : : }
6913 : :
6914 : 152 : Dwarf_Addr begin;
6915 : 152 : Dwarf_Addr end;
6916 [ + + ]: 152 : if (address_size == 8)
6917 : : {
6918 [ - + ]: 120 : begin = read_8ubyte_unaligned_inc (dbg, readp);
6919 [ - + ]: 120 : end = read_8ubyte_unaligned_inc (dbg, readp);
6920 : : }
6921 : : else
6922 : : {
6923 [ - + ]: 32 : begin = read_4ubyte_unaligned_inc (dbg, readp);
6924 [ - + ]: 32 : end = read_4ubyte_unaligned_inc (dbg, readp);
6925 [ + - ]: 32 : if (begin == (Dwarf_Addr) (uint32_t) -1)
6926 : : begin = (Dwarf_Addr) -1l;
6927 : : }
6928 : :
6929 [ - + ]: 152 : if (begin == (Dwarf_Addr) -1l) /* Base address entry. */
6930 : : {
6931 [ # # ]: 0 : if (first)
6932 : 0 : fprintf (out, " [%6tx] ", offset);
6933 : : else
6934 : 0 : fprintf (out, " ");
6935 : 0 : fputs (_("base address\n"), out);
6936 : 0 : fprintf (out, " ");
6937 : 0 : print_dwarf_addr (dwflmod, address_size, end, end, out);
6938 : 0 : fprintf (out, "\n");
6939 : 0 : base = end;
6940 : 0 : first = false;
6941 : : }
6942 [ + + ]: 152 : else if (begin == 0 && end == 0) /* End of list entry. */
6943 : : {
6944 [ - + ]: 60 : if (first)
6945 : 0 : fprintf (out, _(" [%6tx] empty list\n"), offset);
6946 : : first = true;
6947 : : }
6948 : : else
6949 : : {
6950 : : /* We have an address range entry. */
6951 [ + + ]: 92 : if (first) /* First address range entry in a list. */
6952 : 60 : fprintf (out, " [%6tx] ", offset);
6953 : : else
6954 : 32 : fprintf (out, " ");
6955 : :
6956 : 92 : fprintf (out, "range %" PRIx64 ", %" PRIx64 "\n", begin, end);
6957 [ + + ]: 92 : if (! print_unresolved_addresses)
6958 : : {
6959 : 72 : fprintf (out, " ");
6960 : 72 : print_dwarf_addr (dwflmod, address_size, base + begin,
6961 : : base + begin, out);
6962 : 72 : fprintf (out, "..\n ");
6963 : 72 : print_dwarf_addr (dwflmod, address_size,
6964 : : base + end - 1, base + end, out);
6965 : 72 : fprintf (out, "\n");
6966 : : }
6967 : :
6968 : : first = false;
6969 : : }
6970 : : }
6971 : : }
6972 : :
6973 : : #define REGNAMESZ 16
6974 : : static const char *
6975 : 39858 : register_info (Ebl *ebl, unsigned int regno, const Ebl_Register_Location *loc,
6976 : : char name[REGNAMESZ], int *bits, int *type)
6977 : : {
6978 : 39858 : const char *set;
6979 : 39858 : const char *pfx;
6980 : 39858 : int ignore;
6981 [ + + + + ]: 116182 : ssize_t n = ebl_register_info (ebl, regno, name, REGNAMESZ, &pfx, &set,
6982 : : bits ?: &ignore, type ?: &ignore);
6983 [ - + ]: 39858 : if (n <= 0)
6984 : : {
6985 [ # # ]: 0 : if (loc != NULL)
6986 : 0 : snprintf (name, REGNAMESZ, "reg%u", loc->regno);
6987 : : else
6988 : 0 : snprintf (name, REGNAMESZ, "??? 0x%x", regno);
6989 [ # # ]: 0 : if (bits != NULL)
6990 [ # # ]: 0 : *bits = loc != NULL ? loc->bits : 0;
6991 [ # # ]: 0 : if (type != NULL)
6992 : 0 : *type = DW_ATE_unsigned;
6993 : 0 : set = "??? unrecognized";
6994 : : }
6995 : : else
6996 : : {
6997 [ + + - + ]: 39858 : if (bits != NULL && *bits <= 0)
6998 [ # # ]: 0 : *bits = loc != NULL ? loc->bits : 0;
6999 [ + + - + ]: 39858 : if (type != NULL && *type == DW_ATE_void)
7000 : 0 : *type = DW_ATE_unsigned;
7001 : :
7002 : : }
7003 : 39858 : return set;
7004 : : }
7005 : :
7006 : : static const unsigned char *
7007 : 280 : read_encoded (unsigned int encoding, const unsigned char *readp,
7008 : : const unsigned char *const endp, uint64_t *res, Dwarf *dbg)
7009 : : {
7010 [ - + ]: 280 : if ((encoding & 0xf) == DW_EH_PE_absptr)
7011 : 0 : encoding = gelf_getclass (dbg->elf) == ELFCLASS32
7012 [ # # ]: 0 : ? DW_EH_PE_udata4 : DW_EH_PE_udata8;
7013 : :
7014 [ - - - + : 280 : switch (encoding & 0xf)
- - + -
- ]
7015 : : {
7016 : 0 : case DW_EH_PE_uleb128:
7017 [ # # ]: 0 : if (readp >= endp)
7018 : 0 : goto invalid;
7019 : 0 : get_uleb128 (*res, readp, endp);
7020 : 0 : break;
7021 : 0 : case DW_EH_PE_sleb128:
7022 [ # # ]: 0 : if (readp >= endp)
7023 : 0 : goto invalid;
7024 : 0 : get_sleb128 (*res, readp, endp);
7025 : 0 : break;
7026 : 0 : case DW_EH_PE_udata2:
7027 [ # # ]: 0 : if (readp + 2 > endp)
7028 : 0 : goto invalid;
7029 [ # # ]: 0 : *res = read_2ubyte_unaligned_inc (dbg, readp);
7030 : 0 : break;
7031 : 140 : case DW_EH_PE_udata4:
7032 [ - + ]: 140 : if (readp + 4 > endp)
7033 : 0 : goto invalid;
7034 [ - + ]: 140 : *res = read_4ubyte_unaligned_inc (dbg, readp);
7035 : 140 : break;
7036 : 0 : case DW_EH_PE_udata8:
7037 [ # # ]: 0 : if (readp + 8 > endp)
7038 : 0 : goto invalid;
7039 [ # # ]: 0 : *res = read_8ubyte_unaligned_inc (dbg, readp);
7040 : 0 : break;
7041 : 0 : case DW_EH_PE_sdata2:
7042 [ # # ]: 0 : if (readp + 2 > endp)
7043 : 0 : goto invalid;
7044 [ # # ]: 0 : *res = read_2sbyte_unaligned_inc (dbg, readp);
7045 : 0 : break;
7046 : 140 : case DW_EH_PE_sdata4:
7047 [ - + ]: 140 : if (readp + 4 > endp)
7048 : 0 : goto invalid;
7049 [ - + ]: 140 : *res = read_4sbyte_unaligned_inc (dbg, readp);
7050 : 140 : break;
7051 : 0 : case DW_EH_PE_sdata8:
7052 [ # # ]: 0 : if (readp + 8 > endp)
7053 : 0 : goto invalid;
7054 [ # # ]: 0 : *res = read_8sbyte_unaligned_inc (dbg, readp);
7055 : 0 : break;
7056 : : default:
7057 : 0 : invalid:
7058 : 0 : error (1, 0,
7059 : 0 : _("invalid encoding"));
7060 : : }
7061 : :
7062 : 280 : return readp;
7063 : : }
7064 : :
7065 : : static const char *
7066 : 38162 : regname (Ebl *ebl, unsigned int regno, char *regnamebuf)
7067 : : {
7068 : 38162 : register_info (ebl, regno, NULL, regnamebuf, NULL, NULL);
7069 : :
7070 : 38162 : return regnamebuf;
7071 : : }
7072 : :
7073 : : static void
7074 : 24482 : print_cfa_program (const unsigned char *readp, const unsigned char *const endp,
7075 : : Dwarf_Word vma_base, unsigned int code_align,
7076 : : int data_align,
7077 : : unsigned int version, unsigned int ptr_size,
7078 : : unsigned int encoding,
7079 : : Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr, Dwarf *dbg,
7080 : : FILE *out)
7081 : : {
7082 : 24482 : char regnamebuf[REGNAMESZ];
7083 : :
7084 : 24482 : fputs ("\n Program:\n", out);
7085 : 24482 : Dwarf_Word pc = vma_base;
7086 [ + + ]: 358280 : while (readp < endp)
7087 : : {
7088 : 333798 : unsigned int opcode = *readp++;
7089 : :
7090 [ + + ]: 333798 : if (opcode < DW_CFA_advance_loc)
7091 : : /* Extended opcode. */
7092 [ + - + + : 198800 : switch (opcode)
+ + - + -
- + + + +
+ + - + -
- - - - -
+ - - ]
7093 : : {
7094 : 68086 : uint64_t op1;
7095 : 68086 : int64_t sop1;
7096 : 68086 : uint64_t op2;
7097 : 68086 : int64_t sop2;
7098 : :
7099 : 68086 : case DW_CFA_nop:
7100 : 68086 : fputs (" nop\n", out);
7101 : 266886 : break;
7102 : 0 : case DW_CFA_set_loc:
7103 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
7104 : 0 : goto invalid;
7105 : 0 : readp = read_encoded (encoding, readp, endp, &op1, dbg);
7106 : 0 : fprintf (out, " set_loc %#" PRIx64 " to %#" PRIx64 "\n",
7107 : 0 : op1, pc = vma_base + op1);
7108 : 0 : break;
7109 : 6840 : case DW_CFA_advance_loc1:
7110 [ - + ]: 6840 : if ((uint64_t) (endp - readp) < 1)
7111 : 0 : goto invalid;
7112 : 13680 : fprintf (out, " advance_loc1 %u to %#" PRIx64 "\n",
7113 : 6840 : *readp, pc += *readp * code_align);
7114 : 6840 : ++readp;
7115 : 6840 : break;
7116 : 2896 : case DW_CFA_advance_loc2:
7117 [ - + ]: 2896 : if ((uint64_t) (endp - readp) < 2)
7118 : 0 : goto invalid;
7119 [ - + ]: 2896 : op1 = read_2ubyte_unaligned_inc (dbg, readp);
7120 : 2896 : fprintf (out, " advance_loc2 %" PRIu64 " to %#" PRIx64 "\n",
7121 : 2896 : op1, pc += op1 * code_align);
7122 : 2896 : break;
7123 : 36 : case DW_CFA_advance_loc4:
7124 [ - + ]: 36 : if ((uint64_t) (endp - readp) < 4)
7125 : 0 : goto invalid;
7126 [ + - ]: 36 : op1 = read_4ubyte_unaligned_inc (dbg, readp);
7127 : 36 : fprintf (out, " advance_loc4 %" PRIu64 " to %#" PRIx64 "\n",
7128 : 36 : op1, pc += op1 * code_align);
7129 : 36 : break;
7130 : 8 : case DW_CFA_offset_extended:
7131 [ - + ]: 8 : if ((uint64_t) (endp - readp) < 1)
7132 : 0 : goto invalid;
7133 : 8 : get_uleb128 (op1, readp, endp);
7134 [ - + ]: 8 : if ((uint64_t) (endp - readp) < 1)
7135 : 0 : goto invalid;
7136 : 8 : get_uleb128 (op2, readp, endp);
7137 : 8 : fprintf (out, " offset_extended r%" PRIu64 " (%s) at cfa%+" PRId64
7138 : : "\n",
7139 : : op1, regname (ebl, op1, regnamebuf), op2 * data_align);
7140 : 8 : break;
7141 : 0 : case DW_CFA_restore_extended:
7142 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
7143 : 0 : goto invalid;
7144 : 0 : get_uleb128 (op1, readp, endp);
7145 : 0 : fprintf (out, " restore_extended r%" PRIu64 " (%s)\n",
7146 : : op1, regname (ebl, op1, regnamebuf));
7147 : 0 : break;
7148 : 110 : case DW_CFA_undefined:
7149 [ - + ]: 110 : if ((uint64_t) (endp - readp) < 1)
7150 : 0 : goto invalid;
7151 : 110 : get_uleb128 (op1, readp, endp);
7152 : 110 : fprintf (out, " undefined r%" PRIu64 " (%s)\n", op1,
7153 : : regname (ebl, op1, regnamebuf));
7154 : 110 : break;
7155 : 0 : case DW_CFA_same_value:
7156 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
7157 : 0 : goto invalid;
7158 : 0 : get_uleb128 (op1, readp, endp);
7159 : 0 : fprintf (out, " same_value r%" PRIu64 " (%s)\n", op1,
7160 : : regname (ebl, op1, regnamebuf));
7161 : 0 : break;
7162 : 0 : case DW_CFA_register:
7163 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
7164 : 0 : goto invalid;
7165 : 0 : get_uleb128 (op1, readp, endp);
7166 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
7167 : 0 : goto invalid;
7168 : 0 : get_uleb128 (op2, readp, endp);
7169 : 0 : fprintf (out,
7170 : : " register r%" PRIu64 " (%s) in r%" PRIu64 " (%s)\n",
7171 : : op1, regname (ebl, op1, regnamebuf), op2,
7172 : : regname (ebl, op2, regnamebuf));
7173 : 0 : break;
7174 : 11966 : case DW_CFA_remember_state:
7175 : 11966 : fputs (" remember_state\n", out);
7176 : 11966 : break;
7177 : 11966 : case DW_CFA_restore_state:
7178 : 11966 : fputs (" restore_state\n", out);
7179 : 11966 : break;
7180 : 418 : case DW_CFA_def_cfa:
7181 [ - + ]: 418 : if ((uint64_t) (endp - readp) < 1)
7182 : 0 : goto invalid;
7183 : 418 : get_uleb128 (op1, readp, endp);
7184 [ - + ]: 418 : if ((uint64_t) (endp - readp) < 1)
7185 : 0 : goto invalid;
7186 : 418 : get_uleb128 (op2, readp, endp);
7187 : 418 : fprintf (out,
7188 : : " def_cfa r%" PRIu64 " (%s) at offset %" PRIu64 "\n",
7189 : : op1, regname (ebl, op1, regnamebuf), op2);
7190 : 418 : break;
7191 : 126 : case DW_CFA_def_cfa_register:
7192 [ - + ]: 126 : if ((uint64_t) (endp - readp) < 1)
7193 : 0 : goto invalid;
7194 : 126 : get_uleb128 (op1, readp, endp);
7195 : 126 : fprintf (out, " def_cfa_register r%" PRIu64 " (%s)\n",
7196 : : op1, regname (ebl, op1, regnamebuf));
7197 : 126 : break;
7198 : 96198 : case DW_CFA_def_cfa_offset:
7199 [ - + ]: 96198 : if ((uint64_t) (endp - readp) < 1)
7200 : 0 : goto invalid;
7201 : 96198 : get_uleb128 (op1, readp, endp);
7202 : 96198 : fprintf (out, " def_cfa_offset %" PRIu64 "\n", op1);
7203 : 96198 : break;
7204 : 118 : case DW_CFA_def_cfa_expression:
7205 [ - + ]: 118 : if ((uint64_t) (endp - readp) < 1)
7206 : 0 : goto invalid;
7207 : 118 : get_uleb128 (op1, readp, endp); /* Length of DW_FORM_block. */
7208 : 118 : fprintf (out, " def_cfa_expression %" PRIu64 "\n", op1);
7209 [ - + ]: 118 : if ((uint64_t) (endp - readp) < op1)
7210 : : {
7211 : 0 : invalid:
7212 : 0 : fputs (_(" <INVALID DATA>\n"), out);
7213 : 0 : return;
7214 : : }
7215 : 118 : print_ops (dwflmod, dbg, 10, 10, version, ptr_size, 0, NULL,
7216 : : op1, readp, out);
7217 : 118 : readp += op1;
7218 : 118 : break;
7219 : 0 : case DW_CFA_expression:
7220 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
7221 : 0 : goto invalid;
7222 : 0 : get_uleb128 (op1, readp, endp);
7223 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
7224 : 0 : goto invalid;
7225 : 0 : get_uleb128 (op2, readp, endp); /* Length of DW_FORM_block. */
7226 : 0 : fprintf (out, " expression r%" PRIu64 " (%s) \n",
7227 : : op1, regname (ebl, op1, regnamebuf));
7228 [ # # ]: 0 : if ((uint64_t) (endp - readp) < op2)
7229 : 0 : goto invalid;
7230 : 0 : print_ops (dwflmod, dbg, 10, 10, version, ptr_size, 0, NULL,
7231 : : op2, readp, out);
7232 : 0 : readp += op2;
7233 : 0 : break;
7234 : 24 : case DW_CFA_offset_extended_sf:
7235 [ - + ]: 24 : if ((uint64_t) (endp - readp) < 1)
7236 : 0 : goto invalid;
7237 : 24 : get_uleb128 (op1, readp, endp);
7238 [ - + ]: 24 : if ((uint64_t) (endp - readp) < 1)
7239 : 0 : goto invalid;
7240 : 24 : get_sleb128 (sop2, readp, endp);
7241 : 24 : fprintf (out, " offset_extended_sf r%" PRIu64 " (%s) at cfa%+"
7242 : : PRId64 "\n",
7243 : : op1, regname (ebl, op1, regnamebuf), sop2 * data_align);
7244 : 24 : break;
7245 : 0 : case DW_CFA_def_cfa_sf:
7246 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
7247 : 0 : goto invalid;
7248 : 0 : get_uleb128 (op1, readp, endp);
7249 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
7250 : 0 : goto invalid;
7251 : 0 : get_sleb128 (sop2, readp, endp);
7252 : 0 : fprintf (out,
7253 : : " def_cfa_sf r%" PRIu64 " (%s) at offset %" PRId64 "\n",
7254 : : op1, regname (ebl, op1, regnamebuf), sop2 * data_align);
7255 : 0 : break;
7256 : 0 : case DW_CFA_def_cfa_offset_sf:
7257 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
7258 : 0 : goto invalid;
7259 : 0 : get_sleb128 (sop1, readp, endp);
7260 : 0 : fprintf (out,
7261 : : " def_cfa_offset_sf %" PRId64 "\n", sop1 * data_align);
7262 : 0 : break;
7263 : 0 : case DW_CFA_val_offset:
7264 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
7265 : 0 : goto invalid;
7266 : 0 : get_uleb128 (op1, readp, endp);
7267 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
7268 : 0 : goto invalid;
7269 : 0 : get_uleb128 (op2, readp, endp);
7270 : 0 : fprintf (out, " val_offset %" PRIu64 " at offset %" PRIu64 "\n",
7271 : : op1, op2 * data_align);
7272 : 0 : break;
7273 : 0 : case DW_CFA_val_offset_sf:
7274 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
7275 : 0 : goto invalid;
7276 : 0 : get_uleb128 (op1, readp, endp);
7277 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
7278 : 0 : goto invalid;
7279 : 0 : get_sleb128 (sop2, readp, endp);
7280 : 0 : fprintf (out,
7281 : : " val_offset_sf %" PRIu64 " at offset %" PRId64 "\n",
7282 : : op1, sop2 * data_align);
7283 : 0 : break;
7284 : 0 : case DW_CFA_val_expression:
7285 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
7286 : 0 : goto invalid;
7287 : 0 : get_uleb128 (op1, readp, endp);
7288 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
7289 : 0 : goto invalid;
7290 : 0 : get_uleb128 (op2, readp, endp); /* Length of DW_FORM_block. */
7291 : 0 : fprintf (out, " val_expression r%" PRIu64 " (%s)\n",
7292 : : op1, regname (ebl, op1, regnamebuf));
7293 [ # # ]: 0 : if ((uint64_t) (endp - readp) < op2)
7294 : 0 : goto invalid;
7295 : 0 : print_ops (dwflmod, dbg, 10, 10, version, ptr_size, 0,
7296 : : NULL, op2, readp, out);
7297 : 0 : readp += op2;
7298 : 0 : break;
7299 : 0 : case DW_CFA_MIPS_advance_loc8:
7300 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 8)
7301 : 0 : goto invalid;
7302 [ # # ]: 0 : op1 = read_8ubyte_unaligned_inc (dbg, readp);
7303 : 0 : fprintf (out,
7304 : : " MIPS_advance_loc8 %" PRIu64 " to %#" PRIx64 "\n",
7305 : 0 : op1, pc += op1 * code_align);
7306 : 0 : break;
7307 : 8 : case DW_CFA_GNU_window_save: /* DW_CFA_AARCH64_negate_ra_state */
7308 [ + - ]: 8 : if (ehdr->e_machine == EM_AARCH64)
7309 : 8 : fputs (" AARCH64_negate_ra_state\n", out);
7310 : : else
7311 : 0 : fputs (" GNU_window_save\n", out);
7312 : : break;
7313 : 0 : case DW_CFA_GNU_args_size:
7314 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
7315 : 0 : goto invalid;
7316 : 0 : get_uleb128 (op1, readp, endp);
7317 : 0 : fprintf (out, " args_size %" PRIu64 "\n", op1);
7318 : 0 : break;
7319 : : default:
7320 : 0 : fprintf (out, " ??? (%u)\n", opcode);
7321 : 0 : break;
7322 : : }
7323 [ + + ]: 134998 : else if (opcode < DW_CFA_offset)
7324 : 97522 : fprintf (out, " advance_loc %u to %#" PRIx64 "\n",
7325 : 97522 : opcode & 0x3f, pc += (opcode & 0x3f) * code_align);
7326 [ + + ]: 37476 : else if (opcode < DW_CFA_restore)
7327 : : {
7328 : 34034 : uint64_t offset;
7329 [ - + ]: 34034 : if ((uint64_t) (endp - readp) < 1)
7330 : 0 : goto invalid;
7331 : 34034 : get_uleb128 (offset, readp, endp);
7332 : 34034 : fprintf (out, " offset r%u (%s) at cfa%+" PRId64 "\n",
7333 : : opcode & 0x3f, regname (ebl, opcode & 0x3f, regnamebuf),
7334 : : offset * data_align);
7335 : : }
7336 : : else
7337 : 3442 : fprintf (out, " restore r%u (%s)\n",
7338 : : opcode & 0x3f, regname (ebl, opcode & 0x3f, regnamebuf));
7339 : : }
7340 : : }
7341 : :
7342 : :
7343 : : static unsigned int
7344 : 24172 : encoded_ptr_size (int encoding, unsigned int ptr_size)
7345 : : {
7346 [ - + - + ]: 24172 : switch (encoding & 7)
7347 : : {
7348 : : case DW_EH_PE_udata4:
7349 : : return 4;
7350 : 0 : case DW_EH_PE_udata8:
7351 : 0 : return 8;
7352 : : case 0:
7353 : : return ptr_size;
7354 : : }
7355 : :
7356 : 0 : fprintf (stderr, "Unsupported pointer encoding: %#x, "
7357 : : "assuming pointer size of %d.\n", encoding, ptr_size);
7358 : 0 : return ptr_size;
7359 : : }
7360 : :
7361 : :
7362 : : static unsigned int
7363 : 642 : print_encoding (unsigned int val, FILE *out)
7364 : : {
7365 [ - - - + : 642 : switch (val & 0xf)
- - - + -
- ]
7366 : : {
7367 : 0 : case DW_EH_PE_absptr:
7368 : 0 : fputs ("absptr", out);
7369 : 0 : break;
7370 : 0 : case DW_EH_PE_uleb128:
7371 : 0 : fputs ("uleb128", out);
7372 : 0 : break;
7373 : 0 : case DW_EH_PE_udata2:
7374 : 0 : fputs ("udata2", out);
7375 : 0 : break;
7376 : 140 : case DW_EH_PE_udata4:
7377 : 140 : fputs ("udata4", out);
7378 : 140 : break;
7379 : 0 : case DW_EH_PE_udata8:
7380 : 0 : fputs ("udata8", out);
7381 : 0 : break;
7382 : 0 : case DW_EH_PE_sleb128:
7383 : 0 : fputs ("sleb128", out);
7384 : 0 : break;
7385 : 0 : case DW_EH_PE_sdata2:
7386 : 0 : fputs ("sdata2", out);
7387 : 0 : break;
7388 : 502 : case DW_EH_PE_sdata4:
7389 : 502 : fputs ("sdata4", out);
7390 : 502 : break;
7391 : 0 : case DW_EH_PE_sdata8:
7392 : 0 : fputs ("sdata8", out);
7393 : 0 : break;
7394 : : default:
7395 : : /* We did not use any of the bits after all. */
7396 : : return val;
7397 : : }
7398 : :
7399 : 642 : return val & ~0xf;
7400 : : }
7401 : :
7402 : :
7403 : : static unsigned int
7404 : 502 : print_relinfo (unsigned int val, FILE *out)
7405 : : {
7406 [ + - + - : 502 : switch (val & 0x70)
- - ]
7407 : : {
7408 : 362 : case DW_EH_PE_pcrel:
7409 : 362 : fputs ("pcrel", out);
7410 : 362 : break;
7411 : 0 : case DW_EH_PE_textrel:
7412 : 0 : fputs ("textrel", out);
7413 : 0 : break;
7414 : 140 : case DW_EH_PE_datarel:
7415 : 140 : fputs ("datarel", out);
7416 : 140 : break;
7417 : 0 : case DW_EH_PE_funcrel:
7418 : 0 : fputs ("funcrel", out);
7419 : 0 : break;
7420 : 0 : case DW_EH_PE_aligned:
7421 : 0 : fputs ("aligned", out);
7422 : 0 : break;
7423 : : default:
7424 : : return val;
7425 : : }
7426 : :
7427 : 502 : return val & ~0x70;
7428 : : }
7429 : :
7430 : :
7431 : : static void
7432 : 642 : print_encoding_base (const char *pfx, unsigned int fde_encoding,
7433 : : FILE *out)
7434 : : {
7435 : 642 : fprintf (out, "(%s", pfx);
7436 : :
7437 [ - + ]: 642 : if (fde_encoding == DW_EH_PE_omit)
7438 : 0 : fputs ("omit)\n", out);
7439 : : else
7440 : : {
7441 : 642 : unsigned int w = fde_encoding;
7442 : :
7443 : 642 : w = print_encoding (w, out);
7444 : :
7445 [ + + ]: 642 : if (w & 0x70)
7446 : : {
7447 [ + - ]: 502 : if (w != fde_encoding)
7448 : 502 : fputc (' ', out);
7449 : :
7450 : 502 : w = print_relinfo (w, out);
7451 : : }
7452 : :
7453 [ - + ]: 642 : if (w != 0)
7454 [ # # ]: 0 : fprintf (out, "%s%x", w != fde_encoding ? " " : "", w);
7455 : :
7456 : 642 : fputs (")\n", out);
7457 : : }
7458 : 642 : }
7459 : :
7460 : :
7461 : : static void
7462 : 196 : print_debug_frame_section (Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr,
7463 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg,
7464 : : FILE *out)
7465 : : {
7466 : 196 : size_t shstrndx;
7467 : : /* We know this call will succeed since it did in the caller. */
7468 : 196 : (void) elf_getshdrstrndx (ebl->elf, &shstrndx);
7469 : 196 : const char *scnname = section_name (ebl, shdr);
7470 : :
7471 : : /* Needed if we find PC-relative addresses. */
7472 : 196 : GElf_Addr bias;
7473 [ - + ]: 196 : if (dwfl_module_getelf (dwflmod, &bias) == NULL)
7474 : : {
7475 : 0 : error (0, 0, _("cannot get ELF: %s"), dwfl_errmsg (-1));
7476 : 0 : return;
7477 : : }
7478 : :
7479 : 196 : bool is_eh_frame = strcmp (scnname, ".eh_frame") == 0;
7480 : 196 : Elf_Data *data;
7481 [ + + ]: 196 : if (is_eh_frame)
7482 : : {
7483 : 144 : data = elf_rawdata (scn, NULL);
7484 [ - + ]: 144 : if (data == NULL)
7485 : : {
7486 : 0 : error (0, 0, _("cannot get %s content: %s"),
7487 : : scnname, elf_errmsg (-1));
7488 : 0 : return;
7489 : : }
7490 : : }
7491 : : else
7492 : : {
7493 : 52 : data = get_debug_elf_data (dbg, ebl, IDX_debug_frame, scn);
7494 [ + - ]: 52 : if (data == NULL)
7495 : : return;
7496 : : }
7497 : :
7498 [ + + ]: 196 : if (is_eh_frame)
7499 : 144 : fprintf (out, _("\
7500 : : \nCall frame information section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
7501 : 144 : elf_ndxscn (scn), scnname, (uint64_t) shdr->sh_offset);
7502 : : else
7503 : 52 : fprintf (out, _("\
7504 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
7505 : 52 : elf_ndxscn (scn), scnname, (uint64_t) shdr->sh_offset);
7506 : :
7507 : 196 : struct cieinfo
7508 : : {
7509 : : ptrdiff_t cie_offset;
7510 : : const char *augmentation;
7511 : : unsigned int code_alignment_factor;
7512 : : unsigned int data_alignment_factor;
7513 : : uint8_t address_size;
7514 : : uint8_t fde_encoding;
7515 : : uint8_t lsda_encoding;
7516 : : struct cieinfo *next;
7517 : 196 : } *cies = NULL;
7518 : :
7519 : 196 : const unsigned char *readp = data->d_buf;
7520 : 196 : const unsigned char *const dataend = ((unsigned char *) data->d_buf
7521 : 196 : + data->d_size);
7522 [ + + ]: 24818 : while (readp < dataend)
7523 : : {
7524 [ - + ]: 24622 : if (unlikely (readp + 4 > dataend))
7525 : : {
7526 : 0 : invalid_data:
7527 : 0 : error (0, 0, _("invalid data in section [%zu] '%s'"),
7528 : : elf_ndxscn (scn), scnname);
7529 : 0 : return;
7530 : : }
7531 : :
7532 : : /* At the beginning there must be a CIE. There can be multiple,
7533 : : hence we test tis in a loop. */
7534 : 24622 : ptrdiff_t offset = readp - (unsigned char *) data->d_buf;
7535 : :
7536 [ + + ]: 24622 : Dwarf_Word unit_length = read_4ubyte_unaligned_inc (dbg, readp);
7537 : 24622 : unsigned int length = 4;
7538 [ - + ]: 24622 : if (unlikely (unit_length == 0xffffffff))
7539 : : {
7540 [ # # ]: 0 : if (unlikely (readp + 8 > dataend))
7541 : 0 : goto invalid_data;
7542 : :
7543 [ # # ]: 0 : unit_length = read_8ubyte_unaligned_inc (dbg, readp);
7544 : 0 : length = 8;
7545 : : }
7546 : :
7547 [ + + ]: 24622 : if (unlikely (unit_length == 0))
7548 : : {
7549 : 140 : fprintf (out, _("\n [%6tx] Zero terminator\n"), offset);
7550 : 140 : continue;
7551 : : }
7552 : :
7553 : 24482 : Dwarf_Word maxsize = dataend - readp;
7554 [ - + ]: 24482 : if (unlikely (unit_length > maxsize))
7555 : 0 : goto invalid_data;
7556 : :
7557 [ + + ]: 24482 : unsigned int ptr_size = ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 4 : 8;
7558 : :
7559 : 24482 : ptrdiff_t start = readp - (unsigned char *) data->d_buf;
7560 : 24482 : const unsigned char *const cieend = readp + unit_length;
7561 [ - + ]: 24482 : if (unlikely (cieend > dataend))
7562 : 0 : goto invalid_data;
7563 : :
7564 : 24482 : Dwarf_Off cie_id;
7565 [ + - ]: 24482 : if (length == 4)
7566 : : {
7567 [ - + ]: 24482 : if (unlikely (cieend - readp < 4))
7568 : 0 : goto invalid_data;
7569 [ + + ]: 24482 : cie_id = read_4ubyte_unaligned_inc (dbg, readp);
7570 [ + + ]: 24482 : if (!is_eh_frame && cie_id == DW_CIE_ID_32)
7571 : : cie_id = DW_CIE_ID_64;
7572 : : }
7573 : : else
7574 : : {
7575 [ # # ]: 0 : if (unlikely (cieend - readp < 8))
7576 : 0 : goto invalid_data;
7577 [ # # ]: 0 : cie_id = read_8ubyte_unaligned_inc (dbg, readp);
7578 : : }
7579 : :
7580 : 48880 : uint_fast8_t version = 2;
7581 : 24398 : unsigned int code_alignment_factor;
7582 : 24398 : int data_alignment_factor;
7583 : 48880 : unsigned int fde_encoding = 0;
7584 : 48880 : unsigned int lsda_encoding = 0;
7585 : 48880 : Dwarf_Word initial_location = 0;
7586 : 48880 : Dwarf_Word vma_base = 0;
7587 : :
7588 [ + + + + ]: 24574 : if (cie_id == (is_eh_frame ? 0 : DW_CIE_ID_64))
7589 : : {
7590 [ - + ]: 310 : if (unlikely (cieend - readp < 2))
7591 : 0 : goto invalid_data;
7592 : 310 : version = *readp++;
7593 : 310 : const char *const augmentation = (const char *) readp;
7594 : 310 : readp = memchr (readp, '\0', cieend - readp);
7595 [ - + ]: 310 : if (unlikely (readp == NULL))
7596 : 0 : goto invalid_data;
7597 : 310 : ++readp;
7598 : :
7599 : 310 : uint_fast8_t segment_size = 0;
7600 [ + + ]: 310 : if (version >= 4)
7601 : : {
7602 [ - + ]: 8 : if (cieend - readp < 5)
7603 : 0 : goto invalid_data;
7604 : 8 : ptr_size = *readp++;
7605 : 8 : segment_size = *readp++;
7606 : : }
7607 : :
7608 [ - + ]: 310 : if (cieend - readp < 1)
7609 : 0 : goto invalid_data;
7610 : 310 : get_uleb128 (code_alignment_factor, readp, cieend);
7611 [ - + ]: 310 : if (cieend - readp < 1)
7612 : 0 : goto invalid_data;
7613 : 310 : get_sleb128 (data_alignment_factor, readp, cieend);
7614 : :
7615 : : /* In some variant for unwind data there is another field. */
7616 [ - + ]: 310 : if (strcmp (augmentation, "eh") == 0)
7617 [ # # ]: 0 : readp += ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 4 : 8;
7618 : :
7619 : 310 : unsigned int return_address_register;
7620 [ - + ]: 310 : if (cieend - readp < 1)
7621 : 0 : goto invalid_data;
7622 [ + + ]: 310 : if (unlikely (version == 1))
7623 : 286 : return_address_register = *readp++;
7624 : : else
7625 : 24 : get_uleb128 (return_address_register, readp, cieend);
7626 : :
7627 : 310 : fprintf (out, "\n [%6tx] CIE length=%" PRIu64 "\n"
7628 : : " CIE_id: %" PRIu64 "\n"
7629 : : " version: %u\n"
7630 : : " augmentation: \"%s\"\n",
7631 : : offset, (uint64_t) unit_length, (uint64_t) cie_id,
7632 : : version, augmentation);
7633 [ + + ]: 310 : if (version >= 4)
7634 : 8 : fprintf (out, " address_size: %u\n"
7635 : : " segment_size: %u\n",
7636 : : ptr_size, segment_size);
7637 : 310 : fprintf (out, " code_alignment_factor: %u\n"
7638 : : " data_alignment_factor: %d\n"
7639 : : " return_address_register: %u\n",
7640 : : code_alignment_factor,
7641 : : data_alignment_factor, return_address_register);
7642 : :
7643 [ + + ]: 310 : if (augmentation[0] == 'z')
7644 : : {
7645 [ - + ]: 222 : if (cieend - readp < 1)
7646 : 0 : goto invalid_data;
7647 : :
7648 : 222 : unsigned int augmentationlen;
7649 : 222 : get_uleb128 (augmentationlen, readp, cieend);
7650 : :
7651 [ - + ]: 222 : if (augmentationlen > (size_t) (cieend - readp))
7652 : : {
7653 : 0 : error (0, 0, _("invalid augmentation length"));
7654 : 0 : readp = cieend;
7655 : 0 : continue;
7656 : : }
7657 : :
7658 : 222 : const char *hdr = "Augmentation data:";
7659 : 222 : const char *cp = augmentation + 1;
7660 [ + + + - ]: 444 : while (*cp != '\0' && cp < augmentation + augmentationlen + 1)
7661 : : {
7662 : 222 : fprintf (out, " %-26s%#x ", hdr, *readp);
7663 : 222 : hdr = "";
7664 : :
7665 [ + - ]: 222 : if (*cp == 'R')
7666 : : {
7667 : 222 : fde_encoding = *readp++;
7668 : 222 : print_encoding_base (_("FDE address encoding: "),
7669 : : fde_encoding, out);
7670 : : }
7671 [ # # ]: 0 : else if (*cp == 'L')
7672 : : {
7673 : 0 : lsda_encoding = *readp++;
7674 : 0 : print_encoding_base (_("LSDA pointer encoding: "),
7675 : : lsda_encoding, out);
7676 : : }
7677 [ # # ]: 0 : else if (*cp == 'P')
7678 : : {
7679 : : /* Personality. This field usually has a relocation
7680 : : attached pointing to __gcc_personality_v0. */
7681 : 0 : const unsigned char *startp = readp;
7682 : 0 : unsigned int encoding = *readp++;
7683 : 0 : uint64_t val = 0;
7684 : 0 : readp = read_encoded (encoding, readp,
7685 : : readp - 1 + augmentationlen,
7686 : : &val, dbg);
7687 : :
7688 [ # # ]: 0 : while (++startp < readp)
7689 : 0 : fprintf (out, "%#x ", *startp);
7690 : :
7691 : 0 : fputc ('(', out);
7692 : 0 : print_encoding (encoding, out);
7693 : 0 : fputc (' ', out);
7694 [ # # ]: 0 : switch (encoding & 0xf)
7695 : : {
7696 : 0 : case DW_EH_PE_sleb128:
7697 : : case DW_EH_PE_sdata2:
7698 : : case DW_EH_PE_sdata4:
7699 : 0 : fprintf (out, "%" PRId64 ")\n", val);
7700 : 0 : break;
7701 : 0 : default:
7702 : 0 : fprintf (out, "%#" PRIx64 ")\n", val);
7703 : 0 : break;
7704 : : }
7705 : : }
7706 : : else
7707 : 0 : fprintf (out, "(%x)\n", *readp++);
7708 : :
7709 : 222 : ++cp;
7710 : : }
7711 : : }
7712 : :
7713 [ + - ]: 310 : if (likely (ptr_size == 4 || ptr_size == 8))
7714 : : {
7715 : 310 : struct cieinfo *newp = alloca (sizeof (*newp));
7716 : 310 : newp->cie_offset = offset;
7717 : 310 : newp->augmentation = augmentation;
7718 : 310 : newp->fde_encoding = fde_encoding;
7719 : 310 : newp->lsda_encoding = lsda_encoding;
7720 : 310 : newp->address_size = ptr_size;
7721 : 310 : newp->code_alignment_factor = code_alignment_factor;
7722 : 310 : newp->data_alignment_factor = data_alignment_factor;
7723 : 310 : newp->next = cies;
7724 : 310 : cies = newp;
7725 : : }
7726 : : }
7727 : : else
7728 : : {
7729 : : struct cieinfo *cie = cies;
7730 [ + - ]: 24172 : while (cie != NULL)
7731 [ + + - + ]: 48344 : if (is_eh_frame
7732 : 24080 : ? ((Dwarf_Off) start - cie_id) == (Dwarf_Off) cie->cie_offset
7733 : 92 : : cie_id == (Dwarf_Off) cie->cie_offset)
7734 : : break;
7735 : : else
7736 : 0 : cie = cie->next;
7737 : 24172 : if (unlikely (cie == NULL))
7738 : : {
7739 : 0 : fputs ("invalid CIE reference in FDE\n", out);
7740 : 0 : return;
7741 : : }
7742 : :
7743 : : /* Initialize from CIE data. */
7744 : 24172 : fde_encoding = cie->fde_encoding;
7745 : 24172 : lsda_encoding = cie->lsda_encoding;
7746 : 24172 : ptr_size = encoded_ptr_size (fde_encoding, cie->address_size);
7747 : 24172 : code_alignment_factor = cie->code_alignment_factor;
7748 : 24172 : data_alignment_factor = cie->data_alignment_factor;
7749 : :
7750 : 24172 : const unsigned char *base = readp;
7751 : : // XXX There are sometimes relocations for this value
7752 [ - + + + : 24172 : initial_location = read_addr_unaligned_inc (ptr_size, dbg, readp);
- + + + ]
7753 : 48344 : Dwarf_Word address_range
7754 [ + + - + : 24172 : = read_addr_unaligned_inc (ptr_size, dbg, readp);
+ + ]
7755 : :
7756 : : /* pcrel for an FDE address is relative to the runtime
7757 : : address of the start_address field itself. Sign extend
7758 : : if necessary to make sure the calculation is done on the
7759 : : full 64 bit address even when initial_location only holds
7760 : : the lower 32 bits. */
7761 : 24172 : Dwarf_Addr pc_start = initial_location;
7762 [ + + ]: 24172 : if (ptr_size == 4)
7763 : 24092 : pc_start = (uint64_t) (int32_t) pc_start;
7764 [ + + ]: 24172 : if ((fde_encoding & 0x70) == DW_EH_PE_pcrel)
7765 : 24068 : pc_start += ((uint64_t) shdr->sh_addr
7766 : 24068 : + (base - (const unsigned char *) data->d_buf)
7767 : 24068 : - bias);
7768 : :
7769 : 24172 : fprintf (out, "\n [%6tx] FDE length=%" PRIu64 " cie=[%6tx]\n"
7770 : : " CIE_pointer: %" PRIu64 "\n"
7771 : : " initial_location: ",
7772 : : offset, (uint64_t) unit_length,
7773 : : cie->cie_offset, (uint64_t) cie_id);
7774 : 24172 : print_dwarf_addr (dwflmod, cie->address_size,
7775 : : pc_start, initial_location, out);
7776 [ + + ]: 24172 : if ((fde_encoding & 0x70) == DW_EH_PE_pcrel)
7777 : : {
7778 : 48136 : vma_base = (((uint64_t) shdr->sh_offset
7779 : 24068 : + (base - (const unsigned char *) data->d_buf)
7780 : 24068 : + (uint64_t) initial_location)
7781 : : & (ptr_size == 4
7782 : : ? UINT64_C (0xffffffff)
7783 [ - + ]: 24068 : : UINT64_C (0xffffffffffffffff)));
7784 : 24068 : fprintf (out, _(" (offset: %#" PRIx64 ")"),
7785 : : (uint64_t) vma_base);
7786 : : }
7787 : :
7788 : 24172 : fprintf (out, "\n address_range: %#" PRIx64,
7789 : : (uint64_t) address_range);
7790 [ + + ]: 24172 : if ((fde_encoding & 0x70) == DW_EH_PE_pcrel)
7791 : 24068 : fprintf (out, _(" (end offset: %#" PRIx64 ")"),
7792 : 24068 : ((uint64_t) vma_base + (uint64_t) address_range)
7793 : : & (ptr_size == 4
7794 : : ? UINT64_C (0xffffffff)
7795 [ - + ]: 24068 : : UINT64_C (0xffffffffffffffff)));
7796 : 24172 : fputc ('\n', out);
7797 : :
7798 [ + + ]: 24172 : if (cie->augmentation[0] == 'z')
7799 : : {
7800 : 24068 : unsigned int augmentationlen;
7801 [ - + ]: 24068 : if (cieend - readp < 1)
7802 : 0 : goto invalid_data;
7803 : 24068 : get_uleb128 (augmentationlen, readp, cieend);
7804 : :
7805 [ - + ]: 24068 : if (augmentationlen > (size_t) (cieend - readp))
7806 : : {
7807 : 0 : error (0, 0, _("invalid augmentation length"));
7808 : 0 : readp = cieend;
7809 : 0 : continue;
7810 : : }
7811 : :
7812 [ - + ]: 24068 : if (augmentationlen > 0)
7813 : : {
7814 : 0 : const char *hdr = "Augmentation data:";
7815 : 0 : const char *cp = cie->augmentation + 1;
7816 : 0 : unsigned int u = 0;
7817 : 0 : while (*cp != '\0'
7818 [ # # # # ]: 0 : && cp < cie->augmentation + augmentationlen + 1)
7819 : : {
7820 [ # # ]: 0 : if (*cp == 'L')
7821 : : {
7822 : 0 : uint64_t lsda_pointer;
7823 : 0 : const unsigned char *p
7824 : 0 : = read_encoded (lsda_encoding, &readp[u],
7825 : : &readp[augmentationlen],
7826 : : &lsda_pointer, dbg);
7827 : 0 : u = p - readp;
7828 : 0 : fprintf (out, _("\
7829 : : %-26sLSDA pointer: %#" PRIx64 "\n"),
7830 : : hdr, lsda_pointer);
7831 : 0 : hdr = "";
7832 : : }
7833 : 0 : ++cp;
7834 : : }
7835 : :
7836 [ # # ]: 0 : while (u < augmentationlen)
7837 : : {
7838 : 0 : fprintf (out, " %-26s%#x\n", hdr, readp[u++]);
7839 : 0 : hdr = "";
7840 : : }
7841 : : }
7842 : :
7843 : 24068 : readp += augmentationlen;
7844 : : }
7845 : : }
7846 : :
7847 : : /* Handle the initialization instructions. */
7848 [ - + ]: 24482 : if (ptr_size != 4 && ptr_size !=8)
7849 : 0 : fprintf (out, "invalid CIE pointer size (%u), must be 4 or 8.\n",
7850 : : ptr_size);
7851 : : else
7852 : 24482 : print_cfa_program (readp, cieend, vma_base, code_alignment_factor,
7853 : : data_alignment_factor, version, ptr_size,
7854 : : fde_encoding, dwflmod, ebl, ehdr, dbg, out);
7855 : 24482 : readp = cieend;
7856 : : }
7857 : : }
7858 : :
7859 : :
7860 : : /* Returns the signedness (or false if it cannot be determined) and
7861 : : the byte size (or zero if it cannot be gotten) of the given DIE
7862 : : DW_AT_type attribute. Uses dwarf_peel_type and dwarf_aggregate_size. */
7863 : : static void
7864 : 353704 : die_type_sign_bytes (Dwarf_Die *die, bool *is_signed, int *bytes)
7865 : : {
7866 : 353704 : Dwarf_Attribute attr;
7867 : 353704 : Dwarf_Die type;
7868 : :
7869 : 353704 : *bytes = 0;
7870 : 353704 : *is_signed = false;
7871 : :
7872 [ + + ]: 353704 : if (dwarf_peel_type (dwarf_formref_die (dwarf_attr_integrate (die,
7873 : : DW_AT_type,
7874 : : &attr), &type),
7875 : : &type) == 0)
7876 : : {
7877 : 974 : Dwarf_Word val;
7878 : 974 : *is_signed = (dwarf_formudata (dwarf_attr (&type, DW_AT_encoding,
7879 : : &attr), &val) == 0
7880 [ + + + + ]: 974 : && (val == DW_ATE_signed || val == DW_ATE_signed_char));
7881 : :
7882 [ + - ]: 974 : if (dwarf_aggregate_size (&type, &val) == 0)
7883 : 974 : *bytes = val;
7884 : : }
7885 : 353704 : }
7886 : :
7887 : : struct attrcb_args
7888 : : {
7889 : : Dwfl_Module *dwflmod;
7890 : : Dwarf *dbg;
7891 : : Dwarf_Die *dies;
7892 : : int level;
7893 : : bool silent;
7894 : : bool is_split;
7895 : : unsigned int version;
7896 : : unsigned int addrsize;
7897 : : unsigned int offset_size;
7898 : : struct Dwarf_CU *cu;
7899 : : FILE *out;
7900 : : };
7901 : :
7902 : :
7903 : : static int
7904 : 8171008 : attr_callback (Dwarf_Attribute *attrp, void *arg)
7905 : : {
7906 : 8171008 : struct attrcb_args *cbargs = (struct attrcb_args *) arg;
7907 : 8171008 : const int level = cbargs->level;
7908 : 8171008 : Dwarf_Die *die = &cbargs->dies[level];
7909 : 8171008 : bool is_split = cbargs->is_split;
7910 : 8171008 : FILE *out = cbargs->out;
7911 : :
7912 [ + - ]: 8171008 : unsigned int attr = dwarf_whatattr (attrp);
7913 [ - + ]: 8171008 : if (unlikely (attr == 0))
7914 : : {
7915 [ # # ]: 0 : if (!cbargs->silent)
7916 : 0 : error (0, 0, _("DIE [%" PRIx64 "] "
7917 : : "cannot get attribute code: %s"),
7918 : : dwarf_dieoffset (die), dwarf_errmsg (-1));
7919 : 0 : return DWARF_CB_ABORT;
7920 : : }
7921 : :
7922 [ - + ]: 8171008 : unsigned int form = dwarf_whatform (attrp);
7923 [ - + ]: 8171008 : if (unlikely (form == 0))
7924 : : {
7925 [ # # ]: 0 : if (!cbargs->silent)
7926 : 0 : error (0, 0, _("DIE [%" PRIx64 "] "
7927 : : "cannot get attribute form: %s"),
7928 : : dwarf_dieoffset (die), dwarf_errmsg (-1));
7929 : 0 : return DWARF_CB_ABORT;
7930 : : }
7931 : :
7932 [ + + + + : 8171008 : switch (form)
+ + + +
- ]
7933 : : {
7934 : 103118 : case DW_FORM_addr:
7935 : : case DW_FORM_addrx:
7936 : : case DW_FORM_addrx1:
7937 : : case DW_FORM_addrx2:
7938 : : case DW_FORM_addrx3:
7939 : : case DW_FORM_addrx4:
7940 : : case DW_FORM_GNU_addr_index:
7941 [ + + ]: 103118 : if (!cbargs->silent)
7942 : : {
7943 : 102576 : Dwarf_Addr addr;
7944 [ - + ]: 102576 : if (unlikely (dwarf_formaddr (attrp, &addr) != 0))
7945 : : {
7946 : 0 : attrval_out:
7947 [ # # ]: 0 : if (!cbargs->silent)
7948 : 0 : error (0, 0, _("DIE [%" PRIx64 "] "
7949 : : "cannot get attribute '%s' (%s) value: "
7950 : : "%s"),
7951 : : dwarf_dieoffset (die),
7952 : : dwarf_attr_name (attr),
7953 : : dwarf_form_name (form),
7954 : : dwarf_errmsg (-1));
7955 : : /* Don't ABORT, it might be other attributes can be resolved. */
7956 : 0 : return DWARF_CB_OK;
7957 : : }
7958 [ + + ]: 102576 : if (form != DW_FORM_addr )
7959 : : {
7960 : 40 : Dwarf_Word word;
7961 [ - + ]: 40 : if (dwarf_formudata (attrp, &word) != 0)
7962 : 0 : goto attrval_out;
7963 : 40 : fprintf (out, " %*s%-20s (%s) [%" PRIx64 "] ",
7964 : : (int) (level * 2), "", dwarf_attr_name (attr),
7965 : : dwarf_form_name (form), word);
7966 : : }
7967 : : else
7968 : 102536 : fprintf (out, " %*s%-20s (%s) ",
7969 : : (int) (level * 2), "", dwarf_attr_name (attr),
7970 : : dwarf_form_name (form));
7971 : 102576 : print_dwarf_addr (cbargs->dwflmod, cbargs->addrsize, addr, addr, out);
7972 : 102576 : fprintf (out, "\n");
7973 : : }
7974 : 7347574 : break;
7975 : :
7976 : 1349324 : case DW_FORM_indirect:
7977 : : case DW_FORM_strp:
7978 : : case DW_FORM_line_strp:
7979 : : case DW_FORM_strx:
7980 : : case DW_FORM_strx1:
7981 : : case DW_FORM_strx2:
7982 : : case DW_FORM_strx3:
7983 : : case DW_FORM_strx4:
7984 : : case DW_FORM_string:
7985 : : case DW_FORM_GNU_strp_alt:
7986 : : case DW_FORM_GNU_str_index:
7987 [ + + ]: 1349324 : if (cbargs->silent)
7988 : : break;
7989 : 1347814 : const char *str = dwarf_formstring (attrp);
7990 [ - + ]: 1347814 : if (unlikely (str == NULL))
7991 : 0 : goto attrval_out;
7992 : 1347814 : fprintf (out, " %*s%-20s (%s) \"%s\"\n",
7993 : : (int) (level * 2), "", dwarf_attr_name (attr),
7994 : : dwarf_form_name (form), str);
7995 : 1347814 : break;
7996 : :
7997 : 1778018 : case DW_FORM_ref_addr:
7998 : : case DW_FORM_ref_udata:
7999 : : case DW_FORM_ref8:
8000 : : case DW_FORM_ref4:
8001 : : case DW_FORM_ref2:
8002 : : case DW_FORM_ref1:
8003 : : case DW_FORM_GNU_ref_alt:
8004 : : case DW_FORM_ref_sup4:
8005 : : case DW_FORM_ref_sup8:
8006 [ + + ]: 1778018 : if (cbargs->silent)
8007 : : break;
8008 : 1776438 : Dwarf_Die ref;
8009 [ - + ]: 1776438 : if (unlikely (dwarf_formref_die (attrp, &ref) == NULL))
8010 : 0 : goto attrval_out;
8011 : :
8012 : 1776438 : fprintf (out, " %*s%-20s (%s) ",
8013 : : (int) (level * 2), "", dwarf_attr_name (attr),
8014 : : dwarf_form_name (form));
8015 [ + + ]: 1776438 : if (is_split)
8016 : 66 : fprintf (out, "{%6" PRIxMAX "}\n", (uintmax_t) dwarf_dieoffset (&ref));
8017 : : else
8018 : 1776372 : fprintf (out, "[%6" PRIxMAX "]\n", (uintmax_t) dwarf_dieoffset (&ref));
8019 : : break;
8020 : :
8021 : 8 : case DW_FORM_ref_sig8:
8022 [ + - ]: 8 : if (cbargs->silent)
8023 : : break;
8024 : 8 : fprintf (out, " %*s%-20s (%s) {%6" PRIx64 "}\n",
8025 : : (int) (level * 2), "", dwarf_attr_name (attr),
8026 : : dwarf_form_name (form),
8027 [ - + ]: 8 : (uint64_t) read_8ubyte_unaligned (attrp->cu->dbg, attrp->valp));
8028 : 8 : break;
8029 : :
8030 : 4536454 : case DW_FORM_sec_offset:
8031 : : case DW_FORM_rnglistx:
8032 : : case DW_FORM_loclistx:
8033 : : case DW_FORM_implicit_const:
8034 : : case DW_FORM_udata:
8035 : : case DW_FORM_sdata:
8036 : : case DW_FORM_data8: /* Note no data16 here, we see that as block. */
8037 : : case DW_FORM_data4:
8038 : : case DW_FORM_data2:
8039 : 4536454 : case DW_FORM_data1:;
8040 : 4536454 : Dwarf_Word num;
8041 [ - + ]: 4536454 : if (unlikely (dwarf_formudata (attrp, &num) != 0))
8042 : 0 : goto attrval_out;
8043 : :
8044 : 4536454 : const char *valuestr = NULL;
8045 : 4536454 : bool as_hex_id = false;
8046 [ + + + + : 4536454 : switch (attr)
+ + + + +
+ - - - -
- - + - +
+ + ]
8047 : : {
8048 : : /* This case can take either a constant or a loclistptr. */
8049 : 565408 : case DW_AT_data_member_location:
8050 [ + - ]: 565408 : if (form != DW_FORM_sec_offset
8051 [ + + ]: 565408 : && (cbargs->version >= 4
8052 [ + - ]: 47612 : || (form != DW_FORM_data4 && form != DW_FORM_data8)))
8053 : : {
8054 [ + - ]: 565408 : if (!cbargs->silent)
8055 : 565408 : fprintf (out, " %*s%-20s (%s) %" PRIuMAX "\n",
8056 : : (int) (level * 2), "", dwarf_attr_name (attr),
8057 : : dwarf_form_name (form), (uintmax_t) num);
8058 : 565408 : return DWARF_CB_OK;
8059 : : }
8060 : 222210 : FALLTHROUGH;
8061 : :
8062 : : /* These cases always take a loclist[ptr] and no constant. */
8063 : : case DW_AT_location:
8064 : : case DW_AT_data_location:
8065 : : case DW_AT_vtable_elem_location:
8066 : : case DW_AT_string_length:
8067 : : case DW_AT_use_location:
8068 : : case DW_AT_frame_base:
8069 : : case DW_AT_return_addr:
8070 : : case DW_AT_static_link:
8071 : : case DW_AT_segment:
8072 : : case DW_AT_GNU_call_site_value:
8073 : : case DW_AT_GNU_call_site_data_value:
8074 : : case DW_AT_GNU_call_site_target:
8075 : : case DW_AT_GNU_call_site_target_clobbered:
8076 : : case DW_AT_GNU_locviews:
8077 : : {
8078 : 222210 : bool nlpt;
8079 [ + + ]: 222210 : if (cbargs->cu->version < 5)
8080 : : {
8081 [ + + ]: 324 : if (! cbargs->is_split)
8082 : : {
8083 : 284 : nlpt = notice_listptr (section_loc, &known_locsptr,
8084 : 284 : cbargs->addrsize,
8085 : 284 : cbargs->offset_size,
8086 : : cbargs->cu, num, attr);
8087 : : }
8088 : : else
8089 : : nlpt = true;
8090 : : }
8091 : : else
8092 : : {
8093 : : /* Only register for a real section offset. Otherwise
8094 : : it is a DW_FORM_loclistx which is just an index
8095 : : number and we should already have registered the
8096 : : section offset for the index when we saw the
8097 : : DW_AT_loclists_base CU attribute. */
8098 [ + + ]: 221886 : if (form == DW_FORM_sec_offset)
8099 : 221808 : nlpt = notice_listptr (section_loc, &known_loclistsptr,
8100 : 221808 : cbargs->addrsize, cbargs->offset_size,
8101 : : cbargs->cu, num, attr);
8102 : : else
8103 : : nlpt = true;
8104 : :
8105 : : }
8106 : :
8107 [ + + ]: 222210 : if (!cbargs->silent)
8108 : : {
8109 [ + + + + ]: 221844 : if (cbargs->cu->version < 5 || form == DW_FORM_sec_offset)
8110 [ - + ]: 221830 : fprintf (out, " %*s%-20s (%s) location list [%6"
8111 : : PRIxMAX "]%s\n",
8112 : : (int) (level * 2), "", dwarf_attr_name (attr),
8113 : : dwarf_form_name (form), (uintmax_t) num,
8114 : : nlpt ? "" : " <WARNING offset too big>");
8115 : : else
8116 : 14 : fprintf (out, " %*s%-20s (%s) location index [%6"
8117 : : PRIxMAX "]\n",
8118 : : (int) (level * 2), "", dwarf_attr_name (attr),
8119 : : dwarf_form_name (form), (uintmax_t) num);
8120 : : }
8121 : : }
8122 : : return DWARF_CB_OK;
8123 : :
8124 : 10 : case DW_AT_loclists_base:
8125 : : {
8126 : 20 : bool nlpt = notice_listptr (section_loc, &known_loclistsptr,
8127 : 10 : cbargs->addrsize, cbargs->offset_size,
8128 : : cbargs->cu, num, attr);
8129 : :
8130 [ + + ]: 10 : if (!cbargs->silent)
8131 [ - + ]: 2 : fprintf (out, " %*s%-20s (%s) location list [%6" PRIxMAX "]%s\n",
8132 : : (int) (level * 2), "", dwarf_attr_name (attr),
8133 : : dwarf_form_name (form), (uintmax_t) num,
8134 : : nlpt ? "" : " <WARNING offset too big>");
8135 : : }
8136 : : return DWARF_CB_OK;
8137 : :
8138 : 35756 : case DW_AT_ranges:
8139 : : case DW_AT_start_scope:
8140 : : {
8141 : 35756 : bool nlpt;
8142 [ + + ]: 35756 : if (cbargs->cu->version < 5)
8143 : 106 : nlpt = notice_listptr (section_ranges, &known_rangelistptr,
8144 : 106 : cbargs->addrsize, cbargs->offset_size,
8145 : : cbargs->cu, num, attr);
8146 : : else
8147 : : {
8148 : : /* Only register for a real section offset. Otherwise
8149 : : it is a DW_FORM_rangelistx which is just an index
8150 : : number and we should already have registered the
8151 : : section offset for the index when we saw the
8152 : : DW_AT_rnglists_base CU attribute. */
8153 [ + + ]: 35650 : if (form == DW_FORM_sec_offset)
8154 : 35638 : nlpt = notice_listptr (section_ranges, &known_rnglistptr,
8155 : 35638 : cbargs->addrsize, cbargs->offset_size,
8156 : : cbargs->cu, num, attr);
8157 : : else
8158 : : nlpt = true;
8159 : : }
8160 : :
8161 [ + + ]: 35756 : if (!cbargs->silent)
8162 : : {
8163 [ + + + + ]: 35670 : if (cbargs->cu->version < 5 || form == DW_FORM_sec_offset)
8164 [ - + ]: 35666 : fprintf (out, " %*s%-20s (%s) range list [%6"
8165 : : PRIxMAX "]%s\n",
8166 : : (int) (level * 2), "", dwarf_attr_name (attr),
8167 : : dwarf_form_name (form), (uintmax_t) num,
8168 : : nlpt ? "" : " <WARNING offset too big>");
8169 : : else
8170 : 4 : fprintf (out, " %*s%-20s (%s) range index [%6"
8171 : : PRIxMAX "]\n",
8172 : : (int) (level * 2), "", dwarf_attr_name (attr),
8173 : : dwarf_form_name (form), (uintmax_t) num);
8174 : : }
8175 : : }
8176 : : return DWARF_CB_OK;
8177 : :
8178 : 8 : case DW_AT_rnglists_base:
8179 : : {
8180 : 16 : bool nlpt = notice_listptr (section_ranges, &known_rnglistptr,
8181 : 8 : cbargs->addrsize, cbargs->offset_size,
8182 : : cbargs->cu, num, attr);
8183 [ + + ]: 8 : if (!cbargs->silent)
8184 [ - + ]: 4 : fprintf (out, " %*s%-20s (%s) range list [%6"
8185 : : PRIxMAX "]%s\n",
8186 : : (int) (level * 2), "", dwarf_attr_name (attr),
8187 : : dwarf_form_name (form), (uintmax_t) num,
8188 : : nlpt ? "" : " <WARNING offset too big>");
8189 : : }
8190 : : return DWARF_CB_OK;
8191 : :
8192 : 34 : case DW_AT_addr_base:
8193 : : case DW_AT_GNU_addr_base:
8194 : : {
8195 : 68 : bool addrbase = notice_listptr (section_addr, &known_addrbases,
8196 : 34 : cbargs->addrsize,
8197 : 34 : cbargs->offset_size,
8198 : : cbargs->cu, num, attr);
8199 [ + + ]: 34 : if (!cbargs->silent)
8200 [ - + ]: 18 : fprintf (out, " %*s%-20s (%s) address base [%6"
8201 : : PRIxMAX "]%s\n",
8202 : : (int) (level * 2), "", dwarf_attr_name (attr),
8203 : : dwarf_form_name (form), (uintmax_t) num,
8204 : : addrbase ? "" : " <WARNING offset too big>");
8205 : : }
8206 : : return DWARF_CB_OK;
8207 : :
8208 : 8 : case DW_AT_str_offsets_base:
8209 : : {
8210 : 16 : bool stroffbase = notice_listptr (section_str, &known_stroffbases,
8211 : 8 : cbargs->addrsize,
8212 : 8 : cbargs->offset_size,
8213 : : cbargs->cu, num, attr);
8214 [ + - ]: 8 : if (!cbargs->silent)
8215 [ - + ]: 8 : fprintf (out, " %*s%-20s (%s) str offsets base [%6"
8216 : : PRIxMAX "]%s\n",
8217 : : (int) (level * 2), "", dwarf_attr_name (attr),
8218 : : dwarf_form_name (form), (uintmax_t) num,
8219 : : stroffbase ? "" : " <WARNING offset too big>");
8220 : : }
8221 : : return DWARF_CB_OK;
8222 : :
8223 : 3736 : case DW_AT_language:
8224 : 3736 : valuestr = dwarf_lang_name (num);
8225 : 3736 : break;
8226 : 4 : case DW_AT_language_name:
8227 : 4 : valuestr = dwarf_lname_name (num);
8228 : 4 : break;
8229 : 59422 : case DW_AT_encoding:
8230 : 59422 : valuestr = dwarf_encoding_name (num);
8231 : 59422 : break;
8232 : 0 : case DW_AT_accessibility:
8233 : 0 : valuestr = dwarf_access_name (num);
8234 : 0 : break;
8235 : 0 : case DW_AT_defaulted:
8236 : 0 : valuestr = dwarf_defaulted_name (num);
8237 : 0 : break;
8238 : 0 : case DW_AT_visibility:
8239 : 0 : valuestr = dwarf_visibility_name (num);
8240 : 0 : break;
8241 : 0 : case DW_AT_virtuality:
8242 : 0 : valuestr = dwarf_virtuality_name (num);
8243 : 0 : break;
8244 : 0 : case DW_AT_identifier_case:
8245 : 0 : valuestr = dwarf_identifier_case_name (num);
8246 : 0 : break;
8247 : 0 : case DW_AT_calling_convention:
8248 : 0 : valuestr = dwarf_calling_convention_name (num);
8249 : 0 : break;
8250 : 7294 : case DW_AT_inline:
8251 : 7294 : valuestr = dwarf_inline_name (num);
8252 : 7294 : break;
8253 : 0 : case DW_AT_ordering:
8254 : 0 : valuestr = dwarf_ordering_name (num);
8255 : 0 : break;
8256 : 996494 : case DW_AT_decl_file:
8257 : : case DW_AT_call_file:
8258 : : {
8259 [ + + ]: 996494 : if (cbargs->silent)
8260 : : break;
8261 : :
8262 : : /* Try to get the actual file, the current interface only
8263 : : gives us full paths, but we only want to show the file
8264 : : name for now. */
8265 : 995646 : Dwarf_Die cudie;
8266 [ + - ]: 995646 : if (dwarf_cu_die (cbargs->cu, &cudie,
8267 : : NULL, NULL, NULL, NULL, NULL, NULL) != NULL)
8268 : : {
8269 : 995646 : Dwarf_Files *files;
8270 : 995646 : size_t nfiles;
8271 [ + - ]: 995646 : if (dwarf_getsrcfiles (&cudie, &files, &nfiles) == 0)
8272 : : {
8273 : 995646 : valuestr = dwarf_filesrc (files, num, NULL, NULL);
8274 [ + - ]: 995646 : if (valuestr != NULL)
8275 : : {
8276 : 995646 : const char *filename = strrchr (valuestr, '/');
8277 [ - + ]: 995646 : if (filename != NULL)
8278 : 995646 : valuestr = filename + 1;
8279 : : }
8280 : : else
8281 : 0 : error (0, 0, _("invalid file (%" PRId64 "): %s"),
8282 : : num, dwarf_errmsg (-1));
8283 : : }
8284 : : else
8285 : 0 : error (0, 0, _("no srcfiles for CU [%" PRIx64 "]"),
8286 : : dwarf_dieoffset (&cudie));
8287 : : }
8288 : : else
8289 : 0 : error (0, 0, _("couldn't get DWARF CU: %s"),
8290 : : dwarf_errmsg (-1));
8291 : 995646 : if (valuestr == NULL)
8292 : : valuestr = "???";
8293 : : }
8294 : 995646 : break;
8295 : 26 : case DW_AT_GNU_dwo_id:
8296 : 26 : as_hex_id = true;
8297 : 26 : break;
8298 : :
8299 : : default:
8300 : : /* Nothing. */
8301 : : break;
8302 : : }
8303 : :
8304 [ + + ]: 3713020 : if (cbargs->silent)
8305 : : break;
8306 : :
8307 : : /* When highpc is in constant form it is relative to lowpc.
8308 : : In that case also show the address. */
8309 : 3709310 : Dwarf_Addr highpc;
8310 [ + + + - ]: 3709310 : if (attr == DW_AT_high_pc && dwarf_highpc (die, &highpc) == 0)
8311 : : {
8312 : 23988 : fprintf (out, " %*s%-20s (%s) %" PRIuMAX " (",
8313 : : (int) (level * 2), "", dwarf_attr_name (attr),
8314 : : dwarf_form_name (form), (uintmax_t) num);
8315 : 23988 : print_dwarf_addr (cbargs->dwflmod, cbargs->addrsize,
8316 : : highpc, highpc, out);
8317 : 23988 : fprintf (out, ")\n");
8318 : : }
8319 : : else
8320 : : {
8321 [ + + ]: 3685322 : if (as_hex_id)
8322 : : {
8323 : 4 : fprintf (out, " %*s%-20s (%s) 0x%.16" PRIx64 "\n",
8324 : : (int) (level * 2), "", dwarf_attr_name (attr),
8325 : : dwarf_form_name (form), num);
8326 : : }
8327 : : else
8328 : : {
8329 : 3685318 : Dwarf_Sword snum = 0;
8330 : 3685318 : bool is_signed;
8331 : 3685318 : int bytes = 0;
8332 [ + + ]: 3685318 : if (attr == DW_AT_const_value)
8333 : 353696 : die_type_sign_bytes (die, &is_signed, &bytes);
8334 : : else
8335 : 3331622 : is_signed = (form == DW_FORM_sdata
8336 : 3331622 : || form == DW_FORM_implicit_const);
8337 : :
8338 [ + + ]: 3685318 : if (is_signed)
8339 [ - + ]: 386034 : if (unlikely (dwarf_formsdata (attrp, &snum) != 0))
8340 : 0 : goto attrval_out;
8341 : :
8342 [ + + ]: 3685318 : if (valuestr == NULL)
8343 : : {
8344 : 2619826 : fprintf (out, " %*s%-20s (%s) ",
8345 : : (int) (level * 2), "", dwarf_attr_name (attr),
8346 : : dwarf_form_name (form));
8347 : : }
8348 : : else
8349 : : {
8350 : 1065492 : fprintf (out, " %*s%-20s (%s) %s (",
8351 : : (int) (level * 2), "", dwarf_attr_name (attr),
8352 : : dwarf_form_name (form), valuestr);
8353 : : }
8354 : :
8355 [ + + + + : 3685318 : switch (bytes)
+ ]
8356 : : {
8357 : 10 : case 1:
8358 [ + + ]: 10 : if (is_signed)
8359 : 2 : fprintf (out, "%" PRId8, (int8_t) snum);
8360 : : else
8361 : 8 : fprintf (out, "%" PRIu8, (uint8_t) num);
8362 : : break;
8363 : :
8364 : 4 : case 2:
8365 [ + + ]: 4 : if (is_signed)
8366 : 2 : fprintf (out, "%" PRId16, (int16_t) snum);
8367 : : else
8368 : 2 : fprintf (out, "%" PRIu16, (uint16_t) num);
8369 : : break;
8370 : :
8371 : 234 : case 4:
8372 [ + + ]: 234 : if (is_signed)
8373 : 210 : fprintf (out, "%" PRId32, (int32_t) snum);
8374 : : else
8375 : 24 : fprintf (out, "%" PRIu32, (uint32_t) num);
8376 : : break;
8377 : :
8378 : 718 : case 8:
8379 [ + + ]: 718 : if (is_signed)
8380 : 2 : fprintf (out, "%" PRId64, (int64_t) snum);
8381 : : else
8382 : 716 : fprintf (out, "%" PRIu64, (uint64_t) num);
8383 : : break;
8384 : :
8385 : 3684352 : default:
8386 [ + + ]: 3684352 : if (is_signed)
8387 : 385818 : fprintf (out, "%" PRIdMAX, (intmax_t) snum);
8388 : : else
8389 : 3298534 : fprintf (out, "%" PRIuMAX, (uintmax_t) num);
8390 : : break;
8391 : : }
8392 : :
8393 : : /* Make clear if we switched from a signed encoding to
8394 : : an unsigned value. */
8395 [ + + ]: 3685318 : if (attr == DW_AT_const_value
8396 [ + + ]: 353696 : && (form == DW_FORM_sdata || form == DW_FORM_implicit_const)
8397 [ + + ]: 2680 : && !is_signed)
8398 : 2662 : fprintf (out, " (%" PRIdMAX ")", (intmax_t) num);
8399 : :
8400 [ + + ]: 3685318 : if (valuestr == NULL)
8401 : 2619826 : fprintf (out, "\n");
8402 : : else
8403 : 1065492 : fprintf (out, ")\n");
8404 : : }
8405 : : }
8406 : : break;
8407 : :
8408 : 18244 : case DW_FORM_flag:
8409 [ + + ]: 18244 : if (cbargs->silent)
8410 : : break;
8411 : 18096 : bool flag;
8412 [ - + ]: 18096 : if (unlikely (dwarf_formflag (attrp, &flag) != 0))
8413 : 0 : goto attrval_out;
8414 : :
8415 : 18096 : fprintf (out, " %*s%-20s (%s) %s\n",
8416 : : (int) (level * 2), "", dwarf_attr_name (attr),
8417 [ + - ]: 18096 : dwarf_form_name (form), flag ? yes_str : no_str);
8418 : 18096 : break;
8419 : :
8420 : 162442 : case DW_FORM_flag_present:
8421 [ + + ]: 162442 : if (cbargs->silent)
8422 : : break;
8423 : 161888 : fprintf (out, " %*s%-20s (%s) %s\n",
8424 : : (int) (level * 2), "", dwarf_attr_name (attr),
8425 : : dwarf_form_name (form), yes_str);
8426 : 161888 : break;
8427 : :
8428 : 223400 : case DW_FORM_exprloc:
8429 : : case DW_FORM_block4:
8430 : : case DW_FORM_block2:
8431 : : case DW_FORM_block1:
8432 : : case DW_FORM_block:
8433 : : case DW_FORM_data16: /* DWARF5 calls this a constant class. */
8434 [ + + ]: 223400 : if (cbargs->silent)
8435 : : break;
8436 : 223138 : Dwarf_Block block;
8437 [ - + ]: 223138 : if (unlikely (dwarf_formblock (attrp, &block) != 0))
8438 : 0 : goto attrval_out;
8439 : :
8440 : 223138 : fprintf (out, " %*s%-20s (%s) ",
8441 : : (int) (level * 2), "", dwarf_attr_name (attr),
8442 : : dwarf_form_name (form));
8443 : :
8444 [ + + + ]: 223138 : switch (attr)
8445 : : {
8446 : 96280 : default:
8447 [ - + ]: 96280 : if (form != DW_FORM_exprloc)
8448 : : {
8449 : 0 : print_block (block.length, block.data, out);
8450 : 0 : break;
8451 : : }
8452 : 126850 : FALLTHROUGH;
8453 : :
8454 : : case DW_AT_location:
8455 : : case DW_AT_data_location:
8456 : : case DW_AT_data_member_location:
8457 : : case DW_AT_vtable_elem_location:
8458 : : case DW_AT_string_length:
8459 : : case DW_AT_use_location:
8460 : : case DW_AT_frame_base:
8461 : : case DW_AT_return_addr:
8462 : : case DW_AT_static_link:
8463 : : case DW_AT_allocated:
8464 : : case DW_AT_associated:
8465 : : case DW_AT_bit_size:
8466 : : case DW_AT_bit_offset:
8467 : : case DW_AT_bit_stride:
8468 : : case DW_AT_byte_size:
8469 : : case DW_AT_byte_stride:
8470 : : case DW_AT_count:
8471 : : case DW_AT_lower_bound:
8472 : : case DW_AT_upper_bound:
8473 : : case DW_AT_GNU_call_site_value:
8474 : : case DW_AT_GNU_call_site_data_value:
8475 : : case DW_AT_GNU_call_site_target:
8476 : : case DW_AT_GNU_call_site_target_clobbered:
8477 [ + + ]: 126850 : if (form == DW_FORM_exprloc
8478 [ + - ]: 140 : || (form != DW_FORM_data16
8479 [ + - ]: 140 : && attrp->cu->version < 4)) /* blocks were expressions. */
8480 : : {
8481 : 223130 : fputc ('\n', out);
8482 : 223130 : print_ops (cbargs->dwflmod, cbargs->dbg,
8483 : 223130 : 12 + level * 2, 12 + level * 2,
8484 : : cbargs->version, cbargs->addrsize, cbargs->offset_size,
8485 : 223130 : attrp->cu, block.length, block.data, out);
8486 : : }
8487 : : else
8488 : 0 : print_block (block.length, block.data, out);
8489 : : break;
8490 : :
8491 : 8 : case DW_AT_discr_list:
8492 [ - + ]: 8 : if (block.length == 0)
8493 : 0 : fputs ("<default>\n", out);
8494 [ + - ]: 8 : else if (form != DW_FORM_data16)
8495 : : {
8496 : 8 : const unsigned char *readp = block.data;
8497 : 8 : const unsigned char *readendp = readp + block.length;
8498 : :
8499 : : /* See if we are dealing with a signed or unsigned
8500 : : values. If the parent of this variant DIE is a
8501 : : variant_part then it will either have a discriminant
8502 : : which points to the member which type is the
8503 : : discriminant type. Or the variant_part itself has a
8504 : : type representing the discriminant. */
8505 : 8 : bool is_signed = false;
8506 [ + - ]: 8 : if (level > 0)
8507 : : {
8508 : 8 : Dwarf_Die *parent = &cbargs->dies[level - 1];
8509 [ + - ]: 8 : if (dwarf_tag (die) == DW_TAG_variant
8510 [ + - ]: 8 : && dwarf_tag (parent) == DW_TAG_variant_part)
8511 : : {
8512 : 8 : Dwarf_Die member;
8513 : 8 : Dwarf_Attribute discr_attr;
8514 : 8 : int bytes;
8515 [ + - ]: 8 : if (dwarf_formref_die (dwarf_attr (parent,
8516 : : DW_AT_discr,
8517 : : &discr_attr),
8518 : : &member) != NULL)
8519 : 8 : die_type_sign_bytes (&member, &is_signed, &bytes);
8520 : : else
8521 : 0 : die_type_sign_bytes (parent, &is_signed, &bytes);
8522 : : }
8523 : : }
8524 [ + + ]: 24 : while (readp < readendp)
8525 : : {
8526 : 16 : int d = (int) *readp++;
8527 : 16 : fprintf (out, "%s ", dwarf_discr_list_name (d));
8528 [ - + ]: 16 : if (readp >= readendp)
8529 : 0 : goto attrval_out;
8530 : :
8531 : 16 : Dwarf_Word val;
8532 : 16 : Dwarf_Sword sval;
8533 [ + + ]: 16 : if (d == DW_DSC_label)
8534 : : {
8535 [ + + ]: 8 : if (is_signed)
8536 : : {
8537 : 4 : get_sleb128 (sval, readp, readendp);
8538 : 4 : fprintf (out, "%" PRId64 "", sval);
8539 : : }
8540 : : else
8541 : : {
8542 : 4 : get_uleb128 (val, readp, readendp);
8543 : 4 : fprintf (out, "%" PRIu64 "", val);
8544 : : }
8545 : : }
8546 [ + - ]: 8 : else if (d == DW_DSC_range)
8547 : : {
8548 [ + + ]: 8 : if (is_signed)
8549 : : {
8550 : 6 : get_sleb128 (sval, readp, readendp);
8551 : 6 : fprintf (out, "%" PRId64 "..", sval);
8552 [ - + ]: 6 : if (readp >= readendp)
8553 : 0 : goto attrval_out;
8554 : 6 : get_sleb128 (sval, readp, readendp);
8555 : 6 : fprintf (out, "%" PRId64 "", sval);
8556 : : }
8557 : : else
8558 : : {
8559 : 2 : get_uleb128 (val, readp, readendp);
8560 : 2 : fprintf (out, "%" PRIu64 "..", val);
8561 [ - + ]: 2 : if (readp >= readendp)
8562 : 0 : goto attrval_out;
8563 : 2 : get_uleb128 (val, readp, readendp);
8564 : 2 : fprintf (out, "%" PRIu64 "", val);
8565 : : }
8566 : : }
8567 : : else
8568 : : {
8569 : 0 : print_block (readendp - readp, readp, out);
8570 : 0 : break;
8571 : : }
8572 [ + + ]: 16 : if (readp < readendp)
8573 : 8 : fprintf (out, ", ");
8574 : : }
8575 : 8 : fputc ('\n', out);
8576 : : }
8577 : : else
8578 : 0 : print_block (block.length, block.data, out);
8579 : : break;
8580 : : }
8581 : : break;
8582 : :
8583 : 0 : default:
8584 [ # # ]: 0 : if (cbargs->silent)
8585 : : break;
8586 : 0 : fprintf (out, " %*s%-20s (%s) ???\n",
8587 : : (int) (level * 2), "", dwarf_attr_name (attr),
8588 : : dwarf_form_name (form));
8589 : 0 : break;
8590 : : }
8591 : :
8592 : 7347574 : return DWARF_CB_OK;
8593 : : }
8594 : :
8595 : : static void
8596 : 208 : print_debug_units (Dwfl_Module *dwflmod,
8597 : : Ebl *ebl, GElf_Ehdr *ehdr __attribute__ ((unused)),
8598 : : Elf_Scn *scn, GElf_Shdr *shdr,
8599 : : Dwarf *dbg, bool debug_types, FILE *out)
8600 : : {
8601 [ + + + + ]: 208 : const bool silent = !(print_debug_sections & section_info) && !debug_types;
8602 : 208 : const char *secname = section_name (ebl, shdr);
8603 : :
8604 : : /* Check section actually exists. */
8605 [ + + ]: 208 : if (!silent)
8606 [ + - ]: 136 : if (get_debug_elf_data (dbg, ebl,
8607 : : debug_types ? IDX_debug_types : IDX_debug_info,
8608 : : scn) == NULL)
8609 : 0 : return;
8610 : :
8611 : 136 : if (!silent)
8612 : 136 : fprintf (out, _("\
8613 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n [Offset]\n"),
8614 : 136 : elf_ndxscn (scn), secname, (uint64_t) shdr->sh_offset);
8615 : :
8616 : : /* If the section is empty we don't have to do anything. */
8617 [ + + + - ]: 208 : if (!silent && shdr->sh_size == 0)
8618 : : return;
8619 : :
8620 : 208 : int maxdies = 20;
8621 : 208 : Dwarf_Die *dies = xmalloc (maxdies * sizeof (Dwarf_Die));
8622 : :
8623 : : /* New compilation unit. */
8624 : 208 : Dwarf_Half version;
8625 : :
8626 : 208 : Dwarf_Die result;
8627 : 208 : Dwarf_Off abbroffset;
8628 : 208 : uint8_t addrsize;
8629 : 208 : uint8_t offsize;
8630 : 208 : uint64_t unit_id;
8631 : 208 : Dwarf_Off subdie_off;
8632 : :
8633 : 208 : int unit_res;
8634 : 208 : Dwarf_CU *cu;
8635 : 208 : Dwarf_CU cu_mem;
8636 : 208 : uint8_t unit_type;
8637 : 208 : Dwarf_Die cudie;
8638 : :
8639 : : /* We cheat a little because we want to see only the CUs from .debug_info
8640 : : or .debug_types. We know the Dwarf_CU struct layout. Set it up at
8641 : : the end of .debug_info if we want .debug_types only. Check the returned
8642 : : Dwarf_CU is still in the expected section. */
8643 [ + + ]: 208 : if (debug_types)
8644 : : {
8645 : 2 : cu_mem.dbg = dbg;
8646 : 2 : cu_mem.end = dbg->sectiondata[IDX_debug_info]->d_size;
8647 : 2 : cu_mem.sec_idx = IDX_debug_info;
8648 : 2 : cu = &cu_mem;
8649 : : }
8650 : : else
8651 : 206 : cu = NULL;
8652 : :
8653 : : next_cu:
8654 : 3962 : unit_res = dwarf_get_units (dbg, cu, &cu, &version, &unit_type,
8655 : : &cudie, NULL);
8656 [ + + ]: 3962 : if (unit_res == 1)
8657 : 206 : goto do_return;
8658 : :
8659 [ - + ]: 3756 : if (unit_res == -1)
8660 : : {
8661 [ # # ]: 0 : if (!silent)
8662 : 0 : error (0, 0, _("cannot get next unit: %s"), dwarf_errmsg (-1));
8663 : 0 : goto do_return;
8664 : : }
8665 : :
8666 [ + + + + ]: 7508 : if (cu->sec_idx != (size_t) (debug_types ? IDX_debug_types : IDX_debug_info))
8667 : 2 : goto do_return;
8668 : :
8669 : 3754 : dwarf_cu_die (cu, &result, NULL, &abbroffset, &addrsize, &offsize,
8670 : : &unit_id, &subdie_off);
8671 : :
8672 [ + + ]: 3754 : if (!silent)
8673 : : {
8674 : 3654 : Dwarf_Off offset = cu->start;
8675 [ + + + - ]: 3654 : if (debug_types && version < 5)
8676 : 4 : {
8677 : 4 : Dwarf_Die typedie;
8678 : 4 : Dwarf_Off dieoffset;
8679 : 4 : dieoffset = dwarf_dieoffset (dwarf_offdie_types (dbg, cu->start
8680 : : + subdie_off,
8681 : : &typedie));
8682 : 4 : fprintf (out, " %s %" PRIu64 ":\n"
8683 : : " %s: %" PRIu16
8684 : : ", %s: %" PRIu64
8685 : : ", %s: %" PRIu8
8686 : : ", %s: %" PRIu8
8687 : : "\n %s: %#" PRIx64
8688 : : ", %s: %#" PRIx64 " [%" PRIx64 "]\n",
8689 : : type_unit_str, (uint64_t) offset, version_str, version,
8690 : : abbrev_offset_str, abbroffset, addr_size_str, addrsize,
8691 : : offset_size_str, offsize, type_sig_str, unit_id,
8692 : : type_offset_str, (uint64_t) subdie_off, dieoffset);
8693 : : }
8694 : : else
8695 : : {
8696 : 3650 : fprintf (out, " %s %" PRIu64 ":\n"
8697 : : " %s: %" PRIu16
8698 : : ", %s: %" PRIu64
8699 : : ", %s: %" PRIu8
8700 : : ", %s: %" PRIu8 "\n",
8701 : : cu_offset_at_str, (uint64_t) offset, version_str, version,
8702 : : abbrev_offset_str, abbroffset, addr_size_str, addrsize,
8703 : : offset_size_str, offsize);
8704 : :
8705 [ + + ]: 3650 : if (version >= 5 || (unit_type != DW_UT_compile
8706 [ + + ]: 170 : && unit_type != DW_UT_partial))
8707 : : {
8708 : 3482 : fprintf (out, " %s: %s (%" PRIu8 ")",
8709 : : unit_type_str, dwarf_unit_name (unit_type), unit_type);
8710 : 3482 : if (unit_type == DW_UT_type
8711 [ + + ]: 3482 : || unit_type == DW_UT_skeleton
8712 [ + - ]: 3472 : || unit_type == DW_UT_split_compile
8713 [ - + ]: 3472 : || unit_type == DW_UT_split_type)
8714 : 10 : fprintf (out, ", %s: 0x%.16" PRIx64 "", unit_id_str, unit_id);
8715 : 3482 : if (unit_type == DW_UT_type
8716 [ - + ]: 3482 : || unit_type == DW_UT_split_type)
8717 : : {
8718 : 0 : Dwarf_Die typedie;
8719 : 0 : Dwarf_Off dieoffset;
8720 : 0 : dwarf_cu_info (cu, NULL, NULL, NULL, &typedie,
8721 : : NULL, NULL, NULL);
8722 : 0 : dieoffset = dwarf_dieoffset (&typedie);
8723 : 0 : fprintf (out, ", %s: %#" PRIx64 " [%" PRIx64 "]",
8724 : : unit_die_off_str, subdie_off, dieoffset);
8725 : : }
8726 : 3482 : fprintf (out, "\n");
8727 : : }
8728 : : }
8729 : : }
8730 : :
8731 [ + - ]: 3754 : if (version < 2 || version > 5
8732 [ + - - + ]: 3754 : || unit_type < DW_UT_compile || unit_type > DW_UT_split_type)
8733 : : {
8734 [ # # ]: 0 : if (!silent)
8735 : 0 : error (0, 0, _("unknown version (%d) or unit type (%d)"),
8736 : : version, unit_type);
8737 : 0 : goto next_cu;
8738 : : }
8739 : :
8740 : 3754 : struct attrcb_args args =
8741 : : {
8742 : : .dwflmod = dwflmod,
8743 : : .silent = silent,
8744 : : .version = version,
8745 : : .addrsize = addrsize,
8746 : : .offset_size = offsize
8747 : : };
8748 : :
8749 : 3754 : bool is_split = false;
8750 : 3754 : int level = 0;
8751 : 3754 : dies[0] = cudie;
8752 : 3754 : args.cu = dies[0].cu;
8753 : 3754 : args.dbg = dbg;
8754 : 3754 : args.is_split = is_split;
8755 : 3754 : args.out = out;
8756 : :
8757 : : /* We might return here again for the split CU subdie. */
8758 : : do_cu:
8759 : 2149820 : do
8760 : : {
8761 : 2149820 : Dwarf_Off offset = dwarf_dieoffset (&dies[level]);
8762 [ - + ]: 2149820 : if (unlikely (offset == (Dwarf_Off) -1))
8763 : : {
8764 [ # # ]: 0 : if (!silent)
8765 : 0 : error (0, 0, _("cannot get DIE offset: %s"),
8766 : : dwarf_errmsg (-1));
8767 : 0 : goto do_return;
8768 : : }
8769 : :
8770 : 2149820 : int tag = dwarf_tag (&dies[level]);
8771 [ - + ]: 2149820 : if (unlikely (tag == DW_TAG_invalid))
8772 : : {
8773 [ # # ]: 0 : if (!silent)
8774 : 0 : error (0, 0, _("cannot get tag of DIE at offset [%" PRIx64
8775 : : "] in section '%s': %s"),
8776 : : (uint64_t) offset, secname, dwarf_errmsg (-1));
8777 : 0 : goto do_return;
8778 : : }
8779 : :
8780 [ + + ]: 2149820 : if (!silent)
8781 : : {
8782 : 2147834 : unsigned int code = dwarf_getabbrevcode (dies[level].abbrev);
8783 [ + + ]: 2147834 : if (is_split)
8784 : 96 : fprintf (out, " {%6" PRIx64 "} ", (uint64_t) offset);
8785 : : else
8786 : 2147738 : fprintf (out, " [%6" PRIx64 "] ", (uint64_t) offset);
8787 : 2147834 : fprintf (out, "%*s%-20s abbrev: %u\n", (int) (level * 2), "",
8788 : : dwarf_tag_name (tag), code);
8789 : : }
8790 : :
8791 : : /* Print the attribute values. */
8792 : 2149820 : args.level = level;
8793 : 2149820 : args.dies = dies;
8794 : 2149820 : (void) dwarf_getattrs (&dies[level], attr_callback, &args, 0);
8795 : :
8796 : : /* Make room for the next level's DIE. */
8797 [ - + ]: 2149820 : if (level + 1 == maxdies)
8798 : 0 : dies = xrealloc (dies, (maxdies += 10) * sizeof (Dwarf_Die));
8799 : :
8800 : 2149820 : int res = dwarf_child (&dies[level], &dies[level + 1]);
8801 [ + + ]: 2149820 : if (res > 0)
8802 : : {
8803 [ + + ]: 2149820 : while ((res = dwarf_siblingof (&dies[level], &dies[level])) == 1)
8804 [ + + ]: 297298 : if (level-- == 0)
8805 : : break;
8806 : :
8807 [ - + ]: 1856284 : if (unlikely (res == -1))
8808 : : {
8809 [ # # ]: 0 : if (!silent)
8810 : 0 : error (0, 0, _("cannot get next DIE: %s\n"),
8811 : : dwarf_errmsg (-1));
8812 : 0 : goto do_return;
8813 : : }
8814 : : }
8815 [ - + ]: 293536 : else if (unlikely (res < 0))
8816 : : {
8817 [ # # ]: 0 : if (!silent)
8818 : 0 : error (0, 0, _("cannot get next DIE: %s"),
8819 : : dwarf_errmsg (-1));
8820 : 0 : goto do_return;
8821 : : }
8822 : : else
8823 : : ++level;
8824 : : }
8825 [ + + ]: 2149820 : while (level >= 0);
8826 : :
8827 : : /* We might want to show the split compile unit if this was a skeleton.
8828 : : We need to scan it if we are requesting printing .debug_ranges for
8829 : : DWARF4 since GNU DebugFission uses "offsets" into the main ranges
8830 : : section. */
8831 [ + + ]: 3762 : if (unit_type == DW_UT_skeleton
8832 [ + + + + ]: 26 : && ((!silent && show_split_units)
8833 [ + + + + ]: 20 : || (version < 5 && (print_debug_sections & section_ranges) != 0)))
8834 : : {
8835 : 10 : Dwarf_Die subdie;
8836 [ + - ]: 10 : if (dwarf_cu_info (cu, NULL, NULL, NULL, &subdie, NULL, NULL, NULL) != 0
8837 [ + + ]: 10 : || dwarf_tag (&subdie) == DW_TAG_invalid)
8838 : : {
8839 [ + - ]: 2 : if (!silent)
8840 : : {
8841 : 2 : Dwarf_Attribute dwo_at;
8842 : 4 : const char *dwo_name =
8843 : 2 : (dwarf_formstring (dwarf_attr (&cudie, DW_AT_dwo_name,
8844 : : &dwo_at))
8845 [ - + ]: 2 : ?: (dwarf_formstring (dwarf_attr (&cudie, DW_AT_GNU_dwo_name,
8846 : : &dwo_at))
8847 [ # # ]: 0 : ?: "<unknown>"));
8848 : 2 : fprintf (stderr,
8849 : : "Could not find split unit '%s', id: %" PRIx64 "\n",
8850 : : dwo_name, unit_id);
8851 : : }
8852 : : }
8853 : : else
8854 : : {
8855 : 8 : Dwarf_CU *split_cu = subdie.cu;
8856 : 8 : dwarf_cu_die (split_cu, &result, NULL, &abbroffset,
8857 : : &addrsize, &offsize, &unit_id, &subdie_off);
8858 : 8 : Dwarf_Off offset = cu->start;
8859 : :
8860 [ + + ]: 8 : if (!silent)
8861 : : {
8862 : 4 : fprintf (out, _(" Split compilation unit at offset %"
8863 : : PRIu64 ":\n"
8864 : : " Version: %" PRIu16
8865 : : ", Abbreviation section offset: %" PRIu64
8866 : : ", Address size: %" PRIu8
8867 : : ", Offset size: %" PRIu8 "\n"),
8868 : : (uint64_t) offset, version, abbroffset,
8869 : : addrsize, offsize);
8870 : 4 : fprintf (out, _(" Unit type: %s (%" PRIu8 ")"),
8871 : : dwarf_unit_name (unit_type), unit_type);
8872 : 4 : fprintf (out, ", Unit id: 0x%.16" PRIx64 "", unit_id);
8873 : 4 : fprintf (out, "\n");
8874 : : }
8875 : :
8876 : 8 : unit_type = DW_UT_split_compile;
8877 : 8 : is_split = true;
8878 : 8 : level = 0;
8879 : 8 : dies[0] = subdie;
8880 : 8 : args.cu = dies[0].cu;
8881 : 8 : args.dbg = split_cu->dbg;
8882 : 8 : args.is_split = is_split;
8883 : 8 : goto do_cu;
8884 : : }
8885 : : }
8886 : :
8887 : : /* And again... */
8888 : 3754 : goto next_cu;
8889 : :
8890 : 208 : do_return:
8891 : 208 : free (dies);
8892 : : }
8893 : :
8894 : : static void
8895 : 206 : print_debug_info_section (Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr,
8896 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg,
8897 : : FILE *out)
8898 : : {
8899 : 42 : print_debug_units (dwflmod, ebl, ehdr, scn, shdr, dbg, false, out);
8900 : 42 : }
8901 : :
8902 : : static void
8903 : 2 : print_debug_types_section (Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr,
8904 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg,
8905 : : FILE *out)
8906 : : {
8907 : 2 : print_debug_units (dwflmod, ebl, ehdr, scn, shdr, dbg, true, out);
8908 : 2 : }
8909 : :
8910 : :
8911 : : static void
8912 : 82 : print_decoded_line_section (Dwfl_Module *dwflmod, Ebl *ebl,
8913 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
8914 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg,
8915 : : FILE *out)
8916 : : {
8917 : 82 : fprintf (out, _("\
8918 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n\n"),
8919 : : elf_ndxscn (scn), section_name (ebl, shdr),
8920 : 82 : (uint64_t) shdr->sh_offset);
8921 : :
8922 : 164 : size_t address_size
8923 [ + + ]: 82 : = elf_getident (ebl->elf, NULL)[EI_CLASS] == ELFCLASS32 ? 4 : 8;
8924 : :
8925 : 82 : Dwarf_Lines *lines;
8926 : 82 : size_t nlines;
8927 : 82 : Dwarf_Off off, next_off = 0;
8928 : 82 : Dwarf_CU *cu = NULL;
8929 : 82 : while (dwarf_next_lines (dbg, off = next_off, &next_off, &cu, NULL, NULL,
8930 [ + + ]: 176 : &lines, &nlines) == 0)
8931 : : {
8932 : 94 : Dwarf_Die cudie;
8933 [ + + + - ]: 94 : if (cu != NULL && dwarf_cu_info (cu, NULL, NULL, &cudie,
8934 : : NULL, NULL, NULL, NULL) == 0)
8935 : 50 : fprintf (out, " CU [%" PRIx64 "] %s\n",
8936 : : dwarf_dieoffset (&cudie), dwarf_diename (&cudie));
8937 : : else
8938 : : {
8939 : : /* DWARF5 lines can be independent of any CU, but they probably
8940 : : are used by some CU. Determine the CU this block is for. */
8941 : 44 : Dwarf_Off cuoffset;
8942 : 44 : Dwarf_Off ncuoffset = 0;
8943 : 44 : size_t hsize;
8944 : 44 : while (dwarf_nextcu (dbg, cuoffset = ncuoffset, &ncuoffset, &hsize,
8945 [ + - ]: 46 : NULL, NULL, NULL) == 0)
8946 : : {
8947 [ - + ]: 46 : if (dwarf_offdie (dbg, cuoffset + hsize, &cudie) == NULL)
8948 : 0 : continue;
8949 : 46 : Dwarf_Attribute stmt_list;
8950 [ - + ]: 46 : if (dwarf_attr (&cudie, DW_AT_stmt_list, &stmt_list) == NULL)
8951 : 0 : continue;
8952 : 46 : Dwarf_Word lineoff;
8953 [ - + ]: 46 : if (dwarf_formudata (&stmt_list, &lineoff) != 0)
8954 : 0 : continue;
8955 [ + + ]: 46 : if (lineoff == off)
8956 : : {
8957 : : /* Found the CU. */
8958 : 44 : cu = cudie.cu;
8959 : 44 : break;
8960 : : }
8961 : : }
8962 : :
8963 [ + - ]: 44 : if (cu != NULL)
8964 : 44 : fprintf (out, " CU [%" PRIx64 "] %s\n",
8965 : : dwarf_dieoffset (&cudie), dwarf_diename (&cudie));
8966 : : else
8967 : 0 : fprintf (out, " No CU\n");
8968 : : }
8969 : :
8970 : 94 : fprintf (out, " line:col SBPE* disc isa op address"
8971 : : " (Statement Block Prologue Epilogue *End)\n");
8972 : 94 : const char *last_file = "";
8973 [ + + ]: 720 : for (size_t n = 0; n < nlines; n++)
8974 : : {
8975 : 626 : Dwarf_Line *line = dwarf_onesrcline (lines, n);
8976 [ - + ]: 626 : if (line == NULL)
8977 : : {
8978 : 0 : fprintf (out, " dwarf_onesrcline: %s\n", dwarf_errmsg (-1));
8979 : 0 : continue;
8980 : : }
8981 : 626 : Dwarf_Word mtime, length;
8982 : 626 : const char *file = dwarf_linesrc (line, &mtime, &length);
8983 [ - + ]: 626 : if (file == NULL)
8984 : : {
8985 : 0 : fprintf (out, " <%s> (mtime: ?, length: ?)\n",
8986 : : dwarf_errmsg (-1));
8987 : 0 : last_file = "";
8988 : : }
8989 [ + + ]: 626 : else if (strcmp (last_file, file) != 0)
8990 : : {
8991 : 114 : fprintf (out, " %s (mtime: %" PRIu64 ", length: %" PRIu64 ")\n",
8992 : : file, mtime, length);
8993 : 114 : last_file = file;
8994 : : }
8995 : :
8996 : 626 : int lineno, colno;
8997 : 626 : bool statement, endseq, block, prologue_end, epilogue_begin;
8998 : 626 : unsigned int lineop, isa, disc;
8999 : 626 : Dwarf_Addr address;
9000 : 626 : dwarf_lineaddr (line, &address);
9001 : 626 : dwarf_lineno (line, &lineno);
9002 : 626 : dwarf_linecol (line, &colno);
9003 : 626 : dwarf_lineop_index (line, &lineop);
9004 : 626 : dwarf_linebeginstatement (line, &statement);
9005 : 626 : dwarf_lineendsequence (line, &endseq);
9006 : 626 : dwarf_lineblock (line, &block);
9007 : 626 : dwarf_lineprologueend (line, &prologue_end);
9008 : 626 : dwarf_lineepiloguebegin (line, &epilogue_begin);
9009 : 626 : dwarf_lineisa (line, &isa);
9010 : 626 : dwarf_linediscriminator (line, &disc);
9011 : :
9012 : : /* End sequence is special, it is one byte past. */
9013 : 626 : fprintf (out, " %4d:%-3d %c%c%c%c%c %4d %3d %2d ",
9014 : : lineno, colno,
9015 [ + + ]: 626 : (statement ? 'S' : ' '),
9016 [ + - ]: 626 : (block ? 'B' : ' '),
9017 [ + + ]: 626 : (prologue_end ? 'P' : ' '),
9018 [ + - ]: 626 : (epilogue_begin ? 'E' : ' '),
9019 [ + + ]: 626 : (endseq ? '*' : ' '),
9020 : : disc, isa, lineop);
9021 : 626 : print_dwarf_addr (dwflmod, address_size,
9022 [ + + ]: 626 : address - (endseq ? 1 : 0), address, out);
9023 : 626 : fprintf (out, "\n");
9024 : :
9025 [ + + ]: 626 : if (endseq)
9026 : 98 : fprintf(out, "\n");
9027 : : }
9028 : : }
9029 : 82 : }
9030 : :
9031 : :
9032 : : /* Print the value of a form.
9033 : : Returns new value of readp, or readendp on failure. */
9034 : : static const unsigned char *
9035 : 144006 : print_form_data (Dwarf *dbg, int form, const unsigned char *readp,
9036 : : const unsigned char *readendp, unsigned int offset_len,
9037 : : Dwarf_Off str_offsets_base, FILE *out)
9038 : : {
9039 : 144006 : Dwarf_Word val;
9040 : 144006 : const unsigned char *endp;
9041 : 144006 : Elf_Data *data;
9042 : 144006 : char *str;
9043 [ + - - - : 144006 : switch (form)
- + - - -
- + - - +
- + - - -
- - ]
9044 : : {
9045 : 16 : case DW_FORM_data1:
9046 [ - + ]: 16 : if (readendp - readp < 1)
9047 : : {
9048 : 0 : invalid_data:
9049 : 0 : error (0, 0, "invalid data");
9050 : 0 : return readendp;
9051 : : }
9052 : 16 : val = *readp++;
9053 : 16 : fprintf (out, " %" PRIx8, (unsigned int) val);
9054 : 16 : break;
9055 : :
9056 : 0 : case DW_FORM_data2:
9057 [ # # ]: 0 : if (readendp - readp < 2)
9058 : 0 : goto invalid_data;
9059 [ # # ]: 0 : val = read_2ubyte_unaligned_inc (dbg, readp);
9060 : 0 : fprintf(out, " %" PRIx16, (unsigned int) val);
9061 : 0 : break;
9062 : :
9063 : 0 : case DW_FORM_data4:
9064 [ # # ]: 0 : if (readendp - readp < 4)
9065 : 0 : goto invalid_data;
9066 [ # # ]: 0 : val = read_4ubyte_unaligned_inc (dbg, readp);
9067 : 0 : fprintf (out, " %" PRIx32, (unsigned int) val);
9068 : 0 : break;
9069 : :
9070 : 0 : case DW_FORM_data8:
9071 [ # # ]: 0 : if (readendp - readp < 8)
9072 : 0 : goto invalid_data;
9073 [ # # ]: 0 : val = read_8ubyte_unaligned_inc (dbg, readp);
9074 : 0 : fprintf (out, " %" PRIx64, val);
9075 : 0 : break;
9076 : :
9077 : 0 : case DW_FORM_sdata:
9078 [ # # ]: 0 : if (readendp - readp < 1)
9079 : 0 : goto invalid_data;
9080 : 0 : get_sleb128 (val, readp, readendp);
9081 : 0 : fprintf (out, " %" PRIx64, val);
9082 : 0 : break;
9083 : :
9084 : 58000 : case DW_FORM_udata:
9085 [ - + ]: 58000 : if (readendp - readp < 1)
9086 : 0 : goto invalid_data;
9087 : 58000 : get_uleb128 (val, readp, readendp);
9088 : 58000 : fprintf (out, " %" PRIx64, val);
9089 : 58000 : break;
9090 : :
9091 : 0 : case DW_FORM_block:
9092 [ # # ]: 0 : if (readendp - readp < 1)
9093 : 0 : goto invalid_data;
9094 : 0 : get_uleb128 (val, readp, readendp);
9095 [ # # ]: 0 : if ((size_t) (readendp - readp) < val)
9096 : 0 : goto invalid_data;
9097 : 0 : print_bytes (val, readp, out);
9098 : 0 : readp += val;
9099 : 0 : break;
9100 : :
9101 : 0 : case DW_FORM_block1:
9102 [ # # ]: 0 : if (readendp - readp < 1)
9103 : 0 : goto invalid_data;
9104 : 0 : val = *readp++;
9105 [ # # ]: 0 : if ((size_t) (readendp - readp) < val)
9106 : 0 : goto invalid_data;
9107 : 0 : print_bytes (val, readp, out);
9108 : 0 : readp += val;
9109 : 0 : break;
9110 : :
9111 : 0 : case DW_FORM_block2:
9112 [ # # ]: 0 : if (readendp - readp < 2)
9113 : 0 : goto invalid_data;
9114 [ # # ]: 0 : val = read_2ubyte_unaligned_inc (dbg, readp);
9115 [ # # ]: 0 : if ((size_t) (readendp - readp) < val)
9116 : 0 : goto invalid_data;
9117 : 0 : print_bytes (val, readp, out);
9118 : 0 : readp += val;
9119 : 0 : break;
9120 : :
9121 : 0 : case DW_FORM_block4:
9122 [ # # ]: 0 : if (readendp - readp < 4)
9123 : 0 : goto invalid_data;
9124 [ # # ]: 0 : val = read_4ubyte_unaligned_inc (dbg, readp);
9125 [ # # ]: 0 : if ((size_t) (readendp - readp) < val)
9126 : 0 : goto invalid_data;
9127 : 0 : print_bytes (val, readp, out);
9128 : 0 : readp += val;
9129 : 0 : break;
9130 : :
9131 : 24 : case DW_FORM_data16:
9132 [ - + ]: 24 : if (readendp - readp < 16)
9133 : 0 : goto invalid_data;
9134 : 24 : print_bytes (16, readp, out);
9135 : 24 : readp += 16;
9136 : 24 : break;
9137 : :
9138 : 0 : case DW_FORM_flag:
9139 [ # # ]: 0 : if (readendp - readp < 1)
9140 : 0 : goto invalid_data;
9141 : 0 : val = *readp++;
9142 [ # # ]: 0 : fprintf (out, "%s", val != 0 ? yes_str : no_str);
9143 : 0 : break;
9144 : :
9145 : 0 : case DW_FORM_string:
9146 : 0 : endp = memchr (readp, '\0', readendp - readp);
9147 [ # # ]: 0 : if (endp == NULL)
9148 : 0 : goto invalid_data;
9149 : 0 : fprintf (out, "%s", readp);
9150 : 0 : readp = endp + 1;
9151 : 0 : break;
9152 : :
9153 : 84410 : case DW_FORM_strp:
9154 : : case DW_FORM_line_strp:
9155 : : case DW_FORM_strp_sup:
9156 [ - + ]: 84410 : if ((size_t) (readendp - readp) < offset_len)
9157 : 0 : goto invalid_data;
9158 [ - + ]: 84410 : if (offset_len == 8)
9159 [ # # ]: 0 : val = read_8ubyte_unaligned_inc (dbg, readp);
9160 : : else
9161 [ - + ]: 84410 : val = read_4ubyte_unaligned_inc (dbg, readp);
9162 [ - + ]: 84410 : if (form == DW_FORM_strp)
9163 : 0 : data = dbg->sectiondata[IDX_debug_str];
9164 [ + - ]: 84410 : else if (form == DW_FORM_line_strp)
9165 : 84410 : data = dbg->sectiondata[IDX_debug_line_str];
9166 : : else /* form == DW_FORM_strp_sup */
9167 : : {
9168 : 0 : Dwarf *alt = dwarf_getalt (dbg);
9169 [ # # ]: 0 : data = alt != NULL ? alt->sectiondata[IDX_debug_str] : NULL;
9170 : : }
9171 [ + - - + ]: 84410 : if (data == NULL || val >= data->d_size
9172 [ - + ]: 84410 : || memchr (data->d_buf + val, '\0', data->d_size - val) == NULL)
9173 : : str = "???";
9174 : : else
9175 : 84410 : str = (char *) data->d_buf + val;
9176 : 84410 : fprintf (out, "%s (%" PRIu64 ")", str, val);
9177 : 84410 : break;
9178 : :
9179 : 0 : case DW_FORM_sec_offset:
9180 [ # # ]: 0 : if ((size_t) (readendp - readp) < offset_len)
9181 : 0 : goto invalid_data;
9182 [ # # ]: 0 : if (offset_len == 8)
9183 [ # # ]: 0 : val = read_8ubyte_unaligned_inc (dbg, readp);
9184 : : else
9185 [ # # ]: 0 : val = read_4ubyte_unaligned_inc (dbg, readp);
9186 : 0 : fprintf (out, "[%" PRIx64 "]", val);
9187 : 0 : break;
9188 : :
9189 : 1556 : case DW_FORM_strx:
9190 : : case DW_FORM_GNU_str_index:
9191 [ - + ]: 1556 : if (readendp - readp < 1)
9192 : 0 : goto invalid_data;
9193 : 1556 : get_uleb128 (val, readp, readendp);
9194 : 1556 : strx_val:
9195 : 1556 : data = dbg->sectiondata[IDX_debug_str_offsets];
9196 [ + - ]: 1556 : if (data == NULL
9197 [ - + ]: 1556 : || data->d_size - str_offsets_base < val * offset_len)
9198 : : str = "???";
9199 : : else
9200 : : {
9201 : 1556 : const unsigned char *strreadp = (data->d_buf + str_offsets_base
9202 : 1556 : + val * offset_len);
9203 : 1556 : const unsigned char *strreadendp = data->d_buf + data->d_size;
9204 [ - + ]: 1556 : if ((size_t) (strreadendp - strreadp) < offset_len)
9205 : : str = "???";
9206 : : else
9207 : : {
9208 : 1556 : Dwarf_Off idx;
9209 [ - + ]: 1556 : if (offset_len == 8)
9210 [ # # ]: 0 : idx = read_8ubyte_unaligned (dbg, strreadp);
9211 : : else
9212 [ - + ]: 1556 : idx = read_4ubyte_unaligned (dbg, strreadp);
9213 : :
9214 : 1556 : data = dbg->sectiondata[IDX_debug_str];
9215 [ - + - + ]: 1556 : if (data == NULL || idx >= data->d_size
9216 [ - + ]: 1556 : || memchr (data->d_buf + idx, '\0',
9217 : : data->d_size - idx) == NULL)
9218 : : str = "???";
9219 : : else
9220 : 1556 : str = (char *) data->d_buf + idx;
9221 : : }
9222 : : }
9223 : 1556 : fprintf (out, "%s (%" PRIu64 ")", str, val);
9224 : 1556 : break;
9225 : :
9226 : 0 : case DW_FORM_strx1:
9227 [ # # ]: 0 : if (readendp - readp < 1)
9228 : 0 : goto invalid_data;
9229 : 0 : val = *readp++;
9230 : 0 : goto strx_val;
9231 : :
9232 : 0 : case DW_FORM_strx2:
9233 [ # # ]: 0 : if (readendp - readp < 2)
9234 : 0 : goto invalid_data;
9235 [ # # ]: 0 : val = read_2ubyte_unaligned_inc (dbg, readp);
9236 : 0 : goto strx_val;
9237 : :
9238 : 0 : case DW_FORM_strx3:
9239 [ # # ]: 0 : if (readendp - readp < 3)
9240 : 0 : goto invalid_data;
9241 : 0 : val = read_3ubyte_unaligned_inc (dbg, readp);
9242 : 0 : goto strx_val;
9243 : :
9244 : 0 : case DW_FORM_strx4:
9245 [ # # ]: 0 : if (readendp - readp < 4)
9246 : 0 : goto invalid_data;
9247 [ # # ]: 0 : val = read_4ubyte_unaligned_inc (dbg, readp);
9248 : 0 : goto strx_val;
9249 : :
9250 : 0 : default:
9251 : 0 : error (0, 0, _("unknown form: %s"), dwarf_form_name (form));
9252 : 0 : return readendp;
9253 : : }
9254 : :
9255 : 144006 : return readp;
9256 : : }
9257 : :
9258 : : /* Only used via run_advance_pc() macro */
9259 : : static inline void
9260 : 786356 : run_advance_pc (unsigned int op_advance,
9261 : : unsigned int minimum_instr_len,
9262 : : unsigned int max_ops_per_instr,
9263 : : unsigned int *op_addr_advance,
9264 : : Dwarf_Word *address,
9265 : : unsigned int *op_index)
9266 : : {
9267 : 786356 : const unsigned int advanced_op_index = (*op_index) + op_advance;
9268 : :
9269 : 786356 : *op_addr_advance = minimum_instr_len * (advanced_op_index
9270 : 786356 : / max_ops_per_instr);
9271 : 786356 : *address = *address + *op_addr_advance;
9272 : 786356 : *op_index = advanced_op_index % max_ops_per_instr;
9273 : : }
9274 : :
9275 : : static void
9276 : 186 : print_debug_line_section (Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr,
9277 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg,
9278 : : FILE *out)
9279 : : {
9280 [ + + ]: 186 : if (decodedline)
9281 : : {
9282 : 82 : print_decoded_line_section (dwflmod, ebl, ehdr, scn, shdr, dbg, out);
9283 : 164 : return;
9284 : : }
9285 : :
9286 : 104 : Elf_Data *data = get_debug_elf_data (dbg, ebl, IDX_debug_line, scn);
9287 [ + - ]: 104 : if (data == NULL)
9288 : : return;
9289 : :
9290 : 104 : fprintf (out, _("\
9291 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
9292 : : elf_ndxscn (scn), section_name (ebl, shdr),
9293 : 104 : (uint64_t) shdr->sh_offset);
9294 : :
9295 [ + - ]: 104 : if (shdr->sh_size == 0)
9296 : : return;
9297 : :
9298 : : /* There is no functionality in libdw to read the information in the
9299 : : way it is represented here. Hardcode the decoder. */
9300 : :
9301 : 104 : const unsigned char *linep = (const unsigned char *) data->d_buf;
9302 : 104 : const unsigned char *lineendp;
9303 : :
9304 : 104 : while (linep
9305 [ + + ]: 3726 : < (lineendp = (const unsigned char *) data->d_buf + data->d_size))
9306 : : {
9307 : 3622 : size_t start_offset = linep - (const unsigned char *) data->d_buf;
9308 : :
9309 : 3622 : fprintf (out, "\n%s %zu:\n",
9310 : : table_offset_lower_str, start_offset);
9311 : :
9312 [ - + ]: 3622 : if (unlikely (linep + 4 > lineendp))
9313 : 0 : goto invalid_data;
9314 [ + + ]: 3622 : Dwarf_Word unit_length = read_4ubyte_unaligned_inc (dbg, linep);
9315 : 3622 : unsigned int length = 4;
9316 [ - + ]: 3622 : if (unlikely (unit_length == 0xffffffff))
9317 : : {
9318 [ # # ]: 0 : if (unlikely (linep + 8 > lineendp))
9319 : : {
9320 : 0 : invalid_data:
9321 : 0 : error (0, 0, _("invalid data in section [%zu] '%s'"),
9322 : : elf_ndxscn (scn), section_name (ebl, shdr));
9323 : 0 : return;
9324 : : }
9325 [ # # ]: 0 : unit_length = read_8ubyte_unaligned_inc (dbg, linep);
9326 : 0 : length = 8;
9327 : : }
9328 : :
9329 : : /* Check whether we have enough room in the section. */
9330 [ - + ]: 3622 : if (unlikely (unit_length > (size_t) (lineendp - linep)))
9331 : 0 : goto invalid_data;
9332 : 3622 : lineendp = linep + unit_length;
9333 : :
9334 : : /* The next element of the header is the version identifier. */
9335 [ - + ]: 3622 : if ((size_t) (lineendp - linep) < 2)
9336 : 0 : goto invalid_data;
9337 [ + + ]: 3622 : uint_fast16_t version = read_2ubyte_unaligned_inc (dbg, linep);
9338 : :
9339 : 7244 : size_t address_size
9340 [ + + ]: 3622 : = elf_getident (ebl->elf, NULL)[EI_CLASS] == ELFCLASS32 ? 4 : 8;
9341 : 3622 : unsigned char segment_selector_size = 0;
9342 [ + + ]: 3622 : if (version > 4)
9343 : : {
9344 [ - + ]: 3470 : if ((size_t) (lineendp - linep) < 2)
9345 : 0 : goto invalid_data;
9346 : 3470 : address_size = *linep++;
9347 : 3470 : segment_selector_size = *linep++;
9348 : : }
9349 : :
9350 : : /* Next comes the header length. */
9351 : 3622 : Dwarf_Word header_length;
9352 [ + - ]: 3622 : if (length == 4)
9353 : : {
9354 [ - + ]: 3622 : if ((size_t) (lineendp - linep) < 4)
9355 : 0 : goto invalid_data;
9356 [ + + ]: 3622 : header_length = read_4ubyte_unaligned_inc (dbg, linep);
9357 : : }
9358 : : else
9359 : : {
9360 [ # # ]: 0 : if ((size_t) (lineendp - linep) < 8)
9361 : 0 : goto invalid_data;
9362 [ # # ]: 0 : header_length = read_8ubyte_unaligned_inc (dbg, linep);
9363 : : }
9364 : :
9365 : 3622 : const unsigned char *header_start = linep;
9366 : :
9367 : : /* Next the minimum instruction length. */
9368 [ - + ]: 3622 : if ((size_t) (lineendp - linep) < 1)
9369 : 0 : goto invalid_data;
9370 : 3622 : uint_fast8_t minimum_instr_len = *linep++;
9371 : :
9372 : : /* Next the maximum operations per instruction, in version 4 format. */
9373 : 3622 : uint_fast8_t max_ops_per_instr;
9374 [ + + ]: 3622 : if (version < 4)
9375 : : max_ops_per_instr = 1;
9376 : : else
9377 : : {
9378 [ - + ]: 3490 : if ((size_t) (lineendp - linep) < 1)
9379 : 0 : goto invalid_data;
9380 : 3490 : max_ops_per_instr = *linep++;
9381 : : }
9382 : :
9383 : : /* We need at least 4 more bytes. */
9384 [ - + ]: 3622 : if ((size_t) (lineendp - linep) < 4)
9385 : 0 : goto invalid_data;
9386 : :
9387 : : /* Then the flag determining the default value of the is_stmt
9388 : : register. */
9389 : 3622 : uint_fast8_t default_is_stmt = *linep++;
9390 : :
9391 : : /* Now the line base. */
9392 : 3622 : int_fast8_t line_base = *linep++;
9393 : :
9394 : : /* And the line range. */
9395 : 3622 : uint_fast8_t line_range = *linep++;
9396 : :
9397 : : /* The opcode base. */
9398 : 3622 : uint_fast8_t opcode_base = *linep++;
9399 : :
9400 : : /* Print what we got so far. */
9401 : 3622 : fprintf (out, "\n"
9402 : : " %s: %" PRIu64 "\n"
9403 : : " %s: %" PRIuFAST16 "\n"
9404 : : " %s: %" PRIu64 "\n"
9405 : : " %s: %zd\n"
9406 : : " %s: %zd\n"
9407 : : " %s: %" PRIuFAST8 "\n"
9408 : : " %s: %" PRIuFAST8 "\n"
9409 : : " %s 'is_stmt': %" PRIuFAST8 "\n"
9410 : : " %s: %" PRIdFAST8 "\n"
9411 : : " %s: %" PRIuFAST8 "\n"
9412 : : " %s: %" PRIuFAST8 "\n"
9413 : : "\n"
9414 : : "%s:\n",
9415 : : length_str, (uint64_t) unit_length, dwarf_ver_str, version,
9416 : : prologue_len_str, (uint64_t) header_length, addr_size_str,
9417 : : address_size, seg_selector_str, (size_t) segment_selector_size,
9418 : : min_inst_len_str, minimum_instr_len, max_op_per_inst_str,
9419 : : max_ops_per_instr, initial_value_if_str, default_is_stmt,
9420 : : line_base_str, line_base, line_range_str, line_range,
9421 : : opcodes_base_str, opcode_base, opcodes_str);
9422 : :
9423 [ - + ]: 3622 : if (version < 2 || version > 5)
9424 : : {
9425 : 0 : error (0, 0, _("cannot handle .debug_line version: %u\n"),
9426 : : (unsigned int) version);
9427 : 0 : linep = lineendp;
9428 : 0 : continue;
9429 : : }
9430 : :
9431 [ - + ]: 3622 : if (address_size != 4 && address_size != 8)
9432 : : {
9433 : 0 : error (0, 0, _("cannot handle address size: %u\n"),
9434 : : (unsigned int) address_size);
9435 : 0 : linep = lineendp;
9436 : 0 : continue;
9437 : : }
9438 : :
9439 [ - + ]: 3622 : if (segment_selector_size != 0)
9440 : : {
9441 : 0 : error (0, 0, _("cannot handle segment selector size: %u\n"),
9442 : : (unsigned int) segment_selector_size);
9443 : 0 : linep = lineendp;
9444 : 0 : continue;
9445 : : }
9446 : :
9447 [ - + ]: 3622 : if (unlikely (linep + opcode_base - 1 >= lineendp))
9448 : : {
9449 : 0 : invalid_unit:
9450 : 0 : error (0, 0,
9451 : 0 : _("invalid data at offset %tu in section [%zu] '%s'"),
9452 : 0 : linep - (const unsigned char *) data->d_buf,
9453 : : elf_ndxscn (scn), section_name (ebl, shdr));
9454 : 0 : linep = lineendp;
9455 : 0 : continue;
9456 : : }
9457 : 3622 : int opcode_base_l10 = 1;
9458 : 3622 : unsigned int tmp = opcode_base;
9459 [ + + ]: 7238 : while (tmp > 10)
9460 : : {
9461 : 3616 : tmp /= 10;
9462 : 3616 : ++opcode_base_l10;
9463 : : }
9464 : : const uint8_t *standard_opcode_lengths = linep - 1;
9465 [ + + ]: 47068 : for (uint_fast8_t cnt = 1; cnt < opcode_base; ++cnt)
9466 : 43446 : fprintf (out, ngettext (" [%*" PRIuFAST8 "] %hhu argument\n",
9467 : : " [%*" PRIuFAST8 "] %hhu arguments\n",
9468 : : (int) linep[cnt - 1]),
9469 : 43446 : opcode_base_l10, cnt, linep[cnt - 1]);
9470 : 3622 : linep += opcode_base - 1;
9471 : :
9472 : 3622 : if (unlikely (linep >= lineendp))
9473 : : goto invalid_unit;
9474 : :
9475 : 3622 : Dwarf_Off str_offsets_base = str_offsets_base_off (dbg, NULL);
9476 : :
9477 : 3622 : fprintf (out, "\n%s:\n", dir_table_str);
9478 [ + + ]: 3622 : if (version > 4)
9479 : : {
9480 : 3470 : struct encpair { uint16_t desc; uint16_t form; };
9481 : 3470 : struct encpair enc[256];
9482 : :
9483 : 3470 : fprintf (out, " [");
9484 : 3470 : if ((size_t) (lineendp - linep) < 1)
9485 : 0 : goto invalid_data;
9486 : 3470 : unsigned char directory_entry_format_count = *linep++;
9487 [ + + ]: 6940 : for (int i = 0; i < directory_entry_format_count; i++)
9488 : : {
9489 : 3470 : uint16_t desc, form;
9490 [ - + ]: 3470 : if ((size_t) (lineendp - linep) < 1)
9491 : 0 : goto invalid_data;
9492 : 3470 : get_uleb128 (desc, linep, lineendp);
9493 [ - + ]: 3470 : if ((size_t) (lineendp - linep) < 1)
9494 : 0 : goto invalid_data;
9495 : 3470 : get_uleb128 (form, linep, lineendp);
9496 : :
9497 : 3470 : enc[i].desc = desc;
9498 : 3470 : enc[i].form = form;
9499 : :
9500 : 3470 : fprintf (out, "%s(%s)",
9501 : : dwarf_line_content_description_name (desc),
9502 : : dwarf_form_name (form));
9503 [ - + ]: 3470 : if (i + 1 < directory_entry_format_count)
9504 : 0 : fprintf (out, ", ");
9505 : : }
9506 : 3470 : fprintf (out, "]\n");
9507 : :
9508 : 3470 : uint64_t directories_count;
9509 [ - + ]: 3470 : if ((size_t) (lineendp - linep) < 1)
9510 : 0 : goto invalid_data;
9511 : 3470 : get_uleb128 (directories_count, linep, lineendp);
9512 : :
9513 : 3470 : if (directory_entry_format_count == 0
9514 [ - + ]: 3470 : && directories_count != 0)
9515 : 0 : goto invalid_data;
9516 : :
9517 [ + + ]: 29864 : for (uint64_t i = 0; i < directories_count; i++)
9518 : : {
9519 : 26394 : fprintf (out, " %-5" PRIu64 " ", i);
9520 [ + + ]: 52788 : for (int j = 0; j < directory_entry_format_count; j++)
9521 : : {
9522 : 26394 : linep = print_form_data (dbg, enc[j].form,
9523 : : linep, lineendp, length,
9524 : : str_offsets_base, out);
9525 [ - + ]: 26394 : if (j + 1 < directory_entry_format_count)
9526 : 0 : fprintf (out, ", ");
9527 : : }
9528 : 26394 : fprintf (out, "\n");
9529 [ - + ]: 26394 : if (linep >= lineendp)
9530 : 0 : goto invalid_unit;
9531 : : }
9532 : : }
9533 : : else
9534 : : {
9535 [ + - + + ]: 766 : while (linep < lineendp && *linep != 0)
9536 : : {
9537 : 614 : const unsigned char *endp = memchr (linep, '\0', lineendp - linep);
9538 [ - + ]: 614 : if (unlikely (endp == NULL))
9539 : 0 : goto invalid_unit;
9540 : :
9541 : 614 : fprintf (out, " %s\n", (char *) linep);
9542 : :
9543 : 614 : linep = endp + 1;
9544 : : }
9545 [ + - - + ]: 152 : if (linep >= lineendp || *linep != 0)
9546 : 0 : goto invalid_unit;
9547 : : /* Skip the final NUL byte. */
9548 : 152 : ++linep;
9549 : : }
9550 : :
9551 [ - + ]: 3622 : if (unlikely (linep >= lineendp))
9552 : 0 : goto invalid_unit;
9553 : :
9554 : 3622 : fprintf (out, "\n%s:\n", file_table_str);
9555 [ + + ]: 3622 : if (version > 4)
9556 : : {
9557 : 3470 : struct encpair { uint16_t desc; uint16_t form; };
9558 : 3470 : struct encpair enc[256];
9559 : :
9560 : 3470 : fprintf (out, " [");
9561 : 3470 : if ((size_t) (lineendp - linep) < 1)
9562 : 0 : goto invalid_data;
9563 : 3470 : unsigned char file_name_format_count = *linep++;
9564 [ + + ]: 10418 : for (int i = 0; i < file_name_format_count; i++)
9565 : : {
9566 : 6948 : uint64_t desc, form;
9567 [ - + ]: 6948 : if ((size_t) (lineendp - linep) < 1)
9568 : 0 : goto invalid_data;
9569 : 6948 : get_uleb128 (desc, linep, lineendp);
9570 [ - + ]: 6948 : if ((size_t) (lineendp - linep) < 1)
9571 : 0 : goto invalid_data;
9572 : 6948 : get_uleb128 (form, linep, lineendp);
9573 : :
9574 [ - + ]: 6948 : if (! libdw_valid_user_form (form))
9575 : 0 : goto invalid_data;
9576 : :
9577 : 6948 : enc[i].desc = desc;
9578 : 6948 : enc[i].form = form;
9579 : :
9580 : 6948 : fprintf (out, "%s(%s)",
9581 : : dwarf_line_content_description_name (desc),
9582 : : dwarf_form_name (form));
9583 [ + + ]: 6948 : if (i + 1 < file_name_format_count)
9584 : 3478 : fprintf (out, ", ");
9585 : : }
9586 : 3470 : fprintf (out, "]\n");
9587 : :
9588 : 3470 : uint64_t file_name_count;
9589 [ - + ]: 3470 : if ((size_t) (lineendp - linep) < 1)
9590 : 0 : goto invalid_data;
9591 : 3470 : get_uleb128 (file_name_count, linep, lineendp);
9592 : :
9593 : 3470 : if (file_name_format_count == 0
9594 [ - + ]: 3470 : && file_name_count != 0)
9595 : 0 : goto invalid_data;
9596 : :
9597 [ + + ]: 61486 : for (uint64_t i = 0; i < file_name_count; i++)
9598 : : {
9599 : 58016 : fprintf (out, " %-5" PRIu64 " ", i);
9600 [ + + ]: 174072 : for (int j = 0; j < file_name_format_count; j++)
9601 : : {
9602 : 116056 : linep = print_form_data (dbg, enc[j].form,
9603 : : linep, lineendp, length,
9604 : : str_offsets_base, out);
9605 [ + + ]: 116056 : if (j + 1 < file_name_format_count)
9606 : 58040 : fprintf (out, ", ");
9607 : : }
9608 : 58016 : fprintf (out, "\n");
9609 [ - + ]: 58016 : if (linep > lineendp)
9610 : 0 : goto invalid_unit;
9611 : : }
9612 : : }
9613 : : else
9614 : : {
9615 : 152 : fputs (_(" Entry Dir Time Size Name\n"), out);
9616 [ + - + + ]: 6154 : for (unsigned int cnt = 1; linep < lineendp && *linep != 0; ++cnt)
9617 : : {
9618 : : /* First comes the file name. */
9619 : 6002 : char *fname = (char *) linep;
9620 : 6002 : unsigned char *endp = memchr (fname, '\0', lineendp - linep);
9621 [ - + ]: 6002 : if (unlikely (endp == NULL))
9622 : 0 : goto invalid_unit;
9623 : 6002 : linep = endp + 1;
9624 : :
9625 : : /* Then the index. */
9626 : 6002 : unsigned int diridx;
9627 [ - + ]: 6002 : if (lineendp - linep < 1)
9628 : 0 : goto invalid_unit;
9629 : 6002 : get_uleb128 (diridx, linep, lineendp);
9630 : :
9631 : : /* Next comes the modification time. */
9632 : 6002 : unsigned int mtime;
9633 [ - + ]: 6002 : if (lineendp - linep < 1)
9634 : 0 : goto invalid_unit;
9635 : 6002 : get_uleb128 (mtime, linep, lineendp);
9636 : :
9637 : : /* Finally the length of the file. */
9638 : 6002 : unsigned int fsize;
9639 [ - + ]: 6002 : if (lineendp - linep < 1)
9640 : 0 : goto invalid_unit;
9641 : 6002 : get_uleb128 (fsize, linep, lineendp);
9642 : :
9643 : 6002 : fprintf (out, " %-5u %-5u %-9u %-9u %s\n",
9644 : : cnt, diridx, mtime, fsize, fname);
9645 : : }
9646 [ + - - + ]: 152 : if (linep >= lineendp || *linep != '\0')
9647 : 0 : goto invalid_unit;
9648 : : /* Skip the final NUL byte. */
9649 : 152 : ++linep;
9650 : : }
9651 : :
9652 : 3622 : unsigned int debug_str_offset = 0;
9653 [ + + ]: 3622 : if (unlikely (linep == header_start + header_length - 4))
9654 : : {
9655 : : /* CUBINs contain an unsigned 4-byte offset */
9656 [ - + ]: 2 : debug_str_offset = read_4ubyte_unaligned_inc (dbg, linep);
9657 : : }
9658 : :
9659 [ + + ]: 3622 : if (linep == lineendp)
9660 : : {
9661 : 40 : fprintf (out, "\n%s.\n", no_line_num_stmts_str);
9662 : 40 : continue;
9663 : : }
9664 : :
9665 : 3582 : fprintf (out, "\n%s:\n", line_num_stmts_str);
9666 : 3582 : Dwarf_Word address = 0;
9667 : 3582 : unsigned int op_index = 0;
9668 : 3582 : size_t line = 1;
9669 : 3582 : uint_fast8_t is_stmt = default_is_stmt;
9670 : :
9671 : : /* Apply the "operation advance" from a special opcode
9672 : : or DW_LNS_advance_pc (as per DWARF4 6.2.5.1). */
9673 : 3582 : unsigned int op_addr_advance;
9674 : : #define advance_pc(op_advance) run_advance_pc(op_advance, minimum_instr_len, \
9675 : : max_ops_per_instr, &op_addr_advance, &address, &op_index)
9676 : :
9677 [ - + ]: 3582 : if (max_ops_per_instr == 0)
9678 : : {
9679 : 0 : error (0, 0,
9680 : 0 : _("invalid maximum operations per instruction is zero"));
9681 : 0 : linep = lineendp;
9682 : 0 : continue;
9683 : : }
9684 : :
9685 [ + + ]: 2530610 : while (linep < lineendp)
9686 : : {
9687 : 2527028 : size_t offset = linep - (const unsigned char *) data->d_buf;
9688 : 2527028 : unsigned int u128;
9689 : 2527028 : int s128;
9690 : :
9691 : : /* Read the opcode. */
9692 : 2527028 : unsigned int opcode = *linep++;
9693 : :
9694 : 2527028 : fprintf (out, " [%6" PRIx64 "]", (uint64_t)offset);
9695 : : /* Is this a special opcode? */
9696 [ + + ]: 2527028 : if (likely (opcode >= opcode_base))
9697 : : {
9698 [ - + ]: 717060 : if (unlikely (line_range == 0))
9699 : 0 : goto invalid_unit;
9700 : :
9701 : : /* Yes. Handling this is quite easy since the opcode value
9702 : : is computed with
9703 : :
9704 : : opcode = (desired line increment - line_base)
9705 : : + (line_range * address advance) + opcode_base
9706 : : */
9707 : 717060 : int line_increment = (line_base
9708 : 717060 : + (opcode - opcode_base) % line_range);
9709 : :
9710 : : /* Perform the increments. */
9711 : 717060 : line += line_increment;
9712 : 717060 : advance_pc ((opcode - opcode_base) / line_range);
9713 : :
9714 : 717060 : fprintf (out, " %s %u: %s+%u = ",
9715 : : special_opcode_str, opcode, address_str, op_addr_advance);
9716 : 717060 : print_dwarf_addr (dwflmod, 0, address, address, out);
9717 [ - + ]: 717060 : if (op_index > 0)
9718 : 0 : fprintf (out, ", op_index = %u, %s%+d = %zu\n",
9719 : : op_index, line_str, line_increment, line);
9720 : : else
9721 : 717060 : fprintf (out, ", %s%+d = %zu\n",
9722 : : line_str, line_increment, line);
9723 : : }
9724 [ + + ]: 1809968 : else if (opcode == 0)
9725 : : {
9726 : : /* This an extended opcode. */
9727 [ - + ]: 191488 : if (unlikely (linep + 2 > lineendp))
9728 : 0 : goto invalid_unit;
9729 : :
9730 : : /* The length. */
9731 : 191488 : unsigned int len = *linep++;
9732 : :
9733 [ - + ]: 191488 : if (unlikely (linep + len > lineendp))
9734 : 0 : goto invalid_unit;
9735 : :
9736 : : /* The sub-opcode. */
9737 : 191488 : opcode = *linep++;
9738 : :
9739 : 191488 : fprintf (out, " %s %u: ", ext_opcode_str, opcode);
9740 : :
9741 [ + + - + : 191488 : switch (opcode)
+ - - ]
9742 : : {
9743 : 10606 : case DW_LNE_end_sequence:
9744 : 10606 : fprintf (out, " %s\n", end_of_seq_str);
9745 : :
9746 : : /* Reset the registers we care about. */
9747 : 10606 : address = 0;
9748 : 10606 : op_index = 0;
9749 : 10606 : line = 1;
9750 : 10606 : is_stmt = default_is_stmt;
9751 : 10606 : break;
9752 : :
9753 : 12330 : case DW_LNE_set_address:
9754 : 12330 : op_index = 0;
9755 [ - + ]: 12330 : if (unlikely ((size_t) (lineendp - linep) < address_size))
9756 : 0 : goto invalid_unit;
9757 [ + + ]: 12330 : if (address_size == 4)
9758 [ + + ]: 50 : address = read_4ubyte_unaligned_inc (dbg, linep);
9759 : : else
9760 [ + + ]: 12280 : address = read_8ubyte_unaligned_inc (dbg, linep);
9761 : : {
9762 : 12330 : fprintf (out, " %s ", set_addr_to_str);
9763 : 12330 : print_dwarf_addr (dwflmod, 0, address, address, out);
9764 : 12330 : fprintf (out, "\n");
9765 : : }
9766 : 12330 : break;
9767 : :
9768 : 0 : case DW_LNE_define_file:
9769 : : {
9770 : 0 : char *fname = (char *) linep;
9771 : 0 : const unsigned char *endp = memchr (linep, '\0',
9772 : 0 : lineendp - linep);
9773 [ # # ]: 0 : if (unlikely (endp == NULL))
9774 : 0 : goto invalid_unit;
9775 : 0 : linep = endp + 1;
9776 : :
9777 : 0 : unsigned int diridx;
9778 [ # # ]: 0 : if (lineendp - linep < 1)
9779 : 0 : goto invalid_unit;
9780 : 0 : get_uleb128 (diridx, linep, lineendp);
9781 : 0 : Dwarf_Word mtime;
9782 [ # # ]: 0 : if (lineendp - linep < 1)
9783 : 0 : goto invalid_unit;
9784 : 0 : get_uleb128 (mtime, linep, lineendp);
9785 : 0 : Dwarf_Word filelength;
9786 [ # # ]: 0 : if (lineendp - linep < 1)
9787 : 0 : goto invalid_unit;
9788 : 0 : get_uleb128 (filelength, linep, lineendp);
9789 : :
9790 : 0 : fprintf (out, _("\
9791 : : define new file: dir=%u, mtime=%" PRIu64 ", length=%" PRIu64 ", name=%s\n"),
9792 : : diridx, (uint64_t) mtime, (uint64_t) filelength,
9793 : : fname);
9794 : : }
9795 : 0 : break;
9796 : :
9797 : 168538 : case DW_LNE_set_discriminator:
9798 : : /* Takes one ULEB128 parameter, the discriminator. */
9799 [ + - - + ]: 168538 : if (unlikely (standard_opcode_lengths[opcode] != 1
9800 : : || lineendp - linep < 1))
9801 : 0 : goto invalid_unit;
9802 : :
9803 : 168538 : get_uleb128 (u128, linep, lineendp);
9804 : 168538 : fprintf (out, " %s %u\n", set_disc_str, u128);
9805 : 168538 : break;
9806 : :
9807 : 14 : case DW_LNE_NVIDIA_inlined_call:
9808 : : {
9809 [ - + ]: 14 : if (unlikely (linep >= lineendp))
9810 : 0 : goto invalid_data;
9811 : :
9812 : 14 : unsigned int context;
9813 : 14 : get_uleb128 (context, linep, lineendp);
9814 : :
9815 [ - + ]: 14 : if (unlikely (linep >= lineendp))
9816 : 0 : goto invalid_data;
9817 : :
9818 : 14 : unsigned int function_name;
9819 : 14 : get_uleb128 (function_name, linep, lineendp);
9820 : 14 : function_name += debug_str_offset;
9821 : :
9822 : 14 : Elf_Data *str_data = dbg->sectiondata[IDX_debug_str];
9823 : 14 : char *function_str;
9824 [ + - - + ]: 14 : if (str_data == NULL || function_name >= str_data->d_size
9825 [ - + ]: 14 : || memchr (str_data->d_buf + function_name, '\0',
9826 : : str_data->d_size - function_name) == NULL)
9827 : : function_str = "???";
9828 : : else
9829 : 14 : function_str = (char *) str_data->d_buf + function_name;
9830 : :
9831 : 14 : fprintf (out, _(" set inlined context %u,"
9832 : : " function name %s (0x%x)\n"),
9833 : : context, function_str, function_name);
9834 : 14 : break;
9835 : : }
9836 : :
9837 : 0 : case DW_LNE_NVIDIA_set_function_name:
9838 : : {
9839 [ # # ]: 0 : if (unlikely (linep >= lineendp))
9840 : 0 : goto invalid_data;
9841 : :
9842 : 0 : unsigned int function_name;
9843 : 0 : get_uleb128 (function_name, linep, lineendp);
9844 : 0 : function_name += debug_str_offset;
9845 : :
9846 : 0 : Elf_Data *str_data = dbg->sectiondata[IDX_debug_str];
9847 : 0 : char *function_str;
9848 [ # # # # ]: 0 : if (str_data == NULL || function_name >= str_data->d_size
9849 [ # # ]: 0 : || memchr (str_data->d_buf + function_name, '\0',
9850 : : str_data->d_size - function_name) == NULL)
9851 : : function_str = "???";
9852 : : else
9853 : 0 : function_str = (char *) str_data->d_buf + function_name;
9854 : :
9855 : 0 : fprintf (out, _(" set function name %s (0x%x)\n"),
9856 : : function_str, function_name);
9857 : : }
9858 : 0 : break;
9859 : :
9860 : 0 : default:
9861 : : /* Unknown, ignore it. */
9862 : 0 : fputs (_(" unknown opcode\n"), out);
9863 : 0 : linep += len - 1;
9864 : 0 : break;
9865 : : }
9866 : : }
9867 [ + - ]: 1618480 : else if (opcode <= DW_LNS_set_isa)
9868 : : {
9869 : : /* This is a known standard opcode. */
9870 [ + + + + : 1618480 : switch (opcode)
+ + - + +
+ - - ]
9871 : : {
9872 : 252084 : case DW_LNS_copy:
9873 : : /* Takes no argument. */
9874 : 252084 : fprintf (out, " %s\n", copy_str);
9875 : 252084 : break;
9876 : :
9877 : 15436 : case DW_LNS_advance_pc:
9878 : : /* Takes one uleb128 parameter which is added to the
9879 : : address. */
9880 [ - + ]: 15436 : if (lineendp - linep < 1)
9881 : 0 : goto invalid_unit;
9882 : 15436 : get_uleb128 (u128, linep, lineendp);
9883 : 15436 : advance_pc (u128);
9884 : : {
9885 : 15436 : fprintf (out, " %s %u %s ",
9886 : : adv_addr_str, op_addr_advance, to_str);
9887 : 15436 : print_dwarf_addr (dwflmod, 0, address, address, out);
9888 [ - + ]: 15436 : if (op_index > 0)
9889 : 0 : fprintf (out, ", op_index %s %u", to_str, op_index);
9890 : 15436 : fprintf (out, "\n");
9891 : : }
9892 : 15436 : break;
9893 : :
9894 : 186546 : case DW_LNS_advance_line:
9895 : : /* Takes one sleb128 parameter which is added to the
9896 : : line. */
9897 [ - + ]: 186546 : if (lineendp - linep < 1)
9898 : 0 : goto invalid_unit;
9899 : 186546 : get_sleb128 (s128, linep, lineendp);
9900 : 186546 : line += s128;
9901 : 186546 : fprintf (out, "\
9902 : : %s %d to %" PRId64 "\n",
9903 : : adv_line_by_str, s128, (int64_t) line);
9904 : 186546 : break;
9905 : :
9906 : 65008 : case DW_LNS_set_file:
9907 : : /* Takes one uleb128 parameter which is stored in file. */
9908 [ - + ]: 65008 : if (lineendp - linep < 1)
9909 : 0 : goto invalid_unit;
9910 : 65008 : get_uleb128 (u128, linep, lineendp);
9911 : 65008 : fprintf (out, " %s %" PRIu64 "\n",
9912 : : set_file_str, (uint64_t) u128);
9913 : 65008 : break;
9914 : :
9915 : 612232 : case DW_LNS_set_column:
9916 : : /* Takes one uleb128 parameter which is stored in column. */
9917 [ + - - + ]: 612232 : if (unlikely (standard_opcode_lengths[opcode] != 1
9918 : : || lineendp - linep < 1))
9919 : 0 : goto invalid_unit;
9920 : :
9921 : 612232 : get_uleb128 (u128, linep, lineendp);
9922 : 612232 : fprintf (out, " %s %" PRIu64 "\n",
9923 : : set_col_str, (uint64_t) u128);
9924 : 612232 : break;
9925 : :
9926 : 433260 : case DW_LNS_negate_stmt:
9927 : : /* Takes no argument. */
9928 : 433260 : is_stmt = 1 - is_stmt;
9929 : 433260 : fprintf (out, " %s '%s' %s %" PRIuFAST8 "\n",
9930 : : set_str, "is_stmt", to_str, is_stmt);
9931 : 433260 : break;
9932 : :
9933 : 0 : case DW_LNS_set_basic_block:
9934 : : /* Takes no argument. */
9935 : 0 : fputs (_(" set basic block flag\n"), out);
9936 : 0 : break;
9937 : :
9938 : 53860 : case DW_LNS_const_add_pc:
9939 : : /* Takes no argument. */
9940 : :
9941 [ - + ]: 53860 : if (unlikely (line_range == 0))
9942 : 0 : goto invalid_unit;
9943 : :
9944 : 53860 : advance_pc ((255 - opcode_base) / line_range);
9945 : : {
9946 : 53860 : fprintf (out, " %s %u to ",
9947 : : adv_addr_by_str, op_addr_advance);
9948 : 53860 : print_dwarf_addr (dwflmod, 0, address, address, out);
9949 [ - + ]: 53860 : if (op_index > 0)
9950 : 0 : fprintf (out, _(", op_index to %u"), op_index);
9951 : 53860 : fprintf (out, "\n");
9952 : : }
9953 : 53860 : break;
9954 : :
9955 : 44 : case DW_LNS_fixed_advance_pc:
9956 : : /* Takes one 16 bit parameter which is added to the
9957 : : address. */
9958 [ + - - + ]: 44 : if (unlikely (standard_opcode_lengths[opcode] != 1
9959 : : || lineendp - linep < 2))
9960 : 0 : goto invalid_unit;
9961 : :
9962 [ - + ]: 44 : u128 = read_2ubyte_unaligned_inc (dbg, linep);
9963 : 44 : address += u128;
9964 : 44 : op_index = 0;
9965 : : {
9966 : 44 : fprintf (out, _("\
9967 : : advance address by fixed value %u to \n"),
9968 : : u128);
9969 : 44 : print_dwarf_addr (dwflmod, 0, address, address, out);
9970 : 44 : fprintf (out, "\n");
9971 : : }
9972 : 44 : break;
9973 : :
9974 : 10 : case DW_LNS_set_prologue_end:
9975 : : /* Takes no argument. */
9976 : 10 : fputs (_(" set prologue end flag\n"), out);
9977 : 10 : break;
9978 : :
9979 : 0 : case DW_LNS_set_epilogue_begin:
9980 : : /* Takes no argument. */
9981 : 0 : fputs (_(" set epilogue begin flag\n"), out);
9982 : 0 : break;
9983 : :
9984 : 0 : case DW_LNS_set_isa:
9985 : : /* Takes one uleb128 parameter which is stored in isa. */
9986 [ # # # # ]: 0 : if (unlikely (standard_opcode_lengths[opcode] != 1
9987 : : || lineendp - linep < 1))
9988 : 0 : goto invalid_unit;
9989 : :
9990 : 0 : get_uleb128 (u128, linep, lineendp);
9991 : 0 : fprintf (out, _(" set isa to %u\n"), u128);
9992 : 0 : break;
9993 : : }
9994 : : }
9995 : : else
9996 : : {
9997 : : /* This is a new opcode the generator but not we know about.
9998 : : Read the parameters associated with it but then discard
9999 : : everything. Read all the parameters for this opcode. */
10000 : 0 : fprintf (out,
10001 : 0 : ngettext (" unknown opcode with %" PRIu8 " parameter:",
10002 : : " unknown opcode with %" PRIu8 " parameters:",
10003 : : standard_opcode_lengths[opcode]),
10004 : 0 : standard_opcode_lengths[opcode]);
10005 : 0 : for (int n = standard_opcode_lengths[opcode];
10006 [ # # # # ]: 0 : n > 0 && linep < lineendp; --n)
10007 : : {
10008 : 0 : get_uleb128 (u128, linep, lineendp);
10009 [ # # ]: 0 : if (n != standard_opcode_lengths[opcode])
10010 : 0 : fputc (',', out);
10011 : 0 : fprintf (out, " %u", u128);
10012 : : }
10013 : :
10014 : : /* Next round, ignore this opcode. */
10015 : 0 : continue;
10016 : : }
10017 : : }
10018 : : }
10019 : :
10020 : : /* There must only be one data block. */
10021 [ - + ]: 104 : assert (elf_getdata (scn, data) == NULL);
10022 : : }
10023 : :
10024 : :
10025 : : static void
10026 : 40 : print_debug_loclists_section (Dwfl_Module *dwflmod,
10027 : : Ebl *ebl,
10028 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
10029 : : Elf_Scn *scn, GElf_Shdr *shdr,
10030 : : Dwarf *dbg, FILE *out)
10031 : : {
10032 : 40 : Elf_Data *data = get_debug_elf_data (dbg, ebl, IDX_debug_loclists, scn);
10033 [ + - ]: 40 : if (data == NULL)
10034 : 0 : return;
10035 : :
10036 : 40 : fprintf (out, _("\
10037 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
10038 : : elf_ndxscn (scn), section_name (ebl, shdr),
10039 : 40 : (uint64_t) shdr->sh_offset);
10040 : :
10041 : : /* For the listptr to get the base address/CU. */
10042 : 40 : sort_listptr (&known_loclistsptr, "loclistsptr");
10043 : 40 : size_t listptr_idx = 0;
10044 : :
10045 : 40 : const unsigned char *readp = data->d_buf;
10046 : 40 : const unsigned char *const dataend = ((unsigned char *) data->d_buf
10047 : 40 : + data->d_size);
10048 [ + + ]: 3132 : while (readp < dataend)
10049 : : {
10050 [ - + ]: 3092 : if (unlikely (readp > dataend - 4))
10051 : : {
10052 : 0 : invalid_data:
10053 : 0 : error (0, 0, _("invalid data in section [%zu] '%s'"),
10054 : : elf_ndxscn (scn), section_name (ebl, shdr));
10055 : 0 : return;
10056 : : }
10057 : :
10058 : 3092 : ptrdiff_t offset = readp - (unsigned char *) data->d_buf;
10059 : 3092 : fprintf (out, "%s 0x%" PRIx64 ":\n\n",
10060 : : table_offset_upper_str, (uint64_t) offset);
10061 : :
10062 [ - + ]: 3092 : uint64_t unit_length = read_4ubyte_unaligned_inc (dbg, readp);
10063 : 3092 : unsigned int offset_size = 4;
10064 [ - + ]: 3092 : if (unlikely (unit_length == 0xffffffff))
10065 : : {
10066 [ # # ]: 0 : if (unlikely (readp > dataend - 8))
10067 : 0 : goto invalid_data;
10068 : :
10069 [ # # ]: 0 : unit_length = read_8ubyte_unaligned_inc (dbg, readp);
10070 : 0 : offset_size = 8;
10071 : : }
10072 : 3092 : fprintf (out, " %s: %8" PRIu64 "\n", length_str, unit_length);
10073 : :
10074 : : /* We need at least 2-bytes + 1-byte + 1-byte + 4-bytes = 8
10075 : : bytes to complete the header. And this unit cannot go beyond
10076 : : the section data. */
10077 [ + - ]: 3092 : if (readp > dataend - 8
10078 [ + - ]: 3092 : || unit_length < 8
10079 [ - + ]: 3092 : || unit_length > (uint64_t) (dataend - readp))
10080 : 0 : goto invalid_data;
10081 : :
10082 : 3092 : const unsigned char *nexthdr = readp + unit_length;
10083 : :
10084 [ - + ]: 3092 : uint16_t version = read_2ubyte_unaligned_inc (dbg, readp);
10085 : 3092 : fprintf (out, " %s: %8" PRIu16 "\n", dwarf_ver_str, version);
10086 : :
10087 [ - + ]: 3092 : if (version != 5)
10088 : : {
10089 : 0 : error (0, 0, _("Unknown version"));
10090 : 0 : goto next_table;
10091 : : }
10092 : :
10093 : 3092 : uint8_t address_size = *readp++;
10094 : 3092 : fprintf (out, " %s: %8" PRIu64 "\n",
10095 : : addr_size_str, (uint64_t) address_size);
10096 : :
10097 [ - + ]: 3092 : if (address_size != 4 && address_size != 8)
10098 : : {
10099 : 0 : error (0, 0, _("unsupported address size"));
10100 : 0 : goto next_table;
10101 : : }
10102 : :
10103 : 3092 : uint8_t segment_size = *readp++;
10104 : 3092 : fprintf (out, " %s: %8" PRIu64 "\n",
10105 : : seg_size_str, (uint64_t) segment_size);
10106 : :
10107 [ - + ]: 3092 : if (segment_size != 0)
10108 : : {
10109 : 0 : error (0, 0, _("unsupported segment size"));
10110 : 0 : goto next_table;
10111 : : }
10112 : :
10113 [ - + ]: 3092 : uint32_t offset_entry_count = read_4ubyte_unaligned_inc (dbg, readp);
10114 : 3092 : fprintf (out, " %s: %8" PRIu64 "\n",
10115 : : offset_ent_str, (uint64_t) offset_entry_count);
10116 : :
10117 : : /* We need the CU that uses this unit to get the initial base address. */
10118 : 3092 : Dwarf_Addr cu_base = 0;
10119 : 3092 : struct Dwarf_CU *cu = NULL;
10120 [ - + ]: 3092 : if (listptr_cu (&known_loclistsptr, &listptr_idx,
10121 : : (Dwarf_Off) offset,
10122 : 3092 : (Dwarf_Off) (nexthdr - (unsigned char *) data->d_buf),
10123 : : &cu_base, &cu)
10124 [ # # ]: 0 : || split_dwarf_cu_base (dbg, &cu, &cu_base))
10125 : 3092 : {
10126 : 3092 : Dwarf_Die cudie;
10127 [ - + ]: 3092 : if (dwarf_cu_die (cu, &cudie,
10128 : : NULL, NULL, NULL, NULL,
10129 : : NULL, NULL) == NULL)
10130 : 0 : fprintf (out, " %s: ", unknown_base_str);
10131 : : else
10132 : 3092 : fprintf (out, " %s [%6" PRIx64 "] %s: ",
10133 : : cu_str, dwarf_dieoffset (&cudie), base_str);
10134 : 3092 : print_dwarf_addr (dwflmod, address_size, cu_base, cu_base, out);
10135 : 3092 : fprintf (out, "\n");
10136 : : }
10137 : : else
10138 : 0 : fprintf (out, _(" Not associated with a CU.\n"));
10139 : :
10140 : 3092 : fprintf (out, "\n");
10141 : :
10142 : 3092 : const unsigned char *offset_array_start = readp;
10143 [ + + ]: 3092 : if (offset_entry_count > 0)
10144 : : {
10145 : 4 : uint64_t max_entries = (unit_length - 8) / offset_size;
10146 [ - + ]: 4 : if (offset_entry_count > max_entries)
10147 : : {
10148 : 0 : error (0, 0,
10149 : 0 : _("too many offset entries for unit length"));
10150 : 0 : offset_entry_count = max_entries;
10151 : : }
10152 : :
10153 : 4 : fprintf (out, _(" Offsets starting at 0x%" PRIx64 ":\n"),
10154 : : (uint64_t) (offset_array_start
10155 : 4 : - (unsigned char *) data->d_buf));
10156 [ + + ]: 36 : for (uint32_t idx = 0; idx < offset_entry_count; idx++)
10157 : : {
10158 : 32 : fprintf (out, " [%6" PRIu32 "] ", idx);
10159 [ + - ]: 32 : if (offset_size == 4)
10160 : : {
10161 [ - + ]: 32 : uint32_t off = read_4ubyte_unaligned_inc (dbg, readp);
10162 : 32 : fprintf (out, "0x%" PRIx32 "\n", off);
10163 : : }
10164 : : else
10165 : : {
10166 [ # # ]: 0 : uint64_t off = read_8ubyte_unaligned_inc (dbg, readp);
10167 : 0 : fprintf (out, "0x%" PRIx64 "\n", off);
10168 : : }
10169 : : }
10170 : 4 : fprintf (out, "\n");
10171 : : }
10172 : :
10173 : 3092 : Dwarf_Addr base = cu_base;
10174 : 3092 : bool start_of_list = true;
10175 [ + + ]: 768674 : while (readp < nexthdr)
10176 : : {
10177 : 765582 : Dwarf_Off off = (Dwarf_Off) (readp - (unsigned char *) data->d_buf);
10178 [ + + ]: 765582 : if (listptr_attr (&known_loclistsptr, listptr_idx, off,
10179 : : DW_AT_GNU_locviews))
10180 : 109918 : {
10181 : 109918 : Dwarf_Off next_off = next_listptr_offset (&known_loclistsptr,
10182 : : &listptr_idx, off);
10183 : 109918 : const unsigned char *locp = readp;
10184 : 109918 : const unsigned char *locendp;
10185 [ + - ]: 109918 : if (next_off == 0
10186 [ + - ]: 109918 : || next_off > (size_t) (nexthdr - ((const unsigned char *)
10187 : : data->d_buf)))
10188 : : locendp = nexthdr;
10189 : : else
10190 : 109918 : locendp = (const unsigned char *) data->d_buf + next_off;
10191 : :
10192 : 109918 : fprintf (out, " Offset: %" PRIx64 ", Index: %" PRIx64 "\n",
10193 : : (uint64_t) (readp - (unsigned char *) data->d_buf),
10194 : 109918 : (uint64_t) (readp - offset_array_start));
10195 : :
10196 [ + + ]: 609866 : while (locp < locendp)
10197 : : {
10198 : 499948 : uint64_t v1, v2;
10199 : 499948 : get_uleb128 (v1, locp, locendp);
10200 [ - + ]: 499948 : if (locp >= locendp)
10201 : : {
10202 : 0 : fprintf (out, _(" <INVALID DATA>\n"));
10203 : 0 : break;
10204 : : }
10205 : 499948 : get_uleb128 (v2, locp, locendp);
10206 : 499948 : fprintf (out,
10207 : : " view pair %" PRId64 ", %" PRId64 "\n", v1, v2);
10208 : : }
10209 : :
10210 : 109918 : fprintf (out, "\n");
10211 : 109918 : readp = (unsigned char *) locendp;
10212 : 109918 : continue;
10213 : : }
10214 : :
10215 : 655664 : uint8_t kind = *readp++;
10216 : 655664 : uint64_t op1, op2, len;
10217 : :
10218 : : /* Skip padding. */
10219 [ - + ]: 655664 : if (start_of_list && kind == DW_LLE_end_of_list)
10220 : 0 : continue;
10221 : :
10222 [ + + ]: 655664 : if (start_of_list)
10223 : : {
10224 : 109982 : base = cu_base;
10225 : 109982 : fprintf (out, " Offset: %" PRIx64 ", Index: %" PRIx64 "\n",
10226 : 109982 : (uint64_t) (readp - (unsigned char *) data->d_buf - 1),
10227 : 109982 : (uint64_t) (readp - offset_array_start - 1));
10228 : 109982 : start_of_list = false;
10229 : : }
10230 : :
10231 : 655664 : fprintf (out, " %s", dwarf_loc_list_encoding_name (kind));
10232 [ + - - + : 655664 : switch (kind)
+ - + - +
- - ]
10233 : : {
10234 : 109982 : case DW_LLE_end_of_list:
10235 : 109982 : start_of_list = true;
10236 : 109982 : fprintf (out, "\n\n");
10237 : 109982 : break;
10238 : :
10239 : 0 : case DW_LLE_base_addressx:
10240 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
10241 : : {
10242 : 0 : invalid_entry:
10243 : 0 : error (0, 0, _("invalid loclists data"));
10244 : 0 : goto next_table;
10245 : : }
10246 : 0 : get_uleb128 (op1, readp, nexthdr);
10247 : 0 : fprintf (out, " %" PRIx64 "\n", op1);
10248 [ # # ]: 0 : if (! print_unresolved_addresses)
10249 : : {
10250 : 0 : Dwarf_Addr addr;
10251 [ # # ]: 0 : if (get_indexed_addr (cu, op1, &addr) != 0)
10252 : 0 : fprintf (out, " ???\n");
10253 : : else
10254 : : {
10255 : 0 : fprintf (out, " ");
10256 : 0 : print_dwarf_addr (dwflmod, address_size, addr, addr, out);
10257 : 0 : fprintf (out, "\n");
10258 : : }
10259 : : }
10260 : : break;
10261 : :
10262 : 0 : case DW_LLE_startx_endx:
10263 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
10264 : 0 : goto invalid_entry;
10265 : 0 : get_uleb128 (op1, readp, nexthdr);
10266 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
10267 : 0 : goto invalid_entry;
10268 : 0 : get_uleb128 (op2, readp, nexthdr);
10269 : 0 : fprintf (out, " %" PRIx64 ", %" PRIx64 "\n", op1, op2);
10270 [ # # ]: 0 : if (! print_unresolved_addresses)
10271 : : {
10272 : 0 : Dwarf_Addr addr1;
10273 : 0 : Dwarf_Addr addr2;
10274 [ # # ]: 0 : if (get_indexed_addr (cu, op1, &addr1) != 0
10275 [ # # ]: 0 : || get_indexed_addr (cu, op2, &addr2) != 0)
10276 : : {
10277 : 0 : fprintf (out, " ???..\n");
10278 : 0 : fprintf (out, " ???\n");
10279 : : }
10280 : : else
10281 : : {
10282 : 0 : fprintf (out, " ");
10283 : 0 : print_dwarf_addr (dwflmod, address_size,
10284 : : addr1, addr1, out);
10285 : 0 : fprintf (out, "..\n ");
10286 : 0 : print_dwarf_addr (dwflmod, address_size,
10287 : : addr2 - 1, addr2, out);
10288 : 0 : fprintf (out, "\n");
10289 : : }
10290 : : }
10291 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
10292 : 0 : goto invalid_entry;
10293 : 0 : get_uleb128 (len, readp, nexthdr);
10294 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < len)
10295 : 0 : goto invalid_entry;
10296 : 0 : print_ops (dwflmod, dbg, 8, 8, version,
10297 : : address_size, offset_size, cu, len, readp, out);
10298 : 0 : readp += len;
10299 : 0 : break;
10300 : :
10301 : 52 : case DW_LLE_startx_length:
10302 [ - + ]: 52 : if ((uint64_t) (nexthdr - readp) < 1)
10303 : 0 : goto invalid_entry;
10304 : 52 : get_uleb128 (op1, readp, nexthdr);
10305 [ - + ]: 52 : if ((uint64_t) (nexthdr - readp) < 1)
10306 : 0 : goto invalid_entry;
10307 : 52 : get_uleb128 (op2, readp, nexthdr);
10308 : 52 : fprintf (out, " %" PRIx64 ", %" PRIx64 "\n", op1, op2);
10309 [ + - ]: 52 : if (! print_unresolved_addresses)
10310 : : {
10311 : 52 : Dwarf_Addr addr1;
10312 : 52 : Dwarf_Addr addr2;
10313 [ - + ]: 52 : if (get_indexed_addr (cu, op1, &addr1) != 0)
10314 : : {
10315 : 0 : fprintf (out, " ???..\n");
10316 : 0 : fprintf (out, " ???\n");
10317 : : }
10318 : : else
10319 : : {
10320 : 52 : addr2 = addr1 + op2;
10321 : 52 : fprintf (out, " ");
10322 : 52 : print_dwarf_addr (dwflmod, address_size, addr1, addr1, out);
10323 : 52 : fprintf (out, "..\n ");
10324 : 52 : print_dwarf_addr (dwflmod, address_size,
10325 : : addr2 - 1, addr2, out);
10326 : 52 : fprintf (out, "\n");
10327 : : }
10328 : : }
10329 [ - + ]: 52 : if ((uint64_t) (nexthdr - readp) < 1)
10330 : 0 : goto invalid_entry;
10331 : 52 : get_uleb128 (len, readp, nexthdr);
10332 [ - + ]: 52 : if ((uint64_t) (nexthdr - readp) < len)
10333 : 0 : goto invalid_entry;
10334 : 52 : print_ops (dwflmod, dbg, 8, 8, version,
10335 : : address_size, offset_size, cu, len, readp, out);
10336 : 52 : readp += len;
10337 : 52 : break;
10338 : :
10339 : 477880 : case DW_LLE_offset_pair:
10340 [ - + ]: 477880 : if ((uint64_t) (nexthdr - readp) < 1)
10341 : 0 : goto invalid_entry;
10342 : 477880 : get_uleb128 (op1, readp, nexthdr);
10343 [ - + ]: 477880 : if ((uint64_t) (nexthdr - readp) < 1)
10344 : 0 : goto invalid_entry;
10345 : 477880 : get_uleb128 (op2, readp, nexthdr);
10346 : 477880 : fprintf (out, " %" PRIx64 ", %" PRIx64 "\n", op1, op2);
10347 [ + - ]: 477880 : if (! print_unresolved_addresses)
10348 : : {
10349 : 477880 : op1 += base;
10350 : 477880 : op2 += base;
10351 : 477880 : fprintf (out, " ");
10352 : 477880 : print_dwarf_addr (dwflmod, address_size, op1, op1, out);
10353 : 477880 : fprintf (out, "..\n ");
10354 : 477880 : print_dwarf_addr (dwflmod, address_size, op2 - 1, op2, out);
10355 : 477880 : fprintf (out, "\n");
10356 : : }
10357 [ - + ]: 477880 : if ((uint64_t) (nexthdr - readp) < 1)
10358 : 0 : goto invalid_entry;
10359 : 477880 : get_uleb128 (len, readp, nexthdr);
10360 [ - + ]: 477880 : if ((uint64_t) (nexthdr - readp) < len)
10361 : 0 : goto invalid_entry;
10362 : 477880 : print_ops (dwflmod, dbg, 8, 8, version,
10363 : : address_size, offset_size, cu, len, readp, out);
10364 : 477880 : readp += len;
10365 : 477880 : break;
10366 : :
10367 : 0 : case DW_LLE_default_location:
10368 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
10369 : 0 : goto invalid_entry;
10370 : 0 : get_uleb128 (len, readp, nexthdr);
10371 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < len)
10372 : 0 : goto invalid_entry;
10373 : 0 : print_ops (dwflmod, dbg, 8, 8, version,
10374 : : address_size, offset_size, cu, len, readp, out);
10375 : 0 : readp += len;
10376 : 0 : break;
10377 : :
10378 : 45630 : case DW_LLE_base_address:
10379 [ - + ]: 45630 : if (address_size == 4)
10380 : : {
10381 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 4)
10382 : 0 : goto invalid_entry;
10383 [ # # ]: 0 : op1 = read_4ubyte_unaligned_inc (dbg, readp);
10384 : : }
10385 : : else
10386 : : {
10387 [ - + ]: 45630 : if ((uint64_t) (nexthdr - readp) < 8)
10388 : 0 : goto invalid_entry;
10389 [ - + ]: 45630 : op1 = read_8ubyte_unaligned_inc (dbg, readp);
10390 : : }
10391 : 45630 : base = op1;
10392 : 45630 : fprintf (out, " 0x%" PRIx64 "\n", base);
10393 [ + - ]: 45630 : if (! print_unresolved_addresses)
10394 : : {
10395 : 45630 : fprintf (out, " ");
10396 : 45630 : print_dwarf_addr (dwflmod, address_size, base, base, out);
10397 : 45630 : fprintf (out, "\n");
10398 : : }
10399 : : break;
10400 : :
10401 : 0 : case DW_LLE_start_end:
10402 [ # # ]: 0 : if (address_size == 4)
10403 : : {
10404 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 8)
10405 : 0 : goto invalid_entry;
10406 [ # # ]: 0 : op1 = read_4ubyte_unaligned_inc (dbg, readp);
10407 [ # # ]: 0 : op2 = read_4ubyte_unaligned_inc (dbg, readp);
10408 : : }
10409 : : else
10410 : : {
10411 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 16)
10412 : 0 : goto invalid_entry;
10413 [ # # ]: 0 : op1 = read_8ubyte_unaligned_inc (dbg, readp);
10414 [ # # ]: 0 : op2 = read_8ubyte_unaligned_inc (dbg, readp);
10415 : : }
10416 : 0 : fprintf (out, " 0x%" PRIx64 "..0x%" PRIx64 "\n", op1, op2);
10417 [ # # ]: 0 : if (! print_unresolved_addresses)
10418 : : {
10419 : 0 : fprintf (out, " ");
10420 : 0 : print_dwarf_addr (dwflmod, address_size, op1, op1, out);
10421 : 0 : fprintf (out, "..\n ");
10422 : 0 : print_dwarf_addr (dwflmod, address_size, op2 - 1, op2, out);
10423 : 0 : fprintf (out, "\n");
10424 : : }
10425 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
10426 : 0 : goto invalid_entry;
10427 : 0 : get_uleb128 (len, readp, nexthdr);
10428 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < len)
10429 : 0 : goto invalid_entry;
10430 : 0 : print_ops (dwflmod, dbg, 8, 8, version,
10431 : : address_size, offset_size, cu, len, readp, out);
10432 : 0 : readp += len;
10433 : 0 : break;
10434 : :
10435 : 22120 : case DW_LLE_start_length:
10436 [ - + ]: 22120 : if (address_size == 4)
10437 : : {
10438 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 4)
10439 : 0 : goto invalid_entry;
10440 [ # # ]: 0 : op1 = read_4ubyte_unaligned_inc (dbg, readp);
10441 : : }
10442 : : else
10443 : : {
10444 [ - + ]: 22120 : if ((uint64_t) (nexthdr - readp) < 8)
10445 : 0 : goto invalid_entry;
10446 [ - + ]: 22120 : op1 = read_8ubyte_unaligned_inc (dbg, readp);
10447 : : }
10448 [ - + ]: 22120 : if ((uint64_t) (nexthdr - readp) < 1)
10449 : 0 : goto invalid_entry;
10450 : 22120 : get_uleb128 (op2, readp, nexthdr);
10451 : 22120 : fprintf (out, " 0x%" PRIx64 ", %" PRIx64 "\n", op1, op2);
10452 [ + + ]: 22120 : if (! print_unresolved_addresses)
10453 : : {
10454 : 22118 : op2 = op1 + op2;
10455 : 22118 : fprintf (out, " ");
10456 : 22118 : print_dwarf_addr (dwflmod, address_size, op1, op1, out);
10457 : 22118 : fprintf (out, "..\n ");
10458 : 22118 : print_dwarf_addr (dwflmod, address_size, op2 - 1, op2, out);
10459 : 22118 : fprintf (out, "\n");
10460 : : }
10461 [ - + ]: 22120 : if ((uint64_t) (nexthdr - readp) < 1)
10462 : 0 : goto invalid_entry;
10463 : 22120 : get_uleb128 (len, readp, nexthdr);
10464 [ - + ]: 22120 : if ((uint64_t) (nexthdr - readp) < len)
10465 : 0 : goto invalid_entry;
10466 : 22120 : print_ops (dwflmod, dbg, 8, 8, version,
10467 : : address_size, offset_size, cu, len, readp, out);
10468 : 22120 : readp += len;
10469 : 22120 : break;
10470 : :
10471 : 0 : case DW_LLE_GNU_view_pair:
10472 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
10473 : 0 : goto invalid_entry;
10474 : 0 : get_uleb128 (op1, readp, nexthdr);
10475 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
10476 : 0 : goto invalid_entry;
10477 : 0 : get_uleb128 (op2, readp, nexthdr);
10478 : 0 : fprintf (out, " %" PRIx64 ", %" PRIx64 "\n", op1, op2);
10479 : 0 : break;
10480 : :
10481 : 0 : default:
10482 : 0 : goto invalid_entry;
10483 : : }
10484 : : }
10485 : :
10486 : 3092 : next_table:
10487 [ - + ]: 3092 : if (readp != nexthdr)
10488 : : {
10489 : 0 : size_t padding = nexthdr - readp;
10490 : 0 : fprintf (out, _(" %zu padding bytes\n\n"), padding);
10491 : 0 : readp = nexthdr;
10492 : : }
10493 : : }
10494 : : }
10495 : :
10496 : :
10497 : : static void
10498 : 52 : print_debug_loc_section (Dwfl_Module *dwflmod,
10499 : : Ebl *ebl, GElf_Ehdr *ehdr,
10500 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg,
10501 : : FILE *out)
10502 : : {
10503 : 52 : Elf_Data *data = get_debug_elf_data (dbg, ebl, IDX_debug_loc, scn);
10504 [ - + ]: 52 : if (data == NULL)
10505 : 0 : return;
10506 : :
10507 : 52 : fprintf (out, _("\
10508 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
10509 : : elf_ndxscn (scn), section_name (ebl, shdr),
10510 : 52 : (uint64_t) shdr->sh_offset);
10511 : :
10512 : 52 : sort_listptr (&known_locsptr, "loclistptr");
10513 : 52 : size_t listptr_idx = 0;
10514 : :
10515 [ + - ]: 52 : uint_fast8_t address_size = ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 4 : 8;
10516 : 52 : uint_fast8_t offset_size = 4;
10517 : :
10518 : 52 : bool first = true;
10519 : 52 : Dwarf_Addr base = 0;
10520 : 52 : unsigned char *readp = data->d_buf;
10521 : 52 : unsigned char *const endp = (unsigned char *) data->d_buf + data->d_size;
10522 : 52 : Dwarf_CU *last_cu = NULL;
10523 [ + + ]: 490 : while (readp < endp)
10524 : : {
10525 : 438 : ptrdiff_t offset = readp - (unsigned char *) data->d_buf;
10526 : 438 : Dwarf_CU *cu = last_cu;
10527 : 438 : unsigned int attr = 0;
10528 : :
10529 [ + + - + ]: 438 : if (first && skip_listptr_hole (&known_locsptr, &listptr_idx,
10530 : : &address_size, &offset_size, &base,
10531 : : &cu, offset, &readp, endp, &attr))
10532 : 0 : continue;
10533 : :
10534 [ + + ]: 438 : if (last_cu != cu)
10535 : : {
10536 : 60 : Dwarf_Die cudie;
10537 [ - + ]: 60 : if (dwarf_cu_die (cu, &cudie,
10538 : : NULL, NULL, NULL, NULL,
10539 : : NULL, NULL) == NULL)
10540 : 0 : fprintf (out, _("\n Unknown CU base: "));
10541 : : else
10542 : 60 : fprintf (out, _("\n CU [%6" PRIx64 "] base: "),
10543 : : dwarf_dieoffset (&cudie));
10544 : 60 : print_dwarf_addr (dwflmod, address_size, base, base, out);
10545 : 60 : fprintf (out, "\n");
10546 : : }
10547 : 438 : last_cu = cu;
10548 : :
10549 [ - + ]: 438 : if (attr == DW_AT_GNU_locviews)
10550 : 0 : {
10551 : 0 : Dwarf_Off next_off = next_listptr_offset (&known_locsptr,
10552 : : &listptr_idx, offset);
10553 : 0 : const unsigned char *locp = readp;
10554 : 0 : const unsigned char *locendp;
10555 [ # # ]: 0 : if (next_off == 0
10556 : 0 : || next_off > (size_t) (endp
10557 [ # # ]: 0 : - (const unsigned char *) data->d_buf))
10558 : 0 : locendp = endp;
10559 : : else
10560 : 0 : locendp = (const unsigned char *) data->d_buf + next_off;
10561 : :
10562 [ # # ]: 0 : while (locp < locendp)
10563 : : {
10564 : 0 : uint64_t v1, v2;
10565 : 0 : get_uleb128 (v1, locp, locendp);
10566 [ # # ]: 0 : if (locp >= locendp)
10567 : : {
10568 : 0 : fprintf (out, _(" [%6tx] <INVALID DATA>\n"), offset);
10569 : 0 : break;
10570 : : }
10571 : 0 : get_uleb128 (v2, locp, locendp);
10572 [ # # ]: 0 : if (first) /* First view pair in a list. */
10573 : 0 : fprintf (out, " [%6tx] ", offset);
10574 : : else
10575 : 0 : fprintf (out, " ");
10576 : 0 : fprintf (out, "view pair %" PRId64 ", %" PRId64 "\n", v1, v2);
10577 : 0 : first = false;
10578 : : }
10579 : :
10580 : 0 : first = true;
10581 : 0 : readp = (unsigned char *) locendp;
10582 : 0 : continue;
10583 : : }
10584 : :
10585 : : /* GNU DebugFission encoded addresses as addrx. */
10586 : 1194 : bool is_debugfission = ((cu != NULL
10587 [ # # ]: 0 : || split_dwarf_cu_base (dbg, &cu, &base))
10588 [ - + + - ]: 438 : && (cu->version < 5
10589 [ + + ]: 438 : && cu->unit_type == DW_UT_split_compile));
10590 : 318 : if (!is_debugfission
10591 [ - + ]: 318 : && unlikely (data->d_size - offset < (size_t) address_size * 2))
10592 : : {
10593 : 0 : invalid_data:
10594 : 0 : fprintf (out, _(" [%6tx] <INVALID DATA>\n"), offset);
10595 : 0 : break;
10596 : : }
10597 : :
10598 : 438 : Dwarf_Addr begin;
10599 : 438 : Dwarf_Addr end;
10600 : 438 : bool use_base = true;
10601 [ + + ]: 438 : if (is_debugfission)
10602 : : {
10603 : 120 : const unsigned char *locp = readp;
10604 : 120 : const unsigned char *locendp = readp + data->d_size;
10605 [ - + ]: 120 : if (locp >= locendp)
10606 : 0 : goto invalid_data;
10607 : :
10608 : 120 : Dwarf_Word idx;
10609 : 120 : unsigned char code = *locp++;
10610 [ + - - + : 120 : switch (code)
- ]
10611 : : {
10612 : 40 : case DW_LLE_GNU_end_of_list_entry:
10613 : 40 : begin = 0;
10614 : 40 : end = 0;
10615 : 40 : break;
10616 : :
10617 : 0 : case DW_LLE_GNU_base_address_selection_entry:
10618 [ # # ]: 0 : if (locp >= locendp)
10619 : 0 : goto invalid_data;
10620 : 0 : begin = (Dwarf_Addr) -1;
10621 : 0 : get_uleb128 (idx, locp, locendp);
10622 [ # # ]: 0 : if (get_indexed_addr (cu, idx, &end) != 0)
10623 : 0 : end = idx; /* ... */
10624 : : break;
10625 : :
10626 : 0 : case DW_LLE_GNU_start_end_entry:
10627 [ # # ]: 0 : if (locp >= locendp)
10628 : 0 : goto invalid_data;
10629 : 0 : get_uleb128 (idx, locp, locendp);
10630 [ # # ]: 0 : if (get_indexed_addr (cu, idx, &begin) != 0)
10631 : 0 : begin = idx; /* ... */
10632 [ # # ]: 0 : if (locp >= locendp)
10633 : 0 : goto invalid_data;
10634 : 0 : get_uleb128 (idx, locp, locendp);
10635 [ # # ]: 0 : if (get_indexed_addr (cu, idx, &end) != 0)
10636 : 0 : end = idx; /* ... */
10637 : : use_base = false;
10638 : : break;
10639 : :
10640 : 80 : case DW_LLE_GNU_start_length_entry:
10641 [ - + ]: 80 : if (locp >= locendp)
10642 : 0 : goto invalid_data;
10643 : 80 : get_uleb128 (idx, locp, locendp);
10644 [ - + ]: 80 : if (get_indexed_addr (cu, idx, &begin) != 0)
10645 : 0 : begin = idx; /* ... */
10646 [ - + ]: 80 : if (locendp - locp < 4)
10647 : 0 : goto invalid_data;
10648 [ - + ]: 80 : end = read_4ubyte_unaligned_inc (dbg, locp);
10649 : 80 : end += begin;
10650 : 80 : use_base = false;
10651 : 80 : break;
10652 : :
10653 : 0 : default:
10654 : 0 : goto invalid_data;
10655 : : }
10656 : :
10657 : 120 : readp = (unsigned char *) locp;
10658 : : }
10659 [ + - ]: 318 : else if (address_size == 8)
10660 : : {
10661 [ + + ]: 318 : begin = read_8ubyte_unaligned_inc (dbg, readp);
10662 [ + + ]: 318 : end = read_8ubyte_unaligned_inc (dbg, readp);
10663 : : }
10664 : : else
10665 : : {
10666 [ # # ]: 0 : begin = read_4ubyte_unaligned_inc (dbg, readp);
10667 [ # # ]: 0 : end = read_4ubyte_unaligned_inc (dbg, readp);
10668 [ # # ]: 0 : if (begin == (Dwarf_Addr) (uint32_t) -1)
10669 : 0 : begin = (Dwarf_Addr) -1l;
10670 : : }
10671 : :
10672 [ - + ]: 438 : if (begin == (Dwarf_Addr) -1l) /* Base address entry. */
10673 : : {
10674 [ # # ]: 0 : if (first)
10675 : 0 : fprintf (out, " [%6tx] ", offset);
10676 : : else
10677 : 0 : fprintf (out, " ");
10678 : 0 : fputs (_("base address\n"), out);
10679 : 0 : fprintf (out, " ");
10680 : 0 : print_dwarf_addr (dwflmod, address_size, end, end, out);
10681 : 0 : fprintf (out, "\n");
10682 : 0 : base = end;
10683 : 0 : first = false;
10684 : : }
10685 [ + + + + ]: 438 : else if (begin == 0 && end == 0) /* End of list entry. */
10686 : : {
10687 [ - + ]: 148 : if (first)
10688 : 0 : fprintf (out, _(" [%6tx] empty list\n"), offset);
10689 : : first = true;
10690 : : }
10691 : : else
10692 : : {
10693 : : /* We have a location expression entry. */
10694 [ + + ]: 290 : uint_fast16_t len = read_2ubyte_unaligned_inc (dbg, readp);
10695 : :
10696 [ + + ]: 290 : if (first) /* First entry in a list. */
10697 : 148 : fprintf (out, " [%6tx] ", offset);
10698 : : else
10699 : 142 : fprintf (out, " ");
10700 : :
10701 : 290 : fprintf (out, "range %" PRIx64 ", %" PRIx64 "\n", begin, end);
10702 [ + + ]: 290 : if (! print_unresolved_addresses)
10703 : : {
10704 : 230 : Dwarf_Addr dab = use_base ? base + begin : begin;
10705 : 230 : Dwarf_Addr dae = use_base ? base + end : end;
10706 : 230 : fprintf (out, " ");
10707 : 230 : print_dwarf_addr (dwflmod, address_size, dab, dab, out);
10708 : 230 : fprintf (out, "..\n ");
10709 : 230 : print_dwarf_addr (dwflmod, address_size, dae - 1, dae, out);
10710 : 230 : fprintf (out, "\n");
10711 : : }
10712 : :
10713 [ - + ]: 290 : if (endp - readp <= (ptrdiff_t) len)
10714 : : {
10715 : 0 : fputs (_(" <INVALID DATA>\n"), out);
10716 : 0 : break;
10717 : : }
10718 : :
10719 [ + - ]: 580 : print_ops (dwflmod, dbg, 11, 11,
10720 : 290 : cu != NULL ? cu->version : 3,
10721 : : address_size, offset_size, cu, len, readp, out);
10722 : :
10723 : 290 : first = false;
10724 : 290 : readp += len;
10725 : : }
10726 : : }
10727 : : }
10728 : :
10729 : : struct mac_culist
10730 : : {
10731 : : Dwarf_Die die;
10732 : : Dwarf_Off offset;
10733 : : Dwarf_Files *files;
10734 : : struct mac_culist *next;
10735 : : };
10736 : :
10737 : :
10738 : : static int
10739 : 0 : mac_compare (const void *p1, const void *p2)
10740 : : {
10741 : 0 : struct mac_culist *m1 = (struct mac_culist *) p1;
10742 : 0 : struct mac_culist *m2 = (struct mac_culist *) p2;
10743 : :
10744 [ # # ]: 0 : if (m1->offset < m2->offset)
10745 : : return -1;
10746 [ # # ]: 0 : if (m1->offset > m2->offset)
10747 : 0 : return 1;
10748 : : return 0;
10749 : : }
10750 : :
10751 : :
10752 : : static void
10753 : 0 : print_debug_macinfo_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
10754 : : Ebl *ebl,
10755 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
10756 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg,
10757 : : FILE *out)
10758 : : {
10759 : 0 : Elf_Data *data = get_debug_elf_data (dbg, ebl, IDX_debug_macinfo, scn);
10760 [ # # ]: 0 : if (data == NULL)
10761 : 0 : return;
10762 : :
10763 : 0 : fprintf (out, _("\
10764 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
10765 : : elf_ndxscn (scn), section_name (ebl, shdr),
10766 : 0 : (uint64_t) shdr->sh_offset);
10767 : 0 : fputc ('\n', out);
10768 : :
10769 : : /* There is no function in libdw to iterate over the raw content of
10770 : : the section but it is easy enough to do. */
10771 : :
10772 : : /* Get the source file information for all CUs. */
10773 : 0 : Dwarf_Off offset;
10774 : 0 : Dwarf_Off ncu = 0;
10775 : 0 : size_t hsize;
10776 : 0 : struct mac_culist *culist = NULL;
10777 : 0 : size_t nculist = 0;
10778 [ # # ]: 0 : while (dwarf_nextcu (dbg, offset = ncu, &ncu, &hsize, NULL, NULL, NULL) == 0)
10779 : : {
10780 : 0 : Dwarf_Die cudie;
10781 [ # # ]: 0 : if (dwarf_offdie (dbg, offset + hsize, &cudie) == NULL)
10782 : 0 : continue;
10783 : :
10784 : 0 : Dwarf_Attribute attr;
10785 [ # # ]: 0 : if (dwarf_attr (&cudie, DW_AT_macro_info, &attr) == NULL)
10786 : 0 : continue;
10787 : :
10788 : 0 : Dwarf_Word macoff;
10789 [ # # ]: 0 : if (dwarf_formudata (&attr, &macoff) != 0)
10790 : 0 : continue;
10791 : :
10792 : 0 : struct mac_culist *newp = (struct mac_culist *) alloca (sizeof (*newp));
10793 : 0 : newp->die = cudie;
10794 : 0 : newp->offset = macoff;
10795 : 0 : newp->files = NULL;
10796 : 0 : newp->next = culist;
10797 : 0 : culist = newp;
10798 : 0 : ++nculist;
10799 : : }
10800 : :
10801 : : /* Convert the list into an array for easier consumption. */
10802 : 0 : struct mac_culist *cus = (struct mac_culist *) alloca ((nculist + 1)
10803 : : * sizeof (*cus));
10804 : : /* Add sentinel. */
10805 : 0 : cus[nculist].offset = data->d_size;
10806 : 0 : cus[nculist].files = (Dwarf_Files *) -1l;
10807 [ # # ]: 0 : if (nculist > 0)
10808 : : {
10809 [ # # ]: 0 : for (size_t cnt = nculist - 1; culist != NULL; --cnt)
10810 : : {
10811 [ # # ]: 0 : assert (cnt < nculist);
10812 : 0 : cus[cnt] = *culist;
10813 : 0 : culist = culist->next;
10814 : : }
10815 : :
10816 : : /* Sort the array according to the offset in the .debug_macinfo
10817 : : section. Note we keep the sentinel at the end. */
10818 : 0 : qsort (cus, nculist, sizeof (*cus), mac_compare);
10819 : : }
10820 : :
10821 : 0 : const unsigned char *readp = (const unsigned char *) data->d_buf;
10822 : 0 : const unsigned char *readendp = readp + data->d_size;
10823 : 0 : int level = 1;
10824 : :
10825 [ # # ]: 0 : while (readp < readendp)
10826 : : {
10827 : 0 : unsigned int opcode = *readp++;
10828 : 0 : unsigned int u128;
10829 : 0 : unsigned int u128_2;
10830 : 0 : const unsigned char *endp;
10831 : :
10832 [ # # # # ]: 0 : switch (opcode)
10833 : : {
10834 : 0 : case DW_MACINFO_define:
10835 : : case DW_MACINFO_undef:
10836 : : case DW_MACINFO_vendor_ext:
10837 : : /* For the first two opcodes the parameters are
10838 : : line, string
10839 : : For the latter
10840 : : number, string.
10841 : : We can treat these cases together. */
10842 : 0 : get_uleb128 (u128, readp, readendp);
10843 : :
10844 : 0 : endp = memchr (readp, '\0', readendp - readp);
10845 [ # # ]: 0 : if (unlikely (endp == NULL))
10846 : : {
10847 : 0 : fprintf (out, _("\
10848 : : %*s*** non-terminated string at end of section"),
10849 : : level, "");
10850 : 0 : return;
10851 : : }
10852 : :
10853 [ # # ]: 0 : if (opcode == DW_MACINFO_define)
10854 : 0 : fprintf (out, "%*s#define %s, line %u\n",
10855 : : level, "", (char *) readp, u128);
10856 [ # # ]: 0 : else if (opcode == DW_MACINFO_undef)
10857 : 0 : fprintf (out, "%*s#undef %s, line %u\n",
10858 : : level, "", (char *) readp, u128);
10859 : : else
10860 : 0 : fprintf (out, " #vendor-ext %s, number %u\n", (char *) readp, u128);
10861 : :
10862 : 0 : readp = endp + 1;
10863 : 0 : break;
10864 : :
10865 : 0 : case DW_MACINFO_start_file:
10866 : : /* The two parameters are line and file index, in this order. */
10867 : 0 : get_uleb128 (u128, readp, readendp);
10868 [ # # ]: 0 : if (readendp - readp < 1)
10869 : : {
10870 : 0 : fprintf (out, _("\
10871 : : %*s*** missing DW_MACINFO_start_file argument at end of section"),
10872 : : level, "");
10873 : 0 : return;
10874 : : }
10875 : 0 : get_uleb128 (u128_2, readp, readendp);
10876 : :
10877 : : /* Find the CU DIE for this file. */
10878 : 0 : size_t macoff = readp - (const unsigned char *) data->d_buf;
10879 : 0 : const char *fname = "???";
10880 [ # # # # ]: 0 : if (macoff >= cus[0].offset && cus[0].offset != data->d_size)
10881 : : {
10882 [ # # # # ]: 0 : while (macoff >= cus[1].offset && cus[1].offset != data->d_size)
10883 : 0 : ++cus;
10884 : :
10885 [ # # ]: 0 : if (cus[0].files == NULL
10886 [ # # ]: 0 : && dwarf_getsrcfiles (&cus[0].die, &cus[0].files, NULL) != 0)
10887 : 0 : cus[0].files = (Dwarf_Files *) -1l;
10888 : :
10889 [ # # ]: 0 : if (cus[0].files != (Dwarf_Files *) -1l)
10890 : 0 : fname = (dwarf_filesrc (cus[0].files, u128_2, NULL, NULL)
10891 [ # # ]: 0 : ?: "???");
10892 : : }
10893 : :
10894 : 0 : fprintf (out, "%*sstart_file %u, [%u] %s\n",
10895 : : level, "", u128, u128_2, fname);
10896 : 0 : ++level;
10897 : 0 : break;
10898 : :
10899 : 0 : case DW_MACINFO_end_file:
10900 : 0 : --level;
10901 : 0 : fprintf (out, "%*send_file\n", level, "");
10902 : : /* Nothing more to do. */
10903 : 0 : break;
10904 : :
10905 : 0 : default:
10906 : : // XXX gcc seems to generate files with a trailing zero.
10907 [ # # # # ]: 0 : if (unlikely (opcode != 0 || readp != readendp))
10908 : 0 : fprintf (out, "%*s*** invalid opcode %u\n", level, "", opcode);
10909 : : break;
10910 : : }
10911 : : }
10912 : : }
10913 : :
10914 : :
10915 : : static void
10916 : 8 : print_debug_macro_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
10917 : : Ebl *ebl,
10918 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
10919 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg,
10920 : : FILE *out)
10921 : : {
10922 : 8 : Elf_Data *data = get_debug_elf_data (dbg, ebl, IDX_debug_macro, scn);
10923 [ + - ]: 8 : if (data == NULL)
10924 : 0 : return;
10925 : :
10926 : 8 : fprintf (out, _("\
10927 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
10928 : : elf_ndxscn (scn), section_name (ebl, shdr),
10929 : 8 : (uint64_t) shdr->sh_offset);
10930 : 8 : fputc ('\n', out);
10931 : :
10932 : : /* Get the source file information for all CUs. Uses same
10933 : : datastructure as macinfo. But uses offset field to directly
10934 : : match .debug_line offset. And just stored in a list. */
10935 : 8 : Dwarf_Off offset;
10936 : 8 : Dwarf_Off ncu = 0;
10937 : 8 : size_t hsize;
10938 : 8 : struct mac_culist *culist = NULL;
10939 [ + + ]: 20 : while (dwarf_nextcu (dbg, offset = ncu, &ncu, &hsize, NULL, NULL, NULL) == 0)
10940 : : {
10941 : 12 : Dwarf_Die cudie;
10942 [ - + ]: 12 : if (dwarf_offdie (dbg, offset + hsize, &cudie) == NULL)
10943 : 0 : continue;
10944 : :
10945 : 12 : Dwarf_Attribute attr;
10946 [ - + ]: 12 : if (dwarf_attr (&cudie, DW_AT_stmt_list, &attr) == NULL)
10947 : 0 : continue;
10948 : :
10949 : 12 : Dwarf_Word lineoff;
10950 [ - + ]: 12 : if (dwarf_formudata (&attr, &lineoff) != 0)
10951 : 0 : continue;
10952 : :
10953 : 12 : struct mac_culist *newp = (struct mac_culist *) alloca (sizeof (*newp));
10954 : 12 : newp->die = cudie;
10955 : 12 : newp->offset = lineoff;
10956 : 12 : newp->files = NULL;
10957 : 12 : newp->next = culist;
10958 : 12 : culist = newp;
10959 : : }
10960 : :
10961 : 8 : const unsigned char *readp = (const unsigned char *) data->d_buf;
10962 : 8 : const unsigned char *readendp = readp + data->d_size;
10963 : :
10964 [ + + ]: 28 : while (readp < readendp)
10965 : : {
10966 : 40 : fprintf (out, _(" Offset: 0x%" PRIx64 "\n"),
10967 : 20 : (uint64_t) (readp - (const unsigned char *) data->d_buf));
10968 : :
10969 : : // Header, 2 byte version, 1 byte flag, optional .debug_line offset,
10970 : : // optional vendor extension macro entry table.
10971 [ - + ]: 20 : if (readp + 2 > readendp)
10972 : : {
10973 : 0 : invalid_data:
10974 : 0 : error (0, 0, _("invalid data"));
10975 : 0 : return;
10976 : : }
10977 [ - + ]: 20 : const uint16_t vers = read_2ubyte_unaligned_inc (dbg, readp);
10978 : 20 : fprintf (out, _(" Version: %" PRIu16 "\n"), vers);
10979 : :
10980 : : // Version 4 is the GNU extension for DWARF4. DWARF5 will use version
10981 : : // 5 when it gets standardized.
10982 [ - + ]: 20 : if (vers != 4 && vers != 5)
10983 : : {
10984 : 0 : fprintf (out, _(" unknown version, cannot parse section\n"));
10985 : 0 : return;
10986 : : }
10987 : :
10988 [ - + ]: 20 : if (readp + 1 > readendp)
10989 : 0 : goto invalid_data;
10990 : 20 : const unsigned char flag = *readp++;
10991 : 20 : fprintf (out, _(" Flag: 0x%" PRIx8), flag);
10992 [ + + ]: 20 : if (flag != 0)
10993 : : {
10994 : 12 : fprintf (out, " (");
10995 [ - + ]: 12 : if ((flag & 0x01) != 0)
10996 : : {
10997 : 0 : fprintf (out, "offset_size");
10998 [ # # ]: 0 : if ((flag & 0xFE) != 0)
10999 : 0 : fprintf (out, ", ");
11000 : : }
11001 [ + - ]: 12 : if ((flag & 0x02) != 0)
11002 : : {
11003 : 12 : fprintf (out, "debug_line_offset");
11004 [ - + ]: 12 : if ((flag & 0xFC) != 0)
11005 : 0 : fprintf (out, ", ");
11006 : : }
11007 [ # # ]: 0 : if ((flag & 0x04) != 0)
11008 : : {
11009 : 0 : fprintf (out, "operands_table");
11010 [ # # ]: 0 : if ((flag & 0xF8) != 0)
11011 : 0 : fprintf (out, ", ");
11012 : : }
11013 [ - + ]: 12 : if ((flag & 0xF8) != 0)
11014 : 0 : fprintf (out, "unknown");
11015 : 12 : fprintf (out, ")");
11016 : : }
11017 : 20 : fprintf (out, "\n");
11018 : :
11019 [ + - ]: 20 : unsigned int offset_len = (flag & 0x01) ? 8 : 4;
11020 : 20 : fprintf (out, _(" Offset length: %" PRIu8 "\n"), offset_len);
11021 : 20 : Dwarf_Off line_offset = -1;
11022 [ + + ]: 20 : if (flag & 0x02)
11023 : : {
11024 [ - + ]: 12 : if (offset_len == 8)
11025 [ # # ]: 0 : line_offset = read_8ubyte_unaligned_inc (dbg, readp);
11026 : : else
11027 [ - + ]: 12 : line_offset = read_4ubyte_unaligned_inc (dbg, readp);
11028 : 12 : fprintf (out, _(" .debug_line offset: 0x%" PRIx64 "\n"),
11029 : : line_offset);
11030 : : }
11031 : :
11032 : 12 : struct mac_culist *cu = NULL;
11033 [ + - ]: 12 : if (line_offset != (Dwarf_Off) -1)
11034 : : {
11035 : : cu = culist;
11036 [ + - + + ]: 16 : while (cu != NULL && line_offset != cu->offset)
11037 : 4 : cu = cu->next;
11038 : : }
11039 : :
11040 [ - + ]: 20 : Dwarf_Off str_offsets_base = str_offsets_base_off (dbg, (cu != NULL
11041 : : ? cu->die.cu
11042 : : : NULL));
11043 : :
11044 : 20 : const unsigned char *vendor[DW_MACRO_hi_user - DW_MACRO_lo_user + 1];
11045 [ - + ]: 20 : memset (vendor, 0, sizeof vendor);
11046 [ - + ]: 20 : if (flag & 0x04)
11047 : : {
11048 : : // 1 byte length, for each item, 1 byte opcode, uleb128 number
11049 : : // of arguments, for each argument 1 byte form code.
11050 [ # # ]: 0 : if (readp + 1 > readendp)
11051 : 0 : goto invalid_data;
11052 : 0 : unsigned int tlen = *readp++;
11053 : 0 : fprintf (out, _(" extension opcode table, %" PRIu8 " items:\n"),
11054 : : tlen);
11055 [ # # ]: 0 : for (unsigned int i = 0; i < tlen; i++)
11056 : : {
11057 [ # # ]: 0 : if (readp + 1 > readendp)
11058 : 0 : goto invalid_data;
11059 : 0 : unsigned int opcode = *readp++;
11060 : 0 : fprintf (out, _(" [%" PRIx8 "]"), opcode);
11061 : 0 : if (opcode < DW_MACRO_lo_user
11062 [ # # ]: 0 : || opcode > DW_MACRO_hi_user)
11063 : 0 : goto invalid_data;
11064 : : // Record the start of description for this vendor opcode.
11065 : : // uleb128 nr args, 1 byte per arg form.
11066 : 0 : vendor[opcode - DW_MACRO_lo_user] = readp;
11067 [ # # ]: 0 : if (readp + 1 > readendp)
11068 : 0 : goto invalid_data;
11069 : 0 : unsigned int args = *readp++;
11070 [ # # ]: 0 : if (args > 0)
11071 : : {
11072 : 0 : fprintf (out, _(" %" PRIu8 " arguments:"), args);
11073 : 0 : while (args > 0)
11074 : : {
11075 [ # # ]: 0 : if (readp + 1 > readendp)
11076 : 0 : goto invalid_data;
11077 : 0 : unsigned int form = *readp++;
11078 : 0 : fprintf (out, " %s", dwarf_form_name (form));
11079 [ # # ]: 0 : if (! libdw_valid_user_form (form))
11080 : 0 : goto invalid_data;
11081 : 0 : args--;
11082 [ # # ]: 0 : if (args > 0)
11083 : 0 : fputc (',', out);
11084 : : }
11085 : : }
11086 : : else
11087 : 0 : fprintf (out, _(" no arguments."));
11088 : 0 : fputc ('\n', out);
11089 : : }
11090 : : }
11091 : 20 : fputc ('\n', out);
11092 : :
11093 : 20 : int level = 1;
11094 [ - + ]: 20 : if (readp + 1 > readendp)
11095 : 0 : goto invalid_data;
11096 : 20 : unsigned int opcode = *readp++;
11097 [ + + ]: 3046 : while (opcode != 0)
11098 : : {
11099 : 3026 : unsigned int u128;
11100 : 3026 : unsigned int u128_2;
11101 : 3026 : const unsigned char *endp;
11102 : 3026 : uint64_t off;
11103 : :
11104 [ + + + - : 3026 : switch (opcode)
+ + + - -
- + + - ]
11105 : : {
11106 : 20 : case DW_MACRO_start_file:
11107 : 20 : get_uleb128 (u128, readp, readendp);
11108 [ - + ]: 20 : if (readp >= readendp)
11109 : 0 : goto invalid_data;
11110 : 20 : get_uleb128 (u128_2, readp, readendp);
11111 : :
11112 : : /* Find the CU DIE that matches this line offset. */
11113 : 20 : const char *fname = "???";
11114 [ - + ]: 20 : if (cu != NULL)
11115 : : {
11116 [ + + ]: 20 : if (cu->files == NULL
11117 [ - + ]: 12 : && dwarf_getsrcfiles (&cu->die, &cu->files,
11118 : : NULL) != 0)
11119 : 0 : cu->files = (Dwarf_Files *) -1l;
11120 : :
11121 [ - + ]: 20 : if (cu->files != (Dwarf_Files *) -1l)
11122 : 20 : fname = (dwarf_filesrc (cu->files, u128_2,
11123 [ - + ]: 20 : NULL, NULL) ?: "???");
11124 : : }
11125 : 20 : fprintf (out, "%*sstart_file %u, [%u] %s\n",
11126 : : level, "", u128, u128_2, fname);
11127 : 20 : ++level;
11128 : 20 : break;
11129 : :
11130 : 20 : case DW_MACRO_end_file:
11131 : 20 : --level;
11132 : 20 : fprintf (out, "%*send_file\n", level, "");
11133 : 20 : break;
11134 : :
11135 : 2 : case DW_MACRO_define:
11136 : 2 : get_uleb128 (u128, readp, readendp);
11137 : 2 : endp = memchr (readp, '\0', readendp - readp);
11138 [ - + ]: 2 : if (endp == NULL)
11139 : 0 : goto invalid_data;
11140 : 2 : fprintf (out, "%*s#define %s, line %u\n",
11141 : : level, "", readp, u128);
11142 : 2 : readp = endp + 1;
11143 : 2 : break;
11144 : :
11145 : 0 : case DW_MACRO_undef:
11146 : 0 : get_uleb128 (u128, readp, readendp);
11147 : 0 : endp = memchr (readp, '\0', readendp - readp);
11148 [ # # ]: 0 : if (endp == NULL)
11149 : 0 : goto invalid_data;
11150 : 0 : fprintf (out, "%*s#undef %s, line %u\n",
11151 : : level, "", readp, u128);
11152 : 0 : readp = endp + 1;
11153 : 0 : break;
11154 : :
11155 : 1414 : case DW_MACRO_define_strp:
11156 : 1414 : get_uleb128 (u128, readp, readendp);
11157 [ - + ]: 1414 : if (readp + offset_len > readendp)
11158 : 0 : goto invalid_data;
11159 [ - + ]: 1414 : if (offset_len == 8)
11160 [ # # ]: 0 : off = read_8ubyte_unaligned_inc (dbg, readp);
11161 : : else
11162 [ - + ]: 1414 : off = read_4ubyte_unaligned_inc (dbg, readp);
11163 : 1414 : fprintf (out, "%*s#define %s, line %u (indirect)\n",
11164 : : level, "", dwarf_getstring (dbg, off, NULL), u128);
11165 : 1414 : break;
11166 : :
11167 : 2 : case DW_MACRO_undef_strp:
11168 : 2 : get_uleb128 (u128, readp, readendp);
11169 [ - + ]: 2 : if (readp + offset_len > readendp)
11170 : 0 : goto invalid_data;
11171 [ - + ]: 2 : if (offset_len == 8)
11172 [ # # ]: 0 : off = read_8ubyte_unaligned_inc (dbg, readp);
11173 : : else
11174 [ - + ]: 2 : off = read_4ubyte_unaligned_inc (dbg, readp);
11175 : 2 : fprintf (out, "%*s#undef %s, line %u (indirect)\n",
11176 : : level, "", dwarf_getstring (dbg, off, NULL), u128);
11177 : 2 : break;
11178 : :
11179 : 12 : case DW_MACRO_import:
11180 [ - + ]: 12 : if (readp + offset_len > readendp)
11181 : 0 : goto invalid_data;
11182 [ - + ]: 12 : if (offset_len == 8)
11183 [ # # ]: 0 : off = read_8ubyte_unaligned_inc (dbg, readp);
11184 : : else
11185 [ - + ]: 12 : off = read_4ubyte_unaligned_inc (dbg, readp);
11186 : 12 : fprintf (out, "%*s#include offset 0x%" PRIx64 "\n",
11187 : : level, "", off);
11188 : 12 : break;
11189 : :
11190 : 0 : case DW_MACRO_define_sup:
11191 : 0 : get_uleb128 (u128, readp, readendp);
11192 : 0 : fprintf (out, "%*s#define ", level, "");
11193 : 0 : readp = print_form_data (dbg, DW_FORM_strp_sup,
11194 : : readp, readendp, offset_len,
11195 : : str_offsets_base, out);
11196 : 0 : fprintf (out, ", line %u (sup)\n", u128);
11197 : 0 : break;
11198 : :
11199 : 0 : case DW_MACRO_undef_sup:
11200 : 0 : get_uleb128 (u128, readp, readendp);
11201 : 0 : fprintf (out, "%*s#undef ", level, "");
11202 : 0 : readp = print_form_data (dbg, DW_FORM_strp_sup,
11203 : : readp, readendp, offset_len,
11204 : : str_offsets_base, out);
11205 : 0 : fprintf (out, ", line %u (sup)\n", u128);
11206 : 0 : break;
11207 : :
11208 : 0 : case DW_MACRO_import_sup:
11209 [ # # ]: 0 : if (readp + offset_len > readendp)
11210 : 0 : goto invalid_data;
11211 [ # # ]: 0 : if (offset_len == 8)
11212 [ # # ]: 0 : off = read_8ubyte_unaligned_inc (dbg, readp);
11213 : : else
11214 [ # # ]: 0 : off = read_4ubyte_unaligned_inc (dbg, readp);
11215 : : // XXX Needs support for reading from supplementary object file.
11216 : 0 : fprintf (out, "%*s#include offset 0x%" PRIx64 " (sup)\n",
11217 : : level, "", off);
11218 : 0 : break;
11219 : :
11220 : 1552 : case DW_MACRO_define_strx:
11221 : 1552 : get_uleb128 (u128, readp, readendp);
11222 : 1552 : fprintf (out, "%*s#define ", level, "");
11223 : 1552 : readp = print_form_data (dbg, DW_FORM_strx,
11224 : : readp, readendp, offset_len,
11225 : : str_offsets_base, out);
11226 : 1552 : fprintf (out, ", line %u (strx)\n", u128);
11227 : 1552 : break;
11228 : :
11229 : 4 : case DW_MACRO_undef_strx:
11230 : 4 : get_uleb128 (u128, readp, readendp);
11231 : 4 : fprintf (out, "%*s#undef ", level, "");
11232 : 4 : readp = print_form_data (dbg, DW_FORM_strx,
11233 : : readp, readendp, offset_len,
11234 : : str_offsets_base, out);
11235 : 4 : fprintf (out, ", line %u (strx)\n", u128);
11236 : 4 : break;
11237 : :
11238 : : default:
11239 : 0 : fprintf (out, "%*svendor opcode 0x%" PRIx8, level, "", opcode);
11240 [ # # ]: 0 : if (opcode < DW_MACRO_lo_user
11241 : : || opcode > DW_MACRO_lo_user
11242 [ # # ]: 0 : || vendor[opcode - DW_MACRO_lo_user] == NULL)
11243 : 0 : goto invalid_data;
11244 : :
11245 : 0 : const unsigned char *op_desc;
11246 : 0 : op_desc = vendor[opcode - DW_MACRO_lo_user];
11247 : :
11248 : : // Just skip the arguments, we cannot really interpret them,
11249 : : // but print as much as we can.
11250 : 0 : unsigned int args = *op_desc++;
11251 [ # # # # ]: 0 : while (args > 0 && readp < readendp)
11252 : : {
11253 : 0 : unsigned int form = *op_desc++;
11254 : 0 : readp = print_form_data (dbg, form, readp, readendp,
11255 : : offset_len, str_offsets_base, out);
11256 : 0 : args--;
11257 [ # # ]: 0 : if (args > 0)
11258 : 0 : fprintf (out, ", ");
11259 : : }
11260 : 0 : fputc ('\n', out);
11261 : : }
11262 : :
11263 [ - + ]: 3026 : if (readp + 1 > readendp)
11264 : 0 : goto invalid_data;
11265 : 3026 : opcode = *readp++;
11266 [ + + ]: 3026 : if (opcode == 0)
11267 : 20 : fputc ('\n', out);
11268 : : }
11269 : : }
11270 : : }
11271 : :
11272 : :
11273 : : typedef struct {
11274 : : int n;
11275 : : FILE *out;
11276 : : } pubnames_arg;
11277 : :
11278 : : /* Callback for printing global names. */
11279 : : static int
11280 : 68 : print_pubnames (Dwarf *dbg __attribute__ ((unused)), Dwarf_Global *global,
11281 : : void *arg)
11282 : : {
11283 : 68 : pubnames_arg *p = (pubnames_arg *) arg;
11284 : :
11285 : 136 : fprintf (p->out, _(" [%5d] DIE offset: %6" PRId64
11286 : : ", CU DIE offset: %6" PRId64 ", name: %s\n"),
11287 : 68 : (p->n)++, global->die_offset, global->cu_offset, global->name);
11288 : :
11289 : 68 : return 0;
11290 : : }
11291 : :
11292 : :
11293 : : /* Print the known exported symbols in the DWARF section '.debug_pubnames'. */
11294 : : static void
11295 : 16 : print_debug_pubnames_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
11296 : : Ebl *ebl,
11297 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
11298 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg,
11299 : : FILE *out)
11300 : : {
11301 : : /* Check section actually exists. */
11302 [ - + ]: 16 : if (get_debug_elf_data (dbg, ebl, IDX_debug_pubnames, scn) == NULL)
11303 : 0 : return;
11304 : :
11305 : 16 : fprintf (out, _("\nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
11306 : : elf_ndxscn (scn), section_name (ebl, shdr),
11307 : 16 : (uint64_t) shdr->sh_offset);
11308 : :
11309 : 16 : pubnames_arg arg = { 0, out };
11310 : 16 : (void) dwarf_getpubnames (dbg, print_pubnames, &arg, 0);
11311 : : }
11312 : :
11313 : : /* Print the content of the DWARF string section '.debug_str'
11314 : : or 'debug_line_str'. */
11315 : : static void
11316 : 134 : print_debug_str_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
11317 : : Ebl *ebl,
11318 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
11319 : : Elf_Scn *scn, GElf_Shdr *shdr,
11320 : : Dwarf *dbg __attribute__ ((unused)),
11321 : : FILE *out)
11322 : : {
11323 : 134 : const char *name = section_name (ebl, shdr);
11324 [ + + ]: 134 : int idx = ((name != NULL && strstr (name, "debug_line_str") != NULL)
11325 : : ? IDX_debug_line_str : IDX_debug_str);
11326 : 134 : Elf_Data *data = get_debug_elf_data (dbg, ebl, idx, scn);
11327 [ + - ]: 134 : if (data == NULL)
11328 : : return;
11329 : :
11330 : 134 : const size_t sh_size = data->d_size;
11331 : :
11332 : : /* Compute floor(log16(shdr->sh_size)). */
11333 : 134 : GElf_Addr tmp = sh_size;
11334 : 134 : int digits = 1;
11335 [ + + ]: 466 : while (tmp >= 16)
11336 : : {
11337 : 332 : ++digits;
11338 : 332 : tmp >>= 4;
11339 : : }
11340 : 134 : digits = MAX (4, digits);
11341 : :
11342 : 134 : fprintf (out, _("\nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"
11343 : : " %*s String\n"),
11344 : : elf_ndxscn (scn),
11345 : 134 : section_name (ebl, shdr), (uint64_t) shdr->sh_offset,
11346 : : /* TRANS: the debugstr| prefix makes the string unique. */
11347 [ + - ]: 134 : digits + 2, sgettext ("debugstr|Offset"));
11348 : :
11349 : 134 : Dwarf_Off offset = 0;
11350 [ + + ]: 162514 : while (offset < sh_size)
11351 : : {
11352 : 162380 : size_t len;
11353 : 162380 : const char *str = (const char *) data->d_buf + offset;
11354 : 162380 : const char *endp = memchr (str, '\0', sh_size - offset);
11355 [ - + ]: 162380 : if (unlikely (endp == NULL))
11356 : : {
11357 : 0 : fprintf (out, _(" *** error, missing string terminator\n"));
11358 : 0 : break;
11359 : : }
11360 : :
11361 : 162380 : fprintf (out, " [%*" PRIx64 "] \"%s\"\n",
11362 : : digits, (uint64_t) offset, str);
11363 : 162380 : len = endp - str;
11364 : 162380 : offset += len + 1;
11365 : : }
11366 : : }
11367 : :
11368 : : static void
11369 : 12 : print_debug_str_offsets_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
11370 : : Ebl *ebl,
11371 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
11372 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg,
11373 : : FILE *out)
11374 : : {
11375 : 12 : Elf_Data *data = get_debug_elf_data (dbg, ebl, IDX_debug_str_offsets, scn);
11376 [ + - ]: 12 : if (data == NULL)
11377 : : return;
11378 : :
11379 : 12 : fprintf (out, _("\
11380 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
11381 : : elf_ndxscn (scn), section_name (ebl, shdr),
11382 : 12 : (uint64_t) shdr->sh_offset);
11383 : :
11384 [ + - ]: 12 : if (shdr->sh_size == 0)
11385 : : return;
11386 : :
11387 : 12 : size_t idx = 0;
11388 : 12 : sort_listptr (&known_stroffbases, "str_offsets");
11389 : :
11390 : 12 : const unsigned char *start = (const unsigned char *) data->d_buf;
11391 : 12 : const unsigned char *readp = start;
11392 : 12 : const unsigned char *readendp = ((const unsigned char *) data->d_buf
11393 : 12 : + data->d_size);
11394 : :
11395 [ + + ]: 28 : while (readp < readendp)
11396 : : {
11397 : : /* Most string offset tables will have a header. For split
11398 : : dwarf unit GNU DebugFission didn't add one. But they were
11399 : : also only defined for split units (main or skeleton units
11400 : : didn't have indirect strings). So if we don't have a
11401 : : DW_AT_str_offsets_base at all and this is offset zero, then
11402 : : just start printing offsets immediately, if this is a .dwo
11403 : : section. */
11404 : 16 : Dwarf_Off off = (Dwarf_Off) (readp
11405 : 16 : - (const unsigned char *) data->d_buf);
11406 : :
11407 : 16 : fprintf (out, "Table at offset %" PRIx64 " ", off);
11408 : :
11409 : : /* Find the first CU that could plausibly be associated with
11410 : : this string offsets index. Skip CUs that point
11411 : : str_offsets_base before this table. */
11412 [ + + ]: 16 : struct listptr *listptr = get_listptr (&known_stroffbases, idx);
11413 [ + + ]: 12 : while (listptr != NULL && listptr->offset < off)
11414 [ + - + + ]: 24 : listptr = get_listptr (&known_stroffbases, ++idx);
11415 : :
11416 : 16 : const unsigned char *next_unitp = readendp;
11417 : 16 : uint8_t offset_size;
11418 : 16 : bool has_header;
11419 [ + + ]: 16 : if (listptr == NULL)
11420 : : {
11421 : : /* This can happen for .dwo files. There is only an header
11422 : : in the case this is a version 5 split DWARF file. */
11423 : 8 : Dwarf_CU *cu;
11424 : 8 : uint8_t unit_type;
11425 [ - + ]: 8 : if (dwarf_get_units (dbg, NULL, &cu, NULL, &unit_type,
11426 : : NULL, NULL) != 0)
11427 : : {
11428 : 0 : error (0, 0, "Warning: Cannot find any DWARF unit.");
11429 : : /* Just guess some values. */
11430 : 0 : has_header = false;
11431 : 0 : offset_size = 4;
11432 : : }
11433 [ + - ]: 8 : else if (off == 0
11434 : 8 : && (unit_type == DW_UT_split_type
11435 [ + - ]: 8 : || unit_type == DW_UT_split_compile))
11436 : : {
11437 : 8 : has_header = cu->version > 4;
11438 : 8 : offset_size = cu->offset_size;
11439 : : }
11440 : : else
11441 : : {
11442 : 0 : error (0, 0,
11443 : : "Warning: No CU references .debug_str_offsets after %"
11444 : : PRIx64, off);
11445 : 0 : has_header = cu->version > 4;
11446 : 0 : offset_size = cu->offset_size;
11447 : : }
11448 : 8 : fprintf (out, "\n");
11449 : : }
11450 : : else
11451 : : {
11452 : : /* This must be DWARF5, since GNU DebugFission didn't define
11453 : : DW_AT_str_offsets_base. */
11454 : 8 : has_header = true;
11455 : :
11456 : 8 : Dwarf_Die cudie;
11457 [ - + ]: 8 : if (dwarf_cu_die (listptr->cu, &cudie,
11458 : : NULL, NULL, NULL, NULL,
11459 : : NULL, NULL) == NULL)
11460 : 0 : fprintf (out, "Unknown CU (%s):\n", dwarf_errmsg (-1));
11461 : : else
11462 : 8 : fprintf (out, "for CU [%6" PRIx64 "]:\n", dwarf_dieoffset (&cudie));
11463 : : }
11464 : :
11465 [ + + ]: 16 : if (has_header)
11466 : : {
11467 : 12 : uint64_t unit_length;
11468 : 12 : uint16_t version;
11469 : 12 : uint16_t padding;
11470 : :
11471 [ - + ]: 12 : unit_length = read_4ubyte_unaligned_inc (dbg, readp);
11472 [ - + ]: 12 : if (unlikely (unit_length == 0xffffffff))
11473 : : {
11474 [ # # ]: 0 : if (unlikely (readp > readendp - 8))
11475 : : {
11476 : 0 : invalid_data:
11477 : 0 : error (0, 0, "Invalid data");
11478 : 0 : return;
11479 : : }
11480 [ # # ]: 0 : unit_length = read_8ubyte_unaligned_inc (dbg, readp);
11481 : 0 : offset_size = 8;
11482 : : }
11483 : : else
11484 : : offset_size = 4;
11485 : :
11486 : 12 : fprintf (out, "\n");
11487 : 12 : fprintf (out, _(" Length: %8" PRIu64 "\n"),
11488 : : unit_length);
11489 : 12 : fprintf (out, _(" Offset size: %8" PRIu8 "\n"),
11490 : : offset_size);
11491 : :
11492 : : /* We need at least 2-bytes (version) + 2-bytes (padding) =
11493 : : 4 bytes to complete the header. And this unit cannot go
11494 : : beyond the section data. */
11495 [ + - ]: 12 : if (readp > readendp - 4
11496 [ + - ]: 12 : || unit_length < 4
11497 [ - + ]: 12 : || unit_length > (uint64_t) (readendp - readp))
11498 : 0 : goto invalid_data;
11499 : :
11500 : 12 : next_unitp = readp + unit_length;
11501 : :
11502 [ - + ]: 12 : version = read_2ubyte_unaligned_inc (dbg, readp);
11503 : 12 : fprintf (out, _(" DWARF version: %8" PRIu16 "\n"), version);
11504 : :
11505 [ - + ]: 12 : if (version != 5)
11506 : : {
11507 : 0 : error (0, 0, _("Unknown version"));
11508 : 0 : goto next_unit;
11509 : : }
11510 : :
11511 [ - + ]: 12 : padding = read_2ubyte_unaligned_inc (dbg, readp);
11512 : 12 : fprintf (out, _(" Padding: %8" PRIx16 "\n"), padding);
11513 : :
11514 [ + + ]: 12 : if (listptr != NULL
11515 [ - + ]: 8 : && listptr->offset != (Dwarf_Off) (readp - start))
11516 : : {
11517 : 0 : error (0, 0, "String offsets index doesn't start after header");
11518 : 0 : goto next_unit;
11519 : : }
11520 : :
11521 : 12 : fprintf (out, "\n");
11522 : : }
11523 : :
11524 : 16 : int digits = 1;
11525 : 16 : size_t offsets = (next_unitp - readp) / offset_size;
11526 [ + + ]: 24 : while (offsets >= 10)
11527 : : {
11528 : 8 : ++digits;
11529 : 8 : offsets /= 10;
11530 : : }
11531 : :
11532 : 16 : unsigned int uidx = 0;
11533 : 16 : size_t index_offset = readp - (const unsigned char *) data->d_buf;
11534 : 16 : fprintf (out, " Offsets start at 0x%zx:\n", index_offset);
11535 [ + + ]: 204 : while (readp <= next_unitp - offset_size)
11536 : : {
11537 : 188 : Dwarf_Word offset;
11538 [ + - ]: 188 : if (offset_size == 4)
11539 [ - + ]: 188 : offset = read_4ubyte_unaligned_inc (dbg, readp);
11540 : : else
11541 [ # # ]: 0 : offset = read_8ubyte_unaligned_inc (dbg, readp);
11542 : 188 : const char *str = dwarf_getstring (dbg, offset, NULL);
11543 [ - + ]: 188 : fprintf (out, " [%*u] [%*" PRIx64 "] \"%s\"\n",
11544 : 188 : digits, uidx++, (int) offset_size * 2, offset, str ?: "???");
11545 : : }
11546 : 16 : fprintf (out, "\n");
11547 : :
11548 [ + - ]: 16 : if (readp != next_unitp)
11549 : 0 : error (0, 0, "extra %zd bytes at end of unit",
11550 : 0 : (size_t) (next_unitp - readp));
11551 : :
11552 : 16 : next_unit:
11553 : : readp = next_unitp;
11554 : : }
11555 : : }
11556 : :
11557 : :
11558 : : /* Print the content of the call frame search table section
11559 : : '.eh_frame_hdr'. */
11560 : : static void
11561 : 140 : print_debug_frame_hdr_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
11562 : : Ebl *ebl __attribute__ ((unused)),
11563 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
11564 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg,
11565 : : FILE *out)
11566 : : {
11567 : 140 : fprintf (out, _("\
11568 : : \nCall frame search table section [%2zu] '.eh_frame_hdr':\n"),
11569 : : elf_ndxscn (scn));
11570 : :
11571 : 140 : Elf_Data *data = elf_rawdata (scn, NULL);
11572 : :
11573 [ - + ]: 140 : if (unlikely (data == NULL))
11574 : : {
11575 : 0 : error (0, 0, _("cannot get %s content: %s"),
11576 : : ".eh_frame_hdr", elf_errmsg (-1));
11577 : 0 : return;
11578 : : }
11579 : :
11580 : 140 : const unsigned char *readp = data->d_buf;
11581 : 140 : const unsigned char *const dataend = ((unsigned char *) data->d_buf
11582 : 140 : + data->d_size);
11583 : :
11584 [ - + ]: 140 : if (unlikely (readp + 4 > dataend))
11585 : : {
11586 : 0 : invalid_data:
11587 : 0 : error (0, 0, _("invalid data"));
11588 : 0 : return;
11589 : : }
11590 : :
11591 : 140 : unsigned int version = *readp++;
11592 : 140 : unsigned int eh_frame_ptr_enc = *readp++;
11593 : 140 : unsigned int fde_count_enc = *readp++;
11594 : 140 : unsigned int table_enc = *readp++;
11595 : :
11596 : 140 : fprintf (out, " version: %u\n"
11597 : : " eh_frame_ptr_enc: %#x ",
11598 : : version, eh_frame_ptr_enc);
11599 : 140 : print_encoding_base ("", eh_frame_ptr_enc, out);
11600 : 140 : fprintf (out, " fde_count_enc: %#x ", fde_count_enc);
11601 : 140 : print_encoding_base ("", fde_count_enc, out);
11602 : 140 : fprintf (out, " table_enc: %#x ", table_enc);
11603 : 140 : print_encoding_base ("", table_enc, out);
11604 : :
11605 : 140 : uint64_t eh_frame_ptr = 0;
11606 [ + - ]: 140 : if (eh_frame_ptr_enc != DW_EH_PE_omit)
11607 : : {
11608 : 140 : readp = read_encoded (eh_frame_ptr_enc, readp, dataend, &eh_frame_ptr,
11609 : : dbg);
11610 [ - + ]: 140 : if (unlikely (readp == NULL))
11611 : 0 : goto invalid_data;
11612 : :
11613 : 140 : fprintf (out, " eh_frame_ptr: %#" PRIx64, eh_frame_ptr);
11614 [ + - ]: 140 : if ((eh_frame_ptr_enc & 0x70) == DW_EH_PE_pcrel)
11615 : 140 : fprintf (out, " (offset: %#" PRIx64 ")",
11616 : : /* +4 because of the 4 byte header of the section. */
11617 : 140 : (uint64_t) shdr->sh_offset + 4 + eh_frame_ptr);
11618 : :
11619 : 140 : fputc ('\n', out);
11620 : : }
11621 : :
11622 : 140 : uint64_t fde_count = 0;
11623 [ + - ]: 140 : if (fde_count_enc != DW_EH_PE_omit)
11624 : : {
11625 : 140 : readp = read_encoded (fde_count_enc, readp, dataend, &fde_count, dbg);
11626 [ - + ]: 140 : if (unlikely (readp == NULL))
11627 : 0 : goto invalid_data;
11628 : :
11629 : 140 : fprintf (out, " fde_count: %" PRIu64 "\n", fde_count);
11630 : : }
11631 : :
11632 [ + - + - ]: 140 : if (fde_count == 0 || table_enc == DW_EH_PE_omit)
11633 : : return;
11634 : :
11635 : 140 : fputs (" Table:\n", out);
11636 : :
11637 : : /* Optimize for the most common case. */
11638 [ + - ]: 140 : if (table_enc == (DW_EH_PE_datarel | DW_EH_PE_sdata4))
11639 [ + + ]: 24176 : while (fde_count > 0 && readp + 8 <= dataend)
11640 : : {
11641 [ - + ]: 24036 : int32_t initial_location = read_4sbyte_unaligned_inc (dbg, readp);
11642 : 24036 : uint64_t initial_offset = ((uint64_t) shdr->sh_offset
11643 : 24036 : + (int64_t) initial_location);
11644 [ - + ]: 24036 : int32_t address = read_4sbyte_unaligned_inc (dbg, readp);
11645 : : // XXX Possibly print symbol name or section offset for initial_offset
11646 : 24036 : fprintf (out, " %#" PRIx32 " (offset: %#6" PRIx64 ") -> %#" PRIx32
11647 : : " fde=[%6" PRIx64 "]\n",
11648 : : initial_location, initial_offset,
11649 : 24036 : address, address - (eh_frame_ptr + 4));
11650 : : }
11651 : : else
11652 : 140 : while (0 && readp < dataend)
11653 : : {
11654 : :
11655 : 140 : }
11656 : : }
11657 : :
11658 : :
11659 : : /* Print the content of the exception handling table section
11660 : : '.eh_frame_hdr'. */
11661 : : static void
11662 : 0 : print_debug_exception_table (Dwfl_Module *dwflmod __attribute__ ((unused)),
11663 : : Ebl *ebl __attribute__ ((unused)),
11664 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
11665 : : Elf_Scn *scn,
11666 : : GElf_Shdr *shdr __attribute__ ((unused)),
11667 : : Dwarf *dbg __attribute__ ((unused)),
11668 : : FILE *out)
11669 : : {
11670 : 0 : fprintf (out, _("\
11671 : : \nException handling table section [%2zu] '.gcc_except_table':\n"),
11672 : : elf_ndxscn (scn));
11673 : :
11674 : 0 : Elf_Data *data = elf_rawdata (scn, NULL);
11675 : :
11676 [ # # ]: 0 : if (unlikely (data == NULL))
11677 : : {
11678 : 0 : error (0, 0, _("cannot get %s content: %s"),
11679 : : ".gcc_except_table", elf_errmsg (-1));
11680 : 0 : return;
11681 : : }
11682 : :
11683 : 0 : const unsigned char *readp = data->d_buf;
11684 : 0 : const unsigned char *const dataend = readp + data->d_size;
11685 : :
11686 [ # # ]: 0 : if (unlikely (readp + 1 > dataend))
11687 : : {
11688 : 0 : invalid_data:
11689 : 0 : error (0, 0, _("invalid data"));
11690 : 0 : return;
11691 : : }
11692 : 0 : unsigned int lpstart_encoding = *readp++;
11693 : 0 : fprintf (out, _(" LPStart encoding: %#x "), lpstart_encoding);
11694 : 0 : print_encoding_base ("", lpstart_encoding, out);
11695 [ # # ]: 0 : if (lpstart_encoding != DW_EH_PE_omit)
11696 : : {
11697 : 0 : uint64_t lpstart;
11698 : 0 : readp = read_encoded (lpstart_encoding, readp, dataend, &lpstart, dbg);
11699 : 0 : fprintf (out, " LPStart: %#" PRIx64 "\n", lpstart);
11700 : : }
11701 : :
11702 [ # # ]: 0 : if (unlikely (readp + 1 > dataend))
11703 : 0 : goto invalid_data;
11704 : 0 : unsigned int ttype_encoding = *readp++;
11705 : 0 : fprintf (out, _(" TType encoding: %#x "), ttype_encoding);
11706 : 0 : print_encoding_base ("", ttype_encoding, out);
11707 : 0 : const unsigned char *ttype_base = NULL;
11708 [ # # ]: 0 : if (ttype_encoding != DW_EH_PE_omit)
11709 : : {
11710 : 0 : unsigned int ttype_base_offset;
11711 [ # # ]: 0 : if (readp >= dataend)
11712 : 0 : goto invalid_data;
11713 : 0 : get_uleb128 (ttype_base_offset, readp, dataend);
11714 : 0 : fprintf (out, " TType base offset: %#x\n", ttype_base_offset);
11715 [ # # ]: 0 : if ((size_t) (dataend - readp) > ttype_base_offset)
11716 : 0 : ttype_base = readp + ttype_base_offset;
11717 : : }
11718 : :
11719 [ # # ]: 0 : if (unlikely (readp + 1 > dataend))
11720 : 0 : goto invalid_data;
11721 : 0 : unsigned int call_site_encoding = *readp++;
11722 : 0 : fprintf (out, _(" Call site encoding: %#x "), call_site_encoding);
11723 : 0 : print_encoding_base ("", call_site_encoding, out);
11724 : 0 : unsigned int call_site_table_len;
11725 [ # # ]: 0 : if (readp >= dataend)
11726 : 0 : goto invalid_data;
11727 : 0 : get_uleb128 (call_site_table_len, readp, dataend);
11728 : :
11729 : 0 : const unsigned char *const action_table = readp + call_site_table_len;
11730 [ # # ]: 0 : if (unlikely (action_table > dataend))
11731 : 0 : goto invalid_data;
11732 : : unsigned int u = 0;
11733 : : unsigned int max_action = 0;
11734 [ # # ]: 0 : while (readp < action_table)
11735 : : {
11736 [ # # ]: 0 : if (u == 0)
11737 : 0 : fputs (_("\n Call site table:\n"), out);
11738 : :
11739 : 0 : uint64_t call_site_start;
11740 : 0 : readp = read_encoded (call_site_encoding, readp, dataend,
11741 : : &call_site_start, dbg);
11742 : 0 : uint64_t call_site_length;
11743 : 0 : readp = read_encoded (call_site_encoding, readp, dataend,
11744 : : &call_site_length, dbg);
11745 : 0 : uint64_t landing_pad;
11746 : 0 : readp = read_encoded (call_site_encoding, readp, dataend,
11747 : : &landing_pad, dbg);
11748 : 0 : unsigned int action;
11749 [ # # ]: 0 : if (readp >= dataend)
11750 : 0 : goto invalid_data;
11751 : 0 : get_uleb128 (action, readp, dataend);
11752 : 0 : max_action = MAX (action, max_action);
11753 : 0 : fprintf (out, _(" [%4u] Call site start: %#" PRIx64 "\n"
11754 : : " Call site length: %" PRIu64 "\n"
11755 : : " Landing pad: %#" PRIx64 "\n"
11756 : : " Action: %u\n"),
11757 : : u++, call_site_start, call_site_length, landing_pad, action);
11758 : : }
11759 [ # # ]: 0 : if (readp != action_table)
11760 : 0 : goto invalid_data;
11761 : :
11762 : 0 : unsigned int max_ar_filter = 0;
11763 [ # # ]: 0 : if (max_action > 0)
11764 : : {
11765 : 0 : fputs ("\n Action table:\n", out);
11766 : :
11767 : 0 : size_t maxdata = (size_t) (dataend - action_table);
11768 [ # # # # ]: 0 : if (max_action > maxdata || maxdata - max_action < 1)
11769 : : {
11770 : 0 : invalid_action_table:
11771 : 0 : fputs (_(" <INVALID DATA>\n"), stdout);
11772 : 0 : return;
11773 : : }
11774 : :
11775 : 0 : const unsigned char *const action_table_end
11776 : 0 : = action_table + max_action + 1;
11777 : :
11778 : 0 : u = 0;
11779 : 0 : do
11780 : : {
11781 : 0 : int ar_filter;
11782 : 0 : get_sleb128 (ar_filter, readp, action_table_end);
11783 [ # # ]: 0 : if (ar_filter > 0 && (unsigned int) ar_filter > max_ar_filter)
11784 : 0 : max_ar_filter = ar_filter;
11785 : 0 : int ar_disp;
11786 [ # # ]: 0 : if (readp >= action_table_end)
11787 : 0 : goto invalid_action_table;
11788 : 0 : get_sleb128 (ar_disp, readp, action_table_end);
11789 : :
11790 : 0 : fprintf (out, " [%4u] ar_filter: % d\n"
11791 : : " ar_disp: % -5d",
11792 : : u, ar_filter, ar_disp);
11793 [ # # ]: 0 : if (abs (ar_disp) & 1)
11794 : 0 : fprintf (out, " -> [%4u]\n", u + (ar_disp + 1) / 2);
11795 [ # # ]: 0 : else if (ar_disp != 0)
11796 : 0 : fputs (" -> ???\n", out);
11797 : : else
11798 : 0 : fputc ('\n', out);
11799 : 0 : ++u;
11800 : : }
11801 [ # # ]: 0 : while (readp < action_table_end);
11802 : : }
11803 : :
11804 [ # # ]: 0 : if (max_ar_filter > 0 && ttype_base != NULL)
11805 : : {
11806 : 0 : unsigned char dsize;
11807 : 0 : fputs ("\n TType table:\n", out);
11808 : :
11809 : : // XXX Not *4, size of encoding;
11810 [ # # ]: 0 : switch (ttype_encoding & 7)
11811 : : {
11812 : : case DW_EH_PE_udata2:
11813 : : case DW_EH_PE_sdata2:
11814 : : dsize = 2;
11815 : : break;
11816 : : case DW_EH_PE_udata4:
11817 : : case DW_EH_PE_sdata4:
11818 : : dsize = 4;
11819 : : break;
11820 : : case DW_EH_PE_udata8:
11821 : : case DW_EH_PE_sdata8:
11822 : : dsize = 8;
11823 : : break;
11824 : 0 : default:
11825 : 0 : dsize = 0;
11826 : 0 : error (1, 0, _("invalid TType encoding"));
11827 : : }
11828 : :
11829 : 0 : if (max_ar_filter
11830 [ # # ]: 0 : > (size_t) (ttype_base - (const unsigned char *) data->d_buf) / dsize)
11831 : 0 : goto invalid_data;
11832 : :
11833 : 0 : readp = ttype_base - max_ar_filter * dsize;
11834 : 0 : do
11835 : : {
11836 : 0 : uint64_t ttype;
11837 : 0 : readp = read_encoded (ttype_encoding, readp, ttype_base, &ttype,
11838 : : dbg);
11839 : 0 : fprintf (out, " [%4u] %#" PRIx64 "\n", max_ar_filter--, ttype);
11840 : : }
11841 [ # # ]: 0 : while (readp < ttype_base);
11842 : : }
11843 : : }
11844 : :
11845 : : /* Print the content of the '.gdb_index' section.
11846 : : http://sourceware.org/gdb/current/onlinedocs/gdb/Index-Section-Format.html
11847 : : */
11848 : : static void
11849 : 8 : print_gdb_index_section (Dwfl_Module *dwflmod, Ebl *ebl,
11850 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
11851 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg,
11852 : : FILE *out)
11853 : : {
11854 : 8 : fprintf (out, _("\nGDB section [%2zu] '%s' at offset %#" PRIx64
11855 : : " contains %" PRId64 " bytes :\n"),
11856 : : elf_ndxscn (scn), section_name (ebl, shdr),
11857 : 8 : (uint64_t) shdr->sh_offset, (uint64_t) shdr->sh_size);
11858 : :
11859 : 8 : Elf_Data *data = elf_rawdata (scn, NULL);
11860 : :
11861 [ - + ]: 8 : if (unlikely (data == NULL))
11862 : : {
11863 : 0 : error (0, 0, _("cannot get %s content: %s"),
11864 : : ".gdb_index", elf_errmsg (-1));
11865 : 4 : return;
11866 : : }
11867 : :
11868 : : // .gdb_index is always in little endian.
11869 : 8 : Dwarf dummy_dbg = { .other_byte_order = MY_ELFDATA != ELFDATA2LSB };
11870 : 8 : dbg = &dummy_dbg;
11871 : :
11872 : 8 : const unsigned char *readp = data->d_buf;
11873 : 8 : const unsigned char *const dataend = readp + data->d_size;
11874 : :
11875 [ - + ]: 8 : if (unlikely (readp + 4 > dataend))
11876 : : {
11877 : 0 : invalid_data:
11878 : 0 : error (0, 0, _("invalid data"));
11879 : 0 : return;
11880 : : }
11881 : :
11882 : 8 : int32_t vers = read_4ubyte_unaligned (dbg, readp);
11883 : 8 : fprintf (out, _(" Version: %" PRId32 "\n"), vers);
11884 : :
11885 : : // The only difference between version 4 and version 5 is the
11886 : : // hash used for generating the table. Version 6 contains symbols
11887 : : // for inlined functions, older versions didn't. Version 7 adds
11888 : : // symbol kinds. Version 8 just indicates that it correctly includes
11889 : : // TUs for symbols. Version 9 adds shortcut table for information
11890 : : // regarding the main function.
11891 [ - + ]: 8 : if (vers < 4 || vers > 9)
11892 : : {
11893 : 0 : fprintf (out, _(" unknown version, cannot parse section\n"));
11894 : 0 : return;
11895 : : }
11896 : :
11897 : 8 : readp += 4;
11898 [ - + ]: 8 : if (unlikely (readp + 4 > dataend))
11899 : 0 : goto invalid_data;
11900 : :
11901 : 8 : uint32_t cu_off = read_4ubyte_unaligned (dbg, readp);
11902 : 8 : fprintf (out, _(" CU offset: %#" PRIx32 "\n"), cu_off);
11903 : :
11904 : 8 : readp += 4;
11905 [ - + ]: 8 : if (unlikely (readp + 4 > dataend))
11906 : 0 : goto invalid_data;
11907 : :
11908 : 8 : uint32_t tu_off = read_4ubyte_unaligned (dbg, readp);
11909 : 8 : fprintf (out, _(" TU offset: %#" PRIx32 "\n"), tu_off);
11910 : :
11911 : 8 : readp += 4;
11912 [ - + ]: 8 : if (unlikely (readp + 4 > dataend))
11913 : 0 : goto invalid_data;
11914 : :
11915 : 8 : uint32_t addr_off = read_4ubyte_unaligned (dbg, readp);
11916 : 8 : fprintf (out, _(" address offset: %#" PRIx32 "\n"), addr_off);
11917 : :
11918 : 8 : readp += 4;
11919 [ - + ]: 8 : if (unlikely (readp + 4 > dataend))
11920 : 0 : goto invalid_data;
11921 : :
11922 : 8 : uint32_t sym_off = read_4ubyte_unaligned (dbg, readp);
11923 : 8 : fprintf (out, _(" symbol offset: %#" PRIx32 "\n"), sym_off);
11924 : :
11925 : 8 : readp += 4;
11926 [ - + ]: 8 : if (unlikely (readp + 4 > dataend))
11927 : 0 : goto invalid_data;
11928 : :
11929 : 8 : uint32_t shortcut_off = 0;
11930 [ + + ]: 8 : if (vers >= 9)
11931 : : {
11932 : 4 : shortcut_off = read_4ubyte_unaligned (dbg, readp);
11933 : 4 : fprintf (out, _(" shortcut offset: %#" PRIx32 "\n"), shortcut_off);
11934 : :
11935 : 4 : readp += 4;
11936 [ - + ]: 4 : if (unlikely (readp + 4 > dataend))
11937 : 0 : goto invalid_data;
11938 : : }
11939 : :
11940 : 8 : uint32_t const_off = read_4ubyte_unaligned (dbg, readp);
11941 : 8 : fprintf (out, _(" constant offset: %#" PRIx32 "\n"), const_off);
11942 : :
11943 [ - + ]: 8 : if (unlikely ((size_t) (dataend - (const unsigned char *) data->d_buf)
11944 : : < const_off))
11945 : 0 : goto invalid_data;
11946 : :
11947 : 8 : readp = data->d_buf + cu_off;
11948 : :
11949 : 8 : const unsigned char *nextp = data->d_buf + tu_off;
11950 [ - + ]: 8 : if (tu_off >= data->d_size)
11951 : 0 : goto invalid_data;
11952 : :
11953 : 8 : size_t cu_nr = (nextp - readp) / 16;
11954 : :
11955 : 8 : fprintf (out, _("\n CU list at offset %#" PRIx32
11956 : : " contains %zu entries:\n"),
11957 : : cu_off, cu_nr);
11958 : :
11959 : 8 : size_t n = 0;
11960 [ + - + + ]: 22 : while (dataend - readp >= 16 && n < cu_nr)
11961 : : {
11962 : 14 : uint64_t off = read_8ubyte_unaligned (dbg, readp);
11963 : 14 : readp += 8;
11964 : :
11965 : 14 : uint64_t len = read_8ubyte_unaligned (dbg, readp);
11966 : 14 : readp += 8;
11967 : :
11968 : 14 : fprintf (out, " [%4zu] start: %0#8" PRIx64
11969 : : ", length: %5" PRIu64 "\n", n, off, len);
11970 : 14 : n++;
11971 : : }
11972 : :
11973 : 8 : readp = data->d_buf + tu_off;
11974 : 8 : nextp = data->d_buf + addr_off;
11975 [ - + ]: 8 : if (addr_off >= data->d_size)
11976 : 0 : goto invalid_data;
11977 : :
11978 : 8 : size_t tu_nr = (nextp - readp) / 24;
11979 : :
11980 : 8 : fprintf (out, _("\n TU list at offset %#" PRIx32
11981 : : " contains %zu entries:\n"),
11982 : : tu_off, tu_nr);
11983 : :
11984 : 8 : n = 0;
11985 [ + - + + ]: 14 : while (dataend - readp >= 24 && n < tu_nr)
11986 : : {
11987 : 6 : uint64_t off = read_8ubyte_unaligned (dbg, readp);
11988 : 6 : readp += 8;
11989 : :
11990 : 6 : uint64_t type = read_8ubyte_unaligned (dbg, readp);
11991 : 6 : readp += 8;
11992 : :
11993 : 6 : uint64_t sig = read_8ubyte_unaligned (dbg, readp);
11994 : 6 : readp += 8;
11995 : :
11996 : 6 : fprintf (out, " [%4zu] CU offset: %5" PRId64
11997 : : ", type offset: %5" PRId64
11998 : : ", signature: %0#8" PRIx64 "\n", n, off, type, sig);
11999 : 6 : n++;
12000 : : }
12001 : :
12002 : 8 : readp = data->d_buf + addr_off;
12003 : 8 : nextp = data->d_buf + sym_off;
12004 [ - + ]: 8 : if (sym_off >= data->d_size)
12005 : 0 : goto invalid_data;
12006 : :
12007 : 8 : size_t addr_nr = (nextp - readp) / 20;
12008 : :
12009 : 8 : fprintf (out, _("\n Address list at offset %#" PRIx32
12010 : : " contains %zu entries:\n"),
12011 : : addr_off, addr_nr);
12012 : :
12013 : 8 : n = 0;
12014 [ + - + + ]: 22 : while (dataend - readp >= 20 && n < addr_nr)
12015 : : {
12016 : 14 : uint64_t low = read_8ubyte_unaligned (dbg, readp);
12017 : 14 : readp += 8;
12018 : :
12019 : 14 : uint64_t high = read_8ubyte_unaligned (dbg, readp);
12020 : 14 : readp += 8;
12021 : :
12022 : 14 : uint32_t idx = read_4ubyte_unaligned (dbg, readp);
12023 : 14 : readp += 4;
12024 : :
12025 : 14 : fprintf (out, " [%4zu] ", n);
12026 : 14 : print_dwarf_addr (dwflmod, 8, low, low, out);
12027 : 14 : fprintf (out, "..");
12028 : 14 : print_dwarf_addr (dwflmod, 8, high - 1, high, out);
12029 : 14 : fprintf (out, ", CU index: %5" PRId32 "\n", idx);
12030 : 14 : n++;
12031 : : }
12032 : :
12033 : 8 : const unsigned char *const_start = data->d_buf + const_off;
12034 [ - + ]: 8 : if (const_off > data->d_size)
12035 : 0 : goto invalid_data;
12036 : :
12037 : 8 : const unsigned char *shortcut_start = NULL;
12038 [ + + ]: 8 : if (vers >= 9)
12039 : : {
12040 [ - + ]: 4 : if (shortcut_off >= data->d_size)
12041 : 0 : goto invalid_data;
12042 : :
12043 : 4 : shortcut_start = data->d_buf + shortcut_off;
12044 : 4 : nextp = shortcut_start;
12045 : : }
12046 : : else
12047 : : nextp = const_start;
12048 : :
12049 : 8 : readp = data->d_buf + sym_off;
12050 : 8 : size_t sym_nr = (nextp - readp) / 8;
12051 : :
12052 : 8 : fprintf (out, _("\n Symbol table at offset %#" PRIx32
12053 : : " contains %zu slots:\n"),
12054 : : sym_off, sym_nr);
12055 : :
12056 : 8 : n = 0;
12057 [ + - + + ]: 8200 : while (dataend - readp >= 8 && n < sym_nr)
12058 : : {
12059 : 8192 : uint32_t name = read_4ubyte_unaligned (dbg, readp);
12060 : 8192 : readp += 4;
12061 : :
12062 : 8192 : uint32_t vector = read_4ubyte_unaligned (dbg, readp);
12063 : 8192 : readp += 4;
12064 : :
12065 [ + + ]: 8192 : if (name != 0 || vector != 0)
12066 : : {
12067 : 60 : const unsigned char *sym = const_start + name;
12068 [ + - - + ]: 60 : if (unlikely ((size_t) (dataend - const_start) < name
12069 : : || memchr (sym, '\0', dataend - sym) == NULL))
12070 : 0 : goto invalid_data;
12071 : :
12072 : 60 : fprintf (out, " [%4zu] symbol: %s, CUs: ", n, sym);
12073 : :
12074 : 60 : const unsigned char *readcus = const_start + vector;
12075 [ - + ]: 60 : if (unlikely ((size_t) (dataend - const_start) < vector))
12076 : 0 : goto invalid_data;
12077 : 60 : uint32_t cus = read_4ubyte_unaligned (dbg, readcus);
12078 [ + + ]: 128 : while (cus--)
12079 : : {
12080 : 68 : uint32_t cu_kind, cu, kind;
12081 : 68 : bool is_static;
12082 : 68 : readcus += 4;
12083 [ - + ]: 68 : if (unlikely (readcus + 4 > dataend))
12084 : 0 : goto invalid_data;
12085 : 68 : cu_kind = read_4ubyte_unaligned (dbg, readcus);
12086 : 68 : cu = cu_kind & ((1 << 24) - 1);
12087 : 68 : kind = (cu_kind >> 28) & 7;
12088 : 68 : is_static = cu_kind & (1U << 31);
12089 [ + + ]: 68 : if (cu > cu_nr - 1)
12090 : 10 : fprintf (out, "%" PRId32 "T", cu - (uint32_t) cu_nr);
12091 : : else
12092 : 58 : fprintf (out, "%" PRId32, cu);
12093 [ + + ]: 68 : if (kind != 0)
12094 : : {
12095 : 52 : fprintf (out, " (");
12096 [ + + + - : 52 : switch (kind)
- ]
12097 : : {
12098 : : case 1:
12099 : 20 : fprintf (out, "type");
12100 : 20 : break;
12101 : : case 2:
12102 : 16 : fprintf (out, "var");
12103 : 16 : break;
12104 : : case 3:
12105 : 16 : fprintf (out, "func");
12106 : 16 : break;
12107 : : case 4:
12108 : 0 : fprintf (out, "other");
12109 : 0 : break;
12110 : : default:
12111 : 0 : fprintf (out, "unknown-0x%" PRIx32, kind);
12112 : 0 : break;
12113 : : }
12114 [ + + ]: 68 : fprintf (out, ":%c)", (is_static ? 'S' : 'G'));
12115 : : }
12116 [ + + ]: 68 : if (cus > 0)
12117 : 8 : fprintf (out, ", ");
12118 : : }
12119 : 60 : fprintf (out, "\n");
12120 : : }
12121 : 8192 : n++;
12122 : : }
12123 : :
12124 [ + + ]: 8 : if (vers < 9)
12125 : : return;
12126 : :
12127 [ - + ]: 4 : if (unlikely (shortcut_start == NULL))
12128 : 0 : goto invalid_data;
12129 : :
12130 : 4 : readp = shortcut_start;
12131 : 4 : nextp = const_start;
12132 : 4 : size_t shortcut_nr = (nextp - readp) / 4;
12133 : :
12134 [ - + ]: 4 : if (unlikely (shortcut_nr != 2))
12135 : 0 : goto invalid_data;
12136 : :
12137 : 8 : fprintf (out,
12138 : 4 : _("\nShortcut table at offset %#" PRIx32 " contains %zu slots:\n"),
12139 : : shortcut_off, shortcut_nr);
12140 : :
12141 : 4 : uint32_t lang = read_4ubyte_unaligned (dbg, readp);
12142 : 4 : readp += 4;
12143 : :
12144 : : /* Include the hex number of LANG in the output if the language
12145 : : is unknown. */
12146 : 4 : const char *lang_str = dwarf_lang_string (lang);
12147 : 4 : lang_str = string_or_unknown (lang_str, lang, DW_LANG_lo_user,
12148 : : DW_LANG_hi_user, true);
12149 : :
12150 : 4 : fprintf (out, _("Language of main: %s\n"), lang_str);
12151 : 4 : fprintf (out, _("Name of main: "));
12152 : :
12153 [ + + ]: 4 : if (lang != 0)
12154 : : {
12155 : 2 : uint32_t name = read_4ubyte_unaligned (dbg, readp);
12156 : 2 : readp += 4;
12157 : 2 : const unsigned char *sym = const_start + name;
12158 : :
12159 [ + - - + ]: 2 : if (unlikely ((size_t) (dataend - const_start) < name
12160 : : || memchr (sym, '\0', dataend - sym) == NULL))
12161 : 0 : goto invalid_data;
12162 : :
12163 : 2 : fprintf (out, "%s\n", sym);
12164 : : }
12165 : : else
12166 : 2 : fprintf (out, "<unknown>\n");
12167 : : }
12168 : :
12169 : : /* Returns true and sets split DWARF CU id if there is a split compile
12170 : : unit in the given Dwarf, and no non-split units are found (before it). */
12171 : : static bool
12172 : 396 : is_split_dwarf (Dwarf *dbg, uint64_t *id, Dwarf_CU **split_cu)
12173 : : {
12174 : 396 : Dwarf_CU *cu = NULL;
12175 [ + + ]: 396 : while (dwarf_get_units (dbg, cu, &cu, NULL, NULL, NULL, NULL) == 0)
12176 : : {
12177 : 394 : uint8_t unit_type;
12178 [ + - ]: 394 : if (dwarf_cu_info (cu, NULL, &unit_type, NULL, NULL,
12179 : : id, NULL, NULL) != 0)
12180 : 394 : return false;
12181 : :
12182 [ + + ]: 394 : if (unit_type != DW_UT_split_compile && unit_type != DW_UT_split_type)
12183 : : return false;
12184 : :
12185 : : /* We really only care about the split compile unit, the types
12186 : : should be fine and self sufficient. Also they don't have an
12187 : : id that we can match with a skeleton unit. */
12188 [ + - ]: 18 : if (unit_type == DW_UT_split_compile)
12189 : : {
12190 : 18 : *split_cu = cu;
12191 : 18 : return true;
12192 : : }
12193 : : }
12194 : :
12195 : : return false;
12196 : : }
12197 : :
12198 : : /* Check that there is one and only one Dwfl_Module, return in arg. */
12199 : : static int
12200 : 18 : getone_dwflmod (Dwfl_Module *dwflmod,
12201 : : void **userdata __attribute__ ((unused)),
12202 : : const char *name __attribute__ ((unused)),
12203 : : Dwarf_Addr base __attribute__ ((unused)),
12204 : : void *arg)
12205 : : {
12206 : 18 : Dwfl_Module **m = (Dwfl_Module **) arg;
12207 [ + - ]: 18 : if (*m != NULL)
12208 : : return DWARF_CB_ABORT;
12209 : 18 : *m = dwflmod;
12210 : 18 : return DWARF_CB_OK;
12211 : : }
12212 : :
12213 : : typedef struct Job_Data {
12214 : : struct Job_Data *next;
12215 : : Dwfl_Module *dwflmod;
12216 : : Ebl *ebl;
12217 : : GElf_Ehdr *ehdr;
12218 : : Elf_Scn scn;
12219 : : GElf_Shdr shdr;
12220 : : Dwarf *dbg;
12221 : : FILE *out;
12222 : : void (*fp) (Dwfl_Module *, Ebl *, GElf_Ehdr *,
12223 : : Elf_Scn *, GElf_Shdr *, Dwarf *, FILE *);
12224 : : } job_data;
12225 : :
12226 : : #ifdef USE_LOCKS
12227 : :
12228 : : /* Thread entry point. */
12229 : : static void *
12230 : : do_job (void *data, FILE *out)
12231 : : {
12232 : : job_data *d = (job_data *) data;
12233 : : d->fp (d->dwflmod, d->ebl, d->ehdr, &d->scn, &d->shdr, d->dbg, out);
12234 : : return NULL;
12235 : : }
12236 : : #endif
12237 : :
12238 : : /* If readelf is built with thread safety, then set up JDATA at index IDX
12239 : : and add it to the job queue.
12240 : :
12241 : : If thread safety is not supported or the maximum number of threads is set
12242 : : to 1, then immediately call START_ROUTINE with the given arguments. */
12243 : : static void
12244 : 1094 : schedule_job (job_data **jdatalist,
12245 : : void (*start_routine) (Dwfl_Module *, Ebl *, GElf_Ehdr *,
12246 : : Elf_Scn *, GElf_Shdr *, Dwarf *, FILE *),
12247 : : Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr, Elf_Scn *scn,
12248 : : GElf_Shdr *shdr, Dwarf *dbg)
12249 : : {
12250 : : #ifdef USE_LOCKS
12251 : : if (max_threads > 1)
12252 : : {
12253 : : job_data *jdata = xmalloc (sizeof (job_data));
12254 : :
12255 : : jdata->dwflmod = dwflmod;
12256 : : jdata->ebl = ebl;
12257 : : jdata->ehdr = ehdr;
12258 : : jdata->scn = *scn;
12259 : : jdata->shdr = *shdr;
12260 : : jdata->dbg = dbg;
12261 : : jdata->fp = start_routine;
12262 : : jdata->next = *jdatalist;
12263 : : *jdatalist = jdata;
12264 : :
12265 : : add_job (do_job, (void *) jdata);
12266 : : }
12267 : : else
12268 : : start_routine (dwflmod, ebl, ehdr, scn, shdr, dbg, stdout);
12269 : : #else
12270 : 1094 : (void) jdatalist;
12271 : :
12272 : 1094 : start_routine (dwflmod, ebl, ehdr, scn, shdr, dbg, stdout);
12273 : : #endif
12274 : 1094 : }
12275 : :
12276 : : static void
12277 : 502 : print_debug (Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr)
12278 : : {
12279 : : /* Used for skeleton file, if necessary for split DWARF. */
12280 : 502 : Dwfl *skel_dwfl = NULL;
12281 : 502 : Dwfl_Module *skel_mod = NULL;
12282 : 502 : char *skel_name = NULL;
12283 : 502 : Dwarf *split_dbg = NULL;
12284 : 502 : Dwarf_CU *split_cu = NULL;
12285 : :
12286 : : /* Before we start the real work get a debug context descriptor. */
12287 : 502 : Dwarf_Addr dwbias;
12288 : 502 : Dwarf *dbg = dwfl_module_getdwarf (dwflmod, &dwbias);
12289 : 502 : Dwarf dummy_dbg =
12290 : : {
12291 : 502 : .elf = ebl->elf,
12292 : 502 : .other_byte_order = MY_ELFDATA != ehdr->e_ident[EI_DATA]
12293 : 502 : };
12294 [ + + ]: 502 : if (dbg == NULL)
12295 : : {
12296 [ - + ]: 106 : if ((print_debug_sections & ~(section_exception|section_frame)) != 0)
12297 : 0 : error (0, 0, _("cannot get debug context descriptor: %s"),
12298 : : dwfl_errmsg (-1));
12299 : : dbg = &dummy_dbg;
12300 : : }
12301 : : else
12302 : : {
12303 : : /* If we are asked about a split dwarf (.dwo) file, use the user
12304 : : provided, or find the corresponding skeleton file. If we got
12305 : : a skeleton file, replace the given dwflmod and dbg, with one
12306 : : derived from the skeleton file to provide enough context. */
12307 : 396 : uint64_t split_id;
12308 [ + + ]: 396 : if (is_split_dwarf (dbg, &split_id, &split_cu))
12309 : : {
12310 [ + - ]: 18 : if (dwarf_skeleton != NULL)
12311 : 18 : skel_name = strdup (dwarf_skeleton);
12312 : : else
12313 : : {
12314 : : /* Replace file.dwo with file.o and see if that matches. */
12315 : 0 : const char *fname;
12316 : 0 : dwfl_module_info (dwflmod, NULL, NULL, NULL, NULL, NULL,
12317 : : &fname, NULL);
12318 [ # # ]: 0 : if (fname != NULL)
12319 : : {
12320 : 0 : size_t flen = strlen (fname);
12321 [ # # # # ]: 0 : if (flen > 4 && strcmp (".dwo", fname + flen - 4) == 0)
12322 : : {
12323 : 0 : skel_name = strdup (fname);
12324 [ # # ]: 0 : if (skel_name != NULL)
12325 : : {
12326 : 0 : skel_name[flen - 3] = 'o';
12327 : 0 : skel_name[flen - 2] = '\0';
12328 : : }
12329 : : }
12330 : : }
12331 : : }
12332 : :
12333 [ - + ]: 18 : if (skel_name != NULL)
12334 : : {
12335 : 18 : int skel_fd = open (skel_name, O_RDONLY);
12336 [ - + ]: 18 : if (skel_fd == -1)
12337 : 0 : fprintf (stderr, "Warning: Couldn't open DWARF skeleton file"
12338 : : " '%s'\n", skel_name);
12339 : : else
12340 : : {
12341 : 18 : skel_dwfl = create_dwfl (skel_fd, skel_name);
12342 : :
12343 : : /* skel_fd was dup'ed by create_dwfl. We can close the
12344 : : original now. */
12345 : 18 : close (skel_fd);
12346 : : }
12347 : :
12348 [ + - ]: 18 : if (skel_dwfl != NULL)
12349 : : {
12350 [ - + ]: 18 : if (dwfl_getmodules (skel_dwfl, &getone_dwflmod,
12351 : : &skel_mod, 0) != 0)
12352 : : {
12353 : 0 : fprintf (stderr, "Warning: Bad DWARF skeleton,"
12354 : : " multiple modules '%s'\n", skel_name);
12355 : 0 : dwfl_end (skel_dwfl);
12356 : 0 : skel_mod = NULL;
12357 : : }
12358 : : }
12359 [ # # ]: 0 : else if (skel_fd != -1)
12360 : 0 : fprintf (stderr, "Warning: Couldn't create skeleton dwfl for"
12361 : : " '%s': %s\n", skel_name, dwfl_errmsg (-1));
12362 : :
12363 [ + - ]: 18 : if (skel_mod != NULL)
12364 : : {
12365 : 18 : Dwarf *skel_dbg = dwfl_module_getdwarf (skel_mod, &dwbias);
12366 [ + - ]: 18 : if (skel_dbg != NULL)
12367 : : {
12368 : : /* First check the skeleton CU DIE, only fetch
12369 : : the split DIE if we know the id matches to
12370 : : not unnecessary search for any split DIEs we
12371 : : don't need. */
12372 : 18 : Dwarf_CU *cu = NULL;
12373 : 18 : while (dwarf_get_units (skel_dbg, cu, &cu,
12374 [ + - ]: 26 : NULL, NULL, NULL, NULL) == 0)
12375 : : {
12376 : 26 : uint8_t unit_type;
12377 : 26 : uint64_t skel_id;
12378 [ + - ]: 26 : if (dwarf_cu_info (cu, NULL, &unit_type, NULL, NULL,
12379 : : &skel_id, NULL, NULL) == 0
12380 [ + - ]: 26 : && unit_type == DW_UT_skeleton
12381 [ + + ]: 26 : && split_id == skel_id)
12382 : : {
12383 : 18 : Dwarf_Die subdie;
12384 [ + - ]: 18 : if (dwarf_cu_info (cu, NULL, NULL, NULL,
12385 : : &subdie,
12386 : : NULL, NULL, NULL) == 0
12387 [ + - ]: 18 : && dwarf_tag (&subdie) != DW_TAG_invalid)
12388 : : {
12389 : 18 : split_dbg = dwarf_cu_getdwarf (subdie.cu);
12390 [ - + ]: 18 : if (split_dbg == NULL)
12391 : 0 : fprintf (stderr,
12392 : : "Warning: Couldn't get split_dbg:"
12393 : : " %s\n", dwarf_errmsg (-1));
12394 : 0 : break;
12395 : : }
12396 : : else
12397 : : {
12398 : : /* Everything matches up, but not
12399 : : according to libdw. Which means
12400 : : the user knew better. So...
12401 : : Terrible hack... We can never
12402 : : destroy the underlying dwfl
12403 : : because it would free the wrong
12404 : : Dwarfs... So we leak memory...*/
12405 [ # # ]: 0 : if (cu->split == NULL
12406 [ # # ]: 0 : && dwarf_skeleton != NULL)
12407 : : {
12408 : 0 : do_not_close_dwfl = true;
12409 : 0 : __libdw_link_skel_split (cu, split_cu);
12410 : 0 : split_dbg = dwarf_cu_getdwarf (split_cu);
12411 : 0 : break;
12412 : : }
12413 : : else
12414 : 0 : fprintf (stderr, "Warning: Couldn't get"
12415 : : " skeleton subdie: %s\n",
12416 : : dwarf_errmsg (-1));
12417 : : }
12418 : : }
12419 : : }
12420 [ - - ]: 18 : if (split_dbg == NULL)
12421 : 0 : fprintf (stderr, "Warning: '%s' didn't contain a skeleton for split id %" PRIx64 "\n", skel_name, split_id);
12422 : : }
12423 : : else
12424 : 0 : fprintf (stderr, "Warning: Couldn't get skeleton DWARF:"
12425 : : " %s\n", dwfl_errmsg (-1));
12426 : : }
12427 : : }
12428 : :
12429 : 0 : if (split_dbg != NULL)
12430 : : {
12431 : 18 : dbg = split_dbg;
12432 : 18 : dwflmod = skel_mod;
12433 : : }
12434 [ # # ]: 0 : else if (skel_name == NULL)
12435 : 0 : fprintf (stderr,
12436 : : "Warning: split DWARF file, but no skeleton found.\n");
12437 : : }
12438 [ + - ]: 378 : else if (dwarf_skeleton != NULL)
12439 : 0 : fprintf (stderr, "Warning: DWARF skeleton given,"
12440 : : " but not a split DWARF file\n");
12441 : : }
12442 : :
12443 : : /* Get the section header string table index. */
12444 : 502 : size_t shstrndx;
12445 [ - + ]: 502 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
12446 : 0 : error_exit (0, _("cannot get section header string table index"));
12447 : :
12448 : 502 : job_data *jdatalist = NULL;
12449 : :
12450 : : /* If the .debug_info section is listed as implicitly required then
12451 : : we must make sure to handle it before handling any other debug
12452 : : section. Various other sections depend on the CU DIEs being
12453 : : scanned (silently) first. */
12454 : 502 : bool implicit_info = (implicit_debug_sections & section_info) != 0;
12455 : 502 : bool explicit_info = (print_debug_sections & section_info) != 0;
12456 [ + + ]: 502 : if (implicit_info)
12457 : : {
12458 : : Elf_Scn *scn = NULL;
12459 [ + - ]: 3132 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
12460 : : {
12461 : 3132 : GElf_Shdr shdr_mem;
12462 : 3132 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
12463 : :
12464 [ + - + + ]: 3132 : if (shdr != NULL && shdr->sh_type == SHT_PROGBITS)
12465 : : {
12466 : 2448 : const char *name = elf_strptr (ebl->elf, shstrndx,
12467 : 1224 : shdr->sh_name);
12468 [ - + ]: 1224 : if (name == NULL)
12469 : 0 : continue;
12470 : :
12471 [ + + ]: 1224 : if (strcmp (name, ".debug_info") == 0
12472 [ + + ]: 1092 : || strcmp (name, ".debug_info.dwo") == 0
12473 [ + + ]: 1074 : || strcmp (name, ".zdebug_info") == 0
12474 [ + - ]: 1062 : || strcmp (name, ".zdebug_info.dwo") == 0
12475 [ + + ]: 1062 : || strcmp (name, ".gnu.debuglto_.debug_info") == 0)
12476 : : {
12477 : 164 : print_debug_info_section (dwflmod, ebl, ehdr,
12478 : : scn, shdr, dbg, stdout);
12479 : 164 : break;
12480 : : }
12481 : : }
12482 : : }
12483 : :
12484 : 164 : print_debug_sections &= ~section_info;
12485 : 164 : implicit_debug_sections &= ~section_info;
12486 : : }
12487 : :
12488 : : /* Look through all the sections for the debugging sections to print. */
12489 : : Elf_Scn *scn = NULL;
12490 [ + + ]: 16590 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
12491 : : {
12492 : 16088 : GElf_Shdr shdr_mem;
12493 : 16088 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
12494 : :
12495 [ + - + + ]: 16088 : if (shdr != NULL && shdr->sh_type == SHT_PROGBITS)
12496 : : {
12497 : 6758 : static const struct
12498 : : {
12499 : : const char *name;
12500 : : enum section_e bitmask;
12501 : : void (*fp) (Dwfl_Module *, Ebl *,
12502 : : GElf_Ehdr *, Elf_Scn *, GElf_Shdr *, Dwarf *, FILE *);
12503 : : } debug_sections[] =
12504 : : {
12505 : : #define NEW_SECTION(name) \
12506 : : { ".debug_" #name, section_##name, print_debug_##name##_section }
12507 : : NEW_SECTION (abbrev),
12508 : : NEW_SECTION (addr),
12509 : : NEW_SECTION (aranges),
12510 : : NEW_SECTION (frame),
12511 : : NEW_SECTION (info),
12512 : : NEW_SECTION (types),
12513 : : NEW_SECTION (line),
12514 : : NEW_SECTION (loc),
12515 : : /* loclists is loc for DWARF5. */
12516 : : { ".debug_loclists", section_loc,
12517 : : print_debug_loclists_section },
12518 : : NEW_SECTION (pubnames),
12519 : : NEW_SECTION (str),
12520 : : /* A DWARF5 specialised debug string section. */
12521 : : { ".debug_line_str", section_str,
12522 : : print_debug_str_section },
12523 : : /* DWARF5 string offsets table. */
12524 : : { ".debug_str_offsets", section_str,
12525 : : print_debug_str_offsets_section },
12526 : : NEW_SECTION (macinfo),
12527 : : NEW_SECTION (macro),
12528 : : NEW_SECTION (ranges),
12529 : : /* rnglists is ranges for DWARF5. */
12530 : : { ".debug_rnglists", section_ranges,
12531 : : print_debug_rnglists_section },
12532 : : { ".eh_frame", section_frame | section_exception,
12533 : : print_debug_frame_section },
12534 : : { ".eh_frame_hdr", section_frame | section_exception,
12535 : : print_debug_frame_hdr_section },
12536 : : { ".gcc_except_table", section_frame | section_exception,
12537 : : print_debug_exception_table },
12538 : : { ".gdb_index", section_gdb_index, print_gdb_index_section }
12539 : : };
12540 : 6758 : const int ndebug_sections = (sizeof (debug_sections)
12541 : : / sizeof (debug_sections[0]));
12542 : 13516 : const char *name = elf_strptr (ebl->elf, shstrndx,
12543 : 6758 : shdr->sh_name);
12544 [ - + ]: 6758 : if (name == NULL)
12545 : 0 : continue;
12546 : :
12547 : : int n;
12548 [ + + ]: 106506 : for (n = 0; n < ndebug_sections; ++n)
12549 : : {
12550 : 103042 : size_t dbglen = strlen (debug_sections[n].name);
12551 : 103042 : size_t scnlen = strlen (name);
12552 [ + + ]: 103042 : if ((strncmp (name, debug_sections[n].name, dbglen) == 0
12553 [ + + ]: 3558 : && (dbglen == scnlen
12554 [ + + ]: 546 : || (scnlen == dbglen + 4
12555 [ + + ]: 466 : && strstr (name, ".dwo") == name + dbglen)))
12556 [ + + + + ]: 99922 : || (name[0] == '.' && name[1] == 'z'
12557 [ + - ]: 1132 : && debug_sections[n].name[1] == 'd'
12558 [ + + ]: 1132 : && strncmp (&name[2], &debug_sections[n].name[1],
12559 : : dbglen - 1) == 0
12560 [ - + ]: 164 : && (scnlen == dbglen + 1
12561 [ # # ]: 0 : || (scnlen == dbglen + 5
12562 [ # # ]: 0 : && strstr (name, ".dwo") == name + dbglen + 1)))
12563 [ + + ]: 99758 : || (scnlen > 14 /* .gnu.debuglto_ prefix. */
12564 [ + + ]: 10170 : && startswith (name, ".gnu.debuglto_")
12565 [ + + ]: 72 : && strcmp (&name[14], debug_sections[n].name) == 0)
12566 : : )
12567 : : {
12568 : 3294 : if (((print_debug_sections | implicit_debug_sections)
12569 [ + + ]: 3294 : & debug_sections[n].bitmask))
12570 : 1094 : schedule_job (&jdatalist, debug_sections[n].fp,
12571 : : dwflmod, ebl, ehdr, scn, shdr, dbg);
12572 : :
12573 : : break;
12574 : : }
12575 : : }
12576 : : }
12577 : : }
12578 : :
12579 : : #ifdef USE_LOCKS
12580 : : /* If max_threads <= 1, then jobs were immediately run in schedule_job. */
12581 : : if (max_threads > 1)
12582 : : run_jobs (max_threads);
12583 : : #endif
12584 : :
12585 : 502 : dwfl_end (skel_dwfl);
12586 : 502 : free (skel_name);
12587 : :
12588 : 502 : while (jdatalist != NULL)
12589 : : {
12590 : : job_data *jdata = jdatalist;
12591 : : jdatalist = jdatalist->next;
12592 : : free (jdata);
12593 : : }
12594 : :
12595 : : /* Turn implicit and/or explicit back on in case we go over another file. */
12596 [ + + ]: 502 : if (implicit_info)
12597 : 164 : implicit_debug_sections |= section_info;
12598 [ + + ]: 502 : if (explicit_info)
12599 : 134 : print_debug_sections |= section_info;
12600 : :
12601 : 502 : reset_listptr (&known_locsptr);
12602 : 502 : reset_listptr (&known_loclistsptr);
12603 : 502 : reset_listptr (&known_rangelistptr);
12604 : 502 : reset_listptr (&known_rnglistptr);
12605 : 502 : reset_listptr (&known_addrbases);
12606 : 502 : reset_listptr (&known_stroffbases);
12607 : 502 : }
12608 : :
12609 : :
12610 : : #define ITEM_INDENT 4
12611 : : #define WRAP_COLUMN 75
12612 : :
12613 : : /* Print "NAME: FORMAT", wrapping when output text would make the line
12614 : : exceed WRAP_COLUMN. Unpadded numbers look better for the core items
12615 : : but this function is also used for registers which should be printed
12616 : : aligned. Fortunately registers output uses fixed fields width (such
12617 : : as %11d) for the alignment.
12618 : :
12619 : : Line breaks should not depend on the particular values although that
12620 : : may happen in some cases of the core items. */
12621 : :
12622 : : static unsigned int
12623 : : __attribute__ ((format (printf, 6, 7)))
12624 : 1642 : print_core_item (unsigned int colno, char sep, unsigned int wrap,
12625 : : size_t name_width, const char *name, const char *format, ...)
12626 : : {
12627 : 1642 : size_t len = strlen (name);
12628 : 1642 : if (name_width < len)
12629 : : name_width = len;
12630 : :
12631 : 1642 : char *out;
12632 : 1642 : va_list ap;
12633 : 1642 : va_start (ap, format);
12634 : 1642 : int out_len = vasprintf (&out, format, ap);
12635 : 1642 : va_end (ap);
12636 [ - + ]: 1642 : if (out_len == -1)
12637 : 0 : error_exit (0, _("memory exhausted"));
12638 : :
12639 : 1642 : size_t n = name_width + sizeof ": " - 1 + out_len;
12640 : :
12641 [ + + ]: 1642 : if (colno == 0)
12642 : : {
12643 : 102 : printf ("%*s", ITEM_INDENT, "");
12644 : 102 : colno = ITEM_INDENT + n;
12645 : : }
12646 [ + + ]: 1540 : else if (colno + 2 + n < wrap)
12647 : : {
12648 : 936 : printf ("%c ", sep);
12649 : 936 : colno += 2 + n;
12650 : : }
12651 : : else
12652 : : {
12653 : 604 : printf ("\n%*s", ITEM_INDENT, "");
12654 : 604 : colno = ITEM_INDENT + n;
12655 : : }
12656 : :
12657 : 1642 : printf ("%s: %*s%s", name, (int) (name_width - len), "", out);
12658 : :
12659 : 1642 : free (out);
12660 : :
12661 : 1642 : return colno;
12662 : : }
12663 : :
12664 : : static const void *
12665 : 1862 : convert (Elf *core, Elf_Type type, uint_fast16_t count,
12666 : : void *value, const void *data, size_t size)
12667 : : {
12668 : 3724 : Elf_Data valuedata =
12669 : : {
12670 : : .d_type = type,
12671 : : .d_buf = value,
12672 [ + + ]: 1862 : .d_size = size ?: gelf_fsize (core, type, count, EV_CURRENT),
12673 : : .d_version = EV_CURRENT,
12674 : : };
12675 : 1862 : Elf_Data indata =
12676 : : {
12677 : : .d_type = type,
12678 : : .d_buf = (void *) data,
12679 : : .d_size = valuedata.d_size,
12680 : : .d_version = EV_CURRENT,
12681 : : };
12682 : :
12683 : 1862 : Elf_Data *d = (gelf_getclass (core) == ELFCLASS32
12684 [ + + ]: 1862 : ? elf32_xlatetom : elf64_xlatetom)
12685 : 1862 : (&valuedata, &indata, elf_getident (core, NULL)[EI_DATA]);
12686 [ - + ]: 1862 : if (d == NULL)
12687 : 0 : error_exit (0, _("cannot convert core note data: %s"),
12688 : : elf_errmsg (-1));
12689 : :
12690 : 1862 : return data + indata.d_size;
12691 : : }
12692 : :
12693 : : typedef uint8_t GElf_Byte;
12694 : :
12695 : : static unsigned int
12696 : 800 : handle_core_item (Elf *core, const GElf_Ehdr *ehdr,
12697 : : const Ebl_Core_Item *item, const void *desc,
12698 : : unsigned int colno, size_t *repeated_size)
12699 : : {
12700 [ + + ]: 800 : uint_fast16_t count = item->count ?: 1;
12701 : : /* Ebl_Core_Item count is always a small number.
12702 : : Make sure the backend didn't put in some large bogus value. */
12703 [ - + ]: 124 : assert (count < 128);
12704 : :
12705 : : #define TYPES \
12706 : : DO_TYPE (BYTE, Byte, "0x%.2" PRIx8, "%" PRId8); \
12707 : : DO_TYPE (HALF, Half, "0x%.4" PRIx16, "%" PRId16); \
12708 : : DO_TYPE (WORD, Word, "0x%.8" PRIx32, "%" PRId32); \
12709 : : DO_TYPE (SWORD, Sword, "%" PRId32, "%" PRId32); \
12710 : : DO_TYPE (XWORD, Xword, "0x%.16" PRIx64, "%" PRId64); \
12711 : : DO_TYPE (SXWORD, Sxword, "%" PRId64, "%" PRId64)
12712 : :
12713 : : #define DO_TYPE(NAME, Name, hex, dec) GElf_##Name Name
12714 : 800 : typedef union { TYPES; } value_t;
12715 : 800 : void *data = alloca (count * sizeof (value_t));
12716 : : #undef DO_TYPE
12717 : :
12718 : : #define DO_TYPE(NAME, Name, hex, dec) \
12719 : : GElf_##Name *value_##Name __attribute__((unused)) = data
12720 : 800 : TYPES;
12721 : : #undef DO_TYPE
12722 : :
12723 : 800 : size_t size = gelf_fsize (core, item->type, count, EV_CURRENT);
12724 : 800 : size_t convsize = size;
12725 [ + + ]: 800 : if (repeated_size != NULL)
12726 : : {
12727 [ + - - + ]: 2 : if (*repeated_size > size && (item->format == 'b' || item->format == 'B'))
12728 : : {
12729 : 0 : data = alloca (*repeated_size);
12730 : 0 : count *= *repeated_size / size;
12731 : 0 : convsize = count * size;
12732 : 0 : *repeated_size -= convsize;
12733 : : }
12734 [ + - + - ]: 2 : else if (item->count != 0 || item->format != '\n')
12735 : 0 : *repeated_size -= size;
12736 : : }
12737 : :
12738 : 800 : convert (core, item->type, count, data, desc + item->offset, convsize);
12739 : :
12740 : 800 : Elf_Type type = item->type;
12741 [ + + ]: 800 : if (type == ELF_T_ADDR)
12742 [ + - ]: 2 : type = gelf_getclass (core) == ELFCLASS32 ? ELF_T_WORD : ELF_T_XWORD;
12743 : :
12744 [ + + + + : 800 : switch (item->format)
+ + + +
- ]
12745 : : {
12746 : 386 : case 'd':
12747 [ - + ]: 386 : assert (count == 1);
12748 [ + + + + : 386 : switch (type)
- + - ]
12749 : : {
12750 : : #define DO_TYPE(NAME, Name, hex, dec) \
12751 : : case ELF_T_##NAME: \
12752 : : colno = print_core_item (colno, ',', WRAP_COLUMN, \
12753 : : 0, item->name, dec, value_##Name[0]); \
12754 : : break
12755 : 386 : TYPES;
12756 : : #undef DO_TYPE
12757 : 0 : default:
12758 : 0 : abort ();
12759 : : }
12760 : : break;
12761 : :
12762 : 222 : case 'x':
12763 [ - + ]: 222 : assert (count == 1);
12764 [ - - + - : 222 : switch (type)
+ - - ]
12765 : : {
12766 : : #define DO_TYPE(NAME, Name, hex, dec) \
12767 : : case ELF_T_##NAME: \
12768 : : colno = print_core_item (colno, ',', WRAP_COLUMN, \
12769 : : 0, item->name, hex, value_##Name[0]); \
12770 : : break
12771 : 222 : TYPES;
12772 : : #undef DO_TYPE
12773 : 0 : default:
12774 : 0 : abort ();
12775 : : }
12776 : : break;
12777 : :
12778 : 44 : case 'b':
12779 : : case 'B':
12780 [ - + ]: 44 : assert (size % sizeof (unsigned int) == 0);
12781 : 44 : unsigned int nbits = count * size * 8;
12782 : 44 : unsigned int pop = 0;
12783 [ + + ]: 112 : for (const unsigned int *i = data; (void *) i < data + count * size; ++i)
12784 : 68 : pop += __builtin_popcount (*i);
12785 : 44 : bool negate = pop > nbits / 2;
12786 : 44 : const unsigned int bias = item->format == 'b';
12787 : :
12788 : 44 : {
12789 [ - + ]: 44 : char printed[(negate ? nbits - pop : pop) * 16 + 1];
12790 : 44 : char *p = printed;
12791 : 44 : *p = '\0';
12792 : :
12793 : 44 : if (BYTE_ORDER != LITTLE_ENDIAN && size > sizeof (unsigned int))
12794 : : {
12795 : : assert (size == sizeof (unsigned int) * 2);
12796 : : for (unsigned int *i = data;
12797 : : (void *) i < data + count * size; i += 2)
12798 : : {
12799 : : unsigned int w = i[1];
12800 : : i[1] = i[0];
12801 : : i[0] = w;
12802 : : }
12803 : : }
12804 : :
12805 : 44 : unsigned int lastbit = 0;
12806 : 44 : unsigned int run = 0;
12807 : 44 : for (const unsigned int *i = data;
12808 [ + + ]: 112 : (void *) i < data + count * size; ++i)
12809 : : {
12810 : 68 : unsigned int bit = ((void *) i - data) * 8;
12811 [ - + ]: 68 : unsigned int w = negate ? ~*i : *i;
12812 [ - + ]: 68 : while (w != 0)
12813 : : {
12814 : : /* Note that a right shift equal to (or greater than)
12815 : : the number of bits of w is undefined behaviour. In
12816 : : particular when the least significant bit is bit 32
12817 : : (w = 0x8000000) then w >>= n is undefined. So
12818 : : explicitly handle that case separately. */
12819 : 0 : unsigned int n = ffs (w);
12820 [ # # ]: 0 : if (n < sizeof (w) * 8)
12821 : 0 : w >>= n;
12822 : : else
12823 : : w = 0;
12824 : 0 : bit += n;
12825 : :
12826 [ # # # # ]: 0 : if (lastbit != 0 && lastbit + 1 == bit)
12827 : 0 : ++run;
12828 : : else
12829 : : {
12830 : 0 : if (lastbit == 0)
12831 : 0 : p += sprintf (p, "%u", bit - bias);
12832 [ # # ]: 0 : else if (run == 0)
12833 : 0 : p += sprintf (p, ",%u", bit - bias);
12834 : : else
12835 : 0 : p += sprintf (p, "-%u,%u", lastbit - bias, bit - bias);
12836 : : run = 0;
12837 : : }
12838 : :
12839 : : lastbit = bit;
12840 : : }
12841 : : }
12842 [ - + - - ]: 44 : if (lastbit > 0 && run > 0 && lastbit + 1 != nbits)
12843 : 0 : p += sprintf (p, "-%u", lastbit - bias);
12844 : :
12845 [ + - ]: 88 : colno = print_core_item (colno, ',', WRAP_COLUMN, 0, item->name,
12846 : : negate ? "~<%s>" : "<%s>", printed);
12847 : : }
12848 : 44 : break;
12849 : :
12850 : 88 : case 'T':
12851 : : case (char) ('T'|0x80):
12852 [ - + ]: 88 : assert (count == 2);
12853 : 88 : Dwarf_Word sec;
12854 : 88 : Dwarf_Word usec;
12855 [ - - + - : 88 : switch (type)
+ - - ]
12856 : : {
12857 : : #define DO_TYPE(NAME, Name, hex, dec) \
12858 : : case ELF_T_##NAME: \
12859 : : sec = value_##Name[0]; \
12860 : : usec = value_##Name[1]; \
12861 : : break
12862 : 88 : TYPES;
12863 : : #undef DO_TYPE
12864 : 0 : default:
12865 : 0 : abort ();
12866 : : }
12867 [ - + ]: 88 : if (unlikely (item->format == (char) ('T'|0x80)))
12868 : : {
12869 : : /* This is a hack for an ill-considered 64-bit ABI where
12870 : : tv_usec is actually a 32-bit field with 32 bits of padding
12871 : : rounding out struct timeval. We've already converted it as
12872 : : a 64-bit field. For little-endian, this just means the
12873 : : high half is the padding; it's presumably zero, but should
12874 : : be ignored anyway. For big-endian, it means the 32-bit
12875 : : field went into the high half of USEC. */
12876 [ # # ]: 0 : if (likely (ehdr->e_ident[EI_DATA] == ELFDATA2MSB))
12877 : 0 : usec >>= 32;
12878 : : else
12879 : 0 : usec &= UINT32_MAX;
12880 : : }
12881 : 88 : colno = print_core_item (colno, ',', WRAP_COLUMN, 0, item->name,
12882 : : "%" PRIu64 ".%.6" PRIu64, sec, usec);
12883 : 88 : break;
12884 : :
12885 : 18 : case 'c':
12886 [ - + ]: 18 : assert (count == 1);
12887 : 18 : colno = print_core_item (colno, ',', WRAP_COLUMN, 0, item->name,
12888 : 18 : "%c", value_Byte[0]);
12889 : 18 : break;
12890 : :
12891 : 36 : case 's':
12892 : 36 : colno = print_core_item (colno, ',', WRAP_COLUMN, 0, item->name,
12893 : : "%.*s", (int) count, value_Byte);
12894 : 36 : break;
12895 : :
12896 : 2 : case '\n':
12897 : : /* This is a list of strings separated by '\n'. */
12898 [ - + ]: 2 : assert (item->count == 0);
12899 [ - + ]: 2 : assert (repeated_size != NULL);
12900 [ - + ]: 2 : assert (item->name == NULL);
12901 [ - + ]: 2 : if (unlikely (item->offset >= *repeated_size))
12902 : : break;
12903 : :
12904 : 2 : const char *s = desc + item->offset;
12905 : 2 : size = *repeated_size - item->offset;
12906 : 2 : *repeated_size = 0;
12907 [ + - ]: 96 : while (size > 0)
12908 : : {
12909 : 96 : const char *eol = memchr (s, '\n', size);
12910 : 96 : int len = size;
12911 [ + + ]: 96 : if (eol != NULL)
12912 : 94 : len = eol - s;
12913 : 96 : printf ("%*s%.*s\n", ITEM_INDENT, "", len, s);
12914 [ + + ]: 96 : if (eol == NULL)
12915 : : break;
12916 : 94 : size -= eol + 1 - s;
12917 : 94 : s = eol + 1;
12918 : : }
12919 : :
12920 : : colno = WRAP_COLUMN;
12921 : : break;
12922 : :
12923 : : case 'h':
12924 : : break;
12925 : :
12926 : 0 : default:
12927 : 0 : error (0, 0, "XXX not handling format '%c' for %s",
12928 : 0 : item->format, item->name);
12929 : 0 : break;
12930 : : }
12931 : :
12932 : : #undef TYPES
12933 : :
12934 : 800 : return colno;
12935 : : }
12936 : :
12937 : :
12938 : : /* Sort items by group, and by layout offset within each group. */
12939 : : static int
12940 : 1790 : compare_core_items (const void *a, const void *b)
12941 : : {
12942 : 1790 : const Ebl_Core_Item *const *p1 = a;
12943 : 1790 : const Ebl_Core_Item *const *p2 = b;
12944 : 1790 : const Ebl_Core_Item *item1 = *p1;
12945 : 1790 : const Ebl_Core_Item *item2 = *p2;
12946 : :
12947 : 1790 : return ((item1->group == item2->group ? 0
12948 [ + + ]: 1790 : : strcmp (item1->group, item2->group))
12949 [ - + ]: 1790 : ?: (int) item1->offset - (int) item2->offset);
12950 : : }
12951 : :
12952 : : /* Sort item groups by layout offset of the first item in the group. */
12953 : : static int
12954 : 278 : compare_core_item_groups (const void *a, const void *b)
12955 : : {
12956 : 278 : const Ebl_Core_Item *const *const *p1 = a;
12957 : 278 : const Ebl_Core_Item *const *const *p2 = b;
12958 : 278 : const Ebl_Core_Item *const *group1 = *p1;
12959 : 278 : const Ebl_Core_Item *const *group2 = *p2;
12960 : 278 : const Ebl_Core_Item *item1 = *group1;
12961 : 278 : const Ebl_Core_Item *item2 = *group2;
12962 : :
12963 : 278 : return (int) item1->offset - (int) item2->offset;
12964 : : }
12965 : :
12966 : : static unsigned int
12967 : 74 : handle_core_items (Elf *core, const GElf_Ehdr *ehdr,
12968 : : const void *desc, size_t descsz,
12969 : : const Ebl_Core_Item *items, size_t nitems)
12970 : 74 : {
12971 [ + + ]: 74 : if (nitems == 0)
12972 : : return 0;
12973 : 68 : unsigned int colno = 0;
12974 : :
12975 : : /* FORMAT '\n' makes sense to be present only as a single item as it
12976 : : processes all the data of a note. FORMATs 'b' and 'B' have a special case
12977 : : if present as a single item but they can be also processed with other
12978 : : items below. */
12979 [ + + + + ]: 68 : if (nitems == 1 && (items[0].format == '\n' || items[0].format == 'b'
12980 [ + - ]: 16 : || items[0].format == 'B'))
12981 : : {
12982 [ - + ]: 2 : assert (items[0].offset == 0);
12983 : 2 : size_t size = descsz;
12984 : 2 : colno = handle_core_item (core, ehdr, items, desc, colno, &size);
12985 : : /* If SIZE is not zero here there is some remaining data. But we do not
12986 : : know how to process it anyway. */
12987 : 2 : return colno;
12988 : : }
12989 [ + + ]: 848 : for (size_t i = 0; i < nitems; ++i)
12990 [ - + ]: 782 : assert (items[i].format != '\n');
12991 : :
12992 : : /* Sort to collect the groups together. */
12993 : 66 : const Ebl_Core_Item *sorted_items[nitems];
12994 [ + + ]: 848 : for (size_t i = 0; i < nitems; ++i)
12995 : 782 : sorted_items[i] = &items[i];
12996 : 66 : qsort (sorted_items, nitems, sizeof sorted_items[0], &compare_core_items);
12997 : :
12998 : : /* Collect the unique groups and sort them. */
12999 : 66 : const Ebl_Core_Item **groups[nitems];
13000 : 66 : groups[0] = &sorted_items[0];
13001 : 66 : size_t ngroups = 1;
13002 [ + + ]: 782 : for (size_t i = 1; i < nitems; ++i)
13003 [ + + ]: 716 : if (sorted_items[i]->group != sorted_items[i - 1]->group
13004 [ + - ]: 150 : && strcmp (sorted_items[i]->group, sorted_items[i - 1]->group))
13005 : 150 : groups[ngroups++] = &sorted_items[i];
13006 : 66 : qsort (groups, ngroups, sizeof groups[0], &compare_core_item_groups);
13007 : :
13008 : : /* Write out all the groups. */
13009 : 66 : const void *last = desc;
13010 : 70 : do
13011 : : {
13012 [ + + ]: 290 : for (size_t i = 0; i < ngroups; ++i)
13013 : : {
13014 : 220 : for (const Ebl_Core_Item **item = groups[i];
13015 : 1018 : (item < &sorted_items[nitems]
13016 [ + + + + ]: 1018 : && ((*item)->group == groups[i][0]->group
13017 [ - + ]: 150 : || !strcmp ((*item)->group, groups[i][0]->group)));
13018 : 798 : ++item)
13019 : 798 : colno = handle_core_item (core, ehdr, *item, desc, colno, NULL);
13020 : :
13021 : : /* Force a line break at the end of the group. */
13022 : 220 : colno = WRAP_COLUMN;
13023 : : }
13024 : :
13025 [ + + ]: 70 : if (descsz == 0)
13026 : : break;
13027 : :
13028 : : /* This set of items consumed a certain amount of the note's data.
13029 : : If there is more data there, we have another unit of the same size.
13030 : : Loop to print that out too. */
13031 : 40 : const Ebl_Core_Item *item = &items[nitems - 1];
13032 : 80 : size_t eltsz = item->offset + gelf_fsize (core, item->type,
13033 : 40 : item->count ?: 1, EV_CURRENT);
13034 : :
13035 : 40 : int reps = -1;
13036 : 40 : do
13037 : : {
13038 : 40 : ++reps;
13039 : 40 : desc += eltsz;
13040 : 40 : descsz -= eltsz;
13041 : : }
13042 [ + + - + ]: 40 : while (descsz >= eltsz && !memcmp (desc, last, eltsz));
13043 : :
13044 [ + - ]: 40 : if (reps == 1)
13045 : : {
13046 : : /* For just one repeat, print it unabridged twice. */
13047 : : desc -= eltsz;
13048 : : descsz += eltsz;
13049 : : }
13050 [ + - ]: 40 : else if (reps > 1)
13051 : 0 : printf (_("\n%*s... <repeats %u more times> ..."),
13052 : : ITEM_INDENT, "", reps);
13053 : :
13054 : 40 : last = desc;
13055 : : }
13056 [ + + ]: 40 : while (descsz > 0);
13057 : :
13058 : : return colno;
13059 : : }
13060 : :
13061 : : static unsigned int
13062 : 324 : handle_core_register (Ebl *ebl, Elf *core, int maxregname,
13063 : : const Ebl_Register_Location *regloc, const void *desc,
13064 : : unsigned int colno)
13065 : : {
13066 [ - + ]: 324 : if (regloc->bits % 8 != 0)
13067 : : {
13068 : 0 : error (0, 0, "Warning: Cannot handle register with %" PRIu8 "bits\n",
13069 : : regloc->bits);
13070 : 0 : return colno;
13071 : : }
13072 : :
13073 : 324 : desc += regloc->offset;
13074 : :
13075 [ + + ]: 1172 : for (int reg = regloc->regno; reg < regloc->regno + regloc->count; ++reg)
13076 : : {
13077 : 848 : char name[REGNAMESZ];
13078 : 848 : int bits;
13079 : 848 : int type;
13080 : 848 : register_info (ebl, reg, regloc, name, &bits, &type);
13081 : :
13082 : : #define TYPES \
13083 : : BITS (8, BYTE, "%4" PRId8, "0x%.2" PRIx8); \
13084 : : BITS (16, HALF, "%6" PRId16, "0x%.4" PRIx16); \
13085 : : BITS (32, WORD, "%11" PRId32, " 0x%.8" PRIx32); \
13086 : : BITS (64, XWORD, "%20" PRId64, " 0x%.16" PRIx64)
13087 : :
13088 : : #define BITS(bits, xtype, sfmt, ufmt) \
13089 : : uint##bits##_t b##bits; int##bits##_t b##bits##s
13090 : 848 : union { TYPES; uint64_t b128[2]; } value;
13091 : : #undef BITS
13092 : :
13093 [ + + ]: 848 : switch (type)
13094 : : {
13095 : 672 : case DW_ATE_unsigned:
13096 : : case DW_ATE_signed:
13097 : : case DW_ATE_address:
13098 [ - + + + : 672 : switch (bits)
+ - ]
13099 : : {
13100 : : #define BITS(bits, xtype, sfmt, ufmt) \
13101 : : case bits: \
13102 : : desc = convert (core, ELF_T_##xtype, 1, &value, desc, 0); \
13103 : : if (type == DW_ATE_signed) \
13104 : : colno = print_core_item (colno, ' ', WRAP_COLUMN, \
13105 : : maxregname, name, \
13106 : : sfmt, value.b##bits##s); \
13107 : : else \
13108 : : colno = print_core_item (colno, ' ', WRAP_COLUMN, \
13109 : : maxregname, name, \
13110 : : ufmt, value.b##bits); \
13111 : : break
13112 : :
13113 [ - - - + : 576 : TYPES;
+ + + + ]
13114 : :
13115 : 96 : case 128:
13116 [ - + ]: 96 : assert (type == DW_ATE_unsigned);
13117 : 96 : desc = convert (core, ELF_T_XWORD, 2, &value, desc, 0);
13118 : 96 : int be = elf_getident (core, NULL)[EI_DATA] == ELFDATA2MSB;
13119 : 96 : colno = print_core_item (colno, ' ', WRAP_COLUMN,
13120 : : maxregname, name,
13121 : : "0x%.16" PRIx64 "%.16" PRIx64,
13122 : 96 : value.b128[!be], value.b128[be]);
13123 : 96 : break;
13124 : :
13125 : 0 : default:
13126 : 0 : abort ();
13127 : : #undef BITS
13128 : : }
13129 : : break;
13130 : :
13131 : 176 : default:
13132 : : /* Print each byte in hex, the whole thing in native byte order. */
13133 [ - + ]: 176 : assert (bits % 8 == 0);
13134 : 176 : const uint8_t *bytes = desc;
13135 : 176 : desc += bits / 8;
13136 : 176 : char hex[bits / 4 + 1];
13137 : 176 : hex[bits / 4] = '\0';
13138 : 176 : int incr = 1;
13139 [ + + ]: 176 : if (elf_getident (core, NULL)[EI_DATA] == ELFDATA2LSB)
13140 : : {
13141 : 96 : bytes += bits / 8 - 1;
13142 : 96 : incr = -1;
13143 : : }
13144 : 176 : size_t idx = 0;
13145 [ + + ]: 1744 : for (char *h = hex; bits > 0; bits -= 8, idx += incr)
13146 : : {
13147 : 1568 : *h++ = "0123456789abcdef"[bytes[idx] >> 4];
13148 : 1568 : *h++ = "0123456789abcdef"[bytes[idx] & 0xf];
13149 : : }
13150 : 176 : colno = print_core_item (colno, ' ', WRAP_COLUMN,
13151 : : maxregname, name, "0x%s", hex);
13152 : 176 : break;
13153 : : }
13154 : 848 : desc += regloc->pad;
13155 : :
13156 : : #undef TYPES
13157 : : }
13158 : :
13159 : : return colno;
13160 : : }
13161 : :
13162 : :
13163 : : struct register_info
13164 : : {
13165 : : const Ebl_Register_Location *regloc;
13166 : : const char *set;
13167 : : char name[REGNAMESZ];
13168 : : int regno;
13169 : : int bits;
13170 : : int type;
13171 : : };
13172 : :
13173 : : static int
13174 : 4132 : register_bitpos (const struct register_info *r)
13175 : : {
13176 : 4132 : return (r->regloc->offset * 8
13177 : 4132 : + ((r->regno - r->regloc->regno)
13178 : 4132 : * (r->regloc->bits + r->regloc->pad * 8)));
13179 : : }
13180 : :
13181 : : static int
13182 : 2124 : compare_sets_by_info (const struct register_info *r1,
13183 : : const struct register_info *r2)
13184 : : {
13185 : 2124 : return ((int) r2->bits - (int) r1->bits
13186 [ + + ]: 2124 : ?: register_bitpos (r1) - register_bitpos (r2));
13187 : : }
13188 : :
13189 : : /* Sort registers by set, and by size and layout offset within each set. */
13190 : : static int
13191 : 9190 : compare_registers (const void *a, const void *b)
13192 : : {
13193 : 9190 : const struct register_info *r1 = a;
13194 : 9190 : const struct register_info *r2 = b;
13195 : :
13196 : : /* Unused elements sort last. */
13197 [ + + ]: 9190 : if (r1->regloc == NULL)
13198 : 6618 : return r2->regloc == NULL ? 0 : 1;
13199 [ + + ]: 2572 : if (r2->regloc == NULL)
13200 : : return -1;
13201 : :
13202 [ + + ]: 2242 : return ((r1->set == r2->set ? 0 : strcmp (r1->set, r2->set))
13203 [ - + ]: 2242 : ?: compare_sets_by_info (r1, r2));
13204 : : }
13205 : :
13206 : : /* Sort register sets by layout offset of the first register in the set. */
13207 : : static int
13208 : 40 : compare_register_sets (const void *a, const void *b)
13209 : : {
13210 : 40 : const struct register_info *const *p1 = a;
13211 : 40 : const struct register_info *const *p2 = b;
13212 : 40 : return compare_sets_by_info (*p1, *p2);
13213 : : }
13214 : :
13215 : : static inline bool
13216 : 1728 : same_set (const struct register_info *a,
13217 : : const struct register_info *b,
13218 : : const struct register_info *regs,
13219 : : size_t maxnreg)
13220 : : {
13221 [ + - ]: 1728 : return (a < ®s[maxnreg] && a->regloc != NULL
13222 [ + - + + ]: 1728 : && b < ®s[maxnreg] && b->regloc != NULL
13223 [ + + ]: 1692 : && a->bits == b->bits
13224 [ + - + + : 3384 : && (a->set == b->set || !strcmp (a->set, b->set)));
- + ]
13225 : : }
13226 : :
13227 : : static unsigned int
13228 : 74 : handle_core_registers (Ebl *ebl, Elf *core, const void *desc,
13229 : : const Ebl_Register_Location *reglocs, size_t nregloc)
13230 : 74 : {
13231 [ + + ]: 74 : if (nregloc == 0)
13232 : : return 0;
13233 : :
13234 : 36 : ssize_t maxnreg = ebl_register_info (ebl, 0, NULL, 0, NULL, NULL, NULL, NULL);
13235 [ - + ]: 36 : if (maxnreg <= 0)
13236 : : {
13237 [ # # ]: 0 : for (size_t i = 0; i < nregloc; ++i)
13238 : 0 : if (maxnreg < reglocs[i].regno + reglocs[i].count)
13239 : : maxnreg = reglocs[i].regno + reglocs[i].count;
13240 [ # # ]: 0 : assert (maxnreg > 0);
13241 : : }
13242 : :
13243 : 36 : struct register_info regs[maxnreg];
13244 : 36 : memset (regs, 0, sizeof regs);
13245 : :
13246 : : /* Sort to collect the sets together. */
13247 : 36 : int maxreg = 0;
13248 [ + + ]: 360 : for (size_t i = 0; i < nregloc; ++i)
13249 : 324 : for (int reg = reglocs[i].regno;
13250 [ + + ]: 1172 : reg < reglocs[i].regno + reglocs[i].count;
13251 : 848 : ++reg)
13252 : : {
13253 [ - + ]: 848 : assert (reg < maxnreg);
13254 : 848 : if (reg > maxreg)
13255 : : maxreg = reg;
13256 : 848 : struct register_info *info = ®s[reg];
13257 : 848 : info->regloc = ®locs[i];
13258 : 848 : info->regno = reg;
13259 : 848 : info->set = register_info (ebl, reg, ®locs[i],
13260 : 848 : info->name, &info->bits, &info->type);
13261 : : }
13262 : 36 : qsort (regs, maxreg + 1, sizeof regs[0], &compare_registers);
13263 : :
13264 : : /* Collect the unique sets and sort them. */
13265 : 36 : struct register_info *sets[maxreg + 1];
13266 : 36 : sets[0] = ®s[0];
13267 : 36 : size_t nsets = 1;
13268 [ + + ]: 2558 : for (int i = 1; i <= maxreg; ++i)
13269 [ + + ]: 2522 : if (regs[i].regloc != NULL
13270 [ + + ]: 812 : && !same_set (®s[i], ®s[i - 1], regs, maxnreg))
13271 : 32 : sets[nsets++] = ®s[i];
13272 : 36 : qsort (sets, nsets, sizeof sets[0], &compare_register_sets);
13273 : :
13274 : : /* Write out all the sets. */
13275 : 36 : unsigned int colno = 0;
13276 [ + + ]: 104 : for (size_t i = 0; i < nsets; ++i)
13277 : : {
13278 : : /* Find the longest name of a register in this set. */
13279 : 68 : size_t maxname = 0;
13280 : 68 : const struct register_info *end;
13281 [ + + ]: 916 : for (end = sets[i]; same_set (sets[i], end, regs, maxnreg); ++end)
13282 : : {
13283 : 848 : size_t len = strlen (end->name);
13284 : 848 : if (len > maxname)
13285 : : maxname = len;
13286 : : }
13287 : :
13288 : : for (const struct register_info *reg = sets[i];
13289 [ + + ]: 392 : reg < end;
13290 : 324 : reg += reg->regloc->count ?: 1)
13291 [ + - ]: 324 : colno = handle_core_register (ebl, core, maxname,
13292 : 324 : reg->regloc, desc, colno);
13293 : :
13294 : : /* Force a line break at the end of the group. */
13295 : 68 : colno = WRAP_COLUMN;
13296 : : }
13297 : :
13298 : : return colno;
13299 : : }
13300 : :
13301 : : static void
13302 : 18 : handle_auxv_note (Ebl *ebl, Elf *core, GElf_Word descsz, GElf_Off desc_pos)
13303 : : {
13304 : 18 : Elf_Data *data = elf_getdata_rawchunk (core, desc_pos, descsz, ELF_T_AUXV);
13305 [ - + ]: 18 : if (data == NULL)
13306 : 0 : elf_error:
13307 : 0 : error_exit (0, _("cannot convert core note data: %s"), elf_errmsg (-1));
13308 : :
13309 : 18 : const size_t nauxv = descsz / gelf_fsize (core, ELF_T_AUXV, 1, EV_CURRENT);
13310 [ + + ]: 354 : for (size_t i = 0; i < nauxv; ++i)
13311 : : {
13312 : 336 : GElf_auxv_t av_mem;
13313 : 336 : GElf_auxv_t *av = gelf_getauxv (data, i, &av_mem);
13314 [ - + ]: 336 : if (av == NULL)
13315 : 0 : goto elf_error;
13316 : :
13317 : 336 : const char *name;
13318 : 336 : const char *fmt;
13319 [ - + ]: 336 : if (ebl_auxv_info (ebl, av->a_type, &name, &fmt) == 0)
13320 : : {
13321 : : /* Unknown type. */
13322 [ # # ]: 0 : if (av->a_un.a_val == 0)
13323 : 0 : printf (" %" PRIu64 "\n", av->a_type);
13324 : : else
13325 : 0 : printf (" %" PRIu64 ": %#" PRIx64 "\n",
13326 : : av->a_type, av->a_un.a_val);
13327 : : }
13328 : : else
13329 [ + + + - : 336 : switch (fmt[0])
+ - ]
13330 : : {
13331 : 18 : case '\0': /* Normally zero. */
13332 [ + - ]: 18 : if (av->a_un.a_val == 0)
13333 : : {
13334 : 18 : printf (" %s\n", name);
13335 : 18 : break;
13336 : : }
13337 : 148 : FALLTHROUGH;
13338 : : case 'x': /* hex */
13339 : : case 'p': /* address */
13340 : : case 's': /* address of string */
13341 : 148 : printf (" %s: %#" PRIx64 "\n", name, av->a_un.a_val);
13342 : 148 : break;
13343 : 162 : case 'u':
13344 : 162 : printf (" %s: %" PRIu64 "\n", name, av->a_un.a_val);
13345 : 162 : break;
13346 : 0 : case 'd':
13347 : 0 : printf (" %s: %" PRId64 "\n", name, av->a_un.a_val);
13348 : 0 : break;
13349 : :
13350 : 8 : case 'b':
13351 : 8 : printf (" %s: %#" PRIx64 " ", name, av->a_un.a_val);
13352 : 8 : GElf_Xword bit = 1;
13353 : 8 : const char *pfx = "<";
13354 [ + + ]: 220 : for (const char *p = fmt + 1; *p != 0; p = strchr (p, '\0') + 1)
13355 : : {
13356 [ + + ]: 212 : if (av->a_un.a_val & bit)
13357 : : {
13358 : 154 : printf ("%s%s", pfx, p);
13359 : 154 : pfx = " ";
13360 : : }
13361 : 212 : bit <<= 1;
13362 : : }
13363 : 8 : printf (">\n");
13364 : 8 : break;
13365 : :
13366 : 0 : default:
13367 : 0 : abort ();
13368 : : }
13369 : : }
13370 : 18 : }
13371 : :
13372 : : static bool
13373 : 390 : buf_has_data (unsigned char const *ptr, unsigned char const *end, size_t sz)
13374 : : {
13375 [ + - + - ]: 390 : return ptr < end && (size_t) (end - ptr) >= sz;
13376 : : }
13377 : :
13378 : : static bool
13379 : 36 : buf_read_int (Elf *core, unsigned char const **ptrp, unsigned char const *end,
13380 : : int *retp)
13381 : : {
13382 [ + - ]: 36 : if (! buf_has_data (*ptrp, end, 4))
13383 : : return false;
13384 : :
13385 : 36 : *ptrp = convert (core, ELF_T_WORD, 1, retp, *ptrp, 4);
13386 : 36 : return true;
13387 : : }
13388 : :
13389 : : static bool
13390 : 354 : buf_read_ulong (Elf *core, unsigned char const **ptrp, unsigned char const *end,
13391 : : uint64_t *retp)
13392 : : {
13393 : 354 : size_t sz = gelf_fsize (core, ELF_T_ADDR, 1, EV_CURRENT);
13394 [ + - ]: 354 : if (! buf_has_data (*ptrp, end, sz))
13395 : : return false;
13396 : :
13397 : 354 : union
13398 : : {
13399 : : uint64_t u64;
13400 : : uint32_t u32;
13401 : : } u;
13402 : :
13403 : 354 : *ptrp = convert (core, ELF_T_ADDR, 1, &u, *ptrp, sz);
13404 : :
13405 [ + + ]: 354 : if (sz == 4)
13406 : 186 : *retp = u.u32;
13407 : : else
13408 : 168 : *retp = u.u64;
13409 : : return true;
13410 : : }
13411 : :
13412 : : static void
13413 : 12 : handle_siginfo_note (Elf *core, GElf_Word descsz, GElf_Off desc_pos)
13414 : : {
13415 : 12 : Elf_Data *data = elf_getdata_rawchunk (core, desc_pos, descsz, ELF_T_BYTE);
13416 [ - + ]: 12 : if (data == NULL)
13417 : 0 : error_exit (0, _("cannot convert core note data: %s"), elf_errmsg (-1));
13418 : :
13419 : 12 : unsigned char const *ptr = data->d_buf;
13420 : 12 : unsigned char const *const end = data->d_buf + data->d_size;
13421 : :
13422 : : /* Siginfo head is three ints: signal number, error number, origin
13423 : : code. */
13424 : 12 : int si_signo, si_errno, si_code;
13425 [ - + ]: 12 : if (! buf_read_int (core, &ptr, end, &si_signo)
13426 [ - + ]: 12 : || ! buf_read_int (core, &ptr, end, &si_errno)
13427 [ - + ]: 12 : || ! buf_read_int (core, &ptr, end, &si_code))
13428 : : {
13429 : 0 : fail:
13430 : 0 : printf (" Not enough data in NT_SIGINFO note.\n");
13431 : 0 : return;
13432 : : }
13433 : :
13434 : : /* Next is a pointer-aligned union of structures. On 64-bit
13435 : : machines, that implies a word of padding. */
13436 [ + + ]: 12 : if (gelf_getclass (core) == ELFCLASS64)
13437 : 6 : ptr += 4;
13438 : :
13439 : 12 : printf (" si_signo: %d, si_errno: %d, si_code: %d\n",
13440 : : si_signo, si_errno, si_code);
13441 : :
13442 [ + - ]: 12 : if (si_code > 0)
13443 [ + - ]: 12 : switch (si_signo)
13444 : : {
13445 : 12 : case CORE_SIGILL:
13446 : : case CORE_SIGFPE:
13447 : : case CORE_SIGSEGV:
13448 : : case CORE_SIGBUS:
13449 : : {
13450 : 12 : uint64_t addr;
13451 [ - + ]: 12 : if (! buf_read_ulong (core, &ptr, end, &addr))
13452 : 0 : goto fail;
13453 : 12 : printf (" fault address: %#" PRIx64 "\n", addr);
13454 : 12 : break;
13455 : : }
13456 : : default:
13457 : : ;
13458 : : }
13459 [ # # ]: 0 : else if (si_code == CORE_SI_USER)
13460 : : {
13461 : 0 : int pid, uid;
13462 [ # # ]: 0 : if (! buf_read_int (core, &ptr, end, &pid)
13463 [ # # ]: 0 : || ! buf_read_int (core, &ptr, end, &uid))
13464 : 0 : goto fail;
13465 : 0 : printf (" sender PID: %d, sender UID: %d\n", pid, uid);
13466 : : }
13467 : : }
13468 : :
13469 : : static void
13470 : 12 : handle_file_note (Elf *core, GElf_Word descsz, GElf_Off desc_pos)
13471 : : {
13472 : 12 : Elf_Data *data = elf_getdata_rawchunk (core, desc_pos, descsz, ELF_T_BYTE);
13473 [ - + ]: 12 : if (data == NULL)
13474 : 0 : error_exit (0, _("cannot convert core note data: %s"), elf_errmsg (-1));
13475 : :
13476 : 12 : unsigned char const *ptr = data->d_buf;
13477 : 12 : unsigned char const *const end = data->d_buf + data->d_size;
13478 : :
13479 : 12 : uint64_t count, page_size;
13480 [ - + ]: 12 : if (! buf_read_ulong (core, &ptr, end, &count)
13481 [ - + ]: 12 : || ! buf_read_ulong (core, &ptr, end, &page_size))
13482 : : {
13483 : 0 : fail:
13484 : 0 : printf (" Not enough data in NT_FILE note.\n");
13485 : 0 : return;
13486 : : }
13487 : :
13488 : 12 : size_t addrsize = gelf_fsize (core, ELF_T_ADDR, 1, EV_CURRENT);
13489 : 12 : uint64_t maxcount = (size_t) (end - ptr) / (3 * addrsize);
13490 [ - + ]: 12 : if (count > maxcount)
13491 : 0 : goto fail;
13492 : :
13493 : : /* Where file names are stored. */
13494 : 12 : unsigned char const *const fstart = ptr + 3 * count * addrsize;
13495 : 12 : char const *fptr = (char *) fstart;
13496 : :
13497 : 12 : printf (" %" PRId64 " files:\n", count);
13498 [ + + ]: 118 : for (uint64_t i = 0; i < count; ++i)
13499 : : {
13500 : 106 : uint64_t mstart, mend, moffset;
13501 [ + - ]: 106 : if (! buf_read_ulong (core, &ptr, fstart, &mstart)
13502 [ + - ]: 106 : || ! buf_read_ulong (core, &ptr, fstart, &mend)
13503 [ - + ]: 106 : || ! buf_read_ulong (core, &ptr, fstart, &moffset))
13504 : 0 : goto fail;
13505 : :
13506 : 106 : const char *fnext = memchr (fptr, '\0', (char *) end - fptr);
13507 [ - + ]: 106 : if (fnext == NULL)
13508 : 0 : goto fail;
13509 : :
13510 : 106 : int ct = printf (" %08" PRIx64 "-%08" PRIx64
13511 : : " %08" PRIx64 " %" PRId64,
13512 : : mstart, mend, moffset * page_size, mend - mstart);
13513 [ + - ]: 106 : printf ("%*s%s\n", ct > 50 ? 3 : 53 - ct, "", fptr);
13514 : :
13515 : 106 : fptr = fnext + 1;
13516 : : }
13517 : : }
13518 : :
13519 : : static void
13520 : 76 : handle_core_note (Ebl *ebl, const GElf_Ehdr *ehdr, const GElf_Nhdr *nhdr,
13521 : : const char *name, const void *desc)
13522 : : {
13523 : 76 : GElf_Word regs_offset;
13524 : 76 : size_t nregloc;
13525 : 76 : const Ebl_Register_Location *reglocs;
13526 : 76 : size_t nitems;
13527 : 76 : const Ebl_Core_Item *items;
13528 : :
13529 [ + + ]: 76 : if (! ebl_core_note (ebl, nhdr, name, desc,
13530 : : ®s_offset, &nregloc, ®locs, &nitems, &items))
13531 : 2 : return;
13532 : :
13533 : : /* Pass 0 for DESCSZ when there are registers in the note,
13534 : : so that the ITEMS array does not describe the whole thing.
13535 : : For non-register notes, the actual descsz might be a multiple
13536 : : of the unit size, not just exactly the unit size. */
13537 : 74 : unsigned int colno = handle_core_items (ebl->elf, ehdr, desc,
13538 [ + + ]: 74 : nregloc == 0 ? nhdr->n_descsz : 0,
13539 : : items, nitems);
13540 [ + + ]: 74 : if (colno != 0)
13541 : 68 : putchar ('\n');
13542 : :
13543 : 74 : colno = handle_core_registers (ebl, ebl->elf, desc + regs_offset,
13544 : : reglocs, nregloc);
13545 [ + + ]: 74 : if (colno != 0)
13546 : 36 : putchar ('\n');
13547 : : }
13548 : :
13549 : : static void
13550 : 506 : handle_notes_data (Ebl *ebl, const GElf_Ehdr *ehdr,
13551 : : GElf_Off start, Elf_Data *data)
13552 : : {
13553 : 506 : fputs (_(" Owner Data size Type\n"), stdout);
13554 : :
13555 [ - + ]: 506 : if (data == NULL)
13556 : 0 : goto bad_note;
13557 : :
13558 : : size_t offset = 0;
13559 : : GElf_Nhdr nhdr;
13560 : : size_t name_offset;
13561 : : size_t desc_offset;
13562 : 5120 : while (offset < data->d_size
13563 [ + + + - ]: 5120 : && (offset = gelf_getnote (data, offset,
13564 : : &nhdr, &name_offset, &desc_offset)) > 0)
13565 : : {
13566 [ + - ]: 4614 : const char *name = nhdr.n_namesz == 0 ? "" : data->d_buf + name_offset;
13567 : 4614 : const char *desc = data->d_buf + desc_offset;
13568 : :
13569 : : /* GNU Build Attributes are weird, they store most of their data
13570 : : into the owner name field. Extract just the owner name
13571 : : prefix here, then use the rest later as data. */
13572 : 4614 : bool is_gnu_build_attr
13573 : 4614 : = startswith (name, ELF_NOTE_GNU_BUILD_ATTRIBUTE_PREFIX);
13574 : 9798 : const char *print_name = (is_gnu_build_attr
13575 [ + + ]: 4614 : ? ELF_NOTE_GNU_BUILD_ATTRIBUTE_PREFIX : name);
13576 : 5184 : size_t print_namesz = (is_gnu_build_attr
13577 : 570 : ? strlen (print_name) : nhdr.n_namesz);
13578 : :
13579 : 4614 : char buf[100];
13580 : 4614 : char buf2[100];
13581 : 4614 : printf (_(" %-13.*s %9" PRId32 " %s\n"),
13582 : : (int) print_namesz, print_name, nhdr.n_descsz,
13583 [ + + ]: 4614 : ehdr->e_type == ET_CORE
13584 : 118 : ? ebl_core_note_type_name (ebl, nhdr.n_type,
13585 : : buf, sizeof (buf))
13586 : 4496 : : ebl_object_note_type_name (ebl, name, nhdr.n_type,
13587 : : nhdr.n_descsz,
13588 : : buf2, sizeof (buf2)));
13589 : :
13590 : : /* Filter out invalid entries. */
13591 : 4614 : if (memchr (name, '\0', nhdr.n_namesz) != NULL
13592 : : /* XXX For now help broken Linux kernels. */
13593 : : || 1)
13594 : : {
13595 [ + + ]: 4614 : if (ehdr->e_type == ET_CORE)
13596 : : {
13597 [ + + ]: 118 : if (nhdr.n_type == NT_AUXV
13598 [ + - ]: 18 : && (nhdr.n_namesz == 4 /* Broken old Linux kernels. */
13599 [ + - + - ]: 18 : || (nhdr.n_namesz == 5 && name[4] == '\0'))
13600 [ + - ]: 18 : && !memcmp (name, "CORE", 4))
13601 : 18 : handle_auxv_note (ebl, ebl->elf, nhdr.n_descsz,
13602 : : start + desc_offset);
13603 [ + + + - ]: 100 : else if (nhdr.n_namesz == 5 && strcmp (name, "CORE") == 0)
13604 [ + + + ]: 76 : switch (nhdr.n_type)
13605 : : {
13606 : 12 : case NT_SIGINFO:
13607 : 12 : handle_siginfo_note (ebl->elf, nhdr.n_descsz,
13608 : : start + desc_offset);
13609 : 12 : break;
13610 : :
13611 : 12 : case NT_FILE:
13612 : 12 : handle_file_note (ebl->elf, nhdr.n_descsz,
13613 : : start + desc_offset);
13614 : 12 : break;
13615 : :
13616 : 52 : default:
13617 : 52 : handle_core_note (ebl, ehdr, &nhdr, name, desc);
13618 : : }
13619 : : else
13620 : 24 : handle_core_note (ebl, ehdr, &nhdr, name, desc);
13621 : : }
13622 : : else
13623 : 4496 : ebl_object_note (ebl, nhdr.n_namesz, name, nhdr.n_type,
13624 : : nhdr.n_descsz, desc);
13625 : : }
13626 : : }
13627 : :
13628 [ + - ]: 506 : if (offset == data->d_size)
13629 : 506 : return;
13630 : :
13631 : 0 : bad_note:
13632 : 0 : error (0, 0,
13633 : 0 : _("cannot get content of note: %s"),
13634 : 0 : data != NULL ? "garbage data" : elf_errmsg (-1));
13635 : : }
13636 : :
13637 : : static void
13638 : 230 : handle_notes (Ebl *ebl, GElf_Ehdr *ehdr)
13639 : : {
13640 : : /* If we have section headers, just look for SHT_NOTE sections.
13641 : : In a debuginfo file, the program headers are not reliable. */
13642 [ + + ]: 230 : if (shnum != 0)
13643 : : {
13644 : : /* Get the section header string table index. */
13645 : 208 : size_t shstrndx;
13646 [ - + ]: 208 : if (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0)
13647 : 0 : error_exit (0, _("cannot get section header string table index"));
13648 : :
13649 : : Elf_Scn *scn = NULL;
13650 [ + + ]: 6446 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
13651 : : {
13652 : 6238 : GElf_Shdr shdr_mem;
13653 : 6238 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
13654 : :
13655 [ + - + + ]: 6238 : if (shdr == NULL || shdr->sh_type != SHT_NOTE)
13656 : : /* Not what we are looking for. */
13657 : 5756 : continue;
13658 : :
13659 [ - + ]: 482 : if (notes_section != NULL)
13660 : : {
13661 : 0 : char *sname = elf_strptr (ebl->elf, shstrndx, shdr->sh_name);
13662 [ # # # # ]: 0 : if (sname == NULL || strcmp (sname, notes_section) != 0)
13663 : 0 : continue;
13664 : : }
13665 : :
13666 : 482 : printf (_("\
13667 : : \nNote section [%2zu] '%s' of %" PRIu64 " bytes at offset %#0" PRIx64 ":\n"),
13668 : : elf_ndxscn (scn),
13669 : 482 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
13670 : : shdr->sh_size, shdr->sh_offset);
13671 : :
13672 : 482 : handle_notes_data (ebl, ehdr, shdr->sh_offset,
13673 : : elf_getdata (scn, NULL));
13674 : : }
13675 : 208 : return;
13676 : : }
13677 : :
13678 : : /* We have to look through the program header to find the note
13679 : : sections. There can be more than one. */
13680 [ + + ]: 280 : for (size_t cnt = 0; cnt < phnum; ++cnt)
13681 : : {
13682 : 258 : GElf_Phdr mem;
13683 : 258 : GElf_Phdr *phdr = gelf_getphdr (ebl->elf, cnt, &mem);
13684 : :
13685 [ + - + + ]: 258 : if (phdr == NULL || phdr->p_type != PT_NOTE)
13686 : : /* Not what we are looking for. */
13687 : 234 : continue;
13688 : :
13689 : 24 : printf (_("\
13690 : : \nNote segment of %" PRIu64 " bytes at offset %#0" PRIx64 ":\n"),
13691 : : phdr->p_filesz, phdr->p_offset);
13692 : :
13693 : 24 : handle_notes_data (ebl, ehdr, phdr->p_offset,
13694 : : elf_getdata_rawchunk (ebl->elf,
13695 : 24 : phdr->p_offset, phdr->p_filesz,
13696 [ + + ]: 24 : (phdr->p_align == 8
13697 : : ? ELF_T_NHDR8 : ELF_T_NHDR)));
13698 : : }
13699 : : }
13700 : :
13701 : :
13702 : : static void
13703 : 12 : hex_dump (const uint8_t *data, size_t len)
13704 : : {
13705 : 12 : size_t pos = 0;
13706 [ + + ]: 56 : while (pos < len)
13707 : : {
13708 : 44 : printf (" 0x%08zx ", pos);
13709 : :
13710 : 44 : const size_t chunk = MIN (len - pos, 16);
13711 : :
13712 [ + + ]: 688 : for (size_t i = 0; i < chunk; ++i)
13713 [ + + ]: 644 : if (i % 4 == 3)
13714 : 160 : printf ("%02x ", data[pos + i]);
13715 : : else
13716 : 484 : printf ("%02x", data[pos + i]);
13717 : :
13718 [ + + ]: 44 : if (chunk < 16)
13719 : 4 : printf ("%*s", (int) ((16 - chunk) * 2 + (16 - chunk + 3) / 4), "");
13720 : :
13721 [ + + ]: 688 : for (size_t i = 0; i < chunk; ++i)
13722 : : {
13723 : 644 : unsigned char b = data[pos + i];
13724 [ + + ]: 1288 : printf ("%c", isprint (b) ? b : '.');
13725 : : }
13726 : :
13727 : 44 : putchar ('\n');
13728 : 44 : pos += chunk;
13729 : : }
13730 : 12 : }
13731 : :
13732 : : static void
13733 : 12 : dump_data_section (Elf_Scn *scn, const GElf_Shdr *shdr, const char *name)
13734 : : {
13735 [ + - - + ]: 12 : if (shdr->sh_size == 0 || shdr->sh_type == SHT_NOBITS)
13736 : 0 : printf (_("\nSection [%zu] '%s' has no data to dump.\n"),
13737 : : elf_ndxscn (scn), name);
13738 : : else
13739 : : {
13740 [ + + ]: 12 : if (print_decompress)
13741 : : {
13742 : : /* We try to decompress the section, but keep the old shdr around
13743 : : so we can show both the original shdr size and the uncompressed
13744 : : data size. */
13745 [ + + ]: 8 : if ((shdr->sh_flags & SHF_COMPRESSED) != 0)
13746 : : {
13747 [ - + ]: 4 : if (elf_compress (scn, 0, 0) < 0)
13748 : 0 : printf ("WARNING: %s [%zd]\n",
13749 : : _("Couldn't uncompress section"),
13750 : : elf_ndxscn (scn));
13751 : : }
13752 [ + - + - ]: 4 : else if (name && startswith (name, ".zdebug"))
13753 : : {
13754 [ - + ]: 4 : if (elf_compress_gnu (scn, 0, 0) < 0)
13755 : 0 : printf ("WARNING: %s [%zd]\n",
13756 : : _("Couldn't uncompress section"),
13757 : : elf_ndxscn (scn));
13758 : : }
13759 : : }
13760 : :
13761 : 12 : Elf_Data *data = elf_rawdata (scn, NULL);
13762 [ - + ]: 12 : if (data == NULL)
13763 : 0 : error (0, 0, _("cannot get data for section [%zu] '%s': %s"),
13764 : : elf_ndxscn (scn), name, elf_errmsg (-1));
13765 : : else
13766 : : {
13767 [ + + ]: 12 : if (data->d_size == shdr->sh_size)
13768 : 4 : printf (_("\nHex dump of section [%zu] '%s', %" PRIu64
13769 : : " bytes at offset %#0" PRIx64 ":\n"),
13770 : : elf_ndxscn (scn), name,
13771 : 4 : shdr->sh_size, shdr->sh_offset);
13772 : : else
13773 : 8 : printf (_("\nHex dump of section [%zu] '%s', %" PRIu64
13774 : : " bytes (%zd uncompressed) at offset %#0"
13775 : : PRIx64 ":\n"),
13776 : : elf_ndxscn (scn), name,
13777 : 8 : shdr->sh_size, data->d_size, shdr->sh_offset);
13778 : 12 : hex_dump (data->d_buf, data->d_size);
13779 : : }
13780 : : }
13781 : 12 : }
13782 : :
13783 : : static void
13784 : 374 : print_string_section (Elf_Scn *scn, const GElf_Shdr *shdr, const char *name)
13785 : : {
13786 [ + - + + ]: 374 : if (shdr->sh_size == 0 || shdr->sh_type == SHT_NOBITS)
13787 : 42 : printf (_("\nSection [%zu] '%s' has no strings to dump.\n"),
13788 : : elf_ndxscn (scn), name);
13789 : : else
13790 : : {
13791 [ + + ]: 332 : if (print_decompress)
13792 : : {
13793 : : /* We try to decompress the section, but keep the old shdr around
13794 : : so we can show both the original shdr size and the uncompressed
13795 : : data size. */
13796 [ - + ]: 2 : if ((shdr->sh_flags & SHF_COMPRESSED) != 0)
13797 : : {
13798 [ # # ]: 0 : if (elf_compress (scn, 0, 0) < 0)
13799 : 0 : printf ("WARNING: %s [%zd]\n",
13800 : : _("Couldn't uncompress section"),
13801 : : elf_ndxscn (scn));
13802 : : }
13803 [ + - + - ]: 2 : else if (name && startswith (name, ".zdebug"))
13804 : : {
13805 [ - + ]: 2 : if (elf_compress_gnu (scn, 0, 0) < 0)
13806 : 0 : printf ("WARNING: %s [%zd]\n",
13807 : : _("Couldn't uncompress section"),
13808 : : elf_ndxscn (scn));
13809 : : }
13810 : : }
13811 : :
13812 : 332 : Elf_Data *data = elf_rawdata (scn, NULL);
13813 [ - + ]: 332 : if (data == NULL)
13814 : 0 : error (0, 0, _("cannot get data for section [%zu] '%s': %s"),
13815 : : elf_ndxscn (scn), name, elf_errmsg (-1));
13816 : : else
13817 : : {
13818 [ + + ]: 332 : if (data->d_size == shdr->sh_size)
13819 : 330 : printf (_("\nString section [%zu] '%s' contains %" PRIu64
13820 : : " bytes at offset %#0" PRIx64 ":\n"),
13821 : : elf_ndxscn (scn), name,
13822 : 330 : shdr->sh_size, shdr->sh_offset);
13823 : : else
13824 : 2 : printf (_("\nString section [%zu] '%s' contains %" PRIu64
13825 : : " bytes (%zd uncompressed) at offset %#0"
13826 : : PRIx64 ":\n"),
13827 : : elf_ndxscn (scn), name,
13828 : 2 : shdr->sh_size, data->d_size, shdr->sh_offset);
13829 : :
13830 : 332 : const char *start = data->d_buf;
13831 : 332 : const char *const limit = start + data->d_size;
13832 : 46964 : do
13833 : : {
13834 : 46964 : const char *end = memchr (start, '\0', limit - start);
13835 : 46964 : const size_t pos = start - (const char *) data->d_buf;
13836 [ - + ]: 46964 : if (unlikely (end == NULL))
13837 : : {
13838 : 0 : printf (" [%6zx]- %.*s\n",
13839 : : pos, (int) (limit - start), start);
13840 : 0 : break;
13841 : : }
13842 : 46964 : printf (" [%6zx] %s\n", pos, start);
13843 : 46964 : start = end + 1;
13844 [ + + ]: 46964 : } while (start < limit);
13845 : : }
13846 : : }
13847 : 374 : }
13848 : :
13849 : : static void
13850 : 198 : for_each_section_argument (Elf *elf, const struct section_argument *list,
13851 : : void (*dump) (Elf_Scn *scn, const GElf_Shdr *shdr,
13852 : : const char *name))
13853 : : {
13854 : : /* Get the section header string table index. */
13855 : 198 : size_t shstrndx;
13856 [ - + ]: 198 : if (elf_getshdrstrndx (elf, &shstrndx) < 0)
13857 : 0 : error_exit (0, _("cannot get section header string table index"));
13858 : :
13859 [ + + ]: 764 : for (const struct section_argument *a = list; a != NULL; a = a->next)
13860 : : {
13861 : 566 : Elf_Scn *scn;
13862 : 566 : GElf_Shdr shdr_mem;
13863 : 566 : const char *name = NULL;
13864 : :
13865 : 566 : char *endp = NULL;
13866 : 566 : unsigned long int shndx = strtoul (a->arg, &endp, 0);
13867 [ + + - + ]: 566 : if (endp != a->arg && *endp == '\0')
13868 : : {
13869 : 2 : scn = elf_getscn (elf, shndx);
13870 [ - + ]: 2 : if (scn == NULL)
13871 : : {
13872 : 0 : error (0, 0, _("\nsection [%lu] does not exist"), shndx);
13873 : 0 : continue;
13874 : : }
13875 : :
13876 [ - + ]: 2 : if (gelf_getshdr (scn, &shdr_mem) == NULL)
13877 : 0 : error_exit (0, _("cannot get section header: %s"),
13878 : : elf_errmsg (-1));
13879 : 2 : name = elf_strptr (elf, shstrndx, shdr_mem.sh_name);
13880 : 2 : (*dump) (scn, &shdr_mem, name);
13881 : : }
13882 : : else
13883 : : {
13884 : : /* Need to look up the section by name. */
13885 : : scn = NULL;
13886 : : bool found = false;
13887 [ + + ]: 18180 : while ((scn = elf_nextscn (elf, scn)) != NULL)
13888 : : {
13889 [ - + ]: 17616 : if (gelf_getshdr (scn, &shdr_mem) == NULL)
13890 : 0 : continue;
13891 : 17616 : name = elf_strptr (elf, shstrndx, shdr_mem.sh_name);
13892 [ - + ]: 17616 : if (name == NULL)
13893 : 0 : continue;
13894 [ + + ]: 17616 : if (!strcmp (name, a->arg))
13895 : : {
13896 : 384 : found = true;
13897 : 384 : (*dump) (scn, &shdr_mem, name);
13898 : : }
13899 : : }
13900 : :
13901 [ + + - + ]: 564 : if (unlikely (!found) && !a->implicit)
13902 : 0 : error (0, 0, _("\nsection '%s' does not exist"), a->arg);
13903 : : }
13904 : : }
13905 : 198 : }
13906 : :
13907 : : static void
13908 : 12 : dump_data (Ebl *ebl)
13909 : : {
13910 : 12 : for_each_section_argument (ebl->elf, dump_data_sections, &dump_data_section);
13911 : 12 : }
13912 : :
13913 : : static void
13914 : 186 : dump_strings (Ebl *ebl)
13915 : : {
13916 : 186 : for_each_section_argument (ebl->elf, string_sections, &print_string_section);
13917 : 186 : }
13918 : :
13919 : : static void
13920 : 0 : print_strings (Ebl *ebl)
13921 : : {
13922 : : /* Get the section header string table index. */
13923 : 0 : size_t shstrndx;
13924 [ # # ]: 0 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
13925 : 0 : error_exit (0, _("cannot get section header string table index"));
13926 : :
13927 : : Elf_Scn *scn;
13928 : : GElf_Shdr shdr_mem;
13929 : : const char *name;
13930 : : scn = NULL;
13931 [ # # ]: 0 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
13932 : : {
13933 [ # # ]: 0 : if (gelf_getshdr (scn, &shdr_mem) == NULL)
13934 : 0 : continue;
13935 : :
13936 [ # # ]: 0 : if (shdr_mem.sh_type != SHT_PROGBITS
13937 [ # # ]: 0 : || !(shdr_mem.sh_flags & SHF_STRINGS))
13938 : 0 : continue;
13939 : :
13940 : 0 : name = elf_strptr (ebl->elf, shstrndx, shdr_mem.sh_name);
13941 [ # # ]: 0 : if (name == NULL)
13942 : 0 : continue;
13943 : :
13944 : 0 : print_string_section (scn, &shdr_mem, name);
13945 : : }
13946 : 0 : }
13947 : :
13948 : : static void
13949 : 4 : dump_archive_index (Elf *elf, const char *fname)
13950 : : {
13951 : 4 : size_t narsym;
13952 : 4 : const Elf_Arsym *arsym = elf_getarsym (elf, &narsym);
13953 [ - + ]: 4 : if (arsym == NULL)
13954 : : {
13955 : 0 : int result = elf_errno ();
13956 [ # # ]: 0 : if (unlikely (result != ELF_E_NO_INDEX))
13957 : 0 : error_exit (0, _("cannot get symbol index of archive '%s': %s"),
13958 : : fname, elf_errmsg (result));
13959 : : else
13960 : 0 : printf (_("\nArchive '%s' has no symbol index\n"), fname);
13961 : 0 : return;
13962 : : }
13963 : :
13964 : 4 : printf (_("\nIndex of archive '%s' has %zu entries:\n"),
13965 : : fname, narsym);
13966 : :
13967 : 4 : size_t as_off = 0;
13968 [ + + ]: 22 : for (const Elf_Arsym *s = arsym; s < &arsym[narsym - 1]; ++s)
13969 : : {
13970 [ + + ]: 18 : if (s->as_off != as_off)
13971 : : {
13972 : 12 : as_off = s->as_off;
13973 : :
13974 : 12 : Elf *subelf = NULL;
13975 [ + - ]: 12 : if (unlikely (elf_rand (elf, as_off) == 0)
13976 [ - + ]: 12 : || unlikely ((subelf = elf_begin (-1, ELF_C_READ_MMAP, elf))
13977 : : == NULL))
13978 : : #if __GLIBC__ < 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ < 7)
13979 : : while (1)
13980 : : #endif
13981 : 0 : error_exit (0,
13982 : : _("cannot extract member at offset %zu in '%s': %s"),
13983 : : as_off, fname, elf_errmsg (-1));
13984 : :
13985 : 12 : const Elf_Arhdr *h = elf_getarhdr (subelf);
13986 [ + - ]: 12 : if (h != NULL)
13987 : 12 : printf (_("Archive member '%s' contains:\n"), h->ar_name);
13988 : :
13989 : 12 : elf_end (subelf);
13990 : : }
13991 : :
13992 : 18 : printf ("\t%s\n", s->as_name);
13993 : : }
13994 : : }
13995 : :
13996 : : #include "debugpred.h"
|