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 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 __linux__
61 : : #define CORE_SIGILL SIGILL
62 : : #define CORE_SIGBUS SIGBUS
63 : : #define CORE_SIGFPE SIGFPE
64 : : #define CORE_SIGSEGV SIGSEGV
65 : : #define CORE_SI_USER SI_USER
66 : : #else
67 : : /* We want the linux version of those as that is what shows up in the core files. */
68 : : #define CORE_SIGILL 4 /* Illegal instruction (ANSI). */
69 : : #define CORE_SIGBUS 7 /* BUS error (4.2 BSD). */
70 : : #define CORE_SIGFPE 8 /* Floating-point exception (ANSI). */
71 : : #define CORE_SIGSEGV 11 /* Segmentation violation (ANSI). */
72 : : #define CORE_SI_USER 0 /* Sent by kill, sigsend. */
73 : : #endif
74 : :
75 : : /* Name and version of program. */
76 : : ARGP_PROGRAM_VERSION_HOOK_DEF = print_version;
77 : :
78 : : /* Bug report address. */
79 : : ARGP_PROGRAM_BUG_ADDRESS_DEF = PACKAGE_BUGREPORT;
80 : :
81 : : /* argp key value for --elf-section, non-ascii. */
82 : : #define ELF_INPUT_SECTION 256
83 : :
84 : : /* argp key value for --dwarf-skeleton, non-ascii. */
85 : : #define DWARF_SKELETON 257
86 : :
87 : : /* argp key value for --dyn-syms, non-ascii. */
88 : : #define PRINT_DYNSYM_TABLE 258
89 : :
90 : : /* Terrible hack for hooking unrelated skeleton/split compile units,
91 : : see __libdw_link_skel_split in print_debug. */
92 : : static bool do_not_close_dwfl = false;
93 : :
94 : : /* Definitions of arguments for argp functions. */
95 : : static const struct argp_option options[] =
96 : : {
97 : : { NULL, 0, NULL, 0, N_("ELF input selection:"), 0 },
98 : : { "elf-section", ELF_INPUT_SECTION, "SECTION", OPTION_ARG_OPTIONAL,
99 : : N_("Use the named SECTION (default .gnu_debugdata) as (compressed) ELF "
100 : : "input data"), 0 },
101 : : { "dwarf-skeleton", DWARF_SKELETON, "FILE", 0,
102 : : N_("Used with -w to find the skeleton Compile Units in FILE associated "
103 : : "with the Split Compile units in a .dwo input file"), 0 },
104 : : { NULL, 0, NULL, 0, N_("ELF output selection:"), 0 },
105 : : { "all", 'a', NULL, 0,
106 : : N_("All these plus -p .strtab -p .dynstr -p .comment"), 0 },
107 : : { "dynamic", 'd', NULL, 0, N_("Display the dynamic segment"), 0 },
108 : : { "file-header", 'h', NULL, 0, N_("Display the ELF file header"), 0 },
109 : : { "histogram", 'I', NULL, 0,
110 : : N_("Display histogram of bucket list lengths"), 0 },
111 : : { "program-headers", 'l', NULL, 0, N_("Display the program headers"), 0 },
112 : : { "segments", 'l', NULL, OPTION_ALIAS | OPTION_HIDDEN, NULL, 0 },
113 : : { "relocs", 'r', NULL, 0, N_("Display relocations"), 0 },
114 : : { "section-groups", 'g', NULL, 0, N_("Display the section groups"), 0 },
115 : : { "section-headers", 'S', NULL, 0, N_("Display the sections' headers"), 0 },
116 : : { "sections", 'S', NULL, OPTION_ALIAS | OPTION_HIDDEN, NULL, 0 },
117 : : { "symbols", 's', "SECTION", OPTION_ARG_OPTIONAL,
118 : : N_("Display the symbol table sections"), 0 },
119 : : { "syms", 's', NULL, OPTION_ALIAS | OPTION_HIDDEN, NULL, 0 },
120 : : { "dyn-syms", PRINT_DYNSYM_TABLE, NULL, 0,
121 : : N_("Display (only) the dynamic symbol table"), 0 },
122 : : { "version-info", 'V', NULL, 0, N_("Display versioning information"), 0 },
123 : : { "notes", 'n', "SECTION", OPTION_ARG_OPTIONAL, N_("Display the ELF notes"), 0 },
124 : : { "arch-specific", 'A', NULL, 0,
125 : : N_("Display architecture specific information, if any"), 0 },
126 : : { "exception", 'e', NULL, 0,
127 : : N_("Display sections for exception handling"), 0 },
128 : :
129 : : { NULL, 0, NULL, 0, N_("Additional output selection:"), 0 },
130 : : { "debug-dump", 'w', "SECTION", OPTION_ARG_OPTIONAL,
131 : : N_("Display DWARF section content. SECTION can be one of abbrev, addr, "
132 : : "aranges, decodedaranges, frame, gdb_index, info, info+, loc, line, "
133 : : "decodedline, ranges, pubnames, str, macinfo, macro or exception"), 0 },
134 : : { "hex-dump", 'x', "SECTION", 0,
135 : : N_("Dump the uninterpreted contents of SECTION, by number or name"), 0 },
136 : : { "strings", 'p', "SECTION", OPTION_ARG_OPTIONAL,
137 : : N_("Print string contents of sections"), 0 },
138 : : { "string-dump", 'p', NULL, OPTION_ALIAS | OPTION_HIDDEN, NULL, 0 },
139 : : { "archive-index", 'c', NULL, 0,
140 : : N_("Display the symbol index of an archive"), 0 },
141 : : { "use-dynamic", 'D', NULL, 0,
142 : : N_("Use the dynamic segment when possible for displaying info"), 0 },
143 : :
144 : : { NULL, 0, NULL, 0, N_("Output control:"), 0 },
145 : : { "numeric-addresses", 'N', NULL, 0,
146 : : N_("Do not find symbol names for addresses in DWARF data"), 0 },
147 : : { "unresolved-address-offsets", 'U', NULL, 0,
148 : : N_("Display just offsets instead of resolving values to addresses in DWARF data"), 0 },
149 : : { "wide", 'W', NULL, 0,
150 : : N_("Ignored for compatibility (lines always wide)"), 0 },
151 : : { "decompress", 'z', NULL, 0,
152 : : N_("Show compression information for compressed sections (when used with -S); decompress section before dumping data (when used with -p or -x)"), 0 },
153 : : { NULL, 0, NULL, 0, NULL, 0 }
154 : : };
155 : :
156 : : /* Short description of program. */
157 : : static const char doc[] = N_("\
158 : : Print information from ELF file in human-readable form.");
159 : :
160 : : /* Strings for arguments in help texts. */
161 : : static const char args_doc[] = N_("FILE...");
162 : :
163 : : /* Prototype for option handler. */
164 : : static error_t parse_opt (int key, char *arg, struct argp_state *state);
165 : :
166 : : /* Data structure to communicate with argp functions. */
167 : : static struct argp argp =
168 : : {
169 : : options, parse_opt, args_doc, doc, NULL, NULL, NULL
170 : : };
171 : :
172 : : /* If non-null, the section from which we should read to (compressed) ELF. */
173 : : static const char *elf_input_section = NULL;
174 : :
175 : : /* If non-null, the file that contains the skeleton CUs. */
176 : : static const char *dwarf_skeleton = NULL;
177 : :
178 : : /* Flags set by the option controlling the output. */
179 : :
180 : : /* True if dynamic segment should be printed. */
181 : : static bool print_dynamic_table;
182 : :
183 : : /* True if the file header should be printed. */
184 : : static bool print_file_header;
185 : :
186 : : /* True if the program headers should be printed. */
187 : : static bool print_program_header;
188 : :
189 : : /* True if relocations should be printed. */
190 : : static bool print_relocations;
191 : :
192 : : /* True if the section headers should be printed. */
193 : : static bool print_section_header;
194 : :
195 : : /* True if the symbol table should be printed. */
196 : : static bool print_symbol_table;
197 : :
198 : : /* True if (only) the dynsym table should be printed. */
199 : : static bool print_dynsym_table;
200 : :
201 : : /* True if reconstruct dynamic symbol table from the PT_DYNAMIC segment. */
202 : : static bool use_dynamic_segment;
203 : :
204 : : /* A specific section name, or NULL to print all symbol tables. */
205 : : static char *symbol_table_section;
206 : :
207 : : /* A specific section name, or NULL to print all ELF notes. */
208 : : static char *notes_section;
209 : :
210 : : /* True if the version information should be printed. */
211 : : static bool print_version_info;
212 : :
213 : : /* True if section groups should be printed. */
214 : : static bool print_section_groups;
215 : :
216 : : /* True if bucket list length histogram should be printed. */
217 : : static bool print_histogram;
218 : :
219 : : /* True if the architecture specific data should be printed. */
220 : : static bool print_arch;
221 : :
222 : : /* True if note section content should be printed. */
223 : : static bool print_notes;
224 : :
225 : : /* True if SHF_STRINGS section content should be printed. */
226 : : static bool print_string_sections;
227 : :
228 : : /* True if archive index should be printed. */
229 : : static bool print_archive_index;
230 : :
231 : : /* True if any of the control options except print_archive_index is set. */
232 : : static bool any_control_option;
233 : :
234 : : /* True if we should print addresses from DWARF in symbolic form. */
235 : : static bool print_address_names = true;
236 : :
237 : : /* True if we should print raw values instead of relativized addresses. */
238 : : static bool print_unresolved_addresses = false;
239 : :
240 : : /* True if we should print the .debug_aranges section using libdw. */
241 : : static bool decodedaranges = false;
242 : :
243 : : /* True if we should print the .debug_aranges section using libdw. */
244 : : static bool decodedline = false;
245 : :
246 : : /* True if we want to show more information about compressed sections. */
247 : : static bool print_decompress = false;
248 : :
249 : : /* True if we want to show split compile units for debug_info skeletons. */
250 : : static bool show_split_units = false;
251 : :
252 : : /* Select printing of debugging sections. */
253 : : static enum section_e
254 : : {
255 : : section_abbrev = 1, /* .debug_abbrev */
256 : : section_aranges = 2, /* .debug_aranges */
257 : : section_frame = 4, /* .debug_frame or .eh_frame & al. */
258 : : section_info = 8, /* .debug_info, (implies .debug_types) */
259 : : section_line = 16, /* .debug_line */
260 : : section_loc = 32, /* .debug_loc */
261 : : section_pubnames = 64, /* .debug_pubnames */
262 : : section_str = 128, /* .debug_str */
263 : : section_macinfo = 256, /* .debug_macinfo */
264 : : section_ranges = 512, /* .debug_ranges */
265 : : section_exception = 1024, /* .eh_frame & al. */
266 : : section_gdb_index = 2048, /* .gdb_index */
267 : : section_macro = 4096, /* .debug_macro */
268 : : section_addr = 8192, /* .debug_addr */
269 : : section_types = 16384, /* .debug_types (implied by .debug_info) */
270 : : section_all = (section_abbrev | section_aranges | section_frame
271 : : | section_info | section_line | section_loc
272 : : | section_pubnames | section_str | section_macinfo
273 : : | section_ranges | section_exception | section_gdb_index
274 : : | section_macro | section_addr | section_types)
275 : : } print_debug_sections, implicit_debug_sections;
276 : :
277 : : /* Select hex dumping of sections. */
278 : : static struct section_argument *dump_data_sections;
279 : : static struct section_argument **dump_data_sections_tail = &dump_data_sections;
280 : :
281 : : /* Select string dumping of sections. */
282 : : static struct section_argument *string_sections;
283 : : static struct section_argument **string_sections_tail = &string_sections;
284 : :
285 : : struct section_argument
286 : : {
287 : : struct section_argument *next;
288 : : const char *arg;
289 : : bool implicit;
290 : : };
291 : :
292 : : /* Numbers of sections and program headers in the file. */
293 : : static size_t shnum;
294 : : static size_t phnum;
295 : :
296 : :
297 : : /* Declarations of local functions. */
298 : : static void process_file (int fd, const char *fname, bool only_one);
299 : : static void process_elf_file (Dwfl_Module *dwflmod, int fd);
300 : : static void print_ehdr (Ebl *ebl, GElf_Ehdr *ehdr);
301 : : static void print_shdr (Ebl *ebl, GElf_Ehdr *ehdr);
302 : : static void print_phdr (Ebl *ebl, GElf_Ehdr *ehdr);
303 : : static void print_scngrp (Ebl *ebl);
304 : : static void print_dynamic (Ebl *ebl);
305 : : static void print_relocs (Ebl *ebl, Dwfl_Module *mod, GElf_Ehdr *ehdr);
306 : : static void handle_relocs_rel (Ebl *ebl, GElf_Ehdr *ehdr, Elf_Scn *scn,
307 : : GElf_Shdr *shdr);
308 : : static void handle_relocs_rela (Ebl *ebl, GElf_Ehdr *ehdr, Elf_Scn *scn,
309 : : GElf_Shdr *shdr);
310 : : static void handle_relocs_relr (Ebl *ebl, Dwfl_Module *mod, Elf_Scn *scn,
311 : : GElf_Shdr *shdr);
312 : : static bool print_symtab (Ebl *ebl, int type);
313 : : static bool handle_symtab (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr);
314 : : static bool handle_dynamic_symtab (Ebl *ebl);
315 : : static void
316 : : process_symtab(
317 : : Ebl * ebl,
318 : : unsigned int nsyms,
319 : : Elf64_Word idx,
320 : : Elf32_Word verneed_stridx,
321 : : Elf32_Word verdef_stridx,
322 : : Elf_Data * symdata,
323 : : Elf_Data * versym_data,
324 : : Elf_Data * symstr_data,
325 : : Elf_Data * verneed_data,
326 : : Elf_Data * verdef_data,
327 : : Elf_Data * xndx_data);
328 : : static void print_verinfo (Ebl *ebl);
329 : : static void handle_verneed (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr);
330 : : static void handle_verdef (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr);
331 : : static void handle_versym (Ebl *ebl, Elf_Scn *scn,
332 : : GElf_Shdr *shdr);
333 : : static void print_debug (Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr);
334 : : static void handle_hash (Ebl *ebl);
335 : : static void handle_notes (Ebl *ebl, GElf_Ehdr *ehdr);
336 : : static void print_liblist (Ebl *ebl);
337 : : static void print_attributes (Ebl *ebl, const GElf_Ehdr *ehdr);
338 : : static void dump_data (Ebl *ebl);
339 : : static void dump_strings (Ebl *ebl);
340 : : static void print_strings (Ebl *ebl);
341 : : static void dump_archive_index (Elf *, const char *);
342 : : static void print_dwarf_addr (Dwfl_Module *dwflmod, int address_size,
343 : : Dwarf_Addr address, Dwarf_Addr raw);
344 : :
345 : : enum dyn_idx
346 : : {
347 : : i_symtab_shndx,
348 : : i_strsz,
349 : : i_verneed,
350 : : i_verneednum,
351 : : i_verdef,
352 : : i_verdefnum,
353 : : i_versym,
354 : : i_symtab,
355 : : i_strtab,
356 : : i_hash,
357 : : i_gnu_hash,
358 : : i_max
359 : : };
360 : :
361 : : /* Declarations of local functions for use-dynamic. */
362 : : static Elf_Data *get_dynscn_strtab (Elf *elf, GElf_Phdr *phdr);
363 : : static void get_dynscn_addrs (Elf *elf, GElf_Phdr *phdr, GElf_Addr addrs[i_max]);
364 : : static void find_offsets (Elf *elf, GElf_Addr main_bias, size_t n,
365 : : GElf_Addr addrs[n], GElf_Off offs[n]);
366 : :
367 : : /* Looked up once with gettext in main. */
368 : : static char *yes_str;
369 : : static char *no_str;
370 : :
371 : : static void
372 : 6 : cleanup_list (struct section_argument *list)
373 : : {
374 [ - + - + ]: 6 : while (list != NULL)
375 : : {
376 : 0 : struct section_argument *a = list;
377 : 0 : list = a->next;
378 : 0 : free (a);
379 : : }
380 : : }
381 : :
382 : : int
383 : 3 : main (int argc, char *argv[])
384 : : {
385 : : /* We use no threads here which can interfere with handling a stream. */
386 : 3 : (void) __fsetlocking (stdout, FSETLOCKING_BYCALLER);
387 : :
388 : : /* Set locale. */
389 : 3 : setlocale (LC_ALL, "");
390 : :
391 : : /* Initialize the message catalog. */
392 : 3 : textdomain (PACKAGE_TARNAME);
393 : :
394 : : /* Look up once. */
395 : 3 : yes_str = _("yes");
396 : 3 : no_str = _("no");
397 : :
398 : : /* Parse and process arguments. */
399 : 3 : int remaining;
400 : 3 : argp_parse (&argp, argc, argv, 0, &remaining, NULL);
401 : :
402 : : /* Before we start tell the ELF library which version we are using. */
403 : 3 : elf_version (EV_CURRENT);
404 : :
405 : : /* Now process all the files given at the command line. */
406 : 3 : bool only_one = remaining + 1 == argc;
407 : 3 : do
408 : : {
409 : : /* Open the file. */
410 : 3 : int fd = open (argv[remaining], O_RDONLY);
411 [ - + ]: 3 : if (fd == -1)
412 : : {
413 : 0 : error (0, errno, _("cannot open input file '%s'"), argv[remaining]);
414 : 0 : continue;
415 : : }
416 : :
417 : 3 : process_file (fd, argv[remaining], only_one);
418 : :
419 : 3 : close (fd);
420 : : }
421 [ - + ]: 3 : while (++remaining < argc);
422 : :
423 : 3 : cleanup_list (dump_data_sections);
424 : 3 : cleanup_list (string_sections);
425 : :
426 : 3 : return error_message_count != 0;
427 : : }
428 : :
429 : : static void
430 : 0 : add_dump_section (const char *name,
431 : : int key,
432 : : bool implicit)
433 : : {
434 : 0 : struct section_argument *a = xmalloc (sizeof *a);
435 : 0 : a->arg = name;
436 : 0 : a->next = NULL;
437 : 0 : a->implicit = implicit;
438 : 0 : struct section_argument ***tailp
439 [ # # ]: 0 : = key == 'x' ? &dump_data_sections_tail : &string_sections_tail;
440 : 0 : **tailp = a;
441 : 0 : *tailp = &a->next;
442 : 0 : }
443 : :
444 : : /* Handle program arguments. */
445 : : static error_t
446 : 18 : parse_opt (int key, char *arg,
447 : : struct argp_state *state __attribute__ ((unused)))
448 : : {
449 [ - - - - : 18 : switch (key)
- - - + -
- - - + -
- - - - -
- - - - +
- - - + ]
450 : : {
451 : 0 : case 'a':
452 : 0 : print_file_header = true;
453 : 0 : print_program_header = true;
454 : 0 : print_relocations = true;
455 : 0 : print_section_header = true;
456 : 0 : print_symbol_table = true;
457 : 0 : print_version_info = true;
458 : 0 : print_dynamic_table = true;
459 : 0 : print_section_groups = true;
460 : 0 : print_histogram = true;
461 : 0 : print_arch = true;
462 : 0 : print_notes = true;
463 : 0 : implicit_debug_sections |= section_exception;
464 : 0 : add_dump_section (".strtab", key, true);
465 : 0 : add_dump_section (".dynstr", key, true);
466 : 0 : add_dump_section (".comment", key, true);
467 : 0 : any_control_option = true;
468 : 0 : break;
469 : 0 : case 'A':
470 : 0 : print_arch = true;
471 : 0 : any_control_option = true;
472 : 0 : break;
473 : 0 : case 'd':
474 : 0 : print_dynamic_table = true;
475 : 0 : any_control_option = true;
476 : 0 : break;
477 : 0 : case 'D':
478 : 0 : use_dynamic_segment = true;
479 : 0 : break;
480 : 0 : case 'e':
481 : 0 : print_debug_sections |= section_exception;
482 : 0 : any_control_option = true;
483 : 0 : break;
484 : 0 : case 'g':
485 : 0 : print_section_groups = true;
486 : 0 : any_control_option = true;
487 : 0 : break;
488 : 1 : case 'h':
489 : 1 : print_file_header = true;
490 : 1 : any_control_option = true;
491 : 1 : break;
492 : 0 : case 'I':
493 : 0 : print_histogram = true;
494 : 0 : any_control_option = true;
495 : 0 : break;
496 : 0 : case 'l':
497 : 0 : print_program_header = true;
498 : 0 : any_control_option = true;
499 : 0 : break;
500 : 0 : case 'n':
501 : 0 : print_notes = true;
502 : 0 : any_control_option = true;
503 : 0 : notes_section = arg;
504 : 0 : break;
505 : 0 : case 'r':
506 : 0 : print_relocations = true;
507 : 0 : any_control_option = true;
508 : 0 : break;
509 : 2 : case 'S':
510 : 2 : print_section_header = true;
511 : 2 : any_control_option = true;
512 : 2 : break;
513 : 0 : case 's':
514 : 0 : print_symbol_table = true;
515 : 0 : any_control_option = true;
516 : 0 : symbol_table_section = arg;
517 : 0 : break;
518 : 0 : case PRINT_DYNSYM_TABLE:
519 : 0 : print_dynsym_table = true;
520 : 0 : any_control_option = true;
521 : 0 : break;
522 : 0 : case 'V':
523 : 0 : print_version_info = true;
524 : 0 : any_control_option = true;
525 : 0 : break;
526 : 0 : case 'c':
527 : 0 : print_archive_index = true;
528 : 0 : break;
529 : 0 : case 'w':
530 [ # # ]: 0 : if (arg == NULL)
531 : : {
532 : 0 : print_debug_sections = section_all;
533 : 0 : implicit_debug_sections = section_info;
534 : 0 : show_split_units = true;
535 : : }
536 [ # # ]: 0 : else if (strcmp (arg, "abbrev") == 0)
537 : 0 : print_debug_sections |= section_abbrev;
538 [ # # ]: 0 : else if (strcmp (arg, "addr") == 0)
539 : : {
540 : 0 : print_debug_sections |= section_addr;
541 : 0 : implicit_debug_sections |= section_info;
542 : : }
543 [ # # ]: 0 : else if (strcmp (arg, "aranges") == 0)
544 : 0 : print_debug_sections |= section_aranges;
545 [ # # ]: 0 : else if (strcmp (arg, "decodedaranges") == 0)
546 : : {
547 : 0 : print_debug_sections |= section_aranges;
548 : 0 : decodedaranges = true;
549 : : }
550 [ # # ]: 0 : else if (strcmp (arg, "ranges") == 0)
551 : : {
552 : 0 : print_debug_sections |= section_ranges;
553 : 0 : implicit_debug_sections |= section_info;
554 : : }
555 [ # # # # ]: 0 : else if (strcmp (arg, "frame") == 0 || strcmp (arg, "frames") == 0)
556 : 0 : print_debug_sections |= section_frame;
557 [ # # ]: 0 : else if (strcmp (arg, "info") == 0)
558 : : {
559 : 0 : print_debug_sections |= section_info;
560 : 0 : print_debug_sections |= section_types;
561 : : }
562 [ # # ]: 0 : else if (strcmp (arg, "info+") == 0)
563 : : {
564 : 0 : print_debug_sections |= section_info;
565 : 0 : print_debug_sections |= section_types;
566 : 0 : show_split_units = true;
567 : : }
568 [ # # ]: 0 : else if (strcmp (arg, "loc") == 0)
569 : : {
570 : 0 : print_debug_sections |= section_loc;
571 : 0 : implicit_debug_sections |= section_info;
572 : : }
573 [ # # ]: 0 : else if (strcmp (arg, "line") == 0)
574 : 0 : print_debug_sections |= section_line;
575 [ # # ]: 0 : else if (strcmp (arg, "decodedline") == 0)
576 : : {
577 : 0 : print_debug_sections |= section_line;
578 : 0 : decodedline = true;
579 : : }
580 [ # # ]: 0 : else if (strcmp (arg, "pubnames") == 0)
581 : 0 : print_debug_sections |= section_pubnames;
582 [ # # ]: 0 : else if (strcmp (arg, "str") == 0)
583 : : {
584 : 0 : print_debug_sections |= section_str;
585 : : /* For mapping string offset tables to CUs. */
586 : 0 : implicit_debug_sections |= section_info;
587 : : }
588 [ # # ]: 0 : else if (strcmp (arg, "macinfo") == 0)
589 : 0 : print_debug_sections |= section_macinfo;
590 [ # # ]: 0 : else if (strcmp (arg, "macro") == 0)
591 : 0 : print_debug_sections |= section_macro;
592 [ # # ]: 0 : else if (strcmp (arg, "exception") == 0)
593 : 0 : print_debug_sections |= section_exception;
594 [ # # ]: 0 : else if (strcmp (arg, "gdb_index") == 0)
595 : 0 : print_debug_sections |= section_gdb_index;
596 : : else
597 : : {
598 : 0 : fprintf (stderr, _("Unknown DWARF debug section `%s'.\n"),
599 : : arg);
600 : 0 : argp_help (&argp, stderr, ARGP_HELP_SEE,
601 : : program_invocation_short_name);
602 : 0 : exit (1);
603 : : }
604 : 0 : any_control_option = true;
605 : 0 : break;
606 : 0 : case 'p':
607 : 0 : any_control_option = true;
608 [ # # ]: 0 : if (arg == NULL)
609 : : {
610 : 0 : print_string_sections = true;
611 : 0 : break;
612 : : }
613 : 0 : FALLTHROUGH;
614 : : case 'x':
615 : 0 : add_dump_section (arg, key, false);
616 : 0 : any_control_option = true;
617 : 0 : break;
618 : 0 : case 'N':
619 : 0 : print_address_names = false;
620 : 0 : break;
621 : 0 : case 'U':
622 : 0 : print_unresolved_addresses = true;
623 : 0 : break;
624 : 0 : case ARGP_KEY_NO_ARGS:
625 : 0 : fputs (_("Missing file name.\n"), stderr);
626 : 0 : goto do_argp_help;
627 : 3 : case ARGP_KEY_FINI:
628 [ + - - - ]: 3 : if (! any_control_option && ! print_archive_index)
629 : : {
630 : 0 : fputs (_("No operation specified.\n"), stderr);
631 : 0 : do_argp_help:
632 : 0 : argp_help (&argp, stderr, ARGP_HELP_SEE,
633 : : program_invocation_short_name);
634 : 0 : exit (EXIT_FAILURE);
635 : : }
636 : : break;
637 : : case 'W': /* Ignored. */
638 : : break;
639 : 0 : case 'z':
640 : 0 : print_decompress = true;
641 : 0 : break;
642 : 0 : case ELF_INPUT_SECTION:
643 [ # # ]: 0 : if (arg == NULL)
644 : 0 : elf_input_section = ".gnu_debugdata";
645 : : else
646 : 0 : elf_input_section = arg;
647 : : break;
648 : 0 : case DWARF_SKELETON:
649 : 0 : dwarf_skeleton = arg;
650 : 0 : break;
651 : : default:
652 : : return ARGP_ERR_UNKNOWN;
653 : : }
654 : : return 0;
655 : : }
656 : :
657 : :
658 : : /* Create a file descriptor to read the data from the
659 : : elf_input_section given a file descriptor to an ELF file. */
660 : : static int
661 : 0 : open_input_section (int fd)
662 : : {
663 : 0 : size_t shnums;
664 : 0 : size_t cnt;
665 : 0 : size_t shstrndx;
666 : 0 : Elf *elf = elf_begin (fd, ELF_C_READ_MMAP, NULL);
667 [ # # ]: 0 : if (elf == NULL)
668 : : {
669 : 0 : error (0, 0, _("cannot generate Elf descriptor: %s"),
670 : : elf_errmsg (-1));
671 : 0 : return -1;
672 : : }
673 : :
674 [ # # ]: 0 : if (elf_getshdrnum (elf, &shnums) < 0)
675 : : {
676 : 0 : error (0, 0, _("cannot determine number of sections: %s"),
677 : : elf_errmsg (-1));
678 : 0 : open_error:
679 : 0 : elf_end (elf);
680 : 0 : return -1;
681 : : }
682 : :
683 [ # # ]: 0 : if (elf_getshdrstrndx (elf, &shstrndx) < 0)
684 : : {
685 : 0 : error (0, 0, _("cannot get section header string table index"));
686 : 0 : goto open_error;
687 : : }
688 : :
689 [ # # ]: 0 : for (cnt = 0; cnt < shnums; ++cnt)
690 : : {
691 : 0 : Elf_Scn *scn = elf_getscn (elf, cnt);
692 [ # # ]: 0 : if (scn == NULL)
693 : : {
694 : 0 : error (0, 0, _("cannot get section: %s"),
695 : : elf_errmsg (-1));
696 : 0 : goto open_error;
697 : : }
698 : :
699 : 0 : GElf_Shdr shdr_mem;
700 : 0 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
701 [ # # ]: 0 : if (unlikely (shdr == NULL))
702 : : {
703 : 0 : error (0, 0, _("cannot get section header: %s"),
704 : : elf_errmsg (-1));
705 : 0 : goto open_error;
706 : : }
707 : :
708 : 0 : const char *sname = elf_strptr (elf, shstrndx, shdr->sh_name);
709 [ # # ]: 0 : if (sname == NULL)
710 : : {
711 : 0 : error (0, 0, _("cannot get section name"));
712 : 0 : goto open_error;
713 : : }
714 : :
715 [ # # ]: 0 : if (strcmp (sname, elf_input_section) == 0)
716 : : {
717 : 0 : Elf_Data *data = elf_rawdata (scn, NULL);
718 [ # # ]: 0 : if (data == NULL)
719 : : {
720 : 0 : error (0, 0, _("cannot get %s content: %s"),
721 : : sname, elf_errmsg (-1));
722 : 0 : goto open_error;
723 : : }
724 : :
725 : : /* Create (and immediately unlink) a temporary file to store
726 : : section data in to create a file descriptor for it. */
727 [ # # ]: 0 : const char *tmpdir = getenv ("TMPDIR") ?: P_tmpdir;
728 : 0 : static const char suffix[] = "/readelfXXXXXX";
729 : 0 : int tmplen = strlen (tmpdir) + sizeof (suffix);
730 : 0 : char *tempname = alloca (tmplen);
731 : 0 : sprintf (tempname, "%s%s", tmpdir, suffix);
732 : :
733 : 0 : int sfd = mkstemp (tempname);
734 [ # # ]: 0 : if (sfd == -1)
735 : : {
736 : 0 : error (0, 0, _("cannot create temp file '%s'"),
737 : : tempname);
738 : 0 : goto open_error;
739 : : }
740 : 0 : unlink (tempname);
741 : :
742 : 0 : ssize_t size = data->d_size;
743 [ # # ]: 0 : if (write_retry (sfd, data->d_buf, size) != size)
744 : : {
745 : 0 : error (0, 0, _("cannot write section data"));
746 : 0 : goto open_error;
747 : : }
748 : :
749 [ # # ]: 0 : if (elf_end (elf) != 0)
750 : : {
751 : 0 : error (0, 0, _("error while closing Elf descriptor: %s"),
752 : : elf_errmsg (-1));
753 : 0 : return -1;
754 : : }
755 : :
756 [ # # ]: 0 : if (lseek (sfd, 0, SEEK_SET) == -1)
757 : : {
758 : 0 : error (0, 0, _("error while rewinding file descriptor"));
759 : 0 : return -1;
760 : : }
761 : :
762 : : return sfd;
763 : : }
764 : : }
765 : :
766 : : /* Named section not found. */
767 [ # # ]: 0 : if (elf_end (elf) != 0)
768 : 0 : error (0, 0, _("error while closing Elf descriptor: %s"),
769 : : elf_errmsg (-1));
770 : : return -1;
771 : : }
772 : :
773 : : /* Check if the file is an archive, and if so dump its index. */
774 : : static void
775 : 0 : check_archive_index (int fd, const char *fname, bool only_one)
776 : : {
777 : : /* Create an `Elf' descriptor. */
778 : 0 : Elf *elf = elf_begin (fd, ELF_C_READ_MMAP, NULL);
779 [ # # ]: 0 : if (elf == NULL)
780 : 0 : error (0, 0, _("cannot generate Elf descriptor: %s"),
781 : : elf_errmsg (-1));
782 : : else
783 : : {
784 [ # # ]: 0 : if (elf_kind (elf) == ELF_K_AR)
785 : : {
786 [ # # ]: 0 : if (!only_one)
787 : 0 : printf ("\n%s:\n\n", fname);
788 : 0 : dump_archive_index (elf, fname);
789 : : }
790 : : else
791 : 0 : error (0, 0,
792 : 0 : _("'%s' is not an archive, cannot print archive index"),
793 : : fname);
794 : :
795 : : /* Now we can close the descriptor. */
796 [ # # ]: 0 : if (elf_end (elf) != 0)
797 : 0 : error (0, 0, _("error while closing Elf descriptor: %s"),
798 : : elf_errmsg (-1));
799 : : }
800 : 0 : }
801 : :
802 : : /* Trivial callback used for checking if we opened an archive. */
803 : : static int
804 : 3 : count_dwflmod (Dwfl_Module *dwflmod __attribute__ ((unused)),
805 : : void **userdata __attribute__ ((unused)),
806 : : const char *name __attribute__ ((unused)),
807 : : Dwarf_Addr base __attribute__ ((unused)),
808 : : void *arg)
809 : : {
810 [ + - ]: 3 : if (*(bool *) arg)
811 : : return DWARF_CB_ABORT;
812 : 3 : *(bool *) arg = true;
813 : 3 : return DWARF_CB_OK;
814 : : }
815 : :
816 : : struct process_dwflmod_args
817 : : {
818 : : int fd;
819 : : bool only_one;
820 : : };
821 : :
822 : : static int
823 : 3 : process_dwflmod (Dwfl_Module *dwflmod,
824 : : void **userdata __attribute__ ((unused)),
825 : : const char *name __attribute__ ((unused)),
826 : : Dwarf_Addr base __attribute__ ((unused)),
827 : : void *arg)
828 : : {
829 : 3 : const struct process_dwflmod_args *a = arg;
830 : :
831 : : /* Print the file name. */
832 [ - + ]: 3 : if (!a->only_one)
833 : : {
834 : 0 : const char *fname;
835 : 0 : dwfl_module_info (dwflmod, NULL, NULL, NULL, NULL, NULL, &fname, NULL);
836 : :
837 : 0 : printf ("\n%s:\n\n", fname);
838 : : }
839 : :
840 : 3 : process_elf_file (dwflmod, a->fd);
841 : :
842 : 3 : return DWARF_CB_OK;
843 : : }
844 : :
845 : : /* Stub libdwfl callback, only the ELF handle already open is ever used.
846 : : Only used for finding the alternate debug file if the Dwarf comes from
847 : : the main file. We are not interested in separate debuginfo. */
848 : : static int
849 : 0 : find_no_debuginfo (Dwfl_Module *mod,
850 : : void **userdata,
851 : : const char *modname,
852 : : Dwarf_Addr base,
853 : : const char *file_name,
854 : : const char *debuglink_file,
855 : : GElf_Word debuglink_crc,
856 : : char **debuginfo_file_name)
857 : : {
858 : 0 : Dwarf_Addr dwbias;
859 : 0 : dwfl_module_info (mod, NULL, NULL, NULL, &dwbias, NULL, NULL, NULL);
860 : :
861 : : /* We are only interested if the Dwarf has been setup on the main
862 : : elf file but is only missing the alternate debug link. If dwbias
863 : : hasn't even been setup, this is searching for separate debuginfo
864 : : for the main elf. We don't care in that case. */
865 [ # # ]: 0 : if (dwbias == (Dwarf_Addr) -1)
866 : : return -1;
867 : :
868 : 0 : return dwfl_standard_find_debuginfo (mod, userdata, modname, base,
869 : : file_name, debuglink_file,
870 : : debuglink_crc, debuginfo_file_name);
871 : : }
872 : :
873 : : static Dwfl *
874 : 3 : create_dwfl (int fd, const char *fname)
875 : : {
876 : : /* Duplicate an fd for dwfl_report_offline to swallow. */
877 : 3 : int dwfl_fd = dup (fd);
878 [ - + ]: 3 : if (unlikely (dwfl_fd < 0))
879 : 0 : error_exit (errno, "dup");
880 : :
881 : : /* Use libdwfl in a trivial way to open the libdw handle for us.
882 : : This takes care of applying relocations to DWARF data in ET_REL files. */
883 : 3 : static const Dwfl_Callbacks callbacks =
884 : : {
885 : : .section_address = dwfl_offline_section_address,
886 : : .find_debuginfo = find_no_debuginfo
887 : : };
888 : 3 : Dwfl *dwfl = dwfl_begin (&callbacks);
889 [ + - ]: 3 : if (likely (dwfl != NULL))
890 : : /* Let 0 be the logical address of the file (or first in archive). */
891 : 3 : dwfl->offline_next_address = 0;
892 [ - + ]: 3 : if (dwfl_report_offline (dwfl, fname, fname, dwfl_fd) == NULL)
893 : : {
894 : 0 : struct stat st;
895 [ # # ]: 0 : if (fstat (dwfl_fd, &st) != 0)
896 : 0 : error (0, errno, _("cannot stat input file"));
897 [ # # ]: 0 : else if (unlikely (st.st_size == 0))
898 : 0 : error (0, 0, _("input file is empty"));
899 : : else
900 : 0 : error (0, 0, _("failed reading '%s': %s"),
901 : : fname, dwfl_errmsg (-1));
902 : 0 : close (dwfl_fd); /* Consumed on success, not on failure. */
903 : 0 : dwfl = NULL;
904 : : }
905 : : else
906 : 3 : dwfl_report_end (dwfl, NULL, NULL);
907 : :
908 : 3 : return dwfl;
909 : : }
910 : :
911 : : /* Process one input file. */
912 : : static void
913 : 3 : process_file (int fd, const char *fname, bool only_one)
914 : : {
915 [ - + ]: 3 : if (print_archive_index)
916 : 0 : check_archive_index (fd, fname, only_one);
917 : :
918 [ + - ]: 3 : if (!any_control_option)
919 : : return;
920 : :
921 [ - + ]: 3 : if (elf_input_section != NULL)
922 : : {
923 : : /* Replace fname and fd with section content. */
924 : 0 : char *fnname = alloca (strlen (fname) + strlen (elf_input_section) + 2);
925 : 0 : sprintf (fnname, "%s:%s", fname, elf_input_section);
926 : 0 : fd = open_input_section (fd);
927 [ # # ]: 0 : if (fd == -1)
928 : : {
929 : 0 : error (0, 0, _("No such section '%s' in '%s'"),
930 : : elf_input_section, fname);
931 : 0 : return;
932 : : }
933 : : fname = fnname;
934 : : }
935 : :
936 : 3 : Dwfl *dwfl = create_dwfl (fd, fname);
937 [ + - ]: 3 : if (dwfl != NULL)
938 : : {
939 [ + - ]: 3 : if (only_one)
940 : : {
941 : : /* Clear ONLY_ONE if we have multiple modules, from an archive. */
942 : 3 : bool seen = false;
943 : 3 : only_one = dwfl_getmodules (dwfl, &count_dwflmod, &seen, 0) == 0;
944 : : }
945 : :
946 : : /* Process the one or more modules gleaned from this file. */
947 : 3 : struct process_dwflmod_args a = { .fd = fd, .only_one = only_one };
948 : 3 : dwfl_getmodules (dwfl, &process_dwflmod, &a, 0);
949 : : }
950 : : /* Terrible hack for hooking unrelated skeleton/split compile units,
951 : : see __libdw_link_skel_split in print_debug. */
952 [ + - ]: 3 : if (! do_not_close_dwfl)
953 : 3 : dwfl_end (dwfl);
954 : :
955 : : /* Need to close the replaced fd if we created it. Caller takes
956 : : care of original. */
957 [ - + ]: 3 : if (elf_input_section != NULL)
958 : 0 : close (fd);
959 : : }
960 : :
961 : : /* Check whether there are any compressed sections in the ELF file. */
962 : : static bool
963 : 1 : elf_contains_chdrs (Elf *elf)
964 : : {
965 : 1 : Elf_Scn *scn = NULL;
966 [ + + ]: 31 : while ((scn = elf_nextscn (elf, scn)) != NULL)
967 : : {
968 : 30 : GElf_Shdr shdr_mem;
969 : 30 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
970 [ + - - + ]: 30 : if (shdr != NULL && (shdr->sh_flags & SHF_COMPRESSED) != 0)
971 : 0 : return true;
972 : : }
973 : : return false;
974 : : }
975 : :
976 : : /* Process one ELF file. */
977 : : static void
978 : 3 : process_elf_file (Dwfl_Module *dwflmod, int fd)
979 : : {
980 : 3 : GElf_Addr dwflbias;
981 : 3 : Elf *elf = dwfl_module_getelf (dwflmod, &dwflbias);
982 : :
983 : 3 : GElf_Ehdr ehdr_mem;
984 : 3 : GElf_Ehdr *ehdr = gelf_getehdr (elf, &ehdr_mem);
985 : :
986 [ - + ]: 3 : if (ehdr == NULL)
987 : : {
988 : 0 : error (0, 0, _("cannot read ELF header: %s"), elf_errmsg (-1));
989 : 0 : return;
990 : : }
991 : :
992 : 3 : Ebl *ebl = ebl_openbackend (elf);
993 [ - + ]: 3 : if (unlikely (ebl == NULL))
994 : : {
995 : 0 : ebl_error:
996 : 0 : error (0, errno, _("cannot create EBL handle"));
997 : 0 : return;
998 : : }
999 : :
1000 : : /* Determine the number of sections. */
1001 [ - + ]: 3 : if (unlikely (elf_getshdrnum (ebl->elf, &shnum) < 0))
1002 : 0 : error_exit (0, _("cannot determine number of sections: %s"),
1003 : : elf_errmsg (-1));
1004 : :
1005 : : /* Determine the number of phdrs. */
1006 [ - + ]: 3 : if (unlikely (elf_getphdrnum (ebl->elf, &phnum) < 0))
1007 : 0 : error_exit (0, _("cannot determine number of program headers: %s"),
1008 : : elf_errmsg (-1));
1009 : :
1010 : : /* For an ET_REL file, libdwfl has adjusted the in-core shdrs and
1011 : : may have applied relocation to some sections. If there are any
1012 : : compressed sections, any pass (or libdw/libdwfl) might have
1013 : : uncompressed them. So we need to get a fresh Elf handle on the
1014 : : file to display those. */
1015 : 7 : bool print_unchanged = ((print_section_header
1016 [ + - ]: 1 : || print_relocations
1017 [ + - ]: 1 : || dump_data_sections != NULL
1018 [ - + ]: 1 : || print_notes)
1019 [ + + + + ]: 3 : && (ehdr->e_type == ET_REL
1020 [ - + ]: 1 : || elf_contains_chdrs (ebl->elf)));
1021 : :
1022 : 1 : Elf *pure_elf = NULL;
1023 : 1 : Ebl *pure_ebl = ebl;
1024 : 1 : if (print_unchanged)
1025 : : {
1026 : : /* Read the file afresh. */
1027 : 1 : off_t aroff = elf_getaroff (elf);
1028 : 1 : pure_elf = dwelf_elf_begin (fd);
1029 [ - + ]: 1 : if (aroff > 0)
1030 : : {
1031 : : /* Archive member. */
1032 : 0 : (void) elf_rand (pure_elf, aroff);
1033 : 0 : Elf *armem = elf_begin (-1, ELF_C_READ_MMAP, pure_elf);
1034 : 0 : elf_end (pure_elf);
1035 : 0 : pure_elf = armem;
1036 : : }
1037 [ - + ]: 1 : if (pure_elf == NULL)
1038 : : {
1039 : 0 : error (0, 0, _("cannot read ELF: %s"), elf_errmsg (-1));
1040 : 0 : return;
1041 : : }
1042 : 1 : pure_ebl = ebl_openbackend (pure_elf);
1043 [ - + ]: 1 : if (pure_ebl == NULL)
1044 : 0 : goto ebl_error;
1045 : : }
1046 : :
1047 : 3 : bool symtab_printed = false;
1048 : :
1049 [ + + ]: 3 : if (print_file_header)
1050 : 1 : print_ehdr (ebl, ehdr);
1051 [ + + ]: 3 : if (print_section_header)
1052 : 2 : print_shdr (pure_ebl, ehdr);
1053 [ - + ]: 3 : if (print_program_header)
1054 : 0 : print_phdr (ebl, ehdr);
1055 [ - + ]: 3 : if (print_section_groups)
1056 : 0 : print_scngrp (ebl);
1057 [ - + ]: 3 : if (print_dynamic_table)
1058 : 0 : print_dynamic (ebl);
1059 [ - + ]: 3 : if (print_relocations)
1060 : 0 : print_relocs (pure_ebl, dwflmod, ehdr);
1061 [ - + ]: 3 : if (print_histogram)
1062 : 0 : handle_hash (ebl);
1063 [ + - - + ]: 3 : if (print_symbol_table || print_dynsym_table)
1064 : 0 : symtab_printed |= print_symtab (ebl, SHT_DYNSYM);
1065 [ - + ]: 3 : if (print_version_info)
1066 : 0 : print_verinfo (ebl);
1067 [ - + - - ]: 3 : if (print_symbol_table && !use_dynamic_segment)
1068 : 0 : symtab_printed |= print_symtab (ebl, SHT_SYMTAB);
1069 : :
1070 [ + - - + ]: 3 : if ((print_symbol_table || print_dynsym_table)
1071 [ # # # # ]: 0 : && !symtab_printed && symbol_table_section != NULL)
1072 : 0 : printf ("WARNING: %s: '%s'\n", _("cannot find section"),
1073 : : symbol_table_section);
1074 : :
1075 [ - + ]: 3 : if (print_arch)
1076 : 0 : print_liblist (ebl);
1077 [ - + ]: 3 : if (print_arch)
1078 : 0 : print_attributes (ebl, ehdr);
1079 [ - + ]: 3 : if (dump_data_sections != NULL)
1080 : 0 : dump_data (pure_ebl);
1081 [ - + ]: 3 : if (string_sections != NULL)
1082 : 0 : dump_strings (ebl);
1083 [ - + ]: 3 : if ((print_debug_sections | implicit_debug_sections) != 0)
1084 : 0 : print_debug (dwflmod, ebl, ehdr);
1085 [ - + ]: 3 : if (print_notes)
1086 : 0 : handle_notes (pure_ebl, ehdr);
1087 [ - + ]: 3 : if (print_string_sections)
1088 : 0 : print_strings (ebl);
1089 : :
1090 [ + + ]: 3 : if (pure_ebl != ebl)
1091 : : {
1092 : 1 : ebl_closebackend (ebl);
1093 : 1 : ebl_closebackend (pure_ebl);
1094 : 1 : elf_end (pure_elf);
1095 : : }
1096 : : else
1097 : 2 : ebl_closebackend (ebl);
1098 : : }
1099 : :
1100 : :
1101 : : /* Print file type. */
1102 : : static void
1103 : 1 : print_file_type (unsigned short int e_type)
1104 : : {
1105 [ + - ]: 1 : if (likely (e_type <= ET_CORE))
1106 : : {
1107 : 1 : static const char *const knowntypes[] =
1108 : : {
1109 : : N_("NONE (None)"),
1110 : : N_("REL (Relocatable file)"),
1111 : : N_("EXEC (Executable file)"),
1112 : : N_("DYN (Shared object file)"),
1113 : : N_("CORE (Core file)")
1114 : : };
1115 : 1 : puts (_(knowntypes[e_type]));
1116 : : }
1117 [ # # ]: 0 : else if (e_type >= ET_LOOS && e_type <= ET_HIOS)
1118 : 0 : printf (_("OS Specific: (%x)\n"), e_type);
1119 [ # # ]: 0 : else if (e_type >= ET_LOPROC /* && e_type <= ET_HIPROC always true */)
1120 : 0 : printf (_("Processor Specific: (%x)\n"), e_type);
1121 : : else
1122 : 0 : puts ("???");
1123 : 1 : }
1124 : :
1125 : :
1126 : : /* Print ELF header. */
1127 : : static void
1128 : 1 : print_ehdr (Ebl *ebl, GElf_Ehdr *ehdr)
1129 : : {
1130 : 1 : fputs (_("ELF Header:\n Magic: "), stdout);
1131 [ + + ]: 17 : for (size_t cnt = 0; cnt < EI_NIDENT; ++cnt)
1132 : 16 : printf (" %02hhx", ehdr->e_ident[cnt]);
1133 : :
1134 : 1 : printf (_("\n Class: %s\n"),
1135 [ + - ]: 1 : ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? "ELF32"
1136 : : : ehdr->e_ident[EI_CLASS] == ELFCLASS64 ? "ELF64"
1137 [ - + ]: 1 : : "\?\?\?");
1138 : :
1139 : 1 : printf (_(" Data: %s\n"),
1140 [ - + ]: 1 : ehdr->e_ident[EI_DATA] == ELFDATA2LSB
1141 : : ? "2's complement, little endian"
1142 : : : ehdr->e_ident[EI_DATA] == ELFDATA2MSB
1143 [ # # ]: 0 : ? "2's complement, big endian" : "\?\?\?");
1144 : :
1145 : 3 : printf (_(" Ident Version: %hhd %s\n"),
1146 : 1 : ehdr->e_ident[EI_VERSION],
1147 [ + - ]: 1 : ehdr->e_ident[EI_VERSION] == EV_CURRENT ? _("(current)")
1148 : : : "(\?\?\?)");
1149 : :
1150 : 1 : char buf[512];
1151 : 1 : printf (_(" OS/ABI: %s\n"),
1152 : 1 : ebl_osabi_name (ebl, ehdr->e_ident[EI_OSABI], buf, sizeof (buf)));
1153 : :
1154 : 2 : printf (_(" ABI Version: %hhd\n"),
1155 : 1 : ehdr->e_ident[EI_ABIVERSION]);
1156 : :
1157 : 1 : fputs (_(" Type: "), stdout);
1158 : 1 : print_file_type (ehdr->e_type);
1159 : :
1160 : 1 : const char *machine = dwelf_elf_e_machine_string (ehdr->e_machine);
1161 [ + - ]: 1 : if (machine != NULL)
1162 : 1 : printf (_(" Machine: %s\n"), machine);
1163 : : else
1164 : 1 : printf (_(" Machine: <unknown>: 0x%x\n"),
1165 : 0 : ehdr->e_machine);
1166 : :
1167 : 1 : printf (_(" Version: %d %s\n"),
1168 : : ehdr->e_version,
1169 [ + - ]: 1 : ehdr->e_version == EV_CURRENT ? _("(current)") : "(\?\?\?)");
1170 : :
1171 : 1 : printf (_(" Entry point address: %#" PRIx64 "\n"),
1172 : : ehdr->e_entry);
1173 : :
1174 : 1 : printf (_(" Start of program headers: %" PRId64 " %s\n"),
1175 : : ehdr->e_phoff, _("(bytes into file)"));
1176 : :
1177 : 1 : printf (_(" Start of section headers: %" PRId64 " %s\n"),
1178 : : ehdr->e_shoff, _("(bytes into file)"));
1179 : :
1180 : 1 : printf (_(" Flags: %s\n"),
1181 : : ebl_machine_flag_name (ebl, ehdr->e_flags, buf, sizeof (buf)));
1182 : :
1183 : 2 : printf (_(" Size of this header: %" PRId16 " %s\n"),
1184 : 1 : ehdr->e_ehsize, _("(bytes)"));
1185 : :
1186 : 2 : printf (_(" Size of program header entries: %" PRId16 " %s\n"),
1187 : 1 : ehdr->e_phentsize, _("(bytes)"));
1188 : :
1189 : 2 : printf (_(" Number of program headers entries: %" PRId16),
1190 : 1 : ehdr->e_phnum);
1191 [ - + ]: 1 : if (ehdr->e_phnum == PN_XNUM)
1192 : : {
1193 : 0 : GElf_Shdr shdr_mem;
1194 : 0 : GElf_Shdr *shdr = gelf_getshdr (elf_getscn (ebl->elf, 0), &shdr_mem);
1195 [ # # ]: 0 : if (shdr != NULL)
1196 : 0 : printf (_(" (%" PRIu32 " in [0].sh_info)"),
1197 : 0 : (uint32_t) shdr->sh_info);
1198 : : else
1199 : 0 : fputs (_(" ([0] not available)"), stdout);
1200 : : }
1201 : 1 : fputc ('\n', stdout);
1202 : :
1203 : 2 : printf (_(" Size of section header entries: %" PRId16 " %s\n"),
1204 : 1 : ehdr->e_shentsize, _("(bytes)"));
1205 : :
1206 : 2 : printf (_(" Number of section headers entries: %" PRId16),
1207 : 1 : ehdr->e_shnum);
1208 [ - + ]: 1 : if (ehdr->e_shnum == 0)
1209 : : {
1210 : 0 : GElf_Shdr shdr_mem;
1211 : 0 : GElf_Shdr *shdr = gelf_getshdr (elf_getscn (ebl->elf, 0), &shdr_mem);
1212 [ # # ]: 0 : if (shdr != NULL)
1213 : 0 : printf (_(" (%" PRIu32 " in [0].sh_size)"),
1214 : 0 : (uint32_t) shdr->sh_size);
1215 : : else
1216 : 0 : fputs (_(" ([0] not available)"), stdout);
1217 : : }
1218 : 1 : fputc ('\n', stdout);
1219 : :
1220 [ - + ]: 1 : if (unlikely (ehdr->e_shstrndx == SHN_XINDEX))
1221 : : {
1222 : 0 : GElf_Shdr shdr_mem;
1223 : 0 : GElf_Shdr *shdr = gelf_getshdr (elf_getscn (ebl->elf, 0), &shdr_mem);
1224 [ # # ]: 0 : if (shdr != NULL)
1225 : : /* We managed to get the zeroth section. */
1226 : 0 : snprintf (buf, sizeof (buf), _(" (%" PRIu32 " in [0].sh_link)"),
1227 : 0 : (uint32_t) shdr->sh_link);
1228 : : else
1229 : : {
1230 : 0 : strncpy (buf, _(" ([0] not available)"), sizeof (buf) - 1);
1231 : 0 : buf[sizeof (buf) - 1] = '\0';
1232 : : }
1233 : :
1234 : 0 : printf (_(" Section header string table index: XINDEX%s\n\n"),
1235 : : buf);
1236 : : }
1237 : : else
1238 : 1 : printf (_(" Section header string table index: %" PRId16 "\n\n"),
1239 : : ehdr->e_shstrndx);
1240 : 1 : }
1241 : :
1242 : :
1243 : : static const char *
1244 : 0 : get_visibility_type (int value)
1245 : : {
1246 [ # # # # : 0 : switch (value)
# ]
1247 : : {
1248 : : case STV_DEFAULT:
1249 : : return "DEFAULT";
1250 : 0 : case STV_INTERNAL:
1251 : 0 : return "INTERNAL";
1252 : 0 : case STV_HIDDEN:
1253 : 0 : return "HIDDEN";
1254 : 0 : case STV_PROTECTED:
1255 : 0 : return "PROTECTED";
1256 : 0 : default:
1257 : 0 : return "???";
1258 : : }
1259 : : }
1260 : :
1261 : : static const char *
1262 : 0 : elf_ch_type_name (unsigned int code)
1263 : : {
1264 : 0 : switch (code)
1265 : : {
1266 : : case 0:
1267 : : return "NONE";
1268 : 0 : case ELFCOMPRESS_ZLIB:
1269 : 0 : return "ZLIB";
1270 : 0 : case ELFCOMPRESS_ZSTD:
1271 : 0 : return "ZSTD";
1272 : 0 : default:
1273 : 0 : return "UNKNOWN";
1274 : : }
1275 : : }
1276 : :
1277 : : /* Print the section headers. */
1278 : : static void
1279 : 2 : print_shdr (Ebl *ebl, GElf_Ehdr *ehdr)
1280 : : {
1281 : 2 : size_t cnt;
1282 : 2 : size_t shstrndx;
1283 : :
1284 [ + - ]: 2 : if (! print_file_header)
1285 : : {
1286 : 2 : size_t sections;
1287 [ - + ]: 2 : if (unlikely (elf_getshdrnum (ebl->elf, §ions) < 0))
1288 : 0 : error_exit (0, _("cannot get number of sections: %s"),
1289 : : elf_errmsg (-1));
1290 : :
1291 : 2 : printf (_("\
1292 : : There are %zd section headers, starting at offset %#" PRIx64 ":\n\
1293 : : \n"),
1294 : : sections, ehdr->e_shoff);
1295 : : }
1296 : :
1297 : : /* Get the section header string table index. */
1298 [ - + ]: 2 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
1299 : 0 : error_exit (0, _("cannot get section header string table index: %s"),
1300 : : elf_errmsg (-1));
1301 : :
1302 : 2 : puts (_("Section Headers:"));
1303 : :
1304 [ - + ]: 2 : if (ehdr->e_ident[EI_CLASS] == ELFCLASS32)
1305 : 0 : puts (_("[Nr] Name Type Addr Off Size ES Flags Lk Inf Al"));
1306 : : else
1307 : 2 : puts (_("[Nr] Name Type Addr Off Size ES Flags Lk Inf Al"));
1308 : :
1309 [ - + ]: 2 : if (print_decompress)
1310 : : {
1311 [ # # ]: 0 : if (ehdr->e_ident[EI_CLASS] == ELFCLASS32)
1312 : 0 : puts (_(" [Compression Size Al]"));
1313 : : else
1314 : 0 : puts (_(" [Compression Size Al]"));
1315 : : }
1316 : :
1317 [ + + ]: 44 : for (cnt = 0; cnt < shnum; ++cnt)
1318 : : {
1319 : 42 : Elf_Scn *scn = elf_getscn (ebl->elf, cnt);
1320 : :
1321 [ - + ]: 42 : if (unlikely (scn == NULL))
1322 : 0 : error_exit (0, _("cannot get section: %s"),
1323 : : elf_errmsg (-1));
1324 : :
1325 : : /* Get the section header. */
1326 : 42 : GElf_Shdr shdr_mem;
1327 : 42 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
1328 [ - + ]: 42 : if (unlikely (shdr == NULL))
1329 : 0 : error_exit (0, _("cannot get section header: %s"),
1330 : : elf_errmsg (-1));
1331 : :
1332 : 42 : char flagbuf[20];
1333 : 42 : char *cp = flagbuf;
1334 [ + + ]: 42 : if (shdr->sh_flags & SHF_WRITE)
1335 : 9 : *cp++ = 'W';
1336 [ + + ]: 42 : if (shdr->sh_flags & SHF_ALLOC)
1337 : 26 : *cp++ = 'A';
1338 [ + + ]: 42 : if (shdr->sh_flags & SHF_EXECINSTR)
1339 : 5 : *cp++ = 'X';
1340 [ - + ]: 42 : if (shdr->sh_flags & SHF_MERGE)
1341 : 0 : *cp++ = 'M';
1342 [ - + ]: 42 : if (shdr->sh_flags & SHF_STRINGS)
1343 : 0 : *cp++ = 'S';
1344 [ - + ]: 42 : if (shdr->sh_flags & SHF_INFO_LINK)
1345 : 0 : *cp++ = 'I';
1346 [ - + ]: 42 : if (shdr->sh_flags & SHF_LINK_ORDER)
1347 : 0 : *cp++ = 'L';
1348 [ - + ]: 42 : if (shdr->sh_flags & SHF_OS_NONCONFORMING)
1349 : 0 : *cp++ = 'N';
1350 [ - + ]: 42 : if (shdr->sh_flags & SHF_GROUP)
1351 : 0 : *cp++ = 'G';
1352 [ - + ]: 42 : if (shdr->sh_flags & SHF_TLS)
1353 : 0 : *cp++ = 'T';
1354 [ - + ]: 42 : if (shdr->sh_flags & SHF_COMPRESSED)
1355 : 0 : *cp++ = 'C';
1356 [ - + ]: 42 : if (shdr->sh_flags & SHF_ORDERED)
1357 : 0 : *cp++ = 'O';
1358 [ - + ]: 42 : if (shdr->sh_flags & SHF_EXCLUDE)
1359 : 0 : *cp++ = 'E';
1360 [ - + ]: 42 : if (shdr->sh_flags & SHF_GNU_RETAIN)
1361 : 0 : *cp++ = 'R';
1362 : 42 : *cp = '\0';
1363 : :
1364 : 42 : const char *sname;
1365 : 42 : char buf[128];
1366 [ - + ]: 42 : sname = elf_strptr (ebl->elf, shstrndx, shdr->sh_name) ?: "<corrupt>";
1367 [ + - ]: 84 : printf ("[%2zu] %-20s %-12s %0*" PRIx64 " %0*" PRIx64 " %0*" PRIx64
1368 : : " %2" PRId64 " %-5s %2" PRId32 " %3" PRId32
1369 : : " %2" PRId64 "\n",
1370 : : cnt, sname,
1371 : 42 : ebl_section_type_name (ebl, shdr->sh_type, buf, sizeof (buf)),
1372 : : ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 8 : 16, shdr->sh_addr,
1373 : : ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 6 : 8, shdr->sh_offset,
1374 [ + - ]: 42 : ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 6 : 8, shdr->sh_size,
1375 : : shdr->sh_entsize, flagbuf, shdr->sh_link, shdr->sh_info,
1376 : : shdr->sh_addralign);
1377 : :
1378 [ - + ]: 42 : if (print_decompress)
1379 : : {
1380 [ # # ]: 0 : if ((shdr->sh_flags & SHF_COMPRESSED) != 0)
1381 : : {
1382 : 0 : GElf_Chdr chdr;
1383 [ # # ]: 0 : if (gelf_getchdr (scn, &chdr) != NULL)
1384 [ # # # # ]: 0 : printf (" [ELF %s (%" PRId32 ") %0*" PRIx64
1385 : : " %2" PRId64 "]\n",
1386 : : elf_ch_type_name (chdr.ch_type),
1387 : : chdr.ch_type,
1388 [ # # ]: 0 : ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 6 : 8,
1389 : : chdr.ch_size, chdr.ch_addralign);
1390 : : else
1391 : 0 : error (0, 0,
1392 : 0 : _("bad compression header for section %zd: %s"),
1393 : : elf_ndxscn (scn), elf_errmsg (-1));
1394 : : }
1395 [ # # ]: 0 : else if (startswith (sname, ".zdebug"))
1396 : : {
1397 : 0 : ssize_t size;
1398 [ # # ]: 0 : if ((size = dwelf_scn_gnu_compressed_size (scn)) >= 0)
1399 : 42 : printf (" [GNU ZLIB %0*zx ]\n",
1400 [ # # ]: 0 : ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 6 : 8, size);
1401 : : else
1402 : 42 : error (0, 0,
1403 : 0 : _("bad gnu compressed size for section %zd: %s"),
1404 : : elf_ndxscn (scn), elf_errmsg (-1));
1405 : : }
1406 : : }
1407 : : }
1408 : :
1409 : 2 : fputc ('\n', stdout);
1410 : 2 : }
1411 : :
1412 : :
1413 : : /* Print the program header. */
1414 : : static void
1415 : 0 : print_phdr (Ebl *ebl, GElf_Ehdr *ehdr)
1416 : : {
1417 [ # # ]: 0 : if (phnum == 0)
1418 : : /* No program header, this is OK in relocatable objects. */
1419 : 0 : return;
1420 : :
1421 : 0 : puts (_("Program Headers:"));
1422 [ # # ]: 0 : if (ehdr->e_ident[EI_CLASS] == ELFCLASS32)
1423 : 0 : puts (_("\
1424 : : Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align"));
1425 : : else
1426 : 0 : puts (_("\
1427 : : Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align"));
1428 : :
1429 : : /* Process all program headers. */
1430 : : bool has_relro = false;
1431 : : GElf_Addr relro_from = 0;
1432 : : GElf_Addr relro_to = 0;
1433 [ # # ]: 0 : for (size_t cnt = 0; cnt < phnum; ++cnt)
1434 : : {
1435 : 0 : char buf[128];
1436 : 0 : GElf_Phdr mem;
1437 : 0 : GElf_Phdr *phdr = gelf_getphdr (ebl->elf, cnt, &mem);
1438 : :
1439 : : /* If for some reason the header cannot be returned show this. */
1440 [ # # ]: 0 : if (unlikely (phdr == NULL))
1441 : : {
1442 : 0 : puts (" ???");
1443 : 0 : continue;
1444 : : }
1445 : :
1446 : 0 : printf (" %-14s 0x%06" PRIx64 " 0x%0*" PRIx64 " 0x%0*" PRIx64
1447 : : " 0x%06" PRIx64 " 0x%06" PRIx64 " %c%c%c 0x%" PRIx64 "\n",
1448 : 0 : ebl_segment_type_name (ebl, phdr->p_type, buf, sizeof (buf)),
1449 : : phdr->p_offset,
1450 : : ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 8 : 16, phdr->p_vaddr,
1451 [ # # ]: 0 : ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 8 : 16, phdr->p_paddr,
1452 : : phdr->p_filesz,
1453 : : phdr->p_memsz,
1454 [ # # ]: 0 : phdr->p_flags & PF_R ? 'R' : ' ',
1455 [ # # ]: 0 : phdr->p_flags & PF_W ? 'W' : ' ',
1456 [ # # ]: 0 : phdr->p_flags & PF_X ? 'E' : ' ',
1457 : : phdr->p_align);
1458 : :
1459 [ # # ]: 0 : if (phdr->p_type == PT_INTERP)
1460 : : {
1461 : : /* If we are sure the file offset is valid then we can show
1462 : : the user the name of the interpreter. We check whether
1463 : : there is a section at the file offset. Normally there
1464 : : would be a section called ".interp". But in separate
1465 : : .debug files it is a NOBITS section (and so doesn't match
1466 : : with gelf_offscn). Which probably means the offset is
1467 : : not valid another reason could be because the ELF file
1468 : : just doesn't contain any section headers, in that case
1469 : : just play it safe and don't display anything. */
1470 : :
1471 : 0 : Elf_Scn *scn = gelf_offscn (ebl->elf, phdr->p_offset);
1472 : 0 : GElf_Shdr shdr_mem;
1473 : 0 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
1474 : :
1475 : 0 : size_t maxsize;
1476 : 0 : char *filedata = elf_rawfile (ebl->elf, &maxsize);
1477 : :
1478 [ # # # # ]: 0 : if (shdr != NULL && shdr->sh_type == SHT_PROGBITS
1479 [ # # # # ]: 0 : && filedata != NULL && phdr->p_offset < maxsize
1480 [ # # ]: 0 : && phdr->p_filesz <= maxsize - phdr->p_offset
1481 [ # # ]: 0 : && memchr (filedata + phdr->p_offset, '\0',
1482 : : phdr->p_filesz) != NULL)
1483 : 0 : printf (_("\t[Requesting program interpreter: %s]\n"),
1484 : : filedata + phdr->p_offset);
1485 : : }
1486 [ # # ]: 0 : else if (phdr->p_type == PT_GNU_RELRO)
1487 : : {
1488 : 0 : has_relro = true;
1489 : 0 : relro_from = phdr->p_vaddr;
1490 : 0 : relro_to = relro_from + phdr->p_memsz;
1491 : : }
1492 : : }
1493 : :
1494 : 0 : size_t sections;
1495 [ # # ]: 0 : if (unlikely (elf_getshdrnum (ebl->elf, §ions) < 0))
1496 : 0 : error_exit (0, _("cannot get number of sections: %s"),
1497 : : elf_errmsg (-1));
1498 : :
1499 [ # # ]: 0 : if (sections == 0)
1500 : : /* No sections in the file. Punt. */
1501 : : return;
1502 : :
1503 : : /* Get the section header string table index. */
1504 : 0 : size_t shstrndx;
1505 [ # # ]: 0 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
1506 : 0 : error_exit (0, _("cannot get section header string table index"));
1507 : :
1508 : 0 : puts (_("\n Section to Segment mapping:\n Segment Sections..."));
1509 : :
1510 [ # # ]: 0 : for (size_t cnt = 0; cnt < phnum; ++cnt)
1511 : : {
1512 : : /* Print the segment number. */
1513 : 0 : printf (" %2.2zu ", cnt);
1514 : :
1515 : 0 : GElf_Phdr phdr_mem;
1516 : 0 : GElf_Phdr *phdr = gelf_getphdr (ebl->elf, cnt, &phdr_mem);
1517 : : /* This must not happen. */
1518 [ # # ]: 0 : if (unlikely (phdr == NULL))
1519 : 0 : error_exit (0, _("cannot get program header: %s"),
1520 : : elf_errmsg (-1));
1521 : :
1522 : : /* Iterate over the sections. */
1523 : : bool in_relro = false;
1524 : : bool in_ro = false;
1525 [ # # ]: 0 : for (size_t inner = 1; inner < shnum; ++inner)
1526 : : {
1527 : 0 : Elf_Scn *scn = elf_getscn (ebl->elf, inner);
1528 : : /* This should not happen. */
1529 [ # # ]: 0 : if (unlikely (scn == NULL))
1530 : 0 : error_exit (0, _("cannot get section: %s"),
1531 : : elf_errmsg (-1));
1532 : :
1533 : : /* Get the section header. */
1534 : 0 : GElf_Shdr shdr_mem;
1535 : 0 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
1536 [ # # ]: 0 : if (unlikely (shdr == NULL))
1537 : 0 : error_exit (0, _("cannot get section header: %s"),
1538 : : elf_errmsg (-1));
1539 : :
1540 [ # # ]: 0 : if (shdr->sh_size > 0
1541 : : /* Compare allocated sections by VMA, unallocated
1542 : : sections by file offset. */
1543 [ # # ]: 0 : && (shdr->sh_flags & SHF_ALLOC
1544 : 0 : ? (shdr->sh_addr >= phdr->p_vaddr
1545 [ # # ]: 0 : && (shdr->sh_addr + shdr->sh_size
1546 [ # # ]: 0 : <= phdr->p_vaddr + phdr->p_memsz))
1547 : 0 : : (shdr->sh_offset >= phdr->p_offset
1548 [ # # ]: 0 : && (shdr->sh_offset + shdr->sh_size
1549 [ # # ]: 0 : <= phdr->p_offset + phdr->p_filesz))))
1550 : : {
1551 [ # # ]: 0 : if (has_relro && !in_relro
1552 [ # # ]: 0 : && shdr->sh_addr >= relro_from
1553 [ # # ]: 0 : && shdr->sh_addr + shdr->sh_size <= relro_to)
1554 : : {
1555 : 0 : fputs (" [RELRO:", stdout);
1556 : 0 : in_relro = true;
1557 : : }
1558 [ # # # # ]: 0 : else if (has_relro && in_relro && shdr->sh_addr >= relro_to)
1559 : : {
1560 : 0 : fputs ("]", stdout);
1561 : 0 : in_relro = false;
1562 : : }
1563 [ # # ]: 0 : else if (has_relro && in_relro
1564 [ # # ]: 0 : && shdr->sh_addr + shdr->sh_size > relro_to)
1565 : 0 : fputs ("] <RELRO:", stdout);
1566 [ # # ]: 0 : else if (phdr->p_type == PT_LOAD && (phdr->p_flags & PF_W) == 0)
1567 : : {
1568 [ # # ]: 0 : if (!in_ro)
1569 : : {
1570 : 0 : fputs (" [RO:", stdout);
1571 : 0 : in_ro = true;
1572 : : }
1573 : : }
1574 : : else
1575 : : {
1576 : : /* Determine the segment this section is part of. */
1577 : : size_t cnt2;
1578 : : GElf_Phdr phdr2_mem;
1579 : : GElf_Phdr *phdr2 = NULL;
1580 [ # # ]: 0 : for (cnt2 = 0; cnt2 < phnum; ++cnt2)
1581 : : {
1582 : 0 : phdr2 = gelf_getphdr (ebl->elf, cnt2, &phdr2_mem);
1583 : :
1584 [ # # # # ]: 0 : if (phdr2 != NULL && phdr2->p_type == PT_LOAD
1585 [ # # ]: 0 : && shdr->sh_addr >= phdr2->p_vaddr
1586 : 0 : && (shdr->sh_addr + shdr->sh_size
1587 [ # # ]: 0 : <= phdr2->p_vaddr + phdr2->p_memsz))
1588 : : break;
1589 : : }
1590 : :
1591 [ # # ]: 0 : if (cnt2 < phnum)
1592 : : {
1593 [ # # # # ]: 0 : if ((phdr2->p_flags & PF_W) == 0 && !in_ro)
1594 : : {
1595 : 0 : fputs (" [RO:", stdout);
1596 : 0 : in_ro = true;
1597 : : }
1598 [ # # # # ]: 0 : else if ((phdr2->p_flags & PF_W) != 0 && in_ro)
1599 : : {
1600 : 0 : fputs ("]", stdout);
1601 : 0 : in_ro = false;
1602 : : }
1603 : : }
1604 : : }
1605 : :
1606 : 0 : printf (" %s",
1607 : 0 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name));
1608 : :
1609 : : /* Signal that this section is only partially covered. */
1610 [ # # ]: 0 : if (has_relro && in_relro
1611 [ # # ]: 0 : && shdr->sh_addr + shdr->sh_size > relro_to)
1612 : : {
1613 : 0 : fputs (">", stdout);
1614 : 0 : in_relro = false;
1615 : : }
1616 : : }
1617 : : }
1618 [ # # ]: 0 : if (in_relro || in_ro)
1619 : 0 : fputs ("]", stdout);
1620 : :
1621 : : /* Finish the line. */
1622 : 0 : fputc ('\n', stdout);
1623 : : }
1624 : : }
1625 : :
1626 : :
1627 : : static const char *
1628 : 0 : section_name (Ebl *ebl, GElf_Shdr *shdr)
1629 : : {
1630 : 0 : size_t shstrndx;
1631 [ # # # # ]: 0 : if (shdr == NULL || elf_getshdrstrndx (ebl->elf, &shstrndx) < 0)
1632 : 0 : return "???";
1633 [ # # ]: 0 : return elf_strptr (ebl->elf, shstrndx, shdr->sh_name) ?: "???";
1634 : : }
1635 : :
1636 : :
1637 : : static void
1638 : 0 : handle_scngrp (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr)
1639 : : {
1640 : : /* Get the data of the section. */
1641 : 0 : Elf_Data *data = elf_getdata (scn, NULL);
1642 : :
1643 : 0 : Elf_Scn *symscn = elf_getscn (ebl->elf, shdr->sh_link);
1644 : 0 : GElf_Shdr symshdr_mem;
1645 : 0 : GElf_Shdr *symshdr = gelf_getshdr (symscn, &symshdr_mem);
1646 : 0 : Elf_Data *symdata = elf_getdata (symscn, NULL);
1647 : :
1648 [ # # # # ]: 0 : if (data == NULL || data->d_size < sizeof (Elf32_Word) || symshdr == NULL
1649 [ # # ]: 0 : || symdata == NULL)
1650 : 0 : return;
1651 : :
1652 : : /* Get the section header string table index. */
1653 : 0 : size_t shstrndx;
1654 [ # # ]: 0 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
1655 : 0 : error_exit (0, _("cannot get section header string table index"));
1656 : :
1657 : 0 : Elf32_Word *grpref = (Elf32_Word *) data->d_buf;
1658 : :
1659 : 0 : GElf_Sym sym_mem;
1660 : 0 : GElf_Sym *sym = gelf_getsym (symdata, shdr->sh_info, &sym_mem);
1661 : :
1662 [ # # # # ]: 0 : printf ((grpref[0] & GRP_COMDAT)
1663 : 0 : ? ngettext ("\
1664 : : \nCOMDAT section group [%2zu] '%s' with signature '%s' contains %zu entry:\n",
1665 : : "\
1666 : : \nCOMDAT section group [%2zu] '%s' with signature '%s' contains %zu entries:\n",
1667 : : data->d_size / sizeof (Elf32_Word) - 1)
1668 : 0 : : ngettext ("\
1669 : : \nSection group [%2zu] '%s' with signature '%s' contains %zu entry:\n", "\
1670 : : \nSection group [%2zu] '%s' with signature '%s' contains %zu entries:\n",
1671 : : data->d_size / sizeof (Elf32_Word) - 1),
1672 : : elf_ndxscn (scn),
1673 : 0 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
1674 : : (sym == NULL ? NULL
1675 : 0 : : elf_strptr (ebl->elf, symshdr->sh_link, sym->st_name))
1676 : 0 : ?: _("<INVALID SYMBOL>"),
1677 [ # # ]: 0 : data->d_size / sizeof (Elf32_Word) - 1);
1678 : :
1679 [ # # ]: 0 : for (size_t cnt = 1; cnt < data->d_size / sizeof (Elf32_Word); ++cnt)
1680 : : {
1681 : 0 : GElf_Shdr grpshdr_mem;
1682 : 0 : GElf_Shdr *grpshdr = gelf_getshdr (elf_getscn (ebl->elf, grpref[cnt]),
1683 : : &grpshdr_mem);
1684 : :
1685 : 0 : const char *str;
1686 [ # # ]: 0 : printf (" [%2u] %s\n",
1687 : : grpref[cnt],
1688 : : grpshdr != NULL
1689 [ # # ]: 0 : && (str = elf_strptr (ebl->elf, shstrndx, grpshdr->sh_name))
1690 : 0 : ? str : _("<INVALID SECTION>"));
1691 : : }
1692 : : }
1693 : :
1694 : :
1695 : : static void
1696 : 0 : print_scngrp (Ebl *ebl)
1697 : : {
1698 : : /* Find all relocation sections and handle them. */
1699 : 0 : Elf_Scn *scn = NULL;
1700 : :
1701 [ # # ]: 0 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
1702 : : {
1703 : : /* Handle the section if it is a symbol table. */
1704 : 0 : GElf_Shdr shdr_mem;
1705 : 0 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
1706 : :
1707 [ # # # # ]: 0 : if (shdr != NULL && shdr->sh_type == SHT_GROUP)
1708 : : {
1709 [ # # ]: 0 : if ((shdr->sh_flags & SHF_COMPRESSED) != 0)
1710 : : {
1711 [ # # ]: 0 : if (elf_compress (scn, 0, 0) < 0)
1712 : 0 : printf ("WARNING: %s [%zd]\n",
1713 : : _("Couldn't uncompress section"),
1714 : : elf_ndxscn (scn));
1715 : 0 : shdr = gelf_getshdr (scn, &shdr_mem);
1716 [ # # ]: 0 : if (unlikely (shdr == NULL))
1717 : 0 : error_exit (0, _("cannot get section [%zd] header: %s"),
1718 : : elf_ndxscn (scn),
1719 : : elf_errmsg (-1));
1720 : : }
1721 : 0 : handle_scngrp (ebl, scn, shdr);
1722 : : }
1723 : : }
1724 : 0 : }
1725 : :
1726 : :
1727 : : static const struct flags
1728 : : {
1729 : : int mask;
1730 : : const char *str;
1731 : : } dt_flags[] =
1732 : : {
1733 : : { DF_ORIGIN, "ORIGIN" },
1734 : : { DF_SYMBOLIC, "SYMBOLIC" },
1735 : : { DF_TEXTREL, "TEXTREL" },
1736 : : { DF_BIND_NOW, "BIND_NOW" },
1737 : : { DF_STATIC_TLS, "STATIC_TLS" }
1738 : : };
1739 : : static const int ndt_flags = sizeof (dt_flags) / sizeof (dt_flags[0]);
1740 : :
1741 : : static const struct flags dt_flags_1[] =
1742 : : {
1743 : : { DF_1_NOW, "NOW" },
1744 : : { DF_1_GLOBAL, "GLOBAL" },
1745 : : { DF_1_GROUP, "GROUP" },
1746 : : { DF_1_NODELETE, "NODELETE" },
1747 : : { DF_1_LOADFLTR, "LOADFLTR" },
1748 : : { DF_1_INITFIRST, "INITFIRST" },
1749 : : { DF_1_NOOPEN, "NOOPEN" },
1750 : : { DF_1_ORIGIN, "ORIGIN" },
1751 : : { DF_1_DIRECT, "DIRECT" },
1752 : : { DF_1_TRANS, "TRANS" },
1753 : : { DF_1_INTERPOSE, "INTERPOSE" },
1754 : : { DF_1_NODEFLIB, "NODEFLIB" },
1755 : : { DF_1_NODUMP, "NODUMP" },
1756 : : { DF_1_CONFALT, "CONFALT" },
1757 : : { DF_1_ENDFILTEE, "ENDFILTEE" },
1758 : : { DF_1_DISPRELDNE, "DISPRELDNE" },
1759 : : { DF_1_DISPRELPND, "DISPRELPND" },
1760 : : };
1761 : : static const int ndt_flags_1 = sizeof (dt_flags_1) / sizeof (dt_flags_1[0]);
1762 : :
1763 : : static const struct flags dt_feature_1[] =
1764 : : {
1765 : : { DTF_1_PARINIT, "PARINIT" },
1766 : : { DTF_1_CONFEXP, "CONFEXP" }
1767 : : };
1768 : : static const int ndt_feature_1 = (sizeof (dt_feature_1)
1769 : : / sizeof (dt_feature_1[0]));
1770 : :
1771 : : static const struct flags dt_posflag_1[] =
1772 : : {
1773 : : { DF_P1_LAZYLOAD, "LAZYLOAD" },
1774 : : { DF_P1_GROUPPERM, "GROUPPERM" }
1775 : : };
1776 : : static const int ndt_posflag_1 = (sizeof (dt_posflag_1)
1777 : : / sizeof (dt_posflag_1[0]));
1778 : :
1779 : :
1780 : : static void
1781 : 0 : print_flags (int class, GElf_Xword d_val, const struct flags *flags,
1782 : : int nflags)
1783 : : {
1784 : 0 : bool first = true;
1785 : 0 : int cnt;
1786 : :
1787 [ # # ]: 0 : for (cnt = 0; cnt < nflags; ++cnt)
1788 [ # # ]: 0 : if (d_val & flags[cnt].mask)
1789 : : {
1790 [ # # ]: 0 : if (!first)
1791 : 0 : putchar (' ');
1792 : 0 : fputs (flags[cnt].str, stdout);
1793 : 0 : d_val &= ~flags[cnt].mask;
1794 : 0 : first = false;
1795 : : }
1796 : :
1797 [ # # ]: 0 : if (d_val != 0)
1798 : : {
1799 [ # # ]: 0 : if (!first)
1800 : 0 : putchar (' ');
1801 [ # # ]: 0 : printf ("%#0*" PRIx64, class == ELFCLASS32 ? 10 : 18, d_val);
1802 : : }
1803 : :
1804 : 0 : putchar ('\n');
1805 : 0 : }
1806 : :
1807 : :
1808 : : static void
1809 : 0 : print_dt_flags (int class, GElf_Xword d_val)
1810 : : {
1811 : 0 : print_flags (class, d_val, dt_flags, ndt_flags);
1812 : 0 : }
1813 : :
1814 : :
1815 : : static void
1816 : 0 : print_dt_flags_1 (int class, GElf_Xword d_val)
1817 : : {
1818 : 0 : print_flags (class, d_val, dt_flags_1, ndt_flags_1);
1819 : 0 : }
1820 : :
1821 : :
1822 : : static void
1823 : 0 : print_dt_feature_1 (int class, GElf_Xword d_val)
1824 : : {
1825 : 0 : print_flags (class, d_val, dt_feature_1, ndt_feature_1);
1826 : 0 : }
1827 : :
1828 : :
1829 : : static void
1830 : 0 : print_dt_posflag_1 (int class, GElf_Xword d_val)
1831 : : {
1832 : 0 : print_flags (class, d_val, dt_posflag_1, ndt_posflag_1);
1833 : 0 : }
1834 : :
1835 : :
1836 : : static size_t
1837 : 0 : get_dyn_ents (Elf_Data * dyn_data)
1838 : : {
1839 : 0 : GElf_Dyn *dyn;
1840 : 0 : GElf_Dyn dyn_mem;
1841 : 0 : size_t dyn_idx = 0;
1842 : 0 : do
1843 : : {
1844 : 0 : dyn = gelf_getdyn(dyn_data, dyn_idx, &dyn_mem);
1845 [ # # ]: 0 : if (dyn != NULL)
1846 : 0 : ++dyn_idx;
1847 : : }
1848 [ # # ]: 0 : while (dyn != NULL && dyn->d_tag != DT_NULL);
1849 : :
1850 : 0 : return dyn_idx;
1851 : : }
1852 : :
1853 : :
1854 : : static void
1855 : 0 : handle_dynamic (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr, GElf_Phdr *phdr)
1856 : : {
1857 : 0 : int class = gelf_getclass (ebl->elf);
1858 : 0 : GElf_Shdr glink_mem;
1859 : 0 : GElf_Shdr *glink;
1860 : 0 : Elf_Data *data;
1861 : 0 : size_t cnt;
1862 : 0 : size_t shstrndx;
1863 : 0 : size_t dyn_ents;
1864 : :
1865 : : /* Get the data of the section. */
1866 [ # # # # ]: 0 : if (use_dynamic_segment && phdr != NULL)
1867 : 0 : data = elf_getdata_rawchunk(ebl->elf, phdr->p_offset,
1868 : : phdr->p_filesz, ELF_T_DYN);
1869 : : else
1870 : 0 : data = elf_getdata (scn, NULL);
1871 : :
1872 [ # # ]: 0 : if (data == NULL)
1873 : 0 : return;
1874 : :
1875 : : /* Get the dynamic section entry number */
1876 : 0 : dyn_ents = get_dyn_ents (data);
1877 : :
1878 [ # # # # ]: 0 : if (!use_dynamic_segment && shdr != NULL)
1879 : : {
1880 : : /* Get the section header string table index. */
1881 [ # # ]: 0 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
1882 : 0 : error_exit (0, _("cannot get section header string table index"));
1883 : :
1884 : 0 : glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link), &glink_mem);
1885 [ # # ]: 0 : if (glink == NULL)
1886 : 0 : error_exit (0, _("invalid sh_link value in section %zu"),
1887 : : elf_ndxscn (scn));
1888 : :
1889 : 0 : printf (ngettext ("\
1890 : : \nDynamic segment contains %lu entry:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'\n",
1891 : : "\
1892 : : \nDynamic segment contains %lu entries:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'\n",
1893 : : dyn_ents),
1894 : : (unsigned long int) dyn_ents,
1895 : : class == ELFCLASS32 ? 10 : 18, shdr->sh_addr,
1896 : : shdr->sh_offset,
1897 [ # # ]: 0 : (int) shdr->sh_link,
1898 : 0 : elf_strptr (ebl->elf, shstrndx, glink->sh_name));
1899 : : }
1900 [ # # ]: 0 : else if (phdr != NULL)
1901 : : {
1902 [ # # ]: 0 : printf (ngettext ("\
1903 : : \nDynamic segment contains %lu entry:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 "\n",
1904 : : "\
1905 : : \nDynamic segment contains %lu entries:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 "\n",
1906 : : dyn_ents),
1907 : : (unsigned long int) dyn_ents,
1908 : : class == ELFCLASS32 ? 10 : 18, phdr->p_paddr,
1909 : : phdr->p_offset);
1910 : : }
1911 : :
1912 : 0 : fputs (_(" Type Value\n"), stdout);
1913 : :
1914 : : /* if --use-dynamic option is enabled,
1915 : : use the string table to get the related library info. */
1916 : 0 : Elf_Data *strtab_data = NULL;
1917 [ # # # # ]: 0 : if (use_dynamic_segment && phdr != NULL)
1918 : : {
1919 : 0 : strtab_data = get_dynscn_strtab(ebl->elf, phdr);
1920 [ # # ]: 0 : if (strtab_data == NULL)
1921 : 0 : error_exit (0, _("cannot get string table by using dynamic segment"));
1922 : : }
1923 : :
1924 [ # # ]: 0 : for (cnt = 0; cnt < dyn_ents; ++cnt)
1925 : : {
1926 : 0 : GElf_Dyn dynmem;
1927 : 0 : GElf_Dyn *dyn = gelf_getdyn (data, cnt, &dynmem);
1928 [ # # ]: 0 : if (dyn == NULL)
1929 : : break;
1930 : :
1931 : 0 : char buf[64];
1932 : 0 : printf (" %-17s ",
1933 : : ebl_dynamic_tag_name (ebl, dyn->d_tag, buf, sizeof (buf)));
1934 : :
1935 : 0 : char *name = NULL;
1936 : 0 : if (dyn->d_tag == DT_NEEDED
1937 [ # # ]: 0 : || dyn->d_tag == DT_SONAME
1938 [ # # ]: 0 : || dyn->d_tag == DT_RPATH
1939 [ # # ]: 0 : || dyn->d_tag == DT_RUNPATH)
1940 : : {
1941 [ # # # # ]: 0 : if (! use_dynamic_segment && shdr != NULL)
1942 : 0 : name = elf_strptr (ebl->elf, shdr->sh_link, dyn->d_un.d_val);
1943 [ # # ]: 0 : else if (dyn->d_un.d_val < strtab_data->d_size
1944 : 0 : && memrchr (strtab_data->d_buf + dyn->d_un.d_val, '\0',
1945 [ # # ]: 0 : strtab_data->d_size - 1 - dyn->d_un.d_val) != NULL)
1946 : 0 : name = ((char *) strtab_data->d_buf) + dyn->d_un.d_val;
1947 : : }
1948 : :
1949 [ # # # # : 0 : switch (dyn->d_tag)
# # # # #
# # # # ]
1950 : : {
1951 : 0 : case DT_NULL:
1952 : : case DT_DEBUG:
1953 : : case DT_BIND_NOW:
1954 : : case DT_TEXTREL:
1955 : : /* No further output. */
1956 : 0 : fputc ('\n', stdout);
1957 : 0 : break;
1958 : :
1959 : 0 : case DT_NEEDED:
1960 : 0 : printf (_("Shared library: [%s]\n"), name);
1961 : : break;
1962 : :
1963 : 0 : case DT_SONAME:
1964 : 0 : printf (_("Library soname: [%s]\n"), name);
1965 : : break;
1966 : :
1967 : 0 : case DT_RPATH:
1968 : 0 : printf (_("Library rpath: [%s]\n"), name);
1969 : : break;
1970 : :
1971 : 0 : case DT_RUNPATH:
1972 : 0 : printf (_("Library runpath: [%s]\n"), name);
1973 : : break;
1974 : :
1975 : 0 : case DT_PLTRELSZ:
1976 : : case DT_RELASZ:
1977 : : case DT_STRSZ:
1978 : : case DT_RELSZ:
1979 : : case DT_RELRSZ:
1980 : : case DT_RELAENT:
1981 : : case DT_SYMENT:
1982 : : case DT_RELENT:
1983 : : case DT_RELRENT:
1984 : : case DT_PLTPADSZ:
1985 : : case DT_MOVEENT:
1986 : : case DT_MOVESZ:
1987 : : case DT_INIT_ARRAYSZ:
1988 : : case DT_FINI_ARRAYSZ:
1989 : : case DT_SYMINSZ:
1990 : : case DT_SYMINENT:
1991 : : case DT_GNU_CONFLICTSZ:
1992 : : case DT_GNU_LIBLISTSZ:
1993 : 0 : printf (_("%" PRId64 " (bytes)\n"), dyn->d_un.d_val);
1994 : : break;
1995 : :
1996 : 0 : case DT_VERDEFNUM:
1997 : : case DT_VERNEEDNUM:
1998 : : case DT_RELACOUNT:
1999 : : case DT_RELCOUNT:
2000 : 0 : printf ("%" PRId64 "\n", dyn->d_un.d_val);
2001 : : break;
2002 : :
2003 : 0 : case DT_PLTREL:;
2004 : 0 : const char *tagname = ebl_dynamic_tag_name (ebl, dyn->d_un.d_val,
2005 : : NULL, 0);
2006 [ # # ]: 0 : puts (tagname ?: "???");
2007 : 0 : break;
2008 : :
2009 : 0 : case DT_FLAGS:
2010 : 0 : print_dt_flags (class, dyn->d_un.d_val);
2011 : : break;
2012 : :
2013 : 0 : case DT_FLAGS_1:
2014 : 0 : print_dt_flags_1 (class, dyn->d_un.d_val);
2015 : : break;
2016 : :
2017 : 0 : case DT_FEATURE_1:
2018 : 0 : print_dt_feature_1 (class, dyn->d_un.d_val);
2019 : : break;
2020 : :
2021 : 0 : case DT_POSFLAG_1:
2022 : 0 : print_dt_posflag_1 (class, dyn->d_un.d_val);
2023 : : break;
2024 : :
2025 : 0 : default:
2026 [ # # ]: 0 : printf ("%#0*" PRIx64 "\n",
2027 : : class == ELFCLASS32 ? 10 : 18, dyn->d_un.d_val);
2028 : : break;
2029 : : }
2030 : : }
2031 : : }
2032 : :
2033 : :
2034 : : /* Print the dynamic segment. */
2035 : : static void
2036 : 0 : print_dynamic (Ebl *ebl)
2037 : : {
2038 [ # # ]: 0 : for (size_t i = 0; i < phnum; ++i)
2039 : : {
2040 : 0 : GElf_Phdr phdr_mem;
2041 : 0 : GElf_Phdr *phdr = gelf_getphdr (ebl->elf, i, &phdr_mem);
2042 : :
2043 [ # # # # ]: 0 : if (phdr != NULL && phdr->p_type == PT_DYNAMIC)
2044 : : {
2045 : 0 : Elf_Scn *scn = gelf_offscn (ebl->elf, phdr->p_offset);
2046 : 0 : GElf_Shdr shdr_mem;
2047 : 0 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
2048 [ # # ]: 0 : if ((use_dynamic_segment && phdr != NULL)
2049 [ # # # # ]: 0 : || (shdr != NULL && shdr->sh_type == SHT_DYNAMIC))
2050 : 0 : handle_dynamic (ebl, scn, shdr, phdr);
2051 : 0 : break;
2052 : : }
2053 : : }
2054 : 0 : }
2055 : :
2056 : :
2057 : : /* Print relocations. */
2058 : : static void
2059 : 0 : print_relocs (Ebl *ebl, Dwfl_Module *mod, GElf_Ehdr *ehdr)
2060 : : {
2061 : : /* Find all relocation sections and handle them. */
2062 : 0 : Elf_Scn *scn = NULL;
2063 : :
2064 [ # # ]: 0 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
2065 : : {
2066 : : /* Handle the section if it is a symbol table. */
2067 : 0 : GElf_Shdr shdr_mem;
2068 : 0 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
2069 : :
2070 [ # # ]: 0 : if (likely (shdr != NULL))
2071 : : {
2072 [ # # ]: 0 : if (shdr->sh_type == SHT_REL)
2073 : 0 : handle_relocs_rel (ebl, ehdr, scn, shdr);
2074 [ # # ]: 0 : else if (shdr->sh_type == SHT_RELA)
2075 : 0 : handle_relocs_rela (ebl, ehdr, scn, shdr);
2076 [ # # ]: 0 : else if (shdr->sh_type == SHT_RELR)
2077 : 0 : handle_relocs_relr (ebl, mod, scn, shdr);
2078 : : }
2079 : : }
2080 : 0 : }
2081 : :
2082 : :
2083 : : /* Handle a relocation section. */
2084 : : static void
2085 : 0 : handle_relocs_rel (Ebl *ebl, GElf_Ehdr *ehdr, Elf_Scn *scn, GElf_Shdr *shdr)
2086 : : {
2087 : 0 : int class = gelf_getclass (ebl->elf);
2088 : 0 : size_t sh_entsize = gelf_fsize (ebl->elf, ELF_T_REL, 1, EV_CURRENT);
2089 : 0 : int nentries = shdr->sh_size / sh_entsize;
2090 : :
2091 : : /* Get the data of the section. */
2092 : 0 : Elf_Data *data = elf_getdata (scn, NULL);
2093 [ # # ]: 0 : if (data == NULL)
2094 : 0 : return;
2095 : :
2096 : : /* Get the symbol table information. */
2097 : 0 : Elf_Scn *symscn = elf_getscn (ebl->elf, shdr->sh_link);
2098 : 0 : GElf_Shdr symshdr_mem;
2099 : 0 : GElf_Shdr *symshdr = gelf_getshdr (symscn, &symshdr_mem);
2100 : 0 : Elf_Data *symdata = elf_getdata (symscn, NULL);
2101 : :
2102 : : /* Get the section header of the section the relocations are for. */
2103 : 0 : GElf_Shdr destshdr_mem;
2104 : 0 : GElf_Shdr *destshdr = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_info),
2105 : : &destshdr_mem);
2106 : :
2107 [ # # # # ]: 0 : if (unlikely (symshdr == NULL || symdata == NULL || destshdr == NULL))
2108 : : {
2109 : 0 : printf (_("\nInvalid symbol table at offset %#0" PRIx64 "\n"),
2110 : : shdr->sh_offset);
2111 : 0 : return;
2112 : : }
2113 : :
2114 : : /* Search for the optional extended section index table. */
2115 : 0 : Elf_Data *xndxdata = NULL;
2116 : 0 : int xndxscnidx = elf_scnshndx (scn);
2117 [ # # ]: 0 : if (unlikely (xndxscnidx > 0))
2118 : 0 : xndxdata = elf_getdata (elf_getscn (ebl->elf, xndxscnidx), NULL);
2119 : :
2120 : : /* Get the section header string table index. */
2121 : 0 : size_t shstrndx;
2122 [ # # ]: 0 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
2123 : 0 : error_exit (0, _("cannot get section header string table index"));
2124 : :
2125 [ # # ]: 0 : if (shdr->sh_info != 0)
2126 : 0 : printf (ngettext ("\
2127 : : \nRelocation section [%2zu] '%s' for section [%2u] '%s' at offset %#0" PRIx64 " contains %d entry:\n",
2128 : : "\
2129 : : \nRelocation section [%2zu] '%s' for section [%2u] '%s' at offset %#0" PRIx64 " contains %d entries:\n",
2130 : : nentries),
2131 : : elf_ndxscn (scn),
2132 : 0 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
2133 : 0 : (unsigned int) shdr->sh_info,
2134 : 0 : elf_strptr (ebl->elf, shstrndx, destshdr->sh_name),
2135 : : shdr->sh_offset,
2136 : : nentries);
2137 : : else
2138 : : /* The .rel.dyn section does not refer to a specific section but
2139 : : instead of section index zero. Do not try to print a section
2140 : : name. */
2141 : 0 : printf (ngettext ("\
2142 : : \nRelocation section [%2u] '%s' at offset %#0" PRIx64 " contains %d entry:\n",
2143 : : "\
2144 : : \nRelocation section [%2u] '%s' at offset %#0" PRIx64 " contains %d entries:\n",
2145 : : nentries),
2146 : 0 : (unsigned int) elf_ndxscn (scn),
2147 : 0 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
2148 : : shdr->sh_offset,
2149 : : nentries);
2150 [ # # ]: 0 : fputs (class == ELFCLASS32
2151 : 0 : ? _("\
2152 : : Offset Type Value Name\n")
2153 : 0 : : _("\
2154 : : Offset Type Value Name\n"),
2155 : : stdout);
2156 : :
2157 : 0 : int is_statically_linked = 0;
2158 [ # # ]: 0 : for (int cnt = 0; cnt < nentries; ++cnt)
2159 : : {
2160 : 0 : GElf_Rel relmem;
2161 : 0 : GElf_Rel *rel = gelf_getrel (data, cnt, &relmem);
2162 [ # # ]: 0 : if (likely (rel != NULL))
2163 : : {
2164 : 0 : char buf[128];
2165 : 0 : GElf_Sym symmem;
2166 : 0 : Elf32_Word xndx;
2167 : 0 : GElf_Sym *sym = gelf_getsymshndx (symdata, xndxdata,
2168 : 0 : GELF_R_SYM (rel->r_info),
2169 : : &symmem, &xndx);
2170 [ # # ]: 0 : if (unlikely (sym == NULL))
2171 : : {
2172 : : /* As a special case we have to handle relocations in static
2173 : : executables. This only happens for IRELATIVE relocations
2174 : : (so far). There is no symbol table. */
2175 [ # # ]: 0 : if (is_statically_linked == 0)
2176 : : {
2177 : : /* Find the program header and look for a PT_INTERP entry. */
2178 : 0 : is_statically_linked = -1;
2179 [ # # ]: 0 : if (ehdr->e_type == ET_EXEC)
2180 : : {
2181 : 0 : is_statically_linked = 1;
2182 : :
2183 [ # # ]: 0 : for (size_t inner = 0; inner < phnum; ++inner)
2184 : : {
2185 : 0 : GElf_Phdr phdr_mem;
2186 : 0 : GElf_Phdr *phdr = gelf_getphdr (ebl->elf, inner,
2187 : : &phdr_mem);
2188 [ # # # # ]: 0 : if (phdr != NULL && phdr->p_type == PT_INTERP)
2189 : : {
2190 : 0 : is_statically_linked = -1;
2191 : 0 : break;
2192 : : }
2193 : : }
2194 : : }
2195 : : }
2196 : :
2197 [ # # # # ]: 0 : if (is_statically_linked > 0 && shdr->sh_link == 0)
2198 [ # # # # ]: 0 : printf ("\
2199 : : %#0*" PRIx64 " %-20s %*s %s\n",
2200 : : class == ELFCLASS32 ? 10 : 18, rel->r_offset,
2201 : 0 : ebl_reloc_type_check (ebl, GELF_R_TYPE (rel->r_info))
2202 : : /* Avoid the leading R_ which isn't carrying any
2203 : : information. */
2204 : 0 : ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info),
2205 : : buf, sizeof (buf)) + 2
2206 : 0 : : _("<INVALID RELOC>"),
2207 : : class == ELFCLASS32 ? 10 : 18, "",
2208 : 0 : elf_strptr (ebl->elf, shstrndx, destshdr->sh_name));
2209 : : else
2210 [ # # # # ]: 0 : printf (" %#0*" PRIx64 " %-20s <%s %ld>\n",
2211 : : class == ELFCLASS32 ? 10 : 18, rel->r_offset,
2212 : 0 : ebl_reloc_type_check (ebl, GELF_R_TYPE (rel->r_info))
2213 : : /* Avoid the leading R_ which isn't carrying any
2214 : : information. */
2215 : 0 : ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info),
2216 : : buf, sizeof (buf)) + 2
2217 : 0 : : _("<INVALID RELOC>"),
2218 : : _("INVALID SYMBOL"),
2219 : 0 : (long int) GELF_R_SYM (rel->r_info));
2220 : : }
2221 [ # # ]: 0 : else if (GELF_ST_TYPE (sym->st_info) != STT_SECTION)
2222 [ # # ]: 0 : printf (" %#0*" PRIx64 " %-20s %#0*" PRIx64 " %s\n",
2223 : : class == ELFCLASS32 ? 10 : 18, rel->r_offset,
2224 [ # # ]: 0 : likely (ebl_reloc_type_check (ebl,
2225 : : GELF_R_TYPE (rel->r_info)))
2226 : : /* Avoid the leading R_ which isn't carrying any
2227 : : information. */
2228 : 0 : ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info),
2229 : : buf, sizeof (buf)) + 2
2230 : 0 : : _("<INVALID RELOC>"),
2231 : : class == ELFCLASS32 ? 10 : 18, sym->st_value,
2232 : 0 : elf_strptr (ebl->elf, symshdr->sh_link, sym->st_name));
2233 : : else
2234 : : {
2235 : : /* This is a relocation against a STT_SECTION symbol. */
2236 : 0 : GElf_Shdr secshdr_mem;
2237 : 0 : GElf_Shdr *secshdr;
2238 : 0 : secshdr = gelf_getshdr (elf_getscn (ebl->elf,
2239 [ # # ]: 0 : sym->st_shndx == SHN_XINDEX
2240 : 0 : ? xndx : sym->st_shndx),
2241 : : &secshdr_mem);
2242 : :
2243 [ # # ]: 0 : if (unlikely (secshdr == NULL))
2244 [ # # # # ]: 0 : printf (" %#0*" PRIx64 " %-20s <%s %ld>\n",
2245 : : class == ELFCLASS32 ? 10 : 18, rel->r_offset,
2246 : 0 : ebl_reloc_type_check (ebl, GELF_R_TYPE (rel->r_info))
2247 : : /* Avoid the leading R_ which isn't carrying any
2248 : : information. */
2249 : 0 : ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info),
2250 : : buf, sizeof (buf)) + 2
2251 : 0 : : _("<INVALID RELOC>"),
2252 : : _("INVALID SECTION"),
2253 [ # # ]: 0 : (long int) (sym->st_shndx == SHN_XINDEX
2254 : 0 : ? xndx : sym->st_shndx));
2255 : : else
2256 [ # # # # ]: 0 : printf (" %#0*" PRIx64 " %-20s %#0*" PRIx64 " %s\n",
2257 : : class == ELFCLASS32 ? 10 : 18, rel->r_offset,
2258 : 0 : ebl_reloc_type_check (ebl, GELF_R_TYPE (rel->r_info))
2259 : : /* Avoid the leading R_ which isn't carrying any
2260 : : information. */
2261 : 0 : ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info),
2262 : : buf, sizeof (buf)) + 2
2263 : 0 : : _("<INVALID RELOC>"),
2264 : : class == ELFCLASS32 ? 10 : 18, sym->st_value,
2265 : 0 : elf_strptr (ebl->elf, shstrndx, secshdr->sh_name));
2266 : : }
2267 : : }
2268 : : }
2269 : : }
2270 : :
2271 : :
2272 : : /* Handle a relocation section. */
2273 : : static void
2274 : 0 : handle_relocs_rela (Ebl *ebl, GElf_Ehdr *ehdr, Elf_Scn *scn, GElf_Shdr *shdr)
2275 : : {
2276 : 0 : int class = gelf_getclass (ebl->elf);
2277 : 0 : size_t sh_entsize = gelf_fsize (ebl->elf, ELF_T_RELA, 1, EV_CURRENT);
2278 : 0 : int nentries = shdr->sh_size / sh_entsize;
2279 : :
2280 : : /* Get the data of the section. */
2281 : 0 : Elf_Data *data = elf_getdata (scn, NULL);
2282 [ # # ]: 0 : if (data == NULL)
2283 : 0 : return;
2284 : :
2285 : : /* Get the symbol table information. */
2286 : 0 : Elf_Scn *symscn = elf_getscn (ebl->elf, shdr->sh_link);
2287 : 0 : GElf_Shdr symshdr_mem;
2288 : 0 : GElf_Shdr *symshdr = gelf_getshdr (symscn, &symshdr_mem);
2289 : 0 : Elf_Data *symdata = elf_getdata (symscn, NULL);
2290 : :
2291 : : /* Get the section header of the section the relocations are for. */
2292 : 0 : GElf_Shdr destshdr_mem;
2293 : 0 : GElf_Shdr *destshdr = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_info),
2294 : : &destshdr_mem);
2295 : :
2296 [ # # # # ]: 0 : if (unlikely (symshdr == NULL || symdata == NULL || destshdr == NULL))
2297 : : {
2298 : 0 : printf (_("\nInvalid symbol table at offset %#0" PRIx64 "\n"),
2299 : : shdr->sh_offset);
2300 : 0 : return;
2301 : : }
2302 : :
2303 : : /* Search for the optional extended section index table. */
2304 : 0 : Elf_Data *xndxdata = NULL;
2305 : 0 : int xndxscnidx = elf_scnshndx (scn);
2306 [ # # ]: 0 : if (unlikely (xndxscnidx > 0))
2307 : 0 : xndxdata = elf_getdata (elf_getscn (ebl->elf, xndxscnidx), NULL);
2308 : :
2309 : : /* Get the section header string table index. */
2310 : 0 : size_t shstrndx;
2311 [ # # ]: 0 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
2312 : 0 : error_exit (0, _("cannot get section header string table index"));
2313 : :
2314 [ # # ]: 0 : if (shdr->sh_info != 0)
2315 : 0 : printf (ngettext ("\
2316 : : \nRelocation section [%2zu] '%s' for section [%2u] '%s' at offset %#0" PRIx64 " contains %d entry:\n",
2317 : : "\
2318 : : \nRelocation section [%2zu] '%s' for section [%2u] '%s' at offset %#0" PRIx64 " contains %d entries:\n",
2319 : : nentries),
2320 : : elf_ndxscn (scn),
2321 : 0 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
2322 : 0 : (unsigned int) shdr->sh_info,
2323 : 0 : elf_strptr (ebl->elf, shstrndx, destshdr->sh_name),
2324 : : shdr->sh_offset,
2325 : : nentries);
2326 : : else
2327 : : /* The .rela.dyn section does not refer to a specific section but
2328 : : instead of section index zero. Do not try to print a section
2329 : : name. */
2330 : 0 : printf (ngettext ("\
2331 : : \nRelocation section [%2u] '%s' at offset %#0" PRIx64 " contains %d entry:\n",
2332 : : "\
2333 : : \nRelocation section [%2u] '%s' at offset %#0" PRIx64 " contains %d entries:\n",
2334 : : nentries),
2335 : 0 : (unsigned int) elf_ndxscn (scn),
2336 : 0 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
2337 : : shdr->sh_offset,
2338 : : nentries);
2339 [ # # ]: 0 : fputs (class == ELFCLASS32
2340 : 0 : ? _("\
2341 : : Offset Type Value Addend Name\n")
2342 : 0 : : _("\
2343 : : Offset Type Value Addend Name\n"),
2344 : : stdout);
2345 : :
2346 : 0 : int is_statically_linked = 0;
2347 [ # # ]: 0 : for (int cnt = 0; cnt < nentries; ++cnt)
2348 : : {
2349 : 0 : GElf_Rela relmem;
2350 : 0 : GElf_Rela *rel = gelf_getrela (data, cnt, &relmem);
2351 [ # # ]: 0 : if (likely (rel != NULL))
2352 : : {
2353 : 0 : char buf[64];
2354 : 0 : GElf_Sym symmem;
2355 : 0 : Elf32_Word xndx;
2356 : 0 : GElf_Sym *sym = gelf_getsymshndx (symdata, xndxdata,
2357 : 0 : GELF_R_SYM (rel->r_info),
2358 : : &symmem, &xndx);
2359 : :
2360 [ # # ]: 0 : if (unlikely (sym == NULL))
2361 : : {
2362 : : /* As a special case we have to handle relocations in static
2363 : : executables. This only happens for IRELATIVE relocations
2364 : : (so far). There is no symbol table. */
2365 [ # # ]: 0 : if (is_statically_linked == 0)
2366 : : {
2367 : : /* Find the program header and look for a PT_INTERP entry. */
2368 : 0 : is_statically_linked = -1;
2369 [ # # ]: 0 : if (ehdr->e_type == ET_EXEC)
2370 : : {
2371 : 0 : is_statically_linked = 1;
2372 : :
2373 [ # # ]: 0 : for (size_t inner = 0; inner < phnum; ++inner)
2374 : : {
2375 : 0 : GElf_Phdr phdr_mem;
2376 : 0 : GElf_Phdr *phdr = gelf_getphdr (ebl->elf, inner,
2377 : : &phdr_mem);
2378 [ # # # # ]: 0 : if (phdr != NULL && phdr->p_type == PT_INTERP)
2379 : : {
2380 : 0 : is_statically_linked = -1;
2381 : 0 : break;
2382 : : }
2383 : : }
2384 : : }
2385 : : }
2386 : :
2387 [ # # # # ]: 0 : if (is_statically_linked > 0 && shdr->sh_link == 0)
2388 [ # # # # ]: 0 : printf ("\
2389 : : %#0*" PRIx64 " %-15s %*s %#6" PRIx64 " %s\n",
2390 : : class == ELFCLASS32 ? 10 : 18, rel->r_offset,
2391 : 0 : ebl_reloc_type_check (ebl, GELF_R_TYPE (rel->r_info))
2392 : : /* Avoid the leading R_ which isn't carrying any
2393 : : information. */
2394 : 0 : ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info),
2395 : : buf, sizeof (buf)) + 2
2396 : 0 : : _("<INVALID RELOC>"),
2397 : : class == ELFCLASS32 ? 10 : 18, "",
2398 : : rel->r_addend,
2399 : 0 : elf_strptr (ebl->elf, shstrndx, destshdr->sh_name));
2400 : : else
2401 [ # # # # ]: 0 : printf (" %#0*" PRIx64 " %-15s <%s %ld>\n",
2402 : : class == ELFCLASS32 ? 10 : 18, rel->r_offset,
2403 : 0 : ebl_reloc_type_check (ebl, GELF_R_TYPE (rel->r_info))
2404 : : /* Avoid the leading R_ which isn't carrying any
2405 : : information. */
2406 : 0 : ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info),
2407 : : buf, sizeof (buf)) + 2
2408 : 0 : : _("<INVALID RELOC>"),
2409 : : _("INVALID SYMBOL"),
2410 : 0 : (long int) GELF_R_SYM (rel->r_info));
2411 : : }
2412 [ # # ]: 0 : else if (GELF_ST_TYPE (sym->st_info) != STT_SECTION)
2413 [ # # ]: 0 : printf ("\
2414 : : %#0*" PRIx64 " %-15s %#0*" PRIx64 " %+6" PRId64 " %s\n",
2415 : : class == ELFCLASS32 ? 10 : 18, rel->r_offset,
2416 [ # # ]: 0 : likely (ebl_reloc_type_check (ebl,
2417 : : GELF_R_TYPE (rel->r_info)))
2418 : : /* Avoid the leading R_ which isn't carrying any
2419 : : information. */
2420 : 0 : ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info),
2421 : : buf, sizeof (buf)) + 2
2422 : 0 : : _("<INVALID RELOC>"),
2423 : : class == ELFCLASS32 ? 10 : 18, sym->st_value,
2424 : : rel->r_addend,
2425 : 0 : elf_strptr (ebl->elf, symshdr->sh_link, sym->st_name));
2426 : : else
2427 : : {
2428 : : /* This is a relocation against a STT_SECTION symbol. */
2429 : 0 : GElf_Shdr secshdr_mem;
2430 : 0 : GElf_Shdr *secshdr;
2431 : 0 : secshdr = gelf_getshdr (elf_getscn (ebl->elf,
2432 [ # # ]: 0 : sym->st_shndx == SHN_XINDEX
2433 : 0 : ? xndx : sym->st_shndx),
2434 : : &secshdr_mem);
2435 : :
2436 [ # # ]: 0 : if (unlikely (secshdr == NULL))
2437 [ # # # # ]: 0 : printf (" %#0*" PRIx64 " %-15s <%s %ld>\n",
2438 : : class == ELFCLASS32 ? 10 : 18, rel->r_offset,
2439 : 0 : ebl_reloc_type_check (ebl, GELF_R_TYPE (rel->r_info))
2440 : : /* Avoid the leading R_ which isn't carrying any
2441 : : information. */
2442 : 0 : ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info),
2443 : : buf, sizeof (buf)) + 2
2444 : 0 : : _("<INVALID RELOC>"),
2445 : : _("INVALID SECTION"),
2446 [ # # ]: 0 : (long int) (sym->st_shndx == SHN_XINDEX
2447 : 0 : ? xndx : sym->st_shndx));
2448 : : else
2449 [ # # # # ]: 0 : printf ("\
2450 : : %#0*" PRIx64 " %-15s %#0*" PRIx64 " %+6" PRId64 " %s\n",
2451 : : class == ELFCLASS32 ? 10 : 18, rel->r_offset,
2452 : 0 : ebl_reloc_type_check (ebl, GELF_R_TYPE (rel->r_info))
2453 : : /* Avoid the leading R_ which isn't carrying any
2454 : : information. */
2455 : 0 : ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info),
2456 : : buf, sizeof (buf)) + 2
2457 : 0 : : _("<INVALID RELOC>"),
2458 : : class == ELFCLASS32 ? 10 : 18, sym->st_value,
2459 : : rel->r_addend,
2460 : 0 : elf_strptr (ebl->elf, shstrndx, secshdr->sh_name));
2461 : : }
2462 : : }
2463 : : }
2464 : : }
2465 : :
2466 : : /* Handle a relocation section. */
2467 : : static void
2468 : 0 : handle_relocs_relr (Ebl *ebl, Dwfl_Module *mod, Elf_Scn *scn, GElf_Shdr *shdr)
2469 : : {
2470 : 0 : int class = gelf_getclass (ebl->elf);
2471 : 0 : size_t sh_entsize = gelf_fsize (ebl->elf, ELF_T_RELR, 1, EV_CURRENT);
2472 : 0 : int nentries = shdr->sh_size / sh_entsize;
2473 : :
2474 : : /* Get the data of the section. */
2475 : 0 : Elf_Data *data = elf_getdata (scn, NULL);
2476 [ # # ]: 0 : if (data == NULL)
2477 : 0 : return;
2478 : :
2479 : : /* Get the section header string table index. */
2480 : 0 : size_t shstrndx;
2481 [ # # ]: 0 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
2482 : 0 : error_exit (0, _("cannot get section header string table index"));
2483 : :
2484 : : /* A .relr.dyn section does not refer to a specific section. */
2485 : 0 : printf (ngettext ("\
2486 : : \nRelocation section [%2u] '%s' at offset %#0" PRIx64 " contains %d entry:\n",
2487 : : "\
2488 : : \nRelocation section [%2u] '%s' at offset %#0" PRIx64 " contains %d entries:\n",
2489 : : nentries),
2490 : 0 : (unsigned int) elf_ndxscn (scn),
2491 : 0 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
2492 : : shdr->sh_offset,
2493 : : nentries);
2494 : :
2495 [ # # ]: 0 : if (class == ELFCLASS32)
2496 : : {
2497 : : uint32_t base = 0;
2498 [ # # ]: 0 : for (int cnt = 0; cnt < nentries; ++cnt)
2499 : : {
2500 : 0 : Elf32_Word *words = data->d_buf;
2501 : 0 : Elf32_Word entry = words[cnt];
2502 : :
2503 : : /* Just the raw entries? */
2504 [ # # ]: 0 : if (print_unresolved_addresses)
2505 : 0 : printf (" %#010" PRIx32 "%s\n", entry,
2506 [ # # ]: 0 : (entry & 1) == 0 ? " *" : "");
2507 : : else
2508 : : {
2509 : : /* A real address, also sets base. */
2510 [ # # ]: 0 : if ((entry & 1) == 0)
2511 : : {
2512 : 0 : printf (" ");
2513 : 0 : print_dwarf_addr (mod, 4, entry, entry);
2514 : 0 : printf (" *\n");
2515 : :
2516 : 0 : base = entry + 4;
2517 : : }
2518 : : else
2519 : : {
2520 : : /* Untangle address from base and bits. */
2521 : : uint32_t addr;
2522 [ # # ]: 0 : for (addr = base; (entry >>= 1) != 0; addr += 4)
2523 [ # # ]: 0 : if ((entry & 1) != 0)
2524 : : {
2525 : 0 : printf (" ");
2526 : 0 : print_dwarf_addr (mod, 4, addr, addr);
2527 : 0 : printf ("\n");
2528 : : }
2529 : 0 : base += 4 * (4 * 8 - 1);
2530 : : }
2531 : : }
2532 : : }
2533 : : }
2534 : : else
2535 : : {
2536 : : uint64_t base = 0;
2537 [ # # ]: 0 : for (int cnt = 0; cnt < nentries; ++cnt)
2538 : : {
2539 : 0 : Elf64_Xword *xwords = data->d_buf;
2540 : 0 : Elf64_Xword entry = xwords[cnt];
2541 : :
2542 : : /* Just the raw entries? */
2543 [ # # ]: 0 : if (print_unresolved_addresses)
2544 : 0 : printf (" %#018" PRIx64 "%s\n", entry,
2545 [ # # ]: 0 : (entry & 1) == 0 ? " *" : "");
2546 : : else
2547 : : {
2548 : : /* A real address, also sets base. */
2549 [ # # ]: 0 : if ((entry & 1) == 0)
2550 : : {
2551 : 0 : printf (" ");
2552 : 0 : print_dwarf_addr (mod, 8, entry, entry);
2553 : 0 : printf (" *\n");
2554 : :
2555 : 0 : base = entry + 8;
2556 : : }
2557 : : else
2558 : : {
2559 : : /* Untangle address from base and bits. */
2560 : : uint64_t addr;
2561 [ # # ]: 0 : for (addr = base; (entry >>= 1) != 0; addr += 8)
2562 [ # # ]: 0 : if ((entry & 1) != 0)
2563 : : {
2564 : 0 : printf (" ");
2565 : 0 : print_dwarf_addr (mod, 8, addr, addr);
2566 : 0 : printf ("\n");
2567 : : }
2568 : 0 : base += 8 * (8 * 8 - 1);
2569 : : }
2570 : : }
2571 : : }
2572 : : }
2573 : : }
2574 : :
2575 : : /* Print the program header. Return true if a symtab is printed,
2576 : : false otherwise. */
2577 : : static bool
2578 : 0 : print_symtab (Ebl *ebl, int type)
2579 : : {
2580 : : /* Use the dynamic section info to display symbol tables. */
2581 [ # # # # ]: 0 : if (use_dynamic_segment && type == SHT_DYNSYM)
2582 : 0 : return handle_dynamic_symtab(ebl);
2583 : :
2584 : : /* Find the symbol table(s). For this we have to search through the
2585 : : section table. */
2586 : : Elf_Scn *scn = NULL;
2587 : : bool symtab_printed = false;
2588 : :
2589 [ # # ]: 0 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
2590 : : {
2591 : : /* Handle the section if it is a symbol table. */
2592 : 0 : GElf_Shdr shdr_mem;
2593 : 0 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
2594 : :
2595 [ # # # # ]: 0 : if (shdr != NULL && shdr->sh_type == (GElf_Word) type)
2596 : : {
2597 [ # # ]: 0 : if (symbol_table_section != NULL)
2598 : : {
2599 : : /* Get the section header string table index. */
2600 : 0 : size_t shstrndx;
2601 : 0 : const char *sname;
2602 [ # # ]: 0 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
2603 : 0 : error_exit (0,
2604 : : _("cannot get section header string table index"));
2605 : 0 : sname = elf_strptr (ebl->elf, shstrndx, shdr->sh_name);
2606 [ # # # # ]: 0 : if (sname == NULL || strcmp (sname, symbol_table_section) != 0)
2607 : 0 : continue;
2608 : : }
2609 : :
2610 [ # # ]: 0 : if ((shdr->sh_flags & SHF_COMPRESSED) != 0)
2611 : : {
2612 [ # # ]: 0 : if (elf_compress (scn, 0, 0) < 0)
2613 : 0 : printf ("WARNING: %s [%zd]\n",
2614 : : _("Couldn't uncompress section"),
2615 : : elf_ndxscn (scn));
2616 : 0 : shdr = gelf_getshdr (scn, &shdr_mem);
2617 [ # # ]: 0 : if (unlikely (shdr == NULL))
2618 : 0 : error_exit (0,
2619 : : _("cannot get section [%zd] header: %s"),
2620 : : elf_ndxscn (scn), elf_errmsg (-1));
2621 : : }
2622 : 0 : symtab_printed = handle_symtab (ebl, scn, shdr);
2623 : : }
2624 : : }
2625 : :
2626 : : return symtab_printed;
2627 : : }
2628 : :
2629 : :
2630 : : static void
2631 : 0 : process_symtab (Ebl *ebl, unsigned int nsyms, Elf64_Word idx,
2632 : : Elf32_Word verneed_stridx, Elf32_Word verdef_stridx,
2633 : : Elf_Data *symdata, Elf_Data *versym_data,
2634 : : Elf_Data *symstr_data, Elf_Data *verneed_data,
2635 : : Elf_Data *verdef_data, Elf_Data *xndx_data)
2636 : : {
2637 [ # # ]: 0 : for (unsigned int cnt = 0; cnt < nsyms; ++cnt)
2638 : : {
2639 : 0 : char typebuf[64];
2640 : 0 : char bindbuf[64];
2641 : 0 : char scnbuf[64];
2642 : 0 : const char *sym_name;
2643 : 0 : Elf32_Word xndx;
2644 : 0 : GElf_Sym sym_mem;
2645 : 0 : GElf_Sym *sym
2646 : 0 : = gelf_getsymshndx (symdata, xndx_data, cnt, &sym_mem, &xndx);
2647 : :
2648 [ # # ]: 0 : if (unlikely (sym == NULL))
2649 : 0 : continue;
2650 : :
2651 : : /* Determine the real section index. */
2652 [ # # ]: 0 : if (likely (sym->st_shndx != SHN_XINDEX))
2653 : 0 : xndx = sym->st_shndx;
2654 [ # # ]: 0 : if (use_dynamic_segment == true)
2655 : : {
2656 [ # # ]: 0 : if (validate_str (symstr_data->d_buf, sym->st_name,
2657 : : symstr_data->d_size))
2658 : 0 : sym_name = (char *)symstr_data->d_buf + sym->st_name;
2659 : : else
2660 : : sym_name = NULL;
2661 : : }
2662 : : else
2663 : 0 : sym_name = elf_strptr (ebl->elf, idx, sym->st_name);
2664 : :
2665 [ # # ]: 0 : if (sym_name == NULL)
2666 : : sym_name = "???";
2667 : :
2668 [ # # ]: 0 : printf (_ ("\
2669 : : %5u: %0*" PRIx64 " %6" PRId64 " %-7s %-6s %-9s %6s %s"),
2670 : 0 : cnt, gelf_getclass (ebl->elf) == ELFCLASS32 ? 8 : 16,
2671 : : sym->st_value, sym->st_size,
2672 : 0 : ebl_symbol_type_name (ebl, GELF_ST_TYPE (sym->st_info), typebuf,
2673 : : sizeof (typebuf)),
2674 : 0 : ebl_symbol_binding_name (ebl, GELF_ST_BIND (sym->st_info),
2675 : : bindbuf, sizeof (bindbuf)),
2676 : 0 : get_visibility_type (GELF_ST_VISIBILITY (sym->st_other)),
2677 : 0 : ebl_section_name (ebl, sym->st_shndx, xndx, scnbuf,
2678 : : sizeof (scnbuf), NULL, shnum),
2679 : : sym_name);
2680 : :
2681 [ # # ]: 0 : if (versym_data != NULL)
2682 : : {
2683 : : /* Get the version information. */
2684 : 0 : GElf_Versym versym_mem;
2685 : 0 : GElf_Versym *versym = gelf_getversym (versym_data, cnt, &versym_mem);
2686 : :
2687 [ # # # # ]: 0 : if (versym != NULL && ((*versym & 0x8000) != 0 || *versym > 1))
2688 : : {
2689 : 0 : bool is_nobits = false;
2690 : 0 : bool check_def = xndx != SHN_UNDEF;
2691 : :
2692 [ # # # # ]: 0 : if (xndx < SHN_LORESERVE || sym->st_shndx == SHN_XINDEX)
2693 : : {
2694 : 0 : GElf_Shdr symshdr_mem;
2695 : 0 : GElf_Shdr *symshdr = gelf_getshdr (
2696 : : elf_getscn (ebl->elf, xndx), &symshdr_mem);
2697 : :
2698 : 0 : is_nobits
2699 [ # # # # ]: 0 : = (symshdr != NULL && symshdr->sh_type == SHT_NOBITS);
2700 : : }
2701 : :
2702 [ # # ]: 0 : if (is_nobits || !check_def)
2703 : : {
2704 : : /* We must test both. */
2705 : 0 : GElf_Vernaux vernaux_mem;
2706 : 0 : GElf_Vernaux *vernaux = NULL;
2707 : 0 : size_t vn_offset = 0;
2708 : :
2709 : 0 : GElf_Verneed verneed_mem;
2710 : 0 : GElf_Verneed *verneed
2711 : 0 : = gelf_getverneed (verneed_data, 0, &verneed_mem);
2712 [ # # ]: 0 : while (verneed != NULL)
2713 : : {
2714 : 0 : size_t vna_offset = vn_offset;
2715 : :
2716 : 0 : vernaux = gelf_getvernaux (verneed_data,
2717 : 0 : vna_offset += verneed->vn_aux,
2718 : : &vernaux_mem);
2719 [ # # ]: 0 : while (vernaux != NULL && vernaux->vna_other != *versym
2720 [ # # ]: 0 : && vernaux->vna_next != 0
2721 [ # # ]: 0 : && (verneed_data->d_size - vna_offset
2722 [ # # ]: 0 : >= vernaux->vna_next))
2723 : : {
2724 : : /* Update the offset. */
2725 : 0 : vna_offset += vernaux->vna_next;
2726 : :
2727 : 0 : vernaux = (vernaux->vna_next == 0
2728 : : ? NULL
2729 : 0 : : gelf_getvernaux (verneed_data,
2730 : : vna_offset,
2731 : : &vernaux_mem));
2732 : : }
2733 : :
2734 : : /* Check whether we found the version. */
2735 [ # # # # ]: 0 : if (vernaux != NULL && vernaux->vna_other == *versym)
2736 : : /* Found it. */
2737 : : break;
2738 : :
2739 [ # # ]: 0 : if (verneed_data->d_size - vn_offset < verneed->vn_next)
2740 : : break;
2741 : :
2742 : 0 : vn_offset += verneed->vn_next;
2743 : 0 : verneed
2744 : : = (verneed->vn_next == 0
2745 : : ? NULL
2746 [ # # ]: 0 : : gelf_getverneed (verneed_data, vn_offset,
2747 : : &verneed_mem));
2748 : : }
2749 : :
2750 [ # # # # ]: 0 : if (vernaux != NULL && vernaux->vna_other == *versym)
2751 : : {
2752 : 0 : printf ("@%s (%u)",
2753 [ # # ]: 0 : use_dynamic_segment == true
2754 : 0 : ? (char *)symstr_data->d_buf
2755 : 0 : + vernaux->vna_name
2756 : 0 : : elf_strptr (ebl->elf, verneed_stridx,
2757 : 0 : vernaux->vna_name),
2758 : : (unsigned int)vernaux->vna_other);
2759 : 0 : check_def = 0;
2760 : : }
2761 [ # # ]: 0 : else if (unlikely (!is_nobits))
2762 : 0 : error (0, 0, _ ("bad dynamic symbol"));
2763 : : else
2764 : : check_def = 1;
2765 : : }
2766 : :
2767 [ # # # # ]: 0 : if (check_def && *versym != 0x8001)
2768 : : {
2769 : : /* We must test both. */
2770 : 0 : size_t vd_offset = 0;
2771 : :
2772 : 0 : GElf_Verdef verdef_mem;
2773 : 0 : GElf_Verdef *verdef
2774 : 0 : = gelf_getverdef (verdef_data, 0, &verdef_mem);
2775 [ # # ]: 0 : while (verdef != NULL)
2776 : : {
2777 [ # # ]: 0 : if (verdef->vd_ndx == (*versym & 0x7fff))
2778 : : /* Found the definition. */
2779 : : break;
2780 : :
2781 [ # # ]: 0 : if (verdef_data->d_size - vd_offset < verdef->vd_next)
2782 : : break;
2783 : :
2784 : 0 : vd_offset += verdef->vd_next;
2785 : 0 : verdef = (verdef->vd_next == 0
2786 : : ? NULL
2787 [ # # ]: 0 : : gelf_getverdef (verdef_data, vd_offset,
2788 : : &verdef_mem));
2789 : : }
2790 : :
2791 [ # # ]: 0 : if (verdef != NULL)
2792 : : {
2793 : 0 : GElf_Verdaux verdaux_mem;
2794 : 0 : GElf_Verdaux *verdaux = gelf_getverdaux (
2795 : 0 : verdef_data, vd_offset + verdef->vd_aux,
2796 : : &verdaux_mem);
2797 : :
2798 [ # # ]: 0 : if (verdaux != NULL)
2799 [ # # ]: 0 : printf ((*versym & 0x8000) ? "@%s" : "@@%s",
2800 [ # # ]: 0 : use_dynamic_segment == true
2801 : 0 : ? (char *)symstr_data->d_buf
2802 : 0 : + verdaux->vda_name
2803 : 0 : : elf_strptr (ebl->elf, verdef_stridx,
2804 : 0 : verdaux->vda_name));
2805 : : }
2806 : : }
2807 : : }
2808 : : }
2809 : :
2810 : 0 : putchar ('\n');
2811 : : }
2812 : 0 : }
2813 : :
2814 : :
2815 : : static bool
2816 : 0 : handle_symtab (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr)
2817 : : {
2818 : 0 : Elf_Data *versym_data = NULL;
2819 : 0 : Elf_Data *verneed_data = NULL;
2820 : 0 : Elf_Data *verdef_data = NULL;
2821 : 0 : Elf_Data *xndx_data = NULL;
2822 : 0 : int class = gelf_getclass (ebl->elf);
2823 : 0 : Elf32_Word verneed_stridx = 0;
2824 : 0 : Elf32_Word verdef_stridx = 0;
2825 : :
2826 : : /* Get the data of the section. */
2827 : 0 : Elf_Data *data = elf_getdata (scn, NULL);
2828 [ # # ]: 0 : if (data == NULL)
2829 : : return false;
2830 : :
2831 : : /* Find out whether we have other sections we might need. */
2832 : : Elf_Scn *runscn = NULL;
2833 [ # # ]: 0 : while ((runscn = elf_nextscn (ebl->elf, runscn)) != NULL)
2834 : : {
2835 : 0 : GElf_Shdr runshdr_mem;
2836 : 0 : GElf_Shdr *runshdr = gelf_getshdr (runscn, &runshdr_mem);
2837 : :
2838 [ # # ]: 0 : if (likely (runshdr != NULL))
2839 : : {
2840 [ # # ]: 0 : if (runshdr->sh_type == SHT_GNU_versym
2841 [ # # ]: 0 : && runshdr->sh_link == elf_ndxscn (scn))
2842 : : /* Bingo, found the version information. Now get the data. */
2843 : 0 : versym_data = elf_getdata (runscn, NULL);
2844 [ # # ]: 0 : else if (runshdr->sh_type == SHT_GNU_verneed)
2845 : : {
2846 : : /* This is the information about the needed versions. */
2847 : 0 : verneed_data = elf_getdata (runscn, NULL);
2848 : 0 : verneed_stridx = runshdr->sh_link;
2849 : : }
2850 [ # # ]: 0 : else if (runshdr->sh_type == SHT_GNU_verdef)
2851 : : {
2852 : : /* This is the information about the defined versions. */
2853 : 0 : verdef_data = elf_getdata (runscn, NULL);
2854 : 0 : verdef_stridx = runshdr->sh_link;
2855 : : }
2856 [ # # ]: 0 : else if (runshdr->sh_type == SHT_SYMTAB_SHNDX
2857 [ # # ]: 0 : && runshdr->sh_link == elf_ndxscn (scn))
2858 : : /* Extended section index. */
2859 : 0 : xndx_data = elf_getdata (runscn, NULL);
2860 : : }
2861 : : }
2862 : :
2863 : : /* Get the section header string table index. */
2864 : 0 : size_t shstrndx;
2865 [ # # ]: 0 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
2866 : 0 : error_exit (0, _("cannot get section header string table index"));
2867 : :
2868 : 0 : GElf_Shdr glink_mem;
2869 : 0 : GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
2870 : : &glink_mem);
2871 [ # # ]: 0 : if (glink == NULL)
2872 : 0 : error_exit (0, _("invalid sh_link value in section %zu"),
2873 : : elf_ndxscn (scn));
2874 : :
2875 : : /* Now we can compute the number of entries in the section. */
2876 : 0 : unsigned int nsyms = data->d_size / (class == ELFCLASS32
2877 : : ? sizeof (Elf32_Sym)
2878 [ # # ]: 0 : : sizeof (Elf64_Sym));
2879 : :
2880 : 0 : printf (ngettext ("\nSymbol table [%2u] '%s' contains %u entry:\n",
2881 : : "\nSymbol table [%2u] '%s' contains %u entries:\n",
2882 : : nsyms),
2883 : 0 : (unsigned int) elf_ndxscn (scn),
2884 : 0 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name), nsyms);
2885 : 0 : printf (ngettext (" %lu local symbol String table: [%2u] '%s'\n",
2886 : : " %lu local symbols String table: [%2u] '%s'\n",
2887 : : shdr->sh_info),
2888 : 0 : (unsigned long int) shdr->sh_info,
2889 : 0 : (unsigned int) shdr->sh_link,
2890 : 0 : elf_strptr (ebl->elf, shstrndx, glink->sh_name));
2891 : :
2892 [ # # ]: 0 : fputs (class == ELFCLASS32
2893 : 0 : ? _("\
2894 : : Num: Value Size Type Bind Vis Ndx Name\n")
2895 : 0 : : _("\
2896 : : Num: Value Size Type Bind Vis Ndx Name\n"),
2897 : : stdout);
2898 : :
2899 : 0 : process_symtab(ebl, nsyms, shdr->sh_link, verneed_stridx, verdef_stridx,
2900 : : data, versym_data, NULL, verneed_data, verdef_data, xndx_data);
2901 : 0 : return true;
2902 : : }
2903 : :
2904 : :
2905 : : static bool
2906 : 0 : handle_dynamic_symtab (Ebl *ebl)
2907 : : {
2908 : 0 : GElf_Phdr phdr_mem;
2909 : 0 : GElf_Phdr *phdr = NULL;
2910 : : /* phnum is a static variable which was already fetched in function
2911 : : process_elf_file. */
2912 [ # # ]: 0 : for (size_t i = 0; i < phnum; ++i)
2913 : : {
2914 : 0 : phdr = gelf_getphdr (ebl->elf, i, &phdr_mem);
2915 [ # # # # ]: 0 : if (phdr == NULL || phdr->p_type == PT_DYNAMIC)
2916 : : break;
2917 : : }
2918 [ # # ]: 0 : if (phdr == NULL)
2919 : : return false;
2920 : :
2921 : 0 : GElf_Addr addrs[i_max] = {
2922 : : 0,
2923 : : };
2924 : 0 : GElf_Off offs[i_max] = {
2925 : : 0,
2926 : : };
2927 : 0 : get_dynscn_addrs (ebl->elf, phdr, addrs);
2928 : 0 : find_offsets (ebl->elf, 0, i_max, addrs, offs);
2929 : :
2930 : 0 : size_t syments = 0;
2931 : :
2932 : 0 : GElf_Ehdr ehdr_mem;
2933 : 0 : GElf_Ehdr *ehdr = gelf_getehdr (ebl->elf, &ehdr_mem);
2934 : :
2935 [ # # ]: 0 : if (offs[i_hash] != 0)
2936 : : {
2937 : : /* In the original format, .hash says the size of .dynsym. */
2938 : :
2939 [ # # # # : 0 : size_t entsz = SH_ENTSIZE_HASH (ehdr);
# # ]
2940 : : Elf_Data *data
2941 : 0 : = elf_getdata_rawchunk (ebl->elf, offs[i_hash] + entsz, entsz,
2942 : : (entsz == 4 ? ELF_T_WORD : ELF_T_XWORD));
2943 [ # # ]: 0 : if (data != NULL)
2944 : 0 : syments = (entsz == 4 ? *(const GElf_Word *)data->d_buf
2945 [ # # ]: 0 : : *(const GElf_Xword *)data->d_buf);
2946 : : }
2947 [ # # # # ]: 0 : if (offs[i_gnu_hash] != 0 && syments == 0)
2948 : : {
2949 : : /* In the new format, we can derive it with some work. */
2950 : :
2951 : 0 : const struct
2952 : : {
2953 : : Elf32_Word nbuckets;
2954 : : Elf32_Word symndx;
2955 : : Elf32_Word maskwords;
2956 : : Elf32_Word shift2;
2957 : : } * header;
2958 : :
2959 : 0 : Elf_Data *data = elf_getdata_rawchunk (ebl->elf, offs[i_gnu_hash],
2960 : : sizeof *header, ELF_T_WORD);
2961 [ # # ]: 0 : if (data != NULL)
2962 : : {
2963 : 0 : header = data->d_buf;
2964 : 0 : Elf32_Word nbuckets = header->nbuckets;
2965 : 0 : Elf32_Word symndx = header->symndx;
2966 : 0 : GElf_Off buckets_at
2967 : : = (offs[i_gnu_hash] + sizeof *header
2968 : 0 : + (gelf_getclass (ebl->elf) * sizeof (Elf32_Word)
2969 : 0 : * header->maskwords));
2970 : :
2971 : : // elf_getdata_rawchunk takes a size_t, make sure it
2972 : : // doesn't overflow.
2973 : : #if SIZE_MAX <= UINT32_MAX
2974 : : if (nbuckets > SIZE_MAX / sizeof (Elf32_Word))
2975 : : data = NULL;
2976 : : else
2977 : : #endif
2978 : 0 : data = elf_getdata_rawchunk (ebl->elf, buckets_at,
2979 : : nbuckets * sizeof (Elf32_Word),
2980 : : ELF_T_WORD);
2981 [ # # ]: 0 : if (data != NULL && symndx < nbuckets)
2982 : : {
2983 : 0 : const Elf32_Word *const buckets = data->d_buf;
2984 : 0 : Elf32_Word maxndx = symndx;
2985 [ # # ]: 0 : for (Elf32_Word bucket = 0; bucket < nbuckets; ++bucket)
2986 : 0 : if (buckets[bucket] > maxndx)
2987 : : maxndx = buckets[bucket];
2988 : :
2989 : 0 : GElf_Off hasharr_at
2990 : : = (buckets_at + nbuckets * sizeof (Elf32_Word));
2991 : 0 : hasharr_at += (maxndx - symndx) * sizeof (Elf32_Word);
2992 : 0 : do
2993 : : {
2994 : 0 : data = elf_getdata_rawchunk (
2995 : : ebl->elf, hasharr_at, sizeof (Elf32_Word), ELF_T_WORD);
2996 [ # # # # ]: 0 : if (data != NULL && (*(const Elf32_Word *)data->d_buf & 1u))
2997 : : {
2998 : 0 : syments = maxndx + 1;
2999 : 0 : break;
3000 : : }
3001 : 0 : ++maxndx;
3002 : 0 : hasharr_at += sizeof (Elf32_Word);
3003 : : }
3004 [ # # ]: 0 : while (data != NULL);
3005 : : }
3006 : : }
3007 : : }
3008 [ # # # # ]: 0 : if (offs[i_strtab] > offs[i_symtab] && syments == 0)
3009 : 0 : syments = ((offs[i_strtab] - offs[i_symtab])
3010 : 0 : / gelf_fsize (ebl->elf, ELF_T_SYM, 1, EV_CURRENT));
3011 : :
3012 [ # # # # : 0 : if (syments <= 0 || offs[i_strtab] == 0 || offs[i_symtab] == 0)
# # ]
3013 : : {
3014 : 0 : error_exit (0, _ ("Dynamic symbol information is not available for "
3015 : : "displaying symbols."));
3016 : : }
3017 : :
3018 : : /* All the data chunk initializaion. */
3019 : 0 : Elf_Data *symdata = NULL;
3020 : 0 : Elf_Data *symstrdata = NULL;
3021 : 0 : Elf_Data *versym_data = NULL;
3022 : 0 : Elf_Data *verdef_data = NULL;
3023 : 0 : Elf_Data *verneed_data = NULL;
3024 : :
3025 : 0 : symdata = elf_getdata_rawchunk (
3026 : : ebl->elf, offs[i_symtab],
3027 : : gelf_fsize (ebl->elf, ELF_T_SYM, syments, EV_CURRENT), ELF_T_SYM);
3028 : 0 : symstrdata = elf_getdata_rawchunk (ebl->elf, offs[i_strtab], addrs[i_strsz],
3029 : : ELF_T_BYTE);
3030 : 0 : versym_data = elf_getdata_rawchunk (
3031 : 0 : ebl->elf, offs[i_versym], syments * sizeof (Elf64_Half), ELF_T_HALF);
3032 : :
3033 : : /* Get the verneed_data without vernaux. */
3034 : 0 : verneed_data = elf_getdata_rawchunk (
3035 : 0 : ebl->elf, offs[i_verneed], addrs[i_verneednum] * sizeof (Elf64_Verneed),
3036 : : ELF_T_VNEED);
3037 : 0 : size_t vernauxnum = 0;
3038 : 0 : size_t vn_next_offset = 0;
3039 : :
3040 [ # # ]: 0 : for (size_t i = 0; i < addrs[i_verneednum]; i++)
3041 : : {
3042 : 0 : GElf_Verneed *verneed
3043 : 0 : = (GElf_Verneed *)(verneed_data->d_buf + vn_next_offset);
3044 : 0 : vernauxnum += verneed->vn_cnt;
3045 : 0 : vn_next_offset += verneed->vn_next;
3046 : : }
3047 : :
3048 : : /* Update the verneed_data to include the vernaux. */
3049 : 0 : verneed_data = elf_getdata_rawchunk (
3050 : : ebl->elf, offs[i_verneed],
3051 : 0 : (addrs[i_verneednum] + vernauxnum) * sizeof (GElf_Verneed), ELF_T_VNEED);
3052 : :
3053 : : /* Get the verdef_data without verdaux. */
3054 : 0 : verdef_data = elf_getdata_rawchunk (
3055 : 0 : ebl->elf, offs[i_verdef], addrs[i_verdefnum] * sizeof (Elf64_Verdef),
3056 : : ELF_T_VDEF);
3057 : 0 : size_t verdauxnum = 0;
3058 : 0 : size_t vd_next_offset = 0;
3059 : :
3060 [ # # ]: 0 : for (size_t i = 0; i < addrs[i_verdefnum]; i++)
3061 : : {
3062 : 0 : GElf_Verdef *verdef
3063 : 0 : = (GElf_Verdef *)(verdef_data->d_buf + vd_next_offset);
3064 : 0 : verdauxnum += verdef->vd_cnt;
3065 : 0 : vd_next_offset += verdef->vd_next;
3066 : : }
3067 : :
3068 : : /* Update the verdef_data to include the verdaux. */
3069 : 0 : verdef_data = elf_getdata_rawchunk (
3070 : : ebl->elf, offs[i_verdef],
3071 : 0 : (addrs[i_verdefnum] + verdauxnum) * sizeof (GElf_Verdef), ELF_T_VDEF);
3072 : :
3073 : 0 : unsigned int nsyms = (unsigned int)syments;
3074 : 0 : process_symtab (ebl, nsyms, 0, 0, 0, symdata, versym_data, symstrdata,
3075 : : verneed_data, verdef_data, NULL);
3076 : 0 : return true;
3077 : : }
3078 : :
3079 : :
3080 : : /* Print version information. */
3081 : : static void
3082 : 0 : print_verinfo (Ebl *ebl)
3083 : : {
3084 : : /* Find the version information sections. For this we have to
3085 : : search through the section table. */
3086 : 0 : Elf_Scn *scn = NULL;
3087 : :
3088 [ # # ]: 0 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
3089 : : {
3090 : : /* Handle the section if it is part of the versioning handling. */
3091 : 0 : GElf_Shdr shdr_mem;
3092 : 0 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
3093 : :
3094 [ # # ]: 0 : if (likely (shdr != NULL))
3095 : : {
3096 [ # # ]: 0 : if (shdr->sh_type == SHT_GNU_verneed)
3097 : 0 : handle_verneed (ebl, scn, shdr);
3098 [ # # ]: 0 : else if (shdr->sh_type == SHT_GNU_verdef)
3099 : 0 : handle_verdef (ebl, scn, shdr);
3100 [ # # ]: 0 : else if (shdr->sh_type == SHT_GNU_versym)
3101 : 0 : handle_versym (ebl, scn, shdr);
3102 : : }
3103 : : }
3104 : 0 : }
3105 : :
3106 : :
3107 : : static const char *
3108 : 0 : get_ver_flags (unsigned int flags)
3109 : : {
3110 : 0 : static char buf[32];
3111 : 0 : char *endp;
3112 : :
3113 [ # # ]: 0 : if (flags == 0)
3114 : 0 : return _("none");
3115 : :
3116 [ # # ]: 0 : if (flags & VER_FLG_BASE)
3117 : 0 : endp = stpcpy (buf, "BASE ");
3118 : : else
3119 : : endp = buf;
3120 : :
3121 [ # # ]: 0 : if (flags & VER_FLG_WEAK)
3122 : : {
3123 [ # # ]: 0 : if (endp != buf)
3124 : 0 : endp = stpcpy (endp, "| ");
3125 : :
3126 : 0 : endp = stpcpy (endp, "WEAK ");
3127 : : }
3128 : :
3129 [ # # ]: 0 : if (unlikely (flags & ~(VER_FLG_BASE | VER_FLG_WEAK)))
3130 : : {
3131 : 0 : strncpy (endp, _("| <unknown>"), buf + sizeof (buf) - endp);
3132 : 0 : buf[sizeof (buf) - 1] = '\0';
3133 : : }
3134 : :
3135 : : return buf;
3136 : : }
3137 : :
3138 : :
3139 : : static void
3140 : 0 : handle_verneed (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr)
3141 : : {
3142 : 0 : int class = gelf_getclass (ebl->elf);
3143 : :
3144 : : /* Get the data of the section. */
3145 : 0 : Elf_Data *data = elf_getdata (scn, NULL);
3146 [ # # ]: 0 : if (data == NULL)
3147 : 0 : return;
3148 : :
3149 : : /* Get the section header string table index. */
3150 : 0 : size_t shstrndx;
3151 [ # # ]: 0 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
3152 : 0 : error_exit (0, _("cannot get section header string table index"));
3153 : :
3154 : 0 : GElf_Shdr glink_mem;
3155 : 0 : GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
3156 : : &glink_mem);
3157 [ # # ]: 0 : if (glink == NULL)
3158 : 0 : error_exit (0, _("invalid sh_link value in section %zu"),
3159 : : elf_ndxscn (scn));
3160 : :
3161 : 0 : printf (ngettext ("\
3162 : : \nVersion needs section [%2u] '%s' contains %d entry:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'\n",
3163 : : "\
3164 : : \nVersion needs section [%2u] '%s' contains %d entries:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'\n",
3165 : : shdr->sh_info),
3166 : 0 : (unsigned int) elf_ndxscn (scn),
3167 : 0 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name), shdr->sh_info,
3168 : : class == ELFCLASS32 ? 10 : 18, shdr->sh_addr,
3169 : : shdr->sh_offset,
3170 [ # # ]: 0 : (unsigned int) shdr->sh_link,
3171 : 0 : elf_strptr (ebl->elf, shstrndx, glink->sh_name));
3172 : :
3173 : 0 : unsigned int offset = 0;
3174 [ # # ]: 0 : for (unsigned int cnt = shdr->sh_info; cnt > 0; cnt--)
3175 : : {
3176 : : /* Get the data at the next offset. */
3177 : 0 : GElf_Verneed needmem;
3178 : 0 : GElf_Verneed *need = gelf_getverneed (data, offset, &needmem);
3179 [ # # ]: 0 : if (unlikely (need == NULL))
3180 : : break;
3181 : :
3182 : 0 : printf (_(" %#06x: Version: %hu File: %s Cnt: %hu\n"),
3183 : 0 : offset, (unsigned short int) need->vn_version,
3184 : 0 : elf_strptr (ebl->elf, shdr->sh_link, need->vn_file),
3185 : 0 : (unsigned short int) need->vn_cnt);
3186 : :
3187 : 0 : unsigned int auxoffset = offset + need->vn_aux;
3188 [ # # ]: 0 : for (unsigned int cnt2 = need->vn_cnt; cnt2 > 0; cnt2--)
3189 : : {
3190 : 0 : GElf_Vernaux auxmem;
3191 : 0 : GElf_Vernaux *aux = gelf_getvernaux (data, auxoffset, &auxmem);
3192 [ # # ]: 0 : if (unlikely (aux == NULL))
3193 : : break;
3194 : :
3195 : 0 : printf (_(" %#06x: Name: %s Flags: %s Version: %hu\n"),
3196 : : auxoffset,
3197 : 0 : elf_strptr (ebl->elf, shdr->sh_link, aux->vna_name),
3198 : 0 : get_ver_flags (aux->vna_flags),
3199 : 0 : (unsigned short int) aux->vna_other);
3200 : :
3201 [ # # ]: 0 : if (aux->vna_next == 0)
3202 : : break;
3203 : :
3204 : 0 : auxoffset += aux->vna_next;
3205 : : }
3206 : :
3207 : : /* Find the next offset. */
3208 [ # # ]: 0 : if (need->vn_next == 0)
3209 : : break;
3210 : :
3211 : 0 : offset += need->vn_next;
3212 : : }
3213 : : }
3214 : :
3215 : :
3216 : : static void
3217 : 0 : handle_verdef (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr)
3218 : : {
3219 : : /* Get the data of the section. */
3220 : 0 : Elf_Data *data = elf_getdata (scn, NULL);
3221 [ # # ]: 0 : if (data == NULL)
3222 : 0 : return;
3223 : :
3224 : : /* Get the section header string table index. */
3225 : 0 : size_t shstrndx;
3226 [ # # ]: 0 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
3227 : 0 : error_exit (0, _("cannot get section header string table index"));
3228 : :
3229 : 0 : GElf_Shdr glink_mem;
3230 : 0 : GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
3231 : : &glink_mem);
3232 [ # # ]: 0 : if (glink == NULL)
3233 : 0 : error_exit (0, _("invalid sh_link value in section %zu"),
3234 : : elf_ndxscn (scn));
3235 : :
3236 : 0 : int class = gelf_getclass (ebl->elf);
3237 : 0 : printf (ngettext ("\
3238 : : \nVersion definition section [%2u] '%s' contains %d entry:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'\n",
3239 : : "\
3240 : : \nVersion definition section [%2u] '%s' contains %d entries:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'\n",
3241 : : shdr->sh_info),
3242 : 0 : (unsigned int) elf_ndxscn (scn),
3243 : 0 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
3244 : : shdr->sh_info,
3245 : : class == ELFCLASS32 ? 10 : 18, shdr->sh_addr,
3246 : : shdr->sh_offset,
3247 [ # # ]: 0 : (unsigned int) shdr->sh_link,
3248 : 0 : elf_strptr (ebl->elf, shstrndx, glink->sh_name));
3249 : :
3250 : 0 : unsigned int offset = 0;
3251 [ # # ]: 0 : for (unsigned int cnt = shdr->sh_info; cnt > 0; cnt--)
3252 : : {
3253 : : /* Get the data at the next offset. */
3254 : 0 : GElf_Verdef defmem;
3255 : 0 : GElf_Verdef *def = gelf_getverdef (data, offset, &defmem);
3256 [ # # ]: 0 : if (unlikely (def == NULL))
3257 : : break;
3258 : :
3259 : 0 : unsigned int auxoffset = offset + def->vd_aux;
3260 : 0 : GElf_Verdaux auxmem;
3261 : 0 : GElf_Verdaux *aux = gelf_getverdaux (data, auxoffset, &auxmem);
3262 [ # # ]: 0 : if (unlikely (aux == NULL))
3263 : : break;
3264 : :
3265 : 0 : printf (_("\
3266 : : %#06x: Version: %hd Flags: %s Index: %hd Cnt: %hd Name: %s\n"),
3267 : 0 : offset, def->vd_version,
3268 : 0 : get_ver_flags (def->vd_flags),
3269 : 0 : def->vd_ndx,
3270 : 0 : def->vd_cnt,
3271 : 0 : elf_strptr (ebl->elf, shdr->sh_link, aux->vda_name));
3272 : :
3273 : 0 : auxoffset += aux->vda_next;
3274 [ # # ]: 0 : for (unsigned int cnt2 = 1; cnt2 < def->vd_cnt; ++cnt2)
3275 : : {
3276 : 0 : aux = gelf_getverdaux (data, auxoffset, &auxmem);
3277 [ # # ]: 0 : if (unlikely (aux == NULL))
3278 : : break;
3279 : :
3280 : 0 : printf (_(" %#06x: Parent %d: %s\n"),
3281 : : auxoffset, cnt2,
3282 : 0 : elf_strptr (ebl->elf, shdr->sh_link, aux->vda_name));
3283 : :
3284 [ # # ]: 0 : if (aux->vda_next == 0)
3285 : : break;
3286 : :
3287 : 0 : auxoffset += aux->vda_next;
3288 : : }
3289 : :
3290 : : /* Find the next offset. */
3291 [ # # ]: 0 : if (def->vd_next == 0)
3292 : : break;
3293 : 0 : offset += def->vd_next;
3294 : : }
3295 : : }
3296 : :
3297 : :
3298 : : static void
3299 : 0 : handle_versym (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr)
3300 : : {
3301 : 0 : int class = gelf_getclass (ebl->elf);
3302 : 0 : const char **vername;
3303 : 0 : const char **filename;
3304 : :
3305 : : /* Get the data of the section. */
3306 : 0 : Elf_Data *data = elf_getdata (scn, NULL);
3307 [ # # ]: 0 : if (data == NULL)
3308 : 0 : return;
3309 : :
3310 : : /* Get the section header string table index. */
3311 : 0 : size_t shstrndx;
3312 [ # # ]: 0 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
3313 : 0 : error_exit (0, _("cannot get section header string table index"));
3314 : :
3315 : : /* We have to find the version definition section and extract the
3316 : : version names. */
3317 : : Elf_Scn *defscn = NULL;
3318 : : Elf_Scn *needscn = NULL;
3319 : :
3320 : : Elf_Scn *verscn = NULL;
3321 [ # # ]: 0 : while ((verscn = elf_nextscn (ebl->elf, verscn)) != NULL)
3322 : : {
3323 : 0 : GElf_Shdr vershdr_mem;
3324 : 0 : GElf_Shdr *vershdr = gelf_getshdr (verscn, &vershdr_mem);
3325 : :
3326 [ # # ]: 0 : if (likely (vershdr != NULL))
3327 : : {
3328 [ # # ]: 0 : if (vershdr->sh_type == SHT_GNU_verdef)
3329 : : defscn = verscn;
3330 [ # # ]: 0 : else if (vershdr->sh_type == SHT_GNU_verneed)
3331 : 0 : needscn = verscn;
3332 : : }
3333 : : }
3334 : :
3335 : 0 : size_t nvername;
3336 [ # # ]: 0 : if (defscn != NULL || needscn != NULL)
3337 : : {
3338 : : /* We have a version information (better should have). Now get
3339 : : the version names. First find the maximum version number. */
3340 : 0 : nvername = 0;
3341 [ # # ]: 0 : if (defscn != NULL)
3342 : : {
3343 : : /* Run through the version definitions and find the highest
3344 : : index. */
3345 : 0 : unsigned int offset = 0;
3346 : 0 : Elf_Data *defdata;
3347 : 0 : GElf_Shdr defshdrmem;
3348 : 0 : GElf_Shdr *defshdr;
3349 : :
3350 : 0 : defdata = elf_getdata (defscn, NULL);
3351 [ # # ]: 0 : if (unlikely (defdata == NULL))
3352 : 0 : return;
3353 : :
3354 : 0 : defshdr = gelf_getshdr (defscn, &defshdrmem);
3355 [ # # ]: 0 : if (unlikely (defshdr == NULL))
3356 : : return;
3357 : :
3358 [ # # ]: 0 : for (unsigned int cnt = 0; cnt < defshdr->sh_info; ++cnt)
3359 : : {
3360 : 0 : GElf_Verdef defmem;
3361 : 0 : GElf_Verdef *def;
3362 : :
3363 : : /* Get the data at the next offset. */
3364 : 0 : def = gelf_getverdef (defdata, offset, &defmem);
3365 [ # # ]: 0 : if (unlikely (def == NULL))
3366 : : break;
3367 : :
3368 : 0 : nvername = MAX (nvername, (size_t) (def->vd_ndx & 0x7fff));
3369 : :
3370 [ # # ]: 0 : if (def->vd_next == 0)
3371 : : break;
3372 : 0 : offset += def->vd_next;
3373 : : }
3374 : : }
3375 [ # # ]: 0 : if (needscn != NULL)
3376 : : {
3377 : 0 : unsigned int offset = 0;
3378 : 0 : Elf_Data *needdata;
3379 : 0 : GElf_Shdr needshdrmem;
3380 : 0 : GElf_Shdr *needshdr;
3381 : :
3382 : 0 : needdata = elf_getdata (needscn, NULL);
3383 [ # # ]: 0 : if (unlikely (needdata == NULL))
3384 : 0 : return;
3385 : :
3386 : 0 : needshdr = gelf_getshdr (needscn, &needshdrmem);
3387 [ # # ]: 0 : if (unlikely (needshdr == NULL))
3388 : : return;
3389 : :
3390 [ # # ]: 0 : for (unsigned int cnt = 0; cnt < needshdr->sh_info; ++cnt)
3391 : : {
3392 : 0 : GElf_Verneed needmem;
3393 : 0 : GElf_Verneed *need;
3394 : 0 : unsigned int auxoffset;
3395 : 0 : int cnt2;
3396 : :
3397 : : /* Get the data at the next offset. */
3398 : 0 : need = gelf_getverneed (needdata, offset, &needmem);
3399 [ # # ]: 0 : if (unlikely (need == NULL))
3400 : : break;
3401 : :
3402 : : /* Run through the auxiliary entries. */
3403 : 0 : auxoffset = offset + need->vn_aux;
3404 [ # # ]: 0 : for (cnt2 = need->vn_cnt; --cnt2 >= 0; )
3405 : : {
3406 : 0 : GElf_Vernaux auxmem;
3407 : 0 : GElf_Vernaux *aux;
3408 : :
3409 : 0 : aux = gelf_getvernaux (needdata, auxoffset, &auxmem);
3410 [ # # ]: 0 : if (unlikely (aux == NULL))
3411 : : break;
3412 : :
3413 : 0 : nvername = MAX (nvername,
3414 : : (size_t) (aux->vna_other & 0x7fff));
3415 : :
3416 [ # # ]: 0 : if (aux->vna_next == 0)
3417 : : break;
3418 : 0 : auxoffset += aux->vna_next;
3419 : : }
3420 : :
3421 [ # # ]: 0 : if (need->vn_next == 0)
3422 : : break;
3423 : 0 : offset += need->vn_next;
3424 : : }
3425 : : }
3426 : :
3427 : : /* This is the number of versions we know about. */
3428 : 0 : ++nvername;
3429 : :
3430 : : /* Allocate the array. */
3431 : 0 : vername = (const char **) alloca (nvername * sizeof (const char *));
3432 [ # # ]: 0 : memset(vername, 0, nvername * sizeof (const char *));
3433 : 0 : filename = (const char **) alloca (nvername * sizeof (const char *));
3434 : 0 : memset(filename, 0, nvername * sizeof (const char *));
3435 : :
3436 : : /* Run through the data structures again and collect the strings. */
3437 [ # # ]: 0 : if (defscn != NULL)
3438 : : {
3439 : : /* Run through the version definitions and find the highest
3440 : : index. */
3441 : 0 : unsigned int offset = 0;
3442 : 0 : Elf_Data *defdata;
3443 : 0 : GElf_Shdr defshdrmem;
3444 : 0 : GElf_Shdr *defshdr;
3445 : :
3446 : 0 : defdata = elf_getdata (defscn, NULL);
3447 [ # # ]: 0 : if (unlikely (defdata == NULL))
3448 : 0 : return;
3449 : :
3450 : 0 : defshdr = gelf_getshdr (defscn, &defshdrmem);
3451 [ # # ]: 0 : if (unlikely (defshdr == NULL))
3452 : : return;
3453 : :
3454 [ # # ]: 0 : for (unsigned int cnt = 0; cnt < defshdr->sh_info; ++cnt)
3455 : : {
3456 : :
3457 : : /* Get the data at the next offset. */
3458 : 0 : GElf_Verdef defmem;
3459 : 0 : GElf_Verdef *def = gelf_getverdef (defdata, offset, &defmem);
3460 [ # # ]: 0 : if (unlikely (def == NULL))
3461 : : break;
3462 : :
3463 : 0 : GElf_Verdaux auxmem;
3464 : 0 : GElf_Verdaux *aux = gelf_getverdaux (defdata,
3465 : 0 : offset + def->vd_aux,
3466 : : &auxmem);
3467 [ # # ]: 0 : if (unlikely (aux == NULL))
3468 : : break;
3469 : :
3470 : 0 : vername[def->vd_ndx & 0x7fff]
3471 : 0 : = elf_strptr (ebl->elf, defshdr->sh_link, aux->vda_name);
3472 : 0 : filename[def->vd_ndx & 0x7fff] = NULL;
3473 : :
3474 [ # # ]: 0 : if (def->vd_next == 0)
3475 : : break;
3476 : 0 : offset += def->vd_next;
3477 : : }
3478 : : }
3479 [ # # ]: 0 : if (needscn != NULL)
3480 : : {
3481 : 0 : unsigned int offset = 0;
3482 : :
3483 : 0 : Elf_Data *needdata = elf_getdata (needscn, NULL);
3484 : 0 : GElf_Shdr needshdrmem;
3485 : 0 : GElf_Shdr *needshdr = gelf_getshdr (needscn, &needshdrmem);
3486 [ # # ]: 0 : if (unlikely (needdata == NULL || needshdr == NULL))
3487 : 0 : return;
3488 : :
3489 [ # # ]: 0 : for (unsigned int cnt = 0; cnt < needshdr->sh_info; ++cnt)
3490 : : {
3491 : : /* Get the data at the next offset. */
3492 : 0 : GElf_Verneed needmem;
3493 : 0 : GElf_Verneed *need = gelf_getverneed (needdata, offset,
3494 : : &needmem);
3495 [ # # ]: 0 : if (unlikely (need == NULL))
3496 : : break;
3497 : :
3498 : : /* Run through the auxiliary entries. */
3499 : 0 : unsigned int auxoffset = offset + need->vn_aux;
3500 [ # # ]: 0 : for (int cnt2 = need->vn_cnt; --cnt2 >= 0; )
3501 : : {
3502 : 0 : GElf_Vernaux auxmem;
3503 : 0 : GElf_Vernaux *aux = gelf_getvernaux (needdata, auxoffset,
3504 : : &auxmem);
3505 [ # # ]: 0 : if (unlikely (aux == NULL))
3506 : : break;
3507 : :
3508 : 0 : vername[aux->vna_other & 0x7fff]
3509 : 0 : = elf_strptr (ebl->elf, needshdr->sh_link, aux->vna_name);
3510 : 0 : filename[aux->vna_other & 0x7fff]
3511 : 0 : = elf_strptr (ebl->elf, needshdr->sh_link, need->vn_file);
3512 : :
3513 [ # # ]: 0 : if (aux->vna_next == 0)
3514 : : break;
3515 : 0 : auxoffset += aux->vna_next;
3516 : : }
3517 : :
3518 [ # # ]: 0 : if (need->vn_next == 0)
3519 : : break;
3520 : 0 : offset += need->vn_next;
3521 : : }
3522 : : }
3523 : : }
3524 : : else
3525 : : {
3526 : : vername = NULL;
3527 : : nvername = 1;
3528 : : filename = NULL;
3529 : : }
3530 : :
3531 : 0 : GElf_Shdr glink_mem;
3532 : 0 : GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
3533 : : &glink_mem);
3534 : 0 : size_t sh_entsize = gelf_fsize (ebl->elf, ELF_T_HALF, 1, EV_CURRENT);
3535 [ # # ]: 0 : if (glink == NULL)
3536 : 0 : error_exit (0, _("invalid sh_link value in section %zu"),
3537 : : elf_ndxscn (scn));
3538 : :
3539 : : /* Print the header. */
3540 : 0 : printf (ngettext ("\
3541 : : \nVersion symbols section [%2u] '%s' contains %d entry:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'",
3542 : : "\
3543 : : \nVersion symbols section [%2u] '%s' contains %d entries:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'",
3544 : : shdr->sh_size / sh_entsize),
3545 : 0 : (unsigned int) elf_ndxscn (scn),
3546 : 0 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
3547 : 0 : (int) (shdr->sh_size / sh_entsize),
3548 : : class == ELFCLASS32 ? 10 : 18, shdr->sh_addr,
3549 : : shdr->sh_offset,
3550 [ # # ]: 0 : (unsigned int) shdr->sh_link,
3551 : 0 : elf_strptr (ebl->elf, shstrndx, glink->sh_name));
3552 : :
3553 : : /* Now we can finally look at the actual contents of this section. */
3554 [ # # ]: 0 : for (unsigned int cnt = 0; cnt < shdr->sh_size / sh_entsize; ++cnt)
3555 : : {
3556 [ # # ]: 0 : if (cnt % 2 == 0)
3557 : 0 : printf ("\n %4d:", cnt);
3558 : :
3559 : 0 : GElf_Versym symmem;
3560 : 0 : GElf_Versym *sym = gelf_getversym (data, cnt, &symmem);
3561 [ # # ]: 0 : if (sym == NULL)
3562 : : break;
3563 : :
3564 [ # # # ]: 0 : switch (*sym)
3565 : : {
3566 : 0 : ssize_t n;
3567 : 0 : case 0:
3568 : 0 : fputs (_(" 0 *local* "),
3569 : : stdout);
3570 : 0 : break;
3571 : :
3572 : 0 : case 1:
3573 : 0 : fputs (_(" 1 *global* "),
3574 : : stdout);
3575 : 0 : break;
3576 : :
3577 : 0 : default:
3578 [ # # ]: 0 : n = printf ("%4d%c%s",
3579 [ # # ]: 0 : *sym & 0x7fff, *sym & 0x8000 ? 'h' : ' ',
3580 : : (vername != NULL
3581 [ # # ]: 0 : && (unsigned int) (*sym & 0x7fff) < nvername)
3582 : 0 : ? vername[*sym & 0x7fff] : "???");
3583 [ # # ]: 0 : if ((unsigned int) (*sym & 0x7fff) < nvername
3584 [ # # # # ]: 0 : && filename != NULL && filename[*sym & 0x7fff] != NULL)
3585 : 0 : n += printf ("(%s)", filename[*sym & 0x7fff]);
3586 : 0 : printf ("%*s", MAX (0, 33 - (int) n), " ");
3587 : : break;
3588 : : }
3589 : : }
3590 : 0 : putchar ('\n');
3591 : : }
3592 : :
3593 : :
3594 : : static void
3595 : 0 : print_hash_info (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr, size_t shstrndx,
3596 : : uint_fast32_t maxlength, Elf32_Word nbucket,
3597 : : uint_fast32_t nsyms, uint32_t *lengths, const char *extrastr)
3598 : : {
3599 : 0 : uint32_t *counts = xcalloc (maxlength + 1, sizeof (uint32_t));
3600 : :
3601 [ # # ]: 0 : for (Elf32_Word cnt = 0; cnt < nbucket; ++cnt)
3602 : 0 : ++counts[lengths[cnt]];
3603 : :
3604 : 0 : GElf_Shdr glink_mem;
3605 : 0 : GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf,
3606 : 0 : shdr->sh_link),
3607 : : &glink_mem);
3608 [ # # ]: 0 : if (glink == NULL)
3609 : : {
3610 : 0 : error (0, 0, _("invalid sh_link value in section %zu"),
3611 : : elf_ndxscn (scn));
3612 : 0 : free (counts);
3613 : 0 : return;
3614 : : }
3615 : :
3616 [ # # ]: 0 : printf (ngettext ("\
3617 : : \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",
3618 : : "\
3619 : : \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",
3620 : : nbucket),
3621 : 0 : (unsigned int) elf_ndxscn (scn),
3622 : 0 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
3623 : : (int) nbucket,
3624 : 0 : gelf_getclass (ebl->elf) == ELFCLASS32 ? 10 : 18,
3625 : : shdr->sh_addr,
3626 : : shdr->sh_offset,
3627 : 0 : (unsigned int) shdr->sh_link,
3628 : 0 : elf_strptr (ebl->elf, shstrndx, glink->sh_name));
3629 : :
3630 [ # # ]: 0 : if (extrastr != NULL)
3631 : 0 : fputs (extrastr, stdout);
3632 : :
3633 [ # # ]: 0 : if (likely (nbucket > 0))
3634 : : {
3635 : 0 : uint64_t success = 0;
3636 : :
3637 : : /* xgettext:no-c-format */
3638 : 0 : fputs (_("\
3639 : : Length Number % of total Coverage\n"), stdout);
3640 : 0 : printf (_(" 0 %6" PRIu32 " %5.1f%%\n"),
3641 : 0 : counts[0], (counts[0] * 100.0) / nbucket);
3642 : :
3643 : 0 : uint64_t nzero_counts = 0;
3644 [ # # ]: 0 : for (Elf32_Word cnt = 1; cnt <= maxlength; ++cnt)
3645 : : {
3646 : 0 : nzero_counts += counts[cnt] * cnt;
3647 : 0 : printf (_("\
3648 : : %7d %6" PRIu32 " %5.1f%% %5.1f%%\n"),
3649 : 0 : (int) cnt, counts[cnt], (counts[cnt] * 100.0) / nbucket,
3650 : 0 : (nzero_counts * 100.0) / nsyms);
3651 : : }
3652 : :
3653 : : Elf32_Word acc = 0;
3654 [ # # ]: 0 : for (Elf32_Word cnt = 1; cnt <= maxlength; ++cnt)
3655 : : {
3656 : 0 : acc += cnt;
3657 : 0 : success += counts[cnt] * acc;
3658 : : }
3659 : :
3660 [ # # ]: 0 : if (nzero_counts > 0)
3661 : 0 : printf (_("\
3662 : : Average number of tests: successful lookup: %f\n\
3663 : : unsuccessful lookup: %f\n"),
3664 : 0 : (double) success / (double) nzero_counts,
3665 : 0 : (double) nzero_counts / (double) nbucket);
3666 : : }
3667 : :
3668 : 0 : free (counts);
3669 : : }
3670 : :
3671 : :
3672 : : /* This function handles the traditional System V-style hash table format. */
3673 : : static void
3674 : 0 : handle_sysv_hash (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr, size_t shstrndx)
3675 : : {
3676 : 0 : Elf_Data *data = elf_getdata (scn, NULL);
3677 [ # # ]: 0 : if (unlikely (data == NULL))
3678 : : {
3679 : 0 : error (0, 0, _("cannot get data for section %d: %s"),
3680 : 0 : (int) elf_ndxscn (scn), elf_errmsg (-1));
3681 : 0 : return;
3682 : : }
3683 : :
3684 [ # # ]: 0 : if (unlikely (data->d_size < 2 * sizeof (Elf32_Word)))
3685 : : {
3686 : 0 : invalid_data:
3687 : 0 : error (0, 0, _("invalid data in sysv.hash section %d"),
3688 : 0 : (int) elf_ndxscn (scn));
3689 : 0 : return;
3690 : : }
3691 : :
3692 : 0 : Elf32_Word nbucket = ((Elf32_Word *) data->d_buf)[0];
3693 : 0 : Elf32_Word nchain = ((Elf32_Word *) data->d_buf)[1];
3694 : :
3695 : 0 : uint64_t used_buf = (2ULL + nchain + nbucket) * sizeof (Elf32_Word);
3696 [ # # ]: 0 : if (used_buf > data->d_size)
3697 : 0 : goto invalid_data;
3698 : :
3699 : 0 : Elf32_Word *bucket = &((Elf32_Word *) data->d_buf)[2];
3700 : 0 : Elf32_Word *chain = &((Elf32_Word *) data->d_buf)[2 + nbucket];
3701 : :
3702 : 0 : uint32_t *lengths = xcalloc (nbucket, sizeof (uint32_t));
3703 : :
3704 : 0 : uint_fast32_t maxlength = 0;
3705 : 0 : uint_fast32_t nsyms = 0;
3706 [ # # ]: 0 : for (Elf32_Word cnt = 0; cnt < nbucket; ++cnt)
3707 : : {
3708 : 0 : Elf32_Word inner = bucket[cnt];
3709 : 0 : Elf32_Word chain_len = 0;
3710 [ # # ]: 0 : while (inner > 0 && inner < nchain)
3711 : : {
3712 : 0 : ++nsyms;
3713 : 0 : ++chain_len;
3714 [ # # ]: 0 : if (chain_len > nchain)
3715 : : {
3716 : 0 : error (0, 0, _("invalid chain in sysv.hash section %d"),
3717 : 0 : (int) elf_ndxscn (scn));
3718 : 0 : free (lengths);
3719 : 0 : return;
3720 : : }
3721 [ # # ]: 0 : if (maxlength < ++lengths[cnt])
3722 : 0 : ++maxlength;
3723 : :
3724 : 0 : inner = chain[inner];
3725 : : }
3726 : : }
3727 : :
3728 : 0 : print_hash_info (ebl, scn, shdr, shstrndx, maxlength, nbucket, nsyms,
3729 : : lengths, NULL);
3730 : :
3731 : 0 : free (lengths);
3732 : : }
3733 : :
3734 : :
3735 : : /* This function handles the incorrect, System V-style hash table
3736 : : format some 64-bit architectures use. */
3737 : : static void
3738 : 0 : handle_sysv_hash64 (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr, size_t shstrndx)
3739 : : {
3740 : 0 : Elf_Data *data = elf_getdata (scn, NULL);
3741 [ # # ]: 0 : if (unlikely (data == NULL))
3742 : : {
3743 : 0 : error (0, 0, _("cannot get data for section %d: %s"),
3744 : 0 : (int) elf_ndxscn (scn), elf_errmsg (-1));
3745 : 0 : return;
3746 : : }
3747 : :
3748 [ # # ]: 0 : if (unlikely (data->d_size < 2 * sizeof (Elf64_Xword)))
3749 : : {
3750 : 0 : invalid_data:
3751 : 0 : error (0, 0, _("invalid data in sysv.hash64 section %d"),
3752 : 0 : (int) elf_ndxscn (scn));
3753 : 0 : return;
3754 : : }
3755 : :
3756 : 0 : Elf64_Xword nbucket = ((Elf64_Xword *) data->d_buf)[0];
3757 : 0 : Elf64_Xword nchain = ((Elf64_Xword *) data->d_buf)[1];
3758 : :
3759 : 0 : uint64_t maxwords = data->d_size / sizeof (Elf64_Xword);
3760 [ # # ]: 0 : if (maxwords < 2
3761 [ # # ]: 0 : || maxwords - 2 < nbucket
3762 [ # # ]: 0 : || maxwords - 2 - nbucket < nchain)
3763 : 0 : goto invalid_data;
3764 : :
3765 : 0 : Elf64_Xword *bucket = &((Elf64_Xword *) data->d_buf)[2];
3766 : 0 : Elf64_Xword *chain = &((Elf64_Xword *) data->d_buf)[2 + nbucket];
3767 : :
3768 : 0 : uint32_t *lengths = xcalloc (nbucket, sizeof (uint32_t));
3769 : :
3770 : 0 : uint_fast32_t maxlength = 0;
3771 : 0 : uint_fast32_t nsyms = 0;
3772 [ # # ]: 0 : for (Elf64_Xword cnt = 0; cnt < nbucket; ++cnt)
3773 : : {
3774 : 0 : Elf64_Xword inner = bucket[cnt];
3775 : 0 : Elf64_Xword chain_len = 0;
3776 [ # # ]: 0 : while (inner > 0 && inner < nchain)
3777 : : {
3778 : 0 : ++nsyms;
3779 : 0 : ++chain_len;
3780 [ # # ]: 0 : if (chain_len > nchain)
3781 : : {
3782 : 0 : error (0, 0, _("invalid chain in sysv.hash64 section %d"),
3783 : 0 : (int) elf_ndxscn (scn));
3784 : 0 : free (lengths);
3785 : 0 : return;
3786 : : }
3787 [ # # ]: 0 : if (maxlength < ++lengths[cnt])
3788 : 0 : ++maxlength;
3789 : :
3790 : 0 : inner = chain[inner];
3791 : : }
3792 : : }
3793 : :
3794 : 0 : print_hash_info (ebl, scn, shdr, shstrndx, maxlength, nbucket, nsyms,
3795 : : lengths, NULL);
3796 : :
3797 : 0 : free (lengths);
3798 : : }
3799 : :
3800 : :
3801 : : /* This function handles the GNU-style hash table format. */
3802 : : static void
3803 : 0 : handle_gnu_hash (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr, size_t shstrndx)
3804 : : {
3805 : 0 : uint32_t *lengths = NULL;
3806 : 0 : Elf_Data *data = elf_getdata (scn, NULL);
3807 [ # # ]: 0 : if (unlikely (data == NULL))
3808 : : {
3809 : 0 : error (0, 0, _("cannot get data for section %d: %s"),
3810 : 0 : (int) elf_ndxscn (scn), elf_errmsg (-1));
3811 : 0 : return;
3812 : : }
3813 : :
3814 [ # # ]: 0 : if (unlikely (data->d_size < 4 * sizeof (Elf32_Word)))
3815 : : {
3816 : 0 : invalid_data:
3817 : 0 : free (lengths);
3818 : 0 : error (0, 0, _("invalid data in gnu.hash section %d"),
3819 : 0 : (int) elf_ndxscn (scn));
3820 : 0 : return;
3821 : : }
3822 : :
3823 : 0 : Elf32_Word nbucket = ((Elf32_Word *) data->d_buf)[0];
3824 : 0 : Elf32_Word symbias = ((Elf32_Word *) data->d_buf)[1];
3825 : :
3826 : : /* Next comes the size of the bitmap. It's measured in words for
3827 : : the architecture. It's 32 bits for 32 bit archs, and 64 bits for
3828 : : 64 bit archs. There is always a bloom filter present, so zero is
3829 : : an invalid value. */
3830 : 0 : Elf32_Word bitmask_words = ((Elf32_Word *) data->d_buf)[2];
3831 [ # # ]: 0 : if (gelf_getclass (ebl->elf) == ELFCLASS64)
3832 : 0 : bitmask_words *= 2;
3833 : :
3834 [ # # ]: 0 : if (bitmask_words == 0)
3835 : 0 : goto invalid_data;
3836 : :
3837 : 0 : Elf32_Word shift = ((Elf32_Word *) data->d_buf)[3];
3838 : :
3839 : : /* Is there still room for the sym chain?
3840 : : Use uint64_t calculation to prevent 32bit overflow. */
3841 : 0 : uint64_t used_buf = (4ULL + bitmask_words + nbucket) * sizeof (Elf32_Word);
3842 : 0 : uint32_t max_nsyms = (data->d_size - used_buf) / sizeof (Elf32_Word);
3843 [ # # ]: 0 : if (used_buf > data->d_size)
3844 : 0 : goto invalid_data;
3845 : :
3846 : 0 : lengths = xcalloc (nbucket, sizeof (uint32_t));
3847 : :
3848 : 0 : Elf32_Word *bitmask = &((Elf32_Word *) data->d_buf)[4];
3849 : 0 : Elf32_Word *bucket = &((Elf32_Word *) data->d_buf)[4 + bitmask_words];
3850 : 0 : Elf32_Word *chain = &((Elf32_Word *) data->d_buf)[4 + bitmask_words
3851 : 0 : + nbucket];
3852 : :
3853 : : /* Compute distribution of chain lengths. */
3854 : 0 : uint_fast32_t maxlength = 0;
3855 : 0 : uint_fast32_t nsyms = 0;
3856 [ # # ]: 0 : for (Elf32_Word cnt = 0; cnt < nbucket; ++cnt)
3857 [ # # ]: 0 : if (bucket[cnt] != 0)
3858 : : {
3859 : 0 : Elf32_Word inner = bucket[cnt] - symbias;
3860 : 0 : do
3861 : : {
3862 : 0 : ++nsyms;
3863 [ # # ]: 0 : if (maxlength < ++lengths[cnt])
3864 : 0 : ++maxlength;
3865 [ # # ]: 0 : if (inner >= max_nsyms)
3866 : 0 : goto invalid_data;
3867 : : }
3868 [ # # ]: 0 : while ((chain[inner++] & 1) == 0);
3869 : : }
3870 : :
3871 : : /* Count bits in bitmask. */
3872 : : uint_fast32_t nbits = 0;
3873 [ # # ]: 0 : for (Elf32_Word cnt = 0; cnt < bitmask_words; ++cnt)
3874 : : {
3875 : 0 : uint_fast32_t word = bitmask[cnt];
3876 : :
3877 : 0 : word = (word & 0x55555555) + ((word >> 1) & 0x55555555);
3878 : 0 : word = (word & 0x33333333) + ((word >> 2) & 0x33333333);
3879 : 0 : word = (word & 0x0f0f0f0f) + ((word >> 4) & 0x0f0f0f0f);
3880 : 0 : word = (word & 0x00ff00ff) + ((word >> 8) & 0x00ff00ff);
3881 : 0 : nbits += (word & 0x0000ffff) + ((word >> 16) & 0x0000ffff);
3882 : : }
3883 : :
3884 : 0 : char *str = xasprintf (_("\
3885 : : Symbol Bias: %u\n\
3886 : : Bitmask Size: %zu bytes %" PRIuFAST32 "%% bits set 2nd hash shift: %u\n"),
3887 : : (unsigned int) symbias,
3888 : : bitmask_words * sizeof (Elf32_Word),
3889 : 0 : ((nbits * 100 + 50)
3890 : 0 : / (uint_fast32_t) (bitmask_words
3891 : : * sizeof (Elf32_Word) * 8)),
3892 : : (unsigned int) shift);
3893 : :
3894 : 0 : print_hash_info (ebl, scn, shdr, shstrndx, maxlength, nbucket, nsyms,
3895 : : lengths, str);
3896 : :
3897 : 0 : free (str);
3898 : 0 : free (lengths);
3899 : : }
3900 : :
3901 : :
3902 : : /* Find the symbol table(s). For this we have to search through the
3903 : : section table. */
3904 : : static void
3905 : 0 : handle_hash (Ebl *ebl)
3906 : : {
3907 : : /* Get the section header string table index. */
3908 : 0 : size_t shstrndx;
3909 [ # # ]: 0 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
3910 : 0 : error_exit (0, _("cannot get section header string table index"));
3911 : :
3912 : : Elf_Scn *scn = NULL;
3913 [ # # ]: 0 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
3914 : : {
3915 : : /* Handle the section if it is a symbol table. */
3916 : 0 : GElf_Shdr shdr_mem;
3917 : 0 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
3918 : :
3919 [ # # ]: 0 : if (likely (shdr != NULL))
3920 : : {
3921 [ # # ]: 0 : if ((shdr->sh_type == SHT_HASH || shdr->sh_type == SHT_GNU_HASH)
3922 [ # # ]: 0 : && (shdr->sh_flags & SHF_COMPRESSED) != 0)
3923 : : {
3924 [ # # ]: 0 : if (elf_compress (scn, 0, 0) < 0)
3925 : 0 : printf ("WARNING: %s [%zd]\n",
3926 : : _("Couldn't uncompress section"),
3927 : : elf_ndxscn (scn));
3928 : 0 : shdr = gelf_getshdr (scn, &shdr_mem);
3929 [ # # ]: 0 : if (unlikely (shdr == NULL))
3930 : 0 : error_exit (0, _("cannot get section [%zd] header: %s"),
3931 : : elf_ndxscn (scn), elf_errmsg (-1));
3932 : : }
3933 : :
3934 [ # # ]: 0 : if (shdr->sh_type == SHT_HASH)
3935 : : {
3936 [ # # ]: 0 : if (ebl_sysvhash_entrysize (ebl) == sizeof (Elf64_Xword))
3937 : 0 : handle_sysv_hash64 (ebl, scn, shdr, shstrndx);
3938 : : else
3939 : 0 : handle_sysv_hash (ebl, scn, shdr, shstrndx);
3940 : : }
3941 [ # # ]: 0 : else if (shdr->sh_type == SHT_GNU_HASH)
3942 : 0 : handle_gnu_hash (ebl, scn, shdr, shstrndx);
3943 : : }
3944 : : }
3945 : 0 : }
3946 : :
3947 : :
3948 : : static void
3949 : 0 : print_liblist (Ebl *ebl)
3950 : : {
3951 : : /* Find the library list sections. For this we have to search
3952 : : through the section table. */
3953 : 0 : Elf_Scn *scn = NULL;
3954 : :
3955 : : /* Get the section header string table index. */
3956 : 0 : size_t shstrndx;
3957 [ # # ]: 0 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
3958 : 0 : error_exit (0, _("cannot get section header string table index"));
3959 : :
3960 [ # # ]: 0 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
3961 : : {
3962 : 0 : GElf_Shdr shdr_mem;
3963 : 0 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
3964 : :
3965 [ # # # # ]: 0 : if (shdr != NULL && shdr->sh_type == SHT_GNU_LIBLIST)
3966 : : {
3967 : 0 : size_t sh_entsize = gelf_fsize (ebl->elf, ELF_T_LIB, 1, EV_CURRENT);
3968 : 0 : int nentries = shdr->sh_size / sh_entsize;
3969 : 0 : printf (ngettext ("\
3970 : : \nLibrary list section [%2zu] '%s' at offset %#0" PRIx64 " contains %d entry:\n",
3971 : : "\
3972 : : \nLibrary list section [%2zu] '%s' at offset %#0" PRIx64 " contains %d entries:\n",
3973 : : nentries),
3974 : : elf_ndxscn (scn),
3975 : 0 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
3976 : : shdr->sh_offset,
3977 : : nentries);
3978 : :
3979 : 0 : Elf_Data *data = elf_getdata (scn, NULL);
3980 [ # # ]: 0 : if (data == NULL)
3981 : 0 : return;
3982 : :
3983 : 0 : puts (_("\
3984 : : Library Time Stamp Checksum Version Flags"));
3985 : :
3986 [ # # ]: 0 : for (int cnt = 0; cnt < nentries; ++cnt)
3987 : : {
3988 : 0 : GElf_Lib lib_mem;
3989 : 0 : GElf_Lib *lib = gelf_getlib (data, cnt, &lib_mem);
3990 [ # # ]: 0 : if (unlikely (lib == NULL))
3991 : 0 : continue;
3992 : :
3993 : 0 : time_t t = (time_t) lib->l_time_stamp;
3994 : 0 : struct tm *tm = gmtime (&t);
3995 [ # # ]: 0 : if (unlikely (tm == NULL))
3996 : 0 : continue;
3997 : :
3998 : 0 : printf (" [%2d] %-29s %04u-%02u-%02uT%02u:%02u:%02u %08x %-7u %u\n",
3999 : 0 : cnt, elf_strptr (ebl->elf, shdr->sh_link, lib->l_name),
4000 : 0 : tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
4001 : : tm->tm_hour, tm->tm_min, tm->tm_sec,
4002 : 0 : (unsigned int) lib->l_checksum,
4003 : 0 : (unsigned int) lib->l_version,
4004 : 0 : (unsigned int) lib->l_flags);
4005 : : }
4006 : : }
4007 : : }
4008 : : }
4009 : :
4010 : : static inline size_t
4011 : 0 : left (Elf_Data *data,
4012 : : const unsigned char *p)
4013 : : {
4014 : 0 : return (const unsigned char *) data->d_buf + data->d_size - p;
4015 : : }
4016 : :
4017 : : static void
4018 : 0 : print_attributes (Ebl *ebl, const GElf_Ehdr *ehdr)
4019 : : {
4020 : : /* Find the object attributes sections. For this we have to search
4021 : : through the section table. */
4022 : 0 : Elf_Scn *scn = NULL;
4023 : :
4024 : : /* Get the section header string table index. */
4025 : 0 : size_t shstrndx;
4026 [ # # ]: 0 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
4027 : 0 : error_exit (0, _("cannot get section header string table index"));
4028 : :
4029 [ # # ]: 0 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
4030 : : {
4031 : 0 : GElf_Shdr shdr_mem;
4032 : 0 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
4033 : :
4034 [ # # # # ]: 0 : if (shdr == NULL || (shdr->sh_type != SHT_GNU_ATTRIBUTES
4035 [ # # ]: 0 : && (shdr->sh_type != SHT_ARM_ATTRIBUTES
4036 [ # # ]: 0 : || ehdr->e_machine != EM_ARM)
4037 [ # # ]: 0 : && (shdr->sh_type != SHT_CSKY_ATTRIBUTES
4038 [ # # ]: 0 : || ehdr->e_machine != EM_CSKY)
4039 [ # # ]: 0 : && (shdr->sh_type != SHT_RISCV_ATTRIBUTES
4040 [ # # ]: 0 : || ehdr->e_machine != EM_RISCV)))
4041 : 0 : continue;
4042 : :
4043 : 0 : printf (_("\
4044 : : \nObject attributes section [%2zu] '%s' of %" PRIu64
4045 : : " bytes at offset %#0" PRIx64 ":\n"),
4046 : : elf_ndxscn (scn),
4047 : 0 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
4048 : : shdr->sh_size, shdr->sh_offset);
4049 : :
4050 : 0 : Elf_Data *data = elf_rawdata (scn, NULL);
4051 [ # # # # ]: 0 : if (unlikely (data == NULL || data->d_size == 0))
4052 : 0 : return;
4053 : :
4054 : 0 : const unsigned char *p = data->d_buf;
4055 : :
4056 : : /* There is only one 'version', A. */
4057 [ # # ]: 0 : if (unlikely (*p++ != 'A'))
4058 : : return;
4059 : :
4060 : 0 : fputs (_(" Owner Size\n"), stdout);
4061 : :
4062 : : /* Loop over the sections. */
4063 [ # # ]: 0 : while (left (data, p) >= 4)
4064 : : {
4065 : : /* Section length. */
4066 : 0 : uint32_t len;
4067 [ # # ]: 0 : memcpy (&len, p, sizeof len);
4068 : :
4069 [ # # ]: 0 : if (MY_ELFDATA != ehdr->e_ident[EI_DATA])
4070 : 0 : CONVERT (len);
4071 : :
4072 [ # # ]: 0 : if (unlikely (len > left (data, p)))
4073 : : break;
4074 : :
4075 : : /* Section vendor name. */
4076 : 0 : const unsigned char *name = p + sizeof len;
4077 : 0 : p += len;
4078 : :
4079 : 0 : unsigned const char *q = memchr (name, '\0', len);
4080 [ # # ]: 0 : if (unlikely (q == NULL))
4081 : : break;
4082 : 0 : ++q;
4083 : :
4084 : 0 : printf (_(" %-13s %4" PRIu32 "\n"), name, len);
4085 : :
4086 : 0 : bool gnu_vendor = (q - name == sizeof "gnu"
4087 [ # # # # ]: 0 : && !memcmp (name, "gnu", sizeof "gnu"));
4088 : :
4089 : : /* Loop over subsections. */
4090 [ # # ]: 0 : if (shdr->sh_type != SHT_GNU_ATTRIBUTES
4091 [ # # ]: 0 : || gnu_vendor)
4092 [ # # ]: 0 : while (q < p)
4093 : : {
4094 : 0 : const unsigned char *const sub = q;
4095 : :
4096 : 0 : unsigned int subsection_tag;
4097 : 0 : get_uleb128 (subsection_tag, q, p);
4098 [ # # ]: 0 : if (unlikely (q >= p))
4099 : : break;
4100 : :
4101 : 0 : uint32_t subsection_len;
4102 [ # # ]: 0 : if (unlikely (p - sub < (ptrdiff_t) sizeof subsection_len))
4103 : : break;
4104 : :
4105 [ # # ]: 0 : memcpy (&subsection_len, q, sizeof subsection_len);
4106 : :
4107 [ # # ]: 0 : if (MY_ELFDATA != ehdr->e_ident[EI_DATA])
4108 : 0 : CONVERT (subsection_len);
4109 : :
4110 : : /* Don't overflow, ptrdiff_t might be 32bits, but signed. */
4111 [ # # # # ]: 0 : if (unlikely (subsection_len == 0
4112 : : || subsection_len >= (uint32_t) PTRDIFF_MAX
4113 : : || p - sub < (ptrdiff_t) subsection_len))
4114 : : break;
4115 : :
4116 : 0 : const unsigned char *r = q + sizeof subsection_len;
4117 : 0 : q = sub + subsection_len;
4118 : :
4119 [ # # ]: 0 : switch (subsection_tag)
4120 : : {
4121 : 0 : default:
4122 : : /* Unknown subsection, print and skip. */
4123 : 0 : printf (_(" %-4u %12" PRIu32 "\n"),
4124 : : subsection_tag, subsection_len);
4125 : : break;
4126 : :
4127 : 0 : case 1: /* Tag_File */
4128 : 0 : printf (_(" File: %11" PRIu32 "\n"),
4129 : : subsection_len);
4130 : :
4131 [ # # ]: 0 : while (r < q)
4132 : : {
4133 : 0 : unsigned int tag;
4134 : 0 : get_uleb128 (tag, r, q);
4135 [ # # ]: 0 : if (unlikely (r >= q))
4136 : : break;
4137 : :
4138 : : /* GNU style tags have either a uleb128 value,
4139 : : when lowest bit is not set, or a string
4140 : : when the lowest bit is set.
4141 : : "compatibility" (32) is special. It has
4142 : : both a string and a uleb128 value. For
4143 : : non-gnu we assume 6 till 31 only take ints.
4144 : : XXX see arm backend, do we need a separate
4145 : : hook? */
4146 : 0 : uint64_t value = 0;
4147 : 0 : const char *string = NULL;
4148 [ # # # # ]: 0 : if (tag == 32 || (tag & 1) == 0
4149 [ # # # # ]: 0 : || (! gnu_vendor && (tag > 5 && tag < 32)))
4150 : : {
4151 : : // Note r >= q check above.
4152 : 0 : get_uleb128 (value, r, q);
4153 [ # # ]: 0 : if (r > q)
4154 : : break;
4155 : : }
4156 [ # # ]: 0 : if (tag == 32
4157 [ # # ]: 0 : || ((tag & 1) != 0
4158 [ # # ]: 0 : && (gnu_vendor
4159 [ # # ]: 0 : || (! gnu_vendor && tag > 32)))
4160 [ # # # # ]: 0 : || (! gnu_vendor && tag > 3 && tag < 6))
4161 : : {
4162 : 0 : string = (const char *) r;
4163 : 0 : r = memchr (r, '\0', q - r);
4164 [ # # ]: 0 : if (r == NULL)
4165 : : break;
4166 : 0 : ++r;
4167 : : }
4168 : :
4169 : 0 : const char *tag_name = NULL;
4170 : 0 : const char *value_name = NULL;
4171 : 0 : ebl_check_object_attribute (ebl, (const char *) name,
4172 : : tag, value,
4173 : : &tag_name, &value_name);
4174 : :
4175 [ # # ]: 0 : if (tag_name != NULL)
4176 : : {
4177 [ # # ]: 0 : if (tag == 32)
4178 : 0 : printf (_(" %s: %" PRId64 ", %s\n"),
4179 : : tag_name, value, string);
4180 [ # # # # ]: 0 : else if (string == NULL && value_name == NULL)
4181 : 0 : printf (_(" %s: %" PRId64 "\n"),
4182 : : tag_name, value);
4183 : : else
4184 : 0 : printf (_(" %s: %s\n"),
4185 : : tag_name, string ?: value_name);
4186 : : }
4187 : : else
4188 : : {
4189 : : /* For "gnu" vendor 32 "compatibility" has
4190 : : already been handled above. */
4191 [ # # # # ]: 0 : assert (tag != 32
4192 : : || strcmp ((const char *) name, "gnu"));
4193 [ # # ]: 0 : if (string == NULL)
4194 : 0 : printf (_(" %u: %" PRId64 "\n"),
4195 : : tag, value);
4196 : : else
4197 : 0 : printf (_(" %u: %s\n"),
4198 : : tag, string);
4199 : : }
4200 : : }
4201 : : }
4202 : : }
4203 : : }
4204 : : }
4205 : : }
4206 : :
4207 : : /* Returns either the (relocated) data from the Dwarf, or tries to get
4208 : : the "raw" (uncompressed) data from the Elf section. Produces a
4209 : : warning if the data cannot be found (or decompressed). */
4210 : : static Elf_Data *
4211 : 0 : get_debug_elf_data (Dwarf *dbg, Ebl *ebl, int idx, Elf_Scn *scn)
4212 : : {
4213 : : /* We prefer to get the section data from the Dwarf because that
4214 : : might have been relocated already. Note this is subtly wrong if
4215 : : there are multiple sections with the same .debug name. */
4216 [ # # ]: 0 : if (dbg->sectiondata[idx] != NULL)
4217 : : return dbg->sectiondata[idx];
4218 : :
4219 : 0 : GElf_Shdr shdr_mem;
4220 : 0 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
4221 [ # # # # ]: 0 : if (shdr != NULL && (shdr->sh_flags & SHF_COMPRESSED) != 0)
4222 : : {
4223 [ # # ]: 0 : if (elf_compress (scn, 0, 0) < 0)
4224 : : {
4225 : 0 : error (0, 0, "%s [%zd] '%s'\n",
4226 : : _("Couldn't uncompress section"),
4227 : : elf_ndxscn (scn), section_name (ebl, shdr));
4228 : 0 : return NULL;
4229 : : }
4230 : : }
4231 : :
4232 : 0 : Elf_Data *data = elf_getdata (scn, NULL);
4233 [ # # ]: 0 : if (data == NULL)
4234 : 0 : error (0, 0, "%s [%zd] '%s': %s\n",
4235 : : _("Couldn't get data from section"),
4236 : : elf_ndxscn (scn), section_name (ebl, shdr), elf_errmsg (-1));
4237 : :
4238 : 0 : return elf_getdata (scn, NULL);
4239 : : }
4240 : :
4241 : : static void
4242 : 0 : print_dwarf_addr (Dwfl_Module *dwflmod,
4243 : : int address_size, Dwarf_Addr address, Dwarf_Addr raw)
4244 : : {
4245 : : /* See if there is a name we can give for this address. */
4246 : 0 : GElf_Sym sym;
4247 : 0 : GElf_Off off = 0;
4248 [ # # ]: 0 : const char *name = (print_address_names && ! print_unresolved_addresses)
4249 : 0 : ? dwfl_module_addrinfo (dwflmod, address, &off, &sym, NULL, NULL, NULL)
4250 [ # # ]: 0 : : NULL;
4251 : :
4252 : 0 : const char *scn;
4253 [ # # ]: 0 : if (print_unresolved_addresses)
4254 : : {
4255 : 0 : address = raw;
4256 : 0 : scn = NULL;
4257 : : }
4258 : : else
4259 : : {
4260 : : /* Relativize the address. */
4261 : 0 : int n = dwfl_module_relocations (dwflmod);
4262 [ # # ]: 0 : int i = n < 1 ? -1 : dwfl_module_relocate_address (dwflmod, &address);
4263 : :
4264 : : /* In an ET_REL file there is a section name to refer to. */
4265 : 0 : scn = (i < 0 ? NULL
4266 [ # # ]: 0 : : dwfl_module_relocation_info (dwflmod, i, NULL));
4267 : : }
4268 : :
4269 [ # # ]: 0 : if ((name != NULL
4270 : 0 : ? (off != 0
4271 : : ? (scn != NULL
4272 : : ? (address_size == 0
4273 : 0 : ? printf ("%s+%#" PRIx64 " <%s+%#" PRIx64 ">",
4274 : : scn, address, name, off)
4275 : 0 : : printf ("%s+%#0*" PRIx64 " <%s+%#" PRIx64 ">",
4276 : 0 : scn, 2 + address_size * 2, address,
4277 : : name, off))
4278 : : : (address_size == 0
4279 : 0 : ? printf ("%#" PRIx64 " <%s+%#" PRIx64 ">",
4280 : : address, name, off)
4281 : 0 : : printf ("%#0*" PRIx64 " <%s+%#" PRIx64 ">",
4282 : 0 : 2 + address_size * 2, address,
4283 : : name, off)))
4284 : : : (scn != NULL
4285 : : ? (address_size == 0
4286 : 0 : ? printf ("%s+%#" PRIx64 " <%s>", scn, address, name)
4287 : 0 : : printf ("%s+%#0*" PRIx64 " <%s>",
4288 : 0 : scn, 2 + address_size * 2, address, name))
4289 : : : (address_size == 0
4290 : 0 : ? printf ("%#" PRIx64 " <%s>", address, name)
4291 : 0 : : printf ("%#0*" PRIx64 " <%s>",
4292 : 0 : 2 + address_size * 2, address, name))))
4293 : : : (scn != NULL
4294 : : ? (address_size == 0
4295 : 0 : ? printf ("%s+%#" PRIx64, scn, address)
4296 : 0 : : printf ("%s+%#0*" PRIx64, scn, 2 + address_size * 2, address))
4297 : : : (address_size == 0
4298 : 0 : ? printf ("%#" PRIx64, address)
4299 [ # # # # : 0 : : printf ("%#0*" PRIx64, 2 + address_size * 2, address)))) < 0)
# # # # #
# # # # #
# # # # #
# # # ]
4300 : 0 : error_exit (0, _("sprintf failure"));
4301 : 0 : }
4302 : :
4303 : :
4304 : : static const char *
4305 : 0 : dwarf_tag_string (unsigned int tag)
4306 : : {
4307 [ # # # # : 0 : switch (tag)
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# ]
4308 : : {
4309 : : #define DWARF_ONE_KNOWN_DW_TAG(NAME, CODE) case CODE: return #NAME;
4310 : 0 : DWARF_ALL_KNOWN_DW_TAG
4311 : : #undef DWARF_ONE_KNOWN_DW_TAG
4312 : 0 : default:
4313 : 0 : return NULL;
4314 : : }
4315 : : }
4316 : :
4317 : :
4318 : : static const char *
4319 : 0 : dwarf_attr_string (unsigned int attrnum)
4320 : : {
4321 [ # # # # : 0 : switch (attrnum)
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # ]
4322 : : {
4323 : : #define DWARF_ONE_KNOWN_DW_AT(NAME, CODE) case CODE: return #NAME;
4324 : 0 : DWARF_ALL_KNOWN_DW_AT
4325 : : #undef DWARF_ONE_KNOWN_DW_AT
4326 : 0 : default:
4327 : 0 : return NULL;
4328 : : }
4329 : : }
4330 : :
4331 : :
4332 : : static const char *
4333 : 0 : dwarf_form_string (unsigned int form)
4334 : : {
4335 [ # # # # : 0 : switch (form)
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # ]
4336 : : {
4337 : : #define DWARF_ONE_KNOWN_DW_FORM(NAME, CODE) case CODE: return #NAME;
4338 : 0 : DWARF_ALL_KNOWN_DW_FORM
4339 : : #undef DWARF_ONE_KNOWN_DW_FORM
4340 : 0 : default:
4341 : 0 : return NULL;
4342 : : }
4343 : : }
4344 : :
4345 : :
4346 : : static const char *
4347 : 0 : dwarf_lang_string (unsigned int lang)
4348 : : {
4349 [ # # # # : 0 : switch (lang)
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # ]
4350 : : {
4351 : : #define DWARF_ONE_KNOWN_DW_LANG(NAME, CODE) case CODE: return #NAME;
4352 : 0 : DWARF_ALL_KNOWN_DW_LANG
4353 : : #undef DWARF_ONE_KNOWN_DW_LANG
4354 : 0 : default:
4355 : 0 : return NULL;
4356 : : }
4357 : : }
4358 : :
4359 : :
4360 : : static const char *
4361 : 0 : dwarf_inline_string (unsigned int code)
4362 : : {
4363 : 0 : static const char *const known[] =
4364 : : {
4365 : : #define DWARF_ONE_KNOWN_DW_INL(NAME, CODE) [CODE] = #NAME,
4366 : : DWARF_ALL_KNOWN_DW_INL
4367 : : #undef DWARF_ONE_KNOWN_DW_INL
4368 : : };
4369 : :
4370 : 0 : if (likely (code < sizeof (known) / sizeof (known[0])))
4371 : 0 : return known[code];
4372 : :
4373 : : return NULL;
4374 : : }
4375 : :
4376 : :
4377 : : static const char *
4378 : 0 : dwarf_encoding_string (unsigned int code)
4379 : : {
4380 : 0 : static const char *const known[] =
4381 : : {
4382 : : #define DWARF_ONE_KNOWN_DW_ATE(NAME, CODE) [CODE] = #NAME,
4383 : : DWARF_ALL_KNOWN_DW_ATE
4384 : : #undef DWARF_ONE_KNOWN_DW_ATE
4385 : : };
4386 : :
4387 : 0 : if (likely (code < sizeof (known) / sizeof (known[0])))
4388 : 0 : return known[code];
4389 : :
4390 : : return NULL;
4391 : : }
4392 : :
4393 : :
4394 : : static const char *
4395 : 0 : dwarf_access_string (unsigned int code)
4396 : : {
4397 : 0 : static const char *const known[] =
4398 : : {
4399 : : #define DWARF_ONE_KNOWN_DW_ACCESS(NAME, CODE) [CODE] = #NAME,
4400 : : DWARF_ALL_KNOWN_DW_ACCESS
4401 : : #undef DWARF_ONE_KNOWN_DW_ACCESS
4402 : : };
4403 : :
4404 : 0 : if (likely (code < sizeof (known) / sizeof (known[0])))
4405 : 0 : return known[code];
4406 : :
4407 : : return NULL;
4408 : : }
4409 : :
4410 : :
4411 : : static const char *
4412 : 0 : dwarf_defaulted_string (unsigned int code)
4413 : : {
4414 : 0 : static const char *const known[] =
4415 : : {
4416 : : #define DWARF_ONE_KNOWN_DW_DEFAULTED(NAME, CODE) [CODE] = #NAME,
4417 : : DWARF_ALL_KNOWN_DW_DEFAULTED
4418 : : #undef DWARF_ONE_KNOWN_DW_DEFAULTED
4419 : : };
4420 : :
4421 : 0 : if (likely (code < sizeof (known) / sizeof (known[0])))
4422 : 0 : return known[code];
4423 : :
4424 : : return NULL;
4425 : : }
4426 : :
4427 : :
4428 : : static const char *
4429 : 0 : dwarf_visibility_string (unsigned int code)
4430 : : {
4431 : 0 : static const char *const known[] =
4432 : : {
4433 : : #define DWARF_ONE_KNOWN_DW_VIS(NAME, CODE) [CODE] = #NAME,
4434 : : DWARF_ALL_KNOWN_DW_VIS
4435 : : #undef DWARF_ONE_KNOWN_DW_VIS
4436 : : };
4437 : :
4438 : 0 : if (likely (code < sizeof (known) / sizeof (known[0])))
4439 : 0 : return known[code];
4440 : :
4441 : : return NULL;
4442 : : }
4443 : :
4444 : :
4445 : : static const char *
4446 : 0 : dwarf_virtuality_string (unsigned int code)
4447 : : {
4448 : 0 : static const char *const known[] =
4449 : : {
4450 : : #define DWARF_ONE_KNOWN_DW_VIRTUALITY(NAME, CODE) [CODE] = #NAME,
4451 : : DWARF_ALL_KNOWN_DW_VIRTUALITY
4452 : : #undef DWARF_ONE_KNOWN_DW_VIRTUALITY
4453 : : };
4454 : :
4455 : 0 : if (likely (code < sizeof (known) / sizeof (known[0])))
4456 : 0 : return known[code];
4457 : :
4458 : : return NULL;
4459 : : }
4460 : :
4461 : :
4462 : : static const char *
4463 : 0 : dwarf_identifier_case_string (unsigned int code)
4464 : : {
4465 : 0 : static const char *const known[] =
4466 : : {
4467 : : #define DWARF_ONE_KNOWN_DW_ID(NAME, CODE) [CODE] = #NAME,
4468 : : DWARF_ALL_KNOWN_DW_ID
4469 : : #undef DWARF_ONE_KNOWN_DW_ID
4470 : : };
4471 : :
4472 : 0 : if (likely (code < sizeof (known) / sizeof (known[0])))
4473 : 0 : return known[code];
4474 : :
4475 : : return NULL;
4476 : : }
4477 : :
4478 : :
4479 : : static const char *
4480 : 0 : dwarf_calling_convention_string (unsigned int code)
4481 : : {
4482 : 0 : static const char *const known[] =
4483 : : {
4484 : : #define DWARF_ONE_KNOWN_DW_CC(NAME, CODE) [CODE] = #NAME,
4485 : : DWARF_ALL_KNOWN_DW_CC
4486 : : #undef DWARF_ONE_KNOWN_DW_CC
4487 : : };
4488 : :
4489 : 0 : if (likely (code < sizeof (known) / sizeof (known[0])))
4490 : 0 : return known[code];
4491 : :
4492 : : return NULL;
4493 : : }
4494 : :
4495 : :
4496 : : static const char *
4497 : 0 : dwarf_ordering_string (unsigned int code)
4498 : : {
4499 : 0 : static const char *const known[] =
4500 : : {
4501 : : #define DWARF_ONE_KNOWN_DW_ORD(NAME, CODE) [CODE] = #NAME,
4502 : : DWARF_ALL_KNOWN_DW_ORD
4503 : : #undef DWARF_ONE_KNOWN_DW_ORD
4504 : : };
4505 : :
4506 : 0 : if (likely (code < sizeof (known) / sizeof (known[0])))
4507 : 0 : return known[code];
4508 : :
4509 : : return NULL;
4510 : : }
4511 : :
4512 : :
4513 : : static const char *
4514 : 0 : dwarf_discr_list_string (unsigned int code)
4515 : : {
4516 : 0 : static const char *const known[] =
4517 : : {
4518 : : #define DWARF_ONE_KNOWN_DW_DSC(NAME, CODE) [CODE] = #NAME,
4519 : : DWARF_ALL_KNOWN_DW_DSC
4520 : : #undef DWARF_ONE_KNOWN_DW_DSC
4521 : : };
4522 : :
4523 : 0 : if (likely (code < sizeof (known) / sizeof (known[0])))
4524 : 0 : return known[code];
4525 : :
4526 : : return NULL;
4527 : : }
4528 : :
4529 : :
4530 : : static const char *
4531 : 0 : dwarf_locexpr_opcode_string (unsigned int code)
4532 : : {
4533 : 0 : static const char *const known[] =
4534 : : {
4535 : : /* Normally we can't afford building huge table of 64K entries,
4536 : : most of them zero, just because there are a couple defined
4537 : : values at the far end. In case of opcodes, it's OK. */
4538 : : #define DWARF_ONE_KNOWN_DW_OP(NAME, CODE) [CODE] = #NAME,
4539 : : DWARF_ALL_KNOWN_DW_OP
4540 : : #undef DWARF_ONE_KNOWN_DW_OP
4541 : : };
4542 : :
4543 : 0 : if (likely (code < sizeof (known) / sizeof (known[0])))
4544 : 0 : return known[code];
4545 : :
4546 : : return NULL;
4547 : : }
4548 : :
4549 : :
4550 : : static const char *
4551 : 0 : dwarf_unit_string (unsigned int type)
4552 : : {
4553 [ # # # # : 0 : switch (type)
# # # ]
4554 : : {
4555 : : #define DWARF_ONE_KNOWN_DW_UT(NAME, CODE) case CODE: return #NAME;
4556 : 0 : DWARF_ALL_KNOWN_DW_UT
4557 : : #undef DWARF_ONE_KNOWN_DW_UT
4558 : 0 : default:
4559 : 0 : return NULL;
4560 : : }
4561 : : }
4562 : :
4563 : :
4564 : : static const char *
4565 : 0 : dwarf_range_list_encoding_string (unsigned int kind)
4566 : : {
4567 [ # # # # : 0 : switch (kind)
# # # #
# ]
4568 : : {
4569 : : #define DWARF_ONE_KNOWN_DW_RLE(NAME, CODE) case CODE: return #NAME;
4570 : 0 : DWARF_ALL_KNOWN_DW_RLE
4571 : : #undef DWARF_ONE_KNOWN_DW_RLE
4572 : 0 : default:
4573 : 0 : return NULL;
4574 : : }
4575 : : }
4576 : :
4577 : :
4578 : : static const char *
4579 : 0 : dwarf_loc_list_encoding_string (unsigned int kind)
4580 : : {
4581 [ # # # # : 0 : switch (kind)
# # # # #
# # ]
4582 : : {
4583 : : #define DWARF_ONE_KNOWN_DW_LLE(NAME, CODE) case CODE: return #NAME;
4584 : 0 : DWARF_ALL_KNOWN_DW_LLE
4585 : : #undef DWARF_ONE_KNOWN_DW_LLE
4586 : : /* DW_LLE_GNU_view_pair is special/incompatible with default codes. */
4587 : 0 : case DW_LLE_GNU_view_pair: return "GNU_view_pair";
4588 : 0 : default:
4589 : 0 : return NULL;
4590 : : }
4591 : : }
4592 : :
4593 : :
4594 : : static const char *
4595 : 0 : dwarf_line_content_description_string (unsigned int kind)
4596 : : {
4597 [ # # # # : 0 : switch (kind)
# # ]
4598 : : {
4599 : : #define DWARF_ONE_KNOWN_DW_LNCT(NAME, CODE) case CODE: return #NAME;
4600 : 0 : DWARF_ALL_KNOWN_DW_LNCT
4601 : : #undef DWARF_ONE_KNOWN_DW_LNCT
4602 : 0 : default:
4603 : 0 : return NULL;
4604 : : }
4605 : : }
4606 : :
4607 : :
4608 : : /* Used by all dwarf_foo_name functions. */
4609 : : static const char *
4610 : 0 : string_or_unknown (const char *known, unsigned int code,
4611 : : unsigned int lo_user, unsigned int hi_user,
4612 : : bool print_unknown_num)
4613 : : {
4614 : 0 : static char unknown_buf[20];
4615 : :
4616 [ # # ]: 0 : if (likely (known != NULL))
4617 : : return known;
4618 : :
4619 [ # # # # ]: 0 : if (lo_user != 0 && code >= lo_user && code <= hi_user)
4620 : : {
4621 : 0 : snprintf (unknown_buf, sizeof unknown_buf, "lo_user+%#x",
4622 : : code - lo_user);
4623 : 0 : return unknown_buf;
4624 : : }
4625 : :
4626 [ # # ]: 0 : if (print_unknown_num)
4627 : : {
4628 : 0 : snprintf (unknown_buf, sizeof unknown_buf, "??? (%#x)", code);
4629 : 0 : return unknown_buf;
4630 : : }
4631 : :
4632 : : return "???";
4633 : : }
4634 : :
4635 : :
4636 : : static const char *
4637 : 0 : dwarf_tag_name (unsigned int tag)
4638 : : {
4639 : 0 : const char *ret = dwarf_tag_string (tag);
4640 : 0 : return string_or_unknown (ret, tag, DW_TAG_lo_user, DW_TAG_hi_user, true);
4641 : : }
4642 : :
4643 : : static const char *
4644 : 0 : dwarf_attr_name (unsigned int attr)
4645 : : {
4646 : 0 : const char *ret = dwarf_attr_string (attr);
4647 : 0 : return string_or_unknown (ret, attr, DW_AT_lo_user, DW_AT_hi_user, true);
4648 : : }
4649 : :
4650 : :
4651 : : static const char *
4652 : 0 : dwarf_form_name (unsigned int form)
4653 : : {
4654 : 0 : const char *ret = dwarf_form_string (form);
4655 : 0 : return string_or_unknown (ret, form, 0, 0, true);
4656 : : }
4657 : :
4658 : :
4659 : : static const char *
4660 : 0 : dwarf_lang_name (unsigned int lang)
4661 : : {
4662 : 0 : const char *ret = dwarf_lang_string (lang);
4663 : 0 : return string_or_unknown (ret, lang, DW_LANG_lo_user, DW_LANG_hi_user, false);
4664 : : }
4665 : :
4666 : :
4667 : : static const char *
4668 : 0 : dwarf_inline_name (unsigned int code)
4669 : : {
4670 [ # # ]: 0 : const char *ret = dwarf_inline_string (code);
4671 : 0 : return string_or_unknown (ret, code, 0, 0, false);
4672 : : }
4673 : :
4674 : :
4675 : : static const char *
4676 : 0 : dwarf_encoding_name (unsigned int code)
4677 : : {
4678 [ # # ]: 0 : const char *ret = dwarf_encoding_string (code);
4679 : 0 : return string_or_unknown (ret, code, DW_ATE_lo_user, DW_ATE_hi_user, false);
4680 : : }
4681 : :
4682 : :
4683 : : static const char *
4684 : 0 : dwarf_access_name (unsigned int code)
4685 : : {
4686 [ # # ]: 0 : const char *ret = dwarf_access_string (code);
4687 : 0 : return string_or_unknown (ret, code, 0, 0, false);
4688 : : }
4689 : :
4690 : :
4691 : : static const char *
4692 : 0 : dwarf_defaulted_name (unsigned int code)
4693 : : {
4694 [ # # ]: 0 : const char *ret = dwarf_defaulted_string (code);
4695 : 0 : return string_or_unknown (ret, code, 0, 0, false);
4696 : : }
4697 : :
4698 : :
4699 : : static const char *
4700 : 0 : dwarf_visibility_name (unsigned int code)
4701 : : {
4702 [ # # ]: 0 : const char *ret = dwarf_visibility_string (code);
4703 : 0 : return string_or_unknown (ret, code, 0, 0, false);
4704 : : }
4705 : :
4706 : :
4707 : : static const char *
4708 : 0 : dwarf_virtuality_name (unsigned int code)
4709 : : {
4710 [ # # ]: 0 : const char *ret = dwarf_virtuality_string (code);
4711 : 0 : return string_or_unknown (ret, code, 0, 0, false);
4712 : : }
4713 : :
4714 : :
4715 : : static const char *
4716 : 0 : dwarf_identifier_case_name (unsigned int code)
4717 : : {
4718 [ # # ]: 0 : const char *ret = dwarf_identifier_case_string (code);
4719 : 0 : return string_or_unknown (ret, code, 0, 0, false);
4720 : : }
4721 : :
4722 : :
4723 : : static const char *
4724 : 0 : dwarf_calling_convention_name (unsigned int code)
4725 : : {
4726 [ # # ]: 0 : const char *ret = dwarf_calling_convention_string (code);
4727 : 0 : return string_or_unknown (ret, code, DW_CC_lo_user, DW_CC_hi_user, false);
4728 : : }
4729 : :
4730 : :
4731 : : static const char *
4732 : 0 : dwarf_ordering_name (unsigned int code)
4733 : : {
4734 [ # # ]: 0 : const char *ret = dwarf_ordering_string (code);
4735 : 0 : return string_or_unknown (ret, code, 0, 0, false);
4736 : : }
4737 : :
4738 : :
4739 : : static const char *
4740 : 0 : dwarf_discr_list_name (unsigned int code)
4741 : : {
4742 [ # # ]: 0 : const char *ret = dwarf_discr_list_string (code);
4743 : 0 : return string_or_unknown (ret, code, 0, 0, false);
4744 : : }
4745 : :
4746 : :
4747 : : static const char *
4748 : 0 : dwarf_unit_name (unsigned int type)
4749 : : {
4750 : 0 : const char *ret = dwarf_unit_string (type);
4751 : 0 : return string_or_unknown (ret, type, DW_UT_lo_user, DW_UT_hi_user, true);
4752 : : }
4753 : :
4754 : :
4755 : : static const char *
4756 : 0 : dwarf_range_list_encoding_name (unsigned int kind)
4757 : : {
4758 : 0 : const char *ret = dwarf_range_list_encoding_string (kind);
4759 : 0 : return string_or_unknown (ret, kind, 0, 0, false);
4760 : : }
4761 : :
4762 : :
4763 : : static const char *
4764 : 0 : dwarf_loc_list_encoding_name (unsigned int kind)
4765 : : {
4766 : 0 : const char *ret = dwarf_loc_list_encoding_string (kind);
4767 : 0 : return string_or_unknown (ret, kind, 0, 0, false);
4768 : : }
4769 : :
4770 : :
4771 : : static const char *
4772 : 0 : dwarf_line_content_description_name (unsigned int kind)
4773 : : {
4774 : 0 : const char *ret = dwarf_line_content_description_string (kind);
4775 : 0 : return string_or_unknown (ret, kind, DW_LNCT_lo_user, DW_LNCT_hi_user,
4776 : : false);
4777 : : }
4778 : :
4779 : :
4780 : : static void
4781 : 0 : print_block (size_t n, const void *block)
4782 : : {
4783 [ # # ]: 0 : if (n == 0)
4784 : 0 : puts (_("empty block"));
4785 : : else
4786 : : {
4787 : 0 : printf (_("%zu byte block:"), n);
4788 : 0 : const unsigned char *data = block;
4789 : 0 : do
4790 : 0 : printf (" %02x", *data++);
4791 [ # # ]: 0 : while (--n > 0);
4792 : 0 : putchar ('\n');
4793 : : }
4794 : 0 : }
4795 : :
4796 : : static void
4797 : 0 : print_bytes (size_t n, const unsigned char *bytes)
4798 : : {
4799 : 0 : while (n-- > 0)
4800 : : {
4801 : 0 : printf ("%02x", *bytes++);
4802 [ # # ]: 0 : if (n > 0)
4803 [ # # ]: 0 : printf (" ");
4804 : : }
4805 : 0 : }
4806 : :
4807 : : static int
4808 : 0 : get_indexed_addr (Dwarf_CU *cu, Dwarf_Word idx, Dwarf_Addr *addr)
4809 : : {
4810 [ # # ]: 0 : if (cu == NULL)
4811 : : return -1;
4812 : :
4813 : 0 : Elf_Data *debug_addr = cu->dbg->sectiondata[IDX_debug_addr];
4814 [ # # ]: 0 : if (debug_addr == NULL)
4815 : : return -1;
4816 : :
4817 : 0 : Dwarf_Off base = __libdw_cu_addr_base (cu);
4818 : 0 : Dwarf_Word off = idx * cu->address_size;
4819 [ # # ]: 0 : if (base > debug_addr->d_size
4820 [ # # ]: 0 : || off > debug_addr->d_size - base
4821 [ # # ]: 0 : || cu->address_size > debug_addr->d_size - base - off)
4822 : : return -1;
4823 : :
4824 : 0 : const unsigned char *addrp = debug_addr->d_buf + base + off;
4825 [ # # ]: 0 : if (cu->address_size == 4)
4826 [ # # ]: 0 : *addr = read_4ubyte_unaligned (cu->dbg, addrp);
4827 : : else
4828 [ # # ]: 0 : *addr = read_8ubyte_unaligned (cu->dbg, addrp);
4829 : :
4830 : : return 0;
4831 : : }
4832 : :
4833 : : static void
4834 : 0 : print_ops (Dwfl_Module *dwflmod, Dwarf *dbg, int indent, int indentrest,
4835 : : unsigned int vers, unsigned int addrsize, unsigned int offset_size,
4836 : : struct Dwarf_CU *cu, Dwarf_Word len, const unsigned char *data)
4837 : : {
4838 [ # # ]: 0 : const unsigned int ref_size = vers < 3 ? addrsize : offset_size;
4839 : :
4840 [ # # ]: 0 : if (len == 0)
4841 : : {
4842 : 0 : printf ("%*s(empty)\n", indent, "");
4843 : 0 : return;
4844 : : }
4845 : :
4846 : : #define NEED(n) if (len < (Dwarf_Word) (n)) goto invalid
4847 : : #define CONSUME(n) NEED (n); else len -= (n)
4848 : :
4849 : : Dwarf_Word offset = 0;
4850 [ # # ]: 0 : while (len-- > 0)
4851 : 0 : {
4852 : 0 : uint_fast8_t op = *data++;
4853 : :
4854 [ # # ]: 0 : const char *op_name = dwarf_locexpr_opcode_string (op);
4855 [ # # ]: 0 : if (unlikely (op_name == NULL))
4856 : : {
4857 : 0 : static char buf[20];
4858 [ # # ]: 0 : if (op >= DW_OP_lo_user)
4859 : 0 : snprintf (buf, sizeof buf, "lo_user+%#x", op - DW_OP_lo_user);
4860 : : else
4861 : 0 : snprintf (buf, sizeof buf, "??? (%#x)", op);
4862 : : op_name = buf;
4863 : : }
4864 : :
4865 [ # # # # : 0 : switch (op)
# # # # #
# # # # #
# # # # #
# # # # #
# # # # ]
4866 : : {
4867 : 0 : case DW_OP_addr:;
4868 : : /* Address operand. */
4869 : 0 : Dwarf_Word addr;
4870 [ # # ]: 0 : NEED (addrsize);
4871 [ # # ]: 0 : if (addrsize == 4)
4872 [ # # ]: 0 : addr = read_4ubyte_unaligned (dbg, data);
4873 [ # # ]: 0 : else if (addrsize == 8)
4874 [ # # ]: 0 : addr = read_8ubyte_unaligned (dbg, data);
4875 : : else
4876 : 0 : goto invalid;
4877 : 0 : data += addrsize;
4878 : 0 : CONSUME (addrsize);
4879 : :
4880 : 0 : printf ("%*s[%2" PRIuMAX "] %s ",
4881 : : indent, "", (uintmax_t) offset, op_name);
4882 : 0 : print_dwarf_addr (dwflmod, 0, addr, addr);
4883 : 0 : printf ("\n");
4884 : :
4885 : 0 : offset += 1 + addrsize;
4886 : 0 : break;
4887 : :
4888 : 0 : case DW_OP_call_ref:
4889 : : case DW_OP_GNU_variable_value:
4890 : : /* Offset operand. */
4891 [ # # ]: 0 : if (ref_size != 4 && ref_size != 8)
4892 : 0 : goto invalid; /* Cannot be used in CFA. */
4893 [ # # ]: 0 : NEED (ref_size);
4894 [ # # ]: 0 : if (ref_size == 4)
4895 [ # # ]: 0 : addr = read_4ubyte_unaligned (dbg, data);
4896 : : else
4897 [ # # ]: 0 : addr = read_8ubyte_unaligned (dbg, data);
4898 : 0 : data += ref_size;
4899 : 0 : CONSUME (ref_size);
4900 : : /* addr is a DIE offset, so format it as one. */
4901 : 0 : printf ("%*s[%2" PRIuMAX "] %s [%6" PRIxMAX "]\n",
4902 : : indent, "", (uintmax_t) offset,
4903 : : op_name, (uintmax_t) addr);
4904 : 0 : offset += 1 + ref_size;
4905 : 0 : break;
4906 : :
4907 : 0 : case DW_OP_deref_size:
4908 : : case DW_OP_xderef_size:
4909 : : case DW_OP_pick:
4910 : : case DW_OP_const1u:
4911 : : // XXX value might be modified by relocation
4912 [ # # ]: 0 : NEED (1);
4913 : 0 : printf ("%*s[%2" PRIuMAX "] %s %" PRIu8 "\n",
4914 : : indent, "", (uintmax_t) offset,
4915 : 0 : op_name, *((uint8_t *) data));
4916 : 0 : ++data;
4917 : 0 : --len;
4918 : 0 : offset += 2;
4919 : 0 : break;
4920 : :
4921 : 0 : case DW_OP_const2u:
4922 [ # # ]: 0 : NEED (2);
4923 : : // XXX value might be modified by relocation
4924 : 0 : printf ("%*s[%2" PRIuMAX "] %s %" PRIu16 "\n",
4925 : : indent, "", (uintmax_t) offset,
4926 [ # # ]: 0 : op_name, read_2ubyte_unaligned (dbg, data));
4927 : 0 : CONSUME (2);
4928 : 0 : data += 2;
4929 : 0 : offset += 3;
4930 : 0 : break;
4931 : :
4932 : 0 : case DW_OP_const4u:
4933 [ # # ]: 0 : NEED (4);
4934 : : // XXX value might be modified by relocation
4935 : 0 : printf ("%*s[%2" PRIuMAX "] %s %" PRIu32 "\n",
4936 : : indent, "", (uintmax_t) offset,
4937 [ # # ]: 0 : op_name, read_4ubyte_unaligned (dbg, data));
4938 : 0 : CONSUME (4);
4939 : 0 : data += 4;
4940 : 0 : offset += 5;
4941 : 0 : break;
4942 : :
4943 : 0 : case DW_OP_const8u:
4944 [ # # ]: 0 : NEED (8);
4945 : : // XXX value might be modified by relocation
4946 : 0 : printf ("%*s[%2" PRIuMAX "] %s %" PRIu64 "\n",
4947 : : indent, "", (uintmax_t) offset,
4948 [ # # ]: 0 : op_name, (uint64_t) read_8ubyte_unaligned (dbg, data));
4949 : 0 : CONSUME (8);
4950 : 0 : data += 8;
4951 : 0 : offset += 9;
4952 : 0 : break;
4953 : :
4954 : 0 : case DW_OP_const1s:
4955 [ # # ]: 0 : NEED (1);
4956 : : // XXX value might be modified by relocation
4957 : 0 : printf ("%*s[%2" PRIuMAX "] %s %" PRId8 "\n",
4958 : : indent, "", (uintmax_t) offset,
4959 : 0 : op_name, *((int8_t *) data));
4960 : 0 : ++data;
4961 : 0 : --len;
4962 : 0 : offset += 2;
4963 : 0 : break;
4964 : :
4965 : 0 : case DW_OP_const2s:
4966 [ # # ]: 0 : NEED (2);
4967 : : // XXX value might be modified by relocation
4968 : 0 : printf ("%*s[%2" PRIuMAX "] %s %" PRId16 "\n",
4969 : : indent, "", (uintmax_t) offset,
4970 [ # # ]: 0 : op_name, read_2sbyte_unaligned (dbg, data));
4971 : 0 : CONSUME (2);
4972 : 0 : data += 2;
4973 : 0 : offset += 3;
4974 : 0 : break;
4975 : :
4976 : 0 : case DW_OP_const4s:
4977 [ # # ]: 0 : NEED (4);
4978 : : // XXX value might be modified by relocation
4979 : 0 : printf ("%*s[%2" PRIuMAX "] %s %" PRId32 "\n",
4980 : : indent, "", (uintmax_t) offset,
4981 [ # # ]: 0 : op_name, read_4sbyte_unaligned (dbg, data));
4982 : 0 : CONSUME (4);
4983 : 0 : data += 4;
4984 : 0 : offset += 5;
4985 : 0 : break;
4986 : :
4987 : 0 : case DW_OP_const8s:
4988 [ # # ]: 0 : NEED (8);
4989 : : // XXX value might be modified by relocation
4990 : 0 : printf ("%*s[%2" PRIuMAX "] %s %" PRId64 "\n",
4991 : : indent, "", (uintmax_t) offset,
4992 [ # # ]: 0 : op_name, read_8sbyte_unaligned (dbg, data));
4993 : 0 : CONSUME (8);
4994 : 0 : data += 8;
4995 : 0 : offset += 9;
4996 : 0 : break;
4997 : :
4998 : 0 : case DW_OP_piece:
4999 : : case DW_OP_regx:
5000 : : case DW_OP_plus_uconst:
5001 : 0 : case DW_OP_constu:;
5002 : 0 : const unsigned char *start = data;
5003 : 0 : uint64_t uleb;
5004 [ # # ]: 0 : NEED (1);
5005 : 0 : get_uleb128 (uleb, data, data + len);
5006 : 0 : printf ("%*s[%2" PRIuMAX "] %s %" PRIu64 "\n",
5007 : : indent, "", (uintmax_t) offset, op_name, uleb);
5008 [ # # ]: 0 : CONSUME (data - start);
5009 : 0 : offset += 1 + (data - start);
5010 : 0 : break;
5011 : :
5012 : 0 : case DW_OP_addrx:
5013 : : case DW_OP_GNU_addr_index:
5014 : : case DW_OP_constx:
5015 : 0 : case DW_OP_GNU_const_index:;
5016 : 0 : start = data;
5017 [ # # ]: 0 : NEED (1);
5018 : 0 : get_uleb128 (uleb, data, data + len);
5019 : 0 : printf ("%*s[%2" PRIuMAX "] %s [%" PRIu64 "] ",
5020 : : indent, "", (uintmax_t) offset, op_name, uleb);
5021 [ # # ]: 0 : CONSUME (data - start);
5022 : 0 : offset += 1 + (data - start);
5023 [ # # ]: 0 : if (get_indexed_addr (cu, uleb, &addr) != 0)
5024 : 0 : printf ("???\n");
5025 : : else
5026 : : {
5027 : 0 : print_dwarf_addr (dwflmod, 0, addr, addr);
5028 : 0 : printf ("\n");
5029 : : }
5030 : : break;
5031 : :
5032 : 0 : case DW_OP_bit_piece:
5033 : 0 : start = data;
5034 : 0 : uint64_t uleb2;
5035 [ # # ]: 0 : NEED (1);
5036 : 0 : get_uleb128 (uleb, data, data + len);
5037 : 0 : NEED (1);
5038 : 0 : get_uleb128 (uleb2, data, data + len);
5039 : 0 : printf ("%*s[%2" PRIuMAX "] %s %" PRIu64 ", %" PRIu64 "\n",
5040 : : indent, "", (uintmax_t) offset, op_name, uleb, uleb2);
5041 [ # # ]: 0 : CONSUME (data - start);
5042 : 0 : offset += 1 + (data - start);
5043 : 0 : break;
5044 : :
5045 : 0 : case DW_OP_fbreg:
5046 : : case DW_OP_breg0 ... DW_OP_breg31:
5047 : : case DW_OP_consts:
5048 : 0 : start = data;
5049 : 0 : int64_t sleb;
5050 [ # # ]: 0 : NEED (1);
5051 : 0 : get_sleb128 (sleb, data, data + len);
5052 : 0 : printf ("%*s[%2" PRIuMAX "] %s %" PRId64 "\n",
5053 : : indent, "", (uintmax_t) offset, op_name, sleb);
5054 [ # # ]: 0 : CONSUME (data - start);
5055 : 0 : offset += 1 + (data - start);
5056 : 0 : break;
5057 : :
5058 : 0 : case DW_OP_bregx:
5059 : 0 : start = data;
5060 [ # # ]: 0 : NEED (1);
5061 : 0 : get_uleb128 (uleb, data, data + len);
5062 : 0 : NEED (1);
5063 : 0 : get_sleb128 (sleb, data, data + len);
5064 : 0 : printf ("%*s[%2" PRIuMAX "] %s %" PRIu64 " %" PRId64 "\n",
5065 : : indent, "", (uintmax_t) offset, op_name, uleb, sleb);
5066 [ # # ]: 0 : CONSUME (data - start);
5067 : 0 : offset += 1 + (data - start);
5068 : 0 : break;
5069 : :
5070 : 0 : case DW_OP_call2:
5071 [ # # ]: 0 : NEED (2);
5072 : 0 : printf ("%*s[%2" PRIuMAX "] %s [%6" PRIx16 "]\n",
5073 : : indent, "", (uintmax_t) offset, op_name,
5074 [ # # ]: 0 : read_2ubyte_unaligned (dbg, data));
5075 : 0 : CONSUME (2);
5076 : 0 : data += 2;
5077 : 0 : offset += 3;
5078 : 0 : break;
5079 : :
5080 : 0 : case DW_OP_call4:
5081 [ # # ]: 0 : NEED (4);
5082 : 0 : printf ("%*s[%2" PRIuMAX "] %s [%6" PRIx32 "]\n",
5083 : : indent, "", (uintmax_t) offset, op_name,
5084 [ # # ]: 0 : read_4ubyte_unaligned (dbg, data));
5085 : 0 : CONSUME (4);
5086 : 0 : data += 4;
5087 : 0 : offset += 5;
5088 : 0 : break;
5089 : :
5090 : 0 : case DW_OP_skip:
5091 : : case DW_OP_bra:
5092 [ # # ]: 0 : NEED (2);
5093 : 0 : printf ("%*s[%2" PRIuMAX "] %s %" PRIuMAX "\n",
5094 : : indent, "", (uintmax_t) offset, op_name,
5095 [ # # ]: 0 : (uintmax_t) (offset + read_2sbyte_unaligned (dbg, data) + 3));
5096 : 0 : CONSUME (2);
5097 : 0 : data += 2;
5098 : 0 : offset += 3;
5099 : 0 : break;
5100 : :
5101 : 0 : case DW_OP_implicit_value:
5102 : 0 : start = data;
5103 [ # # ]: 0 : NEED (1);
5104 : 0 : get_uleb128 (uleb, data, data + len);
5105 : 0 : printf ("%*s[%2" PRIuMAX "] %s: ",
5106 : : indent, "", (uintmax_t) offset, op_name);
5107 [ # # ]: 0 : NEED (uleb);
5108 : 0 : print_block (uleb, data);
5109 : 0 : data += uleb;
5110 [ # # ]: 0 : CONSUME (data - start);
5111 : 0 : offset += 1 + (data - start);
5112 : 0 : break;
5113 : :
5114 : 0 : case DW_OP_implicit_pointer:
5115 : : case DW_OP_GNU_implicit_pointer:
5116 : : /* DIE offset operand. */
5117 : 0 : start = data;
5118 [ # # ]: 0 : NEED (ref_size);
5119 [ # # ]: 0 : if (ref_size != 4 && ref_size != 8)
5120 : 0 : goto invalid; /* Cannot be used in CFA. */
5121 [ # # ]: 0 : if (ref_size == 4)
5122 [ # # ]: 0 : addr = read_4ubyte_unaligned (dbg, data);
5123 : : else
5124 [ # # ]: 0 : addr = read_8ubyte_unaligned (dbg, data);
5125 : 0 : data += ref_size;
5126 : : /* Byte offset operand. */
5127 [ # # ]: 0 : NEED (1);
5128 : 0 : get_sleb128 (sleb, data, data + len);
5129 : :
5130 : 0 : printf ("%*s[%2" PRIuMAX "] %s [%6" PRIxMAX "] %+" PRId64 "\n",
5131 : : indent, "", (intmax_t) offset,
5132 : : op_name, (uintmax_t) addr, sleb);
5133 [ # # ]: 0 : CONSUME (data - start);
5134 : 0 : offset += 1 + (data - start);
5135 : 0 : break;
5136 : :
5137 : 0 : case DW_OP_entry_value:
5138 : : case DW_OP_GNU_entry_value:
5139 : : /* Size plus expression block. */
5140 : 0 : start = data;
5141 [ # # ]: 0 : NEED (1);
5142 : 0 : get_uleb128 (uleb, data, data + len);
5143 : 0 : printf ("%*s[%2" PRIuMAX "] %s:\n",
5144 : : indent, "", (uintmax_t) offset, op_name);
5145 [ # # ]: 0 : NEED (uleb);
5146 : 0 : print_ops (dwflmod, dbg, indent + 5, indent + 5, vers,
5147 : : addrsize, offset_size, cu, uleb, data);
5148 : 0 : data += uleb;
5149 [ # # ]: 0 : CONSUME (data - start);
5150 : 0 : offset += 1 + (data - start);
5151 : 0 : break;
5152 : :
5153 : 0 : case DW_OP_const_type:
5154 : : case DW_OP_GNU_const_type:
5155 : : /* uleb128 CU relative DW_TAG_base_type DIE offset, 1-byte
5156 : : unsigned size plus block. */
5157 : 0 : start = data;
5158 [ # # ]: 0 : NEED (1);
5159 : 0 : get_uleb128 (uleb, data, data + len);
5160 [ # # # # ]: 0 : if (! print_unresolved_addresses && cu != NULL)
5161 : 0 : uleb += cu->start;
5162 : 0 : NEED (1);
5163 : 0 : uint8_t usize = *(uint8_t *) data++;
5164 [ # # ]: 0 : NEED (usize);
5165 : 0 : printf ("%*s[%2" PRIuMAX "] %s [%6" PRIxMAX "] ",
5166 : : indent, "", (uintmax_t) offset, op_name, uleb);
5167 : 0 : print_block (usize, data);
5168 : 0 : data += usize;
5169 [ # # ]: 0 : CONSUME (data - start);
5170 : 0 : offset += 1 + (data - start);
5171 : 0 : break;
5172 : :
5173 : 0 : case DW_OP_regval_type:
5174 : : case DW_OP_GNU_regval_type:
5175 : : /* uleb128 register number, uleb128 CU relative
5176 : : DW_TAG_base_type DIE offset. */
5177 : 0 : start = data;
5178 [ # # ]: 0 : NEED (1);
5179 : 0 : get_uleb128 (uleb, data, data + len);
5180 : 0 : NEED (1);
5181 : 0 : get_uleb128 (uleb2, data, data + len);
5182 [ # # # # ]: 0 : if (! print_unresolved_addresses && cu != NULL)
5183 : 0 : uleb2 += cu->start;
5184 : 0 : printf ("%*s[%2" PRIuMAX "] %s %" PRIu64 " [%6" PRIx64 "]\n",
5185 : : indent, "", (uintmax_t) offset, op_name, uleb, uleb2);
5186 [ # # ]: 0 : CONSUME (data - start);
5187 : 0 : offset += 1 + (data - start);
5188 : 0 : break;
5189 : :
5190 : 0 : case DW_OP_deref_type:
5191 : : case DW_OP_GNU_deref_type:
5192 : : /* 1-byte unsigned size of value, uleb128 CU relative
5193 : : DW_TAG_base_type DIE offset. */
5194 : 0 : start = data;
5195 [ # # ]: 0 : NEED (1);
5196 : 0 : usize = *(uint8_t *) data++;
5197 : 0 : NEED (1);
5198 : 0 : get_uleb128 (uleb, data, data + len);
5199 [ # # # # ]: 0 : if (! print_unresolved_addresses && cu != NULL)
5200 : 0 : uleb += cu->start;
5201 : 0 : printf ("%*s[%2" PRIuMAX "] %s %" PRIu8 " [%6" PRIxMAX "]\n",
5202 : : indent, "", (uintmax_t) offset,
5203 : : op_name, usize, uleb);
5204 [ # # ]: 0 : CONSUME (data - start);
5205 : 0 : offset += 1 + (data - start);
5206 : 0 : break;
5207 : :
5208 : 0 : case DW_OP_xderef_type:
5209 : : /* 1-byte unsigned size of value, uleb128 base_type DIE offset. */
5210 : 0 : start = data;
5211 [ # # ]: 0 : NEED (1);
5212 : 0 : usize = *(uint8_t *) data++;
5213 : 0 : NEED (1);
5214 : 0 : get_uleb128 (uleb, data, data + len);
5215 : 0 : printf ("%*s[%4" PRIuMAX "] %s %" PRIu8 " [%6" PRIxMAX "]\n",
5216 : : indent, "", (uintmax_t) offset,
5217 : : op_name, usize, uleb);
5218 [ # # ]: 0 : CONSUME (data - start);
5219 : 0 : offset += 1 + (data - start);
5220 : 0 : break;
5221 : :
5222 : 0 : case DW_OP_convert:
5223 : : case DW_OP_GNU_convert:
5224 : : case DW_OP_reinterpret:
5225 : : case DW_OP_GNU_reinterpret:
5226 : : /* uleb128 CU relative offset to DW_TAG_base_type, or zero
5227 : : for conversion to untyped. */
5228 : 0 : start = data;
5229 [ # # ]: 0 : NEED (1);
5230 : 0 : get_uleb128 (uleb, data, data + len);
5231 [ # # # # : 0 : if (uleb != 0 && ! print_unresolved_addresses && cu != NULL)
# # ]
5232 : 0 : uleb += cu->start;
5233 : 0 : printf ("%*s[%2" PRIuMAX "] %s [%6" PRIxMAX "]\n",
5234 : : indent, "", (uintmax_t) offset, op_name, uleb);
5235 [ # # ]: 0 : CONSUME (data - start);
5236 : 0 : offset += 1 + (data - start);
5237 : 0 : break;
5238 : :
5239 : 0 : case DW_OP_GNU_parameter_ref:
5240 : : /* 4 byte CU relative reference to the abstract optimized away
5241 : : DW_TAG_formal_parameter. */
5242 [ # # ]: 0 : NEED (4);
5243 [ # # ]: 0 : uintmax_t param_off = (uintmax_t) read_4ubyte_unaligned (dbg, data);
5244 [ # # # # ]: 0 : if (! print_unresolved_addresses && cu != NULL)
5245 : 0 : param_off += cu->start;
5246 : 0 : printf ("%*s[%2" PRIuMAX "] %s [%6" PRIxMAX "]\n",
5247 : : indent, "", (uintmax_t) offset, op_name, param_off);
5248 : 0 : CONSUME (4);
5249 : 0 : data += 4;
5250 : 0 : offset += 5;
5251 : 0 : break;
5252 : :
5253 : : default:
5254 : : /* No Operand. */
5255 : 0 : printf ("%*s[%2" PRIuMAX "] %s\n",
5256 : : indent, "", (uintmax_t) offset, op_name);
5257 : 0 : ++offset;
5258 : 0 : break;
5259 : : }
5260 : :
5261 : 0 : indent = indentrest;
5262 : 0 : continue;
5263 : :
5264 : 0 : invalid:
5265 : 0 : printf (_("%*s[%2" PRIuMAX "] %s <TRUNCATED>\n"),
5266 : : indent, "", (uintmax_t) offset, op_name);
5267 : : break;
5268 : : }
5269 : : }
5270 : :
5271 : :
5272 : : /* Turn the addresses into file offsets by using the phdrs. */
5273 : : static void
5274 : 0 : find_offsets(Elf *elf, GElf_Addr main_bias, size_t n,
5275 : : GElf_Addr addrs[n], GElf_Off offs[n])
5276 : : {
5277 : 0 : size_t unsolved = n;
5278 [ # # ]: 0 : for (size_t i = 0; i < phnum; ++i) {
5279 : 0 : GElf_Phdr phdr_mem;
5280 : 0 : GElf_Phdr *phdr = gelf_getphdr(elf, i, &phdr_mem);
5281 [ # # # # : 0 : if (phdr != NULL && phdr->p_type == PT_LOAD && phdr->p_memsz > 0)
# # ]
5282 [ # # ]: 0 : for (size_t j = 0; j < n; ++j)
5283 [ # # # # ]: 0 : if (offs[j] == 0 && addrs[j] >= phdr->p_vaddr + main_bias &&
5284 [ # # ]: 0 : addrs[j] - (phdr->p_vaddr + main_bias) < phdr->p_filesz) {
5285 : 0 : offs[j] = addrs[j] - (phdr->p_vaddr + main_bias) + phdr->p_offset;
5286 [ # # ]: 0 : if (--unsolved == 0)
5287 : : break;
5288 : : }
5289 : : }
5290 : 0 : }
5291 : :
5292 : : /* The dynamic segment (type PT_DYNAMIC), contains the .dynamic section.
5293 : : And .dynamic section contains an array of the dynamic structures.
5294 : : We use the array to get:
5295 : : DT_STRTAB: the address of the string table
5296 : : DT_SYMTAB: the address of the symbol table
5297 : : DT_STRSZ: the size, in bytes, of the string table
5298 : : ... */
5299 : : static void
5300 : 0 : get_dynscn_addrs(Elf *elf, GElf_Phdr *phdr, GElf_Addr addrs[i_max])
5301 : : {
5302 : 0 : Elf_Data *data = elf_getdata_rawchunk(
5303 : 0 : elf, phdr->p_offset, phdr->p_filesz, ELF_T_DYN);
5304 : :
5305 : 0 : int dyn_idx = 0;
5306 : 0 : for (;; ++dyn_idx) {
5307 : 0 : GElf_Dyn dyn_mem;
5308 : 0 : GElf_Dyn *dyn = gelf_getdyn(data, dyn_idx, &dyn_mem);
5309 : : /* DT_NULL Marks end of dynamic section. */
5310 [ # # # # ]: 0 : if (dyn == NULL || dyn->d_tag == DT_NULL)
5311 : : break;
5312 : :
5313 [ # # # # : 0 : switch (dyn->d_tag) {
# # # # #
# # # ]
5314 : 0 : case DT_SYMTAB:
5315 : 0 : addrs[i_symtab] = dyn->d_un.d_ptr;
5316 : 0 : break;
5317 : :
5318 : 0 : case DT_HASH:
5319 : 0 : addrs[i_hash] = dyn->d_un.d_ptr;
5320 : 0 : break;
5321 : :
5322 : 0 : case DT_GNU_HASH:
5323 : 0 : addrs[i_gnu_hash] = dyn->d_un.d_ptr;
5324 : 0 : break;
5325 : :
5326 : 0 : case DT_STRTAB:
5327 : 0 : addrs[i_strtab] = dyn->d_un.d_ptr;
5328 : 0 : break;
5329 : :
5330 : 0 : case DT_VERSYM:
5331 : 0 : addrs[i_versym] = dyn->d_un.d_ptr;
5332 : 0 : break;
5333 : :
5334 : 0 : case DT_VERDEF:
5335 : 0 : addrs[i_verdef] = dyn->d_un.d_ptr;
5336 : 0 : break;
5337 : :
5338 : 0 : case DT_VERDEFNUM:
5339 : 0 : addrs[i_verdefnum] = dyn->d_un.d_val;
5340 : 0 : break;
5341 : :
5342 : 0 : case DT_VERNEED:
5343 : 0 : addrs[i_verneed] = dyn->d_un.d_ptr;
5344 : 0 : break;
5345 : :
5346 : 0 : case DT_VERNEEDNUM:
5347 : 0 : addrs[i_verneednum] = dyn->d_un.d_val;
5348 : 0 : break;
5349 : :
5350 : 0 : case DT_STRSZ:
5351 : 0 : addrs[i_strsz] = dyn->d_un.d_val;
5352 : 0 : break;
5353 : :
5354 : 0 : case DT_SYMTAB_SHNDX:
5355 : 0 : addrs[i_symtab_shndx] = dyn->d_un.d_ptr;
5356 : 0 : break;
5357 : : }
5358 : : }
5359 : 0 : }
5360 : :
5361 : :
5362 : : /* Use dynamic segment to get data for the string table section. */
5363 : : static Elf_Data *
5364 : 0 : get_dynscn_strtab(Elf *elf, GElf_Phdr *phdr)
5365 : : {
5366 : 0 : Elf_Data *strtab_data;
5367 : 0 : GElf_Addr addrs[i_max] = {0,};
5368 : 0 : GElf_Off offs[i_max] = {0,};
5369 : 0 : get_dynscn_addrs(elf, phdr, addrs);
5370 : 0 : find_offsets(elf, 0, i_max, addrs, offs);
5371 : 0 : strtab_data = elf_getdata_rawchunk(
5372 : 0 : elf, offs[i_strtab], addrs[i_strsz], ELF_T_BYTE);
5373 : 0 : return strtab_data;
5374 : : }
5375 : :
5376 : :
5377 : : struct listptr
5378 : : {
5379 : : Dwarf_Off offset:(64 - 3);
5380 : : bool addr64:1;
5381 : : bool dwarf64:1;
5382 : : bool warned:1;
5383 : : struct Dwarf_CU *cu;
5384 : : unsigned int attr;
5385 : : };
5386 : :
5387 : : #define listptr_offset_size(p) ((p)->dwarf64 ? 8 : 4)
5388 : : #define listptr_address_size(p) ((p)->addr64 ? 8 : 4)
5389 : :
5390 : : static Dwarf_Addr
5391 : 0 : cudie_base (Dwarf_Die *cudie)
5392 : : {
5393 : 0 : Dwarf_Addr base;
5394 : : /* Find the base address of the compilation unit. It will normally
5395 : : be specified by DW_AT_low_pc. In DWARF-3 draft 4, the base
5396 : : address could be overridden by DW_AT_entry_pc. It's been
5397 : : removed, but GCC emits DW_AT_entry_pc and not DW_AT_lowpc for
5398 : : compilation units with discontinuous ranges. */
5399 [ # # ]: 0 : if (unlikely (dwarf_lowpc (cudie, &base) != 0))
5400 : : {
5401 : 0 : Dwarf_Attribute attr_mem;
5402 [ # # ]: 0 : if (dwarf_formaddr (dwarf_attr (cudie, DW_AT_entry_pc, &attr_mem),
5403 : : &base) != 0)
5404 : 0 : base = 0;
5405 : : }
5406 : 0 : return base;
5407 : : }
5408 : :
5409 : : static Dwarf_Addr
5410 : 0 : listptr_base (struct listptr *p)
5411 : : {
5412 : 0 : Dwarf_Die cu = CUDIE (p->cu);
5413 : 0 : return cudie_base (&cu);
5414 : : }
5415 : :
5416 : : /* To store the name used in compare_listptr */
5417 : : static const char *sort_listptr_name;
5418 : :
5419 : : static int
5420 : 0 : compare_listptr (const void *a, const void *b)
5421 : : {
5422 : 0 : const char *name = sort_listptr_name;
5423 : 0 : struct listptr *p1 = (void *) a;
5424 : 0 : struct listptr *p2 = (void *) b;
5425 : :
5426 [ # # ]: 0 : if (p1->offset < p2->offset)
5427 : : return -1;
5428 [ # # ]: 0 : if (p1->offset > p2->offset)
5429 : : return 1;
5430 : :
5431 [ # # # # ]: 0 : if (!p1->warned && !p2->warned)
5432 : : {
5433 [ # # ]: 0 : if (p1->addr64 != p2->addr64)
5434 : : {
5435 : 0 : p1->warned = p2->warned = true;
5436 : 0 : error (0, 0,
5437 : 0 : _("%s %#" PRIx64 " used with different address sizes"),
5438 : : name, (uint64_t) p1->offset);
5439 : : }
5440 [ # # ]: 0 : if (p1->dwarf64 != p2->dwarf64)
5441 : : {
5442 : 0 : p1->warned = p2->warned = true;
5443 : 0 : error (0, 0,
5444 : 0 : _("%s %#" PRIx64 " used with different offset sizes"),
5445 : 0 : name, (uint64_t) p1->offset);
5446 : : }
5447 [ # # ]: 0 : if (listptr_base (p1) != listptr_base (p2))
5448 : : {
5449 : 0 : p1->warned = p2->warned = true;
5450 : 0 : error (0, 0,
5451 : 0 : _("%s %#" PRIx64 " used with different base addresses"),
5452 : 0 : name, (uint64_t) p1->offset);
5453 : : }
5454 [ # # ]: 0 : if (p1->attr != p2 ->attr)
5455 : : {
5456 : 0 : p1->warned = p2->warned = true;
5457 : 0 : error (0, 0,
5458 : 0 : _("%s %#" PRIx64
5459 : : " used with different attribute %s and %s"),
5460 : 0 : name, (uint64_t) p1->offset, dwarf_attr_name (p1->attr),
5461 : : dwarf_attr_name (p2->attr));
5462 : : }
5463 : : }
5464 : :
5465 : : return 0;
5466 : : }
5467 : :
5468 : : struct listptr_table
5469 : : {
5470 : : size_t n;
5471 : : size_t alloc;
5472 : : struct listptr *table;
5473 : : };
5474 : :
5475 : : static struct listptr_table known_locsptr;
5476 : : static struct listptr_table known_loclistsptr;
5477 : : static struct listptr_table known_rangelistptr;
5478 : : static struct listptr_table known_rnglistptr;
5479 : : static struct listptr_table known_addrbases;
5480 : : static struct listptr_table known_stroffbases;
5481 : :
5482 : : static void
5483 : 0 : reset_listptr (struct listptr_table *table)
5484 : : {
5485 : 0 : free (table->table);
5486 : 0 : table->table = NULL;
5487 : 0 : table->n = table->alloc = 0;
5488 : : }
5489 : :
5490 : : /* Returns false if offset doesn't fit. See struct listptr. */
5491 : : static bool
5492 : 0 : notice_listptr (enum section_e section, struct listptr_table *table,
5493 : : uint_fast8_t address_size, uint_fast8_t offset_size,
5494 : : struct Dwarf_CU *cu, Dwarf_Off offset, unsigned int attr)
5495 : : {
5496 [ # # ]: 0 : if (print_debug_sections & section)
5497 : : {
5498 [ # # ]: 0 : if (table->n == table->alloc)
5499 : : {
5500 [ # # ]: 0 : if (table->alloc == 0)
5501 : 0 : table->alloc = 128;
5502 : : else
5503 : 0 : table->alloc *= 2;
5504 : 0 : table->table = xrealloc (table->table,
5505 : 0 : table->alloc * sizeof table->table[0]);
5506 : : }
5507 : :
5508 : 0 : struct listptr *p = &table->table[table->n++];
5509 : :
5510 : 0 : *p = (struct listptr)
5511 : : {
5512 : 0 : .addr64 = address_size == 8,
5513 : 0 : .dwarf64 = offset_size == 8,
5514 : : .offset = offset,
5515 : : .cu = cu,
5516 : : .attr = attr
5517 : : };
5518 : :
5519 [ # # ]: 0 : if (p->offset != offset)
5520 : : {
5521 : 0 : table->n--;
5522 : 0 : return false;
5523 : : }
5524 : : }
5525 : : return true;
5526 : : }
5527 : :
5528 : : static void
5529 : 0 : sort_listptr (struct listptr_table *table, const char *name)
5530 : : {
5531 [ # # ]: 0 : if (table->n > 0)
5532 : : {
5533 : 0 : sort_listptr_name = name;
5534 : 0 : qsort (table->table, table->n, sizeof table->table[0],
5535 : : &compare_listptr);
5536 : : }
5537 : 0 : }
5538 : :
5539 : : static bool
5540 : 0 : skip_listptr_hole (struct listptr_table *table, size_t *idxp,
5541 : : uint_fast8_t *address_sizep, uint_fast8_t *offset_sizep,
5542 : : Dwarf_Addr *base, struct Dwarf_CU **cu, ptrdiff_t offset,
5543 : : unsigned char **readp, unsigned char *endp,
5544 : : unsigned int *attr)
5545 : : {
5546 [ # # ]: 0 : if (table->n == 0)
5547 : : return false;
5548 : :
5549 [ # # # # ]: 0 : while (*idxp < table->n && table->table[*idxp].offset < (Dwarf_Off) offset)
5550 : 0 : ++*idxp;
5551 : :
5552 : 0 : struct listptr *p = &table->table[*idxp];
5553 : :
5554 [ # # ]: 0 : if (*idxp == table->n
5555 [ # # ]: 0 : || p->offset >= (Dwarf_Off) (endp - *readp + offset))
5556 : : {
5557 : 0 : *readp = endp;
5558 : 0 : printf (_(" [%6tx] <UNUSED GARBAGE IN REST OF SECTION>\n"),
5559 : : offset);
5560 : 0 : return true;
5561 : : }
5562 : :
5563 [ # # ]: 0 : if (p->offset != (Dwarf_Off) offset)
5564 : : {
5565 : 0 : *readp += p->offset - offset;
5566 : 0 : printf (_(" [%6tx] <UNUSED GARBAGE> ... %" PRIu64 " bytes ...\n"),
5567 : : offset, (Dwarf_Off) p->offset - offset);
5568 : 0 : return true;
5569 : : }
5570 : :
5571 [ # # ]: 0 : if (address_sizep != NULL)
5572 [ # # ]: 0 : *address_sizep = listptr_address_size (p);
5573 [ # # ]: 0 : if (offset_sizep != NULL)
5574 [ # # ]: 0 : *offset_sizep = listptr_offset_size (p);
5575 [ # # ]: 0 : if (base != NULL)
5576 : 0 : *base = listptr_base (p);
5577 [ # # ]: 0 : if (cu != NULL)
5578 : 0 : *cu = p->cu;
5579 [ # # ]: 0 : if (attr != NULL)
5580 : 0 : *attr = p->attr;
5581 : :
5582 : : return false;
5583 : : }
5584 : :
5585 : : static Dwarf_Off
5586 : 0 : next_listptr_offset (struct listptr_table *table, size_t *idxp, Dwarf_Off off)
5587 : : {
5588 : : /* Note that multiple attributes could in theory point to the same loclist
5589 : : offset, so make sure we pick one that is bigger than the current one.
5590 : : The table is sorted on offset. */
5591 [ # # ]: 0 : if (*idxp < table->n)
5592 : : {
5593 [ # # ]: 0 : while (++*idxp < table->n)
5594 : : {
5595 : 0 : Dwarf_Off next = table->table[*idxp].offset;
5596 [ # # ]: 0 : if (next > off)
5597 : 0 : return next;
5598 : : }
5599 : : }
5600 : : return 0;
5601 : : }
5602 : :
5603 : : /* Returns the listptr associated with the given index, or NULL. */
5604 : : static struct listptr *
5605 : 0 : get_listptr (struct listptr_table *table, size_t idx)
5606 : : {
5607 : 0 : if (idx >= table->n)
5608 : : return NULL;
5609 : 0 : return &table->table[idx];
5610 : : }
5611 : :
5612 : : /* Returns the next index, base address and CU associated with the
5613 : : list unit offsets. If there is none false is returned, otherwise
5614 : : true. Assumes the table has been sorted. */
5615 : : static bool
5616 : 0 : listptr_cu (struct listptr_table *table, size_t *idxp,
5617 : : Dwarf_Off start, Dwarf_Off end,
5618 : : Dwarf_Addr *base, struct Dwarf_CU **cu)
5619 : : {
5620 : 0 : while (*idxp < table->n
5621 [ # # # # ]: 0 : && table->table[*idxp].offset < start)
5622 : 0 : ++*idxp;
5623 : :
5624 [ # # ]: 0 : if (*idxp < table->n
5625 [ # # ]: 0 : && table->table[*idxp].offset >= start
5626 [ # # ]: 0 : && table->table[*idxp].offset < end)
5627 : : {
5628 : 0 : struct listptr *p = &table->table[*idxp];
5629 : 0 : *base = listptr_base (p);
5630 : 0 : *cu = p->cu;
5631 : 0 : return true;
5632 : : }
5633 : :
5634 : : return false;
5635 : : }
5636 : :
5637 : : /* Returns the next index with the current CU for the given attribute.
5638 : : If there is none false is returned, otherwise true. Assumes the
5639 : : table has been sorted. */
5640 : : static bool
5641 : 0 : listptr_attr (struct listptr_table *table, size_t idxp,
5642 : : Dwarf_Off offset, unsigned int attr)
5643 : : {
5644 : 0 : struct listptr *listptr;
5645 : 0 : do
5646 : : {
5647 [ # # ]: 0 : listptr = get_listptr (table, idxp);
5648 [ # # ]: 0 : if (listptr == NULL)
5649 : : return false;
5650 : :
5651 [ # # # # ]: 0 : if (listptr->offset == offset && listptr->attr == attr)
5652 : : return true;
5653 : :
5654 : 0 : idxp++;
5655 : : }
5656 [ # # ]: 0 : while (listptr->offset <= offset);
5657 : :
5658 : : return false;
5659 : : }
5660 : :
5661 : : static void
5662 : 0 : print_debug_abbrev_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
5663 : : Ebl *ebl, GElf_Ehdr *ehdr __attribute__ ((unused)),
5664 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
5665 : : {
5666 : 0 : Elf_Data *elf_data = get_debug_elf_data (dbg, ebl, IDX_debug_abbrev, scn);
5667 [ # # ]: 0 : if (elf_data == NULL)
5668 : : return;
5669 : :
5670 : 0 : const size_t sh_size = elf_data->d_size;
5671 : :
5672 : 0 : printf (_("\nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"
5673 : : " [ Code]\n"),
5674 : : elf_ndxscn (scn), section_name (ebl, shdr),
5675 : 0 : (uint64_t) shdr->sh_offset);
5676 : :
5677 : 0 : Dwarf_Off offset = 0;
5678 [ # # ]: 0 : while (offset < sh_size)
5679 : : {
5680 : 0 : printf (_("\nAbbreviation section at offset %" PRIu64 ":\n"),
5681 : : offset);
5682 : :
5683 : 0 : while (1)
5684 : 0 : {
5685 : 0 : size_t length;
5686 : 0 : Dwarf_Abbrev abbrev;
5687 : :
5688 : 0 : int res = dwarf_offabbrev (dbg, offset, &length, &abbrev);
5689 [ # # ]: 0 : if (res != 0)
5690 : : {
5691 [ # # ]: 0 : if (unlikely (res < 0))
5692 : : {
5693 : 0 : printf (_("\
5694 : : *** error while reading abbreviation: %s\n"),
5695 : : dwarf_errmsg (-1));
5696 : 0 : return;
5697 : : }
5698 : :
5699 : : /* This is the NUL byte at the end of the section. */
5700 : 0 : ++offset;
5701 : 0 : break;
5702 : : }
5703 : :
5704 : : /* We know these calls can never fail. */
5705 : 0 : unsigned int code = dwarf_getabbrevcode (&abbrev);
5706 : 0 : unsigned int tag = dwarf_getabbrevtag (&abbrev);
5707 : 0 : int has_children = dwarf_abbrevhaschildren (&abbrev);
5708 : :
5709 [ # # ]: 0 : printf (_(" [%5u] offset: %" PRId64
5710 : : ", children: %s, tag: %s\n"),
5711 : : code, (int64_t) offset,
5712 : : has_children ? yes_str : no_str,
5713 : : dwarf_tag_name (tag));
5714 : :
5715 : 0 : size_t cnt = 0;
5716 : 0 : unsigned int name;
5717 : 0 : unsigned int form;
5718 : 0 : Dwarf_Sword data;
5719 : 0 : Dwarf_Off enoffset;
5720 : 0 : while (dwarf_getabbrevattr_data (&abbrev, cnt, &name, &form,
5721 [ # # ]: 0 : &data, &enoffset) == 0)
5722 : : {
5723 : 0 : printf (" attr: %s, form: %s",
5724 : : dwarf_attr_name (name), dwarf_form_name (form));
5725 [ # # ]: 0 : if (form == DW_FORM_implicit_const)
5726 : 0 : printf (" (%" PRId64 ")", data);
5727 : 0 : printf (", offset: %#" PRIx64 "\n", (uint64_t) enoffset);
5728 : 0 : ++cnt;
5729 : : }
5730 : :
5731 : 0 : offset += length;
5732 : : }
5733 : : }
5734 : : }
5735 : :
5736 : :
5737 : : static void
5738 : 0 : print_debug_addr_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
5739 : : Ebl *ebl, GElf_Ehdr *ehdr,
5740 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
5741 : : {
5742 : 0 : Elf_Data *data = get_debug_elf_data (dbg, ebl, IDX_debug_addr, scn);
5743 [ # # ]: 0 : if (data == NULL)
5744 : : return;
5745 : :
5746 : 0 : printf (_("\
5747 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
5748 : : elf_ndxscn (scn), section_name (ebl, shdr),
5749 : 0 : (uint64_t) shdr->sh_offset);
5750 : :
5751 [ # # ]: 0 : if (shdr->sh_size == 0)
5752 : : return;
5753 : :
5754 : 0 : size_t idx = 0;
5755 : 0 : sort_listptr (&known_addrbases, "addr_base");
5756 : :
5757 : 0 : const unsigned char *start = (const unsigned char *) data->d_buf;
5758 : 0 : const unsigned char *readp = start;
5759 : 0 : const unsigned char *readendp = ((const unsigned char *) data->d_buf
5760 : 0 : + data->d_size);
5761 : :
5762 : 0 : while (readp < readendp)
5763 : : {
5764 : : /* We cannot really know whether or not there is an header. The
5765 : : DebugFission extension to DWARF4 doesn't add one. The DWARF5
5766 : : .debug_addr variant does. Whether or not we have an header,
5767 : : DW_AT_[GNU_]addr_base points at "index 0". So if the current
5768 : : offset equals the CU addr_base then we can just start
5769 : : printing addresses. If there is no CU with an exact match
5770 : : then we'll try to parse the header first. */
5771 : 0 : Dwarf_Off off = (Dwarf_Off) (readp
5772 : 0 : - (const unsigned char *) data->d_buf);
5773 : :
5774 : 0 : printf ("Table at offset %" PRIx64 " ", off);
5775 : :
5776 [ # # ]: 0 : struct listptr *listptr = get_listptr (&known_addrbases, idx++);
5777 : 0 : const unsigned char *next_unitp;
5778 : :
5779 : 0 : uint64_t unit_length;
5780 : 0 : uint16_t version;
5781 : 0 : uint8_t address_size;
5782 : 0 : uint8_t segment_size;
5783 [ # # ]: 0 : if (listptr == NULL)
5784 : : {
5785 : 0 : error (0, 0, "Warning: No CU references .debug_addr after %" PRIx64,
5786 : : off);
5787 : :
5788 : : /* We will have to assume it is just addresses to the end... */
5789 [ # # ]: 0 : address_size = ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 4 : 8;
5790 : 0 : next_unitp = readendp;
5791 : 0 : printf ("Unknown CU:\n");
5792 : : }
5793 : : else
5794 : : {
5795 : 0 : Dwarf_Die cudie;
5796 [ # # ]: 0 : if (dwarf_cu_die (listptr->cu, &cudie,
5797 : : NULL, NULL, NULL, NULL,
5798 : : NULL, NULL) == NULL)
5799 : 0 : printf ("Unknown CU (%s):\n", dwarf_errmsg (-1));
5800 : : else
5801 : 0 : printf ("for CU [%6" PRIx64 "]:\n", dwarf_dieoffset (&cudie));
5802 : :
5803 [ # # ]: 0 : if (listptr->offset == off)
5804 : : {
5805 [ # # ]: 0 : address_size = listptr_address_size (listptr);
5806 : 0 : segment_size = 0;
5807 : 0 : version = 4;
5808 : :
5809 : : /* The addresses start here, but where do they end? */
5810 [ # # ]: 0 : listptr = get_listptr (&known_addrbases, idx);
5811 [ # # ]: 0 : if (listptr == NULL)
5812 : : next_unitp = readendp;
5813 [ # # ]: 0 : else if (listptr->cu->version < 5)
5814 : : {
5815 : 0 : next_unitp = start + listptr->offset;
5816 [ # # # # ]: 0 : if (listptr->offset < off || listptr->offset > data->d_size)
5817 : : {
5818 : 0 : error (0, 0,
5819 : : "Warning: Bad address base for next unit at %"
5820 : : PRIx64, off);
5821 : 0 : next_unitp = readendp;
5822 : : }
5823 : : }
5824 : : else
5825 : : {
5826 : : /* Tricky, we don't have a header for this unit, but
5827 : : there is one for the next. We will have to
5828 : : "guess" how big it is and subtract it from the
5829 : : offset (because that points after the header). */
5830 [ # # ]: 0 : unsigned int offset_size = listptr_offset_size (listptr);
5831 : 0 : Dwarf_Off next_off = (listptr->offset
5832 : 0 : - (offset_size == 4 ? 4 : 12) /* len */
5833 : : - 2 /* version */
5834 : : - 1 /* address size */
5835 : 0 : - 1); /* segment selector size */
5836 : 0 : next_unitp = start + next_off;
5837 [ # # # # ]: 0 : if (next_off < off || next_off > data->d_size)
5838 : : {
5839 : 0 : error (0, 0,
5840 : : "Warning: Couldn't calculate .debug_addr "
5841 : : " unit length at %" PRIx64, off);
5842 : 0 : next_unitp = readendp;
5843 : : }
5844 : : }
5845 : 0 : unit_length = (uint64_t) (next_unitp - readp);
5846 : :
5847 : : /* Pretend we have a header. */
5848 : 0 : printf ("\n");
5849 : 0 : printf (_(" Length: %8" PRIu64 "\n"),
5850 : : unit_length);
5851 : 0 : printf (_(" DWARF version: %8" PRIu16 "\n"), version);
5852 : 0 : printf (_(" Address size: %8" PRIu64 "\n"),
5853 : : (uint64_t) address_size);
5854 : 0 : printf (_(" Segment size: %8" PRIu64 "\n"),
5855 : : (uint64_t) segment_size);
5856 : 0 : printf ("\n");
5857 : : }
5858 : : else
5859 : : {
5860 : : /* OK, we have to parse an header first. */
5861 [ # # ]: 0 : unit_length = read_4ubyte_unaligned_inc (dbg, readp);
5862 [ # # ]: 0 : if (unlikely (unit_length == 0xffffffff))
5863 : : {
5864 [ # # ]: 0 : if (unlikely (readp > readendp - 8))
5865 : : {
5866 : 0 : invalid_data:
5867 : 0 : error (0, 0, "Invalid data");
5868 : 0 : return;
5869 : : }
5870 [ # # ]: 0 : unit_length = read_8ubyte_unaligned_inc (dbg, readp);
5871 : : }
5872 : 0 : printf ("\n");
5873 : 0 : printf (_(" Length: %8" PRIu64 "\n"),
5874 : : unit_length);
5875 : :
5876 : : /* We need at least 2-bytes (version) + 1-byte
5877 : : (addr_size) + 1-byte (segment_size) = 4 bytes to
5878 : : complete the header. And this unit cannot go beyond
5879 : : the section data. */
5880 [ # # ]: 0 : if (readp > readendp - 4
5881 [ # # ]: 0 : || unit_length < 4
5882 [ # # ]: 0 : || unit_length > (uint64_t) (readendp - readp))
5883 : 0 : goto invalid_data;
5884 : :
5885 : 0 : next_unitp = readp + unit_length;
5886 : :
5887 [ # # ]: 0 : version = read_2ubyte_unaligned_inc (dbg, readp);
5888 : 0 : printf (_(" DWARF version: %8" PRIu16 "\n"), version);
5889 : :
5890 [ # # ]: 0 : if (version != 5)
5891 : : {
5892 : 0 : error (0, 0, _("Unknown version"));
5893 : 0 : goto next_unit;
5894 : : }
5895 : :
5896 : 0 : address_size = *readp++;
5897 : 0 : printf (_(" Address size: %8" PRIu64 "\n"),
5898 : : (uint64_t) address_size);
5899 : :
5900 [ # # ]: 0 : if (address_size != 4 && address_size != 8)
5901 : : {
5902 : 0 : error (0, 0, _("unsupported address size"));
5903 : 0 : goto next_unit;
5904 : : }
5905 : :
5906 : 0 : segment_size = *readp++;
5907 : 0 : printf (_(" Segment size: %8" PRIu64 "\n"),
5908 : : (uint64_t) segment_size);
5909 : 0 : printf ("\n");
5910 : :
5911 [ # # ]: 0 : if (segment_size != 0)
5912 : : {
5913 : 0 : error (0, 0, _("unsupported segment size"));
5914 : 0 : goto next_unit;
5915 : : }
5916 : :
5917 [ # # ]: 0 : if (listptr->offset != (Dwarf_Off) (readp - start))
5918 : : {
5919 : 0 : error (0, 0, "Address index doesn't start after header");
5920 : 0 : goto next_unit;
5921 : : }
5922 : : }
5923 : : }
5924 : :
5925 : 0 : int digits = 1;
5926 : 0 : size_t addresses = (next_unitp - readp) / address_size;
5927 [ # # ]: 0 : while (addresses >= 10)
5928 : : {
5929 : 0 : ++digits;
5930 : 0 : addresses /= 10;
5931 : : }
5932 : :
5933 : 0 : unsigned int uidx = 0;
5934 : 0 : size_t index_offset = readp - (const unsigned char *) data->d_buf;
5935 : 0 : printf (" Addresses start at offset 0x%zx:\n", index_offset);
5936 : 0 : while (readp <= next_unitp - address_size)
5937 : : {
5938 [ # # # # : 0 : Dwarf_Addr addr = read_addr_unaligned_inc (address_size, dbg,
# # # # ]
5939 : : readp);
5940 : 0 : printf (" [%*u] ", digits, uidx++);
5941 : 0 : print_dwarf_addr (dwflmod, address_size, addr, addr);
5942 [ # # ]: 0 : printf ("\n");
5943 : : }
5944 : 0 : printf ("\n");
5945 : :
5946 [ # # ]: 0 : if (readp != next_unitp)
5947 [ # # ]: 0 : error (0, 0, "extra %zd bytes at end of unit",
5948 : 0 : (size_t) (next_unitp - readp));
5949 : :
5950 : 0 : next_unit:
5951 : : readp = next_unitp;
5952 : : }
5953 : : }
5954 : :
5955 : : /* Print content of DWARF .debug_aranges section. We fortunately do
5956 : : not have to know a bit about the structure of the section, libdwarf
5957 : : takes care of it. */
5958 : : static void
5959 : 0 : print_decoded_aranges_section (Ebl *ebl, GElf_Ehdr *ehdr, Elf_Scn *scn,
5960 : : GElf_Shdr *shdr, Dwarf *dbg)
5961 : : {
5962 : 0 : Dwarf_Aranges *aranges;
5963 : 0 : size_t cnt;
5964 [ # # ]: 0 : if (unlikely (dwarf_getaranges (dbg, &aranges, &cnt) != 0))
5965 : : {
5966 : 0 : error (0, 0, _("cannot get .debug_aranges content: %s"),
5967 : : dwarf_errmsg (-1));
5968 : 0 : return;
5969 : : }
5970 : :
5971 : 0 : GElf_Shdr glink_mem;
5972 : 0 : GElf_Shdr *glink;
5973 : 0 : glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link), &glink_mem);
5974 [ # # ]: 0 : if (glink == NULL)
5975 : : {
5976 : 0 : error (0, 0, _("invalid sh_link value in section %zu"),
5977 : : elf_ndxscn (scn));
5978 : 0 : return;
5979 : : }
5980 : :
5981 : 0 : printf (ngettext ("\
5982 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 " contains %zu entry:\n",
5983 : : "\
5984 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 " contains %zu entries:\n",
5985 : : cnt),
5986 : : elf_ndxscn (scn), section_name (ebl, shdr),
5987 : 0 : (uint64_t) shdr->sh_offset, cnt);
5988 : :
5989 : : /* Compute floor(log16(cnt)). */
5990 : 0 : size_t tmp = cnt;
5991 : 0 : int digits = 1;
5992 [ # # ]: 0 : while (tmp >= 16)
5993 : : {
5994 : 0 : ++digits;
5995 : 0 : tmp >>= 4;
5996 : : }
5997 : :
5998 [ # # ]: 0 : for (size_t n = 0; n < cnt; ++n)
5999 : : {
6000 : 0 : Dwarf_Arange *runp = dwarf_onearange (aranges, n);
6001 [ # # ]: 0 : if (unlikely (runp == NULL))
6002 : : {
6003 : 0 : printf ("cannot get arange %zu: %s\n", n, dwarf_errmsg (-1));
6004 : 0 : return;
6005 : : }
6006 : :
6007 : 0 : Dwarf_Addr start;
6008 : 0 : Dwarf_Word length;
6009 : 0 : Dwarf_Off offset;
6010 : :
6011 [ # # ]: 0 : if (unlikely (dwarf_getarangeinfo (runp, &start, &length, &offset) != 0))
6012 : 0 : printf (_(" [%*zu] ???\n"), digits, n);
6013 : : else
6014 : 0 : printf (_(" [%*zu] start: %0#*" PRIx64
6015 : : ", length: %5" PRIu64 ", CU DIE offset: %6"
6016 : : PRId64 "\n"),
6017 [ # # ]: 0 : digits, n, ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 10 : 18,
6018 : : (uint64_t) start, (uint64_t) length, (int64_t) offset);
6019 : : }
6020 : : }
6021 : :
6022 : :
6023 : : /* Print content of DWARF .debug_aranges section. */
6024 : : static void
6025 : 0 : print_debug_aranges_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
6026 : : Ebl *ebl, GElf_Ehdr *ehdr, Elf_Scn *scn,
6027 : : GElf_Shdr *shdr, Dwarf *dbg)
6028 : : {
6029 [ # # ]: 0 : if (decodedaranges)
6030 : : {
6031 : 0 : print_decoded_aranges_section (ebl, ehdr, scn, shdr, dbg);
6032 : 0 : return;
6033 : : }
6034 : :
6035 : 0 : Elf_Data *data = get_debug_elf_data (dbg, ebl, IDX_debug_aranges, scn);
6036 [ # # ]: 0 : if (data == NULL)
6037 : : return;
6038 : :
6039 : 0 : printf (_("\
6040 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
6041 : : elf_ndxscn (scn), section_name (ebl, shdr),
6042 : 0 : (uint64_t) shdr->sh_offset);
6043 : :
6044 : 0 : const unsigned char *readp = data->d_buf;
6045 : 0 : const unsigned char *readendp = readp + data->d_size;
6046 : :
6047 [ # # ]: 0 : while (readp < readendp)
6048 : : {
6049 : 0 : const unsigned char *hdrstart = readp;
6050 : 0 : size_t start_offset = hdrstart - (const unsigned char *) data->d_buf;
6051 : :
6052 : 0 : printf (_("\nTable at offset %zu:\n"), start_offset);
6053 [ # # ]: 0 : if (readp + 4 > readendp)
6054 : : {
6055 : 0 : invalid_data:
6056 : 0 : error (0, 0, _("invalid data in section [%zu] '%s'"),
6057 : : elf_ndxscn (scn), section_name (ebl, shdr));
6058 : 0 : return;
6059 : : }
6060 : :
6061 [ # # ]: 0 : Dwarf_Word length = read_4ubyte_unaligned_inc (dbg, readp);
6062 : 0 : unsigned int length_bytes = 4;
6063 [ # # ]: 0 : if (length == DWARF3_LENGTH_64_BIT)
6064 : : {
6065 [ # # ]: 0 : if (readp + 8 > readendp)
6066 : 0 : goto invalid_data;
6067 [ # # ]: 0 : length = read_8ubyte_unaligned_inc (dbg, readp);
6068 : 0 : length_bytes = 8;
6069 : : }
6070 : :
6071 : 0 : const unsigned char *nexthdr = readp + length;
6072 : 0 : printf (_("\n Length: %6" PRIu64 "\n"),
6073 : : (uint64_t) length);
6074 : :
6075 [ # # ]: 0 : if (unlikely (length > (size_t) (readendp - readp)))
6076 : 0 : goto invalid_data;
6077 : :
6078 [ # # ]: 0 : if (length == 0)
6079 : 0 : continue;
6080 : :
6081 [ # # ]: 0 : if (readp + 2 > readendp)
6082 : 0 : goto invalid_data;
6083 [ # # ]: 0 : uint_fast16_t version = read_2ubyte_unaligned_inc (dbg, readp);
6084 : 0 : printf (_(" DWARF version: %6" PRIuFAST16 "\n"),
6085 : : version);
6086 [ # # ]: 0 : if (version != 2)
6087 : : {
6088 : 0 : error (0, 0, _("unsupported aranges version"));
6089 : 0 : goto next_table;
6090 : : }
6091 : :
6092 : 0 : Dwarf_Word offset;
6093 [ # # ]: 0 : if (readp + length_bytes > readendp)
6094 : 0 : goto invalid_data;
6095 [ # # ]: 0 : if (length_bytes == 8)
6096 [ # # ]: 0 : offset = read_8ubyte_unaligned_inc (dbg, readp);
6097 : : else
6098 [ # # ]: 0 : offset = read_4ubyte_unaligned_inc (dbg, readp);
6099 : 0 : printf (_(" CU offset: %6" PRIx64 "\n"),
6100 : : (uint64_t) offset);
6101 : :
6102 [ # # ]: 0 : if (readp + 1 > readendp)
6103 : 0 : goto invalid_data;
6104 : 0 : unsigned int address_size = *readp++;
6105 : 0 : printf (_(" Address size: %6" PRIu64 "\n"),
6106 : : (uint64_t) address_size);
6107 [ # # ]: 0 : if (address_size != 4 && address_size != 8)
6108 : : {
6109 : 0 : error (0, 0, _("unsupported address size"));
6110 : 0 : goto next_table;
6111 : : }
6112 : :
6113 [ # # ]: 0 : if (readp + 1 > readendp)
6114 : 0 : goto invalid_data;
6115 : 0 : unsigned int segment_size = *readp++;
6116 : 0 : printf (_(" Segment size: %6" PRIu64 "\n\n"),
6117 : : (uint64_t) segment_size);
6118 [ # # # # ]: 0 : if (segment_size != 0 && segment_size != 4 && segment_size != 8)
6119 : : {
6120 : 0 : error (0, 0, _("unsupported segment size"));
6121 : 0 : goto next_table;
6122 : : }
6123 : :
6124 : : /* Round the address to the next multiple of 2*address_size. */
6125 : 0 : readp += ((2 * address_size - ((readp - hdrstart) % (2 * address_size)))
6126 : 0 : % (2 * address_size));
6127 : :
6128 : 0 : while (readp < nexthdr)
6129 : : {
6130 : 0 : Dwarf_Word range_address;
6131 : 0 : Dwarf_Word range_length;
6132 : 0 : Dwarf_Word segment = 0;
6133 [ # # ]: 0 : if (readp + 2 * address_size + segment_size > readendp)
6134 : 0 : goto invalid_data;
6135 [ # # ]: 0 : if (address_size == 4)
6136 : : {
6137 [ # # ]: 0 : range_address = read_4ubyte_unaligned_inc (dbg, readp);
6138 [ # # ]: 0 : range_length = read_4ubyte_unaligned_inc (dbg, readp);
6139 : : }
6140 : : else
6141 : : {
6142 [ # # ]: 0 : range_address = read_8ubyte_unaligned_inc (dbg, readp);
6143 [ # # ]: 0 : range_length = read_8ubyte_unaligned_inc (dbg, readp);
6144 : : }
6145 : :
6146 [ # # ]: 0 : if (segment_size == 4)
6147 [ # # ]: 0 : segment = read_4ubyte_unaligned_inc (dbg, readp);
6148 [ # # ]: 0 : else if (segment_size == 8)
6149 [ # # ]: 0 : segment = read_8ubyte_unaligned_inc (dbg, readp);
6150 : :
6151 [ # # ]: 0 : if (range_address == 0 && range_length == 0 && segment == 0)
6152 : : break;
6153 : :
6154 : 0 : printf (" ");
6155 : 0 : print_dwarf_addr (dwflmod, address_size, range_address,
6156 : : range_address);
6157 : 0 : printf ("..");
6158 : 0 : print_dwarf_addr (dwflmod, address_size,
6159 : 0 : range_address + range_length - 1,
6160 : : range_length);
6161 [ # # ]: 0 : if (segment_size != 0)
6162 : 0 : printf (" (%" PRIx64 ")\n", (uint64_t) segment);
6163 : : else
6164 [ # # ]: 0 : printf ("\n");
6165 : : }
6166 : :
6167 : 0 : next_table:
6168 [ # # ]: 0 : if (readp != nexthdr)
6169 : : {
6170 : 0 : size_t padding = nexthdr - readp;
6171 : 0 : printf (_(" %zu padding bytes\n"), padding);
6172 : 0 : readp = nexthdr;
6173 : : }
6174 : : }
6175 : : }
6176 : :
6177 : :
6178 : : static bool is_split_dwarf (Dwarf *dbg, uint64_t *id, Dwarf_CU **split_cu);
6179 : :
6180 : : /* Returns true and sets cu and cu_base if the given Dwarf is a split
6181 : : DWARF (.dwo) file. */
6182 : : static bool
6183 : 0 : split_dwarf_cu_base (Dwarf *dbg, Dwarf_CU **cu, Dwarf_Addr *cu_base)
6184 : : {
6185 : 0 : uint64_t id;
6186 [ # # ]: 0 : if (is_split_dwarf (dbg, &id, cu))
6187 : : {
6188 : 0 : Dwarf_Die cudie;
6189 [ # # ]: 0 : if (dwarf_cu_info (*cu, NULL, NULL, &cudie, NULL, NULL, NULL, NULL) == 0)
6190 : : {
6191 : 0 : *cu_base = cudie_base (&cudie);
6192 : 0 : return true;
6193 : : }
6194 : : }
6195 : : return false;
6196 : : }
6197 : :
6198 : : /* Print content of DWARF .debug_rnglists section. */
6199 : : static void
6200 : 0 : print_debug_rnglists_section (Dwfl_Module *dwflmod,
6201 : : Ebl *ebl,
6202 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
6203 : : Elf_Scn *scn, GElf_Shdr *shdr,
6204 : : Dwarf *dbg __attribute__((unused)))
6205 : : {
6206 : 0 : Elf_Data *data = get_debug_elf_data (dbg, ebl, IDX_debug_rnglists, scn);
6207 [ # # ]: 0 : if (data == NULL)
6208 : 0 : return;
6209 : :
6210 : 0 : printf (_("\
6211 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
6212 : : elf_ndxscn (scn), section_name (ebl, shdr),
6213 : 0 : (uint64_t) shdr->sh_offset);
6214 : :
6215 : : /* For the listptr to get the base address/CU. */
6216 : 0 : sort_listptr (&known_rnglistptr, "rnglistptr");
6217 : 0 : size_t listptr_idx = 0;
6218 : :
6219 : 0 : const unsigned char *readp = data->d_buf;
6220 : 0 : const unsigned char *const dataend = ((unsigned char *) data->d_buf
6221 : 0 : + data->d_size);
6222 [ # # ]: 0 : while (readp < dataend)
6223 : : {
6224 [ # # ]: 0 : if (unlikely (readp > dataend - 4))
6225 : : {
6226 : 0 : invalid_data:
6227 : 0 : error (0, 0, _("invalid data in section [%zu] '%s'"),
6228 : : elf_ndxscn (scn), section_name (ebl, shdr));
6229 : 0 : return;
6230 : : }
6231 : :
6232 : 0 : ptrdiff_t offset = readp - (unsigned char *) data->d_buf;
6233 : 0 : printf (_("Table at Offset 0x%" PRIx64 ":\n\n"),
6234 : : (uint64_t) offset);
6235 : :
6236 [ # # ]: 0 : uint64_t unit_length = read_4ubyte_unaligned_inc (dbg, readp);
6237 : 0 : unsigned int offset_size = 4;
6238 [ # # ]: 0 : if (unlikely (unit_length == 0xffffffff))
6239 : : {
6240 [ # # ]: 0 : if (unlikely (readp > dataend - 8))
6241 : 0 : goto invalid_data;
6242 : :
6243 [ # # ]: 0 : unit_length = read_8ubyte_unaligned_inc (dbg, readp);
6244 : 0 : offset_size = 8;
6245 : : }
6246 : 0 : printf (_(" Length: %8" PRIu64 "\n"), unit_length);
6247 : :
6248 : : /* We need at least 2-bytes + 1-byte + 1-byte + 4-bytes = 8
6249 : : bytes to complete the header. And this unit cannot go beyond
6250 : : the section data. */
6251 [ # # ]: 0 : if (readp > dataend - 8
6252 [ # # ]: 0 : || unit_length < 8
6253 [ # # ]: 0 : || unit_length > (uint64_t) (dataend - readp))
6254 : 0 : goto invalid_data;
6255 : :
6256 : 0 : const unsigned char *nexthdr = readp + unit_length;
6257 : :
6258 [ # # ]: 0 : uint16_t version = read_2ubyte_unaligned_inc (dbg, readp);
6259 : 0 : printf (_(" DWARF version: %8" PRIu16 "\n"), version);
6260 : :
6261 [ # # ]: 0 : if (version != 5)
6262 : : {
6263 : 0 : error (0, 0, _("Unknown version"));
6264 : 0 : goto next_table;
6265 : : }
6266 : :
6267 : 0 : uint8_t address_size = *readp++;
6268 : 0 : printf (_(" Address size: %8" PRIu64 "\n"),
6269 : : (uint64_t) address_size);
6270 : :
6271 [ # # ]: 0 : if (address_size != 4 && address_size != 8)
6272 : : {
6273 : 0 : error (0, 0, _("unsupported address size"));
6274 : 0 : goto next_table;
6275 : : }
6276 : :
6277 : 0 : uint8_t segment_size = *readp++;
6278 : 0 : printf (_(" Segment size: %8" PRIu64 "\n"),
6279 : : (uint64_t) segment_size);
6280 : :
6281 [ # # # # ]: 0 : if (segment_size != 0 && segment_size != 4 && segment_size != 8)
6282 : : {
6283 : 0 : error (0, 0, _("unsupported segment size"));
6284 : 0 : goto next_table;
6285 : : }
6286 : :
6287 [ # # ]: 0 : uint32_t offset_entry_count = read_4ubyte_unaligned_inc (dbg, readp);
6288 : 0 : printf (_(" Offset entries: %8" PRIu64 "\n"),
6289 : : (uint64_t) offset_entry_count);
6290 : :
6291 : : /* We need the CU that uses this unit to get the initial base address. */
6292 : 0 : Dwarf_Addr cu_base = 0;
6293 : 0 : struct Dwarf_CU *cu = NULL;
6294 [ # # ]: 0 : if (listptr_cu (&known_rnglistptr, &listptr_idx,
6295 : : (Dwarf_Off) offset,
6296 : 0 : (Dwarf_Off) (nexthdr - (unsigned char *) data->d_buf),
6297 : : &cu_base, &cu)
6298 [ # # ]: 0 : || split_dwarf_cu_base (dbg, &cu, &cu_base))
6299 : 0 : {
6300 : 0 : Dwarf_Die cudie;
6301 [ # # ]: 0 : if (dwarf_cu_die (cu, &cudie,
6302 : : NULL, NULL, NULL, NULL,
6303 : : NULL, NULL) == NULL)
6304 : 0 : printf (_(" Unknown CU base: "));
6305 : : else
6306 : 0 : printf (_(" CU [%6" PRIx64 "] base: "),
6307 : : dwarf_dieoffset (&cudie));
6308 : 0 : print_dwarf_addr (dwflmod, address_size, cu_base, cu_base);
6309 : 0 : printf ("\n");
6310 : : }
6311 : : else
6312 : 0 : printf (_(" Not associated with a CU.\n"));
6313 : :
6314 : 0 : printf ("\n");
6315 : :
6316 : 0 : const unsigned char *offset_array_start = readp;
6317 [ # # ]: 0 : if (offset_entry_count > 0)
6318 : : {
6319 : 0 : uint64_t max_entries = (unit_length - 8) / offset_size;
6320 [ # # ]: 0 : if (offset_entry_count > max_entries)
6321 : : {
6322 : 0 : error (0, 0,
6323 : 0 : _("too many offset entries for unit length"));
6324 : 0 : offset_entry_count = max_entries;
6325 : : }
6326 : :
6327 : 0 : printf (_(" Offsets starting at 0x%" PRIx64 ":\n"),
6328 : : (uint64_t) (offset_array_start
6329 : 0 : - (unsigned char *) data->d_buf));
6330 [ # # ]: 0 : for (uint32_t idx = 0; idx < offset_entry_count; idx++)
6331 : : {
6332 : 0 : printf (" [%6" PRIu32 "] ", idx);
6333 [ # # ]: 0 : if (offset_size == 4)
6334 : : {
6335 [ # # ]: 0 : uint32_t off = read_4ubyte_unaligned_inc (dbg, readp);
6336 : 0 : printf ("0x%" PRIx32 "\n", off);
6337 : : }
6338 : : else
6339 : : {
6340 [ # # ]: 0 : uint64_t off = read_8ubyte_unaligned_inc (dbg, readp);
6341 : 0 : printf ("0x%" PRIx64 "\n", off);
6342 : : }
6343 : : }
6344 : 0 : printf ("\n");
6345 : : }
6346 : :
6347 : 0 : Dwarf_Addr base = cu_base;
6348 : 0 : bool start_of_list = true;
6349 : 0 : while (readp < nexthdr)
6350 : : {
6351 : 0 : uint8_t kind = *readp++;
6352 : 0 : uint64_t op1, op2;
6353 : :
6354 : : /* Skip padding. */
6355 [ # # ]: 0 : if (start_of_list && kind == DW_RLE_end_of_list)
6356 : 0 : continue;
6357 : :
6358 [ # # ]: 0 : if (start_of_list)
6359 : : {
6360 : 0 : base = cu_base;
6361 : 0 : printf (" Offset: %" PRIx64 ", Index: %" PRIx64 "\n",
6362 : 0 : (uint64_t) (readp - (unsigned char *) data->d_buf - 1),
6363 : 0 : (uint64_t) (readp - offset_array_start - 1));
6364 : 0 : start_of_list = false;
6365 : : }
6366 : :
6367 : 0 : printf (" %s", dwarf_range_list_encoding_name (kind));
6368 [ # # # # : 0 : switch (kind)
# # # #
# ]
6369 : : {
6370 : 0 : case DW_RLE_end_of_list:
6371 : 0 : start_of_list = true;
6372 : 0 : printf ("\n\n");
6373 : : break;
6374 : :
6375 : 0 : case DW_RLE_base_addressx:
6376 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
6377 : : {
6378 : 0 : invalid_range:
6379 : 0 : error (0, 0, _("invalid range list data"));
6380 : 0 : goto next_table;
6381 : : }
6382 : 0 : get_uleb128 (op1, readp, nexthdr);
6383 : 0 : printf (" %" PRIx64 "\n", op1);
6384 [ # # ]: 0 : if (! print_unresolved_addresses)
6385 : : {
6386 : 0 : Dwarf_Addr addr;
6387 [ # # ]: 0 : if (get_indexed_addr (cu, op1, &addr) != 0)
6388 : 0 : printf (" ???\n");
6389 : : else
6390 : : {
6391 : 0 : printf (" ");
6392 : 0 : print_dwarf_addr (dwflmod, address_size, addr, addr);
6393 : 0 : printf ("\n");
6394 : : }
6395 : : }
6396 : : break;
6397 : :
6398 : 0 : case DW_RLE_startx_endx:
6399 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
6400 : 0 : goto invalid_range;
6401 : 0 : get_uleb128 (op1, readp, nexthdr);
6402 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
6403 : 0 : goto invalid_range;
6404 : 0 : get_uleb128 (op2, readp, nexthdr);
6405 : 0 : printf (" %" PRIx64 ", %" PRIx64 "\n", op1, op2);
6406 [ # # ]: 0 : if (! print_unresolved_addresses)
6407 : : {
6408 : 0 : Dwarf_Addr addr1;
6409 : 0 : Dwarf_Addr addr2;
6410 [ # # ]: 0 : if (get_indexed_addr (cu, op1, &addr1) != 0
6411 [ # # ]: 0 : || get_indexed_addr (cu, op2, &addr2) != 0)
6412 : : {
6413 : 0 : printf (" ???..\n");
6414 : 0 : printf (" ???\n");
6415 : : }
6416 : : else
6417 : : {
6418 : 0 : printf (" ");
6419 : 0 : print_dwarf_addr (dwflmod, address_size, addr1, addr1);
6420 : 0 : printf ("..\n ");
6421 : 0 : print_dwarf_addr (dwflmod, address_size,
6422 : : addr2 - 1, addr2);
6423 : 0 : printf ("\n");
6424 : : }
6425 : : }
6426 : : break;
6427 : :
6428 : 0 : case DW_RLE_startx_length:
6429 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
6430 : 0 : goto invalid_range;
6431 : 0 : get_uleb128 (op1, readp, nexthdr);
6432 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
6433 : 0 : goto invalid_range;
6434 : 0 : get_uleb128 (op2, readp, nexthdr);
6435 : 0 : printf (" %" PRIx64 ", %" PRIx64 "\n", op1, op2);
6436 [ # # ]: 0 : if (! print_unresolved_addresses)
6437 : : {
6438 : 0 : Dwarf_Addr addr1;
6439 : 0 : Dwarf_Addr addr2;
6440 [ # # ]: 0 : if (get_indexed_addr (cu, op1, &addr1) != 0)
6441 : : {
6442 : 0 : printf (" ???..\n");
6443 : 0 : printf (" ???\n");
6444 : : }
6445 : : else
6446 : : {
6447 : 0 : addr2 = addr1 + op2;
6448 : 0 : printf (" ");
6449 : 0 : print_dwarf_addr (dwflmod, address_size, addr1, addr1);
6450 : 0 : printf ("..\n ");
6451 : 0 : print_dwarf_addr (dwflmod, address_size,
6452 : : addr2 - 1, addr2);
6453 : 0 : printf ("\n");
6454 : : }
6455 : : }
6456 : : break;
6457 : :
6458 : 0 : case DW_RLE_offset_pair:
6459 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
6460 : 0 : goto invalid_range;
6461 : 0 : get_uleb128 (op1, readp, nexthdr);
6462 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
6463 : 0 : goto invalid_range;
6464 : 0 : get_uleb128 (op2, readp, nexthdr);
6465 : 0 : printf (" %" PRIx64 ", %" PRIx64 "\n", op1, op2);
6466 [ # # ]: 0 : if (! print_unresolved_addresses)
6467 : : {
6468 : 0 : op1 += base;
6469 : 0 : op2 += base;
6470 : 0 : printf (" ");
6471 : 0 : print_dwarf_addr (dwflmod, address_size, op1, op1);
6472 : 0 : printf ("..\n ");
6473 : 0 : print_dwarf_addr (dwflmod, address_size, op2 - 1, op2);
6474 : 0 : printf ("\n");
6475 : : }
6476 : : break;
6477 : :
6478 : 0 : case DW_RLE_base_address:
6479 [ # # ]: 0 : if (address_size == 4)
6480 : : {
6481 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 4)
6482 : 0 : goto invalid_range;
6483 [ # # ]: 0 : op1 = read_4ubyte_unaligned_inc (dbg, readp);
6484 : : }
6485 : : else
6486 : : {
6487 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 8)
6488 : 0 : goto invalid_range;
6489 [ # # ]: 0 : op1 = read_8ubyte_unaligned_inc (dbg, readp);
6490 : : }
6491 : 0 : base = op1;
6492 : 0 : printf (" 0x%" PRIx64 "\n", base);
6493 [ # # ]: 0 : if (! print_unresolved_addresses)
6494 : : {
6495 : 0 : printf (" ");
6496 : 0 : print_dwarf_addr (dwflmod, address_size, base, base);
6497 : 0 : printf ("\n");
6498 : : }
6499 : : break;
6500 : :
6501 : 0 : case DW_RLE_start_end:
6502 [ # # ]: 0 : if (address_size == 4)
6503 : : {
6504 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 8)
6505 : 0 : goto invalid_range;
6506 [ # # ]: 0 : op1 = read_4ubyte_unaligned_inc (dbg, readp);
6507 [ # # ]: 0 : op2 = read_4ubyte_unaligned_inc (dbg, readp);
6508 : : }
6509 : : else
6510 : : {
6511 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 16)
6512 : 0 : goto invalid_range;
6513 [ # # ]: 0 : op1 = read_8ubyte_unaligned_inc (dbg, readp);
6514 [ # # ]: 0 : op2 = read_8ubyte_unaligned_inc (dbg, readp);
6515 : : }
6516 : 0 : printf (" 0x%" PRIx64 "..0x%" PRIx64 "\n", op1, op2);
6517 [ # # ]: 0 : if (! print_unresolved_addresses)
6518 : : {
6519 : 0 : printf (" ");
6520 : 0 : print_dwarf_addr (dwflmod, address_size, op1, op1);
6521 : 0 : printf ("..\n ");
6522 : 0 : print_dwarf_addr (dwflmod, address_size, op2 - 1, op2);
6523 : 0 : printf ("\n");
6524 : : }
6525 : : break;
6526 : :
6527 : 0 : case DW_RLE_start_length:
6528 [ # # ]: 0 : if (address_size == 4)
6529 : : {
6530 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 4)
6531 : 0 : goto invalid_range;
6532 [ # # ]: 0 : op1 = read_4ubyte_unaligned_inc (dbg, readp);
6533 : : }
6534 : : else
6535 : : {
6536 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 8)
6537 : 0 : goto invalid_range;
6538 [ # # ]: 0 : op1 = read_8ubyte_unaligned_inc (dbg, readp);
6539 : : }
6540 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
6541 : 0 : goto invalid_range;
6542 : 0 : get_uleb128 (op2, readp, nexthdr);
6543 : 0 : printf (" 0x%" PRIx64 ", %" PRIx64 "\n", op1, op2);
6544 [ # # ]: 0 : if (! print_unresolved_addresses)
6545 : : {
6546 : 0 : op2 = op1 + op2;
6547 : 0 : printf (" ");
6548 : 0 : print_dwarf_addr (dwflmod, address_size, op1, op1);
6549 : 0 : printf ("..\n ");
6550 : 0 : print_dwarf_addr (dwflmod, address_size, op2 - 1, op2);
6551 [ # # ]: 0 : printf ("\n");
6552 : : }
6553 : : break;
6554 : :
6555 : 0 : default:
6556 : 0 : goto invalid_range;
6557 : : }
6558 : : }
6559 : :
6560 : 0 : next_table:
6561 [ # # ]: 0 : if (readp != nexthdr)
6562 : : {
6563 : 0 : size_t padding = nexthdr - readp;
6564 : 0 : printf (_(" %zu padding bytes\n\n"), padding);
6565 : 0 : readp = nexthdr;
6566 : : }
6567 : : }
6568 : : }
6569 : :
6570 : : /* Print content of DWARF .debug_ranges section. */
6571 : : static void
6572 : 0 : print_debug_ranges_section (Dwfl_Module *dwflmod,
6573 : : Ebl *ebl, GElf_Ehdr *ehdr,
6574 : : Elf_Scn *scn, GElf_Shdr *shdr,
6575 : : Dwarf *dbg)
6576 : : {
6577 : 0 : Elf_Data *data = get_debug_elf_data (dbg, ebl, IDX_debug_ranges, scn);
6578 [ # # ]: 0 : if (data == NULL)
6579 : 0 : return;
6580 : :
6581 : 0 : printf (_("\
6582 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
6583 : : elf_ndxscn (scn), section_name (ebl, shdr),
6584 : 0 : (uint64_t) shdr->sh_offset);
6585 : :
6586 : 0 : sort_listptr (&known_rangelistptr, "rangelistptr");
6587 : 0 : size_t listptr_idx = 0;
6588 : :
6589 [ # # ]: 0 : uint_fast8_t address_size = ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 4 : 8;
6590 : :
6591 : 0 : bool first = true;
6592 : 0 : Dwarf_Addr base = 0;
6593 : 0 : unsigned char *const endp = (unsigned char *) data->d_buf + data->d_size;
6594 : 0 : unsigned char *readp = data->d_buf;
6595 : 0 : Dwarf_CU *last_cu = NULL;
6596 [ # # ]: 0 : while (readp < endp)
6597 : : {
6598 : 0 : ptrdiff_t offset = readp - (unsigned char *) data->d_buf;
6599 : 0 : Dwarf_CU *cu = last_cu;
6600 : :
6601 [ # # # # ]: 0 : if (first && skip_listptr_hole (&known_rangelistptr, &listptr_idx,
6602 : : &address_size, NULL, &base, &cu,
6603 : : offset, &readp, endp, NULL))
6604 : 0 : continue;
6605 : :
6606 [ # # ]: 0 : if (last_cu != cu)
6607 : : {
6608 : 0 : Dwarf_Die cudie;
6609 [ # # ]: 0 : if (dwarf_cu_die (cu, &cudie,
6610 : : NULL, NULL, NULL, NULL,
6611 : : NULL, NULL) == NULL)
6612 : 0 : printf (_("\n Unknown CU base: "));
6613 : : else
6614 : 0 : printf (_("\n CU [%6" PRIx64 "] base: "),
6615 : : dwarf_dieoffset (&cudie));
6616 : 0 : print_dwarf_addr (dwflmod, address_size, base, base);
6617 : 0 : printf ("\n");
6618 : : }
6619 : 0 : last_cu = cu;
6620 : :
6621 [ # # ]: 0 : if (unlikely (data->d_size - offset < (size_t) address_size * 2))
6622 : : {
6623 : 0 : printf (_(" [%6tx] <INVALID DATA>\n"), offset);
6624 : 0 : break;
6625 : : }
6626 : :
6627 : 0 : Dwarf_Addr begin;
6628 : 0 : Dwarf_Addr end;
6629 [ # # ]: 0 : if (address_size == 8)
6630 : : {
6631 [ # # ]: 0 : begin = read_8ubyte_unaligned_inc (dbg, readp);
6632 [ # # ]: 0 : end = read_8ubyte_unaligned_inc (dbg, readp);
6633 : : }
6634 : : else
6635 : : {
6636 [ # # ]: 0 : begin = read_4ubyte_unaligned_inc (dbg, readp);
6637 [ # # ]: 0 : end = read_4ubyte_unaligned_inc (dbg, readp);
6638 [ # # ]: 0 : if (begin == (Dwarf_Addr) (uint32_t) -1)
6639 : : begin = (Dwarf_Addr) -1l;
6640 : : }
6641 : :
6642 [ # # ]: 0 : if (begin == (Dwarf_Addr) -1l) /* Base address entry. */
6643 : : {
6644 [ # # ]: 0 : if (first)
6645 : 0 : printf (" [%6tx] ", offset);
6646 : : else
6647 : 0 : printf (" ");
6648 : 0 : puts (_("base address"));
6649 : 0 : printf (" ");
6650 : 0 : print_dwarf_addr (dwflmod, address_size, end, end);
6651 : 0 : printf ("\n");
6652 : 0 : base = end;
6653 : 0 : first = false;
6654 : : }
6655 [ # # ]: 0 : else if (begin == 0 && end == 0) /* End of list entry. */
6656 : : {
6657 [ # # ]: 0 : if (first)
6658 : 0 : printf (_(" [%6tx] empty list\n"), offset);
6659 : : first = true;
6660 : : }
6661 : : else
6662 : : {
6663 : : /* We have an address range entry. */
6664 [ # # ]: 0 : if (first) /* First address range entry in a list. */
6665 : 0 : printf (" [%6tx] ", offset);
6666 : : else
6667 : 0 : printf (" ");
6668 : :
6669 : 0 : printf ("range %" PRIx64 ", %" PRIx64 "\n", begin, end);
6670 [ # # ]: 0 : if (! print_unresolved_addresses)
6671 : : {
6672 : 0 : printf (" ");
6673 : 0 : print_dwarf_addr (dwflmod, address_size, base + begin,
6674 : : base + begin);
6675 : 0 : printf ("..\n ");
6676 : 0 : print_dwarf_addr (dwflmod, address_size,
6677 : : base + end - 1, base + end);
6678 : 0 : printf ("\n");
6679 : : }
6680 : :
6681 : : first = false;
6682 : : }
6683 : : }
6684 : : }
6685 : :
6686 : : #define REGNAMESZ 16
6687 : : static const char *
6688 : 0 : register_info (Ebl *ebl, unsigned int regno, const Ebl_Register_Location *loc,
6689 : : char name[REGNAMESZ], int *bits, int *type)
6690 : : {
6691 : 0 : const char *set;
6692 : 0 : const char *pfx;
6693 : 0 : int ignore;
6694 [ # # # # ]: 0 : ssize_t n = ebl_register_info (ebl, regno, name, REGNAMESZ, &pfx, &set,
6695 : : bits ?: &ignore, type ?: &ignore);
6696 [ # # ]: 0 : if (n <= 0)
6697 : : {
6698 [ # # ]: 0 : if (loc != NULL)
6699 : 0 : snprintf (name, REGNAMESZ, "reg%u", loc->regno);
6700 : : else
6701 : 0 : snprintf (name, REGNAMESZ, "??? 0x%x", regno);
6702 [ # # ]: 0 : if (bits != NULL)
6703 [ # # ]: 0 : *bits = loc != NULL ? loc->bits : 0;
6704 [ # # ]: 0 : if (type != NULL)
6705 : 0 : *type = DW_ATE_unsigned;
6706 : 0 : set = "??? unrecognized";
6707 : : }
6708 : : else
6709 : : {
6710 [ # # # # ]: 0 : if (bits != NULL && *bits <= 0)
6711 [ # # ]: 0 : *bits = loc != NULL ? loc->bits : 0;
6712 [ # # # # ]: 0 : if (type != NULL && *type == DW_ATE_void)
6713 : 0 : *type = DW_ATE_unsigned;
6714 : :
6715 : : }
6716 : 0 : return set;
6717 : : }
6718 : :
6719 : : static const unsigned char *
6720 : 0 : read_encoded (unsigned int encoding, const unsigned char *readp,
6721 : : const unsigned char *const endp, uint64_t *res, Dwarf *dbg)
6722 : : {
6723 [ # # ]: 0 : if ((encoding & 0xf) == DW_EH_PE_absptr)
6724 : 0 : encoding = gelf_getclass (dbg->elf) == ELFCLASS32
6725 [ # # ]: 0 : ? DW_EH_PE_udata4 : DW_EH_PE_udata8;
6726 : :
6727 [ # # # # : 0 : switch (encoding & 0xf)
# # # #
# ]
6728 : : {
6729 : 0 : case DW_EH_PE_uleb128:
6730 [ # # ]: 0 : if (readp >= endp)
6731 : 0 : goto invalid;
6732 : 0 : get_uleb128 (*res, readp, endp);
6733 : 0 : break;
6734 : 0 : case DW_EH_PE_sleb128:
6735 [ # # ]: 0 : if (readp >= endp)
6736 : 0 : goto invalid;
6737 : 0 : get_sleb128 (*res, readp, endp);
6738 : 0 : break;
6739 : 0 : case DW_EH_PE_udata2:
6740 [ # # ]: 0 : if (readp + 2 > endp)
6741 : 0 : goto invalid;
6742 [ # # ]: 0 : *res = read_2ubyte_unaligned_inc (dbg, readp);
6743 : 0 : break;
6744 : 0 : case DW_EH_PE_udata4:
6745 [ # # ]: 0 : if (readp + 4 > endp)
6746 : 0 : goto invalid;
6747 [ # # ]: 0 : *res = read_4ubyte_unaligned_inc (dbg, readp);
6748 : 0 : break;
6749 : 0 : case DW_EH_PE_udata8:
6750 [ # # ]: 0 : if (readp + 8 > endp)
6751 : 0 : goto invalid;
6752 [ # # ]: 0 : *res = read_8ubyte_unaligned_inc (dbg, readp);
6753 : 0 : break;
6754 : 0 : case DW_EH_PE_sdata2:
6755 [ # # ]: 0 : if (readp + 2 > endp)
6756 : 0 : goto invalid;
6757 [ # # ]: 0 : *res = read_2sbyte_unaligned_inc (dbg, readp);
6758 : 0 : break;
6759 : 0 : case DW_EH_PE_sdata4:
6760 [ # # ]: 0 : if (readp + 4 > endp)
6761 : 0 : goto invalid;
6762 [ # # ]: 0 : *res = read_4sbyte_unaligned_inc (dbg, readp);
6763 : 0 : break;
6764 : 0 : case DW_EH_PE_sdata8:
6765 [ # # ]: 0 : if (readp + 8 > endp)
6766 : 0 : goto invalid;
6767 [ # # ]: 0 : *res = read_8sbyte_unaligned_inc (dbg, readp);
6768 : 0 : break;
6769 : : default:
6770 : 0 : invalid:
6771 : 0 : error (1, 0,
6772 : 0 : _("invalid encoding"));
6773 : : }
6774 : :
6775 : 0 : return readp;
6776 : : }
6777 : :
6778 : : static const char *
6779 : 0 : regname (Ebl *ebl, unsigned int regno, char *regnamebuf)
6780 : : {
6781 : 0 : register_info (ebl, regno, NULL, regnamebuf, NULL, NULL);
6782 : :
6783 : 0 : return regnamebuf;
6784 : : }
6785 : :
6786 : : static void
6787 : 0 : print_cfa_program (const unsigned char *readp, const unsigned char *const endp,
6788 : : Dwarf_Word vma_base, unsigned int code_align,
6789 : : int data_align,
6790 : : unsigned int version, unsigned int ptr_size,
6791 : : unsigned int encoding,
6792 : : Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr, Dwarf *dbg)
6793 : : {
6794 : 0 : char regnamebuf[REGNAMESZ];
6795 : :
6796 : 0 : puts ("\n Program:");
6797 : 0 : Dwarf_Word pc = vma_base;
6798 : 0 : while (readp < endp)
6799 : : {
6800 : 0 : unsigned int opcode = *readp++;
6801 : :
6802 [ # # ]: 0 : if (opcode < DW_CFA_advance_loc)
6803 : : /* Extended opcode. */
6804 [ # # # # : 0 : switch (opcode)
# # # # #
# # # # #
# # # # #
# # # # #
# # # ]
6805 : : {
6806 : 0 : uint64_t op1;
6807 : 0 : int64_t sop1;
6808 : 0 : uint64_t op2;
6809 : 0 : int64_t sop2;
6810 : :
6811 : 0 : case DW_CFA_nop:
6812 : 0 : puts (" nop");
6813 : 0 : break;
6814 : 0 : case DW_CFA_set_loc:
6815 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
6816 : 0 : goto invalid;
6817 : 0 : readp = read_encoded (encoding, readp, endp, &op1, dbg);
6818 : 0 : printf (" set_loc %#" PRIx64 " to %#" PRIx64 "\n",
6819 : 0 : op1, pc = vma_base + op1);
6820 : : break;
6821 : 0 : case DW_CFA_advance_loc1:
6822 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
6823 : 0 : goto invalid;
6824 : 0 : printf (" advance_loc1 %u to %#" PRIx64 "\n",
6825 : 0 : *readp, pc += *readp * code_align);
6826 : 0 : ++readp;
6827 : 0 : break;
6828 : 0 : case DW_CFA_advance_loc2:
6829 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 2)
6830 : 0 : goto invalid;
6831 [ # # ]: 0 : op1 = read_2ubyte_unaligned_inc (dbg, readp);
6832 : 0 : printf (" advance_loc2 %" PRIu64 " to %#" PRIx64 "\n",
6833 : 0 : op1, pc += op1 * code_align);
6834 : : break;
6835 : 0 : case DW_CFA_advance_loc4:
6836 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 4)
6837 : 0 : goto invalid;
6838 [ # # ]: 0 : op1 = read_4ubyte_unaligned_inc (dbg, readp);
6839 : 0 : printf (" advance_loc4 %" PRIu64 " to %#" PRIx64 "\n",
6840 : 0 : op1, pc += op1 * code_align);
6841 : : break;
6842 : 0 : case DW_CFA_offset_extended:
6843 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
6844 : 0 : goto invalid;
6845 : 0 : get_uleb128 (op1, readp, endp);
6846 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
6847 : 0 : goto invalid;
6848 : 0 : get_uleb128 (op2, readp, endp);
6849 : 0 : printf (" offset_extended r%" PRIu64 " (%s) at cfa%+" PRId64
6850 : : "\n",
6851 : : op1, regname (ebl, op1, regnamebuf), op2 * data_align);
6852 : : break;
6853 : 0 : case DW_CFA_restore_extended:
6854 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
6855 : 0 : goto invalid;
6856 : 0 : get_uleb128 (op1, readp, endp);
6857 : 0 : printf (" restore_extended r%" PRIu64 " (%s)\n",
6858 : : op1, regname (ebl, op1, regnamebuf));
6859 : : break;
6860 : 0 : case DW_CFA_undefined:
6861 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
6862 : 0 : goto invalid;
6863 : 0 : get_uleb128 (op1, readp, endp);
6864 : 0 : printf (" undefined r%" PRIu64 " (%s)\n", op1,
6865 : : regname (ebl, op1, regnamebuf));
6866 : : break;
6867 : 0 : case DW_CFA_same_value:
6868 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
6869 : 0 : goto invalid;
6870 : 0 : get_uleb128 (op1, readp, endp);
6871 : 0 : printf (" same_value r%" PRIu64 " (%s)\n", op1,
6872 : : regname (ebl, op1, regnamebuf));
6873 : : break;
6874 : 0 : case DW_CFA_register:
6875 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
6876 : 0 : goto invalid;
6877 : 0 : get_uleb128 (op1, readp, endp);
6878 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
6879 : 0 : goto invalid;
6880 : 0 : get_uleb128 (op2, readp, endp);
6881 : 0 : printf (" register r%" PRIu64 " (%s) in r%" PRIu64 " (%s)\n",
6882 : : op1, regname (ebl, op1, regnamebuf), op2,
6883 : : regname (ebl, op2, regnamebuf));
6884 : : break;
6885 : 0 : case DW_CFA_remember_state:
6886 : 0 : puts (" remember_state");
6887 : 0 : break;
6888 : 0 : case DW_CFA_restore_state:
6889 : 0 : puts (" restore_state");
6890 : 0 : break;
6891 : 0 : case DW_CFA_def_cfa:
6892 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
6893 : 0 : goto invalid;
6894 : 0 : get_uleb128 (op1, readp, endp);
6895 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
6896 : 0 : goto invalid;
6897 : 0 : get_uleb128 (op2, readp, endp);
6898 : 0 : printf (" def_cfa r%" PRIu64 " (%s) at offset %" PRIu64 "\n",
6899 : : op1, regname (ebl, op1, regnamebuf), op2);
6900 : : break;
6901 : 0 : case DW_CFA_def_cfa_register:
6902 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
6903 : 0 : goto invalid;
6904 : 0 : get_uleb128 (op1, readp, endp);
6905 : 0 : printf (" def_cfa_register r%" PRIu64 " (%s)\n",
6906 : : op1, regname (ebl, op1, regnamebuf));
6907 : : break;
6908 : 0 : case DW_CFA_def_cfa_offset:
6909 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
6910 : 0 : goto invalid;
6911 : 0 : get_uleb128 (op1, readp, endp);
6912 : 0 : printf (" def_cfa_offset %" PRIu64 "\n", op1);
6913 : : break;
6914 : 0 : case DW_CFA_def_cfa_expression:
6915 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
6916 : 0 : goto invalid;
6917 : 0 : get_uleb128 (op1, readp, endp); /* Length of DW_FORM_block. */
6918 : 0 : printf (" def_cfa_expression %" PRIu64 "\n", op1);
6919 [ # # ]: 0 : if ((uint64_t) (endp - readp) < op1)
6920 : : {
6921 : 0 : invalid:
6922 : 0 : fputs (_(" <INVALID DATA>\n"), stdout);
6923 : 0 : return;
6924 : : }
6925 : 0 : print_ops (dwflmod, dbg, 10, 10, version, ptr_size, 0, NULL,
6926 : : op1, readp);
6927 : 0 : readp += op1;
6928 : 0 : break;
6929 : 0 : case DW_CFA_expression:
6930 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
6931 : 0 : goto invalid;
6932 : 0 : get_uleb128 (op1, readp, endp);
6933 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
6934 : 0 : goto invalid;
6935 : 0 : get_uleb128 (op2, readp, endp); /* Length of DW_FORM_block. */
6936 : 0 : printf (" expression r%" PRIu64 " (%s) \n",
6937 : : op1, regname (ebl, op1, regnamebuf));
6938 [ # # ]: 0 : if ((uint64_t) (endp - readp) < op2)
6939 : 0 : goto invalid;
6940 : 0 : print_ops (dwflmod, dbg, 10, 10, version, ptr_size, 0, NULL,
6941 : : op2, readp);
6942 : 0 : readp += op2;
6943 : 0 : break;
6944 : 0 : case DW_CFA_offset_extended_sf:
6945 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
6946 : 0 : goto invalid;
6947 : 0 : get_uleb128 (op1, readp, endp);
6948 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
6949 : 0 : goto invalid;
6950 : 0 : get_sleb128 (sop2, readp, endp);
6951 : 0 : printf (" offset_extended_sf r%" PRIu64 " (%s) at cfa%+"
6952 : : PRId64 "\n",
6953 : : op1, regname (ebl, op1, regnamebuf), sop2 * data_align);
6954 : : break;
6955 : 0 : case DW_CFA_def_cfa_sf:
6956 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
6957 : 0 : goto invalid;
6958 : 0 : get_uleb128 (op1, readp, endp);
6959 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
6960 : 0 : goto invalid;
6961 : 0 : get_sleb128 (sop2, readp, endp);
6962 : 0 : printf (" def_cfa_sf r%" PRIu64 " (%s) at offset %" PRId64 "\n",
6963 : : op1, regname (ebl, op1, regnamebuf), sop2 * data_align);
6964 : : break;
6965 : 0 : case DW_CFA_def_cfa_offset_sf:
6966 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
6967 : 0 : goto invalid;
6968 : 0 : get_sleb128 (sop1, readp, endp);
6969 : 0 : printf (" def_cfa_offset_sf %" PRId64 "\n", sop1 * data_align);
6970 : : break;
6971 : 0 : case DW_CFA_val_offset:
6972 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
6973 : 0 : goto invalid;
6974 : 0 : get_uleb128 (op1, readp, endp);
6975 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
6976 : 0 : goto invalid;
6977 : 0 : get_uleb128 (op2, readp, endp);
6978 : 0 : printf (" val_offset %" PRIu64 " at offset %" PRIu64 "\n",
6979 : : op1, op2 * data_align);
6980 : : break;
6981 : 0 : case DW_CFA_val_offset_sf:
6982 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
6983 : 0 : goto invalid;
6984 : 0 : get_uleb128 (op1, readp, endp);
6985 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
6986 : 0 : goto invalid;
6987 : 0 : get_sleb128 (sop2, readp, endp);
6988 : 0 : printf (" val_offset_sf %" PRIu64 " at offset %" PRId64 "\n",
6989 : : op1, sop2 * data_align);
6990 : : break;
6991 : 0 : case DW_CFA_val_expression:
6992 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
6993 : 0 : goto invalid;
6994 : 0 : get_uleb128 (op1, readp, endp);
6995 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
6996 : 0 : goto invalid;
6997 : 0 : get_uleb128 (op2, readp, endp); /* Length of DW_FORM_block. */
6998 : 0 : printf (" val_expression r%" PRIu64 " (%s)\n",
6999 : : op1, regname (ebl, op1, regnamebuf));
7000 [ # # ]: 0 : if ((uint64_t) (endp - readp) < op2)
7001 : 0 : goto invalid;
7002 : 0 : print_ops (dwflmod, dbg, 10, 10, version, ptr_size, 0,
7003 : : NULL, op2, readp);
7004 : 0 : readp += op2;
7005 : 0 : break;
7006 : 0 : case DW_CFA_MIPS_advance_loc8:
7007 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 8)
7008 : 0 : goto invalid;
7009 [ # # ]: 0 : op1 = read_8ubyte_unaligned_inc (dbg, readp);
7010 : 0 : printf (" MIPS_advance_loc8 %" PRIu64 " to %#" PRIx64 "\n",
7011 : 0 : op1, pc += op1 * code_align);
7012 : : break;
7013 : 0 : case DW_CFA_GNU_window_save: /* DW_CFA_AARCH64_negate_ra_state */
7014 [ # # ]: 0 : if (ehdr->e_machine == EM_AARCH64)
7015 : 0 : puts (" AARCH64_negate_ra_state");
7016 : : else
7017 : 0 : puts (" GNU_window_save");
7018 : : break;
7019 : 0 : case DW_CFA_GNU_args_size:
7020 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
7021 : 0 : goto invalid;
7022 : 0 : get_uleb128 (op1, readp, endp);
7023 : 0 : printf (" args_size %" PRIu64 "\n", op1);
7024 : : break;
7025 : : default:
7026 : 0 : printf (" ??? (%u)\n", opcode);
7027 : : break;
7028 : : }
7029 [ # # ]: 0 : else if (opcode < DW_CFA_offset)
7030 : 0 : printf (" advance_loc %u to %#" PRIx64 "\n",
7031 : 0 : opcode & 0x3f, pc += (opcode & 0x3f) * code_align);
7032 [ # # ]: 0 : else if (opcode < DW_CFA_restore)
7033 : : {
7034 : 0 : uint64_t offset;
7035 [ # # ]: 0 : if ((uint64_t) (endp - readp) < 1)
7036 : 0 : goto invalid;
7037 : 0 : get_uleb128 (offset, readp, endp);
7038 : 0 : printf (" offset r%u (%s) at cfa%+" PRId64 "\n",
7039 : : opcode & 0x3f, regname (ebl, opcode & 0x3f, regnamebuf),
7040 : : offset * data_align);
7041 : : }
7042 : : else
7043 [ # # ]: 0 : printf (" restore r%u (%s)\n",
7044 : : opcode & 0x3f, regname (ebl, opcode & 0x3f, regnamebuf));
7045 : : }
7046 : : }
7047 : :
7048 : :
7049 : : static unsigned int
7050 : 0 : encoded_ptr_size (int encoding, unsigned int ptr_size)
7051 : : {
7052 [ # # # # ]: 0 : switch (encoding & 7)
7053 : : {
7054 : : case DW_EH_PE_udata4:
7055 : : return 4;
7056 : 0 : case DW_EH_PE_udata8:
7057 : 0 : return 8;
7058 : : case 0:
7059 : : return ptr_size;
7060 : : }
7061 : :
7062 : 0 : fprintf (stderr, "Unsupported pointer encoding: %#x, "
7063 : : "assuming pointer size of %d.\n", encoding, ptr_size);
7064 : 0 : return ptr_size;
7065 : : }
7066 : :
7067 : :
7068 : : static unsigned int
7069 : 0 : print_encoding (unsigned int val)
7070 : : {
7071 [ # # # # : 0 : switch (val & 0xf)
# # # # #
# ]
7072 : : {
7073 : 0 : case DW_EH_PE_absptr:
7074 : 0 : fputs ("absptr", stdout);
7075 : 0 : break;
7076 : 0 : case DW_EH_PE_uleb128:
7077 : 0 : fputs ("uleb128", stdout);
7078 : 0 : break;
7079 : 0 : case DW_EH_PE_udata2:
7080 : 0 : fputs ("udata2", stdout);
7081 : 0 : break;
7082 : 0 : case DW_EH_PE_udata4:
7083 : 0 : fputs ("udata4", stdout);
7084 : 0 : break;
7085 : 0 : case DW_EH_PE_udata8:
7086 : 0 : fputs ("udata8", stdout);
7087 : 0 : break;
7088 : 0 : case DW_EH_PE_sleb128:
7089 : 0 : fputs ("sleb128", stdout);
7090 : 0 : break;
7091 : 0 : case DW_EH_PE_sdata2:
7092 : 0 : fputs ("sdata2", stdout);
7093 : 0 : break;
7094 : 0 : case DW_EH_PE_sdata4:
7095 : 0 : fputs ("sdata4", stdout);
7096 : 0 : break;
7097 : 0 : case DW_EH_PE_sdata8:
7098 : 0 : fputs ("sdata8", stdout);
7099 : 0 : break;
7100 : : default:
7101 : : /* We did not use any of the bits after all. */
7102 : : return val;
7103 : : }
7104 : :
7105 : 0 : return val & ~0xf;
7106 : : }
7107 : :
7108 : :
7109 : : static unsigned int
7110 : 0 : print_relinfo (unsigned int val)
7111 : : {
7112 [ # # # # : 0 : switch (val & 0x70)
# # ]
7113 : : {
7114 : 0 : case DW_EH_PE_pcrel:
7115 : 0 : fputs ("pcrel", stdout);
7116 : 0 : break;
7117 : 0 : case DW_EH_PE_textrel:
7118 : 0 : fputs ("textrel", stdout);
7119 : 0 : break;
7120 : 0 : case DW_EH_PE_datarel:
7121 : 0 : fputs ("datarel", stdout);
7122 : 0 : break;
7123 : 0 : case DW_EH_PE_funcrel:
7124 : 0 : fputs ("funcrel", stdout);
7125 : 0 : break;
7126 : 0 : case DW_EH_PE_aligned:
7127 : 0 : fputs ("aligned", stdout);
7128 : 0 : break;
7129 : : default:
7130 : : return val;
7131 : : }
7132 : :
7133 : 0 : return val & ~0x70;
7134 : : }
7135 : :
7136 : :
7137 : : static void
7138 : 0 : print_encoding_base (const char *pfx, unsigned int fde_encoding)
7139 : : {
7140 : 0 : printf ("(%s", pfx);
7141 : :
7142 [ # # ]: 0 : if (fde_encoding == DW_EH_PE_omit)
7143 : 0 : puts ("omit)");
7144 : : else
7145 : : {
7146 : 0 : unsigned int w = fde_encoding;
7147 : :
7148 : 0 : w = print_encoding (w);
7149 : :
7150 [ # # ]: 0 : if (w & 0x70)
7151 : : {
7152 [ # # ]: 0 : if (w != fde_encoding)
7153 : 0 : fputc (' ', stdout);
7154 : :
7155 : 0 : w = print_relinfo (w);
7156 : : }
7157 : :
7158 [ # # ]: 0 : if (w != 0)
7159 [ # # ]: 0 : printf ("%s%x", w != fde_encoding ? " " : "", w);
7160 : :
7161 : 0 : puts (")");
7162 : : }
7163 : 0 : }
7164 : :
7165 : :
7166 : : static void
7167 : 0 : print_debug_frame_section (Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr,
7168 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
7169 : : {
7170 : 0 : size_t shstrndx;
7171 : : /* We know this call will succeed since it did in the caller. */
7172 : 0 : (void) elf_getshdrstrndx (ebl->elf, &shstrndx);
7173 : 0 : const char *scnname = elf_strptr (ebl->elf, shstrndx, shdr->sh_name);
7174 : :
7175 : : /* Needed if we find PC-relative addresses. */
7176 : 0 : GElf_Addr bias;
7177 [ # # ]: 0 : if (dwfl_module_getelf (dwflmod, &bias) == NULL)
7178 : : {
7179 : 0 : error (0, 0, _("cannot get ELF: %s"), dwfl_errmsg (-1));
7180 : 0 : return;
7181 : : }
7182 : :
7183 : 0 : bool is_eh_frame = strcmp (scnname, ".eh_frame") == 0;
7184 : 0 : Elf_Data *data;
7185 [ # # ]: 0 : if (is_eh_frame)
7186 : : {
7187 : 0 : data = elf_rawdata (scn, NULL);
7188 [ # # ]: 0 : if (data == NULL)
7189 : : {
7190 : 0 : error (0, 0, _("cannot get %s content: %s"),
7191 : : scnname, elf_errmsg (-1));
7192 : 0 : return;
7193 : : }
7194 : : }
7195 : : else
7196 : : {
7197 : 0 : data = get_debug_elf_data (dbg, ebl, IDX_debug_frame, scn);
7198 [ # # ]: 0 : if (data == NULL)
7199 : : return;
7200 : : }
7201 : :
7202 [ # # ]: 0 : if (is_eh_frame)
7203 : 0 : printf (_("\
7204 : : \nCall frame information section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
7205 : 0 : elf_ndxscn (scn), scnname, (uint64_t) shdr->sh_offset);
7206 : : else
7207 : 0 : printf (_("\
7208 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
7209 : 0 : elf_ndxscn (scn), scnname, (uint64_t) shdr->sh_offset);
7210 : :
7211 : 0 : struct cieinfo
7212 : : {
7213 : : ptrdiff_t cie_offset;
7214 : : const char *augmentation;
7215 : : unsigned int code_alignment_factor;
7216 : : unsigned int data_alignment_factor;
7217 : : uint8_t address_size;
7218 : : uint8_t fde_encoding;
7219 : : uint8_t lsda_encoding;
7220 : : struct cieinfo *next;
7221 : 0 : } *cies = NULL;
7222 : :
7223 : 0 : const unsigned char *readp = data->d_buf;
7224 : 0 : const unsigned char *const dataend = ((unsigned char *) data->d_buf
7225 : 0 : + data->d_size);
7226 [ # # ]: 0 : while (readp < dataend)
7227 : : {
7228 [ # # ]: 0 : if (unlikely (readp + 4 > dataend))
7229 : : {
7230 : 0 : invalid_data:
7231 : 0 : error (0, 0, _("invalid data in section [%zu] '%s'"),
7232 : : elf_ndxscn (scn), scnname);
7233 : 0 : return;
7234 : : }
7235 : :
7236 : : /* At the beginning there must be a CIE. There can be multiple,
7237 : : hence we test tis in a loop. */
7238 : 0 : ptrdiff_t offset = readp - (unsigned char *) data->d_buf;
7239 : :
7240 [ # # ]: 0 : Dwarf_Word unit_length = read_4ubyte_unaligned_inc (dbg, readp);
7241 : 0 : unsigned int length = 4;
7242 [ # # ]: 0 : if (unlikely (unit_length == 0xffffffff))
7243 : : {
7244 [ # # ]: 0 : if (unlikely (readp + 8 > dataend))
7245 : 0 : goto invalid_data;
7246 : :
7247 [ # # ]: 0 : unit_length = read_8ubyte_unaligned_inc (dbg, readp);
7248 : 0 : length = 8;
7249 : : }
7250 : :
7251 [ # # ]: 0 : if (unlikely (unit_length == 0))
7252 : : {
7253 : 0 : printf (_("\n [%6tx] Zero terminator\n"), offset);
7254 : 0 : continue;
7255 : : }
7256 : :
7257 : 0 : Dwarf_Word maxsize = dataend - readp;
7258 [ # # ]: 0 : if (unlikely (unit_length > maxsize))
7259 : 0 : goto invalid_data;
7260 : :
7261 [ # # ]: 0 : unsigned int ptr_size = ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 4 : 8;
7262 : :
7263 : 0 : ptrdiff_t start = readp - (unsigned char *) data->d_buf;
7264 : 0 : const unsigned char *const cieend = readp + unit_length;
7265 [ # # ]: 0 : if (unlikely (cieend > dataend))
7266 : 0 : goto invalid_data;
7267 : :
7268 : 0 : Dwarf_Off cie_id;
7269 [ # # ]: 0 : if (length == 4)
7270 : : {
7271 [ # # ]: 0 : if (unlikely (cieend - readp < 4))
7272 : 0 : goto invalid_data;
7273 [ # # ]: 0 : cie_id = read_4ubyte_unaligned_inc (dbg, readp);
7274 [ # # ]: 0 : if (!is_eh_frame && cie_id == DW_CIE_ID_32)
7275 : : cie_id = DW_CIE_ID_64;
7276 : : }
7277 : : else
7278 : : {
7279 [ # # ]: 0 : if (unlikely (cieend - readp < 8))
7280 : 0 : goto invalid_data;
7281 [ # # ]: 0 : cie_id = read_8ubyte_unaligned_inc (dbg, readp);
7282 : : }
7283 : :
7284 : 0 : uint_fast8_t version = 2;
7285 : 0 : unsigned int code_alignment_factor;
7286 : 0 : int data_alignment_factor;
7287 : 0 : unsigned int fde_encoding = 0;
7288 : 0 : unsigned int lsda_encoding = 0;
7289 : 0 : Dwarf_Word initial_location = 0;
7290 : 0 : Dwarf_Word vma_base = 0;
7291 : :
7292 [ # # # # ]: 0 : if (cie_id == (is_eh_frame ? 0 : DW_CIE_ID_64))
7293 : : {
7294 [ # # ]: 0 : if (unlikely (cieend - readp < 2))
7295 : 0 : goto invalid_data;
7296 : 0 : version = *readp++;
7297 : 0 : const char *const augmentation = (const char *) readp;
7298 : 0 : readp = memchr (readp, '\0', cieend - readp);
7299 [ # # ]: 0 : if (unlikely (readp == NULL))
7300 : 0 : goto invalid_data;
7301 : 0 : ++readp;
7302 : :
7303 : 0 : uint_fast8_t segment_size = 0;
7304 [ # # ]: 0 : if (version >= 4)
7305 : : {
7306 [ # # ]: 0 : if (cieend - readp < 5)
7307 : 0 : goto invalid_data;
7308 : 0 : ptr_size = *readp++;
7309 : 0 : segment_size = *readp++;
7310 : : }
7311 : :
7312 [ # # ]: 0 : if (cieend - readp < 1)
7313 : 0 : goto invalid_data;
7314 : 0 : get_uleb128 (code_alignment_factor, readp, cieend);
7315 [ # # ]: 0 : if (cieend - readp < 1)
7316 : 0 : goto invalid_data;
7317 : 0 : get_sleb128 (data_alignment_factor, readp, cieend);
7318 : :
7319 : : /* In some variant for unwind data there is another field. */
7320 [ # # ]: 0 : if (strcmp (augmentation, "eh") == 0)
7321 [ # # ]: 0 : readp += ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 4 : 8;
7322 : :
7323 : 0 : unsigned int return_address_register;
7324 [ # # ]: 0 : if (cieend - readp < 1)
7325 : 0 : goto invalid_data;
7326 [ # # ]: 0 : if (unlikely (version == 1))
7327 : 0 : return_address_register = *readp++;
7328 : : else
7329 : 0 : get_uleb128 (return_address_register, readp, cieend);
7330 : :
7331 : 0 : printf ("\n [%6tx] CIE length=%" PRIu64 "\n"
7332 : : " CIE_id: %" PRIu64 "\n"
7333 : : " version: %u\n"
7334 : : " augmentation: \"%s\"\n",
7335 : : offset, (uint64_t) unit_length, (uint64_t) cie_id,
7336 : : version, augmentation);
7337 [ # # ]: 0 : if (version >= 4)
7338 : 0 : printf (" address_size: %u\n"
7339 : : " segment_size: %u\n",
7340 : : ptr_size, segment_size);
7341 : 0 : printf (" code_alignment_factor: %u\n"
7342 : : " data_alignment_factor: %d\n"
7343 : : " return_address_register: %u\n",
7344 : : code_alignment_factor,
7345 : : data_alignment_factor, return_address_register);
7346 : :
7347 [ # # ]: 0 : if (augmentation[0] == 'z')
7348 : : {
7349 [ # # ]: 0 : if (cieend - readp < 1)
7350 : 0 : goto invalid_data;
7351 : :
7352 : 0 : unsigned int augmentationlen;
7353 : 0 : get_uleb128 (augmentationlen, readp, cieend);
7354 : :
7355 [ # # ]: 0 : if (augmentationlen > (size_t) (cieend - readp))
7356 : : {
7357 : 0 : error (0, 0, _("invalid augmentation length"));
7358 : 0 : readp = cieend;
7359 : 0 : continue;
7360 : : }
7361 : :
7362 : 0 : const char *hdr = "Augmentation data:";
7363 : 0 : const char *cp = augmentation + 1;
7364 [ # # # # ]: 0 : while (*cp != '\0' && cp < augmentation + augmentationlen + 1)
7365 : : {
7366 : 0 : printf (" %-26s%#x ", hdr, *readp);
7367 : 0 : hdr = "";
7368 : :
7369 [ # # ]: 0 : if (*cp == 'R')
7370 : : {
7371 : 0 : fde_encoding = *readp++;
7372 : 0 : print_encoding_base (_("FDE address encoding: "),
7373 : : fde_encoding);
7374 : : }
7375 [ # # ]: 0 : else if (*cp == 'L')
7376 : : {
7377 : 0 : lsda_encoding = *readp++;
7378 : 0 : print_encoding_base (_("LSDA pointer encoding: "),
7379 : : lsda_encoding);
7380 : : }
7381 [ # # ]: 0 : else if (*cp == 'P')
7382 : : {
7383 : : /* Personality. This field usually has a relocation
7384 : : attached pointing to __gcc_personality_v0. */
7385 : 0 : const unsigned char *startp = readp;
7386 : 0 : unsigned int encoding = *readp++;
7387 : 0 : uint64_t val = 0;
7388 : 0 : readp = read_encoded (encoding, readp,
7389 : : readp - 1 + augmentationlen,
7390 : : &val, dbg);
7391 : :
7392 : 0 : while (++startp < readp)
7393 [ # # ]: 0 : printf ("%#x ", *startp);
7394 : :
7395 : 0 : putchar ('(');
7396 : 0 : print_encoding (encoding);
7397 : 0 : putchar (' ');
7398 [ # # ]: 0 : switch (encoding & 0xf)
7399 : : {
7400 : 0 : case DW_EH_PE_sleb128:
7401 : : case DW_EH_PE_sdata2:
7402 : : case DW_EH_PE_sdata4:
7403 : 0 : printf ("%" PRId64 ")\n", val);
7404 : : break;
7405 : 0 : default:
7406 : 0 : printf ("%#" PRIx64 ")\n", val);
7407 : : break;
7408 : : }
7409 : : }
7410 : : else
7411 : 0 : printf ("(%x)\n", *readp++);
7412 : :
7413 : 0 : ++cp;
7414 : : }
7415 : : }
7416 : :
7417 [ # # ]: 0 : if (likely (ptr_size == 4 || ptr_size == 8))
7418 : : {
7419 : 0 : struct cieinfo *newp = alloca (sizeof (*newp));
7420 : 0 : newp->cie_offset = offset;
7421 : 0 : newp->augmentation = augmentation;
7422 : 0 : newp->fde_encoding = fde_encoding;
7423 : 0 : newp->lsda_encoding = lsda_encoding;
7424 : 0 : newp->address_size = ptr_size;
7425 : 0 : newp->code_alignment_factor = code_alignment_factor;
7426 : 0 : newp->data_alignment_factor = data_alignment_factor;
7427 : 0 : newp->next = cies;
7428 : 0 : cies = newp;
7429 : : }
7430 : : }
7431 : : else
7432 : : {
7433 : : struct cieinfo *cie = cies;
7434 [ # # ]: 0 : while (cie != NULL)
7435 [ # # # # ]: 0 : if (is_eh_frame
7436 : 0 : ? ((Dwarf_Off) start - cie_id) == (Dwarf_Off) cie->cie_offset
7437 : 0 : : cie_id == (Dwarf_Off) cie->cie_offset)
7438 : : break;
7439 : : else
7440 : 0 : cie = cie->next;
7441 [ # # ]: 0 : if (unlikely (cie == NULL))
7442 : : {
7443 : 0 : puts ("invalid CIE reference in FDE");
7444 : 0 : return;
7445 : : }
7446 : :
7447 : : /* Initialize from CIE data. */
7448 : 0 : fde_encoding = cie->fde_encoding;
7449 : 0 : lsda_encoding = cie->lsda_encoding;
7450 : 0 : ptr_size = encoded_ptr_size (fde_encoding, cie->address_size);
7451 : 0 : code_alignment_factor = cie->code_alignment_factor;
7452 : 0 : data_alignment_factor = cie->data_alignment_factor;
7453 : :
7454 : 0 : const unsigned char *base = readp;
7455 : : // XXX There are sometimes relocations for this value
7456 [ # # # # : 0 : initial_location = read_addr_unaligned_inc (ptr_size, dbg, readp);
# # # # ]
7457 : 0 : Dwarf_Word address_range
7458 [ # # # # : 0 : = read_addr_unaligned_inc (ptr_size, dbg, readp);
# # ]
7459 : :
7460 : : /* pcrel for an FDE address is relative to the runtime
7461 : : address of the start_address field itself. Sign extend
7462 : : if necessary to make sure the calculation is done on the
7463 : : full 64 bit address even when initial_location only holds
7464 : : the lower 32 bits. */
7465 : 0 : Dwarf_Addr pc_start = initial_location;
7466 [ # # ]: 0 : if (ptr_size == 4)
7467 : 0 : pc_start = (uint64_t) (int32_t) pc_start;
7468 [ # # ]: 0 : if ((fde_encoding & 0x70) == DW_EH_PE_pcrel)
7469 : 0 : pc_start += ((uint64_t) shdr->sh_addr
7470 : 0 : + (base - (const unsigned char *) data->d_buf)
7471 : 0 : - bias);
7472 : :
7473 : 0 : printf ("\n [%6tx] FDE length=%" PRIu64 " cie=[%6tx]\n"
7474 : : " CIE_pointer: %" PRIu64 "\n"
7475 : : " initial_location: ",
7476 : : offset, (uint64_t) unit_length,
7477 : : cie->cie_offset, (uint64_t) cie_id);
7478 : 0 : print_dwarf_addr (dwflmod, cie->address_size,
7479 : : pc_start, initial_location);
7480 [ # # ]: 0 : if ((fde_encoding & 0x70) == DW_EH_PE_pcrel)
7481 : : {
7482 : 0 : vma_base = (((uint64_t) shdr->sh_offset
7483 : 0 : + (base - (const unsigned char *) data->d_buf)
7484 : 0 : + (uint64_t) initial_location)
7485 : : & (ptr_size == 4
7486 : : ? UINT64_C (0xffffffff)
7487 [ # # ]: 0 : : UINT64_C (0xffffffffffffffff)));
7488 : 0 : printf (_(" (offset: %#" PRIx64 ")"),
7489 : : (uint64_t) vma_base);
7490 : : }
7491 : :
7492 : 0 : printf ("\n address_range: %#" PRIx64,
7493 : : (uint64_t) address_range);
7494 [ # # ]: 0 : if ((fde_encoding & 0x70) == DW_EH_PE_pcrel)
7495 : 0 : printf (_(" (end offset: %#" PRIx64 ")"),
7496 : 0 : ((uint64_t) vma_base + (uint64_t) address_range)
7497 : : & (ptr_size == 4
7498 : : ? UINT64_C (0xffffffff)
7499 [ # # ]: 0 : : UINT64_C (0xffffffffffffffff)));
7500 : 0 : putchar ('\n');
7501 : :
7502 [ # # ]: 0 : if (cie->augmentation[0] == 'z')
7503 : : {
7504 : 0 : unsigned int augmentationlen;
7505 [ # # ]: 0 : if (cieend - readp < 1)
7506 : 0 : goto invalid_data;
7507 : 0 : get_uleb128 (augmentationlen, readp, cieend);
7508 : :
7509 [ # # ]: 0 : if (augmentationlen > (size_t) (cieend - readp))
7510 : : {
7511 : 0 : error (0, 0, _("invalid augmentation length"));
7512 : 0 : readp = cieend;
7513 : 0 : continue;
7514 : : }
7515 : :
7516 [ # # ]: 0 : if (augmentationlen > 0)
7517 : : {
7518 : 0 : const char *hdr = "Augmentation data:";
7519 : 0 : const char *cp = cie->augmentation + 1;
7520 : 0 : unsigned int u = 0;
7521 : 0 : while (*cp != '\0'
7522 [ # # # # ]: 0 : && cp < cie->augmentation + augmentationlen + 1)
7523 : : {
7524 [ # # ]: 0 : if (*cp == 'L')
7525 : : {
7526 : 0 : uint64_t lsda_pointer;
7527 : 0 : const unsigned char *p
7528 : 0 : = read_encoded (lsda_encoding, &readp[u],
7529 : : &readp[augmentationlen],
7530 : : &lsda_pointer, dbg);
7531 : 0 : u = p - readp;
7532 : 0 : printf (_("\
7533 : : %-26sLSDA pointer: %#" PRIx64 "\n"),
7534 : : hdr, lsda_pointer);
7535 : 0 : hdr = "";
7536 : : }
7537 : 0 : ++cp;
7538 : : }
7539 : :
7540 [ # # ]: 0 : while (u < augmentationlen)
7541 : : {
7542 : 0 : printf (" %-26s%#x\n", hdr, readp[u++]);
7543 : 0 : hdr = "";
7544 : : }
7545 : : }
7546 : :
7547 : 0 : readp += augmentationlen;
7548 : : }
7549 : : }
7550 : :
7551 : : /* Handle the initialization instructions. */
7552 [ # # ]: 0 : if (ptr_size != 4 && ptr_size !=8)
7553 : 0 : printf ("invalid CIE pointer size (%u), must be 4 or 8.\n", ptr_size);
7554 : : else
7555 : 0 : print_cfa_program (readp, cieend, vma_base, code_alignment_factor,
7556 : : data_alignment_factor, version, ptr_size,
7557 : : fde_encoding, dwflmod, ebl, ehdr, dbg);
7558 : 0 : readp = cieend;
7559 : : }
7560 : : }
7561 : :
7562 : :
7563 : : /* Returns the signedness (or false if it cannot be determined) and
7564 : : the byte size (or zero if it cannot be gotten) of the given DIE
7565 : : DW_AT_type attribute. Uses dwarf_peel_type and dwarf_aggregate_size. */
7566 : : static void
7567 : 0 : die_type_sign_bytes (Dwarf_Die *die, bool *is_signed, int *bytes)
7568 : : {
7569 : 0 : Dwarf_Attribute attr;
7570 : 0 : Dwarf_Die type;
7571 : :
7572 : 0 : *bytes = 0;
7573 : 0 : *is_signed = false;
7574 : :
7575 [ # # ]: 0 : if (dwarf_peel_type (dwarf_formref_die (dwarf_attr_integrate (die,
7576 : : DW_AT_type,
7577 : : &attr), &type),
7578 : : &type) == 0)
7579 : : {
7580 : 0 : Dwarf_Word val;
7581 : 0 : *is_signed = (dwarf_formudata (dwarf_attr (&type, DW_AT_encoding,
7582 : : &attr), &val) == 0
7583 [ # # # # ]: 0 : && (val == DW_ATE_signed || val == DW_ATE_signed_char));
7584 : :
7585 [ # # ]: 0 : if (dwarf_aggregate_size (&type, &val) == 0)
7586 : 0 : *bytes = val;
7587 : : }
7588 : 0 : }
7589 : :
7590 : : struct attrcb_args
7591 : : {
7592 : : Dwfl_Module *dwflmod;
7593 : : Dwarf *dbg;
7594 : : Dwarf_Die *dies;
7595 : : int level;
7596 : : bool silent;
7597 : : bool is_split;
7598 : : unsigned int version;
7599 : : unsigned int addrsize;
7600 : : unsigned int offset_size;
7601 : : struct Dwarf_CU *cu;
7602 : : };
7603 : :
7604 : :
7605 : : static int
7606 : 0 : attr_callback (Dwarf_Attribute *attrp, void *arg)
7607 : : {
7608 : 0 : struct attrcb_args *cbargs = (struct attrcb_args *) arg;
7609 : 0 : const int level = cbargs->level;
7610 : 0 : Dwarf_Die *die = &cbargs->dies[level];
7611 : 0 : bool is_split = cbargs->is_split;
7612 : :
7613 [ # # ]: 0 : unsigned int attr = dwarf_whatattr (attrp);
7614 [ # # ]: 0 : if (unlikely (attr == 0))
7615 : : {
7616 [ # # ]: 0 : if (!cbargs->silent)
7617 : 0 : error (0, 0, _("DIE [%" PRIx64 "] "
7618 : : "cannot get attribute code: %s"),
7619 : : dwarf_dieoffset (die), dwarf_errmsg (-1));
7620 : 0 : return DWARF_CB_ABORT;
7621 : : }
7622 : :
7623 [ # # ]: 0 : unsigned int form = dwarf_whatform (attrp);
7624 [ # # ]: 0 : if (unlikely (form == 0))
7625 : : {
7626 [ # # ]: 0 : if (!cbargs->silent)
7627 : 0 : error (0, 0, _("DIE [%" PRIx64 "] "
7628 : : "cannot get attribute form: %s"),
7629 : : dwarf_dieoffset (die), dwarf_errmsg (-1));
7630 : 0 : return DWARF_CB_ABORT;
7631 : : }
7632 : :
7633 [ # # # # : 0 : switch (form)
# # # #
# ]
7634 : : {
7635 : 0 : case DW_FORM_addr:
7636 : : case DW_FORM_addrx:
7637 : : case DW_FORM_addrx1:
7638 : : case DW_FORM_addrx2:
7639 : : case DW_FORM_addrx3:
7640 : : case DW_FORM_addrx4:
7641 : : case DW_FORM_GNU_addr_index:
7642 [ # # ]: 0 : if (!cbargs->silent)
7643 : : {
7644 : 0 : Dwarf_Addr addr;
7645 [ # # ]: 0 : if (unlikely (dwarf_formaddr (attrp, &addr) != 0))
7646 : : {
7647 : 0 : attrval_out:
7648 [ # # ]: 0 : if (!cbargs->silent)
7649 : 0 : error (0, 0, _("DIE [%" PRIx64 "] "
7650 : : "cannot get attribute '%s' (%s) value: "
7651 : : "%s"),
7652 : : dwarf_dieoffset (die),
7653 : : dwarf_attr_name (attr),
7654 : : dwarf_form_name (form),
7655 : : dwarf_errmsg (-1));
7656 : : /* Don't ABORT, it might be other attributes can be resolved. */
7657 : 0 : return DWARF_CB_OK;
7658 : : }
7659 [ # # ]: 0 : if (form != DW_FORM_addr )
7660 : : {
7661 : 0 : Dwarf_Word word;
7662 [ # # ]: 0 : if (dwarf_formudata (attrp, &word) != 0)
7663 : 0 : goto attrval_out;
7664 : 0 : printf (" %*s%-20s (%s) [%" PRIx64 "] ",
7665 : : (int) (level * 2), "", dwarf_attr_name (attr),
7666 : : dwarf_form_name (form), word);
7667 : : }
7668 : : else
7669 : 0 : printf (" %*s%-20s (%s) ",
7670 : : (int) (level * 2), "", dwarf_attr_name (attr),
7671 : : dwarf_form_name (form));
7672 : 0 : print_dwarf_addr (cbargs->dwflmod, cbargs->addrsize, addr, addr);
7673 : 0 : printf ("\n");
7674 : : }
7675 : 0 : break;
7676 : :
7677 : 0 : case DW_FORM_indirect:
7678 : : case DW_FORM_strp:
7679 : : case DW_FORM_line_strp:
7680 : : case DW_FORM_strx:
7681 : : case DW_FORM_strx1:
7682 : : case DW_FORM_strx2:
7683 : : case DW_FORM_strx3:
7684 : : case DW_FORM_strx4:
7685 : : case DW_FORM_string:
7686 : : case DW_FORM_GNU_strp_alt:
7687 : : case DW_FORM_GNU_str_index:
7688 [ # # ]: 0 : if (cbargs->silent)
7689 : : break;
7690 : 0 : const char *str = dwarf_formstring (attrp);
7691 [ # # ]: 0 : if (unlikely (str == NULL))
7692 : 0 : goto attrval_out;
7693 : 0 : printf (" %*s%-20s (%s) \"%s\"\n",
7694 : : (int) (level * 2), "", dwarf_attr_name (attr),
7695 : : dwarf_form_name (form), str);
7696 : : break;
7697 : :
7698 : 0 : case DW_FORM_ref_addr:
7699 : : case DW_FORM_ref_udata:
7700 : : case DW_FORM_ref8:
7701 : : case DW_FORM_ref4:
7702 : : case DW_FORM_ref2:
7703 : : case DW_FORM_ref1:
7704 : : case DW_FORM_GNU_ref_alt:
7705 : : case DW_FORM_ref_sup4:
7706 : : case DW_FORM_ref_sup8:
7707 [ # # ]: 0 : if (cbargs->silent)
7708 : : break;
7709 : 0 : Dwarf_Die ref;
7710 [ # # ]: 0 : if (unlikely (dwarf_formref_die (attrp, &ref) == NULL))
7711 : 0 : goto attrval_out;
7712 : :
7713 : 0 : printf (" %*s%-20s (%s) ",
7714 : : (int) (level * 2), "", dwarf_attr_name (attr),
7715 : : dwarf_form_name (form));
7716 [ # # ]: 0 : if (is_split)
7717 : 0 : printf ("{%6" PRIxMAX "}\n", (uintmax_t) dwarf_dieoffset (&ref));
7718 : : else
7719 : 0 : printf ("[%6" PRIxMAX "]\n", (uintmax_t) dwarf_dieoffset (&ref));
7720 : : break;
7721 : :
7722 : 0 : case DW_FORM_ref_sig8:
7723 [ # # ]: 0 : if (cbargs->silent)
7724 : : break;
7725 : 0 : printf (" %*s%-20s (%s) {%6" PRIx64 "}\n",
7726 : : (int) (level * 2), "", dwarf_attr_name (attr),
7727 : : dwarf_form_name (form),
7728 [ # # ]: 0 : (uint64_t) read_8ubyte_unaligned (attrp->cu->dbg, attrp->valp));
7729 : : break;
7730 : :
7731 : 0 : case DW_FORM_sec_offset:
7732 : : case DW_FORM_rnglistx:
7733 : : case DW_FORM_loclistx:
7734 : : case DW_FORM_implicit_const:
7735 : : case DW_FORM_udata:
7736 : : case DW_FORM_sdata:
7737 : : case DW_FORM_data8: /* Note no data16 here, we see that as block. */
7738 : : case DW_FORM_data4:
7739 : : case DW_FORM_data2:
7740 : 0 : case DW_FORM_data1:;
7741 : 0 : Dwarf_Word num;
7742 [ # # ]: 0 : if (unlikely (dwarf_formudata (attrp, &num) != 0))
7743 : 0 : goto attrval_out;
7744 : :
7745 : 0 : const char *valuestr = NULL;
7746 : 0 : bool as_hex_id = false;
7747 [ # # # # : 0 : switch (attr)
# # # # #
# # # # #
# # # # #
# ]
7748 : : {
7749 : : /* This case can take either a constant or a loclistptr. */
7750 : 0 : case DW_AT_data_member_location:
7751 [ # # ]: 0 : if (form != DW_FORM_sec_offset
7752 [ # # ]: 0 : && (cbargs->version >= 4
7753 [ # # ]: 0 : || (form != DW_FORM_data4 && form != DW_FORM_data8)))
7754 : : {
7755 [ # # ]: 0 : if (!cbargs->silent)
7756 : 0 : printf (" %*s%-20s (%s) %" PRIuMAX "\n",
7757 : : (int) (level * 2), "", dwarf_attr_name (attr),
7758 : : dwarf_form_name (form), (uintmax_t) num);
7759 : 0 : return DWARF_CB_OK;
7760 : : }
7761 : 0 : FALLTHROUGH;
7762 : :
7763 : : /* These cases always take a loclist[ptr] and no constant. */
7764 : : case DW_AT_location:
7765 : : case DW_AT_data_location:
7766 : : case DW_AT_vtable_elem_location:
7767 : : case DW_AT_string_length:
7768 : : case DW_AT_use_location:
7769 : : case DW_AT_frame_base:
7770 : : case DW_AT_return_addr:
7771 : : case DW_AT_static_link:
7772 : : case DW_AT_segment:
7773 : : case DW_AT_GNU_call_site_value:
7774 : : case DW_AT_GNU_call_site_data_value:
7775 : : case DW_AT_GNU_call_site_target:
7776 : : case DW_AT_GNU_call_site_target_clobbered:
7777 : : case DW_AT_GNU_locviews:
7778 : : {
7779 : 0 : bool nlpt;
7780 [ # # ]: 0 : if (cbargs->cu->version < 5)
7781 : : {
7782 [ # # ]: 0 : if (! cbargs->is_split)
7783 : : {
7784 : 0 : nlpt = notice_listptr (section_loc, &known_locsptr,
7785 : 0 : cbargs->addrsize,
7786 : 0 : cbargs->offset_size,
7787 : : cbargs->cu, num, attr);
7788 : : }
7789 : : else
7790 : : nlpt = true;
7791 : : }
7792 : : else
7793 : : {
7794 : : /* Only register for a real section offset. Otherwise
7795 : : it is a DW_FORM_loclistx which is just an index
7796 : : number and we should already have registered the
7797 : : section offset for the index when we saw the
7798 : : DW_AT_loclists_base CU attribute. */
7799 [ # # ]: 0 : if (form == DW_FORM_sec_offset)
7800 : 0 : nlpt = notice_listptr (section_loc, &known_loclistsptr,
7801 : 0 : cbargs->addrsize, cbargs->offset_size,
7802 : : cbargs->cu, num, attr);
7803 : : else
7804 : : nlpt = true;
7805 : :
7806 : : }
7807 : :
7808 [ # # ]: 0 : if (!cbargs->silent)
7809 : : {
7810 [ # # # # ]: 0 : if (cbargs->cu->version < 5 || form == DW_FORM_sec_offset)
7811 [ # # ]: 0 : printf (" %*s%-20s (%s) location list [%6"
7812 : : PRIxMAX "]%s\n",
7813 : : (int) (level * 2), "", dwarf_attr_name (attr),
7814 : : dwarf_form_name (form), (uintmax_t) num,
7815 : : nlpt ? "" : " <WARNING offset too big>");
7816 : : else
7817 : 0 : printf (" %*s%-20s (%s) location index [%6"
7818 : : PRIxMAX "]\n",
7819 : : (int) (level * 2), "", dwarf_attr_name (attr),
7820 : : dwarf_form_name (form), (uintmax_t) num);
7821 : : }
7822 : : }
7823 : : return DWARF_CB_OK;
7824 : :
7825 : 0 : case DW_AT_loclists_base:
7826 : : {
7827 : 0 : bool nlpt = notice_listptr (section_loc, &known_loclistsptr,
7828 : 0 : cbargs->addrsize, cbargs->offset_size,
7829 : : cbargs->cu, num, attr);
7830 : :
7831 [ # # ]: 0 : if (!cbargs->silent)
7832 [ # # ]: 0 : printf (" %*s%-20s (%s) location list [%6" PRIxMAX "]%s\n",
7833 : : (int) (level * 2), "", dwarf_attr_name (attr),
7834 : : dwarf_form_name (form), (uintmax_t) num,
7835 : : nlpt ? "" : " <WARNING offset too big>");
7836 : : }
7837 : : return DWARF_CB_OK;
7838 : :
7839 : 0 : case DW_AT_ranges:
7840 : : case DW_AT_start_scope:
7841 : : {
7842 : 0 : bool nlpt;
7843 [ # # ]: 0 : if (cbargs->cu->version < 5)
7844 : 0 : nlpt = notice_listptr (section_ranges, &known_rangelistptr,
7845 : 0 : cbargs->addrsize, cbargs->offset_size,
7846 : : cbargs->cu, num, attr);
7847 : : else
7848 : : {
7849 : : /* Only register for a real section offset. Otherwise
7850 : : it is a DW_FORM_rangelistx which is just an index
7851 : : number and we should already have registered the
7852 : : section offset for the index when we saw the
7853 : : DW_AT_rnglists_base CU attribute. */
7854 [ # # ]: 0 : if (form == DW_FORM_sec_offset)
7855 : 0 : nlpt = notice_listptr (section_ranges, &known_rnglistptr,
7856 : 0 : cbargs->addrsize, cbargs->offset_size,
7857 : : cbargs->cu, num, attr);
7858 : : else
7859 : : nlpt = true;
7860 : : }
7861 : :
7862 [ # # ]: 0 : if (!cbargs->silent)
7863 : : {
7864 [ # # # # ]: 0 : if (cbargs->cu->version < 5 || form == DW_FORM_sec_offset)
7865 [ # # ]: 0 : printf (" %*s%-20s (%s) range list [%6"
7866 : : PRIxMAX "]%s\n",
7867 : : (int) (level * 2), "", dwarf_attr_name (attr),
7868 : : dwarf_form_name (form), (uintmax_t) num,
7869 : : nlpt ? "" : " <WARNING offset too big>");
7870 : : else
7871 : 0 : printf (" %*s%-20s (%s) range index [%6"
7872 : : PRIxMAX "]\n",
7873 : : (int) (level * 2), "", dwarf_attr_name (attr),
7874 : : dwarf_form_name (form), (uintmax_t) num);
7875 : : }
7876 : : }
7877 : : return DWARF_CB_OK;
7878 : :
7879 : 0 : case DW_AT_rnglists_base:
7880 : : {
7881 : 0 : bool nlpt = notice_listptr (section_ranges, &known_rnglistptr,
7882 : 0 : cbargs->addrsize, cbargs->offset_size,
7883 : : cbargs->cu, num, attr);
7884 [ # # ]: 0 : if (!cbargs->silent)
7885 [ # # ]: 0 : printf (" %*s%-20s (%s) range list [%6"
7886 : : PRIxMAX "]%s\n",
7887 : : (int) (level * 2), "", dwarf_attr_name (attr),
7888 : : dwarf_form_name (form), (uintmax_t) num,
7889 : : nlpt ? "" : " <WARNING offset too big>");
7890 : : }
7891 : : return DWARF_CB_OK;
7892 : :
7893 : 0 : case DW_AT_addr_base:
7894 : : case DW_AT_GNU_addr_base:
7895 : : {
7896 : 0 : bool addrbase = notice_listptr (section_addr, &known_addrbases,
7897 : 0 : cbargs->addrsize,
7898 : 0 : cbargs->offset_size,
7899 : : cbargs->cu, num, attr);
7900 [ # # ]: 0 : if (!cbargs->silent)
7901 [ # # ]: 0 : printf (" %*s%-20s (%s) address base [%6"
7902 : : PRIxMAX "]%s\n",
7903 : : (int) (level * 2), "", dwarf_attr_name (attr),
7904 : : dwarf_form_name (form), (uintmax_t) num,
7905 : : addrbase ? "" : " <WARNING offset too big>");
7906 : : }
7907 : : return DWARF_CB_OK;
7908 : :
7909 : 0 : case DW_AT_str_offsets_base:
7910 : : {
7911 : 0 : bool stroffbase = notice_listptr (section_str, &known_stroffbases,
7912 : 0 : cbargs->addrsize,
7913 : 0 : cbargs->offset_size,
7914 : : cbargs->cu, num, attr);
7915 [ # # ]: 0 : if (!cbargs->silent)
7916 [ # # ]: 0 : printf (" %*s%-20s (%s) str offsets base [%6"
7917 : : PRIxMAX "]%s\n",
7918 : : (int) (level * 2), "", dwarf_attr_name (attr),
7919 : : dwarf_form_name (form), (uintmax_t) num,
7920 : : stroffbase ? "" : " <WARNING offset too big>");
7921 : : }
7922 : : return DWARF_CB_OK;
7923 : :
7924 : 0 : case DW_AT_language:
7925 : 0 : valuestr = dwarf_lang_name (num);
7926 : 0 : break;
7927 : 0 : case DW_AT_encoding:
7928 : 0 : valuestr = dwarf_encoding_name (num);
7929 : 0 : break;
7930 : 0 : case DW_AT_accessibility:
7931 : 0 : valuestr = dwarf_access_name (num);
7932 : 0 : break;
7933 : 0 : case DW_AT_defaulted:
7934 : 0 : valuestr = dwarf_defaulted_name (num);
7935 : 0 : break;
7936 : 0 : case DW_AT_visibility:
7937 : 0 : valuestr = dwarf_visibility_name (num);
7938 : 0 : break;
7939 : 0 : case DW_AT_virtuality:
7940 : 0 : valuestr = dwarf_virtuality_name (num);
7941 : 0 : break;
7942 : 0 : case DW_AT_identifier_case:
7943 : 0 : valuestr = dwarf_identifier_case_name (num);
7944 : 0 : break;
7945 : 0 : case DW_AT_calling_convention:
7946 : 0 : valuestr = dwarf_calling_convention_name (num);
7947 : 0 : break;
7948 : 0 : case DW_AT_inline:
7949 : 0 : valuestr = dwarf_inline_name (num);
7950 : 0 : break;
7951 : 0 : case DW_AT_ordering:
7952 : 0 : valuestr = dwarf_ordering_name (num);
7953 : 0 : break;
7954 : 0 : case DW_AT_decl_file:
7955 : : case DW_AT_call_file:
7956 : : {
7957 [ # # ]: 0 : if (cbargs->silent)
7958 : : break;
7959 : :
7960 : : /* Try to get the actual file, the current interface only
7961 : : gives us full paths, but we only want to show the file
7962 : : name for now. */
7963 : 0 : Dwarf_Die cudie;
7964 [ # # ]: 0 : if (dwarf_cu_die (cbargs->cu, &cudie,
7965 : : NULL, NULL, NULL, NULL, NULL, NULL) != NULL)
7966 : : {
7967 : 0 : Dwarf_Files *files;
7968 : 0 : size_t nfiles;
7969 [ # # ]: 0 : if (dwarf_getsrcfiles (&cudie, &files, &nfiles) == 0)
7970 : : {
7971 : 0 : valuestr = dwarf_filesrc (files, num, NULL, NULL);
7972 [ # # ]: 0 : if (valuestr != NULL)
7973 : : {
7974 : 0 : char *filename = strrchr (valuestr, '/');
7975 [ # # ]: 0 : if (filename != NULL)
7976 : 0 : valuestr = filename + 1;
7977 : : }
7978 : : else
7979 : 0 : error (0, 0, _("invalid file (%" PRId64 "): %s"),
7980 : : num, dwarf_errmsg (-1));
7981 : : }
7982 : : else
7983 : 0 : error (0, 0, _("no srcfiles for CU [%" PRIx64 "]"),
7984 : : dwarf_dieoffset (&cudie));
7985 : : }
7986 : : else
7987 : 0 : error (0, 0, _("couldn't get DWARF CU: %s"),
7988 : : dwarf_errmsg (-1));
7989 : 0 : if (valuestr == NULL)
7990 : : valuestr = "???";
7991 : : }
7992 : 0 : break;
7993 : 0 : case DW_AT_GNU_dwo_id:
7994 : 0 : as_hex_id = true;
7995 : 0 : break;
7996 : :
7997 : : default:
7998 : : /* Nothing. */
7999 : : break;
8000 : : }
8001 : :
8002 [ # # ]: 0 : if (cbargs->silent)
8003 : : break;
8004 : :
8005 : : /* When highpc is in constant form it is relative to lowpc.
8006 : : In that case also show the address. */
8007 : 0 : Dwarf_Addr highpc;
8008 [ # # # # ]: 0 : if (attr == DW_AT_high_pc && dwarf_highpc (die, &highpc) == 0)
8009 : : {
8010 : 0 : printf (" %*s%-20s (%s) %" PRIuMAX " (",
8011 : : (int) (level * 2), "", dwarf_attr_name (attr),
8012 : : dwarf_form_name (form), (uintmax_t) num);
8013 : 0 : print_dwarf_addr (cbargs->dwflmod, cbargs->addrsize, highpc, highpc);
8014 : 0 : printf (")\n");
8015 : : }
8016 : : else
8017 : : {
8018 [ # # ]: 0 : if (as_hex_id)
8019 : : {
8020 : 0 : printf (" %*s%-20s (%s) 0x%.16" PRIx64 "\n",
8021 : : (int) (level * 2), "", dwarf_attr_name (attr),
8022 : : dwarf_form_name (form), num);
8023 : : }
8024 : : else
8025 : : {
8026 : 0 : Dwarf_Sword snum = 0;
8027 : 0 : bool is_signed;
8028 : 0 : int bytes = 0;
8029 [ # # ]: 0 : if (attr == DW_AT_const_value)
8030 : 0 : die_type_sign_bytes (die, &is_signed, &bytes);
8031 : : else
8032 : 0 : is_signed = (form == DW_FORM_sdata
8033 : 0 : || form == DW_FORM_implicit_const);
8034 : :
8035 [ # # ]: 0 : if (is_signed)
8036 [ # # ]: 0 : if (unlikely (dwarf_formsdata (attrp, &snum) != 0))
8037 : 0 : goto attrval_out;
8038 : :
8039 [ # # ]: 0 : if (valuestr == NULL)
8040 : : {
8041 : 0 : printf (" %*s%-20s (%s) ",
8042 : : (int) (level * 2), "", dwarf_attr_name (attr),
8043 : : dwarf_form_name (form));
8044 : : }
8045 : : else
8046 : : {
8047 : 0 : printf (" %*s%-20s (%s) %s (",
8048 : : (int) (level * 2), "", dwarf_attr_name (attr),
8049 : : dwarf_form_name (form), valuestr);
8050 : : }
8051 : :
8052 [ # # # # : 0 : switch (bytes)
# ]
8053 : : {
8054 : 0 : case 1:
8055 [ # # ]: 0 : if (is_signed)
8056 : 0 : printf ("%" PRId8, (int8_t) snum);
8057 : : else
8058 : 0 : printf ("%" PRIu8, (uint8_t) num);
8059 : : break;
8060 : :
8061 : 0 : case 2:
8062 [ # # ]: 0 : if (is_signed)
8063 : 0 : printf ("%" PRId16, (int16_t) snum);
8064 : : else
8065 : 0 : printf ("%" PRIu16, (uint16_t) num);
8066 : : break;
8067 : :
8068 : 0 : case 4:
8069 [ # # ]: 0 : if (is_signed)
8070 : 0 : printf ("%" PRId32, (int32_t) snum);
8071 : : else
8072 : 0 : printf ("%" PRIu32, (uint32_t) num);
8073 : : break;
8074 : :
8075 : 0 : case 8:
8076 [ # # ]: 0 : if (is_signed)
8077 : 0 : printf ("%" PRId64, (int64_t) snum);
8078 : : else
8079 : 0 : printf ("%" PRIu64, (uint64_t) num);
8080 : : break;
8081 : :
8082 : 0 : default:
8083 [ # # ]: 0 : if (is_signed)
8084 : 0 : printf ("%" PRIdMAX, (intmax_t) snum);
8085 : : else
8086 : 0 : printf ("%" PRIuMAX, (uintmax_t) num);
8087 : : break;
8088 : : }
8089 : :
8090 : : /* Make clear if we switched from a signed encoding to
8091 : : an unsigned value. */
8092 [ # # ]: 0 : if (attr == DW_AT_const_value
8093 [ # # ]: 0 : && (form == DW_FORM_sdata || form == DW_FORM_implicit_const)
8094 [ # # ]: 0 : && !is_signed)
8095 : 0 : printf (" (%" PRIdMAX ")", (intmax_t) num);
8096 : :
8097 [ # # ]: 0 : if (valuestr == NULL)
8098 : 0 : printf ("\n");
8099 : : else
8100 : 0 : printf (")\n");
8101 : : }
8102 : : }
8103 : : break;
8104 : :
8105 : 0 : case DW_FORM_flag:
8106 [ # # ]: 0 : if (cbargs->silent)
8107 : : break;
8108 : 0 : bool flag;
8109 [ # # ]: 0 : if (unlikely (dwarf_formflag (attrp, &flag) != 0))
8110 : 0 : goto attrval_out;
8111 : :
8112 : 0 : printf (" %*s%-20s (%s) %s\n",
8113 : : (int) (level * 2), "", dwarf_attr_name (attr),
8114 [ # # ]: 0 : dwarf_form_name (form), flag ? yes_str : no_str);
8115 : : break;
8116 : :
8117 : 0 : case DW_FORM_flag_present:
8118 [ # # ]: 0 : if (cbargs->silent)
8119 : : break;
8120 : 0 : printf (" %*s%-20s (%s) %s\n",
8121 : : (int) (level * 2), "", dwarf_attr_name (attr),
8122 : : dwarf_form_name (form), yes_str);
8123 : : break;
8124 : :
8125 : 0 : case DW_FORM_exprloc:
8126 : : case DW_FORM_block4:
8127 : : case DW_FORM_block2:
8128 : : case DW_FORM_block1:
8129 : : case DW_FORM_block:
8130 : : case DW_FORM_data16: /* DWARF5 calls this a constant class. */
8131 [ # # ]: 0 : if (cbargs->silent)
8132 : : break;
8133 : 0 : Dwarf_Block block;
8134 [ # # ]: 0 : if (unlikely (dwarf_formblock (attrp, &block) != 0))
8135 : 0 : goto attrval_out;
8136 : :
8137 : 0 : printf (" %*s%-20s (%s) ",
8138 : : (int) (level * 2), "", dwarf_attr_name (attr),
8139 : : dwarf_form_name (form));
8140 : :
8141 [ # # # ]: 0 : switch (attr)
8142 : : {
8143 : 0 : default:
8144 [ # # ]: 0 : if (form != DW_FORM_exprloc)
8145 : : {
8146 : 0 : print_block (block.length, block.data);
8147 : 0 : break;
8148 : : }
8149 : 0 : FALLTHROUGH;
8150 : :
8151 : : case DW_AT_location:
8152 : : case DW_AT_data_location:
8153 : : case DW_AT_data_member_location:
8154 : : case DW_AT_vtable_elem_location:
8155 : : case DW_AT_string_length:
8156 : : case DW_AT_use_location:
8157 : : case DW_AT_frame_base:
8158 : : case DW_AT_return_addr:
8159 : : case DW_AT_static_link:
8160 : : case DW_AT_allocated:
8161 : : case DW_AT_associated:
8162 : : case DW_AT_bit_size:
8163 : : case DW_AT_bit_offset:
8164 : : case DW_AT_bit_stride:
8165 : : case DW_AT_byte_size:
8166 : : case DW_AT_byte_stride:
8167 : : case DW_AT_count:
8168 : : case DW_AT_lower_bound:
8169 : : case DW_AT_upper_bound:
8170 : : case DW_AT_GNU_call_site_value:
8171 : : case DW_AT_GNU_call_site_data_value:
8172 : : case DW_AT_GNU_call_site_target:
8173 : : case DW_AT_GNU_call_site_target_clobbered:
8174 [ # # ]: 0 : if (form == DW_FORM_exprloc
8175 [ # # ]: 0 : || (form != DW_FORM_data16
8176 [ # # ]: 0 : && attrp->cu->version < 4)) /* blocks were expressions. */
8177 : : {
8178 : 0 : putchar ('\n');
8179 : 0 : print_ops (cbargs->dwflmod, cbargs->dbg,
8180 : 0 : 12 + level * 2, 12 + level * 2,
8181 : : cbargs->version, cbargs->addrsize, cbargs->offset_size,
8182 : 0 : attrp->cu, block.length, block.data);
8183 : : }
8184 : : else
8185 : 0 : print_block (block.length, block.data);
8186 : : break;
8187 : :
8188 : 0 : case DW_AT_discr_list:
8189 [ # # ]: 0 : if (block.length == 0)
8190 : 0 : puts ("<default>");
8191 [ # # ]: 0 : else if (form != DW_FORM_data16)
8192 : : {
8193 : 0 : const unsigned char *readp = block.data;
8194 : 0 : const unsigned char *readendp = readp + block.length;
8195 : :
8196 : : /* See if we are dealing with a signed or unsigned
8197 : : values. If the parent of this variant DIE is a
8198 : : variant_part then it will either have a discriminant
8199 : : which points to the member which type is the
8200 : : discriminant type. Or the variant_part itself has a
8201 : : type representing the discriminant. */
8202 : 0 : bool is_signed = false;
8203 [ # # ]: 0 : if (level > 0)
8204 : : {
8205 : 0 : Dwarf_Die *parent = &cbargs->dies[level - 1];
8206 [ # # ]: 0 : if (dwarf_tag (die) == DW_TAG_variant
8207 [ # # ]: 0 : && dwarf_tag (parent) == DW_TAG_variant_part)
8208 : : {
8209 : 0 : Dwarf_Die member;
8210 : 0 : Dwarf_Attribute discr_attr;
8211 : 0 : int bytes;
8212 [ # # ]: 0 : if (dwarf_formref_die (dwarf_attr (parent,
8213 : : DW_AT_discr,
8214 : : &discr_attr),
8215 : : &member) != NULL)
8216 : 0 : die_type_sign_bytes (&member, &is_signed, &bytes);
8217 : : else
8218 : 0 : die_type_sign_bytes (parent, &is_signed, &bytes);
8219 : : }
8220 : : }
8221 : 0 : while (readp < readendp)
8222 : : {
8223 : 0 : int d = (int) *readp++;
8224 : 0 : printf ("%s ", dwarf_discr_list_name (d));
8225 [ # # ]: 0 : if (readp >= readendp)
8226 : 0 : goto attrval_out;
8227 : :
8228 : 0 : Dwarf_Word val;
8229 : 0 : Dwarf_Sword sval;
8230 [ # # ]: 0 : if (d == DW_DSC_label)
8231 : : {
8232 [ # # ]: 0 : if (is_signed)
8233 : : {
8234 : 0 : get_sleb128 (sval, readp, readendp);
8235 : 0 : printf ("%" PRId64 "", sval);
8236 : : }
8237 : : else
8238 : : {
8239 : 0 : get_uleb128 (val, readp, readendp);
8240 : 0 : printf ("%" PRIu64 "", val);
8241 : : }
8242 : : }
8243 [ # # ]: 0 : else if (d == DW_DSC_range)
8244 : : {
8245 [ # # ]: 0 : if (is_signed)
8246 : : {
8247 : 0 : get_sleb128 (sval, readp, readendp);
8248 : 0 : printf ("%" PRId64 "..", sval);
8249 [ # # ]: 0 : if (readp >= readendp)
8250 : 0 : goto attrval_out;
8251 : 0 : get_sleb128 (sval, readp, readendp);
8252 : 0 : printf ("%" PRId64 "", sval);
8253 : : }
8254 : : else
8255 : : {
8256 : 0 : get_uleb128 (val, readp, readendp);
8257 : 0 : printf ("%" PRIu64 "..", val);
8258 [ # # ]: 0 : if (readp >= readendp)
8259 : 0 : goto attrval_out;
8260 : 0 : get_uleb128 (val, readp, readendp);
8261 : 0 : printf ("%" PRIu64 "", val);
8262 : : }
8263 : : }
8264 : : else
8265 : : {
8266 : 0 : print_block (readendp - readp, readp);
8267 : 0 : break;
8268 : : }
8269 [ # # ]: 0 : if (readp < readendp)
8270 [ # # ]: 0 : printf (", ");
8271 : : }
8272 : 0 : putchar ('\n');
8273 : : }
8274 : : else
8275 : 0 : print_block (block.length, block.data);
8276 : : break;
8277 : : }
8278 : : break;
8279 : :
8280 : 0 : default:
8281 [ # # ]: 0 : if (cbargs->silent)
8282 : : break;
8283 : 0 : printf (" %*s%-20s (%s) ???\n",
8284 : : (int) (level * 2), "", dwarf_attr_name (attr),
8285 : : dwarf_form_name (form));
8286 : : break;
8287 : : }
8288 : :
8289 : 0 : return DWARF_CB_OK;
8290 : : }
8291 : :
8292 : : static void
8293 : 0 : print_debug_units (Dwfl_Module *dwflmod,
8294 : : Ebl *ebl, GElf_Ehdr *ehdr __attribute__ ((unused)),
8295 : : Elf_Scn *scn, GElf_Shdr *shdr,
8296 : : Dwarf *dbg, bool debug_types)
8297 : : {
8298 [ # # # # ]: 0 : const bool silent = !(print_debug_sections & section_info) && !debug_types;
8299 : 0 : const char *secname = section_name (ebl, shdr);
8300 : :
8301 : : /* Check section actually exists. */
8302 [ # # ]: 0 : if (!silent)
8303 [ # # ]: 0 : if (get_debug_elf_data (dbg, ebl,
8304 : : debug_types ? IDX_debug_types : IDX_debug_info,
8305 : : scn) == NULL)
8306 : 0 : return;
8307 : :
8308 : 0 : if (!silent)
8309 : 0 : printf (_("\
8310 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n [Offset]\n"),
8311 : 0 : elf_ndxscn (scn), secname, (uint64_t) shdr->sh_offset);
8312 : :
8313 : : /* If the section is empty we don't have to do anything. */
8314 [ # # # # ]: 0 : if (!silent && shdr->sh_size == 0)
8315 : : return;
8316 : :
8317 : 0 : int maxdies = 20;
8318 : 0 : Dwarf_Die *dies = xmalloc (maxdies * sizeof (Dwarf_Die));
8319 : :
8320 : : /* New compilation unit. */
8321 : 0 : Dwarf_Half version;
8322 : :
8323 : 0 : Dwarf_Die result;
8324 : 0 : Dwarf_Off abbroffset;
8325 : 0 : uint8_t addrsize;
8326 : 0 : uint8_t offsize;
8327 : 0 : uint64_t unit_id;
8328 : 0 : Dwarf_Off subdie_off;
8329 : :
8330 : 0 : int unit_res;
8331 : 0 : Dwarf_CU *cu;
8332 : 0 : Dwarf_CU cu_mem;
8333 : 0 : uint8_t unit_type;
8334 : 0 : Dwarf_Die cudie;
8335 : :
8336 : : /* We cheat a little because we want to see only the CUs from .debug_info
8337 : : or .debug_types. We know the Dwarf_CU struct layout. Set it up at
8338 : : the end of .debug_info if we want .debug_types only. Check the returned
8339 : : Dwarf_CU is still in the expected section. */
8340 [ # # ]: 0 : if (debug_types)
8341 : : {
8342 : 0 : cu_mem.dbg = dbg;
8343 : 0 : cu_mem.end = dbg->sectiondata[IDX_debug_info]->d_size;
8344 : 0 : cu_mem.sec_idx = IDX_debug_info;
8345 : 0 : cu = &cu_mem;
8346 : : }
8347 : : else
8348 : 0 : cu = NULL;
8349 : :
8350 : : next_cu:
8351 : 0 : unit_res = dwarf_get_units (dbg, cu, &cu, &version, &unit_type,
8352 : : &cudie, NULL);
8353 [ # # ]: 0 : if (unit_res == 1)
8354 : 0 : goto do_return;
8355 : :
8356 [ # # ]: 0 : if (unit_res == -1)
8357 : : {
8358 [ # # ]: 0 : if (!silent)
8359 : 0 : error (0, 0, _("cannot get next unit: %s"), dwarf_errmsg (-1));
8360 : 0 : goto do_return;
8361 : : }
8362 : :
8363 [ # # # # ]: 0 : if (cu->sec_idx != (size_t) (debug_types ? IDX_debug_types : IDX_debug_info))
8364 : 0 : goto do_return;
8365 : :
8366 : 0 : dwarf_cu_die (cu, &result, NULL, &abbroffset, &addrsize, &offsize,
8367 : : &unit_id, &subdie_off);
8368 : :
8369 [ # # ]: 0 : if (!silent)
8370 : : {
8371 : 0 : Dwarf_Off offset = cu->start;
8372 [ # # # # ]: 0 : if (debug_types && version < 5)
8373 : 0 : {
8374 : 0 : Dwarf_Die typedie;
8375 : 0 : Dwarf_Off dieoffset;
8376 : 0 : dieoffset = dwarf_dieoffset (dwarf_offdie_types (dbg, cu->start
8377 : : + subdie_off,
8378 : : &typedie));
8379 : 0 : printf (_(" Type unit at offset %" PRIu64 ":\n"
8380 : : " Version: %" PRIu16
8381 : : ", Abbreviation section offset: %" PRIu64
8382 : : ", Address size: %" PRIu8
8383 : : ", Offset size: %" PRIu8
8384 : : "\n Type signature: %#" PRIx64
8385 : : ", Type offset: %#" PRIx64 " [%" PRIx64 "]\n"),
8386 : : (uint64_t) offset, version, abbroffset, addrsize, offsize,
8387 : : unit_id, (uint64_t) subdie_off, dieoffset);
8388 : : }
8389 : : else
8390 : : {
8391 : 0 : printf (_(" Compilation unit at offset %" PRIu64 ":\n"
8392 : : " Version: %" PRIu16
8393 : : ", Abbreviation section offset: %" PRIu64
8394 : : ", Address size: %" PRIu8
8395 : : ", Offset size: %" PRIu8 "\n"),
8396 : : (uint64_t) offset, version, abbroffset, addrsize, offsize);
8397 : :
8398 [ # # ]: 0 : if (version >= 5 || (unit_type != DW_UT_compile
8399 [ # # ]: 0 : && unit_type != DW_UT_partial))
8400 : : {
8401 : 0 : printf (_(" Unit type: %s (%" PRIu8 ")"),
8402 : : dwarf_unit_name (unit_type), unit_type);
8403 : 0 : if (unit_type == DW_UT_type
8404 [ # # ]: 0 : || unit_type == DW_UT_skeleton
8405 [ # # ]: 0 : || unit_type == DW_UT_split_compile
8406 [ # # ]: 0 : || unit_type == DW_UT_split_type)
8407 : 0 : printf (", Unit id: 0x%.16" PRIx64 "", unit_id);
8408 : 0 : if (unit_type == DW_UT_type
8409 [ # # ]: 0 : || unit_type == DW_UT_split_type)
8410 : : {
8411 : 0 : Dwarf_Die typedie;
8412 : 0 : Dwarf_Off dieoffset;
8413 : 0 : dwarf_cu_info (cu, NULL, NULL, NULL, &typedie,
8414 : : NULL, NULL, NULL);
8415 : 0 : dieoffset = dwarf_dieoffset (&typedie);
8416 : 0 : printf (", Unit DIE off: %#" PRIx64 " [%" PRIx64 "]",
8417 : : subdie_off, dieoffset);
8418 : : }
8419 : 0 : printf ("\n");
8420 : : }
8421 : : }
8422 : : }
8423 : :
8424 [ # # ]: 0 : if (version < 2 || version > 5
8425 [ # # # # ]: 0 : || unit_type < DW_UT_compile || unit_type > DW_UT_split_type)
8426 : : {
8427 [ # # ]: 0 : if (!silent)
8428 : 0 : error (0, 0, _("unknown version (%d) or unit type (%d)"),
8429 : : version, unit_type);
8430 : 0 : goto next_cu;
8431 : : }
8432 : :
8433 : 0 : struct attrcb_args args =
8434 : : {
8435 : : .dwflmod = dwflmod,
8436 : : .silent = silent,
8437 : : .version = version,
8438 : : .addrsize = addrsize,
8439 : : .offset_size = offsize
8440 : : };
8441 : :
8442 : 0 : bool is_split = false;
8443 : 0 : int level = 0;
8444 : 0 : dies[0] = cudie;
8445 : 0 : args.cu = dies[0].cu;
8446 : 0 : args.dbg = dbg;
8447 : 0 : args.is_split = is_split;
8448 : :
8449 : : /* We might return here again for the split CU subdie. */
8450 : : do_cu:
8451 : 0 : do
8452 : : {
8453 : 0 : Dwarf_Off offset = dwarf_dieoffset (&dies[level]);
8454 [ # # ]: 0 : if (unlikely (offset == (Dwarf_Off) -1))
8455 : : {
8456 [ # # ]: 0 : if (!silent)
8457 : 0 : error (0, 0, _("cannot get DIE offset: %s"),
8458 : : dwarf_errmsg (-1));
8459 : 0 : goto do_return;
8460 : : }
8461 : :
8462 : 0 : int tag = dwarf_tag (&dies[level]);
8463 [ # # ]: 0 : if (unlikely (tag == DW_TAG_invalid))
8464 : : {
8465 [ # # ]: 0 : if (!silent)
8466 : 0 : error (0, 0, _("cannot get tag of DIE at offset [%" PRIx64
8467 : : "] in section '%s': %s"),
8468 : : (uint64_t) offset, secname, dwarf_errmsg (-1));
8469 : 0 : goto do_return;
8470 : : }
8471 : :
8472 [ # # ]: 0 : if (!silent)
8473 : : {
8474 : 0 : unsigned int code = dwarf_getabbrevcode (dies[level].abbrev);
8475 [ # # ]: 0 : if (is_split)
8476 : 0 : printf (" {%6" PRIx64 "} ", (uint64_t) offset);
8477 : : else
8478 : 0 : printf (" [%6" PRIx64 "] ", (uint64_t) offset);
8479 : 0 : printf ("%*s%-20s abbrev: %u\n", (int) (level * 2), "",
8480 : : dwarf_tag_name (tag), code);
8481 : : }
8482 : :
8483 : : /* Print the attribute values. */
8484 : 0 : args.level = level;
8485 : 0 : args.dies = dies;
8486 : 0 : (void) dwarf_getattrs (&dies[level], attr_callback, &args, 0);
8487 : :
8488 : : /* Make room for the next level's DIE. */
8489 [ # # ]: 0 : if (level + 1 == maxdies)
8490 : 0 : dies = xrealloc (dies, (maxdies += 10) * sizeof (Dwarf_Die));
8491 : :
8492 : 0 : int res = dwarf_child (&dies[level], &dies[level + 1]);
8493 [ # # ]: 0 : if (res > 0)
8494 : : {
8495 [ # # ]: 0 : while ((res = dwarf_siblingof (&dies[level], &dies[level])) == 1)
8496 [ # # ]: 0 : if (level-- == 0)
8497 : : break;
8498 : :
8499 [ # # ]: 0 : if (unlikely (res == -1))
8500 : : {
8501 [ # # ]: 0 : if (!silent)
8502 : 0 : error (0, 0, _("cannot get next DIE: %s\n"),
8503 : : dwarf_errmsg (-1));
8504 : 0 : goto do_return;
8505 : : }
8506 : : }
8507 [ # # ]: 0 : else if (unlikely (res < 0))
8508 : : {
8509 [ # # ]: 0 : if (!silent)
8510 : 0 : error (0, 0, _("cannot get next DIE: %s"),
8511 : : dwarf_errmsg (-1));
8512 : 0 : goto do_return;
8513 : : }
8514 : : else
8515 : : ++level;
8516 : : }
8517 [ # # ]: 0 : while (level >= 0);
8518 : :
8519 : : /* We might want to show the split compile unit if this was a skeleton.
8520 : : We need to scan it if we are requesting printing .debug_ranges for
8521 : : DWARF4 since GNU DebugFission uses "offsets" into the main ranges
8522 : : section. */
8523 [ # # ]: 0 : if (unit_type == DW_UT_skeleton
8524 [ # # # # ]: 0 : && ((!silent && show_split_units)
8525 [ # # # # ]: 0 : || (version < 5 && (print_debug_sections & section_ranges) != 0)))
8526 : : {
8527 : 0 : Dwarf_Die subdie;
8528 [ # # ]: 0 : if (dwarf_cu_info (cu, NULL, NULL, NULL, &subdie, NULL, NULL, NULL) != 0
8529 [ # # ]: 0 : || dwarf_tag (&subdie) == DW_TAG_invalid)
8530 : : {
8531 [ # # ]: 0 : if (!silent)
8532 : : {
8533 : 0 : Dwarf_Attribute dwo_at;
8534 : 0 : const char *dwo_name =
8535 : 0 : (dwarf_formstring (dwarf_attr (&cudie, DW_AT_dwo_name,
8536 : : &dwo_at))
8537 [ # # ]: 0 : ?: (dwarf_formstring (dwarf_attr (&cudie, DW_AT_GNU_dwo_name,
8538 : : &dwo_at))
8539 [ # # ]: 0 : ?: "<unknown>"));
8540 : 0 : fprintf (stderr,
8541 : : "Could not find split unit '%s', id: %" PRIx64 "\n",
8542 : : dwo_name, unit_id);
8543 : : }
8544 : : }
8545 : : else
8546 : : {
8547 : 0 : Dwarf_CU *split_cu = subdie.cu;
8548 : 0 : dwarf_cu_die (split_cu, &result, NULL, &abbroffset,
8549 : : &addrsize, &offsize, &unit_id, &subdie_off);
8550 : 0 : Dwarf_Off offset = cu->start;
8551 : :
8552 [ # # ]: 0 : if (!silent)
8553 : : {
8554 : 0 : printf (_(" Split compilation unit at offset %"
8555 : : PRIu64 ":\n"
8556 : : " Version: %" PRIu16
8557 : : ", Abbreviation section offset: %" PRIu64
8558 : : ", Address size: %" PRIu8
8559 : : ", Offset size: %" PRIu8 "\n"),
8560 : : (uint64_t) offset, version, abbroffset,
8561 : : addrsize, offsize);
8562 : 0 : printf (_(" Unit type: %s (%" PRIu8 ")"),
8563 : : dwarf_unit_name (unit_type), unit_type);
8564 : 0 : printf (", Unit id: 0x%.16" PRIx64 "", unit_id);
8565 : 0 : printf ("\n");
8566 : : }
8567 : :
8568 : 0 : unit_type = DW_UT_split_compile;
8569 : 0 : is_split = true;
8570 : 0 : level = 0;
8571 : 0 : dies[0] = subdie;
8572 : 0 : args.cu = dies[0].cu;
8573 : 0 : args.dbg = split_cu->dbg;
8574 : 0 : args.is_split = is_split;
8575 : 0 : goto do_cu;
8576 : : }
8577 : : }
8578 : :
8579 : : /* And again... */
8580 : 0 : goto next_cu;
8581 : :
8582 : 0 : do_return:
8583 : 0 : free (dies);
8584 : : }
8585 : :
8586 : : static void
8587 : 0 : print_debug_info_section (Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr,
8588 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
8589 : : {
8590 : 0 : print_debug_units (dwflmod, ebl, ehdr, scn, shdr, dbg, false);
8591 : 0 : }
8592 : :
8593 : : static void
8594 : 0 : print_debug_types_section (Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr,
8595 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
8596 : : {
8597 : 0 : print_debug_units (dwflmod, ebl, ehdr, scn, shdr, dbg, true);
8598 : 0 : }
8599 : :
8600 : :
8601 : : static void
8602 : 0 : print_decoded_line_section (Dwfl_Module *dwflmod, Ebl *ebl,
8603 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
8604 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
8605 : : {
8606 : 0 : printf (_("\
8607 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n\n"),
8608 : : elf_ndxscn (scn), section_name (ebl, shdr),
8609 : 0 : (uint64_t) shdr->sh_offset);
8610 : :
8611 : 0 : size_t address_size
8612 [ # # ]: 0 : = elf_getident (ebl->elf, NULL)[EI_CLASS] == ELFCLASS32 ? 4 : 8;
8613 : :
8614 : 0 : Dwarf_Lines *lines;
8615 : 0 : size_t nlines;
8616 : 0 : Dwarf_Off off, next_off = 0;
8617 : 0 : Dwarf_CU *cu = NULL;
8618 : 0 : while (dwarf_next_lines (dbg, off = next_off, &next_off, &cu, NULL, NULL,
8619 [ # # ]: 0 : &lines, &nlines) == 0)
8620 : : {
8621 : 0 : Dwarf_Die cudie;
8622 [ # # # # ]: 0 : if (cu != NULL && dwarf_cu_info (cu, NULL, NULL, &cudie,
8623 : : NULL, NULL, NULL, NULL) == 0)
8624 : 0 : printf (" CU [%" PRIx64 "] %s\n",
8625 : : dwarf_dieoffset (&cudie), dwarf_diename (&cudie));
8626 : : else
8627 : : {
8628 : : /* DWARF5 lines can be independent of any CU, but they probably
8629 : : are used by some CU. Determine the CU this block is for. */
8630 : 0 : Dwarf_Off cuoffset;
8631 : 0 : Dwarf_Off ncuoffset = 0;
8632 : 0 : size_t hsize;
8633 : 0 : while (dwarf_nextcu (dbg, cuoffset = ncuoffset, &ncuoffset, &hsize,
8634 [ # # ]: 0 : NULL, NULL, NULL) == 0)
8635 : : {
8636 [ # # ]: 0 : if (dwarf_offdie (dbg, cuoffset + hsize, &cudie) == NULL)
8637 : 0 : continue;
8638 : 0 : Dwarf_Attribute stmt_list;
8639 [ # # ]: 0 : if (dwarf_attr (&cudie, DW_AT_stmt_list, &stmt_list) == NULL)
8640 : 0 : continue;
8641 : 0 : Dwarf_Word lineoff;
8642 [ # # ]: 0 : if (dwarf_formudata (&stmt_list, &lineoff) != 0)
8643 : 0 : continue;
8644 [ # # ]: 0 : if (lineoff == off)
8645 : : {
8646 : : /* Found the CU. */
8647 : 0 : cu = cudie.cu;
8648 : 0 : break;
8649 : : }
8650 : : }
8651 : :
8652 [ # # ]: 0 : if (cu != NULL)
8653 : 0 : printf (" CU [%" PRIx64 "] %s\n",
8654 : : dwarf_dieoffset (&cudie), dwarf_diename (&cudie));
8655 : : else
8656 : 0 : printf (" No CU\n");
8657 : : }
8658 : :
8659 : 0 : printf (" line:col SBPE* disc isa op address"
8660 : : " (Statement Block Prologue Epilogue *End)\n");
8661 : 0 : const char *last_file = "";
8662 [ # # ]: 0 : for (size_t n = 0; n < nlines; n++)
8663 : : {
8664 : 0 : Dwarf_Line *line = dwarf_onesrcline (lines, n);
8665 [ # # ]: 0 : if (line == NULL)
8666 : : {
8667 : 0 : printf (" dwarf_onesrcline: %s\n", dwarf_errmsg (-1));
8668 : 0 : continue;
8669 : : }
8670 : 0 : Dwarf_Word mtime, length;
8671 : 0 : const char *file = dwarf_linesrc (line, &mtime, &length);
8672 [ # # ]: 0 : if (file == NULL)
8673 : : {
8674 : 0 : printf (" <%s> (mtime: ?, length: ?)\n", dwarf_errmsg (-1));
8675 : 0 : last_file = "";
8676 : : }
8677 [ # # ]: 0 : else if (strcmp (last_file, file) != 0)
8678 : : {
8679 : 0 : printf (" %s (mtime: %" PRIu64 ", length: %" PRIu64 ")\n",
8680 : : file, mtime, length);
8681 : 0 : last_file = file;
8682 : : }
8683 : :
8684 : 0 : int lineno, colno;
8685 : 0 : bool statement, endseq, block, prologue_end, epilogue_begin;
8686 : 0 : unsigned int lineop, isa, disc;
8687 : 0 : Dwarf_Addr address;
8688 : 0 : dwarf_lineaddr (line, &address);
8689 : 0 : dwarf_lineno (line, &lineno);
8690 : 0 : dwarf_linecol (line, &colno);
8691 : 0 : dwarf_lineop_index (line, &lineop);
8692 : 0 : dwarf_linebeginstatement (line, &statement);
8693 : 0 : dwarf_lineendsequence (line, &endseq);
8694 : 0 : dwarf_lineblock (line, &block);
8695 : 0 : dwarf_lineprologueend (line, &prologue_end);
8696 : 0 : dwarf_lineepiloguebegin (line, &epilogue_begin);
8697 : 0 : dwarf_lineisa (line, &isa);
8698 : 0 : dwarf_linediscriminator (line, &disc);
8699 : :
8700 : : /* End sequence is special, it is one byte past. */
8701 : 0 : printf (" %4d:%-3d %c%c%c%c%c %4d %3d %2d ",
8702 : : lineno, colno,
8703 [ # # ]: 0 : (statement ? 'S' : ' '),
8704 [ # # ]: 0 : (block ? 'B' : ' '),
8705 [ # # ]: 0 : (prologue_end ? 'P' : ' '),
8706 [ # # ]: 0 : (epilogue_begin ? 'E' : ' '),
8707 [ # # ]: 0 : (endseq ? '*' : ' '),
8708 : : disc, isa, lineop);
8709 : 0 : print_dwarf_addr (dwflmod, address_size,
8710 [ # # ]: 0 : address - (endseq ? 1 : 0), address);
8711 : 0 : printf ("\n");
8712 : :
8713 [ # # ]: 0 : if (endseq)
8714 : 0 : printf("\n");
8715 : : }
8716 : : }
8717 : 0 : }
8718 : :
8719 : :
8720 : : /* Print the value of a form.
8721 : : Returns new value of readp, or readendp on failure. */
8722 : : static const unsigned char *
8723 : 0 : print_form_data (Dwarf *dbg, int form, const unsigned char *readp,
8724 : : const unsigned char *readendp, unsigned int offset_len,
8725 : : Dwarf_Off str_offsets_base)
8726 : : {
8727 : 0 : Dwarf_Word val;
8728 : 0 : unsigned char *endp;
8729 : 0 : Elf_Data *data;
8730 : 0 : char *str;
8731 [ # # # # : 0 : switch (form)
# # # # #
# # # # #
# # # # #
# # ]
8732 : : {
8733 : 0 : case DW_FORM_data1:
8734 [ # # ]: 0 : if (readendp - readp < 1)
8735 : : {
8736 : 0 : invalid_data:
8737 : 0 : error (0, 0, "invalid data");
8738 : 0 : return readendp;
8739 : : }
8740 : 0 : val = *readp++;
8741 : 0 : printf (" %" PRIx8, (unsigned int) val);
8742 : : break;
8743 : :
8744 : 0 : case DW_FORM_data2:
8745 [ # # ]: 0 : if (readendp - readp < 2)
8746 : 0 : goto invalid_data;
8747 [ # # ]: 0 : val = read_2ubyte_unaligned_inc (dbg, readp);
8748 : 0 : printf(" %" PRIx16, (unsigned int) val);
8749 : : break;
8750 : :
8751 : 0 : case DW_FORM_data4:
8752 [ # # ]: 0 : if (readendp - readp < 4)
8753 : 0 : goto invalid_data;
8754 [ # # ]: 0 : val = read_4ubyte_unaligned_inc (dbg, readp);
8755 : 0 : printf (" %" PRIx32, (unsigned int) val);
8756 : : break;
8757 : :
8758 : 0 : case DW_FORM_data8:
8759 [ # # ]: 0 : if (readendp - readp < 8)
8760 : 0 : goto invalid_data;
8761 [ # # ]: 0 : val = read_8ubyte_unaligned_inc (dbg, readp);
8762 : 0 : printf (" %" PRIx64, val);
8763 : : break;
8764 : :
8765 : 0 : case DW_FORM_sdata:
8766 [ # # ]: 0 : if (readendp - readp < 1)
8767 : 0 : goto invalid_data;
8768 : 0 : get_sleb128 (val, readp, readendp);
8769 : 0 : printf (" %" PRIx64, val);
8770 : : break;
8771 : :
8772 : 0 : case DW_FORM_udata:
8773 [ # # ]: 0 : if (readendp - readp < 1)
8774 : 0 : goto invalid_data;
8775 : 0 : get_uleb128 (val, readp, readendp);
8776 : 0 : printf (" %" PRIx64, val);
8777 : : break;
8778 : :
8779 : 0 : case DW_FORM_block:
8780 [ # # ]: 0 : if (readendp - readp < 1)
8781 : 0 : goto invalid_data;
8782 : 0 : get_uleb128 (val, readp, readendp);
8783 [ # # ]: 0 : if ((size_t) (readendp - readp) < val)
8784 : 0 : goto invalid_data;
8785 : 0 : print_bytes (val, readp);
8786 : 0 : readp += val;
8787 : 0 : break;
8788 : :
8789 : 0 : case DW_FORM_block1:
8790 [ # # ]: 0 : if (readendp - readp < 1)
8791 : 0 : goto invalid_data;
8792 : 0 : val = *readp++;
8793 [ # # ]: 0 : if ((size_t) (readendp - readp) < val)
8794 : 0 : goto invalid_data;
8795 : 0 : print_bytes (val, readp);
8796 : 0 : readp += val;
8797 : 0 : break;
8798 : :
8799 : 0 : case DW_FORM_block2:
8800 [ # # ]: 0 : if (readendp - readp < 2)
8801 : 0 : goto invalid_data;
8802 [ # # ]: 0 : val = read_2ubyte_unaligned_inc (dbg, readp);
8803 [ # # ]: 0 : if ((size_t) (readendp - readp) < val)
8804 : 0 : goto invalid_data;
8805 : 0 : print_bytes (val, readp);
8806 : 0 : readp += val;
8807 : 0 : break;
8808 : :
8809 : 0 : case DW_FORM_block4:
8810 [ # # ]: 0 : if (readendp - readp < 4)
8811 : 0 : goto invalid_data;
8812 [ # # ]: 0 : val = read_4ubyte_unaligned_inc (dbg, readp);
8813 [ # # ]: 0 : if ((size_t) (readendp - readp) < val)
8814 : 0 : goto invalid_data;
8815 : 0 : print_bytes (val, readp);
8816 : 0 : readp += val;
8817 : 0 : break;
8818 : :
8819 : 0 : case DW_FORM_data16:
8820 [ # # ]: 0 : if (readendp - readp < 16)
8821 : 0 : goto invalid_data;
8822 : 0 : print_bytes (16, readp);
8823 : 0 : readp += 16;
8824 : 0 : break;
8825 : :
8826 : 0 : case DW_FORM_flag:
8827 [ # # ]: 0 : if (readendp - readp < 1)
8828 : 0 : goto invalid_data;
8829 : 0 : val = *readp++;
8830 [ # # ]: 0 : printf ("%s", val != 0 ? yes_str : no_str);
8831 : : break;
8832 : :
8833 : 0 : case DW_FORM_string:
8834 : 0 : endp = memchr (readp, '\0', readendp - readp);
8835 [ # # ]: 0 : if (endp == NULL)
8836 : 0 : goto invalid_data;
8837 : 0 : printf ("%s", readp);
8838 : 0 : readp = endp + 1;
8839 : 0 : break;
8840 : :
8841 : 0 : case DW_FORM_strp:
8842 : : case DW_FORM_line_strp:
8843 : : case DW_FORM_strp_sup:
8844 [ # # ]: 0 : if ((size_t) (readendp - readp) < offset_len)
8845 : 0 : goto invalid_data;
8846 [ # # ]: 0 : if (offset_len == 8)
8847 [ # # ]: 0 : val = read_8ubyte_unaligned_inc (dbg, readp);
8848 : : else
8849 [ # # ]: 0 : val = read_4ubyte_unaligned_inc (dbg, readp);
8850 [ # # ]: 0 : if (form == DW_FORM_strp)
8851 : 0 : data = dbg->sectiondata[IDX_debug_str];
8852 [ # # ]: 0 : else if (form == DW_FORM_line_strp)
8853 : 0 : data = dbg->sectiondata[IDX_debug_line_str];
8854 : : else /* form == DW_FORM_strp_sup */
8855 : : {
8856 : 0 : Dwarf *alt = dwarf_getalt (dbg);
8857 [ # # ]: 0 : data = alt != NULL ? alt->sectiondata[IDX_debug_str] : NULL;
8858 : : }
8859 [ # # # # ]: 0 : if (data == NULL || val >= data->d_size
8860 [ # # ]: 0 : || memchr (data->d_buf + val, '\0', data->d_size - val) == NULL)
8861 : : str = "???";
8862 : : else
8863 : 0 : str = (char *) data->d_buf + val;
8864 : 0 : printf ("%s (%" PRIu64 ")", str, val);
8865 : : break;
8866 : :
8867 : 0 : case DW_FORM_sec_offset:
8868 [ # # ]: 0 : if ((size_t) (readendp - readp) < offset_len)
8869 : 0 : goto invalid_data;
8870 [ # # ]: 0 : if (offset_len == 8)
8871 [ # # ]: 0 : val = read_8ubyte_unaligned_inc (dbg, readp);
8872 : : else
8873 [ # # ]: 0 : val = read_4ubyte_unaligned_inc (dbg, readp);
8874 : 0 : printf ("[%" PRIx64 "]", val);
8875 : : break;
8876 : :
8877 : 0 : case DW_FORM_strx:
8878 : : case DW_FORM_GNU_str_index:
8879 [ # # ]: 0 : if (readendp - readp < 1)
8880 : 0 : goto invalid_data;
8881 : 0 : get_uleb128 (val, readp, readendp);
8882 : 0 : strx_val:
8883 : 0 : data = dbg->sectiondata[IDX_debug_str_offsets];
8884 [ # # ]: 0 : if (data == NULL
8885 [ # # ]: 0 : || data->d_size - str_offsets_base < val * offset_len)
8886 : : str = "???";
8887 : : else
8888 : : {
8889 : 0 : const unsigned char *strreadp = (data->d_buf + str_offsets_base
8890 : 0 : + val * offset_len);
8891 : 0 : const unsigned char *strreadendp = data->d_buf + data->d_size;
8892 [ # # ]: 0 : if ((size_t) (strreadendp - strreadp) < offset_len)
8893 : : str = "???";
8894 : : else
8895 : : {
8896 : 0 : Dwarf_Off idx;
8897 [ # # ]: 0 : if (offset_len == 8)
8898 [ # # ]: 0 : idx = read_8ubyte_unaligned (dbg, strreadp);
8899 : : else
8900 [ # # ]: 0 : idx = read_4ubyte_unaligned (dbg, strreadp);
8901 : :
8902 : 0 : data = dbg->sectiondata[IDX_debug_str];
8903 [ # # # # ]: 0 : if (data == NULL || idx >= data->d_size
8904 [ # # ]: 0 : || memchr (data->d_buf + idx, '\0',
8905 : : data->d_size - idx) == NULL)
8906 : : str = "???";
8907 : : else
8908 : 0 : str = (char *) data->d_buf + idx;
8909 : : }
8910 : : }
8911 : 0 : printf ("%s (%" PRIu64 ")", str, val);
8912 : : break;
8913 : :
8914 : 0 : case DW_FORM_strx1:
8915 [ # # ]: 0 : if (readendp - readp < 1)
8916 : 0 : goto invalid_data;
8917 : 0 : val = *readp++;
8918 : 0 : goto strx_val;
8919 : :
8920 : 0 : case DW_FORM_strx2:
8921 [ # # ]: 0 : if (readendp - readp < 2)
8922 : 0 : goto invalid_data;
8923 [ # # ]: 0 : val = read_2ubyte_unaligned_inc (dbg, readp);
8924 : 0 : goto strx_val;
8925 : :
8926 : 0 : case DW_FORM_strx3:
8927 [ # # ]: 0 : if (readendp - readp < 3)
8928 : 0 : goto invalid_data;
8929 : 0 : val = read_3ubyte_unaligned_inc (dbg, readp);
8930 : 0 : goto strx_val;
8931 : :
8932 : 0 : case DW_FORM_strx4:
8933 [ # # ]: 0 : if (readendp - readp < 4)
8934 : 0 : goto invalid_data;
8935 [ # # ]: 0 : val = read_4ubyte_unaligned_inc (dbg, readp);
8936 : 0 : goto strx_val;
8937 : :
8938 : 0 : default:
8939 : 0 : error (0, 0, _("unknown form: %s"), dwarf_form_name (form));
8940 : 0 : return readendp;
8941 : : }
8942 : :
8943 : 0 : return readp;
8944 : : }
8945 : :
8946 : : /* Only used via run_advance_pc() macro */
8947 : : static inline void
8948 : 0 : run_advance_pc (unsigned int op_advance,
8949 : : unsigned int minimum_instr_len,
8950 : : unsigned int max_ops_per_instr,
8951 : : unsigned int *op_addr_advance,
8952 : : Dwarf_Word *address,
8953 : : unsigned int *op_index)
8954 : : {
8955 : 0 : const unsigned int advanced_op_index = (*op_index) + op_advance;
8956 : :
8957 : 0 : *op_addr_advance = minimum_instr_len * (advanced_op_index
8958 : 0 : / max_ops_per_instr);
8959 : 0 : *address = *address + *op_addr_advance;
8960 : 0 : *op_index = advanced_op_index % max_ops_per_instr;
8961 : : }
8962 : :
8963 : : static void
8964 : 0 : print_debug_line_section (Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr,
8965 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
8966 : : {
8967 [ # # ]: 0 : if (decodedline)
8968 : : {
8969 : 0 : print_decoded_line_section (dwflmod, ebl, ehdr, scn, shdr, dbg);
8970 : 0 : return;
8971 : : }
8972 : :
8973 : 0 : Elf_Data *data = get_debug_elf_data (dbg, ebl, IDX_debug_line, scn);
8974 [ # # ]: 0 : if (data == NULL)
8975 : : return;
8976 : :
8977 : 0 : printf (_("\
8978 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
8979 : : elf_ndxscn (scn), section_name (ebl, shdr),
8980 : 0 : (uint64_t) shdr->sh_offset);
8981 : :
8982 [ # # ]: 0 : if (shdr->sh_size == 0)
8983 : : return;
8984 : :
8985 : : /* There is no functionality in libdw to read the information in the
8986 : : way it is represented here. Hardcode the decoder. */
8987 : :
8988 : 0 : const unsigned char *linep = (const unsigned char *) data->d_buf;
8989 : 0 : const unsigned char *lineendp;
8990 : :
8991 : 0 : while (linep
8992 [ # # ]: 0 : < (lineendp = (const unsigned char *) data->d_buf + data->d_size))
8993 : : {
8994 : 0 : size_t start_offset = linep - (const unsigned char *) data->d_buf;
8995 : :
8996 : 0 : printf (_("\nTable at offset %zu:\n"), start_offset);
8997 : :
8998 [ # # ]: 0 : if (unlikely (linep + 4 > lineendp))
8999 : 0 : goto invalid_data;
9000 [ # # ]: 0 : Dwarf_Word unit_length = read_4ubyte_unaligned_inc (dbg, linep);
9001 : 0 : unsigned int length = 4;
9002 [ # # ]: 0 : if (unlikely (unit_length == 0xffffffff))
9003 : : {
9004 [ # # ]: 0 : if (unlikely (linep + 8 > lineendp))
9005 : : {
9006 : 0 : invalid_data:
9007 : 0 : error (0, 0, _("invalid data in section [%zu] '%s'"),
9008 : : elf_ndxscn (scn), section_name (ebl, shdr));
9009 : 0 : return;
9010 : : }
9011 [ # # ]: 0 : unit_length = read_8ubyte_unaligned_inc (dbg, linep);
9012 : 0 : length = 8;
9013 : : }
9014 : :
9015 : : /* Check whether we have enough room in the section. */
9016 [ # # ]: 0 : if (unlikely (unit_length > (size_t) (lineendp - linep)))
9017 : 0 : goto invalid_data;
9018 : 0 : lineendp = linep + unit_length;
9019 : :
9020 : : /* The next element of the header is the version identifier. */
9021 [ # # ]: 0 : if ((size_t) (lineendp - linep) < 2)
9022 : 0 : goto invalid_data;
9023 [ # # ]: 0 : uint_fast16_t version = read_2ubyte_unaligned_inc (dbg, linep);
9024 : :
9025 : 0 : size_t address_size
9026 [ # # ]: 0 : = elf_getident (ebl->elf, NULL)[EI_CLASS] == ELFCLASS32 ? 4 : 8;
9027 : 0 : unsigned char segment_selector_size = 0;
9028 [ # # ]: 0 : if (version > 4)
9029 : : {
9030 [ # # ]: 0 : if ((size_t) (lineendp - linep) < 2)
9031 : 0 : goto invalid_data;
9032 : 0 : address_size = *linep++;
9033 : 0 : segment_selector_size = *linep++;
9034 : : }
9035 : :
9036 : : /* Next comes the header length. */
9037 : 0 : Dwarf_Word header_length;
9038 [ # # ]: 0 : if (length == 4)
9039 : : {
9040 [ # # ]: 0 : if ((size_t) (lineendp - linep) < 4)
9041 : 0 : goto invalid_data;
9042 [ # # ]: 0 : header_length = read_4ubyte_unaligned_inc (dbg, linep);
9043 : : }
9044 : : else
9045 : : {
9046 [ # # ]: 0 : if ((size_t) (lineendp - linep) < 8)
9047 : 0 : goto invalid_data;
9048 [ # # ]: 0 : header_length = read_8ubyte_unaligned_inc (dbg, linep);
9049 : : }
9050 : :
9051 : 0 : const unsigned char *header_start = linep;
9052 : :
9053 : : /* Next the minimum instruction length. */
9054 [ # # ]: 0 : if ((size_t) (lineendp - linep) < 1)
9055 : 0 : goto invalid_data;
9056 : 0 : uint_fast8_t minimum_instr_len = *linep++;
9057 : :
9058 : : /* Next the maximum operations per instruction, in version 4 format. */
9059 : 0 : uint_fast8_t max_ops_per_instr;
9060 [ # # ]: 0 : if (version < 4)
9061 : : max_ops_per_instr = 1;
9062 : : else
9063 : : {
9064 [ # # ]: 0 : if ((size_t) (lineendp - linep) < 1)
9065 : 0 : goto invalid_data;
9066 : 0 : max_ops_per_instr = *linep++;
9067 : : }
9068 : :
9069 : : /* We need at least 4 more bytes. */
9070 [ # # ]: 0 : if ((size_t) (lineendp - linep) < 4)
9071 : 0 : goto invalid_data;
9072 : :
9073 : : /* Then the flag determining the default value of the is_stmt
9074 : : register. */
9075 : 0 : uint_fast8_t default_is_stmt = *linep++;
9076 : :
9077 : : /* Now the line base. */
9078 : 0 : int_fast8_t line_base = *linep++;
9079 : :
9080 : : /* And the line range. */
9081 : 0 : uint_fast8_t line_range = *linep++;
9082 : :
9083 : : /* The opcode base. */
9084 : 0 : uint_fast8_t opcode_base = *linep++;
9085 : :
9086 : : /* Print what we got so far. */
9087 : 0 : printf (_("\n"
9088 : : " Length: %" PRIu64 "\n"
9089 : : " DWARF version: %" PRIuFAST16 "\n"
9090 : : " Prologue length: %" PRIu64 "\n"
9091 : : " Address size: %zd\n"
9092 : : " Segment selector size: %zd\n"
9093 : : " Min instruction length: %" PRIuFAST8 "\n"
9094 : : " Max operations per instruction: %" PRIuFAST8 "\n"
9095 : : " Initial value if 'is_stmt': %" PRIuFAST8 "\n"
9096 : : " Line base: %" PRIdFAST8 "\n"
9097 : : " Line range: %" PRIuFAST8 "\n"
9098 : : " Opcode base: %" PRIuFAST8 "\n"
9099 : : "\n"
9100 : : "Opcodes:\n"),
9101 : : (uint64_t) unit_length, version, (uint64_t) header_length,
9102 : : address_size, (size_t) segment_selector_size,
9103 : : minimum_instr_len, max_ops_per_instr,
9104 : : default_is_stmt, line_base,
9105 : : line_range, opcode_base);
9106 : :
9107 [ # # ]: 0 : if (version < 2 || version > 5)
9108 : : {
9109 : 0 : error (0, 0, _("cannot handle .debug_line version: %u\n"),
9110 : : (unsigned int) version);
9111 : 0 : linep = lineendp;
9112 : 0 : continue;
9113 : : }
9114 : :
9115 [ # # ]: 0 : if (address_size != 4 && address_size != 8)
9116 : : {
9117 : 0 : error (0, 0, _("cannot handle address size: %u\n"),
9118 : : (unsigned int) address_size);
9119 : 0 : linep = lineendp;
9120 : 0 : continue;
9121 : : }
9122 : :
9123 [ # # ]: 0 : if (segment_selector_size != 0)
9124 : : {
9125 : 0 : error (0, 0, _("cannot handle segment selector size: %u\n"),
9126 : : (unsigned int) segment_selector_size);
9127 : 0 : linep = lineendp;
9128 : 0 : continue;
9129 : : }
9130 : :
9131 [ # # ]: 0 : if (unlikely (linep + opcode_base - 1 >= lineendp))
9132 : : {
9133 : 0 : invalid_unit:
9134 : 0 : error (0, 0,
9135 : 0 : _("invalid data at offset %tu in section [%zu] '%s'"),
9136 : 0 : linep - (const unsigned char *) data->d_buf,
9137 : : elf_ndxscn (scn), section_name (ebl, shdr));
9138 : 0 : linep = lineendp;
9139 : 0 : continue;
9140 : : }
9141 : 0 : int opcode_base_l10 = 1;
9142 : 0 : unsigned int tmp = opcode_base;
9143 [ # # ]: 0 : while (tmp > 10)
9144 : : {
9145 : 0 : tmp /= 10;
9146 : 0 : ++opcode_base_l10;
9147 : : }
9148 : : const uint8_t *standard_opcode_lengths = linep - 1;
9149 [ # # ]: 0 : for (uint_fast8_t cnt = 1; cnt < opcode_base; ++cnt)
9150 : 0 : printf (ngettext (" [%*" PRIuFAST8 "] %hhu argument\n",
9151 : : " [%*" PRIuFAST8 "] %hhu arguments\n",
9152 : : (int) linep[cnt - 1]),
9153 : 0 : opcode_base_l10, cnt, linep[cnt - 1]);
9154 : 0 : linep += opcode_base - 1;
9155 : :
9156 : 0 : if (unlikely (linep >= lineendp))
9157 : : goto invalid_unit;
9158 : :
9159 : 0 : Dwarf_Off str_offsets_base = str_offsets_base_off (dbg, NULL);
9160 : :
9161 : 0 : puts (_("\nDirectory table:"));
9162 [ # # ]: 0 : if (version > 4)
9163 : : {
9164 : 0 : struct encpair { uint16_t desc; uint16_t form; };
9165 : 0 : struct encpair enc[256];
9166 : :
9167 : 0 : printf (_(" ["));
9168 [ # # ]: 0 : if ((size_t) (lineendp - linep) < 1)
9169 : 0 : goto invalid_data;
9170 : 0 : unsigned char directory_entry_format_count = *linep++;
9171 : 0 : for (int i = 0; i < directory_entry_format_count; i++)
9172 : : {
9173 : 0 : uint16_t desc, form;
9174 [ # # ]: 0 : if ((size_t) (lineendp - linep) < 1)
9175 : 0 : goto invalid_data;
9176 : 0 : get_uleb128 (desc, linep, lineendp);
9177 [ # # ]: 0 : if ((size_t) (lineendp - linep) < 1)
9178 : 0 : goto invalid_data;
9179 : 0 : get_uleb128 (form, linep, lineendp);
9180 : :
9181 : 0 : enc[i].desc = desc;
9182 : 0 : enc[i].form = form;
9183 : :
9184 : 0 : printf ("%s(%s)",
9185 : : dwarf_line_content_description_name (desc),
9186 : : dwarf_form_name (form));
9187 [ # # ]: 0 : if (i + 1 < directory_entry_format_count)
9188 [ # # ]: 0 : printf (", ");
9189 : : }
9190 : 0 : printf ("]\n");
9191 : :
9192 : 0 : uint64_t directories_count;
9193 [ # # ]: 0 : if ((size_t) (lineendp - linep) < 1)
9194 : 0 : goto invalid_data;
9195 : 0 : get_uleb128 (directories_count, linep, lineendp);
9196 : :
9197 : 0 : if (directory_entry_format_count == 0
9198 [ # # ]: 0 : && directories_count != 0)
9199 : 0 : goto invalid_data;
9200 : :
9201 [ # # ]: 0 : for (uint64_t i = 0; i < directories_count; i++)
9202 : : {
9203 : 0 : printf (" %-5" PRIu64 " ", i);
9204 : 0 : for (int j = 0; j < directory_entry_format_count; j++)
9205 : : {
9206 : 0 : linep = print_form_data (dbg, enc[j].form,
9207 : : linep, lineendp, length,
9208 : : str_offsets_base);
9209 [ # # ]: 0 : if (j + 1 < directory_entry_format_count)
9210 [ # # ]: 0 : printf (", ");
9211 : : }
9212 : 0 : printf ("\n");
9213 [ # # ]: 0 : if (linep >= lineendp)
9214 : 0 : goto invalid_unit;
9215 : : }
9216 : : }
9217 : : else
9218 : : {
9219 [ # # # # ]: 0 : while (linep < lineendp && *linep != 0)
9220 : : {
9221 : 0 : unsigned char *endp = memchr (linep, '\0', lineendp - linep);
9222 [ # # ]: 0 : if (unlikely (endp == NULL))
9223 : 0 : goto invalid_unit;
9224 : :
9225 : 0 : printf (" %s\n", (char *) linep);
9226 : :
9227 : 0 : linep = endp + 1;
9228 : : }
9229 [ # # # # ]: 0 : if (linep >= lineendp || *linep != 0)
9230 : 0 : goto invalid_unit;
9231 : : /* Skip the final NUL byte. */
9232 : 0 : ++linep;
9233 : : }
9234 : :
9235 [ # # ]: 0 : if (unlikely (linep >= lineendp))
9236 : 0 : goto invalid_unit;
9237 : :
9238 : 0 : puts (_("\nFile name table:"));
9239 [ # # ]: 0 : if (version > 4)
9240 : : {
9241 : 0 : struct encpair { uint16_t desc; uint16_t form; };
9242 : 0 : struct encpair enc[256];
9243 : :
9244 : 0 : printf (_(" ["));
9245 [ # # ]: 0 : if ((size_t) (lineendp - linep) < 1)
9246 : 0 : goto invalid_data;
9247 : 0 : unsigned char file_name_format_count = *linep++;
9248 : 0 : for (int i = 0; i < file_name_format_count; i++)
9249 : : {
9250 : 0 : uint64_t desc, form;
9251 [ # # ]: 0 : if ((size_t) (lineendp - linep) < 1)
9252 : 0 : goto invalid_data;
9253 : 0 : get_uleb128 (desc, linep, lineendp);
9254 [ # # ]: 0 : if ((size_t) (lineendp - linep) < 1)
9255 : 0 : goto invalid_data;
9256 : 0 : get_uleb128 (form, linep, lineendp);
9257 : :
9258 [ # # ]: 0 : if (! libdw_valid_user_form (form))
9259 : 0 : goto invalid_data;
9260 : :
9261 : 0 : enc[i].desc = desc;
9262 : 0 : enc[i].form = form;
9263 : :
9264 : 0 : printf ("%s(%s)",
9265 : : dwarf_line_content_description_name (desc),
9266 : : dwarf_form_name (form));
9267 [ # # ]: 0 : if (i + 1 < file_name_format_count)
9268 [ # # ]: 0 : printf (", ");
9269 : : }
9270 : 0 : printf ("]\n");
9271 : :
9272 : 0 : uint64_t file_name_count;
9273 [ # # ]: 0 : if ((size_t) (lineendp - linep) < 1)
9274 : 0 : goto invalid_data;
9275 : 0 : get_uleb128 (file_name_count, linep, lineendp);
9276 : :
9277 : 0 : if (file_name_format_count == 0
9278 [ # # ]: 0 : && file_name_count != 0)
9279 : 0 : goto invalid_data;
9280 : :
9281 [ # # ]: 0 : for (uint64_t i = 0; i < file_name_count; i++)
9282 : : {
9283 : 0 : printf (" %-5" PRIu64 " ", i);
9284 : 0 : for (int j = 0; j < file_name_format_count; j++)
9285 : : {
9286 : 0 : linep = print_form_data (dbg, enc[j].form,
9287 : : linep, lineendp, length,
9288 : : str_offsets_base);
9289 [ # # ]: 0 : if (j + 1 < file_name_format_count)
9290 [ # # ]: 0 : printf (", ");
9291 : : }
9292 : 0 : printf ("\n");
9293 [ # # ]: 0 : if (linep > lineendp)
9294 : 0 : goto invalid_unit;
9295 : : }
9296 : : }
9297 : : else
9298 : : {
9299 : 0 : puts (_(" Entry Dir Time Size Name"));
9300 [ # # # # ]: 0 : for (unsigned int cnt = 1; linep < lineendp && *linep != 0; ++cnt)
9301 : : {
9302 : : /* First comes the file name. */
9303 : 0 : char *fname = (char *) linep;
9304 : 0 : unsigned char *endp = memchr (fname, '\0', lineendp - linep);
9305 [ # # ]: 0 : if (unlikely (endp == NULL))
9306 : 0 : goto invalid_unit;
9307 : 0 : linep = endp + 1;
9308 : :
9309 : : /* Then the index. */
9310 : 0 : unsigned int diridx;
9311 [ # # ]: 0 : if (lineendp - linep < 1)
9312 : 0 : goto invalid_unit;
9313 : 0 : get_uleb128 (diridx, linep, lineendp);
9314 : :
9315 : : /* Next comes the modification time. */
9316 : 0 : unsigned int mtime;
9317 [ # # ]: 0 : if (lineendp - linep < 1)
9318 : 0 : goto invalid_unit;
9319 : 0 : get_uleb128 (mtime, linep, lineendp);
9320 : :
9321 : : /* Finally the length of the file. */
9322 : 0 : unsigned int fsize;
9323 [ # # ]: 0 : if (lineendp - linep < 1)
9324 : 0 : goto invalid_unit;
9325 : 0 : get_uleb128 (fsize, linep, lineendp);
9326 : :
9327 : 0 : printf (" %-5u %-5u %-9u %-9u %s\n",
9328 : : cnt, diridx, mtime, fsize, fname);
9329 : : }
9330 [ # # # # ]: 0 : if (linep >= lineendp || *linep != '\0')
9331 : 0 : goto invalid_unit;
9332 : : /* Skip the final NUL byte. */
9333 : 0 : ++linep;
9334 : : }
9335 : :
9336 : 0 : unsigned int debug_str_offset = 0;
9337 [ # # ]: 0 : if (unlikely (linep == header_start + header_length - 4))
9338 : : {
9339 : : /* CUBINs contain an unsigned 4-byte offset */
9340 [ # # ]: 0 : debug_str_offset = read_4ubyte_unaligned_inc (dbg, linep);
9341 : : }
9342 : :
9343 [ # # ]: 0 : if (linep == lineendp)
9344 : : {
9345 : 0 : puts (_("\nNo line number statements."));
9346 : 0 : continue;
9347 : : }
9348 : :
9349 : 0 : puts (_("\nLine number statements:"));
9350 : 0 : Dwarf_Word address = 0;
9351 : 0 : unsigned int op_index = 0;
9352 : 0 : size_t line = 1;
9353 : 0 : uint_fast8_t is_stmt = default_is_stmt;
9354 : :
9355 : : /* Apply the "operation advance" from a special opcode
9356 : : or DW_LNS_advance_pc (as per DWARF4 6.2.5.1). */
9357 : 0 : unsigned int op_addr_advance;
9358 : : #define advance_pc(op_advance) run_advance_pc(op_advance, minimum_instr_len, \
9359 : : max_ops_per_instr, &op_addr_advance, &address, &op_index)
9360 : :
9361 [ # # ]: 0 : if (max_ops_per_instr == 0)
9362 : : {
9363 : 0 : error (0, 0,
9364 : 0 : _("invalid maximum operations per instruction is zero"));
9365 : 0 : linep = lineendp;
9366 : 0 : continue;
9367 : : }
9368 : :
9369 : 0 : while (linep < lineendp)
9370 : : {
9371 : 0 : size_t offset = linep - (const unsigned char *) data->d_buf;
9372 : 0 : unsigned int u128;
9373 : 0 : int s128;
9374 : :
9375 : : /* Read the opcode. */
9376 : 0 : unsigned int opcode = *linep++;
9377 : :
9378 : 0 : printf (" [%6" PRIx64 "]", (uint64_t)offset);
9379 : : /* Is this a special opcode? */
9380 [ # # ]: 0 : if (likely (opcode >= opcode_base))
9381 : : {
9382 [ # # ]: 0 : if (unlikely (line_range == 0))
9383 : 0 : goto invalid_unit;
9384 : :
9385 : : /* Yes. Handling this is quite easy since the opcode value
9386 : : is computed with
9387 : :
9388 : : opcode = (desired line increment - line_base)
9389 : : + (line_range * address advance) + opcode_base
9390 : : */
9391 : 0 : int line_increment = (line_base
9392 : 0 : + (opcode - opcode_base) % line_range);
9393 : :
9394 : : /* Perform the increments. */
9395 : 0 : line += line_increment;
9396 : 0 : advance_pc ((opcode - opcode_base) / line_range);
9397 : :
9398 : 0 : printf (_(" special opcode %u: address+%u = "),
9399 : : opcode, op_addr_advance);
9400 : 0 : print_dwarf_addr (dwflmod, 0, address, address);
9401 [ # # ]: 0 : if (op_index > 0)
9402 : 0 : printf (_(", op_index = %u, line%+d = %zu\n"),
9403 : : op_index, line_increment, line);
9404 : : else
9405 : 0 : printf (_(", line%+d = %zu\n"),
9406 : : line_increment, line);
9407 : : }
9408 [ # # ]: 0 : else if (opcode == 0)
9409 : : {
9410 : : /* This an extended opcode. */
9411 [ # # ]: 0 : if (unlikely (linep + 2 > lineendp))
9412 : 0 : goto invalid_unit;
9413 : :
9414 : : /* The length. */
9415 : 0 : unsigned int len = *linep++;
9416 : :
9417 [ # # ]: 0 : if (unlikely (linep + len > lineendp))
9418 : 0 : goto invalid_unit;
9419 : :
9420 : : /* The sub-opcode. */
9421 : 0 : opcode = *linep++;
9422 : :
9423 : 0 : printf (_(" extended opcode %u: "), opcode);
9424 : :
9425 [ # # # # : 0 : switch (opcode)
# # # ]
9426 : : {
9427 : 0 : case DW_LNE_end_sequence:
9428 : 0 : puts (_(" end of sequence"));
9429 : :
9430 : : /* Reset the registers we care about. */
9431 : 0 : address = 0;
9432 : 0 : op_index = 0;
9433 : 0 : line = 1;
9434 : 0 : is_stmt = default_is_stmt;
9435 : 0 : break;
9436 : :
9437 : 0 : case DW_LNE_set_address:
9438 : 0 : op_index = 0;
9439 [ # # ]: 0 : if (unlikely ((size_t) (lineendp - linep) < address_size))
9440 : 0 : goto invalid_unit;
9441 [ # # ]: 0 : if (address_size == 4)
9442 [ # # ]: 0 : address = read_4ubyte_unaligned_inc (dbg, linep);
9443 : : else
9444 [ # # ]: 0 : address = read_8ubyte_unaligned_inc (dbg, linep);
9445 : : {
9446 : 0 : printf (_(" set address to "));
9447 : 0 : print_dwarf_addr (dwflmod, 0, address, address);
9448 : 0 : printf ("\n");
9449 : : }
9450 : : break;
9451 : :
9452 : 0 : case DW_LNE_define_file:
9453 : : {
9454 : 0 : char *fname = (char *) linep;
9455 : 0 : unsigned char *endp = memchr (linep, '\0',
9456 : 0 : lineendp - linep);
9457 [ # # ]: 0 : if (unlikely (endp == NULL))
9458 : 0 : goto invalid_unit;
9459 : 0 : linep = endp + 1;
9460 : :
9461 : 0 : unsigned int diridx;
9462 [ # # ]: 0 : if (lineendp - linep < 1)
9463 : 0 : goto invalid_unit;
9464 : 0 : get_uleb128 (diridx, linep, lineendp);
9465 : 0 : Dwarf_Word mtime;
9466 [ # # ]: 0 : if (lineendp - linep < 1)
9467 : 0 : goto invalid_unit;
9468 : 0 : get_uleb128 (mtime, linep, lineendp);
9469 : 0 : Dwarf_Word filelength;
9470 [ # # ]: 0 : if (lineendp - linep < 1)
9471 : 0 : goto invalid_unit;
9472 : 0 : get_uleb128 (filelength, linep, lineendp);
9473 : :
9474 : 0 : printf (_("\
9475 : : define new file: dir=%u, mtime=%" PRIu64 ", length=%" PRIu64 ", name=%s\n"),
9476 : : diridx, (uint64_t) mtime, (uint64_t) filelength,
9477 : : fname);
9478 : : }
9479 : : break;
9480 : :
9481 : 0 : case DW_LNE_set_discriminator:
9482 : : /* Takes one ULEB128 parameter, the discriminator. */
9483 [ # # # # ]: 0 : if (unlikely (standard_opcode_lengths[opcode] != 1
9484 : : || lineendp - linep < 1))
9485 : 0 : goto invalid_unit;
9486 : :
9487 : 0 : get_uleb128 (u128, linep, lineendp);
9488 : 0 : printf (_(" set discriminator to %u\n"), u128);
9489 : : break;
9490 : :
9491 : 0 : case DW_LNE_NVIDIA_inlined_call:
9492 : : {
9493 [ # # ]: 0 : if (unlikely (linep >= lineendp))
9494 : 0 : goto invalid_data;
9495 : :
9496 : 0 : unsigned int context;
9497 : 0 : get_uleb128 (context, linep, lineendp);
9498 : :
9499 [ # # ]: 0 : if (unlikely (linep >= lineendp))
9500 : 0 : goto invalid_data;
9501 : :
9502 : 0 : unsigned int function_name;
9503 : 0 : get_uleb128 (function_name, linep, lineendp);
9504 : 0 : function_name += debug_str_offset;
9505 : :
9506 : 0 : Elf_Data *str_data = dbg->sectiondata[IDX_debug_str];
9507 : 0 : char *function_str;
9508 [ # # # # ]: 0 : if (str_data == NULL || function_name >= str_data->d_size
9509 [ # # ]: 0 : || memchr (str_data->d_buf + function_name, '\0',
9510 : : str_data->d_size - function_name) == NULL)
9511 : : function_str = "???";
9512 : : else
9513 : 0 : function_str = (char *) str_data->d_buf + function_name;
9514 : :
9515 : 0 : printf (_(" set inlined context %u,"
9516 : : " function name %s (0x%x)\n"),
9517 : : context, function_str, function_name);
9518 : : break;
9519 : : }
9520 : :
9521 : 0 : case DW_LNE_NVIDIA_set_function_name:
9522 : : {
9523 [ # # ]: 0 : if (unlikely (linep >= lineendp))
9524 : 0 : goto invalid_data;
9525 : :
9526 : 0 : unsigned int function_name;
9527 : 0 : get_uleb128 (function_name, linep, lineendp);
9528 : 0 : function_name += debug_str_offset;
9529 : :
9530 : 0 : Elf_Data *str_data = dbg->sectiondata[IDX_debug_str];
9531 : 0 : char *function_str;
9532 [ # # # # ]: 0 : if (str_data == NULL || function_name >= str_data->d_size
9533 [ # # ]: 0 : || memchr (str_data->d_buf + function_name, '\0',
9534 : : str_data->d_size - function_name) == NULL)
9535 : : function_str = "???";
9536 : : else
9537 : 0 : function_str = (char *) str_data->d_buf + function_name;
9538 : :
9539 : 0 : printf (_(" set function name %s (0x%x)\n"),
9540 : : function_str, function_name);
9541 : : }
9542 : : break;
9543 : :
9544 : 0 : default:
9545 : : /* Unknown, ignore it. */
9546 : 0 : puts (_(" unknown opcode"));
9547 : 0 : linep += len - 1;
9548 : 0 : break;
9549 : : }
9550 : : }
9551 [ # # ]: 0 : else if (opcode <= DW_LNS_set_isa)
9552 : : {
9553 : : /* This is a known standard opcode. */
9554 [ # # # # : 0 : switch (opcode)
# # # # #
# # # ]
9555 : : {
9556 : 0 : case DW_LNS_copy:
9557 : : /* Takes no argument. */
9558 : 0 : puts (_(" copy"));
9559 : 0 : break;
9560 : :
9561 : 0 : case DW_LNS_advance_pc:
9562 : : /* Takes one uleb128 parameter which is added to the
9563 : : address. */
9564 [ # # ]: 0 : if (lineendp - linep < 1)
9565 : 0 : goto invalid_unit;
9566 : 0 : get_uleb128 (u128, linep, lineendp);
9567 : 0 : advance_pc (u128);
9568 : : {
9569 : 0 : printf (_(" advance address by %u to "),
9570 : : op_addr_advance);
9571 : 0 : print_dwarf_addr (dwflmod, 0, address, address);
9572 [ # # ]: 0 : if (op_index > 0)
9573 : 0 : printf (_(", op_index to %u"), op_index);
9574 : 0 : printf ("\n");
9575 : : }
9576 : : break;
9577 : :
9578 : 0 : case DW_LNS_advance_line:
9579 : : /* Takes one sleb128 parameter which is added to the
9580 : : line. */
9581 [ # # ]: 0 : if (lineendp - linep < 1)
9582 : 0 : goto invalid_unit;
9583 : 0 : get_sleb128 (s128, linep, lineendp);
9584 : 0 : line += s128;
9585 : 0 : printf (_("\
9586 : : advance line by constant %d to %" PRId64 "\n"),
9587 : : s128, (int64_t) line);
9588 : : break;
9589 : :
9590 : 0 : case DW_LNS_set_file:
9591 : : /* Takes one uleb128 parameter which is stored in file. */
9592 [ # # ]: 0 : if (lineendp - linep < 1)
9593 : 0 : goto invalid_unit;
9594 : 0 : get_uleb128 (u128, linep, lineendp);
9595 : 0 : printf (_(" set file to %" PRIu64 "\n"),
9596 : : (uint64_t) u128);
9597 : : break;
9598 : :
9599 : 0 : case DW_LNS_set_column:
9600 : : /* Takes one uleb128 parameter which is stored in column. */
9601 [ # # # # ]: 0 : if (unlikely (standard_opcode_lengths[opcode] != 1
9602 : : || lineendp - linep < 1))
9603 : 0 : goto invalid_unit;
9604 : :
9605 : 0 : get_uleb128 (u128, linep, lineendp);
9606 : 0 : printf (_(" set column to %" PRIu64 "\n"),
9607 : : (uint64_t) u128);
9608 : : break;
9609 : :
9610 : 0 : case DW_LNS_negate_stmt:
9611 : : /* Takes no argument. */
9612 : 0 : is_stmt = 1 - is_stmt;
9613 : 0 : printf (_(" set '%s' to %" PRIuFAST8 "\n"),
9614 : : "is_stmt", is_stmt);
9615 : : break;
9616 : :
9617 : 0 : case DW_LNS_set_basic_block:
9618 : : /* Takes no argument. */
9619 : 0 : puts (_(" set basic block flag"));
9620 : 0 : break;
9621 : :
9622 : 0 : case DW_LNS_const_add_pc:
9623 : : /* Takes no argument. */
9624 : :
9625 [ # # ]: 0 : if (unlikely (line_range == 0))
9626 : 0 : goto invalid_unit;
9627 : :
9628 : 0 : advance_pc ((255 - opcode_base) / line_range);
9629 : : {
9630 : 0 : printf (_(" advance address by constant %u to "),
9631 : : op_addr_advance);
9632 : 0 : print_dwarf_addr (dwflmod, 0, address, address);
9633 [ # # ]: 0 : if (op_index > 0)
9634 : 0 : printf (_(", op_index to %u"), op_index);
9635 : 0 : printf ("\n");
9636 : : }
9637 : : break;
9638 : :
9639 : 0 : case DW_LNS_fixed_advance_pc:
9640 : : /* Takes one 16 bit parameter which is added to the
9641 : : address. */
9642 [ # # # # ]: 0 : if (unlikely (standard_opcode_lengths[opcode] != 1
9643 : : || lineendp - linep < 2))
9644 : 0 : goto invalid_unit;
9645 : :
9646 [ # # ]: 0 : u128 = read_2ubyte_unaligned_inc (dbg, linep);
9647 : 0 : address += u128;
9648 : 0 : op_index = 0;
9649 : : {
9650 : 0 : printf (_("\
9651 : : advance address by fixed value %u to \n"),
9652 : : u128);
9653 : 0 : print_dwarf_addr (dwflmod, 0, address, address);
9654 : 0 : printf ("\n");
9655 : : }
9656 : : break;
9657 : :
9658 : 0 : case DW_LNS_set_prologue_end:
9659 : : /* Takes no argument. */
9660 : 0 : puts (_(" set prologue end flag"));
9661 : 0 : break;
9662 : :
9663 : 0 : case DW_LNS_set_epilogue_begin:
9664 : : /* Takes no argument. */
9665 : 0 : puts (_(" set epilogue begin flag"));
9666 : 0 : break;
9667 : :
9668 : 0 : case DW_LNS_set_isa:
9669 : : /* Takes one uleb128 parameter which is stored in isa. */
9670 [ # # # # ]: 0 : if (unlikely (standard_opcode_lengths[opcode] != 1
9671 : : || lineendp - linep < 1))
9672 : 0 : goto invalid_unit;
9673 : :
9674 : 0 : get_uleb128 (u128, linep, lineendp);
9675 [ # # ]: 0 : printf (_(" set isa to %u\n"), u128);
9676 : : break;
9677 : : }
9678 : : }
9679 : : else
9680 : : {
9681 : : /* This is a new opcode the generator but not we know about.
9682 : : Read the parameters associated with it but then discard
9683 : : everything. Read all the parameters for this opcode. */
9684 : 0 : printf (ngettext (" unknown opcode with %" PRIu8 " parameter:",
9685 : : " unknown opcode with %" PRIu8 " parameters:",
9686 : : standard_opcode_lengths[opcode]),
9687 : 0 : standard_opcode_lengths[opcode]);
9688 : 0 : for (int n = standard_opcode_lengths[opcode];
9689 [ # # # # ]: 0 : n > 0 && linep < lineendp; --n)
9690 : : {
9691 : 0 : get_uleb128 (u128, linep, lineendp);
9692 [ # # ]: 0 : if (n != standard_opcode_lengths[opcode])
9693 : 0 : fputc (',', stdout);
9694 : 0 : printf (" %u", u128);
9695 : : }
9696 : :
9697 : : /* Next round, ignore this opcode. */
9698 : 0 : continue;
9699 : : }
9700 : : }
9701 : : }
9702 : :
9703 : : /* There must only be one data block. */
9704 [ # # ]: 0 : assert (elf_getdata (scn, data) == NULL);
9705 : : }
9706 : :
9707 : :
9708 : : static void
9709 : 0 : print_debug_loclists_section (Dwfl_Module *dwflmod,
9710 : : Ebl *ebl,
9711 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
9712 : : Elf_Scn *scn, GElf_Shdr *shdr,
9713 : : Dwarf *dbg)
9714 : : {
9715 : 0 : Elf_Data *data = get_debug_elf_data (dbg, ebl, IDX_debug_loclists, scn);
9716 [ # # ]: 0 : if (data == NULL)
9717 : 0 : return;
9718 : :
9719 : 0 : printf (_("\
9720 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
9721 : : elf_ndxscn (scn), section_name (ebl, shdr),
9722 : 0 : (uint64_t) shdr->sh_offset);
9723 : :
9724 : : /* For the listptr to get the base address/CU. */
9725 : 0 : sort_listptr (&known_loclistsptr, "loclistsptr");
9726 : 0 : size_t listptr_idx = 0;
9727 : :
9728 : 0 : const unsigned char *readp = data->d_buf;
9729 : 0 : const unsigned char *const dataend = ((unsigned char *) data->d_buf
9730 : 0 : + data->d_size);
9731 [ # # ]: 0 : while (readp < dataend)
9732 : : {
9733 [ # # ]: 0 : if (unlikely (readp > dataend - 4))
9734 : : {
9735 : 0 : invalid_data:
9736 : 0 : error (0, 0, _("invalid data in section [%zu] '%s'"),
9737 : : elf_ndxscn (scn), section_name (ebl, shdr));
9738 : 0 : return;
9739 : : }
9740 : :
9741 : 0 : ptrdiff_t offset = readp - (unsigned char *) data->d_buf;
9742 : 0 : printf (_("Table at Offset 0x%" PRIx64 ":\n\n"),
9743 : : (uint64_t) offset);
9744 : :
9745 [ # # ]: 0 : uint64_t unit_length = read_4ubyte_unaligned_inc (dbg, readp);
9746 : 0 : unsigned int offset_size = 4;
9747 [ # # ]: 0 : if (unlikely (unit_length == 0xffffffff))
9748 : : {
9749 [ # # ]: 0 : if (unlikely (readp > dataend - 8))
9750 : 0 : goto invalid_data;
9751 : :
9752 [ # # ]: 0 : unit_length = read_8ubyte_unaligned_inc (dbg, readp);
9753 : 0 : offset_size = 8;
9754 : : }
9755 : 0 : printf (_(" Length: %8" PRIu64 "\n"), unit_length);
9756 : :
9757 : : /* We need at least 2-bytes + 1-byte + 1-byte + 4-bytes = 8
9758 : : bytes to complete the header. And this unit cannot go beyond
9759 : : the section data. */
9760 [ # # ]: 0 : if (readp > dataend - 8
9761 [ # # ]: 0 : || unit_length < 8
9762 [ # # ]: 0 : || unit_length > (uint64_t) (dataend - readp))
9763 : 0 : goto invalid_data;
9764 : :
9765 : 0 : const unsigned char *nexthdr = readp + unit_length;
9766 : :
9767 [ # # ]: 0 : uint16_t version = read_2ubyte_unaligned_inc (dbg, readp);
9768 : 0 : printf (_(" DWARF version: %8" PRIu16 "\n"), version);
9769 : :
9770 [ # # ]: 0 : if (version != 5)
9771 : : {
9772 : 0 : error (0, 0, _("Unknown version"));
9773 : 0 : goto next_table;
9774 : : }
9775 : :
9776 : 0 : uint8_t address_size = *readp++;
9777 : 0 : printf (_(" Address size: %8" PRIu64 "\n"),
9778 : : (uint64_t) address_size);
9779 : :
9780 [ # # ]: 0 : if (address_size != 4 && address_size != 8)
9781 : : {
9782 : 0 : error (0, 0, _("unsupported address size"));
9783 : 0 : goto next_table;
9784 : : }
9785 : :
9786 : 0 : uint8_t segment_size = *readp++;
9787 : 0 : printf (_(" Segment size: %8" PRIu64 "\n"),
9788 : : (uint64_t) segment_size);
9789 : :
9790 [ # # ]: 0 : if (segment_size != 0)
9791 : : {
9792 : 0 : error (0, 0, _("unsupported segment size"));
9793 : 0 : goto next_table;
9794 : : }
9795 : :
9796 [ # # ]: 0 : uint32_t offset_entry_count = read_4ubyte_unaligned_inc (dbg, readp);
9797 : 0 : printf (_(" Offset entries: %8" PRIu64 "\n"),
9798 : : (uint64_t) offset_entry_count);
9799 : :
9800 : : /* We need the CU that uses this unit to get the initial base address. */
9801 : 0 : Dwarf_Addr cu_base = 0;
9802 : 0 : struct Dwarf_CU *cu = NULL;
9803 [ # # ]: 0 : if (listptr_cu (&known_loclistsptr, &listptr_idx,
9804 : : (Dwarf_Off) offset,
9805 : 0 : (Dwarf_Off) (nexthdr - (unsigned char *) data->d_buf),
9806 : : &cu_base, &cu)
9807 [ # # ]: 0 : || split_dwarf_cu_base (dbg, &cu, &cu_base))
9808 : 0 : {
9809 : 0 : Dwarf_Die cudie;
9810 [ # # ]: 0 : if (dwarf_cu_die (cu, &cudie,
9811 : : NULL, NULL, NULL, NULL,
9812 : : NULL, NULL) == NULL)
9813 : 0 : printf (_(" Unknown CU base: "));
9814 : : else
9815 : 0 : printf (_(" CU [%6" PRIx64 "] base: "),
9816 : : dwarf_dieoffset (&cudie));
9817 : 0 : print_dwarf_addr (dwflmod, address_size, cu_base, cu_base);
9818 : 0 : printf ("\n");
9819 : : }
9820 : : else
9821 : 0 : printf (_(" Not associated with a CU.\n"));
9822 : :
9823 : 0 : printf ("\n");
9824 : :
9825 : 0 : const unsigned char *offset_array_start = readp;
9826 [ # # ]: 0 : if (offset_entry_count > 0)
9827 : : {
9828 : 0 : uint64_t max_entries = (unit_length - 8) / offset_size;
9829 [ # # ]: 0 : if (offset_entry_count > max_entries)
9830 : : {
9831 : 0 : error (0, 0,
9832 : 0 : _("too many offset entries for unit length"));
9833 : 0 : offset_entry_count = max_entries;
9834 : : }
9835 : :
9836 : 0 : printf (_(" Offsets starting at 0x%" PRIx64 ":\n"),
9837 : : (uint64_t) (offset_array_start
9838 : 0 : - (unsigned char *) data->d_buf));
9839 [ # # ]: 0 : for (uint32_t idx = 0; idx < offset_entry_count; idx++)
9840 : : {
9841 : 0 : printf (" [%6" PRIu32 "] ", idx);
9842 [ # # ]: 0 : if (offset_size == 4)
9843 : : {
9844 [ # # ]: 0 : uint32_t off = read_4ubyte_unaligned_inc (dbg, readp);
9845 : 0 : printf ("0x%" PRIx32 "\n", off);
9846 : : }
9847 : : else
9848 : : {
9849 [ # # ]: 0 : uint64_t off = read_8ubyte_unaligned_inc (dbg, readp);
9850 : 0 : printf ("0x%" PRIx64 "\n", off);
9851 : : }
9852 : : }
9853 : 0 : printf ("\n");
9854 : : }
9855 : :
9856 : 0 : Dwarf_Addr base = cu_base;
9857 : 0 : bool start_of_list = true;
9858 : 0 : while (readp < nexthdr)
9859 : : {
9860 : 0 : Dwarf_Off off = (Dwarf_Off) (readp - (unsigned char *) data->d_buf);
9861 [ # # ]: 0 : if (listptr_attr (&known_loclistsptr, listptr_idx, off,
9862 : : DW_AT_GNU_locviews))
9863 : 0 : {
9864 : 0 : Dwarf_Off next_off = next_listptr_offset (&known_loclistsptr,
9865 : : &listptr_idx, off);
9866 : 0 : const unsigned char *locp = readp;
9867 : 0 : const unsigned char *locendp;
9868 [ # # ]: 0 : if (next_off == 0
9869 [ # # ]: 0 : || next_off > (size_t) (nexthdr - ((const unsigned char *)
9870 : : data->d_buf)))
9871 : : locendp = nexthdr;
9872 : : else
9873 : 0 : locendp = (const unsigned char *) data->d_buf + next_off;
9874 : :
9875 : 0 : printf (" Offset: %" PRIx64 ", Index: %" PRIx64 "\n",
9876 : : (uint64_t) (readp - (unsigned char *) data->d_buf),
9877 : 0 : (uint64_t) (readp - offset_array_start));
9878 : :
9879 : 0 : while (locp < locendp)
9880 : : {
9881 : 0 : uint64_t v1, v2;
9882 : 0 : get_uleb128 (v1, locp, locendp);
9883 [ # # ]: 0 : if (locp >= locendp)
9884 : : {
9885 : 0 : printf (_(" <INVALID DATA>\n"));
9886 : : break;
9887 : : }
9888 : 0 : get_uleb128 (v2, locp, locendp);
9889 [ # # ]: 0 : printf (" view pair %" PRId64 ", %" PRId64 "\n", v1, v2);
9890 : : }
9891 : :
9892 : 0 : printf ("\n");
9893 : 0 : readp = (unsigned char *) locendp;
9894 : 0 : continue;
9895 : : }
9896 : :
9897 : 0 : uint8_t kind = *readp++;
9898 : 0 : uint64_t op1, op2, len;
9899 : :
9900 : : /* Skip padding. */
9901 [ # # ]: 0 : if (start_of_list && kind == DW_LLE_end_of_list)
9902 : 0 : continue;
9903 : :
9904 [ # # ]: 0 : if (start_of_list)
9905 : : {
9906 : 0 : base = cu_base;
9907 : 0 : printf (" Offset: %" PRIx64 ", Index: %" PRIx64 "\n",
9908 : 0 : (uint64_t) (readp - (unsigned char *) data->d_buf - 1),
9909 : 0 : (uint64_t) (readp - offset_array_start - 1));
9910 : 0 : start_of_list = false;
9911 : : }
9912 : :
9913 : 0 : printf (" %s", dwarf_loc_list_encoding_name (kind));
9914 [ # # # # : 0 : switch (kind)
# # # # #
# # ]
9915 : : {
9916 : 0 : case DW_LLE_end_of_list:
9917 : 0 : start_of_list = true;
9918 : 0 : printf ("\n\n");
9919 : : break;
9920 : :
9921 : 0 : case DW_LLE_base_addressx:
9922 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
9923 : : {
9924 : 0 : invalid_entry:
9925 : 0 : error (0, 0, _("invalid loclists data"));
9926 : 0 : goto next_table;
9927 : : }
9928 : 0 : get_uleb128 (op1, readp, nexthdr);
9929 : 0 : printf (" %" PRIx64 "\n", op1);
9930 [ # # ]: 0 : if (! print_unresolved_addresses)
9931 : : {
9932 : 0 : Dwarf_Addr addr;
9933 [ # # ]: 0 : if (get_indexed_addr (cu, op1, &addr) != 0)
9934 : 0 : printf (" ???\n");
9935 : : else
9936 : : {
9937 : 0 : printf (" ");
9938 : 0 : print_dwarf_addr (dwflmod, address_size, addr, addr);
9939 : 0 : printf ("\n");
9940 : : }
9941 : : }
9942 : : break;
9943 : :
9944 : 0 : case DW_LLE_startx_endx:
9945 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
9946 : 0 : goto invalid_entry;
9947 : 0 : get_uleb128 (op1, readp, nexthdr);
9948 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
9949 : 0 : goto invalid_entry;
9950 : 0 : get_uleb128 (op2, readp, nexthdr);
9951 : 0 : printf (" %" PRIx64 ", %" PRIx64 "\n", op1, op2);
9952 [ # # ]: 0 : if (! print_unresolved_addresses)
9953 : : {
9954 : 0 : Dwarf_Addr addr1;
9955 : 0 : Dwarf_Addr addr2;
9956 [ # # ]: 0 : if (get_indexed_addr (cu, op1, &addr1) != 0
9957 [ # # ]: 0 : || get_indexed_addr (cu, op2, &addr2) != 0)
9958 : : {
9959 : 0 : printf (" ???..\n");
9960 : 0 : printf (" ???\n");
9961 : : }
9962 : : else
9963 : : {
9964 : 0 : printf (" ");
9965 : 0 : print_dwarf_addr (dwflmod, address_size, addr1, addr1);
9966 : 0 : printf ("..\n ");
9967 : 0 : print_dwarf_addr (dwflmod, address_size,
9968 : : addr2 - 1, addr2);
9969 : 0 : printf ("\n");
9970 : : }
9971 : : }
9972 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
9973 : 0 : goto invalid_entry;
9974 : 0 : get_uleb128 (len, readp, nexthdr);
9975 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < len)
9976 : 0 : goto invalid_entry;
9977 : 0 : print_ops (dwflmod, dbg, 8, 8, version,
9978 : : address_size, offset_size, cu, len, readp);
9979 : 0 : readp += len;
9980 : 0 : break;
9981 : :
9982 : 0 : case DW_LLE_startx_length:
9983 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
9984 : 0 : goto invalid_entry;
9985 : 0 : get_uleb128 (op1, readp, nexthdr);
9986 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
9987 : 0 : goto invalid_entry;
9988 : 0 : get_uleb128 (op2, readp, nexthdr);
9989 : 0 : printf (" %" PRIx64 ", %" PRIx64 "\n", op1, op2);
9990 [ # # ]: 0 : if (! print_unresolved_addresses)
9991 : : {
9992 : 0 : Dwarf_Addr addr1;
9993 : 0 : Dwarf_Addr addr2;
9994 [ # # ]: 0 : if (get_indexed_addr (cu, op1, &addr1) != 0)
9995 : : {
9996 : 0 : printf (" ???..\n");
9997 : 0 : printf (" ???\n");
9998 : : }
9999 : : else
10000 : : {
10001 : 0 : addr2 = addr1 + op2;
10002 : 0 : printf (" ");
10003 : 0 : print_dwarf_addr (dwflmod, address_size, addr1, addr1);
10004 : 0 : printf ("..\n ");
10005 : 0 : print_dwarf_addr (dwflmod, address_size,
10006 : : addr2 - 1, addr2);
10007 : 0 : printf ("\n");
10008 : : }
10009 : : }
10010 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
10011 : 0 : goto invalid_entry;
10012 : 0 : get_uleb128 (len, readp, nexthdr);
10013 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < len)
10014 : 0 : goto invalid_entry;
10015 : 0 : print_ops (dwflmod, dbg, 8, 8, version,
10016 : : address_size, offset_size, cu, len, readp);
10017 : 0 : readp += len;
10018 : 0 : break;
10019 : :
10020 : 0 : case DW_LLE_offset_pair:
10021 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
10022 : 0 : goto invalid_entry;
10023 : 0 : get_uleb128 (op1, readp, nexthdr);
10024 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
10025 : 0 : goto invalid_entry;
10026 : 0 : get_uleb128 (op2, readp, nexthdr);
10027 : 0 : printf (" %" PRIx64 ", %" PRIx64 "\n", op1, op2);
10028 [ # # ]: 0 : if (! print_unresolved_addresses)
10029 : : {
10030 : 0 : op1 += base;
10031 : 0 : op2 += base;
10032 : 0 : printf (" ");
10033 : 0 : print_dwarf_addr (dwflmod, address_size, op1, op1);
10034 : 0 : printf ("..\n ");
10035 : 0 : print_dwarf_addr (dwflmod, address_size, op2 - 1, op2);
10036 : 0 : printf ("\n");
10037 : : }
10038 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
10039 : 0 : goto invalid_entry;
10040 : 0 : get_uleb128 (len, readp, nexthdr);
10041 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < len)
10042 : 0 : goto invalid_entry;
10043 : 0 : print_ops (dwflmod, dbg, 8, 8, version,
10044 : : address_size, offset_size, cu, len, readp);
10045 : 0 : readp += len;
10046 : 0 : break;
10047 : :
10048 : 0 : case DW_LLE_default_location:
10049 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
10050 : 0 : goto invalid_entry;
10051 : 0 : get_uleb128 (len, readp, nexthdr);
10052 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < len)
10053 : 0 : goto invalid_entry;
10054 : 0 : print_ops (dwflmod, dbg, 8, 8, version,
10055 : : address_size, offset_size, cu, len, readp);
10056 : 0 : readp += len;
10057 : 0 : break;
10058 : :
10059 : 0 : case DW_LLE_base_address:
10060 [ # # ]: 0 : if (address_size == 4)
10061 : : {
10062 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 4)
10063 : 0 : goto invalid_entry;
10064 [ # # ]: 0 : op1 = read_4ubyte_unaligned_inc (dbg, readp);
10065 : : }
10066 : : else
10067 : : {
10068 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 8)
10069 : 0 : goto invalid_entry;
10070 [ # # ]: 0 : op1 = read_8ubyte_unaligned_inc (dbg, readp);
10071 : : }
10072 : 0 : base = op1;
10073 : 0 : printf (" 0x%" PRIx64 "\n", base);
10074 [ # # ]: 0 : if (! print_unresolved_addresses)
10075 : : {
10076 : 0 : printf (" ");
10077 : 0 : print_dwarf_addr (dwflmod, address_size, base, base);
10078 : 0 : printf ("\n");
10079 : : }
10080 : : break;
10081 : :
10082 : 0 : case DW_LLE_start_end:
10083 [ # # ]: 0 : if (address_size == 4)
10084 : : {
10085 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 8)
10086 : 0 : goto invalid_entry;
10087 [ # # ]: 0 : op1 = read_4ubyte_unaligned_inc (dbg, readp);
10088 [ # # ]: 0 : op2 = read_4ubyte_unaligned_inc (dbg, readp);
10089 : : }
10090 : : else
10091 : : {
10092 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 16)
10093 : 0 : goto invalid_entry;
10094 [ # # ]: 0 : op1 = read_8ubyte_unaligned_inc (dbg, readp);
10095 [ # # ]: 0 : op2 = read_8ubyte_unaligned_inc (dbg, readp);
10096 : : }
10097 : 0 : printf (" 0x%" PRIx64 "..0x%" PRIx64 "\n", op1, op2);
10098 [ # # ]: 0 : if (! print_unresolved_addresses)
10099 : : {
10100 : 0 : printf (" ");
10101 : 0 : print_dwarf_addr (dwflmod, address_size, op1, op1);
10102 : 0 : printf ("..\n ");
10103 : 0 : print_dwarf_addr (dwflmod, address_size, op2 - 1, op2);
10104 : 0 : printf ("\n");
10105 : : }
10106 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
10107 : 0 : goto invalid_entry;
10108 : 0 : get_uleb128 (len, readp, nexthdr);
10109 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < len)
10110 : 0 : goto invalid_entry;
10111 : 0 : print_ops (dwflmod, dbg, 8, 8, version,
10112 : : address_size, offset_size, cu, len, readp);
10113 : 0 : readp += len;
10114 : 0 : break;
10115 : :
10116 : 0 : case DW_LLE_start_length:
10117 [ # # ]: 0 : if (address_size == 4)
10118 : : {
10119 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 4)
10120 : 0 : goto invalid_entry;
10121 [ # # ]: 0 : op1 = read_4ubyte_unaligned_inc (dbg, readp);
10122 : : }
10123 : : else
10124 : : {
10125 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 8)
10126 : 0 : goto invalid_entry;
10127 [ # # ]: 0 : op1 = read_8ubyte_unaligned_inc (dbg, readp);
10128 : : }
10129 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
10130 : 0 : goto invalid_entry;
10131 : 0 : get_uleb128 (op2, readp, nexthdr);
10132 : 0 : printf (" 0x%" PRIx64 ", %" PRIx64 "\n", op1, op2);
10133 [ # # ]: 0 : if (! print_unresolved_addresses)
10134 : : {
10135 : 0 : op2 = op1 + op2;
10136 : 0 : printf (" ");
10137 : 0 : print_dwarf_addr (dwflmod, address_size, op1, op1);
10138 : 0 : printf ("..\n ");
10139 : 0 : print_dwarf_addr (dwflmod, address_size, op2 - 1, op2);
10140 : 0 : printf ("\n");
10141 : : }
10142 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
10143 : 0 : goto invalid_entry;
10144 : 0 : get_uleb128 (len, readp, nexthdr);
10145 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < len)
10146 : 0 : goto invalid_entry;
10147 : 0 : print_ops (dwflmod, dbg, 8, 8, version,
10148 : : address_size, offset_size, cu, len, readp);
10149 : 0 : readp += len;
10150 : 0 : break;
10151 : :
10152 : 0 : case DW_LLE_GNU_view_pair:
10153 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
10154 : 0 : goto invalid_entry;
10155 : 0 : get_uleb128 (op1, readp, nexthdr);
10156 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
10157 : 0 : goto invalid_entry;
10158 : 0 : get_uleb128 (op2, readp, nexthdr);
10159 [ # # ]: 0 : printf (" %" PRIx64 ", %" PRIx64 "\n", op1, op2);
10160 : : break;
10161 : :
10162 : 0 : default:
10163 : 0 : goto invalid_entry;
10164 : : }
10165 : : }
10166 : :
10167 : 0 : next_table:
10168 [ # # ]: 0 : if (readp != nexthdr)
10169 : : {
10170 : 0 : size_t padding = nexthdr - readp;
10171 : 0 : printf (_(" %zu padding bytes\n\n"), padding);
10172 : 0 : readp = nexthdr;
10173 : : }
10174 : : }
10175 : : }
10176 : :
10177 : :
10178 : : static void
10179 : 0 : print_debug_loc_section (Dwfl_Module *dwflmod,
10180 : : Ebl *ebl, GElf_Ehdr *ehdr,
10181 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
10182 : : {
10183 : 0 : Elf_Data *data = get_debug_elf_data (dbg, ebl, IDX_debug_loc, scn);
10184 [ # # ]: 0 : if (data == NULL)
10185 : 0 : return;
10186 : :
10187 : 0 : printf (_("\
10188 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
10189 : : elf_ndxscn (scn), section_name (ebl, shdr),
10190 : 0 : (uint64_t) shdr->sh_offset);
10191 : :
10192 : 0 : sort_listptr (&known_locsptr, "loclistptr");
10193 : 0 : size_t listptr_idx = 0;
10194 : :
10195 [ # # ]: 0 : uint_fast8_t address_size = ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 4 : 8;
10196 : 0 : uint_fast8_t offset_size = 4;
10197 : :
10198 : 0 : bool first = true;
10199 : 0 : Dwarf_Addr base = 0;
10200 : 0 : unsigned char *readp = data->d_buf;
10201 : 0 : unsigned char *const endp = (unsigned char *) data->d_buf + data->d_size;
10202 : 0 : Dwarf_CU *last_cu = NULL;
10203 [ # # ]: 0 : while (readp < endp)
10204 : : {
10205 : 0 : ptrdiff_t offset = readp - (unsigned char *) data->d_buf;
10206 : 0 : Dwarf_CU *cu = last_cu;
10207 : 0 : unsigned int attr = 0;
10208 : :
10209 [ # # # # ]: 0 : if (first && skip_listptr_hole (&known_locsptr, &listptr_idx,
10210 : : &address_size, &offset_size, &base,
10211 : : &cu, offset, &readp, endp, &attr))
10212 : 0 : continue;
10213 : :
10214 [ # # ]: 0 : if (last_cu != cu)
10215 : : {
10216 : 0 : Dwarf_Die cudie;
10217 [ # # ]: 0 : if (dwarf_cu_die (cu, &cudie,
10218 : : NULL, NULL, NULL, NULL,
10219 : : NULL, NULL) == NULL)
10220 : 0 : printf (_("\n Unknown CU base: "));
10221 : : else
10222 : 0 : printf (_("\n CU [%6" PRIx64 "] base: "),
10223 : : dwarf_dieoffset (&cudie));
10224 : 0 : print_dwarf_addr (dwflmod, address_size, base, base);
10225 : 0 : printf ("\n");
10226 : : }
10227 : 0 : last_cu = cu;
10228 : :
10229 [ # # ]: 0 : if (attr == DW_AT_GNU_locviews)
10230 : 0 : {
10231 : 0 : Dwarf_Off next_off = next_listptr_offset (&known_locsptr,
10232 : : &listptr_idx, offset);
10233 : 0 : const unsigned char *locp = readp;
10234 : 0 : const unsigned char *locendp;
10235 [ # # ]: 0 : if (next_off == 0
10236 : 0 : || next_off > (size_t) (endp
10237 [ # # ]: 0 : - (const unsigned char *) data->d_buf))
10238 : 0 : locendp = endp;
10239 : : else
10240 : 0 : locendp = (const unsigned char *) data->d_buf + next_off;
10241 : :
10242 [ # # ]: 0 : while (locp < locendp)
10243 : : {
10244 : 0 : uint64_t v1, v2;
10245 : 0 : get_uleb128 (v1, locp, locendp);
10246 [ # # ]: 0 : if (locp >= locendp)
10247 : : {
10248 : 0 : printf (_(" [%6tx] <INVALID DATA>\n"), offset);
10249 : : break;
10250 : : }
10251 : 0 : get_uleb128 (v2, locp, locendp);
10252 [ # # ]: 0 : if (first) /* First view pair in a list. */
10253 : 0 : printf (" [%6tx] ", offset);
10254 : : else
10255 : 0 : printf (" ");
10256 : 0 : printf ("view pair %" PRId64 ", %" PRId64 "\n", v1, v2);
10257 : 0 : first = false;
10258 : : }
10259 : :
10260 : 0 : first = true;
10261 : 0 : readp = (unsigned char *) locendp;
10262 : 0 : continue;
10263 : : }
10264 : :
10265 : : /* GNU DebugFission encoded addresses as addrx. */
10266 : 0 : bool is_debugfission = ((cu != NULL
10267 [ # # ]: 0 : || split_dwarf_cu_base (dbg, &cu, &base))
10268 [ # # # # ]: 0 : && (cu->version < 5
10269 [ # # ]: 0 : && cu->unit_type == DW_UT_split_compile));
10270 [ # # ]: 0 : if (!is_debugfission
10271 [ # # ]: 0 : && unlikely (data->d_size - offset < (size_t) address_size * 2))
10272 : : {
10273 : 0 : invalid_data:
10274 : 0 : printf (_(" [%6tx] <INVALID DATA>\n"), offset);
10275 : 0 : break;
10276 : : }
10277 : :
10278 : 0 : Dwarf_Addr begin;
10279 : 0 : Dwarf_Addr end;
10280 : 0 : bool use_base = true;
10281 : 0 : if (is_debugfission)
10282 : : {
10283 : 0 : const unsigned char *locp = readp;
10284 : 0 : const unsigned char *locendp = readp + data->d_size;
10285 [ # # ]: 0 : if (locp >= locendp)
10286 : 0 : goto invalid_data;
10287 : :
10288 : 0 : Dwarf_Word idx;
10289 : 0 : unsigned char code = *locp++;
10290 [ # # # # : 0 : switch (code)
# ]
10291 : : {
10292 : 0 : case DW_LLE_GNU_end_of_list_entry:
10293 : 0 : begin = 0;
10294 : 0 : end = 0;
10295 : 0 : break;
10296 : :
10297 : 0 : case DW_LLE_GNU_base_address_selection_entry:
10298 [ # # ]: 0 : if (locp >= locendp)
10299 : 0 : goto invalid_data;
10300 : 0 : begin = (Dwarf_Addr) -1;
10301 : 0 : get_uleb128 (idx, locp, locendp);
10302 [ # # ]: 0 : if (get_indexed_addr (cu, idx, &end) != 0)
10303 : 0 : end = idx; /* ... */
10304 : : break;
10305 : :
10306 : 0 : case DW_LLE_GNU_start_end_entry:
10307 [ # # ]: 0 : if (locp >= locendp)
10308 : 0 : goto invalid_data;
10309 : 0 : get_uleb128 (idx, locp, locendp);
10310 [ # # ]: 0 : if (get_indexed_addr (cu, idx, &begin) != 0)
10311 : 0 : begin = idx; /* ... */
10312 [ # # ]: 0 : if (locp >= locendp)
10313 : 0 : goto invalid_data;
10314 : 0 : get_uleb128 (idx, locp, locendp);
10315 [ # # ]: 0 : if (get_indexed_addr (cu, idx, &end) != 0)
10316 : 0 : end = idx; /* ... */
10317 : : use_base = false;
10318 : : break;
10319 : :
10320 : 0 : case DW_LLE_GNU_start_length_entry:
10321 [ # # ]: 0 : if (locp >= locendp)
10322 : 0 : goto invalid_data;
10323 : 0 : get_uleb128 (idx, locp, locendp);
10324 [ # # ]: 0 : if (get_indexed_addr (cu, idx, &begin) != 0)
10325 : 0 : begin = idx; /* ... */
10326 [ # # ]: 0 : if (locendp - locp < 4)
10327 : 0 : goto invalid_data;
10328 [ # # ]: 0 : end = read_4ubyte_unaligned_inc (dbg, locp);
10329 : 0 : end += begin;
10330 : 0 : use_base = false;
10331 : 0 : break;
10332 : :
10333 : 0 : default:
10334 : 0 : goto invalid_data;
10335 : : }
10336 : :
10337 : 0 : readp = (unsigned char *) locp;
10338 : : }
10339 [ # # ]: 0 : else if (address_size == 8)
10340 : : {
10341 [ # # ]: 0 : begin = read_8ubyte_unaligned_inc (dbg, readp);
10342 [ # # ]: 0 : end = read_8ubyte_unaligned_inc (dbg, readp);
10343 : : }
10344 : : else
10345 : : {
10346 [ # # ]: 0 : begin = read_4ubyte_unaligned_inc (dbg, readp);
10347 [ # # ]: 0 : end = read_4ubyte_unaligned_inc (dbg, readp);
10348 [ # # ]: 0 : if (begin == (Dwarf_Addr) (uint32_t) -1)
10349 : 0 : begin = (Dwarf_Addr) -1l;
10350 : : }
10351 : :
10352 [ # # ]: 0 : if (begin == (Dwarf_Addr) -1l) /* Base address entry. */
10353 : : {
10354 [ # # ]: 0 : if (first)
10355 : 0 : printf (" [%6tx] ", offset);
10356 : : else
10357 : 0 : printf (" ");
10358 : 0 : puts (_("base address"));
10359 : 0 : printf (" ");
10360 : 0 : print_dwarf_addr (dwflmod, address_size, end, end);
10361 : 0 : printf ("\n");
10362 : 0 : base = end;
10363 : 0 : first = false;
10364 : : }
10365 [ # # # # ]: 0 : else if (begin == 0 && end == 0) /* End of list entry. */
10366 : : {
10367 [ # # ]: 0 : if (first)
10368 : 0 : printf (_(" [%6tx] empty list\n"), offset);
10369 : : first = true;
10370 : : }
10371 : : else
10372 : : {
10373 : : /* We have a location expression entry. */
10374 [ # # ]: 0 : uint_fast16_t len = read_2ubyte_unaligned_inc (dbg, readp);
10375 : :
10376 [ # # ]: 0 : if (first) /* First entry in a list. */
10377 : 0 : printf (" [%6tx] ", offset);
10378 : : else
10379 : 0 : printf (" ");
10380 : :
10381 : 0 : printf ("range %" PRIx64 ", %" PRIx64 "\n", begin, end);
10382 [ # # ]: 0 : if (! print_unresolved_addresses)
10383 : : {
10384 [ # # ]: 0 : Dwarf_Addr dab = use_base ? base + begin : begin;
10385 [ # # ]: 0 : Dwarf_Addr dae = use_base ? base + end : end;
10386 : 0 : printf (" ");
10387 : 0 : print_dwarf_addr (dwflmod, address_size, dab, dab);
10388 : 0 : printf ("..\n ");
10389 : 0 : print_dwarf_addr (dwflmod, address_size, dae - 1, dae);
10390 : 0 : printf ("\n");
10391 : : }
10392 : :
10393 [ # # ]: 0 : if (endp - readp <= (ptrdiff_t) len)
10394 : : {
10395 : 0 : fputs (_(" <INVALID DATA>\n"), stdout);
10396 : 0 : break;
10397 : : }
10398 : :
10399 [ # # ]: 0 : print_ops (dwflmod, dbg, 11, 11,
10400 : 0 : cu != NULL ? cu->version : 3,
10401 : : address_size, offset_size, cu, len, readp);
10402 : :
10403 : 0 : first = false;
10404 : 0 : readp += len;
10405 : : }
10406 : : }
10407 : : }
10408 : :
10409 : : struct mac_culist
10410 : : {
10411 : : Dwarf_Die die;
10412 : : Dwarf_Off offset;
10413 : : Dwarf_Files *files;
10414 : : struct mac_culist *next;
10415 : : };
10416 : :
10417 : :
10418 : : static int
10419 : 0 : mac_compare (const void *p1, const void *p2)
10420 : : {
10421 : 0 : struct mac_culist *m1 = (struct mac_culist *) p1;
10422 : 0 : struct mac_culist *m2 = (struct mac_culist *) p2;
10423 : :
10424 [ # # ]: 0 : if (m1->offset < m2->offset)
10425 : : return -1;
10426 [ # # ]: 0 : if (m1->offset > m2->offset)
10427 : 0 : return 1;
10428 : : return 0;
10429 : : }
10430 : :
10431 : :
10432 : : static void
10433 : 0 : print_debug_macinfo_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
10434 : : Ebl *ebl,
10435 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
10436 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
10437 : : {
10438 : 0 : Elf_Data *data = get_debug_elf_data (dbg, ebl, IDX_debug_macinfo, scn);
10439 [ # # ]: 0 : if (data == NULL)
10440 : 0 : return;
10441 : :
10442 : 0 : printf (_("\
10443 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
10444 : : elf_ndxscn (scn), section_name (ebl, shdr),
10445 : 0 : (uint64_t) shdr->sh_offset);
10446 : 0 : fputc ('\n', stdout);
10447 : :
10448 : : /* There is no function in libdw to iterate over the raw content of
10449 : : the section but it is easy enough to do. */
10450 : :
10451 : : /* Get the source file information for all CUs. */
10452 : 0 : Dwarf_Off offset;
10453 : 0 : Dwarf_Off ncu = 0;
10454 : 0 : size_t hsize;
10455 : 0 : struct mac_culist *culist = NULL;
10456 : 0 : size_t nculist = 0;
10457 [ # # ]: 0 : while (dwarf_nextcu (dbg, offset = ncu, &ncu, &hsize, NULL, NULL, NULL) == 0)
10458 : : {
10459 : 0 : Dwarf_Die cudie;
10460 [ # # ]: 0 : if (dwarf_offdie (dbg, offset + hsize, &cudie) == NULL)
10461 : 0 : continue;
10462 : :
10463 : 0 : Dwarf_Attribute attr;
10464 [ # # ]: 0 : if (dwarf_attr (&cudie, DW_AT_macro_info, &attr) == NULL)
10465 : 0 : continue;
10466 : :
10467 : 0 : Dwarf_Word macoff;
10468 [ # # ]: 0 : if (dwarf_formudata (&attr, &macoff) != 0)
10469 : 0 : continue;
10470 : :
10471 : 0 : struct mac_culist *newp = (struct mac_culist *) alloca (sizeof (*newp));
10472 : 0 : newp->die = cudie;
10473 : 0 : newp->offset = macoff;
10474 : 0 : newp->files = NULL;
10475 : 0 : newp->next = culist;
10476 : 0 : culist = newp;
10477 : 0 : ++nculist;
10478 : : }
10479 : :
10480 : : /* Convert the list into an array for easier consumption. */
10481 : 0 : struct mac_culist *cus = (struct mac_culist *) alloca ((nculist + 1)
10482 : : * sizeof (*cus));
10483 : : /* Add sentinel. */
10484 : 0 : cus[nculist].offset = data->d_size;
10485 : 0 : cus[nculist].files = (Dwarf_Files *) -1l;
10486 [ # # ]: 0 : if (nculist > 0)
10487 : : {
10488 [ # # ]: 0 : for (size_t cnt = nculist - 1; culist != NULL; --cnt)
10489 : : {
10490 [ # # ]: 0 : assert (cnt < nculist);
10491 : 0 : cus[cnt] = *culist;
10492 : 0 : culist = culist->next;
10493 : : }
10494 : :
10495 : : /* Sort the array according to the offset in the .debug_macinfo
10496 : : section. Note we keep the sentinel at the end. */
10497 : 0 : qsort (cus, nculist, sizeof (*cus), mac_compare);
10498 : : }
10499 : :
10500 : 0 : const unsigned char *readp = (const unsigned char *) data->d_buf;
10501 : 0 : const unsigned char *readendp = readp + data->d_size;
10502 : 0 : int level = 1;
10503 : :
10504 : 0 : while (readp < readendp)
10505 : : {
10506 : 0 : unsigned int opcode = *readp++;
10507 : 0 : unsigned int u128;
10508 : 0 : unsigned int u128_2;
10509 : 0 : const unsigned char *endp;
10510 : :
10511 [ # # # # ]: 0 : switch (opcode)
10512 : : {
10513 : 0 : case DW_MACINFO_define:
10514 : : case DW_MACINFO_undef:
10515 : : case DW_MACINFO_vendor_ext:
10516 : : /* For the first two opcodes the parameters are
10517 : : line, string
10518 : : For the latter
10519 : : number, string.
10520 : : We can treat these cases together. */
10521 : 0 : get_uleb128 (u128, readp, readendp);
10522 : :
10523 : 0 : endp = memchr (readp, '\0', readendp - readp);
10524 [ # # ]: 0 : if (unlikely (endp == NULL))
10525 : : {
10526 : 0 : printf (_("\
10527 : : %*s*** non-terminated string at end of section"),
10528 : : level, "");
10529 : 0 : return;
10530 : : }
10531 : :
10532 [ # # ]: 0 : if (opcode == DW_MACINFO_define)
10533 : 0 : printf ("%*s#define %s, line %u\n",
10534 : : level, "", (char *) readp, u128);
10535 [ # # ]: 0 : else if (opcode == DW_MACINFO_undef)
10536 : 0 : printf ("%*s#undef %s, line %u\n",
10537 : : level, "", (char *) readp, u128);
10538 : : else
10539 : 0 : printf (" #vendor-ext %s, number %u\n", (char *) readp, u128);
10540 : :
10541 : 0 : readp = endp + 1;
10542 : 0 : break;
10543 : :
10544 : 0 : case DW_MACINFO_start_file:
10545 : : /* The two parameters are line and file index, in this order. */
10546 : 0 : get_uleb128 (u128, readp, readendp);
10547 [ # # ]: 0 : if (readendp - readp < 1)
10548 : : {
10549 : 0 : printf (_("\
10550 : : %*s*** missing DW_MACINFO_start_file argument at end of section"),
10551 : : level, "");
10552 : 0 : return;
10553 : : }
10554 : 0 : get_uleb128 (u128_2, readp, readendp);
10555 : :
10556 : : /* Find the CU DIE for this file. */
10557 : 0 : size_t macoff = readp - (const unsigned char *) data->d_buf;
10558 : 0 : const char *fname = "???";
10559 [ # # # # ]: 0 : if (macoff >= cus[0].offset && cus[0].offset != data->d_size)
10560 : : {
10561 [ # # # # ]: 0 : while (macoff >= cus[1].offset && cus[1].offset != data->d_size)
10562 : 0 : ++cus;
10563 : :
10564 [ # # ]: 0 : if (cus[0].files == NULL
10565 [ # # ]: 0 : && dwarf_getsrcfiles (&cus[0].die, &cus[0].files, NULL) != 0)
10566 : 0 : cus[0].files = (Dwarf_Files *) -1l;
10567 : :
10568 [ # # ]: 0 : if (cus[0].files != (Dwarf_Files *) -1l)
10569 : 0 : fname = (dwarf_filesrc (cus[0].files, u128_2, NULL, NULL)
10570 [ # # ]: 0 : ?: "???");
10571 : : }
10572 : :
10573 : 0 : printf ("%*sstart_file %u, [%u] %s\n",
10574 : : level, "", u128, u128_2, fname);
10575 : 0 : ++level;
10576 : 0 : break;
10577 : :
10578 : 0 : case DW_MACINFO_end_file:
10579 : 0 : --level;
10580 : 0 : printf ("%*send_file\n", level, "");
10581 : : /* Nothing more to do. */
10582 : : break;
10583 : :
10584 : 0 : default:
10585 : : // XXX gcc seems to generate files with a trailing zero.
10586 [ # # # # ]: 0 : if (unlikely (opcode != 0 || readp != readendp))
10587 [ # # ]: 0 : printf ("%*s*** invalid opcode %u\n", level, "", opcode);
10588 : : break;
10589 : : }
10590 : : }
10591 : : }
10592 : :
10593 : :
10594 : : static void
10595 : 0 : print_debug_macro_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
10596 : : Ebl *ebl,
10597 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
10598 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
10599 : : {
10600 : 0 : Elf_Data *data = get_debug_elf_data (dbg, ebl, IDX_debug_macro, scn);
10601 [ # # ]: 0 : if (data == NULL)
10602 : 0 : return;
10603 : :
10604 : 0 : printf (_("\
10605 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
10606 : : elf_ndxscn (scn), section_name (ebl, shdr),
10607 : 0 : (uint64_t) shdr->sh_offset);
10608 : 0 : fputc ('\n', stdout);
10609 : :
10610 : : /* Get the source file information for all CUs. Uses same
10611 : : datastructure as macinfo. But uses offset field to directly
10612 : : match .debug_line offset. And just stored in a list. */
10613 : 0 : Dwarf_Off offset;
10614 : 0 : Dwarf_Off ncu = 0;
10615 : 0 : size_t hsize;
10616 : 0 : struct mac_culist *culist = NULL;
10617 [ # # ]: 0 : while (dwarf_nextcu (dbg, offset = ncu, &ncu, &hsize, NULL, NULL, NULL) == 0)
10618 : : {
10619 : 0 : Dwarf_Die cudie;
10620 [ # # ]: 0 : if (dwarf_offdie (dbg, offset + hsize, &cudie) == NULL)
10621 : 0 : continue;
10622 : :
10623 : 0 : Dwarf_Attribute attr;
10624 [ # # ]: 0 : if (dwarf_attr (&cudie, DW_AT_stmt_list, &attr) == NULL)
10625 : 0 : continue;
10626 : :
10627 : 0 : Dwarf_Word lineoff;
10628 [ # # ]: 0 : if (dwarf_formudata (&attr, &lineoff) != 0)
10629 : 0 : continue;
10630 : :
10631 : 0 : struct mac_culist *newp = (struct mac_culist *) alloca (sizeof (*newp));
10632 : 0 : newp->die = cudie;
10633 : 0 : newp->offset = lineoff;
10634 : 0 : newp->files = NULL;
10635 : 0 : newp->next = culist;
10636 : 0 : culist = newp;
10637 : : }
10638 : :
10639 : 0 : const unsigned char *readp = (const unsigned char *) data->d_buf;
10640 : 0 : const unsigned char *readendp = readp + data->d_size;
10641 : :
10642 [ # # ]: 0 : while (readp < readendp)
10643 : : {
10644 : 0 : printf (_(" Offset: 0x%" PRIx64 "\n"),
10645 : 0 : (uint64_t) (readp - (const unsigned char *) data->d_buf));
10646 : :
10647 : : // Header, 2 byte version, 1 byte flag, optional .debug_line offset,
10648 : : // optional vendor extension macro entry table.
10649 [ # # ]: 0 : if (readp + 2 > readendp)
10650 : : {
10651 : 0 : invalid_data:
10652 : 0 : error (0, 0, _("invalid data"));
10653 : 0 : return;
10654 : : }
10655 [ # # ]: 0 : const uint16_t vers = read_2ubyte_unaligned_inc (dbg, readp);
10656 : 0 : printf (_(" Version: %" PRIu16 "\n"), vers);
10657 : :
10658 : : // Version 4 is the GNU extension for DWARF4. DWARF5 will use version
10659 : : // 5 when it gets standardized.
10660 [ # # ]: 0 : if (vers != 4 && vers != 5)
10661 : : {
10662 : 0 : printf (_(" unknown version, cannot parse section\n"));
10663 : 0 : return;
10664 : : }
10665 : :
10666 [ # # ]: 0 : if (readp + 1 > readendp)
10667 : 0 : goto invalid_data;
10668 : 0 : const unsigned char flag = *readp++;
10669 : 0 : printf (_(" Flag: 0x%" PRIx8), flag);
10670 [ # # ]: 0 : if (flag != 0)
10671 : : {
10672 : 0 : printf (" (");
10673 [ # # ]: 0 : if ((flag & 0x01) != 0)
10674 : : {
10675 : 0 : printf ("offset_size");
10676 [ # # ]: 0 : if ((flag & 0xFE) != 0)
10677 : 0 : printf (", ");
10678 : : }
10679 [ # # ]: 0 : if ((flag & 0x02) != 0)
10680 : : {
10681 : 0 : printf ("debug_line_offset");
10682 [ # # ]: 0 : if ((flag & 0xFC) != 0)
10683 : 0 : printf (", ");
10684 : : }
10685 [ # # ]: 0 : if ((flag & 0x04) != 0)
10686 : : {
10687 : 0 : printf ("operands_table");
10688 [ # # ]: 0 : if ((flag & 0xF8) != 0)
10689 : 0 : printf (", ");
10690 : : }
10691 [ # # ]: 0 : if ((flag & 0xF8) != 0)
10692 : 0 : printf ("unknown");
10693 : 0 : printf (")");
10694 : : }
10695 : 0 : printf ("\n");
10696 : :
10697 [ # # ]: 0 : unsigned int offset_len = (flag & 0x01) ? 8 : 4;
10698 : 0 : printf (_(" Offset length: %" PRIu8 "\n"), offset_len);
10699 : 0 : Dwarf_Off line_offset = -1;
10700 [ # # ]: 0 : if (flag & 0x02)
10701 : : {
10702 [ # # ]: 0 : if (offset_len == 8)
10703 [ # # ]: 0 : line_offset = read_8ubyte_unaligned_inc (dbg, readp);
10704 : : else
10705 [ # # ]: 0 : line_offset = read_4ubyte_unaligned_inc (dbg, readp);
10706 : 0 : printf (_(" .debug_line offset: 0x%" PRIx64 "\n"),
10707 : : line_offset);
10708 : : }
10709 : :
10710 : 0 : struct mac_culist *cu = NULL;
10711 [ # # ]: 0 : if (line_offset != (Dwarf_Off) -1)
10712 : : {
10713 : : cu = culist;
10714 [ # # # # ]: 0 : while (cu != NULL && line_offset != cu->offset)
10715 : 0 : cu = cu->next;
10716 : : }
10717 : :
10718 [ # # ]: 0 : Dwarf_Off str_offsets_base = str_offsets_base_off (dbg, (cu != NULL
10719 : : ? cu->die.cu
10720 : : : NULL));
10721 : :
10722 : 0 : const unsigned char *vendor[DW_MACRO_hi_user - DW_MACRO_lo_user + 1];
10723 [ # # ]: 0 : memset (vendor, 0, sizeof vendor);
10724 [ # # ]: 0 : if (flag & 0x04)
10725 : : {
10726 : : // 1 byte length, for each item, 1 byte opcode, uleb128 number
10727 : : // of arguments, for each argument 1 byte form code.
10728 [ # # ]: 0 : if (readp + 1 > readendp)
10729 : 0 : goto invalid_data;
10730 : 0 : unsigned int tlen = *readp++;
10731 : 0 : printf (_(" extension opcode table, %" PRIu8 " items:\n"),
10732 : : tlen);
10733 [ # # ]: 0 : for (unsigned int i = 0; i < tlen; i++)
10734 : : {
10735 [ # # ]: 0 : if (readp + 1 > readendp)
10736 : 0 : goto invalid_data;
10737 : 0 : unsigned int opcode = *readp++;
10738 : 0 : printf (_(" [%" PRIx8 "]"), opcode);
10739 : 0 : if (opcode < DW_MACRO_lo_user
10740 [ # # ]: 0 : || opcode > DW_MACRO_hi_user)
10741 : 0 : goto invalid_data;
10742 : : // Record the start of description for this vendor opcode.
10743 : : // uleb128 nr args, 1 byte per arg form.
10744 : 0 : vendor[opcode - DW_MACRO_lo_user] = readp;
10745 [ # # ]: 0 : if (readp + 1 > readendp)
10746 : 0 : goto invalid_data;
10747 : 0 : unsigned int args = *readp++;
10748 [ # # ]: 0 : if (args > 0)
10749 : : {
10750 : 0 : printf (_(" %" PRIu8 " arguments:"), args);
10751 : 0 : while (args > 0)
10752 : : {
10753 : 0 : if (readp + 1 > readendp)
10754 : 0 : goto invalid_data;
10755 : 0 : unsigned int form = *readp++;
10756 : 0 : printf (" %s", dwarf_form_name (form));
10757 [ # # ]: 0 : if (! libdw_valid_user_form (form))
10758 : 0 : goto invalid_data;
10759 : 0 : args--;
10760 [ # # ]: 0 : if (args > 0)
10761 [ # # ]: 0 : putchar (',');
10762 : : }
10763 : : }
10764 : : else
10765 : 0 : printf (_(" no arguments."));
10766 : 0 : putchar ('\n');
10767 : : }
10768 : : }
10769 : 0 : putchar ('\n');
10770 : :
10771 : 0 : int level = 1;
10772 [ # # ]: 0 : if (readp + 1 > readendp)
10773 : 0 : goto invalid_data;
10774 : 0 : unsigned int opcode = *readp++;
10775 : 0 : while (opcode != 0)
10776 : : {
10777 : 0 : unsigned int u128;
10778 : 0 : unsigned int u128_2;
10779 : 0 : const unsigned char *endp;
10780 : 0 : uint64_t off;
10781 : :
10782 [ # # # # : 0 : switch (opcode)
# # # # #
# # # # ]
10783 : : {
10784 : 0 : case DW_MACRO_start_file:
10785 : 0 : get_uleb128 (u128, readp, readendp);
10786 [ # # ]: 0 : if (readp >= readendp)
10787 : 0 : goto invalid_data;
10788 : 0 : get_uleb128 (u128_2, readp, readendp);
10789 : :
10790 : : /* Find the CU DIE that matches this line offset. */
10791 : 0 : const char *fname = "???";
10792 [ # # ]: 0 : if (cu != NULL)
10793 : : {
10794 [ # # ]: 0 : if (cu->files == NULL
10795 [ # # ]: 0 : && dwarf_getsrcfiles (&cu->die, &cu->files,
10796 : : NULL) != 0)
10797 : 0 : cu->files = (Dwarf_Files *) -1l;
10798 : :
10799 [ # # ]: 0 : if (cu->files != (Dwarf_Files *) -1l)
10800 : 0 : fname = (dwarf_filesrc (cu->files, u128_2,
10801 [ # # ]: 0 : NULL, NULL) ?: "???");
10802 : : }
10803 : 0 : printf ("%*sstart_file %u, [%u] %s\n",
10804 : : level, "", u128, u128_2, fname);
10805 : 0 : ++level;
10806 : 0 : break;
10807 : :
10808 : 0 : case DW_MACRO_end_file:
10809 : 0 : --level;
10810 : 0 : printf ("%*send_file\n", level, "");
10811 : : break;
10812 : :
10813 : 0 : case DW_MACRO_define:
10814 : 0 : get_uleb128 (u128, readp, readendp);
10815 : 0 : endp = memchr (readp, '\0', readendp - readp);
10816 [ # # ]: 0 : if (endp == NULL)
10817 : 0 : goto invalid_data;
10818 : 0 : printf ("%*s#define %s, line %u\n",
10819 : : level, "", readp, u128);
10820 : 0 : readp = endp + 1;
10821 : 0 : break;
10822 : :
10823 : 0 : case DW_MACRO_undef:
10824 : 0 : get_uleb128 (u128, readp, readendp);
10825 : 0 : endp = memchr (readp, '\0', readendp - readp);
10826 [ # # ]: 0 : if (endp == NULL)
10827 : 0 : goto invalid_data;
10828 : 0 : printf ("%*s#undef %s, line %u\n",
10829 : : level, "", readp, u128);
10830 : 0 : readp = endp + 1;
10831 : 0 : break;
10832 : :
10833 : 0 : case DW_MACRO_define_strp:
10834 : 0 : get_uleb128 (u128, readp, readendp);
10835 [ # # ]: 0 : if (readp + offset_len > readendp)
10836 : 0 : goto invalid_data;
10837 [ # # ]: 0 : if (offset_len == 8)
10838 [ # # ]: 0 : off = read_8ubyte_unaligned_inc (dbg, readp);
10839 : : else
10840 [ # # ]: 0 : off = read_4ubyte_unaligned_inc (dbg, readp);
10841 : 0 : printf ("%*s#define %s, line %u (indirect)\n",
10842 : : level, "", dwarf_getstring (dbg, off, NULL), u128);
10843 : : break;
10844 : :
10845 : 0 : case DW_MACRO_undef_strp:
10846 : 0 : get_uleb128 (u128, readp, readendp);
10847 [ # # ]: 0 : if (readp + offset_len > readendp)
10848 : 0 : goto invalid_data;
10849 [ # # ]: 0 : if (offset_len == 8)
10850 [ # # ]: 0 : off = read_8ubyte_unaligned_inc (dbg, readp);
10851 : : else
10852 [ # # ]: 0 : off = read_4ubyte_unaligned_inc (dbg, readp);
10853 : 0 : printf ("%*s#undef %s, line %u (indirect)\n",
10854 : : level, "", dwarf_getstring (dbg, off, NULL), u128);
10855 : : break;
10856 : :
10857 : 0 : case DW_MACRO_import:
10858 [ # # ]: 0 : if (readp + offset_len > readendp)
10859 : 0 : goto invalid_data;
10860 [ # # ]: 0 : if (offset_len == 8)
10861 [ # # ]: 0 : off = read_8ubyte_unaligned_inc (dbg, readp);
10862 : : else
10863 [ # # ]: 0 : off = read_4ubyte_unaligned_inc (dbg, readp);
10864 : 0 : printf ("%*s#include offset 0x%" PRIx64 "\n",
10865 : : level, "", off);
10866 : : break;
10867 : :
10868 : 0 : case DW_MACRO_define_sup:
10869 : 0 : get_uleb128 (u128, readp, readendp);
10870 : 0 : printf ("%*s#define ", level, "");
10871 : 0 : readp = print_form_data (dbg, DW_FORM_strp_sup,
10872 : : readp, readendp, offset_len,
10873 : : str_offsets_base);
10874 : 0 : printf (", line %u (sup)\n", u128);
10875 : : break;
10876 : :
10877 : 0 : case DW_MACRO_undef_sup:
10878 : 0 : get_uleb128 (u128, readp, readendp);
10879 : 0 : printf ("%*s#undef ", level, "");
10880 : 0 : readp = print_form_data (dbg, DW_FORM_strp_sup,
10881 : : readp, readendp, offset_len,
10882 : : str_offsets_base);
10883 : 0 : printf (", line %u (sup)\n", u128);
10884 : : break;
10885 : :
10886 : 0 : case DW_MACRO_import_sup:
10887 [ # # ]: 0 : if (readp + offset_len > readendp)
10888 : 0 : goto invalid_data;
10889 [ # # ]: 0 : if (offset_len == 8)
10890 [ # # ]: 0 : off = read_8ubyte_unaligned_inc (dbg, readp);
10891 : : else
10892 [ # # ]: 0 : off = read_4ubyte_unaligned_inc (dbg, readp);
10893 : : // XXX Needs support for reading from supplementary object file.
10894 : 0 : printf ("%*s#include offset 0x%" PRIx64 " (sup)\n",
10895 : : level, "", off);
10896 : : break;
10897 : :
10898 : 0 : case DW_MACRO_define_strx:
10899 : 0 : get_uleb128 (u128, readp, readendp);
10900 : 0 : printf ("%*s#define ", level, "");
10901 : 0 : readp = print_form_data (dbg, DW_FORM_strx,
10902 : : readp, readendp, offset_len,
10903 : : str_offsets_base);
10904 : 0 : printf (", line %u (strx)\n", u128);
10905 : : break;
10906 : :
10907 : 0 : case DW_MACRO_undef_strx:
10908 : 0 : get_uleb128 (u128, readp, readendp);
10909 : 0 : printf ("%*s#undef ", level, "");
10910 : 0 : readp = print_form_data (dbg, DW_FORM_strx,
10911 : : readp, readendp, offset_len,
10912 : : str_offsets_base);
10913 : 0 : printf (", line %u (strx)\n", u128);
10914 : : break;
10915 : :
10916 : : default:
10917 : 0 : printf ("%*svendor opcode 0x%" PRIx8, level, "", opcode);
10918 [ # # ]: 0 : if (opcode < DW_MACRO_lo_user
10919 : : || opcode > DW_MACRO_lo_user
10920 [ # # ]: 0 : || vendor[opcode - DW_MACRO_lo_user] == NULL)
10921 : 0 : goto invalid_data;
10922 : :
10923 : 0 : const unsigned char *op_desc;
10924 : 0 : op_desc = vendor[opcode - DW_MACRO_lo_user];
10925 : :
10926 : : // Just skip the arguments, we cannot really interpret them,
10927 : : // but print as much as we can.
10928 : 0 : unsigned int args = *op_desc++;
10929 [ # # ]: 0 : while (args > 0 && readp < readendp)
10930 : : {
10931 : 0 : unsigned int form = *op_desc++;
10932 : 0 : readp = print_form_data (dbg, form, readp, readendp,
10933 : : offset_len, str_offsets_base);
10934 : 0 : args--;
10935 [ # # ]: 0 : if (args > 0)
10936 [ # # ]: 0 : printf (", ");
10937 : : }
10938 : 0 : putchar ('\n');
10939 : : }
10940 : :
10941 [ # # ]: 0 : if (readp + 1 > readendp)
10942 : 0 : goto invalid_data;
10943 : 0 : opcode = *readp++;
10944 [ # # ]: 0 : if (opcode == 0)
10945 [ # # ]: 0 : putchar ('\n');
10946 : : }
10947 : : }
10948 : : }
10949 : :
10950 : :
10951 : : /* Callback for printing global names. */
10952 : : static int
10953 : 0 : print_pubnames (Dwarf *dbg __attribute__ ((unused)), Dwarf_Global *global,
10954 : : void *arg)
10955 : : {
10956 : 0 : int *np = (int *) arg;
10957 : :
10958 : 0 : printf (_(" [%5d] DIE offset: %6" PRId64
10959 : : ", CU DIE offset: %6" PRId64 ", name: %s\n"),
10960 : 0 : (*np)++, global->die_offset, global->cu_offset, global->name);
10961 : :
10962 : 0 : return 0;
10963 : : }
10964 : :
10965 : :
10966 : : /* Print the known exported symbols in the DWARF section '.debug_pubnames'. */
10967 : : static void
10968 : 0 : print_debug_pubnames_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
10969 : : Ebl *ebl,
10970 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
10971 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
10972 : : {
10973 : : /* Check section actually exists. */
10974 [ # # ]: 0 : if (get_debug_elf_data (dbg, ebl, IDX_debug_pubnames, scn) == NULL)
10975 : 0 : return;
10976 : :
10977 : 0 : printf (_("\nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
10978 : : elf_ndxscn (scn), section_name (ebl, shdr),
10979 : 0 : (uint64_t) shdr->sh_offset);
10980 : :
10981 : 0 : int n = 0;
10982 : 0 : (void) dwarf_getpubnames (dbg, print_pubnames, &n, 0);
10983 : : }
10984 : :
10985 : : /* Print the content of the DWARF string section '.debug_str'
10986 : : or 'debug_line_str'. */
10987 : : static void
10988 : 0 : print_debug_str_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
10989 : : Ebl *ebl,
10990 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
10991 : : Elf_Scn *scn, GElf_Shdr *shdr,
10992 : : Dwarf *dbg __attribute__ ((unused)))
10993 : : {
10994 : 0 : const char *name = section_name (ebl, shdr);
10995 [ # # ]: 0 : int idx = ((name != NULL && strstr (name, "debug_line_str") != NULL)
10996 [ # # ]: 0 : ? IDX_debug_line_str : IDX_debug_str);
10997 : 0 : Elf_Data *data = get_debug_elf_data (dbg, ebl, idx, scn);
10998 [ # # ]: 0 : if (data == NULL)
10999 : : return;
11000 : :
11001 : 0 : const size_t sh_size = data->d_size;
11002 : :
11003 : : /* Compute floor(log16(shdr->sh_size)). */
11004 : 0 : GElf_Addr tmp = sh_size;
11005 : 0 : int digits = 1;
11006 [ # # ]: 0 : while (tmp >= 16)
11007 : : {
11008 : 0 : ++digits;
11009 : 0 : tmp >>= 4;
11010 : : }
11011 : 0 : digits = MAX (4, digits);
11012 : :
11013 : 0 : printf (_("\nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"
11014 : : " %*s String\n"),
11015 : : elf_ndxscn (scn),
11016 : 0 : section_name (ebl, shdr), (uint64_t) shdr->sh_offset,
11017 : : /* TRANS: the debugstr| prefix makes the string unique. */
11018 [ # # ]: 0 : digits + 2, sgettext ("debugstr|Offset"));
11019 : :
11020 : 0 : Dwarf_Off offset = 0;
11021 [ # # ]: 0 : while (offset < sh_size)
11022 : : {
11023 : 0 : size_t len;
11024 : 0 : const char *str = (const char *) data->d_buf + offset;
11025 : 0 : const char *endp = memchr (str, '\0', sh_size - offset);
11026 [ # # ]: 0 : if (unlikely (endp == NULL))
11027 : : {
11028 : 0 : printf (_(" *** error, missing string terminator\n"));
11029 : : break;
11030 : : }
11031 : :
11032 : 0 : printf (" [%*" PRIx64 "] \"%s\"\n", digits, (uint64_t) offset, str);
11033 : 0 : len = endp - str;
11034 : 0 : offset += len + 1;
11035 : : }
11036 : : }
11037 : :
11038 : : static void
11039 : 0 : print_debug_str_offsets_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
11040 : : Ebl *ebl,
11041 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
11042 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
11043 : : {
11044 : 0 : Elf_Data *data = get_debug_elf_data (dbg, ebl, IDX_debug_str_offsets, scn);
11045 [ # # ]: 0 : if (data == NULL)
11046 : : return;
11047 : :
11048 : 0 : printf (_("\
11049 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
11050 : : elf_ndxscn (scn), section_name (ebl, shdr),
11051 : 0 : (uint64_t) shdr->sh_offset);
11052 : :
11053 [ # # ]: 0 : if (shdr->sh_size == 0)
11054 : : return;
11055 : :
11056 : 0 : size_t idx = 0;
11057 : 0 : sort_listptr (&known_stroffbases, "str_offsets");
11058 : :
11059 : 0 : const unsigned char *start = (const unsigned char *) data->d_buf;
11060 : 0 : const unsigned char *readp = start;
11061 : 0 : const unsigned char *readendp = ((const unsigned char *) data->d_buf
11062 : 0 : + data->d_size);
11063 : :
11064 : 0 : while (readp < readendp)
11065 : : {
11066 : : /* Most string offset tables will have a header. For split
11067 : : dwarf unit GNU DebugFission didn't add one. But they were
11068 : : also only defined for split units (main or skeleton units
11069 : : didn't have indirect strings). So if we don't have a
11070 : : DW_AT_str_offsets_base at all and this is offset zero, then
11071 : : just start printing offsets immediately, if this is a .dwo
11072 : : section. */
11073 : 0 : Dwarf_Off off = (Dwarf_Off) (readp
11074 : 0 : - (const unsigned char *) data->d_buf);
11075 : :
11076 : 0 : printf ("Table at offset %" PRIx64 " ", off);
11077 : :
11078 [ # # ]: 0 : struct listptr *listptr = get_listptr (&known_stroffbases, idx++);
11079 : 0 : const unsigned char *next_unitp = readendp;
11080 : 0 : uint8_t offset_size;
11081 : 0 : bool has_header;
11082 [ # # ]: 0 : if (listptr == NULL)
11083 : : {
11084 : : /* This can happen for .dwo files. There is only an header
11085 : : in the case this is a version 5 split DWARF file. */
11086 : 0 : Dwarf_CU *cu;
11087 : 0 : uint8_t unit_type;
11088 [ # # ]: 0 : if (dwarf_get_units (dbg, NULL, &cu, NULL, &unit_type,
11089 : : NULL, NULL) != 0)
11090 : : {
11091 : 0 : error (0, 0, "Warning: Cannot find any DWARF unit.");
11092 : : /* Just guess some values. */
11093 : 0 : has_header = false;
11094 : 0 : offset_size = 4;
11095 : : }
11096 [ # # ]: 0 : else if (off == 0
11097 : 0 : && (unit_type == DW_UT_split_type
11098 [ # # ]: 0 : || unit_type == DW_UT_split_compile))
11099 : : {
11100 : 0 : has_header = cu->version > 4;
11101 : 0 : offset_size = cu->offset_size;
11102 : : }
11103 : : else
11104 : : {
11105 : 0 : error (0, 0,
11106 : : "Warning: No CU references .debug_str_offsets after %"
11107 : : PRIx64, off);
11108 : 0 : has_header = cu->version > 4;
11109 : 0 : offset_size = cu->offset_size;
11110 : : }
11111 : 0 : printf ("\n");
11112 : : }
11113 : : else
11114 : : {
11115 : : /* This must be DWARF5, since GNU DebugFission didn't define
11116 : : DW_AT_str_offsets_base. */
11117 : 0 : has_header = true;
11118 : :
11119 : 0 : Dwarf_Die cudie;
11120 [ # # ]: 0 : if (dwarf_cu_die (listptr->cu, &cudie,
11121 : : NULL, NULL, NULL, NULL,
11122 : : NULL, NULL) == NULL)
11123 : 0 : printf ("Unknown CU (%s):\n", dwarf_errmsg (-1));
11124 : : else
11125 : 0 : printf ("for CU [%6" PRIx64 "]:\n", dwarf_dieoffset (&cudie));
11126 : : }
11127 : :
11128 [ # # ]: 0 : if (has_header)
11129 : : {
11130 : 0 : uint64_t unit_length;
11131 : 0 : uint16_t version;
11132 : 0 : uint16_t padding;
11133 : :
11134 [ # # ]: 0 : unit_length = read_4ubyte_unaligned_inc (dbg, readp);
11135 [ # # ]: 0 : if (unlikely (unit_length == 0xffffffff))
11136 : : {
11137 [ # # ]: 0 : if (unlikely (readp > readendp - 8))
11138 : : {
11139 : 0 : invalid_data:
11140 : 0 : error (0, 0, "Invalid data");
11141 : 0 : return;
11142 : : }
11143 [ # # ]: 0 : unit_length = read_8ubyte_unaligned_inc (dbg, readp);
11144 : 0 : offset_size = 8;
11145 : : }
11146 : : else
11147 : : offset_size = 4;
11148 : :
11149 : 0 : printf ("\n");
11150 : 0 : printf (_(" Length: %8" PRIu64 "\n"),
11151 : : unit_length);
11152 : 0 : printf (_(" Offset size: %8" PRIu8 "\n"),
11153 : : offset_size);
11154 : :
11155 : : /* We need at least 2-bytes (version) + 2-bytes (padding) =
11156 : : 4 bytes to complete the header. And this unit cannot go
11157 : : beyond the section data. */
11158 [ # # ]: 0 : if (readp > readendp - 4
11159 [ # # ]: 0 : || unit_length < 4
11160 [ # # ]: 0 : || unit_length > (uint64_t) (readendp - readp))
11161 : 0 : goto invalid_data;
11162 : :
11163 : 0 : next_unitp = readp + unit_length;
11164 : :
11165 [ # # ]: 0 : version = read_2ubyte_unaligned_inc (dbg, readp);
11166 : 0 : printf (_(" DWARF version: %8" PRIu16 "\n"), version);
11167 : :
11168 [ # # ]: 0 : if (version != 5)
11169 : : {
11170 : 0 : error (0, 0, _("Unknown version"));
11171 : 0 : goto next_unit;
11172 : : }
11173 : :
11174 [ # # ]: 0 : padding = read_2ubyte_unaligned_inc (dbg, readp);
11175 : 0 : printf (_(" Padding: %8" PRIx16 "\n"), padding);
11176 : :
11177 [ # # ]: 0 : if (listptr != NULL
11178 [ # # ]: 0 : && listptr->offset != (Dwarf_Off) (readp - start))
11179 : : {
11180 : 0 : error (0, 0, "String offsets index doesn't start after header");
11181 : 0 : goto next_unit;
11182 : : }
11183 : :
11184 : 0 : printf ("\n");
11185 : : }
11186 : :
11187 : 0 : int digits = 1;
11188 : 0 : size_t offsets = (next_unitp - readp) / offset_size;
11189 [ # # ]: 0 : while (offsets >= 10)
11190 : : {
11191 : 0 : ++digits;
11192 : 0 : offsets /= 10;
11193 : : }
11194 : :
11195 : 0 : unsigned int uidx = 0;
11196 : 0 : size_t index_offset = readp - (const unsigned char *) data->d_buf;
11197 : 0 : printf (" Offsets start at 0x%zx:\n", index_offset);
11198 : 0 : while (readp <= next_unitp - offset_size)
11199 : : {
11200 : 0 : Dwarf_Word offset;
11201 [ # # ]: 0 : if (offset_size == 4)
11202 [ # # ]: 0 : offset = read_4ubyte_unaligned_inc (dbg, readp);
11203 : : else
11204 [ # # ]: 0 : offset = read_8ubyte_unaligned_inc (dbg, readp);
11205 : 0 : const char *str = dwarf_getstring (dbg, offset, NULL);
11206 [ # # # # ]: 0 : printf (" [%*u] [%*" PRIx64 "] \"%s\"\n",
11207 : 0 : digits, uidx++, (int) offset_size * 2, offset, str ?: "???");
11208 : : }
11209 : 0 : printf ("\n");
11210 : :
11211 [ # # ]: 0 : if (readp != next_unitp)
11212 [ # # ]: 0 : error (0, 0, "extra %zd bytes at end of unit",
11213 : 0 : (size_t) (next_unitp - readp));
11214 : :
11215 : 0 : next_unit:
11216 : : readp = next_unitp;
11217 : : }
11218 : : }
11219 : :
11220 : :
11221 : : /* Print the content of the call frame search table section
11222 : : '.eh_frame_hdr'. */
11223 : : static void
11224 : 0 : print_debug_frame_hdr_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
11225 : : Ebl *ebl __attribute__ ((unused)),
11226 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
11227 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
11228 : : {
11229 : 0 : printf (_("\
11230 : : \nCall frame search table section [%2zu] '.eh_frame_hdr':\n"),
11231 : : elf_ndxscn (scn));
11232 : :
11233 : 0 : Elf_Data *data = elf_rawdata (scn, NULL);
11234 : :
11235 [ # # ]: 0 : if (unlikely (data == NULL))
11236 : : {
11237 : 0 : error (0, 0, _("cannot get %s content: %s"),
11238 : : ".eh_frame_hdr", elf_errmsg (-1));
11239 : 0 : return;
11240 : : }
11241 : :
11242 : 0 : const unsigned char *readp = data->d_buf;
11243 : 0 : const unsigned char *const dataend = ((unsigned char *) data->d_buf
11244 : 0 : + data->d_size);
11245 : :
11246 [ # # ]: 0 : if (unlikely (readp + 4 > dataend))
11247 : : {
11248 : 0 : invalid_data:
11249 : 0 : error (0, 0, _("invalid data"));
11250 : 0 : return;
11251 : : }
11252 : :
11253 : 0 : unsigned int version = *readp++;
11254 : 0 : unsigned int eh_frame_ptr_enc = *readp++;
11255 : 0 : unsigned int fde_count_enc = *readp++;
11256 : 0 : unsigned int table_enc = *readp++;
11257 : :
11258 : 0 : printf (" version: %u\n"
11259 : : " eh_frame_ptr_enc: %#x ",
11260 : : version, eh_frame_ptr_enc);
11261 : 0 : print_encoding_base ("", eh_frame_ptr_enc);
11262 : 0 : printf (" fde_count_enc: %#x ", fde_count_enc);
11263 : 0 : print_encoding_base ("", fde_count_enc);
11264 : 0 : printf (" table_enc: %#x ", table_enc);
11265 : 0 : print_encoding_base ("", table_enc);
11266 : :
11267 : 0 : uint64_t eh_frame_ptr = 0;
11268 [ # # ]: 0 : if (eh_frame_ptr_enc != DW_EH_PE_omit)
11269 : : {
11270 : 0 : readp = read_encoded (eh_frame_ptr_enc, readp, dataend, &eh_frame_ptr,
11271 : : dbg);
11272 [ # # ]: 0 : if (unlikely (readp == NULL))
11273 : 0 : goto invalid_data;
11274 : :
11275 : 0 : printf (" eh_frame_ptr: %#" PRIx64, eh_frame_ptr);
11276 [ # # ]: 0 : if ((eh_frame_ptr_enc & 0x70) == DW_EH_PE_pcrel)
11277 : 0 : printf (" (offset: %#" PRIx64 ")",
11278 : : /* +4 because of the 4 byte header of the section. */
11279 : 0 : (uint64_t) shdr->sh_offset + 4 + eh_frame_ptr);
11280 : :
11281 : 0 : putchar ('\n');
11282 : : }
11283 : :
11284 : 0 : uint64_t fde_count = 0;
11285 [ # # ]: 0 : if (fde_count_enc != DW_EH_PE_omit)
11286 : : {
11287 : 0 : readp = read_encoded (fde_count_enc, readp, dataend, &fde_count, dbg);
11288 [ # # ]: 0 : if (unlikely (readp == NULL))
11289 : 0 : goto invalid_data;
11290 : :
11291 : 0 : printf (" fde_count: %" PRIu64 "\n", fde_count);
11292 : : }
11293 : :
11294 [ # # # # ]: 0 : if (fde_count == 0 || table_enc == DW_EH_PE_omit)
11295 : : return;
11296 : :
11297 : 0 : puts (" Table:");
11298 : :
11299 : : /* Optimize for the most common case. */
11300 [ # # ]: 0 : if (table_enc == (DW_EH_PE_datarel | DW_EH_PE_sdata4))
11301 : 0 : while (fde_count > 0 && readp + 8 <= dataend)
11302 : : {
11303 [ # # ]: 0 : int32_t initial_location = read_4sbyte_unaligned_inc (dbg, readp);
11304 : 0 : uint64_t initial_offset = ((uint64_t) shdr->sh_offset
11305 : 0 : + (int64_t) initial_location);
11306 [ # # ]: 0 : int32_t address = read_4sbyte_unaligned_inc (dbg, readp);
11307 : : // XXX Possibly print symbol name or section offset for initial_offset
11308 [ # # ]: 0 : printf (" %#" PRIx32 " (offset: %#6" PRIx64 ") -> %#" PRIx32
11309 : : " fde=[%6" PRIx64 "]\n",
11310 : : initial_location, initial_offset,
11311 : 0 : address, address - (eh_frame_ptr + 4));
11312 : : }
11313 : : else
11314 : 0 : while (0 && readp < dataend)
11315 : : {
11316 : :
11317 : 0 : }
11318 : : }
11319 : :
11320 : :
11321 : : /* Print the content of the exception handling table section
11322 : : '.eh_frame_hdr'. */
11323 : : static void
11324 : 0 : print_debug_exception_table (Dwfl_Module *dwflmod __attribute__ ((unused)),
11325 : : Ebl *ebl __attribute__ ((unused)),
11326 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
11327 : : Elf_Scn *scn,
11328 : : GElf_Shdr *shdr __attribute__ ((unused)),
11329 : : Dwarf *dbg __attribute__ ((unused)))
11330 : : {
11331 : 0 : printf (_("\
11332 : : \nException handling table section [%2zu] '.gcc_except_table':\n"),
11333 : : elf_ndxscn (scn));
11334 : :
11335 : 0 : Elf_Data *data = elf_rawdata (scn, NULL);
11336 : :
11337 [ # # ]: 0 : if (unlikely (data == NULL))
11338 : : {
11339 : 0 : error (0, 0, _("cannot get %s content: %s"),
11340 : : ".gcc_except_table", elf_errmsg (-1));
11341 : 0 : return;
11342 : : }
11343 : :
11344 : 0 : const unsigned char *readp = data->d_buf;
11345 : 0 : const unsigned char *const dataend = readp + data->d_size;
11346 : :
11347 [ # # ]: 0 : if (unlikely (readp + 1 > dataend))
11348 : : {
11349 : 0 : invalid_data:
11350 : 0 : error (0, 0, _("invalid data"));
11351 : 0 : return;
11352 : : }
11353 : 0 : unsigned int lpstart_encoding = *readp++;
11354 : 0 : printf (_(" LPStart encoding: %#x "), lpstart_encoding);
11355 : 0 : print_encoding_base ("", lpstart_encoding);
11356 [ # # ]: 0 : if (lpstart_encoding != DW_EH_PE_omit)
11357 : : {
11358 : 0 : uint64_t lpstart;
11359 : 0 : readp = read_encoded (lpstart_encoding, readp, dataend, &lpstart, dbg);
11360 : 0 : printf (" LPStart: %#" PRIx64 "\n", lpstart);
11361 : : }
11362 : :
11363 [ # # ]: 0 : if (unlikely (readp + 1 > dataend))
11364 : 0 : goto invalid_data;
11365 : 0 : unsigned int ttype_encoding = *readp++;
11366 : 0 : printf (_(" TType encoding: %#x "), ttype_encoding);
11367 : 0 : print_encoding_base ("", ttype_encoding);
11368 : 0 : const unsigned char *ttype_base = NULL;
11369 [ # # ]: 0 : if (ttype_encoding != DW_EH_PE_omit)
11370 : : {
11371 : 0 : unsigned int ttype_base_offset;
11372 [ # # ]: 0 : if (readp >= dataend)
11373 : 0 : goto invalid_data;
11374 : 0 : get_uleb128 (ttype_base_offset, readp, dataend);
11375 : 0 : printf (" TType base offset: %#x\n", ttype_base_offset);
11376 [ # # ]: 0 : if ((size_t) (dataend - readp) > ttype_base_offset)
11377 : 0 : ttype_base = readp + ttype_base_offset;
11378 : : }
11379 : :
11380 [ # # ]: 0 : if (unlikely (readp + 1 > dataend))
11381 : 0 : goto invalid_data;
11382 : 0 : unsigned int call_site_encoding = *readp++;
11383 : 0 : printf (_(" Call site encoding: %#x "), call_site_encoding);
11384 : 0 : print_encoding_base ("", call_site_encoding);
11385 : 0 : unsigned int call_site_table_len;
11386 [ # # ]: 0 : if (readp >= dataend)
11387 : 0 : goto invalid_data;
11388 : 0 : get_uleb128 (call_site_table_len, readp, dataend);
11389 : :
11390 : 0 : const unsigned char *const action_table = readp + call_site_table_len;
11391 [ # # ]: 0 : if (unlikely (action_table > dataend))
11392 : 0 : goto invalid_data;
11393 : : unsigned int u = 0;
11394 : : unsigned int max_action = 0;
11395 [ # # ]: 0 : while (readp < action_table)
11396 : : {
11397 [ # # ]: 0 : if (u == 0)
11398 : 0 : puts (_("\n Call site table:"));
11399 : :
11400 : 0 : uint64_t call_site_start;
11401 : 0 : readp = read_encoded (call_site_encoding, readp, dataend,
11402 : : &call_site_start, dbg);
11403 : 0 : uint64_t call_site_length;
11404 : 0 : readp = read_encoded (call_site_encoding, readp, dataend,
11405 : : &call_site_length, dbg);
11406 : 0 : uint64_t landing_pad;
11407 : 0 : readp = read_encoded (call_site_encoding, readp, dataend,
11408 : : &landing_pad, dbg);
11409 : 0 : unsigned int action;
11410 [ # # ]: 0 : if (readp >= dataend)
11411 : 0 : goto invalid_data;
11412 : 0 : get_uleb128 (action, readp, dataend);
11413 : 0 : max_action = MAX (action, max_action);
11414 : 0 : printf (_(" [%4u] Call site start: %#" PRIx64 "\n"
11415 : : " Call site length: %" PRIu64 "\n"
11416 : : " Landing pad: %#" PRIx64 "\n"
11417 : : " Action: %u\n"),
11418 : : u++, call_site_start, call_site_length, landing_pad, action);
11419 : : }
11420 [ # # ]: 0 : if (readp != action_table)
11421 : 0 : goto invalid_data;
11422 : :
11423 : 0 : unsigned int max_ar_filter = 0;
11424 [ # # ]: 0 : if (max_action > 0)
11425 : : {
11426 : 0 : puts ("\n Action table:");
11427 : :
11428 : 0 : size_t maxdata = (size_t) (dataend - action_table);
11429 [ # # # # ]: 0 : if (max_action > maxdata || maxdata - max_action < 1)
11430 : : {
11431 : 0 : invalid_action_table:
11432 : 0 : fputs (_(" <INVALID DATA>\n"), stdout);
11433 : 0 : return;
11434 : : }
11435 : :
11436 : 0 : const unsigned char *const action_table_end
11437 : 0 : = action_table + max_action + 1;
11438 : :
11439 : 0 : u = 0;
11440 : 0 : do
11441 : : {
11442 : 0 : int ar_filter;
11443 : 0 : get_sleb128 (ar_filter, readp, action_table_end);
11444 [ # # ]: 0 : if (ar_filter > 0 && (unsigned int) ar_filter > max_ar_filter)
11445 : 0 : max_ar_filter = ar_filter;
11446 : 0 : int ar_disp;
11447 [ # # ]: 0 : if (readp >= action_table_end)
11448 : 0 : goto invalid_action_table;
11449 : 0 : get_sleb128 (ar_disp, readp, action_table_end);
11450 : :
11451 : 0 : printf (" [%4u] ar_filter: % d\n"
11452 : : " ar_disp: % -5d",
11453 : : u, ar_filter, ar_disp);
11454 [ # # ]: 0 : if (abs (ar_disp) & 1)
11455 : 0 : printf (" -> [%4u]\n", u + (ar_disp + 1) / 2);
11456 [ # # ]: 0 : else if (ar_disp != 0)
11457 : 0 : puts (" -> ???");
11458 : : else
11459 : 0 : putchar ('\n');
11460 : 0 : ++u;
11461 : : }
11462 [ # # ]: 0 : while (readp < action_table_end);
11463 : : }
11464 : :
11465 [ # # ]: 0 : if (max_ar_filter > 0 && ttype_base != NULL)
11466 : : {
11467 : 0 : unsigned char dsize;
11468 : 0 : puts ("\n TType table:");
11469 : :
11470 : : // XXX Not *4, size of encoding;
11471 [ # # ]: 0 : switch (ttype_encoding & 7)
11472 : : {
11473 : : case DW_EH_PE_udata2:
11474 : : case DW_EH_PE_sdata2:
11475 : : dsize = 2;
11476 : : break;
11477 : : case DW_EH_PE_udata4:
11478 : : case DW_EH_PE_sdata4:
11479 : : dsize = 4;
11480 : : break;
11481 : : case DW_EH_PE_udata8:
11482 : : case DW_EH_PE_sdata8:
11483 : : dsize = 8;
11484 : : break;
11485 : 0 : default:
11486 : 0 : dsize = 0;
11487 : 0 : error (1, 0, _("invalid TType encoding"));
11488 : : }
11489 : :
11490 : 0 : if (max_ar_filter
11491 [ # # ]: 0 : > (size_t) (ttype_base - (const unsigned char *) data->d_buf) / dsize)
11492 : 0 : goto invalid_data;
11493 : :
11494 : 0 : readp = ttype_base - max_ar_filter * dsize;
11495 : 0 : do
11496 : : {
11497 : 0 : uint64_t ttype;
11498 : 0 : readp = read_encoded (ttype_encoding, readp, ttype_base, &ttype,
11499 : : dbg);
11500 : 0 : printf (" [%4u] %#" PRIx64 "\n", max_ar_filter--, ttype);
11501 : : }
11502 [ # # ]: 0 : while (readp < ttype_base);
11503 : : }
11504 : : }
11505 : :
11506 : : /* Print the content of the '.gdb_index' section.
11507 : : http://sourceware.org/gdb/current/onlinedocs/gdb/Index-Section-Format.html
11508 : : */
11509 : : static void
11510 : 0 : print_gdb_index_section (Dwfl_Module *dwflmod, Ebl *ebl,
11511 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
11512 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
11513 : : {
11514 : 0 : printf (_("\nGDB section [%2zu] '%s' at offset %#" PRIx64
11515 : : " contains %" PRId64 " bytes :\n"),
11516 : : elf_ndxscn (scn), section_name (ebl, shdr),
11517 : 0 : (uint64_t) shdr->sh_offset, (uint64_t) shdr->sh_size);
11518 : :
11519 : 0 : Elf_Data *data = elf_rawdata (scn, NULL);
11520 : :
11521 [ # # ]: 0 : if (unlikely (data == NULL))
11522 : : {
11523 : 0 : error (0, 0, _("cannot get %s content: %s"),
11524 : : ".gdb_index", elf_errmsg (-1));
11525 : 0 : return;
11526 : : }
11527 : :
11528 : : // .gdb_index is always in little endian.
11529 : 0 : Dwarf dummy_dbg = { .other_byte_order = MY_ELFDATA != ELFDATA2LSB };
11530 : 0 : dbg = &dummy_dbg;
11531 : :
11532 : 0 : const unsigned char *readp = data->d_buf;
11533 : 0 : const unsigned char *const dataend = readp + data->d_size;
11534 : :
11535 [ # # ]: 0 : if (unlikely (readp + 4 > dataend))
11536 : : {
11537 : 0 : invalid_data:
11538 : 0 : error (0, 0, _("invalid data"));
11539 : 0 : return;
11540 : : }
11541 : :
11542 : 0 : int32_t vers = read_4ubyte_unaligned (dbg, readp);
11543 : 0 : printf (_(" Version: %" PRId32 "\n"), vers);
11544 : :
11545 : : // The only difference between version 4 and version 5 is the
11546 : : // hash used for generating the table. Version 6 contains symbols
11547 : : // for inlined functions, older versions didn't. Version 7 adds
11548 : : // symbol kinds. Version 8 just indicates that it correctly includes
11549 : : // TUs for symbols. Version 9 adds shortcut table for information
11550 : : // regarding the main function.
11551 [ # # ]: 0 : if (vers < 4 || vers > 9)
11552 : : {
11553 : 0 : printf (_(" unknown version, cannot parse section\n"));
11554 : 0 : return;
11555 : : }
11556 : :
11557 : 0 : readp += 4;
11558 [ # # ]: 0 : if (unlikely (readp + 4 > dataend))
11559 : 0 : goto invalid_data;
11560 : :
11561 : 0 : uint32_t cu_off = read_4ubyte_unaligned (dbg, readp);
11562 : 0 : printf (_(" CU offset: %#" PRIx32 "\n"), cu_off);
11563 : :
11564 : 0 : readp += 4;
11565 [ # # ]: 0 : if (unlikely (readp + 4 > dataend))
11566 : 0 : goto invalid_data;
11567 : :
11568 : 0 : uint32_t tu_off = read_4ubyte_unaligned (dbg, readp);
11569 : 0 : printf (_(" TU offset: %#" PRIx32 "\n"), tu_off);
11570 : :
11571 : 0 : readp += 4;
11572 [ # # ]: 0 : if (unlikely (readp + 4 > dataend))
11573 : 0 : goto invalid_data;
11574 : :
11575 : 0 : uint32_t addr_off = read_4ubyte_unaligned (dbg, readp);
11576 : 0 : printf (_(" address offset: %#" PRIx32 "\n"), addr_off);
11577 : :
11578 : 0 : readp += 4;
11579 [ # # ]: 0 : if (unlikely (readp + 4 > dataend))
11580 : 0 : goto invalid_data;
11581 : :
11582 : 0 : uint32_t sym_off = read_4ubyte_unaligned (dbg, readp);
11583 : 0 : printf (_(" symbol offset: %#" PRIx32 "\n"), sym_off);
11584 : :
11585 : 0 : readp += 4;
11586 [ # # ]: 0 : if (unlikely (readp + 4 > dataend))
11587 : 0 : goto invalid_data;
11588 : :
11589 : 0 : uint32_t shortcut_off = 0;
11590 [ # # ]: 0 : if (vers >= 9)
11591 : : {
11592 : 0 : shortcut_off = read_4ubyte_unaligned (dbg, readp);
11593 : 0 : printf (_(" shortcut offset: %#" PRIx32 "\n"), shortcut_off);
11594 : :
11595 : 0 : readp += 4;
11596 [ # # ]: 0 : if (unlikely (readp + 4 > dataend))
11597 : 0 : goto invalid_data;
11598 : : }
11599 : :
11600 : 0 : uint32_t const_off = read_4ubyte_unaligned (dbg, readp);
11601 : 0 : printf (_(" constant offset: %#" PRIx32 "\n"), const_off);
11602 : :
11603 [ # # ]: 0 : if (unlikely ((size_t) (dataend - (const unsigned char *) data->d_buf)
11604 : : < const_off))
11605 : 0 : goto invalid_data;
11606 : :
11607 : 0 : readp = data->d_buf + cu_off;
11608 : :
11609 : 0 : const unsigned char *nextp = data->d_buf + tu_off;
11610 [ # # ]: 0 : if (tu_off >= data->d_size)
11611 : 0 : goto invalid_data;
11612 : :
11613 : 0 : size_t cu_nr = (nextp - readp) / 16;
11614 : :
11615 : 0 : printf (_("\n CU list at offset %#" PRIx32
11616 : : " contains %zu entries:\n"),
11617 : : cu_off, cu_nr);
11618 : :
11619 : 0 : size_t n = 0;
11620 [ # # # # ]: 0 : while (dataend - readp >= 16 && n < cu_nr)
11621 : : {
11622 : 0 : uint64_t off = read_8ubyte_unaligned (dbg, readp);
11623 : 0 : readp += 8;
11624 : :
11625 : 0 : uint64_t len = read_8ubyte_unaligned (dbg, readp);
11626 : 0 : readp += 8;
11627 : :
11628 : 0 : printf (" [%4zu] start: %0#8" PRIx64
11629 : : ", length: %5" PRIu64 "\n", n, off, len);
11630 : 0 : n++;
11631 : : }
11632 : :
11633 : 0 : readp = data->d_buf + tu_off;
11634 : 0 : nextp = data->d_buf + addr_off;
11635 [ # # ]: 0 : if (addr_off >= data->d_size)
11636 : 0 : goto invalid_data;
11637 : :
11638 : 0 : size_t tu_nr = (nextp - readp) / 24;
11639 : :
11640 : 0 : printf (_("\n TU list at offset %#" PRIx32
11641 : : " contains %zu entries:\n"),
11642 : : tu_off, tu_nr);
11643 : :
11644 : 0 : n = 0;
11645 [ # # # # ]: 0 : while (dataend - readp >= 24 && n < tu_nr)
11646 : : {
11647 : 0 : uint64_t off = read_8ubyte_unaligned (dbg, readp);
11648 : 0 : readp += 8;
11649 : :
11650 : 0 : uint64_t type = read_8ubyte_unaligned (dbg, readp);
11651 : 0 : readp += 8;
11652 : :
11653 : 0 : uint64_t sig = read_8ubyte_unaligned (dbg, readp);
11654 : 0 : readp += 8;
11655 : :
11656 : 0 : printf (" [%4zu] CU offset: %5" PRId64
11657 : : ", type offset: %5" PRId64
11658 : : ", signature: %0#8" PRIx64 "\n", n, off, type, sig);
11659 : 0 : n++;
11660 : : }
11661 : :
11662 : 0 : readp = data->d_buf + addr_off;
11663 : 0 : nextp = data->d_buf + sym_off;
11664 [ # # ]: 0 : if (sym_off >= data->d_size)
11665 : 0 : goto invalid_data;
11666 : :
11667 : 0 : size_t addr_nr = (nextp - readp) / 20;
11668 : :
11669 : 0 : printf (_("\n Address list at offset %#" PRIx32
11670 : : " contains %zu entries:\n"),
11671 : : addr_off, addr_nr);
11672 : :
11673 : 0 : n = 0;
11674 [ # # # # ]: 0 : while (dataend - readp >= 20 && n < addr_nr)
11675 : : {
11676 : 0 : uint64_t low = read_8ubyte_unaligned (dbg, readp);
11677 : 0 : readp += 8;
11678 : :
11679 : 0 : uint64_t high = read_8ubyte_unaligned (dbg, readp);
11680 : 0 : readp += 8;
11681 : :
11682 : 0 : uint32_t idx = read_4ubyte_unaligned (dbg, readp);
11683 : 0 : readp += 4;
11684 : :
11685 : 0 : printf (" [%4zu] ", n);
11686 : 0 : print_dwarf_addr (dwflmod, 8, low, low);
11687 : 0 : printf ("..");
11688 : 0 : print_dwarf_addr (dwflmod, 8, high - 1, high);
11689 : 0 : printf (", CU index: %5" PRId32 "\n", idx);
11690 : 0 : n++;
11691 : : }
11692 : :
11693 : 0 : const unsigned char *const_start = data->d_buf + const_off;
11694 [ # # ]: 0 : if (const_off > data->d_size)
11695 : 0 : goto invalid_data;
11696 : :
11697 : 0 : const unsigned char *shortcut_start = NULL;
11698 [ # # ]: 0 : if (vers >= 9)
11699 : : {
11700 [ # # ]: 0 : if (shortcut_off >= data->d_size)
11701 : 0 : goto invalid_data;
11702 : :
11703 : 0 : shortcut_start = data->d_buf + shortcut_off;
11704 : 0 : nextp = shortcut_start;
11705 : : }
11706 : : else
11707 : : nextp = const_start;
11708 : :
11709 : 0 : readp = data->d_buf + sym_off;
11710 : 0 : size_t sym_nr = (nextp - readp) / 8;
11711 : :
11712 : 0 : printf (_("\n Symbol table at offset %#" PRIx32
11713 : : " contains %zu slots:\n"),
11714 : : sym_off, sym_nr);
11715 : :
11716 : 0 : n = 0;
11717 [ # # # # ]: 0 : while (dataend - readp >= 8 && n < sym_nr)
11718 : : {
11719 : 0 : uint32_t name = read_4ubyte_unaligned (dbg, readp);
11720 : 0 : readp += 4;
11721 : :
11722 : 0 : uint32_t vector = read_4ubyte_unaligned (dbg, readp);
11723 : 0 : readp += 4;
11724 : :
11725 [ # # ]: 0 : if (name != 0 || vector != 0)
11726 : : {
11727 : 0 : const unsigned char *sym = const_start + name;
11728 [ # # # # ]: 0 : if (unlikely ((size_t) (dataend - const_start) < name
11729 : : || memchr (sym, '\0', dataend - sym) == NULL))
11730 : 0 : goto invalid_data;
11731 : :
11732 : 0 : printf (" [%4zu] symbol: %s, CUs: ", n, sym);
11733 : :
11734 : 0 : const unsigned char *readcus = const_start + vector;
11735 [ # # ]: 0 : if (unlikely ((size_t) (dataend - const_start) < vector))
11736 : 0 : goto invalid_data;
11737 : 0 : uint32_t cus = read_4ubyte_unaligned (dbg, readcus);
11738 : 0 : while (cus--)
11739 : : {
11740 : 0 : uint32_t cu_kind, cu, kind;
11741 : 0 : bool is_static;
11742 : 0 : readcus += 4;
11743 [ # # ]: 0 : if (unlikely (readcus + 4 > dataend))
11744 : 0 : goto invalid_data;
11745 : 0 : cu_kind = read_4ubyte_unaligned (dbg, readcus);
11746 : 0 : cu = cu_kind & ((1 << 24) - 1);
11747 : 0 : kind = (cu_kind >> 28) & 7;
11748 : 0 : is_static = cu_kind & (1U << 31);
11749 [ # # ]: 0 : if (cu > cu_nr - 1)
11750 : 0 : printf ("%" PRId32 "T", cu - (uint32_t) cu_nr);
11751 : : else
11752 : 0 : printf ("%" PRId32, cu);
11753 [ # # ]: 0 : if (kind != 0)
11754 : : {
11755 : 0 : printf (" (");
11756 [ # # # # : 0 : switch (kind)
# ]
11757 : : {
11758 : : case 1:
11759 : 0 : printf ("type");
11760 : : break;
11761 : : case 2:
11762 : 0 : printf ("var");
11763 : : break;
11764 : : case 3:
11765 : 0 : printf ("func");
11766 : : break;
11767 : : case 4:
11768 : 0 : printf ("other");
11769 : : break;
11770 : : default:
11771 : 0 : printf ("unknown-0x%" PRIx32, kind);
11772 : : break;
11773 : : }
11774 [ # # ]: 0 : printf (":%c)", (is_static ? 'S' : 'G'));
11775 : : }
11776 [ # # ]: 0 : if (cus > 0)
11777 [ # # ]: 0 : printf (", ");
11778 : : }
11779 : 0 : printf ("\n");
11780 : : }
11781 : 0 : n++;
11782 : : }
11783 : :
11784 [ # # ]: 0 : if (vers < 9)
11785 : : return;
11786 : :
11787 [ # # ]: 0 : if (unlikely (shortcut_start == NULL))
11788 : 0 : goto invalid_data;
11789 : :
11790 : 0 : readp = shortcut_start;
11791 : 0 : nextp = const_start;
11792 : 0 : size_t shortcut_nr = (nextp - readp) / 4;
11793 : :
11794 [ # # ]: 0 : if (unlikely (shortcut_nr != 2))
11795 : 0 : goto invalid_data;
11796 : :
11797 : 0 : printf (_("\nShortcut table at offset %#" PRIx32 " contains %zu slots:\n"),
11798 : : shortcut_off, shortcut_nr);
11799 : :
11800 : 0 : uint32_t lang = read_4ubyte_unaligned (dbg, readp);
11801 : 0 : readp += 4;
11802 : :
11803 : : /* Include the hex number of LANG in the output if the language
11804 : : is unknown. */
11805 : 0 : const char *lang_str = dwarf_lang_string (lang);
11806 : 0 : lang_str = string_or_unknown (lang_str, lang, DW_LANG_lo_user,
11807 : : DW_LANG_hi_user, true);
11808 : :
11809 : 0 : printf (_("Language of main: %s\n"), lang_str);
11810 : 0 : printf (_("Name of main: "));
11811 : :
11812 [ # # ]: 0 : if (lang != 0)
11813 : : {
11814 : 0 : uint32_t name = read_4ubyte_unaligned (dbg, readp);
11815 : 0 : readp += 4;
11816 : 0 : const unsigned char *sym = const_start + name;
11817 : :
11818 [ # # # # ]: 0 : if (unlikely ((size_t) (dataend - const_start) < name
11819 : : || memchr (sym, '\0', dataend - sym) == NULL))
11820 : 0 : goto invalid_data;
11821 : :
11822 : 0 : printf ("%s\n", sym);
11823 : : }
11824 : : else
11825 : 0 : printf ("<unknown>\n");
11826 : : }
11827 : :
11828 : : /* Returns true and sets split DWARF CU id if there is a split compile
11829 : : unit in the given Dwarf, and no non-split units are found (before it). */
11830 : : static bool
11831 : 0 : is_split_dwarf (Dwarf *dbg, uint64_t *id, Dwarf_CU **split_cu)
11832 : : {
11833 : 0 : Dwarf_CU *cu = NULL;
11834 [ # # ]: 0 : while (dwarf_get_units (dbg, cu, &cu, NULL, NULL, NULL, NULL) == 0)
11835 : : {
11836 : 0 : uint8_t unit_type;
11837 [ # # ]: 0 : if (dwarf_cu_info (cu, NULL, &unit_type, NULL, NULL,
11838 : : id, NULL, NULL) != 0)
11839 : 0 : return false;
11840 : :
11841 [ # # ]: 0 : if (unit_type != DW_UT_split_compile && unit_type != DW_UT_split_type)
11842 : : return false;
11843 : :
11844 : : /* We really only care about the split compile unit, the types
11845 : : should be fine and self sufficient. Also they don't have an
11846 : : id that we can match with a skeleton unit. */
11847 [ # # ]: 0 : if (unit_type == DW_UT_split_compile)
11848 : : {
11849 : 0 : *split_cu = cu;
11850 : 0 : return true;
11851 : : }
11852 : : }
11853 : :
11854 : : return false;
11855 : : }
11856 : :
11857 : : /* Check that there is one and only one Dwfl_Module, return in arg. */
11858 : : static int
11859 : 0 : getone_dwflmod (Dwfl_Module *dwflmod,
11860 : : void **userdata __attribute__ ((unused)),
11861 : : const char *name __attribute__ ((unused)),
11862 : : Dwarf_Addr base __attribute__ ((unused)),
11863 : : void *arg)
11864 : : {
11865 : 0 : Dwfl_Module **m = (Dwfl_Module **) arg;
11866 [ # # ]: 0 : if (*m != NULL)
11867 : : return DWARF_CB_ABORT;
11868 : 0 : *m = dwflmod;
11869 : 0 : return DWARF_CB_OK;
11870 : : }
11871 : :
11872 : : static void
11873 : 0 : print_debug (Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr)
11874 : : {
11875 : : /* Used for skeleton file, if necessary for split DWARF. */
11876 : 0 : Dwfl *skel_dwfl = NULL;
11877 : 0 : Dwfl_Module *skel_mod = NULL;
11878 : 0 : char *skel_name = NULL;
11879 : 0 : Dwarf *split_dbg = NULL;
11880 : 0 : Dwarf_CU *split_cu = NULL;
11881 : :
11882 : : /* Before we start the real work get a debug context descriptor. */
11883 : 0 : Dwarf_Addr dwbias;
11884 : 0 : Dwarf *dbg = dwfl_module_getdwarf (dwflmod, &dwbias);
11885 : 0 : Dwarf dummy_dbg =
11886 : : {
11887 : 0 : .elf = ebl->elf,
11888 : 0 : .other_byte_order = MY_ELFDATA != ehdr->e_ident[EI_DATA]
11889 : : };
11890 [ # # ]: 0 : if (dbg == NULL)
11891 : : {
11892 [ # # ]: 0 : if ((print_debug_sections & ~(section_exception|section_frame)) != 0)
11893 : 0 : error (0, 0, _("cannot get debug context descriptor: %s"),
11894 : : dwfl_errmsg (-1));
11895 : : dbg = &dummy_dbg;
11896 : : }
11897 : : else
11898 : : {
11899 : : /* If we are asked about a split dwarf (.dwo) file, use the user
11900 : : provided, or find the corresponding skeleton file. If we got
11901 : : a skeleton file, replace the given dwflmod and dbg, with one
11902 : : derived from the skeleton file to provide enough context. */
11903 : 0 : uint64_t split_id;
11904 [ # # ]: 0 : if (is_split_dwarf (dbg, &split_id, &split_cu))
11905 : : {
11906 [ # # ]: 0 : if (dwarf_skeleton != NULL)
11907 : 0 : skel_name = strdup (dwarf_skeleton);
11908 : : else
11909 : : {
11910 : : /* Replace file.dwo with file.o and see if that matches. */
11911 : 0 : const char *fname;
11912 : 0 : dwfl_module_info (dwflmod, NULL, NULL, NULL, NULL, NULL,
11913 : : &fname, NULL);
11914 [ # # ]: 0 : if (fname != NULL)
11915 : : {
11916 : 0 : size_t flen = strlen (fname);
11917 [ # # # # ]: 0 : if (flen > 4 && strcmp (".dwo", fname + flen - 4) == 0)
11918 : : {
11919 : 0 : skel_name = strdup (fname);
11920 [ # # ]: 0 : if (skel_name != NULL)
11921 : : {
11922 : 0 : skel_name[flen - 3] = 'o';
11923 : 0 : skel_name[flen - 2] = '\0';
11924 : : }
11925 : : }
11926 : : }
11927 : : }
11928 : :
11929 [ # # ]: 0 : if (skel_name != NULL)
11930 : : {
11931 : 0 : int skel_fd = open (skel_name, O_RDONLY);
11932 [ # # ]: 0 : if (skel_fd == -1)
11933 : 0 : fprintf (stderr, "Warning: Couldn't open DWARF skeleton file"
11934 : : " '%s'\n", skel_name);
11935 : : else
11936 : : {
11937 : 0 : skel_dwfl = create_dwfl (skel_fd, skel_name);
11938 : :
11939 : : /* skel_fd was dup'ed by create_dwfl. We can close the
11940 : : original now. */
11941 : 0 : close (skel_fd);
11942 : : }
11943 : :
11944 [ # # ]: 0 : if (skel_dwfl != NULL)
11945 : : {
11946 [ # # ]: 0 : if (dwfl_getmodules (skel_dwfl, &getone_dwflmod,
11947 : : &skel_mod, 0) != 0)
11948 : : {
11949 : 0 : fprintf (stderr, "Warning: Bad DWARF skeleton,"
11950 : : " multiple modules '%s'\n", skel_name);
11951 : 0 : dwfl_end (skel_dwfl);
11952 : 0 : skel_mod = NULL;
11953 : : }
11954 : : }
11955 [ # # ]: 0 : else if (skel_fd != -1)
11956 : 0 : fprintf (stderr, "Warning: Couldn't create skeleton dwfl for"
11957 : : " '%s': %s\n", skel_name, dwfl_errmsg (-1));
11958 : :
11959 [ # # ]: 0 : if (skel_mod != NULL)
11960 : : {
11961 : 0 : Dwarf *skel_dbg = dwfl_module_getdwarf (skel_mod, &dwbias);
11962 [ # # ]: 0 : if (skel_dbg != NULL)
11963 : : {
11964 : : /* First check the skeleton CU DIE, only fetch
11965 : : the split DIE if we know the id matches to
11966 : : not unnecessary search for any split DIEs we
11967 : : don't need. */
11968 : 0 : Dwarf_CU *cu = NULL;
11969 : 0 : while (dwarf_get_units (skel_dbg, cu, &cu,
11970 [ # # ]: 0 : NULL, NULL, NULL, NULL) == 0)
11971 : : {
11972 : 0 : uint8_t unit_type;
11973 : 0 : uint64_t skel_id;
11974 [ # # ]: 0 : if (dwarf_cu_info (cu, NULL, &unit_type, NULL, NULL,
11975 : : &skel_id, NULL, NULL) == 0
11976 [ # # ]: 0 : && unit_type == DW_UT_skeleton
11977 [ # # ]: 0 : && split_id == skel_id)
11978 : : {
11979 : 0 : Dwarf_Die subdie;
11980 [ # # ]: 0 : if (dwarf_cu_info (cu, NULL, NULL, NULL,
11981 : : &subdie,
11982 : : NULL, NULL, NULL) == 0
11983 [ # # ]: 0 : && dwarf_tag (&subdie) != DW_TAG_invalid)
11984 : : {
11985 : 0 : split_dbg = dwarf_cu_getdwarf (subdie.cu);
11986 [ # # ]: 0 : if (split_dbg == NULL)
11987 : 0 : fprintf (stderr,
11988 : : "Warning: Couldn't get split_dbg:"
11989 : : " %s\n", dwarf_errmsg (-1));
11990 : 0 : break;
11991 : : }
11992 : : else
11993 : : {
11994 : : /* Everything matches up, but not
11995 : : according to libdw. Which means
11996 : : the user knew better. So...
11997 : : Terrible hack... We can never
11998 : : destroy the underlying dwfl
11999 : : because it would free the wrong
12000 : : Dwarfs... So we leak memory...*/
12001 [ # # ]: 0 : if (cu->split == NULL
12002 [ # # ]: 0 : && dwarf_skeleton != NULL)
12003 : : {
12004 : 0 : do_not_close_dwfl = true;
12005 : 0 : __libdw_link_skel_split (cu, split_cu);
12006 : 0 : split_dbg = dwarf_cu_getdwarf (split_cu);
12007 : 0 : break;
12008 : : }
12009 : : else
12010 : 0 : fprintf (stderr, "Warning: Couldn't get"
12011 : : " skeleton subdie: %s\n",
12012 : : dwarf_errmsg (-1));
12013 : : }
12014 : : }
12015 : : }
12016 [ # # ]: 0 : if (split_dbg == NULL)
12017 : 0 : fprintf (stderr, "Warning: '%s' didn't contain a skeleton for split id %" PRIx64 "\n", skel_name, split_id);
12018 : : }
12019 : : else
12020 : 0 : fprintf (stderr, "Warning: Couldn't get skeleton DWARF:"
12021 : : " %s\n", dwfl_errmsg (-1));
12022 : : }
12023 : : }
12024 : :
12025 : 0 : if (split_dbg != NULL)
12026 : : {
12027 : 0 : dbg = split_dbg;
12028 : 0 : dwflmod = skel_mod;
12029 : : }
12030 [ # # ]: 0 : else if (skel_name == NULL)
12031 : 0 : fprintf (stderr,
12032 : : "Warning: split DWARF file, but no skeleton found.\n");
12033 : : }
12034 [ # # ]: 0 : else if (dwarf_skeleton != NULL)
12035 : 0 : fprintf (stderr, "Warning: DWARF skeleton given,"
12036 : : " but not a split DWARF file\n");
12037 : : }
12038 : :
12039 : : /* Get the section header string table index. */
12040 : 0 : size_t shstrndx;
12041 [ # # ]: 0 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
12042 : 0 : error_exit (0, _("cannot get section header string table index"));
12043 : :
12044 : : /* If the .debug_info section is listed as implicitly required then
12045 : : we must make sure to handle it before handling any other debug
12046 : : section. Various other sections depend on the CU DIEs being
12047 : : scanned (silently) first. */
12048 : 0 : bool implicit_info = (implicit_debug_sections & section_info) != 0;
12049 : 0 : bool explicit_info = (print_debug_sections & section_info) != 0;
12050 [ # # ]: 0 : if (implicit_info)
12051 : : {
12052 : : Elf_Scn *scn = NULL;
12053 [ # # ]: 0 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
12054 : : {
12055 : 0 : GElf_Shdr shdr_mem;
12056 : 0 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
12057 : :
12058 [ # # # # ]: 0 : if (shdr != NULL && shdr->sh_type == SHT_PROGBITS)
12059 : : {
12060 : 0 : const char *name = elf_strptr (ebl->elf, shstrndx,
12061 : 0 : shdr->sh_name);
12062 [ # # ]: 0 : if (name == NULL)
12063 : 0 : continue;
12064 : :
12065 [ # # ]: 0 : if (strcmp (name, ".debug_info") == 0
12066 [ # # ]: 0 : || strcmp (name, ".debug_info.dwo") == 0
12067 [ # # ]: 0 : || strcmp (name, ".zdebug_info") == 0
12068 [ # # ]: 0 : || strcmp (name, ".zdebug_info.dwo") == 0
12069 [ # # ]: 0 : || strcmp (name, ".gnu.debuglto_.debug_info") == 0)
12070 : : {
12071 : 0 : print_debug_info_section (dwflmod, ebl, ehdr,
12072 : : scn, shdr, dbg);
12073 : 0 : break;
12074 : : }
12075 : : }
12076 : : }
12077 : 0 : print_debug_sections &= ~section_info;
12078 : 0 : implicit_debug_sections &= ~section_info;
12079 : : }
12080 : :
12081 : : /* Look through all the sections for the debugging sections to print. */
12082 : : Elf_Scn *scn = NULL;
12083 [ # # ]: 0 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
12084 : : {
12085 : 0 : GElf_Shdr shdr_mem;
12086 : 0 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
12087 : :
12088 [ # # # # ]: 0 : if (shdr != NULL && shdr->sh_type == SHT_PROGBITS)
12089 : : {
12090 : 0 : static const struct
12091 : : {
12092 : : const char *name;
12093 : : enum section_e bitmask;
12094 : : void (*fp) (Dwfl_Module *, Ebl *,
12095 : : GElf_Ehdr *, Elf_Scn *, GElf_Shdr *, Dwarf *);
12096 : : } debug_sections[] =
12097 : : {
12098 : : #define NEW_SECTION(name) \
12099 : : { ".debug_" #name, section_##name, print_debug_##name##_section }
12100 : : NEW_SECTION (abbrev),
12101 : : NEW_SECTION (addr),
12102 : : NEW_SECTION (aranges),
12103 : : NEW_SECTION (frame),
12104 : : NEW_SECTION (info),
12105 : : NEW_SECTION (types),
12106 : : NEW_SECTION (line),
12107 : : NEW_SECTION (loc),
12108 : : /* loclists is loc for DWARF5. */
12109 : : { ".debug_loclists", section_loc,
12110 : : print_debug_loclists_section },
12111 : : NEW_SECTION (pubnames),
12112 : : NEW_SECTION (str),
12113 : : /* A DWARF5 specialised debug string section. */
12114 : : { ".debug_line_str", section_str,
12115 : : print_debug_str_section },
12116 : : /* DWARF5 string offsets table. */
12117 : : { ".debug_str_offsets", section_str,
12118 : : print_debug_str_offsets_section },
12119 : : NEW_SECTION (macinfo),
12120 : : NEW_SECTION (macro),
12121 : : NEW_SECTION (ranges),
12122 : : /* rnglists is ranges for DWARF5. */
12123 : : { ".debug_rnglists", section_ranges,
12124 : : print_debug_rnglists_section },
12125 : : { ".eh_frame", section_frame | section_exception,
12126 : : print_debug_frame_section },
12127 : : { ".eh_frame_hdr", section_frame | section_exception,
12128 : : print_debug_frame_hdr_section },
12129 : : { ".gcc_except_table", section_frame | section_exception,
12130 : : print_debug_exception_table },
12131 : : { ".gdb_index", section_gdb_index, print_gdb_index_section }
12132 : : };
12133 : 0 : const int ndebug_sections = (sizeof (debug_sections)
12134 : : / sizeof (debug_sections[0]));
12135 : 0 : const char *name = elf_strptr (ebl->elf, shstrndx,
12136 : 0 : shdr->sh_name);
12137 [ # # ]: 0 : if (name == NULL)
12138 : 0 : continue;
12139 : :
12140 : : int n;
12141 [ # # ]: 0 : for (n = 0; n < ndebug_sections; ++n)
12142 : : {
12143 : 0 : size_t dbglen = strlen (debug_sections[n].name);
12144 : 0 : size_t scnlen = strlen (name);
12145 [ # # ]: 0 : if ((strncmp (name, debug_sections[n].name, dbglen) == 0
12146 [ # # ]: 0 : && (dbglen == scnlen
12147 [ # # ]: 0 : || (scnlen == dbglen + 4
12148 [ # # ]: 0 : && strstr (name, ".dwo") == name + dbglen)))
12149 [ # # # # ]: 0 : || (name[0] == '.' && name[1] == 'z'
12150 [ # # ]: 0 : && debug_sections[n].name[1] == 'd'
12151 [ # # ]: 0 : && strncmp (&name[2], &debug_sections[n].name[1],
12152 : : dbglen - 1) == 0
12153 [ # # ]: 0 : && (scnlen == dbglen + 1
12154 [ # # ]: 0 : || (scnlen == dbglen + 5
12155 [ # # ]: 0 : && strstr (name, ".dwo") == name + dbglen + 1)))
12156 [ # # ]: 0 : || (scnlen > 14 /* .gnu.debuglto_ prefix. */
12157 [ # # ]: 0 : && startswith (name, ".gnu.debuglto_")
12158 [ # # ]: 0 : && strcmp (&name[14], debug_sections[n].name) == 0)
12159 : : )
12160 : : {
12161 : 0 : if ((print_debug_sections | implicit_debug_sections)
12162 [ # # ]: 0 : & debug_sections[n].bitmask)
12163 : 0 : debug_sections[n].fp (dwflmod, ebl, ehdr, scn, shdr, dbg);
12164 : : break;
12165 : : }
12166 : : }
12167 : : }
12168 : : }
12169 : :
12170 : 0 : dwfl_end (skel_dwfl);
12171 : 0 : free (skel_name);
12172 : :
12173 : : /* Turn implicit and/or explicit back on in case we go over another file. */
12174 [ # # ]: 0 : if (implicit_info)
12175 : 0 : implicit_debug_sections |= section_info;
12176 [ # # ]: 0 : if (explicit_info)
12177 : 0 : print_debug_sections |= section_info;
12178 : :
12179 : 0 : reset_listptr (&known_locsptr);
12180 : 0 : reset_listptr (&known_loclistsptr);
12181 : 0 : reset_listptr (&known_rangelistptr);
12182 : 0 : reset_listptr (&known_rnglistptr);
12183 : 0 : reset_listptr (&known_addrbases);
12184 : 0 : reset_listptr (&known_stroffbases);
12185 : 0 : }
12186 : :
12187 : :
12188 : : #define ITEM_INDENT 4
12189 : : #define WRAP_COLUMN 75
12190 : :
12191 : : /* Print "NAME: FORMAT", wrapping when output text would make the line
12192 : : exceed WRAP_COLUMN. Unpadded numbers look better for the core items
12193 : : but this function is also used for registers which should be printed
12194 : : aligned. Fortunately registers output uses fixed fields width (such
12195 : : as %11d) for the alignment.
12196 : :
12197 : : Line breaks should not depend on the particular values although that
12198 : : may happen in some cases of the core items. */
12199 : :
12200 : : static unsigned int
12201 : : __attribute__ ((format (printf, 6, 7)))
12202 : 0 : print_core_item (unsigned int colno, char sep, unsigned int wrap,
12203 : : size_t name_width, const char *name, const char *format, ...)
12204 : : {
12205 : 0 : size_t len = strlen (name);
12206 : 0 : if (name_width < len)
12207 : : name_width = len;
12208 : :
12209 : 0 : char *out;
12210 : 0 : va_list ap;
12211 : 0 : va_start (ap, format);
12212 : 0 : int out_len = vasprintf (&out, format, ap);
12213 : 0 : va_end (ap);
12214 [ # # ]: 0 : if (out_len == -1)
12215 : 0 : error_exit (0, _("memory exhausted"));
12216 : :
12217 : 0 : size_t n = name_width + sizeof ": " - 1 + out_len;
12218 : :
12219 [ # # ]: 0 : if (colno == 0)
12220 : : {
12221 : 0 : printf ("%*s", ITEM_INDENT, "");
12222 : 0 : colno = ITEM_INDENT + n;
12223 : : }
12224 [ # # ]: 0 : else if (colno + 2 + n < wrap)
12225 : : {
12226 : 0 : printf ("%c ", sep);
12227 : 0 : colno += 2 + n;
12228 : : }
12229 : : else
12230 : : {
12231 : 0 : printf ("\n%*s", ITEM_INDENT, "");
12232 : 0 : colno = ITEM_INDENT + n;
12233 : : }
12234 : :
12235 : 0 : printf ("%s: %*s%s", name, (int) (name_width - len), "", out);
12236 : :
12237 : 0 : free (out);
12238 : :
12239 : 0 : return colno;
12240 : : }
12241 : :
12242 : : static const void *
12243 : 0 : convert (Elf *core, Elf_Type type, uint_fast16_t count,
12244 : : void *value, const void *data, size_t size)
12245 : : {
12246 : 0 : Elf_Data valuedata =
12247 : : {
12248 : : .d_type = type,
12249 : : .d_buf = value,
12250 [ # # ]: 0 : .d_size = size ?: gelf_fsize (core, type, count, EV_CURRENT),
12251 : : .d_version = EV_CURRENT,
12252 : : };
12253 : 0 : Elf_Data indata =
12254 : : {
12255 : : .d_type = type,
12256 : : .d_buf = (void *) data,
12257 : : .d_size = valuedata.d_size,
12258 : : .d_version = EV_CURRENT,
12259 : : };
12260 : :
12261 : 0 : Elf_Data *d = (gelf_getclass (core) == ELFCLASS32
12262 [ # # ]: 0 : ? elf32_xlatetom : elf64_xlatetom)
12263 : 0 : (&valuedata, &indata, elf_getident (core, NULL)[EI_DATA]);
12264 [ # # ]: 0 : if (d == NULL)
12265 : 0 : error_exit (0, _("cannot convert core note data: %s"),
12266 : : elf_errmsg (-1));
12267 : :
12268 : 0 : return data + indata.d_size;
12269 : : }
12270 : :
12271 : : typedef uint8_t GElf_Byte;
12272 : :
12273 : : static unsigned int
12274 : 0 : handle_core_item (Elf *core, const Ebl_Core_Item *item, const void *desc,
12275 : : unsigned int colno, size_t *repeated_size)
12276 : : {
12277 [ # # ]: 0 : uint_fast16_t count = item->count ?: 1;
12278 : : /* Ebl_Core_Item count is always a small number.
12279 : : Make sure the backend didn't put in some large bogus value. */
12280 [ # # ]: 0 : assert (count < 128);
12281 : :
12282 : : #define TYPES \
12283 : : DO_TYPE (BYTE, Byte, "0x%.2" PRIx8, "%" PRId8); \
12284 : : DO_TYPE (HALF, Half, "0x%.4" PRIx16, "%" PRId16); \
12285 : : DO_TYPE (WORD, Word, "0x%.8" PRIx32, "%" PRId32); \
12286 : : DO_TYPE (SWORD, Sword, "%" PRId32, "%" PRId32); \
12287 : : DO_TYPE (XWORD, Xword, "0x%.16" PRIx64, "%" PRId64); \
12288 : : DO_TYPE (SXWORD, Sxword, "%" PRId64, "%" PRId64)
12289 : :
12290 : : #define DO_TYPE(NAME, Name, hex, dec) GElf_##Name Name
12291 : 0 : typedef union { TYPES; } value_t;
12292 : 0 : void *data = alloca (count * sizeof (value_t));
12293 : : #undef DO_TYPE
12294 : :
12295 : : #define DO_TYPE(NAME, Name, hex, dec) \
12296 : : GElf_##Name *value_##Name __attribute__((unused)) = data
12297 : 0 : TYPES;
12298 : : #undef DO_TYPE
12299 : :
12300 : 0 : size_t size = gelf_fsize (core, item->type, count, EV_CURRENT);
12301 : 0 : size_t convsize = size;
12302 [ # # ]: 0 : if (repeated_size != NULL)
12303 : : {
12304 [ # # # # ]: 0 : if (*repeated_size > size && (item->format == 'b' || item->format == 'B'))
12305 : : {
12306 : 0 : data = alloca (*repeated_size);
12307 : 0 : count *= *repeated_size / size;
12308 : 0 : convsize = count * size;
12309 : 0 : *repeated_size -= convsize;
12310 : : }
12311 [ # # # # ]: 0 : else if (item->count != 0 || item->format != '\n')
12312 : 0 : *repeated_size -= size;
12313 : : }
12314 : :
12315 : 0 : convert (core, item->type, count, data, desc + item->offset, convsize);
12316 : :
12317 : 0 : Elf_Type type = item->type;
12318 [ # # ]: 0 : if (type == ELF_T_ADDR)
12319 [ # # ]: 0 : type = gelf_getclass (core) == ELFCLASS32 ? ELF_T_WORD : ELF_T_XWORD;
12320 : :
12321 [ # # # # : 0 : switch (item->format)
# # # #
# ]
12322 : : {
12323 : 0 : case 'd':
12324 [ # # ]: 0 : assert (count == 1);
12325 [ # # # # : 0 : switch (type)
# # # ]
12326 : : {
12327 : : #define DO_TYPE(NAME, Name, hex, dec) \
12328 : : case ELF_T_##NAME: \
12329 : : colno = print_core_item (colno, ',', WRAP_COLUMN, \
12330 : : 0, item->name, dec, value_##Name[0]); \
12331 : : break
12332 : 0 : TYPES;
12333 : : #undef DO_TYPE
12334 : 0 : default:
12335 : 0 : abort ();
12336 : : }
12337 : : break;
12338 : :
12339 : 0 : case 'x':
12340 [ # # ]: 0 : assert (count == 1);
12341 [ # # # # : 0 : switch (type)
# # # ]
12342 : : {
12343 : : #define DO_TYPE(NAME, Name, hex, dec) \
12344 : : case ELF_T_##NAME: \
12345 : : colno = print_core_item (colno, ',', WRAP_COLUMN, \
12346 : : 0, item->name, hex, value_##Name[0]); \
12347 : : break
12348 : 0 : TYPES;
12349 : : #undef DO_TYPE
12350 : 0 : default:
12351 : 0 : abort ();
12352 : : }
12353 : : break;
12354 : :
12355 : 0 : case 'b':
12356 : : case 'B':
12357 [ # # ]: 0 : assert (size % sizeof (unsigned int) == 0);
12358 : 0 : unsigned int nbits = count * size * 8;
12359 : 0 : unsigned int pop = 0;
12360 [ # # ]: 0 : for (const unsigned int *i = data; (void *) i < data + count * size; ++i)
12361 : 0 : pop += __builtin_popcount (*i);
12362 : 0 : bool negate = pop > nbits / 2;
12363 : 0 : const unsigned int bias = item->format == 'b';
12364 : :
12365 : 0 : {
12366 [ # # ]: 0 : char printed[(negate ? nbits - pop : pop) * 16 + 1];
12367 : 0 : char *p = printed;
12368 : 0 : *p = '\0';
12369 : :
12370 : 0 : if (BYTE_ORDER != LITTLE_ENDIAN && size > sizeof (unsigned int))
12371 : : {
12372 : : assert (size == sizeof (unsigned int) * 2);
12373 : : for (unsigned int *i = data;
12374 : : (void *) i < data + count * size; i += 2)
12375 : : {
12376 : : unsigned int w = i[1];
12377 : : i[1] = i[0];
12378 : : i[0] = w;
12379 : : }
12380 : : }
12381 : :
12382 : 0 : unsigned int lastbit = 0;
12383 : 0 : unsigned int run = 0;
12384 : 0 : for (const unsigned int *i = data;
12385 [ # # ]: 0 : (void *) i < data + count * size; ++i)
12386 : : {
12387 : 0 : unsigned int bit = ((void *) i - data) * 8;
12388 [ # # ]: 0 : unsigned int w = negate ? ~*i : *i;
12389 [ # # ]: 0 : while (w != 0)
12390 : : {
12391 : : /* Note that a right shift equal to (or greater than)
12392 : : the number of bits of w is undefined behaviour. In
12393 : : particular when the least significant bit is bit 32
12394 : : (w = 0x8000000) then w >>= n is undefined. So
12395 : : explicitly handle that case separately. */
12396 : 0 : unsigned int n = ffs (w);
12397 [ # # ]: 0 : if (n < sizeof (w) * 8)
12398 : 0 : w >>= n;
12399 : : else
12400 : : w = 0;
12401 : 0 : bit += n;
12402 : :
12403 [ # # # # ]: 0 : if (lastbit != 0 && lastbit + 1 == bit)
12404 : 0 : ++run;
12405 : : else
12406 : : {
12407 : 0 : if (lastbit == 0)
12408 : 0 : p += sprintf (p, "%u", bit - bias);
12409 [ # # ]: 0 : else if (run == 0)
12410 : 0 : p += sprintf (p, ",%u", bit - bias);
12411 : : else
12412 : 0 : p += sprintf (p, "-%u,%u", lastbit - bias, bit - bias);
12413 : : run = 0;
12414 : : }
12415 : :
12416 : : lastbit = bit;
12417 : : }
12418 : : }
12419 [ # # # # ]: 0 : if (lastbit > 0 && run > 0 && lastbit + 1 != nbits)
12420 : 0 : p += sprintf (p, "-%u", lastbit - bias);
12421 : :
12422 [ # # ]: 0 : colno = print_core_item (colno, ',', WRAP_COLUMN, 0, item->name,
12423 : : negate ? "~<%s>" : "<%s>", printed);
12424 : : }
12425 : 0 : break;
12426 : :
12427 : 0 : case 'T':
12428 : : case (char) ('T'|0x80):
12429 [ # # ]: 0 : assert (count == 2);
12430 : 0 : Dwarf_Word sec;
12431 : 0 : Dwarf_Word usec;
12432 [ # # # # : 0 : switch (type)
# # # ]
12433 : : {
12434 : : #define DO_TYPE(NAME, Name, hex, dec) \
12435 : : case ELF_T_##NAME: \
12436 : : sec = value_##Name[0]; \
12437 : : usec = value_##Name[1]; \
12438 : : break
12439 : 0 : TYPES;
12440 : : #undef DO_TYPE
12441 : 0 : default:
12442 : 0 : abort ();
12443 : : }
12444 [ # # ]: 0 : if (unlikely (item->format == (char) ('T'|0x80)))
12445 : : {
12446 : : /* This is a hack for an ill-considered 64-bit ABI where
12447 : : tv_usec is actually a 32-bit field with 32 bits of padding
12448 : : rounding out struct timeval. We've already converted it as
12449 : : a 64-bit field. For little-endian, this just means the
12450 : : high half is the padding; it's presumably zero, but should
12451 : : be ignored anyway. For big-endian, it means the 32-bit
12452 : : field went into the high half of USEC. */
12453 : 0 : GElf_Ehdr ehdr_mem;
12454 : 0 : GElf_Ehdr *ehdr = gelf_getehdr (core, &ehdr_mem);
12455 [ # # ]: 0 : if (likely (ehdr->e_ident[EI_DATA] == ELFDATA2MSB))
12456 : 0 : usec >>= 32;
12457 : : else
12458 : 0 : usec &= UINT32_MAX;
12459 : : }
12460 : 0 : colno = print_core_item (colno, ',', WRAP_COLUMN, 0, item->name,
12461 : : "%" PRIu64 ".%.6" PRIu64, sec, usec);
12462 : 0 : break;
12463 : :
12464 : 0 : case 'c':
12465 [ # # ]: 0 : assert (count == 1);
12466 : 0 : colno = print_core_item (colno, ',', WRAP_COLUMN, 0, item->name,
12467 : 0 : "%c", value_Byte[0]);
12468 : 0 : break;
12469 : :
12470 : 0 : case 's':
12471 : 0 : colno = print_core_item (colno, ',', WRAP_COLUMN, 0, item->name,
12472 : : "%.*s", (int) count, value_Byte);
12473 : 0 : break;
12474 : :
12475 : 0 : case '\n':
12476 : : /* This is a list of strings separated by '\n'. */
12477 [ # # ]: 0 : assert (item->count == 0);
12478 [ # # ]: 0 : assert (repeated_size != NULL);
12479 [ # # ]: 0 : assert (item->name == NULL);
12480 [ # # ]: 0 : if (unlikely (item->offset >= *repeated_size))
12481 : : break;
12482 : :
12483 : 0 : const char *s = desc + item->offset;
12484 : 0 : size = *repeated_size - item->offset;
12485 : 0 : *repeated_size = 0;
12486 [ # # ]: 0 : while (size > 0)
12487 : : {
12488 : 0 : const char *eol = memchr (s, '\n', size);
12489 : 0 : int len = size;
12490 [ # # ]: 0 : if (eol != NULL)
12491 : 0 : len = eol - s;
12492 : 0 : printf ("%*s%.*s\n", ITEM_INDENT, "", len, s);
12493 [ # # ]: 0 : if (eol == NULL)
12494 : : break;
12495 : 0 : size -= eol + 1 - s;
12496 : 0 : s = eol + 1;
12497 : : }
12498 : :
12499 : : colno = WRAP_COLUMN;
12500 : : break;
12501 : :
12502 : : case 'h':
12503 : : break;
12504 : :
12505 : 0 : default:
12506 : 0 : error (0, 0, "XXX not handling format '%c' for %s",
12507 : 0 : item->format, item->name);
12508 : : break;
12509 : : }
12510 : :
12511 : : #undef TYPES
12512 : :
12513 : 0 : return colno;
12514 : : }
12515 : :
12516 : :
12517 : : /* Sort items by group, and by layout offset within each group. */
12518 : : static int
12519 : 0 : compare_core_items (const void *a, const void *b)
12520 : : {
12521 : 0 : const Ebl_Core_Item *const *p1 = a;
12522 : 0 : const Ebl_Core_Item *const *p2 = b;
12523 : 0 : const Ebl_Core_Item *item1 = *p1;
12524 : 0 : const Ebl_Core_Item *item2 = *p2;
12525 : :
12526 : 0 : return ((item1->group == item2->group ? 0
12527 [ # # ]: 0 : : strcmp (item1->group, item2->group))
12528 [ # # ]: 0 : ?: (int) item1->offset - (int) item2->offset);
12529 : : }
12530 : :
12531 : : /* Sort item groups by layout offset of the first item in the group. */
12532 : : static int
12533 : 0 : compare_core_item_groups (const void *a, const void *b)
12534 : : {
12535 : 0 : const Ebl_Core_Item *const *const *p1 = a;
12536 : 0 : const Ebl_Core_Item *const *const *p2 = b;
12537 : 0 : const Ebl_Core_Item *const *group1 = *p1;
12538 : 0 : const Ebl_Core_Item *const *group2 = *p2;
12539 : 0 : const Ebl_Core_Item *item1 = *group1;
12540 : 0 : const Ebl_Core_Item *item2 = *group2;
12541 : :
12542 : 0 : return (int) item1->offset - (int) item2->offset;
12543 : : }
12544 : :
12545 : : static unsigned int
12546 : 0 : handle_core_items (Elf *core, const void *desc, size_t descsz,
12547 : : const Ebl_Core_Item *items, size_t nitems)
12548 : 0 : {
12549 [ # # ]: 0 : if (nitems == 0)
12550 : : return 0;
12551 : 0 : unsigned int colno = 0;
12552 : :
12553 : : /* FORMAT '\n' makes sense to be present only as a single item as it
12554 : : processes all the data of a note. FORMATs 'b' and 'B' have a special case
12555 : : if present as a single item but they can be also processed with other
12556 : : items below. */
12557 [ # # # # ]: 0 : if (nitems == 1 && (items[0].format == '\n' || items[0].format == 'b'
12558 [ # # ]: 0 : || items[0].format == 'B'))
12559 : : {
12560 [ # # ]: 0 : assert (items[0].offset == 0);
12561 : 0 : size_t size = descsz;
12562 : 0 : colno = handle_core_item (core, items, desc, colno, &size);
12563 : : /* If SIZE is not zero here there is some remaining data. But we do not
12564 : : know how to process it anyway. */
12565 : 0 : return colno;
12566 : : }
12567 [ # # ]: 0 : for (size_t i = 0; i < nitems; ++i)
12568 [ # # ]: 0 : assert (items[i].format != '\n');
12569 : :
12570 : : /* Sort to collect the groups together. */
12571 : 0 : const Ebl_Core_Item *sorted_items[nitems];
12572 [ # # ]: 0 : for (size_t i = 0; i < nitems; ++i)
12573 : 0 : sorted_items[i] = &items[i];
12574 : 0 : qsort (sorted_items, nitems, sizeof sorted_items[0], &compare_core_items);
12575 : :
12576 : : /* Collect the unique groups and sort them. */
12577 : 0 : const Ebl_Core_Item **groups[nitems];
12578 : 0 : groups[0] = &sorted_items[0];
12579 : 0 : size_t ngroups = 1;
12580 [ # # ]: 0 : for (size_t i = 1; i < nitems; ++i)
12581 [ # # ]: 0 : if (sorted_items[i]->group != sorted_items[i - 1]->group
12582 [ # # ]: 0 : && strcmp (sorted_items[i]->group, sorted_items[i - 1]->group))
12583 : 0 : groups[ngroups++] = &sorted_items[i];
12584 : 0 : qsort (groups, ngroups, sizeof groups[0], &compare_core_item_groups);
12585 : :
12586 : : /* Write out all the groups. */
12587 : 0 : const void *last = desc;
12588 : 0 : do
12589 : : {
12590 [ # # ]: 0 : for (size_t i = 0; i < ngroups; ++i)
12591 : : {
12592 : 0 : for (const Ebl_Core_Item **item = groups[i];
12593 : 0 : (item < &sorted_items[nitems]
12594 [ # # # # ]: 0 : && ((*item)->group == groups[i][0]->group
12595 [ # # ]: 0 : || !strcmp ((*item)->group, groups[i][0]->group)));
12596 : 0 : ++item)
12597 : 0 : colno = handle_core_item (core, *item, desc, colno, NULL);
12598 : :
12599 : : /* Force a line break at the end of the group. */
12600 : 0 : colno = WRAP_COLUMN;
12601 : : }
12602 : :
12603 [ # # ]: 0 : if (descsz == 0)
12604 : : break;
12605 : :
12606 : : /* This set of items consumed a certain amount of the note's data.
12607 : : If there is more data there, we have another unit of the same size.
12608 : : Loop to print that out too. */
12609 : 0 : const Ebl_Core_Item *item = &items[nitems - 1];
12610 : 0 : size_t eltsz = item->offset + gelf_fsize (core, item->type,
12611 : 0 : item->count ?: 1, EV_CURRENT);
12612 : :
12613 : 0 : int reps = -1;
12614 : 0 : do
12615 : : {
12616 : 0 : ++reps;
12617 : 0 : desc += eltsz;
12618 : 0 : descsz -= eltsz;
12619 : : }
12620 [ # # # # ]: 0 : while (descsz >= eltsz && !memcmp (desc, last, eltsz));
12621 : :
12622 [ # # ]: 0 : if (reps == 1)
12623 : : {
12624 : : /* For just one repeat, print it unabridged twice. */
12625 : 0 : desc -= eltsz;
12626 : 0 : descsz += eltsz;
12627 : : }
12628 [ # # ]: 0 : else if (reps > 1)
12629 : 0 : printf (_("\n%*s... <repeats %u more times> ..."),
12630 : : ITEM_INDENT, "", reps);
12631 : :
12632 : 0 : last = desc;
12633 : : }
12634 [ # # ]: 0 : while (descsz > 0);
12635 : :
12636 : : return colno;
12637 : : }
12638 : :
12639 : : static unsigned int
12640 : 0 : handle_core_register (Ebl *ebl, Elf *core, int maxregname,
12641 : : const Ebl_Register_Location *regloc, const void *desc,
12642 : : unsigned int colno)
12643 : : {
12644 [ # # ]: 0 : if (regloc->bits % 8 != 0)
12645 : : {
12646 : 0 : error (0, 0, "Warning: Cannot handle register with %" PRIu8 "bits\n",
12647 : : regloc->bits);
12648 : 0 : return colno;
12649 : : }
12650 : :
12651 : 0 : desc += regloc->offset;
12652 : :
12653 [ # # ]: 0 : for (int reg = regloc->regno; reg < regloc->regno + regloc->count; ++reg)
12654 : : {
12655 : 0 : char name[REGNAMESZ];
12656 : 0 : int bits;
12657 : 0 : int type;
12658 : 0 : register_info (ebl, reg, regloc, name, &bits, &type);
12659 : :
12660 : : #define TYPES \
12661 : : BITS (8, BYTE, "%4" PRId8, "0x%.2" PRIx8); \
12662 : : BITS (16, HALF, "%6" PRId16, "0x%.4" PRIx16); \
12663 : : BITS (32, WORD, "%11" PRId32, " 0x%.8" PRIx32); \
12664 : : BITS (64, XWORD, "%20" PRId64, " 0x%.16" PRIx64)
12665 : :
12666 : : #define BITS(bits, xtype, sfmt, ufmt) \
12667 : : uint##bits##_t b##bits; int##bits##_t b##bits##s
12668 : 0 : union { TYPES; uint64_t b128[2]; } value;
12669 : : #undef BITS
12670 : :
12671 [ # # ]: 0 : switch (type)
12672 : : {
12673 : 0 : case DW_ATE_unsigned:
12674 : : case DW_ATE_signed:
12675 : : case DW_ATE_address:
12676 [ # # # # : 0 : switch (bits)
# # ]
12677 : : {
12678 : : #define BITS(bits, xtype, sfmt, ufmt) \
12679 : : case bits: \
12680 : : desc = convert (core, ELF_T_##xtype, 1, &value, desc, 0); \
12681 : : if (type == DW_ATE_signed) \
12682 : : colno = print_core_item (colno, ' ', WRAP_COLUMN, \
12683 : : maxregname, name, \
12684 : : sfmt, value.b##bits##s); \
12685 : : else \
12686 : : colno = print_core_item (colno, ' ', WRAP_COLUMN, \
12687 : : maxregname, name, \
12688 : : ufmt, value.b##bits); \
12689 : : break
12690 : :
12691 [ # # # # : 0 : TYPES;
# # # # ]
12692 : :
12693 : 0 : case 128:
12694 [ # # ]: 0 : assert (type == DW_ATE_unsigned);
12695 : 0 : desc = convert (core, ELF_T_XWORD, 2, &value, desc, 0);
12696 : 0 : int be = elf_getident (core, NULL)[EI_DATA] == ELFDATA2MSB;
12697 : 0 : colno = print_core_item (colno, ' ', WRAP_COLUMN,
12698 : : maxregname, name,
12699 : : "0x%.16" PRIx64 "%.16" PRIx64,
12700 : 0 : value.b128[!be], value.b128[be]);
12701 : 0 : break;
12702 : :
12703 : 0 : default:
12704 : 0 : abort ();
12705 : : #undef BITS
12706 : : }
12707 : : break;
12708 : :
12709 : 0 : default:
12710 : : /* Print each byte in hex, the whole thing in native byte order. */
12711 [ # # ]: 0 : assert (bits % 8 == 0);
12712 : 0 : const uint8_t *bytes = desc;
12713 : 0 : desc += bits / 8;
12714 : 0 : char hex[bits / 4 + 1];
12715 : 0 : hex[bits / 4] = '\0';
12716 : 0 : int incr = 1;
12717 [ # # ]: 0 : if (elf_getident (core, NULL)[EI_DATA] == ELFDATA2LSB)
12718 : : {
12719 : 0 : bytes += bits / 8 - 1;
12720 : 0 : incr = -1;
12721 : : }
12722 : 0 : size_t idx = 0;
12723 [ # # ]: 0 : for (char *h = hex; bits > 0; bits -= 8, idx += incr)
12724 : : {
12725 : 0 : *h++ = "0123456789abcdef"[bytes[idx] >> 4];
12726 : 0 : *h++ = "0123456789abcdef"[bytes[idx] & 0xf];
12727 : : }
12728 : 0 : colno = print_core_item (colno, ' ', WRAP_COLUMN,
12729 : : maxregname, name, "0x%s", hex);
12730 : 0 : break;
12731 : : }
12732 : 0 : desc += regloc->pad;
12733 : :
12734 : : #undef TYPES
12735 : : }
12736 : :
12737 : : return colno;
12738 : : }
12739 : :
12740 : :
12741 : : struct register_info
12742 : : {
12743 : : const Ebl_Register_Location *regloc;
12744 : : const char *set;
12745 : : char name[REGNAMESZ];
12746 : : int regno;
12747 : : int bits;
12748 : : int type;
12749 : : };
12750 : :
12751 : : static int
12752 : 0 : register_bitpos (const struct register_info *r)
12753 : : {
12754 : 0 : return (r->regloc->offset * 8
12755 : 0 : + ((r->regno - r->regloc->regno)
12756 : 0 : * (r->regloc->bits + r->regloc->pad * 8)));
12757 : : }
12758 : :
12759 : : static int
12760 : 0 : compare_sets_by_info (const struct register_info *r1,
12761 : : const struct register_info *r2)
12762 : : {
12763 : 0 : return ((int) r2->bits - (int) r1->bits
12764 [ # # ]: 0 : ?: register_bitpos (r1) - register_bitpos (r2));
12765 : : }
12766 : :
12767 : : /* Sort registers by set, and by size and layout offset within each set. */
12768 : : static int
12769 : 0 : compare_registers (const void *a, const void *b)
12770 : : {
12771 : 0 : const struct register_info *r1 = a;
12772 : 0 : const struct register_info *r2 = b;
12773 : :
12774 : : /* Unused elements sort last. */
12775 [ # # ]: 0 : if (r1->regloc == NULL)
12776 : 0 : return r2->regloc == NULL ? 0 : 1;
12777 [ # # ]: 0 : if (r2->regloc == NULL)
12778 : : return -1;
12779 : :
12780 [ # # ]: 0 : return ((r1->set == r2->set ? 0 : strcmp (r1->set, r2->set))
12781 [ # # ]: 0 : ?: compare_sets_by_info (r1, r2));
12782 : : }
12783 : :
12784 : : /* Sort register sets by layout offset of the first register in the set. */
12785 : : static int
12786 : 0 : compare_register_sets (const void *a, const void *b)
12787 : : {
12788 : 0 : const struct register_info *const *p1 = a;
12789 : 0 : const struct register_info *const *p2 = b;
12790 : 0 : return compare_sets_by_info (*p1, *p2);
12791 : : }
12792 : :
12793 : : static inline bool
12794 : 0 : same_set (const struct register_info *a,
12795 : : const struct register_info *b,
12796 : : const struct register_info *regs,
12797 : : size_t maxnreg)
12798 : : {
12799 [ # # ]: 0 : return (a < ®s[maxnreg] && a->regloc != NULL
12800 [ # # # # ]: 0 : && b < ®s[maxnreg] && b->regloc != NULL
12801 [ # # ]: 0 : && a->bits == b->bits
12802 [ # # # # : 0 : && (a->set == b->set || !strcmp (a->set, b->set)));
# # ]
12803 : : }
12804 : :
12805 : : static unsigned int
12806 : 0 : handle_core_registers (Ebl *ebl, Elf *core, const void *desc,
12807 : : const Ebl_Register_Location *reglocs, size_t nregloc)
12808 : 0 : {
12809 [ # # ]: 0 : if (nregloc == 0)
12810 : : return 0;
12811 : :
12812 : 0 : ssize_t maxnreg = ebl_register_info (ebl, 0, NULL, 0, NULL, NULL, NULL, NULL);
12813 [ # # ]: 0 : if (maxnreg <= 0)
12814 : : {
12815 [ # # ]: 0 : for (size_t i = 0; i < nregloc; ++i)
12816 : 0 : if (maxnreg < reglocs[i].regno + reglocs[i].count)
12817 : : maxnreg = reglocs[i].regno + reglocs[i].count;
12818 [ # # ]: 0 : assert (maxnreg > 0);
12819 : : }
12820 : :
12821 : 0 : struct register_info regs[maxnreg];
12822 : 0 : memset (regs, 0, sizeof regs);
12823 : :
12824 : : /* Sort to collect the sets together. */
12825 : 0 : int maxreg = 0;
12826 [ # # ]: 0 : for (size_t i = 0; i < nregloc; ++i)
12827 : 0 : for (int reg = reglocs[i].regno;
12828 [ # # ]: 0 : reg < reglocs[i].regno + reglocs[i].count;
12829 : 0 : ++reg)
12830 : : {
12831 [ # # ]: 0 : assert (reg < maxnreg);
12832 : 0 : if (reg > maxreg)
12833 : : maxreg = reg;
12834 : 0 : struct register_info *info = ®s[reg];
12835 : 0 : info->regloc = ®locs[i];
12836 : 0 : info->regno = reg;
12837 : 0 : info->set = register_info (ebl, reg, ®locs[i],
12838 : 0 : info->name, &info->bits, &info->type);
12839 : : }
12840 : 0 : qsort (regs, maxreg + 1, sizeof regs[0], &compare_registers);
12841 : :
12842 : : /* Collect the unique sets and sort them. */
12843 : 0 : struct register_info *sets[maxreg + 1];
12844 : 0 : sets[0] = ®s[0];
12845 : 0 : size_t nsets = 1;
12846 [ # # ]: 0 : for (int i = 1; i <= maxreg; ++i)
12847 [ # # ]: 0 : if (regs[i].regloc != NULL
12848 [ # # ]: 0 : && !same_set (®s[i], ®s[i - 1], regs, maxnreg))
12849 : 0 : sets[nsets++] = ®s[i];
12850 : 0 : qsort (sets, nsets, sizeof sets[0], &compare_register_sets);
12851 : :
12852 : : /* Write out all the sets. */
12853 : 0 : unsigned int colno = 0;
12854 [ # # ]: 0 : for (size_t i = 0; i < nsets; ++i)
12855 : : {
12856 : : /* Find the longest name of a register in this set. */
12857 : 0 : size_t maxname = 0;
12858 : 0 : const struct register_info *end;
12859 [ # # ]: 0 : for (end = sets[i]; same_set (sets[i], end, regs, maxnreg); ++end)
12860 : : {
12861 : 0 : size_t len = strlen (end->name);
12862 : 0 : if (len > maxname)
12863 : : maxname = len;
12864 : : }
12865 : :
12866 : : for (const struct register_info *reg = sets[i];
12867 [ # # ]: 0 : reg < end;
12868 : 0 : reg += reg->regloc->count ?: 1)
12869 [ # # ]: 0 : colno = handle_core_register (ebl, core, maxname,
12870 : 0 : reg->regloc, desc, colno);
12871 : :
12872 : : /* Force a line break at the end of the group. */
12873 : 0 : colno = WRAP_COLUMN;
12874 : : }
12875 : :
12876 : : return colno;
12877 : : }
12878 : :
12879 : : static void
12880 : 0 : handle_auxv_note (Ebl *ebl, Elf *core, GElf_Word descsz, GElf_Off desc_pos)
12881 : : {
12882 : 0 : Elf_Data *data = elf_getdata_rawchunk (core, desc_pos, descsz, ELF_T_AUXV);
12883 [ # # ]: 0 : if (data == NULL)
12884 : 0 : elf_error:
12885 : 0 : error_exit (0, _("cannot convert core note data: %s"), elf_errmsg (-1));
12886 : :
12887 : 0 : const size_t nauxv = descsz / gelf_fsize (core, ELF_T_AUXV, 1, EV_CURRENT);
12888 [ # # ]: 0 : for (size_t i = 0; i < nauxv; ++i)
12889 : : {
12890 : 0 : GElf_auxv_t av_mem;
12891 : 0 : GElf_auxv_t *av = gelf_getauxv (data, i, &av_mem);
12892 [ # # ]: 0 : if (av == NULL)
12893 : 0 : goto elf_error;
12894 : :
12895 : 0 : const char *name;
12896 : 0 : const char *fmt;
12897 [ # # ]: 0 : if (ebl_auxv_info (ebl, av->a_type, &name, &fmt) == 0)
12898 : : {
12899 : : /* Unknown type. */
12900 [ # # ]: 0 : if (av->a_un.a_val == 0)
12901 : 0 : printf (" %" PRIu64 "\n", av->a_type);
12902 : : else
12903 : 0 : printf (" %" PRIu64 ": %#" PRIx64 "\n",
12904 : : av->a_type, av->a_un.a_val);
12905 : : }
12906 : : else
12907 [ # # # # : 0 : switch (fmt[0])
# # ]
12908 : : {
12909 : 0 : case '\0': /* Normally zero. */
12910 [ # # ]: 0 : if (av->a_un.a_val == 0)
12911 : : {
12912 : 0 : printf (" %s\n", name);
12913 : : break;
12914 : : }
12915 : 0 : FALLTHROUGH;
12916 : : case 'x': /* hex */
12917 : : case 'p': /* address */
12918 : : case 's': /* address of string */
12919 : 0 : printf (" %s: %#" PRIx64 "\n", name, av->a_un.a_val);
12920 : : break;
12921 : 0 : case 'u':
12922 : 0 : printf (" %s: %" PRIu64 "\n", name, av->a_un.a_val);
12923 : : break;
12924 : 0 : case 'd':
12925 : 0 : printf (" %s: %" PRId64 "\n", name, av->a_un.a_val);
12926 : : break;
12927 : :
12928 : 0 : case 'b':
12929 : 0 : printf (" %s: %#" PRIx64 " ", name, av->a_un.a_val);
12930 : 0 : GElf_Xword bit = 1;
12931 : 0 : const char *pfx = "<";
12932 [ # # ]: 0 : for (const char *p = fmt + 1; *p != 0; p = strchr (p, '\0') + 1)
12933 : : {
12934 [ # # ]: 0 : if (av->a_un.a_val & bit)
12935 : : {
12936 : 0 : printf ("%s%s", pfx, p);
12937 : 0 : pfx = " ";
12938 : : }
12939 : 0 : bit <<= 1;
12940 : : }
12941 : 0 : printf (">\n");
12942 : : break;
12943 : :
12944 : 0 : default:
12945 : 0 : abort ();
12946 : : }
12947 : : }
12948 : 0 : }
12949 : :
12950 : : static bool
12951 : 0 : buf_has_data (unsigned char const *ptr, unsigned char const *end, size_t sz)
12952 : : {
12953 [ # # # # ]: 0 : return ptr < end && (size_t) (end - ptr) >= sz;
12954 : : }
12955 : :
12956 : : static bool
12957 : 0 : buf_read_int (Elf *core, unsigned char const **ptrp, unsigned char const *end,
12958 : : int *retp)
12959 : : {
12960 [ # # # # ]: 0 : if (! buf_has_data (*ptrp, end, 4))
12961 : : return false;
12962 : :
12963 : 0 : *ptrp = convert (core, ELF_T_WORD, 1, retp, *ptrp, 4);
12964 : 0 : return true;
12965 : : }
12966 : :
12967 : : static bool
12968 : 0 : buf_read_ulong (Elf *core, unsigned char const **ptrp, unsigned char const *end,
12969 : : uint64_t *retp)
12970 : : {
12971 : 0 : size_t sz = gelf_fsize (core, ELF_T_ADDR, 1, EV_CURRENT);
12972 [ # # # # ]: 0 : if (! buf_has_data (*ptrp, end, sz))
12973 : : return false;
12974 : :
12975 : 0 : union
12976 : : {
12977 : : uint64_t u64;
12978 : : uint32_t u32;
12979 : : } u;
12980 : :
12981 : 0 : *ptrp = convert (core, ELF_T_ADDR, 1, &u, *ptrp, sz);
12982 : :
12983 [ # # ]: 0 : if (sz == 4)
12984 : 0 : *retp = u.u32;
12985 : : else
12986 : 0 : *retp = u.u64;
12987 : : return true;
12988 : : }
12989 : :
12990 : : static void
12991 : 0 : handle_siginfo_note (Elf *core, GElf_Word descsz, GElf_Off desc_pos)
12992 : : {
12993 : 0 : Elf_Data *data = elf_getdata_rawchunk (core, desc_pos, descsz, ELF_T_BYTE);
12994 [ # # ]: 0 : if (data == NULL)
12995 : 0 : error_exit (0, _("cannot convert core note data: %s"), elf_errmsg (-1));
12996 : :
12997 : 0 : unsigned char const *ptr = data->d_buf;
12998 : 0 : unsigned char const *const end = data->d_buf + data->d_size;
12999 : :
13000 : : /* Siginfo head is three ints: signal number, error number, origin
13001 : : code. */
13002 : 0 : int si_signo, si_errno, si_code;
13003 [ # # ]: 0 : if (! buf_read_int (core, &ptr, end, &si_signo)
13004 [ # # ]: 0 : || ! buf_read_int (core, &ptr, end, &si_errno)
13005 [ # # ]: 0 : || ! buf_read_int (core, &ptr, end, &si_code))
13006 : : {
13007 : 0 : fail:
13008 : 0 : printf (" Not enough data in NT_SIGINFO note.\n");
13009 : 0 : return;
13010 : : }
13011 : :
13012 : : /* Next is a pointer-aligned union of structures. On 64-bit
13013 : : machines, that implies a word of padding. */
13014 [ # # ]: 0 : if (gelf_getclass (core) == ELFCLASS64)
13015 : 0 : ptr += 4;
13016 : :
13017 : 0 : printf (" si_signo: %d, si_errno: %d, si_code: %d\n",
13018 : : si_signo, si_errno, si_code);
13019 : :
13020 [ # # ]: 0 : if (si_code > 0)
13021 [ # # ]: 0 : switch (si_signo)
13022 : : {
13023 : 0 : case CORE_SIGILL:
13024 : : case CORE_SIGFPE:
13025 : : case CORE_SIGSEGV:
13026 : : case CORE_SIGBUS:
13027 : : {
13028 : 0 : uint64_t addr;
13029 [ # # ]: 0 : if (! buf_read_ulong (core, &ptr, end, &addr))
13030 : 0 : goto fail;
13031 : 0 : printf (" fault address: %#" PRIx64 "\n", addr);
13032 : 0 : break;
13033 : : }
13034 : : default:
13035 : : ;
13036 : : }
13037 [ # # ]: 0 : else if (si_code == CORE_SI_USER)
13038 : : {
13039 : 0 : int pid, uid;
13040 [ # # ]: 0 : if (! buf_read_int (core, &ptr, end, &pid)
13041 [ # # ]: 0 : || ! buf_read_int (core, &ptr, end, &uid))
13042 : 0 : goto fail;
13043 : 0 : printf (" sender PID: %d, sender UID: %d\n", pid, uid);
13044 : : }
13045 : : }
13046 : :
13047 : : static void
13048 : 0 : handle_file_note (Elf *core, GElf_Word descsz, GElf_Off desc_pos)
13049 : : {
13050 : 0 : Elf_Data *data = elf_getdata_rawchunk (core, desc_pos, descsz, ELF_T_BYTE);
13051 [ # # ]: 0 : if (data == NULL)
13052 : 0 : error_exit (0, _("cannot convert core note data: %s"), elf_errmsg (-1));
13053 : :
13054 : 0 : unsigned char const *ptr = data->d_buf;
13055 : 0 : unsigned char const *const end = data->d_buf + data->d_size;
13056 : :
13057 : 0 : uint64_t count, page_size;
13058 [ # # ]: 0 : if (! buf_read_ulong (core, &ptr, end, &count)
13059 [ # # ]: 0 : || ! buf_read_ulong (core, &ptr, end, &page_size))
13060 : : {
13061 : 0 : fail:
13062 : 0 : printf (" Not enough data in NT_FILE note.\n");
13063 : 0 : return;
13064 : : }
13065 : :
13066 : 0 : size_t addrsize = gelf_fsize (core, ELF_T_ADDR, 1, EV_CURRENT);
13067 : 0 : uint64_t maxcount = (size_t) (end - ptr) / (3 * addrsize);
13068 [ # # ]: 0 : if (count > maxcount)
13069 : 0 : goto fail;
13070 : :
13071 : : /* Where file names are stored. */
13072 : 0 : unsigned char const *const fstart = ptr + 3 * count * addrsize;
13073 : 0 : char const *fptr = (char *) fstart;
13074 : :
13075 : 0 : printf (" %" PRId64 " files:\n", count);
13076 [ # # ]: 0 : for (uint64_t i = 0; i < count; ++i)
13077 : : {
13078 : 0 : uint64_t mstart, mend, moffset;
13079 [ # # ]: 0 : if (! buf_read_ulong (core, &ptr, fstart, &mstart)
13080 [ # # ]: 0 : || ! buf_read_ulong (core, &ptr, fstart, &mend)
13081 [ # # ]: 0 : || ! buf_read_ulong (core, &ptr, fstart, &moffset))
13082 : 0 : goto fail;
13083 : :
13084 : 0 : const char *fnext = memchr (fptr, '\0', (char *) end - fptr);
13085 [ # # ]: 0 : if (fnext == NULL)
13086 : 0 : goto fail;
13087 : :
13088 : 0 : int ct = printf (" %08" PRIx64 "-%08" PRIx64
13089 : : " %08" PRIx64 " %" PRId64,
13090 : : mstart, mend, moffset * page_size, mend - mstart);
13091 [ # # ]: 0 : printf ("%*s%s\n", ct > 50 ? 3 : 53 - ct, "", fptr);
13092 : :
13093 : 0 : fptr = fnext + 1;
13094 : : }
13095 : : }
13096 : :
13097 : : static void
13098 : 0 : handle_core_note (Ebl *ebl, const GElf_Nhdr *nhdr,
13099 : : const char *name, const void *desc)
13100 : : {
13101 : 0 : GElf_Word regs_offset;
13102 : 0 : size_t nregloc;
13103 : 0 : const Ebl_Register_Location *reglocs;
13104 : 0 : size_t nitems;
13105 : 0 : const Ebl_Core_Item *items;
13106 : :
13107 [ # # ]: 0 : if (! ebl_core_note (ebl, nhdr, name, desc,
13108 : : ®s_offset, &nregloc, ®locs, &nitems, &items))
13109 : 0 : return;
13110 : :
13111 : : /* Pass 0 for DESCSZ when there are registers in the note,
13112 : : so that the ITEMS array does not describe the whole thing.
13113 : : For non-register notes, the actual descsz might be a multiple
13114 : : of the unit size, not just exactly the unit size. */
13115 : 0 : unsigned int colno = handle_core_items (ebl->elf, desc,
13116 [ # # ]: 0 : nregloc == 0 ? nhdr->n_descsz : 0,
13117 : : items, nitems);
13118 [ # # ]: 0 : if (colno != 0)
13119 : 0 : putchar ('\n');
13120 : :
13121 : 0 : colno = handle_core_registers (ebl, ebl->elf, desc + regs_offset,
13122 : : reglocs, nregloc);
13123 [ # # ]: 0 : if (colno != 0)
13124 : 0 : putchar ('\n');
13125 : : }
13126 : :
13127 : : static void
13128 : 0 : handle_notes_data (Ebl *ebl, const GElf_Ehdr *ehdr,
13129 : : GElf_Off start, Elf_Data *data)
13130 : : {
13131 : 0 : fputs (_(" Owner Data size Type\n"), stdout);
13132 : :
13133 [ # # ]: 0 : if (data == NULL)
13134 : 0 : goto bad_note;
13135 : :
13136 : : size_t offset = 0;
13137 : : GElf_Nhdr nhdr;
13138 : : size_t name_offset;
13139 : : size_t desc_offset;
13140 : 0 : while (offset < data->d_size
13141 [ # # # # ]: 0 : && (offset = gelf_getnote (data, offset,
13142 : : &nhdr, &name_offset, &desc_offset)) > 0)
13143 : : {
13144 [ # # ]: 0 : const char *name = nhdr.n_namesz == 0 ? "" : data->d_buf + name_offset;
13145 : 0 : const char *desc = data->d_buf + desc_offset;
13146 : :
13147 : : /* GNU Build Attributes are weird, they store most of their data
13148 : : into the owner name field. Extract just the owner name
13149 : : prefix here, then use the rest later as data. */
13150 : 0 : bool is_gnu_build_attr
13151 : 0 : = startswith (name, ELF_NOTE_GNU_BUILD_ATTRIBUTE_PREFIX);
13152 : 0 : const char *print_name = (is_gnu_build_attr
13153 [ # # ]: 0 : ? ELF_NOTE_GNU_BUILD_ATTRIBUTE_PREFIX : name);
13154 : 0 : size_t print_namesz = (is_gnu_build_attr
13155 : 0 : ? strlen (print_name) : nhdr.n_namesz);
13156 : :
13157 : 0 : char buf[100];
13158 : 0 : char buf2[100];
13159 : 0 : printf (_(" %-13.*s %9" PRId32 " %s\n"),
13160 : : (int) print_namesz, print_name, nhdr.n_descsz,
13161 [ # # ]: 0 : ehdr->e_type == ET_CORE
13162 : 0 : ? ebl_core_note_type_name (ebl, nhdr.n_type,
13163 : : buf, sizeof (buf))
13164 : 0 : : ebl_object_note_type_name (ebl, name, nhdr.n_type,
13165 : : nhdr.n_descsz,
13166 : : buf2, sizeof (buf2)));
13167 : :
13168 : : /* Filter out invalid entries. */
13169 : 0 : if (memchr (name, '\0', nhdr.n_namesz) != NULL
13170 : : /* XXX For now help broken Linux kernels. */
13171 : : || 1)
13172 : : {
13173 [ # # ]: 0 : if (ehdr->e_type == ET_CORE)
13174 : : {
13175 [ # # ]: 0 : if (nhdr.n_type == NT_AUXV
13176 [ # # ]: 0 : && (nhdr.n_namesz == 4 /* Broken old Linux kernels. */
13177 [ # # # # ]: 0 : || (nhdr.n_namesz == 5 && name[4] == '\0'))
13178 [ # # ]: 0 : && !memcmp (name, "CORE", 4))
13179 : 0 : handle_auxv_note (ebl, ebl->elf, nhdr.n_descsz,
13180 : : start + desc_offset);
13181 [ # # # # ]: 0 : else if (nhdr.n_namesz == 5 && strcmp (name, "CORE") == 0)
13182 [ # # # ]: 0 : switch (nhdr.n_type)
13183 : : {
13184 : 0 : case NT_SIGINFO:
13185 : 0 : handle_siginfo_note (ebl->elf, nhdr.n_descsz,
13186 : : start + desc_offset);
13187 : 0 : break;
13188 : :
13189 : 0 : case NT_FILE:
13190 : 0 : handle_file_note (ebl->elf, nhdr.n_descsz,
13191 : : start + desc_offset);
13192 : 0 : break;
13193 : :
13194 : 0 : default:
13195 : 0 : handle_core_note (ebl, &nhdr, name, desc);
13196 : : }
13197 : : else
13198 : 0 : handle_core_note (ebl, &nhdr, name, desc);
13199 : : }
13200 : : else
13201 : 0 : ebl_object_note (ebl, nhdr.n_namesz, name, nhdr.n_type,
13202 : : nhdr.n_descsz, desc);
13203 : : }
13204 : : }
13205 : :
13206 [ # # ]: 0 : if (offset == data->d_size)
13207 : 0 : return;
13208 : :
13209 : 0 : bad_note:
13210 : 0 : error (0, 0,
13211 : 0 : _("cannot get content of note: %s"),
13212 : 0 : data != NULL ? "garbage data" : elf_errmsg (-1));
13213 : : }
13214 : :
13215 : : static void
13216 : 0 : handle_notes (Ebl *ebl, GElf_Ehdr *ehdr)
13217 : : {
13218 : : /* If we have section headers, just look for SHT_NOTE sections.
13219 : : In a debuginfo file, the program headers are not reliable. */
13220 [ # # ]: 0 : if (shnum != 0)
13221 : : {
13222 : : /* Get the section header string table index. */
13223 : 0 : size_t shstrndx;
13224 [ # # ]: 0 : if (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0)
13225 : 0 : error_exit (0, _("cannot get section header string table index"));
13226 : :
13227 : : Elf_Scn *scn = NULL;
13228 [ # # ]: 0 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
13229 : : {
13230 : 0 : GElf_Shdr shdr_mem;
13231 : 0 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
13232 : :
13233 [ # # # # ]: 0 : if (shdr == NULL || shdr->sh_type != SHT_NOTE)
13234 : : /* Not what we are looking for. */
13235 : 0 : continue;
13236 : :
13237 [ # # ]: 0 : if (notes_section != NULL)
13238 : : {
13239 : 0 : char *sname = elf_strptr (ebl->elf, shstrndx, shdr->sh_name);
13240 [ # # # # ]: 0 : if (sname == NULL || strcmp (sname, notes_section) != 0)
13241 : 0 : continue;
13242 : : }
13243 : :
13244 : 0 : printf (_("\
13245 : : \nNote section [%2zu] '%s' of %" PRIu64 " bytes at offset %#0" PRIx64 ":\n"),
13246 : : elf_ndxscn (scn),
13247 : 0 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
13248 : : shdr->sh_size, shdr->sh_offset);
13249 : :
13250 : 0 : handle_notes_data (ebl, ehdr, shdr->sh_offset,
13251 : : elf_getdata (scn, NULL));
13252 : : }
13253 : 0 : return;
13254 : : }
13255 : :
13256 : : /* We have to look through the program header to find the note
13257 : : sections. There can be more than one. */
13258 [ # # ]: 0 : for (size_t cnt = 0; cnt < phnum; ++cnt)
13259 : : {
13260 : 0 : GElf_Phdr mem;
13261 : 0 : GElf_Phdr *phdr = gelf_getphdr (ebl->elf, cnt, &mem);
13262 : :
13263 [ # # # # ]: 0 : if (phdr == NULL || phdr->p_type != PT_NOTE)
13264 : : /* Not what we are looking for. */
13265 : 0 : continue;
13266 : :
13267 : 0 : printf (_("\
13268 : : \nNote segment of %" PRIu64 " bytes at offset %#0" PRIx64 ":\n"),
13269 : : phdr->p_filesz, phdr->p_offset);
13270 : :
13271 : 0 : handle_notes_data (ebl, ehdr, phdr->p_offset,
13272 : : elf_getdata_rawchunk (ebl->elf,
13273 : 0 : phdr->p_offset, phdr->p_filesz,
13274 [ # # ]: 0 : (phdr->p_align == 8
13275 : : ? ELF_T_NHDR8 : ELF_T_NHDR)));
13276 : : }
13277 : : }
13278 : :
13279 : :
13280 : : static void
13281 : 0 : hex_dump (const uint8_t *data, size_t len)
13282 : : {
13283 : 0 : size_t pos = 0;
13284 [ # # ]: 0 : while (pos < len)
13285 : : {
13286 : 0 : printf (" 0x%08zx ", pos);
13287 : :
13288 : 0 : const size_t chunk = MIN (len - pos, 16);
13289 : :
13290 [ # # ]: 0 : for (size_t i = 0; i < chunk; ++i)
13291 [ # # ]: 0 : if (i % 4 == 3)
13292 : 0 : printf ("%02x ", data[pos + i]);
13293 : : else
13294 : 0 : printf ("%02x", data[pos + i]);
13295 : :
13296 [ # # ]: 0 : if (chunk < 16)
13297 : 0 : printf ("%*s", (int) ((16 - chunk) * 2 + (16 - chunk + 3) / 4), "");
13298 : :
13299 [ # # ]: 0 : for (size_t i = 0; i < chunk; ++i)
13300 : : {
13301 : 0 : unsigned char b = data[pos + i];
13302 [ # # ]: 0 : printf ("%c", isprint (b) ? b : '.');
13303 : : }
13304 : :
13305 : 0 : putchar ('\n');
13306 : 0 : pos += chunk;
13307 : : }
13308 : 0 : }
13309 : :
13310 : : static void
13311 : 0 : dump_data_section (Elf_Scn *scn, const GElf_Shdr *shdr, const char *name)
13312 : : {
13313 [ # # # # ]: 0 : if (shdr->sh_size == 0 || shdr->sh_type == SHT_NOBITS)
13314 : 0 : printf (_("\nSection [%zu] '%s' has no data to dump.\n"),
13315 : : elf_ndxscn (scn), name);
13316 : : else
13317 : : {
13318 [ # # ]: 0 : if (print_decompress)
13319 : : {
13320 : : /* We try to decompress the section, but keep the old shdr around
13321 : : so we can show both the original shdr size and the uncompressed
13322 : : data size. */
13323 [ # # ]: 0 : if ((shdr->sh_flags & SHF_COMPRESSED) != 0)
13324 : : {
13325 [ # # ]: 0 : if (elf_compress (scn, 0, 0) < 0)
13326 : 0 : printf ("WARNING: %s [%zd]\n",
13327 : : _("Couldn't uncompress section"),
13328 : : elf_ndxscn (scn));
13329 : : }
13330 [ # # # # ]: 0 : else if (name && startswith (name, ".zdebug"))
13331 : : {
13332 [ # # ]: 0 : if (elf_compress_gnu (scn, 0, 0) < 0)
13333 : 0 : printf ("WARNING: %s [%zd]\n",
13334 : : _("Couldn't uncompress section"),
13335 : : elf_ndxscn (scn));
13336 : : }
13337 : : }
13338 : :
13339 : 0 : Elf_Data *data = elf_rawdata (scn, NULL);
13340 [ # # ]: 0 : if (data == NULL)
13341 : 0 : error (0, 0, _("cannot get data for section [%zu] '%s': %s"),
13342 : : elf_ndxscn (scn), name, elf_errmsg (-1));
13343 : : else
13344 : : {
13345 [ # # ]: 0 : if (data->d_size == shdr->sh_size)
13346 : 0 : printf (_("\nHex dump of section [%zu] '%s', %" PRIu64
13347 : : " bytes at offset %#0" PRIx64 ":\n"),
13348 : : elf_ndxscn (scn), name,
13349 : 0 : shdr->sh_size, shdr->sh_offset);
13350 : : else
13351 : 0 : printf (_("\nHex dump of section [%zu] '%s', %" PRIu64
13352 : : " bytes (%zd uncompressed) at offset %#0"
13353 : : PRIx64 ":\n"),
13354 : : elf_ndxscn (scn), name,
13355 : 0 : shdr->sh_size, data->d_size, shdr->sh_offset);
13356 : 0 : hex_dump (data->d_buf, data->d_size);
13357 : : }
13358 : : }
13359 : 0 : }
13360 : :
13361 : : static void
13362 : 0 : print_string_section (Elf_Scn *scn, const GElf_Shdr *shdr, const char *name)
13363 : : {
13364 [ # # # # ]: 0 : if (shdr->sh_size == 0 || shdr->sh_type == SHT_NOBITS)
13365 : 0 : printf (_("\nSection [%zu] '%s' has no strings to dump.\n"),
13366 : : elf_ndxscn (scn), name);
13367 : : else
13368 : : {
13369 [ # # ]: 0 : if (print_decompress)
13370 : : {
13371 : : /* We try to decompress the section, but keep the old shdr around
13372 : : so we can show both the original shdr size and the uncompressed
13373 : : data size. */
13374 [ # # ]: 0 : if ((shdr->sh_flags & SHF_COMPRESSED) != 0)
13375 : : {
13376 [ # # ]: 0 : if (elf_compress (scn, 0, 0) < 0)
13377 : 0 : printf ("WARNING: %s [%zd]\n",
13378 : : _("Couldn't uncompress section"),
13379 : : elf_ndxscn (scn));
13380 : : }
13381 [ # # # # ]: 0 : else if (name && startswith (name, ".zdebug"))
13382 : : {
13383 [ # # ]: 0 : if (elf_compress_gnu (scn, 0, 0) < 0)
13384 : 0 : printf ("WARNING: %s [%zd]\n",
13385 : : _("Couldn't uncompress section"),
13386 : : elf_ndxscn (scn));
13387 : : }
13388 : : }
13389 : :
13390 : 0 : Elf_Data *data = elf_rawdata (scn, NULL);
13391 [ # # ]: 0 : if (data == NULL)
13392 : 0 : error (0, 0, _("cannot get data for section [%zu] '%s': %s"),
13393 : : elf_ndxscn (scn), name, elf_errmsg (-1));
13394 : : else
13395 : : {
13396 [ # # ]: 0 : if (data->d_size == shdr->sh_size)
13397 : 0 : printf (_("\nString section [%zu] '%s' contains %" PRIu64
13398 : : " bytes at offset %#0" PRIx64 ":\n"),
13399 : : elf_ndxscn (scn), name,
13400 : 0 : shdr->sh_size, shdr->sh_offset);
13401 : : else
13402 : 0 : printf (_("\nString section [%zu] '%s' contains %" PRIu64
13403 : : " bytes (%zd uncompressed) at offset %#0"
13404 : : PRIx64 ":\n"),
13405 : : elf_ndxscn (scn), name,
13406 : 0 : shdr->sh_size, data->d_size, shdr->sh_offset);
13407 : :
13408 : 0 : const char *start = data->d_buf;
13409 : 0 : const char *const limit = start + data->d_size;
13410 : 0 : do
13411 : : {
13412 : 0 : const char *end = memchr (start, '\0', limit - start);
13413 : 0 : const size_t pos = start - (const char *) data->d_buf;
13414 [ # # ]: 0 : if (unlikely (end == NULL))
13415 : : {
13416 : 0 : printf (" [%6zx]- %.*s\n",
13417 : : pos, (int) (limit - start), start);
13418 : : break;
13419 : : }
13420 : 0 : printf (" [%6zx] %s\n", pos, start);
13421 : 0 : start = end + 1;
13422 [ # # ]: 0 : } while (start < limit);
13423 : : }
13424 : : }
13425 : 0 : }
13426 : :
13427 : : static void
13428 : 0 : for_each_section_argument (Elf *elf, const struct section_argument *list,
13429 : : void (*dump) (Elf_Scn *scn, const GElf_Shdr *shdr,
13430 : : const char *name))
13431 : : {
13432 : : /* Get the section header string table index. */
13433 : 0 : size_t shstrndx;
13434 [ # # ]: 0 : if (elf_getshdrstrndx (elf, &shstrndx) < 0)
13435 : 0 : error_exit (0, _("cannot get section header string table index"));
13436 : :
13437 [ # # ]: 0 : for (const struct section_argument *a = list; a != NULL; a = a->next)
13438 : : {
13439 : 0 : Elf_Scn *scn;
13440 : 0 : GElf_Shdr shdr_mem;
13441 : 0 : const char *name = NULL;
13442 : :
13443 : 0 : char *endp = NULL;
13444 : 0 : unsigned long int shndx = strtoul (a->arg, &endp, 0);
13445 [ # # # # ]: 0 : if (endp != a->arg && *endp == '\0')
13446 : : {
13447 : 0 : scn = elf_getscn (elf, shndx);
13448 [ # # ]: 0 : if (scn == NULL)
13449 : : {
13450 : 0 : error (0, 0, _("\nsection [%lu] does not exist"), shndx);
13451 : 0 : continue;
13452 : : }
13453 : :
13454 [ # # ]: 0 : if (gelf_getshdr (scn, &shdr_mem) == NULL)
13455 : 0 : error_exit (0, _("cannot get section header: %s"),
13456 : : elf_errmsg (-1));
13457 : 0 : name = elf_strptr (elf, shstrndx, shdr_mem.sh_name);
13458 : 0 : (*dump) (scn, &shdr_mem, name);
13459 : : }
13460 : : else
13461 : : {
13462 : : /* Need to look up the section by name. */
13463 : : scn = NULL;
13464 : : bool found = false;
13465 [ # # ]: 0 : while ((scn = elf_nextscn (elf, scn)) != NULL)
13466 : : {
13467 [ # # ]: 0 : if (gelf_getshdr (scn, &shdr_mem) == NULL)
13468 : 0 : continue;
13469 : 0 : name = elf_strptr (elf, shstrndx, shdr_mem.sh_name);
13470 [ # # ]: 0 : if (name == NULL)
13471 : 0 : continue;
13472 [ # # ]: 0 : if (!strcmp (name, a->arg))
13473 : : {
13474 : 0 : found = true;
13475 : 0 : (*dump) (scn, &shdr_mem, name);
13476 : : }
13477 : : }
13478 : :
13479 [ # # # # ]: 0 : if (unlikely (!found) && !a->implicit)
13480 : 0 : error (0, 0, _("\nsection '%s' does not exist"), a->arg);
13481 : : }
13482 : : }
13483 : 0 : }
13484 : :
13485 : : static void
13486 : 0 : dump_data (Ebl *ebl)
13487 : : {
13488 : 0 : for_each_section_argument (ebl->elf, dump_data_sections, &dump_data_section);
13489 : 0 : }
13490 : :
13491 : : static void
13492 : 0 : dump_strings (Ebl *ebl)
13493 : : {
13494 : 0 : for_each_section_argument (ebl->elf, string_sections, &print_string_section);
13495 : 0 : }
13496 : :
13497 : : static void
13498 : 0 : print_strings (Ebl *ebl)
13499 : : {
13500 : : /* Get the section header string table index. */
13501 : 0 : size_t shstrndx;
13502 [ # # ]: 0 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
13503 : 0 : error_exit (0, _("cannot get section header string table index"));
13504 : :
13505 : : Elf_Scn *scn;
13506 : : GElf_Shdr shdr_mem;
13507 : : const char *name;
13508 : : scn = NULL;
13509 [ # # ]: 0 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
13510 : : {
13511 [ # # ]: 0 : if (gelf_getshdr (scn, &shdr_mem) == NULL)
13512 : 0 : continue;
13513 : :
13514 [ # # ]: 0 : if (shdr_mem.sh_type != SHT_PROGBITS
13515 [ # # ]: 0 : || !(shdr_mem.sh_flags & SHF_STRINGS))
13516 : 0 : continue;
13517 : :
13518 : 0 : name = elf_strptr (ebl->elf, shstrndx, shdr_mem.sh_name);
13519 [ # # ]: 0 : if (name == NULL)
13520 : 0 : continue;
13521 : :
13522 : 0 : print_string_section (scn, &shdr_mem, name);
13523 : : }
13524 : 0 : }
13525 : :
13526 : : static void
13527 : 0 : dump_archive_index (Elf *elf, const char *fname)
13528 : : {
13529 : 0 : size_t narsym;
13530 : 0 : const Elf_Arsym *arsym = elf_getarsym (elf, &narsym);
13531 [ # # ]: 0 : if (arsym == NULL)
13532 : : {
13533 : 0 : int result = elf_errno ();
13534 [ # # ]: 0 : if (unlikely (result != ELF_E_NO_INDEX))
13535 : 0 : error_exit (0, _("cannot get symbol index of archive '%s': %s"),
13536 : : fname, elf_errmsg (result));
13537 : : else
13538 : 0 : printf (_("\nArchive '%s' has no symbol index\n"), fname);
13539 : 0 : return;
13540 : : }
13541 : :
13542 : 0 : printf (_("\nIndex of archive '%s' has %zu entries:\n"),
13543 : : fname, narsym);
13544 : :
13545 : 0 : size_t as_off = 0;
13546 [ # # ]: 0 : for (const Elf_Arsym *s = arsym; s < &arsym[narsym - 1]; ++s)
13547 : : {
13548 [ # # ]: 0 : if (s->as_off != as_off)
13549 : : {
13550 : 0 : as_off = s->as_off;
13551 : :
13552 : 0 : Elf *subelf = NULL;
13553 [ # # ]: 0 : if (unlikely (elf_rand (elf, as_off) == 0)
13554 [ # # ]: 0 : || unlikely ((subelf = elf_begin (-1, ELF_C_READ_MMAP, elf))
13555 : : == NULL))
13556 : : #if __GLIBC__ < 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ < 7)
13557 : : while (1)
13558 : : #endif
13559 : 0 : error_exit (0,
13560 : : _("cannot extract member at offset %zu in '%s': %s"),
13561 : : as_off, fname, elf_errmsg (-1));
13562 : :
13563 : 0 : const Elf_Arhdr *h = elf_getarhdr (subelf);
13564 : :
13565 : 0 : printf (_("Archive member '%s' contains:\n"), h->ar_name);
13566 : :
13567 : 0 : elf_end (subelf);
13568 : : }
13569 : :
13570 : 0 : printf ("\t%s\n", s->as_name);
13571 : : }
13572 : : }
13573 : :
13574 : : #include "debugpred.h"
|