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 : 1828 : cleanup_list (struct section_argument *list)
373 : : {
374 [ + + + + ]: 2502 : while (list != NULL)
375 : : {
376 : 674 : struct section_argument *a = list;
377 : 674 : list = a->next;
378 : 674 : free (a);
379 : : }
380 : : }
381 : :
382 : : int
383 : 914 : main (int argc, char *argv[])
384 : : {
385 : : /* We use no threads here which can interfere with handling a stream. */
386 : 914 : (void) __fsetlocking (stdout, FSETLOCKING_BYCALLER);
387 : :
388 : : /* Set locale. */
389 : 914 : setlocale (LC_ALL, "");
390 : :
391 : : /* Initialize the message catalog. */
392 : 914 : textdomain (PACKAGE_TARNAME);
393 : :
394 : : /* Look up once. */
395 : 914 : yes_str = _("yes");
396 : 914 : no_str = _("no");
397 : :
398 : : /* Parse and process arguments. */
399 : 914 : int remaining;
400 : 914 : argp_parse (&argp, argc, argv, 0, &remaining, NULL);
401 : :
402 : : /* Before we start tell the ELF library which version we are using. */
403 : 914 : elf_version (EV_CURRENT);
404 : :
405 : : /* Now process all the files given at the command line. */
406 : 914 : bool only_one = remaining + 1 == argc;
407 : 992 : do
408 : : {
409 : : /* Open the file. */
410 : 992 : int fd = open (argv[remaining], O_RDONLY);
411 [ - + ]: 992 : if (fd == -1)
412 : : {
413 : 0 : error (0, errno, _("cannot open input file '%s'"), argv[remaining]);
414 : 0 : continue;
415 : : }
416 : :
417 : 992 : process_file (fd, argv[remaining], only_one);
418 : :
419 : 992 : close (fd);
420 : : }
421 [ + + ]: 992 : while (++remaining < argc);
422 : :
423 : 914 : cleanup_list (dump_data_sections);
424 : 914 : cleanup_list (string_sections);
425 : :
426 : 914 : return error_message_count != 0;
427 : : }
428 : :
429 : : static void
430 : 674 : add_dump_section (const char *name,
431 : : int key,
432 : : bool implicit)
433 : : {
434 : 674 : struct section_argument *a = xmalloc (sizeof *a);
435 : 674 : a->arg = name;
436 : 674 : a->next = NULL;
437 : 674 : a->implicit = implicit;
438 : 1348 : struct section_argument ***tailp
439 [ + + ]: 674 : = key == 'x' ? &dump_data_sections_tail : &string_sections_tail;
440 : 674 : **tailp = a;
441 : 674 : *tailp = &a->next;
442 : 674 : }
443 : :
444 : : /* Handle program arguments. */
445 : : static error_t
446 : 5802 : parse_opt (int key, char *arg,
447 : : struct argp_state *state __attribute__ ((unused)))
448 : : {
449 [ + + - + : 5802 : switch (key)
+ - + + -
+ + + + +
+ - + + +
+ + + - +
+ + + + ]
450 : : {
451 : 220 : case 'a':
452 : 220 : print_file_header = true;
453 : 220 : print_program_header = true;
454 : 220 : print_relocations = true;
455 : 220 : print_section_header = true;
456 : 220 : print_symbol_table = true;
457 : 220 : print_version_info = true;
458 : 220 : print_dynamic_table = true;
459 : 220 : print_section_groups = true;
460 : 220 : print_histogram = true;
461 : 220 : print_arch = true;
462 : 220 : print_notes = true;
463 : 220 : implicit_debug_sections |= section_exception;
464 : 220 : add_dump_section (".strtab", key, true);
465 : 220 : add_dump_section (".dynstr", key, true);
466 : 220 : add_dump_section (".comment", key, true);
467 : 220 : any_control_option = true;
468 : 220 : break;
469 : 8 : case 'A':
470 : 8 : print_arch = true;
471 : 8 : any_control_option = true;
472 : 8 : break;
473 : 6 : case 'd':
474 : 6 : print_dynamic_table = true;
475 : 6 : any_control_option = true;
476 : 6 : break;
477 : 2 : case 'D':
478 : 2 : use_dynamic_segment = true;
479 : 2 : break;
480 : 0 : case 'e':
481 : 0 : print_debug_sections |= section_exception;
482 : 0 : any_control_option = true;
483 : 0 : break;
484 : 18 : case 'g':
485 : 18 : print_section_groups = true;
486 : 18 : any_control_option = true;
487 : 18 : break;
488 : 6 : case 'h':
489 : 6 : print_file_header = true;
490 : 6 : any_control_option = true;
491 : 6 : break;
492 : 0 : case 'I':
493 : 0 : print_histogram = true;
494 : 0 : any_control_option = true;
495 : 0 : break;
496 : 4 : case 'l':
497 : 4 : print_program_header = true;
498 : 4 : any_control_option = true;
499 : 4 : break;
500 : 46 : case 'n':
501 : 46 : print_notes = true;
502 : 46 : any_control_option = true;
503 : 46 : notes_section = arg;
504 : 46 : break;
505 : 2 : case 'r':
506 : 2 : print_relocations = true;
507 : 2 : any_control_option = true;
508 : 2 : break;
509 : 326 : case 'S':
510 : 326 : print_section_header = true;
511 : 326 : any_control_option = true;
512 : 326 : break;
513 : 34 : case 's':
514 : 34 : print_symbol_table = true;
515 : 34 : any_control_option = true;
516 : 34 : symbol_table_section = arg;
517 : 34 : break;
518 : 2 : case PRINT_DYNSYM_TABLE:
519 : 2 : print_dynsym_table = true;
520 : 2 : any_control_option = true;
521 : 2 : break;
522 : 0 : case 'V':
523 : 0 : print_version_info = true;
524 : 0 : any_control_option = true;
525 : 0 : break;
526 : 4 : case 'c':
527 : 4 : print_archive_index = true;
528 : 4 : break;
529 : 256 : case 'w':
530 [ + + ]: 256 : if (arg == NULL)
531 : : {
532 : 82 : print_debug_sections = section_all;
533 : 82 : implicit_debug_sections = section_info;
534 : 82 : show_split_units = true;
535 : : }
536 [ - + ]: 174 : else if (strcmp (arg, "abbrev") == 0)
537 : 0 : print_debug_sections |= section_abbrev;
538 [ + + ]: 174 : else if (strcmp (arg, "addr") == 0)
539 : : {
540 : 4 : print_debug_sections |= section_addr;
541 : 4 : implicit_debug_sections |= section_info;
542 : : }
543 [ + + ]: 170 : else if (strcmp (arg, "aranges") == 0)
544 : 6 : print_debug_sections |= section_aranges;
545 [ + + ]: 164 : else if (strcmp (arg, "decodedaranges") == 0)
546 : : {
547 : 2 : print_debug_sections |= section_aranges;
548 : 2 : decodedaranges = true;
549 : : }
550 [ + + ]: 162 : else if (strcmp (arg, "ranges") == 0)
551 : : {
552 : 26 : print_debug_sections |= section_ranges;
553 : 26 : implicit_debug_sections |= section_info;
554 : : }
555 [ + + + + ]: 136 : else if (strcmp (arg, "frame") == 0 || strcmp (arg, "frames") == 0)
556 : 6 : print_debug_sections |= section_frame;
557 [ + + ]: 130 : else if (strcmp (arg, "info") == 0)
558 : : {
559 : 38 : print_debug_sections |= section_info;
560 : 38 : print_debug_sections |= section_types;
561 : : }
562 [ + + ]: 92 : else if (strcmp (arg, "info+") == 0)
563 : : {
564 : 4 : print_debug_sections |= section_info;
565 : 4 : print_debug_sections |= section_types;
566 : 4 : show_split_units = true;
567 : : }
568 [ + + ]: 88 : else if (strcmp (arg, "loc") == 0)
569 : : {
570 : 38 : print_debug_sections |= section_loc;
571 : 38 : implicit_debug_sections |= section_info;
572 : : }
573 [ + + ]: 50 : else if (strcmp (arg, "line") == 0)
574 : 16 : print_debug_sections |= section_line;
575 [ + + ]: 34 : else if (strcmp (arg, "decodedline") == 0)
576 : : {
577 : 14 : print_debug_sections |= section_line;
578 : 14 : decodedline = true;
579 : : }
580 [ - + ]: 20 : else if (strcmp (arg, "pubnames") == 0)
581 : 0 : print_debug_sections |= section_pubnames;
582 [ + + ]: 20 : else if (strcmp (arg, "str") == 0)
583 : : {
584 : 6 : print_debug_sections |= section_str;
585 : : /* For mapping string offset tables to CUs. */
586 : 6 : implicit_debug_sections |= section_info;
587 : : }
588 [ - + ]: 14 : else if (strcmp (arg, "macinfo") == 0)
589 : 0 : print_debug_sections |= section_macinfo;
590 [ + + ]: 14 : else if (strcmp (arg, "macro") == 0)
591 : 6 : print_debug_sections |= section_macro;
592 [ - + ]: 8 : else if (strcmp (arg, "exception") == 0)
593 : 0 : print_debug_sections |= section_exception;
594 [ + - ]: 8 : else if (strcmp (arg, "gdb_index") == 0)
595 : 8 : 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 : 256 : any_control_option = true;
605 : 256 : break;
606 : 2 : case 'p':
607 : 2 : any_control_option = true;
608 [ - + ]: 2 : if (arg == NULL)
609 : : {
610 : 0 : print_string_sections = true;
611 : 0 : break;
612 : : }
613 : 14 : FALLTHROUGH;
614 : : case 'x':
615 : 14 : add_dump_section (arg, key, false);
616 : 14 : any_control_option = true;
617 : 14 : break;
618 : 86 : case 'N':
619 : 86 : print_address_names = false;
620 : 86 : break;
621 : 56 : case 'U':
622 : 56 : print_unresolved_addresses = true;
623 : 56 : break;
624 : 0 : case ARGP_KEY_NO_ARGS:
625 : 0 : fputs (_("Missing file name.\n"), stderr);
626 : 0 : goto do_argp_help;
627 : 914 : case ARGP_KEY_FINI:
628 [ + + + - ]: 914 : 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 : 122 : case 'z':
640 : 122 : print_decompress = true;
641 : 122 : break;
642 : 10 : case ELF_INPUT_SECTION:
643 [ + - ]: 10 : if (arg == NULL)
644 : 10 : elf_input_section = ".gnu_debugdata";
645 : : else
646 : 0 : elf_input_section = arg;
647 : : break;
648 : 10 : case DWARF_SKELETON:
649 : 10 : dwarf_skeleton = arg;
650 : 10 : 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 : 10 : open_input_section (int fd)
662 : : {
663 : 10 : size_t shnums;
664 : 10 : size_t cnt;
665 : 10 : size_t shstrndx;
666 : 10 : Elf *elf = elf_begin (fd, ELF_C_READ_MMAP, NULL);
667 [ - + ]: 10 : 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 [ - + ]: 10 : 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 [ - + ]: 10 : 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 [ + - ]: 236 : for (cnt = 0; cnt < shnums; ++cnt)
690 : : {
691 : 236 : Elf_Scn *scn = elf_getscn (elf, cnt);
692 [ - + ]: 236 : 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 : 236 : GElf_Shdr shdr_mem;
700 : 236 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
701 [ - + ]: 236 : 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 : 236 : const char *sname = elf_strptr (elf, shstrndx, shdr->sh_name);
709 [ - + ]: 236 : if (sname == NULL)
710 : : {
711 : 0 : error (0, 0, _("cannot get section name"));
712 : 0 : goto open_error;
713 : : }
714 : :
715 [ + + ]: 236 : if (strcmp (sname, elf_input_section) == 0)
716 : : {
717 : 10 : Elf_Data *data = elf_rawdata (scn, NULL);
718 [ - + ]: 10 : 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 [ + - ]: 10 : const char *tmpdir = getenv ("TMPDIR") ?: P_tmpdir;
728 : 10 : static const char suffix[] = "/readelfXXXXXX";
729 : 10 : int tmplen = strlen (tmpdir) + sizeof (suffix);
730 : 10 : char *tempname = alloca (tmplen);
731 : 10 : sprintf (tempname, "%s%s", tmpdir, suffix);
732 : :
733 : 10 : int sfd = mkstemp (tempname);
734 [ - + ]: 10 : if (sfd == -1)
735 : : {
736 : 0 : error (0, 0, _("cannot create temp file '%s'"),
737 : : tempname);
738 : 0 : goto open_error;
739 : : }
740 : 10 : unlink (tempname);
741 : :
742 : 10 : ssize_t size = data->d_size;
743 [ - + ]: 10 : 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 [ - + ]: 10 : if (elf_end (elf) != 0)
750 : : {
751 : 0 : error (0, 0, _("error while closing Elf descriptor: %s"),
752 : : elf_errmsg (-1));
753 : 10 : return -1;
754 : : }
755 : :
756 [ - + ]: 10 : 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 : 4 : check_archive_index (int fd, const char *fname, bool only_one)
776 : : {
777 : : /* Create an `Elf' descriptor. */
778 : 4 : Elf *elf = elf_begin (fd, ELF_C_READ_MMAP, NULL);
779 [ - + ]: 4 : if (elf == NULL)
780 : 0 : error (0, 0, _("cannot generate Elf descriptor: %s"),
781 : : elf_errmsg (-1));
782 : : else
783 : : {
784 [ + - ]: 4 : if (elf_kind (elf) == ELF_K_AR)
785 : : {
786 [ - + ]: 4 : if (!only_one)
787 : 0 : printf ("\n%s:\n\n", fname);
788 : 4 : 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 [ - + ]: 4 : if (elf_end (elf) != 0)
797 : 0 : error (0, 0, _("error while closing Elf descriptor: %s"),
798 : : elf_errmsg (-1));
799 : : }
800 : 4 : }
801 : :
802 : : /* Trivial callback used for checking if we opened an archive. */
803 : : static int
804 : 892 : 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 [ + - ]: 892 : if (*(bool *) arg)
811 : : return DWARF_CB_ABORT;
812 : 892 : *(bool *) arg = true;
813 : 892 : 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 : 988 : 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 : 988 : const struct process_dwflmod_args *a = arg;
830 : :
831 : : /* Print the file name. */
832 [ + + ]: 988 : if (!a->only_one)
833 : : {
834 : 96 : const char *fname;
835 : 96 : dwfl_module_info (dwflmod, NULL, NULL, NULL, NULL, NULL, &fname, NULL);
836 : :
837 : 96 : printf ("\n%s:\n\n", fname);
838 : : }
839 : :
840 : 988 : process_elf_file (dwflmod, a->fd);
841 : :
842 : 988 : 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 : 284 : 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 : 284 : Dwarf_Addr dwbias;
859 : 284 : 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 [ + + ]: 284 : if (dwbias == (Dwarf_Addr) -1)
866 : : return -1;
867 : :
868 : 44 : 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 : 1006 : create_dwfl (int fd, const char *fname)
875 : : {
876 : : /* Duplicate an fd for dwfl_report_offline to swallow. */
877 : 1006 : int dwfl_fd = dup (fd);
878 [ - + ]: 1006 : 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 : 1006 : static const Dwfl_Callbacks callbacks =
884 : : {
885 : : .section_address = dwfl_offline_section_address,
886 : : .find_debuginfo = find_no_debuginfo
887 : : };
888 : 1006 : Dwfl *dwfl = dwfl_begin (&callbacks);
889 [ + - ]: 1006 : if (likely (dwfl != NULL))
890 : : /* Let 0 be the logical address of the file (or first in archive). */
891 : 1006 : dwfl->offline_next_address = 0;
892 [ - + ]: 1006 : 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 : 1006 : dwfl_report_end (dwfl, NULL, NULL);
907 : :
908 : 1006 : return dwfl;
909 : : }
910 : :
911 : : /* Process one input file. */
912 : : static void
913 : 992 : process_file (int fd, const char *fname, bool only_one)
914 : : {
915 [ + + ]: 992 : if (print_archive_index)
916 : 4 : check_archive_index (fd, fname, only_one);
917 : :
918 [ + + ]: 992 : if (!any_control_option)
919 : : return;
920 : :
921 [ + + ]: 988 : if (elf_input_section != NULL)
922 : : {
923 : : /* Replace fname and fd with section content. */
924 : 10 : char *fnname = alloca (strlen (fname) + strlen (elf_input_section) + 2);
925 : 10 : sprintf (fnname, "%s:%s", fname, elf_input_section);
926 : 10 : fd = open_input_section (fd);
927 [ - + ]: 10 : 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 : 988 : Dwfl *dwfl = create_dwfl (fd, fname);
937 [ + - ]: 988 : if (dwfl != NULL)
938 : : {
939 [ + + ]: 988 : if (only_one)
940 : : {
941 : : /* Clear ONLY_ONE if we have multiple modules, from an archive. */
942 : 892 : bool seen = false;
943 : 892 : only_one = dwfl_getmodules (dwfl, &count_dwflmod, &seen, 0) == 0;
944 : : }
945 : :
946 : : /* Process the one or more modules gleaned from this file. */
947 : 988 : struct process_dwflmod_args a = { .fd = fd, .only_one = only_one };
948 : 988 : 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 [ + - ]: 988 : if (! do_not_close_dwfl)
953 : 988 : dwfl_end (dwfl);
954 : :
955 : : /* Need to close the replaced fd if we created it. Caller takes
956 : : care of original. */
957 [ + + ]: 988 : if (elf_input_section != NULL)
958 : 10 : close (fd);
959 : : }
960 : :
961 : : /* Check whether there are any compressed sections in the ELF file. */
962 : : static bool
963 : 530 : elf_contains_chdrs (Elf *elf)
964 : : {
965 : 530 : Elf_Scn *scn = NULL;
966 [ + + ]: 799124 : while ((scn = elf_nextscn (elf, scn)) != NULL)
967 : : {
968 : 798708 : GElf_Shdr shdr_mem;
969 : 798708 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
970 [ + - + + ]: 798708 : if (shdr != NULL && (shdr->sh_flags & SHF_COMPRESSED) != 0)
971 : 114 : return true;
972 : : }
973 : : return false;
974 : : }
975 : :
976 : : /* Process one ELF file. */
977 : : static void
978 : 988 : process_elf_file (Dwfl_Module *dwflmod, int fd)
979 : : {
980 : 988 : GElf_Addr dwflbias;
981 : 988 : Elf *elf = dwfl_module_getelf (dwflmod, &dwflbias);
982 : :
983 : 988 : GElf_Ehdr ehdr_mem;
984 : 988 : GElf_Ehdr *ehdr = gelf_getehdr (elf, &ehdr_mem);
985 : :
986 [ - + ]: 988 : if (ehdr == NULL)
987 : : {
988 : 0 : error (0, 0, _("cannot read ELF header: %s"), elf_errmsg (-1));
989 : 0 : return;
990 : : }
991 : :
992 : 988 : Ebl *ebl = ebl_openbackend (elf);
993 [ - + ]: 988 : 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 [ - + ]: 988 : 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 [ - + ]: 988 : 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 : 2166 : bool print_unchanged = ((print_section_header
1016 [ + + ]: 442 : || print_relocations
1017 [ + + ]: 440 : || dump_data_sections != NULL
1018 [ + + ]: 428 : || print_notes)
1019 [ + + + + ]: 1048 : && (ehdr->e_type == ET_REL
1020 [ + + ]: 530 : || elf_contains_chdrs (ebl->elf)));
1021 : :
1022 : 190 : Elf *pure_elf = NULL;
1023 : 190 : Ebl *pure_ebl = ebl;
1024 : 190 : if (print_unchanged)
1025 : : {
1026 : : /* Read the file afresh. */
1027 : 190 : off_t aroff = elf_getaroff (elf);
1028 : 190 : pure_elf = dwelf_elf_begin (fd);
1029 [ - + ]: 190 : 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 [ - + ]: 190 : if (pure_elf == NULL)
1038 : : {
1039 : 0 : error (0, 0, _("cannot read ELF: %s"), elf_errmsg (-1));
1040 : 0 : return;
1041 : : }
1042 : 190 : pure_ebl = ebl_openbackend (pure_elf);
1043 [ - + ]: 190 : if (pure_ebl == NULL)
1044 : 0 : goto ebl_error;
1045 : : }
1046 : :
1047 : 988 : bool symtab_printed = false;
1048 : :
1049 [ + + ]: 988 : if (print_file_header)
1050 : 226 : print_ehdr (ebl, ehdr);
1051 [ + + ]: 988 : if (print_section_header)
1052 : 546 : print_shdr (pure_ebl, ehdr);
1053 [ + + ]: 988 : if (print_program_header)
1054 : 224 : print_phdr (ebl, ehdr);
1055 [ + + ]: 988 : if (print_section_groups)
1056 : 238 : print_scngrp (ebl);
1057 [ + + ]: 988 : if (print_dynamic_table)
1058 : 226 : print_dynamic (ebl);
1059 [ + + ]: 988 : if (print_relocations)
1060 : 222 : print_relocs (pure_ebl, dwflmod, ehdr);
1061 [ + + ]: 988 : if (print_histogram)
1062 : 220 : handle_hash (ebl);
1063 [ + + + + ]: 988 : if (print_symbol_table || print_dynsym_table)
1064 : 256 : symtab_printed |= print_symtab (ebl, SHT_DYNSYM);
1065 [ + + ]: 988 : if (print_version_info)
1066 : 220 : print_verinfo (ebl);
1067 [ + + + - ]: 988 : if (print_symbol_table && !use_dynamic_segment)
1068 : 254 : symtab_printed |= print_symtab (ebl, SHT_SYMTAB);
1069 : :
1070 [ + + + + ]: 988 : if ((print_symbol_table || print_dynsym_table)
1071 [ + + + + ]: 256 : && !symtab_printed && symbol_table_section != NULL)
1072 : 2 : printf ("WARNING: %s: '%s'\n", _("cannot find section"),
1073 : : symbol_table_section);
1074 : :
1075 [ + + ]: 988 : if (print_arch)
1076 : 228 : print_liblist (ebl);
1077 [ + + ]: 988 : if (print_arch)
1078 : 228 : print_attributes (ebl, ehdr);
1079 [ + + ]: 988 : if (dump_data_sections != NULL)
1080 : 12 : dump_data (pure_ebl);
1081 [ + + ]: 988 : if (string_sections != NULL)
1082 : 222 : dump_strings (ebl);
1083 [ + + ]: 988 : if ((print_debug_sections | implicit_debug_sections) != 0)
1084 : 526 : print_debug (dwflmod, ebl, ehdr);
1085 [ + + ]: 988 : if (print_notes)
1086 : 266 : handle_notes (pure_ebl, ehdr);
1087 [ - + ]: 988 : if (print_string_sections)
1088 : 0 : print_strings (ebl);
1089 : :
1090 [ + + ]: 988 : if (pure_ebl != ebl)
1091 : : {
1092 : 190 : ebl_closebackend (ebl);
1093 : 190 : ebl_closebackend (pure_ebl);
1094 : 190 : elf_end (pure_elf);
1095 : : }
1096 : : else
1097 : 798 : ebl_closebackend (ebl);
1098 : : }
1099 : :
1100 : :
1101 : : /* Print file type. */
1102 : : static void
1103 : 226 : print_file_type (unsigned short int e_type)
1104 : : {
1105 [ + - ]: 226 : if (likely (e_type <= ET_CORE))
1106 : : {
1107 : 226 : 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 : 226 : 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 : 226 : }
1124 : :
1125 : :
1126 : : /* Print ELF header. */
1127 : : static void
1128 : 226 : print_ehdr (Ebl *ebl, GElf_Ehdr *ehdr)
1129 : : {
1130 : 226 : fputs_unlocked (_("ELF Header:\n Magic: "), stdout);
1131 [ + + ]: 3842 : for (size_t cnt = 0; cnt < EI_NIDENT; ++cnt)
1132 : 3616 : printf (" %02hhx", ehdr->e_ident[cnt]);
1133 : :
1134 : 226 : printf (_("\n Class: %s\n"),
1135 [ + + ]: 226 : ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? "ELF32"
1136 : : : ehdr->e_ident[EI_CLASS] == ELFCLASS64 ? "ELF64"
1137 [ - + ]: 186 : : "\?\?\?");
1138 : :
1139 : 226 : printf (_(" Data: %s\n"),
1140 [ - + ]: 226 : 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 : 678 : printf (_(" Ident Version: %hhd %s\n"),
1146 : 226 : ehdr->e_ident[EI_VERSION],
1147 [ + - ]: 226 : ehdr->e_ident[EI_VERSION] == EV_CURRENT ? _("(current)")
1148 : : : "(\?\?\?)");
1149 : :
1150 : 226 : char buf[512];
1151 : 226 : printf (_(" OS/ABI: %s\n"),
1152 : 226 : ebl_osabi_name (ebl, ehdr->e_ident[EI_OSABI], buf, sizeof (buf)));
1153 : :
1154 : 452 : printf (_(" ABI Version: %hhd\n"),
1155 : 226 : ehdr->e_ident[EI_ABIVERSION]);
1156 : :
1157 : 226 : fputs_unlocked (_(" Type: "), stdout);
1158 : 226 : print_file_type (ehdr->e_type);
1159 : :
1160 : 226 : const char *machine = dwelf_elf_e_machine_string (ehdr->e_machine);
1161 [ + - ]: 226 : if (machine != NULL)
1162 : 226 : printf (_(" Machine: %s\n"), machine);
1163 : : else
1164 : 0 : printf (_(" Machine: <unknown>: 0x%x\n"),
1165 : 0 : ehdr->e_machine);
1166 : :
1167 : 226 : printf (_(" Version: %d %s\n"),
1168 : : ehdr->e_version,
1169 [ + - ]: 226 : ehdr->e_version == EV_CURRENT ? _("(current)") : "(\?\?\?)");
1170 : :
1171 : 226 : printf (_(" Entry point address: %#" PRIx64 "\n"),
1172 : : ehdr->e_entry);
1173 : :
1174 : 226 : printf (_(" Start of program headers: %" PRId64 " %s\n"),
1175 : : ehdr->e_phoff, _("(bytes into file)"));
1176 : :
1177 : 226 : printf (_(" Start of section headers: %" PRId64 " %s\n"),
1178 : : ehdr->e_shoff, _("(bytes into file)"));
1179 : :
1180 : 226 : printf (_(" Flags: %s\n"),
1181 : : ebl_machine_flag_name (ebl, ehdr->e_flags, buf, sizeof (buf)));
1182 : :
1183 : 452 : printf (_(" Size of this header: %" PRId16 " %s\n"),
1184 : 226 : ehdr->e_ehsize, _("(bytes)"));
1185 : :
1186 : 452 : printf (_(" Size of program header entries: %" PRId16 " %s\n"),
1187 : 226 : ehdr->e_phentsize, _("(bytes)"));
1188 : :
1189 : 452 : printf (_(" Number of program headers entries: %" PRId16),
1190 : 226 : ehdr->e_phnum);
1191 [ + + ]: 226 : if (ehdr->e_phnum == PN_XNUM)
1192 : : {
1193 : 2 : GElf_Shdr shdr_mem;
1194 : 2 : GElf_Shdr *shdr = gelf_getshdr (elf_getscn (ebl->elf, 0), &shdr_mem);
1195 [ + - ]: 2 : if (shdr != NULL)
1196 : 2 : printf (_(" (%" PRIu32 " in [0].sh_info)"),
1197 : 2 : (uint32_t) shdr->sh_info);
1198 : : else
1199 : 0 : fputs_unlocked (_(" ([0] not available)"), stdout);
1200 : : }
1201 [ - + ]: 226 : fputc_unlocked ('\n', stdout);
1202 : :
1203 : 452 : printf (_(" Size of section header entries: %" PRId16 " %s\n"),
1204 : 226 : ehdr->e_shentsize, _("(bytes)"));
1205 : :
1206 : 452 : printf (_(" Number of section headers entries: %" PRId16),
1207 : 226 : ehdr->e_shnum);
1208 [ - + ]: 226 : 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_unlocked (_(" ([0] not available)"), stdout);
1217 : : }
1218 [ - + ]: 226 : fputc_unlocked ('\n', stdout);
1219 : :
1220 [ - + ]: 226 : 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 : 226 : printf (_(" Section header string table index: %" PRId16 "\n\n"),
1239 : : ehdr->e_shstrndx);
1240 : 226 : }
1241 : :
1242 : :
1243 : : static const char *
1244 : 74582 : get_visibility_type (int value)
1245 : : {
1246 : 74582 : switch (value)
1247 : : {
1248 : : case STV_DEFAULT:
1249 : : return "DEFAULT";
1250 : : case STV_INTERNAL:
1251 : : return "INTERNAL";
1252 : : case STV_HIDDEN:
1253 : : return "HIDDEN";
1254 : : case STV_PROTECTED:
1255 : : return "PROTECTED";
1256 : : default:
1257 : : return "???";
1258 : : }
1259 : : }
1260 : :
1261 : : static const char *
1262 : 384 : elf_ch_type_name (unsigned int code)
1263 : : {
1264 : 384 : switch (code)
1265 : : {
1266 : : case 0:
1267 : : return "NONE";
1268 : : case ELFCOMPRESS_ZLIB:
1269 : : return "ZLIB";
1270 : : case ELFCOMPRESS_ZSTD:
1271 : : return "ZSTD";
1272 : : default:
1273 : : return "UNKNOWN";
1274 : : }
1275 : : }
1276 : :
1277 : : /* Print the section headers. */
1278 : : static void
1279 : 546 : print_shdr (Ebl *ebl, GElf_Ehdr *ehdr)
1280 : : {
1281 : 546 : size_t cnt;
1282 : 546 : size_t shstrndx;
1283 : :
1284 [ + + ]: 546 : if (! print_file_header)
1285 : : {
1286 : 326 : size_t sections;
1287 [ - + ]: 326 : 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 : 326 : 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 [ - + ]: 546 : 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 : 546 : puts (_("Section Headers:"));
1303 : :
1304 [ + + ]: 546 : if (ehdr->e_ident[EI_CLASS] == ELFCLASS32)
1305 : 202 : puts (_("[Nr] Name Type Addr Off Size ES Flags Lk Inf Al"));
1306 : : else
1307 : 344 : puts (_("[Nr] Name Type Addr Off Size ES Flags Lk Inf Al"));
1308 : :
1309 [ + + ]: 546 : if (print_decompress)
1310 : : {
1311 [ + + ]: 112 : if (ehdr->e_ident[EI_CLASS] == ELFCLASS32)
1312 : 56 : puts (_(" [Compression Size Al]"));
1313 : : else
1314 : 56 : puts (_(" [Compression Size Al]"));
1315 : : }
1316 : :
1317 [ + + ]: 1587572 : for (cnt = 0; cnt < shnum; ++cnt)
1318 : : {
1319 : 1587026 : Elf_Scn *scn = elf_getscn (ebl->elf, cnt);
1320 : :
1321 [ - + ]: 1587026 : if (unlikely (scn == NULL))
1322 : 0 : error_exit (0, _("cannot get section: %s"),
1323 : : elf_errmsg (-1));
1324 : :
1325 : : /* Get the section header. */
1326 : 1587026 : GElf_Shdr shdr_mem;
1327 : 1587026 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
1328 [ - + ]: 1587026 : if (unlikely (shdr == NULL))
1329 : 0 : error_exit (0, _("cannot get section header: %s"),
1330 : : elf_errmsg (-1));
1331 : :
1332 : 1587026 : char flagbuf[20];
1333 : 1587026 : char *cp = flagbuf;
1334 [ + + ]: 1587026 : if (shdr->sh_flags & SHF_WRITE)
1335 : 3040 : *cp++ = 'W';
1336 [ + + ]: 1587026 : if (shdr->sh_flags & SHF_ALLOC)
1337 : 9368 : *cp++ = 'A';
1338 [ + + ]: 1587026 : if (shdr->sh_flags & SHF_EXECINSTR)
1339 : 1636 : *cp++ = 'X';
1340 [ + + ]: 1587026 : if (shdr->sh_flags & SHF_MERGE)
1341 : 458 : *cp++ = 'M';
1342 [ + + ]: 1587026 : if (shdr->sh_flags & SHF_STRINGS)
1343 : 450 : *cp++ = 'S';
1344 [ + + ]: 1587026 : if (shdr->sh_flags & SHF_INFO_LINK)
1345 : 214 : *cp++ = 'I';
1346 [ + + ]: 1587026 : if (shdr->sh_flags & SHF_LINK_ORDER)
1347 : 4 : *cp++ = 'L';
1348 [ - + ]: 1587026 : if (shdr->sh_flags & SHF_OS_NONCONFORMING)
1349 : 0 : *cp++ = 'N';
1350 [ - + ]: 1587026 : if (shdr->sh_flags & SHF_GROUP)
1351 : 0 : *cp++ = 'G';
1352 [ + + ]: 1587026 : if (shdr->sh_flags & SHF_TLS)
1353 : 30 : *cp++ = 'T';
1354 [ + + ]: 1587026 : if (shdr->sh_flags & SHF_COMPRESSED)
1355 : 414 : *cp++ = 'C';
1356 [ - + ]: 1587026 : if (shdr->sh_flags & SHF_ORDERED)
1357 : 0 : *cp++ = 'O';
1358 [ - + ]: 1587026 : if (shdr->sh_flags & SHF_EXCLUDE)
1359 : 0 : *cp++ = 'E';
1360 [ + + ]: 1587026 : if (shdr->sh_flags & SHF_GNU_RETAIN)
1361 : 2 : *cp++ = 'R';
1362 : 1587026 : *cp = '\0';
1363 : :
1364 : 1587026 : const char *sname;
1365 : 1587026 : char buf[128];
1366 [ - + ]: 1587026 : sname = elf_strptr (ebl->elf, shstrndx, shdr->sh_name) ?: "<corrupt>";
1367 [ + + ]: 3174052 : 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 : 1587026 : 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 [ + + ]: 1587026 : 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 [ + + ]: 1587026 : if (print_decompress)
1379 : : {
1380 [ + + ]: 1880 : if ((shdr->sh_flags & SHF_COMPRESSED) != 0)
1381 : : {
1382 : 384 : GElf_Chdr chdr;
1383 [ + - ]: 384 : if (gelf_getchdr (scn, &chdr) != NULL)
1384 [ + - ]: 768 : printf (" [ELF %s (%" PRId32 ") %0*" PRIx64
1385 : : " %2" PRId64 "]\n",
1386 : : elf_ch_type_name (chdr.ch_type),
1387 : : chdr.ch_type,
1388 [ + + ]: 384 : 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 [ + + ]: 1496 : else if (startswith (sname, ".zdebug"))
1396 : : {
1397 : 24 : ssize_t size;
1398 [ + - ]: 24 : if ((size = dwelf_scn_gnu_compressed_size (scn)) >= 0)
1399 : 24 : printf (" [GNU ZLIB %0*zx ]\n",
1400 [ + + ]: 24 : ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 6 : 8, size);
1401 : : else
1402 : 0 : 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 [ - + ]: 546 : fputc_unlocked ('\n', stdout);
1410 : 546 : }
1411 : :
1412 : :
1413 : : /* Print the program header. */
1414 : : static void
1415 : 224 : print_phdr (Ebl *ebl, GElf_Ehdr *ehdr)
1416 : : {
1417 [ + + ]: 224 : if (phnum == 0)
1418 : : /* No program header, this is OK in relocatable objects. */
1419 : 12 : return;
1420 : :
1421 : 212 : puts (_("Program Headers:"));
1422 [ + + ]: 212 : if (ehdr->e_ident[EI_CLASS] == ELFCLASS32)
1423 : 32 : puts (_("\
1424 : : Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align"));
1425 : : else
1426 : 180 : 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 [ + + ]: 2418 : for (size_t cnt = 0; cnt < phnum; ++cnt)
1434 : : {
1435 : 2206 : char buf[128];
1436 : 2206 : GElf_Phdr mem;
1437 : 2206 : GElf_Phdr *phdr = gelf_getphdr (ebl->elf, cnt, &mem);
1438 : :
1439 : : /* If for some reason the header cannot be returned show this. */
1440 [ - + ]: 2206 : if (unlikely (phdr == NULL))
1441 : : {
1442 : 0 : puts (" ???");
1443 : 0 : continue;
1444 : : }
1445 : :
1446 : 2206 : 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 : 2206 : 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 [ + + ]: 2206 : ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 8 : 16, phdr->p_paddr,
1452 : : phdr->p_filesz,
1453 : : phdr->p_memsz,
1454 [ - + ]: 2206 : phdr->p_flags & PF_R ? 'R' : ' ',
1455 [ + + ]: 2206 : phdr->p_flags & PF_W ? 'W' : ' ',
1456 [ + + ]: 2206 : phdr->p_flags & PF_X ? 'E' : ' ',
1457 : : phdr->p_align);
1458 : :
1459 [ + + ]: 2206 : 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 : 208 : Elf_Scn *scn = gelf_offscn (ebl->elf, phdr->p_offset);
1472 : 208 : GElf_Shdr shdr_mem;
1473 : 208 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
1474 : :
1475 : 208 : size_t maxsize;
1476 : 208 : char *filedata = elf_rawfile (ebl->elf, &maxsize);
1477 : :
1478 [ + - + + ]: 208 : if (shdr != NULL && shdr->sh_type == SHT_PROGBITS
1479 [ + - + - ]: 154 : && filedata != NULL && phdr->p_offset < maxsize
1480 [ + - ]: 154 : && phdr->p_filesz <= maxsize - phdr->p_offset
1481 [ + - ]: 154 : && memchr (filedata + phdr->p_offset, '\0',
1482 : : phdr->p_filesz) != NULL)
1483 : 154 : printf (_("\t[Requesting program interpreter: %s]\n"),
1484 : : filedata + phdr->p_offset);
1485 : : }
1486 [ + + ]: 1998 : else if (phdr->p_type == PT_GNU_RELRO)
1487 : : {
1488 : 182 : has_relro = true;
1489 : 182 : relro_from = phdr->p_vaddr;
1490 : 182 : relro_to = relro_from + phdr->p_memsz;
1491 : : }
1492 : : }
1493 : :
1494 : 212 : size_t sections;
1495 [ - + ]: 212 : 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 [ + - ]: 212 : if (sections == 0)
1500 : : /* No sections in the file. Punt. */
1501 : : return;
1502 : :
1503 : : /* Get the section header string table index. */
1504 : 212 : size_t shstrndx;
1505 [ - + ]: 212 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
1506 : 0 : error_exit (0, _("cannot get section header string table index"));
1507 : :
1508 : 212 : puts (_("\n Section to Segment mapping:\n Segment Sections..."));
1509 : :
1510 [ + + ]: 2418 : for (size_t cnt = 0; cnt < phnum; ++cnt)
1511 : : {
1512 : : /* Print the segment number. */
1513 : 2206 : printf (" %2.2zu ", cnt);
1514 : :
1515 : 2206 : GElf_Phdr phdr_mem;
1516 : 2206 : GElf_Phdr *phdr = gelf_getphdr (ebl->elf, cnt, &phdr_mem);
1517 : : /* This must not happen. */
1518 [ - + ]: 2206 : 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 [ + + ]: 72072 : for (size_t inner = 1; inner < shnum; ++inner)
1526 : : {
1527 : 69866 : Elf_Scn *scn = elf_getscn (ebl->elf, inner);
1528 : : /* This should not happen. */
1529 [ - + ]: 69866 : if (unlikely (scn == NULL))
1530 : 0 : error_exit (0, _("cannot get section: %s"),
1531 : : elf_errmsg (-1));
1532 : :
1533 : : /* Get the section header. */
1534 : 69866 : GElf_Shdr shdr_mem;
1535 : 69866 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
1536 [ - + ]: 69866 : if (unlikely (shdr == NULL))
1537 : 0 : error_exit (0, _("cannot get section header: %s"),
1538 : : elf_errmsg (-1));
1539 : :
1540 [ + - ]: 69866 : if (shdr->sh_size > 0
1541 : : /* Compare allocated sections by VMA, unallocated
1542 : : sections by file offset. */
1543 [ + + ]: 69866 : && (shdr->sh_flags & SHF_ALLOC
1544 : 54156 : ? (shdr->sh_addr >= phdr->p_vaddr
1545 [ + + ]: 54156 : && (shdr->sh_addr + shdr->sh_size
1546 [ + + ]: 36782 : <= phdr->p_vaddr + phdr->p_memsz))
1547 : 15710 : : (shdr->sh_offset >= phdr->p_offset
1548 [ + + ]: 15710 : && (shdr->sh_offset + shdr->sh_size
1549 [ + + ]: 13768 : <= phdr->p_offset + phdr->p_filesz))))
1550 : : {
1551 [ + + ]: 7464 : if (has_relro && !in_relro
1552 [ + + ]: 4938 : && shdr->sh_addr >= relro_from
1553 [ + + ]: 880 : && shdr->sh_addr + shdr->sh_size <= relro_to)
1554 : : {
1555 : 564 : fputs_unlocked (" [RELRO:", stdout);
1556 : 564 : in_relro = true;
1557 : : }
1558 [ + + + + ]: 6900 : else if (has_relro && in_relro && shdr->sh_addr >= relro_to)
1559 : : {
1560 : 164 : fputs_unlocked ("]", stdout);
1561 : 164 : in_relro = false;
1562 : : }
1563 [ + + ]: 6736 : else if (has_relro && in_relro
1564 [ + + ]: 1382 : && shdr->sh_addr + shdr->sh_size > relro_to)
1565 : 18 : fputs_unlocked ("] <RELRO:", stdout);
1566 [ + + ]: 6718 : else if (phdr->p_type == PT_LOAD && (phdr->p_flags & PF_W) == 0)
1567 : : {
1568 [ + + ]: 3828 : if (!in_ro)
1569 : : {
1570 : 432 : fputs_unlocked (" [RO:", stdout);
1571 : 432 : 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 [ + - ]: 12744 : for (cnt2 = 0; cnt2 < phnum; ++cnt2)
1581 : : {
1582 : 12744 : phdr2 = gelf_getphdr (ebl->elf, cnt2, &phdr2_mem);
1583 : :
1584 [ + - + + ]: 12744 : if (phdr2 != NULL && phdr2->p_type == PT_LOAD
1585 [ + - ]: 7088 : && shdr->sh_addr >= phdr2->p_vaddr
1586 : 7088 : && (shdr->sh_addr + shdr->sh_size
1587 [ + + ]: 7088 : <= phdr2->p_vaddr + phdr2->p_memsz))
1588 : : break;
1589 : : }
1590 : :
1591 [ + - ]: 2890 : if (cnt2 < phnum)
1592 : : {
1593 [ + + + + ]: 2890 : if ((phdr2->p_flags & PF_W) == 0 && !in_ro)
1594 : : {
1595 : 730 : fputs_unlocked (" [RO:", stdout);
1596 : 730 : in_ro = true;
1597 : : }
1598 [ + + + - ]: 2160 : else if ((phdr2->p_flags & PF_W) != 0 && in_ro)
1599 : : {
1600 : 0 : fputs_unlocked ("]", stdout);
1601 : 0 : in_ro = false;
1602 : : }
1603 : : }
1604 : : }
1605 : :
1606 : 7464 : printf (" %s",
1607 : 7464 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name));
1608 : :
1609 : : /* Signal that this section is only partially covered. */
1610 [ + + ]: 7464 : if (has_relro && in_relro
1611 [ + + ]: 1946 : && shdr->sh_addr + shdr->sh_size > relro_to)
1612 : : {
1613 : 18 : fputs_unlocked (">", stdout);
1614 : 18 : in_relro = false;
1615 : : }
1616 : : }
1617 : : }
1618 [ + + ]: 2206 : if (in_relro || in_ro)
1619 : 1544 : fputs_unlocked ("]", stdout);
1620 : :
1621 : : /* Finish the line. */
1622 [ - + ]: 4412 : fputc_unlocked ('\n', stdout);
1623 : : }
1624 : : }
1625 : :
1626 : :
1627 : : static const char *
1628 : 1012 : section_name (Ebl *ebl, GElf_Shdr *shdr)
1629 : : {
1630 : 1012 : size_t shstrndx;
1631 [ + - - + ]: 1012 : if (shdr == NULL || elf_getshdrstrndx (ebl->elf, &shstrndx) < 0)
1632 : 0 : return "???";
1633 [ - + ]: 1012 : return elf_strptr (ebl->elf, shstrndx, shdr->sh_name) ?: "???";
1634 : : }
1635 : :
1636 : :
1637 : : static void
1638 : 32 : handle_scngrp (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr)
1639 : : {
1640 : : /* Get the data of the section. */
1641 : 32 : Elf_Data *data = elf_getdata (scn, NULL);
1642 : :
1643 : 32 : Elf_Scn *symscn = elf_getscn (ebl->elf, shdr->sh_link);
1644 : 32 : GElf_Shdr symshdr_mem;
1645 : 32 : GElf_Shdr *symshdr = gelf_getshdr (symscn, &symshdr_mem);
1646 : 32 : Elf_Data *symdata = elf_getdata (symscn, NULL);
1647 : :
1648 [ + - + - ]: 32 : if (data == NULL || data->d_size < sizeof (Elf32_Word) || symshdr == NULL
1649 [ - + ]: 32 : || symdata == NULL)
1650 : 0 : return;
1651 : :
1652 : : /* Get the section header string table index. */
1653 : 32 : size_t shstrndx;
1654 [ - + ]: 32 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
1655 : 0 : error_exit (0, _("cannot get section header string table index"));
1656 : :
1657 : 32 : Elf32_Word *grpref = (Elf32_Word *) data->d_buf;
1658 : :
1659 : 32 : GElf_Sym sym_mem;
1660 : 32 : GElf_Sym *sym = gelf_getsym (symdata, shdr->sh_info, &sym_mem);
1661 : :
1662 [ - + + + ]: 64 : printf ((grpref[0] & GRP_COMDAT)
1663 : 8 : ? 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 : 24 : : 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 : 32 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
1674 : : (sym == NULL ? NULL
1675 : 32 : : elf_strptr (ebl->elf, symshdr->sh_link, sym->st_name))
1676 : 0 : ?: _("<INVALID SYMBOL>"),
1677 [ + - ]: 32 : data->d_size / sizeof (Elf32_Word) - 1);
1678 : :
1679 [ + + ]: 104 : for (size_t cnt = 1; cnt < data->d_size / sizeof (Elf32_Word); ++cnt)
1680 : : {
1681 : 72 : GElf_Shdr grpshdr_mem;
1682 : 72 : GElf_Shdr *grpshdr = gelf_getshdr (elf_getscn (ebl->elf, grpref[cnt]),
1683 : : &grpshdr_mem);
1684 : :
1685 : 72 : const char *str;
1686 [ + - ]: 144 : printf (" [%2u] %s\n",
1687 : : grpref[cnt],
1688 : : grpshdr != NULL
1689 [ - + ]: 72 : && (str = elf_strptr (ebl->elf, shstrndx, grpshdr->sh_name))
1690 : 0 : ? str : _("<INVALID SECTION>"));
1691 : : }
1692 : : }
1693 : :
1694 : :
1695 : : static void
1696 : 238 : print_scngrp (Ebl *ebl)
1697 : : {
1698 : : /* Find all relocation sections and handle them. */
1699 : 238 : Elf_Scn *scn = NULL;
1700 : :
1701 [ + + ]: 7654 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
1702 : : {
1703 : : /* Handle the section if it is a symbol table. */
1704 : 7416 : GElf_Shdr shdr_mem;
1705 : 7416 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
1706 : :
1707 [ + - + + ]: 7416 : if (shdr != NULL && shdr->sh_type == SHT_GROUP)
1708 : : {
1709 [ - + ]: 32 : 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 : 32 : handle_scngrp (ebl, scn, shdr);
1722 : : }
1723 : : }
1724 : 238 : }
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 : 32 : print_flags (int class, GElf_Xword d_val, const struct flags *flags,
1782 : : int nflags)
1783 : : {
1784 : 32 : bool first = true;
1785 : 32 : int cnt;
1786 : :
1787 [ + + ]: 348 : for (cnt = 0; cnt < nflags; ++cnt)
1788 [ + + ]: 316 : if (d_val & flags[cnt].mask)
1789 : : {
1790 [ + + ]: 68 : if (!first)
1791 [ - + ]: 36 : putchar_unlocked (' ');
1792 : 68 : fputs_unlocked (flags[cnt].str, stdout);
1793 : 68 : d_val &= ~flags[cnt].mask;
1794 : 68 : first = false;
1795 : : }
1796 : :
1797 [ + + ]: 32 : if (d_val != 0)
1798 : : {
1799 [ + - ]: 20 : if (!first)
1800 [ - + ]: 20 : putchar_unlocked (' ');
1801 [ + + ]: 32 : printf ("%#0*" PRIx64, class == ELFCLASS32 ? 10 : 18, d_val);
1802 : : }
1803 : :
1804 [ - + ]: 32 : putchar_unlocked ('\n');
1805 : 32 : }
1806 : :
1807 : :
1808 : : static void
1809 : 14 : print_dt_flags (int class, GElf_Xword d_val)
1810 : : {
1811 : 14 : print_flags (class, d_val, dt_flags, ndt_flags);
1812 : 14 : }
1813 : :
1814 : :
1815 : : static void
1816 : 14 : print_dt_flags_1 (int class, GElf_Xword d_val)
1817 : : {
1818 : 14 : print_flags (class, d_val, dt_flags_1, ndt_flags_1);
1819 : 14 : }
1820 : :
1821 : :
1822 : : static void
1823 : 2 : print_dt_feature_1 (int class, GElf_Xword d_val)
1824 : : {
1825 : 2 : print_flags (class, d_val, dt_feature_1, ndt_feature_1);
1826 : 2 : }
1827 : :
1828 : :
1829 : : static void
1830 : 2 : print_dt_posflag_1 (int class, GElf_Xword d_val)
1831 : : {
1832 : 2 : print_flags (class, d_val, dt_posflag_1, ndt_posflag_1);
1833 : 2 : }
1834 : :
1835 : :
1836 : : static size_t
1837 : 160 : get_dyn_ents (Elf_Data * dyn_data)
1838 : : {
1839 : 160 : GElf_Dyn *dyn;
1840 : 160 : GElf_Dyn dyn_mem;
1841 : 160 : size_t dyn_idx = 0;
1842 : 3866 : do
1843 : : {
1844 : 3866 : dyn = gelf_getdyn(dyn_data, dyn_idx, &dyn_mem);
1845 [ + - ]: 3866 : if (dyn != NULL)
1846 : 3866 : ++dyn_idx;
1847 : : }
1848 [ + + ]: 3866 : while (dyn != NULL && dyn->d_tag != DT_NULL);
1849 : :
1850 : 160 : return dyn_idx;
1851 : : }
1852 : :
1853 : :
1854 : : static void
1855 : 160 : handle_dynamic (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr, GElf_Phdr *phdr)
1856 : : {
1857 : 160 : int class = gelf_getclass (ebl->elf);
1858 : 160 : GElf_Shdr glink_mem;
1859 : 160 : GElf_Shdr *glink;
1860 : 160 : Elf_Data *data;
1861 : 160 : size_t cnt;
1862 : 160 : size_t shstrndx;
1863 : 160 : size_t dyn_ents;
1864 : :
1865 : : /* Get the data of the section. */
1866 [ + + + - ]: 160 : if (use_dynamic_segment && phdr != NULL)
1867 : 2 : data = elf_getdata_rawchunk(ebl->elf, phdr->p_offset,
1868 : : phdr->p_filesz, ELF_T_DYN);
1869 : : else
1870 : 158 : data = elf_getdata (scn, NULL);
1871 : :
1872 [ - + ]: 160 : if (data == NULL)
1873 : 0 : return;
1874 : :
1875 : : /* Get the dynamic section entry number */
1876 : 160 : dyn_ents = get_dyn_ents (data);
1877 : :
1878 [ + + + - ]: 160 : if (!use_dynamic_segment && shdr != NULL)
1879 : : {
1880 : : /* Get the section header string table index. */
1881 [ - + ]: 158 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
1882 : 0 : error_exit (0, _("cannot get section header string table index"));
1883 : :
1884 : 158 : glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link), &glink_mem);
1885 [ - + ]: 158 : if (glink == NULL)
1886 : 0 : error_exit (0, _("invalid sh_link value in section %zu"),
1887 : : elf_ndxscn (scn));
1888 : :
1889 : 158 : 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 [ + + ]: 158 : (int) shdr->sh_link,
1898 : 158 : elf_strptr (ebl->elf, shstrndx, glink->sh_name));
1899 : : }
1900 [ + - ]: 2 : else if (phdr != NULL)
1901 : : {
1902 [ + - ]: 4 : 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 : 160 : fputs_unlocked (_(" Type Value\n"), stdout);
1913 : :
1914 : : /* if --use-dynamic option is enabled,
1915 : : use the string table to get the related library info. */
1916 : 160 : Elf_Data *strtab_data = NULL;
1917 [ + + + - ]: 160 : if (use_dynamic_segment && phdr != NULL)
1918 : : {
1919 : 2 : strtab_data = get_dynscn_strtab(ebl->elf, phdr);
1920 [ - + ]: 2 : if (strtab_data == NULL)
1921 : 0 : error_exit (0, _("cannot get string table by using dynamic segment"));
1922 : : }
1923 : :
1924 [ + + ]: 4026 : for (cnt = 0; cnt < dyn_ents; ++cnt)
1925 : : {
1926 : 3866 : GElf_Dyn dynmem;
1927 : 3866 : GElf_Dyn *dyn = gelf_getdyn (data, cnt, &dynmem);
1928 [ + - ]: 3866 : if (dyn == NULL)
1929 : : break;
1930 : :
1931 : 3866 : char buf[64];
1932 : 3866 : printf (" %-17s ",
1933 : : ebl_dynamic_tag_name (ebl, dyn->d_tag, buf, sizeof (buf)));
1934 : :
1935 : 3866 : char *name = NULL;
1936 : 3866 : if (dyn->d_tag == DT_NEEDED
1937 [ + + ]: 3866 : || dyn->d_tag == DT_SONAME
1938 : : || dyn->d_tag == DT_RPATH
1939 : : || dyn->d_tag == DT_RUNPATH)
1940 : : {
1941 [ + + + - ]: 264 : if (! use_dynamic_segment && shdr != NULL)
1942 : 260 : name = elf_strptr (ebl->elf, shdr->sh_link, dyn->d_un.d_val);
1943 [ - + ]: 4 : else if (dyn->d_un.d_val < strtab_data->d_size
1944 : 4 : && memrchr (strtab_data->d_buf + dyn->d_un.d_val, '\0',
1945 [ - + ]: 4 : strtab_data->d_size - 1 - dyn->d_un.d_val) != NULL)
1946 : 3866 : name = ((char *) strtab_data->d_buf) + dyn->d_un.d_val;
1947 : : }
1948 : :
1949 [ + + + + : 3866 : switch (dyn->d_tag)
+ + + + +
+ + + + ]
1950 : : {
1951 : 316 : case DT_NULL:
1952 : : case DT_DEBUG:
1953 : : case DT_BIND_NOW:
1954 : : case DT_TEXTREL:
1955 : : /* No further output. */
1956 [ - + ]: 4182 : fputc_unlocked ('\n', stdout);
1957 : : break;
1958 : :
1959 : 254 : case DT_NEEDED:
1960 : 254 : printf (_("Shared library: [%s]\n"), name);
1961 : 254 : break;
1962 : :
1963 : 6 : case DT_SONAME:
1964 : 6 : printf (_("Library soname: [%s]\n"), name);
1965 : 6 : break;
1966 : :
1967 : 2 : case DT_RPATH:
1968 : 2 : printf (_("Library rpath: [%s]\n"), name);
1969 : 2 : break;
1970 : :
1971 : 2 : case DT_RUNPATH:
1972 : 2 : printf (_("Library runpath: [%s]\n"), name);
1973 : 2 : break;
1974 : :
1975 : 1062 : 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 : 1062 : printf (_("%" PRId64 " (bytes)\n"), dyn->d_un.d_val);
1994 : 1062 : break;
1995 : :
1996 : 194 : case DT_VERDEFNUM:
1997 : : case DT_VERNEEDNUM:
1998 : : case DT_RELACOUNT:
1999 : : case DT_RELCOUNT:
2000 : 194 : printf ("%" PRId64 "\n", dyn->d_un.d_val);
2001 : 194 : break;
2002 : :
2003 : 132 : case DT_PLTREL:;
2004 : 132 : const char *tagname = ebl_dynamic_tag_name (ebl, dyn->d_un.d_val,
2005 : : NULL, 0);
2006 [ + + ]: 132 : puts (tagname ?: "???");
2007 : 132 : break;
2008 : :
2009 : 14 : case DT_FLAGS:
2010 : 14 : print_dt_flags (class, dyn->d_un.d_val);
2011 : 14 : break;
2012 : :
2013 : 14 : case DT_FLAGS_1:
2014 : 14 : print_dt_flags_1 (class, dyn->d_un.d_val);
2015 : 14 : break;
2016 : :
2017 : 2 : case DT_FEATURE_1:
2018 : 2 : print_dt_feature_1 (class, dyn->d_un.d_val);
2019 : 2 : break;
2020 : :
2021 : 2 : case DT_POSFLAG_1:
2022 : 2 : print_dt_posflag_1 (class, dyn->d_un.d_val);
2023 : 2 : break;
2024 : :
2025 : 1866 : default:
2026 [ + + ]: 1866 : printf ("%#0*" PRIx64 "\n",
2027 : : class == ELFCLASS32 ? 10 : 18, dyn->d_un.d_val);
2028 : 1866 : break;
2029 : : }
2030 : : }
2031 : : }
2032 : :
2033 : :
2034 : : /* Print the dynamic segment. */
2035 : : static void
2036 : 226 : print_dynamic (Ebl *ebl)
2037 : : {
2038 [ + + ]: 1276 : for (size_t i = 0; i < phnum; ++i)
2039 : : {
2040 : 1264 : GElf_Phdr phdr_mem;
2041 : 1264 : GElf_Phdr *phdr = gelf_getphdr (ebl->elf, i, &phdr_mem);
2042 : :
2043 [ + - + + ]: 1264 : if (phdr != NULL && phdr->p_type == PT_DYNAMIC)
2044 : : {
2045 : 214 : Elf_Scn *scn = gelf_offscn (ebl->elf, phdr->p_offset);
2046 : 214 : GElf_Shdr shdr_mem;
2047 : 214 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
2048 [ + + ]: 214 : if ((use_dynamic_segment && phdr != NULL)
2049 [ + + + + ]: 212 : || (shdr != NULL && shdr->sh_type == SHT_DYNAMIC))
2050 : 160 : handle_dynamic (ebl, scn, shdr, phdr);
2051 : 214 : break;
2052 : : }
2053 : : }
2054 : 226 : }
2055 : :
2056 : :
2057 : : /* Print relocations. */
2058 : : static void
2059 : 222 : print_relocs (Ebl *ebl, Dwfl_Module *mod, GElf_Ehdr *ehdr)
2060 : : {
2061 : : /* Find all relocation sections and handle them. */
2062 : 222 : Elf_Scn *scn = NULL;
2063 : :
2064 [ + + ]: 7206 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
2065 : : {
2066 : : /* Handle the section if it is a symbol table. */
2067 : 6984 : GElf_Shdr shdr_mem;
2068 : 6984 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
2069 : :
2070 [ + - ]: 6984 : if (likely (shdr != NULL))
2071 : : {
2072 [ + + ]: 6984 : if (shdr->sh_type == SHT_REL)
2073 : 100 : handle_relocs_rel (ebl, ehdr, scn, shdr);
2074 [ + + ]: 6884 : else if (shdr->sh_type == SHT_RELA)
2075 : 294 : handle_relocs_rela (ebl, ehdr, scn, shdr);
2076 [ - + ]: 6590 : else if (shdr->sh_type == SHT_RELR)
2077 : 0 : handle_relocs_relr (ebl, mod, scn, shdr);
2078 : : }
2079 : : }
2080 : 222 : }
2081 : :
2082 : :
2083 : : /* Handle a relocation section. */
2084 : : static void
2085 : 100 : handle_relocs_rel (Ebl *ebl, GElf_Ehdr *ehdr, Elf_Scn *scn, GElf_Shdr *shdr)
2086 : : {
2087 : 100 : int class = gelf_getclass (ebl->elf);
2088 : 100 : size_t sh_entsize = gelf_fsize (ebl->elf, ELF_T_REL, 1, EV_CURRENT);
2089 : 100 : int nentries = shdr->sh_size / sh_entsize;
2090 : :
2091 : : /* Get the data of the section. */
2092 : 100 : Elf_Data *data = elf_getdata (scn, NULL);
2093 [ + - ]: 100 : if (data == NULL)
2094 : 0 : return;
2095 : :
2096 : : /* Get the symbol table information. */
2097 : 100 : Elf_Scn *symscn = elf_getscn (ebl->elf, shdr->sh_link);
2098 : 100 : GElf_Shdr symshdr_mem;
2099 : 100 : GElf_Shdr *symshdr = gelf_getshdr (symscn, &symshdr_mem);
2100 : 100 : Elf_Data *symdata = elf_getdata (symscn, NULL);
2101 : :
2102 : : /* Get the section header of the section the relocations are for. */
2103 : 100 : GElf_Shdr destshdr_mem;
2104 : 100 : GElf_Shdr *destshdr = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_info),
2105 : : &destshdr_mem);
2106 : :
2107 [ + - - + ]: 100 : 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 : 100 : Elf_Data *xndxdata = NULL;
2116 : 100 : int xndxscnidx = elf_scnshndx (scn);
2117 [ - + ]: 100 : 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 : 100 : size_t shstrndx;
2122 [ - + ]: 100 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
2123 : 0 : error_exit (0, _("cannot get section header string table index"));
2124 : :
2125 [ + + ]: 100 : if (shdr->sh_info != 0)
2126 : 80 : 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 : 80 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
2133 : 80 : (unsigned int) shdr->sh_info,
2134 : 80 : 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 : 20 : 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 : 20 : (unsigned int) elf_ndxscn (scn),
2147 : 20 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
2148 : : shdr->sh_offset,
2149 : : nentries);
2150 [ + - ]: 100 : fputs_unlocked (class == ELFCLASS32
2151 : 100 : ? _("\
2152 : : Offset Type Value Name\n")
2153 : 0 : : _("\
2154 : : Offset Type Value Name\n"),
2155 : : stdout);
2156 : :
2157 : 100 : int is_statically_linked = 0;
2158 [ + + ]: 25966 : for (int cnt = 0; cnt < nentries; ++cnt)
2159 : : {
2160 : 25866 : GElf_Rel relmem;
2161 : 25866 : GElf_Rel *rel = gelf_getrel (data, cnt, &relmem);
2162 [ + - ]: 25866 : if (likely (rel != NULL))
2163 : : {
2164 : 25866 : char buf[128];
2165 : 25866 : GElf_Sym symmem;
2166 : 25866 : Elf32_Word xndx;
2167 : 51732 : GElf_Sym *sym = gelf_getsymshndx (symdata, xndxdata,
2168 : 25866 : GELF_R_SYM (rel->r_info),
2169 : : &symmem, &xndx);
2170 [ - + ]: 25866 : 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 [ + + ]: 25866 : else if (GELF_ST_TYPE (sym->st_info) != STT_SECTION)
2222 [ - + ]: 292 : printf (" %#0*" PRIx64 " %-20s %#0*" PRIx64 " %s\n",
2223 : : class == ELFCLASS32 ? 10 : 18, rel->r_offset,
2224 [ + - ]: 146 : 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 : 146 : ? 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 : 146 : elf_strptr (ebl->elf, symshdr->sh_link, sym->st_name));
2233 : : else
2234 : : {
2235 : : /* This is a relocation against a STT_SECTION symbol. */
2236 : 25720 : GElf_Shdr secshdr_mem;
2237 : 25720 : GElf_Shdr *secshdr;
2238 : 25720 : secshdr = gelf_getshdr (elf_getscn (ebl->elf,
2239 [ + - ]: 25720 : sym->st_shndx == SHN_XINDEX
2240 : 0 : ? xndx : sym->st_shndx),
2241 : : &secshdr_mem);
2242 : :
2243 [ - + ]: 25720 : 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 [ - + + - ]: 77160 : printf (" %#0*" PRIx64 " %-20s %#0*" PRIx64 " %s\n",
2257 : : class == ELFCLASS32 ? 10 : 18, rel->r_offset,
2258 : 25720 : ebl_reloc_type_check (ebl, GELF_R_TYPE (rel->r_info))
2259 : : /* Avoid the leading R_ which isn't carrying any
2260 : : information. */
2261 : 25720 : ? 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 : 25720 : elf_strptr (ebl->elf, shstrndx, secshdr->sh_name));
2266 : : }
2267 : : }
2268 : : }
2269 : : }
2270 : :
2271 : :
2272 : : /* Handle a relocation section. */
2273 : : static void
2274 : 294 : handle_relocs_rela (Ebl *ebl, GElf_Ehdr *ehdr, Elf_Scn *scn, GElf_Shdr *shdr)
2275 : : {
2276 : 294 : int class = gelf_getclass (ebl->elf);
2277 : 294 : size_t sh_entsize = gelf_fsize (ebl->elf, ELF_T_RELA, 1, EV_CURRENT);
2278 : 294 : int nentries = shdr->sh_size / sh_entsize;
2279 : :
2280 : : /* Get the data of the section. */
2281 : 294 : Elf_Data *data = elf_getdata (scn, NULL);
2282 [ + - ]: 294 : if (data == NULL)
2283 : 0 : return;
2284 : :
2285 : : /* Get the symbol table information. */
2286 : 294 : Elf_Scn *symscn = elf_getscn (ebl->elf, shdr->sh_link);
2287 : 294 : GElf_Shdr symshdr_mem;
2288 : 294 : GElf_Shdr *symshdr = gelf_getshdr (symscn, &symshdr_mem);
2289 : 294 : Elf_Data *symdata = elf_getdata (symscn, NULL);
2290 : :
2291 : : /* Get the section header of the section the relocations are for. */
2292 : 294 : GElf_Shdr destshdr_mem;
2293 : 294 : GElf_Shdr *destshdr = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_info),
2294 : : &destshdr_mem);
2295 : :
2296 [ + - - + ]: 294 : 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 : 294 : Elf_Data *xndxdata = NULL;
2305 : 294 : int xndxscnidx = elf_scnshndx (scn);
2306 [ - + ]: 294 : 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 : 294 : size_t shstrndx;
2311 [ - + ]: 294 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
2312 : 0 : error_exit (0, _("cannot get section header string table index"));
2313 : :
2314 [ + + ]: 294 : if (shdr->sh_info != 0)
2315 : 160 : 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 : 160 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
2322 : 160 : (unsigned int) shdr->sh_info,
2323 : 160 : 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 : 134 : 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 : 134 : (unsigned int) elf_ndxscn (scn),
2336 : 134 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
2337 : : shdr->sh_offset,
2338 : : nentries);
2339 [ - + ]: 294 : fputs_unlocked (class == ELFCLASS32
2340 : 0 : ? _("\
2341 : : Offset Type Value Addend Name\n")
2342 : 294 : : _("\
2343 : : Offset Type Value Addend Name\n"),
2344 : : stdout);
2345 : :
2346 : 294 : int is_statically_linked = 0;
2347 [ + + ]: 18636 : for (int cnt = 0; cnt < nentries; ++cnt)
2348 : : {
2349 : 18342 : GElf_Rela relmem;
2350 : 18342 : GElf_Rela *rel = gelf_getrela (data, cnt, &relmem);
2351 [ + - ]: 18342 : if (likely (rel != NULL))
2352 : : {
2353 : 18342 : char buf[64];
2354 : 18342 : GElf_Sym symmem;
2355 : 18342 : Elf32_Word xndx;
2356 : 36684 : GElf_Sym *sym = gelf_getsymshndx (symdata, xndxdata,
2357 : 18342 : GELF_R_SYM (rel->r_info),
2358 : : &symmem, &xndx);
2359 : :
2360 [ - + ]: 18342 : 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 [ + + ]: 18342 : else if (GELF_ST_TYPE (sym->st_info) != STT_SECTION)
2413 [ + - ]: 8208 : printf ("\
2414 : : %#0*" PRIx64 " %-15s %#0*" PRIx64 " %+6" PRId64 " %s\n",
2415 : : class == ELFCLASS32 ? 10 : 18, rel->r_offset,
2416 [ + - ]: 4104 : 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 : 4104 : ? 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 : 4104 : elf_strptr (ebl->elf, symshdr->sh_link, sym->st_name));
2426 : : else
2427 : : {
2428 : : /* This is a relocation against a STT_SECTION symbol. */
2429 : 14238 : GElf_Shdr secshdr_mem;
2430 : 14238 : GElf_Shdr *secshdr;
2431 : 14238 : secshdr = gelf_getshdr (elf_getscn (ebl->elf,
2432 [ + - ]: 14238 : sym->st_shndx == SHN_XINDEX
2433 : 0 : ? xndx : sym->st_shndx),
2434 : : &secshdr_mem);
2435 : :
2436 [ - + ]: 14238 : 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 [ + - + - ]: 42714 : printf ("\
2450 : : %#0*" PRIx64 " %-15s %#0*" PRIx64 " %+6" PRId64 " %s\n",
2451 : : class == ELFCLASS32 ? 10 : 18, rel->r_offset,
2452 : 14238 : ebl_reloc_type_check (ebl, GELF_R_TYPE (rel->r_info))
2453 : : /* Avoid the leading R_ which isn't carrying any
2454 : : information. */
2455 : 14238 : ? 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 : 14238 : 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 : 510 : print_symtab (Ebl *ebl, int type)
2579 : : {
2580 : : /* Use the dynamic section info to display symbol tables. */
2581 [ - + - - ]: 510 : 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 [ + + ]: 16320 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
2590 : : {
2591 : : /* Handle the section if it is a symbol table. */
2592 : 15810 : GElf_Shdr shdr_mem;
2593 : 15810 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
2594 : :
2595 [ + - + + ]: 15810 : if (shdr != NULL && shdr->sh_type == (GElf_Word) type)
2596 : : {
2597 [ + + ]: 310 : if (symbol_table_section != NULL)
2598 : : {
2599 : : /* Get the section header string table index. */
2600 : 14 : size_t shstrndx;
2601 : 14 : const char *sname;
2602 [ - + ]: 14 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
2603 : 0 : error_exit (0,
2604 : : _("cannot get section header string table index"));
2605 : 14 : sname = elf_strptr (ebl->elf, shstrndx, shdr->sh_name);
2606 [ + - + + ]: 14 : if (sname == NULL || strcmp (sname, symbol_table_section) != 0)
2607 : 8 : continue;
2608 : : }
2609 : :
2610 [ - + ]: 302 : 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 : 302 : symtab_printed = handle_symtab (ebl, scn, shdr);
2623 : : }
2624 : : }
2625 : :
2626 : : return symtab_printed;
2627 : : }
2628 : :
2629 : :
2630 : : static void
2631 : 302 : 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 [ + + ]: 74884 : for (unsigned int cnt = 0; cnt < nsyms; ++cnt)
2638 : : {
2639 : 74582 : char typebuf[64];
2640 : 74582 : char bindbuf[64];
2641 : 74582 : char scnbuf[64];
2642 : 74582 : Elf32_Word xndx;
2643 : 74582 : GElf_Sym sym_mem;
2644 : 74582 : GElf_Sym *sym
2645 : 74582 : = gelf_getsymshndx (symdata, xndx_data, cnt, &sym_mem, &xndx);
2646 : :
2647 [ - + ]: 74582 : if (unlikely (sym == NULL))
2648 : 0 : continue;
2649 : :
2650 : : /* Determine the real section index. */
2651 [ + - ]: 74582 : if (likely (sym->st_shndx != SHN_XINDEX))
2652 : 74582 : xndx = sym->st_shndx;
2653 : :
2654 [ + + ]: 148110 : printf (_ ("\
2655 : : %5u: %0*" PRIx64 " %6" PRId64 " %-7s %-6s %-9s %6s %s"),
2656 : 74582 : cnt, gelf_getclass (ebl->elf) == ELFCLASS32 ? 8 : 16,
2657 : : sym->st_value, sym->st_size,
2658 : 74582 : ebl_symbol_type_name (ebl, GELF_ST_TYPE (sym->st_info), typebuf,
2659 : : sizeof (typebuf)),
2660 : 74582 : ebl_symbol_binding_name (ebl, GELF_ST_BIND (sym->st_info),
2661 : : bindbuf, sizeof (bindbuf)),
2662 : 74582 : get_visibility_type (GELF_ST_VISIBILITY (sym->st_other)),
2663 : 74582 : ebl_section_name (ebl, sym->st_shndx, xndx, scnbuf,
2664 : : sizeof (scnbuf), NULL, shnum),
2665 [ - + ]: 74582 : use_dynamic_segment == true
2666 : 0 : ? (char *)symstr_data->d_buf + sym->st_name
2667 : 74582 : : elf_strptr (ebl->elf, idx, sym->st_name));
2668 : :
2669 [ + + ]: 74582 : if (versym_data != NULL)
2670 : : {
2671 : : /* Get the version information. */
2672 : 3380 : GElf_Versym versym_mem;
2673 : 3380 : GElf_Versym *versym = gelf_getversym (versym_data, cnt, &versym_mem);
2674 : :
2675 [ + - + + ]: 3380 : if (versym != NULL && ((*versym & 0x8000) != 0 || *versym > 1))
2676 : : {
2677 : 2452 : bool is_nobits = false;
2678 : 2452 : bool check_def = xndx != SHN_UNDEF;
2679 : :
2680 [ + + - + ]: 2452 : if (xndx < SHN_LORESERVE || sym->st_shndx == SHN_XINDEX)
2681 : : {
2682 : 2432 : GElf_Shdr symshdr_mem;
2683 : 2432 : GElf_Shdr *symshdr = gelf_getshdr (
2684 : : elf_getscn (ebl->elf, xndx), &symshdr_mem);
2685 : :
2686 : 2432 : is_nobits
2687 [ + - + + ]: 4840 : = (symshdr != NULL && symshdr->sh_type == SHT_NOBITS);
2688 : : }
2689 : :
2690 [ + + ]: 2452 : if (is_nobits || !check_def)
2691 : : {
2692 : : /* We must test both. */
2693 : 2148 : GElf_Vernaux vernaux_mem;
2694 : 2148 : GElf_Vernaux *vernaux = NULL;
2695 : 2148 : size_t vn_offset = 0;
2696 : :
2697 : 2148 : GElf_Verneed verneed_mem;
2698 : 2148 : GElf_Verneed *verneed
2699 : 2148 : = gelf_getverneed (verneed_data, 0, &verneed_mem);
2700 [ + - ]: 7826 : while (verneed != NULL)
2701 : : {
2702 : 7826 : size_t vna_offset = vn_offset;
2703 : :
2704 : 7826 : vernaux = gelf_getvernaux (verneed_data,
2705 : 7826 : vna_offset += verneed->vn_aux,
2706 : : &vernaux_mem);
2707 [ + + ]: 27990 : while (vernaux != NULL && vernaux->vna_other != *versym
2708 [ + + ]: 18016 : && vernaux->vna_next != 0
2709 [ + - ]: 20164 : && (verneed_data->d_size - vna_offset
2710 [ + - ]: 12338 : >= vernaux->vna_next))
2711 : : {
2712 : : /* Update the offset. */
2713 : 12338 : vna_offset += vernaux->vna_next;
2714 : :
2715 : 12338 : vernaux = (vernaux->vna_next == 0
2716 : : ? NULL
2717 : 12338 : : gelf_getvernaux (verneed_data,
2718 : : vna_offset,
2719 : : &vernaux_mem));
2720 : : }
2721 : :
2722 : : /* Check whether we found the version. */
2723 [ + - + + ]: 7826 : if (vernaux != NULL && vernaux->vna_other == *versym)
2724 : : /* Found it. */
2725 : : break;
2726 : :
2727 [ + - ]: 5678 : if (verneed_data->d_size - vn_offset < verneed->vn_next)
2728 : : break;
2729 : :
2730 : 5678 : vn_offset += verneed->vn_next;
2731 : 5678 : verneed
2732 : : = (verneed->vn_next == 0
2733 : : ? NULL
2734 [ + - ]: 5678 : : gelf_getverneed (verneed_data, vn_offset,
2735 : : &verneed_mem));
2736 : : }
2737 : :
2738 [ + - + - ]: 2148 : if (vernaux != NULL && vernaux->vna_other == *versym)
2739 : : {
2740 : 4296 : printf ("@%s (%u)",
2741 [ - + ]: 2148 : use_dynamic_segment == true
2742 : 0 : ? (char *)symstr_data->d_buf
2743 : 0 : + vernaux->vna_name
2744 : 2148 : : elf_strptr (ebl->elf, verneed_stridx,
2745 : 2148 : vernaux->vna_name),
2746 : : (unsigned int)vernaux->vna_other);
2747 : 2148 : check_def = 0;
2748 : : }
2749 [ # # ]: 0 : else if (unlikely (!is_nobits))
2750 : 0 : error (0, 0, _ ("bad dynamic symbol"));
2751 : : else
2752 : : check_def = 1;
2753 : : }
2754 : :
2755 [ + - + - ]: 2452 : if (check_def && *versym != 0x8001)
2756 : : {
2757 : : /* We must test both. */
2758 : 304 : size_t vd_offset = 0;
2759 : :
2760 : 304 : GElf_Verdef verdef_mem;
2761 : 304 : GElf_Verdef *verdef
2762 : 304 : = gelf_getverdef (verdef_data, 0, &verdef_mem);
2763 [ + - ]: 866 : while (verdef != NULL)
2764 : : {
2765 [ + + ]: 866 : if (verdef->vd_ndx == (*versym & 0x7fff))
2766 : : /* Found the definition. */
2767 : : break;
2768 : :
2769 [ + - ]: 562 : if (verdef_data->d_size - vd_offset < verdef->vd_next)
2770 : : break;
2771 : :
2772 : 562 : vd_offset += verdef->vd_next;
2773 : 562 : verdef = (verdef->vd_next == 0
2774 : : ? NULL
2775 [ + - ]: 562 : : gelf_getverdef (verdef_data, vd_offset,
2776 : : &verdef_mem));
2777 : : }
2778 : :
2779 [ + - ]: 304 : if (verdef != NULL)
2780 : : {
2781 : 304 : GElf_Verdaux verdaux_mem;
2782 : 608 : GElf_Verdaux *verdaux = gelf_getverdaux (
2783 : 304 : verdef_data, vd_offset + verdef->vd_aux,
2784 : : &verdaux_mem);
2785 : :
2786 [ + - ]: 304 : if (verdaux != NULL)
2787 [ + - ]: 608 : printf ((*versym & 0x8000) ? "@%s" : "@@%s",
2788 [ - + ]: 304 : use_dynamic_segment == true
2789 : 0 : ? (char *)symstr_data->d_buf
2790 : 0 : + verdaux->vda_name
2791 : 304 : : elf_strptr (ebl->elf, verdef_stridx,
2792 : 304 : verdaux->vda_name));
2793 : : }
2794 : : }
2795 : : }
2796 : : }
2797 : :
2798 [ + + ]: 149164 : putchar_unlocked ('\n');
2799 : : }
2800 : 302 : }
2801 : :
2802 : :
2803 : : static bool
2804 : 302 : handle_symtab (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr)
2805 : : {
2806 : 302 : Elf_Data *versym_data = NULL;
2807 : 302 : Elf_Data *verneed_data = NULL;
2808 : 302 : Elf_Data *verdef_data = NULL;
2809 : 302 : Elf_Data *xndx_data = NULL;
2810 : 302 : int class = gelf_getclass (ebl->elf);
2811 : 302 : Elf32_Word verneed_stridx = 0;
2812 : 302 : Elf32_Word verdef_stridx = 0;
2813 : :
2814 : : /* Get the data of the section. */
2815 : 302 : Elf_Data *data = elf_getdata (scn, NULL);
2816 [ + - ]: 302 : if (data == NULL)
2817 : : return false;
2818 : :
2819 : : /* Find out whether we have other sections we might need. */
2820 : : Elf_Scn *runscn = NULL;
2821 [ + + ]: 9932 : while ((runscn = elf_nextscn (ebl->elf, runscn)) != NULL)
2822 : : {
2823 : 9630 : GElf_Shdr runshdr_mem;
2824 : 9630 : GElf_Shdr *runshdr = gelf_getshdr (runscn, &runshdr_mem);
2825 : :
2826 [ + - ]: 9630 : if (likely (runshdr != NULL))
2827 : : {
2828 [ + + ]: 9630 : if (runshdr->sh_type == SHT_GNU_versym
2829 [ + + ]: 226 : && runshdr->sh_link == elf_ndxscn (scn))
2830 : : /* Bingo, found the version information. Now get the data. */
2831 : 174 : versym_data = elf_getdata (runscn, NULL);
2832 [ + + ]: 9456 : else if (runshdr->sh_type == SHT_GNU_verneed)
2833 : : {
2834 : : /* This is the information about the needed versions. */
2835 : 226 : verneed_data = elf_getdata (runscn, NULL);
2836 : 226 : verneed_stridx = runshdr->sh_link;
2837 : : }
2838 [ + + ]: 9230 : else if (runshdr->sh_type == SHT_GNU_verdef)
2839 : : {
2840 : : /* This is the information about the defined versions. */
2841 : 8 : verdef_data = elf_getdata (runscn, NULL);
2842 : 8 : verdef_stridx = runshdr->sh_link;
2843 : : }
2844 [ + - ]: 9222 : else if (runshdr->sh_type == SHT_SYMTAB_SHNDX
2845 [ # # ]: 0 : && runshdr->sh_link == elf_ndxscn (scn))
2846 : : /* Extended section index. */
2847 : 0 : xndx_data = elf_getdata (runscn, NULL);
2848 : : }
2849 : : }
2850 : :
2851 : : /* Get the section header string table index. */
2852 : 302 : size_t shstrndx;
2853 [ - + ]: 302 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
2854 : 0 : error_exit (0, _("cannot get section header string table index"));
2855 : :
2856 : 302 : GElf_Shdr glink_mem;
2857 : 302 : GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
2858 : : &glink_mem);
2859 [ - + ]: 302 : if (glink == NULL)
2860 : 0 : error_exit (0, _("invalid sh_link value in section %zu"),
2861 : : elf_ndxscn (scn));
2862 : :
2863 : : /* Now we can compute the number of entries in the section. */
2864 : 604 : unsigned int nsyms = data->d_size / (class == ELFCLASS32
2865 : : ? sizeof (Elf32_Sym)
2866 [ + + ]: 302 : : sizeof (Elf64_Sym));
2867 : :
2868 : 302 : printf (ngettext ("\nSymbol table [%2u] '%s' contains %u entry:\n",
2869 : : "\nSymbol table [%2u] '%s' contains %u entries:\n",
2870 : : nsyms),
2871 : 302 : (unsigned int) elf_ndxscn (scn),
2872 : 302 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name), nsyms);
2873 : 302 : printf (ngettext (" %lu local symbol String table: [%2u] '%s'\n",
2874 : : " %lu local symbols String table: [%2u] '%s'\n",
2875 : : shdr->sh_info),
2876 : 302 : (unsigned long int) shdr->sh_info,
2877 : 302 : (unsigned int) shdr->sh_link,
2878 : 302 : elf_strptr (ebl->elf, shstrndx, glink->sh_name));
2879 : :
2880 [ + + ]: 604 : fputs_unlocked (class == ELFCLASS32
2881 : 38 : ? _("\
2882 : : Num: Value Size Type Bind Vis Ndx Name\n")
2883 : 264 : : _("\
2884 : : Num: Value Size Type Bind Vis Ndx Name\n"),
2885 : : stdout);
2886 : :
2887 : 302 : process_symtab(ebl, nsyms, shdr->sh_link, verneed_stridx, verdef_stridx,
2888 : : data, versym_data, NULL, verneed_data, verdef_data, xndx_data);
2889 : 302 : return true;
2890 : : }
2891 : :
2892 : :
2893 : : static bool
2894 : 0 : handle_dynamic_symtab (Ebl *ebl)
2895 : : {
2896 : 0 : GElf_Phdr phdr_mem;
2897 : 0 : GElf_Phdr *phdr = NULL;
2898 : : /* phnum is a static variable which was already fetched in function
2899 : : process_elf_file. */
2900 [ # # ]: 0 : for (size_t i = 0; i < phnum; ++i)
2901 : : {
2902 : 0 : phdr = gelf_getphdr (ebl->elf, i, &phdr_mem);
2903 [ # # ]: 0 : if (phdr->p_type == PT_DYNAMIC)
2904 : : break;
2905 : : }
2906 [ # # ]: 0 : if (phdr == NULL)
2907 : : return false;
2908 : :
2909 : 0 : GElf_Addr addrs[i_max] = {
2910 : : 0,
2911 : : };
2912 : 0 : GElf_Off offs[i_max] = {
2913 : : 0,
2914 : : };
2915 : 0 : get_dynscn_addrs (ebl->elf, phdr, addrs);
2916 : 0 : find_offsets (ebl->elf, 0, i_max, addrs, offs);
2917 : :
2918 : 0 : size_t syments = 0;
2919 : :
2920 : 0 : GElf_Ehdr ehdr_mem;
2921 : 0 : GElf_Ehdr *ehdr = gelf_getehdr (ebl->elf, &ehdr_mem);
2922 : :
2923 [ # # ]: 0 : if (offs[i_hash] != 0)
2924 : : {
2925 : : /* In the original format, .hash says the size of .dynsym. */
2926 : :
2927 [ # # # # : 0 : size_t entsz = SH_ENTSIZE_HASH (ehdr);
# # ]
2928 : : Elf_Data *data
2929 : 0 : = elf_getdata_rawchunk (ebl->elf, offs[i_hash] + entsz, entsz,
2930 : : (entsz == 4 ? ELF_T_WORD : ELF_T_XWORD));
2931 [ # # ]: 0 : if (data != NULL)
2932 : 0 : syments = (entsz == 4 ? *(const GElf_Word *)data->d_buf
2933 [ # # ]: 0 : : *(const GElf_Xword *)data->d_buf);
2934 : : }
2935 [ # # # # ]: 0 : if (offs[i_gnu_hash] != 0 && syments == 0)
2936 : : {
2937 : : /* In the new format, we can derive it with some work. */
2938 : :
2939 : 0 : const struct
2940 : : {
2941 : : Elf32_Word nbuckets;
2942 : : Elf32_Word symndx;
2943 : : Elf32_Word maskwords;
2944 : : Elf32_Word shift2;
2945 : : } * header;
2946 : :
2947 : 0 : Elf_Data *data = elf_getdata_rawchunk (ebl->elf, offs[i_gnu_hash],
2948 : : sizeof *header, ELF_T_WORD);
2949 [ # # ]: 0 : if (data != NULL)
2950 : : {
2951 : 0 : header = data->d_buf;
2952 : 0 : Elf32_Word nbuckets = header->nbuckets;
2953 : 0 : Elf32_Word symndx = header->symndx;
2954 : 0 : GElf_Off buckets_at
2955 : : = (offs[i_gnu_hash] + sizeof *header
2956 : 0 : + (gelf_getclass (ebl->elf) * sizeof (Elf32_Word)
2957 : 0 : * header->maskwords));
2958 : :
2959 : : // elf_getdata_rawchunk takes a size_t, make sure it
2960 : : // doesn't overflow.
2961 : : #if SIZE_MAX <= UINT32_MAX
2962 : : if (nbuckets > SIZE_MAX / sizeof (Elf32_Word))
2963 : : data = NULL;
2964 : : else
2965 : : #endif
2966 : 0 : data = elf_getdata_rawchunk (ebl->elf, buckets_at,
2967 : : nbuckets * sizeof (Elf32_Word),
2968 : : ELF_T_WORD);
2969 [ # # ]: 0 : if (data != NULL && symndx < nbuckets)
2970 : : {
2971 : 0 : const Elf32_Word *const buckets = data->d_buf;
2972 : 0 : Elf32_Word maxndx = symndx;
2973 [ # # ]: 0 : for (Elf32_Word bucket = 0; bucket < nbuckets; ++bucket)
2974 : 0 : if (buckets[bucket] > maxndx)
2975 : : maxndx = buckets[bucket];
2976 : :
2977 : 0 : GElf_Off hasharr_at
2978 : : = (buckets_at + nbuckets * sizeof (Elf32_Word));
2979 : 0 : hasharr_at += (maxndx - symndx) * sizeof (Elf32_Word);
2980 : 0 : do
2981 : : {
2982 : 0 : data = elf_getdata_rawchunk (
2983 : : ebl->elf, hasharr_at, sizeof (Elf32_Word), ELF_T_WORD);
2984 [ # # # # ]: 0 : if (data != NULL && (*(const Elf32_Word *)data->d_buf & 1u))
2985 : : {
2986 : 0 : syments = maxndx + 1;
2987 : 0 : break;
2988 : : }
2989 : 0 : ++maxndx;
2990 : 0 : hasharr_at += sizeof (Elf32_Word);
2991 : : }
2992 [ # # ]: 0 : while (data != NULL);
2993 : : }
2994 : : }
2995 : : }
2996 [ # # # # ]: 0 : if (offs[i_strtab] > offs[i_symtab] && syments == 0)
2997 : 0 : syments = ((offs[i_strtab] - offs[i_symtab])
2998 : 0 : / gelf_fsize (ebl->elf, ELF_T_SYM, 1, EV_CURRENT));
2999 : :
3000 [ # # # # : 0 : if (syments <= 0 || offs[i_strtab] == 0 || offs[i_symtab] == 0)
# # ]
3001 : : {
3002 : 0 : error_exit (0, _ ("Dynamic symbol information is not available for "
3003 : : "displaying symbols."));
3004 : : }
3005 : :
3006 : : /* All the data chunk initializaion. */
3007 : 0 : Elf_Data *symdata = NULL;
3008 : 0 : Elf_Data *symstrdata = NULL;
3009 : 0 : Elf_Data *versym_data = NULL;
3010 : 0 : Elf_Data *verdef_data = NULL;
3011 : 0 : Elf_Data *verneed_data = NULL;
3012 : :
3013 : 0 : symdata = elf_getdata_rawchunk (
3014 : : ebl->elf, offs[i_symtab],
3015 : : gelf_fsize (ebl->elf, ELF_T_SYM, syments, EV_CURRENT), ELF_T_SYM);
3016 : 0 : symstrdata = elf_getdata_rawchunk (ebl->elf, offs[i_strtab], addrs[i_strsz],
3017 : : ELF_T_BYTE);
3018 : 0 : versym_data = elf_getdata_rawchunk (
3019 : 0 : ebl->elf, offs[i_versym], syments * sizeof (Elf64_Half), ELF_T_HALF);
3020 : :
3021 : : /* Get the verneed_data without vernaux. */
3022 : 0 : verneed_data = elf_getdata_rawchunk (
3023 : 0 : ebl->elf, offs[i_verneed], addrs[i_verneednum] * sizeof (Elf64_Verneed),
3024 : : ELF_T_VNEED);
3025 : 0 : size_t vernauxnum = 0;
3026 : 0 : size_t vn_next_offset = 0;
3027 : :
3028 [ # # ]: 0 : for (size_t i = 0; i < addrs[i_verneednum]; i++)
3029 : : {
3030 : 0 : GElf_Verneed *verneed
3031 : 0 : = (GElf_Verneed *)(verneed_data->d_buf + vn_next_offset);
3032 : 0 : vernauxnum += verneed->vn_cnt;
3033 : 0 : vn_next_offset += verneed->vn_next;
3034 : : }
3035 : :
3036 : : /* Update the verneed_data to include the vernaux. */
3037 : 0 : verneed_data = elf_getdata_rawchunk (
3038 : : ebl->elf, offs[i_verneed],
3039 : 0 : (addrs[i_verneednum] + vernauxnum) * sizeof (GElf_Verneed), ELF_T_VNEED);
3040 : :
3041 : : /* Get the verdef_data without verdaux. */
3042 : 0 : verdef_data = elf_getdata_rawchunk (
3043 : 0 : ebl->elf, offs[i_verdef], addrs[i_verdefnum] * sizeof (Elf64_Verdef),
3044 : : ELF_T_VDEF);
3045 : 0 : size_t verdauxnum = 0;
3046 : 0 : size_t vd_next_offset = 0;
3047 : :
3048 [ # # ]: 0 : for (size_t i = 0; i < addrs[i_verdefnum]; i++)
3049 : : {
3050 : 0 : GElf_Verdef *verdef
3051 : 0 : = (GElf_Verdef *)(verdef_data->d_buf + vd_next_offset);
3052 : 0 : verdauxnum += verdef->vd_cnt;
3053 : 0 : vd_next_offset += verdef->vd_next;
3054 : : }
3055 : :
3056 : : /* Update the verdef_data to include the verdaux. */
3057 : 0 : verdef_data = elf_getdata_rawchunk (
3058 : : ebl->elf, offs[i_verdef],
3059 : 0 : (addrs[i_verdefnum] + verdauxnum) * sizeof (GElf_Verdef), ELF_T_VDEF);
3060 : :
3061 : 0 : unsigned int nsyms = (unsigned int)syments;
3062 : 0 : process_symtab (ebl, nsyms, 0, 0, 0, symdata, versym_data, symstrdata,
3063 : : verneed_data, verdef_data, NULL);
3064 : 0 : return true;
3065 : : }
3066 : :
3067 : :
3068 : : /* Print version information. */
3069 : : static void
3070 : 220 : print_verinfo (Ebl *ebl)
3071 : : {
3072 : : /* Find the version information sections. For this we have to
3073 : : search through the section table. */
3074 : 220 : Elf_Scn *scn = NULL;
3075 : :
3076 [ + + ]: 7136 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
3077 : : {
3078 : : /* Handle the section if it is part of the versioning handling. */
3079 : 6916 : GElf_Shdr shdr_mem;
3080 : 6916 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
3081 : :
3082 [ + - ]: 6916 : if (likely (shdr != NULL))
3083 : : {
3084 [ + + ]: 6916 : if (shdr->sh_type == SHT_GNU_verneed)
3085 : 154 : handle_verneed (ebl, scn, shdr);
3086 [ + + ]: 6762 : else if (shdr->sh_type == SHT_GNU_verdef)
3087 : 4 : handle_verdef (ebl, scn, shdr);
3088 [ + + ]: 6758 : else if (shdr->sh_type == SHT_GNU_versym)
3089 : 154 : handle_versym (ebl, scn, shdr);
3090 : : }
3091 : : }
3092 : 220 : }
3093 : :
3094 : :
3095 : : static const char *
3096 : 370 : get_ver_flags (unsigned int flags)
3097 : : {
3098 : 370 : static char buf[32];
3099 : 370 : char *endp;
3100 : :
3101 [ + + ]: 370 : if (flags == 0)
3102 : 366 : return _("none");
3103 : :
3104 [ + - ]: 4 : if (flags & VER_FLG_BASE)
3105 : 4 : endp = stpcpy (buf, "BASE ");
3106 : : else
3107 : : endp = buf;
3108 : :
3109 [ - + ]: 4 : if (flags & VER_FLG_WEAK)
3110 : : {
3111 [ # # ]: 0 : if (endp != buf)
3112 : 0 : endp = stpcpy (endp, "| ");
3113 : :
3114 : 0 : endp = stpcpy (endp, "WEAK ");
3115 : : }
3116 : :
3117 [ - + ]: 4 : if (unlikely (flags & ~(VER_FLG_BASE | VER_FLG_WEAK)))
3118 : : {
3119 : 0 : strncpy (endp, _("| <unknown>"), buf + sizeof (buf) - endp);
3120 : 0 : buf[sizeof (buf) - 1] = '\0';
3121 : : }
3122 : :
3123 : : return buf;
3124 : : }
3125 : :
3126 : :
3127 : : static void
3128 : 154 : handle_verneed (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr)
3129 : : {
3130 : 154 : int class = gelf_getclass (ebl->elf);
3131 : :
3132 : : /* Get the data of the section. */
3133 : 154 : Elf_Data *data = elf_getdata (scn, NULL);
3134 [ - + ]: 154 : if (data == NULL)
3135 : 0 : return;
3136 : :
3137 : : /* Get the section header string table index. */
3138 : 154 : size_t shstrndx;
3139 [ - + ]: 154 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
3140 : 0 : error_exit (0, _("cannot get section header string table index"));
3141 : :
3142 : 154 : GElf_Shdr glink_mem;
3143 : 154 : GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
3144 : : &glink_mem);
3145 [ - + ]: 154 : if (glink == NULL)
3146 : 0 : error_exit (0, _("invalid sh_link value in section %zu"),
3147 : : elf_ndxscn (scn));
3148 : :
3149 : 154 : printf (ngettext ("\
3150 : : \nVersion needs section [%2u] '%s' contains %d entry:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'\n",
3151 : : "\
3152 : : \nVersion needs section [%2u] '%s' contains %d entries:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'\n",
3153 : : shdr->sh_info),
3154 : 154 : (unsigned int) elf_ndxscn (scn),
3155 : 154 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name), shdr->sh_info,
3156 : : class == ELFCLASS32 ? 10 : 18, shdr->sh_addr,
3157 : : shdr->sh_offset,
3158 [ + + ]: 154 : (unsigned int) shdr->sh_link,
3159 : 154 : elf_strptr (ebl->elf, shstrndx, glink->sh_name));
3160 : :
3161 : 154 : unsigned int offset = 0;
3162 [ + - ]: 208 : for (int cnt = shdr->sh_info; --cnt >= 0; )
3163 : : {
3164 : : /* Get the data at the next offset. */
3165 : 208 : GElf_Verneed needmem;
3166 : 208 : GElf_Verneed *need = gelf_getverneed (data, offset, &needmem);
3167 [ + - ]: 208 : if (unlikely (need == NULL))
3168 : : break;
3169 : :
3170 : 624 : printf (_(" %#06x: Version: %hu File: %s Cnt: %hu\n"),
3171 : 208 : offset, (unsigned short int) need->vn_version,
3172 : 208 : elf_strptr (ebl->elf, shdr->sh_link, need->vn_file),
3173 : 208 : (unsigned short int) need->vn_cnt);
3174 : :
3175 : 208 : unsigned int auxoffset = offset + need->vn_aux;
3176 [ + - ]: 346 : for (int cnt2 = need->vn_cnt; --cnt2 >= 0; )
3177 : : {
3178 : 346 : GElf_Vernaux auxmem;
3179 : 346 : GElf_Vernaux *aux = gelf_getvernaux (data, auxoffset, &auxmem);
3180 [ + - ]: 346 : if (unlikely (aux == NULL))
3181 : : break;
3182 : :
3183 : 346 : printf (_(" %#06x: Name: %s Flags: %s Version: %hu\n"),
3184 : : auxoffset,
3185 : 346 : elf_strptr (ebl->elf, shdr->sh_link, aux->vna_name),
3186 : 346 : get_ver_flags (aux->vna_flags),
3187 : 346 : (unsigned short int) aux->vna_other);
3188 : :
3189 [ + + ]: 346 : if (aux->vna_next == 0)
3190 : : break;
3191 : :
3192 : 138 : auxoffset += aux->vna_next;
3193 : : }
3194 : :
3195 : : /* Find the next offset. */
3196 [ + + ]: 208 : if (need->vn_next == 0)
3197 : : break;
3198 : :
3199 : 54 : offset += need->vn_next;
3200 : : }
3201 : : }
3202 : :
3203 : :
3204 : : static void
3205 : 4 : handle_verdef (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr)
3206 : : {
3207 : : /* Get the data of the section. */
3208 : 4 : Elf_Data *data = elf_getdata (scn, NULL);
3209 [ - + ]: 4 : if (data == NULL)
3210 : 0 : return;
3211 : :
3212 : : /* Get the section header string table index. */
3213 : 4 : size_t shstrndx;
3214 [ - + ]: 4 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
3215 : 0 : error_exit (0, _("cannot get section header string table index"));
3216 : :
3217 : 4 : GElf_Shdr glink_mem;
3218 : 4 : GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
3219 : : &glink_mem);
3220 [ - + ]: 4 : if (glink == NULL)
3221 : 0 : error_exit (0, _("invalid sh_link value in section %zu"),
3222 : : elf_ndxscn (scn));
3223 : :
3224 : 4 : int class = gelf_getclass (ebl->elf);
3225 : 4 : printf (ngettext ("\
3226 : : \nVersion definition section [%2u] '%s' contains %d entry:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'\n",
3227 : : "\
3228 : : \nVersion definition section [%2u] '%s' contains %d entries:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'\n",
3229 : : shdr->sh_info),
3230 : 4 : (unsigned int) elf_ndxscn (scn),
3231 : 4 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
3232 : : shdr->sh_info,
3233 : : class == ELFCLASS32 ? 10 : 18, shdr->sh_addr,
3234 : : shdr->sh_offset,
3235 [ + - ]: 4 : (unsigned int) shdr->sh_link,
3236 : 4 : elf_strptr (ebl->elf, shstrndx, glink->sh_name));
3237 : :
3238 : 4 : unsigned int offset = 0;
3239 [ + - ]: 24 : for (int cnt = shdr->sh_info; --cnt >= 0; )
3240 : : {
3241 : : /* Get the data at the next offset. */
3242 : 24 : GElf_Verdef defmem;
3243 : 24 : GElf_Verdef *def = gelf_getverdef (data, offset, &defmem);
3244 [ + - ]: 24 : if (unlikely (def == NULL))
3245 : : break;
3246 : :
3247 : 24 : unsigned int auxoffset = offset + def->vd_aux;
3248 : 24 : GElf_Verdaux auxmem;
3249 : 24 : GElf_Verdaux *aux = gelf_getverdaux (data, auxoffset, &auxmem);
3250 [ + - ]: 24 : if (unlikely (aux == NULL))
3251 : : break;
3252 : :
3253 : 96 : printf (_("\
3254 : : %#06x: Version: %hd Flags: %s Index: %hd Cnt: %hd Name: %s\n"),
3255 : 24 : offset, def->vd_version,
3256 : 24 : get_ver_flags (def->vd_flags),
3257 : 24 : def->vd_ndx,
3258 : 24 : def->vd_cnt,
3259 : 24 : elf_strptr (ebl->elf, shdr->sh_link, aux->vda_name));
3260 : :
3261 : 24 : auxoffset += aux->vda_next;
3262 [ + + ]: 24 : for (int cnt2 = 1; cnt2 < def->vd_cnt; ++cnt2)
3263 : : {
3264 : 16 : aux = gelf_getverdaux (data, auxoffset, &auxmem);
3265 [ + - ]: 16 : if (unlikely (aux == NULL))
3266 : : break;
3267 : :
3268 : 16 : printf (_(" %#06x: Parent %d: %s\n"),
3269 : : auxoffset, cnt2,
3270 : 16 : elf_strptr (ebl->elf, shdr->sh_link, aux->vda_name));
3271 : :
3272 [ - + ]: 16 : if (aux->vda_next == 0)
3273 : : break;
3274 : :
3275 : 0 : auxoffset += aux->vda_next;
3276 : : }
3277 : :
3278 : : /* Find the next offset. */
3279 [ + + ]: 24 : if (def->vd_next == 0)
3280 : : break;
3281 : 20 : offset += def->vd_next;
3282 : : }
3283 : : }
3284 : :
3285 : :
3286 : : static void
3287 : 154 : handle_versym (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr)
3288 : : {
3289 : 154 : int class = gelf_getclass (ebl->elf);
3290 : 154 : const char **vername;
3291 : 154 : const char **filename;
3292 : :
3293 : : /* Get the data of the section. */
3294 : 154 : Elf_Data *data = elf_getdata (scn, NULL);
3295 [ + - ]: 154 : if (data == NULL)
3296 : 0 : return;
3297 : :
3298 : : /* Get the section header string table index. */
3299 : 154 : size_t shstrndx;
3300 [ - + ]: 154 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
3301 : 0 : error_exit (0, _("cannot get section header string table index"));
3302 : :
3303 : : /* We have to find the version definition section and extract the
3304 : : version names. */
3305 : : Elf_Scn *defscn = NULL;
3306 : : Elf_Scn *needscn = NULL;
3307 : :
3308 : : Elf_Scn *verscn = NULL;
3309 [ + + ]: 4754 : while ((verscn = elf_nextscn (ebl->elf, verscn)) != NULL)
3310 : : {
3311 : 4600 : GElf_Shdr vershdr_mem;
3312 : 4600 : GElf_Shdr *vershdr = gelf_getshdr (verscn, &vershdr_mem);
3313 : :
3314 [ - + ]: 4600 : if (likely (vershdr != NULL))
3315 : : {
3316 [ + + ]: 4600 : if (vershdr->sh_type == SHT_GNU_verdef)
3317 : : defscn = verscn;
3318 [ + + ]: 4596 : else if (vershdr->sh_type == SHT_GNU_verneed)
3319 : 4600 : needscn = verscn;
3320 : : }
3321 : : }
3322 : :
3323 : 154 : size_t nvername;
3324 [ + - ]: 154 : if (defscn != NULL || needscn != NULL)
3325 : : {
3326 : : /* We have a version information (better should have). Now get
3327 : : the version names. First find the maximum version number. */
3328 : 154 : nvername = 0;
3329 [ + + ]: 154 : if (defscn != NULL)
3330 : : {
3331 : : /* Run through the version definitions and find the highest
3332 : : index. */
3333 : 4 : unsigned int offset = 0;
3334 : 4 : Elf_Data *defdata;
3335 : 4 : GElf_Shdr defshdrmem;
3336 : 4 : GElf_Shdr *defshdr;
3337 : :
3338 : 4 : defdata = elf_getdata (defscn, NULL);
3339 [ + - ]: 4 : if (unlikely (defdata == NULL))
3340 : 0 : return;
3341 : :
3342 : 4 : defshdr = gelf_getshdr (defscn, &defshdrmem);
3343 [ + - ]: 4 : if (unlikely (defshdr == NULL))
3344 : : return;
3345 : :
3346 [ + - ]: 24 : for (unsigned int cnt = 0; cnt < defshdr->sh_info; ++cnt)
3347 : : {
3348 : 24 : GElf_Verdef defmem;
3349 : 24 : GElf_Verdef *def;
3350 : :
3351 : : /* Get the data at the next offset. */
3352 : 24 : def = gelf_getverdef (defdata, offset, &defmem);
3353 [ + - ]: 24 : if (unlikely (def == NULL))
3354 : : break;
3355 : :
3356 : 24 : nvername = MAX (nvername, (size_t) (def->vd_ndx & 0x7fff));
3357 : :
3358 [ + + ]: 24 : if (def->vd_next == 0)
3359 : : break;
3360 : 20 : offset += def->vd_next;
3361 : : }
3362 : : }
3363 [ + - ]: 154 : if (needscn != NULL)
3364 : : {
3365 : 154 : unsigned int offset = 0;
3366 : 154 : Elf_Data *needdata;
3367 : 154 : GElf_Shdr needshdrmem;
3368 : 154 : GElf_Shdr *needshdr;
3369 : :
3370 : 154 : needdata = elf_getdata (needscn, NULL);
3371 [ + - ]: 154 : if (unlikely (needdata == NULL))
3372 : 0 : return;
3373 : :
3374 : 154 : needshdr = gelf_getshdr (needscn, &needshdrmem);
3375 [ + - ]: 154 : if (unlikely (needshdr == NULL))
3376 : : return;
3377 : :
3378 [ + - ]: 208 : for (unsigned int cnt = 0; cnt < needshdr->sh_info; ++cnt)
3379 : : {
3380 : 208 : GElf_Verneed needmem;
3381 : 208 : GElf_Verneed *need;
3382 : 208 : unsigned int auxoffset;
3383 : 208 : int cnt2;
3384 : :
3385 : : /* Get the data at the next offset. */
3386 : 208 : need = gelf_getverneed (needdata, offset, &needmem);
3387 [ + - ]: 208 : if (unlikely (need == NULL))
3388 : : break;
3389 : :
3390 : : /* Run through the auxiliary entries. */
3391 : 208 : auxoffset = offset + need->vn_aux;
3392 [ + - ]: 346 : for (cnt2 = need->vn_cnt; --cnt2 >= 0; )
3393 : : {
3394 : 346 : GElf_Vernaux auxmem;
3395 : 346 : GElf_Vernaux *aux;
3396 : :
3397 : 346 : aux = gelf_getvernaux (needdata, auxoffset, &auxmem);
3398 [ + - ]: 346 : if (unlikely (aux == NULL))
3399 : : break;
3400 : :
3401 : 346 : nvername = MAX (nvername,
3402 : : (size_t) (aux->vna_other & 0x7fff));
3403 : :
3404 [ + + ]: 346 : if (aux->vna_next == 0)
3405 : : break;
3406 : 138 : auxoffset += aux->vna_next;
3407 : : }
3408 : :
3409 [ + + ]: 208 : if (need->vn_next == 0)
3410 : : break;
3411 : 54 : offset += need->vn_next;
3412 : : }
3413 : : }
3414 : :
3415 : : /* This is the number of versions we know about. */
3416 : 154 : ++nvername;
3417 : :
3418 : : /* Allocate the array. */
3419 : 154 : vername = (const char **) alloca (nvername * sizeof (const char *));
3420 [ + + ]: 154 : memset(vername, 0, nvername * sizeof (const char *));
3421 : 154 : filename = (const char **) alloca (nvername * sizeof (const char *));
3422 : 154 : memset(filename, 0, nvername * sizeof (const char *));
3423 : :
3424 : : /* Run through the data structures again and collect the strings. */
3425 [ + + ]: 154 : if (defscn != NULL)
3426 : : {
3427 : : /* Run through the version definitions and find the highest
3428 : : index. */
3429 : 4 : unsigned int offset = 0;
3430 : 4 : Elf_Data *defdata;
3431 : 4 : GElf_Shdr defshdrmem;
3432 : 4 : GElf_Shdr *defshdr;
3433 : :
3434 : 4 : defdata = elf_getdata (defscn, NULL);
3435 [ + - ]: 4 : if (unlikely (defdata == NULL))
3436 : 0 : return;
3437 : :
3438 : 4 : defshdr = gelf_getshdr (defscn, &defshdrmem);
3439 [ + - ]: 4 : if (unlikely (defshdr == NULL))
3440 : : return;
3441 : :
3442 [ + - ]: 24 : for (unsigned int cnt = 0; cnt < defshdr->sh_info; ++cnt)
3443 : : {
3444 : :
3445 : : /* Get the data at the next offset. */
3446 : 24 : GElf_Verdef defmem;
3447 : 24 : GElf_Verdef *def = gelf_getverdef (defdata, offset, &defmem);
3448 [ + - ]: 24 : if (unlikely (def == NULL))
3449 : : break;
3450 : :
3451 : 24 : GElf_Verdaux auxmem;
3452 : 48 : GElf_Verdaux *aux = gelf_getverdaux (defdata,
3453 : 24 : offset + def->vd_aux,
3454 : : &auxmem);
3455 [ + - ]: 24 : if (unlikely (aux == NULL))
3456 : : break;
3457 : :
3458 : 24 : vername[def->vd_ndx & 0x7fff]
3459 : 24 : = elf_strptr (ebl->elf, defshdr->sh_link, aux->vda_name);
3460 : 24 : filename[def->vd_ndx & 0x7fff] = NULL;
3461 : :
3462 [ + + ]: 24 : if (def->vd_next == 0)
3463 : : break;
3464 : 20 : offset += def->vd_next;
3465 : : }
3466 : : }
3467 [ + - ]: 154 : if (needscn != NULL)
3468 : : {
3469 : 154 : unsigned int offset = 0;
3470 : :
3471 : 154 : Elf_Data *needdata = elf_getdata (needscn, NULL);
3472 : 154 : GElf_Shdr needshdrmem;
3473 : 154 : GElf_Shdr *needshdr = gelf_getshdr (needscn, &needshdrmem);
3474 [ - + ]: 154 : if (unlikely (needdata == NULL || needshdr == NULL))
3475 : 0 : return;
3476 : :
3477 [ + - ]: 208 : for (unsigned int cnt = 0; cnt < needshdr->sh_info; ++cnt)
3478 : : {
3479 : : /* Get the data at the next offset. */
3480 : 208 : GElf_Verneed needmem;
3481 : 208 : GElf_Verneed *need = gelf_getverneed (needdata, offset,
3482 : : &needmem);
3483 [ + - ]: 208 : if (unlikely (need == NULL))
3484 : : break;
3485 : :
3486 : : /* Run through the auxiliary entries. */
3487 : 208 : unsigned int auxoffset = offset + need->vn_aux;
3488 [ + - ]: 346 : for (int cnt2 = need->vn_cnt; --cnt2 >= 0; )
3489 : : {
3490 : 346 : GElf_Vernaux auxmem;
3491 : 346 : GElf_Vernaux *aux = gelf_getvernaux (needdata, auxoffset,
3492 : : &auxmem);
3493 [ + - ]: 346 : if (unlikely (aux == NULL))
3494 : : break;
3495 : :
3496 : 346 : vername[aux->vna_other & 0x7fff]
3497 : 346 : = elf_strptr (ebl->elf, needshdr->sh_link, aux->vna_name);
3498 : 346 : filename[aux->vna_other & 0x7fff]
3499 : 346 : = elf_strptr (ebl->elf, needshdr->sh_link, need->vn_file);
3500 : :
3501 [ + + ]: 346 : if (aux->vna_next == 0)
3502 : : break;
3503 : 138 : auxoffset += aux->vna_next;
3504 : : }
3505 : :
3506 [ + + ]: 208 : if (need->vn_next == 0)
3507 : : break;
3508 : 54 : offset += need->vn_next;
3509 : : }
3510 : : }
3511 : : }
3512 : : else
3513 : : {
3514 : : vername = NULL;
3515 : : nvername = 1;
3516 : : filename = NULL;
3517 : : }
3518 : :
3519 : 154 : GElf_Shdr glink_mem;
3520 : 154 : GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
3521 : : &glink_mem);
3522 : 154 : size_t sh_entsize = gelf_fsize (ebl->elf, ELF_T_HALF, 1, EV_CURRENT);
3523 [ - + ]: 154 : if (glink == NULL)
3524 : 0 : error_exit (0, _("invalid sh_link value in section %zu"),
3525 : : elf_ndxscn (scn));
3526 : :
3527 : : /* Print the header. */
3528 : 154 : printf (ngettext ("\
3529 : : \nVersion symbols section [%2u] '%s' contains %d entry:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'",
3530 : : "\
3531 : : \nVersion symbols section [%2u] '%s' contains %d entries:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'",
3532 : : shdr->sh_size / sh_entsize),
3533 : 154 : (unsigned int) elf_ndxscn (scn),
3534 : 154 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
3535 : 154 : (int) (shdr->sh_size / sh_entsize),
3536 : : class == ELFCLASS32 ? 10 : 18, shdr->sh_addr,
3537 : : shdr->sh_offset,
3538 [ + + ]: 154 : (unsigned int) shdr->sh_link,
3539 : 154 : elf_strptr (ebl->elf, shstrndx, glink->sh_name));
3540 : :
3541 : : /* Now we can finally look at the actual contents of this section. */
3542 [ + + ]: 3276 : for (unsigned int cnt = 0; cnt < shdr->sh_size / sh_entsize; ++cnt)
3543 : : {
3544 [ + + ]: 3122 : if (cnt % 2 == 0)
3545 : 1594 : printf ("\n %4d:", cnt);
3546 : :
3547 : 3122 : GElf_Versym symmem;
3548 : 3122 : GElf_Versym *sym = gelf_getversym (data, cnt, &symmem);
3549 [ + - ]: 3122 : if (sym == NULL)
3550 : : break;
3551 : :
3552 [ + + + ]: 3122 : switch (*sym)
3553 : : {
3554 : 286 : ssize_t n;
3555 : 286 : case 0:
3556 : 286 : fputs_unlocked (_(" 0 *local* "),
3557 : : stdout);
3558 : 286 : break;
3559 : :
3560 : 422 : case 1:
3561 : 422 : fputs_unlocked (_(" 1 *global* "),
3562 : : stdout);
3563 : 422 : break;
3564 : :
3565 : 2414 : default:
3566 [ + - ]: 4828 : n = printf ("%4d%c%s",
3567 [ + - ]: 2414 : *sym & 0x7fff, *sym & 0x8000 ? 'h' : ' ',
3568 : : (vername != NULL
3569 [ + - ]: 2414 : && (unsigned int) (*sym & 0x7fff) < nvername)
3570 : 2414 : ? vername[*sym & 0x7fff] : "???");
3571 [ + - ]: 2414 : if ((unsigned int) (*sym & 0x7fff) < nvername
3572 [ + - + + ]: 2414 : && filename != NULL && filename[*sym & 0x7fff] != NULL)
3573 : 2110 : n += printf ("(%s)", filename[*sym & 0x7fff]);
3574 : 2414 : printf ("%*s", MAX (0, 33 - (int) n), " ");
3575 : 2414 : break;
3576 : : }
3577 : : }
3578 [ - + ]: 308 : putchar_unlocked ('\n');
3579 : : }
3580 : :
3581 : :
3582 : : static void
3583 : 154 : print_hash_info (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr, size_t shstrndx,
3584 : : uint_fast32_t maxlength, Elf32_Word nbucket,
3585 : : uint_fast32_t nsyms, uint32_t *lengths, const char *extrastr)
3586 : : {
3587 : 154 : uint32_t *counts = xcalloc (maxlength + 1, sizeof (uint32_t));
3588 : :
3589 [ + + ]: 568 : for (Elf32_Word cnt = 0; cnt < nbucket; ++cnt)
3590 : 414 : ++counts[lengths[cnt]];
3591 : :
3592 : 154 : GElf_Shdr glink_mem;
3593 : 154 : GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf,
3594 : 154 : shdr->sh_link),
3595 : : &glink_mem);
3596 [ - + ]: 154 : if (glink == NULL)
3597 : : {
3598 : 0 : error (0, 0, _("invalid sh_link value in section %zu"),
3599 : : elf_ndxscn (scn));
3600 : 0 : return;
3601 : : }
3602 : :
3603 [ + + ]: 308 : printf (ngettext ("\
3604 : : \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",
3605 : : "\
3606 : : \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",
3607 : : nbucket),
3608 : 154 : (unsigned int) elf_ndxscn (scn),
3609 : 154 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
3610 : : (int) nbucket,
3611 : 154 : gelf_getclass (ebl->elf) == ELFCLASS32 ? 10 : 18,
3612 : : shdr->sh_addr,
3613 : : shdr->sh_offset,
3614 : 154 : (unsigned int) shdr->sh_link,
3615 : 154 : elf_strptr (ebl->elf, shstrndx, glink->sh_name));
3616 : :
3617 [ + - ]: 154 : if (extrastr != NULL)
3618 : 154 : fputs (extrastr, stdout);
3619 : :
3620 [ + - ]: 154 : if (likely (nbucket > 0))
3621 : : {
3622 : 154 : uint64_t success = 0;
3623 : :
3624 : : /* xgettext:no-c-format */
3625 : 154 : fputs_unlocked (_("\
3626 : : Length Number % of total Coverage\n"), stdout);
3627 : 154 : printf (_(" 0 %6" PRIu32 " %5.1f%%\n"),
3628 : 154 : counts[0], (counts[0] * 100.0) / nbucket);
3629 : :
3630 : 154 : uint64_t nzero_counts = 0;
3631 [ + + ]: 216 : for (Elf32_Word cnt = 1; cnt <= maxlength; ++cnt)
3632 : : {
3633 : 62 : nzero_counts += counts[cnt] * cnt;
3634 : 62 : printf (_("\
3635 : : %7d %6" PRIu32 " %5.1f%% %5.1f%%\n"),
3636 : 62 : (int) cnt, counts[cnt], (counts[cnt] * 100.0) / nbucket,
3637 : 62 : (nzero_counts * 100.0) / nsyms);
3638 : : }
3639 : :
3640 : : Elf32_Word acc = 0;
3641 [ + + ]: 216 : for (Elf32_Word cnt = 1; cnt <= maxlength; ++cnt)
3642 : : {
3643 : 62 : acc += cnt;
3644 : 62 : success += counts[cnt] * acc;
3645 : : }
3646 : :
3647 : 154 : printf (_("\
3648 : : Average number of tests: successful lookup: %f\n\
3649 : : unsuccessful lookup: %f\n"),
3650 : 154 : (double) success / (double) nzero_counts,
3651 : 154 : (double) nzero_counts / (double) nbucket);
3652 : : }
3653 : :
3654 : 154 : free (counts);
3655 : : }
3656 : :
3657 : :
3658 : : /* This function handles the traditional System V-style hash table format. */
3659 : : static void
3660 : 0 : handle_sysv_hash (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr, size_t shstrndx)
3661 : : {
3662 : 0 : Elf_Data *data = elf_getdata (scn, NULL);
3663 [ # # ]: 0 : if (unlikely (data == NULL))
3664 : : {
3665 : 0 : error (0, 0, _("cannot get data for section %d: %s"),
3666 : 0 : (int) elf_ndxscn (scn), elf_errmsg (-1));
3667 : 0 : return;
3668 : : }
3669 : :
3670 [ # # ]: 0 : if (unlikely (data->d_size < 2 * sizeof (Elf32_Word)))
3671 : : {
3672 : 0 : invalid_data:
3673 : 0 : error (0, 0, _("invalid data in sysv.hash section %d"),
3674 : 0 : (int) elf_ndxscn (scn));
3675 : 0 : return;
3676 : : }
3677 : :
3678 : 0 : Elf32_Word nbucket = ((Elf32_Word *) data->d_buf)[0];
3679 : 0 : Elf32_Word nchain = ((Elf32_Word *) data->d_buf)[1];
3680 : :
3681 : 0 : uint64_t used_buf = (2ULL + nchain + nbucket) * sizeof (Elf32_Word);
3682 [ # # ]: 0 : if (used_buf > data->d_size)
3683 : 0 : goto invalid_data;
3684 : :
3685 : 0 : Elf32_Word *bucket = &((Elf32_Word *) data->d_buf)[2];
3686 : 0 : Elf32_Word *chain = &((Elf32_Word *) data->d_buf)[2 + nbucket];
3687 : :
3688 : 0 : uint32_t *lengths = xcalloc (nbucket, sizeof (uint32_t));
3689 : :
3690 : 0 : uint_fast32_t maxlength = 0;
3691 : 0 : uint_fast32_t nsyms = 0;
3692 [ # # ]: 0 : for (Elf32_Word cnt = 0; cnt < nbucket; ++cnt)
3693 : : {
3694 : 0 : Elf32_Word inner = bucket[cnt];
3695 : 0 : Elf32_Word chain_len = 0;
3696 [ # # ]: 0 : while (inner > 0 && inner < nchain)
3697 : : {
3698 : 0 : ++nsyms;
3699 : 0 : ++chain_len;
3700 [ # # ]: 0 : if (chain_len > nchain)
3701 : : {
3702 : 0 : error (0, 0, _("invalid chain in sysv.hash section %d"),
3703 : 0 : (int) elf_ndxscn (scn));
3704 : 0 : free (lengths);
3705 : 0 : return;
3706 : : }
3707 [ # # ]: 0 : if (maxlength < ++lengths[cnt])
3708 : 0 : ++maxlength;
3709 : :
3710 : 0 : inner = chain[inner];
3711 : : }
3712 : : }
3713 : :
3714 : 0 : print_hash_info (ebl, scn, shdr, shstrndx, maxlength, nbucket, nsyms,
3715 : : lengths, NULL);
3716 : :
3717 : 0 : free (lengths);
3718 : : }
3719 : :
3720 : :
3721 : : /* This function handles the incorrect, System V-style hash table
3722 : : format some 64-bit architectures use. */
3723 : : static void
3724 : 0 : handle_sysv_hash64 (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr, size_t shstrndx)
3725 : : {
3726 : 0 : Elf_Data *data = elf_getdata (scn, NULL);
3727 [ # # ]: 0 : if (unlikely (data == NULL))
3728 : : {
3729 : 0 : error (0, 0, _("cannot get data for section %d: %s"),
3730 : 0 : (int) elf_ndxscn (scn), elf_errmsg (-1));
3731 : 0 : return;
3732 : : }
3733 : :
3734 [ # # ]: 0 : if (unlikely (data->d_size < 2 * sizeof (Elf64_Xword)))
3735 : : {
3736 : 0 : invalid_data:
3737 : 0 : error (0, 0, _("invalid data in sysv.hash64 section %d"),
3738 : 0 : (int) elf_ndxscn (scn));
3739 : 0 : return;
3740 : : }
3741 : :
3742 : 0 : Elf64_Xword nbucket = ((Elf64_Xword *) data->d_buf)[0];
3743 : 0 : Elf64_Xword nchain = ((Elf64_Xword *) data->d_buf)[1];
3744 : :
3745 : 0 : uint64_t maxwords = data->d_size / sizeof (Elf64_Xword);
3746 : 0 : if (maxwords < 2
3747 [ # # ]: 0 : || maxwords - 2 < nbucket
3748 [ # # ]: 0 : || maxwords - 2 - nbucket < nchain)
3749 : 0 : goto invalid_data;
3750 : :
3751 : 0 : Elf64_Xword *bucket = &((Elf64_Xword *) data->d_buf)[2];
3752 : 0 : Elf64_Xword *chain = &((Elf64_Xword *) data->d_buf)[2 + nbucket];
3753 : :
3754 : 0 : uint32_t *lengths = xcalloc (nbucket, sizeof (uint32_t));
3755 : :
3756 : 0 : uint_fast32_t maxlength = 0;
3757 : 0 : uint_fast32_t nsyms = 0;
3758 [ # # ]: 0 : for (Elf64_Xword cnt = 0; cnt < nbucket; ++cnt)
3759 : : {
3760 : 0 : Elf64_Xword inner = bucket[cnt];
3761 : 0 : Elf64_Xword chain_len = 0;
3762 [ # # ]: 0 : while (inner > 0 && inner < nchain)
3763 : : {
3764 : 0 : ++nsyms;
3765 : 0 : ++chain_len;
3766 [ # # ]: 0 : if (chain_len > nchain)
3767 : : {
3768 : 0 : error (0, 0, _("invalid chain in sysv.hash64 section %d"),
3769 : 0 : (int) elf_ndxscn (scn));
3770 : 0 : free (lengths);
3771 : 0 : return;
3772 : : }
3773 [ # # ]: 0 : if (maxlength < ++lengths[cnt])
3774 : 0 : ++maxlength;
3775 : :
3776 : 0 : inner = chain[inner];
3777 : : }
3778 : : }
3779 : :
3780 : 0 : print_hash_info (ebl, scn, shdr, shstrndx, maxlength, nbucket, nsyms,
3781 : : lengths, NULL);
3782 : :
3783 : 0 : free (lengths);
3784 : : }
3785 : :
3786 : :
3787 : : /* This function handles the GNU-style hash table format. */
3788 : : static void
3789 : 154 : handle_gnu_hash (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr, size_t shstrndx)
3790 : : {
3791 : 154 : uint32_t *lengths = NULL;
3792 : 154 : Elf_Data *data = elf_getdata (scn, NULL);
3793 [ - + ]: 154 : if (unlikely (data == NULL))
3794 : : {
3795 : 0 : error (0, 0, _("cannot get data for section %d: %s"),
3796 : 0 : (int) elf_ndxscn (scn), elf_errmsg (-1));
3797 : 0 : return;
3798 : : }
3799 : :
3800 [ - + ]: 154 : if (unlikely (data->d_size < 4 * sizeof (Elf32_Word)))
3801 : : {
3802 : 0 : invalid_data:
3803 : 0 : free (lengths);
3804 : 0 : error (0, 0, _("invalid data in gnu.hash section %d"),
3805 : 0 : (int) elf_ndxscn (scn));
3806 : 0 : return;
3807 : : }
3808 : :
3809 : 154 : Elf32_Word nbucket = ((Elf32_Word *) data->d_buf)[0];
3810 : 154 : Elf32_Word symbias = ((Elf32_Word *) data->d_buf)[1];
3811 : :
3812 : : /* Next comes the size of the bitmap. It's measured in words for
3813 : : the architecture. It's 32 bits for 32 bit archs, and 64 bits for
3814 : : 64 bit archs. There is always a bloom filter present, so zero is
3815 : : an invalid value. */
3816 : 154 : Elf32_Word bitmask_words = ((Elf32_Word *) data->d_buf)[2];
3817 [ + + ]: 154 : if (gelf_getclass (ebl->elf) == ELFCLASS64)
3818 : 134 : bitmask_words *= 2;
3819 : :
3820 [ - + ]: 154 : if (bitmask_words == 0)
3821 : 0 : goto invalid_data;
3822 : :
3823 : 154 : Elf32_Word shift = ((Elf32_Word *) data->d_buf)[3];
3824 : :
3825 : : /* Is there still room for the sym chain?
3826 : : Use uint64_t calculation to prevent 32bit overflow. */
3827 : 154 : uint64_t used_buf = (4ULL + bitmask_words + nbucket) * sizeof (Elf32_Word);
3828 : 154 : uint32_t max_nsyms = (data->d_size - used_buf) / sizeof (Elf32_Word);
3829 [ - + ]: 154 : if (used_buf > data->d_size)
3830 : 0 : goto invalid_data;
3831 : :
3832 : 154 : lengths = xcalloc (nbucket, sizeof (uint32_t));
3833 : :
3834 : 154 : Elf32_Word *bitmask = &((Elf32_Word *) data->d_buf)[4];
3835 : 154 : Elf32_Word *bucket = &((Elf32_Word *) data->d_buf)[4 + bitmask_words];
3836 : 154 : Elf32_Word *chain = &((Elf32_Word *) data->d_buf)[4 + bitmask_words
3837 : 154 : + nbucket];
3838 : :
3839 : : /* Compute distribution of chain lengths. */
3840 : 154 : uint_fast32_t maxlength = 0;
3841 : 154 : uint_fast32_t nsyms = 0;
3842 [ + + ]: 568 : for (Elf32_Word cnt = 0; cnt < nbucket; ++cnt)
3843 [ + + ]: 414 : if (bucket[cnt] != 0)
3844 : : {
3845 : 196 : Elf32_Word inner = bucket[cnt] - symbias;
3846 : 364 : do
3847 : : {
3848 : 364 : ++nsyms;
3849 [ + + ]: 364 : if (maxlength < ++lengths[cnt])
3850 : 62 : ++maxlength;
3851 [ - + ]: 364 : if (inner >= max_nsyms)
3852 : 0 : goto invalid_data;
3853 : : }
3854 [ + + ]: 364 : while ((chain[inner++] & 1) == 0);
3855 : : }
3856 : :
3857 : : /* Count bits in bitmask. */
3858 : : uint_fast32_t nbits = 0;
3859 [ + + ]: 514 : for (Elf32_Word cnt = 0; cnt < bitmask_words; ++cnt)
3860 : : {
3861 : 360 : uint_fast32_t word = bitmask[cnt];
3862 : :
3863 : 360 : word = (word & 0x55555555) + ((word >> 1) & 0x55555555);
3864 : 360 : word = (word & 0x33333333) + ((word >> 2) & 0x33333333);
3865 : 360 : word = (word & 0x0f0f0f0f) + ((word >> 4) & 0x0f0f0f0f);
3866 : 360 : word = (word & 0x00ff00ff) + ((word >> 8) & 0x00ff00ff);
3867 : 360 : nbits += (word & 0x0000ffff) + ((word >> 16) & 0x0000ffff);
3868 : : }
3869 : :
3870 : 308 : char *str = xasprintf (_("\
3871 : : Symbol Bias: %u\n\
3872 : : Bitmask Size: %zu bytes %" PRIuFAST32 "%% bits set 2nd hash shift: %u\n"),
3873 : : (unsigned int) symbias,
3874 : : bitmask_words * sizeof (Elf32_Word),
3875 : 154 : ((nbits * 100 + 50)
3876 : 154 : / (uint_fast32_t) (bitmask_words
3877 : : * sizeof (Elf32_Word) * 8)),
3878 : : (unsigned int) shift);
3879 : :
3880 : 154 : print_hash_info (ebl, scn, shdr, shstrndx, maxlength, nbucket, nsyms,
3881 : : lengths, str);
3882 : :
3883 : 154 : free (str);
3884 : 154 : free (lengths);
3885 : : }
3886 : :
3887 : :
3888 : : /* Find the symbol table(s). For this we have to search through the
3889 : : section table. */
3890 : : static void
3891 : 220 : handle_hash (Ebl *ebl)
3892 : : {
3893 : : /* Get the section header string table index. */
3894 : 220 : size_t shstrndx;
3895 [ - + ]: 220 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
3896 : 0 : error_exit (0, _("cannot get section header string table index"));
3897 : :
3898 : : Elf_Scn *scn = NULL;
3899 [ + + ]: 7136 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
3900 : : {
3901 : : /* Handle the section if it is a symbol table. */
3902 : 6916 : GElf_Shdr shdr_mem;
3903 : 6916 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
3904 : :
3905 [ + - ]: 6916 : if (likely (shdr != NULL))
3906 : : {
3907 [ + + ]: 6916 : if ((shdr->sh_type == SHT_HASH || shdr->sh_type == SHT_GNU_HASH)
3908 [ - + ]: 154 : && (shdr->sh_flags & SHF_COMPRESSED) != 0)
3909 : : {
3910 [ # # ]: 0 : if (elf_compress (scn, 0, 0) < 0)
3911 : 0 : printf ("WARNING: %s [%zd]\n",
3912 : : _("Couldn't uncompress section"),
3913 : : elf_ndxscn (scn));
3914 : 0 : shdr = gelf_getshdr (scn, &shdr_mem);
3915 [ # # ]: 0 : if (unlikely (shdr == NULL))
3916 : 0 : error_exit (0, _("cannot get section [%zd] header: %s"),
3917 : : elf_ndxscn (scn), elf_errmsg (-1));
3918 : : }
3919 : :
3920 [ - + ]: 6916 : if (shdr->sh_type == SHT_HASH)
3921 : : {
3922 [ # # ]: 0 : if (ebl_sysvhash_entrysize (ebl) == sizeof (Elf64_Xword))
3923 : 0 : handle_sysv_hash64 (ebl, scn, shdr, shstrndx);
3924 : : else
3925 : 0 : handle_sysv_hash (ebl, scn, shdr, shstrndx);
3926 : : }
3927 [ + + ]: 6916 : else if (shdr->sh_type == SHT_GNU_HASH)
3928 : 154 : handle_gnu_hash (ebl, scn, shdr, shstrndx);
3929 : : }
3930 : : }
3931 : 220 : }
3932 : :
3933 : :
3934 : : static void
3935 : 228 : print_liblist (Ebl *ebl)
3936 : : {
3937 : : /* Find the library list sections. For this we have to search
3938 : : through the section table. */
3939 : 228 : Elf_Scn *scn = NULL;
3940 : :
3941 : : /* Get the section header string table index. */
3942 : 228 : size_t shstrndx;
3943 [ - + ]: 228 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
3944 : 0 : error_exit (0, _("cannot get section header string table index"));
3945 : :
3946 [ + + ]: 7262 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
3947 : : {
3948 : 7034 : GElf_Shdr shdr_mem;
3949 : 7034 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
3950 : :
3951 [ + - - + ]: 7034 : if (shdr != NULL && shdr->sh_type == SHT_GNU_LIBLIST)
3952 : : {
3953 : 0 : size_t sh_entsize = gelf_fsize (ebl->elf, ELF_T_LIB, 1, EV_CURRENT);
3954 : 0 : int nentries = shdr->sh_size / sh_entsize;
3955 : 0 : printf (ngettext ("\
3956 : : \nLibrary list section [%2zu] '%s' at offset %#0" PRIx64 " contains %d entry:\n",
3957 : : "\
3958 : : \nLibrary list section [%2zu] '%s' at offset %#0" PRIx64 " contains %d entries:\n",
3959 : : nentries),
3960 : : elf_ndxscn (scn),
3961 : 0 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
3962 : : shdr->sh_offset,
3963 : : nentries);
3964 : :
3965 : 0 : Elf_Data *data = elf_getdata (scn, NULL);
3966 [ # # ]: 0 : if (data == NULL)
3967 : 0 : return;
3968 : :
3969 : 0 : puts (_("\
3970 : : Library Time Stamp Checksum Version Flags"));
3971 : :
3972 [ # # ]: 0 : for (int cnt = 0; cnt < nentries; ++cnt)
3973 : : {
3974 : 0 : GElf_Lib lib_mem;
3975 : 0 : GElf_Lib *lib = gelf_getlib (data, cnt, &lib_mem);
3976 [ # # ]: 0 : if (unlikely (lib == NULL))
3977 : 0 : continue;
3978 : :
3979 : 0 : time_t t = (time_t) lib->l_time_stamp;
3980 : 0 : struct tm *tm = gmtime (&t);
3981 [ # # ]: 0 : if (unlikely (tm == NULL))
3982 : 0 : continue;
3983 : :
3984 : 0 : printf (" [%2d] %-29s %04u-%02u-%02uT%02u:%02u:%02u %08x %-7u %u\n",
3985 : 0 : cnt, elf_strptr (ebl->elf, shdr->sh_link, lib->l_name),
3986 : 0 : tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
3987 : : tm->tm_hour, tm->tm_min, tm->tm_sec,
3988 : 0 : (unsigned int) lib->l_checksum,
3989 : 0 : (unsigned int) lib->l_version,
3990 : 0 : (unsigned int) lib->l_flags);
3991 : : }
3992 : : }
3993 : : }
3994 : : }
3995 : :
3996 : : static inline size_t
3997 : 16 : left (Elf_Data *data,
3998 : : const unsigned char *p)
3999 : : {
4000 : 16 : return (const unsigned char *) data->d_buf + data->d_size - p;
4001 : : }
4002 : :
4003 : : static void
4004 : 228 : print_attributes (Ebl *ebl, const GElf_Ehdr *ehdr)
4005 : : {
4006 : : /* Find the object attributes sections. For this we have to search
4007 : : through the section table. */
4008 : 228 : Elf_Scn *scn = NULL;
4009 : :
4010 : : /* Get the section header string table index. */
4011 : 228 : size_t shstrndx;
4012 [ - + ]: 228 : if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
4013 : 0 : error_exit (0, _("cannot get section header string table index"));
4014 : :
4015 [ + + ]: 7262 : while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
4016 : : {
4017 : 7034 : GElf_Shdr shdr_mem;
4018 : 7034 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
4019 : :
4020 [ + - + + ]: 7034 : if (shdr == NULL || (shdr->sh_type != SHT_GNU_ATTRIBUTES
4021 [ + + ]: 7028 : && (shdr->sh_type != SHT_ARM_ATTRIBUTES
4022 [ - + ]: 2 : || ehdr->e_machine != EM_ARM)
4023 [ + + ]: 7026 : && (shdr->sh_type != SHT_CSKY_ATTRIBUTES
4024 [ + - ]: 2 : || ehdr->e_machine != EM_CSKY)
4025 [ - + ]: 7026 : && (shdr->sh_type != SHT_RISCV_ATTRIBUTES
4026 [ # # ]: 0 : || ehdr->e_machine != EM_RISCV)))
4027 : 7026 : continue;
4028 : :
4029 : 8 : printf (_("\
4030 : : \nObject attributes section [%2zu] '%s' of %" PRIu64
4031 : : " bytes at offset %#0" PRIx64 ":\n"),
4032 : : elf_ndxscn (scn),
4033 : 8 : elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
4034 : : shdr->sh_size, shdr->sh_offset);
4035 : :
4036 : 8 : Elf_Data *data = elf_rawdata (scn, NULL);
4037 [ + - + - ]: 8 : if (unlikely (data == NULL || data->d_size == 0))
4038 : 0 : return;
4039 : :
4040 : 8 : const unsigned char *p = data->d_buf;
4041 : :
4042 : : /* There is only one 'version', A. */
4043 [ + - ]: 8 : if (unlikely (*p++ != 'A'))
4044 : : return;
4045 : :
4046 : 8 : fputs_unlocked (_(" Owner Size\n"), stdout);
4047 : :
4048 : : /* Loop over the sections. */
4049 [ + + ]: 16 : while (left (data, p) >= 4)
4050 : : {
4051 : : /* Section length. */
4052 : 8 : uint32_t len;
4053 [ + + ]: 8 : memcpy (&len, p, sizeof len);
4054 : :
4055 [ + + ]: 8 : if (MY_ELFDATA != ehdr->e_ident[EI_DATA])
4056 : 4 : CONVERT (len);
4057 : :
4058 [ + - ]: 8 : if (unlikely (len > left (data, p)))
4059 : : break;
4060 : :
4061 : : /* Section vendor name. */
4062 : 8 : const unsigned char *name = p + sizeof len;
4063 : 8 : p += len;
4064 : :
4065 : 8 : unsigned const char *q = memchr (name, '\0', len);
4066 [ + - ]: 8 : if (unlikely (q == NULL))
4067 : : break;
4068 : 8 : ++q;
4069 : :
4070 : 8 : printf (_(" %-13s %4" PRIu32 "\n"), name, len);
4071 : :
4072 : 16 : bool gnu_vendor = (q - name == sizeof "gnu"
4073 [ + + - + ]: 8 : && !memcmp (name, "gnu", sizeof "gnu"));
4074 : :
4075 : : /* Loop over subsections. */
4076 [ + + ]: 8 : if (shdr->sh_type != SHT_GNU_ATTRIBUTES
4077 [ + - ]: 6 : || gnu_vendor)
4078 [ + + ]: 16 : while (q < p)
4079 : : {
4080 : 8 : const unsigned char *const sub = q;
4081 : :
4082 : 8 : unsigned int subsection_tag;
4083 : 8 : get_uleb128 (subsection_tag, q, p);
4084 [ + - ]: 8 : if (unlikely (q >= p))
4085 : : break;
4086 : :
4087 : 8 : uint32_t subsection_len;
4088 [ + - ]: 8 : if (unlikely (p - sub < (ptrdiff_t) sizeof subsection_len))
4089 : : break;
4090 : :
4091 [ + + ]: 8 : memcpy (&subsection_len, q, sizeof subsection_len);
4092 : :
4093 [ + + ]: 8 : if (MY_ELFDATA != ehdr->e_ident[EI_DATA])
4094 : 4 : CONVERT (subsection_len);
4095 : :
4096 : : /* Don't overflow, ptrdiff_t might be 32bits, but signed. */
4097 [ + - + - ]: 8 : if (unlikely (subsection_len == 0
4098 : : || subsection_len >= (uint32_t) PTRDIFF_MAX
4099 : : || p - sub < (ptrdiff_t) subsection_len))
4100 : : break;
4101 : :
4102 : 8 : const unsigned char *r = q + sizeof subsection_len;
4103 : 8 : q = sub + subsection_len;
4104 : :
4105 [ - + ]: 8 : switch (subsection_tag)
4106 : : {
4107 : 0 : default:
4108 : : /* Unknown subsection, print and skip. */
4109 : 0 : printf (_(" %-4u %12" PRIu32 "\n"),
4110 : : subsection_tag, subsection_len);
4111 : 0 : break;
4112 : :
4113 : 8 : case 1: /* Tag_File */
4114 : 8 : printf (_(" File: %11" PRIu32 "\n"),
4115 : : subsection_len);
4116 : :
4117 [ + + ]: 52 : while (r < q)
4118 : : {
4119 : 44 : unsigned int tag;
4120 : 44 : get_uleb128 (tag, r, q);
4121 [ + - ]: 44 : if (unlikely (r >= q))
4122 : : break;
4123 : :
4124 : : /* GNU style tags have either a uleb128 value,
4125 : : when lowest bit is not set, or a string
4126 : : when the lowest bit is set.
4127 : : "compatibility" (32) is special. It has
4128 : : both a string and a uleb128 value. For
4129 : : non-gnu we assume 6 till 31 only take ints.
4130 : : XXX see arm backend, do we need a separate
4131 : : hook? */
4132 : 44 : uint64_t value = 0;
4133 : 44 : const char *string = NULL;
4134 [ + - + + ]: 44 : if (tag == 32 || (tag & 1) == 0
4135 [ + - + + ]: 16 : || (! gnu_vendor && (tag > 5 && tag < 32)))
4136 : : {
4137 : : // Note r >= q check above.
4138 : 42 : get_uleb128 (value, r, q);
4139 [ + - ]: 42 : if (r > q)
4140 : : break;
4141 : : }
4142 [ + - ]: 44 : if (tag == 32
4143 [ + + ]: 44 : || ((tag & 1) != 0
4144 [ + - ]: 16 : && (gnu_vendor
4145 [ + - ]: 16 : || (! gnu_vendor && tag > 32)))
4146 [ + + + + ]: 44 : || (! gnu_vendor && tag > 3 && tag < 6))
4147 : : {
4148 : 2 : string = (const char *) r;
4149 : 2 : r = memchr (r, '\0', q - r);
4150 [ + - ]: 2 : if (r == NULL)
4151 : : break;
4152 : 2 : ++r;
4153 : : }
4154 : :
4155 : 44 : const char *tag_name = NULL;
4156 : 44 : const char *value_name = NULL;
4157 : 44 : ebl_check_object_attribute (ebl, (const char *) name,
4158 : : tag, value,
4159 : : &tag_name, &value_name);
4160 : :
4161 [ + - ]: 44 : if (tag_name != NULL)
4162 : : {
4163 [ - + ]: 44 : if (tag == 32)
4164 : 0 : printf (_(" %s: %" PRId64 ", %s\n"),
4165 : : tag_name, value, string);
4166 [ + + + + ]: 44 : else if (string == NULL && value_name == NULL)
4167 : 2 : printf (_(" %s: %" PRId64 "\n"),
4168 : : tag_name, value);
4169 : : else
4170 : 42 : printf (_(" %s: %s\n"),
4171 : : tag_name, string ?: value_name);
4172 : : }
4173 : : else
4174 : : {
4175 : : /* For "gnu" vendor 32 "compatibility" has
4176 : : already been handled above. */
4177 [ # # # # ]: 0 : assert (tag != 32
4178 : : || strcmp ((const char *) name, "gnu"));
4179 [ # # ]: 0 : if (string == NULL)
4180 : 0 : printf (_(" %u: %" PRId64 "\n"),
4181 : : tag, value);
4182 : : else
4183 : 0 : printf (_(" %u: %s\n"),
4184 : : tag, string);
4185 : : }
4186 : : }
4187 : : }
4188 : : }
4189 : : }
4190 : : }
4191 : : }
4192 : :
4193 : : /* Returns either the (relocated) data from the Dwarf, or tries to get
4194 : : the "raw" (uncompressed) data from the Elf section. Produces a
4195 : : warning if the data cannot be found (or decompressed). */
4196 : : static Elf_Data *
4197 : 774 : get_debug_elf_data (Dwarf *dbg, Ebl *ebl, int idx, Elf_Scn *scn)
4198 : : {
4199 : : /* We prefer to get the section data from the Dwarf because that
4200 : : might have been relocated already. Note this is subtly wrong if
4201 : : there are multiple sections with the same .debug name. */
4202 [ - + ]: 774 : if (dbg->sectiondata[idx] != NULL)
4203 : : return dbg->sectiondata[idx];
4204 : :
4205 : 0 : GElf_Shdr shdr_mem;
4206 : 0 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
4207 [ # # # # ]: 0 : if (shdr != NULL && (shdr->sh_flags & SHF_COMPRESSED) != 0)
4208 : : {
4209 [ # # ]: 0 : if (elf_compress (scn, 0, 0) < 0)
4210 : : {
4211 : 0 : error (0, 0, "%s [%zd] '%s'\n",
4212 : : _("Couldn't uncompress section"),
4213 : : elf_ndxscn (scn), section_name (ebl, shdr));
4214 : 0 : return NULL;
4215 : : }
4216 : : }
4217 : :
4218 : 0 : Elf_Data *data = elf_getdata (scn, NULL);
4219 [ # # ]: 0 : if (data == NULL)
4220 : 0 : error (0, 0, "%s [%zd] '%s': %s\n",
4221 : : _("Couldn't get data from section"),
4222 : : elf_ndxscn (scn), section_name (ebl, shdr), elf_errmsg (-1));
4223 : :
4224 : 0 : return elf_getdata (scn, NULL);
4225 : : }
4226 : :
4227 : : static void
4228 : 2076760 : print_dwarf_addr (Dwfl_Module *dwflmod,
4229 : : int address_size, Dwarf_Addr address, Dwarf_Addr raw)
4230 : : {
4231 : : /* See if there is a name we can give for this address. */
4232 : 2076760 : GElf_Sym sym;
4233 : 2076760 : GElf_Off off = 0;
4234 [ + + ]: 12706 : const char *name = (print_address_names && ! print_unresolved_addresses)
4235 : 12542 : ? dwfl_module_addrinfo (dwflmod, address, &off, &sym, NULL, NULL, NULL)
4236 [ + + ]: 2076760 : : NULL;
4237 : :
4238 : 2076760 : const char *scn;
4239 [ + + ]: 2076760 : if (print_unresolved_addresses)
4240 : : {
4241 : 168 : address = raw;
4242 : 168 : scn = NULL;
4243 : : }
4244 : : else
4245 : : {
4246 : : /* Relativize the address. */
4247 : 2076592 : int n = dwfl_module_relocations (dwflmod);
4248 [ + + ]: 2076592 : int i = n < 1 ? -1 : dwfl_module_relocate_address (dwflmod, &address);
4249 : :
4250 : : /* In an ET_REL file there is a section name to refer to. */
4251 : 259404 : scn = (i < 0 ? NULL
4252 [ - + ]: 259404 : : dwfl_module_relocation_info (dwflmod, i, NULL));
4253 : : }
4254 : :
4255 [ - + ]: 2076760 : if ((name != NULL
4256 : 12164 : ? (off != 0
4257 : : ? (scn != NULL
4258 : : ? (address_size == 0
4259 : 2 : ? printf ("%s+%#" PRIx64 " <%s+%#" PRIx64 ">",
4260 : : scn, address, name, off)
4261 : 8 : : printf ("%s+%#0*" PRIx64 " <%s+%#" PRIx64 ">",
4262 : 4 : scn, 2 + address_size * 2, address,
4263 : : name, off))
4264 : : : (address_size == 0
4265 : 364 : ? printf ("%#" PRIx64 " <%s+%#" PRIx64 ">",
4266 : : address, name, off)
4267 : 2252 : : printf ("%#0*" PRIx64 " <%s+%#" PRIx64 ">",
4268 : 1126 : 2 + address_size * 2, address,
4269 : : name, off)))
4270 : : : (scn != NULL
4271 : : ? (address_size == 0
4272 : 2 : ? printf ("%s+%#" PRIx64 " <%s>", scn, address, name)
4273 : 28 : : printf ("%s+%#0*" PRIx64 " <%s>",
4274 : 14 : scn, 2 + address_size * 2, address, name))
4275 : : : (address_size == 0
4276 : 160 : ? printf ("%#" PRIx64 " <%s>", address, name)
4277 : 20984 : : printf ("%#0*" PRIx64 " <%s>",
4278 : 10492 : 2 + address_size * 2, address, name))))
4279 : : : (scn != NULL
4280 : : ? (address_size == 0
4281 : 96588 : ? printf ("%s+%#" PRIx64, scn, address)
4282 : 162794 : : printf ("%s+%#0*" PRIx64, scn, 2 + address_size * 2, address))
4283 : : : (address_size == 0
4284 : 665286 : ? printf ("%#" PRIx64, address)
4285 [ + + + + : 3013592 : : printf ("%#0*" PRIx64, 2 + address_size * 2, address)))) < 0)
+ + + + +
+ + + + +
+ + + + +
+ + + ]
4286 : 0 : error_exit (0, _("sprintf failure"));
4287 : 2076760 : }
4288 : :
4289 : :
4290 : : static const char *
4291 : 2110380 : dwarf_tag_string (unsigned int tag)
4292 : : {
4293 [ - + + - : 2110380 : switch (tag)
- - - - +
+ + + + -
- - - - -
+ - + - +
- - + + -
+ - - - -
- - - + -
+ - + + +
- - - - -
- + - - +
- - - + -
+ + + + -
- - - - +
+ + + - +
+ + + - -
- ]
4294 : : {
4295 : : #define DWARF_ONE_KNOWN_DW_TAG(NAME, CODE) case CODE: return #NAME;
4296 : 2110380 : DWARF_ALL_KNOWN_DW_TAG
4297 : : #undef DWARF_ONE_KNOWN_DW_TAG
4298 : 0 : default:
4299 : 0 : return NULL;
4300 : : }
4301 : : }
4302 : :
4303 : :
4304 : : static const char *
4305 : 8183380 : dwarf_attr_string (unsigned int attrnum)
4306 : : {
4307 [ + - + - : 8183380 : switch (attrnum)
- - - + -
- + + + -
- - - + +
- + - - +
- + - + -
- - - - -
- - - - -
- - - - -
- - - + -
+ - + - +
- - - + +
- - - + -
+ - + + -
- + + + +
- + + + -
+ - - + -
+ - + + -
+ - - + +
+ + - - -
- - + + +
+ - + - +
- - - - +
+ - + - +
+ - + + +
+ + - - +
- - - + -
+ - - - -
+ + - + -
- - - + -
- - + + -
+ - - - -
+ - - - -
- - + + -
- - - - -
- + ]
4308 : : {
4309 : : #define DWARF_ONE_KNOWN_DW_AT(NAME, CODE) case CODE: return #NAME;
4310 : 8183378 : DWARF_ALL_KNOWN_DW_AT
4311 : : #undef DWARF_ONE_KNOWN_DW_AT
4312 : 0 : default:
4313 : 0 : return NULL;
4314 : : }
4315 : : }
4316 : :
4317 : :
4318 : : static const char *
4319 : 8193196 : dwarf_form_string (unsigned int form)
4320 : : {
4321 [ + + + + : 8193196 : switch (form)
+ - - - -
- + - - +
- + + + +
+ + + - +
+ - - + -
- + - - +
+ + + + +
- + - - -
- + - + ]
4322 : : {
4323 : : #define DWARF_ONE_KNOWN_DW_FORM(NAME, CODE) case CODE: return #NAME;
4324 : 8193192 : DWARF_ALL_KNOWN_DW_FORM
4325 : : #undef DWARF_ONE_KNOWN_DW_FORM
4326 : 0 : default:
4327 : 0 : return NULL;
4328 : : }
4329 : : }
4330 : :
4331 : :
4332 : : static const char *
4333 : 3534 : dwarf_lang_string (unsigned int lang)
4334 : : {
4335 [ + - - + : 3534 : switch (lang)
+ + + - -
- - - - -
- + - - -
- - - - -
- - - - -
- - - - -
- - - +
- ]
4336 : : {
4337 : : #define DWARF_ONE_KNOWN_DW_LANG(NAME, CODE) case CODE: return #NAME;
4338 : 3532 : DWARF_ALL_KNOWN_DW_LANG
4339 : : #undef DWARF_ONE_KNOWN_DW_LANG
4340 : 2 : default:
4341 : 2 : return NULL;
4342 : : }
4343 : : }
4344 : :
4345 : :
4346 : : static const char *
4347 : 6936 : dwarf_inline_string (unsigned int code)
4348 : : {
4349 : 6936 : static const char *const known[] =
4350 : : {
4351 : : #define DWARF_ONE_KNOWN_DW_INL(NAME, CODE) [CODE] = #NAME,
4352 : : DWARF_ALL_KNOWN_DW_INL
4353 : : #undef DWARF_ONE_KNOWN_DW_INL
4354 : : };
4355 : :
4356 : 6936 : if (likely (code < sizeof (known) / sizeof (known[0])))
4357 : 6936 : return known[code];
4358 : :
4359 : : return NULL;
4360 : : }
4361 : :
4362 : :
4363 : : static const char *
4364 : 51036 : dwarf_encoding_string (unsigned int code)
4365 : : {
4366 : 51036 : static const char *const known[] =
4367 : : {
4368 : : #define DWARF_ONE_KNOWN_DW_ATE(NAME, CODE) [CODE] = #NAME,
4369 : : DWARF_ALL_KNOWN_DW_ATE
4370 : : #undef DWARF_ONE_KNOWN_DW_ATE
4371 : : };
4372 : :
4373 : 51036 : if (likely (code < sizeof (known) / sizeof (known[0])))
4374 : 51036 : return known[code];
4375 : :
4376 : : return NULL;
4377 : : }
4378 : :
4379 : :
4380 : : static const char *
4381 : 0 : dwarf_access_string (unsigned int code)
4382 : : {
4383 : 0 : static const char *const known[] =
4384 : : {
4385 : : #define DWARF_ONE_KNOWN_DW_ACCESS(NAME, CODE) [CODE] = #NAME,
4386 : : DWARF_ALL_KNOWN_DW_ACCESS
4387 : : #undef DWARF_ONE_KNOWN_DW_ACCESS
4388 : : };
4389 : :
4390 : 0 : if (likely (code < sizeof (known) / sizeof (known[0])))
4391 : 0 : return known[code];
4392 : :
4393 : : return NULL;
4394 : : }
4395 : :
4396 : :
4397 : : static const char *
4398 : 0 : dwarf_defaulted_string (unsigned int code)
4399 : : {
4400 : 0 : static const char *const known[] =
4401 : : {
4402 : : #define DWARF_ONE_KNOWN_DW_DEFAULTED(NAME, CODE) [CODE] = #NAME,
4403 : : DWARF_ALL_KNOWN_DW_DEFAULTED
4404 : : #undef DWARF_ONE_KNOWN_DW_DEFAULTED
4405 : : };
4406 : :
4407 : 0 : if (likely (code < sizeof (known) / sizeof (known[0])))
4408 : 0 : return known[code];
4409 : :
4410 : : return NULL;
4411 : : }
4412 : :
4413 : :
4414 : : static const char *
4415 : 0 : dwarf_visibility_string (unsigned int code)
4416 : : {
4417 : 0 : static const char *const known[] =
4418 : : {
4419 : : #define DWARF_ONE_KNOWN_DW_VIS(NAME, CODE) [CODE] = #NAME,
4420 : : DWARF_ALL_KNOWN_DW_VIS
4421 : : #undef DWARF_ONE_KNOWN_DW_VIS
4422 : : };
4423 : :
4424 : 0 : if (likely (code < sizeof (known) / sizeof (known[0])))
4425 : 0 : return known[code];
4426 : :
4427 : : return NULL;
4428 : : }
4429 : :
4430 : :
4431 : : static const char *
4432 : 0 : dwarf_virtuality_string (unsigned int code)
4433 : : {
4434 : 0 : static const char *const known[] =
4435 : : {
4436 : : #define DWARF_ONE_KNOWN_DW_VIRTUALITY(NAME, CODE) [CODE] = #NAME,
4437 : : DWARF_ALL_KNOWN_DW_VIRTUALITY
4438 : : #undef DWARF_ONE_KNOWN_DW_VIRTUALITY
4439 : : };
4440 : :
4441 : 0 : if (likely (code < sizeof (known) / sizeof (known[0])))
4442 : 0 : return known[code];
4443 : :
4444 : : return NULL;
4445 : : }
4446 : :
4447 : :
4448 : : static const char *
4449 : 0 : dwarf_identifier_case_string (unsigned int code)
4450 : : {
4451 : 0 : static const char *const known[] =
4452 : : {
4453 : : #define DWARF_ONE_KNOWN_DW_ID(NAME, CODE) [CODE] = #NAME,
4454 : : DWARF_ALL_KNOWN_DW_ID
4455 : : #undef DWARF_ONE_KNOWN_DW_ID
4456 : : };
4457 : :
4458 : 0 : if (likely (code < sizeof (known) / sizeof (known[0])))
4459 : 0 : return known[code];
4460 : :
4461 : : return NULL;
4462 : : }
4463 : :
4464 : :
4465 : : static const char *
4466 : 0 : dwarf_calling_convention_string (unsigned int code)
4467 : : {
4468 : 0 : static const char *const known[] =
4469 : : {
4470 : : #define DWARF_ONE_KNOWN_DW_CC(NAME, CODE) [CODE] = #NAME,
4471 : : DWARF_ALL_KNOWN_DW_CC
4472 : : #undef DWARF_ONE_KNOWN_DW_CC
4473 : : };
4474 : :
4475 : 0 : if (likely (code < sizeof (known) / sizeof (known[0])))
4476 : 0 : return known[code];
4477 : :
4478 : : return NULL;
4479 : : }
4480 : :
4481 : :
4482 : : static const char *
4483 : 0 : dwarf_ordering_string (unsigned int code)
4484 : : {
4485 : 0 : static const char *const known[] =
4486 : : {
4487 : : #define DWARF_ONE_KNOWN_DW_ORD(NAME, CODE) [CODE] = #NAME,
4488 : : DWARF_ALL_KNOWN_DW_ORD
4489 : : #undef DWARF_ONE_KNOWN_DW_ORD
4490 : : };
4491 : :
4492 : 0 : if (likely (code < sizeof (known) / sizeof (known[0])))
4493 : 0 : return known[code];
4494 : :
4495 : : return NULL;
4496 : : }
4497 : :
4498 : :
4499 : : static const char *
4500 : 16 : dwarf_discr_list_string (unsigned int code)
4501 : : {
4502 : 16 : static const char *const known[] =
4503 : : {
4504 : : #define DWARF_ONE_KNOWN_DW_DSC(NAME, CODE) [CODE] = #NAME,
4505 : : DWARF_ALL_KNOWN_DW_DSC
4506 : : #undef DWARF_ONE_KNOWN_DW_DSC
4507 : : };
4508 : :
4509 : 16 : if (likely (code < sizeof (known) / sizeof (known[0])))
4510 : 16 : return known[code];
4511 : :
4512 : : return NULL;
4513 : : }
4514 : :
4515 : :
4516 : : static const char *
4517 : 1061726 : dwarf_locexpr_opcode_string (unsigned int code)
4518 : : {
4519 : 1061726 : static const char *const known[] =
4520 : : {
4521 : : /* Normally we can't afford building huge table of 64K entries,
4522 : : most of them zero, just because there are a couple defined
4523 : : values at the far end. In case of opcodes, it's OK. */
4524 : : #define DWARF_ONE_KNOWN_DW_OP(NAME, CODE) [CODE] = #NAME,
4525 : : DWARF_ALL_KNOWN_DW_OP
4526 : : #undef DWARF_ONE_KNOWN_DW_OP
4527 : : };
4528 : :
4529 : 1061726 : if (likely (code < sizeof (known) / sizeof (known[0])))
4530 : 1061726 : return known[code];
4531 : :
4532 : : return NULL;
4533 : : }
4534 : :
4535 : :
4536 : : static const char *
4537 : 3284 : dwarf_unit_string (unsigned int type)
4538 : : {
4539 : 3284 : switch (type)
4540 : : {
4541 : : #define DWARF_ONE_KNOWN_DW_UT(NAME, CODE) case CODE: return #NAME;
4542 : : DWARF_ALL_KNOWN_DW_UT
4543 : : #undef DWARF_ONE_KNOWN_DW_UT
4544 : : default:
4545 : : return NULL;
4546 : : }
4547 : : }
4548 : :
4549 : :
4550 : : static const char *
4551 : 126232 : dwarf_range_list_encoding_string (unsigned int kind)
4552 : : {
4553 : 126232 : switch (kind)
4554 : : {
4555 : : #define DWARF_ONE_KNOWN_DW_RLE(NAME, CODE) case CODE: return #NAME;
4556 : : DWARF_ALL_KNOWN_DW_RLE
4557 : : #undef DWARF_ONE_KNOWN_DW_RLE
4558 : : default:
4559 : : return NULL;
4560 : : }
4561 : : }
4562 : :
4563 : :
4564 : : static const char *
4565 : 594948 : dwarf_loc_list_encoding_string (unsigned int kind)
4566 : : {
4567 : 594948 : switch (kind)
4568 : : {
4569 : : #define DWARF_ONE_KNOWN_DW_LLE(NAME, CODE) case CODE: return #NAME;
4570 : : DWARF_ALL_KNOWN_DW_LLE
4571 : : #undef DWARF_ONE_KNOWN_DW_LLE
4572 : : /* DW_LLE_GNU_view_pair is special/incompatible with default codes. */
4573 : : case DW_LLE_GNU_view_pair: return "GNU_view_pair";
4574 : : default:
4575 : : return NULL;
4576 : : }
4577 : : }
4578 : :
4579 : :
4580 : : static const char *
4581 : 9816 : dwarf_line_content_description_string (unsigned int kind)
4582 : : {
4583 : 9816 : switch (kind)
4584 : : {
4585 : : #define DWARF_ONE_KNOWN_DW_LNCT(NAME, CODE) case CODE: return #NAME;
4586 : : DWARF_ALL_KNOWN_DW_LNCT
4587 : : #undef DWARF_ONE_KNOWN_DW_LNCT
4588 : : default:
4589 : : return NULL;
4590 : : }
4591 : : }
4592 : :
4593 : :
4594 : : /* Used by all dwarf_foo_name functions. */
4595 : : static const char *
4596 : 19282758 : string_or_unknown (const char *known, unsigned int code,
4597 : : unsigned int lo_user, unsigned int hi_user,
4598 : : bool print_unknown_num)
4599 : : {
4600 : 19282758 : static char unknown_buf[20];
4601 : :
4602 [ + + ]: 19282758 : if (likely (known != NULL))
4603 : : return known;
4604 : :
4605 [ - + - - ]: 2 : if (lo_user != 0 && code >= lo_user && code <= hi_user)
4606 : : {
4607 : 0 : snprintf (unknown_buf, sizeof unknown_buf, "lo_user+%#x",
4608 : : code - lo_user);
4609 : 0 : return unknown_buf;
4610 : : }
4611 : :
4612 [ + - ]: 2 : if (print_unknown_num)
4613 : : {
4614 : 2 : snprintf (unknown_buf, sizeof unknown_buf, "??? (%#x)", code);
4615 : 2 : return unknown_buf;
4616 : : }
4617 : :
4618 : : return "???";
4619 : : }
4620 : :
4621 : :
4622 : : static const char *
4623 : 2110380 : dwarf_tag_name (unsigned int tag)
4624 : : {
4625 : 2110380 : const char *ret = dwarf_tag_string (tag);
4626 : 2110380 : return string_or_unknown (ret, tag, DW_TAG_lo_user, DW_TAG_hi_user, true);
4627 : : }
4628 : :
4629 : : static const char *
4630 : 8183380 : dwarf_attr_name (unsigned int attr)
4631 : : {
4632 : 8183380 : const char *ret = dwarf_attr_string (attr);
4633 : 8183380 : return string_or_unknown (ret, attr, DW_AT_lo_user, DW_AT_hi_user, true);
4634 : : }
4635 : :
4636 : :
4637 : : static const char *
4638 : 8193196 : dwarf_form_name (unsigned int form)
4639 : : {
4640 : 8193196 : const char *ret = dwarf_form_string (form);
4641 : 8193196 : return string_or_unknown (ret, form, 0, 0, true);
4642 : : }
4643 : :
4644 : :
4645 : : static const char *
4646 : 3530 : dwarf_lang_name (unsigned int lang)
4647 : : {
4648 : 3530 : const char *ret = dwarf_lang_string (lang);
4649 : 3530 : return string_or_unknown (ret, lang, DW_LANG_lo_user, DW_LANG_hi_user, false);
4650 : : }
4651 : :
4652 : :
4653 : : static const char *
4654 : 6936 : dwarf_inline_name (unsigned int code)
4655 : : {
4656 [ + - ]: 6936 : const char *ret = dwarf_inline_string (code);
4657 : 6936 : return string_or_unknown (ret, code, 0, 0, false);
4658 : : }
4659 : :
4660 : :
4661 : : static const char *
4662 : 51036 : dwarf_encoding_name (unsigned int code)
4663 : : {
4664 [ + - ]: 51036 : const char *ret = dwarf_encoding_string (code);
4665 : 51036 : return string_or_unknown (ret, code, DW_ATE_lo_user, DW_ATE_hi_user, false);
4666 : : }
4667 : :
4668 : :
4669 : : static const char *
4670 : 0 : dwarf_access_name (unsigned int code)
4671 : : {
4672 [ # # ]: 0 : const char *ret = dwarf_access_string (code);
4673 : 0 : return string_or_unknown (ret, code, 0, 0, false);
4674 : : }
4675 : :
4676 : :
4677 : : static const char *
4678 : 0 : dwarf_defaulted_name (unsigned int code)
4679 : : {
4680 [ # # ]: 0 : const char *ret = dwarf_defaulted_string (code);
4681 : 0 : return string_or_unknown (ret, code, 0, 0, false);
4682 : : }
4683 : :
4684 : :
4685 : : static const char *
4686 : 0 : dwarf_visibility_name (unsigned int code)
4687 : : {
4688 [ # # ]: 0 : const char *ret = dwarf_visibility_string (code);
4689 : 0 : return string_or_unknown (ret, code, 0, 0, false);
4690 : : }
4691 : :
4692 : :
4693 : : static const char *
4694 : 0 : dwarf_virtuality_name (unsigned int code)
4695 : : {
4696 [ # # ]: 0 : const char *ret = dwarf_virtuality_string (code);
4697 : 0 : return string_or_unknown (ret, code, 0, 0, false);
4698 : : }
4699 : :
4700 : :
4701 : : static const char *
4702 : 0 : dwarf_identifier_case_name (unsigned int code)
4703 : : {
4704 [ # # ]: 0 : const char *ret = dwarf_identifier_case_string (code);
4705 : 0 : return string_or_unknown (ret, code, 0, 0, false);
4706 : : }
4707 : :
4708 : :
4709 : : static const char *
4710 : 0 : dwarf_calling_convention_name (unsigned int code)
4711 : : {
4712 [ # # ]: 0 : const char *ret = dwarf_calling_convention_string (code);
4713 : 0 : return string_or_unknown (ret, code, DW_CC_lo_user, DW_CC_hi_user, false);
4714 : : }
4715 : :
4716 : :
4717 : : static const char *
4718 : 0 : dwarf_ordering_name (unsigned int code)
4719 : : {
4720 [ # # ]: 0 : const char *ret = dwarf_ordering_string (code);
4721 : 0 : return string_or_unknown (ret, code, 0, 0, false);
4722 : : }
4723 : :
4724 : :
4725 : : static const char *
4726 : 16 : dwarf_discr_list_name (unsigned int code)
4727 : : {
4728 [ + - ]: 16 : const char *ret = dwarf_discr_list_string (code);
4729 : 16 : return string_or_unknown (ret, code, 0, 0, false);
4730 : : }
4731 : :
4732 : :
4733 : : static const char *
4734 : 3284 : dwarf_unit_name (unsigned int type)
4735 : : {
4736 [ + - ]: 3284 : const char *ret = dwarf_unit_string (type);
4737 : 3284 : return string_or_unknown (ret, type, DW_UT_lo_user, DW_UT_hi_user, true);
4738 : : }
4739 : :
4740 : :
4741 : : static const char *
4742 : 126232 : dwarf_range_list_encoding_name (unsigned int kind)
4743 : : {
4744 [ + - ]: 126232 : const char *ret = dwarf_range_list_encoding_string (kind);
4745 : 126232 : return string_or_unknown (ret, kind, 0, 0, false);
4746 : : }
4747 : :
4748 : :
4749 : : static const char *
4750 : 594948 : dwarf_loc_list_encoding_name (unsigned int kind)
4751 : : {
4752 [ + - ]: 594948 : const char *ret = dwarf_loc_list_encoding_string (kind);
4753 : 594948 : return string_or_unknown (ret, kind, 0, 0, false);
4754 : : }
4755 : :
4756 : :
4757 : : static const char *
4758 : 9816 : dwarf_line_content_description_name (unsigned int kind)
4759 : : {
4760 [ + - ]: 9816 : const char *ret = dwarf_line_content_description_string (kind);
4761 : 9816 : return string_or_unknown (ret, kind, DW_LNCT_lo_user, DW_LNCT_hi_user,
4762 : : false);
4763 : : }
4764 : :
4765 : :
4766 : : static void
4767 : 510 : print_block (size_t n, const void *block)
4768 : : {
4769 [ - + ]: 510 : if (n == 0)
4770 : 0 : puts (_("empty block"));
4771 : : else
4772 : : {
4773 : 510 : printf (_("%zu byte block:"), n);
4774 : 510 : const unsigned char *data = block;
4775 : 2250 : do
4776 : 2250 : printf (" %02x", *data++);
4777 [ + + ]: 2250 : while (--n > 0);
4778 : 510 : putchar ('\n');
4779 : : }
4780 : 510 : }
4781 : :
4782 : : static void
4783 : 0 : print_bytes (size_t n, const unsigned char *bytes)
4784 : : {
4785 [ # # ]: 0 : while (n-- > 0)
4786 : : {
4787 : 0 : printf ("%02x", *bytes++);
4788 [ # # ]: 0 : if (n > 0)
4789 : 0 : printf (" ");
4790 : : }
4791 : 0 : }
4792 : :
4793 : : static int
4794 : 136 : get_indexed_addr (Dwarf_CU *cu, Dwarf_Word idx, Dwarf_Addr *addr)
4795 : : {
4796 [ - + ]: 136 : if (cu == NULL)
4797 : : return -1;
4798 : :
4799 : 136 : Elf_Data *debug_addr = cu->dbg->sectiondata[IDX_debug_addr];
4800 [ - + ]: 136 : if (debug_addr == NULL)
4801 : : return -1;
4802 : :
4803 : 136 : Dwarf_Off base = __libdw_cu_addr_base (cu);
4804 : 136 : Dwarf_Word off = idx * cu->address_size;
4805 [ - + ]: 136 : if (base > debug_addr->d_size
4806 [ - + ]: 136 : || off > debug_addr->d_size - base
4807 [ - + ]: 136 : || cu->address_size > debug_addr->d_size - base - off)
4808 : : return -1;
4809 : :
4810 : 136 : const unsigned char *addrp = debug_addr->d_buf + base + off;
4811 [ - + ]: 136 : if (cu->address_size == 4)
4812 [ # # ]: 0 : *addr = read_4ubyte_unaligned (cu->dbg, addrp);
4813 : : else
4814 [ - + ]: 136 : *addr = read_8ubyte_unaligned (cu->dbg, addrp);
4815 : :
4816 : : return 0;
4817 : : }
4818 : :
4819 : : static void
4820 : 743594 : print_ops (Dwfl_Module *dwflmod, Dwarf *dbg, int indent, int indentrest,
4821 : : unsigned int vers, unsigned int addrsize, unsigned int offset_size,
4822 : : struct Dwarf_CU *cu, Dwarf_Word len, const unsigned char *data)
4823 : : {
4824 [ + + ]: 743594 : const unsigned int ref_size = vers < 3 ? addrsize : offset_size;
4825 : :
4826 [ - + ]: 743594 : if (len == 0)
4827 : : {
4828 : 0 : printf ("%*s(empty)\n", indent, "");
4829 : 0 : return;
4830 : : }
4831 : :
4832 : : #define NEED(n) if (len < (Dwarf_Word) (n)) goto invalid
4833 : : #define CONSUME(n) NEED (n); else len -= (n)
4834 : :
4835 : : Dwarf_Word offset = 0;
4836 [ + + ]: 1805320 : while (len-- > 0)
4837 : 1061726 : {
4838 : 1061726 : uint_fast8_t op = *data++;
4839 : :
4840 [ + - ]: 1061726 : const char *op_name = dwarf_locexpr_opcode_string (op);
4841 [ - + ]: 1061726 : if (unlikely (op_name == NULL))
4842 : : {
4843 : 0 : static char buf[20];
4844 [ # # ]: 0 : if (op >= DW_OP_lo_user)
4845 : 0 : snprintf (buf, sizeof buf, "lo_user+%#x", op - DW_OP_lo_user);
4846 : : else
4847 : 0 : snprintf (buf, sizeof buf, "??? (%#x)", op);
4848 : : op_name = buf;
4849 : : }
4850 : :
4851 [ + - + + : 1061726 : switch (op)
+ + + + -
- + + - +
- - + + +
+ + - - -
- + + + ]
4852 : : {
4853 : 25686 : case DW_OP_addr:;
4854 : : /* Address operand. */
4855 : 25686 : Dwarf_Word addr;
4856 [ - + ]: 25686 : NEED (addrsize);
4857 [ + + ]: 25686 : if (addrsize == 4)
4858 [ + + ]: 84 : addr = read_4ubyte_unaligned (dbg, data);
4859 [ + - ]: 25602 : else if (addrsize == 8)
4860 [ + + ]: 25602 : addr = read_8ubyte_unaligned (dbg, data);
4861 : : else
4862 : 0 : goto invalid;
4863 : 25686 : data += addrsize;
4864 : 25686 : CONSUME (addrsize);
4865 : :
4866 : 25686 : printf ("%*s[%2" PRIuMAX "] %s ",
4867 : : indent, "", (uintmax_t) offset, op_name);
4868 : 25686 : print_dwarf_addr (dwflmod, 0, addr, addr);
4869 : 25686 : printf ("\n");
4870 : :
4871 : 25686 : offset += 1 + addrsize;
4872 : 25686 : break;
4873 : :
4874 : 0 : case DW_OP_call_ref:
4875 : : case DW_OP_GNU_variable_value:
4876 : : /* Offset operand. */
4877 [ # # ]: 0 : if (ref_size != 4 && ref_size != 8)
4878 : 0 : goto invalid; /* Cannot be used in CFA. */
4879 [ # # ]: 0 : NEED (ref_size);
4880 [ # # ]: 0 : if (ref_size == 4)
4881 [ # # ]: 0 : addr = read_4ubyte_unaligned (dbg, data);
4882 : : else
4883 [ # # ]: 0 : addr = read_8ubyte_unaligned (dbg, data);
4884 : 0 : data += ref_size;
4885 : 0 : CONSUME (ref_size);
4886 : : /* addr is a DIE offset, so format it as one. */
4887 : 0 : printf ("%*s[%2" PRIuMAX "] %s [%6" PRIxMAX "]\n",
4888 : : indent, "", (uintmax_t) offset,
4889 : : op_name, (uintmax_t) addr);
4890 : 0 : offset += 1 + ref_size;
4891 : 0 : break;
4892 : :
4893 : 26424 : case DW_OP_deref_size:
4894 : : case DW_OP_xderef_size:
4895 : : case DW_OP_pick:
4896 : : case DW_OP_const1u:
4897 : : // XXX value might be modified by relocation
4898 [ - + ]: 26424 : NEED (1);
4899 : 52848 : printf ("%*s[%2" PRIuMAX "] %s %" PRIu8 "\n",
4900 : : indent, "", (uintmax_t) offset,
4901 : 26424 : op_name, *((uint8_t *) data));
4902 : 26424 : ++data;
4903 : 26424 : --len;
4904 : 26424 : offset += 2;
4905 : 26424 : break;
4906 : :
4907 : 3214 : case DW_OP_const2u:
4908 [ - + ]: 3214 : NEED (2);
4909 : : // XXX value might be modified by relocation
4910 : 0 : printf ("%*s[%2" PRIuMAX "] %s %" PRIu16 "\n",
4911 : : indent, "", (uintmax_t) offset,
4912 [ - + ]: 3214 : op_name, read_2ubyte_unaligned (dbg, data));
4913 : 3214 : CONSUME (2);
4914 : 3214 : data += 2;
4915 : 3214 : offset += 3;
4916 : 3214 : break;
4917 : :
4918 : 2422 : case DW_OP_const4u:
4919 [ - + ]: 2422 : NEED (4);
4920 : : // XXX value might be modified by relocation
4921 : 2422 : printf ("%*s[%2" PRIuMAX "] %s %" PRIu32 "\n",
4922 : : indent, "", (uintmax_t) offset,
4923 [ - + ]: 2422 : op_name, read_4ubyte_unaligned (dbg, data));
4924 : 2422 : CONSUME (4);
4925 : 2422 : data += 4;
4926 : 2422 : offset += 5;
4927 : 2422 : break;
4928 : :
4929 : 114 : case DW_OP_const8u:
4930 [ - + ]: 114 : NEED (8);
4931 : : // XXX value might be modified by relocation
4932 : 114 : printf ("%*s[%2" PRIuMAX "] %s %" PRIu64 "\n",
4933 : : indent, "", (uintmax_t) offset,
4934 [ - + ]: 114 : op_name, (uint64_t) read_8ubyte_unaligned (dbg, data));
4935 : 114 : CONSUME (8);
4936 : 114 : data += 8;
4937 : 114 : offset += 9;
4938 : 114 : break;
4939 : :
4940 : 4842 : case DW_OP_const1s:
4941 [ - + ]: 4842 : NEED (1);
4942 : : // XXX value might be modified by relocation
4943 : 9684 : printf ("%*s[%2" PRIuMAX "] %s %" PRId8 "\n",
4944 : : indent, "", (uintmax_t) offset,
4945 : 4842 : op_name, *((int8_t *) data));
4946 : 4842 : ++data;
4947 : 4842 : --len;
4948 : 4842 : offset += 2;
4949 : 4842 : break;
4950 : :
4951 : 10 : case DW_OP_const2s:
4952 [ - + ]: 10 : NEED (2);
4953 : : // XXX value might be modified by relocation
4954 : 0 : printf ("%*s[%2" PRIuMAX "] %s %" PRId16 "\n",
4955 : : indent, "", (uintmax_t) offset,
4956 [ - + ]: 10 : op_name, read_2sbyte_unaligned (dbg, data));
4957 : 10 : CONSUME (2);
4958 : 10 : data += 2;
4959 : 10 : offset += 3;
4960 : 10 : break;
4961 : :
4962 : 0 : case DW_OP_const4s:
4963 [ # # ]: 0 : NEED (4);
4964 : : // XXX value might be modified by relocation
4965 : 0 : printf ("%*s[%2" PRIuMAX "] %s %" PRId32 "\n",
4966 : : indent, "", (uintmax_t) offset,
4967 [ # # ]: 0 : op_name, read_4sbyte_unaligned (dbg, data));
4968 : 0 : CONSUME (4);
4969 : 0 : data += 4;
4970 : 0 : offset += 5;
4971 : 0 : break;
4972 : :
4973 : 0 : case DW_OP_const8s:
4974 [ # # ]: 0 : NEED (8);
4975 : : // XXX value might be modified by relocation
4976 : 0 : printf ("%*s[%2" PRIuMAX "] %s %" PRId64 "\n",
4977 : : indent, "", (uintmax_t) offset,
4978 [ # # ]: 0 : op_name, read_8sbyte_unaligned (dbg, data));
4979 : 0 : CONSUME (8);
4980 : 0 : data += 8;
4981 : 0 : offset += 9;
4982 : 0 : break;
4983 : :
4984 : 15012 : case DW_OP_piece:
4985 : : case DW_OP_regx:
4986 : : case DW_OP_plus_uconst:
4987 : 15012 : case DW_OP_constu:;
4988 : 15012 : const unsigned char *start = data;
4989 : 15012 : uint64_t uleb;
4990 [ - + ]: 15012 : NEED (1);
4991 : 15012 : get_uleb128 (uleb, data, data + len);
4992 : 15012 : printf ("%*s[%2" PRIuMAX "] %s %" PRIu64 "\n",
4993 : : indent, "", (uintmax_t) offset, op_name, uleb);
4994 [ - + ]: 15012 : CONSUME (data - start);
4995 : 15012 : offset += 1 + (data - start);
4996 : 15012 : break;
4997 : :
4998 : 4 : case DW_OP_addrx:
4999 : : case DW_OP_GNU_addr_index:
5000 : : case DW_OP_constx:
5001 : 4 : case DW_OP_GNU_const_index:;
5002 : 4 : start = data;
5003 [ - + ]: 4 : NEED (1);
5004 : 4 : get_uleb128 (uleb, data, data + len);
5005 : 4 : printf ("%*s[%2" PRIuMAX "] %s [%" PRIu64 "] ",
5006 : : indent, "", (uintmax_t) offset, op_name, uleb);
5007 [ - + ]: 4 : CONSUME (data - start);
5008 : 4 : offset += 1 + (data - start);
5009 [ - + ]: 4 : if (get_indexed_addr (cu, uleb, &addr) != 0)
5010 : 0 : printf ("???\n");
5011 : : else
5012 : : {
5013 : 4 : print_dwarf_addr (dwflmod, 0, addr, addr);
5014 : 4 : printf ("\n");
5015 : : }
5016 : : break;
5017 : :
5018 : 0 : case DW_OP_bit_piece:
5019 : 0 : start = data;
5020 : 0 : uint64_t uleb2;
5021 [ # # ]: 0 : NEED (1);
5022 : 0 : get_uleb128 (uleb, data, data + len);
5023 : 0 : NEED (1);
5024 : 0 : get_uleb128 (uleb2, data, data + len);
5025 : 0 : printf ("%*s[%2" PRIuMAX "] %s %" PRIu64 ", %" PRIu64 "\n",
5026 : : indent, "", (uintmax_t) offset, op_name, uleb, uleb2);
5027 [ # # ]: 0 : CONSUME (data - start);
5028 : 0 : offset += 1 + (data - start);
5029 : 0 : break;
5030 : :
5031 : 183926 : case DW_OP_fbreg:
5032 : : case DW_OP_breg0 ... DW_OP_breg31:
5033 : : case DW_OP_consts:
5034 : 183926 : start = data;
5035 : 183926 : int64_t sleb;
5036 [ - + ]: 183926 : NEED (1);
5037 : 183926 : get_sleb128 (sleb, data, data + len);
5038 : 183926 : printf ("%*s[%2" PRIuMAX "] %s %" PRId64 "\n",
5039 : : indent, "", (uintmax_t) offset, op_name, sleb);
5040 [ - + ]: 183926 : CONSUME (data - start);
5041 : 183926 : offset += 1 + (data - start);
5042 : 183926 : break;
5043 : :
5044 : 0 : case DW_OP_bregx:
5045 : 0 : start = data;
5046 [ # # ]: 0 : NEED (1);
5047 : 0 : get_uleb128 (uleb, data, data + len);
5048 : 0 : NEED (1);
5049 : 0 : get_sleb128 (sleb, data, data + len);
5050 : 0 : printf ("%*s[%2" PRIuMAX "] %s %" PRIu64 " %" PRId64 "\n",
5051 : : indent, "", (uintmax_t) offset, op_name, uleb, sleb);
5052 [ # # ]: 0 : CONSUME (data - start);
5053 : 0 : offset += 1 + (data - start);
5054 : 0 : break;
5055 : :
5056 : 0 : case DW_OP_call2:
5057 [ # # ]: 0 : NEED (2);
5058 : 0 : printf ("%*s[%2" PRIuMAX "] %s [%6" PRIx16 "]\n",
5059 : : indent, "", (uintmax_t) offset, op_name,
5060 [ # # ]: 0 : read_2ubyte_unaligned (dbg, data));
5061 : 0 : CONSUME (2);
5062 : 0 : data += 2;
5063 : 0 : offset += 3;
5064 : 0 : break;
5065 : :
5066 : 8 : case DW_OP_call4:
5067 [ - + ]: 8 : NEED (4);
5068 : 8 : printf ("%*s[%2" PRIuMAX "] %s [%6" PRIx32 "]\n",
5069 : : indent, "", (uintmax_t) offset, op_name,
5070 [ - + ]: 8 : read_4ubyte_unaligned (dbg, data));
5071 : 8 : CONSUME (4);
5072 : 8 : data += 4;
5073 : 8 : offset += 5;
5074 : 8 : break;
5075 : :
5076 : 1194 : case DW_OP_skip:
5077 : : case DW_OP_bra:
5078 [ - + ]: 1194 : NEED (2);
5079 : 2388 : printf ("%*s[%2" PRIuMAX "] %s %" PRIuMAX "\n",
5080 : : indent, "", (uintmax_t) offset, op_name,
5081 [ - + ]: 1194 : (uintmax_t) (offset + read_2sbyte_unaligned (dbg, data) + 3));
5082 : 1194 : CONSUME (2);
5083 : 1194 : data += 2;
5084 : 1194 : offset += 3;
5085 : 1194 : break;
5086 : :
5087 : 510 : case DW_OP_implicit_value:
5088 : 510 : start = data;
5089 [ - + ]: 510 : NEED (1);
5090 : 510 : get_uleb128 (uleb, data, data + len);
5091 : 510 : printf ("%*s[%2" PRIuMAX "] %s: ",
5092 : : indent, "", (uintmax_t) offset, op_name);
5093 [ - + ]: 510 : NEED (uleb);
5094 : 510 : print_block (uleb, data);
5095 : 510 : data += uleb;
5096 [ - + ]: 510 : CONSUME (data - start);
5097 : 510 : offset += 1 + (data - start);
5098 : 510 : break;
5099 : :
5100 : 5014 : case DW_OP_implicit_pointer:
5101 : : case DW_OP_GNU_implicit_pointer:
5102 : : /* DIE offset operand. */
5103 : 5014 : start = data;
5104 [ - + ]: 5014 : NEED (ref_size);
5105 [ - + ]: 5014 : if (ref_size != 4 && ref_size != 8)
5106 : 0 : goto invalid; /* Cannot be used in CFA. */
5107 [ + - ]: 5014 : if (ref_size == 4)
5108 [ - + ]: 5014 : addr = read_4ubyte_unaligned (dbg, data);
5109 : : else
5110 [ # # ]: 0 : addr = read_8ubyte_unaligned (dbg, data);
5111 : 5014 : data += ref_size;
5112 : : /* Byte offset operand. */
5113 [ - + ]: 5014 : NEED (1);
5114 : 5014 : get_sleb128 (sleb, data, data + len);
5115 : :
5116 : 5014 : printf ("%*s[%2" PRIuMAX "] %s [%6" PRIxMAX "] %+" PRId64 "\n",
5117 : : indent, "", (intmax_t) offset,
5118 : : op_name, (uintmax_t) addr, sleb);
5119 [ - + ]: 5014 : CONSUME (data - start);
5120 : 5014 : offset += 1 + (data - start);
5121 : 5014 : break;
5122 : :
5123 : 58466 : case DW_OP_entry_value:
5124 : : case DW_OP_GNU_entry_value:
5125 : : /* Size plus expression block. */
5126 : 58466 : start = data;
5127 [ - + ]: 58466 : NEED (1);
5128 : 58466 : get_uleb128 (uleb, data, data + len);
5129 : 58466 : printf ("%*s[%2" PRIuMAX "] %s:\n",
5130 : : indent, "", (uintmax_t) offset, op_name);
5131 [ - + ]: 58466 : NEED (uleb);
5132 : 58466 : print_ops (dwflmod, dbg, indent + 5, indent + 5, vers,
5133 : : addrsize, offset_size, cu, uleb, data);
5134 : 58466 : data += uleb;
5135 [ - + ]: 58466 : CONSUME (data - start);
5136 : 58466 : offset += 1 + (data - start);
5137 : 58466 : break;
5138 : :
5139 : 0 : case DW_OP_const_type:
5140 : : case DW_OP_GNU_const_type:
5141 : : /* uleb128 CU relative DW_TAG_base_type DIE offset, 1-byte
5142 : : unsigned size plus block. */
5143 : 0 : start = data;
5144 [ # # ]: 0 : NEED (1);
5145 : 0 : get_uleb128 (uleb, data, data + len);
5146 [ # # # # ]: 0 : if (! print_unresolved_addresses && cu != NULL)
5147 : 0 : uleb += cu->start;
5148 : 0 : NEED (1);
5149 : 0 : uint8_t usize = *(uint8_t *) data++;
5150 [ # # ]: 0 : NEED (usize);
5151 : 0 : printf ("%*s[%2" PRIuMAX "] %s [%6" PRIxMAX "] ",
5152 : : indent, "", (uintmax_t) offset, op_name, uleb);
5153 : 0 : print_block (usize, data);
5154 : 0 : data += usize;
5155 [ # # ]: 0 : CONSUME (data - start);
5156 : 0 : offset += 1 + (data - start);
5157 : 0 : break;
5158 : :
5159 : 0 : case DW_OP_regval_type:
5160 : : case DW_OP_GNU_regval_type:
5161 : : /* uleb128 register number, uleb128 CU relative
5162 : : DW_TAG_base_type DIE offset. */
5163 : 0 : start = data;
5164 [ # # ]: 0 : NEED (1);
5165 : 0 : get_uleb128 (uleb, data, data + len);
5166 : 0 : NEED (1);
5167 : 0 : get_uleb128 (uleb2, data, data + len);
5168 [ # # # # ]: 0 : if (! print_unresolved_addresses && cu != NULL)
5169 : 0 : uleb2 += cu->start;
5170 : 0 : printf ("%*s[%2" PRIuMAX "] %s %" PRIu64 " [%6" PRIx64 "]\n",
5171 : : indent, "", (uintmax_t) offset, op_name, uleb, uleb2);
5172 [ # # ]: 0 : CONSUME (data - start);
5173 : 0 : offset += 1 + (data - start);
5174 : 0 : break;
5175 : :
5176 : 0 : case DW_OP_deref_type:
5177 : : case DW_OP_GNU_deref_type:
5178 : : /* 1-byte unsigned size of value, uleb128 CU relative
5179 : : DW_TAG_base_type DIE offset. */
5180 : 0 : start = data;
5181 [ # # ]: 0 : NEED (1);
5182 : 0 : usize = *(uint8_t *) data++;
5183 : 0 : NEED (1);
5184 : 0 : get_uleb128 (uleb, data, data + len);
5185 [ # # # # ]: 0 : if (! print_unresolved_addresses && cu != NULL)
5186 : 0 : uleb += cu->start;
5187 : 0 : printf ("%*s[%2" PRIuMAX "] %s %" PRIu8 " [%6" PRIxMAX "]\n",
5188 : : indent, "", (uintmax_t) offset,
5189 : : op_name, usize, uleb);
5190 [ # # ]: 0 : CONSUME (data - start);
5191 : 0 : offset += 1 + (data - start);
5192 : 0 : break;
5193 : :
5194 : 0 : case DW_OP_xderef_type:
5195 : : /* 1-byte unsigned size of value, uleb128 base_type DIE offset. */
5196 : 0 : start = data;
5197 [ # # ]: 0 : NEED (1);
5198 : 0 : usize = *(uint8_t *) data++;
5199 : 0 : NEED (1);
5200 : 0 : get_uleb128 (uleb, data, data + len);
5201 : 0 : printf ("%*s[%4" 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 : 816 : case DW_OP_convert:
5209 : : case DW_OP_GNU_convert:
5210 : : case DW_OP_reinterpret:
5211 : : case DW_OP_GNU_reinterpret:
5212 : : /* uleb128 CU relative offset to DW_TAG_base_type, or zero
5213 : : for conversion to untyped. */
5214 : 816 : start = data;
5215 [ - + ]: 816 : NEED (1);
5216 : 816 : get_uleb128 (uleb, data, data + len);
5217 [ + + + - : 816 : if (uleb != 0 && ! print_unresolved_addresses && cu != NULL)
+ - ]
5218 : 544 : uleb += cu->start;
5219 : 816 : printf ("%*s[%2" PRIuMAX "] %s [%6" PRIxMAX "]\n",
5220 : : indent, "", (uintmax_t) offset, op_name, uleb);
5221 [ - + ]: 816 : CONSUME (data - start);
5222 : 816 : offset += 1 + (data - start);
5223 : 816 : break;
5224 : :
5225 : 6 : case DW_OP_GNU_parameter_ref:
5226 : : /* 4 byte CU relative reference to the abstract optimized away
5227 : : DW_TAG_formal_parameter. */
5228 [ - + ]: 6 : NEED (4);
5229 [ - + ]: 6 : uintmax_t param_off = (uintmax_t) read_4ubyte_unaligned (dbg, data);
5230 [ + - + - ]: 6 : if (! print_unresolved_addresses && cu != NULL)
5231 : 6 : param_off += cu->start;
5232 : 6 : printf ("%*s[%2" PRIuMAX "] %s [%6" PRIxMAX "]\n",
5233 : : indent, "", (uintmax_t) offset, op_name, param_off);
5234 : 6 : CONSUME (4);
5235 : 6 : data += 4;
5236 : 6 : offset += 5;
5237 : 6 : break;
5238 : :
5239 : : default:
5240 : : /* No Operand. */
5241 : 734058 : printf ("%*s[%2" PRIuMAX "] %s\n",
5242 : : indent, "", (uintmax_t) offset, op_name);
5243 : 734058 : ++offset;
5244 : 734058 : break;
5245 : : }
5246 : :
5247 : 1061726 : indent = indentrest;
5248 : 1061726 : continue;
5249 : :
5250 : 0 : invalid:
5251 : 0 : printf (_("%*s[%2" PRIuMAX "] %s <TRUNCATED>\n"),
5252 : : indent, "", (uintmax_t) offset, op_name);
5253 : 0 : break;
5254 : : }
5255 : : }
5256 : :
5257 : :
5258 : : /* Turn the addresses into file offsets by using the phdrs. */
5259 : : static void
5260 : 2 : find_offsets(Elf *elf, GElf_Addr main_bias, size_t n,
5261 : : GElf_Addr addrs[n], GElf_Off offs[n])
5262 : : {
5263 : 2 : size_t unsolved = n;
5264 [ + + ]: 18 : for (size_t i = 0; i < phnum; ++i) {
5265 : 16 : GElf_Phdr phdr_mem;
5266 : 16 : GElf_Phdr *phdr = gelf_getphdr(elf, i, &phdr_mem);
5267 [ + - + + : 16 : if (phdr != NULL && phdr->p_type == PT_LOAD && phdr->p_memsz > 0)
+ - ]
5268 [ + + ]: 46 : for (size_t j = 0; j < n; ++j)
5269 [ + + + + ]: 44 : if (offs[j] == 0 && addrs[j] >= phdr->p_vaddr + main_bias &&
5270 [ + - ]: 22 : addrs[j] - (phdr->p_vaddr + main_bias) < phdr->p_filesz) {
5271 : 22 : offs[j] = addrs[j] - (phdr->p_vaddr + main_bias) + phdr->p_offset;
5272 [ + + ]: 22 : if (--unsolved == 0)
5273 : : break;
5274 : : }
5275 : : }
5276 : 2 : }
5277 : :
5278 : : /* The dynamic segment (type PT_DYNAMIC), contains the .dynamic section.
5279 : : And .dynamic section contains an array of the dynamic structures.
5280 : : We use the array to get:
5281 : : DT_STRTAB: the address of the string table
5282 : : DT_SYMTAB: the address of the symbol table
5283 : : DT_STRSZ: the size, in bytes, of the string table
5284 : : ... */
5285 : : static void
5286 : 2 : get_dynscn_addrs(Elf *elf, GElf_Phdr *phdr, GElf_Addr addrs[i_max])
5287 : : {
5288 : 2 : Elf_Data *data = elf_getdata_rawchunk(
5289 : 2 : elf, phdr->p_offset, phdr->p_filesz, ELF_T_DYN);
5290 : :
5291 : 2 : int dyn_idx = 0;
5292 : 44 : for (;; ++dyn_idx) {
5293 : 46 : GElf_Dyn dyn_mem;
5294 : 46 : GElf_Dyn *dyn = gelf_getdyn(data, dyn_idx, &dyn_mem);
5295 : : /* DT_NULL Marks end of dynamic section. */
5296 [ + - + + ]: 46 : if (dyn == NULL || dyn->d_tag == DT_NULL)
5297 : : break;
5298 : :
5299 [ + - + + : 44 : switch (dyn->d_tag) {
+ + + + +
+ - + ]
5300 : 2 : case DT_SYMTAB:
5301 : 2 : addrs[i_symtab] = dyn->d_un.d_ptr;
5302 : 2 : break;
5303 : :
5304 : 0 : case DT_HASH:
5305 : 0 : addrs[i_hash] = dyn->d_un.d_ptr;
5306 : 0 : break;
5307 : :
5308 : 2 : case DT_GNU_HASH:
5309 : 2 : addrs[i_gnu_hash] = dyn->d_un.d_ptr;
5310 : 2 : break;
5311 : :
5312 : 2 : case DT_STRTAB:
5313 : 2 : addrs[i_strtab] = dyn->d_un.d_ptr;
5314 : 2 : break;
5315 : :
5316 : 2 : case DT_VERSYM:
5317 : 2 : addrs[i_versym] = dyn->d_un.d_ptr;
5318 : 2 : break;
5319 : :
5320 : 2 : case DT_VERDEF:
5321 : 2 : addrs[i_verdef] = dyn->d_un.d_ptr;
5322 : 2 : break;
5323 : :
5324 : 2 : case DT_VERDEFNUM:
5325 : 2 : addrs[i_verdefnum] = dyn->d_un.d_val;
5326 : 2 : break;
5327 : :
5328 : 2 : case DT_VERNEED:
5329 : 2 : addrs[i_verneed] = dyn->d_un.d_ptr;
5330 : 2 : break;
5331 : :
5332 : 2 : case DT_VERNEEDNUM:
5333 : 2 : addrs[i_verneednum] = dyn->d_un.d_val;
5334 : 2 : break;
5335 : :
5336 : 2 : case DT_STRSZ:
5337 : 2 : addrs[i_strsz] = dyn->d_un.d_val;
5338 : 2 : break;
5339 : :
5340 : 0 : case DT_SYMTAB_SHNDX:
5341 : 0 : addrs[i_symtab_shndx] = dyn->d_un.d_ptr;
5342 : 0 : break;
5343 : : }
5344 : : }
5345 : 2 : }
5346 : :
5347 : :
5348 : : /* Use dynamic segment to get data for the string table section. */
5349 : : static Elf_Data *
5350 : 2 : get_dynscn_strtab(Elf *elf, GElf_Phdr *phdr)
5351 : : {
5352 : 2 : Elf_Data *strtab_data;
5353 : 2 : GElf_Addr addrs[i_max] = {0,};
5354 : 2 : GElf_Off offs[i_max] = {0,};
5355 : 2 : get_dynscn_addrs(elf, phdr, addrs);
5356 : 2 : find_offsets(elf, 0, i_max, addrs, offs);
5357 : 4 : strtab_data = elf_getdata_rawchunk(
5358 : 2 : elf, offs[i_strtab], addrs[i_strsz], ELF_T_BYTE);
5359 : 2 : return strtab_data;
5360 : : }
5361 : :
5362 : :
5363 : : struct listptr
5364 : : {
5365 : : Dwarf_Off offset:(64 - 3);
5366 : : bool addr64:1;
5367 : : bool dwarf64:1;
5368 : : bool warned:1;
5369 : : struct Dwarf_CU *cu;
5370 : : unsigned int attr;
5371 : : };
5372 : :
5373 : : #define listptr_offset_size(p) ((p)->dwarf64 ? 8 : 4)
5374 : : #define listptr_address_size(p) ((p)->addr64 ? 8 : 4)
5375 : :
5376 : : static Dwarf_Addr
5377 : 20260 : cudie_base (Dwarf_Die *cudie)
5378 : : {
5379 : 20260 : Dwarf_Addr base;
5380 : : /* Find the base address of the compilation unit. It will normally
5381 : : be specified by DW_AT_low_pc. In DWARF-3 draft 4, the base
5382 : : address could be overridden by DW_AT_entry_pc. It's been
5383 : : removed, but GCC emits DW_AT_entry_pc and not DW_AT_lowpc for
5384 : : compilation units with discontinuous ranges. */
5385 [ - + ]: 20260 : if (unlikely (dwarf_lowpc (cudie, &base) != 0))
5386 : : {
5387 : 0 : Dwarf_Attribute attr_mem;
5388 [ # # ]: 0 : if (dwarf_formaddr (dwarf_attr (cudie, DW_AT_entry_pc, &attr_mem),
5389 : : &base) != 0)
5390 : 0 : base = 0;
5391 : : }
5392 : 20260 : return base;
5393 : : }
5394 : :
5395 : : static Dwarf_Addr
5396 : 20260 : listptr_base (struct listptr *p)
5397 : : {
5398 : 20260 : Dwarf_Die cu = CUDIE (p->cu);
5399 : 20260 : return cudie_base (&cu);
5400 : : }
5401 : :
5402 : : /* To store the name used in compare_listptr */
5403 : : static const char *sort_listptr_name;
5404 : :
5405 : : static int
5406 : 1646324 : compare_listptr (const void *a, const void *b)
5407 : : {
5408 : 1646324 : const char *name = sort_listptr_name;
5409 : 1646324 : struct listptr *p1 = (void *) a;
5410 : 1646324 : struct listptr *p2 = (void *) b;
5411 : :
5412 [ + + ]: 1646324 : if (p1->offset < p2->offset)
5413 : : return -1;
5414 [ + + ]: 158740 : if (p1->offset > p2->offset)
5415 : : return 1;
5416 : :
5417 [ + - - + ]: 7644 : if (!p1->warned && !p2->warned)
5418 : : {
5419 [ - + ]: 7644 : if (p1->addr64 != p2->addr64)
5420 : : {
5421 : 0 : p1->warned = p2->warned = true;
5422 : 0 : error (0, 0,
5423 : 0 : _("%s %#" PRIx64 " used with different address sizes"),
5424 : : name, (uint64_t) p1->offset);
5425 : : }
5426 [ - + ]: 7644 : if (p1->dwarf64 != p2->dwarf64)
5427 : : {
5428 : 0 : p1->warned = p2->warned = true;
5429 : 0 : error (0, 0,
5430 : 0 : _("%s %#" PRIx64 " used with different offset sizes"),
5431 : 0 : name, (uint64_t) p1->offset);
5432 : : }
5433 [ - + ]: 7644 : if (listptr_base (p1) != listptr_base (p2))
5434 : : {
5435 : 0 : p1->warned = p2->warned = true;
5436 : 0 : error (0, 0,
5437 : 0 : _("%s %#" PRIx64 " used with different base addresses"),
5438 : 0 : name, (uint64_t) p1->offset);
5439 : : }
5440 [ + - ]: 7644 : if (p1->attr != p2 ->attr)
5441 : : {
5442 : 0 : p1->warned = p2->warned = true;
5443 : 0 : error (0, 0,
5444 : 0 : _("%s %#" PRIx64
5445 : : " used with different attribute %s and %s"),
5446 : 0 : name, (uint64_t) p1->offset, dwarf_attr_name (p1->attr),
5447 : : dwarf_attr_name (p2->attr));
5448 : : }
5449 : : }
5450 : :
5451 : : return 0;
5452 : : }
5453 : :
5454 : : struct listptr_table
5455 : : {
5456 : : size_t n;
5457 : : size_t alloc;
5458 : : struct listptr *table;
5459 : : };
5460 : :
5461 : : static struct listptr_table known_locsptr;
5462 : : static struct listptr_table known_loclistsptr;
5463 : : static struct listptr_table known_rangelistptr;
5464 : : static struct listptr_table known_rnglistptr;
5465 : : static struct listptr_table known_addrbases;
5466 : : static struct listptr_table known_stroffbases;
5467 : :
5468 : : static void
5469 : 526 : reset_listptr (struct listptr_table *table)
5470 : : {
5471 : 526 : free (table->table);
5472 : 526 : table->table = NULL;
5473 : 526 : table->n = table->alloc = 0;
5474 : : }
5475 : :
5476 : : /* Returns false if offset doesn't fit. See struct listptr. */
5477 : : static bool
5478 : 239786 : notice_listptr (enum section_e section, struct listptr_table *table,
5479 : : uint_fast8_t address_size, uint_fast8_t offset_size,
5480 : : struct Dwarf_CU *cu, Dwarf_Off offset, unsigned int attr)
5481 : : {
5482 [ + + ]: 239786 : if (print_debug_sections & section)
5483 : : {
5484 [ + + ]: 239534 : if (table->n == table->alloc)
5485 : : {
5486 [ + + ]: 400 : if (table->alloc == 0)
5487 : 166 : table->alloc = 128;
5488 : : else
5489 : 234 : table->alloc *= 2;
5490 : 400 : table->table = xrealloc (table->table,
5491 : 400 : table->alloc * sizeof table->table[0]);
5492 : : }
5493 : :
5494 : 239534 : struct listptr *p = &table->table[table->n++];
5495 : :
5496 : 239534 : *p = (struct listptr)
5497 : : {
5498 : 239534 : .addr64 = address_size == 8,
5499 : 239534 : .dwarf64 = offset_size == 8,
5500 : : .offset = offset,
5501 : : .cu = cu,
5502 : : .attr = attr
5503 : : };
5504 : :
5505 [ + - ]: 239534 : if (p->offset != offset)
5506 : : {
5507 : 0 : table->n--;
5508 : 0 : return false;
5509 : : }
5510 : : }
5511 : : return true;
5512 : : }
5513 : :
5514 : : static void
5515 : 174 : sort_listptr (struct listptr_table *table, const char *name)
5516 : : {
5517 [ + + ]: 174 : if (table->n > 0)
5518 : : {
5519 : 166 : sort_listptr_name = name;
5520 : 166 : qsort (table->table, table->n, sizeof table->table[0],
5521 : : &compare_listptr);
5522 : : }
5523 : 174 : }
5524 : :
5525 : : static bool
5526 : 208 : skip_listptr_hole (struct listptr_table *table, size_t *idxp,
5527 : : uint_fast8_t *address_sizep, uint_fast8_t *offset_sizep,
5528 : : Dwarf_Addr *base, struct Dwarf_CU **cu, ptrdiff_t offset,
5529 : : unsigned char **readp, unsigned char *endp,
5530 : : unsigned int *attr)
5531 : : {
5532 [ - + ]: 208 : if (table->n == 0)
5533 : : return false;
5534 : :
5535 [ + - + + ]: 336 : while (*idxp < table->n && table->table[*idxp].offset < (Dwarf_Off) offset)
5536 : 128 : ++*idxp;
5537 : :
5538 : 208 : struct listptr *p = &table->table[*idxp];
5539 : :
5540 [ + - ]: 208 : if (*idxp == table->n
5541 [ - + ]: 208 : || p->offset >= (Dwarf_Off) (endp - *readp + offset))
5542 : : {
5543 : 0 : *readp = endp;
5544 : 0 : printf (_(" [%6tx] <UNUSED GARBAGE IN REST OF SECTION>\n"),
5545 : : offset);
5546 : 0 : return true;
5547 : : }
5548 : :
5549 [ - + ]: 208 : if (p->offset != (Dwarf_Off) offset)
5550 : : {
5551 : 0 : *readp += p->offset - offset;
5552 : 0 : printf (_(" [%6tx] <UNUSED GARBAGE> ... %" PRIu64 " bytes ...\n"),
5553 : : offset, (Dwarf_Off) p->offset - offset);
5554 : 0 : return true;
5555 : : }
5556 : :
5557 [ + - ]: 208 : if (address_sizep != NULL)
5558 [ + + ]: 224 : *address_sizep = listptr_address_size (p);
5559 [ + + ]: 208 : if (offset_sizep != NULL)
5560 [ + - ]: 296 : *offset_sizep = listptr_offset_size (p);
5561 [ + - ]: 208 : if (base != NULL)
5562 : 208 : *base = listptr_base (p);
5563 [ + - ]: 208 : if (cu != NULL)
5564 : 208 : *cu = p->cu;
5565 [ + + ]: 208 : if (attr != NULL)
5566 : 148 : *attr = p->attr;
5567 : :
5568 : : return false;
5569 : : }
5570 : :
5571 : : static Dwarf_Off
5572 : 102982 : next_listptr_offset (struct listptr_table *table, size_t *idxp, Dwarf_Off off)
5573 : : {
5574 : : /* Note that multiple attributes could in theory point to the same loclist
5575 : : offset, so make sure we pick one that is bigger than the current one.
5576 : : The table is sorted on offset. */
5577 [ + - ]: 102982 : if (*idxp < table->n)
5578 : : {
5579 [ + - ]: 204794 : while (++*idxp < table->n)
5580 : : {
5581 : 204794 : Dwarf_Off next = table->table[*idxp].offset;
5582 [ + + ]: 204794 : if (next > off)
5583 : 102982 : return next;
5584 : : }
5585 : : }
5586 : : return 0;
5587 : : }
5588 : :
5589 : : /* Returns the listptr associated with the given index, or NULL. */
5590 : : static struct listptr *
5591 : 1396206 : get_listptr (struct listptr_table *table, size_t idx)
5592 : : {
5593 : 1396206 : if (idx >= table->n)
5594 : : return NULL;
5595 : 1396032 : return &table->table[idx];
5596 : : }
5597 : :
5598 : : /* Returns the next index, base address and CU associated with the
5599 : : list unit offsets. If there is none false is returned, otherwise
5600 : : true. Assumes the table has been sorted. */
5601 : : static bool
5602 : 4764 : listptr_cu (struct listptr_table *table, size_t *idxp,
5603 : : Dwarf_Off start, Dwarf_Off end,
5604 : : Dwarf_Addr *base, struct Dwarf_CU **cu)
5605 : : {
5606 : 4764 : while (*idxp < table->n
5607 [ + - + + ]: 36578 : && table->table[*idxp].offset < start)
5608 : 31814 : ++*idxp;
5609 : :
5610 [ + - ]: 4764 : if (*idxp < table->n
5611 [ + - ]: 4764 : && table->table[*idxp].offset >= start
5612 [ + - ]: 4764 : && table->table[*idxp].offset < end)
5613 : : {
5614 : 4764 : struct listptr *p = &table->table[*idxp];
5615 : 4764 : *base = listptr_base (p);
5616 : 4764 : *cu = p->cu;
5617 : 4764 : return true;
5618 : : }
5619 : :
5620 : : return false;
5621 : : }
5622 : :
5623 : : /* Returns the next index with the current CU for the given attribute.
5624 : : If there is none false is returned, otherwise true. Assumes the
5625 : : table has been sorted. */
5626 : : static bool
5627 : 697930 : listptr_attr (struct listptr_table *table, size_t idxp,
5628 : : Dwarf_Off offset, unsigned int attr)
5629 : : {
5630 : 1396186 : struct listptr *listptr;
5631 : 1396186 : do
5632 : : {
5633 [ + + ]: 1396186 : listptr = get_listptr (table, idxp);
5634 [ + - ]: 1396022 : if (listptr == NULL)
5635 : : return false;
5636 : :
5637 [ + + + + ]: 1396022 : if (listptr->offset == offset && listptr->attr == attr)
5638 : : return true;
5639 : :
5640 : 1293040 : idxp++;
5641 : : }
5642 [ + + ]: 1293040 : while (listptr->offset <= offset);
5643 : :
5644 : : return false;
5645 : : }
5646 : :
5647 : : static void
5648 : 84 : print_debug_abbrev_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
5649 : : Ebl *ebl, GElf_Ehdr *ehdr __attribute__ ((unused)),
5650 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
5651 : : {
5652 : 84 : Elf_Data *elf_data = get_debug_elf_data (dbg, ebl, IDX_debug_abbrev, scn);
5653 [ + - ]: 84 : if (elf_data == NULL)
5654 : : return;
5655 : :
5656 : 84 : const size_t sh_size = elf_data->d_size;
5657 : :
5658 : 84 : printf (_("\nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"
5659 : : " [ Code]\n"),
5660 : : elf_ndxscn (scn), section_name (ebl, shdr),
5661 : 84 : (uint64_t) shdr->sh_offset);
5662 : :
5663 : 84 : Dwarf_Off offset = 0;
5664 [ + + ]: 3476 : while (offset < sh_size)
5665 : : {
5666 : 3392 : printf (_("\nAbbreviation section at offset %" PRIu64 ":\n"),
5667 : : offset);
5668 : :
5669 : 313876 : while (1)
5670 : 155242 : {
5671 : 158634 : size_t length;
5672 : 158634 : Dwarf_Abbrev abbrev;
5673 : :
5674 : 158634 : int res = dwarf_offabbrev (dbg, offset, &length, &abbrev);
5675 [ + + ]: 158634 : if (res != 0)
5676 : : {
5677 [ - + ]: 3392 : if (unlikely (res < 0))
5678 : : {
5679 : 0 : printf (_("\
5680 : : *** error while reading abbreviation: %s\n"),
5681 : : dwarf_errmsg (-1));
5682 : 0 : return;
5683 : : }
5684 : :
5685 : : /* This is the NUL byte at the end of the section. */
5686 : 3392 : ++offset;
5687 : 3392 : break;
5688 : : }
5689 : :
5690 : : /* We know these calls can never fail. */
5691 : 155242 : unsigned int code = dwarf_getabbrevcode (&abbrev);
5692 : 155242 : unsigned int tag = dwarf_getabbrevtag (&abbrev);
5693 : 155242 : int has_children = dwarf_abbrevhaschildren (&abbrev);
5694 : :
5695 [ + + ]: 155242 : printf (_(" [%5u] offset: %" PRId64
5696 : : ", children: %s, tag: %s\n"),
5697 : : code, (int64_t) offset,
5698 : : has_children ? yes_str : no_str,
5699 : : dwarf_tag_name (tag));
5700 : :
5701 : 155242 : size_t cnt = 0;
5702 : 155242 : unsigned int name;
5703 : 155242 : unsigned int form;
5704 : 155242 : Dwarf_Sword data;
5705 : 155242 : Dwarf_Off enoffset;
5706 : 155242 : while (dwarf_getabbrevattr_data (&abbrev, cnt, &name, &form,
5707 [ + + ]: 887864 : &data, &enoffset) == 0)
5708 : : {
5709 : 732622 : printf (" attr: %s, form: %s",
5710 : : dwarf_attr_name (name), dwarf_form_name (form));
5711 [ + + ]: 732622 : if (form == DW_FORM_implicit_const)
5712 : 53544 : printf (" (%" PRId64 ")", data);
5713 : 732622 : printf (", offset: %#" PRIx64 "\n", (uint64_t) enoffset);
5714 : 732622 : ++cnt;
5715 : : }
5716 : :
5717 : 155242 : offset += length;
5718 : : }
5719 : : }
5720 : : }
5721 : :
5722 : :
5723 : : static void
5724 : 4 : print_debug_addr_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
5725 : : Ebl *ebl, GElf_Ehdr *ehdr,
5726 : : Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
5727 : : {
5728 : 4 : Elf_Data *data = get_debug_elf_data (dbg, ebl, IDX_debug_addr, scn);
5729 [ + - ]: 4 : if (data == NULL)
5730 : : return;
5731 : :
5732 : 4 : printf (_("\
5733 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
5734 : : elf_ndxscn (scn), section_name (ebl, shdr),
5735 : 4 : (uint64_t) shdr->sh_offset);
5736 : :
5737 [ + - ]: 4 : if (shdr->sh_size == 0)
5738 : : return;
5739 : :
5740 : 4 : size_t idx = 0;
5741 : 4 : sort_listptr (&known_addrbases, "addr_base");
5742 : :
5743 : 4 : const unsigned char *start = (const unsigned char *) data->d_buf;
5744 : 4 : const unsigned char *readp = start;
5745 : 4 : const unsigned char *readendp = ((const unsigned char *) data->d_buf
5746 : 4 : + data->d_size);
5747 : :
5748 [ + + ]: 12 : while (readp < readendp)
5749 : : {
5750 : : /* We cannot really know whether or not there is an header. The
5751 : : DebugFission extension to DWARF4 doesn't add one. The DWARF5
5752 : : .debug_addr variant does. Whether or not we have an header,
5753 : : DW_AT_[GNU_]addr_base points at "index 0". So if the current
5754 : : offset equals the CU addr_base then we can just start
5755 : : printing addresses. If there is no CU with an exact match
5756 : : then we'll try to parse the header first. */
5757 : 8 : Dwarf_Off off = (Dwarf_Off) (readp
5758 : 8 : - (const unsigned char *) data->d_buf);
5759 : :
5760 : 8 : printf ("Table at offset %" PRIx64 " ", off);
5761 : :
5762 [ + - ]: 8 : struct listptr *listptr = get_listptr (&known_addrbases, idx++);
5763 : 8 : const unsigned char *next_unitp;
5764 : :
5765 : 8 : uint64_t unit_length;
5766 : 8 : uint16_t version;
5767 : 8 : uint8_t address_size;
5768 : 8 : uint8_t segment_size;
5769 [ - + ]: 8 : if (listptr == NULL)
5770 : : {
5771 : 0 : error (0, 0, "Warning: No CU references .debug_addr after %" PRIx64,
5772 : : off);
5773 : :
5774 : : /* We will have to assume it is just addresses to the end... */
5775 [ # # ]: 0 : address_size = ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 4 : 8;
5776 : 0 : next_unitp = readendp;
5777 : 0 : printf ("Unknown CU:\n");
5778 : : }
5779 : : else
5780 : : {
5781 : 8 : Dwarf_Die cudie;
5782 [ - + ]: 8 : if (dwarf_cu_die (listptr->cu, &cudie,
5783 : : NULL, NULL, NULL, NULL,
5784 : : NULL, NULL) == NULL)
5785 : 0 : printf ("Unknown CU (%s):\n", dwarf_errmsg (-1));
5786 : : else
5787 : 8 : printf ("for CU [%6" PRIx64 "]:\n", dwarf_dieoffset (&cudie));
5788 : :
5789 [ + + ]: 8 : if (listptr->offset == off)
5790 : : {
5791 [ - + ]: 4 : address_size = listptr_address_size (listptr);
5792 : 4 : segment_size = 0;
5793 : 4 : version = 4;
5794 : :
5795 : : /* The addresses start here, but where do they end? */
5796 [ + + ]: 4 : listptr = get_listptr (&known_addrbases, idx);
5797 [ - + ]: 2 : if (listptr == NULL)
5798 : : next_unitp = readendp;
5799 [ + - ]: 2 : else if (listptr->cu->version < 5)
5800 : : {
5801 : 2 : next_unitp = start + listptr->offset;
5802 [ + - - + ]: 2 : if (listptr->offset < off || listptr->offset > data->d_size)
5803 : : {
5804 : 0 : error (0, 0,
5805 : : "Warning: Bad address base for next unit at %"
5806 : : PRIx64, off);
5807 : 0 : next_unitp = readendp;
5808 : : }
5809 : : }
5810 : : else
5811 : : {
5812 : : /* Tricky, we don't have a header for this unit, but
5813 : : there is one for the next. We will have to
5814 : : "guess" how big it is and subtract it from the
5815 : : offset (because that points after the header). */
5816 [ # # ]: 0 : unsigned int offset_size = listptr_offset_size (listptr);
5817 : 0 : Dwarf_Off next_off = (listptr->offset
5818 : 0 : - (offset_size == 4 ? 4 : 12) /* len */
5819 : : - 2 /* version */
5820 : : - 1 /* address size */
5821 : 0 : - 1); /* segment selector size */
5822 : 0 : next_unitp = start + next_off;
5823 [ # # # # ]: 0 : if (next_off < off || next_off > data->d_size)
5824 : : {
5825 : 0 : error (0, 0,
5826 : : "Warning: Couldn't calculate .debug_addr "
5827 : : " unit length at %" PRIx64, off);
5828 : 0 : next_unitp = readendp;
5829 : : }
5830 : : }
5831 : 4 : unit_length = (uint64_t) (next_unitp - readp);
5832 : :
5833 : : /* Pretend we have a header. */
5834 : 4 : printf ("\n");
5835 : 4 : printf (_(" Length: %8" PRIu64 "\n"),
5836 : : unit_length);
5837 : 4 : printf (_(" DWARF version: %8" PRIu16 "\n"), version);
5838 : 4 : printf (_(" Address size: %8" PRIu64 "\n"),
5839 : : (uint64_t) address_size);
5840 : 4 : printf (_(" Segment size: %8" PRIu64 "\n"),
5841 : : (uint64_t) segment_size);
5842 : 4 : printf ("\n");
5843 : : }
5844 : : else
5845 : : {
5846 : : /* OK, we have to parse an header first. */
5847 [ - + ]: 4 : unit_length = read_4ubyte_unaligned_inc (dbg, readp);
5848 [ - + ]: 4 : if (unlikely (unit_length == 0xffffffff))
5849 : : {
5850 [ # # ]: 0 : if (unlikely (readp > readendp - 8))
5851 : : {
5852 : 0 : invalid_data:
5853 : 0 : error (0, 0, "Invalid data");
5854 : 0 : return;
5855 : : }
5856 [ # # ]: 0 : unit_length = read_8ubyte_unaligned_inc (dbg, readp);
5857 : : }
5858 : 4 : printf ("\n");
5859 : 4 : printf (_(" Length: %8" PRIu64 "\n"),
5860 : : unit_length);
5861 : :
5862 : : /* We need at least 2-bytes (version) + 1-byte
5863 : : (addr_size) + 1-byte (segment_size) = 4 bytes to
5864 : : complete the header. And this unit cannot go beyond
5865 : : the section data. */
5866 [ + - ]: 4 : if (readp > readendp - 4
5867 [ + - ]: 4 : || unit_length < 4
5868 [ - + ]: 4 : || unit_length > (uint64_t) (readendp - readp))
5869 : 0 : goto invalid_data;
5870 : :
5871 : 4 : next_unitp = readp + unit_length;
5872 : :
5873 [ - + ]: 4 : version = read_2ubyte_unaligned_inc (dbg, readp);
5874 : 4 : printf (_(" DWARF version: %8" PRIu16 "\n"), version);
5875 : :
5876 [ - + ]: 4 : if (version != 5)
5877 : : {
5878 : 0 : error (0, 0, _("Unknown version"));
5879 : 0 : goto next_unit;
5880 : : }
5881 : :
5882 : 4 : address_size = *readp++;
5883 : 4 : printf (_(" Address size: %8" PRIu64 "\n"),
5884 : : (uint64_t) address_size);
5885 : :
5886 [ - + ]: 4 : if (address_size != 4 && address_size != 8)
5887 : : {
5888 : 0 : error (0, 0, _("unsupported address size"));
5889 : 0 : goto next_unit;
5890 : : }
5891 : :
5892 : 4 : segment_size = *readp++;
5893 : 4 : printf (_(" Segment size: %8" PRIu64 "\n"),
5894 : : (uint64_t) segment_size);
5895 : 4 : printf ("\n");
5896 : :
5897 [ - + ]: 4 : if (segment_size != 0)
5898 : : {
5899 : 0 : error (0, 0, _("unsupported segment size"));
5900 : 0 : goto next_unit;
5901 : : }
5902 : :
5903 [ - + ]: 4 : if (listptr->offset != (Dwarf_Off) (readp - start))
5904 : : {
5905 : 0 : error (0, 0, "Address index doesn't start after header");
5906 : 0 : goto next_unit;
5907 : : }
5908 : : }
5909 : : }
5910 : :
5911 : 8 : int digits = 1;
5912 : 8 : size_t addresses = (next_unitp - readp) / address_size;
5913 [ + + ]: 16 : while (addresses >= 10)
5914 : : {
5915 : 8 : ++digits;
5916 : 8 : addresses /= 10;
5917 : : }
5918 : :
5919 : 8 : unsigned int uidx = 0;
5920 : 8 : size_t index_offset = readp - (const unsigned char *) data->d_buf;
5921 : 8 : printf (" Addresses start at offset 0x%zx:\n", index_offset);
5922 [ + + ]: 152 : while (readp <= next_unitp - address_size)
5923 : : {
5924 [ - + - + : 144 : Dwarf_Addr addr = read_addr_unaligned_inc (address_size, dbg,
- - - + ]
5925 : : readp);
5926 : 144 : printf (" [%*u] ", digits, uidx++);
5927 : 144 : print_dwarf_addr (dwflmod, address_size, addr, addr);
5928 : 144 : printf ("\n");
5929 : : }
5930 : 8 : printf ("\n");
5931 : :
5932 [ + - ]: 8 : if (readp != next_unitp)
5933 : 0 : error (0, 0, "extra %zd bytes at end of unit",
5934 : 0 : (size_t) (next_unitp - readp));
5935 : :
5936 : 8 : next_unit:
5937 : : readp = next_unitp;
5938 : : }
5939 : : }
5940 : :
5941 : : /* Print content of DWARF .debug_aranges section. We fortunately do
5942 : : not have to know a bit about the structure of the section, libdwarf
5943 : : takes care of it. */
5944 : : static void
5945 : 2 : print_decoded_aranges_section (Ebl *ebl, GElf_Ehdr *ehdr, Elf_Scn *scn,
5946 : : GElf_Shdr *shdr, Dwarf *dbg)
5947 : : {
5948 : 2 : Dwarf_Aranges *aranges;
5949 : 2 : size_t cnt;
5950 [ - + ]: 2 : if (unlikely (dwarf_getaranges (dbg, &aranges, &cnt) != 0))
5951 : : {
5952 : 0 : error (0, 0, _("cannot get .debug_aranges content: %s"),
5953 : : dwarf_errmsg (-1));
5954 : 0 : return;
5955 : : }
5956 : :
5957 : 2 : GElf_Shdr glink_mem;
5958 : 2 : GElf_Shdr *glink;
5959 : 2 : glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link), &glink_mem);
5960 [ - + ]: 2 : if (glink == NULL)
5961 : : {
5962 : 0 : error (0, 0, _("invalid sh_link value in section %zu"),
5963 : : elf_ndxscn (scn));
5964 : 0 : return;
5965 : : }
5966 : :
5967 : 2 : printf (ngettext ("\
5968 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 " contains %zu entry:\n",
5969 : : "\
5970 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 " contains %zu entries:\n",
5971 : : cnt),
5972 : : elf_ndxscn (scn), section_name (ebl, shdr),
5973 : 2 : (uint64_t) shdr->sh_offset, cnt);
5974 : :
5975 : : /* Compute floor(log16(cnt)). */
5976 : 2 : size_t tmp = cnt;
5977 : 2 : int digits = 1;
5978 [ - + ]: 2 : while (tmp >= 16)
5979 : : {
5980 : 0 : ++digits;
5981 : 0 : tmp >>= 4;
5982 : : }
5983 : :
5984 [ + + ]: 12 : for (size_t n = 0; n < cnt; ++n)
5985 : : {
5986 : 10 : Dwarf_Arange *runp = dwarf_onearange (aranges, n);
5987 [ - + ]: 10 : if (unlikely (runp == NULL))
5988 : : {
5989 : 0 : printf ("cannot get arange %zu: %s\n", n, dwarf_errmsg (-1));
5990 : 0 : return;
5991 : : }
5992 : :
5993 : 10 : Dwarf_Addr start;
5994 : 10 : Dwarf_Word length;
5995 : 10 : Dwarf_Off offset;
5996 : :
5997 [ - + ]: 10 : if (unlikely (dwarf_getarangeinfo (runp, &start, &length, &offset) != 0))
5998 : 0 : printf (_(" [%*zu] ???\n"), digits, n);
5999 : : else
6000 : 10 : printf (_(" [%*zu] start: %0#*" PRIx64
6001 : : ", length: %5" PRIu64 ", CU DIE offset: %6"
6002 : : PRId64 "\n"),
6003 [ - + ]: 10 : digits, n, ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 10 : 18,
6004 : : (uint64_t) start, (uint64_t) length, (int64_t) offset);
6005 : : }
6006 : : }
6007 : :
6008 : :
6009 : : /* Print content of DWARF .debug_aranges section. */
6010 : : static void
6011 : 92 : print_debug_aranges_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
6012 : : Ebl *ebl, GElf_Ehdr *ehdr, Elf_Scn *scn,
6013 : : GElf_Shdr *shdr, Dwarf *dbg)
6014 : : {
6015 [ + + ]: 92 : if (decodedaranges)
6016 : : {
6017 : 2 : print_decoded_aranges_section (ebl, ehdr, scn, shdr, dbg);
6018 : 2 : return;
6019 : : }
6020 : :
6021 : 90 : Elf_Data *data = get_debug_elf_data (dbg, ebl, IDX_debug_aranges, scn);
6022 [ + - ]: 90 : if (data == NULL)
6023 : : return;
6024 : :
6025 : 90 : printf (_("\
6026 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
6027 : : elf_ndxscn (scn), section_name (ebl, shdr),
6028 : 90 : (uint64_t) shdr->sh_offset);
6029 : :
6030 : 90 : const unsigned char *readp = data->d_buf;
6031 : 90 : const unsigned char *readendp = readp + data->d_size;
6032 : :
6033 [ + + ]: 3476 : while (readp < readendp)
6034 : : {
6035 : 3386 : const unsigned char *hdrstart = readp;
6036 : 3386 : size_t start_offset = hdrstart - (const unsigned char *) data->d_buf;
6037 : :
6038 : 3386 : printf (_("\nTable at offset %zu:\n"), start_offset);
6039 [ - + ]: 3386 : if (readp + 4 > readendp)
6040 : : {
6041 : 0 : invalid_data:
6042 : 0 : error (0, 0, _("invalid data in section [%zu] '%s'"),
6043 : : elf_ndxscn (scn), section_name (ebl, shdr));
6044 : 0 : return;
6045 : : }
6046 : :
6047 [ + + ]: 3386 : Dwarf_Word length = read_4ubyte_unaligned_inc (dbg, readp);
6048 : 3386 : unsigned int length_bytes = 4;
6049 [ - + ]: 3386 : if (length == DWARF3_LENGTH_64_BIT)
6050 : : {
6051 [ # # ]: 0 : if (readp + 8 > readendp)
6052 : 0 : goto invalid_data;
6053 [ # # ]: 0 : length = read_8ubyte_unaligned_inc (dbg, readp);
6054 : 0 : length_bytes = 8;
6055 : : }
6056 : :
6057 : 3386 : const unsigned char *nexthdr = readp + length;
6058 : 3386 : printf (_("\n Length: %6" PRIu64 "\n"),
6059 : : (uint64_t) length);
6060 : :
6061 [ - + ]: 3386 : if (unlikely (length > (size_t) (readendp - readp)))
6062 : 0 : goto invalid_data;
6063 : :
6064 [ - + ]: 3386 : if (length == 0)
6065 : 0 : continue;
6066 : :
6067 [ - + ]: 3386 : if (readp + 2 > readendp)
6068 : 0 : goto invalid_data;
6069 [ + + ]: 3386 : uint_fast16_t version = read_2ubyte_unaligned_inc (dbg, readp);
6070 : 3386 : printf (_(" DWARF version: %6" PRIuFAST16 "\n"),
6071 : : version);
6072 [ - + ]: 3386 : if (version != 2)
6073 : : {
6074 : 0 : error (0, 0, _("unsupported aranges version"));
6075 : 0 : goto next_table;
6076 : : }
6077 : :
6078 : 3386 : Dwarf_Word offset;
6079 [ - + ]: 3386 : if (readp + length_bytes > readendp)
6080 : 0 : goto invalid_data;
6081 [ - + ]: 3386 : if (length_bytes == 8)
6082 [ # # ]: 0 : offset = read_8ubyte_unaligned_inc (dbg, readp);
6083 : : else
6084 [ + + ]: 3386 : offset = read_4ubyte_unaligned_inc (dbg, readp);
6085 : 3386 : printf (_(" CU offset: %6" PRIx64 "\n"),
6086 : : (uint64_t) offset);
6087 : :
6088 [ - + ]: 3386 : if (readp + 1 > readendp)
6089 : 0 : goto invalid_data;
6090 : 3386 : unsigned int address_size = *readp++;
6091 : 3386 : printf (_(" Address size: %6" PRIu64 "\n"),
6092 : : (uint64_t) address_size);
6093 [ - + ]: 3386 : if (address_size != 4 && address_size != 8)
6094 : : {
6095 : 0 : error (0, 0, _("unsupported address size"));
6096 : 0 : goto next_table;
6097 : : }
6098 : :
6099 [ - + ]: 3386 : if (readp + 1 > readendp)
6100 : 0 : goto invalid_data;
6101 : 3386 : unsigned int segment_size = *readp++;
6102 : 3386 : printf (_(" Segment size: %6" PRIu64 "\n\n"),
6103 : : (uint64_t) segment_size);
6104 [ - + - - ]: 3386 : if (segment_size != 0 && segment_size != 4 && segment_size != 8)
6105 : : {
6106 : 0 : error (0, 0, _("unsupported segment size"));
6107 : 0 : goto next_table;
6108 : : }
6109 : :
6110 : : /* Round the address to the next multiple of 2*address_size. */
6111 : 3386 : readp += ((2 * address_size - ((readp - hdrstart) % (2 * address_size)))
6112 : 3386 : % (2 * address_size));
6113 : :
6114 [ + - ]: 7046 : while (readp < nexthdr)
6115 : : {
6116 : 7046 : Dwarf_Word range_address;
6117 : 7046 : Dwarf_Word range_length;
6118 : 7046 : Dwarf_Word segment = 0;
6119 [ - + ]: 7046 : if (readp + 2 * address_size + segment_size > readendp)
6120 : 0 : goto invalid_data;
6121 [ + + ]: 7046 : if (address_size == 4)
6122 : : {
6123 [ + + ]: 92 : range_address = read_4ubyte_unaligned_inc (dbg, readp);
6124 [ + + ]: 92 : range_length = read_4ubyte_unaligned_inc (dbg, readp);
6125 : : }
6126 : : else
6127 : : {
6128 [ + + ]: 6954 : range_address = read_8ubyte_unaligned_inc (dbg, readp);
6129 [ + + ]: 6954 : range_length = read_8ubyte_unaligned_inc (dbg, readp);
6130 : : }
6131 : :
6132 [ - + ]: 7046 : if (segment_size == 4)
6133 [ # # ]: 0 : segment = read_4ubyte_unaligned_inc (dbg, readp);
6134 [ - + ]: 7046 : else if (segment_size == 8)
6135 [ # # ]: 0 : segment = read_8ubyte_unaligned_inc (dbg, readp);
6136 : :
6137 [ + + ]: 7046 : if (range_address == 0 && range_length == 0 && segment == 0)
6138 : : break;
6139 : :
6140 : 3660 : printf (" ");
6141 : 3660 : print_dwarf_addr (dwflmod, address_size, range_address,
6142 : : range_address);
6143 : 3660 : printf ("..");
6144 : 3660 : print_dwarf_addr (dwflmod, address_size,
6145 : 3660 : range_address + range_length - 1,
6146 : : range_length);
6147 [ - + ]: 3660 : if (segment_size != 0)
6148 : 0 : printf (" (%" PRIx64 ")\n", (uint64_t) segment);
6149 : : else
6150 : 3660 : printf ("\n");
6151 : : }
6152 : :
6153 : 3386 : next_table:
6154 [ - + ]: 3386 : if (readp != nexthdr)
6155 : : {
6156 : 0 : size_t padding = nexthdr - readp;
6157 : 0 : printf (_(" %zu padding bytes\n"), padding);
6158 : 0 : readp = nexthdr;
6159 : : }
6160 : : }
6161 : : }
6162 : :
6163 : :
6164 : : static bool is_split_dwarf (Dwarf *dbg, uint64_t *id, Dwarf_CU **split_cu);
6165 : :
6166 : : /* Returns true and sets cu and cu_base if the given Dwarf is a split
6167 : : DWARF (.dwo) file. */
6168 : : static bool
6169 : 0 : split_dwarf_cu_base (Dwarf *dbg, Dwarf_CU **cu, Dwarf_Addr *cu_base)
6170 : : {
6171 : 0 : uint64_t id;
6172 [ # # ]: 0 : if (is_split_dwarf (dbg, &id, cu))
6173 : : {
6174 : 0 : Dwarf_Die cudie;
6175 [ # # ]: 0 : if (dwarf_cu_info (*cu, NULL, NULL, &cudie, NULL, NULL, NULL, NULL) == 0)
6176 : : {
6177 : 0 : *cu_base = cudie_base (&cudie);
6178 : 0 : return true;
6179 : : }
6180 : : }
6181 : : return false;
6182 : : }
6183 : :
6184 : : /* Print content of DWARF .debug_rnglists section. */
6185 : : static void
6186 : 38 : print_debug_rnglists_section (Dwfl_Module *dwflmod,
6187 : : Ebl *ebl,
6188 : : GElf_Ehdr *ehdr __attribute__ ((unused)),
6189 : : Elf_Scn *scn, GElf_Shdr *shdr,
6190 : : Dwarf *dbg __attribute__((unused)))
6191 : : {
6192 : 38 : Elf_Data *data = get_debug_elf_data (dbg, ebl, IDX_debug_rnglists, scn);
6193 [ + - ]: 38 : if (data == NULL)
6194 : 0 : return;
6195 : :
6196 : 38 : printf (_("\
6197 : : \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
6198 : : elf_ndxscn (scn), section_name (ebl, shdr),
6199 : 38 : (uint64_t) shdr->sh_offset);
6200 : :
6201 : : /* For the listptr to get the base address/CU. */
6202 : 38 : sort_listptr (&known_rnglistptr, "rnglistptr");
6203 : 38 : size_t listptr_idx = 0;
6204 : :
6205 : 38 : const unsigned char *readp = data->d_buf;
6206 : 38 : const unsigned char *const dataend = ((unsigned char *) data->d_buf
6207 : 38 : + data->d_size);
6208 [ + + ]: 1880 : while (readp < dataend)
6209 : : {
6210 [ - + ]: 1842 : if (unlikely (readp > dataend - 4))
6211 : : {
6212 : 0 : invalid_data:
6213 : 0 : error (0, 0, _("invalid data in section [%zu] '%s'"),
6214 : : elf_ndxscn (scn), section_name (ebl, shdr));
6215 : 0 : return;
6216 : : }
6217 : :
6218 : 1842 : ptrdiff_t offset = readp - (unsigned char *) data->d_buf;
6219 : 1842 : printf (_("Table at Offset 0x%" PRIx64 ":\n\n"),
6220 : : (uint64_t) offset);
6221 : :
6222 [ - + ]: 1842 : uint64_t unit_length = read_4ubyte_unaligned_inc (dbg, readp);
6223 : 1842 : unsigned int offset_size = 4;
6224 [ - + ]: 1842 : if (unlikely (unit_length == 0xffffffff))
6225 : : {
6226 [ # # ]: 0 : if (unlikely (readp > dataend - 8))
6227 : 0 : goto invalid_data;
6228 : :
6229 [ # # ]: 0 : unit_length = read_8ubyte_unaligned_inc (dbg, readp);
6230 : 0 : offset_size = 8;
6231 : : }
6232 : 1842 : printf (_(" Length: %8" PRIu64 "\n"), unit_length);
6233 : :
6234 : : /* We need at least 2-bytes + 1-byte + 1-byte + 4-bytes = 8
6235 : : bytes to complete the header. And this unit cannot go beyond
6236 : : the section data. */
6237 [ + - ]: 1842 : if (readp > dataend - 8
6238 [ + - ]: 1842 : || unit_length < 8
6239 [ - + ]: 1842 : || unit_length > (uint64_t) (dataend - readp))
6240 : 0 : goto invalid_data;
6241 : :
6242 : 1842 : const unsigned char *nexthdr = readp + unit_length;
6243 : :
6244 [ - + ]: 1842 : uint16_t version = read_2ubyte_unaligned_inc (dbg, readp);
6245 : 1842 : printf (_(" DWARF version: %8" PRIu16 "\n"), version);
6246 : :
6247 [ - + ]: 1842 : if (version != 5)
6248 : : {
6249 : 0 : error (0, 0, _("Unknown version"));
6250 : 0 : goto next_table;
6251 : : }
6252 : :
6253 : 1842 : uint8_t address_size = *readp++;
6254 : 1842 : printf (_(" Address size: %8" PRIu64 "\n"),
6255 : : (uint64_t) address_size);
6256 : :
6257 [ - + ]: 1842 : if (address_size != 4 && address_size != 8)
6258 : : {
6259 : 0 : error (0, 0, _("unsupported address size"));
6260 : 0 : goto next_table;
6261 : : }
6262 : :
6263 : 1842 : uint8_t segment_size = *readp++;
6264 : 1842 : printf (_(" Segment size: %8" PRIu64 "\n"),
6265 : : (uint64_t) segment_size);
6266 : :
6267 [ - + - - ]: 1842 : if (segment_size != 0 && segment_size != 4 && segment_size != 8)
6268 : : {
6269 : 0 : error (0, 0, _("unsupported segment size"));
6270 : 0 : goto next_table;
6271 : : }
6272 : :
6273 [ - + ]: 1842 : uint32_t offset_entry_count = read_4ubyte_unaligned_inc (dbg, readp);
6274 : 1842 : printf (_(" Offset entries: %8" PRIu64 "\n"),
6275 : : (uint64_t) offset_entry_count);
6276 : :
6277 : : /* We need the CU that uses this unit to get the initial base address. */
6278 : 1842 : Dwarf_Addr cu_base = 0;
6279 : 1842 : struct Dwarf_CU *cu = NULL;
6280 [ - + ]: 1842 : if (listptr_cu (&known_rnglistptr, &listptr_idx,
6281 : : (Dwarf_Off) offset,
6282 : 1842 : (Dwarf_Off) (nexthdr - (unsigned char *) data->d_buf),
6283 : : &cu_base, &cu)
6284 [ # # ]: 0 : || split_dwarf_cu_base (dbg, &cu, &cu_base))
6285 : 1842 : {
6286 : 1842 : Dwarf_Die cudie;
6287 [ - + ]: 1842 : if (dwarf_cu_die (cu, &cudie,
6288 : : NULL, NULL, NULL, NULL,
6289 : : NULL, NULL) == NULL)
6290 : 0 : printf (_(" Unknown CU base: "));
6291 : : else
6292 : 1842 : printf (_(" CU [%6" PRIx64 "] base: "),
6293 : : dwarf_dieoffset (&cudie));
6294 : 1842 : print_dwarf_addr (dwflmod, address_size, cu_base, cu_base);
6295 : 1842 : printf ("\n");
6296 : : }
6297 : : else
6298 : 0 : printf (_(" Not associated with a CU.\n"));
6299 : :
6300 : 1842 : printf ("\n");
6301 : :
6302 : 1842 : const unsigned char *offset_array_start = readp;
6303 [ + + ]: 1842 : if (offset_entry_count > 0)
6304 : : {
6305 : 2 : uint64_t max_entries = (unit_length - 8) / offset_size;
6306 [ - + ]: 2 : if (offset_entry_count > max_entries)
6307 : : {
6308 : 0 : error (0, 0,
6309 : 0 : _("too many offset entries for unit length"));
6310 : 0 : offset_entry_count = max_entries;
6311 : : }
6312 : :
6313 : 2 : printf (_(" Offsets starting at 0x%" PRIx64 ":\n"),
6314 : : (uint64_t) (offset_array_start
6315 : 2 : - (unsigned char *) data->d_buf));
6316 [ + + ]: 6 : for (uint32_t idx = 0; idx < offset_entry_count; idx++)
6317 : : {
6318 : 4 : printf (" [%6" PRIu32 "] ", idx);
6319 [ + - ]: 4 : if (offset_size == 4)
6320 : : {
6321 [ - + ]: 4 : uint32_t off = read_4ubyte_unaligned_inc (dbg, readp);
6322 : 4 : printf ("0x%" PRIx32 "\n", off);
6323 : : }
6324 : : else
6325 : : {
6326 [ # # ]: 0 : uint64_t off = read_8ubyte_unaligned_inc (dbg, readp);
6327 : 0 : printf ("0x%" PRIx64 "\n", off);
6328 : : }
6329 : : }
6330 : 2 : printf ("\n");
6331 : : }
6332 : :
6333 : 1842 : Dwarf_Addr base = cu_base;
6334 : 1842 : bool start_of_list = true;
6335 [ + + ]: 128074 : while (readp < nexthdr)
6336 : : {
6337 : 126232 : uint8_t kind = *readp++;
6338 : 126232 : uint64_t op1, op2;
6339 : :
6340 : : /* Skip padding. */
6341 [ - + ]: 126232 : if (start_of_list && kind == DW_RLE_end_of_list)
6342 : 0 : continue;
6343 : :
6344 [ + + ]: 126232 : if (start_of_list)
6345 : : {
6346 : 26414 : base = cu_base;
6347 : 26414 : printf (" Offset: %" PRIx64 ", Index: %" PRIx64 "\n",
6348 : 26414 : (uint64_t) (readp - (unsigned char *) data->d_buf - 1),
6349 : 26414 : (uint64_t) (readp - offset_array_start - 1));
6350 : 26414 : start_of_list = false;
6351 : : }
6352 : :
6353 : 126232 : printf (" %s", dwarf_range_list_encoding_name (kind));
6354 [ + - - - : 126232 : switch (kind)
+ + - +
- ]
6355 : : {
6356 : 26414 : case DW_RLE_end_of_list:
6357 : 26414 : start_of_list = true;
6358 : 26414 : printf ("\n\n");
6359 : 26414 : break;
6360 : :
6361 : 0 : case DW_RLE_base_addressx:
6362 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
6363 : : {
6364 : 0 : invalid_range:
6365 : 0 : error (0, 0, _("invalid range list data"));
6366 : 0 : goto next_table;
6367 : : }
6368 : 0 : get_uleb128 (op1, readp, nexthdr);
6369 : 0 : printf (" %" PRIx64 "\n", op1);
6370 [ # # ]: 0 : if (! print_unresolved_addresses)
6371 : : {
6372 : 0 : Dwarf_Addr addr;
6373 [ # # ]: 0 : if (get_indexed_addr (cu, op1, &addr) != 0)
6374 : 0 : printf (" ???\n");
6375 : : else
6376 : : {
6377 : 0 : printf (" ");
6378 : 0 : print_dwarf_addr (dwflmod, address_size, addr, addr);
6379 : 0 : printf ("\n");
6380 : : }
6381 : : }
6382 : : break;
6383 : :
6384 : 0 : case DW_RLE_startx_endx:
6385 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
6386 : 0 : goto invalid_range;
6387 : 0 : get_uleb128 (op1, readp, nexthdr);
6388 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
6389 : 0 : goto invalid_range;
6390 : 0 : get_uleb128 (op2, readp, nexthdr);
6391 : 0 : printf (" %" PRIx64 ", %" PRIx64 "\n", op1, op2);
6392 [ # # ]: 0 : if (! print_unresolved_addresses)
6393 : : {
6394 : 0 : Dwarf_Addr addr1;
6395 : 0 : Dwarf_Addr addr2;
6396 [ # # ]: 0 : if (get_indexed_addr (cu, op1, &addr1) != 0
6397 [ # # ]: 0 : || get_indexed_addr (cu, op2, &addr2) != 0)
6398 : : {
6399 : 0 : printf (" ???..\n");
6400 : 0 : printf (" ???\n");
6401 : : }
6402 : : else
6403 : : {
6404 : 0 : printf (" ");
6405 : 0 : print_dwarf_addr (dwflmod, address_size, addr1, addr1);
6406 : 0 : printf ("..\n ");
6407 : 0 : print_dwarf_addr (dwflmod, address_size,
6408 : : addr2 - 1, addr2);
6409 : 0 : printf ("\n");
6410 : : }
6411 : : }
6412 : : break;
6413 : :
6414 : 0 : case DW_RLE_startx_length:
6415 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
6416 : 0 : goto invalid_range;
6417 : 0 : get_uleb128 (op1, readp, nexthdr);
6418 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 1)
6419 : 0 : goto invalid_range;
6420 : 0 : get_uleb128 (op2, readp, nexthdr);
6421 : 0 : printf (" %" PRIx64 ", %" PRIx64 "\n", op1, op2);
6422 [ # # ]: 0 : if (! print_unresolved_addresses)
6423 : : {
6424 : 0 : Dwarf_Addr addr1;
6425 : 0 : Dwarf_Addr addr2;
6426 [ # # ]: 0 : if (get_indexed_addr (cu, op1, &addr1) != 0)
6427 : : {
6428 : 0 : printf (" ???..\n");
6429 : 0 : printf (" ???\n");
6430 : : }
6431 : : else
6432 : : {
6433 : 0 : addr2 = addr1 + op2;
6434 : 0 : printf (" ");
6435 : 0 : print_dwarf_addr (dwflmod, address_size, addr1, addr1);
6436 : 0 : printf ("..\n ");
6437 : 0 : print_dwarf_addr (dwflmod, address_size,
6438 : : addr2 - 1, addr2);
6439 : 0 : printf ("\n");
6440 : : }
6441 : : }
6442 : : break;
6443 : :
6444 : 90518 : case DW_RLE_offset_pair:
6445 [ - + ]: 90518 : if ((uint64_t) (nexthdr - readp) < 1)
6446 : 0 : goto invalid_range;
6447 : 90518 : get_uleb128 (op1, readp, nexthdr);
6448 [ - + ]: 90518 : if ((uint64_t) (nexthdr - readp) < 1)
6449 : 0 : goto invalid_range;
6450 : 90518 : get_uleb128 (op2, readp, nexthdr);
6451 : 90518 : printf (" %" PRIx64 ", %" PRIx64 "\n", op1, op2);
6452 [ + - ]: 90518 : if (! print_unresolved_addresses)
6453 : : {
6454 : 90518 : op1 += base;
6455 : 90518 : op2 += base;
6456 : 90518 : printf (" ");
6457 : 90518 : print_dwarf_addr (dwflmod, address_size, op1, op1);
6458 : 90518 : printf ("..\n ");
6459 : 90518 : print_dwarf_addr (dwflmod, address_size, op2 - 1, op2);
6460 : 90518 : printf ("\n");
6461 : : }
6462 : : break;
6463 : :
6464 : 8380 : case DW_RLE_base_address:
6465 [ - + ]: 8380 : if (address_size == 4)
6466 : : {
6467 [ # # ]: 0 : if ((uint64_t) (nexthdr - readp) < 4)
6468 : 0 : goto invalid_range;
6469 [ # # ]: 0 : op1 = read_4ubyte_unaligned_inc (dbg, readp);
6470 : : }
6471 : : else
6472 : : {
6473 [ - + ]: 8380 : if ((uint64_t) (nexthdr - readp) < 8)
6474 : 0 : goto invalid_range;
6475 [ - + ]: 8380 : op1 = read_8ubyte_unaligned_inc (dbg, readp);
6476 : : }
6477 : 8380 : base = op1;
6478 : 8380 : printf (" 0x%" PRIx64 "\n", base);
6479 [ - + ]: 8380 : |