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 (validate_str (symstr_data->d_buf, sym->st_name,
2878 : : symstr_data->d_size))
2879 : 0 : sym_name = (char *)symstr_data->d_buf + sym->st_name;
2880 : : else
2881 : : sym_name = NULL;
2882 : : }
2883 : : else
2884 : 80490 : sym_name = elf_strptr (ebl->elf, idx, sym->st_name);
2885 : :
2886 [ - + ]: 80490 : if (sym_name == NULL)
2887 : : sym_name = "???";
2888 : :
2889 [ + + ]: 160092 : printf (_ ("\
2890 : : %5u: %0*" PRIx64 " %6" PRId64 " %-7s %-6s %-9s %6s %s"),
2891 : 80490 : cnt, gelf_getclass (ebl->elf) == ELFCLASS32 ? 8 : 16,
2892 : : sym->st_value, sym->st_size,
2893 : 80490 : ebl_symbol_type_name (ebl, GELF_ST_TYPE (sym->st_info), typebuf,
2894 : : sizeof (typebuf)),
2895 : 80490 : ebl_symbol_binding_name (ebl, GELF_ST_BIND (sym->st_info),
2896 : : bindbuf, sizeof (bindbuf)),
2897 : 80490 : get_visibility_type (GELF_ST_VISIBILITY (sym->st_other)),
2898 : 80490 : ebl_section_name (ebl, sym->st_shndx, xndx, scnbuf,
2899 : : sizeof (scnbuf), NULL, shnum),
2900 : : sym_name);
2901 : :
2902 [ + + ]: 80490 : if (versym_data != NULL)
2903 : : {
2904 : : /* Get the version information. */
2905 : 3436 : GElf_Versym versym_mem;
2906 : 3436 : GElf_Versym *versym = gelf_getversym (versym_data, cnt, &versym_mem);
2907 : :
2908 [ + - + + ]: 3436 : if (versym != NULL && ((*versym & 0x8000) != 0 || *versym > 1))
2909 : : {
2910 : 2476 : bool is_nobits = false;
2911 : 2476 : bool check_def = xndx != SHN_UNDEF;
2912 : :
2913 [ + + - + ]: 2476 : if (xndx < SHN_LORESERVE || sym->st_shndx == SHN_XINDEX)
2914 : : {
2915 : 2456 : GElf_Shdr symshdr_mem;
2916 : 2456 : GElf_Shdr *symshdr = gelf_getshdr (
2917 : : elf_getscn (ebl->elf, xndx), &symshdr_mem);
2918 : :
2919 : 2456 : is_nobits
2920 [ + - + + ]: 4888 : = (symshdr != NULL && symshdr->sh_type == SHT_NOBITS);
2921 : : }
2922 : :
2923 [ + + ]: 2476 : if (is_nobits || !check_def)
2924 : : {
2925 : : /* We must test both. */
2926 : 2172 : GElf_Vernaux vernaux_mem;
2927 : 2172 : GElf_Vernaux *vernaux = NULL;
2928 : 2172 : size_t vn_offset = 0;
2929 : :
2930 : 2172 : GElf_Verneed verneed_mem;
2931 : 2172 : GElf_Verneed *verneed
2932 : 2172 : = gelf_getverneed (verneed_data, 0, &verneed_mem);
2933 [ + - ]: 7902 : while (verneed != NULL)
2934 : : {
2935 : 7902 : size_t vna_offset = vn_offset;
2936 : :
2937 : 7902 : vernaux = gelf_getvernaux (verneed_data,
2938 : 7902 : vna_offset += verneed->vn_aux,
2939 : : &vernaux_mem);
2940 [ + + ]: 28762 : while (vernaux != NULL && vernaux->vna_other != *versym
2941 [ + + ]: 18688 : && vernaux->vna_next != 0
2942 [ + - ]: 20860 : && (verneed_data->d_size - vna_offset
2943 [ + - ]: 12958 : >= vernaux->vna_next))
2944 : : {
2945 : : /* Update the offset. */
2946 : 12958 : vna_offset += vernaux->vna_next;
2947 : :
2948 : 12958 : vernaux = (vernaux->vna_next == 0
2949 : : ? NULL
2950 : 12958 : : gelf_getvernaux (verneed_data,
2951 : : vna_offset,
2952 : : &vernaux_mem));
2953 : : }
2954 : :
2955 : : /* Check whether we found the version. */
2956 [ + - + + ]: 7902 : if (vernaux != NULL && vernaux->vna_other == *versym)
2957 : : /* Found it. */
2958 : : break;
2959 : :
2960 [ + - ]: 5730 : if (verneed_data->d_size - vn_offset < verneed->vn_next)
2961 : : break;
2962 : :
2963 : 5730 : vn_offset += verneed->vn_next;
2964 : 5730 : verneed
2965 : : = (verneed->vn_next == 0
2966 : : ? NULL
2967 [ + - ]: 5730 : : gelf_getverneed (verneed_data, vn_offset,
2968 : : &verneed_mem));
2969 : : }
2970 : :
2971 [ + - + - ]: 2172 : if (vernaux != NULL && vernaux->vna_other == *versym)
2972 : : {
2973 : 4344 : printf ("@%s (%u)",
2974 [ - + ]: 2172 : use_dynamic_segment == true
2975 : 0 : ? (char *)symstr_data->d_buf
2976 : 0 : + vernaux->vna_name
2977 : 2172 : : elf_strptr (ebl->elf, verneed_stridx,
2978 : 2172 : vernaux->vna_name),
2979 : : (unsigned int)vernaux->vna_other);
2980 : 2172 : check_def = 0;
2981 : : }
2982 [ # # ]: 0 : else if (unlikely (!is_nobits))
2983 : 0 : error (0, 0, _ ("bad dynamic symbol"));
2984 : : else
2985 : : check_def = 1;
2986 : : }
2987 : :
2988 [ + - + - ]: 2476 : if (check_def && *versym != 0x8001)
2989 : : {
2990 : : /* We must test both. */
2991 : 304 : size_t vd_offset = 0;
2992 : :
2993 : 304 : GElf_Verdef verdef_mem;
2994 : 304 : GElf_Verdef *verdef
2995 : 304 : = gelf_getverdef (verdef_data, 0, &verdef_mem);
2996 [ + - ]: 866 : while (verdef != NULL)
2997 : : {
2998 [ + + ]: 866 : if (verdef->vd_ndx == (*versym & 0x7fff))
2999 : : /* Found the definition. */
3000 : : break;
3001 : :
3002 [ + - ]: 562 : if (verdef_data->d_size - vd_offset < verdef->vd_next)
3003 : : break;
3004 : :
3005 : 562 : vd_offset += verdef->vd_next;
3006 : 562 : verdef = (verdef->vd_next == 0
3007 : : ? NULL
3008 [ + - ]: 562 : : gelf_getverdef (verdef_data, vd_offset,
3009 : : &verdef_mem));
3010 : : }
3011 : :
3012 [ + - ]: 304 : if (verdef != NULL)
3013 : : {
3014 : 304 : GElf_Verdaux verdaux_mem;
3015 : 608 : GElf_Verdaux *verdaux = gelf_getverdaux (
3016 : 304 : verdef_data, vd_offset + verdef->vd_aux,
3017 : : &verdaux_mem);
3018 : :
3019 [ + - ]: 304 : if (verdaux != NULL)
3020 [ + - ]: 608 : printf ((*versym & 0x8000) ? "@%s" : "@@%s",
3021 [ - + ]: 304 : use_dynamic_segment == true
3022 : 0 : ? (char *)symstr_data->d_buf
3023 : 0 : + verdaux->vda_name
3024 : 304 : : elf_strptr (ebl->elf, verdef_stridx,
3025 : 304 : verdaux->vda_name));
3026 : : }
3027 : : }
3028 : : }
3029 : : }
3030 : :
3031 : 80490 : putchar ('\n');
3032 : : }
3033 : 266 : }
3034 : :
3035 : :
3036 : : static bool
3037 : 266 : handle_symtab (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr)
3038 : : {
3039 : 266 : Elf_Data *versym_data = NULL;
3040 : 266 : Elf_Data *verneed_data = NULL;
3041 : 266 : Elf_Data *verdef_data = NULL;
3042 : 266 : Elf_Data *xndx_data = NULL;
3043 : 266 : int class = gelf_getclass (ebl->elf);
3044 : 266 : Elf32_Word verneed_stridx = 0;
3045 : 266 : Elf32_Word verdef_stridx = 0;
3046 : :
3047 : : /* Get the data of the section. */
3048 : 266 : Elf_Data *data = elf_getdata (scn, NULL);
3049 [ + - ]: 266 : if (data == NULL)
3050 : : return false;
3051 : :
3052 : : /* Find out whether we have other sections we might need. */
3053 : : Elf_Scn *runscn = NULL;
3054 [ + + ]: 8784 : while ((runscn = elf_nextscn (ebl->elf, runscn)) != NULL)
3055 : : {
3056 : 8518 : GElf_Shdr runshdr_mem;
3057 : 8518 : GElf_Shdr *runshdr = gelf_getshdr (runscn, &runshdr_mem);
3058 : :
3059 [ + - ]: 8518 : if (likely (runshdr != NULL))
3060 : : {
3061 [ + + ]: 8518 : if (runshdr->sh_type == SHT_GNU_versym
3062 [ + + ]: 202 : && runshdr->sh_link == elf_ndxscn (scn))
3063 : : /* Bingo, found the version information. Now get the data. */
3064 : 150 : versym_data = elf_getdata (runscn, NULL);
3065 [ + + ]: 8368 : else if (runshdr->sh_type == SHT_GNU_verneed)
3066 : : {
3067 : : /* This is the information about the needed versions. */
3068 : 202 : verneed_data = elf_getdata (runscn, NULL);
3069 : 202 : verneed_stridx = runshdr->sh_link;
3070 : : }
3071 [ + + ]: 8166 : else if (runshdr->sh_type == SHT_GNU_verdef)
3072 : : {
3073 : : /* This is the information about the defined versions. */
3074 : 8 : verdef_data = elf_getdata (runscn, NULL);
3075 : 8 : verdef_stridx = runshdr->sh_link;
3076 : : }
3077 [ + - ]: 8158 : else if (runshdr->sh_type == SHT_SYMTAB_SHNDX
3078 [ # # ]: 0 : && runshdr->sh_link == elf_ndxscn (scn))
3079 : : /* Extended section index. */
3080 : 0 : xndx_data = elf_getdata (runscn, NULL);
3081 : : }
3082 : : }
3083 : :
3084 : : /* Get the section header string table index. */
3085 : 266 : size_t shstrndx;
3086 [ - + ]: 266 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
3087 : 0 : error_exit (0, _("cannot get section header string table index"));
3088 : :
3089 : 266 : GElf_Shdr glink_mem;
3090 : 266 : GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
3091 : : &glink_mem);
3092 [ - + ]: 266 : if (glink == NULL)
3093 : 0 : error_exit (0, _("invalid sh_link value in section %zu"),
3094 : : elf_ndxscn (scn));
3095 : :
3096 : : /* Now we can compute the number of entries in the section. */
3097 : 532 : unsigned int nsyms = data->d_size / (class == ELFCLASS32
3098 : : ? sizeof (Elf32_Sym)
3099 [ + + ]: 266 : : sizeof (Elf64_Sym));
3100 : :
3101 : 266 : printf (ngettext ("\nSymbol table [%2u] '%s' contains %u entry:\n",
3102 : : "\nSymbol table [%2u] '%s' contains %u entries:\n",
3103 : : nsyms),
3104 : 266 : (unsigned int) elf_ndxscn (scn),
3105 : 266 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name), nsyms);
3106 : 266 : printf (ngettext (" %lu local symbol String table: [%2u] '%s'\n",
3107 : : " %lu local symbols String table: [%2u] '%s'\n",
3108 : : shdr->sh_info),
3109 : 266 : (unsigned long int) shdr->sh_info,
3110 : 266 : (unsigned int) shdr->sh_link,
3111 : 266 : elf_strptr (ebl->elf, shstrndx, glink->sh_name));
3112 : :
3113 [ + + ]: 532 : fputs (class == ELFCLASS32
3114 : 32 : ? _("\
3115 : : Num: Value Size Type Bind Vis Ndx Name\n")
3116 : 234 : : _("\
3117 : : Num: Value Size Type Bind Vis Ndx Name\n"),
3118 : : stdout);
3119 : :
3120 : 266 : process_symtab(ebl, nsyms, shdr->sh_link, verneed_stridx, verdef_stridx,
3121 : : data, versym_data, NULL, verneed_data, verdef_data, xndx_data);
3122 : 266 : return true;
3123 : : }
3124 : :
3125 : :
3126 : : static bool
3127 : 0 : handle_dynamic_symtab (Ebl *ebl)
3128 : : {
3129 : 0 : GElf_Phdr phdr_mem;
3130 : 0 : GElf_Phdr *phdr = NULL;
3131 : : /* phnum is a static variable which was already fetched in function
3132 : : process_elf_file. */
3133 [ # # ]: 0 : for (size_t i = 0; i < phnum; ++i)
3134 : : {
3135 : 0 : phdr = gelf_getphdr (ebl->elf, i, &phdr_mem);
3136 [ # # # # ]: 0 : if (phdr == NULL || phdr->p_type == PT_DYNAMIC)
3137 : : break;
3138 : : }
3139 [ # # ]: 0 : if (phdr == NULL)
3140 : : return false;
3141 : :
3142 : 0 : GElf_Addr addrs[i_max] = {
3143 : : 0,
3144 : : };
3145 : 0 : GElf_Off offs[i_max] = {
3146 : : 0,
3147 : : };
3148 : 0 : get_dynscn_addrs (ebl->elf, phdr, addrs);
3149 : 0 : find_offsets (ebl->elf, 0, i_max, addrs, offs);
3150 : :
3151 : 0 : size_t syments = 0;
3152 : :
3153 : 0 : GElf_Ehdr ehdr_mem;
3154 : 0 : GElf_Ehdr *ehdr = gelf_getehdr (ebl->elf, &ehdr_mem);
3155 : :
3156 [ # # ]: 0 : if (offs[i_hash] != 0)
3157 : : {
3158 : : /* In the original format, .hash says the size of .dynsym. */
3159 : :
3160 [ # # # # : 0 : size_t entsz = SH_ENTSIZE_HASH (ehdr);
# # ]
3161 : : Elf_Data *data
3162 : 0 : = elf_getdata_rawchunk (ebl->elf, offs[i_hash] + entsz, entsz,
3163 : : (entsz == 4 ? ELF_T_WORD : ELF_T_XWORD));
3164 [ # # ]: 0 : if (data != NULL)
3165 : 0 : syments = (entsz == 4 ? *(const GElf_Word *)data->d_buf
3166 [ # # ]: 0 : : *(const GElf_Xword *)data->d_buf);
3167 : : }
3168 [ # # # # ]: 0 : if (offs[i_gnu_hash] != 0 && syments == 0)
3169 : : {
3170 : : /* In the new format, we can derive it with some work. */
3171 : :
3172 : 0 : const struct
3173 : : {
3174 : : Elf32_Word nbuckets;
3175 : : Elf32_Word symndx;
3176 : : Elf32_Word maskwords;
3177 : : Elf32_Word shift2;
3178 : : } * header;
3179 : :
3180 : 0 : Elf_Data *data = elf_getdata_rawchunk (ebl->elf, offs[i_gnu_hash],
3181 : : sizeof *header, ELF_T_WORD);
3182 [ # # ]: 0 : if (data != NULL)
3183 : : {
3184 : 0 : header = data->d_buf;
3185 : 0 : Elf32_Word nbuckets = header->nbuckets;
3186 : 0 : Elf32_Word symndx = header->symndx;
3187 : 0 : GElf_Off buckets_at
3188 : : = (offs[i_gnu_hash] + sizeof *header
3189 : 0 : + (gelf_getclass (ebl->elf) * sizeof (Elf32_Word)
3190 : 0 : * header->maskwords));
3191 : :
3192 : : // elf_getdata_rawchunk takes a size_t, make sure it
3193 : : // doesn't overflow.
3194 : : #if SIZE_MAX <= UINT32_MAX
3195 : : if (nbuckets > SIZE_MAX / sizeof (Elf32_Word))
3196 : : data = NULL;
3197 : : else
3198 : : #endif
3199 : 0 : data = elf_getdata_rawchunk (ebl->elf, buckets_at,
3200 : : nbuckets * sizeof (Elf32_Word),
3201 : : ELF_T_WORD);
3202 [ # # ]: 0 : if (data != NULL && symndx < nbuckets)
3203 : : {
3204 : 0 : const Elf32_Word *const buckets = data->d_buf;
3205 : 0 : Elf32_Word maxndx = symndx;
3206 [ # # ]: 0 : for (Elf32_Word bucket = 0; bucket < nbuckets; ++bucket)
3207 : 0 : if (buckets[bucket] > maxndx)
3208 : : maxndx = buckets[bucket];
3209 : :
3210 : 0 : GElf_Off hasharr_at
3211 : : = (buckets_at + nbuckets * sizeof (Elf32_Word));
3212 : 0 : hasharr_at += (maxndx - symndx) * sizeof (Elf32_Word);
3213 : 0 : do
3214 : : {
3215 : 0 : data = elf_getdata_rawchunk (
3216 : : ebl->elf, hasharr_at, sizeof (Elf32_Word), ELF_T_WORD);
3217 [ # # # # ]: 0 : if (data != NULL && (*(const Elf32_Word *)data->d_buf & 1u))
3218 : : {
3219 : 0 : syments = maxndx + 1;
3220 : 0 : break;
3221 : : }
3222 : 0 : ++maxndx;
3223 : 0 : hasharr_at += sizeof (Elf32_Word);
3224 : : }
3225 [ # # ]: 0 : while (data != NULL);
3226 : : }
3227 : : }
3228 : : }
3229 [ # # # # ]: 0 : if (offs[i_strtab] > offs[i_symtab] && syments == 0)
3230 : 0 : syments = ((offs[i_strtab] - offs[i_symtab])
3231 : 0 : / gelf_fsize (ebl->elf, ELF_T_SYM, 1, EV_CURRENT));
3232 : :
3233 [ # # # # : 0 : if (syments <= 0 || offs[i_strtab] == 0 || offs[i_symtab] == 0)
# # ]
3234 : : {
3235 : 0 : error_exit (0, _ ("Dynamic symbol information is not available for "
3236 : : "displaying symbols."));
3237 : : }
3238 : :
3239 : : /* All the data chunk initializaion. */
3240 : 0 : Elf_Data *symdata = NULL;
3241 : 0 : Elf_Data *symstrdata = NULL;
3242 : 0 : Elf_Data *versym_data = NULL;
3243 : 0 : Elf_Data *verdef_data = NULL;
3244 : 0 : Elf_Data *verneed_data = NULL;
3245 : :
3246 : 0 : if (offs[i_symtab] != 0)
3247 : 0 : symdata = elf_getdata_rawchunk (
3248 : : ebl->elf, offs[i_symtab],
3249 : : gelf_fsize (ebl->elf, ELF_T_SYM, syments, EV_CURRENT), ELF_T_SYM);
3250 : :
3251 [ # # ]: 0 : if (offs[i_strtab] != 0 && addrs[i_strsz] != 0)
3252 : 0 : symstrdata = elf_getdata_rawchunk (ebl->elf, offs[i_strtab], addrs[i_strsz],
3253 : : ELF_T_BYTE);
3254 : :
3255 [ # # ]: 0 : if (offs[i_versym] != 0)
3256 : 0 : versym_data = elf_getdata_rawchunk (
3257 : : ebl->elf, offs[i_versym], syments * sizeof (Elf64_Half), ELF_T_HALF);
3258 : :
3259 : : /* Get the verneed_data without vernaux. */
3260 [ # # # # ]: 0 : if (offs[i_verneed] != 0 && addrs[i_verneednum] != 0)
3261 : : {
3262 : 0 : verneed_data = elf_getdata_rawchunk (
3263 : : ebl->elf, offs[i_verneed], addrs[i_verneednum] * sizeof (Elf64_Verneed),
3264 : : ELF_T_VNEED);
3265 : :
3266 [ # # ]: 0 : if (verneed_data->d_size < sizeof (GElf_Verneed))
3267 : 0 : error_exit (0, _("malformed SHT_GNU_verneed data"));
3268 : : }
3269 : :
3270 : 0 : size_t vernauxnum = 0;
3271 : 0 : size_t vn_next_offset = 0;
3272 : :
3273 [ # # ]: 0 : if (verneed_data != NULL && verneed_data->d_buf != NULL)
3274 [ # # ]: 0 : for (size_t i = 0; i < addrs[i_verneednum]; i++)
3275 : : {
3276 [ # # ]: 0 : if (vn_next_offset > (verneed_data->d_size - sizeof (GElf_Verneed)))
3277 : 0 : error_exit (0, _("invalid SHT_GNU_verneed data"));
3278 : :
3279 : 0 : GElf_Verneed *verneed
3280 : : = (GElf_Verneed *)(verneed_data->d_buf + vn_next_offset);
3281 : 0 : vernauxnum += verneed->vn_cnt;
3282 : 0 : vn_next_offset += verneed->vn_next;
3283 : : }
3284 : :
3285 : : /* Update the verneed_data to include the vernaux. */
3286 [ # # # # ]: 0 : if (offs[i_verneed] != 0 && addrs[i_verneednum] != 0)
3287 : 0 : verneed_data = elf_getdata_rawchunk (
3288 : : ebl->elf, offs[i_verneed],
3289 : 0 : (addrs[i_verneednum] + vernauxnum) * sizeof (GElf_Verneed),
3290 : : ELF_T_VNEED);
3291 : :
3292 : : /* Get the verdef_data without verdaux. */
3293 [ # # # # ]: 0 : if (offs[i_verdef] != 0 && addrs[i_verdefnum] != 0)
3294 : : {
3295 : 0 : verdef_data = elf_getdata_rawchunk (
3296 : : ebl->elf, offs[i_verdef], addrs[i_verdefnum] * sizeof (Elf64_Verdef),
3297 : : ELF_T_VDEF);
3298 : :
3299 [ # # ]: 0 : if (verdef_data->d_size < sizeof (GElf_Verdef))
3300 : 0 : error_exit (0, _("malformed SHT_GNU_verdef data"));
3301 : : }
3302 : :
3303 : 0 : size_t verdauxnum = 0;
3304 : 0 : size_t vd_next_offset = 0;
3305 : :
3306 [ # # ]: 0 : if (verdef_data != NULL && verdef_data->d_buf != NULL)
3307 [ # # ]: 0 : for (size_t i = 0; i < addrs[i_verdefnum]; i++)
3308 : : {
3309 [ # # ]: 0 : if (vd_next_offset > (verdef_data->d_size - sizeof (GElf_Verdef)))
3310 : 0 : error_exit (0, _("invalid SHT_GNU_verdef data"));
3311 : :
3312 : 0 : GElf_Verdef *verdef
3313 : : = (GElf_Verdef *)(verdef_data->d_buf + vd_next_offset);
3314 : 0 : verdauxnum += verdef->vd_cnt;
3315 : 0 : vd_next_offset += verdef->vd_next;
3316 : : }
3317 : :
3318 : : /* Update the verdef_data to include the verdaux. */
3319 [ # # # # ]: 0 : if (offs[i_verdef] != 0 && addrs[i_verdefnum] != 0)
3320 : 0 : verdef_data = elf_getdata_rawchunk (
3321 : : ebl->elf, offs[i_verdef],
3322 : 0 : (addrs[i_verdefnum] + verdauxnum) * sizeof (GElf_Verdef), ELF_T_VDEF);
3323 : :
3324 : 0 : unsigned int nsyms = (unsigned int)syments;
3325 : 0 : process_symtab (ebl, nsyms, 0, 0, 0, symdata, versym_data, symstrdata,
3326 : : verneed_data, verdef_data, NULL);
3327 : 0 : return true;
3328 : : }
3329 : :
3330 : :
3331 : : /* Print version information. */
3332 : : static void
3333 : 184 : print_verinfo (Ebl *ebl)
3334 : : {
3335 : : /* Find the version information sections. For this we have to
3336 : : search through the section table. */
3337 : 184 : Elf_Scn *scn = NULL;
3338 : :
3339 [ + + ]: 6004 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
3340 : : {
3341 : : /* Handle the section if it is part of the versioning handling. */
3342 : 5820 : GElf_Shdr shdr_mem;
3343 : 5820 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
3344 : :
3345 [ + - ]: 5820 : if (likely (shdr != NULL))
3346 : : {
3347 [ + + ]: 5820 : if (shdr->sh_type == SHT_GNU_verneed)
3348 : 130 : handle_verneed (ebl, scn, shdr);
3349 [ + + ]: 5690 : else if (shdr->sh_type == SHT_GNU_verdef)
3350 : 4 : handle_verdef (ebl, scn, shdr);
3351 [ + + ]: 5686 : else if (shdr->sh_type == SHT_GNU_versym)
3352 : 130 : handle_versym (ebl, scn, shdr);
3353 : : }
3354 : : }
3355 : 184 : }
3356 : :
3357 : :
3358 : : static const char *
3359 : 380 : get_ver_flags (unsigned int flags)
3360 : : {
3361 : 380 : static char buf[32];
3362 : 380 : char *endp;
3363 : :
3364 [ + + ]: 380 : if (flags == 0)
3365 : 376 : return _("none");
3366 : :
3367 [ + - ]: 4 : if (flags & VER_FLG_BASE)
3368 : 4 : endp = stpcpy (buf, "BASE ");
3369 : : else
3370 : : endp = buf;
3371 : :
3372 [ - + ]: 4 : if (flags & VER_FLG_WEAK)
3373 : : {
3374 [ # # ]: 0 : if (endp != buf)
3375 : 0 : endp = stpcpy (endp, "| ");
3376 : :
3377 : 0 : endp = stpcpy (endp, "WEAK ");
3378 : : }
3379 : :
3380 [ - + ]: 4 : if (unlikely (flags & ~(VER_FLG_BASE | VER_FLG_WEAK)))
3381 : : {
3382 : 0 : strncpy (endp, _("| <unknown>"), buf + sizeof (buf) - endp);
3383 : 0 : buf[sizeof (buf) - 1] = '\0';
3384 : : }
3385 : :
3386 : : return buf;
3387 : : }
3388 : :
3389 : :
3390 : : static void
3391 : 130 : handle_verneed (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr)
3392 : : {
3393 : 130 : int class = gelf_getclass (ebl->elf);
3394 : :
3395 : : /* Get the data of the section. */
3396 : 130 : Elf_Data *data = elf_getdata (scn, NULL);
3397 [ - + ]: 130 : if (data == NULL)
3398 : 0 : return;
3399 : :
3400 : : /* Get the section header string table index. */
3401 : 130 : size_t shstrndx;
3402 [ - + ]: 130 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
3403 : 0 : error_exit (0, _("cannot get section header string table index"));
3404 : :
3405 : 130 : GElf_Shdr glink_mem;
3406 : 130 : GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
3407 : : &glink_mem);
3408 [ - + ]: 130 : if (glink == NULL)
3409 : 0 : error_exit (0, _("invalid sh_link value in section %zu"),
3410 : : elf_ndxscn (scn));
3411 : :
3412 : 130 : printf (ngettext ("\
3413 : : \nVersion needs section [%2u] '%s' contains %d entry:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'\n",
3414 : : "\
3415 : : \nVersion needs section [%2u] '%s' contains %d entries:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'\n",
3416 : : shdr->sh_info),
3417 : 130 : (unsigned int) elf_ndxscn (scn),
3418 : 130 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name), shdr->sh_info,
3419 : : class == ELFCLASS32 ? 10 : 18, shdr->sh_addr,
3420 : : shdr->sh_offset,
3421 [ + + ]: 130 : (unsigned int) shdr->sh_link,
3422 : 130 : elf_strptr (ebl->elf, shstrndx, glink->sh_name));
3423 : :
3424 : 130 : unsigned int offset = 0;
3425 [ + - ]: 184 : for (unsigned int cnt = shdr->sh_info; cnt > 0; cnt--)
3426 : : {
3427 : : /* Get the data at the next offset. */
3428 : 184 : GElf_Verneed needmem;
3429 : 184 : GElf_Verneed *need = gelf_getverneed (data, offset, &needmem);
3430 [ + - ]: 184 : if (unlikely (need == NULL))
3431 : : break;
3432 : :
3433 : 552 : printf (_(" %#06x: Version: %hu File: %s Cnt: %hu\n"),
3434 : 184 : offset, (unsigned short int) need->vn_version,
3435 : 184 : elf_strptr (ebl->elf, shdr->sh_link, need->vn_file),
3436 : 184 : (unsigned short int) need->vn_cnt);
3437 : :
3438 : 184 : unsigned int auxoffset = offset + need->vn_aux;
3439 [ + - ]: 356 : for (unsigned int cnt2 = need->vn_cnt; cnt2 > 0; cnt2--)
3440 : : {
3441 : 356 : GElf_Vernaux auxmem;
3442 : 356 : GElf_Vernaux *aux = gelf_getvernaux (data, auxoffset, &auxmem);
3443 [ + - ]: 356 : if (unlikely (aux == NULL))
3444 : : break;
3445 : :
3446 : 356 : printf (_(" %#06x: Name: %s Flags: %s Version: %hu\n"),
3447 : : auxoffset,
3448 : 356 : elf_strptr (ebl->elf, shdr->sh_link, aux->vna_name),
3449 : 356 : get_ver_flags (aux->vna_flags),
3450 : 356 : (unsigned short int) aux->vna_other);
3451 : :
3452 [ + + ]: 356 : if (aux->vna_next == 0)
3453 : : break;
3454 : :
3455 : 172 : auxoffset += aux->vna_next;
3456 : : }
3457 : :
3458 : : /* Find the next offset. */
3459 [ + + ]: 184 : if (need->vn_next == 0)
3460 : : break;
3461 : :
3462 : 54 : offset += need->vn_next;
3463 : : }
3464 : : }
3465 : :
3466 : :
3467 : : static void
3468 : 4 : handle_verdef (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr)
3469 : : {
3470 : : /* Get the data of the section. */
3471 : 4 : Elf_Data *data = elf_getdata (scn, NULL);
3472 [ - + ]: 4 : if (data == NULL)
3473 : 0 : return;
3474 : :
3475 : : /* Get the section header string table index. */
3476 : 4 : size_t shstrndx;
3477 [ - + ]: 4 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
3478 : 0 : error_exit (0, _("cannot get section header string table index"));
3479 : :
3480 : 4 : GElf_Shdr glink_mem;
3481 : 4 : GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
3482 : : &glink_mem);
3483 [ - + ]: 4 : if (glink == NULL)
3484 : 0 : error_exit (0, _("invalid sh_link value in section %zu"),
3485 : : elf_ndxscn (scn));
3486 : :
3487 : 4 : int class = gelf_getclass (ebl->elf);
3488 : 4 : printf (ngettext ("\
3489 : : \nVersion definition section [%2u] '%s' contains %d entry:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'\n",
3490 : : "\
3491 : : \nVersion definition section [%2u] '%s' contains %d entries:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'\n",
3492 : : shdr->sh_info),
3493 : 4 : (unsigned int) elf_ndxscn (scn),
3494 : 4 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
3495 : : shdr->sh_info,
3496 : : class == ELFCLASS32 ? 10 : 18, shdr->sh_addr,
3497 : : shdr->sh_offset,
3498 [ + - ]: 4 : (unsigned int) shdr->sh_link,
3499 : 4 : elf_strptr (ebl->elf, shstrndx, glink->sh_name));
3500 : :
3501 : 4 : unsigned int offset = 0;
3502 [ + - ]: 24 : for (unsigned int cnt = shdr->sh_info; cnt > 0; cnt--)
3503 : : {
3504 : : /* Get the data at the next offset. */
3505 : 24 : GElf_Verdef defmem;
3506 : 24 : GElf_Verdef *def = gelf_getverdef (data, offset, &defmem);
3507 [ + - ]: 24 : if (unlikely (def == NULL))
3508 : : break;
3509 : :
3510 : 24 : unsigned int auxoffset = offset + def->vd_aux;
3511 : 24 : GElf_Verdaux auxmem;
3512 : 24 : GElf_Verdaux *aux = gelf_getverdaux (data, auxoffset, &auxmem);
3513 [ + - ]: 24 : if (unlikely (aux == NULL))
3514 : : break;
3515 : :
3516 : 96 : printf (_("\
3517 : : %#06x: Version: %hd Flags: %s Index: %hd Cnt: %hd Name: %s\n"),
3518 : 24 : offset, def->vd_version,
3519 : 24 : get_ver_flags (def->vd_flags),
3520 : 24 : def->vd_ndx,
3521 : 24 : def->vd_cnt,
3522 : 24 : elf_strptr (ebl->elf, shdr->sh_link, aux->vda_name));
3523 : :
3524 : 24 : auxoffset += aux->vda_next;
3525 [ + + ]: 24 : for (unsigned int cnt2 = 1; cnt2 < def->vd_cnt; ++cnt2)
3526 : : {
3527 : 16 : aux = gelf_getverdaux (data, auxoffset, &auxmem);
3528 [ + - ]: 16 : if (unlikely (aux == NULL))
3529 : : break;
3530 : :
3531 : 16 : printf (_(" %#06x: Parent %d: %s\n"),
3532 : : auxoffset, cnt2,
3533 : 16 : elf_strptr (ebl->elf, shdr->sh_link, aux->vda_name));
3534 : :
3535 [ - + ]: 16 : if (aux->vda_next == 0)
3536 : : break;
3537 : :
3538 : 0 : auxoffset += aux->vda_next;
3539 : : }
3540 : :
3541 : : /* Find the next offset. */
3542 [ + + ]: 24 : if (def->vd_next == 0)
3543 : : break;
3544 : 20 : offset += def->vd_next;
3545 : : }
3546 : : }
3547 : :
3548 : :
3549 : : static void
3550 : 130 : handle_versym (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr)
3551 : : {
3552 : 130 : int class = gelf_getclass (ebl->elf);
3553 : 130 : const char **vername;
3554 : 130 : const char **filename;
3555 : :
3556 : : /* Get the data of the section. */
3557 : 130 : Elf_Data *data = elf_getdata (scn, NULL);
3558 [ + - ]: 130 : if (data == NULL)
3559 : 0 : return;
3560 : :
3561 : : /* Get the section header string table index. */
3562 : 130 : size_t shstrndx;
3563 [ - + ]: 130 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
3564 : 0 : error_exit (0, _("cannot get section header string table index"));
3565 : :
3566 : : /* We have to find the version definition section and extract the
3567 : : version names. */
3568 : : Elf_Scn *defscn = NULL;
3569 : : Elf_Scn *needscn = NULL;
3570 : :
3571 : : Elf_Scn *verscn = NULL;
3572 [ + + ]: 4046 : while ((verscn = elf_nextscn (ebl->elf, verscn)) != NULL)
3573 : : {
3574 : 3916 : GElf_Shdr vershdr_mem;
3575 : 3916 : GElf_Shdr *vershdr = gelf_getshdr (verscn, &vershdr_mem);
3576 : :
3577 [ - + ]: 3916 : if (likely (vershdr != NULL))
3578 : : {
3579 [ + + ]: 3916 : if (vershdr->sh_type == SHT_GNU_verdef)
3580 : : defscn = verscn;
3581 [ + + ]: 3912 : else if (vershdr->sh_type == SHT_GNU_verneed)
3582 : 3916 : needscn = verscn;
3583 : : }
3584 : : }
3585 : :
3586 : 130 : size_t nvername;
3587 [ + - ]: 130 : if (defscn != NULL || needscn != NULL)
3588 : : {
3589 : : /* We have a version information (better should have). Now get
3590 : : the version names. First find the maximum version number. */
3591 : 130 : nvername = 0;
3592 [ + + ]: 130 : if (defscn != NULL)
3593 : : {
3594 : : /* Run through the version definitions and find the highest
3595 : : index. */
3596 : 4 : unsigned int offset = 0;
3597 : 4 : Elf_Data *defdata;
3598 : 4 : GElf_Shdr defshdrmem;
3599 : 4 : GElf_Shdr *defshdr;
3600 : :
3601 : 4 : defdata = elf_getdata (defscn, NULL);
3602 [ + - ]: 4 : if (unlikely (defdata == NULL))
3603 : 0 : return;
3604 : :
3605 : 4 : defshdr = gelf_getshdr (defscn, &defshdrmem);
3606 [ + - ]: 4 : if (unlikely (defshdr == NULL))
3607 : : return;
3608 : :
3609 [ + - ]: 24 : for (unsigned int cnt = 0; cnt < defshdr->sh_info; ++cnt)
3610 : : {
3611 : 24 : GElf_Verdef defmem;
3612 : 24 : GElf_Verdef *def;
3613 : :
3614 : : /* Get the data at the next offset. */
3615 : 24 : def = gelf_getverdef (defdata, offset, &defmem);
3616 [ + - ]: 24 : if (unlikely (def == NULL))
3617 : : break;
3618 : :
3619 : 24 : nvername = MAX (nvername, (size_t) (def->vd_ndx & 0x7fff));
3620 : :
3621 [ + + ]: 24 : if (def->vd_next == 0)
3622 : : break;
3623 : 20 : offset += def->vd_next;
3624 : : }
3625 : : }
3626 [ + - ]: 130 : if (needscn != NULL)
3627 : : {
3628 : 130 : unsigned int offset = 0;
3629 : 130 : Elf_Data *needdata;
3630 : 130 : GElf_Shdr needshdrmem;
3631 : 130 : GElf_Shdr *needshdr;
3632 : :
3633 : 130 : needdata = elf_getdata (needscn, NULL);
3634 [ + - ]: 130 : if (unlikely (needdata == NULL))
3635 : 0 : return;
3636 : :
3637 : 130 : needshdr = gelf_getshdr (needscn, &needshdrmem);
3638 [ + - ]: 130 : if (unlikely (needshdr == NULL))
3639 : : return;
3640 : :
3641 [ + - ]: 184 : for (unsigned int cnt = 0; cnt < needshdr->sh_info; ++cnt)
3642 : : {
3643 : 184 : GElf_Verneed needmem;
3644 : 184 : GElf_Verneed *need;
3645 : 184 : unsigned int auxoffset;
3646 : 184 : int cnt2;
3647 : :
3648 : : /* Get the data at the next offset. */
3649 : 184 : need = gelf_getverneed (needdata, offset, &needmem);
3650 [ + - ]: 184 : if (unlikely (need == NULL))
3651 : : break;
3652 : :
3653 : : /* Run through the auxiliary entries. */
3654 : 184 : auxoffset = offset + need->vn_aux;
3655 [ + - ]: 356 : for (cnt2 = need->vn_cnt; --cnt2 >= 0; )
3656 : : {
3657 : 356 : GElf_Vernaux auxmem;
3658 : 356 : GElf_Vernaux *aux;
3659 : :
3660 : 356 : aux = gelf_getvernaux (needdata, auxoffset, &auxmem);
3661 [ + - ]: 356 : if (unlikely (aux == NULL))
3662 : : break;
3663 : :
3664 : 356 : nvername = MAX (nvername,
3665 : : (size_t) (aux->vna_other & 0x7fff));
3666 : :
3667 [ + + ]: 356 : if (aux->vna_next == 0)
3668 : : break;
3669 : 172 : auxoffset += aux->vna_next;
3670 : : }
3671 : :
3672 [ + + ]: 184 : if (need->vn_next == 0)
3673 : : break;
3674 : 54 : offset += need->vn_next;
3675 : : }
3676 : : }
3677 : :
3678 : : /* This is the number of versions we know about. */
3679 : 130 : ++nvername;
3680 : :
3681 : : /* Allocate the array. */
3682 : 130 : vername = (const char **) alloca (nvername * sizeof (const char *));
3683 [ + + ]: 130 : memset(vername, 0, nvername * sizeof (const char *));
3684 : 130 : filename = (const char **) alloca (nvername * sizeof (const char *));
3685 : 130 : memset(filename, 0, nvername * sizeof (const char *));
3686 : :
3687 : : /* Run through the data structures again and collect the strings. */
3688 [ + + ]: 130 : if (defscn != NULL)
3689 : : {
3690 : : /* Run through the version definitions and find the highest
3691 : : index. */
3692 : 4 : unsigned int offset = 0;
3693 : 4 : Elf_Data *defdata;
3694 : 4 : GElf_Shdr defshdrmem;
3695 : 4 : GElf_Shdr *defshdr;
3696 : :
3697 : 4 : defdata = elf_getdata (defscn, NULL);
3698 [ + - ]: 4 : if (unlikely (defdata == NULL))
3699 : 0 : return;
3700 : :
3701 : 4 : defshdr = gelf_getshdr (defscn, &defshdrmem);
3702 [ + - ]: 4 : if (unlikely (defshdr == NULL))
3703 : : return;
3704 : :
3705 [ + - ]: 24 : for (unsigned int cnt = 0; cnt < defshdr->sh_info; ++cnt)
3706 : : {
3707 : :
3708 : : /* Get the data at the next offset. */
3709 : 24 : GElf_Verdef defmem;
3710 : 24 : GElf_Verdef *def = gelf_getverdef (defdata, offset, &defmem);
3711 [ + - ]: 24 : if (unlikely (def == NULL))
3712 : : break;
3713 : :
3714 : 24 : GElf_Verdaux auxmem;
3715 : 48 : GElf_Verdaux *aux = gelf_getverdaux (defdata,
3716 : 24 : offset + def->vd_aux,
3717 : : &auxmem);
3718 [ + - ]: 24 : if (unlikely (aux == NULL))
3719 : : break;
3720 : :
3721 : 24 : vername[def->vd_ndx & 0x7fff]
3722 : 24 : = elf_strptr (ebl->elf, defshdr->sh_link, aux->vda_name);
3723 : 24 : filename[def->vd_ndx & 0x7fff] = NULL;
3724 : :
3725 [ + + ]: 24 : if (def->vd_next == 0)
3726 : : break;
3727 : 20 : offset += def->vd_next;
3728 : : }
3729 : : }
3730 [ + - ]: 130 : if (needscn != NULL)
3731 : : {
3732 : 130 : unsigned int offset = 0;
3733 : :
3734 : 130 : Elf_Data *needdata = elf_getdata (needscn, NULL);
3735 : 130 : GElf_Shdr needshdrmem;
3736 : 130 : GElf_Shdr *needshdr = gelf_getshdr (needscn, &needshdrmem);
3737 [ - + ]: 130 : if (unlikely (needdata == NULL || needshdr == NULL))
3738 : 0 : return;
3739 : :
3740 [ + - ]: 184 : for (unsigned int cnt = 0; cnt < needshdr->sh_info; ++cnt)
3741 : : {
3742 : : /* Get the data at the next offset. */
3743 : 184 : GElf_Verneed needmem;
3744 : 184 : GElf_Verneed *need = gelf_getverneed (needdata, offset,
3745 : : &needmem);
3746 [ + - ]: 184 : if (unlikely (need == NULL))
3747 : : break;
3748 : :
3749 : : /* Run through the auxiliary entries. */
3750 : 184 : unsigned int auxoffset = offset + need->vn_aux;
3751 [ + - ]: 356 : for (int cnt2 = need->vn_cnt; --cnt2 >= 0; )
3752 : : {
3753 : 356 : GElf_Vernaux auxmem;
3754 : 356 : GElf_Vernaux *aux = gelf_getvernaux (needdata, auxoffset,
3755 : : &auxmem);
3756 [ + - ]: 356 : if (unlikely (aux == NULL))
3757 : : break;
3758 : :
3759 : 356 : vername[aux->vna_other & 0x7fff]
3760 : 356 : = elf_strptr (ebl->elf, needshdr->sh_link, aux->vna_name);
3761 : 356 : filename[aux->vna_other & 0x7fff]
3762 : 356 : = elf_strptr (ebl->elf, needshdr->sh_link, need->vn_file);
3763 : :
3764 [ + + ]: 356 : if (aux->vna_next == 0)
3765 : : break;
3766 : 172 : auxoffset += aux->vna_next;
3767 : : }
3768 : :
3769 [ + + ]: 184 : if (need->vn_next == 0)
3770 : : break;
3771 : 54 : offset += need->vn_next;
3772 : : }
3773 : : }
3774 : : }
3775 : : else
3776 : : {
3777 : : vername = NULL;
3778 : : nvername = 1;
3779 : : filename = NULL;
3780 : : }
3781 : :
3782 : 130 : GElf_Shdr glink_mem;
3783 : 130 : GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
3784 : : &glink_mem);
3785 : 130 : size_t sh_entsize = gelf_fsize (ebl->elf, ELF_T_HALF, 1, EV_CURRENT);
3786 [ - + ]: 130 : if (glink == NULL)
3787 : 0 : error_exit (0, _("invalid sh_link value in section %zu"),
3788 : : elf_ndxscn (scn));
3789 : :
3790 : : /* Print the header. */
3791 : 130 : printf (ngettext ("\
3792 : : \nVersion symbols section [%2u] '%s' contains %d entry:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'",
3793 : : "\
3794 : : \nVersion symbols section [%2u] '%s' contains %d entries:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'",
3795 : : shdr->sh_size / sh_entsize),
3796 : 130 : (unsigned int) elf_ndxscn (scn),
3797 : 130 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
3798 : 130 : (int) (shdr->sh_size / sh_entsize),
3799 : : class == ELFCLASS32 ? 10 : 18, shdr->sh_addr,
3800 : : shdr->sh_offset,
3801 [ + + ]: 130 : (unsigned int) shdr->sh_link,
3802 : 130 : elf_strptr (ebl->elf, shstrndx, glink->sh_name));
3803 : :
3804 : : /* Now we can finally look at the actual contents of this section. */
3805 [ + + ]: 3308 : for (unsigned int cnt = 0; cnt < shdr->sh_size / sh_entsize; ++cnt)
3806 : : {
3807 [ + + ]: 3178 : if (cnt % 2 == 0)
3808 : 1608 : printf ("\n %4d:", cnt);
3809 : :
3810 : 3178 : GElf_Versym symmem;
3811 : 3178 : GElf_Versym *sym = gelf_getversym (data, cnt, &symmem);
3812 [ + - ]: 3178 : if (sym == NULL)
3813 : : break;
3814 : :
3815 [ + + + ]: 3178 : switch (*sym)
3816 : : {
3817 : 238 : ssize_t n;
3818 : 238 : case 0:
3819 : 238 : fputs (_(" 0 *local* "),
3820 : : stdout);
3821 : 238 : break;
3822 : :
3823 : 502 : case 1:
3824 : 502 : fputs (_(" 1 *global* "),
3825 : : stdout);
3826 : 502 : break;
3827 : :
3828 : 2438 : default:
3829 [ + - ]: 4876 : n = printf ("%4d%c%s",
3830 [ + - ]: 2438 : *sym & 0x7fff, *sym & 0x8000 ? 'h' : ' ',
3831 : : (vername != NULL
3832 [ + - ]: 2438 : && (unsigned int) (*sym & 0x7fff) < nvername)
3833 : 2438 : ? vername[*sym & 0x7fff] : "???");
3834 [ + - ]: 2438 : if ((unsigned int) (*sym & 0x7fff) < nvername
3835 [ + - + + ]: 2438 : && filename != NULL && filename[*sym & 0x7fff] != NULL)
3836 : 2134 : n += printf ("(%s)", filename[*sym & 0x7fff]);
3837 : 2438 : printf ("%*s", MAX (0, 33 - (int) n), " ");
3838 : 2438 : break;
3839 : : }
3840 : : }
3841 : 130 : putchar ('\n');
3842 : : }
3843 : :
3844 : :
3845 : : static void
3846 : 130 : print_hash_info (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr, size_t shstrndx,
3847 : : uint_fast32_t maxlength, Elf32_Word nbucket,
3848 : : uint_fast32_t nsyms, uint32_t *lengths, const char *extrastr)
3849 : : {
3850 : 130 : uint32_t *counts = xcalloc (maxlength + 1, sizeof (uint32_t));
3851 : :
3852 [ + + ]: 550 : for (Elf32_Word cnt = 0; cnt < nbucket; ++cnt)
3853 : 420 : ++counts[lengths[cnt]];
3854 : :
3855 : 130 : GElf_Shdr glink_mem;
3856 : 130 : GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf,
3857 : 130 : shdr->sh_link),
3858 : : &glink_mem);
3859 [ - + ]: 130 : if (glink == NULL)
3860 : : {
3861 : 0 : error (0, 0, _("invalid sh_link value in section %zu"),
3862 : : elf_ndxscn (scn));
3863 : 0 : free (counts);
3864 : 0 : return;
3865 : : }
3866 : :
3867 [ + + ]: 260 : printf (ngettext ("\
3868 : : \nHistogram for bucket list length in section [%2u] '%s' (total of %d bucket):\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'\n",
3869 : : "\
3870 : : \nHistogram for bucket list length in section [%2u] '%s' (total of %d buckets):\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'\n",
3871 : : nbucket),
3872 : 130 : (unsigned int) elf_ndxscn (scn),
3873 : 130 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
3874 : : (int) nbucket,
3875 : 130 : gelf_getclass (ebl->elf) == ELFCLASS32 ? 10 : 18,
3876 : : shdr->sh_addr,
3877 : : shdr->sh_offset,
3878 : 130 : (unsigned int) shdr->sh_link,
3879 : 130 : elf_strptr (ebl->elf, shstrndx, glink->sh_name));
3880 : :
3881 [ + - ]: 130 : if (extrastr != NULL)
3882 : 130 : fputs (extrastr, stdout);
3883 : :
3884 [ + - ]: 130 : if (likely (nbucket > 0))
3885 : : {
3886 : 130 : uint64_t success = 0;
3887 : :
3888 : : /* xgettext:no-c-format */
3889 : 130 : fputs (_("\
3890 : : Length Number % of total Coverage\n"), stdout);
3891 : 130 : printf (_(" 0 %6" PRIu32 " %5.1f%%\n"),
3892 : 130 : counts[0], (counts[0] * 100.0) / nbucket);
3893 : :
3894 : 130 : uint64_t nzero_counts = 0;
3895 [ + + ]: 228 : for (Elf32_Word cnt = 1; cnt <= maxlength; ++cnt)
3896 : : {
3897 : 98 : nzero_counts += counts[cnt] * cnt;
3898 : 98 : printf (_("\
3899 : : %7d %6" PRIu32 " %5.1f%% %5.1f%%\n"),
3900 : 98 : (int) cnt, counts[cnt], (counts[cnt] * 100.0) / nbucket,
3901 : 98 : (nzero_counts * 100.0) / nsyms);
3902 : : }
3903 : :
3904 : : Elf32_Word acc = 0;
3905 [ + + ]: 228 : for (Elf32_Word cnt = 1; cnt <= maxlength; ++cnt)
3906 : : {
3907 : 98 : acc += cnt;
3908 : 98 : success += counts[cnt] * acc;
3909 : : }
3910 : :
3911 [ + + ]: 130 : if (nzero_counts > 0)
3912 : 62 : printf (_("\
3913 : : Average number of tests: successful lookup: %f\n\
3914 : : unsuccessful lookup: %f\n"),
3915 : 62 : (double) success / (double) nzero_counts,
3916 : 62 : (double) nzero_counts / (double) nbucket);
3917 : : }
3918 : :
3919 : 130 : free (counts);
3920 : : }
3921 : :
3922 : :
3923 : : /* This function handles the traditional System V-style hash table format. */
3924 : : static void
3925 : 0 : handle_sysv_hash (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr, size_t shstrndx)
3926 : : {
3927 : 0 : Elf_Data *data = elf_getdata (scn, NULL);
3928 [ # # ]: 0 : if (unlikely (data == NULL))
3929 : : {
3930 : 0 : error (0, 0, _("cannot get data for section %d: %s"),
3931 : 0 : (int) elf_ndxscn (scn), elf_errmsg (-1));
3932 : 0 : return;
3933 : : }
3934 : :
3935 [ # # ]: 0 : if (unlikely (data->d_size < 2 * sizeof (Elf32_Word)))
3936 : : {
3937 : 0 : invalid_data:
3938 : 0 : error (0, 0, _("invalid data in sysv.hash section %d"),
3939 : 0 : (int) elf_ndxscn (scn));
3940 : 0 : return;
3941 : : }
3942 : :
3943 : 0 : Elf32_Word nbucket = ((Elf32_Word *) data->d_buf)[0];
3944 : 0 : Elf32_Word nchain = ((Elf32_Word *) data->d_buf)[1];
3945 : :
3946 : 0 : uint64_t used_buf = (2ULL + nchain + nbucket) * sizeof (Elf32_Word);
3947 [ # # ]: 0 : if (used_buf > data->d_size)
3948 : 0 : goto invalid_data;
3949 : :
3950 : 0 : Elf32_Word *bucket = &((Elf32_Word *) data->d_buf)[2];
3951 : 0 : Elf32_Word *chain = &((Elf32_Word *) data->d_buf)[2 + nbucket];
3952 : :
3953 : 0 : uint32_t *lengths = xcalloc (nbucket, sizeof (uint32_t));
3954 : :
3955 : 0 : uint_fast32_t maxlength = 0;
3956 : 0 : uint_fast32_t nsyms = 0;
3957 [ # # ]: 0 : for (Elf32_Word cnt = 0; cnt < nbucket; ++cnt)
3958 : : {
3959 : 0 : Elf32_Word inner = bucket[cnt];
3960 : 0 : Elf32_Word chain_len = 0;
3961 [ # # ]: 0 : while (inner > 0 && inner < nchain)
3962 : : {
3963 : 0 : ++nsyms;
3964 : 0 : ++chain_len;
3965 [ # # ]: 0 : if (chain_len > nchain)
3966 : : {
3967 : 0 : error (0, 0, _("invalid chain in sysv.hash section %d"),
3968 : 0 : (int) elf_ndxscn (scn));
3969 : 0 : free (lengths);
3970 : 0 : return;
3971 : : }
3972 [ # # ]: 0 : if (maxlength < ++lengths[cnt])
3973 : 0 : ++maxlength;
3974 : :
3975 : 0 : inner = chain[inner];
3976 : : }
3977 : : }
3978 : :
3979 : 0 : print_hash_info (ebl, scn, shdr, shstrndx, maxlength, nbucket, nsyms,
3980 : : lengths, NULL);
3981 : :
3982 : 0 : free (lengths);
3983 : : }
3984 : :
3985 : :
3986 : : /* This function handles the incorrect, System V-style hash table
3987 : : format some 64-bit architectures use. */
3988 : : static void
3989 : 0 : handle_sysv_hash64 (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr, size_t shstrndx)
3990 : : {
3991 : 0 : Elf_Data *data = elf_getdata (scn, NULL);
3992 [ # # ]: 0 : if (unlikely (data == NULL))
3993 : : {
3994 : 0 : error (0, 0, _("cannot get data for section %d: %s"),
3995 : 0 : (int) elf_ndxscn (scn), elf_errmsg (-1));
3996 : 0 : return;
3997 : : }
3998 : :
3999 [ # # ]: 0 : if (unlikely (data->d_size < 2 * sizeof (Elf64_Xword)))
4000 : : {
4001 : 0 : invalid_data:
4002 : 0 : error (0, 0, _("invalid data in sysv.hash64 section %d"),
4003 : 0 : (int) elf_ndxscn (scn));
4004 : 0 : return;
4005 : : }
4006 : :
4007 : 0 : Elf64_Xword nbucket = ((Elf64_Xword *) data->d_buf)[0];
4008 : 0 : Elf64_Xword nchain = ((Elf64_Xword *) data->d_buf)[1];
4009 : :
4010 : 0 : uint64_t maxwords = data->d_size / sizeof (Elf64_Xword);
4011 : 0 : if (maxwords < 2
4012 [ # # ]: 0 : || maxwords - 2 < nbucket
4013 [ # # ]: 0 : || maxwords - 2 - nbucket < nchain)
4014 : 0 : goto invalid_data;
4015 : :
4016 : 0 : Elf64_Xword *bucket = &((Elf64_Xword *) data->d_buf)[2];
4017 : 0 : Elf64_Xword *chain = &((Elf64_Xword *) data->d_buf)[2 + nbucket];
4018 : :
4019 : 0 : uint32_t *lengths = xcalloc (nbucket, sizeof (uint32_t));
4020 : :
4021 : 0 : uint_fast32_t maxlength = 0;
4022 : 0 : uint_fast32_t nsyms = 0;
4023 [ # # ]: 0 : for (Elf64_Xword cnt = 0; cnt < nbucket; ++cnt)
4024 : : {
4025 : 0 : Elf64_Xword inner = bucket[cnt];
4026 : 0 : Elf64_Xword chain_len = 0;
4027 [ # # ]: 0 : while (inner > 0 && inner < nchain)
4028 : : {
4029 : 0 : ++nsyms;
4030 : 0 : ++chain_len;
4031 [ # # ]: 0 : if (chain_len > nchain)
4032 : : {
4033 : 0 : error (0, 0, _("invalid chain in sysv.hash64 section %d"),
4034 : 0 : (int) elf_ndxscn (scn));
4035 : 0 : free (lengths);
4036 : 0 : return;
4037 : : }
4038 [ # # ]: 0 : if (maxlength < ++lengths[cnt])
4039 : 0 : ++maxlength;
4040 : :
4041 : 0 : inner = chain[inner];
4042 : : }
4043 : : }
4044 : :
4045 : 0 : print_hash_info (ebl, scn, shdr, shstrndx, maxlength, nbucket, nsyms,
4046 : : lengths, NULL);
4047 : :
4048 : 0 : free (lengths);
4049 : : }
4050 : :
4051 : :
4052 : : /* This function handles the GNU-style hash table format. */
4053 : : static void
4054 : 130 : handle_gnu_hash (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr, size_t shstrndx)
4055 : : {
4056 : 130 : uint32_t *lengths = NULL;
4057 : 130 : Elf_Data *data = elf_getdata (scn, NULL);
4058 [ - + ]: 130 : if (unlikely (data == NULL))
4059 : : {
4060 : 0 : error (0, 0, _("cannot get data for section %d: %s"),
4061 : 0 : (int) elf_ndxscn (scn), elf_errmsg (-1));
4062 : 0 : return;
4063 : : }
4064 : :
4065 [ - + ]: 130 : if (unlikely (data->d_size < 4 * sizeof (Elf32_Word)))
4066 : : {
4067 : 0 : invalid_data:
4068 : 0 : free (lengths);
4069 : 0 : error (0, 0, _("invalid data in gnu.hash section %d"),
4070 : 0 : (int) elf_ndxscn (scn));
4071 : 0 : return;
4072 : : }
4073 : :
4074 : 130 : Elf32_Word nbucket = ((Elf32_Word *) data->d_buf)[0];
4075 : 130 : Elf32_Word symbias = ((Elf32_Word *) data->d_buf)[1];
4076 : :
4077 : : /* Next comes the size of the bitmap. It's measured in words for
4078 : : the architecture. It's 32 bits for 32 bit archs, and 64 bits for
4079 : : 64 bit archs. There is always a bloom filter present, so zero is
4080 : : an invalid value. */
4081 : 130 : Elf32_Word bitmask_words = ((Elf32_Word *) data->d_buf)[2];
4082 [ + + ]: 130 : if (gelf_getclass (ebl->elf) == ELFCLASS64)
4083 : 114 : bitmask_words *= 2;
4084 : :
4085 [ - + ]: 130 : if (bitmask_words == 0)
4086 : 0 : goto invalid_data;
4087 : :
4088 : 130 : Elf32_Word shift = ((Elf32_Word *) data->d_buf)[3];
4089 : :
4090 : : /* Is there still room for the sym chain?
4091 : : Use uint64_t calculation to prevent 32bit overflow. */
4092 : 130 : uint64_t used_buf = (4ULL + bitmask_words + nbucket) * sizeof (Elf32_Word);
4093 : 130 : uint32_t max_nsyms = (data->d_size - used_buf) / sizeof (Elf32_Word);
4094 [ - + ]: 130 : if (used_buf > data->d_size)
4095 : 0 : goto invalid_data;
4096 : :
4097 : 130 : lengths = xcalloc (nbucket, sizeof (uint32_t));
4098 : :
4099 : 130 : Elf32_Word *bitmask = &((Elf32_Word *) data->d_buf)[4];
4100 : 130 : Elf32_Word *bucket = &((Elf32_Word *) data->d_buf)[4 + bitmask_words];
4101 : 130 : Elf32_Word *chain = &((Elf32_Word *) data->d_buf)[4 + bitmask_words
4102 : 130 : + nbucket];
4103 : :
4104 : : /* Compute distribution of chain lengths. */
4105 : 130 : uint_fast32_t maxlength = 0;
4106 : 130 : uint_fast32_t nsyms = 0;
4107 [ + + ]: 550 : for (Elf32_Word cnt = 0; cnt < nbucket; ++cnt)
4108 [ + + ]: 420 : if (bucket[cnt] != 0)
4109 : : {
4110 : 232 : Elf32_Word inner = bucket[cnt] - symbias;
4111 : 414 : do
4112 : : {
4113 : 414 : ++nsyms;
4114 [ + + ]: 414 : if (maxlength < ++lengths[cnt])
4115 : 98 : ++maxlength;
4116 [ - + ]: 414 : if (inner >= max_nsyms)
4117 : 0 : goto invalid_data;
4118 : : }
4119 [ + + ]: 414 : while ((chain[inner++] & 1) == 0);
4120 : : }
4121 : :
4122 : : /* Count bits in bitmask. */
4123 : : uint_fast32_t nbits = 0;
4124 [ + + ]: 446 : for (Elf32_Word cnt = 0; cnt < bitmask_words; ++cnt)
4125 : : {
4126 : 316 : uint_fast32_t word = bitmask[cnt];
4127 : :
4128 : 316 : word = (word & 0x55555555) + ((word >> 1) & 0x55555555);
4129 : 316 : word = (word & 0x33333333) + ((word >> 2) & 0x33333333);
4130 : 316 : word = (word & 0x0f0f0f0f) + ((word >> 4) & 0x0f0f0f0f);
4131 : 316 : word = (word & 0x00ff00ff) + ((word >> 8) & 0x00ff00ff);
4132 : 316 : nbits += (word & 0x0000ffff) + ((word >> 16) & 0x0000ffff);
4133 : : }
4134 : :
4135 : 260 : char *str = xasprintf (_("\
4136 : : Symbol Bias: %u\n\
4137 : : Bitmask Size: %zu bytes %" PRIuFAST32 "%% bits set 2nd hash shift: %u\n"),
4138 : : (unsigned int) symbias,
4139 : : bitmask_words * sizeof (Elf32_Word),
4140 : 130 : ((nbits * 100 + 50)
4141 : 130 : / (uint_fast32_t) (bitmask_words
4142 : : * sizeof (Elf32_Word) * 8)),
4143 : : (unsigned int) shift);
4144 : :
4145 : 130 : print_hash_info (ebl, scn, shdr, shstrndx, maxlength, nbucket, nsyms,
4146 : : lengths, str);
4147 : :
4148 : 130 : free (str);
4149 : 130 : free (lengths);
4150 : : }
4151 : :
4152 : :
4153 : : /* Find the symbol table(s). For this we have to search through the
4154 : : section table. */
4155 : : static void
4156 : 184 : handle_hash (Ebl *ebl)
4157 : : {
4158 : : /* Get the section header string table index. */
4159 : 184 : size_t shstrndx;
4160 [ - + ]: 184 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
4161 : 0 : error_exit (0, _("cannot get section header string table index"));
4162 : :
4163 : : Elf_Scn *scn = NULL;
4164 [ + + ]: 6004 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
4165 : : {
4166 : : /* Handle the section if it is a symbol table. */
4167 : 5820 : GElf_Shdr shdr_mem;
4168 : 5820 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
4169 : :
4170 [ + - ]: 5820 : if (likely (shdr != NULL))
4171 : : {
4172 [ + + ]: 5820 : if ((shdr->sh_type == SHT_HASH || shdr->sh_type == SHT_GNU_HASH)
4173 [ - + ]: 130 : && (shdr->sh_flags & SHF_COMPRESSED) != 0)
4174 : : {
4175 [ # # ]: 0 : if (elf_compress (scn, 0, 0) < 0)
4176 : 0 : printf ("WARNING: %s [%zd]\n",
4177 : : _("Couldn't uncompress section"),
4178 : : elf_ndxscn (scn));
4179 : 0 : shdr = gelf_getshdr (scn, &shdr_mem);
4180 [ # # ]: 0 : if (unlikely (shdr == NULL))
4181 : 0 : error_exit (0, _("cannot get section [%zd] header: %s"),
4182 : : elf_ndxscn (scn), elf_errmsg (-1));
4183 : : }
4184 : :
4185 [ - + ]: 5820 : if (shdr->sh_type == SHT_HASH)
4186 : : {
4187 [ # # ]: 0 : if (ebl_sysvhash_entrysize (ebl) == sizeof (Elf64_Xword))
4188 : 0 : handle_sysv_hash64 (ebl, scn, shdr, shstrndx);
4189 : : else
4190 : 0 : handle_sysv_hash (ebl, scn, shdr, shstrndx);
4191 : : }
4192 [ + + ]: 5820 : else if (shdr->sh_type == SHT_GNU_HASH)
4193 : 130 : handle_gnu_hash (ebl, scn, shdr, shstrndx);
4194 : : }
4195 : : }
4196 : 184 : }
4197 : :
4198 : :
4199 : : static void
4200 : 192 : print_liblist (Ebl *ebl)
4201 : : {
4202 : : /* Find the library list sections. For this we have to search
4203 : : through the section table. */
4204 : 192 : Elf_Scn *scn = NULL;
4205 : :
4206 : : /* Get the section header string table index. */
4207 : 192 : size_t shstrndx;
4208 [ - + ]: 192 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
4209 : 0 : error_exit (0, _("cannot get section header string table index"));
4210 : :
4211 [ + + ]: 6130 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
4212 : : {
4213 : 5938 : GElf_Shdr shdr_mem;
4214 : 5938 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
4215 : :
4216 [ + - - + ]: 5938 : if (shdr != NULL && shdr->sh_type == SHT_GNU_LIBLIST)
4217 : : {
4218 : 0 : size_t sh_entsize = gelf_fsize (ebl->elf, ELF_T_LIB, 1, EV_CURRENT);
4219 : 0 : int nentries = shdr->sh_size / sh_entsize;
4220 : 0 : printf (ngettext ("\
4221 : : \nLibrary list section [%2zu] '%s' at offset %#0" PRIx64 " contains %d entry:\n",
4222 : : "\
4223 : : \nLibrary list section [%2zu] '%s' at offset %#0" PRIx64 " contains %d entries:\n",
4224 : : nentries),
4225 : : elf_ndxscn (scn),
4226 : 0 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
4227 : : shdr->sh_offset,
4228 : : nentries);
4229 : :
4230 : 0 : Elf_Data *data = elf_getdata (scn, NULL);
4231 [ # # ]: 0 : if (data == NULL)
4232 : 0 : return;
4233 : :
4234 : 0 : puts (_("\
4235 : : Library Time Stamp Checksum Version Flags"));
4236 : :
4237 [ # # ]: 0 : for (int cnt = 0; cnt < nentries; ++cnt)
4238 : : {
4239 : 0 : GElf_Lib lib_mem;
4240 : 0 : GElf_Lib *lib = gelf_getlib (data, cnt, &lib_mem);
4241 [ # # ]: 0 : if (unlikely (lib == NULL))
4242 : 0 : continue;
4243 : :
4244 : 0 : time_t t = (time_t) lib->l_time_stamp;
4245 : 0 : struct tm *tm = gmtime (&t);
4246 [ # # ]: 0 : if (unlikely (tm == NULL))
4247 : 0 : continue;
4248 : :
4249 : 0 : printf (" [%2d] %-29s %04u-%02u-%02uT%02u:%02u:%02u %08x %-7u %u\n",
4250 : 0 : cnt, elf_strptr (ebl->elf, shdr->sh_link, lib->l_name),
4251 : 0 : tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
4252 : : tm->tm_hour, tm->tm_min, tm->tm_sec,
4253 : 0 : (unsigned int) lib->l_checksum,
4254 : 0 : (unsigned int) lib->l_version,
4255 : 0 : (unsigned int) lib->l_flags);
4256 : : }
4257 : : }
4258 : : }
4259 : : }
4260 : :
4261 : : static inline size_t
4262 : 16 : left (Elf_Data *data,
4263 : : const unsigned char *p)
4264 : : {
4265 : 16 : return (const unsigned char *) data->d_buf + data->d_size - p;
4266 : : }
4267 : :
4268 : : static void
4269 : 192 : print_attributes (Ebl *ebl, const GElf_Ehdr *ehdr)
4270 : : {
4271 : : /* Find the object attributes sections. For this we have to search
4272 : : through the section table. */
4273 : 192 : Elf_Scn *scn = NULL;
4274 : :
4275 : : /* Get the section header string table index. */
4276 : 192 : size_t shstrndx;
4277 [ - + ]: 192 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
4278 : 0 : error_exit (0, _("cannot get section header string table index"));
4279 : :
4280 [ + + ]: 6130 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
4281 : : {
4282 : 5938 : GElf_Shdr shdr_mem;
4283 : 5938 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
4284 : :
4285 [ + - + + ]: 5938 : if (shdr == NULL || (shdr->sh_type != SHT_GNU_ATTRIBUTES
4286 [ + + ]: 5932 : && (shdr->sh_type != SHT_ARM_ATTRIBUTES
4287 [ - + ]: 2 : || ehdr->e_machine != EM_ARM)
4288 [ + + ]: 5930 : && (shdr->sh_type != SHT_CSKY_ATTRIBUTES
4289 [ + - ]: 2 : || ehdr->e_machine != EM_CSKY)
4290 [ - + ]: 5930 : && (shdr->sh_type != SHT_RISCV_ATTRIBUTES
4291 [ # # ]: 0 : || ehdr->e_machine != EM_RISCV)))
4292 : 5930 : continue;
4293 : :
4294 : 8 : printf (_("\
4295 : : \nObject attributes section [%2zu] '%s' of %" PRIu64
4296 : : " bytes at offset %#0" PRIx64 ":\n"),
4297 : : elf_ndxscn (scn),
4298 : 8 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
4299 : : shdr->sh_size, shdr->sh_offset);
4300 : :
4301 : 8 : Elf_Data *data = elf_rawdata (scn, NULL);
4302 [ + - + - ]: 8 : if (unlikely (data == NULL || data->d_size == 0))
4303 : 0 : return;
4304 : :
4305 : 8 : const unsigned char *p = data->d_buf;
4306 : :
4307 : : /* There is only one 'version', A. */
4308 [ + - ]: 8 : if (unlikely (*p++ != 'A'))
4309 : : return;
4310 : :
4311 : 8 : fputs (_(" Owner Size\n"), stdout);
4312 : :
4313 : : /* Loop over the sections. */
4314 [ + + ]: 16 : while (left (data, p) >= 4)
4315 : : {
4316 : : /* Section length. */
4317 : 8 : uint32_t len;
4318 [ + + ]: 8 : memcpy (&len, p, sizeof len);
4319 : :
4320 [ + + ]: 8 : if (MY_ELFDATA != ehdr->e_ident[EI_DATA])
4321 : 4 : CONVERT (len);
4322 : :
4323 [ + - ]: 8 : if (unlikely (len > left (data, p)))
4324 : : break;
4325 : :
4326 : : /* Section vendor name. */
4327 : 8 : const unsigned char *name = p + sizeof len;
4328 : 8 : p += len;
4329 : :
4330 : 8 : unsigned const char *q = memchr (name, '\0', len);
4331 [ + - ]: 8 : if (unlikely (q == NULL))
4332 : : break;
4333 : 8 : ++q;
4334 : :
4335 : 8 : printf (_(" %-13s %4" PRIu32 "\n"), name, len);
4336 : :
4337 : 16 : bool gnu_vendor = (q - name == sizeof "gnu"
4338 [ + + - + ]: 8 : && !memcmp (name, "gnu", sizeof "gnu"));
4339 : :
4340 : : /* Loop over subsections. */
4341 [ + + ]: 8 : if (shdr->sh_type != SHT_GNU_ATTRIBUTES
4342 [ + - ]: 6 : || gnu_vendor)
4343 [ + + ]: 16 : while (q < p)
4344 : : {
4345 : 8 : const unsigned char *const sub = q;
4346 : :
4347 : 8 : unsigned int subsection_tag;
4348 : 8 : get_uleb128 (subsection_tag, q, p);
4349 [ + - ]: 8 : if (unlikely (q >= p))
4350 : : break;
4351 : :
4352 : 8 : uint32_t subsection_len;
4353 [ + - ]: 8 : if (unlikely (p - sub < (ptrdiff_t) sizeof subsection_len))
4354 : : break;
4355 : :
4356 [ + + ]: 8 : memcpy (&subsection_len, q, sizeof subsection_len);
4357 : :
4358 [ + + ]: 8 : if (MY_ELFDATA != ehdr->e_ident[EI_DATA])
4359 : 4 : CONVERT (subsection_len);
4360 : :
4361 : : /* Don't overflow, ptrdiff_t might be 32bits, but signed. */
4362 [ + - + - ]: 8 : if (unlikely (subsection_len == 0
4363 : : || subsection_len >= (uint32_t) PTRDIFF_MAX
4364 : : || p - sub < (ptrdiff_t) subsection_len))
4365 : : break;
4366 : :
4367 : 8 : const unsigned char *r = q + sizeof subsection_len;
4368 : 8 : q = sub + subsection_len;
4369 : :
4370 [ - + ]: 8 : switch (subsection_tag)
4371 : : {
4372 : 0 : default:
4373 : : /* Unknown subsection, print and skip. */
4374 : 0 : printf (_(" %-4u %12" PRIu32 "\n"),
4375 : : subsection_tag, subsection_len);
4376 : 0 : break;
4377 : :
4378 : 8 : case 1: /* Tag_File */
4379 : 8 : printf (_(" File: %11" PRIu32 "\n"),
4380 : : subsection_len);
4381 : :
4382 [ + + ]: 52 : while (r < q)
4383 : : {
4384 : 44 : unsigned int tag;
4385 : 44 : get_uleb128 (tag, r, q);
4386 [ + - ]: 44 : if (unlikely (r >= q))
4387 : : break;
4388 : :
4389 : : /* GNU style tags have either a uleb128 value,
4390 : : when lowest bit is not set, or a string
4391 : : when the lowest bit is set.
4392 : : "compatibility" (32) is special. It has
4393 : : both a string and a uleb128 value. For
4394 : : non-gnu we assume 6 till 31 only take ints.
4395 : : XXX see arm backend, do we need a separate
4396 : : hook? */
4397 : 44 : uint64_t value = 0;
4398 : 44 : const char *string = NULL;
4399 [ + - + + ]: 44 : if (tag == 32 || (tag & 1) == 0
4400 [ + - + + ]: 16 : || (! gnu_vendor && (tag > 5 && tag < 32)))
4401 : : {
4402 : : // Note r >= q check above.
4403 : 42 : get_uleb128 (value, r, q);
4404 [ + - ]: 42 : if (r > q)
4405 : : break;
4406 : : }
4407 [ + - ]: 44 : if (tag == 32
4408 [ + + ]: 44 : || ((tag & 1) != 0
4409 [ + - ]: 16 : && (gnu_vendor
4410 [ + - ]: 16 : || (! gnu_vendor && tag > 32)))
4411 [ + + + + ]: 44 : || (! gnu_vendor && tag > 3 && tag < 6))
4412 : : {
4413 : 2 : string = (const char *) r;
4414 : 2 : r = memchr (r, '\0', q - r);
4415 [ + - ]: 2 : if (r == NULL)
4416 : : break;
4417 : 2 : ++r;
4418 : : }
4419 : :
4420 : 44 : const char *tag_name = NULL;
4421 : 44 : const char *value_name = NULL;
4422 : 44 : ebl_check_object_attribute (ebl, (const char *) name,
4423 : : tag, value,
4424 : : &tag_name, &value_name);
4425 : :
4426 [ + - ]: 44 : if (tag_name != NULL)
4427 : : {
4428 [ - + ]: 44 : if (tag == 32)
4429 : 0 : printf (_(" %s: %" PRId64 ", %s\n"),
4430 : : tag_name, value, string);
4431 [ + + + + ]: 44 : else if (string == NULL && value_name == NULL)
4432 : 2 : printf (_(" %s: %" PRId64 "\n"),
4433 : : tag_name, value);
4434 : : else
4435 : 42 : printf (_(" %s: %s\n"),
4436 : : tag_name, string ?: value_name);
4437 : : }
4438 : : else
4439 : : {
4440 : : /* For "gnu" vendor 32 "compatibility" has
4441 : : already been handled above. */
4442 [ # # # # ]: 0 : assert (tag != 32
4443 : : || strcmp ((const char *) name, "gnu"));
4444 [ # # ]: 0 : if (string == NULL)
4445 : 0 : printf (_(" %u: %" PRId64 "\n"),
4446 : : tag, value);
4447 : : else
4448 : 0 : printf (_(" %u: %s\n"),
4449 : : tag, string);
4450 : : }
4451 : : }
4452 : : }
4453 : : }
4454 : : }
4455 : : }
4456 : : }
4457 : :
4458 : : /* Returns either the (relocated) data from the Dwarf, or tries to get
4459 : : the "raw" (uncompressed) data from the Elf section. Produces a
4460 : : warning if the data cannot be found (or decompressed). */
4461 : : static Elf_Data *
4462 : 810 : get_debug_elf_data (Dwarf *dbg, Ebl *ebl, int idx, Elf_Scn *scn)
4463 : : {
4464 : : /* We prefer to get the section data from the Dwarf because that
4465 : : might have been relocated already. Note this is subtly wrong if
4466 : : there are multiple sections with the same .debug name. */
4467 [ - + ]: 810 : if (dbg->sectiondata[idx] != NULL)
4468 : : return dbg->sectiondata[idx];
4469 : :
4470 : 0 : GElf_Shdr shdr_mem;
4471 : 0 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
4472 [ # # # # ]: 0 : if (shdr != NULL && (shdr->sh_flags & SHF_COMPRESSED) != 0)
4473 : : {
4474 [ # # ]: 0 : if (elf_compress (scn, 0, 0) < 0)
4475 : : {
4476 : 0 : error (0, 0, "%s [%zd] '%s'\n",
4477 : : _("Couldn't uncompress section"),
4478 : : elf_ndxscn (scn), section_name (ebl, shdr));
4479 : 0 : return NULL;
4480 : : }
4481 : : }
4482 : :
4483 : 0 : Elf_Data *data = elf_getdata (scn, NULL);
4484 [ # # ]: 0 : if (data == NULL)
4485 : 0 : error (0, 0, "%s [%zd] '%s': %s\n",
4486 : : _("Couldn't get data from section"),
4487 : : elf_ndxscn (scn), section_name (ebl, shdr), elf_errmsg (-1));
4488 : :
4489 : 0 : return elf_getdata (scn, NULL);
4490 : : }
4491 : :
4492 : : static void
4493 : 2259466 : print_dwarf_addr (Dwfl_Module *dwflmod,
4494 : : int address_size, Dwarf_Addr address, Dwarf_Addr raw,
4495 : : FILE *out)
4496 : : {
4497 : : /* See if there is a name we can give for this address. */
4498 : 2259466 : GElf_Sym sym;
4499 : 2259466 : GElf_Off off = 0;
4500 [ + + ]: 14150 : const char *name = (print_address_names && ! print_unresolved_addresses)
4501 : 13986 : ? dwfl_module_addrinfo (dwflmod, address, &off, &sym, NULL, NULL, NULL)
4502 [ + + ]: 2259466 : : NULL;
4503 : :
4504 : 2259466 : const char *scn;
4505 [ + + ]: 2259466 : if (print_unresolved_addresses)
4506 : : {
4507 : 168 : address = raw;
4508 : 168 : scn = NULL;
4509 : : }
4510 : : else
4511 : : {
4512 : : /* Relativize the address. */
4513 : 2259298 : int n = dwfl_module_relocations (dwflmod);
4514 [ + + ]: 2259298 : int i = n < 1 ? -1 : dwfl_module_relocate_address (dwflmod, &address);
4515 : :
4516 : : /* In an ET_REL file there is a section name to refer to. */
4517 : 2256546 : scn = (i < 0 ? NULL
4518 [ - + ]: 2256546 : : dwfl_module_relocation_info (dwflmod, i, NULL));
4519 : : }
4520 : :
4521 [ - + ]: 2259466 : if ((name != NULL
4522 : 13578 : ? (off != 0
4523 : : ? (scn != NULL
4524 : : ? (address_size == 0
4525 : 2 : ? fprintf (out, "%s+%#" PRIx64 " <%s+%#" PRIx64 ">",
4526 : : scn, address, name, off)
4527 : 8 : : fprintf (out, "%s+%#0*" PRIx64 " <%s+%#" PRIx64 ">",
4528 : 4 : scn, 2 + address_size * 2, address,
4529 : : name, off))
4530 : : : (address_size == 0
4531 : 364 : ? fprintf (out, "%#" PRIx64 " <%s+%#" PRIx64 ">",
4532 : : address, name, off)
4533 : 2156 : : fprintf (out, "%#0*" PRIx64 " <%s+%#" PRIx64 ">",
4534 : 1078 : 2 + address_size * 2, address,
4535 : : name, off)))
4536 : : : (scn != NULL
4537 : : ? (address_size == 0
4538 : 2 : ? fprintf (out, "%s+%#" PRIx64 " <%s>", scn, address, name)
4539 : 22256 : : fprintf (out, "%s+%#0*" PRIx64 " <%s>",
4540 : 11128 : scn, 2 + address_size * 2, address, name))
4541 : : : (address_size == 0
4542 : 160 : ? fprintf (out, "%#" PRIx64 " <%s>", address, name)
4543 : 1680 : : fprintf (out, "%#0*" PRIx64 " <%s>",
4544 : 840 : 2 + address_size * 2, address, name))))
4545 : : : (scn != NULL
4546 : : ? (address_size == 0
4547 : 825120 : ? fprintf (out, "%s+%#" PRIx64, scn, address)
4548 : 2840580 : : fprintf (out, "%s+%#0*" PRIx64,
4549 : 1420290 : scn, 2 + address_size * 2, address))
4550 : : : (address_size == 0
4551 : 70 : ? fprintf (out, "%#" PRIx64, address)
4552 : 816 : : fprintf (out, "%#0*" PRIx64,
4553 [ + + + + : 4518524 : 2 + address_size * 2, address)))) < 0)
+ + + + +
+ + + + +
+ + + + +
+ + + ]
4554 : 0 : error_exit (0, _("sprintf failure"));
4555 : 2259466 : }
4556 : :
4557 : :
4558 : : static const char *
4559 : 2312416 : dwarf_tag_string (unsigned int tag)
4560 : : {
4561 [ - + + - : 2312416 : switch (tag)
- - - - +
+ + + + -
- - - - -
+ - + - +
- - + + -
+ - - - -
- - - + -
+ - + + +
- - - - -
- + - - +
- - - + -
+ + + + -
- - - - +
+ + + - +
+ + + - -
- ]
4562 : : {
4563 : : #define DWARF_ONE_KNOWN_DW_TAG(NAME, CODE) case CODE: return #NAME;
4564 : 2312416 : DWARF_ALL_KNOWN_DW_TAG
4565 : : #undef DWARF_ONE_KNOWN_DW_TAG
4566 : 0 : default:
4567 : 0 : return NULL;
4568 : : }
4569 : : }
4570 : :
4571 : :
4572 : : static const char *
4573 : 8941410 : dwarf_attr_string (unsigned int attrnum)
4574 : : {
4575 [ + - + - : 8941410 : switch (attrnum)
- - - + -
- + + + -
- - - + +
- + - - +
- + - + -
- - - - -
- - - - -
- - - - -
- - - + -
+ - + - +
- - - + +
- - - + -
+ - + + -
- + + + +
- + + + -
+ - - + -
+ - + + -
+ - - + +
+ + - - -
- - + + +
+ - + - +
- - + - +
+ - + - +
+ - + + +
+ + + + -
- + - - -
+ - + - -
- - + + -
+ - - - -
+ - - - +
+ - + - -
- - + + -
- - - - +
+ - - - -
- - - + ]
4576 : : {
4577 : : #define DWARF_ONE_KNOWN_DW_AT(NAME, CODE) case CODE: return #NAME;
4578 : 8941408 : DWARF_ALL_KNOWN_DW_AT
4579 : : #undef DWARF_ONE_KNOWN_DW_AT
4580 : 0 : default:
4581 : 0 : return NULL;
4582 : : }
4583 : : }
4584 : :
4585 : :
4586 : : static const char *
4587 : 8951828 : dwarf_form_string (unsigned int form)
4588 : : {
4589 [ + + + + : 8951828 : switch (form)
+ - - - -
- + - - +
+ + + + +
+ + + - +
+ - - + -
- + - - +
+ + + + +
- + + - -
- + - + ]
4590 : : {
4591 : : #define DWARF_ONE_KNOWN_DW_FORM(NAME, CODE) case CODE: return #NAME;
4592 : 8951824 : DWARF_ALL_KNOWN_DW_FORM
4593 : : #undef DWARF_ONE_KNOWN_DW_FORM
4594 : 0 : default:
4595 : 0 : return NULL;
4596 : : }
4597 : : }
4598 : :
4599 : :
4600 : : static const char *
4601 : 3740 : dwarf_lang_string (unsigned int lang)
4602 : : {
4603 [ - - + - : 3740 : switch (lang)
- - - + -
- + + - +
- - + - -
- - - - -
- - - - -
+ - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - + - ]
4604 : : {
4605 : : #define DWARF_ONE_KNOWN_DW_LANG(NAME, CODE) case CODE: return #NAME;
4606 : 3738 : DWARF_ALL_KNOWN_DW_LANG
4607 : : #undef DWARF_ONE_KNOWN_DW_LANG
4608 : 2 : default:
4609 : 2 : return NULL;
4610 : : }
4611 : : }
4612 : :
4613 : :
4614 : : static const char *
4615 : 4 : dwarf_lname_string (unsigned int lname)
4616 : : {
4617 [ - - - + : 4 : switch (lname)
- + - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - ]
4618 : : {
4619 : : #define DWARF_ONE_KNOWN_DW_LNAME(NAME, CODE) case CODE: return #NAME;
4620 : 4 : DWARF_ALL_KNOWN_DW_LNAME
4621 : : #undef DWARF_ONE_KNOWN_DW_LNAME
4622 : 0 : default:
4623 : 0 : return NULL;
4624 : : }
4625 : : }
4626 : :
4627 : :
4628 : : static const char *
4629 : 7294 : dwarf_inline_string (unsigned int code)
4630 : : {
4631 : 7294 : static const char *const known[] =
4632 : : {
4633 : : #define DWARF_ONE_KNOWN_DW_INL(NAME, CODE) [CODE] = #NAME,
4634 : : DWARF_ALL_KNOWN_DW_INL
4635 : : #undef DWARF_ONE_KNOWN_DW_INL
4636 : : };
4637 : :
4638 : 7294 : if (likely (code < sizeof (known) / sizeof (known[0])))
4639 : 7294 : return known[code];
4640 : :
4641 : : return NULL;
4642 : : }
4643 : :
4644 : :
4645 : : static const char *
4646 : 59422 : dwarf_encoding_string (unsigned int code)
4647 : : {
4648 : 59422 : static const char *const known[] =
4649 : : {
4650 : : #define DWARF_ONE_KNOWN_DW_ATE(NAME, CODE) [CODE] = #NAME,
4651 : : DWARF_ALL_KNOWN_DW_ATE
4652 : : #undef DWARF_ONE_KNOWN_DW_ATE
4653 : : };
4654 : :
4655 : 59422 : if (likely (code < sizeof (known) / sizeof (known[0])))
4656 : 59422 : return known[code];
4657 : :
4658 : : return NULL;
4659 : : }
4660 : :
4661 : :
4662 : : static const char *
4663 : 0 : dwarf_access_string (unsigned int code)
4664 : : {
4665 : 0 : static const char *const known[] =
4666 : : {
4667 : : #define DWARF_ONE_KNOWN_DW_ACCESS(NAME, CODE) [CODE] = #NAME,
4668 : : DWARF_ALL_KNOWN_DW_ACCESS
4669 : : #undef DWARF_ONE_KNOWN_DW_ACCESS
4670 : : };
4671 : :
4672 : 0 : if (likely (code < sizeof (known) / sizeof (known[0])))
4673 : 0 : return known[code];
4674 : :
4675 : : return NULL;
4676 : : }
4677 : :
4678 : :
4679 : : static const char *
4680 : 0 : dwarf_defaulted_string (unsigned int code)
4681 : : {
4682 : 0 : static const char *const known[] =
4683 : : {
4684 : : #define DWARF_ONE_KNOWN_DW_DEFAULTED(NAME, CODE) [CODE] = #NAME,
4685 : : DWARF_ALL_KNOWN_DW_DEFAULTED
4686 : : #undef DWARF_ONE_KNOWN_DW_DEFAULTED
4687 : : };
4688 : :
4689 : 0 : if (likely (code < sizeof (known) / sizeof (known[0])))
4690 : 0 : return known[code];
4691 : :
4692 : : return NULL;
4693 : : }
4694 : :
4695 : :
4696 : : static const char *
4697 : 0 : dwarf_visibility_string (unsigned int code)
4698 : : {
4699 : 0 : static const char *const known[] =
4700 : : {
4701 : : #define DWARF_ONE_KNOWN_DW_VIS(NAME, CODE) [CODE] = #NAME,
4702 : : DWARF_ALL_KNOWN_DW_VIS
4703 : : #undef DWARF_ONE_KNOWN_DW_VIS
4704 : : };
4705 : :
4706 : 0 : if (likely (code < sizeof (known) / sizeof (known[0])))
4707 : 0 : return known[code];
4708 : :
4709 : : return NULL;
4710 : : }
4711 : :
4712 : :
4713 : : static const char *
4714 : 0 : dwarf_virtuality_string (unsigned int code)
4715 : : {
4716 : 0 : static const char *const known[] =
4717 : : {
4718 : : #define DWARF_ONE_KNOWN_DW_VIRTUALITY(NAME, CODE) [CODE] = #NAME,
4719 : : DWARF_ALL_KNOWN_DW_VIRTUALITY
4720 : : #undef DWARF_ONE_KNOWN_DW_VIRTUALITY
4721 : : };
4722 : :
4723 : 0 : if (likely (code < sizeof (known) / sizeof (known[0])))
4724 : 0 : return known[code];
4725 : :
4726 : : return NULL;
4727 : : }
4728 : :
4729 : :
4730 : : static const char *
4731 : 0 : dwarf_identifier_case_string (unsigned int code)
4732 : : {
4733 : 0 : static const char *const known[] =
4734 : : {
4735 : : #define DWARF_ONE_KNOWN_DW_ID(NAME, CODE) [CODE] = #NAME,
4736 : : DWARF_ALL_KNOWN_DW_ID
4737 : : #undef DWARF_ONE_KNOWN_DW_ID
4738 : : };
4739 : :
4740 : 0 : if (likely (code < sizeof (known) / sizeof (known[0])))
4741 : 0 : return known[code];
4742 : :
4743 : : return NULL;
4744 : : }
4745 : :
4746 : :
4747 : : static const char *
4748 : 0 : dwarf_calling_convention_string (unsigned int code)
4749 : : {
4750 : 0 : static const char *const known[] =
4751 : : {
4752 : : #define DWARF_ONE_KNOWN_DW_CC(NAME, CODE) [CODE] = #NAME,
4753 : : DWARF_ALL_KNOWN_DW_CC
4754 : : #undef DWARF_ONE_KNOWN_DW_CC
4755 : : };
4756 : :
4757 : 0 : if (likely (code < sizeof (known) / sizeof (known[0])))
4758 : 0 : return known[code];
4759 : :
4760 : : return NULL;
4761 : : }
4762 : :
4763 : :
4764 : : static const char *
4765 : 0 : dwarf_ordering_string (unsigned int code)
4766 : : {
4767 : 0 : static const char *const known[] =
4768 : : {
4769 : : #define DWARF_ONE_KNOWN_DW_ORD(NAME, CODE) [CODE] = #NAME,
4770 : : DWARF_ALL_KNOWN_DW_ORD
4771 : : #undef DWARF_ONE_KNOWN_DW_ORD
4772 : : };
4773 : :
4774 : 0 : if (likely (code < sizeof (known) / sizeof (known[0])))
4775 : 0 : return known[code];
4776 : :
4777 : : return NULL;
4778 : : }
4779 : :
4780 : :
4781 : : static const char *
4782 : 16 : dwarf_discr_list_string (unsigned int code)
4783 : : {
4784 : 16 : static const char *const known[] =
4785 : : {
4786 : : #define DWARF_ONE_KNOWN_DW_DSC(NAME, CODE) [CODE] = #NAME,
4787 : : DWARF_ALL_KNOWN_DW_DSC
4788 : : #undef DWARF_ONE_KNOWN_DW_DSC
4789 : : };
4790 : :
4791 : 16 : if (likely (code < sizeof (known) / sizeof (known[0])))
4792 : 16 : return known[code];
4793 : :
4794 : : return NULL;
4795 : : }
4796 : :
4797 : :
4798 : : static const char *
4799 : 1117674 : dwarf_locexpr_opcode_string (unsigned int code)
4800 : : {
4801 : 1117674 : static const char *const known[] =
4802 : : {
4803 : : /* Normally we can't afford building huge table of 64K entries,
4804 : : most of them zero, just because there are a couple defined
4805 : : values at the far end. In case of opcodes, it's OK. */
4806 : : #define DWARF_ONE_KNOWN_DW_OP(NAME, CODE) [CODE] = #NAME,
4807 : : DWARF_ALL_KNOWN_DW_OP
4808 : : #undef DWARF_ONE_KNOWN_DW_OP
4809 : : };
4810 : :
4811 : 1117674 : if (likely (code < sizeof (known) / sizeof (known[0])))
4812 : 1117674 : return known[code];
4813 : :
4814 : : return NULL;
4815 : : }
4816 : :
4817 : :
4818 : : static const char *
4819 : 3486 : dwarf_unit_string (unsigned int type)
4820 : : {
4821 [ - + - - : 3486 : switch (type)
- - + ]
4822 : : {
4823 : : #define DWARF_ONE_KNOWN_DW_UT(NAME, CODE) case CODE: return #NAME;
4824 : 14 : DWARF_ALL_KNOWN_DW_UT
4825 : : #undef DWARF_ONE_KNOWN_DW_UT
4826 : 0 : default:
4827 : 0 : return NULL;
4828 : : }
4829 : : }
4830 : :
4831 : :
4832 : : static const char *
4833 : 148322 : dwarf_range_list_encoding_string (unsigned int kind)
4834 : : {
4835 [ - + + - : 148322 : switch (kind)
+ - - -
+ ]
4836 : : {
4837 : : #define DWARF_ONE_KNOWN_DW_RLE(NAME, CODE) case CODE: return #NAME;
4838 : 131306 : DWARF_ALL_KNOWN_DW_RLE
4839 : : #undef DWARF_ONE_KNOWN_DW_RLE
4840 : 0 : default:
4841 : 0 : return NULL;
4842 : : }
4843 : : }
4844 : :
4845 : :
4846 : : static const char *
4847 : 655664 : dwarf_loc_list_encoding_string (unsigned int kind)
4848 : : {
4849 [ - - + + : 655664 : switch (kind)
- + - + -
- + ]
4850 : : {
4851 : : #define DWARF_ONE_KNOWN_DW_LLE(NAME, CODE) case CODE: return #NAME;
4852 : 610034 : DWARF_ALL_KNOWN_DW_LLE
4853 : : #undef DWARF_ONE_KNOWN_DW_LLE
4854 : : /* DW_LLE_GNU_view_pair is special/incompatible with default codes. */
4855 : 0 : case DW_LLE_GNU_view_pair: return "GNU_view_pair";
4856 : 0 : default:
4857 : 0 : return NULL;
4858 : : }
4859 : : }
4860 : :
4861 : :
4862 : : static const char *
4863 : 10418 : dwarf_line_content_description_string (unsigned int kind)
4864 : : {
4865 [ + + - - : 10418 : switch (kind)
- + ]
4866 : : {
4867 : : #define DWARF_ONE_KNOWN_DW_LNCT(NAME, CODE) case CODE: return #NAME;
4868 : 10410 : DWARF_ALL_KNOWN_DW_LNCT
4869 : : #undef DWARF_ONE_KNOWN_DW_LNCT
4870 : 0 : default:
4871 : 0 : return NULL;
4872 : : }
4873 : : }
4874 : :
4875 : :
4876 : : /* Used by all dwarf_foo_name functions. */
4877 : : static const char *
4878 : 21094020 : string_or_unknown (const char *known, unsigned int code,
4879 : : unsigned int lo_user, unsigned int hi_user,
4880 : : bool print_unknown_num)
4881 : : {
4882 : 21094020 : static char unknown_buf[20];
4883 : :
4884 [ + + ]: 21094020 : if (likely (known != NULL))
4885 : : return known;
4886 : :
4887 [ - + - - ]: 2 : if (lo_user != 0 && code >= lo_user && code <= hi_user)
4888 : : {
4889 : 0 : snprintf (unknown_buf, sizeof unknown_buf, "lo_user+%#x",
4890 : : code - lo_user);
4891 : 0 : return unknown_buf;
4892 : : }
4893 : :
4894 [ + - ]: 2 : if (print_unknown_num)
4895 : : {
4896 : 2 : snprintf (unknown_buf, sizeof unknown_buf, "??? (%#x)", code);
4897 : 2 : return unknown_buf;
4898 : : }
4899 : :
4900 : : return "???";
4901 : : }
4902 : :
4903 : :
4904 : : static const char *
4905 : 2312416 : dwarf_tag_name (unsigned int tag)
4906 : : {
4907 : 2312416 : const char *ret = dwarf_tag_string (tag);
4908 : 2312416 : return string_or_unknown (ret, tag, DW_TAG_lo_user, DW_TAG_hi_user, true);
4909 : : }
4910 : :
4911 : : static const char *
4912 : 8941410 : dwarf_attr_name (unsigned int attr)
4913 : : {
4914 : 8941410 : const char *ret = dwarf_attr_string (attr);
4915 : 8941410 : return string_or_unknown (ret, attr, DW_AT_lo_user, DW_AT_hi_user, true);
4916 : : }
4917 : :
4918 : :
4919 : : static const char *
4920 : 8951828 : dwarf_form_name (unsigned int form)
4921 : : {
4922 : 8951828 : const char *ret = dwarf_form_string (form);
4923 : 8951828 : return string_or_unknown (ret, form, 0, 0, true);
4924 : : }
4925 : :
4926 : :
4927 : : static const char *
4928 : 3736 : dwarf_lang_name (unsigned int lang)
4929 : : {
4930 : 3736 : const char *ret = dwarf_lang_string (lang);
4931 : 3736 : return string_or_unknown (ret, lang, DW_LANG_lo_user, DW_LANG_hi_user, false);
4932 : : }
4933 : :
4934 : :
4935 : : static const char *
4936 : 4 : dwarf_lname_name (unsigned int lname)
4937 : : {
4938 : 4 : const char *ret = dwarf_lname_string (lname);
4939 : 4 : return string_or_unknown (ret, lname, DW_LNAME_lo_user, DW_LNAME_hi_user,
4940 : : false);
4941 : : }
4942 : :
4943 : :
4944 : : static const char *
4945 : 7294 : dwarf_inline_name (unsigned int code)
4946 : : {
4947 [ + - ]: 7294 : const char *ret = dwarf_inline_string (code);
4948 : 7294 : return string_or_unknown (ret, code, 0, 0, false);
4949 : : }
4950 : :
4951 : :
4952 : : static const char *
4953 : 59422 : dwarf_encoding_name (unsigned int code)
4954 : : {
4955 [ + - ]: 59422 : const char *ret = dwarf_encoding_string (code);
4956 : 59422 : return string_or_unknown (ret, code, DW_ATE_lo_user, DW_ATE_hi_user, false);
4957 : : }
4958 : :
4959 : :
4960 : : static const char *
4961 : 0 : dwarf_access_name (unsigned int code)
4962 : : {
4963 [ # # ]: 0 : const char *ret = dwarf_access_string (code);
4964 : 0 : return string_or_unknown (ret, code, 0, 0, false);
4965 : : }
4966 : :
4967 : :
4968 : : static const char *
4969 : 0 : dwarf_defaulted_name (unsigned int code)
4970 : : {
4971 [ # # ]: 0 : const char *ret = dwarf_defaulted_string (code);
4972 : 0 : return string_or_unknown (ret, code, 0, 0, false);
4973 : : }
4974 : :
4975 : :
4976 : : static const char *
4977 : 0 : dwarf_visibility_name (unsigned int code)
4978 : : {
4979 [ # # ]: 0 : const char *ret = dwarf_visibility_string (code);
4980 : 0 : return string_or_unknown (ret, code, 0, 0, false);
4981 : : }
4982 : :
4983 : :
4984 : : static const char *
4985 : 0 : dwarf_virtuality_name (unsigned int code)
4986 : : {
4987 [ # # ]: 0 : const char *ret = dwarf_virtuality_string (code);
4988 : 0 : return string_or_unknown (ret, code, 0, 0, false);
4989 : : }
4990 : :
4991 : :
4992 : : static const char *
4993 : 0 : dwarf_identifier_case_name (unsigned int code)
4994 : : {
4995 [ # # ]: 0 : const char *ret = dwarf_identifier_case_string (code);
4996 : 0 : return string_or_unknown (ret, code, 0, 0, false);
4997 : : }
4998 : :
4999 : :
5000 : : static const char *
5001 : 0 : dwarf_calling_convention_name (unsigned int code)
5002 : : {
5003 [ # # ]: 0 : const char *ret = dwarf_calling_convention_string (code);
5004 : 0 : return string_or_unknown (ret, code, DW_CC_lo_user, DW_CC_hi_user, false);
5005 : : }
5006 : :
5007 : :
5008 : : static const char *
5009 : 0 : dwarf_ordering_name (unsigned int code)
5010 : : {
5011 [ # # ]: 0 : const char *ret = dwarf_ordering_string (code);
5012 : 0 : return string_or_unknown (ret, code, 0, 0, false);
5013 : : }
5014 : :
5015 : :
5016 : : static const char *
5017 : 16 : dwarf_discr_list_name (unsigned int code)
5018 : : {
5019 [ + - ]: 16 : const char *ret = dwarf_discr_list_string (code);
5020 : 16 : return string_or_unknown (ret, code, 0, 0, false);
5021 : : }
5022 : :
5023 : :
5024 : : static const char *
5025 : 3486 : dwarf_unit_name (unsigned int type)
5026 : : {
5027 : 3486 : const char *ret = dwarf_unit_string (type);
5028 : 3486 : return string_or_unknown (ret, type, DW_UT_lo_user, DW_UT_hi_user, true);
5029 : : }
5030 : :
5031 : :
5032 : : static const char *
5033 : 148322 : dwarf_range_list_encoding_name (unsigned int kind)
5034 : : {
5035 : 148322 : const char *ret = dwarf_range_list_encoding_string (kind);
5036 : 148322 : return string_or_unknown (ret, kind, 0, 0, false);
5037 : : }
5038 : :
5039 : :
5040 : : static const char *
5041 : 655664 : dwarf_loc_list_encoding_name (unsigned int kind)
5042 : : {
5043 : 655664 : const char *ret = dwarf_loc_list_encoding_string (kind);
5044 : 655664 : return string_or_unknown (ret, kind, 0, 0, false);
5045 : : }
5046 : :
5047 : :
5048 : : static const char *
5049 : 10418 : dwarf_line_content_description_name (unsigned int kind)
5050 : : {
5051 : 10418 : const char *ret = dwarf_line_content_description_string (kind);
5052 : 10418 : return string_or_unknown (ret, kind, DW_LNCT_lo_user, DW_LNCT_hi_user,
5053 : : false);
5054 : : }
5055 : :
5056 : :
5057 : : static void
5058 : 510 : print_block (size_t n, const void *block, FILE *out)
5059 : : {
5060 [ - + ]: 510 : if (n == 0)
5061 : 0 : fputs (empty_block_str, out);
5062 : : else
5063 : : {
5064 : 510 : fprintf (out, "%zu %s:", n, byte_block_str);
5065 : 510 : const unsigned char *data = block;
5066 : 2250 : do
5067 : 2250 : fprintf (out, " %02x", *data++);
5068 [ + + ]: 2250 : while (--n > 0);
5069 : 510 : fputc ('\n', out);
5070 : : }
5071 : 510 : }
5072 : :
5073 : : static void
5074 : 24 : print_bytes (size_t n, const unsigned char *bytes, FILE *out)
5075 : : {
5076 [ + + ]: 408 : while (n-- > 0)
5077 : : {
5078 : 384 : fprintf (out, "%02x", *bytes++);
5079 [ + + ]: 384 : if (n > 0)
5080 : 360 : fprintf (out, " ");
5081 : : }
5082 : 24 : }
5083 : :
5084 : : static int
5085 : 148 : get_indexed_addr (Dwarf_CU *cu, Dwarf_Word idx, Dwarf_Addr *addr)
5086 : : {
5087 [ - + ]: 148 : if (cu == NULL)
5088 : : return -1;
5089 : :
5090 : 148 : Elf_Data *debug_addr = cu->dbg->sectiondata[IDX_debug_addr];
5091 [ - + ]: 148 : if (debug_addr == NULL)
5092 : : return -1;
5093 : :
5094 : 148 : Dwarf_Off base = __libdw_cu_addr_base (cu);
5095 : 148 : Dwarf_Word off = idx * cu->address_size;
5096 [ - + ]: 148 : if (base > debug_addr->d_size
5097 [ - + ]: 148 : || off > debug_addr->d_size - base
5098 [ - + ]: 148 : || cu->address_size > debug_addr->d_size - base - off)
5099 : : return -1;
5100 : :
5101 : 148 : const unsigned char *addrp = debug_addr->d_buf + base + off;
5102 [ + + ]: 148 : if (cu->address_size == 4)
5103 [ - + ]: 12 : *addr = read_4ubyte_unaligned (cu->dbg, addrp);
5104 : : else
5105 [ - + ]: 136 : *addr = read_8ubyte_unaligned (cu->dbg, addrp);
5106 : :
5107 : : return 0;
5108 : : }
5109 : :
5110 : : static void
5111 : 786070 : print_ops (Dwfl_Module *dwflmod, Dwarf *dbg, int indent, int indentrest,
5112 : : unsigned int vers, unsigned int addrsize, unsigned int offset_size,
5113 : : struct Dwarf_CU *cu, Dwarf_Word len, const unsigned char *data,
5114 : : FILE *out)
5115 : : {
5116 [ + + ]: 786070 : const unsigned int ref_size = vers < 3 ? addrsize : offset_size;
5117 : :
5118 [ - + ]: 786070 : if (len == 0)
5119 : : {
5120 : 0 : fprintf (out, "%*s(empty)\n", indent, "");
5121 : 0 : return;
5122 : : }
5123 : :
5124 : : #define NEED(n) if (len < (Dwarf_Word) (n)) goto invalid
5125 : : #define CONSUME(n) NEED (n); else len -= (n)
5126 : :
5127 : : Dwarf_Word offset = 0;
5128 [ + + ]: 1903744 : while (len-- > 0)
5129 : 1117674 : {
5130 : 1117674 : uint_fast8_t op = *data++;
5131 : :
5132 [ + - ]: 1117674 : const char *op_name = dwarf_locexpr_opcode_string (op);
5133 [ - + ]: 1117674 : if (unlikely (op_name == NULL))
5134 : : {
5135 : 0 : static char buf[20];
5136 [ # # ]: 0 : if (op >= DW_OP_lo_user)
5137 : 0 : snprintf (buf, sizeof buf, "lo_user+%#x", op - DW_OP_lo_user);
5138 : : else
5139 : 0 : snprintf (buf, sizeof buf, "??? (%#x)", op);
5140 : : op_name = buf;
5141 : : }
5142 : :
5143 [ + - + + : 1117674 : switch (op)
+ + + + -
- + + - +
- - + + +
+ + - - -
- + + + ]
5144 : : {
5145 : 26972 : case DW_OP_addr:;
5146 : : /* Address operand. */
5147 : 26972 : Dwarf_Word addr;
5148 [ - + ]: 26972 : NEED (addrsize);
5149 [ + + ]: 26972 : if (addrsize == 4)
5150 [ + + ]: 108 : addr = read_4ubyte_unaligned (dbg, data);
5151 [ + - ]: 26864 : else if (addrsize == 8)
5152 [ + + ]: 26864 : addr = read_8ubyte_unaligned (dbg, data);
5153 : : else
5154 : 0 : goto invalid;
5155 : 26972 : data += addrsize;
5156 : 26972 : CONSUME (addrsize);
5157 : :
5158 : 26972 : fprintf (out, "%*s[%2" PRIuMAX "] %s ",
5159 : : indent, "", (uintmax_t) offset, op_name);
5160 : 26972 : print_dwarf_addr (dwflmod, 0, addr, addr, out);
5161 : 26972 : fprintf (out, "\n");
5162 : :
5163 : 26972 : offset += 1 + addrsize;
5164 : 26972 : break;
5165 : :
5166 : 0 : case DW_OP_call_ref:
5167 : : case DW_OP_GNU_variable_value:
5168 : : /* Offset operand. */
5169 [ # # ]: 0 : if (ref_size != 4 && ref_size != 8)
5170 : 0 : goto invalid; /* Cannot be used in CFA. */
5171 [ # # ]: 0 : NEED (ref_size);
5172 [ # # ]: 0 : if (ref_size == 4)
5173 [ # # ]: 0 : addr = read_4ubyte_unaligned (dbg, data);
5174 : : else
5175 [ # # ]: 0 : addr = read_8ubyte_unaligned (dbg, data);
5176 : 0 : data += ref_size;
5177 : 0 : CONSUME (ref_size);
5178 : : /* addr is a DIE offset, so format it as one. */
5179 : 0 : fprintf (out, "%*s[%2" PRIuMAX "] %s [%6" PRIxMAX "]\n",
5180 : : indent, "", (uintmax_t) offset,
5181 : : op_name, (uintmax_t) addr);
5182 : 0 : offset += 1 + ref_size;
5183 : 0 : break;
5184 : :
5185 : 24688 : case DW_OP_deref_size:
5186 : : case DW_OP_xderef_size:
5187 : : case DW_OP_pick:
5188 : : case DW_OP_const1u:
5189 : : // XXX value might be modified by relocation
5190 [ - + ]: 24688 : NEED (1);
5191 : 49376 : fprintf (out, "%*s[%2" PRIuMAX "] %s %" PRIu8 "\n",
5192 : : indent, "", (uintmax_t) offset,
5193 : 24688 : op_name, *((uint8_t *) data));
5194 : 24688 : ++data;
5195 : 24688 : --len;
5196 : 24688 : offset += 2;
5197 : 24688 : break;
5198 : :
5199 : 3298 : case DW_OP_const2u:
5200 [ - + ]: 3298 : NEED (2);
5201 : : // XXX value might be modified by relocation
5202 : 6596 : fprintf (out, "%*s[%2" PRIuMAX "] %s %" PRIu16 "\n",
5203 : : indent, "", (uintmax_t) offset,
5204 [ - + ]: 3298 : op_name, read_2ubyte_unaligned (dbg, data));
5205 : 3298 : CONSUME (2);
5206 : 3298 : data += 2;
5207 : 3298 : offset += 3;
5208 : 3298 : break;
5209 : :
5210 : 2906 : case DW_OP_const4u:
5211 [ - + ]: 2906 : NEED (4);
5212 : : // XXX value might be modified by relocation
5213 : 2906 : fprintf (out, "%*s[%2" PRIuMAX "] %s %" PRIu32 "\n",
5214 : : indent, "", (uintmax_t) offset,
5215 [ - + ]: 2906 : op_name, read_4ubyte_unaligned (dbg, data));
5216 : 2906 : CONSUME (4);
5217 : 2906 : data += 4;
5218 : 2906 : offset += 5;
5219 : 2906 : break;
5220 : :
5221 : 126 : case DW_OP_const8u:
5222 [ - + ]: 126 : NEED (8);
5223 : : // XXX value might be modified by relocation
5224 : 126 : fprintf (out, "%*s[%2" PRIuMAX "] %s %" PRIu64 "\n",
5225 : : indent, "", (uintmax_t) offset,
5226 [ - + ]: 126 : op_name, (uint64_t) read_8ubyte_unaligned (dbg, data));
5227 : 126 : CONSUME (8);
5228 : 126 : data += 8;
5229 : 126 : offset += 9;
5230 : 126 : break;
5231 : :
5232 : 5152 : case DW_OP_const1s:
5233 [ - + ]: 5152 : NEED (1);
5234 : : // XXX value might be modified by relocation
5235 : 10304 : fprintf (out, "%*s[%2" PRIuMAX "] %s %" PRId8 "\n",
5236 : : indent, "", (uintmax_t) offset,
5237 : 5152 : op_name, *((int8_t *) data));
5238 : 5152 : ++data;
5239 : 5152 : --len;
5240 : 5152 : offset += 2;
5241 : 5152 : break;
5242 : :
5243 : 10 : case DW_OP_const2s:
5244 [ - + ]: 10 : NEED (2);
5245 : : // XXX value might be modified by relocation
5246 : 20 : fprintf (out, "%*s[%2" PRIuMAX "] %s %" PRId16 "\n",
5247 : : indent, "", (uintmax_t) offset,
5248 [ - + ]: 10 : op_name, read_2sbyte_unaligned (dbg, data));
5249 : 10 : CONSUME (2);
5250 : 10 : data += 2;
5251 : 10 : offset += 3;
5252 : 10 : break;
5253 : :
5254 : 0 : case DW_OP_const4s:
5255 [ # # ]: 0 : NEED (4);
5256 : : // XXX value might be modified by relocation
5257 : 0 : fprintf (out, "%*s[%2" PRIuMAX "] %s %" PRId32 "\n",
5258 : : indent, "", (uintmax_t) offset,
5259 [ # # ]: 0 : op_name, read_4sbyte_unaligned (dbg, data));
5260 : 0 : CONSUME (4);
5261 : 0 : data += 4;
5262 : 0 : offset += 5;
5263 : 0 : break;
5264 : :
5265 : 0 : case DW_OP_const8s:
5266 [ # # ]: 0 : NEED (8);
5267 : : // XXX value might be modified by relocation
5268 : 0 : fprintf (out, "%*s[%2" PRIuMAX "] %s %" PRId64 "\n",
5269 : : indent, "", (uintmax_t) offset,
5270 [ # # ]: 0 : op_name, read_8sbyte_unaligned (dbg, data));
5271 : 0 : CONSUME (8);
5272 : 0 : data += 8;
5273 : 0 : offset += 9;
5274 : 0 : break;
5275 : :
5276 : 25492 : case DW_OP_piece:
5277 : : case DW_OP_regx:
5278 : : case DW_OP_plus_uconst:
5279 : 25492 : case DW_OP_constu:;
5280 : 25492 : const unsigned char *start = data;
5281 : 25492 : uint64_t uleb;
5282 [ - + ]: 25492 : NEED (1);
5283 : 25492 : get_uleb128 (uleb, data, data + len);
5284 : 25492 : fprintf (out, "%*s[%2" PRIuMAX "] %s %" PRIu64 "\n",
5285 : : indent, "", (uintmax_t) offset, op_name, uleb);
5286 [ - + ]: 25492 : CONSUME (data - start);
5287 : 25492 : offset += 1 + (data - start);
5288 : 25492 : break;
5289 : :
5290 : 16 : case DW_OP_addrx:
5291 : : case DW_OP_GNU_addr_index:
5292 : : case DW_OP_constx:
5293 : 16 : case DW_OP_GNU_const_index:;
5294 : 16 : start = data;
5295 [ - + ]: 16 : NEED (1);
5296 : 16 : get_uleb128 (uleb, data, data + len);
5297 : 16 : fprintf (out, "%*s[%2" PRIuMAX "] %s [%" PRIu64 "] ",
5298 : : indent, "", (uintmax_t) offset, op_name, uleb);
5299 [ - + ]: 16 : CONSUME (data - start);
5300 : 16 : offset += 1 + (data - start);
5301 [ - + ]: 16 : if (get_indexed_addr (cu, uleb, &addr) != 0)
5302 : 0 : fprintf (out, "???\n");
5303 : : else
5304 : : {
5305 : 16 : print_dwarf_addr (dwflmod, 0, addr, addr, out);
5306 : 16 : fprintf (out, "\n");
5307 : : }
5308 : : break;
5309 : :
5310 : 0 : case DW_OP_bit_piece:
5311 : 0 : start = data;
5312 : 0 : uint64_t uleb2;
5313 [ # # ]: 0 : NEED (1);
5314 : 0 : get_uleb128 (uleb, data, data + len);
5315 : 0 : NEED (1);
5316 : 0 : get_uleb128 (uleb2, data, data + len);
5317 : 0 : fprintf (out, "%*s[%2" PRIuMAX "] %s %" PRIu64 ", %" PRIu64 "\n",
5318 : : indent, "", (uintmax_t) offset, op_name, uleb, uleb2);
5319 [ # # ]: 0 : CONSUME (data - start);
5320 : 0 : offset += 1 + (data - start);
5321 : 0 : break;
5322 : :
5323 : 188766 : case DW_OP_fbreg:
5324 : : case DW_OP_breg0 ... DW_OP_breg31:
5325 : : case DW_OP_consts:
5326 : 188766 : start = data;
5327 : 188766 : int64_t sleb;
5328 [ - + ]: 188766 : NEED (1);
5329 : 188766 : get_sleb128 (sleb, data, data + len);
5330 : 188766 : fprintf (out, "%*s[%2" PRIuMAX "] %s %" PRId64 "\n",
5331 : : indent, "", (uintmax_t) offset, op_name, sleb);
5332 [ - + ]: 188766 : CONSUME (data - start);
5333 : 188766 : offset += 1 + (data - start);
5334 : 188766 : break;
5335 : :
5336 : 0 : case DW_OP_bregx:
5337 : 0 : start = data;
5338 [ # # ]: 0 : NEED (1);
5339 : 0 : get_uleb128 (uleb, data, data + len);
5340 : 0 : NEED (1);
5341 : 0 : get_sleb128 (sleb, data, data + len);
5342 : 0 : fprintf (out, "%*s[%2" PRIuMAX "] %s %" PRIu64 " %" PRId64 "\n",
5343 : : indent, "", (uintmax_t) offset, op_name, uleb, sleb);
5344 [ # # ]: 0 : CONSUME (data - start);
5345 : 0 : offset += 1 + (data - start);
5346 : 0 : break;
5347 : :
5348 : 0 : case DW_OP_call2:
5349 [ # # ]: 0 : NEED (2);
5350 : 0 : fprintf (out, "%*s[%2" PRIuMAX "] %s [%6" PRIx16 "]\n",
5351 : : indent, "", (uintmax_t) offset, op_name,
5352 [ # # ]: 0 : read_2ubyte_unaligned (dbg, data));
5353 : 0 : CONSUME (2);
5354 : 0 : data += 2;
5355 : 0 : offset += 3;
5356 : 0 : break;
5357 : :
5358 : 8 : case DW_OP_call4:
5359 [ - + ]: 8 : NEED (4);
5360 : 8 : fprintf (out, "%*s[%2" PRIuMAX "] %s [%6" PRIx32 "]\n",
5361 : : indent, "", (uintmax_t) offset, op_name,
5362 [ - + ]: 8 : read_4ubyte_unaligned (dbg, data));
5363 : 8 : CONSUME (4);
5364 : 8 : data += 4;
5365 : 8 : offset += 5;
5366 : 8 : break;
5367 : :
5368 : 1120 : case DW_OP_skip:
5369 : : case DW_OP_bra:
5370 [ - + ]: 1120 : NEED (2);
5371 : 2240 : fprintf (out, "%*s[%2" PRIuMAX "] %s %" PRIuMAX "\n",
5372 : : indent, "", (uintmax_t) offset, op_name,
5373 [ - + ]: 1120 : (uintmax_t) (offset + read_2sbyte_unaligned (dbg, data) + 3));
5374 : 1120 : CONSUME (2);
5375 : 1120 : data += 2;
5376 : 1120 : offset += 3;
5377 : 1120 : break;
5378 : :
5379 : 510 : case DW_OP_implicit_value:
5380 : 510 : start = data;
5381 [ - + ]: 510 : NEED (1);
5382 : 510 : get_uleb128 (uleb, data, data + len);
5383 : 510 : fprintf (out, "%*s[%2" PRIuMAX "] %s: ",
5384 : : indent, "", (uintmax_t) offset, op_name);
5385 [ - + ]: 510 : NEED (uleb);
5386 : 510 : print_block (uleb, data, out);
5387 : 510 : data += uleb;
5388 [ - + ]: 510 : CONSUME (data - start);
5389 : 510 : offset += 1 + (data - start);
5390 : 510 : break;
5391 : :
5392 : 5912 : case DW_OP_implicit_pointer:
5393 : : case DW_OP_GNU_implicit_pointer:
5394 : : /* DIE offset operand. */
5395 : 5912 : start = data;
5396 [ - + ]: 5912 : NEED (ref_size);
5397 [ - + ]: 5912 : if (ref_size != 4 && ref_size != 8)
5398 : 0 : goto invalid; /* Cannot be used in CFA. */
5399 [ + - ]: 5912 : if (ref_size == 4)
5400 [ - + ]: 5912 : addr = read_4ubyte_unaligned (dbg, data);
5401 : : else
5402 [ # # ]: 0 : addr = read_8ubyte_unaligned (dbg, data);
5403 : 5912 : data += ref_size;
5404 : : /* Byte offset operand. */
5405 [ - + ]: 5912 : NEED (1);
5406 : 5912 : get_sleb128 (sleb, data, data + len);
5407 : :
5408 : 5912 : fprintf (out, "%*s[%2" PRIuMAX "] %s [%6" PRIxMAX "] %+" PRId64 "\n",
5409 : : indent, "", (intmax_t) offset,
5410 : : op_name, (uintmax_t) addr, sleb);
5411 [ - + ]: 5912 : CONSUME (data - start);
5412 : 5912 : offset += 1 + (data - start);
5413 : 5912 : break;
5414 : :
5415 : 62480 : case DW_OP_entry_value:
5416 : : case DW_OP_GNU_entry_value:
5417 : : /* Size plus expression block. */
5418 : 62480 : start = data;
5419 [ - + ]: 62480 : NEED (1);
5420 : 62480 : get_uleb128 (uleb, data, data + len);
5421 : 62480 : fprintf (out, "%*s[%2" PRIuMAX "] %s:\n",
5422 : : indent, "", (uintmax_t) offset, op_name);
5423 [ - + ]: 62480 : NEED (uleb);
5424 : 62480 : print_ops (dwflmod, dbg, indent + 5, indent + 5, vers,
5425 : : addrsize, offset_size, cu, uleb, data, out);
5426 : 62480 : data += uleb;
5427 [ - + ]: 62480 : CONSUME (data - start);
5428 : 62480 : offset += 1 + (data - start);
5429 : 62480 : break;
5430 : :
5431 : 0 : case DW_OP_const_type:
5432 : : case DW_OP_GNU_const_type:
5433 : : /* uleb128 CU relative DW_TAG_base_type DIE offset, 1-byte
5434 : : unsigned size plus block. */
5435 : 0 : start = data;
5436 [ # # ]: 0 : NEED (1);
5437 : 0 : get_uleb128 (uleb, data, data + len);
5438 [ # # # # ]: 0 : if (! print_unresolved_addresses && cu != NULL)
5439 : 0 : uleb += cu->start;
5440 : 0 : NEED (1);
5441 : 0 : uint8_t usize = *(uint8_t *) data++;
5442 [ # # ]: 0 : NEED (usize);
5443 : 0 : fprintf (out, "%*s[%2" PRIuMAX "] %s [%6" PRIxMAX "] ",
5444 : : indent, "", (uintmax_t) offset, op_name, uleb);
5445 : 0 : print_block (usize, data, out);
5446 : 0 : data += usize;
5447 [ # # ]: 0 : CONSUME (data - start);
5448 : 0 : offset += 1 + (data - start);
5449 : 0 : break;
5450 : :
5451 : 0 : case DW_OP_regval_type:
5452 : : case DW_OP_GNU_regval_type:
5453 : : /* uleb128 register number, uleb128 CU relative
5454 : : DW_TAG_base_type DIE offset. */
5455 : 0 : start = data;
5456 [ # # ]: 0 : NEED (1);
5457 : 0 : get_uleb128 (uleb, data, data + len);
5458 : 0 : NEED (1);
5459 : 0 : get_uleb128 (uleb2, data, data + len);
5460 [ # # # # ]: 0 : if (! print_unresolved_addresses && cu != NULL)
5461 : 0 : uleb2 += cu->start;
5462 : 0 : fprintf (out, "%*s[%2" PRIuMAX "] %s %" PRIu64 " [%6" PRIx64 "]\n",
5463 : : indent, "", (uintmax_t) offset, op_name, uleb, uleb2);
5464 [ # # ]: 0 : CONSUME (data - start);
5465 : 0 : offset += 1 + (data - start);
5466 : 0 : break;
5467 : :
5468 : 0 : case DW_OP_deref_type:
5469 : : case DW_OP_GNU_deref_type:
5470 : : /* 1-byte unsigned size of value, uleb128 CU relative
5471 : : DW_TAG_base_type DIE offset. */
5472 : 0 : start = data;
5473 [ # # ]: 0 : NEED (1);
5474 : 0 : usize = *(uint8_t *) data++;
5475 : 0 : NEED (1);
5476 : 0 : get_uleb128 (uleb, data, data + len);
5477 [ # # # # ]: 0 : if (! print_unresolved_addresses && cu != NULL)
5478 : 0 : uleb += cu->start;
5479 : 0 : fprintf (out, "%*s[%2" PRIuMAX "] %s %" PRIu8 " [%6" PRIxMAX "]\n",
5480 : : indent, "", (uintmax_t) offset,
5481 : : op_name, usize, uleb);
5482 [ # # ]: 0 : CONSUME (data - start);
5483 : 0 : offset += 1 + (data - start);
5484 : 0 : break;
5485 : :
5486 : 0 : case DW_OP_xderef_type:
5487 : : /* 1-byte unsigned size of value, uleb128 base_type DIE offset. */
5488 : 0 : start = data;
5489 [ # # ]: 0 : NEED (1);
5490 : 0 : usize = *(uint8_t *) data++;
5491 : 0 : NEED (1);
5492 : 0 : get_uleb128 (uleb, data, data + len);
5493 : 0 : fprintf (out, "%*s[%4" PRIuMAX "] %s %" PRIu8 " [%6" PRIxMAX "]\n",
5494 : : indent, "", (uintmax_t) offset,
5495 : : op_name, usize, uleb);
5496 [ # # ]: 0 : CONSUME (data - start);
5497 : 0 : offset += 1 + (data - start);
5498 : 0 : break;
5499 : :
5500 : 786 : case DW_OP_convert:
5501 : : case DW_OP_GNU_convert:
5502 : : case DW_OP_reinterpret:
5503 : : case DW_OP_GNU_reinterpret:
5504 : : /* uleb128 CU relative offset to DW_TAG_base_type, or zero
5505 : : for conversion to untyped. */
5506 : 786 : start = data;
5507 [ - + ]: 786 : NEED (1);
5508 : 786 : get_uleb128 (uleb, data, data + len);
5509 [ + + + - : 786 : if (uleb != 0 && ! print_unresolved_addresses && cu != NULL)
+ - ]
5510 : 524 : uleb += cu->start;
5511 : 786 : fprintf (out, "%*s[%2" PRIuMAX "] %s [%6" PRIxMAX "]\n",
5512 : : indent, "", (uintmax_t) offset, op_name, uleb);
5513 [ - + ]: 786 : CONSUME (data - start);
5514 : 786 : offset += 1 + (data - start);
5515 : 786 : break;
5516 : :
5517 : 8 : case DW_OP_GNU_parameter_ref:
5518 : : /* 4 byte CU relative reference to the abstract optimized away
5519 : : DW_TAG_formal_parameter. */
5520 [ - + ]: 8 : NEED (4);
5521 [ - + ]: 8 : uintmax_t param_off = (uintmax_t) read_4ubyte_unaligned (dbg, data);
5522 [ + - + - ]: 8 : if (! print_unresolved_addresses && cu != NULL)
5523 : 8 : param_off += cu->start;
5524 : 8 : fprintf (out, "%*s[%2" PRIuMAX "] %s [%6" PRIxMAX "]\n",
5525 : : indent, "", (uintmax_t) offset, op_name, param_off);
5526 : 8 : CONSUME (4);
5527 : 8 : data += 4;
5528 : 8 : offset += 5;
5529 : 8 : break;
5530 : :
5531 : : default:
5532 : : /* No Operand. */
5533 : 769424 : fprintf (out, "%*s[%2" PRIuMAX "] %s\n",
5534 : : indent, "", (uintmax_t) offset, op_name);
5535 : 769424 : ++offset;
5536 : 769424 : break;
5537 : : }
5538 : :
5539 : 1117674 : indent = indentrest;
5540 : 1117674 : continue;
5541 : :
5542 : 0 : invalid:
5543 : 0 : fprintf (out, _("%*s[%2" PRIuMAX "] %s <TRUNCATED>\n"),
5544 : : indent, "", (uintmax_t) offset, op_name);
5545 : 0 : break;
5546 : : }
5547 : : }
5548 : :
5549 : :
5550 : : /* Turn the addresses into file offsets by using the phdrs. */
5551 : : static void
5552 : 2 : find_offsets(Elf *elf, GElf_Addr main_bias, size_t n,
5553 : : GElf_Addr addrs[n], GElf_Off offs[n])
5554 : : {
5555 : 2 : size_t unsolved = n;
5556 [ + + ]: 18 : for (size_t i = 0; i < phnum; ++i) {
5557 : 16 : GElf_Phdr phdr_mem;
5558 : 16 : GElf_Phdr *phdr = gelf_getphdr(elf, i, &phdr_mem);
5559 [ + - + + : 16 : if (phdr != NULL && phdr->p_type == PT_LOAD && phdr->p_memsz > 0)
+ - ]
5560 [ + + ]: 46 : for (size_t j = 0; j < n; ++j)
5561 [ + + + + ]: 44 : if (offs[j] == 0 && addrs[j] >= phdr->p_vaddr + main_bias &&
5562 [ + - ]: 22 : addrs[j] - (phdr->p_vaddr + main_bias) < phdr->p_filesz) {
5563 : 22 : offs[j] = addrs[j] - (phdr->p_vaddr + main_bias) + phdr->p_offset;
5564 [ + + ]: 22 : if (--unsolved == 0)
5565 : : break;
5566 : : }
5567 : : }
5568 : 2 : }
5569 : :
5570 : : /* The dynamic segment (type PT_DYNAMIC), contains the .dynamic section.
5571 : : And .dynamic section contains an array of the dynamic structures.
5572 : : We use the array to get:
5573 : : DT_STRTAB: the address of the string table
5574 : : DT_SYMTAB: the address of the symbol table
5575 : : DT_STRSZ: the size, in bytes, of the string table
5576 : : ... */
5577 : : static void
5578 : 2 : get_dynscn_addrs(Elf *elf, GElf_Phdr *phdr, GElf_Addr addrs[i_max])
5579 : : {
5580 : 2 : Elf_Data *data = elf_getdata_rawchunk(
5581 : 2 : elf, phdr->p_offset, phdr->p_filesz, ELF_T_DYN);
5582 : :
5583 : 2 : int dyn_idx = 0;
5584 : 44 : for (;; ++dyn_idx) {
5585 : 46 : GElf_Dyn dyn_mem;
5586 : 46 : GElf_Dyn *dyn = gelf_getdyn(data, dyn_idx, &dyn_mem);
5587 : : /* DT_NULL Marks end of dynamic section. */
5588 [ + - + + ]: 46 : if (dyn == NULL || dyn->d_tag == DT_NULL)
5589 : : break;
5590 : :
5591 [ + - + + : 44 : switch (dyn->d_tag) {
+ + + + +
+ - + ]
5592 : 2 : case DT_SYMTAB:
5593 : 2 : addrs[i_symtab] = dyn->d_un.d_ptr;
5594 : 2 : break;
5595 : :
5596 : 0 : case DT_HASH:
5597 : 0 : addrs[i_hash] = dyn->d_un.d_ptr;
5598 : 0 : break;
5599 : :
5600 : 2 : case DT_GNU_HASH:
5601 : 2 : addrs[i_gnu_hash] = dyn->d_un.d_ptr;
5602 : 2 : break;
5603 : :
5604 : 2 : case DT_STRTAB:
5605 : 2 : addrs[i_strtab] = dyn->d_un.d_ptr;
5606 : 2 : break;
5607 : :
5608 : 2 : case DT_VERSYM:
5609 : 2 : addrs[i_versym] = dyn->d_un.d_ptr;
5610 : 2 : break;
5611 : :
5612 : 2 : case DT_VERDEF:
5613 : 2 : addrs[i_verdef] = dyn->d_un.d_ptr;
5614 : 2 : break;
5615 : :
5616 : 2 : case DT_VERDEFNUM:
5617 : 2 : addrs[i_verdefnum] = dyn->d_un.d_val;
5618 : 2 : break;
5619 : :
5620 : 2 : case DT_VERNEED:
5621 : 2 : addrs[i_verneed] = dyn->d_un.d_ptr;
5622 : 2 : break;
5623 : :
5624 : 2 : case DT_VERNEEDNUM:
5625 : 2 : addrs[i_verneednum] = dyn->d_un.d_val;
5626 : 2 : break;
5627 : :
5628 : 2 : case DT_STRSZ:
5629 : 2 : addrs[i_strsz] = dyn->d_un.d_val;
5630 : 2 : break;
5631 : :
5632 : 0 : case DT_SYMTAB_SHNDX:
5633 : 0 : addrs[i_symtab_shndx] = dyn->d_un.d_ptr;
5634 : 0 : break;
5635 : : }
5636 : : }
5637 : 2 : }
5638 : :
5639 : :
5640 : : /* Use dynamic segment to get data for the string table section. */
5641 : : static Elf_Data *
5642 : 2 : get_dynscn_strtab(Elf *elf, GElf_Phdr *phdr)
5643 : : {
5644 : 2 : Elf_Data *strtab_data;
5645 : 2 : GElf_Addr addrs[i_max] = {0,};
5646 : 2 : GElf_Off offs[i_max] = {0,};
5647 : 2 : get_dynscn_addrs(elf, phdr, addrs);
5648 : 2 : find_offsets(elf, 0, i_max, addrs, offs);
5649 : 4 : strtab_data = elf_getdata_rawchunk(
5650 : 2 : elf, offs[i_strtab], addrs[i_strsz], ELF_T_BYTE);
5651 : 2 : return strtab_data;
5652 : : }
5653 : :
5654 : :
5655 : : struct listptr
5656 : : {
5657 : : Dwarf_Off offset:(64 - 3);
5658 : : bool addr64:1;
5659 : : bool dwarf64:1;
5660 : : bool warned:1;
5661 : : struct Dwarf_CU *cu;
5662 : : unsigned int attr;
5663 : : };
5664 : :
5665 : : #define listptr_offset_size(p) ((p)->dwarf64 ? 8 : 4)
5666 : : #define listptr_address_size(p) ((p)->addr64 ? 8 : 4)
5667 : :
5668 : : static Dwarf_Addr
5669 : 5342 : cudie_base (Dwarf_Die *cudie)
5670 : : {
5671 : 5342 : Dwarf_Addr base;
5672 : : /* Find the base address of the compilation unit. It will normally
5673 : : be specified by DW_AT_low_pc. In DWARF-3 draft 4, the base
5674 : : address could be overridden by DW_AT_entry_pc. It's been
5675 : : removed, but GCC emits DW_AT_entry_pc and not DW_AT_lowpc for
5676 : : compilation units with discontinuous ranges. */
5677 [ - + ]: 5342 : if (unlikely (dwarf_lowpc (cudie, &base) != 0))
5678 : : {
5679 : 0 : Dwarf_Attribute attr_mem;
5680 [ # # ]: 0 : if (dwarf_formaddr (dwarf_attr (cudie, DW_AT_entry_pc, &attr_mem),
5681 : : &base) != 0)
5682 : 0 : base = 0;
5683 : : }
5684 : 5342 : return base;
5685 : : }
5686 : :
5687 : : static Dwarf_Addr
5688 : 5342 : listptr_base (struct listptr *p)
5689 : : {
5690 : 5342 : Dwarf_Die cu = CUDIE (p->cu);
5691 : 5342 : return cudie_base (&cu);
5692 : : }
5693 : :
5694 : : /* To store the name used in compare_listptr */
5695 : : static _Thread_local const char *sort_listptr_name;
5696 : :
5697 : : static int
5698 : 1790640 : compare_listptr (const void *a, const void *b)
5699 : : {
5700 : 1790640 : const char *name = sort_listptr_name;
5701 : 1790640 : struct listptr *p1 = (void *) a;
5702 : 1790640 : struct listptr *p2 = (void *) b;
5703 : :
5704 [ + + ]: 1790640 : if (p1->offset < p2->offset)
5705 : : return -1;
5706 [ + + ]: 175570 : if (p1->offset > p2->offset)
5707 : : return 1;
5708 : :
5709 [ + - - + ]: 8174 : if (!p1->warned && !p2->warned)
5710 : : {
5711 [ - + ]: 8174 : if (p1->addr64 != p2->addr64)
5712 : : {
5713 : 0 : p1->warned = p2->warned = true;
5714 : 0 : error (0, 0,
5715 : 0 : _("%s %#" PRIx64 " used with different address sizes"),
5716 : : name, (uint64_t) p1->offset);
5717 : : }
5718 [ - + ]: 8174 : if (p1->dwarf64 != p2->dwarf64)
5719 : : {
5720 : 0 : p1->warned = p2->warned = true;
5721 : 0 : error (0, 0,
5722 : 0 : _("%s %#" PRIx64 " used with different offset sizes"),
5723 : 0 : name, (uint64_t) p1->offset);
5724 : : }
5725 : :
5726 : : /* Note: CUs can share the same listptr_base. So we don't check
5727 : : (listptr_base (p1) != listptr_base (p2)) */
5728 : :
5729 [ + - ]: 8174 : if (p1->attr != p2 ->attr)
5730 : : {
5731 : 0 : p1->warned = p2->warned = true;
5732 : 0 : error (0, 0,
5733 : 0 : _("%s %#" PRIx64
5734 : : " used with different attribute %s and %s"),
5735 : 0 : name, (uint64_t) p1->offset, dwarf_attr_name (p1->attr),
5736 : : dwarf_attr_name (p2->attr));
5737 : : }
5738 : : }
5739 : :
5740 : : return 0;
5741 : : }
5742 : :
5743 : : struct listptr_table
5744 : : {
5745 : : size_t n;
5746 : : size_t alloc;
5747 : : struct listptr *table;
5748 : : };
5749 : :
5750 : : static struct listptr_table known_locsptr;
5751 : : static struct listptr_table known_loclistsptr;
5752 : : static struct listptr_table known_rangelistptr;
5753 : : static struct listptr_table known_rnglistptr;
5754 : : static struct listptr_table known_addrbases;
5755 : : static struct listptr_table known_stroffbases;
5756 : :
5757 : : static void
5758 : 502 : reset_listptr (struct listptr_table *table)
5759 : : {
5760 : 502 : free (table->table);
5761 : 502 : table->table = NULL;
5762 : 502 : table->n = table->alloc = 0;
5763 : : }
5764 : :
5765 : : /* Returns false if offset doesn't fit. See struct listptr. */
5766 : : static bool
5767 : 257896 : notice_listptr (enum section_e section, struct listptr_table *table,
5768 : : uint_fast8_t address_size, uint_fast8_t offset_size,
5769 : : struct Dwarf_CU *cu, Dwarf_Off offset, unsigned int attr)
5770 : : {
5771 [ + + ]: 257896 : if (print_debug_sections & section)
5772 : : {
5773 [ + + ]: 257644 : if (table->n == table->alloc)
5774 : : {
5775 [ + + ]: 410 : if (table->alloc == 0)
5776 : 174 : table->alloc = 128;
5777 : : else
5778 : 236 : table->alloc *= 2;
5779 : 410 : table->table = xrealloc (table->table,
5780 : 410 : table->alloc * sizeof table->table[0]);
5781 : : }
5782 : :
5783 : 257644 : struct listptr *p = &table->table[table->n++];
5784 : :
5785 : 257644 : *p = (struct listptr)
5786 : : {
5787 : 257644 : .addr64 = address_size == 8,
5788 : 257644 : .dwarf64 = offset_size == 8,
5789 : : .offset = offset,
5790 : : .cu = cu,
5791 : : .attr = attr
5792 : : };
5793 : :
5794 [ + - ]: 257644 : if (p->offset != offset)
5795 : : {
5796 : 0 : table->n--;
5797 : 0 : return false;
5798 : : }
5799 : : }
5800 : : return true;
5801 : : }
5802 : :
5803 : : static void
5804 : 182 : sort_listptr (struct listptr_table *table, const char *name)
5805 : : {
5806 [ + + ]: 182 : if (table->n > 0)
5807 : : {
5808 : 174 : sort_listptr_name = name;
5809 : 174 : qsort (table->table, table->n, sizeof table->table[0],
5810 : : &compare_listptr);
5811 : : }
5812 : 182 : }
5813 : :
5814 : : static bool
5815 : 208 : skip_listptr_hole (struct listptr_table *table, size_t *idxp,
5816 : : uint_fast8_t *address_sizep, uint_fast8_t *offset_sizep,
5817 : : Dwarf_Addr *base, struct Dwarf_CU **cu, ptrdiff_t offset,
5818 : : unsigned char **readp, unsigned char *endp,
5819 : : unsigned int *attr)
5820 : : {
5821 [ - + ]: 208 : if (table->n == 0)
5822 : : return false;
5823 : :
5824 [ + - + + ]: 336 : while (*idxp < table->n && table->table[*idxp].offset < (Dwarf_Off) offset)
5825 : 128 : ++*idxp;
5826 : :
5827 : 208 : struct listptr *p = &table->table[*idxp];
5828 : :
5829 [ + - ]: 208 : if (*idxp == table->n
5830 [ - + ]: 208 : || p->offset >= (Dwarf_Off) (endp - *readp + offset))
5831 : : {
5832 : 0 : *readp = endp;
5833 : 0 : printf (_(" [%6tx] <UNUSED GARBAGE IN REST OF SECTION>\n"),
5834 : : offset);
5835 : 0 : return true;
5836 : : }
5837 : :
5838 [ - + ]: 208 : if (p->offset != (Dwarf_Off) offset)
5839 : : {
5840 : 0 : *readp += p->offset - offset;
5841 : 0 : printf (_(" [%6tx] <UNUSED GARBAGE> ... %" PRIu64 " bytes ...\n"),
5842 : : offset, (Dwarf_Off) p->offset - offset);
5843 : 0 : return true;
5844 : : }
5845 : :
5846 [ + - ]: 208 : if (address_sizep != NULL)
5847 [ + + ]: 224 : *address_sizep = listptr_address_size (p);
5848 [ + + ]: 208 : if (offset_sizep != NULL)
5849 [ + - ]: 296 : *offset_sizep = listptr_offset_size (p);
5850 [ + - ]: 208 : if (base != NULL)
5851 : 208 : *base = listptr_base (p);
5852 [ + - ]: 208 : if (cu != NULL)
5853 : 208 : *cu = p->cu;
5854 [ + + ]: 208 : if (attr != NULL)
5855 : 148 : *attr = p->attr;
5856 : :
5857 : : return false;
5858 : : }
5859 : :
5860 : : static Dwarf_Off
5861 : 109918 : next_listptr_offset (struct listptr_table *table, size_t *idxp, Dwarf_Off off)
5862 : : {
5863 : : /* Note that multiple attributes could in theory point to the same loclist
5864 : : offset, so make sure we pick one that is bigger than the current one.
5865 : : The table is sorted on offset. */
5866 [ + - ]: 109918 : if (*idxp < table->n)
5867 : : {
5868 [ + - ]: 218660 : while (++*idxp < table->n)
5869 : : {
5870 : 218660 : Dwarf_Off next = table->table[*idxp].offset;
5871 [ + + ]: 218660 : if (next > off)
5872 : : return next;
5873 : : }
5874 : : }
5875 : : return 0;
5876 : : }
5877 : :
5878 : : /* Returns the listptr associated with the given index, or NULL. */
5879 : : static struct listptr *
5880 : 1531880 : get_listptr (struct listptr_table *table, size_t idx)
5881 : : {
5882 : 1531880 : if (idx >= table->n)
5883 : : return NULL;
5884 : 36 : return &table->table[idx];
5885 : : }
5886 : :
5887 : : /* Returns the next index, base address and CU associated with the
5888 : : list unit offsets. If there is none false is returned, otherwise
5889 : : true. Assumes the table has been sorted. */
5890 : : static bool
5891 : 5134 : listptr_cu (struct listptr_table *table, size_t *idxp,
5892 : : Dwarf_Off start, Dwarf_Off end,
5893 : : Dwarf_Addr *base, struct Dwarf_CU **cu)
5894 : : {
5895 : 5134 : while (*idxp < table->n
5896 [ + - + + ]: 41092 : && table->table[*idxp].offset < start)
5897 : 35958 : ++*idxp;
5898 : :
5899 [ + - ]: 5134 : if (*idxp < table->n
5900 [ + - ]: 5134 : && table->table[*idxp].offset >= start
5901 [ + - ]: 5134 : && table->table[*idxp].offset < end)
5902 : : {
5903 : 5134 : struct listptr *p = &table->table[*idxp];
5904 : 5134 : *base = listptr_base (p);
5905 : 5134 : *cu = p->cu;
5906 : 5134 : return true;
5907 : : }
5908 : :
5909 : : return false;
5910 : : }
5911 : :
5912 : : /* Returns the next index with the current CU for the given attribute.
5913 : : If there is none false is returned, otherwise true. Assumes the
5914 : : table has been sorted. */
5915 : : static bool
5916 : 765582 : listptr_attr (struct listptr_table *table, size_t idxp,
5917 : : Dwarf_Off offset, unsigned int attr)
5918 : : {
5919 : 1531832 : struct listptr *listptr;
5920 : 1531832 : do
5921 : : {
5922 [ + + ]: 1531832 : listptr = get_listptr (table, idxp);
5923 [ + - ]: 1531592 : if (listptr == NULL)
5924 : : return false;
5925 : :
5926 [ + + + + ]: 1531592 : if (listptr->offset == offset && listptr->attr == attr)
5927 : : return true;
5928 : :
5929 : 1421674 : idxp++;
5930 : : }
5931 [ + + ]: 1421674 : while (listptr->offset <= offset);
5932 : :
5933 : : return false;
5934 : : }
5935 : :
5936 : : static void
5937 : 88 : print_debug_abbrev_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
5938 : : Ebl *ebl, GElf_Ehdr *ehdr __attribute__ ((unused)),
5939 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg,
5940 : : FILE *out)
5941 : : {
5942 : 88 : Elf_Data *elf_data = get_debug_elf_data (dbg, ebl, IDX_debug_abbrev, scn);
5943 [ + - ]: 88 : if (elf_data == NULL)
5944 : : return;
5945 : :
5946 : 88 : const size_t sh_size = elf_data->d_size;
5947 : :
5948 : 88 : fprintf (out, _("\nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"
5949 : : " [ Code]\n"),
5950 : : elf_ndxscn (scn), section_name (ebl, shdr),
5951 : 88 : (uint64_t) shdr->sh_offset);
5952 : :
5953 : 88 : Dwarf_Off offset = 0;
5954 [ + + ]: 3682 : while (offset < sh_size)
5955 : : {
5956 : 3594 : fprintf (out, _("\nAbbreviation section at offset %" PRIu64 ":\n"),
5957 : : offset);
5958 : :
5959 : 332758 : while (1)
5960 : 164582 : {
5961 : 168176 : size_t length;
5962 : 168176 : Dwarf_Abbrev abbrev;
5963 : :
5964 : 168176 : int res = dwarf_offabbrev (dbg, offset, &length, &abbrev);
5965 [ + + ]: 168176 : if (res != 0)
5966 : : {
5967 [ - + ]: 3594 : if (unlikely (res < 0))
5968 : : {
5969 : 0 : fprintf (out, _("\
5970 : : *** error while reading abbreviation: %s\n"),
5971 : : dwarf_errmsg (-1));
5972 : 0 : return;
5973 : : }
5974 : :
5975 : : /* This is the NUL byte at the end of the section. */
5976 : 3594 : ++offset;
5977 : 3594 : break;
5978 : : }
5979 : :
5980 : : /* We know these calls can never fail. */
5981 : 164582 : unsigned int code = dwarf_getabbrevcode (&abbrev);
5982 : 164582 : unsigned int tag = dwarf_getabbrevtag (&abbrev);
5983 : 164582 : int has_children = dwarf_abbrevhaschildren (&abbrev);
5984 : :
5985 [ + + ]: 164582 : fprintf (out, " [%5u] %s: %" PRId64
5986 : : ", %s: %s, %s: %s\n",
5987 : : code, offset_str, (int64_t) offset,
5988 : : children_str, has_children ? yes_str : no_str,
5989 : : tag_str, dwarf_tag_name (tag));
5990 : :
5991 : 164582 : size_t cnt = 0;
5992 : 164582 : unsigned int name;
5993 : 164582 : unsigned int form;
5994 : 164582 : Dwarf_Sword data;
5995 : 164582 : Dwarf_Off enoffset;
5996 : 164582 : while (dwarf_getabbrevattr_data (&abbrev, cnt, &name, &form,
5997 [ + + ]: 943770 : &data, &enoffset) == 0)
5998 : : {
5999 : 779188 : fprintf (out, " attr: %s, form: %s",
6000 : : dwarf_attr_name (name), dwarf_form_name (form));
6001 [ + + ]: 779188 : if (form == DW_FORM_implicit_const)
6002 : 55010 : fprintf (out, " (%" PRId64 ")", data);
6003 : 779188 : fprintf (out, ", offset: %#" PRIx64 "\n", (uint64_t) enoffset);
6004 : 779188 : ++cnt;
6005 : : }
6006 : :
6007 : 164582 : offset += length;
6008 : : }
6009 : : }
6010 : : }
6011 : :
6012 : :
6013 : : static void
6014 : 8 : print_debug_addr_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
6015 : : Ebl *ebl, GElf_Ehdr *ehdr,
6016 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg,
6017 : : FILE *out)
6018 : : {
6019 : 8 : Elf_Data *data = get_debug_elf_data (dbg, ebl, IDX_debug_addr, scn);
6020 [ + - ]: 8 : if (data == NULL)
6021 : : return;
6022 : :
6023 : 8 : fprintf (out, _("\
6024 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
6025 : : elf_ndxscn (scn), section_name (ebl, shdr),
6026 : 8 : (uint64_t) shdr->sh_offset);
6027 : :
6028 [ + - ]: 8 : if (shdr->sh_size == 0)
6029 : : return;
6030 : :
6031 : 8 : size_t idx = 0;
6032 : 8 : sort_listptr (&known_addrbases, "addr_base");
6033 : :
6034 : 8 : const unsigned char *start = (const unsigned char *) data->d_buf;
6035 : 8 : const unsigned char *readp = start;
6036 : 8 : const unsigned char *readendp = ((const unsigned char *) data->d_buf
6037 : 8 : + data->d_size);
6038 : :
6039 [ + + ]: 24 : while (readp < readendp)
6040 : : {
6041 : : /* We cannot really know whether or not there is an header. The
6042 : : DebugFission extension to DWARF4 doesn't add one. The DWARF5
6043 : : .debug_addr variant does. Whether or not we have an header,
6044 : : DW_AT_[GNU_]addr_base points at "index 0". So if the current
6045 : : offset equals the CU addr_base then we can just start
6046 : : printing addresses. If there is no CU with an exact match
6047 : : then we'll try to parse the header first. */
6048 : 16 : Dwarf_Off off = (Dwarf_Off) (readp
6049 : 16 : - (const unsigned char *) data->d_buf);
6050 : :
6051 : 16 : fprintf (out, "Table at offset %" PRIx64 " ", off);
6052 : :
6053 : : /* Find the first CU that could plausibly be associated with
6054 : : this address base offset. Skip CUs that point their addr_base
6055 : : before this table. */
6056 [ + - ]: 16 : struct listptr *listptr = get_listptr (&known_addrbases, idx);
6057 [ + + ]: 24 : while (listptr != NULL && listptr->offset < off)
6058 [ + - + - ]: 32 : listptr = get_listptr (&known_addrbases, ++idx);
6059 : :
6060 : 16 : const unsigned char *next_unitp;
6061 : :
6062 : 16 : uint64_t unit_length;
6063 : 16 : uint16_t version;
6064 : 16 : uint8_t address_size;
6065 : 16 : uint8_t segment_size;
6066 [ - + ]: 16 : if (listptr == NULL)
6067 : : {
6068 : 0 : error (0, 0, "Warning: No CU references .debug_addr after %" PRIx64,
6069 : : off);
6070 : :
6071 : : /* We will have to assume it is just addresses to the end... */
6072 [ # # ]: 0 : address_size = ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 4 : 8;
6073 : 0 : next_unitp = readendp;
6074 : 0 : fprintf (out, "Unknown CU:\n");
6075 : : }
6076 : : else
6077 : : {
6078 : 16 : Dwarf_Die cudie;
6079 [ - + ]: 16 : if (dwarf_cu_die (listptr->cu, &cudie,
6080 : : NULL, NULL, NULL, NULL,
6081 : : NULL, NULL) == NULL)
6082 : 0 : fprintf (out, "Unknown CU (%s):\n", dwarf_errmsg (-1));
6083 : : else
6084 : 16 : fprintf (out, "for CU [%6" PRIx64 "]:\n", dwarf_dieoffset (&cudie));
6085 : :
6086 [ + + ]: 16 : if (listptr->offset == off)
6087 : : {
6088 [ - + ]: 4 : address_size = listptr_address_size (listptr);
6089 : 4 : segment_size = 0;
6090 : 4 : version = 4;
6091 : :
6092 : : /* The addresses start here, but where do they end? */
6093 [ + + ]: 4 : listptr = get_listptr (&known_addrbases, idx + 1);
6094 [ - + ]: 2 : if (listptr == NULL)
6095 : : next_unitp = readendp;
6096 [ + - ]: 2 : else if (listptr->cu->version < 5)
6097 : : {
6098 : 2 : next_unitp = start + listptr->offset;
6099 [ + - - + ]: 2 : if (listptr->offset < off || listptr->offset > data->d_size)
6100 : : {
6101 : 0 : error (0, 0,
6102 : : "Warning: Bad address base for next unit at %"
6103 : : PRIx64, off);
6104 : 0 : next_unitp = readendp;
6105 : : }
6106 : : }
6107 : : else
6108 : : {
6109 : : /* Tricky, we don't have a header for this unit, but
6110 : : there is one for the next. We will have to
6111 : : "guess" how big it is and subtract it from the
6112 : : offset (because that points after the header). */
6113 [ # # ]: 0 : unsigned int offset_size = listptr_offset_size (listptr);
6114 : 0 : Dwarf_Off next_off = (listptr->offset
6115 : 0 : - (offset_size == 4 ? 4 : 12) /* len */
6116 : : - 2 /* version */
6117 : : - 1 /* address size */
6118 : 0 : - 1); /* segment selector size */
6119 : 0 : next_unitp = start + next_off;
6120 [ # # # # ]: 0 : if (next_off < off || next_off > data->d_size)
6121 : : {
6122 : 0 : error (0, 0,
6123 : : "Warning: Couldn't calculate .debug_addr "
6124 : : " unit length at %" PRIx64, off);
6125 : 0 : next_unitp = readendp;
6126 : : }
6127 : : }
6128 : 4 : unit_length = (uint64_t) (next_unitp - readp);
6129 : :
6130 : : /* Pretend we have a header. */
6131 : 4 : fprintf (out, "\n");
6132 : 4 : fprintf (out, _(" Length: %8" PRIu64 "\n"),
6133 : : unit_length);
6134 : 4 : fprintf (out, _(" DWARF version: %8" PRIu16 "\n"), version);
6135 : 4 : fprintf (out, _(" Address size: %8" PRIu64 "\n"),
6136 : : (uint64_t) address_size);
6137 : 4 : fprintf (out, _(" Segment size: %8" PRIu64 "\n"),
6138 : : (uint64_t) segment_size);
6139 : 4 : fprintf (out, "\n");
6140 : : }
6141 : : else
6142 : : {
6143 : : /* OK, we have to parse an header first. */
6144 [ - + ]: 12 : unit_length = read_4ubyte_unaligned_inc (dbg, readp);
6145 [ - + ]: 12 : if (unlikely (unit_length == 0xffffffff))
6146 : : {
6147 [ # # ]: 0 : if (unlikely (readp > readendp - 8))
6148 : : {
6149 : 0 : invalid_data:
6150 : 0 : error (0, 0, "Invalid data");
6151 : 0 : return;
6152 : : }
6153 [ # # ]: 0 : unit_length = read_8ubyte_unaligned_inc (dbg, readp);
6154 : : }
6155 : 12 : fprintf (out, "\n");
6156 : 12 : fprintf (out, _(" Length: %8" PRIu64 "\n"),
6157 : : unit_length);
6158 : :
6159 : : /* We need at least 2-bytes (version) + 1-byte
6160 : : (addr_size) + 1-byte (segment_size) = 4 bytes to
6161 : : complete the header. And this unit cannot go beyond
6162 : : the section data. */
6163 [ + - ]: 12 : if (readp > readendp - 4
6164 [ + - ]: 12 : || unit_length < 4
6165 [ - + ]: 12 : || unit_length > (uint64_t) (readendp - readp))
6166 : 0 : goto invalid_data;
6167 : :
6168 : 12 : next_unitp = readp + unit_length;
6169 : :
6170 [ - + ]: 12 : version = read_2ubyte_unaligned_inc (dbg, readp);
6171 : 12 : fprintf (out, _(" DWARF version: %8" PRIu16 "\n"), version);
6172 : :
6173 [ - + ]: 12 : if (version != 5)
6174 : : {
6175 : 0 : error (0, 0, _("Unknown version"));
6176 : 0 : goto next_unit;
6177 : : }
6178 : :
6179 : 12 : address_size = *readp++;
6180 : 12 : fprintf (out, _(" Address size: %8" PRIu64 "\n"),
6181 : : (uint64_t) address_size);
6182 : :
6183 [ - + ]: 12 : if (address_size != 4 && address_size != 8)
6184 : : {
6185 : 0 : error (0, 0, _("unsupported address size"));
6186 : 0 : goto next_unit;
6187 : : }
6188 : :
6189 : 12 : segment_size = *readp++;
6190 : 12 : fprintf (out, _(" Segment size: %8" PRIu64 "\n"),
6191 : : (uint64_t) segment_size);
6192 : 12 : fprintf (out, "\n");
6193 : :
6194 [ - + ]: 12 : if (segment_size != 0)
6195 : : {
6196 : 0 : error (0, 0, _("unsupported segment size"));
6197 : 0 : goto next_unit;
6198 : : }
6199 : :
6200 [ - + ]: 12 : if (listptr->offset != (Dwarf_Off) (readp - start))
6201 : : {
6202 : 0 : error (0, 0, "Address index doesn't start after header");
6203 : 0 : goto next_unit;
6204 : : }
6205 : : }
6206 : : }
6207 : :
6208 : 16 : int digits = 1;
6209 : 16 : size_t addresses = (next_unitp - readp) / address_size;
6210 [ + + ]: 24 : while (addresses >= 10)
6211 : : {
6212 : 8 : ++digits;
6213 : 8 : addresses /= 10;
6214 : : }
6215 : :
6216 : 16 : unsigned int uidx = 0;
6217 : 16 : size_t index_offset = readp - (const unsigned char *) data->d_buf;
6218 : 16 : fprintf (out, " Addresses start at offset 0x%zx:\n", index_offset);
6219 [ + + ]: 188 : while (readp <= next_unitp - address_size)
6220 : : {
6221 [ - + + + : 172 : Dwarf_Addr addr = read_addr_unaligned_inc (address_size, dbg,
- + - + ]
6222 : : readp);
6223 : 172 : fprintf (out, " [%*u] ", digits, uidx++);
6224 : 172 : print_dwarf_addr (dwflmod, address_size, addr, addr, out);
6225 : 172 : fprintf (out, "\n");
6226 : : }
6227 : 16 : fprintf (out, "\n");
6228 : :
6229 [ + - ]: 16 : if (readp != next_unitp)
6230 : 0 : error (0, 0, "extra %zd bytes at end of unit",
6231 : 0 : (size_t) (next_unitp - readp));
6232 : :
6233 : 16 : next_unit:
6234 : : readp = next_unitp;
6235 : : }
6236 : : }
6237 : :
6238 : : /* Print content of DWARF .debug_aranges section. We fortunately do
6239 : : not have to know a bit about the structure of the section, libdwarf
6240 : : takes care of it. */
6241 : : static void
6242 : 2 : print_decoded_aranges_section (Ebl *ebl, GElf_Ehdr *ehdr, Elf_Scn *scn,
6243 : : GElf_Shdr *shdr, Dwarf *dbg, FILE *out)
6244 : : {
6245 : 2 : Dwarf_Aranges *aranges;
6246 : 2 : size_t cnt;
6247 [ - + ]: 2 : if (unlikely (dwarf_getaranges (dbg, &aranges, &cnt) != 0))
6248 : : {
6249 : 0 : error (0, 0, _("cannot get .debug_aranges content: %s"),
6250 : : dwarf_errmsg (-1));
6251 : 0 : return;
6252 : : }
6253 : :
6254 : 2 : GElf_Shdr glink_mem;
6255 : 2 : GElf_Shdr *glink;
6256 : 2 : glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link), &glink_mem);
6257 [ - + ]: 2 : if (glink == NULL)
6258 : : {
6259 : 0 : error (0, 0, _("invalid sh_link value in section %zu"),
6260 : : elf_ndxscn (scn));
6261 : 0 : return;
6262 : : }
6263 : :
6264 : 2 : fprintf (out, ngettext ("\
6265 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 " contains %zu entry:\n",
6266 : : "\
6267 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 " contains %zu entries:\n",
6268 : : cnt),
6269 : : elf_ndxscn (scn), section_name (ebl, shdr),
6270 : 2 : (uint64_t) shdr->sh_offset, cnt);
6271 : :
6272 : : /* Compute floor(log16(cnt)). */
6273 : 2 : size_t tmp = cnt;
6274 : 2 : int digits = 1;
6275 [ - + ]: 2 : while (tmp >= 16)
6276 : : {
6277 : 0 : ++digits;
6278 : 0 : tmp >>= 4;
6279 : : }
6280 : :
6281 [ + + ]: 12 : for (size_t n = 0; n < cnt; ++n)
6282 : : {
6283 : 10 : Dwarf_Arange *runp = dwarf_onearange (aranges, n);
6284 [ - + ]: 10 : if (unlikely (runp == NULL))
6285 : : {
6286 : 0 : fprintf (out, "cannot get arange %zu: %s\n", n, dwarf_errmsg (-1));
6287 : 0 : return;
6288 : : }
6289 : :
6290 : 10 : Dwarf_Addr start;
6291 : 10 : Dwarf_Word length;
6292 : 10 : Dwarf_Off offset;
6293 : :
6294 [ - + ]: 10 : if (unlikely (dwarf_getarangeinfo (runp, &start, &length, &offset) != 0))
6295 : 0 : fprintf (out, _(" [%*zu] ???\n"), digits, n);
6296 : : else
6297 : 10 : fprintf (out, _(" [%*zu] start: %0#*" PRIx64
6298 : : ", length: %5" PRIu64 ", CU DIE offset: %6"
6299 : : PRId64 "\n"),
6300 [ - + ]: 10 : digits, n, ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 10 : 18,
6301 : : (uint64_t) start, (uint64_t) length, (int64_t) offset);
6302 : : }
6303 : : }
6304 : :
6305 : :
6306 : : /* Print content of DWARF .debug_aranges section. */
6307 : : static void
6308 : 92 : print_debug_aranges_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
6309 : : Ebl *ebl, GElf_Ehdr *ehdr, Elf_Scn *scn,
6310 : : GElf_Shdr *shdr, Dwarf *dbg, FILE *out)
6311 : : {
6312 [ + + ]: 92 : if (decodedaranges)
6313 : : {
6314 : 2 : print_decoded_aranges_section (ebl, ehdr, scn, shdr, dbg, out);
6315 : 2 : return;
6316 : : }
6317 : :
6318 : 90 : Elf_Data *data = get_debug_elf_data (dbg, ebl, IDX_debug_aranges, scn);
6319 [ + - ]: 90 : if (data == NULL)
6320 : : return;
6321 : :
6322 : 90 : fprintf (out, _("\
6323 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
6324 : : elf_ndxscn (scn), section_name (ebl, shdr),
6325 : 90 : (uint64_t) shdr->sh_offset);
6326 : :
6327 : 90 : const unsigned char *readp = data->d_buf;
6328 : 90 : const unsigned char *readendp = readp + data->d_size;
6329 : :
6330 [ + + ]: 3666 : while (readp < readendp)
6331 : : {
6332 : 3576 : const unsigned char *hdrstart = readp;
6333 : 3576 : size_t start_offset = hdrstart - (const unsigned char *) data->d_buf;
6334 : :
6335 : 3576 : fprintf (out, "\n%s %zu:\n", table_offset_lower_str, start_offset);
6336 [ - + ]: 3576 : if (readp + 4 > readendp)
6337 : : {
6338 : 0 : invalid_data:
6339 : 0 : error (0, 0, _("invalid data in section [%zu] '%s'"),
6340 : : elf_ndxscn (scn), section_name (ebl, shdr));
6341 : 0 : return;
6342 : : }
6343 : :
6344 [ + + ]: 3576 : Dwarf_Word length = read_4ubyte_unaligned_inc (dbg, readp);
6345 : 3576 : unsigned int length_bytes = 4;
6346 [ - + ]: 3576 : if (length == DWARF3_LENGTH_64_BIT)
6347 : : {
6348 [ # # ]: 0 : if (readp + 8 > readendp)
6349 : 0 : goto invalid_data;
6350 [ # # ]: 0 : length = read_8ubyte_unaligned_inc (dbg, readp);
6351 : 0 : length_bytes = 8;
6352 : : }
6353 : :
6354 : 3576 : const unsigned char *nexthdr = readp + length;
6355 : 3576 : fprintf (out, "\n %s: %6" PRIu64 "\n",
6356 : : length_str, (uint64_t) length);
6357 : :
6358 [ - + ]: 3576 : if (unlikely (length > (size_t) (readendp - readp)))
6359 : 0 : goto invalid_data;
6360 : :
6361 [ - + ]: 3576 : if (length == 0)
6362 : 0 : continue;
6363 : :
6364 [ - + ]: 3576 : if (readp + 2 > readendp)
6365 : 0 : goto invalid_data;
6366 [ + + ]: 3576 : uint_fast16_t version = read_2ubyte_unaligned_inc (dbg, readp);
6367 : 3576 : fprintf (out, " %s: %6" PRIuFAST16 "\n",
6368 : : dwarf_ver_str, version);
6369 [ - + ]: 3576 : if (version != 2)
6370 : : {
6371 : 0 : error (0, 0, _("unsupported aranges version"));
6372 : 0 : goto next_table;
6373 : : }
6374 : :
6375 : 3576 : Dwarf_Word offset;
6376 [ - + ]: 3576 : if (readp + length_bytes > readendp)
6377 : 0 : goto invalid_data;
6378 [ - + ]: 3576 : if (length_bytes == 8)
6379 [ # # ]: 0 : offset = read_8ubyte_unaligned_inc (dbg, readp);
6380 : : else
6381 [ + + ]: 3576 : offset = read_4ubyte_unaligned_inc (dbg, readp);
6382 : 3576 : fprintf (out, " %s: %6" PRIx64 "\n",
6383 : : cu_offset_str, (uint64_t) offset);
6384 : :
6385 [ - + ]: 3576 : if (readp + 1 > readendp)
6386 : 0 : goto invalid_data;
6387 : 3576 : unsigned int address_size = *readp++;
6388 : 3576 : fprintf (out, " %s: %6" PRIu64 "\n",
6389 : : addr_size_str, (uint64_t) address_size);
6390 [ - + ]: 3576 : if (address_size != 4 && address_size != 8)
6391 : : {
6392 : 0 : error (0, 0, _("unsupported address size"));
6393 : 0 : goto next_table;
6394 : : }
6395 : :
6396 [ - + ]: 3576 : if (readp + 1 > readendp)
6397 : 0 : goto invalid_data;
6398 : 3576 : unsigned int segment_size = *readp++;
6399 : 3576 : fprintf (out, " %s: %6" PRIu64 "\n\n",
6400 : : seg_size_str, (uint64_t) segment_size);
6401 [ - + - - ]: 3576 : if (segment_size != 0 && segment_size != 4 && segment_size != 8)
6402 : : {
6403 : 0 : error (0, 0, _("unsupported segment size"));
6404 : 0 : goto next_table;
6405 : : }
6406 : :
6407 : : /* Round the address to the next multiple of 2*address_size. */
6408 : 3576 : readp += ((2 * address_size - ((readp - hdrstart) % (2 * address_size)))
6409 : 3576 : % (2 * address_size));
6410 : :
6411 [ + - ]: 9168 : while (readp < nexthdr)
6412 : : {
6413 : 9168 : Dwarf_Word range_address;
6414 : 9168 : Dwarf_Word range_length;
6415 : 9168 : Dwarf_Word segment = 0;
6416 [ - + ]: 9168 : if (readp + 2 * address_size + segment_size > readendp)
6417 : 0 : goto invalid_data;
6418 [ + + ]: 9168 : if (address_size == 4)
6419 : : {
6420 [ + + ]: 92 : range_address = read_4ubyte_unaligned_inc (dbg, readp);
6421 [ + + ]: 92 : range_length = read_4ubyte_unaligned_inc (dbg, readp);
6422 : : }
6423 : : else
6424 : : {
6425 [ + + ]: 9076 : range_address = read_8ubyte_unaligned_inc (dbg, readp);
6426 [ + + ]: 9076 : range_length = read_8ubyte_unaligned_inc (dbg, readp);
6427 : : }
6428 : :
6429 [ - + ]: 9168 : if (segment_size == 4)
6430 [ # # ]: 0 : segment = read_4ubyte_unaligned_inc (dbg, readp);
6431 [ - + ]: 9168 : else if (segment_size == 8)
6432 [ # # ]: 0 : segment = read_8ubyte_unaligned_inc (dbg, readp);
6433 : :
6434 [ + + ]: 9168 : if (range_address == 0 && range_length == 0 && segment == 0)
6435 : : break;
6436 : :
6437 : 5592 : fprintf (out, " ");
6438 : 5592 : print_dwarf_addr (dwflmod, address_size, range_address,
6439 : : range_address, out);
6440 : 5592 : fprintf (out, "..");
6441 : 5592 : print_dwarf_addr (dwflmod, address_size,
6442 : 5592 : range_address + range_length - 1,
6443 : : range_length, out);
6444 [ - + ]: 5592 : if (segment_size != 0)
6445 : 0 : fprintf (out, " (%" PRIx64 ")\n", (uint64_t) segment);
6446 : : else
6447 : 5592 : fprintf (out, "\n");
6448 : : }
6449 : :
6450 : 3576 : next_table:
6451 [ - + ]: 3576 : if (readp != nexthdr)
6452 : : {
6453 : 0 : size_t padding = nexthdr - readp;
6454 : 0 : fprintf (out, _(" %zu padding bytes\n"), padding);
6455 : 0 : readp = nexthdr;
6456 : : }
6457 : : }
6458 : : }
6459 : :
6460 : :
6461 : : static bool is_split_dwarf (Dwarf *dbg, uint64_t *id, Dwarf_CU **split_cu);
6462 : :
6463 : : /* Returns true and sets cu and cu_base if the given Dwarf is a split
6464 : : DWARF (.dwo) file. */
6465 : : static bool
6466 : 0 : split_dwarf_cu_base (Dwarf *dbg, Dwarf_CU **cu, Dwarf_Addr *cu_base)
6467 : : {
6468 : 0 : uint64_t id;
6469 [ # # ]: 0 : if (is_split_dwarf (dbg, &id, cu))
6470 : : {
6471 : 0 : Dwarf_Die cudie;
6472 [ # # ]: 0 : if (dwarf_cu_info (*cu, NULL, NULL, &cudie, NULL, NULL, NULL, NULL) == 0)
6473 : : {
6474 : 0 : *cu_base = cudie_base (&cudie);
6475 : 0 : return true;
6476 : : }
6477 : : }
6478 : : return false;
6479 : : }
6480 : :
6481 : : /* Print content of DWARF .debug_rnglists section. */
6482 : : static void
6483 : 38 : print_debug_rnglists_section (Dwfl_Module *dwflmod,
6484 : : Ebl *ebl,
6485 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
6486 : : Elf_Scn *scn, GElf_Shdr *shdr,
6487 : : Dwarf *dbg __attribute__((unused)),
6488 : : FILE *out)
6489 : : {
6490 : 38 : Elf_Data *data = get_debug_elf_data (dbg, ebl, IDX_debug_rnglists, scn);
6491 [ + - ]: 38 : if (data == NULL)
6492 : 0 : return;
6493 : :
6494 : 38 : fprintf (out, _("\
6495 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
6496 : : elf_ndxscn (scn), section_name (ebl, shdr),
6497 : 38 : (uint64_t) shdr->sh_offset);
6498 : :
6499 : : /* For the listptr to get the base address/CU. */
6500 : 38 : sort_listptr (&known_rnglistptr, "rnglistptr");
6501 : 38 : size_t listptr_idx = 0;
6502 : :
6503 : 38 : const unsigned char *readp = data->d_buf;
6504 : 38 : const unsigned char *const dataend = ((unsigned char *) data->d_buf
6505 : 38 : + data->d_size);
6506 [ + + ]: 2080 : while (readp < dataend)
6507 : : {
6508 [ - + ]: 2042 : if (unlikely (readp > dataend - 4))
6509 : : {
6510 : 0 : invalid_data:
6511 : 0 : error (0, 0, _("invalid data in section [%zu] '%s'"),
6512 : : elf_ndxscn (scn), section_name (ebl, shdr));
6513 : 0 : return;
6514 : : }
6515 : :
6516 : 2042 : ptrdiff_t offset = readp - (unsigned char *) data->d_buf;
6517 : 2042 : fprintf (out, "%s 0x%" PRIx64 ":\n\n",
6518 : : table_offset_upper_str, (uint64_t) offset);
6519 : :
6520 [ - + ]: 2042 : uint64_t unit_length = read_4ubyte_unaligned_inc (dbg, readp);
6521 : 2042 : unsigned int offset_size = 4;
6522 [ - + ]: 2042 : if (unlikely (unit_length == 0xffffffff))
6523 : : {
6524 [ # # ]: 0 : if (unlikely (readp > dataend - 8))
6525 : 0 : goto invalid_data;
6526 : :
6527 [ # # ]: 0 : unit_length = read_8ubyte_unaligned_inc (dbg, readp);
6528 : 0 : offset_size = 8;
6529 : : }
6530 : 2042 : fprintf (out, " %s: %8" PRIu64 "\n", length_str, unit_length);
6531 : :
6532 : : /* We need at least 2-bytes + 1-byte + 1-byte + 4-bytes = 8
6533 : : bytes to complete the header. And this unit cannot go beyond
6534 : : the section data. */
6535 [ + - ]: 2042 : if (readp > dataend - 8
6536 [ + - ]: 2042 : || unit_length < 8
6537 [ - + ]: 2042 : || unit_length > (uint64_t) (dataend - readp))
6538 : 0 : goto invalid_data;
6539 : :
6540 : 2042 : const unsigned char *nexthdr = readp + unit_length;
6541 : :
6542 [ - + ]: 2042 : uint16_t version = read_2ubyte_unaligned_inc (dbg, readp);
6543 : 2042 : fprintf (out, " %s: %8" PRIu16 "\n", dwarf_ver_str, version);
6544 : :
6545 [ - + ]: 2042 : if (version != 5)
6546 : : {
6547 : 0 : error (0, 0, _("Unknown version"));
6548 : 0 : goto next_table;
6549 : : }
6550 : :
6551 : 2042 : uint8_t address_size = *readp++;
6552 : 2042 : fprintf (out, " %s: %8" PRIu64 "\n",
6553 : : addr_size_str, (uint64_t) address_size);
6554 : :
6555 [ - + ]: 2042 : if (address_size != 4 && address_size != 8)
6556 : : {
6557 : 0 : error (0, 0, _("unsupported address size"));
6558 : 0 : goto next_table;
6559 : : }
6560 : :
6561 : 2042 : uint8_t segment_size = *readp++;
6562 : 2042 : fprintf (out, _(" Segment size: %8" PRIu64 "\n"),
6563 : : (uint64_t) segment_size);
6564 : :
6565 [ - + - - ]: 2042 : if (segment_size != 0 && segment_size != 4 && segment_size != 8)
6566 : : {
6567 : 0 : error (0, 0, _("unsupported segment size"));
6568 : 0 : goto next_table;
6569 : : }
6570 : :
6571 [ - + ]: 2042 : uint32_t offset_entry_count = read_4ubyte_unaligned_inc (dbg, readp);
6572 : 2042 : fprintf (out, " %s: %8" PRIu64 "\n",
6573 : : offset_ent_str, (uint64_t) offset_entry_count);
6574 : :
6575 : : /* We need the CU that uses this unit to get the initial base address. */
6576 : 2042 : Dwarf_Addr cu_base = 0;
6577 : 2042 : struct Dwarf_CU *cu = NULL;
6578 [ - + ]: 2042 : if (listptr_cu (&known_rnglistptr, &listptr_idx,
6579 : : (Dwarf_Off) offset,
6580 : 2042 : (Dwarf_Off) (nexthdr - (unsigned char *) data->d_buf),
6581 : : &cu_base, &cu)
6582 [ # # ]: 0 : || split_dwarf_cu_base (dbg, &cu, &cu_base))
6583 : 2042 : {
6584 : 2042 : Dwarf_Die cudie;
6585 [ - + ]: 2042 : if (dwarf_cu_die (cu, &cudie,
6586 : : NULL, NULL, NULL, NULL,
6587 : : NULL, NULL) == NULL)
6588 : 0 : fprintf (out, " %s: ", unknown_base_str);
6589 : : else
6590 : 2042 : fprintf (out, " %s [%6" PRIx64 "] %s: ",
6591 : : cu_str, dwarf_dieoffset (&cudie), base_str);
6592 : 2042 : print_dwarf_addr (dwflmod, address_size, cu_base, cu_base, out);
6593 : 2042 : fprintf (out, "\n");
6594 : : }
6595 : : else
6596 : 0 : fprintf (out, _(" Not associated with a CU.\n"));
6597 : :
6598 : 2042 : fprintf (out, "\n");
6599 : :
6600 : 2042 : const unsigned char *offset_array_start = readp;
6601 [ + + ]: 2042 : if (offset_entry_count > 0)
6602 : : {
6603 : 2 : uint64_t max_entries = (unit_length - 8) / offset_size;
6604 [ - + ]: 2 : if (offset_entry_count > max_entries)
6605 : : {
6606 : 0 : error (0, 0,
6607 : 0 : _("too many offset entries for unit length"));
6608 : 0 : offset_entry_count = max_entries;
6609 : : }
6610 : :
6611 : 2 : fprintf (out, _(" Offsets starting at 0x%" PRIx64 ":\n"),
6612 : : (uint64_t) (offset_array_start
6613 : 2 : - (unsigned char *) data->d_buf));
6614 [ + + ]: 6 : for (uint32_t idx = 0; idx < offset_entry_count; idx++)
6615 : : {
6616 : 4 : fprintf (out, " [%6" PRIu32 "] ", idx);
6617 [ + - ]: 4 : if (offset_size == 4)
6618 : : {
6619 [ - + ]: 4 : uint32_t off = read_4ubyte_unaligned_inc (dbg, readp);
6620 : 4 : fprintf (out, "0x%" PRIx32 "\n", off);
6621 : : }
6622 : : else
6623 : : {
6624 [ # # ]: 0 : uint64_t off = read_8ubyte_unaligned_inc (dbg, readp);
6625 : 0 : fprintf (out, "0x%" PRIx64 "\n", off);
6626 : : }
6627 : : }
6628 : 2 : fprintf (out, "\n");
6629 : : }
6630 : :
6631 : 2042 : Dwarf_Addr base = cu_base;
6632 : 2042 : bool start_of_list = true;
6633 [ + + ]: 150364 : while (readp < nexthdr)
6634 : : {
6635 : 148322 : uint8_t kind = *readp++;
6636 : 148322 : uint64_t op1, op2;
6637 : :
6638 : : /* Skip padding. */
6639 [ - + ]: 148322 : if (start_of_list && kind == DW_RLE_end_of_list)
6640 : 0 : continue;
6641 : :
6642 [ + + ]: 148322 : if (start_of_list)
6643 : : {
6644 : 30104 : base = cu_base;
6645 : 30104 : fprintf (out, " Offset: %" PRIx64 ", Index: %" PRIx64 "\n",
6646 : 30104 : (uint64_t) (readp - (unsigned char *) data->d_buf - 1),
6647 : 30104 : (uint64_t) (readp - offset_array_start - 1));
6648 : 30104 : start_of_list = false;
6649 : : }
6650 : :
6651 : 148322 : fprintf (out, " %s", dwarf_range_list_encoding_name (kind));
6652 [ + - - - : 148322 : switch (kind)
+ + - +
- ]
6653 : : {
6654 : 30104 : case DW_RLE_end_of_list:
6655 : 30104 : start_of_list = true;
6656 : 30104 : fprintf (out, "\n\n");
6657 : 30104 : break;
6658 : :
6659 : 0 : case DW_RLE_base_addressx:
6660 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
6661 : : {
6662 : 0 : invalid_range:
6663 : 0 : error (0, 0, _("invalid range list data"));
6664 : 0 : goto next_table;
6665 : : }
6666 : 0 : get_uleb128 (op1, readp, nexthdr);
6667 : 0 : fprintf (out, " %" PRIx64 "\n", op1);
6668 [ # # ]: 0 : if (! print_unresolved_addresses)
6669 : : {
6670 : 0 : Dwarf_Addr addr;
6671 [ # # ]: 0 : if (get_indexed_addr (cu, op1, &addr) != 0)
6672 : 0 : fprintf (out, " ???\n");
6673 : : else
6674 : : {
6675 : 0 : fprintf (out, " ");
6676 : 0 : print_dwarf_addr (dwflmod, address_size, addr,
6677 : : addr, out);
6678 : 0 : fprintf (out, "\n");
6679 : : }
6680 : : }
6681 : : break;
6682 : :
6683 : 0 : case DW_RLE_startx_endx:
6684 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
6685 : 0 : goto invalid_range;
6686 : 0 : get_uleb128 (op1, readp, nexthdr);
6687 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
6688 : 0 : goto invalid_range;
6689 : 0 : get_uleb128 (op2, readp, nexthdr);
6690 : 0 : fprintf (out, " %" PRIx64 ", %" PRIx64 "\n", op1, op2);
6691 [ # # ]: 0 : if (! print_unresolved_addresses)
6692 : : {
6693 : 0 : Dwarf_Addr addr1;
6694 : 0 : Dwarf_Addr addr2;
6695 [ # # ]: 0 : if (get_indexed_addr (cu, op1, &addr1) != 0
6696 [ # # ]: 0 : || get_indexed_addr (cu, op2, &addr2) != 0)
6697 : : {
6698 : 0 : fprintf (out, " ???..\n");
6699 : 0 : fprintf (out, " ???\n");
6700 : : }
6701 : : else
6702 : : {
6703 : 0 : fprintf (out, " ");
6704 : 0 : print_dwarf_addr (dwflmod, address_size,
6705 : : addr1, addr1, out);
6706 : 0 : fprintf (out, "..\n ");
6707 : 0 : print_dwarf_addr (dwflmod, address_size,
6708 : : addr2 - 1, addr2, out);
6709 : 0 : fprintf (out, "\n");
6710 : : }
6711 : : }
6712 : : break;
6713 : :
6714 : 0 : case DW_RLE_startx_length:
6715 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
6716 : 0 : goto invalid_range;
6717 : 0 : get_uleb128 (op1, readp, nexthdr);
6718 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
6719 : 0 : goto invalid_range;
6720 : 0 : get_uleb128 (op2, readp, nexthdr);
6721 : 0 : fprintf (out, " %" PRIx64 ", %" PRIx64 "\n", op1, op2);
6722 [ # # ]: 0 : if (! print_unresolved_addresses)
6723 : : {
6724 : 0 : Dwarf_Addr addr1;
6725 : 0 : Dwarf_Addr addr2;
6726 [ # # ]: 0 : if (get_indexed_addr (cu, op1, &addr1) != 0)
6727 : : {
6728 : 0 : fprintf (out, " ???..\n");
6729 : 0 : fprintf (out, " ???\n");
6730 : : }
6731 : : else
6732 : : {
6733 : 0 : addr2 = addr1 + op2;
6734 : 0 : fprintf (out, " ");
6735 : 0 : print_dwarf_addr (dwflmod, address_size, addr1, addr1, out);
6736 : 0 : fprintf (out, "..\n ");
6737 : 0 : print_dwarf_addr (dwflmod, address_size,
6738 : : addr2 - 1, addr2, out);
6739 : 0 : fprintf (out, "\n");
6740 : : }
6741 : : }
6742 : : break;
6743 : :
6744 : 93842 : case DW_RLE_offset_pair:
6745 [ - + ]: 93842 : if ((uint64_t) (nexthdr - readp) < 1)
6746 : 0 : goto invalid_range;
6747 : 93842 : get_uleb128 (op1, readp, nexthdr);
6748 [ - + ]: 93842 : if ((uint64_t) (nexthdr - readp) < 1)
6749 : 0 : goto invalid_range;
6750 : 93842 : get_uleb128 (op2, readp, nexthdr);
6751 : 93842 : fprintf (out, " %" PRIx64 ", %" PRIx64 "\n", op1, op2);
6752 [ + - ]: 93842 : if (! print_unresolved_addresses)
6753 : : {
6754 : 93842 : op1 += base;
6755 : 93842 : op2 += base;
6756 : 93842 : fprintf (out, " ");
6757 : 93842 : print_dwarf_addr (dwflmod, address_size, op1, op1, out);
6758 : 93842 : fprintf (out, "..\n ");
6759 : 93842 : print_dwarf_addr (dwflmod, address_size, op2 - 1, op2, out);
6760 : 93842 : fprintf (out, "\n");
6761 : : }
6762 : : break;
6763 : :
6764 : 17016 : case DW_RLE_base_address:
6765 [ - + ]: 17016 : if (address_size == 4)
6766 : : {
6767 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 4)
6768 : 0 : goto invalid_range;
6769 [ # # ]: 0 : op1 = read_4ubyte_unaligned_inc (dbg, readp);
6770 : : }
6771 : : else
6772 : : {
6773 [ - + ]: 17016 : if ((uint64_t) (nexthdr - readp) < 8)
6774 : 0 : goto invalid_range;
6775 [ - + ]: 17016 : op1 = read_8ubyte_unaligned_inc (dbg, readp);
6776 : : }
6777 : 17016 : base = op1;
6778 : 17016 : fprintf (out, " 0x%" PRIx64 "\n", base);
6779 [ + - ]: 17016 : if (! print_unresolved_addresses)
6780 : : {
6781 : 17016 : fprintf (out, " ");
6782 : 17016 : print_dwarf_addr (dwflmod, address_size, base, base, out);
6783 : 17016 : fprintf (out, "\n");
6784 : : }
6785 : : break;
6786 : :
6787 : 0 : case DW_RLE_start_end:
6788 [ # # ]: 0 : if (address_size == 4)
6789 : : {
6790 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 8)
6791 : 0 : goto invalid_range;
6792 [ # # ]: 0 : op1 = read_4ubyte_unaligned_inc (dbg, readp);
6793 [ # # ]: 0 : op2 = read_4ubyte_unaligned_inc (dbg, readp);
6794 : : }
6795 : : else
6796 : : {
6797 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 16)
6798 : 0 : goto invalid_range;
6799 [ # # ]: 0 : op1 = read_8ubyte_unaligned_inc (dbg, readp);
6800 [ # # ]: 0 : op2 = read_8ubyte_unaligned_inc (dbg, readp);
6801 : : }
6802 : 0 : fprintf (out, " 0x%" PRIx64 "..0x%" PRIx64 "\n", op1, op2);
6803 [ # # ]: 0 : if (! print_unresolved_addresses)
6804 : : {
6805 : 0 : fprintf (out, " ");
6806 : 0 : print_dwarf_addr (dwflmod, address_size, op1, op1, out);
6807 : 0 : fprintf (out, "..\n ");
6808 : 0 : print_dwarf_addr (dwflmod, address_size, op2 - 1, op2, out);
6809 : 0 : fprintf (out, "\n");
6810 : : }
6811 : : break;
6812 : :
6813 : 7360 : case DW_RLE_start_length:
6814 [ - + ]: 7360 : if (address_size == 4)
6815 : : {
6816 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 4)
6817 : 0 : goto invalid_range;
6818 [ # # ]: 0 : op1 = read_4ubyte_unaligned_inc (dbg, readp);
6819 : : }
6820 : : else
6821 : : {
6822 [ - + ]: 7360 : if ((uint64_t) (nexthdr - readp) < 8)
6823 : 0 : goto invalid_range;
6824 [ - + ]: 7360 : op1 = read_8ubyte_unaligned_inc (dbg, readp);
6825 : : }
6826 [ - + ]: 7360 : if ((uint64_t) (nexthdr - readp) < 1)
6827 : 0 : goto invalid_range;
6828 : 7360 : get_uleb128 (op2, readp, nexthdr);
6829 : 7360 : fprintf (out, " 0x%" PRIx64 ", %" PRIx64 "\n", op1, op2);
6830 [ + + ]: 7360 : if (! print_unresolved_addresses)
6831 : : {
6832 : 7358 : op2 = op1 + op2;
6833 : 7358 : fprintf (out, " ");
6834 : 7358 : print_dwarf_addr (dwflmod, address_size, op1, op1, out);
6835 : 7358 : fprintf (out, "..\n ");
6836 : 7358 : print_dwarf_addr (dwflmod, address_size, op2 - 1, op2, out);
6837 : 7358 : fprintf (out, "\n");
6838 : : }
6839 : : break;
6840 : :
6841 : 0 : default:
6842 : 0 : goto invalid_range;
6843 : : }
6844 : : }
6845 : :
6846 : 2042 : next_table:
6847 [ - + ]: 2042 : if (readp != nexthdr)
6848 : : {
6849 : 0 : size_t padding = nexthdr - readp;
6850 : 0 : fprintf (out, _(" %zu padding bytes\n\n"), padding);
6851 : 0 : readp = nexthdr;
6852 : : }
6853 : : }
6854 : : }
6855 : :
6856 : : /* Print content of DWARF .debug_ranges section. */
6857 : : static void
6858 : 32 : print_debug_ranges_section (Dwfl_Module *dwflmod,
6859 : : Ebl *ebl, GElf_Ehdr *ehdr,
6860 : : Elf_Scn *scn, GElf_Shdr *shdr,
6861 : : Dwarf *dbg, FILE *out)
6862 : : {
6863 : 32 : Elf_Data *data = get_debug_elf_data (dbg, ebl, IDX_debug_ranges, scn);
6864 [ - + ]: 32 : if (data == NULL)
6865 : 0 : return;
6866 : :
6867 : 32 : fprintf (out, _("\
6868 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
6869 : : elf_ndxscn (scn), section_name (ebl, shdr),
6870 : 32 : (uint64_t) shdr->sh_offset);
6871 : :
6872 : 32 : sort_listptr (&known_rangelistptr, "rangelistptr");
6873 : 32 : size_t listptr_idx = 0;
6874 : :
6875 [ + + ]: 32 : uint_fast8_t address_size = ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 4 : 8;
6876 : :
6877 : 32 : bool first = true;
6878 : 32 : Dwarf_Addr base = 0;
6879 : 32 : unsigned char *const endp = (unsigned char *) data->d_buf + data->d_size;
6880 : 32 : unsigned char *readp = data->d_buf;
6881 : 32 : Dwarf_CU *last_cu = NULL;
6882 [ + + ]: 184 : while (readp < endp)
6883 : : {
6884 : 152 : ptrdiff_t offset = readp - (unsigned char *) data->d_buf;
6885 : 152 : Dwarf_CU *cu = last_cu;
6886 : :
6887 [ + + - + ]: 152 : if (first && skip_listptr_hole (&known_rangelistptr, &listptr_idx,
6888 : : &address_size, NULL, &base, &cu,
6889 : : offset, &readp, endp, NULL))
6890 : 0 : continue;
6891 : :
6892 [ + + ]: 152 : if (last_cu != cu)
6893 : : {
6894 : 58 : Dwarf_Die cudie;
6895 [ - + ]: 58 : if (dwarf_cu_die (cu, &cudie,
6896 : : NULL, NULL, NULL, NULL,
6897 : : NULL, NULL) == NULL)
6898 : 0 : fprintf (out, _("\n Unknown CU base: "));
6899 : : else
6900 : 58 : fprintf (out, _("\n CU [%6" PRIx64 "] base: "),
6901 : : dwarf_dieoffset (&cudie));
6902 : 58 : print_dwarf_addr (dwflmod, address_size, base, base, out);
6903 : 58 : fprintf (out, "\n");
6904 : : }
6905 : 152 : last_cu = cu;
6906 : :
6907 [ - + ]: 152 : if (unlikely (data->d_size - offset < (size_t) address_size * 2))
6908 : : {
6909 : 0 : fprintf (out, _(" [%6tx] <INVALID DATA>\n"), offset);
6910 : 0 : break;
6911 : : }
6912 : :
6913 : 152 : Dwarf_Addr begin;
6914 : 152 : Dwarf_Addr end;
6915 [ + + ]: 152 : if (address_size == 8)
6916 : : {
6917 [ - + ]: 120 : begin = read_8ubyte_unaligned_inc (dbg, readp);
6918 [ - + ]: 120 : end = read_8ubyte_unaligned_inc (dbg, readp);
6919 : : }
6920 : : else
6921 : : {
6922 [ - + ]: 32 : begin = read_4ubyte_unaligned_inc (dbg, readp);
6923 [ - + ]: 32 : end = read_4ubyte_unaligned_inc (dbg, readp);
6924 [ + - ]: 32 : if (begin == (Dwarf_Addr) (uint32_t) -1)
6925 : : begin = (Dwarf_Addr) -1l;
6926 : : }
6927 : :
6928 [ - + ]: 152 : if (begin == (Dwarf_Addr) -1l) /* Base address entry. */
6929 : : {
6930 [ # # ]: 0 : if (first)
6931 : 0 : fprintf (out, " [%6tx] ", offset);
6932 : : else
6933 : 0 : fprintf (out, " ");
6934 : 0 : fputs (_("base address\n"), out);
6935 : 0 : fprintf (out, " ");
6936 : 0 : print_dwarf_addr (dwflmod, address_size, end, end, out);
6937 : 0 : fprintf (out, "\n");
6938 : 0 : base = end;
6939 : 0 : first = false;
6940 : : }
6941 [ + + ]: 152 : else if (begin == 0 && end == 0) /* End of list entry. */
6942 : : {
6943 [ - + ]: 60 : if (first)
6944 : 0 : fprintf (out, _(" [%6tx] empty list\n"), offset);
6945 : : first = true;
6946 : : }
6947 : : else
6948 : : {
6949 : : /* We have an address range entry. */
6950 [ + + ]: 92 : if (first) /* First address range entry in a list. */
6951 : 60 : fprintf (out, " [%6tx] ", offset);
6952 : : else
6953 : 32 : fprintf (out, " ");
6954 : :
6955 : 92 : fprintf (out, "range %" PRIx64 ", %" PRIx64 "\n", begin, end);
6956 [ + + ]: 92 : if (! print_unresolved_addresses)
6957 : : {
6958 : 72 : fprintf (out, " ");
6959 : 72 : print_dwarf_addr (dwflmod, address_size, base + begin,
6960 : : base + begin, out);
6961 : 72 : fprintf (out, "..\n ");
6962 : 72 : print_dwarf_addr (dwflmod, address_size,
6963 : : base + end - 1, base + end, out);
6964 : 72 : fprintf (out, "\n");
6965 : : }
6966 : :
6967 : : first = false;
6968 : : }
6969 : : }
6970 : : }
6971 : :
6972 : : #define REGNAMESZ 16
6973 : : static const char *
6974 : 39858 : register_info (Ebl *ebl, unsigned int regno, const Ebl_Register_Location *loc,
6975 : : char name[REGNAMESZ], int *bits, int *type)
6976 : : {
6977 : 39858 : const char *set;
6978 : 39858 : const char *pfx;
6979 : 39858 : int ignore;
6980 [ + + + + ]: 116182 : ssize_t n = ebl_register_info (ebl, regno, name, REGNAMESZ, &pfx, &set,
6981 : : bits ?: &ignore, type ?: &ignore);
6982 [ - + ]: 39858 : if (n <= 0)
6983 : : {
6984 [ # # ]: 0 : if (loc != NULL)
6985 : 0 : snprintf (name, REGNAMESZ, "reg%u", loc->regno);
6986 : : else
6987 : 0 : snprintf (name, REGNAMESZ, "??? 0x%x", regno);
6988 [ # # ]: 0 : if (bits != NULL)
6989 [ # # ]: 0 : *bits = loc != NULL ? loc->bits : 0;
6990 [ # # ]: 0 : if (type != NULL)
6991 : 0 : *type = DW_ATE_unsigned;
6992 : 0 : set = "??? unrecognized";
6993 : : }
6994 : : else
6995 : : {
6996 [ + + - + ]: 39858 : if (bits != NULL && *bits <= 0)
6997 [ # # ]: 0 : *bits = loc != NULL ? loc->bits : 0;
6998 [ + + - + ]: 39858 : if (type != NULL && *type == DW_ATE_void)
6999 : 0 : *type = DW_ATE_unsigned;
7000 : :
7001 : : }
7002 : 39858 : return set;
7003 : : }
7004 : :
7005 : : static const unsigned char *
7006 : 280 : read_encoded (unsigned int encoding, const unsigned char *readp,
7007 : : const unsigned char *const endp, uint64_t *res, Dwarf *dbg)
7008 : : {
7009 [ - + ]: 280 : if ((encoding & 0xf) == DW_EH_PE_absptr)
7010 : 0 : encoding = gelf_getclass (dbg->elf) == ELFCLASS32
7011 [ # # ]: 0 : ? DW_EH_PE_udata4 : DW_EH_PE_udata8;
7012 : :
7013 [ - - - + : 280 : switch (encoding & 0xf)
- - + -
- ]
7014 : : {
7015 : 0 : case DW_EH_PE_uleb128:
7016 [ # # ]: 0 : if (readp >= endp)
7017 : 0 : goto invalid;
7018 : 0 : get_uleb128 (*res, readp, endp);
7019 : 0 : break;
7020 : 0 : case DW_EH_PE_sleb128:
7021 [ # # ]: 0 : if (readp >= endp)
7022 : 0 : goto invalid;
7023 : 0 : get_sleb128 (*res, readp, endp);
7024 : 0 : break;
7025 : 0 : case DW_EH_PE_udata2:
7026 [ # # ]: 0 : if (readp + 2 > endp)
7027 : 0 : goto invalid;
7028 [ # # ]: 0 : *res = read_2ubyte_unaligned_inc (dbg, readp);
7029 : 0 : break;
7030 : 140 : case DW_EH_PE_udata4:
7031 [ - + ]: 140 : if (readp + 4 > endp)
7032 : 0 : goto invalid;
7033 [ - + ]: 140 : *res = read_4ubyte_unaligned_inc (dbg, readp);
7034 : 140 : break;
7035 : 0 : case DW_EH_PE_udata8:
7036 [ # # ]: 0 : if (readp + 8 > endp)
7037 : 0 : goto invalid;
7038 [ # # ]: 0 : *res = read_8ubyte_unaligned_inc (dbg, readp);
7039 : 0 : break;
7040 : 0 : case DW_EH_PE_sdata2:
7041 [ # # ]: 0 : if (readp + 2 > endp)
7042 : 0 : goto invalid;
7043 [ # # ]: 0 : *res = read_2sbyte_unaligned_inc (dbg, readp);
7044 : 0 : break;
7045 : 140 : case DW_EH_PE_sdata4:
7046 [ - + ]: 140 : if (readp + 4 > endp)
7047 : 0 : goto invalid;
7048 [ - + ]: 140 : *res = read_4sbyte_unaligned_inc (dbg, readp);
7049 : 140 : break;
7050 : 0 : case DW_EH_PE_sdata8:
7051 [ # # ]: 0 : if (readp + 8 > endp)
7052 : 0 : goto invalid;
7053 [ # # ]: 0 : *res = read_8sbyte_unaligned_inc (dbg, readp);
7054 : 0 : break;
7055 : : default:
7056 : 0 : invalid:
7057 : 0 : error (1, 0,
7058 : 0 : _("invalid encoding"));
7059 : : }
7060 : :
7061 : 280 : return readp;
7062 : : }
7063 : :
7064 : : static const char *
7065 : 38162 : regname (Ebl *ebl, unsigned int regno, char *regnamebuf)
7066 : : {
7067 : 38162 : register_info (ebl, regno, NULL, regnamebuf, NULL, NULL);
7068 : :
7069 : 38162 : return regnamebuf;
7070 : : }
7071 : :
7072 : : static void
7073 : 24482 : print_cfa_program (const unsigned char *readp, const unsigned char *const endp,
7074 : : Dwarf_Word vma_base, unsigned int code_align,
7075 : : int data_align,
7076 : : unsigned int version, unsigned int ptr_size,
7077 : : unsigned int encoding,
7078 : : Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr, Dwarf *dbg,
7079 : : FILE *out)
7080 : : {
7081 : 24482 : char regnamebuf[REGNAMESZ];
7082 : :
7083 : 24482 : fputs ("\n Program:\n", out);
7084 : 24482 : Dwarf_Word pc = vma_base;
7085 [ + + ]: 358280 : while (readp < endp)
7086 : : {
7087 : 333798 : unsigned int opcode = *readp++;
7088 : :
7089 [ + + ]: 333798 : if (opcode < DW_CFA_advance_loc)
7090 : : /* Extended opcode. */
7091 [ + - + + : 198800 : switch (opcode)
+ + - + -
- + + + +
+ + - + -
- - - - -
+ - - ]
7092 : : {
7093 : 68086 : uint64_t op1;
7094 : 68086 : int64_t sop1;
7095 : 68086 : uint64_t op2;
7096 : 68086 : int64_t sop2;
7097 : :
7098 : 68086 : case DW_CFA_nop:
7099 : 68086 : fputs (" nop\n", out);
7100 : 266886 : break;
7101 : 0 : case DW_CFA_set_loc:
7102 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
7103 : 0 : goto invalid;
7104 : 0 : readp = read_encoded (encoding, readp, endp, &op1, dbg);
7105 : 0 : fprintf (out, " set_loc %#" PRIx64 " to %#" PRIx64 "\n",
7106 : 0 : op1, pc = vma_base + op1);
7107 : 0 : break;
7108 : 6840 : case DW_CFA_advance_loc1:
7109 [ - + ]: 6840 : if ((uint64_t) (endp - readp) < 1)
7110 : 0 : goto invalid;
7111 : 13680 : fprintf (out, " advance_loc1 %u to %#" PRIx64 "\n",
7112 : 6840 : *readp, pc += *readp * code_align);
7113 : 6840 : ++readp;
7114 : 6840 : break;
7115 : 2896 : case DW_CFA_advance_loc2:
7116 [ - + ]: 2896 : if ((uint64_t) (endp - readp) < 2)
7117 : 0 : goto invalid;
7118 [ - + ]: 2896 : op1 = read_2ubyte_unaligned_inc (dbg, readp);
7119 : 2896 : fprintf (out, " advance_loc2 %" PRIu64 " to %#" PRIx64 "\n",
7120 : 2896 : op1, pc += op1 * code_align);
7121 : 2896 : break;
7122 : 36 : case DW_CFA_advance_loc4:
7123 [ - + ]: 36 : if ((uint64_t) (endp - readp) < 4)
7124 : 0 : goto invalid;
7125 [ + - ]: 36 : op1 = read_4ubyte_unaligned_inc (dbg, readp);
7126 : 36 : fprintf (out, " advance_loc4 %" PRIu64 " to %#" PRIx64 "\n",
7127 : 36 : op1, pc += op1 * code_align);
7128 : 36 : break;
7129 : 8 : case DW_CFA_offset_extended:
7130 [ - + ]: 8 : if ((uint64_t) (endp - readp) < 1)
7131 : 0 : goto invalid;
7132 : 8 : get_uleb128 (op1, readp, endp);
7133 [ - + ]: 8 : if ((uint64_t) (endp - readp) < 1)
7134 : 0 : goto invalid;
7135 : 8 : get_uleb128 (op2, readp, endp);
7136 : 8 : fprintf (out, " offset_extended r%" PRIu64 " (%s) at cfa%+" PRId64
7137 : : "\n",
7138 : : op1, regname (ebl, op1, regnamebuf), op2 * data_align);
7139 : 8 : break;
7140 : 0 : case DW_CFA_restore_extended:
7141 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
7142 : 0 : goto invalid;
7143 : 0 : get_uleb128 (op1, readp, endp);
7144 : 0 : fprintf (out, " restore_extended r%" PRIu64 " (%s)\n",
7145 : : op1, regname (ebl, op1, regnamebuf));
7146 : 0 : break;
7147 : 110 : case DW_CFA_undefined:
7148 [ - + ]: 110 : if ((uint64_t) (endp - readp) < 1)
7149 : 0 : goto invalid;
7150 : 110 : get_uleb128 (op1, readp, endp);
7151 : 110 : fprintf (out, " undefined r%" PRIu64 " (%s)\n", op1,
7152 : : regname (ebl, op1, regnamebuf));
7153 : 110 : break;
7154 : 0 : case DW_CFA_same_value:
7155 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
7156 : 0 : goto invalid;
7157 : 0 : get_uleb128 (op1, readp, endp);
7158 : 0 : fprintf (out, " same_value r%" PRIu64 " (%s)\n", op1,
7159 : : regname (ebl, op1, regnamebuf));
7160 : 0 : break;
7161 : 0 : case DW_CFA_register:
7162 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
7163 : 0 : goto invalid;
7164 : 0 : get_uleb128 (op1, readp, endp);
7165 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
7166 : 0 : goto invalid;
7167 : 0 : get_uleb128 (op2, readp, endp);
7168 : 0 : fprintf (out,
7169 : : " register r%" PRIu64 " (%s) in r%" PRIu64 " (%s)\n",
7170 : : op1, regname (ebl, op1, regnamebuf), op2,
7171 : : regname (ebl, op2, regnamebuf));
7172 : 0 : break;
7173 : 11966 : case DW_CFA_remember_state:
7174 : 11966 : fputs (" remember_state\n", out);
7175 : 11966 : break;
7176 : 11966 : case DW_CFA_restore_state:
7177 : 11966 : fputs (" restore_state\n", out);
7178 : 11966 : break;
7179 : 418 : case DW_CFA_def_cfa:
7180 [ - + ]: 418 : if ((uint64_t) (endp - readp) < 1)
7181 : 0 : goto invalid;
7182 : 418 : get_uleb128 (op1, readp, endp);
7183 [ - + ]: 418 : if ((uint64_t) (endp - readp) < 1)
7184 : 0 : goto invalid;
7185 : 418 : get_uleb128 (op2, readp, endp);
7186 : 418 : fprintf (out,
7187 : : " def_cfa r%" PRIu64 " (%s) at offset %" PRIu64 "\n",
7188 : : op1, regname (ebl, op1, regnamebuf), op2);
7189 : 418 : break;
7190 : 126 : case DW_CFA_def_cfa_register:
7191 [ - + ]: 126 : if ((uint64_t) (endp - readp) < 1)
7192 : 0 : goto invalid;
7193 : 126 : get_uleb128 (op1, readp, endp);
7194 : 126 : fprintf (out, " def_cfa_register r%" PRIu64 " (%s)\n",
7195 : : op1, regname (ebl, op1, regnamebuf));
7196 : 126 : break;
7197 : 96198 : case DW_CFA_def_cfa_offset:
7198 [ - + ]: 96198 : if ((uint64_t) (endp - readp) < 1)
7199 : 0 : goto invalid;
7200 : 96198 : get_uleb128 (op1, readp, endp);
7201 : 96198 : fprintf (out, " def_cfa_offset %" PRIu64 "\n", op1);
7202 : 96198 : break;
7203 : 118 : case DW_CFA_def_cfa_expression:
7204 [ - + ]: 118 : if ((uint64_t) (endp - readp) < 1)
7205 : 0 : goto invalid;
7206 : 118 : get_uleb128 (op1, readp, endp); /* Length of DW_FORM_block. */
7207 : 118 : fprintf (out, " def_cfa_expression %" PRIu64 "\n", op1);
7208 [ - + ]: 118 : if ((uint64_t) (endp - readp) < op1)
7209 : : {
7210 : 0 : invalid:
7211 : 0 : fputs (_(" <INVALID DATA>\n"), out);
7212 : 0 : return;
7213 : : }
7214 : 118 : print_ops (dwflmod, dbg, 10, 10, version, ptr_size, 0, NULL,
7215 : : op1, readp, out);
7216 : 118 : readp += op1;
7217 : 118 : break;
7218 : 0 : case DW_CFA_expression:
7219 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
7220 : 0 : goto invalid;
7221 : 0 : get_uleb128 (op1, readp, endp);
7222 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
7223 : 0 : goto invalid;
7224 : 0 : get_uleb128 (op2, readp, endp); /* Length of DW_FORM_block. */
7225 : 0 : fprintf (out, " expression r%" PRIu64 " (%s) \n",
7226 : : op1, regname (ebl, op1, regnamebuf));
7227 [ # # ]: 0 : if ((uint64_t) (endp - readp) < op2)
7228 : 0 : goto invalid;
7229 : 0 : print_ops (dwflmod, dbg, 10, 10, version, ptr_size, 0, NULL,
7230 : : op2, readp, out);
7231 : 0 : readp += op2;
7232 : 0 : break;
7233 : 24 : case DW_CFA_offset_extended_sf:
7234 [ - + ]: 24 : if ((uint64_t) (endp - readp) < 1)
7235 : 0 : goto invalid;
7236 : 24 : get_uleb128 (op1, readp, endp);
7237 [ - + ]: 24 : if ((uint64_t) (endp - readp) < 1)
7238 : 0 : goto invalid;
7239 : 24 : get_sleb128 (sop2, readp, endp);
7240 : 24 : fprintf (out, " offset_extended_sf r%" PRIu64 " (%s) at cfa%+"
7241 : : PRId64 "\n",
7242 : : op1, regname (ebl, op1, regnamebuf), sop2 * data_align);
7243 : 24 : break;
7244 : 0 : case DW_CFA_def_cfa_sf:
7245 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
7246 : 0 : goto invalid;
7247 : 0 : get_uleb128 (op1, readp, endp);
7248 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
7249 : 0 : goto invalid;
7250 : 0 : get_sleb128 (sop2, readp, endp);
7251 : 0 : fprintf (out,
7252 : : " def_cfa_sf r%" PRIu64 " (%s) at offset %" PRId64 "\n",
7253 : : op1, regname (ebl, op1, regnamebuf), sop2 * data_align);
7254 : 0 : break;
7255 : 0 : case DW_CFA_def_cfa_offset_sf:
7256 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
7257 : 0 : goto invalid;
7258 : 0 : get_sleb128 (sop1, readp, endp);
7259 : 0 : fprintf (out,
7260 : : " def_cfa_offset_sf %" PRId64 "\n", sop1 * data_align);
7261 : 0 : break;
7262 : 0 : case DW_CFA_val_offset:
7263 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
7264 : 0 : goto invalid;
7265 : 0 : get_uleb128 (op1, readp, endp);
7266 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
7267 : 0 : goto invalid;
7268 : 0 : get_uleb128 (op2, readp, endp);
7269 : 0 : fprintf (out, " val_offset %" PRIu64 " at offset %" PRIu64 "\n",
7270 : : op1, op2 * data_align);
7271 : 0 : break;
7272 : 0 : case DW_CFA_val_offset_sf:
7273 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
7274 : 0 : goto invalid;
7275 : 0 : get_uleb128 (op1, readp, endp);
7276 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
7277 : 0 : goto invalid;
7278 : 0 : get_sleb128 (sop2, readp, endp);
7279 : 0 : fprintf (out,
7280 : : " val_offset_sf %" PRIu64 " at offset %" PRId64 "\n",
7281 : : op1, sop2 * data_align);
7282 : 0 : break;
7283 : 0 : case DW_CFA_val_expression:
7284 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
7285 : 0 : goto invalid;
7286 : 0 : get_uleb128 (op1, readp, endp);
7287 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
7288 : 0 : goto invalid;
7289 : 0 : get_uleb128 (op2, readp, endp); /* Length of DW_FORM_block. */
7290 : 0 : fprintf (out, " val_expression r%" PRIu64 " (%s)\n",
7291 : : op1, regname (ebl, op1, regnamebuf));
7292 [ # # ]: 0 : if ((uint64_t) (endp - readp) < op2)
7293 : 0 : goto invalid;
7294 : 0 : print_ops (dwflmod, dbg, 10, 10, version, ptr_size, 0,
7295 : : NULL, op2, readp, out);
7296 : 0 : readp += op2;
7297 : 0 : break;
7298 : 0 : case DW_CFA_MIPS_advance_loc8:
7299 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 8)
7300 : 0 : goto invalid;
7301 [ # # ]: 0 : op1 = read_8ubyte_unaligned_inc (dbg, readp);
7302 : 0 : fprintf (out,
7303 : : " MIPS_advance_loc8 %" PRIu64 " to %#" PRIx64 "\n",
7304 : 0 : op1, pc += op1 * code_align);
7305 : 0 : break;
7306 : 8 : case DW_CFA_GNU_window_save: /* DW_CFA_AARCH64_negate_ra_state */
7307 [ + - ]: 8 : if (ehdr->e_machine == EM_AARCH64)
7308 : 8 : fputs (" AARCH64_negate_ra_state\n", out);
7309 : : else
7310 : 0 : fputs (" GNU_window_save\n", out);
7311 : : break;
7312 : 0 : case DW_CFA_GNU_args_size:
7313 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
7314 : 0 : goto invalid;
7315 : 0 : get_uleb128 (op1, readp, endp);
7316 : 0 : fprintf (out, " args_size %" PRIu64 "\n", op1);
7317 : 0 : break;
7318 : : default:
7319 : 0 : fprintf (out, " ??? (%u)\n", opcode);
7320 : 0 : break;
7321 : : }
7322 [ + + ]: 134998 : else if (opcode < DW_CFA_offset)
7323 : 97522 : fprintf (out, " advance_loc %u to %#" PRIx64 "\n",
7324 : 97522 : opcode & 0x3f, pc += (opcode & 0x3f) * code_align);
7325 [ + + ]: 37476 : else if (opcode < DW_CFA_restore)
7326 : : {
7327 : 34034 : uint64_t offset;
7328 [ - + ]: 34034 : if ((uint64_t) (endp - readp) < 1)
7329 : 0 : goto invalid;
7330 : 34034 : get_uleb128 (offset, readp, endp);
7331 : 34034 : fprintf (out, " offset r%u (%s) at cfa%+" PRId64 "\n",
7332 : : opcode & 0x3f, regname (ebl, opcode & 0x3f, regnamebuf),
7333 : : offset * data_align);
7334 : : }
7335 : : else
7336 : 3442 : fprintf (out, " restore r%u (%s)\n",
7337 : : opcode & 0x3f, regname (ebl, opcode & 0x3f, regnamebuf));
7338 : : }
7339 : : }
7340 : :
7341 : :
7342 : : static unsigned int
7343 : 24172 : encoded_ptr_size (int encoding, unsigned int ptr_size)
7344 : : {
7345 [ - + - + ]: 24172 : switch (encoding & 7)
7346 : : {
7347 : : case DW_EH_PE_udata4:
7348 : : return 4;
7349 : 0 : case DW_EH_PE_udata8:
7350 : 0 : return 8;
7351 : : case 0:
7352 : : return ptr_size;
7353 : : }
7354 : :
7355 : 0 : fprintf (stderr, "Unsupported pointer encoding: %#x, "
7356 : : "assuming pointer size of %d.\n", encoding, ptr_size);
7357 : 0 : return ptr_size;
7358 : : }
7359 : :
7360 : :
7361 : : static unsigned int
7362 : 642 : print_encoding (unsigned int val, FILE *out)
7363 : : {
7364 [ - - - + : 642 : switch (val & 0xf)
- - - + -
- ]
7365 : : {
7366 : 0 : case DW_EH_PE_absptr:
7367 : 0 : fputs ("absptr", out);
7368 : 0 : break;
7369 : 0 : case DW_EH_PE_uleb128:
7370 : 0 : fputs ("uleb128", out);
7371 : 0 : break;
7372 : 0 : case DW_EH_PE_udata2:
7373 : 0 : fputs ("udata2", out);
7374 : 0 : break;
7375 : 140 : case DW_EH_PE_udata4:
7376 : 140 : fputs ("udata4", out);
7377 : 140 : break;
7378 : 0 : case DW_EH_PE_udata8:
7379 : 0 : fputs ("udata8", out);
7380 : 0 : break;
7381 : 0 : case DW_EH_PE_sleb128:
7382 : 0 : fputs ("sleb128", out);
7383 : 0 : break;
7384 : 0 : case DW_EH_PE_sdata2:
7385 : 0 : fputs ("sdata2", out);
7386 : 0 : break;
7387 : 502 : case DW_EH_PE_sdata4:
7388 : 502 : fputs ("sdata4", out);
7389 : 502 : break;
7390 : 0 : case DW_EH_PE_sdata8:
7391 : 0 : fputs ("sdata8", out);
7392 : 0 : break;
7393 : : default:
7394 : : /* We did not use any of the bits after all. */
7395 : : return val;
7396 : : }
7397 : :
7398 : 642 : return val & ~0xf;
7399 : : }
7400 : :
7401 : :
7402 : : static unsigned int
7403 : 502 : print_relinfo (unsigned int val, FILE *out)
7404 : : {
7405 [ + - + - : 502 : switch (val & 0x70)
- - ]
7406 : : {
7407 : 362 : case DW_EH_PE_pcrel:
7408 : 362 : fputs ("pcrel", out);
7409 : 362 : break;
7410 : 0 : case DW_EH_PE_textrel:
7411 : 0 : fputs ("textrel", out);
7412 : 0 : break;
7413 : 140 : case DW_EH_PE_datarel:
7414 : 140 : fputs ("datarel", out);
7415 : 140 : break;
7416 : 0 : case DW_EH_PE_funcrel:
7417 : 0 : fputs ("funcrel", out);
7418 : 0 : break;
7419 : 0 : case DW_EH_PE_aligned:
7420 : 0 : fputs ("aligned", out);
7421 : 0 : break;
7422 : : default:
7423 : : return val;
7424 : : }
7425 : :
7426 : 502 : return val & ~0x70;
7427 : : }
7428 : :
7429 : :
7430 : : static void
7431 : 642 : print_encoding_base (const char *pfx, unsigned int fde_encoding,
7432 : : FILE *out)
7433 : : {
7434 : 642 : fprintf (out, "(%s", pfx);
7435 : :
7436 [ - + ]: 642 : if (fde_encoding == DW_EH_PE_omit)
7437 : 0 : fputs ("omit)\n", out);
7438 : : else
7439 : : {
7440 : 642 : unsigned int w = fde_encoding;
7441 : :
7442 : 642 : w = print_encoding (w, out);
7443 : :
7444 [ + + ]: 642 : if (w & 0x70)
7445 : : {
7446 [ + - ]: 502 : if (w != fde_encoding)
7447 : 502 : fputc (' ', out);
7448 : :
7449 : 502 : w = print_relinfo (w, out);
7450 : : }
7451 : :
7452 [ - + ]: 642 : if (w != 0)
7453 [ # # ]: 0 : fprintf (out, "%s%x", w != fde_encoding ? " " : "", w);
7454 : :
7455 : 642 : fputs (")\n", out);
7456 : : }
7457 : 642 : }
7458 : :
7459 : :
7460 : : static void
7461 : 196 : print_debug_frame_section (Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr,
7462 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg,
7463 : : FILE *out)
7464 : : {
7465 : 196 : size_t shstrndx;
7466 : : /* We know this call will succeed since it did in the caller. */
7467 : 196 : (void) elf_getshdrstrndx (ebl->elf, &shstrndx);
7468 : 196 : const char *scnname = section_name (ebl, shdr);
7469 : :
7470 : : /* Needed if we find PC-relative addresses. */
7471 : 196 : GElf_Addr bias;
7472 [ - + ]: 196 : if (dwfl_module_getelf (dwflmod, &bias) == NULL)
7473 : : {
7474 : 0 : error (0, 0, _("cannot get ELF: %s"), dwfl_errmsg (-1));
7475 : 0 : return;
7476 : : }
7477 : :
7478 : 196 : bool is_eh_frame = strcmp (scnname, ".eh_frame") == 0;
7479 : 196 : Elf_Data *data;
7480 [ + + ]: 196 : if (is_eh_frame)
7481 : : {
7482 : 144 : data = elf_rawdata (scn, NULL);
7483 [ - + ]: 144 : if (data == NULL)
7484 : : {
7485 : 0 : error (0, 0, _("cannot get %s content: %s"),
7486 : : scnname, elf_errmsg (-1));
7487 : 0 : return;
7488 : : }
7489 : : }
7490 : : else
7491 : : {
7492 : 52 : data = get_debug_elf_data (dbg, ebl, IDX_debug_frame, scn);
7493 [ + - ]: 52 : if (data == NULL)
7494 : : return;
7495 : : }
7496 : :
7497 [ + + ]: 196 : if (is_eh_frame)
7498 : 144 : fprintf (out, _("\
7499 : : \nCall frame information section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
7500 : 144 : elf_ndxscn (scn), scnname, (uint64_t) shdr->sh_offset);
7501 : : else
7502 : 52 : fprintf (out, _("\
7503 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
7504 : 52 : elf_ndxscn (scn), scnname, (uint64_t) shdr->sh_offset);
7505 : :
7506 : 196 : struct cieinfo
7507 : : {
7508 : : ptrdiff_t cie_offset;
7509 : : const char *augmentation;
7510 : : unsigned int code_alignment_factor;
7511 : : unsigned int data_alignment_factor;
7512 : : uint8_t address_size;
7513 : : uint8_t fde_encoding;
7514 : : uint8_t lsda_encoding;
7515 : : struct cieinfo *next;
7516 : 196 : } *cies = NULL;
7517 : :
7518 : 196 : const unsigned char *readp = data->d_buf;
7519 : 196 : const unsigned char *const dataend = ((unsigned char *) data->d_buf
7520 : 196 : + data->d_size);
7521 [ + + ]: 24818 : while (readp < dataend)
7522 : : {
7523 [ - + ]: 24622 : if (unlikely (readp + 4 > dataend))
7524 : : {
7525 : 0 : invalid_data:
7526 : 0 : error (0, 0, _("invalid data in section [%zu] '%s'"),
7527 : : elf_ndxscn (scn), scnname);
7528 : 0 : return;
7529 : : }
7530 : :
7531 : : /* At the beginning there must be a CIE. There can be multiple,
7532 : : hence we test tis in a loop. */
7533 : 24622 : ptrdiff_t offset = readp - (unsigned char *) data->d_buf;
7534 : :
7535 [ + + ]: 24622 : Dwarf_Word unit_length = read_4ubyte_unaligned_inc (dbg, readp);
7536 : 24622 : unsigned int length = 4;
7537 [ - + ]: 24622 : if (unlikely (unit_length == 0xffffffff))
7538 : : {
7539 [ # # ]: 0 : if (unlikely (readp + 8 > dataend))
7540 : 0 : goto invalid_data;
7541 : :
7542 [ # # ]: 0 : unit_length = read_8ubyte_unaligned_inc (dbg, readp);
7543 : 0 : length = 8;
7544 : : }
7545 : :
7546 [ + + ]: 24622 : if (unlikely (unit_length == 0))
7547 : : {
7548 : 140 : fprintf (out, _("\n [%6tx] Zero terminator\n"), offset);
7549 : 140 : continue;
7550 : : }
7551 : :
7552 : 24482 : Dwarf_Word maxsize = dataend - readp;
7553 [ - + ]: 24482 : if (unlikely (unit_length > maxsize))
7554 : 0 : goto invalid_data;
7555 : :
7556 [ + + ]: 24482 : unsigned int ptr_size = ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 4 : 8;
7557 : :
7558 : 24482 : ptrdiff_t start = readp - (unsigned char *) data->d_buf;
7559 : 24482 : const unsigned char *const cieend = readp + unit_length;
7560 [ - + ]: 24482 : if (unlikely (cieend > dataend))
7561 : 0 : goto invalid_data;
7562 : :
7563 : 24482 : Dwarf_Off cie_id;
7564 [ + - ]: 24482 : if (length == 4)
7565 : : {
7566 [ - + ]: 24482 : if (unlikely (cieend - readp < 4))
7567 : 0 : goto invalid_data;
7568 [ + + ]: 24482 : cie_id = read_4ubyte_unaligned_inc (dbg, readp);
7569 [ + + ]: 24482 : if (!is_eh_frame && cie_id == DW_CIE_ID_32)
7570 : : cie_id = DW_CIE_ID_64;
7571 : : }
7572 : : else
7573 : : {
7574 [ # # ]: 0 : if (unlikely (cieend - readp < 8))
7575 : 0 : goto invalid_data;
7576 [ # # ]: 0 : cie_id = read_8ubyte_unaligned_inc (dbg, readp);
7577 : : }
7578 : :
7579 : 48880 : uint_fast8_t version = 2;
7580 : 24398 : unsigned int code_alignment_factor;
7581 : 24398 : int data_alignment_factor;
7582 : 48880 : unsigned int fde_encoding = 0;
7583 : 48880 : unsigned int lsda_encoding = 0;
7584 : 48880 : Dwarf_Word initial_location = 0;
7585 : 48880 : Dwarf_Word vma_base = 0;
7586 : :
7587 [ + + + + ]: 24574 : if (cie_id == (is_eh_frame ? 0 : DW_CIE_ID_64))
7588 : : {
7589 [ - + ]: 310 : if (unlikely (cieend - readp < 2))
7590 : 0 : goto invalid_data;
7591 : 310 : version = *readp++;
7592 : 310 : const char *const augmentation = (const char *) readp;
7593 : 310 : readp = memchr (readp, '\0', cieend - readp);
7594 [ - + ]: 310 : if (unlikely (readp == NULL))
7595 : 0 : goto invalid_data;
7596 : 310 : ++readp;
7597 : :
7598 : 310 : uint_fast8_t segment_size = 0;
7599 [ + + ]: 310 : if (version >= 4)
7600 : : {
7601 [ - + ]: 8 : if (cieend - readp < 5)
7602 : 0 : goto invalid_data;
7603 : 8 : ptr_size = *readp++;
7604 : 8 : segment_size = *readp++;
7605 : : }
7606 : :
7607 [ - + ]: 310 : if (cieend - readp < 1)
7608 : 0 : goto invalid_data;
7609 : 310 : get_uleb128 (code_alignment_factor, readp, cieend);
7610 [ - + ]: 310 : if (cieend - readp < 1)
7611 : 0 : goto invalid_data;
7612 : 310 : get_sleb128 (data_alignment_factor, readp, cieend);
7613 : :
7614 : : /* In some variant for unwind data there is another field. */
7615 [ - + ]: 310 : if (strcmp (augmentation, "eh") == 0)
7616 [ # # ]: 0 : readp += ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 4 : 8;
7617 : :
7618 : 310 : unsigned int return_address_register;
7619 [ - + ]: 310 : if (cieend - readp < 1)
7620 : 0 : goto invalid_data;
7621 [ + + ]: 310 : if (unlikely (version == 1))
7622 : 286 : return_address_register = *readp++;
7623 : : else
7624 : 24 : get_uleb128 (return_address_register, readp, cieend);
7625 : :
7626 : 310 : fprintf (out, "\n [%6tx] CIE length=%" PRIu64 "\n"
7627 : : " CIE_id: %" PRIu64 "\n"
7628 : : " version: %u\n"
7629 : : " augmentation: \"%s\"\n",
7630 : : offset, (uint64_t) unit_length, (uint64_t) cie_id,
7631 : : version, augmentation);
7632 [ + + ]: 310 : if (version >= 4)
7633 : 8 : fprintf (out, " address_size: %u\n"
7634 : : " segment_size: %u\n",
7635 : : ptr_size, segment_size);
7636 : 310 : fprintf (out, " code_alignment_factor: %u\n"
7637 : : " data_alignment_factor: %d\n"
7638 : : " return_address_register: %u\n",
7639 : : code_alignment_factor,
7640 : : data_alignment_factor, return_address_register);
7641 : :
7642 [ + + ]: 310 : if (augmentation[0] == 'z')
7643 : : {
7644 [ - + ]: 222 : if (cieend - readp < 1)
7645 : 0 : goto invalid_data;
7646 : :
7647 : 222 : unsigned int augmentationlen;
7648 : 222 : get_uleb128 (augmentationlen, readp, cieend);
7649 : :
7650 [ - + ]: 222 : if (augmentationlen > (size_t) (cieend - readp))
7651 : : {
7652 : 0 : error (0, 0, _("invalid augmentation length"));
7653 : 0 : readp = cieend;
7654 : 0 : continue;
7655 : : }
7656 : :
7657 : 222 : const char *hdr = "Augmentation data:";
7658 : 222 : const char *cp = augmentation + 1;
7659 [ + + + - ]: 444 : while (*cp != '\0' && cp < augmentation + augmentationlen + 1)
7660 : : {
7661 : 222 : fprintf (out, " %-26s%#x ", hdr, *readp);
7662 : 222 : hdr = "";
7663 : :
7664 [ + - ]: 222 : if (*cp == 'R')
7665 : : {
7666 : 222 : fde_encoding = *readp++;
7667 : 222 : print_encoding_base (_("FDE address encoding: "),
7668 : : fde_encoding, out);
7669 : : }
7670 [ # # ]: 0 : else if (*cp == 'L')
7671 : : {
7672 : 0 : lsda_encoding = *readp++;
7673 : 0 : print_encoding_base (_("LSDA pointer encoding: "),
7674 : : lsda_encoding, out);
7675 : : }
7676 [ # # ]: 0 : else if (*cp == 'P')
7677 : : {
7678 : : /* Personality. This field usually has a relocation
7679 : : attached pointing to __gcc_personality_v0. */
7680 : 0 : const unsigned char *startp = readp;
7681 : 0 : unsigned int encoding = *readp++;
7682 : 0 : uint64_t val = 0;
7683 : 0 : readp = read_encoded (encoding, readp,
7684 : : readp - 1 + augmentationlen,
7685 : : &val, dbg);
7686 : :
7687 [ # # ]: 0 : while (++startp < readp)
7688 : 0 : fprintf (out, "%#x ", *startp);
7689 : :
7690 : 0 : fputc ('(', out);
7691 : 0 : print_encoding (encoding, out);
7692 : 0 : fputc (' ', out);
7693 [ # # ]: 0 : switch (encoding & 0xf)
7694 : : {
7695 : 0 : case DW_EH_PE_sleb128:
7696 : : case DW_EH_PE_sdata2:
7697 : : case DW_EH_PE_sdata4:
7698 : 0 : fprintf (out, "%" PRId64 ")\n", val);
7699 : 0 : break;
7700 : 0 : default:
7701 : 0 : fprintf (out, "%#" PRIx64 ")\n", val);
7702 : 0 : break;
7703 : : }
7704 : : }
7705 : : else
7706 : 0 : fprintf (out, "(%x)\n", *readp++);
7707 : :
7708 : 222 : ++cp;
7709 : : }
7710 : : }
7711 : :
7712 [ + - ]: 310 : if (likely (ptr_size == 4 || ptr_size == 8))
7713 : : {
7714 : 310 : struct cieinfo *newp = alloca (sizeof (*newp));
7715 : 310 : newp->cie_offset = offset;
7716 : 310 : newp->augmentation = augmentation;
7717 : 310 : newp->fde_encoding = fde_encoding;
7718 : 310 : newp->lsda_encoding = lsda_encoding;
7719 : 310 : newp->address_size = ptr_size;
7720 : 310 : newp->code_alignment_factor = code_alignment_factor;
7721 : 310 : newp->data_alignment_factor = data_alignment_factor;
7722 : 310 : newp->next = cies;
7723 : 310 : cies = newp;
7724 : : }
7725 : : }
7726 : : else
7727 : : {
7728 : : struct cieinfo *cie = cies;
7729 [ + - ]: 24172 : while (cie != NULL)
7730 [ + + - + ]: 48344 : if (is_eh_frame
7731 : 24080 : ? ((Dwarf_Off) start - cie_id) == (Dwarf_Off) cie->cie_offset
7732 : 92 : : cie_id == (Dwarf_Off) cie->cie_offset)
7733 : : break;
7734 : : else
7735 : 0 : cie = cie->next;
7736 : 24172 : if (unlikely (cie == NULL))
7737 : : {
7738 : 0 : fputs ("invalid CIE reference in FDE\n", out);
7739 : 0 : return;
7740 : : }
7741 : :
7742 : : /* Initialize from CIE data. */
7743 : 24172 : fde_encoding = cie->fde_encoding;
7744 : 24172 : lsda_encoding = cie->lsda_encoding;
7745 : 24172 : ptr_size = encoded_ptr_size (fde_encoding, cie->address_size);
7746 : 24172 : code_alignment_factor = cie->code_alignment_factor;
7747 : 24172 : data_alignment_factor = cie->data_alignment_factor;
7748 : :
7749 : 24172 : const unsigned char *base = readp;
7750 : : // XXX There are sometimes relocations for this value
7751 [ - + + + : 24172 : initial_location = read_addr_unaligned_inc (ptr_size, dbg, readp);
- + + + ]
7752 : 48344 : Dwarf_Word address_range
7753 [ + + - + : 24172 : = read_addr_unaligned_inc (ptr_size, dbg, readp);
+ + ]
7754 : :
7755 : : /* pcrel for an FDE address is relative to the runtime
7756 : : address of the start_address field itself. Sign extend
7757 : : if necessary to make sure the calculation is done on the
7758 : : full 64 bit address even when initial_location only holds
7759 : : the lower 32 bits. */
7760 : 24172 : Dwarf_Addr pc_start = initial_location;
7761 [ + + ]: 24172 : if (ptr_size == 4)
7762 : 24092 : pc_start = (uint64_t) (int32_t) pc_start;
7763 [ + + ]: 24172 : if ((fde_encoding & 0x70) == DW_EH_PE_pcrel)
7764 : 24068 : pc_start += ((uint64_t) shdr->sh_addr
7765 : 24068 : + (base - (const unsigned char *) data->d_buf)
7766 : 24068 : - bias);
7767 : :
7768 : 24172 : fprintf (out, "\n [%6tx] FDE length=%" PRIu64 " cie=[%6tx]\n"
7769 : : " CIE_pointer: %" PRIu64 "\n"
7770 : : " initial_location: ",
7771 : : offset, (uint64_t) unit_length,
7772 : : cie->cie_offset, (uint64_t) cie_id);
7773 : 24172 : print_dwarf_addr (dwflmod, cie->address_size,
7774 : : pc_start, initial_location, out);
7775 [ + + ]: 24172 : if ((fde_encoding & 0x70) == DW_EH_PE_pcrel)
7776 : : {
7777 : 48136 : vma_base = (((uint64_t) shdr->sh_offset
7778 : 24068 : + (base - (const unsigned char *) data->d_buf)
7779 : 24068 : + (uint64_t) initial_location)
7780 : : & (ptr_size == 4
7781 : : ? UINT64_C (0xffffffff)
7782 [ - + ]: 24068 : : UINT64_C (0xffffffffffffffff)));
7783 : 24068 : fprintf (out, _(" (offset: %#" PRIx64 ")"),
7784 : : (uint64_t) vma_base);
7785 : : }
7786 : :
7787 : 24172 : fprintf (out, "\n address_range: %#" PRIx64,
7788 : : (uint64_t) address_range);
7789 [ + + ]: 24172 : if ((fde_encoding & 0x70) == DW_EH_PE_pcrel)
7790 : 24068 : fprintf (out, _(" (end offset: %#" PRIx64 ")"),
7791 : 24068 : ((uint64_t) vma_base + (uint64_t) address_range)
7792 : : & (ptr_size == 4
7793 : : ? UINT64_C (0xffffffff)
7794 [ - + ]: 24068 : : UINT64_C (0xffffffffffffffff)));
7795 : 24172 : fputc ('\n', out);
7796 : :
7797 [ + + ]: 24172 : if (cie->augmentation[0] == 'z')
7798 : : {
7799 : 24068 : unsigned int augmentationlen;
7800 [ - + ]: 24068 : if (cieend - readp < 1)
7801 : 0 : goto invalid_data;
7802 : 24068 : get_uleb128 (augmentationlen, readp, cieend);
7803 : :
7804 [ - + ]: 24068 : if (augmentationlen > (size_t) (cieend - readp))
7805 : : {
7806 : 0 : error (0, 0, _("invalid augmentation length"));
7807 : 0 : readp = cieend;
7808 : 0 : continue;
7809 : : }
7810 : :
7811 [ - + ]: 24068 : if (augmentationlen > 0)
7812 : : {
7813 : 0 : const char *hdr = "Augmentation data:";
7814 : 0 : const char *cp = cie->augmentation + 1;
7815 : 0 : unsigned int u = 0;
7816 : 0 : while (*cp != '\0'
7817 [ # # # # ]: 0 : && cp < cie->augmentation + augmentationlen + 1)
7818 : : {
7819 [ # # ]: 0 : if (*cp == 'L')
7820 : : {
7821 : 0 : uint64_t lsda_pointer;
7822 : 0 : const unsigned char *p
7823 : 0 : = read_encoded (lsda_encoding, &readp[u],
7824 : : &readp[augmentationlen],
7825 : : &lsda_pointer, dbg);
7826 : 0 : u = p - readp;
7827 : 0 : fprintf (out, _("\
7828 : : %-26sLSDA pointer: %#" PRIx64 "\n"),
7829 : : hdr, lsda_pointer);
7830 : 0 : hdr = "";
7831 : : }
7832 : 0 : ++cp;
7833 : : }
7834 : :
7835 [ # # ]: 0 : while (u < augmentationlen)
7836 : : {
7837 : 0 : fprintf (out, " %-26s%#x\n", hdr, readp[u++]);
7838 : 0 : hdr = "";
7839 : : }
7840 : : }
7841 : :
7842 : 24068 : readp += augmentationlen;
7843 : : }
7844 : : }
7845 : :
7846 : : /* Handle the initialization instructions. */
7847 [ - + ]: 24482 : if (ptr_size != 4 && ptr_size !=8)
7848 : 0 : fprintf (out, "invalid CIE pointer size (%u), must be 4 or 8.\n",
7849 : : ptr_size);
7850 : : else
7851 : 24482 : print_cfa_program (readp, cieend, vma_base, code_alignment_factor,
7852 : : data_alignment_factor, version, ptr_size,
7853 : : fde_encoding, dwflmod, ebl, ehdr, dbg, out);
7854 : 24482 : readp = cieend;
7855 : : }
7856 : : }
7857 : :
7858 : :
7859 : : /* Returns the signedness (or false if it cannot be determined) and
7860 : : the byte size (or zero if it cannot be gotten) of the given DIE
7861 : : DW_AT_type attribute. Uses dwarf_peel_type and dwarf_aggregate_size. */
7862 : : static void
7863 : 353704 : die_type_sign_bytes (Dwarf_Die *die, bool *is_signed, int *bytes)
7864 : : {
7865 : 353704 : Dwarf_Attribute attr;
7866 : 353704 : Dwarf_Die type;
7867 : :
7868 : 353704 : *bytes = 0;
7869 : 353704 : *is_signed = false;
7870 : :
7871 [ + + ]: 353704 : if (dwarf_peel_type (dwarf_formref_die (dwarf_attr_integrate (die,
7872 : : DW_AT_type,
7873 : : &attr), &type),
7874 : : &type) == 0)
7875 : : {
7876 : 974 : Dwarf_Word val;
7877 : 974 : *is_signed = (dwarf_formudata (dwarf_attr (&type, DW_AT_encoding,
7878 : : &attr), &val) == 0
7879 [ + + + + ]: 974 : && (val == DW_ATE_signed || val == DW_ATE_signed_char));
7880 : :
7881 [ + - ]: 974 : if (dwarf_aggregate_size (&type, &val) == 0)
7882 : 974 : *bytes = val;
7883 : : }
7884 : 353704 : }
7885 : :
7886 : : struct attrcb_args
7887 : : {
7888 : : Dwfl_Module *dwflmod;
7889 : : Dwarf *dbg;
7890 : : Dwarf_Die *dies;
7891 : : int level;
7892 : : bool silent;
7893 : : bool is_split;
7894 : : unsigned int version;
7895 : : unsigned int addrsize;
7896 : : unsigned int offset_size;
7897 : : struct Dwarf_CU *cu;
7898 : : FILE *out;
7899 : : };
7900 : :
7901 : :
7902 : : static int
7903 : 8171008 : attr_callback (Dwarf_Attribute *attrp, void *arg)
7904 : : {
7905 : 8171008 : struct attrcb_args *cbargs = (struct attrcb_args *) arg;
7906 : 8171008 : const int level = cbargs->level;
7907 : 8171008 : Dwarf_Die *die = &cbargs->dies[level];
7908 : 8171008 : bool is_split = cbargs->is_split;
7909 : 8171008 : FILE *out = cbargs->out;
7910 : :
7911 [ + - ]: 8171008 : unsigned int attr = dwarf_whatattr (attrp);
7912 [ - + ]: 8171008 : if (unlikely (attr == 0))
7913 : : {
7914 [ # # ]: 0 : if (!cbargs->silent)
7915 : 0 : error (0, 0, _("DIE [%" PRIx64 "] "
7916 : : "cannot get attribute code: %s"),
7917 : : dwarf_dieoffset (die), dwarf_errmsg (-1));
7918 : 0 : return DWARF_CB_ABORT;
7919 : : }
7920 : :
7921 [ - + ]: 8171008 : unsigned int form = dwarf_whatform (attrp);
7922 [ - + ]: 8171008 : if (unlikely (form == 0))
7923 : : {
7924 [ # # ]: 0 : if (!cbargs->silent)
7925 : 0 : error (0, 0, _("DIE [%" PRIx64 "] "
7926 : : "cannot get attribute form: %s"),
7927 : : dwarf_dieoffset (die), dwarf_errmsg (-1));
7928 : 0 : return DWARF_CB_ABORT;
7929 : : }
7930 : :
7931 [ + + + + : 8171008 : switch (form)
+ + + +
- ]
7932 : : {
7933 : 103118 : case DW_FORM_addr:
7934 : : case DW_FORM_addrx:
7935 : : case DW_FORM_addrx1:
7936 : : case DW_FORM_addrx2:
7937 : : case DW_FORM_addrx3:
7938 : : case DW_FORM_addrx4:
7939 : : case DW_FORM_GNU_addr_index:
7940 [ + + ]: 103118 : if (!cbargs->silent)
7941 : : {
7942 : 102576 : Dwarf_Addr addr;
7943 [ - + ]: 102576 : if (unlikely (dwarf_formaddr (attrp, &addr) != 0))
7944 : : {
7945 : 0 : attrval_out:
7946 [ # # ]: 0 : if (!cbargs->silent)
7947 : 0 : error (0, 0, _("DIE [%" PRIx64 "] "
7948 : : "cannot get attribute '%s' (%s) value: "
7949 : : "%s"),
7950 : : dwarf_dieoffset (die),
7951 : : dwarf_attr_name (attr),
7952 : : dwarf_form_name (form),
7953 : : dwarf_errmsg (-1));
7954 : : /* Don't ABORT, it might be other attributes can be resolved. */
7955 : 0 : return DWARF_CB_OK;
7956 : : }
7957 [ + + ]: 102576 : if (form != DW_FORM_addr )
7958 : : {
7959 : 40 : Dwarf_Word word;
7960 [ - + ]: 40 : if (dwarf_formudata (attrp, &word) != 0)
7961 : 0 : goto attrval_out;
7962 : 40 : fprintf (out, " %*s%-20s (%s) [%" PRIx64 "] ",
7963 : : (int) (level * 2), "", dwarf_attr_name (attr),
7964 : : dwarf_form_name (form), word);
7965 : : }
7966 : : else
7967 : 102536 : fprintf (out, " %*s%-20s (%s) ",
7968 : : (int) (level * 2), "", dwarf_attr_name (attr),
7969 : : dwarf_form_name (form));
7970 : 102576 : print_dwarf_addr (cbargs->dwflmod, cbargs->addrsize, addr, addr, out);
7971 : 102576 : fprintf (out, "\n");
7972 : : }
7973 : 7347574 : break;
7974 : :
7975 : 1349324 : case DW_FORM_indirect:
7976 : : case DW_FORM_strp:
7977 : : case DW_FORM_line_strp:
7978 : : case DW_FORM_strx:
7979 : : case DW_FORM_strx1:
7980 : : case DW_FORM_strx2:
7981 : : case DW_FORM_strx3:
7982 : : case DW_FORM_strx4:
7983 : : case DW_FORM_string:
7984 : : case DW_FORM_GNU_strp_alt:
7985 : : case DW_FORM_GNU_str_index:
7986 [ + + ]: 1349324 : if (cbargs->silent)
7987 : : break;
7988 : 1347814 : const char *str = dwarf_formstring (attrp);
7989 [ - + ]: 1347814 : if (unlikely (str == NULL))
7990 : 0 : goto attrval_out;
7991 : 1347814 : fprintf (out, " %*s%-20s (%s) \"%s\"\n",
7992 : : (int) (level * 2), "", dwarf_attr_name (attr),
7993 : : dwarf_form_name (form), str);
7994 : 1347814 : break;
7995 : :
7996 : 1778018 : case DW_FORM_ref_addr:
7997 : : case DW_FORM_ref_udata:
7998 : : case DW_FORM_ref8:
7999 : : case DW_FORM_ref4:
8000 : : case DW_FORM_ref2:
8001 : : case DW_FORM_ref1:
8002 : : case DW_FORM_GNU_ref_alt:
8003 : : case DW_FORM_ref_sup4:
8004 : : case DW_FORM_ref_sup8:
8005 [ + + ]: 1778018 : if (cbargs->silent)
8006 : : break;
8007 : 1776438 : Dwarf_Die ref;
8008 [ - + ]: 1776438 : if (unlikely (dwarf_formref_die (attrp, &ref) == NULL))
8009 : 0 : goto attrval_out;
8010 : :
8011 : 1776438 : fprintf (out, " %*s%-20s (%s) ",
8012 : : (int) (level * 2), "", dwarf_attr_name (attr),
8013 : : dwarf_form_name (form));
8014 [ + + ]: 1776438 : if (is_split)
8015 : 66 : fprintf (out, "{%6" PRIxMAX "}\n", (uintmax_t) dwarf_dieoffset (&ref));
8016 : : else
8017 : 1776372 : fprintf (out, "[%6" PRIxMAX "]\n", (uintmax_t) dwarf_dieoffset (&ref));
8018 : : break;
8019 : :
8020 : 8 : case DW_FORM_ref_sig8:
8021 [ + - ]: 8 : if (cbargs->silent)
8022 : : break;
8023 : 8 : fprintf (out, " %*s%-20s (%s) {%6" PRIx64 "}\n",
8024 : : (int) (level * 2), "", dwarf_attr_name (attr),
8025 : : dwarf_form_name (form),
8026 [ - + ]: 8 : (uint64_t) read_8ubyte_unaligned (attrp->cu->dbg, attrp->valp));
8027 : 8 : break;
8028 : :
8029 : 4536454 : case DW_FORM_sec_offset:
8030 : : case DW_FORM_rnglistx:
8031 : : case DW_FORM_loclistx:
8032 : : case DW_FORM_implicit_const:
8033 : : case DW_FORM_udata:
8034 : : case DW_FORM_sdata:
8035 : : case DW_FORM_data8: /* Note no data16 here, we see that as block. */
8036 : : case DW_FORM_data4:
8037 : : case DW_FORM_data2:
8038 : 4536454 : case DW_FORM_data1:;
8039 : 4536454 : Dwarf_Word num;
8040 [ - + ]: 4536454 : if (unlikely (dwarf_formudata (attrp, &num) != 0))
8041 : 0 : goto attrval_out;
8042 : :
8043 : 4536454 : const char *valuestr = NULL;
8044 : 4536454 : bool as_hex_id = false;
8045 [ + + + + : 4536454 : switch (attr)
+ + + + +
+ - - - -
- - + - +
+ + ]
8046 : : {
8047 : : /* This case can take either a constant or a loclistptr. */
8048 : 565408 : case DW_AT_data_member_location:
8049 [ + - ]: 565408 : if (form != DW_FORM_sec_offset
8050 [ + + ]: 565408 : && (cbargs->version >= 4
8051 [ + - ]: 47612 : || (form != DW_FORM_data4 && form != DW_FORM_data8)))
8052 : : {
8053 [ + - ]: 565408 : if (!cbargs->silent)
8054 : 565408 : fprintf (out, " %*s%-20s (%s) %" PRIuMAX "\n",
8055 : : (int) (level * 2), "", dwarf_attr_name (attr),
8056 : : dwarf_form_name (form), (uintmax_t) num);
8057 : 565408 : return DWARF_CB_OK;
8058 : : }
8059 : 222210 : FALLTHROUGH;
8060 : :
8061 : : /* These cases always take a loclist[ptr] and no constant. */
8062 : : case DW_AT_location:
8063 : : case DW_AT_data_location:
8064 : : case DW_AT_vtable_elem_location:
8065 : : case DW_AT_string_length:
8066 : : case DW_AT_use_location:
8067 : : case DW_AT_frame_base:
8068 : : case DW_AT_return_addr:
8069 : : case DW_AT_static_link:
8070 : : case DW_AT_segment:
8071 : : case DW_AT_GNU_call_site_value:
8072 : : case DW_AT_GNU_call_site_data_value:
8073 : : case DW_AT_GNU_call_site_target:
8074 : : case DW_AT_GNU_call_site_target_clobbered:
8075 : : case DW_AT_GNU_locviews:
8076 : : {
8077 : 222210 : bool nlpt;
8078 [ + + ]: 222210 : if (cbargs->cu->version < 5)
8079 : : {
8080 [ + + ]: 324 : if (! cbargs->is_split)
8081 : : {
8082 : 284 : nlpt = notice_listptr (section_loc, &known_locsptr,
8083 : 284 : cbargs->addrsize,
8084 : 284 : cbargs->offset_size,
8085 : : cbargs->cu, num, attr);
8086 : : }
8087 : : else
8088 : : nlpt = true;
8089 : : }
8090 : : else
8091 : : {
8092 : : /* Only register for a real section offset. Otherwise
8093 : : it is a DW_FORM_loclistx which is just an index
8094 : : number and we should already have registered the
8095 : : section offset for the index when we saw the
8096 : : DW_AT_loclists_base CU attribute. */
8097 [ + + ]: 221886 : if (form == DW_FORM_sec_offset)
8098 : 221808 : nlpt = notice_listptr (section_loc, &known_loclistsptr,
8099 : 221808 : cbargs->addrsize, cbargs->offset_size,
8100 : : cbargs->cu, num, attr);
8101 : : else
8102 : : nlpt = true;
8103 : :
8104 : : }
8105 : :
8106 [ + + ]: 222210 : if (!cbargs->silent)
8107 : : {
8108 [ + + + + ]: 221844 : if (cbargs->cu->version < 5 || form == DW_FORM_sec_offset)
8109 [ - + ]: 221830 : fprintf (out, " %*s%-20s (%s) location list [%6"
8110 : : PRIxMAX "]%s\n",
8111 : : (int) (level * 2), "", dwarf_attr_name (attr),
8112 : : dwarf_form_name (form), (uintmax_t) num,
8113 : : nlpt ? "" : " <WARNING offset too big>");
8114 : : else
8115 : 14 : fprintf (out, " %*s%-20s (%s) location index [%6"
8116 : : PRIxMAX "]\n",
8117 : : (int) (level * 2), "", dwarf_attr_name (attr),
8118 : : dwarf_form_name (form), (uintmax_t) num);
8119 : : }
8120 : : }
8121 : : return DWARF_CB_OK;
8122 : :
8123 : 10 : case DW_AT_loclists_base:
8124 : : {
8125 : 20 : bool nlpt = notice_listptr (section_loc, &known_loclistsptr,
8126 : 10 : cbargs->addrsize, cbargs->offset_size,
8127 : : cbargs->cu, num, attr);
8128 : :
8129 [ + + ]: 10 : if (!cbargs->silent)
8130 [ - + ]: 2 : fprintf (out, " %*s%-20s (%s) location list [%6" PRIxMAX "]%s\n",
8131 : : (int) (level * 2), "", dwarf_attr_name (attr),
8132 : : dwarf_form_name (form), (uintmax_t) num,
8133 : : nlpt ? "" : " <WARNING offset too big>");
8134 : : }
8135 : : return DWARF_CB_OK;
8136 : :
8137 : 35756 : case DW_AT_ranges:
8138 : : case DW_AT_start_scope:
8139 : : {
8140 : 35756 : bool nlpt;
8141 [ + + ]: 35756 : if (cbargs->cu->version < 5)
8142 : 106 : nlpt = notice_listptr (section_ranges, &known_rangelistptr,
8143 : 106 : cbargs->addrsize, cbargs->offset_size,
8144 : : cbargs->cu, num, attr);
8145 : : else
8146 : : {
8147 : : /* Only register for a real section offset. Otherwise
8148 : : it is a DW_FORM_rangelistx which is just an index
8149 : : number and we should already have registered the
8150 : : section offset for the index when we saw the
8151 : : DW_AT_rnglists_base CU attribute. */
8152 [ + + ]: 35650 : if (form == DW_FORM_sec_offset)
8153 : 35638 : nlpt = notice_listptr (section_ranges, &known_rnglistptr,
8154 : 35638 : cbargs->addrsize, cbargs->offset_size,
8155 : : cbargs->cu, num, attr);
8156 : : else
8157 : : nlpt = true;
8158 : : }
8159 : :
8160 [ + + ]: 35756 : if (!cbargs->silent)
8161 : : {
8162 [ + + + + ]: 35670 : if (cbargs->cu->version < 5 || form == DW_FORM_sec_offset)
8163 [ - + ]: 35666 : fprintf (out, " %*s%-20s (%s) range list [%6"
8164 : : PRIxMAX "]%s\n",
8165 : : (int) (level * 2), "", dwarf_attr_name (attr),
8166 : : dwarf_form_name (form), (uintmax_t) num,
8167 : : nlpt ? "" : " <WARNING offset too big>");
8168 : : else
8169 : 4 : fprintf (out, " %*s%-20s (%s) range index [%6"
8170 : : PRIxMAX "]\n",
8171 : : (int) (level * 2), "", dwarf_attr_name (attr),
8172 : : dwarf_form_name (form), (uintmax_t) num);
8173 : : }
8174 : : }
8175 : : return DWARF_CB_OK;
8176 : :
8177 : 8 : case DW_AT_rnglists_base:
8178 : : {
8179 : 16 : bool nlpt = notice_listptr (section_ranges, &known_rnglistptr,
8180 : 8 : cbargs->addrsize, cbargs->offset_size,
8181 : : cbargs->cu, num, attr);
8182 [ + + ]: 8 : if (!cbargs->silent)
8183 [ - + ]: 4 : fprintf (out, " %*s%-20s (%s) range list [%6"
8184 : : PRIxMAX "]%s\n",
8185 : : (int) (level * 2), "", dwarf_attr_name (attr),
8186 : : dwarf_form_name (form), (uintmax_t) num,
8187 : : nlpt ? "" : " <WARNING offset too big>");
8188 : : }
8189 : : return DWARF_CB_OK;
8190 : :
8191 : 34 : case DW_AT_addr_base:
8192 : : case DW_AT_GNU_addr_base:
8193 : : {
8194 : 68 : bool addrbase = notice_listptr (section_addr, &known_addrbases,
8195 : 34 : cbargs->addrsize,
8196 : 34 : cbargs->offset_size,
8197 : : cbargs->cu, num, attr);
8198 [ + + ]: 34 : if (!cbargs->silent)
8199 [ - + ]: 18 : fprintf (out, " %*s%-20s (%s) address base [%6"
8200 : : PRIxMAX "]%s\n",
8201 : : (int) (level * 2), "", dwarf_attr_name (attr),
8202 : : dwarf_form_name (form), (uintmax_t) num,
8203 : : addrbase ? "" : " <WARNING offset too big>");
8204 : : }
8205 : : return DWARF_CB_OK;
8206 : :
8207 : 8 : case DW_AT_str_offsets_base:
8208 : : {
8209 : 16 : bool stroffbase = notice_listptr (section_str, &known_stroffbases,
8210 : 8 : cbargs->addrsize,
8211 : 8 : cbargs->offset_size,
8212 : : cbargs->cu, num, attr);
8213 [ + - ]: 8 : if (!cbargs->silent)
8214 [ - + ]: 8 : fprintf (out, " %*s%-20s (%s) str offsets base [%6"
8215 : : PRIxMAX "]%s\n",
8216 : : (int) (level * 2), "", dwarf_attr_name (attr),
8217 : : dwarf_form_name (form), (uintmax_t) num,
8218 : : stroffbase ? "" : " <WARNING offset too big>");
8219 : : }
8220 : : return DWARF_CB_OK;
8221 : :
8222 : 3736 : case DW_AT_language:
8223 : 3736 : valuestr = dwarf_lang_name (num);
8224 : 3736 : break;
8225 : 4 : case DW_AT_language_name:
8226 : 4 : valuestr = dwarf_lname_name (num);
8227 : 4 : break;
8228 : 59422 : case DW_AT_encoding:
8229 : 59422 : valuestr = dwarf_encoding_name (num);
8230 : 59422 : break;
8231 : 0 : case DW_AT_accessibility:
8232 : 0 : valuestr = dwarf_access_name (num);
8233 : 0 : break;
8234 : 0 : case DW_AT_defaulted:
8235 : 0 : valuestr = dwarf_defaulted_name (num);
8236 : 0 : break;
8237 : 0 : case DW_AT_visibility:
8238 : 0 : valuestr = dwarf_visibility_name (num);
8239 : 0 : break;
8240 : 0 : case DW_AT_virtuality:
8241 : 0 : valuestr = dwarf_virtuality_name (num);
8242 : 0 : break;
8243 : 0 : case DW_AT_identifier_case:
8244 : 0 : valuestr = dwarf_identifier_case_name (num);
8245 : 0 : break;
8246 : 0 : case DW_AT_calling_convention:
8247 : 0 : valuestr = dwarf_calling_convention_name (num);
8248 : 0 : break;
8249 : 7294 : case DW_AT_inline:
8250 : 7294 : valuestr = dwarf_inline_name (num);
8251 : 7294 : break;
8252 : 0 : case DW_AT_ordering:
8253 : 0 : valuestr = dwarf_ordering_name (num);
8254 : 0 : break;
8255 : 996494 : case DW_AT_decl_file:
8256 : : case DW_AT_call_file:
8257 : : {
8258 [ + + ]: 996494 : if (cbargs->silent)
8259 : : break;
8260 : :
8261 : : /* Try to get the actual file, the current interface only
8262 : : gives us full paths, but we only want to show the file
8263 : : name for now. */
8264 : 995646 : Dwarf_Die cudie;
8265 [ + - ]: 995646 : if (dwarf_cu_die (cbargs->cu, &cudie,
8266 : : NULL, NULL, NULL, NULL, NULL, NULL) != NULL)
8267 : : {
8268 : 995646 : Dwarf_Files *files;
8269 : 995646 : size_t nfiles;
8270 [ + - ]: 995646 : if (dwarf_getsrcfiles (&cudie, &files, &nfiles) == 0)
8271 : : {
8272 : 995646 : valuestr = dwarf_filesrc (files, num, NULL, NULL);
8273 [ + - ]: 995646 : if (valuestr != NULL)
8274 : : {
8275 : 995646 : const char *filename = strrchr (valuestr, '/');
8276 [ - + ]: 995646 : if (filename != NULL)
8277 : 995646 : valuestr = filename + 1;
8278 : : }
8279 : : else
8280 : 0 : error (0, 0, _("invalid file (%" PRId64 "): %s"),
8281 : : num, dwarf_errmsg (-1));
8282 : : }
8283 : : else
8284 : 0 : error (0, 0, _("no srcfiles for CU [%" PRIx64 "]"),
8285 : : dwarf_dieoffset (&cudie));
8286 : : }
8287 : : else
8288 : 0 : error (0, 0, _("couldn't get DWARF CU: %s"),
8289 : : dwarf_errmsg (-1));
8290 : 995646 : if (valuestr == NULL)
8291 : : valuestr = "???";
8292 : : }
8293 : 995646 : break;
8294 : 26 : case DW_AT_GNU_dwo_id:
8295 : 26 : as_hex_id = true;
8296 : 26 : break;
8297 : :
8298 : : default:
8299 : : /* Nothing. */
8300 : : break;
8301 : : }
8302 : :
8303 [ + + ]: 3713020 : if (cbargs->silent)
8304 : : break;
8305 : :
8306 : : /* When highpc is in constant form it is relative to lowpc.
8307 : : In that case also show the address. */
8308 : 3709310 : Dwarf_Addr highpc;
8309 [ + + + - ]: 3709310 : if (attr == DW_AT_high_pc && dwarf_highpc (die, &highpc) == 0)
8310 : : {
8311 : 23988 : fprintf (out, " %*s%-20s (%s) %" PRIuMAX " (",
8312 : : (int) (level * 2), "", dwarf_attr_name (attr),
8313 : : dwarf_form_name (form), (uintmax_t) num);
8314 : 23988 : print_dwarf_addr (cbargs->dwflmod, cbargs->addrsize,
8315 : : highpc, highpc, out);
8316 : 23988 : fprintf (out, ")\n");
8317 : : }
8318 : : else
8319 : : {
8320 [ + + ]: 3685322 : if (as_hex_id)
8321 : : {
8322 : 4 : fprintf (out, " %*s%-20s (%s) 0x%.16" PRIx64 "\n",
8323 : : (int) (level * 2), "", dwarf_attr_name (attr),
8324 : : dwarf_form_name (form), num);
8325 : : }
8326 : : else
8327 : : {
8328 : 3685318 : Dwarf_Sword snum = 0;
8329 : 3685318 : bool is_signed;
8330 : 3685318 : int bytes = 0;
8331 [ + + ]: 3685318 : if (attr == DW_AT_const_value)
8332 : 353696 : die_type_sign_bytes (die, &is_signed, &bytes);
8333 : : else
8334 : 3331622 : is_signed = (form == DW_FORM_sdata
8335 : 3331622 : || form == DW_FORM_implicit_const);
8336 : :
8337 [ + + ]: 3685318 : if (is_signed)
8338 [ - + ]: 386034 : if (unlikely (dwarf_formsdata (attrp, &snum) != 0))
8339 : 0 : goto attrval_out;
8340 : :
8341 [ + + ]: 3685318 : if (valuestr == NULL)
8342 : : {
8343 : 2619826 : fprintf (out, " %*s%-20s (%s) ",
8344 : : (int) (level * 2), "", dwarf_attr_name (attr),
8345 : : dwarf_form_name (form));
8346 : : }
8347 : : else
8348 : : {
8349 : 1065492 : fprintf (out, " %*s%-20s (%s) %s (",
8350 : : (int) (level * 2), "", dwarf_attr_name (attr),
8351 : : dwarf_form_name (form), valuestr);
8352 : : }
8353 : :
8354 [ + + + + : 3685318 : switch (bytes)
+ ]
8355 : : {
8356 : 10 : case 1:
8357 [ + + ]: 10 : if (is_signed)
8358 : 2 : fprintf (out, "%" PRId8, (int8_t) snum);
8359 : : else
8360 : 8 : fprintf (out, "%" PRIu8, (uint8_t) num);
8361 : : break;
8362 : :
8363 : 4 : case 2:
8364 [ + + ]: 4 : if (is_signed)
8365 : 2 : fprintf (out, "%" PRId16, (int16_t) snum);
8366 : : else
8367 : 2 : fprintf (out, "%" PRIu16, (uint16_t) num);
8368 : : break;
8369 : :
8370 : 234 : case 4:
8371 [ + + ]: 234 : if (is_signed)
8372 : 210 : fprintf (out, "%" PRId32, (int32_t) snum);
8373 : : else
8374 : 24 : fprintf (out, "%" PRIu32, (uint32_t) num);
8375 : : break;
8376 : :
8377 : 718 : case 8:
8378 [ + + ]: 718 : if (is_signed)
8379 : 2 : fprintf (out, "%" PRId64, (int64_t) snum);
8380 : : else
8381 : 716 : fprintf (out, "%" PRIu64, (uint64_t) num);
8382 : : break;
8383 : :
8384 : 3684352 : default:
8385 [ + + ]: 3684352 : if (is_signed)
8386 : 385818 : fprintf (out, "%" PRIdMAX, (intmax_t) snum);
8387 : : else
8388 : 3298534 : fprintf (out, "%" PRIuMAX, (uintmax_t) num);
8389 : : break;
8390 : : }
8391 : :
8392 : : /* Make clear if we switched from a signed encoding to
8393 : : an unsigned value. */
8394 [ + + ]: 3685318 : if (attr == DW_AT_const_value
8395 [ + + ]: 353696 : && (form == DW_FORM_sdata || form == DW_FORM_implicit_const)
8396 [ + + ]: 2680 : && !is_signed)
8397 : 2662 : fprintf (out, " (%" PRIdMAX ")", (intmax_t) num);
8398 : :
8399 [ + + ]: 3685318 : if (valuestr == NULL)
8400 : 2619826 : fprintf (out, "\n");
8401 : : else
8402 : 1065492 : fprintf (out, ")\n");
8403 : : }
8404 : : }
8405 : : break;
8406 : :
8407 : 18244 : case DW_FORM_flag:
8408 [ + + ]: 18244 : if (cbargs->silent)
8409 : : break;
8410 : 18096 : bool flag;
8411 [ - + ]: 18096 : if (unlikely (dwarf_formflag (attrp, &flag) != 0))
8412 : 0 : goto attrval_out;
8413 : :
8414 : 18096 : fprintf (out, " %*s%-20s (%s) %s\n",
8415 : : (int) (level * 2), "", dwarf_attr_name (attr),
8416 [ + - ]: 18096 : dwarf_form_name (form), flag ? yes_str : no_str);
8417 : 18096 : break;
8418 : :
8419 : 162442 : case DW_FORM_flag_present:
8420 [ + + ]: 162442 : if (cbargs->silent)
8421 : : break;
8422 : 161888 : fprintf (out, " %*s%-20s (%s) %s\n",
8423 : : (int) (level * 2), "", dwarf_attr_name (attr),
8424 : : dwarf_form_name (form), yes_str);
8425 : 161888 : break;
8426 : :
8427 : 223400 : case DW_FORM_exprloc:
8428 : : case DW_FORM_block4:
8429 : : case DW_FORM_block2:
8430 : : case DW_FORM_block1:
8431 : : case DW_FORM_block:
8432 : : case DW_FORM_data16: /* DWARF5 calls this a constant class. */
8433 [ + + ]: 223400 : if (cbargs->silent)
8434 : : break;
8435 : 223138 : Dwarf_Block block;
8436 [ - + ]: 223138 : if (unlikely (dwarf_formblock (attrp, &block) != 0))
8437 : 0 : goto attrval_out;
8438 : :
8439 : 223138 : fprintf (out, " %*s%-20s (%s) ",
8440 : : (int) (level * 2), "", dwarf_attr_name (attr),
8441 : : dwarf_form_name (form));
8442 : :
8443 [ + + + ]: 223138 : switch (attr)
8444 : : {
8445 : 96280 : default:
8446 [ - + ]: 96280 : if (form != DW_FORM_exprloc)
8447 : : {
8448 : 0 : print_block (block.length, block.data, out);
8449 : 0 : break;
8450 : : }
8451 : 126850 : FALLTHROUGH;
8452 : :
8453 : : case DW_AT_location:
8454 : : case DW_AT_data_location:
8455 : : case DW_AT_data_member_location:
8456 : : case DW_AT_vtable_elem_location:
8457 : : case DW_AT_string_length:
8458 : : case DW_AT_use_location:
8459 : : case DW_AT_frame_base:
8460 : : case DW_AT_return_addr:
8461 : : case DW_AT_static_link:
8462 : : case DW_AT_allocated:
8463 : : case DW_AT_associated:
8464 : : case DW_AT_bit_size:
8465 : : case DW_AT_bit_offset:
8466 : : case DW_AT_bit_stride:
8467 : : case DW_AT_byte_size:
8468 : : case DW_AT_byte_stride:
8469 : : case DW_AT_count:
8470 : : case DW_AT_lower_bound:
8471 : : case DW_AT_upper_bound:
8472 : : case DW_AT_GNU_call_site_value:
8473 : : case DW_AT_GNU_call_site_data_value:
8474 : : case DW_AT_GNU_call_site_target:
8475 : : case DW_AT_GNU_call_site_target_clobbered:
8476 [ + + ]: 126850 : if (form == DW_FORM_exprloc
8477 [ + - ]: 140 : || (form != DW_FORM_data16
8478 [ + - ]: 140 : && attrp->cu->version < 4)) /* blocks were expressions. */
8479 : : {
8480 : 223130 : fputc ('\n', out);
8481 : 223130 : print_ops (cbargs->dwflmod, cbargs->dbg,
8482 : 223130 : 12 + level * 2, 12 + level * 2,
8483 : : cbargs->version, cbargs->addrsize, cbargs->offset_size,
8484 : 223130 : attrp->cu, block.length, block.data, out);
8485 : : }
8486 : : else
8487 : 0 : print_block (block.length, block.data, out);
8488 : : break;
8489 : :
8490 : 8 : case DW_AT_discr_list:
8491 [ - + ]: 8 : if (block.length == 0)
8492 : 0 : fputs ("<default>\n", out);
8493 [ + - ]: 8 : else if (form != DW_FORM_data16)
8494 : : {
8495 : 8 : const unsigned char *readp = block.data;
8496 : 8 : const unsigned char *readendp = readp + block.length;
8497 : :
8498 : : /* See if we are dealing with a signed or unsigned
8499 : : values. If the parent of this variant DIE is a
8500 : : variant_part then it will either have a discriminant
8501 : : which points to the member which type is the
8502 : : discriminant type. Or the variant_part itself has a
8503 : : type representing the discriminant. */
8504 : 8 : bool is_signed = false;
8505 [ + - ]: 8 : if (level > 0)
8506 : : {
8507 : 8 : Dwarf_Die *parent = &cbargs->dies[level - 1];
8508 [ + - ]: 8 : if (dwarf_tag (die) == DW_TAG_variant
8509 [ + - ]: 8 : && dwarf_tag (parent) == DW_TAG_variant_part)
8510 : : {
8511 : 8 : Dwarf_Die member;
8512 : 8 : Dwarf_Attribute discr_attr;
8513 : 8 : int bytes;
8514 [ + - ]: 8 : if (dwarf_formref_die (dwarf_attr (parent,
8515 : : DW_AT_discr,
8516 : : &discr_attr),
8517 : : &member) != NULL)
8518 : 8 : die_type_sign_bytes (&member, &is_signed, &bytes);
8519 : : else
8520 : 0 : die_type_sign_bytes (parent, &is_signed, &bytes);
8521 : : }
8522 : : }
8523 [ + + ]: 24 : while (readp < readendp)
8524 : : {
8525 : 16 : int d = (int) *readp++;
8526 : 16 : fprintf (out, "%s ", dwarf_discr_list_name (d));
8527 [ - + ]: 16 : if (readp >= readendp)
8528 : 0 : goto attrval_out;
8529 : :
8530 : 16 : Dwarf_Word val;
8531 : 16 : Dwarf_Sword sval;
8532 [ + + ]: 16 : if (d == DW_DSC_label)
8533 : : {
8534 [ + + ]: 8 : if (is_signed)
8535 : : {
8536 : 4 : get_sleb128 (sval, readp, readendp);
8537 : 4 : fprintf (out, "%" PRId64 "", sval);
8538 : : }
8539 : : else
8540 : : {
8541 : 4 : get_uleb128 (val, readp, readendp);
8542 : 4 : fprintf (out, "%" PRIu64 "", val);
8543 : : }
8544 : : }
8545 [ + - ]: 8 : else if (d == DW_DSC_range)
8546 : : {
8547 [ + + ]: 8 : if (is_signed)
8548 : : {
8549 : 6 : get_sleb128 (sval, readp, readendp);
8550 : 6 : fprintf (out, "%" PRId64 "..", sval);
8551 [ - + ]: 6 : if (readp >= readendp)
8552 : 0 : goto attrval_out;
8553 : 6 : get_sleb128 (sval, readp, readendp);
8554 : 6 : fprintf (out, "%" PRId64 "", sval);
8555 : : }
8556 : : else
8557 : : {
8558 : 2 : get_uleb128 (val, readp, readendp);
8559 : 2 : fprintf (out, "%" PRIu64 "..", val);
8560 [ - + ]: 2 : if (readp >= readendp)
8561 : 0 : goto attrval_out;
8562 : 2 : get_uleb128 (val, readp, readendp);
8563 : 2 : fprintf (out, "%" PRIu64 "", val);
8564 : : }
8565 : : }
8566 : : else
8567 : : {
8568 : 0 : print_block (readendp - readp, readp, out);
8569 : 0 : break;
8570 : : }
8571 [ + + ]: 16 : if (readp < readendp)
8572 : 8 : fprintf (out, ", ");
8573 : : }
8574 : 8 : fputc ('\n', out);
8575 : : }
8576 : : else
8577 : 0 : print_block (block.length, block.data, out);
8578 : : break;
8579 : : }
8580 : : break;
8581 : :
8582 : 0 : default:
8583 [ # # ]: 0 : if (cbargs->silent)
8584 : : break;
8585 : 0 : fprintf (out, " %*s%-20s (%s) ???\n",
8586 : : (int) (level * 2), "", dwarf_attr_name (attr),
8587 : : dwarf_form_name (form));
8588 : 0 : break;
8589 : : }
8590 : :
8591 : 7347574 : return DWARF_CB_OK;
8592 : : }
8593 : :
8594 : : static void
8595 : 208 : print_debug_units (Dwfl_Module *dwflmod,
8596 : : Ebl *ebl, GElf_Ehdr *ehdr __attribute__ ((unused)),
8597 : : Elf_Scn *scn, GElf_Shdr *shdr,
8598 : : Dwarf *dbg, bool debug_types, FILE *out)
8599 : : {
8600 [ + + + + ]: 208 : const bool silent = !(print_debug_sections & section_info) && !debug_types;
8601 : 208 : const char *secname = section_name (ebl, shdr);
8602 : :
8603 : : /* Check section actually exists. */
8604 [ + + ]: 208 : if (!silent)
8605 [ + - ]: 136 : if (get_debug_elf_data (dbg, ebl,
8606 : : debug_types ? IDX_debug_types : IDX_debug_info,
8607 : : scn) == NULL)
8608 : 0 : return;
8609 : :
8610 : 136 : if (!silent)
8611 : 136 : fprintf (out, _("\
8612 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n [Offset]\n"),
8613 : 136 : elf_ndxscn (scn), secname, (uint64_t) shdr->sh_offset);
8614 : :
8615 : : /* If the section is empty we don't have to do anything. */
8616 [ + + + - ]: 208 : if (!silent && shdr->sh_size == 0)
8617 : : return;
8618 : :
8619 : 208 : int maxdies = 20;
8620 : 208 : Dwarf_Die *dies = xmalloc (maxdies * sizeof (Dwarf_Die));
8621 : :
8622 : : /* New compilation unit. */
8623 : 208 : Dwarf_Half version;
8624 : :
8625 : 208 : Dwarf_Die result;
8626 : 208 : Dwarf_Off abbroffset;
8627 : 208 : uint8_t addrsize;
8628 : 208 : uint8_t offsize;
8629 : 208 : uint64_t unit_id;
8630 : 208 : Dwarf_Off subdie_off;
8631 : :
8632 : 208 : int unit_res;
8633 : 208 : Dwarf_CU *cu;
8634 : 208 : Dwarf_CU cu_mem;
8635 : 208 : uint8_t unit_type;
8636 : 208 : Dwarf_Die cudie;
8637 : :
8638 : : /* We cheat a little because we want to see only the CUs from .debug_info
8639 : : or .debug_types. We know the Dwarf_CU struct layout. Set it up at
8640 : : the end of .debug_info if we want .debug_types only. Check the returned
8641 : : Dwarf_CU is still in the expected section. */
8642 [ + + ]: 208 : if (debug_types)
8643 : : {
8644 : 2 : cu_mem.dbg = dbg;
8645 : 2 : cu_mem.end = dbg->sectiondata[IDX_debug_info]->d_size;
8646 : 2 : cu_mem.sec_idx = IDX_debug_info;
8647 : 2 : cu = &cu_mem;
8648 : : }
8649 : : else
8650 : 206 : cu = NULL;
8651 : :
8652 : : next_cu:
8653 : 3962 : unit_res = dwarf_get_units (dbg, cu, &cu, &version, &unit_type,
8654 : : &cudie, NULL);
8655 [ + + ]: 3962 : if (unit_res == 1)
8656 : 206 : goto do_return;
8657 : :
8658 [ - + ]: 3756 : if (unit_res == -1)
8659 : : {
8660 [ # # ]: 0 : if (!silent)
8661 : 0 : error (0, 0, _("cannot get next unit: %s"), dwarf_errmsg (-1));
8662 : 0 : goto do_return;
8663 : : }
8664 : :
8665 [ + + + + ]: 7508 : if (cu->sec_idx != (size_t) (debug_types ? IDX_debug_types : IDX_debug_info))
8666 : 2 : goto do_return;
8667 : :
8668 : 3754 : dwarf_cu_die (cu, &result, NULL, &abbroffset, &addrsize, &offsize,
8669 : : &unit_id, &subdie_off);
8670 : :
8671 [ + + ]: 3754 : if (!silent)
8672 : : {
8673 : 3654 : Dwarf_Off offset = cu->start;
8674 [ + + + - ]: 3654 : if (debug_types && version < 5)
8675 : 4 : {
8676 : 4 : Dwarf_Die typedie;
8677 : 4 : Dwarf_Off dieoffset;
8678 : 4 : dieoffset = dwarf_dieoffset (dwarf_offdie_types (dbg, cu->start
8679 : : + subdie_off,
8680 : : &typedie));
8681 : 4 : fprintf (out, " %s %" PRIu64 ":\n"
8682 : : " %s: %" PRIu16
8683 : : ", %s: %" PRIu64
8684 : : ", %s: %" PRIu8
8685 : : ", %s: %" PRIu8
8686 : : "\n %s: %#" PRIx64
8687 : : ", %s: %#" PRIx64 " [%" PRIx64 "]\n",
8688 : : type_unit_str, (uint64_t) offset, version_str, version,
8689 : : abbrev_offset_str, abbroffset, addr_size_str, addrsize,
8690 : : offset_size_str, offsize, type_sig_str, unit_id,
8691 : : type_offset_str, (uint64_t) subdie_off, dieoffset);
8692 : : }
8693 : : else
8694 : : {
8695 : 3650 : fprintf (out, " %s %" PRIu64 ":\n"
8696 : : " %s: %" PRIu16
8697 : : ", %s: %" PRIu64
8698 : : ", %s: %" PRIu8
8699 : : ", %s: %" PRIu8 "\n",
8700 : : cu_offset_at_str, (uint64_t) offset, version_str, version,
8701 : : abbrev_offset_str, abbroffset, addr_size_str, addrsize,
8702 : : offset_size_str, offsize);
8703 : :
8704 [ + + ]: 3650 : if (version >= 5 || (unit_type != DW_UT_compile
8705 [ + + ]: 170 : && unit_type != DW_UT_partial))
8706 : : {
8707 : 3482 : fprintf (out, " %s: %s (%" PRIu8 ")",
8708 : : unit_type_str, dwarf_unit_name (unit_type), unit_type);
8709 : 3482 : if (unit_type == DW_UT_type
8710 [ + + ]: 3482 : || unit_type == DW_UT_skeleton
8711 [ + - ]: 3472 : || unit_type == DW_UT_split_compile
8712 [ - + ]: 3472 : || unit_type == DW_UT_split_type)
8713 : 10 : fprintf (out, ", %s: 0x%.16" PRIx64 "", unit_id_str, unit_id);
8714 : 3482 : if (unit_type == DW_UT_type
8715 [ - + ]: 3482 : || unit_type == DW_UT_split_type)
8716 : : {
8717 : 0 : Dwarf_Die typedie;
8718 : 0 : Dwarf_Off dieoffset;
8719 : 0 : dwarf_cu_info (cu, NULL, NULL, NULL, &typedie,
8720 : : NULL, NULL, NULL);
8721 : 0 : dieoffset = dwarf_dieoffset (&typedie);
8722 : 0 : fprintf (out, ", %s: %#" PRIx64 " [%" PRIx64 "]",
8723 : : unit_die_off_str, subdie_off, dieoffset);
8724 : : }
8725 : 3482 : fprintf (out, "\n");
8726 : : }
8727 : : }
8728 : : }
8729 : :
8730 [ + - ]: 3754 : if (version < 2 || version > 5
8731 [ + - - + ]: 3754 : || unit_type < DW_UT_compile || unit_type > DW_UT_split_type)
8732 : : {
8733 [ # # ]: 0 : if (!silent)
8734 : 0 : error (0, 0, _("unknown version (%d) or unit type (%d)"),
8735 : : version, unit_type);
8736 : 0 : goto next_cu;
8737 : : }
8738 : :
8739 : 3754 : struct attrcb_args args =
8740 : : {
8741 : : .dwflmod = dwflmod,
8742 : : .silent = silent,
8743 : : .version = version,
8744 : : .addrsize = addrsize,
8745 : : .offset_size = offsize
8746 : : };
8747 : :
8748 : 3754 : bool is_split = false;
8749 : 3754 : int level = 0;
8750 : 3754 : dies[0] = cudie;
8751 : 3754 : args.cu = dies[0].cu;
8752 : 3754 : args.dbg = dbg;
8753 : 3754 : args.is_split = is_split;
8754 : 3754 : args.out = out;
8755 : :
8756 : : /* We might return here again for the split CU subdie. */
8757 : : do_cu:
8758 : 2149820 : do
8759 : : {
8760 : 2149820 : Dwarf_Off offset = dwarf_dieoffset (&dies[level]);
8761 [ - + ]: 2149820 : if (unlikely (offset == (Dwarf_Off) -1))
8762 : : {
8763 [ # # ]: 0 : if (!silent)
8764 : 0 : error (0, 0, _("cannot get DIE offset: %s"),
8765 : : dwarf_errmsg (-1));
8766 : 0 : goto do_return;
8767 : : }
8768 : :
8769 : 2149820 : int tag = dwarf_tag (&dies[level]);
8770 [ - + ]: 2149820 : if (unlikely (tag == DW_TAG_invalid))
8771 : : {
8772 [ # # ]: 0 : if (!silent)
8773 : 0 : error (0, 0, _("cannot get tag of DIE at offset [%" PRIx64
8774 : : "] in section '%s': %s"),
8775 : : (uint64_t) offset, secname, dwarf_errmsg (-1));
8776 : 0 : goto do_return;
8777 : : }
8778 : :
8779 [ + + ]: 2149820 : if (!silent)
8780 : : {
8781 : 2147834 : unsigned int code = dwarf_getabbrevcode (dies[level].abbrev);
8782 [ + + ]: 2147834 : if (is_split)
8783 : 96 : fprintf (out, " {%6" PRIx64 "} ", (uint64_t) offset);
8784 : : else
8785 : 2147738 : fprintf (out, " [%6" PRIx64 "] ", (uint64_t) offset);
8786 : 2147834 : fprintf (out, "%*s%-20s abbrev: %u\n", (int) (level * 2), "",
8787 : : dwarf_tag_name (tag), code);
8788 : : }
8789 : :
8790 : : /* Print the attribute values. */
8791 : 2149820 : args.level = level;
8792 : 2149820 : args.dies = dies;
8793 : 2149820 : (void) dwarf_getattrs (&dies[level], attr_callback, &args, 0);
8794 : :
8795 : : /* Make room for the next level's DIE. */
8796 [ - + ]: 2149820 : if (level + 1 == maxdies)
8797 : 0 : dies = xrealloc (dies, (maxdies += 10) * sizeof (Dwarf_Die));
8798 : :
8799 : 2149820 : int res = dwarf_child (&dies[level], &dies[level + 1]);
8800 [ + + ]: 2149820 : if (res > 0)
8801 : : {
8802 [ + + ]: 2149820 : while ((res = dwarf_siblingof (&dies[level], &dies[level])) == 1)
8803 [ + + ]: 297298 : if (level-- == 0)
8804 : : break;
8805 : :
8806 [ - + ]: 1856284 : if (unlikely (res == -1))
8807 : : {
8808 [ # # ]: 0 : if (!silent)
8809 : 0 : error (0, 0, _("cannot get next DIE: %s\n"),
8810 : : dwarf_errmsg (-1));
8811 : 0 : goto do_return;
8812 : : }
8813 : : }
8814 [ - + ]: 293536 : else if (unlikely (res < 0))
8815 : : {
8816 [ # # ]: 0 : if (!silent)
8817 : 0 : error (0, 0, _("cannot get next DIE: %s"),
8818 : : dwarf_errmsg (-1));
8819 : 0 : goto do_return;
8820 : : }
8821 : : else
8822 : : ++level;
8823 : : }
8824 [ + + ]: 2149820 : while (level >= 0);
8825 : :
8826 : : /* We might want to show the split compile unit if this was a skeleton.
8827 : : We need to scan it if we are requesting printing .debug_ranges for
8828 : : DWARF4 since GNU DebugFission uses "offsets" into the main ranges
8829 : : section. */
8830 [ + + ]: 3762 : if (unit_type == DW_UT_skeleton
8831 [ + + + + ]: 26 : && ((!silent && show_split_units)
8832 [ + + + + ]: 20 : || (version < 5 && (print_debug_sections & section_ranges) != 0)))
8833 : : {
8834 : 10 : Dwarf_Die subdie;
8835 [ + - ]: 10 : if (dwarf_cu_info (cu, NULL, NULL, NULL, &subdie, NULL, NULL, NULL) != 0
8836 [ + + ]: 10 : || dwarf_tag (&subdie) == DW_TAG_invalid)
8837 : : {
8838 [ + - ]: 2 : if (!silent)
8839 : : {
8840 : 2 : Dwarf_Attribute dwo_at;
8841 : 4 : const char *dwo_name =
8842 : 2 : (dwarf_formstring (dwarf_attr (&cudie, DW_AT_dwo_name,
8843 : : &dwo_at))
8844 [ - + ]: 2 : ?: (dwarf_formstring (dwarf_attr (&cudie, DW_AT_GNU_dwo_name,
8845 : : &dwo_at))
8846 [ # # ]: 0 : ?: "<unknown>"));
8847 : 2 : fprintf (stderr,
8848 : : "Could not find split unit '%s', id: %" PRIx64 "\n",
8849 : : dwo_name, unit_id);
8850 : : }
8851 : : }
8852 : : else
8853 : : {
8854 : 8 : Dwarf_CU *split_cu = subdie.cu;
8855 : 8 : dwarf_cu_die (split_cu, &result, NULL, &abbroffset,
8856 : : &addrsize, &offsize, &unit_id, &subdie_off);
8857 : 8 : Dwarf_Off offset = cu->start;
8858 : :
8859 [ + + ]: 8 : if (!silent)
8860 : : {
8861 : 4 : fprintf (out, _(" Split compilation unit at offset %"
8862 : : PRIu64 ":\n"
8863 : : " Version: %" PRIu16
8864 : : ", Abbreviation section offset: %" PRIu64
8865 : : ", Address size: %" PRIu8
8866 : : ", Offset size: %" PRIu8 "\n"),
8867 : : (uint64_t) offset, version, abbroffset,
8868 : : addrsize, offsize);
8869 : 4 : fprintf (out, _(" Unit type: %s (%" PRIu8 ")"),
8870 : : dwarf_unit_name (unit_type), unit_type);
8871 : 4 : fprintf (out, ", Unit id: 0x%.16" PRIx64 "", unit_id);
8872 : 4 : fprintf (out, "\n");
8873 : : }
8874 : :
8875 : 8 : unit_type = DW_UT_split_compile;
8876 : 8 : is_split = true;
8877 : 8 : level = 0;
8878 : 8 : dies[0] = subdie;
8879 : 8 : args.cu = dies[0].cu;
8880 : 8 : args.dbg = split_cu->dbg;
8881 : 8 : args.is_split = is_split;
8882 : 8 : goto do_cu;
8883 : : }
8884 : : }
8885 : :
8886 : : /* And again... */
8887 : 3754 : goto next_cu;
8888 : :
8889 : 208 : do_return:
8890 : 208 : free (dies);
8891 : : }
8892 : :
8893 : : static void
8894 : 206 : print_debug_info_section (Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr,
8895 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg,
8896 : : FILE *out)
8897 : : {
8898 : 42 : print_debug_units (dwflmod, ebl, ehdr, scn, shdr, dbg, false, out);
8899 : 42 : }
8900 : :
8901 : : static void
8902 : 2 : print_debug_types_section (Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr,
8903 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg,
8904 : : FILE *out)
8905 : : {
8906 : 2 : print_debug_units (dwflmod, ebl, ehdr, scn, shdr, dbg, true, out);
8907 : 2 : }
8908 : :
8909 : :
8910 : : static void
8911 : 82 : print_decoded_line_section (Dwfl_Module *dwflmod, Ebl *ebl,
8912 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
8913 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg,
8914 : : FILE *out)
8915 : : {
8916 : 82 : fprintf (out, _("\
8917 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n\n"),
8918 : : elf_ndxscn (scn), section_name (ebl, shdr),
8919 : 82 : (uint64_t) shdr->sh_offset);
8920 : :
8921 : 164 : size_t address_size
8922 [ + + ]: 82 : = elf_getident (ebl->elf, NULL)[EI_CLASS] == ELFCLASS32 ? 4 : 8;
8923 : :
8924 : 82 : Dwarf_Lines *lines;
8925 : 82 : size_t nlines;
8926 : 82 : Dwarf_Off off, next_off = 0;
8927 : 82 : Dwarf_CU *cu = NULL;
8928 : 82 : while (dwarf_next_lines (dbg, off = next_off, &next_off, &cu, NULL, NULL,
8929 [ + + ]: 176 : &lines, &nlines) == 0)
8930 : : {
8931 : 94 : Dwarf_Die cudie;
8932 [ + + + - ]: 94 : if (cu != NULL && dwarf_cu_info (cu, NULL, NULL, &cudie,
8933 : : NULL, NULL, NULL, NULL) == 0)
8934 : 50 : fprintf (out, " CU [%" PRIx64 "] %s\n",
8935 : : dwarf_dieoffset (&cudie), dwarf_diename (&cudie));
8936 : : else
8937 : : {
8938 : : /* DWARF5 lines can be independent of any CU, but they probably
8939 : : are used by some CU. Determine the CU this block is for. */
8940 : 44 : Dwarf_Off cuoffset;
8941 : 44 : Dwarf_Off ncuoffset = 0;
8942 : 44 : size_t hsize;
8943 : 44 : while (dwarf_nextcu (dbg, cuoffset = ncuoffset, &ncuoffset, &hsize,
8944 [ + - ]: 46 : NULL, NULL, NULL) == 0)
8945 : : {
8946 [ - + ]: 46 : if (dwarf_offdie (dbg, cuoffset + hsize, &cudie) == NULL)
8947 : 0 : continue;
8948 : 46 : Dwarf_Attribute stmt_list;
8949 [ - + ]: 46 : if (dwarf_attr (&cudie, DW_AT_stmt_list, &stmt_list) == NULL)
8950 : 0 : continue;
8951 : 46 : Dwarf_Word lineoff;
8952 [ - + ]: 46 : if (dwarf_formudata (&stmt_list, &lineoff) != 0)
8953 : 0 : continue;
8954 [ + + ]: 46 : if (lineoff == off)
8955 : : {
8956 : : /* Found the CU. */
8957 : 44 : cu = cudie.cu;
8958 : 44 : break;
8959 : : }
8960 : : }
8961 : :
8962 [ + - ]: 44 : if (cu != NULL)
8963 : 44 : fprintf (out, " CU [%" PRIx64 "] %s\n",
8964 : : dwarf_dieoffset (&cudie), dwarf_diename (&cudie));
8965 : : else
8966 : 0 : fprintf (out, " No CU\n");
8967 : : }
8968 : :
8969 : 94 : fprintf (out, " line:col SBPE* disc isa op address"
8970 : : " (Statement Block Prologue Epilogue *End)\n");
8971 : 94 : const char *last_file = "";
8972 [ + + ]: 720 : for (size_t n = 0; n < nlines; n++)
8973 : : {
8974 : 626 : Dwarf_Line *line = dwarf_onesrcline (lines, n);
8975 [ - + ]: 626 : if (line == NULL)
8976 : : {
8977 : 0 : fprintf (out, " dwarf_onesrcline: %s\n", dwarf_errmsg (-1));
8978 : 0 : continue;
8979 : : }
8980 : 626 : Dwarf_Word mtime, length;
8981 : 626 : const char *file = dwarf_linesrc (line, &mtime, &length);
8982 [ - + ]: 626 : if (file == NULL)
8983 : : {
8984 : 0 : fprintf (out, " <%s> (mtime: ?, length: ?)\n",
8985 : : dwarf_errmsg (-1));
8986 : 0 : last_file = "";
8987 : : }
8988 [ + + ]: 626 : else if (strcmp (last_file, file) != 0)
8989 : : {
8990 : 114 : fprintf (out, " %s (mtime: %" PRIu64 ", length: %" PRIu64 ")\n",
8991 : : file, mtime, length);
8992 : 114 : last_file = file;
8993 : : }
8994 : :
8995 : 626 : int lineno, colno;
8996 : 626 : bool statement, endseq, block, prologue_end, epilogue_begin;
8997 : 626 : unsigned int lineop, isa, disc;
8998 : 626 : Dwarf_Addr address;
8999 : 626 : dwarf_lineaddr (line, &address);
9000 : 626 : dwarf_lineno (line, &lineno);
9001 : 626 : dwarf_linecol (line, &colno);
9002 : 626 : dwarf_lineop_index (line, &lineop);
9003 : 626 : dwarf_linebeginstatement (line, &statement);
9004 : 626 : dwarf_lineendsequence (line, &endseq);
9005 : 626 : dwarf_lineblock (line, &block);
9006 : 626 : dwarf_lineprologueend (line, &prologue_end);
9007 : 626 : dwarf_lineepiloguebegin (line, &epilogue_begin);
9008 : 626 : dwarf_lineisa (line, &isa);
9009 : 626 : dwarf_linediscriminator (line, &disc);
9010 : :
9011 : : /* End sequence is special, it is one byte past. */
9012 : 626 : fprintf (out, " %4d:%-3d %c%c%c%c%c %4d %3d %2d ",
9013 : : lineno, colno,
9014 [ + + ]: 626 : (statement ? 'S' : ' '),
9015 [ + - ]: 626 : (block ? 'B' : ' '),
9016 [ + + ]: 626 : (prologue_end ? 'P' : ' '),
9017 [ + - ]: 626 : (epilogue_begin ? 'E' : ' '),
9018 [ + + ]: 626 : (endseq ? '*' : ' '),
9019 : : disc, isa, lineop);
9020 : 626 : print_dwarf_addr (dwflmod, address_size,
9021 [ + + ]: 626 : address - (endseq ? 1 : 0), address, out);
9022 : 626 : fprintf (out, "\n");
9023 : :
9024 [ + + ]: 626 : if (endseq)
9025 : 98 : fprintf(out, "\n");
9026 : : }
9027 : : }
9028 : 82 : }
9029 : :
9030 : :
9031 : : /* Print the value of a form.
9032 : : Returns new value of readp, or readendp on failure. */
9033 : : static const unsigned char *
9034 : 144006 : print_form_data (Dwarf *dbg, int form, const unsigned char *readp,
9035 : : const unsigned char *readendp, unsigned int offset_len,
9036 : : Dwarf_Off str_offsets_base, FILE *out)
9037 : : {
9038 : 144006 : Dwarf_Word val;
9039 : 144006 : const unsigned char *endp;
9040 : 144006 : Elf_Data *data;
9041 : 144006 : char *str;
9042 [ + - - - : 144006 : switch (form)
- + - - -
- + - - +
- + - - -
- - ]
9043 : : {
9044 : 16 : case DW_FORM_data1:
9045 [ - + ]: 16 : if (readendp - readp < 1)
9046 : : {
9047 : 0 : invalid_data:
9048 : 0 : error (0, 0, "invalid data");
9049 : 0 : return readendp;
9050 : : }
9051 : 16 : val = *readp++;
9052 : 16 : fprintf (out, " %" PRIx8, (unsigned int) val);
9053 : 16 : break;
9054 : :
9055 : 0 : case DW_FORM_data2:
9056 [ # # ]: 0 : if (readendp - readp < 2)
9057 : 0 : goto invalid_data;
9058 [ # # ]: 0 : val = read_2ubyte_unaligned_inc (dbg, readp);
9059 : 0 : fprintf(out, " %" PRIx16, (unsigned int) val);
9060 : 0 : break;
9061 : :
9062 : 0 : case DW_FORM_data4:
9063 [ # # ]: 0 : if (readendp - readp < 4)
9064 : 0 : goto invalid_data;
9065 [ # # ]: 0 : val = read_4ubyte_unaligned_inc (dbg, readp);
9066 : 0 : fprintf (out, " %" PRIx32, (unsigned int) val);
9067 : 0 : break;
9068 : :
9069 : 0 : case DW_FORM_data8:
9070 [ # # ]: 0 : if (readendp - readp < 8)
9071 : 0 : goto invalid_data;
9072 [ # # ]: 0 : val = read_8ubyte_unaligned_inc (dbg, readp);
9073 : 0 : fprintf (out, " %" PRIx64, val);
9074 : 0 : break;
9075 : :
9076 : 0 : case DW_FORM_sdata:
9077 [ # # ]: 0 : if (readendp - readp < 1)
9078 : 0 : goto invalid_data;
9079 : 0 : get_sleb128 (val, readp, readendp);
9080 : 0 : fprintf (out, " %" PRIx64, val);
9081 : 0 : break;
9082 : :
9083 : 58000 : case DW_FORM_udata:
9084 [ - + ]: 58000 : if (readendp - readp < 1)
9085 : 0 : goto invalid_data;
9086 : 58000 : get_uleb128 (val, readp, readendp);
9087 : 58000 : fprintf (out, " %" PRIx64, val);
9088 : 58000 : break;
9089 : :
9090 : 0 : case DW_FORM_block:
9091 [ # # ]: 0 : if (readendp - readp < 1)
9092 : 0 : goto invalid_data;
9093 : 0 : get_uleb128 (val, readp, readendp);
9094 [ # # ]: 0 : if ((size_t) (readendp - readp) < val)
9095 : 0 : goto invalid_data;
9096 : 0 : print_bytes (val, readp, out);
9097 : 0 : readp += val;
9098 : 0 : break;
9099 : :
9100 : 0 : case DW_FORM_block1:
9101 [ # # ]: 0 : if (readendp - readp < 1)
9102 : 0 : goto invalid_data;
9103 : 0 : val = *readp++;
9104 [ # # ]: 0 : if ((size_t) (readendp - readp) < val)
9105 : 0 : goto invalid_data;
9106 : 0 : print_bytes (val, readp, out);
9107 : 0 : readp += val;
9108 : 0 : break;
9109 : :
9110 : 0 : case DW_FORM_block2:
9111 [ # # ]: 0 : if (readendp - readp < 2)
9112 : 0 : goto invalid_data;
9113 [ # # ]: 0 : val = read_2ubyte_unaligned_inc (dbg, readp);
9114 [ # # ]: 0 : if ((size_t) (readendp - readp) < val)
9115 : 0 : goto invalid_data;
9116 : 0 : print_bytes (val, readp, out);
9117 : 0 : readp += val;
9118 : 0 : break;
9119 : :
9120 : 0 : case DW_FORM_block4:
9121 [ # # ]: 0 : if (readendp - readp < 4)
9122 : 0 : goto invalid_data;
9123 [ # # ]: 0 : val = read_4ubyte_unaligned_inc (dbg, readp);
9124 [ # # ]: 0 : if ((size_t) (readendp - readp) < val)
9125 : 0 : goto invalid_data;
9126 : 0 : print_bytes (val, readp, out);
9127 : 0 : readp += val;
9128 : 0 : break;
9129 : :
9130 : 24 : case DW_FORM_data16:
9131 [ - + ]: 24 : if (readendp - readp < 16)
9132 : 0 : goto invalid_data;
9133 : 24 : print_bytes (16, readp, out);
9134 : 24 : readp += 16;
9135 : 24 : break;
9136 : :
9137 : 0 : case DW_FORM_flag:
9138 [ # # ]: 0 : if (readendp - readp < 1)
9139 : 0 : goto invalid_data;
9140 : 0 : val = *readp++;
9141 [ # # ]: 0 : fprintf (out, "%s", val != 0 ? yes_str : no_str);
9142 : 0 : break;
9143 : :
9144 : 0 : case DW_FORM_string:
9145 : 0 : endp = memchr (readp, '\0', readendp - readp);
9146 [ # # ]: 0 : if (endp == NULL)
9147 : 0 : goto invalid_data;
9148 : 0 : fprintf (out, "%s", readp);
9149 : 0 : readp = endp + 1;
9150 : 0 : break;
9151 : :
9152 : 84410 : case DW_FORM_strp:
9153 : : case DW_FORM_line_strp:
9154 : : case DW_FORM_strp_sup:
9155 [ - + ]: 84410 : if ((size_t) (readendp - readp) < offset_len)
9156 : 0 : goto invalid_data;
9157 [ - + ]: 84410 : if (offset_len == 8)
9158 [ # # ]: 0 : val = read_8ubyte_unaligned_inc (dbg, readp);
9159 : : else
9160 [ - + ]: 84410 : val = read_4ubyte_unaligned_inc (dbg, readp);
9161 [ - + ]: 84410 : if (form == DW_FORM_strp)
9162 : 0 : data = dbg->sectiondata[IDX_debug_str];
9163 [ + - ]: 84410 : else if (form == DW_FORM_line_strp)
9164 : 84410 : data = dbg->sectiondata[IDX_debug_line_str];
9165 : : else /* form == DW_FORM_strp_sup */
9166 : : {
9167 : 0 : Dwarf *alt = dwarf_getalt (dbg);
9168 [ # # ]: 0 : data = alt != NULL ? alt->sectiondata[IDX_debug_str] : NULL;
9169 : : }
9170 [ + - - + ]: 84410 : if (data == NULL || val >= data->d_size
9171 [ - + ]: 84410 : || memchr (data->d_buf + val, '\0', data->d_size - val) == NULL)
9172 : : str = "???";
9173 : : else
9174 : 84410 : str = (char *) data->d_buf + val;
9175 : 84410 : fprintf (out, "%s (%" PRIu64 ")", str, val);
9176 : 84410 : break;
9177 : :
9178 : 0 : case DW_FORM_sec_offset:
9179 [ # # ]: 0 : if ((size_t) (readendp - readp) < offset_len)
9180 : 0 : goto invalid_data;
9181 [ # # ]: 0 : if (offset_len == 8)
9182 [ # # ]: 0 : val = read_8ubyte_unaligned_inc (dbg, readp);
9183 : : else
9184 [ # # ]: 0 : val = read_4ubyte_unaligned_inc (dbg, readp);
9185 : 0 : fprintf (out, "[%" PRIx64 "]", val);
9186 : 0 : break;
9187 : :
9188 : 1556 : case DW_FORM_strx:
9189 : : case DW_FORM_GNU_str_index:
9190 [ - + ]: 1556 : if (readendp - readp < 1)
9191 : 0 : goto invalid_data;
9192 : 1556 : get_uleb128 (val, readp, readendp);
9193 : 1556 : strx_val:
9194 : 1556 : data = dbg->sectiondata[IDX_debug_str_offsets];
9195 [ + - ]: 1556 : if (data == NULL
9196 [ - + ]: 1556 : || data->d_size - str_offsets_base < val * offset_len)
9197 : : str = "???";
9198 : : else
9199 : : {
9200 : 1556 : const unsigned char *strreadp = (data->d_buf + str_offsets_base
9201 : 1556 : + val * offset_len);
9202 : 1556 : const unsigned char *strreadendp = data->d_buf + data->d_size;
9203 [ - + ]: 1556 : if ((size_t) (strreadendp - strreadp) < offset_len)
9204 : : str = "???";
9205 : : else
9206 : : {
9207 : 1556 : Dwarf_Off idx;
9208 [ - + ]: 1556 : if (offset_len == 8)
9209 [ # # ]: 0 : idx = read_8ubyte_unaligned (dbg, strreadp);
9210 : : else
9211 [ - + ]: 1556 : idx = read_4ubyte_unaligned (dbg, strreadp);
9212 : :
9213 : 1556 : data = dbg->sectiondata[IDX_debug_str];
9214 [ - + - + ]: 1556 : if (data == NULL || idx >= data->d_size
9215 [ - + ]: 1556 : || memchr (data->d_buf + idx, '\0',
9216 : : data->d_size - idx) == NULL)
9217 : : str = "???";
9218 : : else
9219 : 1556 : str = (char *) data->d_buf + idx;
9220 : : }
9221 : : }
9222 : 1556 : fprintf (out, "%s (%" PRIu64 ")", str, val);
9223 : 1556 : break;
9224 : :
9225 : 0 : case DW_FORM_strx1:
9226 [ # # ]: 0 : if (readendp - readp < 1)
9227 : 0 : goto invalid_data;
9228 : 0 : val = *readp++;
9229 : 0 : goto strx_val;
9230 : :
9231 : 0 : case DW_FORM_strx2:
9232 [ # # ]: 0 : if (readendp - readp < 2)
9233 : 0 : goto invalid_data;
9234 [ # # ]: 0 : val = read_2ubyte_unaligned_inc (dbg, readp);
9235 : 0 : goto strx_val;
9236 : :
9237 : 0 : case DW_FORM_strx3:
9238 [ # # ]: 0 : if (readendp - readp < 3)
9239 : 0 : goto invalid_data;
9240 : 0 : val = read_3ubyte_unaligned_inc (dbg, readp);
9241 : 0 : goto strx_val;
9242 : :
9243 : 0 : case DW_FORM_strx4:
9244 [ # # ]: 0 : if (readendp - readp < 4)
9245 : 0 : goto invalid_data;
9246 [ # # ]: 0 : val = read_4ubyte_unaligned_inc (dbg, readp);
9247 : 0 : goto strx_val;
9248 : :
9249 : 0 : default:
9250 : 0 : error (0, 0, _("unknown form: %s"), dwarf_form_name (form));
9251 : 0 : return readendp;
9252 : : }
9253 : :
9254 : 144006 : return readp;
9255 : : }
9256 : :
9257 : : /* Only used via run_advance_pc() macro */
9258 : : static inline void
9259 : 786356 : run_advance_pc (unsigned int op_advance,
9260 : : unsigned int minimum_instr_len,
9261 : : unsigned int max_ops_per_instr,
9262 : : unsigned int *op_addr_advance,
9263 : : Dwarf_Word *address,
9264 : : unsigned int *op_index)
9265 : : {
9266 : 786356 : const unsigned int advanced_op_index = (*op_index) + op_advance;
9267 : :
9268 : 786356 : *op_addr_advance = minimum_instr_len * (advanced_op_index
9269 : 786356 : / max_ops_per_instr);
9270 : 786356 : *address = *address + *op_addr_advance;
9271 : 786356 : *op_index = advanced_op_index % max_ops_per_instr;
9272 : : }
9273 : :
9274 : : static void
9275 : 186 : print_debug_line_section (Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr,
9276 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg,
9277 : : FILE *out)
9278 : : {
9279 [ + + ]: 186 : if (decodedline)
9280 : : {
9281 : 82 : print_decoded_line_section (dwflmod, ebl, ehdr, scn, shdr, dbg, out);
9282 : 164 : return;
9283 : : }
9284 : :
9285 : 104 : Elf_Data *data = get_debug_elf_data (dbg, ebl, IDX_debug_line, scn);
9286 [ + - ]: 104 : if (data == NULL)
9287 : : return;
9288 : :
9289 : 104 : fprintf (out, _("\
9290 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
9291 : : elf_ndxscn (scn), section_name (ebl, shdr),
9292 : 104 : (uint64_t) shdr->sh_offset);
9293 : :
9294 [ + - ]: 104 : if (shdr->sh_size == 0)
9295 : : return;
9296 : :
9297 : : /* There is no functionality in libdw to read the information in the
9298 : : way it is represented here. Hardcode the decoder. */
9299 : :
9300 : 104 : const unsigned char *linep = (const unsigned char *) data->d_buf;
9301 : 104 : const unsigned char *lineendp;
9302 : :
9303 : 104 : while (linep
9304 [ + + ]: 3726 : < (lineendp = (const unsigned char *) data->d_buf + data->d_size))
9305 : : {
9306 : 3622 : size_t start_offset = linep - (const unsigned char *) data->d_buf;
9307 : :
9308 : 3622 : fprintf (out, "\n%s %zu:\n",
9309 : : table_offset_lower_str, start_offset);
9310 : :
9311 [ - + ]: 3622 : if (unlikely (linep + 4 > lineendp))
9312 : 0 : goto invalid_data;
9313 [ + + ]: 3622 : Dwarf_Word unit_length = read_4ubyte_unaligned_inc (dbg, linep);
9314 : 3622 : unsigned int length = 4;
9315 [ - + ]: 3622 : if (unlikely (unit_length == 0xffffffff))
9316 : : {
9317 [ # # ]: 0 : if (unlikely (linep + 8 > lineendp))
9318 : : {
9319 : 0 : invalid_data:
9320 : 0 : error (0, 0, _("invalid data in section [%zu] '%s'"),
9321 : : elf_ndxscn (scn), section_name (ebl, shdr));
9322 : 0 : return;
9323 : : }
9324 [ # # ]: 0 : unit_length = read_8ubyte_unaligned_inc (dbg, linep);
9325 : 0 : length = 8;
9326 : : }
9327 : :
9328 : : /* Check whether we have enough room in the section. */
9329 [ - + ]: 3622 : if (unlikely (unit_length > (size_t) (lineendp - linep)))
9330 : 0 : goto invalid_data;
9331 : 3622 : lineendp = linep + unit_length;
9332 : :
9333 : : /* The next element of the header is the version identifier. */
9334 [ - + ]: 3622 : if ((size_t) (lineendp - linep) < 2)
9335 : 0 : goto invalid_data;
9336 [ + + ]: 3622 : uint_fast16_t version = read_2ubyte_unaligned_inc (dbg, linep);
9337 : :
9338 : 7244 : size_t address_size
9339 [ + + ]: 3622 : = elf_getident (ebl->elf, NULL)[EI_CLASS] == ELFCLASS32 ? 4 : 8;
9340 : 3622 : unsigned char segment_selector_size = 0;
9341 [ + + ]: 3622 : if (version > 4)
9342 : : {
9343 [ - + ]: 3470 : if ((size_t) (lineendp - linep) < 2)
9344 : 0 : goto invalid_data;
9345 : 3470 : address_size = *linep++;
9346 : 3470 : segment_selector_size = *linep++;
9347 : : }
9348 : :
9349 : : /* Next comes the header length. */
9350 : 3622 : Dwarf_Word header_length;
9351 [ + - ]: 3622 : if (length == 4)
9352 : : {
9353 [ - + ]: 3622 : if ((size_t) (lineendp - linep) < 4)
9354 : 0 : goto invalid_data;
9355 [ + + ]: 3622 : header_length = read_4ubyte_unaligned_inc (dbg, linep);
9356 : : }
9357 : : else
9358 : : {
9359 [ # # ]: 0 : if ((size_t) (lineendp - linep) < 8)
9360 : 0 : goto invalid_data;
9361 [ # # ]: 0 : header_length = read_8ubyte_unaligned_inc (dbg, linep);
9362 : : }
9363 : :
9364 : 3622 : const unsigned char *header_start = linep;
9365 : :
9366 : : /* Next the minimum instruction length. */
9367 [ - + ]: 3622 : if ((size_t) (lineendp - linep) < 1)
9368 : 0 : goto invalid_data;
9369 : 3622 : uint_fast8_t minimum_instr_len = *linep++;
9370 : :
9371 : : /* Next the maximum operations per instruction, in version 4 format. */
9372 : 3622 : uint_fast8_t max_ops_per_instr;
9373 [ + + ]: 3622 : if (version < 4)
9374 : : max_ops_per_instr = 1;
9375 : : else
9376 : : {
9377 [ - + ]: 3490 : if ((size_t) (lineendp - linep) < 1)
9378 : 0 : goto invalid_data;
9379 : 3490 : max_ops_per_instr = *linep++;
9380 : : }
9381 : :
9382 : : /* We need at least 4 more bytes. */
9383 [ - + ]: 3622 : if ((size_t) (lineendp - linep) < 4)
9384 : 0 : goto invalid_data;
9385 : :
9386 : : /* Then the flag determining the default value of the is_stmt
9387 : : register. */
9388 : 3622 : uint_fast8_t default_is_stmt = *linep++;
9389 : :
9390 : : /* Now the line base. */
9391 : 3622 : int_fast8_t line_base = *linep++;
9392 : :
9393 : : /* And the line range. */
9394 : 3622 : uint_fast8_t line_range = *linep++;
9395 : :
9396 : : /* The opcode base. */
9397 : 3622 : uint_fast8_t opcode_base = *linep++;
9398 : :
9399 : : /* Print what we got so far. */
9400 : 3622 : fprintf (out, "\n"
9401 : : " %s: %" PRIu64 "\n"
9402 : : " %s: %" PRIuFAST16 "\n"
9403 : : " %s: %" PRIu64 "\n"
9404 : : " %s: %zd\n"
9405 : : " %s: %zd\n"
9406 : : " %s: %" PRIuFAST8 "\n"
9407 : : " %s: %" PRIuFAST8 "\n"
9408 : : " %s 'is_stmt': %" PRIuFAST8 "\n"
9409 : : " %s: %" PRIdFAST8 "\n"
9410 : : " %s: %" PRIuFAST8 "\n"
9411 : : " %s: %" PRIuFAST8 "\n"
9412 : : "\n"
9413 : : "%s:\n",
9414 : : length_str, (uint64_t) unit_length, dwarf_ver_str, version,
9415 : : prologue_len_str, (uint64_t) header_length, addr_size_str,
9416 : : address_size, seg_selector_str, (size_t) segment_selector_size,
9417 : : min_inst_len_str, minimum_instr_len, max_op_per_inst_str,
9418 : : max_ops_per_instr, initial_value_if_str, default_is_stmt,
9419 : : line_base_str, line_base, line_range_str, line_range,
9420 : : opcodes_base_str, opcode_base, opcodes_str);
9421 : :
9422 [ - + ]: 3622 : if (version < 2 || version > 5)
9423 : : {
9424 : 0 : error (0, 0, _("cannot handle .debug_line version: %u\n"),
9425 : : (unsigned int) version);
9426 : 0 : linep = lineendp;
9427 : 0 : continue;
9428 : : }
9429 : :
9430 [ - + ]: 3622 : if (address_size != 4 && address_size != 8)
9431 : : {
9432 : 0 : error (0, 0, _("cannot handle address size: %u\n"),
9433 : : (unsigned int) address_size);
9434 : 0 : linep = lineendp;
9435 : 0 : continue;
9436 : : }
9437 : :
9438 [ - + ]: 3622 : if (segment_selector_size != 0)
9439 : : {
9440 : 0 : error (0, 0, _("cannot handle segment selector size: %u\n"),
9441 : : (unsigned int) segment_selector_size);
9442 : 0 : linep = lineendp;
9443 : 0 : continue;
9444 : : }
9445 : :
9446 [ - + ]: 3622 : if (unlikely (linep + opcode_base - 1 >= lineendp))
9447 : : {
9448 : 0 : invalid_unit:
9449 : 0 : error (0, 0,
9450 : 0 : _("invalid data at offset %tu in section [%zu] '%s'"),
9451 : 0 : linep - (const unsigned char *) data->d_buf,
9452 : : elf_ndxscn (scn), section_name (ebl, shdr));
9453 : 0 : linep = lineendp;
9454 : 0 : continue;
9455 : : }
9456 : 3622 : int opcode_base_l10 = 1;
9457 : 3622 : unsigned int tmp = opcode_base;
9458 [ + + ]: 7238 : while (tmp > 10)
9459 : : {
9460 : 3616 : tmp /= 10;
9461 : 3616 : ++opcode_base_l10;
9462 : : }
9463 : : const uint8_t *standard_opcode_lengths = linep - 1;
9464 [ + + ]: 47068 : for (uint_fast8_t cnt = 1; cnt < opcode_base; ++cnt)
9465 : 43446 : fprintf (out, ngettext (" [%*" PRIuFAST8 "] %hhu argument\n",
9466 : : " [%*" PRIuFAST8 "] %hhu arguments\n",
9467 : : (int) linep[cnt - 1]),
9468 : 43446 : opcode_base_l10, cnt, linep[cnt - 1]);
9469 : 3622 : linep += opcode_base - 1;
9470 : :
9471 : 3622 : if (unlikely (linep >= lineendp))
9472 : : goto invalid_unit;
9473 : :
9474 : 3622 : Dwarf_Off str_offsets_base = str_offsets_base_off (dbg, NULL);
9475 : :
9476 : 3622 : fprintf (out, "\n%s:\n", dir_table_str);
9477 [ + + ]: 3622 : if (version > 4)
9478 : : {
9479 : 3470 : struct encpair { uint16_t desc; uint16_t form; };
9480 : 3470 : struct encpair enc[256];
9481 : :
9482 : 3470 : fprintf (out, " [");
9483 : 3470 : if ((size_t) (lineendp - linep) < 1)
9484 : 0 : goto invalid_data;
9485 : 3470 : unsigned char directory_entry_format_count = *linep++;
9486 [ + + ]: 6940 : for (int i = 0; i < directory_entry_format_count; i++)
9487 : : {
9488 : 3470 : uint16_t desc, form;
9489 [ - + ]: 3470 : if ((size_t) (lineendp - linep) < 1)
9490 : 0 : goto invalid_data;
9491 : 3470 : get_uleb128 (desc, linep, lineendp);
9492 [ - + ]: 3470 : if ((size_t) (lineendp - linep) < 1)
9493 : 0 : goto invalid_data;
9494 : 3470 : get_uleb128 (form, linep, lineendp);
9495 : :
9496 : 3470 : enc[i].desc = desc;
9497 : 3470 : enc[i].form = form;
9498 : :
9499 : 3470 : fprintf (out, "%s(%s)",
9500 : : dwarf_line_content_description_name (desc),
9501 : : dwarf_form_name (form));
9502 [ - + ]: 3470 : if (i + 1 < directory_entry_format_count)
9503 : 0 : fprintf (out, ", ");
9504 : : }
9505 : 3470 : fprintf (out, "]\n");
9506 : :
9507 : 3470 : uint64_t directories_count;
9508 [ - + ]: 3470 : if ((size_t) (lineendp - linep) < 1)
9509 : 0 : goto invalid_data;
9510 : 3470 : get_uleb128 (directories_count, linep, lineendp);
9511 : :
9512 : 3470 : if (directory_entry_format_count == 0
9513 [ - + ]: 3470 : && directories_count != 0)
9514 : 0 : goto invalid_data;
9515 : :
9516 [ + + ]: 29864 : for (uint64_t i = 0; i < directories_count; i++)
9517 : : {
9518 : 26394 : fprintf (out, " %-5" PRIu64 " ", i);
9519 [ + + ]: 52788 : for (int j = 0; j < directory_entry_format_count; j++)
9520 : : {
9521 : 26394 : linep = print_form_data (dbg, enc[j].form,
9522 : : linep, lineendp, length,
9523 : : str_offsets_base, out);
9524 [ - + ]: 26394 : if (j + 1 < directory_entry_format_count)
9525 : 0 : fprintf (out, ", ");
9526 : : }
9527 : 26394 : fprintf (out, "\n");
9528 [ - + ]: 26394 : if (linep >= lineendp)
9529 : 0 : goto invalid_unit;
9530 : : }
9531 : : }
9532 : : else
9533 : : {
9534 [ + - + + ]: 766 : while (linep < lineendp && *linep != 0)
9535 : : {
9536 : 614 : const unsigned char *endp = memchr (linep, '\0', lineendp - linep);
9537 [ - + ]: 614 : if (unlikely (endp == NULL))
9538 : 0 : goto invalid_unit;
9539 : :
9540 : 614 : fprintf (out, " %s\n", (char *) linep);
9541 : :
9542 : 614 : linep = endp + 1;
9543 : : }
9544 [ + - - + ]: 152 : if (linep >= lineendp || *linep != 0)
9545 : 0 : goto invalid_unit;
9546 : : /* Skip the final NUL byte. */
9547 : 152 : ++linep;
9548 : : }
9549 : :
9550 [ - + ]: 3622 : if (unlikely (linep >= lineendp))
9551 : 0 : goto invalid_unit;
9552 : :
9553 : 3622 : fprintf (out, "\n%s:\n", file_table_str);
9554 [ + + ]: 3622 : if (version > 4)
9555 : : {
9556 : 3470 : struct encpair { uint16_t desc; uint16_t form; };
9557 : 3470 : struct encpair enc[256];
9558 : :
9559 : 3470 : fprintf (out, " [");
9560 : 3470 : if ((size_t) (lineendp - linep) < 1)
9561 : 0 : goto invalid_data;
9562 : 3470 : unsigned char file_name_format_count = *linep++;
9563 [ + + ]: 10418 : for (int i = 0; i < file_name_format_count; i++)
9564 : : {
9565 : 6948 : uint64_t desc, form;
9566 [ - + ]: 6948 : if ((size_t) (lineendp - linep) < 1)
9567 : 0 : goto invalid_data;
9568 : 6948 : get_uleb128 (desc, linep, lineendp);
9569 [ - + ]: 6948 : if ((size_t) (lineendp - linep) < 1)
9570 : 0 : goto invalid_data;
9571 : 6948 : get_uleb128 (form, linep, lineendp);
9572 : :
9573 [ - + ]: 6948 : if (! libdw_valid_user_form (form))
9574 : 0 : goto invalid_data;
9575 : :
9576 : 6948 : enc[i].desc = desc;
9577 : 6948 : enc[i].form = form;
9578 : :
9579 : 6948 : fprintf (out, "%s(%s)",
9580 : : dwarf_line_content_description_name (desc),
9581 : : dwarf_form_name (form));
9582 [ + + ]: 6948 : if (i + 1 < file_name_format_count)
9583 : 3478 : fprintf (out, ", ");
9584 : : }
9585 : 3470 : fprintf (out, "]\n");
9586 : :
9587 : 3470 : uint64_t file_name_count;
9588 [ - + ]: 3470 : if ((size_t) (lineendp - linep) < 1)
9589 : 0 : goto invalid_data;
9590 : 3470 : get_uleb128 (file_name_count, linep, lineendp);
9591 : :
9592 : 3470 : if (file_name_format_count == 0
9593 [ - + ]: 3470 : && file_name_count != 0)
9594 : 0 : goto invalid_data;
9595 : :
9596 [ + + ]: 61486 : for (uint64_t i = 0; i < file_name_count; i++)
9597 : : {
9598 : 58016 : fprintf (out, " %-5" PRIu64 " ", i);
9599 [ + + ]: 174072 : for (int j = 0; j < file_name_format_count; j++)
9600 : : {
9601 : 116056 : linep = print_form_data (dbg, enc[j].form,
9602 : : linep, lineendp, length,
9603 : : str_offsets_base, out);
9604 [ + + ]: 116056 : if (j + 1 < file_name_format_count)
9605 : 58040 : fprintf (out, ", ");
9606 : : }
9607 : 58016 : fprintf (out, "\n");
9608 [ - + ]: 58016 : if (linep > lineendp)
9609 : 0 : goto invalid_unit;
9610 : : }
9611 : : }
9612 : : else
9613 : : {
9614 : 152 : fputs (_(" Entry Dir Time Size Name\n"), out);
9615 [ + - + + ]: 6154 : for (unsigned int cnt = 1; linep < lineendp && *linep != 0; ++cnt)
9616 : : {
9617 : : /* First comes the file name. */
9618 : 6002 : char *fname = (char *) linep;
9619 : 6002 : unsigned char *endp = memchr (fname, '\0', lineendp - linep);
9620 [ - + ]: 6002 : if (unlikely (endp == NULL))
9621 : 0 : goto invalid_unit;
9622 : 6002 : linep = endp + 1;
9623 : :
9624 : : /* Then the index. */
9625 : 6002 : unsigned int diridx;
9626 [ - + ]: 6002 : if (lineendp - linep < 1)
9627 : 0 : goto invalid_unit;
9628 : 6002 : get_uleb128 (diridx, linep, lineendp);
9629 : :
9630 : : /* Next comes the modification time. */
9631 : 6002 : unsigned int mtime;
9632 [ - + ]: 6002 : if (lineendp - linep < 1)
9633 : 0 : goto invalid_unit;
9634 : 6002 : get_uleb128 (mtime, linep, lineendp);
9635 : :
9636 : : /* Finally the length of the file. */
9637 : 6002 : unsigned int fsize;
9638 [ - + ]: 6002 : if (lineendp - linep < 1)
9639 : 0 : goto invalid_unit;
9640 : 6002 : get_uleb128 (fsize, linep, lineendp);
9641 : :
9642 : 6002 : fprintf (out, " %-5u %-5u %-9u %-9u %s\n",
9643 : : cnt, diridx, mtime, fsize, fname);
9644 : : }
9645 [ + - - + ]: 152 : if (linep >= lineendp || *linep != '\0')
9646 : 0 : goto invalid_unit;
9647 : : /* Skip the final NUL byte. */
9648 : 152 : ++linep;
9649 : : }
9650 : :
9651 : 3622 : unsigned int debug_str_offset = 0;
9652 [ + + ]: 3622 : if (unlikely (linep == header_start + header_length - 4))
9653 : : {
9654 : : /* CUBINs contain an unsigned 4-byte offset */
9655 [ - + ]: 2 : debug_str_offset = read_4ubyte_unaligned_inc (dbg, linep);
9656 : : }
9657 : :
9658 [ + + ]: 3622 : if (linep == lineendp)
9659 : : {
9660 : 40 : fprintf (out, "\n%s.\n", no_line_num_stmts_str);
9661 : 40 : continue;
9662 : : }
9663 : :
9664 : 3582 : fprintf (out, "\n%s:\n", line_num_stmts_str);
9665 : 3582 : Dwarf_Word address = 0;
9666 : 3582 : unsigned int op_index = 0;
9667 : 3582 : size_t line = 1;
9668 : 3582 : uint_fast8_t is_stmt = default_is_stmt;
9669 : :
9670 : : /* Apply the "operation advance" from a special opcode
9671 : : or DW_LNS_advance_pc (as per DWARF4 6.2.5.1). */
9672 : 3582 : unsigned int op_addr_advance;
9673 : : #define advance_pc(op_advance) run_advance_pc(op_advance, minimum_instr_len, \
9674 : : max_ops_per_instr, &op_addr_advance, &address, &op_index)
9675 : :
9676 [ - + ]: 3582 : if (max_ops_per_instr == 0)
9677 : : {
9678 : 0 : error (0, 0,
9679 : 0 : _("invalid maximum operations per instruction is zero"));
9680 : 0 : linep = lineendp;
9681 : 0 : continue;
9682 : : }
9683 : :
9684 [ + + ]: 2530610 : while (linep < lineendp)
9685 : : {
9686 : 2527028 : size_t offset = linep - (const unsigned char *) data->d_buf;
9687 : 2527028 : unsigned int u128;
9688 : 2527028 : int s128;
9689 : :
9690 : : /* Read the opcode. */
9691 : 2527028 : unsigned int opcode = *linep++;
9692 : :
9693 : 2527028 : fprintf (out, " [%6" PRIx64 "]", (uint64_t)offset);
9694 : : /* Is this a special opcode? */
9695 [ + + ]: 2527028 : if (likely (opcode >= opcode_base))
9696 : : {
9697 [ - + ]: 717060 : if (unlikely (line_range == 0))
9698 : 0 : goto invalid_unit;
9699 : :
9700 : : /* Yes. Handling this is quite easy since the opcode value
9701 : : is computed with
9702 : :
9703 : : opcode = (desired line increment - line_base)
9704 : : + (line_range * address advance) + opcode_base
9705 : : */
9706 : 717060 : int line_increment = (line_base
9707 : 717060 : + (opcode - opcode_base) % line_range);
9708 : :
9709 : : /* Perform the increments. */
9710 : 717060 : line += line_increment;
9711 : 717060 : advance_pc ((opcode - opcode_base) / line_range);
9712 : :
9713 : 717060 : fprintf (out, " %s %u: %s+%u = ",
9714 : : special_opcode_str, opcode, address_str, op_addr_advance);
9715 : 717060 : print_dwarf_addr (dwflmod, 0, address, address, out);
9716 [ - + ]: 717060 : if (op_index > 0)
9717 : 0 : fprintf (out, ", op_index = %u, %s%+d = %zu\n",
9718 : : op_index, line_str, line_increment, line);
9719 : : else
9720 : 717060 : fprintf (out, ", %s%+d = %zu\n",
9721 : : line_str, line_increment, line);
9722 : : }
9723 [ + + ]: 1809968 : else if (opcode == 0)
9724 : : {
9725 : : /* This an extended opcode. */
9726 [ - + ]: 191488 : if (unlikely (linep + 2 > lineendp))
9727 : 0 : goto invalid_unit;
9728 : :
9729 : : /* The length. */
9730 : 191488 : unsigned int len = *linep++;
9731 : :
9732 [ - + ]: 191488 : if (unlikely (linep + len > lineendp))
9733 : 0 : goto invalid_unit;
9734 : :
9735 : : /* The sub-opcode. */
9736 : 191488 : opcode = *linep++;
9737 : :
9738 : 191488 : fprintf (out, " %s %u: ", ext_opcode_str, opcode);
9739 : :
9740 [ + + - + : 191488 : switch (opcode)
+ - - ]
9741 : : {
9742 : 10606 : case DW_LNE_end_sequence:
9743 : 10606 : fprintf (out, " %s\n", end_of_seq_str);
9744 : :
9745 : : /* Reset the registers we care about. */
9746 : 10606 : address = 0;
9747 : 10606 : op_index = 0;
9748 : 10606 : line = 1;
9749 : 10606 : is_stmt = default_is_stmt;
9750 : 10606 : break;
9751 : :
9752 : 12330 : case DW_LNE_set_address:
9753 : 12330 : op_index = 0;
9754 [ - + ]: 12330 : if (unlikely ((size_t) (lineendp - linep) < address_size))
9755 : 0 : goto invalid_unit;
9756 [ + + ]: 12330 : if (address_size == 4)
9757 [ + + ]: 50 : address = read_4ubyte_unaligned_inc (dbg, linep);
9758 : : else
9759 [ + + ]: 12280 : address = read_8ubyte_unaligned_inc (dbg, linep);
9760 : : {
9761 : 12330 : fprintf (out, " %s ", set_addr_to_str);
9762 : 12330 : print_dwarf_addr (dwflmod, 0, address, address, out);
9763 : 12330 : fprintf (out, "\n");
9764 : : }
9765 : 12330 : break;
9766 : :
9767 : 0 : case DW_LNE_define_file:
9768 : : {
9769 : 0 : char *fname = (char *) linep;
9770 : 0 : const unsigned char *endp = memchr (linep, '\0',
9771 : 0 : lineendp - linep);
9772 [ # # ]: 0 : if (unlikely (endp == NULL))
9773 : 0 : goto invalid_unit;
9774 : 0 : linep = endp + 1;
9775 : :
9776 : 0 : unsigned int diridx;
9777 [ # # ]: 0 : if (lineendp - linep < 1)
9778 : 0 : goto invalid_unit;
9779 : 0 : get_uleb128 (diridx, linep, lineendp);
9780 : 0 : Dwarf_Word mtime;
9781 [ # # ]: 0 : if (lineendp - linep < 1)
9782 : 0 : goto invalid_unit;
9783 : 0 : get_uleb128 (mtime, linep, lineendp);
9784 : 0 : Dwarf_Word filelength;
9785 [ # # ]: 0 : if (lineendp - linep < 1)
9786 : 0 : goto invalid_unit;
9787 : 0 : get_uleb128 (filelength, linep, lineendp);
9788 : :
9789 : 0 : fprintf (out, _("\
9790 : : define new file: dir=%u, mtime=%" PRIu64 ", length=%" PRIu64 ", name=%s\n"),
9791 : : diridx, (uint64_t) mtime, (uint64_t) filelength,
9792 : : fname);
9793 : : }
9794 : 0 : break;
9795 : :
9796 : 168538 : case DW_LNE_set_discriminator:
9797 : : /* Takes one ULEB128 parameter, the discriminator. */
9798 [ + - - + ]: 168538 : if (unlikely (standard_opcode_lengths[opcode] != 1
9799 : : || lineendp - linep < 1))
9800 : 0 : goto invalid_unit;
9801 : :
9802 : 168538 : get_uleb128 (u128, linep, lineendp);
9803 : 168538 : fprintf (out, " %s %u\n", set_disc_str, u128);
9804 : 168538 : break;
9805 : :
9806 : 14 : case DW_LNE_NVIDIA_inlined_call:
9807 : : {
9808 [ - + ]: 14 : if (unlikely (linep >= lineendp))
9809 : 0 : goto invalid_data;
9810 : :
9811 : 14 : unsigned int context;
9812 : 14 : get_uleb128 (context, linep, lineendp);
9813 : :
9814 [ - + ]: 14 : if (unlikely (linep >= lineendp))
9815 : 0 : goto invalid_data;
9816 : :
9817 : 14 : unsigned int function_name;
9818 : 14 : get_uleb128 (function_name, linep, lineendp);
9819 : 14 : function_name += debug_str_offset;
9820 : :
9821 : 14 : Elf_Data *str_data = dbg->sectiondata[IDX_debug_str];
9822 : 14 : char *function_str;
9823 [ + - - + ]: 14 : if (str_data == NULL || function_name >= str_data->d_size
9824 [ - + ]: 14 : || memchr (str_data->d_buf + function_name, '\0',
9825 : : str_data->d_size - function_name) == NULL)
9826 : : function_str = "???";
9827 : : else
9828 : 14 : function_str = (char *) str_data->d_buf + function_name;
9829 : :
9830 : 14 : fprintf (out, _(" set inlined context %u,"
9831 : : " function name %s (0x%x)\n"),
9832 : : context, function_str, function_name);
9833 : 14 : break;
9834 : : }
9835 : :
9836 : 0 : case DW_LNE_NVIDIA_set_function_name:
9837 : : {
9838 [ # # ]: 0 : if (unlikely (linep >= lineendp))
9839 : 0 : goto invalid_data;
9840 : :
9841 : 0 : unsigned int function_name;
9842 : 0 : get_uleb128 (function_name, linep, lineendp);
9843 : 0 : function_name += debug_str_offset;
9844 : :
9845 : 0 : Elf_Data *str_data = dbg->sectiondata[IDX_debug_str];
9846 : 0 : char *function_str;
9847 [ # # # # ]: 0 : if (str_data == NULL || function_name >= str_data->d_size
9848 [ # # ]: 0 : || memchr (str_data->d_buf + function_name, '\0',
9849 : : str_data->d_size - function_name) == NULL)
9850 : : function_str = "???";
9851 : : else
9852 : 0 : function_str = (char *) str_data->d_buf + function_name;
9853 : :
9854 : 0 : fprintf (out, _(" set function name %s (0x%x)\n"),
9855 : : function_str, function_name);
9856 : : }
9857 : 0 : break;
9858 : :
9859 : 0 : default:
9860 : : /* Unknown, ignore it. */
9861 : 0 : fputs (_(" unknown opcode\n"), out);
9862 : 0 : linep += len - 1;
9863 : 0 : break;
9864 : : }
9865 : : }
9866 [ + - ]: 1618480 : else if (opcode <= DW_LNS_set_isa)
9867 : : {
9868 : : /* This is a known standard opcode. */
9869 [ + + + + : 1618480 : switch (opcode)
+ + - + +
+ - - ]
9870 : : {
9871 : 252084 : case DW_LNS_copy:
9872 : : /* Takes no argument. */
9873 : 252084 : fprintf (out, " %s\n", copy_str);
9874 : 252084 : break;
9875 : :
9876 : 15436 : case DW_LNS_advance_pc:
9877 : : /* Takes one uleb128 parameter which is added to the
9878 : : address. */
9879 [ - + ]: 15436 : if (lineendp - linep < 1)
9880 : 0 : goto invalid_unit;
9881 : 15436 : get_uleb128 (u128, linep, lineendp);
9882 : 15436 : advance_pc (u128);
9883 : : {
9884 : 15436 : fprintf (out, " %s %u %s ",
9885 : : adv_addr_str, op_addr_advance, to_str);
9886 : 15436 : print_dwarf_addr (dwflmod, 0, address, address, out);
9887 [ - + ]: 15436 : if (op_index > 0)
9888 : 0 : fprintf (out, ", op_index %s %u", to_str, op_index);
9889 : 15436 : fprintf (out, "\n");
9890 : : }
9891 : 15436 : break;
9892 : :
9893 : 186546 : case DW_LNS_advance_line:
9894 : : /* Takes one sleb128 parameter which is added to the
9895 : : line. */
9896 [ - + ]: 186546 : if (lineendp - linep < 1)
9897 : 0 : goto invalid_unit;
9898 : 186546 : get_sleb128 (s128, linep, lineendp);
9899 : 186546 : line += s128;
9900 : 186546 : fprintf (out, "\
9901 : : %s %d to %" PRId64 "\n",
9902 : : adv_line_by_str, s128, (int64_t) line);
9903 : 186546 : break;
9904 : :
9905 : 65008 : case DW_LNS_set_file:
9906 : : /* Takes one uleb128 parameter which is stored in file. */
9907 [ - + ]: 65008 : if (lineendp - linep < 1)
9908 : 0 : goto invalid_unit;
9909 : 65008 : get_uleb128 (u128, linep, lineendp);
9910 : 65008 : fprintf (out, " %s %" PRIu64 "\n",
9911 : : set_file_str, (uint64_t) u128);
9912 : 65008 : break;
9913 : :
9914 : 612232 : case DW_LNS_set_column:
9915 : : /* Takes one uleb128 parameter which is stored in column. */
9916 [ + - - + ]: 612232 : if (unlikely (standard_opcode_lengths[opcode] != 1
9917 : : || lineendp - linep < 1))
9918 : 0 : goto invalid_unit;
9919 : :
9920 : 612232 : get_uleb128 (u128, linep, lineendp);
9921 : 612232 : fprintf (out, " %s %" PRIu64 "\n",
9922 : : set_col_str, (uint64_t) u128);
9923 : 612232 : break;
9924 : :
9925 : 433260 : case DW_LNS_negate_stmt:
9926 : : /* Takes no argument. */
9927 : 433260 : is_stmt = 1 - is_stmt;
9928 : 433260 : fprintf (out, " %s '%s' %s %" PRIuFAST8 "\n",
9929 : : set_str, "is_stmt", to_str, is_stmt);
9930 : 433260 : break;
9931 : :
9932 : 0 : case DW_LNS_set_basic_block:
9933 : : /* Takes no argument. */
9934 : 0 : fputs (_(" set basic block flag\n"), out);
9935 : 0 : break;
9936 : :
9937 : 53860 : case DW_LNS_const_add_pc:
9938 : : /* Takes no argument. */
9939 : :
9940 [ - + ]: 53860 : if (unlikely (line_range == 0))
9941 : 0 : goto invalid_unit;
9942 : :
9943 : 53860 : advance_pc ((255 - opcode_base) / line_range);
9944 : : {
9945 : 53860 : fprintf (out, " %s %u to ",
9946 : : adv_addr_by_str, op_addr_advance);
9947 : 53860 : print_dwarf_addr (dwflmod, 0, address, address, out);
9948 [ - + ]: 53860 : if (op_index > 0)
9949 : 0 : fprintf (out, _(", op_index to %u"), op_index);
9950 : 53860 : fprintf (out, "\n");
9951 : : }
9952 : 53860 : break;
9953 : :
9954 : 44 : case DW_LNS_fixed_advance_pc:
9955 : : /* Takes one 16 bit parameter which is added to the
9956 : : address. */
9957 [ + - - + ]: 44 : if (unlikely (standard_opcode_lengths[opcode] != 1
9958 : : || lineendp - linep < 2))
9959 : 0 : goto invalid_unit;
9960 : :
9961 [ - + ]: 44 : u128 = read_2ubyte_unaligned_inc (dbg, linep);
9962 : 44 : address += u128;
9963 : 44 : op_index = 0;
9964 : : {
9965 : 44 : fprintf (out, _("\
9966 : : advance address by fixed value %u to \n"),
9967 : : u128);
9968 : 44 : print_dwarf_addr (dwflmod, 0, address, address, out);
9969 : 44 : fprintf (out, "\n");
9970 : : }
9971 : 44 : break;
9972 : :
9973 : 10 : case DW_LNS_set_prologue_end:
9974 : : /* Takes no argument. */
9975 : 10 : fputs (_(" set prologue end flag\n"), out);
9976 : 10 : break;
9977 : :
9978 : 0 : case DW_LNS_set_epilogue_begin:
9979 : : /* Takes no argument. */
9980 : 0 : fputs (_(" set epilogue begin flag\n"), out);
9981 : 0 : break;
9982 : :
9983 : 0 : case DW_LNS_set_isa:
9984 : : /* Takes one uleb128 parameter which is stored in isa. */
9985 [ # # # # ]: 0 : if (unlikely (standard_opcode_lengths[opcode] != 1
9986 : : || lineendp - linep < 1))
9987 : 0 : goto invalid_unit;
9988 : :
9989 : 0 : get_uleb128 (u128, linep, lineendp);
9990 : 0 : fprintf (out, _(" set isa to %u\n"), u128);
9991 : 0 : break;
9992 : : }
9993 : : }
9994 : : else
9995 : : {
9996 : : /* This is a new opcode the generator but not we know about.
9997 : : Read the parameters associated with it but then discard
9998 : : everything. Read all the parameters for this opcode. */
9999 : 0 : fprintf (out,
10000 : 0 : ngettext (" unknown opcode with %" PRIu8 " parameter:",
10001 : : " unknown opcode with %" PRIu8 " parameters:",
10002 : : standard_opcode_lengths[opcode]),
10003 : 0 : standard_opcode_lengths[opcode]);
10004 : 0 : for (int n = standard_opcode_lengths[opcode];
10005 [ # # # # ]: 0 : n > 0 && linep < lineendp; --n)
10006 : : {
10007 : 0 : get_uleb128 (u128, linep, lineendp);
10008 [ # # ]: 0 : if (n != standard_opcode_lengths[opcode])
10009 : 0 : fputc (',', out);
10010 : 0 : fprintf (out, " %u", u128);
10011 : : }
10012 : :
10013 : : /* Next round, ignore this opcode. */
10014 : 0 : continue;
10015 : : }
10016 : : }
10017 : : }
10018 : :
10019 : : /* There must only be one data block. */
10020 [ - + ]: 104 : assert (elf_getdata (scn, data) == NULL);
10021 : : }
10022 : :
10023 : :
10024 : : static void
10025 : 40 : print_debug_loclists_section (Dwfl_Module *dwflmod,
10026 : : Ebl *ebl,
10027 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
10028 : : Elf_Scn *scn, GElf_Shdr *shdr,
10029 : : Dwarf *dbg, FILE *out)
10030 : : {
10031 : 40 : Elf_Data *data = get_debug_elf_data (dbg, ebl, IDX_debug_loclists, scn);
10032 [ + - ]: 40 : if (data == NULL)
10033 : 0 : return;
10034 : :
10035 : 40 : fprintf (out, _("\
10036 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
10037 : : elf_ndxscn (scn), section_name (ebl, shdr),
10038 : 40 : (uint64_t) shdr->sh_offset);
10039 : :
10040 : : /* For the listptr to get the base address/CU. */
10041 : 40 : sort_listptr (&known_loclistsptr, "loclistsptr");
10042 : 40 : size_t listptr_idx = 0;
10043 : :
10044 : 40 : const unsigned char *readp = data->d_buf;
10045 : 40 : const unsigned char *const dataend = ((unsigned char *) data->d_buf
10046 : 40 : + data->d_size);
10047 [ + + ]: 3132 : while (readp < dataend)
10048 : : {
10049 [ - + ]: 3092 : if (unlikely (readp > dataend - 4))
10050 : : {
10051 : 0 : invalid_data:
10052 : 0 : error (0, 0, _("invalid data in section [%zu] '%s'"),
10053 : : elf_ndxscn (scn), section_name (ebl, shdr));
10054 : 0 : return;
10055 : : }
10056 : :
10057 : 3092 : ptrdiff_t offset = readp - (unsigned char *) data->d_buf;
10058 : 3092 : fprintf (out, "%s 0x%" PRIx64 ":\n\n",
10059 : : table_offset_upper_str, (uint64_t) offset);
10060 : :
10061 [ - + ]: 3092 : uint64_t unit_length = read_4ubyte_unaligned_inc (dbg, readp);
10062 : 3092 : unsigned int offset_size = 4;
10063 [ - + ]: 3092 : if (unlikely (unit_length == 0xffffffff))
10064 : : {
10065 [ # # ]: 0 : if (unlikely (readp > dataend - 8))
10066 : 0 : goto invalid_data;
10067 : :
10068 [ # # ]: 0 : unit_length = read_8ubyte_unaligned_inc (dbg, readp);
10069 : 0 : offset_size = 8;
10070 : : }
10071 : 3092 : fprintf (out, " %s: %8" PRIu64 "\n", length_str, unit_length);
10072 : :
10073 : : /* We need at least 2-bytes + 1-byte + 1-byte + 4-bytes = 8
10074 : : bytes to complete the header. And this unit cannot go beyond
10075 : : the section data. */
10076 [ + - ]: 3092 : if (readp > dataend - 8
10077 [ + - ]: 3092 : || unit_length < 8
10078 [ - + ]: 3092 : || unit_length > (uint64_t) (dataend - readp))
10079 : 0 : goto invalid_data;
10080 : :
10081 : 3092 : const unsigned char *nexthdr = readp + unit_length;
10082 : :
10083 [ - + ]: 3092 : uint16_t version = read_2ubyte_unaligned_inc (dbg, readp);
10084 : 3092 : fprintf (out, " %s: %8" PRIu16 "\n", dwarf_ver_str, version);
10085 : :
10086 [ - + ]: 3092 : if (version != 5)
10087 : : {
10088 : 0 : error (0, 0, _("Unknown version"));
10089 : 0 : goto next_table;
10090 : : }
10091 : :
10092 : 3092 : uint8_t address_size = *readp++;
10093 : 3092 : fprintf (out, " %s: %8" PRIu64 "\n",
10094 : : addr_size_str, (uint64_t) address_size);
10095 : :
10096 [ - + ]: 3092 : if (address_size != 4 && address_size != 8)
10097 : : {
10098 : 0 : error (0, 0, _("unsupported address size"));
10099 : 0 : goto next_table;
10100 : : }
10101 : :
10102 : 3092 : uint8_t segment_size = *readp++;
10103 : 3092 : fprintf (out, " %s: %8" PRIu64 "\n",
10104 : : seg_size_str, (uint64_t) segment_size);
10105 : :
10106 [ - + ]: 3092 : if (segment_size != 0)
10107 : : {
10108 : 0 : error (0, 0, _("unsupported segment size"));
10109 : 0 : goto next_table;
10110 : : }
10111 : :
10112 [ - + ]: 3092 : uint32_t offset_entry_count = read_4ubyte_unaligned_inc (dbg, readp);
10113 : 3092 : fprintf (out, " %s: %8" PRIu64 "\n",
10114 : : offset_ent_str, (uint64_t) offset_entry_count);
10115 : :
10116 : : /* We need the CU that uses this unit to get the initial base address. */
10117 : 3092 : Dwarf_Addr cu_base = 0;
10118 : 3092 : struct Dwarf_CU *cu = NULL;
10119 [ - + ]: 3092 : if (listptr_cu (&known_loclistsptr, &listptr_idx,
10120 : : (Dwarf_Off) offset,
10121 : 3092 : (Dwarf_Off) (nexthdr - (unsigned char *) data->d_buf),
10122 : : &cu_base, &cu)
10123 [ # # ]: 0 : || split_dwarf_cu_base (dbg, &cu, &cu_base))
10124 : 3092 : {
10125 : 3092 : Dwarf_Die cudie;
10126 [ - + ]: 3092 : if (dwarf_cu_die (cu, &cudie,
10127 : : NULL, NULL, NULL, NULL,
10128 : : NULL, NULL) == NULL)
10129 : 0 : fprintf (out, " %s: ", unknown_base_str);
10130 : : else
10131 : 3092 : fprintf (out, " %s [%6" PRIx64 "] %s: ",
10132 : : cu_str, dwarf_dieoffset (&cudie), base_str);
10133 : 3092 : print_dwarf_addr (dwflmod, address_size, cu_base, cu_base, out);
10134 : 3092 : fprintf (out, "\n");
10135 : : }
10136 : : else
10137 : 0 : fprintf (out, _(" Not associated with a CU.\n"));
10138 : :
10139 : 3092 : fprintf (out, "\n");
10140 : :
10141 : 3092 : const unsigned char *offset_array_start = readp;
10142 [ + + ]: 3092 : if (offset_entry_count > 0)
10143 : : {
10144 : 4 : uint64_t max_entries = (unit_length - 8) / offset_size;
10145 [ - + ]: 4 : if (offset_entry_count > max_entries)
10146 : : {
10147 : 0 : error (0, 0,
10148 : 0 : _("too many offset entries for unit length"));
10149 : 0 : offset_entry_count = max_entries;
10150 : : }
10151 : :
10152 : 4 : fprintf (out, _(" Offsets starting at 0x%" PRIx64 ":\n"),
10153 : : (uint64_t) (offset_array_start
10154 : 4 : - (unsigned char *) data->d_buf));
10155 [ + + ]: 36 : for (uint32_t idx = 0; idx < offset_entry_count; idx++)
10156 : : {
10157 : 32 : fprintf (out, " [%6" PRIu32 "] ", idx);
10158 [ + - ]: 32 : if (offset_size == 4)
10159 : : {
10160 [ - + ]: 32 : uint32_t off = read_4ubyte_unaligned_inc (dbg, readp);
10161 : 32 : fprintf (out, "0x%" PRIx32 "\n", off);
10162 : : }
10163 : : else
10164 : : {
10165 [ # # ]: 0 : uint64_t off = read_8ubyte_unaligned_inc (dbg, readp);
10166 : 0 : fprintf (out, "0x%" PRIx64 "\n", off);
10167 : : }
10168 : : }
10169 : 4 : fprintf (out, "\n");
10170 : : }
10171 : :
10172 : 3092 : Dwarf_Addr base = cu_base;
10173 : 3092 : bool start_of_list = true;
10174 [ + + ]: 768674 : while (readp < nexthdr)
10175 : : {
10176 : 765582 : Dwarf_Off off = (Dwarf_Off) (readp - (unsigned char *) data->d_buf);
10177 [ + + ]: 765582 : if (listptr_attr (&known_loclistsptr, listptr_idx, off,
10178 : : DW_AT_GNU_locviews))
10179 : 109918 : {
10180 : 109918 : Dwarf_Off next_off = next_listptr_offset (&known_loclistsptr,
10181 : : &listptr_idx, off);
10182 : 109918 : const unsigned char *locp = readp;
10183 : 109918 : const unsigned char *locendp;
10184 [ + - ]: 109918 : if (next_off == 0
10185 [ + - ]: 109918 : || next_off > (size_t) (nexthdr - ((const unsigned char *)
10186 : : data->d_buf)))
10187 : : locendp = nexthdr;
10188 : : else
10189 : 109918 : locendp = (const unsigned char *) data->d_buf + next_off;
10190 : :
10191 : 109918 : fprintf (out, " Offset: %" PRIx64 ", Index: %" PRIx64 "\n",
10192 : : (uint64_t) (readp - (unsigned char *) data->d_buf),
10193 : 109918 : (uint64_t) (readp - offset_array_start));
10194 : :
10195 [ + + ]: 609866 : while (locp < locendp)
10196 : : {
10197 : 499948 : uint64_t v1, v2;
10198 : 499948 : get_uleb128 (v1, locp, locendp);
10199 [ - + ]: 499948 : if (locp >= locendp)
10200 : : {
10201 : 0 : fprintf (out, _(" <INVALID DATA>\n"));
10202 : 0 : break;
10203 : : }
10204 : 499948 : get_uleb128 (v2, locp, locendp);
10205 : 499948 : fprintf (out,
10206 : : " view pair %" PRId64 ", %" PRId64 "\n", v1, v2);
10207 : : }
10208 : :
10209 : 109918 : fprintf (out, "\n");
10210 : 109918 : readp = (unsigned char *) locendp;
10211 : 109918 : continue;
10212 : : }
10213 : :
10214 : 655664 : uint8_t kind = *readp++;
10215 : 655664 : uint64_t op1, op2, len;
10216 : :
10217 : : /* Skip padding. */
10218 [ - + ]: 655664 : if (start_of_list && kind == DW_LLE_end_of_list)
10219 : 0 : continue;
10220 : :
10221 [ + + ]: 655664 : if (start_of_list)
10222 : : {
10223 : 109982 : base = cu_base;
10224 : 109982 : fprintf (out, " Offset: %" PRIx64 ", Index: %" PRIx64 "\n",
10225 : 109982 : (uint64_t) (readp - (unsigned char *) data->d_buf - 1),
10226 : 109982 : (uint64_t) (readp - offset_array_start - 1));
10227 : 109982 : start_of_list = false;
10228 : : }
10229 : :
10230 : 655664 : fprintf (out, " %s", dwarf_loc_list_encoding_name (kind));
10231 [ + - - + : 655664 : switch (kind)
+ - + - +
- - ]
10232 : : {
10233 : 109982 : case DW_LLE_end_of_list:
10234 : 109982 : start_of_list = true;
10235 : 109982 : fprintf (out, "\n\n");
10236 : 109982 : break;
10237 : :
10238 : 0 : case DW_LLE_base_addressx:
10239 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
10240 : : {
10241 : 0 : invalid_entry:
10242 : 0 : error (0, 0, _("invalid loclists data"));
10243 : 0 : goto next_table;
10244 : : }
10245 : 0 : get_uleb128 (op1, readp, nexthdr);
10246 : 0 : fprintf (out, " %" PRIx64 "\n", op1);
10247 [ # # ]: 0 : if (! print_unresolved_addresses)
10248 : : {
10249 : 0 : Dwarf_Addr addr;
10250 [ # # ]: 0 : if (get_indexed_addr (cu, op1, &addr) != 0)
10251 : 0 : fprintf (out, " ???\n");
10252 : : else
10253 : : {
10254 : 0 : fprintf (out, " ");
10255 : 0 : print_dwarf_addr (dwflmod, address_size, addr, addr, out);
10256 : 0 : fprintf (out, "\n");
10257 : : }
10258 : : }
10259 : : break;
10260 : :
10261 : 0 : case DW_LLE_startx_endx:
10262 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
10263 : 0 : goto invalid_entry;
10264 : 0 : get_uleb128 (op1, readp, nexthdr);
10265 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
10266 : 0 : goto invalid_entry;
10267 : 0 : get_uleb128 (op2, readp, nexthdr);
10268 : 0 : fprintf (out, " %" PRIx64 ", %" PRIx64 "\n", op1, op2);
10269 [ # # ]: 0 : if (! print_unresolved_addresses)
10270 : : {
10271 : 0 : Dwarf_Addr addr1;
10272 : 0 : Dwarf_Addr addr2;
10273 [ # # ]: 0 : if (get_indexed_addr (cu, op1, &addr1) != 0
10274 [ # # ]: 0 : || get_indexed_addr (cu, op2, &addr2) != 0)
10275 : : {
10276 : 0 : fprintf (out, " ???..\n");
10277 : 0 : fprintf (out, " ???\n");
10278 : : }
10279 : : else
10280 : : {
10281 : 0 : fprintf (out, " ");
10282 : 0 : print_dwarf_addr (dwflmod, address_size,
10283 : : addr1, addr1, out);
10284 : 0 : fprintf (out, "..\n ");
10285 : 0 : print_dwarf_addr (dwflmod, address_size,
10286 : : addr2 - 1, addr2, out);
10287 : 0 : fprintf (out, "\n");
10288 : : }
10289 : : }
10290 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
10291 : 0 : goto invalid_entry;
10292 : 0 : get_uleb128 (len, readp, nexthdr);
10293 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < len)
10294 : 0 : goto invalid_entry;
10295 : 0 : print_ops (dwflmod, dbg, 8, 8, version,
10296 : : address_size, offset_size, cu, len, readp, out);
10297 : 0 : readp += len;
10298 : 0 : break;
10299 : :
10300 : 52 : case DW_LLE_startx_length:
10301 [ - + ]: 52 : if ((uint64_t) (nexthdr - readp) < 1)
10302 : 0 : goto invalid_entry;
10303 : 52 : get_uleb128 (op1, readp, nexthdr);
10304 [ - + ]: 52 : if ((uint64_t) (nexthdr - readp) < 1)
10305 : 0 : goto invalid_entry;
10306 : 52 : get_uleb128 (op2, readp, nexthdr);
10307 : 52 : fprintf (out, " %" PRIx64 ", %" PRIx64 "\n", op1, op2);
10308 [ + - ]: 52 : if (! print_unresolved_addresses)
10309 : : {
10310 : 52 : Dwarf_Addr addr1;
10311 : 52 : Dwarf_Addr addr2;
10312 [ - + ]: 52 : if (get_indexed_addr (cu, op1, &addr1) != 0)
10313 : : {
10314 : 0 : fprintf (out, " ???..\n");
10315 : 0 : fprintf (out, " ???\n");
10316 : : }
10317 : : else
10318 : : {
10319 : 52 : addr2 = addr1 + op2;
10320 : 52 : fprintf (out, " ");
10321 : 52 : print_dwarf_addr (dwflmod, address_size, addr1, addr1, out);
10322 : 52 : fprintf (out, "..\n ");
10323 : 52 : print_dwarf_addr (dwflmod, address_size,
10324 : : addr2 - 1, addr2, out);
10325 : 52 : fprintf (out, "\n");
10326 : : }
10327 : : }
10328 [ - + ]: 52 : if ((uint64_t) (nexthdr - readp) < 1)
10329 : 0 : goto invalid_entry;
10330 : 52 : get_uleb128 (len, readp, nexthdr);
10331 [ - + ]: 52 : if ((uint64_t) (nexthdr - readp) < len)
10332 : 0 : goto invalid_entry;
10333 : 52 : print_ops (dwflmod, dbg, 8, 8, version,
10334 : : address_size, offset_size, cu, len, readp, out);
10335 : 52 : readp += len;
10336 : 52 : break;
10337 : :
10338 : 477880 : case DW_LLE_offset_pair:
10339 [ - + ]: 477880 : if ((uint64_t) (nexthdr - readp) < 1)
10340 : 0 : goto invalid_entry;
10341 : 477880 : get_uleb128 (op1, readp, nexthdr);
10342 [ - + ]: 477880 : if ((uint64_t) (nexthdr - readp) < 1)
10343 : 0 : goto invalid_entry;
10344 : 477880 : get_uleb128 (op2, readp, nexthdr);
10345 : 477880 : fprintf (out, " %" PRIx64 ", %" PRIx64 "\n", op1, op2);
10346 [ + - ]: 477880 : if (! print_unresolved_addresses)
10347 : : {
10348 : 477880 : op1 += base;
10349 : 477880 : op2 += base;
10350 : 477880 : fprintf (out, " ");
10351 : 477880 : print_dwarf_addr (dwflmod, address_size, op1, op1, out);
10352 : 477880 : fprintf (out, "..\n ");
10353 : 477880 : print_dwarf_addr (dwflmod, address_size, op2 - 1, op2, out);
10354 : 477880 : fprintf (out, "\n");
10355 : : }
10356 [ - + ]: 477880 : if ((uint64_t) (nexthdr - readp) < 1)
10357 : 0 : goto invalid_entry;
10358 : 477880 : get_uleb128 (len, readp, nexthdr);
10359 [ - + ]: 477880 : if ((uint64_t) (nexthdr - readp) < len)
10360 : 0 : goto invalid_entry;
10361 : 477880 : print_ops (dwflmod, dbg, 8, 8, version,
10362 : : address_size, offset_size, cu, len, readp, out);
10363 : 477880 : readp += len;
10364 : 477880 : break;
10365 : :
10366 : 0 : case DW_LLE_default_location:
10367 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
10368 : 0 : goto invalid_entry;
10369 : 0 : get_uleb128 (len, readp, nexthdr);
10370 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < len)
10371 : 0 : goto invalid_entry;
10372 : 0 : print_ops (dwflmod, dbg, 8, 8, version,
10373 : : address_size, offset_size, cu, len, readp, out);
10374 : 0 : readp += len;
10375 : 0 : break;
10376 : :
10377 : 45630 : case DW_LLE_base_address:
10378 [ - + ]: 45630 : if (address_size == 4)
10379 : : {
10380 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 4)
10381 : 0 : goto invalid_entry;
10382 [ # # ]: 0 : op1 = read_4ubyte_unaligned_inc (dbg, readp);
10383 : : }
10384 : : else
10385 : : {
10386 [ - + ]: 45630 : if ((uint64_t) (nexthdr - readp) < 8)
10387 : 0 : goto invalid_entry;
10388 [ - + ]: 45630 : op1 = read_8ubyte_unaligned_inc (dbg, readp);
10389 : : }
10390 : 45630 : base = op1;
10391 : 45630 : fprintf (out, " 0x%" PRIx64 "\n", base);
10392 [ + - ]: 45630 : if (! print_unresolved_addresses)
10393 : : {
10394 : 45630 : fprintf (out, " ");
10395 : 45630 : print_dwarf_addr (dwflmod, address_size, base, base, out);
10396 : 45630 : fprintf (out, "\n");
10397 : : }
10398 : : break;
10399 : :
10400 : 0 : case DW_LLE_start_end:
10401 [ # # ]: 0 : if (address_size == 4)
10402 : : {
10403 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 8)
10404 : 0 : goto invalid_entry;
10405 [ # # ]: 0 : op1 = read_4ubyte_unaligned_inc (dbg, readp);
10406 [ # # ]: 0 : op2 = read_4ubyte_unaligned_inc (dbg, readp);
10407 : : }
10408 : : else
10409 : : {
10410 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 16)
10411 : 0 : goto invalid_entry;
10412 [ # # ]: 0 : op1 = read_8ubyte_unaligned_inc (dbg, readp);
10413 [ # # ]: 0 : op2 = read_8ubyte_unaligned_inc (dbg, readp);
10414 : : }
10415 : 0 : fprintf (out, " 0x%" PRIx64 "..0x%" PRIx64 "\n", op1, op2);
10416 [ # # ]: 0 : if (! print_unresolved_addresses)
10417 : : {
10418 : 0 : fprintf (out, " ");
10419 : 0 : print_dwarf_addr (dwflmod, address_size, op1, op1, out);
10420 : 0 : fprintf (out, "..\n ");
10421 : 0 : print_dwarf_addr (dwflmod, address_size, op2 - 1, op2, out);
10422 : 0 : fprintf (out, "\n");
10423 : : }
10424 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
10425 : 0 : goto invalid_entry;
10426 : 0 : get_uleb128 (len, readp, nexthdr);
10427 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < len)
10428 : 0 : goto invalid_entry;
10429 : 0 : print_ops (dwflmod, dbg, 8, 8, version,
10430 : : address_size, offset_size, cu, len, readp, out);
10431 : 0 : readp += len;
10432 : 0 : break;
10433 : :
10434 : 22120 : case DW_LLE_start_length:
10435 [ - + ]: 22120 : if (address_size == 4)
10436 : : {
10437 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 4)
10438 : 0 : goto invalid_entry;
10439 [ # # ]: 0 : op1 = read_4ubyte_unaligned_inc (dbg, readp);
10440 : : }
10441 : : else
10442 : : {
10443 [ - + ]: 22120 : if ((uint64_t) (nexthdr - readp) < 8)
10444 : 0 : goto invalid_entry;
10445 [ - + ]: 22120 : op1 = read_8ubyte_unaligned_inc (dbg, readp);
10446 : : }
10447 [ - + ]: 22120 : if ((uint64_t) (nexthdr - readp) < 1)
10448 : 0 : goto invalid_entry;
10449 : 22120 : get_uleb128 (op2, readp, nexthdr);
10450 : 22120 : fprintf (out, " 0x%" PRIx64 ", %" PRIx64 "\n", op1, op2);
10451 [ + + ]: 22120 : if (! print_unresolved_addresses)
10452 : : {
10453 : 22118 : op2 = op1 + op2;
10454 : 22118 : fprintf (out, " ");
10455 : 22118 : print_dwarf_addr (dwflmod, address_size, op1, op1, out);
10456 : 22118 : fprintf (out, "..\n ");
10457 : 22118 : print_dwarf_addr (dwflmod, address_size, op2 - 1, op2, out);
10458 : 22118 : fprintf (out, "\n");
10459 : : }
10460 [ - + ]: 22120 : if ((uint64_t) (nexthdr - readp) < 1)
10461 : 0 : goto invalid_entry;
10462 : 22120 : get_uleb128 (len, readp, nexthdr);
10463 [ - + ]: 22120 : if ((uint64_t) (nexthdr - readp) < len)
10464 : 0 : goto invalid_entry;
10465 : 22120 : print_ops (dwflmod, dbg, 8, 8, version,
10466 : : address_size, offset_size, cu, len, readp, out);
10467 : 22120 : readp += len;
10468 : 22120 : break;
10469 : :
10470 : 0 : case DW_LLE_GNU_view_pair:
10471 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
10472 : 0 : goto invalid_entry;
10473 : 0 : get_uleb128 (op1, readp, nexthdr);
10474 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
10475 : 0 : goto invalid_entry;
10476 : 0 : get_uleb128 (op2, readp, nexthdr);
10477 : 0 : fprintf (out, " %" PRIx64 ", %" PRIx64 "\n", op1, op2);
10478 : 0 : break;
10479 : :
10480 : 0 : default:
10481 : 0 : goto invalid_entry;
10482 : : }
10483 : : }
10484 : :
10485 : 3092 : next_table:
10486 [ - + ]: 3092 : if (readp != nexthdr)
10487 : : {
10488 : 0 : size_t padding = nexthdr - readp;
10489 : 0 : fprintf (out, _(" %zu padding bytes\n\n"), padding);
10490 : 0 : readp = nexthdr;
10491 : : }
10492 : : }
10493 : : }
10494 : :
10495 : :
10496 : : static void
10497 : 52 : print_debug_loc_section (Dwfl_Module *dwflmod,
10498 : : Ebl *ebl, GElf_Ehdr *ehdr,
10499 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg,
10500 : : FILE *out)
10501 : : {
10502 : 52 : Elf_Data *data = get_debug_elf_data (dbg, ebl, IDX_debug_loc, scn);
10503 [ - + ]: 52 : if (data == NULL)
10504 : 0 : return;
10505 : :
10506 : 52 : fprintf (out, _("\
10507 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
10508 : : elf_ndxscn (scn), section_name (ebl, shdr),
10509 : 52 : (uint64_t) shdr->sh_offset);
10510 : :
10511 : 52 : sort_listptr (&known_locsptr, "loclistptr");
10512 : 52 : size_t listptr_idx = 0;
10513 : :
10514 [ + - ]: 52 : uint_fast8_t address_size = ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 4 : 8;
10515 : 52 : uint_fast8_t offset_size = 4;
10516 : :
10517 : 52 : bool first = true;
10518 : 52 : Dwarf_Addr base = 0;
10519 : 52 : unsigned char *readp = data->d_buf;
10520 : 52 : unsigned char *const endp = (unsigned char *) data->d_buf + data->d_size;
10521 : 52 : Dwarf_CU *last_cu = NULL;
10522 [ + + ]: 490 : while (readp < endp)
10523 : : {
10524 : 438 : ptrdiff_t offset = readp - (unsigned char *) data->d_buf;
10525 : 438 : Dwarf_CU *cu = last_cu;
10526 : 438 : unsigned int attr = 0;
10527 : :
10528 [ + + - + ]: 438 : if (first && skip_listptr_hole (&known_locsptr, &listptr_idx,
10529 : : &address_size, &offset_size, &base,
10530 : : &cu, offset, &readp, endp, &attr))
10531 : 0 : continue;
10532 : :
10533 [ + + ]: 438 : if (last_cu != cu)
10534 : : {
10535 : 60 : Dwarf_Die cudie;
10536 [ - + ]: 60 : if (dwarf_cu_die (cu, &cudie,
10537 : : NULL, NULL, NULL, NULL,
10538 : : NULL, NULL) == NULL)
10539 : 0 : fprintf (out, _("\n Unknown CU base: "));
10540 : : else
10541 : 60 : fprintf (out, _("\n CU [%6" PRIx64 "] base: "),
10542 : : dwarf_dieoffset (&cudie));
10543 : 60 : print_dwarf_addr (dwflmod, address_size, base, base, out);
10544 : 60 : fprintf (out, "\n");
10545 : : }
10546 : 438 : last_cu = cu;
10547 : :
10548 [ - + ]: 438 : if (attr == DW_AT_GNU_locviews)
10549 : 0 : {
10550 : 0 : Dwarf_Off next_off = next_listptr_offset (&known_locsptr,
10551 : : &listptr_idx, offset);
10552 : 0 : const unsigned char *locp = readp;
10553 : 0 : const unsigned char *locendp;
10554 [ # # ]: 0 : if (next_off == 0
10555 : 0 : || next_off > (size_t) (endp
10556 [ # # ]: 0 : - (const unsigned char *) data->d_buf))
10557 : 0 : locendp = endp;
10558 : : else
10559 : 0 : locendp = (const unsigned char *) data->d_buf + next_off;
10560 : :
10561 [ # # ]: 0 : while (locp < locendp)
10562 : : {
10563 : 0 : uint64_t v1, v2;
10564 : 0 : get_uleb128 (v1, locp, locendp);
10565 [ # # ]: 0 : if (locp >= locendp)
10566 : : {
10567 : 0 : fprintf (out, _(" [%6tx] <INVALID DATA>\n"), offset);
10568 : 0 : break;
10569 : : }
10570 : 0 : get_uleb128 (v2, locp, locendp);
10571 [ # # ]: 0 : if (first) /* First view pair in a list. */
10572 : 0 : fprintf (out, " [%6tx] ", offset);
10573 : : else
10574 : 0 : fprintf (out, " ");
10575 : 0 : fprintf (out, "view pair %" PRId64 ", %" PRId64 "\n", v1, v2);
10576 : 0 : first = false;
10577 : : }
10578 : :
10579 : 0 : first = true;
10580 : 0 : readp = (unsigned char *) locendp;
10581 : 0 : continue;
10582 : : }
10583 : :
10584 : : /* GNU DebugFission encoded addresses as addrx. */
10585 : 1194 : bool is_debugfission = ((cu != NULL
10586 [ # # ]: 0 : || split_dwarf_cu_base (dbg, &cu, &base))
10587 [ - + + - ]: 438 : && (cu->version < 5
10588 [ + + ]: 438 : && cu->unit_type == DW_UT_split_compile));
10589 : 318 : if (!is_debugfission
10590 [ - + ]: 318 : && unlikely (data->d_size - offset < (size_t) address_size * 2))
10591 : : {
10592 : 0 : invalid_data:
10593 : 0 : fprintf (out, _(" [%6tx] <INVALID DATA>\n"), offset);
10594 : 0 : break;
10595 : : }
10596 : :
10597 : 438 : Dwarf_Addr begin;
10598 : 438 : Dwarf_Addr end;
10599 : 438 : bool use_base = true;
10600 [ + + ]: 438 : if (is_debugfission)
10601 : : {
10602 : 120 : const unsigned char *locp = readp;
10603 : 120 : const unsigned char *locendp = readp + data->d_size;
10604 [ - + ]: 120 : if (locp >= locendp)
10605 : 0 : goto invalid_data;
10606 : :
10607 : 120 : Dwarf_Word idx;
10608 : 120 : unsigned char code = *locp++;
10609 [ + - - + : 120 : switch (code)
- ]
10610 : : {
10611 : 40 : case DW_LLE_GNU_end_of_list_entry:
10612 : 40 : begin = 0;
10613 : 40 : end = 0;
10614 : 40 : break;
10615 : :
10616 : 0 : case DW_LLE_GNU_base_address_selection_entry:
10617 [ # # ]: 0 : if (locp >= locendp)
10618 : 0 : goto invalid_data;
10619 : 0 : begin = (Dwarf_Addr) -1;
10620 : 0 : get_uleb128 (idx, locp, locendp);
10621 [ # # ]: 0 : if (get_indexed_addr (cu, idx, &end) != 0)
10622 : 0 : end = idx; /* ... */
10623 : : break;
10624 : :
10625 : 0 : case DW_LLE_GNU_start_end_entry:
10626 [ # # ]: 0 : if (locp >= locendp)
10627 : 0 : goto invalid_data;
10628 : 0 : get_uleb128 (idx, locp, locendp);
10629 [ # # ]: 0 : if (get_indexed_addr (cu, idx, &begin) != 0)
10630 : 0 : begin = idx; /* ... */
10631 [ # # ]: 0 : if (locp >= locendp)
10632 : 0 : goto invalid_data;
10633 : 0 : get_uleb128 (idx, locp, locendp);
10634 [ # # ]: 0 : if (get_indexed_addr (cu, idx, &end) != 0)
10635 : 0 : end = idx; /* ... */
10636 : : use_base = false;
10637 : : break;
10638 : :
10639 : 80 : case DW_LLE_GNU_start_length_entry:
10640 [ - + ]: 80 : if (locp >= locendp)
10641 : 0 : goto invalid_data;
10642 : 80 : get_uleb128 (idx, locp, locendp);
10643 [ - + ]: 80 : if (get_indexed_addr (cu, idx, &begin) != 0)
10644 : 0 : begin = idx; /* ... */
10645 [ - + ]: 80 : if (locendp - locp < 4)
10646 : 0 : goto invalid_data;
10647 [ - + ]: 80 : end = read_4ubyte_unaligned_inc (dbg, locp);
10648 : 80 : end += begin;
10649 : 80 : use_base = false;
10650 : 80 : break;
10651 : :
10652 : 0 : default:
10653 : 0 : goto invalid_data;
10654 : : }
10655 : :
10656 : 120 : readp = (unsigned char *) locp;
10657 : : }
10658 [ + - ]: 318 : else if (address_size == 8)
10659 : : {
10660 [ + + ]: 318 : begin = read_8ubyte_unaligned_inc (dbg, readp);
10661 [ + + ]: 318 : end = read_8ubyte_unaligned_inc (dbg, readp);
10662 : : }
10663 : : else
10664 : : {
10665 [ # # ]: 0 : begin = read_4ubyte_unaligned_inc (dbg, readp);
10666 [ # # ]: 0 : end = read_4ubyte_unaligned_inc (dbg, readp);
10667 [ # # ]: 0 : if (begin == (Dwarf_Addr) (uint32_t) -1)
10668 : 0 : begin = (Dwarf_Addr) -1l;
10669 : : }
10670 : :
10671 [ - + ]: 438 : if (begin == (Dwarf_Addr) -1l) /* Base address entry. */
10672 : : {
10673 [ # # ]: 0 : if (first)
10674 : 0 : fprintf (out, " [%6tx] ", offset);
10675 : : else
10676 : 0 : fprintf (out, " ");
10677 : 0 : fputs (_("base address\n"), out);
10678 : 0 : fprintf (out, " ");
10679 : 0 : print_dwarf_addr (dwflmod, address_size, end, end, out);
10680 : 0 : fprintf (out, "\n");
10681 : 0 : base = end;
10682 : 0 : first = false;
10683 : : }
10684 [ + + + + ]: 438 : else if (begin == 0 && end == 0) /* End of list entry. */
10685 : : {
10686 [ - + ]: 148 : if (first)
10687 : 0 : fprintf (out, _(" [%6tx] empty list\n"), offset);
10688 : : first = true;
10689 : : }
10690 : : else
10691 : : {
10692 : : /* We have a location expression entry. */
10693 [ + + ]: 290 : uint_fast16_t len = read_2ubyte_unaligned_inc (dbg, readp);
10694 : :
10695 [ + + ]: 290 : if (first) /* First entry in a list. */
10696 : 148 : fprintf (out, " [%6tx] ", offset);
10697 : : else
10698 : 142 : fprintf (out, " ");
10699 : :
10700 : 290 : fprintf (out, "range %" PRIx64 ", %" PRIx64 "\n", begin, end);
10701 [ + + ]: 290 : if (! print_unresolved_addresses)
10702 : : {
10703 : 230 : Dwarf_Addr dab = use_base ? base + begin : begin;
10704 : 230 : Dwarf_Addr dae = use_base ? base + end : end;
10705 : 230 : fprintf (out, " ");
10706 : 230 : print_dwarf_addr (dwflmod, address_size, dab, dab, out);
10707 : 230 : fprintf (out, "..\n ");
10708 : 230 : print_dwarf_addr (dwflmod, address_size, dae - 1, dae, out);
10709 : 230 : fprintf (out, "\n");
10710 : : }
10711 : :
10712 [ - + ]: 290 : if (endp - readp <= (ptrdiff_t) len)
10713 : : {
10714 : 0 : fputs (_(" <INVALID DATA>\n"), out);
10715 : 0 : break;
10716 : : }
10717 : :
10718 [ + - ]: 580 : print_ops (dwflmod, dbg, 11, 11,
10719 : 290 : cu != NULL ? cu->version : 3,
10720 : : address_size, offset_size, cu, len, readp, out);
10721 : :
10722 : 290 : first = false;
10723 : 290 : readp += len;
10724 : : }
10725 : : }
10726 : : }
10727 : :
10728 : : struct mac_culist
10729 : : {
10730 : : Dwarf_Die die;
10731 : : Dwarf_Off offset;
10732 : : Dwarf_Files *files;
10733 : : struct mac_culist *next;
10734 : : };
10735 : :
10736 : :
10737 : : static int
10738 : 0 : mac_compare (const void *p1, const void *p2)
10739 : : {
10740 : 0 : struct mac_culist *m1 = (struct mac_culist *) p1;
10741 : 0 : struct mac_culist *m2 = (struct mac_culist *) p2;
10742 : :
10743 [ # # ]: 0 : if (m1->offset < m2->offset)
10744 : : return -1;
10745 [ # # ]: 0 : if (m1->offset > m2->offset)
10746 : 0 : return 1;
10747 : : return 0;
10748 : : }
10749 : :
10750 : :
10751 : : static void
10752 : 0 : print_debug_macinfo_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
10753 : : Ebl *ebl,
10754 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
10755 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg,
10756 : : FILE *out)
10757 : : {
10758 : 0 : Elf_Data *data = get_debug_elf_data (dbg, ebl, IDX_debug_macinfo, scn);
10759 [ # # ]: 0 : if (data == NULL)
10760 : 0 : return;
10761 : :
10762 : 0 : fprintf (out, _("\
10763 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
10764 : : elf_ndxscn (scn), section_name (ebl, shdr),
10765 : 0 : (uint64_t) shdr->sh_offset);
10766 : 0 : fputc ('\n', out);
10767 : :
10768 : : /* There is no function in libdw to iterate over the raw content of
10769 : : the section but it is easy enough to do. */
10770 : :
10771 : : /* Get the source file information for all CUs. */
10772 : 0 : Dwarf_Off offset;
10773 : 0 : Dwarf_Off ncu = 0;
10774 : 0 : size_t hsize;
10775 : 0 : struct mac_culist *culist = NULL;
10776 : 0 : size_t nculist = 0;
10777 [ # # ]: 0 : while (dwarf_nextcu (dbg, offset = ncu, &ncu, &hsize, NULL, NULL, NULL) == 0)
10778 : : {
10779 : 0 : Dwarf_Die cudie;
10780 [ # # ]: 0 : if (dwarf_offdie (dbg, offset + hsize, &cudie) == NULL)
10781 : 0 : continue;
10782 : :
10783 : 0 : Dwarf_Attribute attr;
10784 [ # # ]: 0 : if (dwarf_attr (&cudie, DW_AT_macro_info, &attr) == NULL)
10785 : 0 : continue;
10786 : :
10787 : 0 : Dwarf_Word macoff;
10788 [ # # ]: 0 : if (dwarf_formudata (&attr, &macoff) != 0)
10789 : 0 : continue;
10790 : :
10791 : 0 : struct mac_culist *newp = (struct mac_culist *) alloca (sizeof (*newp));
10792 : 0 : newp->die = cudie;
10793 : 0 : newp->offset = macoff;
10794 : 0 : newp->files = NULL;
10795 : 0 : newp->next = culist;
10796 : 0 : culist = newp;
10797 : 0 : ++nculist;
10798 : : }
10799 : :
10800 : : /* Convert the list into an array for easier consumption. */
10801 : 0 : struct mac_culist *cus = (struct mac_culist *) alloca ((nculist + 1)
10802 : : * sizeof (*cus));
10803 : : /* Add sentinel. */
10804 : 0 : cus[nculist].offset = data->d_size;
10805 : 0 : cus[nculist].files = (Dwarf_Files *) -1l;
10806 [ # # ]: 0 : if (nculist > 0)
10807 : : {
10808 [ # # ]: 0 : for (size_t cnt = nculist - 1; culist != NULL; --cnt)
10809 : : {
10810 [ # # ]: 0 : assert (cnt < nculist);
10811 : 0 : cus[cnt] = *culist;
10812 : 0 : culist = culist->next;
10813 : : }
10814 : :
10815 : : /* Sort the array according to the offset in the .debug_macinfo
10816 : : section. Note we keep the sentinel at the end. */
10817 : 0 : qsort (cus, nculist, sizeof (*cus), mac_compare);
10818 : : }
10819 : :
10820 : 0 : const unsigned char *readp = (const unsigned char *) data->d_buf;
10821 : 0 : const unsigned char *readendp = readp + data->d_size;
10822 : 0 : int level = 1;
10823 : :
10824 [ # # ]: 0 : while (readp < readendp)
10825 : : {
10826 : 0 : unsigned int opcode = *readp++;
10827 : 0 : unsigned int u128;
10828 : 0 : unsigned int u128_2;
10829 : 0 : const unsigned char *endp;
10830 : :
10831 [ # # # # ]: 0 : switch (opcode)
10832 : : {
10833 : 0 : case DW_MACINFO_define:
10834 : : case DW_MACINFO_undef:
10835 : : case DW_MACINFO_vendor_ext:
10836 : : /* For the first two opcodes the parameters are
10837 : : line, string
10838 : : For the latter
10839 : : number, string.
10840 : : We can treat these cases together. */
10841 : 0 : get_uleb128 (u128, readp, readendp);
10842 : :
10843 : 0 : endp = memchr (readp, '\0', readendp - readp);
10844 [ # # ]: 0 : if (unlikely (endp == NULL))
10845 : : {
10846 : 0 : fprintf (out, _("\
10847 : : %*s*** non-terminated string at end of section"),
10848 : : level, "");
10849 : 0 : return;
10850 : : }
10851 : :
10852 [ # # ]: 0 : if (opcode == DW_MACINFO_define)
10853 : 0 : fprintf (out, "%*s#define %s, line %u\n",
10854 : : level, "", (char *) readp, u128);
10855 [ # # ]: 0 : else if (opcode == DW_MACINFO_undef)
10856 : 0 : fprintf (out, "%*s#undef %s, line %u\n",
10857 : : level, "", (char *) readp, u128);
10858 : : else
10859 : 0 : fprintf (out, " #vendor-ext %s, number %u\n", (char *) readp, u128);
10860 : :
10861 : 0 : readp = endp + 1;
10862 : 0 : break;
10863 : :
10864 : 0 : case DW_MACINFO_start_file:
10865 : : /* The two parameters are line and file index, in this order. */
10866 : 0 : get_uleb128 (u128, readp, readendp);
10867 [ # # ]: 0 : if (readendp - readp < 1)
10868 : : {
10869 : 0 : fprintf (out, _("\
10870 : : %*s*** missing DW_MACINFO_start_file argument at end of section"),
10871 : : level, "");
10872 : 0 : return;
10873 : : }
10874 : 0 : get_uleb128 (u128_2, readp, readendp);
10875 : :
10876 : : /* Find the CU DIE for this file. */
10877 : 0 : size_t macoff = readp - (const unsigned char *) data->d_buf;
10878 : 0 : const char *fname = "???";
10879 [ # # # # ]: 0 : if (macoff >= cus[0].offset && cus[0].offset != data->d_size)
10880 : : {
10881 [ # # # # ]: 0 : while (macoff >= cus[1].offset && cus[1].offset != data->d_size)
10882 : 0 : ++cus;
10883 : :
10884 [ # # ]: 0 : if (cus[0].files == NULL
10885 [ # # ]: 0 : && dwarf_getsrcfiles (&cus[0].die, &cus[0].files, NULL) != 0)
10886 : 0 : cus[0].files = (Dwarf_Files *) -1l;
10887 : :
10888 [ # # ]: 0 : if (cus[0].files != (Dwarf_Files *) -1l)
10889 : 0 : fname = (dwarf_filesrc (cus[0].files, u128_2, NULL, NULL)
10890 [ # # ]: 0 : ?: "???");
10891 : : }
10892 : :
10893 : 0 : fprintf (out, "%*sstart_file %u, [%u] %s\n",
10894 : : level, "", u128, u128_2, fname);
10895 : 0 : ++level;
10896 : 0 : break;
10897 : :
10898 : 0 : case DW_MACINFO_end_file:
10899 : 0 : --level;
10900 : 0 : fprintf (out, "%*send_file\n", level, "");
10901 : : /* Nothing more to do. */
10902 : 0 : break;
10903 : :
10904 : 0 : default:
10905 : : // XXX gcc seems to generate files with a trailing zero.
10906 [ # # # # ]: 0 : if (unlikely (opcode != 0 || readp != readendp))
10907 : 0 : fprintf (out, "%*s*** invalid opcode %u\n", level, "", opcode);
10908 : : break;
10909 : : }
10910 : : }
10911 : : }
10912 : :
10913 : :
10914 : : static void
10915 : 8 : print_debug_macro_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
10916 : : Ebl *ebl,
10917 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
10918 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg,
10919 : : FILE *out)
10920 : : {
10921 : 8 : Elf_Data *data = get_debug_elf_data (dbg, ebl, IDX_debug_macro, scn);
10922 [ + - ]: 8 : if (data == NULL)
10923 : 0 : return;
10924 : :
10925 : 8 : fprintf (out, _("\
10926 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
10927 : : elf_ndxscn (scn), section_name (ebl, shdr),
10928 : 8 : (uint64_t) shdr->sh_offset);
10929 : 8 : fputc ('\n', out);
10930 : :
10931 : : /* Get the source file information for all CUs. Uses same
10932 : : datastructure as macinfo. But uses offset field to directly
10933 : : match .debug_line offset. And just stored in a list. */
10934 : 8 : Dwarf_Off offset;
10935 : 8 : Dwarf_Off ncu = 0;
10936 : 8 : size_t hsize;
10937 : 8 : struct mac_culist *culist = NULL;
10938 [ + + ]: 20 : while (dwarf_nextcu (dbg, offset = ncu, &ncu, &hsize, NULL, NULL, NULL) == 0)
10939 : : {
10940 : 12 : Dwarf_Die cudie;
10941 [ - + ]: 12 : if (dwarf_offdie (dbg, offset + hsize, &cudie) == NULL)
10942 : 0 : continue;
10943 : :
10944 : 12 : Dwarf_Attribute attr;
10945 [ - + ]: 12 : if (dwarf_attr (&cudie, DW_AT_stmt_list, &attr) == NULL)
10946 : 0 : continue;
10947 : :
10948 : 12 : Dwarf_Word lineoff;
10949 [ - + ]: 12 : if (dwarf_formudata (&attr, &lineoff) != 0)
10950 : 0 : continue;
10951 : :
10952 : 12 : struct mac_culist *newp = (struct mac_culist *) alloca (sizeof (*newp));
10953 : 12 : newp->die = cudie;
10954 : 12 : newp->offset = lineoff;
10955 : 12 : newp->files = NULL;
10956 : 12 : newp->next = culist;
10957 : 12 : culist = newp;
10958 : : }
10959 : :
10960 : 8 : const unsigned char *readp = (const unsigned char *) data->d_buf;
10961 : 8 : const unsigned char *readendp = readp + data->d_size;
10962 : :
10963 [ + + ]: 28 : while (readp < readendp)
10964 : : {
10965 : 40 : fprintf (out, _(" Offset: 0x%" PRIx64 "\n"),
10966 : 20 : (uint64_t) (readp - (const unsigned char *) data->d_buf));
10967 : :
10968 : : // Header, 2 byte version, 1 byte flag, optional .debug_line offset,
10969 : : // optional vendor extension macro entry table.
10970 [ - + ]: 20 : if (readp + 2 > readendp)
10971 : : {
10972 : 0 : invalid_data:
10973 : 0 : error (0, 0, _("invalid data"));
10974 : 0 : return;
10975 : : }
10976 [ - + ]: 20 : const uint16_t vers = read_2ubyte_unaligned_inc (dbg, readp);
10977 : 20 : fprintf (out, _(" Version: %" PRIu16 "\n"), vers);
10978 : :
10979 : : // Version 4 is the GNU extension for DWARF4. DWARF5 will use version
10980 : : // 5 when it gets standardized.
10981 [ - + ]: 20 : if (vers != 4 && vers != 5)
10982 : : {
10983 : 0 : fprintf (out, _(" unknown version, cannot parse section\n"));
10984 : 0 : return;
10985 : : }
10986 : :
10987 [ - + ]: 20 : if (readp + 1 > readendp)
10988 : 0 : goto invalid_data;
10989 : 20 : const unsigned char flag = *readp++;
10990 : 20 : fprintf (out, _(" Flag: 0x%" PRIx8), flag);
10991 [ + + ]: 20 : if (flag != 0)
10992 : : {
10993 : 12 : fprintf (out, " (");
10994 [ - + ]: 12 : if ((flag & 0x01) != 0)
10995 : : {
10996 : 0 : fprintf (out, "offset_size");
10997 [ # # ]: 0 : if ((flag & 0xFE) != 0)
10998 : 0 : fprintf (out, ", ");
10999 : : }
11000 [ + - ]: 12 : if ((flag & 0x02) != 0)
11001 : : {
11002 : 12 : fprintf (out, "debug_line_offset");
11003 [ - + ]: 12 : if ((flag & 0xFC) != 0)
11004 : 0 : fprintf (out, ", ");
11005 : : }
11006 [ # # ]: 0 : if ((flag & 0x04) != 0)
11007 : : {
11008 : 0 : fprintf (out, "operands_table");
11009 [ # # ]: 0 : if ((flag & 0xF8) != 0)
11010 : 0 : fprintf (out, ", ");
11011 : : }
11012 [ - + ]: 12 : if ((flag & 0xF8) != 0)
11013 : 0 : fprintf (out, "unknown");
11014 : 12 : fprintf (out, ")");
11015 : : }
11016 : 20 : fprintf (out, "\n");
11017 : :
11018 [ + - ]: 20 : unsigned int offset_len = (flag & 0x01) ? 8 : 4;
11019 : 20 : fprintf (out, _(" Offset length: %" PRIu8 "\n"), offset_len);
11020 : 20 : Dwarf_Off line_offset = -1;
11021 [ + + ]: 20 : if (flag & 0x02)
11022 : : {
11023 [ - + ]: 12 : if (offset_len == 8)
11024 [ # # ]: 0 : line_offset = read_8ubyte_unaligned_inc (dbg, readp);
11025 : : else
11026 [ - + ]: 12 : line_offset = read_4ubyte_unaligned_inc (dbg, readp);
11027 : 12 : fprintf (out, _(" .debug_line offset: 0x%" PRIx64 "\n"),
11028 : : line_offset);
11029 : : }
11030 : :
11031 : 12 : struct mac_culist *cu = NULL;
11032 [ + - ]: 12 : if (line_offset != (Dwarf_Off) -1)
11033 : : {
11034 : : cu = culist;
11035 [ + - + + ]: 16 : while (cu != NULL && line_offset != cu->offset)
11036 : 4 : cu = cu->next;
11037 : : }
11038 : :
11039 [ - + ]: 20 : Dwarf_Off str_offsets_base = str_offsets_base_off (dbg, (cu != NULL
11040 : : ? cu->die.cu
11041 : : : NULL));
11042 : :
11043 : 20 : const unsigned char *vendor[DW_MACRO_hi_user - DW_MACRO_lo_user + 1];
11044 [ - + ]: 20 : memset (vendor, 0, sizeof vendor);
11045 [ - + ]: 20 : if (flag & 0x04)
11046 : : {
11047 : : // 1 byte length, for each item, 1 byte opcode, uleb128 number
11048 : : // of arguments, for each argument 1 byte form code.
11049 [ # # ]: 0 : if (readp + 1 > readendp)
11050 : 0 : goto invalid_data;
11051 : 0 : unsigned int tlen = *readp++;
11052 : 0 : fprintf (out, _(" extension opcode table, %" PRIu8 " items:\n"),
11053 : : tlen);
11054 [ # # ]: 0 : for (unsigned int i = 0; i < tlen; i++)
11055 : : {
11056 [ # # ]: 0 : if (readp + 1 > readendp)
11057 : 0 : goto invalid_data;
11058 : 0 : unsigned int opcode = *readp++;
11059 : 0 : fprintf (out, _(" [%" PRIx8 "]"), opcode);
11060 : 0 : if (opcode < DW_MACRO_lo_user
11061 [ # # ]: 0 : || opcode > DW_MACRO_hi_user)
11062 : 0 : goto invalid_data;
11063 : : // Record the start of description for this vendor opcode.
11064 : : // uleb128 nr args, 1 byte per arg form.
11065 : 0 : vendor[opcode - DW_MACRO_lo_user] = readp;
11066 [ # # ]: 0 : if (readp + 1 > readendp)
11067 : 0 : goto invalid_data;
11068 : 0 : unsigned int args = *readp++;
11069 [ # # ]: 0 : if (args > 0)
11070 : : {
11071 : 0 : fprintf (out, _(" %" PRIu8 " arguments:"), args);
11072 : 0 : while (args > 0)
11073 : : {
11074 [ # # ]: 0 : if (readp + 1 > readendp)
11075 : 0 : goto invalid_data;
11076 : 0 : unsigned int form = *readp++;
11077 : 0 : fprintf (out, " %s", dwarf_form_name (form));
11078 [ # # ]: 0 : if (! libdw_valid_user_form (form))
11079 : 0 : goto invalid_data;
11080 : 0 : args--;
11081 [ # # ]: 0 : if (args > 0)
11082 : 0 : fputc (',', out);
11083 : : }
11084 : : }
11085 : : else
11086 : 0 : fprintf (out, _(" no arguments."));
11087 : 0 : fputc ('\n', out);
11088 : : }
11089 : : }
11090 : 20 : fputc ('\n', out);
11091 : :
11092 : 20 : int level = 1;
11093 [ - + ]: 20 : if (readp + 1 > readendp)
11094 : 0 : goto invalid_data;
11095 : 20 : unsigned int opcode = *readp++;
11096 [ + + ]: 3046 : while (opcode != 0)
11097 : : {
11098 : 3026 : unsigned int u128;
11099 : 3026 : unsigned int u128_2;
11100 : 3026 : const unsigned char *endp;
11101 : 3026 : uint64_t off;
11102 : :
11103 [ + + + - : 3026 : switch (opcode)
+ + + - -
- + + - ]
11104 : : {
11105 : 20 : case DW_MACRO_start_file:
11106 : 20 : get_uleb128 (u128, readp, readendp);
11107 [ - + ]: 20 : if (readp >= readendp)
11108 : 0 : goto invalid_data;
11109 : 20 : get_uleb128 (u128_2, readp, readendp);
11110 : :
11111 : : /* Find the CU DIE that matches this line offset. */
11112 : 20 : const char *fname = "???";
11113 [ - + ]: 20 : if (cu != NULL)
11114 : : {
11115 [ + + ]: 20 : if (cu->files == NULL
11116 [ - + ]: 12 : && dwarf_getsrcfiles (&cu->die, &cu->files,
11117 : : NULL) != 0)
11118 : 0 : cu->files = (Dwarf_Files *) -1l;
11119 : :
11120 [ - + ]: 20 : if (cu->files != (Dwarf_Files *) -1l)
11121 : 20 : fname = (dwarf_filesrc (cu->files, u128_2,
11122 [ - + ]: 20 : NULL, NULL) ?: "???");
11123 : : }
11124 : 20 : fprintf (out, "%*sstart_file %u, [%u] %s\n",
11125 : : level, "", u128, u128_2, fname);
11126 : 20 : ++level;
11127 : 20 : break;
11128 : :
11129 : 20 : case DW_MACRO_end_file:
11130 : 20 : --level;
11131 : 20 : fprintf (out, "%*send_file\n", level, "");
11132 : 20 : break;
11133 : :
11134 : 2 : case DW_MACRO_define:
11135 : 2 : get_uleb128 (u128, readp, readendp);
11136 : 2 : endp = memchr (readp, '\0', readendp - readp);
11137 [ - + ]: 2 : if (endp == NULL)
11138 : 0 : goto invalid_data;
11139 : 2 : fprintf (out, "%*s#define %s, line %u\n",
11140 : : level, "", readp, u128);
11141 : 2 : readp = endp + 1;
11142 : 2 : break;
11143 : :
11144 : 0 : case DW_MACRO_undef:
11145 : 0 : get_uleb128 (u128, readp, readendp);
11146 : 0 : endp = memchr (readp, '\0', readendp - readp);
11147 [ # # ]: 0 : if (endp == NULL)
11148 : 0 : goto invalid_data;
11149 : 0 : fprintf (out, "%*s#undef %s, line %u\n",
11150 : : level, "", readp, u128);
11151 : 0 : readp = endp + 1;
11152 : 0 : break;
11153 : :
11154 : 1414 : case DW_MACRO_define_strp:
11155 : 1414 : get_uleb128 (u128, readp, readendp);
11156 [ - + ]: 1414 : if (readp + offset_len > readendp)
11157 : 0 : goto invalid_data;
11158 [ - + ]: 1414 : if (offset_len == 8)
11159 [ # # ]: 0 : off = read_8ubyte_unaligned_inc (dbg, readp);
11160 : : else
11161 [ - + ]: 1414 : off = read_4ubyte_unaligned_inc (dbg, readp);
11162 : 1414 : fprintf (out, "%*s#define %s, line %u (indirect)\n",
11163 : : level, "", dwarf_getstring (dbg, off, NULL), u128);
11164 : 1414 : break;
11165 : :
11166 : 2 : case DW_MACRO_undef_strp:
11167 : 2 : get_uleb128 (u128, readp, readendp);
11168 [ - + ]: 2 : if (readp + offset_len > readendp)
11169 : 0 : goto invalid_data;
11170 [ - + ]: 2 : if (offset_len == 8)
11171 [ # # ]: 0 : off = read_8ubyte_unaligned_inc (dbg, readp);
11172 : : else
11173 [ - + ]: 2 : off = read_4ubyte_unaligned_inc (dbg, readp);
11174 : 2 : fprintf (out, "%*s#undef %s, line %u (indirect)\n",
11175 : : level, "", dwarf_getstring (dbg, off, NULL), u128);
11176 : 2 : break;
11177 : :
11178 : 12 : case DW_MACRO_import:
11179 [ - + ]: 12 : if (readp + offset_len > readendp)
11180 : 0 : goto invalid_data;
11181 [ - + ]: 12 : if (offset_len == 8)
11182 [ # # ]: 0 : off = read_8ubyte_unaligned_inc (dbg, readp);
11183 : : else
11184 [ - + ]: 12 : off = read_4ubyte_unaligned_inc (dbg, readp);
11185 : 12 : fprintf (out, "%*s#include offset 0x%" PRIx64 "\n",
11186 : : level, "", off);
11187 : 12 : break;
11188 : :
11189 : 0 : case DW_MACRO_define_sup:
11190 : 0 : get_uleb128 (u128, readp, readendp);
11191 : 0 : fprintf (out, "%*s#define ", level, "");
11192 : 0 : readp = print_form_data (dbg, DW_FORM_strp_sup,
11193 : : readp, readendp, offset_len,
11194 : : str_offsets_base, out);
11195 : 0 : fprintf (out, ", line %u (sup)\n", u128);
11196 : 0 : break;
11197 : :
11198 : 0 : case DW_MACRO_undef_sup:
11199 : 0 : get_uleb128 (u128, readp, readendp);
11200 : 0 : fprintf (out, "%*s#undef ", level, "");
11201 : 0 : readp = print_form_data (dbg, DW_FORM_strp_sup,
11202 : : readp, readendp, offset_len,
11203 : : str_offsets_base, out);
11204 : 0 : fprintf (out, ", line %u (sup)\n", u128);
11205 : 0 : break;
11206 : :
11207 : 0 : case DW_MACRO_import_sup:
11208 [ # # ]: 0 : if (readp + offset_len > readendp)
11209 : 0 : goto invalid_data;
11210 [ # # ]: 0 : if (offset_len == 8)
11211 [ # # ]: 0 : off = read_8ubyte_unaligned_inc (dbg, readp);
11212 : : else
11213 [ # # ]: 0 : off = read_4ubyte_unaligned_inc (dbg, readp);
11214 : : // XXX Needs support for reading from supplementary object file.
11215 : 0 : fprintf (out, "%*s#include offset 0x%" PRIx64 " (sup)\n",
11216 : : level, "", off);
11217 : 0 : break;
11218 : :
11219 : 1552 : case DW_MACRO_define_strx:
11220 : 1552 : get_uleb128 (u128, readp, readendp);
11221 : 1552 : fprintf (out, "%*s#define ", level, "");
11222 : 1552 : readp = print_form_data (dbg, DW_FORM_strx,
11223 : : readp, readendp, offset_len,
11224 : : str_offsets_base, out);
11225 : 1552 : fprintf (out, ", line %u (strx)\n", u128);
11226 : 1552 : break;
11227 : :
11228 : 4 : case DW_MACRO_undef_strx:
11229 : 4 : get_uleb128 (u128, readp, readendp);
11230 : 4 : fprintf (out, "%*s#undef ", level, "");
11231 : 4 : readp = print_form_data (dbg, DW_FORM_strx,
11232 : : readp, readendp, offset_len,
11233 : : str_offsets_base, out);
11234 : 4 : fprintf (out, ", line %u (strx)\n", u128);
11235 : 4 : break;
11236 : :
11237 : : default:
11238 : 0 : fprintf (out, "%*svendor opcode 0x%" PRIx8, level, "", opcode);
11239 [ # # ]: 0 : if (opcode < DW_MACRO_lo_user
11240 : : || opcode > DW_MACRO_lo_user
11241 [ # # ]: 0 : || vendor[opcode - DW_MACRO_lo_user] == NULL)
11242 : 0 : goto invalid_data;
11243 : :
11244 : 0 : const unsigned char *op_desc;
11245 : 0 : op_desc = vendor[opcode - DW_MACRO_lo_user];
11246 : :
11247 : : // Just skip the arguments, we cannot really interpret them,
11248 : : // but print as much as we can.
11249 : 0 : unsigned int args = *op_desc++;
11250 [ # # # # ]: 0 : while (args > 0 && readp < readendp)
11251 : : {
11252 : 0 : unsigned int form = *op_desc++;
11253 : 0 : readp = print_form_data (dbg, form, readp, readendp,
11254 : : offset_len, str_offsets_base, out);
11255 : 0 : args--;
11256 [ # # ]: 0 : if (args > 0)
11257 : 0 : fprintf (out, ", ");
11258 : : }
11259 : 0 : fputc ('\n', out);
11260 : : }
11261 : :
11262 [ - + ]: 3026 : if (readp + 1 > readendp)
11263 : 0 : goto invalid_data;
11264 : 3026 : opcode = *readp++;
11265 [ + + ]: 3026 : if (opcode == 0)
11266 : 20 : fputc ('\n', out);
11267 : : }
11268 : : }
11269 : : }
11270 : :
11271 : :
11272 : : typedef struct {
11273 : : int n;
11274 : : FILE *out;
11275 : : } pubnames_arg;
11276 : :
11277 : : /* Callback for printing global names. */
11278 : : static int
11279 : 68 : print_pubnames (Dwarf *dbg __attribute__ ((unused)), Dwarf_Global *global,
11280 : : void *arg)
11281 : : {
11282 : 68 : pubnames_arg *p = (pubnames_arg *) arg;
11283 : :
11284 : 136 : fprintf (p->out, _(" [%5d] DIE offset: %6" PRId64
11285 : : ", CU DIE offset: %6" PRId64 ", name: %s\n"),
11286 : 68 : (p->n)++, global->die_offset, global->cu_offset, global->name);
11287 : :
11288 : 68 : return 0;
11289 : : }
11290 : :
11291 : :
11292 : : /* Print the known exported symbols in the DWARF section '.debug_pubnames'. */
11293 : : static void
11294 : 16 : print_debug_pubnames_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
11295 : : Ebl *ebl,
11296 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
11297 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg,
11298 : : FILE *out)
11299 : : {
11300 : : /* Check section actually exists. */
11301 [ - + ]: 16 : if (get_debug_elf_data (dbg, ebl, IDX_debug_pubnames, scn) == NULL)
11302 : 0 : return;
11303 : :
11304 : 16 : fprintf (out, _("\nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
11305 : : elf_ndxscn (scn), section_name (ebl, shdr),
11306 : 16 : (uint64_t) shdr->sh_offset);
11307 : :
11308 : 16 : pubnames_arg arg = { 0, out };
11309 : 16 : (void) dwarf_getpubnames (dbg, print_pubnames, &arg, 0);
11310 : : }
11311 : :
11312 : : /* Print the content of the DWARF string section '.debug_str'
11313 : : or 'debug_line_str'. */
11314 : : static void
11315 : 134 : print_debug_str_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
11316 : : Ebl *ebl,
11317 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
11318 : : Elf_Scn *scn, GElf_Shdr *shdr,
11319 : : Dwarf *dbg __attribute__ ((unused)),
11320 : : FILE *out)
11321 : : {
11322 : 134 : const char *name = section_name (ebl, shdr);
11323 [ + + ]: 134 : int idx = ((name != NULL && strstr (name, "debug_line_str") != NULL)
11324 : : ? IDX_debug_line_str : IDX_debug_str);
11325 : 134 : Elf_Data *data = get_debug_elf_data (dbg, ebl, idx, scn);
11326 [ + - ]: 134 : if (data == NULL)
11327 : : return;
11328 : :
11329 : 134 : const size_t sh_size = data->d_size;
11330 : :
11331 : : /* Compute floor(log16(shdr->sh_size)). */
11332 : 134 : GElf_Addr tmp = sh_size;
11333 : 134 : int digits = 1;
11334 [ + + ]: 466 : while (tmp >= 16)
11335 : : {
11336 : 332 : ++digits;
11337 : 332 : tmp >>= 4;
11338 : : }
11339 : 134 : digits = MAX (4, digits);
11340 : :
11341 : 134 : fprintf (out, _("\nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"
11342 : : " %*s String\n"),
11343 : : elf_ndxscn (scn),
11344 : 134 : section_name (ebl, shdr), (uint64_t) shdr->sh_offset,
11345 : : /* TRANS: the debugstr| prefix makes the string unique. */
11346 [ + - ]: 134 : digits + 2, sgettext ("debugstr|Offset"));
11347 : :
11348 : 134 : Dwarf_Off offset = 0;
11349 [ + + ]: 162514 : while (offset < sh_size)
11350 : : {
11351 : 162380 : size_t len;
11352 : 162380 : const char *str = (const char *) data->d_buf + offset;
11353 : 162380 : const char *endp = memchr (str, '\0', sh_size - offset);
11354 [ - + ]: 162380 : if (unlikely (endp == NULL))
11355 : : {
11356 : 0 : fprintf (out, _(" *** error, missing string terminator\n"));
11357 : 0 : break;
11358 : : }
11359 : :
11360 : 162380 : fprintf (out, " [%*" PRIx64 "] \"%s\"\n",
11361 : : digits, (uint64_t) offset, str);
11362 : 162380 : len = endp - str;
11363 : 162380 : offset += len + 1;
11364 : : }
11365 : : }
11366 : :
11367 : : static void
11368 : 12 : print_debug_str_offsets_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
11369 : : Ebl *ebl,
11370 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
11371 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg,
11372 : : FILE *out)
11373 : : {
11374 : 12 : Elf_Data *data = get_debug_elf_data (dbg, ebl, IDX_debug_str_offsets, scn);
11375 [ + - ]: 12 : if (data == NULL)
11376 : : return;
11377 : :
11378 : 12 : fprintf (out, _("\
11379 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
11380 : : elf_ndxscn (scn), section_name (ebl, shdr),
11381 : 12 : (uint64_t) shdr->sh_offset);
11382 : :
11383 [ + - ]: 12 : if (shdr->sh_size == 0)
11384 : : return;
11385 : :
11386 : 12 : size_t idx = 0;
11387 : 12 : sort_listptr (&known_stroffbases, "str_offsets");
11388 : :
11389 : 12 : const unsigned char *start = (const unsigned char *) data->d_buf;
11390 : 12 : const unsigned char *readp = start;
11391 : 12 : const unsigned char *readendp = ((const unsigned char *) data->d_buf
11392 : 12 : + data->d_size);
11393 : :
11394 [ + + ]: 28 : while (readp < readendp)
11395 : : {
11396 : : /* Most string offset tables will have a header. For split
11397 : : dwarf unit GNU DebugFission didn't add one. But they were
11398 : : also only defined for split units (main or skeleton units
11399 : : didn't have indirect strings). So if we don't have a
11400 : : DW_AT_str_offsets_base at all and this is offset zero, then
11401 : : just start printing offsets immediately, if this is a .dwo
11402 : : section. */
11403 : 16 : Dwarf_Off off = (Dwarf_Off) (readp
11404 : 16 : - (const unsigned char *) data->d_buf);
11405 : :
11406 : 16 : fprintf (out, "Table at offset %" PRIx64 " ", off);
11407 : :
11408 : : /* Find the first CU that could plausibly be associated with
11409 : : this string offsets index. Skip CUs that point
11410 : : str_offsets_base before this table. */
11411 [ + + ]: 16 : struct listptr *listptr = get_listptr (&known_stroffbases, idx);
11412 [ + + ]: 12 : while (listptr != NULL && listptr->offset < off)
11413 [ + - + + ]: 24 : listptr = get_listptr (&known_stroffbases, ++idx);
11414 : :
11415 : 16 : const unsigned char *next_unitp = readendp;
11416 : 16 : uint8_t offset_size;
11417 : 16 : bool has_header;
11418 [ + + ]: 16 : if (listptr == NULL)
11419 : : {
11420 : : /* This can happen for .dwo files. There is only an header
11421 : : in the case this is a version 5 split DWARF file. */
11422 : 8 : Dwarf_CU *cu;
11423 : 8 : uint8_t unit_type;
11424 [ - + ]: 8 : if (dwarf_get_units (dbg, NULL, &cu, NULL, &unit_type,
11425 : : NULL, NULL) != 0)
11426 : : {
11427 : 0 : error (0, 0, "Warning: Cannot find any DWARF unit.");
11428 : : /* Just guess some values. */
11429 : 0 : has_header = false;
11430 : 0 : offset_size = 4;
11431 : : }
11432 [ + - ]: 8 : else if (off == 0
11433 : 8 : && (unit_type == DW_UT_split_type
11434 [ + - ]: 8 : || unit_type == DW_UT_split_compile))
11435 : : {
11436 : 8 : has_header = cu->version > 4;
11437 : 8 : offset_size = cu->offset_size;
11438 : : }
11439 : : else
11440 : : {
11441 : 0 : error (0, 0,
11442 : : "Warning: No CU references .debug_str_offsets after %"
11443 : : PRIx64, off);
11444 : 0 : has_header = cu->version > 4;
11445 : 0 : offset_size = cu->offset_size;
11446 : : }
11447 : 8 : fprintf (out, "\n");
11448 : : }
11449 : : else
11450 : : {
11451 : : /* This must be DWARF5, since GNU DebugFission didn't define
11452 : : DW_AT_str_offsets_base. */
11453 : 8 : has_header = true;
11454 : :
11455 : 8 : Dwarf_Die cudie;
11456 [ - + ]: 8 : if (dwarf_cu_die (listptr->cu, &cudie,
11457 : : NULL, NULL, NULL, NULL,
11458 : : NULL, NULL) == NULL)
11459 : 0 : fprintf (out, "Unknown CU (%s):\n", dwarf_errmsg (-1));
11460 : : else
11461 : 8 : fprintf (out, "for CU [%6" PRIx64 "]:\n", dwarf_dieoffset (&cudie));
11462 : : }
11463 : :
11464 [ + + ]: 16 : if (has_header)
11465 : : {
11466 : 12 : uint64_t unit_length;
11467 : 12 : uint16_t version;
11468 : 12 : uint16_t padding;
11469 : :
11470 [ - + ]: 12 : unit_length = read_4ubyte_unaligned_inc (dbg, readp);
11471 [ - + ]: 12 : if (unlikely (unit_length == 0xffffffff))
11472 : : {
11473 [ # # ]: 0 : if (unlikely (readp > readendp - 8))
11474 : : {
11475 : 0 : invalid_data:
11476 : 0 : error (0, 0, "Invalid data");
11477 : 0 : return;
11478 : : }
11479 [ # # ]: 0 : unit_length = read_8ubyte_unaligned_inc (dbg, readp);
11480 : 0 : offset_size = 8;
11481 : : }
11482 : : else
11483 : : offset_size = 4;
11484 : :
11485 : 12 : fprintf (out, "\n");
11486 : 12 : fprintf (out, _(" Length: %8" PRIu64 "\n"),
11487 : : unit_length);
11488 : 12 : fprintf (out, _(" Offset size: %8" PRIu8 "\n"),
11489 : : offset_size);
11490 : :
11491 : : /* We need at least 2-bytes (version) + 2-bytes (padding) =
11492 : : 4 bytes to complete the header. And this unit cannot go
11493 : : beyond the section data. */
11494 [ + - ]: 12 : if (readp > readendp - 4
11495 [ + - ]: 12 : || unit_length < 4
11496 [ - + ]: 12 : || unit_length > (uint64_t) (readendp - readp))
11497 : 0 : goto invalid_data;
11498 : :
11499 : 12 : next_unitp = readp + unit_length;
11500 : :
11501 [ - + ]: 12 : version = read_2ubyte_unaligned_inc (dbg, readp);
11502 : 12 : fprintf (out, _(" DWARF version: %8" PRIu16 "\n"), version);
11503 : :
11504 [ - + ]: 12 : if (version != 5)
11505 : : {
11506 : 0 : error (0, 0, _("Unknown version"));
11507 : 0 : goto next_unit;
11508 : : }
11509 : :
11510 [ - + ]: 12 : padding = read_2ubyte_unaligned_inc (dbg, readp);
11511 : 12 : fprintf (out, _(" Padding: %8" PRIx16 "\n"), padding);
11512 : :
11513 [ + + ]: 12 : if (listptr != NULL
11514 [ - + ]: 8 : && listptr->offset != (Dwarf_Off) (readp - start))
11515 : : {
11516 : 0 : error (0, 0, "String offsets index doesn't start after header");
11517 : 0 : goto next_unit;
11518 : : }
11519 : :
11520 : 12 : fprintf (out, "\n");
11521 : : }
11522 : :
11523 : 16 : int digits = 1;
11524 : 16 : size_t offsets = (next_unitp - readp) / offset_size;
11525 [ + + ]: 24 : while (offsets >= 10)
11526 : : {
11527 : 8 : ++digits;
11528 : 8 : offsets /= 10;
11529 : : }
11530 : :
11531 : 16 : unsigned int uidx = 0;
11532 : 16 : size_t index_offset = readp - (const unsigned char *) data->d_buf;
11533 : 16 : fprintf (out, " Offsets start at 0x%zx:\n", index_offset);
11534 [ + + ]: 204 : while (readp <= next_unitp - offset_size)
11535 : : {
11536 : 188 : Dwarf_Word offset;
11537 [ + - ]: 188 : if (offset_size == 4)
11538 [ - + ]: 188 : offset = read_4ubyte_unaligned_inc (dbg, readp);
11539 : : else
11540 [ # # ]: 0 : offset = read_8ubyte_unaligned_inc (dbg, readp);
11541 : 188 : const char *str = dwarf_getstring (dbg, offset, NULL);
11542 [ - + ]: 188 : fprintf (out, " [%*u] [%*" PRIx64 "] \"%s\"\n",
11543 : 188 : digits, uidx++, (int) offset_size * 2, offset, str ?: "???");
11544 : : }
11545 : 16 : fprintf (out, "\n");
11546 : :
11547 [ + - ]: 16 : if (readp != next_unitp)
11548 : 0 : error (0, 0, "extra %zd bytes at end of unit",
11549 : 0 : (size_t) (next_unitp - readp));
11550 : :
11551 : 16 : next_unit:
11552 : : readp = next_unitp;
11553 : : }
11554 : : }
11555 : :
11556 : :
11557 : : /* Print the content of the call frame search table section
11558 : : '.eh_frame_hdr'. */
11559 : : static void
11560 : 140 : print_debug_frame_hdr_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
11561 : : Ebl *ebl __attribute__ ((unused)),
11562 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
11563 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg,
11564 : : FILE *out)
11565 : : {
11566 : 140 : fprintf (out, _("\
11567 : : \nCall frame search table section [%2zu] '.eh_frame_hdr':\n"),
11568 : : elf_ndxscn (scn));
11569 : :
11570 : 140 : Elf_Data *data = elf_rawdata (scn, NULL);
11571 : :
11572 [ - + ]: 140 : if (unlikely (data == NULL))
11573 : : {
11574 : 0 : error (0, 0, _("cannot get %s content: %s"),
11575 : : ".eh_frame_hdr", elf_errmsg (-1));
11576 : 0 : return;
11577 : : }
11578 : :
11579 : 140 : const unsigned char *readp = data->d_buf;
11580 : 140 : const unsigned char *const dataend = ((unsigned char *) data->d_buf
11581 : 140 : + data->d_size);
11582 : :
11583 [ - + ]: 140 : if (unlikely (readp + 4 > dataend))
11584 : : {
11585 : 0 : invalid_data:
11586 : 0 : error (0, 0, _("invalid data"));
11587 : 0 : return;
11588 : : }
11589 : :
11590 : 140 : unsigned int version = *readp++;
11591 : 140 : unsigned int eh_frame_ptr_enc = *readp++;
11592 : 140 : unsigned int fde_count_enc = *readp++;
11593 : 140 : unsigned int table_enc = *readp++;
11594 : :
11595 : 140 : fprintf (out, " version: %u\n"
11596 : : " eh_frame_ptr_enc: %#x ",
11597 : : version, eh_frame_ptr_enc);
11598 : 140 : print_encoding_base ("", eh_frame_ptr_enc, out);
11599 : 140 : fprintf (out, " fde_count_enc: %#x ", fde_count_enc);
11600 : 140 : print_encoding_base ("", fde_count_enc, out);
11601 : 140 : fprintf (out, " table_enc: %#x ", table_enc);
11602 : 140 : print_encoding_base ("", table_enc, out);
11603 : :
11604 : 140 : uint64_t eh_frame_ptr = 0;
11605 [ + - ]: 140 : if (eh_frame_ptr_enc != DW_EH_PE_omit)
11606 : : {
11607 : 140 : readp = read_encoded (eh_frame_ptr_enc, readp, dataend, &eh_frame_ptr,
11608 : : dbg);
11609 [ - + ]: 140 : if (unlikely (readp == NULL))
11610 : 0 : goto invalid_data;
11611 : :
11612 : 140 : fprintf (out, " eh_frame_ptr: %#" PRIx64, eh_frame_ptr);
11613 [ + - ]: 140 : if ((eh_frame_ptr_enc & 0x70) == DW_EH_PE_pcrel)
11614 : 140 : fprintf (out, " (offset: %#" PRIx64 ")",
11615 : : /* +4 because of the 4 byte header of the section. */
11616 : 140 : (uint64_t) shdr->sh_offset + 4 + eh_frame_ptr);
11617 : :
11618 : 140 : fputc ('\n', out);
11619 : : }
11620 : :
11621 : 140 : uint64_t fde_count = 0;
11622 [ + - ]: 140 : if (fde_count_enc != DW_EH_PE_omit)
11623 : : {
11624 : 140 : readp = read_encoded (fde_count_enc, readp, dataend, &fde_count, dbg);
11625 [ - + ]: 140 : if (unlikely (readp == NULL))
11626 : 0 : goto invalid_data;
11627 : :
11628 : 140 : fprintf (out, " fde_count: %" PRIu64 "\n", fde_count);
11629 : : }
11630 : :
11631 [ + - + - ]: 140 : if (fde_count == 0 || table_enc == DW_EH_PE_omit)
11632 : : return;
11633 : :
11634 : 140 : fputs (" Table:\n", out);
11635 : :
11636 : : /* Optimize for the most common case. */
11637 [ + - ]: 140 : if (table_enc == (DW_EH_PE_datarel | DW_EH_PE_sdata4))
11638 [ + + ]: 24176 : while (fde_count > 0 && readp + 8 <= dataend)
11639 : : {
11640 [ - + ]: 24036 : int32_t initial_location = read_4sbyte_unaligned_inc (dbg, readp);
11641 : 24036 : uint64_t initial_offset = ((uint64_t) shdr->sh_offset
11642 : 24036 : + (int64_t) initial_location);
11643 [ - + ]: 24036 : int32_t address = read_4sbyte_unaligned_inc (dbg, readp);
11644 : : // XXX Possibly print symbol name or section offset for initial_offset
11645 : 24036 : fprintf (out, " %#" PRIx32 " (offset: %#6" PRIx64 ") -> %#" PRIx32
11646 : : " fde=[%6" PRIx64 "]\n",
11647 : : initial_location, initial_offset,
11648 : 24036 : address, address - (eh_frame_ptr + 4));
11649 : : }
11650 : : else
11651 : 140 : while (0 && readp < dataend)
11652 : : {
11653 : :
11654 : 140 : }
11655 : : }
11656 : :
11657 : :
11658 : : /* Print the content of the exception handling table section
11659 : : '.eh_frame_hdr'. */
11660 : : static void
11661 : 0 : print_debug_exception_table (Dwfl_Module *dwflmod __attribute__ ((unused)),
11662 : : Ebl *ebl __attribute__ ((unused)),
11663 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
11664 : : Elf_Scn *scn,
11665 : : GElf_Shdr *shdr __attribute__ ((unused)),
11666 : : Dwarf *dbg __attribute__ ((unused)),
11667 : : FILE *out)
11668 : : {
11669 : 0 : fprintf (out, _("\
11670 : : \nException handling table section [%2zu] '.gcc_except_table':\n"),
11671 : : elf_ndxscn (scn));
11672 : :
11673 : 0 : Elf_Data *data = elf_rawdata (scn, NULL);
11674 : :
11675 [ # # ]: 0 : if (unlikely (data == NULL))
11676 : : {
11677 : 0 : error (0, 0, _("cannot get %s content: %s"),
11678 : : ".gcc_except_table", elf_errmsg (-1));
11679 : 0 : return;
11680 : : }
11681 : :
11682 : 0 : const unsigned char *readp = data->d_buf;
11683 : 0 : const unsigned char *const dataend = readp + data->d_size;
11684 : :
11685 [ # # ]: 0 : if (unlikely (readp + 1 > dataend))
11686 : : {
11687 : 0 : invalid_data:
11688 : 0 : error (0, 0, _("invalid data"));
11689 : 0 : return;
11690 : : }
11691 : 0 : unsigned int lpstart_encoding = *readp++;
11692 : 0 : fprintf (out, _(" LPStart encoding: %#x "), lpstart_encoding);
11693 : 0 : print_encoding_base ("", lpstart_encoding, out);
11694 [ # # ]: 0 : if (lpstart_encoding != DW_EH_PE_omit)
11695 : : {
11696 : 0 : uint64_t lpstart;
11697 : 0 : readp = read_encoded (lpstart_encoding, readp, dataend, &lpstart, dbg);
11698 : 0 : fprintf (out, " LPStart: %#" PRIx64 "\n", lpstart);
11699 : : }
11700 : :
11701 [ # # ]: 0 : if (unlikely (readp + 1 > dataend))
11702 : 0 : goto invalid_data;
11703 : 0 : unsigned int ttype_encoding = *readp++;
11704 : 0 : fprintf (out, _(" TType encoding: %#x "), ttype_encoding);
11705 : 0 : print_encoding_base ("", ttype_encoding, out);
11706 : 0 : const unsigned char *ttype_base = NULL;
11707 [ # # ]: 0 : if (ttype_encoding != DW_EH_PE_omit)
11708 : : {
11709 : 0 : unsigned int ttype_base_offset;
11710 [ # # ]: 0 : if (readp >= dataend)
11711 : 0 : goto invalid_data;
11712 : 0 : get_uleb128 (ttype_base_offset, readp, dataend);
11713 : 0 : fprintf (out, " TType base offset: %#x\n", ttype_base_offset);
11714 [ # # ]: 0 : if ((size_t) (dataend - readp) > ttype_base_offset)
11715 : 0 : ttype_base = readp + ttype_base_offset;
11716 : : }
11717 : :
11718 [ # # ]: 0 : if (unlikely (readp + 1 > dataend))
11719 : 0 : goto invalid_data;
11720 : 0 : unsigned int call_site_encoding = *readp++;
11721 : 0 : fprintf (out, _(" Call site encoding: %#x "), call_site_encoding);
11722 : 0 : print_encoding_base ("", call_site_encoding, out);
11723 : 0 : unsigned int call_site_table_len;
11724 [ # # ]: 0 : if (readp >= dataend)
11725 : 0 : goto invalid_data;
11726 : 0 : get_uleb128 (call_site_table_len, readp, dataend);
11727 : :
11728 : 0 : const unsigned char *const action_table = readp + call_site_table_len;
11729 [ # # ]: 0 : if (unlikely (action_table > dataend))
11730 : 0 : goto invalid_data;
11731 : : unsigned int u = 0;
11732 : : unsigned int max_action = 0;
11733 [ # # ]: 0 : while (readp < action_table)
11734 : : {
11735 [ # # ]: 0 : if (u == 0)
11736 : 0 : fputs (_("\n Call site table:\n"), out);
11737 : :
11738 : 0 : uint64_t call_site_start;
11739 : 0 : readp = read_encoded (call_site_encoding, readp, dataend,
11740 : : &call_site_start, dbg);
11741 : 0 : uint64_t call_site_length;
11742 : 0 : readp = read_encoded (call_site_encoding, readp, dataend,
11743 : : &call_site_length, dbg);
11744 : 0 : uint64_t landing_pad;
11745 : 0 : readp = read_encoded (call_site_encoding, readp, dataend,
11746 : : &landing_pad, dbg);
11747 : 0 : unsigned int action;
11748 [ # # ]: 0 : if (readp >= dataend)
11749 : 0 : goto invalid_data;
11750 : 0 : get_uleb128 (action, readp, dataend);
11751 : 0 : max_action = MAX (action, max_action);
11752 : 0 : fprintf (out, _(" [%4u] Call site start: %#" PRIx64 "\n"
11753 : : " Call site length: %" PRIu64 "\n"
11754 : : " Landing pad: %#" PRIx64 "\n"
11755 : : " Action: %u\n"),
11756 : : u++, call_site_start, call_site_length, landing_pad, action);
11757 : : }
11758 [ # # ]: 0 : if (readp != action_table)
11759 : 0 : goto invalid_data;
11760 : :
11761 : 0 : unsigned int max_ar_filter = 0;
11762 [ # # ]: 0 : if (max_action > 0)
11763 : : {
11764 : 0 : fputs ("\n Action table:\n", out);
11765 : :
11766 : 0 : size_t maxdata = (size_t) (dataend - action_table);
11767 [ # # # # ]: 0 : if (max_action > maxdata || maxdata - max_action < 1)
11768 : : {
11769 : 0 : invalid_action_table:
11770 : 0 : fputs (_(" <INVALID DATA>\n"), stdout);
11771 : 0 : return;
11772 : : }
11773 : :
11774 : 0 : const unsigned char *const action_table_end
11775 : 0 : = action_table + max_action + 1;
11776 : :
11777 : 0 : u = 0;
11778 : 0 : do
11779 : : {
11780 : 0 : int ar_filter;
11781 : 0 : get_sleb128 (ar_filter, readp, action_table_end);
11782 [ # # ]: 0 : if (ar_filter > 0 && (unsigned int) ar_filter > max_ar_filter)
11783 : 0 : max_ar_filter = ar_filter;
11784 : 0 : int ar_disp;
11785 [ # # ]: 0 : if (readp >= action_table_end)
11786 : 0 : goto invalid_action_table;
11787 : 0 : get_sleb128 (ar_disp, readp, action_table_end);
11788 : :
11789 : 0 : fprintf (out, " [%4u] ar_filter: % d\n"
11790 : : " ar_disp: % -5d",
11791 : : u, ar_filter, ar_disp);
11792 [ # # ]: 0 : if (abs (ar_disp) & 1)
11793 : 0 : fprintf (out, " -> [%4u]\n", u + (ar_disp + 1) / 2);
11794 [ # # ]: 0 : else if (ar_disp != 0)
11795 : 0 : fputs (" -> ???\n", out);
11796 : : else
11797 : 0 : fputc ('\n', out);
11798 : 0 : ++u;
11799 : : }
11800 [ # # ]: 0 : while (readp < action_table_end);
11801 : : }
11802 : :
11803 [ # # ]: 0 : if (max_ar_filter > 0 && ttype_base != NULL)
11804 : : {
11805 : 0 : unsigned char dsize;
11806 : 0 : fputs ("\n TType table:\n", out);
11807 : :
11808 : : // XXX Not *4, size of encoding;
11809 [ # # ]: 0 : switch (ttype_encoding & 7)
11810 : : {
11811 : : case DW_EH_PE_udata2:
11812 : : case DW_EH_PE_sdata2:
11813 : : dsize = 2;
11814 : : break;
11815 : : case DW_EH_PE_udata4:
11816 : : case DW_EH_PE_sdata4:
11817 : : dsize = 4;
11818 : : break;
11819 : : case DW_EH_PE_udata8:
11820 : : case DW_EH_PE_sdata8:
11821 : : dsize = 8;
11822 : : break;
11823 : 0 : default:
11824 : 0 : dsize = 0;
11825 : 0 : error (1, 0, _("invalid TType encoding"));
11826 : : }
11827 : :
11828 : 0 : if (max_ar_filter
11829 [ # # ]: 0 : > (size_t) (ttype_base - (const unsigned char *) data->d_buf) / dsize)
11830 : 0 : goto invalid_data;
11831 : :
11832 : 0 : readp = ttype_base - max_ar_filter * dsize;
11833 : 0 : do
11834 : : {
11835 : 0 : uint64_t ttype;
11836 : 0 : readp = read_encoded (ttype_encoding, readp, ttype_base, &ttype,
11837 : : dbg);
11838 : 0 : fprintf (out, " [%4u] %#" PRIx64 "\n", max_ar_filter--, ttype);
11839 : : }
11840 [ # # ]: 0 : while (readp < ttype_base);
11841 : : }
11842 : : }
11843 : :
11844 : : /* Print the content of the '.gdb_index' section.
11845 : : http://sourceware.org/gdb/current/onlinedocs/gdb/Index-Section-Format.html
11846 : : */
11847 : : static void
11848 : 8 : print_gdb_index_section (Dwfl_Module *dwflmod, Ebl *ebl,
11849 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
11850 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg,
11851 : : FILE *out)
11852 : : {
11853 : 8 : fprintf (out, _("\nGDB section [%2zu] '%s' at offset %#" PRIx64
11854 : : " contains %" PRId64 " bytes :\n"),
11855 : : elf_ndxscn (scn), section_name (ebl, shdr),
11856 : 8 : (uint64_t) shdr->sh_offset, (uint64_t) shdr->sh_size);
11857 : :
11858 : 8 : Elf_Data *data = elf_rawdata (scn, NULL);
11859 : :
11860 [ - + ]: 8 : if (unlikely (data == NULL))
11861 : : {
11862 : 0 : error (0, 0, _("cannot get %s content: %s"),
11863 : : ".gdb_index", elf_errmsg (-1));
11864 : 4 : return;
11865 : : }
11866 : :
11867 : : // .gdb_index is always in little endian.
11868 : 8 : Dwarf dummy_dbg = { .other_byte_order = MY_ELFDATA != ELFDATA2LSB };
11869 : 8 : dbg = &dummy_dbg;
11870 : :
11871 : 8 : const unsigned char *readp = data->d_buf;
11872 : 8 : const unsigned char *const dataend = readp + data->d_size;
11873 : :
11874 [ - + ]: 8 : if (unlikely (readp + 4 > dataend))
11875 : : {
11876 : 0 : invalid_data:
11877 : 0 : error (0, 0, _("invalid data"));
11878 : 0 : return;
11879 : : }
11880 : :
11881 : 8 : int32_t vers = read_4ubyte_unaligned (dbg, readp);
11882 : 8 : fprintf (out, _(" Version: %" PRId32 "\n"), vers);
11883 : :
11884 : : // The only difference between version 4 and version 5 is the
11885 : : // hash used for generating the table. Version 6 contains symbols
11886 : : // for inlined functions, older versions didn't. Version 7 adds
11887 : : // symbol kinds. Version 8 just indicates that it correctly includes
11888 : : // TUs for symbols. Version 9 adds shortcut table for information
11889 : : // regarding the main function.
11890 [ - + ]: 8 : if (vers < 4 || vers > 9)
11891 : : {
11892 : 0 : fprintf (out, _(" unknown version, cannot parse section\n"));
11893 : 0 : return;
11894 : : }
11895 : :
11896 : 8 : readp += 4;
11897 [ - + ]: 8 : if (unlikely (readp + 4 > dataend))
11898 : 0 : goto invalid_data;
11899 : :
11900 : 8 : uint32_t cu_off = read_4ubyte_unaligned (dbg, readp);
11901 : 8 : fprintf (out, _(" CU offset: %#" PRIx32 "\n"), cu_off);
11902 : :
11903 : 8 : readp += 4;
11904 [ - + ]: 8 : if (unlikely (readp + 4 > dataend))
11905 : 0 : goto invalid_data;
11906 : :
11907 : 8 : uint32_t tu_off = read_4ubyte_unaligned (dbg, readp);
11908 : 8 : fprintf (out, _(" TU offset: %#" PRIx32 "\n"), tu_off);
11909 : :
11910 : 8 : readp += 4;
11911 [ - + ]: 8 : if (unlikely (readp + 4 > dataend))
11912 : 0 : goto invalid_data;
11913 : :
11914 : 8 : uint32_t addr_off = read_4ubyte_unaligned (dbg, readp);
11915 : 8 : fprintf (out, _(" address offset: %#" PRIx32 "\n"), addr_off);
11916 : :
11917 : 8 : readp += 4;
11918 [ - + ]: 8 : if (unlikely (readp + 4 > dataend))
11919 : 0 : goto invalid_data;
11920 : :
11921 : 8 : uint32_t sym_off = read_4ubyte_unaligned (dbg, readp);
11922 : 8 : fprintf (out, _(" symbol offset: %#" PRIx32 "\n"), sym_off);
11923 : :
11924 : 8 : readp += 4;
11925 [ - + ]: 8 : if (unlikely (readp + 4 > dataend))
11926 : 0 : goto invalid_data;
11927 : :
11928 : 8 : uint32_t shortcut_off = 0;
11929 [ + + ]: 8 : if (vers >= 9)
11930 : : {
11931 : 4 : shortcut_off = read_4ubyte_unaligned (dbg, readp);
11932 : 4 : fprintf (out, _(" shortcut offset: %#" PRIx32 "\n"), shortcut_off);
11933 : :
11934 : 4 : readp += 4;
11935 [ - + ]: 4 : if (unlikely (readp + 4 > dataend))
11936 : 0 : goto invalid_data;
11937 : : }
11938 : :
11939 : 8 : uint32_t const_off = read_4ubyte_unaligned (dbg, readp);
11940 : 8 : fprintf (out, _(" constant offset: %#" PRIx32 "\n"), const_off);
11941 : :
11942 [ - + ]: 8 : if (unlikely ((size_t) (dataend - (const unsigned char *) data->d_buf)
11943 : : < const_off))
11944 : 0 : goto invalid_data;
11945 : :
11946 : 8 : readp = data->d_buf + cu_off;
11947 : :
11948 : 8 : const unsigned char *nextp = data->d_buf + tu_off;
11949 [ - + ]: 8 : if (tu_off >= data->d_size)
11950 : 0 : goto invalid_data;
11951 : :
11952 : 8 : size_t cu_nr = (nextp - readp) / 16;
11953 : :
11954 : 8 : fprintf (out, _("\n CU list at offset %#" PRIx32
11955 : : " contains %zu entries:\n"),
11956 : : cu_off, cu_nr);
11957 : :
11958 : 8 : size_t n = 0;
11959 [ + - + + ]: 22 : while (dataend - readp >= 16 && n < cu_nr)
11960 : : {
11961 : 14 : uint64_t off = read_8ubyte_unaligned (dbg, readp);
11962 : 14 : readp += 8;
11963 : :
11964 : 14 : uint64_t len = read_8ubyte_unaligned (dbg, readp);
11965 : 14 : readp += 8;
11966 : :
11967 : 14 : fprintf (out, " [%4zu] start: %0#8" PRIx64
11968 : : ", length: %5" PRIu64 "\n", n, off, len);
11969 : 14 : n++;
11970 : : }
11971 : :
11972 : 8 : readp = data->d_buf + tu_off;
11973 : 8 : nextp = data->d_buf + addr_off;
11974 [ - + ]: 8 : if (addr_off >= data->d_size)
11975 : 0 : goto invalid_data;
11976 : :
11977 : 8 : size_t tu_nr = (nextp - readp) / 24;
11978 : :
11979 : 8 : fprintf (out, _("\n TU list at offset %#" PRIx32
11980 : : " contains %zu entries:\n"),
11981 : : tu_off, tu_nr);
11982 : :
11983 : 8 : n = 0;
11984 [ + - + + ]: 14 : while (dataend - readp >= 24 && n < tu_nr)
11985 : : {
11986 : 6 : uint64_t off = read_8ubyte_unaligned (dbg, readp);
11987 : 6 : readp += 8;
11988 : :
11989 : 6 : uint64_t type = read_8ubyte_unaligned (dbg, readp);
11990 : 6 : readp += 8;
11991 : :
11992 : 6 : uint64_t sig = read_8ubyte_unaligned (dbg, readp);
11993 : 6 : readp += 8;
11994 : :
11995 : 6 : fprintf (out, " [%4zu] CU offset: %5" PRId64
11996 : : ", type offset: %5" PRId64
11997 : : ", signature: %0#8" PRIx64 "\n", n, off, type, sig);
11998 : 6 : n++;
11999 : : }
12000 : :
12001 : 8 : readp = data->d_buf + addr_off;
12002 : 8 : nextp = data->d_buf + sym_off;
12003 [ - + ]: 8 : if (sym_off >= data->d_size)
12004 : 0 : goto invalid_data;
12005 : :
12006 : 8 : size_t addr_nr = (nextp - readp) / 20;
12007 : :
12008 : 8 : fprintf (out, _("\n Address list at offset %#" PRIx32
12009 : : " contains %zu entries:\n"),
12010 : : addr_off, addr_nr);
12011 : :
12012 : 8 : n = 0;
12013 [ + - + + ]: 22 : while (dataend - readp >= 20 && n < addr_nr)
12014 : : {
12015 : 14 : uint64_t low = read_8ubyte_unaligned (dbg, readp);
12016 : 14 : readp += 8;
12017 : :
12018 : 14 : uint64_t high = read_8ubyte_unaligned (dbg, readp);
12019 : 14 : readp += 8;
12020 : :
12021 : 14 : uint32_t idx = read_4ubyte_unaligned (dbg, readp);
12022 : 14 : readp += 4;
12023 : :
12024 : 14 : fprintf (out, " [%4zu] ", n);
12025 : 14 : print_dwarf_addr (dwflmod, 8, low, low, out);
12026 : 14 : fprintf (out, "..");
12027 : 14 : print_dwarf_addr (dwflmod, 8, high - 1, high, out);
12028 : 14 : fprintf (out, ", CU index: %5" PRId32 "\n", idx);
12029 : 14 : n++;
12030 : : }
12031 : :
12032 : 8 : const unsigned char *const_start = data->d_buf + const_off;
12033 [ - + ]: 8 : if (const_off > data->d_size)
12034 : 0 : goto invalid_data;
12035 : :
12036 : 8 : const unsigned char *shortcut_start = NULL;
12037 [ + + ]: 8 : if (vers >= 9)
12038 : : {
12039 [ - + ]: 4 : if (shortcut_off >= data->d_size)
12040 : 0 : goto invalid_data;
12041 : :
12042 : 4 : shortcut_start = data->d_buf + shortcut_off;
12043 : 4 : nextp = shortcut_start;
12044 : : }
12045 : : else
12046 : : nextp = const_start;
12047 : :
12048 : 8 : readp = data->d_buf + sym_off;
12049 : 8 : size_t sym_nr = (nextp - readp) / 8;
12050 : :
12051 : 8 : fprintf (out, _("\n Symbol table at offset %#" PRIx32
12052 : : " contains %zu slots:\n"),
12053 : : sym_off, sym_nr);
12054 : :
12055 : 8 : n = 0;
12056 [ + - + + ]: 8200 : while (dataend - readp >= 8 && n < sym_nr)
12057 : : {
12058 : 8192 : uint32_t name = read_4ubyte_unaligned (dbg, readp);
12059 : 8192 : readp += 4;
12060 : :
12061 : 8192 : uint32_t vector = read_4ubyte_unaligned (dbg, readp);
12062 : 8192 : readp += 4;
12063 : :
12064 [ + + ]: 8192 : if (name != 0 || vector != 0)
12065 : : {
12066 : 60 : const unsigned char *sym = const_start + name;
12067 [ + - - + ]: 60 : if (unlikely ((size_t) (dataend - const_start) < name
12068 : : || memchr (sym, '\0', dataend - sym) == NULL))
12069 : 0 : goto invalid_data;
12070 : :
12071 : 60 : fprintf (out, " [%4zu] symbol: %s, CUs: ", n, sym);
12072 : :
12073 : 60 : const unsigned char *readcus = const_start + vector;
12074 [ - + ]: 60 : if (unlikely ((size_t) (dataend - const_start) < vector))
12075 : 0 : goto invalid_data;
12076 : 60 : uint32_t cus = read_4ubyte_unaligned (dbg, readcus);
12077 [ + + ]: 128 : while (cus--)
12078 : : {
12079 : 68 : uint32_t cu_kind, cu, kind;
12080 : 68 : bool is_static;
12081 : 68 : readcus += 4;
12082 [ - + ]: 68 : if (unlikely (readcus + 4 > dataend))
12083 : 0 : goto invalid_data;
12084 : 68 : cu_kind = read_4ubyte_unaligned (dbg, readcus);
12085 : 68 : cu = cu_kind & ((1 << 24) - 1);
12086 : 68 : kind = (cu_kind >> 28) & 7;
12087 : 68 : is_static = cu_kind & (1U << 31);
12088 [ + + ]: 68 : if (cu > cu_nr - 1)
12089 : 10 : fprintf (out, "%" PRId32 "T", cu - (uint32_t) cu_nr);
12090 : : else
12091 : 58 : fprintf (out, "%" PRId32, cu);
12092 [ + + ]: 68 : if (kind != 0)
12093 : : {
12094 : 52 : fprintf (out, " (");
12095 [ + + + - : 52 : switch (kind)
- ]
12096 : : {
12097 : : case 1:
12098 : 20 : fprintf (out, "type");
12099 : 20 : break;
12100 : : case 2:
12101 : 16 : fprintf (out, "var");
12102 : 16 : break;
12103 : : case 3:
12104 : 16 : fprintf (out, "func");
12105 : 16 : break;
12106 : : case 4:
12107 : 0 : fprintf (out, "other");
12108 : 0 : break;
12109 : : default:
12110 : 0 : fprintf (out, "unknown-0x%" PRIx32, kind);
12111 : 0 : break;
12112 : : }
12113 [ + + ]: 68 : fprintf (out, ":%c)", (is_static ? 'S' : 'G'));
12114 : : }
12115 [ + + ]: 68 : if (cus > 0)
12116 : 8 : fprintf (out, ", ");
12117 : : }
12118 : 60 : fprintf (out, "\n");
12119 : : }
12120 : 8192 : n++;
12121 : : }
12122 : :
12123 [ + + ]: 8 : if (vers < 9)
12124 : : return;
12125 : :
12126 [ - + ]: 4 : if (unlikely (shortcut_start == NULL))
12127 : 0 : goto invalid_data;
12128 : :
12129 : 4 : readp = shortcut_start;
12130 : 4 : nextp = const_start;
12131 : 4 : size_t shortcut_nr = (nextp - readp) / 4;
12132 : :
12133 [ - + ]: 4 : if (unlikely (shortcut_nr != 2))
12134 : 0 : goto invalid_data;
12135 : :
12136 : 8 : fprintf (out,
12137 : 4 : _("\nShortcut table at offset %#" PRIx32 " contains %zu slots:\n"),
12138 : : shortcut_off, shortcut_nr);
12139 : :
12140 : 4 : uint32_t lang = read_4ubyte_unaligned (dbg, readp);
12141 : 4 : readp += 4;
12142 : :
12143 : : /* Include the hex number of LANG in the output if the language
12144 : : is unknown. */
12145 : 4 : const char *lang_str = dwarf_lang_string (lang);
12146 : 4 : lang_str = string_or_unknown (lang_str, lang, DW_LANG_lo_user,
12147 : : DW_LANG_hi_user, true);
12148 : :
12149 : 4 : fprintf (out, _("Language of main: %s\n"), lang_str);
12150 : 4 : fprintf (out, _("Name of main: "));
12151 : :
12152 [ + + ]: 4 : if (lang != 0)
12153 : : {
12154 : 2 : uint32_t name = read_4ubyte_unaligned (dbg, readp);
12155 : 2 : readp += 4;
12156 : 2 : const unsigned char *sym = const_start + name;
12157 : :
12158 [ + - - + ]: 2 : if (unlikely ((size_t) (dataend - const_start) < name
12159 : : || memchr (sym, '\0', dataend - sym) == NULL))
12160 : 0 : goto invalid_data;
12161 : :
12162 : 2 : fprintf (out, "%s\n", sym);
12163 : : }
12164 : : else
12165 : 2 : fprintf (out, "<unknown>\n");
12166 : : }
12167 : :
12168 : : /* Returns true and sets split DWARF CU id if there is a split compile
12169 : : unit in the given Dwarf, and no non-split units are found (before it). */
12170 : : static bool
12171 : 396 : is_split_dwarf (Dwarf *dbg, uint64_t *id, Dwarf_CU **split_cu)
12172 : : {
12173 : 396 : Dwarf_CU *cu = NULL;
12174 [ + + ]: 396 : while (dwarf_get_units (dbg, cu, &cu, NULL, NULL, NULL, NULL) == 0)
12175 : : {
12176 : 394 : uint8_t unit_type;
12177 [ + - ]: 394 : if (dwarf_cu_info (cu, NULL, &unit_type, NULL, NULL,
12178 : : id, NULL, NULL) != 0)
12179 : 394 : return false;
12180 : :
12181 [ + + ]: 394 : if (unit_type != DW_UT_split_compile && unit_type != DW_UT_split_type)
12182 : : return false;
12183 : :
12184 : : /* We really only care about the split compile unit, the types
12185 : : should be fine and self sufficient. Also they don't have an
12186 : : id that we can match with a skeleton unit. */
12187 [ + - ]: 18 : if (unit_type == DW_UT_split_compile)
12188 : : {
12189 : 18 : *split_cu = cu;
12190 : 18 : return true;
12191 : : }
12192 : : }
12193 : :
12194 : : return false;
12195 : : }
12196 : :
12197 : : /* Check that there is one and only one Dwfl_Module, return in arg. */
12198 : : static int
12199 : 18 : getone_dwflmod (Dwfl_Module *dwflmod,
12200 : : void **userdata __attribute__ ((unused)),
12201 : : const char *name __attribute__ ((unused)),
12202 : : Dwarf_Addr base __attribute__ ((unused)),
12203 : : void *arg)
12204 : : {
12205 : 18 : Dwfl_Module **m = (Dwfl_Module **) arg;
12206 [ + - ]: 18 : if (*m != NULL)
12207 : : return DWARF_CB_ABORT;
12208 : 18 : *m = dwflmod;
12209 : 18 : return DWARF_CB_OK;
12210 : : }
12211 : :
12212 : : typedef struct Job_Data {
12213 : : struct Job_Data *next;
12214 : : Dwfl_Module *dwflmod;
12215 : : Ebl *ebl;
12216 : : GElf_Ehdr *ehdr;
12217 : : Elf_Scn scn;
12218 : : GElf_Shdr shdr;
12219 : : Dwarf *dbg;
12220 : : FILE *out;
12221 : : void (*fp) (Dwfl_Module *, Ebl *, GElf_Ehdr *,
12222 : : Elf_Scn *, GElf_Shdr *, Dwarf *, FILE *);
12223 : : } job_data;
12224 : :
12225 : : #ifdef USE_LOCKS
12226 : :
12227 : : /* Thread entry point. */
12228 : : static void *
12229 : : do_job (void *data, FILE *out)
12230 : : {
12231 : : job_data *d = (job_data *) data;
12232 : : d->fp (d->dwflmod, d->ebl, d->ehdr, &d->scn, &d->shdr, d->dbg, out);
12233 : : return NULL;
12234 : : }
12235 : : #endif
12236 : :
12237 : : /* If readelf is built with thread safety, then set up JDATA at index IDX
12238 : : and add it to the job queue.
12239 : :
12240 : : If thread safety is not supported or the maximum number of threads is set
12241 : : to 1, then immediately call START_ROUTINE with the given arguments. */
12242 : : static void
12243 : 1094 : schedule_job (job_data **jdatalist,
12244 : : void (*start_routine) (Dwfl_Module *, Ebl *, GElf_Ehdr *,
12245 : : Elf_Scn *, GElf_Shdr *, Dwarf *, FILE *),
12246 : : Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr, Elf_Scn *scn,
12247 : : GElf_Shdr *shdr, Dwarf *dbg)
12248 : : {
12249 : : #ifdef USE_LOCKS
12250 : : if (max_threads > 1)
12251 : : {
12252 : : job_data *jdata = xmalloc (sizeof (job_data));
12253 : :
12254 : : jdata->dwflmod = dwflmod;
12255 : : jdata->ebl = ebl;
12256 : : jdata->ehdr = ehdr;
12257 : : jdata->scn = *scn;
12258 : : jdata->shdr = *shdr;
12259 : : jdata->dbg = dbg;
12260 : : jdata->fp = start_routine;
12261 : : jdata->next = *jdatalist;
12262 : : *jdatalist = jdata;
12263 : :
12264 : : add_job (do_job, (void *) jdata);
12265 : : }
12266 : : else
12267 : : start_routine (dwflmod, ebl, ehdr, scn, shdr, dbg, stdout);
12268 : : #else
12269 : 1094 : (void) jdatalist;
12270 : :
12271 : 1094 : start_routine (dwflmod, ebl, ehdr, scn, shdr, dbg, stdout);
12272 : : #endif
12273 : 1094 : }
12274 : :
12275 : : static void
12276 : 502 : print_debug (Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr)
12277 : : {
12278 : : /* Used for skeleton file, if necessary for split DWARF. */
12279 : 502 : Dwfl *skel_dwfl = NULL;
12280 : 502 : Dwfl_Module *skel_mod = NULL;
12281 : 502 : char *skel_name = NULL;
12282 : 502 : Dwarf *split_dbg = NULL;
12283 : 502 : Dwarf_CU *split_cu = NULL;
12284 : :
12285 : : /* Before we start the real work get a debug context descriptor. */
12286 : 502 : Dwarf_Addr dwbias;
12287 : 502 : Dwarf *dbg = dwfl_module_getdwarf (dwflmod, &dwbias);
12288 : 502 : Dwarf dummy_dbg =
12289 : : {
12290 : 502 : .elf = ebl->elf,
12291 : 502 : .other_byte_order = MY_ELFDATA != ehdr->e_ident[EI_DATA]
12292 : 502 : };
12293 [ + + ]: 502 : if (dbg == NULL)
12294 : : {
12295 [ - + ]: 106 : if ((print_debug_sections & ~(section_exception|section_frame)) != 0)
12296 : 0 : error (0, 0, _("cannot get debug context descriptor: %s"),
12297 : : dwfl_errmsg (-1));
12298 : : dbg = &dummy_dbg;
12299 : : }
12300 : : else
12301 : : {
12302 : : /* If we are asked about a split dwarf (.dwo) file, use the user
12303 : : provided, or find the corresponding skeleton file. If we got
12304 : : a skeleton file, replace the given dwflmod and dbg, with one
12305 : : derived from the skeleton file to provide enough context. */
12306 : 396 : uint64_t split_id;
12307 [ + + ]: 396 : if (is_split_dwarf (dbg, &split_id, &split_cu))
12308 : : {
12309 [ + - ]: 18 : if (dwarf_skeleton != NULL)
12310 : 18 : skel_name = strdup (dwarf_skeleton);
12311 : : else
12312 : : {
12313 : : /* Replace file.dwo with file.o and see if that matches. */
12314 : 0 : const char *fname;
12315 : 0 : dwfl_module_info (dwflmod, NULL, NULL, NULL, NULL, NULL,
12316 : : &fname, NULL);
12317 [ # # ]: 0 : if (fname != NULL)
12318 : : {
12319 : 0 : size_t flen = strlen (fname);
12320 [ # # # # ]: 0 : if (flen > 4 && strcmp (".dwo", fname + flen - 4) == 0)
12321 : : {
12322 : 0 : skel_name = strdup (fname);
12323 [ # # ]: 0 : if (skel_name != NULL)
12324 : : {
12325 : 0 : skel_name[flen - 3] = 'o';
12326 : 0 : skel_name[flen - 2] = '\0';
12327 : : }
12328 : : }
12329 : : }
12330 : : }
12331 : :
12332 [ - + ]: 18 : if (skel_name != NULL)
12333 : : {
12334 : 18 : int skel_fd = open (skel_name, O_RDONLY);
12335 [ - + ]: 18 : if (skel_fd == -1)
12336 : 0 : fprintf (stderr, "Warning: Couldn't open DWARF skeleton file"
12337 : : " '%s'\n", skel_name);
12338 : : else
12339 : : {
12340 : 18 : skel_dwfl = create_dwfl (skel_fd, skel_name);
12341 : :
12342 : : /* skel_fd was dup'ed by create_dwfl. We can close the
12343 : : original now. */
12344 : 18 : close (skel_fd);
12345 : : }
12346 : :
12347 [ + - ]: 18 : if (skel_dwfl != NULL)
12348 : : {
12349 [ - + ]: 18 : if (dwfl_getmodules (skel_dwfl, &getone_dwflmod,
12350 : : &skel_mod, 0) != 0)
12351 : : {
12352 : 0 : fprintf (stderr, "Warning: Bad DWARF skeleton,"
12353 : : " multiple modules '%s'\n", skel_name);
12354 : 0 : dwfl_end (skel_dwfl);
12355 : 0 : skel_mod = NULL;
12356 : : }
12357 : : }
12358 [ # # ]: 0 : else if (skel_fd != -1)
12359 : 0 : fprintf (stderr, "Warning: Couldn't create skeleton dwfl for"
12360 : : " '%s': %s\n", skel_name, dwfl_errmsg (-1));
12361 : :
12362 [ + - ]: 18 : if (skel_mod != NULL)
12363 : : {
12364 : 18 : Dwarf *skel_dbg = dwfl_module_getdwarf (skel_mod, &dwbias);
12365 [ + - ]: 18 : if (skel_dbg != NULL)
12366 : : {
12367 : : /* First check the skeleton CU DIE, only fetch
12368 : : the split DIE if we know the id matches to
12369 : : not unnecessary search for any split DIEs we
12370 : : don't need. */
12371 : 18 : Dwarf_CU *cu = NULL;
12372 : 18 : while (dwarf_get_units (skel_dbg, cu, &cu,
12373 [ + - ]: 26 : NULL, NULL, NULL, NULL) == 0)
12374 : : {
12375 : 26 : uint8_t unit_type;
12376 : 26 : uint64_t skel_id;
12377 [ + - ]: 26 : if (dwarf_cu_info (cu, NULL, &unit_type, NULL, NULL,
12378 : : &skel_id, NULL, NULL) == 0
12379 [ + - ]: 26 : && unit_type == DW_UT_skeleton
12380 [ + + ]: 26 : && split_id == skel_id)
12381 : : {
12382 : 18 : Dwarf_Die subdie;
12383 [ + - ]: 18 : if (dwarf_cu_info (cu, NULL, NULL, NULL,
12384 : : &subdie,
12385 : : NULL, NULL, NULL) == 0
12386 [ + - ]: 18 : && dwarf_tag (&subdie) != DW_TAG_invalid)
12387 : : {
12388 : 18 : split_dbg = dwarf_cu_getdwarf (subdie.cu);
12389 [ - + ]: 18 : if (split_dbg == NULL)
12390 : 0 : fprintf (stderr,
12391 : : "Warning: Couldn't get split_dbg:"
12392 : : " %s\n", dwarf_errmsg (-1));
12393 : 0 : break;
12394 : : }
12395 : : else
12396 : : {
12397 : : /* Everything matches up, but not
12398 : : according to libdw. Which means
12399 : : the user knew better. So...
12400 : : Terrible hack... We can never
12401 : : destroy the underlying dwfl
12402 : : because it would free the wrong
12403 : : Dwarfs... So we leak memory...*/
12404 [ # # ]: 0 : if (cu->split == NULL
12405 [ # # ]: 0 : && dwarf_skeleton != NULL)
12406 : : {
12407 : 0 : do_not_close_dwfl = true;
12408 : 0 : __libdw_link_skel_split (cu, split_cu);
12409 : 0 : split_dbg = dwarf_cu_getdwarf (split_cu);
12410 : 0 : break;
12411 : : }
12412 : : else
12413 : 0 : fprintf (stderr, "Warning: Couldn't get"
12414 : : " skeleton subdie: %s\n",
12415 : : dwarf_errmsg (-1));
12416 : : }
12417 : : }
12418 : : }
12419 [ - - ]: 18 : if (split_dbg == NULL)
12420 : 0 : fprintf (stderr, "Warning: '%s' didn't contain a skeleton for split id %" PRIx64 "\n", skel_name, split_id);
12421 : : }
12422 : : else
12423 : 0 : fprintf (stderr, "Warning: Couldn't get skeleton DWARF:"
12424 : : " %s\n", dwfl_errmsg (-1));
12425 : : }
12426 : : }
12427 : :
12428 : 0 : if (split_dbg != NULL)
12429 : : {
12430 : 18 : dbg = split_dbg;
12431 : 18 : dwflmod = skel_mod;
12432 : : }
12433 [ # # ]: 0 : else if (skel_name == NULL)
12434 : 0 : fprintf (stderr,
12435 : : "Warning: split DWARF file, but no skeleton found.\n");
12436 : : }
12437 [ + - ]: 378 : else if (dwarf_skeleton != NULL)
12438 : 0 : fprintf (stderr, "Warning: DWARF skeleton given,"
12439 : : " but not a split DWARF file\n");
12440 : : }
12441 : :
12442 : : /* Get the section header string table index. */
12443 : 502 : size_t shstrndx;
12444 [ - + ]: 502 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
12445 : 0 : error_exit (0, _("cannot get section header string table index"));
12446 : :
12447 : 502 : job_data *jdatalist = NULL;
12448 : :
12449 : : /* If the .debug_info section is listed as implicitly required then
12450 : : we must make sure to handle it before handling any other debug
12451 : : section. Various other sections depend on the CU DIEs being
12452 : : scanned (silently) first. */
12453 : 502 : bool implicit_info = (implicit_debug_sections & section_info) != 0;
12454 : 502 : bool explicit_info = (print_debug_sections & section_info) != 0;
12455 [ + + ]: 502 : if (implicit_info)
12456 : : {
12457 : : Elf_Scn *scn = NULL;
12458 [ + - ]: 3132 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
12459 : : {
12460 : 3132 : GElf_Shdr shdr_mem;
12461 : 3132 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
12462 : :
12463 [ + - + + ]: 3132 : if (shdr != NULL && shdr->sh_type == SHT_PROGBITS)
12464 : : {
12465 : 2448 : const char *name = elf_strptr (ebl->elf, shstrndx,
12466 : 1224 : shdr->sh_name);
12467 [ - + ]: 1224 : if (name == NULL)
12468 : 0 : continue;
12469 : :
12470 [ + + ]: 1224 : if (strcmp (name, ".debug_info") == 0
12471 [ + + ]: 1092 : || strcmp (name, ".debug_info.dwo") == 0
12472 [ + + ]: 1074 : || strcmp (name, ".zdebug_info") == 0
12473 [ + - ]: 1062 : || strcmp (name, ".zdebug_info.dwo") == 0
12474 [ + + ]: 1062 : || strcmp (name, ".gnu.debuglto_.debug_info") == 0)
12475 : : {
12476 : 164 : print_debug_info_section (dwflmod, ebl, ehdr,
12477 : : scn, shdr, dbg, stdout);
12478 : 164 : break;
12479 : : }
12480 : : }
12481 : : }
12482 : :
12483 : 164 : print_debug_sections &= ~section_info;
12484 : 164 : implicit_debug_sections &= ~section_info;
12485 : : }
12486 : :
12487 : : /* Look through all the sections for the debugging sections to print. */
12488 : : Elf_Scn *scn = NULL;
12489 [ + + ]: 16590 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
12490 : : {
12491 : 16088 : GElf_Shdr shdr_mem;
12492 : 16088 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
12493 : :
12494 [ + - + + ]: 16088 : if (shdr != NULL && shdr->sh_type == SHT_PROGBITS)
12495 : : {
12496 : 6758 : static const struct
12497 : : {
12498 : : const char *name;
12499 : : enum section_e bitmask;
12500 : : void (*fp) (Dwfl_Module *, Ebl *,
12501 : : GElf_Ehdr *, Elf_Scn *, GElf_Shdr *, Dwarf *, FILE *);
12502 : : } debug_sections[] =
12503 : : {
12504 : : #define NEW_SECTION(name) \
12505 : : { ".debug_" #name, section_##name, print_debug_##name##_section }
12506 : : NEW_SECTION (abbrev),
12507 : : NEW_SECTION (addr),
12508 : : NEW_SECTION (aranges),
12509 : : NEW_SECTION (frame),
12510 : : NEW_SECTION (info),
12511 : : NEW_SECTION (types),
12512 : : NEW_SECTION (line),
12513 : : NEW_SECTION (loc),
12514 : : /* loclists is loc for DWARF5. */
12515 : : { ".debug_loclists", section_loc,
12516 : : print_debug_loclists_section },
12517 : : NEW_SECTION (pubnames),
12518 : : NEW_SECTION (str),
12519 : : /* A DWARF5 specialised debug string section. */
12520 : : { ".debug_line_str", section_str,
12521 : : print_debug_str_section },
12522 : : /* DWARF5 string offsets table. */
12523 : : { ".debug_str_offsets", section_str,
12524 : : print_debug_str_offsets_section },
12525 : : NEW_SECTION (macinfo),
12526 : : NEW_SECTION (macro),
12527 : : NEW_SECTION (ranges),
12528 : : /* rnglists is ranges for DWARF5. */
12529 : : { ".debug_rnglists", section_ranges,
12530 : : print_debug_rnglists_section },
12531 : : { ".eh_frame", section_frame | section_exception,
12532 : : print_debug_frame_section },
12533 : : { ".eh_frame_hdr", section_frame | section_exception,
12534 : : print_debug_frame_hdr_section },
12535 : : { ".gcc_except_table", section_frame | section_exception,
12536 : : print_debug_exception_table },
12537 : : { ".gdb_index", section_gdb_index, print_gdb_index_section }
12538 : : };
12539 : 6758 : const int ndebug_sections = (sizeof (debug_sections)
12540 : : / sizeof (debug_sections[0]));
12541 : 13516 : const char *name = elf_strptr (ebl->elf, shstrndx,
12542 : 6758 : shdr->sh_name);
12543 [ - + ]: 6758 : if (name == NULL)
12544 : 0 : continue;
12545 : :
12546 : : int n;
12547 [ + + ]: 106506 : for (n = 0; n < ndebug_sections; ++n)
12548 : : {
12549 : 103042 : size_t dbglen = strlen (debug_sections[n].name);
12550 : 103042 : size_t scnlen = strlen (name);
12551 [ + + ]: 103042 : if ((strncmp (name, debug_sections[n].name, dbglen) == 0
12552 [ + + ]: 3558 : && (dbglen == scnlen
12553 [ + + ]: 546 : || (scnlen == dbglen + 4
12554 [ + + ]: 466 : && strstr (name, ".dwo") == name + dbglen)))
12555 [ + + + + ]: 99922 : || (name[0] == '.' && name[1] == 'z'
12556 [ + - ]: 1132 : && debug_sections[n].name[1] == 'd'
12557 [ + + ]: 1132 : && strncmp (&name[2], &debug_sections[n].name[1],
12558 : : dbglen - 1) == 0
12559 [ - + ]: 164 : && (scnlen == dbglen + 1
12560 [ # # ]: 0 : || (scnlen == dbglen + 5
12561 [ # # ]: 0 : && strstr (name, ".dwo") == name + dbglen + 1)))
12562 [ + + ]: 99758 : || (scnlen > 14 /* .gnu.debuglto_ prefix. */
12563 [ + + ]: 10170 : && startswith (name, ".gnu.debuglto_")
12564 [ + + ]: 72 : && strcmp (&name[14], debug_sections[n].name) == 0)
12565 : : )
12566 : : {
12567 : 3294 : if (((print_debug_sections | implicit_debug_sections)
12568 [ + + ]: 3294 : & debug_sections[n].bitmask))
12569 : 1094 : schedule_job (&jdatalist, debug_sections[n].fp,
12570 : : dwflmod, ebl, ehdr, scn, shdr, dbg);
12571 : :
12572 : : break;
12573 : : }
12574 : : }
12575 : : }
12576 : : }
12577 : :
12578 : : #ifdef USE_LOCKS
12579 : : /* If max_threads <= 1, then jobs were immediately run in schedule_job. */
12580 : : if (max_threads > 1)
12581 : : run_jobs (max_threads);
12582 : : #endif
12583 : :
12584 : 502 : dwfl_end (skel_dwfl);
12585 : 502 : free (skel_name);
12586 : :
12587 : 502 : while (jdatalist != NULL)
12588 : : {
12589 : : job_data *jdata = jdatalist;
12590 : : jdatalist = jdatalist->next;
12591 : : free (jdata);
12592 : : }
12593 : :
12594 : : /* Turn implicit and/or explicit back on in case we go over another file. */
12595 [ + + ]: 502 : if (implicit_info)
12596 : 164 : implicit_debug_sections |= section_info;
12597 [ + + ]: 502 : if (explicit_info)
12598 : 134 : print_debug_sections |= section_info;
12599 : :
12600 : 502 : reset_listptr (&known_locsptr);
12601 : 502 : reset_listptr (&known_loclistsptr);
12602 : 502 : reset_listptr (&known_rangelistptr);
12603 : 502 : reset_listptr (&known_rnglistptr);
12604 : 502 : reset_listptr (&known_addrbases);
12605 : 502 : reset_listptr (&known_stroffbases);
12606 : 502 : }
12607 : :
12608 : :
12609 : : #define ITEM_INDENT 4
12610 : : #define WRAP_COLUMN 75
12611 : :
12612 : : /* Print "NAME: FORMAT", wrapping when output text would make the line
12613 : : exceed WRAP_COLUMN. Unpadded numbers look better for the core items
12614 : : but this function is also used for registers which should be printed
12615 : : aligned. Fortunately registers output uses fixed fields width (such
12616 : : as %11d) for the alignment.
12617 : :
12618 : : Line breaks should not depend on the particular values although that
12619 : : may happen in some cases of the core items. */
12620 : :
12621 : : static unsigned int
12622 : : __attribute__ ((format (printf, 6, 7)))
12623 : 1642 : print_core_item (unsigned int colno, char sep, unsigned int wrap,
12624 : : size_t name_width, const char *name, const char *format, ...)
12625 : : {
12626 : 1642 : size_t len = strlen (name);
12627 : 1642 : if (name_width < len)
12628 : : name_width = len;
12629 : :
12630 : 1642 : char *out;
12631 : 1642 : va_list ap;
12632 : 1642 : va_start (ap, format);
12633 : 1642 : int out_len = vasprintf (&out, format, ap);
12634 : 1642 : va_end (ap);
12635 [ - + ]: 1642 : if (out_len == -1)
12636 : 0 : error_exit (0, _("memory exhausted"));
12637 : :
12638 : 1642 : size_t n = name_width + sizeof ": " - 1 + out_len;
12639 : :
12640 [ + + ]: 1642 : if (colno == 0)
12641 : : {
12642 : 102 : printf ("%*s", ITEM_INDENT, "");
12643 : 102 : colno = ITEM_INDENT + n;
12644 : : }
12645 [ + + ]: 1540 : else if (colno + 2 + n < wrap)
12646 : : {
12647 : 936 : printf ("%c ", sep);
12648 : 936 : colno += 2 + n;
12649 : : }
12650 : : else
12651 : : {
12652 : 604 : printf ("\n%*s", ITEM_INDENT, "");
12653 : 604 : colno = ITEM_INDENT + n;
12654 : : }
12655 : :
12656 : 1642 : printf ("%s: %*s%s", name, (int) (name_width - len), "", out);
12657 : :
12658 : 1642 : free (out);
12659 : :
12660 : 1642 : return colno;
12661 : : }
12662 : :
12663 : : static const void *
12664 : 1862 : convert (Elf *core, Elf_Type type, uint_fast16_t count,
12665 : : void *value, const void *data, size_t size)
12666 : : {
12667 : 3724 : Elf_Data valuedata =
12668 : : {
12669 : : .d_type = type,
12670 : : .d_buf = value,
12671 [ + + ]: 1862 : .d_size = size ?: gelf_fsize (core, type, count, EV_CURRENT),
12672 : : .d_version = EV_CURRENT,
12673 : : };
12674 : 1862 : Elf_Data indata =
12675 : : {
12676 : : .d_type = type,
12677 : : .d_buf = (void *) data,
12678 : : .d_size = valuedata.d_size,
12679 : : .d_version = EV_CURRENT,
12680 : : };
12681 : :
12682 : 1862 : Elf_Data *d = (gelf_getclass (core) == ELFCLASS32
12683 [ + + ]: 1862 : ? elf32_xlatetom : elf64_xlatetom)
12684 : 1862 : (&valuedata, &indata, elf_getident (core, NULL)[EI_DATA]);
12685 [ - + ]: 1862 : if (d == NULL)
12686 : 0 : error_exit (0, _("cannot convert core note data: %s"),
12687 : : elf_errmsg (-1));
12688 : :
12689 : 1862 : return data + indata.d_size;
12690 : : }
12691 : :
12692 : : typedef uint8_t GElf_Byte;
12693 : :
12694 : : static unsigned int
12695 : 800 : handle_core_item (Elf *core, const GElf_Ehdr *ehdr,
12696 : : const Ebl_Core_Item *item, const void *desc,
12697 : : unsigned int colno, size_t *repeated_size)
12698 : : {
12699 [ + + ]: 800 : uint_fast16_t count = item->count ?: 1;
12700 : : /* Ebl_Core_Item count is always a small number.
12701 : : Make sure the backend didn't put in some large bogus value. */
12702 [ - + ]: 124 : assert (count < 128);
12703 : :
12704 : : #define TYPES \
12705 : : DO_TYPE (BYTE, Byte, "0x%.2" PRIx8, "%" PRId8); \
12706 : : DO_TYPE (HALF, Half, "0x%.4" PRIx16, "%" PRId16); \
12707 : : DO_TYPE (WORD, Word, "0x%.8" PRIx32, "%" PRId32); \
12708 : : DO_TYPE (SWORD, Sword, "%" PRId32, "%" PRId32); \
12709 : : DO_TYPE (XWORD, Xword, "0x%.16" PRIx64, "%" PRId64); \
12710 : : DO_TYPE (SXWORD, Sxword, "%" PRId64, "%" PRId64)
12711 : :
12712 : : #define DO_TYPE(NAME, Name, hex, dec) GElf_##Name Name
12713 : 800 : typedef union { TYPES; } value_t;
12714 : 800 : void *data = alloca (count * sizeof (value_t));
12715 : : #undef DO_TYPE
12716 : :
12717 : : #define DO_TYPE(NAME, Name, hex, dec) \
12718 : : GElf_##Name *value_##Name __attribute__((unused)) = data
12719 : 800 : TYPES;
12720 : : #undef DO_TYPE
12721 : :
12722 : 800 : size_t size = gelf_fsize (core, item->type, count, EV_CURRENT);
12723 : 800 : size_t convsize = size;
12724 [ + + ]: 800 : if (repeated_size != NULL)
12725 : : {
12726 [ + - - + ]: 2 : if (*repeated_size > size && (item->format == 'b' || item->format == 'B'))
12727 : : {
12728 : 0 : data = alloca (*repeated_size);
12729 : 0 : count *= *repeated_size / size;
12730 : 0 : convsize = count * size;
12731 : 0 : *repeated_size -= convsize;
12732 : : }
12733 [ + - + - ]: 2 : else if (item->count != 0 || item->format != '\n')
12734 : 0 : *repeated_size -= size;
12735 : : }
12736 : :
12737 : 800 : convert (core, item->type, count, data, desc + item->offset, convsize);
12738 : :
12739 : 800 : Elf_Type type = item->type;
12740 [ + + ]: 800 : if (type == ELF_T_ADDR)
12741 [ + - ]: 2 : type = gelf_getclass (core) == ELFCLASS32 ? ELF_T_WORD : ELF_T_XWORD;
12742 : :
12743 [ + + + + : 800 : switch (item->format)
+ + + +
- ]
12744 : : {
12745 : 386 : case 'd':
12746 [ - + ]: 386 : assert (count == 1);
12747 [ + + + + : 386 : switch (type)
- + - ]
12748 : : {
12749 : : #define DO_TYPE(NAME, Name, hex, dec) \
12750 : : case ELF_T_##NAME: \
12751 : : colno = print_core_item (colno, ',', WRAP_COLUMN, \
12752 : : 0, item->name, dec, value_##Name[0]); \
12753 : : break
12754 : 386 : TYPES;
12755 : : #undef DO_TYPE
12756 : 0 : default:
12757 : 0 : abort ();
12758 : : }
12759 : : break;
12760 : :
12761 : 222 : case 'x':
12762 [ - + ]: 222 : assert (count == 1);
12763 [ - - + - : 222 : switch (type)
+ - - ]
12764 : : {
12765 : : #define DO_TYPE(NAME, Name, hex, dec) \
12766 : : case ELF_T_##NAME: \
12767 : : colno = print_core_item (colno, ',', WRAP_COLUMN, \
12768 : : 0, item->name, hex, value_##Name[0]); \
12769 : : break
12770 : 222 : TYPES;
12771 : : #undef DO_TYPE
12772 : 0 : default:
12773 : 0 : abort ();
12774 : : }
12775 : : break;
12776 : :
12777 : 44 : case 'b':
12778 : : case 'B':
12779 [ - + ]: 44 : assert (size % sizeof (unsigned int) == 0);
12780 : 44 : unsigned int nbits = count * size * 8;
12781 : 44 : unsigned int pop = 0;
12782 [ + + ]: 112 : for (const unsigned int *i = data; (void *) i < data + count * size; ++i)
12783 : 68 : pop += __builtin_popcount (*i);
12784 : 44 : bool negate = pop > nbits / 2;
12785 : 44 : const unsigned int bias = item->format == 'b';
12786 : :
12787 : 44 : {
12788 [ - + ]: 44 : char printed[(negate ? nbits - pop : pop) * 16 + 1];
12789 : 44 : char *p = printed;
12790 : 44 : *p = '\0';
12791 : :
12792 : 44 : if (BYTE_ORDER != LITTLE_ENDIAN && size > sizeof (unsigned int))
12793 : : {
12794 : : assert (size == sizeof (unsigned int) * 2);
12795 : : for (unsigned int *i = data;
12796 : : (void *) i < data + count * size; i += 2)
12797 : : {
12798 : : unsigned int w = i[1];
12799 : : i[1] = i[0];
12800 : : i[0] = w;
12801 : : }
12802 : : }
12803 : :
12804 : 44 : unsigned int lastbit = 0;
12805 : 44 : unsigned int run = 0;
12806 : 44 : for (const unsigned int *i = data;
12807 [ + + ]: 112 : (void *) i < data + count * size; ++i)
12808 : : {
12809 : 68 : unsigned int bit = ((void *) i - data) * 8;
12810 [ - + ]: 68 : unsigned int w = negate ? ~*i : *i;
12811 [ - + ]: 68 : while (w != 0)
12812 : : {
12813 : : /* Note that a right shift equal to (or greater than)
12814 : : the number of bits of w is undefined behaviour. In
12815 : : particular when the least significant bit is bit 32
12816 : : (w = 0x8000000) then w >>= n is undefined. So
12817 : : explicitly handle that case separately. */
12818 : 0 : unsigned int n = ffs (w);
12819 [ # # ]: 0 : if (n < sizeof (w) * 8)
12820 : 0 : w >>= n;
12821 : : else
12822 : : w = 0;
12823 : 0 : bit += n;
12824 : :
12825 [ # # # # ]: 0 : if (lastbit != 0 && lastbit + 1 == bit)
12826 : 0 : ++run;
12827 : : else
12828 : : {
12829 : 0 : if (lastbit == 0)
12830 : 0 : p += sprintf (p, "%u", bit - bias);
12831 [ # # ]: 0 : else if (run == 0)
12832 : 0 : p += sprintf (p, ",%u", bit - bias);
12833 : : else
12834 : 0 : p += sprintf (p, "-%u,%u", lastbit - bias, bit - bias);
12835 : : run = 0;
12836 : : }
12837 : :
12838 : : lastbit = bit;
12839 : : }
12840 : : }
12841 [ - + - - ]: 44 : if (lastbit > 0 && run > 0 && lastbit + 1 != nbits)
12842 : 0 : p += sprintf (p, "-%u", lastbit - bias);
12843 : :
12844 [ + - ]: 88 : colno = print_core_item (colno, ',', WRAP_COLUMN, 0, item->name,
12845 : : negate ? "~<%s>" : "<%s>", printed);
12846 : : }
12847 : 44 : break;
12848 : :
12849 : 88 : case 'T':
12850 : : case (char) ('T'|0x80):
12851 [ - + ]: 88 : assert (count == 2);
12852 : 88 : Dwarf_Word sec;
12853 : 88 : Dwarf_Word usec;
12854 [ - - + - : 88 : switch (type)
+ - - ]
12855 : : {
12856 : : #define DO_TYPE(NAME, Name, hex, dec) \
12857 : : case ELF_T_##NAME: \
12858 : : sec = value_##Name[0]; \
12859 : : usec = value_##Name[1]; \
12860 : : break
12861 : 88 : TYPES;
12862 : : #undef DO_TYPE
12863 : 0 : default:
12864 : 0 : abort ();
12865 : : }
12866 [ - + ]: 88 : if (unlikely (item->format == (char) ('T'|0x80)))
12867 : : {
12868 : : /* This is a hack for an ill-considered 64-bit ABI where
12869 : : tv_usec is actually a 32-bit field with 32 bits of padding
12870 : : rounding out struct timeval. We've already converted it as
12871 : : a 64-bit field. For little-endian, this just means the
12872 : : high half is the padding; it's presumably zero, but should
12873 : : be ignored anyway. For big-endian, it means the 32-bit
12874 : : field went into the high half of USEC. */
12875 [ # # ]: 0 : if (likely (ehdr->e_ident[EI_DATA] == ELFDATA2MSB))
12876 : 0 : usec >>= 32;
12877 : : else
12878 : 0 : usec &= UINT32_MAX;
12879 : : }
12880 : 88 : colno = print_core_item (colno, ',', WRAP_COLUMN, 0, item->name,
12881 : : "%" PRIu64 ".%.6" PRIu64, sec, usec);
12882 : 88 : break;
12883 : :
12884 : 18 : case 'c':
12885 [ - + ]: 18 : assert (count == 1);
12886 : 18 : colno = print_core_item (colno, ',', WRAP_COLUMN, 0, item->name,
12887 : 18 : "%c", value_Byte[0]);
12888 : 18 : break;
12889 : :
12890 : 36 : case 's':
12891 : 36 : colno = print_core_item (colno, ',', WRAP_COLUMN, 0, item->name,
12892 : : "%.*s", (int) count, value_Byte);
12893 : 36 : break;
12894 : :
12895 : 2 : case '\n':
12896 : : /* This is a list of strings separated by '\n'. */
12897 [ - + ]: 2 : assert (item->count == 0);
12898 [ - + ]: 2 : assert (repeated_size != NULL);
12899 [ - + ]: 2 : assert (item->name == NULL);
12900 [ - + ]: 2 : if (unlikely (item->offset >= *repeated_size))
12901 : : break;
12902 : :
12903 : 2 : const char *s = desc + item->offset;
12904 : 2 : size = *repeated_size - item->offset;
12905 : 2 : *repeated_size = 0;
12906 [ + - ]: 96 : while (size > 0)
12907 : : {
12908 : 96 : const char *eol = memchr (s, '\n', size);
12909 : 96 : int len = size;
12910 [ + + ]: 96 : if (eol != NULL)
12911 : 94 : len = eol - s;
12912 : 96 : printf ("%*s%.*s\n", ITEM_INDENT, "", len, s);
12913 [ + + ]: 96 : if (eol == NULL)
12914 : : break;
12915 : 94 : size -= eol + 1 - s;
12916 : 94 : s = eol + 1;
12917 : : }
12918 : :
12919 : : colno = WRAP_COLUMN;
12920 : : break;
12921 : :
12922 : : case 'h':
12923 : : break;
12924 : :
12925 : 0 : default:
12926 : 0 : error (0, 0, "XXX not handling format '%c' for %s",
12927 : 0 : item->format, item->name);
12928 : 0 : break;
12929 : : }
12930 : :
12931 : : #undef TYPES
12932 : :
12933 : 800 : return colno;
12934 : : }
12935 : :
12936 : :
12937 : : /* Sort items by group, and by layout offset within each group. */
12938 : : static int
12939 : 1790 : compare_core_items (const void *a, const void *b)
12940 : : {
12941 : 1790 : const Ebl_Core_Item *const *p1 = a;
12942 : 1790 : const Ebl_Core_Item *const *p2 = b;
12943 : 1790 : const Ebl_Core_Item *item1 = *p1;
12944 : 1790 : const Ebl_Core_Item *item2 = *p2;
12945 : :
12946 : 1790 : return ((item1->group == item2->group ? 0
12947 [ + + ]: 1790 : : strcmp (item1->group, item2->group))
12948 [ - + ]: 1790 : ?: (int) item1->offset - (int) item2->offset);
12949 : : }
12950 : :
12951 : : /* Sort item groups by layout offset of the first item in the group. */
12952 : : static int
12953 : 278 : compare_core_item_groups (const void *a, const void *b)
12954 : : {
12955 : 278 : const Ebl_Core_Item *const *const *p1 = a;
12956 : 278 : const Ebl_Core_Item *const *const *p2 = b;
12957 : 278 : const Ebl_Core_Item *const *group1 = *p1;
12958 : 278 : const Ebl_Core_Item *const *group2 = *p2;
12959 : 278 : const Ebl_Core_Item *item1 = *group1;
12960 : 278 : const Ebl_Core_Item *item2 = *group2;
12961 : :
12962 : 278 : return (int) item1->offset - (int) item2->offset;
12963 : : }
12964 : :
12965 : : static unsigned int
12966 : 74 : handle_core_items (Elf *core, const GElf_Ehdr *ehdr,
12967 : : const void *desc, size_t descsz,
12968 : : const Ebl_Core_Item *items, size_t nitems)
12969 : 74 : {
12970 [ + + ]: 74 : if (nitems == 0)
12971 : : return 0;
12972 : 68 : unsigned int colno = 0;
12973 : :
12974 : : /* FORMAT '\n' makes sense to be present only as a single item as it
12975 : : processes all the data of a note. FORMATs 'b' and 'B' have a special case
12976 : : if present as a single item but they can be also processed with other
12977 : : items below. */
12978 [ + + + + ]: 68 : if (nitems == 1 && (items[0].format == '\n' || items[0].format == 'b'
12979 [ + - ]: 16 : || items[0].format == 'B'))
12980 : : {
12981 [ - + ]: 2 : assert (items[0].offset == 0);
12982 : 2 : size_t size = descsz;
12983 : 2 : colno = handle_core_item (core, ehdr, items, desc, colno, &size);
12984 : : /* If SIZE is not zero here there is some remaining data. But we do not
12985 : : know how to process it anyway. */
12986 : 2 : return colno;
12987 : : }
12988 [ + + ]: 848 : for (size_t i = 0; i < nitems; ++i)
12989 [ - + ]: 782 : assert (items[i].format != '\n');
12990 : :
12991 : : /* Sort to collect the groups together. */
12992 : 66 : const Ebl_Core_Item *sorted_items[nitems];
12993 [ + + ]: 848 : for (size_t i = 0; i < nitems; ++i)
12994 : 782 : sorted_items[i] = &items[i];
12995 : 66 : qsort (sorted_items, nitems, sizeof sorted_items[0], &compare_core_items);
12996 : :
12997 : : /* Collect the unique groups and sort them. */
12998 : 66 : const Ebl_Core_Item **groups[nitems];
12999 : 66 : groups[0] = &sorted_items[0];
13000 : 66 : size_t ngroups = 1;
13001 [ + + ]: 782 : for (size_t i = 1; i < nitems; ++i)
13002 [ + + ]: 716 : if (sorted_items[i]->group != sorted_items[i - 1]->group
13003 [ + - ]: 150 : && strcmp (sorted_items[i]->group, sorted_items[i - 1]->group))
13004 : 150 : groups[ngroups++] = &sorted_items[i];
13005 : 66 : qsort (groups, ngroups, sizeof groups[0], &compare_core_item_groups);
13006 : :
13007 : : /* Write out all the groups. */
13008 : 66 : const void *last = desc;
13009 : 70 : do
13010 : : {
13011 [ + + ]: 290 : for (size_t i = 0; i < ngroups; ++i)
13012 : : {
13013 : 220 : for (const Ebl_Core_Item **item = groups[i];
13014 : 1018 : (item < &sorted_items[nitems]
13015 [ + + + + ]: 1018 : && ((*item)->group == groups[i][0]->group
13016 [ - + ]: 150 : || !strcmp ((*item)->group, groups[i][0]->group)));
13017 : 798 : ++item)
13018 : 798 : colno = handle_core_item (core, ehdr, *item, desc, colno, NULL);
13019 : :
13020 : : /* Force a line break at the end of the group. */
13021 : 220 : colno = WRAP_COLUMN;
13022 : : }
13023 : :
13024 [ + + ]: 70 : if (descsz == 0)
13025 : : break;
13026 : :
13027 : : /* This set of items consumed a certain amount of the note's data.
13028 : : If there is more data there, we have another unit of the same size.
13029 : : Loop to print that out too. */
13030 : 40 : const Ebl_Core_Item *item = &items[nitems - 1];
13031 : 80 : size_t eltsz = item->offset + gelf_fsize (core, item->type,
13032 : 40 : item->count ?: 1, EV_CURRENT);
13033 : :
13034 : 40 : int reps = -1;
13035 : 40 : do
13036 : : {
13037 : 40 : ++reps;
13038 : 40 : desc += eltsz;
13039 : 40 : descsz -= eltsz;
13040 : : }
13041 [ + + - + ]: 40 : while (descsz >= eltsz && !memcmp (desc, last, eltsz));
13042 : :
13043 [ + - ]: 40 : if (reps == 1)
13044 : : {
13045 : : /* For just one repeat, print it unabridged twice. */
13046 : : desc -= eltsz;
13047 : : descsz += eltsz;
13048 : : }
13049 [ + - ]: 40 : else if (reps > 1)
13050 : 0 : printf (_("\n%*s... <repeats %u more times> ..."),
13051 : : ITEM_INDENT, "", reps);
13052 : :
13053 : 40 : last = desc;
13054 : : }
13055 [ + + ]: 40 : while (descsz > 0);
13056 : :
13057 : : return colno;
13058 : : }
13059 : :
13060 : : static unsigned int
13061 : 324 : handle_core_register (Ebl *ebl, Elf *core, int maxregname,
13062 : : const Ebl_Register_Location *regloc, const void *desc,
13063 : : unsigned int colno)
13064 : : {
13065 [ - + ]: 324 : if (regloc->bits % 8 != 0)
13066 : : {
13067 : 0 : error (0, 0, "Warning: Cannot handle register with %" PRIu8 "bits\n",
13068 : : regloc->bits);
13069 : 0 : return colno;
13070 : : }
13071 : :
13072 : 324 : desc += regloc->offset;
13073 : :
13074 [ + + ]: 1172 : for (int reg = regloc->regno; reg < regloc->regno + regloc->count; ++reg)
13075 : : {
13076 : 848 : char name[REGNAMESZ];
13077 : 848 : int bits;
13078 : 848 : int type;
13079 : 848 : register_info (ebl, reg, regloc, name, &bits, &type);
13080 : :
13081 : : #define TYPES \
13082 : : BITS (8, BYTE, "%4" PRId8, "0x%.2" PRIx8); \
13083 : : BITS (16, HALF, "%6" PRId16, "0x%.4" PRIx16); \
13084 : : BITS (32, WORD, "%11" PRId32, " 0x%.8" PRIx32); \
13085 : : BITS (64, XWORD, "%20" PRId64, " 0x%.16" PRIx64)
13086 : :
13087 : : #define BITS(bits, xtype, sfmt, ufmt) \
13088 : : uint##bits##_t b##bits; int##bits##_t b##bits##s
13089 : 848 : union { TYPES; uint64_t b128[2]; } value;
13090 : : #undef BITS
13091 : :
13092 [ + + ]: 848 : switch (type)
13093 : : {
13094 : 672 : case DW_ATE_unsigned:
13095 : : case DW_ATE_signed:
13096 : : case DW_ATE_address:
13097 [ - + + + : 672 : switch (bits)
+ - ]
13098 : : {
13099 : : #define BITS(bits, xtype, sfmt, ufmt) \
13100 : : case bits: \
13101 : : desc = convert (core, ELF_T_##xtype, 1, &value, desc, 0); \
13102 : : if (type == DW_ATE_signed) \
13103 : : colno = print_core_item (colno, ' ', WRAP_COLUMN, \
13104 : : maxregname, name, \
13105 : : sfmt, value.b##bits##s); \
13106 : : else \
13107 : : colno = print_core_item (colno, ' ', WRAP_COLUMN, \
13108 : : maxregname, name, \
13109 : : ufmt, value.b##bits); \
13110 : : break
13111 : :
13112 [ - - - + : 576 : TYPES;
+ + + + ]
13113 : :
13114 : 96 : case 128:
13115 [ - + ]: 96 : assert (type == DW_ATE_unsigned);
13116 : 96 : desc = convert (core, ELF_T_XWORD, 2, &value, desc, 0);
13117 : 96 : int be = elf_getident (core, NULL)[EI_DATA] == ELFDATA2MSB;
13118 : 96 : colno = print_core_item (colno, ' ', WRAP_COLUMN,
13119 : : maxregname, name,
13120 : : "0x%.16" PRIx64 "%.16" PRIx64,
13121 : 96 : value.b128[!be], value.b128[be]);
13122 : 96 : break;
13123 : :
13124 : 0 : default:
13125 : 0 : abort ();
13126 : : #undef BITS
13127 : : }
13128 : : break;
13129 : :
13130 : 176 : default:
13131 : : /* Print each byte in hex, the whole thing in native byte order. */
13132 [ - + ]: 176 : assert (bits % 8 == 0);
13133 : 176 : const uint8_t *bytes = desc;
13134 : 176 : desc += bits / 8;
13135 : 176 : char hex[bits / 4 + 1];
13136 : 176 : hex[bits / 4] = '\0';
13137 : 176 : int incr = 1;
13138 [ + + ]: 176 : if (elf_getident (core, NULL)[EI_DATA] == ELFDATA2LSB)
13139 : : {
13140 : 96 : bytes += bits / 8 - 1;
13141 : 96 : incr = -1;
13142 : : }
13143 : 176 : size_t idx = 0;
13144 [ + + ]: 1744 : for (char *h = hex; bits > 0; bits -= 8, idx += incr)
13145 : : {
13146 : 1568 : *h++ = "0123456789abcdef"[bytes[idx] >> 4];
13147 : 1568 : *h++ = "0123456789abcdef"[bytes[idx] & 0xf];
13148 : : }
13149 : 176 : colno = print_core_item (colno, ' ', WRAP_COLUMN,
13150 : : maxregname, name, "0x%s", hex);
13151 : 176 : break;
13152 : : }
13153 : 848 : desc += regloc->pad;
13154 : :
13155 : : #undef TYPES
13156 : : }
13157 : :
13158 : : return colno;
13159 : : }
13160 : :
13161 : :
13162 : : struct register_info
13163 : : {
13164 : : const Ebl_Register_Location *regloc;
13165 : : const char *set;
13166 : : char name[REGNAMESZ];
13167 : : int regno;
13168 : : int bits;
13169 : : int type;
13170 : : };
13171 : :
13172 : : static int
13173 : 4132 : register_bitpos (const struct register_info *r)
13174 : : {
13175 : 4132 : return (r->regloc->offset * 8
13176 : 4132 : + ((r->regno - r->regloc->regno)
13177 : 4132 : * (r->regloc->bits + r->regloc->pad * 8)));
13178 : : }
13179 : :
13180 : : static int
13181 : 2124 : compare_sets_by_info (const struct register_info *r1,
13182 : : const struct register_info *r2)
13183 : : {
13184 : 2124 : return ((int) r2->bits - (int) r1->bits
13185 [ + + ]: 2124 : ?: register_bitpos (r1) - register_bitpos (r2));
13186 : : }
13187 : :
13188 : : /* Sort registers by set, and by size and layout offset within each set. */
13189 : : static int
13190 : 9190 : compare_registers (const void *a, const void *b)
13191 : : {
13192 : 9190 : const struct register_info *r1 = a;
13193 : 9190 : const struct register_info *r2 = b;
13194 : :
13195 : : /* Unused elements sort last. */
13196 [ + + ]: 9190 : if (r1->regloc == NULL)
13197 : 6618 : return r2->regloc == NULL ? 0 : 1;
13198 [ + + ]: 2572 : if (r2->regloc == NULL)
13199 : : return -1;
13200 : :
13201 [ + + ]: 2242 : return ((r1->set == r2->set ? 0 : strcmp (r1->set, r2->set))
13202 [ - + ]: 2242 : ?: compare_sets_by_info (r1, r2));
13203 : : }
13204 : :
13205 : : /* Sort register sets by layout offset of the first register in the set. */
13206 : : static int
13207 : 40 : compare_register_sets (const void *a, const void *b)
13208 : : {
13209 : 40 : const struct register_info *const *p1 = a;
13210 : 40 : const struct register_info *const *p2 = b;
13211 : 40 : return compare_sets_by_info (*p1, *p2);
13212 : : }
13213 : :
13214 : : static inline bool
13215 : 1728 : same_set (const struct register_info *a,
13216 : : const struct register_info *b,
13217 : : const struct register_info *regs,
13218 : : size_t maxnreg)
13219 : : {
13220 [ + - ]: 1728 : return (a < ®s[maxnreg] && a->regloc != NULL
13221 [ + - + + ]: 1728 : && b < ®s[maxnreg] && b->regloc != NULL
13222 [ + + ]: 1692 : && a->bits == b->bits
13223 [ + - + + : 3384 : && (a->set == b->set || !strcmp (a->set, b->set)));
- + ]
13224 : : }
13225 : :
13226 : : static unsigned int
13227 : 74 : handle_core_registers (Ebl *ebl, Elf *core, const void *desc,
13228 : : const Ebl_Register_Location *reglocs, size_t nregloc)
13229 : 74 : {
13230 [ + + ]: 74 : if (nregloc == 0)
13231 : : return 0;
13232 : :
13233 : 36 : ssize_t maxnreg = ebl_register_info (ebl, 0, NULL, 0, NULL, NULL, NULL, NULL);
13234 [ - + ]: 36 : if (maxnreg <= 0)
13235 : : {
13236 [ # # ]: 0 : for (size_t i = 0; i < nregloc; ++i)
13237 : 0 : if (maxnreg < reglocs[i].regno + reglocs[i].count)
13238 : : maxnreg = reglocs[i].regno + reglocs[i].count;
13239 [ # # ]: 0 : assert (maxnreg > 0);
13240 : : }
13241 : :
13242 : 36 : struct register_info regs[maxnreg];
13243 : 36 : memset (regs, 0, sizeof regs);
13244 : :
13245 : : /* Sort to collect the sets together. */
13246 : 36 : int maxreg = 0;
13247 [ + + ]: 360 : for (size_t i = 0; i < nregloc; ++i)
13248 : 324 : for (int reg = reglocs[i].regno;
13249 [ + + ]: 1172 : reg < reglocs[i].regno + reglocs[i].count;
13250 : 848 : ++reg)
13251 : : {
13252 [ - + ]: 848 : assert (reg < maxnreg);
13253 : 848 : if (reg > maxreg)
13254 : : maxreg = reg;
13255 : 848 : struct register_info *info = ®s[reg];
13256 : 848 : info->regloc = ®locs[i];
13257 : 848 : info->regno = reg;
13258 : 848 : info->set = register_info (ebl, reg, ®locs[i],
13259 : 848 : info->name, &info->bits, &info->type);
13260 : : }
13261 : 36 : qsort (regs, maxreg + 1, sizeof regs[0], &compare_registers);
13262 : :
13263 : : /* Collect the unique sets and sort them. */
13264 : 36 : struct register_info *sets[maxreg + 1];
13265 : 36 : sets[0] = ®s[0];
13266 : 36 : size_t nsets = 1;
13267 [ + + ]: 2558 : for (int i = 1; i <= maxreg; ++i)
13268 [ + + ]: 2522 : if (regs[i].regloc != NULL
13269 [ + + ]: 812 : && !same_set (®s[i], ®s[i - 1], regs, maxnreg))
13270 : 32 : sets[nsets++] = ®s[i];
13271 : 36 : qsort (sets, nsets, sizeof sets[0], &compare_register_sets);
13272 : :
13273 : : /* Write out all the sets. */
13274 : 36 : unsigned int colno = 0;
13275 [ + + ]: 104 : for (size_t i = 0; i < nsets; ++i)
13276 : : {
13277 : : /* Find the longest name of a register in this set. */
13278 : 68 : size_t maxname = 0;
13279 : 68 : const struct register_info *end;
13280 [ + + ]: 916 : for (end = sets[i]; same_set (sets[i], end, regs, maxnreg); ++end)
13281 : : {
13282 : 848 : size_t len = strlen (end->name);
13283 : 848 : if (len > maxname)
13284 : : maxname = len;
13285 : : }
13286 : :
13287 : : for (const struct register_info *reg = sets[i];
13288 [ + + ]: 392 : reg < end;
13289 : 324 : reg += reg->regloc->count ?: 1)
13290 [ + - ]: 324 : colno = handle_core_register (ebl, core, maxname,
13291 : 324 : reg->regloc, desc, colno);
13292 : :
13293 : : /* Force a line break at the end of the group. */
13294 : 68 : colno = WRAP_COLUMN;
13295 : : }
13296 : :
13297 : : return colno;
13298 : : }
13299 : :
13300 : : static void
13301 : 18 : handle_auxv_note (Ebl *ebl, Elf *core, GElf_Word descsz, GElf_Off desc_pos)
13302 : : {
13303 : 18 : Elf_Data *data = elf_getdata_rawchunk (core, desc_pos, descsz, ELF_T_AUXV);
13304 [ - + ]: 18 : if (data == NULL)
13305 : 0 : elf_error:
13306 : 0 : error_exit (0, _("cannot convert core note data: %s"), elf_errmsg (-1));
13307 : :
13308 : 18 : const size_t nauxv = descsz / gelf_fsize (core, ELF_T_AUXV, 1, EV_CURRENT);
13309 [ + + ]: 354 : for (size_t i = 0; i < nauxv; ++i)
13310 : : {
13311 : 336 : GElf_auxv_t av_mem;
13312 : 336 : GElf_auxv_t *av = gelf_getauxv (data, i, &av_mem);
13313 [ - + ]: 336 : if (av == NULL)
13314 : 0 : goto elf_error;
13315 : :
13316 : 336 : const char *name;
13317 : 336 : const char *fmt;
13318 [ - + ]: 336 : if (ebl_auxv_info (ebl, av->a_type, &name, &fmt) == 0)
13319 : : {
13320 : : /* Unknown type. */
13321 [ # # ]: 0 : if (av->a_un.a_val == 0)
13322 : 0 : printf (" %" PRIu64 "\n", av->a_type);
13323 : : else
13324 : 0 : printf (" %" PRIu64 ": %#" PRIx64 "\n",
13325 : : av->a_type, av->a_un.a_val);
13326 : : }
13327 : : else
13328 [ + + + - : 336 : switch (fmt[0])
+ - ]
13329 : : {
13330 : 18 : case '\0': /* Normally zero. */
13331 [ + - ]: 18 : if (av->a_un.a_val == 0)
13332 : : {
13333 : 18 : printf (" %s\n", name);
13334 : 18 : break;
13335 : : }
13336 : 148 : FALLTHROUGH;
13337 : : case 'x': /* hex */
13338 : : case 'p': /* address */
13339 : : case 's': /* address of string */
13340 : 148 : printf (" %s: %#" PRIx64 "\n", name, av->a_un.a_val);
13341 : 148 : break;
13342 : 162 : case 'u':
13343 : 162 : printf (" %s: %" PRIu64 "\n", name, av->a_un.a_val);
13344 : 162 : break;
13345 : 0 : case 'd':
13346 : 0 : printf (" %s: %" PRId64 "\n", name, av->a_un.a_val);
13347 : 0 : break;
13348 : :
13349 : 8 : case 'b':
13350 : 8 : printf (" %s: %#" PRIx64 " ", name, av->a_un.a_val);
13351 : 8 : GElf_Xword bit = 1;
13352 : 8 : const char *pfx = "<";
13353 [ + + ]: 220 : for (const char *p = fmt + 1; *p != 0; p = strchr (p, '\0') + 1)
13354 : : {
13355 [ + + ]: 212 : if (av->a_un.a_val & bit)
13356 : : {
13357 : 154 : printf ("%s%s", pfx, p);
13358 : 154 : pfx = " ";
13359 : : }
13360 : 212 : bit <<= 1;
13361 : : }
13362 : 8 : printf (">\n");
13363 : 8 : break;
13364 : :
13365 : 0 : default:
13366 : 0 : abort ();
13367 : : }
13368 : : }
13369 : 18 : }
13370 : :
13371 : : static bool
13372 : 390 : buf_has_data (unsigned char const *ptr, unsigned char const *end, size_t sz)
13373 : : {
13374 [ + - + - ]: 390 : return ptr < end && (size_t) (end - ptr) >= sz;
13375 : : }
13376 : :
13377 : : static bool
13378 : 36 : buf_read_int (Elf *core, unsigned char const **ptrp, unsigned char const *end,
13379 : : int *retp)
13380 : : {
13381 [ + - ]: 36 : if (! buf_has_data (*ptrp, end, 4))
13382 : : return false;
13383 : :
13384 : 36 : *ptrp = convert (core, ELF_T_WORD, 1, retp, *ptrp, 4);
13385 : 36 : return true;
13386 : : }
13387 : :
13388 : : static bool
13389 : 354 : buf_read_ulong (Elf *core, unsigned char const **ptrp, unsigned char const *end,
13390 : : uint64_t *retp)
13391 : : {
13392 : 354 : size_t sz = gelf_fsize (core, ELF_T_ADDR, 1, EV_CURRENT);
13393 [ + - ]: 354 : if (! buf_has_data (*ptrp, end, sz))
13394 : : return false;
13395 : :
13396 : 354 : union
13397 : : {
13398 : : uint64_t u64;
13399 : : uint32_t u32;
13400 : : } u;
13401 : :
13402 : 354 : *ptrp = convert (core, ELF_T_ADDR, 1, &u, *ptrp, sz);
13403 : :
13404 [ + + ]: 354 : if (sz == 4)
13405 : 186 : *retp = u.u32;
13406 : : else
13407 : 168 : *retp = u.u64;
13408 : : return true;
13409 : : }
13410 : :
13411 : : static void
13412 : 12 : handle_siginfo_note (Elf *core, GElf_Word descsz, GElf_Off desc_pos)
13413 : : {
13414 : 12 : Elf_Data *data = elf_getdata_rawchunk (core, desc_pos, descsz, ELF_T_BYTE);
13415 [ - + ]: 12 : if (data == NULL)
13416 : 0 : error_exit (0, _("cannot convert core note data: %s"), elf_errmsg (-1));
13417 : :
13418 : 12 : unsigned char const *ptr = data->d_buf;
13419 : 12 : unsigned char const *const end = data->d_buf + data->d_size;
13420 : :
13421 : : /* Siginfo head is three ints: signal number, error number, origin
13422 : : code. */
13423 : 12 : int si_signo, si_errno, si_code;
13424 [ - + ]: 12 : if (! buf_read_int (core, &ptr, end, &si_signo)
13425 [ - + ]: 12 : || ! buf_read_int (core, &ptr, end, &si_errno)
13426 [ - + ]: 12 : || ! buf_read_int (core, &ptr, end, &si_code))
13427 : : {
13428 : 0 : fail:
13429 : 0 : printf (" Not enough data in NT_SIGINFO note.\n");
13430 : 0 : return;
13431 : : }
13432 : :
13433 : : /* Next is a pointer-aligned union of structures. On 64-bit
13434 : : machines, that implies a word of padding. */
13435 [ + + ]: 12 : if (gelf_getclass (core) == ELFCLASS64)
13436 : 6 : ptr += 4;
13437 : :
13438 : 12 : printf (" si_signo: %d, si_errno: %d, si_code: %d\n",
13439 : : si_signo, si_errno, si_code);
13440 : :
13441 [ + - ]: 12 : if (si_code > 0)
13442 [ + - ]: 12 : switch (si_signo)
13443 : : {
13444 : 12 : case CORE_SIGILL:
13445 : : case CORE_SIGFPE:
13446 : : case CORE_SIGSEGV:
13447 : : case CORE_SIGBUS:
13448 : : {
13449 : 12 : uint64_t addr;
13450 [ - + ]: 12 : if (! buf_read_ulong (core, &ptr, end, &addr))
13451 : 0 : goto fail;
13452 : 12 : printf (" fault address: %#" PRIx64 "\n", addr);
13453 : 12 : break;
13454 : : }
13455 : : default:
13456 : : ;
13457 : : }
13458 [ # # ]: 0 : else if (si_code == CORE_SI_USER)
13459 : : {
13460 : 0 : int pid, uid;
13461 [ # # ]: 0 : if (! buf_read_int (core, &ptr, end, &pid)
13462 [ # # ]: 0 : || ! buf_read_int (core, &ptr, end, &uid))
13463 : 0 : goto fail;
13464 : 0 : printf (" sender PID: %d, sender UID: %d\n", pid, uid);
13465 : : }
13466 : : }
13467 : :
13468 : : static void
13469 : 12 : handle_file_note (Elf *core, GElf_Word descsz, GElf_Off desc_pos)
13470 : : {
13471 : 12 : Elf_Data *data = elf_getdata_rawchunk (core, desc_pos, descsz, ELF_T_BYTE);
13472 [ - + ]: 12 : if (data == NULL)
13473 : 0 : error_exit (0, _("cannot convert core note data: %s"), elf_errmsg (-1));
13474 : :
13475 : 12 : unsigned char const *ptr = data->d_buf;
13476 : 12 : unsigned char const *const end = data->d_buf + data->d_size;
13477 : :
13478 : 12 : uint64_t count, page_size;
13479 [ - + ]: 12 : if (! buf_read_ulong (core, &ptr, end, &count)
13480 [ - + ]: 12 : || ! buf_read_ulong (core, &ptr, end, &page_size))
13481 : : {
13482 : 0 : fail:
13483 : 0 : printf (" Not enough data in NT_FILE note.\n");
13484 : 0 : return;
13485 : : }
13486 : :
13487 : 12 : size_t addrsize = gelf_fsize (core, ELF_T_ADDR, 1, EV_CURRENT);
13488 : 12 : uint64_t maxcount = (size_t) (end - ptr) / (3 * addrsize);
13489 [ - + ]: 12 : if (count > maxcount)
13490 : 0 : goto fail;
13491 : :
13492 : : /* Where file names are stored. */
13493 : 12 : unsigned char const *const fstart = ptr + 3 * count * addrsize;
13494 : 12 : char const *fptr = (char *) fstart;
13495 : :
13496 : 12 : printf (" %" PRId64 " files:\n", count);
13497 [ + + ]: 118 : for (uint64_t i = 0; i < count; ++i)
13498 : : {
13499 : 106 : uint64_t mstart, mend, moffset;
13500 [ + - ]: 106 : if (! buf_read_ulong (core, &ptr, fstart, &mstart)
13501 [ + - ]: 106 : || ! buf_read_ulong (core, &ptr, fstart, &mend)
13502 [ - + ]: 106 : || ! buf_read_ulong (core, &ptr, fstart, &moffset))
13503 : 0 : goto fail;
13504 : :
13505 : 106 : const char *fnext = memchr (fptr, '\0', (char *) end - fptr);
13506 [ - + ]: 106 : if (fnext == NULL)
13507 : 0 : goto fail;
13508 : :
13509 : 106 : int ct = printf (" %08" PRIx64 "-%08" PRIx64
13510 : : " %08" PRIx64 " %" PRId64,
13511 : : mstart, mend, moffset * page_size, mend - mstart);
13512 [ + - ]: 106 : printf ("%*s%s\n", ct > 50 ? 3 : 53 - ct, "", fptr);
13513 : :
13514 : 106 : fptr = fnext + 1;
13515 : : }
13516 : : }
13517 : :
13518 : : static void
13519 : 76 : handle_core_note (Ebl *ebl, const GElf_Ehdr *ehdr, const GElf_Nhdr *nhdr,
13520 : : const char *name, const void *desc)
13521 : : {
13522 : 76 : GElf_Word regs_offset;
13523 : 76 : size_t nregloc;
13524 : 76 : const Ebl_Register_Location *reglocs;
13525 : 76 : size_t nitems;
13526 : 76 : const Ebl_Core_Item *items;
13527 : :
13528 [ + + ]: 76 : if (! ebl_core_note (ebl, nhdr, name, desc,
13529 : : ®s_offset, &nregloc, ®locs, &nitems, &items))
13530 : 2 : return;
13531 : :
13532 : : /* Pass 0 for DESCSZ when there are registers in the note,
13533 : : so that the ITEMS array does not describe the whole thing.
13534 : : For non-register notes, the actual descsz might be a multiple
13535 : : of the unit size, not just exactly the unit size. */
13536 : 74 : unsigned int colno = handle_core_items (ebl->elf, ehdr, desc,
13537 [ + + ]: 74 : nregloc == 0 ? nhdr->n_descsz : 0,
13538 : : items, nitems);
13539 [ + + ]: 74 : if (colno != 0)
13540 : 68 : putchar ('\n');
13541 : :
13542 : 74 : colno = handle_core_registers (ebl, ebl->elf, desc + regs_offset,
13543 : : reglocs, nregloc);
13544 [ + + ]: 74 : if (colno != 0)
13545 : 36 : putchar ('\n');
13546 : : }
13547 : :
13548 : : static void
13549 : 506 : handle_notes_data (Ebl *ebl, const GElf_Ehdr *ehdr,
13550 : : GElf_Off start, Elf_Data *data)
13551 : : {
13552 : 506 : fputs (_(" Owner Data size Type\n"), stdout);
13553 : :
13554 [ - + ]: 506 : if (data == NULL)
13555 : 0 : goto bad_note;
13556 : :
13557 : : size_t offset = 0;
13558 : : GElf_Nhdr nhdr;
13559 : : size_t name_offset;
13560 : : size_t desc_offset;
13561 : 5120 : while (offset < data->d_size
13562 [ + + + - ]: 5120 : && (offset = gelf_getnote (data, offset,
13563 : : &nhdr, &name_offset, &desc_offset)) > 0)
13564 : : {
13565 [ + - ]: 4614 : const char *name = nhdr.n_namesz == 0 ? "" : data->d_buf + name_offset;
13566 : 4614 : const char *desc = data->d_buf + desc_offset;
13567 : :
13568 : : /* GNU Build Attributes are weird, they store most of their data
13569 : : into the owner name field. Extract just the owner name
13570 : : prefix here, then use the rest later as data. */
13571 : 4614 : bool is_gnu_build_attr
13572 : 4614 : = startswith (name, ELF_NOTE_GNU_BUILD_ATTRIBUTE_PREFIX);
13573 : 9798 : const char *print_name = (is_gnu_build_attr
13574 [ + + ]: 4614 : ? ELF_NOTE_GNU_BUILD_ATTRIBUTE_PREFIX : name);
13575 : 5184 : size_t print_namesz = (is_gnu_build_attr
13576 : 570 : ? strlen (print_name) : nhdr.n_namesz);
13577 : :
13578 : 4614 : char buf[100];
13579 : 4614 : char buf2[100];
13580 : 4614 : printf (_(" %-13.*s %9" PRId32 " %s\n"),
13581 : : (int) print_namesz, print_name, nhdr.n_descsz,
13582 [ + + ]: 4614 : ehdr->e_type == ET_CORE
13583 : 118 : ? ebl_core_note_type_name (ebl, nhdr.n_type,
13584 : : buf, sizeof (buf))
13585 : 4496 : : ebl_object_note_type_name (ebl, name, nhdr.n_type,
13586 : : nhdr.n_descsz,
13587 : : buf2, sizeof (buf2)));
13588 : :
13589 : : /* Filter out invalid entries. */
13590 : 4614 : if (memchr (name, '\0', nhdr.n_namesz) != NULL
13591 : : /* XXX For now help broken Linux kernels. */
13592 : : || 1)
13593 : : {
13594 [ + + ]: 4614 : if (ehdr->e_type == ET_CORE)
13595 : : {
13596 [ + + ]: 118 : if (nhdr.n_type == NT_AUXV
13597 [ + - ]: 18 : && (nhdr.n_namesz == 4 /* Broken old Linux kernels. */
13598 [ + - + - ]: 18 : || (nhdr.n_namesz == 5 && name[4] == '\0'))
13599 [ + - ]: 18 : && !memcmp (name, "CORE", 4))
13600 : 18 : handle_auxv_note (ebl, ebl->elf, nhdr.n_descsz,
13601 : : start + desc_offset);
13602 [ + + + - ]: 100 : else if (nhdr.n_namesz == 5 && strcmp (name, "CORE") == 0)
13603 [ + + + ]: 76 : switch (nhdr.n_type)
13604 : : {
13605 : 12 : case NT_SIGINFO:
13606 : 12 : handle_siginfo_note (ebl->elf, nhdr.n_descsz,
13607 : : start + desc_offset);
13608 : 12 : break;
13609 : :
13610 : 12 : case NT_FILE:
13611 : 12 : handle_file_note (ebl->elf, nhdr.n_descsz,
13612 : : start + desc_offset);
13613 : 12 : break;
13614 : :
13615 : 52 : default:
13616 : 52 : handle_core_note (ebl, ehdr, &nhdr, name, desc);
13617 : : }
13618 : : else
13619 : 24 : handle_core_note (ebl, ehdr, &nhdr, name, desc);
13620 : : }
13621 : : else
13622 : 4496 : ebl_object_note (ebl, nhdr.n_namesz, name, nhdr.n_type,
13623 : : nhdr.n_descsz, desc);
13624 : : }
13625 : : }
13626 : :
13627 [ + - ]: 506 : if (offset == data->d_size)
13628 : 506 : return;
13629 : :
13630 : 0 : bad_note:
13631 : 0 : error (0, 0,
13632 : 0 : _("cannot get content of note: %s"),
13633 : 0 : data != NULL ? "garbage data" : elf_errmsg (-1));
13634 : : }
13635 : :
13636 : : static void
13637 : 230 : handle_notes (Ebl *ebl, GElf_Ehdr *ehdr)
13638 : : {
13639 : : /* If we have section headers, just look for SHT_NOTE sections.
13640 : : In a debuginfo file, the program headers are not reliable. */
13641 [ + + ]: 230 : if (shnum != 0)
13642 : : {
13643 : : /* Get the section header string table index. */
13644 : 208 : size_t shstrndx;
13645 [ - + ]: 208 : if (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0)
13646 : 0 : error_exit (0, _("cannot get section header string table index"));
13647 : :
13648 : : Elf_Scn *scn = NULL;
13649 [ + + ]: 6446 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
13650 : : {
13651 : 6238 : GElf_Shdr shdr_mem;
13652 : 6238 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
13653 : :
13654 [ + - + + ]: 6238 : if (shdr == NULL || shdr->sh_type != SHT_NOTE)
13655 : : /* Not what we are looking for. */
13656 : 5756 : continue;
13657 : :
13658 [ - + ]: 482 : if (notes_section != NULL)
13659 : : {
13660 : 0 : char *sname = elf_strptr (ebl->elf, shstrndx, shdr->sh_name);
13661 [ # # # # ]: 0 : if (sname == NULL || strcmp (sname, notes_section) != 0)
13662 : 0 : continue;
13663 : : }
13664 : :
13665 : 482 : printf (_("\
13666 : : \nNote section [%2zu] '%s' of %" PRIu64 " bytes at offset %#0" PRIx64 ":\n"),
13667 : : elf_ndxscn (scn),
13668 : 482 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
13669 : : shdr->sh_size, shdr->sh_offset);
13670 : :
13671 : 482 : handle_notes_data (ebl, ehdr, shdr->sh_offset,
13672 : : elf_getdata (scn, NULL));
13673 : : }
13674 : 208 : return;
13675 : : }
13676 : :
13677 : : /* We have to look through the program header to find the note
13678 : : sections. There can be more than one. */
13679 [ + + ]: 280 : for (size_t cnt = 0; cnt < phnum; ++cnt)
13680 : : {
13681 : 258 : GElf_Phdr mem;
13682 : 258 : GElf_Phdr *phdr = gelf_getphdr (ebl->elf, cnt, &mem);
13683 : :
13684 [ + - + + ]: 258 : if (phdr == NULL || phdr->p_type != PT_NOTE)
13685 : : /* Not what we are looking for. */
13686 : 234 : continue;
13687 : :
13688 : 24 : printf (_("\
13689 : : \nNote segment of %" PRIu64 " bytes at offset %#0" PRIx64 ":\n"),
13690 : : phdr->p_filesz, phdr->p_offset);
13691 : :
13692 : 24 : handle_notes_data (ebl, ehdr, phdr->p_offset,
13693 : : elf_getdata_rawchunk (ebl->elf,
13694 : 24 : phdr->p_offset, phdr->p_filesz,
13695 [ + + ]: 24 : (phdr->p_align == 8
13696 : : ? ELF_T_NHDR8 : ELF_T_NHDR)));
13697 : : }
13698 : : }
13699 : :
13700 : :
13701 : : static void
13702 : 12 : hex_dump (const uint8_t *data, size_t len)
13703 : : {
13704 : 12 : size_t pos = 0;
13705 [ + + ]: 56 : while (pos < len)
13706 : : {
13707 : 44 : printf (" 0x%08zx ", pos);
13708 : :
13709 : 44 : const size_t chunk = MIN (len - pos, 16);
13710 : :
13711 [ + + ]: 688 : for (size_t i = 0; i < chunk; ++i)
13712 [ + + ]: 644 : if (i % 4 == 3)
13713 : 160 : printf ("%02x ", data[pos + i]);
13714 : : else
13715 : 484 : printf ("%02x", data[pos + i]);
13716 : :
13717 [ + + ]: 44 : if (chunk < 16)
13718 : 4 : printf ("%*s", (int) ((16 - chunk) * 2 + (16 - chunk + 3) / 4), "");
13719 : :
13720 [ + + ]: 688 : for (size_t i = 0; i < chunk; ++i)
13721 : : {
13722 : 644 : unsigned char b = data[pos + i];
13723 [ + + ]: 1288 : printf ("%c", isprint (b) ? b : '.');
13724 : : }
13725 : :
13726 : 44 : putchar ('\n');
13727 : 44 : pos += chunk;
13728 : : }
13729 : 12 : }
13730 : :
13731 : : static void
13732 : 12 : dump_data_section (Elf_Scn *scn, const GElf_Shdr *shdr, const char *name)
13733 : : {
13734 [ + - - + ]: 12 : if (shdr->sh_size == 0 || shdr->sh_type == SHT_NOBITS)
13735 : 0 : printf (_("\nSection [%zu] '%s' has no data to dump.\n"),
13736 : : elf_ndxscn (scn), name);
13737 : : else
13738 : : {
13739 [ + + ]: 12 : if (print_decompress)
13740 : : {
13741 : : /* We try to decompress the section, but keep the old shdr around
13742 : : so we can show both the original shdr size and the uncompressed
13743 : : data size. */
13744 [ + + ]: 8 : if ((shdr->sh_flags & SHF_COMPRESSED) != 0)
13745 : : {
13746 [ - + ]: 4 : if (elf_compress (scn, 0, 0) < 0)
13747 : 0 : printf ("WARNING: %s [%zd]\n",
13748 : : _("Couldn't uncompress section"),
13749 : : elf_ndxscn (scn));
13750 : : }
13751 [ + - + - ]: 4 : else if (name && startswith (name, ".zdebug"))
13752 : : {
13753 [ - + ]: 4 : if (elf_compress_gnu (scn, 0, 0) < 0)
13754 : 0 : printf ("WARNING: %s [%zd]\n",
13755 : : _("Couldn't uncompress section"),
13756 : : elf_ndxscn (scn));
13757 : : }
13758 : : }
13759 : :
13760 : 12 : Elf_Data *data = elf_rawdata (scn, NULL);
13761 [ - + ]: 12 : if (data == NULL)
13762 : 0 : error (0, 0, _("cannot get data for section [%zu] '%s': %s"),
13763 : : elf_ndxscn (scn), name, elf_errmsg (-1));
13764 : : else
13765 : : {
13766 [ + + ]: 12 : if (data->d_size == shdr->sh_size)
13767 : 4 : printf (_("\nHex dump of section [%zu] '%s', %" PRIu64
13768 : : " bytes at offset %#0" PRIx64 ":\n"),
13769 : : elf_ndxscn (scn), name,
13770 : 4 : shdr->sh_size, shdr->sh_offset);
13771 : : else
13772 : 8 : printf (_("\nHex dump of section [%zu] '%s', %" PRIu64
13773 : : " bytes (%zd uncompressed) at offset %#0"
13774 : : PRIx64 ":\n"),
13775 : : elf_ndxscn (scn), name,
13776 : 8 : shdr->sh_size, data->d_size, shdr->sh_offset);
13777 : 12 : hex_dump (data->d_buf, data->d_size);
13778 : : }
13779 : : }
13780 : 12 : }
13781 : :
13782 : : static void
13783 : 374 : print_string_section (Elf_Scn *scn, const GElf_Shdr *shdr, const char *name)
13784 : : {
13785 [ + - + + ]: 374 : if (shdr->sh_size == 0 || shdr->sh_type == SHT_NOBITS)
13786 : 42 : printf (_("\nSection [%zu] '%s' has no strings to dump.\n"),
13787 : : elf_ndxscn (scn), name);
13788 : : else
13789 : : {
13790 [ + + ]: 332 : if (print_decompress)
13791 : : {
13792 : : /* We try to decompress the section, but keep the old shdr around
13793 : : so we can show both the original shdr size and the uncompressed
13794 : : data size. */
13795 [ - + ]: 2 : if ((shdr->sh_flags & SHF_COMPRESSED) != 0)
13796 : : {
13797 [ # # ]: 0 : if (elf_compress (scn, 0, 0) < 0)
13798 : 0 : printf ("WARNING: %s [%zd]\n",
13799 : : _("Couldn't uncompress section"),
13800 : : elf_ndxscn (scn));
13801 : : }
13802 [ + - + - ]: 2 : else if (name && startswith (name, ".zdebug"))
13803 : : {
13804 [ - + ]: 2 : if (elf_compress_gnu (scn, 0, 0) < 0)
13805 : 0 : printf ("WARNING: %s [%zd]\n",
13806 : : _("Couldn't uncompress section"),
13807 : : elf_ndxscn (scn));
13808 : : }
13809 : : }
13810 : :
13811 : 332 : Elf_Data *data = elf_rawdata (scn, NULL);
13812 [ - + ]: 332 : if (data == NULL)
13813 : 0 : error (0, 0, _("cannot get data for section [%zu] '%s': %s"),
13814 : : elf_ndxscn (scn), name, elf_errmsg (-1));
13815 : : else
13816 : : {
13817 [ + + ]: 332 : if (data->d_size == shdr->sh_size)
13818 : 330 : printf (_("\nString section [%zu] '%s' contains %" PRIu64
13819 : : " bytes at offset %#0" PRIx64 ":\n"),
13820 : : elf_ndxscn (scn), name,
13821 : 330 : shdr->sh_size, shdr->sh_offset);
13822 : : else
13823 : 2 : printf (_("\nString section [%zu] '%s' contains %" PRIu64
13824 : : " bytes (%zd uncompressed) at offset %#0"
13825 : : PRIx64 ":\n"),
13826 : : elf_ndxscn (scn), name,
13827 : 2 : shdr->sh_size, data->d_size, shdr->sh_offset);
13828 : :
13829 : 332 : const char *start = data->d_buf;
13830 : 332 : const char *const limit = start + data->d_size;
13831 : 46964 : do
13832 : : {
13833 : 46964 : const char *end = memchr (start, '\0', limit - start);
13834 : 46964 : const size_t pos = start - (const char *) data->d_buf;
13835 [ - + ]: 46964 : if (unlikely (end == NULL))
13836 : : {
13837 : 0 : printf (" [%6zx]- %.*s\n",
13838 : : pos, (int) (limit - start), start);
13839 : 0 : break;
13840 : : }
13841 : 46964 : printf (" [%6zx] %s\n", pos, start);
13842 : 46964 : start = end + 1;
13843 [ + + ]: 46964 : } while (start < limit);
13844 : : }
13845 : : }
13846 : 374 : }
13847 : :
13848 : : static void
13849 : 198 : for_each_section_argument (Elf *elf, const struct section_argument *list,
13850 : : void (*dump) (Elf_Scn *scn, const GElf_Shdr *shdr,
13851 : : const char *name))
13852 : : {
13853 : : /* Get the section header string table index. */
13854 : 198 : size_t shstrndx;
13855 [ - + ]: 198 : if (elf_getshdrstrndx (elf, &shstrndx) < 0)
13856 : 0 : error_exit (0, _("cannot get section header string table index"));
13857 : :
13858 [ + + ]: 764 : for (const struct section_argument *a = list; a != NULL; a = a->next)
13859 : : {
13860 : 566 : Elf_Scn *scn;
13861 : 566 : GElf_Shdr shdr_mem;
13862 : 566 : const char *name = NULL;
13863 : :
13864 : 566 : char *endp = NULL;
13865 : 566 : unsigned long int shndx = strtoul (a->arg, &endp, 0);
13866 [ + + - + ]: 566 : if (endp != a->arg && *endp == '\0')
13867 : : {
13868 : 2 : scn = elf_getscn (elf, shndx);
13869 [ - + ]: 2 : if (scn == NULL)
13870 : : {
13871 : 0 : error (0, 0, _("\nsection [%lu] does not exist"), shndx);
13872 : 0 : continue;
13873 : : }
13874 : :
13875 [ - + ]: 2 : if (gelf_getshdr (scn, &shdr_mem) == NULL)
13876 : 0 : error_exit (0, _("cannot get section header: %s"),
13877 : : elf_errmsg (-1));
13878 : 2 : name = elf_strptr (elf, shstrndx, shdr_mem.sh_name);
13879 : 2 : (*dump) (scn, &shdr_mem, name);
13880 : : }
13881 : : else
13882 : : {
13883 : : /* Need to look up the section by name. */
13884 : : scn = NULL;
13885 : : bool found = false;
13886 [ + + ]: 18180 : while ((scn = elf_nextscn (elf, scn)) != NULL)
13887 : : {
13888 [ - + ]: 17616 : if (gelf_getshdr (scn, &shdr_mem) == NULL)
13889 : 0 : continue;
13890 : 17616 : name = elf_strptr (elf, shstrndx, shdr_mem.sh_name);
13891 [ - + ]: 17616 : if (name == NULL)
13892 : 0 : continue;
13893 [ + + ]: 17616 : if (!strcmp (name, a->arg))
13894 : : {
13895 : 384 : found = true;
13896 : 384 : (*dump) (scn, &shdr_mem, name);
13897 : : }
13898 : : }
13899 : :
13900 [ + + - + ]: 564 : if (unlikely (!found) && !a->implicit)
13901 : 0 : error (0, 0, _("\nsection '%s' does not exist"), a->arg);
13902 : : }
13903 : : }
13904 : 198 : }
13905 : :
13906 : : static void
13907 : 12 : dump_data (Ebl *ebl)
13908 : : {
13909 : 12 : for_each_section_argument (ebl->elf, dump_data_sections, &dump_data_section);
13910 : 12 : }
13911 : :
13912 : : static void
13913 : 186 : dump_strings (Ebl *ebl)
13914 : : {
13915 : 186 : for_each_section_argument (ebl->elf, string_sections, &print_string_section);
13916 : 186 : }
13917 : :
13918 : : static void
13919 : 0 : print_strings (Ebl *ebl)
13920 : : {
13921 : : /* Get the section header string table index. */
13922 : 0 : size_t shstrndx;
13923 [ # # ]: 0 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
13924 : 0 : error_exit (0, _("cannot get section header string table index"));
13925 : :
13926 : : Elf_Scn *scn;
13927 : : GElf_Shdr shdr_mem;
13928 : : const char *name;
13929 : : scn = NULL;
13930 [ # # ]: 0 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
13931 : : {
13932 [ # # ]: 0 : if (gelf_getshdr (scn, &shdr_mem) == NULL)
13933 : 0 : continue;
13934 : :
13935 [ # # ]: 0 : if (shdr_mem.sh_type != SHT_PROGBITS
13936 [ # # ]: 0 : || !(shdr_mem.sh_flags & SHF_STRINGS))
13937 : 0 : continue;
13938 : :
13939 : 0 : name = elf_strptr (ebl->elf, shstrndx, shdr_mem.sh_name);
13940 [ # # ]: 0 : if (name == NULL)
13941 : 0 : continue;
13942 : :
13943 : 0 : print_string_section (scn, &shdr_mem, name);
13944 : : }
13945 : 0 : }
13946 : :
13947 : : static void
13948 : 4 : dump_archive_index (Elf *elf, const char *fname)
13949 : : {
13950 : 4 : size_t narsym;
13951 : 4 : const Elf_Arsym *arsym = elf_getarsym (elf, &narsym);
13952 [ - + ]: 4 : if (arsym == NULL)
13953 : : {
13954 : 0 : int result = elf_errno ();
13955 [ # # ]: 0 : if (unlikely (result != ELF_E_NO_INDEX))
13956 : 0 : error_exit (0, _("cannot get symbol index of archive '%s': %s"),
13957 : : fname, elf_errmsg (result));
13958 : : else
13959 : 0 : printf (_("\nArchive '%s' has no symbol index\n"), fname);
13960 : 0 : return;
13961 : : }
13962 : :
13963 : 4 : printf (_("\nIndex of archive '%s' has %zu entries:\n"),
13964 : : fname, narsym);
13965 : :
13966 : 4 : size_t as_off = 0;
13967 [ + + ]: 22 : for (const Elf_Arsym *s = arsym; s < &arsym[narsym - 1]; ++s)
13968 : : {
13969 [ + + ]: 18 : if (s->as_off != as_off)
13970 : : {
13971 : 12 : as_off = s->as_off;
13972 : :
13973 : 12 : Elf *subelf = NULL;
13974 [ + - ]: 12 : if (unlikely (elf_rand (elf, as_off) == 0)
13975 [ - + ]: 12 : || unlikely ((subelf = elf_begin (-1, ELF_C_READ_MMAP, elf))
13976 : : == NULL))
13977 : : #if __GLIBC__ < 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ < 7)
13978 : : while (1)
13979 : : #endif
13980 : 0 : error_exit (0,
13981 : : _("cannot extract member at offset %zu in '%s': %s"),
13982 : : as_off, fname, elf_errmsg (-1));
13983 : :
13984 : 12 : const Elf_Arhdr *h = elf_getarhdr (subelf);
13985 [ + - ]: 12 : if (h != NULL)
13986 : 12 : printf (_("Archive member '%s' contains:\n"), h->ar_name);
13987 : :
13988 : 12 : elf_end (subelf);
13989 : : }
13990 : :
13991 : 18 : printf ("\t%s\n", s->as_name);
13992 : : }
13993 : : }
13994 : :
13995 : : #include "debugpred.h"
|