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 : 900 : init_gettext_strs (void)
455 : : {
456 : 900 : yes_str = _("yes");
457 : 900 : no_str = _("no");
458 : 900 : empty_block_str = _("empty block");
459 : 900 : byte_block_str = _("byte block");
460 : 900 : offset_str = _("offset");
461 : 900 : children_str = _("children");
462 : 900 : tag_str = _("tag");
463 : 900 : table_offset_lower_str = _("Table at offset");
464 : 900 : table_offset_upper_str = _("Table at Offset");
465 : 900 : length_str = _("Length");
466 : 900 : dwarf_ver_str = _("DWARF version");
467 : 900 : cu_offset_str = _("CU offset");
468 : 900 : addr_size_str = _("Address size");
469 : 900 : seg_size_str = _("Segment size");
470 : 900 : offset_ent_str = _("Offset entries");
471 : 900 : cu_str = _("CU");
472 : 900 : base_str = _("base");
473 : 900 : type_unit_str = _("Type unit at offset");
474 : 900 : version_str = _("Version");
475 : 900 : abbrev_offset_str = _("Abbreviation section offset");
476 : 900 : offset_size_str = _("Offset size");
477 : 900 : type_sig_str = _("Type signature");
478 : 900 : type_offset_str = _("Type offset");
479 : 900 : cu_offset_at_str = _("Compilation unit at offset");
480 : 900 : unit_type_str = _("Unit type");
481 : 900 : unit_id_str = _("Unit id");
482 : 900 : unit_die_off_str = _("Unit DIE off");
483 : 900 : prologue_len_str = _("Prologue length");
484 : 900 : seg_selector_str = _("Segment selector size");
485 : 900 : min_inst_len_str = _("Min instruction length");
486 : 900 : max_op_per_inst_str = _("Max operations per instruction");
487 : 900 : initial_value_if_str = _("Initial value if");
488 : 900 : line_base_str = _("Line base");
489 : 900 : line_range_str = _("Line range");
490 : 900 : opcodes_base_str = _("Opcode base");
491 : 900 : opcodes_str = _("Opcodes");
492 : 900 : special_opcode_str = _("special opcode");
493 : 900 : address_str = _("address");
494 : 900 : line_str = _("line");
495 : 900 : ext_opcode_str = _("extended opcode");
496 : 900 : set_disc_str = _("set discriminator to");
497 : 900 : adv_addr_str = _("advance address by");
498 : 900 : to_str = _("to");
499 : 900 : set_file_str = _("set file to");
500 : 900 : set_col_str = _("set column to");
501 : 900 : set_str = _("set");
502 : 900 : adv_addr_by_str = _("advance address by constant");
503 : 900 : adv_line_by_str = _("advance line by constant");
504 : 900 : unknown_base_str = _("Unknown CU base");
505 : 900 : dir_table_str = _("Directory table");
506 : 900 : file_table_str = _("File name table");
507 : 900 : no_line_num_stmts_str = _("No line number statements");
508 : 900 : line_num_stmts_str = _("Line number statements");
509 : 900 : end_of_seq_str = _("end of sequence");
510 : 900 : set_addr_to_str = _("set address to");
511 : 900 : copy_str = _("copy");
512 : 900 : }
513 : :
514 : :
515 : : static void
516 : 1800 : cleanup_list (struct section_argument *list)
517 : : {
518 [ + + + + ]: 2366 : 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 : 900 : main (int argc, char *argv[])
565 : : {
566 : : /* We use no threads here which can interfere with handling a stream. */
567 : 900 : (void) __fsetlocking (stdout, FSETLOCKING_BYCALLER);
568 : :
569 : : /* Set locale. */
570 : 900 : setlocale (LC_ALL, "");
571 : :
572 : : /* Initialize the message catalog. */
573 : 900 : textdomain (PACKAGE_TARNAME);
574 : :
575 : : /* Look up once. */
576 : 900 : init_gettext_strs ();
577 : :
578 : : /* Parse and process arguments. */
579 : 900 : int remaining;
580 : 900 : argp_parse (&argp, argc, argv, 0, &remaining, NULL);
581 : :
582 : : /* Before we start tell the ELF library which version we are using. */
583 : 900 : 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 : 900 : bool only_one = remaining + 1 == argc;
593 : 982 : do
594 : : {
595 : : /* Open the file. */
596 : 982 : int fd = open (argv[remaining], O_RDONLY);
597 [ - + ]: 982 : if (fd == -1)
598 : : {
599 : 0 : error (0, errno, _("cannot open input file '%s'"), argv[remaining]);
600 : 0 : continue;
601 : : }
602 : :
603 : 982 : process_file (fd, argv[remaining], only_one);
604 : :
605 : 982 : close (fd);
606 : : }
607 [ + + ]: 982 : while (++remaining < argc);
608 : :
609 : 900 : cleanup_list (dump_data_sections);
610 : 900 : cleanup_list (string_sections);
611 : :
612 : 900 : 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 : 5722 : parse_opt (int key, char *arg,
633 : : struct argp_state *state __attribute__ ((unused)))
634 : : {
635 [ + + - + : 5722 : 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 : 48 : case 'n':
687 : 48 : print_notes = true;
688 : 48 : any_control_option = true;
689 : 48 : notes_section = arg;
690 : 48 : 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 : 272 : case 'w':
729 [ + + ]: 272 : 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 [ - + ]: 186 : else if (strcmp (arg, "abbrev") == 0)
736 : 0 : print_debug_sections |= section_abbrev;
737 [ + + ]: 186 : else if (strcmp (arg, "addr") == 0)
738 : : {
739 : 4 : print_debug_sections |= section_addr;
740 : 4 : implicit_debug_sections |= section_info;
741 : : }
742 [ + + ]: 182 : else if (strcmp (arg, "aranges") == 0)
743 : 6 : print_debug_sections |= section_aranges;
744 [ + + ]: 176 : else if (strcmp (arg, "decodedaranges") == 0)
745 : : {
746 : 2 : print_debug_sections |= section_aranges;
747 : 2 : decodedaranges = true;
748 : : }
749 [ + + ]: 174 : else if (strcmp (arg, "ranges") == 0)
750 : : {
751 : 26 : print_debug_sections |= section_ranges;
752 : 26 : implicit_debug_sections |= section_info;
753 : : }
754 [ + + + + ]: 148 : else if (strcmp (arg, "frame") == 0 || strcmp (arg, "frames") == 0)
755 : 6 : print_debug_sections |= section_frame;
756 [ + + ]: 142 : else if (strcmp (arg, "info") == 0)
757 : : {
758 : 42 : print_debug_sections |= section_info;
759 : 42 : print_debug_sections |= section_types;
760 : : }
761 [ + + ]: 100 : else if (strcmp (arg, "info+") == 0)
762 : : {
763 : 6 : print_debug_sections |= section_info;
764 : 6 : print_debug_sections |= section_types;
765 : 6 : 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 : 272 : any_control_option = true;
806 : 272 : 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 : 900 : case ARGP_KEY_FINI:
829 [ + + + - ]: 900 : 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 : 878 : 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 [ + - ]: 878 : if (*(bool *) arg)
1012 : : return DWARF_CB_ABORT;
1013 : 878 : *(bool *) arg = true;
1014 : 878 : 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 : 978 : 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 : 978 : const struct process_dwflmod_args *a = arg;
1031 : :
1032 : : /* Print the file name. */
1033 [ + + ]: 978 : 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 : 978 : process_elf_file (dwflmod, a->fd);
1042 : :
1043 : 978 : 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 : 996 : create_dwfl (int fd, const char *fname)
1076 : : {
1077 : : /* Duplicate an fd for dwfl_report_offline to swallow. */
1078 : 996 : int dwfl_fd = dup (fd);
1079 [ - + ]: 996 : 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 : 996 : static const Dwfl_Callbacks callbacks =
1085 : : {
1086 : : .section_address = dwfl_offline_section_address,
1087 : : .find_debuginfo = find_no_debuginfo
1088 : : };
1089 : 996 : Dwfl *dwfl = dwfl_begin (&callbacks);
1090 [ + - ]: 996 : if (likely (dwfl != NULL))
1091 : : /* Let 0 be the logical address of the file (or first in archive). */
1092 : 996 : dwfl->offline_next_address = 0;
1093 [ - + ]: 996 : 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 : 996 : dwfl_report_end (dwfl, NULL, NULL);
1109 : :
1110 : 996 : return dwfl;
1111 : : }
1112 : :
1113 : : /* Process one input file. */
1114 : : static void
1115 : 982 : process_file (int fd, const char *fname, bool only_one)
1116 : : {
1117 [ + + ]: 982 : if (print_archive_index)
1118 : 4 : check_archive_index (fd, fname, only_one);
1119 : :
1120 [ + + ]: 982 : if (!any_control_option)
1121 : : return;
1122 : :
1123 [ + + ]: 978 : 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 : 978 : Dwfl *dwfl = create_dwfl (fd, fname);
1139 [ + - ]: 978 : if (dwfl != NULL)
1140 : : {
1141 [ + + ]: 978 : if (only_one)
1142 : : {
1143 : : /* Clear ONLY_ONE if we have multiple modules, from an archive. */
1144 : 878 : bool seen = false;
1145 : 878 : only_one = dwfl_getmodules (dwfl, &count_dwflmod, &seen, 0) == 0;
1146 : : }
1147 : :
1148 : : /* Process the one or more modules gleaned from this file. */
1149 : 978 : struct process_dwflmod_args a = { .fd = fd, .only_one = only_one };
1150 : 978 : 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 [ + - ]: 978 : if (! do_not_close_dwfl)
1155 : 978 : dwfl_end (dwfl);
1156 : :
1157 : : /* Need to close the replaced fd if we created it. Caller takes
1158 : : care of original. */
1159 [ + + ]: 978 : 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 : 494 : elf_contains_chdrs (Elf *elf)
1166 : : {
1167 : 494 : Elf_Scn *scn = NULL;
1168 [ + + ]: 797910 : 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 : 978 : process_elf_file (Dwfl_Module *dwflmod, int fd)
1181 : : {
1182 : 978 : GElf_Addr dwflbias;
1183 : 978 : Elf *elf = dwfl_module_getelf (dwflmod, &dwflbias);
1184 : :
1185 : 978 : GElf_Ehdr ehdr_mem;
1186 : 978 : GElf_Ehdr *ehdr = gelf_getehdr (elf, &ehdr_mem);
1187 : :
1188 [ - + ]: 978 : if (ehdr == NULL)
1189 : : {
1190 : 0 : error (0, 0, _("cannot read ELF header: %s"), elf_errmsg (-1));
1191 : 0 : return;
1192 : : }
1193 : :
1194 : 978 : Ebl *ebl = ebl_openbackend (elf);
1195 [ - + ]: 978 : 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 [ - + ]: 978 : 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 [ - + ]: 978 : 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 : 2152 : bool print_unchanged = ((print_section_header
1218 [ + + ]: 472 : || print_relocations
1219 [ + + ]: 462 : || dump_data_sections != NULL
1220 [ + + ]: 450 : || print_notes)
1221 [ + + + + ]: 1048 : && (ehdr->e_type == ET_REL
1222 [ + + ]: 494 : || 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 : 978 : bool symtab_printed = false;
1250 : :
1251 [ + + ]: 978 : if (print_file_header)
1252 : 190 : print_ehdr (ebl, ehdr);
1253 [ + + ]: 978 : if (print_section_header)
1254 : 506 : print_shdr (pure_ebl, ehdr);
1255 [ + + ]: 978 : if (print_program_header)
1256 : 188 : print_phdr (ebl, ehdr);
1257 [ + + ]: 978 : if (print_section_groups)
1258 : 202 : print_scngrp (ebl);
1259 [ + + ]: 978 : if (print_dynamic_table)
1260 : 190 : print_dynamic (ebl);
1261 [ + + ]: 978 : if (print_relocations)
1262 : 194 : print_relocs (pure_ebl, dwflmod, ehdr);
1263 [ + + ]: 978 : if (print_histogram)
1264 : 184 : handle_hash (ebl);
1265 [ + + + + ]: 978 : if (print_symbol_table || print_dynsym_table)
1266 : 220 : symtab_printed |= print_symtab (ebl, SHT_DYNSYM);
1267 [ + + ]: 978 : if (print_version_info)
1268 : 184 : print_verinfo (ebl);
1269 [ + + + - ]: 978 : if (print_symbol_table && !use_dynamic_segment)
1270 : 218 : symtab_printed |= print_symtab (ebl, SHT_SYMTAB);
1271 : :
1272 [ + + + + ]: 978 : 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 [ + + ]: 978 : if (print_arch)
1278 : 192 : print_liblist (ebl);
1279 [ + + ]: 978 : if (print_arch)
1280 : 192 : print_attributes (ebl, ehdr);
1281 [ + + ]: 978 : if (dump_data_sections != NULL)
1282 : 12 : dump_data (pure_ebl);
1283 [ + + ]: 978 : if (string_sections != NULL)
1284 : 186 : dump_strings (ebl);
1285 [ + + ]: 978 : if ((print_debug_sections | implicit_debug_sections) != 0)
1286 : 510 : print_debug (dwflmod, ebl, ehdr);
1287 [ + + ]: 978 : if (print_notes)
1288 : 232 : handle_notes (pure_ebl, ehdr);
1289 [ - + ]: 978 : if (print_string_sections)
1290 : 0 : print_strings (ebl);
1291 : :
1292 [ + + ]: 978 : 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 : 782 : 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 : 89596 : get_visibility_type (int value)
1447 : : {
1448 [ - + - - : 89596 : 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 : 1264 : section_name (Ebl *ebl, GElf_Shdr *shdr)
1841 : : {
1842 : 1264 : size_t shstrndx;
1843 [ + - - + ]: 1264 : if (shdr == NULL || elf_getshdrstrndx (ebl->elf, &shstrndx) < 0)
1844 : 0 : return "???";
1845 [ - + ]: 1264 : 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 [ + + ]: 1182856 : for (int cnt = 0; cnt < nentries; ++cnt)
2568 : : {
2569 : 1182574 : GElf_Rela relmem;
2570 : 1182574 : GElf_Rela *rel = gelf_getrela (data, cnt, &relmem);
2571 [ + - ]: 1182574 : if (likely (rel != NULL))
2572 : : {
2573 : 1182574 : char buf[64];
2574 : 1182574 : GElf_Sym symmem;
2575 : 1182574 : Elf32_Word xndx;
2576 : 2365148 : GElf_Sym *sym = gelf_getsymshndx (symdata, xndxdata,
2577 : 1182574 : GELF_R_SYM (rel->r_info),
2578 : : &symmem, &xndx);
2579 : :
2580 [ - + ]: 1182574 : 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 [ + + ]: 1182574 : else if (GELF_ST_TYPE (sym->st_info) != STT_SECTION
2633 [ + + ]: 644686 : && !(GELF_ST_TYPE (sym->st_info) == STT_NOTYPE
2634 [ + + ]: 117842 : && GELF_ST_BIND (sym->st_info) == STB_LOCAL
2635 [ + + ]: 116304 : && sym->st_shndx != SHN_UNDEF
2636 [ + + ]: 716 : && sym->st_value == 0)) // local start section label
2637 [ + - ]: 1289348 : printf ("\
2638 : : %#0*" PRIx64 " %-15s %#0*" PRIx64 " %+6" PRId64 " %s\n",
2639 : : class == ELFCLASS32 ? 10 : 18, rel->r_offset,
2640 [ + - ]: 644674 : 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 : 644674 : ? 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 : 644674 : 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 : 537900 : GElf_Shdr secshdr_mem;
2656 : 537900 : GElf_Shdr *secshdr;
2657 : 537900 : secshdr = gelf_getshdr (elf_getscn (ebl->elf,
2658 [ + + ]: 537900 : sym->st_shndx == SHN_XINDEX
2659 : 2088 : ? xndx : sym->st_shndx),
2660 : : &secshdr_mem);
2661 : :
2662 [ - + ]: 537900 : 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 [ + - + - ]: 1613700 : printf ("\
2676 : : %#0*" PRIx64 " %-15s %#0*" PRIx64 " %+6" PRId64 " %s\n",
2677 : : class == ELFCLASS32 ? 10 : 18, rel->r_offset,
2678 : 537900 : ebl_reloc_type_check (ebl, GELF_R_TYPE (rel->r_info))
2679 : : /* Avoid the leading R_ which isn't carrying any
2680 : : information. */
2681 : 537900 : ? 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 : 537900 : 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 [ + + ]: 89862 : for (unsigned int cnt = 0; cnt < nsyms; ++cnt)
2864 : : {
2865 : 89596 : char typebuf[64];
2866 : 89596 : char bindbuf[64];
2867 : 89596 : char scnbuf[64];
2868 : 89596 : const char *sym_name;
2869 : 89596 : Elf32_Word xndx;
2870 : 89596 : GElf_Sym sym_mem;
2871 : 89596 : GElf_Sym *sym
2872 : 89596 : = gelf_getsymshndx (symdata, xndx_data, cnt, &sym_mem, &xndx);
2873 : :
2874 [ - + ]: 89596 : if (unlikely (sym == NULL))
2875 : 0 : continue;
2876 : :
2877 : : /* Determine the real section index. */
2878 [ + - ]: 89596 : if (likely (sym->st_shndx != SHN_XINDEX))
2879 : 89596 : xndx = sym->st_shndx;
2880 [ - + ]: 89596 : 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 : 89596 : sym_name = elf_strptr (ebl->elf, idx, sym->st_name);
2891 : :
2892 [ - + ]: 89596 : if (sym_name == NULL)
2893 : : sym_name = "???";
2894 : :
2895 [ + + ]: 178304 : printf (_ ("\
2896 : : %5u: %0*" PRIx64 " %6" PRId64 " %-7s %-6s %-9s %6s %s"),
2897 : 89596 : cnt, gelf_getclass (ebl->elf) == ELFCLASS32 ? 8 : 16,
2898 : : sym->st_value, sym->st_size,
2899 : 89596 : ebl_symbol_type_name (ebl, GELF_ST_TYPE (sym->st_info), typebuf,
2900 : : sizeof (typebuf)),
2901 : 89596 : ebl_symbol_binding_name (ebl, GELF_ST_BIND (sym->st_info),
2902 : : bindbuf, sizeof (bindbuf)),
2903 : 89596 : get_visibility_type (GELF_ST_VISIBILITY (sym->st_other)),
2904 : 89596 : ebl_section_name (ebl, sym->st_shndx, xndx, scnbuf,
2905 : : sizeof (scnbuf), NULL, shnum),
2906 : : sym_name);
2907 : :
2908 [ + + ]: 89596 : if (versym_data != NULL)
2909 : : {
2910 : : /* Get the version information. */
2911 : 3524 : GElf_Versym versym_mem;
2912 : 3524 : GElf_Versym *versym = gelf_getversym (versym_data, cnt, &versym_mem);
2913 : :
2914 [ + - + + ]: 3524 : if (versym != NULL && ((*versym & 0x8000) != 0 || *versym > 1))
2915 : : {
2916 : 2564 : bool is_nobits = false;
2917 : 2564 : bool check_def = xndx != SHN_UNDEF;
2918 : :
2919 [ + + - + ]: 2564 : if (xndx < SHN_LORESERVE || sym->st_shndx == SHN_XINDEX)
2920 : : {
2921 : 2544 : GElf_Shdr symshdr_mem;
2922 : 2544 : GElf_Shdr *symshdr = gelf_getshdr (
2923 : : elf_getscn (ebl->elf, xndx), &symshdr_mem);
2924 : :
2925 : 2544 : is_nobits
2926 [ + - + + ]: 5064 : = (symshdr != NULL && symshdr->sh_type == SHT_NOBITS);
2927 : : }
2928 : :
2929 [ + + ]: 2564 : if (is_nobits || !check_def)
2930 : : {
2931 : : /* We must test both. */
2932 : 2260 : GElf_Vernaux vernaux_mem;
2933 : 2260 : GElf_Vernaux *vernaux = NULL;
2934 : 2260 : size_t vn_offset = 0;
2935 : :
2936 : 2260 : GElf_Verneed verneed_mem;
2937 : 2260 : GElf_Verneed *verneed
2938 : 2260 : = gelf_getverneed (verneed_data, 0, &verneed_mem);
2939 [ + - ]: 8258 : while (verneed != NULL)
2940 : : {
2941 : 8258 : size_t vna_offset = vn_offset;
2942 : :
2943 : 8258 : vernaux = gelf_getvernaux (verneed_data,
2944 : 8258 : vna_offset += verneed->vn_aux,
2945 : : &vernaux_mem);
2946 [ + + ]: 30364 : while (vernaux != NULL && vernaux->vna_other != *versym
2947 [ + + ]: 19846 : && vernaux->vna_next != 0
2948 [ + - ]: 22106 : && (verneed_data->d_size - vna_offset
2949 [ + - ]: 13848 : >= vernaux->vna_next))
2950 : : {
2951 : : /* Update the offset. */
2952 : 13848 : vna_offset += vernaux->vna_next;
2953 : :
2954 : 13848 : vernaux = (vernaux->vna_next == 0
2955 : : ? NULL
2956 : 13848 : : gelf_getvernaux (verneed_data,
2957 : : vna_offset,
2958 : : &vernaux_mem));
2959 : : }
2960 : :
2961 : : /* Check whether we found the version. */
2962 [ + - + + ]: 8258 : if (vernaux != NULL && vernaux->vna_other == *versym)
2963 : : /* Found it. */
2964 : : break;
2965 : :
2966 [ + - ]: 5998 : if (verneed_data->d_size - vn_offset < verneed->vn_next)
2967 : : break;
2968 : :
2969 : 5998 : vn_offset += verneed->vn_next;
2970 : 5998 : verneed
2971 : : = (verneed->vn_next == 0
2972 : : ? NULL
2973 [ + - ]: 5998 : : gelf_getverneed (verneed_data, vn_offset,
2974 : : &verneed_mem));
2975 : : }
2976 : :
2977 [ + - + - ]: 2260 : if (vernaux != NULL && vernaux->vna_other == *versym)
2978 : : {
2979 : 4520 : printf ("@%s (%u)",
2980 [ - + ]: 2260 : use_dynamic_segment == true
2981 : 0 : ? (char *)symstr_data->d_buf
2982 : 0 : + vernaux->vna_name
2983 : 2260 : : elf_strptr (ebl->elf, verneed_stridx,
2984 : 2260 : vernaux->vna_name),
2985 : : (unsigned int)vernaux->vna_other);
2986 : 2260 : 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 [ + - + - ]: 2564 : 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 : 89596 : 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 [ + + ]: 3396 : for (unsigned int cnt = 0; cnt < shdr->sh_size / sh_entsize; ++cnt)
3812 : : {
3813 [ + + ]: 3266 : if (cnt % 2 == 0)
3814 : 1650 : printf ("\n %4d:", cnt);
3815 : :
3816 : 3266 : GElf_Versym symmem;
3817 : 3266 : GElf_Versym *sym = gelf_getversym (data, cnt, &symmem);
3818 [ + - ]: 3266 : if (sym == NULL)
3819 : : break;
3820 : :
3821 [ + + + ]: 3266 : 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 : 2526 : default:
3835 [ + - ]: 5052 : n = printf ("%4d%c%s",
3836 [ + - ]: 2526 : *sym & 0x7fff, *sym & 0x8000 ? 'h' : ' ',
3837 : : (vername != NULL
3838 [ + - ]: 2526 : && (unsigned int) (*sym & 0x7fff) < nvername)
3839 : 2526 : ? vername[*sym & 0x7fff] : "???");
3840 [ + - ]: 2526 : if ((unsigned int) (*sym & 0x7fff) < nvername
3841 [ + - + + ]: 2526 : && filename != NULL && filename[*sym & 0x7fff] != NULL)
3842 : 2222 : n += printf ("(%s)", filename[*sym & 0x7fff]);
3843 : 2526 : printf ("%*s", MAX (0, 33 - (int) n), " ");
3844 : 2526 : 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 : 814 : 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 [ - + ]: 814 : 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 : 2587670 : 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 : 2587670 : GElf_Sym sym;
4505 : 2587670 : GElf_Off off = 0;
4506 [ + + ]: 14276 : const char *name = (print_address_names && ! print_unresolved_addresses)
4507 : 14112 : ? dwfl_module_addrinfo (dwflmod, address, &off, &sym, NULL, NULL, NULL)
4508 [ + + ]: 2587670 : : NULL;
4509 : :
4510 : 2587670 : const char *scn;
4511 [ + + ]: 2587670 : if (print_unresolved_addresses)
4512 : : {
4513 : 168 : address = raw;
4514 : 168 : scn = NULL;
4515 : : }
4516 : : else
4517 : : {
4518 : : /* Relativize the address. */
4519 : 2587502 : int n = dwfl_module_relocations (dwflmod);
4520 [ + + ]: 2587502 : 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 : 2584732 : scn = (i < 0 ? NULL
4524 [ - + ]: 2584732 : : dwfl_module_relocation_info (dwflmod, i, NULL));
4525 : : }
4526 : :
4527 [ - + ]: 2587670 : if ((name != NULL
4528 : 13696 : ? (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 : 22472 : : fprintf (out, "%s+%#0*" PRIx64 " <%s>",
4546 : 11236 : scn, 2 + address_size * 2, address, name))
4547 : : : (address_size == 0
4548 : 160 : ? fprintf (out, "%#" PRIx64 " <%s>", address, name)
4549 : 1700 : : fprintf (out, "%#0*" PRIx64 " <%s>",
4550 : 850 : 2 + address_size * 2, address, name))))
4551 : : : (scn != NULL
4552 : : ? (address_size == 0
4553 : 945534 : ? fprintf (out, "%s+%#" PRIx64, scn, address)
4554 : 3255908 : : fprintf (out, "%s+%#0*" PRIx64,
4555 : 1627954 : scn, 2 + address_size * 2, address))
4556 : : : (address_size == 0
4557 : 70 : ? fprintf (out, "%#" PRIx64, address)
4558 : 832 : : fprintf (out, "%#0*" PRIx64,
4559 [ + + + + : 5174924 : 2 + address_size * 2, address)))) < 0)
+ + + + +
+ + + + +
+ + + + +
+ + + ]
4560 : 0 : error_exit (0, _("sprintf failure"));
4561 : 2587670 : }
4562 : :
4563 : :
4564 : : static const char *
4565 : 2651210 : dwarf_tag_string (unsigned int tag)
4566 : : {
4567 [ - + + - : 2651210 : switch (tag)
- - - - +
+ + + + -
- - - - -
+ - + - +
- - + + -
+ - - - -
- - - + -
+ - + + +
- - - - -
- + - - +
- - - + -
+ + + + -
- - - - +
+ + + - +
+ + + - -
- ]
4568 : : {
4569 : : #define DWARF_ONE_KNOWN_DW_TAG(NAME, CODE) case CODE: return #NAME;
4570 : 2651210 : 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 : 10223812 : dwarf_attr_string (unsigned int attrnum)
4580 : : {
4581 [ + - + - : 10223812 : switch (attrnum)
- - - + -
- + + + -
- - - + +
- + - - +
- + - + -
- - - - -
- - - - -
- - - - -
- - - + -
+ - + - +
- - - + +
- - - + -
+ - + + -
- + + + +
- + + + -
+ - - + -
+ - + + -
+ - - + +
+ + - - -
- - + + +
+ - + - +
- - + - +
+ - + - +
+ - + + +
+ + + + -
- + - - -
+ - + - -
- - + + -
+ - - - -
+ - - - +
+ - + - -
- - + + -
- - - - +
+ - - - -
- - - + ]
4582 : : {
4583 : : #define DWARF_ONE_KNOWN_DW_AT(NAME, CODE) case CODE: return #NAME;
4584 : 10223810 : 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 : 10235850 : dwarf_form_string (unsigned int form)
4594 : : {
4595 [ + + + + : 10235850 : switch (form)
+ - - - -
- + - - +
+ + + + +
+ + + - +
+ - - + -
- + + - +
+ + + + +
+ + + - -
- + - + ]
4596 : : {
4597 : : #define DWARF_ONE_KNOWN_DW_FORM(NAME, CODE) case CODE: return #NAME;
4598 : 10235846 : 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 : 4284 : dwarf_lang_string (unsigned int lang)
4608 : : {
4609 [ - - + - : 4284 : switch (lang)
- - - + -
- + + - +
- - + - -
- - - - -
- - - - -
+ - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - + - ]
4610 : : {
4611 : : #define DWARF_ONE_KNOWN_DW_LANG(NAME, CODE) case CODE: return #NAME;
4612 : 4282 : 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 : 8 : dwarf_lname_string (unsigned int lname)
4622 : : {
4623 [ - - - + : 8 : switch (lname)
- + - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - ]
4624 : : {
4625 : : #define DWARF_ONE_KNOWN_DW_LNAME(NAME, CODE) case CODE: return #NAME;
4626 : 8 : 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 : 8344 : dwarf_inline_string (unsigned int code)
4636 : : {
4637 : 8344 : 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 : 8344 : if (likely (code < sizeof (known) / sizeof (known[0])))
4645 : 8344 : return known[code];
4646 : :
4647 : : return NULL;
4648 : : }
4649 : :
4650 : :
4651 : : static const char *
4652 : 68362 : dwarf_encoding_string (unsigned int code)
4653 : : {
4654 : 68362 : 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 : 68362 : if (likely (code < sizeof (known) / sizeof (known[0])))
4662 : 68362 : 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 : 1280468 : dwarf_locexpr_opcode_string (unsigned int code)
4806 : : {
4807 : 1280468 : 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 : 1280468 : if (likely (code < sizeof (known) / sizeof (known[0])))
4818 : 1280468 : return known[code];
4819 : :
4820 : : return NULL;
4821 : : }
4822 : :
4823 : :
4824 : : static const char *
4825 : 4032 : dwarf_unit_string (unsigned int type)
4826 : : {
4827 [ - + + - : 4032 : switch (type)
- - + ]
4828 : : {
4829 : : #define DWARF_ONE_KNOWN_DW_UT(NAME, CODE) case CODE: return #NAME;
4830 : 18 : 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 : 169588 : dwarf_range_list_encoding_string (unsigned int kind)
4840 : : {
4841 [ - + + - : 169588 : switch (kind)
+ - - -
+ ]
4842 : : {
4843 : : #define DWARF_ONE_KNOWN_DW_RLE(NAME, CODE) case CODE: return #NAME;
4844 : 150510 : 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 : 752178 : dwarf_loc_list_encoding_string (unsigned int kind)
4854 : : {
4855 [ - - + + : 752178 : switch (kind)
- + - + -
- + ]
4856 : : {
4857 : : #define DWARF_ONE_KNOWN_DW_LLE(NAME, CODE) case CODE: return #NAME;
4858 : 699922 : 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 : 12038 : dwarf_line_content_description_string (unsigned int kind)
4870 : : {
4871 [ + + - - : 12038 : switch (kind)
- + ]
4872 : : {
4873 : : #define DWARF_ONE_KNOWN_DW_LNCT(NAME, CODE) case CODE: return #NAME;
4874 : 12030 : 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 : 24129722 : 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 : 24129722 : static char unknown_buf[20];
4931 : :
4932 [ + + ]: 24129722 : 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 : 2651210 : dwarf_tag_name (unsigned int tag)
4954 : : {
4955 : 2651210 : const char *ret = dwarf_tag_string (tag);
4956 : 2651210 : return string_or_unknown (ret, tag, DW_TAG_lo_user, DW_TAG_hi_user, true);
4957 : : }
4958 : :
4959 : : static const char *
4960 : 10223812 : dwarf_attr_name (unsigned int attr)
4961 : : {
4962 : 10223812 : const char *ret = dwarf_attr_string (attr);
4963 : 10223812 : return string_or_unknown (ret, attr, DW_AT_lo_user, DW_AT_hi_user, true);
4964 : : }
4965 : :
4966 : :
4967 : : static const char *
4968 : 10235850 : dwarf_form_name (unsigned int form)
4969 : : {
4970 : 10235850 : const char *ret = dwarf_form_string (form);
4971 : 10235850 : return string_or_unknown (ret, form, 0, 0, true);
4972 : : }
4973 : :
4974 : :
4975 : : static const char *
4976 : 4280 : dwarf_lang_name (unsigned int lang)
4977 : : {
4978 : 4280 : const char *ret = dwarf_lang_string (lang);
4979 : 4280 : return string_or_unknown (ret, lang, DW_LANG_lo_user, DW_LANG_hi_user, false);
4980 : : }
4981 : :
4982 : :
4983 : : static const char *
4984 : 8 : dwarf_lname_name (unsigned int lname)
4985 : : {
4986 : 8 : const char *ret = dwarf_lname_string (lname);
4987 : 8 : return string_or_unknown (ret, lname, DW_LNAME_lo_user, DW_LNAME_hi_user,
4988 : : false);
4989 : : }
4990 : :
4991 : :
4992 : : static const char *
4993 : 8344 : dwarf_inline_name (unsigned int code)
4994 : : {
4995 [ + - ]: 8344 : const char *ret = dwarf_inline_string (code);
4996 : 8344 : return string_or_unknown (ret, code, 0, 0, false);
4997 : : }
4998 : :
4999 : :
5000 : : static const char *
5001 : 68362 : dwarf_encoding_name (unsigned int code)
5002 : : {
5003 [ + - ]: 68362 : const char *ret = dwarf_encoding_string (code);
5004 : 68362 : 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 : 4032 : dwarf_unit_name (unsigned int type)
5074 : : {
5075 : 4032 : const char *ret = dwarf_unit_string (type);
5076 : 4032 : return string_or_unknown (ret, type, DW_UT_lo_user, DW_UT_hi_user, true);
5077 : : }
5078 : :
5079 : :
5080 : : static const char *
5081 : 169588 : dwarf_range_list_encoding_name (unsigned int kind)
5082 : : {
5083 : 169588 : const char *ret = dwarf_range_list_encoding_string (kind);
5084 : 169588 : return string_or_unknown (ret, kind, 0, 0, false);
5085 : : }
5086 : :
5087 : :
5088 : : static const char *
5089 : 752178 : dwarf_loc_list_encoding_name (unsigned int kind)
5090 : : {
5091 : 752178 : const char *ret = dwarf_loc_list_encoding_string (kind);
5092 : 752178 : return string_or_unknown (ret, kind, 0, 0, false);
5093 : : }
5094 : :
5095 : :
5096 : : static const char *
5097 : 12038 : dwarf_line_content_description_name (unsigned int kind)
5098 : : {
5099 : 12038 : const char *ret = dwarf_line_content_description_string (kind);
5100 : 12038 : 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 : 899432 : 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 [ + + ]: 899432 : const unsigned int ref_size = vers < 3 ? addrsize : offset_size;
5165 : :
5166 [ - + ]: 899432 : 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 [ + + ]: 2179900 : while (len-- > 0)
5177 : 1280468 : {
5178 : 1280468 : uint_fast8_t op = *data++;
5179 : :
5180 [ + - ]: 1280468 : const char *op_name = dwarf_locexpr_opcode_string (op);
5181 [ - + ]: 1280468 : 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 [ + - + + : 1280468 : switch (op)
+ + + + -
- + + - +
- - + + +
+ + - - -
- + + + ]
5192 : : {
5193 : 30744 : case DW_OP_addr:;
5194 : : /* Address operand. */
5195 : 30744 : Dwarf_Word addr;
5196 [ - + ]: 30744 : NEED (addrsize);
5197 [ + + ]: 30744 : if (addrsize == 4)
5198 [ + + ]: 108 : addr = read_4ubyte_unaligned (dbg, data);
5199 [ + - ]: 30636 : else if (addrsize == 8)
5200 [ + + ]: 30636 : addr = read_8ubyte_unaligned (dbg, data);
5201 : : else
5202 : 0 : goto invalid;
5203 : 30744 : data += addrsize;
5204 : 30744 : CONSUME (addrsize);
5205 : :
5206 : 30744 : fprintf (out, "%*s[%2" PRIuMAX "] %s ",
5207 : : indent, "", (uintmax_t) offset, op_name);
5208 : 30744 : print_dwarf_addr (dwflmod, 0, addr, addr, out);
5209 : 30744 : fprintf (out, "\n");
5210 : :
5211 : 30744 : offset += 1 + addrsize;
5212 : 30744 : 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 : 28836 : 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 [ - + ]: 28836 : NEED (1);
5239 : 57672 : fprintf (out, "%*s[%2" PRIuMAX "] %s %" PRIu8 "\n",
5240 : : indent, "", (uintmax_t) offset,
5241 : 28836 : op_name, *((uint8_t *) data));
5242 : 28836 : ++data;
5243 : 28836 : --len;
5244 : 28836 : offset += 2;
5245 : 28836 : break;
5246 : :
5247 : 3718 : case DW_OP_const2u:
5248 [ - + ]: 3718 : NEED (2);
5249 : : // XXX value might be modified by relocation
5250 : 7436 : fprintf (out, "%*s[%2" PRIuMAX "] %s %" PRIu16 "\n",
5251 : : indent, "", (uintmax_t) offset,
5252 [ - + ]: 3718 : op_name, read_2ubyte_unaligned (dbg, data));
5253 : 3718 : CONSUME (2);
5254 : 3718 : data += 2;
5255 : 3718 : offset += 3;
5256 : 3718 : 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 : 5572 : case DW_OP_const1s:
5281 [ - + ]: 5572 : NEED (1);
5282 : : // XXX value might be modified by relocation
5283 : 11144 : fprintf (out, "%*s[%2" PRIuMAX "] %s %" PRId8 "\n",
5284 : : indent, "", (uintmax_t) offset,
5285 : 5572 : op_name, *((int8_t *) data));
5286 : 5572 : ++data;
5287 : 5572 : --len;
5288 : 5572 : offset += 2;
5289 : 5572 : 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 : 28590 : case DW_OP_piece:
5325 : : case DW_OP_regx:
5326 : : case DW_OP_plus_uconst:
5327 : 28590 : case DW_OP_constu:;
5328 : 28590 : const unsigned char *start = data;
5329 : 28590 : uint64_t uleb;
5330 [ - + ]: 28590 : NEED (1);
5331 : 28590 : get_uleb128 (uleb, data, data + len);
5332 : 28590 : fprintf (out, "%*s[%2" PRIuMAX "] %s %" PRIu64 "\n",
5333 : : indent, "", (uintmax_t) offset, op_name, uleb);
5334 [ - + ]: 28590 : CONSUME (data - start);
5335 : 28590 : offset += 1 + (data - start);
5336 : 28590 : 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 : 213038 : case DW_OP_fbreg:
5372 : : case DW_OP_breg0 ... DW_OP_breg31:
5373 : : case DW_OP_consts:
5374 : 213038 : start = data;
5375 : 213038 : int64_t sleb;
5376 [ - + ]: 213038 : NEED (1);
5377 : 213038 : get_sleb128 (sleb, data, data + len);
5378 : 213038 : fprintf (out, "%*s[%2" PRIuMAX "] %s %" PRId64 "\n",
5379 : : indent, "", (uintmax_t) offset, op_name, sleb);
5380 [ - + ]: 213038 : CONSUME (data - start);
5381 : 213038 : offset += 1 + (data - start);
5382 : 213038 : 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 : 1272 : case DW_OP_skip:
5417 : : case DW_OP_bra:
5418 [ - + ]: 1272 : NEED (2);
5419 : 2544 : fprintf (out, "%*s[%2" PRIuMAX "] %s %" PRIuMAX "\n",
5420 : : indent, "", (uintmax_t) offset, op_name,
5421 [ - + ]: 1272 : (uintmax_t) (offset + read_2sbyte_unaligned (dbg, data) + 3));
5422 : 1272 : CONSUME (2);
5423 : 1272 : data += 2;
5424 : 1272 : offset += 3;
5425 : 1272 : 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 : 6868 : case DW_OP_implicit_pointer:
5441 : : case DW_OP_GNU_implicit_pointer:
5442 : : /* DIE offset operand. */
5443 : 6868 : start = data;
5444 [ - + ]: 6868 : NEED (ref_size);
5445 [ - + ]: 6868 : if (ref_size != 4 && ref_size != 8)
5446 : 0 : goto invalid; /* Cannot be used in CFA. */
5447 [ + - ]: 6868 : if (ref_size == 4)
5448 [ - + ]: 6868 : addr = read_4ubyte_unaligned (dbg, data);
5449 : : else
5450 [ # # ]: 0 : addr = read_8ubyte_unaligned (dbg, data);
5451 : 6868 : data += ref_size;
5452 : : /* Byte offset operand. */
5453 [ - + ]: 6868 : NEED (1);
5454 : 6868 : get_sleb128 (sleb, data, data + len);
5455 : :
5456 : 6868 : fprintf (out, "%*s[%2" PRIuMAX "] %s [%6" PRIxMAX "] %+" PRId64 "\n",
5457 : : indent, "", (intmax_t) offset,
5458 : : op_name, (uintmax_t) addr, sleb);
5459 [ - + ]: 6868 : CONSUME (data - start);
5460 : 6868 : offset += 1 + (data - start);
5461 : 6868 : break;
5462 : :
5463 : 72874 : case DW_OP_entry_value:
5464 : : case DW_OP_GNU_entry_value:
5465 : : /* Size plus expression block. */
5466 : 72874 : start = data;
5467 [ - + ]: 72874 : NEED (1);
5468 : 72874 : get_uleb128 (uleb, data, data + len);
5469 : 72874 : fprintf (out, "%*s[%2" PRIuMAX "] %s:\n",
5470 : : indent, "", (uintmax_t) offset, op_name);
5471 [ - + ]: 72874 : NEED (uleb);
5472 : 72874 : print_ops (dwflmod, dbg, indent + 5, indent + 5, vers,
5473 : : addrsize, offset_size, cu, uleb, data, out);
5474 : 72874 : data += uleb;
5475 [ - + ]: 72874 : CONSUME (data - start);
5476 : 72874 : offset += 1 + (data - start);
5477 : 72874 : 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 : 883964 : fprintf (out, "%*s[%2" PRIuMAX "] %s\n",
5582 : : indent, "", (uintmax_t) offset, op_name);
5583 : 883964 : ++offset;
5584 : 883964 : break;
5585 : : }
5586 : :
5587 : 1280468 : indent = indentrest;
5588 : 1280468 : 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 : 6130 : cudie_base (Dwarf_Die *cudie)
5718 : : {
5719 : 6130 : 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 [ - + ]: 6130 : 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 : 6130 : return base;
5733 : : }
5734 : :
5735 : : static Dwarf_Addr
5736 : 6130 : listptr_base (struct listptr *p)
5737 : : {
5738 : 6130 : Dwarf_Die cu = CUDIE (p->cu);
5739 : 6130 : 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 : 2069032 : compare_listptr (const void *a, const void *b)
5747 : : {
5748 : 2069032 : const char *name = sort_listptr_name;
5749 : 2069032 : struct listptr *p1 = (void *) a;
5750 : 2069032 : struct listptr *p2 = (void *) b;
5751 : :
5752 [ + + ]: 2069032 : if (p1->offset < p2->offset)
5753 : : return -1;
5754 [ + + ]: 202072 : if (p1->offset > p2->offset)
5755 : : return 1;
5756 : :
5757 [ + - - + ]: 9618 : if (!p1->warned && !p2->warned)
5758 : : {
5759 [ - + ]: 9618 : 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 [ - + ]: 9618 : 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 [ + - ]: 9618 : 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 : 510 : reset_listptr (struct listptr_table *table)
5807 : : {
5808 : 510 : free (table->table);
5809 : 510 : table->table = NULL;
5810 : 510 : table->n = table->alloc = 0;
5811 : : }
5812 : :
5813 : : /* Returns false if offset doesn't fit. See struct listptr. */
5814 : : static bool
5815 : 295006 : 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 [ + + ]: 295006 : if (print_debug_sections & section)
5820 : : {
5821 [ + + ]: 294750 : if (table->n == table->alloc)
5822 : : {
5823 [ + + ]: 424 : if (table->alloc == 0)
5824 : 174 : table->alloc = 128;
5825 : : else
5826 : 250 : table->alloc *= 2;
5827 : 424 : table->table = xrealloc (table->table,
5828 : 424 : table->alloc * sizeof table->table[0]);
5829 : : }
5830 : :
5831 : 294750 : struct listptr *p = &table->table[table->n++];
5832 : :
5833 : 294750 : *p = (struct listptr)
5834 : : {
5835 : 294750 : .addr64 = address_size == 8,
5836 : 294750 : .dwarf64 = offset_size == 8,
5837 : : .offset = offset,
5838 : : .cu = cu,
5839 : : .attr = attr
5840 : : };
5841 : :
5842 [ + - ]: 294750 : 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 : 125726 : 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 [ + - ]: 125726 : if (*idxp < table->n)
5915 : : {
5916 [ + - ]: 249878 : while (++*idxp < table->n)
5917 : : {
5918 : 249878 : Dwarf_Off next = table->table[*idxp].offset;
5919 [ + + ]: 249878 : 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 : 1756248 : get_listptr (struct listptr_table *table, size_t idx)
5929 : : {
5930 : 1756248 : 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 : 5922 : 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 : 5922 : while (*idxp < table->n
5944 [ + - + + ]: 47800 : && table->table[*idxp].offset < start)
5945 : 41878 : ++*idxp;
5946 : :
5947 [ + - ]: 5922 : if (*idxp < table->n
5948 [ + - ]: 5922 : && table->table[*idxp].offset >= start
5949 [ + - ]: 5922 : && table->table[*idxp].offset < end)
5950 : : {
5951 : 5922 : struct listptr *p = &table->table[*idxp];
5952 : 5922 : *base = listptr_base (p);
5953 : 5922 : *cu = p->cu;
5954 : 5922 : 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 : 877904 : listptr_attr (struct listptr_table *table, size_t idxp,
5965 : : Dwarf_Off offset, unsigned int attr)
5966 : : {
5967 : 1756200 : struct listptr *listptr;
5968 : 1756200 : do
5969 : : {
5970 [ + + ]: 1756200 : listptr = get_listptr (table, idxp);
5971 [ + - ]: 1755964 : if (listptr == NULL)
5972 : : return false;
5973 : :
5974 [ + + + + ]: 1755964 : if (listptr->offset == offset && listptr->attr == attr)
5975 : : return true;
5976 : :
5977 : 1630238 : idxp++;
5978 : : }
5979 [ + + ]: 1630238 : 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 [ + + ]: 4222 : while (offset < sh_size)
6003 : : {
6004 : 4134 : fprintf (out, _("\nAbbreviation section at offset %" PRIu64 ":\n"),
6005 : : offset);
6006 : :
6007 : 381198 : while (1)
6008 : 188532 : {
6009 : 192666 : size_t length;
6010 : 192666 : Dwarf_Abbrev abbrev;
6011 : :
6012 : 192666 : int res = dwarf_offabbrev (dbg, offset, &length, &abbrev);
6013 [ + + ]: 192666 : if (res != 0)
6014 : : {
6015 [ - + ]: 4134 : 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 : 4134 : ++offset;
6025 : 4134 : break;
6026 : : }
6027 : :
6028 : : /* We know these calls can never fail. */
6029 : 188532 : unsigned int code = dwarf_getabbrevcode (&abbrev);
6030 : 188532 : unsigned int tag = dwarf_getabbrevtag (&abbrev);
6031 : 188532 : int has_children = dwarf_abbrevhaschildren (&abbrev);
6032 : :
6033 [ + + ]: 188532 : 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 : 188532 : size_t cnt = 0;
6040 : 188532 : unsigned int name;
6041 : 188532 : unsigned int form;
6042 : 188532 : Dwarf_Sword data;
6043 : 188532 : Dwarf_Off enoffset;
6044 : 188532 : while (dwarf_getabbrevattr_data (&abbrev, cnt, &name, &form,
6045 [ + + ]: 1079418 : &data, &enoffset) == 0)
6046 : : {
6047 : 890886 : fprintf (out, " attr: %s, form: %s",
6048 : : dwarf_attr_name (name), dwarf_form_name (form));
6049 [ + + ]: 890886 : if (form == DW_FORM_implicit_const)
6050 : 62292 : fprintf (out, " (%" PRId64 ")", data);
6051 : 890886 : fprintf (out, ", offset: %#" PRIx64 "\n", (uint64_t) enoffset);
6052 : 890886 : ++cnt;
6053 : : }
6054 : :
6055 : 188532 : 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 [ + + ]: 4206 : while (readp < readendp)
6379 : : {
6380 : 4116 : const unsigned char *hdrstart = readp;
6381 : 4116 : size_t start_offset = hdrstart - (const unsigned char *) data->d_buf;
6382 : :
6383 : 4116 : fprintf (out, "\n%s %zu:\n", table_offset_lower_str, start_offset);
6384 [ - + ]: 4116 : 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 [ + + ]: 4116 : Dwarf_Word length = read_4ubyte_unaligned_inc (dbg, readp);
6393 : 4116 : unsigned int length_bytes = 4;
6394 [ - + ]: 4116 : 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 : 4116 : const unsigned char *nexthdr = readp + length;
6403 : 4116 : fprintf (out, "\n %s: %6" PRIu64 "\n",
6404 : : length_str, (uint64_t) length);
6405 : :
6406 [ - + ]: 4116 : if (unlikely (length > (size_t) (readendp - readp)))
6407 : 0 : goto invalid_data;
6408 : :
6409 [ - + ]: 4116 : if (length == 0)
6410 : 0 : continue;
6411 : :
6412 [ - + ]: 4116 : if (readp + 2 > readendp)
6413 : 0 : goto invalid_data;
6414 [ + + ]: 4116 : uint_fast16_t version = read_2ubyte_unaligned_inc (dbg, readp);
6415 : 4116 : fprintf (out, " %s: %6" PRIuFAST16 "\n",
6416 : : dwarf_ver_str, version);
6417 [ - + ]: 4116 : if (version != 2)
6418 : : {
6419 : 0 : error (0, 0, _("unsupported aranges version"));
6420 : 0 : goto next_table;
6421 : : }
6422 : :
6423 : 4116 : Dwarf_Word offset;
6424 [ - + ]: 4116 : if (readp + length_bytes > readendp)
6425 : 0 : goto invalid_data;
6426 [ - + ]: 4116 : if (length_bytes == 8)
6427 [ # # ]: 0 : offset = read_8ubyte_unaligned_inc (dbg, readp);
6428 : : else
6429 [ + + ]: 4116 : offset = read_4ubyte_unaligned_inc (dbg, readp);
6430 : 4116 : fprintf (out, " %s: %6" PRIx64 "\n",
6431 : : cu_offset_str, (uint64_t) offset);
6432 : :
6433 [ - + ]: 4116 : if (readp + 1 > readendp)
6434 : 0 : goto invalid_data;
6435 : 4116 : unsigned int address_size = *readp++;
6436 : 4116 : fprintf (out, " %s: %6" PRIu64 "\n",
6437 : : addr_size_str, (uint64_t) address_size);
6438 [ - + ]: 4116 : if (address_size != 4 && address_size != 8)
6439 : : {
6440 : 0 : error (0, 0, _("unsupported address size"));
6441 : 0 : goto next_table;
6442 : : }
6443 : :
6444 [ - + ]: 4116 : if (readp + 1 > readendp)
6445 : 0 : goto invalid_data;
6446 : 4116 : unsigned int segment_size = *readp++;
6447 : 4116 : fprintf (out, " %s: %6" PRIu64 "\n\n",
6448 : : seg_size_str, (uint64_t) segment_size);
6449 [ - + - - ]: 4116 : 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 : 4116 : readp += ((2 * address_size - ((readp - hdrstart) % (2 * address_size)))
6457 : 4116 : % (2 * address_size));
6458 : :
6459 [ + - ]: 10540 : while (readp < nexthdr)
6460 : : {
6461 : 10540 : Dwarf_Word range_address;
6462 : 10540 : Dwarf_Word range_length;
6463 : 10540 : Dwarf_Word segment = 0;
6464 [ - + ]: 10540 : if (readp + 2 * address_size + segment_size > readendp)
6465 : 0 : goto invalid_data;
6466 [ + + ]: 10540 : 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 [ + + ]: 10448 : range_address = read_8ubyte_unaligned_inc (dbg, readp);
6474 [ + + ]: 10448 : range_length = read_8ubyte_unaligned_inc (dbg, readp);
6475 : : }
6476 : :
6477 [ - + ]: 10540 : if (segment_size == 4)
6478 [ # # ]: 0 : segment = read_4ubyte_unaligned_inc (dbg, readp);
6479 [ - + ]: 10540 : else if (segment_size == 8)
6480 [ # # ]: 0 : segment = read_8ubyte_unaligned_inc (dbg, readp);
6481 : :
6482 [ + + ]: 10540 : if (range_address == 0 && range_length == 0 && segment == 0)
6483 : : break;
6484 : :
6485 : 6424 : fprintf (out, " ");
6486 : 6424 : print_dwarf_addr (dwflmod, address_size, range_address,
6487 : : range_address, out);
6488 : 6424 : fprintf (out, "..");
6489 : 6424 : print_dwarf_addr (dwflmod, address_size,
6490 : 6424 : range_address + range_length - 1,
6491 : : range_length, out);
6492 [ - + ]: 6424 : if (segment_size != 0)
6493 : 0 : fprintf (out, " (%" PRIx64 ")\n", (uint64_t) segment);
6494 : : else
6495 : 6424 : fprintf (out, "\n");
6496 : : }
6497 : :
6498 : 4116 : next_table:
6499 [ - + ]: 4116 : 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 [ + + ]: 2394 : while (readp < dataend)
6555 : : {
6556 [ - + ]: 2356 : 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 : 2356 : ptrdiff_t offset = readp - (unsigned char *) data->d_buf;
6565 : 2356 : fprintf (out, "%s 0x%" PRIx64 ":\n\n",
6566 : : table_offset_upper_str, (uint64_t) offset);
6567 : :
6568 [ - + ]: 2356 : uint64_t unit_length = read_4ubyte_unaligned_inc (dbg, readp);
6569 : 2356 : unsigned int offset_size = 4;
6570 [ - + ]: 2356 : 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 : 2356 : 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 [ + - ]: 2356 : if (readp > dataend - 8
6584 [ + - ]: 2356 : || unit_length < 8
6585 [ - + ]: 2356 : || unit_length > (uint64_t) (dataend - readp))
6586 : 0 : goto invalid_data;
6587 : :
6588 : 2356 : const unsigned char *nexthdr = readp + unit_length;
6589 : :
6590 [ - + ]: 2356 : uint16_t version = read_2ubyte_unaligned_inc (dbg, readp);
6591 : 2356 : fprintf (out, " %s: %8" PRIu16 "\n", dwarf_ver_str, version);
6592 : :
6593 [ - + ]: 2356 : if (version != 5)
6594 : : {
6595 : 0 : error (0, 0, _("Unknown version"));
6596 : 0 : goto next_table;
6597 : : }
6598 : :
6599 : 2356 : uint8_t address_size = *readp++;
6600 : 2356 : fprintf (out, " %s: %8" PRIu64 "\n",
6601 : : addr_size_str, (uint64_t) address_size);
6602 : :
6603 [ - + ]: 2356 : if (address_size != 4 && address_size != 8)
6604 : : {
6605 : 0 : error (0, 0, _("unsupported address size"));
6606 : 0 : goto next_table;
6607 : : }
6608 : :
6609 : 2356 : uint8_t segment_size = *readp++;
6610 : 2356 : fprintf (out, _(" Segment size: %8" PRIu64 "\n"),
6611 : : (uint64_t) segment_size);
6612 : :
6613 [ - + - - ]: 2356 : 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 [ - + ]: 2356 : uint32_t offset_entry_count = read_4ubyte_unaligned_inc (dbg, readp);
6620 : 2356 : 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 : 2356 : Dwarf_Addr cu_base = 0;
6625 : 2356 : struct Dwarf_CU *cu = NULL;
6626 [ - + ]: 2356 : if (listptr_cu (&known_rnglistptr, &listptr_idx,
6627 : : (Dwarf_Off) offset,
6628 : 2356 : (Dwarf_Off) (nexthdr - (unsigned char *) data->d_buf),
6629 : : &cu_base, &cu)
6630 [ # # ]: 0 : || split_dwarf_cu_base (dbg, &cu, &cu_base))
6631 : 2356 : {
6632 : 2356 : Dwarf_Die cudie;
6633 [ - + ]: 2356 : 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 : 2356 : fprintf (out, " %s [%6" PRIx64 "] %s: ",
6639 : : cu_str, dwarf_dieoffset (&cudie), base_str);
6640 : 2356 : print_dwarf_addr (dwflmod, address_size, cu_base, cu_base, out);
6641 : 2356 : fprintf (out, "\n");
6642 : : }
6643 : : else
6644 : 0 : fprintf (out, _(" Not associated with a CU.\n"));
6645 : :
6646 : 2356 : fprintf (out, "\n");
6647 : :
6648 : 2356 : const unsigned char *offset_array_start = readp;
6649 [ + + ]: 2356 : 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 : 2356 : Dwarf_Addr base = cu_base;
6680 : 2356 : bool start_of_list = true;
6681 [ + + ]: 171944 : while (readp < nexthdr)
6682 : : {
6683 : 169588 : uint8_t kind = *readp++;
6684 : 169588 : uint64_t op1, op2;
6685 : :
6686 : : /* Skip padding. */
6687 [ - + ]: 169588 : if (start_of_list && kind == DW_RLE_end_of_list)
6688 : 0 : continue;
6689 : :
6690 [ + + ]: 169588 : if (start_of_list)
6691 : : {
6692 : 34340 : base = cu_base;
6693 : 34340 : fprintf (out, " Offset: %" PRIx64 ", Index: %" PRIx64 "\n",
6694 : 34340 : (uint64_t) (readp - (unsigned char *) data->d_buf - 1),
6695 : 34340 : (uint64_t) (readp - offset_array_start - 1));
6696 : 34340 : start_of_list = false;
6697 : : }
6698 : :
6699 : 169588 : fprintf (out, " %s", dwarf_range_list_encoding_name (kind));
6700 [ + - - - : 169588 : switch (kind)
+ + - +
- ]
6701 : : {
6702 : 34340 : case DW_RLE_end_of_list:
6703 : 34340 : start_of_list = true;
6704 : 34340 : fprintf (out, "\n\n");
6705 : 34340 : 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 : 107728 : case DW_RLE_offset_pair:
6793 [ - + ]: 107728 : if ((uint64_t) (nexthdr - readp) < 1)
6794 : 0 : goto invalid_range;
6795 : 107728 : get_uleb128 (op1, readp, nexthdr);
6796 [ - + ]: 107728 : if ((uint64_t) (nexthdr - readp) < 1)
6797 : 0 : goto invalid_range;
6798 : 107728 : get_uleb128 (op2, readp, nexthdr);
6799 : 107728 : fprintf (out, " %" PRIx64 ", %" PRIx64 "\n", op1, op2);
6800 [ + - ]: 107728 : if (! print_unresolved_addresses)
6801 : : {
6802 : 107728 : op1 += base;
6803 : 107728 : op2 += base;
6804 : 107728 : fprintf (out, " ");
6805 : 107728 : print_dwarf_addr (dwflmod, address_size, op1, op1, out);
6806 : 107728 : fprintf (out, "..\n ");
6807 : 107728 : print_dwarf_addr (dwflmod, address_size, op2 - 1, op2, out);
6808 : 107728 : fprintf (out, "\n");
6809 : : }
6810 : : break;
6811 : :
6812 : 19078 : case DW_RLE_base_address:
6813 [ - + ]: 19078 : 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 [ - + ]: 19078 : if ((uint64_t) (nexthdr - readp) < 8)
6822 : 0 : goto invalid_range;
6823 [ - + ]: 19078 : op1 = read_8ubyte_unaligned_inc (dbg, readp);
6824 : : }
6825 : 19078 : base = op1;
6826 : 19078 : fprintf (out, " 0x%" PRIx64 "\n", base);
6827 [ + - ]: 19078 : if (! print_unresolved_addresses)
6828 : : {
6829 : 19078 : fprintf (out, " ");
6830 : 19078 : print_dwarf_addr (dwflmod, address_size, base, base, out);
6831 : 19078 : 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 : 8442 : case DW_RLE_start_length:
6862 [ - + ]: 8442 : 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 [ - + ]: 8442 : if ((uint64_t) (nexthdr - readp) < 8)
6871 : 0 : goto invalid_range;
6872 [ - + ]: 8442 : op1 = read_8ubyte_unaligned_inc (dbg, readp);
6873 : : }
6874 [ - + ]: 8442 : if ((uint64_t) (nexthdr - readp) < 1)
6875 : 0 : goto invalid_range;
6876 : 8442 : get_uleb128 (op2, readp, nexthdr);
6877 : 8442 : fprintf (out, " 0x%" PRIx64 ", %" PRIx64 "\n", op1, op2);
6878 [ + + ]: 8442 : if (! print_unresolved_addresses)
6879 : : {
6880 : 8440 : op2 = op1 + op2;
6881 : 8440 : fprintf (out, " ");
6882 : 8440 : print_dwarf_addr (dwflmod, address_size, op1, op1, out);
6883 : 8440 : fprintf (out, "..\n ");
6884 : 8440 : print_dwarf_addr (dwflmod, address_size, op2 - 1, op2, out);
6885 : 8440 : fprintf (out, "\n");
6886 : : }
6887 : : break;
6888 : :
6889 : 0 : default:
6890 : 0 : goto invalid_range;
6891 : : }
6892 : : }
6893 : :
6894 : 2356 : next_table:
6895 [ - + ]: 2356 : 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 : 44288 : register_info (Ebl *ebl, unsigned int regno, const Ebl_Register_Location *loc,
7023 : : char name[REGNAMESZ], int *bits, int *type)
7024 : : {
7025 : 44288 : const char *set;
7026 : 44288 : const char *pfx;
7027 : 44288 : int ignore;
7028 [ + + + + ]: 129472 : ssize_t n = ebl_register_info (ebl, regno, name, REGNAMESZ, &pfx, &set,
7029 : : bits ?: &ignore, type ?: &ignore);
7030 [ - + ]: 44288 : 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 [ + + - + ]: 44288 : if (bits != NULL && *bits <= 0)
7045 [ # # ]: 0 : *bits = loc != NULL ? loc->bits : 0;
7046 [ + + - + ]: 44288 : if (type != NULL && *type == DW_ATE_void)
7047 : 0 : *type = DW_ATE_unsigned;
7048 : :
7049 : : }
7050 : 44288 : 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 : 42592 : regname (Ebl *ebl, unsigned int regno, char *regnamebuf)
7114 : : {
7115 : 42592 : register_info (ebl, regno, NULL, regnamebuf, NULL, NULL);
7116 : :
7117 : 42592 : return regnamebuf;
7118 : : }
7119 : :
7120 : : static void
7121 : 27512 : 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 : 27512 : char regnamebuf[REGNAMESZ];
7130 : :
7131 : 27512 : fputs ("\n Program:\n", out);
7132 : 27512 : Dwarf_Word pc = vma_base;
7133 [ + + ]: 401382 : while (readp < endp)
7134 : : {
7135 : 373870 : unsigned int opcode = *readp++;
7136 : :
7137 [ + + ]: 373870 : if (opcode < DW_CFA_advance_loc)
7138 : : /* Extended opcode. */
7139 [ + - + + : 222986 : switch (opcode)
+ + - + -
- + + + +
+ + - + -
- - - - -
+ - - ]
7140 : : {
7141 : 76692 : uint64_t op1;
7142 : 76692 : int64_t sop1;
7143 : 76692 : uint64_t op2;
7144 : 76692 : int64_t sop2;
7145 : :
7146 : 76692 : case DW_CFA_nop:
7147 : 76692 : fputs (" nop\n", out);
7148 : 299678 : 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 : 7716 : case DW_CFA_advance_loc1:
7157 [ - + ]: 7716 : if ((uint64_t) (endp - readp) < 1)
7158 : 0 : goto invalid;
7159 : 15432 : fprintf (out, " advance_loc1 %u to %#" PRIx64 "\n",
7160 : 7716 : *readp, pc += *readp * code_align);
7161 : 7716 : ++readp;
7162 : 7716 : break;
7163 : 3244 : case DW_CFA_advance_loc2:
7164 [ - + ]: 3244 : if ((uint64_t) (endp - readp) < 2)
7165 : 0 : goto invalid;
7166 [ - + ]: 3244 : op1 = read_2ubyte_unaligned_inc (dbg, readp);
7167 : 3244 : fprintf (out, " advance_loc2 %" PRIu64 " to %#" PRIx64 "\n",
7168 : 3244 : op1, pc += op1 * code_align);
7169 : 3244 : 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 : 13440 : case DW_CFA_remember_state:
7222 : 13440 : fputs (" remember_state\n", out);
7223 : 13440 : break;
7224 : 13440 : case DW_CFA_restore_state:
7225 : 13440 : fputs (" restore_state\n", out);
7226 : 13440 : 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 : 107606 : case DW_CFA_def_cfa_offset:
7246 [ - + ]: 107606 : if ((uint64_t) (endp - readp) < 1)
7247 : 0 : goto invalid;
7248 : 107606 : get_uleb128 (op1, readp, endp);
7249 : 107606 : fprintf (out, " def_cfa_offset %" PRIu64 "\n", op1);
7250 : 107606 : 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 [ + + ]: 150884 : else if (opcode < DW_CFA_offset)
7371 : 108978 : fprintf (out, " advance_loc %u to %#" PRIx64 "\n",
7372 : 108978 : opcode & 0x3f, pc += (opcode & 0x3f) * code_align);
7373 [ + + ]: 41906 : else if (opcode < DW_CFA_restore)
7374 : : {
7375 : 38056 : uint64_t offset;
7376 [ - + ]: 38056 : if ((uint64_t) (endp - readp) < 1)
7377 : 0 : goto invalid;
7378 : 38056 : get_uleb128 (offset, readp, endp);
7379 : 38056 : 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 : 3850 : fprintf (out, " restore r%u (%s)\n",
7385 : : opcode & 0x3f, regname (ebl, opcode & 0x3f, regnamebuf));
7386 : : }
7387 : : }
7388 : :
7389 : :
7390 : : static unsigned int
7391 : 27202 : encoded_ptr_size (int encoding, unsigned int ptr_size)
7392 : : {
7393 [ - + - + ]: 27202 : 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 [ + + ]: 27848 : while (readp < dataend)
7570 : : {
7571 [ - + ]: 27652 : 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 : 27652 : ptrdiff_t offset = readp - (unsigned char *) data->d_buf;
7582 : :
7583 [ + + ]: 27652 : Dwarf_Word unit_length = read_4ubyte_unaligned_inc (dbg, readp);
7584 : 27652 : unsigned int length = 4;
7585 [ - + ]: 27652 : 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 [ + + ]: 27652 : if (unlikely (unit_length == 0))
7595 : : {
7596 : 140 : fprintf (out, _("\n [%6tx] Zero terminator\n"), offset);
7597 : 140 : continue;
7598 : : }
7599 : :
7600 : 27512 : Dwarf_Word maxsize = dataend - readp;
7601 [ - + ]: 27512 : if (unlikely (unit_length > maxsize))
7602 : 0 : goto invalid_data;
7603 : :
7604 [ + + ]: 27512 : unsigned int ptr_size = ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 4 : 8;
7605 : :
7606 : 27512 : ptrdiff_t start = readp - (unsigned char *) data->d_buf;
7607 : 27512 : const unsigned char *const cieend = readp + unit_length;
7608 [ - + ]: 27512 : if (unlikely (cieend > dataend))
7609 : 0 : goto invalid_data;
7610 : :
7611 : 27512 : Dwarf_Off cie_id;
7612 [ + - ]: 27512 : if (length == 4)
7613 : : {
7614 [ - + ]: 27512 : if (unlikely (cieend - readp < 4))
7615 : 0 : goto invalid_data;
7616 [ + + ]: 27512 : cie_id = read_4ubyte_unaligned_inc (dbg, readp);
7617 [ + + ]: 27512 : 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 : 54940 : uint_fast8_t version = 2;
7628 : 27428 : unsigned int code_alignment_factor;
7629 : 27428 : int data_alignment_factor;
7630 : 54940 : unsigned int fde_encoding = 0;
7631 : 54940 : unsigned int lsda_encoding = 0;
7632 : 54940 : Dwarf_Word initial_location = 0;
7633 : 54940 : Dwarf_Word vma_base = 0;
7634 : :
7635 [ + + + + ]: 27604 : 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 [ + - ]: 27202 : while (cie != NULL)
7778 [ + + - + ]: 54404 : if (is_eh_frame
7779 : 27110 : ? ((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 : 27202 : 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 : 27202 : fde_encoding = cie->fde_encoding;
7792 : 27202 : lsda_encoding = cie->lsda_encoding;
7793 : 27202 : ptr_size = encoded_ptr_size (fde_encoding, cie->address_size);
7794 : 27202 : code_alignment_factor = cie->code_alignment_factor;
7795 : 27202 : data_alignment_factor = cie->data_alignment_factor;
7796 : :
7797 : 27202 : const unsigned char *base = readp;
7798 : : // XXX There are sometimes relocations for this value
7799 [ - + + + : 27202 : initial_location = read_addr_unaligned_inc (ptr_size, dbg, readp);
- + + + ]
7800 : 54404 : Dwarf_Word address_range
7801 [ + + - + : 27202 : = 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 : 27202 : Dwarf_Addr pc_start = initial_location;
7809 [ + + ]: 27202 : if (ptr_size == 4)
7810 : 27122 : pc_start = (uint64_t) (int32_t) pc_start;
7811 [ + + ]: 27202 : if ((fde_encoding & 0x70) == DW_EH_PE_pcrel)
7812 : 27098 : pc_start += ((uint64_t) shdr->sh_addr
7813 : 27098 : + (base - (const unsigned char *) data->d_buf)
7814 : 27098 : - bias);
7815 : :
7816 : 27202 : 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 : 27202 : print_dwarf_addr (dwflmod, cie->address_size,
7822 : : pc_start, initial_location, out);
7823 [ + + ]: 27202 : if ((fde_encoding & 0x70) == DW_EH_PE_pcrel)
7824 : : {
7825 : 54196 : vma_base = (((uint64_t) shdr->sh_offset
7826 : 27098 : + (base - (const unsigned char *) data->d_buf)
7827 : 27098 : + (uint64_t) initial_location)
7828 : : & (ptr_size == 4
7829 : : ? UINT64_C (0xffffffff)
7830 [ - + ]: 27098 : : UINT64_C (0xffffffffffffffff)));
7831 : 27098 : fprintf (out, _(" (offset: %#" PRIx64 ")"),
7832 : : (uint64_t) vma_base);
7833 : : }
7834 : :
7835 : 27202 : fprintf (out, "\n address_range: %#" PRIx64,
7836 : : (uint64_t) address_range);
7837 [ + + ]: 27202 : if ((fde_encoding & 0x70) == DW_EH_PE_pcrel)
7838 : 27098 : fprintf (out, _(" (end offset: %#" PRIx64 ")"),
7839 : 27098 : ((uint64_t) vma_base + (uint64_t) address_range)
7840 : : & (ptr_size == 4
7841 : : ? UINT64_C (0xffffffff)
7842 [ - + ]: 27098 : : UINT64_C (0xffffffffffffffff)));
7843 : 27202 : fputc ('\n', out);
7844 : :
7845 [ + + ]: 27202 : if (cie->augmentation[0] == 'z')
7846 : : {
7847 : 27098 : unsigned int augmentationlen;
7848 [ - + ]: 27098 : if (cieend - readp < 1)
7849 : 0 : goto invalid_data;
7850 : 27098 : get_uleb128 (augmentationlen, readp, cieend);
7851 : :
7852 [ - + ]: 27098 : 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 [ - + ]: 27098 : 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 : 27098 : readp += augmentationlen;
7891 : : }
7892 : : }
7893 : :
7894 : : /* Handle the initialization instructions. */
7895 [ - + ]: 27512 : 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 : 27512 : 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 : 27512 : 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 : 410040 : die_type_sign_bytes (Dwarf_Die *die, bool *is_signed, int *bytes)
7912 : : {
7913 : 410040 : Dwarf_Attribute attr;
7914 : 410040 : Dwarf_Die type;
7915 : :
7916 : 410040 : *bytes = 0;
7917 : 410040 : *is_signed = false;
7918 : :
7919 [ + + ]: 410040 : if (dwarf_peel_type (dwarf_formref_die (dwarf_attr_integrate (die,
7920 : : DW_AT_type,
7921 : : &attr), &type),
7922 : : &type) == 0)
7923 : : {
7924 : 1090 : Dwarf_Word val;
7925 : 1090 : *is_signed = (dwarf_formudata (dwarf_attr (&type, DW_AT_encoding,
7926 : : &attr), &val) == 0
7927 [ + + + + ]: 1090 : && (val == DW_ATE_signed || val == DW_ATE_signed_char));
7928 : :
7929 [ + - ]: 1090 : if (dwarf_aggregate_size (&type, &val) == 0)
7930 : 1090 : *bytes = val;
7931 : : }
7932 : 410040 : }
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 : 9341712 : attr_callback (Dwarf_Attribute *attrp, void *arg)
7952 : : {
7953 : 9341712 : struct attrcb_args *cbargs = (struct attrcb_args *) arg;
7954 : 9341712 : const int level = cbargs->level;
7955 : 9341712 : Dwarf_Die *die = &cbargs->dies[level];
7956 : 9341712 : bool is_split = cbargs->is_split;
7957 : 9341712 : FILE *out = cbargs->out;
7958 : :
7959 [ + - ]: 9341712 : unsigned int attr = dwarf_whatattr (attrp);
7960 [ - + ]: 9341712 : 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 [ - + ]: 9341712 : unsigned int form = dwarf_whatform (attrp);
7970 [ - + ]: 9341712 : 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 [ + + + + : 9341712 : switch (form)
+ + + +
- ]
7980 : : {
7981 : 115436 : 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 [ + + ]: 115436 : if (!cbargs->silent)
7989 : : {
7990 : 114894 : Dwarf_Addr addr;
7991 [ - + ]: 114894 : 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 [ + + ]: 114894 : if (form != DW_FORM_addr )
8006 : : {
8007 : 42 : Dwarf_Word word;
8008 [ - + ]: 42 : if (dwarf_formudata (attrp, &word) != 0)
8009 : 0 : goto attrval_out;
8010 : 42 : fprintf (out, " %*s%-20s (%s) [%" PRIx64 "] ",
8011 : : (int) (level * 2), "", dwarf_attr_name (attr),
8012 : : dwarf_form_name (form), word);
8013 : : }
8014 : : else
8015 : 114852 : fprintf (out, " %*s%-20s (%s) ",
8016 : : (int) (level * 2), "", dwarf_attr_name (attr),
8017 : : dwarf_form_name (form));
8018 : 114894 : print_dwarf_addr (cbargs->dwflmod, cbargs->addrsize, addr, addr, out);
8019 : 114894 : fprintf (out, "\n");
8020 : : }
8021 : 8403490 : break;
8022 : :
8023 : 1545128 : case DW_FORM_indirect:
8024 : : case DW_FORM_strp:
8025 : : case DW_FORM_strp_sup:
8026 : : case DW_FORM_line_strp:
8027 : : case DW_FORM_strx:
8028 : : case DW_FORM_strx1:
8029 : : case DW_FORM_strx2:
8030 : : case DW_FORM_strx3:
8031 : : case DW_FORM_strx4:
8032 : : case DW_FORM_string:
8033 : : case DW_FORM_GNU_strp_alt:
8034 : : case DW_FORM_GNU_str_index:
8035 [ + + ]: 1545128 : if (cbargs->silent)
8036 : : break;
8037 : 1543618 : const char *str = dwarf_formstring (attrp);
8038 [ - + ]: 1543618 : if (unlikely (str == NULL))
8039 : 0 : goto attrval_out;
8040 : 1543618 : fprintf (out, " %*s%-20s (%s) \"%s\"\n",
8041 : : (int) (level * 2), "", dwarf_attr_name (attr),
8042 : : dwarf_form_name (form), str);
8043 : 1543618 : break;
8044 : :
8045 : 2035246 : case DW_FORM_ref_addr:
8046 : : case DW_FORM_ref_udata:
8047 : : case DW_FORM_ref8:
8048 : : case DW_FORM_ref4:
8049 : : case DW_FORM_ref2:
8050 : : case DW_FORM_ref1:
8051 : : case DW_FORM_GNU_ref_alt:
8052 : : case DW_FORM_ref_sup4:
8053 : : case DW_FORM_ref_sup8:
8054 [ + + ]: 2035246 : if (cbargs->silent)
8055 : : break;
8056 : 2033666 : Dwarf_Die ref;
8057 [ - + ]: 2033666 : if (unlikely (dwarf_formref_die (attrp, &ref) == NULL))
8058 : 0 : goto attrval_out;
8059 : :
8060 : 2033666 : fprintf (out, " %*s%-20s (%s) ",
8061 : : (int) (level * 2), "", dwarf_attr_name (attr),
8062 : : dwarf_form_name (form));
8063 [ + + ]: 2033666 : if (is_split)
8064 : 68 : fprintf (out, "{%6" PRIxMAX "}\n", (uintmax_t) dwarf_dieoffset (&ref));
8065 : : else
8066 : 2033598 : fprintf (out, "[%6" PRIxMAX "]\n", (uintmax_t) dwarf_dieoffset (&ref));
8067 : : break;
8068 : :
8069 : 8 : case DW_FORM_ref_sig8:
8070 [ + - ]: 8 : if (cbargs->silent)
8071 : : break;
8072 : 8 : fprintf (out, " %*s%-20s (%s) {%6" PRIx64 "}\n",
8073 : : (int) (level * 2), "", dwarf_attr_name (attr),
8074 : : dwarf_form_name (form),
8075 [ - + ]: 8 : (uint64_t) read_8ubyte_unaligned (attrp->cu->dbg, attrp->valp));
8076 : 8 : break;
8077 : :
8078 : 5188928 : case DW_FORM_sec_offset:
8079 : : case DW_FORM_rnglistx:
8080 : : case DW_FORM_loclistx:
8081 : : case DW_FORM_implicit_const:
8082 : : case DW_FORM_udata:
8083 : : case DW_FORM_sdata:
8084 : : case DW_FORM_data8: /* Note no data16 here, we see that as block. */
8085 : : case DW_FORM_data4:
8086 : : case DW_FORM_data2:
8087 : 5188928 : case DW_FORM_data1:;
8088 : 5188928 : Dwarf_Word num;
8089 [ - + ]: 5188928 : if (unlikely (dwarf_formudata (attrp, &num) != 0))
8090 : 0 : goto attrval_out;
8091 : :
8092 : 5188928 : const char *valuestr = NULL;
8093 : 5188928 : bool as_hex_id = false;
8094 [ + + + + : 5188928 : switch (attr)
+ + + + +
+ - - - -
- - + - +
+ + ]
8095 : : {
8096 : : /* This case can take either a constant or a loclistptr. */
8097 : 643086 : case DW_AT_data_member_location:
8098 [ + - ]: 643086 : if (form != DW_FORM_sec_offset
8099 [ + + ]: 643086 : && (cbargs->version >= 4
8100 [ + - ]: 47612 : || (form != DW_FORM_data4 && form != DW_FORM_data8)))
8101 : : {
8102 [ + - ]: 643086 : if (!cbargs->silent)
8103 : 643086 : fprintf (out, " %*s%-20s (%s) %" PRIuMAX "\n",
8104 : : (int) (level * 2), "", dwarf_attr_name (attr),
8105 : : dwarf_form_name (form), (uintmax_t) num);
8106 : 643086 : return DWARF_CB_OK;
8107 : : }
8108 : 253902 : FALLTHROUGH;
8109 : :
8110 : : /* These cases always take a loclist[ptr] and no constant. */
8111 : : case DW_AT_location:
8112 : : case DW_AT_data_location:
8113 : : case DW_AT_vtable_elem_location:
8114 : : case DW_AT_string_length:
8115 : : case DW_AT_use_location:
8116 : : case DW_AT_frame_base:
8117 : : case DW_AT_return_addr:
8118 : : case DW_AT_static_link:
8119 : : case DW_AT_segment:
8120 : : case DW_AT_GNU_call_site_value:
8121 : : case DW_AT_GNU_call_site_data_value:
8122 : : case DW_AT_GNU_call_site_target:
8123 : : case DW_AT_GNU_call_site_target_clobbered:
8124 : : case DW_AT_GNU_locviews:
8125 : : {
8126 : 253902 : bool nlpt;
8127 [ + + ]: 253902 : if (cbargs->cu->version < 5)
8128 : : {
8129 [ + + ]: 324 : if (! cbargs->is_split)
8130 : : {
8131 : 284 : nlpt = notice_listptr (section_loc, &known_locsptr,
8132 : 284 : cbargs->addrsize,
8133 : 284 : cbargs->offset_size,
8134 : : cbargs->cu, num, attr);
8135 : : }
8136 : : else
8137 : : nlpt = true;
8138 : : }
8139 : : else
8140 : : {
8141 : : /* Only register for a real section offset. Otherwise
8142 : : it is a DW_FORM_loclistx which is just an index
8143 : : number and we should already have registered the
8144 : : section offset for the index when we saw the
8145 : : DW_AT_loclists_base CU attribute. */
8146 [ + + ]: 253578 : if (form == DW_FORM_sec_offset)
8147 : 253500 : nlpt = notice_listptr (section_loc, &known_loclistsptr,
8148 : 253500 : cbargs->addrsize, cbargs->offset_size,
8149 : : cbargs->cu, num, attr);
8150 : : else
8151 : : nlpt = true;
8152 : :
8153 : : }
8154 : :
8155 [ + + ]: 253902 : if (!cbargs->silent)
8156 : : {
8157 [ + + + + ]: 253536 : if (cbargs->cu->version < 5 || form == DW_FORM_sec_offset)
8158 [ - + ]: 253522 : fprintf (out, " %*s%-20s (%s) location list [%6"
8159 : : PRIxMAX "]%s\n",
8160 : : (int) (level * 2), "", dwarf_attr_name (attr),
8161 : : dwarf_form_name (form), (uintmax_t) num,
8162 : : nlpt ? "" : " <WARNING offset too big>");
8163 : : else
8164 : 14 : fprintf (out, " %*s%-20s (%s) location index [%6"
8165 : : PRIxMAX "]\n",
8166 : : (int) (level * 2), "", dwarf_attr_name (attr),
8167 : : dwarf_form_name (form), (uintmax_t) num);
8168 : : }
8169 : : }
8170 : : return DWARF_CB_OK;
8171 : :
8172 : 10 : case DW_AT_loclists_base:
8173 : : {
8174 : 20 : bool nlpt = notice_listptr (section_loc, &known_loclistsptr,
8175 : 10 : cbargs->addrsize, cbargs->offset_size,
8176 : : cbargs->cu, num, attr);
8177 : :
8178 [ + + ]: 10 : if (!cbargs->silent)
8179 [ - + ]: 2 : fprintf (out, " %*s%-20s (%s) location list [%6" PRIxMAX "]%s\n",
8180 : : (int) (level * 2), "", dwarf_attr_name (attr),
8181 : : dwarf_form_name (form), (uintmax_t) num,
8182 : : nlpt ? "" : " <WARNING offset too big>");
8183 : : }
8184 : : return DWARF_CB_OK;
8185 : :
8186 : 41172 : case DW_AT_ranges:
8187 : : case DW_AT_start_scope:
8188 : : {
8189 : 41172 : bool nlpt;
8190 [ + + ]: 41172 : if (cbargs->cu->version < 5)
8191 : 106 : nlpt = notice_listptr (section_ranges, &known_rangelistptr,
8192 : 106 : cbargs->addrsize, cbargs->offset_size,
8193 : : cbargs->cu, num, attr);
8194 : : else
8195 : : {
8196 : : /* Only register for a real section offset. Otherwise
8197 : : it is a DW_FORM_rangelistx which is just an index
8198 : : number and we should already have registered the
8199 : : section offset for the index when we saw the
8200 : : DW_AT_rnglists_base CU attribute. */
8201 [ + + ]: 41066 : if (form == DW_FORM_sec_offset)
8202 : 41054 : nlpt = notice_listptr (section_ranges, &known_rnglistptr,
8203 : 41054 : cbargs->addrsize, cbargs->offset_size,
8204 : : cbargs->cu, num, attr);
8205 : : else
8206 : : nlpt = true;
8207 : : }
8208 : :
8209 [ + + ]: 41172 : if (!cbargs->silent)
8210 : : {
8211 [ + + + + ]: 41086 : if (cbargs->cu->version < 5 || form == DW_FORM_sec_offset)
8212 [ - + ]: 41082 : fprintf (out, " %*s%-20s (%s) range list [%6"
8213 : : PRIxMAX "]%s\n",
8214 : : (int) (level * 2), "", dwarf_attr_name (attr),
8215 : : dwarf_form_name (form), (uintmax_t) num,
8216 : : nlpt ? "" : " <WARNING offset too big>");
8217 : : else
8218 : 4 : fprintf (out, " %*s%-20s (%s) range index [%6"
8219 : : PRIxMAX "]\n",
8220 : : (int) (level * 2), "", dwarf_attr_name (attr),
8221 : : dwarf_form_name (form), (uintmax_t) num);
8222 : : }
8223 : : }
8224 : : return DWARF_CB_OK;
8225 : :
8226 : 8 : case DW_AT_rnglists_base:
8227 : : {
8228 : 16 : bool nlpt = notice_listptr (section_ranges, &known_rnglistptr,
8229 : 8 : cbargs->addrsize, cbargs->offset_size,
8230 : : cbargs->cu, num, attr);
8231 [ + + ]: 8 : if (!cbargs->silent)
8232 [ - + ]: 4 : fprintf (out, " %*s%-20s (%s) range list [%6"
8233 : : PRIxMAX "]%s\n",
8234 : : (int) (level * 2), "", dwarf_attr_name (attr),
8235 : : dwarf_form_name (form), (uintmax_t) num,
8236 : : nlpt ? "" : " <WARNING offset too big>");
8237 : : }
8238 : : return DWARF_CB_OK;
8239 : :
8240 : 36 : case DW_AT_addr_base:
8241 : : case DW_AT_GNU_addr_base:
8242 : : {
8243 : 72 : bool addrbase = notice_listptr (section_addr, &known_addrbases,
8244 : 36 : cbargs->addrsize,
8245 : 36 : cbargs->offset_size,
8246 : : cbargs->cu, num, attr);
8247 [ + + ]: 36 : if (!cbargs->silent)
8248 [ - + ]: 20 : fprintf (out, " %*s%-20s (%s) address base [%6"
8249 : : PRIxMAX "]%s\n",
8250 : : (int) (level * 2), "", dwarf_attr_name (attr),
8251 : : dwarf_form_name (form), (uintmax_t) num,
8252 : : addrbase ? "" : " <WARNING offset too big>");
8253 : : }
8254 : : return DWARF_CB_OK;
8255 : :
8256 : 8 : case DW_AT_str_offsets_base:
8257 : : {
8258 : 16 : bool stroffbase = notice_listptr (section_str, &known_stroffbases,
8259 : 8 : cbargs->addrsize,
8260 : 8 : cbargs->offset_size,
8261 : : cbargs->cu, num, attr);
8262 [ + - ]: 8 : if (!cbargs->silent)
8263 [ - + ]: 8 : fprintf (out, " %*s%-20s (%s) str offsets base [%6"
8264 : : PRIxMAX "]%s\n",
8265 : : (int) (level * 2), "", dwarf_attr_name (attr),
8266 : : dwarf_form_name (form), (uintmax_t) num,
8267 : : stroffbase ? "" : " <WARNING offset too big>");
8268 : : }
8269 : : return DWARF_CB_OK;
8270 : :
8271 : 4280 : case DW_AT_language:
8272 : 4280 : valuestr = dwarf_lang_name (num);
8273 : 4280 : break;
8274 : 8 : case DW_AT_language_name:
8275 : 8 : valuestr = dwarf_lname_name (num);
8276 : 8 : break;
8277 : 68362 : case DW_AT_encoding:
8278 : 68362 : valuestr = dwarf_encoding_name (num);
8279 : 68362 : break;
8280 : 0 : case DW_AT_accessibility:
8281 : 0 : valuestr = dwarf_access_name (num);
8282 : 0 : break;
8283 : 0 : case DW_AT_defaulted:
8284 : 0 : valuestr = dwarf_defaulted_name (num);
8285 : 0 : break;
8286 : 0 : case DW_AT_visibility:
8287 : 0 : valuestr = dwarf_visibility_name (num);
8288 : 0 : break;
8289 : 0 : case DW_AT_virtuality:
8290 : 0 : valuestr = dwarf_virtuality_name (num);
8291 : 0 : break;
8292 : 0 : case DW_AT_identifier_case:
8293 : 0 : valuestr = dwarf_identifier_case_name (num);
8294 : 0 : break;
8295 : 0 : case DW_AT_calling_convention:
8296 : 0 : valuestr = dwarf_calling_convention_name (num);
8297 : 0 : break;
8298 : 8344 : case DW_AT_inline:
8299 : 8344 : valuestr = dwarf_inline_name (num);
8300 : 8344 : break;
8301 : 0 : case DW_AT_ordering:
8302 : 0 : valuestr = dwarf_ordering_name (num);
8303 : 0 : break;
8304 : 1135376 : case DW_AT_decl_file:
8305 : : case DW_AT_call_file:
8306 : : {
8307 [ + + ]: 1135376 : if (cbargs->silent)
8308 : : break;
8309 : :
8310 : : /* Try to get the actual file, the current interface only
8311 : : gives us full paths, but we only want to show the file
8312 : : name for now. */
8313 : 1134528 : Dwarf_Die cudie;
8314 [ + - ]: 1134528 : if (dwarf_cu_die (cbargs->cu, &cudie,
8315 : : NULL, NULL, NULL, NULL, NULL, NULL) != NULL)
8316 : : {
8317 : 1134528 : Dwarf_Files *files;
8318 : 1134528 : size_t nfiles;
8319 [ + - ]: 1134528 : if (dwarf_getsrcfiles (&cudie, &files, &nfiles) == 0)
8320 : : {
8321 : 1134528 : valuestr = dwarf_filesrc (files, num, NULL, NULL);
8322 [ + - ]: 1134528 : if (valuestr != NULL)
8323 : : {
8324 : 1134528 : const char *filename = strrchr (valuestr, '/');
8325 [ - + ]: 1134528 : if (filename != NULL)
8326 : 1134528 : valuestr = filename + 1;
8327 : : }
8328 : : else
8329 : 0 : error (0, 0, _("invalid file (%" PRId64 "): %s"),
8330 : : num, dwarf_errmsg (-1));
8331 : : }
8332 : : else
8333 : 0 : error (0, 0, _("no srcfiles for CU [%" PRIx64 "]"),
8334 : : dwarf_dieoffset (&cudie));
8335 : : }
8336 : : else
8337 : 0 : error (0, 0, _("couldn't get DWARF CU: %s"),
8338 : : dwarf_errmsg (-1));
8339 : 1134528 : if (valuestr == NULL)
8340 : : valuestr = "???";
8341 : : }
8342 : 1134528 : break;
8343 : 26 : case DW_AT_GNU_dwo_id:
8344 : 26 : as_hex_id = true;
8345 : 26 : break;
8346 : :
8347 : : default:
8348 : : /* Nothing. */
8349 : : break;
8350 : : }
8351 : :
8352 [ + + ]: 4250706 : if (cbargs->silent)
8353 : : break;
8354 : :
8355 : : /* When highpc is in constant form it is relative to lowpc.
8356 : : In that case also show the address. */
8357 : 4246996 : Dwarf_Addr highpc;
8358 [ + + + - ]: 4246996 : if (attr == DW_AT_high_pc && dwarf_highpc (die, &highpc) == 0)
8359 : : {
8360 : 27194 : fprintf (out, " %*s%-20s (%s) %" PRIuMAX " (",
8361 : : (int) (level * 2), "", dwarf_attr_name (attr),
8362 : : dwarf_form_name (form), (uintmax_t) num);
8363 : 27194 : print_dwarf_addr (cbargs->dwflmod, cbargs->addrsize,
8364 : : highpc, highpc, out);
8365 : 27194 : fprintf (out, ")\n");
8366 : : }
8367 : : else
8368 : : {
8369 [ + + ]: 4219802 : if (as_hex_id)
8370 : : {
8371 : 4 : fprintf (out, " %*s%-20s (%s) 0x%.16" PRIx64 "\n",
8372 : : (int) (level * 2), "", dwarf_attr_name (attr),
8373 : : dwarf_form_name (form), num);
8374 : : }
8375 : : else
8376 : : {
8377 : 4219798 : Dwarf_Sword snum = 0;
8378 : 4219798 : bool is_signed;
8379 : 4219798 : int bytes = 0;
8380 [ + + ]: 4219798 : if (attr == DW_AT_const_value)
8381 : 410032 : die_type_sign_bytes (die, &is_signed, &bytes);
8382 : : else
8383 : 3809766 : is_signed = (form == DW_FORM_sdata
8384 : 3809766 : || form == DW_FORM_implicit_const);
8385 : :
8386 [ + + ]: 4219798 : if (is_signed)
8387 [ - + ]: 448760 : if (unlikely (dwarf_formsdata (attrp, &snum) != 0))
8388 : 0 : goto attrval_out;
8389 : :
8390 [ + + ]: 4219798 : if (valuestr == NULL)
8391 : : {
8392 : 3004886 : fprintf (out, " %*s%-20s (%s) ",
8393 : : (int) (level * 2), "", dwarf_attr_name (attr),
8394 : : dwarf_form_name (form));
8395 : : }
8396 : : else
8397 : : {
8398 : 1214912 : fprintf (out, " %*s%-20s (%s) %s (",
8399 : : (int) (level * 2), "", dwarf_attr_name (attr),
8400 : : dwarf_form_name (form), valuestr);
8401 : : }
8402 : :
8403 [ + + + + : 4219798 : switch (bytes)
+ ]
8404 : : {
8405 : 10 : case 1:
8406 [ + + ]: 10 : if (is_signed)
8407 : 2 : fprintf (out, "%" PRId8, (int8_t) snum);
8408 : : else
8409 : 8 : fprintf (out, "%" PRIu8, (uint8_t) num);
8410 : : break;
8411 : :
8412 : 4 : case 2:
8413 [ + + ]: 4 : if (is_signed)
8414 : 2 : fprintf (out, "%" PRId16, (int16_t) snum);
8415 : : else
8416 : 2 : fprintf (out, "%" PRIu16, (uint16_t) num);
8417 : : break;
8418 : :
8419 : 278 : case 4:
8420 [ + + ]: 278 : if (is_signed)
8421 : 252 : fprintf (out, "%" PRId32, (int32_t) snum);
8422 : : else
8423 : 26 : fprintf (out, "%" PRIu32, (uint32_t) num);
8424 : : break;
8425 : :
8426 : 790 : case 8:
8427 [ + + ]: 790 : if (is_signed)
8428 : 2 : fprintf (out, "%" PRId64, (int64_t) snum);
8429 : : else
8430 : 788 : fprintf (out, "%" PRIu64, (uint64_t) num);
8431 : : break;
8432 : :
8433 : 4218716 : default:
8434 [ + + ]: 4218716 : if (is_signed)
8435 : 448502 : fprintf (out, "%" PRIdMAX, (intmax_t) snum);
8436 : : else
8437 : 3770214 : fprintf (out, "%" PRIuMAX, (uintmax_t) num);
8438 : : break;
8439 : : }
8440 : :
8441 : : /* Make clear if we switched from a signed encoding to
8442 : : an unsigned value. */
8443 [ + + ]: 4219798 : if (attr == DW_AT_const_value
8444 [ + + ]: 410032 : && (form == DW_FORM_sdata || form == DW_FORM_implicit_const)
8445 [ + + ]: 2740 : && !is_signed)
8446 : 2720 : fprintf (out, " (%" PRIdMAX ")", (intmax_t) num);
8447 : :
8448 [ + + ]: 4219798 : if (valuestr == NULL)
8449 : 3004886 : fprintf (out, "\n");
8450 : : else
8451 : 1214912 : fprintf (out, ")\n");
8452 : : }
8453 : : }
8454 : : break;
8455 : :
8456 : 18244 : case DW_FORM_flag:
8457 [ + + ]: 18244 : if (cbargs->silent)
8458 : : break;
8459 : 18096 : bool flag;
8460 [ - + ]: 18096 : if (unlikely (dwarf_formflag (attrp, &flag) != 0))
8461 : 0 : goto attrval_out;
8462 : :
8463 : 18096 : fprintf (out, " %*s%-20s (%s) %s\n",
8464 : : (int) (level * 2), "", dwarf_attr_name (attr),
8465 [ + - ]: 18096 : dwarf_form_name (form), flag ? yes_str : no_str);
8466 : 18096 : break;
8467 : :
8468 : 186434 : case DW_FORM_flag_present:
8469 [ + + ]: 186434 : if (cbargs->silent)
8470 : : break;
8471 : 185880 : fprintf (out, " %*s%-20s (%s) %s\n",
8472 : : (int) (level * 2), "", dwarf_attr_name (attr),
8473 : : dwarf_form_name (form), yes_str);
8474 : 185880 : break;
8475 : :
8476 : 252288 : case DW_FORM_exprloc:
8477 : : case DW_FORM_block4:
8478 : : case DW_FORM_block2:
8479 : : case DW_FORM_block1:
8480 : : case DW_FORM_block:
8481 : : case DW_FORM_data16: /* DWARF5 calls this a constant class. */
8482 [ + + ]: 252288 : if (cbargs->silent)
8483 : : break;
8484 : 252026 : Dwarf_Block block;
8485 [ - + ]: 252026 : if (unlikely (dwarf_formblock (attrp, &block) != 0))
8486 : 0 : goto attrval_out;
8487 : :
8488 : 252026 : fprintf (out, " %*s%-20s (%s) ",
8489 : : (int) (level * 2), "", dwarf_attr_name (attr),
8490 : : dwarf_form_name (form));
8491 : :
8492 [ + + + ]: 252026 : switch (attr)
8493 : : {
8494 : 108234 : default:
8495 [ - + ]: 108234 : if (form != DW_FORM_exprloc)
8496 : : {
8497 : 0 : print_block (block.length, block.data, out);
8498 : 0 : break;
8499 : : }
8500 : 143784 : FALLTHROUGH;
8501 : :
8502 : : case DW_AT_location:
8503 : : case DW_AT_data_location:
8504 : : case DW_AT_data_member_location:
8505 : : case DW_AT_vtable_elem_location:
8506 : : case DW_AT_string_length:
8507 : : case DW_AT_use_location:
8508 : : case DW_AT_frame_base:
8509 : : case DW_AT_return_addr:
8510 : : case DW_AT_static_link:
8511 : : case DW_AT_allocated:
8512 : : case DW_AT_associated:
8513 : : case DW_AT_bit_size:
8514 : : case DW_AT_bit_offset:
8515 : : case DW_AT_bit_stride:
8516 : : case DW_AT_byte_size:
8517 : : case DW_AT_byte_stride:
8518 : : case DW_AT_count:
8519 : : case DW_AT_lower_bound:
8520 : : case DW_AT_upper_bound:
8521 : : case DW_AT_GNU_call_site_value:
8522 : : case DW_AT_GNU_call_site_data_value:
8523 : : case DW_AT_GNU_call_site_target:
8524 : : case DW_AT_GNU_call_site_target_clobbered:
8525 [ + + ]: 143784 : if (form == DW_FORM_exprloc
8526 [ + - ]: 140 : || (form != DW_FORM_data16
8527 [ + - ]: 140 : && attrp->cu->version < 4)) /* blocks were expressions. */
8528 : : {
8529 : 252018 : fputc ('\n', out);
8530 : 252018 : print_ops (cbargs->dwflmod, cbargs->dbg,
8531 : 252018 : 12 + level * 2, 12 + level * 2,
8532 : : cbargs->version, cbargs->addrsize, cbargs->offset_size,
8533 : 252018 : attrp->cu, block.length, block.data, out);
8534 : : }
8535 : : else
8536 : 0 : print_block (block.length, block.data, out);
8537 : : break;
8538 : :
8539 : 8 : case DW_AT_discr_list:
8540 [ - + ]: 8 : if (block.length == 0)
8541 : 0 : fputs ("<default>\n", out);
8542 [ + - ]: 8 : else if (form != DW_FORM_data16)
8543 : : {
8544 : 8 : const unsigned char *readp = block.data;
8545 : 8 : const unsigned char *readendp = readp + block.length;
8546 : :
8547 : : /* See if we are dealing with a signed or unsigned
8548 : : values. If the parent of this variant DIE is a
8549 : : variant_part then it will either have a discriminant
8550 : : which points to the member which type is the
8551 : : discriminant type. Or the variant_part itself has a
8552 : : type representing the discriminant. */
8553 : 8 : bool is_signed = false;
8554 [ + - ]: 8 : if (level > 0)
8555 : : {
8556 : 8 : Dwarf_Die *parent = &cbargs->dies[level - 1];
8557 [ + - ]: 8 : if (dwarf_tag (die) == DW_TAG_variant
8558 [ + - ]: 8 : && dwarf_tag (parent) == DW_TAG_variant_part)
8559 : : {
8560 : 8 : Dwarf_Die member;
8561 : 8 : Dwarf_Attribute discr_attr;
8562 : 8 : int bytes;
8563 [ + - ]: 8 : if (dwarf_formref_die (dwarf_attr (parent,
8564 : : DW_AT_discr,
8565 : : &discr_attr),
8566 : : &member) != NULL)
8567 : 8 : die_type_sign_bytes (&member, &is_signed, &bytes);
8568 : : else
8569 : 0 : die_type_sign_bytes (parent, &is_signed, &bytes);
8570 : : }
8571 : : }
8572 [ + + ]: 24 : while (readp < readendp)
8573 : : {
8574 : 16 : int d = (int) *readp++;
8575 : 16 : fprintf (out, "%s ", dwarf_discr_list_name (d));
8576 [ - + ]: 16 : if (readp >= readendp)
8577 : 0 : goto attrval_out;
8578 : :
8579 : 16 : Dwarf_Word val;
8580 : 16 : Dwarf_Sword sval;
8581 [ + + ]: 16 : if (d == DW_DSC_label)
8582 : : {
8583 [ + + ]: 8 : if (is_signed)
8584 : : {
8585 : 4 : get_sleb128 (sval, readp, readendp);
8586 : 4 : fprintf (out, "%" PRId64 "", sval);
8587 : : }
8588 : : else
8589 : : {
8590 : 4 : get_uleb128 (val, readp, readendp);
8591 : 4 : fprintf (out, "%" PRIu64 "", val);
8592 : : }
8593 : : }
8594 [ + - ]: 8 : else if (d == DW_DSC_range)
8595 : : {
8596 [ + + ]: 8 : if (is_signed)
8597 : : {
8598 : 6 : get_sleb128 (sval, readp, readendp);
8599 : 6 : fprintf (out, "%" PRId64 "..", sval);
8600 [ - + ]: 6 : if (readp >= readendp)
8601 : 0 : goto attrval_out;
8602 : 6 : get_sleb128 (sval, readp, readendp);
8603 : 6 : fprintf (out, "%" PRId64 "", sval);
8604 : : }
8605 : : else
8606 : : {
8607 : 2 : get_uleb128 (val, readp, readendp);
8608 : 2 : fprintf (out, "%" PRIu64 "..", val);
8609 [ - + ]: 2 : if (readp >= readendp)
8610 : 0 : goto attrval_out;
8611 : 2 : get_uleb128 (val, readp, readendp);
8612 : 2 : fprintf (out, "%" PRIu64 "", val);
8613 : : }
8614 : : }
8615 : : else
8616 : : {
8617 : 0 : print_block (readendp - readp, readp, out);
8618 : 0 : break;
8619 : : }
8620 [ + + ]: 16 : if (readp < readendp)
8621 : 8 : fprintf (out, ", ");
8622 : : }
8623 : 8 : fputc ('\n', out);
8624 : : }
8625 : : else
8626 : 0 : print_block (block.length, block.data, out);
8627 : : break;
8628 : : }
8629 : : break;
8630 : :
8631 : 0 : default:
8632 [ # # ]: 0 : if (cbargs->silent)
8633 : : break;
8634 : 0 : fprintf (out, " %*s%-20s (%s) ???\n",
8635 : : (int) (level * 2), "", dwarf_attr_name (attr),
8636 : : dwarf_form_name (form));
8637 : 0 : break;
8638 : : }
8639 : :
8640 : 8403490 : return DWARF_CB_OK;
8641 : : }
8642 : :
8643 : : static void
8644 : 212 : print_debug_units (Dwfl_Module *dwflmod,
8645 : : Ebl *ebl, GElf_Ehdr *ehdr __attribute__ ((unused)),
8646 : : Elf_Scn *scn, GElf_Shdr *shdr,
8647 : : Dwarf *dbg, bool debug_types, FILE *out)
8648 : : {
8649 [ + + + + ]: 212 : const bool silent = !(print_debug_sections & section_info) && !debug_types;
8650 : 212 : const char *secname = section_name (ebl, shdr);
8651 : :
8652 : : /* Check section actually exists. */
8653 [ + + ]: 212 : if (!silent)
8654 [ + - ]: 140 : if (get_debug_elf_data (dbg, ebl,
8655 : : debug_types ? IDX_debug_types : IDX_debug_info,
8656 : : scn) == NULL)
8657 : 0 : return;
8658 : :
8659 : 140 : if (!silent)
8660 : 140 : fprintf (out, _("\
8661 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n [Offset]\n"),
8662 : 140 : elf_ndxscn (scn), secname, (uint64_t) shdr->sh_offset);
8663 : :
8664 : : /* If the section is empty we don't have to do anything. */
8665 [ + + + - ]: 212 : if (!silent && shdr->sh_size == 0)
8666 : : return;
8667 : :
8668 : 212 : int maxdies = 20;
8669 : 212 : Dwarf_Die *dies = xmalloc (maxdies * sizeof (Dwarf_Die));
8670 : :
8671 : : /* New compilation unit. */
8672 : 212 : Dwarf_Half version;
8673 : :
8674 : 212 : Dwarf_Die result;
8675 : 212 : Dwarf_Off abbroffset;
8676 : 212 : uint8_t addrsize;
8677 : 212 : uint8_t offsize;
8678 : 212 : uint64_t unit_id;
8679 : 212 : Dwarf_Off subdie_off;
8680 : :
8681 : 212 : int unit_res;
8682 : 212 : Dwarf_CU *cu;
8683 : 212 : Dwarf_CU cu_mem;
8684 : 212 : uint8_t unit_type;
8685 : 212 : Dwarf_Die cudie;
8686 : :
8687 : : /* We cheat a little because we want to see only the CUs from .debug_info
8688 : : or .debug_types. We know the Dwarf_CU struct layout. Set it up at
8689 : : the end of .debug_info if we want .debug_types only. Check the returned
8690 : : Dwarf_CU is still in the expected section. */
8691 [ + + ]: 212 : if (debug_types)
8692 : : {
8693 : 2 : cu_mem.dbg = dbg;
8694 : 2 : cu_mem.end = dbg->sectiondata[IDX_debug_info]->d_size;
8695 : 2 : cu_mem.sec_idx = IDX_debug_info;
8696 : 2 : cu = &cu_mem;
8697 : : }
8698 : : else
8699 : 210 : cu = NULL;
8700 : :
8701 : : next_cu:
8702 : 4510 : unit_res = dwarf_get_units (dbg, cu, &cu, &version, &unit_type,
8703 : : &cudie, NULL);
8704 [ + + ]: 4510 : if (unit_res == 1)
8705 : 210 : goto do_return;
8706 : :
8707 [ - + ]: 4300 : if (unit_res == -1)
8708 : : {
8709 [ # # ]: 0 : if (!silent)
8710 : 0 : error (0, 0, _("cannot get next unit: %s"), dwarf_errmsg (-1));
8711 : 0 : goto do_return;
8712 : : }
8713 : :
8714 [ + + + + ]: 8596 : if (cu->sec_idx != (size_t) (debug_types ? IDX_debug_types : IDX_debug_info))
8715 : 2 : goto do_return;
8716 : :
8717 : 4298 : dwarf_cu_die (cu, &result, NULL, &abbroffset, &addrsize, &offsize,
8718 : : &unit_id, &subdie_off);
8719 : :
8720 [ + + ]: 4298 : if (!silent)
8721 : : {
8722 : 4198 : Dwarf_Off offset = cu->start;
8723 [ + + + - ]: 4198 : if (debug_types && version < 5)
8724 : 4 : {
8725 : 4 : Dwarf_Die typedie;
8726 : 4 : Dwarf_Off dieoffset;
8727 : 4 : dieoffset = dwarf_dieoffset (dwarf_offdie_types (dbg, cu->start
8728 : : + subdie_off,
8729 : : &typedie));
8730 : 4 : fprintf (out, " %s %" PRIu64 ":\n"
8731 : : " %s: %" PRIu16
8732 : : ", %s: %" PRIu64
8733 : : ", %s: %" PRIu8
8734 : : ", %s: %" PRIu8
8735 : : "\n %s: %#" PRIx64
8736 : : ", %s: %#" PRIx64 " [%" PRIx64 "]\n",
8737 : : type_unit_str, (uint64_t) offset, version_str, version,
8738 : : abbrev_offset_str, abbroffset, addr_size_str, addrsize,
8739 : : offset_size_str, offsize, type_sig_str, unit_id,
8740 : : type_offset_str, (uint64_t) subdie_off, dieoffset);
8741 : : }
8742 : : else
8743 : : {
8744 : 4194 : fprintf (out, " %s %" PRIu64 ":\n"
8745 : : " %s: %" PRIu16
8746 : : ", %s: %" PRIu64
8747 : : ", %s: %" PRIu8
8748 : : ", %s: %" PRIu8 "\n",
8749 : : cu_offset_at_str, (uint64_t) offset, version_str, version,
8750 : : abbrev_offset_str, abbroffset, addr_size_str, addrsize,
8751 : : offset_size_str, offsize);
8752 : :
8753 [ + + ]: 4194 : if (version >= 5 || (unit_type != DW_UT_compile
8754 [ + + ]: 170 : && unit_type != DW_UT_partial))
8755 : : {
8756 : 4026 : fprintf (out, " %s: %s (%" PRIu8 ")",
8757 : : unit_type_str, dwarf_unit_name (unit_type), unit_type);
8758 : 4026 : if (unit_type == DW_UT_type
8759 [ + + ]: 4026 : || unit_type == DW_UT_skeleton
8760 [ + - ]: 4014 : || unit_type == DW_UT_split_compile
8761 [ - + ]: 4014 : || unit_type == DW_UT_split_type)
8762 : 12 : fprintf (out, ", %s: 0x%.16" PRIx64 "", unit_id_str, unit_id);
8763 : 4026 : if (unit_type == DW_UT_type
8764 [ - + ]: 4026 : || unit_type == DW_UT_split_type)
8765 : : {
8766 : 0 : Dwarf_Die typedie;
8767 : 0 : Dwarf_Off dieoffset;
8768 : 0 : dwarf_cu_info (cu, NULL, NULL, NULL, &typedie,
8769 : : NULL, NULL, NULL);
8770 : 0 : dieoffset = dwarf_dieoffset (&typedie);
8771 : 0 : fprintf (out, ", %s: %#" PRIx64 " [%" PRIx64 "]",
8772 : : unit_die_off_str, subdie_off, dieoffset);
8773 : : }
8774 : 4026 : fprintf (out, "\n");
8775 : : }
8776 : : }
8777 : : }
8778 : :
8779 [ + - ]: 4298 : if (version < 2 || version > 5
8780 [ + - - + ]: 4298 : || unit_type < DW_UT_compile || unit_type > DW_UT_split_type)
8781 : : {
8782 [ # # ]: 0 : if (!silent)
8783 : 0 : error (0, 0, _("unknown version (%d) or unit type (%d)"),
8784 : : version, unit_type);
8785 : 0 : goto next_cu;
8786 : : }
8787 : :
8788 : 4298 : struct attrcb_args args =
8789 : : {
8790 : : .dwflmod = dwflmod,
8791 : : .silent = silent,
8792 : : .version = version,
8793 : : .addrsize = addrsize,
8794 : : .offset_size = offsize
8795 : : };
8796 : :
8797 : 4298 : bool is_split = false;
8798 : 4298 : int level = 0;
8799 : 4298 : dies[0] = cudie;
8800 : 4298 : args.cu = dies[0].cu;
8801 : 4298 : args.dbg = dbg;
8802 : 4298 : args.is_split = is_split;
8803 : 4298 : args.out = out;
8804 : :
8805 : : /* We might return here again for the split CU subdie. */
8806 : : do_cu:
8807 : 2464664 : do
8808 : : {
8809 : 2464664 : Dwarf_Off offset = dwarf_dieoffset (&dies[level]);
8810 [ - + ]: 2464664 : if (unlikely (offset == (Dwarf_Off) -1))
8811 : : {
8812 [ # # ]: 0 : if (!silent)
8813 : 0 : error (0, 0, _("cannot get DIE offset: %s"),
8814 : : dwarf_errmsg (-1));
8815 : 0 : goto do_return;
8816 : : }
8817 : :
8818 : 2464664 : int tag = dwarf_tag (&dies[level]);
8819 [ - + ]: 2464664 : if (unlikely (tag == DW_TAG_invalid))
8820 : : {
8821 [ # # ]: 0 : if (!silent)
8822 : 0 : error (0, 0, _("cannot get tag of DIE at offset [%" PRIx64
8823 : : "] in section '%s': %s"),
8824 : : (uint64_t) offset, secname, dwarf_errmsg (-1));
8825 : 0 : goto do_return;
8826 : : }
8827 : :
8828 [ + + ]: 2464664 : if (!silent)
8829 : : {
8830 : 2462678 : unsigned int code = dwarf_getabbrevcode (dies[level].abbrev);
8831 [ + + ]: 2462678 : if (is_split)
8832 : 102 : fprintf (out, " {%6" PRIx64 "} ", (uint64_t) offset);
8833 : : else
8834 : 2462576 : fprintf (out, " [%6" PRIx64 "] ", (uint64_t) offset);
8835 : 2462678 : fprintf (out, "%*s%-20s abbrev: %u\n", (int) (level * 2), "",
8836 : : dwarf_tag_name (tag), code);
8837 : : }
8838 : :
8839 : : /* Print the attribute values. */
8840 : 2464664 : args.level = level;
8841 : 2464664 : args.dies = dies;
8842 : 2464664 : (void) dwarf_getattrs (&dies[level], attr_callback, &args, 0);
8843 : :
8844 : : /* Make room for the next level's DIE. */
8845 [ - + ]: 2464664 : if (level + 1 == maxdies)
8846 : 0 : dies = xrealloc (dies, (maxdies += 10) * sizeof (Dwarf_Die));
8847 : :
8848 : 2464664 : int res = dwarf_child (&dies[level], &dies[level + 1]);
8849 [ + + ]: 2464664 : if (res > 0)
8850 : : {
8851 [ + + ]: 2464664 : while ((res = dwarf_siblingof (&dies[level], &dies[level])) == 1)
8852 [ + + ]: 338714 : if (level-- == 0)
8853 : : break;
8854 : :
8855 [ - + ]: 2130258 : if (unlikely (res == -1))
8856 : : {
8857 [ # # ]: 0 : if (!silent)
8858 : 0 : error (0, 0, _("cannot get next DIE: %s\n"),
8859 : : dwarf_errmsg (-1));
8860 : 0 : goto do_return;
8861 : : }
8862 : : }
8863 [ - + ]: 334406 : else if (unlikely (res < 0))
8864 : : {
8865 [ # # ]: 0 : if (!silent)
8866 : 0 : error (0, 0, _("cannot get next DIE: %s"),
8867 : : dwarf_errmsg (-1));
8868 : 0 : goto do_return;
8869 : : }
8870 : : else
8871 : : ++level;
8872 : : }
8873 [ + + ]: 2464664 : while (level >= 0);
8874 : :
8875 : : /* We might want to show the split compile unit if this was a skeleton.
8876 : : We need to scan it if we are requesting printing .debug_ranges for
8877 : : DWARF4 since GNU DebugFission uses "offsets" into the main ranges
8878 : : section. */
8879 [ + + ]: 4308 : if (unit_type == DW_UT_skeleton
8880 [ + + + + ]: 28 : && ((!silent && show_split_units)
8881 [ + + + + ]: 20 : || (version < 5 && (print_debug_sections & section_ranges) != 0)))
8882 : : {
8883 : 12 : Dwarf_Die subdie;
8884 [ + - ]: 12 : if (dwarf_cu_info (cu, NULL, NULL, NULL, &subdie, NULL, NULL, NULL) != 0
8885 [ + + ]: 12 : || dwarf_tag (&subdie) == DW_TAG_invalid)
8886 : : {
8887 [ + - ]: 2 : if (!silent)
8888 : : {
8889 : 2 : Dwarf_Attribute dwo_at;
8890 : 4 : const char *dwo_name =
8891 : 2 : (dwarf_formstring (dwarf_attr (&cudie, DW_AT_dwo_name,
8892 : : &dwo_at))
8893 [ - + ]: 2 : ?: (dwarf_formstring (dwarf_attr (&cudie, DW_AT_GNU_dwo_name,
8894 : : &dwo_at))
8895 [ # # ]: 0 : ?: "<unknown>"));
8896 : 2 : fprintf (stderr,
8897 : : "Could not find split unit '%s', id: %" PRIx64 "\n",
8898 : : dwo_name, unit_id);
8899 : : }
8900 : : }
8901 : : else
8902 : : {
8903 : 10 : Dwarf_CU *split_cu = subdie.cu;
8904 : 10 : Dwarf_Half split_version; /* Should be the same version as skel. */
8905 : 10 : dwarf_cu_die (split_cu, &result, &split_version, &abbroffset,
8906 : : &addrsize, &offsize, &unit_id, &subdie_off);
8907 : 10 : Dwarf_Off offset = cu->start;
8908 : 10 : uint8_t split_unit_type; /* Should be DW_UT_split_compile. */
8909 : 10 : uint64_t split_unit_id; /* Should be the same as id. */
8910 [ - + ]: 10 : if (dwarf_cu_info (split_cu, NULL, &split_unit_type, NULL, NULL,
8911 : : &split_unit_id, NULL, NULL) != 0)
8912 : : {
8913 : : /* Really shouldn't happen unless split_cu is NULL. */
8914 : 0 : split_unit_type = DW_UT_split_compile;
8915 : 0 : split_unit_id = unit_id;
8916 : : }
8917 : :
8918 [ + + ]: 10 : if (!silent)
8919 : : {
8920 : 6 : fprintf (out, _(" Split compilation unit at offset %"
8921 : : PRIu64 ":\n"
8922 : : " Version: %" PRIu16
8923 : : ", Abbreviation section offset: %" PRIu64
8924 : : ", Address size: %" PRIu8
8925 : : ", Offset size: %" PRIu8 "\n"),
8926 : : (uint64_t) offset, split_version, abbroffset,
8927 : : addrsize, offsize);
8928 : 6 : fprintf (out, _(" Unit type: %s (%" PRIu8 ")"),
8929 : : dwarf_unit_name (split_unit_type), split_unit_type);
8930 : 6 : fprintf (out, ", Unit id: 0x%.16" PRIx64 "", split_unit_id);
8931 : 6 : fprintf (out, "\n");
8932 : : }
8933 : :
8934 : 10 : unit_type = split_unit_type;
8935 : 10 : is_split = true;
8936 : 10 : level = 0;
8937 : 10 : dies[0] = subdie;
8938 : 10 : args.cu = dies[0].cu;
8939 : 10 : args.dbg = split_cu->dbg;
8940 : 10 : args.is_split = is_split;
8941 : 10 : goto do_cu;
8942 : : }
8943 : : }
8944 : :
8945 : : /* And again... */
8946 : 4298 : goto next_cu;
8947 : :
8948 : 212 : do_return:
8949 : 212 : free (dies);
8950 : : }
8951 : :
8952 : : static void
8953 : 210 : print_debug_info_section (Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr,
8954 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg,
8955 : : FILE *out)
8956 : : {
8957 : 46 : print_debug_units (dwflmod, ebl, ehdr, scn, shdr, dbg, false, out);
8958 : 46 : }
8959 : :
8960 : : static void
8961 : 2 : print_debug_types_section (Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr,
8962 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg,
8963 : : FILE *out)
8964 : : {
8965 : 2 : print_debug_units (dwflmod, ebl, ehdr, scn, shdr, dbg, true, out);
8966 : 2 : }
8967 : :
8968 : :
8969 : : static void
8970 : 82 : print_decoded_line_section (Dwfl_Module *dwflmod, Ebl *ebl,
8971 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
8972 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg,
8973 : : FILE *out)
8974 : : {
8975 : 82 : fprintf (out, _("\
8976 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n\n"),
8977 : : elf_ndxscn (scn), section_name (ebl, shdr),
8978 : 82 : (uint64_t) shdr->sh_offset);
8979 : :
8980 : 164 : size_t address_size
8981 [ + + ]: 82 : = elf_getident (ebl->elf, NULL)[EI_CLASS] == ELFCLASS32 ? 4 : 8;
8982 : :
8983 : 82 : Dwarf_Lines *lines;
8984 : 82 : size_t nlines;
8985 : 82 : Dwarf_Off off, next_off = 0;
8986 : 82 : Dwarf_CU *cu = NULL;
8987 : 82 : while (dwarf_next_lines (dbg, off = next_off, &next_off, &cu, NULL, NULL,
8988 [ + + ]: 176 : &lines, &nlines) == 0)
8989 : : {
8990 : 94 : Dwarf_Die cudie;
8991 [ + + + - ]: 94 : if (cu != NULL && dwarf_cu_info (cu, NULL, NULL, &cudie,
8992 : : NULL, NULL, NULL, NULL) == 0)
8993 : 50 : fprintf (out, " CU [%" PRIx64 "] %s\n",
8994 : : dwarf_dieoffset (&cudie), dwarf_diename (&cudie));
8995 : : else
8996 : : {
8997 : : /* DWARF5 lines can be independent of any CU, but they probably
8998 : : are used by some CU. Determine the CU this block is for. */
8999 : 44 : Dwarf_Off cuoffset;
9000 : 44 : Dwarf_Off ncuoffset = 0;
9001 : 44 : size_t hsize;
9002 : 44 : while (dwarf_nextcu (dbg, cuoffset = ncuoffset, &ncuoffset, &hsize,
9003 [ + - ]: 46 : NULL, NULL, NULL) == 0)
9004 : : {
9005 [ - + ]: 46 : if (dwarf_offdie (dbg, cuoffset + hsize, &cudie) == NULL)
9006 : 0 : continue;
9007 : 46 : Dwarf_Attribute stmt_list;
9008 [ - + ]: 46 : if (dwarf_attr (&cudie, DW_AT_stmt_list, &stmt_list) == NULL)
9009 : 0 : continue;
9010 : 46 : Dwarf_Word lineoff;
9011 [ - + ]: 46 : if (dwarf_formudata (&stmt_list, &lineoff) != 0)
9012 : 0 : continue;
9013 [ + + ]: 46 : if (lineoff == off)
9014 : : {
9015 : : /* Found the CU. */
9016 : 44 : cu = cudie.cu;
9017 : 44 : break;
9018 : : }
9019 : : }
9020 : :
9021 [ + - ]: 44 : if (cu != NULL)
9022 : 44 : fprintf (out, " CU [%" PRIx64 "] %s\n",
9023 : : dwarf_dieoffset (&cudie), dwarf_diename (&cudie));
9024 : : else
9025 : 0 : fprintf (out, " No CU\n");
9026 : : }
9027 : :
9028 : 94 : fprintf (out, " line:col SBPE* disc isa op address"
9029 : : " (Statement Block Prologue Epilogue *End)\n");
9030 : 94 : const char *last_file = "";
9031 [ + + ]: 720 : for (size_t n = 0; n < nlines; n++)
9032 : : {
9033 : 626 : Dwarf_Line *line = dwarf_onesrcline (lines, n);
9034 [ - + ]: 626 : if (line == NULL)
9035 : : {
9036 : 0 : fprintf (out, " dwarf_onesrcline: %s\n", dwarf_errmsg (-1));
9037 : 0 : continue;
9038 : : }
9039 : 626 : Dwarf_Word mtime, length;
9040 : 626 : const char *file = dwarf_linesrc (line, &mtime, &length);
9041 [ - + ]: 626 : if (file == NULL)
9042 : : {
9043 : 0 : fprintf (out, " <%s> (mtime: ?, length: ?)\n",
9044 : : dwarf_errmsg (-1));
9045 : 0 : last_file = "";
9046 : : }
9047 [ + + ]: 626 : else if (strcmp (last_file, file) != 0)
9048 : : {
9049 : 114 : fprintf (out, " %s (mtime: %" PRIu64 ", length: %" PRIu64 ")\n",
9050 : : file, mtime, length);
9051 : 114 : last_file = file;
9052 : : }
9053 : :
9054 : 626 : int lineno, colno;
9055 : 626 : bool statement, endseq, block, prologue_end, epilogue_begin;
9056 : 626 : unsigned int lineop, isa, disc;
9057 : 626 : Dwarf_Addr address;
9058 : 626 : dwarf_lineaddr (line, &address);
9059 : 626 : dwarf_lineno (line, &lineno);
9060 : 626 : dwarf_linecol (line, &colno);
9061 : 626 : dwarf_lineop_index (line, &lineop);
9062 : 626 : dwarf_linebeginstatement (line, &statement);
9063 : 626 : dwarf_lineendsequence (line, &endseq);
9064 : 626 : dwarf_lineblock (line, &block);
9065 : 626 : dwarf_lineprologueend (line, &prologue_end);
9066 : 626 : dwarf_lineepiloguebegin (line, &epilogue_begin);
9067 : 626 : dwarf_lineisa (line, &isa);
9068 : 626 : dwarf_linediscriminator (line, &disc);
9069 : :
9070 : : /* End sequence is special, it is one byte past. */
9071 : 626 : fprintf (out, " %4d:%-3d %c%c%c%c%c %4d %3d %2d ",
9072 : : lineno, colno,
9073 [ + + ]: 626 : (statement ? 'S' : ' '),
9074 [ + - ]: 626 : (block ? 'B' : ' '),
9075 [ + + ]: 626 : (prologue_end ? 'P' : ' '),
9076 [ + - ]: 626 : (epilogue_begin ? 'E' : ' '),
9077 [ + + ]: 626 : (endseq ? '*' : ' '),
9078 : : disc, isa, lineop);
9079 : 626 : print_dwarf_addr (dwflmod, address_size,
9080 [ + + ]: 626 : address - (endseq ? 1 : 0), address, out);
9081 : 626 : fprintf (out, "\n");
9082 : :
9083 [ + + ]: 626 : if (endseq)
9084 : 98 : fprintf(out, "\n");
9085 : : }
9086 : : }
9087 : 82 : }
9088 : :
9089 : :
9090 : : /* Print the value of a form.
9091 : : Returns new value of readp, or readendp on failure. */
9092 : : static const unsigned char *
9093 : 166776 : print_form_data (Dwarf *dbg, int form, const unsigned char *readp,
9094 : : const unsigned char *readendp, unsigned int offset_len,
9095 : : Dwarf_Off str_offsets_base, FILE *out)
9096 : : {
9097 : 166776 : Dwarf_Word val;
9098 : 166776 : const unsigned char *endp;
9099 : 166776 : Elf_Data *data;
9100 : 166776 : char *str;
9101 [ + - - - : 166776 : switch (form)
- + - - -
- + - - +
- + - - -
- - ]
9102 : : {
9103 : 16 : case DW_FORM_data1:
9104 [ - + ]: 16 : if (readendp - readp < 1)
9105 : : {
9106 : 0 : invalid_data:
9107 : 0 : error (0, 0, "invalid data");
9108 : 0 : return readendp;
9109 : : }
9110 : 16 : val = *readp++;
9111 : 16 : fprintf (out, " %" PRIx8, (unsigned int) val);
9112 : 16 : break;
9113 : :
9114 : 0 : case DW_FORM_data2:
9115 [ # # ]: 0 : if (readendp - readp < 2)
9116 : 0 : goto invalid_data;
9117 [ # # ]: 0 : val = read_2ubyte_unaligned_inc (dbg, readp);
9118 : 0 : fprintf(out, " %" PRIx16, (unsigned int) val);
9119 : 0 : break;
9120 : :
9121 : 0 : case DW_FORM_data4:
9122 [ # # ]: 0 : if (readendp - readp < 4)
9123 : 0 : goto invalid_data;
9124 [ # # ]: 0 : val = read_4ubyte_unaligned_inc (dbg, readp);
9125 : 0 : fprintf (out, " %" PRIx32, (unsigned int) val);
9126 : 0 : break;
9127 : :
9128 : 0 : case DW_FORM_data8:
9129 [ # # ]: 0 : if (readendp - readp < 8)
9130 : 0 : goto invalid_data;
9131 [ # # ]: 0 : val = read_8ubyte_unaligned_inc (dbg, readp);
9132 : 0 : fprintf (out, " %" PRIx64, val);
9133 : 0 : break;
9134 : :
9135 : 0 : case DW_FORM_sdata:
9136 [ # # ]: 0 : if (readendp - readp < 1)
9137 : 0 : goto invalid_data;
9138 : 0 : get_sleb128 (val, readp, readendp);
9139 : 0 : fprintf (out, " %" PRIx64, val);
9140 : 0 : break;
9141 : :
9142 : 67300 : case DW_FORM_udata:
9143 [ - + ]: 67300 : if (readendp - readp < 1)
9144 : 0 : goto invalid_data;
9145 : 67300 : get_uleb128 (val, readp, readendp);
9146 : 67300 : fprintf (out, " %" PRIx64, val);
9147 : 67300 : break;
9148 : :
9149 : 0 : case DW_FORM_block:
9150 [ # # ]: 0 : if (readendp - readp < 1)
9151 : 0 : goto invalid_data;
9152 : 0 : get_uleb128 (val, readp, readendp);
9153 [ # # ]: 0 : if ((size_t) (readendp - readp) < val)
9154 : 0 : goto invalid_data;
9155 : 0 : print_bytes (val, readp, out);
9156 : 0 : readp += val;
9157 : 0 : break;
9158 : :
9159 : 0 : case DW_FORM_block1:
9160 [ # # ]: 0 : if (readendp - readp < 1)
9161 : 0 : goto invalid_data;
9162 : 0 : val = *readp++;
9163 [ # # ]: 0 : if ((size_t) (readendp - readp) < val)
9164 : 0 : goto invalid_data;
9165 : 0 : print_bytes (val, readp, out);
9166 : 0 : readp += val;
9167 : 0 : break;
9168 : :
9169 : 0 : case DW_FORM_block2:
9170 [ # # ]: 0 : if (readendp - readp < 2)
9171 : 0 : goto invalid_data;
9172 [ # # ]: 0 : val = read_2ubyte_unaligned_inc (dbg, readp);
9173 [ # # ]: 0 : if ((size_t) (readendp - readp) < val)
9174 : 0 : goto invalid_data;
9175 : 0 : print_bytes (val, readp, out);
9176 : 0 : readp += val;
9177 : 0 : break;
9178 : :
9179 : 0 : case DW_FORM_block4:
9180 [ # # ]: 0 : if (readendp - readp < 4)
9181 : 0 : goto invalid_data;
9182 [ # # ]: 0 : val = read_4ubyte_unaligned_inc (dbg, readp);
9183 [ # # ]: 0 : if ((size_t) (readendp - readp) < val)
9184 : 0 : goto invalid_data;
9185 : 0 : print_bytes (val, readp, out);
9186 : 0 : readp += val;
9187 : 0 : break;
9188 : :
9189 : 24 : case DW_FORM_data16:
9190 [ - + ]: 24 : if (readendp - readp < 16)
9191 : 0 : goto invalid_data;
9192 : 24 : print_bytes (16, readp, out);
9193 : 24 : readp += 16;
9194 : 24 : break;
9195 : :
9196 : 0 : case DW_FORM_flag:
9197 [ # # ]: 0 : if (readendp - readp < 1)
9198 : 0 : goto invalid_data;
9199 : 0 : val = *readp++;
9200 [ # # ]: 0 : fprintf (out, "%s", val != 0 ? yes_str : no_str);
9201 : 0 : break;
9202 : :
9203 : 0 : case DW_FORM_string:
9204 : 0 : endp = memchr (readp, '\0', readendp - readp);
9205 [ # # ]: 0 : if (endp == NULL)
9206 : 0 : goto invalid_data;
9207 : 0 : fprintf (out, "%s", readp);
9208 : 0 : readp = endp + 1;
9209 : 0 : break;
9210 : :
9211 : 97880 : case DW_FORM_strp:
9212 : : case DW_FORM_line_strp:
9213 : : case DW_FORM_strp_sup:
9214 [ - + ]: 97880 : if ((size_t) (readendp - readp) < offset_len)
9215 : 0 : goto invalid_data;
9216 [ - + ]: 97880 : if (offset_len == 8)
9217 [ # # ]: 0 : val = read_8ubyte_unaligned_inc (dbg, readp);
9218 : : else
9219 [ - + ]: 97880 : val = read_4ubyte_unaligned_inc (dbg, readp);
9220 [ - + ]: 97880 : if (form == DW_FORM_strp)
9221 : 0 : data = dbg->sectiondata[IDX_debug_str];
9222 [ + - ]: 97880 : else if (form == DW_FORM_line_strp)
9223 : 97880 : data = dbg->sectiondata[IDX_debug_line_str];
9224 : : else /* form == DW_FORM_strp_sup */
9225 : : {
9226 : 0 : Dwarf *alt = dwarf_getalt (dbg);
9227 [ # # ]: 0 : data = alt != NULL ? alt->sectiondata[IDX_debug_str] : NULL;
9228 : : }
9229 [ + - - + ]: 97880 : if (data == NULL || val >= data->d_size
9230 [ - + ]: 97880 : || memchr (data->d_buf + val, '\0', data->d_size - val) == NULL)
9231 : : str = "???";
9232 : : else
9233 : 97880 : str = (char *) data->d_buf + val;
9234 : 97880 : fprintf (out, "%s (%" PRIu64 ")", str, val);
9235 : 97880 : break;
9236 : :
9237 : 0 : case DW_FORM_sec_offset:
9238 [ # # ]: 0 : if ((size_t) (readendp - readp) < offset_len)
9239 : 0 : goto invalid_data;
9240 [ # # ]: 0 : if (offset_len == 8)
9241 [ # # ]: 0 : val = read_8ubyte_unaligned_inc (dbg, readp);
9242 : : else
9243 [ # # ]: 0 : val = read_4ubyte_unaligned_inc (dbg, readp);
9244 : 0 : fprintf (out, "[%" PRIx64 "]", val);
9245 : 0 : break;
9246 : :
9247 : 1556 : case DW_FORM_strx:
9248 : : case DW_FORM_GNU_str_index:
9249 [ - + ]: 1556 : if (readendp - readp < 1)
9250 : 0 : goto invalid_data;
9251 : 1556 : get_uleb128 (val, readp, readendp);
9252 : 1556 : strx_val:
9253 : 1556 : data = dbg->sectiondata[IDX_debug_str_offsets];
9254 [ + - ]: 1556 : if (data == NULL
9255 [ - + ]: 1556 : || data->d_size - str_offsets_base < val * offset_len)
9256 : : str = "???";
9257 : : else
9258 : : {
9259 : 1556 : const unsigned char *strreadp = (data->d_buf + str_offsets_base
9260 : 1556 : + val * offset_len);
9261 : 1556 : const unsigned char *strreadendp = data->d_buf + data->d_size;
9262 [ - + ]: 1556 : if ((size_t) (strreadendp - strreadp) < offset_len)
9263 : : str = "???";
9264 : : else
9265 : : {
9266 : 1556 : Dwarf_Off idx;
9267 [ - + ]: 1556 : if (offset_len == 8)
9268 [ # # ]: 0 : idx = read_8ubyte_unaligned (dbg, strreadp);
9269 : : else
9270 [ - + ]: 1556 : idx = read_4ubyte_unaligned (dbg, strreadp);
9271 : :
9272 : 1556 : data = dbg->sectiondata[IDX_debug_str];
9273 [ - + - + ]: 1556 : if (data == NULL || idx >= data->d_size
9274 [ - + ]: 1556 : || memchr (data->d_buf + idx, '\0',
9275 : : data->d_size - idx) == NULL)
9276 : : str = "???";
9277 : : else
9278 : 1556 : str = (char *) data->d_buf + idx;
9279 : : }
9280 : : }
9281 : 1556 : fprintf (out, "%s (%" PRIu64 ")", str, val);
9282 : 1556 : break;
9283 : :
9284 : 0 : case DW_FORM_strx1:
9285 [ # # ]: 0 : if (readendp - readp < 1)
9286 : 0 : goto invalid_data;
9287 : 0 : val = *readp++;
9288 : 0 : goto strx_val;
9289 : :
9290 : 0 : case DW_FORM_strx2:
9291 [ # # ]: 0 : if (readendp - readp < 2)
9292 : 0 : goto invalid_data;
9293 [ # # ]: 0 : val = read_2ubyte_unaligned_inc (dbg, readp);
9294 : 0 : goto strx_val;
9295 : :
9296 : 0 : case DW_FORM_strx3:
9297 [ # # ]: 0 : if (readendp - readp < 3)
9298 : 0 : goto invalid_data;
9299 : 0 : val = read_3ubyte_unaligned_inc (dbg, readp);
9300 : 0 : goto strx_val;
9301 : :
9302 : 0 : case DW_FORM_strx4:
9303 [ # # ]: 0 : if (readendp - readp < 4)
9304 : 0 : goto invalid_data;
9305 [ # # ]: 0 : val = read_4ubyte_unaligned_inc (dbg, readp);
9306 : 0 : goto strx_val;
9307 : :
9308 : 0 : default:
9309 : 0 : error (0, 0, _("unknown form: %s"), dwarf_form_name (form));
9310 : 0 : return readendp;
9311 : : }
9312 : :
9313 : 166776 : return readp;
9314 : : }
9315 : :
9316 : : /* Only used via run_advance_pc() macro */
9317 : : static inline void
9318 : 901068 : run_advance_pc (unsigned int op_advance,
9319 : : unsigned int minimum_instr_len,
9320 : : unsigned int max_ops_per_instr,
9321 : : unsigned int *op_addr_advance,
9322 : : Dwarf_Word *address,
9323 : : unsigned int *op_index)
9324 : : {
9325 : 901068 : const unsigned int advanced_op_index = (*op_index) + op_advance;
9326 : :
9327 : 901068 : *op_addr_advance = minimum_instr_len * (advanced_op_index
9328 : 901068 : / max_ops_per_instr);
9329 : 901068 : *address = *address + *op_addr_advance;
9330 : 901068 : *op_index = advanced_op_index % max_ops_per_instr;
9331 : : }
9332 : :
9333 : : static void
9334 : 186 : print_debug_line_section (Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr,
9335 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg,
9336 : : FILE *out)
9337 : : {
9338 [ + + ]: 186 : if (decodedline)
9339 : : {
9340 : 82 : print_decoded_line_section (dwflmod, ebl, ehdr, scn, shdr, dbg, out);
9341 : 164 : return;
9342 : : }
9343 : :
9344 : 104 : Elf_Data *data = get_debug_elf_data (dbg, ebl, IDX_debug_line, scn);
9345 [ + - ]: 104 : if (data == NULL)
9346 : : return;
9347 : :
9348 : 104 : fprintf (out, _("\
9349 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
9350 : : elf_ndxscn (scn), section_name (ebl, shdr),
9351 : 104 : (uint64_t) shdr->sh_offset);
9352 : :
9353 [ + - ]: 104 : if (shdr->sh_size == 0)
9354 : : return;
9355 : :
9356 : : /* There is no functionality in libdw to read the information in the
9357 : : way it is represented here. Hardcode the decoder. */
9358 : :
9359 : 104 : const unsigned char *linep = (const unsigned char *) data->d_buf;
9360 : 104 : const unsigned char *lineendp;
9361 : :
9362 : 104 : while (linep
9363 [ + + ]: 4266 : < (lineendp = (const unsigned char *) data->d_buf + data->d_size))
9364 : : {
9365 : 4162 : size_t start_offset = linep - (const unsigned char *) data->d_buf;
9366 : :
9367 : 4162 : fprintf (out, "\n%s %zu:\n",
9368 : : table_offset_lower_str, start_offset);
9369 : :
9370 [ - + ]: 4162 : if (unlikely (linep + 4 > lineendp))
9371 : 0 : goto invalid_data;
9372 [ + + ]: 4162 : Dwarf_Word unit_length = read_4ubyte_unaligned_inc (dbg, linep);
9373 : 4162 : unsigned int length = 4;
9374 [ - + ]: 4162 : if (unlikely (unit_length == 0xffffffff))
9375 : : {
9376 [ # # ]: 0 : if (unlikely (linep + 8 > lineendp))
9377 : : {
9378 : 0 : invalid_data:
9379 : 0 : error (0, 0, _("invalid data in section [%zu] '%s'"),
9380 : : elf_ndxscn (scn), section_name (ebl, shdr));
9381 : 0 : return;
9382 : : }
9383 [ # # ]: 0 : unit_length = read_8ubyte_unaligned_inc (dbg, linep);
9384 : 0 : length = 8;
9385 : : }
9386 : :
9387 : : /* Check whether we have enough room in the section. */
9388 [ - + ]: 4162 : if (unlikely (unit_length > (size_t) (lineendp - linep)))
9389 : 0 : goto invalid_data;
9390 : 4162 : lineendp = linep + unit_length;
9391 : :
9392 : : /* The next element of the header is the version identifier. */
9393 [ - + ]: 4162 : if ((size_t) (lineendp - linep) < 2)
9394 : 0 : goto invalid_data;
9395 [ + + ]: 4162 : uint_fast16_t version = read_2ubyte_unaligned_inc (dbg, linep);
9396 : :
9397 : 8324 : size_t address_size
9398 [ + + ]: 4162 : = elf_getident (ebl->elf, NULL)[EI_CLASS] == ELFCLASS32 ? 4 : 8;
9399 : 4162 : unsigned char segment_selector_size = 0;
9400 [ + + ]: 4162 : if (version > 4)
9401 : : {
9402 [ - + ]: 4010 : if ((size_t) (lineendp - linep) < 2)
9403 : 0 : goto invalid_data;
9404 : 4010 : address_size = *linep++;
9405 : 4010 : segment_selector_size = *linep++;
9406 : : }
9407 : :
9408 : : /* Next comes the header length. */
9409 : 4162 : Dwarf_Word header_length;
9410 [ + - ]: 4162 : if (length == 4)
9411 : : {
9412 [ - + ]: 4162 : if ((size_t) (lineendp - linep) < 4)
9413 : 0 : goto invalid_data;
9414 [ + + ]: 4162 : header_length = read_4ubyte_unaligned_inc (dbg, linep);
9415 : : }
9416 : : else
9417 : : {
9418 [ # # ]: 0 : if ((size_t) (lineendp - linep) < 8)
9419 : 0 : goto invalid_data;
9420 [ # # ]: 0 : header_length = read_8ubyte_unaligned_inc (dbg, linep);
9421 : : }
9422 : :
9423 : 4162 : const unsigned char *header_start = linep;
9424 : :
9425 : : /* Next the minimum instruction length. */
9426 [ - + ]: 4162 : if ((size_t) (lineendp - linep) < 1)
9427 : 0 : goto invalid_data;
9428 : 4162 : uint_fast8_t minimum_instr_len = *linep++;
9429 : :
9430 : : /* Next the maximum operations per instruction, in version 4 format. */
9431 : 4162 : uint_fast8_t max_ops_per_instr;
9432 [ + + ]: 4162 : if (version < 4)
9433 : : max_ops_per_instr = 1;
9434 : : else
9435 : : {
9436 [ - + ]: 4030 : if ((size_t) (lineendp - linep) < 1)
9437 : 0 : goto invalid_data;
9438 : 4030 : max_ops_per_instr = *linep++;
9439 : : }
9440 : :
9441 : : /* We need at least 4 more bytes. */
9442 [ - + ]: 4162 : if ((size_t) (lineendp - linep) < 4)
9443 : 0 : goto invalid_data;
9444 : :
9445 : : /* Then the flag determining the default value of the is_stmt
9446 : : register. */
9447 : 4162 : uint_fast8_t default_is_stmt = *linep++;
9448 : :
9449 : : /* Now the line base. */
9450 : 4162 : int_fast8_t line_base = *linep++;
9451 : :
9452 : : /* And the line range. */
9453 : 4162 : uint_fast8_t line_range = *linep++;
9454 : :
9455 : : /* The opcode base. */
9456 : 4162 : uint_fast8_t opcode_base = *linep++;
9457 : :
9458 : : /* Print what we got so far. */
9459 : 4162 : fprintf (out, "\n"
9460 : : " %s: %" PRIu64 "\n"
9461 : : " %s: %" PRIuFAST16 "\n"
9462 : : " %s: %" PRIu64 "\n"
9463 : : " %s: %zd\n"
9464 : : " %s: %zd\n"
9465 : : " %s: %" PRIuFAST8 "\n"
9466 : : " %s: %" PRIuFAST8 "\n"
9467 : : " %s 'is_stmt': %" PRIuFAST8 "\n"
9468 : : " %s: %" PRIdFAST8 "\n"
9469 : : " %s: %" PRIuFAST8 "\n"
9470 : : " %s: %" PRIuFAST8 "\n"
9471 : : "\n"
9472 : : "%s:\n",
9473 : : length_str, (uint64_t) unit_length, dwarf_ver_str, version,
9474 : : prologue_len_str, (uint64_t) header_length, addr_size_str,
9475 : : address_size, seg_selector_str, (size_t) segment_selector_size,
9476 : : min_inst_len_str, minimum_instr_len, max_op_per_inst_str,
9477 : : max_ops_per_instr, initial_value_if_str, default_is_stmt,
9478 : : line_base_str, line_base, line_range_str, line_range,
9479 : : opcodes_base_str, opcode_base, opcodes_str);
9480 : :
9481 [ - + ]: 4162 : if (version < 2 || version > 5)
9482 : : {
9483 : 0 : error (0, 0, _("cannot handle .debug_line version: %u\n"),
9484 : : (unsigned int) version);
9485 : 0 : linep = lineendp;
9486 : 0 : continue;
9487 : : }
9488 : :
9489 [ - + ]: 4162 : if (address_size != 4 && address_size != 8)
9490 : : {
9491 : 0 : error (0, 0, _("cannot handle address size: %u\n"),
9492 : : (unsigned int) address_size);
9493 : 0 : linep = lineendp;
9494 : 0 : continue;
9495 : : }
9496 : :
9497 [ - + ]: 4162 : if (segment_selector_size != 0)
9498 : : {
9499 : 0 : error (0, 0, _("cannot handle segment selector size: %u\n"),
9500 : : (unsigned int) segment_selector_size);
9501 : 0 : linep = lineendp;
9502 : 0 : continue;
9503 : : }
9504 : :
9505 [ - + ]: 4162 : if (unlikely (linep + opcode_base - 1 >= lineendp))
9506 : : {
9507 : 0 : invalid_unit:
9508 : 0 : error (0, 0,
9509 : 0 : _("invalid data at offset %tu in section [%zu] '%s'"),
9510 : 0 : linep - (const unsigned char *) data->d_buf,
9511 : : elf_ndxscn (scn), section_name (ebl, shdr));
9512 : 0 : linep = lineendp;
9513 : 0 : continue;
9514 : : }
9515 : 4162 : int opcode_base_l10 = 1;
9516 : 4162 : unsigned int tmp = opcode_base;
9517 [ + + ]: 8318 : while (tmp > 10)
9518 : : {
9519 : 4156 : tmp /= 10;
9520 : 4156 : ++opcode_base_l10;
9521 : : }
9522 : : const uint8_t *standard_opcode_lengths = linep - 1;
9523 [ + + ]: 54088 : for (uint_fast8_t cnt = 1; cnt < opcode_base; ++cnt)
9524 : 49926 : fprintf (out, ngettext (" [%*" PRIuFAST8 "] %hhu argument\n",
9525 : : " [%*" PRIuFAST8 "] %hhu arguments\n",
9526 : : (int) linep[cnt - 1]),
9527 : 49926 : opcode_base_l10, cnt, linep[cnt - 1]);
9528 : 4162 : linep += opcode_base - 1;
9529 : :
9530 : 4162 : if (unlikely (linep >= lineendp))
9531 : : goto invalid_unit;
9532 : :
9533 : 4162 : Dwarf_Off str_offsets_base = str_offsets_base_off (dbg, NULL);
9534 : :
9535 : 4162 : fprintf (out, "\n%s:\n", dir_table_str);
9536 [ + + ]: 4162 : if (version > 4)
9537 : : {
9538 : 4010 : struct encpair { uint16_t desc; uint16_t form; };
9539 : 4010 : struct encpair enc[256];
9540 : :
9541 : 4010 : fprintf (out, " [");
9542 : 4010 : if ((size_t) (lineendp - linep) < 1)
9543 : 0 : goto invalid_data;
9544 : 4010 : unsigned char directory_entry_format_count = *linep++;
9545 [ + + ]: 8020 : for (int i = 0; i < directory_entry_format_count; i++)
9546 : : {
9547 : 4010 : uint16_t desc, form;
9548 [ - + ]: 4010 : if ((size_t) (lineendp - linep) < 1)
9549 : 0 : goto invalid_data;
9550 : 4010 : get_uleb128 (desc, linep, lineendp);
9551 [ - + ]: 4010 : if ((size_t) (lineendp - linep) < 1)
9552 : 0 : goto invalid_data;
9553 : 4010 : get_uleb128 (form, linep, lineendp);
9554 : :
9555 : 4010 : enc[i].desc = desc;
9556 : 4010 : enc[i].form = form;
9557 : :
9558 : 4010 : fprintf (out, "%s(%s)",
9559 : : dwarf_line_content_description_name (desc),
9560 : : dwarf_form_name (form));
9561 [ - + ]: 4010 : if (i + 1 < directory_entry_format_count)
9562 : 0 : fprintf (out, ", ");
9563 : : }
9564 : 4010 : fprintf (out, "]\n");
9565 : :
9566 : 4010 : uint64_t directories_count;
9567 [ - + ]: 4010 : if ((size_t) (lineendp - linep) < 1)
9568 : 0 : goto invalid_data;
9569 : 4010 : get_uleb128 (directories_count, linep, lineendp);
9570 : :
9571 : 4010 : if (directory_entry_format_count == 0
9572 [ - + ]: 4010 : && directories_count != 0)
9573 : 0 : goto invalid_data;
9574 : :
9575 [ + + ]: 34574 : for (uint64_t i = 0; i < directories_count; i++)
9576 : : {
9577 : 30564 : fprintf (out, " %-5" PRIu64 " ", i);
9578 [ + + ]: 61128 : for (int j = 0; j < directory_entry_format_count; j++)
9579 : : {
9580 : 30564 : linep = print_form_data (dbg, enc[j].form,
9581 : : linep, lineendp, length,
9582 : : str_offsets_base, out);
9583 [ - + ]: 30564 : if (j + 1 < directory_entry_format_count)
9584 : 0 : fprintf (out, ", ");
9585 : : }
9586 : 30564 : fprintf (out, "\n");
9587 [ - + ]: 30564 : if (linep >= lineendp)
9588 : 0 : goto invalid_unit;
9589 : : }
9590 : : }
9591 : : else
9592 : : {
9593 [ + - + + ]: 766 : while (linep < lineendp && *linep != 0)
9594 : : {
9595 : 614 : const unsigned char *endp = memchr (linep, '\0', lineendp - linep);
9596 [ - + ]: 614 : if (unlikely (endp == NULL))
9597 : 0 : goto invalid_unit;
9598 : :
9599 : 614 : fprintf (out, " %s\n", (char *) linep);
9600 : :
9601 : 614 : linep = endp + 1;
9602 : : }
9603 [ + - - + ]: 152 : if (linep >= lineendp || *linep != 0)
9604 : 0 : goto invalid_unit;
9605 : : /* Skip the final NUL byte. */
9606 : 152 : ++linep;
9607 : : }
9608 : :
9609 [ - + ]: 4162 : if (unlikely (linep >= lineendp))
9610 : 0 : goto invalid_unit;
9611 : :
9612 : 4162 : fprintf (out, "\n%s:\n", file_table_str);
9613 [ + + ]: 4162 : if (version > 4)
9614 : : {
9615 : 4010 : struct encpair { uint16_t desc; uint16_t form; };
9616 : 4010 : struct encpair enc[256];
9617 : :
9618 : 4010 : fprintf (out, " [");
9619 : 4010 : if ((size_t) (lineendp - linep) < 1)
9620 : 0 : goto invalid_data;
9621 : 4010 : unsigned char file_name_format_count = *linep++;
9622 [ + + ]: 12038 : for (int i = 0; i < file_name_format_count; i++)
9623 : : {
9624 : 8028 : uint64_t desc, form;
9625 [ - + ]: 8028 : if ((size_t) (lineendp - linep) < 1)
9626 : 0 : goto invalid_data;
9627 : 8028 : get_uleb128 (desc, linep, lineendp);
9628 [ - + ]: 8028 : if ((size_t) (lineendp - linep) < 1)
9629 : 0 : goto invalid_data;
9630 : 8028 : get_uleb128 (form, linep, lineendp);
9631 : :
9632 [ - + ]: 8028 : if (! libdw_valid_user_form (form))
9633 : 0 : goto invalid_data;
9634 : :
9635 : 8028 : enc[i].desc = desc;
9636 : 8028 : enc[i].form = form;
9637 : :
9638 : 8028 : fprintf (out, "%s(%s)",
9639 : : dwarf_line_content_description_name (desc),
9640 : : dwarf_form_name (form));
9641 [ + + ]: 8028 : if (i + 1 < file_name_format_count)
9642 : 4018 : fprintf (out, ", ");
9643 : : }
9644 : 4010 : fprintf (out, "]\n");
9645 : :
9646 : 4010 : uint64_t file_name_count;
9647 [ - + ]: 4010 : if ((size_t) (lineendp - linep) < 1)
9648 : 0 : goto invalid_data;
9649 : 4010 : get_uleb128 (file_name_count, linep, lineendp);
9650 : :
9651 : 4010 : if (file_name_format_count == 0
9652 [ - + ]: 4010 : && file_name_count != 0)
9653 : 0 : goto invalid_data;
9654 : :
9655 [ + + ]: 71326 : for (uint64_t i = 0; i < file_name_count; i++)
9656 : : {
9657 : 67316 : fprintf (out, " %-5" PRIu64 " ", i);
9658 [ + + ]: 201972 : for (int j = 0; j < file_name_format_count; j++)
9659 : : {
9660 : 134656 : linep = print_form_data (dbg, enc[j].form,
9661 : : linep, lineendp, length,
9662 : : str_offsets_base, out);
9663 [ + + ]: 134656 : if (j + 1 < file_name_format_count)
9664 : 67340 : fprintf (out, ", ");
9665 : : }
9666 : 67316 : fprintf (out, "\n");
9667 [ - + ]: 67316 : if (linep > lineendp)
9668 : 0 : goto invalid_unit;
9669 : : }
9670 : : }
9671 : : else
9672 : : {
9673 : 152 : fputs (_(" Entry Dir Time Size Name\n"), out);
9674 [ + - + + ]: 6154 : for (unsigned int cnt = 1; linep < lineendp && *linep != 0; ++cnt)
9675 : : {
9676 : : /* First comes the file name. */
9677 : 6002 : char *fname = (char *) linep;
9678 : 6002 : unsigned char *endp = memchr (fname, '\0', lineendp - linep);
9679 [ - + ]: 6002 : if (unlikely (endp == NULL))
9680 : 0 : goto invalid_unit;
9681 : 6002 : linep = endp + 1;
9682 : :
9683 : : /* Then the index. */
9684 : 6002 : unsigned int diridx;
9685 [ - + ]: 6002 : if (lineendp - linep < 1)
9686 : 0 : goto invalid_unit;
9687 : 6002 : get_uleb128 (diridx, linep, lineendp);
9688 : :
9689 : : /* Next comes the modification time. */
9690 : 6002 : unsigned int mtime;
9691 [ - + ]: 6002 : if (lineendp - linep < 1)
9692 : 0 : goto invalid_unit;
9693 : 6002 : get_uleb128 (mtime, linep, lineendp);
9694 : :
9695 : : /* Finally the length of the file. */
9696 : 6002 : unsigned int fsize;
9697 [ - + ]: 6002 : if (lineendp - linep < 1)
9698 : 0 : goto invalid_unit;
9699 : 6002 : get_uleb128 (fsize, linep, lineendp);
9700 : :
9701 : 6002 : fprintf (out, " %-5u %-5u %-9u %-9u %s\n",
9702 : : cnt, diridx, mtime, fsize, fname);
9703 : : }
9704 [ + - - + ]: 152 : if (linep >= lineendp || *linep != '\0')
9705 : 0 : goto invalid_unit;
9706 : : /* Skip the final NUL byte. */
9707 : 152 : ++linep;
9708 : : }
9709 : :
9710 : 4162 : unsigned int debug_str_offset = 0;
9711 [ + + ]: 4162 : if (unlikely (linep == header_start + header_length - 4))
9712 : : {
9713 : : /* CUBINs contain an unsigned 4-byte offset */
9714 [ - + ]: 2 : debug_str_offset = read_4ubyte_unaligned_inc (dbg, linep);
9715 : : }
9716 : :
9717 [ + + ]: 4162 : if (linep == lineendp)
9718 : : {
9719 : 40 : fprintf (out, "\n%s.\n", no_line_num_stmts_str);
9720 : 40 : continue;
9721 : : }
9722 : :
9723 : 4122 : fprintf (out, "\n%s:\n", line_num_stmts_str);
9724 : 4122 : Dwarf_Word address = 0;
9725 : 4122 : unsigned int op_index = 0;
9726 : 4122 : size_t line = 1;
9727 : 4122 : uint_fast8_t is_stmt = default_is_stmt;
9728 : :
9729 : : /* Apply the "operation advance" from a special opcode
9730 : : or DW_LNS_advance_pc (as per DWARF4 6.2.5.1). */
9731 : 4122 : unsigned int op_addr_advance;
9732 : : #define advance_pc(op_advance) run_advance_pc(op_advance, minimum_instr_len, \
9733 : : max_ops_per_instr, &op_addr_advance, &address, &op_index)
9734 : :
9735 [ - + ]: 4122 : if (max_ops_per_instr == 0)
9736 : : {
9737 : 0 : error (0, 0,
9738 : 0 : _("invalid maximum operations per instruction is zero"));
9739 : 0 : linep = lineendp;
9740 : 0 : continue;
9741 : : }
9742 : :
9743 [ + + ]: 2894048 : while (linep < lineendp)
9744 : : {
9745 : 2889926 : size_t offset = linep - (const unsigned char *) data->d_buf;
9746 : 2889926 : unsigned int u128;
9747 : 2889926 : int s128;
9748 : :
9749 : : /* Read the opcode. */
9750 : 2889926 : unsigned int opcode = *linep++;
9751 : :
9752 : 2889926 : fprintf (out, " [%6" PRIx64 "]", (uint64_t)offset);
9753 : : /* Is this a special opcode? */
9754 [ + + ]: 2889926 : if (likely (opcode >= opcode_base))
9755 : : {
9756 [ - + ]: 821516 : if (unlikely (line_range == 0))
9757 : 0 : goto invalid_unit;
9758 : :
9759 : : /* Yes. Handling this is quite easy since the opcode value
9760 : : is computed with
9761 : :
9762 : : opcode = (desired line increment - line_base)
9763 : : + (line_range * address advance) + opcode_base
9764 : : */
9765 : 821516 : int line_increment = (line_base
9766 : 821516 : + (opcode - opcode_base) % line_range);
9767 : :
9768 : : /* Perform the increments. */
9769 : 821516 : line += line_increment;
9770 : 821516 : advance_pc ((opcode - opcode_base) / line_range);
9771 : :
9772 : 821516 : fprintf (out, " %s %u: %s+%u = ",
9773 : : special_opcode_str, opcode, address_str, op_addr_advance);
9774 : 821516 : print_dwarf_addr (dwflmod, 0, address, address, out);
9775 [ - + ]: 821516 : if (op_index > 0)
9776 : 0 : fprintf (out, ", op_index = %u, %s%+d = %zu\n",
9777 : : op_index, line_str, line_increment, line);
9778 : : else
9779 : 821516 : fprintf (out, ", %s%+d = %zu\n",
9780 : : line_str, line_increment, line);
9781 : : }
9782 [ + + ]: 2068410 : else if (opcode == 0)
9783 : : {
9784 : : /* This an extended opcode. */
9785 [ - + ]: 218950 : if (unlikely (linep + 2 > lineendp))
9786 : 0 : goto invalid_unit;
9787 : :
9788 : : /* The length. */
9789 : 218950 : unsigned int len = *linep++;
9790 : :
9791 [ - + ]: 218950 : if (unlikely (linep + len > lineendp))
9792 : 0 : goto invalid_unit;
9793 : :
9794 : : /* The sub-opcode. */
9795 : 218950 : opcode = *linep++;
9796 : :
9797 : 218950 : fprintf (out, " %s %u: ", ext_opcode_str, opcode);
9798 : :
9799 [ + + - + : 218950 : switch (opcode)
+ - - ]
9800 : : {
9801 : 12322 : case DW_LNE_end_sequence:
9802 : 12322 : fprintf (out, " %s\n", end_of_seq_str);
9803 : :
9804 : : /* Reset the registers we care about. */
9805 : 12322 : address = 0;
9806 : 12322 : op_index = 0;
9807 : 12322 : line = 1;
9808 : 12322 : is_stmt = default_is_stmt;
9809 : 12322 : break;
9810 : :
9811 : 14260 : case DW_LNE_set_address:
9812 : 14260 : op_index = 0;
9813 [ - + ]: 14260 : if (unlikely ((size_t) (lineendp - linep) < address_size))
9814 : 0 : goto invalid_unit;
9815 [ + + ]: 14260 : if (address_size == 4)
9816 [ + + ]: 50 : address = read_4ubyte_unaligned_inc (dbg, linep);
9817 : : else
9818 [ + + ]: 14210 : address = read_8ubyte_unaligned_inc (dbg, linep);
9819 : : {
9820 : 14260 : fprintf (out, " %s ", set_addr_to_str);
9821 : 14260 : print_dwarf_addr (dwflmod, 0, address, address, out);
9822 : 14260 : fprintf (out, "\n");
9823 : : }
9824 : 14260 : break;
9825 : :
9826 : 0 : case DW_LNE_define_file:
9827 : : {
9828 : 0 : char *fname = (char *) linep;
9829 : 0 : const unsigned char *endp = memchr (linep, '\0',
9830 : 0 : lineendp - linep);
9831 [ # # ]: 0 : if (unlikely (endp == NULL))
9832 : 0 : goto invalid_unit;
9833 : 0 : linep = endp + 1;
9834 : :
9835 : 0 : unsigned int diridx;
9836 [ # # ]: 0 : if (lineendp - linep < 1)
9837 : 0 : goto invalid_unit;
9838 : 0 : get_uleb128 (diridx, linep, lineendp);
9839 : 0 : Dwarf_Word mtime;
9840 [ # # ]: 0 : if (lineendp - linep < 1)
9841 : 0 : goto invalid_unit;
9842 : 0 : get_uleb128 (mtime, linep, lineendp);
9843 : 0 : Dwarf_Word filelength;
9844 [ # # ]: 0 : if (lineendp - linep < 1)
9845 : 0 : goto invalid_unit;
9846 : 0 : get_uleb128 (filelength, linep, lineendp);
9847 : :
9848 : 0 : fprintf (out, _("\
9849 : : define new file: dir=%u, mtime=%" PRIu64 ", length=%" PRIu64 ", name=%s\n"),
9850 : : diridx, (uint64_t) mtime, (uint64_t) filelength,
9851 : : fname);
9852 : : }
9853 : 0 : break;
9854 : :
9855 : 192354 : case DW_LNE_set_discriminator:
9856 : : /* Takes one ULEB128 parameter, the discriminator. */
9857 [ + - - + ]: 192354 : if (unlikely (standard_opcode_lengths[opcode] != 1
9858 : : || lineendp - linep < 1))
9859 : 0 : goto invalid_unit;
9860 : :
9861 : 192354 : get_uleb128 (u128, linep, lineendp);
9862 : 192354 : fprintf (out, " %s %u\n", set_disc_str, u128);
9863 : 192354 : break;
9864 : :
9865 : 14 : case DW_LNE_NVIDIA_inlined_call:
9866 : : {
9867 [ - + ]: 14 : if (unlikely (linep >= lineendp))
9868 : 0 : goto invalid_data;
9869 : :
9870 : 14 : unsigned int context;
9871 : 14 : get_uleb128 (context, linep, lineendp);
9872 : :
9873 [ - + ]: 14 : if (unlikely (linep >= lineendp))
9874 : 0 : goto invalid_data;
9875 : :
9876 : 14 : unsigned int function_name;
9877 : 14 : get_uleb128 (function_name, linep, lineendp);
9878 : 14 : function_name += debug_str_offset;
9879 : :
9880 : 14 : Elf_Data *str_data = dbg->sectiondata[IDX_debug_str];
9881 : 14 : char *function_str;
9882 [ + - - + ]: 14 : if (str_data == NULL || function_name >= str_data->d_size
9883 [ - + ]: 14 : || memchr (str_data->d_buf + function_name, '\0',
9884 : : str_data->d_size - function_name) == NULL)
9885 : : function_str = "???";
9886 : : else
9887 : 14 : function_str = (char *) str_data->d_buf + function_name;
9888 : :
9889 : 14 : fprintf (out, _(" set inlined context %u,"
9890 : : " function name %s (0x%x)\n"),
9891 : : context, function_str, function_name);
9892 : 14 : break;
9893 : : }
9894 : :
9895 : 0 : case DW_LNE_NVIDIA_set_function_name:
9896 : : {
9897 [ # # ]: 0 : if (unlikely (linep >= lineendp))
9898 : 0 : goto invalid_data;
9899 : :
9900 : 0 : unsigned int function_name;
9901 : 0 : get_uleb128 (function_name, linep, lineendp);
9902 : 0 : function_name += debug_str_offset;
9903 : :
9904 : 0 : Elf_Data *str_data = dbg->sectiondata[IDX_debug_str];
9905 : 0 : char *function_str;
9906 [ # # # # ]: 0 : if (str_data == NULL || function_name >= str_data->d_size
9907 [ # # ]: 0 : || memchr (str_data->d_buf + function_name, '\0',
9908 : : str_data->d_size - function_name) == NULL)
9909 : : function_str = "???";
9910 : : else
9911 : 0 : function_str = (char *) str_data->d_buf + function_name;
9912 : :
9913 : 0 : fprintf (out, _(" set function name %s (0x%x)\n"),
9914 : : function_str, function_name);
9915 : : }
9916 : 0 : break;
9917 : :
9918 : 0 : default:
9919 : : /* Unknown, ignore it. */
9920 : 0 : fputs (_(" unknown opcode\n"), out);
9921 : 0 : linep += len - 1;
9922 : 0 : break;
9923 : : }
9924 : : }
9925 [ + - ]: 1849460 : else if (opcode <= DW_LNS_set_isa)
9926 : : {
9927 : : /* This is a known standard opcode. */
9928 [ + + + + : 1849460 : switch (opcode)
+ + - + +
+ - - ]
9929 : : {
9930 : 287694 : case DW_LNS_copy:
9931 : : /* Takes no argument. */
9932 : 287694 : fprintf (out, " %s\n", copy_str);
9933 : 287694 : break;
9934 : :
9935 : 17802 : case DW_LNS_advance_pc:
9936 : : /* Takes one uleb128 parameter which is added to the
9937 : : address. */
9938 [ - + ]: 17802 : if (lineendp - linep < 1)
9939 : 0 : goto invalid_unit;
9940 : 17802 : get_uleb128 (u128, linep, lineendp);
9941 : 17802 : advance_pc (u128);
9942 : : {
9943 : 17802 : fprintf (out, " %s %u %s ",
9944 : : adv_addr_str, op_addr_advance, to_str);
9945 : 17802 : print_dwarf_addr (dwflmod, 0, address, address, out);
9946 [ - + ]: 17802 : if (op_index > 0)
9947 : 0 : fprintf (out, ", op_index %s %u", to_str, op_index);
9948 : 17802 : fprintf (out, "\n");
9949 : : }
9950 : 17802 : break;
9951 : :
9952 : 212056 : case DW_LNS_advance_line:
9953 : : /* Takes one sleb128 parameter which is added to the
9954 : : line. */
9955 [ - + ]: 212056 : if (lineendp - linep < 1)
9956 : 0 : goto invalid_unit;
9957 : 212056 : get_sleb128 (s128, linep, lineendp);
9958 : 212056 : line += s128;
9959 : 212056 : fprintf (out, "\
9960 : : %s %d to %" PRId64 "\n",
9961 : : adv_line_by_str, s128, (int64_t) line);
9962 : 212056 : break;
9963 : :
9964 : 72066 : case DW_LNS_set_file:
9965 : : /* Takes one uleb128 parameter which is stored in file. */
9966 [ - + ]: 72066 : if (lineendp - linep < 1)
9967 : 0 : goto invalid_unit;
9968 : 72066 : get_uleb128 (u128, linep, lineendp);
9969 : 72066 : fprintf (out, " %s %" PRIu64 "\n",
9970 : : set_file_str, (uint64_t) u128);
9971 : 72066 : break;
9972 : :
9973 : 700846 : case DW_LNS_set_column:
9974 : : /* Takes one uleb128 parameter which is stored in column. */
9975 [ + - - + ]: 700846 : if (unlikely (standard_opcode_lengths[opcode] != 1
9976 : : || lineendp - linep < 1))
9977 : 0 : goto invalid_unit;
9978 : :
9979 : 700846 : get_uleb128 (u128, linep, lineendp);
9980 : 700846 : fprintf (out, " %s %" PRIu64 "\n",
9981 : : set_col_str, (uint64_t) u128);
9982 : 700846 : break;
9983 : :
9984 : 497192 : case DW_LNS_negate_stmt:
9985 : : /* Takes no argument. */
9986 : 497192 : is_stmt = 1 - is_stmt;
9987 : 497192 : fprintf (out, " %s '%s' %s %" PRIuFAST8 "\n",
9988 : : set_str, "is_stmt", to_str, is_stmt);
9989 : 497192 : break;
9990 : :
9991 : 0 : case DW_LNS_set_basic_block:
9992 : : /* Takes no argument. */
9993 : 0 : fputs (_(" set basic block flag\n"), out);
9994 : 0 : break;
9995 : :
9996 : 61750 : case DW_LNS_const_add_pc:
9997 : : /* Takes no argument. */
9998 : :
9999 [ - + ]: 61750 : if (unlikely (line_range == 0))
10000 : 0 : goto invalid_unit;
10001 : :
10002 : 61750 : advance_pc ((255 - opcode_base) / line_range);
10003 : : {
10004 : 61750 : fprintf (out, " %s %u to ",
10005 : : adv_addr_by_str, op_addr_advance);
10006 : 61750 : print_dwarf_addr (dwflmod, 0, address, address, out);
10007 [ - + ]: 61750 : if (op_index > 0)
10008 : 0 : fprintf (out, _(", op_index to %u"), op_index);
10009 : 61750 : fprintf (out, "\n");
10010 : : }
10011 : 61750 : break;
10012 : :
10013 : 44 : case DW_LNS_fixed_advance_pc:
10014 : : /* Takes one 16 bit parameter which is added to the
10015 : : address. */
10016 [ + - - + ]: 44 : if (unlikely (standard_opcode_lengths[opcode] != 1
10017 : : || lineendp - linep < 2))
10018 : 0 : goto invalid_unit;
10019 : :
10020 [ - + ]: 44 : u128 = read_2ubyte_unaligned_inc (dbg, linep);
10021 : 44 : address += u128;
10022 : 44 : op_index = 0;
10023 : : {
10024 : 44 : fprintf (out, _("\
10025 : : advance address by fixed value %u to \n"),
10026 : : u128);
10027 : 44 : print_dwarf_addr (dwflmod, 0, address, address, out);
10028 : 44 : fprintf (out, "\n");
10029 : : }
10030 : 44 : break;
10031 : :
10032 : 10 : case DW_LNS_set_prologue_end:
10033 : : /* Takes no argument. */
10034 : 10 : fputs (_(" set prologue end flag\n"), out);
10035 : 10 : break;
10036 : :
10037 : 0 : case DW_LNS_set_epilogue_begin:
10038 : : /* Takes no argument. */
10039 : 0 : fputs (_(" set epilogue begin flag\n"), out);
10040 : 0 : break;
10041 : :
10042 : 0 : case DW_LNS_set_isa:
10043 : : /* Takes one uleb128 parameter which is stored in isa. */
10044 [ # # # # ]: 0 : if (unlikely (standard_opcode_lengths[opcode] != 1
10045 : : || lineendp - linep < 1))
10046 : 0 : goto invalid_unit;
10047 : :
10048 : 0 : get_uleb128 (u128, linep, lineendp);
10049 : 0 : fprintf (out, _(" set isa to %u\n"), u128);
10050 : 0 : break;
10051 : : }
10052 : : }
10053 : : else
10054 : : {
10055 : : /* This is a new opcode the generator but not we know about.
10056 : : Read the parameters associated with it but then discard
10057 : : everything. Read all the parameters for this opcode. */
10058 : 0 : fprintf (out,
10059 : 0 : ngettext (" unknown opcode with %" PRIu8 " parameter:",
10060 : : " unknown opcode with %" PRIu8 " parameters:",
10061 : : standard_opcode_lengths[opcode]),
10062 : 0 : standard_opcode_lengths[opcode]);
10063 : 0 : for (int n = standard_opcode_lengths[opcode];
10064 [ # # # # ]: 0 : n > 0 && linep < lineendp; --n)
10065 : : {
10066 : 0 : get_uleb128 (u128, linep, lineendp);
10067 [ # # ]: 0 : if (n != standard_opcode_lengths[opcode])
10068 : 0 : fputc (',', out);
10069 : 0 : fprintf (out, " %u", u128);
10070 : : }
10071 : :
10072 : : /* Next round, ignore this opcode. */
10073 : 0 : continue;
10074 : : }
10075 : : }
10076 : : }
10077 : :
10078 : : /* There must only be one data block. */
10079 [ - + ]: 104 : assert (elf_getdata (scn, data) == NULL);
10080 : : }
10081 : :
10082 : :
10083 : : static void
10084 : 40 : print_debug_loclists_section (Dwfl_Module *dwflmod,
10085 : : Ebl *ebl,
10086 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
10087 : : Elf_Scn *scn, GElf_Shdr *shdr,
10088 : : Dwarf *dbg, FILE *out)
10089 : : {
10090 : 40 : Elf_Data *data = get_debug_elf_data (dbg, ebl, IDX_debug_loclists, scn);
10091 [ + - ]: 40 : if (data == NULL)
10092 : 0 : return;
10093 : :
10094 : 40 : fprintf (out, _("\
10095 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
10096 : : elf_ndxscn (scn), section_name (ebl, shdr),
10097 : 40 : (uint64_t) shdr->sh_offset);
10098 : :
10099 : : /* For the listptr to get the base address/CU. */
10100 : 40 : sort_listptr (&known_loclistsptr, "loclistsptr");
10101 : 40 : size_t listptr_idx = 0;
10102 : :
10103 : 40 : const unsigned char *readp = data->d_buf;
10104 : 40 : const unsigned char *const dataend = ((unsigned char *) data->d_buf
10105 : 40 : + data->d_size);
10106 [ + + ]: 3606 : while (readp < dataend)
10107 : : {
10108 [ - + ]: 3566 : if (unlikely (readp > dataend - 4))
10109 : : {
10110 : 0 : invalid_data:
10111 : 0 : error (0, 0, _("invalid data in section [%zu] '%s'"),
10112 : : elf_ndxscn (scn), section_name (ebl, shdr));
10113 : 0 : return;
10114 : : }
10115 : :
10116 : 3566 : ptrdiff_t offset = readp - (unsigned char *) data->d_buf;
10117 : 3566 : fprintf (out, "%s 0x%" PRIx64 ":\n\n",
10118 : : table_offset_upper_str, (uint64_t) offset);
10119 : :
10120 [ - + ]: 3566 : uint64_t unit_length = read_4ubyte_unaligned_inc (dbg, readp);
10121 : 3566 : unsigned int offset_size = 4;
10122 [ - + ]: 3566 : if (unlikely (unit_length == 0xffffffff))
10123 : : {
10124 [ # # ]: 0 : if (unlikely (readp > dataend - 8))
10125 : 0 : goto invalid_data;
10126 : :
10127 [ # # ]: 0 : unit_length = read_8ubyte_unaligned_inc (dbg, readp);
10128 : 0 : offset_size = 8;
10129 : : }
10130 : 3566 : fprintf (out, " %s: %8" PRIu64 "\n", length_str, unit_length);
10131 : :
10132 : : /* We need at least 2-bytes + 1-byte + 1-byte + 4-bytes = 8
10133 : : bytes to complete the header. And this unit cannot go beyond
10134 : : the section data. */
10135 [ + - ]: 3566 : if (readp > dataend - 8
10136 [ + - ]: 3566 : || unit_length < 8
10137 [ - + ]: 3566 : || unit_length > (uint64_t) (dataend - readp))
10138 : 0 : goto invalid_data;
10139 : :
10140 : 3566 : const unsigned char *nexthdr = readp + unit_length;
10141 : :
10142 [ - + ]: 3566 : uint16_t version = read_2ubyte_unaligned_inc (dbg, readp);
10143 : 3566 : fprintf (out, " %s: %8" PRIu16 "\n", dwarf_ver_str, version);
10144 : :
10145 [ - + ]: 3566 : if (version != 5)
10146 : : {
10147 : 0 : error (0, 0, _("Unknown version"));
10148 : 0 : goto next_table;
10149 : : }
10150 : :
10151 : 3566 : uint8_t address_size = *readp++;
10152 : 3566 : fprintf (out, " %s: %8" PRIu64 "\n",
10153 : : addr_size_str, (uint64_t) address_size);
10154 : :
10155 [ - + ]: 3566 : if (address_size != 4 && address_size != 8)
10156 : : {
10157 : 0 : error (0, 0, _("unsupported address size"));
10158 : 0 : goto next_table;
10159 : : }
10160 : :
10161 : 3566 : uint8_t segment_size = *readp++;
10162 : 3566 : fprintf (out, " %s: %8" PRIu64 "\n",
10163 : : seg_size_str, (uint64_t) segment_size);
10164 : :
10165 [ - + ]: 3566 : if (segment_size != 0)
10166 : : {
10167 : 0 : error (0, 0, _("unsupported segment size"));
10168 : 0 : goto next_table;
10169 : : }
10170 : :
10171 [ - + ]: 3566 : uint32_t offset_entry_count = read_4ubyte_unaligned_inc (dbg, readp);
10172 : 3566 : fprintf (out, " %s: %8" PRIu64 "\n",
10173 : : offset_ent_str, (uint64_t) offset_entry_count);
10174 : :
10175 : : /* We need the CU that uses this unit to get the initial base address. */
10176 : 3566 : Dwarf_Addr cu_base = 0;
10177 : 3566 : struct Dwarf_CU *cu = NULL;
10178 [ - + ]: 3566 : if (listptr_cu (&known_loclistsptr, &listptr_idx,
10179 : : (Dwarf_Off) offset,
10180 : 3566 : (Dwarf_Off) (nexthdr - (unsigned char *) data->d_buf),
10181 : : &cu_base, &cu)
10182 [ # # ]: 0 : || split_dwarf_cu_base (dbg, &cu, &cu_base))
10183 : 3566 : {
10184 : 3566 : Dwarf_Die cudie;
10185 [ - + ]: 3566 : if (dwarf_cu_die (cu, &cudie,
10186 : : NULL, NULL, NULL, NULL,
10187 : : NULL, NULL) == NULL)
10188 : 0 : fprintf (out, " %s: ", unknown_base_str);
10189 : : else
10190 : 3566 : fprintf (out, " %s [%6" PRIx64 "] %s: ",
10191 : : cu_str, dwarf_dieoffset (&cudie), base_str);
10192 : 3566 : print_dwarf_addr (dwflmod, address_size, cu_base, cu_base, out);
10193 : 3566 : fprintf (out, "\n");
10194 : : }
10195 : : else
10196 : 0 : fprintf (out, _(" Not associated with a CU.\n"));
10197 : :
10198 : 3566 : fprintf (out, "\n");
10199 : :
10200 : 3566 : const unsigned char *offset_array_start = readp;
10201 [ + + ]: 3566 : if (offset_entry_count > 0)
10202 : : {
10203 : 4 : uint64_t max_entries = (unit_length - 8) / offset_size;
10204 [ - + ]: 4 : if (offset_entry_count > max_entries)
10205 : : {
10206 : 0 : error (0, 0,
10207 : 0 : _("too many offset entries for unit length"));
10208 : 0 : offset_entry_count = max_entries;
10209 : : }
10210 : :
10211 : 4 : fprintf (out, _(" Offsets starting at 0x%" PRIx64 ":\n"),
10212 : : (uint64_t) (offset_array_start
10213 : 4 : - (unsigned char *) data->d_buf));
10214 [ + + ]: 36 : for (uint32_t idx = 0; idx < offset_entry_count; idx++)
10215 : : {
10216 : 32 : fprintf (out, " [%6" PRIu32 "] ", idx);
10217 [ + - ]: 32 : if (offset_size == 4)
10218 : : {
10219 [ - + ]: 32 : uint32_t off = read_4ubyte_unaligned_inc (dbg, readp);
10220 : 32 : fprintf (out, "0x%" PRIx32 "\n", off);
10221 : : }
10222 : : else
10223 : : {
10224 [ # # ]: 0 : uint64_t off = read_8ubyte_unaligned_inc (dbg, readp);
10225 : 0 : fprintf (out, "0x%" PRIx64 "\n", off);
10226 : : }
10227 : : }
10228 : 4 : fprintf (out, "\n");
10229 : : }
10230 : :
10231 : 3566 : Dwarf_Addr base = cu_base;
10232 : 3566 : bool start_of_list = true;
10233 [ + + ]: 881470 : while (readp < nexthdr)
10234 : : {
10235 : 877904 : Dwarf_Off off = (Dwarf_Off) (readp - (unsigned char *) data->d_buf);
10236 [ + + ]: 877904 : if (listptr_attr (&known_loclistsptr, listptr_idx, off,
10237 : : DW_AT_GNU_locviews))
10238 : 125726 : {
10239 : 125726 : Dwarf_Off next_off = next_listptr_offset (&known_loclistsptr,
10240 : : &listptr_idx, off);
10241 : 125726 : const unsigned char *locp = readp;
10242 : 125726 : const unsigned char *locendp;
10243 [ + - ]: 125726 : if (next_off == 0
10244 [ + - ]: 125726 : || next_off > (size_t) (nexthdr - ((const unsigned char *)
10245 : : data->d_buf)))
10246 : : locendp = nexthdr;
10247 : : else
10248 : 125726 : locendp = (const unsigned char *) data->d_buf + next_off;
10249 : :
10250 : 125726 : fprintf (out, " Offset: %" PRIx64 ", Index: %" PRIx64 "\n",
10251 : : (uint64_t) (readp - (unsigned char *) data->d_buf),
10252 : 125726 : (uint64_t) (readp - offset_array_start));
10253 : :
10254 [ + + ]: 699754 : while (locp < locendp)
10255 : : {
10256 : 574028 : uint64_t v1, v2;
10257 : 574028 : get_uleb128 (v1, locp, locendp);
10258 [ - + ]: 574028 : if (locp >= locendp)
10259 : : {
10260 : 0 : fprintf (out, _(" <INVALID DATA>\n"));
10261 : 0 : break;
10262 : : }
10263 : 574028 : get_uleb128 (v2, locp, locendp);
10264 : 574028 : fprintf (out,
10265 : : " view pair %" PRId64 ", %" PRId64 "\n", v1, v2);
10266 : : }
10267 : :
10268 : 125726 : fprintf (out, "\n");
10269 : 125726 : readp = (unsigned char *) locendp;
10270 : 125726 : continue;
10271 : : }
10272 : :
10273 : 752178 : uint8_t kind = *readp++;
10274 : 752178 : uint64_t op1, op2, len;
10275 : :
10276 : : /* Skip padding. */
10277 [ - + ]: 752178 : if (start_of_list && kind == DW_LLE_end_of_list)
10278 : 0 : continue;
10279 : :
10280 [ + + ]: 752178 : if (start_of_list)
10281 : : {
10282 : 125790 : base = cu_base;
10283 : 125790 : fprintf (out, " Offset: %" PRIx64 ", Index: %" PRIx64 "\n",
10284 : 125790 : (uint64_t) (readp - (unsigned char *) data->d_buf - 1),
10285 : 125790 : (uint64_t) (readp - offset_array_start - 1));
10286 : 125790 : start_of_list = false;
10287 : : }
10288 : :
10289 : 752178 : fprintf (out, " %s", dwarf_loc_list_encoding_name (kind));
10290 [ + - - + : 752178 : switch (kind)
+ - + - +
- - ]
10291 : : {
10292 : 125790 : case DW_LLE_end_of_list:
10293 : 125790 : start_of_list = true;
10294 : 125790 : fprintf (out, "\n\n");
10295 : 125790 : break;
10296 : :
10297 : 0 : case DW_LLE_base_addressx:
10298 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
10299 : : {
10300 : 0 : invalid_entry:
10301 : 0 : error (0, 0, _("invalid loclists data"));
10302 : 0 : goto next_table;
10303 : : }
10304 : 0 : get_uleb128 (op1, readp, nexthdr);
10305 : 0 : fprintf (out, " %" PRIx64 "\n", op1);
10306 [ # # ]: 0 : if (! print_unresolved_addresses)
10307 : : {
10308 : 0 : Dwarf_Addr addr;
10309 [ # # ]: 0 : if (get_indexed_addr (cu, op1, &addr) != 0)
10310 : 0 : fprintf (out, " ???\n");
10311 : : else
10312 : : {
10313 : 0 : fprintf (out, " ");
10314 : 0 : print_dwarf_addr (dwflmod, address_size, addr, addr, out);
10315 : 0 : fprintf (out, "\n");
10316 : : }
10317 : : }
10318 : : break;
10319 : :
10320 : 0 : case DW_LLE_startx_endx:
10321 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
10322 : 0 : goto invalid_entry;
10323 : 0 : get_uleb128 (op1, readp, nexthdr);
10324 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
10325 : 0 : goto invalid_entry;
10326 : 0 : get_uleb128 (op2, readp, nexthdr);
10327 : 0 : fprintf (out, " %" PRIx64 ", %" PRIx64 "\n", op1, op2);
10328 [ # # ]: 0 : if (! print_unresolved_addresses)
10329 : : {
10330 : 0 : Dwarf_Addr addr1;
10331 : 0 : Dwarf_Addr addr2;
10332 [ # # ]: 0 : if (get_indexed_addr (cu, op1, &addr1) != 0
10333 [ # # ]: 0 : || get_indexed_addr (cu, op2, &addr2) != 0)
10334 : : {
10335 : 0 : fprintf (out, " ???..\n");
10336 : 0 : fprintf (out, " ???\n");
10337 : : }
10338 : : else
10339 : : {
10340 : 0 : fprintf (out, " ");
10341 : 0 : print_dwarf_addr (dwflmod, address_size,
10342 : : addr1, addr1, out);
10343 : 0 : fprintf (out, "..\n ");
10344 : 0 : print_dwarf_addr (dwflmod, address_size,
10345 : : addr2 - 1, addr2, out);
10346 : 0 : fprintf (out, "\n");
10347 : : }
10348 : : }
10349 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
10350 : 0 : goto invalid_entry;
10351 : 0 : get_uleb128 (len, readp, nexthdr);
10352 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < len)
10353 : 0 : goto invalid_entry;
10354 : 0 : print_ops (dwflmod, dbg, 8, 8, version,
10355 : : address_size, offset_size, cu, len, readp, out);
10356 : 0 : readp += len;
10357 : 0 : break;
10358 : :
10359 : 52 : case DW_LLE_startx_length:
10360 [ - + ]: 52 : if ((uint64_t) (nexthdr - readp) < 1)
10361 : 0 : goto invalid_entry;
10362 : 52 : get_uleb128 (op1, readp, nexthdr);
10363 [ - + ]: 52 : if ((uint64_t) (nexthdr - readp) < 1)
10364 : 0 : goto invalid_entry;
10365 : 52 : get_uleb128 (op2, readp, nexthdr);
10366 : 52 : fprintf (out, " %" PRIx64 ", %" PRIx64 "\n", op1, op2);
10367 [ + - ]: 52 : if (! print_unresolved_addresses)
10368 : : {
10369 : 52 : Dwarf_Addr addr1;
10370 : 52 : Dwarf_Addr addr2;
10371 [ - + ]: 52 : if (get_indexed_addr (cu, op1, &addr1) != 0)
10372 : : {
10373 : 0 : fprintf (out, " ???..\n");
10374 : 0 : fprintf (out, " ???\n");
10375 : : }
10376 : : else
10377 : : {
10378 : 52 : addr2 = addr1 + op2;
10379 : 52 : fprintf (out, " ");
10380 : 52 : print_dwarf_addr (dwflmod, address_size, addr1, addr1, out);
10381 : 52 : fprintf (out, "..\n ");
10382 : 52 : print_dwarf_addr (dwflmod, address_size,
10383 : : addr2 - 1, addr2, out);
10384 : 52 : fprintf (out, "\n");
10385 : : }
10386 : : }
10387 [ - + ]: 52 : if ((uint64_t) (nexthdr - readp) < 1)
10388 : 0 : goto invalid_entry;
10389 : 52 : get_uleb128 (len, readp, nexthdr);
10390 [ - + ]: 52 : if ((uint64_t) (nexthdr - readp) < len)
10391 : 0 : goto invalid_entry;
10392 : 52 : print_ops (dwflmod, dbg, 8, 8, version,
10393 : : address_size, offset_size, cu, len, readp, out);
10394 : 52 : readp += len;
10395 : 52 : break;
10396 : :
10397 : 549468 : case DW_LLE_offset_pair:
10398 [ - + ]: 549468 : if ((uint64_t) (nexthdr - readp) < 1)
10399 : 0 : goto invalid_entry;
10400 : 549468 : get_uleb128 (op1, readp, nexthdr);
10401 [ - + ]: 549468 : if ((uint64_t) (nexthdr - readp) < 1)
10402 : 0 : goto invalid_entry;
10403 : 549468 : get_uleb128 (op2, readp, nexthdr);
10404 : 549468 : fprintf (out, " %" PRIx64 ", %" PRIx64 "\n", op1, op2);
10405 [ + - ]: 549468 : if (! print_unresolved_addresses)
10406 : : {
10407 : 549468 : op1 += base;
10408 : 549468 : op2 += base;
10409 : 549468 : fprintf (out, " ");
10410 : 549468 : print_dwarf_addr (dwflmod, address_size, op1, op1, out);
10411 : 549468 : fprintf (out, "..\n ");
10412 : 549468 : print_dwarf_addr (dwflmod, address_size, op2 - 1, op2, out);
10413 : 549468 : fprintf (out, "\n");
10414 : : }
10415 [ - + ]: 549468 : if ((uint64_t) (nexthdr - readp) < 1)
10416 : 0 : goto invalid_entry;
10417 : 549468 : get_uleb128 (len, readp, nexthdr);
10418 [ - + ]: 549468 : if ((uint64_t) (nexthdr - readp) < len)
10419 : 0 : goto invalid_entry;
10420 : 549468 : print_ops (dwflmod, dbg, 8, 8, version,
10421 : : address_size, offset_size, cu, len, readp, out);
10422 : 549468 : readp += len;
10423 : 549468 : break;
10424 : :
10425 : 0 : case DW_LLE_default_location:
10426 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
10427 : 0 : goto invalid_entry;
10428 : 0 : get_uleb128 (len, readp, nexthdr);
10429 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < len)
10430 : 0 : goto invalid_entry;
10431 : 0 : print_ops (dwflmod, dbg, 8, 8, version,
10432 : : address_size, offset_size, cu, len, readp, out);
10433 : 0 : readp += len;
10434 : 0 : break;
10435 : :
10436 : 52256 : case DW_LLE_base_address:
10437 [ - + ]: 52256 : if (address_size == 4)
10438 : : {
10439 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 4)
10440 : 0 : goto invalid_entry;
10441 [ # # ]: 0 : op1 = read_4ubyte_unaligned_inc (dbg, readp);
10442 : : }
10443 : : else
10444 : : {
10445 [ - + ]: 52256 : if ((uint64_t) (nexthdr - readp) < 8)
10446 : 0 : goto invalid_entry;
10447 [ - + ]: 52256 : op1 = read_8ubyte_unaligned_inc (dbg, readp);
10448 : : }
10449 : 52256 : base = op1;
10450 : 52256 : fprintf (out, " 0x%" PRIx64 "\n", base);
10451 [ + - ]: 52256 : if (! print_unresolved_addresses)
10452 : : {
10453 : 52256 : fprintf (out, " ");
10454 : 52256 : print_dwarf_addr (dwflmod, address_size, base, base, out);
10455 : 52256 : fprintf (out, "\n");
10456 : : }
10457 : : break;
10458 : :
10459 : 0 : case DW_LLE_start_end:
10460 [ # # ]: 0 : if (address_size == 4)
10461 : : {
10462 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 8)
10463 : 0 : goto invalid_entry;
10464 [ # # ]: 0 : op1 = read_4ubyte_unaligned_inc (dbg, readp);
10465 [ # # ]: 0 : op2 = read_4ubyte_unaligned_inc (dbg, readp);
10466 : : }
10467 : : else
10468 : : {
10469 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 16)
10470 : 0 : goto invalid_entry;
10471 [ # # ]: 0 : op1 = read_8ubyte_unaligned_inc (dbg, readp);
10472 [ # # ]: 0 : op2 = read_8ubyte_unaligned_inc (dbg, readp);
10473 : : }
10474 : 0 : fprintf (out, " 0x%" PRIx64 "..0x%" PRIx64 "\n", op1, op2);
10475 [ # # ]: 0 : if (! print_unresolved_addresses)
10476 : : {
10477 : 0 : fprintf (out, " ");
10478 : 0 : print_dwarf_addr (dwflmod, address_size, op1, op1, out);
10479 : 0 : fprintf (out, "..\n ");
10480 : 0 : print_dwarf_addr (dwflmod, address_size, op2 - 1, op2, out);
10481 : 0 : fprintf (out, "\n");
10482 : : }
10483 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
10484 : 0 : goto invalid_entry;
10485 : 0 : get_uleb128 (len, readp, nexthdr);
10486 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < len)
10487 : 0 : goto invalid_entry;
10488 : 0 : print_ops (dwflmod, dbg, 8, 8, version,
10489 : : address_size, offset_size, cu, len, readp, out);
10490 : 0 : readp += len;
10491 : 0 : break;
10492 : :
10493 : 24612 : case DW_LLE_start_length:
10494 [ - + ]: 24612 : if (address_size == 4)
10495 : : {
10496 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 4)
10497 : 0 : goto invalid_entry;
10498 [ # # ]: 0 : op1 = read_4ubyte_unaligned_inc (dbg, readp);
10499 : : }
10500 : : else
10501 : : {
10502 [ - + ]: 24612 : if ((uint64_t) (nexthdr - readp) < 8)
10503 : 0 : goto invalid_entry;
10504 [ - + ]: 24612 : op1 = read_8ubyte_unaligned_inc (dbg, readp);
10505 : : }
10506 [ - + ]: 24612 : if ((uint64_t) (nexthdr - readp) < 1)
10507 : 0 : goto invalid_entry;
10508 : 24612 : get_uleb128 (op2, readp, nexthdr);
10509 : 24612 : fprintf (out, " 0x%" PRIx64 ", %" PRIx64 "\n", op1, op2);
10510 [ + + ]: 24612 : if (! print_unresolved_addresses)
10511 : : {
10512 : 24610 : op2 = op1 + op2;
10513 : 24610 : fprintf (out, " ");
10514 : 24610 : print_dwarf_addr (dwflmod, address_size, op1, op1, out);
10515 : 24610 : fprintf (out, "..\n ");
10516 : 24610 : print_dwarf_addr (dwflmod, address_size, op2 - 1, op2, out);
10517 : 24610 : fprintf (out, "\n");
10518 : : }
10519 [ - + ]: 24612 : if ((uint64_t) (nexthdr - readp) < 1)
10520 : 0 : goto invalid_entry;
10521 : 24612 : get_uleb128 (len, readp, nexthdr);
10522 [ - + ]: 24612 : if ((uint64_t) (nexthdr - readp) < len)
10523 : 0 : goto invalid_entry;
10524 : 24612 : print_ops (dwflmod, dbg, 8, 8, version,
10525 : : address_size, offset_size, cu, len, readp, out);
10526 : 24612 : readp += len;
10527 : 24612 : break;
10528 : :
10529 : 0 : case DW_LLE_GNU_view_pair:
10530 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
10531 : 0 : goto invalid_entry;
10532 : 0 : get_uleb128 (op1, readp, nexthdr);
10533 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
10534 : 0 : goto invalid_entry;
10535 : 0 : get_uleb128 (op2, readp, nexthdr);
10536 : 0 : fprintf (out, " %" PRIx64 ", %" PRIx64 "\n", op1, op2);
10537 : 0 : break;
10538 : :
10539 : 0 : default:
10540 : 0 : goto invalid_entry;
10541 : : }
10542 : : }
10543 : :
10544 : 3566 : next_table:
10545 [ - + ]: 3566 : if (readp != nexthdr)
10546 : : {
10547 : 0 : size_t padding = nexthdr - readp;
10548 : 0 : fprintf (out, _(" %zu padding bytes\n\n"), padding);
10549 : 0 : readp = nexthdr;
10550 : : }
10551 : : }
10552 : : }
10553 : :
10554 : :
10555 : : static void
10556 : 52 : print_debug_loc_section (Dwfl_Module *dwflmod,
10557 : : Ebl *ebl, GElf_Ehdr *ehdr,
10558 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg,
10559 : : FILE *out)
10560 : : {
10561 : 52 : Elf_Data *data = get_debug_elf_data (dbg, ebl, IDX_debug_loc, scn);
10562 [ - + ]: 52 : if (data == NULL)
10563 : 0 : return;
10564 : :
10565 : 52 : fprintf (out, _("\
10566 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
10567 : : elf_ndxscn (scn), section_name (ebl, shdr),
10568 : 52 : (uint64_t) shdr->sh_offset);
10569 : :
10570 : 52 : sort_listptr (&known_locsptr, "loclistptr");
10571 : 52 : size_t listptr_idx = 0;
10572 : :
10573 [ + - ]: 52 : uint_fast8_t address_size = ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 4 : 8;
10574 : 52 : uint_fast8_t offset_size = 4;
10575 : :
10576 : 52 : bool first = true;
10577 : 52 : Dwarf_Addr base = 0;
10578 : 52 : unsigned char *readp = data->d_buf;
10579 : 52 : unsigned char *const endp = (unsigned char *) data->d_buf + data->d_size;
10580 : 52 : Dwarf_CU *last_cu = NULL;
10581 [ + + ]: 490 : while (readp < endp)
10582 : : {
10583 : 438 : ptrdiff_t offset = readp - (unsigned char *) data->d_buf;
10584 : 438 : Dwarf_CU *cu = last_cu;
10585 : 438 : unsigned int attr = 0;
10586 : :
10587 [ + + - + ]: 438 : if (first && skip_listptr_hole (&known_locsptr, &listptr_idx,
10588 : : &address_size, &offset_size, &base,
10589 : : &cu, offset, &readp, endp, &attr))
10590 : 0 : continue;
10591 : :
10592 [ + + ]: 438 : if (last_cu != cu)
10593 : : {
10594 : 60 : Dwarf_Die cudie;
10595 [ - + ]: 60 : if (dwarf_cu_die (cu, &cudie,
10596 : : NULL, NULL, NULL, NULL,
10597 : : NULL, NULL) == NULL)
10598 : 0 : fprintf (out, _("\n Unknown CU base: "));
10599 : : else
10600 : 60 : fprintf (out, _("\n CU [%6" PRIx64 "] base: "),
10601 : : dwarf_dieoffset (&cudie));
10602 : 60 : print_dwarf_addr (dwflmod, address_size, base, base, out);
10603 : 60 : fprintf (out, "\n");
10604 : : }
10605 : 438 : last_cu = cu;
10606 : :
10607 [ - + ]: 438 : if (attr == DW_AT_GNU_locviews)
10608 : 0 : {
10609 : 0 : Dwarf_Off next_off = next_listptr_offset (&known_locsptr,
10610 : : &listptr_idx, offset);
10611 : 0 : const unsigned char *locp = readp;
10612 : 0 : const unsigned char *locendp;
10613 [ # # ]: 0 : if (next_off == 0
10614 : 0 : || next_off > (size_t) (endp
10615 [ # # ]: 0 : - (const unsigned char *) data->d_buf))
10616 : 0 : locendp = endp;
10617 : : else
10618 : 0 : locendp = (const unsigned char *) data->d_buf + next_off;
10619 : :
10620 [ # # ]: 0 : while (locp < locendp)
10621 : : {
10622 : 0 : uint64_t v1, v2;
10623 : 0 : get_uleb128 (v1, locp, locendp);
10624 [ # # ]: 0 : if (locp >= locendp)
10625 : : {
10626 : 0 : fprintf (out, _(" [%6tx] <INVALID DATA>\n"), offset);
10627 : 0 : break;
10628 : : }
10629 : 0 : get_uleb128 (v2, locp, locendp);
10630 [ # # ]: 0 : if (first) /* First view pair in a list. */
10631 : 0 : fprintf (out, " [%6tx] ", offset);
10632 : : else
10633 : 0 : fprintf (out, " ");
10634 : 0 : fprintf (out, "view pair %" PRId64 ", %" PRId64 "\n", v1, v2);
10635 : 0 : first = false;
10636 : : }
10637 : :
10638 : 0 : first = true;
10639 : 0 : readp = (unsigned char *) locendp;
10640 : 0 : continue;
10641 : : }
10642 : :
10643 : : /* GNU DebugFission encoded addresses as addrx. */
10644 : 1194 : bool is_debugfission = ((cu != NULL
10645 [ # # ]: 0 : || split_dwarf_cu_base (dbg, &cu, &base))
10646 [ - + + - ]: 438 : && (cu->version < 5
10647 [ + + ]: 438 : && cu->unit_type == DW_UT_split_compile));
10648 : 318 : if (!is_debugfission
10649 [ - + ]: 318 : && unlikely (data->d_size - offset < (size_t) address_size * 2))
10650 : : {
10651 : 0 : invalid_data:
10652 : 0 : fprintf (out, _(" [%6tx] <INVALID DATA>\n"), offset);
10653 : 0 : break;
10654 : : }
10655 : :
10656 : 438 : Dwarf_Addr begin;
10657 : 438 : Dwarf_Addr end;
10658 : 438 : bool use_base = true;
10659 [ + + ]: 438 : if (is_debugfission)
10660 : : {
10661 : 120 : const unsigned char *locp = readp;
10662 : 120 : const unsigned char *locendp = readp + data->d_size;
10663 [ - + ]: 120 : if (locp >= locendp)
10664 : 0 : goto invalid_data;
10665 : :
10666 : 120 : Dwarf_Word idx;
10667 : 120 : unsigned char code = *locp++;
10668 [ + - - + : 120 : switch (code)
- ]
10669 : : {
10670 : 40 : case DW_LLE_GNU_end_of_list_entry:
10671 : 40 : begin = 0;
10672 : 40 : end = 0;
10673 : 40 : break;
10674 : :
10675 : 0 : case DW_LLE_GNU_base_address_selection_entry:
10676 [ # # ]: 0 : if (locp >= locendp)
10677 : 0 : goto invalid_data;
10678 : 0 : begin = (Dwarf_Addr) -1;
10679 : 0 : get_uleb128 (idx, locp, locendp);
10680 [ # # ]: 0 : if (get_indexed_addr (cu, idx, &end) != 0)
10681 : 0 : end = idx; /* ... */
10682 : : break;
10683 : :
10684 : 0 : case DW_LLE_GNU_start_end_entry:
10685 [ # # ]: 0 : if (locp >= locendp)
10686 : 0 : goto invalid_data;
10687 : 0 : get_uleb128 (idx, locp, locendp);
10688 [ # # ]: 0 : if (get_indexed_addr (cu, idx, &begin) != 0)
10689 : 0 : begin = idx; /* ... */
10690 [ # # ]: 0 : if (locp >= locendp)
10691 : 0 : goto invalid_data;
10692 : 0 : get_uleb128 (idx, locp, locendp);
10693 [ # # ]: 0 : if (get_indexed_addr (cu, idx, &end) != 0)
10694 : 0 : end = idx; /* ... */
10695 : : use_base = false;
10696 : : break;
10697 : :
10698 : 80 : case DW_LLE_GNU_start_length_entry:
10699 [ - + ]: 80 : if (locp >= locendp)
10700 : 0 : goto invalid_data;
10701 : 80 : get_uleb128 (idx, locp, locendp);
10702 [ - + ]: 80 : if (get_indexed_addr (cu, idx, &begin) != 0)
10703 : 0 : begin = idx; /* ... */
10704 [ - + ]: 80 : if (locendp - locp < 4)
10705 : 0 : goto invalid_data;
10706 [ - + ]: 80 : end = read_4ubyte_unaligned_inc (dbg, locp);
10707 : 80 : end += begin;
10708 : 80 : use_base = false;
10709 : 80 : break;
10710 : :
10711 : 0 : default:
10712 : 0 : goto invalid_data;
10713 : : }
10714 : :
10715 : 120 : readp = (unsigned char *) locp;
10716 : : }
10717 [ + - ]: 318 : else if (address_size == 8)
10718 : : {
10719 [ + + ]: 318 : begin = read_8ubyte_unaligned_inc (dbg, readp);
10720 [ + + ]: 318 : end = read_8ubyte_unaligned_inc (dbg, readp);
10721 : : }
10722 : : else
10723 : : {
10724 [ # # ]: 0 : begin = read_4ubyte_unaligned_inc (dbg, readp);
10725 [ # # ]: 0 : end = read_4ubyte_unaligned_inc (dbg, readp);
10726 [ # # ]: 0 : if (begin == (Dwarf_Addr) (uint32_t) -1)
10727 : 0 : begin = (Dwarf_Addr) -1l;
10728 : : }
10729 : :
10730 [ - + ]: 438 : if (begin == (Dwarf_Addr) -1l) /* Base address entry. */
10731 : : {
10732 [ # # ]: 0 : if (first)
10733 : 0 : fprintf (out, " [%6tx] ", offset);
10734 : : else
10735 : 0 : fprintf (out, " ");
10736 : 0 : fputs (_("base address\n"), out);
10737 : 0 : fprintf (out, " ");
10738 : 0 : print_dwarf_addr (dwflmod, address_size, end, end, out);
10739 : 0 : fprintf (out, "\n");
10740 : 0 : base = end;
10741 : 0 : first = false;
10742 : : }
10743 [ + + + + ]: 438 : else if (begin == 0 && end == 0) /* End of list entry. */
10744 : : {
10745 [ - + ]: 148 : if (first)
10746 : 0 : fprintf (out, _(" [%6tx] empty list\n"), offset);
10747 : : first = true;
10748 : : }
10749 : : else
10750 : : {
10751 : : /* We have a location expression entry. */
10752 [ + + ]: 290 : uint_fast16_t len = read_2ubyte_unaligned_inc (dbg, readp);
10753 : :
10754 [ + + ]: 290 : if (first) /* First entry in a list. */
10755 : 148 : fprintf (out, " [%6tx] ", offset);
10756 : : else
10757 : 142 : fprintf (out, " ");
10758 : :
10759 : 290 : fprintf (out, "range %" PRIx64 ", %" PRIx64 "\n", begin, end);
10760 [ + + ]: 290 : if (! print_unresolved_addresses)
10761 : : {
10762 : 230 : Dwarf_Addr dab = use_base ? base + begin : begin;
10763 : 230 : Dwarf_Addr dae = use_base ? base + end : end;
10764 : 230 : fprintf (out, " ");
10765 : 230 : print_dwarf_addr (dwflmod, address_size, dab, dab, out);
10766 : 230 : fprintf (out, "..\n ");
10767 : 230 : print_dwarf_addr (dwflmod, address_size, dae - 1, dae, out);
10768 : 230 : fprintf (out, "\n");
10769 : : }
10770 : :
10771 [ - + ]: 290 : if (endp - readp <= (ptrdiff_t) len)
10772 : : {
10773 : 0 : fputs (_(" <INVALID DATA>\n"), out);
10774 : 0 : break;
10775 : : }
10776 : :
10777 [ + - ]: 580 : print_ops (dwflmod, dbg, 11, 11,
10778 : 290 : cu != NULL ? cu->version : 3,
10779 : : address_size, offset_size, cu, len, readp, out);
10780 : :
10781 : 290 : first = false;
10782 : 290 : readp += len;
10783 : : }
10784 : : }
10785 : : }
10786 : :
10787 : : struct mac_culist
10788 : : {
10789 : : Dwarf_Die die;
10790 : : Dwarf_Off offset;
10791 : : Dwarf_Files *files;
10792 : : struct mac_culist *next;
10793 : : };
10794 : :
10795 : :
10796 : : static int
10797 : 0 : mac_compare (const void *p1, const void *p2)
10798 : : {
10799 : 0 : struct mac_culist *m1 = (struct mac_culist *) p1;
10800 : 0 : struct mac_culist *m2 = (struct mac_culist *) p2;
10801 : :
10802 [ # # ]: 0 : if (m1->offset < m2->offset)
10803 : : return -1;
10804 [ # # ]: 0 : if (m1->offset > m2->offset)
10805 : 0 : return 1;
10806 : : return 0;
10807 : : }
10808 : :
10809 : :
10810 : : static void
10811 : 0 : print_debug_macinfo_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
10812 : : Ebl *ebl,
10813 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
10814 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg,
10815 : : FILE *out)
10816 : : {
10817 : 0 : Elf_Data *data = get_debug_elf_data (dbg, ebl, IDX_debug_macinfo, scn);
10818 [ # # ]: 0 : if (data == NULL)
10819 : 0 : return;
10820 : :
10821 : 0 : fprintf (out, _("\
10822 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
10823 : : elf_ndxscn (scn), section_name (ebl, shdr),
10824 : 0 : (uint64_t) shdr->sh_offset);
10825 : 0 : fputc ('\n', out);
10826 : :
10827 : : /* There is no function in libdw to iterate over the raw content of
10828 : : the section but it is easy enough to do. */
10829 : :
10830 : : /* Get the source file information for all CUs. */
10831 : 0 : Dwarf_Off offset;
10832 : 0 : Dwarf_Off ncu = 0;
10833 : 0 : size_t hsize;
10834 : 0 : struct mac_culist *culist = NULL;
10835 : 0 : size_t nculist = 0;
10836 [ # # ]: 0 : while (dwarf_nextcu (dbg, offset = ncu, &ncu, &hsize, NULL, NULL, NULL) == 0)
10837 : : {
10838 : 0 : Dwarf_Die cudie;
10839 [ # # ]: 0 : if (dwarf_offdie (dbg, offset + hsize, &cudie) == NULL)
10840 : 0 : continue;
10841 : :
10842 : 0 : Dwarf_Attribute attr;
10843 [ # # ]: 0 : if (dwarf_attr (&cudie, DW_AT_macro_info, &attr) == NULL)
10844 : 0 : continue;
10845 : :
10846 : 0 : Dwarf_Word macoff;
10847 [ # # ]: 0 : if (dwarf_formudata (&attr, &macoff) != 0)
10848 : 0 : continue;
10849 : :
10850 : 0 : struct mac_culist *newp = (struct mac_culist *) alloca (sizeof (*newp));
10851 : 0 : newp->die = cudie;
10852 : 0 : newp->offset = macoff;
10853 : 0 : newp->files = NULL;
10854 : 0 : newp->next = culist;
10855 : 0 : culist = newp;
10856 : 0 : ++nculist;
10857 : : }
10858 : :
10859 : : /* Convert the list into an array for easier consumption. */
10860 : 0 : struct mac_culist *cus = (struct mac_culist *) alloca ((nculist + 1)
10861 : : * sizeof (*cus));
10862 : : /* Add sentinel. */
10863 : 0 : cus[nculist].offset = data->d_size;
10864 : 0 : cus[nculist].files = (Dwarf_Files *) -1l;
10865 [ # # ]: 0 : if (nculist > 0)
10866 : : {
10867 [ # # ]: 0 : for (size_t cnt = nculist - 1; culist != NULL; --cnt)
10868 : : {
10869 [ # # ]: 0 : assert (cnt < nculist);
10870 : 0 : cus[cnt] = *culist;
10871 : 0 : culist = culist->next;
10872 : : }
10873 : :
10874 : : /* Sort the array according to the offset in the .debug_macinfo
10875 : : section. Note we keep the sentinel at the end. */
10876 : 0 : qsort (cus, nculist, sizeof (*cus), mac_compare);
10877 : : }
10878 : :
10879 : 0 : const unsigned char *readp = (const unsigned char *) data->d_buf;
10880 : 0 : const unsigned char *readendp = readp + data->d_size;
10881 : 0 : int level = 1;
10882 : :
10883 [ # # ]: 0 : while (readp < readendp)
10884 : : {
10885 : 0 : unsigned int opcode = *readp++;
10886 : 0 : unsigned int u128;
10887 : 0 : unsigned int u128_2;
10888 : 0 : const unsigned char *endp;
10889 : :
10890 [ # # # # ]: 0 : switch (opcode)
10891 : : {
10892 : 0 : case DW_MACINFO_define:
10893 : : case DW_MACINFO_undef:
10894 : : case DW_MACINFO_vendor_ext:
10895 : : /* For the first two opcodes the parameters are
10896 : : line, string
10897 : : For the latter
10898 : : number, string.
10899 : : We can treat these cases together. */
10900 : 0 : get_uleb128 (u128, readp, readendp);
10901 : :
10902 : 0 : endp = memchr (readp, '\0', readendp - readp);
10903 [ # # ]: 0 : if (unlikely (endp == NULL))
10904 : : {
10905 : 0 : fprintf (out, _("\
10906 : : %*s*** non-terminated string at end of section"),
10907 : : level, "");
10908 : 0 : return;
10909 : : }
10910 : :
10911 [ # # ]: 0 : if (opcode == DW_MACINFO_define)
10912 : 0 : fprintf (out, "%*s#define %s, line %u\n",
10913 : : level, "", (char *) readp, u128);
10914 [ # # ]: 0 : else if (opcode == DW_MACINFO_undef)
10915 : 0 : fprintf (out, "%*s#undef %s, line %u\n",
10916 : : level, "", (char *) readp, u128);
10917 : : else
10918 : 0 : fprintf (out, " #vendor-ext %s, number %u\n", (char *) readp, u128);
10919 : :
10920 : 0 : readp = endp + 1;
10921 : 0 : break;
10922 : :
10923 : 0 : case DW_MACINFO_start_file:
10924 : : /* The two parameters are line and file index, in this order. */
10925 : 0 : get_uleb128 (u128, readp, readendp);
10926 [ # # ]: 0 : if (readendp - readp < 1)
10927 : : {
10928 : 0 : fprintf (out, _("\
10929 : : %*s*** missing DW_MACINFO_start_file argument at end of section"),
10930 : : level, "");
10931 : 0 : return;
10932 : : }
10933 : 0 : get_uleb128 (u128_2, readp, readendp);
10934 : :
10935 : : /* Find the CU DIE for this file. */
10936 : 0 : size_t macoff = readp - (const unsigned char *) data->d_buf;
10937 : 0 : const char *fname = "???";
10938 [ # # # # ]: 0 : if (macoff >= cus[0].offset && cus[0].offset != data->d_size)
10939 : : {
10940 [ # # # # ]: 0 : while (macoff >= cus[1].offset && cus[1].offset != data->d_size)
10941 : 0 : ++cus;
10942 : :
10943 [ # # ]: 0 : if (cus[0].files == NULL
10944 [ # # ]: 0 : && dwarf_getsrcfiles (&cus[0].die, &cus[0].files, NULL) != 0)
10945 : 0 : cus[0].files = (Dwarf_Files *) -1l;
10946 : :
10947 [ # # ]: 0 : if (cus[0].files != (Dwarf_Files *) -1l)
10948 : 0 : fname = (dwarf_filesrc (cus[0].files, u128_2, NULL, NULL)
10949 [ # # ]: 0 : ?: "???");
10950 : : }
10951 : :
10952 : 0 : fprintf (out, "%*sstart_file %u, [%u] %s\n",
10953 : : level, "", u128, u128_2, fname);
10954 : 0 : ++level;
10955 : 0 : break;
10956 : :
10957 : 0 : case DW_MACINFO_end_file:
10958 : 0 : --level;
10959 : 0 : fprintf (out, "%*send_file\n", level, "");
10960 : : /* Nothing more to do. */
10961 : 0 : break;
10962 : :
10963 : 0 : default:
10964 : : // XXX gcc seems to generate files with a trailing zero.
10965 [ # # # # ]: 0 : if (unlikely (opcode != 0 || readp != readendp))
10966 : 0 : fprintf (out, "%*s*** invalid opcode %u\n", level, "", opcode);
10967 : : break;
10968 : : }
10969 : : }
10970 : : }
10971 : :
10972 : :
10973 : : static void
10974 : 8 : print_debug_macro_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
10975 : : Ebl *ebl,
10976 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
10977 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg,
10978 : : FILE *out)
10979 : : {
10980 : 8 : Elf_Data *data = get_debug_elf_data (dbg, ebl, IDX_debug_macro, scn);
10981 [ + - ]: 8 : if (data == NULL)
10982 : 0 : return;
10983 : :
10984 : 8 : fprintf (out, _("\
10985 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
10986 : : elf_ndxscn (scn), section_name (ebl, shdr),
10987 : 8 : (uint64_t) shdr->sh_offset);
10988 : 8 : fputc ('\n', out);
10989 : :
10990 : : /* Get the source file information for all CUs. Uses same
10991 : : datastructure as macinfo. But uses offset field to directly
10992 : : match .debug_line offset. And just stored in a list. */
10993 : 8 : Dwarf_Off offset;
10994 : 8 : Dwarf_Off ncu = 0;
10995 : 8 : size_t hsize;
10996 : 8 : struct mac_culist *culist = NULL;
10997 [ + + ]: 20 : while (dwarf_nextcu (dbg, offset = ncu, &ncu, &hsize, NULL, NULL, NULL) == 0)
10998 : : {
10999 : 12 : Dwarf_Die cudie;
11000 [ - + ]: 12 : if (dwarf_offdie (dbg, offset + hsize, &cudie) == NULL)
11001 : 0 : continue;
11002 : :
11003 : 12 : Dwarf_Attribute attr;
11004 [ - + ]: 12 : if (dwarf_attr (&cudie, DW_AT_stmt_list, &attr) == NULL)
11005 : 0 : continue;
11006 : :
11007 : 12 : Dwarf_Word lineoff;
11008 [ - + ]: 12 : if (dwarf_formudata (&attr, &lineoff) != 0)
11009 : 0 : continue;
11010 : :
11011 : 12 : struct mac_culist *newp = (struct mac_culist *) alloca (sizeof (*newp));
11012 : 12 : newp->die = cudie;
11013 : 12 : newp->offset = lineoff;
11014 : 12 : newp->files = NULL;
11015 : 12 : newp->next = culist;
11016 : 12 : culist = newp;
11017 : : }
11018 : :
11019 : 8 : const unsigned char *readp = (const unsigned char *) data->d_buf;
11020 : 8 : const unsigned char *readendp = readp + data->d_size;
11021 : :
11022 [ + + ]: 28 : while (readp < readendp)
11023 : : {
11024 : 40 : fprintf (out, _(" Offset: 0x%" PRIx64 "\n"),
11025 : 20 : (uint64_t) (readp - (const unsigned char *) data->d_buf));
11026 : :
11027 : : // Header, 2 byte version, 1 byte flag, optional .debug_line offset,
11028 : : // optional vendor extension macro entry table.
11029 [ - + ]: 20 : if (readp + 2 > readendp)
11030 : : {
11031 : 0 : invalid_data:
11032 : 0 : error (0, 0, _("invalid data"));
11033 : 0 : return;
11034 : : }
11035 [ - + ]: 20 : const uint16_t vers = read_2ubyte_unaligned_inc (dbg, readp);
11036 : 20 : fprintf (out, _(" Version: %" PRIu16 "\n"), vers);
11037 : :
11038 : : // Version 4 is the GNU extension for DWARF4. DWARF5 will use version
11039 : : // 5 when it gets standardized.
11040 [ - + ]: 20 : if (vers != 4 && vers != 5)
11041 : : {
11042 : 0 : fprintf (out, _(" unknown version, cannot parse section\n"));
11043 : 0 : return;
11044 : : }
11045 : :
11046 [ - + ]: 20 : if (readp + 1 > readendp)
11047 : 0 : goto invalid_data;
11048 : 20 : const unsigned char flag = *readp++;
11049 : 20 : fprintf (out, _(" Flag: 0x%" PRIx8), flag);
11050 [ + + ]: 20 : if (flag != 0)
11051 : : {
11052 : 12 : fprintf (out, " (");
11053 [ - + ]: 12 : if ((flag & 0x01) != 0)
11054 : : {
11055 : 0 : fprintf (out, "offset_size");
11056 [ # # ]: 0 : if ((flag & 0xFE) != 0)
11057 : 0 : fprintf (out, ", ");
11058 : : }
11059 [ + - ]: 12 : if ((flag & 0x02) != 0)
11060 : : {
11061 : 12 : fprintf (out, "debug_line_offset");
11062 [ - + ]: 12 : if ((flag & 0xFC) != 0)
11063 : 0 : fprintf (out, ", ");
11064 : : }
11065 [ # # ]: 0 : if ((flag & 0x04) != 0)
11066 : : {
11067 : 0 : fprintf (out, "operands_table");
11068 [ # # ]: 0 : if ((flag & 0xF8) != 0)
11069 : 0 : fprintf (out, ", ");
11070 : : }
11071 [ - + ]: 12 : if ((flag & 0xF8) != 0)
11072 : 0 : fprintf (out, "unknown");
11073 : 12 : fprintf (out, ")");
11074 : : }
11075 : 20 : fprintf (out, "\n");
11076 : :
11077 [ + - ]: 20 : unsigned int offset_len = (flag & 0x01) ? 8 : 4;
11078 : 20 : fprintf (out, _(" Offset length: %" PRIu8 "\n"), offset_len);
11079 : 20 : Dwarf_Off line_offset = -1;
11080 [ + + ]: 20 : if (flag & 0x02)
11081 : : {
11082 [ - + ]: 12 : if (offset_len == 8)
11083 [ # # ]: 0 : line_offset = read_8ubyte_unaligned_inc (dbg, readp);
11084 : : else
11085 [ - + ]: 12 : line_offset = read_4ubyte_unaligned_inc (dbg, readp);
11086 : 12 : fprintf (out, _(" .debug_line offset: 0x%" PRIx64 "\n"),
11087 : : line_offset);
11088 : : }
11089 : :
11090 : 12 : struct mac_culist *cu = NULL;
11091 [ + - ]: 12 : if (line_offset != (Dwarf_Off) -1)
11092 : : {
11093 : : cu = culist;
11094 [ + - + + ]: 16 : while (cu != NULL && line_offset != cu->offset)
11095 : 4 : cu = cu->next;
11096 : : }
11097 : :
11098 [ - + ]: 20 : Dwarf_Off str_offsets_base = str_offsets_base_off (dbg, (cu != NULL
11099 : : ? cu->die.cu
11100 : : : NULL));
11101 : :
11102 : 20 : const unsigned char *vendor[DW_MACRO_hi_user - DW_MACRO_lo_user + 1];
11103 [ - + ]: 20 : memset (vendor, 0, sizeof vendor);
11104 [ - + ]: 20 : if (flag & 0x04)
11105 : : {
11106 : : // 1 byte length, for each item, 1 byte opcode, uleb128 number
11107 : : // of arguments, for each argument 1 byte form code.
11108 [ # # ]: 0 : if (readp + 1 > readendp)
11109 : 0 : goto invalid_data;
11110 : 0 : unsigned int tlen = *readp++;
11111 : 0 : fprintf (out, _(" extension opcode table, %" PRIu8 " items:\n"),
11112 : : tlen);
11113 [ # # ]: 0 : for (unsigned int i = 0; i < tlen; i++)
11114 : : {
11115 [ # # ]: 0 : if (readp + 1 > readendp)
11116 : 0 : goto invalid_data;
11117 : 0 : unsigned int opcode = *readp++;
11118 : 0 : fprintf (out, _(" [%" PRIx8 "]"), opcode);
11119 : 0 : if (opcode < DW_MACRO_lo_user
11120 [ # # ]: 0 : || opcode > DW_MACRO_hi_user)
11121 : 0 : goto invalid_data;
11122 : : // Record the start of description for this vendor opcode.
11123 : : // uleb128 nr args, 1 byte per arg form.
11124 : 0 : vendor[opcode - DW_MACRO_lo_user] = readp;
11125 [ # # ]: 0 : if (readp + 1 > readendp)
11126 : 0 : goto invalid_data;
11127 : 0 : unsigned int args = *readp++;
11128 [ # # ]: 0 : if (args > 0)
11129 : : {
11130 : 0 : fprintf (out, _(" %" PRIu8 " arguments:"), args);
11131 : 0 : while (args > 0)
11132 : : {
11133 [ # # ]: 0 : if (readp + 1 > readendp)
11134 : 0 : goto invalid_data;
11135 : 0 : unsigned int form = *readp++;
11136 : 0 : fprintf (out, " %s", dwarf_form_name (form));
11137 [ # # ]: 0 : if (! libdw_valid_user_form (form))
11138 : 0 : goto invalid_data;
11139 : 0 : args--;
11140 [ # # ]: 0 : if (args > 0)
11141 : 0 : fputc (',', out);
11142 : : }
11143 : : }
11144 : : else
11145 : 0 : fprintf (out, _(" no arguments."));
11146 : 0 : fputc ('\n', out);
11147 : : }
11148 : : }
11149 : 20 : fputc ('\n', out);
11150 : :
11151 : 20 : int level = 1;
11152 [ - + ]: 20 : if (readp + 1 > readendp)
11153 : 0 : goto invalid_data;
11154 : 20 : unsigned int opcode = *readp++;
11155 [ + + ]: 3046 : while (opcode != 0)
11156 : : {
11157 : 3026 : unsigned int u128;
11158 : 3026 : unsigned int u128_2;
11159 : 3026 : const unsigned char *endp;
11160 : 3026 : uint64_t off;
11161 : :
11162 [ + + + - : 3026 : switch (opcode)
+ + + - -
- + + - ]
11163 : : {
11164 : 20 : case DW_MACRO_start_file:
11165 : 20 : get_uleb128 (u128, readp, readendp);
11166 [ - + ]: 20 : if (readp >= readendp)
11167 : 0 : goto invalid_data;
11168 : 20 : get_uleb128 (u128_2, readp, readendp);
11169 : :
11170 : : /* Find the CU DIE that matches this line offset. */
11171 : 20 : const char *fname = "???";
11172 [ - + ]: 20 : if (cu != NULL)
11173 : : {
11174 [ + + ]: 20 : if (cu->files == NULL
11175 [ - + ]: 12 : && dwarf_getsrcfiles (&cu->die, &cu->files,
11176 : : NULL) != 0)
11177 : 0 : cu->files = (Dwarf_Files *) -1l;
11178 : :
11179 [ - + ]: 20 : if (cu->files != (Dwarf_Files *) -1l)
11180 : 20 : fname = (dwarf_filesrc (cu->files, u128_2,
11181 [ - + ]: 20 : NULL, NULL) ?: "???");
11182 : : }
11183 : 20 : fprintf (out, "%*sstart_file %u, [%u] %s\n",
11184 : : level, "", u128, u128_2, fname);
11185 : 20 : ++level;
11186 : 20 : break;
11187 : :
11188 : 20 : case DW_MACRO_end_file:
11189 : 20 : --level;
11190 : 20 : fprintf (out, "%*send_file\n", level, "");
11191 : 20 : break;
11192 : :
11193 : 2 : case DW_MACRO_define:
11194 : 2 : get_uleb128 (u128, readp, readendp);
11195 : 2 : endp = memchr (readp, '\0', readendp - readp);
11196 [ - + ]: 2 : if (endp == NULL)
11197 : 0 : goto invalid_data;
11198 : 2 : fprintf (out, "%*s#define %s, line %u\n",
11199 : : level, "", readp, u128);
11200 : 2 : readp = endp + 1;
11201 : 2 : break;
11202 : :
11203 : 0 : case DW_MACRO_undef:
11204 : 0 : get_uleb128 (u128, readp, readendp);
11205 : 0 : endp = memchr (readp, '\0', readendp - readp);
11206 [ # # ]: 0 : if (endp == NULL)
11207 : 0 : goto invalid_data;
11208 : 0 : fprintf (out, "%*s#undef %s, line %u\n",
11209 : : level, "", readp, u128);
11210 : 0 : readp = endp + 1;
11211 : 0 : break;
11212 : :
11213 : 1414 : case DW_MACRO_define_strp:
11214 : 1414 : get_uleb128 (u128, readp, readendp);
11215 [ - + ]: 1414 : if (readp + offset_len > readendp)
11216 : 0 : goto invalid_data;
11217 [ - + ]: 1414 : if (offset_len == 8)
11218 [ # # ]: 0 : off = read_8ubyte_unaligned_inc (dbg, readp);
11219 : : else
11220 [ - + ]: 1414 : off = read_4ubyte_unaligned_inc (dbg, readp);
11221 : 1414 : fprintf (out, "%*s#define %s, line %u (indirect)\n",
11222 : : level, "", dwarf_getstring (dbg, off, NULL), u128);
11223 : 1414 : break;
11224 : :
11225 : 2 : case DW_MACRO_undef_strp:
11226 : 2 : get_uleb128 (u128, readp, readendp);
11227 [ - + ]: 2 : if (readp + offset_len > readendp)
11228 : 0 : goto invalid_data;
11229 [ - + ]: 2 : if (offset_len == 8)
11230 [ # # ]: 0 : off = read_8ubyte_unaligned_inc (dbg, readp);
11231 : : else
11232 [ - + ]: 2 : off = read_4ubyte_unaligned_inc (dbg, readp);
11233 : 2 : fprintf (out, "%*s#undef %s, line %u (indirect)\n",
11234 : : level, "", dwarf_getstring (dbg, off, NULL), u128);
11235 : 2 : break;
11236 : :
11237 : 12 : case DW_MACRO_import:
11238 [ - + ]: 12 : if (readp + offset_len > readendp)
11239 : 0 : goto invalid_data;
11240 [ - + ]: 12 : if (offset_len == 8)
11241 [ # # ]: 0 : off = read_8ubyte_unaligned_inc (dbg, readp);
11242 : : else
11243 [ - + ]: 12 : off = read_4ubyte_unaligned_inc (dbg, readp);
11244 : 12 : fprintf (out, "%*s#include offset 0x%" PRIx64 "\n",
11245 : : level, "", off);
11246 : 12 : break;
11247 : :
11248 : 0 : case DW_MACRO_define_sup:
11249 : 0 : get_uleb128 (u128, readp, readendp);
11250 : 0 : fprintf (out, "%*s#define ", level, "");
11251 : 0 : readp = print_form_data (dbg, DW_FORM_strp_sup,
11252 : : readp, readendp, offset_len,
11253 : : str_offsets_base, out);
11254 : 0 : fprintf (out, ", line %u (sup)\n", u128);
11255 : 0 : break;
11256 : :
11257 : 0 : case DW_MACRO_undef_sup:
11258 : 0 : get_uleb128 (u128, readp, readendp);
11259 : 0 : fprintf (out, "%*s#undef ", level, "");
11260 : 0 : readp = print_form_data (dbg, DW_FORM_strp_sup,
11261 : : readp, readendp, offset_len,
11262 : : str_offsets_base, out);
11263 : 0 : fprintf (out, ", line %u (sup)\n", u128);
11264 : 0 : break;
11265 : :
11266 : 0 : case DW_MACRO_import_sup:
11267 [ # # ]: 0 : if (readp + offset_len > readendp)
11268 : 0 : goto invalid_data;
11269 [ # # ]: 0 : if (offset_len == 8)
11270 [ # # ]: 0 : off = read_8ubyte_unaligned_inc (dbg, readp);
11271 : : else
11272 [ # # ]: 0 : off = read_4ubyte_unaligned_inc (dbg, readp);
11273 : : // XXX Needs support for reading from supplementary object file.
11274 : 0 : fprintf (out, "%*s#include offset 0x%" PRIx64 " (sup)\n",
11275 : : level, "", off);
11276 : 0 : break;
11277 : :
11278 : 1552 : case DW_MACRO_define_strx:
11279 : 1552 : get_uleb128 (u128, readp, readendp);
11280 : 1552 : fprintf (out, "%*s#define ", level, "");
11281 : 1552 : readp = print_form_data (dbg, DW_FORM_strx,
11282 : : readp, readendp, offset_len,
11283 : : str_offsets_base, out);
11284 : 1552 : fprintf (out, ", line %u (strx)\n", u128);
11285 : 1552 : break;
11286 : :
11287 : 4 : case DW_MACRO_undef_strx:
11288 : 4 : get_uleb128 (u128, readp, readendp);
11289 : 4 : fprintf (out, "%*s#undef ", level, "");
11290 : 4 : readp = print_form_data (dbg, DW_FORM_strx,
11291 : : readp, readendp, offset_len,
11292 : : str_offsets_base, out);
11293 : 4 : fprintf (out, ", line %u (strx)\n", u128);
11294 : 4 : break;
11295 : :
11296 : : default:
11297 : 0 : fprintf (out, "%*svendor opcode 0x%" PRIx8, level, "", opcode);
11298 [ # # ]: 0 : if (opcode < DW_MACRO_lo_user
11299 : : || opcode > DW_MACRO_lo_user
11300 [ # # ]: 0 : || vendor[opcode - DW_MACRO_lo_user] == NULL)
11301 : 0 : goto invalid_data;
11302 : :
11303 : 0 : const unsigned char *op_desc;
11304 : 0 : op_desc = vendor[opcode - DW_MACRO_lo_user];
11305 : :
11306 : : // Just skip the arguments, we cannot really interpret them,
11307 : : // but print as much as we can.
11308 : 0 : unsigned int args = *op_desc++;
11309 [ # # # # ]: 0 : while (args > 0 && readp < readendp)
11310 : : {
11311 : 0 : unsigned int form = *op_desc++;
11312 : 0 : readp = print_form_data (dbg, form, readp, readendp,
11313 : : offset_len, str_offsets_base, out);
11314 : 0 : args--;
11315 [ # # ]: 0 : if (args > 0)
11316 : 0 : fprintf (out, ", ");
11317 : : }
11318 : 0 : fputc ('\n', out);
11319 : : }
11320 : :
11321 [ - + ]: 3026 : if (readp + 1 > readendp)
11322 : 0 : goto invalid_data;
11323 : 3026 : opcode = *readp++;
11324 [ + + ]: 3026 : if (opcode == 0)
11325 : 20 : fputc ('\n', out);
11326 : : }
11327 : : }
11328 : : }
11329 : :
11330 : :
11331 : : typedef struct {
11332 : : int n;
11333 : : FILE *out;
11334 : : } pubnames_arg;
11335 : :
11336 : : /* Callback for printing global names. */
11337 : : static int
11338 : 68 : print_pubnames (Dwarf *dbg __attribute__ ((unused)), Dwarf_Global *global,
11339 : : void *arg)
11340 : : {
11341 : 68 : pubnames_arg *p = (pubnames_arg *) arg;
11342 : :
11343 : 136 : fprintf (p->out, _(" [%5d] DIE offset: %6" PRId64
11344 : : ", CU DIE offset: %6" PRId64 ", name: %s\n"),
11345 : 68 : (p->n)++, global->die_offset, global->cu_offset, global->name);
11346 : :
11347 : 68 : return 0;
11348 : : }
11349 : :
11350 : :
11351 : : /* Print the known exported symbols in the DWARF section '.debug_pubnames'. */
11352 : : static void
11353 : 16 : print_debug_pubnames_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
11354 : : Ebl *ebl,
11355 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
11356 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg,
11357 : : FILE *out)
11358 : : {
11359 : : /* Check section actually exists. */
11360 [ - + ]: 16 : if (get_debug_elf_data (dbg, ebl, IDX_debug_pubnames, scn) == NULL)
11361 : 0 : return;
11362 : :
11363 : 16 : fprintf (out, _("\nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
11364 : : elf_ndxscn (scn), section_name (ebl, shdr),
11365 : 16 : (uint64_t) shdr->sh_offset);
11366 : :
11367 : 16 : pubnames_arg arg = { 0, out };
11368 : 16 : (void) dwarf_getpubnames (dbg, print_pubnames, &arg, 0);
11369 : : }
11370 : :
11371 : : /* Print the content of the DWARF string section '.debug_str'
11372 : : or 'debug_line_str'. */
11373 : : static void
11374 : 134 : print_debug_str_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
11375 : : Ebl *ebl,
11376 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
11377 : : Elf_Scn *scn, GElf_Shdr *shdr,
11378 : : Dwarf *dbg __attribute__ ((unused)),
11379 : : FILE *out)
11380 : : {
11381 : 134 : const char *name = section_name (ebl, shdr);
11382 [ + + ]: 134 : int idx = ((name != NULL && strstr (name, "debug_line_str") != NULL)
11383 : : ? IDX_debug_line_str : IDX_debug_str);
11384 : 134 : Elf_Data *data = get_debug_elf_data (dbg, ebl, idx, scn);
11385 [ + - ]: 134 : if (data == NULL)
11386 : : return;
11387 : :
11388 : 134 : const size_t sh_size = data->d_size;
11389 : :
11390 : : /* Compute floor(log16(shdr->sh_size)). */
11391 : 134 : GElf_Addr tmp = sh_size;
11392 : 134 : int digits = 1;
11393 [ + + ]: 470 : while (tmp >= 16)
11394 : : {
11395 : 336 : ++digits;
11396 : 336 : tmp >>= 4;
11397 : : }
11398 : 134 : digits = MAX (4, digits);
11399 : :
11400 : 134 : fprintf (out, _("\nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"
11401 : : " %*s String\n"),
11402 : : elf_ndxscn (scn),
11403 : 134 : section_name (ebl, shdr), (uint64_t) shdr->sh_offset,
11404 : : /* TRANS: the debugstr| prefix makes the string unique. */
11405 [ + - ]: 134 : digits + 2, sgettext ("debugstr|Offset"));
11406 : :
11407 : 134 : Dwarf_Off offset = 0;
11408 [ + + ]: 173838 : while (offset < sh_size)
11409 : : {
11410 : 173704 : size_t len;
11411 : 173704 : const char *str = (const char *) data->d_buf + offset;
11412 : 173704 : const char *endp = memchr (str, '\0', sh_size - offset);
11413 [ - + ]: 173704 : if (unlikely (endp == NULL))
11414 : : {
11415 : 0 : fprintf (out, _(" *** error, missing string terminator\n"));
11416 : 0 : break;
11417 : : }
11418 : :
11419 : 173704 : fprintf (out, " [%*" PRIx64 "] \"%s\"\n",
11420 : : digits, (uint64_t) offset, str);
11421 : 173704 : len = endp - str;
11422 : 173704 : offset += len + 1;
11423 : : }
11424 : : }
11425 : :
11426 : : static void
11427 : 12 : print_debug_str_offsets_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
11428 : : Ebl *ebl,
11429 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
11430 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg,
11431 : : FILE *out)
11432 : : {
11433 : 12 : Elf_Data *data = get_debug_elf_data (dbg, ebl, IDX_debug_str_offsets, scn);
11434 [ + - ]: 12 : if (data == NULL)
11435 : : return;
11436 : :
11437 : 12 : fprintf (out, _("\
11438 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
11439 : : elf_ndxscn (scn), section_name (ebl, shdr),
11440 : 12 : (uint64_t) shdr->sh_offset);
11441 : :
11442 [ + - ]: 12 : if (shdr->sh_size == 0)
11443 : : return;
11444 : :
11445 : 12 : size_t idx = 0;
11446 : 12 : sort_listptr (&known_stroffbases, "str_offsets");
11447 : :
11448 : 12 : const unsigned char *start = (const unsigned char *) data->d_buf;
11449 : 12 : const unsigned char *readp = start;
11450 : 12 : const unsigned char *readendp = ((const unsigned char *) data->d_buf
11451 : 12 : + data->d_size);
11452 : :
11453 [ + + ]: 28 : while (readp < readendp)
11454 : : {
11455 : : /* Most string offset tables will have a header. For split
11456 : : dwarf unit GNU DebugFission didn't add one. But they were
11457 : : also only defined for split units (main or skeleton units
11458 : : didn't have indirect strings). So if we don't have a
11459 : : DW_AT_str_offsets_base at all and this is offset zero, then
11460 : : just start printing offsets immediately, if this is a .dwo
11461 : : section. */
11462 : 16 : Dwarf_Off off = (Dwarf_Off) (readp
11463 : 16 : - (const unsigned char *) data->d_buf);
11464 : :
11465 : 16 : fprintf (out, "Table at offset %" PRIx64 " ", off);
11466 : :
11467 : : /* Find the first CU that could plausibly be associated with
11468 : : this string offsets index. Skip CUs that point
11469 : : str_offsets_base before this table. */
11470 [ + + ]: 16 : struct listptr *listptr = get_listptr (&known_stroffbases, idx);
11471 [ + + ]: 12 : while (listptr != NULL && listptr->offset < off)
11472 [ + - + + ]: 24 : listptr = get_listptr (&known_stroffbases, ++idx);
11473 : :
11474 : 16 : const unsigned char *next_unitp = readendp;
11475 : 16 : uint8_t offset_size;
11476 : 16 : bool has_header;
11477 [ + + ]: 16 : if (listptr == NULL)
11478 : : {
11479 : : /* This can happen for .dwo files. There is only an header
11480 : : in the case this is a version 5 split DWARF file. */
11481 : 8 : Dwarf_CU *cu;
11482 : 8 : uint8_t unit_type;
11483 [ - + ]: 8 : if (dwarf_get_units (dbg, NULL, &cu, NULL, &unit_type,
11484 : : NULL, NULL) != 0)
11485 : : {
11486 : 0 : error (0, 0, "Warning: Cannot find any DWARF unit.");
11487 : : /* Just guess some values. */
11488 : 0 : has_header = false;
11489 : 0 : offset_size = 4;
11490 : : }
11491 [ + - ]: 8 : else if (off == 0
11492 : 8 : && (unit_type == DW_UT_split_type
11493 [ + - ]: 8 : || unit_type == DW_UT_split_compile))
11494 : : {
11495 : 8 : has_header = cu->version > 4;
11496 : 8 : offset_size = cu->offset_size;
11497 : : }
11498 : : else
11499 : : {
11500 : 0 : error (0, 0,
11501 : : "Warning: No CU references .debug_str_offsets after %"
11502 : : PRIx64, off);
11503 : 0 : has_header = cu->version > 4;
11504 : 0 : offset_size = cu->offset_size;
11505 : : }
11506 : 8 : fprintf (out, "\n");
11507 : : }
11508 : : else
11509 : : {
11510 : : /* This must be DWARF5, since GNU DebugFission didn't define
11511 : : DW_AT_str_offsets_base. */
11512 : 8 : has_header = true;
11513 : :
11514 : 8 : Dwarf_Die cudie;
11515 [ - + ]: 8 : if (dwarf_cu_die (listptr->cu, &cudie,
11516 : : NULL, NULL, NULL, NULL,
11517 : : NULL, NULL) == NULL)
11518 : 0 : fprintf (out, "Unknown CU (%s):\n", dwarf_errmsg (-1));
11519 : : else
11520 : 8 : fprintf (out, "for CU [%6" PRIx64 "]:\n", dwarf_dieoffset (&cudie));
11521 : : }
11522 : :
11523 [ + + ]: 16 : if (has_header)
11524 : : {
11525 : 12 : uint64_t unit_length;
11526 : 12 : uint16_t version;
11527 : 12 : uint16_t padding;
11528 : :
11529 [ - + ]: 12 : unit_length = read_4ubyte_unaligned_inc (dbg, readp);
11530 [ - + ]: 12 : if (unlikely (unit_length == 0xffffffff))
11531 : : {
11532 [ # # ]: 0 : if (unlikely (readp > readendp - 8))
11533 : : {
11534 : 0 : invalid_data:
11535 : 0 : error (0, 0, "Invalid data");
11536 : 0 : return;
11537 : : }
11538 [ # # ]: 0 : unit_length = read_8ubyte_unaligned_inc (dbg, readp);
11539 : 0 : offset_size = 8;
11540 : : }
11541 : : else
11542 : : offset_size = 4;
11543 : :
11544 : 12 : fprintf (out, "\n");
11545 : 12 : fprintf (out, _(" Length: %8" PRIu64 "\n"),
11546 : : unit_length);
11547 : 12 : fprintf (out, _(" Offset size: %8" PRIu8 "\n"),
11548 : : offset_size);
11549 : :
11550 : : /* We need at least 2-bytes (version) + 2-bytes (padding) =
11551 : : 4 bytes to complete the header. And this unit cannot go
11552 : : beyond the section data. */
11553 [ + - ]: 12 : if (readp > readendp - 4
11554 [ + - ]: 12 : || unit_length < 4
11555 [ - + ]: 12 : || unit_length > (uint64_t) (readendp - readp))
11556 : 0 : goto invalid_data;
11557 : :
11558 : 12 : next_unitp = readp + unit_length;
11559 : :
11560 [ - + ]: 12 : version = read_2ubyte_unaligned_inc (dbg, readp);
11561 : 12 : fprintf (out, _(" DWARF version: %8" PRIu16 "\n"), version);
11562 : :
11563 [ - + ]: 12 : if (version != 5)
11564 : : {
11565 : 0 : error (0, 0, _("Unknown version"));
11566 : 0 : goto next_unit;
11567 : : }
11568 : :
11569 [ - + ]: 12 : padding = read_2ubyte_unaligned_inc (dbg, readp);
11570 : 12 : fprintf (out, _(" Padding: %8" PRIx16 "\n"), padding);
11571 : :
11572 [ + + ]: 12 : if (listptr != NULL
11573 [ - + ]: 8 : && listptr->offset != (Dwarf_Off) (readp - start))
11574 : : {
11575 : 0 : error (0, 0, "String offsets index doesn't start after header");
11576 : 0 : goto next_unit;
11577 : : }
11578 : :
11579 : 12 : fprintf (out, "\n");
11580 : : }
11581 : :
11582 : 16 : int digits = 1;
11583 : 16 : size_t offsets = (next_unitp - readp) / offset_size;
11584 [ + + ]: 24 : while (offsets >= 10)
11585 : : {
11586 : 8 : ++digits;
11587 : 8 : offsets /= 10;
11588 : : }
11589 : :
11590 : 16 : unsigned int uidx = 0;
11591 : 16 : size_t index_offset = readp - (const unsigned char *) data->d_buf;
11592 : 16 : fprintf (out, " Offsets start at 0x%zx:\n", index_offset);
11593 [ + + ]: 204 : while (readp <= next_unitp - offset_size)
11594 : : {
11595 : 188 : Dwarf_Word offset;
11596 [ + - ]: 188 : if (offset_size == 4)
11597 [ - + ]: 188 : offset = read_4ubyte_unaligned_inc (dbg, readp);
11598 : : else
11599 [ # # ]: 0 : offset = read_8ubyte_unaligned_inc (dbg, readp);
11600 : 188 : const char *str = dwarf_getstring (dbg, offset, NULL);
11601 [ - + ]: 188 : fprintf (out, " [%*u] [%*" PRIx64 "] \"%s\"\n",
11602 : 188 : digits, uidx++, (int) offset_size * 2, offset, str ?: "???");
11603 : : }
11604 : 16 : fprintf (out, "\n");
11605 : :
11606 [ + - ]: 16 : if (readp != next_unitp)
11607 : 0 : error (0, 0, "extra %zd bytes at end of unit",
11608 : 0 : (size_t) (next_unitp - readp));
11609 : :
11610 : 16 : next_unit:
11611 : : readp = next_unitp;
11612 : : }
11613 : : }
11614 : :
11615 : :
11616 : : /* Print the content of the call frame search table section
11617 : : '.eh_frame_hdr'. */
11618 : : static void
11619 : 140 : print_debug_frame_hdr_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
11620 : : Ebl *ebl __attribute__ ((unused)),
11621 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
11622 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg,
11623 : : FILE *out)
11624 : : {
11625 : 140 : fprintf (out, _("\
11626 : : \nCall frame search table section [%2zu] '.eh_frame_hdr':\n"),
11627 : : elf_ndxscn (scn));
11628 : :
11629 : 140 : Elf_Data *data = elf_rawdata (scn, NULL);
11630 : :
11631 [ - + ]: 140 : if (unlikely (data == NULL))
11632 : : {
11633 : 0 : error (0, 0, _("cannot get %s content: %s"),
11634 : : ".eh_frame_hdr", elf_errmsg (-1));
11635 : 0 : return;
11636 : : }
11637 : :
11638 : 140 : const unsigned char *readp = data->d_buf;
11639 : 140 : const unsigned char *const dataend = ((unsigned char *) data->d_buf
11640 : 140 : + data->d_size);
11641 : :
11642 [ - + ]: 140 : if (unlikely (readp + 4 > dataend))
11643 : : {
11644 : 0 : invalid_data:
11645 : 0 : error (0, 0, _("invalid data"));
11646 : 0 : return;
11647 : : }
11648 : :
11649 : 140 : unsigned int version = *readp++;
11650 : 140 : unsigned int eh_frame_ptr_enc = *readp++;
11651 : 140 : unsigned int fde_count_enc = *readp++;
11652 : 140 : unsigned int table_enc = *readp++;
11653 : :
11654 : 140 : fprintf (out, " version: %u\n"
11655 : : " eh_frame_ptr_enc: %#x ",
11656 : : version, eh_frame_ptr_enc);
11657 : 140 : print_encoding_base ("", eh_frame_ptr_enc, out);
11658 : 140 : fprintf (out, " fde_count_enc: %#x ", fde_count_enc);
11659 : 140 : print_encoding_base ("", fde_count_enc, out);
11660 : 140 : fprintf (out, " table_enc: %#x ", table_enc);
11661 : 140 : print_encoding_base ("", table_enc, out);
11662 : :
11663 : 140 : uint64_t eh_frame_ptr = 0;
11664 [ + - ]: 140 : if (eh_frame_ptr_enc != DW_EH_PE_omit)
11665 : : {
11666 : 140 : readp = read_encoded (eh_frame_ptr_enc, readp, dataend, &eh_frame_ptr,
11667 : : dbg);
11668 [ - + ]: 140 : if (unlikely (readp == NULL))
11669 : 0 : goto invalid_data;
11670 : :
11671 : 140 : fprintf (out, " eh_frame_ptr: %#" PRIx64, eh_frame_ptr);
11672 [ + - ]: 140 : if ((eh_frame_ptr_enc & 0x70) == DW_EH_PE_pcrel)
11673 : 140 : fprintf (out, " (offset: %#" PRIx64 ")",
11674 : : /* +4 because of the 4 byte header of the section. */
11675 : 140 : (uint64_t) shdr->sh_offset + 4 + eh_frame_ptr);
11676 : :
11677 : 140 : fputc ('\n', out);
11678 : : }
11679 : :
11680 : 140 : uint64_t fde_count = 0;
11681 [ + - ]: 140 : if (fde_count_enc != DW_EH_PE_omit)
11682 : : {
11683 : 140 : readp = read_encoded (fde_count_enc, readp, dataend, &fde_count, dbg);
11684 [ - + ]: 140 : if (unlikely (readp == NULL))
11685 : 0 : goto invalid_data;
11686 : :
11687 : 140 : fprintf (out, " fde_count: %" PRIu64 "\n", fde_count);
11688 : : }
11689 : :
11690 [ + - + - ]: 140 : if (fde_count == 0 || table_enc == DW_EH_PE_omit)
11691 : : return;
11692 : :
11693 : 140 : fputs (" Table:\n", out);
11694 : :
11695 : : /* Optimize for the most common case. */
11696 [ + - ]: 140 : if (table_enc == (DW_EH_PE_datarel | DW_EH_PE_sdata4))
11697 [ + + ]: 27206 : while (fde_count > 0 && readp + 8 <= dataend)
11698 : : {
11699 [ - + ]: 27066 : int32_t initial_location = read_4sbyte_unaligned_inc (dbg, readp);
11700 : 27066 : uint64_t initial_offset = ((uint64_t) shdr->sh_offset
11701 : 27066 : + (int64_t) initial_location);
11702 [ - + ]: 27066 : int32_t address = read_4sbyte_unaligned_inc (dbg, readp);
11703 : : // XXX Possibly print symbol name or section offset for initial_offset
11704 : 27066 : fprintf (out, " %#" PRIx32 " (offset: %#6" PRIx64 ") -> %#" PRIx32
11705 : : " fde=[%6" PRIx64 "]\n",
11706 : : initial_location, initial_offset,
11707 : 27066 : address, address - (eh_frame_ptr + 4));
11708 : : }
11709 : : else
11710 : 140 : while (0 && readp < dataend)
11711 : : {
11712 : :
11713 : 140 : }
11714 : : }
11715 : :
11716 : :
11717 : : /* Print the content of the exception handling table section
11718 : : '.eh_frame_hdr'. */
11719 : : static void
11720 : 0 : print_debug_exception_table (Dwfl_Module *dwflmod __attribute__ ((unused)),
11721 : : Ebl *ebl __attribute__ ((unused)),
11722 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
11723 : : Elf_Scn *scn,
11724 : : GElf_Shdr *shdr __attribute__ ((unused)),
11725 : : Dwarf *dbg __attribute__ ((unused)),
11726 : : FILE *out)
11727 : : {
11728 : 0 : fprintf (out, _("\
11729 : : \nException handling table section [%2zu] '.gcc_except_table':\n"),
11730 : : elf_ndxscn (scn));
11731 : :
11732 : 0 : Elf_Data *data = elf_rawdata (scn, NULL);
11733 : :
11734 [ # # ]: 0 : if (unlikely (data == NULL))
11735 : : {
11736 : 0 : error (0, 0, _("cannot get %s content: %s"),
11737 : : ".gcc_except_table", elf_errmsg (-1));
11738 : 0 : return;
11739 : : }
11740 : :
11741 : 0 : const unsigned char *readp = data->d_buf;
11742 : 0 : const unsigned char *const dataend = readp + data->d_size;
11743 : :
11744 [ # # ]: 0 : if (unlikely (readp + 1 > dataend))
11745 : : {
11746 : 0 : invalid_data:
11747 : 0 : error (0, 0, _("invalid data"));
11748 : 0 : return;
11749 : : }
11750 : 0 : unsigned int lpstart_encoding = *readp++;
11751 : 0 : fprintf (out, _(" LPStart encoding: %#x "), lpstart_encoding);
11752 : 0 : print_encoding_base ("", lpstart_encoding, out);
11753 [ # # ]: 0 : if (lpstart_encoding != DW_EH_PE_omit)
11754 : : {
11755 : 0 : uint64_t lpstart;
11756 : 0 : readp = read_encoded (lpstart_encoding, readp, dataend, &lpstart, dbg);
11757 : 0 : fprintf (out, " LPStart: %#" PRIx64 "\n", lpstart);
11758 : : }
11759 : :
11760 [ # # ]: 0 : if (unlikely (readp + 1 > dataend))
11761 : 0 : goto invalid_data;
11762 : 0 : unsigned int ttype_encoding = *readp++;
11763 : 0 : fprintf (out, _(" TType encoding: %#x "), ttype_encoding);
11764 : 0 : print_encoding_base ("", ttype_encoding, out);
11765 : 0 : const unsigned char *ttype_base = NULL;
11766 [ # # ]: 0 : if (ttype_encoding != DW_EH_PE_omit)
11767 : : {
11768 : 0 : unsigned int ttype_base_offset;
11769 [ # # ]: 0 : if (readp >= dataend)
11770 : 0 : goto invalid_data;
11771 : 0 : get_uleb128 (ttype_base_offset, readp, dataend);
11772 : 0 : fprintf (out, " TType base offset: %#x\n", ttype_base_offset);
11773 [ # # ]: 0 : if ((size_t) (dataend - readp) > ttype_base_offset)
11774 : 0 : ttype_base = readp + ttype_base_offset;
11775 : : }
11776 : :
11777 [ # # ]: 0 : if (unlikely (readp + 1 > dataend))
11778 : 0 : goto invalid_data;
11779 : 0 : unsigned int call_site_encoding = *readp++;
11780 : 0 : fprintf (out, _(" Call site encoding: %#x "), call_site_encoding);
11781 : 0 : print_encoding_base ("", call_site_encoding, out);
11782 : 0 : unsigned int call_site_table_len;
11783 [ # # ]: 0 : if (readp >= dataend)
11784 : 0 : goto invalid_data;
11785 : 0 : get_uleb128 (call_site_table_len, readp, dataend);
11786 : :
11787 : 0 : const unsigned char *const action_table = readp + call_site_table_len;
11788 [ # # ]: 0 : if (unlikely (action_table > dataend))
11789 : 0 : goto invalid_data;
11790 : : unsigned int u = 0;
11791 : : unsigned int max_action = 0;
11792 [ # # ]: 0 : while (readp < action_table)
11793 : : {
11794 [ # # ]: 0 : if (u == 0)
11795 : 0 : fputs (_("\n Call site table:\n"), out);
11796 : :
11797 : 0 : uint64_t call_site_start;
11798 : 0 : readp = read_encoded (call_site_encoding, readp, dataend,
11799 : : &call_site_start, dbg);
11800 : 0 : uint64_t call_site_length;
11801 : 0 : readp = read_encoded (call_site_encoding, readp, dataend,
11802 : : &call_site_length, dbg);
11803 : 0 : uint64_t landing_pad;
11804 : 0 : readp = read_encoded (call_site_encoding, readp, dataend,
11805 : : &landing_pad, dbg);
11806 : 0 : unsigned int action;
11807 [ # # ]: 0 : if (readp >= dataend)
11808 : 0 : goto invalid_data;
11809 : 0 : get_uleb128 (action, readp, dataend);
11810 : 0 : max_action = MAX (action, max_action);
11811 : 0 : fprintf (out, _(" [%4u] Call site start: %#" PRIx64 "\n"
11812 : : " Call site length: %" PRIu64 "\n"
11813 : : " Landing pad: %#" PRIx64 "\n"
11814 : : " Action: %u\n"),
11815 : : u++, call_site_start, call_site_length, landing_pad, action);
11816 : : }
11817 [ # # ]: 0 : if (readp != action_table)
11818 : 0 : goto invalid_data;
11819 : :
11820 : 0 : unsigned int max_ar_filter = 0;
11821 [ # # ]: 0 : if (max_action > 0)
11822 : : {
11823 : 0 : fputs ("\n Action table:\n", out);
11824 : :
11825 : 0 : size_t maxdata = (size_t) (dataend - action_table);
11826 [ # # # # ]: 0 : if (max_action > maxdata || maxdata - max_action < 1)
11827 : : {
11828 : 0 : invalid_action_table:
11829 : 0 : fputs (_(" <INVALID DATA>\n"), stdout);
11830 : 0 : return;
11831 : : }
11832 : :
11833 : 0 : const unsigned char *const action_table_end
11834 : 0 : = action_table + max_action + 1;
11835 : :
11836 : 0 : u = 0;
11837 : 0 : do
11838 : : {
11839 : 0 : int ar_filter;
11840 : 0 : get_sleb128 (ar_filter, readp, action_table_end);
11841 [ # # ]: 0 : if (ar_filter > 0 && (unsigned int) ar_filter > max_ar_filter)
11842 : 0 : max_ar_filter = ar_filter;
11843 : 0 : int ar_disp;
11844 [ # # ]: 0 : if (readp >= action_table_end)
11845 : 0 : goto invalid_action_table;
11846 : 0 : get_sleb128 (ar_disp, readp, action_table_end);
11847 : :
11848 : 0 : fprintf (out, " [%4u] ar_filter: % d\n"
11849 : : " ar_disp: % -5d",
11850 : : u, ar_filter, ar_disp);
11851 [ # # ]: 0 : if (abs (ar_disp) & 1)
11852 : 0 : fprintf (out, " -> [%4u]\n", u + (ar_disp + 1) / 2);
11853 [ # # ]: 0 : else if (ar_disp != 0)
11854 : 0 : fputs (" -> ???\n", out);
11855 : : else
11856 : 0 : fputc ('\n', out);
11857 : 0 : ++u;
11858 : : }
11859 [ # # ]: 0 : while (readp < action_table_end);
11860 : : }
11861 : :
11862 [ # # ]: 0 : if (max_ar_filter > 0 && ttype_base != NULL)
11863 : : {
11864 : 0 : unsigned char dsize;
11865 : 0 : fputs ("\n TType table:\n", out);
11866 : :
11867 : : // XXX Not *4, size of encoding;
11868 [ # # ]: 0 : switch (ttype_encoding & 7)
11869 : : {
11870 : : case DW_EH_PE_udata2:
11871 : : case DW_EH_PE_sdata2:
11872 : : dsize = 2;
11873 : : break;
11874 : : case DW_EH_PE_udata4:
11875 : : case DW_EH_PE_sdata4:
11876 : : dsize = 4;
11877 : : break;
11878 : : case DW_EH_PE_udata8:
11879 : : case DW_EH_PE_sdata8:
11880 : : dsize = 8;
11881 : : break;
11882 : 0 : default:
11883 : 0 : dsize = 0;
11884 : 0 : error (1, 0, _("invalid TType encoding"));
11885 : : }
11886 : :
11887 : 0 : if (max_ar_filter
11888 [ # # ]: 0 : > (size_t) (ttype_base - (const unsigned char *) data->d_buf) / dsize)
11889 : 0 : goto invalid_data;
11890 : :
11891 : 0 : readp = ttype_base - max_ar_filter * dsize;
11892 : 0 : do
11893 : : {
11894 : 0 : uint64_t ttype;
11895 : 0 : readp = read_encoded (ttype_encoding, readp, ttype_base, &ttype,
11896 : : dbg);
11897 : 0 : fprintf (out, " [%4u] %#" PRIx64 "\n", max_ar_filter--, ttype);
11898 : : }
11899 [ # # ]: 0 : while (readp < ttype_base);
11900 : : }
11901 : : }
11902 : :
11903 : : /* Print the content of the '.gdb_index' section.
11904 : : http://sourceware.org/gdb/current/onlinedocs/gdb/Index-Section-Format.html
11905 : : */
11906 : : static void
11907 : 8 : print_gdb_index_section (Dwfl_Module *dwflmod, Ebl *ebl,
11908 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
11909 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg,
11910 : : FILE *out)
11911 : : {
11912 : 8 : fprintf (out, _("\nGDB section [%2zu] '%s' at offset %#" PRIx64
11913 : : " contains %" PRId64 " bytes :\n"),
11914 : : elf_ndxscn (scn), section_name (ebl, shdr),
11915 : 8 : (uint64_t) shdr->sh_offset, (uint64_t) shdr->sh_size);
11916 : :
11917 : 8 : Elf_Data *data = elf_rawdata (scn, NULL);
11918 : :
11919 [ - + ]: 8 : if (unlikely (data == NULL))
11920 : : {
11921 : 0 : error (0, 0, _("cannot get %s content: %s"),
11922 : : ".gdb_index", elf_errmsg (-1));
11923 : 4 : return;
11924 : : }
11925 : :
11926 : : // .gdb_index is always in little endian.
11927 : 8 : Dwarf dummy_dbg = { .other_byte_order = MY_ELFDATA != ELFDATA2LSB };
11928 : 8 : dbg = &dummy_dbg;
11929 : :
11930 : 8 : const unsigned char *readp = data->d_buf;
11931 : 8 : const unsigned char *const dataend = readp + data->d_size;
11932 : :
11933 [ - + ]: 8 : if (unlikely (readp + 4 > dataend))
11934 : : {
11935 : 0 : invalid_data:
11936 : 0 : error (0, 0, _("invalid data"));
11937 : 0 : return;
11938 : : }
11939 : :
11940 : 8 : int32_t vers = read_4ubyte_unaligned (dbg, readp);
11941 : 8 : fprintf (out, _(" Version: %" PRId32 "\n"), vers);
11942 : :
11943 : : // The only difference between version 4 and version 5 is the
11944 : : // hash used for generating the table. Version 6 contains symbols
11945 : : // for inlined functions, older versions didn't. Version 7 adds
11946 : : // symbol kinds. Version 8 just indicates that it correctly includes
11947 : : // TUs for symbols. Version 9 adds shortcut table for information
11948 : : // regarding the main function.
11949 [ - + ]: 8 : if (vers < 4 || vers > 9)
11950 : : {
11951 : 0 : fprintf (out, _(" unknown version, cannot parse section\n"));
11952 : 0 : return;
11953 : : }
11954 : :
11955 : 8 : readp += 4;
11956 [ - + ]: 8 : if (unlikely (readp + 4 > dataend))
11957 : 0 : goto invalid_data;
11958 : :
11959 : 8 : uint32_t cu_off = read_4ubyte_unaligned (dbg, readp);
11960 : 8 : fprintf (out, _(" CU offset: %#" PRIx32 "\n"), cu_off);
11961 : :
11962 : 8 : readp += 4;
11963 [ - + ]: 8 : if (unlikely (readp + 4 > dataend))
11964 : 0 : goto invalid_data;
11965 : :
11966 : 8 : uint32_t tu_off = read_4ubyte_unaligned (dbg, readp);
11967 : 8 : fprintf (out, _(" TU offset: %#" PRIx32 "\n"), tu_off);
11968 : :
11969 : 8 : readp += 4;
11970 [ - + ]: 8 : if (unlikely (readp + 4 > dataend))
11971 : 0 : goto invalid_data;
11972 : :
11973 : 8 : uint32_t addr_off = read_4ubyte_unaligned (dbg, readp);
11974 : 8 : fprintf (out, _(" address offset: %#" PRIx32 "\n"), addr_off);
11975 : :
11976 : 8 : readp += 4;
11977 [ - + ]: 8 : if (unlikely (readp + 4 > dataend))
11978 : 0 : goto invalid_data;
11979 : :
11980 : 8 : uint32_t sym_off = read_4ubyte_unaligned (dbg, readp);
11981 : 8 : fprintf (out, _(" symbol offset: %#" PRIx32 "\n"), sym_off);
11982 : :
11983 : 8 : readp += 4;
11984 [ - + ]: 8 : if (unlikely (readp + 4 > dataend))
11985 : 0 : goto invalid_data;
11986 : :
11987 : 8 : uint32_t shortcut_off = 0;
11988 [ + + ]: 8 : if (vers >= 9)
11989 : : {
11990 : 4 : shortcut_off = read_4ubyte_unaligned (dbg, readp);
11991 : 4 : fprintf (out, _(" shortcut offset: %#" PRIx32 "\n"), shortcut_off);
11992 : :
11993 : 4 : readp += 4;
11994 [ - + ]: 4 : if (unlikely (readp + 4 > dataend))
11995 : 0 : goto invalid_data;
11996 : : }
11997 : :
11998 : 8 : uint32_t const_off = read_4ubyte_unaligned (dbg, readp);
11999 : 8 : fprintf (out, _(" constant offset: %#" PRIx32 "\n"), const_off);
12000 : :
12001 [ - + ]: 8 : if (unlikely ((size_t) (dataend - (const unsigned char *) data->d_buf)
12002 : : < const_off))
12003 : 0 : goto invalid_data;
12004 : :
12005 : 8 : readp = data->d_buf + cu_off;
12006 : :
12007 : 8 : const unsigned char *nextp = data->d_buf + tu_off;
12008 [ - + ]: 8 : if (tu_off >= data->d_size)
12009 : 0 : goto invalid_data;
12010 : :
12011 : 8 : size_t cu_nr = (nextp - readp) / 16;
12012 : :
12013 : 8 : fprintf (out, _("\n CU list at offset %#" PRIx32
12014 : : " contains %zu entries:\n"),
12015 : : cu_off, cu_nr);
12016 : :
12017 : 8 : size_t n = 0;
12018 [ + - + + ]: 22 : while (dataend - readp >= 16 && n < cu_nr)
12019 : : {
12020 : 14 : uint64_t off = read_8ubyte_unaligned (dbg, readp);
12021 : 14 : readp += 8;
12022 : :
12023 : 14 : uint64_t len = read_8ubyte_unaligned (dbg, readp);
12024 : 14 : readp += 8;
12025 : :
12026 : 14 : fprintf (out, " [%4zu] start: %0#8" PRIx64
12027 : : ", length: %5" PRIu64 "\n", n, off, len);
12028 : 14 : n++;
12029 : : }
12030 : :
12031 : 8 : readp = data->d_buf + tu_off;
12032 : 8 : nextp = data->d_buf + addr_off;
12033 [ - + ]: 8 : if (addr_off >= data->d_size)
12034 : 0 : goto invalid_data;
12035 : :
12036 : 8 : size_t tu_nr = (nextp - readp) / 24;
12037 : :
12038 : 8 : fprintf (out, _("\n TU list at offset %#" PRIx32
12039 : : " contains %zu entries:\n"),
12040 : : tu_off, tu_nr);
12041 : :
12042 : 8 : n = 0;
12043 [ + - + + ]: 14 : while (dataend - readp >= 24 && n < tu_nr)
12044 : : {
12045 : 6 : uint64_t off = read_8ubyte_unaligned (dbg, readp);
12046 : 6 : readp += 8;
12047 : :
12048 : 6 : uint64_t type = read_8ubyte_unaligned (dbg, readp);
12049 : 6 : readp += 8;
12050 : :
12051 : 6 : uint64_t sig = read_8ubyte_unaligned (dbg, readp);
12052 : 6 : readp += 8;
12053 : :
12054 : 6 : fprintf (out, " [%4zu] CU offset: %5" PRId64
12055 : : ", type offset: %5" PRId64
12056 : : ", signature: %0#8" PRIx64 "\n", n, off, type, sig);
12057 : 6 : n++;
12058 : : }
12059 : :
12060 : 8 : readp = data->d_buf + addr_off;
12061 : 8 : nextp = data->d_buf + sym_off;
12062 [ - + ]: 8 : if (sym_off >= data->d_size)
12063 : 0 : goto invalid_data;
12064 : :
12065 : 8 : size_t addr_nr = (nextp - readp) / 20;
12066 : :
12067 : 8 : fprintf (out, _("\n Address list at offset %#" PRIx32
12068 : : " contains %zu entries:\n"),
12069 : : addr_off, addr_nr);
12070 : :
12071 : 8 : n = 0;
12072 [ + - + + ]: 22 : while (dataend - readp >= 20 && n < addr_nr)
12073 : : {
12074 : 14 : uint64_t low = read_8ubyte_unaligned (dbg, readp);
12075 : 14 : readp += 8;
12076 : :
12077 : 14 : uint64_t high = read_8ubyte_unaligned (dbg, readp);
12078 : 14 : readp += 8;
12079 : :
12080 : 14 : uint32_t idx = read_4ubyte_unaligned (dbg, readp);
12081 : 14 : readp += 4;
12082 : :
12083 : 14 : fprintf (out, " [%4zu] ", n);
12084 : 14 : print_dwarf_addr (dwflmod, 8, low, low, out);
12085 : 14 : fprintf (out, "..");
12086 : 14 : print_dwarf_addr (dwflmod, 8, high - 1, high, out);
12087 : 14 : fprintf (out, ", CU index: %5" PRId32 "\n", idx);
12088 : 14 : n++;
12089 : : }
12090 : :
12091 : 8 : const unsigned char *const_start = data->d_buf + const_off;
12092 [ - + ]: 8 : if (const_off > data->d_size)
12093 : 0 : goto invalid_data;
12094 : :
12095 : 8 : const unsigned char *shortcut_start = NULL;
12096 [ + + ]: 8 : if (vers >= 9)
12097 : : {
12098 [ - + ]: 4 : if (shortcut_off >= data->d_size)
12099 : 0 : goto invalid_data;
12100 : :
12101 : 4 : shortcut_start = data->d_buf + shortcut_off;
12102 : 4 : nextp = shortcut_start;
12103 : : }
12104 : : else
12105 : : nextp = const_start;
12106 : :
12107 : 8 : readp = data->d_buf + sym_off;
12108 : 8 : size_t sym_nr = (nextp - readp) / 8;
12109 : :
12110 : 8 : fprintf (out, _("\n Symbol table at offset %#" PRIx32
12111 : : " contains %zu slots:\n"),
12112 : : sym_off, sym_nr);
12113 : :
12114 : 8 : n = 0;
12115 [ + - + + ]: 8200 : while (dataend - readp >= 8 && n < sym_nr)
12116 : : {
12117 : 8192 : uint32_t name = read_4ubyte_unaligned (dbg, readp);
12118 : 8192 : readp += 4;
12119 : :
12120 : 8192 : uint32_t vector = read_4ubyte_unaligned (dbg, readp);
12121 : 8192 : readp += 4;
12122 : :
12123 [ + + ]: 8192 : if (name != 0 || vector != 0)
12124 : : {
12125 : 60 : const unsigned char *sym = const_start + name;
12126 [ + - - + ]: 60 : if (unlikely ((size_t) (dataend - const_start) < name
12127 : : || memchr (sym, '\0', dataend - sym) == NULL))
12128 : 0 : goto invalid_data;
12129 : :
12130 : 60 : fprintf (out, " [%4zu] symbol: %s, CUs: ", n, sym);
12131 : :
12132 : 60 : const unsigned char *readcus = const_start + vector;
12133 [ - + ]: 60 : if (unlikely ((size_t) (dataend - const_start) < vector))
12134 : 0 : goto invalid_data;
12135 : 60 : uint32_t cus = read_4ubyte_unaligned (dbg, readcus);
12136 [ + + ]: 128 : while (cus--)
12137 : : {
12138 : 68 : uint32_t cu_kind, cu, kind;
12139 : 68 : bool is_static;
12140 : 68 : readcus += 4;
12141 [ - + ]: 68 : if (unlikely (readcus + 4 > dataend))
12142 : 0 : goto invalid_data;
12143 : 68 : cu_kind = read_4ubyte_unaligned (dbg, readcus);
12144 : 68 : cu = cu_kind & ((1 << 24) - 1);
12145 : 68 : kind = (cu_kind >> 28) & 7;
12146 : 68 : is_static = cu_kind & (1U << 31);
12147 [ + + ]: 68 : if (cu > cu_nr - 1)
12148 : 10 : fprintf (out, "%" PRId32 "T", cu - (uint32_t) cu_nr);
12149 : : else
12150 : 58 : fprintf (out, "%" PRId32, cu);
12151 [ + + ]: 68 : if (kind != 0)
12152 : : {
12153 : 52 : fprintf (out, " (");
12154 [ + + + - : 52 : switch (kind)
- ]
12155 : : {
12156 : : case 1:
12157 : 20 : fprintf (out, "type");
12158 : 20 : break;
12159 : : case 2:
12160 : 16 : fprintf (out, "var");
12161 : 16 : break;
12162 : : case 3:
12163 : 16 : fprintf (out, "func");
12164 : 16 : break;
12165 : : case 4:
12166 : 0 : fprintf (out, "other");
12167 : 0 : break;
12168 : : default:
12169 : 0 : fprintf (out, "unknown-0x%" PRIx32, kind);
12170 : 0 : break;
12171 : : }
12172 [ + + ]: 68 : fprintf (out, ":%c)", (is_static ? 'S' : 'G'));
12173 : : }
12174 [ + + ]: 68 : if (cus > 0)
12175 : 8 : fprintf (out, ", ");
12176 : : }
12177 : 60 : fprintf (out, "\n");
12178 : : }
12179 : 8192 : n++;
12180 : : }
12181 : :
12182 [ + + ]: 8 : if (vers < 9)
12183 : : return;
12184 : :
12185 [ - + ]: 4 : if (unlikely (shortcut_start == NULL))
12186 : 0 : goto invalid_data;
12187 : :
12188 : 4 : readp = shortcut_start;
12189 : 4 : nextp = const_start;
12190 : 4 : size_t shortcut_nr = (nextp - readp) / 4;
12191 : :
12192 [ - + ]: 4 : if (unlikely (shortcut_nr != 2))
12193 : 0 : goto invalid_data;
12194 : :
12195 : 8 : fprintf (out,
12196 : 4 : _("\nShortcut table at offset %#" PRIx32 " contains %zu slots:\n"),
12197 : : shortcut_off, shortcut_nr);
12198 : :
12199 : 4 : uint32_t lang = read_4ubyte_unaligned (dbg, readp);
12200 : 4 : readp += 4;
12201 : :
12202 : : /* Include the hex number of LANG in the output if the language
12203 : : is unknown. */
12204 : 4 : const char *lang_str = dwarf_lang_string (lang);
12205 : 4 : lang_str = string_or_unknown (lang_str, lang, DW_LANG_lo_user,
12206 : : DW_LANG_hi_user, true);
12207 : :
12208 : 4 : fprintf (out, _("Language of main: %s\n"), lang_str);
12209 : 4 : fprintf (out, _("Name of main: "));
12210 : :
12211 [ + + ]: 4 : if (lang != 0)
12212 : : {
12213 : 2 : uint32_t name = read_4ubyte_unaligned (dbg, readp);
12214 : 2 : readp += 4;
12215 : 2 : const unsigned char *sym = const_start + name;
12216 : :
12217 [ + - - + ]: 2 : if (unlikely ((size_t) (dataend - const_start) < name
12218 : : || memchr (sym, '\0', dataend - sym) == NULL))
12219 : 0 : goto invalid_data;
12220 : :
12221 : 2 : fprintf (out, "%s\n", sym);
12222 : : }
12223 : : else
12224 : 2 : fprintf (out, "<unknown>\n");
12225 : : }
12226 : :
12227 : : /* Print the content of the '.debug_cu_index' or '.debug_tu_index'
12228 : : sections. */
12229 : : static void
12230 : 8 : print_cu_index_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
12231 : : Ebl *ebl, GElf_Ehdr *ehdr __attribute__ ((unused)),
12232 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg,
12233 : : FILE *out)
12234 : : {
12235 : 8 : const char *sname = section_name (ebl, shdr);
12236 : 8 : fprintf (out, _("\nDWARF section [%2zu] '%s' at offset %#" PRIx64
12237 : : " contains %" PRId64 " bytes :\n"),
12238 : : elf_ndxscn (scn), sname,
12239 : 8 : (uint64_t) shdr->sh_offset, (uint64_t) shdr->sh_size);
12240 : :
12241 : 8 : Elf_Data *data = elf_rawdata (scn, NULL);
12242 : :
12243 [ - + ]: 8 : if (unlikely (data == NULL))
12244 : : {
12245 : 0 : error (0, 0, _("cannot get %s content: %s"), sname, elf_errmsg (-1));
12246 : 0 : return;
12247 : : }
12248 : :
12249 [ + + ]: 8 : bool is_tu = sname != NULL && strcmp (sname, ".debug_tu_index") == 0;
12250 : :
12251 : 8 : const unsigned char *readp = data->d_buf;
12252 : 8 : const unsigned char *const dataend = readp + data->d_size;
12253 : :
12254 [ - + ]: 8 : if (unlikely (readp > dataend - 4))
12255 : : {
12256 : 0 : invalid_data:
12257 : 0 : error (0, 0, _("invalid data"));
12258 : 0 : return;
12259 : : }
12260 : :
12261 : : /* If read as 4 bytes the version is 2, it is GNU DebugFission for
12262 : : DWARF 5, otherwise the version is in the first 2 bytes, with 2
12263 : : bytes padding. */
12264 [ - + ]: 8 : int32_t vers = read_4ubyte_unaligned (dbg, readp);
12265 [ + + ]: 8 : if (vers != 2)
12266 [ - + ]: 4 : vers = read_2ubyte_unaligned (dbg, readp);
12267 : 8 : fprintf (out, _(" Version: %8" PRId32 "\n"), vers);
12268 : :
12269 : : /* There used to be a version 1, which we don't support, but is
12270 : : described at https://gcc.gnu.org/wiki/DebugFissionDWP
12271 : : Version 2 is GNU DebugFission for DWARF4.
12272 : : Version 5 is the standardized DWARF5 index.
12273 : : Version 6 supports DWARF64 as described in
12274 : : https://dwarfstd.org/issues/220708.2.html */
12275 [ - + - - ]: 8 : if (vers != 2 && vers != 5 && vers != 6)
12276 : : {
12277 : 0 : error (0, 0, _("unknown version, cannot parse section"));
12278 : 0 : return;
12279 : : }
12280 : :
12281 : : /* The offset size field is only available in version 6. */
12282 : 8 : uint8_t offset_size_flag = 0;
12283 : 8 : if (vers < 6)
12284 : 8 : readp += 4;
12285 : : else
12286 : : {
12287 : 0 : readp += 3;
12288 : 0 : offset_size_flag = *readp;
12289 [ # # ]: 0 : fprintf (out, _(" Offset Size: %4" PRIu8 "\n"),
12290 : : offset_size_flag == 0 ? 32 : 64);
12291 : 0 : readp++;
12292 : : }
12293 : :
12294 : : /* Both offsets and sizes are either 32 or 64 bits (4 or 8 bytes). */
12295 [ - - ]: 8 : const unsigned char off_bytes = offset_size_flag == 0 ? 4 : 8;
12296 : :
12297 [ - + ]: 8 : if (unlikely (readp > dataend - 4))
12298 : 0 : goto invalid_data;
12299 [ - + ]: 8 : uint32_t section_count = read_4ubyte_unaligned (dbg, readp);
12300 : 8 : fprintf (out, _(" Columns: %8" PRId32 "\n"), section_count);
12301 : :
12302 : 8 : readp += 4;
12303 [ - + ]: 8 : if (unlikely (readp > dataend - 4))
12304 : 0 : goto invalid_data;
12305 [ - + ]: 8 : uint32_t unit_count = read_4ubyte_unaligned (dbg, readp);
12306 : 8 : fprintf (out, _(" Entries: %8" PRId32 "\n"), unit_count);
12307 : :
12308 : 8 : readp += 4;
12309 [ - + ]: 8 : if (unlikely (readp > dataend - 4))
12310 : 0 : goto invalid_data;
12311 [ - + ]: 8 : uint32_t slot_count = read_4ubyte_unaligned (dbg, readp);
12312 : 8 : fprintf (out, _(" Slots: %8" PRId32 "\n"), slot_count);
12313 : :
12314 : : /* Really should be slot_count > 3 * unit_count / 2, but accept as
12315 : : long as slot_count is at least unit_count. */
12316 [ - + ]: 8 : if (slot_count < unit_count)
12317 : : {
12318 : 0 : error (0, 0, _("Must have at least as many slots as entries"));
12319 : 0 : return;
12320 : : }
12321 : :
12322 : 8 : readp += 4;
12323 : :
12324 : : /* Hash table starts directly after header (16 bytes). */
12325 : 8 : const unsigned char *hash_table = readp;
12326 : : /* Indices (which slot is used for each hash id entry) start after
12327 : : the hash table (ids of 8 bytes). */
12328 : 8 : const unsigned char *indices = hash_table + slot_count * 8;
12329 : : /* Sections used starts after the indices, indices and hash table
12330 : : have the same number of slots, indices are 4 bytes each, */
12331 : 8 : const unsigned char *sections = indices + slot_count * 4;
12332 : : /* Offset slots for each section follow the one row of sections. */
12333 : 8 : const unsigned char *offsets = sections + section_count * 4;
12334 : : /* Size slots for each section follow the offsets (used rows). */
12335 : 8 : const unsigned char *lengths = (offsets +
12336 : 8 : unit_count * section_count * off_bytes);
12337 : : /* The size table has one row of section_count slots per unit,
12338 : : just like the offset table, so it spans unit_count rows (not one). */
12339 : 8 : const unsigned char *lengths_end = (lengths +
12340 : : unit_count * section_count * off_bytes);
12341 : :
12342 : : /* Sanity check the above against dataend. */
12343 [ + - ]: 8 : if ((slot_count > UINT32_MAX / 8)
12344 : : || (section_count > SIZE_MAX / off_bytes)
12345 : : || (unit_count > SIZE_MAX / off_bytes)
12346 : : || ((unit_count != 0) && (section_count > SIZE_MAX / unit_count))
12347 : : || ((section_count != 0) && (unit_count > SIZE_MAX / section_count))
12348 [ + - ]: 8 : || ((unit_count != 0)
12349 [ + - ]: 8 : && (section_count > SIZE_MAX / (4 * (size_t) unit_count)))
12350 : 8 : || (indices > dataend)
12351 [ + - ]: 8 : || (sections > dataend)
12352 : 8 : || (offsets > dataend)
12353 [ + - ]: 8 : || (lengths > dataend)
12354 [ - + ]: 8 : || (lengths_end > dataend))
12355 : 0 : goto invalid_data;
12356 : :
12357 : 8 : fprintf (out, _("\n Offset table\n"));
12358 [ + + ]: 8 : fprintf (out, " slot %s", is_tu ? "tu sig" : "dwo id");
12359 : 8 : fprintf (out, " ");
12360 [ + + ]: 60 : for (size_t i = 0; i < section_count; i++)
12361 : : {
12362 [ - + ]: 52 : uint32_t section = read_4ubyte_unaligned (dbg, sections + i * 4);
12363 : 52 : const char *sec_str = dwarf_section_short_string (vers, section);
12364 [ - + ]: 52 : if (sec_str == NULL)
12365 : 0 : fprintf (out, " ??? %2x", section);
12366 : : else
12367 : 52 : fprintf (out, " %6s", sec_str);
12368 : : }
12369 : 8 : fprintf (out, "\n");
12370 : :
12371 [ + + ]: 96 : for (size_t i = 0; i < slot_count; i++)
12372 : : {
12373 : 88 : uint64_t id;
12374 : 88 : uint32_t row;
12375 [ - + ]: 88 : id = read_8ubyte_unaligned (dbg, hash_table + i * 8);
12376 [ - + ]: 88 : row = read_4ubyte_unaligned (dbg, indices + i * 4);
12377 : : /* Only print used rows. */
12378 [ + + ]: 88 : if (id != 0 && row != 0)
12379 : : {
12380 : 20 : fprintf (out, " [%3zd] %016" PRIx64 " ", i, id);
12381 [ - + ]: 20 : if (row > unit_count)
12382 : : {
12383 : 0 : error (0, 0, _("Row (%" PRIu32 ") larger than "
12384 : : "unit count (%" PRIu32 ")"), row, unit_count);
12385 : 0 : continue;
12386 : : }
12387 : : /* Note row is one based, not zero based. */
12388 : 20 : const unsigned char *prow = (offsets
12389 : 20 : + ((row - 1) * section_count
12390 : 20 : * off_bytes));
12391 [ + - ]: 20 : if (off_bytes == 4)
12392 [ + + ]: 150 : for (size_t j = 0; j < section_count; j++)
12393 : : {
12394 [ - + ]: 130 : uint32_t off = read_4ubyte_unaligned (dbg, prow + j * 4);
12395 : 130 : fprintf (out, " %6" PRIu32, off);
12396 : : }
12397 : : else
12398 [ # # ]: 0 : for (size_t j = 0; j < section_count; j++)
12399 : : {
12400 [ # # ]: 0 : uint64_t off = read_8ubyte_unaligned (dbg, prow + j * 8);
12401 : 0 : fprintf (out, " %6" PRIu64, off);
12402 : : }
12403 : 20 : fprintf (out, "\n");
12404 : : }
12405 : : }
12406 : :
12407 : 8 : fprintf (out, _("\n Size table\n"));
12408 : 8 : fprintf (out, " slot %s", is_tu ? "tu sig" : "dwo id");
12409 : 8 : fprintf (out, " ");
12410 [ + + ]: 60 : for (size_t i = 0; i < section_count; i++)
12411 : : {
12412 [ - + ]: 52 : uint32_t section = read_4ubyte_unaligned (dbg, sections + i * 4);
12413 : 52 : const char *sec_str = dwarf_section_short_string (vers, section);
12414 [ - + ]: 52 : if (sec_str == NULL)
12415 : 0 : fprintf (out, " ??? %2x", section);
12416 : : else
12417 : 52 : fprintf (out, " %6s", sec_str);
12418 : : }
12419 : 8 : fprintf (out, "\n");
12420 : :
12421 [ + + ]: 96 : for (size_t i = 0; i < slot_count; i++)
12422 : : {
12423 : 88 : uint64_t id;
12424 : 88 : uint32_t row;
12425 [ - + ]: 88 : id = read_8ubyte_unaligned (dbg, hash_table + i * 8);
12426 [ - + ]: 88 : row = read_4ubyte_unaligned (dbg, indices + i * 4);
12427 : : /* Only print used rows. */
12428 [ + + ]: 88 : if (id != 0 && row != 0)
12429 : : {
12430 : 20 : fprintf (out, " [%3zd] %016" PRIx64 " ", i, id);
12431 [ - + ]: 20 : if (row > unit_count)
12432 : : {
12433 : 0 : error (0, 0, _("Row (%" PRIu32 ") larger than "
12434 : : "unit count (%" PRIu32 ")"), row, unit_count);
12435 : 0 : continue;
12436 : : }
12437 : : /* Note row is one based, not zero based. */
12438 : 20 : const unsigned char *prow = (lengths
12439 : 20 : + (row - 1) * section_count * off_bytes);
12440 [ + - ]: 20 : if (off_bytes == 4)
12441 [ + + ]: 150 : for (size_t j = 0; j < section_count; j++)
12442 : : {
12443 [ - + ]: 130 : uint32_t sz = read_4ubyte_unaligned (dbg, prow + j * 4);
12444 : 130 : fprintf (out, " %6" PRIu32, sz);
12445 : : }
12446 : : else
12447 [ # # ]: 0 : for (size_t j = 0; j < section_count; j++)
12448 : : {
12449 [ # # ]: 0 : uint64_t sz = read_8ubyte_unaligned (dbg, prow + j * 8);
12450 : 0 : fprintf (out, " %6" PRIu64, sz);
12451 : : }
12452 : 20 : fprintf (out, "\n");
12453 : : }
12454 : : }
12455 : : }
12456 : :
12457 : : /* Returns true and sets split DWARF CU id if there is a split compile
12458 : : unit in the given Dwarf, and no non-split units are found (before it). */
12459 : : static bool
12460 : 210 : is_split_dwarf (Dwarf *dbg, uint64_t *id, Dwarf_CU **split_cu)
12461 : : {
12462 : 210 : Dwarf_CU *cu = NULL;
12463 [ + - ]: 210 : while (dwarf_get_units (dbg, cu, &cu, NULL, NULL, NULL, NULL) == 0)
12464 : : {
12465 : 210 : uint8_t unit_type;
12466 [ + - ]: 210 : if (dwarf_cu_info (cu, NULL, &unit_type, NULL, NULL,
12467 : : id, NULL, NULL) != 0)
12468 : 210 : return false;
12469 : :
12470 [ + + ]: 210 : if (unit_type != DW_UT_split_compile && unit_type != DW_UT_split_type)
12471 : : return false;
12472 : :
12473 : : /* We really only care about the split compile unit, the types
12474 : : should be fine and self sufficient. Also they don't have an
12475 : : id that we can match with a skeleton unit. */
12476 [ + - ]: 18 : if (unit_type == DW_UT_split_compile)
12477 : : {
12478 : 18 : *split_cu = cu;
12479 : 18 : return true;
12480 : : }
12481 : : }
12482 : :
12483 : : return false;
12484 : : }
12485 : :
12486 : : /* Check that there is one and only one Dwfl_Module, return in arg. */
12487 : : static int
12488 : 18 : getone_dwflmod (Dwfl_Module *dwflmod,
12489 : : void **userdata __attribute__ ((unused)),
12490 : : const char *name __attribute__ ((unused)),
12491 : : Dwarf_Addr base __attribute__ ((unused)),
12492 : : void *arg)
12493 : : {
12494 : 18 : Dwfl_Module **m = (Dwfl_Module **) arg;
12495 [ + - ]: 18 : if (*m != NULL)
12496 : : return DWARF_CB_ABORT;
12497 : 18 : *m = dwflmod;
12498 : 18 : return DWARF_CB_OK;
12499 : : }
12500 : :
12501 : : typedef struct Job_Data {
12502 : : struct Job_Data *next;
12503 : : Dwfl_Module *dwflmod;
12504 : : Ebl *ebl;
12505 : : GElf_Ehdr *ehdr;
12506 : : Elf_Scn scn;
12507 : : GElf_Shdr shdr;
12508 : : Dwarf *dbg;
12509 : : FILE *out;
12510 : : void (*fp) (Dwfl_Module *, Ebl *, GElf_Ehdr *,
12511 : : Elf_Scn *, GElf_Shdr *, Dwarf *, FILE *);
12512 : : } job_data;
12513 : :
12514 : : #ifdef USE_LOCKS
12515 : :
12516 : : /* Thread entry point. */
12517 : : static void *
12518 : : do_job (void *data, FILE *out)
12519 : : {
12520 : : job_data *d = (job_data *) data;
12521 : : d->fp (d->dwflmod, d->ebl, d->ehdr, &d->scn, &d->shdr, d->dbg, out);
12522 : : return NULL;
12523 : : }
12524 : : #endif
12525 : :
12526 : : /* If readelf is built with thread safety, then set up JDATA at index IDX
12527 : : and add it to the job queue.
12528 : :
12529 : : If thread safety is not supported or the maximum number of threads is set
12530 : : to 1, then immediately call START_ROUTINE with the given arguments. */
12531 : : static void
12532 : 1106 : schedule_job (job_data **jdatalist,
12533 : : void (*start_routine) (Dwfl_Module *, Ebl *, GElf_Ehdr *,
12534 : : Elf_Scn *, GElf_Shdr *, Dwarf *, FILE *),
12535 : : Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr, Elf_Scn *scn,
12536 : : GElf_Shdr *shdr, Dwarf *dbg)
12537 : : {
12538 : : #ifdef USE_LOCKS
12539 : : if (max_threads > 1)
12540 : : {
12541 : : job_data *jdata = xmalloc (sizeof (job_data));
12542 : :
12543 : : jdata->dwflmod = dwflmod;
12544 : : jdata->ebl = ebl;
12545 : : jdata->ehdr = ehdr;
12546 : : jdata->scn = *scn;
12547 : : jdata->shdr = *shdr;
12548 : : jdata->dbg = dbg;
12549 : : jdata->fp = start_routine;
12550 : : jdata->next = *jdatalist;
12551 : : *jdatalist = jdata;
12552 : :
12553 : : add_job (do_job, (void *) jdata);
12554 : : }
12555 : : else
12556 : : start_routine (dwflmod, ebl, ehdr, scn, shdr, dbg, stdout);
12557 : : #else
12558 : 1106 : (void) jdatalist;
12559 : :
12560 : 1106 : start_routine (dwflmod, ebl, ehdr, scn, shdr, dbg, stdout);
12561 : : #endif
12562 : 1106 : }
12563 : :
12564 : : static void
12565 : 510 : print_debug (Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr)
12566 : : {
12567 : : /* Used for skeleton file, if necessary for split DWARF. */
12568 : 510 : Dwfl *skel_dwfl = NULL;
12569 : 510 : Dwfl_Module *skel_mod = NULL;
12570 : 510 : char *skel_name = NULL;
12571 : 510 : Dwarf *split_dbg = NULL;
12572 : 510 : Dwarf_CU *split_cu = NULL;
12573 : :
12574 : : /* If we need to implicitly or explicitly scan the debug_info section
12575 : : we might need a bit more info, like a skeleton for split dwarf file). */
12576 : 510 : bool implicit_info = (implicit_debug_sections & section_info) != 0;
12577 : 510 : bool explicit_info = (print_debug_sections & section_info) != 0;
12578 : :
12579 : : /* Before we start the real work get a debug context descriptor. */
12580 : 510 : Dwarf_Addr dwbias;
12581 : 510 : Dwarf *dbg = dwfl_module_getdwarf (dwflmod, &dwbias);
12582 : 510 : Dwarf dummy_dbg =
12583 : : {
12584 : 510 : .elf = ebl->elf,
12585 : 510 : .other_byte_order = MY_ELFDATA != ehdr->e_ident[EI_DATA]
12586 : 510 : };
12587 [ + + ]: 510 : if (dbg == NULL)
12588 : : {
12589 [ - + ]: 106 : if ((print_debug_sections & ~(section_exception|section_frame)) != 0)
12590 : 0 : error (0, 0, _("cannot get debug context descriptor: %s"),
12591 : : dwfl_errmsg (-1));
12592 : : dbg = &dummy_dbg;
12593 : : }
12594 [ + + ]: 404 : else if (implicit_info || explicit_info)
12595 : : {
12596 : : /* If we are asked about a split dwarf (.dwo) file, use the user
12597 : : provided, or find the corresponding skeleton file. If we got
12598 : : a skeleton file, replace the given dwflmod and dbg, with one
12599 : : derived from the skeleton file to provide enough context. */
12600 : 210 : uint64_t split_id;
12601 [ + + ]: 210 : if (is_split_dwarf (dbg, &split_id, &split_cu))
12602 : : {
12603 [ + - ]: 18 : if (dwarf_skeleton != NULL)
12604 : 18 : skel_name = strdup (dwarf_skeleton);
12605 : : else
12606 : : {
12607 : : /* Replace file.dwo with file.o and see if that matches. */
12608 : 0 : const char *fname;
12609 : 0 : dwfl_module_info (dwflmod, NULL, NULL, NULL, NULL, NULL,
12610 : : &fname, NULL);
12611 [ # # ]: 0 : if (fname != NULL)
12612 : : {
12613 : 0 : size_t flen = strlen (fname);
12614 [ # # # # ]: 0 : if (flen > 4 && strcmp (".dwo", fname + flen - 4) == 0)
12615 : : {
12616 : 0 : skel_name = strdup (fname);
12617 [ # # ]: 0 : if (skel_name != NULL)
12618 : : {
12619 : 0 : skel_name[flen - 3] = 'o';
12620 : 0 : skel_name[flen - 2] = '\0';
12621 : : }
12622 : : }
12623 : : }
12624 : : }
12625 : :
12626 [ - + ]: 18 : if (skel_name != NULL)
12627 : : {
12628 : 18 : int skel_fd = open (skel_name, O_RDONLY);
12629 [ - + ]: 18 : if (skel_fd == -1)
12630 : 0 : fprintf (stderr, "Warning: Couldn't open DWARF skeleton file"
12631 : : " '%s'\n", skel_name);
12632 : : else
12633 : : {
12634 : 18 : skel_dwfl = create_dwfl (skel_fd, skel_name);
12635 : :
12636 : : /* skel_fd was dup'ed by create_dwfl. We can close the
12637 : : original now. */
12638 : 18 : close (skel_fd);
12639 : : }
12640 : :
12641 [ + - ]: 18 : if (skel_dwfl != NULL)
12642 : : {
12643 [ - + ]: 18 : if (dwfl_getmodules (skel_dwfl, &getone_dwflmod,
12644 : : &skel_mod, 0) != 0)
12645 : : {
12646 : 0 : fprintf (stderr, "Warning: Bad DWARF skeleton,"
12647 : : " multiple modules '%s'\n", skel_name);
12648 : 0 : dwfl_end (skel_dwfl);
12649 : 0 : skel_mod = NULL;
12650 : : }
12651 : : }
12652 [ # # ]: 0 : else if (skel_fd != -1)
12653 : 0 : fprintf (stderr, "Warning: Couldn't create skeleton dwfl for"
12654 : : " '%s': %s\n", skel_name, dwfl_errmsg (-1));
12655 : :
12656 [ + - ]: 18 : if (skel_mod != NULL)
12657 : : {
12658 : 18 : Dwarf *skel_dbg = dwfl_module_getdwarf (skel_mod, &dwbias);
12659 [ + - ]: 18 : if (skel_dbg != NULL)
12660 : : {
12661 : : /* First check the skeleton CU DIE, only fetch
12662 : : the split DIE if we know the id matches to
12663 : : not unnecessary search for any split DIEs we
12664 : : don't need. */
12665 : 18 : Dwarf_CU *cu = NULL;
12666 : 18 : while (dwarf_get_units (skel_dbg, cu, &cu,
12667 [ + - ]: 26 : NULL, NULL, NULL, NULL) == 0)
12668 : : {
12669 : 26 : uint8_t unit_type;
12670 : 26 : uint64_t skel_id;
12671 [ + - ]: 26 : if (dwarf_cu_info (cu, NULL, &unit_type, NULL, NULL,
12672 : : &skel_id, NULL, NULL) == 0
12673 [ + - ]: 26 : && unit_type == DW_UT_skeleton
12674 [ + + ]: 26 : && split_id == skel_id)
12675 : : {
12676 : 18 : Dwarf_Die subdie;
12677 [ + - ]: 18 : if (dwarf_cu_info (cu, NULL, NULL, NULL,
12678 : : &subdie,
12679 : : NULL, NULL, NULL) == 0
12680 [ + - ]: 18 : && dwarf_tag (&subdie) != DW_TAG_invalid)
12681 : : {
12682 : 18 : split_dbg = dwarf_cu_getdwarf (subdie.cu);
12683 [ - + ]: 18 : if (split_dbg == NULL)
12684 : 0 : fprintf (stderr,
12685 : : "Warning: Couldn't get split_dbg:"
12686 : : " %s\n", dwarf_errmsg (-1));
12687 : 0 : break;
12688 : : }
12689 : : else
12690 : : {
12691 : : /* Everything matches up, but not
12692 : : according to libdw. Which means
12693 : : the user knew better. So...
12694 : : Terrible hack... We can never
12695 : : destroy the underlying dwfl
12696 : : because it would free the wrong
12697 : : Dwarfs... So we leak memory...*/
12698 [ # # ]: 0 : if (cu->split == NULL
12699 [ # # ]: 0 : && dwarf_skeleton != NULL)
12700 : : {
12701 : 0 : do_not_close_dwfl = true;
12702 : 0 : __libdw_link_skel_split (cu, split_cu);
12703 : 0 : split_dbg = dwarf_cu_getdwarf (split_cu);
12704 : 0 : break;
12705 : : }
12706 : : else
12707 : 0 : fprintf (stderr, "Warning: Couldn't get"
12708 : : " skeleton subdie: %s\n",
12709 : : dwarf_errmsg (-1));
12710 : : }
12711 : : }
12712 : : }
12713 [ - - ]: 18 : if (split_dbg == NULL)
12714 : 0 : fprintf (stderr, "Warning: '%s' didn't contain a skeleton for split id %" PRIx64 "\n", skel_name, split_id);
12715 : : }
12716 : : else
12717 : 0 : fprintf (stderr, "Warning: Couldn't get skeleton DWARF:"
12718 : : " %s\n", dwfl_errmsg (-1));
12719 : : }
12720 : : }
12721 : :
12722 : 0 : if (split_dbg != NULL)
12723 : : {
12724 : 18 : dbg = split_dbg;
12725 : 18 : dwflmod = skel_mod;
12726 : : }
12727 [ # # ]: 0 : else if (skel_name == NULL)
12728 : 0 : fprintf (stderr,
12729 : : "Warning: split DWARF file, but no skeleton found.\n");
12730 : : }
12731 [ + - ]: 192 : else if (dwarf_skeleton != NULL)
12732 : 0 : fprintf (stderr, "Warning: DWARF skeleton given,"
12733 : : " but not a split DWARF file\n");
12734 : : }
12735 : :
12736 : : /* Get the section header string table index. */
12737 : 510 : size_t shstrndx;
12738 [ - + ]: 510 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
12739 : 0 : error_exit (0, _("cannot get section header string table index"));
12740 : :
12741 : 510 : job_data *jdatalist = NULL;
12742 : :
12743 : : /* If the .debug_info section is listed as implicitly required then
12744 : : we must make sure to handle it before handling any other debug
12745 : : section. Various other sections depend on the CU DIEs being
12746 : : scanned (silently) first. */
12747 [ + + ]: 510 : if (implicit_info)
12748 : : {
12749 : : Elf_Scn *scn = NULL;
12750 [ + - ]: 3134 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
12751 : : {
12752 : 3134 : GElf_Shdr shdr_mem;
12753 : 3134 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
12754 : :
12755 [ + - + + ]: 3134 : if (shdr != NULL && shdr->sh_type == SHT_PROGBITS)
12756 : : {
12757 : 2452 : const char *name = elf_strptr (ebl->elf, shstrndx,
12758 : 1226 : shdr->sh_name);
12759 [ - + ]: 1226 : if (name == NULL)
12760 : 0 : continue;
12761 : :
12762 [ + + ]: 1226 : if (strcmp (name, ".debug_info") == 0
12763 [ + + ]: 1094 : || strcmp (name, ".debug_info.dwo") == 0
12764 [ + + ]: 1076 : || strcmp (name, ".zdebug_info") == 0
12765 [ + - ]: 1064 : || strcmp (name, ".zdebug_info.dwo") == 0
12766 [ + + ]: 1064 : || strcmp (name, ".gnu.debuglto_.debug_info") == 0)
12767 : : {
12768 : 164 : print_debug_info_section (dwflmod, ebl, ehdr,
12769 : : scn, shdr, dbg, stdout);
12770 : 164 : break;
12771 : : }
12772 : : }
12773 : : }
12774 : :
12775 : 164 : print_debug_sections &= ~section_info;
12776 : 164 : implicit_debug_sections &= ~section_info;
12777 : : }
12778 : :
12779 : : /* Look through all the sections for the debugging sections to print. */
12780 : : Elf_Scn *scn = NULL;
12781 [ + + ]: 16796 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
12782 : : {
12783 : 16286 : GElf_Shdr shdr_mem;
12784 : 16286 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
12785 : :
12786 [ + - + + ]: 16286 : if (shdr != NULL && shdr->sh_type == SHT_PROGBITS)
12787 : : {
12788 : 6882 : static const struct
12789 : : {
12790 : : const char *name;
12791 : : enum section_e bitmask;
12792 : : void (*fp) (Dwfl_Module *, Ebl *,
12793 : : GElf_Ehdr *, Elf_Scn *, GElf_Shdr *, Dwarf *, FILE *);
12794 : : } debug_sections[] =
12795 : : {
12796 : : #define NEW_SECTION(name) \
12797 : : { ".debug_" #name, section_##name, print_debug_##name##_section }
12798 : : NEW_SECTION (abbrev),
12799 : : NEW_SECTION (addr),
12800 : : NEW_SECTION (aranges),
12801 : : NEW_SECTION (frame),
12802 : : NEW_SECTION (info),
12803 : : NEW_SECTION (types),
12804 : : NEW_SECTION (line),
12805 : : NEW_SECTION (loc),
12806 : : /* loclists is loc for DWARF5. */
12807 : : { ".debug_loclists", section_loc,
12808 : : print_debug_loclists_section },
12809 : : NEW_SECTION (pubnames),
12810 : : NEW_SECTION (str),
12811 : : /* A DWARF5 specialised debug string section. */
12812 : : { ".debug_line_str", section_str,
12813 : : print_debug_str_section },
12814 : : /* DWARF5 string offsets table. */
12815 : : { ".debug_str_offsets", section_str,
12816 : : print_debug_str_offsets_section },
12817 : : NEW_SECTION (macinfo),
12818 : : NEW_SECTION (macro),
12819 : : NEW_SECTION (ranges),
12820 : : /* rnglists is ranges for DWARF5. */
12821 : : { ".debug_rnglists", section_ranges,
12822 : : print_debug_rnglists_section },
12823 : : { ".eh_frame", section_frame | section_exception,
12824 : : print_debug_frame_section },
12825 : : { ".eh_frame_hdr", section_frame | section_exception,
12826 : : print_debug_frame_hdr_section },
12827 : : { ".gcc_except_table", section_frame | section_exception,
12828 : : print_debug_exception_table },
12829 : : { ".gdb_index", section_gdb_index, print_gdb_index_section },
12830 : : { ".debug_cu_index", section_cu_index, print_cu_index_section },
12831 : : { ".debug_tu_index", section_cu_index, print_cu_index_section },
12832 : : };
12833 : 6882 : const int ndebug_sections = (sizeof (debug_sections)
12834 : : / sizeof (debug_sections[0]));
12835 : 13764 : const char *name = elf_strptr (ebl->elf, shstrndx,
12836 : 6882 : shdr->sh_name);
12837 [ - + ]: 6882 : if (name == NULL)
12838 : 0 : continue;
12839 : :
12840 : : int n;
12841 [ + + ]: 115422 : for (n = 0; n < ndebug_sections; ++n)
12842 : : {
12843 : 111908 : size_t dbglen = strlen (debug_sections[n].name);
12844 : 111908 : size_t scnlen = strlen (name);
12845 [ + + ]: 111908 : if ((strncmp (name, debug_sections[n].name, dbglen) == 0
12846 [ + + ]: 3646 : && (dbglen == scnlen
12847 [ + + ]: 592 : || (scnlen == dbglen + 4
12848 [ + + ]: 506 : && strstr (name, ".dwo") == name + dbglen)))
12849 [ + + + + ]: 108714 : || (name[0] == '.' && name[1] == 'z'
12850 [ + - ]: 1132 : && debug_sections[n].name[1] == 'd'
12851 [ + + ]: 1132 : && strncmp (&name[2], &debug_sections[n].name[1],
12852 : : dbglen - 1) == 0
12853 [ - + ]: 164 : && (scnlen == dbglen + 1
12854 [ # # ]: 0 : || (scnlen == dbglen + 5
12855 [ # # ]: 0 : && strstr (name, ".dwo") == name + dbglen + 1)))
12856 [ + + ]: 108550 : || (scnlen > 14 /* .gnu.debuglto_ prefix. */
12857 [ + + ]: 11280 : && startswith (name, ".gnu.debuglto_")
12858 [ + + ]: 72 : && strcmp (&name[14], debug_sections[n].name) == 0)
12859 : : )
12860 : : {
12861 : 3368 : if (((print_debug_sections | implicit_debug_sections)
12862 [ + + ]: 3368 : & debug_sections[n].bitmask))
12863 : 1106 : schedule_job (&jdatalist, debug_sections[n].fp,
12864 : : dwflmod, ebl, ehdr, scn, shdr, dbg);
12865 : :
12866 : : break;
12867 : : }
12868 : : }
12869 : : }
12870 : : }
12871 : :
12872 : : #ifdef USE_LOCKS
12873 : : /* If max_threads <= 1, then jobs were immediately run in schedule_job. */
12874 : : if (max_threads > 1)
12875 : : run_jobs (max_threads);
12876 : : #endif
12877 : :
12878 : 510 : dwfl_end (skel_dwfl);
12879 : 510 : free (skel_name);
12880 : :
12881 : 510 : while (jdatalist != NULL)
12882 : : {
12883 : : job_data *jdata = jdatalist;
12884 : : jdatalist = jdatalist->next;
12885 : : free (jdata);
12886 : : }
12887 : :
12888 : : /* Turn implicit and/or explicit back on in case we go over another file. */
12889 [ + + ]: 510 : if (implicit_info)
12890 : 164 : implicit_debug_sections |= section_info;
12891 [ + + ]: 510 : if (explicit_info)
12892 : 138 : print_debug_sections |= section_info;
12893 : :
12894 : 510 : reset_listptr (&known_locsptr);
12895 : 510 : reset_listptr (&known_loclistsptr);
12896 : 510 : reset_listptr (&known_rangelistptr);
12897 : 510 : reset_listptr (&known_rnglistptr);
12898 : 510 : reset_listptr (&known_addrbases);
12899 : 510 : reset_listptr (&known_stroffbases);
12900 : 510 : }
12901 : :
12902 : :
12903 : : #define ITEM_INDENT 4
12904 : : #define WRAP_COLUMN 75
12905 : :
12906 : : /* Print "NAME: FORMAT", wrapping when output text would make the line
12907 : : exceed WRAP_COLUMN. Unpadded numbers look better for the core items
12908 : : but this function is also used for registers which should be printed
12909 : : aligned. Fortunately registers output uses fixed fields width (such
12910 : : as %11d) for the alignment.
12911 : :
12912 : : Line breaks should not depend on the particular values although that
12913 : : may happen in some cases of the core items. */
12914 : :
12915 : : static unsigned int
12916 : : __attribute__ ((format (printf, 6, 7)))
12917 : 1644 : print_core_item (unsigned int colno, char sep, unsigned int wrap,
12918 : : size_t name_width, const char *name, const char *format, ...)
12919 : : {
12920 : 1644 : size_t len = strlen (name);
12921 : 1644 : if (name_width < len)
12922 : : name_width = len;
12923 : :
12924 : 1644 : char *out;
12925 : 1644 : va_list ap;
12926 : 1644 : va_start (ap, format);
12927 : 1644 : int out_len = vasprintf (&out, format, ap);
12928 : 1644 : va_end (ap);
12929 [ - + ]: 1644 : if (out_len == -1)
12930 : 0 : error_exit (0, _("memory exhausted"));
12931 : :
12932 : 1644 : size_t n = name_width + sizeof ": " - 1 + out_len;
12933 : :
12934 [ + + ]: 1644 : if (colno == 0)
12935 : : {
12936 : 104 : printf ("%*s", ITEM_INDENT, "");
12937 : 104 : colno = ITEM_INDENT + n;
12938 : : }
12939 [ + + ]: 1540 : else if (colno + 2 + n < wrap)
12940 : : {
12941 : 936 : printf ("%c ", sep);
12942 : 936 : colno += 2 + n;
12943 : : }
12944 : : else
12945 : : {
12946 : 604 : printf ("\n%*s", ITEM_INDENT, "");
12947 : 604 : colno = ITEM_INDENT + n;
12948 : : }
12949 : :
12950 : 1644 : printf ("%s: %*s%s", name, (int) (name_width - len), "", out);
12951 : :
12952 : 1644 : free (out);
12953 : :
12954 : 1644 : return colno;
12955 : : }
12956 : :
12957 : : static const void *
12958 : 1864 : convert (Elf *core, Elf_Type type, uint_fast16_t count,
12959 : : void *value, const void *data, size_t size)
12960 : : {
12961 : 3728 : Elf_Data valuedata =
12962 : : {
12963 : : .d_type = type,
12964 : : .d_buf = value,
12965 [ + + ]: 1864 : .d_size = size ?: gelf_fsize (core, type, count, EV_CURRENT),
12966 : : .d_version = EV_CURRENT,
12967 : : };
12968 : 1864 : Elf_Data indata =
12969 : : {
12970 : : .d_type = type,
12971 : : .d_buf = (void *) data,
12972 : : .d_size = valuedata.d_size,
12973 : : .d_version = EV_CURRENT,
12974 : : };
12975 : :
12976 : 1864 : Elf_Data *d = (gelf_getclass (core) == ELFCLASS32
12977 [ + + ]: 1864 : ? elf32_xlatetom : elf64_xlatetom)
12978 : 1864 : (&valuedata, &indata, elf_getident (core, NULL)[EI_DATA]);
12979 [ - + ]: 1864 : if (d == NULL)
12980 : 0 : error_exit (0, _("cannot convert core note data: %s"),
12981 : : elf_errmsg (-1));
12982 : :
12983 : 1864 : return data + indata.d_size;
12984 : : }
12985 : :
12986 : : typedef uint8_t GElf_Byte;
12987 : :
12988 : : static unsigned int
12989 : 802 : handle_core_item (Elf *core, const GElf_Ehdr *ehdr,
12990 : : const Ebl_Core_Item *item, const void *desc,
12991 : : unsigned int colno, size_t *repeated_size)
12992 : : {
12993 [ + + ]: 802 : uint_fast16_t count = item->count ?: 1;
12994 : : /* Ebl_Core_Item count is always a small number.
12995 : : Make sure the backend didn't put in some large bogus value. */
12996 [ - + ]: 124 : assert (count < 128);
12997 : :
12998 : : #define TYPES \
12999 : : DO_TYPE (BYTE, Byte, "0x%.2" PRIx8, "%" PRId8); \
13000 : : DO_TYPE (HALF, Half, "0x%.4" PRIx16, "%" PRId16); \
13001 : : DO_TYPE (WORD, Word, "0x%.8" PRIx32, "%" PRId32); \
13002 : : DO_TYPE (SWORD, Sword, "%" PRId32, "%" PRId32); \
13003 : : DO_TYPE (XWORD, Xword, "0x%.16" PRIx64, "%" PRId64); \
13004 : : DO_TYPE (SXWORD, Sxword, "%" PRId64, "%" PRId64)
13005 : :
13006 : : #define DO_TYPE(NAME, Name, hex, dec) GElf_##Name Name
13007 : 802 : typedef union { TYPES; } value_t;
13008 : 802 : void *data = alloca (count * sizeof (value_t));
13009 : : #undef DO_TYPE
13010 : :
13011 : : #define DO_TYPE(NAME, Name, hex, dec) \
13012 : : GElf_##Name *value_##Name __attribute__((unused)) = data
13013 : 802 : TYPES;
13014 : : #undef DO_TYPE
13015 : :
13016 : 802 : size_t size = gelf_fsize (core, item->type, count, EV_CURRENT);
13017 : 802 : size_t convsize = size;
13018 [ + + ]: 802 : if (repeated_size != NULL)
13019 : : {
13020 [ + - + + ]: 4 : if (*repeated_size > size && (item->format == 'b' || item->format == 'B'))
13021 : 2 : {
13022 : : /* The descriptor size of a core note comes from an untrusted
13023 : : ELF file. Cap the stack allocation so a malformed note that
13024 : : claims a very large n_descsz cannot exhaust the process
13025 : : stack and crash readelf. count and convsize are derived
13026 : : from the capped size so convert () cannot write past data. */
13027 : 2 : size_t alloc_size = MIN (*repeated_size, 64 * 1024);
13028 : 2 : data = alloca (alloc_size);
13029 : 2 : count *= alloc_size / size;
13030 : 2 : convsize = count * size;
13031 : 2 : *repeated_size -= convsize;
13032 : : }
13033 [ + - + - ]: 2 : else if (item->count != 0 || item->format != '\n')
13034 : 0 : *repeated_size -= size;
13035 : : }
13036 : :
13037 : 802 : convert (core, item->type, count, data, desc + item->offset, convsize);
13038 : :
13039 : 802 : Elf_Type type = item->type;
13040 [ + + ]: 802 : if (type == ELF_T_ADDR)
13041 [ + - ]: 2 : type = gelf_getclass (core) == ELFCLASS32 ? ELF_T_WORD : ELF_T_XWORD;
13042 : :
13043 [ + + + + : 802 : switch (item->format)
+ + + +
- ]
13044 : : {
13045 : 386 : case 'd':
13046 [ - + ]: 386 : assert (count == 1);
13047 [ + + + + : 386 : switch (type)
- + - ]
13048 : : {
13049 : : #define DO_TYPE(NAME, Name, hex, dec) \
13050 : : case ELF_T_##NAME: \
13051 : : colno = print_core_item (colno, ',', WRAP_COLUMN, \
13052 : : 0, item->name, dec, value_##Name[0]); \
13053 : : break
13054 : 386 : TYPES;
13055 : : #undef DO_TYPE
13056 : 0 : default:
13057 : 0 : abort ();
13058 : : }
13059 : : break;
13060 : :
13061 : 222 : case 'x':
13062 [ - + ]: 222 : assert (count == 1);
13063 [ - - + - : 222 : switch (type)
+ - - ]
13064 : : {
13065 : : #define DO_TYPE(NAME, Name, hex, dec) \
13066 : : case ELF_T_##NAME: \
13067 : : colno = print_core_item (colno, ',', WRAP_COLUMN, \
13068 : : 0, item->name, hex, value_##Name[0]); \
13069 : : break
13070 : 222 : TYPES;
13071 : : #undef DO_TYPE
13072 : 0 : default:
13073 : 0 : abort ();
13074 : : }
13075 : : break;
13076 : :
13077 : 46 : case 'b':
13078 : : case 'B':
13079 [ - + ]: 46 : assert (size % sizeof (unsigned int) == 0);
13080 : 46 : unsigned int nbits = count * size * 8;
13081 : 46 : unsigned int pop = 0;
13082 [ + + ]: 32882 : for (const unsigned int *i = data; (void *) i < data + count * size; ++i)
13083 : 32836 : pop += __builtin_popcount (*i);
13084 : 46 : bool negate = pop > nbits / 2;
13085 : 46 : const unsigned int bias = item->format == 'b';
13086 : :
13087 : 46 : {
13088 [ - + ]: 46 : char printed[(negate ? nbits - pop : pop) * 16 + 1];
13089 : 46 : char *p = printed;
13090 : 46 : *p = '\0';
13091 : :
13092 : 46 : if (BYTE_ORDER != LITTLE_ENDIAN && size > sizeof (unsigned int))
13093 : : {
13094 : : assert (size == sizeof (unsigned int) * 2);
13095 : : for (unsigned int *i = data;
13096 : : (void *) i < data + count * size; i += 2)
13097 : : {
13098 : : unsigned int w = i[1];
13099 : : i[1] = i[0];
13100 : : i[0] = w;
13101 : : }
13102 : : }
13103 : :
13104 : 46 : unsigned int lastbit = 0;
13105 : 46 : unsigned int run = 0;
13106 : 46 : for (const unsigned int *i = data;
13107 [ + + ]: 32882 : (void *) i < data + count * size; ++i)
13108 : : {
13109 : 32836 : unsigned int bit = ((void *) i - data) * 8;
13110 [ - + ]: 32836 : unsigned int w = negate ? ~*i : *i;
13111 [ - + ]: 32836 : while (w != 0)
13112 : : {
13113 : : /* Note that a right shift equal to (or greater than)
13114 : : the number of bits of w is undefined behaviour. In
13115 : : particular when the least significant bit is bit 32
13116 : : (w = 0x8000000) then w >>= n is undefined. So
13117 : : explicitly handle that case separately. */
13118 : 0 : unsigned int n = ffs (w);
13119 [ # # ]: 0 : if (n < sizeof (w) * 8)
13120 : 0 : w >>= n;
13121 : : else
13122 : : w = 0;
13123 : 0 : bit += n;
13124 : :
13125 [ # # # # ]: 0 : if (lastbit != 0 && lastbit + 1 == bit)
13126 : 0 : ++run;
13127 : : else
13128 : : {
13129 : 0 : if (lastbit == 0)
13130 : 0 : p += sprintf (p, "%u", bit - bias);
13131 [ # # ]: 0 : else if (run == 0)
13132 : 0 : p += sprintf (p, ",%u", bit - bias);
13133 : : else
13134 : 0 : p += sprintf (p, "-%u,%u", lastbit - bias, bit - bias);
13135 : : run = 0;
13136 : : }
13137 : :
13138 : : lastbit = bit;
13139 : : }
13140 : : }
13141 [ - + - - ]: 46 : if (lastbit > 0 && run > 0 && lastbit + 1 != nbits)
13142 : 0 : p += sprintf (p, "-%u", lastbit - bias);
13143 : :
13144 [ + - ]: 92 : colno = print_core_item (colno, ',', WRAP_COLUMN, 0, item->name,
13145 : : negate ? "~<%s>" : "<%s>", printed);
13146 : : }
13147 : 46 : break;
13148 : :
13149 : 88 : case 'T':
13150 : : case (char) ('T'|0x80):
13151 [ - + ]: 88 : assert (count == 2);
13152 : 88 : Dwarf_Word sec;
13153 : 88 : Dwarf_Word usec;
13154 [ - - + - : 88 : switch (type)
+ - - ]
13155 : : {
13156 : : #define DO_TYPE(NAME, Name, hex, dec) \
13157 : : case ELF_T_##NAME: \
13158 : : sec = value_##Name[0]; \
13159 : : usec = value_##Name[1]; \
13160 : : break
13161 : 88 : TYPES;
13162 : : #undef DO_TYPE
13163 : 0 : default:
13164 : 0 : abort ();
13165 : : }
13166 [ - + ]: 88 : if (unlikely (item->format == (char) ('T'|0x80)))
13167 : : {
13168 : : /* This is a hack for an ill-considered 64-bit ABI where
13169 : : tv_usec is actually a 32-bit field with 32 bits of padding
13170 : : rounding out struct timeval. We've already converted it as
13171 : : a 64-bit field. For little-endian, this just means the
13172 : : high half is the padding; it's presumably zero, but should
13173 : : be ignored anyway. For big-endian, it means the 32-bit
13174 : : field went into the high half of USEC. */
13175 [ # # ]: 0 : if (likely (ehdr->e_ident[EI_DATA] == ELFDATA2MSB))
13176 : 0 : usec >>= 32;
13177 : : else
13178 : 0 : usec &= UINT32_MAX;
13179 : : }
13180 : 88 : colno = print_core_item (colno, ',', WRAP_COLUMN, 0, item->name,
13181 : : "%" PRIu64 ".%.6" PRIu64, sec, usec);
13182 : 88 : break;
13183 : :
13184 : 18 : case 'c':
13185 [ - + ]: 18 : assert (count == 1);
13186 : 18 : colno = print_core_item (colno, ',', WRAP_COLUMN, 0, item->name,
13187 : 18 : "%c", value_Byte[0]);
13188 : 18 : break;
13189 : :
13190 : 36 : case 's':
13191 : 36 : colno = print_core_item (colno, ',', WRAP_COLUMN, 0, item->name,
13192 : : "%.*s", (int) count, value_Byte);
13193 : 36 : break;
13194 : :
13195 : 2 : case '\n':
13196 : : /* This is a list of strings separated by '\n'. */
13197 [ - + ]: 2 : assert (item->count == 0);
13198 [ - + ]: 2 : assert (repeated_size != NULL);
13199 [ - + ]: 2 : assert (item->name == NULL);
13200 [ - + ]: 2 : if (unlikely (item->offset >= *repeated_size))
13201 : : break;
13202 : :
13203 : 2 : const char *s = desc + item->offset;
13204 : 2 : size = *repeated_size - item->offset;
13205 : 2 : *repeated_size = 0;
13206 [ + - ]: 96 : while (size > 0)
13207 : : {
13208 : 96 : const char *eol = memchr (s, '\n', size);
13209 : 96 : int len = size;
13210 [ + + ]: 96 : if (eol != NULL)
13211 : 94 : len = eol - s;
13212 : 96 : printf ("%*s%.*s\n", ITEM_INDENT, "", len, s);
13213 [ + + ]: 96 : if (eol == NULL)
13214 : : break;
13215 : 94 : size -= eol + 1 - s;
13216 : 94 : s = eol + 1;
13217 : : }
13218 : :
13219 : : colno = WRAP_COLUMN;
13220 : : break;
13221 : :
13222 : : case 'h':
13223 : : break;
13224 : :
13225 : 0 : default:
13226 : 0 : error (0, 0, "XXX not handling format '%c' for %s",
13227 : 0 : item->format, item->name);
13228 : 0 : break;
13229 : : }
13230 : :
13231 : : #undef TYPES
13232 : :
13233 : 802 : return colno;
13234 : : }
13235 : :
13236 : :
13237 : : /* Sort items by group, and by layout offset within each group. */
13238 : : static int
13239 : 1790 : compare_core_items (const void *a, const void *b)
13240 : : {
13241 : 1790 : const Ebl_Core_Item *const *p1 = a;
13242 : 1790 : const Ebl_Core_Item *const *p2 = b;
13243 : 1790 : const Ebl_Core_Item *item1 = *p1;
13244 : 1790 : const Ebl_Core_Item *item2 = *p2;
13245 : :
13246 : 1790 : return ((item1->group == item2->group ? 0
13247 [ + + ]: 1790 : : strcmp (item1->group, item2->group))
13248 [ - + ]: 1790 : ?: (int) item1->offset - (int) item2->offset);
13249 : : }
13250 : :
13251 : : /* Sort item groups by layout offset of the first item in the group. */
13252 : : static int
13253 : 278 : compare_core_item_groups (const void *a, const void *b)
13254 : : {
13255 : 278 : const Ebl_Core_Item *const *const *p1 = a;
13256 : 278 : const Ebl_Core_Item *const *const *p2 = b;
13257 : 278 : const Ebl_Core_Item *const *group1 = *p1;
13258 : 278 : const Ebl_Core_Item *const *group2 = *p2;
13259 : 278 : const Ebl_Core_Item *item1 = *group1;
13260 : 278 : const Ebl_Core_Item *item2 = *group2;
13261 : :
13262 : 278 : return (int) item1->offset - (int) item2->offset;
13263 : : }
13264 : :
13265 : : static unsigned int
13266 : 76 : handle_core_items (Elf *core, const GElf_Ehdr *ehdr,
13267 : : const void *desc, size_t descsz,
13268 : : const Ebl_Core_Item *items, size_t nitems)
13269 : 76 : {
13270 [ + + ]: 76 : if (nitems == 0)
13271 : : return 0;
13272 : 70 : unsigned int colno = 0;
13273 : :
13274 : : /* FORMAT '\n' makes sense to be present only as a single item as it
13275 : : processes all the data of a note. FORMATs 'b' and 'B' have a special case
13276 : : if present as a single item but they can be also processed with other
13277 : : items below. */
13278 [ + + + + ]: 70 : if (nitems == 1 && (items[0].format == '\n' || items[0].format == 'b'
13279 [ + - ]: 16 : || items[0].format == 'B'))
13280 : : {
13281 [ - + ]: 4 : assert (items[0].offset == 0);
13282 : 4 : size_t size = descsz;
13283 : 4 : colno = handle_core_item (core, ehdr, items, desc, colno, &size);
13284 : : /* If SIZE is not zero here there is some remaining data. But we do not
13285 : : know how to process it anyway. */
13286 : 4 : return colno;
13287 : : }
13288 [ + + ]: 848 : for (size_t i = 0; i < nitems; ++i)
13289 [ - + ]: 782 : assert (items[i].format != '\n');
13290 : :
13291 : : /* Sort to collect the groups together. */
13292 : 66 : const Ebl_Core_Item *sorted_items[nitems];
13293 [ + + ]: 848 : for (size_t i = 0; i < nitems; ++i)
13294 : 782 : sorted_items[i] = &items[i];
13295 : 66 : qsort (sorted_items, nitems, sizeof sorted_items[0], &compare_core_items);
13296 : :
13297 : : /* Collect the unique groups and sort them. */
13298 : 66 : const Ebl_Core_Item **groups[nitems];
13299 : 66 : groups[0] = &sorted_items[0];
13300 : 66 : size_t ngroups = 1;
13301 [ + + ]: 782 : for (size_t i = 1; i < nitems; ++i)
13302 [ + + ]: 716 : if (sorted_items[i]->group != sorted_items[i - 1]->group
13303 [ + - ]: 150 : && strcmp (sorted_items[i]->group, sorted_items[i - 1]->group))
13304 : 150 : groups[ngroups++] = &sorted_items[i];
13305 : 66 : qsort (groups, ngroups, sizeof groups[0], &compare_core_item_groups);
13306 : :
13307 : : /* Write out all the groups. */
13308 : 66 : const void *last = desc;
13309 : 70 : do
13310 : : {
13311 [ + + ]: 290 : for (size_t i = 0; i < ngroups; ++i)
13312 : : {
13313 : 220 : for (const Ebl_Core_Item **item = groups[i];
13314 : 1018 : (item < &sorted_items[nitems]
13315 [ + + + + ]: 1018 : && ((*item)->group == groups[i][0]->group
13316 [ - + ]: 150 : || !strcmp ((*item)->group, groups[i][0]->group)));
13317 : 798 : ++item)
13318 : 798 : colno = handle_core_item (core, ehdr, *item, desc, colno, NULL);
13319 : :
13320 : : /* Force a line break at the end of the group. */
13321 : 220 : colno = WRAP_COLUMN;
13322 : : }
13323 : :
13324 [ + + ]: 70 : if (descsz == 0)
13325 : : break;
13326 : :
13327 : : /* This set of items consumed a certain amount of the note's data.
13328 : : If there is more data there, we have another unit of the same size.
13329 : : Loop to print that out too. */
13330 : 40 : const Ebl_Core_Item *item = &items[nitems - 1];
13331 : 80 : size_t eltsz = item->offset + gelf_fsize (core, item->type,
13332 : 40 : item->count ?: 1, EV_CURRENT);
13333 : :
13334 : 40 : int reps = -1;
13335 : 40 : do
13336 : : {
13337 : 40 : ++reps;
13338 : 40 : desc += eltsz;
13339 : 40 : descsz -= eltsz;
13340 : : }
13341 [ + + - + ]: 40 : while (descsz >= eltsz && !memcmp (desc, last, eltsz));
13342 : :
13343 [ + - ]: 40 : if (reps == 1)
13344 : : {
13345 : : /* For just one repeat, print it unabridged twice. */
13346 : : desc -= eltsz;
13347 : : descsz += eltsz;
13348 : : }
13349 [ + - ]: 40 : else if (reps > 1)
13350 : 0 : printf (_("\n%*s... <repeats %u more times> ..."),
13351 : : ITEM_INDENT, "", reps);
13352 : :
13353 : 40 : last = desc;
13354 : : }
13355 [ + + ]: 40 : while (descsz > 0);
13356 : :
13357 : : return colno;
13358 : : }
13359 : :
13360 : : static unsigned int
13361 : 324 : handle_core_register (Ebl *ebl, Elf *core, int maxregname,
13362 : : const Ebl_Register_Location *regloc, const void *desc,
13363 : : unsigned int colno)
13364 : : {
13365 [ - + ]: 324 : if (regloc->bits % 8 != 0)
13366 : : {
13367 : 0 : error (0, 0, "Warning: Cannot handle register with %" PRIu8 "bits\n",
13368 : : regloc->bits);
13369 : 0 : return colno;
13370 : : }
13371 : :
13372 : 324 : desc += regloc->offset;
13373 : :
13374 [ + + ]: 1172 : for (int reg = regloc->regno; reg < regloc->regno + regloc->count; ++reg)
13375 : : {
13376 : 848 : char name[REGNAMESZ];
13377 : 848 : int bits;
13378 : 848 : int type;
13379 : 848 : register_info (ebl, reg, regloc, name, &bits, &type);
13380 : :
13381 : : #define TYPES \
13382 : : BITS (8, BYTE, "%4" PRId8, "0x%.2" PRIx8); \
13383 : : BITS (16, HALF, "%6" PRId16, "0x%.4" PRIx16); \
13384 : : BITS (32, WORD, "%11" PRId32, " 0x%.8" PRIx32); \
13385 : : BITS (64, XWORD, "%20" PRId64, " 0x%.16" PRIx64)
13386 : :
13387 : : #define BITS(bits, xtype, sfmt, ufmt) \
13388 : : uint##bits##_t b##bits; int##bits##_t b##bits##s
13389 : 848 : union { TYPES; uint64_t b128[2]; } value;
13390 : : #undef BITS
13391 : :
13392 [ + + ]: 848 : switch (type)
13393 : : {
13394 : 672 : case DW_ATE_unsigned:
13395 : : case DW_ATE_signed:
13396 : : case DW_ATE_address:
13397 [ - + + + : 672 : switch (bits)
+ - ]
13398 : : {
13399 : : #define BITS(bits, xtype, sfmt, ufmt) \
13400 : : case bits: \
13401 : : desc = convert (core, ELF_T_##xtype, 1, &value, desc, 0); \
13402 : : if (type == DW_ATE_signed) \
13403 : : colno = print_core_item (colno, ' ', WRAP_COLUMN, \
13404 : : maxregname, name, \
13405 : : sfmt, value.b##bits##s); \
13406 : : else \
13407 : : colno = print_core_item (colno, ' ', WRAP_COLUMN, \
13408 : : maxregname, name, \
13409 : : ufmt, value.b##bits); \
13410 : : break
13411 : :
13412 [ - - - + : 576 : TYPES;
+ + + + ]
13413 : :
13414 : 96 : case 128:
13415 [ - + ]: 96 : assert (type == DW_ATE_unsigned);
13416 : 96 : desc = convert (core, ELF_T_XWORD, 2, &value, desc, 0);
13417 : 96 : int be = elf_getident (core, NULL)[EI_DATA] == ELFDATA2MSB;
13418 : 96 : colno = print_core_item (colno, ' ', WRAP_COLUMN,
13419 : : maxregname, name,
13420 : : "0x%.16" PRIx64 "%.16" PRIx64,
13421 : 96 : value.b128[!be], value.b128[be]);
13422 : 96 : break;
13423 : :
13424 : 0 : default:
13425 : 0 : abort ();
13426 : : #undef BITS
13427 : : }
13428 : : break;
13429 : :
13430 : 176 : default:
13431 : : /* Print each byte in hex, the whole thing in native byte order. */
13432 [ - + ]: 176 : assert (bits % 8 == 0);
13433 : 176 : const uint8_t *bytes = desc;
13434 : 176 : desc += bits / 8;
13435 : 176 : char hex[bits / 4 + 1];
13436 : 176 : hex[bits / 4] = '\0';
13437 : 176 : int incr = 1;
13438 [ + + ]: 176 : if (elf_getident (core, NULL)[EI_DATA] == ELFDATA2LSB)
13439 : : {
13440 : 96 : bytes += bits / 8 - 1;
13441 : 96 : incr = -1;
13442 : : }
13443 : 176 : size_t idx = 0;
13444 [ + + ]: 1744 : for (char *h = hex; bits > 0; bits -= 8, idx += incr)
13445 : : {
13446 : 1568 : *h++ = "0123456789abcdef"[bytes[idx] >> 4];
13447 : 1568 : *h++ = "0123456789abcdef"[bytes[idx] & 0xf];
13448 : : }
13449 : 176 : colno = print_core_item (colno, ' ', WRAP_COLUMN,
13450 : : maxregname, name, "0x%s", hex);
13451 : 176 : break;
13452 : : }
13453 : 848 : desc += regloc->pad;
13454 : :
13455 : : #undef TYPES
13456 : : }
13457 : :
13458 : : return colno;
13459 : : }
13460 : :
13461 : :
13462 : : struct register_info
13463 : : {
13464 : : const Ebl_Register_Location *regloc;
13465 : : const char *set;
13466 : : char name[REGNAMESZ];
13467 : : int regno;
13468 : : int bits;
13469 : : int type;
13470 : : };
13471 : :
13472 : : static int
13473 : 4132 : register_bitpos (const struct register_info *r)
13474 : : {
13475 : 4132 : return (r->regloc->offset * 8
13476 : 4132 : + ((r->regno - r->regloc->regno)
13477 : 4132 : * (r->regloc->bits + r->regloc->pad * 8)));
13478 : : }
13479 : :
13480 : : static int
13481 : 2124 : compare_sets_by_info (const struct register_info *r1,
13482 : : const struct register_info *r2)
13483 : : {
13484 : 2124 : return ((int) r2->bits - (int) r1->bits
13485 [ + + ]: 2124 : ?: register_bitpos (r1) - register_bitpos (r2));
13486 : : }
13487 : :
13488 : : /* Sort registers by set, and by size and layout offset within each set. */
13489 : : static int
13490 : 9190 : compare_registers (const void *a, const void *b)
13491 : : {
13492 : 9190 : const struct register_info *r1 = a;
13493 : 9190 : const struct register_info *r2 = b;
13494 : :
13495 : : /* Unused elements sort last. */
13496 [ + + ]: 9190 : if (r1->regloc == NULL)
13497 : 6618 : return r2->regloc == NULL ? 0 : 1;
13498 [ + + ]: 2572 : if (r2->regloc == NULL)
13499 : : return -1;
13500 : :
13501 [ + + ]: 2242 : return ((r1->set == r2->set ? 0 : strcmp (r1->set, r2->set))
13502 [ - + ]: 2242 : ?: compare_sets_by_info (r1, r2));
13503 : : }
13504 : :
13505 : : /* Sort register sets by layout offset of the first register in the set. */
13506 : : static int
13507 : 40 : compare_register_sets (const void *a, const void *b)
13508 : : {
13509 : 40 : const struct register_info *const *p1 = a;
13510 : 40 : const struct register_info *const *p2 = b;
13511 : 40 : return compare_sets_by_info (*p1, *p2);
13512 : : }
13513 : :
13514 : : static inline bool
13515 : 1728 : same_set (const struct register_info *a,
13516 : : const struct register_info *b,
13517 : : const struct register_info *regs,
13518 : : size_t maxnreg)
13519 : : {
13520 [ + - ]: 1728 : return (a < ®s[maxnreg] && a->regloc != NULL
13521 [ + - + + ]: 1728 : && b < ®s[maxnreg] && b->regloc != NULL
13522 [ + + ]: 1692 : && a->bits == b->bits
13523 [ + - + + : 3384 : && (a->set == b->set || !strcmp (a->set, b->set)));
- + ]
13524 : : }
13525 : :
13526 : : static unsigned int
13527 : 76 : handle_core_registers (Ebl *ebl, Elf *core, const void *desc,
13528 : : const Ebl_Register_Location *reglocs, size_t nregloc)
13529 : 76 : {
13530 [ + + ]: 76 : if (nregloc == 0)
13531 : : return 0;
13532 : :
13533 : 36 : ssize_t maxnreg = ebl_register_info (ebl, 0, NULL, 0, NULL, NULL, NULL, NULL);
13534 [ - + ]: 36 : if (maxnreg <= 0)
13535 : : {
13536 [ # # ]: 0 : for (size_t i = 0; i < nregloc; ++i)
13537 : 0 : if (maxnreg < reglocs[i].regno + reglocs[i].count)
13538 : : maxnreg = reglocs[i].regno + reglocs[i].count;
13539 [ # # ]: 0 : assert (maxnreg > 0);
13540 : : }
13541 : :
13542 : 36 : struct register_info regs[maxnreg];
13543 : 36 : memset (regs, 0, sizeof regs);
13544 : :
13545 : : /* Sort to collect the sets together. */
13546 : 36 : int maxreg = 0;
13547 [ + + ]: 360 : for (size_t i = 0; i < nregloc; ++i)
13548 : 324 : for (int reg = reglocs[i].regno;
13549 [ + + ]: 1172 : reg < reglocs[i].regno + reglocs[i].count;
13550 : 848 : ++reg)
13551 : : {
13552 [ - + ]: 848 : assert (reg < maxnreg);
13553 : 848 : if (reg > maxreg)
13554 : : maxreg = reg;
13555 : 848 : struct register_info *info = ®s[reg];
13556 : 848 : info->regloc = ®locs[i];
13557 : 848 : info->regno = reg;
13558 : 848 : info->set = register_info (ebl, reg, ®locs[i],
13559 : 848 : info->name, &info->bits, &info->type);
13560 : : }
13561 : 36 : qsort (regs, maxreg + 1, sizeof regs[0], &compare_registers);
13562 : :
13563 : : /* Collect the unique sets and sort them. */
13564 : 36 : struct register_info *sets[maxreg + 1];
13565 : 36 : sets[0] = ®s[0];
13566 : 36 : size_t nsets = 1;
13567 [ + + ]: 2558 : for (int i = 1; i <= maxreg; ++i)
13568 [ + + ]: 2522 : if (regs[i].regloc != NULL
13569 [ + + ]: 812 : && !same_set (®s[i], ®s[i - 1], regs, maxnreg))
13570 : 32 : sets[nsets++] = ®s[i];
13571 : 36 : qsort (sets, nsets, sizeof sets[0], &compare_register_sets);
13572 : :
13573 : : /* Write out all the sets. */
13574 : 36 : unsigned int colno = 0;
13575 [ + + ]: 104 : for (size_t i = 0; i < nsets; ++i)
13576 : : {
13577 : : /* Find the longest name of a register in this set. */
13578 : 68 : size_t maxname = 0;
13579 : 68 : const struct register_info *end;
13580 [ + + ]: 916 : for (end = sets[i]; same_set (sets[i], end, regs, maxnreg); ++end)
13581 : : {
13582 : 848 : size_t len = strlen (end->name);
13583 : 848 : if (len > maxname)
13584 : : maxname = len;
13585 : : }
13586 : :
13587 : : for (const struct register_info *reg = sets[i];
13588 [ + + ]: 392 : reg < end;
13589 : 324 : reg += reg->regloc->count ?: 1)
13590 [ + - ]: 324 : colno = handle_core_register (ebl, core, maxname,
13591 : 324 : reg->regloc, desc, colno);
13592 : :
13593 : : /* Force a line break at the end of the group. */
13594 : 68 : colno = WRAP_COLUMN;
13595 : : }
13596 : :
13597 : : return colno;
13598 : : }
13599 : :
13600 : : static void
13601 : 18 : handle_auxv_note (Ebl *ebl, Elf *core, GElf_Word descsz, GElf_Off desc_pos)
13602 : : {
13603 : 18 : Elf_Data *data = elf_getdata_rawchunk (core, desc_pos, descsz, ELF_T_AUXV);
13604 [ - + ]: 18 : if (data == NULL)
13605 : 0 : elf_error:
13606 : 0 : error_exit (0, _("cannot convert core note data: %s"), elf_errmsg (-1));
13607 : :
13608 : 18 : const size_t nauxv = descsz / gelf_fsize (core, ELF_T_AUXV, 1, EV_CURRENT);
13609 [ + + ]: 354 : for (size_t i = 0; i < nauxv; ++i)
13610 : : {
13611 : 336 : GElf_auxv_t av_mem;
13612 : 336 : GElf_auxv_t *av = gelf_getauxv (data, i, &av_mem);
13613 [ - + ]: 336 : if (av == NULL)
13614 : 0 : goto elf_error;
13615 : :
13616 : 336 : const char *name;
13617 : 336 : const char *fmt;
13618 [ - + ]: 336 : if (ebl_auxv_info (ebl, av->a_type, &name, &fmt) == 0)
13619 : : {
13620 : : /* Unknown type. */
13621 [ # # ]: 0 : if (av->a_un.a_val == 0)
13622 : 0 : printf (" %" PRIu64 "\n", av->a_type);
13623 : : else
13624 : 0 : printf (" %" PRIu64 ": %#" PRIx64 "\n",
13625 : : av->a_type, av->a_un.a_val);
13626 : : }
13627 : : else
13628 [ + + + - : 336 : switch (fmt[0])
+ - ]
13629 : : {
13630 : 18 : case '\0': /* Normally zero. */
13631 [ + - ]: 18 : if (av->a_un.a_val == 0)
13632 : : {
13633 : 18 : printf (" %s\n", name);
13634 : 18 : break;
13635 : : }
13636 : 148 : FALLTHROUGH;
13637 : : case 'x': /* hex */
13638 : : case 'p': /* address */
13639 : : case 's': /* address of string */
13640 : 148 : printf (" %s: %#" PRIx64 "\n", name, av->a_un.a_val);
13641 : 148 : break;
13642 : 162 : case 'u':
13643 : 162 : printf (" %s: %" PRIu64 "\n", name, av->a_un.a_val);
13644 : 162 : break;
13645 : 0 : case 'd':
13646 : 0 : printf (" %s: %" PRId64 "\n", name, av->a_un.a_val);
13647 : 0 : break;
13648 : :
13649 : 8 : case 'b':
13650 : 8 : printf (" %s: %#" PRIx64 " ", name, av->a_un.a_val);
13651 : 8 : GElf_Xword bit = 1;
13652 : 8 : const char *pfx = "<";
13653 [ + + ]: 220 : for (const char *p = fmt + 1; *p != 0; p = strchr (p, '\0') + 1)
13654 : : {
13655 [ + + ]: 212 : if (av->a_un.a_val & bit)
13656 : : {
13657 : 154 : printf ("%s%s", pfx, p);
13658 : 154 : pfx = " ";
13659 : : }
13660 : 212 : bit <<= 1;
13661 : : }
13662 : 8 : printf (">\n");
13663 : 8 : break;
13664 : :
13665 : 0 : default:
13666 : 0 : abort ();
13667 : : }
13668 : : }
13669 : 18 : }
13670 : :
13671 : : static bool
13672 : 390 : buf_has_data (unsigned char const *ptr, unsigned char const *end, size_t sz)
13673 : : {
13674 [ + - + - ]: 390 : return ptr < end && (size_t) (end - ptr) >= sz;
13675 : : }
13676 : :
13677 : : static bool
13678 : 36 : buf_read_int (Elf *core, unsigned char const **ptrp, unsigned char const *end,
13679 : : int *retp)
13680 : : {
13681 [ + - ]: 36 : if (! buf_has_data (*ptrp, end, 4))
13682 : : return false;
13683 : :
13684 : 36 : *ptrp = convert (core, ELF_T_WORD, 1, retp, *ptrp, 4);
13685 : 36 : return true;
13686 : : }
13687 : :
13688 : : static bool
13689 : 354 : buf_read_ulong (Elf *core, unsigned char const **ptrp, unsigned char const *end,
13690 : : uint64_t *retp)
13691 : : {
13692 : 354 : size_t sz = gelf_fsize (core, ELF_T_ADDR, 1, EV_CURRENT);
13693 [ + - ]: 354 : if (! buf_has_data (*ptrp, end, sz))
13694 : : return false;
13695 : :
13696 : 354 : union
13697 : : {
13698 : : uint64_t u64;
13699 : : uint32_t u32;
13700 : : } u;
13701 : :
13702 : 354 : *ptrp = convert (core, ELF_T_ADDR, 1, &u, *ptrp, sz);
13703 : :
13704 [ + + ]: 354 : if (sz == 4)
13705 : 186 : *retp = u.u32;
13706 : : else
13707 : 168 : *retp = u.u64;
13708 : : return true;
13709 : : }
13710 : :
13711 : : static void
13712 : 12 : handle_siginfo_note (Elf *core, GElf_Word descsz, GElf_Off desc_pos)
13713 : : {
13714 : 12 : Elf_Data *data = elf_getdata_rawchunk (core, desc_pos, descsz, ELF_T_BYTE);
13715 [ - + ]: 12 : if (data == NULL)
13716 : 0 : error_exit (0, _("cannot convert core note data: %s"), elf_errmsg (-1));
13717 : :
13718 : 12 : unsigned char const *ptr = data->d_buf;
13719 : 12 : unsigned char const *const end = data->d_buf + data->d_size;
13720 : :
13721 : : /* Siginfo head is three ints: signal number, error number, origin
13722 : : code. */
13723 : 12 : int si_signo, si_errno, si_code;
13724 [ - + ]: 12 : if (! buf_read_int (core, &ptr, end, &si_signo)
13725 [ - + ]: 12 : || ! buf_read_int (core, &ptr, end, &si_errno)
13726 [ - + ]: 12 : || ! buf_read_int (core, &ptr, end, &si_code))
13727 : : {
13728 : 0 : fail:
13729 : 0 : printf (" Not enough data in NT_SIGINFO note.\n");
13730 : 0 : return;
13731 : : }
13732 : :
13733 : : /* Next is a pointer-aligned union of structures. On 64-bit
13734 : : machines, that implies a word of padding. */
13735 [ + + ]: 12 : if (gelf_getclass (core) == ELFCLASS64)
13736 : 6 : ptr += 4;
13737 : :
13738 : 12 : printf (" si_signo: %d, si_errno: %d, si_code: %d\n",
13739 : : si_signo, si_errno, si_code);
13740 : :
13741 [ + - ]: 12 : if (si_code > 0)
13742 [ + - ]: 12 : switch (si_signo)
13743 : : {
13744 : 12 : case CORE_SIGILL:
13745 : : case CORE_SIGFPE:
13746 : : case CORE_SIGSEGV:
13747 : : case CORE_SIGBUS:
13748 : : {
13749 : 12 : uint64_t addr;
13750 [ - + ]: 12 : if (! buf_read_ulong (core, &ptr, end, &addr))
13751 : 0 : goto fail;
13752 : 12 : printf (" fault address: %#" PRIx64 "\n", addr);
13753 : 12 : break;
13754 : : }
13755 : : default:
13756 : : ;
13757 : : }
13758 [ # # ]: 0 : else if (si_code == CORE_SI_USER)
13759 : : {
13760 : 0 : int pid, uid;
13761 [ # # ]: 0 : if (! buf_read_int (core, &ptr, end, &pid)
13762 [ # # ]: 0 : || ! buf_read_int (core, &ptr, end, &uid))
13763 : 0 : goto fail;
13764 : 0 : printf (" sender PID: %d, sender UID: %d\n", pid, uid);
13765 : : }
13766 : : }
13767 : :
13768 : : static void
13769 : 12 : handle_file_note (Elf *core, GElf_Word descsz, GElf_Off desc_pos)
13770 : : {
13771 : 12 : Elf_Data *data = elf_getdata_rawchunk (core, desc_pos, descsz, ELF_T_BYTE);
13772 [ - + ]: 12 : if (data == NULL)
13773 : 0 : error_exit (0, _("cannot convert core note data: %s"), elf_errmsg (-1));
13774 : :
13775 : 12 : unsigned char const *ptr = data->d_buf;
13776 : 12 : unsigned char const *const end = data->d_buf + data->d_size;
13777 : :
13778 : 12 : uint64_t count, page_size;
13779 [ - + ]: 12 : if (! buf_read_ulong (core, &ptr, end, &count)
13780 [ - + ]: 12 : || ! buf_read_ulong (core, &ptr, end, &page_size))
13781 : : {
13782 : 0 : fail:
13783 : 0 : printf (" Not enough data in NT_FILE note.\n");
13784 : 0 : return;
13785 : : }
13786 : :
13787 : 12 : size_t addrsize = gelf_fsize (core, ELF_T_ADDR, 1, EV_CURRENT);
13788 : 12 : uint64_t maxcount = (size_t) (end - ptr) / (3 * addrsize);
13789 [ - + ]: 12 : if (count > maxcount)
13790 : 0 : goto fail;
13791 : :
13792 : : /* Where file names are stored. */
13793 : 12 : unsigned char const *const fstart = ptr + 3 * count * addrsize;
13794 : 12 : char const *fptr = (char *) fstart;
13795 : :
13796 : 12 : printf (" %" PRId64 " files:\n", count);
13797 [ + + ]: 118 : for (uint64_t i = 0; i < count; ++i)
13798 : : {
13799 : 106 : uint64_t mstart, mend, moffset;
13800 [ + - ]: 106 : if (! buf_read_ulong (core, &ptr, fstart, &mstart)
13801 [ + - ]: 106 : || ! buf_read_ulong (core, &ptr, fstart, &mend)
13802 [ - + ]: 106 : || ! buf_read_ulong (core, &ptr, fstart, &moffset))
13803 : 0 : goto fail;
13804 : :
13805 : 106 : const char *fnext = memchr (fptr, '\0', (char *) end - fptr);
13806 [ - + ]: 106 : if (fnext == NULL)
13807 : 0 : goto fail;
13808 : :
13809 : 106 : int ct = printf (" %08" PRIx64 "-%08" PRIx64
13810 : : " %08" PRIx64 " %" PRId64,
13811 : : mstart, mend, moffset * page_size, mend - mstart);
13812 [ + - ]: 106 : printf ("%*s%s\n", ct > 50 ? 3 : 53 - ct, "", fptr);
13813 : :
13814 : 106 : fptr = fnext + 1;
13815 : : }
13816 : : }
13817 : :
13818 : : static void
13819 : 78 : handle_core_note (Ebl *ebl, const GElf_Ehdr *ehdr, const GElf_Nhdr *nhdr,
13820 : : const char *name, const void *desc)
13821 : : {
13822 : 78 : GElf_Word regs_offset;
13823 : 78 : size_t nregloc;
13824 : 78 : const Ebl_Register_Location *reglocs;
13825 : 78 : size_t nitems;
13826 : 78 : const Ebl_Core_Item *items;
13827 : :
13828 [ + + ]: 78 : if (! ebl_core_note (ebl, nhdr, name, desc,
13829 : : ®s_offset, &nregloc, ®locs, &nitems, &items))
13830 : 2 : return;
13831 : :
13832 : : /* Pass 0 for DESCSZ when there are registers in the note,
13833 : : so that the ITEMS array does not describe the whole thing.
13834 : : For non-register notes, the actual descsz might be a multiple
13835 : : of the unit size, not just exactly the unit size. */
13836 : 76 : unsigned int colno = handle_core_items (ebl->elf, ehdr, desc,
13837 [ + + ]: 76 : nregloc == 0 ? nhdr->n_descsz : 0,
13838 : : items, nitems);
13839 [ + + ]: 76 : if (colno != 0)
13840 : 70 : putchar ('\n');
13841 : :
13842 : 76 : colno = handle_core_registers (ebl, ebl->elf, desc + regs_offset,
13843 : : reglocs, nregloc);
13844 [ + + ]: 76 : if (colno != 0)
13845 : 36 : putchar ('\n');
13846 : : }
13847 : :
13848 : : static void
13849 : 508 : handle_notes_data (Ebl *ebl, const GElf_Ehdr *ehdr,
13850 : : GElf_Off start, Elf_Data *data)
13851 : : {
13852 : 508 : fputs (_(" Owner Data size Type\n"), stdout);
13853 : :
13854 [ - + ]: 508 : if (data == NULL)
13855 : 0 : goto bad_note;
13856 : :
13857 : : size_t offset = 0;
13858 : : GElf_Nhdr nhdr;
13859 : : size_t name_offset;
13860 : : size_t desc_offset;
13861 : 5124 : while (offset < data->d_size
13862 [ + + + - ]: 5124 : && (offset = gelf_getnote (data, offset,
13863 : : &nhdr, &name_offset, &desc_offset)) > 0)
13864 : : {
13865 [ + - ]: 4616 : const char *name = nhdr.n_namesz == 0 ? "" : data->d_buf + name_offset;
13866 : 4616 : const char *desc = data->d_buf + desc_offset;
13867 : :
13868 : : /* GNU Build Attributes are weird, they store most of their data
13869 : : into the owner name field. Extract just the owner name
13870 : : prefix here, then use the rest later as data. */
13871 : 4616 : bool is_gnu_build_attr
13872 : 4616 : = startswith (name, ELF_NOTE_GNU_BUILD_ATTRIBUTE_PREFIX);
13873 : 9804 : const char *print_name = (is_gnu_build_attr
13874 [ + + ]: 4616 : ? ELF_NOTE_GNU_BUILD_ATTRIBUTE_PREFIX : name);
13875 : 5188 : size_t print_namesz = (is_gnu_build_attr
13876 : 572 : ? strlen (print_name) : nhdr.n_namesz);
13877 : :
13878 : 4616 : char buf[100];
13879 : 4616 : char buf2[100];
13880 : 4616 : printf (_(" %-13.*s %9" PRId32 " %s\n"),
13881 : : (int) print_namesz, print_name, nhdr.n_descsz,
13882 [ + + ]: 4616 : ehdr->e_type == ET_CORE
13883 : 120 : ? ebl_core_note_type_name (ebl, nhdr.n_type,
13884 : : buf, sizeof (buf))
13885 : 4496 : : ebl_object_note_type_name (ebl, name, nhdr.n_type,
13886 : : nhdr.n_descsz,
13887 : : buf2, sizeof (buf2)));
13888 : :
13889 : : /* Filter out invalid entries. */
13890 : 4616 : if (memchr (name, '\0', nhdr.n_namesz) != NULL
13891 : : /* XXX For now help broken Linux kernels. */
13892 : : || 1)
13893 : : {
13894 [ + + ]: 4616 : if (ehdr->e_type == ET_CORE)
13895 : : {
13896 [ + + ]: 120 : if (nhdr.n_type == NT_AUXV
13897 [ + - ]: 18 : && (nhdr.n_namesz == 4 /* Broken old Linux kernels. */
13898 [ + - + - ]: 18 : || (nhdr.n_namesz == 5 && name[4] == '\0'))
13899 [ + - ]: 18 : && !memcmp (name, "CORE", 4))
13900 : 18 : handle_auxv_note (ebl, ebl->elf, nhdr.n_descsz,
13901 : : start + desc_offset);
13902 [ + + + - ]: 102 : else if (nhdr.n_namesz == 5 && strcmp (name, "CORE") == 0)
13903 [ + + + ]: 78 : switch (nhdr.n_type)
13904 : : {
13905 : 12 : case NT_SIGINFO:
13906 : 12 : handle_siginfo_note (ebl->elf, nhdr.n_descsz,
13907 : : start + desc_offset);
13908 : 12 : break;
13909 : :
13910 : 12 : case NT_FILE:
13911 : 12 : handle_file_note (ebl->elf, nhdr.n_descsz,
13912 : : start + desc_offset);
13913 : 12 : break;
13914 : :
13915 : 54 : default:
13916 : 54 : handle_core_note (ebl, ehdr, &nhdr, name, desc);
13917 : : }
13918 : : else
13919 : 24 : handle_core_note (ebl, ehdr, &nhdr, name, desc);
13920 : : }
13921 : : else
13922 : 4496 : ebl_object_note (ebl, nhdr.n_namesz, name, nhdr.n_type,
13923 : : nhdr.n_descsz, desc);
13924 : : }
13925 : : }
13926 : :
13927 [ + - ]: 508 : if (offset == data->d_size)
13928 : 508 : return;
13929 : :
13930 : 0 : bad_note:
13931 : 0 : error (0, 0,
13932 : 0 : _("cannot get content of note: %s"),
13933 : 0 : data != NULL ? "garbage data" : elf_errmsg (-1));
13934 : : }
13935 : :
13936 : : static void
13937 : 232 : handle_notes (Ebl *ebl, GElf_Ehdr *ehdr)
13938 : : {
13939 : : /* If we have section headers, just look for SHT_NOTE sections.
13940 : : In a debuginfo file, the program headers are not reliable. */
13941 [ + + ]: 232 : if (shnum != 0)
13942 : : {
13943 : : /* Get the section header string table index. */
13944 : 208 : size_t shstrndx;
13945 [ - + ]: 208 : if (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0)
13946 : 0 : error_exit (0, _("cannot get section header string table index"));
13947 : :
13948 : : Elf_Scn *scn = NULL;
13949 [ + + ]: 6448 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
13950 : : {
13951 : 6240 : GElf_Shdr shdr_mem;
13952 : 6240 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
13953 : :
13954 [ + - + + ]: 6240 : if (shdr == NULL || shdr->sh_type != SHT_NOTE)
13955 : : /* Not what we are looking for. */
13956 : 5758 : continue;
13957 : :
13958 [ - + ]: 482 : if (notes_section != NULL)
13959 : : {
13960 : 0 : char *sname = elf_strptr (ebl->elf, shstrndx, shdr->sh_name);
13961 [ # # # # ]: 0 : if (sname == NULL || strcmp (sname, notes_section) != 0)
13962 : 0 : continue;
13963 : : }
13964 : :
13965 : 482 : printf (_("\
13966 : : \nNote section [%2zu] '%s' of %" PRIu64 " bytes at offset %#0" PRIx64 ":\n"),
13967 : : elf_ndxscn (scn),
13968 : 482 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
13969 : : shdr->sh_size, shdr->sh_offset);
13970 : :
13971 : 482 : handle_notes_data (ebl, ehdr, shdr->sh_offset,
13972 : : elf_getdata (scn, NULL));
13973 : : }
13974 : 208 : return;
13975 : : }
13976 : :
13977 : : /* We have to look through the program header to find the note
13978 : : sections. There can be more than one. */
13979 [ + + ]: 284 : for (size_t cnt = 0; cnt < phnum; ++cnt)
13980 : : {
13981 : 260 : GElf_Phdr mem;
13982 : 260 : GElf_Phdr *phdr = gelf_getphdr (ebl->elf, cnt, &mem);
13983 : :
13984 [ + - + + ]: 260 : if (phdr == NULL || phdr->p_type != PT_NOTE)
13985 : : /* Not what we are looking for. */
13986 : 234 : continue;
13987 : :
13988 : 26 : printf (_("\
13989 : : \nNote segment of %" PRIu64 " bytes at offset %#0" PRIx64 ":\n"),
13990 : : phdr->p_filesz, phdr->p_offset);
13991 : :
13992 : 26 : handle_notes_data (ebl, ehdr, phdr->p_offset,
13993 : : elf_getdata_rawchunk (ebl->elf,
13994 : 26 : phdr->p_offset, phdr->p_filesz,
13995 [ + + ]: 26 : (phdr->p_align == 8
13996 : : ? ELF_T_NHDR8 : ELF_T_NHDR)));
13997 : : }
13998 : : }
13999 : :
14000 : :
14001 : : static void
14002 : 12 : hex_dump (const uint8_t *data, size_t len)
14003 : : {
14004 : 12 : size_t pos = 0;
14005 [ + + ]: 56 : while (pos < len)
14006 : : {
14007 : 44 : printf (" 0x%08zx ", pos);
14008 : :
14009 : 44 : const size_t chunk = MIN (len - pos, 16);
14010 : :
14011 [ + + ]: 688 : for (size_t i = 0; i < chunk; ++i)
14012 [ + + ]: 644 : if (i % 4 == 3)
14013 : 160 : printf ("%02x ", data[pos + i]);
14014 : : else
14015 : 484 : printf ("%02x", data[pos + i]);
14016 : :
14017 [ + + ]: 44 : if (chunk < 16)
14018 : 4 : printf ("%*s", (int) ((16 - chunk) * 2 + (16 - chunk + 3) / 4), "");
14019 : :
14020 [ + + ]: 688 : for (size_t i = 0; i < chunk; ++i)
14021 : : {
14022 : 644 : unsigned char b = data[pos + i];
14023 [ + + ]: 1288 : printf ("%c", isprint (b) ? b : '.');
14024 : : }
14025 : :
14026 : 44 : putchar ('\n');
14027 : 44 : pos += chunk;
14028 : : }
14029 : 12 : }
14030 : :
14031 : : static void
14032 : 12 : dump_data_section (Elf_Scn *scn, const GElf_Shdr *shdr, const char *name)
14033 : : {
14034 [ + - - + ]: 12 : if (shdr->sh_size == 0 || shdr->sh_type == SHT_NOBITS)
14035 : 0 : printf (_("\nSection [%zu] '%s' has no data to dump.\n"),
14036 : : elf_ndxscn (scn), name);
14037 : : else
14038 : : {
14039 [ + + ]: 12 : if (print_decompress)
14040 : : {
14041 : : /* We try to decompress the section, but keep the old shdr around
14042 : : so we can show both the original shdr size and the uncompressed
14043 : : data size. */
14044 [ + + ]: 8 : if ((shdr->sh_flags & SHF_COMPRESSED) != 0)
14045 : : {
14046 [ - + ]: 4 : if (elf_compress (scn, 0, 0) < 0)
14047 : 0 : printf ("WARNING: %s [%zd]\n",
14048 : : _("Couldn't uncompress section"),
14049 : : elf_ndxscn (scn));
14050 : : }
14051 [ + - + - ]: 4 : else if (name && startswith (name, ".zdebug"))
14052 : : {
14053 [ - + ]: 4 : if (elf_compress_gnu (scn, 0, 0) < 0)
14054 : 0 : printf ("WARNING: %s [%zd]\n",
14055 : : _("Couldn't uncompress section"),
14056 : : elf_ndxscn (scn));
14057 : : }
14058 : : }
14059 : :
14060 : 12 : Elf_Data *data = elf_rawdata (scn, NULL);
14061 [ - + ]: 12 : if (data == NULL)
14062 : 0 : error (0, 0, _("cannot get data for section [%zu] '%s': %s"),
14063 : : elf_ndxscn (scn), name, elf_errmsg (-1));
14064 : : else
14065 : : {
14066 [ + + ]: 12 : if (data->d_size == shdr->sh_size)
14067 : 4 : printf (_("\nHex dump of section [%zu] '%s', %" PRIu64
14068 : : " bytes at offset %#0" PRIx64 ":\n"),
14069 : : elf_ndxscn (scn), name,
14070 : 4 : shdr->sh_size, shdr->sh_offset);
14071 : : else
14072 : 8 : printf (_("\nHex dump of section [%zu] '%s', %" PRIu64
14073 : : " bytes (%zd uncompressed) at offset %#0"
14074 : : PRIx64 ":\n"),
14075 : : elf_ndxscn (scn), name,
14076 : 8 : shdr->sh_size, data->d_size, shdr->sh_offset);
14077 : 12 : hex_dump (data->d_buf, data->d_size);
14078 : : }
14079 : : }
14080 : 12 : }
14081 : :
14082 : : static void
14083 : 374 : print_string_section (Elf_Scn *scn, const GElf_Shdr *shdr, const char *name)
14084 : : {
14085 [ + - + + ]: 374 : if (shdr->sh_size == 0 || shdr->sh_type == SHT_NOBITS)
14086 : 42 : printf (_("\nSection [%zu] '%s' has no strings to dump.\n"),
14087 : : elf_ndxscn (scn), name);
14088 : : else
14089 : : {
14090 [ + + ]: 332 : if (print_decompress)
14091 : : {
14092 : : /* We try to decompress the section, but keep the old shdr around
14093 : : so we can show both the original shdr size and the uncompressed
14094 : : data size. */
14095 [ - + ]: 2 : if ((shdr->sh_flags & SHF_COMPRESSED) != 0)
14096 : : {
14097 [ # # ]: 0 : if (elf_compress (scn, 0, 0) < 0)
14098 : 0 : printf ("WARNING: %s [%zd]\n",
14099 : : _("Couldn't uncompress section"),
14100 : : elf_ndxscn (scn));
14101 : : }
14102 [ + - + - ]: 2 : else if (name && startswith (name, ".zdebug"))
14103 : : {
14104 [ - + ]: 2 : if (elf_compress_gnu (scn, 0, 0) < 0)
14105 : 0 : printf ("WARNING: %s [%zd]\n",
14106 : : _("Couldn't uncompress section"),
14107 : : elf_ndxscn (scn));
14108 : : }
14109 : : }
14110 : :
14111 : 332 : Elf_Data *data = elf_rawdata (scn, NULL);
14112 [ - + ]: 332 : if (data == NULL)
14113 : 0 : error (0, 0, _("cannot get data for section [%zu] '%s': %s"),
14114 : : elf_ndxscn (scn), name, elf_errmsg (-1));
14115 : : else
14116 : : {
14117 [ + + ]: 332 : if (data->d_size == shdr->sh_size)
14118 : 330 : printf (_("\nString section [%zu] '%s' contains %" PRIu64
14119 : : " bytes at offset %#0" PRIx64 ":\n"),
14120 : : elf_ndxscn (scn), name,
14121 : 330 : shdr->sh_size, shdr->sh_offset);
14122 : : else
14123 : 2 : printf (_("\nString section [%zu] '%s' contains %" PRIu64
14124 : : " bytes (%zd uncompressed) at offset %#0"
14125 : : PRIx64 ":\n"),
14126 : : elf_ndxscn (scn), name,
14127 : 2 : shdr->sh_size, data->d_size, shdr->sh_offset);
14128 : :
14129 : 332 : const char *start = data->d_buf;
14130 : 332 : const char *const limit = start + data->d_size;
14131 : 51760 : do
14132 : : {
14133 : 51760 : const char *end = memchr (start, '\0', limit - start);
14134 : 51760 : const size_t pos = start - (const char *) data->d_buf;
14135 [ - + ]: 51760 : if (unlikely (end == NULL))
14136 : : {
14137 : 0 : printf (" [%6zx]- %.*s\n",
14138 : : pos, (int) (limit - start), start);
14139 : 0 : break;
14140 : : }
14141 : 51760 : printf (" [%6zx] %s\n", pos, start);
14142 : 51760 : start = end + 1;
14143 [ + + ]: 51760 : } while (start < limit);
14144 : : }
14145 : : }
14146 : 374 : }
14147 : :
14148 : : static void
14149 : 198 : for_each_section_argument (Elf *elf, const struct section_argument *list,
14150 : : void (*dump) (Elf_Scn *scn, const GElf_Shdr *shdr,
14151 : : const char *name))
14152 : : {
14153 : : /* Get the section header string table index. */
14154 : 198 : size_t shstrndx;
14155 [ - + ]: 198 : if (elf_getshdrstrndx (elf, &shstrndx) < 0)
14156 : 0 : error_exit (0, _("cannot get section header string table index"));
14157 : :
14158 [ + + ]: 764 : for (const struct section_argument *a = list; a != NULL; a = a->next)
14159 : : {
14160 : 566 : Elf_Scn *scn;
14161 : 566 : GElf_Shdr shdr_mem;
14162 : 566 : const char *name = NULL;
14163 : :
14164 : 566 : char *endp = NULL;
14165 : 566 : unsigned long int shndx = strtoul (a->arg, &endp, 0);
14166 [ + + - + ]: 566 : if (endp != a->arg && *endp == '\0')
14167 : : {
14168 : 2 : scn = elf_getscn (elf, shndx);
14169 [ - + ]: 2 : if (scn == NULL)
14170 : : {
14171 : 0 : error (0, 0, _("\nsection [%lu] does not exist"), shndx);
14172 : 0 : continue;
14173 : : }
14174 : :
14175 [ - + ]: 2 : if (gelf_getshdr (scn, &shdr_mem) == NULL)
14176 : 0 : error_exit (0, _("cannot get section header: %s"),
14177 : : elf_errmsg (-1));
14178 : 2 : name = elf_strptr (elf, shstrndx, shdr_mem.sh_name);
14179 : 2 : (*dump) (scn, &shdr_mem, name);
14180 : : }
14181 : : else
14182 : : {
14183 : : /* Need to look up the section by name. */
14184 : : scn = NULL;
14185 : : bool found = false;
14186 [ + + ]: 18186 : while ((scn = elf_nextscn (elf, scn)) != NULL)
14187 : : {
14188 [ - + ]: 17622 : if (gelf_getshdr (scn, &shdr_mem) == NULL)
14189 : 0 : continue;
14190 : 17622 : name = elf_strptr (elf, shstrndx, shdr_mem.sh_name);
14191 [ - + ]: 17622 : if (name == NULL)
14192 : 0 : continue;
14193 [ + + ]: 17622 : if (!strcmp (name, a->arg))
14194 : : {
14195 : 384 : found = true;
14196 : 384 : (*dump) (scn, &shdr_mem, name);
14197 : : }
14198 : : }
14199 : :
14200 [ + + - + ]: 564 : if (unlikely (!found) && !a->implicit)
14201 : 0 : error (0, 0, _("\nsection '%s' does not exist"), a->arg);
14202 : : }
14203 : : }
14204 : 198 : }
14205 : :
14206 : : static void
14207 : 12 : dump_data (Ebl *ebl)
14208 : : {
14209 : 12 : for_each_section_argument (ebl->elf, dump_data_sections, &dump_data_section);
14210 : 12 : }
14211 : :
14212 : : static void
14213 : 186 : dump_strings (Ebl *ebl)
14214 : : {
14215 : 186 : for_each_section_argument (ebl->elf, string_sections, &print_string_section);
14216 : 186 : }
14217 : :
14218 : : static void
14219 : 0 : print_strings (Ebl *ebl)
14220 : : {
14221 : : /* Get the section header string table index. */
14222 : 0 : size_t shstrndx;
14223 [ # # ]: 0 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
14224 : 0 : error_exit (0, _("cannot get section header string table index"));
14225 : :
14226 : : Elf_Scn *scn;
14227 : : GElf_Shdr shdr_mem;
14228 : : const char *name;
14229 : : scn = NULL;
14230 [ # # ]: 0 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
14231 : : {
14232 [ # # ]: 0 : if (gelf_getshdr (scn, &shdr_mem) == NULL)
14233 : 0 : continue;
14234 : :
14235 [ # # ]: 0 : if (shdr_mem.sh_type != SHT_PROGBITS
14236 [ # # ]: 0 : || !(shdr_mem.sh_flags & SHF_STRINGS))
14237 : 0 : continue;
14238 : :
14239 : 0 : name = elf_strptr (ebl->elf, shstrndx, shdr_mem.sh_name);
14240 [ # # ]: 0 : if (name == NULL)
14241 : 0 : continue;
14242 : :
14243 : 0 : print_string_section (scn, &shdr_mem, name);
14244 : : }
14245 : 0 : }
14246 : :
14247 : : static void
14248 : 4 : dump_archive_index (Elf *elf, const char *fname)
14249 : : {
14250 : 4 : size_t narsym;
14251 : 4 : const Elf_Arsym *arsym = elf_getarsym (elf, &narsym);
14252 [ - + ]: 4 : if (arsym == NULL)
14253 : : {
14254 : 0 : int result = elf_errno ();
14255 [ # # ]: 0 : if (unlikely (result != ELF_E_NO_INDEX))
14256 : 0 : error_exit (0, _("cannot get symbol index of archive '%s': %s"),
14257 : : fname, elf_errmsg (result));
14258 : : else
14259 : 0 : printf (_("\nArchive '%s' has no symbol index\n"), fname);
14260 : 0 : return;
14261 : : }
14262 : :
14263 : 4 : printf (_("\nIndex of archive '%s' has %zu entries:\n"),
14264 : : fname, narsym);
14265 : :
14266 : 4 : size_t as_off = 0;
14267 [ + + ]: 22 : for (const Elf_Arsym *s = arsym; s < &arsym[narsym - 1]; ++s)
14268 : : {
14269 [ + + ]: 18 : if (s->as_off != as_off)
14270 : : {
14271 : 12 : as_off = s->as_off;
14272 : :
14273 : 12 : Elf *subelf = NULL;
14274 [ + - ]: 12 : if (unlikely (elf_rand (elf, as_off) == 0)
14275 [ - + ]: 12 : || unlikely ((subelf = elf_begin (-1, ELF_C_READ_MMAP, elf))
14276 : : == NULL))
14277 : : #if __GLIBC__ < 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ < 7)
14278 : : while (1)
14279 : : #endif
14280 : 0 : error_exit (0,
14281 : : _("cannot extract member at offset %zu in '%s': %s"),
14282 : : as_off, fname, elf_errmsg (-1));
14283 : :
14284 : 12 : const Elf_Arhdr *h = elf_getarhdr (subelf);
14285 [ + - ]: 12 : if (h != NULL)
14286 : 12 : printf (_("Archive member '%s' contains:\n"), h->ar_name);
14287 : :
14288 : 12 : elf_end (subelf);
14289 : : }
14290 : :
14291 : 18 : printf ("\t%s\n", s->as_name);
14292 : : }
14293 : : }
14294 : :
14295 : : #include "debugpred.h"
|