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, 2026 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, cu_index, decodedaranges, frame, gdb_index, info, info+, "
145 : : "loc, line, decodedline, ranges, pubnames, str, macinfo, macro or "
146 : : "exception"), 0 },
147 : : { "hex-dump", 'x', "SECTION", 0,
148 : : N_("Dump the uninterpreted contents of SECTION, by number or name"), 0 },
149 : : { "strings", 'p', "SECTION", OPTION_ARG_OPTIONAL,
150 : : N_("Print string contents of sections"), 0 },
151 : : { "string-dump", 'p', NULL, OPTION_ALIAS | OPTION_HIDDEN, NULL, 0 },
152 : : { "archive-index", 'c', NULL, 0,
153 : : N_("Display the symbol index of an archive"), 0 },
154 : : { "use-dynamic", 'D', NULL, 0,
155 : : N_("Use the dynamic segment when possible for displaying info"), 0 },
156 : :
157 : : { NULL, 0, NULL, 0, N_("Output control:"), 0 },
158 : : { "numeric-addresses", 'N', NULL, 0,
159 : : N_("Do not find symbol names for addresses in DWARF data"), 0 },
160 : : { "unresolved-address-offsets", 'U', NULL, 0,
161 : : N_("Display just offsets instead of resolving values to addresses in DWARF data"), 0 },
162 : : { "wide", 'W', NULL, 0,
163 : : N_("Ignored for compatibility (lines always wide)"), 0 },
164 : : { "decompress", 'z', NULL, 0,
165 : : N_("Show compression information for compressed sections (when used with -S); decompress section before dumping data (when used with -p or -x)"), 0 },
166 : : #ifdef USE_LOCKS
167 : : { "concurrency", 'C', "NUM", 0,
168 : : N_("Set maximum number of threads. Defaults to the number of CPUs."), 0 },
169 : : #endif
170 : : { NULL, 0, NULL, 0, NULL, 0 }
171 : : };
172 : :
173 : : /* Short description of program. */
174 : : static const char doc[] = N_("\
175 : : Print information from ELF file in human-readable form.");
176 : :
177 : : /* Strings for arguments in help texts. */
178 : : static const char args_doc[] = N_("FILE...");
179 : :
180 : : /* Prototype for option handler. */
181 : : static error_t parse_opt (int key, char *arg, struct argp_state *state);
182 : :
183 : : /* Data structure to communicate with argp functions. */
184 : : static struct argp argp =
185 : : {
186 : : options, parse_opt, args_doc, doc, NULL, NULL, NULL
187 : : };
188 : :
189 : : /* If non-null, the section from which we should read to (compressed) ELF. */
190 : : static const char *elf_input_section = NULL;
191 : :
192 : : /* If non-null, the file that contains the skeleton CUs. */
193 : : static const char *dwarf_skeleton = NULL;
194 : :
195 : : /* Flags set by the option controlling the output. */
196 : :
197 : : /* True if dynamic segment should be printed. */
198 : : static bool print_dynamic_table;
199 : :
200 : : /* True if the file header should be printed. */
201 : : static bool print_file_header;
202 : :
203 : : /* True if the program headers should be printed. */
204 : : static bool print_program_header;
205 : :
206 : : /* True if relocations should be printed. */
207 : : static bool print_relocations;
208 : :
209 : : /* True if the section headers should be printed. */
210 : : static bool print_section_header;
211 : :
212 : : /* True if the symbol table should be printed. */
213 : : static bool print_symbol_table;
214 : :
215 : : /* True if (only) the dynsym table should be printed. */
216 : : static bool print_dynsym_table;
217 : :
218 : : /* True if reconstruct dynamic symbol table from the PT_DYNAMIC segment. */
219 : : static bool use_dynamic_segment;
220 : :
221 : : /* A specific section name, or NULL to print all symbol tables. */
222 : : static char *symbol_table_section;
223 : :
224 : : /* A specific section name, or NULL to print all ELF notes. */
225 : : static char *notes_section;
226 : :
227 : : /* True if the version information should be printed. */
228 : : static bool print_version_info;
229 : :
230 : : /* True if section groups should be printed. */
231 : : static bool print_section_groups;
232 : :
233 : : /* True if bucket list length histogram should be printed. */
234 : : static bool print_histogram;
235 : :
236 : : /* True if the architecture specific data should be printed. */
237 : : static bool print_arch;
238 : :
239 : : /* True if note section content should be printed. */
240 : : static bool print_notes;
241 : :
242 : : /* True if SHF_STRINGS section content should be printed. */
243 : : static bool print_string_sections;
244 : :
245 : : /* True if archive index should be printed. */
246 : : static bool print_archive_index;
247 : :
248 : : /* True if any of the control options except print_archive_index is set. */
249 : : static bool any_control_option;
250 : :
251 : : /* True if we should print addresses from DWARF in symbolic form. */
252 : : static bool print_address_names = true;
253 : :
254 : : /* True if we should print raw values instead of relativized addresses. */
255 : : static bool print_unresolved_addresses = false;
256 : :
257 : : /* True if we should print the .debug_aranges section using libdw. */
258 : : static bool decodedaranges = false;
259 : :
260 : : /* True if we should print the .debug_aranges section using libdw. */
261 : : static bool decodedline = false;
262 : :
263 : : /* True if we want to show more information about compressed sections. */
264 : : static bool print_decompress = false;
265 : :
266 : : /* True if we want to show split compile units for debug_info skeletons. */
267 : : static bool show_split_units = false;
268 : :
269 : : #if USE_LOCKS
270 : : /* Maximum number of threads. */
271 : : static int max_threads = 0;
272 : : #endif
273 : :
274 : : /* Select printing of debugging sections. */
275 : : static enum section_e
276 : : {
277 : : section_abbrev = 1, /* .debug_abbrev */
278 : : section_aranges = 2, /* .debug_aranges */
279 : : section_frame = 4, /* .debug_frame or .eh_frame & al. */
280 : : section_info = 8, /* .debug_info, (implies .debug_types) */
281 : : section_line = 16, /* .debug_line */
282 : : section_loc = 32, /* .debug_loc */
283 : : section_pubnames = 64, /* .debug_pubnames */
284 : : section_str = 128, /* .debug_str */
285 : : section_macinfo = 256, /* .debug_macinfo */
286 : : section_ranges = 512, /* .debug_ranges */
287 : : section_exception = 1024, /* .eh_frame & al. */
288 : : section_gdb_index = 2048, /* .gdb_index */
289 : : section_macro = 4096, /* .debug_macro */
290 : : section_addr = 8192, /* .debug_addr */
291 : : section_types = 16384, /* .debug_types (implied by .debug_info) */
292 : : section_cu_index = 32768, /* .debug_cu_index (include .debug_tu_index) */
293 : : section_all = (section_abbrev | section_aranges | section_frame
294 : : | section_info | section_line | section_loc
295 : : | section_pubnames | section_str | section_macinfo
296 : : | section_ranges | section_exception | section_gdb_index
297 : : | section_macro | section_addr | section_types
298 : : | section_cu_index)
299 : : } print_debug_sections, implicit_debug_sections;
300 : :
301 : : /* Select hex dumping of sections. */
302 : : static struct section_argument *dump_data_sections;
303 : : static struct section_argument **dump_data_sections_tail = &dump_data_sections;
304 : :
305 : : /* Select string dumping of sections. */
306 : : static struct section_argument *string_sections;
307 : : static struct section_argument **string_sections_tail = &string_sections;
308 : :
309 : : struct section_argument
310 : : {
311 : : struct section_argument *next;
312 : : const char *arg;
313 : : bool implicit;
314 : : };
315 : :
316 : : /* Numbers of sections and program headers in the file. */
317 : : static size_t shnum;
318 : : static size_t phnum;
319 : :
320 : :
321 : : /* Declarations of local functions. */
322 : : static void process_file (int fd, const char *fname, bool only_one);
323 : : static void process_elf_file (Dwfl_Module *dwflmod, int fd);
324 : : static void print_ehdr (Ebl *ebl, GElf_Ehdr *ehdr);
325 : : static void print_shdr (Ebl *ebl, GElf_Ehdr *ehdr);
326 : : static void print_phdr (Ebl *ebl, GElf_Ehdr *ehdr);
327 : : static void print_scngrp (Ebl *ebl);
328 : : static void print_dynamic (Ebl *ebl);
329 : : static void print_relocs (Ebl *ebl, Dwfl_Module *mod, GElf_Ehdr *ehdr);
330 : : static void handle_relocs_rel (Ebl *ebl, GElf_Ehdr *ehdr, Elf_Scn *scn,
331 : : GElf_Shdr *shdr);
332 : : static void handle_relocs_rela (Ebl *ebl, GElf_Ehdr *ehdr, Elf_Scn *scn,
333 : : GElf_Shdr *shdr);
334 : : static void handle_relocs_relr (Ebl *ebl, Dwfl_Module *mod, Elf_Scn *scn,
335 : : GElf_Shdr *shdr);
336 : : static bool print_symtab (Ebl *ebl, int type);
337 : : static bool handle_symtab (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr);
338 : : static bool handle_dynamic_symtab (Ebl *ebl);
339 : : static void
340 : : process_symtab(
341 : : Ebl * ebl,
342 : : unsigned int nsyms,
343 : : Elf64_Word idx,
344 : : Elf32_Word verneed_stridx,
345 : : Elf32_Word verdef_stridx,
346 : : Elf_Data * symdata,
347 : : Elf_Data * versym_data,
348 : : Elf_Data * symstr_data,
349 : : Elf_Data * verneed_data,
350 : : Elf_Data * verdef_data,
351 : : Elf_Data * xndx_data);
352 : : static void print_verinfo (Ebl *ebl);
353 : : static void handle_verneed (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr);
354 : : static void handle_verdef (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr);
355 : : static void handle_versym (Ebl *ebl, Elf_Scn *scn,
356 : : GElf_Shdr *shdr);
357 : : static void print_debug (Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr);
358 : : static void handle_hash (Ebl *ebl);
359 : : static void handle_notes (Ebl *ebl, GElf_Ehdr *ehdr);
360 : : static void print_liblist (Ebl *ebl);
361 : : static void print_attributes (Ebl *ebl, const GElf_Ehdr *ehdr);
362 : : static void dump_data (Ebl *ebl);
363 : : static void dump_strings (Ebl *ebl);
364 : : static void print_strings (Ebl *ebl);
365 : : static void dump_archive_index (Elf *, const char *);
366 : : static void print_dwarf_addr (Dwfl_Module *dwflmod, int address_size,
367 : : Dwarf_Addr address, Dwarf_Addr raw, FILE *out);
368 : : static void print_flag_info(void);
369 : :
370 : : enum dyn_idx
371 : : {
372 : : i_symtab_shndx,
373 : : i_strsz,
374 : : i_verneed,
375 : : i_verneednum,
376 : : i_verdef,
377 : : i_verdefnum,
378 : : i_versym,
379 : : i_symtab,
380 : : i_strtab,
381 : : i_hash,
382 : : i_gnu_hash,
383 : : i_max
384 : : };
385 : :
386 : : /* Declarations of local functions for use-dynamic. */
387 : : static Elf_Data *get_dynscn_strtab (Elf *elf, GElf_Phdr *phdr);
388 : : static void get_dynscn_addrs (Elf *elf, GElf_Phdr *phdr, GElf_Addr addrs[i_max]);
389 : : static void find_offsets (Elf *elf, GElf_Addr main_bias, size_t n,
390 : : GElf_Addr addrs[n], GElf_Off offs[n]);
391 : :
392 : : /* Looked up once with gettext in main. */
393 : : static char *yes_str;
394 : : static char *no_str;
395 : : static char *empty_block_str;
396 : : static char *byte_block_str;
397 : : static char *offset_str;
398 : : static char *children_str;
399 : : static char *tag_str;
400 : : static char *length_str;
401 : : static char *dwarf_ver_str;
402 : : static char *cu_offset_str;
403 : : static char *seg_size_str;
404 : : static char *table_offset_lower_str;
405 : : static char *table_offset_upper_str;
406 : : static char *addr_size_str;
407 : : static char *offset_ent_str;
408 : : static char *cu_str;
409 : : static char *base_str;
410 : : static char *type_unit_str;
411 : : static char *version_str;
412 : : static char *abbrev_offset_str;
413 : : static char *addr_size_str;
414 : : static char *offset_size_str;
415 : : static char *type_sig_str;
416 : : static char *type_offset_str;
417 : : static char *cu_offset_at_str;
418 : : static char *unit_type_str;
419 : : static char *unit_id_str;
420 : : static char *unit_die_off_str;
421 : : static char *prologue_len_str;
422 : : static char *seg_selector_str;
423 : : static char *min_inst_len_str;
424 : : static char *max_op_per_inst_str;
425 : : static char *initial_value_if_str;
426 : : static char *line_base_str;
427 : : static char *line_range_str;
428 : : static char *opcodes_base_str;
429 : : static char *opcodes_str;
430 : : static char *special_opcode_str;
431 : : static char *address_str;
432 : : static char *line_str;
433 : : static char *ext_opcode_str;
434 : : static char *set_disc_str;
435 : : static char *adv_addr_str;
436 : : static char *to_str;
437 : : static char *set_file_str;
438 : : static char *set_col_str;
439 : : static char *set_str;
440 : : static char *adv_line_by_str;
441 : : static char *adv_addr_by_str;
442 : : static char *unknown_base_str;
443 : : static char *unknown_base_str;
444 : : static char *dir_table_str;
445 : : static char *file_table_str;
446 : : static char *no_line_num_stmts_str;
447 : : static char *line_num_stmts_str;
448 : : static char *end_of_seq_str;
449 : : static char *set_addr_to_str;
450 : : static char *copy_str;
451 : :
452 : :
453 : : static void
454 : 894 : init_gettext_strs (void)
455 : : {
456 : 894 : yes_str = _("yes");
457 : 894 : no_str = _("no");
458 : 894 : empty_block_str = _("empty block");
459 : 894 : byte_block_str = _("byte block");
460 : 894 : offset_str = _("offset");
461 : 894 : children_str = _("children");
462 : 894 : tag_str = _("tag");
463 : 894 : table_offset_lower_str = _("Table at offset");
464 : 894 : table_offset_upper_str = _("Table at Offset");
465 : 894 : length_str = _("Length");
466 : 894 : dwarf_ver_str = _("DWARF version");
467 : 894 : cu_offset_str = _("CU offset");
468 : 894 : addr_size_str = _("Address size");
469 : 894 : seg_size_str = _("Segment size");
470 : 894 : offset_ent_str = _("Offset entries");
471 : 894 : cu_str = _("CU");
472 : 894 : base_str = _("base");
473 : 894 : type_unit_str = _("Type unit at offset");
474 : 894 : version_str = _("Version");
475 : 894 : abbrev_offset_str = _("Abbreviation section offset");
476 : 894 : offset_size_str = _("Offset size");
477 : 894 : type_sig_str = _("Type signature");
478 : 894 : type_offset_str = _("Type offset");
479 : 894 : cu_offset_at_str = _("Compilation unit at offset");
480 : 894 : unit_type_str = _("Unit type");
481 : 894 : unit_id_str = _("Unit id");
482 : 894 : unit_die_off_str = _("Unit DIE off");
483 : 894 : prologue_len_str = _("Prologue length");
484 : 894 : seg_selector_str = _("Segment selector size");
485 : 894 : min_inst_len_str = _("Min instruction length");
486 : 894 : max_op_per_inst_str = _("Max operations per instruction");
487 : 894 : initial_value_if_str = _("Initial value if");
488 : 894 : line_base_str = _("Line base");
489 : 894 : line_range_str = _("Line range");
490 : 894 : opcodes_base_str = _("Opcode base");
491 : 894 : opcodes_str = _("Opcodes");
492 : 894 : special_opcode_str = _("special opcode");
493 : 894 : address_str = _("address");
494 : 894 : line_str = _("line");
495 : 894 : ext_opcode_str = _("extended opcode");
496 : 894 : set_disc_str = _("set discriminator to");
497 : 894 : adv_addr_str = _("advance address by");
498 : 894 : to_str = _("to");
499 : 894 : set_file_str = _("set file to");
500 : 894 : set_col_str = _("set column to");
501 : 894 : set_str = _("set");
502 : 894 : adv_addr_by_str = _("advance address by constant");
503 : 894 : adv_line_by_str = _("advance line by constant");
504 : 894 : unknown_base_str = _("Unknown CU base");
505 : 894 : dir_table_str = _("Directory table");
506 : 894 : file_table_str = _("File name table");
507 : 894 : no_line_num_stmts_str = _("No line number statements");
508 : 894 : line_num_stmts_str = _("Line number statements");
509 : 894 : end_of_seq_str = _("end of sequence");
510 : 894 : set_addr_to_str = _("set address to");
511 : 894 : copy_str = _("copy");
512 : 894 : }
513 : :
514 : :
515 : : static void
516 : 1788 : cleanup_list (struct section_argument *list)
517 : : {
518 [ + + + + ]: 2354 : while (list != NULL)
519 : : {
520 : 566 : struct section_argument *a = list;
521 : 566 : list = a->next;
522 : 566 : free (a);
523 : : }
524 : : }
525 : :
526 : : #ifdef USE_LOCKS
527 : : /* Estimate the maximum number of threads. This is normally
528 : : #CPU. Return value is guaranteed to be at least 1. */
529 : : static int
530 : : default_concurrency (void)
531 : : {
532 : : unsigned aff = 0;
533 : : #ifdef HAVE_SCHED_GETAFFINITY
534 : : {
535 : : int ret;
536 : : cpu_set_t mask;
537 : : CPU_ZERO (&mask);
538 : : ret = sched_getaffinity (0, sizeof(mask), &mask);
539 : : if (ret == 0)
540 : : aff = CPU_COUNT (&mask);
541 : : }
542 : : #endif
543 : :
544 : : unsigned fn = 0;
545 : : #ifdef HAVE_GETRLIMIT
546 : : {
547 : : struct rlimit rlim;
548 : : int rc = getrlimit (RLIMIT_NOFILE, &rlim);
549 : : if (rc == 0)
550 : : fn = MAX ((rlim_t) 1, (rlim.rlim_cur - 100) / 2);
551 : : /* Conservatively estimate that at least 2 fds are used
552 : : by each thread. */
553 : : }
554 : : #endif
555 : :
556 : : unsigned d = MIN (MAX (aff, 1U),
557 : : MAX (fn, 1U));
558 : :
559 : : return d;
560 : : }
561 : : #endif
562 : :
563 : : int
564 : 894 : main (int argc, char *argv[])
565 : : {
566 : : /* We use no threads here which can interfere with handling a stream. */
567 : 894 : (void) __fsetlocking (stdout, FSETLOCKING_BYCALLER);
568 : :
569 : : /* Set locale. */
570 : 894 : setlocale (LC_ALL, "");
571 : :
572 : : /* Initialize the message catalog. */
573 : 894 : textdomain (PACKAGE_TARNAME);
574 : :
575 : : /* Look up once. */
576 : 894 : init_gettext_strs ();
577 : :
578 : : /* Parse and process arguments. */
579 : 894 : int remaining;
580 : 894 : argp_parse (&argp, argc, argv, 0, &remaining, NULL);
581 : :
582 : : /* Before we start tell the ELF library which version we are using. */
583 : 894 : elf_version (EV_CURRENT);
584 : :
585 : : #ifdef USE_LOCKS
586 : : /* If concurrency wasn't set by argp_parse, then set a default value. */
587 : : if (max_threads == 0)
588 : : max_threads = default_concurrency ();
589 : : #endif
590 : :
591 : : /* Now process all the files given at the command line. */
592 : 894 : bool only_one = remaining + 1 == argc;
593 : 976 : do
594 : : {
595 : : /* Open the file. */
596 : 976 : int fd = open (argv[remaining], O_RDONLY);
597 [ - + ]: 976 : if (fd == -1)
598 : : {
599 : 0 : error (0, errno, _("cannot open input file '%s'"), argv[remaining]);
600 : 0 : continue;
601 : : }
602 : :
603 : 976 : process_file (fd, argv[remaining], only_one);
604 : :
605 : 976 : close (fd);
606 : : }
607 [ + + ]: 976 : while (++remaining < argc);
608 : :
609 : 894 : cleanup_list (dump_data_sections);
610 : 894 : cleanup_list (string_sections);
611 : :
612 : 894 : return error_message_count != 0;
613 : : }
614 : :
615 : : static void
616 : 566 : add_dump_section (const char *name,
617 : : int key,
618 : : bool implicit)
619 : : {
620 : 566 : struct section_argument *a = xmalloc (sizeof *a);
621 : 566 : a->arg = name;
622 : 566 : a->next = NULL;
623 : 566 : a->implicit = implicit;
624 : 1132 : struct section_argument ***tailp
625 [ + + ]: 566 : = key == 'x' ? &dump_data_sections_tail : &string_sections_tail;
626 : 566 : **tailp = a;
627 : 566 : *tailp = &a->next;
628 : 566 : }
629 : :
630 : : /* Handle program arguments. */
631 : : static error_t
632 : 5686 : parse_opt (int key, char *arg,
633 : : struct argp_state *state __attribute__ ((unused)))
634 : : {
635 [ + + - + : 5686 : switch (key)
+ - + + -
+ + + + +
+ - + + +
+ + + - +
+ + + + ]
636 : : {
637 : 184 : case 'a':
638 : 184 : print_file_header = true;
639 : 184 : print_program_header = true;
640 : 184 : print_relocations = true;
641 : 184 : print_section_header = true;
642 : 184 : print_symbol_table = true;
643 : 184 : print_version_info = true;
644 : 184 : print_dynamic_table = true;
645 : 184 : print_section_groups = true;
646 : 184 : print_histogram = true;
647 : 184 : print_arch = true;
648 : 184 : print_notes = true;
649 : 184 : implicit_debug_sections |= section_exception;
650 : 184 : add_dump_section (".strtab", key, true);
651 : 184 : add_dump_section (".dynstr", key, true);
652 : 184 : add_dump_section (".comment", key, true);
653 : 184 : any_control_option = true;
654 : 184 : break;
655 : 8 : case 'A':
656 : 8 : print_arch = true;
657 : 8 : any_control_option = true;
658 : 8 : break;
659 : 6 : case 'd':
660 : 6 : print_dynamic_table = true;
661 : 6 : any_control_option = true;
662 : 6 : break;
663 : 2 : case 'D':
664 : 2 : use_dynamic_segment = true;
665 : 2 : break;
666 : 0 : case 'e':
667 : 0 : print_debug_sections |= section_exception;
668 : 0 : any_control_option = true;
669 : 0 : break;
670 : 18 : case 'g':
671 : 18 : print_section_groups = true;
672 : 18 : any_control_option = true;
673 : 18 : break;
674 : 6 : case 'h':
675 : 6 : print_file_header = true;
676 : 6 : any_control_option = true;
677 : 6 : break;
678 : 0 : case 'I':
679 : 0 : print_histogram = true;
680 : 0 : any_control_option = true;
681 : 0 : break;
682 : 4 : case 'l':
683 : 4 : print_program_header = true;
684 : 4 : any_control_option = true;
685 : 4 : break;
686 : 46 : case 'n':
687 : 46 : print_notes = true;
688 : 46 : any_control_option = true;
689 : 46 : notes_section = arg;
690 : 46 : break;
691 : 10 : case 'r':
692 : 10 : print_relocations = true;
693 : 10 : any_control_option = true;
694 : 10 : break;
695 : 322 : case 'S':
696 : 322 : print_section_header = true;
697 : 322 : any_control_option = true;
698 : 322 : break;
699 : 34 : case 's':
700 : 34 : print_symbol_table = true;
701 : 34 : any_control_option = true;
702 : 34 : symbol_table_section = arg;
703 : 34 : break;
704 : 2 : case PRINT_DYNSYM_TABLE:
705 : 2 : print_dynsym_table = true;
706 : 2 : any_control_option = true;
707 : 2 : break;
708 : 0 : case 'V':
709 : 0 : print_version_info = true;
710 : 0 : any_control_option = true;
711 : 0 : break;
712 : 4 : case 'c':
713 : 4 : print_archive_index = true;
714 : 4 : break;
715 : : #if USE_LOCKS
716 : : case 'C':
717 : : if (arg != NULL)
718 : : {
719 : : max_threads = atoi (arg);
720 : : if (max_threads < 1)
721 : : {
722 : : argp_error (state, _("-C NUM minimum 1"));
723 : : return EINVAL;
724 : : }
725 : : }
726 : : break;
727 : : #endif
728 : 268 : case 'w':
729 [ + + ]: 268 : if (arg == NULL)
730 : : {
731 : 86 : print_debug_sections = section_all;
732 : 86 : implicit_debug_sections = section_info;
733 : 86 : show_split_units = true;
734 : : }
735 [ - + ]: 182 : else if (strcmp (arg, "abbrev") == 0)
736 : 0 : print_debug_sections |= section_abbrev;
737 [ + + ]: 182 : else if (strcmp (arg, "addr") == 0)
738 : : {
739 : 4 : print_debug_sections |= section_addr;
740 : 4 : implicit_debug_sections |= section_info;
741 : : }
742 [ + + ]: 178 : else if (strcmp (arg, "aranges") == 0)
743 : 6 : print_debug_sections |= section_aranges;
744 [ + + ]: 172 : else if (strcmp (arg, "decodedaranges") == 0)
745 : : {
746 : 2 : print_debug_sections |= section_aranges;
747 : 2 : decodedaranges = true;
748 : : }
749 [ + + ]: 170 : else if (strcmp (arg, "ranges") == 0)
750 : : {
751 : 26 : print_debug_sections |= section_ranges;
752 : 26 : implicit_debug_sections |= section_info;
753 : : }
754 [ + + + + ]: 144 : else if (strcmp (arg, "frame") == 0 || strcmp (arg, "frames") == 0)
755 : 6 : print_debug_sections |= section_frame;
756 [ + + ]: 138 : else if (strcmp (arg, "info") == 0)
757 : : {
758 : 40 : print_debug_sections |= section_info;
759 : 40 : print_debug_sections |= section_types;
760 : : }
761 [ + + ]: 98 : else if (strcmp (arg, "info+") == 0)
762 : : {
763 : 4 : print_debug_sections |= section_info;
764 : 4 : print_debug_sections |= section_types;
765 : 4 : show_split_units = true;
766 : : }
767 [ + + ]: 94 : else if (strcmp (arg, "loc") == 0)
768 : : {
769 : 38 : print_debug_sections |= section_loc;
770 : 38 : implicit_debug_sections |= section_info;
771 : : }
772 [ + + ]: 56 : else if (strcmp (arg, "line") == 0)
773 : 16 : print_debug_sections |= section_line;
774 [ + + ]: 40 : else if (strcmp (arg, "decodedline") == 0)
775 : : {
776 : 14 : print_debug_sections |= section_line;
777 : 14 : decodedline = true;
778 : : }
779 [ - + ]: 26 : else if (strcmp (arg, "pubnames") == 0)
780 : 0 : print_debug_sections |= section_pubnames;
781 [ + + ]: 26 : else if (strcmp (arg, "str") == 0)
782 : : {
783 : 6 : print_debug_sections |= section_str;
784 : : /* For mapping string offset tables to CUs. */
785 : 6 : implicit_debug_sections |= section_info;
786 : : }
787 [ - + ]: 20 : else if (strcmp (arg, "macinfo") == 0)
788 : 0 : print_debug_sections |= section_macinfo;
789 [ + + ]: 20 : else if (strcmp (arg, "macro") == 0)
790 : 8 : print_debug_sections |= section_macro;
791 [ - + ]: 12 : else if (strcmp (arg, "exception") == 0)
792 : 0 : print_debug_sections |= section_exception;
793 [ + + ]: 12 : else if (strcmp (arg, "gdb_index") == 0)
794 : 8 : print_debug_sections |= section_gdb_index;
795 [ + - ]: 4 : else if (strcmp (arg, "cu_index") == 0)
796 : 4 : print_debug_sections |= section_cu_index;
797 : : else
798 : : {
799 : 0 : fprintf (stderr, _("Unknown DWARF debug section `%s'.\n"),
800 : : arg);
801 : 0 : argp_help (&argp, stderr, ARGP_HELP_SEE,
802 : : program_invocation_short_name);
803 : 0 : exit (1);
804 : : }
805 : 268 : any_control_option = true;
806 : 268 : break;
807 : 2 : case 'p':
808 : 2 : any_control_option = true;
809 [ - + ]: 2 : if (arg == NULL)
810 : : {
811 : 0 : print_string_sections = true;
812 : 0 : break;
813 : : }
814 : 14 : FALLTHROUGH;
815 : : case 'x':
816 : 14 : add_dump_section (arg, key, false);
817 : 14 : any_control_option = true;
818 : 14 : break;
819 : 90 : case 'N':
820 : 90 : print_address_names = false;
821 : 90 : break;
822 : 56 : case 'U':
823 : 56 : print_unresolved_addresses = true;
824 : 56 : break;
825 : 0 : case ARGP_KEY_NO_ARGS:
826 : 0 : fputs (_("Missing file name.\n"), stderr);
827 : 0 : goto do_argp_help;
828 : 894 : case ARGP_KEY_FINI:
829 [ + + + - ]: 894 : if (! any_control_option && ! print_archive_index)
830 : : {
831 : 0 : fputs (_("No operation specified.\n"), stderr);
832 : 0 : do_argp_help:
833 : 0 : argp_help (&argp, stderr, ARGP_HELP_SEE,
834 : : program_invocation_short_name);
835 : 0 : exit (EXIT_FAILURE);
836 : : }
837 : : break;
838 : : case 'W': /* Ignored. */
839 : : break;
840 : 122 : case 'z':
841 : 122 : print_decompress = true;
842 : 122 : break;
843 : 10 : case ELF_INPUT_SECTION:
844 [ + - ]: 10 : if (arg == NULL)
845 : 10 : elf_input_section = ".gnu_debugdata";
846 : : else
847 : 0 : elf_input_section = arg;
848 : : break;
849 : 10 : case DWARF_SKELETON:
850 : 10 : dwarf_skeleton = arg;
851 : 10 : break;
852 : : default:
853 : : return ARGP_ERR_UNKNOWN;
854 : : }
855 : : return 0;
856 : : }
857 : :
858 : :
859 : : /* Create a file descriptor to read the data from the
860 : : elf_input_section given a file descriptor to an ELF file. */
861 : : static int
862 : 10 : open_input_section (int fd)
863 : : {
864 : 10 : size_t shnums;
865 : 10 : size_t cnt;
866 : 10 : size_t shstrndx;
867 : 10 : Elf *elf = elf_begin (fd, ELF_C_READ_MMAP, NULL);
868 [ - + ]: 10 : if (elf == NULL)
869 : : {
870 : 0 : error (0, 0, _("cannot generate Elf descriptor: %s"),
871 : : elf_errmsg (-1));
872 : 0 : return -1;
873 : : }
874 : :
875 [ - + ]: 10 : if (elf_getshdrnum (elf, &shnums) < 0)
876 : : {
877 : 0 : error (0, 0, _("cannot determine number of sections: %s"),
878 : : elf_errmsg (-1));
879 : 0 : open_error:
880 : 0 : elf_end (elf);
881 : 0 : return -1;
882 : : }
883 : :
884 [ - + ]: 10 : if (elf_getshdrstrndx (elf, &shstrndx) < 0)
885 : : {
886 : 0 : error (0, 0, _("cannot get section header string table index"));
887 : 0 : goto open_error;
888 : : }
889 : :
890 [ + - ]: 236 : for (cnt = 0; cnt < shnums; ++cnt)
891 : : {
892 : 236 : Elf_Scn *scn = elf_getscn (elf, cnt);
893 [ - + ]: 236 : if (scn == NULL)
894 : : {
895 : 0 : error (0, 0, _("cannot get section: %s"),
896 : : elf_errmsg (-1));
897 : 0 : goto open_error;
898 : : }
899 : :
900 : 236 : GElf_Shdr shdr_mem;
901 : 236 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
902 [ - + ]: 236 : if (unlikely (shdr == NULL))
903 : : {
904 : 0 : error (0, 0, _("cannot get section header: %s"),
905 : : elf_errmsg (-1));
906 : 0 : goto open_error;
907 : : }
908 : :
909 : 236 : const char *sname = elf_strptr (elf, shstrndx, shdr->sh_name);
910 [ - + ]: 236 : if (sname == NULL)
911 : : {
912 : 0 : error (0, 0, _("cannot get section name"));
913 : 0 : goto open_error;
914 : : }
915 : :
916 [ + + ]: 236 : if (strcmp (sname, elf_input_section) == 0)
917 : : {
918 : 10 : Elf_Data *data = elf_rawdata (scn, NULL);
919 [ - + ]: 10 : if (data == NULL)
920 : : {
921 : 0 : error (0, 0, _("cannot get %s content: %s"),
922 : : sname, elf_errmsg (-1));
923 : 0 : goto open_error;
924 : : }
925 : :
926 : : /* Create (and immediately unlink) a temporary file to store
927 : : section data in to create a file descriptor for it. */
928 [ + - ]: 10 : const char *tmpdir = getenv ("TMPDIR") ?: P_tmpdir;
929 : 10 : static const char suffix[] = "/readelfXXXXXX";
930 : 10 : int tmplen = strlen (tmpdir) + sizeof (suffix);
931 : 10 : char *tempname = alloca (tmplen);
932 : 10 : sprintf (tempname, "%s%s", tmpdir, suffix);
933 : :
934 : 10 : int sfd = mkstemp (tempname);
935 [ - + ]: 10 : if (sfd == -1)
936 : : {
937 : 0 : error (0, 0, _("cannot create temp file '%s'"),
938 : : tempname);
939 : 0 : goto open_error;
940 : : }
941 : 10 : unlink (tempname);
942 : :
943 : 10 : ssize_t size = data->d_size;
944 [ - + ]: 10 : if (write_retry (sfd, data->d_buf, size) != size)
945 : : {
946 : 0 : error (0, 0, _("cannot write section data"));
947 : 0 : goto open_error;
948 : : }
949 : :
950 [ - + ]: 10 : if (elf_end (elf) != 0)
951 : : {
952 : 0 : error (0, 0, _("error while closing Elf descriptor: %s"),
953 : : elf_errmsg (-1));
954 : 10 : return -1;
955 : : }
956 : :
957 [ - + ]: 10 : if (lseek (sfd, 0, SEEK_SET) == -1)
958 : : {
959 : 0 : error (0, 0, _("error while rewinding file descriptor"));
960 : 0 : return -1;
961 : : }
962 : :
963 : : return sfd;
964 : : }
965 : : }
966 : :
967 : : /* Named section not found. */
968 [ # # ]: 0 : if (elf_end (elf) != 0)
969 : 0 : error (0, 0, _("error while closing Elf descriptor: %s"),
970 : : elf_errmsg (-1));
971 : : return -1;
972 : : }
973 : :
974 : : /* Check if the file is an archive, and if so dump its index. */
975 : : static void
976 : 4 : check_archive_index (int fd, const char *fname, bool only_one)
977 : : {
978 : : /* Create an `Elf' descriptor. */
979 : 4 : Elf *elf = elf_begin (fd, ELF_C_READ_MMAP, NULL);
980 [ - + ]: 4 : if (elf == NULL)
981 : 0 : error (0, 0, _("cannot generate Elf descriptor: %s"),
982 : : elf_errmsg (-1));
983 : : else
984 : : {
985 [ + - ]: 4 : if (elf_kind (elf) == ELF_K_AR)
986 : : {
987 [ - + ]: 4 : if (!only_one)
988 : 0 : printf ("\n%s:\n\n", fname);
989 : 4 : dump_archive_index (elf, fname);
990 : : }
991 : : else
992 : 0 : error (0, 0,
993 : 0 : _("'%s' is not an archive, cannot print archive index"),
994 : : fname);
995 : :
996 : : /* Now we can close the descriptor. */
997 [ - + ]: 4 : if (elf_end (elf) != 0)
998 : 0 : error (0, 0, _("error while closing Elf descriptor: %s"),
999 : : elf_errmsg (-1));
1000 : : }
1001 : 4 : }
1002 : :
1003 : : /* Trivial callback used for checking if we opened an archive. */
1004 : : static int
1005 : 872 : count_dwflmod (Dwfl_Module *dwflmod __attribute__ ((unused)),
1006 : : void **userdata __attribute__ ((unused)),
1007 : : const char *name __attribute__ ((unused)),
1008 : : Dwarf_Addr base __attribute__ ((unused)),
1009 : : void *arg)
1010 : : {
1011 [ + - ]: 872 : if (*(bool *) arg)
1012 : : return DWARF_CB_ABORT;
1013 : 872 : *(bool *) arg = true;
1014 : 872 : return DWARF_CB_OK;
1015 : : }
1016 : :
1017 : : struct process_dwflmod_args
1018 : : {
1019 : : int fd;
1020 : : bool only_one;
1021 : : };
1022 : :
1023 : : static int
1024 : 972 : process_dwflmod (Dwfl_Module *dwflmod,
1025 : : void **userdata __attribute__ ((unused)),
1026 : : const char *name __attribute__ ((unused)),
1027 : : Dwarf_Addr base __attribute__ ((unused)),
1028 : : void *arg)
1029 : : {
1030 : 972 : const struct process_dwflmod_args *a = arg;
1031 : :
1032 : : /* Print the file name. */
1033 [ + + ]: 972 : if (!a->only_one)
1034 : : {
1035 : 100 : const char *fname;
1036 : 100 : dwfl_module_info (dwflmod, NULL, NULL, NULL, NULL, NULL, &fname, NULL);
1037 : :
1038 : 100 : printf ("\n%s:\n\n", fname);
1039 : : }
1040 : :
1041 : 972 : process_elf_file (dwflmod, a->fd);
1042 : :
1043 : 972 : return DWARF_CB_OK;
1044 : : }
1045 : :
1046 : : /* Stub libdwfl callback, only the ELF handle already open is ever used.
1047 : : Only used for finding the alternate debug file if the Dwarf comes from
1048 : : the main file. We are not interested in separate debuginfo. */
1049 : : static int
1050 : 262 : find_no_debuginfo (Dwfl_Module *mod,
1051 : : void **userdata,
1052 : : const char *modname,
1053 : : Dwarf_Addr base,
1054 : : const char *file_name,
1055 : : const char *debuglink_file,
1056 : : GElf_Word debuglink_crc,
1057 : : char **debuginfo_file_name)
1058 : : {
1059 : 262 : Dwarf_Addr dwbias;
1060 : 262 : dwfl_module_info (mod, NULL, NULL, NULL, &dwbias, NULL, NULL, NULL);
1061 : :
1062 : : /* We are only interested if the Dwarf has been setup on the main
1063 : : elf file but is only missing the alternate debug link. If dwbias
1064 : : hasn't even been setup, this is searching for separate debuginfo
1065 : : for the main elf. We don't care in that case. */
1066 [ + + ]: 262 : if (dwbias == (Dwarf_Addr) -1)
1067 : : return -1;
1068 : :
1069 : 70 : return dwfl_standard_find_debuginfo (mod, userdata, modname, base,
1070 : : file_name, debuglink_file,
1071 : : debuglink_crc, debuginfo_file_name);
1072 : : }
1073 : :
1074 : : static Dwfl *
1075 : 990 : create_dwfl (int fd, const char *fname)
1076 : : {
1077 : : /* Duplicate an fd for dwfl_report_offline to swallow. */
1078 : 990 : int dwfl_fd = dup (fd);
1079 [ - + ]: 990 : if (unlikely (dwfl_fd < 0))
1080 : 0 : error_exit (errno, "dup");
1081 : :
1082 : : /* Use libdwfl in a trivial way to open the libdw handle for us.
1083 : : This takes care of applying relocations to DWARF data in ET_REL files. */
1084 : 990 : static const Dwfl_Callbacks callbacks =
1085 : : {
1086 : : .section_address = dwfl_offline_section_address,
1087 : : .find_debuginfo = find_no_debuginfo
1088 : : };
1089 : 990 : Dwfl *dwfl = dwfl_begin (&callbacks);
1090 [ + - ]: 990 : if (likely (dwfl != NULL))
1091 : : /* Let 0 be the logical address of the file (or first in archive). */
1092 : 990 : dwfl->offline_next_address = 0;
1093 [ - + ]: 990 : if (dwfl_report_offline (dwfl, fname, fname, dwfl_fd) == NULL)
1094 : : {
1095 : 0 : struct stat st;
1096 [ # # ]: 0 : if (fstat (dwfl_fd, &st) != 0)
1097 : 0 : error (0, errno, _("cannot stat input file"));
1098 [ # # ]: 0 : else if (unlikely (st.st_size == 0))
1099 : 0 : error (0, 0, _("input file is empty"));
1100 : : else
1101 : 0 : error (0, 0, _("failed reading '%s': %s"),
1102 : : fname, dwfl_errmsg (-1));
1103 : 0 : close (dwfl_fd); /* Consumed on success, not on failure. */
1104 : 0 : dwfl_end (dwfl);
1105 : 0 : dwfl = NULL;
1106 : : }
1107 : : else
1108 : 990 : dwfl_report_end (dwfl, NULL, NULL);
1109 : :
1110 : 990 : return dwfl;
1111 : : }
1112 : :
1113 : : /* Process one input file. */
1114 : : static void
1115 : 976 : process_file (int fd, const char *fname, bool only_one)
1116 : : {
1117 [ + + ]: 976 : if (print_archive_index)
1118 : 4 : check_archive_index (fd, fname, only_one);
1119 : :
1120 [ + + ]: 976 : if (!any_control_option)
1121 : : return;
1122 : :
1123 [ + + ]: 972 : if (elf_input_section != NULL)
1124 : : {
1125 : : /* Replace fname and fd with section content. */
1126 : 10 : char *fnname = alloca (strlen (fname) + strlen (elf_input_section) + 2);
1127 : 10 : sprintf (fnname, "%s:%s", fname, elf_input_section);
1128 : 10 : fd = open_input_section (fd);
1129 [ - + ]: 10 : if (fd == -1)
1130 : : {
1131 : 0 : error (0, 0, _("No such section '%s' in '%s'"),
1132 : : elf_input_section, fname);
1133 : 0 : return;
1134 : : }
1135 : : fname = fnname;
1136 : : }
1137 : :
1138 : 972 : Dwfl *dwfl = create_dwfl (fd, fname);
1139 [ + - ]: 972 : if (dwfl != NULL)
1140 : : {
1141 [ + + ]: 972 : if (only_one)
1142 : : {
1143 : : /* Clear ONLY_ONE if we have multiple modules, from an archive. */
1144 : 872 : bool seen = false;
1145 : 872 : only_one = dwfl_getmodules (dwfl, &count_dwflmod, &seen, 0) == 0;
1146 : : }
1147 : :
1148 : : /* Process the one or more modules gleaned from this file. */
1149 : 972 : struct process_dwflmod_args a = { .fd = fd, .only_one = only_one };
1150 : 972 : dwfl_getmodules (dwfl, &process_dwflmod, &a, 0);
1151 : : }
1152 : : /* Terrible hack for hooking unrelated skeleton/split compile units,
1153 : : see __libdw_link_skel_split in print_debug. */
1154 [ + - ]: 972 : if (! do_not_close_dwfl)
1155 : 972 : dwfl_end (dwfl);
1156 : :
1157 : : /* Need to close the replaced fd if we created it. Caller takes
1158 : : care of original. */
1159 [ + + ]: 972 : if (elf_input_section != NULL)
1160 : 10 : close (fd);
1161 : : }
1162 : :
1163 : : /* Check whether there are any compressed sections in the ELF file. */
1164 : : static bool
1165 : 492 : elf_contains_chdrs (Elf *elf)
1166 : : {
1167 : 492 : Elf_Scn *scn = NULL;
1168 [ + + ]: 797908 : while ((scn = elf_nextscn (elf, scn)) != NULL)
1169 : : {
1170 : 797530 : GElf_Shdr shdr_mem;
1171 : 797530 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
1172 [ + - + + ]: 797530 : if (shdr != NULL && (shdr->sh_flags & SHF_COMPRESSED) != 0)
1173 : 114 : return true;
1174 : : }
1175 : : return false;
1176 : : }
1177 : :
1178 : : /* Process one ELF file. */
1179 : : static void
1180 : 972 : process_elf_file (Dwfl_Module *dwflmod, int fd)
1181 : : {
1182 : 972 : GElf_Addr dwflbias;
1183 : 972 : Elf *elf = dwfl_module_getelf (dwflmod, &dwflbias);
1184 : :
1185 : 972 : GElf_Ehdr ehdr_mem;
1186 : 972 : GElf_Ehdr *ehdr = gelf_getehdr (elf, &ehdr_mem);
1187 : :
1188 [ - + ]: 972 : if (ehdr == NULL)
1189 : : {
1190 : 0 : error (0, 0, _("cannot read ELF header: %s"), elf_errmsg (-1));
1191 : 0 : return;
1192 : : }
1193 : :
1194 : 972 : Ebl *ebl = ebl_openbackend (elf);
1195 [ - + ]: 972 : if (unlikely (ebl == NULL))
1196 : : {
1197 : 0 : ebl_error:
1198 : 0 : error (0, errno, _("cannot create EBL handle"));
1199 : 0 : return;
1200 : : }
1201 : :
1202 : : /* Determine the number of sections. */
1203 [ - + ]: 972 : if (unlikely (elf_getshdrnum (ebl->elf, &shnum) < 0))
1204 : 0 : error_exit (0, _("cannot determine number of sections: %s"),
1205 : : elf_errmsg (-1));
1206 : :
1207 : : /* Determine the number of phdrs. */
1208 [ - + ]: 972 : if (unlikely (elf_getphdrnum (ebl->elf, &phnum) < 0))
1209 : 0 : error_exit (0, _("cannot determine number of program headers: %s"),
1210 : : elf_errmsg (-1));
1211 : :
1212 : : /* For an ET_REL file, libdwfl has adjusted the in-core shdrs and
1213 : : may have applied relocation to some sections. If there are any
1214 : : compressed sections, any pass (or libdw/libdwfl) might have
1215 : : uncompressed them. So we need to get a fresh Elf handle on the
1216 : : file to display those. */
1217 : 2140 : bool print_unchanged = ((print_section_header
1218 [ + + ]: 466 : || print_relocations
1219 [ + + ]: 456 : || dump_data_sections != NULL
1220 [ + + ]: 444 : || print_notes)
1221 [ + + + + ]: 1040 : && (ehdr->e_type == ET_REL
1222 [ + + ]: 492 : || elf_contains_chdrs (ebl->elf)));
1223 : :
1224 : 196 : Elf *pure_elf = NULL;
1225 : 196 : Ebl *pure_ebl = ebl;
1226 : 196 : if (print_unchanged)
1227 : : {
1228 : : /* Read the file afresh. */
1229 : 196 : off_t aroff = elf_getaroff (elf);
1230 : 196 : pure_elf = dwelf_elf_begin (fd);
1231 [ - + ]: 196 : if (aroff > 0)
1232 : : {
1233 : : /* Archive member. */
1234 : 0 : (void) elf_rand (pure_elf, aroff);
1235 : 0 : Elf *armem = elf_begin (-1, ELF_C_READ_MMAP, pure_elf);
1236 : 0 : elf_end (pure_elf);
1237 : 0 : pure_elf = armem;
1238 : : }
1239 [ - + ]: 196 : if (pure_elf == NULL)
1240 : : {
1241 : 0 : error (0, 0, _("cannot read ELF: %s"), elf_errmsg (-1));
1242 : 0 : return;
1243 : : }
1244 : 196 : pure_ebl = ebl_openbackend (pure_elf);
1245 [ - + ]: 196 : if (pure_ebl == NULL)
1246 : 0 : goto ebl_error;
1247 : : }
1248 : :
1249 : 972 : bool symtab_printed = false;
1250 : :
1251 [ + + ]: 972 : if (print_file_header)
1252 : 190 : print_ehdr (ebl, ehdr);
1253 [ + + ]: 972 : if (print_section_header)
1254 : 506 : print_shdr (pure_ebl, ehdr);
1255 [ + + ]: 972 : if (print_program_header)
1256 : 188 : print_phdr (ebl, ehdr);
1257 [ + + ]: 972 : if (print_section_groups)
1258 : 202 : print_scngrp (ebl);
1259 [ + + ]: 972 : if (print_dynamic_table)
1260 : 190 : print_dynamic (ebl);
1261 [ + + ]: 972 : if (print_relocations)
1262 : 194 : print_relocs (pure_ebl, dwflmod, ehdr);
1263 [ + + ]: 972 : if (print_histogram)
1264 : 184 : handle_hash (ebl);
1265 [ + + + + ]: 972 : if (print_symbol_table || print_dynsym_table)
1266 : 220 : symtab_printed |= print_symtab (ebl, SHT_DYNSYM);
1267 [ + + ]: 972 : if (print_version_info)
1268 : 184 : print_verinfo (ebl);
1269 [ + + + - ]: 972 : if (print_symbol_table && !use_dynamic_segment)
1270 : 218 : symtab_printed |= print_symtab (ebl, SHT_SYMTAB);
1271 : :
1272 [ + + + + ]: 972 : if ((print_symbol_table || print_dynsym_table)
1273 [ + + + + ]: 220 : && !symtab_printed && symbol_table_section != NULL)
1274 : 2 : printf ("WARNING: %s: '%s'\n", _("cannot find section"),
1275 : : symbol_table_section);
1276 : :
1277 [ + + ]: 972 : if (print_arch)
1278 : 192 : print_liblist (ebl);
1279 [ + + ]: 972 : if (print_arch)
1280 : 192 : print_attributes (ebl, ehdr);
1281 [ + + ]: 972 : if (dump_data_sections != NULL)
1282 : 12 : dump_data (pure_ebl);
1283 [ + + ]: 972 : if (string_sections != NULL)
1284 : 186 : dump_strings (ebl);
1285 [ + + ]: 972 : if ((print_debug_sections | implicit_debug_sections) != 0)
1286 : 506 : print_debug (dwflmod, ebl, ehdr);
1287 [ + + ]: 972 : if (print_notes)
1288 : 230 : handle_notes (pure_ebl, ehdr);
1289 [ - + ]: 972 : if (print_string_sections)
1290 : 0 : print_strings (ebl);
1291 : :
1292 [ + + ]: 972 : if (pure_ebl != ebl)
1293 : : {
1294 : 196 : ebl_closebackend (ebl);
1295 : 196 : ebl_closebackend (pure_ebl);
1296 : 196 : elf_end (pure_elf);
1297 : : }
1298 : : else
1299 : 776 : ebl_closebackend (ebl);
1300 : : }
1301 : :
1302 : :
1303 : : /* Print file type. */
1304 : : static void
1305 : 190 : print_file_type (unsigned short int e_type)
1306 : : {
1307 [ + - ]: 190 : if (likely (e_type <= ET_CORE))
1308 : : {
1309 : 190 : static const char *const knowntypes[] =
1310 : : {
1311 : : N_("NONE (None)"),
1312 : : N_("REL (Relocatable file)"),
1313 : : N_("EXEC (Executable file)"),
1314 : : N_("DYN (Shared object file)"),
1315 : : N_("CORE (Core file)")
1316 : : };
1317 : 190 : puts (_(knowntypes[e_type]));
1318 : : }
1319 [ # # ]: 0 : else if (e_type >= ET_LOOS && e_type <= ET_HIOS)
1320 : 0 : printf (_("OS Specific: (%x)\n"), e_type);
1321 [ # # ]: 0 : else if (e_type >= ET_LOPROC /* && e_type <= ET_HIPROC always true */)
1322 : 0 : printf (_("Processor Specific: (%x)\n"), e_type);
1323 : : else
1324 : 0 : puts ("???");
1325 : 190 : }
1326 : :
1327 : :
1328 : : /* Print ELF header. */
1329 : : static void
1330 : 190 : print_ehdr (Ebl *ebl, GElf_Ehdr *ehdr)
1331 : : {
1332 : 190 : fputs (_("ELF Header:\n Magic: "), stdout);
1333 [ + + ]: 3230 : for (size_t cnt = 0; cnt < EI_NIDENT; ++cnt)
1334 : 3040 : printf (" %02hhx", ehdr->e_ident[cnt]);
1335 : :
1336 : 190 : printf (_("\n Class: %s\n"),
1337 [ + + ]: 190 : ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? "ELF32"
1338 : : : ehdr->e_ident[EI_CLASS] == ELFCLASS64 ? "ELF64"
1339 [ - + ]: 156 : : "\?\?\?");
1340 : :
1341 : 190 : printf (_(" Data: %s\n"),
1342 [ - + ]: 190 : ehdr->e_ident[EI_DATA] == ELFDATA2LSB
1343 : : ? "2's complement, little endian"
1344 : : : ehdr->e_ident[EI_DATA] == ELFDATA2MSB
1345 [ # # ]: 0 : ? "2's complement, big endian" : "\?\?\?");
1346 : :
1347 : 570 : printf (_(" Ident Version: %hhd %s\n"),
1348 : 190 : ehdr->e_ident[EI_VERSION],
1349 [ + - ]: 190 : ehdr->e_ident[EI_VERSION] == EV_CURRENT ? _("(current)")
1350 : : : "(\?\?\?)");
1351 : :
1352 : 190 : char buf[512];
1353 : 190 : printf (_(" OS/ABI: %s\n"),
1354 : 190 : ebl_osabi_name (ebl, ehdr->e_ident[EI_OSABI], buf, sizeof (buf)));
1355 : :
1356 : 380 : printf (_(" ABI Version: %hhd\n"),
1357 : 190 : ehdr->e_ident[EI_ABIVERSION]);
1358 : :
1359 : 190 : fputs (_(" Type: "), stdout);
1360 : 190 : print_file_type (ehdr->e_type);
1361 : :
1362 : 190 : const char *machine = dwelf_elf_e_machine_string (ehdr->e_machine);
1363 [ + - ]: 190 : if (machine != NULL)
1364 : 190 : printf (_(" Machine: %s\n"), machine);
1365 : : else
1366 : 0 : printf (_(" Machine: <unknown>: 0x%x\n"),
1367 : 0 : ehdr->e_machine);
1368 : :
1369 : 190 : printf (_(" Version: %d %s\n"),
1370 : : ehdr->e_version,
1371 [ + - ]: 190 : ehdr->e_version == EV_CURRENT ? _("(current)") : "(\?\?\?)");
1372 : :
1373 : 190 : printf (_(" Entry point address: %#" PRIx64 "\n"),
1374 : : ehdr->e_entry);
1375 : :
1376 : 190 : printf (_(" Start of program headers: %" PRId64 " %s\n"),
1377 : : ehdr->e_phoff, _("(bytes into file)"));
1378 : :
1379 : 190 : printf (_(" Start of section headers: %" PRId64 " %s\n"),
1380 : : ehdr->e_shoff, _("(bytes into file)"));
1381 : :
1382 : 190 : printf (_(" Flags: %s\n"),
1383 : : ebl_machine_flag_name (ebl, ehdr->e_flags, buf, sizeof (buf)));
1384 : :
1385 : 380 : printf (_(" Size of this header: %" PRIu16 " %s\n"),
1386 : 190 : ehdr->e_ehsize, _("(bytes)"));
1387 : :
1388 : 380 : printf (_(" Size of program header entries: %" PRIu16 " %s\n"),
1389 : 190 : ehdr->e_phentsize, _("(bytes)"));
1390 : :
1391 : 380 : printf (_(" Number of program headers entries: %" PRIu16),
1392 : 190 : ehdr->e_phnum);
1393 [ + + ]: 190 : if (ehdr->e_phnum == PN_XNUM)
1394 : : {
1395 : 2 : GElf_Shdr shdr_mem;
1396 : 2 : GElf_Shdr *shdr = gelf_getshdr (elf_getscn (ebl->elf, 0), &shdr_mem);
1397 [ + - ]: 2 : if (shdr != NULL)
1398 : 2 : printf (_(" (%" PRIu32 " in [0].sh_info)"),
1399 : 2 : (uint32_t) shdr->sh_info);
1400 : : else
1401 : 0 : fputs (_(" ([0] not available)"), stdout);
1402 : : }
1403 : 190 : fputc ('\n', stdout);
1404 : :
1405 : 380 : printf (_(" Size of section header entries: %" PRIu16 " %s\n"),
1406 : 190 : ehdr->e_shentsize, _("(bytes)"));
1407 : :
1408 : 380 : printf (_(" Number of section headers entries: %" PRIu16),
1409 : 190 : ehdr->e_shnum);
1410 [ - + ]: 190 : if (ehdr->e_shnum == 0)
1411 : : {
1412 : 0 : GElf_Shdr shdr_mem;
1413 : 0 : GElf_Shdr *shdr = gelf_getshdr (elf_getscn (ebl->elf, 0), &shdr_mem);
1414 [ # # ]: 0 : if (shdr != NULL)
1415 : 0 : printf (_(" (%" PRIu32 " in [0].sh_size)"),
1416 : 0 : (uint32_t) shdr->sh_size);
1417 : : else
1418 : 0 : fputs (_(" ([0] not available)"), stdout);
1419 : : }
1420 : 190 : fputc ('\n', stdout);
1421 : :
1422 [ - + ]: 190 : if (unlikely (ehdr->e_shstrndx == SHN_XINDEX))
1423 : : {
1424 : 0 : GElf_Shdr shdr_mem;
1425 : 0 : GElf_Shdr *shdr = gelf_getshdr (elf_getscn (ebl->elf, 0), &shdr_mem);
1426 [ # # ]: 0 : if (shdr != NULL)
1427 : : /* We managed to get the zeroth section. */
1428 : 0 : snprintf (buf, sizeof (buf), _(" (%" PRIu32 " in [0].sh_link)"),
1429 : 0 : (uint32_t) shdr->sh_link);
1430 : : else
1431 : : {
1432 : 0 : strncpy (buf, _(" ([0] not available)"), sizeof (buf) - 1);
1433 : 0 : buf[sizeof (buf) - 1] = '\0';
1434 : : }
1435 : :
1436 : 0 : printf (_(" Section header string table index: XINDEX%s\n\n"),
1437 : : buf);
1438 : : }
1439 : : else
1440 : 190 : printf (_(" Section header string table index: %" PRIu16 "\n\n"),
1441 : : ehdr->e_shstrndx);
1442 : 190 : }
1443 : :
1444 : :
1445 : : static const char *
1446 : 88984 : get_visibility_type (int value)
1447 : : {
1448 [ - + - - : 88984 : switch (value)
+ ]
1449 : : {
1450 : : case STV_DEFAULT:
1451 : : return "DEFAULT";
1452 : 0 : case STV_INTERNAL:
1453 : 0 : return "INTERNAL";
1454 : 972 : case STV_HIDDEN:
1455 : 972 : return "HIDDEN";
1456 : 0 : case STV_PROTECTED:
1457 : 0 : return "PROTECTED";
1458 : 0 : default:
1459 : 0 : return "???";
1460 : : }
1461 : : }
1462 : :
1463 : : static const char *
1464 : 384 : elf_ch_type_name (unsigned int code)
1465 : : {
1466 : 384 : switch (code)
1467 : : {
1468 : : case 0:
1469 : : return "NONE";
1470 : 24 : case ELFCOMPRESS_ZLIB:
1471 : 24 : return "ZLIB";
1472 : 360 : case ELFCOMPRESS_ZSTD:
1473 : 360 : return "ZSTD";
1474 : 0 : default:
1475 : 0 : return "UNKNOWN";
1476 : : }
1477 : : }
1478 : :
1479 : : /* Print the section headers. */
1480 : : static void
1481 : 506 : print_shdr (Ebl *ebl, GElf_Ehdr *ehdr)
1482 : : {
1483 : 506 : size_t cnt;
1484 : 506 : size_t shstrndx;
1485 : :
1486 [ + + ]: 506 : if (! print_file_header)
1487 : : {
1488 : 322 : size_t sections;
1489 [ - + ]: 322 : if (unlikely (elf_getshdrnum (ebl->elf, §ions) < 0))
1490 : 0 : error_exit (0, _("cannot get number of sections: %s"),
1491 : : elf_errmsg (-1));
1492 : :
1493 : 322 : printf (_("\
1494 : : There are %zd section headers, starting at offset %#" PRIx64 ":\n\
1495 : : \n"),
1496 : : sections, ehdr->e_shoff);
1497 : : }
1498 : :
1499 : : /* Get the section header string table index. */
1500 [ - + ]: 506 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
1501 : 0 : error_exit (0, _("cannot get section header string table index: %s"),
1502 : : elf_errmsg (-1));
1503 : :
1504 : 506 : puts (_("Section Headers:"));
1505 : :
1506 [ + + ]: 506 : if (ehdr->e_ident[EI_CLASS] == ELFCLASS32)
1507 : 196 : puts (_("[Nr] Name Type Addr Off Size ES Flags Lk Inf Al"));
1508 : : else
1509 : 310 : puts (_("[Nr] Name Type Addr Off Size ES Flags Lk Inf Al"));
1510 : :
1511 [ + + ]: 506 : if (print_decompress)
1512 : : {
1513 [ + + ]: 112 : if (ehdr->e_ident[EI_CLASS] == ELFCLASS32)
1514 : 56 : puts (_(" [Compression Size Al]"));
1515 : : else
1516 : 56 : puts (_(" [Compression Size Al]"));
1517 : : }
1518 : :
1519 [ + + ]: 1586318 : for (cnt = 0; cnt < shnum; ++cnt)
1520 : : {
1521 : 1585812 : Elf_Scn *scn = elf_getscn (ebl->elf, cnt);
1522 : :
1523 [ - + ]: 1585812 : if (unlikely (scn == NULL))
1524 : 0 : error_exit (0, _("cannot get section: %s"),
1525 : : elf_errmsg (-1));
1526 : :
1527 : : /* Get the section header. */
1528 : 1585812 : GElf_Shdr shdr_mem;
1529 : 1585812 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
1530 [ - + ]: 1585812 : if (unlikely (shdr == NULL))
1531 : 0 : error_exit (0, _("cannot get section header: %s"),
1532 : : elf_errmsg (-1));
1533 : :
1534 : 1585812 : char flagbuf[20];
1535 : 1585812 : char *cp = flagbuf;
1536 [ + + ]: 1585812 : if (shdr->sh_flags & SHF_WRITE)
1537 : 2766 : *cp++ = 'W';
1538 [ + + ]: 1585812 : if (shdr->sh_flags & SHF_ALLOC)
1539 : 8522 : *cp++ = 'A';
1540 [ + + ]: 1585812 : if (shdr->sh_flags & SHF_EXECINSTR)
1541 : 1564 : *cp++ = 'X';
1542 [ + + ]: 1585812 : if (shdr->sh_flags & SHF_MERGE)
1543 : 420 : *cp++ = 'M';
1544 [ + + ]: 1585812 : if (shdr->sh_flags & SHF_STRINGS)
1545 : 380 : *cp++ = 'S';
1546 [ + + ]: 1585812 : if (shdr->sh_flags & SHF_INFO_LINK)
1547 : 196 : *cp++ = 'I';
1548 [ + + ]: 1585812 : if (shdr->sh_flags & SHF_LINK_ORDER)
1549 : 4 : *cp++ = 'L';
1550 [ - + ]: 1585812 : if (shdr->sh_flags & SHF_OS_NONCONFORMING)
1551 : 0 : *cp++ = 'N';
1552 [ - + ]: 1585812 : if (shdr->sh_flags & SHF_GROUP)
1553 : 0 : *cp++ = 'G';
1554 [ + + ]: 1585812 : if (shdr->sh_flags & SHF_TLS)
1555 : 32 : *cp++ = 'T';
1556 [ + + ]: 1585812 : if (shdr->sh_flags & SHF_COMPRESSED)
1557 : 414 : *cp++ = 'C';
1558 [ - + ]: 1585812 : if (shdr->sh_flags & SHF_ORDERED)
1559 : 0 : *cp++ = 'O';
1560 [ - + ]: 1585812 : if (shdr->sh_flags & SHF_EXCLUDE)
1561 : 0 : *cp++ = 'E';
1562 [ + + ]: 1585812 : if (shdr->sh_flags & SHF_GNU_RETAIN)
1563 : 2 : *cp++ = 'R';
1564 : 1585812 : *cp = '\0';
1565 : :
1566 : 1585812 : const char *sname;
1567 : 1585812 : char buf[128];
1568 [ - + ]: 1585812 : sname = elf_strptr (ebl->elf, shstrndx, shdr->sh_name) ?: "<corrupt>";
1569 [ + + ]: 3171624 : printf ("[%2zu] %-20s %-12s %0*" PRIx64 " %0*" PRIx64 " %0*" PRIx64
1570 : : " %2" PRId64 " %-5s %2" PRId32 " %3" PRId32
1571 : : " %2" PRId64 "\n",
1572 : : cnt, sname,
1573 : 1585812 : ebl_section_type_name (ebl, shdr->sh_type, buf, sizeof (buf)),
1574 : : ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 8 : 16, shdr->sh_addr,
1575 : : ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 6 : 8, shdr->sh_offset,
1576 [ + + ]: 1585812 : ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 6 : 8, shdr->sh_size,
1577 : : shdr->sh_entsize, flagbuf, shdr->sh_link, shdr->sh_info,
1578 : : shdr->sh_addralign);
1579 : :
1580 [ + + ]: 1585812 : if (print_decompress)
1581 : : {
1582 [ + + ]: 1880 : if ((shdr->sh_flags & SHF_COMPRESSED) != 0)
1583 : : {
1584 : 384 : GElf_Chdr chdr;
1585 [ + - ]: 384 : if (gelf_getchdr (scn, &chdr) != NULL)
1586 [ + + - - ]: 768 : printf (" [ELF %s (%" PRId32 ") %0*" PRIx64
1587 : : " %2" PRId64 "]\n",
1588 : : elf_ch_type_name (chdr.ch_type),
1589 : : chdr.ch_type,
1590 [ + + ]: 384 : ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 6 : 8,
1591 : : chdr.ch_size, chdr.ch_addralign);
1592 : : else
1593 : 0 : error (0, 0,
1594 : 0 : _("bad compression header for section %zd: %s"),
1595 : : elf_ndxscn (scn), elf_errmsg (-1));
1596 : : }
1597 [ + + ]: 1496 : else if (startswith (sname, ".zdebug"))
1598 : : {
1599 : 24 : ssize_t size;
1600 [ + - ]: 24 : if ((size = dwelf_scn_gnu_compressed_size (scn)) >= 0)
1601 : 24 : printf (" [GNU ZLIB %0*zx ]\n",
1602 [ + + ]: 24 : ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 6 : 8, size);
1603 : : else
1604 : 0 : error (0, 0,
1605 : 0 : _("bad gnu compressed size for section %zd: %s"),
1606 : : elf_ndxscn (scn), elf_errmsg (-1));
1607 : : }
1608 : : }
1609 : : }
1610 : :
1611 : 506 : print_flag_info();
1612 : 506 : fputc ('\n', stdout);
1613 : 506 : }
1614 : :
1615 : : /* Print flag information. */
1616 : : static void
1617 : 506 : print_flag_info (void)
1618 : : {
1619 : 506 : puts ("Key to Flags:");
1620 : 506 : puts (" W (write), A (alloc), X (execute), M (merge), S (strings), I (info),");
1621 : 506 : puts (" L (link order), N (extra OS processing required), G (group), T (TLS),");
1622 : 506 : puts (" C (compressed), O (ordered), R (GNU retain), E (exclude)");
1623 : 506 : }
1624 : :
1625 : : /* Print the program header. */
1626 : : static void
1627 : 188 : print_phdr (Ebl *ebl, GElf_Ehdr *ehdr)
1628 : : {
1629 [ + + ]: 188 : if (phnum == 0)
1630 : : /* No program header, this is OK in relocatable objects. */
1631 : 12 : return;
1632 : :
1633 : 176 : puts (_("Program Headers:"));
1634 [ + + ]: 176 : if (ehdr->e_ident[EI_CLASS] == ELFCLASS32)
1635 : 26 : puts (_("\
1636 : : Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align"));
1637 : : else
1638 : 150 : puts (_("\
1639 : : Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align"));
1640 : :
1641 : : /* Process all program headers. */
1642 : : bool has_relro = false;
1643 : : GElf_Addr relro_from = 0;
1644 : : GElf_Addr relro_to = 0;
1645 [ + + ]: 2074 : for (size_t cnt = 0; cnt < phnum; ++cnt)
1646 : : {
1647 : 1898 : char buf[128];
1648 : 1898 : GElf_Phdr mem;
1649 : 1898 : GElf_Phdr *phdr = gelf_getphdr (ebl->elf, cnt, &mem);
1650 : :
1651 : : /* If for some reason the header cannot be returned show this. */
1652 [ - + ]: 1898 : if (unlikely (phdr == NULL))
1653 : : {
1654 : 0 : puts (" ???");
1655 : 0 : continue;
1656 : : }
1657 : :
1658 : 1898 : printf (" %-14s 0x%06" PRIx64 " 0x%0*" PRIx64 " 0x%0*" PRIx64
1659 : : " 0x%06" PRIx64 " 0x%06" PRIx64 " %c%c%c 0x%" PRIx64 "\n",
1660 : 1898 : ebl_segment_type_name (ebl, phdr->p_type, buf, sizeof (buf)),
1661 : : phdr->p_offset,
1662 : : ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 8 : 16, phdr->p_vaddr,
1663 [ + + ]: 1898 : ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 8 : 16, phdr->p_paddr,
1664 : : phdr->p_filesz,
1665 : : phdr->p_memsz,
1666 [ - + ]: 1898 : phdr->p_flags & PF_R ? 'R' : ' ',
1667 [ + + ]: 1898 : phdr->p_flags & PF_W ? 'W' : ' ',
1668 [ + + ]: 1898 : phdr->p_flags & PF_X ? 'E' : ' ',
1669 : : phdr->p_align);
1670 : :
1671 [ + + ]: 1898 : if (phdr->p_type == PT_INTERP)
1672 : : {
1673 : : /* If we are sure the file offset is valid then we can show
1674 : : the user the name of the interpreter. We check whether
1675 : : there is a section at the file offset. Normally there
1676 : : would be a section called ".interp". But in separate
1677 : : .debug files it is a NOBITS section (and so doesn't match
1678 : : with gelf_offscn). Which probably means the offset is
1679 : : not valid another reason could be because the ELF file
1680 : : just doesn't contain any section headers, in that case
1681 : : just play it safe and don't display anything. */
1682 : :
1683 : 172 : Elf_Scn *scn = gelf_offscn (ebl->elf, phdr->p_offset);
1684 : 172 : GElf_Shdr shdr_mem;
1685 : 172 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
1686 : :
1687 : 172 : size_t maxsize;
1688 : 172 : char *filedata = elf_rawfile (ebl->elf, &maxsize);
1689 : :
1690 [ + + + + ]: 172 : if (shdr != NULL && shdr->sh_type == SHT_PROGBITS
1691 [ + - + - ]: 130 : && filedata != NULL && phdr->p_offset < maxsize
1692 [ + - ]: 130 : && phdr->p_filesz <= maxsize - phdr->p_offset
1693 [ + - ]: 130 : && memchr (filedata + phdr->p_offset, '\0',
1694 : : phdr->p_filesz) != NULL)
1695 : 130 : printf (_("\t[Requesting program interpreter: %s]\n"),
1696 : : filedata + phdr->p_offset);
1697 : : }
1698 [ + + ]: 1726 : else if (phdr->p_type == PT_GNU_RELRO)
1699 : : {
1700 : 152 : has_relro = true;
1701 : 152 : relro_from = phdr->p_vaddr;
1702 : 152 : relro_to = relro_from + phdr->p_memsz;
1703 : : }
1704 : : }
1705 : :
1706 : 176 : size_t sections;
1707 [ - + ]: 176 : if (unlikely (elf_getshdrnum (ebl->elf, §ions) < 0))
1708 : 0 : error_exit (0, _("cannot get number of sections: %s"),
1709 : : elf_errmsg (-1));
1710 : :
1711 [ + - ]: 176 : if (sections == 0)
1712 : : /* No sections in the file. Punt. */
1713 : : return;
1714 : :
1715 : : /* Get the section header string table index. */
1716 : 176 : size_t shstrndx;
1717 [ - + ]: 176 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
1718 : 0 : error_exit (0, _("cannot get section header string table index"));
1719 : :
1720 : 176 : puts (_("\n Section to Segment mapping:\n Segment Sections..."));
1721 : :
1722 [ + + ]: 2074 : for (size_t cnt = 0; cnt < phnum; ++cnt)
1723 : : {
1724 : : /* Print the segment number. */
1725 : 1898 : printf (" %2.2zu ", cnt);
1726 : :
1727 : 1898 : GElf_Phdr phdr_mem;
1728 : 1898 : GElf_Phdr *phdr = gelf_getphdr (ebl->elf, cnt, &phdr_mem);
1729 : : /* This must not happen. */
1730 [ - + ]: 1898 : if (unlikely (phdr == NULL))
1731 : 0 : error_exit (0, _("cannot get program header: %s"),
1732 : : elf_errmsg (-1));
1733 : :
1734 : : /* Iterate over the sections. */
1735 : : bool in_relro = false;
1736 : : bool in_ro = false;
1737 [ + + ]: 62236 : for (size_t inner = 1; inner < shnum; ++inner)
1738 : : {
1739 : 60338 : Elf_Scn *scn = elf_getscn (ebl->elf, inner);
1740 : : /* This should not happen. */
1741 [ - + ]: 60338 : if (unlikely (scn == NULL))
1742 : 0 : error_exit (0, _("cannot get section: %s"),
1743 : : elf_errmsg (-1));
1744 : :
1745 : : /* Get the section header. */
1746 : 60338 : GElf_Shdr shdr_mem;
1747 : 60338 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
1748 [ - + ]: 60338 : if (unlikely (shdr == NULL))
1749 : 0 : error_exit (0, _("cannot get section header: %s"),
1750 : : elf_errmsg (-1));
1751 : :
1752 [ + - ]: 60338 : if (shdr->sh_size > 0
1753 : : /* Compare allocated sections by VMA, unallocated
1754 : : sections by file offset. */
1755 [ + + ]: 60338 : && (shdr->sh_flags & SHF_ALLOC
1756 : 47612 : ? (shdr->sh_addr >= phdr->p_vaddr
1757 [ + + ]: 47612 : && (shdr->sh_addr + shdr->sh_size
1758 [ + + ]: 32014 : <= phdr->p_vaddr + phdr->p_memsz))
1759 : 12726 : : (shdr->sh_offset >= phdr->p_offset
1760 [ + + ]: 12726 : && (shdr->sh_offset + shdr->sh_size
1761 [ + + ]: 11200 : <= phdr->p_offset + phdr->p_filesz))))
1762 : : {
1763 [ + + ]: 6268 : if (has_relro && !in_relro
1764 [ + + ]: 4178 : && shdr->sh_addr >= relro_from
1765 [ + + ]: 732 : && shdr->sh_addr + shdr->sh_size <= relro_to)
1766 : : {
1767 : 474 : fputs (" [RELRO:", stdout);
1768 : 474 : in_relro = true;
1769 : : }
1770 [ + + + + ]: 5794 : else if (has_relro && in_relro && shdr->sh_addr >= relro_to)
1771 : : {
1772 : 134 : fputs ("]", stdout);
1773 : 134 : in_relro = false;
1774 : : }
1775 [ + + ]: 5660 : else if (has_relro && in_relro
1776 [ + + ]: 1172 : && shdr->sh_addr + shdr->sh_size > relro_to)
1777 : 18 : fputs ("] <RELRO:", stdout);
1778 [ + + ]: 5642 : else if (phdr->p_type == PT_LOAD && (phdr->p_flags & PF_W) == 0)
1779 : : {
1780 [ + + ]: 3226 : if (!in_ro)
1781 : : {
1782 : 372 : fputs (" [RO:", stdout);
1783 : 372 : in_ro = true;
1784 : : }
1785 : : }
1786 : : else
1787 : : {
1788 : : /* Determine the segment this section is part of. */
1789 : : size_t cnt2;
1790 : : GElf_Phdr phdr2_mem;
1791 : : GElf_Phdr *phdr2 = NULL;
1792 [ + - ]: 10896 : for (cnt2 = 0; cnt2 < phnum; ++cnt2)
1793 : : {
1794 : 10896 : phdr2 = gelf_getphdr (ebl->elf, cnt2, &phdr2_mem);
1795 : :
1796 [ + - + + ]: 10896 : if (phdr2 != NULL && phdr2->p_type == PT_LOAD
1797 [ + - ]: 6176 : && shdr->sh_addr >= phdr2->p_vaddr
1798 : 6176 : && (shdr->sh_addr + shdr->sh_size
1799 [ + + ]: 6176 : <= phdr2->p_vaddr + phdr2->p_memsz))
1800 : : break;
1801 : : }
1802 : :
1803 [ + - ]: 2416 : if (cnt2 < phnum)
1804 : : {
1805 [ + + + + ]: 2416 : if ((phdr2->p_flags & PF_W) == 0 && !in_ro)
1806 : : {
1807 : 656 : fputs (" [RO:", stdout);
1808 : 656 : in_ro = true;
1809 : : }
1810 [ + + + - ]: 1760 : else if ((phdr2->p_flags & PF_W) != 0 && in_ro)
1811 : : {
1812 : 0 : fputs ("]", stdout);
1813 : 0 : in_ro = false;
1814 : : }
1815 : : }
1816 : : }
1817 : :
1818 : 6268 : printf (" %s",
1819 : 6268 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name));
1820 : :
1821 : : /* Signal that this section is only partially covered. */
1822 [ + + ]: 6268 : if (has_relro && in_relro
1823 [ + + ]: 1646 : && shdr->sh_addr + shdr->sh_size > relro_to)
1824 : : {
1825 : 18 : fputs (">", stdout);
1826 : 18 : in_relro = false;
1827 : : }
1828 : : }
1829 : : }
1830 [ + + ]: 1898 : if (in_relro || in_ro)
1831 : 1350 : fputs ("]", stdout);
1832 : :
1833 : : /* Finish the line. */
1834 : 1898 : fputc ('\n', stdout);
1835 : : }
1836 : : }
1837 : :
1838 : :
1839 : : static const char *
1840 : 1260 : section_name (Ebl *ebl, GElf_Shdr *shdr)
1841 : : {
1842 : 1260 : size_t shstrndx;
1843 [ + - - + ]: 1260 : if (shdr == NULL || elf_getshdrstrndx (ebl->elf, &shstrndx) < 0)
1844 : 0 : return "???";
1845 [ - + ]: 1260 : return elf_strptr (ebl->elf, shstrndx, shdr->sh_name) ?: "???";
1846 : : }
1847 : :
1848 : :
1849 : : static void
1850 : 32 : handle_scngrp (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr)
1851 : : {
1852 : : /* Get the data of the section. */
1853 : 32 : Elf_Data *data = elf_getdata (scn, NULL);
1854 : :
1855 : 32 : Elf_Scn *symscn = elf_getscn (ebl->elf, shdr->sh_link);
1856 : 32 : GElf_Shdr symshdr_mem;
1857 : 32 : GElf_Shdr *symshdr = gelf_getshdr (symscn, &symshdr_mem);
1858 : 32 : Elf_Data *symdata = elf_getdata (symscn, NULL);
1859 : :
1860 [ + - + - ]: 32 : if (data == NULL || data->d_size < sizeof (Elf32_Word) || symshdr == NULL
1861 [ - + ]: 32 : || symdata == NULL)
1862 : 0 : return;
1863 : :
1864 : : /* Get the section header string table index. */
1865 : 32 : size_t shstrndx;
1866 [ - + ]: 32 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
1867 : 0 : error_exit (0, _("cannot get section header string table index"));
1868 : :
1869 : 32 : Elf32_Word *grpref = (Elf32_Word *) data->d_buf;
1870 : :
1871 : 32 : GElf_Sym sym_mem;
1872 : 32 : GElf_Sym *sym = gelf_getsym (symdata, shdr->sh_info, &sym_mem);
1873 : :
1874 [ - + + + ]: 64 : printf ((grpref[0] & GRP_COMDAT)
1875 : 8 : ? ngettext ("\
1876 : : \nCOMDAT section group [%2zu] '%s' with signature '%s' contains %zu entry:\n",
1877 : : "\
1878 : : \nCOMDAT section group [%2zu] '%s' with signature '%s' contains %zu entries:\n",
1879 : : data->d_size / sizeof (Elf32_Word) - 1)
1880 : 24 : : ngettext ("\
1881 : : \nSection group [%2zu] '%s' with signature '%s' contains %zu entry:\n", "\
1882 : : \nSection group [%2zu] '%s' with signature '%s' contains %zu entries:\n",
1883 : : data->d_size / sizeof (Elf32_Word) - 1),
1884 : : elf_ndxscn (scn),
1885 : 32 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
1886 : : (sym == NULL ? NULL
1887 : 32 : : elf_strptr (ebl->elf, symshdr->sh_link, sym->st_name))
1888 : 0 : ?: _("<INVALID SYMBOL>"),
1889 [ + - ]: 32 : data->d_size / sizeof (Elf32_Word) - 1);
1890 : :
1891 [ + + ]: 104 : for (size_t cnt = 1; cnt < data->d_size / sizeof (Elf32_Word); ++cnt)
1892 : : {
1893 : 72 : GElf_Shdr grpshdr_mem;
1894 : 72 : GElf_Shdr *grpshdr = gelf_getshdr (elf_getscn (ebl->elf, grpref[cnt]),
1895 : : &grpshdr_mem);
1896 : :
1897 : 72 : const char *str;
1898 [ + - ]: 144 : printf (" [%2u] %s\n",
1899 : : grpref[cnt],
1900 : : grpshdr != NULL
1901 [ - + ]: 72 : && (str = elf_strptr (ebl->elf, shstrndx, grpshdr->sh_name))
1902 : 0 : ? str : _("<INVALID SECTION>"));
1903 : : }
1904 : : }
1905 : :
1906 : :
1907 : : static void
1908 : 202 : print_scngrp (Ebl *ebl)
1909 : : {
1910 : : /* Find all relocation sections and handle them. */
1911 : 202 : Elf_Scn *scn = NULL;
1912 : :
1913 [ + + ]: 6524 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
1914 : : {
1915 : : /* Handle the section if it is a symbol table. */
1916 : 6322 : GElf_Shdr shdr_mem;
1917 : 6322 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
1918 : :
1919 [ + - + + ]: 6322 : if (shdr != NULL && shdr->sh_type == SHT_GROUP)
1920 : : {
1921 [ - + ]: 32 : if ((shdr->sh_flags & SHF_COMPRESSED) != 0)
1922 : : {
1923 [ # # ]: 0 : if (elf_compress (scn, 0, 0) < 0)
1924 : 0 : printf ("WARNING: %s [%zd]\n",
1925 : : _("Couldn't uncompress section"),
1926 : : elf_ndxscn (scn));
1927 : 0 : shdr = gelf_getshdr (scn, &shdr_mem);
1928 [ # # ]: 0 : if (unlikely (shdr == NULL))
1929 : 0 : error_exit (0, _("cannot get section [%zd] header: %s"),
1930 : : elf_ndxscn (scn),
1931 : : elf_errmsg (-1));
1932 : : }
1933 : 32 : handle_scngrp (ebl, scn, shdr);
1934 : : }
1935 : : }
1936 : 202 : }
1937 : :
1938 : :
1939 : : static const struct flags
1940 : : {
1941 : : int mask;
1942 : : const char *str;
1943 : : } dt_flags[] =
1944 : : {
1945 : : { DF_ORIGIN, "ORIGIN" },
1946 : : { DF_SYMBOLIC, "SYMBOLIC" },
1947 : : { DF_TEXTREL, "TEXTREL" },
1948 : : { DF_BIND_NOW, "BIND_NOW" },
1949 : : { DF_STATIC_TLS, "STATIC_TLS" }
1950 : : };
1951 : : static const int ndt_flags = sizeof (dt_flags) / sizeof (dt_flags[0]);
1952 : :
1953 : : static const struct flags dt_flags_1[] =
1954 : : {
1955 : : { DF_1_NOW, "NOW" },
1956 : : { DF_1_GLOBAL, "GLOBAL" },
1957 : : { DF_1_GROUP, "GROUP" },
1958 : : { DF_1_NODELETE, "NODELETE" },
1959 : : { DF_1_LOADFLTR, "LOADFLTR" },
1960 : : { DF_1_INITFIRST, "INITFIRST" },
1961 : : { DF_1_NOOPEN, "NOOPEN" },
1962 : : { DF_1_ORIGIN, "ORIGIN" },
1963 : : { DF_1_DIRECT, "DIRECT" },
1964 : : { DF_1_TRANS, "TRANS" },
1965 : : { DF_1_INTERPOSE, "INTERPOSE" },
1966 : : { DF_1_NODEFLIB, "NODEFLIB" },
1967 : : { DF_1_NODUMP, "NODUMP" },
1968 : : { DF_1_CONFALT, "CONFALT" },
1969 : : { DF_1_ENDFILTEE, "ENDFILTEE" },
1970 : : { DF_1_DISPRELDNE, "DISPRELDNE" },
1971 : : { DF_1_DISPRELPND, "DISPRELPND" },
1972 : : };
1973 : : static const int ndt_flags_1 = sizeof (dt_flags_1) / sizeof (dt_flags_1[0]);
1974 : :
1975 : : static const struct flags dt_feature_1[] =
1976 : : {
1977 : : { DTF_1_PARINIT, "PARINIT" },
1978 : : { DTF_1_CONFEXP, "CONFEXP" }
1979 : : };
1980 : : static const int ndt_feature_1 = (sizeof (dt_feature_1)
1981 : : / sizeof (dt_feature_1[0]));
1982 : :
1983 : : static const struct flags dt_posflag_1[] =
1984 : : {
1985 : : { DF_P1_LAZYLOAD, "LAZYLOAD" },
1986 : : { DF_P1_GROUPPERM, "GROUPPERM" }
1987 : : };
1988 : : static const int ndt_posflag_1 = (sizeof (dt_posflag_1)
1989 : : / sizeof (dt_posflag_1[0]));
1990 : :
1991 : :
1992 : : static void
1993 : 74 : print_flags (int class, GElf_Xword d_val, const struct flags *flags,
1994 : : int nflags)
1995 : : {
1996 : 74 : bool first = true;
1997 : 74 : int cnt;
1998 : :
1999 [ + + ]: 1104 : for (cnt = 0; cnt < nflags; ++cnt)
2000 [ + + ]: 1030 : if (d_val & flags[cnt].mask)
2001 : : {
2002 [ + + ]: 68 : if (!first)
2003 : 36 : putchar (' ');
2004 : 68 : fputs (flags[cnt].str, stdout);
2005 : 68 : d_val &= ~flags[cnt].mask;
2006 : 68 : first = false;
2007 : : }
2008 : :
2009 [ + + ]: 74 : if (d_val != 0)
2010 : : {
2011 [ + + ]: 62 : if (!first)
2012 : 20 : putchar (' ');
2013 [ + + ]: 116 : printf ("%#0*" PRIx64, class == ELFCLASS32 ? 10 : 18, d_val);
2014 : : }
2015 : :
2016 : 74 : putchar ('\n');
2017 : 74 : }
2018 : :
2019 : :
2020 : : static void
2021 : 14 : print_dt_flags (int class, GElf_Xword d_val)
2022 : : {
2023 : 14 : print_flags (class, d_val, dt_flags, ndt_flags);
2024 : 14 : }
2025 : :
2026 : :
2027 : : static void
2028 : 56 : print_dt_flags_1 (int class, GElf_Xword d_val)
2029 : : {
2030 : 56 : print_flags (class, d_val, dt_flags_1, ndt_flags_1);
2031 : 56 : }
2032 : :
2033 : :
2034 : : static void
2035 : 2 : print_dt_feature_1 (int class, GElf_Xword d_val)
2036 : : {
2037 : 2 : print_flags (class, d_val, dt_feature_1, ndt_feature_1);
2038 : 2 : }
2039 : :
2040 : :
2041 : : static void
2042 : 2 : print_dt_posflag_1 (int class, GElf_Xword d_val)
2043 : : {
2044 : 2 : print_flags (class, d_val, dt_posflag_1, ndt_posflag_1);
2045 : 2 : }
2046 : :
2047 : :
2048 : : static size_t
2049 : 136 : get_dyn_ents (Elf_Data * dyn_data)
2050 : : {
2051 : 136 : GElf_Dyn *dyn;
2052 : 136 : GElf_Dyn dyn_mem;
2053 : 136 : size_t dyn_idx = 0;
2054 : 3414 : do
2055 : : {
2056 : 3414 : dyn = gelf_getdyn(dyn_data, dyn_idx, &dyn_mem);
2057 [ + - ]: 3414 : if (dyn != NULL)
2058 : 3414 : ++dyn_idx;
2059 : : }
2060 [ + + ]: 3414 : while (dyn != NULL && dyn->d_tag != DT_NULL);
2061 : :
2062 : 136 : return dyn_idx;
2063 : : }
2064 : :
2065 : :
2066 : : static void
2067 : 136 : handle_dynamic (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr, GElf_Phdr *phdr)
2068 : : {
2069 : 136 : int class = gelf_getclass (ebl->elf);
2070 : 136 : GElf_Shdr glink_mem;
2071 : 136 : GElf_Shdr *glink;
2072 : 136 : Elf_Data *data;
2073 : 136 : size_t cnt;
2074 : 136 : size_t shstrndx;
2075 : 136 : size_t dyn_ents;
2076 : :
2077 : : /* Get the data of the section. */
2078 [ + + + - ]: 136 : if (use_dynamic_segment && phdr != NULL)
2079 : 2 : data = elf_getdata_rawchunk(ebl->elf, phdr->p_offset,
2080 : : phdr->p_filesz, ELF_T_DYN);
2081 : : else
2082 : 134 : data = elf_getdata (scn, NULL);
2083 : :
2084 [ - + ]: 136 : if (data == NULL)
2085 : 0 : return;
2086 : :
2087 : : /* Get the dynamic section entry number */
2088 : 136 : dyn_ents = get_dyn_ents (data);
2089 : :
2090 [ + + + - ]: 136 : if (!use_dynamic_segment && shdr != NULL)
2091 : : {
2092 : : /* Get the section header string table index. */
2093 [ - + ]: 134 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
2094 : 0 : error_exit (0, _("cannot get section header string table index"));
2095 : :
2096 : 134 : glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link), &glink_mem);
2097 [ - + ]: 134 : if (glink == NULL)
2098 : 0 : error_exit (0, _("invalid sh_link value in section %zu"),
2099 : : elf_ndxscn (scn));
2100 : :
2101 : 134 : printf (ngettext ("\
2102 : : \nDynamic segment contains %lu entry:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'\n",
2103 : : "\
2104 : : \nDynamic segment contains %lu entries:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'\n",
2105 : : dyn_ents),
2106 : : (unsigned long int) dyn_ents,
2107 : : class == ELFCLASS32 ? 10 : 18, shdr->sh_addr,
2108 : : shdr->sh_offset,
2109 [ + + ]: 134 : (int) shdr->sh_link,
2110 : 134 : elf_strptr (ebl->elf, shstrndx, glink->sh_name));
2111 : : }
2112 [ + - ]: 2 : else if (phdr != NULL)
2113 : : {
2114 [ + - ]: 4 : printf (ngettext ("\
2115 : : \nDynamic segment contains %lu entry:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 "\n",
2116 : : "\
2117 : : \nDynamic segment contains %lu entries:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 "\n",
2118 : : dyn_ents),
2119 : : (unsigned long int) dyn_ents,
2120 : : class == ELFCLASS32 ? 10 : 18, phdr->p_paddr,
2121 : : phdr->p_offset);
2122 : : }
2123 : :
2124 : 136 : fputs (_(" Type Value\n"), stdout);
2125 : :
2126 : : /* if --use-dynamic option is enabled,
2127 : : use the string table to get the related library info. */
2128 : 136 : Elf_Data *strtab_data = NULL;
2129 [ + + + - ]: 136 : if (use_dynamic_segment && phdr != NULL)
2130 : : {
2131 : 2 : strtab_data = get_dynscn_strtab(ebl->elf, phdr);
2132 [ - + ]: 2 : if (strtab_data == NULL)
2133 : 0 : error_exit (0, _("cannot get string table by using dynamic segment"));
2134 : : }
2135 : :
2136 [ + + ]: 3550 : for (cnt = 0; cnt < dyn_ents; ++cnt)
2137 : : {
2138 : 3414 : GElf_Dyn dynmem;
2139 : 3414 : GElf_Dyn *dyn = gelf_getdyn (data, cnt, &dynmem);
2140 [ + - ]: 3414 : if (dyn == NULL)
2141 : : break;
2142 : :
2143 : 3414 : char buf[64];
2144 : 3414 : printf (" %-17s ",
2145 : : ebl_dynamic_tag_name (ebl, dyn->d_tag, buf, sizeof (buf)));
2146 : :
2147 : 3414 : char *name = NULL;
2148 : 3414 : if (dyn->d_tag == DT_NEEDED
2149 [ + + ]: 3414 : || dyn->d_tag == DT_SONAME
2150 : : || dyn->d_tag == DT_RPATH
2151 : : || dyn->d_tag == DT_RUNPATH)
2152 : : {
2153 [ + + + - ]: 236 : if (! use_dynamic_segment && shdr != NULL)
2154 : 232 : name = elf_strptr (ebl->elf, shdr->sh_link, dyn->d_un.d_val);
2155 [ - + ]: 4 : else if (dyn->d_un.d_val < strtab_data->d_size
2156 : 4 : && memrchr (strtab_data->d_buf + dyn->d_un.d_val, '\0',
2157 [ - + ]: 4 : strtab_data->d_size - 1 - dyn->d_un.d_val) != NULL)
2158 : 3414 : name = ((char *) strtab_data->d_buf) + dyn->d_un.d_val;
2159 : : }
2160 : :
2161 [ + + + + : 3414 : switch (dyn->d_tag)
+ + + + +
+ + + + ]
2162 : : {
2163 : 268 : case DT_NULL:
2164 : : case DT_DEBUG:
2165 : : case DT_BIND_NOW:
2166 : : case DT_TEXTREL:
2167 : : /* No further output. */
2168 : 268 : fputc ('\n', stdout);
2169 : 268 : break;
2170 : :
2171 : 226 : case DT_NEEDED:
2172 : 226 : printf (_("Shared library: [%s]\n"), name);
2173 : 226 : break;
2174 : :
2175 : 6 : case DT_SONAME:
2176 : 6 : printf (_("Library soname: [%s]\n"), name);
2177 : 6 : break;
2178 : :
2179 : 2 : case DT_RPATH:
2180 : 2 : printf (_("Library rpath: [%s]\n"), name);
2181 : 2 : break;
2182 : :
2183 : 2 : case DT_RUNPATH:
2184 : 2 : printf (_("Library runpath: [%s]\n"), name);
2185 : 2 : break;
2186 : :
2187 : 902 : case DT_PLTRELSZ:
2188 : : case DT_RELASZ:
2189 : : case DT_STRSZ:
2190 : : case DT_RELSZ:
2191 : : case DT_RELRSZ:
2192 : : case DT_RELAENT:
2193 : : case DT_SYMENT:
2194 : : case DT_RELENT:
2195 : : case DT_RELRENT:
2196 : : case DT_PLTPADSZ:
2197 : : case DT_MOVEENT:
2198 : : case DT_MOVESZ:
2199 : : case DT_INIT_ARRAYSZ:
2200 : : case DT_FINI_ARRAYSZ:
2201 : : case DT_SYMINSZ:
2202 : : case DT_SYMINENT:
2203 : : case DT_GNU_CONFLICTSZ:
2204 : : case DT_GNU_LIBLISTSZ:
2205 : 902 : printf (_("%" PRId64 " (bytes)\n"), dyn->d_un.d_val);
2206 : 902 : break;
2207 : :
2208 : 212 : case DT_VERDEFNUM:
2209 : : case DT_VERNEEDNUM:
2210 : : case DT_RELACOUNT:
2211 : : case DT_RELCOUNT:
2212 : 212 : printf ("%" PRId64 "\n", dyn->d_un.d_val);
2213 : 212 : break;
2214 : :
2215 : 108 : case DT_PLTREL:;
2216 : 108 : const char *tagname = ebl_dynamic_tag_name (ebl, dyn->d_un.d_val,
2217 : : NULL, 0);
2218 [ + + ]: 108 : puts (tagname ?: "???");
2219 : 108 : break;
2220 : :
2221 : 14 : case DT_FLAGS:
2222 : 14 : print_dt_flags (class, dyn->d_un.d_val);
2223 : 14 : break;
2224 : :
2225 : 56 : case DT_FLAGS_1:
2226 : 56 : print_dt_flags_1 (class, dyn->d_un.d_val);
2227 : 56 : break;
2228 : :
2229 : 2 : case DT_FEATURE_1:
2230 : 2 : print_dt_feature_1 (class, dyn->d_un.d_val);
2231 : 2 : break;
2232 : :
2233 : 2 : case DT_POSFLAG_1:
2234 : 2 : print_dt_posflag_1 (class, dyn->d_un.d_val);
2235 : 2 : break;
2236 : :
2237 : 1614 : default:
2238 [ + + ]: 1614 : printf ("%#0*" PRIx64 "\n",
2239 : : class == ELFCLASS32 ? 10 : 18, dyn->d_un.d_val);
2240 : 1614 : break;
2241 : : }
2242 : : }
2243 : : }
2244 : :
2245 : :
2246 : : /* Print the dynamic segment. */
2247 : : static void
2248 : 190 : print_dynamic (Ebl *ebl)
2249 : : {
2250 [ + + ]: 1072 : for (size_t i = 0; i < phnum; ++i)
2251 : : {
2252 : 1060 : GElf_Phdr phdr_mem;
2253 : 1060 : GElf_Phdr *phdr = gelf_getphdr (ebl->elf, i, &phdr_mem);
2254 : :
2255 [ + - + + ]: 1060 : if (phdr != NULL && phdr->p_type == PT_DYNAMIC)
2256 : : {
2257 : 178 : Elf_Scn *scn = gelf_offscn (ebl->elf, phdr->p_offset);
2258 : 178 : GElf_Shdr shdr_mem;
2259 : 178 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
2260 [ + + ]: 178 : if ((use_dynamic_segment && phdr != NULL)
2261 [ + + + - ]: 176 : || (shdr != NULL && shdr->sh_type == SHT_DYNAMIC))
2262 : 136 : handle_dynamic (ebl, scn, shdr, phdr);
2263 : 178 : break;
2264 : : }
2265 : : }
2266 : 190 : }
2267 : :
2268 : :
2269 : : /* Print relocations. */
2270 : : static void
2271 : 194 : print_relocs (Ebl *ebl, Dwfl_Module *mod, GElf_Ehdr *ehdr)
2272 : : {
2273 : : /* Find all relocation sections and handle them. */
2274 : 194 : Elf_Scn *scn = NULL;
2275 : :
2276 [ + + ]: 530468 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
2277 : : {
2278 : : /* Handle the section if it is a symbol table. */
2279 : 530274 : GElf_Shdr shdr_mem;
2280 : 530274 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
2281 : :
2282 [ + - ]: 530274 : if (likely (shdr != NULL))
2283 : : {
2284 [ + + ]: 530274 : if (shdr->sh_type == SHT_REL)
2285 : 92 : handle_relocs_rel (ebl, ehdr, scn, shdr);
2286 [ + + ]: 530182 : else if (shdr->sh_type == SHT_RELA)
2287 : 282 : handle_relocs_rela (ebl, ehdr, scn, shdr);
2288 [ - + ]: 529900 : else if (shdr->sh_type == SHT_RELR)
2289 : 0 : handle_relocs_relr (ebl, mod, scn, shdr);
2290 : : }
2291 : : }
2292 : 194 : }
2293 : :
2294 : :
2295 : : /* Handle a relocation section. */
2296 : : static void
2297 : 92 : handle_relocs_rel (Ebl *ebl, GElf_Ehdr *ehdr, Elf_Scn *scn, GElf_Shdr *shdr)
2298 : : {
2299 : 92 : int class = gelf_getclass (ebl->elf);
2300 : 92 : size_t sh_entsize = gelf_fsize (ebl->elf, ELF_T_REL, 1, EV_CURRENT);
2301 : 92 : int nentries = shdr->sh_size / sh_entsize;
2302 : :
2303 : : /* Get the data of the section. */
2304 : 92 : Elf_Data *data = elf_getdata (scn, NULL);
2305 [ + - ]: 92 : if (data == NULL)
2306 : 0 : return;
2307 : :
2308 : : /* Get the symbol table information. */
2309 : 92 : Elf_Scn *symscn = elf_getscn (ebl->elf, shdr->sh_link);
2310 : 92 : GElf_Shdr symshdr_mem;
2311 : 92 : GElf_Shdr *symshdr = gelf_getshdr (symscn, &symshdr_mem);
2312 : 92 : Elf_Data *symdata = elf_getdata (symscn, NULL);
2313 : :
2314 : : /* Get the section header of the section the relocations are for. */
2315 : 92 : GElf_Shdr destshdr_mem;
2316 : 92 : GElf_Shdr *destshdr = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_info),
2317 : : &destshdr_mem);
2318 : :
2319 [ + - - + ]: 92 : if (unlikely (symshdr == NULL || symdata == NULL || destshdr == NULL))
2320 : : {
2321 : 0 : printf (_("\nInvalid symbol table at offset %#0" PRIx64 "\n"),
2322 : : shdr->sh_offset);
2323 : 0 : return;
2324 : : }
2325 : :
2326 : : /* Search for the optional extended section index table if there are
2327 : : more than 64k sections. */
2328 : 92 : Elf_Data *xndxdata = NULL;
2329 [ - + ]: 92 : int xndxscnidx = shnum >= SHN_LORESERVE ? elf_scnshndx (symscn) : 0;
2330 [ # # ]: 0 : if (unlikely (xndxscnidx > 0))
2331 : 0 : xndxdata = elf_getdata (elf_getscn (ebl->elf, xndxscnidx), NULL);
2332 : :
2333 : : /* Get the section header string table index. */
2334 : 92 : size_t shstrndx;
2335 [ - + ]: 92 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
2336 : 0 : error_exit (0, _("cannot get section header string table index"));
2337 : :
2338 [ + + ]: 92 : if (shdr->sh_info != 0)
2339 : 76 : printf (ngettext ("\
2340 : : \nRelocation section [%2zu] '%s' for section [%2u] '%s' at offset %#0" PRIx64 " contains %d entry:\n",
2341 : : "\
2342 : : \nRelocation section [%2zu] '%s' for section [%2u] '%s' at offset %#0" PRIx64 " contains %d entries:\n",
2343 : : nentries),
2344 : : elf_ndxscn (scn),
2345 : 76 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
2346 : 76 : (unsigned int) shdr->sh_info,
2347 : 76 : elf_strptr (ebl->elf, shstrndx, destshdr->sh_name),
2348 : : shdr->sh_offset,
2349 : : nentries);
2350 : : else
2351 : : /* The .rel.dyn section does not refer to a specific section but
2352 : : instead of section index zero. Do not try to print a section
2353 : : name. */
2354 : 16 : printf (ngettext ("\
2355 : : \nRelocation section [%2u] '%s' at offset %#0" PRIx64 " contains %d entry:\n",
2356 : : "\
2357 : : \nRelocation section [%2u] '%s' at offset %#0" PRIx64 " contains %d entries:\n",
2358 : : nentries),
2359 : 16 : (unsigned int) elf_ndxscn (scn),
2360 : 16 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
2361 : : shdr->sh_offset,
2362 : : nentries);
2363 [ + - ]: 92 : fputs (class == ELFCLASS32
2364 : 92 : ? _("\
2365 : : Offset Type Value Name\n")
2366 : 0 : : _("\
2367 : : Offset Type Value Name\n"),
2368 : : stdout);
2369 : :
2370 : 92 : int is_statically_linked = 0;
2371 [ + + ]: 25942 : for (int cnt = 0; cnt < nentries; ++cnt)
2372 : : {
2373 : 25850 : GElf_Rel relmem;
2374 : 25850 : GElf_Rel *rel = gelf_getrel (data, cnt, &relmem);
2375 [ + - ]: 25850 : if (likely (rel != NULL))
2376 : : {
2377 : 25850 : char buf[128];
2378 : 25850 : GElf_Sym symmem;
2379 : 25850 : Elf32_Word xndx;
2380 : 51700 : GElf_Sym *sym = gelf_getsymshndx (symdata, xndxdata,
2381 : 25850 : GELF_R_SYM (rel->r_info),
2382 : : &symmem, &xndx);
2383 [ - + ]: 25850 : if (unlikely (sym == NULL))
2384 : : {
2385 : : /* As a special case we have to handle relocations in static
2386 : : executables. This only happens for IRELATIVE relocations
2387 : : (so far). There is no symbol table. */
2388 [ # # ]: 0 : if (is_statically_linked == 0)
2389 : : {
2390 : : /* Find the program header and look for a PT_INTERP entry. */
2391 : 0 : is_statically_linked = -1;
2392 [ # # ]: 0 : if (ehdr->e_type == ET_EXEC)
2393 : : {
2394 : 0 : is_statically_linked = 1;
2395 : :
2396 [ # # ]: 0 : for (size_t inner = 0; inner < phnum; ++inner)
2397 : : {
2398 : 0 : GElf_Phdr phdr_mem;
2399 : 0 : GElf_Phdr *phdr = gelf_getphdr (ebl->elf, inner,
2400 : : &phdr_mem);
2401 [ # # # # ]: 0 : if (phdr != NULL && phdr->p_type == PT_INTERP)
2402 : : {
2403 : 0 : is_statically_linked = -1;
2404 : 0 : break;
2405 : : }
2406 : : }
2407 : : }
2408 : : }
2409 : :
2410 [ # # # # ]: 0 : if (is_statically_linked > 0 && shdr->sh_link == 0)
2411 [ # # # # ]: 0 : printf ("\
2412 : : %#0*" PRIx64 " %-20s %*s %s\n",
2413 : : class == ELFCLASS32 ? 10 : 18, rel->r_offset,
2414 : 0 : ebl_reloc_type_check (ebl, GELF_R_TYPE (rel->r_info))
2415 : : /* Avoid the leading R_ which isn't carrying any
2416 : : information. */
2417 : 0 : ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info),
2418 : : buf, sizeof (buf)) + 2
2419 : 0 : : _("<INVALID RELOC>"),
2420 : : class == ELFCLASS32 ? 10 : 18, "",
2421 : 0 : elf_strptr (ebl->elf, shstrndx, destshdr->sh_name));
2422 : : else
2423 [ # # # # ]: 0 : printf (" %#0*" PRIx64 " %-20s <%s %ld>\n",
2424 : : class == ELFCLASS32 ? 10 : 18, rel->r_offset,
2425 : 0 : ebl_reloc_type_check (ebl, GELF_R_TYPE (rel->r_info))
2426 : : /* Avoid the leading R_ which isn't carrying any
2427 : : information. */
2428 : 0 : ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info),
2429 : : buf, sizeof (buf)) + 2
2430 : 0 : : _("<INVALID RELOC>"),
2431 : : _("INVALID SYMBOL"),
2432 : 0 : (long int) GELF_R_SYM (rel->r_info));
2433 : : }
2434 [ + + ]: 25850 : else if (GELF_ST_TYPE (sym->st_info) != STT_SECTION
2435 [ + + ]: 130 : && !(GELF_ST_TYPE (sym->st_info) == STT_NOTYPE
2436 [ - + ]: 66 : && GELF_ST_BIND (sym->st_info) == STB_LOCAL
2437 [ # # ]: 0 : && sym->st_shndx != SHN_UNDEF
2438 [ # # ]: 0 : && sym->st_value == 0)) // local start section label
2439 [ - + ]: 260 : printf (" %#0*" PRIx64 " %-20s %#0*" PRIx64 " %s\n",
2440 : : class == ELFCLASS32 ? 10 : 18, rel->r_offset,
2441 [ + - ]: 130 : likely (ebl_reloc_type_check (ebl,
2442 : : GELF_R_TYPE (rel->r_info)))
2443 : : /* Avoid the leading R_ which isn't carrying any
2444 : : information. */
2445 : 130 : ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info),
2446 : : buf, sizeof (buf)) + 2
2447 : 0 : : _("<INVALID RELOC>"),
2448 : : class == ELFCLASS32 ? 10 : 18, sym->st_value,
2449 : 130 : elf_strptr (ebl->elf, symshdr->sh_link, sym->st_name));
2450 : : else
2451 : : {
2452 : : /* This is a relocation against a STT_SECTION symbol
2453 : : or a local start section label for which we print
2454 : : section name. */
2455 : 25720 : GElf_Shdr secshdr_mem;
2456 : 25720 : GElf_Shdr *secshdr;
2457 : 25720 : secshdr = gelf_getshdr (elf_getscn (ebl->elf,
2458 [ + - ]: 25720 : sym->st_shndx == SHN_XINDEX
2459 : 0 : ? xndx : sym->st_shndx),
2460 : : &secshdr_mem);
2461 : :
2462 [ - + ]: 25720 : if (unlikely (secshdr == NULL))
2463 [ # # # # ]: 0 : printf (" %#0*" PRIx64 " %-20s <%s %ld>\n",
2464 : : class == ELFCLASS32 ? 10 : 18, rel->r_offset,
2465 : 0 : ebl_reloc_type_check (ebl, GELF_R_TYPE (rel->r_info))
2466 : : /* Avoid the leading R_ which isn't carrying any
2467 : : information. */
2468 : 0 : ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info),
2469 : : buf, sizeof (buf)) + 2
2470 : 0 : : _("<INVALID RELOC>"),
2471 : : _("INVALID SECTION"),
2472 [ # # ]: 0 : (long int) (sym->st_shndx == SHN_XINDEX
2473 : 0 : ? xndx : sym->st_shndx));
2474 : : else
2475 [ - + + - ]: 77160 : printf (" %#0*" PRIx64 " %-20s %#0*" PRIx64 " %s\n",
2476 : : class == ELFCLASS32 ? 10 : 18, rel->r_offset,
2477 : 25720 : ebl_reloc_type_check (ebl, GELF_R_TYPE (rel->r_info))
2478 : : /* Avoid the leading R_ which isn't carrying any
2479 : : information. */
2480 : 25720 : ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info),
2481 : : buf, sizeof (buf)) + 2
2482 : 0 : : _("<INVALID RELOC>"),
2483 : : class == ELFCLASS32 ? 10 : 18, sym->st_value,
2484 : 25720 : elf_strptr (ebl->elf, shstrndx, secshdr->sh_name));
2485 : : }
2486 : : }
2487 : : }
2488 : : }
2489 : :
2490 : :
2491 : : /* Handle a relocation section. */
2492 : : static void
2493 : 282 : handle_relocs_rela (Ebl *ebl, GElf_Ehdr *ehdr, Elf_Scn *scn, GElf_Shdr *shdr)
2494 : : {
2495 : 282 : int class = gelf_getclass (ebl->elf);
2496 : 282 : size_t sh_entsize = gelf_fsize (ebl->elf, ELF_T_RELA, 1, EV_CURRENT);
2497 : 282 : int nentries = shdr->sh_size / sh_entsize;
2498 : :
2499 : : /* Get the data of the section. */
2500 : 282 : Elf_Data *data = elf_getdata (scn, NULL);
2501 [ + - ]: 282 : if (data == NULL)
2502 : 0 : return;
2503 : :
2504 : : /* Get the symbol table information. */
2505 : 282 : Elf_Scn *symscn = elf_getscn (ebl->elf, shdr->sh_link);
2506 : 282 : GElf_Shdr symshdr_mem;
2507 : 282 : GElf_Shdr *symshdr = gelf_getshdr (symscn, &symshdr_mem);
2508 : 282 : Elf_Data *symdata = elf_getdata (symscn, NULL);
2509 : :
2510 : : /* Get the section header of the section the relocations are for. */
2511 : 282 : GElf_Shdr destshdr_mem;
2512 : 282 : GElf_Shdr *destshdr = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_info),
2513 : : &destshdr_mem);
2514 : :
2515 [ + - - + ]: 282 : if (unlikely (symshdr == NULL || symdata == NULL || destshdr == NULL))
2516 : : {
2517 : 0 : printf (_("\nInvalid symbol table at offset %#0" PRIx64 "\n"),
2518 : : shdr->sh_offset);
2519 : 0 : return;
2520 : : }
2521 : :
2522 : : /* Search for the optional extended section index table if there are
2523 : : more than 64k sections. */
2524 : 282 : Elf_Data *xndxdata = NULL;
2525 [ + + ]: 282 : int xndxscnidx = shnum >= SHN_LORESERVE ? elf_scnshndx (symscn) : 0;
2526 [ - + ]: 16 : if (unlikely (xndxscnidx > 0))
2527 : 16 : xndxdata = elf_getdata (elf_getscn (ebl->elf, xndxscnidx), NULL);
2528 : :
2529 : : /* Get the section header string table index. */
2530 : 282 : size_t shstrndx;
2531 [ - + ]: 282 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
2532 : 0 : error_exit (0, _("cannot get section header string table index"));
2533 : :
2534 [ + + ]: 282 : if (shdr->sh_info != 0)
2535 : 168 : printf (ngettext ("\
2536 : : \nRelocation section [%2zu] '%s' for section [%2u] '%s' at offset %#0" PRIx64 " contains %d entry:\n",
2537 : : "\
2538 : : \nRelocation section [%2zu] '%s' for section [%2u] '%s' at offset %#0" PRIx64 " contains %d entries:\n",
2539 : : nentries),
2540 : : elf_ndxscn (scn),
2541 : 168 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
2542 : 168 : (unsigned int) shdr->sh_info,
2543 : 168 : elf_strptr (ebl->elf, shstrndx, destshdr->sh_name),
2544 : : shdr->sh_offset,
2545 : : nentries);
2546 : : else
2547 : : /* The .rela.dyn section does not refer to a specific section but
2548 : : instead of section index zero. Do not try to print a section
2549 : : name. */
2550 : 114 : printf (ngettext ("\
2551 : : \nRelocation section [%2u] '%s' at offset %#0" PRIx64 " contains %d entry:\n",
2552 : : "\
2553 : : \nRelocation section [%2u] '%s' at offset %#0" PRIx64 " contains %d entries:\n",
2554 : : nentries),
2555 : 114 : (unsigned int) elf_ndxscn (scn),
2556 : 114 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
2557 : : shdr->sh_offset,
2558 : : nentries);
2559 [ - + ]: 282 : fputs (class == ELFCLASS32
2560 : 0 : ? _("\
2561 : : Offset Type Value Addend Name\n")
2562 : 282 : : _("\
2563 : : Offset Type Value Addend Name\n"),
2564 : : stdout);
2565 : :
2566 : 282 : int is_statically_linked = 0;
2567 [ + + ]: 1182086 : for (int cnt = 0; cnt < nentries; ++cnt)
2568 : : {
2569 : 1181804 : GElf_Rela relmem;
2570 : 1181804 : GElf_Rela *rel = gelf_getrela (data, cnt, &relmem);
2571 [ + - ]: 1181804 : if (likely (rel != NULL))
2572 : : {
2573 : 1181804 : char buf[64];
2574 : 1181804 : GElf_Sym symmem;
2575 : 1181804 : Elf32_Word xndx;
2576 : 2363608 : GElf_Sym *sym = gelf_getsymshndx (symdata, xndxdata,
2577 : 1181804 : GELF_R_SYM (rel->r_info),
2578 : : &symmem, &xndx);
2579 : :
2580 [ - + ]: 1181804 : if (unlikely (sym == NULL))
2581 : : {
2582 : : /* As a special case we have to handle relocations in static
2583 : : executables. This only happens for IRELATIVE relocations
2584 : : (so far). There is no symbol table. */
2585 [ # # ]: 0 : if (is_statically_linked == 0)
2586 : : {
2587 : : /* Find the program header and look for a PT_INTERP entry. */
2588 : 0 : is_statically_linked = -1;
2589 [ # # ]: 0 : if (ehdr->e_type == ET_EXEC)
2590 : : {
2591 : 0 : is_statically_linked = 1;
2592 : :
2593 [ # # ]: 0 : for (size_t inner = 0; inner < phnum; ++inner)
2594 : : {
2595 : 0 : GElf_Phdr phdr_mem;
2596 : 0 : GElf_Phdr *phdr = gelf_getphdr (ebl->elf, inner,
2597 : : &phdr_mem);
2598 [ # # # # ]: 0 : if (phdr != NULL && phdr->p_type == PT_INTERP)
2599 : : {
2600 : 0 : is_statically_linked = -1;
2601 : 0 : break;
2602 : : }
2603 : : }
2604 : : }
2605 : : }
2606 : :
2607 [ # # # # ]: 0 : if (is_statically_linked > 0 && shdr->sh_link == 0)
2608 [ # # # # ]: 0 : printf ("\
2609 : : %#0*" PRIx64 " %-15s %*s %#6" PRIx64 " %s\n",
2610 : : class == ELFCLASS32 ? 10 : 18, rel->r_offset,
2611 : 0 : ebl_reloc_type_check (ebl, GELF_R_TYPE (rel->r_info))
2612 : : /* Avoid the leading R_ which isn't carrying any
2613 : : information. */
2614 : 0 : ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info),
2615 : : buf, sizeof (buf)) + 2
2616 : 0 : : _("<INVALID RELOC>"),
2617 : : class == ELFCLASS32 ? 10 : 18, "",
2618 : : rel->r_addend,
2619 : 0 : elf_strptr (ebl->elf, shstrndx, destshdr->sh_name));
2620 : : else
2621 [ # # # # ]: 0 : printf (" %#0*" PRIx64 " %-15s <%s %ld>\n",
2622 : : class == ELFCLASS32 ? 10 : 18, rel->r_offset,
2623 : 0 : ebl_reloc_type_check (ebl, GELF_R_TYPE (rel->r_info))
2624 : : /* Avoid the leading R_ which isn't carrying any
2625 : : information. */
2626 : 0 : ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info),
2627 : : buf, sizeof (buf)) + 2
2628 : 0 : : _("<INVALID RELOC>"),
2629 : : _("INVALID SYMBOL"),
2630 : 0 : (long int) GELF_R_SYM (rel->r_info));
2631 : : }
2632 [ + + ]: 1181804 : else if (GELF_ST_TYPE (sym->st_info) != STT_SECTION
2633 [ + + ]: 643918 : && !(GELF_ST_TYPE (sym->st_info) == STT_NOTYPE
2634 [ + + ]: 117072 : && GELF_ST_BIND (sym->st_info) == STB_LOCAL
2635 [ + + ]: 115534 : && sym->st_shndx != SHN_UNDEF
2636 [ + + ]: 716 : && sym->st_value == 0)) // local start section label
2637 [ + - ]: 1287812 : printf ("\
2638 : : %#0*" PRIx64 " %-15s %#0*" PRIx64 " %+6" PRId64 " %s\n",
2639 : : class == ELFCLASS32 ? 10 : 18, rel->r_offset,
2640 [ + - ]: 643906 : likely (ebl_reloc_type_check (ebl,
2641 : : GELF_R_TYPE (rel->r_info)))
2642 : : /* Avoid the leading R_ which isn't carrying any
2643 : : information. */
2644 : 643906 : ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info),
2645 : : buf, sizeof (buf)) + 2
2646 : 0 : : _("<INVALID RELOC>"),
2647 : : class == ELFCLASS32 ? 10 : 18, sym->st_value,
2648 : : rel->r_addend,
2649 : 643906 : elf_strptr (ebl->elf, symshdr->sh_link, sym->st_name));
2650 : : else
2651 : : {
2652 : : /* This is a relocation against a STT_SECTION symbol
2653 : : or a local start section label for which we print
2654 : : section name. */
2655 : 537898 : GElf_Shdr secshdr_mem;
2656 : 537898 : GElf_Shdr *secshdr;
2657 : 537898 : secshdr = gelf_getshdr (elf_getscn (ebl->elf,
2658 [ + + ]: 537898 : sym->st_shndx == SHN_XINDEX
2659 : 2088 : ? xndx : sym->st_shndx),
2660 : : &secshdr_mem);
2661 : :
2662 [ - + ]: 537898 : if (unlikely (secshdr == NULL))
2663 [ # # # # ]: 0 : printf (" %#0*" PRIx64 " %-15s <%s %ld>\n",
2664 : : class == ELFCLASS32 ? 10 : 18, rel->r_offset,
2665 : 0 : ebl_reloc_type_check (ebl, GELF_R_TYPE (rel->r_info))
2666 : : /* Avoid the leading R_ which isn't carrying any
2667 : : information. */
2668 : 0 : ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info),
2669 : : buf, sizeof (buf)) + 2
2670 : 0 : : _("<INVALID RELOC>"),
2671 : : _("INVALID SECTION"),
2672 [ # # ]: 0 : (long int) (sym->st_shndx == SHN_XINDEX
2673 : 0 : ? xndx : sym->st_shndx));
2674 : : else
2675 [ + - + - ]: 1613694 : printf ("\
2676 : : %#0*" PRIx64 " %-15s %#0*" PRIx64 " %+6" PRId64 " %s\n",
2677 : : class == ELFCLASS32 ? 10 : 18, rel->r_offset,
2678 : 537898 : ebl_reloc_type_check (ebl, GELF_R_TYPE (rel->r_info))
2679 : : /* Avoid the leading R_ which isn't carrying any
2680 : : information. */
2681 : 537898 : ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info),
2682 : : buf, sizeof (buf)) + 2
2683 : 0 : : _("<INVALID RELOC>"),
2684 : : class == ELFCLASS32 ? 10 : 18, sym->st_value,
2685 : : rel->r_addend,
2686 : 537898 : elf_strptr (ebl->elf, shstrndx, secshdr->sh_name));
2687 : : }
2688 : : }
2689 : : }
2690 : : }
2691 : :
2692 : : /* Handle a relocation section. */
2693 : : static void
2694 : 0 : handle_relocs_relr (Ebl *ebl, Dwfl_Module *mod, Elf_Scn *scn, GElf_Shdr *shdr)
2695 : : {
2696 : 0 : int class = gelf_getclass (ebl->elf);
2697 : 0 : size_t sh_entsize = gelf_fsize (ebl->elf, ELF_T_RELR, 1, EV_CURRENT);
2698 : 0 : int nentries = shdr->sh_size / sh_entsize;
2699 : :
2700 : : /* Get the data of the section. */
2701 : 0 : Elf_Data *data = elf_getdata (scn, NULL);
2702 [ # # ]: 0 : if (data == NULL)
2703 : 0 : return;
2704 : :
2705 : : /* Get the section header string table index. */
2706 : 0 : size_t shstrndx;
2707 [ # # ]: 0 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
2708 : 0 : error_exit (0, _("cannot get section header string table index"));
2709 : :
2710 : : /* A .relr.dyn section does not refer to a specific section. */
2711 : 0 : printf (ngettext ("\
2712 : : \nRelocation section [%2u] '%s' at offset %#0" PRIx64 " contains %d entry:\n",
2713 : : "\
2714 : : \nRelocation section [%2u] '%s' at offset %#0" PRIx64 " contains %d entries:\n",
2715 : : nentries),
2716 : 0 : (unsigned int) elf_ndxscn (scn),
2717 : 0 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
2718 : : shdr->sh_offset,
2719 : : nentries);
2720 : :
2721 [ # # ]: 0 : if (class == ELFCLASS32)
2722 : : {
2723 : : uint32_t base = 0;
2724 [ # # ]: 0 : for (int cnt = 0; cnt < nentries; ++cnt)
2725 : : {
2726 : 0 : Elf32_Word *words = data->d_buf;
2727 : 0 : Elf32_Word entry = words[cnt];
2728 : :
2729 : : /* Just the raw entries? */
2730 [ # # ]: 0 : if (print_unresolved_addresses)
2731 : 0 : printf (" %#010" PRIx32 "%s\n", entry,
2732 [ # # ]: 0 : (entry & 1) == 0 ? " *" : "");
2733 : : else
2734 : : {
2735 : : /* A real address, also sets base. */
2736 [ # # ]: 0 : if ((entry & 1) == 0)
2737 : : {
2738 : 0 : printf (" ");
2739 : 0 : print_dwarf_addr (mod, 4, entry, entry, stdout);
2740 : 0 : printf (" *\n");
2741 : :
2742 : 0 : base = entry + 4;
2743 : : }
2744 : : else
2745 : : {
2746 : : /* Untangle address from base and bits. */
2747 : : uint32_t addr;
2748 [ # # ]: 0 : for (addr = base; (entry >>= 1) != 0; addr += 4)
2749 [ # # ]: 0 : if ((entry & 1) != 0)
2750 : : {
2751 : 0 : printf (" ");
2752 : 0 : print_dwarf_addr (mod, 4, addr, addr, stdout);
2753 : 0 : printf ("\n");
2754 : : }
2755 : 0 : base += 4 * (4 * 8 - 1);
2756 : : }
2757 : : }
2758 : : }
2759 : : }
2760 : : else
2761 : : {
2762 : : uint64_t base = 0;
2763 [ # # ]: 0 : for (int cnt = 0; cnt < nentries; ++cnt)
2764 : : {
2765 : 0 : Elf64_Xword *xwords = data->d_buf;
2766 : 0 : Elf64_Xword entry = xwords[cnt];
2767 : :
2768 : : /* Just the raw entries? */
2769 [ # # ]: 0 : if (print_unresolved_addresses)
2770 : 0 : printf (" %#018" PRIx64 "%s\n", entry,
2771 [ # # ]: 0 : (entry & 1) == 0 ? " *" : "");
2772 : : else
2773 : : {
2774 : : /* A real address, also sets base. */
2775 [ # # ]: 0 : if ((entry & 1) == 0)
2776 : : {
2777 : 0 : printf (" ");
2778 : 0 : print_dwarf_addr (mod, 8, entry, entry, stdout);
2779 : 0 : printf (" *\n");
2780 : :
2781 : 0 : base = entry + 8;
2782 : : }
2783 : : else
2784 : : {
2785 : : /* Untangle address from base and bits. */
2786 : : uint64_t addr;
2787 [ # # ]: 0 : for (addr = base; (entry >>= 1) != 0; addr += 8)
2788 [ # # ]: 0 : if ((entry & 1) != 0)
2789 : : {
2790 : 0 : printf (" ");
2791 : 0 : print_dwarf_addr (mod, 8, addr, addr, stdout);
2792 : 0 : printf ("\n");
2793 : : }
2794 : 0 : base += 8 * (8 * 8 - 1);
2795 : : }
2796 : : }
2797 : : }
2798 : : }
2799 : : }
2800 : :
2801 : : /* Print the program header. Return true if a symtab is printed,
2802 : : false otherwise. */
2803 : : static bool
2804 : 438 : print_symtab (Ebl *ebl, int type)
2805 : : {
2806 : : /* Use the dynamic section info to display symbol tables. */
2807 [ + - - - ]: 438 : if (use_dynamic_segment && type == SHT_DYNSYM)
2808 : 0 : return handle_dynamic_symtab(ebl);
2809 : :
2810 : : /* Find the symbol table(s). For this we have to search through the
2811 : : section table. */
2812 : : Elf_Scn *scn = NULL;
2813 : : bool symtab_printed = false;
2814 : :
2815 [ + + ]: 14060 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
2816 : : {
2817 : : /* Handle the section if it is a symbol table. */
2818 : 13622 : GElf_Shdr shdr_mem;
2819 : 13622 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
2820 : :
2821 [ + - + + ]: 13622 : if (shdr != NULL && shdr->sh_type == (GElf_Word) type)
2822 : : {
2823 [ + + ]: 274 : if (symbol_table_section != NULL)
2824 : : {
2825 : : /* Get the section header string table index. */
2826 : 14 : size_t shstrndx;
2827 : 14 : const char *sname;
2828 [ - + ]: 14 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
2829 : 0 : error_exit (0,
2830 : : _("cannot get section header string table index"));
2831 : 14 : sname = elf_strptr (ebl->elf, shstrndx, shdr->sh_name);
2832 [ + - + + ]: 14 : if (sname == NULL || strcmp (sname, symbol_table_section) != 0)
2833 : 8 : continue;
2834 : : }
2835 : :
2836 [ - + ]: 266 : if ((shdr->sh_flags & SHF_COMPRESSED) != 0)
2837 : : {
2838 [ # # ]: 0 : if (elf_compress (scn, 0, 0) < 0)
2839 : 0 : printf ("WARNING: %s [%zd]\n",
2840 : : _("Couldn't uncompress section"),
2841 : : elf_ndxscn (scn));
2842 : 0 : shdr = gelf_getshdr (scn, &shdr_mem);
2843 [ # # ]: 0 : if (unlikely (shdr == NULL))
2844 : 0 : error_exit (0,
2845 : : _("cannot get section [%zd] header: %s"),
2846 : : elf_ndxscn (scn), elf_errmsg (-1));
2847 : : }
2848 : 266 : symtab_printed = handle_symtab (ebl, scn, shdr);
2849 : : }
2850 : : }
2851 : :
2852 : : return symtab_printed;
2853 : : }
2854 : :
2855 : :
2856 : : static void
2857 : 266 : process_symtab (Ebl *ebl, unsigned int nsyms, Elf64_Word idx,
2858 : : Elf32_Word verneed_stridx, Elf32_Word verdef_stridx,
2859 : : Elf_Data *symdata, Elf_Data *versym_data,
2860 : : Elf_Data *symstr_data, Elf_Data *verneed_data,
2861 : : Elf_Data *verdef_data, Elf_Data *xndx_data)
2862 : : {
2863 [ + + ]: 89250 : for (unsigned int cnt = 0; cnt < nsyms; ++cnt)
2864 : : {
2865 : 88984 : char typebuf[64];
2866 : 88984 : char bindbuf[64];
2867 : 88984 : char scnbuf[64];
2868 : 88984 : const char *sym_name;
2869 : 88984 : Elf32_Word xndx;
2870 : 88984 : GElf_Sym sym_mem;
2871 : 88984 : GElf_Sym *sym
2872 : 88984 : = gelf_getsymshndx (symdata, xndx_data, cnt, &sym_mem, &xndx);
2873 : :
2874 [ - + ]: 88984 : if (unlikely (sym == NULL))
2875 : 0 : continue;
2876 : :
2877 : : /* Determine the real section index. */
2878 [ + - ]: 88984 : if (likely (sym->st_shndx != SHN_XINDEX))
2879 : 88984 : xndx = sym->st_shndx;
2880 [ - + ]: 88984 : if (use_dynamic_segment == true)
2881 : : {
2882 [ # # ]: 0 : if (symstr_data != NULL
2883 [ # # ]: 0 : && validate_str (symstr_data->d_buf, sym->st_name,
2884 : : symstr_data->d_size))
2885 : 0 : sym_name = (char *)symstr_data->d_buf + sym->st_name;
2886 : : else
2887 : : sym_name = NULL;
2888 : : }
2889 : : else
2890 : 88984 : sym_name = elf_strptr (ebl->elf, idx, sym->st_name);
2891 : :
2892 [ - + ]: 88984 : if (sym_name == NULL)
2893 : : sym_name = "???";
2894 : :
2895 [ + + ]: 177080 : printf (_ ("\
2896 : : %5u: %0*" PRIx64 " %6" PRId64 " %-7s %-6s %-9s %6s %s"),
2897 : 88984 : cnt, gelf_getclass (ebl->elf) == ELFCLASS32 ? 8 : 16,
2898 : : sym->st_value, sym->st_size,
2899 : 88984 : ebl_symbol_type_name (ebl, GELF_ST_TYPE (sym->st_info), typebuf,
2900 : : sizeof (typebuf)),
2901 : 88984 : ebl_symbol_binding_name (ebl, GELF_ST_BIND (sym->st_info),
2902 : : bindbuf, sizeof (bindbuf)),
2903 : 88984 : get_visibility_type (GELF_ST_VISIBILITY (sym->st_other)),
2904 : 88984 : ebl_section_name (ebl, sym->st_shndx, xndx, scnbuf,
2905 : : sizeof (scnbuf), NULL, shnum),
2906 : : sym_name);
2907 : :
2908 [ + + ]: 88984 : if (versym_data != NULL)
2909 : : {
2910 : : /* Get the version information. */
2911 : 3526 : GElf_Versym versym_mem;
2912 : 3526 : GElf_Versym *versym = gelf_getversym (versym_data, cnt, &versym_mem);
2913 : :
2914 [ + - + + ]: 3526 : if (versym != NULL && ((*versym & 0x8000) != 0 || *versym > 1))
2915 : : {
2916 : 2566 : bool is_nobits = false;
2917 : 2566 : bool check_def = xndx != SHN_UNDEF;
2918 : :
2919 [ + + - + ]: 2566 : if (xndx < SHN_LORESERVE || sym->st_shndx == SHN_XINDEX)
2920 : : {
2921 : 2546 : GElf_Shdr symshdr_mem;
2922 : 2546 : GElf_Shdr *symshdr = gelf_getshdr (
2923 : : elf_getscn (ebl->elf, xndx), &symshdr_mem);
2924 : :
2925 : 2546 : is_nobits
2926 [ + - + + ]: 5068 : = (symshdr != NULL && symshdr->sh_type == SHT_NOBITS);
2927 : : }
2928 : :
2929 [ + + ]: 2566 : if (is_nobits || !check_def)
2930 : : {
2931 : : /* We must test both. */
2932 : 2262 : GElf_Vernaux vernaux_mem;
2933 : 2262 : GElf_Vernaux *vernaux = NULL;
2934 : 2262 : size_t vn_offset = 0;
2935 : :
2936 : 2262 : GElf_Verneed verneed_mem;
2937 : 2262 : GElf_Verneed *verneed
2938 : 2262 : = gelf_getverneed (verneed_data, 0, &verneed_mem);
2939 [ + - ]: 8262 : while (verneed != NULL)
2940 : : {
2941 : 8262 : size_t vna_offset = vn_offset;
2942 : :
2943 : 8262 : vernaux = gelf_getvernaux (verneed_data,
2944 : 8262 : vna_offset += verneed->vn_aux,
2945 : : &vernaux_mem);
2946 [ + + ]: 30384 : while (vernaux != NULL && vernaux->vna_other != *versym
2947 [ + + ]: 19860 : && vernaux->vna_next != 0
2948 [ + - ]: 22122 : && (verneed_data->d_size - vna_offset
2949 [ + - ]: 13860 : >= vernaux->vna_next))
2950 : : {
2951 : : /* Update the offset. */
2952 : 13860 : vna_offset += vernaux->vna_next;
2953 : :
2954 : 13860 : vernaux = (vernaux->vna_next == 0
2955 : : ? NULL
2956 : 13860 : : gelf_getvernaux (verneed_data,
2957 : : vna_offset,
2958 : : &vernaux_mem));
2959 : : }
2960 : :
2961 : : /* Check whether we found the version. */
2962 [ + - + + ]: 8262 : if (vernaux != NULL && vernaux->vna_other == *versym)
2963 : : /* Found it. */
2964 : : break;
2965 : :
2966 [ + - ]: 6000 : if (verneed_data->d_size - vn_offset < verneed->vn_next)
2967 : : break;
2968 : :
2969 : 6000 : vn_offset += verneed->vn_next;
2970 : 6000 : verneed
2971 : : = (verneed->vn_next == 0
2972 : : ? NULL
2973 [ + - ]: 6000 : : gelf_getverneed (verneed_data, vn_offset,
2974 : : &verneed_mem));
2975 : : }
2976 : :
2977 [ + - + - ]: 2262 : if (vernaux != NULL && vernaux->vna_other == *versym)
2978 : : {
2979 : 4524 : printf ("@%s (%u)",
2980 [ - + ]: 2262 : use_dynamic_segment == true
2981 : 0 : ? (char *)symstr_data->d_buf
2982 : 0 : + vernaux->vna_name
2983 : 2262 : : elf_strptr (ebl->elf, verneed_stridx,
2984 : 2262 : vernaux->vna_name),
2985 : : (unsigned int)vernaux->vna_other);
2986 : 2262 : check_def = 0;
2987 : : }
2988 [ # # ]: 0 : else if (unlikely (!is_nobits))
2989 : 0 : error (0, 0, _ ("bad dynamic symbol"));
2990 : : else
2991 : : check_def = 1;
2992 : : }
2993 : :
2994 [ + - + - ]: 2566 : if (check_def && *versym != 0x8001)
2995 : : {
2996 : : /* We must test both. */
2997 : 304 : size_t vd_offset = 0;
2998 : :
2999 : 304 : GElf_Verdef verdef_mem;
3000 : 304 : GElf_Verdef *verdef
3001 : 304 : = gelf_getverdef (verdef_data, 0, &verdef_mem);
3002 [ + - ]: 866 : while (verdef != NULL)
3003 : : {
3004 [ + + ]: 866 : if (verdef->vd_ndx == (*versym & 0x7fff))
3005 : : /* Found the definition. */
3006 : : break;
3007 : :
3008 [ + - ]: 562 : if (verdef_data->d_size - vd_offset < verdef->vd_next)
3009 : : break;
3010 : :
3011 : 562 : vd_offset += verdef->vd_next;
3012 : 562 : verdef = (verdef->vd_next == 0
3013 : : ? NULL
3014 [ + - ]: 562 : : gelf_getverdef (verdef_data, vd_offset,
3015 : : &verdef_mem));
3016 : : }
3017 : :
3018 [ + - ]: 304 : if (verdef != NULL)
3019 : : {
3020 : 304 : GElf_Verdaux verdaux_mem;
3021 : 608 : GElf_Verdaux *verdaux = gelf_getverdaux (
3022 : 304 : verdef_data, vd_offset + verdef->vd_aux,
3023 : : &verdaux_mem);
3024 : :
3025 [ + - ]: 304 : if (verdaux != NULL)
3026 [ + - ]: 608 : printf ((*versym & 0x8000) ? "@%s" : "@@%s",
3027 [ - + ]: 304 : use_dynamic_segment == true
3028 : 0 : ? (char *)symstr_data->d_buf
3029 : 0 : + verdaux->vda_name
3030 : 304 : : elf_strptr (ebl->elf, verdef_stridx,
3031 : 304 : verdaux->vda_name));
3032 : : }
3033 : : }
3034 : : }
3035 : : }
3036 : :
3037 : 88984 : putchar ('\n');
3038 : : }
3039 : 266 : }
3040 : :
3041 : :
3042 : : static bool
3043 : 266 : handle_symtab (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr)
3044 : : {
3045 : 266 : Elf_Data *versym_data = NULL;
3046 : 266 : Elf_Data *verneed_data = NULL;
3047 : 266 : Elf_Data *verdef_data = NULL;
3048 : 266 : Elf_Data *xndx_data = NULL;
3049 : 266 : int class = gelf_getclass (ebl->elf);
3050 : 266 : Elf32_Word verneed_stridx = 0;
3051 : 266 : Elf32_Word verdef_stridx = 0;
3052 : :
3053 : : /* Get the data of the section. */
3054 : 266 : Elf_Data *data = elf_getdata (scn, NULL);
3055 [ + - ]: 266 : if (data == NULL)
3056 : : return false;
3057 : :
3058 : : /* Find out whether we have other sections we might need. */
3059 : : Elf_Scn *runscn = NULL;
3060 [ + + ]: 8788 : while ((runscn = elf_nextscn (ebl->elf, runscn)) != NULL)
3061 : : {
3062 : 8522 : GElf_Shdr runshdr_mem;
3063 : 8522 : GElf_Shdr *runshdr = gelf_getshdr (runscn, &runshdr_mem);
3064 : :
3065 [ + - ]: 8522 : if (likely (runshdr != NULL))
3066 : : {
3067 [ + + ]: 8522 : if (runshdr->sh_type == SHT_GNU_versym
3068 [ + + ]: 202 : && runshdr->sh_link == elf_ndxscn (scn))
3069 : : /* Bingo, found the version information. Now get the data. */
3070 : 150 : versym_data = elf_getdata (runscn, NULL);
3071 [ + + ]: 8372 : else if (runshdr->sh_type == SHT_GNU_verneed)
3072 : : {
3073 : : /* This is the information about the needed versions. */
3074 : 202 : verneed_data = elf_getdata (runscn, NULL);
3075 : 202 : verneed_stridx = runshdr->sh_link;
3076 : : }
3077 [ + + ]: 8170 : else if (runshdr->sh_type == SHT_GNU_verdef)
3078 : : {
3079 : : /* This is the information about the defined versions. */
3080 : 8 : verdef_data = elf_getdata (runscn, NULL);
3081 : 8 : verdef_stridx = runshdr->sh_link;
3082 : : }
3083 [ + - ]: 8162 : else if (runshdr->sh_type == SHT_SYMTAB_SHNDX
3084 [ # # ]: 0 : && runshdr->sh_link == elf_ndxscn (scn))
3085 : : /* Extended section index. */
3086 : 0 : xndx_data = elf_getdata (runscn, NULL);
3087 : : }
3088 : : }
3089 : :
3090 : : /* Get the section header string table index. */
3091 : 266 : size_t shstrndx;
3092 [ - + ]: 266 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
3093 : 0 : error_exit (0, _("cannot get section header string table index"));
3094 : :
3095 : 266 : GElf_Shdr glink_mem;
3096 : 266 : GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
3097 : : &glink_mem);
3098 [ - + ]: 266 : if (glink == NULL)
3099 : 0 : error_exit (0, _("invalid sh_link value in section %zu"),
3100 : : elf_ndxscn (scn));
3101 : :
3102 : : /* Now we can compute the number of entries in the section. */
3103 : 532 : unsigned int nsyms = data->d_size / (class == ELFCLASS32
3104 : : ? sizeof (Elf32_Sym)
3105 [ + + ]: 266 : : sizeof (Elf64_Sym));
3106 : :
3107 : 266 : printf (ngettext ("\nSymbol table [%2u] '%s' contains %u entry:\n",
3108 : : "\nSymbol table [%2u] '%s' contains %u entries:\n",
3109 : : nsyms),
3110 : 266 : (unsigned int) elf_ndxscn (scn),
3111 : 266 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name), nsyms);
3112 : 266 : printf (ngettext (" %lu local symbol String table: [%2u] '%s'\n",
3113 : : " %lu local symbols String table: [%2u] '%s'\n",
3114 : : shdr->sh_info),
3115 : 266 : (unsigned long int) shdr->sh_info,
3116 : 266 : (unsigned int) shdr->sh_link,
3117 : 266 : elf_strptr (ebl->elf, shstrndx, glink->sh_name));
3118 : :
3119 [ + + ]: 532 : fputs (class == ELFCLASS32
3120 : 32 : ? _("\
3121 : : Num: Value Size Type Bind Vis Ndx Name\n")
3122 : 234 : : _("\
3123 : : Num: Value Size Type Bind Vis Ndx Name\n"),
3124 : : stdout);
3125 : :
3126 : 266 : process_symtab(ebl, nsyms, shdr->sh_link, verneed_stridx, verdef_stridx,
3127 : : data, versym_data, NULL, verneed_data, verdef_data, xndx_data);
3128 : 266 : return true;
3129 : : }
3130 : :
3131 : :
3132 : : static bool
3133 : 0 : handle_dynamic_symtab (Ebl *ebl)
3134 : : {
3135 : 0 : GElf_Phdr phdr_mem;
3136 : 0 : GElf_Phdr *phdr = NULL;
3137 : : /* phnum is a static variable which was already fetched in function
3138 : : process_elf_file. */
3139 [ # # ]: 0 : for (size_t i = 0; i < phnum; ++i)
3140 : : {
3141 : 0 : phdr = gelf_getphdr (ebl->elf, i, &phdr_mem);
3142 [ # # # # ]: 0 : if (phdr == NULL || phdr->p_type == PT_DYNAMIC)
3143 : : break;
3144 : : }
3145 [ # # ]: 0 : if (phdr == NULL)
3146 : : return false;
3147 : :
3148 : 0 : GElf_Addr addrs[i_max] = {
3149 : : 0,
3150 : : };
3151 : 0 : GElf_Off offs[i_max] = {
3152 : : 0,
3153 : : };
3154 : 0 : get_dynscn_addrs (ebl->elf, phdr, addrs);
3155 : 0 : find_offsets (ebl->elf, 0, i_max, addrs, offs);
3156 : :
3157 : 0 : size_t syments = 0;
3158 : :
3159 : 0 : GElf_Ehdr ehdr_mem;
3160 : 0 : GElf_Ehdr *ehdr = gelf_getehdr (ebl->elf, &ehdr_mem);
3161 : :
3162 [ # # ]: 0 : if (offs[i_hash] != 0)
3163 : : {
3164 : : /* In the original format, .hash says the size of .dynsym. */
3165 : :
3166 [ # # # # : 0 : size_t entsz = SH_ENTSIZE_HASH (ehdr);
# # ]
3167 : : Elf_Data *data
3168 : 0 : = elf_getdata_rawchunk (ebl->elf, offs[i_hash] + entsz, entsz,
3169 : : (entsz == 4 ? ELF_T_WORD : ELF_T_XWORD));
3170 [ # # ]: 0 : if (data != NULL)
3171 : 0 : syments = (entsz == 4 ? *(const GElf_Word *)data->d_buf
3172 [ # # ]: 0 : : *(const GElf_Xword *)data->d_buf);
3173 : : }
3174 [ # # # # ]: 0 : if (offs[i_gnu_hash] != 0 && syments == 0)
3175 : : {
3176 : : /* In the new format, we can derive it with some work. */
3177 : :
3178 : 0 : const struct
3179 : : {
3180 : : Elf32_Word nbuckets;
3181 : : Elf32_Word symndx;
3182 : : Elf32_Word maskwords;
3183 : : Elf32_Word shift2;
3184 : : } * header;
3185 : :
3186 : 0 : Elf_Data *data = elf_getdata_rawchunk (ebl->elf, offs[i_gnu_hash],
3187 : : sizeof *header, ELF_T_WORD);
3188 [ # # ]: 0 : if (data != NULL)
3189 : : {
3190 : 0 : header = data->d_buf;
3191 : 0 : Elf32_Word nbuckets = header->nbuckets;
3192 : 0 : Elf32_Word symndx = header->symndx;
3193 : 0 : GElf_Off buckets_at
3194 : : = (offs[i_gnu_hash] + sizeof *header
3195 : 0 : + (gelf_getclass (ebl->elf) * sizeof (Elf32_Word)
3196 : 0 : * header->maskwords));
3197 : :
3198 : : // elf_getdata_rawchunk takes a size_t, make sure it
3199 : : // doesn't overflow.
3200 : : #if SIZE_MAX <= UINT32_MAX
3201 : : if (nbuckets > SIZE_MAX / sizeof (Elf32_Word))
3202 : : data = NULL;
3203 : : else
3204 : : #endif
3205 : 0 : data = elf_getdata_rawchunk (ebl->elf, buckets_at,
3206 : : nbuckets * sizeof (Elf32_Word),
3207 : : ELF_T_WORD);
3208 [ # # ]: 0 : if (data != NULL && symndx < nbuckets)
3209 : : {
3210 : 0 : const Elf32_Word *const buckets = data->d_buf;
3211 : 0 : Elf32_Word maxndx = symndx;
3212 [ # # ]: 0 : for (Elf32_Word bucket = 0; bucket < nbuckets; ++bucket)
3213 : 0 : if (buckets[bucket] > maxndx)
3214 : : maxndx = buckets[bucket];
3215 : :
3216 : 0 : GElf_Off hasharr_at
3217 : : = (buckets_at + nbuckets * sizeof (Elf32_Word));
3218 : 0 : hasharr_at += (maxndx - symndx) * sizeof (Elf32_Word);
3219 : 0 : do
3220 : : {
3221 : 0 : data = elf_getdata_rawchunk (
3222 : : ebl->elf, hasharr_at, sizeof (Elf32_Word), ELF_T_WORD);
3223 [ # # # # ]: 0 : if (data != NULL && (*(const Elf32_Word *)data->d_buf & 1u))
3224 : : {
3225 : 0 : syments = maxndx + 1;
3226 : 0 : break;
3227 : : }
3228 : 0 : ++maxndx;
3229 : 0 : hasharr_at += sizeof (Elf32_Word);
3230 : : }
3231 [ # # ]: 0 : while (data != NULL);
3232 : : }
3233 : : }
3234 : : }
3235 [ # # # # ]: 0 : if (offs[i_strtab] > offs[i_symtab] && syments == 0)
3236 : 0 : syments = ((offs[i_strtab] - offs[i_symtab])
3237 : 0 : / gelf_fsize (ebl->elf, ELF_T_SYM, 1, EV_CURRENT));
3238 : :
3239 [ # # # # : 0 : if (syments <= 0 || offs[i_strtab] == 0 || offs[i_symtab] == 0)
# # ]
3240 : : {
3241 : 0 : error_exit (0, _ ("Dynamic symbol information is not available for "
3242 : : "displaying symbols."));
3243 : : }
3244 : :
3245 : : /* All the data chunk initializaion. */
3246 : 0 : Elf_Data *symdata = NULL;
3247 : 0 : Elf_Data *symstrdata = NULL;
3248 : 0 : Elf_Data *versym_data = NULL;
3249 : 0 : Elf_Data *verdef_data = NULL;
3250 : 0 : Elf_Data *verneed_data = NULL;
3251 : :
3252 : 0 : if (offs[i_symtab] != 0)
3253 : 0 : symdata = elf_getdata_rawchunk (
3254 : : ebl->elf, offs[i_symtab],
3255 : : gelf_fsize (ebl->elf, ELF_T_SYM, syments, EV_CURRENT), ELF_T_SYM);
3256 : :
3257 [ # # ]: 0 : if (offs[i_strtab] != 0 && addrs[i_strsz] != 0)
3258 : 0 : symstrdata = elf_getdata_rawchunk (ebl->elf, offs[i_strtab], addrs[i_strsz],
3259 : : ELF_T_BYTE);
3260 : :
3261 [ # # ]: 0 : if (offs[i_versym] != 0)
3262 : 0 : versym_data = elf_getdata_rawchunk (
3263 : : ebl->elf, offs[i_versym], syments * sizeof (Elf64_Half), ELF_T_HALF);
3264 : :
3265 : : /* Get the verneed_data without vernaux. */
3266 [ # # # # ]: 0 : if (offs[i_verneed] != 0 && addrs[i_verneednum] != 0)
3267 : : {
3268 : 0 : verneed_data = elf_getdata_rawchunk (
3269 : : ebl->elf, offs[i_verneed], addrs[i_verneednum] * sizeof (Elf64_Verneed),
3270 : : ELF_T_VNEED);
3271 : :
3272 [ # # ]: 0 : if (verneed_data->d_size < sizeof (GElf_Verneed))
3273 : 0 : error_exit (0, _("malformed SHT_GNU_verneed data"));
3274 : : }
3275 : :
3276 : 0 : size_t vernauxnum = 0;
3277 : 0 : size_t vn_next_offset = 0;
3278 : :
3279 [ # # ]: 0 : if (verneed_data != NULL && verneed_data->d_buf != NULL)
3280 [ # # ]: 0 : for (size_t i = 0; i < addrs[i_verneednum]; i++)
3281 : : {
3282 [ # # ]: 0 : if (vn_next_offset > (verneed_data->d_size - sizeof (GElf_Verneed)))
3283 : 0 : error_exit (0, _("invalid SHT_GNU_verneed data"));
3284 : :
3285 : 0 : GElf_Verneed *verneed
3286 : : = (GElf_Verneed *)(verneed_data->d_buf + vn_next_offset);
3287 : 0 : vernauxnum += verneed->vn_cnt;
3288 : 0 : vn_next_offset += verneed->vn_next;
3289 : : }
3290 : :
3291 : : /* Update the verneed_data to include the vernaux. */
3292 [ # # # # ]: 0 : if (offs[i_verneed] != 0 && addrs[i_verneednum] != 0)
3293 : 0 : verneed_data = elf_getdata_rawchunk (
3294 : : ebl->elf, offs[i_verneed],
3295 : 0 : (addrs[i_verneednum] + vernauxnum) * sizeof (GElf_Verneed),
3296 : : ELF_T_VNEED);
3297 : :
3298 : : /* Get the verdef_data without verdaux. */
3299 [ # # # # ]: 0 : if (offs[i_verdef] != 0 && addrs[i_verdefnum] != 0)
3300 : : {
3301 : 0 : verdef_data = elf_getdata_rawchunk (
3302 : : ebl->elf, offs[i_verdef], addrs[i_verdefnum] * sizeof (Elf64_Verdef),
3303 : : ELF_T_VDEF);
3304 : :
3305 [ # # ]: 0 : if (verdef_data->d_size < sizeof (GElf_Verdef))
3306 : 0 : error_exit (0, _("malformed SHT_GNU_verdef data"));
3307 : : }
3308 : :
3309 : 0 : size_t verdauxnum = 0;
3310 : 0 : size_t vd_next_offset = 0;
3311 : :
3312 [ # # ]: 0 : if (verdef_data != NULL && verdef_data->d_buf != NULL)
3313 [ # # ]: 0 : for (size_t i = 0; i < addrs[i_verdefnum]; i++)
3314 : : {
3315 [ # # ]: 0 : if (vd_next_offset > (verdef_data->d_size - sizeof (GElf_Verdef)))
3316 : 0 : error_exit (0, _("invalid SHT_GNU_verdef data"));
3317 : :
3318 : 0 : GElf_Verdef *verdef
3319 : : = (GElf_Verdef *)(verdef_data->d_buf + vd_next_offset);
3320 : 0 : verdauxnum += verdef->vd_cnt;
3321 : 0 : vd_next_offset += verdef->vd_next;
3322 : : }
3323 : :
3324 : : /* Update the verdef_data to include the verdaux. */
3325 [ # # # # ]: 0 : if (offs[i_verdef] != 0 && addrs[i_verdefnum] != 0)
3326 : 0 : verdef_data = elf_getdata_rawchunk (
3327 : : ebl->elf, offs[i_verdef],
3328 : 0 : (addrs[i_verdefnum] + verdauxnum) * sizeof (GElf_Verdef), ELF_T_VDEF);
3329 : :
3330 : 0 : unsigned int nsyms = (unsigned int)syments;
3331 : 0 : process_symtab (ebl, nsyms, 0, 0, 0, symdata, versym_data, symstrdata,
3332 : : verneed_data, verdef_data, NULL);
3333 : 0 : return true;
3334 : : }
3335 : :
3336 : :
3337 : : /* Print version information. */
3338 : : static void
3339 : 184 : print_verinfo (Ebl *ebl)
3340 : : {
3341 : : /* Find the version information sections. For this we have to
3342 : : search through the section table. */
3343 : 184 : Elf_Scn *scn = NULL;
3344 : :
3345 [ + + ]: 6006 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
3346 : : {
3347 : : /* Handle the section if it is part of the versioning handling. */
3348 : 5822 : GElf_Shdr shdr_mem;
3349 : 5822 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
3350 : :
3351 [ + - ]: 5822 : if (likely (shdr != NULL))
3352 : : {
3353 [ + + ]: 5822 : if (shdr->sh_type == SHT_GNU_verneed)
3354 : 130 : handle_verneed (ebl, scn, shdr);
3355 [ + + ]: 5692 : else if (shdr->sh_type == SHT_GNU_verdef)
3356 : 4 : handle_verdef (ebl, scn, shdr);
3357 [ + + ]: 5688 : else if (shdr->sh_type == SHT_GNU_versym)
3358 : 130 : handle_versym (ebl, scn, shdr);
3359 : : }
3360 : : }
3361 : 184 : }
3362 : :
3363 : :
3364 : : static const char *
3365 : 384 : get_ver_flags (unsigned int flags)
3366 : : {
3367 : 384 : static char buf[32];
3368 : 384 : char *endp;
3369 : :
3370 [ + + ]: 384 : if (flags == 0)
3371 : 380 : return _("none");
3372 : :
3373 [ + - ]: 4 : if (flags & VER_FLG_BASE)
3374 : 4 : endp = stpcpy (buf, "BASE ");
3375 : : else
3376 : : endp = buf;
3377 : :
3378 [ - + ]: 4 : if (flags & VER_FLG_WEAK)
3379 : : {
3380 [ # # ]: 0 : if (endp != buf)
3381 : 0 : endp = stpcpy (endp, "| ");
3382 : :
3383 : 0 : endp = stpcpy (endp, "WEAK ");
3384 : : }
3385 : :
3386 [ - + ]: 4 : if (unlikely (flags & ~(VER_FLG_BASE | VER_FLG_WEAK)))
3387 : : {
3388 : 0 : strncpy (endp, _("| <unknown>"), buf + sizeof (buf) - endp);
3389 : 0 : buf[sizeof (buf) - 1] = '\0';
3390 : : }
3391 : :
3392 : : return buf;
3393 : : }
3394 : :
3395 : :
3396 : : static void
3397 : 130 : handle_verneed (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr)
3398 : : {
3399 : 130 : int class = gelf_getclass (ebl->elf);
3400 : :
3401 : : /* Get the data of the section. */
3402 : 130 : Elf_Data *data = elf_getdata (scn, NULL);
3403 [ - + ]: 130 : if (data == NULL)
3404 : 0 : return;
3405 : :
3406 : : /* Get the section header string table index. */
3407 : 130 : size_t shstrndx;
3408 [ - + ]: 130 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
3409 : 0 : error_exit (0, _("cannot get section header string table index"));
3410 : :
3411 : 130 : GElf_Shdr glink_mem;
3412 : 130 : GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
3413 : : &glink_mem);
3414 [ - + ]: 130 : if (glink == NULL)
3415 : 0 : error_exit (0, _("invalid sh_link value in section %zu"),
3416 : : elf_ndxscn (scn));
3417 : :
3418 : 130 : printf (ngettext ("\
3419 : : \nVersion needs section [%2u] '%s' contains %d entry:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'\n",
3420 : : "\
3421 : : \nVersion needs section [%2u] '%s' contains %d entries:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'\n",
3422 : : shdr->sh_info),
3423 : 130 : (unsigned int) elf_ndxscn (scn),
3424 : 130 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name), shdr->sh_info,
3425 : : class == ELFCLASS32 ? 10 : 18, shdr->sh_addr,
3426 : : shdr->sh_offset,
3427 [ + + ]: 130 : (unsigned int) shdr->sh_link,
3428 : 130 : elf_strptr (ebl->elf, shstrndx, glink->sh_name));
3429 : :
3430 : 130 : unsigned int offset = 0;
3431 [ + - ]: 184 : for (unsigned int cnt = shdr->sh_info; cnt > 0; cnt--)
3432 : : {
3433 : : /* Get the data at the next offset. */
3434 : 184 : GElf_Verneed needmem;
3435 : 184 : GElf_Verneed *need = gelf_getverneed (data, offset, &needmem);
3436 [ + - ]: 184 : if (unlikely (need == NULL))
3437 : : break;
3438 : :
3439 : 552 : printf (_(" %#06x: Version: %hu File: %s Cnt: %hu\n"),
3440 : 184 : offset, (unsigned short int) need->vn_version,
3441 : 184 : elf_strptr (ebl->elf, shdr->sh_link, need->vn_file),
3442 : 184 : (unsigned short int) need->vn_cnt);
3443 : :
3444 : 184 : unsigned int auxoffset = offset + need->vn_aux;
3445 [ + - ]: 360 : for (unsigned int cnt2 = need->vn_cnt; cnt2 > 0; cnt2--)
3446 : : {
3447 : 360 : GElf_Vernaux auxmem;
3448 : 360 : GElf_Vernaux *aux = gelf_getvernaux (data, auxoffset, &auxmem);
3449 [ + - ]: 360 : if (unlikely (aux == NULL))
3450 : : break;
3451 : :
3452 : 360 : printf (_(" %#06x: Name: %s Flags: %s Version: %hu\n"),
3453 : : auxoffset,
3454 : 360 : elf_strptr (ebl->elf, shdr->sh_link, aux->vna_name),
3455 : 360 : get_ver_flags (aux->vna_flags),
3456 : 360 : (unsigned short int) aux->vna_other);
3457 : :
3458 [ + + ]: 360 : if (aux->vna_next == 0)
3459 : : break;
3460 : :
3461 : 176 : auxoffset += aux->vna_next;
3462 : : }
3463 : :
3464 : : /* Find the next offset. */
3465 [ + + ]: 184 : if (need->vn_next == 0)
3466 : : break;
3467 : :
3468 : 54 : offset += need->vn_next;
3469 : : }
3470 : : }
3471 : :
3472 : :
3473 : : static void
3474 : 4 : handle_verdef (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr)
3475 : : {
3476 : : /* Get the data of the section. */
3477 : 4 : Elf_Data *data = elf_getdata (scn, NULL);
3478 [ - + ]: 4 : if (data == NULL)
3479 : 0 : return;
3480 : :
3481 : : /* Get the section header string table index. */
3482 : 4 : size_t shstrndx;
3483 [ - + ]: 4 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
3484 : 0 : error_exit (0, _("cannot get section header string table index"));
3485 : :
3486 : 4 : GElf_Shdr glink_mem;
3487 : 4 : GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
3488 : : &glink_mem);
3489 [ - + ]: 4 : if (glink == NULL)
3490 : 0 : error_exit (0, _("invalid sh_link value in section %zu"),
3491 : : elf_ndxscn (scn));
3492 : :
3493 : 4 : int class = gelf_getclass (ebl->elf);
3494 : 4 : printf (ngettext ("\
3495 : : \nVersion definition section [%2u] '%s' contains %d entry:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'\n",
3496 : : "\
3497 : : \nVersion definition section [%2u] '%s' contains %d entries:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'\n",
3498 : : shdr->sh_info),
3499 : 4 : (unsigned int) elf_ndxscn (scn),
3500 : 4 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
3501 : : shdr->sh_info,
3502 : : class == ELFCLASS32 ? 10 : 18, shdr->sh_addr,
3503 : : shdr->sh_offset,
3504 [ + - ]: 4 : (unsigned int) shdr->sh_link,
3505 : 4 : elf_strptr (ebl->elf, shstrndx, glink->sh_name));
3506 : :
3507 : 4 : unsigned int offset = 0;
3508 [ + - ]: 24 : for (unsigned int cnt = shdr->sh_info; cnt > 0; cnt--)
3509 : : {
3510 : : /* Get the data at the next offset. */
3511 : 24 : GElf_Verdef defmem;
3512 : 24 : GElf_Verdef *def = gelf_getverdef (data, offset, &defmem);
3513 [ + - ]: 24 : if (unlikely (def == NULL))
3514 : : break;
3515 : :
3516 : 24 : unsigned int auxoffset = offset + def->vd_aux;
3517 : 24 : GElf_Verdaux auxmem;
3518 : 24 : GElf_Verdaux *aux = gelf_getverdaux (data, auxoffset, &auxmem);
3519 [ + - ]: 24 : if (unlikely (aux == NULL))
3520 : : break;
3521 : :
3522 : 96 : printf (_("\
3523 : : %#06x: Version: %hd Flags: %s Index: %hd Cnt: %hd Name: %s\n"),
3524 : 24 : offset, def->vd_version,
3525 : 24 : get_ver_flags (def->vd_flags),
3526 : 24 : def->vd_ndx,
3527 : 24 : def->vd_cnt,
3528 : 24 : elf_strptr (ebl->elf, shdr->sh_link, aux->vda_name));
3529 : :
3530 : 24 : auxoffset += aux->vda_next;
3531 [ + + ]: 24 : for (unsigned int cnt2 = 1; cnt2 < def->vd_cnt; ++cnt2)
3532 : : {
3533 : 16 : aux = gelf_getverdaux (data, auxoffset, &auxmem);
3534 [ + - ]: 16 : if (unlikely (aux == NULL))
3535 : : break;
3536 : :
3537 : 16 : printf (_(" %#06x: Parent %d: %s\n"),
3538 : : auxoffset, cnt2,
3539 : 16 : elf_strptr (ebl->elf, shdr->sh_link, aux->vda_name));
3540 : :
3541 [ - + ]: 16 : if (aux->vda_next == 0)
3542 : : break;
3543 : :
3544 : 0 : auxoffset += aux->vda_next;
3545 : : }
3546 : :
3547 : : /* Find the next offset. */
3548 [ + + ]: 24 : if (def->vd_next == 0)
3549 : : break;
3550 : 20 : offset += def->vd_next;
3551 : : }
3552 : : }
3553 : :
3554 : :
3555 : : static void
3556 : 130 : handle_versym (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr)
3557 : : {
3558 : 130 : int class = gelf_getclass (ebl->elf);
3559 : 130 : const char **vername;
3560 : 130 : const char **filename;
3561 : :
3562 : : /* Get the data of the section. */
3563 : 130 : Elf_Data *data = elf_getdata (scn, NULL);
3564 [ + - ]: 130 : if (data == NULL)
3565 : 0 : return;
3566 : :
3567 : : /* Get the section header string table index. */
3568 : 130 : size_t shstrndx;
3569 [ - + ]: 130 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
3570 : 0 : error_exit (0, _("cannot get section header string table index"));
3571 : :
3572 : : /* We have to find the version definition section and extract the
3573 : : version names. */
3574 : : Elf_Scn *defscn = NULL;
3575 : : Elf_Scn *needscn = NULL;
3576 : :
3577 : : Elf_Scn *verscn = NULL;
3578 [ + + ]: 4048 : while ((verscn = elf_nextscn (ebl->elf, verscn)) != NULL)
3579 : : {
3580 : 3918 : GElf_Shdr vershdr_mem;
3581 : 3918 : GElf_Shdr *vershdr = gelf_getshdr (verscn, &vershdr_mem);
3582 : :
3583 [ - + ]: 3918 : if (likely (vershdr != NULL))
3584 : : {
3585 [ + + ]: 3918 : if (vershdr->sh_type == SHT_GNU_verdef)
3586 : : defscn = verscn;
3587 [ + + ]: 3914 : else if (vershdr->sh_type == SHT_GNU_verneed)
3588 : 3918 : needscn = verscn;
3589 : : }
3590 : : }
3591 : :
3592 : 130 : size_t nvername;
3593 [ + - ]: 130 : if (defscn != NULL || needscn != NULL)
3594 : : {
3595 : : /* We have a version information (better should have). Now get
3596 : : the version names. First find the maximum version number. */
3597 : 130 : nvername = 0;
3598 [ + + ]: 130 : if (defscn != NULL)
3599 : : {
3600 : : /* Run through the version definitions and find the highest
3601 : : index. */
3602 : 4 : unsigned int offset = 0;
3603 : 4 : Elf_Data *defdata;
3604 : 4 : GElf_Shdr defshdrmem;
3605 : 4 : GElf_Shdr *defshdr;
3606 : :
3607 : 4 : defdata = elf_getdata (defscn, NULL);
3608 [ + - ]: 4 : if (unlikely (defdata == NULL))
3609 : 0 : return;
3610 : :
3611 : 4 : defshdr = gelf_getshdr (defscn, &defshdrmem);
3612 [ + - ]: 4 : if (unlikely (defshdr == NULL))
3613 : : return;
3614 : :
3615 [ + - ]: 24 : for (unsigned int cnt = 0; cnt < defshdr->sh_info; ++cnt)
3616 : : {
3617 : 24 : GElf_Verdef defmem;
3618 : 24 : GElf_Verdef *def;
3619 : :
3620 : : /* Get the data at the next offset. */
3621 : 24 : def = gelf_getverdef (defdata, offset, &defmem);
3622 [ + - ]: 24 : if (unlikely (def == NULL))
3623 : : break;
3624 : :
3625 : 24 : nvername = MAX (nvername, (size_t) (def->vd_ndx & 0x7fff));
3626 : :
3627 [ + + ]: 24 : if (def->vd_next == 0)
3628 : : break;
3629 : 20 : offset += def->vd_next;
3630 : : }
3631 : : }
3632 [ + - ]: 130 : if (needscn != NULL)
3633 : : {
3634 : 130 : unsigned int offset = 0;
3635 : 130 : Elf_Data *needdata;
3636 : 130 : GElf_Shdr needshdrmem;
3637 : 130 : GElf_Shdr *needshdr;
3638 : :
3639 : 130 : needdata = elf_getdata (needscn, NULL);
3640 [ + - ]: 130 : if (unlikely (needdata == NULL))
3641 : 0 : return;
3642 : :
3643 : 130 : needshdr = gelf_getshdr (needscn, &needshdrmem);
3644 [ + - ]: 130 : if (unlikely (needshdr == NULL))
3645 : : return;
3646 : :
3647 [ + - ]: 184 : for (unsigned int cnt = 0; cnt < needshdr->sh_info; ++cnt)
3648 : : {
3649 : 184 : GElf_Verneed needmem;
3650 : 184 : GElf_Verneed *need;
3651 : 184 : unsigned int auxoffset;
3652 : 184 : int cnt2;
3653 : :
3654 : : /* Get the data at the next offset. */
3655 : 184 : need = gelf_getverneed (needdata, offset, &needmem);
3656 [ + - ]: 184 : if (unlikely (need == NULL))
3657 : : break;
3658 : :
3659 : : /* Run through the auxiliary entries. */
3660 : 184 : auxoffset = offset + need->vn_aux;
3661 [ + - ]: 360 : for (cnt2 = need->vn_cnt; --cnt2 >= 0; )
3662 : : {
3663 : 360 : GElf_Vernaux auxmem;
3664 : 360 : GElf_Vernaux *aux;
3665 : :
3666 : 360 : aux = gelf_getvernaux (needdata, auxoffset, &auxmem);
3667 [ + - ]: 360 : if (unlikely (aux == NULL))
3668 : : break;
3669 : :
3670 : 360 : nvername = MAX (nvername,
3671 : : (size_t) (aux->vna_other & 0x7fff));
3672 : :
3673 [ + + ]: 360 : if (aux->vna_next == 0)
3674 : : break;
3675 : 176 : auxoffset += aux->vna_next;
3676 : : }
3677 : :
3678 [ + + ]: 184 : if (need->vn_next == 0)
3679 : : break;
3680 : 54 : offset += need->vn_next;
3681 : : }
3682 : : }
3683 : :
3684 : : /* This is the number of versions we know about. */
3685 : 130 : ++nvername;
3686 : :
3687 : : /* Allocate the array. */
3688 : 130 : vername = (const char **) alloca (nvername * sizeof (const char *));
3689 [ + + ]: 130 : memset(vername, 0, nvername * sizeof (const char *));
3690 : 130 : filename = (const char **) alloca (nvername * sizeof (const char *));
3691 : 130 : memset(filename, 0, nvername * sizeof (const char *));
3692 : :
3693 : : /* Run through the data structures again and collect the strings. */
3694 [ + + ]: 130 : if (defscn != NULL)
3695 : : {
3696 : : /* Run through the version definitions and find the highest
3697 : : index. */
3698 : 4 : unsigned int offset = 0;
3699 : 4 : Elf_Data *defdata;
3700 : 4 : GElf_Shdr defshdrmem;
3701 : 4 : GElf_Shdr *defshdr;
3702 : :
3703 : 4 : defdata = elf_getdata (defscn, NULL);
3704 [ + - ]: 4 : if (unlikely (defdata == NULL))
3705 : 0 : return;
3706 : :
3707 : 4 : defshdr = gelf_getshdr (defscn, &defshdrmem);
3708 [ + - ]: 4 : if (unlikely (defshdr == NULL))
3709 : : return;
3710 : :
3711 [ + - ]: 24 : for (unsigned int cnt = 0; cnt < defshdr->sh_info; ++cnt)
3712 : : {
3713 : :
3714 : : /* Get the data at the next offset. */
3715 : 24 : GElf_Verdef defmem;
3716 : 24 : GElf_Verdef *def = gelf_getverdef (defdata, offset, &defmem);
3717 [ + - ]: 24 : if (unlikely (def == NULL))
3718 : : break;
3719 : :
3720 : 24 : GElf_Verdaux auxmem;
3721 : 48 : GElf_Verdaux *aux = gelf_getverdaux (defdata,
3722 : 24 : offset + def->vd_aux,
3723 : : &auxmem);
3724 [ + - ]: 24 : if (unlikely (aux == NULL))
3725 : : break;
3726 : :
3727 : 24 : vername[def->vd_ndx & 0x7fff]
3728 : 24 : = elf_strptr (ebl->elf, defshdr->sh_link, aux->vda_name);
3729 : 24 : filename[def->vd_ndx & 0x7fff] = NULL;
3730 : :
3731 [ + + ]: 24 : if (def->vd_next == 0)
3732 : : break;
3733 : 20 : offset += def->vd_next;
3734 : : }
3735 : : }
3736 [ + - ]: 130 : if (needscn != NULL)
3737 : : {
3738 : 130 : unsigned int offset = 0;
3739 : :
3740 : 130 : Elf_Data *needdata = elf_getdata (needscn, NULL);
3741 : 130 : GElf_Shdr needshdrmem;
3742 : 130 : GElf_Shdr *needshdr = gelf_getshdr (needscn, &needshdrmem);
3743 [ - + ]: 130 : if (unlikely (needdata == NULL || needshdr == NULL))
3744 : 0 : return;
3745 : :
3746 [ + - ]: 184 : for (unsigned int cnt = 0; cnt < needshdr->sh_info; ++cnt)
3747 : : {
3748 : : /* Get the data at the next offset. */
3749 : 184 : GElf_Verneed needmem;
3750 : 184 : GElf_Verneed *need = gelf_getverneed (needdata, offset,
3751 : : &needmem);
3752 [ + - ]: 184 : if (unlikely (need == NULL))
3753 : : break;
3754 : :
3755 : : /* Run through the auxiliary entries. */
3756 : 184 : unsigned int auxoffset = offset + need->vn_aux;
3757 [ + - ]: 360 : for (int cnt2 = need->vn_cnt; --cnt2 >= 0; )
3758 : : {
3759 : 360 : GElf_Vernaux auxmem;
3760 : 360 : GElf_Vernaux *aux = gelf_getvernaux (needdata, auxoffset,
3761 : : &auxmem);
3762 [ + - ]: 360 : if (unlikely (aux == NULL))
3763 : : break;
3764 : :
3765 : 360 : vername[aux->vna_other & 0x7fff]
3766 : 360 : = elf_strptr (ebl->elf, needshdr->sh_link, aux->vna_name);
3767 : 360 : filename[aux->vna_other & 0x7fff]
3768 : 360 : = elf_strptr (ebl->elf, needshdr->sh_link, need->vn_file);
3769 : :
3770 [ + + ]: 360 : if (aux->vna_next == 0)
3771 : : break;
3772 : 176 : auxoffset += aux->vna_next;
3773 : : }
3774 : :
3775 [ + + ]: 184 : if (need->vn_next == 0)
3776 : : break;
3777 : 54 : offset += need->vn_next;
3778 : : }
3779 : : }
3780 : : }
3781 : : else
3782 : : {
3783 : : vername = NULL;
3784 : : nvername = 1;
3785 : : filename = NULL;
3786 : : }
3787 : :
3788 : 130 : GElf_Shdr glink_mem;
3789 : 130 : GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
3790 : : &glink_mem);
3791 : 130 : size_t sh_entsize = gelf_fsize (ebl->elf, ELF_T_HALF, 1, EV_CURRENT);
3792 [ - + ]: 130 : if (glink == NULL)
3793 : 0 : error_exit (0, _("invalid sh_link value in section %zu"),
3794 : : elf_ndxscn (scn));
3795 : :
3796 : : /* Print the header. */
3797 : 130 : printf (ngettext ("\
3798 : : \nVersion symbols section [%2u] '%s' contains %d entry:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'",
3799 : : "\
3800 : : \nVersion symbols section [%2u] '%s' contains %d entries:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'",
3801 : : shdr->sh_size / sh_entsize),
3802 : 130 : (unsigned int) elf_ndxscn (scn),
3803 : 130 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
3804 : 130 : (int) (shdr->sh_size / sh_entsize),
3805 : : class == ELFCLASS32 ? 10 : 18, shdr->sh_addr,
3806 : : shdr->sh_offset,
3807 [ + + ]: 130 : (unsigned int) shdr->sh_link,
3808 : 130 : elf_strptr (ebl->elf, shstrndx, glink->sh_name));
3809 : :
3810 : : /* Now we can finally look at the actual contents of this section. */
3811 [ + + ]: 3398 : for (unsigned int cnt = 0; cnt < shdr->sh_size / sh_entsize; ++cnt)
3812 : : {
3813 [ + + ]: 3268 : if (cnt % 2 == 0)
3814 : 1652 : printf ("\n %4d:", cnt);
3815 : :
3816 : 3268 : GElf_Versym symmem;
3817 : 3268 : GElf_Versym *sym = gelf_getversym (data, cnt, &symmem);
3818 [ + - ]: 3268 : if (sym == NULL)
3819 : : break;
3820 : :
3821 [ + + + ]: 3268 : switch (*sym)
3822 : : {
3823 : 238 : ssize_t n;
3824 : 238 : case 0:
3825 : 238 : fputs (_(" 0 *local* "),
3826 : : stdout);
3827 : 238 : break;
3828 : :
3829 : 502 : case 1:
3830 : 502 : fputs (_(" 1 *global* "),
3831 : : stdout);
3832 : 502 : break;
3833 : :
3834 : 2528 : default:
3835 [ + - ]: 5056 : n = printf ("%4d%c%s",
3836 [ + - ]: 2528 : *sym & 0x7fff, *sym & 0x8000 ? 'h' : ' ',
3837 : : (vername != NULL
3838 [ + - ]: 2528 : && (unsigned int) (*sym & 0x7fff) < nvername)
3839 : 2528 : ? vername[*sym & 0x7fff] : "???");
3840 [ + - ]: 2528 : if ((unsigned int) (*sym & 0x7fff) < nvername
3841 [ + - + + ]: 2528 : && filename != NULL && filename[*sym & 0x7fff] != NULL)
3842 : 2224 : n += printf ("(%s)", filename[*sym & 0x7fff]);
3843 : 2528 : printf ("%*s", MAX (0, 33 - (int) n), " ");
3844 : 2528 : break;
3845 : : }
3846 : : }
3847 : 130 : putchar ('\n');
3848 : : }
3849 : :
3850 : :
3851 : : static void
3852 : 130 : print_hash_info (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr, size_t shstrndx,
3853 : : uint_fast32_t maxlength, Elf32_Word nbucket,
3854 : : uint_fast32_t nsyms, uint32_t *lengths, const char *extrastr)
3855 : : {
3856 : 130 : uint32_t *counts = xcalloc (maxlength + 1, sizeof (uint32_t));
3857 : :
3858 [ + + ]: 550 : for (Elf32_Word cnt = 0; cnt < nbucket; ++cnt)
3859 : 420 : ++counts[lengths[cnt]];
3860 : :
3861 : 130 : GElf_Shdr glink_mem;
3862 : 130 : GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf,
3863 : 130 : shdr->sh_link),
3864 : : &glink_mem);
3865 [ - + ]: 130 : if (glink == NULL)
3866 : : {
3867 : 0 : error (0, 0, _("invalid sh_link value in section %zu"),
3868 : : elf_ndxscn (scn));
3869 : 0 : free (counts);
3870 : 0 : return;
3871 : : }
3872 : :
3873 [ + + ]: 260 : printf (ngettext ("\
3874 : : \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",
3875 : : "\
3876 : : \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",
3877 : : nbucket),
3878 : 130 : (unsigned int) elf_ndxscn (scn),
3879 : 130 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
3880 : : (int) nbucket,
3881 : 130 : gelf_getclass (ebl->elf) == ELFCLASS32 ? 10 : 18,
3882 : : shdr->sh_addr,
3883 : : shdr->sh_offset,
3884 : 130 : (unsigned int) shdr->sh_link,
3885 : 130 : elf_strptr (ebl->elf, shstrndx, glink->sh_name));
3886 : :
3887 [ + - ]: 130 : if (extrastr != NULL)
3888 : 130 : fputs (extrastr, stdout);
3889 : :
3890 [ + - ]: 130 : if (likely (nbucket > 0))
3891 : : {
3892 : 130 : uint64_t success = 0;
3893 : :
3894 : : /* xgettext:no-c-format */
3895 : 130 : fputs (_("\
3896 : : Length Number % of total Coverage\n"), stdout);
3897 : 130 : printf (_(" 0 %6" PRIu32 " %5.1f%%\n"),
3898 : 130 : counts[0], (counts[0] * 100.0) / nbucket);
3899 : :
3900 : 130 : uint64_t nzero_counts = 0;
3901 [ + + ]: 230 : for (Elf32_Word cnt = 1; cnt <= maxlength; ++cnt)
3902 : : {
3903 : 100 : nzero_counts += counts[cnt] * cnt;
3904 : 100 : printf (_("\
3905 : : %7d %6" PRIu32 " %5.1f%% %5.1f%%\n"),
3906 : 100 : (int) cnt, counts[cnt], (counts[cnt] * 100.0) / nbucket,
3907 : 100 : (nzero_counts * 100.0) / nsyms);
3908 : : }
3909 : :
3910 : : Elf32_Word acc = 0;
3911 [ + + ]: 230 : for (Elf32_Word cnt = 1; cnt <= maxlength; ++cnt)
3912 : : {
3913 : 100 : acc += cnt;
3914 : 100 : success += counts[cnt] * acc;
3915 : : }
3916 : :
3917 [ + + ]: 130 : if (nzero_counts > 0)
3918 : 62 : printf (_("\
3919 : : Average number of tests: successful lookup: %f\n\
3920 : : unsuccessful lookup: %f\n"),
3921 : 62 : (double) success / (double) nzero_counts,
3922 : 62 : (double) nzero_counts / (double) nbucket);
3923 : : }
3924 : :
3925 : 130 : free (counts);
3926 : : }
3927 : :
3928 : :
3929 : : /* This function handles the traditional System V-style hash table format. */
3930 : : static void
3931 : 0 : handle_sysv_hash (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr, size_t shstrndx)
3932 : : {
3933 : 0 : Elf_Data *data = elf_getdata (scn, NULL);
3934 [ # # ]: 0 : if (unlikely (data == NULL))
3935 : : {
3936 : 0 : error (0, 0, _("cannot get data for section %d: %s"),
3937 : 0 : (int) elf_ndxscn (scn), elf_errmsg (-1));
3938 : 0 : return;
3939 : : }
3940 : :
3941 [ # # ]: 0 : if (unlikely (data->d_size < 2 * sizeof (Elf32_Word)))
3942 : : {
3943 : 0 : invalid_data:
3944 : 0 : error (0, 0, _("invalid data in sysv.hash section %d"),
3945 : 0 : (int) elf_ndxscn (scn));
3946 : 0 : return;
3947 : : }
3948 : :
3949 : 0 : Elf32_Word nbucket = ((Elf32_Word *) data->d_buf)[0];
3950 : 0 : Elf32_Word nchain = ((Elf32_Word *) data->d_buf)[1];
3951 : :
3952 : 0 : uint64_t used_buf = (2ULL + nchain + nbucket) * sizeof (Elf32_Word);
3953 [ # # ]: 0 : if (used_buf > data->d_size)
3954 : 0 : goto invalid_data;
3955 : :
3956 : 0 : Elf32_Word *bucket = &((Elf32_Word *) data->d_buf)[2];
3957 : 0 : Elf32_Word *chain = &((Elf32_Word *) data->d_buf)[2 + nbucket];
3958 : :
3959 : 0 : uint32_t *lengths = xcalloc (nbucket, sizeof (uint32_t));
3960 : :
3961 : 0 : uint_fast32_t maxlength = 0;
3962 : 0 : uint_fast32_t nsyms = 0;
3963 [ # # ]: 0 : for (Elf32_Word cnt = 0; cnt < nbucket; ++cnt)
3964 : : {
3965 : 0 : Elf32_Word inner = bucket[cnt];
3966 : 0 : Elf32_Word chain_len = 0;
3967 [ # # ]: 0 : while (inner > 0 && inner < nchain)
3968 : : {
3969 : 0 : ++nsyms;
3970 : 0 : ++chain_len;
3971 [ # # ]: 0 : if (chain_len > nchain)
3972 : : {
3973 : 0 : error (0, 0, _("invalid chain in sysv.hash section %d"),
3974 : 0 : (int) elf_ndxscn (scn));
3975 : 0 : free (lengths);
3976 : 0 : return;
3977 : : }
3978 [ # # ]: 0 : if (maxlength < ++lengths[cnt])
3979 : 0 : ++maxlength;
3980 : :
3981 : 0 : inner = chain[inner];
3982 : : }
3983 : : }
3984 : :
3985 : 0 : print_hash_info (ebl, scn, shdr, shstrndx, maxlength, nbucket, nsyms,
3986 : : lengths, NULL);
3987 : :
3988 : 0 : free (lengths);
3989 : : }
3990 : :
3991 : :
3992 : : /* This function handles the incorrect, System V-style hash table
3993 : : format some 64-bit architectures use. */
3994 : : static void
3995 : 0 : handle_sysv_hash64 (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr, size_t shstrndx)
3996 : : {
3997 : 0 : Elf_Data *data = elf_getdata (scn, NULL);
3998 [ # # ]: 0 : if (unlikely (data == NULL))
3999 : : {
4000 : 0 : error (0, 0, _("cannot get data for section %d: %s"),
4001 : 0 : (int) elf_ndxscn (scn), elf_errmsg (-1));
4002 : 0 : return;
4003 : : }
4004 : :
4005 [ # # ]: 0 : if (unlikely (data->d_size < 2 * sizeof (Elf64_Xword)))
4006 : : {
4007 : 0 : invalid_data:
4008 : 0 : error (0, 0, _("invalid data in sysv.hash64 section %d"),
4009 : 0 : (int) elf_ndxscn (scn));
4010 : 0 : return;
4011 : : }
4012 : :
4013 : 0 : Elf64_Xword nbucket = ((Elf64_Xword *) data->d_buf)[0];
4014 : 0 : Elf64_Xword nchain = ((Elf64_Xword *) data->d_buf)[1];
4015 : :
4016 : 0 : uint64_t maxwords = data->d_size / sizeof (Elf64_Xword);
4017 : 0 : if (maxwords < 2
4018 [ # # ]: 0 : || maxwords - 2 < nbucket
4019 [ # # ]: 0 : || maxwords - 2 - nbucket < nchain)
4020 : 0 : goto invalid_data;
4021 : :
4022 : 0 : Elf64_Xword *bucket = &((Elf64_Xword *) data->d_buf)[2];
4023 : 0 : Elf64_Xword *chain = &((Elf64_Xword *) data->d_buf)[2 + nbucket];
4024 : :
4025 : 0 : uint32_t *lengths = xcalloc (nbucket, sizeof (uint32_t));
4026 : :
4027 : 0 : uint_fast32_t maxlength = 0;
4028 : 0 : uint_fast32_t nsyms = 0;
4029 [ # # ]: 0 : for (Elf64_Xword cnt = 0; cnt < nbucket; ++cnt)
4030 : : {
4031 : 0 : Elf64_Xword inner = bucket[cnt];
4032 : 0 : Elf64_Xword chain_len = 0;
4033 [ # # ]: 0 : while (inner > 0 && inner < nchain)
4034 : : {
4035 : 0 : ++nsyms;
4036 : 0 : ++chain_len;
4037 [ # # ]: 0 : if (chain_len > nchain)
4038 : : {
4039 : 0 : error (0, 0, _("invalid chain in sysv.hash64 section %d"),
4040 : 0 : (int) elf_ndxscn (scn));
4041 : 0 : free (lengths);
4042 : 0 : return;
4043 : : }
4044 [ # # ]: 0 : if (maxlength < ++lengths[cnt])
4045 : 0 : ++maxlength;
4046 : :
4047 : 0 : inner = chain[inner];
4048 : : }
4049 : : }
4050 : :
4051 : 0 : print_hash_info (ebl, scn, shdr, shstrndx, maxlength, nbucket, nsyms,
4052 : : lengths, NULL);
4053 : :
4054 : 0 : free (lengths);
4055 : : }
4056 : :
4057 : :
4058 : : /* This function handles the GNU-style hash table format. */
4059 : : static void
4060 : 130 : handle_gnu_hash (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr, size_t shstrndx)
4061 : : {
4062 : 130 : uint32_t *lengths = NULL;
4063 : 130 : Elf_Data *data = elf_getdata (scn, NULL);
4064 [ - + ]: 130 : if (unlikely (data == NULL))
4065 : : {
4066 : 0 : error (0, 0, _("cannot get data for section %d: %s"),
4067 : 0 : (int) elf_ndxscn (scn), elf_errmsg (-1));
4068 : 0 : return;
4069 : : }
4070 : :
4071 [ - + ]: 130 : if (unlikely (data->d_size < 4 * sizeof (Elf32_Word)))
4072 : : {
4073 : 0 : invalid_data:
4074 : 0 : free (lengths);
4075 : 0 : error (0, 0, _("invalid data in gnu.hash section %d"),
4076 : 0 : (int) elf_ndxscn (scn));
4077 : 0 : return;
4078 : : }
4079 : :
4080 : 130 : Elf32_Word nbucket = ((Elf32_Word *) data->d_buf)[0];
4081 : 130 : Elf32_Word symbias = ((Elf32_Word *) data->d_buf)[1];
4082 : :
4083 : : /* Next comes the size of the bitmap. It's measured in words for
4084 : : the architecture. It's 32 bits for 32 bit archs, and 64 bits for
4085 : : 64 bit archs. There is always a bloom filter present, so zero is
4086 : : an invalid value. */
4087 : 130 : Elf32_Word bitmask_words = ((Elf32_Word *) data->d_buf)[2];
4088 [ + + ]: 130 : if (gelf_getclass (ebl->elf) == ELFCLASS64)
4089 : 114 : bitmask_words *= 2;
4090 : :
4091 [ - + ]: 130 : if (bitmask_words == 0)
4092 : 0 : goto invalid_data;
4093 : :
4094 : 130 : Elf32_Word shift = ((Elf32_Word *) data->d_buf)[3];
4095 : :
4096 : : /* Is there still room for the sym chain?
4097 : : Use uint64_t calculation to prevent 32bit overflow. */
4098 : 130 : uint64_t used_buf = (4ULL + bitmask_words + nbucket) * sizeof (Elf32_Word);
4099 : 130 : uint32_t max_nsyms = (data->d_size - used_buf) / sizeof (Elf32_Word);
4100 [ - + ]: 130 : if (used_buf > data->d_size)
4101 : 0 : goto invalid_data;
4102 : :
4103 : 130 : lengths = xcalloc (nbucket, sizeof (uint32_t));
4104 : :
4105 : 130 : Elf32_Word *bitmask = &((Elf32_Word *) data->d_buf)[4];
4106 : 130 : Elf32_Word *bucket = &((Elf32_Word *) data->d_buf)[4 + bitmask_words];
4107 : 130 : Elf32_Word *chain = &((Elf32_Word *) data->d_buf)[4 + bitmask_words
4108 : 130 : + nbucket];
4109 : :
4110 : : /* Compute distribution of chain lengths. */
4111 : 130 : uint_fast32_t maxlength = 0;
4112 : 130 : uint_fast32_t nsyms = 0;
4113 [ + + ]: 550 : for (Elf32_Word cnt = 0; cnt < nbucket; ++cnt)
4114 [ + + ]: 420 : if (bucket[cnt] != 0)
4115 : : {
4116 : 232 : Elf32_Word inner = bucket[cnt] - symbias;
4117 : 416 : do
4118 : : {
4119 : 416 : ++nsyms;
4120 [ + + ]: 416 : if (maxlength < ++lengths[cnt])
4121 : 100 : ++maxlength;
4122 [ - + ]: 416 : if (inner >= max_nsyms)
4123 : 0 : goto invalid_data;
4124 : : }
4125 [ + + ]: 416 : while ((chain[inner++] & 1) == 0);
4126 : : }
4127 : :
4128 : : /* Count bits in bitmask. */
4129 : : uint_fast32_t nbits = 0;
4130 [ + + ]: 446 : for (Elf32_Word cnt = 0; cnt < bitmask_words; ++cnt)
4131 : : {
4132 : 316 : uint_fast32_t word = bitmask[cnt];
4133 : :
4134 : 316 : word = (word & 0x55555555) + ((word >> 1) & 0x55555555);
4135 : 316 : word = (word & 0x33333333) + ((word >> 2) & 0x33333333);
4136 : 316 : word = (word & 0x0f0f0f0f) + ((word >> 4) & 0x0f0f0f0f);
4137 : 316 : word = (word & 0x00ff00ff) + ((word >> 8) & 0x00ff00ff);
4138 : 316 : nbits += (word & 0x0000ffff) + ((word >> 16) & 0x0000ffff);
4139 : : }
4140 : :
4141 : 260 : char *str = xasprintf (_("\
4142 : : Symbol Bias: %u\n\
4143 : : Bitmask Size: %zu bytes %" PRIuFAST32 "%% bits set 2nd hash shift: %u\n"),
4144 : : (unsigned int) symbias,
4145 : : bitmask_words * sizeof (Elf32_Word),
4146 : 130 : ((nbits * 100 + 50)
4147 : 130 : / (uint_fast32_t) (bitmask_words
4148 : : * sizeof (Elf32_Word) * 8)),
4149 : : (unsigned int) shift);
4150 : :
4151 : 130 : print_hash_info (ebl, scn, shdr, shstrndx, maxlength, nbucket, nsyms,
4152 : : lengths, str);
4153 : :
4154 : 130 : free (str);
4155 : 130 : free (lengths);
4156 : : }
4157 : :
4158 : :
4159 : : /* Find the symbol table(s). For this we have to search through the
4160 : : section table. */
4161 : : static void
4162 : 184 : handle_hash (Ebl *ebl)
4163 : : {
4164 : : /* Get the section header string table index. */
4165 : 184 : size_t shstrndx;
4166 [ - + ]: 184 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
4167 : 0 : error_exit (0, _("cannot get section header string table index"));
4168 : :
4169 : : Elf_Scn *scn = NULL;
4170 [ + + ]: 6006 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
4171 : : {
4172 : : /* Handle the section if it is a symbol table. */
4173 : 5822 : GElf_Shdr shdr_mem;
4174 : 5822 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
4175 : :
4176 [ + - ]: 5822 : if (likely (shdr != NULL))
4177 : : {
4178 [ + + ]: 5822 : if ((shdr->sh_type == SHT_HASH || shdr->sh_type == SHT_GNU_HASH)
4179 [ - + ]: 130 : && (shdr->sh_flags & SHF_COMPRESSED) != 0)
4180 : : {
4181 [ # # ]: 0 : if (elf_compress (scn, 0, 0) < 0)
4182 : 0 : printf ("WARNING: %s [%zd]\n",
4183 : : _("Couldn't uncompress section"),
4184 : : elf_ndxscn (scn));
4185 : 0 : shdr = gelf_getshdr (scn, &shdr_mem);
4186 [ # # ]: 0 : if (unlikely (shdr == NULL))
4187 : 0 : error_exit (0, _("cannot get section [%zd] header: %s"),
4188 : : elf_ndxscn (scn), elf_errmsg (-1));
4189 : : }
4190 : :
4191 [ - + ]: 5822 : if (shdr->sh_type == SHT_HASH)
4192 : : {
4193 [ # # ]: 0 : if (ebl_sysvhash_entrysize (ebl) == sizeof (Elf64_Xword))
4194 : 0 : handle_sysv_hash64 (ebl, scn, shdr, shstrndx);
4195 : : else
4196 : 0 : handle_sysv_hash (ebl, scn, shdr, shstrndx);
4197 : : }
4198 [ + + ]: 5822 : else if (shdr->sh_type == SHT_GNU_HASH)
4199 : 130 : handle_gnu_hash (ebl, scn, shdr, shstrndx);
4200 : : }
4201 : : }
4202 : 184 : }
4203 : :
4204 : :
4205 : : static void
4206 : 192 : print_liblist (Ebl *ebl)
4207 : : {
4208 : : /* Find the library list sections. For this we have to search
4209 : : through the section table. */
4210 : 192 : Elf_Scn *scn = NULL;
4211 : :
4212 : : /* Get the section header string table index. */
4213 : 192 : size_t shstrndx;
4214 [ - + ]: 192 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
4215 : 0 : error_exit (0, _("cannot get section header string table index"));
4216 : :
4217 [ + + ]: 6132 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
4218 : : {
4219 : 5940 : GElf_Shdr shdr_mem;
4220 : 5940 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
4221 : :
4222 [ + - - + ]: 5940 : if (shdr != NULL && shdr->sh_type == SHT_GNU_LIBLIST)
4223 : : {
4224 : 0 : size_t sh_entsize = gelf_fsize (ebl->elf, ELF_T_LIB, 1, EV_CURRENT);
4225 : 0 : int nentries = shdr->sh_size / sh_entsize;
4226 : 0 : printf (ngettext ("\
4227 : : \nLibrary list section [%2zu] '%s' at offset %#0" PRIx64 " contains %d entry:\n",
4228 : : "\
4229 : : \nLibrary list section [%2zu] '%s' at offset %#0" PRIx64 " contains %d entries:\n",
4230 : : nentries),
4231 : : elf_ndxscn (scn),
4232 : 0 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
4233 : : shdr->sh_offset,
4234 : : nentries);
4235 : :
4236 : 0 : Elf_Data *data = elf_getdata (scn, NULL);
4237 [ # # ]: 0 : if (data == NULL)
4238 : 0 : return;
4239 : :
4240 : 0 : puts (_("\
4241 : : Library Time Stamp Checksum Version Flags"));
4242 : :
4243 [ # # ]: 0 : for (int cnt = 0; cnt < nentries; ++cnt)
4244 : : {
4245 : 0 : GElf_Lib lib_mem;
4246 : 0 : GElf_Lib *lib = gelf_getlib (data, cnt, &lib_mem);
4247 [ # # ]: 0 : if (unlikely (lib == NULL))
4248 : 0 : continue;
4249 : :
4250 : 0 : time_t t = (time_t) lib->l_time_stamp;
4251 : 0 : struct tm *tm = gmtime (&t);
4252 [ # # ]: 0 : if (unlikely (tm == NULL))
4253 : 0 : continue;
4254 : :
4255 : 0 : printf (" [%2d] %-29s %04u-%02u-%02uT%02u:%02u:%02u %08x %-7u %u\n",
4256 : 0 : cnt, elf_strptr (ebl->elf, shdr->sh_link, lib->l_name),
4257 : 0 : tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
4258 : : tm->tm_hour, tm->tm_min, tm->tm_sec,
4259 : 0 : (unsigned int) lib->l_checksum,
4260 : 0 : (unsigned int) lib->l_version,
4261 : 0 : (unsigned int) lib->l_flags);
4262 : : }
4263 : : }
4264 : : }
4265 : : }
4266 : :
4267 : : static inline size_t
4268 : 16 : left (Elf_Data *data,
4269 : : const unsigned char *p)
4270 : : {
4271 : 16 : return (const unsigned char *) data->d_buf + data->d_size - p;
4272 : : }
4273 : :
4274 : : static void
4275 : 192 : print_attributes (Ebl *ebl, const GElf_Ehdr *ehdr)
4276 : : {
4277 : : /* Find the object attributes sections. For this we have to search
4278 : : through the section table. */
4279 : 192 : Elf_Scn *scn = NULL;
4280 : :
4281 : : /* Get the section header string table index. */
4282 : 192 : size_t shstrndx;
4283 [ - + ]: 192 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
4284 : 0 : error_exit (0, _("cannot get section header string table index"));
4285 : :
4286 [ + + ]: 6132 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
4287 : : {
4288 : 5940 : GElf_Shdr shdr_mem;
4289 : 5940 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
4290 : :
4291 [ + - + + ]: 5940 : if (shdr == NULL || (shdr->sh_type != SHT_GNU_ATTRIBUTES
4292 [ + + ]: 5934 : && (shdr->sh_type != SHT_ARM_ATTRIBUTES
4293 [ - + ]: 2 : || ehdr->e_machine != EM_ARM)
4294 [ + + ]: 5932 : && (shdr->sh_type != SHT_CSKY_ATTRIBUTES
4295 [ + - ]: 2 : || ehdr->e_machine != EM_CSKY)
4296 [ - + ]: 5932 : && (shdr->sh_type != SHT_RISCV_ATTRIBUTES
4297 [ # # ]: 0 : || ehdr->e_machine != EM_RISCV)))
4298 : 5932 : continue;
4299 : :
4300 : 8 : printf (_("\
4301 : : \nObject attributes section [%2zu] '%s' of %" PRIu64
4302 : : " bytes at offset %#0" PRIx64 ":\n"),
4303 : : elf_ndxscn (scn),
4304 : 8 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
4305 : : shdr->sh_size, shdr->sh_offset);
4306 : :
4307 : 8 : Elf_Data *data = elf_rawdata (scn, NULL);
4308 [ + - + - ]: 8 : if (unlikely (data == NULL || data->d_size == 0))
4309 : 0 : return;
4310 : :
4311 : 8 : const unsigned char *p = data->d_buf;
4312 : :
4313 : : /* There is only one 'version', A. */
4314 [ + - ]: 8 : if (unlikely (*p++ != 'A'))
4315 : : return;
4316 : :
4317 : 8 : fputs (_(" Owner Size\n"), stdout);
4318 : :
4319 : : /* Loop over the sections. */
4320 [ + + ]: 16 : while (left (data, p) >= 4)
4321 : : {
4322 : : /* Section length. */
4323 : 8 : uint32_t len;
4324 [ + + ]: 8 : memcpy (&len, p, sizeof len);
4325 : :
4326 [ + + ]: 8 : if (MY_ELFDATA != ehdr->e_ident[EI_DATA])
4327 : 4 : CONVERT (len);
4328 : :
4329 [ + - ]: 8 : if (unlikely (len > left (data, p)))
4330 : : break;
4331 : :
4332 : : /* Section vendor name. */
4333 : 8 : const unsigned char *name = p + sizeof len;
4334 : 8 : p += len;
4335 : :
4336 : 8 : unsigned const char *q = memchr (name, '\0', len);
4337 [ + - ]: 8 : if (unlikely (q == NULL))
4338 : : break;
4339 : 8 : ++q;
4340 : :
4341 : 8 : printf (_(" %-13s %4" PRIu32 "\n"), name, len);
4342 : :
4343 : 16 : bool gnu_vendor = (q - name == sizeof "gnu"
4344 [ + + - + ]: 8 : && !memcmp (name, "gnu", sizeof "gnu"));
4345 : :
4346 : : /* Loop over subsections. */
4347 [ + + ]: 8 : if (shdr->sh_type != SHT_GNU_ATTRIBUTES
4348 [ + - ]: 6 : || gnu_vendor)
4349 [ + + ]: 16 : while (q < p)
4350 : : {
4351 : 8 : const unsigned char *const sub = q;
4352 : :
4353 : 8 : unsigned int subsection_tag;
4354 : 8 : get_uleb128 (subsection_tag, q, p);
4355 [ + - ]: 8 : if (unlikely (q >= p))
4356 : : break;
4357 : :
4358 : 8 : uint32_t subsection_len;
4359 [ + - ]: 8 : if (unlikely (p - sub < (ptrdiff_t) sizeof subsection_len))
4360 : : break;
4361 : :
4362 [ + + ]: 8 : memcpy (&subsection_len, q, sizeof subsection_len);
4363 : :
4364 [ + + ]: 8 : if (MY_ELFDATA != ehdr->e_ident[EI_DATA])
4365 : 4 : CONVERT (subsection_len);
4366 : :
4367 : : /* Don't overflow, ptrdiff_t might be 32bits, but signed. */
4368 [ + - + - ]: 8 : if (unlikely (subsection_len == 0
4369 : : || subsection_len >= (uint32_t) PTRDIFF_MAX
4370 : : || p - sub < (ptrdiff_t) subsection_len))
4371 : : break;
4372 : :
4373 : 8 : const unsigned char *r = q + sizeof subsection_len;
4374 : 8 : q = sub + subsection_len;
4375 : :
4376 [ - + ]: 8 : switch (subsection_tag)
4377 : : {
4378 : 0 : default:
4379 : : /* Unknown subsection, print and skip. */
4380 : 0 : printf (_(" %-4u %12" PRIu32 "\n"),
4381 : : subsection_tag, subsection_len);
4382 : 0 : break;
4383 : :
4384 : 8 : case 1: /* Tag_File */
4385 : 8 : printf (_(" File: %11" PRIu32 "\n"),
4386 : : subsection_len);
4387 : :
4388 [ + + ]: 52 : while (r < q)
4389 : : {
4390 : 44 : unsigned int tag;
4391 : 44 : get_uleb128 (tag, r, q);
4392 [ + - ]: 44 : if (unlikely (r >= q))
4393 : : break;
4394 : :
4395 : : /* GNU style tags have either a uleb128 value,
4396 : : when lowest bit is not set, or a string
4397 : : when the lowest bit is set.
4398 : : "compatibility" (32) is special. It has
4399 : : both a string and a uleb128 value. For
4400 : : non-gnu we assume 6 till 31 only take ints.
4401 : : XXX see arm backend, do we need a separate
4402 : : hook? */
4403 : 44 : uint64_t value = 0;
4404 : 44 : const char *string = NULL;
4405 [ + - + + ]: 44 : if (tag == 32 || (tag & 1) == 0
4406 [ + - + + ]: 16 : || (! gnu_vendor && (tag > 5 && tag < 32)))
4407 : : {
4408 : : // Note r >= q check above.
4409 : 42 : get_uleb128 (value, r, q);
4410 [ + - ]: 42 : if (r > q)
4411 : : break;
4412 : : }
4413 [ + - ]: 44 : if (tag == 32
4414 [ + + ]: 44 : || ((tag & 1) != 0
4415 [ + - ]: 16 : && (gnu_vendor
4416 [ + - ]: 16 : || (! gnu_vendor && tag > 32)))
4417 [ + + + + ]: 44 : || (! gnu_vendor && tag > 3 && tag < 6))
4418 : : {
4419 : 2 : string = (const char *) r;
4420 : 2 : r = memchr (r, '\0', q - r);
4421 [ + - ]: 2 : if (r == NULL)
4422 : : break;
4423 : 2 : ++r;
4424 : : }
4425 : :
4426 : 44 : const char *tag_name = NULL;
4427 : 44 : const char *value_name = NULL;
4428 : 44 : ebl_check_object_attribute (ebl, (const char *) name,
4429 : : tag, value,
4430 : : &tag_name, &value_name);
4431 : :
4432 [ + - ]: 44 : if (tag_name != NULL)
4433 : : {
4434 [ - + ]: 44 : if (tag == 32)
4435 : 0 : printf (_(" %s: %" PRId64 ", %s\n"),
4436 : : tag_name, value, string);
4437 [ + + + + ]: 44 : else if (string == NULL && value_name == NULL)
4438 : 2 : printf (_(" %s: %" PRId64 "\n"),
4439 : : tag_name, value);
4440 : : else
4441 : 42 : printf (_(" %s: %s\n"),
4442 : : tag_name, string ?: value_name);
4443 : : }
4444 : : else
4445 : : {
4446 : : /* For "gnu" vendor 32 "compatibility" has
4447 : : already been handled above. */
4448 [ # # # # ]: 0 : assert (tag != 32
4449 : : || strcmp ((const char *) name, "gnu"));
4450 [ # # ]: 0 : if (string == NULL)
4451 : 0 : printf (_(" %u: %" PRId64 "\n"),
4452 : : tag, value);
4453 : : else
4454 : 0 : printf (_(" %u: %s\n"),
4455 : : tag, string);
4456 : : }
4457 : : }
4458 : : }
4459 : : }
4460 : : }
4461 : : }
4462 : : }
4463 : :
4464 : : /* Returns either the (relocated) data from the Dwarf, or tries to get
4465 : : the "raw" (uncompressed) data from the Elf section. Produces a
4466 : : warning if the data cannot be found (or decompressed). */
4467 : : static Elf_Data *
4468 : 810 : get_debug_elf_data (Dwarf *dbg, Ebl *ebl, int idx, Elf_Scn *scn)
4469 : : {
4470 : : /* We prefer to get the section data from the Dwarf because that
4471 : : might have been relocated already. Note this is subtly wrong if
4472 : : there are multiple sections with the same .debug name. */
4473 [ - + ]: 810 : if (dbg->sectiondata[idx] != NULL)
4474 : : return dbg->sectiondata[idx];
4475 : :
4476 : 0 : GElf_Shdr shdr_mem;
4477 : 0 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
4478 [ # # # # ]: 0 : if (shdr != NULL && (shdr->sh_flags & SHF_COMPRESSED) != 0)
4479 : : {
4480 [ # # ]: 0 : if (elf_compress (scn, 0, 0) < 0)
4481 : : {
4482 : 0 : error (0, 0, "%s [%zd] '%s'\n",
4483 : : _("Couldn't uncompress section"),
4484 : : elf_ndxscn (scn), section_name (ebl, shdr));
4485 : 0 : return NULL;
4486 : : }
4487 : : }
4488 : :
4489 : 0 : Elf_Data *data = elf_getdata (scn, NULL);
4490 [ # # ]: 0 : if (data == NULL)
4491 : 0 : error (0, 0, "%s [%zd] '%s': %s\n",
4492 : : _("Couldn't get data from section"),
4493 : : elf_ndxscn (scn), section_name (ebl, shdr), elf_errmsg (-1));
4494 : :
4495 : 0 : return elf_getdata (scn, NULL);
4496 : : }
4497 : :
4498 : : static void
4499 : 2558514 : print_dwarf_addr (Dwfl_Module *dwflmod,
4500 : : int address_size, Dwarf_Addr address, Dwarf_Addr raw,
4501 : : FILE *out)
4502 : : {
4503 : : /* See if there is a name we can give for this address. */
4504 : 2558514 : GElf_Sym sym;
4505 : 2558514 : GElf_Off off = 0;
4506 [ + + ]: 14162 : const char *name = (print_address_names && ! print_unresolved_addresses)
4507 : 13998 : ? dwfl_module_addrinfo (dwflmod, address, &off, &sym, NULL, NULL, NULL)
4508 [ + + ]: 2558514 : : NULL;
4509 : :
4510 : 2558514 : const char *scn;
4511 [ + + ]: 2558514 : if (print_unresolved_addresses)
4512 : : {
4513 : 168 : address = raw;
4514 : 168 : scn = NULL;
4515 : : }
4516 : : else
4517 : : {
4518 : : /* Relativize the address. */
4519 : 2558346 : int n = dwfl_module_relocations (dwflmod);
4520 [ + + ]: 2558346 : int i = n < 1 ? -1 : dwfl_module_relocate_address (dwflmod, &address);
4521 : :
4522 : : /* In an ET_REL file there is a section name to refer to. */
4523 : 2555594 : scn = (i < 0 ? NULL
4524 [ - + ]: 2555594 : : dwfl_module_relocation_info (dwflmod, i, NULL));
4525 : : }
4526 : :
4527 [ - + ]: 2558514 : if ((name != NULL
4528 : 13590 : ? (off != 0
4529 : : ? (scn != NULL
4530 : : ? (address_size == 0
4531 : 2 : ? fprintf (out, "%s+%#" PRIx64 " <%s+%#" PRIx64 ">",
4532 : : scn, address, name, off)
4533 : 8 : : fprintf (out, "%s+%#0*" PRIx64 " <%s+%#" PRIx64 ">",
4534 : 4 : scn, 2 + address_size * 2, address,
4535 : : name, off))
4536 : : : (address_size == 0
4537 : 364 : ? fprintf (out, "%#" PRIx64 " <%s+%#" PRIx64 ">",
4538 : : address, name, off)
4539 : 2156 : : fprintf (out, "%#0*" PRIx64 " <%s+%#" PRIx64 ">",
4540 : 1078 : 2 + address_size * 2, address,
4541 : : name, off)))
4542 : : : (scn != NULL
4543 : : ? (address_size == 0
4544 : 2 : ? fprintf (out, "%s+%#" PRIx64 " <%s>", scn, address, name)
4545 : 22280 : : fprintf (out, "%s+%#0*" PRIx64 " <%s>",
4546 : 11140 : scn, 2 + address_size * 2, address, name))
4547 : : : (address_size == 0
4548 : 160 : ? fprintf (out, "%#" PRIx64 " <%s>", address, name)
4549 : 1680 : : fprintf (out, "%#0*" PRIx64 " <%s>",
4550 : 840 : 2 + address_size * 2, address, name))))
4551 : : : (scn != NULL
4552 : : ? (address_size == 0
4553 : 937080 : ? fprintf (out, "%s+%#" PRIx64, scn, address)
4554 : 3214732 : : fprintf (out, "%s+%#0*" PRIx64,
4555 : 1607366 : scn, 2 + address_size * 2, address))
4556 : : : (address_size == 0
4557 : 70 : ? fprintf (out, "%#" PRIx64, address)
4558 : 816 : : fprintf (out, "%#0*" PRIx64,
4559 [ + + + + : 5116620 : 2 + address_size * 2, address)))) < 0)
+ + + + +
+ + + + +
+ + + + +
+ + + ]
4560 : 0 : error_exit (0, _("sprintf failure"));
4561 : 2558514 : }
4562 : :
4563 : :
4564 : : static const char *
4565 : 2638200 : dwarf_tag_string (unsigned int tag)
4566 : : {
4567 [ - + + - : 2638200 : switch (tag)
- - - - +
+ + + + -
- - - - -
+ - + - +
- - + + -
+ - - - -
- - - + -
+ - + + +
- - - - -
- + - - +
- - - + -
+ + + + -
- - - - +
+ + + - +
+ + + - -
- ]
4568 : : {
4569 : : #define DWARF_ONE_KNOWN_DW_TAG(NAME, CODE) case CODE: return #NAME;
4570 : 2638200 : DWARF_ALL_KNOWN_DW_TAG
4571 : : #undef DWARF_ONE_KNOWN_DW_TAG
4572 : 0 : default:
4573 : 0 : return NULL;
4574 : : }
4575 : : }
4576 : :
4577 : :
4578 : : static const char *
4579 : 10175774 : dwarf_attr_string (unsigned int attrnum)
4580 : : {
4581 [ + - + - : 10175774 : switch (attrnum)
- - - + -
- + + + -
- - - + +
- + - - +
- + - + -
- - - - -
- - - - -
- - - - -
- - - + -
+ - + - +
- - - + +
- - - + -
+ - + + -
- + + + +
- + + + -
+ - - + -
+ - + + -
+ - - + +
+ + - - -
- - + + +
+ - + - +
- - + - +
+ - + - +
+ - + + +
+ + + + -
- + - - -
+ - + - -
- - + + -
+ - - - -
+ - - - +
+ - + - -
- - + + -
- - - - +
+ - - - -
- - - + ]
4582 : : {
4583 : : #define DWARF_ONE_KNOWN_DW_AT(NAME, CODE) case CODE: return #NAME;
4584 : 10175772 : DWARF_ALL_KNOWN_DW_AT
4585 : : #undef DWARF_ONE_KNOWN_DW_AT
4586 : 0 : default:
4587 : 0 : return NULL;
4588 : : }
4589 : : }
4590 : :
4591 : :
4592 : : static const char *
4593 : 10187704 : dwarf_form_string (unsigned int form)
4594 : : {
4595 [ + + + + : 10187704 : switch (form)
+ - - - -
- + - - +
+ + + + +
+ + + - +
+ - - + -
- + - - +
+ + + + +
- + + - -
- + - + ]
4596 : : {
4597 : : #define DWARF_ONE_KNOWN_DW_FORM(NAME, CODE) case CODE: return #NAME;
4598 : 10187700 : DWARF_ALL_KNOWN_DW_FORM
4599 : : #undef DWARF_ONE_KNOWN_DW_FORM
4600 : 0 : default:
4601 : 0 : return NULL;
4602 : : }
4603 : : }
4604 : :
4605 : :
4606 : : static const char *
4607 : 4244 : dwarf_lang_string (unsigned int lang)
4608 : : {
4609 [ - - + - : 4244 : switch (lang)
- - - + -
- + + - +
- - + - -
- - - - -
- - - - -
+ - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - + - ]
4610 : : {
4611 : : #define DWARF_ONE_KNOWN_DW_LANG(NAME, CODE) case CODE: return #NAME;
4612 : 4242 : DWARF_ALL_KNOWN_DW_LANG
4613 : : #undef DWARF_ONE_KNOWN_DW_LANG
4614 : 2 : default:
4615 : 2 : return NULL;
4616 : : }
4617 : : }
4618 : :
4619 : :
4620 : : static const char *
4621 : 4 : dwarf_lname_string (unsigned int lname)
4622 : : {
4623 [ - - - + : 4 : switch (lname)
- + - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - ]
4624 : : {
4625 : : #define DWARF_ONE_KNOWN_DW_LNAME(NAME, CODE) case CODE: return #NAME;
4626 : 4 : DWARF_ALL_KNOWN_DW_LNAME
4627 : : #undef DWARF_ONE_KNOWN_DW_LNAME
4628 : 0 : default:
4629 : 0 : return NULL;
4630 : : }
4631 : : }
4632 : :
4633 : :
4634 : : static const char *
4635 : 8272 : dwarf_inline_string (unsigned int code)
4636 : : {
4637 : 8272 : static const char *const known[] =
4638 : : {
4639 : : #define DWARF_ONE_KNOWN_DW_INL(NAME, CODE) [CODE] = #NAME,
4640 : : DWARF_ALL_KNOWN_DW_INL
4641 : : #undef DWARF_ONE_KNOWN_DW_INL
4642 : : };
4643 : :
4644 : 8272 : if (likely (code < sizeof (known) / sizeof (known[0])))
4645 : 8272 : return known[code];
4646 : :
4647 : : return NULL;
4648 : : }
4649 : :
4650 : :
4651 : : static const char *
4652 : 67820 : dwarf_encoding_string (unsigned int code)
4653 : : {
4654 : 67820 : static const char *const known[] =
4655 : : {
4656 : : #define DWARF_ONE_KNOWN_DW_ATE(NAME, CODE) [CODE] = #NAME,
4657 : : DWARF_ALL_KNOWN_DW_ATE
4658 : : #undef DWARF_ONE_KNOWN_DW_ATE
4659 : : };
4660 : :
4661 : 67820 : if (likely (code < sizeof (known) / sizeof (known[0])))
4662 : 67820 : return known[code];
4663 : :
4664 : : return NULL;
4665 : : }
4666 : :
4667 : :
4668 : : static const char *
4669 : 0 : dwarf_access_string (unsigned int code)
4670 : : {
4671 : 0 : static const char *const known[] =
4672 : : {
4673 : : #define DWARF_ONE_KNOWN_DW_ACCESS(NAME, CODE) [CODE] = #NAME,
4674 : : DWARF_ALL_KNOWN_DW_ACCESS
4675 : : #undef DWARF_ONE_KNOWN_DW_ACCESS
4676 : : };
4677 : :
4678 : 0 : if (likely (code < sizeof (known) / sizeof (known[0])))
4679 : 0 : return known[code];
4680 : :
4681 : : return NULL;
4682 : : }
4683 : :
4684 : :
4685 : : static const char *
4686 : 0 : dwarf_defaulted_string (unsigned int code)
4687 : : {
4688 : 0 : static const char *const known[] =
4689 : : {
4690 : : #define DWARF_ONE_KNOWN_DW_DEFAULTED(NAME, CODE) [CODE] = #NAME,
4691 : : DWARF_ALL_KNOWN_DW_DEFAULTED
4692 : : #undef DWARF_ONE_KNOWN_DW_DEFAULTED
4693 : : };
4694 : :
4695 : 0 : if (likely (code < sizeof (known) / sizeof (known[0])))
4696 : 0 : return known[code];
4697 : :
4698 : : return NULL;
4699 : : }
4700 : :
4701 : :
4702 : : static const char *
4703 : 0 : dwarf_visibility_string (unsigned int code)
4704 : : {
4705 : 0 : static const char *const known[] =
4706 : : {
4707 : : #define DWARF_ONE_KNOWN_DW_VIS(NAME, CODE) [CODE] = #NAME,
4708 : : DWARF_ALL_KNOWN_DW_VIS
4709 : : #undef DWARF_ONE_KNOWN_DW_VIS
4710 : : };
4711 : :
4712 : 0 : if (likely (code < sizeof (known) / sizeof (known[0])))
4713 : 0 : return known[code];
4714 : :
4715 : : return NULL;
4716 : : }
4717 : :
4718 : :
4719 : : static const char *
4720 : 0 : dwarf_virtuality_string (unsigned int code)
4721 : : {
4722 : 0 : static const char *const known[] =
4723 : : {
4724 : : #define DWARF_ONE_KNOWN_DW_VIRTUALITY(NAME, CODE) [CODE] = #NAME,
4725 : : DWARF_ALL_KNOWN_DW_VIRTUALITY
4726 : : #undef DWARF_ONE_KNOWN_DW_VIRTUALITY
4727 : : };
4728 : :
4729 : 0 : if (likely (code < sizeof (known) / sizeof (known[0])))
4730 : 0 : return known[code];
4731 : :
4732 : : return NULL;
4733 : : }
4734 : :
4735 : :
4736 : : static const char *
4737 : 0 : dwarf_identifier_case_string (unsigned int code)
4738 : : {
4739 : 0 : static const char *const known[] =
4740 : : {
4741 : : #define DWARF_ONE_KNOWN_DW_ID(NAME, CODE) [CODE] = #NAME,
4742 : : DWARF_ALL_KNOWN_DW_ID
4743 : : #undef DWARF_ONE_KNOWN_DW_ID
4744 : : };
4745 : :
4746 : 0 : if (likely (code < sizeof (known) / sizeof (known[0])))
4747 : 0 : return known[code];
4748 : :
4749 : : return NULL;
4750 : : }
4751 : :
4752 : :
4753 : : static const char *
4754 : 0 : dwarf_calling_convention_string (unsigned int code)
4755 : : {
4756 : 0 : static const char *const known[] =
4757 : : {
4758 : : #define DWARF_ONE_KNOWN_DW_CC(NAME, CODE) [CODE] = #NAME,
4759 : : DWARF_ALL_KNOWN_DW_CC
4760 : : #undef DWARF_ONE_KNOWN_DW_CC
4761 : : };
4762 : :
4763 : 0 : if (likely (code < sizeof (known) / sizeof (known[0])))
4764 : 0 : return known[code];
4765 : :
4766 : : return NULL;
4767 : : }
4768 : :
4769 : :
4770 : : static const char *
4771 : 0 : dwarf_ordering_string (unsigned int code)
4772 : : {
4773 : 0 : static const char *const known[] =
4774 : : {
4775 : : #define DWARF_ONE_KNOWN_DW_ORD(NAME, CODE) [CODE] = #NAME,
4776 : : DWARF_ALL_KNOWN_DW_ORD
4777 : : #undef DWARF_ONE_KNOWN_DW_ORD
4778 : : };
4779 : :
4780 : 0 : if (likely (code < sizeof (known) / sizeof (known[0])))
4781 : 0 : return known[code];
4782 : :
4783 : : return NULL;
4784 : : }
4785 : :
4786 : :
4787 : : static const char *
4788 : 16 : dwarf_discr_list_string (unsigned int code)
4789 : : {
4790 : 16 : static const char *const known[] =
4791 : : {
4792 : : #define DWARF_ONE_KNOWN_DW_DSC(NAME, CODE) [CODE] = #NAME,
4793 : : DWARF_ALL_KNOWN_DW_DSC
4794 : : #undef DWARF_ONE_KNOWN_DW_DSC
4795 : : };
4796 : :
4797 : 16 : if (likely (code < sizeof (known) / sizeof (known[0])))
4798 : 16 : return known[code];
4799 : :
4800 : : return NULL;
4801 : : }
4802 : :
4803 : :
4804 : : static const char *
4805 : 1264200 : dwarf_locexpr_opcode_string (unsigned int code)
4806 : : {
4807 : 1264200 : static const char *const known[] =
4808 : : {
4809 : : /* Normally we can't afford building huge table of 64K entries,
4810 : : most of them zero, just because there are a couple defined
4811 : : values at the far end. In case of opcodes, it's OK. */
4812 : : #define DWARF_ONE_KNOWN_DW_OP(NAME, CODE) [CODE] = #NAME,
4813 : : DWARF_ALL_KNOWN_DW_OP
4814 : : #undef DWARF_ONE_KNOWN_DW_OP
4815 : : };
4816 : :
4817 : 1264200 : if (likely (code < sizeof (known) / sizeof (known[0])))
4818 : 1264200 : return known[code];
4819 : :
4820 : : return NULL;
4821 : : }
4822 : :
4823 : :
4824 : : static const char *
4825 : 3990 : dwarf_unit_string (unsigned int type)
4826 : : {
4827 [ - + + - : 3990 : switch (type)
- - + ]
4828 : : {
4829 : : #define DWARF_ONE_KNOWN_DW_UT(NAME, CODE) case CODE: return #NAME;
4830 : 14 : DWARF_ALL_KNOWN_DW_UT
4831 : : #undef DWARF_ONE_KNOWN_DW_UT
4832 : 0 : default:
4833 : 0 : return NULL;
4834 : : }
4835 : : }
4836 : :
4837 : :
4838 : : static const char *
4839 : 168166 : dwarf_range_list_encoding_string (unsigned int kind)
4840 : : {
4841 [ - + + - : 168166 : switch (kind)
+ - - -
+ ]
4842 : : {
4843 : : #define DWARF_ONE_KNOWN_DW_RLE(NAME, CODE) case CODE: return #NAME;
4844 : 149138 : DWARF_ALL_KNOWN_DW_RLE
4845 : : #undef DWARF_ONE_KNOWN_DW_RLE
4846 : 0 : default:
4847 : 0 : return NULL;
4848 : : }
4849 : : }
4850 : :
4851 : :
4852 : : static const char *
4853 : 742242 : dwarf_loc_list_encoding_string (unsigned int kind)
4854 : : {
4855 [ - - + + : 742242 : switch (kind)
- + - + -
- + ]
4856 : : {
4857 : : #define DWARF_ONE_KNOWN_DW_LLE(NAME, CODE) case CODE: return #NAME;
4858 : 690248 : DWARF_ALL_KNOWN_DW_LLE
4859 : : #undef DWARF_ONE_KNOWN_DW_LLE
4860 : : /* DW_LLE_GNU_view_pair is special/incompatible with default codes. */
4861 : 0 : case DW_LLE_GNU_view_pair: return "GNU_view_pair";
4862 : 0 : default:
4863 : 0 : return NULL;
4864 : : }
4865 : : }
4866 : :
4867 : :
4868 : : static const char *
4869 : 11930 : dwarf_line_content_description_string (unsigned int kind)
4870 : : {
4871 [ + + - - : 11930 : switch (kind)
- + ]
4872 : : {
4873 : : #define DWARF_ONE_KNOWN_DW_LNCT(NAME, CODE) case CODE: return #NAME;
4874 : 11922 : DWARF_ALL_KNOWN_DW_LNCT
4875 : : #undef DWARF_ONE_KNOWN_DW_LNCT
4876 : 0 : default:
4877 : 0 : return NULL;
4878 : : }
4879 : : }
4880 : :
4881 : :
4882 : : /* Doesn't use dwarf_section_name because we want short (max 6 chars)
4883 : : lowercase strings and result depends on version. */
4884 : : static const char *
4885 : 104 : dwarf_section_short_string (unsigned int vers, unsigned kind)
4886 : : {
4887 : 104 : const char *sec_str = NULL;
4888 [ + + + + ]: 104 : if (vers == 2 && (kind == 7 || kind == 8)) /* macinfo or macro */
4889 : : sec_str = "macro";
4890 : : else
4891 : : {
4892 [ + + + + : 96 : switch (kind)
+ + + +
- ]
4893 : : {
4894 : 12 : case DW_SECT_INFO:
4895 : 12 : sec_str = "info";
4896 : 12 : break;
4897 : 4 : case DW_SECT_TYPES:
4898 : 4 : sec_str = "types"; /* Only really valid for version 2. */
4899 : 4 : break;
4900 : 16 : case DW_SECT_ABBREV:
4901 : 16 : sec_str = "abbrv";
4902 : 16 : break;
4903 : 16 : case DW_SECT_LINE:
4904 : 16 : sec_str = "line";
4905 : 16 : break;
4906 : 16 : case DW_SECT_LOCLISTS:
4907 : 16 : sec_str = "locs";
4908 : 16 : break;
4909 : 16 : case DW_SECT_STR_OFFSETS:
4910 : 16 : sec_str = "stroff";
4911 : 16 : break;
4912 : : case DW_SECT_MACRO:
4913 : 16 : sec_str = "macro";
4914 : : break;
4915 : 8 : case DW_SECT_RNGLISTS:
4916 : 8 : sec_str = "rngs";
4917 : 8 : break;
4918 : : }
4919 : : }
4920 : :
4921 : 104 : return sec_str;
4922 : : }
4923 : :
4924 : : /* Used by all dwarf_foo_name functions. */
4925 : : static const char *
4926 : 24008362 : string_or_unknown (const char *known, unsigned int code,
4927 : : unsigned int lo_user, unsigned int hi_user,
4928 : : bool print_unknown_num)
4929 : : {
4930 : 24008362 : static char unknown_buf[20];
4931 : :
4932 [ + + ]: 24008362 : if (likely (known != NULL))
4933 : : return known;
4934 : :
4935 [ - + - - ]: 2 : if (lo_user != 0 && code >= lo_user && code <= hi_user)
4936 : : {
4937 : 0 : snprintf (unknown_buf, sizeof unknown_buf, "lo_user+%#x",
4938 : : code - lo_user);
4939 : 0 : return unknown_buf;
4940 : : }
4941 : :
4942 [ + - ]: 2 : if (print_unknown_num)
4943 : : {
4944 : 2 : snprintf (unknown_buf, sizeof unknown_buf, "??? (%#x)", code);
4945 : 2 : return unknown_buf;
4946 : : }
4947 : :
4948 : : return "???";
4949 : : }
4950 : :
4951 : :
4952 : : static const char *
4953 : 2638200 : dwarf_tag_name (unsigned int tag)
4954 : : {
4955 : 2638200 : const char *ret = dwarf_tag_string (tag);
4956 : 2638200 : return string_or_unknown (ret, tag, DW_TAG_lo_user, DW_TAG_hi_user, true);
4957 : : }
4958 : :
4959 : : static const char *
4960 : 10175774 : dwarf_attr_name (unsigned int attr)
4961 : : {
4962 : 10175774 : const char *ret = dwarf_attr_string (attr);
4963 : 10175774 : return string_or_unknown (ret, attr, DW_AT_lo_user, DW_AT_hi_user, true);
4964 : : }
4965 : :
4966 : :
4967 : : static const char *
4968 : 10187704 : dwarf_form_name (unsigned int form)
4969 : : {
4970 : 10187704 : const char *ret = dwarf_form_string (form);
4971 : 10187704 : return string_or_unknown (ret, form, 0, 0, true);
4972 : : }
4973 : :
4974 : :
4975 : : static const char *
4976 : 4240 : dwarf_lang_name (unsigned int lang)
4977 : : {
4978 : 4240 : const char *ret = dwarf_lang_string (lang);
4979 : 4240 : return string_or_unknown (ret, lang, DW_LANG_lo_user, DW_LANG_hi_user, false);
4980 : : }
4981 : :
4982 : :
4983 : : static const char *
4984 : 4 : dwarf_lname_name (unsigned int lname)
4985 : : {
4986 : 4 : const char *ret = dwarf_lname_string (lname);
4987 : 4 : return string_or_unknown (ret, lname, DW_LNAME_lo_user, DW_LNAME_hi_user,
4988 : : false);
4989 : : }
4990 : :
4991 : :
4992 : : static const char *
4993 : 8272 : dwarf_inline_name (unsigned int code)
4994 : : {
4995 [ + - ]: 8272 : const char *ret = dwarf_inline_string (code);
4996 : 8272 : return string_or_unknown (ret, code, 0, 0, false);
4997 : : }
4998 : :
4999 : :
5000 : : static const char *
5001 : 67820 : dwarf_encoding_name (unsigned int code)
5002 : : {
5003 [ + - ]: 67820 : const char *ret = dwarf_encoding_string (code);
5004 : 67820 : return string_or_unknown (ret, code, DW_ATE_lo_user, DW_ATE_hi_user, false);
5005 : : }
5006 : :
5007 : :
5008 : : static const char *
5009 : 0 : dwarf_access_name (unsigned int code)
5010 : : {
5011 [ # # ]: 0 : const char *ret = dwarf_access_string (code);
5012 : 0 : return string_or_unknown (ret, code, 0, 0, false);
5013 : : }
5014 : :
5015 : :
5016 : : static const char *
5017 : 0 : dwarf_defaulted_name (unsigned int code)
5018 : : {
5019 [ # # ]: 0 : const char *ret = dwarf_defaulted_string (code);
5020 : 0 : return string_or_unknown (ret, code, 0, 0, false);
5021 : : }
5022 : :
5023 : :
5024 : : static const char *
5025 : 0 : dwarf_visibility_name (unsigned int code)
5026 : : {
5027 [ # # ]: 0 : const char *ret = dwarf_visibility_string (code);
5028 : 0 : return string_or_unknown (ret, code, 0, 0, false);
5029 : : }
5030 : :
5031 : :
5032 : : static const char *
5033 : 0 : dwarf_virtuality_name (unsigned int code)
5034 : : {
5035 [ # # ]: 0 : const char *ret = dwarf_virtuality_string (code);
5036 : 0 : return string_or_unknown (ret, code, 0, 0, false);
5037 : : }
5038 : :
5039 : :
5040 : : static const char *
5041 : 0 : dwarf_identifier_case_name (unsigned int code)
5042 : : {
5043 [ # # ]: 0 : const char *ret = dwarf_identifier_case_string (code);
5044 : 0 : return string_or_unknown (ret, code, 0, 0, false);
5045 : : }
5046 : :
5047 : :
5048 : : static const char *
5049 : 0 : dwarf_calling_convention_name (unsigned int code)
5050 : : {
5051 [ # # ]: 0 : const char *ret = dwarf_calling_convention_string (code);
5052 : 0 : return string_or_unknown (ret, code, DW_CC_lo_user, DW_CC_hi_user, false);
5053 : : }
5054 : :
5055 : :
5056 : : static const char *
5057 : 0 : dwarf_ordering_name (unsigned int code)
5058 : : {
5059 [ # # ]: 0 : const char *ret = dwarf_ordering_string (code);
5060 : 0 : return string_or_unknown (ret, code, 0, 0, false);
5061 : : }
5062 : :
5063 : :
5064 : : static const char *
5065 : 16 : dwarf_discr_list_name (unsigned int code)
5066 : : {
5067 [ + - ]: 16 : const char *ret = dwarf_discr_list_string (code);
5068 : 16 : return string_or_unknown (ret, code, 0, 0, false);
5069 : : }
5070 : :
5071 : :
5072 : : static const char *
5073 : 3990 : dwarf_unit_name (unsigned int type)
5074 : : {
5075 : 3990 : const char *ret = dwarf_unit_string (type);
5076 : 3990 : return string_or_unknown (ret, type, DW_UT_lo_user, DW_UT_hi_user, true);
5077 : : }
5078 : :
5079 : :
5080 : : static const char *
5081 : 168166 : dwarf_range_list_encoding_name (unsigned int kind)
5082 : : {
5083 : 168166 : const char *ret = dwarf_range_list_encoding_string (kind);
5084 : 168166 : return string_or_unknown (ret, kind, 0, 0, false);
5085 : : }
5086 : :
5087 : :
5088 : : static const char *
5089 : 742242 : dwarf_loc_list_encoding_name (unsigned int kind)
5090 : : {
5091 : 742242 : const char *ret = dwarf_loc_list_encoding_string (kind);
5092 : 742242 : return string_or_unknown (ret, kind, 0, 0, false);
5093 : : }
5094 : :
5095 : :
5096 : : static const char *
5097 : 11930 : dwarf_line_content_description_name (unsigned int kind)
5098 : : {
5099 : 11930 : const char *ret = dwarf_line_content_description_string (kind);
5100 : 11930 : return string_or_unknown (ret, kind, DW_LNCT_lo_user, DW_LNCT_hi_user,
5101 : : false);
5102 : : }
5103 : :
5104 : :
5105 : : static void
5106 : 590 : print_block (size_t n, const void *block, FILE *out)
5107 : : {
5108 [ - + ]: 590 : if (n == 0)
5109 : 0 : fputs (empty_block_str, out);
5110 : : else
5111 : : {
5112 : 590 : fprintf (out, "%zu %s:", n, byte_block_str);
5113 : 590 : const unsigned char *data = block;
5114 : 2582 : do
5115 : 2582 : fprintf (out, " %02x", *data++);
5116 [ + + ]: 2582 : while (--n > 0);
5117 : 590 : fputc ('\n', out);
5118 : : }
5119 : 590 : }
5120 : :
5121 : : static void
5122 : 24 : print_bytes (size_t n, const unsigned char *bytes, FILE *out)
5123 : : {
5124 [ + + ]: 408 : while (n-- > 0)
5125 : : {
5126 : 384 : fprintf (out, "%02x", *bytes++);
5127 [ + + ]: 384 : if (n > 0)
5128 : 360 : fprintf (out, " ");
5129 : : }
5130 : 24 : }
5131 : :
5132 : : static int
5133 : 148 : get_indexed_addr (Dwarf_CU *cu, Dwarf_Word idx, Dwarf_Addr *addr)
5134 : : {
5135 [ - + ]: 148 : if (cu == NULL)
5136 : : return -1;
5137 : :
5138 : 148 : Elf_Data *debug_addr = cu->dbg->sectiondata[IDX_debug_addr];
5139 [ - + ]: 148 : if (debug_addr == NULL)
5140 : : return -1;
5141 : :
5142 : 148 : Dwarf_Off base = __libdw_cu_addr_base (cu);
5143 : 148 : Dwarf_Word off = idx * cu->address_size;
5144 [ - + ]: 148 : if (base > debug_addr->d_size
5145 [ - + ]: 148 : || off > debug_addr->d_size - base
5146 [ - + ]: 148 : || cu->address_size > debug_addr->d_size - base - off)
5147 : : return -1;
5148 : :
5149 : 148 : const unsigned char *addrp = debug_addr->d_buf + base + off;
5150 [ + + ]: 148 : if (cu->address_size == 4)
5151 [ - + ]: 12 : *addr = read_4ubyte_unaligned (cu->dbg, addrp);
5152 : : else
5153 [ - + ]: 136 : *addr = read_8ubyte_unaligned (cu->dbg, addrp);
5154 : :
5155 : : return 0;
5156 : : }
5157 : :
5158 : : static void
5159 : 887324 : print_ops (Dwfl_Module *dwflmod, Dwarf *dbg, int indent, int indentrest,
5160 : : unsigned int vers, unsigned int addrsize, unsigned int offset_size,
5161 : : struct Dwarf_CU *cu, Dwarf_Word len, const unsigned char *data,
5162 : : FILE *out)
5163 : : {
5164 [ + + ]: 887324 : const unsigned int ref_size = vers < 3 ? addrsize : offset_size;
5165 : :
5166 [ - + ]: 887324 : if (len == 0)
5167 : : {
5168 : 0 : fprintf (out, "%*s(empty)\n", indent, "");
5169 : 0 : return;
5170 : : }
5171 : :
5172 : : #define NEED(n) if (len < (Dwarf_Word) (n)) goto invalid
5173 : : #define CONSUME(n) NEED (n); else len -= (n)
5174 : :
5175 : : Dwarf_Word offset = 0;
5176 [ + + ]: 2151524 : while (len-- > 0)
5177 : 1264200 : {
5178 : 1264200 : uint_fast8_t op = *data++;
5179 : :
5180 [ + - ]: 1264200 : const char *op_name = dwarf_locexpr_opcode_string (op);
5181 [ - + ]: 1264200 : if (unlikely (op_name == NULL))
5182 : : {
5183 : 0 : static char buf[20];
5184 [ # # ]: 0 : if (op >= DW_OP_lo_user)
5185 : 0 : snprintf (buf, sizeof buf, "lo_user+%#x", op - DW_OP_lo_user);
5186 : : else
5187 : 0 : snprintf (buf, sizeof buf, "??? (%#x)", op);
5188 : : op_name = buf;
5189 : : }
5190 : :
5191 [ + - + + : 1264200 : switch (op)
+ + + + -
- + + - +
- - + + +
+ + - - -
- + + + ]
5192 : : {
5193 : 30650 : case DW_OP_addr:;
5194 : : /* Address operand. */
5195 : 30650 : Dwarf_Word addr;
5196 [ - + ]: 30650 : NEED (addrsize);
5197 [ + + ]: 30650 : if (addrsize == 4)
5198 [ + + ]: 108 : addr = read_4ubyte_unaligned (dbg, data);
5199 [ + - ]: 30542 : else if (addrsize == 8)
5200 [ + + ]: 30542 : addr = read_8ubyte_unaligned (dbg, data);
5201 : : else
5202 : 0 : goto invalid;
5203 : 30650 : data += addrsize;
5204 : 30650 : CONSUME (addrsize);
5205 : :
5206 : 30650 : fprintf (out, "%*s[%2" PRIuMAX "] %s ",
5207 : : indent, "", (uintmax_t) offset, op_name);
5208 : 30650 : print_dwarf_addr (dwflmod, 0, addr, addr, out);
5209 : 30650 : fprintf (out, "\n");
5210 : :
5211 : 30650 : offset += 1 + addrsize;
5212 : 30650 : break;
5213 : :
5214 : 0 : case DW_OP_call_ref:
5215 : : case DW_OP_GNU_variable_value:
5216 : : /* Offset operand. */
5217 [ # # ]: 0 : if (ref_size != 4 && ref_size != 8)
5218 : 0 : goto invalid; /* Cannot be used in CFA. */
5219 [ # # ]: 0 : NEED (ref_size);
5220 [ # # ]: 0 : if (ref_size == 4)
5221 [ # # ]: 0 : addr = read_4ubyte_unaligned (dbg, data);
5222 : : else
5223 [ # # ]: 0 : addr = read_8ubyte_unaligned (dbg, data);
5224 : 0 : data += ref_size;
5225 : 0 : CONSUME (ref_size);
5226 : : /* addr is a DIE offset, so format it as one. */
5227 : 0 : fprintf (out, "%*s[%2" PRIuMAX "] %s [%6" PRIxMAX "]\n",
5228 : : indent, "", (uintmax_t) offset,
5229 : : op_name, (uintmax_t) addr);
5230 : 0 : offset += 1 + ref_size;
5231 : 0 : break;
5232 : :
5233 : 28434 : case DW_OP_deref_size:
5234 : : case DW_OP_xderef_size:
5235 : : case DW_OP_pick:
5236 : : case DW_OP_const1u:
5237 : : // XXX value might be modified by relocation
5238 [ - + ]: 28434 : NEED (1);
5239 : 56868 : fprintf (out, "%*s[%2" PRIuMAX "] %s %" PRIu8 "\n",
5240 : : indent, "", (uintmax_t) offset,
5241 : 28434 : op_name, *((uint8_t *) data));
5242 : 28434 : ++data;
5243 : 28434 : --len;
5244 : 28434 : offset += 2;
5245 : 28434 : break;
5246 : :
5247 : 3742 : case DW_OP_const2u:
5248 [ - + ]: 3742 : NEED (2);
5249 : : // XXX value might be modified by relocation
5250 : 7484 : fprintf (out, "%*s[%2" PRIuMAX "] %s %" PRIu16 "\n",
5251 : : indent, "", (uintmax_t) offset,
5252 [ - + ]: 3742 : op_name, read_2ubyte_unaligned (dbg, data));
5253 : 3742 : CONSUME (2);
5254 : 3742 : data += 2;
5255 : 3742 : offset += 3;
5256 : 3742 : break;
5257 : :
5258 : 3326 : case DW_OP_const4u:
5259 [ - + ]: 3326 : NEED (4);
5260 : : // XXX value might be modified by relocation
5261 : 3326 : fprintf (out, "%*s[%2" PRIuMAX "] %s %" PRIu32 "\n",
5262 : : indent, "", (uintmax_t) offset,
5263 [ - + ]: 3326 : op_name, read_4ubyte_unaligned (dbg, data));
5264 : 3326 : CONSUME (4);
5265 : 3326 : data += 4;
5266 : 3326 : offset += 5;
5267 : 3326 : break;
5268 : :
5269 : 138 : case DW_OP_const8u:
5270 [ - + ]: 138 : NEED (8);
5271 : : // XXX value might be modified by relocation
5272 : 138 : fprintf (out, "%*s[%2" PRIuMAX "] %s %" PRIu64 "\n",
5273 : : indent, "", (uintmax_t) offset,
5274 [ - + ]: 138 : op_name, (uint64_t) read_8ubyte_unaligned (dbg, data));
5275 : 138 : CONSUME (8);
5276 : 138 : data += 8;
5277 : 138 : offset += 9;
5278 : 138 : break;
5279 : :
5280 : 5488 : case DW_OP_const1s:
5281 [ - + ]: 5488 : NEED (1);
5282 : : // XXX value might be modified by relocation
5283 : 10976 : fprintf (out, "%*s[%2" PRIuMAX "] %s %" PRId8 "\n",
5284 : : indent, "", (uintmax_t) offset,
5285 : 5488 : op_name, *((int8_t *) data));
5286 : 5488 : ++data;
5287 : 5488 : --len;
5288 : 5488 : offset += 2;
5289 : 5488 : break;
5290 : :
5291 : 12 : case DW_OP_const2s:
5292 [ - + ]: 12 : NEED (2);
5293 : : // XXX value might be modified by relocation
5294 : 24 : fprintf (out, "%*s[%2" PRIuMAX "] %s %" PRId16 "\n",
5295 : : indent, "", (uintmax_t) offset,
5296 [ - + ]: 12 : op_name, read_2sbyte_unaligned (dbg, data));
5297 : 12 : CONSUME (2);
5298 : 12 : data += 2;
5299 : 12 : offset += 3;
5300 : 12 : break;
5301 : :
5302 : 0 : case DW_OP_const4s:
5303 [ # # ]: 0 : NEED (4);
5304 : : // XXX value might be modified by relocation
5305 : 0 : fprintf (out, "%*s[%2" PRIuMAX "] %s %" PRId32 "\n",
5306 : : indent, "", (uintmax_t) offset,
5307 [ # # ]: 0 : op_name, read_4sbyte_unaligned (dbg, data));
5308 : 0 : CONSUME (4);
5309 : 0 : data += 4;
5310 : 0 : offset += 5;
5311 : 0 : break;
5312 : :
5313 : 0 : case DW_OP_const8s:
5314 [ # # ]: 0 : NEED (8);
5315 : : // XXX value might be modified by relocation
5316 : 0 : fprintf (out, "%*s[%2" PRIuMAX "] %s %" PRId64 "\n",
5317 : : indent, "", (uintmax_t) offset,
5318 [ # # ]: 0 : op_name, read_8sbyte_unaligned (dbg, data));
5319 : 0 : CONSUME (8);
5320 : 0 : data += 8;
5321 : 0 : offset += 9;
5322 : 0 : break;
5323 : :
5324 : 28532 : case DW_OP_piece:
5325 : : case DW_OP_regx:
5326 : : case DW_OP_plus_uconst:
5327 : 28532 : case DW_OP_constu:;
5328 : 28532 : const unsigned char *start = data;
5329 : 28532 : uint64_t uleb;
5330 [ - + ]: 28532 : NEED (1);
5331 : 28532 : get_uleb128 (uleb, data, data + len);
5332 : 28532 : fprintf (out, "%*s[%2" PRIuMAX "] %s %" PRIu64 "\n",
5333 : : indent, "", (uintmax_t) offset, op_name, uleb);
5334 [ - + ]: 28532 : CONSUME (data - start);
5335 : 28532 : offset += 1 + (data - start);
5336 : 28532 : break;
5337 : :
5338 : 16 : case DW_OP_addrx:
5339 : : case DW_OP_GNU_addr_index:
5340 : : case DW_OP_constx:
5341 : 16 : case DW_OP_GNU_const_index:;
5342 : 16 : start = data;
5343 [ - + ]: 16 : NEED (1);
5344 : 16 : get_uleb128 (uleb, data, data + len);
5345 : 16 : fprintf (out, "%*s[%2" PRIuMAX "] %s [%" PRIu64 "] ",
5346 : : indent, "", (uintmax_t) offset, op_name, uleb);
5347 [ - + ]: 16 : CONSUME (data - start);
5348 : 16 : offset += 1 + (data - start);
5349 [ - + ]: 16 : if (get_indexed_addr (cu, uleb, &addr) != 0)
5350 : 0 : fprintf (out, "???\n");
5351 : : else
5352 : : {
5353 : 16 : print_dwarf_addr (dwflmod, 0, addr, addr, out);
5354 : 16 : fprintf (out, "\n");
5355 : : }
5356 : : break;
5357 : :
5358 : 0 : case DW_OP_bit_piece:
5359 : 0 : start = data;
5360 : 0 : uint64_t uleb2;
5361 [ # # ]: 0 : NEED (1);
5362 : 0 : get_uleb128 (uleb, data, data + len);
5363 : 0 : NEED (1);
5364 : 0 : get_uleb128 (uleb2, data, data + len);
5365 : 0 : fprintf (out, "%*s[%2" PRIuMAX "] %s %" PRIu64 ", %" PRIu64 "\n",
5366 : : indent, "", (uintmax_t) offset, op_name, uleb, uleb2);
5367 [ # # ]: 0 : CONSUME (data - start);
5368 : 0 : offset += 1 + (data - start);
5369 : 0 : break;
5370 : :
5371 : 210782 : case DW_OP_fbreg:
5372 : : case DW_OP_breg0 ... DW_OP_breg31:
5373 : : case DW_OP_consts:
5374 : 210782 : start = data;
5375 : 210782 : int64_t sleb;
5376 [ - + ]: 210782 : NEED (1);
5377 : 210782 : get_sleb128 (sleb, data, data + len);
5378 : 210782 : fprintf (out, "%*s[%2" PRIuMAX "] %s %" PRId64 "\n",
5379 : : indent, "", (uintmax_t) offset, op_name, sleb);
5380 [ - + ]: 210782 : CONSUME (data - start);
5381 : 210782 : offset += 1 + (data - start);
5382 : 210782 : break;
5383 : :
5384 : 0 : case DW_OP_bregx:
5385 : 0 : start = data;
5386 [ # # ]: 0 : NEED (1);
5387 : 0 : get_uleb128 (uleb, data, data + len);
5388 : 0 : NEED (1);
5389 : 0 : get_sleb128 (sleb, data, data + len);
5390 : 0 : fprintf (out, "%*s[%2" PRIuMAX "] %s %" PRIu64 " %" PRId64 "\n",
5391 : : indent, "", (uintmax_t) offset, op_name, uleb, sleb);
5392 [ # # ]: 0 : CONSUME (data - start);
5393 : 0 : offset += 1 + (data - start);
5394 : 0 : break;
5395 : :
5396 : 0 : case DW_OP_call2:
5397 [ # # ]: 0 : NEED (2);
5398 : 0 : fprintf (out, "%*s[%2" PRIuMAX "] %s [%6" PRIx16 "]\n",
5399 : : indent, "", (uintmax_t) offset, op_name,
5400 [ # # ]: 0 : read_2ubyte_unaligned (dbg, data));
5401 : 0 : CONSUME (2);
5402 : 0 : data += 2;
5403 : 0 : offset += 3;
5404 : 0 : break;
5405 : :
5406 : 8 : case DW_OP_call4:
5407 [ - + ]: 8 : NEED (4);
5408 : 8 : fprintf (out, "%*s[%2" PRIuMAX "] %s [%6" PRIx32 "]\n",
5409 : : indent, "", (uintmax_t) offset, op_name,
5410 [ - + ]: 8 : read_4ubyte_unaligned (dbg, data));
5411 : 8 : CONSUME (4);
5412 : 8 : data += 4;
5413 : 8 : offset += 5;
5414 : 8 : break;
5415 : :
5416 : 1262 : case DW_OP_skip:
5417 : : case DW_OP_bra:
5418 [ - + ]: 1262 : NEED (2);
5419 : 2524 : fprintf (out, "%*s[%2" PRIuMAX "] %s %" PRIuMAX "\n",
5420 : : indent, "", (uintmax_t) offset, op_name,
5421 [ - + ]: 1262 : (uintmax_t) (offset + read_2sbyte_unaligned (dbg, data) + 3));
5422 : 1262 : CONSUME (2);
5423 : 1262 : data += 2;
5424 : 1262 : offset += 3;
5425 : 1262 : break;
5426 : :
5427 : 590 : case DW_OP_implicit_value:
5428 : 590 : start = data;
5429 [ - + ]: 590 : NEED (1);
5430 : 590 : get_uleb128 (uleb, data, data + len);
5431 : 590 : fprintf (out, "%*s[%2" PRIuMAX "] %s: ",
5432 : : indent, "", (uintmax_t) offset, op_name);
5433 [ - + ]: 590 : NEED (uleb);
5434 : 590 : print_block (uleb, data, out);
5435 : 590 : data += uleb;
5436 [ - + ]: 590 : CONSUME (data - start);
5437 : 590 : offset += 1 + (data - start);
5438 : 590 : break;
5439 : :
5440 : 6824 : case DW_OP_implicit_pointer:
5441 : : case DW_OP_GNU_implicit_pointer:
5442 : : /* DIE offset operand. */
5443 : 6824 : start = data;
5444 [ - + ]: 6824 : NEED (ref_size);
5445 [ - + ]: 6824 : if (ref_size != 4 && ref_size != 8)
5446 : 0 : goto invalid; /* Cannot be used in CFA. */
5447 [ + - ]: 6824 : if (ref_size == 4)
5448 [ - + ]: 6824 : addr = read_4ubyte_unaligned (dbg, data);
5449 : : else
5450 [ # # ]: 0 : addr = read_8ubyte_unaligned (dbg, data);
5451 : 6824 : data += ref_size;
5452 : : /* Byte offset operand. */
5453 [ - + ]: 6824 : NEED (1);
5454 : 6824 : get_sleb128 (sleb, data, data + len);
5455 : :
5456 : 6824 : fprintf (out, "%*s[%2" PRIuMAX "] %s [%6" PRIxMAX "] %+" PRId64 "\n",
5457 : : indent, "", (intmax_t) offset,
5458 : : op_name, (uintmax_t) addr, sleb);
5459 [ - + ]: 6824 : CONSUME (data - start);
5460 : 6824 : offset += 1 + (data - start);
5461 : 6824 : break;
5462 : :
5463 : 71302 : case DW_OP_entry_value:
5464 : : case DW_OP_GNU_entry_value:
5465 : : /* Size plus expression block. */
5466 : 71302 : start = data;
5467 [ - + ]: 71302 : NEED (1);
5468 : 71302 : get_uleb128 (uleb, data, data + len);
5469 : 71302 : fprintf (out, "%*s[%2" PRIuMAX "] %s:\n",
5470 : : indent, "", (uintmax_t) offset, op_name);
5471 [ - + ]: 71302 : NEED (uleb);
5472 : 71302 : print_ops (dwflmod, dbg, indent + 5, indent + 5, vers,
5473 : : addrsize, offset_size, cu, uleb, data, out);
5474 : 71302 : data += uleb;
5475 [ - + ]: 71302 : CONSUME (data - start);
5476 : 71302 : offset += 1 + (data - start);
5477 : 71302 : break;
5478 : :
5479 : 0 : case DW_OP_const_type:
5480 : : case DW_OP_GNU_const_type:
5481 : : /* uleb128 CU relative DW_TAG_base_type DIE offset, 1-byte
5482 : : unsigned size plus block. */
5483 : 0 : start = data;
5484 [ # # ]: 0 : NEED (1);
5485 : 0 : get_uleb128 (uleb, data, data + len);
5486 [ # # # # ]: 0 : if (! print_unresolved_addresses && cu != NULL)
5487 : 0 : uleb += cu->start;
5488 : 0 : NEED (1);
5489 : 0 : uint8_t usize = *(uint8_t *) data++;
5490 [ # # ]: 0 : NEED (usize);
5491 : 0 : fprintf (out, "%*s[%2" PRIuMAX "] %s [%6" PRIxMAX "] ",
5492 : : indent, "", (uintmax_t) offset, op_name, uleb);
5493 : 0 : print_block (usize, data, out);
5494 : 0 : data += usize;
5495 [ # # ]: 0 : CONSUME (data - start);
5496 : 0 : offset += 1 + (data - start);
5497 : 0 : break;
5498 : :
5499 : 0 : case DW_OP_regval_type:
5500 : : case DW_OP_GNU_regval_type:
5501 : : /* uleb128 register number, uleb128 CU relative
5502 : : DW_TAG_base_type DIE offset. */
5503 : 0 : start = data;
5504 [ # # ]: 0 : NEED (1);
5505 : 0 : get_uleb128 (uleb, data, data + len);
5506 : 0 : NEED (1);
5507 : 0 : get_uleb128 (uleb2, data, data + len);
5508 [ # # # # ]: 0 : if (! print_unresolved_addresses && cu != NULL)
5509 : 0 : uleb2 += cu->start;
5510 : 0 : fprintf (out, "%*s[%2" PRIuMAX "] %s %" PRIu64 " [%6" PRIx64 "]\n",
5511 : : indent, "", (uintmax_t) offset, op_name, uleb, uleb2);
5512 [ # # ]: 0 : CONSUME (data - start);
5513 : 0 : offset += 1 + (data - start);
5514 : 0 : break;
5515 : :
5516 : 0 : case DW_OP_deref_type:
5517 : : case DW_OP_GNU_deref_type:
5518 : : /* 1-byte unsigned size of value, uleb128 CU relative
5519 : : DW_TAG_base_type DIE offset. */
5520 : 0 : start = data;
5521 [ # # ]: 0 : NEED (1);
5522 : 0 : usize = *(uint8_t *) data++;
5523 : 0 : NEED (1);
5524 : 0 : get_uleb128 (uleb, data, data + len);
5525 [ # # # # ]: 0 : if (! print_unresolved_addresses && cu != NULL)
5526 : 0 : uleb += cu->start;
5527 : 0 : fprintf (out, "%*s[%2" PRIuMAX "] %s %" PRIu8 " [%6" PRIxMAX "]\n",
5528 : : indent, "", (uintmax_t) offset,
5529 : : op_name, usize, uleb);
5530 [ # # ]: 0 : CONSUME (data - start);
5531 : 0 : offset += 1 + (data - start);
5532 : 0 : break;
5533 : :
5534 : 0 : case DW_OP_xderef_type:
5535 : : /* 1-byte unsigned size of value, uleb128 base_type DIE offset. */
5536 : 0 : start = data;
5537 [ # # ]: 0 : NEED (1);
5538 : 0 : usize = *(uint8_t *) data++;
5539 : 0 : NEED (1);
5540 : 0 : get_uleb128 (uleb, data, data + len);
5541 : 0 : fprintf (out, "%*s[%4" PRIuMAX "] %s %" PRIu8 " [%6" PRIxMAX "]\n",
5542 : : indent, "", (uintmax_t) offset,
5543 : : op_name, usize, uleb);
5544 [ # # ]: 0 : CONSUME (data - start);
5545 : 0 : offset += 1 + (data - start);
5546 : 0 : break;
5547 : :
5548 : 894 : case DW_OP_convert:
5549 : : case DW_OP_GNU_convert:
5550 : : case DW_OP_reinterpret:
5551 : : case DW_OP_GNU_reinterpret:
5552 : : /* uleb128 CU relative offset to DW_TAG_base_type, or zero
5553 : : for conversion to untyped. */
5554 : 894 : start = data;
5555 [ - + ]: 894 : NEED (1);
5556 : 894 : get_uleb128 (uleb, data, data + len);
5557 [ + + + - : 894 : if (uleb != 0 && ! print_unresolved_addresses && cu != NULL)
+ - ]
5558 : 596 : uleb += cu->start;
5559 : 894 : fprintf (out, "%*s[%2" PRIuMAX "] %s [%6" PRIxMAX "]\n",
5560 : : indent, "", (uintmax_t) offset, op_name, uleb);
5561 [ - + ]: 894 : CONSUME (data - start);
5562 : 894 : offset += 1 + (data - start);
5563 : 894 : break;
5564 : :
5565 : 8 : case DW_OP_GNU_parameter_ref:
5566 : : /* 4 byte CU relative reference to the abstract optimized away
5567 : : DW_TAG_formal_parameter. */
5568 [ - + ]: 8 : NEED (4);
5569 [ - + ]: 8 : uintmax_t param_off = (uintmax_t) read_4ubyte_unaligned (dbg, data);
5570 [ + - + - ]: 8 : if (! print_unresolved_addresses && cu != NULL)
5571 : 8 : param_off += cu->start;
5572 : 8 : fprintf (out, "%*s[%2" PRIuMAX "] %s [%6" PRIxMAX "]\n",
5573 : : indent, "", (uintmax_t) offset, op_name, param_off);
5574 : 8 : CONSUME (4);
5575 : 8 : data += 4;
5576 : 8 : offset += 5;
5577 : 8 : break;
5578 : :
5579 : : default:
5580 : : /* No Operand. */
5581 : 872192 : fprintf (out, "%*s[%2" PRIuMAX "] %s\n",
5582 : : indent, "", (uintmax_t) offset, op_name);
5583 : 872192 : ++offset;
5584 : 872192 : break;
5585 : : }
5586 : :
5587 : 1264200 : indent = indentrest;
5588 : 1264200 : continue;
5589 : :
5590 : 0 : invalid:
5591 : 0 : fprintf (out, _("%*s[%2" PRIuMAX "] %s <TRUNCATED>\n"),
5592 : : indent, "", (uintmax_t) offset, op_name);
5593 : 0 : break;
5594 : : }
5595 : : }
5596 : :
5597 : :
5598 : : /* Turn the addresses into file offsets by using the phdrs. */
5599 : : static void
5600 : 2 : find_offsets(Elf *elf, GElf_Addr main_bias, size_t n,
5601 : : GElf_Addr addrs[n], GElf_Off offs[n])
5602 : : {
5603 : 2 : size_t unsolved = n;
5604 [ + + ]: 18 : for (size_t i = 0; i < phnum; ++i) {
5605 : 16 : GElf_Phdr phdr_mem;
5606 : 16 : GElf_Phdr *phdr = gelf_getphdr(elf, i, &phdr_mem);
5607 [ + - + + : 16 : if (phdr != NULL && phdr->p_type == PT_LOAD && phdr->p_memsz > 0)
+ - ]
5608 [ + + ]: 46 : for (size_t j = 0; j < n; ++j)
5609 [ + + + + ]: 44 : if (offs[j] == 0 && addrs[j] >= phdr->p_vaddr + main_bias &&
5610 [ + - ]: 22 : addrs[j] - (phdr->p_vaddr + main_bias) < phdr->p_filesz) {
5611 : 22 : offs[j] = addrs[j] - (phdr->p_vaddr + main_bias) + phdr->p_offset;
5612 [ + + ]: 22 : if (--unsolved == 0)
5613 : : break;
5614 : : }
5615 : : }
5616 : 2 : }
5617 : :
5618 : : /* The dynamic segment (type PT_DYNAMIC), contains the .dynamic section.
5619 : : And .dynamic section contains an array of the dynamic structures.
5620 : : We use the array to get:
5621 : : DT_STRTAB: the address of the string table
5622 : : DT_SYMTAB: the address of the symbol table
5623 : : DT_STRSZ: the size, in bytes, of the string table
5624 : : ... */
5625 : : static void
5626 : 2 : get_dynscn_addrs(Elf *elf, GElf_Phdr *phdr, GElf_Addr addrs[i_max])
5627 : : {
5628 : 2 : Elf_Data *data = elf_getdata_rawchunk(
5629 : 2 : elf, phdr->p_offset, phdr->p_filesz, ELF_T_DYN);
5630 : :
5631 : 2 : int dyn_idx = 0;
5632 : 44 : for (;; ++dyn_idx) {
5633 : 46 : GElf_Dyn dyn_mem;
5634 : 46 : GElf_Dyn *dyn = gelf_getdyn(data, dyn_idx, &dyn_mem);
5635 : : /* DT_NULL Marks end of dynamic section. */
5636 [ + - + + ]: 46 : if (dyn == NULL || dyn->d_tag == DT_NULL)
5637 : : break;
5638 : :
5639 [ + - + + : 44 : switch (dyn->d_tag) {
+ + + + +
+ - + ]
5640 : 2 : case DT_SYMTAB:
5641 : 2 : addrs[i_symtab] = dyn->d_un.d_ptr;
5642 : 2 : break;
5643 : :
5644 : 0 : case DT_HASH:
5645 : 0 : addrs[i_hash] = dyn->d_un.d_ptr;
5646 : 0 : break;
5647 : :
5648 : 2 : case DT_GNU_HASH:
5649 : 2 : addrs[i_gnu_hash] = dyn->d_un.d_ptr;
5650 : 2 : break;
5651 : :
5652 : 2 : case DT_STRTAB:
5653 : 2 : addrs[i_strtab] = dyn->d_un.d_ptr;
5654 : 2 : break;
5655 : :
5656 : 2 : case DT_VERSYM:
5657 : 2 : addrs[i_versym] = dyn->d_un.d_ptr;
5658 : 2 : break;
5659 : :
5660 : 2 : case DT_VERDEF:
5661 : 2 : addrs[i_verdef] = dyn->d_un.d_ptr;
5662 : 2 : break;
5663 : :
5664 : 2 : case DT_VERDEFNUM:
5665 : 2 : addrs[i_verdefnum] = dyn->d_un.d_val;
5666 : 2 : break;
5667 : :
5668 : 2 : case DT_VERNEED:
5669 : 2 : addrs[i_verneed] = dyn->d_un.d_ptr;
5670 : 2 : break;
5671 : :
5672 : 2 : case DT_VERNEEDNUM:
5673 : 2 : addrs[i_verneednum] = dyn->d_un.d_val;
5674 : 2 : break;
5675 : :
5676 : 2 : case DT_STRSZ:
5677 : 2 : addrs[i_strsz] = dyn->d_un.d_val;
5678 : 2 : break;
5679 : :
5680 : 0 : case DT_SYMTAB_SHNDX:
5681 : 0 : addrs[i_symtab_shndx] = dyn->d_un.d_ptr;
5682 : 0 : break;
5683 : : }
5684 : : }
5685 : 2 : }
5686 : :
5687 : :
5688 : : /* Use dynamic segment to get data for the string table section. */
5689 : : static Elf_Data *
5690 : 2 : get_dynscn_strtab(Elf *elf, GElf_Phdr *phdr)
5691 : : {
5692 : 2 : Elf_Data *strtab_data;
5693 : 2 : GElf_Addr addrs[i_max] = {0,};
5694 : 2 : GElf_Off offs[i_max] = {0,};
5695 : 2 : get_dynscn_addrs(elf, phdr, addrs);
5696 : 2 : find_offsets(elf, 0, i_max, addrs, offs);
5697 : 4 : strtab_data = elf_getdata_rawchunk(
5698 : 2 : elf, offs[i_strtab], addrs[i_strsz], ELF_T_BYTE);
5699 : 2 : return strtab_data;
5700 : : }
5701 : :
5702 : :
5703 : : struct listptr
5704 : : {
5705 : : Dwarf_Off offset:(64 - 3);
5706 : : bool addr64:1;
5707 : : bool dwarf64:1;
5708 : : bool warned:1;
5709 : : struct Dwarf_CU *cu;
5710 : : unsigned int attr;
5711 : : };
5712 : :
5713 : : #define listptr_offset_size(p) ((p)->dwarf64 ? 8 : 4)
5714 : : #define listptr_address_size(p) ((p)->addr64 ? 8 : 4)
5715 : :
5716 : : static Dwarf_Addr
5717 : 6058 : cudie_base (Dwarf_Die *cudie)
5718 : : {
5719 : 6058 : Dwarf_Addr base;
5720 : : /* Find the base address of the compilation unit. It will normally
5721 : : be specified by DW_AT_low_pc. In DWARF-3 draft 4, the base
5722 : : address could be overridden by DW_AT_entry_pc. It's been
5723 : : removed, but GCC emits DW_AT_entry_pc and not DW_AT_lowpc for
5724 : : compilation units with discontinuous ranges. */
5725 [ - + ]: 6058 : if (unlikely (dwarf_lowpc (cudie, &base) != 0))
5726 : : {
5727 : 0 : Dwarf_Attribute attr_mem;
5728 [ # # ]: 0 : if (dwarf_formaddr (dwarf_attr (cudie, DW_AT_entry_pc, &attr_mem),
5729 : : &base) != 0)
5730 : 0 : base = 0;
5731 : : }
5732 : 6058 : return base;
5733 : : }
5734 : :
5735 : : static Dwarf_Addr
5736 : 6058 : listptr_base (struct listptr *p)
5737 : : {
5738 : 6058 : Dwarf_Die cu = CUDIE (p->cu);
5739 : 6058 : return cudie_base (&cu);
5740 : : }
5741 : :
5742 : : /* To store the name used in compare_listptr */
5743 : : static _Thread_local const char *sort_listptr_name;
5744 : :
5745 : : static int
5746 : 2045496 : compare_listptr (const void *a, const void *b)
5747 : : {
5748 : 2045496 : const char *name = sort_listptr_name;
5749 : 2045496 : struct listptr *p1 = (void *) a;
5750 : 2045496 : struct listptr *p2 = (void *) b;
5751 : :
5752 [ + + ]: 2045496 : if (p1->offset < p2->offset)
5753 : : return -1;
5754 [ + + ]: 200568 : if (p1->offset > p2->offset)
5755 : : return 1;
5756 : :
5757 [ + - - + ]: 9594 : if (!p1->warned && !p2->warned)
5758 : : {
5759 [ - + ]: 9594 : if (p1->addr64 != p2->addr64)
5760 : : {
5761 : 0 : p1->warned = p2->warned = true;
5762 : 0 : error (0, 0,
5763 : 0 : _("%s %#" PRIx64 " used with different address sizes"),
5764 : : name, (uint64_t) p1->offset);
5765 : : }
5766 [ - + ]: 9594 : if (p1->dwarf64 != p2->dwarf64)
5767 : : {
5768 : 0 : p1->warned = p2->warned = true;
5769 : 0 : error (0, 0,
5770 : 0 : _("%s %#" PRIx64 " used with different offset sizes"),
5771 : 0 : name, (uint64_t) p1->offset);
5772 : : }
5773 : :
5774 : : /* Note: CUs can share the same listptr_base. So we don't check
5775 : : (listptr_base (p1) != listptr_base (p2)) */
5776 : :
5777 [ + - ]: 9594 : if (p1->attr != p2 ->attr)
5778 : : {
5779 : 0 : p1->warned = p2->warned = true;
5780 : 0 : error (0, 0,
5781 : 0 : _("%s %#" PRIx64
5782 : : " used with different attribute %s and %s"),
5783 : 0 : name, (uint64_t) p1->offset, dwarf_attr_name (p1->attr),
5784 : : dwarf_attr_name (p2->attr));
5785 : : }
5786 : : }
5787 : :
5788 : : return 0;
5789 : : }
5790 : :
5791 : : struct listptr_table
5792 : : {
5793 : : size_t n;
5794 : : size_t alloc;
5795 : : struct listptr *table;
5796 : : };
5797 : :
5798 : : static struct listptr_table known_locsptr;
5799 : : static struct listptr_table known_loclistsptr;
5800 : : static struct listptr_table known_rangelistptr;
5801 : : static struct listptr_table known_rnglistptr;
5802 : : static struct listptr_table known_addrbases;
5803 : : static struct listptr_table known_stroffbases;
5804 : :
5805 : : static void
5806 : 506 : reset_listptr (struct listptr_table *table)
5807 : : {
5808 : 506 : free (table->table);
5809 : 506 : table->table = NULL;
5810 : 506 : table->n = table->alloc = 0;
5811 : : }
5812 : :
5813 : : /* Returns false if offset doesn't fit. See struct listptr. */
5814 : : static bool
5815 : 292064 : notice_listptr (enum section_e section, struct listptr_table *table,
5816 : : uint_fast8_t address_size, uint_fast8_t offset_size,
5817 : : struct Dwarf_CU *cu, Dwarf_Off offset, unsigned int attr)
5818 : : {
5819 [ + + ]: 292064 : if (print_debug_sections & section)
5820 : : {
5821 [ + + ]: 291812 : if (table->n == table->alloc)
5822 : : {
5823 [ + + ]: 420 : if (table->alloc == 0)
5824 : 174 : table->alloc = 128;
5825 : : else
5826 : 246 : table->alloc *= 2;
5827 : 420 : table->table = xrealloc (table->table,
5828 : 420 : table->alloc * sizeof table->table[0]);
5829 : : }
5830 : :
5831 : 291812 : struct listptr *p = &table->table[table->n++];
5832 : :
5833 : 291812 : *p = (struct listptr)
5834 : : {
5835 : 291812 : .addr64 = address_size == 8,
5836 : 291812 : .dwarf64 = offset_size == 8,
5837 : : .offset = offset,
5838 : : .cu = cu,
5839 : : .attr = attr
5840 : : };
5841 : :
5842 [ + - ]: 291812 : if (p->offset != offset)
5843 : : {
5844 : 0 : table->n--;
5845 : 0 : return false;
5846 : : }
5847 : : }
5848 : : return true;
5849 : : }
5850 : :
5851 : : static void
5852 : 182 : sort_listptr (struct listptr_table *table, const char *name)
5853 : : {
5854 [ + + ]: 182 : if (table->n > 0)
5855 : : {
5856 : 174 : sort_listptr_name = name;
5857 : 174 : qsort (table->table, table->n, sizeof table->table[0],
5858 : : &compare_listptr);
5859 : : }
5860 : 182 : }
5861 : :
5862 : : static bool
5863 : 208 : skip_listptr_hole (struct listptr_table *table, size_t *idxp,
5864 : : uint_fast8_t *address_sizep, uint_fast8_t *offset_sizep,
5865 : : Dwarf_Addr *base, struct Dwarf_CU **cu, ptrdiff_t offset,
5866 : : unsigned char **readp, unsigned char *endp,
5867 : : unsigned int *attr)
5868 : : {
5869 [ - + ]: 208 : if (table->n == 0)
5870 : : return false;
5871 : :
5872 [ + - + + ]: 336 : while (*idxp < table->n && table->table[*idxp].offset < (Dwarf_Off) offset)
5873 : 128 : ++*idxp;
5874 : :
5875 : 208 : struct listptr *p = &table->table[*idxp];
5876 : :
5877 [ + - ]: 208 : if (*idxp == table->n
5878 [ - + ]: 208 : || p->offset >= (Dwarf_Off) (endp - *readp + offset))
5879 : : {
5880 : 0 : *readp = endp;
5881 : 0 : printf (_(" [%6tx] <UNUSED GARBAGE IN REST OF SECTION>\n"),
5882 : : offset);
5883 : 0 : return true;
5884 : : }
5885 : :
5886 [ - + ]: 208 : if (p->offset != (Dwarf_Off) offset)
5887 : : {
5888 : 0 : *readp += p->offset - offset;
5889 : 0 : printf (_(" [%6tx] <UNUSED GARBAGE> ... %" PRIu64 " bytes ...\n"),
5890 : : offset, (Dwarf_Off) p->offset - offset);
5891 : 0 : return true;
5892 : : }
5893 : :
5894 [ + - ]: 208 : if (address_sizep != NULL)
5895 [ + + ]: 224 : *address_sizep = listptr_address_size (p);
5896 [ + + ]: 208 : if (offset_sizep != NULL)
5897 [ + - ]: 296 : *offset_sizep = listptr_offset_size (p);
5898 [ + - ]: 208 : if (base != NULL)
5899 : 208 : *base = listptr_base (p);
5900 [ + - ]: 208 : if (cu != NULL)
5901 : 208 : *cu = p->cu;
5902 [ + + ]: 208 : if (attr != NULL)
5903 : 148 : *attr = p->attr;
5904 : :
5905 : : return false;
5906 : : }
5907 : :
5908 : : static Dwarf_Off
5909 : 124410 : next_listptr_offset (struct listptr_table *table, size_t *idxp, Dwarf_Off off)
5910 : : {
5911 : : /* Note that multiple attributes could in theory point to the same loclist
5912 : : offset, so make sure we pick one that is bigger than the current one.
5913 : : The table is sorted on offset. */
5914 [ + - ]: 124410 : if (*idxp < table->n)
5915 : : {
5916 [ + - ]: 247282 : while (++*idxp < table->n)
5917 : : {
5918 : 247282 : Dwarf_Off next = table->table[*idxp].offset;
5919 [ + + ]: 247282 : if (next > off)
5920 : : return next;
5921 : : }
5922 : : }
5923 : : return 0;
5924 : : }
5925 : :
5926 : : /* Returns the listptr associated with the given index, or NULL. */
5927 : : static struct listptr *
5928 : 1733780 : get_listptr (struct listptr_table *table, size_t idx)
5929 : : {
5930 : 1733780 : if (idx >= table->n)
5931 : : return NULL;
5932 : 36 : return &table->table[idx];
5933 : : }
5934 : :
5935 : : /* Returns the next index, base address and CU associated with the
5936 : : list unit offsets. If there is none false is returned, otherwise
5937 : : true. Assumes the table has been sorted. */
5938 : : static bool
5939 : 5850 : listptr_cu (struct listptr_table *table, size_t *idxp,
5940 : : Dwarf_Off start, Dwarf_Off end,
5941 : : Dwarf_Addr *base, struct Dwarf_CU **cu)
5942 : : {
5943 : 5850 : while (*idxp < table->n
5944 [ + - + + ]: 47384 : && table->table[*idxp].offset < start)
5945 : 41534 : ++*idxp;
5946 : :
5947 [ + - ]: 5850 : if (*idxp < table->n
5948 [ + - ]: 5850 : && table->table[*idxp].offset >= start
5949 [ + - ]: 5850 : && table->table[*idxp].offset < end)
5950 : : {
5951 : 5850 : struct listptr *p = &table->table[*idxp];
5952 : 5850 : *base = listptr_base (p);
5953 : 5850 : *cu = p->cu;
5954 : 5850 : return true;
5955 : : }
5956 : :
5957 : : return false;
5958 : : }
5959 : :
5960 : : /* Returns the next index with the current CU for the given attribute.
5961 : : If there is none false is returned, otherwise true. Assumes the
5962 : : table has been sorted. */
5963 : : static bool
5964 : 866652 : listptr_attr (struct listptr_table *table, size_t idxp,
5965 : : Dwarf_Off offset, unsigned int attr)
5966 : : {
5967 : 1733732 : struct listptr *listptr;
5968 : 1733732 : do
5969 : : {
5970 [ + + ]: 1733732 : listptr = get_listptr (table, idxp);
5971 [ + - ]: 1733496 : if (listptr == NULL)
5972 : : return false;
5973 : :
5974 [ + + + + ]: 1733496 : if (listptr->offset == offset && listptr->attr == attr)
5975 : : return true;
5976 : :
5977 : 1609086 : idxp++;
5978 : : }
5979 [ + + ]: 1609086 : while (listptr->offset <= offset);
5980 : :
5981 : : return false;
5982 : : }
5983 : :
5984 : : static void
5985 : 88 : print_debug_abbrev_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
5986 : : Ebl *ebl, GElf_Ehdr *ehdr __attribute__ ((unused)),
5987 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg,
5988 : : FILE *out)
5989 : : {
5990 : 88 : Elf_Data *elf_data = get_debug_elf_data (dbg, ebl, IDX_debug_abbrev, scn);
5991 [ + - ]: 88 : if (elf_data == NULL)
5992 : : return;
5993 : :
5994 : 88 : const size_t sh_size = elf_data->d_size;
5995 : :
5996 : 88 : fprintf (out, _("\nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"
5997 : : " [ Code]\n"),
5998 : : elf_ndxscn (scn), section_name (ebl, shdr),
5999 : 88 : (uint64_t) shdr->sh_offset);
6000 : :
6001 : 88 : Dwarf_Off offset = 0;
6002 [ + + ]: 4186 : while (offset < sh_size)
6003 : : {
6004 : 4098 : fprintf (out, _("\nAbbreviation section at offset %" PRIu64 ":\n"),
6005 : : offset);
6006 : :
6007 : 379402 : while (1)
6008 : 187652 : {
6009 : 191750 : size_t length;
6010 : 191750 : Dwarf_Abbrev abbrev;
6011 : :
6012 : 191750 : int res = dwarf_offabbrev (dbg, offset, &length, &abbrev);
6013 [ + + ]: 191750 : if (res != 0)
6014 : : {
6015 [ - + ]: 4098 : if (unlikely (res < 0))
6016 : : {
6017 : 0 : fprintf (out, _("\
6018 : : *** error while reading abbreviation: %s\n"),
6019 : : dwarf_errmsg (-1));
6020 : 0 : return;
6021 : : }
6022 : :
6023 : : /* This is the NUL byte at the end of the section. */
6024 : 4098 : ++offset;
6025 : 4098 : break;
6026 : : }
6027 : :
6028 : : /* We know these calls can never fail. */
6029 : 187652 : unsigned int code = dwarf_getabbrevcode (&abbrev);
6030 : 187652 : unsigned int tag = dwarf_getabbrevtag (&abbrev);
6031 : 187652 : int has_children = dwarf_abbrevhaschildren (&abbrev);
6032 : :
6033 [ + + ]: 187652 : fprintf (out, " [%5u] %s: %" PRId64
6034 : : ", %s: %s, %s: %s\n",
6035 : : code, offset_str, (int64_t) offset,
6036 : : children_str, has_children ? yes_str : no_str,
6037 : : tag_str, dwarf_tag_name (tag));
6038 : :
6039 : 187652 : size_t cnt = 0;
6040 : 187652 : unsigned int name;
6041 : 187652 : unsigned int form;
6042 : 187652 : Dwarf_Sword data;
6043 : 187652 : Dwarf_Off enoffset;
6044 : 187652 : while (dwarf_getabbrevattr_data (&abbrev, cnt, &name, &form,
6045 [ + + ]: 1076858 : &data, &enoffset) == 0)
6046 : : {
6047 : 889206 : fprintf (out, " attr: %s, form: %s",
6048 : : dwarf_attr_name (name), dwarf_form_name (form));
6049 [ + + ]: 889206 : if (form == DW_FORM_implicit_const)
6050 : 62518 : fprintf (out, " (%" PRId64 ")", data);
6051 : 889206 : fprintf (out, ", offset: %#" PRIx64 "\n", (uint64_t) enoffset);
6052 : 889206 : ++cnt;
6053 : : }
6054 : :
6055 : 187652 : offset += length;
6056 : : }
6057 : : }
6058 : : }
6059 : :
6060 : :
6061 : : static void
6062 : 8 : print_debug_addr_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
6063 : : Ebl *ebl, GElf_Ehdr *ehdr,
6064 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg,
6065 : : FILE *out)
6066 : : {
6067 : 8 : Elf_Data *data = get_debug_elf_data (dbg, ebl, IDX_debug_addr, scn);
6068 [ + - ]: 8 : if (data == NULL)
6069 : : return;
6070 : :
6071 : 8 : fprintf (out, _("\
6072 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
6073 : : elf_ndxscn (scn), section_name (ebl, shdr),
6074 : 8 : (uint64_t) shdr->sh_offset);
6075 : :
6076 [ + - ]: 8 : if (shdr->sh_size == 0)
6077 : : return;
6078 : :
6079 : 8 : size_t idx = 0;
6080 : 8 : sort_listptr (&known_addrbases, "addr_base");
6081 : :
6082 : 8 : const unsigned char *start = (const unsigned char *) data->d_buf;
6083 : 8 : const unsigned char *readp = start;
6084 : 8 : const unsigned char *readendp = ((const unsigned char *) data->d_buf
6085 : 8 : + data->d_size);
6086 : :
6087 [ + + ]: 24 : while (readp < readendp)
6088 : : {
6089 : : /* We cannot really know whether or not there is an header. The
6090 : : DebugFission extension to DWARF4 doesn't add one. The DWARF5
6091 : : .debug_addr variant does. Whether or not we have an header,
6092 : : DW_AT_[GNU_]addr_base points at "index 0". So if the current
6093 : : offset equals the CU addr_base then we can just start
6094 : : printing addresses. If there is no CU with an exact match
6095 : : then we'll try to parse the header first. */
6096 : 16 : Dwarf_Off off = (Dwarf_Off) (readp
6097 : 16 : - (const unsigned char *) data->d_buf);
6098 : :
6099 : 16 : fprintf (out, "Table at offset %" PRIx64 " ", off);
6100 : :
6101 : : /* Find the first CU that could plausibly be associated with
6102 : : this address base offset. Skip CUs that point their addr_base
6103 : : before this table. */
6104 [ + - ]: 16 : struct listptr *listptr = get_listptr (&known_addrbases, idx);
6105 [ + + ]: 24 : while (listptr != NULL && listptr->offset < off)
6106 [ + - + - ]: 32 : listptr = get_listptr (&known_addrbases, ++idx);
6107 : :
6108 : 16 : const unsigned char *next_unitp;
6109 : :
6110 : 16 : uint64_t unit_length;
6111 : 16 : uint16_t version;
6112 : 16 : uint8_t address_size;
6113 : 16 : uint8_t segment_size;
6114 [ - + ]: 16 : if (listptr == NULL)
6115 : : {
6116 : 0 : error (0, 0, "Warning: No CU references .debug_addr after %" PRIx64,
6117 : : off);
6118 : :
6119 : : /* We will have to assume it is just addresses to the end... */
6120 [ # # ]: 0 : address_size = ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 4 : 8;
6121 : 0 : next_unitp = readendp;
6122 : 0 : fprintf (out, "Unknown CU:\n");
6123 : : }
6124 : : else
6125 : : {
6126 : 16 : Dwarf_Die cudie;
6127 [ - + ]: 16 : if (dwarf_cu_die (listptr->cu, &cudie,
6128 : : NULL, NULL, NULL, NULL,
6129 : : NULL, NULL) == NULL)
6130 : 0 : fprintf (out, "Unknown CU (%s):\n", dwarf_errmsg (-1));
6131 : : else
6132 : 16 : fprintf (out, "for CU [%6" PRIx64 "]:\n", dwarf_dieoffset (&cudie));
6133 : :
6134 [ + + ]: 16 : if (listptr->offset == off)
6135 : : {
6136 [ - + ]: 4 : address_size = listptr_address_size (listptr);
6137 : 4 : segment_size = 0;
6138 : 4 : version = 4;
6139 : :
6140 : : /* The addresses start here, but where do they end? */
6141 [ + + ]: 4 : listptr = get_listptr (&known_addrbases, idx + 1);
6142 [ - + ]: 2 : if (listptr == NULL)
6143 : : next_unitp = readendp;
6144 [ + - ]: 2 : else if (listptr->cu->version < 5)
6145 : : {
6146 : 2 : next_unitp = start + listptr->offset;
6147 [ + - - + ]: 2 : if (listptr->offset < off || listptr->offset > data->d_size)
6148 : : {
6149 : 0 : error (0, 0,
6150 : : "Warning: Bad address base for next unit at %"
6151 : : PRIx64, off);
6152 : 0 : next_unitp = readendp;
6153 : : }
6154 : : }
6155 : : else
6156 : : {
6157 : : /* Tricky, we don't have a header for this unit, but
6158 : : there is one for the next. We will have to
6159 : : "guess" how big it is and subtract it from the
6160 : : offset (because that points after the header). */
6161 [ # # ]: 0 : unsigned int offset_size = listptr_offset_size (listptr);
6162 : 0 : Dwarf_Off next_off = (listptr->offset
6163 : 0 : - (offset_size == 4 ? 4 : 12) /* len */
6164 : : - 2 /* version */
6165 : : - 1 /* address size */
6166 : 0 : - 1); /* segment selector size */
6167 : 0 : next_unitp = start + next_off;
6168 [ # # # # ]: 0 : if (next_off < off || next_off > data->d_size)
6169 : : {
6170 : 0 : error (0, 0,
6171 : : "Warning: Couldn't calculate .debug_addr "
6172 : : " unit length at %" PRIx64, off);
6173 : 0 : next_unitp = readendp;
6174 : : }
6175 : : }
6176 : 4 : unit_length = (uint64_t) (next_unitp - readp);
6177 : :
6178 : : /* Pretend we have a header. */
6179 : 4 : fprintf (out, "\n");
6180 : 4 : fprintf (out, _(" Length: %8" PRIu64 "\n"),
6181 : : unit_length);
6182 : 4 : fprintf (out, _(" DWARF version: %8" PRIu16 "\n"), version);
6183 : 4 : fprintf (out, _(" Address size: %8" PRIu64 "\n"),
6184 : : (uint64_t) address_size);
6185 : 4 : fprintf (out, _(" Segment size: %8" PRIu64 "\n"),
6186 : : (uint64_t) segment_size);
6187 : 4 : fprintf (out, "\n");
6188 : : }
6189 : : else
6190 : : {
6191 : : /* OK, we have to parse an header first. */
6192 [ - + ]: 12 : unit_length = read_4ubyte_unaligned_inc (dbg, readp);
6193 [ - + ]: 12 : if (unlikely (unit_length == 0xffffffff))
6194 : : {
6195 [ # # ]: 0 : if (unlikely (readp > readendp - 8))
6196 : : {
6197 : 0 : invalid_data:
6198 : 0 : error (0, 0, "Invalid data");
6199 : 0 : return;
6200 : : }
6201 [ # # ]: 0 : unit_length = read_8ubyte_unaligned_inc (dbg, readp);
6202 : : }
6203 : 12 : fprintf (out, "\n");
6204 : 12 : fprintf (out, _(" Length: %8" PRIu64 "\n"),
6205 : : unit_length);
6206 : :
6207 : : /* We need at least 2-bytes (version) + 1-byte
6208 : : (addr_size) + 1-byte (segment_size) = 4 bytes to
6209 : : complete the header. And this unit cannot go beyond
6210 : : the section data. */
6211 [ + - ]: 12 : if (readp > readendp - 4
6212 [ + - ]: 12 : || unit_length < 4
6213 [ - + ]: 12 : || unit_length > (uint64_t) (readendp - readp))
6214 : 0 : goto invalid_data;
6215 : :
6216 : 12 : next_unitp = readp + unit_length;
6217 : :
6218 [ - + ]: 12 : version = read_2ubyte_unaligned_inc (dbg, readp);
6219 : 12 : fprintf (out, _(" DWARF version: %8" PRIu16 "\n"), version);
6220 : :
6221 [ - + ]: 12 : if (version != 5)
6222 : : {
6223 : 0 : error (0, 0, _("Unknown version"));
6224 : 0 : goto next_unit;
6225 : : }
6226 : :
6227 : 12 : address_size = *readp++;
6228 : 12 : fprintf (out, _(" Address size: %8" PRIu64 "\n"),
6229 : : (uint64_t) address_size);
6230 : :
6231 [ - + ]: 12 : if (address_size != 4 && address_size != 8)
6232 : : {
6233 : 0 : error (0, 0, _("unsupported address size"));
6234 : 0 : goto next_unit;
6235 : : }
6236 : :
6237 : 12 : segment_size = *readp++;
6238 : 12 : fprintf (out, _(" Segment size: %8" PRIu64 "\n"),
6239 : : (uint64_t) segment_size);
6240 : 12 : fprintf (out, "\n");
6241 : :
6242 [ - + ]: 12 : if (segment_size != 0)
6243 : : {
6244 : 0 : error (0, 0, _("unsupported segment size"));
6245 : 0 : goto next_unit;
6246 : : }
6247 : :
6248 [ - + ]: 12 : if (listptr->offset != (Dwarf_Off) (readp - start))
6249 : : {
6250 : 0 : error (0, 0, "Address index doesn't start after header");
6251 : 0 : goto next_unit;
6252 : : }
6253 : : }
6254 : : }
6255 : :
6256 : 16 : int digits = 1;
6257 : 16 : size_t addresses = (next_unitp - readp) / address_size;
6258 [ + + ]: 24 : while (addresses >= 10)
6259 : : {
6260 : 8 : ++digits;
6261 : 8 : addresses /= 10;
6262 : : }
6263 : :
6264 : 16 : unsigned int uidx = 0;
6265 : 16 : size_t index_offset = readp - (const unsigned char *) data->d_buf;
6266 : 16 : fprintf (out, " Addresses start at offset 0x%zx:\n", index_offset);
6267 [ + + ]: 188 : while (readp <= next_unitp - address_size)
6268 : : {
6269 [ - + + + : 172 : Dwarf_Addr addr = read_addr_unaligned_inc (address_size, dbg,
- + - + ]
6270 : : readp);
6271 : 172 : fprintf (out, " [%*u] ", digits, uidx++);
6272 : 172 : print_dwarf_addr (dwflmod, address_size, addr, addr, out);
6273 : 172 : fprintf (out, "\n");
6274 : : }
6275 : 16 : fprintf (out, "\n");
6276 : :
6277 [ + - ]: 16 : if (readp != next_unitp)
6278 : 0 : error (0, 0, "extra %zd bytes at end of unit",
6279 : 0 : (size_t) (next_unitp - readp));
6280 : :
6281 : 16 : next_unit:
6282 : : readp = next_unitp;
6283 : : }
6284 : : }
6285 : :
6286 : : /* Print content of DWARF .debug_aranges section. We fortunately do
6287 : : not have to know a bit about the structure of the section, libdwarf
6288 : : takes care of it. */
6289 : : static void
6290 : 2 : print_decoded_aranges_section (Ebl *ebl, GElf_Ehdr *ehdr, Elf_Scn *scn,
6291 : : GElf_Shdr *shdr, Dwarf *dbg, FILE *out)
6292 : : {
6293 : 2 : Dwarf_Aranges *aranges;
6294 : 2 : size_t cnt;
6295 [ - + ]: 2 : if (unlikely (dwarf_getaranges (dbg, &aranges, &cnt) != 0))
6296 : : {
6297 : 0 : error (0, 0, _("cannot get .debug_aranges content: %s"),
6298 : : dwarf_errmsg (-1));
6299 : 0 : return;
6300 : : }
6301 : :
6302 : 2 : GElf_Shdr glink_mem;
6303 : 2 : GElf_Shdr *glink;
6304 : 2 : glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link), &glink_mem);
6305 [ - + ]: 2 : if (glink == NULL)
6306 : : {
6307 : 0 : error (0, 0, _("invalid sh_link value in section %zu"),
6308 : : elf_ndxscn (scn));
6309 : 0 : return;
6310 : : }
6311 : :
6312 : 2 : fprintf (out, ngettext ("\
6313 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 " contains %zu entry:\n",
6314 : : "\
6315 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 " contains %zu entries:\n",
6316 : : cnt),
6317 : : elf_ndxscn (scn), section_name (ebl, shdr),
6318 : 2 : (uint64_t) shdr->sh_offset, cnt);
6319 : :
6320 : : /* Compute floor(log16(cnt)). */
6321 : 2 : size_t tmp = cnt;
6322 : 2 : int digits = 1;
6323 [ - + ]: 2 : while (tmp >= 16)
6324 : : {
6325 : 0 : ++digits;
6326 : 0 : tmp >>= 4;
6327 : : }
6328 : :
6329 [ + + ]: 12 : for (size_t n = 0; n < cnt; ++n)
6330 : : {
6331 : 10 : Dwarf_Arange *runp = dwarf_onearange (aranges, n);
6332 [ - + ]: 10 : if (unlikely (runp == NULL))
6333 : : {
6334 : 0 : fprintf (out, "cannot get arange %zu: %s\n", n, dwarf_errmsg (-1));
6335 : 0 : return;
6336 : : }
6337 : :
6338 : 10 : Dwarf_Addr start;
6339 : 10 : Dwarf_Word length;
6340 : 10 : Dwarf_Off offset;
6341 : :
6342 [ - + ]: 10 : if (unlikely (dwarf_getarangeinfo (runp, &start, &length, &offset) != 0))
6343 : 0 : fprintf (out, _(" [%*zu] ???\n"), digits, n);
6344 : : else
6345 : 10 : fprintf (out, _(" [%*zu] start: %0#*" PRIx64
6346 : : ", length: %5" PRIu64 ", CU DIE offset: %6"
6347 : : PRId64 "\n"),
6348 [ - + ]: 10 : digits, n, ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 10 : 18,
6349 : : (uint64_t) start, (uint64_t) length, (int64_t) offset);
6350 : : }
6351 : : }
6352 : :
6353 : :
6354 : : /* Print content of DWARF .debug_aranges section. */
6355 : : static void
6356 : 92 : print_debug_aranges_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
6357 : : Ebl *ebl, GElf_Ehdr *ehdr, Elf_Scn *scn,
6358 : : GElf_Shdr *shdr, Dwarf *dbg, FILE *out)
6359 : : {
6360 [ + + ]: 92 : if (decodedaranges)
6361 : : {
6362 : 2 : print_decoded_aranges_section (ebl, ehdr, scn, shdr, dbg, out);
6363 : 2 : return;
6364 : : }
6365 : :
6366 : 90 : Elf_Data *data = get_debug_elf_data (dbg, ebl, IDX_debug_aranges, scn);
6367 [ + - ]: 90 : if (data == NULL)
6368 : : return;
6369 : :
6370 : 90 : fprintf (out, _("\
6371 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
6372 : : elf_ndxscn (scn), section_name (ebl, shdr),
6373 : 90 : (uint64_t) shdr->sh_offset);
6374 : :
6375 : 90 : const unsigned char *readp = data->d_buf;
6376 : 90 : const unsigned char *readendp = readp + data->d_size;
6377 : :
6378 [ + + ]: 4170 : while (readp < readendp)
6379 : : {
6380 : 4080 : const unsigned char *hdrstart = readp;
6381 : 4080 : size_t start_offset = hdrstart - (const unsigned char *) data->d_buf;
6382 : :
6383 : 4080 : fprintf (out, "\n%s %zu:\n", table_offset_lower_str, start_offset);
6384 [ - + ]: 4080 : if (readp + 4 > readendp)
6385 : : {
6386 : 0 : invalid_data:
6387 : 0 : error (0, 0, _("invalid data in section [%zu] '%s'"),
6388 : : elf_ndxscn (scn), section_name (ebl, shdr));
6389 : 0 : return;
6390 : : }
6391 : :
6392 [ + + ]: 4080 : Dwarf_Word length = read_4ubyte_unaligned_inc (dbg, readp);
6393 : 4080 : unsigned int length_bytes = 4;
6394 [ - + ]: 4080 : if (length == DWARF3_LENGTH_64_BIT)
6395 : : {
6396 [ # # ]: 0 : if (readp + 8 > readendp)
6397 : 0 : goto invalid_data;
6398 [ # # ]: 0 : length = read_8ubyte_unaligned_inc (dbg, readp);
6399 : 0 : length_bytes = 8;
6400 : : }
6401 : :
6402 : 4080 : const unsigned char *nexthdr = readp + length;
6403 : 4080 : fprintf (out, "\n %s: %6" PRIu64 "\n",
6404 : : length_str, (uint64_t) length);
6405 : :
6406 [ - + ]: 4080 : if (unlikely (length > (size_t) (readendp - readp)))
6407 : 0 : goto invalid_data;
6408 : :
6409 [ - + ]: 4080 : if (length == 0)
6410 : 0 : continue;
6411 : :
6412 [ - + ]: 4080 : if (readp + 2 > readendp)
6413 : 0 : goto invalid_data;
6414 [ + + ]: 4080 : uint_fast16_t version = read_2ubyte_unaligned_inc (dbg, readp);
6415 : 4080 : fprintf (out, " %s: %6" PRIuFAST16 "\n",
6416 : : dwarf_ver_str, version);
6417 [ - + ]: 4080 : if (version != 2)
6418 : : {
6419 : 0 : error (0, 0, _("unsupported aranges version"));
6420 : 0 : goto next_table;
6421 : : }
6422 : :
6423 : 4080 : Dwarf_Word offset;
6424 [ - + ]: 4080 : if (readp + length_bytes > readendp)
6425 : 0 : goto invalid_data;
6426 [ - + ]: 4080 : if (length_bytes == 8)
6427 [ # # ]: 0 : offset = read_8ubyte_unaligned_inc (dbg, readp);
6428 : : else
6429 [ + + ]: 4080 : offset = read_4ubyte_unaligned_inc (dbg, readp);
6430 : 4080 : fprintf (out, " %s: %6" PRIx64 "\n",
6431 : : cu_offset_str, (uint64_t) offset);
6432 : :
6433 [ - + ]: 4080 : if (readp + 1 > readendp)
6434 : 0 : goto invalid_data;
6435 : 4080 : unsigned int address_size = *readp++;
6436 : 4080 : fprintf (out, " %s: %6" PRIu64 "\n",
6437 : : addr_size_str, (uint64_t) address_size);
6438 [ - + ]: 4080 : if (address_size != 4 && address_size != 8)
6439 : : {
6440 : 0 : error (0, 0, _("unsupported address size"));
6441 : 0 : goto next_table;
6442 : : }
6443 : :
6444 [ - + ]: 4080 : if (readp + 1 > readendp)
6445 : 0 : goto invalid_data;
6446 : 4080 : unsigned int segment_size = *readp++;
6447 : 4080 : fprintf (out, " %s: %6" PRIu64 "\n\n",
6448 : : seg_size_str, (uint64_t) segment_size);
6449 [ - + - - ]: 4080 : if (segment_size != 0 && segment_size != 4 && segment_size != 8)
6450 : : {
6451 : 0 : error (0, 0, _("unsupported segment size"));
6452 : 0 : goto next_table;
6453 : : }
6454 : :
6455 : : /* Round the address to the next multiple of 2*address_size. */
6456 : 4080 : readp += ((2 * address_size - ((readp - hdrstart) % (2 * address_size)))
6457 : 4080 : % (2 * address_size));
6458 : :
6459 [ + - ]: 10468 : while (readp < nexthdr)
6460 : : {
6461 : 10468 : Dwarf_Word range_address;
6462 : 10468 : Dwarf_Word range_length;
6463 : 10468 : Dwarf_Word segment = 0;
6464 [ - + ]: 10468 : if (readp + 2 * address_size + segment_size > readendp)
6465 : 0 : goto invalid_data;
6466 [ + + ]: 10468 : if (address_size == 4)
6467 : : {
6468 [ + + ]: 92 : range_address = read_4ubyte_unaligned_inc (dbg, readp);
6469 [ + + ]: 92 : range_length = read_4ubyte_unaligned_inc (dbg, readp);
6470 : : }
6471 : : else
6472 : : {
6473 [ + + ]: 10376 : range_address = read_8ubyte_unaligned_inc (dbg, readp);
6474 [ + + ]: 10376 : range_length = read_8ubyte_unaligned_inc (dbg, readp);
6475 : : }
6476 : :
6477 [ - + ]: 10468 : if (segment_size == 4)
6478 [ # # ]: 0 : segment = read_4ubyte_unaligned_inc (dbg, readp);
6479 [ - + ]: 10468 : else if (segment_size == 8)
6480 [ # # ]: 0 : segment = read_8ubyte_unaligned_inc (dbg, readp);
6481 : :
6482 [ + + ]: 10468 : if (range_address == 0 && range_length == 0 && segment == 0)
6483 : : break;
6484 : :
6485 : 6388 : fprintf (out, " ");
6486 : 6388 : print_dwarf_addr (dwflmod, address_size, range_address,
6487 : : range_address, out);
6488 : 6388 : fprintf (out, "..");
6489 : 6388 : print_dwarf_addr (dwflmod, address_size,
6490 : 6388 : range_address + range_length - 1,
6491 : : range_length, out);
6492 [ - + ]: 6388 : if (segment_size != 0)
6493 : 0 : fprintf (out, " (%" PRIx64 ")\n", (uint64_t) segment);
6494 : : else
6495 : 6388 : fprintf (out, "\n");
6496 : : }
6497 : :
6498 : 4080 : next_table:
6499 [ - + ]: 4080 : if (readp != nexthdr)
6500 : : {
6501 : 0 : size_t padding = nexthdr - readp;
6502 : 0 : fprintf (out, _(" %zu padding bytes\n"), padding);
6503 : 0 : readp = nexthdr;
6504 : : }
6505 : : }
6506 : : }
6507 : :
6508 : :
6509 : : static bool is_split_dwarf (Dwarf *dbg, uint64_t *id, Dwarf_CU **split_cu);
6510 : :
6511 : : /* Returns true and sets cu and cu_base if the given Dwarf is a split
6512 : : DWARF (.dwo) file. */
6513 : : static bool
6514 : 0 : split_dwarf_cu_base (Dwarf *dbg, Dwarf_CU **cu, Dwarf_Addr *cu_base)
6515 : : {
6516 : 0 : uint64_t id;
6517 [ # # ]: 0 : if (is_split_dwarf (dbg, &id, cu))
6518 : : {
6519 : 0 : Dwarf_Die cudie;
6520 [ # # ]: 0 : if (dwarf_cu_info (*cu, NULL, NULL, &cudie, NULL, NULL, NULL, NULL) == 0)
6521 : : {
6522 : 0 : *cu_base = cudie_base (&cudie);
6523 : 0 : return true;
6524 : : }
6525 : : }
6526 : : return false;
6527 : : }
6528 : :
6529 : : /* Print content of DWARF .debug_rnglists section. */
6530 : : static void
6531 : 38 : print_debug_rnglists_section (Dwfl_Module *dwflmod,
6532 : : Ebl *ebl,
6533 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
6534 : : Elf_Scn *scn, GElf_Shdr *shdr,
6535 : : Dwarf *dbg __attribute__((unused)),
6536 : : FILE *out)
6537 : : {
6538 : 38 : Elf_Data *data = get_debug_elf_data (dbg, ebl, IDX_debug_rnglists, scn);
6539 [ + - ]: 38 : if (data == NULL)
6540 : 0 : return;
6541 : :
6542 : 38 : fprintf (out, _("\
6543 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
6544 : : elf_ndxscn (scn), section_name (ebl, shdr),
6545 : 38 : (uint64_t) shdr->sh_offset);
6546 : :
6547 : : /* For the listptr to get the base address/CU. */
6548 : 38 : sort_listptr (&known_rnglistptr, "rnglistptr");
6549 : 38 : size_t listptr_idx = 0;
6550 : :
6551 : 38 : const unsigned char *readp = data->d_buf;
6552 : 38 : const unsigned char *const dataend = ((unsigned char *) data->d_buf
6553 : 38 : + data->d_size);
6554 [ + + ]: 2358 : while (readp < dataend)
6555 : : {
6556 [ - + ]: 2320 : if (unlikely (readp > dataend - 4))
6557 : : {
6558 : 0 : invalid_data:
6559 : 0 : error (0, 0, _("invalid data in section [%zu] '%s'"),
6560 : : elf_ndxscn (scn), section_name (ebl, shdr));
6561 : 0 : return;
6562 : : }
6563 : :
6564 : 2320 : ptrdiff_t offset = readp - (unsigned char *) data->d_buf;
6565 : 2320 : fprintf (out, "%s 0x%" PRIx64 ":\n\n",
6566 : : table_offset_upper_str, (uint64_t) offset);
6567 : :
6568 [ - + ]: 2320 : uint64_t unit_length = read_4ubyte_unaligned_inc (dbg, readp);
6569 : 2320 : unsigned int offset_size = 4;
6570 [ - + ]: 2320 : if (unlikely (unit_length == 0xffffffff))
6571 : : {
6572 [ # # ]: 0 : if (unlikely (readp > dataend - 8))
6573 : 0 : goto invalid_data;
6574 : :
6575 [ # # ]: 0 : unit_length = read_8ubyte_unaligned_inc (dbg, readp);
6576 : 0 : offset_size = 8;
6577 : : }
6578 : 2320 : fprintf (out, " %s: %8" PRIu64 "\n", length_str, unit_length);
6579 : :
6580 : : /* We need at least 2-bytes + 1-byte + 1-byte + 4-bytes = 8
6581 : : bytes to complete the header. And this unit cannot go beyond
6582 : : the section data. */
6583 [ + - ]: 2320 : if (readp > dataend - 8
6584 [ + - ]: 2320 : || unit_length < 8
6585 [ - + ]: 2320 : || unit_length > (uint64_t) (dataend - readp))
6586 : 0 : goto invalid_data;
6587 : :
6588 : 2320 : const unsigned char *nexthdr = readp + unit_length;
6589 : :
6590 [ - + ]: 2320 : uint16_t version = read_2ubyte_unaligned_inc (dbg, readp);
6591 : 2320 : fprintf (out, " %s: %8" PRIu16 "\n", dwarf_ver_str, version);
6592 : :
6593 [ - + ]: 2320 : if (version != 5)
6594 : : {
6595 : 0 : error (0, 0, _("Unknown version"));
6596 : 0 : goto next_table;
6597 : : }
6598 : :
6599 : 2320 : uint8_t address_size = *readp++;
6600 : 2320 : fprintf (out, " %s: %8" PRIu64 "\n",
6601 : : addr_size_str, (uint64_t) address_size);
6602 : :
6603 [ - + ]: 2320 : if (address_size != 4 && address_size != 8)
6604 : : {
6605 : 0 : error (0, 0, _("unsupported address size"));
6606 : 0 : goto next_table;
6607 : : }
6608 : :
6609 : 2320 : uint8_t segment_size = *readp++;
6610 : 2320 : fprintf (out, _(" Segment size: %8" PRIu64 "\n"),
6611 : : (uint64_t) segment_size);
6612 : :
6613 [ - + - - ]: 2320 : if (segment_size != 0 && segment_size != 4 && segment_size != 8)
6614 : : {
6615 : 0 : error (0, 0, _("unsupported segment size"));
6616 : 0 : goto next_table;
6617 : : }
6618 : :
6619 [ - + ]: 2320 : uint32_t offset_entry_count = read_4ubyte_unaligned_inc (dbg, readp);
6620 : 2320 : fprintf (out, " %s: %8" PRIu64 "\n",
6621 : : offset_ent_str, (uint64_t) offset_entry_count);
6622 : :
6623 : : /* We need the CU that uses this unit to get the initial base address. */
6624 : 2320 : Dwarf_Addr cu_base = 0;
6625 : 2320 : struct Dwarf_CU *cu = NULL;
6626 [ - + ]: 2320 : if (listptr_cu (&known_rnglistptr, &listptr_idx,
6627 : : (Dwarf_Off) offset,
6628 : 2320 : (Dwarf_Off) (nexthdr - (unsigned char *) data->d_buf),
6629 : : &cu_base, &cu)
6630 [ # # ]: 0 : || split_dwarf_cu_base (dbg, &cu, &cu_base))
6631 : 2320 : {
6632 : 2320 : Dwarf_Die cudie;
6633 [ - + ]: 2320 : if (dwarf_cu_die (cu, &cudie,
6634 : : NULL, NULL, NULL, NULL,
6635 : : NULL, NULL) == NULL)
6636 : 0 : fprintf (out, " %s: ", unknown_base_str);
6637 : : else
6638 : 2320 : fprintf (out, " %s [%6" PRIx64 "] %s: ",
6639 : : cu_str, dwarf_dieoffset (&cudie), base_str);
6640 : 2320 : print_dwarf_addr (dwflmod, address_size, cu_base, cu_base, out);
6641 : 2320 : fprintf (out, "\n");
6642 : : }
6643 : : else
6644 : 0 : fprintf (out, _(" Not associated with a CU.\n"));
6645 : :
6646 : 2320 : fprintf (out, "\n");
6647 : :
6648 : 2320 : const unsigned char *offset_array_start = readp;
6649 [ + + ]: 2320 : if (offset_entry_count > 0)
6650 : : {
6651 : 2 : uint64_t max_entries = (unit_length - 8) / offset_size;
6652 [ - + ]: 2 : if (offset_entry_count > max_entries)
6653 : : {
6654 : 0 : error (0, 0,
6655 : 0 : _("too many offset entries for unit length"));
6656 : 0 : offset_entry_count = max_entries;
6657 : : }
6658 : :
6659 : 2 : fprintf (out, _(" Offsets starting at 0x%" PRIx64 ":\n"),
6660 : : (uint64_t) (offset_array_start
6661 : 2 : - (unsigned char *) data->d_buf));
6662 [ + + ]: 6 : for (uint32_t idx = 0; idx < offset_entry_count; idx++)
6663 : : {
6664 : 4 : fprintf (out, " [%6" PRIu32 "] ", idx);
6665 [ + - ]: 4 : if (offset_size == 4)
6666 : : {
6667 [ - + ]: 4 : uint32_t off = read_4ubyte_unaligned_inc (dbg, readp);
6668 : 4 : fprintf (out, "0x%" PRIx32 "\n", off);
6669 : : }
6670 : : else
6671 : : {
6672 [ # # ]: 0 : uint64_t off = read_8ubyte_unaligned_inc (dbg, readp);
6673 : 0 : fprintf (out, "0x%" PRIx64 "\n", off);
6674 : : }
6675 : : }
6676 : 2 : fprintf (out, "\n");
6677 : : }
6678 : :
6679 : 2320 : Dwarf_Addr base = cu_base;
6680 : 2320 : bool start_of_list = true;
6681 [ + + ]: 170486 : while (readp < nexthdr)
6682 : : {
6683 : 168166 : uint8_t kind = *readp++;
6684 : 168166 : uint64_t op1, op2;
6685 : :
6686 : : /* Skip padding. */
6687 [ - + ]: 168166 : if (start_of_list && kind == DW_RLE_end_of_list)
6688 : 0 : continue;
6689 : :
6690 [ + + ]: 168166 : if (start_of_list)
6691 : : {
6692 : 34094 : base = cu_base;
6693 : 34094 : fprintf (out, " Offset: %" PRIx64 ", Index: %" PRIx64 "\n",
6694 : 34094 : (uint64_t) (readp - (unsigned char *) data->d_buf - 1),
6695 : 34094 : (uint64_t) (readp - offset_array_start - 1));
6696 : 34094 : start_of_list = false;
6697 : : }
6698 : :
6699 : 168166 : fprintf (out, " %s", dwarf_range_list_encoding_name (kind));
6700 [ + - - - : 168166 : switch (kind)
+ + - +
- ]
6701 : : {
6702 : 34094 : case DW_RLE_end_of_list:
6703 : 34094 : start_of_list = true;
6704 : 34094 : fprintf (out, "\n\n");
6705 : 34094 : break;
6706 : :
6707 : 0 : case DW_RLE_base_addressx:
6708 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
6709 : : {
6710 : 0 : invalid_range:
6711 : 0 : error (0, 0, _("invalid range list data"));
6712 : 0 : goto next_table;
6713 : : }
6714 : 0 : get_uleb128 (op1, readp, nexthdr);
6715 : 0 : fprintf (out, " %" PRIx64 "\n", op1);
6716 [ # # ]: 0 : if (! print_unresolved_addresses)
6717 : : {
6718 : 0 : Dwarf_Addr addr;
6719 [ # # ]: 0 : if (get_indexed_addr (cu, op1, &addr) != 0)
6720 : 0 : fprintf (out, " ???\n");
6721 : : else
6722 : : {
6723 : 0 : fprintf (out, " ");
6724 : 0 : print_dwarf_addr (dwflmod, address_size, addr,
6725 : : addr, out);
6726 : 0 : fprintf (out, "\n");
6727 : : }
6728 : : }
6729 : : break;
6730 : :
6731 : 0 : case DW_RLE_startx_endx:
6732 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
6733 : 0 : goto invalid_range;
6734 : 0 : get_uleb128 (op1, readp, nexthdr);
6735 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
6736 : 0 : goto invalid_range;
6737 : 0 : get_uleb128 (op2, readp, nexthdr);
6738 : 0 : fprintf (out, " %" PRIx64 ", %" PRIx64 "\n", op1, op2);
6739 [ # # ]: 0 : if (! print_unresolved_addresses)
6740 : : {
6741 : 0 : Dwarf_Addr addr1;
6742 : 0 : Dwarf_Addr addr2;
6743 [ # # ]: 0 : if (get_indexed_addr (cu, op1, &addr1) != 0
6744 [ # # ]: 0 : || get_indexed_addr (cu, op2, &addr2) != 0)
6745 : : {
6746 : 0 : fprintf (out, " ???..\n");
6747 : 0 : fprintf (out, " ???\n");
6748 : : }
6749 : : else
6750 : : {
6751 : 0 : fprintf (out, " ");
6752 : 0 : print_dwarf_addr (dwflmod, address_size,
6753 : : addr1, addr1, out);
6754 : 0 : fprintf (out, "..\n ");
6755 : 0 : print_dwarf_addr (dwflmod, address_size,
6756 : : addr2 - 1, addr2, out);
6757 : 0 : fprintf (out, "\n");
6758 : : }
6759 : : }
6760 : : break;
6761 : :
6762 : 0 : case DW_RLE_startx_length:
6763 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
6764 : 0 : goto invalid_range;
6765 : 0 : get_uleb128 (op1, readp, nexthdr);
6766 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
6767 : 0 : goto invalid_range;
6768 : 0 : get_uleb128 (op2, readp, nexthdr);
6769 : 0 : fprintf (out, " %" PRIx64 ", %" PRIx64 "\n", op1, op2);
6770 [ # # ]: 0 : if (! print_unresolved_addresses)
6771 : : {
6772 : 0 : Dwarf_Addr addr1;
6773 : 0 : Dwarf_Addr addr2;
6774 [ # # ]: 0 : if (get_indexed_addr (cu, op1, &addr1) != 0)
6775 : : {
6776 : 0 : fprintf (out, " ???..\n");
6777 : 0 : fprintf (out, " ???\n");
6778 : : }
6779 : : else
6780 : : {
6781 : 0 : addr2 = addr1 + op2;
6782 : 0 : fprintf (out, " ");
6783 : 0 : print_dwarf_addr (dwflmod, address_size, addr1, addr1, out);
6784 : 0 : fprintf (out, "..\n ");
6785 : 0 : print_dwarf_addr (dwflmod, address_size,
6786 : : addr2 - 1, addr2, out);
6787 : 0 : fprintf (out, "\n");
6788 : : }
6789 : : }
6790 : : break;
6791 : :
6792 : 106606 : case DW_RLE_offset_pair:
6793 [ - + ]: 106606 : if ((uint64_t) (nexthdr - readp) < 1)
6794 : 0 : goto invalid_range;
6795 : 106606 : get_uleb128 (op1, readp, nexthdr);
6796 [ - + ]: 106606 : if ((uint64_t) (nexthdr - readp) < 1)
6797 : 0 : goto invalid_range;
6798 : 106606 : get_uleb128 (op2, readp, nexthdr);
6799 : 106606 : fprintf (out, " %" PRIx64 ", %" PRIx64 "\n", op1, op2);
6800 [ + - ]: 106606 : if (! print_unresolved_addresses)
6801 : : {
6802 : 106606 : op1 += base;
6803 : 106606 : op2 += base;
6804 : 106606 : fprintf (out, " ");
6805 : 106606 : print_dwarf_addr (dwflmod, address_size, op1, op1, out);
6806 : 106606 : fprintf (out, "..\n ");
6807 : 106606 : print_dwarf_addr (dwflmod, address_size, op2 - 1, op2, out);
6808 : 106606 : fprintf (out, "\n");
6809 : : }
6810 : : break;
6811 : :
6812 : 19028 : case DW_RLE_base_address:
6813 [ - + ]: 19028 : if (address_size == 4)
6814 : : {
6815 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 4)
6816 : 0 : goto invalid_range;
6817 [ # # ]: 0 : op1 = read_4ubyte_unaligned_inc (dbg, readp);
6818 : : }
6819 : : else
6820 : : {
6821 [ - + ]: 19028 : if ((uint64_t) (nexthdr - readp) < 8)
6822 : 0 : goto invalid_range;
6823 [ - + ]: 19028 : op1 = read_8ubyte_unaligned_inc (dbg, readp);
6824 : : }
6825 : 19028 : base = op1;
6826 : 19028 : fprintf (out, " 0x%" PRIx64 "\n", base);
6827 [ + - ]: 19028 : if (! print_unresolved_addresses)
6828 : : {
6829 : 19028 : fprintf (out, " ");
6830 : 19028 : print_dwarf_addr (dwflmod, address_size, base, base, out);
6831 : 19028 : fprintf (out, "\n");
6832 : : }
6833 : : break;
6834 : :
6835 : 0 : case DW_RLE_start_end:
6836 [ # # ]: 0 : if (address_size == 4)
6837 : : {
6838 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 8)
6839 : 0 : goto invalid_range;
6840 [ # # ]: 0 : op1 = read_4ubyte_unaligned_inc (dbg, readp);
6841 [ # # ]: 0 : op2 = read_4ubyte_unaligned_inc (dbg, readp);
6842 : : }
6843 : : else
6844 : : {
6845 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 16)
6846 : 0 : goto invalid_range;
6847 [ # # ]: 0 : op1 = read_8ubyte_unaligned_inc (dbg, readp);
6848 [ # # ]: 0 : op2 = read_8ubyte_unaligned_inc (dbg, readp);
6849 : : }
6850 : 0 : fprintf (out, " 0x%" PRIx64 "..0x%" PRIx64 "\n", op1, op2);
6851 [ # # ]: 0 : if (! print_unresolved_addresses)
6852 : : {
6853 : 0 : fprintf (out, " ");
6854 : 0 : print_dwarf_addr (dwflmod, address_size, op1, op1, out);
6855 : 0 : fprintf (out, "..\n ");
6856 : 0 : print_dwarf_addr (dwflmod, address_size, op2 - 1, op2, out);
6857 : 0 : fprintf (out, "\n");
6858 : : }
6859 : : break;
6860 : :
6861 : 8438 : case DW_RLE_start_length:
6862 [ - + ]: 8438 : if (address_size == 4)
6863 : : {
6864 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 4)
6865 : 0 : goto invalid_range;
6866 [ # # ]: 0 : op1 = read_4ubyte_unaligned_inc (dbg, readp);
6867 : : }
6868 : : else
6869 : : {
6870 [ - + ]: 8438 : if ((uint64_t) (nexthdr - readp) < 8)
6871 : 0 : goto invalid_range;
6872 [ - + ]: 8438 : op1 = read_8ubyte_unaligned_inc (dbg, readp);
6873 : : }
6874 [ - + ]: 8438 : if ((uint64_t) (nexthdr - readp) < 1)
6875 : 0 : goto invalid_range;
6876 : 8438 : get_uleb128 (op2, readp, nexthdr);
6877 : 8438 : fprintf (out, " 0x%" PRIx64 ", %" PRIx64 "\n", op1, op2);
6878 [ + + ]: 8438 : if (! print_unresolved_addresses)
6879 : : {
6880 : 8436 : op2 = op1 + op2;
6881 : 8436 : fprintf (out, " ");
6882 : 8436 : print_dwarf_addr (dwflmod, address_size, op1, op1, out);
6883 : 8436 : fprintf (out, "..\n ");
6884 : 8436 : print_dwarf_addr (dwflmod, address_size, op2 - 1, op2, out);
6885 : 8436 : fprintf (out, "\n");
6886 : : }
6887 : : break;
6888 : :
6889 : 0 : default:
6890 : 0 : goto invalid_range;
6891 : : }
6892 : : }
6893 : :
6894 : 2320 : next_table:
6895 [ - + ]: 2320 : if (readp != nexthdr)
6896 : : {
6897 : 0 : size_t padding = nexthdr - readp;
6898 : 0 : fprintf (out, _(" %zu padding bytes\n\n"), padding);
6899 : 0 : readp = nexthdr;
6900 : : }
6901 : : }
6902 : : }
6903 : :
6904 : : /* Print content of DWARF .debug_ranges section. */
6905 : : static void
6906 : 32 : print_debug_ranges_section (Dwfl_Module *dwflmod,
6907 : : Ebl *ebl, GElf_Ehdr *ehdr,
6908 : : Elf_Scn *scn, GElf_Shdr *shdr,
6909 : : Dwarf *dbg, FILE *out)
6910 : : {
6911 : 32 : Elf_Data *data = get_debug_elf_data (dbg, ebl, IDX_debug_ranges, scn);
6912 [ - + ]: 32 : if (data == NULL)
6913 : 0 : return;
6914 : :
6915 : 32 : fprintf (out, _("\
6916 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
6917 : : elf_ndxscn (scn), section_name (ebl, shdr),
6918 : 32 : (uint64_t) shdr->sh_offset);
6919 : :
6920 : 32 : sort_listptr (&known_rangelistptr, "rangelistptr");
6921 : 32 : size_t listptr_idx = 0;
6922 : :
6923 [ + + ]: 32 : uint_fast8_t address_size = ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 4 : 8;
6924 : :
6925 : 32 : bool first = true;
6926 : 32 : Dwarf_Addr base = 0;
6927 : 32 : unsigned char *const endp = (unsigned char *) data->d_buf + data->d_size;
6928 : 32 : unsigned char *readp = data->d_buf;
6929 : 32 : Dwarf_CU *last_cu = NULL;
6930 [ + + ]: 184 : while (readp < endp)
6931 : : {
6932 : 152 : ptrdiff_t offset = readp - (unsigned char *) data->d_buf;
6933 : 152 : Dwarf_CU *cu = last_cu;
6934 : :
6935 [ + + - + ]: 152 : if (first && skip_listptr_hole (&known_rangelistptr, &listptr_idx,
6936 : : &address_size, NULL, &base, &cu,
6937 : : offset, &readp, endp, NULL))
6938 : 0 : continue;
6939 : :
6940 [ + + ]: 152 : if (last_cu != cu)
6941 : : {
6942 : 58 : Dwarf_Die cudie;
6943 [ - + ]: 58 : if (dwarf_cu_die (cu, &cudie,
6944 : : NULL, NULL, NULL, NULL,
6945 : : NULL, NULL) == NULL)
6946 : 0 : fprintf (out, _("\n Unknown CU base: "));
6947 : : else
6948 : 58 : fprintf (out, _("\n CU [%6" PRIx64 "] base: "),
6949 : : dwarf_dieoffset (&cudie));
6950 : 58 : print_dwarf_addr (dwflmod, address_size, base, base, out);
6951 : 58 : fprintf (out, "\n");
6952 : : }
6953 : 152 : last_cu = cu;
6954 : :
6955 [ - + ]: 152 : if (unlikely (data->d_size - offset < (size_t) address_size * 2))
6956 : : {
6957 : 0 : fprintf (out, _(" [%6tx] <INVALID DATA>\n"), offset);
6958 : 0 : break;
6959 : : }
6960 : :
6961 : 152 : Dwarf_Addr begin;
6962 : 152 : Dwarf_Addr end;
6963 [ + + ]: 152 : if (address_size == 8)
6964 : : {
6965 [ - + ]: 120 : begin = read_8ubyte_unaligned_inc (dbg, readp);
6966 [ - + ]: 120 : end = read_8ubyte_unaligned_inc (dbg, readp);
6967 : : }
6968 : : else
6969 : : {
6970 [ - + ]: 32 : begin = read_4ubyte_unaligned_inc (dbg, readp);
6971 [ - + ]: 32 : end = read_4ubyte_unaligned_inc (dbg, readp);
6972 [ + - ]: 32 : if (begin == (Dwarf_Addr) (uint32_t) -1)
6973 : : begin = (Dwarf_Addr) -1l;
6974 : : }
6975 : :
6976 [ - + ]: 152 : if (begin == (Dwarf_Addr) -1l) /* Base address entry. */
6977 : : {
6978 [ # # ]: 0 : if (first)
6979 : 0 : fprintf (out, " [%6tx] ", offset);
6980 : : else
6981 : 0 : fprintf (out, " ");
6982 : 0 : fputs (_("base address\n"), out);
6983 : 0 : fprintf (out, " ");
6984 : 0 : print_dwarf_addr (dwflmod, address_size, end, end, out);
6985 : 0 : fprintf (out, "\n");
6986 : 0 : base = end;
6987 : 0 : first = false;
6988 : : }
6989 [ + + ]: 152 : else if (begin == 0 && end == 0) /* End of list entry. */
6990 : : {
6991 [ - + ]: 60 : if (first)
6992 : 0 : fprintf (out, _(" [%6tx] empty list\n"), offset);
6993 : : first = true;
6994 : : }
6995 : : else
6996 : : {
6997 : : /* We have an address range entry. */
6998 [ + + ]: 92 : if (first) /* First address range entry in a list. */
6999 : 60 : fprintf (out, " [%6tx] ", offset);
7000 : : else
7001 : 32 : fprintf (out, " ");
7002 : :
7003 : 92 : fprintf (out, "range %" PRIx64 ", %" PRIx64 "\n", begin, end);
7004 [ + + ]: 92 : if (! print_unresolved_addresses)
7005 : : {
7006 : 72 : fprintf (out, " ");
7007 : 72 : print_dwarf_addr (dwflmod, address_size, base + begin,
7008 : : base + begin, out);
7009 : 72 : fprintf (out, "..\n ");
7010 : 72 : print_dwarf_addr (dwflmod, address_size,
7011 : : base + end - 1, base + end, out);
7012 : 72 : fprintf (out, "\n");
7013 : : }
7014 : :
7015 : : first = false;
7016 : : }
7017 : : }
7018 : : }
7019 : :
7020 : : #define REGNAMESZ 16
7021 : : static const char *
7022 : 43826 : register_info (Ebl *ebl, unsigned int regno, const Ebl_Register_Location *loc,
7023 : : char name[REGNAMESZ], int *bits, int *type)
7024 : : {
7025 : 43826 : const char *set;
7026 : 43826 : const char *pfx;
7027 : 43826 : int ignore;
7028 [ + + + + ]: 128086 : ssize_t n = ebl_register_info (ebl, regno, name, REGNAMESZ, &pfx, &set,
7029 : : bits ?: &ignore, type ?: &ignore);
7030 [ - + ]: 43826 : if (n <= 0)
7031 : : {
7032 [ # # ]: 0 : if (loc != NULL)
7033 : 0 : snprintf (name, REGNAMESZ, "reg%u", loc->regno);
7034 : : else
7035 : 0 : snprintf (name, REGNAMESZ, "??? 0x%x", regno);
7036 [ # # ]: 0 : if (bits != NULL)
7037 [ # # ]: 0 : *bits = loc != NULL ? loc->bits : 0;
7038 [ # # ]: 0 : if (type != NULL)
7039 : 0 : *type = DW_ATE_unsigned;
7040 : 0 : set = "??? unrecognized";
7041 : : }
7042 : : else
7043 : : {
7044 [ + + - + ]: 43826 : if (bits != NULL && *bits <= 0)
7045 [ # # ]: 0 : *bits = loc != NULL ? loc->bits : 0;
7046 [ + + - + ]: 43826 : if (type != NULL && *type == DW_ATE_void)
7047 : 0 : *type = DW_ATE_unsigned;
7048 : :
7049 : : }
7050 : 43826 : return set;
7051 : : }
7052 : :
7053 : : static const unsigned char *
7054 : 280 : read_encoded (unsigned int encoding, const unsigned char *readp,
7055 : : const unsigned char *const endp, uint64_t *res, Dwarf *dbg)
7056 : : {
7057 [ - + ]: 280 : if ((encoding & 0xf) == DW_EH_PE_absptr)
7058 : 0 : encoding = gelf_getclass (dbg->elf) == ELFCLASS32
7059 [ # # ]: 0 : ? DW_EH_PE_udata4 : DW_EH_PE_udata8;
7060 : :
7061 [ - - - + : 280 : switch (encoding & 0xf)
- - + -
- ]
7062 : : {
7063 : 0 : case DW_EH_PE_uleb128:
7064 [ # # ]: 0 : if (readp >= endp)
7065 : 0 : goto invalid;
7066 : 0 : get_uleb128 (*res, readp, endp);
7067 : 0 : break;
7068 : 0 : case DW_EH_PE_sleb128:
7069 [ # # ]: 0 : if (readp >= endp)
7070 : 0 : goto invalid;
7071 : 0 : get_sleb128 (*res, readp, endp);
7072 : 0 : break;
7073 : 0 : case DW_EH_PE_udata2:
7074 [ # # ]: 0 : if (readp + 2 > endp)
7075 : 0 : goto invalid;
7076 [ # # ]: 0 : *res = read_2ubyte_unaligned_inc (dbg, readp);
7077 : 0 : break;
7078 : 140 : case DW_EH_PE_udata4:
7079 [ - + ]: 140 : if (readp + 4 > endp)
7080 : 0 : goto invalid;
7081 [ - + ]: 140 : *res = read_4ubyte_unaligned_inc (dbg, readp);
7082 : 140 : break;
7083 : 0 : case DW_EH_PE_udata8:
7084 [ # # ]: 0 : if (readp + 8 > endp)
7085 : 0 : goto invalid;
7086 [ # # ]: 0 : *res = read_8ubyte_unaligned_inc (dbg, readp);
7087 : 0 : break;
7088 : 0 : case DW_EH_PE_sdata2:
7089 [ # # ]: 0 : if (readp + 2 > endp)
7090 : 0 : goto invalid;
7091 [ # # ]: 0 : *res = read_2sbyte_unaligned_inc (dbg, readp);
7092 : 0 : break;
7093 : 140 : case DW_EH_PE_sdata4:
7094 [ - + ]: 140 : if (readp + 4 > endp)
7095 : 0 : goto invalid;
7096 [ - + ]: 140 : *res = read_4sbyte_unaligned_inc (dbg, readp);
7097 : 140 : break;
7098 : 0 : case DW_EH_PE_sdata8:
7099 [ # # ]: 0 : if (readp + 8 > endp)
7100 : 0 : goto invalid;
7101 [ # # ]: 0 : *res = read_8sbyte_unaligned_inc (dbg, readp);
7102 : 0 : break;
7103 : : default:
7104 : 0 : invalid:
7105 : 0 : error (1, 0,
7106 : 0 : _("invalid encoding"));
7107 : : }
7108 : :
7109 : 280 : return readp;
7110 : : }
7111 : :
7112 : : static const char *
7113 : 42130 : regname (Ebl *ebl, unsigned int regno, char *regnamebuf)
7114 : : {
7115 : 42130 : register_info (ebl, regno, NULL, regnamebuf, NULL, NULL);
7116 : :
7117 : 42130 : return regnamebuf;
7118 : : }
7119 : :
7120 : : static void
7121 : 27288 : print_cfa_program (const unsigned char *readp, const unsigned char *const endp,
7122 : : Dwarf_Word vma_base, unsigned int code_align,
7123 : : int data_align,
7124 : : unsigned int version, unsigned int ptr_size,
7125 : : unsigned int encoding,
7126 : : Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr, Dwarf *dbg,
7127 : : FILE *out)
7128 : : {
7129 : 27288 : char regnamebuf[REGNAMESZ];
7130 : :
7131 : 27288 : fputs ("\n Program:\n", out);
7132 : 27288 : Dwarf_Word pc = vma_base;
7133 [ + + ]: 397676 : while (readp < endp)
7134 : : {
7135 : 370388 : unsigned int opcode = *readp++;
7136 : :
7137 [ + + ]: 370388 : if (opcode < DW_CFA_advance_loc)
7138 : : /* Extended opcode. */
7139 [ + - + + : 221030 : switch (opcode)
+ + - + -
- + + + +
+ + - + -
- - - - -
+ - - ]
7140 : : {
7141 : 76220 : uint64_t op1;
7142 : 76220 : int64_t sop1;
7143 : 76220 : uint64_t op2;
7144 : 76220 : int64_t sop2;
7145 : :
7146 : 76220 : case DW_CFA_nop:
7147 : 76220 : fputs (" nop\n", out);
7148 : 297250 : break;
7149 : 0 : case DW_CFA_set_loc:
7150 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
7151 : 0 : goto invalid;
7152 : 0 : readp = read_encoded (encoding, readp, endp, &op1, dbg);
7153 : 0 : fprintf (out, " set_loc %#" PRIx64 " to %#" PRIx64 "\n",
7154 : 0 : op1, pc = vma_base + op1);
7155 : 0 : break;
7156 : 7622 : case DW_CFA_advance_loc1:
7157 [ - + ]: 7622 : if ((uint64_t) (endp - readp) < 1)
7158 : 0 : goto invalid;
7159 : 15244 : fprintf (out, " advance_loc1 %u to %#" PRIx64 "\n",
7160 : 7622 : *readp, pc += *readp * code_align);
7161 : 7622 : ++readp;
7162 : 7622 : break;
7163 : 3198 : case DW_CFA_advance_loc2:
7164 [ - + ]: 3198 : if ((uint64_t) (endp - readp) < 2)
7165 : 0 : goto invalid;
7166 [ - + ]: 3198 : op1 = read_2ubyte_unaligned_inc (dbg, readp);
7167 : 3198 : fprintf (out, " advance_loc2 %" PRIu64 " to %#" PRIx64 "\n",
7168 : 3198 : op1, pc += op1 * code_align);
7169 : 3198 : break;
7170 : 36 : case DW_CFA_advance_loc4:
7171 [ - + ]: 36 : if ((uint64_t) (endp - readp) < 4)
7172 : 0 : goto invalid;
7173 [ + - ]: 36 : op1 = read_4ubyte_unaligned_inc (dbg, readp);
7174 : 36 : fprintf (out, " advance_loc4 %" PRIu64 " to %#" PRIx64 "\n",
7175 : 36 : op1, pc += op1 * code_align);
7176 : 36 : break;
7177 : 8 : case DW_CFA_offset_extended:
7178 [ - + ]: 8 : if ((uint64_t) (endp - readp) < 1)
7179 : 0 : goto invalid;
7180 : 8 : get_uleb128 (op1, readp, endp);
7181 [ - + ]: 8 : if ((uint64_t) (endp - readp) < 1)
7182 : 0 : goto invalid;
7183 : 8 : get_uleb128 (op2, readp, endp);
7184 : 8 : fprintf (out, " offset_extended r%" PRIu64 " (%s) at cfa%+" PRId64
7185 : : "\n",
7186 : : op1, regname (ebl, op1, regnamebuf), op2 * data_align);
7187 : 8 : break;
7188 : 0 : case DW_CFA_restore_extended:
7189 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
7190 : 0 : goto invalid;
7191 : 0 : get_uleb128 (op1, readp, endp);
7192 : 0 : fprintf (out, " restore_extended r%" PRIu64 " (%s)\n",
7193 : : op1, regname (ebl, op1, regnamebuf));
7194 : 0 : break;
7195 : 110 : case DW_CFA_undefined:
7196 [ - + ]: 110 : if ((uint64_t) (endp - readp) < 1)
7197 : 0 : goto invalid;
7198 : 110 : get_uleb128 (op1, readp, endp);
7199 : 110 : fprintf (out, " undefined r%" PRIu64 " (%s)\n", op1,
7200 : : regname (ebl, op1, regnamebuf));
7201 : 110 : break;
7202 : 0 : case DW_CFA_same_value:
7203 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
7204 : 0 : goto invalid;
7205 : 0 : get_uleb128 (op1, readp, endp);
7206 : 0 : fprintf (out, " same_value r%" PRIu64 " (%s)\n", op1,
7207 : : regname (ebl, op1, regnamebuf));
7208 : 0 : break;
7209 : 0 : case DW_CFA_register:
7210 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
7211 : 0 : goto invalid;
7212 : 0 : get_uleb128 (op1, readp, endp);
7213 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
7214 : 0 : goto invalid;
7215 : 0 : get_uleb128 (op2, readp, endp);
7216 : 0 : fprintf (out,
7217 : : " register r%" PRIu64 " (%s) in r%" PRIu64 " (%s)\n",
7218 : : op1, regname (ebl, op1, regnamebuf), op2,
7219 : : regname (ebl, op2, regnamebuf));
7220 : 0 : break;
7221 : 13328 : case DW_CFA_remember_state:
7222 : 13328 : fputs (" remember_state\n", out);
7223 : 13328 : break;
7224 : 13328 : case DW_CFA_restore_state:
7225 : 13328 : fputs (" restore_state\n", out);
7226 : 13328 : break;
7227 : 418 : case DW_CFA_def_cfa:
7228 [ - + ]: 418 : if ((uint64_t) (endp - readp) < 1)
7229 : 0 : goto invalid;
7230 : 418 : get_uleb128 (op1, readp, endp);
7231 [ - + ]: 418 : if ((uint64_t) (endp - readp) < 1)
7232 : 0 : goto invalid;
7233 : 418 : get_uleb128 (op2, readp, endp);
7234 : 418 : fprintf (out,
7235 : : " def_cfa r%" PRIu64 " (%s) at offset %" PRIu64 "\n",
7236 : : op1, regname (ebl, op1, regnamebuf), op2);
7237 : 418 : break;
7238 : 126 : case DW_CFA_def_cfa_register:
7239 [ - + ]: 126 : if ((uint64_t) (endp - readp) < 1)
7240 : 0 : goto invalid;
7241 : 126 : get_uleb128 (op1, readp, endp);
7242 : 126 : fprintf (out, " def_cfa_register r%" PRIu64 " (%s)\n",
7243 : : op1, regname (ebl, op1, regnamebuf));
7244 : 126 : break;
7245 : 106486 : case DW_CFA_def_cfa_offset:
7246 [ - + ]: 106486 : if ((uint64_t) (endp - readp) < 1)
7247 : 0 : goto invalid;
7248 : 106486 : get_uleb128 (op1, readp, endp);
7249 : 106486 : fprintf (out, " def_cfa_offset %" PRIu64 "\n", op1);
7250 : 106486 : break;
7251 : 118 : case DW_CFA_def_cfa_expression:
7252 [ - + ]: 118 : if ((uint64_t) (endp - readp) < 1)
7253 : 0 : goto invalid;
7254 : 118 : get_uleb128 (op1, readp, endp); /* Length of DW_FORM_block. */
7255 : 118 : fprintf (out, " def_cfa_expression %" PRIu64 "\n", op1);
7256 [ - + ]: 118 : if ((uint64_t) (endp - readp) < op1)
7257 : : {
7258 : 0 : invalid:
7259 : 0 : fputs (_(" <INVALID DATA>\n"), out);
7260 : 0 : return;
7261 : : }
7262 : 118 : print_ops (dwflmod, dbg, 10, 10, version, ptr_size, 0, NULL,
7263 : : op1, readp, out);
7264 : 118 : readp += op1;
7265 : 118 : break;
7266 : 0 : case DW_CFA_expression:
7267 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
7268 : 0 : goto invalid;
7269 : 0 : get_uleb128 (op1, readp, endp);
7270 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
7271 : 0 : goto invalid;
7272 : 0 : get_uleb128 (op2, readp, endp); /* Length of DW_FORM_block. */
7273 : 0 : fprintf (out, " expression r%" PRIu64 " (%s) \n",
7274 : : op1, regname (ebl, op1, regnamebuf));
7275 [ # # ]: 0 : if ((uint64_t) (endp - readp) < op2)
7276 : 0 : goto invalid;
7277 : 0 : print_ops (dwflmod, dbg, 10, 10, version, ptr_size, 0, NULL,
7278 : : op2, readp, out);
7279 : 0 : readp += op2;
7280 : 0 : break;
7281 : 24 : case DW_CFA_offset_extended_sf:
7282 [ - + ]: 24 : if ((uint64_t) (endp - readp) < 1)
7283 : 0 : goto invalid;
7284 : 24 : get_uleb128 (op1, readp, endp);
7285 [ - + ]: 24 : if ((uint64_t) (endp - readp) < 1)
7286 : 0 : goto invalid;
7287 : 24 : get_sleb128 (sop2, readp, endp);
7288 : 24 : fprintf (out, " offset_extended_sf r%" PRIu64 " (%s) at cfa%+"
7289 : : PRId64 "\n",
7290 : : op1, regname (ebl, op1, regnamebuf), sop2 * data_align);
7291 : 24 : break;
7292 : 0 : case DW_CFA_def_cfa_sf:
7293 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
7294 : 0 : goto invalid;
7295 : 0 : get_uleb128 (op1, readp, endp);
7296 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
7297 : 0 : goto invalid;
7298 : 0 : get_sleb128 (sop2, readp, endp);
7299 : 0 : fprintf (out,
7300 : : " def_cfa_sf r%" PRIu64 " (%s) at offset %" PRId64 "\n",
7301 : : op1, regname (ebl, op1, regnamebuf), sop2 * data_align);
7302 : 0 : break;
7303 : 0 : case DW_CFA_def_cfa_offset_sf:
7304 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
7305 : 0 : goto invalid;
7306 : 0 : get_sleb128 (sop1, readp, endp);
7307 : 0 : fprintf (out,
7308 : : " def_cfa_offset_sf %" PRId64 "\n", sop1 * data_align);
7309 : 0 : break;
7310 : 0 : case DW_CFA_val_offset:
7311 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
7312 : 0 : goto invalid;
7313 : 0 : get_uleb128 (op1, readp, endp);
7314 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
7315 : 0 : goto invalid;
7316 : 0 : get_uleb128 (op2, readp, endp);
7317 : 0 : fprintf (out, " val_offset %" PRIu64 " at offset %" PRIu64 "\n",
7318 : : op1, op2 * data_align);
7319 : 0 : break;
7320 : 0 : case DW_CFA_val_offset_sf:
7321 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
7322 : 0 : goto invalid;
7323 : 0 : get_uleb128 (op1, readp, endp);
7324 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
7325 : 0 : goto invalid;
7326 : 0 : get_sleb128 (sop2, readp, endp);
7327 : 0 : fprintf (out,
7328 : : " val_offset_sf %" PRIu64 " at offset %" PRId64 "\n",
7329 : : op1, sop2 * data_align);
7330 : 0 : break;
7331 : 0 : case DW_CFA_val_expression:
7332 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
7333 : 0 : goto invalid;
7334 : 0 : get_uleb128 (op1, readp, endp);
7335 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
7336 : 0 : goto invalid;
7337 : 0 : get_uleb128 (op2, readp, endp); /* Length of DW_FORM_block. */
7338 : 0 : fprintf (out, " val_expression r%" PRIu64 " (%s)\n",
7339 : : op1, regname (ebl, op1, regnamebuf));
7340 [ # # ]: 0 : if ((uint64_t) (endp - readp) < op2)
7341 : 0 : goto invalid;
7342 : 0 : print_ops (dwflmod, dbg, 10, 10, version, ptr_size, 0,
7343 : : NULL, op2, readp, out);
7344 : 0 : readp += op2;
7345 : 0 : break;
7346 : 0 : case DW_CFA_MIPS_advance_loc8:
7347 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 8)
7348 : 0 : goto invalid;
7349 [ # # ]: 0 : op1 = read_8ubyte_unaligned_inc (dbg, readp);
7350 : 0 : fprintf (out,
7351 : : " MIPS_advance_loc8 %" PRIu64 " to %#" PRIx64 "\n",
7352 : 0 : op1, pc += op1 * code_align);
7353 : 0 : break;
7354 : 8 : case DW_CFA_GNU_window_save: /* DW_CFA_AARCH64_negate_ra_state */
7355 [ + - ]: 8 : if (ehdr->e_machine == EM_AARCH64)
7356 : 8 : fputs (" AARCH64_negate_ra_state\n", out);
7357 : : else
7358 : 0 : fputs (" GNU_window_save\n", out);
7359 : : break;
7360 : 0 : case DW_CFA_GNU_args_size:
7361 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
7362 : 0 : goto invalid;
7363 : 0 : get_uleb128 (op1, readp, endp);
7364 : 0 : fprintf (out, " args_size %" PRIu64 "\n", op1);
7365 : 0 : break;
7366 : : default:
7367 : 0 : fprintf (out, " ??? (%u)\n", opcode);
7368 : 0 : break;
7369 : : }
7370 [ + + ]: 149358 : else if (opcode < DW_CFA_offset)
7371 : 107914 : fprintf (out, " advance_loc %u to %#" PRIx64 "\n",
7372 : 107914 : opcode & 0x3f, pc += (opcode & 0x3f) * code_align);
7373 [ + + ]: 41444 : else if (opcode < DW_CFA_restore)
7374 : : {
7375 : 37622 : uint64_t offset;
7376 [ - + ]: 37622 : if ((uint64_t) (endp - readp) < 1)
7377 : 0 : goto invalid;
7378 : 37622 : get_uleb128 (offset, readp, endp);
7379 : 37622 : fprintf (out, " offset r%u (%s) at cfa%+" PRId64 "\n",
7380 : : opcode & 0x3f, regname (ebl, opcode & 0x3f, regnamebuf),
7381 : : offset * data_align);
7382 : : }
7383 : : else
7384 : 3822 : fprintf (out, " restore r%u (%s)\n",
7385 : : opcode & 0x3f, regname (ebl, opcode & 0x3f, regnamebuf));
7386 : : }
7387 : : }
7388 : :
7389 : :
7390 : : static unsigned int
7391 : 26978 : encoded_ptr_size (int encoding, unsigned int ptr_size)
7392 : : {
7393 [ - + - + ]: 26978 : switch (encoding & 7)
7394 : : {
7395 : : case DW_EH_PE_udata4:
7396 : : return 4;
7397 : 0 : case DW_EH_PE_udata8:
7398 : 0 : return 8;
7399 : : case 0:
7400 : : return ptr_size;
7401 : : }
7402 : :
7403 : 0 : fprintf (stderr, "Unsupported pointer encoding: %#x, "
7404 : : "assuming pointer size of %d.\n", encoding, ptr_size);
7405 : 0 : return ptr_size;
7406 : : }
7407 : :
7408 : :
7409 : : static unsigned int
7410 : 642 : print_encoding (unsigned int val, FILE *out)
7411 : : {
7412 [ - - - + : 642 : switch (val & 0xf)
- - - + -
- ]
7413 : : {
7414 : 0 : case DW_EH_PE_absptr:
7415 : 0 : fputs ("absptr", out);
7416 : 0 : break;
7417 : 0 : case DW_EH_PE_uleb128:
7418 : 0 : fputs ("uleb128", out);
7419 : 0 : break;
7420 : 0 : case DW_EH_PE_udata2:
7421 : 0 : fputs ("udata2", out);
7422 : 0 : break;
7423 : 140 : case DW_EH_PE_udata4:
7424 : 140 : fputs ("udata4", out);
7425 : 140 : break;
7426 : 0 : case DW_EH_PE_udata8:
7427 : 0 : fputs ("udata8", out);
7428 : 0 : break;
7429 : 0 : case DW_EH_PE_sleb128:
7430 : 0 : fputs ("sleb128", out);
7431 : 0 : break;
7432 : 0 : case DW_EH_PE_sdata2:
7433 : 0 : fputs ("sdata2", out);
7434 : 0 : break;
7435 : 502 : case DW_EH_PE_sdata4:
7436 : 502 : fputs ("sdata4", out);
7437 : 502 : break;
7438 : 0 : case DW_EH_PE_sdata8:
7439 : 0 : fputs ("sdata8", out);
7440 : 0 : break;
7441 : : default:
7442 : : /* We did not use any of the bits after all. */
7443 : : return val;
7444 : : }
7445 : :
7446 : 642 : return val & ~0xf;
7447 : : }
7448 : :
7449 : :
7450 : : static unsigned int
7451 : 502 : print_relinfo (unsigned int val, FILE *out)
7452 : : {
7453 [ + - + - : 502 : switch (val & 0x70)
- - ]
7454 : : {
7455 : 362 : case DW_EH_PE_pcrel:
7456 : 362 : fputs ("pcrel", out);
7457 : 362 : break;
7458 : 0 : case DW_EH_PE_textrel:
7459 : 0 : fputs ("textrel", out);
7460 : 0 : break;
7461 : 140 : case DW_EH_PE_datarel:
7462 : 140 : fputs ("datarel", out);
7463 : 140 : break;
7464 : 0 : case DW_EH_PE_funcrel:
7465 : 0 : fputs ("funcrel", out);
7466 : 0 : break;
7467 : 0 : case DW_EH_PE_aligned:
7468 : 0 : fputs ("aligned", out);
7469 : 0 : break;
7470 : : default:
7471 : : return val;
7472 : : }
7473 : :
7474 : 502 : return val & ~0x70;
7475 : : }
7476 : :
7477 : :
7478 : : static void
7479 : 642 : print_encoding_base (const char *pfx, unsigned int fde_encoding,
7480 : : FILE *out)
7481 : : {
7482 : 642 : fprintf (out, "(%s", pfx);
7483 : :
7484 [ - + ]: 642 : if (fde_encoding == DW_EH_PE_omit)
7485 : 0 : fputs ("omit)\n", out);
7486 : : else
7487 : : {
7488 : 642 : unsigned int w = fde_encoding;
7489 : :
7490 : 642 : w = print_encoding (w, out);
7491 : :
7492 [ + + ]: 642 : if (w & 0x70)
7493 : : {
7494 [ + - ]: 502 : if (w != fde_encoding)
7495 : 502 : fputc (' ', out);
7496 : :
7497 : 502 : w = print_relinfo (w, out);
7498 : : }
7499 : :
7500 [ - + ]: 642 : if (w != 0)
7501 [ # # ]: 0 : fprintf (out, "%s%x", w != fde_encoding ? " " : "", w);
7502 : :
7503 : 642 : fputs (")\n", out);
7504 : : }
7505 : 642 : }
7506 : :
7507 : :
7508 : : static void
7509 : 196 : print_debug_frame_section (Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr,
7510 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg,
7511 : : FILE *out)
7512 : : {
7513 : 196 : size_t shstrndx;
7514 : : /* We know this call will succeed since it did in the caller. */
7515 : 196 : (void) elf_getshdrstrndx (ebl->elf, &shstrndx);
7516 : 196 : const char *scnname = section_name (ebl, shdr);
7517 : :
7518 : : /* Needed if we find PC-relative addresses. */
7519 : 196 : GElf_Addr bias;
7520 [ - + ]: 196 : if (dwfl_module_getelf (dwflmod, &bias) == NULL)
7521 : : {
7522 : 0 : error (0, 0, _("cannot get ELF: %s"), dwfl_errmsg (-1));
7523 : 0 : return;
7524 : : }
7525 : :
7526 : 196 : bool is_eh_frame = strcmp (scnname, ".eh_frame") == 0;
7527 : 196 : Elf_Data *data;
7528 [ + + ]: 196 : if (is_eh_frame)
7529 : : {
7530 : 144 : data = elf_rawdata (scn, NULL);
7531 [ - + ]: 144 : if (data == NULL)
7532 : : {
7533 : 0 : error (0, 0, _("cannot get %s content: %s"),
7534 : : scnname, elf_errmsg (-1));
7535 : 0 : return;
7536 : : }
7537 : : }
7538 : : else
7539 : : {
7540 : 52 : data = get_debug_elf_data (dbg, ebl, IDX_debug_frame, scn);
7541 [ + - ]: 52 : if (data == NULL)
7542 : : return;
7543 : : }
7544 : :
7545 [ + + ]: 196 : if (is_eh_frame)
7546 : 144 : fprintf (out, _("\
7547 : : \nCall frame information section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
7548 : 144 : elf_ndxscn (scn), scnname, (uint64_t) shdr->sh_offset);
7549 : : else
7550 : 52 : fprintf (out, _("\
7551 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
7552 : 52 : elf_ndxscn (scn), scnname, (uint64_t) shdr->sh_offset);
7553 : :
7554 : 196 : struct cieinfo
7555 : : {
7556 : : ptrdiff_t cie_offset;
7557 : : const char *augmentation;
7558 : : unsigned int code_alignment_factor;
7559 : : unsigned int data_alignment_factor;
7560 : : uint8_t address_size;
7561 : : uint8_t fde_encoding;
7562 : : uint8_t lsda_encoding;
7563 : : struct cieinfo *next;
7564 : 196 : } *cies = NULL;
7565 : :
7566 : 196 : const unsigned char *readp = data->d_buf;
7567 : 196 : const unsigned char *const dataend = ((unsigned char *) data->d_buf
7568 : 196 : + data->d_size);
7569 [ + + ]: 27624 : while (readp < dataend)
7570 : : {
7571 [ - + ]: 27428 : if (unlikely (readp + 4 > dataend))
7572 : : {
7573 : 0 : invalid_data:
7574 : 0 : error (0, 0, _("invalid data in section [%zu] '%s'"),
7575 : : elf_ndxscn (scn), scnname);
7576 : 0 : return;
7577 : : }
7578 : :
7579 : : /* At the beginning there must be a CIE. There can be multiple,
7580 : : hence we test tis in a loop. */
7581 : 27428 : ptrdiff_t offset = readp - (unsigned char *) data->d_buf;
7582 : :
7583 [ + + ]: 27428 : Dwarf_Word unit_length = read_4ubyte_unaligned_inc (dbg, readp);
7584 : 27428 : unsigned int length = 4;
7585 [ - + ]: 27428 : if (unlikely (unit_length == 0xffffffff))
7586 : : {
7587 [ # # ]: 0 : if (unlikely (readp + 8 > dataend))
7588 : 0 : goto invalid_data;
7589 : :
7590 [ # # ]: 0 : unit_length = read_8ubyte_unaligned_inc (dbg, readp);
7591 : 0 : length = 8;
7592 : : }
7593 : :
7594 [ + + ]: 27428 : if (unlikely (unit_length == 0))
7595 : : {
7596 : 140 : fprintf (out, _("\n [%6tx] Zero terminator\n"), offset);
7597 : 140 : continue;
7598 : : }
7599 : :
7600 : 27288 : Dwarf_Word maxsize = dataend - readp;
7601 [ - + ]: 27288 : if (unlikely (unit_length > maxsize))
7602 : 0 : goto invalid_data;
7603 : :
7604 [ + + ]: 27288 : unsigned int ptr_size = ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 4 : 8;
7605 : :
7606 : 27288 : ptrdiff_t start = readp - (unsigned char *) data->d_buf;
7607 : 27288 : const unsigned char *const cieend = readp + unit_length;
7608 [ - + ]: 27288 : if (unlikely (cieend > dataend))
7609 : 0 : goto invalid_data;
7610 : :
7611 : 27288 : Dwarf_Off cie_id;
7612 [ + - ]: 27288 : if (length == 4)
7613 : : {
7614 [ - + ]: 27288 : if (unlikely (cieend - readp < 4))
7615 : 0 : goto invalid_data;
7616 [ + + ]: 27288 : cie_id = read_4ubyte_unaligned_inc (dbg, readp);
7617 [ + + ]: 27288 : if (!is_eh_frame && cie_id == DW_CIE_ID_32)
7618 : : cie_id = DW_CIE_ID_64;
7619 : : }
7620 : : else
7621 : : {
7622 [ # # ]: 0 : if (unlikely (cieend - readp < 8))
7623 : 0 : goto invalid_data;
7624 [ # # ]: 0 : cie_id = read_8ubyte_unaligned_inc (dbg, readp);
7625 : : }
7626 : :
7627 : 54492 : uint_fast8_t version = 2;
7628 : 27204 : unsigned int code_alignment_factor;
7629 : 27204 : int data_alignment_factor;
7630 : 54492 : unsigned int fde_encoding = 0;
7631 : 54492 : unsigned int lsda_encoding = 0;
7632 : 54492 : Dwarf_Word initial_location = 0;
7633 : 54492 : Dwarf_Word vma_base = 0;
7634 : :
7635 [ + + + + ]: 27380 : if (cie_id == (is_eh_frame ? 0 : DW_CIE_ID_64))
7636 : : {
7637 [ - + ]: 310 : if (unlikely (cieend - readp < 2))
7638 : 0 : goto invalid_data;
7639 : 310 : version = *readp++;
7640 : 310 : const char *const augmentation = (const char *) readp;
7641 : 310 : readp = memchr (readp, '\0', cieend - readp);
7642 [ - + ]: 310 : if (unlikely (readp == NULL))
7643 : 0 : goto invalid_data;
7644 : 310 : ++readp;
7645 : :
7646 : 310 : uint_fast8_t segment_size = 0;
7647 [ + + ]: 310 : if (version >= 4)
7648 : : {
7649 [ - + ]: 8 : if (cieend - readp < 5)
7650 : 0 : goto invalid_data;
7651 : 8 : ptr_size = *readp++;
7652 : 8 : segment_size = *readp++;
7653 : : }
7654 : :
7655 [ - + ]: 310 : if (cieend - readp < 1)
7656 : 0 : goto invalid_data;
7657 : 310 : get_uleb128 (code_alignment_factor, readp, cieend);
7658 [ - + ]: 310 : if (cieend - readp < 1)
7659 : 0 : goto invalid_data;
7660 : 310 : get_sleb128 (data_alignment_factor, readp, cieend);
7661 : :
7662 : : /* In some variant for unwind data there is another field. */
7663 [ - + ]: 310 : if (strcmp (augmentation, "eh") == 0)
7664 [ # # ]: 0 : readp += ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 4 : 8;
7665 : :
7666 : 310 : unsigned int return_address_register;
7667 [ - + ]: 310 : if (cieend - readp < 1)
7668 : 0 : goto invalid_data;
7669 [ + + ]: 310 : if (unlikely (version == 1))
7670 : 286 : return_address_register = *readp++;
7671 : : else
7672 : 24 : get_uleb128 (return_address_register, readp, cieend);
7673 : :
7674 : 310 : fprintf (out, "\n [%6tx] CIE length=%" PRIu64 "\n"
7675 : : " CIE_id: %" PRIu64 "\n"
7676 : : " version: %u\n"
7677 : : " augmentation: \"%s\"\n",
7678 : : offset, (uint64_t) unit_length, (uint64_t) cie_id,
7679 : : version, augmentation);
7680 [ + + ]: 310 : if (version >= 4)
7681 : 8 : fprintf (out, " address_size: %u\n"
7682 : : " segment_size: %u\n",
7683 : : ptr_size, segment_size);
7684 : 310 : fprintf (out, " code_alignment_factor: %u\n"
7685 : : " data_alignment_factor: %d\n"
7686 : : " return_address_register: %u\n",
7687 : : code_alignment_factor,
7688 : : data_alignment_factor, return_address_register);
7689 : :
7690 [ + + ]: 310 : if (augmentation[0] == 'z')
7691 : : {
7692 [ - + ]: 222 : if (cieend - readp < 1)
7693 : 0 : goto invalid_data;
7694 : :
7695 : 222 : unsigned int augmentationlen;
7696 : 222 : get_uleb128 (augmentationlen, readp, cieend);
7697 : :
7698 [ - + ]: 222 : if (augmentationlen > (size_t) (cieend - readp))
7699 : : {
7700 : 0 : error (0, 0, _("invalid augmentation length"));
7701 : 0 : readp = cieend;
7702 : 0 : continue;
7703 : : }
7704 : :
7705 : 222 : const char *hdr = "Augmentation data:";
7706 : 222 : const char *cp = augmentation + 1;
7707 [ + + + - ]: 444 : while (*cp != '\0' && cp < augmentation + augmentationlen + 1)
7708 : : {
7709 : 222 : fprintf (out, " %-26s%#x ", hdr, *readp);
7710 : 222 : hdr = "";
7711 : :
7712 [ + - ]: 222 : if (*cp == 'R')
7713 : : {
7714 : 222 : fde_encoding = *readp++;
7715 : 222 : print_encoding_base (_("FDE address encoding: "),
7716 : : fde_encoding, out);
7717 : : }
7718 [ # # ]: 0 : else if (*cp == 'L')
7719 : : {
7720 : 0 : lsda_encoding = *readp++;
7721 : 0 : print_encoding_base (_("LSDA pointer encoding: "),
7722 : : lsda_encoding, out);
7723 : : }
7724 [ # # ]: 0 : else if (*cp == 'P')
7725 : : {
7726 : : /* Personality. This field usually has a relocation
7727 : : attached pointing to __gcc_personality_v0. */
7728 : 0 : const unsigned char *startp = readp;
7729 : 0 : unsigned int encoding = *readp++;
7730 : 0 : uint64_t val = 0;
7731 : 0 : readp = read_encoded (encoding, readp,
7732 : : readp - 1 + augmentationlen,
7733 : : &val, dbg);
7734 : :
7735 [ # # ]: 0 : while (++startp < readp)
7736 : 0 : fprintf (out, "%#x ", *startp);
7737 : :
7738 : 0 : fputc ('(', out);
7739 : 0 : print_encoding (encoding, out);
7740 : 0 : fputc (' ', out);
7741 [ # # ]: 0 : switch (encoding & 0xf)
7742 : : {
7743 : 0 : case DW_EH_PE_sleb128:
7744 : : case DW_EH_PE_sdata2:
7745 : : case DW_EH_PE_sdata4:
7746 : 0 : fprintf (out, "%" PRId64 ")\n", val);
7747 : 0 : break;
7748 : 0 : default:
7749 : 0 : fprintf (out, "%#" PRIx64 ")\n", val);
7750 : 0 : break;
7751 : : }
7752 : : }
7753 : : else
7754 : 0 : fprintf (out, "(%x)\n", *readp++);
7755 : :
7756 : 222 : ++cp;
7757 : : }
7758 : : }
7759 : :
7760 [ + - ]: 310 : if (likely (ptr_size == 4 || ptr_size == 8))
7761 : : {
7762 : 310 : struct cieinfo *newp = alloca (sizeof (*newp));
7763 : 310 : newp->cie_offset = offset;
7764 : 310 : newp->augmentation = augmentation;
7765 : 310 : newp->fde_encoding = fde_encoding;
7766 : 310 : newp->lsda_encoding = lsda_encoding;
7767 : 310 : newp->address_size = ptr_size;
7768 : 310 : newp->code_alignment_factor = code_alignment_factor;
7769 : 310 : newp->data_alignment_factor = data_alignment_factor;
7770 : 310 : newp->next = cies;
7771 : 310 : cies = newp;
7772 : : }
7773 : : }
7774 : : else
7775 : : {
7776 : : struct cieinfo *cie = cies;
7777 [ + - ]: 26978 : while (cie != NULL)
7778 [ + + - + ]: 53956 : if (is_eh_frame
7779 : 26886 : ? ((Dwarf_Off) start - cie_id) == (Dwarf_Off) cie->cie_offset
7780 : 92 : : cie_id == (Dwarf_Off) cie->cie_offset)
7781 : : break;
7782 : : else
7783 : 0 : cie = cie->next;
7784 : 26978 : if (unlikely (cie == NULL))
7785 : : {
7786 : 0 : fputs ("invalid CIE reference in FDE\n", out);
7787 : 0 : return;
7788 : : }
7789 : :
7790 : : /* Initialize from CIE data. */
7791 : 26978 : fde_encoding = cie->fde_encoding;
7792 : 26978 : lsda_encoding = cie->lsda_encoding;
7793 : 26978 : ptr_size = encoded_ptr_size (fde_encoding, cie->address_size);
7794 : 26978 : code_alignment_factor = cie->code_alignment_factor;
7795 : 26978 : data_alignment_factor = cie->data_alignment_factor;
7796 : :
7797 : 26978 : const unsigned char *base = readp;
7798 : : // XXX There are sometimes relocations for this value
7799 [ - + + + : 26978 : initial_location = read_addr_unaligned_inc (ptr_size, dbg, readp);
- + + + ]
7800 : 53956 : Dwarf_Word address_range
7801 [ + + - + : 26978 : = read_addr_unaligned_inc (ptr_size, dbg, readp);
+ + ]
7802 : :
7803 : : /* pcrel for an FDE address is relative to the runtime
7804 : : address of the start_address field itself. Sign extend
7805 : : if necessary to make sure the calculation is done on the
7806 : : full 64 bit address even when initial_location only holds
7807 : : the lower 32 bits. */
7808 : 26978 : Dwarf_Addr pc_start = initial_location;
7809 [ + + ]: 26978 : if (ptr_size == 4)
7810 : 26898 : pc_start = (uint64_t) (int32_t) pc_start;
7811 [ + + ]: 26978 : if ((fde_encoding & 0x70) == DW_EH_PE_pcrel)
7812 : 26874 : pc_start += ((uint64_t) shdr->sh_addr
7813 : 26874 : + (base - (const unsigned char *) data->d_buf)
7814 : 26874 : - bias);
7815 : :
7816 : 26978 : fprintf (out, "\n [%6tx] FDE length=%" PRIu64 " cie=[%6tx]\n"
7817 : : " CIE_pointer: %" PRIu64 "\n"
7818 : : " initial_location: ",
7819 : : offset, (uint64_t) unit_length,
7820 : : cie->cie_offset, (uint64_t) cie_id);
7821 : 26978 : print_dwarf_addr (dwflmod, cie->address_size,
7822 : : pc_start, initial_location, out);
7823 [ + + ]: 26978 : if ((fde_encoding & 0x70) == DW_EH_PE_pcrel)
7824 : : {
7825 : 53748 : vma_base = (((uint64_t) shdr->sh_offset
7826 : 26874 : + (base - (const unsigned char *) data->d_buf)
7827 : 26874 : + (uint64_t) initial_location)
7828 : : & (ptr_size == 4
7829 : : ? UINT64_C (0xffffffff)
7830 [ - + ]: 26874 : : UINT64_C (0xffffffffffffffff)));
7831 : 26874 : fprintf (out, _(" (offset: %#" PRIx64 ")"),
7832 : : (uint64_t) vma_base);
7833 : : }
7834 : :
7835 : 26978 : fprintf (out, "\n address_range: %#" PRIx64,
7836 : : (uint64_t) address_range);
7837 [ + + ]: 26978 : if ((fde_encoding & 0x70) == DW_EH_PE_pcrel)
7838 : 26874 : fprintf (out, _(" (end offset: %#" PRIx64 ")"),
7839 : 26874 : ((uint64_t) vma_base + (uint64_t) address_range)
7840 : : & (ptr_size == 4
7841 : : ? UINT64_C (0xffffffff)
7842 [ - + ]: 26874 : : UINT64_C (0xffffffffffffffff)));
7843 : 26978 : fputc ('\n', out);
7844 : :
7845 [ + + ]: 26978 : if (cie->augmentation[0] == 'z')
7846 : : {
7847 : 26874 : unsigned int augmentationlen;
7848 [ - + ]: 26874 : if (cieend - readp < 1)
7849 : 0 : goto invalid_data;
7850 : 26874 : get_uleb128 (augmentationlen, readp, cieend);
7851 : :
7852 [ - + ]: 26874 : if (augmentationlen > (size_t) (cieend - readp))
7853 : : {
7854 : 0 : error (0, 0, _("invalid augmentation length"));
7855 : 0 : readp = cieend;
7856 : 0 : continue;
7857 : : }
7858 : :
7859 [ - + ]: 26874 : if (augmentationlen > 0)
7860 : : {
7861 : 0 : const char *hdr = "Augmentation data:";
7862 : 0 : const char *cp = cie->augmentation + 1;
7863 : 0 : unsigned int u = 0;
7864 : 0 : while (*cp != '\0'
7865 [ # # # # ]: 0 : && cp < cie->augmentation + augmentationlen + 1)
7866 : : {
7867 [ # # ]: 0 : if (*cp == 'L')
7868 : : {
7869 : 0 : uint64_t lsda_pointer;
7870 : 0 : const unsigned char *p
7871 : 0 : = read_encoded (lsda_encoding, &readp[u],
7872 : : &readp[augmentationlen],
7873 : : &lsda_pointer, dbg);
7874 : 0 : u = p - readp;
7875 : 0 : fprintf (out, _("\
7876 : : %-26sLSDA pointer: %#" PRIx64 "\n"),
7877 : : hdr, lsda_pointer);
7878 : 0 : hdr = "";
7879 : : }
7880 : 0 : ++cp;
7881 : : }
7882 : :
7883 [ # # ]: 0 : while (u < augmentationlen)
7884 : : {
7885 : 0 : fprintf (out, " %-26s%#x\n", hdr, readp[u++]);
7886 : 0 : hdr = "";
7887 : : }
7888 : : }
7889 : :
7890 : 26874 : readp += augmentationlen;
7891 : : }
7892 : : }
7893 : :
7894 : : /* Handle the initialization instructions. */
7895 [ - + ]: 27288 : if (ptr_size != 4 && ptr_size !=8)
7896 : 0 : fprintf (out, "invalid CIE pointer size (%u), must be 4 or 8.\n",
7897 : : ptr_size);
7898 : : else
7899 : 27288 : print_cfa_program (readp, cieend, vma_base, code_alignment_factor,
7900 : : data_alignment_factor, version, ptr_size,
7901 : : fde_encoding, dwflmod, ebl, ehdr, dbg, out);
7902 : 27288 : readp = cieend;
7903 : : }
7904 : : }
7905 : :
7906 : :
7907 : : /* Returns the signedness (or false if it cannot be determined) and
7908 : : the byte size (or zero if it cannot be gotten) of the given DIE
7909 : : DW_AT_type attribute. Uses dwarf_peel_type and dwarf_aggregate_size. */
7910 : : static void
7911 : 408120 : die_type_sign_bytes (Dwarf_Die *die, bool *is_signed, int *bytes)
7912 : : {
7913 : 408120 : Dwarf_Attribute attr;
7914 : 408120 : Dwarf_Die type;
7915 : :
7916 : 408120 : *bytes = 0;
7917 : 408120 : *is_signed = false;
7918 : :
7919 [ + + ]: 408120 : if (dwarf_peel_type (dwarf_formref_die (dwarf_attr_integrate (die,
7920 : : DW_AT_type,
7921 : : &attr), &type),
7922 : : &type) == 0)
7923 : : {
7924 : 1066 : Dwarf_Word val;
7925 : 1066 : *is_signed = (dwarf_formudata (dwarf_attr (&type, DW_AT_encoding,
7926 : : &attr), &val) == 0
7927 [ + + + + ]: 1066 : && (val == DW_ATE_signed || val == DW_ATE_signed_char));
7928 : :
7929 [ + - ]: 1066 : if (dwarf_aggregate_size (&type, &val) == 0)
7930 : 1066 : *bytes = val;
7931 : : }
7932 : 408120 : }
7933 : :
7934 : : struct attrcb_args
7935 : : {
7936 : : Dwfl_Module *dwflmod;
7937 : : Dwarf *dbg;
7938 : : Dwarf_Die *dies;
7939 : : int level;
7940 : : bool silent;
7941 : : bool is_split;
7942 : : unsigned int version;
7943 : : unsigned int addrsize;
7944 : : unsigned int offset_size;
7945 : : struct Dwarf_CU *cu;
7946 : : FILE *out;
7947 : : };
7948 : :
7949 : :
7950 : : static int
7951 : 9295354 : attr_callback (Dwarf_Attribute *attrp, void *arg)
7952 : : {
7953 : 9295354 : struct attrcb_args *cbargs = (struct attrcb_args *) arg;
7954 : 9295354 : const int level = cbargs->level;
7955 : 9295354 : Dwarf_Die *die = &cbargs->dies[level];
7956 : 9295354 : bool is_split = cbargs->is_split;
7957 : 9295354 : FILE *out = cbargs->out;
7958 : :
7959 [ + - ]: 9295354 : unsigned int attr = dwarf_whatattr (attrp);
7960 [ - + ]: 9295354 : if (unlikely (attr == 0))
7961 : : {
7962 [ # # ]: 0 : if (!cbargs->silent)
7963 : 0 : error (0, 0, _("DIE [%" PRIx64 "] "
7964 : : "cannot get attribute code: %s"),
7965 : : dwarf_dieoffset (die), dwarf_errmsg (-1));
7966 : 0 : return DWARF_CB_ABORT;
7967 : : }
7968 : :
7969 [ - + ]: 9295354 : unsigned int form = dwarf_whatform (attrp);
7970 [ - + ]: 9295354 : if (unlikely (form == 0))
7971 : : {
7972 [ # # ]: 0 : if (!cbargs->silent)
7973 : 0 : error (0, 0, _("DIE [%" PRIx64 "] "
7974 : : "cannot get attribute form: %s"),
7975 : : dwarf_dieoffset (die), dwarf_errmsg (-1));
7976 : 0 : return DWARF_CB_ABORT;
7977 : : }
7978 : :
7979 [ + + + + : 9295354 : switch (form)
+ + + +
- ]
7980 : : {
7981 : 114582 : case DW_FORM_addr:
7982 : : case DW_FORM_addrx:
7983 : : case DW_FORM_addrx1:
7984 : : case DW_FORM_addrx2:
7985 : : case DW_FORM_addrx3:
7986 : : case DW_FORM_addrx4:
7987 : : case DW_FORM_GNU_addr_index:
7988 [ + + ]: 114582 : if (!cbargs->silent)
7989 : : {
7990 : 114040 : Dwarf_Addr addr;
7991 [ - + ]: 114040 : if (unlikely (dwarf_formaddr (attrp, &addr) != 0))
7992 : : {
7993 : 0 : attrval_out:
7994 [ # # ]: 0 : if (!cbargs->silent)
7995 : 0 : error (0, 0, _("DIE [%" PRIx64 "] "
7996 : : "cannot get attribute '%s' (%s) value: "
7997 : : "%s"),
7998 : : dwarf_dieoffset (die),
7999 : : dwarf_attr_name (attr),
8000 : : dwarf_form_name (form),
8001 : : dwarf_errmsg (-1));
8002 : : /* Don't ABORT, it might be other attributes can be resolved. */
8003 : 0 : return DWARF_CB_OK;
8004 : : }
8005 [ + + ]: 114040 : if (form != DW_FORM_addr )
8006 : : {
8007 : 40 : Dwarf_Word word;
8008 [ - + ]: 40 : if (dwarf_formudata (attrp, &word) != 0)
8009 : 0 : goto attrval_out;
8010 : 40 : fprintf (out, " %*s%-20s (%s) [%" PRIx64 "] ",
8011 : : (int) (level * 2), "", dwarf_attr_name (attr),
8012 : : dwarf_form_name (form), word);
8013 : : }
8014 : : else
8015 : 114000 : fprintf (out, " %*s%-20s (%s) ",
8016 : : (int) (level * 2), "", dwarf_attr_name (attr),
8017 : : dwarf_form_name (form));
8018 : 114040 : print_dwarf_addr (cbargs->dwflmod, cbargs->addrsize, addr, addr, out);
8019 : 114040 : fprintf (out, "\n");
8020 : : }
8021 : 8361910 : break;
8022 : :
8023 : 1538066 : case DW_FORM_indirect:
8024 : : case DW_FORM_strp:
8025 : : case DW_FORM_line_strp:
8026 : : case DW_FORM_strx:
8027 : : case DW_FORM_strx1:
8028 : : case DW_FORM_strx2:
8029 : : case DW_FORM_strx3:
8030 : : case DW_FORM_strx4:
8031 : : case DW_FORM_string:
8032 : : case DW_FORM_GNU_strp_alt:
8033 : : case DW_FORM_GNU_str_index:
8034 [ + + ]: 1538066 : if (cbargs->silent)
8035 : : break;
8036 : 1536556 : const char *str = dwarf_formstring (attrp);
8037 [ - + ]: 1536556 : if (unlikely (str == NULL))
8038 : 0 : goto attrval_out;
8039 : 1536556 : fprintf (out, " %*s%-20s (%s) \"%s\"\n",
8040 : : (int) (level * 2), "", dwarf_attr_name (attr),
8041 : : dwarf_form_name (form), str);
8042 : 1536556 : break;
8043 : :
8044 : 2025978 : case DW_FORM_ref_addr:
8045 : : case DW_FORM_ref_udata:
8046 : : case DW_FORM_ref8:
8047 : : case DW_FORM_ref4:
8048 : : case DW_FORM_ref2:
8049 : : case DW_FORM_ref1:
8050 : : case DW_FORM_GNU_ref_alt:
8051 : : case DW_FORM_ref_sup4:
8052 : : case DW_FORM_ref_sup8:
8053 [ + + ]: 2025978 : if (cbargs->silent)
8054 : : break;
8055 : 2024398 : Dwarf_Die ref;
8056 [ - + ]: 2024398 : if (unlikely (dwarf_formref_die (attrp, &ref) == NULL))
8057 : 0 : goto attrval_out;
8058 : :
8059 : 2024398 : fprintf (out, " %*s%-20s (%s) ",
8060 : : (int) (level * 2), "", dwarf_attr_name (attr),
8061 : : dwarf_form_name (form));
8062 [ + + ]: 2024398 : if (is_split)
8063 : 66 : fprintf (out, "{%6" PRIxMAX "}\n", (uintmax_t) dwarf_dieoffset (&ref));
8064 : : else
8065 : 2024332 : fprintf (out, "[%6" PRIxMAX "]\n", (uintmax_t) dwarf_dieoffset (&ref));
8066 : : break;
8067 : :
8068 : 8 : case DW_FORM_ref_sig8:
8069 [ + - ]: 8 : if (cbargs->silent)
8070 : : break;
8071 : 8 : fprintf (out, " %*s%-20s (%s) {%6" PRIx64 "}\n",
8072 : : (int) (level * 2), "", dwarf_attr_name (attr),
8073 : : dwarf_form_name (form),
8074 [ - + ]: 8 : (uint64_t) read_8ubyte_unaligned (attrp->cu->dbg, attrp->valp));
8075 : 8 : break;
8076 : :
8077 : 5162816 : case DW_FORM_sec_offset:
8078 : : case DW_FORM_rnglistx:
8079 : : case DW_FORM_loclistx:
8080 : : case DW_FORM_implicit_const:
8081 : : case DW_FORM_udata:
8082 : : case DW_FORM_sdata:
8083 : : case DW_FORM_data8: /* Note no data16 here, we see that as block. */
8084 : : case DW_FORM_data4:
8085 : : case DW_FORM_data2:
8086 : 5162816 : case DW_FORM_data1:;
8087 : 5162816 : Dwarf_Word num;
8088 [ - + ]: 5162816 : if (unlikely (dwarf_formudata (attrp, &num) != 0))
8089 : 0 : goto attrval_out;
8090 : :
8091 : 5162816 : const char *valuestr = NULL;
8092 : 5162816 : bool as_hex_id = false;
8093 [ + + + + : 5162816 : switch (attr)
+ + + + +
+ - - - -
- - + - +
+ + ]
8094 : : {
8095 : : /* This case can take either a constant or a loclistptr. */
8096 : 641250 : case DW_AT_data_member_location:
8097 [ + - ]: 641250 : if (form != DW_FORM_sec_offset
8098 [ + + ]: 641250 : && (cbargs->version >= 4
8099 [ + - ]: 47612 : || (form != DW_FORM_data4 && form != DW_FORM_data8)))
8100 : : {
8101 [ + - ]: 641250 : if (!cbargs->silent)
8102 : 641250 : fprintf (out, " %*s%-20s (%s) %" PRIuMAX "\n",
8103 : : (int) (level * 2), "", dwarf_attr_name (attr),
8104 : : dwarf_form_name (form), (uintmax_t) num);
8105 : 641250 : return DWARF_CB_OK;
8106 : : }
8107 : 251270 : FALLTHROUGH;
8108 : :
8109 : : /* These cases always take a loclist[ptr] and no constant. */
8110 : : case DW_AT_location:
8111 : : case DW_AT_data_location:
8112 : : case DW_AT_vtable_elem_location:
8113 : : case DW_AT_string_length:
8114 : : case DW_AT_use_location:
8115 : : case DW_AT_frame_base:
8116 : : case DW_AT_return_addr:
8117 : : case DW_AT_static_link:
8118 : : case DW_AT_segment:
8119 : : case DW_AT_GNU_call_site_value:
8120 : : case DW_AT_GNU_call_site_data_value:
8121 : : case DW_AT_GNU_call_site_target:
8122 : : case DW_AT_GNU_call_site_target_clobbered:
8123 : : case DW_AT_GNU_locviews:
8124 : : {
8125 : 251270 : bool nlpt;
8126 [ + + ]: 251270 : if (cbargs->cu->version < 5)
8127 : : {
8128 [ + + ]: 324 : if (! cbargs->is_split)
8129 : : {
8130 : 284 : nlpt = notice_listptr (section_loc, &known_locsptr,
8131 : 284 : cbargs->addrsize,
8132 : 284 : cbargs->offset_size,
8133 : : cbargs->cu, num, attr);
8134 : : }
8135 : : else
8136 : : nlpt = true;
8137 : : }
8138 : : else
8139 : : {
8140 : : /* Only register for a real section offset. Otherwise
8141 : : it is a DW_FORM_loclistx which is just an index
8142 : : number and we should already have registered the
8143 : : section offset for the index when we saw the
8144 : : DW_AT_loclists_base CU attribute. */
8145 [ + + ]: 250946 : if (form == DW_FORM_sec_offset)
8146 : 250868 : nlpt = notice_listptr (section_loc, &known_loclistsptr,
8147 : 250868 : cbargs->addrsize, cbargs->offset_size,
8148 : : cbargs->cu, num, attr);
8149 : : else
8150 : : nlpt = true;
8151 : :
8152 : : }
8153 : :
8154 [ + + ]: 251270 : if (!cbargs->silent)
8155 : : {
8156 [ + + + + ]: 250904 : if (cbargs->cu->version < 5 || form == DW_FORM_sec_offset)
8157 [ - + ]: 250890 : fprintf (out, " %*s%-20s (%s) location list [%6"
8158 : : PRIxMAX "]%s\n",
8159 : : (int) (level * 2), "", dwarf_attr_name (attr),
8160 : : dwarf_form_name (form), (uintmax_t) num,
8161 : : nlpt ? "" : " <WARNING offset too big>");
8162 : : else
8163 : 14 : fprintf (out, " %*s%-20s (%s) location index [%6"
8164 : : PRIxMAX "]\n",
8165 : : (int) (level * 2), "", dwarf_attr_name (attr),
8166 : : dwarf_form_name (form), (uintmax_t) num);
8167 : : }
8168 : : }
8169 : : return DWARF_CB_OK;
8170 : :
8171 : 10 : case DW_AT_loclists_base:
8172 : : {
8173 : 20 : bool nlpt = notice_listptr (section_loc, &known_loclistsptr,
8174 : 10 : cbargs->addrsize, cbargs->offset_size,
8175 : : cbargs->cu, num, attr);
8176 : :
8177 [ + + ]: 10 : if (!cbargs->silent)
8178 [ - + ]: 2 : fprintf (out, " %*s%-20s (%s) location list [%6" PRIxMAX "]%s\n",
8179 : : (int) (level * 2), "", dwarf_attr_name (attr),
8180 : : dwarf_form_name (form), (uintmax_t) num,
8181 : : nlpt ? "" : " <WARNING offset too big>");
8182 : : }
8183 : : return DWARF_CB_OK;
8184 : :
8185 : 40864 : case DW_AT_ranges:
8186 : : case DW_AT_start_scope:
8187 : : {
8188 : 40864 : bool nlpt;
8189 [ + + ]: 40864 : if (cbargs->cu->version < 5)
8190 : 106 : nlpt = notice_listptr (section_ranges, &known_rangelistptr,
8191 : 106 : cbargs->addrsize, cbargs->offset_size,
8192 : : cbargs->cu, num, attr);
8193 : : else
8194 : : {
8195 : : /* Only register for a real section offset. Otherwise
8196 : : it is a DW_FORM_rangelistx which is just an index
8197 : : number and we should already have registered the
8198 : : section offset for the index when we saw the
8199 : : DW_AT_rnglists_base CU attribute. */
8200 [ + + ]: 40758 : if (form == DW_FORM_sec_offset)
8201 : 40746 : nlpt = notice_listptr (section_ranges, &known_rnglistptr,
8202 : 40746 : cbargs->addrsize, cbargs->offset_size,
8203 : : cbargs->cu, num, attr);
8204 : : else
8205 : : nlpt = true;
8206 : : }
8207 : :
8208 [ + + ]: 40864 : if (!cbargs->silent)
8209 : : {
8210 [ + + + + ]: 40778 : if (cbargs->cu->version < 5 || form == DW_FORM_sec_offset)
8211 [ - + ]: 40774 : fprintf (out, " %*s%-20s (%s) range list [%6"
8212 : : PRIxMAX "]%s\n",
8213 : : (int) (level * 2), "", dwarf_attr_name (attr),
8214 : : dwarf_form_name (form), (uintmax_t) num,
8215 : : nlpt ? "" : " <WARNING offset too big>");
8216 : : else
8217 : 4 : fprintf (out, " %*s%-20s (%s) range index [%6"
8218 : : PRIxMAX "]\n",
8219 : : (int) (level * 2), "", dwarf_attr_name (attr),
8220 : : dwarf_form_name (form), (uintmax_t) num);
8221 : : }
8222 : : }
8223 : : return DWARF_CB_OK;
8224 : :
8225 : 8 : case DW_AT_rnglists_base:
8226 : : {
8227 : 16 : bool nlpt = notice_listptr (section_ranges, &known_rnglistptr,
8228 : 8 : cbargs->addrsize, cbargs->offset_size,
8229 : : cbargs->cu, num, attr);
8230 [ + + ]: 8 : if (!cbargs->silent)
8231 [ - + ]: 4 : fprintf (out, " %*s%-20s (%s) range list [%6"
8232 : : PRIxMAX "]%s\n",
8233 : : (int) (level * 2), "", dwarf_attr_name (attr),
8234 : : dwarf_form_name (form), (uintmax_t) num,
8235 : : nlpt ? "" : " <WARNING offset too big>");
8236 : : }
8237 : : return DWARF_CB_OK;
8238 : :
8239 : 34 : case DW_AT_addr_base:
8240 : : case DW_AT_GNU_addr_base:
8241 : : {
8242 : 68 : bool addrbase = notice_listptr (section_addr, &known_addrbases,
8243 : 34 : cbargs->addrsize,
8244 : 34 : cbargs->offset_size,
8245 : : cbargs->cu, num, attr);
8246 [ + + ]: 34 : if (!cbargs->silent)
8247 [ - + ]: 18 : fprintf (out, " %*s%-20s (%s) address base [%6"
8248 : : PRIxMAX "]%s\n",
8249 : : (int) (level * 2), "", dwarf_attr_name (attr),
8250 : : dwarf_form_name (form), (uintmax_t) num,
8251 : : addrbase ? "" : " <WARNING offset too big>");
8252 : : }
8253 : : return DWARF_CB_OK;
8254 : :
8255 : 8 : case DW_AT_str_offsets_base:
8256 : : {
8257 : 16 : bool stroffbase = notice_listptr (section_str, &known_stroffbases,
8258 : 8 : cbargs->addrsize,
8259 : 8 : cbargs->offset_size,
8260 : : cbargs->cu, num, attr);
8261 [ + - ]: 8 : if (!cbargs->silent)
8262 [ - + ]: 8 : fprintf (out, " %*s%-20s (%s) str offsets base [%6"
8263 : : PRIxMAX "]%s\n",
8264 : : (int) (level * 2), "", dwarf_attr_name (attr),
8265 : : dwarf_form_name (form), (uintmax_t) num,
8266 : : stroffbase ? "" : " <WARNING offset too big>");
8267 : : }
8268 : : return DWARF_CB_OK;
8269 : :
8270 : 4240 : case DW_AT_language:
8271 : 4240 : valuestr = dwarf_lang_name (num);
8272 : 4240 : break;
8273 : 4 : case DW_AT_language_name:
8274 : 4 : valuestr = dwarf_lname_name (num);
8275 : 4 : break;
8276 : 67820 : case DW_AT_encoding:
8277 : 67820 : valuestr = dwarf_encoding_name (num);
8278 : 67820 : break;
8279 : 0 : case DW_AT_accessibility:
8280 : 0 : valuestr = dwarf_access_name (num);
8281 : 0 : break;
8282 : 0 : case DW_AT_defaulted:
8283 : 0 : valuestr = dwarf_defaulted_name (num);
8284 : 0 : break;
8285 : 0 : case DW_AT_visibility:
8286 : 0 : valuestr = dwarf_visibility_name (num);
8287 : 0 : break;
8288 : 0 : case DW_AT_virtuality:
8289 : 0 : valuestr = dwarf_virtuality_name (num);
8290 : 0 : break;
8291 : 0 : case DW_AT_identifier_case:
8292 : 0 : valuestr = dwarf_identifier_case_name (num);
8293 : 0 : break;
8294 : 0 : case DW_AT_calling_convention:
8295 : 0 : valuestr = dwarf_calling_convention_name (num);
8296 : 0 : break;
8297 : 8272 : case DW_AT_inline:
8298 : 8272 : valuestr = dwarf_inline_name (num);
8299 : 8272 : break;
8300 : 0 : case DW_AT_ordering:
8301 : 0 : valuestr = dwarf_ordering_name (num);
8302 : 0 : break;
8303 : 1129758 : case DW_AT_decl_file:
8304 : : case DW_AT_call_file:
8305 : : {
8306 [ + + ]: 1129758 : if (cbargs->silent)
8307 : : break;
8308 : :
8309 : : /* Try to get the actual file, the current interface only
8310 : : gives us full paths, but we only want to show the file
8311 : : name for now. */
8312 : 1128910 : Dwarf_Die cudie;
8313 [ + - ]: 1128910 : if (dwarf_cu_die (cbargs->cu, &cudie,
8314 : : NULL, NULL, NULL, NULL, NULL, NULL) != NULL)
8315 : : {
8316 : 1128910 : Dwarf_Files *files;
8317 : 1128910 : size_t nfiles;
8318 [ + - ]: 1128910 : if (dwarf_getsrcfiles (&cudie, &files, &nfiles) == 0)
8319 : : {
8320 : 1128910 : valuestr = dwarf_filesrc (files, num, NULL, NULL);
8321 [ + - ]: 1128910 : if (valuestr != NULL)
8322 : : {
8323 : 1128910 : const char *filename = strrchr (valuestr, '/');
8324 [ - + ]: 1128910 : if (filename != NULL)
8325 : 1128910 : valuestr = filename + 1;
8326 : : }
8327 : : else
8328 : 0 : error (0, 0, _("invalid file (%" PRId64 "): %s"),
8329 : : num, dwarf_errmsg (-1));
8330 : : }
8331 : : else
8332 : 0 : error (0, 0, _("no srcfiles for CU [%" PRIx64 "]"),
8333 : : dwarf_dieoffset (&cudie));
8334 : : }
8335 : : else
8336 : 0 : error (0, 0, _("couldn't get DWARF CU: %s"),
8337 : : dwarf_errmsg (-1));
8338 : 1128910 : if (valuestr == NULL)
8339 : : valuestr = "???";
8340 : : }
8341 : 1128910 : break;
8342 : 26 : case DW_AT_GNU_dwo_id:
8343 : 26 : as_hex_id = true;
8344 : 26 : break;
8345 : :
8346 : : default:
8347 : : /* Nothing. */
8348 : : break;
8349 : : }
8350 : :
8351 [ + + ]: 4229372 : if (cbargs->silent)
8352 : : break;
8353 : :
8354 : : /* When highpc is in constant form it is relative to lowpc.
8355 : : In that case also show the address. */
8356 : 4225662 : Dwarf_Addr highpc;
8357 [ + + + - ]: 4225662 : if (attr == DW_AT_high_pc && dwarf_highpc (die, &highpc) == 0)
8358 : : {
8359 : 26994 : fprintf (out, " %*s%-20s (%s) %" PRIuMAX " (",
8360 : : (int) (level * 2), "", dwarf_attr_name (attr),
8361 : : dwarf_form_name (form), (uintmax_t) num);
8362 : 26994 : print_dwarf_addr (cbargs->dwflmod, cbargs->addrsize,
8363 : : highpc, highpc, out);
8364 : 26994 : fprintf (out, ")\n");
8365 : : }
8366 : : else
8367 : : {
8368 [ + + ]: 4198668 : if (as_hex_id)
8369 : : {
8370 : 4 : fprintf (out, " %*s%-20s (%s) 0x%.16" PRIx64 "\n",
8371 : : (int) (level * 2), "", dwarf_attr_name (attr),
8372 : : dwarf_form_name (form), num);
8373 : : }
8374 : : else
8375 : : {
8376 : 4198664 : Dwarf_Sword snum = 0;
8377 : 4198664 : bool is_signed;
8378 : 4198664 : int bytes = 0;
8379 [ + + ]: 4198664 : if (attr == DW_AT_const_value)
8380 : 408112 : die_type_sign_bytes (die, &is_signed, &bytes);
8381 : : else
8382 : 3790552 : is_signed = (form == DW_FORM_sdata
8383 : 3790552 : || form == DW_FORM_implicit_const);
8384 : :
8385 [ + + ]: 4198664 : if (is_signed)
8386 [ - + ]: 445530 : if (unlikely (dwarf_formsdata (attrp, &snum) != 0))
8387 : 0 : goto attrval_out;
8388 : :
8389 [ + + ]: 4198664 : if (valuestr == NULL)
8390 : : {
8391 : 2990028 : fprintf (out, " %*s%-20s (%s) ",
8392 : : (int) (level * 2), "", dwarf_attr_name (attr),
8393 : : dwarf_form_name (form));
8394 : : }
8395 : : else
8396 : : {
8397 : 1208636 : fprintf (out, " %*s%-20s (%s) %s (",
8398 : : (int) (level * 2), "", dwarf_attr_name (attr),
8399 : : dwarf_form_name (form), valuestr);
8400 : : }
8401 : :
8402 [ + + + + : 4198664 : switch (bytes)
+ ]
8403 : : {
8404 : 10 : case 1:
8405 [ + + ]: 10 : if (is_signed)
8406 : 2 : fprintf (out, "%" PRId8, (int8_t) snum);
8407 : : else
8408 : 8 : fprintf (out, "%" PRIu8, (uint8_t) num);
8409 : : break;
8410 : :
8411 : 4 : case 2:
8412 [ + + ]: 4 : if (is_signed)
8413 : 2 : fprintf (out, "%" PRId16, (int16_t) snum);
8414 : : else
8415 : 2 : fprintf (out, "%" PRIu16, (uint16_t) num);
8416 : : break;
8417 : :
8418 : 254 : case 4:
8419 [ + + ]: 254 : if (is_signed)
8420 : 228 : fprintf (out, "%" PRId32, (int32_t) snum);
8421 : : else
8422 : 26 : fprintf (out, "%" PRIu32, (uint32_t) num);
8423 : : break;
8424 : :
8425 : 790 : case 8:
8426 [ + + ]: 790 : if (is_signed)
8427 : 2 : fprintf (out, "%" PRId64, (int64_t) snum);
8428 : : else
8429 : 788 : fprintf (out, "%" PRIu64, (uint64_t) num);
8430 : : break;
8431 : :
8432 : 4197606 : default:
8433 [ + + ]: 4197606 : if (is_signed)
8434 : 445296 : fprintf (out, "%" PRIdMAX, (intmax_t) snum);
8435 : : else
8436 : 3752310 : fprintf (out, "%" PRIuMAX, (uintmax_t) num);
8437 : : break;
8438 : : }
8439 : :
8440 : : /* Make clear if we switched from a signed encoding to
8441 : : an unsigned value. */
8442 [ + + ]: 4198664 : if (attr == DW_AT_const_value
8443 [ + + ]: 408112 : && (form == DW_FORM_sdata || form == DW_FORM_implicit_const)
8444 [ + + ]: 2740 : && !is_signed)
8445 : 2720 : fprintf (out, " (%" PRIdMAX ")", (intmax_t) num);
8446 : :
8447 [ + + ]: 4198664 : if (valuestr == NULL)
8448 : 2990028 : fprintf (out, "\n");
8449 : : else
8450 : 1208636 : fprintf (out, ")\n");
8451 : : }
8452 : : }
8453 : : break;
8454 : :
8455 : 18244 : case DW_FORM_flag:
8456 [ + + ]: 18244 : if (cbargs->silent)
8457 : : break;
8458 : 18096 : bool flag;
8459 [ - + ]: 18096 : if (unlikely (dwarf_formflag (attrp, &flag) != 0))
8460 : 0 : goto attrval_out;
8461 : :
8462 : 18096 : fprintf (out, " %*s%-20s (%s) %s\n",
8463 : : (int) (level * 2), "", dwarf_attr_name (attr),
8464 [ + - ]: 18096 : dwarf_form_name (form), flag ? yes_str : no_str);
8465 : 18096 : break;
8466 : :
8467 : 185550 : case DW_FORM_flag_present:
8468 [ + + ]: 185550 : if (cbargs->silent)
8469 : : break;
8470 : 184996 : fprintf (out, " %*s%-20s (%s) %s\n",
8471 : : (int) (level * 2), "", dwarf_attr_name (attr),
8472 : : dwarf_form_name (form), yes_str);
8473 : 184996 : break;
8474 : :
8475 : 250110 : case DW_FORM_exprloc:
8476 : : case DW_FORM_block4:
8477 : : case DW_FORM_block2:
8478 : : case DW_FORM_block1:
8479 : : case DW_FORM_block:
8480 : : case DW_FORM_data16: /* DWARF5 calls this a constant class. */
8481 [ + + ]: 250110 : if (cbargs->silent)
8482 : : break;
8483 : 249848 : Dwarf_Block block;
8484 [ - + ]: 249848 : if (unlikely (dwarf_formblock (attrp, &block) != 0))
8485 : 0 : goto attrval_out;
8486 : :
8487 : 249848 : fprintf (out, " %*s%-20s (%s) ",
8488 : : (int) (level * 2), "", dwarf_attr_name (attr),
8489 : : dwarf_form_name (form));
8490 : :
8491 [ + + + ]: 249848 : switch (attr)
8492 : : {
8493 : 107216 : default:
8494 [ - + ]: 107216 : if (form != DW_FORM_exprloc)
8495 : : {
8496 : 0 : print_block (block.length, block.data, out);
8497 : 0 : break;
8498 : : }
8499 : 142624 : FALLTHROUGH;
8500 : :
8501 : : case DW_AT_location:
8502 : : case DW_AT_data_location:
8503 : : case DW_AT_data_member_location:
8504 : : case DW_AT_vtable_elem_location:
8505 : : case DW_AT_string_length:
8506 : : case DW_AT_use_location:
8507 : : case DW_AT_frame_base:
8508 : : case DW_AT_return_addr:
8509 : : case DW_AT_static_link:
8510 : : case DW_AT_allocated:
8511 : : case DW_AT_associated:
8512 : : case DW_AT_bit_size:
8513 : : case DW_AT_bit_offset:
8514 : : case DW_AT_bit_stride:
8515 : : case DW_AT_byte_size:
8516 : : case DW_AT_byte_stride:
8517 : : case DW_AT_count:
8518 : : case DW_AT_lower_bound:
8519 : : case DW_AT_upper_bound:
8520 : : case DW_AT_GNU_call_site_value:
8521 : : case DW_AT_GNU_call_site_data_value:
8522 : : case DW_AT_GNU_call_site_target:
8523 : : case DW_AT_GNU_call_site_target_clobbered:
8524 [ + + ]: 142624 : if (form == DW_FORM_exprloc
8525 [ + - ]: 140 : || (form != DW_FORM_data16
8526 [ + - ]: 140 : && attrp->cu->version < 4)) /* blocks were expressions. */
8527 : : {
8528 : 249840 : fputc ('\n', out);
8529 : 249840 : print_ops (cbargs->dwflmod, cbargs->dbg,
8530 : 249840 : 12 + level * 2, 12 + level * 2,
8531 : : cbargs->version, cbargs->addrsize, cbargs->offset_size,
8532 : 249840 : attrp->cu, block.length, block.data, out);
8533 : : }
8534 : : else
8535 : 0 : print_block (block.length, block.data, out);
8536 : : break;
8537 : :
8538 : 8 : case DW_AT_discr_list:
8539 [ - + ]: 8 : if (block.length == 0)
8540 : 0 : fputs ("<default>\n", out);
8541 [ + - ]: 8 : else if (form != DW_FORM_data16)
8542 : : {
8543 : 8 : const unsigned char *readp = block.data;
8544 : 8 : const unsigned char *readendp = readp + block.length;
8545 : :
8546 : : /* See if we are dealing with a signed or unsigned
8547 : : values. If the parent of this variant DIE is a
8548 : : variant_part then it will either have a discriminant
8549 : : which points to the member which type is the
8550 : : discriminant type. Or the variant_part itself has a
8551 : : type representing the discriminant. */
8552 : 8 : bool is_signed = false;
8553 [ + - ]: 8 : if (level > 0)
8554 : : {
8555 : 8 : Dwarf_Die *parent = &cbargs->dies[level - 1];
8556 [ + - ]: 8 : if (dwarf_tag (die) == DW_TAG_variant
8557 [ + - ]: 8 : && dwarf_tag (parent) == DW_TAG_variant_part)
8558 : : {
8559 : 8 : Dwarf_Die member;
8560 : 8 : Dwarf_Attribute discr_attr;
8561 : 8 : int bytes;
8562 [ + - ]: 8 : if (dwarf_formref_die (dwarf_attr (parent,
8563 : : DW_AT_discr,
8564 : : &discr_attr),
8565 : : &member) != NULL)
8566 : 8 : die_type_sign_bytes (&member, &is_signed, &bytes);
8567 : : else
8568 : 0 : die_type_sign_bytes (parent, &is_signed, &bytes);
8569 : : }
8570 : : }
8571 [ + + ]: 24 : while (readp < readendp)
8572 : : {
8573 : 16 : int d = (int) *readp++;
8574 : 16 : fprintf (out, "%s ", dwarf_discr_list_name (d));
8575 [ - + ]: 16 : if (readp >= readendp)
8576 : 0 : goto attrval_out;
8577 : :
8578 : 16 : Dwarf_Word val;
8579 : 16 : Dwarf_Sword sval;
8580 [ + + ]: 16 : if (d == DW_DSC_label)
8581 : : {
8582 [ + + ]: 8 : if (is_signed)
8583 : : {
8584 : 4 : get_sleb128 (sval, readp, readendp);
8585 : 4 : fprintf (out, "%" PRId64 "", sval);
8586 : : }
8587 : : else
8588 : : {
8589 : 4 : get_uleb128 (val, readp, readendp);
8590 : 4 : fprintf (out, "%" PRIu64 "", val);
8591 : : }
8592 : : }
8593 [ + - ]: 8 : else if (d == DW_DSC_range)
8594 : : {
8595 [ + + ]: 8 : if (is_signed)
8596 : : {
8597 : 6 : get_sleb128 (sval, readp, readendp);
8598 : 6 : fprintf (out, "%" PRId64 "..", sval);
8599 [ - + ]: 6 : if (readp >= readendp)
8600 : 0 : goto attrval_out;
8601 : 6 : get_sleb128 (sval, readp, readendp);
8602 : 6 : fprintf (out, "%" PRId64 "", sval);
8603 : : }
8604 : : else
8605 : : {
8606 : 2 : get_uleb128 (val, readp, readendp);
8607 : 2 : fprintf (out, "%" PRIu64 "..", val);
8608 [ - + ]: 2 : if (readp >= readendp)
8609 : 0 : goto attrval_out;
8610 : 2 : get_uleb128 (val, readp, readendp);
8611 : 2 : fprintf (out, "%" PRIu64 "", val);
8612 : : }
8613 : : }
8614 : : else
8615 : : {
8616 : 0 : print_block (readendp - readp, readp, out);
8617 : 0 : break;
8618 : : }
8619 [ + + ]: 16 : if (readp < readendp)
8620 : 8 : fprintf (out, ", ");
8621 : : }
8622 : 8 : fputc ('\n', out);
8623 : : }
8624 : : else
8625 : 0 : print_block (block.length, block.data, out);
8626 : : break;
8627 : : }
8628 : : break;
8629 : :
8630 : 0 : default:
8631 [ # # ]: 0 : if (cbargs->silent)
8632 : : break;
8633 : 0 : fprintf (out, " %*s%-20s (%s) ???\n",
8634 : : (int) (level * 2), "", dwarf_attr_name (attr),
8635 : : dwarf_form_name (form));
8636 : 0 : break;
8637 : : }
8638 : :
8639 : 8361910 : return DWARF_CB_OK;
8640 : : }
8641 : :
8642 : : static void
8643 : 208 : print_debug_units (Dwfl_Module *dwflmod,
8644 : : Ebl *ebl, GElf_Ehdr *ehdr __attribute__ ((unused)),
8645 : : Elf_Scn *scn, GElf_Shdr *shdr,
8646 : : Dwarf *dbg, bool debug_types, FILE *out)
8647 : : {
8648 [ + + + + ]: 208 : const bool silent = !(print_debug_sections & section_info) && !debug_types;
8649 : 208 : const char *secname = section_name (ebl, shdr);
8650 : :
8651 : : /* Check section actually exists. */
8652 [ + + ]: 208 : if (!silent)
8653 [ + - ]: 136 : if (get_debug_elf_data (dbg, ebl,
8654 : : debug_types ? IDX_debug_types : IDX_debug_info,
8655 : : scn) == NULL)
8656 : 0 : return;
8657 : :
8658 : 136 : if (!silent)
8659 : 136 : fprintf (out, _("\
8660 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n [Offset]\n"),
8661 : 136 : elf_ndxscn (scn), secname, (uint64_t) shdr->sh_offset);
8662 : :
8663 : : /* If the section is empty we don't have to do anything. */
8664 [ + + + - ]: 208 : if (!silent && shdr->sh_size == 0)
8665 : : return;
8666 : :
8667 : 208 : int maxdies = 20;
8668 : 208 : Dwarf_Die *dies = xmalloc (maxdies * sizeof (Dwarf_Die));
8669 : :
8670 : : /* New compilation unit. */
8671 : 208 : Dwarf_Half version;
8672 : :
8673 : 208 : Dwarf_Die result;
8674 : 208 : Dwarf_Off abbroffset;
8675 : 208 : uint8_t addrsize;
8676 : 208 : uint8_t offsize;
8677 : 208 : uint64_t unit_id;
8678 : 208 : Dwarf_Off subdie_off;
8679 : :
8680 : 208 : int unit_res;
8681 : 208 : Dwarf_CU *cu;
8682 : 208 : Dwarf_CU cu_mem;
8683 : 208 : uint8_t unit_type;
8684 : 208 : Dwarf_Die cudie;
8685 : :
8686 : : /* We cheat a little because we want to see only the CUs from .debug_info
8687 : : or .debug_types. We know the Dwarf_CU struct layout. Set it up at
8688 : : the end of .debug_info if we want .debug_types only. Check the returned
8689 : : Dwarf_CU is still in the expected section. */
8690 [ + + ]: 208 : if (debug_types)
8691 : : {
8692 : 2 : cu_mem.dbg = dbg;
8693 : 2 : cu_mem.end = dbg->sectiondata[IDX_debug_info]->d_size;
8694 : 2 : cu_mem.sec_idx = IDX_debug_info;
8695 : 2 : cu = &cu_mem;
8696 : : }
8697 : : else
8698 : 206 : cu = NULL;
8699 : :
8700 : : next_cu:
8701 : 4466 : unit_res = dwarf_get_units (dbg, cu, &cu, &version, &unit_type,
8702 : : &cudie, NULL);
8703 [ + + ]: 4466 : if (unit_res == 1)
8704 : 206 : goto do_return;
8705 : :
8706 [ - + ]: 4260 : if (unit_res == -1)
8707 : : {
8708 [ # # ]: 0 : if (!silent)
8709 : 0 : error (0, 0, _("cannot get next unit: %s"), dwarf_errmsg (-1));
8710 : 0 : goto do_return;
8711 : : }
8712 : :
8713 [ + + + + ]: 8516 : if (cu->sec_idx != (size_t) (debug_types ? IDX_debug_types : IDX_debug_info))
8714 : 2 : goto do_return;
8715 : :
8716 : 4258 : dwarf_cu_die (cu, &result, NULL, &abbroffset, &addrsize, &offsize,
8717 : : &unit_id, &subdie_off);
8718 : :
8719 [ + + ]: 4258 : if (!silent)
8720 : : {
8721 : 4158 : Dwarf_Off offset = cu->start;
8722 [ + + + - ]: 4158 : if (debug_types && version < 5)
8723 : 4 : {
8724 : 4 : Dwarf_Die typedie;
8725 : 4 : Dwarf_Off dieoffset;
8726 : 4 : dieoffset = dwarf_dieoffset (dwarf_offdie_types (dbg, cu->start
8727 : : + subdie_off,
8728 : : &typedie));
8729 : 4 : fprintf (out, " %s %" PRIu64 ":\n"
8730 : : " %s: %" PRIu16
8731 : : ", %s: %" PRIu64
8732 : : ", %s: %" PRIu8
8733 : : ", %s: %" PRIu8
8734 : : "\n %s: %#" PRIx64
8735 : : ", %s: %#" PRIx64 " [%" PRIx64 "]\n",
8736 : : type_unit_str, (uint64_t) offset, version_str, version,
8737 : : abbrev_offset_str, abbroffset, addr_size_str, addrsize,
8738 : : offset_size_str, offsize, type_sig_str, unit_id,
8739 : : type_offset_str, (uint64_t) subdie_off, dieoffset);
8740 : : }
8741 : : else
8742 : : {
8743 : 4154 : fprintf (out, " %s %" PRIu64 ":\n"
8744 : : " %s: %" PRIu16
8745 : : ", %s: %" PRIu64
8746 : : ", %s: %" PRIu8
8747 : : ", %s: %" PRIu8 "\n",
8748 : : cu_offset_at_str, (uint64_t) offset, version_str, version,
8749 : : abbrev_offset_str, abbroffset, addr_size_str, addrsize,
8750 : : offset_size_str, offsize);
8751 : :
8752 [ + + ]: 4154 : if (version >= 5 || (unit_type != DW_UT_compile
8753 [ + + ]: 170 : && unit_type != DW_UT_partial))
8754 : : {
8755 : 3986 : fprintf (out, " %s: %s (%" PRIu8 ")",
8756 : : unit_type_str, dwarf_unit_name (unit_type), unit_type);
8757 : 3986 : if (unit_type == DW_UT_type
8758 [ + + ]: 3986 : || unit_type == DW_UT_skeleton
8759 [ + - ]: 3976 : || unit_type == DW_UT_split_compile
8760 [ - + ]: 3976 : || unit_type == DW_UT_split_type)
8761 : 10 : fprintf (out, ", %s: 0x%.16" PRIx64 "", unit_id_str, unit_id);
8762 : 3986 : if (unit_type == DW_UT_type
8763 [ - + ]: 3986 : || unit_type == DW_UT_split_type)
8764 : : {
8765 : 0 : Dwarf_Die typedie;
8766 : 0 : Dwarf_Off dieoffset;
8767 : 0 : dwarf_cu_info (cu, NULL, NULL, NULL, &typedie,
8768 : : NULL, NULL, NULL);
8769 : 0 : dieoffset = dwarf_dieoffset (&typedie);
8770 : 0 : fprintf (out, ", %s: %#" PRIx64 " [%" PRIx64 "]",
8771 : : unit_die_off_str, subdie_off, dieoffset);
8772 : : }
8773 : 3986 : fprintf (out, "\n");
8774 : : }
8775 : : }
8776 : : }
8777 : :
8778 [ + - ]: 4258 : if (version < 2 || version > 5
8779 [ + - - + ]: 4258 : || unit_type < DW_UT_compile || unit_type > DW_UT_split_type)
8780 : : {
8781 [ # # ]: 0 : if (!silent)
8782 : 0 : error (0, 0, _("unknown version (%d) or unit type (%d)"),
8783 : : version, unit_type);
8784 : 0 : goto next_cu;
8785 : : }
8786 : :
8787 : 4258 : struct attrcb_args args =
8788 : : {
8789 : : .dwflmod = dwflmod,
8790 : : .silent = silent,
8791 : : .version = version,
8792 : : .addrsize = addrsize,
8793 : : .offset_size = offsize
8794 : : };
8795 : :
8796 : 4258 : bool is_split = false;
8797 : 4258 : int level = 0;
8798 : 4258 : dies[0] = cudie;
8799 : 4258 : args.cu = dies[0].cu;
8800 : 4258 : args.dbg = dbg;
8801 : 4258 : args.is_split = is_split;
8802 : 4258 : args.out = out;
8803 : :
8804 : : /* We might return here again for the split CU subdie. */
8805 : : do_cu:
8806 : 2452534 : do
8807 : : {
8808 : 2452534 : Dwarf_Off offset = dwarf_dieoffset (&dies[level]);
8809 [ - + ]: 2452534 : if (unlikely (offset == (Dwarf_Off) -1))
8810 : : {
8811 [ # # ]: 0 : if (!silent)
8812 : 0 : error (0, 0, _("cannot get DIE offset: %s"),
8813 : : dwarf_errmsg (-1));
8814 : 0 : goto do_return;
8815 : : }
8816 : :
8817 : 2452534 : int tag = dwarf_tag (&dies[level]);
8818 [ - + ]: 2452534 : if (unlikely (tag == DW_TAG_invalid))
8819 : : {
8820 [ # # ]: 0 : if (!silent)
8821 : 0 : error (0, 0, _("cannot get tag of DIE at offset [%" PRIx64
8822 : : "] in section '%s': %s"),
8823 : : (uint64_t) offset, secname, dwarf_errmsg (-1));
8824 : 0 : goto do_return;
8825 : : }
8826 : :
8827 [ + + ]: 2452534 : if (!silent)
8828 : : {
8829 : 2450548 : unsigned int code = dwarf_getabbrevcode (dies[level].abbrev);
8830 [ + + ]: 2450548 : if (is_split)
8831 : 96 : fprintf (out, " {%6" PRIx64 "} ", (uint64_t) offset);
8832 : : else
8833 : 2450452 : fprintf (out, " [%6" PRIx64 "] ", (uint64_t) offset);
8834 : 2450548 : fprintf (out, "%*s%-20s abbrev: %u\n", (int) (level * 2), "",
8835 : : dwarf_tag_name (tag), code);
8836 : : }
8837 : :
8838 : : /* Print the attribute values. */
8839 : 2452534 : args.level = level;
8840 : 2452534 : args.dies = dies;
8841 : 2452534 : (void) dwarf_getattrs (&dies[level], attr_callback, &args, 0);
8842 : :
8843 : : /* Make room for the next level's DIE. */
8844 [ - + ]: 2452534 : if (level + 1 == maxdies)
8845 : 0 : dies = xrealloc (dies, (maxdies += 10) * sizeof (Dwarf_Die));
8846 : :
8847 : 2452534 : int res = dwarf_child (&dies[level], &dies[level + 1]);
8848 [ + + ]: 2452534 : if (res > 0)
8849 : : {
8850 [ + + ]: 2452534 : while ((res = dwarf_siblingof (&dies[level], &dies[level])) == 1)
8851 [ + + ]: 336866 : if (level-- == 0)
8852 : : break;
8853 : :
8854 [ - + ]: 2119934 : if (unlikely (res == -1))
8855 : : {
8856 [ # # ]: 0 : if (!silent)
8857 : 0 : error (0, 0, _("cannot get next DIE: %s\n"),
8858 : : dwarf_errmsg (-1));
8859 : 0 : goto do_return;
8860 : : }
8861 : : }
8862 [ - + ]: 332600 : else if (unlikely (res < 0))
8863 : : {
8864 [ # # ]: 0 : if (!silent)
8865 : 0 : error (0, 0, _("cannot get next DIE: %s"),
8866 : : dwarf_errmsg (-1));
8867 : 0 : goto do_return;
8868 : : }
8869 : : else
8870 : : ++level;
8871 : : }
8872 [ + + ]: 2452534 : while (level >= 0);
8873 : :
8874 : : /* We might want to show the split compile unit if this was a skeleton.
8875 : : We need to scan it if we are requesting printing .debug_ranges for
8876 : : DWARF4 since GNU DebugFission uses "offsets" into the main ranges
8877 : : section. */
8878 [ + + ]: 4266 : if (unit_type == DW_UT_skeleton
8879 [ + + + + ]: 26 : && ((!silent && show_split_units)
8880 [ + + + + ]: 20 : || (version < 5 && (print_debug_sections & section_ranges) != 0)))
8881 : : {
8882 : 10 : Dwarf_Die subdie;
8883 [ + - ]: 10 : if (dwarf_cu_info (cu, NULL, NULL, NULL, &subdie, NULL, NULL, NULL) != 0
8884 [ + + ]: 10 : || dwarf_tag (&subdie) == DW_TAG_invalid)
8885 : : {
8886 [ + - ]: 2 : if (!silent)
8887 : : {
8888 : 2 : Dwarf_Attribute dwo_at;
8889 : 4 : const char *dwo_name =
8890 : 2 : (dwarf_formstring (dwarf_attr (&cudie, DW_AT_dwo_name,
8891 : : &dwo_at))
8892 [ - + ]: 2 : ?: (dwarf_formstring (dwarf_attr (&cudie, DW_AT_GNU_dwo_name,
8893 : : &dwo_at))
8894 [ # # ]: 0 : ?: "<unknown>"));
8895 : 2 : fprintf (stderr,
8896 : : "Could not find split unit '%s', id: %" PRIx64 "\n",
8897 : : dwo_name, unit_id);
8898 : : }
8899 : : }
8900 : : else
8901 : : {
8902 : 8 : Dwarf_CU *split_cu = subdie.cu;
8903 : 8 : Dwarf_Half split_version; /* Should be the same version as skel. */
8904 : 8 : dwarf_cu_die (split_cu, &result, &split_version, &abbroffset,
8905 : : &addrsize, &offsize, &unit_id, &subdie_off);
8906 : 8 : Dwarf_Off offset = cu->start;
8907 : 8 : uint8_t split_unit_type; /* Should be DW_UT_split_compile. */
8908 : 8 : uint64_t split_unit_id; /* Should be the same as id. */
8909 [ - + ]: 8 : if (dwarf_cu_info (split_cu, NULL, &split_unit_type, NULL, NULL,
8910 : : &split_unit_id, NULL, NULL) != 0)
8911 : : {
8912 : : /* Really shouldn't happen unless split_cu is NULL. */
8913 : 0 : split_unit_type = DW_UT_split_compile;
8914 : 0 : split_unit_id = unit_id;
8915 : : }
8916 : :
8917 [ + + ]: 8 : if (!silent)
8918 : : {
8919 : 4 : fprintf (out, _(" Split compilation unit at offset %"
8920 : : PRIu64 ":\n"
8921 : : " Version: %" PRIu16
8922 : : ", Abbreviation section offset: %" PRIu64
8923 : : ", Address size: %" PRIu8
8924 : : ", Offset size: %" PRIu8 "\n"),
8925 : : (uint64_t) offset, split_version, abbroffset,
8926 : : addrsize, offsize);
8927 : 4 : fprintf (out, _(" Unit type: %s (%" PRIu8 ")"),
8928 : : dwarf_unit_name (split_unit_type), split_unit_type);
8929 : 4 : fprintf (out, ", Unit id: 0x%.16" PRIx64 "", split_unit_id);
8930 : 4 : fprintf (out, "\n");
8931 : : }
8932 : :
8933 : 8 : unit_type = split_unit_type;
8934 : 8 : is_split = true;
8935 : 8 : level = 0;
8936 : 8 : dies[0] = subdie;
8937 : 8 : args.cu = dies[0].cu;
8938 : 8 : args.dbg = split_cu->dbg;
8939 : 8 : args.is_split = is_split;
8940 : 8 : goto do_cu;
8941 : : }
8942 : : }
8943 : :
8944 : : /* And again... */
8945 : 4258 : goto next_cu;
8946 : :
8947 : 208 : do_return:
8948 : 208 : free (dies);
8949 : : }
8950 : :
8951 : : static void
8952 : 206 : print_debug_info_section (Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr,
8953 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg,
8954 : : FILE *out)
8955 : : {
8956 : 42 : print_debug_units (dwflmod, ebl, ehdr, scn, shdr, dbg, false, out);
8957 : 42 : }
8958 : :
8959 : : static void
8960 : 2 : print_debug_types_section (Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr,
8961 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg,
8962 : : FILE *out)
8963 : : {
8964 : 2 : print_debug_units (dwflmod, ebl, ehdr, scn, shdr, dbg, true, out);
8965 : 2 : }
8966 : :
8967 : :
8968 : : static void
8969 : 82 : print_decoded_line_section (Dwfl_Module *dwflmod, Ebl *ebl,
8970 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
8971 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg,
8972 : : FILE *out)
8973 : : {
8974 : 82 : fprintf (out, _("\
8975 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n\n"),
8976 : : elf_ndxscn (scn), section_name (ebl, shdr),
8977 : 82 : (uint64_t) shdr->sh_offset);
8978 : :
8979 : 164 : size_t address_size
8980 [ + + ]: 82 : = elf_getident (ebl->elf, NULL)[EI_CLASS] == ELFCLASS32 ? 4 : 8;
8981 : :
8982 : 82 : Dwarf_Lines *lines;
8983 : 82 : size_t nlines;
8984 : 82 : Dwarf_Off off, next_off = 0;
8985 : 82 : Dwarf_CU *cu = NULL;
8986 : 82 : while (dwarf_next_lines (dbg, off = next_off, &next_off, &cu, NULL, NULL,
8987 [ + + ]: 176 : &lines, &nlines) == 0)
8988 : : {
8989 : 94 : Dwarf_Die cudie;
8990 [ + + + - ]: 94 : if (cu != NULL && dwarf_cu_info (cu, NULL, NULL, &cudie,
8991 : : NULL, NULL, NULL, NULL) == 0)
8992 : 50 : fprintf (out, " CU [%" PRIx64 "] %s\n",
8993 : : dwarf_dieoffset (&cudie), dwarf_diename (&cudie));
8994 : : else
8995 : : {
8996 : : /* DWARF5 lines can be independent of any CU, but they probably
8997 : : are used by some CU. Determine the CU this block is for. */
8998 : 44 : Dwarf_Off cuoffset;
8999 : 44 : Dwarf_Off ncuoffset = 0;
9000 : 44 : size_t hsize;
9001 : 44 : while (dwarf_nextcu (dbg, cuoffset = ncuoffset, &ncuoffset, &hsize,
9002 [ + - ]: 46 : NULL, NULL, NULL) == 0)
9003 : : {
9004 [ - + ]: 46 : if (dwarf_offdie (dbg, cuoffset + hsize, &cudie) == NULL)
9005 : 0 : continue;
9006 : 46 : Dwarf_Attribute stmt_list;
9007 [ - + ]: 46 : if (dwarf_attr (&cudie, DW_AT_stmt_list, &stmt_list) == NULL)
9008 : 0 : continue;
9009 : 46 : Dwarf_Word lineoff;
9010 [ - + ]: 46 : if (dwarf_formudata (&stmt_list, &lineoff) != 0)
9011 : 0 : continue;
9012 [ + + ]: 46 : if (lineoff == off)
9013 : : {
9014 : : /* Found the CU. */
9015 : 44 : cu = cudie.cu;
9016 : 44 : break;
9017 : : }
9018 : : }
9019 : :
9020 [ + - ]: 44 : if (cu != NULL)
9021 : 44 : fprintf (out, " CU [%" PRIx64 "] %s\n",
9022 : : dwarf_dieoffset (&cudie), dwarf_diename (&cudie));
9023 : : else
9024 : 0 : fprintf (out, " No CU\n");
9025 : : }
9026 : :
9027 : 94 : fprintf (out, " line:col SBPE* disc isa op address"
9028 : : " (Statement Block Prologue Epilogue *End)\n");
9029 : 94 : const char *last_file = "";
9030 [ + + ]: 720 : for (size_t n = 0; n < nlines; n++)
9031 : : {
9032 : 626 : Dwarf_Line *line = dwarf_onesrcline (lines, n);
9033 [ - + ]: 626 : if (line == NULL)
9034 : : {
9035 : 0 : fprintf (out, " dwarf_onesrcline: %s\n", dwarf_errmsg (-1));
9036 : 0 : continue;
9037 : : }
9038 : 626 : Dwarf_Word mtime, length;
9039 : 626 : const char *file = dwarf_linesrc (line, &mtime, &length);
9040 [ - + ]: 626 : if (file == NULL)
9041 : : {
9042 : 0 : fprintf (out, " <%s> (mtime: ?, length: ?)\n",
9043 : : dwarf_errmsg (-1));
9044 : 0 : last_file = "";
9045 : : }
9046 [ + + ]: 626 : else if (strcmp (last_file, file) != 0)
9047 : : {
9048 : 114 : fprintf (out, " %s (mtime: %" PRIu64 ", length: %" PRIu64 ")\n",
9049 : : file, mtime, length);
9050 : 114 : last_file = file;
9051 : : }
9052 : :
9053 : 626 : int lineno, colno;
9054 : 626 : bool statement, endseq, block, prologue_end, epilogue_begin;
9055 : 626 : unsigned int lineop, isa, disc;
9056 : 626 : Dwarf_Addr address;
9057 : 626 : dwarf_lineaddr (line, &address);
9058 : 626 : dwarf_lineno (line, &lineno);
9059 : 626 : dwarf_linecol (line, &colno);
9060 : 626 : dwarf_lineop_index (line, &lineop);
9061 : 626 : dwarf_linebeginstatement (line, &statement);
9062 : 626 : dwarf_lineendsequence (line, &endseq);
9063 : 626 : dwarf_lineblock (line, &block);
9064 : 626 : dwarf_lineprologueend (line, &prologue_end);
9065 : 626 : dwarf_lineepiloguebegin (line, &epilogue_begin);
9066 : 626 : dwarf_lineisa (line, &isa);
9067 : 626 : dwarf_linediscriminator (line, &disc);
9068 : :
9069 : : /* End sequence is special, it is one byte past. */
9070 : 626 : fprintf (out, " %4d:%-3d %c%c%c%c%c %4d %3d %2d ",
9071 : : lineno, colno,
9072 [ + + ]: 626 : (statement ? 'S' : ' '),
9073 [ + - ]: 626 : (block ? 'B' : ' '),
9074 [ + + ]: 626 : (prologue_end ? 'P' : ' '),
9075 [ + - ]: 626 : (epilogue_begin ? 'E' : ' '),
9076 [ + + ]: 626 : (endseq ? '*' : ' '),
9077 : : disc, isa, lineop);
9078 : 626 : print_dwarf_addr (dwflmod, address_size,
9079 [ + + ]: 626 : address - (endseq ? 1 : 0), address, out);
9080 : 626 : fprintf (out, "\n");
9081 : :
9082 [ + + ]: 626 : if (endseq)
9083 : 98 : fprintf(out, "\n");
9084 : : }
9085 : : }
9086 : 82 : }
9087 : :
9088 : :
9089 : : /* Print the value of a form.
9090 : : Returns new value of readp, or readendp on failure. */
9091 : : static const unsigned char *
9092 : 165252 : print_form_data (Dwarf *dbg, int form, const unsigned char *readp,
9093 : : const unsigned char *readendp, unsigned int offset_len,
9094 : : Dwarf_Off str_offsets_base, FILE *out)
9095 : : {
9096 : 165252 : Dwarf_Word val;
9097 : 165252 : const unsigned char *endp;
9098 : 165252 : Elf_Data *data;
9099 : 165252 : char *str;
9100 [ + - - - : 165252 : switch (form)
- + - - -
- + - - +
- + - - -
- - ]
9101 : : {
9102 : 16 : case DW_FORM_data1:
9103 [ - + ]: 16 : if (readendp - readp < 1)
9104 : : {
9105 : 0 : invalid_data:
9106 : 0 : error (0, 0, "invalid data");
9107 : 0 : return readendp;
9108 : : }
9109 : 16 : val = *readp++;
9110 : 16 : fprintf (out, " %" PRIx8, (unsigned int) val);
9111 : 16 : break;
9112 : :
9113 : 0 : case DW_FORM_data2:
9114 [ # # ]: 0 : if (readendp - readp < 2)
9115 : 0 : goto invalid_data;
9116 [ # # ]: 0 : val = read_2ubyte_unaligned_inc (dbg, readp);
9117 : 0 : fprintf(out, " %" PRIx16, (unsigned int) val);
9118 : 0 : break;
9119 : :
9120 : 0 : case DW_FORM_data4:
9121 [ # # ]: 0 : if (readendp - readp < 4)
9122 : 0 : goto invalid_data;
9123 [ # # ]: 0 : val = read_4ubyte_unaligned_inc (dbg, readp);
9124 : 0 : fprintf (out, " %" PRIx32, (unsigned int) val);
9125 : 0 : break;
9126 : :
9127 : 0 : case DW_FORM_data8:
9128 [ # # ]: 0 : if (readendp - readp < 8)
9129 : 0 : goto invalid_data;
9130 [ # # ]: 0 : val = read_8ubyte_unaligned_inc (dbg, readp);
9131 : 0 : fprintf (out, " %" PRIx64, val);
9132 : 0 : break;
9133 : :
9134 : 0 : case DW_FORM_sdata:
9135 [ # # ]: 0 : if (readendp - readp < 1)
9136 : 0 : goto invalid_data;
9137 : 0 : get_sleb128 (val, readp, readendp);
9138 : 0 : fprintf (out, " %" PRIx64, val);
9139 : 0 : break;
9140 : :
9141 : 66666 : case DW_FORM_udata:
9142 [ - + ]: 66666 : if (readendp - readp < 1)
9143 : 0 : goto invalid_data;
9144 : 66666 : get_uleb128 (val, readp, readendp);
9145 : 66666 : fprintf (out, " %" PRIx64, val);
9146 : 66666 : break;
9147 : :
9148 : 0 : case DW_FORM_block:
9149 [ # # ]: 0 : if (readendp - readp < 1)
9150 : 0 : goto invalid_data;
9151 : 0 : get_uleb128 (val, readp, readendp);
9152 [ # # ]: 0 : if ((size_t) (readendp - readp) < val)
9153 : 0 : goto invalid_data;
9154 : 0 : print_bytes (val, readp, out);
9155 : 0 : readp += val;
9156 : 0 : break;
9157 : :
9158 : 0 : case DW_FORM_block1:
9159 [ # # ]: 0 : if (readendp - readp < 1)
9160 : 0 : goto invalid_data;
9161 : 0 : val = *readp++;
9162 [ # # ]: 0 : if ((size_t) (readendp - readp) < val)
9163 : 0 : goto invalid_data;
9164 : 0 : print_bytes (val, readp, out);
9165 : 0 : readp += val;
9166 : 0 : break;
9167 : :
9168 : 0 : case DW_FORM_block2:
9169 [ # # ]: 0 : if (readendp - readp < 2)
9170 : 0 : goto invalid_data;
9171 [ # # ]: 0 : val = read_2ubyte_unaligned_inc (dbg, readp);
9172 [ # # ]: 0 : if ((size_t) (readendp - readp) < val)
9173 : 0 : goto invalid_data;
9174 : 0 : print_bytes (val, readp, out);
9175 : 0 : readp += val;
9176 : 0 : break;
9177 : :
9178 : 0 : case DW_FORM_block4:
9179 [ # # ]: 0 : if (readendp - readp < 4)
9180 : 0 : goto invalid_data;
9181 [ # # ]: 0 : val = read_4ubyte_unaligned_inc (dbg, readp);
9182 [ # # ]: 0 : if ((size_t) (readendp - readp) < val)
9183 : 0 : goto invalid_data;
9184 : 0 : print_bytes (val, readp, out);
9185 : 0 : readp += val;
9186 : 0 : break;
9187 : :
9188 : 24 : case DW_FORM_data16:
9189 [ - + ]: 24 : if (readendp - readp < 16)
9190 : 0 : goto invalid_data;
9191 : 24 : print_bytes (16, readp, out);
9192 : 24 : readp += 16;
9193 : 24 : break;
9194 : :
9195 : 0 : case DW_FORM_flag:
9196 [ # # ]: 0 : if (readendp - readp < 1)
9197 : 0 : goto invalid_data;
9198 : 0 : val = *readp++;
9199 [ # # ]: 0 : fprintf (out, "%s", val != 0 ? yes_str : no_str);
9200 : 0 : break;
9201 : :
9202 : 0 : case DW_FORM_string:
9203 : 0 : endp = memchr (readp, '\0', readendp - readp);
9204 [ # # ]: 0 : if (endp == NULL)
9205 : 0 : goto invalid_data;
9206 : 0 : fprintf (out, "%s", readp);
9207 : 0 : readp = endp + 1;
9208 : 0 : break;
9209 : :
9210 : 96990 : case DW_FORM_strp:
9211 : : case DW_FORM_line_strp:
9212 : : case DW_FORM_strp_sup:
9213 [ - + ]: 96990 : if ((size_t) (readendp - readp) < offset_len)
9214 : 0 : goto invalid_data;
9215 [ - + ]: 96990 : if (offset_len == 8)
9216 [ # # ]: 0 : val = read_8ubyte_unaligned_inc (dbg, readp);
9217 : : else
9218 [ - + ]: 96990 : val = read_4ubyte_unaligned_inc (dbg, readp);
9219 [ - + ]: 96990 : if (form == DW_FORM_strp)
9220 : 0 : data = dbg->sectiondata[IDX_debug_str];
9221 [ + - ]: 96990 : else if (form == DW_FORM_line_strp)
9222 : 96990 : data = dbg->sectiondata[IDX_debug_line_str];
9223 : : else /* form == DW_FORM_strp_sup */
9224 : : {
9225 : 0 : Dwarf *alt = dwarf_getalt (dbg);
9226 [ # # ]: 0 : data = alt != NULL ? alt->sectiondata[IDX_debug_str] : NULL;
9227 : : }
9228 [ + - - + ]: 96990 : if (data == NULL || val >= data->d_size
9229 [ - + ]: 96990 : || memchr (data->d_buf + val, '\0', data->d_size - val) == NULL)
9230 : : str = "???";
9231 : : else
9232 : 96990 : str = (char *) data->d_buf + val;
9233 : 96990 : fprintf (out, "%s (%" PRIu64 ")", str, val);
9234 : 96990 : break;
9235 : :
9236 : 0 : case DW_FORM_sec_offset:
9237 [ # # ]: 0 : if ((size_t) (readendp - readp) < offset_len)
9238 : 0 : goto invalid_data;
9239 [ # # ]: 0 : if (offset_len == 8)
9240 [ # # ]: 0 : val = read_8ubyte_unaligned_inc (dbg, readp);
9241 : : else
9242 [ # # ]: 0 : val = read_4ubyte_unaligned_inc (dbg, readp);
9243 : 0 : fprintf (out, "[%" PRIx64 "]", val);
9244 : 0 : break;
9245 : :
9246 : 1556 : case DW_FORM_strx:
9247 : : case DW_FORM_GNU_str_index:
9248 [ - + ]: 1556 : if (readendp - readp < 1)
9249 : 0 : goto invalid_data;
9250 : 1556 : get_uleb128 (val, readp, readendp);
9251 : 1556 : strx_val:
9252 : 1556 : data = dbg->sectiondata[IDX_debug_str_offsets];
9253 [ + - ]: 1556 : if (data == NULL
9254 [ - + ]: 1556 : || data->d_size - str_offsets_base < val * offset_len)
9255 : : str = "???";
9256 : : else
9257 : : {
9258 : 1556 : const unsigned char *strreadp = (data->d_buf + str_offsets_base
9259 : 1556 : + val * offset_len);
9260 : 1556 : const unsigned char *strreadendp = data->d_buf + data->d_size;
9261 [ - + ]: 1556 : if ((size_t) (strreadendp - strreadp) < offset_len)
9262 : : str = "???";
9263 : : else
9264 : : {
9265 : 1556 : Dwarf_Off idx;
9266 [ - + ]: 1556 : if (offset_len == 8)
9267 [ # # ]: 0 : idx = read_8ubyte_unaligned (dbg, strreadp);
9268 : : else
9269 [ - + ]: 1556 : idx = read_4ubyte_unaligned (dbg, strreadp);
9270 : :
9271 : 1556 : data = dbg->sectiondata[IDX_debug_str];
9272 [ - + - + ]: 1556 : if (data == NULL || idx >= data->d_size
9273 [ - + ]: 1556 : || memchr (data->d_buf + idx, '\0',
9274 : : data->d_size - idx) == NULL)
9275 : : str = "???";
9276 : : else
9277 : 1556 : str = (char *) data->d_buf + idx;
9278 : : }
9279 : : }
9280 : 1556 : fprintf (out, "%s (%" PRIu64 ")", str, val);
9281 : 1556 : break;
9282 : :
9283 : 0 : case DW_FORM_strx1:
9284 [ # # ]: 0 : if (readendp - readp < 1)
9285 : 0 : goto invalid_data;
9286 : 0 : val = *readp++;
9287 : 0 : goto strx_val;
9288 : :
9289 : 0 : case DW_FORM_strx2:
9290 [ # # ]: 0 : if (readendp - readp < 2)
9291 : 0 : goto invalid_data;
9292 [ # # ]: 0 : val = read_2ubyte_unaligned_inc (dbg, readp);
9293 : 0 : goto strx_val;
9294 : :
9295 : 0 : case DW_FORM_strx3:
9296 [ # # ]: 0 : if (readendp - readp < 3)
9297 : 0 : goto invalid_data;
9298 : 0 : val = read_3ubyte_unaligned_inc (dbg, readp);
9299 : 0 : goto strx_val;
9300 : :
9301 : 0 : case DW_FORM_strx4:
9302 [ # # ]: 0 : if (readendp - readp < 4)
9303 : 0 : goto invalid_data;
9304 [ # # ]: 0 : val = read_4ubyte_unaligned_inc (dbg, readp);
9305 : 0 : goto strx_val;
9306 : :
9307 : 0 : default:
9308 : 0 : error (0, 0, _("unknown form: %s"), dwarf_form_name (form));
9309 : 0 : return readendp;
9310 : : }
9311 : :
9312 : 165252 : return readp;
9313 : : }
9314 : :
9315 : : /* Only used via run_advance_pc() macro */
9316 : : static inline void
9317 : 892828 : run_advance_pc (unsigned int op_advance,
9318 : : unsigned int minimum_instr_len,
9319 : : unsigned int max_ops_per_instr,
9320 : : unsigned int *op_addr_advance,
9321 : : Dwarf_Word *address,
9322 : : unsigned int *op_index)
9323 : : {
9324 : 892828 : const unsigned int advanced_op_index = (*op_index) + op_advance;
9325 : :
9326 : 892828 : *op_addr_advance = minimum_instr_len * (advanced_op_index
9327 : 892828 : / max_ops_per_instr);
9328 : 892828 : *address = *address + *op_addr_advance;
9329 : 892828 : *op_index = advanced_op_index % max_ops_per_instr;
9330 : : }
9331 : :
9332 : : static void
9333 : 186 : print_debug_line_section (Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr,
9334 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg,
9335 : : FILE *out)
9336 : : {
9337 [ + + ]: 186 : if (decodedline)
9338 : : {
9339 : 82 : print_decoded_line_section (dwflmod, ebl, ehdr, scn, shdr, dbg, out);
9340 : 164 : return;
9341 : : }
9342 : :
9343 : 104 : Elf_Data *data = get_debug_elf_data (dbg, ebl, IDX_debug_line, scn);
9344 [ + - ]: 104 : if (data == NULL)
9345 : : return;
9346 : :
9347 : 104 : fprintf (out, _("\
9348 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
9349 : : elf_ndxscn (scn), section_name (ebl, shdr),
9350 : 104 : (uint64_t) shdr->sh_offset);
9351 : :
9352 [ + - ]: 104 : if (shdr->sh_size == 0)
9353 : : return;
9354 : :
9355 : : /* There is no functionality in libdw to read the information in the
9356 : : way it is represented here. Hardcode the decoder. */
9357 : :
9358 : 104 : const unsigned char *linep = (const unsigned char *) data->d_buf;
9359 : 104 : const unsigned char *lineendp;
9360 : :
9361 : 104 : while (linep
9362 [ + + ]: 4230 : < (lineendp = (const unsigned char *) data->d_buf + data->d_size))
9363 : : {
9364 : 4126 : size_t start_offset = linep - (const unsigned char *) data->d_buf;
9365 : :
9366 : 4126 : fprintf (out, "\n%s %zu:\n",
9367 : : table_offset_lower_str, start_offset);
9368 : :
9369 [ - + ]: 4126 : if (unlikely (linep + 4 > lineendp))
9370 : 0 : goto invalid_data;
9371 [ + + ]: 4126 : Dwarf_Word unit_length = read_4ubyte_unaligned_inc (dbg, linep);
9372 : 4126 : unsigned int length = 4;
9373 [ - + ]: 4126 : if (unlikely (unit_length == 0xffffffff))
9374 : : {
9375 [ # # ]: 0 : if (unlikely (linep + 8 > lineendp))
9376 : : {
9377 : 0 : invalid_data:
9378 : 0 : error (0, 0, _("invalid data in section [%zu] '%s'"),
9379 : : elf_ndxscn (scn), section_name (ebl, shdr));
9380 : 0 : return;
9381 : : }
9382 [ # # ]: 0 : unit_length = read_8ubyte_unaligned_inc (dbg, linep);
9383 : 0 : length = 8;
9384 : : }
9385 : :
9386 : : /* Check whether we have enough room in the section. */
9387 [ - + ]: 4126 : if (unlikely (unit_length > (size_t) (lineendp - linep)))
9388 : 0 : goto invalid_data;
9389 : 4126 : lineendp = linep + unit_length;
9390 : :
9391 : : /* The next element of the header is the version identifier. */
9392 [ - + ]: 4126 : if ((size_t) (lineendp - linep) < 2)
9393 : 0 : goto invalid_data;
9394 [ + + ]: 4126 : uint_fast16_t version = read_2ubyte_unaligned_inc (dbg, linep);
9395 : :
9396 : 8252 : size_t address_size
9397 [ + + ]: 4126 : = elf_getident (ebl->elf, NULL)[EI_CLASS] == ELFCLASS32 ? 4 : 8;
9398 : 4126 : unsigned char segment_selector_size = 0;
9399 [ + + ]: 4126 : if (version > 4)
9400 : : {
9401 [ - + ]: 3974 : if ((size_t) (lineendp - linep) < 2)
9402 : 0 : goto invalid_data;
9403 : 3974 : address_size = *linep++;
9404 : 3974 : segment_selector_size = *linep++;
9405 : : }
9406 : :
9407 : : /* Next comes the header length. */
9408 : 4126 : Dwarf_Word header_length;
9409 [ + - ]: 4126 : if (length == 4)
9410 : : {
9411 [ - + ]: 4126 : if ((size_t) (lineendp - linep) < 4)
9412 : 0 : goto invalid_data;
9413 [ + + ]: 4126 : header_length = read_4ubyte_unaligned_inc (dbg, linep);
9414 : : }
9415 : : else
9416 : : {
9417 [ # # ]: 0 : if ((size_t) (lineendp - linep) < 8)
9418 : 0 : goto invalid_data;
9419 [ # # ]: 0 : header_length = read_8ubyte_unaligned_inc (dbg, linep);
9420 : : }
9421 : :
9422 : 4126 : const unsigned char *header_start = linep;
9423 : :
9424 : : /* Next the minimum instruction length. */
9425 [ - + ]: 4126 : if ((size_t) (lineendp - linep) < 1)
9426 : 0 : goto invalid_data;
9427 : 4126 : uint_fast8_t minimum_instr_len = *linep++;
9428 : :
9429 : : /* Next the maximum operations per instruction, in version 4 format. */
9430 : 4126 : uint_fast8_t max_ops_per_instr;
9431 [ + + ]: 4126 : if (version < 4)
9432 : : max_ops_per_instr = 1;
9433 : : else
9434 : : {
9435 [ - + ]: 3994 : if ((size_t) (lineendp - linep) < 1)
9436 : 0 : goto invalid_data;
9437 : 3994 : max_ops_per_instr = *linep++;
9438 : : }
9439 : :
9440 : : /* We need at least 4 more bytes. */
9441 [ - + ]: 4126 : if ((size_t) (lineendp - linep) < 4)
9442 : 0 : goto invalid_data;
9443 : :
9444 : : /* Then the flag determining the default value of the is_stmt
9445 : : register. */
9446 : 4126 : uint_fast8_t default_is_stmt = *linep++;
9447 : :
9448 : : /* Now the line base. */
9449 : 4126 : int_fast8_t line_base = *linep++;
9450 : :
9451 : : /* And the line range. */
9452 : 4126 : uint_fast8_t line_range = *linep++;
9453 : :
9454 : : /* The opcode base. */
9455 : 4126 : uint_fast8_t opcode_base = *linep++;
9456 : :
9457 : : /* Print what we got so far. */
9458 : 4126 : fprintf (out, "\n"
9459 : : " %s: %" PRIu64 "\n"
9460 : : " %s: %" PRIuFAST16 "\n"
9461 : : " %s: %" PRIu64 "\n"
9462 : : " %s: %zd\n"
9463 : : " %s: %zd\n"
9464 : : " %s: %" PRIuFAST8 "\n"
9465 : : " %s: %" PRIuFAST8 "\n"
9466 : : " %s 'is_stmt': %" PRIuFAST8 "\n"
9467 : : " %s: %" PRIdFAST8 "\n"
9468 : : " %s: %" PRIuFAST8 "\n"
9469 : : " %s: %" PRIuFAST8 "\n"
9470 : : "\n"
9471 : : "%s:\n",
9472 : : length_str, (uint64_t) unit_length, dwarf_ver_str, version,
9473 : : prologue_len_str, (uint64_t) header_length, addr_size_str,
9474 : : address_size, seg_selector_str, (size_t) segment_selector_size,
9475 : : min_inst_len_str, minimum_instr_len, max_op_per_inst_str,
9476 : : max_ops_per_instr, initial_value_if_str, default_is_stmt,
9477 : : line_base_str, line_base, line_range_str, line_range,
9478 : : opcodes_base_str, opcode_base, opcodes_str);
9479 : :
9480 [ - + ]: 4126 : if (version < 2 || version > 5)
9481 : : {
9482 : 0 : error (0, 0, _("cannot handle .debug_line version: %u\n"),
9483 : : (unsigned int) version);
9484 : 0 : linep = lineendp;
9485 : 0 : continue;
9486 : : }
9487 : :
9488 [ - + ]: 4126 : if (address_size != 4 && address_size != 8)
9489 : : {
9490 : 0 : error (0, 0, _("cannot handle address size: %u\n"),
9491 : : (unsigned int) address_size);
9492 : 0 : linep = lineendp;
9493 : 0 : continue;
9494 : : }
9495 : :
9496 [ - + ]: 4126 : if (segment_selector_size != 0)
9497 : : {
9498 : 0 : error (0, 0, _("cannot handle segment selector size: %u\n"),
9499 : : (unsigned int) segment_selector_size);
9500 : 0 : linep = lineendp;
9501 : 0 : continue;
9502 : : }
9503 : :
9504 [ - + ]: 4126 : if (unlikely (linep + opcode_base - 1 >= lineendp))
9505 : : {
9506 : 0 : invalid_unit:
9507 : 0 : error (0, 0,
9508 : 0 : _("invalid data at offset %tu in section [%zu] '%s'"),
9509 : 0 : linep - (const unsigned char *) data->d_buf,
9510 : : elf_ndxscn (scn), section_name (ebl, shdr));
9511 : 0 : linep = lineendp;
9512 : 0 : continue;
9513 : : }
9514 : 4126 : int opcode_base_l10 = 1;
9515 : 4126 : unsigned int tmp = opcode_base;
9516 [ + + ]: 8246 : while (tmp > 10)
9517 : : {
9518 : 4120 : tmp /= 10;
9519 : 4120 : ++opcode_base_l10;
9520 : : }
9521 : : const uint8_t *standard_opcode_lengths = linep - 1;
9522 [ + + ]: 53620 : for (uint_fast8_t cnt = 1; cnt < opcode_base; ++cnt)
9523 : 49494 : fprintf (out, ngettext (" [%*" PRIuFAST8 "] %hhu argument\n",
9524 : : " [%*" PRIuFAST8 "] %hhu arguments\n",
9525 : : (int) linep[cnt - 1]),
9526 : 49494 : opcode_base_l10, cnt, linep[cnt - 1]);
9527 : 4126 : linep += opcode_base - 1;
9528 : :
9529 : 4126 : if (unlikely (linep >= lineendp))
9530 : : goto invalid_unit;
9531 : :
9532 : 4126 : Dwarf_Off str_offsets_base = str_offsets_base_off (dbg, NULL);
9533 : :
9534 : 4126 : fprintf (out, "\n%s:\n", dir_table_str);
9535 [ + + ]: 4126 : if (version > 4)
9536 : : {
9537 : 3974 : struct encpair { uint16_t desc; uint16_t form; };
9538 : 3974 : struct encpair enc[256];
9539 : :
9540 : 3974 : fprintf (out, " [");
9541 : 3974 : if ((size_t) (lineendp - linep) < 1)
9542 : 0 : goto invalid_data;
9543 : 3974 : unsigned char directory_entry_format_count = *linep++;
9544 [ + + ]: 7948 : for (int i = 0; i < directory_entry_format_count; i++)
9545 : : {
9546 : 3974 : uint16_t desc, form;
9547 [ - + ]: 3974 : if ((size_t) (lineendp - linep) < 1)
9548 : 0 : goto invalid_data;
9549 : 3974 : get_uleb128 (desc, linep, lineendp);
9550 [ - + ]: 3974 : if ((size_t) (lineendp - linep) < 1)
9551 : 0 : goto invalid_data;
9552 : 3974 : get_uleb128 (form, linep, lineendp);
9553 : :
9554 : 3974 : enc[i].desc = desc;
9555 : 3974 : enc[i].form = form;
9556 : :
9557 : 3974 : fprintf (out, "%s(%s)",
9558 : : dwarf_line_content_description_name (desc),
9559 : : dwarf_form_name (form));
9560 [ - + ]: 3974 : if (i + 1 < directory_entry_format_count)
9561 : 0 : fprintf (out, ", ");
9562 : : }
9563 : 3974 : fprintf (out, "]\n");
9564 : :
9565 : 3974 : uint64_t directories_count;
9566 [ - + ]: 3974 : if ((size_t) (lineendp - linep) < 1)
9567 : 0 : goto invalid_data;
9568 : 3974 : get_uleb128 (directories_count, linep, lineendp);
9569 : :
9570 : 3974 : if (directory_entry_format_count == 0
9571 [ - + ]: 3974 : && directories_count != 0)
9572 : 0 : goto invalid_data;
9573 : :
9574 [ + + ]: 34282 : for (uint64_t i = 0; i < directories_count; i++)
9575 : : {
9576 : 30308 : fprintf (out, " %-5" PRIu64 " ", i);
9577 [ + + ]: 60616 : for (int j = 0; j < directory_entry_format_count; j++)
9578 : : {
9579 : 30308 : linep = print_form_data (dbg, enc[j].form,
9580 : : linep, lineendp, length,
9581 : : str_offsets_base, out);
9582 [ - + ]: 30308 : if (j + 1 < directory_entry_format_count)
9583 : 0 : fprintf (out, ", ");
9584 : : }
9585 : 30308 : fprintf (out, "\n");
9586 [ - + ]: 30308 : if (linep >= lineendp)
9587 : 0 : goto invalid_unit;
9588 : : }
9589 : : }
9590 : : else
9591 : : {
9592 [ + - + + ]: 766 : while (linep < lineendp && *linep != 0)
9593 : : {
9594 : 614 : const unsigned char *endp = memchr (linep, '\0', lineendp - linep);
9595 [ - + ]: 614 : if (unlikely (endp == NULL))
9596 : 0 : goto invalid_unit;
9597 : :
9598 : 614 : fprintf (out, " %s\n", (char *) linep);
9599 : :
9600 : 614 : linep = endp + 1;
9601 : : }
9602 [ + - - + ]: 152 : if (linep >= lineendp || *linep != 0)
9603 : 0 : goto invalid_unit;
9604 : : /* Skip the final NUL byte. */
9605 : 152 : ++linep;
9606 : : }
9607 : :
9608 [ - + ]: 4126 : if (unlikely (linep >= lineendp))
9609 : 0 : goto invalid_unit;
9610 : :
9611 : 4126 : fprintf (out, "\n%s:\n", file_table_str);
9612 [ + + ]: 4126 : if (version > 4)
9613 : : {
9614 : 3974 : struct encpair { uint16_t desc; uint16_t form; };
9615 : 3974 : struct encpair enc[256];
9616 : :
9617 : 3974 : fprintf (out, " [");
9618 : 3974 : if ((size_t) (lineendp - linep) < 1)
9619 : 0 : goto invalid_data;
9620 : 3974 : unsigned char file_name_format_count = *linep++;
9621 [ + + ]: 11930 : for (int i = 0; i < file_name_format_count; i++)
9622 : : {
9623 : 7956 : uint64_t desc, form;
9624 [ - + ]: 7956 : if ((size_t) (lineendp - linep) < 1)
9625 : 0 : goto invalid_data;
9626 : 7956 : get_uleb128 (desc, linep, lineendp);
9627 [ - + ]: 7956 : if ((size_t) (lineendp - linep) < 1)
9628 : 0 : goto invalid_data;
9629 : 7956 : get_uleb128 (form, linep, lineendp);
9630 : :
9631 [ - + ]: 7956 : if (! libdw_valid_user_form (form))
9632 : 0 : goto invalid_data;
9633 : :
9634 : 7956 : enc[i].desc = desc;
9635 : 7956 : enc[i].form = form;
9636 : :
9637 : 7956 : fprintf (out, "%s(%s)",
9638 : : dwarf_line_content_description_name (desc),
9639 : : dwarf_form_name (form));
9640 [ + + ]: 7956 : if (i + 1 < file_name_format_count)
9641 : 3982 : fprintf (out, ", ");
9642 : : }
9643 : 3974 : fprintf (out, "]\n");
9644 : :
9645 : 3974 : uint64_t file_name_count;
9646 [ - + ]: 3974 : if ((size_t) (lineendp - linep) < 1)
9647 : 0 : goto invalid_data;
9648 : 3974 : get_uleb128 (file_name_count, linep, lineendp);
9649 : :
9650 : 3974 : if (file_name_format_count == 0
9651 [ - + ]: 3974 : && file_name_count != 0)
9652 : 0 : goto invalid_data;
9653 : :
9654 [ + + ]: 70656 : for (uint64_t i = 0; i < file_name_count; i++)
9655 : : {
9656 : 66682 : fprintf (out, " %-5" PRIu64 " ", i);
9657 [ + + ]: 200070 : for (int j = 0; j < file_name_format_count; j++)
9658 : : {
9659 : 133388 : linep = print_form_data (dbg, enc[j].form,
9660 : : linep, lineendp, length,
9661 : : str_offsets_base, out);
9662 [ + + ]: 133388 : if (j + 1 < file_name_format_count)
9663 : 66706 : fprintf (out, ", ");
9664 : : }
9665 : 66682 : fprintf (out, "\n");
9666 [ - + ]: 66682 : if (linep > lineendp)
9667 : 0 : goto invalid_unit;
9668 : : }
9669 : : }
9670 : : else
9671 : : {
9672 : 152 : fputs (_(" Entry Dir Time Size Name\n"), out);
9673 [ + - + + ]: 6154 : for (unsigned int cnt = 1; linep < lineendp && *linep != 0; ++cnt)
9674 : : {
9675 : : /* First comes the file name. */
9676 : 6002 : char *fname = (char *) linep;
9677 : 6002 : unsigned char *endp = memchr (fname, '\0', lineendp - linep);
9678 [ - + ]: 6002 : if (unlikely (endp == NULL))
9679 : 0 : goto invalid_unit;
9680 : 6002 : linep = endp + 1;
9681 : :
9682 : : /* Then the index. */
9683 : 6002 : unsigned int diridx;
9684 [ - + ]: 6002 : if (lineendp - linep < 1)
9685 : 0 : goto invalid_unit;
9686 : 6002 : get_uleb128 (diridx, linep, lineendp);
9687 : :
9688 : : /* Next comes the modification time. */
9689 : 6002 : unsigned int mtime;
9690 [ - + ]: 6002 : if (lineendp - linep < 1)
9691 : 0 : goto invalid_unit;
9692 : 6002 : get_uleb128 (mtime, linep, lineendp);
9693 : :
9694 : : /* Finally the length of the file. */
9695 : 6002 : unsigned int fsize;
9696 [ - + ]: 6002 : if (lineendp - linep < 1)
9697 : 0 : goto invalid_unit;
9698 : 6002 : get_uleb128 (fsize, linep, lineendp);
9699 : :
9700 : 6002 : fprintf (out, " %-5u %-5u %-9u %-9u %s\n",
9701 : : cnt, diridx, mtime, fsize, fname);
9702 : : }
9703 [ + - - + ]: 152 : if (linep >= lineendp || *linep != '\0')
9704 : 0 : goto invalid_unit;
9705 : : /* Skip the final NUL byte. */
9706 : 152 : ++linep;
9707 : : }
9708 : :
9709 : 4126 : unsigned int debug_str_offset = 0;
9710 [ + + ]: 4126 : if (unlikely (linep == header_start + header_length - 4))
9711 : : {
9712 : : /* CUBINs contain an unsigned 4-byte offset */
9713 [ - + ]: 2 : debug_str_offset = read_4ubyte_unaligned_inc (dbg, linep);
9714 : : }
9715 : :
9716 [ + + ]: 4126 : if (linep == lineendp)
9717 : : {
9718 : 40 : fprintf (out, "\n%s.\n", no_line_num_stmts_str);
9719 : 40 : continue;
9720 : : }
9721 : :
9722 : 4086 : fprintf (out, "\n%s:\n", line_num_stmts_str);
9723 : 4086 : Dwarf_Word address = 0;
9724 : 4086 : unsigned int op_index = 0;
9725 : 4086 : size_t line = 1;
9726 : 4086 : uint_fast8_t is_stmt = default_is_stmt;
9727 : :
9728 : : /* Apply the "operation advance" from a special opcode
9729 : : or DW_LNS_advance_pc (as per DWARF4 6.2.5.1). */
9730 : 4086 : unsigned int op_addr_advance;
9731 : : #define advance_pc(op_advance) run_advance_pc(op_advance, minimum_instr_len, \
9732 : : max_ops_per_instr, &op_addr_advance, &address, &op_index)
9733 : :
9734 [ - + ]: 4086 : if (max_ops_per_instr == 0)
9735 : : {
9736 : 0 : error (0, 0,
9737 : 0 : _("invalid maximum operations per instruction is zero"));
9738 : 0 : linep = lineendp;
9739 : 0 : continue;
9740 : : }
9741 : :
9742 [ + + ]: 2868464 : while (linep < lineendp)
9743 : : {
9744 : 2864378 : size_t offset = linep - (const unsigned char *) data->d_buf;
9745 : 2864378 : unsigned int u128;
9746 : 2864378 : int s128;
9747 : :
9748 : : /* Read the opcode. */
9749 : 2864378 : unsigned int opcode = *linep++;
9750 : :
9751 : 2864378 : fprintf (out, " [%6" PRIx64 "]", (uint64_t)offset);
9752 : : /* Is this a special opcode? */
9753 [ + + ]: 2864378 : if (likely (opcode >= opcode_base))
9754 : : {
9755 [ - + ]: 814238 : if (unlikely (line_range == 0))
9756 : 0 : goto invalid_unit;
9757 : :
9758 : : /* Yes. Handling this is quite easy since the opcode value
9759 : : is computed with
9760 : :
9761 : : opcode = (desired line increment - line_base)
9762 : : + (line_range * address advance) + opcode_base
9763 : : */
9764 : 814238 : int line_increment = (line_base
9765 : 814238 : + (opcode - opcode_base) % line_range);
9766 : :
9767 : : /* Perform the increments. */
9768 : 814238 : line += line_increment;
9769 : 814238 : advance_pc ((opcode - opcode_base) / line_range);
9770 : :
9771 : 814238 : fprintf (out, " %s %u: %s+%u = ",
9772 : : special_opcode_str, opcode, address_str, op_addr_advance);
9773 : 814238 : print_dwarf_addr (dwflmod, 0, address, address, out);
9774 [ - + ]: 814238 : if (op_index > 0)
9775 : 0 : fprintf (out, ", op_index = %u, %s%+d = %zu\n",
9776 : : op_index, line_str, line_increment, line);
9777 : : else
9778 : 814238 : fprintf (out, ", %s%+d = %zu\n",
9779 : : line_str, line_increment, line);
9780 : : }
9781 [ + + ]: 2050140 : else if (opcode == 0)
9782 : : {
9783 : : /* This an extended opcode. */
9784 [ - + ]: 216318 : if (unlikely (linep + 2 > lineendp))
9785 : 0 : goto invalid_unit;
9786 : :
9787 : : /* The length. */
9788 : 216318 : unsigned int len = *linep++;
9789 : :
9790 [ - + ]: 216318 : if (unlikely (linep + len > lineendp))
9791 : 0 : goto invalid_unit;
9792 : :
9793 : : /* The sub-opcode. */
9794 : 216318 : opcode = *linep++;
9795 : :
9796 : 216318 : fprintf (out, " %s %u: ", ext_opcode_str, opcode);
9797 : :
9798 [ + + - + : 216318 : switch (opcode)
+ - - ]
9799 : : {
9800 : 12214 : case DW_LNE_end_sequence:
9801 : 12214 : fprintf (out, " %s\n", end_of_seq_str);
9802 : :
9803 : : /* Reset the registers we care about. */
9804 : 12214 : address = 0;
9805 : 12214 : op_index = 0;
9806 : 12214 : line = 1;
9807 : 12214 : is_stmt = default_is_stmt;
9808 : 12214 : break;
9809 : :
9810 : 14140 : case DW_LNE_set_address:
9811 : 14140 : op_index = 0;
9812 [ - + ]: 14140 : if (unlikely ((size_t) (lineendp - linep) < address_size))
9813 : 0 : goto invalid_unit;
9814 [ + + ]: 14140 : if (address_size == 4)
9815 [ + + ]: 50 : address = read_4ubyte_unaligned_inc (dbg, linep);
9816 : : else
9817 [ + + ]: 14090 : address = read_8ubyte_unaligned_inc (dbg, linep);
9818 : : {
9819 : 14140 : fprintf (out, " %s ", set_addr_to_str);
9820 : 14140 : print_dwarf_addr (dwflmod, 0, address, address, out);
9821 : 14140 : fprintf (out, "\n");
9822 : : }
9823 : 14140 : break;
9824 : :
9825 : 0 : case DW_LNE_define_file:
9826 : : {
9827 : 0 : char *fname = (char *) linep;
9828 : 0 : const unsigned char *endp = memchr (linep, '\0',
9829 : 0 : lineendp - linep);
9830 [ # # ]: 0 : if (unlikely (endp == NULL))
9831 : 0 : goto invalid_unit;
9832 : 0 : linep = endp + 1;
9833 : :
9834 : 0 : unsigned int diridx;
9835 [ # # ]: 0 : if (lineendp - linep < 1)
9836 : 0 : goto invalid_unit;
9837 : 0 : get_uleb128 (diridx, linep, lineendp);
9838 : 0 : Dwarf_Word mtime;
9839 [ # # ]: 0 : if (lineendp - linep < 1)
9840 : 0 : goto invalid_unit;
9841 : 0 : get_uleb128 (mtime, linep, lineendp);
9842 : 0 : Dwarf_Word filelength;
9843 [ # # ]: 0 : if (lineendp - linep < 1)
9844 : 0 : goto invalid_unit;
9845 : 0 : get_uleb128 (filelength, linep, lineendp);
9846 : :
9847 : 0 : fprintf (out, _("\
9848 : : define new file: dir=%u, mtime=%" PRIu64 ", length=%" PRIu64 ", name=%s\n"),
9849 : : diridx, (uint64_t) mtime, (uint64_t) filelength,
9850 : : fname);
9851 : : }
9852 : 0 : break;
9853 : :
9854 : 189950 : case DW_LNE_set_discriminator:
9855 : : /* Takes one ULEB128 parameter, the discriminator. */
9856 [ + - - + ]: 189950 : if (unlikely (standard_opcode_lengths[opcode] != 1
9857 : : || lineendp - linep < 1))
9858 : 0 : goto invalid_unit;
9859 : :
9860 : 189950 : get_uleb128 (u128, linep, lineendp);
9861 : 189950 : fprintf (out, " %s %u\n", set_disc_str, u128);
9862 : 189950 : break;
9863 : :
9864 : 14 : case DW_LNE_NVIDIA_inlined_call:
9865 : : {
9866 [ - + ]: 14 : if (unlikely (linep >= lineendp))
9867 : 0 : goto invalid_data;
9868 : :
9869 : 14 : unsigned int context;
9870 : 14 : get_uleb128 (context, linep, lineendp);
9871 : :
9872 [ - + ]: 14 : if (unlikely (linep >= lineendp))
9873 : 0 : goto invalid_data;
9874 : :
9875 : 14 : unsigned int function_name;
9876 : 14 : get_uleb128 (function_name, linep, lineendp);
9877 : 14 : function_name += debug_str_offset;
9878 : :
9879 : 14 : Elf_Data *str_data = dbg->sectiondata[IDX_debug_str];
9880 : 14 : char *function_str;
9881 [ + - - + ]: 14 : if (str_data == NULL || function_name >= str_data->d_size
9882 [ - + ]: 14 : || memchr (str_data->d_buf + function_name, '\0',
9883 : : str_data->d_size - function_name) == NULL)
9884 : : function_str = "???";
9885 : : else
9886 : 14 : function_str = (char *) str_data->d_buf + function_name;
9887 : :
9888 : 14 : fprintf (out, _(" set inlined context %u,"
9889 : : " function name %s (0x%x)\n"),
9890 : : context, function_str, function_name);
9891 : 14 : break;
9892 : : }
9893 : :
9894 : 0 : case DW_LNE_NVIDIA_set_function_name:
9895 : : {
9896 [ # # ]: 0 : if (unlikely (linep >= lineendp))
9897 : 0 : goto invalid_data;
9898 : :
9899 : 0 : unsigned int function_name;
9900 : 0 : get_uleb128 (function_name, linep, lineendp);
9901 : 0 : function_name += debug_str_offset;
9902 : :
9903 : 0 : Elf_Data *str_data = dbg->sectiondata[IDX_debug_str];
9904 : 0 : char *function_str;
9905 [ # # # # ]: 0 : if (str_data == NULL || function_name >= str_data->d_size
9906 [ # # ]: 0 : || memchr (str_data->d_buf + function_name, '\0',
9907 : : str_data->d_size - function_name) == NULL)
9908 : : function_str = "???";
9909 : : else
9910 : 0 : function_str = (char *) str_data->d_buf + function_name;
9911 : :
9912 : 0 : fprintf (out, _(" set function name %s (0x%x)\n"),
9913 : : function_str, function_name);
9914 : : }
9915 : 0 : break;
9916 : :
9917 : 0 : default:
9918 : : /* Unknown, ignore it. */
9919 : 0 : fputs (_(" unknown opcode\n"), out);
9920 : 0 : linep += len - 1;
9921 : 0 : break;
9922 : : }
9923 : : }
9924 [ + - ]: 1833822 : else if (opcode <= DW_LNS_set_isa)
9925 : : {
9926 : : /* This is a known standard opcode. */
9927 [ + + + + : 1833822 : switch (opcode)
+ + - + +
+ - - ]
9928 : : {
9929 : 285404 : case DW_LNS_copy:
9930 : : /* Takes no argument. */
9931 : 285404 : fprintf (out, " %s\n", copy_str);
9932 : 285404 : break;
9933 : :
9934 : 17606 : case DW_LNS_advance_pc:
9935 : : /* Takes one uleb128 parameter which is added to the
9936 : : address. */
9937 [ - + ]: 17606 : if (lineendp - linep < 1)
9938 : 0 : goto invalid_unit;
9939 : 17606 : get_uleb128 (u128, linep, lineendp);
9940 : 17606 : advance_pc (u128);
9941 : : {
9942 : 17606 : fprintf (out, " %s %u %s ",
9943 : : adv_addr_str, op_addr_advance, to_str);
9944 : 17606 : print_dwarf_addr (dwflmod, 0, address, address, out);
9945 [ - + ]: 17606 : if (op_index > 0)
9946 : 0 : fprintf (out, ", op_index %s %u", to_str, op_index);
9947 : 17606 : fprintf (out, "\n");
9948 : : }
9949 : 17606 : break;
9950 : :
9951 : 210552 : case DW_LNS_advance_line:
9952 : : /* Takes one sleb128 parameter which is added to the
9953 : : line. */
9954 [ - + ]: 210552 : if (lineendp - linep < 1)
9955 : 0 : goto invalid_unit;
9956 : 210552 : get_sleb128 (s128, linep, lineendp);
9957 : 210552 : line += s128;
9958 : 210552 : fprintf (out, "\
9959 : : %s %d to %" PRId64 "\n",
9960 : : adv_line_by_str, s128, (int64_t) line);
9961 : 210552 : break;
9962 : :
9963 : 71702 : case DW_LNS_set_file:
9964 : : /* Takes one uleb128 parameter which is stored in file. */
9965 [ - + ]: 71702 : if (lineendp - linep < 1)
9966 : 0 : goto invalid_unit;
9967 : 71702 : get_uleb128 (u128, linep, lineendp);
9968 : 71702 : fprintf (out, " %s %" PRIu64 "\n",
9969 : : set_file_str, (uint64_t) u128);
9970 : 71702 : break;
9971 : :
9972 : 694808 : case DW_LNS_set_column:
9973 : : /* Takes one uleb128 parameter which is stored in column. */
9974 [ + - - + ]: 694808 : if (unlikely (standard_opcode_lengths[opcode] != 1
9975 : : || lineendp - linep < 1))
9976 : 0 : goto invalid_unit;
9977 : :
9978 : 694808 : get_uleb128 (u128, linep, lineendp);
9979 : 694808 : fprintf (out, " %s %" PRIu64 "\n",
9980 : : set_col_str, (uint64_t) u128);
9981 : 694808 : break;
9982 : :
9983 : 492712 : case DW_LNS_negate_stmt:
9984 : : /* Takes no argument. */
9985 : 492712 : is_stmt = 1 - is_stmt;
9986 : 492712 : fprintf (out, " %s '%s' %s %" PRIuFAST8 "\n",
9987 : : set_str, "is_stmt", to_str, is_stmt);
9988 : 492712 : break;
9989 : :
9990 : 0 : case DW_LNS_set_basic_block:
9991 : : /* Takes no argument. */
9992 : 0 : fputs (_(" set basic block flag\n"), out);
9993 : 0 : break;
9994 : :
9995 : 60984 : case DW_LNS_const_add_pc:
9996 : : /* Takes no argument. */
9997 : :
9998 [ - + ]: 60984 : if (unlikely (line_range == 0))
9999 : 0 : goto invalid_unit;
10000 : :
10001 : 60984 : advance_pc ((255 - opcode_base) / line_range);
10002 : : {
10003 : 60984 : fprintf (out, " %s %u to ",
10004 : : adv_addr_by_str, op_addr_advance);
10005 : 60984 : print_dwarf_addr (dwflmod, 0, address, address, out);
10006 [ - + ]: 60984 : if (op_index > 0)
10007 : 0 : fprintf (out, _(", op_index to %u"), op_index);
10008 : 60984 : fprintf (out, "\n");
10009 : : }
10010 : 60984 : break;
10011 : :
10012 : 44 : case DW_LNS_fixed_advance_pc:
10013 : : /* Takes one 16 bit parameter which is added to the
10014 : : address. */
10015 [ + - - + ]: 44 : if (unlikely (standard_opcode_lengths[opcode] != 1
10016 : : || lineendp - linep < 2))
10017 : 0 : goto invalid_unit;
10018 : :
10019 [ - + ]: 44 : u128 = read_2ubyte_unaligned_inc (dbg, linep);
10020 : 44 : address += u128;
10021 : 44 : op_index = 0;
10022 : : {
10023 : 44 : fprintf (out, _("\
10024 : : advance address by fixed value %u to \n"),
10025 : : u128);
10026 : 44 : print_dwarf_addr (dwflmod, 0, address, address, out);
10027 : 44 : fprintf (out, "\n");
10028 : : }
10029 : 44 : break;
10030 : :
10031 : 10 : case DW_LNS_set_prologue_end:
10032 : : /* Takes no argument. */
10033 : 10 : fputs (_(" set prologue end flag\n"), out);
10034 : 10 : break;
10035 : :
10036 : 0 : case DW_LNS_set_epilogue_begin:
10037 : : /* Takes no argument. */
10038 : 0 : fputs (_(" set epilogue begin flag\n"), out);
10039 : 0 : break;
10040 : :
10041 : 0 : case DW_LNS_set_isa:
10042 : : /* Takes one uleb128 parameter which is stored in isa. */
10043 [ # # # # ]: 0 : if (unlikely (standard_opcode_lengths[opcode] != 1
10044 : : || lineendp - linep < 1))
10045 : 0 : goto invalid_unit;
10046 : :
10047 : 0 : get_uleb128 (u128, linep, lineendp);
10048 : 0 : fprintf (out, _(" set isa to %u\n"), u128);
10049 : 0 : break;
10050 : : }
10051 : : }
10052 : : else
10053 : : {
10054 : : /* This is a new opcode the generator but not we know about.
10055 : : Read the parameters associated with it but then discard
10056 : : everything. Read all the parameters for this opcode. */
10057 : 0 : fprintf (out,
10058 : 0 : ngettext (" unknown opcode with %" PRIu8 " parameter:",
10059 : : " unknown opcode with %" PRIu8 " parameters:",
10060 : : standard_opcode_lengths[opcode]),
10061 : 0 : standard_opcode_lengths[opcode]);
10062 : 0 : for (int n = standard_opcode_lengths[opcode];
10063 [ # # # # ]: 0 : n > 0 && linep < lineendp; --n)
10064 : : {
10065 : 0 : get_uleb128 (u128, linep, lineendp);
10066 [ # # ]: 0 : if (n != standard_opcode_lengths[opcode])
10067 : 0 : fputc (',', out);
10068 : 0 : fprintf (out, " %u", u128);
10069 : : }
10070 : :
10071 : : /* Next round, ignore this opcode. */
10072 : 0 : continue;
10073 : : }
10074 : : }
10075 : : }
10076 : :
10077 : : /* There must only be one data block. */
10078 [ - + ]: 104 : assert (elf_getdata (scn, data) == NULL);
10079 : : }
10080 : :
10081 : :
10082 : : static void
10083 : 40 : print_debug_loclists_section (Dwfl_Module *dwflmod,
10084 : : Ebl *ebl,
10085 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
10086 : : Elf_Scn *scn, GElf_Shdr *shdr,
10087 : : Dwarf *dbg, FILE *out)
10088 : : {
10089 : 40 : Elf_Data *data = get_debug_elf_data (dbg, ebl, IDX_debug_loclists, scn);
10090 [ + - ]: 40 : if (data == NULL)
10091 : 0 : return;
10092 : :
10093 : 40 : fprintf (out, _("\
10094 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
10095 : : elf_ndxscn (scn), section_name (ebl, shdr),
10096 : 40 : (uint64_t) shdr->sh_offset);
10097 : :
10098 : : /* For the listptr to get the base address/CU. */
10099 : 40 : sort_listptr (&known_loclistsptr, "loclistsptr");
10100 : 40 : size_t listptr_idx = 0;
10101 : :
10102 : 40 : const unsigned char *readp = data->d_buf;
10103 : 40 : const unsigned char *const dataend = ((unsigned char *) data->d_buf
10104 : 40 : + data->d_size);
10105 [ + + ]: 3570 : while (readp < dataend)
10106 : : {
10107 [ - + ]: 3530 : if (unlikely (readp > dataend - 4))
10108 : : {
10109 : 0 : invalid_data:
10110 : 0 : error (0, 0, _("invalid data in section [%zu] '%s'"),
10111 : : elf_ndxscn (scn), section_name (ebl, shdr));
10112 : 0 : return;
10113 : : }
10114 : :
10115 : 3530 : ptrdiff_t offset = readp - (unsigned char *) data->d_buf;
10116 : 3530 : fprintf (out, "%s 0x%" PRIx64 ":\n\n",
10117 : : table_offset_upper_str, (uint64_t) offset);
10118 : :
10119 [ - + ]: 3530 : uint64_t unit_length = read_4ubyte_unaligned_inc (dbg, readp);
10120 : 3530 : unsigned int offset_size = 4;
10121 [ - + ]: 3530 : if (unlikely (unit_length == 0xffffffff))
10122 : : {
10123 [ # # ]: 0 : if (unlikely (readp > dataend - 8))
10124 : 0 : goto invalid_data;
10125 : :
10126 [ # # ]: 0 : unit_length = read_8ubyte_unaligned_inc (dbg, readp);
10127 : 0 : offset_size = 8;
10128 : : }
10129 : 3530 : fprintf (out, " %s: %8" PRIu64 "\n", length_str, unit_length);
10130 : :
10131 : : /* We need at least 2-bytes + 1-byte + 1-byte + 4-bytes = 8
10132 : : bytes to complete the header. And this unit cannot go beyond
10133 : : the section data. */
10134 [ + - ]: 3530 : if (readp > dataend - 8
10135 [ + - ]: 3530 : || unit_length < 8
10136 [ - + ]: 3530 : || unit_length > (uint64_t) (dataend - readp))
10137 : 0 : goto invalid_data;
10138 : :
10139 : 3530 : const unsigned char *nexthdr = readp + unit_length;
10140 : :
10141 [ - + ]: 3530 : uint16_t version = read_2ubyte_unaligned_inc (dbg, readp);
10142 : 3530 : fprintf (out, " %s: %8" PRIu16 "\n", dwarf_ver_str, version);
10143 : :
10144 [ - + ]: 3530 : if (version != 5)
10145 : : {
10146 : 0 : error (0, 0, _("Unknown version"));
10147 : 0 : goto next_table;
10148 : : }
10149 : :
10150 : 3530 : uint8_t address_size = *readp++;
10151 : 3530 : fprintf (out, " %s: %8" PRIu64 "\n",
10152 : : addr_size_str, (uint64_t) address_size);
10153 : :
10154 [ - + ]: 3530 : if (address_size != 4 && address_size != 8)
10155 : : {
10156 : 0 : error (0, 0, _("unsupported address size"));
10157 : 0 : goto next_table;
10158 : : }
10159 : :
10160 : 3530 : uint8_t segment_size = *readp++;
10161 : 3530 : fprintf (out, " %s: %8" PRIu64 "\n",
10162 : : seg_size_str, (uint64_t) segment_size);
10163 : :
10164 [ - + ]: 3530 : if (segment_size != 0)
10165 : : {
10166 : 0 : error (0, 0, _("unsupported segment size"));
10167 : 0 : goto next_table;
10168 : : }
10169 : :
10170 [ - + ]: 3530 : uint32_t offset_entry_count = read_4ubyte_unaligned_inc (dbg, readp);
10171 : 3530 : fprintf (out, " %s: %8" PRIu64 "\n",
10172 : : offset_ent_str, (uint64_t) offset_entry_count);
10173 : :
10174 : : /* We need the CU that uses this unit to get the initial base address. */
10175 : 3530 : Dwarf_Addr cu_base = 0;
10176 : 3530 : struct Dwarf_CU *cu = NULL;
10177 [ - + ]: 3530 : if (listptr_cu (&known_loclistsptr, &listptr_idx,
10178 : : (Dwarf_Off) offset,
10179 : 3530 : (Dwarf_Off) (nexthdr - (unsigned char *) data->d_buf),
10180 : : &cu_base, &cu)
10181 [ # # ]: 0 : || split_dwarf_cu_base (dbg, &cu, &cu_base))
10182 : 3530 : {
10183 : 3530 : Dwarf_Die cudie;
10184 [ - + ]: 3530 : if (dwarf_cu_die (cu, &cudie,
10185 : : NULL, NULL, NULL, NULL,
10186 : : NULL, NULL) == NULL)
10187 : 0 : fprintf (out, " %s: ", unknown_base_str);
10188 : : else
10189 : 3530 : fprintf (out, " %s [%6" PRIx64 "] %s: ",
10190 : : cu_str, dwarf_dieoffset (&cudie), base_str);
10191 : 3530 : print_dwarf_addr (dwflmod, address_size, cu_base, cu_base, out);
10192 : 3530 : fprintf (out, "\n");
10193 : : }
10194 : : else
10195 : 0 : fprintf (out, _(" Not associated with a CU.\n"));
10196 : :
10197 : 3530 : fprintf (out, "\n");
10198 : :
10199 : 3530 : const unsigned char *offset_array_start = readp;
10200 [ + + ]: 3530 : if (offset_entry_count > 0)
10201 : : {
10202 : 4 : uint64_t max_entries = (unit_length - 8) / offset_size;
10203 [ - + ]: 4 : if (offset_entry_count > max_entries)
10204 : : {
10205 : 0 : error (0, 0,
10206 : 0 : _("too many offset entries for unit length"));
10207 : 0 : offset_entry_count = max_entries;
10208 : : }
10209 : :
10210 : 4 : fprintf (out, _(" Offsets starting at 0x%" PRIx64 ":\n"),
10211 : : (uint64_t) (offset_array_start
10212 : 4 : - (unsigned char *) data->d_buf));
10213 [ + + ]: 36 : for (uint32_t idx = 0; idx < offset_entry_count; idx++)
10214 : : {
10215 : 32 : fprintf (out, " [%6" PRIu32 "] ", idx);
10216 [ + - ]: 32 : if (offset_size == 4)
10217 : : {
10218 [ - + ]: 32 : uint32_t off = read_4ubyte_unaligned_inc (dbg, readp);
10219 : 32 : fprintf (out, "0x%" PRIx32 "\n", off);
10220 : : }
10221 : : else
10222 : : {
10223 [ # # ]: 0 : uint64_t off = read_8ubyte_unaligned_inc (dbg, readp);
10224 : 0 : fprintf (out, "0x%" PRIx64 "\n", off);
10225 : : }
10226 : : }
10227 : 4 : fprintf (out, "\n");
10228 : : }
10229 : :
10230 : 3530 : Dwarf_Addr base = cu_base;
10231 : 3530 : bool start_of_list = true;
10232 [ + + ]: 870182 : while (readp < nexthdr)
10233 : : {
10234 : 866652 : Dwarf_Off off = (Dwarf_Off) (readp - (unsigned char *) data->d_buf);
10235 [ + + ]: 866652 : if (listptr_attr (&known_loclistsptr, listptr_idx, off,
10236 : : DW_AT_GNU_locviews))
10237 : 124410 : {
10238 : 124410 : Dwarf_Off next_off = next_listptr_offset (&known_loclistsptr,
10239 : : &listptr_idx, off);
10240 : 124410 : const unsigned char *locp = readp;
10241 : 124410 : const unsigned char *locendp;
10242 [ + - ]: 124410 : if (next_off == 0
10243 [ + - ]: 124410 : || next_off > (size_t) (nexthdr - ((const unsigned char *)
10244 : : data->d_buf)))
10245 : : locendp = nexthdr;
10246 : : else
10247 : 124410 : locendp = (const unsigned char *) data->d_buf + next_off;
10248 : :
10249 : 124410 : fprintf (out, " Offset: %" PRIx64 ", Index: %" PRIx64 "\n",
10250 : : (uint64_t) (readp - (unsigned char *) data->d_buf),
10251 : 124410 : (uint64_t) (readp - offset_array_start));
10252 : :
10253 [ + + ]: 690080 : while (locp < locendp)
10254 : : {
10255 : 565670 : uint64_t v1, v2;
10256 : 565670 : get_uleb128 (v1, locp, locendp);
10257 [ - + ]: 565670 : if (locp >= locendp)
10258 : : {
10259 : 0 : fprintf (out, _(" <INVALID DATA>\n"));
10260 : 0 : break;
10261 : : }
10262 : 565670 : get_uleb128 (v2, locp, locendp);
10263 : 565670 : fprintf (out,
10264 : : " view pair %" PRId64 ", %" PRId64 "\n", v1, v2);
10265 : : }
10266 : :
10267 : 124410 : fprintf (out, "\n");
10268 : 124410 : readp = (unsigned char *) locendp;
10269 : 124410 : continue;
10270 : : }
10271 : :
10272 : 742242 : uint8_t kind = *readp++;
10273 : 742242 : uint64_t op1, op2, len;
10274 : :
10275 : : /* Skip padding. */
10276 [ - + ]: 742242 : if (start_of_list && kind == DW_LLE_end_of_list)
10277 : 0 : continue;
10278 : :
10279 [ + + ]: 742242 : if (start_of_list)
10280 : : {
10281 : 124474 : base = cu_base;
10282 : 124474 : fprintf (out, " Offset: %" PRIx64 ", Index: %" PRIx64 "\n",
10283 : 124474 : (uint64_t) (readp - (unsigned char *) data->d_buf - 1),
10284 : 124474 : (uint64_t) (readp - offset_array_start - 1));
10285 : 124474 : start_of_list = false;
10286 : : }
10287 : :
10288 : 742242 : fprintf (out, " %s", dwarf_loc_list_encoding_name (kind));
10289 [ + - - + : 742242 : switch (kind)
+ - + - +
- - ]
10290 : : {
10291 : 124474 : case DW_LLE_end_of_list:
10292 : 124474 : start_of_list = true;
10293 : 124474 : fprintf (out, "\n\n");
10294 : 124474 : break;
10295 : :
10296 : 0 : case DW_LLE_base_addressx:
10297 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
10298 : : {
10299 : 0 : invalid_entry:
10300 : 0 : error (0, 0, _("invalid loclists data"));
10301 : 0 : goto next_table;
10302 : : }
10303 : 0 : get_uleb128 (op1, readp, nexthdr);
10304 : 0 : fprintf (out, " %" PRIx64 "\n", op1);
10305 [ # # ]: 0 : if (! print_unresolved_addresses)
10306 : : {
10307 : 0 : Dwarf_Addr addr;
10308 [ # # ]: 0 : if (get_indexed_addr (cu, op1, &addr) != 0)
10309 : 0 : fprintf (out, " ???\n");
10310 : : else
10311 : : {
10312 : 0 : fprintf (out, " ");
10313 : 0 : print_dwarf_addr (dwflmod, address_size, addr, addr, out);
10314 : 0 : fprintf (out, "\n");
10315 : : }
10316 : : }
10317 : : break;
10318 : :
10319 : 0 : case DW_LLE_startx_endx:
10320 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
10321 : 0 : goto invalid_entry;
10322 : 0 : get_uleb128 (op1, readp, nexthdr);
10323 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
10324 : 0 : goto invalid_entry;
10325 : 0 : get_uleb128 (op2, readp, nexthdr);
10326 : 0 : fprintf (out, " %" PRIx64 ", %" PRIx64 "\n", op1, op2);
10327 [ # # ]: 0 : if (! print_unresolved_addresses)
10328 : : {
10329 : 0 : Dwarf_Addr addr1;
10330 : 0 : Dwarf_Addr addr2;
10331 [ # # ]: 0 : if (get_indexed_addr (cu, op1, &addr1) != 0
10332 [ # # ]: 0 : || get_indexed_addr (cu, op2, &addr2) != 0)
10333 : : {
10334 : 0 : fprintf (out, " ???..\n");
10335 : 0 : fprintf (out, " ???\n");
10336 : : }
10337 : : else
10338 : : {
10339 : 0 : fprintf (out, " ");
10340 : 0 : print_dwarf_addr (dwflmod, address_size,
10341 : : addr1, addr1, out);
10342 : 0 : fprintf (out, "..\n ");
10343 : 0 : print_dwarf_addr (dwflmod, address_size,
10344 : : addr2 - 1, addr2, out);
10345 : 0 : fprintf (out, "\n");
10346 : : }
10347 : : }
10348 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
10349 : 0 : goto invalid_entry;
10350 : 0 : get_uleb128 (len, readp, nexthdr);
10351 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < len)
10352 : 0 : goto invalid_entry;
10353 : 0 : print_ops (dwflmod, dbg, 8, 8, version,
10354 : : address_size, offset_size, cu, len, readp, out);
10355 : 0 : readp += len;
10356 : 0 : break;
10357 : :
10358 : 52 : case DW_LLE_startx_length:
10359 [ - + ]: 52 : if ((uint64_t) (nexthdr - readp) < 1)
10360 : 0 : goto invalid_entry;
10361 : 52 : get_uleb128 (op1, readp, nexthdr);
10362 [ - + ]: 52 : if ((uint64_t) (nexthdr - readp) < 1)
10363 : 0 : goto invalid_entry;
10364 : 52 : get_uleb128 (op2, readp, nexthdr);
10365 : 52 : fprintf (out, " %" PRIx64 ", %" PRIx64 "\n", op1, op2);
10366 [ + - ]: 52 : if (! print_unresolved_addresses)
10367 : : {
10368 : 52 : Dwarf_Addr addr1;
10369 : 52 : Dwarf_Addr addr2;
10370 [ - + ]: 52 : if (get_indexed_addr (cu, op1, &addr1) != 0)
10371 : : {
10372 : 0 : fprintf (out, " ???..\n");
10373 : 0 : fprintf (out, " ???\n");
10374 : : }
10375 : : else
10376 : : {
10377 : 52 : addr2 = addr1 + op2;
10378 : 52 : fprintf (out, " ");
10379 : 52 : print_dwarf_addr (dwflmod, address_size, addr1, addr1, out);
10380 : 52 : fprintf (out, "..\n ");
10381 : 52 : print_dwarf_addr (dwflmod, address_size,
10382 : : addr2 - 1, addr2, out);
10383 : 52 : fprintf (out, "\n");
10384 : : }
10385 : : }
10386 [ - + ]: 52 : if ((uint64_t) (nexthdr - readp) < 1)
10387 : 0 : goto invalid_entry;
10388 : 52 : get_uleb128 (len, readp, nexthdr);
10389 [ - + ]: 52 : if ((uint64_t) (nexthdr - readp) < len)
10390 : 0 : goto invalid_entry;
10391 : 52 : print_ops (dwflmod, dbg, 8, 8, version,
10392 : : address_size, offset_size, cu, len, readp, out);
10393 : 52 : readp += len;
10394 : 52 : break;
10395 : :
10396 : 541012 : case DW_LLE_offset_pair:
10397 [ - + ]: 541012 : if ((uint64_t) (nexthdr - readp) < 1)
10398 : 0 : goto invalid_entry;
10399 : 541012 : get_uleb128 (op1, readp, nexthdr);
10400 [ - + ]: 541012 : if ((uint64_t) (nexthdr - readp) < 1)
10401 : 0 : goto invalid_entry;
10402 : 541012 : get_uleb128 (op2, readp, nexthdr);
10403 : 541012 : fprintf (out, " %" PRIx64 ", %" PRIx64 "\n", op1, op2);
10404 [ + - ]: 541012 : if (! print_unresolved_addresses)
10405 : : {
10406 : 541012 : op1 += base;
10407 : 541012 : op2 += base;
10408 : 541012 : fprintf (out, " ");
10409 : 541012 : print_dwarf_addr (dwflmod, address_size, op1, op1, out);
10410 : 541012 : fprintf (out, "..\n ");
10411 : 541012 : print_dwarf_addr (dwflmod, address_size, op2 - 1, op2, out);
10412 : 541012 : fprintf (out, "\n");
10413 : : }
10414 [ - + ]: 541012 : if ((uint64_t) (nexthdr - readp) < 1)
10415 : 0 : goto invalid_entry;
10416 : 541012 : get_uleb128 (len, readp, nexthdr);
10417 [ - + ]: 541012 : if ((uint64_t) (nexthdr - readp) < len)
10418 : 0 : goto invalid_entry;
10419 : 541012 : print_ops (dwflmod, dbg, 8, 8, version,
10420 : : address_size, offset_size, cu, len, readp, out);
10421 : 541012 : readp += len;
10422 : 541012 : break;
10423 : :
10424 : 0 : case DW_LLE_default_location:
10425 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
10426 : 0 : goto invalid_entry;
10427 : 0 : get_uleb128 (len, readp, nexthdr);
10428 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < len)
10429 : 0 : goto invalid_entry;
10430 : 0 : print_ops (dwflmod, dbg, 8, 8, version,
10431 : : address_size, offset_size, cu, len, readp, out);
10432 : 0 : readp += len;
10433 : 0 : break;
10434 : :
10435 : 51994 : case DW_LLE_base_address:
10436 [ - + ]: 51994 : if (address_size == 4)
10437 : : {
10438 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 4)
10439 : 0 : goto invalid_entry;
10440 [ # # ]: 0 : op1 = read_4ubyte_unaligned_inc (dbg, readp);
10441 : : }
10442 : : else
10443 : : {
10444 [ - + ]: 51994 : if ((uint64_t) (nexthdr - readp) < 8)
10445 : 0 : goto invalid_entry;
10446 [ - + ]: 51994 : op1 = read_8ubyte_unaligned_inc (dbg, readp);
10447 : : }
10448 : 51994 : base = op1;
10449 : 51994 : fprintf (out, " 0x%" PRIx64 "\n", base);
10450 [ + - ]: 51994 : if (! print_unresolved_addresses)
10451 : : {
10452 : 51994 : fprintf (out, " ");
10453 : 51994 : print_dwarf_addr (dwflmod, address_size, base, base, out);
10454 : 51994 : fprintf (out, "\n");
10455 : : }
10456 : : break;
10457 : :
10458 : 0 : case DW_LLE_start_end:
10459 [ # # ]: 0 : if (address_size == 4)
10460 : : {
10461 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 8)
10462 : 0 : goto invalid_entry;
10463 [ # # ]: 0 : op1 = read_4ubyte_unaligned_inc (dbg, readp);
10464 [ # # ]: 0 : op2 = read_4ubyte_unaligned_inc (dbg, readp);
10465 : : }
10466 : : else
10467 : : {
10468 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 16)
10469 : 0 : goto invalid_entry;
10470 [ # # ]: 0 : op1 = read_8ubyte_unaligned_inc (dbg, readp);
10471 [ # # ]: 0 : op2 = read_8ubyte_unaligned_inc (dbg, readp);
10472 : : }
10473 : 0 : fprintf (out, " 0x%" PRIx64 "..0x%" PRIx64 "\n", op1, op2);
10474 [ # # ]: 0 : if (! print_unresolved_addresses)
10475 : : {
10476 : 0 : fprintf (out, " ");
10477 : 0 : print_dwarf_addr (dwflmod, address_size, op1, op1, out);
10478 : 0 : fprintf (out, "..\n ");
10479 : 0 : print_dwarf_addr (dwflmod, address_size, op2 - 1, op2, out);
10480 : 0 : fprintf (out, "\n");
10481 : : }
10482 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
10483 : 0 : goto invalid_entry;
10484 : 0 : get_uleb128 (len, readp, nexthdr);
10485 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < len)
10486 : 0 : goto invalid_entry;
10487 : 0 : print_ops (dwflmod, dbg, 8, 8, version,
10488 : : address_size, offset_size, cu, len, readp, out);
10489 : 0 : readp += len;
10490 : 0 : break;
10491 : :
10492 : 24710 : case DW_LLE_start_length:
10493 [ - + ]: 24710 : if (address_size == 4)
10494 : : {
10495 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 4)
10496 : 0 : goto invalid_entry;
10497 [ # # ]: 0 : op1 = read_4ubyte_unaligned_inc (dbg, readp);
10498 : : }
10499 : : else
10500 : : {
10501 [ - + ]: 24710 : if ((uint64_t) (nexthdr - readp) < 8)
10502 : 0 : goto invalid_entry;
10503 [ - + ]: 24710 : op1 = read_8ubyte_unaligned_inc (dbg, readp);
10504 : : }
10505 [ - + ]: 24710 : if ((uint64_t) (nexthdr - readp) < 1)
10506 : 0 : goto invalid_entry;
10507 : 24710 : get_uleb128 (op2, readp, nexthdr);
10508 : 24710 : fprintf (out, " 0x%" PRIx64 ", %" PRIx64 "\n", op1, op2);
10509 [ + + ]: 24710 : if (! print_unresolved_addresses)
10510 : : {
10511 : 24708 : op2 = op1 + op2;
10512 : 24708 : fprintf (out, " ");
10513 : 24708 : print_dwarf_addr (dwflmod, address_size, op1, op1, out);
10514 : 24708 : fprintf (out, "..\n ");
10515 : 24708 : print_dwarf_addr (dwflmod, address_size, op2 - 1, op2, out);
10516 : 24708 : fprintf (out, "\n");
10517 : : }
10518 [ - + ]: 24710 : if ((uint64_t) (nexthdr - readp) < 1)
10519 : 0 : goto invalid_entry;
10520 : 24710 : get_uleb128 (len, readp, nexthdr);
10521 [ - + ]: 24710 : if ((uint64_t) (nexthdr - readp) < len)
10522 : 0 : goto invalid_entry;
10523 : 24710 : print_ops (dwflmod, dbg, 8, 8, version,
10524 : : address_size, offset_size, cu, len, readp, out);
10525 : 24710 : readp += len;
10526 : 24710 : break;
10527 : :
10528 : 0 : case DW_LLE_GNU_view_pair:
10529 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
10530 : 0 : goto invalid_entry;
10531 : 0 : get_uleb128 (op1, readp, nexthdr);
10532 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
10533 : 0 : goto invalid_entry;
10534 : 0 : get_uleb128 (op2, readp, nexthdr);
10535 : 0 : fprintf (out, " %" PRIx64 ", %" PRIx64 "\n", op1, op2);
10536 : 0 : break;
10537 : :
10538 : 0 : default:
10539 : 0 : goto invalid_entry;
10540 : : }
10541 : : }
10542 : :
10543 : 3530 : next_table:
10544 [ - + ]: 3530 : if (readp != nexthdr)
10545 : : {
10546 : 0 : size_t padding = nexthdr - readp;
10547 : 0 : fprintf (out, _(" %zu padding bytes\n\n"), padding);
10548 : 0 : readp = nexthdr;
10549 : : }
10550 : : }
10551 : : }
10552 : :
10553 : :
10554 : : static void
10555 : 52 : print_debug_loc_section (Dwfl_Module *dwflmod,
10556 : : Ebl *ebl, GElf_Ehdr *ehdr,
10557 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg,
10558 : : FILE *out)
10559 : : {
10560 : 52 : Elf_Data *data = get_debug_elf_data (dbg, ebl, IDX_debug_loc, scn);
10561 [ - + ]: 52 : if (data == NULL)
10562 : 0 : return;
10563 : :
10564 : 52 : fprintf (out, _("\
10565 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
10566 : : elf_ndxscn (scn), section_name (ebl, shdr),
10567 : 52 : (uint64_t) shdr->sh_offset);
10568 : :
10569 : 52 : sort_listptr (&known_locsptr, "loclistptr");
10570 : 52 : size_t listptr_idx = 0;
10571 : :
10572 [ + - ]: 52 : uint_fast8_t address_size = ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 4 : 8;
10573 : 52 : uint_fast8_t offset_size = 4;
10574 : :
10575 : 52 : bool first = true;
10576 : 52 : Dwarf_Addr base = 0;
10577 : 52 : unsigned char *readp = data->d_buf;
10578 : 52 : unsigned char *const endp = (unsigned char *) data->d_buf + data->d_size;
10579 : 52 : Dwarf_CU *last_cu = NULL;
10580 [ + + ]: 490 : while (readp < endp)
10581 : : {
10582 : 438 : ptrdiff_t offset = readp - (unsigned char *) data->d_buf;
10583 : 438 : Dwarf_CU *cu = last_cu;
10584 : 438 : unsigned int attr = 0;
10585 : :
10586 [ + + - + ]: 438 : if (first && skip_listptr_hole (&known_locsptr, &listptr_idx,
10587 : : &address_size, &offset_size, &base,
10588 : : &cu, offset, &readp, endp, &attr))
10589 : 0 : continue;
10590 : :
10591 [ + + ]: 438 : if (last_cu != cu)
10592 : : {
10593 : 60 : Dwarf_Die cudie;
10594 [ - + ]: 60 : if (dwarf_cu_die (cu, &cudie,
10595 : : NULL, NULL, NULL, NULL,
10596 : : NULL, NULL) == NULL)
10597 : 0 : fprintf (out, _("\n Unknown CU base: "));
10598 : : else
10599 : 60 : fprintf (out, _("\n CU [%6" PRIx64 "] base: "),
10600 : : dwarf_dieoffset (&cudie));
10601 : 60 : print_dwarf_addr (dwflmod, address_size, base, base, out);
10602 : 60 : fprintf (out, "\n");
10603 : : }
10604 : 438 : last_cu = cu;
10605 : :
10606 [ - + ]: 438 : if (attr == DW_AT_GNU_locviews)
10607 : 0 : {
10608 : 0 : Dwarf_Off next_off = next_listptr_offset (&known_locsptr,
10609 : : &listptr_idx, offset);
10610 : 0 : const unsigned char *locp = readp;
10611 : 0 : const unsigned char *locendp;
10612 [ # # ]: 0 : if (next_off == 0
10613 : 0 : || next_off > (size_t) (endp
10614 [ # # ]: 0 : - (const unsigned char *) data->d_buf))
10615 : 0 : locendp = endp;
10616 : : else
10617 : 0 : locendp = (const unsigned char *) data->d_buf + next_off;
10618 : :
10619 [ # # ]: 0 : while (locp < locendp)
10620 : : {
10621 : 0 : uint64_t v1, v2;
10622 : 0 : get_uleb128 (v1, locp, locendp);
10623 [ # # ]: 0 : if (locp >= locendp)
10624 : : {
10625 : 0 : fprintf (out, _(" [%6tx] <INVALID DATA>\n"), offset);
10626 : 0 : break;
10627 : : }
10628 : 0 : get_uleb128 (v2, locp, locendp);
10629 [ # # ]: 0 : if (first) /* First view pair in a list. */
10630 : 0 : fprintf (out, " [%6tx] ", offset);
10631 : : else
10632 : 0 : fprintf (out, " ");
10633 : 0 : fprintf (out, "view pair %" PRId64 ", %" PRId64 "\n", v1, v2);
10634 : 0 : first = false;
10635 : : }
10636 : :
10637 : 0 : first = true;
10638 : 0 : readp = (unsigned char *) locendp;
10639 : 0 : continue;
10640 : : }
10641 : :
10642 : : /* GNU DebugFission encoded addresses as addrx. */
10643 : 1194 : bool is_debugfission = ((cu != NULL
10644 [ # # ]: 0 : || split_dwarf_cu_base (dbg, &cu, &base))
10645 [ - + + - ]: 438 : && (cu->version < 5
10646 [ + + ]: 438 : && cu->unit_type == DW_UT_split_compile));
10647 : 318 : if (!is_debugfission
10648 [ - + ]: 318 : && unlikely (data->d_size - offset < (size_t) address_size * 2))
10649 : : {
10650 : 0 : invalid_data:
10651 : 0 : fprintf (out, _(" [%6tx] <INVALID DATA>\n"), offset);
10652 : 0 : break;
10653 : : }
10654 : :
10655 : 438 : Dwarf_Addr begin;
10656 : 438 : Dwarf_Addr end;
10657 : 438 : bool use_base = true;
10658 [ + + ]: 438 : if (is_debugfission)
10659 : : {
10660 : 120 : const unsigned char *locp = readp;
10661 : 120 : const unsigned char *locendp = readp + data->d_size;
10662 [ - + ]: 120 : if (locp >= locendp)
10663 : 0 : goto invalid_data;
10664 : :
10665 : 120 : Dwarf_Word idx;
10666 : 120 : unsigned char code = *locp++;
10667 [ + - - + : 120 : switch (code)
- ]
10668 : : {
10669 : 40 : case DW_LLE_GNU_end_of_list_entry:
10670 : 40 : begin = 0;
10671 : 40 : end = 0;
10672 : 40 : break;
10673 : :
10674 : 0 : case DW_LLE_GNU_base_address_selection_entry:
10675 [ # # ]: 0 : if (locp >= locendp)
10676 : 0 : goto invalid_data;
10677 : 0 : begin = (Dwarf_Addr) -1;
10678 : 0 : get_uleb128 (idx, locp, locendp);
10679 [ # # ]: 0 : if (get_indexed_addr (cu, idx, &end) != 0)
10680 : 0 : end = idx; /* ... */
10681 : : break;
10682 : :
10683 : 0 : case DW_LLE_GNU_start_end_entry:
10684 [ # # ]: 0 : if (locp >= locendp)
10685 : 0 : goto invalid_data;
10686 : 0 : get_uleb128 (idx, locp, locendp);
10687 [ # # ]: 0 : if (get_indexed_addr (cu, idx, &begin) != 0)
10688 : 0 : begin = idx; /* ... */
10689 [ # # ]: 0 : if (locp >= locendp)
10690 : 0 : goto invalid_data;
10691 : 0 : get_uleb128 (idx, locp, locendp);
10692 [ # # ]: 0 : if (get_indexed_addr (cu, idx, &end) != 0)
10693 : 0 : end = idx; /* ... */
10694 : : use_base = false;
10695 : : break;
10696 : :
10697 : 80 : case DW_LLE_GNU_start_length_entry:
10698 [ - + ]: 80 : if (locp >= locendp)
10699 : 0 : goto invalid_data;
10700 : 80 : get_uleb128 (idx, locp, locendp);
10701 [ - + ]: 80 : if (get_indexed_addr (cu, idx, &begin) != 0)
10702 : 0 : begin = idx; /* ... */
10703 [ - + ]: 80 : if (locendp - locp < 4)
10704 : 0 : goto invalid_data;
10705 [ - + ]: 80 : end = read_4ubyte_unaligned_inc (dbg, locp);
10706 : 80 : end += begin;
10707 : 80 : use_base = false;
10708 : 80 : break;
10709 : :
10710 : 0 : default:
10711 : 0 : goto invalid_data;
10712 : : }
10713 : :
10714 : 120 : readp = (unsigned char *) locp;
10715 : : }
10716 [ + - ]: 318 : else if (address_size == 8)
10717 : : {
10718 [ + + ]: 318 : begin = read_8ubyte_unaligned_inc (dbg, readp);
10719 [ + + ]: 318 : end = read_8ubyte_unaligned_inc (dbg, readp);
10720 : : }
10721 : : else
10722 : : {
10723 [ # # ]: 0 : begin = read_4ubyte_unaligned_inc (dbg, readp);
10724 [ # # ]: 0 : end = read_4ubyte_unaligned_inc (dbg, readp);
10725 [ # # ]: 0 : if (begin == (Dwarf_Addr) (uint32_t) -1)
10726 : 0 : begin = (Dwarf_Addr) -1l;
10727 : : }
10728 : :
10729 [ - + ]: 438 : if (begin == (Dwarf_Addr) -1l) /* Base address entry. */
10730 : : {
10731 [ # # ]: 0 : if (first)
10732 : 0 : fprintf (out, " [%6tx] ", offset);
10733 : : else
10734 : 0 : fprintf (out, " ");
10735 : 0 : fputs (_("base address\n"), out);
10736 : 0 : fprintf (out, " ");
10737 : 0 : print_dwarf_addr (dwflmod, address_size, end, end, out);
10738 : 0 : fprintf (out, "\n");
10739 : 0 : base = end;
10740 : 0 : first = false;
10741 : : }
10742 [ + + + + ]: 438 : else if (begin == 0 && end == 0) /* End of list entry. */
10743 : : {
10744 [ - + ]: 148 : if (first)
10745 : 0 : fprintf (out, _(" [%6tx] empty list\n"), offset);
10746 : : first = true;
10747 : : }
10748 : : else
10749 : : {
10750 : : /* We have a location expression entry. */
10751 [ + + ]: 290 : uint_fast16_t len = read_2ubyte_unaligned_inc (dbg, readp);
10752 : :
10753 [ + + ]: 290 : if (first) /* First entry in a list. */
10754 : 148 : fprintf (out, " [%6tx] ", offset);
10755 : : else
10756 : 142 : fprintf (out, " ");
10757 : :
10758 : 290 : fprintf (out, "range %" PRIx64 ", %" PRIx64 "\n", begin, end);
10759 [ + + ]: 290 : if (! print_unresolved_addresses)
10760 : : {
10761 : 230 : Dwarf_Addr dab = use_base ? base + begin : begin;
10762 : 230 : Dwarf_Addr dae = use_base ? base + end : end;
10763 : 230 : fprintf (out, " ");
10764 : 230 : print_dwarf_addr (dwflmod, address_size, dab, dab, out);
10765 : 230 : fprintf (out, "..\n ");
10766 : 230 : print_dwarf_addr (dwflmod, address_size, dae - 1, dae, out);
10767 : 230 : fprintf (out, "\n");
10768 : : }
10769 : :
10770 [ - + ]: 290 : if (endp - readp <= (ptrdiff_t) len)
10771 : : {
10772 : 0 : fputs (_(" <INVALID DATA>\n"), out);
10773 : 0 : break;
10774 : : }
10775 : :
10776 [ + - ]: 580 : print_ops (dwflmod, dbg, 11, 11,
10777 : 290 : cu != NULL ? cu->version : 3,
10778 : : address_size, offset_size, cu, len, readp, out);
10779 : :
10780 : 290 : first = false;
10781 : 290 : readp += len;
10782 : : }
10783 : : }
10784 : : }
10785 : :
10786 : : struct mac_culist
10787 : : {
10788 : : Dwarf_Die die;
10789 : : Dwarf_Off offset;
10790 : : Dwarf_Files *files;
10791 : : struct mac_culist *next;
10792 : : };
10793 : :
10794 : :
10795 : : static int
10796 : 0 : mac_compare (const void *p1, const void *p2)
10797 : : {
10798 : 0 : struct mac_culist *m1 = (struct mac_culist *) p1;
10799 : 0 : struct mac_culist *m2 = (struct mac_culist *) p2;
10800 : :
10801 [ # # ]: 0 : if (m1->offset < m2->offset)
10802 : : return -1;
10803 [ # # ]: 0 : if (m1->offset > m2->offset)
10804 : 0 : return 1;
10805 : : return 0;
10806 : : }
10807 : :
10808 : :
10809 : : static void
10810 : 0 : print_debug_macinfo_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
10811 : : Ebl *ebl,
10812 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
10813 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg,
10814 : : FILE *out)
10815 : : {
10816 : 0 : Elf_Data *data = get_debug_elf_data (dbg, ebl, IDX_debug_macinfo, scn);
10817 [ # # ]: 0 : if (data == NULL)
10818 : 0 : return;
10819 : :
10820 : 0 : fprintf (out, _("\
10821 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
10822 : : elf_ndxscn (scn), section_name (ebl, shdr),
10823 : 0 : (uint64_t) shdr->sh_offset);
10824 : 0 : fputc ('\n', out);
10825 : :
10826 : : /* There is no function in libdw to iterate over the raw content of
10827 : : the section but it is easy enough to do. */
10828 : :
10829 : : /* Get the source file information for all CUs. */
10830 : 0 : Dwarf_Off offset;
10831 : 0 : Dwarf_Off ncu = 0;
10832 : 0 : size_t hsize;
10833 : 0 : struct mac_culist *culist = NULL;
10834 : 0 : size_t nculist = 0;
10835 [ # # ]: 0 : while (dwarf_nextcu (dbg, offset = ncu, &ncu, &hsize, NULL, NULL, NULL) == 0)
10836 : : {
10837 : 0 : Dwarf_Die cudie;
10838 [ # # ]: 0 : if (dwarf_offdie (dbg, offset + hsize, &cudie) == NULL)
10839 : 0 : continue;
10840 : :
10841 : 0 : Dwarf_Attribute attr;
10842 [ # # ]: 0 : if (dwarf_attr (&cudie, DW_AT_macro_info, &attr) == NULL)
10843 : 0 : continue;
10844 : :
10845 : 0 : Dwarf_Word macoff;
10846 [ # # ]: 0 : if (dwarf_formudata (&attr, &macoff) != 0)
10847 : 0 : continue;
10848 : :
10849 : 0 : struct mac_culist *newp = (struct mac_culist *) alloca (sizeof (*newp));
10850 : 0 : newp->die = cudie;
10851 : 0 : newp->offset = macoff;
10852 : 0 : newp->files = NULL;
10853 : 0 : newp->next = culist;
10854 : 0 : culist = newp;
10855 : 0 : ++nculist;
10856 : : }
10857 : :
10858 : : /* Convert the list into an array for easier consumption. */
10859 : 0 : struct mac_culist *cus = (struct mac_culist *) alloca ((nculist + 1)
10860 : : * sizeof (*cus));
10861 : : /* Add sentinel. */
10862 : 0 : cus[nculist].offset = data->d_size;
10863 : 0 : cus[nculist].files = (Dwarf_Files *) -1l;
10864 [ # # ]: 0 : if (nculist > 0)
10865 : : {
10866 [ # # ]: 0 : for (size_t cnt = nculist - 1; culist != NULL; --cnt)
10867 : : {
10868 [ # # ]: 0 : assert (cnt < nculist);
10869 : 0 : cus[cnt] = *culist;
10870 : 0 : culist = culist->next;
10871 : : }
10872 : :
10873 : : /* Sort the array according to the offset in the .debug_macinfo
10874 : : section. Note we keep the sentinel at the end. */
10875 : 0 : qsort (cus, nculist, sizeof (*cus), mac_compare);
10876 : : }
10877 : :
10878 : 0 : const unsigned char *readp = (const unsigned char *) data->d_buf;
10879 : 0 : const unsigned char *readendp = readp + data->d_size;
10880 : 0 : int level = 1;
10881 : :
10882 [ # # ]: 0 : while (readp < readendp)
10883 : : {
10884 : 0 : unsigned int opcode = *readp++;
10885 : 0 : unsigned int u128;
10886 : 0 : unsigned int u128_2;
10887 : 0 : const unsigned char *endp;
10888 : :
10889 [ # # # # ]: 0 : switch (opcode)
10890 : : {
10891 : 0 : case DW_MACINFO_define:
10892 : : case DW_MACINFO_undef:
10893 : : case DW_MACINFO_vendor_ext:
10894 : : /* For the first two opcodes the parameters are
10895 : : line, string
10896 : : For the latter
10897 : : number, string.
10898 : : We can treat these cases together. */
10899 : 0 : get_uleb128 (u128, readp, readendp);
10900 : :
10901 : 0 : endp = memchr (readp, '\0', readendp - readp);
10902 [ # # ]: 0 : if (unlikely (endp == NULL))
10903 : : {
10904 : 0 : fprintf (out, _("\
10905 : : %*s*** non-terminated string at end of section"),
10906 : : level, "");
10907 : 0 : return;
10908 : : }
10909 : :
10910 [ # # ]: 0 : if (opcode == DW_MACINFO_define)
10911 : 0 : fprintf (out, "%*s#define %s, line %u\n",
10912 : : level, "", (char *) readp, u128);
10913 [ # # ]: 0 : else if (opcode == DW_MACINFO_undef)
10914 : 0 : fprintf (out, "%*s#undef %s, line %u\n",
10915 : : level, "", (char *) readp, u128);
10916 : : else
10917 : 0 : fprintf (out, " #vendor-ext %s, number %u\n", (char *) readp, u128);
10918 : :
10919 : 0 : readp = endp + 1;
10920 : 0 : break;
10921 : :
10922 : 0 : case DW_MACINFO_start_file:
10923 : : /* The two parameters are line and file index, in this order. */
10924 : 0 : get_uleb128 (u128, readp, readendp);
10925 [ # # ]: 0 : if (readendp - readp < 1)
10926 : : {
10927 : 0 : fprintf (out, _("\
10928 : : %*s*** missing DW_MACINFO_start_file argument at end of section"),
10929 : : level, "");
10930 : 0 : return;
10931 : : }
10932 : 0 : get_uleb128 (u128_2, readp, readendp);
10933 : :
10934 : : /* Find the CU DIE for this file. */
10935 : 0 : size_t macoff = readp - (const unsigned char *) data->d_buf;
10936 : 0 : const char *fname = "???";
10937 [ # # # # ]: 0 : if (macoff >= cus[0].offset && cus[0].offset != data->d_size)
10938 : : {
10939 [ # # # # ]: 0 : while (macoff >= cus[1].offset && cus[1].offset != data->d_size)
10940 : 0 : ++cus;
10941 : :
10942 [ # # ]: 0 : if (cus[0].files == NULL
10943 [ # # ]: 0 : && dwarf_getsrcfiles (&cus[0].die, &cus[0].files, NULL) != 0)
10944 : 0 : cus[0].files = (Dwarf_Files *) -1l;
10945 : :
10946 [ # # ]: 0 : if (cus[0].files != (Dwarf_Files *) -1l)
10947 : 0 : fname = (dwarf_filesrc (cus[0].files, u128_2, NULL, NULL)
10948 [ # # ]: 0 : ?: "???");
10949 : : }
10950 : :
10951 : 0 : fprintf (out, "%*sstart_file %u, [%u] %s\n",
10952 : : level, "", u128, u128_2, fname);
10953 : 0 : ++level;
10954 : 0 : break;
10955 : :
10956 : 0 : case DW_MACINFO_end_file:
10957 : 0 : --level;
10958 : 0 : fprintf (out, "%*send_file\n", level, "");
10959 : : /* Nothing more to do. */
10960 : 0 : break;
10961 : :
10962 : 0 : default:
10963 : : // XXX gcc seems to generate files with a trailing zero.
10964 [ # # # # ]: 0 : if (unlikely (opcode != 0 || readp != readendp))
10965 : 0 : fprintf (out, "%*s*** invalid opcode %u\n", level, "", opcode);
10966 : : break;
10967 : : }
10968 : : }
10969 : : }
10970 : :
10971 : :
10972 : : static void
10973 : 8 : print_debug_macro_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
10974 : : Ebl *ebl,
10975 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
10976 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg,
10977 : : FILE *out)
10978 : : {
10979 : 8 : Elf_Data *data = get_debug_elf_data (dbg, ebl, IDX_debug_macro, scn);
10980 [ + - ]: 8 : if (data == NULL)
10981 : 0 : return;
10982 : :
10983 : 8 : fprintf (out, _("\
10984 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
10985 : : elf_ndxscn (scn), section_name (ebl, shdr),
10986 : 8 : (uint64_t) shdr->sh_offset);
10987 : 8 : fputc ('\n', out);
10988 : :
10989 : : /* Get the source file information for all CUs. Uses same
10990 : : datastructure as macinfo. But uses offset field to directly
10991 : : match .debug_line offset. And just stored in a list. */
10992 : 8 : Dwarf_Off offset;
10993 : 8 : Dwarf_Off ncu = 0;
10994 : 8 : size_t hsize;
10995 : 8 : struct mac_culist *culist = NULL;
10996 [ + + ]: 20 : while (dwarf_nextcu (dbg, offset = ncu, &ncu, &hsize, NULL, NULL, NULL) == 0)
10997 : : {
10998 : 12 : Dwarf_Die cudie;
10999 [ - + ]: 12 : if (dwarf_offdie (dbg, offset + hsize, &cudie) == NULL)
11000 : 0 : continue;
11001 : :
11002 : 12 : Dwarf_Attribute attr;
11003 [ - + ]: 12 : if (dwarf_attr (&cudie, DW_AT_stmt_list, &attr) == NULL)
11004 : 0 : continue;
11005 : :
11006 : 12 : Dwarf_Word lineoff;
11007 [ - + ]: 12 : if (dwarf_formudata (&attr, &lineoff) != 0)
11008 : 0 : continue;
11009 : :
11010 : 12 : struct mac_culist *newp = (struct mac_culist *) alloca (sizeof (*newp));
11011 : 12 : newp->die = cudie;
11012 : 12 : newp->offset = lineoff;
11013 : 12 : newp->files = NULL;
11014 : 12 : newp->next = culist;
11015 : 12 : culist = newp;
11016 : : }
11017 : :
11018 : 8 : const unsigned char *readp = (const unsigned char *) data->d_buf;
11019 : 8 : const unsigned char *readendp = readp + data->d_size;
11020 : :
11021 [ + + ]: 28 : while (readp < readendp)
11022 : : {
11023 : 40 : fprintf (out, _(" Offset: 0x%" PRIx64 "\n"),
11024 : 20 : (uint64_t) (readp - (const unsigned char *) data->d_buf));
11025 : :
11026 : : // Header, 2 byte version, 1 byte flag, optional .debug_line offset,
11027 : : // optional vendor extension macro entry table.
11028 [ - + ]: 20 : if (readp + 2 > readendp)
11029 : : {
11030 : 0 : invalid_data:
11031 : 0 : error (0, 0, _("invalid data"));
11032 : 0 : return;
11033 : : }
11034 [ - + ]: 20 : const uint16_t vers = read_2ubyte_unaligned_inc (dbg, readp);
11035 : 20 : fprintf (out, _(" Version: %" PRIu16 "\n"), vers);
11036 : :
11037 : : // Version 4 is the GNU extension for DWARF4. DWARF5 will use version
11038 : : // 5 when it gets standardized.
11039 [ - + ]: 20 : if (vers != 4 && vers != 5)
11040 : : {
11041 : 0 : fprintf (out, _(" unknown version, cannot parse section\n"));
11042 : 0 : return;
11043 : : }
11044 : :
11045 [ - + ]: 20 : if (readp + 1 > readendp)
11046 : 0 : goto invalid_data;
11047 : 20 : const unsigned char flag = *readp++;
11048 : 20 : fprintf (out, _(" Flag: 0x%" PRIx8), flag);
11049 [ + + ]: 20 : if (flag != 0)
11050 : : {
11051 : 12 : fprintf (out, " (");
11052 [ - + ]: 12 : if ((flag & 0x01) != 0)
11053 : : {
11054 : 0 : fprintf (out, "offset_size");
11055 [ # # ]: 0 : if ((flag & 0xFE) != 0)
11056 : 0 : fprintf (out, ", ");
11057 : : }
11058 [ + - ]: 12 : if ((flag & 0x02) != 0)
11059 : : {
11060 : 12 : fprintf (out, "debug_line_offset");
11061 [ - + ]: 12 : if ((flag & 0xFC) != 0)
11062 : 0 : fprintf (out, ", ");
11063 : : }
11064 [ # # ]: 0 : if ((flag & 0x04) != 0)
11065 : : {
11066 : 0 : fprintf (out, "operands_table");
11067 [ # # ]: 0 : if ((flag & 0xF8) != 0)
11068 : 0 : fprintf (out, ", ");
11069 : : }
11070 [ - + ]: 12 : if ((flag & 0xF8) != 0)
11071 : 0 : fprintf (out, "unknown");
11072 : 12 : fprintf (out, ")");
11073 : : }
11074 : 20 : fprintf (out, "\n");
11075 : :
11076 [ + - ]: 20 : unsigned int offset_len = (flag & 0x01) ? 8 : 4;
11077 : 20 : fprintf (out, _(" Offset length: %" PRIu8 "\n"), offset_len);
11078 : 20 : Dwarf_Off line_offset = -1;
11079 [ + + ]: 20 : if (flag & 0x02)
11080 : : {
11081 [ - + ]: 12 : if (offset_len == 8)
11082 [ # # ]: 0 : line_offset = read_8ubyte_unaligned_inc (dbg, readp);
11083 : : else
11084 [ - + ]: 12 : line_offset = read_4ubyte_unaligned_inc (dbg, readp);
11085 : 12 : fprintf (out, _(" .debug_line offset: 0x%" PRIx64 "\n"),
11086 : : line_offset);
11087 : : }
11088 : :
11089 : 12 : struct mac_culist *cu = NULL;
11090 [ + - ]: 12 : if (line_offset != (Dwarf_Off) -1)
11091 : : {
11092 : : cu = culist;
11093 [ + - + + ]: 16 : while (cu != NULL && line_offset != cu->offset)
11094 : 4 : cu = cu->next;
11095 : : }
11096 : :
11097 [ - + ]: 20 : Dwarf_Off str_offsets_base = str_offsets_base_off (dbg, (cu != NULL
11098 : : ? cu->die.cu
11099 : : : NULL));
11100 : :
11101 : 20 : const unsigned char *vendor[DW_MACRO_hi_user - DW_MACRO_lo_user + 1];
11102 [ - + ]: 20 : memset (vendor, 0, sizeof vendor);
11103 [ - + ]: 20 : if (flag & 0x04)
11104 : : {
11105 : : // 1 byte length, for each item, 1 byte opcode, uleb128 number
11106 : : // of arguments, for each argument 1 byte form code.
11107 [ # # ]: 0 : if (readp + 1 > readendp)
11108 : 0 : goto invalid_data;
11109 : 0 : unsigned int tlen = *readp++;
11110 : 0 : fprintf (out, _(" extension opcode table, %" PRIu8 " items:\n"),
11111 : : tlen);
11112 [ # # ]: 0 : for (unsigned int i = 0; i < tlen; i++)
11113 : : {
11114 [ # # ]: 0 : if (readp + 1 > readendp)
11115 : 0 : goto invalid_data;
11116 : 0 : unsigned int opcode = *readp++;
11117 : 0 : fprintf (out, _(" [%" PRIx8 "]"), opcode);
11118 : 0 : if (opcode < DW_MACRO_lo_user
11119 [ # # ]: 0 : || opcode > DW_MACRO_hi_user)
11120 : 0 : goto invalid_data;
11121 : : // Record the start of description for this vendor opcode.
11122 : : // uleb128 nr args, 1 byte per arg form.
11123 : 0 : vendor[opcode - DW_MACRO_lo_user] = readp;
11124 [ # # ]: 0 : if (readp + 1 > readendp)
11125 : 0 : goto invalid_data;
11126 : 0 : unsigned int args = *readp++;
11127 [ # # ]: 0 : if (args > 0)
11128 : : {
11129 : 0 : fprintf (out, _(" %" PRIu8 " arguments:"), args);
11130 : 0 : while (args > 0)
11131 : : {
11132 [ # # ]: 0 : if (readp + 1 > readendp)
11133 : 0 : goto invalid_data;
11134 : 0 : unsigned int form = *readp++;
11135 : 0 : fprintf (out, " %s", dwarf_form_name (form));
11136 [ # # ]: 0 : if (! libdw_valid_user_form (form))
11137 : 0 : goto invalid_data;
11138 : 0 : args--;
11139 [ # # ]: 0 : if (args > 0)
11140 : 0 : fputc (',', out);
11141 : : }
11142 : : }
11143 : : else
11144 : 0 : fprintf (out, _(" no arguments."));
11145 : 0 : fputc ('\n', out);
11146 : : }
11147 : : }
11148 : 20 : fputc ('\n', out);
11149 : :
11150 : 20 : int level = 1;
11151 [ - + ]: 20 : if (readp + 1 > readendp)
11152 : 0 : goto invalid_data;
11153 : 20 : unsigned int opcode = *readp++;
11154 [ + + ]: 3046 : while (opcode != 0)
11155 : : {
11156 : 3026 : unsigned int u128;
11157 : 3026 : unsigned int u128_2;
11158 : 3026 : const unsigned char *endp;
11159 : 3026 : uint64_t off;
11160 : :
11161 [ + + + - : 3026 : switch (opcode)
+ + + - -
- + + - ]
11162 : : {
11163 : 20 : case DW_MACRO_start_file:
11164 : 20 : get_uleb128 (u128, readp, readendp);
11165 [ - + ]: 20 : if (readp >= readendp)
11166 : 0 : goto invalid_data;
11167 : 20 : get_uleb128 (u128_2, readp, readendp);
11168 : :
11169 : : /* Find the CU DIE that matches this line offset. */
11170 : 20 : const char *fname = "???";
11171 [ - + ]: 20 : if (cu != NULL)
11172 : : {
11173 [ + + ]: 20 : if (cu->files == NULL
11174 [ - + ]: 12 : && dwarf_getsrcfiles (&cu->die, &cu->files,
11175 : : NULL) != 0)
11176 : 0 : cu->files = (Dwarf_Files *) -1l;
11177 : :
11178 [ - + ]: 20 : if (cu->files != (Dwarf_Files *) -1l)
11179 : 20 : fname = (dwarf_filesrc (cu->files, u128_2,
11180 [ - + ]: 20 : NULL, NULL) ?: "???");
11181 : : }
11182 : 20 : fprintf (out, "%*sstart_file %u, [%u] %s\n",
11183 : : level, "", u128, u128_2, fname);
11184 : 20 : ++level;
11185 : 20 : break;
11186 : :
11187 : 20 : case DW_MACRO_end_file:
11188 : 20 : --level;
11189 : 20 : fprintf (out, "%*send_file\n", level, "");
11190 : 20 : break;
11191 : :
11192 : 2 : case DW_MACRO_define:
11193 : 2 : get_uleb128 (u128, readp, readendp);
11194 : 2 : endp = memchr (readp, '\0', readendp - readp);
11195 [ - + ]: 2 : if (endp == NULL)
11196 : 0 : goto invalid_data;
11197 : 2 : fprintf (out, "%*s#define %s, line %u\n",
11198 : : level, "", readp, u128);
11199 : 2 : readp = endp + 1;
11200 : 2 : break;
11201 : :
11202 : 0 : case DW_MACRO_undef:
11203 : 0 : get_uleb128 (u128, readp, readendp);
11204 : 0 : endp = memchr (readp, '\0', readendp - readp);
11205 [ # # ]: 0 : if (endp == NULL)
11206 : 0 : goto invalid_data;
11207 : 0 : fprintf (out, "%*s#undef %s, line %u\n",
11208 : : level, "", readp, u128);
11209 : 0 : readp = endp + 1;
11210 : 0 : break;
11211 : :
11212 : 1414 : case DW_MACRO_define_strp:
11213 : 1414 : get_uleb128 (u128, readp, readendp);
11214 [ - + ]: 1414 : if (readp + offset_len > readendp)
11215 : 0 : goto invalid_data;
11216 [ - + ]: 1414 : if (offset_len == 8)
11217 [ # # ]: 0 : off = read_8ubyte_unaligned_inc (dbg, readp);
11218 : : else
11219 [ - + ]: 1414 : off = read_4ubyte_unaligned_inc (dbg, readp);
11220 : 1414 : fprintf (out, "%*s#define %s, line %u (indirect)\n",
11221 : : level, "", dwarf_getstring (dbg, off, NULL), u128);
11222 : 1414 : break;
11223 : :
11224 : 2 : case DW_MACRO_undef_strp:
11225 : 2 : get_uleb128 (u128, readp, readendp);
11226 [ - + ]: 2 : if (readp + offset_len > readendp)
11227 : 0 : goto invalid_data;
11228 [ - + ]: 2 : if (offset_len == 8)
11229 [ # # ]: 0 : off = read_8ubyte_unaligned_inc (dbg, readp);
11230 : : else
11231 [ - + ]: 2 : off = read_4ubyte_unaligned_inc (dbg, readp);
11232 : 2 : fprintf (out, "%*s#undef %s, line %u (indirect)\n",
11233 : : level, "", dwarf_getstring (dbg, off, NULL), u128);
11234 : 2 : break;
11235 : :
11236 : 12 : case DW_MACRO_import:
11237 [ - + ]: 12 : if (readp + offset_len > readendp)
11238 : 0 : goto invalid_data;
11239 [ - + ]: 12 : if (offset_len == 8)
11240 [ # # ]: 0 : off = read_8ubyte_unaligned_inc (dbg, readp);
11241 : : else
11242 [ - + ]: 12 : off = read_4ubyte_unaligned_inc (dbg, readp);
11243 : 12 : fprintf (out, "%*s#include offset 0x%" PRIx64 "\n",
11244 : : level, "", off);
11245 : 12 : break;
11246 : :
11247 : 0 : case DW_MACRO_define_sup:
11248 : 0 : get_uleb128 (u128, readp, readendp);
11249 : 0 : fprintf (out, "%*s#define ", level, "");
11250 : 0 : readp = print_form_data (dbg, DW_FORM_strp_sup,
11251 : : readp, readendp, offset_len,
11252 : : str_offsets_base, out);
11253 : 0 : fprintf (out, ", line %u (sup)\n", u128);
11254 : 0 : break;
11255 : :
11256 : 0 : case DW_MACRO_undef_sup:
11257 : 0 : get_uleb128 (u128, readp, readendp);
11258 : 0 : fprintf (out, "%*s#undef ", level, "");
11259 : 0 : readp = print_form_data (dbg, DW_FORM_strp_sup,
11260 : : readp, readendp, offset_len,
11261 : : str_offsets_base, out);
11262 : 0 : fprintf (out, ", line %u (sup)\n", u128);
11263 : 0 : break;
11264 : :
11265 : 0 : case DW_MACRO_import_sup:
11266 [ # # ]: 0 : if (readp + offset_len > readendp)
11267 : 0 : goto invalid_data;
11268 [ # # ]: 0 : if (offset_len == 8)
11269 [ # # ]: 0 : off = read_8ubyte_unaligned_inc (dbg, readp);
11270 : : else
11271 [ # # ]: 0 : off = read_4ubyte_unaligned_inc (dbg, readp);
11272 : : // XXX Needs support for reading from supplementary object file.
11273 : 0 : fprintf (out, "%*s#include offset 0x%" PRIx64 " (sup)\n",
11274 : : level, "", off);
11275 : 0 : break;
11276 : :
11277 : 1552 : case DW_MACRO_define_strx:
11278 : 1552 : get_uleb128 (u128, readp, readendp);
11279 : 1552 : fprintf (out, "%*s#define ", level, "");
11280 : 1552 : readp = print_form_data (dbg, DW_FORM_strx,
11281 : : readp, readendp, offset_len,
11282 : : str_offsets_base, out);
11283 : 1552 : fprintf (out, ", line %u (strx)\n", u128);
11284 : 1552 : break;
11285 : :
11286 : 4 : case DW_MACRO_undef_strx:
11287 : 4 : get_uleb128 (u128, readp, readendp);
11288 : 4 : fprintf (out, "%*s#undef ", level, "");
11289 : 4 : readp = print_form_data (dbg, DW_FORM_strx,
11290 : : readp, readendp, offset_len,
11291 : : str_offsets_base, out);
11292 : 4 : fprintf (out, ", line %u (strx)\n", u128);
11293 : 4 : break;
11294 : :
11295 : : default:
11296 : 0 : fprintf (out, "%*svendor opcode 0x%" PRIx8, level, "", opcode);
11297 [ # # ]: 0 : if (opcode < DW_MACRO_lo_user
11298 : : || opcode > DW_MACRO_lo_user
11299 [ # # ]: 0 : || vendor[opcode - DW_MACRO_lo_user] == NULL)
11300 : 0 : goto invalid_data;
11301 : :
11302 : 0 : const unsigned char *op_desc;
11303 : 0 : op_desc = vendor[opcode - DW_MACRO_lo_user];
11304 : :
11305 : : // Just skip the arguments, we cannot really interpret them,
11306 : : // but print as much as we can.
11307 : 0 : unsigned int args = *op_desc++;
11308 [ # # # # ]: 0 : while (args > 0 && readp < readendp)
11309 : : {
11310 : 0 : unsigned int form = *op_desc++;
11311 : 0 : readp = print_form_data (dbg, form, readp, readendp,
11312 : : offset_len, str_offsets_base, out);
11313 : 0 : args--;
11314 [ # # ]: 0 : if (args > 0)
11315 : 0 : fprintf (out, ", ");
11316 : : }
11317 : 0 : fputc ('\n', out);
11318 : : }
11319 : :
11320 [ - + ]: 3026 : if (readp + 1 > readendp)
11321 : 0 : goto invalid_data;
11322 : 3026 : opcode = *readp++;
11323 [ + + ]: 3026 : if (opcode == 0)
11324 : 20 : fputc ('\n', out);
11325 : : }
11326 : : }
11327 : : }
11328 : :
11329 : :
11330 : : typedef struct {
11331 : : int n;
11332 : : FILE *out;
11333 : : } pubnames_arg;
11334 : :
11335 : : /* Callback for printing global names. */
11336 : : static int
11337 : 68 : print_pubnames (Dwarf *dbg __attribute__ ((unused)), Dwarf_Global *global,
11338 : : void *arg)
11339 : : {
11340 : 68 : pubnames_arg *p = (pubnames_arg *) arg;
11341 : :
11342 : 136 : fprintf (p->out, _(" [%5d] DIE offset: %6" PRId64
11343 : : ", CU DIE offset: %6" PRId64 ", name: %s\n"),
11344 : 68 : (p->n)++, global->die_offset, global->cu_offset, global->name);
11345 : :
11346 : 68 : return 0;
11347 : : }
11348 : :
11349 : :
11350 : : /* Print the known exported symbols in the DWARF section '.debug_pubnames'. */
11351 : : static void
11352 : 16 : print_debug_pubnames_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
11353 : : Ebl *ebl,
11354 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
11355 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg,
11356 : : FILE *out)
11357 : : {
11358 : : /* Check section actually exists. */
11359 [ - + ]: 16 : if (get_debug_elf_data (dbg, ebl, IDX_debug_pubnames, scn) == NULL)
11360 : 0 : return;
11361 : :
11362 : 16 : fprintf (out, _("\nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
11363 : : elf_ndxscn (scn), section_name (ebl, shdr),
11364 : 16 : (uint64_t) shdr->sh_offset);
11365 : :
11366 : 16 : pubnames_arg arg = { 0, out };
11367 : 16 : (void) dwarf_getpubnames (dbg, print_pubnames, &arg, 0);
11368 : : }
11369 : :
11370 : : /* Print the content of the DWARF string section '.debug_str'
11371 : : or 'debug_line_str'. */
11372 : : static void
11373 : 134 : print_debug_str_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
11374 : : Ebl *ebl,
11375 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
11376 : : Elf_Scn *scn, GElf_Shdr *shdr,
11377 : : Dwarf *dbg __attribute__ ((unused)),
11378 : : FILE *out)
11379 : : {
11380 : 134 : const char *name = section_name (ebl, shdr);
11381 [ + + ]: 134 : int idx = ((name != NULL && strstr (name, "debug_line_str") != NULL)
11382 : : ? IDX_debug_line_str : IDX_debug_str);
11383 : 134 : Elf_Data *data = get_debug_elf_data (dbg, ebl, idx, scn);
11384 [ + - ]: 134 : if (data == NULL)
11385 : : return;
11386 : :
11387 : 134 : const size_t sh_size = data->d_size;
11388 : :
11389 : : /* Compute floor(log16(shdr->sh_size)). */
11390 : 134 : GElf_Addr tmp = sh_size;
11391 : 134 : int digits = 1;
11392 [ + + ]: 470 : while (tmp >= 16)
11393 : : {
11394 : 336 : ++digits;
11395 : 336 : tmp >>= 4;
11396 : : }
11397 : 134 : digits = MAX (4, digits);
11398 : :
11399 : 134 : fprintf (out, _("\nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"
11400 : : " %*s String\n"),
11401 : : elf_ndxscn (scn),
11402 : 134 : section_name (ebl, shdr), (uint64_t) shdr->sh_offset,
11403 : : /* TRANS: the debugstr| prefix makes the string unique. */
11404 [ + - ]: 134 : digits + 2, sgettext ("debugstr|Offset"));
11405 : :
11406 : 134 : Dwarf_Off offset = 0;
11407 [ + + ]: 173512 : while (offset < sh_size)
11408 : : {
11409 : 173378 : size_t len;
11410 : 173378 : const char *str = (const char *) data->d_buf + offset;
11411 : 173378 : const char *endp = memchr (str, '\0', sh_size - offset);
11412 [ - + ]: 173378 : if (unlikely (endp == NULL))
11413 : : {
11414 : 0 : fprintf (out, _(" *** error, missing string terminator\n"));
11415 : 0 : break;
11416 : : }
11417 : :
11418 : 173378 : fprintf (out, " [%*" PRIx64 "] \"%s\"\n",
11419 : : digits, (uint64_t) offset, str);
11420 : 173378 : len = endp - str;
11421 : 173378 : offset += len + 1;
11422 : : }
11423 : : }
11424 : :
11425 : : static void
11426 : 12 : print_debug_str_offsets_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
11427 : : Ebl *ebl,
11428 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
11429 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg,
11430 : : FILE *out)
11431 : : {
11432 : 12 : Elf_Data *data = get_debug_elf_data (dbg, ebl, IDX_debug_str_offsets, scn);
11433 [ + - ]: 12 : if (data == NULL)
11434 : : return;
11435 : :
11436 : 12 : fprintf (out, _("\
11437 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
11438 : : elf_ndxscn (scn), section_name (ebl, shdr),
11439 : 12 : (uint64_t) shdr->sh_offset);
11440 : :
11441 [ + - ]: 12 : if (shdr->sh_size == 0)
11442 : : return;
11443 : :
11444 : 12 : size_t idx = 0;
11445 : 12 : sort_listptr (&known_stroffbases, "str_offsets");
11446 : :
11447 : 12 : const unsigned char *start = (const unsigned char *) data->d_buf;
11448 : 12 : const unsigned char *readp = start;
11449 : 12 : const unsigned char *readendp = ((const unsigned char *) data->d_buf
11450 : 12 : + data->d_size);
11451 : :
11452 [ + + ]: 28 : while (readp < readendp)
11453 : : {
11454 : : /* Most string offset tables will have a header. For split
11455 : : dwarf unit GNU DebugFission didn't add one. But they were
11456 : : also only defined for split units (main or skeleton units
11457 : : didn't have indirect strings). So if we don't have a
11458 : : DW_AT_str_offsets_base at all and this is offset zero, then
11459 : : just start printing offsets immediately, if this is a .dwo
11460 : : section. */
11461 : 16 : Dwarf_Off off = (Dwarf_Off) (readp
11462 : 16 : - (const unsigned char *) data->d_buf);
11463 : :
11464 : 16 : fprintf (out, "Table at offset %" PRIx64 " ", off);
11465 : :
11466 : : /* Find the first CU that could plausibly be associated with
11467 : : this string offsets index. Skip CUs that point
11468 : : str_offsets_base before this table. */
11469 [ + + ]: 16 : struct listptr *listptr = get_listptr (&known_stroffbases, idx);
11470 [ + + ]: 12 : while (listptr != NULL && listptr->offset < off)
11471 [ + - + + ]: 24 : listptr = get_listptr (&known_stroffbases, ++idx);
11472 : :
11473 : 16 : const unsigned char *next_unitp = readendp;
11474 : 16 : uint8_t offset_size;
11475 : 16 : bool has_header;
11476 [ + + ]: 16 : if (listptr == NULL)
11477 : : {
11478 : : /* This can happen for .dwo files. There is only an header
11479 : : in the case this is a version 5 split DWARF file. */
11480 : 8 : Dwarf_CU *cu;
11481 : 8 : uint8_t unit_type;
11482 [ - + ]: 8 : if (dwarf_get_units (dbg, NULL, &cu, NULL, &unit_type,
11483 : : NULL, NULL) != 0)
11484 : : {
11485 : 0 : error (0, 0, "Warning: Cannot find any DWARF unit.");
11486 : : /* Just guess some values. */
11487 : 0 : has_header = false;
11488 : 0 : offset_size = 4;
11489 : : }
11490 [ + - ]: 8 : else if (off == 0
11491 : 8 : && (unit_type == DW_UT_split_type
11492 [ + - ]: 8 : || unit_type == DW_UT_split_compile))
11493 : : {
11494 : 8 : has_header = cu->version > 4;
11495 : 8 : offset_size = cu->offset_size;
11496 : : }
11497 : : else
11498 : : {
11499 : 0 : error (0, 0,
11500 : : "Warning: No CU references .debug_str_offsets after %"
11501 : : PRIx64, off);
11502 : 0 : has_header = cu->version > 4;
11503 : 0 : offset_size = cu->offset_size;
11504 : : }
11505 : 8 : fprintf (out, "\n");
11506 : : }
11507 : : else
11508 : : {
11509 : : /* This must be DWARF5, since GNU DebugFission didn't define
11510 : : DW_AT_str_offsets_base. */
11511 : 8 : has_header = true;
11512 : :
11513 : 8 : Dwarf_Die cudie;
11514 [ - + ]: 8 : if (dwarf_cu_die (listptr->cu, &cudie,
11515 : : NULL, NULL, NULL, NULL,
11516 : : NULL, NULL) == NULL)
11517 : 0 : fprintf (out, "Unknown CU (%s):\n", dwarf_errmsg (-1));
11518 : : else
11519 : 8 : fprintf (out, "for CU [%6" PRIx64 "]:\n", dwarf_dieoffset (&cudie));
11520 : : }
11521 : :
11522 [ + + ]: 16 : if (has_header)
11523 : : {
11524 : 12 : uint64_t unit_length;
11525 : 12 : uint16_t version;
11526 : 12 : uint16_t padding;
11527 : :
11528 [ - + ]: 12 : unit_length = read_4ubyte_unaligned_inc (dbg, readp);
11529 [ - + ]: 12 : if (unlikely (unit_length == 0xffffffff))
11530 : : {
11531 [ # # ]: 0 : if (unlikely (readp > readendp - 8))
11532 : : {
11533 : 0 : invalid_data:
11534 : 0 : error (0, 0, "Invalid data");
11535 : 0 : return;
11536 : : }
11537 [ # # ]: 0 : unit_length = read_8ubyte_unaligned_inc (dbg, readp);
11538 : 0 : offset_size = 8;
11539 : : }
11540 : : else
11541 : : offset_size = 4;
11542 : :
11543 : 12 : fprintf (out, "\n");
11544 : 12 : fprintf (out, _(" Length: %8" PRIu64 "\n"),
11545 : : unit_length);
11546 : 12 : fprintf (out, _(" Offset size: %8" PRIu8 "\n"),
11547 : : offset_size);
11548 : :
11549 : : /* We need at least 2-bytes (version) + 2-bytes (padding) =
11550 : : 4 bytes to complete the header. And this unit cannot go
11551 : : beyond the section data. */
11552 [ + - ]: 12 : if (readp > readendp - 4
11553 [ + - ]: 12 : || unit_length < 4
11554 [ - + ]: 12 : || unit_length > (uint64_t) (readendp - readp))
11555 : 0 : goto invalid_data;
11556 : :
11557 : 12 : next_unitp = readp + unit_length;
11558 : :
11559 [ - + ]: 12 : version = read_2ubyte_unaligned_inc (dbg, readp);
11560 : 12 : fprintf (out, _(" DWARF version: %8" PRIu16 "\n"), version);
11561 : :
11562 [ - + ]: 12 : if (version != 5)
11563 : : {
11564 : 0 : error (0, 0, _("Unknown version"));
11565 : 0 : goto next_unit;
11566 : : }
11567 : :
11568 [ - + ]: 12 : padding = read_2ubyte_unaligned_inc (dbg, readp);
11569 : 12 : fprintf (out, _(" Padding: %8" PRIx16 "\n"), padding);
11570 : :
11571 [ + + ]: 12 : if (listptr != NULL
11572 [ - + ]: 8 : && listptr->offset != (Dwarf_Off) (readp - start))
11573 : : {
11574 : 0 : error (0, 0, "String offsets index doesn't start after header");
11575 : 0 : goto next_unit;
11576 : : }
11577 : :
11578 : 12 : fprintf (out, "\n");
11579 : : }
11580 : :
11581 : 16 : int digits = 1;
11582 : 16 : size_t offsets = (next_unitp - readp) / offset_size;
11583 [ + + ]: 24 : while (offsets >= 10)
11584 : : {
11585 : 8 : ++digits;
11586 : 8 : offsets /= 10;
11587 : : }
11588 : :
11589 : 16 : unsigned int uidx = 0;
11590 : 16 : size_t index_offset = readp - (const unsigned char *) data->d_buf;
11591 : 16 : fprintf (out, " Offsets start at 0x%zx:\n", index_offset);
11592 [ + + ]: 204 : while (readp <= next_unitp - offset_size)
11593 : : {
11594 : 188 : Dwarf_Word offset;
11595 [ + - ]: 188 : if (offset_size == 4)
11596 [ - + ]: 188 : offset = read_4ubyte_unaligned_inc (dbg, readp);
11597 : : else
11598 [ # # ]: 0 : offset = read_8ubyte_unaligned_inc (dbg, readp);
11599 : 188 : const char *str = dwarf_getstring (dbg, offset, NULL);
11600 [ - + ]: 188 : fprintf (out, " [%*u] [%*" PRIx64 "] \"%s\"\n",
11601 : 188 : digits, uidx++, (int) offset_size * 2, offset, str ?: "???");
11602 : : }
11603 : 16 : fprintf (out, "\n");
11604 : :
11605 [ + - ]: 16 : if (readp != next_unitp)
11606 : 0 : error (0, 0, "extra %zd bytes at end of unit",
11607 : 0 : (size_t) (next_unitp - readp));
11608 : :
11609 : 16 : next_unit:
11610 : : readp = next_unitp;
11611 : : }
11612 : : }
11613 : :
11614 : :
11615 : : /* Print the content of the call frame search table section
11616 : : '.eh_frame_hdr'. */
11617 : : static void
11618 : 140 : print_debug_frame_hdr_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
11619 : : Ebl *ebl __attribute__ ((unused)),
11620 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
11621 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg,
11622 : : FILE *out)
11623 : : {
11624 : 140 : fprintf (out, _("\
11625 : : \nCall frame search table section [%2zu] '.eh_frame_hdr':\n"),
11626 : : elf_ndxscn (scn));
11627 : :
11628 : 140 : Elf_Data *data = elf_rawdata (scn, NULL);
11629 : :
11630 [ - + ]: 140 : if (unlikely (data == NULL))
11631 : : {
11632 : 0 : error (0, 0, _("cannot get %s content: %s"),
11633 : : ".eh_frame_hdr", elf_errmsg (-1));
11634 : 0 : return;
11635 : : }
11636 : :
11637 : 140 : const unsigned char *readp = data->d_buf;
11638 : 140 : const unsigned char *const dataend = ((unsigned char *) data->d_buf
11639 : 140 : + data->d_size);
11640 : :
11641 [ - + ]: 140 : if (unlikely (readp + 4 > dataend))
11642 : : {
11643 : 0 : invalid_data:
11644 : 0 : error (0, 0, _("invalid data"));
11645 : 0 : return;
11646 : : }
11647 : :
11648 : 140 : unsigned int version = *readp++;
11649 : 140 : unsigned int eh_frame_ptr_enc = *readp++;
11650 : 140 : unsigned int fde_count_enc = *readp++;
11651 : 140 : unsigned int table_enc = *readp++;
11652 : :
11653 : 140 : fprintf (out, " version: %u\n"
11654 : : " eh_frame_ptr_enc: %#x ",
11655 : : version, eh_frame_ptr_enc);
11656 : 140 : print_encoding_base ("", eh_frame_ptr_enc, out);
11657 : 140 : fprintf (out, " fde_count_enc: %#x ", fde_count_enc);
11658 : 140 : print_encoding_base ("", fde_count_enc, out);
11659 : 140 : fprintf (out, " table_enc: %#x ", table_enc);
11660 : 140 : print_encoding_base ("", table_enc, out);
11661 : :
11662 : 140 : uint64_t eh_frame_ptr = 0;
11663 [ + - ]: 140 : if (eh_frame_ptr_enc != DW_EH_PE_omit)
11664 : : {
11665 : 140 : readp = read_encoded (eh_frame_ptr_enc, readp, dataend, &eh_frame_ptr,
11666 : : dbg);
11667 [ - + ]: 140 : if (unlikely (readp == NULL))
11668 : 0 : goto invalid_data;
11669 : :
11670 : 140 : fprintf (out, " eh_frame_ptr: %#" PRIx64, eh_frame_ptr);
11671 [ + - ]: 140 : if ((eh_frame_ptr_enc & 0x70) == DW_EH_PE_pcrel)
11672 : 140 : fprintf (out, " (offset: %#" PRIx64 ")",
11673 : : /* +4 because of the 4 byte header of the section. */
11674 : 140 : (uint64_t) shdr->sh_offset + 4 + eh_frame_ptr);
11675 : :
11676 : 140 : fputc ('\n', out);
11677 : : }
11678 : :
11679 : 140 : uint64_t fde_count = 0;
11680 [ + - ]: 140 : if (fde_count_enc != DW_EH_PE_omit)
11681 : : {
11682 : 140 : readp = read_encoded (fde_count_enc, readp, dataend, &fde_count, dbg);
11683 [ - + ]: 140 : if (unlikely (readp == NULL))
11684 : 0 : goto invalid_data;
11685 : :
11686 : 140 : fprintf (out, " fde_count: %" PRIu64 "\n", fde_count);
11687 : : }
11688 : :
11689 [ + - + - ]: 140 : if (fde_count == 0 || table_enc == DW_EH_PE_omit)
11690 : : return;
11691 : :
11692 : 140 : fputs (" Table:\n", out);
11693 : :
11694 : : /* Optimize for the most common case. */
11695 [ + - ]: 140 : if (table_enc == (DW_EH_PE_datarel | DW_EH_PE_sdata4))
11696 [ + + ]: 26982 : while (fde_count > 0 && readp + 8 <= dataend)
11697 : : {
11698 [ - + ]: 26842 : int32_t initial_location = read_4sbyte_unaligned_inc (dbg, readp);
11699 : 26842 : uint64_t initial_offset = ((uint64_t) shdr->sh_offset
11700 : 26842 : + (int64_t) initial_location);
11701 [ - + ]: 26842 : int32_t address = read_4sbyte_unaligned_inc (dbg, readp);
11702 : : // XXX Possibly print symbol name or section offset for initial_offset
11703 : 26842 : fprintf (out, " %#" PRIx32 " (offset: %#6" PRIx64 ") -> %#" PRIx32
11704 : : " fde=[%6" PRIx64 "]\n",
11705 : : initial_location, initial_offset,
11706 : 26842 : address, address - (eh_frame_ptr + 4));
11707 : : }
11708 : : else
11709 : 140 : while (0 && readp < dataend)
11710 : : {
11711 : :
11712 : 140 : }
11713 : : }
11714 : :
11715 : :
11716 : : /* Print the content of the exception handling table section
11717 : : '.eh_frame_hdr'. */
11718 : : static void
11719 : 0 : print_debug_exception_table (Dwfl_Module *dwflmod __attribute__ ((unused)),
11720 : : Ebl *ebl __attribute__ ((unused)),
11721 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
11722 : : Elf_Scn *scn,
11723 : : GElf_Shdr *shdr __attribute__ ((unused)),
11724 : : Dwarf *dbg __attribute__ ((unused)),
11725 : : FILE *out)
11726 : : {
11727 : 0 : fprintf (out, _("\
11728 : : \nException handling table section [%2zu] '.gcc_except_table':\n"),
11729 : : elf_ndxscn (scn));
11730 : :
11731 : 0 : Elf_Data *data = elf_rawdata (scn, NULL);
11732 : :
11733 [ # # ]: 0 : if (unlikely (data == NULL))
11734 : : {
11735 : 0 : error (0, 0, _("cannot get %s content: %s"),
11736 : : ".gcc_except_table", elf_errmsg (-1));
11737 : 0 : return;
11738 : : }
11739 : :
11740 : 0 : const unsigned char *readp = data->d_buf;
11741 : 0 : const unsigned char *const dataend = readp + data->d_size;
11742 : :
11743 [ # # ]: 0 : if (unlikely (readp + 1 > dataend))
11744 : : {
11745 : 0 : invalid_data:
11746 : 0 : error (0, 0, _("invalid data"));
11747 : 0 : return;
11748 : : }
11749 : 0 : unsigned int lpstart_encoding = *readp++;
11750 : 0 : fprintf (out, _(" LPStart encoding: %#x "), lpstart_encoding);
11751 : 0 : print_encoding_base ("", lpstart_encoding, out);
11752 [ # # ]: 0 : if (lpstart_encoding != DW_EH_PE_omit)
11753 : : {
11754 : 0 : uint64_t lpstart;
11755 : 0 : readp = read_encoded (lpstart_encoding, readp, dataend, &lpstart, dbg);
11756 : 0 : fprintf (out, " LPStart: %#" PRIx64 "\n", lpstart);
11757 : : }
11758 : :
11759 [ # # ]: 0 : if (unlikely (readp + 1 > dataend))
11760 : 0 : goto invalid_data;
11761 : 0 : unsigned int ttype_encoding = *readp++;
11762 : 0 : fprintf (out, _(" TType encoding: %#x "), ttype_encoding);
11763 : 0 : print_encoding_base ("", ttype_encoding, out);
11764 : 0 : const unsigned char *ttype_base = NULL;
11765 [ # # ]: 0 : if (ttype_encoding != DW_EH_PE_omit)
11766 : : {
11767 : 0 : unsigned int ttype_base_offset;
11768 [ # # ]: 0 : if (readp >= dataend)
11769 : 0 : goto invalid_data;
11770 : 0 : get_uleb128 (ttype_base_offset, readp, dataend);
11771 : 0 : fprintf (out, " TType base offset: %#x\n", ttype_base_offset);
11772 [ # # ]: 0 : if ((size_t) (dataend - readp) > ttype_base_offset)
11773 : 0 : ttype_base = readp + ttype_base_offset;
11774 : : }
11775 : :
11776 [ # # ]: 0 : if (unlikely (readp + 1 > dataend))
11777 : 0 : goto invalid_data;
11778 : 0 : unsigned int call_site_encoding = *readp++;
11779 : 0 : fprintf (out, _(" Call site encoding: %#x "), call_site_encoding);
11780 : 0 : print_encoding_base ("", call_site_encoding, out);
11781 : 0 : unsigned int call_site_table_len;
11782 [ # # ]: 0 : if (readp >= dataend)
11783 : 0 : goto invalid_data;
11784 : 0 : get_uleb128 (call_site_table_len, readp, dataend);
11785 : :
11786 : 0 : const unsigned char *const action_table = readp + call_site_table_len;
11787 [ # # ]: 0 : if (unlikely (action_table > dataend))
11788 : 0 : goto invalid_data;
11789 : : unsigned int u = 0;
11790 : : unsigned int max_action = 0;
11791 [ # # ]: 0 : while (readp < action_table)
11792 : : {
11793 [ # # ]: 0 : if (u == 0)
11794 : 0 : fputs (_("\n Call site table:\n"), out);
11795 : :
11796 : 0 : uint64_t call_site_start;
11797 : 0 : readp = read_encoded (call_site_encoding, readp, dataend,
11798 : : &call_site_start, dbg);
11799 : 0 : uint64_t call_site_length;
11800 : 0 : readp = read_encoded (call_site_encoding, readp, dataend,
11801 : : &call_site_length, dbg);
11802 : 0 : uint64_t landing_pad;
11803 : 0 : readp = read_encoded (call_site_encoding, readp, dataend,
11804 : : &landing_pad, dbg);
11805 : 0 : unsigned int action;
11806 [ # # ]: 0 : if (readp >= dataend)
11807 : 0 : goto invalid_data;
11808 : 0 : get_uleb128 (action, readp, dataend);
11809 : 0 : max_action = MAX (action, max_action);
11810 : 0 : fprintf (out, _(" [%4u] Call site start: %#" PRIx64 "\n"
11811 : : " Call site length: %" PRIu64 "\n"
11812 : : " Landing pad: %#" PRIx64 "\n"
11813 : : " Action: %u\n"),
11814 : : u++, call_site_start, call_site_length, landing_pad, action);
11815 : : }
11816 [ # # ]: 0 : if (readp != action_table)
11817 : 0 : goto invalid_data;
11818 : :
11819 : 0 : unsigned int max_ar_filter = 0;
11820 [ # # ]: 0 : if (max_action > 0)
11821 : : {
11822 : 0 : fputs ("\n Action table:\n", out);
11823 : :
11824 : 0 : size_t maxdata = (size_t) (dataend - action_table);
11825 [ # # # # ]: 0 : if (max_action > maxdata || maxdata - max_action < 1)
11826 : : {
11827 : 0 : invalid_action_table:
11828 : 0 : fputs (_(" <INVALID DATA>\n"), stdout);
11829 : 0 : return;
11830 : : }
11831 : :
11832 : 0 : const unsigned char *const action_table_end
11833 : 0 : = action_table + max_action + 1;
11834 : :
11835 : 0 : u = 0;
11836 : 0 : do
11837 : : {
11838 : 0 : int ar_filter;
11839 : 0 : get_sleb128 (ar_filter, readp, action_table_end);
11840 [ # # ]: 0 : if (ar_filter > 0 && (unsigned int) ar_filter > max_ar_filter)
11841 : 0 : max_ar_filter = ar_filter;
11842 : 0 : int ar_disp;
11843 [ # # ]: 0 : if (readp >= action_table_end)
11844 : 0 : goto invalid_action_table;
11845 : 0 : get_sleb128 (ar_disp, readp, action_table_end);
11846 : :
11847 : 0 : fprintf (out, " [%4u] ar_filter: % d\n"
11848 : : " ar_disp: % -5d",
11849 : : u, ar_filter, ar_disp);
11850 [ # # ]: 0 : if (abs (ar_disp) & 1)
11851 : 0 : fprintf (out, " -> [%4u]\n", u + (ar_disp + 1) / 2);
11852 [ # # ]: 0 : else if (ar_disp != 0)
11853 : 0 : fputs (" -> ???\n", out);
11854 : : else
11855 : 0 : fputc ('\n', out);
11856 : 0 : ++u;
11857 : : }
11858 [ # # ]: 0 : while (readp < action_table_end);
11859 : : }
11860 : :
11861 [ # # ]: 0 : if (max_ar_filter > 0 && ttype_base != NULL)
11862 : : {
11863 : 0 : unsigned char dsize;
11864 : 0 : fputs ("\n TType table:\n", out);
11865 : :
11866 : : // XXX Not *4, size of encoding;
11867 [ # # ]: 0 : switch (ttype_encoding & 7)
11868 : : {
11869 : : case DW_EH_PE_udata2:
11870 : : case DW_EH_PE_sdata2:
11871 : : dsize = 2;
11872 : : break;
11873 : : case DW_EH_PE_udata4:
11874 : : case DW_EH_PE_sdata4:
11875 : : dsize = 4;
11876 : : break;
11877 : : case DW_EH_PE_udata8:
11878 : : case DW_EH_PE_sdata8:
11879 : : dsize = 8;
11880 : : break;
11881 : 0 : default:
11882 : 0 : dsize = 0;
11883 : 0 : error (1, 0, _("invalid TType encoding"));
11884 : : }
11885 : :
11886 : 0 : if (max_ar_filter
11887 [ # # ]: 0 : > (size_t) (ttype_base - (const unsigned char *) data->d_buf) / dsize)
11888 : 0 : goto invalid_data;
11889 : :
11890 : 0 : readp = ttype_base - max_ar_filter * dsize;
11891 : 0 : do
11892 : : {
11893 : 0 : uint64_t ttype;
11894 : 0 : readp = read_encoded (ttype_encoding, readp, ttype_base, &ttype,
11895 : : dbg);
11896 : 0 : fprintf (out, " [%4u] %#" PRIx64 "\n", max_ar_filter--, ttype);
11897 : : }
11898 [ # # ]: 0 : while (readp < ttype_base);
11899 : : }
11900 : : }
11901 : :
11902 : : /* Print the content of the '.gdb_index' section.
11903 : : http://sourceware.org/gdb/current/onlinedocs/gdb/Index-Section-Format.html
11904 : : */
11905 : : static void
11906 : 8 : print_gdb_index_section (Dwfl_Module *dwflmod, Ebl *ebl,
11907 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
11908 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg,
11909 : : FILE *out)
11910 : : {
11911 : 8 : fprintf (out, _("\nGDB section [%2zu] '%s' at offset %#" PRIx64
11912 : : " contains %" PRId64 " bytes :\n"),
11913 : : elf_ndxscn (scn), section_name (ebl, shdr),
11914 : 8 : (uint64_t) shdr->sh_offset, (uint64_t) shdr->sh_size);
11915 : :
11916 : 8 : Elf_Data *data = elf_rawdata (scn, NULL);
11917 : :
11918 [ - + ]: 8 : if (unlikely (data == NULL))
11919 : : {
11920 : 0 : error (0, 0, _("cannot get %s content: %s"),
11921 : : ".gdb_index", elf_errmsg (-1));
11922 : 4 : return;
11923 : : }
11924 : :
11925 : : // .gdb_index is always in little endian.
11926 : 8 : Dwarf dummy_dbg = { .other_byte_order = MY_ELFDATA != ELFDATA2LSB };
11927 : 8 : dbg = &dummy_dbg;
11928 : :
11929 : 8 : const unsigned char *readp = data->d_buf;
11930 : 8 : const unsigned char *const dataend = readp + data->d_size;
11931 : :
11932 [ - + ]: 8 : if (unlikely (readp + 4 > dataend))
11933 : : {
11934 : 0 : invalid_data:
11935 : 0 : error (0, 0, _("invalid data"));
11936 : 0 : return;
11937 : : }
11938 : :
11939 : 8 : int32_t vers = read_4ubyte_unaligned (dbg, readp);
11940 : 8 : fprintf (out, _(" Version: %" PRId32 "\n"), vers);
11941 : :
11942 : : // The only difference between version 4 and version 5 is the
11943 : : // hash used for generating the table. Version 6 contains symbols
11944 : : // for inlined functions, older versions didn't. Version 7 adds
11945 : : // symbol kinds. Version 8 just indicates that it correctly includes
11946 : : // TUs for symbols. Version 9 adds shortcut table for information
11947 : : // regarding the main function.
11948 [ - + ]: 8 : if (vers < 4 || vers > 9)
11949 : : {
11950 : 0 : fprintf (out, _(" unknown version, cannot parse section\n"));
11951 : 0 : return;
11952 : : }
11953 : :
11954 : 8 : readp += 4;
11955 [ - + ]: 8 : if (unlikely (readp + 4 > dataend))
11956 : 0 : goto invalid_data;
11957 : :
11958 : 8 : uint32_t cu_off = read_4ubyte_unaligned (dbg, readp);
11959 : 8 : fprintf (out, _(" CU offset: %#" PRIx32 "\n"), cu_off);
11960 : :
11961 : 8 : readp += 4;
11962 [ - + ]: 8 : if (unlikely (readp + 4 > dataend))
11963 : 0 : goto invalid_data;
11964 : :
11965 : 8 : uint32_t tu_off = read_4ubyte_unaligned (dbg, readp);
11966 : 8 : fprintf (out, _(" TU offset: %#" PRIx32 "\n"), tu_off);
11967 : :
11968 : 8 : readp += 4;
11969 [ - + ]: 8 : if (unlikely (readp + 4 > dataend))
11970 : 0 : goto invalid_data;
11971 : :
11972 : 8 : uint32_t addr_off = read_4ubyte_unaligned (dbg, readp);
11973 : 8 : fprintf (out, _(" address offset: %#" PRIx32 "\n"), addr_off);
11974 : :
11975 : 8 : readp += 4;
11976 [ - + ]: 8 : if (unlikely (readp + 4 > dataend))
11977 : 0 : goto invalid_data;
11978 : :
11979 : 8 : uint32_t sym_off = read_4ubyte_unaligned (dbg, readp);
11980 : 8 : fprintf (out, _(" symbol offset: %#" PRIx32 "\n"), sym_off);
11981 : :
11982 : 8 : readp += 4;
11983 [ - + ]: 8 : if (unlikely (readp + 4 > dataend))
11984 : 0 : goto invalid_data;
11985 : :
11986 : 8 : uint32_t shortcut_off = 0;
11987 [ + + ]: 8 : if (vers >= 9)
11988 : : {
11989 : 4 : shortcut_off = read_4ubyte_unaligned (dbg, readp);
11990 : 4 : fprintf (out, _(" shortcut offset: %#" PRIx32 "\n"), shortcut_off);
11991 : :
11992 : 4 : readp += 4;
11993 [ - + ]: 4 : if (unlikely (readp + 4 > dataend))
11994 : 0 : goto invalid_data;
11995 : : }
11996 : :
11997 : 8 : uint32_t const_off = read_4ubyte_unaligned (dbg, readp);
11998 : 8 : fprintf (out, _(" constant offset: %#" PRIx32 "\n"), const_off);
11999 : :
12000 [ - + ]: 8 : if (unlikely ((size_t) (dataend - (const unsigned char *) data->d_buf)
12001 : : < const_off))
12002 : 0 : goto invalid_data;
12003 : :
12004 : 8 : readp = data->d_buf + cu_off;
12005 : :
12006 : 8 : const unsigned char *nextp = data->d_buf + tu_off;
12007 [ - + ]: 8 : if (tu_off >= data->d_size)
12008 : 0 : goto invalid_data;
12009 : :
12010 : 8 : size_t cu_nr = (nextp - readp) / 16;
12011 : :
12012 : 8 : fprintf (out, _("\n CU list at offset %#" PRIx32
12013 : : " contains %zu entries:\n"),
12014 : : cu_off, cu_nr);
12015 : :
12016 : 8 : size_t n = 0;
12017 [ + - + + ]: 22 : while (dataend - readp >= 16 && n < cu_nr)
12018 : : {
12019 : 14 : uint64_t off = read_8ubyte_unaligned (dbg, readp);
12020 : 14 : readp += 8;
12021 : :
12022 : 14 : uint64_t len = read_8ubyte_unaligned (dbg, readp);
12023 : 14 : readp += 8;
12024 : :
12025 : 14 : fprintf (out, " [%4zu] start: %0#8" PRIx64
12026 : : ", length: %5" PRIu64 "\n", n, off, len);
12027 : 14 : n++;
12028 : : }
12029 : :
12030 : 8 : readp = data->d_buf + tu_off;
12031 : 8 : nextp = data->d_buf + addr_off;
12032 [ - + ]: 8 : if (addr_off >= data->d_size)
12033 : 0 : goto invalid_data;
12034 : :
12035 : 8 : size_t tu_nr = (nextp - readp) / 24;
12036 : :
12037 : 8 : fprintf (out, _("\n TU list at offset %#" PRIx32
12038 : : " contains %zu entries:\n"),
12039 : : tu_off, tu_nr);
12040 : :
12041 : 8 : n = 0;
12042 [ + - + + ]: 14 : while (dataend - readp >= 24 && n < tu_nr)
12043 : : {
12044 : 6 : uint64_t off = read_8ubyte_unaligned (dbg, readp);
12045 : 6 : readp += 8;
12046 : :
12047 : 6 : uint64_t type = read_8ubyte_unaligned (dbg, readp);
12048 : 6 : readp += 8;
12049 : :
12050 : 6 : uint64_t sig = read_8ubyte_unaligned (dbg, readp);
12051 : 6 : readp += 8;
12052 : :
12053 : 6 : fprintf (out, " [%4zu] CU offset: %5" PRId64
12054 : : ", type offset: %5" PRId64
12055 : : ", signature: %0#8" PRIx64 "\n", n, off, type, sig);
12056 : 6 : n++;
12057 : : }
12058 : :
12059 : 8 : readp = data->d_buf + addr_off;
12060 : 8 : nextp = data->d_buf + sym_off;
12061 [ - + ]: 8 : if (sym_off >= data->d_size)
12062 : 0 : goto invalid_data;
12063 : :
12064 : 8 : size_t addr_nr = (nextp - readp) / 20;
12065 : :
12066 : 8 : fprintf (out, _("\n Address list at offset %#" PRIx32
12067 : : " contains %zu entries:\n"),
12068 : : addr_off, addr_nr);
12069 : :
12070 : 8 : n = 0;
12071 [ + - + + ]: 22 : while (dataend - readp >= 20 && n < addr_nr)
12072 : : {
12073 : 14 : uint64_t low = read_8ubyte_unaligned (dbg, readp);
12074 : 14 : readp += 8;
12075 : :
12076 : 14 : uint64_t high = read_8ubyte_unaligned (dbg, readp);
12077 : 14 : readp += 8;
12078 : :
12079 : 14 : uint32_t idx = read_4ubyte_unaligned (dbg, readp);
12080 : 14 : readp += 4;
12081 : :
12082 : 14 : fprintf (out, " [%4zu] ", n);
12083 : 14 : print_dwarf_addr (dwflmod, 8, low, low, out);
12084 : 14 : fprintf (out, "..");
12085 : 14 : print_dwarf_addr (dwflmod, 8, high - 1, high, out);
12086 : 14 : fprintf (out, ", CU index: %5" PRId32 "\n", idx);
12087 : 14 : n++;
12088 : : }
12089 : :
12090 : 8 : const unsigned char *const_start = data->d_buf + const_off;
12091 [ - + ]: 8 : if (const_off > data->d_size)
12092 : 0 : goto invalid_data;
12093 : :
12094 : 8 : const unsigned char *shortcut_start = NULL;
12095 [ + + ]: 8 : if (vers >= 9)
12096 : : {
12097 [ - + ]: 4 : if (shortcut_off >= data->d_size)
12098 : 0 : goto invalid_data;
12099 : :
12100 : 4 : shortcut_start = data->d_buf + shortcut_off;
12101 : 4 : nextp = shortcut_start;
12102 : : }
12103 : : else
12104 : : nextp = const_start;
12105 : :
12106 : 8 : readp = data->d_buf + sym_off;
12107 : 8 : size_t sym_nr = (nextp - readp) / 8;
12108 : :
12109 : 8 : fprintf (out, _("\n Symbol table at offset %#" PRIx32
12110 : : " contains %zu slots:\n"),
12111 : : sym_off, sym_nr);
12112 : :
12113 : 8 : n = 0;
12114 [ + - + + ]: 8200 : while (dataend - readp >= 8 && n < sym_nr)
12115 : : {
12116 : 8192 : uint32_t name = read_4ubyte_unaligned (dbg, readp);
12117 : 8192 : readp += 4;
12118 : :
12119 : 8192 : uint32_t vector = read_4ubyte_unaligned (dbg, readp);
12120 : 8192 : readp += 4;
12121 : :
12122 [ + + ]: 8192 : if (name != 0 || vector != 0)
12123 : : {
12124 : 60 : const unsigned char *sym = const_start + name;
12125 [ + - - + ]: 60 : if (unlikely ((size_t) (dataend - const_start) < name
12126 : : || memchr (sym, '\0', dataend - sym) == NULL))
12127 : 0 : goto invalid_data;
12128 : :
12129 : 60 : fprintf (out, " [%4zu] symbol: %s, CUs: ", n, sym);
12130 : :
12131 : 60 : const unsigned char *readcus = const_start + vector;
12132 [ - + ]: 60 : if (unlikely ((size_t) (dataend - const_start) < vector))
12133 : 0 : goto invalid_data;
12134 : 60 : uint32_t cus = read_4ubyte_unaligned (dbg, readcus);
12135 [ + + ]: 128 : while (cus--)
12136 : : {
12137 : 68 : uint32_t cu_kind, cu, kind;
12138 : 68 : bool is_static;
12139 : 68 : readcus += 4;
12140 [ - + ]: 68 : if (unlikely (readcus + 4 > dataend))
12141 : 0 : goto invalid_data;
12142 : 68 : cu_kind = read_4ubyte_unaligned (dbg, readcus);
12143 : 68 : cu = cu_kind & ((1 << 24) - 1);
12144 : 68 : kind = (cu_kind >> 28) & 7;
12145 : 68 : is_static = cu_kind & (1U << 31);
12146 [ + + ]: 68 : if (cu > cu_nr - 1)
12147 : 10 : fprintf (out, "%" PRId32 "T", cu - (uint32_t) cu_nr);
12148 : : else
12149 : 58 : fprintf (out, "%" PRId32, cu);
12150 [ + + ]: 68 : if (kind != 0)
12151 : : {
12152 : 52 : fprintf (out, " (");
12153 [ + + + - : 52 : switch (kind)
- ]
12154 : : {
12155 : : case 1:
12156 : 20 : fprintf (out, "type");
12157 : 20 : break;
12158 : : case 2:
12159 : 16 : fprintf (out, "var");
12160 : 16 : break;
12161 : : case 3:
12162 : 16 : fprintf (out, "func");
12163 : 16 : break;
12164 : : case 4:
12165 : 0 : fprintf (out, "other");
12166 : 0 : break;
12167 : : default:
12168 : 0 : fprintf (out, "unknown-0x%" PRIx32, kind);
12169 : 0 : break;
12170 : : }
12171 [ + + ]: 68 : fprintf (out, ":%c)", (is_static ? 'S' : 'G'));
12172 : : }
12173 [ + + ]: 68 : if (cus > 0)
12174 : 8 : fprintf (out, ", ");
12175 : : }
12176 : 60 : fprintf (out, "\n");
12177 : : }
12178 : 8192 : n++;
12179 : : }
12180 : :
12181 [ + + ]: 8 : if (vers < 9)
12182 : : return;
12183 : :
12184 [ - + ]: 4 : if (unlikely (shortcut_start == NULL))
12185 : 0 : goto invalid_data;
12186 : :
12187 : 4 : readp = shortcut_start;
12188 : 4 : nextp = const_start;
12189 : 4 : size_t shortcut_nr = (nextp - readp) / 4;
12190 : :
12191 [ - + ]: 4 : if (unlikely (shortcut_nr != 2))
12192 : 0 : goto invalid_data;
12193 : :
12194 : 8 : fprintf (out,
12195 : 4 : _("\nShortcut table at offset %#" PRIx32 " contains %zu slots:\n"),
12196 : : shortcut_off, shortcut_nr);
12197 : :
12198 : 4 : uint32_t lang = read_4ubyte_unaligned (dbg, readp);
12199 : 4 : readp += 4;
12200 : :
12201 : : /* Include the hex number of LANG in the output if the language
12202 : : is unknown. */
12203 : 4 : const char *lang_str = dwarf_lang_string (lang);
12204 : 4 : lang_str = string_or_unknown (lang_str, lang, DW_LANG_lo_user,
12205 : : DW_LANG_hi_user, true);
12206 : :
12207 : 4 : fprintf (out, _("Language of main: %s\n"), lang_str);
12208 : 4 : fprintf (out, _("Name of main: "));
12209 : :
12210 [ + + ]: 4 : if (lang != 0)
12211 : : {
12212 : 2 : uint32_t name = read_4ubyte_unaligned (dbg, readp);
12213 : 2 : readp += 4;
12214 : 2 : const unsigned char *sym = const_start + name;
12215 : :
12216 [ + - - + ]: 2 : if (unlikely ((size_t) (dataend - const_start) < name
12217 : : || memchr (sym, '\0', dataend - sym) == NULL))
12218 : 0 : goto invalid_data;
12219 : :
12220 : 2 : fprintf (out, "%s\n", sym);
12221 : : }
12222 : : else
12223 : 2 : fprintf (out, "<unknown>\n");
12224 : : }
12225 : :
12226 : : /* Print the content of the '.debug_cu_index' or '.debug_tu_index'
12227 : : sections. */
12228 : : static void
12229 : 8 : print_cu_index_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
12230 : : Ebl *ebl, GElf_Ehdr *ehdr __attribute__ ((unused)),
12231 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg,
12232 : : FILE *out)
12233 : : {
12234 : 8 : const char *sname = section_name (ebl, shdr);
12235 : 8 : fprintf (out, _("\nDWARF section [%2zu] '%s' at offset %#" PRIx64
12236 : : " contains %" PRId64 " bytes :\n"),
12237 : : elf_ndxscn (scn), sname,
12238 : 8 : (uint64_t) shdr->sh_offset, (uint64_t) shdr->sh_size);
12239 : :
12240 : 8 : Elf_Data *data = elf_rawdata (scn, NULL);
12241 : :
12242 [ - + ]: 8 : if (unlikely (data == NULL))
12243 : : {
12244 : 0 : error (0, 0, _("cannot get %s content: %s"), sname, elf_errmsg (-1));
12245 : 0 : return;
12246 : : }
12247 : :
12248 [ + + ]: 8 : bool is_tu = sname != NULL && strcmp (sname, ".debug_tu_index") == 0;
12249 : :
12250 : 8 : const unsigned char *readp = data->d_buf;
12251 : 8 : const unsigned char *const dataend = readp + data->d_size;
12252 : :
12253 [ - + ]: 8 : if (unlikely (readp > dataend - 4))
12254 : : {
12255 : 0 : invalid_data:
12256 : 0 : error (0, 0, _("invalid data"));
12257 : 0 : return;
12258 : : }
12259 : :
12260 : : /* If read as 4 bytes the version is 2, it is GNU DebugFission for
12261 : : DWARF 5, otherwise the version is in the first 2 bytes, with 2
12262 : : bytes padding. */
12263 [ - + ]: 8 : int32_t vers = read_4ubyte_unaligned (dbg, readp);
12264 [ + + ]: 8 : if (vers != 2)
12265 [ - + ]: 4 : vers = read_2ubyte_unaligned (dbg, readp);
12266 : 8 : fprintf (out, _(" Version: %8" PRId32 "\n"), vers);
12267 : :
12268 : : /* There used to be a version 1, which we don't support, but is
12269 : : described at https://gcc.gnu.org/wiki/DebugFissionDWP
12270 : : Version 2 is GNU DebugFission for DWARF4.
12271 : : Version 5 is the standardized DWARF5 index.
12272 : : Version 6 supports DWARF64 as described in
12273 : : https://dwarfstd.org/issues/220708.2.html */
12274 [ - + - - ]: 8 : if (vers != 2 && vers != 5 && vers != 6)
12275 : : {
12276 : 0 : error (0, 0, _("unknown version, cannot parse section"));
12277 : 0 : return;
12278 : : }
12279 : :
12280 : : /* The offset size field is only available in version 6. */
12281 : 8 : uint8_t offset_size_flag = 0;
12282 : 8 : if (vers < 6)
12283 : 8 : readp += 4;
12284 : : else
12285 : : {
12286 : 0 : readp += 3;
12287 : 0 : offset_size_flag = *readp;
12288 [ # # ]: 0 : fprintf (out, _(" Offset Size: %4" PRIu8 "\n"),
12289 : : offset_size_flag == 0 ? 32 : 64);
12290 : 0 : readp++;
12291 : : }
12292 : :
12293 [ - - ]: 8 : const unsigned char off_bytes = offset_size_flag == 0 ? 4 : 8;
12294 : :
12295 [ - + ]: 8 : if (unlikely (readp > dataend - 4))
12296 : 0 : goto invalid_data;
12297 [ - + ]: 8 : uint32_t section_count = read_4ubyte_unaligned (dbg, readp);
12298 : 8 : fprintf (out, _(" Columns: %8" PRId32 "\n"), section_count);
12299 : :
12300 : 8 : readp += 4;
12301 [ - + ]: 8 : if (unlikely (readp > dataend - 4))
12302 : 0 : goto invalid_data;
12303 [ - + ]: 8 : uint32_t unit_count = read_4ubyte_unaligned (dbg, readp);
12304 : 8 : fprintf (out, _(" Entries: %8" PRId32 "\n"), unit_count);
12305 : :
12306 : 8 : readp += 4;
12307 [ - + ]: 8 : if (unlikely (readp > dataend - 4))
12308 : 0 : goto invalid_data;
12309 [ - + ]: 8 : uint32_t slot_count = read_4ubyte_unaligned (dbg, readp);
12310 : 8 : fprintf (out, _(" Slots: %8" PRId32 "\n"), slot_count);
12311 : :
12312 : : /* Really should be slot_count > 3 * unit_count / 2, but accept as
12313 : : long as slot_count is at least unit_count. */
12314 [ - + ]: 8 : if (slot_count < unit_count)
12315 : : {
12316 : 0 : error (0, 0, _("Must have at least as many slots as entries"));
12317 : 0 : return;
12318 : : }
12319 : :
12320 : 8 : readp += 4;
12321 : :
12322 : : /* Hash table starts directly after header (16 bytes). */
12323 : 8 : const unsigned char *hash_table = readp;
12324 : : /* Indices (which slot is used for each hash id entry) start after
12325 : : the hash table (ids of 8 bytes). */
12326 : 8 : const unsigned char *indices = hash_table + slot_count * 8;
12327 : : /* Sections used starts after the indices, indices and hash table
12328 : : have the same number of slots, indeces are 4 bytes each, */
12329 : 8 : const unsigned char *sections = indices + slot_count * 4;
12330 : : /* Offset slots for each section follow the one row of sections. */
12331 : 8 : const unsigned char *offsets = sections + section_count * 4;
12332 : : /* Size slots for each section follow the offsets (used rows). */
12333 : 8 : const unsigned char *lengths = (offsets +
12334 : 8 : unit_count * section_count * off_bytes);
12335 : 8 : const unsigned char *lengths_end = lengths + section_count * 4;
12336 : :
12337 : : /* Sanity check the above against dataend. */
12338 [ + - ]: 8 : if ((slot_count > UINT32_MAX / 8)
12339 : : || (section_count > SIZE_MAX / off_bytes)
12340 : : || (unit_count > SIZE_MAX / off_bytes)
12341 : : || ((unit_count != 0) && (section_count > SIZE_MAX / unit_count))
12342 : : || ((section_count != 0) && (unit_count > SIZE_MAX / section_count))
12343 : 8 : || (indices > dataend)
12344 [ + - ]: 8 : || (sections > dataend)
12345 : 8 : || (offsets > dataend)
12346 [ + - ]: 8 : || (lengths > dataend)
12347 [ - + ]: 8 : || (lengths_end > dataend))
12348 : 0 : goto invalid_data;
12349 : :
12350 : 8 : fprintf (out, _("\n Offset table\n"));
12351 [ + + ]: 8 : fprintf (out, " slot %s", is_tu ? "tu sig" : "dwo id");
12352 : 8 : fprintf (out, " ");
12353 [ + + ]: 60 : for (size_t i = 0; i < section_count; i++)
12354 : : {
12355 [ - + ]: 52 : uint32_t section = read_4ubyte_unaligned (dbg, sections + i * 4);
12356 : 52 : const char *sec_str = dwarf_section_short_string (vers, section);
12357 [ - + ]: 52 : if (sec_str == NULL)
12358 : 0 : fprintf (out, " ??? %2x", section);
12359 : : else
12360 : 52 : fprintf (out, " %6s", sec_str);
12361 : : }
12362 : 8 : fprintf (out, "\n");
12363 : :
12364 [ + + ]: 96 : for (size_t i = 0; i < slot_count; i++)
12365 : : {
12366 : 88 : uint64_t id;
12367 : 88 : uint32_t row;
12368 [ - + ]: 88 : id = read_8ubyte_unaligned (dbg, hash_table + i * 8);
12369 [ - + ]: 88 : row = read_4ubyte_unaligned (dbg, indices + i * 4);
12370 : : /* Only print used rows. */
12371 [ + + ]: 88 : if (id != 0 && row != 0)
12372 : : {
12373 : 20 : fprintf (out, " [%3zd] %016" PRIx64 " ", i, id);
12374 [ - + ]: 20 : if (row > unit_count)
12375 : : {
12376 : 0 : error (0, 0, _("Row (%" PRIu32 ") larger than "
12377 : : "unit count (%" PRIu32 ")"), row, unit_count);
12378 : 0 : continue;
12379 : : }
12380 : : /* Note row is one based, not zero based. */
12381 : 20 : const unsigned char *prow = (offsets
12382 : 20 : + ((row - 1) * section_count
12383 : 20 : * off_bytes));
12384 [ + - ]: 20 : if (off_bytes == 4)
12385 [ + + ]: 150 : for (size_t j = 0; j < section_count; j++)
12386 : : {
12387 [ - + ]: 130 : uint32_t off = read_4ubyte_unaligned (dbg, prow + j * 4);
12388 : 130 : fprintf (out, " %6" PRIu32, off);
12389 : : }
12390 : : else
12391 [ # # ]: 0 : for (size_t j = 0; j < section_count; j++)
12392 : : {
12393 [ # # ]: 0 : uint64_t off = read_4ubyte_unaligned (dbg, prow + j * 8);
12394 : 0 : fprintf (out, " %6" PRIu64, off);
12395 : : }
12396 : 20 : fprintf (out, "\n");
12397 : : }
12398 : : }
12399 : :
12400 : 8 : fprintf (out, _("\n Size table\n"));
12401 : 8 : fprintf (out, " slot %s", is_tu ? "tu sig" : "dwo id");
12402 : 8 : fprintf (out, " ");
12403 [ + + ]: 60 : for (size_t i = 0; i < section_count; i++)
12404 : : {
12405 [ - + ]: 52 : uint32_t section = read_4ubyte_unaligned (dbg, sections + i * 4);
12406 : 52 : const char *sec_str = dwarf_section_short_string (vers, section);
12407 [ - + ]: 52 : if (sec_str == NULL)
12408 : 0 : fprintf (out, " ??? %2x", section);
12409 : : else
12410 : 52 : fprintf (out, " %6s", sec_str);
12411 : : }
12412 : 8 : fprintf (out, "\n");
12413 : :
12414 [ + + ]: 96 : for (size_t i = 0; i < slot_count; i++)
12415 : : {
12416 : 88 : uint64_t id;
12417 : 88 : uint32_t row;
12418 [ - + ]: 88 : id = read_8ubyte_unaligned (dbg, hash_table + i * 8);
12419 [ - + ]: 88 : row = read_4ubyte_unaligned (dbg, indices + i * 4);
12420 : : /* Only print used rows. */
12421 [ + + ]: 88 : if (id != 0 && row != 0)
12422 : : {
12423 : 20 : fprintf (out, " [%3zd] %016" PRIx64 " ", i, id);
12424 [ - + ]: 20 : if (row > unit_count)
12425 : : {
12426 : 0 : error (0, 0, _("Row (%" PRIu32 ") larger than "
12427 : : "unit count (%" PRIu32 ")"), row, unit_count);
12428 : 0 : continue;
12429 : : }
12430 : : /* Note row is one based, not zero based. */
12431 : 20 : const unsigned char *prow = lengths + (row - 1) * section_count * 4;
12432 [ + + ]: 150 : for (size_t j = 0; j < section_count; j++)
12433 : : {
12434 [ - + ]: 130 : uint32_t off = read_4ubyte_unaligned (dbg, prow + j * 4);
12435 : 130 : fprintf (out, " %6" PRIu32, off);
12436 : : }
12437 : 20 : fprintf (out, "\n");
12438 : : }
12439 : : }
12440 : : }
12441 : :
12442 : : /* Returns true and sets split DWARF CU id if there is a split compile
12443 : : unit in the given Dwarf, and no non-split units are found (before it). */
12444 : : static bool
12445 : 206 : is_split_dwarf (Dwarf *dbg, uint64_t *id, Dwarf_CU **split_cu)
12446 : : {
12447 : 206 : Dwarf_CU *cu = NULL;
12448 [ + - ]: 206 : while (dwarf_get_units (dbg, cu, &cu, NULL, NULL, NULL, NULL) == 0)
12449 : : {
12450 : 206 : uint8_t unit_type;
12451 [ + - ]: 206 : if (dwarf_cu_info (cu, NULL, &unit_type, NULL, NULL,
12452 : : id, NULL, NULL) != 0)
12453 : 206 : return false;
12454 : :
12455 [ + + ]: 206 : if (unit_type != DW_UT_split_compile && unit_type != DW_UT_split_type)
12456 : : return false;
12457 : :
12458 : : /* We really only care about the split compile unit, the types
12459 : : should be fine and self sufficient. Also they don't have an
12460 : : id that we can match with a skeleton unit. */
12461 [ + - ]: 18 : if (unit_type == DW_UT_split_compile)
12462 : : {
12463 : 18 : *split_cu = cu;
12464 : 18 : return true;
12465 : : }
12466 : : }
12467 : :
12468 : : return false;
12469 : : }
12470 : :
12471 : : /* Check that there is one and only one Dwfl_Module, return in arg. */
12472 : : static int
12473 : 18 : getone_dwflmod (Dwfl_Module *dwflmod,
12474 : : void **userdata __attribute__ ((unused)),
12475 : : const char *name __attribute__ ((unused)),
12476 : : Dwarf_Addr base __attribute__ ((unused)),
12477 : : void *arg)
12478 : : {
12479 : 18 : Dwfl_Module **m = (Dwfl_Module **) arg;
12480 [ + - ]: 18 : if (*m != NULL)
12481 : : return DWARF_CB_ABORT;
12482 : 18 : *m = dwflmod;
12483 : 18 : return DWARF_CB_OK;
12484 : : }
12485 : :
12486 : : typedef struct Job_Data {
12487 : : struct Job_Data *next;
12488 : : Dwfl_Module *dwflmod;
12489 : : Ebl *ebl;
12490 : : GElf_Ehdr *ehdr;
12491 : : Elf_Scn scn;
12492 : : GElf_Shdr shdr;
12493 : : Dwarf *dbg;
12494 : : FILE *out;
12495 : : void (*fp) (Dwfl_Module *, Ebl *, GElf_Ehdr *,
12496 : : Elf_Scn *, GElf_Shdr *, Dwarf *, FILE *);
12497 : : } job_data;
12498 : :
12499 : : #ifdef USE_LOCKS
12500 : :
12501 : : /* Thread entry point. */
12502 : : static void *
12503 : : do_job (void *data, FILE *out)
12504 : : {
12505 : : job_data *d = (job_data *) data;
12506 : : d->fp (d->dwflmod, d->ebl, d->ehdr, &d->scn, &d->shdr, d->dbg, out);
12507 : : return NULL;
12508 : : }
12509 : : #endif
12510 : :
12511 : : /* If readelf is built with thread safety, then set up JDATA at index IDX
12512 : : and add it to the job queue.
12513 : :
12514 : : If thread safety is not supported or the maximum number of threads is set
12515 : : to 1, then immediately call START_ROUTINE with the given arguments. */
12516 : : static void
12517 : 1102 : schedule_job (job_data **jdatalist,
12518 : : void (*start_routine) (Dwfl_Module *, Ebl *, GElf_Ehdr *,
12519 : : Elf_Scn *, GElf_Shdr *, Dwarf *, FILE *),
12520 : : Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr, Elf_Scn *scn,
12521 : : GElf_Shdr *shdr, Dwarf *dbg)
12522 : : {
12523 : : #ifdef USE_LOCKS
12524 : : if (max_threads > 1)
12525 : : {
12526 : : job_data *jdata = xmalloc (sizeof (job_data));
12527 : :
12528 : : jdata->dwflmod = dwflmod;
12529 : : jdata->ebl = ebl;
12530 : : jdata->ehdr = ehdr;
12531 : : jdata->scn = *scn;
12532 : : jdata->shdr = *shdr;
12533 : : jdata->dbg = dbg;
12534 : : jdata->fp = start_routine;
12535 : : jdata->next = *jdatalist;
12536 : : *jdatalist = jdata;
12537 : :
12538 : : add_job (do_job, (void *) jdata);
12539 : : }
12540 : : else
12541 : : start_routine (dwflmod, ebl, ehdr, scn, shdr, dbg, stdout);
12542 : : #else
12543 : 1102 : (void) jdatalist;
12544 : :
12545 : 1102 : start_routine (dwflmod, ebl, ehdr, scn, shdr, dbg, stdout);
12546 : : #endif
12547 : 1102 : }
12548 : :
12549 : : static void
12550 : 506 : print_debug (Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr)
12551 : : {
12552 : : /* Used for skeleton file, if necessary for split DWARF. */
12553 : 506 : Dwfl *skel_dwfl = NULL;
12554 : 506 : Dwfl_Module *skel_mod = NULL;
12555 : 506 : char *skel_name = NULL;
12556 : 506 : Dwarf *split_dbg = NULL;
12557 : 506 : Dwarf_CU *split_cu = NULL;
12558 : :
12559 : : /* If we need to implicitly or explicitly scan the debug_info section
12560 : : we might need a bit more info, like a skeleton for split dwarf file). */
12561 : 506 : bool implicit_info = (implicit_debug_sections & section_info) != 0;
12562 : 506 : bool explicit_info = (print_debug_sections & section_info) != 0;
12563 : :
12564 : : /* Before we start the real work get a debug context descriptor. */
12565 : 506 : Dwarf_Addr dwbias;
12566 : 506 : Dwarf *dbg = dwfl_module_getdwarf (dwflmod, &dwbias);
12567 : 506 : Dwarf dummy_dbg =
12568 : : {
12569 : 506 : .elf = ebl->elf,
12570 : 506 : .other_byte_order = MY_ELFDATA != ehdr->e_ident[EI_DATA]
12571 : 506 : };
12572 [ + + ]: 506 : if (dbg == NULL)
12573 : : {
12574 [ - + ]: 106 : if ((print_debug_sections & ~(section_exception|section_frame)) != 0)
12575 : 0 : error (0, 0, _("cannot get debug context descriptor: %s"),
12576 : : dwfl_errmsg (-1));
12577 : : dbg = &dummy_dbg;
12578 : : }
12579 [ + + ]: 400 : else if (implicit_info || explicit_info)
12580 : : {
12581 : : /* If we are asked about a split dwarf (.dwo) file, use the user
12582 : : provided, or find the corresponding skeleton file. If we got
12583 : : a skeleton file, replace the given dwflmod and dbg, with one
12584 : : derived from the skeleton file to provide enough context. */
12585 : 206 : uint64_t split_id;
12586 [ + + ]: 206 : if (is_split_dwarf (dbg, &split_id, &split_cu))
12587 : : {
12588 [ + - ]: 18 : if (dwarf_skeleton != NULL)
12589 : 18 : skel_name = strdup (dwarf_skeleton);
12590 : : else
12591 : : {
12592 : : /* Replace file.dwo with file.o and see if that matches. */
12593 : 0 : const char *fname;
12594 : 0 : dwfl_module_info (dwflmod, NULL, NULL, NULL, NULL, NULL,
12595 : : &fname, NULL);
12596 [ # # ]: 0 : if (fname != NULL)
12597 : : {
12598 : 0 : size_t flen = strlen (fname);
12599 [ # # # # ]: 0 : if (flen > 4 && strcmp (".dwo", fname + flen - 4) == 0)
12600 : : {
12601 : 0 : skel_name = strdup (fname);
12602 [ # # ]: 0 : if (skel_name != NULL)
12603 : : {
12604 : 0 : skel_name[flen - 3] = 'o';
12605 : 0 : skel_name[flen - 2] = '\0';
12606 : : }
12607 : : }
12608 : : }
12609 : : }
12610 : :
12611 [ - + ]: 18 : if (skel_name != NULL)
12612 : : {
12613 : 18 : int skel_fd = open (skel_name, O_RDONLY);
12614 [ - + ]: 18 : if (skel_fd == -1)
12615 : 0 : fprintf (stderr, "Warning: Couldn't open DWARF skeleton file"
12616 : : " '%s'\n", skel_name);
12617 : : else
12618 : : {
12619 : 18 : skel_dwfl = create_dwfl (skel_fd, skel_name);
12620 : :
12621 : : /* skel_fd was dup'ed by create_dwfl. We can close the
12622 : : original now. */
12623 : 18 : close (skel_fd);
12624 : : }
12625 : :
12626 [ + - ]: 18 : if (skel_dwfl != NULL)
12627 : : {
12628 [ - + ]: 18 : if (dwfl_getmodules (skel_dwfl, &getone_dwflmod,
12629 : : &skel_mod, 0) != 0)
12630 : : {
12631 : 0 : fprintf (stderr, "Warning: Bad DWARF skeleton,"
12632 : : " multiple modules '%s'\n", skel_name);
12633 : 0 : dwfl_end (skel_dwfl);
12634 : 0 : skel_mod = NULL;
12635 : : }
12636 : : }
12637 [ # # ]: 0 : else if (skel_fd != -1)
12638 : 0 : fprintf (stderr, "Warning: Couldn't create skeleton dwfl for"
12639 : : " '%s': %s\n", skel_name, dwfl_errmsg (-1));
12640 : :
12641 [ + - ]: 18 : if (skel_mod != NULL)
12642 : : {
12643 : 18 : Dwarf *skel_dbg = dwfl_module_getdwarf (skel_mod, &dwbias);
12644 [ + - ]: 18 : if (skel_dbg != NULL)
12645 : : {
12646 : : /* First check the skeleton CU DIE, only fetch
12647 : : the split DIE if we know the id matches to
12648 : : not unnecessary search for any split DIEs we
12649 : : don't need. */
12650 : 18 : Dwarf_CU *cu = NULL;
12651 : 18 : while (dwarf_get_units (skel_dbg, cu, &cu,
12652 [ + - ]: 26 : NULL, NULL, NULL, NULL) == 0)
12653 : : {
12654 : 26 : uint8_t unit_type;
12655 : 26 : uint64_t skel_id;
12656 [ + - ]: 26 : if (dwarf_cu_info (cu, NULL, &unit_type, NULL, NULL,
12657 : : &skel_id, NULL, NULL) == 0
12658 [ + - ]: 26 : && unit_type == DW_UT_skeleton
12659 [ + + ]: 26 : && split_id == skel_id)
12660 : : {
12661 : 18 : Dwarf_Die subdie;
12662 [ + - ]: 18 : if (dwarf_cu_info (cu, NULL, NULL, NULL,
12663 : : &subdie,
12664 : : NULL, NULL, NULL) == 0
12665 [ + - ]: 18 : && dwarf_tag (&subdie) != DW_TAG_invalid)
12666 : : {
12667 : 18 : split_dbg = dwarf_cu_getdwarf (subdie.cu);
12668 [ - + ]: 18 : if (split_dbg == NULL)
12669 : 0 : fprintf (stderr,
12670 : : "Warning: Couldn't get split_dbg:"
12671 : : " %s\n", dwarf_errmsg (-1));
12672 : 0 : break;
12673 : : }
12674 : : else
12675 : : {
12676 : : /* Everything matches up, but not
12677 : : according to libdw. Which means
12678 : : the user knew better. So...
12679 : : Terrible hack... We can never
12680 : : destroy the underlying dwfl
12681 : : because it would free the wrong
12682 : : Dwarfs... So we leak memory...*/
12683 [ # # ]: 0 : if (cu->split == NULL
12684 [ # # ]: 0 : && dwarf_skeleton != NULL)
12685 : : {
12686 : 0 : do_not_close_dwfl = true;
12687 : 0 : __libdw_link_skel_split (cu, split_cu);
12688 : 0 : split_dbg = dwarf_cu_getdwarf (split_cu);
12689 : 0 : break;
12690 : : }
12691 : : else
12692 : 0 : fprintf (stderr, "Warning: Couldn't get"
12693 : : " skeleton subdie: %s\n",
12694 : : dwarf_errmsg (-1));
12695 : : }
12696 : : }
12697 : : }
12698 [ - - ]: 18 : if (split_dbg == NULL)
12699 : 0 : fprintf (stderr, "Warning: '%s' didn't contain a skeleton for split id %" PRIx64 "\n", skel_name, split_id);
12700 : : }
12701 : : else
12702 : 0 : fprintf (stderr, "Warning: Couldn't get skeleton DWARF:"
12703 : : " %s\n", dwfl_errmsg (-1));
12704 : : }
12705 : : }
12706 : :
12707 : 0 : if (split_dbg != NULL)
12708 : : {
12709 : 18 : dbg = split_dbg;
12710 : 18 : dwflmod = skel_mod;
12711 : : }
12712 [ # # ]: 0 : else if (skel_name == NULL)
12713 : 0 : fprintf (stderr,
12714 : : "Warning: split DWARF file, but no skeleton found.\n");
12715 : : }
12716 [ + - ]: 188 : else if (dwarf_skeleton != NULL)
12717 : 0 : fprintf (stderr, "Warning: DWARF skeleton given,"
12718 : : " but not a split DWARF file\n");
12719 : : }
12720 : :
12721 : : /* Get the section header string table index. */
12722 : 506 : size_t shstrndx;
12723 [ - + ]: 506 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
12724 : 0 : error_exit (0, _("cannot get section header string table index"));
12725 : :
12726 : 506 : job_data *jdatalist = NULL;
12727 : :
12728 : : /* If the .debug_info section is listed as implicitly required then
12729 : : we must make sure to handle it before handling any other debug
12730 : : section. Various other sections depend on the CU DIEs being
12731 : : scanned (silently) first. */
12732 [ + + ]: 506 : if (implicit_info)
12733 : : {
12734 : : Elf_Scn *scn = NULL;
12735 [ + - ]: 3134 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
12736 : : {
12737 : 3134 : GElf_Shdr shdr_mem;
12738 : 3134 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
12739 : :
12740 [ + - + + ]: 3134 : if (shdr != NULL && shdr->sh_type == SHT_PROGBITS)
12741 : : {
12742 : 2452 : const char *name = elf_strptr (ebl->elf, shstrndx,
12743 : 1226 : shdr->sh_name);
12744 [ - + ]: 1226 : if (name == NULL)
12745 : 0 : continue;
12746 : :
12747 [ + + ]: 1226 : if (strcmp (name, ".debug_info") == 0
12748 [ + + ]: 1094 : || strcmp (name, ".debug_info.dwo") == 0
12749 [ + + ]: 1076 : || strcmp (name, ".zdebug_info") == 0
12750 [ + - ]: 1064 : || strcmp (name, ".zdebug_info.dwo") == 0
12751 [ + + ]: 1064 : || strcmp (name, ".gnu.debuglto_.debug_info") == 0)
12752 : : {
12753 : 164 : print_debug_info_section (dwflmod, ebl, ehdr,
12754 : : scn, shdr, dbg, stdout);
12755 : 164 : break;
12756 : : }
12757 : : }
12758 : : }
12759 : :
12760 : 164 : print_debug_sections &= ~section_info;
12761 : 164 : implicit_debug_sections &= ~section_info;
12762 : : }
12763 : :
12764 : : /* Look through all the sections for the debugging sections to print. */
12765 : : Elf_Scn *scn = NULL;
12766 [ + + ]: 16642 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
12767 : : {
12768 : 16136 : GElf_Shdr shdr_mem;
12769 : 16136 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
12770 : :
12771 [ + - + + ]: 16136 : if (shdr != NULL && shdr->sh_type == SHT_PROGBITS)
12772 : : {
12773 : 6800 : static const struct
12774 : : {
12775 : : const char *name;
12776 : : enum section_e bitmask;
12777 : : void (*fp) (Dwfl_Module *, Ebl *,
12778 : : GElf_Ehdr *, Elf_Scn *, GElf_Shdr *, Dwarf *, FILE *);
12779 : : } debug_sections[] =
12780 : : {
12781 : : #define NEW_SECTION(name) \
12782 : : { ".debug_" #name, section_##name, print_debug_##name##_section }
12783 : : NEW_SECTION (abbrev),
12784 : : NEW_SECTION (addr),
12785 : : NEW_SECTION (aranges),
12786 : : NEW_SECTION (frame),
12787 : : NEW_SECTION (info),
12788 : : NEW_SECTION (types),
12789 : : NEW_SECTION (line),
12790 : : NEW_SECTION (loc),
12791 : : /* loclists is loc for DWARF5. */
12792 : : { ".debug_loclists", section_loc,
12793 : : print_debug_loclists_section },
12794 : : NEW_SECTION (pubnames),
12795 : : NEW_SECTION (str),
12796 : : /* A DWARF5 specialised debug string section. */
12797 : : { ".debug_line_str", section_str,
12798 : : print_debug_str_section },
12799 : : /* DWARF5 string offsets table. */
12800 : : { ".debug_str_offsets", section_str,
12801 : : print_debug_str_offsets_section },
12802 : : NEW_SECTION (macinfo),
12803 : : NEW_SECTION (macro),
12804 : : NEW_SECTION (ranges),
12805 : : /* rnglists is ranges for DWARF5. */
12806 : : { ".debug_rnglists", section_ranges,
12807 : : print_debug_rnglists_section },
12808 : : { ".eh_frame", section_frame | section_exception,
12809 : : print_debug_frame_section },
12810 : : { ".eh_frame_hdr", section_frame | section_exception,
12811 : : print_debug_frame_hdr_section },
12812 : : { ".gcc_except_table", section_frame | section_exception,
12813 : : print_debug_exception_table },
12814 : : { ".gdb_index", section_gdb_index, print_gdb_index_section },
12815 : : { ".debug_cu_index", section_cu_index, print_cu_index_section },
12816 : : { ".debug_tu_index", section_cu_index, print_cu_index_section },
12817 : : };
12818 : 6800 : const int ndebug_sections = (sizeof (debug_sections)
12819 : : / sizeof (debug_sections[0]));
12820 : 13600 : const char *name = elf_strptr (ebl->elf, shstrndx,
12821 : 6800 : shdr->sh_name);
12822 [ - + ]: 6800 : if (name == NULL)
12823 : 0 : continue;
12824 : :
12825 : : int n;
12826 [ + + ]: 113950 : for (n = 0; n < ndebug_sections; ++n)
12827 : : {
12828 : 110484 : size_t dbglen = strlen (debug_sections[n].name);
12829 : 110484 : size_t scnlen = strlen (name);
12830 [ + + ]: 110484 : if ((strncmp (name, debug_sections[n].name, dbglen) == 0
12831 [ + + ]: 3604 : && (dbglen == scnlen
12832 [ + + ]: 584 : || (scnlen == dbglen + 4
12833 [ + + ]: 498 : && strstr (name, ".dwo") == name + dbglen)))
12834 [ + + + + ]: 107324 : || (name[0] == '.' && name[1] == 'z'
12835 [ + - ]: 1132 : && debug_sections[n].name[1] == 'd'
12836 [ + + ]: 1132 : && strncmp (&name[2], &debug_sections[n].name[1],
12837 : : dbglen - 1) == 0
12838 [ - + ]: 164 : && (scnlen == dbglen + 1
12839 [ # # ]: 0 : || (scnlen == dbglen + 5
12840 [ # # ]: 0 : && strstr (name, ".dwo") == name + dbglen + 1)))
12841 [ + + ]: 107160 : || (scnlen > 14 /* .gnu.debuglto_ prefix. */
12842 [ + + ]: 11112 : && startswith (name, ".gnu.debuglto_")
12843 [ + + ]: 72 : && strcmp (&name[14], debug_sections[n].name) == 0)
12844 : : )
12845 : : {
12846 : 3334 : if (((print_debug_sections | implicit_debug_sections)
12847 [ + + ]: 3334 : & debug_sections[n].bitmask))
12848 : 1102 : schedule_job (&jdatalist, debug_sections[n].fp,
12849 : : dwflmod, ebl, ehdr, scn, shdr, dbg);
12850 : :
12851 : : break;
12852 : : }
12853 : : }
12854 : : }
12855 : : }
12856 : :
12857 : : #ifdef USE_LOCKS
12858 : : /* If max_threads <= 1, then jobs were immediately run in schedule_job. */
12859 : : if (max_threads > 1)
12860 : : run_jobs (max_threads);
12861 : : #endif
12862 : :
12863 : 506 : dwfl_end (skel_dwfl);
12864 : 506 : free (skel_name);
12865 : :
12866 : 506 : while (jdatalist != NULL)
12867 : : {
12868 : : job_data *jdata = jdatalist;
12869 : : jdatalist = jdatalist->next;
12870 : : free (jdata);
12871 : : }
12872 : :
12873 : : /* Turn implicit and/or explicit back on in case we go over another file. */
12874 [ + + ]: 506 : if (implicit_info)
12875 : 164 : implicit_debug_sections |= section_info;
12876 [ + + ]: 506 : if (explicit_info)
12877 : 134 : print_debug_sections |= section_info;
12878 : :
12879 : 506 : reset_listptr (&known_locsptr);
12880 : 506 : reset_listptr (&known_loclistsptr);
12881 : 506 : reset_listptr (&known_rangelistptr);
12882 : 506 : reset_listptr (&known_rnglistptr);
12883 : 506 : reset_listptr (&known_addrbases);
12884 : 506 : reset_listptr (&known_stroffbases);
12885 : 506 : }
12886 : :
12887 : :
12888 : : #define ITEM_INDENT 4
12889 : : #define WRAP_COLUMN 75
12890 : :
12891 : : /* Print "NAME: FORMAT", wrapping when output text would make the line
12892 : : exceed WRAP_COLUMN. Unpadded numbers look better for the core items
12893 : : but this function is also used for registers which should be printed
12894 : : aligned. Fortunately registers output uses fixed fields width (such
12895 : : as %11d) for the alignment.
12896 : :
12897 : : Line breaks should not depend on the particular values although that
12898 : : may happen in some cases of the core items. */
12899 : :
12900 : : static unsigned int
12901 : : __attribute__ ((format (printf, 6, 7)))
12902 : 1642 : print_core_item (unsigned int colno, char sep, unsigned int wrap,
12903 : : size_t name_width, const char *name, const char *format, ...)
12904 : : {
12905 : 1642 : size_t len = strlen (name);
12906 : 1642 : if (name_width < len)
12907 : : name_width = len;
12908 : :
12909 : 1642 : char *out;
12910 : 1642 : va_list ap;
12911 : 1642 : va_start (ap, format);
12912 : 1642 : int out_len = vasprintf (&out, format, ap);
12913 : 1642 : va_end (ap);
12914 [ - + ]: 1642 : if (out_len == -1)
12915 : 0 : error_exit (0, _("memory exhausted"));
12916 : :
12917 : 1642 : size_t n = name_width + sizeof ": " - 1 + out_len;
12918 : :
12919 [ + + ]: 1642 : if (colno == 0)
12920 : : {
12921 : 102 : printf ("%*s", ITEM_INDENT, "");
12922 : 102 : colno = ITEM_INDENT + n;
12923 : : }
12924 [ + + ]: 1540 : else if (colno + 2 + n < wrap)
12925 : : {
12926 : 936 : printf ("%c ", sep);
12927 : 936 : colno += 2 + n;
12928 : : }
12929 : : else
12930 : : {
12931 : 604 : printf ("\n%*s", ITEM_INDENT, "");
12932 : 604 : colno = ITEM_INDENT + n;
12933 : : }
12934 : :
12935 : 1642 : printf ("%s: %*s%s", name, (int) (name_width - len), "", out);
12936 : :
12937 : 1642 : free (out);
12938 : :
12939 : 1642 : return colno;
12940 : : }
12941 : :
12942 : : static const void *
12943 : 1862 : convert (Elf *core, Elf_Type type, uint_fast16_t count,
12944 : : void *value, const void *data, size_t size)
12945 : : {
12946 : 3724 : Elf_Data valuedata =
12947 : : {
12948 : : .d_type = type,
12949 : : .d_buf = value,
12950 [ + + ]: 1862 : .d_size = size ?: gelf_fsize (core, type, count, EV_CURRENT),
12951 : : .d_version = EV_CURRENT,
12952 : : };
12953 : 1862 : Elf_Data indata =
12954 : : {
12955 : : .d_type = type,
12956 : : .d_buf = (void *) data,
12957 : : .d_size = valuedata.d_size,
12958 : : .d_version = EV_CURRENT,
12959 : : };
12960 : :
12961 : 1862 : Elf_Data *d = (gelf_getclass (core) == ELFCLASS32
12962 [ + + ]: 1862 : ? elf32_xlatetom : elf64_xlatetom)
12963 : 1862 : (&valuedata, &indata, elf_getident (core, NULL)[EI_DATA]);
12964 [ - + ]: 1862 : if (d == NULL)
12965 : 0 : error_exit (0, _("cannot convert core note data: %s"),
12966 : : elf_errmsg (-1));
12967 : :
12968 : 1862 : return data + indata.d_size;
12969 : : }
12970 : :
12971 : : typedef uint8_t GElf_Byte;
12972 : :
12973 : : static unsigned int
12974 : 800 : handle_core_item (Elf *core, const GElf_Ehdr *ehdr,
12975 : : const Ebl_Core_Item *item, const void *desc,
12976 : : unsigned int colno, size_t *repeated_size)
12977 : : {
12978 [ + + ]: 800 : uint_fast16_t count = item->count ?: 1;
12979 : : /* Ebl_Core_Item count is always a small number.
12980 : : Make sure the backend didn't put in some large bogus value. */
12981 [ - + ]: 124 : assert (count < 128);
12982 : :
12983 : : #define TYPES \
12984 : : DO_TYPE (BYTE, Byte, "0x%.2" PRIx8, "%" PRId8); \
12985 : : DO_TYPE (HALF, Half, "0x%.4" PRIx16, "%" PRId16); \
12986 : : DO_TYPE (WORD, Word, "0x%.8" PRIx32, "%" PRId32); \
12987 : : DO_TYPE (SWORD, Sword, "%" PRId32, "%" PRId32); \
12988 : : DO_TYPE (XWORD, Xword, "0x%.16" PRIx64, "%" PRId64); \
12989 : : DO_TYPE (SXWORD, Sxword, "%" PRId64, "%" PRId64)
12990 : :
12991 : : #define DO_TYPE(NAME, Name, hex, dec) GElf_##Name Name
12992 : 800 : typedef union { TYPES; } value_t;
12993 : 800 : void *data = alloca (count * sizeof (value_t));
12994 : : #undef DO_TYPE
12995 : :
12996 : : #define DO_TYPE(NAME, Name, hex, dec) \
12997 : : GElf_##Name *value_##Name __attribute__((unused)) = data
12998 : 800 : TYPES;
12999 : : #undef DO_TYPE
13000 : :
13001 : 800 : size_t size = gelf_fsize (core, item->type, count, EV_CURRENT);
13002 : 800 : size_t convsize = size;
13003 [ + + ]: 800 : if (repeated_size != NULL)
13004 : : {
13005 [ + - - + ]: 2 : if (*repeated_size > size && (item->format == 'b' || item->format == 'B'))
13006 : : {
13007 : 0 : data = alloca (*repeated_size);
13008 : 0 : count *= *repeated_size / size;
13009 : 0 : convsize = count * size;
13010 : 0 : *repeated_size -= convsize;
13011 : : }
13012 [ + - + - ]: 2 : else if (item->count != 0 || item->format != '\n')
13013 : 0 : *repeated_size -= size;
13014 : : }
13015 : :
13016 : 800 : convert (core, item->type, count, data, desc + item->offset, convsize);
13017 : :
13018 : 800 : Elf_Type type = item->type;
13019 [ + + ]: 800 : if (type == ELF_T_ADDR)
13020 [ + - ]: 2 : type = gelf_getclass (core) == ELFCLASS32 ? ELF_T_WORD : ELF_T_XWORD;
13021 : :
13022 [ + + + + : 800 : switch (item->format)
+ + + +
- ]
13023 : : {
13024 : 386 : case 'd':
13025 [ - + ]: 386 : assert (count == 1);
13026 [ + + + + : 386 : switch (type)
- + - ]
13027 : : {
13028 : : #define DO_TYPE(NAME, Name, hex, dec) \
13029 : : case ELF_T_##NAME: \
13030 : : colno = print_core_item (colno, ',', WRAP_COLUMN, \
13031 : : 0, item->name, dec, value_##Name[0]); \
13032 : : break
13033 : 386 : TYPES;
13034 : : #undef DO_TYPE
13035 : 0 : default:
13036 : 0 : abort ();
13037 : : }
13038 : : break;
13039 : :
13040 : 222 : case 'x':
13041 [ - + ]: 222 : assert (count == 1);
13042 [ - - + - : 222 : switch (type)
+ - - ]
13043 : : {
13044 : : #define DO_TYPE(NAME, Name, hex, dec) \
13045 : : case ELF_T_##NAME: \
13046 : : colno = print_core_item (colno, ',', WRAP_COLUMN, \
13047 : : 0, item->name, hex, value_##Name[0]); \
13048 : : break
13049 : 222 : TYPES;
13050 : : #undef DO_TYPE
13051 : 0 : default:
13052 : 0 : abort ();
13053 : : }
13054 : : break;
13055 : :
13056 : 44 : case 'b':
13057 : : case 'B':
13058 [ - + ]: 44 : assert (size % sizeof (unsigned int) == 0);
13059 : 44 : unsigned int nbits = count * size * 8;
13060 : 44 : unsigned int pop = 0;
13061 [ + + ]: 112 : for (const unsigned int *i = data; (void *) i < data + count * size; ++i)
13062 : 68 : pop += __builtin_popcount (*i);
13063 : 44 : bool negate = pop > nbits / 2;
13064 : 44 : const unsigned int bias = item->format == 'b';
13065 : :
13066 : 44 : {
13067 [ - + ]: 44 : char printed[(negate ? nbits - pop : pop) * 16 + 1];
13068 : 44 : char *p = printed;
13069 : 44 : *p = '\0';
13070 : :
13071 : 44 : if (BYTE_ORDER != LITTLE_ENDIAN && size > sizeof (unsigned int))
13072 : : {
13073 : : assert (size == sizeof (unsigned int) * 2);
13074 : : for (unsigned int *i = data;
13075 : : (void *) i < data + count * size; i += 2)
13076 : : {
13077 : : unsigned int w = i[1];
13078 : : i[1] = i[0];
13079 : : i[0] = w;
13080 : : }
13081 : : }
13082 : :
13083 : 44 : unsigned int lastbit = 0;
13084 : 44 : unsigned int run = 0;
13085 : 44 : for (const unsigned int *i = data;
13086 [ + + ]: 112 : (void *) i < data + count * size; ++i)
13087 : : {
13088 : 68 : unsigned int bit = ((void *) i - data) * 8;
13089 [ - + ]: 68 : unsigned int w = negate ? ~*i : *i;
13090 [ - + ]: 68 : while (w != 0)
13091 : : {
13092 : : /* Note that a right shift equal to (or greater than)
13093 : : the number of bits of w is undefined behaviour. In
13094 : : particular when the least significant bit is bit 32
13095 : : (w = 0x8000000) then w >>= n is undefined. So
13096 : : explicitly handle that case separately. */
13097 : 0 : unsigned int n = ffs (w);
13098 [ # # ]: 0 : if (n < sizeof (w) * 8)
13099 : 0 : w >>= n;
13100 : : else
13101 : : w = 0;
13102 : 0 : bit += n;
13103 : :
13104 [ # # # # ]: 0 : if (lastbit != 0 && lastbit + 1 == bit)
13105 : 0 : ++run;
13106 : : else
13107 : : {
13108 : 0 : if (lastbit == 0)
13109 : 0 : p += sprintf (p, "%u", bit - bias);
13110 [ # # ]: 0 : else if (run == 0)
13111 : 0 : p += sprintf (p, ",%u", bit - bias);
13112 : : else
13113 : 0 : p += sprintf (p, "-%u,%u", lastbit - bias, bit - bias);
13114 : : run = 0;
13115 : : }
13116 : :
13117 : : lastbit = bit;
13118 : : }
13119 : : }
13120 [ - + - - ]: 44 : if (lastbit > 0 && run > 0 && lastbit + 1 != nbits)
13121 : 0 : p += sprintf (p, "-%u", lastbit - bias);
13122 : :
13123 [ + - ]: 88 : colno = print_core_item (colno, ',', WRAP_COLUMN, 0, item->name,
13124 : : negate ? "~<%s>" : "<%s>", printed);
13125 : : }
13126 : 44 : break;
13127 : :
13128 : 88 : case 'T':
13129 : : case (char) ('T'|0x80):
13130 [ - + ]: 88 : assert (count == 2);
13131 : 88 : Dwarf_Word sec;
13132 : 88 : Dwarf_Word usec;
13133 [ - - + - : 88 : switch (type)
+ - - ]
13134 : : {
13135 : : #define DO_TYPE(NAME, Name, hex, dec) \
13136 : : case ELF_T_##NAME: \
13137 : : sec = value_##Name[0]; \
13138 : : usec = value_##Name[1]; \
13139 : : break
13140 : 88 : TYPES;
13141 : : #undef DO_TYPE
13142 : 0 : default:
13143 : 0 : abort ();
13144 : : }
13145 [ - + ]: 88 : if (unlikely (item->format == (char) ('T'|0x80)))
13146 : : {
13147 : : /* This is a hack for an ill-considered 64-bit ABI where
13148 : : tv_usec is actually a 32-bit field with 32 bits of padding
13149 : : rounding out struct timeval. We've already converted it as
13150 : : a 64-bit field. For little-endian, this just means the
13151 : : high half is the padding; it's presumably zero, but should
13152 : : be ignored anyway. For big-endian, it means the 32-bit
13153 : : field went into the high half of USEC. */
13154 [ # # ]: 0 : if (likely (ehdr->e_ident[EI_DATA] == ELFDATA2MSB))
13155 : 0 : usec >>= 32;
13156 : : else
13157 : 0 : usec &= UINT32_MAX;
13158 : : }
13159 : 88 : colno = print_core_item (colno, ',', WRAP_COLUMN, 0, item->name,
13160 : : "%" PRIu64 ".%.6" PRIu64, sec, usec);
13161 : 88 : break;
13162 : :
13163 : 18 : case 'c':
13164 [ - + ]: 18 : assert (count == 1);
13165 : 18 : colno = print_core_item (colno, ',', WRAP_COLUMN, 0, item->name,
13166 : 18 : "%c", value_Byte[0]);
13167 : 18 : break;
13168 : :
13169 : 36 : case 's':
13170 : 36 : colno = print_core_item (colno, ',', WRAP_COLUMN, 0, item->name,
13171 : : "%.*s", (int) count, value_Byte);
13172 : 36 : break;
13173 : :
13174 : 2 : case '\n':
13175 : : /* This is a list of strings separated by '\n'. */
13176 [ - + ]: 2 : assert (item->count == 0);
13177 [ - + ]: 2 : assert (repeated_size != NULL);
13178 [ - + ]: 2 : assert (item->name == NULL);
13179 [ - + ]: 2 : if (unlikely (item->offset >= *repeated_size))
13180 : : break;
13181 : :
13182 : 2 : const char *s = desc + item->offset;
13183 : 2 : size = *repeated_size - item->offset;
13184 : 2 : *repeated_size = 0;
13185 [ + - ]: 96 : while (size > 0)
13186 : : {
13187 : 96 : const char *eol = memchr (s, '\n', size);
13188 : 96 : int len = size;
13189 [ + + ]: 96 : if (eol != NULL)
13190 : 94 : len = eol - s;
13191 : 96 : printf ("%*s%.*s\n", ITEM_INDENT, "", len, s);
13192 [ + + ]: 96 : if (eol == NULL)
13193 : : break;
13194 : 94 : size -= eol + 1 - s;
13195 : 94 : s = eol + 1;
13196 : : }
13197 : :
13198 : : colno = WRAP_COLUMN;
13199 : : break;
13200 : :
13201 : : case 'h':
13202 : : break;
13203 : :
13204 : 0 : default:
13205 : 0 : error (0, 0, "XXX not handling format '%c' for %s",
13206 : 0 : item->format, item->name);
13207 : 0 : break;
13208 : : }
13209 : :
13210 : : #undef TYPES
13211 : :
13212 : 800 : return colno;
13213 : : }
13214 : :
13215 : :
13216 : : /* Sort items by group, and by layout offset within each group. */
13217 : : static int
13218 : 1790 : compare_core_items (const void *a, const void *b)
13219 : : {
13220 : 1790 : const Ebl_Core_Item *const *p1 = a;
13221 : 1790 : const Ebl_Core_Item *const *p2 = b;
13222 : 1790 : const Ebl_Core_Item *item1 = *p1;
13223 : 1790 : const Ebl_Core_Item *item2 = *p2;
13224 : :
13225 : 1790 : return ((item1->group == item2->group ? 0
13226 [ + + ]: 1790 : : strcmp (item1->group, item2->group))
13227 [ - + ]: 1790 : ?: (int) item1->offset - (int) item2->offset);
13228 : : }
13229 : :
13230 : : /* Sort item groups by layout offset of the first item in the group. */
13231 : : static int
13232 : 278 : compare_core_item_groups (const void *a, const void *b)
13233 : : {
13234 : 278 : const Ebl_Core_Item *const *const *p1 = a;
13235 : 278 : const Ebl_Core_Item *const *const *p2 = b;
13236 : 278 : const Ebl_Core_Item *const *group1 = *p1;
13237 : 278 : const Ebl_Core_Item *const *group2 = *p2;
13238 : 278 : const Ebl_Core_Item *item1 = *group1;
13239 : 278 : const Ebl_Core_Item *item2 = *group2;
13240 : :
13241 : 278 : return (int) item1->offset - (int) item2->offset;
13242 : : }
13243 : :
13244 : : static unsigned int
13245 : 74 : handle_core_items (Elf *core, const GElf_Ehdr *ehdr,
13246 : : const void *desc, size_t descsz,
13247 : : const Ebl_Core_Item *items, size_t nitems)
13248 : 74 : {
13249 [ + + ]: 74 : if (nitems == 0)
13250 : : return 0;
13251 : 68 : unsigned int colno = 0;
13252 : :
13253 : : /* FORMAT '\n' makes sense to be present only as a single item as it
13254 : : processes all the data of a note. FORMATs 'b' and 'B' have a special case
13255 : : if present as a single item but they can be also processed with other
13256 : : items below. */
13257 [ + + + + ]: 68 : if (nitems == 1 && (items[0].format == '\n' || items[0].format == 'b'
13258 [ + - ]: 16 : || items[0].format == 'B'))
13259 : : {
13260 [ - + ]: 2 : assert (items[0].offset == 0);
13261 : 2 : size_t size = descsz;
13262 : 2 : colno = handle_core_item (core, ehdr, items, desc, colno, &size);
13263 : : /* If SIZE is not zero here there is some remaining data. But we do not
13264 : : know how to process it anyway. */
13265 : 2 : return colno;
13266 : : }
13267 [ + + ]: 848 : for (size_t i = 0; i < nitems; ++i)
13268 [ - + ]: 782 : assert (items[i].format != '\n');
13269 : :
13270 : : /* Sort to collect the groups together. */
13271 : 66 : const Ebl_Core_Item *sorted_items[nitems];
13272 [ + + ]: 848 : for (size_t i = 0; i < nitems; ++i)
13273 : 782 : sorted_items[i] = &items[i];
13274 : 66 : qsort (sorted_items, nitems, sizeof sorted_items[0], &compare_core_items);
13275 : :
13276 : : /* Collect the unique groups and sort them. */
13277 : 66 : const Ebl_Core_Item **groups[nitems];
13278 : 66 : groups[0] = &sorted_items[0];
13279 : 66 : size_t ngroups = 1;
13280 [ + + ]: 782 : for (size_t i = 1; i < nitems; ++i)
13281 [ + + ]: 716 : if (sorted_items[i]->group != sorted_items[i - 1]->group
13282 [ + - ]: 150 : && strcmp (sorted_items[i]->group, sorted_items[i - 1]->group))
13283 : 150 : groups[ngroups++] = &sorted_items[i];
13284 : 66 : qsort (groups, ngroups, sizeof groups[0], &compare_core_item_groups);
13285 : :
13286 : : /* Write out all the groups. */
13287 : 66 : const void *last = desc;
13288 : 70 : do
13289 : : {
13290 [ + + ]: 290 : for (size_t i = 0; i < ngroups; ++i)
13291 : : {
13292 : 220 : for (const Ebl_Core_Item **item = groups[i];
13293 : 1018 : (item < &sorted_items[nitems]
13294 [ + + + + ]: 1018 : && ((*item)->group == groups[i][0]->group
13295 [ - + ]: 150 : || !strcmp ((*item)->group, groups[i][0]->group)));
13296 : 798 : ++item)
13297 : 798 : colno = handle_core_item (core, ehdr, *item, desc, colno, NULL);
13298 : :
13299 : : /* Force a line break at the end of the group. */
13300 : 220 : colno = WRAP_COLUMN;
13301 : : }
13302 : :
13303 [ + + ]: 70 : if (descsz == 0)
13304 : : break;
13305 : :
13306 : : /* This set of items consumed a certain amount of the note's data.
13307 : : If there is more data there, we have another unit of the same size.
13308 : : Loop to print that out too. */
13309 : 40 : const Ebl_Core_Item *item = &items[nitems - 1];
13310 : 80 : size_t eltsz = item->offset + gelf_fsize (core, item->type,
13311 : 40 : item->count ?: 1, EV_CURRENT);
13312 : :
13313 : 40 : int reps = -1;
13314 : 40 : do
13315 : : {
13316 : 40 : ++reps;
13317 : 40 : desc += eltsz;
13318 : 40 : descsz -= eltsz;
13319 : : }
13320 [ + + - + ]: 40 : while (descsz >= eltsz && !memcmp (desc, last, eltsz));
13321 : :
13322 [ + - ]: 40 : if (reps == 1)
13323 : : {
13324 : : /* For just one repeat, print it unabridged twice. */
13325 : : desc -= eltsz;
13326 : : descsz += eltsz;
13327 : : }
13328 [ + - ]: 40 : else if (reps > 1)
13329 : 0 : printf (_("\n%*s... <repeats %u more times> ..."),
13330 : : ITEM_INDENT, "", reps);
13331 : :
13332 : 40 : last = desc;
13333 : : }
13334 [ + + ]: 40 : while (descsz > 0);
13335 : :
13336 : : return colno;
13337 : : }
13338 : :
13339 : : static unsigned int
13340 : 324 : handle_core_register (Ebl *ebl, Elf *core, int maxregname,
13341 : : const Ebl_Register_Location *regloc, const void *desc,
13342 : : unsigned int colno)
13343 : : {
13344 [ - + ]: 324 : if (regloc->bits % 8 != 0)
13345 : : {
13346 : 0 : error (0, 0, "Warning: Cannot handle register with %" PRIu8 "bits\n",
13347 : : regloc->bits);
13348 : 0 : return colno;
13349 : : }
13350 : :
13351 : 324 : desc += regloc->offset;
13352 : :
13353 [ + + ]: 1172 : for (int reg = regloc->regno; reg < regloc->regno + regloc->count; ++reg)
13354 : : {
13355 : 848 : char name[REGNAMESZ];
13356 : 848 : int bits;
13357 : 848 : int type;
13358 : 848 : register_info (ebl, reg, regloc, name, &bits, &type);
13359 : :
13360 : : #define TYPES \
13361 : : BITS (8, BYTE, "%4" PRId8, "0x%.2" PRIx8); \
13362 : : BITS (16, HALF, "%6" PRId16, "0x%.4" PRIx16); \
13363 : : BITS (32, WORD, "%11" PRId32, " 0x%.8" PRIx32); \
13364 : : BITS (64, XWORD, "%20" PRId64, " 0x%.16" PRIx64)
13365 : :
13366 : : #define BITS(bits, xtype, sfmt, ufmt) \
13367 : : uint##bits##_t b##bits; int##bits##_t b##bits##s
13368 : 848 : union { TYPES; uint64_t b128[2]; } value;
13369 : : #undef BITS
13370 : :
13371 [ + + ]: 848 : switch (type)
13372 : : {
13373 : 672 : case DW_ATE_unsigned:
13374 : : case DW_ATE_signed:
13375 : : case DW_ATE_address:
13376 [ - + + + : 672 : switch (bits)
+ - ]
13377 : : {
13378 : : #define BITS(bits, xtype, sfmt, ufmt) \
13379 : : case bits: \
13380 : : desc = convert (core, ELF_T_##xtype, 1, &value, desc, 0); \
13381 : : if (type == DW_ATE_signed) \
13382 : : colno = print_core_item (colno, ' ', WRAP_COLUMN, \
13383 : : maxregname, name, \
13384 : : sfmt, value.b##bits##s); \
13385 : : else \
13386 : : colno = print_core_item (colno, ' ', WRAP_COLUMN, \
13387 : : maxregname, name, \
13388 : : ufmt, value.b##bits); \
13389 : : break
13390 : :
13391 [ - - - + : 576 : TYPES;
+ + + + ]
13392 : :
13393 : 96 : case 128:
13394 [ - + ]: 96 : assert (type == DW_ATE_unsigned);
13395 : 96 : desc = convert (core, ELF_T_XWORD, 2, &value, desc, 0);
13396 : 96 : int be = elf_getident (core, NULL)[EI_DATA] == ELFDATA2MSB;
13397 : 96 : colno = print_core_item (colno, ' ', WRAP_COLUMN,
13398 : : maxregname, name,
13399 : : "0x%.16" PRIx64 "%.16" PRIx64,
13400 : 96 : value.b128[!be], value.b128[be]);
13401 : 96 : break;
13402 : :
13403 : 0 : default:
13404 : 0 : abort ();
13405 : : #undef BITS
13406 : : }
13407 : : break;
13408 : :
13409 : 176 : default:
13410 : : /* Print each byte in hex, the whole thing in native byte order. */
13411 [ - + ]: 176 : assert (bits % 8 == 0);
13412 : 176 : const uint8_t *bytes = desc;
13413 : 176 : desc += bits / 8;
13414 : 176 : char hex[bits / 4 + 1];
13415 : 176 : hex[bits / 4] = '\0';
13416 : 176 : int incr = 1;
13417 [ + + ]: 176 : if (elf_getident (core, NULL)[EI_DATA] == ELFDATA2LSB)
13418 : : {
13419 : 96 : bytes += bits / 8 - 1;
13420 : 96 : incr = -1;
13421 : : }
13422 : 176 : size_t idx = 0;
13423 [ + + ]: 1744 : for (char *h = hex; bits > 0; bits -= 8, idx += incr)
13424 : : {
13425 : 1568 : *h++ = "0123456789abcdef"[bytes[idx] >> 4];
13426 : 1568 : *h++ = "0123456789abcdef"[bytes[idx] & 0xf];
13427 : : }
13428 : 176 : colno = print_core_item (colno, ' ', WRAP_COLUMN,
13429 : : maxregname, name, "0x%s", hex);
13430 : 176 : break;
13431 : : }
13432 : 848 : desc += regloc->pad;
13433 : :
13434 : : #undef TYPES
13435 : : }
13436 : :
13437 : : return colno;
13438 : : }
13439 : :
13440 : :
13441 : : struct register_info
13442 : : {
13443 : : const Ebl_Register_Location *regloc;
13444 : : const char *set;
13445 : : char name[REGNAMESZ];
13446 : : int regno;
13447 : : int bits;
13448 : : int type;
13449 : : };
13450 : :
13451 : : static int
13452 : 4132 : register_bitpos (const struct register_info *r)
13453 : : {
13454 : 4132 : return (r->regloc->offset * 8
13455 : 4132 : + ((r->regno - r->regloc->regno)
13456 : 4132 : * (r->regloc->bits + r->regloc->pad * 8)));
13457 : : }
13458 : :
13459 : : static int
13460 : 2124 : compare_sets_by_info (const struct register_info *r1,
13461 : : const struct register_info *r2)
13462 : : {
13463 : 2124 : return ((int) r2->bits - (int) r1->bits
13464 [ + + ]: 2124 : ?: register_bitpos (r1) - register_bitpos (r2));
13465 : : }
13466 : :
13467 : : /* Sort registers by set, and by size and layout offset within each set. */
13468 : : static int
13469 : 9190 : compare_registers (const void *a, const void *b)
13470 : : {
13471 : 9190 : const struct register_info *r1 = a;
13472 : 9190 : const struct register_info *r2 = b;
13473 : :
13474 : : /* Unused elements sort last. */
13475 [ + + ]: 9190 : if (r1->regloc == NULL)
13476 : 6618 : return r2->regloc == NULL ? 0 : 1;
13477 [ + + ]: 2572 : if (r2->regloc == NULL)
13478 : : return -1;
13479 : :
13480 [ + + ]: 2242 : return ((r1->set == r2->set ? 0 : strcmp (r1->set, r2->set))
13481 [ - + ]: 2242 : ?: compare_sets_by_info (r1, r2));
13482 : : }
13483 : :
13484 : : /* Sort register sets by layout offset of the first register in the set. */
13485 : : static int
13486 : 40 : compare_register_sets (const void *a, const void *b)
13487 : : {
13488 : 40 : const struct register_info *const *p1 = a;
13489 : 40 : const struct register_info *const *p2 = b;
13490 : 40 : return compare_sets_by_info (*p1, *p2);
13491 : : }
13492 : :
13493 : : static inline bool
13494 : 1728 : same_set (const struct register_info *a,
13495 : : const struct register_info *b,
13496 : : const struct register_info *regs,
13497 : : size_t maxnreg)
13498 : : {
13499 [ + - ]: 1728 : return (a < ®s[maxnreg] && a->regloc != NULL
13500 [ + - + + ]: 1728 : && b < ®s[maxnreg] && b->regloc != NULL
13501 [ + + ]: 1692 : && a->bits == b->bits
13502 [ + - + + : 3384 : && (a->set == b->set || !strcmp (a->set, b->set)));
- + ]
13503 : : }
13504 : :
13505 : : static unsigned int
13506 : 74 : handle_core_registers (Ebl *ebl, Elf *core, const void *desc,
13507 : : const Ebl_Register_Location *reglocs, size_t nregloc)
13508 : 74 : {
13509 [ + + ]: 74 : if (nregloc == 0)
13510 : : return 0;
13511 : :
13512 : 36 : ssize_t maxnreg = ebl_register_info (ebl, 0, NULL, 0, NULL, NULL, NULL, NULL);
13513 [ - + ]: 36 : if (maxnreg <= 0)
13514 : : {
13515 [ # # ]: 0 : for (size_t i = 0; i < nregloc; ++i)
13516 : 0 : if (maxnreg < reglocs[i].regno + reglocs[i].count)
13517 : : maxnreg = reglocs[i].regno + reglocs[i].count;
13518 [ # # ]: 0 : assert (maxnreg > 0);
13519 : : }
13520 : :
13521 : 36 : struct register_info regs[maxnreg];
13522 : 36 : memset (regs, 0, sizeof regs);
13523 : :
13524 : : /* Sort to collect the sets together. */
13525 : 36 : int maxreg = 0;
13526 [ + + ]: 360 : for (size_t i = 0; i < nregloc; ++i)
13527 : 324 : for (int reg = reglocs[i].regno;
13528 [ + + ]: 1172 : reg < reglocs[i].regno + reglocs[i].count;
13529 : 848 : ++reg)
13530 : : {
13531 [ - + ]: 848 : assert (reg < maxnreg);
13532 : 848 : if (reg > maxreg)
13533 : : maxreg = reg;
13534 : 848 : struct register_info *info = ®s[reg];
13535 : 848 : info->regloc = ®locs[i];
13536 : 848 : info->regno = reg;
13537 : 848 : info->set = register_info (ebl, reg, ®locs[i],
13538 : 848 : info->name, &info->bits, &info->type);
13539 : : }
13540 : 36 : qsort (regs, maxreg + 1, sizeof regs[0], &compare_registers);
13541 : :
13542 : : /* Collect the unique sets and sort them. */
13543 : 36 : struct register_info *sets[maxreg + 1];
13544 : 36 : sets[0] = ®s[0];
13545 : 36 : size_t nsets = 1;
13546 [ + + ]: 2558 : for (int i = 1; i <= maxreg; ++i)
13547 [ + + ]: 2522 : if (regs[i].regloc != NULL
13548 [ + + ]: 812 : && !same_set (®s[i], ®s[i - 1], regs, maxnreg))
13549 : 32 : sets[nsets++] = ®s[i];
13550 : 36 : qsort (sets, nsets, sizeof sets[0], &compare_register_sets);
13551 : :
13552 : : /* Write out all the sets. */
13553 : 36 : unsigned int colno = 0;
13554 [ + + ]: 104 : for (size_t i = 0; i < nsets; ++i)
13555 : : {
13556 : : /* Find the longest name of a register in this set. */
13557 : 68 : size_t maxname = 0;
13558 : 68 : const struct register_info *end;
13559 [ + + ]: 916 : for (end = sets[i]; same_set (sets[i], end, regs, maxnreg); ++end)
13560 : : {
13561 : 848 : size_t len = strlen (end->name);
13562 : 848 : if (len > maxname)
13563 : : maxname = len;
13564 : : }
13565 : :
13566 : : for (const struct register_info *reg = sets[i];
13567 [ + + ]: 392 : reg < end;
13568 : 324 : reg += reg->regloc->count ?: 1)
13569 [ + - ]: 324 : colno = handle_core_register (ebl, core, maxname,
13570 : 324 : reg->regloc, desc, colno);
13571 : :
13572 : : /* Force a line break at the end of the group. */
13573 : 68 : colno = WRAP_COLUMN;
13574 : : }
13575 : :
13576 : : return colno;
13577 : : }
13578 : :
13579 : : static void
13580 : 18 : handle_auxv_note (Ebl *ebl, Elf *core, GElf_Word descsz, GElf_Off desc_pos)
13581 : : {
13582 : 18 : Elf_Data *data = elf_getdata_rawchunk (core, desc_pos, descsz, ELF_T_AUXV);
13583 [ - + ]: 18 : if (data == NULL)
13584 : 0 : elf_error:
13585 : 0 : error_exit (0, _("cannot convert core note data: %s"), elf_errmsg (-1));
13586 : :
13587 : 18 : const size_t nauxv = descsz / gelf_fsize (core, ELF_T_AUXV, 1, EV_CURRENT);
13588 [ + + ]: 354 : for (size_t i = 0; i < nauxv; ++i)
13589 : : {
13590 : 336 : GElf_auxv_t av_mem;
13591 : 336 : GElf_auxv_t *av = gelf_getauxv (data, i, &av_mem);
13592 [ - + ]: 336 : if (av == NULL)
13593 : 0 : goto elf_error;
13594 : :
13595 : 336 : const char *name;
13596 : 336 : const char *fmt;
13597 [ - + ]: 336 : if (ebl_auxv_info (ebl, av->a_type, &name, &fmt) == 0)
13598 : : {
13599 : : /* Unknown type. */
13600 [ # # ]: 0 : if (av->a_un.a_val == 0)
13601 : 0 : printf (" %" PRIu64 "\n", av->a_type);
13602 : : else
13603 : 0 : printf (" %" PRIu64 ": %#" PRIx64 "\n",
13604 : : av->a_type, av->a_un.a_val);
13605 : : }
13606 : : else
13607 [ + + + - : 336 : switch (fmt[0])
+ - ]
13608 : : {
13609 : 18 : case '\0': /* Normally zero. */
13610 [ + - ]: 18 : if (av->a_un.a_val == 0)
13611 : : {
13612 : 18 : printf (" %s\n", name);
13613 : 18 : break;
13614 : : }
13615 : 148 : FALLTHROUGH;
13616 : : case 'x': /* hex */
13617 : : case 'p': /* address */
13618 : : case 's': /* address of string */
13619 : 148 : printf (" %s: %#" PRIx64 "\n", name, av->a_un.a_val);
13620 : 148 : break;
13621 : 162 : case 'u':
13622 : 162 : printf (" %s: %" PRIu64 "\n", name, av->a_un.a_val);
13623 : 162 : break;
13624 : 0 : case 'd':
13625 : 0 : printf (" %s: %" PRId64 "\n", name, av->a_un.a_val);
13626 : 0 : break;
13627 : :
13628 : 8 : case 'b':
13629 : 8 : printf (" %s: %#" PRIx64 " ", name, av->a_un.a_val);
13630 : 8 : GElf_Xword bit = 1;
13631 : 8 : const char *pfx = "<";
13632 [ + + ]: 220 : for (const char *p = fmt + 1; *p != 0; p = strchr (p, '\0') + 1)
13633 : : {
13634 [ + + ]: 212 : if (av->a_un.a_val & bit)
13635 : : {
13636 : 154 : printf ("%s%s", pfx, p);
13637 : 154 : pfx = " ";
13638 : : }
13639 : 212 : bit <<= 1;
13640 : : }
13641 : 8 : printf (">\n");
13642 : 8 : break;
13643 : :
13644 : 0 : default:
13645 : 0 : abort ();
13646 : : }
13647 : : }
13648 : 18 : }
13649 : :
13650 : : static bool
13651 : 390 : buf_has_data (unsigned char const *ptr, unsigned char const *end, size_t sz)
13652 : : {
13653 [ + - + - ]: 390 : return ptr < end && (size_t) (end - ptr) >= sz;
13654 : : }
13655 : :
13656 : : static bool
13657 : 36 : buf_read_int (Elf *core, unsigned char const **ptrp, unsigned char const *end,
13658 : : int *retp)
13659 : : {
13660 [ + - ]: 36 : if (! buf_has_data (*ptrp, end, 4))
13661 : : return false;
13662 : :
13663 : 36 : *ptrp = convert (core, ELF_T_WORD, 1, retp, *ptrp, 4);
13664 : 36 : return true;
13665 : : }
13666 : :
13667 : : static bool
13668 : 354 : buf_read_ulong (Elf *core, unsigned char const **ptrp, unsigned char const *end,
13669 : : uint64_t *retp)
13670 : : {
13671 : 354 : size_t sz = gelf_fsize (core, ELF_T_ADDR, 1, EV_CURRENT);
13672 [ + - ]: 354 : if (! buf_has_data (*ptrp, end, sz))
13673 : : return false;
13674 : :
13675 : 354 : union
13676 : : {
13677 : : uint64_t u64;
13678 : : uint32_t u32;
13679 : : } u;
13680 : :
13681 : 354 : *ptrp = convert (core, ELF_T_ADDR, 1, &u, *ptrp, sz);
13682 : :
13683 [ + + ]: 354 : if (sz == 4)
13684 : 186 : *retp = u.u32;
13685 : : else
13686 : 168 : *retp = u.u64;
13687 : : return true;
13688 : : }
13689 : :
13690 : : static void
13691 : 12 : handle_siginfo_note (Elf *core, GElf_Word descsz, GElf_Off desc_pos)
13692 : : {
13693 : 12 : Elf_Data *data = elf_getdata_rawchunk (core, desc_pos, descsz, ELF_T_BYTE);
13694 [ - + ]: 12 : if (data == NULL)
13695 : 0 : error_exit (0, _("cannot convert core note data: %s"), elf_errmsg (-1));
13696 : :
13697 : 12 : unsigned char const *ptr = data->d_buf;
13698 : 12 : unsigned char const *const end = data->d_buf + data->d_size;
13699 : :
13700 : : /* Siginfo head is three ints: signal number, error number, origin
13701 : : code. */
13702 : 12 : int si_signo, si_errno, si_code;
13703 [ - + ]: 12 : if (! buf_read_int (core, &ptr, end, &si_signo)
13704 [ - + ]: 12 : || ! buf_read_int (core, &ptr, end, &si_errno)
13705 [ - + ]: 12 : || ! buf_read_int (core, &ptr, end, &si_code))
13706 : : {
13707 : 0 : fail:
13708 : 0 : printf (" Not enough data in NT_SIGINFO note.\n");
13709 : 0 : return;
13710 : : }
13711 : :
13712 : : /* Next is a pointer-aligned union of structures. On 64-bit
13713 : : machines, that implies a word of padding. */
13714 [ + + ]: 12 : if (gelf_getclass (core) == ELFCLASS64)
13715 : 6 : ptr += 4;
13716 : :
13717 : 12 : printf (" si_signo: %d, si_errno: %d, si_code: %d\n",
13718 : : si_signo, si_errno, si_code);
13719 : :
13720 [ + - ]: 12 : if (si_code > 0)
13721 [ + - ]: 12 : switch (si_signo)
13722 : : {
13723 : 12 : case CORE_SIGILL:
13724 : : case CORE_SIGFPE:
13725 : : case CORE_SIGSEGV:
13726 : : case CORE_SIGBUS:
13727 : : {
13728 : 12 : uint64_t addr;
13729 [ - + ]: 12 : if (! buf_read_ulong (core, &ptr, end, &addr))
13730 : 0 : goto fail;
13731 : 12 : printf (" fault address: %#" PRIx64 "\n", addr);
13732 : 12 : break;
13733 : : }
13734 : : default:
13735 : : ;
13736 : : }
13737 [ # # ]: 0 : else if (si_code == CORE_SI_USER)
13738 : : {
13739 : 0 : int pid, uid;
13740 [ # # ]: 0 : if (! buf_read_int (core, &ptr, end, &pid)
13741 [ # # ]: 0 : || ! buf_read_int (core, &ptr, end, &uid))
13742 : 0 : goto fail;
13743 : 0 : printf (" sender PID: %d, sender UID: %d\n", pid, uid);
13744 : : }
13745 : : }
13746 : :
13747 : : static void
13748 : 12 : handle_file_note (Elf *core, GElf_Word descsz, GElf_Off desc_pos)
13749 : : {
13750 : 12 : Elf_Data *data = elf_getdata_rawchunk (core, desc_pos, descsz, ELF_T_BYTE);
13751 [ - + ]: 12 : if (data == NULL)
13752 : 0 : error_exit (0, _("cannot convert core note data: %s"), elf_errmsg (-1));
13753 : :
13754 : 12 : unsigned char const *ptr = data->d_buf;
13755 : 12 : unsigned char const *const end = data->d_buf + data->d_size;
13756 : :
13757 : 12 : uint64_t count, page_size;
13758 [ - + ]: 12 : if (! buf_read_ulong (core, &ptr, end, &count)
13759 [ - + ]: 12 : || ! buf_read_ulong (core, &ptr, end, &page_size))
13760 : : {
13761 : 0 : fail:
13762 : 0 : printf (" Not enough data in NT_FILE note.\n");
13763 : 0 : return;
13764 : : }
13765 : :
13766 : 12 : size_t addrsize = gelf_fsize (core, ELF_T_ADDR, 1, EV_CURRENT);
13767 : 12 : uint64_t maxcount = (size_t) (end - ptr) / (3 * addrsize);
13768 [ - + ]: 12 : if (count > maxcount)
13769 : 0 : goto fail;
13770 : :
13771 : : /* Where file names are stored. */
13772 : 12 : unsigned char const *const fstart = ptr + 3 * count * addrsize;
13773 : 12 : char const *fptr = (char *) fstart;
13774 : :
13775 : 12 : printf (" %" PRId64 " files:\n", count);
13776 [ + + ]: 118 : for (uint64_t i = 0; i < count; ++i)
13777 : : {
13778 : 106 : uint64_t mstart, mend, moffset;
13779 [ + - ]: 106 : if (! buf_read_ulong (core, &ptr, fstart, &mstart)
13780 [ + - ]: 106 : || ! buf_read_ulong (core, &ptr, fstart, &mend)
13781 [ - + ]: 106 : || ! buf_read_ulong (core, &ptr, fstart, &moffset))
13782 : 0 : goto fail;
13783 : :
13784 : 106 : const char *fnext = memchr (fptr, '\0', (char *) end - fptr);
13785 [ - + ]: 106 : if (fnext == NULL)
13786 : 0 : goto fail;
13787 : :
13788 : 106 : int ct = printf (" %08" PRIx64 "-%08" PRIx64
13789 : : " %08" PRIx64 " %" PRId64,
13790 : : mstart, mend, moffset * page_size, mend - mstart);
13791 [ + - ]: 106 : printf ("%*s%s\n", ct > 50 ? 3 : 53 - ct, "", fptr);
13792 : :
13793 : 106 : fptr = fnext + 1;
13794 : : }
13795 : : }
13796 : :
13797 : : static void
13798 : 76 : handle_core_note (Ebl *ebl, const GElf_Ehdr *ehdr, const GElf_Nhdr *nhdr,
13799 : : const char *name, const void *desc)
13800 : : {
13801 : 76 : GElf_Word regs_offset;
13802 : 76 : size_t nregloc;
13803 : 76 : const Ebl_Register_Location *reglocs;
13804 : 76 : size_t nitems;
13805 : 76 : const Ebl_Core_Item *items;
13806 : :
13807 [ + + ]: 76 : if (! ebl_core_note (ebl, nhdr, name, desc,
13808 : : ®s_offset, &nregloc, ®locs, &nitems, &items))
13809 : 2 : return;
13810 : :
13811 : : /* Pass 0 for DESCSZ when there are registers in the note,
13812 : : so that the ITEMS array does not describe the whole thing.
13813 : : For non-register notes, the actual descsz might be a multiple
13814 : : of the unit size, not just exactly the unit size. */
13815 : 74 : unsigned int colno = handle_core_items (ebl->elf, ehdr, desc,
13816 [ + + ]: 74 : nregloc == 0 ? nhdr->n_descsz : 0,
13817 : : items, nitems);
13818 [ + + ]: 74 : if (colno != 0)
13819 : 68 : putchar ('\n');
13820 : :
13821 : 74 : colno = handle_core_registers (ebl, ebl->elf, desc + regs_offset,
13822 : : reglocs, nregloc);
13823 [ + + ]: 74 : if (colno != 0)
13824 : 36 : putchar ('\n');
13825 : : }
13826 : :
13827 : : static void
13828 : 506 : handle_notes_data (Ebl *ebl, const GElf_Ehdr *ehdr,
13829 : : GElf_Off start, Elf_Data *data)
13830 : : {
13831 : 506 : fputs (_(" Owner Data size Type\n"), stdout);
13832 : :
13833 [ - + ]: 506 : if (data == NULL)
13834 : 0 : goto bad_note;
13835 : :
13836 : : size_t offset = 0;
13837 : : GElf_Nhdr nhdr;
13838 : : size_t name_offset;
13839 : : size_t desc_offset;
13840 : 5120 : while (offset < data->d_size
13841 [ + + + - ]: 5120 : && (offset = gelf_getnote (data, offset,
13842 : : &nhdr, &name_offset, &desc_offset)) > 0)
13843 : : {
13844 [ + - ]: 4614 : const char *name = nhdr.n_namesz == 0 ? "" : data->d_buf + name_offset;
13845 : 4614 : const char *desc = data->d_buf + desc_offset;
13846 : :
13847 : : /* GNU Build Attributes are weird, they store most of their data
13848 : : into the owner name field. Extract just the owner name
13849 : : prefix here, then use the rest later as data. */
13850 : 4614 : bool is_gnu_build_attr
13851 : 4614 : = startswith (name, ELF_NOTE_GNU_BUILD_ATTRIBUTE_PREFIX);
13852 : 9798 : const char *print_name = (is_gnu_build_attr
13853 [ + + ]: 4614 : ? ELF_NOTE_GNU_BUILD_ATTRIBUTE_PREFIX : name);
13854 : 5184 : size_t print_namesz = (is_gnu_build_attr
13855 : 570 : ? strlen (print_name) : nhdr.n_namesz);
13856 : :
13857 : 4614 : char buf[100];
13858 : 4614 : char buf2[100];
13859 : 4614 : printf (_(" %-13.*s %9" PRId32 " %s\n"),
13860 : : (int) print_namesz, print_name, nhdr.n_descsz,
13861 [ + + ]: 4614 : ehdr->e_type == ET_CORE
13862 : 118 : ? ebl_core_note_type_name (ebl, nhdr.n_type,
13863 : : buf, sizeof (buf))
13864 : 4496 : : ebl_object_note_type_name (ebl, name, nhdr.n_type,
13865 : : nhdr.n_descsz,
13866 : : buf2, sizeof (buf2)));
13867 : :
13868 : : /* Filter out invalid entries. */
13869 : 4614 : if (memchr (name, '\0', nhdr.n_namesz) != NULL
13870 : : /* XXX For now help broken Linux kernels. */
13871 : : || 1)
13872 : : {
13873 [ + + ]: 4614 : if (ehdr->e_type == ET_CORE)
13874 : : {
13875 [ + + ]: 118 : if (nhdr.n_type == NT_AUXV
13876 [ + - ]: 18 : && (nhdr.n_namesz == 4 /* Broken old Linux kernels. */
13877 [ + - + - ]: 18 : || (nhdr.n_namesz == 5 && name[4] == '\0'))
13878 [ + - ]: 18 : && !memcmp (name, "CORE", 4))
13879 : 18 : handle_auxv_note (ebl, ebl->elf, nhdr.n_descsz,
13880 : : start + desc_offset);
13881 [ + + + - ]: 100 : else if (nhdr.n_namesz == 5 && strcmp (name, "CORE") == 0)
13882 [ + + + ]: 76 : switch (nhdr.n_type)
13883 : : {
13884 : 12 : case NT_SIGINFO:
13885 : 12 : handle_siginfo_note (ebl->elf, nhdr.n_descsz,
13886 : : start + desc_offset);
13887 : 12 : break;
13888 : :
13889 : 12 : case NT_FILE:
13890 : 12 : handle_file_note (ebl->elf, nhdr.n_descsz,
13891 : : start + desc_offset);
13892 : 12 : break;
13893 : :
13894 : 52 : default:
13895 : 52 : handle_core_note (ebl, ehdr, &nhdr, name, desc);
13896 : : }
13897 : : else
13898 : 24 : handle_core_note (ebl, ehdr, &nhdr, name, desc);
13899 : : }
13900 : : else
13901 : 4496 : ebl_object_note (ebl, nhdr.n_namesz, name, nhdr.n_type,
13902 : : nhdr.n_descsz, desc);
13903 : : }
13904 : : }
13905 : :
13906 [ + - ]: 506 : if (offset == data->d_size)
13907 : 506 : return;
13908 : :
13909 : 0 : bad_note:
13910 : 0 : error (0, 0,
13911 : 0 : _("cannot get content of note: %s"),
13912 : 0 : data != NULL ? "garbage data" : elf_errmsg (-1));
13913 : : }
13914 : :
13915 : : static void
13916 : 230 : handle_notes (Ebl *ebl, GElf_Ehdr *ehdr)
13917 : : {
13918 : : /* If we have section headers, just look for SHT_NOTE sections.
13919 : : In a debuginfo file, the program headers are not reliable. */
13920 [ + + ]: 230 : if (shnum != 0)
13921 : : {
13922 : : /* Get the section header string table index. */
13923 : 208 : size_t shstrndx;
13924 [ - + ]: 208 : if (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0)
13925 : 0 : error_exit (0, _("cannot get section header string table index"));
13926 : :
13927 : : Elf_Scn *scn = NULL;
13928 [ + + ]: 6448 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
13929 : : {
13930 : 6240 : GElf_Shdr shdr_mem;
13931 : 6240 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
13932 : :
13933 [ + - + + ]: 6240 : if (shdr == NULL || shdr->sh_type != SHT_NOTE)
13934 : : /* Not what we are looking for. */
13935 : 5758 : continue;
13936 : :
13937 [ - + ]: 482 : if (notes_section != NULL)
13938 : : {
13939 : 0 : char *sname = elf_strptr (ebl->elf, shstrndx, shdr->sh_name);
13940 [ # # # # ]: 0 : if (sname == NULL || strcmp (sname, notes_section) != 0)
13941 : 0 : continue;
13942 : : }
13943 : :
13944 : 482 : printf (_("\
13945 : : \nNote section [%2zu] '%s' of %" PRIu64 " bytes at offset %#0" PRIx64 ":\n"),
13946 : : elf_ndxscn (scn),
13947 : 482 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
13948 : : shdr->sh_size, shdr->sh_offset);
13949 : :
13950 : 482 : handle_notes_data (ebl, ehdr, shdr->sh_offset,
13951 : : elf_getdata (scn, NULL));
13952 : : }
13953 : 208 : return;
13954 : : }
13955 : :
13956 : : /* We have to look through the program header to find the note
13957 : : sections. There can be more than one. */
13958 [ + + ]: 280 : for (size_t cnt = 0; cnt < phnum; ++cnt)
13959 : : {
13960 : 258 : GElf_Phdr mem;
13961 : 258 : GElf_Phdr *phdr = gelf_getphdr (ebl->elf, cnt, &mem);
13962 : :
13963 [ + - + + ]: 258 : if (phdr == NULL || phdr->p_type != PT_NOTE)
13964 : : /* Not what we are looking for. */
13965 : 234 : continue;
13966 : :
13967 : 24 : printf (_("\
13968 : : \nNote segment of %" PRIu64 " bytes at offset %#0" PRIx64 ":\n"),
13969 : : phdr->p_filesz, phdr->p_offset);
13970 : :
13971 : 24 : handle_notes_data (ebl, ehdr, phdr->p_offset,
13972 : : elf_getdata_rawchunk (ebl->elf,
13973 : 24 : phdr->p_offset, phdr->p_filesz,
13974 [ + + ]: 24 : (phdr->p_align == 8
13975 : : ? ELF_T_NHDR8 : ELF_T_NHDR)));
13976 : : }
13977 : : }
13978 : :
13979 : :
13980 : : static void
13981 : 12 : hex_dump (const uint8_t *data, size_t len)
13982 : : {
13983 : 12 : size_t pos = 0;
13984 [ + + ]: 56 : while (pos < len)
13985 : : {
13986 : 44 : printf (" 0x%08zx ", pos);
13987 : :
13988 : 44 : const size_t chunk = MIN (len - pos, 16);
13989 : :
13990 [ + + ]: 688 : for (size_t i = 0; i < chunk; ++i)
13991 [ + + ]: 644 : if (i % 4 == 3)
13992 : 160 : printf ("%02x ", data[pos + i]);
13993 : : else
13994 : 484 : printf ("%02x", data[pos + i]);
13995 : :
13996 [ + + ]: 44 : if (chunk < 16)
13997 : 4 : printf ("%*s", (int) ((16 - chunk) * 2 + (16 - chunk + 3) / 4), "");
13998 : :
13999 [ + + ]: 688 : for (size_t i = 0; i < chunk; ++i)
14000 : : {
14001 : 644 : unsigned char b = data[pos + i];
14002 [ + + ]: 1288 : printf ("%c", isprint (b) ? b : '.');
14003 : : }
14004 : :
14005 : 44 : putchar ('\n');
14006 : 44 : pos += chunk;
14007 : : }
14008 : 12 : }
14009 : :
14010 : : static void
14011 : 12 : dump_data_section (Elf_Scn *scn, const GElf_Shdr *shdr, const char *name)
14012 : : {
14013 [ + - - + ]: 12 : if (shdr->sh_size == 0 || shdr->sh_type == SHT_NOBITS)
14014 : 0 : printf (_("\nSection [%zu] '%s' has no data to dump.\n"),
14015 : : elf_ndxscn (scn), name);
14016 : : else
14017 : : {
14018 [ + + ]: 12 : if (print_decompress)
14019 : : {
14020 : : /* We try to decompress the section, but keep the old shdr around
14021 : : so we can show both the original shdr size and the uncompressed
14022 : : data size. */
14023 [ + + ]: 8 : if ((shdr->sh_flags & SHF_COMPRESSED) != 0)
14024 : : {
14025 [ - + ]: 4 : if (elf_compress (scn, 0, 0) < 0)
14026 : 0 : printf ("WARNING: %s [%zd]\n",
14027 : : _("Couldn't uncompress section"),
14028 : : elf_ndxscn (scn));
14029 : : }
14030 [ + - + - ]: 4 : else if (name && startswith (name, ".zdebug"))
14031 : : {
14032 [ - + ]: 4 : if (elf_compress_gnu (scn, 0, 0) < 0)
14033 : 0 : printf ("WARNING: %s [%zd]\n",
14034 : : _("Couldn't uncompress section"),
14035 : : elf_ndxscn (scn));
14036 : : }
14037 : : }
14038 : :
14039 : 12 : Elf_Data *data = elf_rawdata (scn, NULL);
14040 [ - + ]: 12 : if (data == NULL)
14041 : 0 : error (0, 0, _("cannot get data for section [%zu] '%s': %s"),
14042 : : elf_ndxscn (scn), name, elf_errmsg (-1));
14043 : : else
14044 : : {
14045 [ + + ]: 12 : if (data->d_size == shdr->sh_size)
14046 : 4 : printf (_("\nHex dump of section [%zu] '%s', %" PRIu64
14047 : : " bytes at offset %#0" PRIx64 ":\n"),
14048 : : elf_ndxscn (scn), name,
14049 : 4 : shdr->sh_size, shdr->sh_offset);
14050 : : else
14051 : 8 : printf (_("\nHex dump of section [%zu] '%s', %" PRIu64
14052 : : " bytes (%zd uncompressed) at offset %#0"
14053 : : PRIx64 ":\n"),
14054 : : elf_ndxscn (scn), name,
14055 : 8 : shdr->sh_size, data->d_size, shdr->sh_offset);
14056 : 12 : hex_dump (data->d_buf, data->d_size);
14057 : : }
14058 : : }
14059 : 12 : }
14060 : :
14061 : : static void
14062 : 374 : print_string_section (Elf_Scn *scn, const GElf_Shdr *shdr, const char *name)
14063 : : {
14064 [ + - + + ]: 374 : if (shdr->sh_size == 0 || shdr->sh_type == SHT_NOBITS)
14065 : 42 : printf (_("\nSection [%zu] '%s' has no strings to dump.\n"),
14066 : : elf_ndxscn (scn), name);
14067 : : else
14068 : : {
14069 [ + + ]: 332 : if (print_decompress)
14070 : : {
14071 : : /* We try to decompress the section, but keep the old shdr around
14072 : : so we can show both the original shdr size and the uncompressed
14073 : : data size. */
14074 [ - + ]: 2 : if ((shdr->sh_flags & SHF_COMPRESSED) != 0)
14075 : : {
14076 [ # # ]: 0 : if (elf_compress (scn, 0, 0) < 0)
14077 : 0 : printf ("WARNING: %s [%zd]\n",
14078 : : _("Couldn't uncompress section"),
14079 : : elf_ndxscn (scn));
14080 : : }
14081 [ + - + - ]: 2 : else if (name && startswith (name, ".zdebug"))
14082 : : {
14083 [ - + ]: 2 : if (elf_compress_gnu (scn, 0, 0) < 0)
14084 : 0 : printf ("WARNING: %s [%zd]\n",
14085 : : _("Couldn't uncompress section"),
14086 : : elf_ndxscn (scn));
14087 : : }
14088 : : }
14089 : :
14090 : 332 : Elf_Data *data = elf_rawdata (scn, NULL);
14091 [ - + ]: 332 : if (data == NULL)
14092 : 0 : error (0, 0, _("cannot get data for section [%zu] '%s': %s"),
14093 : : elf_ndxscn (scn), name, elf_errmsg (-1));
14094 : : else
14095 : : {
14096 [ + + ]: 332 : if (data->d_size == shdr->sh_size)
14097 : 330 : printf (_("\nString section [%zu] '%s' contains %" PRIu64
14098 : : " bytes at offset %#0" PRIx64 ":\n"),
14099 : : elf_ndxscn (scn), name,
14100 : 330 : shdr->sh_size, shdr->sh_offset);
14101 : : else
14102 : 2 : printf (_("\nString section [%zu] '%s' contains %" PRIu64
14103 : : " bytes (%zd uncompressed) at offset %#0"
14104 : : PRIx64 ":\n"),
14105 : : elf_ndxscn (scn), name,
14106 : 2 : shdr->sh_size, data->d_size, shdr->sh_offset);
14107 : :
14108 : 332 : const char *start = data->d_buf;
14109 : 332 : const char *const limit = start + data->d_size;
14110 : 51440 : do
14111 : : {
14112 : 51440 : const char *end = memchr (start, '\0', limit - start);
14113 : 51440 : const size_t pos = start - (const char *) data->d_buf;
14114 [ - + ]: 51440 : if (unlikely (end == NULL))
14115 : : {
14116 : 0 : printf (" [%6zx]- %.*s\n",
14117 : : pos, (int) (limit - start), start);
14118 : 0 : break;
14119 : : }
14120 : 51440 : printf (" [%6zx] %s\n", pos, start);
14121 : 51440 : start = end + 1;
14122 [ + + ]: 51440 : } while (start < limit);
14123 : : }
14124 : : }
14125 : 374 : }
14126 : :
14127 : : static void
14128 : 198 : for_each_section_argument (Elf *elf, const struct section_argument *list,
14129 : : void (*dump) (Elf_Scn *scn, const GElf_Shdr *shdr,
14130 : : const char *name))
14131 : : {
14132 : : /* Get the section header string table index. */
14133 : 198 : size_t shstrndx;
14134 [ - + ]: 198 : if (elf_getshdrstrndx (elf, &shstrndx) < 0)
14135 : 0 : error_exit (0, _("cannot get section header string table index"));
14136 : :
14137 [ + + ]: 764 : for (const struct section_argument *a = list; a != NULL; a = a->next)
14138 : : {
14139 : 566 : Elf_Scn *scn;
14140 : 566 : GElf_Shdr shdr_mem;
14141 : 566 : const char *name = NULL;
14142 : :
14143 : 566 : char *endp = NULL;
14144 : 566 : unsigned long int shndx = strtoul (a->arg, &endp, 0);
14145 [ + + - + ]: 566 : if (endp != a->arg && *endp == '\0')
14146 : : {
14147 : 2 : scn = elf_getscn (elf, shndx);
14148 [ - + ]: 2 : if (scn == NULL)
14149 : : {
14150 : 0 : error (0, 0, _("\nsection [%lu] does not exist"), shndx);
14151 : 0 : continue;
14152 : : }
14153 : :
14154 [ - + ]: 2 : if (gelf_getshdr (scn, &shdr_mem) == NULL)
14155 : 0 : error_exit (0, _("cannot get section header: %s"),
14156 : : elf_errmsg (-1));
14157 : 2 : name = elf_strptr (elf, shstrndx, shdr_mem.sh_name);
14158 : 2 : (*dump) (scn, &shdr_mem, name);
14159 : : }
14160 : : else
14161 : : {
14162 : : /* Need to look up the section by name. */
14163 : : scn = NULL;
14164 : : bool found = false;
14165 [ + + ]: 18186 : while ((scn = elf_nextscn (elf, scn)) != NULL)
14166 : : {
14167 [ - + ]: 17622 : if (gelf_getshdr (scn, &shdr_mem) == NULL)
14168 : 0 : continue;
14169 : 17622 : name = elf_strptr (elf, shstrndx, shdr_mem.sh_name);
14170 [ - + ]: 17622 : if (name == NULL)
14171 : 0 : continue;
14172 [ + + ]: 17622 : if (!strcmp (name, a->arg))
14173 : : {
14174 : 384 : found = true;
14175 : 384 : (*dump) (scn, &shdr_mem, name);
14176 : : }
14177 : : }
14178 : :
14179 [ + + - + ]: 564 : if (unlikely (!found) && !a->implicit)
14180 : 0 : error (0, 0, _("\nsection '%s' does not exist"), a->arg);
14181 : : }
14182 : : }
14183 : 198 : }
14184 : :
14185 : : static void
14186 : 12 : dump_data (Ebl *ebl)
14187 : : {
14188 : 12 : for_each_section_argument (ebl->elf, dump_data_sections, &dump_data_section);
14189 : 12 : }
14190 : :
14191 : : static void
14192 : 186 : dump_strings (Ebl *ebl)
14193 : : {
14194 : 186 : for_each_section_argument (ebl->elf, string_sections, &print_string_section);
14195 : 186 : }
14196 : :
14197 : : static void
14198 : 0 : print_strings (Ebl *ebl)
14199 : : {
14200 : : /* Get the section header string table index. */
14201 : 0 : size_t shstrndx;
14202 [ # # ]: 0 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
14203 : 0 : error_exit (0, _("cannot get section header string table index"));
14204 : :
14205 : : Elf_Scn *scn;
14206 : : GElf_Shdr shdr_mem;
14207 : : const char *name;
14208 : : scn = NULL;
14209 [ # # ]: 0 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
14210 : : {
14211 [ # # ]: 0 : if (gelf_getshdr (scn, &shdr_mem) == NULL)
14212 : 0 : continue;
14213 : :
14214 [ # # ]: 0 : if (shdr_mem.sh_type != SHT_PROGBITS
14215 [ # # ]: 0 : || !(shdr_mem.sh_flags & SHF_STRINGS))
14216 : 0 : continue;
14217 : :
14218 : 0 : name = elf_strptr (ebl->elf, shstrndx, shdr_mem.sh_name);
14219 [ # # ]: 0 : if (name == NULL)
14220 : 0 : continue;
14221 : :
14222 : 0 : print_string_section (scn, &shdr_mem, name);
14223 : : }
14224 : 0 : }
14225 : :
14226 : : static void
14227 : 4 : dump_archive_index (Elf *elf, const char *fname)
14228 : : {
14229 : 4 : size_t narsym;
14230 : 4 : const Elf_Arsym *arsym = elf_getarsym (elf, &narsym);
14231 [ - + ]: 4 : if (arsym == NULL)
14232 : : {
14233 : 0 : int result = elf_errno ();
14234 [ # # ]: 0 : if (unlikely (result != ELF_E_NO_INDEX))
14235 : 0 : error_exit (0, _("cannot get symbol index of archive '%s': %s"),
14236 : : fname, elf_errmsg (result));
14237 : : else
14238 : 0 : printf (_("\nArchive '%s' has no symbol index\n"), fname);
14239 : 0 : return;
14240 : : }
14241 : :
14242 : 4 : printf (_("\nIndex of archive '%s' has %zu entries:\n"),
14243 : : fname, narsym);
14244 : :
14245 : 4 : size_t as_off = 0;
14246 [ + + ]: 22 : for (const Elf_Arsym *s = arsym; s < &arsym[narsym - 1]; ++s)
14247 : : {
14248 [ + + ]: 18 : if (s->as_off != as_off)
14249 : : {
14250 : 12 : as_off = s->as_off;
14251 : :
14252 : 12 : Elf *subelf = NULL;
14253 [ + - ]: 12 : if (unlikely (elf_rand (elf, as_off) == 0)
14254 [ - + ]: 12 : || unlikely ((subelf = elf_begin (-1, ELF_C_READ_MMAP, elf))
14255 : : == NULL))
14256 : : #if __GLIBC__ < 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ < 7)
14257 : : while (1)
14258 : : #endif
14259 : 0 : error_exit (0,
14260 : : _("cannot extract member at offset %zu in '%s': %s"),
14261 : : as_off, fname, elf_errmsg (-1));
14262 : :
14263 : 12 : const Elf_Arhdr *h = elf_getarhdr (subelf);
14264 [ + - ]: 12 : if (h != NULL)
14265 : 12 : printf (_("Archive member '%s' contains:\n"), h->ar_name);
14266 : :
14267 : 12 : elf_end (subelf);
14268 : : }
14269 : :
14270 : 18 : printf ("\t%s\n", s->as_name);
14271 : : }
14272 : : }
14273 : :
14274 : : #include "debugpred.h"
|