1 /* dwarf.c -- display DWARF contents of a BFD binary file
2    Copyright (C) 2005-2024 Free Software Foundation, Inc.
3 
4    This file is part of GNU Binutils.
5 
6    This program 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    This program is distributed in the hope that it will be useful,
12    but 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, write to the Free Software
18    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
19    02110-1301, USA.  */
20 
21 #include "sysdep.h"
22 #include "libiberty.h"
23 #include "bfd.h"
24 #include <stdint.h>
25 #include "bucomm.h"
26 #include "elfcomm.h"
27 #include "elf/common.h"
28 #include "dwarf2.h"
29 #include "dwarf.h"
30 #include "gdb/gdb-index.h"
31 #include "filenames.h"
32 #include "safe-ctype.h"
33 #include <assert.h>
34 
35 #ifdef HAVE_LIBDEBUGINFOD
36 #include <elfutils/debuginfod.h>
37 #endif
38 
39 #include <limits.h>
40 #ifndef CHAR_BIT
41 #define CHAR_BIT 8
42 #endif
43 
44 #ifndef ENABLE_CHECKING
45 #define ENABLE_CHECKING 0
46 #endif
47 
48 #undef MAX
49 #undef MIN
50 #define MAX(a, b) ((a) > (b) ? (a) : (b))
51 #define MIN(a, b) ((a) < (b) ? (a) : (b))
52 
53 static const char *regname (unsigned int regno, int row);
54 static const char *regname_internal_by_table_only (unsigned int regno);
55 
56 static int have_frame_base;
57 static int frame_base_level = -1; /* To support nested DW_TAG_subprogram's.  */
58 static int need_base_address;
59 
60 static unsigned int num_debug_info_entries = 0;
61 static unsigned int alloc_num_debug_info_entries = 0;
62 static debug_info *debug_information = NULL;
63 /* Special value for num_debug_info_entries to indicate
64    that the .debug_info section could not be loaded/parsed.  */
65 #define DEBUG_INFO_UNAVAILABLE  (unsigned int) -1
66 
67 /* A .debug_info section can contain multiple links to separate
68    DWO object files.  We use these structures to record these links.  */
69 typedef enum dwo_type
70 {
71   DWO_NAME,
72   DWO_DIR,
73   DWO_ID
74 } dwo_type;
75 
76 typedef struct dwo_info
77 {
78   dwo_type          type;
79   const char *      value;
80   uint64_t          cu_offset;
81   struct dwo_info * next;
82 } dwo_info;
83 
84 static dwo_info *first_dwo_info = NULL;
85 static bool need_dwo_info;
86 
87 separate_info * first_separate_info = NULL;
88 
89 unsigned int eh_addr_size;
90 
91 int do_debug_info;
92 int do_debug_abbrevs;
93 int do_debug_lines;
94 int do_debug_pubnames;
95 int do_debug_pubtypes;
96 int do_debug_aranges;
97 int do_debug_ranges;
98 int do_debug_frames;
99 int do_debug_frames_interp;
100 int do_debug_macinfo;
101 int do_debug_str;
102 int do_debug_str_offsets;
103 int do_debug_loc;
104 int do_gdb_index;
105 int do_trace_info;
106 int do_trace_abbrevs;
107 int do_trace_aranges;
108 int do_debug_addr;
109 int do_debug_cu_index;
110 int do_wide;
111 int do_debug_links;
112 int do_follow_links = DEFAULT_FOR_FOLLOW_LINKS;
113 #ifdef HAVE_LIBDEBUGINFOD
114 int use_debuginfod = 1;
115 #endif
116 bool do_checks;
117 
118 int dwarf_cutoff_level = -1;
119 unsigned long dwarf_start_die;
120 
121 int dwarf_check = 0;
122 
123 /* Collection of CU/TU section sets from .debug_cu_index and .debug_tu_index
124    sections.  For version 1 package files, each set is stored in SHNDX_POOL
125    as a zero-terminated list of section indexes comprising one set of debug
126    sections from a .dwo file.  */
127 
128 static unsigned int *shndx_pool = NULL;
129 static unsigned int shndx_pool_size = 0;
130 static unsigned int shndx_pool_used = 0;
131 
132 /* For version 2 package files, each set contains an array of section offsets
133    and an array of section sizes, giving the offset and size of the
134    contribution from a CU or TU within one of the debug sections.
135    When displaying debug info from a package file, we need to use these
136    tables to locate the corresponding contributions to each section.  */
137 
138 struct cu_tu_set
139 {
140   uint64_t signature;
141   uint64_t section_offsets[DW_SECT_MAX];
142   size_t   section_sizes[DW_SECT_MAX];
143 };
144 
145 static int cu_count = 0;
146 static int tu_count = 0;
147 static struct cu_tu_set *cu_sets = NULL;
148 static struct cu_tu_set *tu_sets = NULL;
149 
150 static bool load_cu_tu_indexes (void *);
151 
152 /* An array that indicates for a given level of CU nesting whether
153    the latest DW_AT_type seen for that level was a signed type or
154    an unsigned type.  */
155 #define MAX_CU_NESTING (1 << 8)
156 static bool level_type_signed[MAX_CU_NESTING];
157 
158 /* Values for do_debug_lines.  */
159 #define FLAG_DEBUG_LINES_RAW   1
160 #define FLAG_DEBUG_LINES_DECODED 2
161 
162 static unsigned int
size_of_encoded_value(int encoding)163 size_of_encoded_value (int encoding)
164 {
165   switch (encoding & 0x7)
166     {
167     default:        /* ??? */
168     case 0:         return eh_addr_size;
169     case 2:         return 2;
170     case 3:         return 4;
171     case 4:         return 8;
172     }
173 }
174 
175 static uint64_t
get_encoded_value(unsigned char ** pdata,int encoding,struct dwarf_section * section,unsigned char * end)176 get_encoded_value (unsigned char **pdata,
177                        int encoding,
178                        struct dwarf_section *section,
179                        unsigned char * end)
180 {
181   unsigned char * data = * pdata;
182   unsigned int size = size_of_encoded_value (encoding);
183   uint64_t val;
184 
185   if (data >= end || size > (size_t) (end - data))
186     {
187       warn (_("Encoded value extends past end of section\n"));
188       * pdata = end;
189       return 0;
190     }
191 
192   /* PR 17512: file: 002-829853-0.004.  */
193   if (size > 8)
194     {
195       warn (_("Encoded size of %d is too large to read\n"), size);
196       * pdata = end;
197       return 0;
198     }
199 
200   /* PR 17512: file: 1085-5603-0.004.  */
201   if (size == 0)
202     {
203       warn (_("Encoded size of 0 is too small to read\n"));
204       * pdata = end;
205       return 0;
206     }
207 
208   if (encoding & DW_EH_PE_signed)
209     val = byte_get_signed (data, size);
210   else
211     val = byte_get (data, size);
212 
213   if ((encoding & 0x70) == DW_EH_PE_pcrel)
214     val += section->address + (data - section->start);
215 
216   * pdata = data + size;
217   return val;
218 }
219 
220 /* Print a uint64_t value (typically an address, offset or length) in
221    hexadecimal format, followed by a space.  The precision displayed is
222    determined by the NUM_BYTES parameter.  */
223 
224 static void
print_hex(uint64_t value,unsigned num_bytes)225 print_hex (uint64_t value, unsigned num_bytes)
226 {
227   if (num_bytes == 0)
228     num_bytes = 2;
229 
230   printf ("%0*" PRIx64 " ", num_bytes * 2,
231             value & ~(~(uint64_t) 0 << num_bytes * 4 << num_bytes * 4));
232 }
233 
234 /* Like print_hex, but no trailing space.  */
235 
236 static void
print_hex_ns(uint64_t value,unsigned num_bytes)237 print_hex_ns (uint64_t value, unsigned num_bytes)
238 {
239   if (num_bytes == 0)
240     num_bytes = 2;
241 
242   printf ("%0*" PRIx64, num_bytes * 2,
243             value & ~(~(uint64_t) 0 << num_bytes * 4 << num_bytes * 4));
244 }
245 
246 /* Print a view number in hexadecimal value, with the same width as
247    print_hex would have printed it.  */
248 
249 static void
print_view(uint64_t value,unsigned num_bytes)250 print_view (uint64_t value, unsigned num_bytes)
251 {
252   if (num_bytes == 0)
253     num_bytes = 2;
254 
255   printf ("v%0*" PRIx64 " ", num_bytes * 2 - 1,
256             value & ~(~(uint64_t) 0 << num_bytes * 4 << num_bytes * 4));
257 }
258 
259 static const char *
null_name(const char * p)260 null_name (const char *p)
261 {
262   if (p == NULL)
263     p = _("unknown");
264   return p;
265 }
266 
267 /* Read in a LEB128 encoded value starting at address DATA.
268    If SIGN is true, return a signed LEB128 value.
269    If LENGTH_RETURN is not NULL, return in it the number of bytes read.
270    If STATUS_RETURN is not NULL, return with bit 0 (LSB) set if the
271    terminating byte was not found and with bit 1 set if the value
272    overflows a uint64_t.
273    No bytes will be read at address END or beyond.  */
274 
275 uint64_t
read_leb128(unsigned char * data,const unsigned char * const end,bool sign,unsigned int * length_return,int * status_return)276 read_leb128 (unsigned char *data,
277                const unsigned char *const end,
278                bool sign,
279                unsigned int *length_return,
280                int *status_return)
281 {
282   uint64_t result = 0;
283   unsigned int num_read = 0;
284   unsigned int shift = 0;
285   int status = 1;
286 
287   while (data < end)
288     {
289       unsigned char byte = *data++;
290       unsigned char lost, mask;
291 
292       num_read++;
293 
294       if (shift < CHAR_BIT * sizeof (result))
295           {
296             result |= ((uint64_t) (byte & 0x7f)) << shift;
297             /* These bits overflowed.  */
298             lost = byte ^ (result >> shift);
299             /* And this is the mask of possible overflow bits.  */
300             mask = 0x7f ^ ((uint64_t) 0x7f << shift >> shift);
301             shift += 7;
302           }
303       else
304           {
305             lost = byte;
306             mask = 0x7f;
307           }
308       if ((lost & mask) != (sign && (int64_t) result < 0 ? mask : 0))
309           status |= 2;
310 
311       if ((byte & 0x80) == 0)
312           {
313             status &= ~1;
314             if (sign && shift < CHAR_BIT * sizeof (result) && (byte & 0x40))
315               result |= -((uint64_t) 1 << shift);
316             break;
317           }
318     }
319 
320   if (length_return != NULL)
321     *length_return = num_read;
322   if (status_return != NULL)
323     *status_return = status;
324 
325   return result;
326 }
327 
328 /* Read AMOUNT bytes from PTR and store them in VAL.
329    Checks to make sure that the read will not reach or pass END.
330    FUNC chooses whether the value read is unsigned or signed, and may
331    be either byte_get or byte_get_signed.  If INC is true, PTR is
332    incremented after reading the value.
333    This macro cannot protect against PTR values derived from user input.
334    The C standard sections 6.5.6 and 6.5.8 say attempts to do so using
335    pointers is undefined behaviour.  */
336 #define SAFE_BYTE_GET_INTERNAL(VAL, PTR, AMOUNT, END, FUNC, INC)      \
337   do                                                                                      \
338     {                                                                                     \
339       size_t amount = (AMOUNT);                                                           \
340       if (sizeof (VAL) < amount)                                                \
341           {                                                                               \
342             error (ngettext ("internal error: attempt to read %d byte "         \
343                                  "of data in to %d sized variable",             \
344                                  "internal error: attempt to read %d bytes "    \
345                                  "of data in to %d sized variable",             \
346                                  amount),                                                 \
347                      (int) amount, (int) sizeof (VAL));                         \
348             amount = sizeof (VAL);                                              \
349           }                                                                               \
350       if (ENABLE_CHECKING)                                                      \
351           assert ((PTR) <= (END));                                              \
352       size_t avail = (END) - (PTR);                                             \
353       if ((PTR) > (END))                                                        \
354           avail = 0;                                                                      \
355       if (amount > avail)                                                       \
356           amount = avail;                                                                 \
357       if (amount == 0)                                                                    \
358           (VAL) = 0;                                                                      \
359       else                                                                                \
360           (VAL) = (FUNC) ((PTR), amount);                                                 \
361       if (INC)                                                                            \
362           (PTR) += amount;                                                      \
363     }                                                                                     \
364   while (0)
365 
366 #define SAFE_BYTE_GET(VAL, PTR, AMOUNT, END)      \
367   SAFE_BYTE_GET_INTERNAL (VAL, PTR, AMOUNT, END, byte_get, false)
368 
369 #define SAFE_BYTE_GET_AND_INC(VAL, PTR, AMOUNT, END)        \
370   SAFE_BYTE_GET_INTERNAL (VAL, PTR, AMOUNT, END, byte_get, true)
371 
372 #define SAFE_SIGNED_BYTE_GET(VAL, PTR, AMOUNT, END)         \
373   SAFE_BYTE_GET_INTERNAL (VAL, PTR, AMOUNT, END, byte_get_signed, false)
374 
375 #define SAFE_SIGNED_BYTE_GET_AND_INC(VAL, PTR, AMOUNT, END) \
376   SAFE_BYTE_GET_INTERNAL (VAL, PTR, AMOUNT, END, byte_get_signed, true)
377 
378 typedef struct State_Machine_Registers
379 {
380   uint64_t address;
381   unsigned int view;
382   unsigned int file;
383   unsigned int line;
384   unsigned int column;
385   int is_stmt;
386   int basic_block;
387   unsigned char op_index;
388   unsigned char end_sequence;
389   /* This variable hold the number of the last entry seen
390      in the File Table.  */
391   unsigned int last_file_entry;
392 } SMR;
393 
394 static SMR state_machine_regs;
395 
396 static void
reset_state_machine(int is_stmt)397 reset_state_machine (int is_stmt)
398 {
399   state_machine_regs.address = 0;
400   state_machine_regs.view = 0;
401   state_machine_regs.op_index = 0;
402   state_machine_regs.file = 1;
403   state_machine_regs.line = 1;
404   state_machine_regs.column = 0;
405   state_machine_regs.is_stmt = is_stmt;
406   state_machine_regs.basic_block = 0;
407   state_machine_regs.end_sequence = 0;
408   state_machine_regs.last_file_entry = 0;
409 }
410 
411 /* Handled an extend line op.
412    Returns the number of bytes read.  */
413 
414 static size_t
process_extended_line_op(unsigned char * data,int is_stmt,unsigned char * end)415 process_extended_line_op (unsigned char * data,
416                                 int is_stmt,
417                                 unsigned char * end)
418 {
419   unsigned char op_code;
420   size_t len, header_len;
421   unsigned char *name;
422   unsigned char *orig_data = data;
423   uint64_t adr, val;
424 
425   READ_ULEB (len, data, end);
426   header_len = data - orig_data;
427 
428   if (len == 0 || data >= end || len > (size_t) (end - data))
429     {
430       warn (_("Badly formed extended line op encountered!\n"));
431       return header_len;
432     }
433 
434   op_code = *data++;
435 
436   printf (_("  Extended opcode %d: "), op_code);
437 
438   switch (op_code)
439     {
440     case DW_LNE_end_sequence:
441       printf (_("End of Sequence\n\n"));
442       reset_state_machine (is_stmt);
443       break;
444 
445     case DW_LNE_set_address:
446       /* PR 17512: file: 002-100480-0.004.  */
447       if (len - 1 > 8)
448           {
449             warn (_("Length (%zu) of DW_LNE_set_address op is too long\n"),
450                     len - 1);
451             adr = 0;
452           }
453       else
454           SAFE_BYTE_GET (adr, data, len - 1, end);
455       printf (_("set Address to %#" PRIx64 "\n"), adr);
456       state_machine_regs.address = adr;
457       state_machine_regs.view = 0;
458       state_machine_regs.op_index = 0;
459       break;
460 
461     case DW_LNE_define_file:
462       printf (_("define new File Table entry\n"));
463       printf (_("  Entry\tDir\tTime\tSize\tName\n"));
464       printf ("   %d\t", ++state_machine_regs.last_file_entry);
465 
466       {
467           size_t l;
468 
469           name = data;
470           l = strnlen ((char *) data, end - data);
471           data += l;
472           if (data < end)
473             data++;
474           READ_ULEB (val, data, end);
475           printf ("%" PRIu64 "\t", val);
476           READ_ULEB (val, data, end);
477           printf ("%" PRIu64 "\t", val);
478           READ_ULEB (val, data, end);
479           printf ("%" PRIu64 "\t", val);
480           printf ("%.*s\n\n", (int) l, name);
481       }
482 
483       if (((size_t) (data - orig_data) != len + header_len) || data >= end)
484           warn (_("DW_LNE_define_file: Bad opcode length\n"));
485       break;
486 
487     case DW_LNE_set_discriminator:
488       READ_ULEB (val, data, end);
489       printf (_("set Discriminator to %" PRIu64 "\n"), val);
490       break;
491 
492     /* HP extensions.  */
493     case DW_LNE_HP_negate_is_UV_update:
494       printf ("DW_LNE_HP_negate_is_UV_update\n");
495       break;
496     case DW_LNE_HP_push_context:
497       printf ("DW_LNE_HP_push_context\n");
498       break;
499     case DW_LNE_HP_pop_context:
500       printf ("DW_LNE_HP_pop_context\n");
501       break;
502     case DW_LNE_HP_set_file_line_column:
503       printf ("DW_LNE_HP_set_file_line_column\n");
504       break;
505     case DW_LNE_HP_set_routine_name:
506       printf ("DW_LNE_HP_set_routine_name\n");
507       break;
508     case DW_LNE_HP_set_sequence:
509       printf ("DW_LNE_HP_set_sequence\n");
510       break;
511     case DW_LNE_HP_negate_post_semantics:
512       printf ("DW_LNE_HP_negate_post_semantics\n");
513       break;
514     case DW_LNE_HP_negate_function_exit:
515       printf ("DW_LNE_HP_negate_function_exit\n");
516       break;
517     case DW_LNE_HP_negate_front_end_logical:
518       printf ("DW_LNE_HP_negate_front_end_logical\n");
519       break;
520     case DW_LNE_HP_define_proc:
521       printf ("DW_LNE_HP_define_proc\n");
522       break;
523     case DW_LNE_HP_source_file_correlation:
524       {
525           unsigned char *edata = data + len - 1;
526 
527           printf ("DW_LNE_HP_source_file_correlation\n");
528 
529           while (data < edata)
530             {
531               unsigned int opc;
532 
533               READ_ULEB (opc, data, edata);
534 
535               switch (opc)
536                 {
537                 case DW_LNE_HP_SFC_formfeed:
538                     printf ("    DW_LNE_HP_SFC_formfeed\n");
539                     break;
540                 case DW_LNE_HP_SFC_set_listing_line:
541                     READ_ULEB (val, data, edata);
542                     printf ("    DW_LNE_HP_SFC_set_listing_line (%" PRIu64 ")\n",
543                               val);
544                     break;
545                 case DW_LNE_HP_SFC_associate:
546                     printf ("    DW_LNE_HP_SFC_associate ");
547                     READ_ULEB (val, data, edata);
548                     printf ("(%" PRIu64 , val);
549                     READ_ULEB (val, data, edata);
550                     printf (",%" PRIu64, val);
551                     READ_ULEB (val, data, edata);
552                     printf (",%" PRIu64 ")\n", val);
553                     break;
554                 default:
555                     printf (_("    UNKNOWN DW_LNE_HP_SFC opcode (%u)\n"), opc);
556                     data = edata;
557                     break;
558                 }
559             }
560       }
561       break;
562 
563     default:
564       {
565           unsigned int rlen = len - 1;
566 
567           if (op_code >= DW_LNE_lo_user
568               /* The test against DW_LNW_hi_user is redundant due to
569                  the limited range of the unsigned char data type used
570                  for op_code.  */
571               /*&& op_code <= DW_LNE_hi_user*/)
572             printf (_("user defined: "));
573           else
574             printf (_("UNKNOWN: "));
575           printf (_("length %d ["), rlen);
576           for (; rlen; rlen--)
577             printf (" %02x", *data++);
578           printf ("]\n");
579       }
580       break;
581     }
582 
583   return len + header_len;
584 }
585 
586 static const unsigned char *
fetch_indirect_string(uint64_t offset)587 fetch_indirect_string (uint64_t offset)
588 {
589   struct dwarf_section *section = &debug_displays [str].section;
590   const unsigned char * ret;
591 
592   if (section->start == NULL)
593     return (const unsigned char *) _("<no .debug_str section>");
594 
595   if (offset >= section->size)
596     {
597       warn (_("DW_FORM_strp offset too big: %#" PRIx64 "\n"), offset);
598       return (const unsigned char *) _("<offset is too big>");
599     }
600 
601   ret = section->start + offset;
602   /* Unfortunately we cannot rely upon the .debug_str section ending with a
603      NUL byte.  Since our caller is expecting to receive a well formed C
604      string we test for the lack of a terminating byte here.  */
605   if (strnlen ((const char *) ret, section->size - offset)
606       == section->size - offset)
607     ret = (const unsigned char *)
608       _("<no NUL byte at end of .debug_str section>");
609 
610   return ret;
611 }
612 
613 static const unsigned char *
fetch_indirect_line_string(uint64_t offset)614 fetch_indirect_line_string (uint64_t offset)
615 {
616   struct dwarf_section *section = &debug_displays [line_str].section;
617   const unsigned char * ret;
618 
619   if (section->start == NULL)
620     return (const unsigned char *) _("<no .debug_line_str section>");
621 
622   if (offset >= section->size)
623     {
624       warn (_("DW_FORM_line_strp offset too big: %#" PRIx64 "\n"), offset);
625       return (const unsigned char *) _("<offset is too big>");
626     }
627 
628   ret = section->start + offset;
629   /* Unfortunately we cannot rely upon the .debug_line_str section ending
630      with a NUL byte.  Since our caller is expecting to receive a well formed
631      C string we test for the lack of a terminating byte here.  */
632   if (strnlen ((const char *) ret, section->size - offset)
633       == section->size - offset)
634     ret = (const unsigned char *)
635       _("<no NUL byte at end of .debug_line_str section>");
636 
637   return ret;
638 }
639 
640 static const char *
fetch_indexed_string(uint64_t idx,struct cu_tu_set * this_set,uint64_t offset_size,bool dwo,uint64_t str_offsets_base)641 fetch_indexed_string (uint64_t idx,
642                           struct cu_tu_set *this_set,
643                           uint64_t offset_size,
644                           bool dwo,
645                           uint64_t str_offsets_base)
646 {
647   enum dwarf_section_display_enum str_sec_idx = dwo ? str_dwo : str;
648   enum dwarf_section_display_enum idx_sec_idx = dwo ? str_index_dwo : str_index;
649   struct dwarf_section *index_section = &debug_displays [idx_sec_idx].section;
650   struct dwarf_section *str_section = &debug_displays [str_sec_idx].section;
651   uint64_t index_offset;
652   uint64_t str_offset;
653   const char * ret;
654 
655   if (index_section->start == NULL)
656     return (dwo ? _("<no .debug_str_offsets.dwo section>")
657                     : _("<no .debug_str_offsets section>"));
658 
659   if (str_section->start == NULL)
660     return (dwo ? _("<no .debug_str.dwo section>")
661                     : _("<no .debug_str section>"));
662 
663   if (_mul_overflow (idx, offset_size, &index_offset)
664       || (this_set != NULL
665             && ((index_offset += this_set->section_offsets [DW_SECT_STR_OFFSETS])
666                 < this_set->section_offsets [DW_SECT_STR_OFFSETS]))
667       || (index_offset += str_offsets_base) < str_offsets_base
668       || index_offset + offset_size < offset_size
669       || index_offset + offset_size > index_section->size)
670     {
671       warn (_("string index of %" PRIu64 " converts to an offset of %#" PRIx64
672                 " which is too big for section %s"),
673               idx, index_offset, str_section->name);
674 
675       return _("<string index too big>");
676     }
677 
678   str_offset = byte_get (index_section->start + index_offset, offset_size);
679 
680   str_offset -= str_section->address;
681   if (str_offset >= str_section->size)
682     {
683       warn (_("indirect offset too big: %#" PRIx64 "\n"), str_offset);
684       return _("<indirect index offset is too big>");
685     }
686 
687   ret = (const char *) str_section->start + str_offset;
688 
689   /* Unfortunately we cannot rely upon str_section ending with a NUL byte.
690      Since our caller is expecting to receive a well formed C string we test
691      for the lack of a terminating byte here.  */
692   if (strnlen (ret, str_section->size - str_offset)
693       == str_section->size - str_offset)
694     return _("<no NUL byte at end of section>");
695 
696   return ret;
697 }
698 
699 static uint64_t
fetch_indexed_addr(uint64_t offset,uint32_t num_bytes)700 fetch_indexed_addr (uint64_t offset, uint32_t num_bytes)
701 {
702   struct dwarf_section *section = &debug_displays [debug_addr].section;
703 
704   if (section->start == NULL)
705     {
706       warn (_("Cannot fetch indexed address: the .debug_addr section is missing\n"));
707       return 0;
708     }
709 
710   if (offset + num_bytes > section->size)
711     {
712       warn (_("Offset into section %s too big: %#" PRIx64 "\n"),
713               section->name, offset);
714       return 0;
715     }
716 
717   return byte_get (section->start + offset, num_bytes);
718 }
719 
720 /* This is for resolving DW_FORM_rnglistx and DW_FORM_loclistx.
721 
722    The memory layout is: base_address (taken from the CU's top DIE) points at a table of offsets,
723    relative to the section start.
724    The table of offsets contains the offsets of objects of interest relative to the table of offsets.
725    IDX is the index of the desired object in said table of offsets.
726 
727    This returns the offset of the desired object relative to the section start or -1 upon failure.  */
728 
729 static uint64_t
fetch_indexed_offset(uint64_t idx,enum dwarf_section_display_enum sec_enum,uint64_t base_address,uint64_t offset_size)730 fetch_indexed_offset (uint64_t                         idx,
731                           enum dwarf_section_display_enum  sec_enum,
732                           uint64_t                         base_address,
733                           uint64_t                         offset_size)
734 {
735   uint64_t offset_of_offset = base_address + idx * offset_size;
736   struct dwarf_section *section = &debug_displays [sec_enum].section;
737 
738   if (section->start == NULL)
739     {
740       warn (_("Unable to locate %s section\n"), section->uncompressed_name);
741       return -1;
742     }
743 
744   if (section->size < 4)
745     {
746       warn (_("Section %s is too small to contain an value indexed from another section!\n"),
747               section->name);
748       return -1;
749     }
750 
751   if (offset_of_offset + offset_size >= section->size)
752     {
753       warn (_("Offset of %#" PRIx64 " is too big for section %s\n"),
754               offset_of_offset, section->name);
755       return -1;
756     }
757 
758   return base_address + byte_get (section->start + offset_of_offset, offset_size);
759 }
760 
761 /* FIXME:  There are better and more efficient ways to handle
762    these structures.  For now though, I just want something that
763    is simple to implement.  */
764 /* Records a single attribute in an abbrev.  */
765 typedef struct abbrev_attr
766 {
767   unsigned long attribute;
768   unsigned long form;
769   int64_t implicit_const;
770   struct abbrev_attr *next;
771 }
772 abbrev_attr;
773 
774 /* Records a single abbrev.  */
775 typedef struct abbrev_entry
776 {
777   unsigned long          number;
778   unsigned long          tag;
779   int                    children;
780   struct abbrev_attr *   first_attr;
781   struct abbrev_attr *   last_attr;
782   struct abbrev_entry *  next;
783 }
784 abbrev_entry;
785 
786 /* Records a set of abbreviations.  */
787 typedef struct abbrev_list
788 {
789   abbrev_entry *        first_abbrev;
790   abbrev_entry *        last_abbrev;
791   unsigned char *       raw;
792   struct abbrev_list *  next;
793   unsigned char *       start_of_next_abbrevs;
794 }
795 abbrev_list;
796 
797 /* Records all the abbrevs found so far.  */
798 static struct abbrev_list * abbrev_lists = NULL;
799 
800 typedef struct abbrev_map
801 {
802   uint64_t start;
803   uint64_t end;
804   abbrev_list *list;
805 } abbrev_map;
806 
807 /* Maps between CU offsets and abbrev sets.  */
808 static abbrev_map *   cu_abbrev_map = NULL;
809 static unsigned long  num_abbrev_map_entries = 0;
810 static unsigned long  next_free_abbrev_map_entry = 0;
811 
812 #define INITIAL_NUM_ABBREV_MAP_ENTRIES 8
813 #define ABBREV_MAP_ENTRIES_INCREMENT   8
814 
815 static void
record_abbrev_list_for_cu(uint64_t start,uint64_t end,abbrev_list * list,abbrev_list * free_list)816 record_abbrev_list_for_cu (uint64_t start, uint64_t end,
817                                  abbrev_list *list, abbrev_list *free_list)
818 {
819   if (free_list != NULL)
820     {
821       list->next = abbrev_lists;
822       abbrev_lists = list;
823     }
824 
825   if (cu_abbrev_map == NULL)
826     {
827       num_abbrev_map_entries = INITIAL_NUM_ABBREV_MAP_ENTRIES;
828       cu_abbrev_map = xmalloc (num_abbrev_map_entries * sizeof (* cu_abbrev_map));
829     }
830   else if (next_free_abbrev_map_entry == num_abbrev_map_entries)
831     {
832       num_abbrev_map_entries += ABBREV_MAP_ENTRIES_INCREMENT;
833       cu_abbrev_map = xrealloc (cu_abbrev_map, num_abbrev_map_entries * sizeof (* cu_abbrev_map));
834     }
835 
836   cu_abbrev_map[next_free_abbrev_map_entry].start = start;
837   cu_abbrev_map[next_free_abbrev_map_entry].end = end;
838   cu_abbrev_map[next_free_abbrev_map_entry].list = list;
839   next_free_abbrev_map_entry ++;
840 }
841 
842 static abbrev_list *
free_abbrev_list(abbrev_list * list)843 free_abbrev_list (abbrev_list *list)
844 {
845   abbrev_entry *abbrv = list->first_abbrev;
846 
847   while (abbrv)
848     {
849       abbrev_attr *attr = abbrv->first_attr;
850 
851       while (attr)
852           {
853             abbrev_attr *next_attr = attr->next;
854             free (attr);
855             attr = next_attr;
856           }
857 
858       abbrev_entry *next_abbrev = abbrv->next;
859       free (abbrv);
860       abbrv = next_abbrev;
861     }
862 
863   abbrev_list *next = list->next;
864   free (list);
865   return next;
866 }
867 
868 static void
free_all_abbrevs(void)869 free_all_abbrevs (void)
870 {
871   while (abbrev_lists)
872     abbrev_lists = free_abbrev_list (abbrev_lists);
873 
874   free (cu_abbrev_map);
875   cu_abbrev_map = NULL;
876   next_free_abbrev_map_entry = 0;
877 }
878 
879 static abbrev_list *
find_abbrev_list_by_raw_abbrev(unsigned char * raw)880 find_abbrev_list_by_raw_abbrev (unsigned char *raw)
881 {
882   abbrev_list * list;
883 
884   for (list = abbrev_lists; list != NULL; list = list->next)
885     if (list->raw == raw)
886       return list;
887 
888   return NULL;
889 }
890 
891 /* Find the abbreviation map for the CU that includes OFFSET.
892    OFFSET is an absolute offset from the start of the .debug_info section.  */
893 /* FIXME: This function is going to slow down readelf & objdump.
894    Not caching abbrevs is likely the answer.  */
895 
896 static  abbrev_map *
find_abbrev_map_by_offset(uint64_t offset)897 find_abbrev_map_by_offset (uint64_t offset)
898 {
899   unsigned long i;
900 
901   for (i = 0; i < next_free_abbrev_map_entry; i++)
902     if (cu_abbrev_map[i].start <= offset
903           && cu_abbrev_map[i].end > offset)
904       return cu_abbrev_map + i;
905 
906   return NULL;
907 }
908 
909 static void
add_abbrev(unsigned long number,unsigned long tag,int children,abbrev_list * list)910 add_abbrev (unsigned long  number,
911               unsigned long  tag,
912               int            children,
913               abbrev_list *  list)
914 {
915   abbrev_entry *  entry;
916 
917   entry = (abbrev_entry *) xmalloc (sizeof (*entry));
918 
919   entry->number     = number;
920   entry->tag        = tag;
921   entry->children   = children;
922   entry->first_attr = NULL;
923   entry->last_attr  = NULL;
924   entry->next       = NULL;
925 
926   assert (list != NULL);
927 
928   if (list->first_abbrev == NULL)
929     list->first_abbrev = entry;
930   else
931     list->last_abbrev->next = entry;
932 
933   list->last_abbrev = entry;
934 }
935 
936 static void
add_abbrev_attr(unsigned long attribute,unsigned long form,int64_t implicit_const,abbrev_list * list)937 add_abbrev_attr (unsigned long attribute,
938                      unsigned long form,
939                      int64_t implicit_const,
940                      abbrev_list *list)
941 {
942   abbrev_attr *attr;
943 
944   attr = (abbrev_attr *) xmalloc (sizeof (*attr));
945 
946   attr->attribute      = attribute;
947   attr->form           = form;
948   attr->implicit_const = implicit_const;
949   attr->next           = NULL;
950 
951   assert (list != NULL && list->last_abbrev != NULL);
952 
953   if (list->last_abbrev->first_attr == NULL)
954     list->last_abbrev->first_attr = attr;
955   else
956     list->last_abbrev->last_attr->next = attr;
957 
958   list->last_abbrev->last_attr = attr;
959 }
960 
961 /* Return processed (partial) contents of a .debug_abbrev section.
962    Returns NULL on errors.  */
963 
964 static abbrev_list *
process_abbrev_set(struct dwarf_section * section,unsigned char * start,unsigned char * end)965 process_abbrev_set (struct dwarf_section *section,
966                         unsigned char *start,
967                         unsigned char *end)
968 {
969   abbrev_list *list = xmalloc (sizeof (*list));
970   list->first_abbrev = NULL;
971   list->last_abbrev = NULL;
972   list->raw = start;
973   list->next = NULL;
974 
975   while (start < end)
976     {
977       unsigned long entry;
978       unsigned long tag;
979       unsigned long attribute;
980       int children;
981 
982       READ_ULEB (entry, start, end);
983 
984       /* A single zero is supposed to end the set according
985            to the standard.  If there's more, then signal that to
986            the caller.  */
987       if (start == end || entry == 0)
988           {
989             list->start_of_next_abbrevs = start != end ? start : NULL;
990             return list;
991           }
992 
993       READ_ULEB (tag, start, end);
994       if (start == end)
995           return free_abbrev_list (list);
996 
997       children = *start++;
998 
999       add_abbrev (entry, tag, children, list);
1000 
1001       do
1002           {
1003             unsigned long form;
1004             /* Initialize it due to a false compiler warning.  */
1005             int64_t implicit_const = -1;
1006 
1007             READ_ULEB (attribute, start, end);
1008             if (start == end)
1009               break;
1010 
1011             READ_ULEB (form, start, end);
1012             if (start == end)
1013               break;
1014 
1015             if (form == DW_FORM_implicit_const)
1016               {
1017                 READ_SLEB (implicit_const, start, end);
1018                 if (start == end)
1019                     break;
1020               }
1021 
1022             add_abbrev_attr (attribute, form, implicit_const, list);
1023           }
1024       while (attribute != 0);
1025     }
1026 
1027   /* Report the missing single zero which ends the section.  */
1028   error (_("%s section not zero terminated\n"), section->name);
1029 
1030   return free_abbrev_list (list);
1031 }
1032 
1033 /* Return a sequence of abbrevs in SECTION starting at ABBREV_BASE
1034    plus ABBREV_OFFSET and finishing at ABBREV_BASE + ABBREV_SIZE.
1035    If FREE_LIST is non-NULL search the already decoded abbrevs on
1036    abbrev_lists first and if found set *FREE_LIST to NULL.  If
1037    searching doesn't find a matching abbrev, set *FREE_LIST to the
1038    newly allocated list.  If FREE_LIST is NULL, no search is done and
1039    the returned abbrev_list is always newly allocated.  */
1040 
1041 static abbrev_list *
find_and_process_abbrev_set(struct dwarf_section * section,uint64_t abbrev_base,uint64_t abbrev_size,uint64_t abbrev_offset,abbrev_list ** free_list)1042 find_and_process_abbrev_set (struct dwarf_section *section,
1043                                    uint64_t abbrev_base,
1044                                    uint64_t abbrev_size,
1045                                    uint64_t abbrev_offset,
1046                                    abbrev_list **free_list)
1047 {
1048   if (free_list)
1049     *free_list = NULL;
1050 
1051   if (abbrev_base >= section->size
1052       || abbrev_size > section->size - abbrev_base)
1053     {
1054       /* PR 17531: file:4bcd9ce9.  */
1055       warn (_("Debug info is corrupted, abbrev size (%#" PRIx64 ")"
1056                 " is larger than abbrev section size (%#" PRIx64 ")\n"),
1057                 abbrev_base + abbrev_size, section->size);
1058       return NULL;
1059     }
1060   if (abbrev_offset >= abbrev_size)
1061     {
1062       warn (_("Debug info is corrupted, abbrev offset (%#" PRIx64 ")"
1063                 " is larger than abbrev section size (%#" PRIx64 ")\n"),
1064               abbrev_offset, abbrev_size);
1065       return NULL;
1066     }
1067 
1068   unsigned char *start = section->start + abbrev_base + abbrev_offset;
1069   unsigned char *end = section->start + abbrev_base + abbrev_size;
1070   abbrev_list *list = NULL;
1071   if (free_list)
1072     list = find_abbrev_list_by_raw_abbrev (start);
1073   if (list == NULL)
1074     {
1075       list = process_abbrev_set (section, start, end);
1076       if (free_list)
1077           *free_list = list;
1078     }
1079   return list;
1080 }
1081 
1082 static const char *
get_TAG_name(uint64_t tag)1083 get_TAG_name (uint64_t tag)
1084 {
1085   const char *name = NULL;
1086 
1087   if ((unsigned int) tag == tag)
1088     name = get_DW_TAG_name ((unsigned int) tag);
1089   if (name == NULL)
1090     {
1091       static char buffer[100];
1092 
1093       if (tag >= DW_TAG_lo_user && tag <= DW_TAG_hi_user)
1094           snprintf (buffer, sizeof (buffer),
1095                       _("User TAG value: %#" PRIx64), tag);
1096       else
1097           snprintf (buffer, sizeof (buffer),
1098                       _("Unknown TAG value: %#" PRIx64), tag);
1099       return buffer;
1100     }
1101 
1102   return name;
1103 }
1104 
1105 static const char *
get_FORM_name(unsigned long form)1106 get_FORM_name (unsigned long form)
1107 {
1108   const char *name = NULL;
1109 
1110   if (form == 0)
1111     return "DW_FORM value: 0";
1112 
1113   if ((unsigned int) form == form)
1114     name = get_DW_FORM_name ((unsigned int) form);
1115   if (name == NULL)
1116     {
1117       static char buffer[100];
1118 
1119       snprintf (buffer, sizeof (buffer), _("Unknown FORM value: %lx"), form);
1120       return buffer;
1121     }
1122 
1123   return name;
1124 }
1125 
1126 static const char *
get_IDX_name(unsigned long idx)1127 get_IDX_name (unsigned long idx)
1128 {
1129   const char *name = NULL;
1130 
1131   if ((unsigned int) idx == idx)
1132     name = get_DW_IDX_name ((unsigned int) idx);
1133   if (name == NULL)
1134     {
1135       static char buffer[100];
1136 
1137       snprintf (buffer, sizeof (buffer), _("Unknown IDX value: %lx"), idx);
1138       return buffer;
1139     }
1140 
1141   return name;
1142 }
1143 
1144 static unsigned char *
display_block(unsigned char * data,uint64_t length,const unsigned char * const end,char delimiter)1145 display_block (unsigned char *data,
1146                  uint64_t length,
1147                  const unsigned char * const end, char delimiter)
1148 {
1149   size_t maxlen;
1150 
1151   printf (_("%c%" PRIu64 " byte block: "), delimiter, length);
1152   if (data > end)
1153     return (unsigned char *) end;
1154 
1155   maxlen = end - data;
1156   length = length > maxlen ? maxlen : length;
1157 
1158   while (length --)
1159     printf ("%" PRIx64 " ", byte_get (data++, 1));
1160 
1161   return data;
1162 }
1163 
1164 static int
decode_location_expression(unsigned char * data,unsigned int pointer_size,unsigned int offset_size,int dwarf_version,uint64_t length,uint64_t cu_offset,struct dwarf_section * section)1165 decode_location_expression (unsigned char * data,
1166                                   unsigned int pointer_size,
1167                                   unsigned int offset_size,
1168                                   int dwarf_version,
1169                                   uint64_t length,
1170                                   uint64_t cu_offset,
1171                                   struct dwarf_section * section)
1172 {
1173   unsigned op;
1174   uint64_t uvalue;
1175   int64_t svalue;
1176   unsigned char *end = data + length;
1177   int need_frame_base = 0;
1178 
1179   while (data < end)
1180     {
1181       op = *data++;
1182 
1183       switch (op)
1184           {
1185           case DW_OP_addr:
1186             SAFE_BYTE_GET_AND_INC (uvalue, data, pointer_size, end);
1187             printf ("DW_OP_addr: %" PRIx64, uvalue);
1188             break;
1189           case DW_OP_deref:
1190             printf ("DW_OP_deref");
1191             break;
1192           case DW_OP_const1u:
1193             SAFE_BYTE_GET_AND_INC (uvalue, data, 1, end);
1194             printf ("DW_OP_const1u: %" PRIu64, uvalue);
1195             break;
1196           case DW_OP_const1s:
1197             SAFE_SIGNED_BYTE_GET_AND_INC (svalue, data, 1, end);
1198             printf ("DW_OP_const1s: %" PRId64, svalue);
1199             break;
1200           case DW_OP_const2u:
1201             SAFE_BYTE_GET_AND_INC (uvalue, data, 2, end);
1202             printf ("DW_OP_const2u: %" PRIu64, uvalue);
1203             break;
1204           case DW_OP_const2s:
1205             SAFE_SIGNED_BYTE_GET_AND_INC (svalue, data, 2, end);
1206             printf ("DW_OP_const2s: %" PRId64, svalue);
1207             break;
1208           case DW_OP_const4u:
1209             SAFE_BYTE_GET_AND_INC (uvalue, data, 4, end);
1210             printf ("DW_OP_const4u: %" PRIu64, uvalue);
1211             break;
1212           case DW_OP_const4s:
1213             SAFE_SIGNED_BYTE_GET_AND_INC (svalue, data, 4, end);
1214             printf ("DW_OP_const4s: %" PRId64, svalue);
1215             break;
1216           case DW_OP_const8u:
1217             SAFE_BYTE_GET_AND_INC (uvalue, data, 8, end);
1218             printf ("DW_OP_const8u: %" PRIu64, uvalue);
1219             break;
1220           case DW_OP_const8s:
1221             SAFE_SIGNED_BYTE_GET_AND_INC (svalue, data, 8, end);
1222             printf ("DW_OP_const8s: %" PRId64, svalue);
1223             break;
1224           case DW_OP_constu:
1225             READ_ULEB (uvalue, data, end);
1226             printf ("DW_OP_constu: %" PRIu64, uvalue);
1227             break;
1228           case DW_OP_consts:
1229             READ_SLEB (svalue, data, end);
1230             printf ("DW_OP_consts: %" PRId64, svalue);
1231             break;
1232           case DW_OP_dup:
1233             printf ("DW_OP_dup");
1234             break;
1235           case DW_OP_drop:
1236             printf ("DW_OP_drop");
1237             break;
1238           case DW_OP_over:
1239             printf ("DW_OP_over");
1240             break;
1241           case DW_OP_pick:
1242             SAFE_BYTE_GET_AND_INC (uvalue, data, 1, end);
1243             printf ("DW_OP_pick: %" PRIu64, uvalue);
1244             break;
1245           case DW_OP_swap:
1246             printf ("DW_OP_swap");
1247             break;
1248           case DW_OP_rot:
1249             printf ("DW_OP_rot");
1250             break;
1251           case DW_OP_xderef:
1252             printf ("DW_OP_xderef");
1253             break;
1254           case DW_OP_abs:
1255             printf ("DW_OP_abs");
1256             break;
1257           case DW_OP_and:
1258             printf ("DW_OP_and");
1259             break;
1260           case DW_OP_div:
1261             printf ("DW_OP_div");
1262             break;
1263           case DW_OP_minus:
1264             printf ("DW_OP_minus");
1265             break;
1266           case DW_OP_mod:
1267             printf ("DW_OP_mod");
1268             break;
1269           case DW_OP_mul:
1270             printf ("DW_OP_mul");
1271             break;
1272           case DW_OP_neg:
1273             printf ("DW_OP_neg");
1274             break;
1275           case DW_OP_not:
1276             printf ("DW_OP_not");
1277             break;
1278           case DW_OP_or:
1279             printf ("DW_OP_or");
1280             break;
1281           case DW_OP_plus:
1282             printf ("DW_OP_plus");
1283             break;
1284           case DW_OP_plus_uconst:
1285             READ_ULEB (uvalue, data, end);
1286             printf ("DW_OP_plus_uconst: %" PRIu64, uvalue);
1287             break;
1288           case DW_OP_shl:
1289             printf ("DW_OP_shl");
1290             break;
1291           case DW_OP_shr:
1292             printf ("DW_OP_shr");
1293             break;
1294           case DW_OP_shra:
1295             printf ("DW_OP_shra");
1296             break;
1297           case DW_OP_xor:
1298             printf ("DW_OP_xor");
1299             break;
1300           case DW_OP_bra:
1301             SAFE_SIGNED_BYTE_GET_AND_INC (svalue, data, 2, end);
1302             printf ("DW_OP_bra: %" PRId64, svalue);
1303             break;
1304           case DW_OP_eq:
1305             printf ("DW_OP_eq");
1306             break;
1307           case DW_OP_ge:
1308             printf ("DW_OP_ge");
1309             break;
1310           case DW_OP_gt:
1311             printf ("DW_OP_gt");
1312             break;
1313           case DW_OP_le:
1314             printf ("DW_OP_le");
1315             break;
1316           case DW_OP_lt:
1317             printf ("DW_OP_lt");
1318             break;
1319           case DW_OP_ne:
1320             printf ("DW_OP_ne");
1321             break;
1322           case DW_OP_skip:
1323             SAFE_SIGNED_BYTE_GET_AND_INC (svalue, data, 2, end);
1324             printf ("DW_OP_skip: %" PRId64, svalue);
1325             break;
1326 
1327           case DW_OP_lit0:
1328           case DW_OP_lit1:
1329           case DW_OP_lit2:
1330           case DW_OP_lit3:
1331           case DW_OP_lit4:
1332           case DW_OP_lit5:
1333           case DW_OP_lit6:
1334           case DW_OP_lit7:
1335           case DW_OP_lit8:
1336           case DW_OP_lit9:
1337           case DW_OP_lit10:
1338           case DW_OP_lit11:
1339           case DW_OP_lit12:
1340           case DW_OP_lit13:
1341           case DW_OP_lit14:
1342           case DW_OP_lit15:
1343           case DW_OP_lit16:
1344           case DW_OP_lit17:
1345           case DW_OP_lit18:
1346           case DW_OP_lit19:
1347           case DW_OP_lit20:
1348           case DW_OP_lit21:
1349           case DW_OP_lit22:
1350           case DW_OP_lit23:
1351           case DW_OP_lit24:
1352           case DW_OP_lit25:
1353           case DW_OP_lit26:
1354           case DW_OP_lit27:
1355           case DW_OP_lit28:
1356           case DW_OP_lit29:
1357           case DW_OP_lit30:
1358           case DW_OP_lit31:
1359             printf ("DW_OP_lit%d", op - DW_OP_lit0);
1360             break;
1361 
1362           case DW_OP_reg0:
1363           case DW_OP_reg1:
1364           case DW_OP_reg2:
1365           case DW_OP_reg3:
1366           case DW_OP_reg4:
1367           case DW_OP_reg5:
1368           case DW_OP_reg6:
1369           case DW_OP_reg7:
1370           case DW_OP_reg8:
1371           case DW_OP_reg9:
1372           case DW_OP_reg10:
1373           case DW_OP_reg11:
1374           case DW_OP_reg12:
1375           case DW_OP_reg13:
1376           case DW_OP_reg14:
1377           case DW_OP_reg15:
1378           case DW_OP_reg16:
1379           case DW_OP_reg17:
1380           case DW_OP_reg18:
1381           case DW_OP_reg19:
1382           case DW_OP_reg20:
1383           case DW_OP_reg21:
1384           case DW_OP_reg22:
1385           case DW_OP_reg23:
1386           case DW_OP_reg24:
1387           case DW_OP_reg25:
1388           case DW_OP_reg26:
1389           case DW_OP_reg27:
1390           case DW_OP_reg28:
1391           case DW_OP_reg29:
1392           case DW_OP_reg30:
1393           case DW_OP_reg31:
1394             printf ("DW_OP_reg%d (%s)", op - DW_OP_reg0,
1395                       regname (op - DW_OP_reg0, 1));
1396             break;
1397 
1398           case DW_OP_breg0:
1399           case DW_OP_breg1:
1400           case DW_OP_breg2:
1401           case DW_OP_breg3:
1402           case DW_OP_breg4:
1403           case DW_OP_breg5:
1404           case DW_OP_breg6:
1405           case DW_OP_breg7:
1406           case DW_OP_breg8:
1407           case DW_OP_breg9:
1408           case DW_OP_breg10:
1409           case DW_OP_breg11:
1410           case DW_OP_breg12:
1411           case DW_OP_breg13:
1412           case DW_OP_breg14:
1413           case DW_OP_breg15:
1414           case DW_OP_breg16:
1415           case DW_OP_breg17:
1416           case DW_OP_breg18:
1417           case DW_OP_breg19:
1418           case DW_OP_breg20:
1419           case DW_OP_breg21:
1420           case DW_OP_breg22:
1421           case DW_OP_breg23:
1422           case DW_OP_breg24:
1423           case DW_OP_breg25:
1424           case DW_OP_breg26:
1425           case DW_OP_breg27:
1426           case DW_OP_breg28:
1427           case DW_OP_breg29:
1428           case DW_OP_breg30:
1429           case DW_OP_breg31:
1430             READ_SLEB (svalue, data, end);
1431             printf ("DW_OP_breg%d (%s): %" PRId64,
1432                       op - DW_OP_breg0, regname (op - DW_OP_breg0, 1), svalue);
1433             break;
1434 
1435           case DW_OP_regx:
1436             READ_ULEB (uvalue, data, end);
1437             printf ("DW_OP_regx: %" PRIu64 " (%s)",
1438                       uvalue, regname (uvalue, 1));
1439             break;
1440           case DW_OP_fbreg:
1441             need_frame_base = 1;
1442             READ_SLEB (svalue, data, end);
1443             printf ("DW_OP_fbreg: %" PRId64, svalue);
1444             break;
1445           case DW_OP_bregx:
1446             READ_ULEB (uvalue, data, end);
1447             READ_SLEB (svalue, data, end);
1448             printf ("DW_OP_bregx: %" PRIu64 " (%s) %" PRId64,
1449                       uvalue, regname (uvalue, 1), svalue);
1450             break;
1451           case DW_OP_piece:
1452             READ_ULEB (uvalue, data, end);
1453             printf ("DW_OP_piece: %" PRIu64, uvalue);
1454             break;
1455           case DW_OP_deref_size:
1456             SAFE_BYTE_GET_AND_INC (uvalue, data, 1, end);
1457             printf ("DW_OP_deref_size: %" PRIu64, uvalue);
1458             break;
1459           case DW_OP_xderef_size:
1460             SAFE_BYTE_GET_AND_INC (uvalue, data, 1, end);
1461             printf ("DW_OP_xderef_size: %" PRIu64, uvalue);
1462             break;
1463           case DW_OP_nop:
1464             printf ("DW_OP_nop");
1465             break;
1466 
1467             /* DWARF 3 extensions.  */
1468           case DW_OP_push_object_address:
1469             printf ("DW_OP_push_object_address");
1470             break;
1471           case DW_OP_call2:
1472             /* FIXME: Strictly speaking for 64-bit DWARF3 files
1473                this ought to be an 8-byte wide computation.  */
1474             SAFE_SIGNED_BYTE_GET_AND_INC (svalue, data, 2, end);
1475             printf ("DW_OP_call2: <%#" PRIx64 ">", svalue + cu_offset);
1476             break;
1477           case DW_OP_call4:
1478             /* FIXME: Strictly speaking for 64-bit DWARF3 files
1479                this ought to be an 8-byte wide computation.  */
1480             SAFE_SIGNED_BYTE_GET_AND_INC (svalue, data, 4, end);
1481             printf ("DW_OP_call4: <%#" PRIx64 ">", svalue + cu_offset);
1482             break;
1483           case DW_OP_call_ref:
1484             /* FIXME: Strictly speaking for 64-bit DWARF3 files
1485                this ought to be an 8-byte wide computation.  */
1486             if (dwarf_version == -1)
1487               {
1488                 printf (_("(DW_OP_call_ref in frame info)"));
1489                 /* No way to tell where the next op is, so just bail.  */
1490                 return need_frame_base;
1491               }
1492             if (dwarf_version == 2)
1493               {
1494                 SAFE_BYTE_GET_AND_INC (uvalue, data, pointer_size, end);
1495               }
1496             else
1497               {
1498                 SAFE_BYTE_GET_AND_INC (uvalue, data, offset_size, end);
1499               }
1500             printf ("DW_OP_call_ref: <%#" PRIx64 ">", uvalue);
1501             break;
1502           case DW_OP_form_tls_address:
1503             printf ("DW_OP_form_tls_address");
1504             break;
1505           case DW_OP_call_frame_cfa:
1506             printf ("DW_OP_call_frame_cfa");
1507             break;
1508           case DW_OP_bit_piece:
1509             printf ("DW_OP_bit_piece: ");
1510             READ_ULEB (uvalue, data, end);
1511             printf (_("size: %" PRIu64 " "), uvalue);
1512             READ_ULEB (uvalue, data, end);
1513             printf (_("offset: %" PRIu64 " "), uvalue);
1514             break;
1515 
1516             /* DWARF 4 extensions.  */
1517           case DW_OP_stack_value:
1518             printf ("DW_OP_stack_value");
1519             break;
1520 
1521           case DW_OP_implicit_value:
1522             printf ("DW_OP_implicit_value");
1523             READ_ULEB (uvalue, data, end);
1524             data = display_block (data, uvalue, end, ' ');
1525             break;
1526 
1527             /* GNU extensions.  */
1528           case DW_OP_GNU_push_tls_address:
1529             printf (_("DW_OP_GNU_push_tls_address or DW_OP_HP_unknown"));
1530             break;
1531           case DW_OP_GNU_uninit:
1532             printf ("DW_OP_GNU_uninit");
1533             /* FIXME: Is there data associated with this OP ?  */
1534             break;
1535           case DW_OP_GNU_encoded_addr:
1536             {
1537               int encoding = 0;
1538               uint64_t addr;
1539 
1540               if (data < end)
1541                 encoding = *data++;
1542               addr = get_encoded_value (&data, encoding, section, end);
1543 
1544               printf ("DW_OP_GNU_encoded_addr: fmt:%02x addr:", encoding);
1545               print_hex_ns (addr, pointer_size);
1546             }
1547             break;
1548           case DW_OP_implicit_pointer:
1549           case DW_OP_GNU_implicit_pointer:
1550             /* FIXME: Strictly speaking for 64-bit DWARF3 files
1551                this ought to be an 8-byte wide computation.  */
1552             if (dwarf_version == -1)
1553               {
1554                 printf (_("(%s in frame info)"),
1555                           (op == DW_OP_implicit_pointer
1556                            ? "DW_OP_implicit_pointer"
1557                            : "DW_OP_GNU_implicit_pointer"));
1558                 /* No way to tell where the next op is, so just bail.  */
1559                 return need_frame_base;
1560               }
1561             if (dwarf_version == 2)
1562               {
1563                 SAFE_BYTE_GET_AND_INC (uvalue, data, pointer_size, end);
1564               }
1565             else
1566               {
1567                 SAFE_BYTE_GET_AND_INC (uvalue, data, offset_size, end);
1568               }
1569             READ_SLEB (svalue, data, end);
1570             printf ("%s: <%#" PRIx64 "> %" PRId64,
1571                       (op == DW_OP_implicit_pointer
1572                        ? "DW_OP_implicit_pointer" : "DW_OP_GNU_implicit_pointer"),
1573                       uvalue, svalue);
1574             break;
1575           case DW_OP_entry_value:
1576           case DW_OP_GNU_entry_value:
1577             READ_ULEB (uvalue, data, end);
1578             /* PR 17531: file: 0cc9cd00.  */
1579             if (uvalue > (size_t) (end - data))
1580               uvalue = end - data;
1581             printf ("%s: (", (op == DW_OP_entry_value ? "DW_OP_entry_value"
1582                                                                 : "DW_OP_GNU_entry_value"));
1583             if (decode_location_expression (data, pointer_size, offset_size,
1584                                                     dwarf_version, uvalue,
1585                                                     cu_offset, section))
1586               need_frame_base = 1;
1587             putchar (')');
1588             data += uvalue;
1589             break;
1590           case DW_OP_const_type:
1591           case DW_OP_GNU_const_type:
1592             READ_ULEB (uvalue, data, end);
1593             printf ("%s: <%#" PRIx64 "> ",
1594                       (op == DW_OP_const_type ? "DW_OP_const_type"
1595                                                     : "DW_OP_GNU_const_type"),
1596                       cu_offset + uvalue);
1597             SAFE_BYTE_GET_AND_INC (uvalue, data, 1, end);
1598             data = display_block (data, uvalue, end, ' ');
1599             break;
1600           case DW_OP_regval_type:
1601           case DW_OP_GNU_regval_type:
1602             READ_ULEB (uvalue, data, end);
1603             printf ("%s: %" PRIu64 " (%s)",
1604                       (op == DW_OP_regval_type ? "DW_OP_regval_type"
1605                                                      : "DW_OP_GNU_regval_type"),
1606                       uvalue, regname (uvalue, 1));
1607             READ_ULEB (uvalue, data, end);
1608             printf (" <%#" PRIx64 ">", cu_offset + uvalue);
1609             break;
1610           case DW_OP_deref_type:
1611           case DW_OP_GNU_deref_type:
1612             SAFE_BYTE_GET_AND_INC (uvalue, data, 1, end);
1613             printf ("%s: %" PRId64,
1614                       (op == DW_OP_deref_type ? "DW_OP_deref_type"
1615                                                     : "DW_OP_GNU_deref_type"),
1616                       uvalue);
1617             READ_ULEB (uvalue, data, end);
1618             printf (" <%#" PRIx64 ">", cu_offset + uvalue);
1619             break;
1620           case DW_OP_convert:
1621           case DW_OP_GNU_convert:
1622             READ_ULEB (uvalue, data, end);
1623             printf ("%s <%#" PRIx64 ">",
1624                       (op == DW_OP_convert ? "DW_OP_convert" : "DW_OP_GNU_convert"),
1625                       uvalue ? cu_offset + uvalue : uvalue);
1626             break;
1627           case DW_OP_reinterpret:
1628           case DW_OP_GNU_reinterpret:
1629             READ_ULEB (uvalue, data, end);
1630             printf ("%s <%#" PRIx64 ">",
1631                       (op == DW_OP_reinterpret ? "DW_OP_reinterpret"
1632                                                      : "DW_OP_GNU_reinterpret"),
1633                       uvalue ? cu_offset + uvalue : uvalue);
1634             break;
1635           case DW_OP_GNU_parameter_ref:
1636             SAFE_BYTE_GET_AND_INC (uvalue, data, 4, end);
1637             printf ("DW_OP_GNU_parameter_ref: <%#" PRIx64 ">",
1638                       cu_offset + uvalue);
1639             break;
1640           case DW_OP_addrx:
1641             READ_ULEB (uvalue, data, end);
1642             printf ("DW_OP_addrx <%#" PRIx64 ">", uvalue);
1643             break;
1644           case DW_OP_GNU_addr_index:
1645             READ_ULEB (uvalue, data, end);
1646             printf ("DW_OP_GNU_addr_index <%#" PRIx64 ">", uvalue);
1647             break;
1648           case DW_OP_GNU_const_index:
1649             READ_ULEB (uvalue, data, end);
1650             printf ("DW_OP_GNU_const_index <%#" PRIx64 ">", uvalue);
1651             break;
1652           case DW_OP_GNU_variable_value:
1653             /* FIXME: Strictly speaking for 64-bit DWARF3 files
1654                this ought to be an 8-byte wide computation.  */
1655             if (dwarf_version == -1)
1656               {
1657                 printf (_("(DW_OP_GNU_variable_value in frame info)"));
1658                 /* No way to tell where the next op is, so just bail.  */
1659                 return need_frame_base;
1660               }
1661             if (dwarf_version == 2)
1662               {
1663                 SAFE_BYTE_GET_AND_INC (uvalue, data, pointer_size, end);
1664               }
1665             else
1666               {
1667                 SAFE_BYTE_GET_AND_INC (uvalue, data, offset_size, end);
1668               }
1669             printf ("DW_OP_GNU_variable_value: <%#" PRIx64 ">", uvalue);
1670             break;
1671 
1672             /* HP extensions.  */
1673           case DW_OP_HP_is_value:
1674             printf ("DW_OP_HP_is_value");
1675             /* FIXME: Is there data associated with this OP ?  */
1676             break;
1677           case DW_OP_HP_fltconst4:
1678             printf ("DW_OP_HP_fltconst4");
1679             /* FIXME: Is there data associated with this OP ?  */
1680             break;
1681           case DW_OP_HP_fltconst8:
1682             printf ("DW_OP_HP_fltconst8");
1683             /* FIXME: Is there data associated with this OP ?  */
1684             break;
1685           case DW_OP_HP_mod_range:
1686             printf ("DW_OP_HP_mod_range");
1687             /* FIXME: Is there data associated with this OP ?  */
1688             break;
1689           case DW_OP_HP_unmod_range:
1690             printf ("DW_OP_HP_unmod_range");
1691             /* FIXME: Is there data associated with this OP ?  */
1692             break;
1693           case DW_OP_HP_tls:
1694             printf ("DW_OP_HP_tls");
1695             /* FIXME: Is there data associated with this OP ?  */
1696             break;
1697 
1698             /* PGI (STMicroelectronics) extensions.  */
1699           case DW_OP_PGI_omp_thread_num:
1700             /* Pushes the thread number for the current thread as it would be
1701                returned by the standard OpenMP library function:
1702                omp_get_thread_num().  The "current thread" is the thread for
1703                which the expression is being evaluated.  */
1704             printf ("DW_OP_PGI_omp_thread_num");
1705             break;
1706 
1707           default:
1708             if (op >= DW_OP_lo_user
1709                 && op <= DW_OP_hi_user)
1710               printf (_("(User defined location op %#x)"), op);
1711             else
1712               printf (_("(Unknown location op %#x)"), op);
1713             /* No way to tell where the next op is, so just bail.  */
1714             return need_frame_base;
1715           }
1716 
1717       /* Separate the ops.  */
1718       if (data < end)
1719           printf ("; ");
1720     }
1721 
1722   return need_frame_base;
1723 }
1724 
1725 /* Find the CU or TU set corresponding to the given CU_OFFSET.
1726    This is used for DWARF package files.  */
1727 
1728 static struct cu_tu_set *
find_cu_tu_set_v2(uint64_t cu_offset,int do_types)1729 find_cu_tu_set_v2 (uint64_t cu_offset, int do_types)
1730 {
1731   struct cu_tu_set *p;
1732   unsigned int nsets;
1733   unsigned int dw_sect;
1734 
1735   if (do_types)
1736     {
1737       p = tu_sets;
1738       nsets = tu_count;
1739       dw_sect = DW_SECT_TYPES;
1740     }
1741   else
1742     {
1743       p = cu_sets;
1744       nsets = cu_count;
1745       dw_sect = DW_SECT_INFO;
1746     }
1747   while (nsets > 0)
1748     {
1749       if (p->section_offsets [dw_sect] == cu_offset)
1750           return p;
1751       p++;
1752       nsets--;
1753     }
1754   return NULL;
1755 }
1756 
1757 static const char *
fetch_alt_indirect_string(uint64_t offset)1758 fetch_alt_indirect_string (uint64_t offset)
1759 {
1760   separate_info * i;
1761 
1762   if (! do_follow_links)
1763     return "";
1764 
1765   if (first_separate_info == NULL)
1766     return _("<no links available>");
1767 
1768   for (i = first_separate_info; i != NULL; i = i->next)
1769     {
1770       struct dwarf_section * section;
1771       const char *           ret;
1772 
1773       if (! load_debug_section (separate_debug_str, i->handle))
1774           continue;
1775 
1776       section = &debug_displays [separate_debug_str].section;
1777 
1778       if (section->start == NULL)
1779           continue;
1780 
1781       if (offset >= section->size)
1782           continue;
1783 
1784       ret = (const char *) (section->start + offset);
1785       /* Unfortunately we cannot rely upon the .debug_str section ending with a
1786            NUL byte.  Since our caller is expecting to receive a well formed C
1787            string we test for the lack of a terminating byte here.  */
1788       if (strnlen ((const char *) ret, section->size - offset)
1789             == section->size - offset)
1790           return _("<no NUL byte at end of alt .debug_str section>");
1791 
1792       return ret;
1793     }
1794 
1795   warn (_("DW_FORM_GNU_strp_alt offset (%#" PRIx64 ")"
1796             " too big or no string sections available\n"), offset);
1797   return _("<offset is too big>");
1798 }
1799 
1800 static const char *
get_AT_name(unsigned long attribute)1801 get_AT_name (unsigned long attribute)
1802 {
1803   const char *name;
1804 
1805   if (attribute == 0)
1806     return "DW_AT value: 0";
1807 
1808   /* One value is shared by the MIPS and HP extensions:  */
1809   if (attribute == DW_AT_MIPS_fde)
1810     return "DW_AT_MIPS_fde or DW_AT_HP_unmodifiable";
1811 
1812   name = get_DW_AT_name (attribute);
1813 
1814   if (name == NULL)
1815     {
1816       static char buffer[100];
1817 
1818       snprintf (buffer, sizeof (buffer), _("Unknown AT value: %lx"),
1819                     attribute);
1820       return buffer;
1821     }
1822 
1823   return name;
1824 }
1825 
1826 static void
add_dwo_info(const char * value,uint64_t cu_offset,dwo_type type)1827 add_dwo_info (const char * value, uint64_t cu_offset, dwo_type type)
1828 {
1829   dwo_info * dwinfo = xmalloc (sizeof * dwinfo);
1830 
1831   dwinfo->type   = type;
1832   dwinfo->value  = value;
1833   dwinfo->cu_offset = cu_offset;
1834   dwinfo->next   = first_dwo_info;
1835   first_dwo_info = dwinfo;
1836 }
1837 
1838 static void
add_dwo_name(const char * name,uint64_t cu_offset)1839 add_dwo_name (const char * name, uint64_t cu_offset)
1840 {
1841   add_dwo_info (name, cu_offset, DWO_NAME);
1842 }
1843 
1844 static void
add_dwo_dir(const char * dir,uint64_t cu_offset)1845 add_dwo_dir (const char * dir, uint64_t cu_offset)
1846 {
1847   add_dwo_info (dir, cu_offset, DWO_DIR);
1848 }
1849 
1850 static void
add_dwo_id(const char * id,uint64_t cu_offset)1851 add_dwo_id (const char * id, uint64_t cu_offset)
1852 {
1853   add_dwo_info (id, cu_offset, DWO_ID);
1854 }
1855 
1856 static void
free_dwo_info(void)1857 free_dwo_info (void)
1858 {
1859   dwo_info * dwinfo;
1860   dwo_info * next;
1861 
1862   for (dwinfo = first_dwo_info; dwinfo != NULL; dwinfo = next)
1863     {
1864       next = dwinfo->next;
1865       free (dwinfo);
1866     }
1867   first_dwo_info = NULL;
1868 }
1869 
1870 /* Ensure that START + UVALUE is less than END.
1871    Return an adjusted UVALUE if necessary to ensure this relationship.  */
1872 
1873 static inline uint64_t
check_uvalue(const unsigned char * start,uint64_t uvalue,const unsigned char * end)1874 check_uvalue (const unsigned char *start,
1875                 uint64_t uvalue,
1876                 const unsigned char *end)
1877 {
1878   uint64_t max_uvalue = end - start;
1879 
1880   /* See PR 17512: file: 008-103549-0.001:0.1.
1881      and PR 24829 for examples of where these tests are triggered.  */
1882   if (uvalue > max_uvalue)
1883     {
1884       warn (_("Corrupt attribute block length: %#" PRIx64 "\n"), uvalue);
1885       uvalue = max_uvalue;
1886     }
1887 
1888   return uvalue;
1889 }
1890 
1891 static unsigned char *
skip_attr_bytes(unsigned long form,unsigned char * data,unsigned char * end,uint64_t pointer_size,uint64_t offset_size,int dwarf_version,uint64_t * value_return)1892 skip_attr_bytes (unsigned long form,
1893                      unsigned char *data,
1894                      unsigned char *end,
1895                      uint64_t pointer_size,
1896                      uint64_t offset_size,
1897                      int dwarf_version,
1898                      uint64_t *value_return)
1899 {
1900   int64_t svalue;
1901   uint64_t uvalue = 0;
1902   uint64_t inc = 0;
1903 
1904   * value_return = 0;
1905 
1906   switch (form)
1907     {
1908     case DW_FORM_ref_addr:
1909       if (dwarf_version == 2)
1910           SAFE_BYTE_GET_AND_INC (uvalue, data, pointer_size, end);
1911       else if (dwarf_version > 2)
1912           SAFE_BYTE_GET_AND_INC (uvalue, data, offset_size, end);
1913       else
1914           return NULL;
1915       break;
1916 
1917     case DW_FORM_addr:
1918       SAFE_BYTE_GET_AND_INC (uvalue, data, pointer_size, end);
1919       break;
1920 
1921     case DW_FORM_strp:
1922     case DW_FORM_line_strp:
1923     case DW_FORM_sec_offset:
1924     case DW_FORM_GNU_ref_alt:
1925     case DW_FORM_GNU_strp_alt:
1926       SAFE_BYTE_GET_AND_INC (uvalue, data, offset_size, end);
1927       break;
1928 
1929     case DW_FORM_flag_present:
1930       uvalue = 1;
1931       break;
1932 
1933     case DW_FORM_ref1:
1934     case DW_FORM_flag:
1935     case DW_FORM_data1:
1936     case DW_FORM_strx1:
1937     case DW_FORM_addrx1:
1938       SAFE_BYTE_GET_AND_INC (uvalue, data, 1, end);
1939       break;
1940 
1941     case DW_FORM_strx3:
1942     case DW_FORM_addrx3:
1943       SAFE_BYTE_GET_AND_INC (uvalue, data, 3, end);
1944       break;
1945 
1946     case DW_FORM_ref2:
1947     case DW_FORM_data2:
1948     case DW_FORM_strx2:
1949     case DW_FORM_addrx2:
1950       SAFE_BYTE_GET_AND_INC (uvalue, data, 2, end);
1951       break;
1952 
1953     case DW_FORM_ref4:
1954     case DW_FORM_data4:
1955     case DW_FORM_strx4:
1956     case DW_FORM_addrx4:
1957       SAFE_BYTE_GET_AND_INC (uvalue, data, 4, end);
1958       break;
1959 
1960     case DW_FORM_sdata:
1961       READ_SLEB (svalue, data, end);
1962       uvalue = svalue;
1963       break;
1964 
1965     case DW_FORM_ref_udata:
1966     case DW_FORM_udata:
1967     case DW_FORM_GNU_str_index:
1968     case DW_FORM_strx:
1969     case DW_FORM_GNU_addr_index:
1970     case DW_FORM_addrx:
1971     case DW_FORM_loclistx:
1972     case DW_FORM_rnglistx:
1973       READ_ULEB (uvalue, data, end);
1974       break;
1975 
1976     case DW_FORM_ref8:
1977       SAFE_BYTE_GET_AND_INC (uvalue, data, 8, end);
1978       break;
1979 
1980     case DW_FORM_data8:
1981     case DW_FORM_ref_sig8:
1982       inc = 8;
1983       break;
1984 
1985     case DW_FORM_data16:
1986       inc = 16;
1987       break;
1988 
1989     case DW_FORM_string:
1990       inc = strnlen ((char *) data, end - data) + 1;
1991       break;
1992 
1993     case DW_FORM_block:
1994     case DW_FORM_exprloc:
1995       READ_ULEB (uvalue, data, end);
1996       inc = uvalue;
1997       break;
1998 
1999     case DW_FORM_block1:
2000       SAFE_BYTE_GET_AND_INC (uvalue, data, 1, end);
2001       inc = uvalue;
2002       break;
2003 
2004     case DW_FORM_block2:
2005       SAFE_BYTE_GET_AND_INC (uvalue, data, 2, end);
2006       inc = uvalue;
2007       break;
2008 
2009     case DW_FORM_block4:
2010       SAFE_BYTE_GET_AND_INC (uvalue, data, 4, end);
2011       inc = uvalue;
2012       break;
2013 
2014     case DW_FORM_indirect:
2015       READ_ULEB (form, data, end);
2016       if (form == DW_FORM_implicit_const)
2017           SKIP_ULEB (data, end);
2018       return skip_attr_bytes (form, data, end, pointer_size, offset_size,
2019                                     dwarf_version, value_return);
2020 
2021     default:
2022       return NULL;
2023     }
2024 
2025   * value_return = uvalue;
2026   if (inc <= (size_t) (end - data))
2027     data += inc;
2028   else
2029     data = end;
2030   return data;
2031 }
2032 
2033 /* Given form FORM with value UVALUE, locate and return the abbreviation
2034    associated with it.  */
2035 
2036 static abbrev_entry *
get_type_abbrev_from_form(unsigned long form,unsigned long uvalue,uint64_t cu_offset,unsigned char * cu_end,const struct dwarf_section * section,unsigned long * abbrev_num_return,unsigned char ** data_return,abbrev_map ** map_return)2037 get_type_abbrev_from_form (unsigned long form,
2038                                  unsigned long uvalue,
2039                                  uint64_t cu_offset,
2040                                  unsigned char *cu_end,
2041                                  const struct dwarf_section *section,
2042                                  unsigned long *abbrev_num_return,
2043                                  unsigned char **data_return,
2044                                  abbrev_map **map_return)
2045 {
2046   unsigned long   abbrev_number;
2047   abbrev_map *    map;
2048   abbrev_entry *  entry;
2049   unsigned char * data;
2050 
2051   if (abbrev_num_return != NULL)
2052     * abbrev_num_return = 0;
2053   if (data_return != NULL)
2054     * data_return = NULL;
2055 
2056   switch (form)
2057     {
2058     case DW_FORM_GNU_ref_alt:
2059     case DW_FORM_ref_sig8:
2060       /* FIXME: We are unable to handle this form at the moment.  */
2061       return NULL;
2062 
2063     case DW_FORM_ref_addr:
2064       if (uvalue >= section->size)
2065           {
2066             warn (_("Unable to resolve ref_addr form: uvalue %lx "
2067                       "> section size %" PRIx64 " (%s)\n"),
2068                     uvalue, section->size, section->name);
2069             return NULL;
2070           }
2071       break;
2072 
2073     case DW_FORM_ref_sup4:
2074     case DW_FORM_ref_sup8:
2075       break;
2076 
2077     case DW_FORM_ref1:
2078     case DW_FORM_ref2:
2079     case DW_FORM_ref4:
2080     case DW_FORM_ref8:
2081     case DW_FORM_ref_udata:
2082       if (uvalue + cu_offset < uvalue
2083             || uvalue + cu_offset > (size_t) (cu_end - section->start))
2084           {
2085             warn (_("Unable to resolve ref form: uvalue %lx + cu_offset %" PRIx64
2086                       " > CU size %tx\n"),
2087                     uvalue, cu_offset, cu_end - section->start);
2088             return NULL;
2089           }
2090       uvalue += cu_offset;
2091       break;
2092 
2093       /* FIXME: Are there other DW_FORMs that can be used by types ?  */
2094 
2095     default:
2096       warn (_("Unexpected form %lx encountered whilst finding abbreviation for type\n"), form);
2097       return NULL;
2098     }
2099 
2100   data = (unsigned char *) section->start + uvalue;
2101   map = find_abbrev_map_by_offset (uvalue);
2102 
2103   if (map == NULL)
2104     {
2105       warn (_("Unable to find abbreviations for CU offset %#lx\n"), uvalue);
2106       return NULL;
2107     }
2108   if (map->list == NULL)
2109     {
2110       warn (_("Empty abbreviation list encountered for CU offset %lx\n"), uvalue);
2111       return NULL;
2112     }
2113 
2114   if (map_return != NULL)
2115     {
2116       if (form == DW_FORM_ref_addr)
2117           *map_return = map;
2118       else
2119           *map_return = NULL;
2120     }
2121 
2122   READ_ULEB (abbrev_number, data, section->start + section->size);
2123 
2124   for (entry = map->list->first_abbrev; entry != NULL; entry = entry->next)
2125     if (entry->number == abbrev_number)
2126       break;
2127 
2128   if (abbrev_num_return != NULL)
2129     * abbrev_num_return = abbrev_number;
2130 
2131   if (data_return != NULL)
2132     * data_return = data;
2133 
2134   if (entry == NULL)
2135     warn (_("Unable to find entry for abbreviation %lu\n"), abbrev_number);
2136 
2137   return entry;
2138 }
2139 
2140 /* Return IS_SIGNED set to TRUE if the type using abbreviation ENTRY
2141    can be determined to be a signed type.  The data for ENTRY can be
2142    found starting at DATA.  */
2143 
2144 static void
get_type_signedness(abbrev_entry * entry,const struct dwarf_section * section,unsigned char * data,unsigned char * end,uint64_t cu_offset,uint64_t pointer_size,uint64_t offset_size,int dwarf_version,bool * is_signed,unsigned int nesting)2145 get_type_signedness (abbrev_entry *entry,
2146                          const struct dwarf_section *section,
2147                          unsigned char *data,
2148                          unsigned char *end,
2149                          uint64_t cu_offset,
2150                          uint64_t pointer_size,
2151                          uint64_t offset_size,
2152                          int dwarf_version,
2153                          bool *is_signed,
2154                          unsigned int nesting)
2155 {
2156   abbrev_attr *   attr;
2157 
2158   * is_signed = false;
2159 
2160 #define MAX_NESTING 20
2161   if (nesting > MAX_NESTING)
2162     {
2163       /* FIXME: Warn - or is this expected ?
2164            NB/ We need to avoid infinite recursion.  */
2165       return;
2166     }
2167 
2168   for (attr = entry->first_attr;
2169        attr != NULL && attr->attribute;
2170        attr = attr->next)
2171     {
2172       unsigned char * orig_data = data;
2173       uint64_t uvalue = 0;
2174 
2175       data = skip_attr_bytes (attr->form, data, end, pointer_size,
2176                                     offset_size, dwarf_version, & uvalue);
2177       if (data == NULL)
2178           return;
2179 
2180       switch (attr->attribute)
2181           {
2182           case DW_AT_linkage_name:
2183           case DW_AT_name:
2184             if (do_wide)
2185               {
2186                 if (attr->form == DW_FORM_strp)
2187                     printf (", %s", fetch_indirect_string (uvalue));
2188                 else if (attr->form == DW_FORM_string)
2189                     printf (", %.*s", (int) (end - orig_data), orig_data);
2190               }
2191             break;
2192 
2193           case DW_AT_type:
2194             /* Recurse.  */
2195             {
2196               abbrev_entry *type_abbrev;
2197               unsigned char *type_data;
2198               abbrev_map *map;
2199 
2200               type_abbrev = get_type_abbrev_from_form (attr->form,
2201                                                                  uvalue,
2202                                                                  cu_offset,
2203                                                                  end,
2204                                                                  section,
2205                                                                  NULL /* abbrev num return */,
2206                                                                  &type_data,
2207                                                                  &map);
2208               if (type_abbrev == NULL)
2209                 break;
2210 
2211               get_type_signedness (type_abbrev, section, type_data,
2212                                          map ? section->start + map->end : end,
2213                                          map ? map->start : cu_offset,
2214                                          pointer_size, offset_size, dwarf_version,
2215                                          is_signed, nesting + 1);
2216             }
2217             break;
2218 
2219           case DW_AT_encoding:
2220             /* Determine signness.  */
2221             switch (uvalue)
2222               {
2223               case DW_ATE_address:
2224                 /* FIXME - some architectures have signed addresses.  */
2225               case DW_ATE_boolean:
2226               case DW_ATE_unsigned:
2227               case DW_ATE_unsigned_char:
2228               case DW_ATE_unsigned_fixed:
2229                 * is_signed = false;
2230                 break;
2231 
2232               default:
2233               case DW_ATE_complex_float:
2234               case DW_ATE_float:
2235               case DW_ATE_signed:
2236               case DW_ATE_signed_char:
2237               case DW_ATE_imaginary_float:
2238               case DW_ATE_decimal_float:
2239               case DW_ATE_signed_fixed:
2240                 * is_signed = true;
2241                 break;
2242               }
2243             break;
2244           }
2245     }
2246 }
2247 
2248 static void
read_and_print_leb128(unsigned char * data,unsigned int * bytes_read,unsigned const char * end,bool is_signed)2249 read_and_print_leb128 (unsigned char *data,
2250                            unsigned int *bytes_read,
2251                            unsigned const char *end,
2252                            bool is_signed)
2253 {
2254   int status;
2255   uint64_t val = read_leb128 (data, end, is_signed, bytes_read, &status);
2256   if (status != 0)
2257     report_leb_status (status);
2258   else if (is_signed)
2259     printf ("%" PRId64, val);
2260   else
2261     printf ("%" PRIu64, val);
2262 }
2263 
2264 static void
display_discr_list(unsigned long form,uint64_t uvalue,unsigned char * data,int level)2265 display_discr_list (unsigned long form,
2266                         uint64_t uvalue,
2267                         unsigned char *data,
2268                         int level)
2269 {
2270   unsigned char *end = data;
2271 
2272   if (uvalue == 0)
2273     {
2274       printf ("[default]");
2275       return;
2276     }
2277 
2278   switch (form)
2279     {
2280     case DW_FORM_block:
2281     case DW_FORM_block1:
2282     case DW_FORM_block2:
2283     case DW_FORM_block4:
2284       /* Move data pointer back to the start of the byte array.  */
2285       data -= uvalue;
2286       break;
2287     default:
2288       printf ("<corrupt>\n");
2289       warn (_("corrupt discr_list - not using a block form\n"));
2290       return;
2291     }
2292 
2293   if (uvalue < 2)
2294     {
2295       printf ("<corrupt>\n");
2296       warn (_("corrupt discr_list - block not long enough\n"));
2297       return;
2298     }
2299 
2300   bool is_signed = (level > 0 && level <= MAX_CU_NESTING
2301                         ? level_type_signed [level - 1] : false);
2302 
2303   printf ("(");
2304   while (data < end)
2305     {
2306       unsigned char     discriminant;
2307       unsigned int      bytes_read;
2308 
2309       SAFE_BYTE_GET_AND_INC (discriminant, data, 1, end);
2310 
2311       switch (discriminant)
2312           {
2313           case DW_DSC_label:
2314             printf ("label ");
2315             read_and_print_leb128 (data, & bytes_read, end, is_signed);
2316             data += bytes_read;
2317             break;
2318 
2319           case DW_DSC_range:
2320             printf ("range ");
2321             read_and_print_leb128 (data, & bytes_read, end, is_signed);
2322             data += bytes_read;
2323 
2324             printf ("..");
2325             read_and_print_leb128 (data, & bytes_read, end, is_signed);
2326             data += bytes_read;
2327             break;
2328 
2329           default:
2330             printf ("<corrupt>\n");
2331             warn (_("corrupt discr_list - unrecognized discriminant byte %#x\n"),
2332                     discriminant);
2333             return;
2334           }
2335 
2336       if (data < end)
2337           printf (", ");
2338     }
2339 
2340   if (is_signed)
2341     printf (")(signed)");
2342   else
2343     printf (")(unsigned)");
2344 }
2345 
2346 static void
display_lang(uint64_t uvalue)2347 display_lang (uint64_t uvalue)
2348 {
2349   switch (uvalue)
2350     {
2351       /* Ordered by the numeric value of these constants.  */
2352     case DW_LANG_C89:                             printf ("ANSI C"); break;
2353     case DW_LANG_C:                     printf ("non-ANSI C"); break;
2354     case DW_LANG_Ada83:                           printf ("Ada"); break;
2355     case DW_LANG_C_plus_plus:           printf ("C++"); break;
2356     case DW_LANG_Cobol74:               printf ("Cobol 74"); break;
2357     case DW_LANG_Cobol85:               printf ("Cobol 85"); break;
2358     case DW_LANG_Fortran77:             printf ("FORTRAN 77"); break;
2359     case DW_LANG_Fortran90:             printf ("Fortran 90"); break;
2360     case DW_LANG_Pascal83:              printf ("ANSI Pascal"); break;
2361     case DW_LANG_Modula2:               printf ("Modula 2"); break;
2362 
2363       /* DWARF 2.1 values.    */
2364     case DW_LANG_Java:                            printf ("Java"); break;
2365     case DW_LANG_C99:                             printf ("ANSI C99"); break;
2366     case DW_LANG_Ada95:                           printf ("ADA 95"); break;
2367     case DW_LANG_Fortran95:             printf ("Fortran 95"); break;
2368 
2369       /* DWARF 3 values.  */
2370     case DW_LANG_PLI:                             printf ("PLI"); break;
2371     case DW_LANG_ObjC:                            printf ("Objective C"); break;
2372     case DW_LANG_ObjC_plus_plus:        printf ("Objective C++"); break;
2373     case DW_LANG_UPC:                             printf ("Unified Parallel C"); break;
2374     case DW_LANG_D:                     printf ("D"); break;
2375 
2376       /* DWARF 4 values.  */
2377     case DW_LANG_Python:                printf ("Python"); break;
2378 
2379       /* DWARF 5 values.  */
2380     case DW_LANG_OpenCL:                printf ("OpenCL"); break;
2381     case DW_LANG_Go:                              printf ("Go"); break;
2382     case DW_LANG_Modula3:               printf ("Modula 3"); break;
2383     case DW_LANG_Haskell:               printf ("Haskell"); break;
2384     case DW_LANG_C_plus_plus_03:        printf ("C++03"); break;
2385     case DW_LANG_C_plus_plus_11:        printf ("C++11"); break;
2386     case DW_LANG_OCaml:                           printf ("OCaml"); break;
2387     case DW_LANG_Rust:                            printf ("Rust"); break;
2388     case DW_LANG_C11:                             printf ("C11"); break;
2389     case DW_LANG_Swift:                           printf ("Swift"); break;
2390     case DW_LANG_Julia:                           printf ("Julia"); break;
2391     case DW_LANG_Dylan:                           printf ("Dylan"); break;
2392     case DW_LANG_C_plus_plus_14:        printf ("C++14"); break;
2393     case DW_LANG_Fortran03:             printf ("Fortran 03"); break;
2394     case DW_LANG_Fortran08:             printf ("Fortran 08"); break;
2395     case DW_LANG_RenderScript:                    printf ("RenderScript"); break;
2396 
2397       /* MIPS extension.  */
2398     case DW_LANG_Mips_Assembler:        printf ("MIPS assembler"); break;
2399 
2400       /* UPC extension.  */
2401     case DW_LANG_Upc:                             printf ("Unified Parallel C"); break;
2402 
2403     default:
2404       if (uvalue >= DW_LANG_lo_user && uvalue <= DW_LANG_hi_user)
2405           printf (_("implementation defined: %#" PRIx64 ""), uvalue);
2406       else
2407           printf (_("unknown: %#" PRIx64 ""), uvalue);
2408       break;
2409     }
2410 }
2411 
2412 static unsigned char *
read_and_display_attr_value(unsigned long attribute,unsigned long form,int64_t implicit_const,unsigned char * start,unsigned char * data,unsigned char * end,uint64_t cu_offset,uint64_t pointer_size,uint64_t offset_size,int dwarf_version,debug_info * debug_info_p,int do_loc,struct dwarf_section * section,struct cu_tu_set * this_set,char delimiter,int level)2413 read_and_display_attr_value (unsigned long attribute,
2414                                    unsigned long form,
2415                                    int64_t implicit_const,
2416                                    unsigned char *start,
2417                                    unsigned char *data,
2418                                    unsigned char *end,
2419                                    uint64_t cu_offset,
2420                                    uint64_t pointer_size,
2421                                    uint64_t offset_size,
2422                                    int dwarf_version,
2423                                    debug_info *debug_info_p,
2424                                    int do_loc,
2425                                    struct dwarf_section *section,
2426                                    struct cu_tu_set *this_set,
2427                                    char delimiter,
2428                                    int level)
2429 {
2430   int64_t svalue;
2431   uint64_t uvalue = 0;
2432   uint64_t uvalue_hi = 0;
2433   unsigned char *block_start = NULL;
2434   unsigned char *orig_data = data;
2435 
2436   if (data > end || (data == end && form != DW_FORM_flag_present))
2437     {
2438       warn (_("Corrupt attribute\n"));
2439       return data;
2440     }
2441 
2442   if (do_wide && ! do_loc)
2443     {
2444       /* PR 26847: Display the name of the form.  */
2445       const char * name = get_FORM_name (form);
2446 
2447       /* For convenience we skip the DW_FORM_ prefix to the name.  */
2448       if (name[0] == 'D')
2449           name += 8; /* strlen ("DW_FORM_")  */
2450       printf ("%c(%s)", delimiter, name);
2451     }
2452 
2453   switch (form)
2454     {
2455     case DW_FORM_ref_addr:
2456       if (dwarf_version == 2)
2457           SAFE_BYTE_GET_AND_INC (uvalue, data, pointer_size, end);
2458       else if (dwarf_version > 2)
2459           SAFE_BYTE_GET_AND_INC (uvalue, data, offset_size, end);
2460       else
2461           error (_("Internal error: DW_FORM_ref_addr is not supported in DWARF version 1.\n"));
2462       break;
2463 
2464     case DW_FORM_addr:
2465       SAFE_BYTE_GET_AND_INC (uvalue, data, pointer_size, end);
2466       break;
2467 
2468     case DW_FORM_strp_sup:
2469     case DW_FORM_strp:
2470     case DW_FORM_line_strp:
2471     case DW_FORM_sec_offset:
2472     case DW_FORM_GNU_ref_alt:
2473     case DW_FORM_GNU_strp_alt:
2474       SAFE_BYTE_GET_AND_INC (uvalue, data, offset_size, end);
2475       break;
2476 
2477     case DW_FORM_flag_present:
2478       uvalue = 1;
2479       break;
2480 
2481     case DW_FORM_ref1:
2482     case DW_FORM_flag:
2483     case DW_FORM_data1:
2484     case DW_FORM_strx1:
2485     case DW_FORM_addrx1:
2486       SAFE_BYTE_GET_AND_INC (uvalue, data, 1, end);
2487       break;
2488 
2489     case DW_FORM_ref2:
2490     case DW_FORM_data2:
2491     case DW_FORM_strx2:
2492     case DW_FORM_addrx2:
2493       SAFE_BYTE_GET_AND_INC (uvalue, data, 2, end);
2494       break;
2495 
2496     case DW_FORM_strx3:
2497     case DW_FORM_addrx3:
2498       SAFE_BYTE_GET_AND_INC (uvalue, data, 3, end);
2499       break;
2500 
2501     case DW_FORM_ref_sup4:
2502     case DW_FORM_ref4:
2503     case DW_FORM_data4:
2504     case DW_FORM_strx4:
2505     case DW_FORM_addrx4:
2506       SAFE_BYTE_GET_AND_INC (uvalue, data, 4, end);
2507       break;
2508 
2509     case DW_FORM_ref_sup8:
2510     case DW_FORM_ref8:
2511     case DW_FORM_data8:
2512     case DW_FORM_ref_sig8:
2513       SAFE_BYTE_GET_AND_INC (uvalue, data, 8, end);
2514       break;
2515 
2516     case DW_FORM_data16:
2517       SAFE_BYTE_GET_AND_INC (uvalue, data, 8, end);
2518       SAFE_BYTE_GET_AND_INC (uvalue_hi, data, 8, end);
2519       if (byte_get != byte_get_little_endian)
2520           {
2521             uint64_t utmp = uvalue;
2522             uvalue = uvalue_hi;
2523             uvalue_hi = utmp;
2524           }
2525       break;
2526 
2527     case DW_FORM_sdata:
2528       READ_SLEB (svalue, data, end);
2529       uvalue = svalue;
2530       break;
2531 
2532     case DW_FORM_GNU_str_index:
2533     case DW_FORM_strx:
2534     case DW_FORM_ref_udata:
2535     case DW_FORM_udata:
2536     case DW_FORM_GNU_addr_index:
2537     case DW_FORM_addrx:
2538     case DW_FORM_loclistx:
2539     case DW_FORM_rnglistx:
2540       READ_ULEB (uvalue, data, end);
2541       break;
2542 
2543     case DW_FORM_indirect:
2544       READ_ULEB (form, data, end);
2545       if (!do_loc)
2546           printf ("%c%s", delimiter, get_FORM_name (form));
2547       if (form == DW_FORM_implicit_const)
2548           READ_SLEB (implicit_const, data, end);
2549       return read_and_display_attr_value (attribute, form, implicit_const,
2550                                                     start, data, end,
2551                                                     cu_offset, pointer_size,
2552                                                     offset_size, dwarf_version,
2553                                                     debug_info_p, do_loc,
2554                                                     section, this_set, delimiter, level);
2555 
2556     case DW_FORM_implicit_const:
2557       uvalue = implicit_const;
2558       break;
2559 
2560     default:
2561       break;
2562     }
2563 
2564   switch (form)
2565     {
2566     case DW_FORM_ref_addr:
2567       if (!do_loc)
2568           printf ("%c<%#" PRIx64 ">", delimiter, uvalue);
2569       break;
2570 
2571     case DW_FORM_GNU_ref_alt:
2572       if (!do_loc)
2573           {
2574             if (do_wide)
2575               /* We have already printed the form name.  */
2576               printf ("%c<%#" PRIx64 ">", delimiter, uvalue);
2577             else
2578               printf ("%c<alt %#" PRIx64 ">", delimiter, uvalue);
2579           }
2580       /* FIXME: Follow the reference...  */
2581       break;
2582 
2583     case DW_FORM_ref1:
2584     case DW_FORM_ref2:
2585     case DW_FORM_ref4:
2586     case DW_FORM_ref_sup4:
2587     case DW_FORM_ref_udata:
2588       if (!do_loc)
2589           printf ("%c<%#" PRIx64 ">", delimiter, uvalue + cu_offset);
2590       break;
2591 
2592     case DW_FORM_data4:
2593     case DW_FORM_addr:
2594     case DW_FORM_sec_offset:
2595       if (!do_loc)
2596           printf ("%c%#" PRIx64, delimiter, uvalue);
2597       break;
2598 
2599     case DW_FORM_flag_present:
2600     case DW_FORM_flag:
2601     case DW_FORM_data1:
2602     case DW_FORM_data2:
2603     case DW_FORM_sdata:
2604       if (!do_loc)
2605           printf ("%c%" PRId64, delimiter, uvalue);
2606       break;
2607 
2608     case DW_FORM_udata:
2609       if (!do_loc)
2610           printf ("%c%" PRIu64, delimiter, uvalue);
2611       break;
2612 
2613     case DW_FORM_implicit_const:
2614       if (!do_loc)
2615           printf ("%c%" PRId64, delimiter, implicit_const);
2616       break;
2617 
2618     case DW_FORM_ref_sup8:
2619     case DW_FORM_ref8:
2620     case DW_FORM_data8:
2621       if (!do_loc)
2622           {
2623             uint64_t utmp = uvalue;
2624             if (form == DW_FORM_ref8)
2625               utmp += cu_offset;
2626             printf ("%c%#" PRIx64, delimiter, utmp);
2627           }
2628       break;
2629 
2630     case DW_FORM_data16:
2631       if (!do_loc)
2632           {
2633             if (uvalue_hi == 0)
2634               printf (" %#" PRIx64, uvalue);
2635             else
2636               printf (" %#" PRIx64 "%016" PRIx64, uvalue_hi, uvalue);
2637           }
2638       break;
2639 
2640     case DW_FORM_string:
2641       if (!do_loc)
2642           printf ("%c%.*s", delimiter, (int) (end - data), data);
2643       data += strnlen ((char *) data, end - data);
2644       if (data < end)
2645           data++;
2646       break;
2647 
2648     case DW_FORM_block:
2649     case DW_FORM_exprloc:
2650       READ_ULEB (uvalue, data, end);
2651     do_block:
2652       block_start = data;
2653       if (block_start >= end)
2654           {
2655             warn (_("Block ends prematurely\n"));
2656             uvalue = 0;
2657             block_start = end;
2658           }
2659 
2660       uvalue = check_uvalue (block_start, uvalue, end);
2661 
2662       data = block_start + uvalue;
2663       if (!do_loc)
2664           {
2665             unsigned char op;
2666 
2667             SAFE_BYTE_GET (op, block_start, sizeof (op), end);
2668             if (op != DW_OP_addrx)
2669               data = display_block (block_start, uvalue, end, delimiter);
2670           }
2671       break;
2672 
2673     case DW_FORM_block1:
2674       SAFE_BYTE_GET_AND_INC (uvalue, data, 1, end);
2675       goto do_block;
2676 
2677     case DW_FORM_block2:
2678       SAFE_BYTE_GET_AND_INC (uvalue, data, 2, end);
2679       goto do_block;
2680 
2681     case DW_FORM_block4:
2682       SAFE_BYTE_GET_AND_INC (uvalue, data, 4, end);
2683       goto do_block;
2684 
2685     case DW_FORM_strp:
2686       if (!do_loc)
2687           {
2688             if (do_wide)
2689               /* We have already displayed the form name.  */
2690               printf (_("%c(offset: %#" PRIx64 "): %s"),
2691                         delimiter, uvalue, fetch_indirect_string (uvalue));
2692             else
2693               printf (_("%c(indirect string, offset: %#" PRIx64 "): %s"),
2694                         delimiter, uvalue, fetch_indirect_string (uvalue));
2695           }
2696       break;
2697 
2698     case DW_FORM_line_strp:
2699       if (!do_loc)
2700           {
2701             if (do_wide)
2702               /* We have already displayed the form name.  */
2703               printf (_("%c(offset: %#" PRIx64 "): %s"),
2704                         delimiter, uvalue, fetch_indirect_line_string (uvalue));
2705             else
2706               printf (_("%c(indirect line string, offset: %#" PRIx64 "): %s"),
2707                         delimiter, uvalue, fetch_indirect_line_string (uvalue));
2708           }
2709       break;
2710 
2711     case DW_FORM_GNU_str_index:
2712     case DW_FORM_strx:
2713     case DW_FORM_strx1:
2714     case DW_FORM_strx2:
2715     case DW_FORM_strx3:
2716     case DW_FORM_strx4:
2717       if (!do_loc)
2718           {
2719             const char *suffix = section ? strrchr (section->name, '.') : NULL;
2720             bool dwo = suffix && strcmp (suffix, ".dwo") == 0;
2721             const char *strng;
2722 
2723             strng = fetch_indexed_string (uvalue, this_set, offset_size, dwo,
2724                                                   debug_info_p ? debug_info_p->str_offsets_base : 0);
2725             if (do_wide)
2726               /* We have already displayed the form name.  */
2727               printf (_("%c(offset: %#" PRIx64 "): %s"),
2728                         delimiter, uvalue, strng);
2729             else
2730               printf (_("%c(indexed string: %#" PRIx64 "): %s"),
2731                         delimiter, uvalue, strng);
2732           }
2733       break;
2734 
2735     case DW_FORM_GNU_strp_alt:
2736       if (!do_loc)
2737           {
2738             if (do_wide)
2739               /* We have already displayed the form name.  */
2740               printf (_("%c(offset: %#" PRIx64 ") %s"),
2741                         delimiter, uvalue, fetch_alt_indirect_string (uvalue));
2742             else
2743               printf (_("%c(alt indirect string, offset: %#" PRIx64 ") %s"),
2744                         delimiter, uvalue, fetch_alt_indirect_string (uvalue));
2745           }
2746       break;
2747 
2748     case DW_FORM_indirect:
2749       /* Handled above.  */
2750       break;
2751 
2752     case DW_FORM_ref_sig8:
2753       if (!do_loc)
2754           printf ("%c%s: %#" PRIx64, delimiter, do_wide ? "" : "signature",
2755                     uvalue);
2756       break;
2757 
2758     case DW_FORM_GNU_addr_index:
2759     case DW_FORM_addrx:
2760     case DW_FORM_addrx1:
2761     case DW_FORM_addrx2:
2762     case DW_FORM_addrx3:
2763     case DW_FORM_addrx4:
2764     case DW_FORM_loclistx:
2765     case DW_FORM_rnglistx:
2766       if (!do_loc)
2767           {
2768             uint64_t base, idx;
2769             const char *suffix = strrchr (section->name, '.');
2770             bool dwo = suffix && strcmp (suffix, ".dwo") == 0;
2771 
2772             if (form == DW_FORM_loclistx)
2773               {
2774                 if (debug_info_p == NULL)
2775                     idx = -1;
2776                 else if (dwo)
2777                     {
2778                       idx = fetch_indexed_offset (uvalue, loclists_dwo,
2779                                                         debug_info_p->loclists_base,
2780                                                         debug_info_p->offset_size);
2781                       if (idx != (uint64_t) -1)
2782                         idx += (offset_size == 8) ? 20 : 12;
2783                     }
2784                 else if (dwarf_version > 4)
2785                     {
2786                       idx = fetch_indexed_offset (uvalue, loclists,
2787                                                         debug_info_p->loclists_base,
2788                                                         debug_info_p->offset_size);
2789                     }
2790                 else
2791                     {
2792                       /* We want to compute:
2793                            idx = fetch_indexed_value (uvalue, loclists,
2794                                                  debug_info_p->loclists_base);
2795                            idx += debug_info_p->loclists_base;
2796                           Fortunately we already have that sum cached in the
2797                           loc_offsets array.  */
2798                       if (uvalue < debug_info_p->num_loc_offsets)
2799                         idx = debug_info_p->loc_offsets [uvalue];
2800                       else
2801                         {
2802                           warn (_("loc_offset %" PRIu64 " too big\n"), uvalue);
2803                           idx = -1;
2804                         }
2805                     }
2806               }
2807             else if (form == DW_FORM_rnglistx)
2808               {
2809                 if (debug_info_p == NULL)
2810                     idx = -1;
2811                 else
2812                     idx = fetch_indexed_offset (uvalue,
2813                                                       dwo ? rnglists_dwo : rnglists,
2814                                                       debug_info_p->rnglists_base,
2815                                                       debug_info_p->offset_size);
2816               }
2817             else
2818               {
2819                 if (debug_info_p == NULL)
2820                     base = 0;
2821                 else if (debug_info_p->addr_base == DEBUG_INFO_UNAVAILABLE)
2822                     base = 0;
2823                 else
2824                     base = debug_info_p->addr_base;
2825 
2826                 base += uvalue * pointer_size;
2827                 idx = fetch_indexed_addr (base, pointer_size);
2828               }
2829 
2830             /* We have already displayed the form name.  */
2831             if (idx != (uint64_t) -1)
2832               printf (_("%c(index: %#" PRIx64 "): %#" PRIx64),
2833                         delimiter, uvalue, idx);
2834           }
2835       break;
2836 
2837     case DW_FORM_strp_sup:
2838       if (!do_loc)
2839           printf ("%c<%#" PRIx64 ">", delimiter, uvalue + cu_offset);
2840       break;
2841 
2842     default:
2843       warn (_("Unrecognized form: %#lx"), form);
2844       /* What to do?  Consume a byte maybe?  */
2845       ++data;
2846       break;
2847     }
2848 
2849   if ((do_loc || do_debug_loc || do_debug_ranges || do_debug_info)
2850       && num_debug_info_entries == 0
2851       && debug_info_p != NULL)
2852     {
2853       switch (attribute)
2854           {
2855           case DW_AT_loclists_base:
2856             if (debug_info_p->loclists_base)
2857               warn (_("CU @ %#" PRIx64 " has multiple loclists_base values "
2858                         "(%#" PRIx64 " and %#" PRIx64 ")"),
2859                       debug_info_p->cu_offset,
2860                       debug_info_p->loclists_base, uvalue);
2861             svalue = uvalue;
2862             if (svalue < 0)
2863               {
2864                 warn (_("CU @ %#" PRIx64 " has has a negative loclists_base "
2865                           "value of %#" PRIx64 " - treating as zero"),
2866                         debug_info_p->cu_offset, svalue);
2867                 uvalue = 0;
2868               }
2869             debug_info_p->loclists_base = uvalue;
2870             break;
2871 
2872           case DW_AT_rnglists_base:
2873             /* Assignment to debug_info_p->rnglists_base is now elsewhere.  */
2874             break;
2875 
2876           case DW_AT_str_offsets_base:
2877             if (debug_info_p->str_offsets_base)
2878               warn (_("CU @ %#" PRIx64 " has multiple str_offsets_base values "
2879                         "%#" PRIx64 " and %#" PRIx64 ")"),
2880                       debug_info_p->cu_offset,
2881                       debug_info_p->str_offsets_base, uvalue);
2882             svalue = uvalue;
2883             if (svalue < 0)
2884               {
2885                 warn (_("CU @ %#" PRIx64 " has has a negative stroffsets_base "
2886                           "value of %#" PRIx64 " - treating as zero"),
2887                         debug_info_p->cu_offset, svalue);
2888                 uvalue = 0;
2889               }
2890             debug_info_p->str_offsets_base = uvalue;
2891             break;
2892 
2893           case DW_AT_frame_base:
2894             /* This is crude; the have_frame_base is reset on the next
2895                subprogram, not at the end of the current topmost one.  */
2896             have_frame_base = 1;
2897             frame_base_level = level;
2898             /* Fall through.  */
2899           case DW_AT_location:
2900           case DW_AT_GNU_locviews:
2901           case DW_AT_string_length:
2902           case DW_AT_return_addr:
2903           case DW_AT_data_member_location:
2904           case DW_AT_vtable_elem_location:
2905           case DW_AT_segment:
2906           case DW_AT_static_link:
2907           case DW_AT_use_location:
2908           case DW_AT_call_value:
2909           case DW_AT_GNU_call_site_value:
2910           case DW_AT_call_data_value:
2911           case DW_AT_GNU_call_site_data_value:
2912           case DW_AT_call_target:
2913           case DW_AT_GNU_call_site_target:
2914           case DW_AT_call_target_clobbered:
2915           case DW_AT_GNU_call_site_target_clobbered:
2916             if ((dwarf_version < 4
2917                  && (form == DW_FORM_data4 || form == DW_FORM_data8))
2918                 || form == DW_FORM_sec_offset
2919                 || form == DW_FORM_loclistx)
2920               {
2921                 /* Process location list.  */
2922                 unsigned int lmax = debug_info_p->max_loc_offsets;
2923                 unsigned int num = debug_info_p->num_loc_offsets;
2924 
2925                 if (lmax == 0 || num >= lmax)
2926                     {
2927                       lmax += 1024;
2928                       debug_info_p->loc_offsets = (uint64_t *)
2929                         xcrealloc (debug_info_p->loc_offsets,
2930                                      lmax, sizeof (*debug_info_p->loc_offsets));
2931                       debug_info_p->loc_views = (uint64_t *)
2932                         xcrealloc (debug_info_p->loc_views,
2933                                      lmax, sizeof (*debug_info_p->loc_views));
2934                       debug_info_p->have_frame_base = (int *)
2935                         xcrealloc (debug_info_p->have_frame_base,
2936                                      lmax, sizeof (*debug_info_p->have_frame_base));
2937                       debug_info_p->max_loc_offsets = lmax;
2938                     }
2939                 if (form == DW_FORM_loclistx)
2940                     uvalue = fetch_indexed_offset (num, loclists,
2941                                                          debug_info_p->loclists_base,
2942                                                          debug_info_p->offset_size);
2943                 else if (this_set != NULL)
2944                     uvalue += this_set->section_offsets [DW_SECT_LOC];
2945 
2946                 debug_info_p->have_frame_base [num] = have_frame_base;
2947                 if (attribute != DW_AT_GNU_locviews)
2948                     {
2949                       /* Corrupt DWARF info can produce more offsets than views.
2950                          See PR 23062 for an example.  */
2951                       if (debug_info_p->num_loc_offsets
2952                           > debug_info_p->num_loc_views)
2953                         warn (_("More location offset attributes than DW_AT_GNU_locview attributes\n"));
2954                       else
2955                         {
2956                           debug_info_p->loc_offsets [num] = uvalue;
2957                           debug_info_p->num_loc_offsets++;
2958                         }
2959                     }
2960                 else
2961                     {
2962                       if (debug_info_p->num_loc_views > num)
2963                         {
2964                           warn (_("The number of views (%u) is greater than the number of locations (%u)\n"),
2965                                   debug_info_p->num_loc_views, num);
2966                           debug_info_p->num_loc_views = num;
2967                         }
2968                       else
2969                         num = debug_info_p->num_loc_views;
2970                       if (num > debug_info_p->num_loc_offsets)
2971                         warn (_("More DW_AT_GNU_locview attributes than location offset attributes\n"));
2972                       else
2973                         {
2974                           debug_info_p->loc_views [num] = uvalue;
2975                           debug_info_p->num_loc_views++;
2976                         }
2977                     }
2978               }
2979             break;
2980 
2981           case DW_AT_low_pc:
2982             if (need_base_address)
2983               {
2984                 if (form == DW_FORM_addrx)
2985                     uvalue = fetch_indexed_addr (debug_info_p->addr_base
2986                                                        + uvalue * pointer_size,
2987                                                        pointer_size);
2988 
2989                 debug_info_p->base_address = uvalue;
2990               }
2991             break;
2992 
2993           case DW_AT_GNU_addr_base:
2994           case DW_AT_addr_base:
2995             debug_info_p->addr_base = uvalue;
2996             /* Retrieved elsewhere so that it is in
2997                place by the time we read low_pc.  */
2998             break;
2999 
3000           case DW_AT_GNU_ranges_base:
3001             debug_info_p->ranges_base = uvalue;
3002             break;
3003 
3004           case DW_AT_ranges:
3005             if ((dwarf_version < 4
3006                  && (form == DW_FORM_data4 || form == DW_FORM_data8))
3007                 || form == DW_FORM_sec_offset
3008                 || form == DW_FORM_rnglistx)
3009               {
3010                 /* Process range list.  */
3011                 unsigned int lmax = debug_info_p->max_range_lists;
3012                 unsigned int num = debug_info_p->num_range_lists;
3013 
3014                 if (lmax == 0 || num >= lmax)
3015                     {
3016                       lmax += 1024;
3017                       debug_info_p->range_lists = (uint64_t *)
3018                         xcrealloc (debug_info_p->range_lists,
3019                                      lmax, sizeof (*debug_info_p->range_lists));
3020                       debug_info_p->max_range_lists = lmax;
3021                     }
3022 
3023                 if (form == DW_FORM_rnglistx)
3024                     uvalue = fetch_indexed_offset (uvalue, rnglists,
3025                                                          debug_info_p->rnglists_base,
3026                                                          debug_info_p->offset_size);
3027 
3028                 debug_info_p->range_lists [num] = uvalue;
3029                 debug_info_p->num_range_lists++;
3030               }
3031             break;
3032 
3033           case DW_AT_GNU_dwo_name:
3034           case DW_AT_dwo_name:
3035             if (need_dwo_info)
3036               switch (form)
3037                 {
3038                 case DW_FORM_strp:
3039                     add_dwo_name ((const char *) fetch_indirect_string (uvalue),
3040                                     cu_offset);
3041                     break;
3042                 case DW_FORM_GNU_strp_alt:
3043                     add_dwo_name ((const char *) fetch_alt_indirect_string (uvalue), cu_offset);
3044                     break;
3045                 case DW_FORM_GNU_str_index:
3046                 case DW_FORM_strx:
3047                 case DW_FORM_strx1:
3048                 case DW_FORM_strx2:
3049                 case DW_FORM_strx3:
3050                 case DW_FORM_strx4:
3051                     add_dwo_name (fetch_indexed_string (uvalue, this_set,
3052                                                                 offset_size, false,
3053                                                                 debug_info_p->str_offsets_base),
3054                                     cu_offset);
3055                     break;
3056                 case DW_FORM_string:
3057                     add_dwo_name ((const char *) orig_data, cu_offset);
3058                     break;
3059                 default:
3060                     warn (_("Unsupported form (%s) for attribute %s\n"),
3061                           get_FORM_name (form), get_AT_name (attribute));
3062                     break;
3063                 }
3064             break;
3065 
3066           case DW_AT_comp_dir:
3067             /* FIXME: Also extract a build-id in a CU/TU.  */
3068             if (need_dwo_info)
3069               switch (form)
3070                 {
3071                 case DW_FORM_strp:
3072                     add_dwo_dir ((const char *) fetch_indirect_string (uvalue), cu_offset);
3073                     break;
3074                 case DW_FORM_GNU_strp_alt:
3075                     add_dwo_dir (fetch_alt_indirect_string (uvalue), cu_offset);
3076                     break;
3077                 case DW_FORM_line_strp:
3078                     add_dwo_dir ((const char *) fetch_indirect_line_string (uvalue), cu_offset);
3079                     break;
3080                 case DW_FORM_GNU_str_index:
3081                 case DW_FORM_strx:
3082                 case DW_FORM_strx1:
3083                 case DW_FORM_strx2:
3084                 case DW_FORM_strx3:
3085                 case DW_FORM_strx4:
3086                     add_dwo_dir (fetch_indexed_string (uvalue, this_set, offset_size, false,
3087                                                                debug_info_p->str_offsets_base),
3088                                    cu_offset);
3089                     break;
3090                 case DW_FORM_string:
3091                     add_dwo_dir ((const char *) orig_data, cu_offset);
3092                     break;
3093                 default:
3094                     warn (_("Unsupported form (%s) for attribute %s\n"),
3095                           get_FORM_name (form), get_AT_name (attribute));
3096                     break;
3097                 }
3098             break;
3099 
3100           case DW_AT_GNU_dwo_id:
3101             if (need_dwo_info)
3102               switch (form)
3103                 {
3104                 case DW_FORM_data8:
3105                     /* FIXME: Record the length of the ID as well ?  */
3106                     add_dwo_id ((const char *) (data - 8), cu_offset);
3107                     break;
3108                 default:
3109                     warn (_("Unsupported form (%s) for attribute %s\n"),
3110                           get_FORM_name (form), get_AT_name (attribute));
3111                     break;
3112                 }
3113             break;
3114 
3115           default:
3116             break;
3117           }
3118     }
3119 
3120   if (do_loc || attribute == 0)
3121     return data;
3122 
3123   /* For some attributes we can display further information.  */
3124   switch (attribute)
3125     {
3126     case DW_AT_type:
3127       if (level >= 0 && level < MAX_CU_NESTING
3128             && uvalue < (size_t) (end - start))
3129           {
3130             bool is_signed = false;
3131             abbrev_entry *type_abbrev;
3132             unsigned char *type_data;
3133             abbrev_map *map;
3134 
3135             type_abbrev = get_type_abbrev_from_form (form, uvalue,
3136                                                                cu_offset, end,
3137                                                                section, NULL,
3138                                                                &type_data, &map);
3139             if (type_abbrev != NULL)
3140               {
3141                 get_type_signedness (type_abbrev, section, type_data,
3142                                            map ? section->start + map->end : end,
3143                                            map ? map->start : cu_offset,
3144                                            pointer_size, offset_size, dwarf_version,
3145                                            & is_signed, 0);
3146               }
3147             level_type_signed[level] = is_signed;
3148           }
3149       break;
3150 
3151     case DW_AT_inline:
3152       printf ("\t");
3153       switch (uvalue)
3154           {
3155           case DW_INL_not_inlined:
3156             printf (_("(not inlined)"));
3157             break;
3158           case DW_INL_inlined:
3159             printf (_("(inlined)"));
3160             break;
3161           case DW_INL_declared_not_inlined:
3162             printf (_("(declared as inline but ignored)"));
3163             break;
3164           case DW_INL_declared_inlined:
3165             printf (_("(declared as inline and inlined)"));
3166             break;
3167           default:
3168             printf (_("  (Unknown inline attribute value: %#" PRIx64 ")"),
3169                       uvalue);
3170             break;
3171           }
3172       break;
3173 
3174     case DW_AT_language:
3175       printf ("\t(");
3176       display_lang (uvalue);
3177       printf (")");
3178       break;
3179 
3180     case DW_AT_encoding:
3181       printf ("\t");
3182       switch (uvalue)
3183           {
3184           case DW_ATE_void:             printf ("(void)"); break;
3185           case DW_ATE_address:                    printf ("(machine address)"); break;
3186           case DW_ATE_boolean:                    printf ("(boolean)"); break;
3187           case DW_ATE_complex_float:    printf ("(complex float)"); break;
3188           case DW_ATE_float:            printf ("(float)"); break;
3189           case DW_ATE_signed:           printf ("(signed)"); break;
3190           case DW_ATE_signed_char:      printf ("(signed char)"); break;
3191           case DW_ATE_unsigned:                   printf ("(unsigned)"); break;
3192           case DW_ATE_unsigned_char:    printf ("(unsigned char)"); break;
3193             /* DWARF 2.1 values:  */
3194           case DW_ATE_imaginary_float:  printf ("(imaginary float)"); break;
3195           case DW_ATE_decimal_float:    printf ("(decimal float)"); break;
3196             /* DWARF 3 values:  */
3197           case DW_ATE_packed_decimal:   printf ("(packed_decimal)"); break;
3198           case DW_ATE_numeric_string:   printf ("(numeric_string)"); break;
3199           case DW_ATE_edited:           printf ("(edited)"); break;
3200           case DW_ATE_signed_fixed:     printf ("(signed_fixed)"); break;
3201           case DW_ATE_unsigned_fixed:   printf ("(unsigned_fixed)"); break;
3202             /* DWARF 4 values:  */
3203           case DW_ATE_UTF:              printf ("(unicode string)"); break;
3204             /* DWARF 5 values:  */
3205           case DW_ATE_UCS:              printf ("(UCS)"); break;
3206           case DW_ATE_ASCII:            printf ("(ASCII)"); break;
3207 
3208             /* HP extensions:  */
3209           case DW_ATE_HP_float80:                 printf ("(HP_float80)"); break;
3210           case DW_ATE_HP_complex_float80:         printf ("(HP_complex_float80)"); break;
3211           case DW_ATE_HP_float128:      printf ("(HP_float128)"); break;
3212           case DW_ATE_HP_complex_float128:printf ("(HP_complex_float128)"); break;
3213           case DW_ATE_HP_floathpintel:  printf ("(HP_floathpintel)"); break;
3214           case DW_ATE_HP_imaginary_float80:       printf ("(HP_imaginary_float80)"); break;
3215           case DW_ATE_HP_imaginary_float128:      printf ("(HP_imaginary_float128)"); break;
3216 
3217           default:
3218             if (uvalue >= DW_ATE_lo_user
3219                 && uvalue <= DW_ATE_hi_user)
3220               printf (_("(user defined type)"));
3221             else
3222               printf (_("(unknown type)"));
3223             break;
3224           }
3225       break;
3226 
3227     case DW_AT_accessibility:
3228       printf ("\t");
3229       switch (uvalue)
3230           {
3231           case DW_ACCESS_public:                  printf ("(public)"); break;
3232           case DW_ACCESS_protected:     printf ("(protected)"); break;
3233           case DW_ACCESS_private:                 printf ("(private)"); break;
3234           default:
3235             printf (_("(unknown accessibility)"));
3236             break;
3237           }
3238       break;
3239 
3240     case DW_AT_visibility:
3241       printf ("\t");
3242       switch (uvalue)
3243           {
3244           case DW_VIS_local:            printf ("(local)"); break;
3245           case DW_VIS_exported:                   printf ("(exported)"); break;
3246           case DW_VIS_qualified:                  printf ("(qualified)"); break;
3247           default:                      printf (_("(unknown visibility)")); break;
3248           }
3249       break;
3250 
3251     case DW_AT_endianity:
3252       printf ("\t");
3253       switch (uvalue)
3254           {
3255           case DW_END_default:                    printf ("(default)"); break;
3256           case DW_END_big:              printf ("(big)"); break;
3257           case DW_END_little:           printf ("(little)"); break;
3258           default:
3259             if (uvalue >= DW_END_lo_user && uvalue <= DW_END_hi_user)
3260               printf (_("(user specified)"));
3261             else
3262               printf (_("(unknown endianity)"));
3263             break;
3264           }
3265       break;
3266 
3267     case DW_AT_virtuality:
3268       printf ("\t");
3269       switch (uvalue)
3270           {
3271           case DW_VIRTUALITY_none:      printf ("(none)"); break;
3272           case DW_VIRTUALITY_virtual:   printf ("(virtual)"); break;
3273           case DW_VIRTUALITY_pure_virtual:printf ("(pure_virtual)"); break;
3274           default:                      printf (_("(unknown virtuality)")); break;
3275           }
3276       break;
3277 
3278     case DW_AT_identifier_case:
3279       printf ("\t");
3280       switch (uvalue)
3281           {
3282           case DW_ID_case_sensitive:    printf ("(case_sensitive)"); break;
3283           case DW_ID_up_case:           printf ("(up_case)"); break;
3284           case DW_ID_down_case:                   printf ("(down_case)"); break;
3285           case DW_ID_case_insensitive:  printf ("(case_insensitive)"); break;
3286           default:                      printf (_("(unknown case)")); break;
3287           }
3288       break;
3289 
3290     case DW_AT_calling_convention:
3291       printf ("\t");
3292       switch (uvalue)
3293           {
3294           case DW_CC_normal:  printf ("(normal)"); break;
3295           case DW_CC_program: printf ("(program)"); break;
3296           case DW_CC_nocall:  printf ("(nocall)"); break;
3297           case DW_CC_pass_by_reference: printf ("(pass by ref)"); break;
3298           case DW_CC_pass_by_value: printf ("(pass by value)"); break;
3299           case DW_CC_GNU_renesas_sh: printf ("(Rensas SH)"); break;
3300           case DW_CC_GNU_borland_fastcall_i386: printf ("(Borland fastcall i386)"); break;
3301           default:
3302             if (uvalue >= DW_CC_lo_user
3303                 && uvalue <= DW_CC_hi_user)
3304               printf (_("(user defined)"));
3305             else
3306               printf (_("(unknown convention)"));
3307           }
3308       break;
3309 
3310     case DW_AT_ordering:
3311       printf ("\t");
3312       switch (uvalue)
3313           {
3314           case 255:
3315           case -1: printf (_("(undefined)")); break;
3316           case 0:  printf ("(row major)"); break;
3317           case 1:  printf ("(column major)"); break;
3318           }
3319       break;
3320 
3321     case DW_AT_decimal_sign:
3322       printf ("\t");
3323       switch (uvalue)
3324           {
3325           case DW_DS_unsigned:            printf (_("(unsigned)")); break;
3326           case DW_DS_leading_overpunch:   printf (_("(leading overpunch)")); break;
3327           case DW_DS_trailing_overpunch:  printf (_("(trailing overpunch)")); break;
3328           case DW_DS_leading_separate:    printf (_("(leading separate)")); break;
3329           case DW_DS_trailing_separate:   printf (_("(trailing separate)")); break;
3330           default:                        printf (_("(unrecognised)")); break;
3331           }
3332       break;
3333 
3334     case DW_AT_defaulted:
3335       printf ("\t");
3336       switch (uvalue)
3337           {
3338           case DW_DEFAULTED_no:           printf (_("(no)")); break;
3339           case DW_DEFAULTED_in_class:     printf (_("(in class)")); break;
3340           case DW_DEFAULTED_out_of_class: printf (_("(out of class)")); break;
3341           default:                        printf (_("(unrecognised)")); break;
3342           }
3343       break;
3344 
3345     case DW_AT_discr_list:
3346       printf ("\t");
3347       display_discr_list (form, uvalue, data, level);
3348       break;
3349 
3350     case DW_AT_frame_base:
3351       have_frame_base = 1;
3352       /* Fall through.  */
3353     case DW_AT_location:
3354     case DW_AT_loclists_base:
3355     case DW_AT_rnglists_base:
3356     case DW_AT_str_offsets_base:
3357     case DW_AT_string_length:
3358     case DW_AT_return_addr:
3359     case DW_AT_data_member_location:
3360     case DW_AT_vtable_elem_location:
3361     case DW_AT_segment:
3362     case DW_AT_static_link:
3363     case DW_AT_use_location:
3364     case DW_AT_call_value:
3365     case DW_AT_GNU_call_site_value:
3366     case DW_AT_call_data_value:
3367     case DW_AT_GNU_call_site_data_value:
3368     case DW_AT_call_target:
3369     case DW_AT_GNU_call_site_target:
3370     case DW_AT_call_target_clobbered:
3371     case DW_AT_GNU_call_site_target_clobbered:
3372       if ((dwarf_version < 4
3373              && (form == DW_FORM_data4 || form == DW_FORM_data8))
3374             || form == DW_FORM_sec_offset
3375             || form == DW_FORM_loclistx)
3376           {
3377             if (attribute != DW_AT_rnglists_base
3378                 && attribute != DW_AT_str_offsets_base)
3379               printf (_(" (location list)"));
3380           }
3381       /* Fall through.  */
3382     case DW_AT_allocated:
3383     case DW_AT_associated:
3384     case DW_AT_data_location:
3385     case DW_AT_stride:
3386     case DW_AT_upper_bound:
3387     case DW_AT_lower_bound:
3388     case DW_AT_rank:
3389       if (block_start)
3390           {
3391             int need_frame_base;
3392 
3393             printf ("\t(");
3394             need_frame_base = decode_location_expression (block_start,
3395                                                                       pointer_size,
3396                                                                       offset_size,
3397                                                                       dwarf_version,
3398                                                                       uvalue,
3399                                                                       cu_offset, section);
3400             printf (")");
3401             if (need_frame_base && !have_frame_base)
3402               printf (_(" [without DW_AT_frame_base]"));
3403           }
3404       break;
3405 
3406     case DW_AT_data_bit_offset:
3407     case DW_AT_byte_size:
3408     case DW_AT_bit_size:
3409     case DW_AT_string_length_byte_size:
3410     case DW_AT_string_length_bit_size:
3411     case DW_AT_bit_stride:
3412       if (form == DW_FORM_exprloc)
3413           {
3414             printf ("\t(");
3415             (void) decode_location_expression (block_start, pointer_size,
3416                                                        offset_size, dwarf_version,
3417                                                        uvalue, cu_offset, section);
3418             printf (")");
3419           }
3420       break;
3421 
3422     case DW_AT_import:
3423       {
3424           unsigned long abbrev_number;
3425           abbrev_entry *entry;
3426 
3427           entry = get_type_abbrev_from_form (form, uvalue, cu_offset, end,
3428                                                      section, & abbrev_number, NULL, NULL);
3429           if (entry == NULL)
3430             {
3431               if (form != DW_FORM_GNU_ref_alt)
3432                 warn (_("Offset %#" PRIx64 " used as value for DW_AT_import attribute of DIE at offset %#tx is too big.\n"),
3433                         uvalue,
3434                         orig_data - section->start);
3435             }
3436           else
3437             {
3438               printf (_("\t[Abbrev Number: %ld"), abbrev_number);
3439               printf (" (%s)", get_TAG_name (entry->tag));
3440               printf ("]");
3441             }
3442       }
3443       break;
3444 
3445     default:
3446       break;
3447     }
3448 
3449   return data;
3450 }
3451 
3452 static unsigned char *
read_and_display_attr(unsigned long attribute,unsigned long form,int64_t implicit_const,unsigned char * start,unsigned char * data,unsigned char * end,uint64_t cu_offset,uint64_t pointer_size,uint64_t offset_size,int dwarf_version,debug_info * debug_info_p,int do_loc,struct dwarf_section * section,struct cu_tu_set * this_set,int level)3453 read_and_display_attr (unsigned long attribute,
3454                            unsigned long form,
3455                            int64_t implicit_const,
3456                            unsigned char *start,
3457                            unsigned char *data,
3458                            unsigned char *end,
3459                            uint64_t cu_offset,
3460                            uint64_t pointer_size,
3461                            uint64_t offset_size,
3462                            int dwarf_version,
3463                            debug_info *debug_info_p,
3464                            int do_loc,
3465                            struct dwarf_section *section,
3466                            struct cu_tu_set *this_set,
3467                            int level)
3468 {
3469   if (!do_loc)
3470     printf ("   %-18s:", get_AT_name (attribute));
3471   data = read_and_display_attr_value (attribute, form, implicit_const,
3472                                               start, data, end,
3473                                               cu_offset, pointer_size, offset_size,
3474                                               dwarf_version, debug_info_p,
3475                                               do_loc, section, this_set, ' ', level);
3476   if (!do_loc)
3477     printf ("\n");
3478   return data;
3479 }
3480 
3481 /* Like load_debug_section, but if the ordinary call fails, and we are
3482    following debug links, then attempt to load the requested section
3483    from one of the separate debug info files.  */
3484 
3485 static bool
load_debug_section_with_follow(enum dwarf_section_display_enum sec_enum,void * handle)3486 load_debug_section_with_follow (enum dwarf_section_display_enum sec_enum,
3487                                         void * handle)
3488 {
3489   if (load_debug_section (sec_enum, handle))
3490     {
3491       if (debug_displays[sec_enum].section.filename == NULL)
3492           {
3493             /* See if we can associate a filename with this section.  */
3494             separate_info * i;
3495 
3496             for (i = first_separate_info; i != NULL; i = i->next)
3497               if (i->handle == handle)
3498                 {
3499                     debug_displays[sec_enum].section.filename = i->filename;
3500                     break;
3501                 }
3502           }
3503 
3504       return true;
3505     }
3506 
3507   if (do_follow_links)
3508     {
3509       separate_info * i;
3510 
3511       for (i = first_separate_info; i != NULL; i = i->next)
3512           {
3513             if (load_debug_section (sec_enum, i->handle))
3514               {
3515                 debug_displays[sec_enum].section.filename = i->filename;
3516 
3517                 /* FIXME: We should check to see if any of the remaining debug info
3518                      files also contain this section, and, umm, do something about it.  */
3519                 return true;
3520               }
3521           }
3522     }
3523 
3524   return false;
3525 }
3526 
3527 static void
introduce(struct dwarf_section * section,bool raw)3528 introduce (struct dwarf_section * section, bool raw)
3529 {
3530   if (raw)
3531     {
3532       if (do_follow_links && section->filename)
3533           printf (_("Raw dump of debug contents of section %s (loaded from %s):\n\n"),
3534                     section->name, section->filename);
3535       else
3536           printf (_("Raw dump of debug contents of section %s:\n\n"), section->name);
3537     }
3538   else
3539     {
3540       if (do_follow_links && section->filename)
3541           printf (_("Contents of the %s section (loaded from %s):\n\n"),
3542                     section->name, section->filename);
3543       else
3544           printf (_("Contents of the %s section:\n\n"), section->name);
3545     }
3546 }
3547 
3548 /* Free memory allocated for one unit in debug_information.  */
3549 
3550 static void
free_debug_information(debug_info * ent)3551 free_debug_information (debug_info *ent)
3552 {
3553   if (ent->max_loc_offsets)
3554     {
3555       free (ent->loc_offsets);
3556       free (ent->loc_views);
3557       free (ent->have_frame_base);
3558     }
3559   if (ent->max_range_lists)
3560     {
3561       free (ent->range_lists);
3562     }
3563 }
3564 
3565 /* For look-ahead in attributes.  When you want to scan a DIE for one specific
3566    attribute and ignore the rest.  */
3567 
3568 static unsigned char *
skip_attribute(unsigned long form,unsigned char * data,unsigned char * end,uint64_t pointer_size,uint64_t offset_size,int dwarf_version)3569 skip_attribute (unsigned long    form,
3570                     unsigned char *  data,
3571                     unsigned char *  end,
3572                     uint64_t         pointer_size,
3573                     uint64_t         offset_size,
3574                     int              dwarf_version)
3575 {
3576   uint64_t temp;
3577   int64_t stemp;
3578 
3579   switch (form)
3580     {
3581     case DW_FORM_ref_addr:
3582       data += dwarf_version == 2 ? pointer_size : offset_size;
3583       break;
3584     case DW_FORM_addr:
3585       data += pointer_size;
3586       break;
3587     case DW_FORM_strp_sup:
3588     case DW_FORM_strp:
3589     case DW_FORM_line_strp:
3590     case DW_FORM_sec_offset:
3591     case DW_FORM_GNU_ref_alt:
3592     case DW_FORM_GNU_strp_alt:
3593       data += offset_size;
3594       break;
3595     case DW_FORM_ref1:
3596     case DW_FORM_flag:
3597     case DW_FORM_data1:
3598     case DW_FORM_strx1:
3599     case DW_FORM_addrx1:
3600       data += 1;
3601       break;
3602     case DW_FORM_ref2:
3603     case DW_FORM_data2:
3604     case DW_FORM_strx2:
3605     case DW_FORM_addrx2:
3606       data += 2;
3607       break;
3608     case DW_FORM_strx3:
3609     case DW_FORM_addrx3:
3610       data += 3;
3611       break;
3612     case DW_FORM_ref_sup4:
3613     case DW_FORM_ref4:
3614     case DW_FORM_data4:
3615     case DW_FORM_strx4:
3616     case DW_FORM_addrx4:
3617       data += 4;
3618       break;
3619     case DW_FORM_ref_sup8:
3620     case DW_FORM_ref8:
3621     case DW_FORM_data8:
3622     case DW_FORM_ref_sig8:
3623       data += 8;
3624       break;
3625     case DW_FORM_data16:
3626       data += 16;
3627       break;
3628     case DW_FORM_sdata:
3629       READ_SLEB (stemp, data, end);
3630       break;
3631     case DW_FORM_GNU_str_index:
3632     case DW_FORM_strx:
3633     case DW_FORM_ref_udata:
3634     case DW_FORM_udata:
3635     case DW_FORM_GNU_addr_index:
3636     case DW_FORM_addrx:
3637     case DW_FORM_loclistx:
3638     case DW_FORM_rnglistx:
3639       READ_ULEB (temp, data, end);
3640       break;
3641 
3642     case DW_FORM_indirect:
3643       while (form == DW_FORM_indirect)
3644         READ_ULEB (form, data, end);
3645       return skip_attribute (form, data, end, pointer_size, offset_size, dwarf_version);
3646 
3647     case DW_FORM_string:
3648       data += strnlen ((char *) data, end - data);
3649       break;
3650     case DW_FORM_block:
3651     case DW_FORM_exprloc:
3652       READ_ULEB (temp, data, end);
3653       data += temp;
3654       break;
3655     case DW_FORM_block1:
3656       SAFE_BYTE_GET_AND_INC (temp, data, 1, end);
3657       data += temp;
3658       break;
3659     case DW_FORM_block2:
3660       SAFE_BYTE_GET_AND_INC (temp, data, 2, end);
3661       data += temp;
3662       break;
3663     case DW_FORM_block4:
3664       SAFE_BYTE_GET_AND_INC (temp, data, 4, end);
3665       data += temp;
3666       break;
3667     case DW_FORM_implicit_const:
3668     case DW_FORM_flag_present:
3669       break;
3670     default:
3671       warn (_("Unexpected form in top DIE\n"));
3672       break;
3673     }
3674   return data;
3675 }
3676 
3677 static void
read_bases(abbrev_entry * entry,unsigned char * data,unsigned char * end,int64_t pointer_size,uint64_t offset_size,int dwarf_version,debug_info * debug_info_p)3678 read_bases (abbrev_entry *   entry,
3679               unsigned char *  data,
3680               unsigned char *  end,
3681               int64_t          pointer_size,
3682               uint64_t         offset_size,
3683               int              dwarf_version,
3684               debug_info *     debug_info_p)
3685 {
3686   abbrev_attr *attr;
3687 
3688   for (attr = entry->first_attr;
3689        attr && attr->attribute;
3690        attr = attr->next)
3691     {
3692       uint64_t uvalue;
3693 
3694       if (attr->attribute == DW_AT_rnglists_base)
3695           {
3696             if (attr->form == DW_FORM_sec_offset)
3697               {
3698                 SAFE_BYTE_GET_AND_INC (uvalue, data, offset_size, end);
3699                 debug_info_p->rnglists_base = uvalue;
3700               }
3701             else
3702               warn (_("Unexpected form of DW_AT_rnglists_base in the top DIE\n"));
3703           }
3704       else if (attr->attribute == DW_AT_addr_base || attr->attribute == DW_AT_GNU_addr_base)
3705           {
3706             if (attr->form == DW_FORM_sec_offset)
3707               {
3708                 SAFE_BYTE_GET_AND_INC (uvalue, data, offset_size, end);
3709                 debug_info_p->addr_base = uvalue;
3710               }
3711             else
3712               warn (_("Unexpected form of DW_AT_addr_base in the top DIE\n"));
3713           }
3714       else
3715           data = skip_attribute (attr->form, data, end, pointer_size,
3716                                      offset_size, dwarf_version);
3717     }
3718 }
3719 
3720 /* Process the contents of a .debug_info section.
3721    If do_loc is TRUE then we are scanning for location lists and dwo tags
3722    and we do not want to display anything to the user.
3723    If do_types is TRUE, we are processing a .debug_types section instead of
3724    a .debug_info section.
3725    The information displayed is restricted by the values in DWARF_START_DIE
3726    and DWARF_CUTOFF_LEVEL.
3727    Returns TRUE upon success.  Otherwise an error or warning message is
3728    printed and FALSE is returned.  */
3729 
3730 static bool
process_debug_info(struct dwarf_section * section,void * file,enum dwarf_section_display_enum abbrev_sec,bool do_loc,bool do_types)3731 process_debug_info (struct dwarf_section * section,
3732                         void *file,
3733                         enum dwarf_section_display_enum abbrev_sec,
3734                         bool do_loc,
3735                         bool do_types)
3736 {
3737   unsigned char *start = section->start;
3738   unsigned char *end = start + section->size;
3739   unsigned char *section_begin;
3740   unsigned int unit;
3741   unsigned int num_units = 0;
3742 
3743   /* First scan the section to get the number of comp units.
3744      Length sanity checks are done here.  */
3745   for (section_begin = start, num_units = 0; section_begin < end;
3746        num_units ++)
3747     {
3748       uint64_t length;
3749 
3750       /* Read the first 4 bytes.  For a 32-bit DWARF section, this
3751            will be the length.  For a 64-bit DWARF section, it'll be
3752            the escape code 0xffffffff followed by an 8 byte length.  */
3753       SAFE_BYTE_GET_AND_INC (length, section_begin, 4, end);
3754 
3755       if (length == 0xffffffff)
3756           SAFE_BYTE_GET_AND_INC (length, section_begin, 8, end);
3757       else if (length >= 0xfffffff0 && length < 0xffffffff)
3758           {
3759             warn (_("Reserved length value (%#" PRIx64 ") found in section %s\n"),
3760                     length, section->name);
3761             return false;
3762           }
3763 
3764       /* Negative values are illegal, they may even cause infinite
3765            looping.  This can happen if we can't accurately apply
3766            relocations to an object file, or if the file is corrupt.  */
3767       if (length > (size_t) (end - section_begin))
3768           {
3769             warn (_("Corrupt unit length (got %#" PRIx64
3770                       " expected at most %#tx) in section %s\n"),
3771                     length, end - section_begin, section->name);
3772             return false;
3773           }
3774       section_begin += length;
3775     }
3776 
3777   if (num_units == 0)
3778     {
3779       error (_("No comp units in %s section ?\n"), section->name);
3780       return false;
3781     }
3782 
3783   if ((do_loc || do_debug_loc || do_debug_ranges || do_debug_info)
3784       && num_debug_info_entries == 0
3785       && ! do_types)
3786     {
3787 
3788       /* Then allocate an array to hold the information.  */
3789       debug_information = (debug_info *) cmalloc (num_units,
3790                                                               sizeof (* debug_information));
3791       if (debug_information == NULL)
3792           {
3793             error (_("Not enough memory for a debug info array of %u entries\n"),
3794                      num_units);
3795             alloc_num_debug_info_entries = num_debug_info_entries = 0;
3796             return false;
3797           }
3798 
3799       /* PR 17531: file: 92ca3797.
3800            We cannot rely upon the debug_information array being initialised
3801            before it is used.  A corrupt file could easily contain references
3802            to a unit for which information has not been made available.  So
3803            we ensure that the array is zeroed here.  */
3804       memset (debug_information, 0, num_units * sizeof (*debug_information));
3805 
3806       alloc_num_debug_info_entries = num_units;
3807     }
3808 
3809   if (!do_loc)
3810     {
3811       load_debug_section_with_follow (str, file);
3812       load_debug_section_with_follow (line_str, file);
3813       load_debug_section_with_follow (str_dwo, file);
3814       load_debug_section_with_follow (str_index, file);
3815       load_debug_section_with_follow (str_index_dwo, file);
3816       load_debug_section_with_follow (debug_addr, file);
3817     }
3818 
3819   load_debug_section_with_follow (abbrev_sec, file);
3820   load_debug_section_with_follow (loclists, file);
3821   load_debug_section_with_follow (rnglists, file);
3822   load_debug_section_with_follow (loclists_dwo, file);
3823   load_debug_section_with_follow (rnglists_dwo, file);
3824 
3825   if (debug_displays [abbrev_sec].section.start == NULL)
3826     {
3827       warn (_("Unable to locate %s section!\n"),
3828               debug_displays [abbrev_sec].section.uncompressed_name);
3829       return false;
3830     }
3831 
3832   if (!do_loc && dwarf_start_die == 0)
3833     introduce (section, false);
3834 
3835   free_all_abbrevs ();
3836 
3837   /* In order to be able to resolve DW_FORM_ref_addr forms we need
3838      to load *all* of the abbrevs for all CUs in this .debug_info
3839      section.  This does effectively mean that we (partially) read
3840      every CU header twice.  */
3841   for (section_begin = start; start < end;)
3842     {
3843       DWARF2_Internal_CompUnit compunit;
3844       unsigned char *hdrptr;
3845       uint64_t abbrev_base;
3846       size_t abbrev_size;
3847       uint64_t cu_offset;
3848       unsigned int offset_size;
3849       struct cu_tu_set *this_set;
3850       unsigned char *end_cu;
3851 
3852       hdrptr = start;
3853       cu_offset = start - section_begin;
3854 
3855       SAFE_BYTE_GET_AND_INC (compunit.cu_length, hdrptr, 4, end);
3856 
3857       if (compunit.cu_length == 0xffffffff)
3858           {
3859             SAFE_BYTE_GET_AND_INC (compunit.cu_length, hdrptr, 8, end);
3860             offset_size = 8;
3861           }
3862       else
3863           offset_size = 4;
3864       end_cu = hdrptr + compunit.cu_length;
3865 
3866       SAFE_BYTE_GET_AND_INC (compunit.cu_version, hdrptr, 2, end_cu);
3867 
3868       this_set = find_cu_tu_set_v2 (cu_offset, do_types);
3869 
3870       if (compunit.cu_version < 5)
3871           {
3872             compunit.cu_unit_type = DW_UT_compile;
3873             /* Initialize it due to a false compiler warning.  */
3874             compunit.cu_pointer_size = -1;
3875           }
3876       else
3877           {
3878             SAFE_BYTE_GET_AND_INC (compunit.cu_unit_type, hdrptr, 1, end_cu);
3879             do_types = (compunit.cu_unit_type == DW_UT_type);
3880 
3881             SAFE_BYTE_GET_AND_INC (compunit.cu_pointer_size, hdrptr, 1, end_cu);
3882           }
3883 
3884       SAFE_BYTE_GET_AND_INC (compunit.cu_abbrev_offset, hdrptr, offset_size,
3885                                    end_cu);
3886 
3887       if (compunit.cu_unit_type == DW_UT_split_compile
3888             || compunit.cu_unit_type == DW_UT_skeleton)
3889           {
3890             uint64_t dwo_id;
3891             SAFE_BYTE_GET_AND_INC (dwo_id, hdrptr, 8, end_cu);
3892           }
3893 
3894       if (this_set == NULL)
3895           {
3896             abbrev_base = 0;
3897             abbrev_size = debug_displays [abbrev_sec].section.size;
3898           }
3899       else
3900           {
3901             abbrev_base = this_set->section_offsets [DW_SECT_ABBREV];
3902             abbrev_size = this_set->section_sizes [DW_SECT_ABBREV];
3903           }
3904 
3905       abbrev_list *list;
3906       abbrev_list *free_list;
3907       list = find_and_process_abbrev_set (&debug_displays[abbrev_sec].section,
3908                                                     abbrev_base, abbrev_size,
3909                                                     compunit.cu_abbrev_offset,
3910                                                     &free_list);
3911       start = end_cu;
3912       if (list != NULL && list->first_abbrev != NULL)
3913           record_abbrev_list_for_cu (cu_offset, start - section_begin,
3914                                            list, free_list);
3915       else if (free_list != NULL)
3916           free_abbrev_list (free_list);
3917     }
3918 
3919   for (start = section_begin, unit = 0; start < end; unit++)
3920     {
3921       DWARF2_Internal_CompUnit compunit;
3922       unsigned char *hdrptr;
3923       unsigned char *tags;
3924       int level, last_level, saved_level;
3925       uint64_t cu_offset;
3926       unsigned int offset_size;
3927       uint64_t signature = 0;
3928       uint64_t type_offset = 0;
3929       struct cu_tu_set *this_set;
3930       uint64_t abbrev_base;
3931       size_t abbrev_size;
3932       unsigned char *end_cu;
3933 
3934       hdrptr = start;
3935       cu_offset = start - section_begin;
3936 
3937       SAFE_BYTE_GET_AND_INC (compunit.cu_length, hdrptr, 4, end);
3938 
3939       if (compunit.cu_length == 0xffffffff)
3940           {
3941             SAFE_BYTE_GET_AND_INC (compunit.cu_length, hdrptr, 8, end);
3942             offset_size = 8;
3943           }
3944       else
3945           offset_size = 4;
3946       end_cu = hdrptr + compunit.cu_length;
3947 
3948       SAFE_BYTE_GET_AND_INC (compunit.cu_version, hdrptr, 2, end_cu);
3949 
3950       this_set = find_cu_tu_set_v2 (cu_offset, do_types);
3951 
3952       if (compunit.cu_version < 5)
3953           {
3954             compunit.cu_unit_type = DW_UT_compile;
3955             /* Initialize it due to a false compiler warning.  */
3956             compunit.cu_pointer_size = -1;
3957           }
3958       else
3959           {
3960             SAFE_BYTE_GET_AND_INC (compunit.cu_unit_type, hdrptr, 1, end_cu);
3961             do_types = (compunit.cu_unit_type == DW_UT_type);
3962 
3963             SAFE_BYTE_GET_AND_INC (compunit.cu_pointer_size, hdrptr, 1, end_cu);
3964           }
3965 
3966       SAFE_BYTE_GET_AND_INC (compunit.cu_abbrev_offset, hdrptr, offset_size, end_cu);
3967 
3968       if (this_set == NULL)
3969           {
3970             abbrev_base = 0;
3971             abbrev_size = debug_displays [abbrev_sec].section.size;
3972           }
3973       else
3974           {
3975             abbrev_base = this_set->section_offsets [DW_SECT_ABBREV];
3976             abbrev_size = this_set->section_sizes [DW_SECT_ABBREV];
3977           }
3978 
3979       if (compunit.cu_version < 5)
3980           SAFE_BYTE_GET_AND_INC (compunit.cu_pointer_size, hdrptr, 1, end_cu);
3981 
3982       bool do_dwo_id = false;
3983       uint64_t dwo_id = 0;
3984       if (compunit.cu_unit_type == DW_UT_split_compile
3985             || compunit.cu_unit_type == DW_UT_skeleton)
3986           {
3987             SAFE_BYTE_GET_AND_INC (dwo_id, hdrptr, 8, end_cu);
3988             do_dwo_id = true;
3989           }
3990 
3991       /* PR 17512: file: 001-108546-0.001:0.1.  */
3992       if (compunit.cu_pointer_size < 2 || compunit.cu_pointer_size > 8)
3993           {
3994             warn (_("Invalid pointer size (%d) in compunit header, using %d instead\n"),
3995                     compunit.cu_pointer_size, offset_size);
3996             compunit.cu_pointer_size = offset_size;
3997           }
3998 
3999       if (do_types)
4000           {
4001             SAFE_BYTE_GET_AND_INC (signature, hdrptr, 8, end_cu);
4002             SAFE_BYTE_GET_AND_INC (type_offset, hdrptr, offset_size, end_cu);
4003           }
4004 
4005       if (dwarf_start_die >= (size_t) (end_cu - section_begin))
4006           {
4007             start = end_cu;
4008             continue;
4009           }
4010 
4011       if ((do_loc || do_debug_loc || do_debug_ranges || do_debug_info)
4012             && num_debug_info_entries == 0
4013             && alloc_num_debug_info_entries > unit
4014             && ! do_types)
4015           {
4016             free_debug_information (&debug_information[unit]);
4017             memset (&debug_information[unit], 0, sizeof (*debug_information));
4018             debug_information[unit].pointer_size = compunit.cu_pointer_size;
4019             debug_information[unit].offset_size = offset_size;
4020             debug_information[unit].dwarf_version = compunit.cu_version;
4021             debug_information[unit].cu_offset = cu_offset;
4022             debug_information[unit].addr_base = DEBUG_INFO_UNAVAILABLE;
4023             debug_information[unit].ranges_base = DEBUG_INFO_UNAVAILABLE;
4024           }
4025 
4026       if (!do_loc && dwarf_start_die == 0)
4027           {
4028             printf (_("  Compilation Unit @ offset %#" PRIx64 ":\n"),
4029                       cu_offset);
4030             printf (_("   Length:        %#" PRIx64 " (%s)\n"),
4031                       compunit.cu_length,
4032                       offset_size == 8 ? "64-bit" : "32-bit");
4033             printf (_("   Version:       %d\n"), compunit.cu_version);
4034             if (compunit.cu_version >= 5)
4035               {
4036                 const char *name = get_DW_UT_name (compunit.cu_unit_type);
4037 
4038                 printf (_("   Unit Type:     %s (%x)\n"),
4039                           null_name (name),
4040                           compunit.cu_unit_type);
4041               }
4042             printf (_("   Abbrev Offset: %#" PRIx64 "\n"),
4043                       compunit.cu_abbrev_offset);
4044             printf (_("   Pointer Size:  %d\n"), compunit.cu_pointer_size);
4045             if (do_types)
4046               {
4047                 printf (_("   Signature:     %#" PRIx64 "\n"), signature);
4048                 printf (_("   Type Offset:   %#" PRIx64 "\n"), type_offset);
4049               }
4050             if (do_dwo_id)
4051               printf (_("   DWO ID:        %#" PRIx64 "\n"), dwo_id);
4052             if (this_set != NULL)
4053               {
4054                 uint64_t *offsets = this_set->section_offsets;
4055                 size_t *sizes = this_set->section_sizes;
4056 
4057                 printf (_("   Section contributions:\n"));
4058                 printf (_("    .debug_abbrev.dwo:       %#" PRIx64 "  %#zx\n"),
4059                           offsets[DW_SECT_ABBREV], sizes[DW_SECT_ABBREV]);
4060                 printf (_("    .debug_line.dwo:         %#" PRIx64 "  %#zx\n"),
4061                           offsets[DW_SECT_LINE], sizes[DW_SECT_LINE]);
4062                 printf (_("    .debug_loc.dwo:          %#" PRIx64 "  %#zx\n"),
4063                           offsets[DW_SECT_LOC], sizes[DW_SECT_LOC]);
4064                 printf (_("    .debug_str_offsets.dwo:  %#" PRIx64 "  %#zx\n"),
4065                           offsets[DW_SECT_STR_OFFSETS], sizes[DW_SECT_STR_OFFSETS]);
4066               }
4067           }
4068 
4069       tags = hdrptr;
4070       start = end_cu;
4071 
4072       if (compunit.cu_version < 2 || compunit.cu_version > 5)
4073           {
4074             warn (_("CU at offset %#" PRIx64 " contains corrupt or "
4075                       "unsupported version number: %d.\n"),
4076                     cu_offset, compunit.cu_version);
4077             continue;
4078           }
4079 
4080       if (compunit.cu_unit_type != DW_UT_compile
4081             && compunit.cu_unit_type != DW_UT_partial
4082             && compunit.cu_unit_type != DW_UT_type
4083             && compunit.cu_unit_type != DW_UT_split_compile
4084             && compunit.cu_unit_type != DW_UT_skeleton)
4085           {
4086             warn (_("CU at offset %#" PRIx64 " contains corrupt or "
4087                       "unsupported unit type: %d.\n"),
4088                     cu_offset, compunit.cu_unit_type);
4089             continue;
4090           }
4091 
4092       /* Process the abbrevs used by this compilation unit.  */
4093       abbrev_list *list;
4094       list = find_and_process_abbrev_set (&debug_displays[abbrev_sec].section,
4095                                                     abbrev_base, abbrev_size,
4096                                                     compunit.cu_abbrev_offset, NULL);
4097       level = 0;
4098       last_level = level;
4099       saved_level = -1;
4100       while (tags < start)
4101           {
4102             unsigned long abbrev_number;
4103             unsigned long die_offset;
4104             abbrev_entry *entry;
4105             abbrev_attr *attr;
4106             int do_printing = 1;
4107 
4108             die_offset = tags - section_begin;
4109 
4110             READ_ULEB (abbrev_number, tags, start);
4111 
4112             /* A null DIE marks the end of a list of siblings or it may also be
4113                a section padding.  */
4114             if (abbrev_number == 0)
4115               {
4116                 /* Check if it can be a section padding for the last CU.  */
4117                 if (level == 0 && start == end)
4118                     {
4119                       unsigned char *chk;
4120 
4121                       for (chk = tags; chk < start; chk++)
4122                         if (*chk != 0)
4123                           break;
4124                       if (chk == start)
4125                         break;
4126                     }
4127 
4128                 if (!do_loc && die_offset >= dwarf_start_die
4129                       && (dwarf_cutoff_level == -1
4130                           || level < dwarf_cutoff_level))
4131                     printf (_(" <%d><%lx>: Abbrev Number: 0\n"),
4132                               level, die_offset);
4133 
4134                 --level;
4135                 if (level < 0)
4136                     {
4137                       static unsigned num_bogus_warns = 0;
4138 
4139                       if (num_bogus_warns < 3)
4140                         {
4141                           warn (_("Bogus end-of-siblings marker detected at offset %lx in %s section\n"),
4142                                   die_offset, section->name);
4143                           num_bogus_warns ++;
4144                           if (num_bogus_warns == 3)
4145                               warn (_("Further warnings about bogus end-of-sibling markers suppressed\n"));
4146                         }
4147                     }
4148                 if (dwarf_start_die != 0 && level < saved_level)
4149                     {
4150                       if (list != NULL)
4151                         free_abbrev_list (list);
4152                       return true;
4153                     }
4154                 continue;
4155               }
4156 
4157             if (!do_loc)
4158               {
4159                 if (dwarf_start_die != 0 && die_offset < dwarf_start_die)
4160                     do_printing = 0;
4161                 else
4162                     {
4163                       if (dwarf_start_die != 0 && die_offset == dwarf_start_die)
4164                         saved_level = level;
4165                       do_printing = (dwarf_cutoff_level == -1
4166                                          || level < dwarf_cutoff_level);
4167                       if (do_printing)
4168                         printf (_(" <%d><%lx>: Abbrev Number: %lu"),
4169                                   level, die_offset, abbrev_number);
4170                       else if (dwarf_cutoff_level == -1
4171                                  || last_level < dwarf_cutoff_level)
4172                         printf (_(" <%d><%lx>: ...\n"), level, die_offset);
4173                       last_level = level;
4174                     }
4175               }
4176 
4177             /* Scan through the abbreviation list until we reach the
4178                correct entry.  */
4179             entry = NULL;
4180             if (list != NULL)
4181               for (entry = list->first_abbrev; entry != NULL; entry = entry->next)
4182                 if (entry->number == abbrev_number)
4183                     break;
4184 
4185             if (entry == NULL)
4186               {
4187                 if (!do_loc && do_printing)
4188                     {
4189                       printf ("\n");
4190                       fflush (stdout);
4191                     }
4192                 warn (_("DIE at offset %#lx refers to abbreviation number %lu which does not exist\n"),
4193                         die_offset, abbrev_number);
4194                 if (list != NULL)
4195                     free_abbrev_list (list);
4196                 return false;
4197               }
4198 
4199             if (!do_loc && do_printing)
4200               printf (" (%s)\n", get_TAG_name (entry->tag));
4201 
4202             switch (entry->tag)
4203               {
4204               default:
4205                 need_base_address = 0;
4206                 break;
4207               case DW_TAG_compile_unit:
4208               case DW_TAG_skeleton_unit:
4209                 need_base_address = 1;
4210                 need_dwo_info = do_loc;
4211                 break;
4212               case DW_TAG_entry_point:
4213                 need_base_address = 0;
4214                 /* Assuming that there is no DW_AT_frame_base.  */
4215                 have_frame_base = 0;
4216                 break;
4217               case DW_TAG_subprogram:
4218                 need_base_address = 0;
4219                     if (level <= frame_base_level)
4220                       /* Don't reset that for nested subprogram.  */
4221                       have_frame_base = 0;
4222                 break;
4223               }
4224 
4225             debug_info *debug_info_p =
4226               (debug_information && unit < alloc_num_debug_info_entries)
4227               ? debug_information + unit : NULL;
4228 
4229             assert (!debug_info_p
4230                       || (debug_info_p->num_loc_offsets
4231                           == debug_info_p->num_loc_views));
4232 
4233             /* Look ahead so that the values of DW_AT_rnglists_base,
4234                DW_AT_[GNU_]addr_base are available before attributes that
4235                reference them are parsed in the same DIE.
4236                Only needed for the top DIE on DWARFv5+.
4237                No simiar treatment for loclists_base because there should
4238                be no loclist attributes in top DIE.  */
4239             if (compunit.cu_version >= 5 && level == 0)
4240               {
4241                 int64_t stemp;
4242 
4243                 read_bases (entry,
4244                                 tags,
4245                                 start,
4246                                 compunit.cu_pointer_size,
4247                                 offset_size,
4248                                 compunit.cu_version,
4249                                 debug_info_p);
4250 
4251                 /* This check was in place before, keep it.  */
4252                 stemp = debug_info_p->rnglists_base;
4253                 if (stemp < 0)
4254                     {
4255                       warn (_("CU @ %#" PRIx64 " has has a negative rnglists_base "
4256                                 "value of %#" PRIx64 " - treating as zero"),
4257                               debug_info_p->cu_offset, stemp);
4258                       debug_info_p->rnglists_base = 0;
4259                     }
4260               }
4261 
4262             for (attr = entry->first_attr;
4263                  attr && attr->attribute;
4264                  attr = attr->next)
4265               {
4266                 if (! do_loc && do_printing)
4267                     /* Show the offset from where the tag was extracted.  */
4268                     printf ("    <%tx>", tags - section_begin);
4269                 tags = read_and_display_attr (attr->attribute,
4270                                                       attr->form,
4271                                                       attr->implicit_const,
4272                                                       section_begin,
4273                                                       tags,
4274                                                       start,
4275                                                       cu_offset,
4276                                                       compunit.cu_pointer_size,
4277                                                       offset_size,
4278                                                       compunit.cu_version,
4279                                                       debug_info_p,
4280                                                       do_loc || ! do_printing,
4281                                                       section,
4282                                                       this_set,
4283                                                       level);
4284               }
4285 
4286             /* If a locview attribute appears before a location one,
4287                make sure we don't associate it with an earlier
4288                loclist. */
4289             if (debug_info_p)
4290               switch (debug_info_p->num_loc_offsets - debug_info_p->num_loc_views)
4291                 {
4292                 case 1:
4293                     debug_info_p->loc_views [debug_info_p->num_loc_views] = -1;
4294                     debug_info_p->num_loc_views++;
4295                     assert (debug_info_p->num_loc_views
4296                               == debug_info_p->num_loc_offsets);
4297                     break;
4298 
4299                 case 0:
4300                     break;
4301 
4302                 case -1:
4303                     warn (_("DIE has locviews without loclist\n"));
4304                     debug_info_p->num_loc_views--;
4305                     break;
4306 
4307                 default:
4308                     assert (0);
4309               }
4310 
4311             if (entry->children)
4312               ++level;
4313           }
4314       if (list != NULL)
4315           free_abbrev_list (list);
4316     }
4317 
4318   /* Set num_debug_info_entries here so that it can be used to check if
4319      we need to process .debug_loc and .debug_ranges sections.  */
4320   if ((do_loc || do_debug_loc || do_debug_ranges || do_debug_info)
4321       && num_debug_info_entries == 0
4322       && ! do_types)
4323     {
4324       if (num_units > alloc_num_debug_info_entries)
4325           num_debug_info_entries = alloc_num_debug_info_entries;
4326       else
4327           num_debug_info_entries = num_units;
4328     }
4329 
4330   if (!do_loc)
4331     printf ("\n");
4332 
4333   return true;
4334 }
4335 
4336 /* Locate and scan the .debug_info section in the file and record the pointer
4337    sizes and offsets for the compilation units in it.  Usually an executable
4338    will have just one pointer size, but this is not guaranteed, and so we try
4339    not to make any assumptions.  Returns zero upon failure, or the number of
4340    compilation units upon success.  */
4341 
4342 static unsigned int
load_debug_info(void * file)4343 load_debug_info (void * file)
4344 {
4345   /* If we have already tried and failed to load the .debug_info
4346      section then do not bother to repeat the task.  */
4347   if (num_debug_info_entries == DEBUG_INFO_UNAVAILABLE)
4348     return 0;
4349 
4350   /* If we already have the information there is nothing else to do.  */
4351   if (num_debug_info_entries > 0)
4352     return num_debug_info_entries;
4353 
4354   /* If this is a DWARF package file, load the CU and TU indexes.  */
4355   (void) load_cu_tu_indexes (file);
4356 
4357   if (load_debug_section_with_follow (info, file)
4358       && process_debug_info (&debug_displays [info].section, file, abbrev, true, false))
4359     return num_debug_info_entries;
4360 
4361   if (load_debug_section_with_follow (info_dwo, file)
4362       && process_debug_info (&debug_displays [info_dwo].section, file,
4363                                    abbrev_dwo, true, false))
4364     return num_debug_info_entries;
4365 
4366   num_debug_info_entries = DEBUG_INFO_UNAVAILABLE;
4367   return 0;
4368 }
4369 
4370 /* Read a DWARF .debug_line section header starting at DATA.
4371    Upon success returns an updated DATA pointer and the LINFO
4372    structure and the END_OF_SEQUENCE pointer will be filled in.
4373    Otherwise returns NULL.  */
4374 
4375 static unsigned char *
read_debug_line_header(struct dwarf_section * section,unsigned char * data,unsigned char * end,DWARF2_Internal_LineInfo * linfo,unsigned char ** end_of_sequence)4376 read_debug_line_header (struct dwarf_section * section,
4377                               unsigned char * data,
4378                               unsigned char * end,
4379                               DWARF2_Internal_LineInfo * linfo,
4380                               unsigned char ** end_of_sequence)
4381 {
4382   unsigned char *hdrptr;
4383 
4384   /* Extract information from the Line Number Program Header.
4385      (section 6.2.4 in the Dwarf3 doc).  */
4386   hdrptr = data;
4387 
4388   /* Get and check the length of the block.  */
4389   SAFE_BYTE_GET_AND_INC (linfo->li_length, hdrptr, 4, end);
4390 
4391   if (linfo->li_length == 0xffffffff)
4392     {
4393       /* This section is 64-bit DWARF 3.  */
4394       SAFE_BYTE_GET_AND_INC (linfo->li_length, hdrptr, 8, end);
4395       linfo->li_offset_size = 8;
4396     }
4397   else
4398     linfo->li_offset_size = 4;
4399 
4400   if (linfo->li_length > (size_t) (end - hdrptr))
4401     {
4402       /* If the length field has a relocation against it, then we should
4403            not complain if it is inaccurate (and probably negative).  This
4404            happens in object files when the .debug_line section is actually
4405            comprised of several different .debug_line.* sections, (some of
4406            which may be removed by linker garbage collection), and a relocation
4407            is used to compute the correct length once that is done.  */
4408       if (reloc_at (section, (hdrptr - section->start) - linfo->li_offset_size))
4409           {
4410             linfo->li_length = end - hdrptr;
4411           }
4412       else
4413           {
4414             warn (_("The length field (%#" PRIx64 ")"
4415                       " in the debug_line header is wrong"
4416                       " - the section is too small\n"),
4417                     linfo->li_length);
4418             return NULL;
4419           }
4420     }
4421   end = hdrptr + linfo->li_length;
4422 
4423   /* Get and check the version number.  */
4424   SAFE_BYTE_GET_AND_INC (linfo->li_version, hdrptr, 2, end);
4425 
4426   if (linfo->li_version != 2
4427       && linfo->li_version != 3
4428       && linfo->li_version != 4
4429       && linfo->li_version != 5)
4430     {
4431       warn (_("Only DWARF version 2, 3, 4 and 5 line info "
4432                 "is currently supported.\n"));
4433       return NULL;
4434     }
4435 
4436   if (linfo->li_version >= 5)
4437     {
4438       SAFE_BYTE_GET_AND_INC (linfo->li_address_size, hdrptr, 1, end);
4439 
4440       SAFE_BYTE_GET_AND_INC (linfo->li_segment_size, hdrptr, 1, end);
4441       if (linfo->li_segment_size != 0)
4442           {
4443             warn (_("The %s section contains "
4444                       "unsupported segment selector size: %d.\n"),
4445                     section->name, linfo->li_segment_size);
4446             return NULL;
4447           }
4448     }
4449 
4450   SAFE_BYTE_GET_AND_INC (linfo->li_prologue_length, hdrptr,
4451                                linfo->li_offset_size, end);
4452   SAFE_BYTE_GET_AND_INC (linfo->li_min_insn_length, hdrptr, 1, end);
4453 
4454   if (linfo->li_version >= 4)
4455     {
4456       SAFE_BYTE_GET_AND_INC (linfo->li_max_ops_per_insn, hdrptr, 1, end);
4457 
4458       if (linfo->li_max_ops_per_insn == 0)
4459           {
4460             warn (_("Invalid maximum operations per insn.\n"));
4461             return NULL;
4462           }
4463     }
4464   else
4465     linfo->li_max_ops_per_insn = 1;
4466 
4467   SAFE_BYTE_GET_AND_INC (linfo->li_default_is_stmt, hdrptr, 1, end);
4468   SAFE_SIGNED_BYTE_GET_AND_INC (linfo->li_line_base, hdrptr, 1, end);
4469   SAFE_BYTE_GET_AND_INC (linfo->li_line_range, hdrptr, 1, end);
4470   SAFE_BYTE_GET_AND_INC (linfo->li_opcode_base, hdrptr, 1, end);
4471 
4472   *end_of_sequence = end;
4473   return hdrptr;
4474 }
4475 
4476 static unsigned char *
display_formatted_table(unsigned char * data,unsigned char * start,unsigned char * end,const DWARF2_Internal_LineInfo * linfo,struct dwarf_section * section,bool is_dir)4477 display_formatted_table (unsigned char *data,
4478                                unsigned char *start,
4479                                unsigned char *end,
4480                                const DWARF2_Internal_LineInfo *linfo,
4481                                struct dwarf_section *section,
4482                                bool is_dir)
4483 {
4484   unsigned char *format_start, format_count, *format, formati;
4485   uint64_t data_count, datai;
4486   unsigned int namepass, last_entry = 0;
4487   const char * table_name = is_dir ? N_("Directory Table") : N_("File Name Table");
4488 
4489   SAFE_BYTE_GET_AND_INC (format_count, data, 1, end);
4490   if (do_checks && format_count > 5)
4491     warn (_("Unexpectedly large number of columns in the %s (%u)\n"),
4492             table_name, format_count);
4493 
4494   format_start = data;
4495   for (formati = 0; formati < format_count; formati++)
4496     {
4497       SKIP_ULEB (data, end);
4498       SKIP_ULEB (data, end);
4499       if (data >= end)
4500           {
4501             warn (_("%s: Corrupt format description entry\n"), table_name);
4502             return data;
4503           }
4504     }
4505 
4506   READ_ULEB (data_count, data, end);
4507   if (data_count == 0)
4508     {
4509       printf (_("\n The %s is empty.\n"), table_name);
4510       return data;
4511     }
4512   else if (data >= end
4513              || data_count > (size_t) (end - data))
4514     {
4515       warn (_("%s: Corrupt entry count %#" PRIx64 "\n"), table_name, data_count);
4516       return data;
4517     }
4518 
4519   else if (format_count == 0)
4520     {
4521       warn (_("%s: format count is zero, but the table is not empty\n"),
4522               table_name);
4523       return end;
4524     }
4525 
4526   printf (_("\n The %s (offset %#tx, lines %" PRIu64 ", columns %u):\n"),
4527             table_name, data - start, data_count, format_count);
4528 
4529   printf (_("  Entry"));
4530   /* Delay displaying name as the last entry for better screen layout.  */
4531   for (namepass = 0; namepass < 2; namepass++)
4532     {
4533       format = format_start;
4534       for (formati = 0; formati < format_count; formati++)
4535           {
4536             uint64_t content_type;
4537 
4538             READ_ULEB (content_type, format, end);
4539             if ((content_type == DW_LNCT_path) == (namepass == 1))
4540               switch (content_type)
4541                 {
4542                 case DW_LNCT_path:
4543                     printf (_("\tName"));
4544                     break;
4545                 case DW_LNCT_directory_index:
4546                     printf (_("\tDir"));
4547                     break;
4548                 case DW_LNCT_timestamp:
4549                     printf (_("\tTime"));
4550                     break;
4551                 case DW_LNCT_size:
4552                     printf (_("\tSize"));
4553                     break;
4554                 case DW_LNCT_MD5:
4555                     printf (_("\tMD5\t\t\t"));
4556                     break;
4557                 default:
4558                     printf (_("\t(Unknown format content type %" PRIu64 ")"),
4559                               content_type);
4560                 }
4561             SKIP_ULEB (format, end);
4562           }
4563     }
4564   putchar ('\n');
4565 
4566   for (datai = 0; datai < data_count; datai++)
4567     {
4568       unsigned char *datapass = data;
4569 
4570       printf ("  %d", last_entry++);
4571       /* Delay displaying name as the last entry for better screen layout.  */
4572       for (namepass = 0; namepass < 2; namepass++)
4573           {
4574             format = format_start;
4575             data = datapass;
4576             for (formati = 0; formati < format_count; formati++)
4577               {
4578                 uint64_t content_type, form;
4579 
4580                 READ_ULEB (content_type, format, end);
4581                 READ_ULEB (form, format, end);
4582                 data = read_and_display_attr_value (0, form, 0, start, data, end,
4583                                                               0, 0, linfo->li_offset_size,
4584                                                               linfo->li_version, NULL,
4585                                   ((content_type == DW_LNCT_path) != (namepass == 1)),
4586                                                               section, NULL, '\t', -1);
4587               }
4588           }
4589 
4590       if (data >= end && (datai < data_count - 1))
4591           {
4592             warn (_("\n%s: Corrupt entries list\n"), table_name);
4593             return data;
4594           }
4595       putchar ('\n');
4596     }
4597   return data;
4598 }
4599 
4600 static int
display_debug_sup(struct dwarf_section * section,void * file ATTRIBUTE_UNUSED)4601 display_debug_sup (struct dwarf_section *  section,
4602                        void *                  file ATTRIBUTE_UNUSED)
4603 {
4604   unsigned char * start = section->start;
4605   unsigned char * end = section->start + section->size;
4606   unsigned int version;
4607   char is_supplementary;
4608   const unsigned char * sup_filename;
4609   size_t sup_filename_len;
4610   unsigned int num_read;
4611   int status;
4612   uint64_t checksum_len;
4613 
4614 
4615   introduce (section, true);
4616   if (section->size < 4)
4617     {
4618       error (_("corrupt .debug_sup section: size is too small\n"));
4619       return 0;
4620     }
4621 
4622   /* Read the data.  */
4623   SAFE_BYTE_GET_AND_INC (version, start, 2, end);
4624   if (version < 5)
4625     warn (_("corrupt .debug_sup section: version < 5"));
4626 
4627   SAFE_BYTE_GET_AND_INC (is_supplementary, start, 1, end);
4628   if (is_supplementary != 0 && is_supplementary != 1)
4629     warn (_("corrupt .debug_sup section: is_supplementary not 0 or 1\n"));
4630 
4631   sup_filename = start;
4632   if (is_supplementary && sup_filename[0] != 0)
4633     warn (_("corrupt .debug_sup section: filename not empty in supplementary section\n"));
4634 
4635   sup_filename_len = strnlen ((const char *) start, end - start);
4636   if (sup_filename_len == (size_t) (end - start))
4637     {
4638       error (_("corrupt .debug_sup section: filename is not NUL terminated\n"));
4639       return 0;
4640     }
4641   start += sup_filename_len + 1;
4642 
4643   checksum_len = read_leb128 (start, end, false /* unsigned */, & num_read, & status);
4644   if (status)
4645     {
4646       error (_("corrupt .debug_sup section: bad LEB128 field for checksum length\n"));
4647       checksum_len = 0;
4648     }
4649   start += num_read;
4650   if (checksum_len > (size_t) (end - start))
4651     {
4652       error (_("corrupt .debug_sup section: checksum length is longer than the remaining section length\n"));
4653       checksum_len = end - start;
4654     }
4655   else if (checksum_len < (size_t) (end - start))
4656     {
4657       warn (_("corrupt .debug_sup section: there are %#" PRIx64
4658                 " extra, unused bytes at the end of the section\n"),
4659               (end - start) - checksum_len);
4660     }
4661 
4662   printf (_("  Version:      %u\n"), version);
4663   printf (_("  Is Supp:      %u\n"), is_supplementary);
4664   printf (_("  Filename:     %s\n"), sup_filename);
4665   printf (_("  Checksum Len: %" PRIu64 "\n"), checksum_len);
4666   if (checksum_len > 0)
4667     {
4668       printf (_("  Checksum:     "));
4669       while (checksum_len--)
4670           printf ("0x%x ", * start++ );
4671       printf ("\n");
4672     }
4673   return 1;
4674 }
4675 
4676 static int
display_debug_lines_raw(struct dwarf_section * section,unsigned char * data,unsigned char * end,void * file)4677 display_debug_lines_raw (struct dwarf_section *  section,
4678                                unsigned char *         data,
4679                                unsigned char *         end,
4680                                void *                  file)
4681 {
4682   unsigned char *start = section->start;
4683   int verbose_view = 0;
4684 
4685   introduce (section, true);
4686 
4687   while (data < end)
4688     {
4689       static DWARF2_Internal_LineInfo saved_linfo;
4690       DWARF2_Internal_LineInfo linfo;
4691       unsigned char *standard_opcodes;
4692       unsigned char *end_of_sequence;
4693       int i;
4694 
4695       if (startswith (section->name, ".debug_line.")
4696             /* Note: the following does not apply to .debug_line.dwo sections.
4697                These are full debug_line sections.  */
4698             && strcmp (section->name, ".debug_line.dwo") != 0)
4699           {
4700             /* Sections named .debug_line.<foo> are fragments of a .debug_line
4701                section containing just the Line Number Statements.  They are
4702                created by the assembler and intended to be used alongside gcc's
4703                -ffunction-sections command line option.  When the linker's
4704                garbage collection decides to discard a .text.<foo> section it
4705                can then also discard the line number information in .debug_line.<foo>.
4706 
4707                Since the section is a fragment it does not have the details
4708                needed to fill out a LineInfo structure, so instead we use the
4709                details from the last full debug_line section that we processed.  */
4710             end_of_sequence = end;
4711             standard_opcodes = NULL;
4712             linfo = saved_linfo;
4713             /* PR 17531: file: 0522b371.  */
4714             if (linfo.li_line_range == 0)
4715               {
4716                 warn (_("Partial .debug_line. section encountered without a prior full .debug_line section\n"));
4717                 return 0;
4718               }
4719             reset_state_machine (linfo.li_default_is_stmt);
4720           }
4721       else
4722           {
4723             unsigned char * hdrptr;
4724 
4725             if ((hdrptr = read_debug_line_header (section, data, end, & linfo,
4726                                                             & end_of_sequence)) == NULL)
4727               return 0;
4728 
4729             printf (_("  Offset:                      %#tx\n"), data - start);
4730             printf (_("  Length:                      %" PRId64 "\n"), linfo.li_length);
4731             printf (_("  DWARF Version:               %d\n"), linfo.li_version);
4732             if (linfo.li_version >= 5)
4733               {
4734                 printf (_("  Address size (bytes):        %d\n"), linfo.li_address_size);
4735                 printf (_("  Segment selector (bytes):    %d\n"), linfo.li_segment_size);
4736               }
4737             printf (_("  Prologue Length:             %d\n"), (int) linfo.li_prologue_length);
4738             printf (_("  Minimum Instruction Length:  %d\n"), linfo.li_min_insn_length);
4739             if (linfo.li_version >= 4)
4740               printf (_("  Maximum Ops per Instruction: %d\n"), linfo.li_max_ops_per_insn);
4741             printf (_("  Initial value of 'is_stmt':  %d\n"), linfo.li_default_is_stmt);
4742             printf (_("  Line Base:                   %d\n"), linfo.li_line_base);
4743             printf (_("  Line Range:                  %d\n"), linfo.li_line_range);
4744             printf (_("  Opcode Base:                 %d\n"), linfo.li_opcode_base);
4745 
4746             /* PR 17512: file: 1665-6428-0.004.  */
4747             if (linfo.li_line_range == 0)
4748               {
4749                 warn (_("Line range of 0 is invalid, using 1 instead\n"));
4750                 linfo.li_line_range = 1;
4751               }
4752 
4753             reset_state_machine (linfo.li_default_is_stmt);
4754 
4755             /* Display the contents of the Opcodes table.  */
4756             standard_opcodes = hdrptr;
4757 
4758             /* PR 17512: file: 002-417945-0.004.  */
4759             if (standard_opcodes + linfo.li_opcode_base >= end)
4760               {
4761                 warn (_("Line Base extends beyond end of section\n"));
4762                 return 0;
4763               }
4764 
4765             printf (_("\n Opcodes:\n"));
4766 
4767             for (i = 1; i < linfo.li_opcode_base; i++)
4768               printf (ngettext ("  Opcode %d has %d arg\n",
4769                                     "  Opcode %d has %d args\n",
4770                                     standard_opcodes[i - 1]),
4771                         i, standard_opcodes[i - 1]);
4772 
4773             /* Display the contents of the Directory table.  */
4774             data = standard_opcodes + linfo.li_opcode_base - 1;
4775 
4776             if (linfo.li_version >= 5)
4777               {
4778                 load_debug_section_with_follow (line_str, file);
4779 
4780                 data = display_formatted_table (data, start, end, &linfo, section,
4781                                                         true);
4782                 data = display_formatted_table (data, start, end, &linfo, section,
4783                                                         false);
4784               }
4785             else
4786               {
4787                 if (*data == 0)
4788                     printf (_("\n The Directory Table is empty.\n"));
4789                 else
4790                     {
4791                       unsigned int last_dir_entry = 0;
4792 
4793                       printf (_("\n The Directory Table (offset %#tx):\n"),
4794                                 data - start);
4795 
4796                       while (data < end && *data != 0)
4797                         {
4798                           printf ("  %d\t%.*s\n", ++last_dir_entry, (int) (end - data), data);
4799 
4800                           data += strnlen ((char *) data, end - data);
4801                           if (data < end)
4802                               data++;
4803                         }
4804 
4805                       /* PR 17512: file: 002-132094-0.004.  */
4806                       if (data >= end - 1)
4807                         break;
4808                     }
4809 
4810                 /* Skip the NUL at the end of the table.  */
4811                 if (data < end)
4812                     data++;
4813 
4814                 /* Display the contents of the File Name table.  */
4815                 if (data >= end || *data == 0)
4816                     printf (_("\n The File Name Table is empty.\n"));
4817                 else
4818                     {
4819                       printf (_("\n The File Name Table (offset %#tx):\n"),
4820                                 data - start);
4821                       printf (_("  Entry\tDir\tTime\tSize\tName\n"));
4822 
4823                       while (data < end && *data != 0)
4824                         {
4825                           unsigned char *name;
4826                           uint64_t val;
4827 
4828                           printf ("  %d\t", ++state_machine_regs.last_file_entry);
4829                           name = data;
4830                           data += strnlen ((char *) data, end - data);
4831                           if (data < end)
4832                               data++;
4833 
4834                           READ_ULEB (val, data, end);
4835                           printf ("%" PRIu64 "\t", val);
4836                           READ_ULEB (val, data, end);
4837                           printf ("%" PRIu64 "\t", val);
4838                           READ_ULEB (val, data, end);
4839                           printf ("%" PRIu64 "\t", val);
4840                           printf ("%.*s\n", (int)(end - name), name);
4841 
4842                           if (data >= end)
4843                               {
4844                                 warn (_("Corrupt file name table entry\n"));
4845                                 break;
4846                               }
4847                         }
4848                     }
4849 
4850                 /* Skip the NUL at the end of the table.  */
4851                 if (data < end)
4852                     data++;
4853               }
4854 
4855             putchar ('\n');
4856             saved_linfo = linfo;
4857           }
4858 
4859       /* Now display the statements.  */
4860       if (data >= end_of_sequence)
4861           printf (_(" No Line Number Statements.\n"));
4862       else
4863           {
4864             printf (_(" Line Number Statements:\n"));
4865 
4866             while (data < end_of_sequence)
4867               {
4868                 unsigned char op_code;
4869                 int adv;
4870                 uint64_t uladv;
4871 
4872                 printf ("  [0x%08tx]", data - start);
4873 
4874                 op_code = *data++;
4875 
4876                 if (op_code >= linfo.li_opcode_base)
4877                     {
4878                       op_code -= linfo.li_opcode_base;
4879                       uladv = (op_code / linfo.li_line_range);
4880                       if (linfo.li_max_ops_per_insn == 1)
4881                         {
4882                           uladv *= linfo.li_min_insn_length;
4883                           state_machine_regs.address += uladv;
4884                           if (uladv)
4885                               state_machine_regs.view = 0;
4886                           printf (_("  Special opcode %d: "
4887                                         "advance Address by %" PRIu64
4888                                         " to %#" PRIx64 "%s"),
4889                                     op_code, uladv, state_machine_regs.address,
4890                                     verbose_view && uladv
4891                                     ? _(" (reset view)") : "");
4892                         }
4893                       else
4894                         {
4895                           unsigned addrdelta
4896                               = ((state_machine_regs.op_index + uladv)
4897                                   / linfo.li_max_ops_per_insn)
4898                               * linfo.li_min_insn_length;
4899 
4900                           state_machine_regs.address += addrdelta;
4901                           state_machine_regs.op_index
4902                               = (state_machine_regs.op_index + uladv)
4903                               % linfo.li_max_ops_per_insn;
4904                           if (addrdelta)
4905                               state_machine_regs.view = 0;
4906                           printf (_("  Special opcode %d: "
4907                                         "advance Address by %" PRIu64
4908                                         " to %#" PRIx64 "[%d]%s"),
4909                                     op_code, uladv, state_machine_regs.address,
4910                                     state_machine_regs.op_index,
4911                                     verbose_view && addrdelta
4912                                     ? _(" (reset view)") : "");
4913                         }
4914                       adv = (op_code % linfo.li_line_range) + linfo.li_line_base;
4915                       state_machine_regs.line += adv;
4916                       printf (_(" and Line by %d to %d"),
4917                                 adv, state_machine_regs.line);
4918                       if (verbose_view || state_machine_regs.view)
4919                         printf (_(" (view %u)\n"), state_machine_regs.view);
4920                       else
4921                         putchar ('\n');
4922                       state_machine_regs.view++;
4923                     }
4924                 else
4925                     switch (op_code)
4926                       {
4927                       case DW_LNS_extended_op:
4928                         data += process_extended_line_op (data,
4929                                                                   linfo.li_default_is_stmt,
4930                                                                   end);
4931                         break;
4932 
4933                       case DW_LNS_copy:
4934                         printf (_("  Copy"));
4935                         if (verbose_view || state_machine_regs.view)
4936                           printf (_(" (view %u)\n"), state_machine_regs.view);
4937                         else
4938                           putchar ('\n');
4939                         state_machine_regs.view++;
4940                         break;
4941 
4942                       case DW_LNS_advance_pc:
4943                         READ_ULEB (uladv, data, end);
4944                         if (linfo.li_max_ops_per_insn == 1)
4945                           {
4946                               uladv *= linfo.li_min_insn_length;
4947                               state_machine_regs.address += uladv;
4948                               if (uladv)
4949                                 state_machine_regs.view = 0;
4950                               printf (_("  Advance PC by %" PRIu64
4951                                           " to %#" PRIx64 "%s\n"),
4952                                         uladv, state_machine_regs.address,
4953                                         verbose_view && uladv
4954                                         ? _(" (reset view)") : "");
4955                           }
4956                         else
4957                           {
4958                               unsigned addrdelta
4959                                 = ((state_machine_regs.op_index + uladv)
4960                                    / linfo.li_max_ops_per_insn)
4961                                 * linfo.li_min_insn_length;
4962                               state_machine_regs.address
4963                                 += addrdelta;
4964                               state_machine_regs.op_index
4965                                 = (state_machine_regs.op_index + uladv)
4966                                 % linfo.li_max_ops_per_insn;
4967                               if (addrdelta)
4968                                 state_machine_regs.view = 0;
4969                               printf (_("  Advance PC by %" PRIu64
4970                                           " to %#" PRIx64 "[%d]%s\n"),
4971                                         uladv, state_machine_regs.address,
4972                                         state_machine_regs.op_index,
4973                                         verbose_view && addrdelta
4974                                         ? _(" (reset view)") : "");
4975                           }
4976                         break;
4977 
4978                       case DW_LNS_advance_line:
4979                         READ_SLEB (adv, data, end);
4980                         state_machine_regs.line += adv;
4981                         printf (_("  Advance Line by %d to %d\n"),
4982                                   adv, state_machine_regs.line);
4983                         break;
4984 
4985                       case DW_LNS_set_file:
4986                         READ_ULEB (uladv, data, end);
4987                         printf (_("  Set File Name to entry %" PRIu64
4988                                     " in the File Name Table\n"), uladv);
4989                         state_machine_regs.file = uladv;
4990                         break;
4991 
4992                       case DW_LNS_set_column:
4993                         READ_ULEB (uladv, data, end);
4994                         printf (_("  Set column to %" PRIu64 "\n"), uladv);
4995                         state_machine_regs.column = uladv;
4996                         break;
4997 
4998                       case DW_LNS_negate_stmt:
4999                         adv = state_machine_regs.is_stmt;
5000                         adv = ! adv;
5001                         printf (_("  Set is_stmt to %d\n"), adv);
5002                         state_machine_regs.is_stmt = adv;
5003                         break;
5004 
5005                       case DW_LNS_set_basic_block:
5006                         printf (_("  Set basic block\n"));
5007                         state_machine_regs.basic_block = 1;
5008                         break;
5009 
5010                       case DW_LNS_const_add_pc:
5011                         uladv = ((255 - linfo.li_opcode_base) / linfo.li_line_range);
5012                         if (linfo.li_max_ops_per_insn)
5013                           {
5014                               uladv *= linfo.li_min_insn_length;
5015                               state_machine_regs.address += uladv;
5016                               if (uladv)
5017                                 state_machine_regs.view = 0;
5018                               printf (_("  Advance PC by constant %" PRIu64
5019                                           " to %#" PRIx64 "%s\n"),
5020                                         uladv, state_machine_regs.address,
5021                                         verbose_view && uladv
5022                                         ? _(" (reset view)") : "");
5023                           }
5024                         else
5025                           {
5026                               unsigned addrdelta
5027                                 = ((state_machine_regs.op_index + uladv)
5028                                    / linfo.li_max_ops_per_insn)
5029                                 * linfo.li_min_insn_length;
5030                               state_machine_regs.address
5031                                 += addrdelta;
5032                               state_machine_regs.op_index
5033                                 = (state_machine_regs.op_index + uladv)
5034                                 % linfo.li_max_ops_per_insn;
5035                               if (addrdelta)
5036                                 state_machine_regs.view = 0;
5037                               printf (_("  Advance PC by constant %" PRIu64
5038                                           " to %#" PRIx64 "[%d]%s\n"),
5039                                         uladv, state_machine_regs.address,
5040                                         state_machine_regs.op_index,
5041                                         verbose_view && addrdelta
5042                                         ? _(" (reset view)") : "");
5043                           }
5044                         break;
5045 
5046                       case DW_LNS_fixed_advance_pc:
5047                         SAFE_BYTE_GET_AND_INC (uladv, data, 2, end);
5048                         state_machine_regs.address += uladv;
5049                         state_machine_regs.op_index = 0;
5050                         printf (_("  Advance PC by fixed size amount %" PRIu64
5051                                     " to %#" PRIx64 "\n"),
5052                                   uladv, state_machine_regs.address);
5053                         /* Do NOT reset view.  */
5054                         break;
5055 
5056                       case DW_LNS_set_prologue_end:
5057                         printf (_("  Set prologue_end to true\n"));
5058                         break;
5059 
5060                       case DW_LNS_set_epilogue_begin:
5061                         printf (_("  Set epilogue_begin to true\n"));
5062                         break;
5063 
5064                       case DW_LNS_set_isa:
5065                         READ_ULEB (uladv, data, end);
5066                         printf (_("  Set ISA to %" PRIu64 "\n"), uladv);
5067                         break;
5068 
5069                       default:
5070                         printf (_("  Unknown opcode %d with operands: "), op_code);
5071 
5072                         if (standard_opcodes != NULL)
5073                           for (i = standard_opcodes[op_code - 1]; i > 0 ; --i)
5074                               {
5075                                 READ_ULEB (uladv, data, end);
5076                                 printf ("%#" PRIx64 "%s", uladv, i == 1 ? "" : ", ");
5077                               }
5078                         putchar ('\n');
5079                         break;
5080                       }
5081               }
5082             putchar ('\n');
5083           }
5084     }
5085 
5086   return 1;
5087 }
5088 
5089 typedef struct
5090 {
5091   char *name;
5092   unsigned int directory_index;
5093   unsigned int modification_date;
5094   unsigned int length;
5095 } File_Entry;
5096 
5097 /* Output a decoded representation of the .debug_line section.  */
5098 
5099 static int
display_debug_lines_decoded(struct dwarf_section * section,unsigned char * start,unsigned char * data,unsigned char * end,void * fileptr)5100 display_debug_lines_decoded (struct dwarf_section *  section,
5101                                    unsigned char *         start,
5102                                    unsigned char *         data,
5103                                    unsigned char *         end,
5104                                    void *                  fileptr)
5105 {
5106   static DWARF2_Internal_LineInfo saved_linfo;
5107 
5108   introduce (section, false);
5109 
5110   while (data < end)
5111     {
5112       /* This loop amounts to one iteration per compilation unit.  */
5113       DWARF2_Internal_LineInfo linfo;
5114       unsigned char *standard_opcodes;
5115       unsigned char *end_of_sequence;
5116       int i;
5117       File_Entry *file_table = NULL;
5118       unsigned int n_files = 0;
5119       char **directory_table = NULL;
5120       unsigned int n_directories = 0;
5121 
5122       if (startswith (section->name, ".debug_line.")
5123             /* Note: the following does not apply to .debug_line.dwo sections.
5124                These are full debug_line sections.  */
5125             && strcmp (section->name, ".debug_line.dwo") != 0)
5126           {
5127             /* See comment in display_debug_lines_raw().  */
5128             end_of_sequence = end;
5129             standard_opcodes = NULL;
5130             linfo = saved_linfo;
5131             /* PR 17531: file: 0522b371.  */
5132             if (linfo.li_line_range == 0)
5133               {
5134                 warn (_("Partial .debug_line. section encountered without a prior full .debug_line section\n"));
5135                 return 0;
5136               }
5137             reset_state_machine (linfo.li_default_is_stmt);
5138           }
5139       else
5140           {
5141             unsigned char *hdrptr;
5142 
5143             if ((hdrptr = read_debug_line_header (section, data, end, & linfo,
5144                                                             & end_of_sequence)) == NULL)
5145                 return 0;
5146 
5147             /* PR 17531: file: 0522b371.  */
5148             if (linfo.li_line_range == 0)
5149               {
5150                 warn (_("Line range of 0 is invalid, using 1 instead\n"));
5151                 linfo.li_line_range = 1;
5152               }
5153             reset_state_machine (linfo.li_default_is_stmt);
5154 
5155             /* Save a pointer to the contents of the Opcodes table.  */
5156             standard_opcodes = hdrptr;
5157 
5158             /* Traverse the Directory table just to count entries.  */
5159             data = standard_opcodes + linfo.li_opcode_base - 1;
5160             /* PR 20440 */
5161             if (data >= end)
5162               {
5163                 warn (_("opcode base of %d extends beyond end of section\n"),
5164                         linfo.li_opcode_base);
5165                 return 0;
5166               }
5167 
5168             if (linfo.li_version >= 5)
5169               {
5170                 unsigned char *format_start, *format;
5171                 unsigned int format_count, formati, entryi;
5172 
5173                 load_debug_section_with_follow (line_str, fileptr);
5174 
5175                 /* Skip directories format.  */
5176                 SAFE_BYTE_GET_AND_INC (format_count, data, 1, end);
5177                 if (do_checks && format_count > 1)
5178                     warn (_("Unexpectedly large number of columns in the directory name table (%u)\n"),
5179                           format_count);
5180                 format_start = data;
5181                 for (formati = 0; formati < format_count; formati++)
5182                     {
5183                       SKIP_ULEB (data, end);
5184                       SKIP_ULEB (data, end);
5185                     }
5186 
5187                 READ_ULEB (n_directories, data, end);
5188                 if (data >= end)
5189                     {
5190                       warn (_("Corrupt directories list\n"));
5191                       break;
5192                     }
5193 
5194                 if (n_directories == 0)
5195                     directory_table = NULL;
5196                 else if (n_directories > section->size)
5197                     {
5198                       warn (_("number of directories (0x%x) exceeds size of section %s\n"),
5199                               n_directories, section->name);
5200                       return 0;
5201                     }
5202                 else
5203                     directory_table = (char **)
5204                       xcalloc (n_directories, sizeof (unsigned char *));
5205 
5206                 for (entryi = 0; entryi < n_directories; entryi++)
5207                     {
5208                       char **pathp = &directory_table[entryi];
5209 
5210                       format = format_start;
5211                       for (formati = 0; formati < format_count; formati++)
5212                         {
5213                           uint64_t content_type, form;
5214                           uint64_t uvalue;
5215 
5216                           READ_ULEB (content_type, format, end);
5217                           READ_ULEB (form, format, end);
5218                           if (data >= end)
5219                               {
5220                                 warn (_("Corrupt directories list\n"));
5221                                 break;
5222                               }
5223                           switch (content_type)
5224                               {
5225                               case DW_LNCT_path:
5226                                 switch (form)
5227                                   {
5228                                   case DW_FORM_string:
5229                                     *pathp = (char *) data;
5230                                     break;
5231                                   case DW_FORM_line_strp:
5232                                     SAFE_BYTE_GET (uvalue, data, linfo.li_offset_size,
5233                                                        end);
5234                                     /* Remove const by the cast.  */
5235                                     *pathp = (char *)
5236                                                fetch_indirect_line_string (uvalue);
5237                                     break;
5238                                   }
5239                                 break;
5240                               }
5241                           data = read_and_display_attr_value (0, form, 0, start,
5242                                                                         data, end, 0, 0,
5243                                                                         linfo.li_offset_size,
5244                                                                         linfo.li_version,
5245                                                                         NULL, 1, section,
5246                                                                         NULL, '\t', -1);
5247                         }
5248                       if (data >= end)
5249                         {
5250                           warn (_("Corrupt directories list\n"));
5251                           break;
5252                         }
5253                     }
5254 
5255                 /* Skip files format.  */
5256                 SAFE_BYTE_GET_AND_INC (format_count, data, 1, end);
5257                 if (do_checks && format_count > 5)
5258                     warn (_("Unexpectedly large number of columns in the file name table (%u)\n"),
5259                           format_count);
5260 
5261                 format_start = data;
5262                 for (formati = 0; formati < format_count; formati++)
5263                     {
5264                       SKIP_ULEB (data, end);
5265                       SKIP_ULEB (data, end);
5266                     }
5267 
5268                 READ_ULEB (n_files, data, end);
5269                 if (data >= end && n_files > 0)
5270                     {
5271                       warn (_("Corrupt file name list\n"));
5272                       break;
5273                     }
5274 
5275                 if (n_files == 0)
5276                     file_table = NULL;
5277                 else if (n_files > section->size)
5278                     {
5279                       warn (_("number of files (0x%x) exceeds size of section %s\n"),
5280                               n_files, section->name);
5281                       return 0;
5282                     }
5283                 else
5284                     file_table = (File_Entry *) xcalloc (n_files,
5285                                                                  sizeof (File_Entry));
5286 
5287                 for (entryi = 0; entryi < n_files; entryi++)
5288                     {
5289                       File_Entry *file = &file_table[entryi];
5290 
5291                       format = format_start;
5292                       for (formati = 0; formati < format_count; formati++)
5293                         {
5294                           uint64_t content_type, form;
5295                           uint64_t uvalue;
5296                           unsigned char *tmp;
5297 
5298                           READ_ULEB (content_type, format, end);
5299                           READ_ULEB (form, format, end);
5300                           if (data >= end)
5301                               {
5302                                 warn (_("Corrupt file name list\n"));
5303                                 break;
5304                               }
5305                           switch (content_type)
5306                               {
5307                               case DW_LNCT_path:
5308                                 switch (form)
5309                                   {
5310                                   case DW_FORM_string:
5311                                     file->name = (char *) data;
5312                                     break;
5313                                   case DW_FORM_line_strp:
5314                                     SAFE_BYTE_GET (uvalue, data, linfo.li_offset_size,
5315                                                        end);
5316                                     /* Remove const by the cast.  */
5317                                     file->name = (char *)
5318                                                      fetch_indirect_line_string (uvalue);
5319                                     break;
5320                                   }
5321                                 break;
5322                               case DW_LNCT_directory_index:
5323                                 switch (form)
5324                                   {
5325                                   case DW_FORM_data1:
5326                                     SAFE_BYTE_GET (file->directory_index, data, 1,
5327                                                        end);
5328                                     break;
5329                                   case DW_FORM_data2:
5330                                     SAFE_BYTE_GET (file->directory_index, data, 2,
5331                                                        end);
5332                                     break;
5333                                   case DW_FORM_udata:
5334                                     tmp = data;
5335                                     READ_ULEB (file->directory_index, tmp, end);
5336                                     break;
5337                                   }
5338                                 break;
5339                               }
5340                           data = read_and_display_attr_value (0, form, 0, start,
5341                                                                         data, end, 0, 0,
5342                                                                         linfo.li_offset_size,
5343                                                                         linfo.li_version,
5344                                                                         NULL, 1, section,
5345                                                                         NULL, '\t', -1);
5346                         }
5347                       if (data >= end)
5348                         {
5349                           warn (_("Corrupt file name list\n"));
5350                           break;
5351                         }
5352                     }
5353               }
5354             else
5355               {
5356                 if (*data != 0)
5357                     {
5358                       char *ptr_directory_table = (char *) data;
5359 
5360                       while (data < end && *data != 0)
5361                         {
5362                           data += strnlen ((char *) data, end - data);
5363                           if (data < end)
5364                               data++;
5365                           n_directories++;
5366                         }
5367 
5368                       /* PR 20440 */
5369                       if (data >= end)
5370                         {
5371                           warn (_("directory table ends unexpectedly\n"));
5372                           n_directories = 0;
5373                           break;
5374                         }
5375 
5376                       /* Go through the directory table again to save the directories.  */
5377                       directory_table = (char **)
5378                         xmalloc (n_directories * sizeof (unsigned char *));
5379 
5380                       i = 0;
5381                       while (*ptr_directory_table != 0)
5382                         {
5383                           directory_table[i] = ptr_directory_table;
5384                           ptr_directory_table += strlen (ptr_directory_table) + 1;
5385                           i++;
5386                         }
5387                     }
5388                 /* Skip the NUL at the end of the table.  */
5389                 data++;
5390 
5391                 /* Traverse the File Name table just to count the entries.  */
5392                 if (data < end && *data != 0)
5393                     {
5394                       unsigned char *ptr_file_name_table = data;
5395 
5396                       while (data < end && *data != 0)
5397                         {
5398                           /* Skip Name, directory index, last modification
5399                                time and length of file.  */
5400                           data += strnlen ((char *) data, end - data);
5401                           if (data < end)
5402                               data++;
5403                           SKIP_ULEB (data, end);
5404                           SKIP_ULEB (data, end);
5405                           SKIP_ULEB (data, end);
5406                           n_files++;
5407                         }
5408 
5409                       if (data >= end)
5410                         {
5411                           warn (_("file table ends unexpectedly\n"));
5412                           n_files = 0;
5413                           break;
5414                         }
5415 
5416                       /* Go through the file table again to save the strings.  */
5417                       file_table = (File_Entry *) xmalloc (n_files * sizeof (File_Entry));
5418 
5419                       i = 0;
5420                       while (*ptr_file_name_table != 0)
5421                         {
5422                           file_table[i].name = (char *) ptr_file_name_table;
5423                           ptr_file_name_table
5424                               += strlen ((char *) ptr_file_name_table) + 1;
5425 
5426                           /* We are not interested in directory, time or size.  */
5427                           READ_ULEB (file_table[i].directory_index,
5428                                          ptr_file_name_table, end);
5429                           READ_ULEB (file_table[i].modification_date,
5430                                          ptr_file_name_table, end);
5431                           READ_ULEB (file_table[i].length,
5432                                          ptr_file_name_table, end);
5433                           i++;
5434                         }
5435                       i = 0;
5436                     }
5437 
5438                 /* Skip the NUL at the end of the table.  */
5439                 data++;
5440               }
5441 
5442             /* Print the Compilation Unit's name and a header.  */
5443             if (file_table == NULL)
5444               printf (_("CU: No directory table\n"));
5445             else if (directory_table == NULL)
5446               printf (_("CU: %s:\n"), null_name (file_table[0].name));
5447             else
5448               {
5449                 unsigned int ix = file_table[0].directory_index;
5450                 const char *directory;
5451 
5452                 if (ix == 0 && linfo.li_version < 5)
5453                     directory = ".";
5454                 /* PR 20439 */
5455                 else if (n_directories == 0)
5456                     directory = _("<unknown>");
5457                 else
5458                     {
5459                       if (linfo.li_version < 5)
5460                         --ix;
5461                       if (ix >= n_directories)
5462                         {
5463                           warn (_("directory index %u "
5464                                     ">= number of directories %u\n"),
5465                                   ix, n_directories);
5466                           directory = _("<corrupt>");
5467                         }
5468                       else
5469                         directory = directory_table[ix];
5470                     }
5471                 if (do_wide)
5472                     printf (_("CU: %s/%s:\n"),
5473                               null_name (directory),
5474                               null_name (file_table[0].name));
5475                 else
5476                     printf ("%s:\n", null_name (file_table[0].name));
5477               }
5478 
5479             if (n_files > 0)
5480               {
5481                 if (do_wide)
5482                     printf (_("File name                            Line number    Starting address    View    Stmt\n"));
5483                 else
5484                     printf (_("File name                        Line number    Starting address    View    Stmt\n"));
5485               }
5486             else
5487               printf (_("CU: Empty file name table\n"));
5488             saved_linfo = linfo;
5489           }
5490 
5491       /* This loop iterates through the Dwarf Line Number Program.  */
5492       while (data < end_of_sequence)
5493           {
5494             unsigned char op_code;
5495             int xop;
5496             int adv;
5497             unsigned long int uladv;
5498             int is_special_opcode = 0;
5499 
5500             op_code = *data++;
5501             xop = op_code;
5502 
5503             if (op_code >= linfo.li_opcode_base)
5504               {
5505                 op_code -= linfo.li_opcode_base;
5506                 uladv = (op_code / linfo.li_line_range);
5507                 if (linfo.li_max_ops_per_insn == 1)
5508                     {
5509                       uladv *= linfo.li_min_insn_length;
5510                       state_machine_regs.address += uladv;
5511                       if (uladv)
5512                         state_machine_regs.view = 0;
5513                     }
5514                 else
5515                     {
5516                       unsigned addrdelta
5517                         = ((state_machine_regs.op_index + uladv)
5518                            / linfo.li_max_ops_per_insn)
5519                         * linfo.li_min_insn_length;
5520                       state_machine_regs.address
5521                         += addrdelta;
5522                       state_machine_regs.op_index
5523                         = (state_machine_regs.op_index + uladv)
5524                         % linfo.li_max_ops_per_insn;
5525                       if (addrdelta)
5526                         state_machine_regs.view = 0;
5527                     }
5528 
5529                 adv = (op_code % linfo.li_line_range) + linfo.li_line_base;
5530                 state_machine_regs.line += adv;
5531                 is_special_opcode = 1;
5532                 /* Increment view after printing this row.  */
5533               }
5534             else
5535               switch (op_code)
5536                 {
5537                 case DW_LNS_extended_op:
5538                     {
5539                       unsigned int ext_op_code_len;
5540                       unsigned char ext_op_code;
5541                       unsigned char *op_code_end;
5542                       unsigned char *op_code_data = data;
5543 
5544                       READ_ULEB (ext_op_code_len, op_code_data, end_of_sequence);
5545                       op_code_end = op_code_data + ext_op_code_len;
5546                       if (ext_op_code_len == 0 || op_code_end > end_of_sequence)
5547                         {
5548                           warn (_("Badly formed extended line op encountered!\n"));
5549                           break;
5550                         }
5551                       ext_op_code = *op_code_data++;
5552                       xop = ext_op_code;
5553                       xop = -xop;
5554 
5555                       switch (ext_op_code)
5556                         {
5557                         case DW_LNE_end_sequence:
5558                           /* Reset stuff after printing this row.  */
5559                           break;
5560                         case DW_LNE_set_address:
5561                           SAFE_BYTE_GET_AND_INC (state_machine_regs.address,
5562                                                        op_code_data,
5563                                                        op_code_end - op_code_data,
5564                                                        op_code_end);
5565                           state_machine_regs.op_index = 0;
5566                           state_machine_regs.view = 0;
5567                           break;
5568                         case DW_LNE_define_file:
5569                           file_table = (File_Entry *) xrealloc
5570                               (file_table, (n_files + 1) * sizeof (File_Entry));
5571 
5572                           ++state_machine_regs.last_file_entry;
5573                           /* Source file name.  */
5574                           file_table[n_files].name = (char *) op_code_data;
5575                           op_code_data += strlen ((char *) op_code_data) + 1;
5576                           /* Directory index.  */
5577                           READ_ULEB (file_table[n_files].directory_index,
5578                                          op_code_data, op_code_end);
5579                           /* Last modification time.  */
5580                           READ_ULEB (file_table[n_files].modification_date,
5581                                          op_code_data, op_code_end);
5582                           /* File length.  */
5583                           READ_ULEB (file_table[n_files].length,
5584                                          op_code_data, op_code_end);
5585                           n_files++;
5586                           break;
5587 
5588                         case DW_LNE_set_discriminator:
5589                         case DW_LNE_HP_set_sequence:
5590                           /* Simply ignored.  */
5591                           break;
5592 
5593                         default:
5594                           printf (_("UNKNOWN (%u): length %ld\n"),
5595                                     ext_op_code, (long int) (op_code_data - data));
5596                           break;
5597                         }
5598                       data = op_code_end;
5599                       break;
5600                     }
5601                 case DW_LNS_copy:
5602                     /* Increment view after printing this row.  */
5603                     break;
5604 
5605                 case DW_LNS_advance_pc:
5606                     READ_ULEB (uladv, data, end);
5607                     if (linfo.li_max_ops_per_insn == 1)
5608                       {
5609                         uladv *= linfo.li_min_insn_length;
5610                         state_machine_regs.address += uladv;
5611                         if (uladv)
5612                           state_machine_regs.view = 0;
5613                       }
5614                     else
5615                       {
5616                         unsigned addrdelta
5617                           = ((state_machine_regs.op_index + uladv)
5618                                / linfo.li_max_ops_per_insn)
5619                           * linfo.li_min_insn_length;
5620                         state_machine_regs.address
5621                           += addrdelta;
5622                         state_machine_regs.op_index
5623                           = (state_machine_regs.op_index + uladv)
5624                           % linfo.li_max_ops_per_insn;
5625                         if (addrdelta)
5626                           state_machine_regs.view = 0;
5627                       }
5628                     break;
5629 
5630                 case DW_LNS_advance_line:
5631                     READ_SLEB (adv, data, end);
5632                     state_machine_regs.line += adv;
5633                     break;
5634 
5635                 case DW_LNS_set_file:
5636                     READ_ULEB (uladv, data, end);
5637                     state_machine_regs.file = uladv;
5638 
5639                     unsigned file = state_machine_regs.file;
5640                     if (linfo.li_version < 5)
5641                       --file;
5642 
5643                     if (file_table == NULL || n_files == 0)
5644                       printf (_("\n [Use file table entry %d]\n"), file);
5645                     /* PR 20439 */
5646                     else if (file >= n_files)
5647                       {
5648                         warn (_("file index %u >= number of files %u\n"),
5649                                 file, n_files);
5650                         printf (_("\n <over large file table index %u>"), file);
5651                       }
5652                     else
5653                       {
5654                         unsigned dir = file_table[file].directory_index;
5655                         if (dir == 0 && linfo.li_version < 5)
5656                           /* If directory index is 0, that means compilation
5657                                current directory.  bfd/dwarf2.c shows
5658                                DW_AT_comp_dir here but in keeping with the
5659                                readelf practice of minimal interpretation of
5660                                file data, we show "./".  */
5661                           printf ("\n./%s:[++]\n",
5662                                     null_name (file_table[file].name));
5663                         else if (directory_table == NULL || n_directories == 0)
5664                           printf (_("\n [Use file %s "
5665                                         "in directory table entry %d]\n"),
5666                                     null_name (file_table[file].name), dir);
5667                         else
5668                           {
5669                               if (linfo.li_version < 5)
5670                                 --dir;
5671                               /* PR 20439 */
5672                               if (dir >= n_directories)
5673                                 {
5674                                   warn (_("directory index %u "
5675                                             ">= number of directories %u\n"),
5676                                           dir, n_directories);
5677                                   printf (_("\n <over large directory table entry "
5678                                               "%u>\n"), dir);
5679                                 }
5680                               else
5681                                 printf ("\n%s/%s:\n",
5682                                           null_name (directory_table[dir]),
5683                                           null_name (file_table[file].name));
5684                           }
5685                       }
5686                     break;
5687 
5688                 case DW_LNS_set_column:
5689                     READ_ULEB (uladv, data, end);
5690                     state_machine_regs.column = uladv;
5691                     break;
5692 
5693                 case DW_LNS_negate_stmt:
5694                     adv = state_machine_regs.is_stmt;
5695                     adv = ! adv;
5696                     state_machine_regs.is_stmt = adv;
5697                     break;
5698 
5699                 case DW_LNS_set_basic_block:
5700                     state_machine_regs.basic_block = 1;
5701                     break;
5702 
5703                 case DW_LNS_const_add_pc:
5704                     uladv = ((255 - linfo.li_opcode_base) / linfo.li_line_range);
5705                     if (linfo.li_max_ops_per_insn == 1)
5706                       {
5707                         uladv *= linfo.li_min_insn_length;
5708                         state_machine_regs.address += uladv;
5709                         if (uladv)
5710                           state_machine_regs.view = 0;
5711                       }
5712                     else
5713                       {
5714                         unsigned addrdelta
5715                           = ((state_machine_regs.op_index + uladv)
5716                                / linfo.li_max_ops_per_insn)
5717                           * linfo.li_min_insn_length;
5718                         state_machine_regs.address
5719                           += addrdelta;
5720                         state_machine_regs.op_index
5721                           = (state_machine_regs.op_index + uladv)
5722                           % linfo.li_max_ops_per_insn;
5723                         if (addrdelta)
5724                           state_machine_regs.view = 0;
5725                       }
5726                     break;
5727 
5728                 case DW_LNS_fixed_advance_pc:
5729                     SAFE_BYTE_GET_AND_INC (uladv, data, 2, end);
5730                     state_machine_regs.address += uladv;
5731                     state_machine_regs.op_index = 0;
5732                     /* Do NOT reset view.  */
5733                     break;
5734 
5735                 case DW_LNS_set_prologue_end:
5736                     break;
5737 
5738                 case DW_LNS_set_epilogue_begin:
5739                     break;
5740 
5741                 case DW_LNS_set_isa:
5742                     READ_ULEB (uladv, data, end);
5743                     printf (_("  Set ISA to %lu\n"), uladv);
5744                     break;
5745 
5746                 default:
5747                     printf (_("  Unknown opcode %d with operands: "), op_code);
5748 
5749                     if (standard_opcodes != NULL)
5750                       for (i = standard_opcodes[op_code - 1]; i > 0 ; --i)
5751                         {
5752                           uint64_t val;
5753 
5754                           READ_ULEB (val, data, end);
5755                           printf ("%#" PRIx64 "%s", val, i == 1 ? "" : ", ");
5756                         }
5757                     putchar ('\n');
5758                     break;
5759                 }
5760 
5761             /* Only Special opcodes, DW_LNS_copy and DW_LNE_end_sequence adds a row
5762                to the DWARF address/line matrix.  */
5763             if ((is_special_opcode) || (xop == -DW_LNE_end_sequence)
5764                 || (xop == DW_LNS_copy))
5765               {
5766                 const unsigned int MAX_FILENAME_LENGTH = 35;
5767                 char *fileName = NULL;
5768                 char *newFileName = NULL;
5769                 size_t fileNameLength;
5770 
5771                 if (file_table)
5772                     {
5773                       unsigned indx = state_machine_regs.file;
5774 
5775                       if (linfo.li_version < 5)
5776                         --indx;
5777                       /* PR 20439  */
5778                       if (indx >= n_files)
5779                         {
5780                           warn (_("file index %u >= number of files %u\n"),
5781                                   indx, n_files);
5782                           fileName = _("<corrupt>");
5783                         }
5784                       else
5785                         fileName = (char *) file_table[indx].name;
5786                     }
5787                 if (!fileName)
5788                     fileName = _("<unknown>");
5789 
5790                 fileNameLength = strlen (fileName);
5791                 newFileName = fileName;
5792                 if (fileNameLength > MAX_FILENAME_LENGTH && !do_wide)
5793                     {
5794                       newFileName = (char *) xmalloc (MAX_FILENAME_LENGTH + 1);
5795                       /* Truncate file name */
5796                       memcpy (newFileName,
5797                                 fileName + fileNameLength - MAX_FILENAME_LENGTH,
5798                                 MAX_FILENAME_LENGTH);
5799                       newFileName[MAX_FILENAME_LENGTH] = 0;
5800                     }
5801 
5802                 /* A row with end_seq set to true has a meaningful address, but
5803                      the other information in the same row is not significant.
5804                      In such a row, print line as "-", and don't print
5805                      view/is_stmt.  */
5806                 if (!do_wide || fileNameLength <= MAX_FILENAME_LENGTH)
5807                     {
5808                       if (linfo.li_max_ops_per_insn == 1)
5809                         {
5810                           if (xop == -DW_LNE_end_sequence)
5811                               printf ("%-31s  %11s  %#18" PRIx64,
5812                                         newFileName, "-",
5813                                         state_machine_regs.address);
5814                           else
5815                               printf ("%-31s  %11d  %#18" PRIx64,
5816                                         newFileName, state_machine_regs.line,
5817                                         state_machine_regs.address);
5818                         }
5819                       else
5820                         {
5821                           if (xop == -DW_LNE_end_sequence)
5822                               printf ("%-31s  %11s  %#18" PRIx64 "[%d]",
5823                                         newFileName, "-",
5824                                         state_machine_regs.address,
5825                                         state_machine_regs.op_index);
5826                           else
5827                               printf ("%-31s  %11d  %#18" PRIx64 "[%d]",
5828                                         newFileName, state_machine_regs.line,
5829                                         state_machine_regs.address,
5830                                         state_machine_regs.op_index);
5831                         }
5832                     }
5833                 else
5834                     {
5835                       if (linfo.li_max_ops_per_insn == 1)
5836                         {
5837                           if (xop == -DW_LNE_end_sequence)
5838                               printf ("%s  %11s  %#18" PRIx64,
5839                                         newFileName, "-",
5840                                         state_machine_regs.address);
5841                           else
5842                               printf ("%s  %11d  %#18" PRIx64,
5843                                         newFileName, state_machine_regs.line,
5844                                         state_machine_regs.address);
5845                         }
5846                       else
5847                         {
5848                           if (xop == -DW_LNE_end_sequence)
5849                               printf ("%s  %11s  %#18" PRIx64 "[%d]",
5850                                         newFileName, "-",
5851                                         state_machine_regs.address,
5852                                         state_machine_regs.op_index);
5853                           else
5854                               printf ("%s  %11d  %#18" PRIx64 "[%d]",
5855                                         newFileName, state_machine_regs.line,
5856                                         state_machine_regs.address,
5857                                         state_machine_regs.op_index);
5858                         }
5859                     }
5860 
5861                 if (xop != -DW_LNE_end_sequence)
5862                     {
5863                       if (state_machine_regs.view)
5864                         printf ("  %6u", state_machine_regs.view);
5865                       else
5866                         printf ("        ");
5867 
5868                       if (state_machine_regs.is_stmt)
5869                         printf ("       x");
5870                     }
5871 
5872                 putchar ('\n');
5873                 state_machine_regs.view++;
5874 
5875                 if (xop == -DW_LNE_end_sequence)
5876                     {
5877                       reset_state_machine (linfo.li_default_is_stmt);
5878                       putchar ('\n');
5879                     }
5880 
5881                 if (newFileName != fileName)
5882                     free (newFileName);
5883               }
5884           }
5885 
5886       if (file_table)
5887           {
5888             free (file_table);
5889             file_table = NULL;
5890             n_files = 0;
5891           }
5892 
5893       if (directory_table)
5894           {
5895             free (directory_table);
5896             directory_table = NULL;
5897             n_directories = 0;
5898           }
5899 
5900       putchar ('\n');
5901     }
5902 
5903   return 1;
5904 }
5905 
5906 static int
display_debug_lines(struct dwarf_section * section,void * file)5907 display_debug_lines (struct dwarf_section *section, void *file)
5908 {
5909   unsigned char *data = section->start;
5910   unsigned char *end = data + section->size;
5911   int retValRaw = 1;
5912   int retValDecoded = 1;
5913 
5914   if (do_debug_lines == 0)
5915     do_debug_lines |= FLAG_DEBUG_LINES_RAW;
5916 
5917   if (do_debug_lines & FLAG_DEBUG_LINES_RAW)
5918     retValRaw = display_debug_lines_raw (section, data, end, file);
5919 
5920   if (do_debug_lines & FLAG_DEBUG_LINES_DECODED)
5921     retValDecoded = display_debug_lines_decoded (section, data, data, end, file);
5922 
5923   if (!retValRaw || !retValDecoded)
5924     return 0;
5925 
5926   return 1;
5927 }
5928 
5929 static debug_info *
find_debug_info_for_offset(uint64_t offset)5930 find_debug_info_for_offset (uint64_t offset)
5931 {
5932   unsigned int i;
5933 
5934   if (num_debug_info_entries == DEBUG_INFO_UNAVAILABLE)
5935     return NULL;
5936 
5937   for (i = 0; i < num_debug_info_entries; i++)
5938     if (debug_information[i].cu_offset == offset)
5939       return debug_information + i;
5940 
5941   return NULL;
5942 }
5943 
5944 static const char *
get_gdb_index_symbol_kind_name(gdb_index_symbol_kind kind)5945 get_gdb_index_symbol_kind_name (gdb_index_symbol_kind kind)
5946 {
5947   /* See gdb/gdb-index.h.  */
5948   static const char * const kinds[] =
5949   {
5950     N_ ("no info"),
5951     N_ ("type"),
5952     N_ ("variable"),
5953     N_ ("function"),
5954     N_ ("other"),
5955     N_ ("unused5"),
5956     N_ ("unused6"),
5957     N_ ("unused7")
5958   };
5959 
5960   return _ (kinds[kind]);
5961 }
5962 
5963 static int
display_debug_pubnames_worker(struct dwarf_section * section,void * file ATTRIBUTE_UNUSED,int is_gnu)5964 display_debug_pubnames_worker (struct dwarf_section *section,
5965                                      void *file ATTRIBUTE_UNUSED,
5966                                      int is_gnu)
5967 {
5968   DWARF2_Internal_PubNames names;
5969   unsigned char *start = section->start;
5970   unsigned char *end = start + section->size;
5971 
5972   /* It does not matter if this load fails,
5973      we test for that later on.  */
5974   load_debug_info (file);
5975 
5976   introduce (section, false);
5977 
5978   while (start < end)
5979     {
5980       unsigned char *data;
5981       unsigned long sec_off = start - section->start;
5982       unsigned int offset_size;
5983 
5984       SAFE_BYTE_GET_AND_INC (names.pn_length, start, 4, end);
5985       if (names.pn_length == 0xffffffff)
5986           {
5987             SAFE_BYTE_GET_AND_INC (names.pn_length, start, 8, end);
5988             offset_size = 8;
5989           }
5990       else
5991           offset_size = 4;
5992 
5993       if (names.pn_length > (size_t) (end - start))
5994           {
5995             warn (_("Debug info is corrupted, "
5996                       "%s header at %#lx has length %#" PRIx64 "\n"),
5997                     section->name, sec_off, names.pn_length);
5998             break;
5999           }
6000 
6001       data = start;
6002       start += names.pn_length;
6003 
6004       SAFE_BYTE_GET_AND_INC (names.pn_version, data, 2, start);
6005       SAFE_BYTE_GET_AND_INC (names.pn_offset, data, offset_size, start);
6006 
6007       if (num_debug_info_entries != DEBUG_INFO_UNAVAILABLE
6008             && num_debug_info_entries > 0
6009             && find_debug_info_for_offset (names.pn_offset) == NULL)
6010           warn (_(".debug_info offset of %#" PRIx64
6011                     " in %s section does not point to a CU header.\n"),
6012                 names.pn_offset, section->name);
6013 
6014       SAFE_BYTE_GET_AND_INC (names.pn_size, data, offset_size, start);
6015 
6016       printf (_("  Length:                              %" PRId64 "\n"),
6017                 names.pn_length);
6018       printf (_("  Version:                             %d\n"),
6019                 names.pn_version);
6020       printf (_("  Offset into .debug_info section:     %#" PRIx64 "\n"),
6021                 names.pn_offset);
6022       printf (_("  Size of area in .debug_info section: %" PRId64 "\n"),
6023                 names.pn_size);
6024 
6025       if (names.pn_version != 2 && names.pn_version != 3)
6026           {
6027             static int warned = 0;
6028 
6029             if (! warned)
6030               {
6031                 warn (_("Only DWARF 2 and 3 pubnames are currently supported\n"));
6032                 warned = 1;
6033               }
6034 
6035             continue;
6036           }
6037 
6038       if (is_gnu)
6039           printf (_("\n    Offset  Kind          Name\n"));
6040       else
6041           printf (_("\n    Offset\tName\n"));
6042 
6043       while (1)
6044           {
6045             size_t maxprint;
6046             uint64_t offset;
6047 
6048             SAFE_BYTE_GET_AND_INC (offset, data, offset_size, start);
6049 
6050             if (offset == 0)
6051               break;
6052 
6053             if (data >= start)
6054               break;
6055             maxprint = (start - data) - 1;
6056 
6057             if (is_gnu)
6058               {
6059                 unsigned int kind_data;
6060                 gdb_index_symbol_kind kind;
6061                 const char *kind_name;
6062                 int is_static;
6063 
6064                 SAFE_BYTE_GET_AND_INC (kind_data, data, 1, start);
6065                 maxprint --;
6066                 /* GCC computes the kind as the upper byte in the CU index
6067                      word, and then right shifts it by the CU index size.
6068                      Left shift KIND to where the gdb-index.h accessor macros
6069                      can use it.  */
6070                 kind_data <<= GDB_INDEX_CU_BITSIZE;
6071                 kind = GDB_INDEX_SYMBOL_KIND_VALUE (kind_data);
6072                 kind_name = get_gdb_index_symbol_kind_name (kind);
6073                 is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (kind_data);
6074                 printf ("    %-6" PRIx64 "  %s,%-10s  %.*s\n",
6075                           offset, is_static ? _("s") : _("g"),
6076                           kind_name, (int) maxprint, data);
6077               }
6078             else
6079               printf ("    %-6" PRIx64 "\t%.*s\n",
6080                         offset, (int) maxprint, data);
6081 
6082             data += strnlen ((char *) data, maxprint);
6083             if (data < start)
6084               data++;
6085             if (data >= start)
6086               break;
6087           }
6088     }
6089 
6090   printf ("\n");
6091   return 1;
6092 }
6093 
6094 static int
display_debug_pubnames(struct dwarf_section * section,void * file)6095 display_debug_pubnames (struct dwarf_section *section, void *file)
6096 {
6097   return display_debug_pubnames_worker (section, file, 0);
6098 }
6099 
6100 static int
display_debug_gnu_pubnames(struct dwarf_section * section,void * file)6101 display_debug_gnu_pubnames (struct dwarf_section *section, void *file)
6102 {
6103   return display_debug_pubnames_worker (section, file, 1);
6104 }
6105 
6106 static int
display_debug_macinfo(struct dwarf_section * section,void * file ATTRIBUTE_UNUSED)6107 display_debug_macinfo (struct dwarf_section *section,
6108                            void *file ATTRIBUTE_UNUSED)
6109 {
6110   unsigned char *start = section->start;
6111   unsigned char *end = start + section->size;
6112   unsigned char *curr = start;
6113   enum dwarf_macinfo_record_type op;
6114 
6115   introduce (section, false);
6116 
6117   while (curr < end)
6118     {
6119       unsigned int lineno;
6120       const unsigned char *string;
6121 
6122       op = (enum dwarf_macinfo_record_type) *curr;
6123       curr++;
6124 
6125       switch (op)
6126           {
6127           case DW_MACINFO_start_file:
6128             {
6129               unsigned int filenum;
6130 
6131               READ_ULEB (lineno, curr, end);
6132               READ_ULEB (filenum, curr, end);
6133               printf (_(" DW_MACINFO_start_file - lineno: %d filenum: %d\n"),
6134                         lineno, filenum);
6135             }
6136             break;
6137 
6138           case DW_MACINFO_end_file:
6139             printf (_(" DW_MACINFO_end_file\n"));
6140             break;
6141 
6142           case DW_MACINFO_define:
6143             READ_ULEB (lineno, curr, end);
6144             string = curr;
6145             curr += strnlen ((char *) string, end - string);
6146             printf (_(" DW_MACINFO_define - lineno : %d macro : %*s\n"),
6147                       lineno, (int) (curr - string), string);
6148             if (curr < end)
6149               curr++;
6150             break;
6151 
6152           case DW_MACINFO_undef:
6153             READ_ULEB (lineno, curr, end);
6154             string = curr;
6155             curr += strnlen ((char *) string, end - string);
6156             printf (_(" DW_MACINFO_undef - lineno : %d macro : %*s\n"),
6157                       lineno, (int) (curr - string), string);
6158             if (curr < end)
6159               curr++;
6160             break;
6161 
6162           case DW_MACINFO_vendor_ext:
6163             {
6164               unsigned int constant;
6165 
6166               READ_ULEB (constant, curr, end);
6167               string = curr;
6168               curr += strnlen ((char *) string, end - string);
6169               printf (_(" DW_MACINFO_vendor_ext - constant : %d string : %*s\n"),
6170                         constant, (int) (curr - string), string);
6171               if (curr < end)
6172                 curr++;
6173             }
6174             break;
6175           }
6176     }
6177 
6178   return 1;
6179 }
6180 
6181 /* Given LINE_OFFSET into the .debug_line section, attempt to return
6182    filename and dirname corresponding to file name table entry with index
6183    FILEIDX.  Return NULL on failure.  */
6184 
6185 static unsigned char *
get_line_filename_and_dirname(uint64_t line_offset,uint64_t fileidx,unsigned char ** dir_name)6186 get_line_filename_and_dirname (uint64_t line_offset,
6187                                      uint64_t fileidx,
6188                                      unsigned char **dir_name)
6189 {
6190   struct dwarf_section *section = &debug_displays [line].section;
6191   unsigned char *hdrptr, *dirtable, *file_name;
6192   unsigned int offset_size;
6193   unsigned int version, opcode_base;
6194   uint64_t length, diridx;
6195   const unsigned char * end;
6196 
6197   *dir_name = NULL;
6198   if (section->start == NULL
6199       || line_offset >= section->size
6200       || fileidx == 0)
6201     return NULL;
6202 
6203   hdrptr = section->start + line_offset;
6204   end = section->start + section->size;
6205 
6206   SAFE_BYTE_GET_AND_INC (length, hdrptr, 4, end);
6207   if (length == 0xffffffff)
6208     {
6209       /* This section is 64-bit DWARF 3.  */
6210       SAFE_BYTE_GET_AND_INC (length, hdrptr, 8, end);
6211       offset_size = 8;
6212     }
6213   else
6214     offset_size = 4;
6215 
6216   if (length > (size_t) (end - hdrptr)
6217       || length < 2 + offset_size + 1 + 3 + 1)
6218     return NULL;
6219   end = hdrptr + length;
6220 
6221   SAFE_BYTE_GET_AND_INC (version, hdrptr, 2, end);
6222   if (version != 2 && version != 3 && version != 4)
6223     return NULL;
6224   hdrptr += offset_size + 1;/* Skip prologue_length and min_insn_length.  */
6225   if (version >= 4)
6226     hdrptr++;                     /* Skip max_ops_per_insn.  */
6227   hdrptr += 3;                    /* Skip default_is_stmt, line_base, line_range.  */
6228 
6229   SAFE_BYTE_GET_AND_INC (opcode_base, hdrptr, 1, end);
6230   if (opcode_base == 0
6231       || opcode_base - 1 >= (size_t) (end - hdrptr))
6232     return NULL;
6233 
6234   hdrptr += opcode_base - 1;
6235 
6236   dirtable = hdrptr;
6237   /* Skip over dirname table.  */
6238   while (*hdrptr != '\0')
6239     {
6240       hdrptr += strnlen ((char *) hdrptr, end - hdrptr);
6241       if (hdrptr < end)
6242           hdrptr++;
6243       if (hdrptr >= end)
6244           return NULL;
6245     }
6246   hdrptr++;                       /* Skip the NUL at the end of the table.  */
6247 
6248   /* Now skip over preceding filename table entries.  */
6249   for (; hdrptr < end && *hdrptr != '\0' && fileidx > 1; fileidx--)
6250     {
6251       hdrptr += strnlen ((char *) hdrptr, end - hdrptr);
6252       if (hdrptr < end)
6253           hdrptr++;
6254       SKIP_ULEB (hdrptr, end);
6255       SKIP_ULEB (hdrptr, end);
6256       SKIP_ULEB (hdrptr, end);
6257     }
6258   if (hdrptr >= end || *hdrptr == '\0')
6259     return NULL;
6260 
6261   file_name = hdrptr;
6262   hdrptr += strnlen ((char *) hdrptr, end - hdrptr);
6263   if (hdrptr < end)
6264     hdrptr++;
6265   if (hdrptr >= end)
6266     return NULL;
6267   READ_ULEB (diridx, hdrptr, end);
6268   if (diridx == 0)
6269     return file_name;
6270   for (; dirtable < end && *dirtable != '\0' && diridx > 1; diridx--)
6271     {
6272       dirtable += strnlen ((char *) dirtable, end - dirtable);
6273       if (dirtable < end)
6274           dirtable++;
6275     }
6276   if (dirtable >= end || *dirtable == '\0')
6277     return NULL;
6278   *dir_name = dirtable;
6279   return file_name;
6280 }
6281 
6282 static int
display_debug_macro(struct dwarf_section * section,void * file)6283 display_debug_macro (struct dwarf_section *section,
6284                          void *file)
6285 {
6286   unsigned char *start = section->start;
6287   unsigned char *end = start + section->size;
6288   unsigned char *curr = start;
6289   unsigned char *extended_op_buf[256];
6290   bool is_dwo = false;
6291   const char *suffix = strrchr (section->name, '.');
6292 
6293   if (suffix && strcmp (suffix, ".dwo") == 0)
6294     is_dwo = true;
6295 
6296   load_debug_section_with_follow (str, file);
6297   load_debug_section_with_follow (line, file);
6298   load_debug_section_with_follow (str_index, file);
6299 
6300   introduce (section, false);
6301 
6302   while (curr < end)
6303     {
6304       unsigned int lineno, version, flags;
6305       unsigned int offset_size;
6306       const unsigned char *string;
6307       uint64_t line_offset = 0, sec_offset = curr - start, offset;
6308       unsigned char **extended_ops = NULL;
6309 
6310       SAFE_BYTE_GET_AND_INC (version, curr, 2, end);
6311       if (version != 4 && version != 5)
6312           {
6313             error (_("Expected to find a version number of 4 or 5 in section %s but found %d instead\n"),
6314                      section->name, version);
6315             return 0;
6316           }
6317 
6318       SAFE_BYTE_GET_AND_INC (flags, curr, 1, end);
6319       offset_size = (flags & 1) ? 8 : 4;
6320       printf (_("  Offset:                      %#" PRIx64 "\n"), sec_offset);
6321       printf (_("  Version:                     %d\n"), version);
6322       printf (_("  Offset size:                 %d\n"), offset_size);
6323       if (flags & 2)
6324           {
6325             SAFE_BYTE_GET_AND_INC (line_offset, curr, offset_size, end);
6326             printf (_("  Offset into .debug_line:     %#" PRIx64 "\n"),
6327                       line_offset);
6328           }
6329       if (flags & 4)
6330           {
6331             unsigned int i, count, op;
6332             uint64_t nargs, n;
6333 
6334             SAFE_BYTE_GET_AND_INC (count, curr, 1, end);
6335 
6336             memset (extended_op_buf, 0, sizeof (extended_op_buf));
6337             extended_ops = extended_op_buf;
6338             if (count)
6339               {
6340                 printf (_("  Extension opcode arguments:\n"));
6341                 for (i = 0; i < count; i++)
6342                     {
6343                       SAFE_BYTE_GET_AND_INC (op, curr, 1, end);
6344                       extended_ops[op] = curr;
6345                       READ_ULEB (nargs, curr, end);
6346                       if (nargs == 0)
6347                         printf (_("    DW_MACRO_%02x has no arguments\n"), op);
6348                       else
6349                         {
6350                           printf (_("    DW_MACRO_%02x arguments: "), op);
6351                           for (n = 0; n < nargs; n++)
6352                               {
6353                                 unsigned int form;
6354 
6355                                 SAFE_BYTE_GET_AND_INC (form, curr, 1, end);
6356                                 printf ("%s%s", get_FORM_name (form),
6357                                           n == nargs - 1 ? "\n" : ", ");
6358                                 switch (form)
6359                                   {
6360                                   case DW_FORM_data1:
6361                                   case DW_FORM_data2:
6362                                   case DW_FORM_data4:
6363                                   case DW_FORM_data8:
6364                                   case DW_FORM_sdata:
6365                                   case DW_FORM_udata:
6366                                   case DW_FORM_block:
6367                                   case DW_FORM_block1:
6368                                   case DW_FORM_block2:
6369                                   case DW_FORM_block4:
6370                                   case DW_FORM_flag:
6371                                   case DW_FORM_string:
6372                                   case DW_FORM_strp:
6373                                   case DW_FORM_sec_offset:
6374                                     break;
6375                                   default:
6376                                     error (_("Invalid extension opcode form %s\n"),
6377                                              get_FORM_name (form));
6378                                     return 0;
6379                                   }
6380                               }
6381                         }
6382                     }
6383               }
6384           }
6385       printf ("\n");
6386 
6387       while (1)
6388           {
6389             unsigned int op;
6390 
6391             if (curr >= end)
6392               {
6393                 error (_(".debug_macro section not zero terminated\n"));
6394                 return 0;
6395               }
6396 
6397             SAFE_BYTE_GET_AND_INC (op, curr, 1, end);
6398             if (op == 0)
6399               break;
6400 
6401             switch (op)
6402               {
6403               case DW_MACRO_define:
6404                 READ_ULEB (lineno, curr, end);
6405                 string = curr;
6406                 curr += strnlen ((char *) string, end - string);
6407                 printf (_(" DW_MACRO_define - lineno : %d macro : %*s\n"),
6408                           lineno, (int) (curr - string), string);
6409                 if (curr < end)
6410                     curr++;
6411                 break;
6412 
6413               case DW_MACRO_undef:
6414                 READ_ULEB (lineno, curr, end);
6415                 string = curr;
6416                 curr += strnlen ((char *) string, end - string);
6417                 printf (_(" DW_MACRO_undef - lineno : %d macro : %*s\n"),
6418                           lineno, (int) (curr - string), string);
6419                 if (curr < end)
6420                     curr++;
6421                 break;
6422 
6423               case DW_MACRO_start_file:
6424                 {
6425                     unsigned int filenum;
6426                     unsigned char *file_name = NULL, *dir_name = NULL;
6427 
6428                     READ_ULEB (lineno, curr, end);
6429                     READ_ULEB (filenum, curr, end);
6430 
6431                     if ((flags & 2) == 0)
6432                       error (_("DW_MACRO_start_file used, but no .debug_line offset provided.\n"));
6433                     else
6434                       file_name
6435                         = get_line_filename_and_dirname (line_offset, filenum,
6436                                                                  &dir_name);
6437                     if (file_name == NULL)
6438                       printf (_(" DW_MACRO_start_file - lineno: %d filenum: %d\n"),
6439                                 lineno, filenum);
6440                     else
6441                       printf (_(" DW_MACRO_start_file - lineno: %d filenum: %d filename: %s%s%s\n"),
6442                                 lineno, filenum,
6443                                 dir_name != NULL ? (const char *) dir_name : "",
6444                                 dir_name != NULL ? "/" : "", file_name);
6445                 }
6446                 break;
6447 
6448               case DW_MACRO_end_file:
6449                 printf (_(" DW_MACRO_end_file\n"));
6450                 break;
6451 
6452               case DW_MACRO_define_strp:
6453                 READ_ULEB (lineno, curr, end);
6454                 if (version == 4 && is_dwo)
6455                     READ_ULEB (offset, curr, end);
6456                 else
6457                     SAFE_BYTE_GET_AND_INC (offset, curr, offset_size, end);
6458                 string = fetch_indirect_string (offset);
6459                 printf (_(" DW_MACRO_define_strp - lineno : %d macro : %s\n"),
6460                           lineno, string);
6461                 break;
6462 
6463               case DW_MACRO_undef_strp:
6464                 READ_ULEB (lineno, curr, end);
6465                 if (version == 4 && is_dwo)
6466                     READ_ULEB (offset, curr, end);
6467                 else
6468                     SAFE_BYTE_GET_AND_INC (offset, curr, offset_size, end);
6469                 string = fetch_indirect_string (offset);
6470                 printf (_(" DW_MACRO_undef_strp - lineno : %d macro : %s\n"),
6471                           lineno, string);
6472                 break;
6473 
6474               case DW_MACRO_import:
6475                 SAFE_BYTE_GET_AND_INC (offset, curr, offset_size, end);
6476                 printf (_(" DW_MACRO_import - offset : %#" PRIx64 "\n"),
6477                           offset);
6478                 break;
6479 
6480               case DW_MACRO_define_sup:
6481                 READ_ULEB (lineno, curr, end);
6482                 SAFE_BYTE_GET_AND_INC (offset, curr, offset_size, end);
6483                 printf (_(" DW_MACRO_define_sup - lineno : %d"
6484                               " macro offset : %#" PRIx64 "\n"),
6485                           lineno, offset);
6486                 break;
6487 
6488               case DW_MACRO_undef_sup:
6489                 READ_ULEB (lineno, curr, end);
6490                 SAFE_BYTE_GET_AND_INC (offset, curr, offset_size, end);
6491                 printf (_(" DW_MACRO_undef_sup - lineno : %d"
6492                               " macro offset : %#" PRIx64 "\n"),
6493                           lineno, offset);
6494                 break;
6495 
6496               case DW_MACRO_import_sup:
6497                 SAFE_BYTE_GET_AND_INC (offset, curr, offset_size, end);
6498                 printf (_(" DW_MACRO_import_sup - offset : %#" PRIx64 "\n"),
6499                           offset);
6500                 break;
6501 
6502               case DW_MACRO_define_strx:
6503               case DW_MACRO_undef_strx:
6504                 READ_ULEB (lineno, curr, end);
6505                 READ_ULEB (offset, curr, end);
6506                 string = (const unsigned char *)
6507                     fetch_indexed_string (offset, NULL, offset_size, false, 0);
6508                 if (op == DW_MACRO_define_strx)
6509                     printf (" DW_MACRO_define_strx ");
6510                 else
6511                     printf (" DW_MACRO_undef_strx ");
6512                 if (do_wide)
6513                     printf (_("(with offset %#" PRIx64 ") "), offset);
6514                 printf (_("lineno : %d macro : %s\n"),
6515                           lineno, string);
6516                 break;
6517 
6518               default:
6519                 if (op >= DW_MACRO_lo_user && op <= DW_MACRO_hi_user)
6520                     {
6521                       printf (_(" <Target Specific macro op: %#x - UNHANDLED"), op);
6522                       break;
6523                     }
6524 
6525                 if (extended_ops == NULL || extended_ops[op] == NULL)
6526                     {
6527                       error (_(" Unknown macro opcode %02x seen\n"), op);
6528                       return 0;
6529                     }
6530                 else
6531                     {
6532                       /* Skip over unhandled opcodes.  */
6533                       uint64_t nargs, n;
6534                       unsigned char *desc = extended_ops[op];
6535                       READ_ULEB (nargs, desc, end);
6536                       if (nargs == 0)
6537                         {
6538                           printf (_(" DW_MACRO_%02x\n"), op);
6539                           break;
6540                         }
6541                       printf (_(" DW_MACRO_%02x -"), op);
6542                       for (n = 0; n < nargs; n++)
6543                         {
6544                           int val;
6545 
6546                           /* DW_FORM_implicit_const is not expected here.  */
6547                           SAFE_BYTE_GET_AND_INC (val, desc, 1, end);
6548                           curr
6549                               = read_and_display_attr_value (0, val, 0,
6550                                                                    start, curr, end, 0, 0,
6551                                                                    offset_size, version,
6552                                                                    NULL, 0, section,
6553                                                                    NULL, ' ', -1);
6554                           if (n != nargs - 1)
6555                               printf (",");
6556                         }
6557                       printf ("\n");
6558                     }
6559                 break;
6560               }
6561           }
6562 
6563       printf ("\n");
6564     }
6565 
6566   return 1;
6567 }
6568 
6569 static int
display_debug_abbrev(struct dwarf_section * section,void * file ATTRIBUTE_UNUSED)6570 display_debug_abbrev (struct dwarf_section *section,
6571                           void *file ATTRIBUTE_UNUSED)
6572 {
6573   abbrev_entry *entry;
6574   unsigned char *start = section->start;
6575 
6576   introduce (section, false);
6577 
6578   do
6579     {
6580       uint64_t offset = start - section->start;
6581       abbrev_list *list = find_and_process_abbrev_set (section, 0,
6582                                                                    section->size, offset,
6583                                                                    NULL);
6584       if (list == NULL)
6585           break;
6586 
6587       if (list->first_abbrev)
6588           printf (_("  Number TAG (%#" PRIx64 ")\n"), offset);
6589 
6590       for (entry = list->first_abbrev; entry; entry = entry->next)
6591           {
6592             abbrev_attr *attr;
6593 
6594             printf ("   %ld      %s    [%s]\n",
6595                       entry->number,
6596                       get_TAG_name (entry->tag),
6597                       entry->children ? _("has children") : _("no children"));
6598 
6599             for (attr = entry->first_attr; attr; attr = attr->next)
6600               {
6601                 printf ("    %-18s %s",
6602                           get_AT_name (attr->attribute),
6603                           get_FORM_name (attr->form));
6604                 if (attr->form == DW_FORM_implicit_const)
6605                     printf (": %" PRId64, attr->implicit_const);
6606                 putchar ('\n');
6607               }
6608           }
6609       start = list->start_of_next_abbrevs;
6610       free_abbrev_list (list);
6611     }
6612   while (start);
6613 
6614   printf ("\n");
6615 
6616   return 1;
6617 }
6618 
6619 /* Return true when ADDR is the maximum address, when addresses are
6620    POINTER_SIZE bytes long.  */
6621 
6622 static bool
is_max_address(uint64_t addr,unsigned int pointer_size)6623 is_max_address (uint64_t addr, unsigned int pointer_size)
6624 {
6625   uint64_t mask = ~(~(uint64_t) 0 << 1 << (pointer_size * 8 - 1));
6626   return ((addr & mask) == mask);
6627 }
6628 
6629 /* Display a view pair list starting at *VSTART_PTR and ending at
6630    VLISTEND within SECTION.  */
6631 
6632 static void
display_view_pair_list(struct dwarf_section * section,unsigned char ** vstart_ptr,unsigned int debug_info_entry,unsigned char * vlistend)6633 display_view_pair_list (struct dwarf_section *section,
6634                               unsigned char **vstart_ptr,
6635                               unsigned int debug_info_entry,
6636                               unsigned char *vlistend)
6637 {
6638   unsigned char *vstart = *vstart_ptr;
6639   unsigned char *section_end = section->start + section->size;
6640   unsigned int pointer_size = debug_information [debug_info_entry].pointer_size;
6641 
6642   if (vlistend < section_end)
6643     section_end = vlistend;
6644 
6645   putchar ('\n');
6646 
6647   while (vstart < section_end)
6648     {
6649       uint64_t off = vstart - section->start;
6650       uint64_t vbegin, vend;
6651 
6652       READ_ULEB (vbegin, vstart, section_end);
6653       if (vstart == section_end)
6654           break;
6655 
6656       READ_ULEB (vend, vstart, section_end);
6657       printf ("    %8.8" PRIx64 " ", off);
6658 
6659       print_view (vbegin, pointer_size);
6660       print_view (vend, pointer_size);
6661       printf (_("location view pair\n"));
6662     }
6663 
6664   putchar ('\n');
6665   *vstart_ptr = vstart;
6666 }
6667 
6668 /* Display a location list from a normal (ie, non-dwo) .debug_loc section.  */
6669 
6670 static void
display_loc_list(struct dwarf_section * section,unsigned char ** start_ptr,unsigned int debug_info_entry,uint64_t offset,uint64_t base_address,unsigned char ** vstart_ptr,int has_frame_base)6671 display_loc_list (struct dwarf_section *section,
6672                       unsigned char **start_ptr,
6673                       unsigned int debug_info_entry,
6674                       uint64_t offset,
6675                       uint64_t base_address,
6676                       unsigned char **vstart_ptr,
6677                       int has_frame_base)
6678 {
6679   unsigned char *start = *start_ptr, *vstart = *vstart_ptr;
6680   unsigned char *section_end = section->start + section->size;
6681   uint64_t cu_offset;
6682   unsigned int pointer_size;
6683   unsigned int offset_size;
6684   int dwarf_version;
6685   uint64_t begin;
6686   uint64_t end;
6687   unsigned short length;
6688   int need_frame_base;
6689 
6690   if (debug_info_entry >= num_debug_info_entries)
6691     {
6692       warn (_("No debug information available for loc lists of entry: %u\n"),
6693               debug_info_entry);
6694       return;
6695     }
6696 
6697   cu_offset = debug_information [debug_info_entry].cu_offset;
6698   pointer_size = debug_information [debug_info_entry].pointer_size;
6699   offset_size = debug_information [debug_info_entry].offset_size;
6700   dwarf_version = debug_information [debug_info_entry].dwarf_version;
6701 
6702   if (pointer_size < 2 || pointer_size > 8)
6703     {
6704       warn (_("Invalid pointer size (%d) in debug info for entry %d\n"),
6705               pointer_size, debug_info_entry);
6706       return;
6707     }
6708 
6709   while (1)
6710     {
6711       uint64_t off = offset + (start - *start_ptr);
6712       uint64_t vbegin = -1, vend = -1;
6713 
6714       if (2 * pointer_size > (size_t) (section_end - start))
6715           {
6716             warn (_("Location list starting at offset %#" PRIx64
6717                       " is not terminated.\n"), offset);
6718             break;
6719           }
6720 
6721       printf ("    ");
6722       print_hex (off, 4);
6723 
6724       SAFE_BYTE_GET_AND_INC (begin, start, pointer_size, section_end);
6725       SAFE_BYTE_GET_AND_INC (end, start, pointer_size, section_end);
6726 
6727       if (begin == 0 && end == 0)
6728           {
6729             /* PR 18374: In a object file we can have a location list that
6730                starts with a begin and end of 0 because there are relocations
6731                that need to be applied to the addresses.  Actually applying
6732                the relocations now does not help as they will probably resolve
6733                to 0, since the object file has not been fully linked.  Real
6734                end of list markers will not have any relocations against them.  */
6735             if (! reloc_at (section, off)
6736                 && ! reloc_at (section, off + pointer_size))
6737               {
6738                 printf (_("<End of list>\n"));
6739                 break;
6740               }
6741           }
6742 
6743       /* Check base address specifiers.  */
6744       if (is_max_address (begin, pointer_size)
6745             && !is_max_address (end, pointer_size))
6746           {
6747             base_address = end;
6748             print_hex (begin, pointer_size);
6749             print_hex (end, pointer_size);
6750             printf (_("(base address)\n"));
6751             continue;
6752           }
6753 
6754       if (vstart)
6755           {
6756             off = offset + (vstart - *start_ptr);
6757 
6758             READ_ULEB (vbegin, vstart, section_end);
6759             print_view (vbegin, pointer_size);
6760 
6761             READ_ULEB (vend, vstart, section_end);
6762             print_view (vend, pointer_size);
6763 
6764             printf (_("views at %8.8" PRIx64 " for:\n    %*s "), off, 8, "");
6765           }
6766 
6767       if (2 > (size_t) (section_end - start))
6768           {
6769             warn (_("Location list starting at offset %#" PRIx64
6770                       " is not terminated.\n"), offset);
6771             break;
6772           }
6773 
6774       SAFE_BYTE_GET_AND_INC (length, start, 2, section_end);
6775 
6776       if (length > (size_t) (section_end - start))
6777           {
6778             warn (_("Location list starting at offset %#" PRIx64
6779                       " is not terminated.\n"), offset);
6780             break;
6781           }
6782 
6783       print_hex (begin + base_address, pointer_size);
6784       print_hex (end + base_address, pointer_size);
6785 
6786       putchar ('(');
6787       need_frame_base = decode_location_expression (start,
6788                                                                 pointer_size,
6789                                                                 offset_size,
6790                                                                 dwarf_version,
6791                                                                 length,
6792                                                                 cu_offset, section);
6793       putchar (')');
6794 
6795       if (need_frame_base && !has_frame_base)
6796           printf (_(" [without DW_AT_frame_base]"));
6797 
6798       if (begin == end && vbegin == vend)
6799           fputs (_(" (start == end)"), stdout);
6800       else if (begin > end || (begin == end && vbegin > vend))
6801           fputs (_(" (start > end)"), stdout);
6802 
6803       putchar ('\n');
6804 
6805       start += length;
6806     }
6807 
6808   *start_ptr = start;
6809   *vstart_ptr = vstart;
6810 }
6811 
6812 /* Display a location list from a normal (ie, non-dwo) .debug_loclists section.  */
6813 
6814 static void
display_loclists_list(struct dwarf_section * section,unsigned char ** start_ptr,debug_info * debug_info_p,uint64_t offset,uint64_t base_address,unsigned char ** vstart_ptr,int has_frame_base)6815 display_loclists_list (struct dwarf_section *  section,
6816                            unsigned char **        start_ptr,
6817                            debug_info *            debug_info_p,
6818                            uint64_t                offset,
6819                            uint64_t                base_address,
6820                            unsigned char **        vstart_ptr,
6821                            int                     has_frame_base)
6822 {
6823   unsigned char *start = *start_ptr;
6824   unsigned char *vstart = *vstart_ptr;
6825   unsigned char *section_end = section->start + section->size;
6826   uint64_t cu_offset;
6827   unsigned int pointer_size;
6828   unsigned int offset_size;
6829   unsigned int dwarf_version;
6830   uint64_t idx;
6831 
6832   /* Initialize it due to a false compiler warning.  */
6833   uint64_t begin = -1, vbegin = -1;
6834   uint64_t end = -1, vend = -1;
6835   uint64_t length;
6836   int need_frame_base;
6837 
6838   cu_offset = debug_info_p->cu_offset;
6839   pointer_size = debug_info_p->pointer_size;
6840   offset_size = debug_info_p->offset_size;
6841   dwarf_version = debug_info_p->dwarf_version;
6842 
6843   if (pointer_size < 2 || pointer_size > 8)
6844     {
6845       warn (_("Invalid pointer size (%d) in debug info for entry %d\n"),
6846               pointer_size, (int)(debug_info_p - debug_information));
6847       return;
6848     }
6849 
6850   while (1)
6851     {
6852       uint64_t off = offset + (start - *start_ptr);
6853       enum dwarf_location_list_entry_type llet;
6854 
6855       if (start + 1 > section_end)
6856           {
6857             warn (_("Location list starting at offset %#" PRIx64
6858                       " is not terminated.\n"), offset);
6859             break;
6860           }
6861 
6862       printf ("    ");
6863       print_hex (off, 4);
6864 
6865       SAFE_BYTE_GET_AND_INC (llet, start, 1, section_end);
6866 
6867       if (vstart && (llet == DW_LLE_offset_pair
6868                          || llet == DW_LLE_start_end
6869                          || llet == DW_LLE_start_length))
6870           {
6871             off = offset + (vstart - *start_ptr);
6872 
6873             READ_ULEB (vbegin, vstart, section_end);
6874             print_view (vbegin, pointer_size);
6875 
6876             READ_ULEB (vend, vstart, section_end);
6877             print_view (vend, pointer_size);
6878 
6879             printf (_("views at %8.8" PRIx64 " for:\n    %*s "), off, 8, "");
6880           }
6881 
6882       switch (llet)
6883           {
6884           case DW_LLE_end_of_list:
6885             printf (_("<End of list>\n"));
6886             break;
6887 
6888           case DW_LLE_base_addressx:
6889             READ_ULEB (idx, start, section_end);
6890             print_hex (idx, pointer_size);
6891             printf (_("(index into .debug_addr) "));
6892             base_address = fetch_indexed_addr
6893               (debug_info_p->addr_base + idx * pointer_size, pointer_size);
6894             print_hex (base_address, pointer_size);
6895             printf (_("(base address)\n"));
6896             break;
6897 
6898           case DW_LLE_startx_endx:
6899             READ_ULEB (idx, start, section_end);
6900             begin = fetch_indexed_addr
6901               (debug_info_p->addr_base + idx * pointer_size, pointer_size);
6902             READ_ULEB (idx, start, section_end);
6903             end = fetch_indexed_addr
6904               (debug_info_p->addr_base + idx * pointer_size, pointer_size);
6905             break;
6906 
6907           case DW_LLE_startx_length:
6908             READ_ULEB (idx, start, section_end);
6909             begin = fetch_indexed_addr
6910               (debug_info_p->addr_base + idx * pointer_size, pointer_size);
6911             READ_ULEB (end, start, section_end);
6912             end += begin;
6913             break;
6914 
6915           case DW_LLE_default_location:
6916             begin = end = 0;
6917             break;
6918 
6919           case DW_LLE_offset_pair:
6920             READ_ULEB (begin, start, section_end);
6921             begin += base_address;
6922             READ_ULEB (end, start, section_end);
6923             end += base_address;
6924             break;
6925 
6926           case DW_LLE_base_address:
6927             SAFE_BYTE_GET_AND_INC (base_address, start, pointer_size,
6928                                          section_end);
6929             print_hex (base_address, pointer_size);
6930             printf (_("(base address)\n"));
6931             break;
6932 
6933           case DW_LLE_start_end:
6934             SAFE_BYTE_GET_AND_INC (begin, start, pointer_size, section_end);
6935             SAFE_BYTE_GET_AND_INC (end, start, pointer_size, section_end);
6936             break;
6937 
6938           case DW_LLE_start_length:
6939             SAFE_BYTE_GET_AND_INC (begin, start, pointer_size, section_end);
6940             READ_ULEB (end, start, section_end);
6941             end += begin;
6942             break;
6943 
6944 #ifdef DW_LLE_view_pair
6945           case DW_LLE_view_pair:
6946             if (vstart)
6947               printf (_("View pair entry in loclist with locviews attribute\n"));
6948             READ_ULEB (vbegin, start, section_end);
6949             print_view (vbegin, pointer_size);
6950 
6951             READ_ULEB (vend, start, section_end);
6952             print_view (vend, pointer_size);
6953 
6954             printf (_("views for:\n"));
6955             continue;
6956 #endif
6957 
6958           default:
6959             error (_("Invalid location list entry type %d\n"), llet);
6960             return;
6961           }
6962 
6963       if (llet == DW_LLE_end_of_list)
6964           break;
6965 
6966       if (llet == DW_LLE_base_address
6967             || llet == DW_LLE_base_addressx)
6968           continue;
6969 
6970       if (start == section_end)
6971           {
6972             warn (_("Location list starting at offset %#" PRIx64
6973                       " is not terminated.\n"), offset);
6974             break;
6975           }
6976       READ_ULEB (length, start, section_end);
6977 
6978       if (length > (size_t) (section_end - start))
6979           {
6980             warn (_("Location list starting at offset %#" PRIx64
6981                       " is not terminated.\n"), offset);
6982             break;
6983           }
6984 
6985       print_hex (begin, pointer_size);
6986       print_hex (end, pointer_size);
6987 
6988       putchar ('(');
6989       need_frame_base = decode_location_expression (start,
6990                                                                 pointer_size,
6991                                                                 offset_size,
6992                                                                 dwarf_version,
6993                                                                 length,
6994                                                                 cu_offset, section);
6995       putchar (')');
6996 
6997       if (need_frame_base && !has_frame_base)
6998           printf (_(" [without DW_AT_frame_base]"));
6999 
7000       if (begin == end && vbegin == vend)
7001           fputs (_(" (start == end)"), stdout);
7002       else if (begin > end || (begin == end && vbegin > vend))
7003           fputs (_(" (start > end)"), stdout);
7004 
7005       putchar ('\n');
7006 
7007       start += length;
7008       vbegin = vend = -1;
7009     }
7010 
7011   if (vbegin != (uint64_t) -1 || vend != (uint64_t) -1)
7012     printf (_("Trailing view pair not used in a range"));
7013 
7014   *start_ptr = start;
7015   *vstart_ptr = vstart;
7016 }
7017 
7018 /* Print a .debug_addr table index in decimal, surrounded by square brackets,
7019    right-adjusted in a field of length LEN, and followed by a space.  */
7020 
7021 static void
print_addr_index(unsigned int idx,unsigned int len)7022 print_addr_index (unsigned int idx, unsigned int len)
7023 {
7024   static char buf[15];
7025   snprintf (buf, sizeof (buf), "[%d]", idx);
7026   printf ("%*s ", len, buf);
7027 }
7028 
7029 /* Display a location list from a .dwo section. It uses address indexes rather
7030    than embedded addresses.  This code closely follows display_loc_list, but the
7031    two are sufficiently different that combining things is very ugly.  */
7032 
7033 static void
display_loc_list_dwo(struct dwarf_section * section,unsigned char ** start_ptr,unsigned int debug_info_entry,uint64_t offset,unsigned char ** vstart_ptr,int has_frame_base)7034 display_loc_list_dwo (struct dwarf_section *section,
7035                           unsigned char **start_ptr,
7036                           unsigned int debug_info_entry,
7037                           uint64_t offset,
7038                           unsigned char **vstart_ptr,
7039                           int has_frame_base)
7040 {
7041   unsigned char *start = *start_ptr, *vstart = *vstart_ptr;
7042   unsigned char *section_end = section->start + section->size;
7043   uint64_t cu_offset;
7044   unsigned int pointer_size;
7045   unsigned int offset_size;
7046   int dwarf_version;
7047   int entry_type;
7048   unsigned short length;
7049   int need_frame_base;
7050   unsigned int idx;
7051 
7052   if (debug_info_entry >= num_debug_info_entries)
7053     {
7054       warn (_("No debug information for loc lists of entry: %u\n"),
7055               debug_info_entry);
7056       return;
7057     }
7058 
7059   cu_offset = debug_information [debug_info_entry].cu_offset;
7060   pointer_size = debug_information [debug_info_entry].pointer_size;
7061   offset_size = debug_information [debug_info_entry].offset_size;
7062   dwarf_version = debug_information [debug_info_entry].dwarf_version;
7063 
7064   if (pointer_size < 2 || pointer_size > 8)
7065     {
7066       warn (_("Invalid pointer size (%d) in debug info for entry %d\n"),
7067               pointer_size, debug_info_entry);
7068       return;
7069     }
7070 
7071   while (1)
7072     {
7073       printf ("    ");
7074       print_hex (offset + (start - *start_ptr), 4);
7075 
7076       if (start >= section_end)
7077           {
7078             warn (_("Location list starting at offset %#" PRIx64
7079                       " is not terminated.\n"), offset);
7080             break;
7081           }
7082 
7083       SAFE_BYTE_GET_AND_INC (entry_type, start, 1, section_end);
7084 
7085       if (vstart)
7086           switch (entry_type)
7087             {
7088             default:
7089               break;
7090 
7091             case 2:
7092             case 3:
7093             case 4:
7094               {
7095                 uint64_t view;
7096                 uint64_t off = offset + (vstart - *start_ptr);
7097 
7098                 READ_ULEB (view, vstart, section_end);
7099                 print_view (view, 8);
7100 
7101                 READ_ULEB (view, vstart, section_end);
7102                 print_view (view, 8);
7103 
7104                 printf (_("views at %8.8" PRIx64 " for:\n    %*s "), off, 8, "");
7105 
7106               }
7107               break;
7108             }
7109 
7110       switch (entry_type)
7111           {
7112           case 0: /* A terminating entry.  */
7113             *start_ptr = start;
7114             *vstart_ptr = vstart;
7115             printf (_("<End of list>\n"));
7116             return;
7117           case 1: /* A base-address entry.  */
7118             READ_ULEB (idx, start, section_end);
7119             print_addr_index (idx, 8);
7120             printf ("%*s", 9 + (vstart ? 2 * 6 : 0), "");
7121             printf (_("(base address selection entry)\n"));
7122             continue;
7123           case 2: /* A start/end entry.  */
7124             READ_ULEB (idx, start, section_end);
7125             print_addr_index (idx, 8);
7126             READ_ULEB (idx, start, section_end);
7127             print_addr_index (idx, 8);
7128             break;
7129           case 3: /* A start/length entry.  */
7130             READ_ULEB (idx, start, section_end);
7131             print_addr_index (idx, 8);
7132             SAFE_BYTE_GET_AND_INC (idx, start, 4, section_end);
7133             printf ("%08x ", idx);
7134             break;
7135           case 4: /* An offset pair entry.  */
7136             SAFE_BYTE_GET_AND_INC (idx, start, 4, section_end);
7137             printf ("%08x ", idx);
7138             SAFE_BYTE_GET_AND_INC (idx, start, 4, section_end);
7139             printf ("%08x ", idx);
7140             break;
7141           default:
7142             warn (_("Unknown location list entry type 0x%x.\n"), entry_type);
7143             *start_ptr = start;
7144             *vstart_ptr = vstart;
7145             return;
7146           }
7147 
7148       if (2 > (size_t) (section_end - start))
7149           {
7150             warn (_("Location list starting at offset %#" PRIx64
7151                       " is not terminated.\n"), offset);
7152             break;
7153           }
7154 
7155       SAFE_BYTE_GET_AND_INC (length, start, 2, section_end);
7156       if (length > (size_t) (section_end - start))
7157           {
7158             warn (_("Location list starting at offset %#" PRIx64
7159                       " is not terminated.\n"), offset);
7160             break;
7161           }
7162 
7163       putchar ('(');
7164       need_frame_base = decode_location_expression (start,
7165                                                                 pointer_size,
7166                                                                 offset_size,
7167                                                                 dwarf_version,
7168                                                                 length,
7169                                                                 cu_offset, section);
7170       putchar (')');
7171 
7172       if (need_frame_base && !has_frame_base)
7173           printf (_(" [without DW_AT_frame_base]"));
7174 
7175       putchar ('\n');
7176 
7177       start += length;
7178     }
7179 
7180   *start_ptr = start;
7181   *vstart_ptr = vstart;
7182 }
7183 
7184 /* Sort array of indexes in ascending order of loc_offsets[idx] and
7185    loc_views.  */
7186 
7187 static uint64_t *loc_offsets, *loc_views;
7188 
7189 static int
loc_offsets_compar(const void * ap,const void * bp)7190 loc_offsets_compar (const void *ap, const void *bp)
7191 {
7192   uint64_t a = loc_offsets[*(const unsigned int *) ap];
7193   uint64_t b = loc_offsets[*(const unsigned int *) bp];
7194 
7195   int ret = (a > b) - (b > a);
7196   if (ret)
7197     return ret;
7198 
7199   a = loc_views[*(const unsigned int *) ap];
7200   b = loc_views[*(const unsigned int *) bp];
7201 
7202   ret = (a > b) - (b > a);
7203 
7204   return ret;
7205 }
7206 
7207 /* Reads and dumps the DWARFv5 loclists compiler unit header,
7208    including the offset table.
7209    Returns the offset of the next compile unit header.  */
7210 
7211 static uint64_t
display_loclists_unit_header(struct dwarf_section * section,uint64_t header_offset,uint32_t * offset_count,unsigned char ** loclists_start)7212 display_loclists_unit_header (struct dwarf_section *  section,
7213                                     uint64_t                header_offset,
7214                                     uint32_t *              offset_count,
7215                                     unsigned char **        loclists_start)
7216 {
7217   uint64_t length;
7218   unsigned char *start = section->start + header_offset;
7219   unsigned char *end = section->start + section->size;
7220   unsigned short version;
7221   unsigned char address_size;
7222   unsigned char segment_selector_size;
7223   bool is_64bit;
7224   uint32_t i;
7225 
7226   printf (_("Table at Offset %#" PRIx64 "\n"), header_offset);
7227 
7228   SAFE_BYTE_GET_AND_INC (length, start, 4, end);
7229   if (length == 0xffffffff)
7230     {
7231       is_64bit = true;
7232       SAFE_BYTE_GET_AND_INC (length, start, 8, end);
7233     }
7234   else
7235     is_64bit = false;
7236 
7237   SAFE_BYTE_GET_AND_INC (version, start, 2, end);
7238   SAFE_BYTE_GET_AND_INC (address_size, start, 1, end);
7239   SAFE_BYTE_GET_AND_INC (segment_selector_size, start, 1, end);
7240   SAFE_BYTE_GET_AND_INC (*offset_count, start, 4, end);
7241 
7242   printf (_("  Length:          %#" PRIx64 "\n"), length);
7243   printf (_("  DWARF version:   %u\n"), version);
7244   printf (_("  Address size:    %u\n"), address_size);
7245   printf (_("  Segment size:    %u\n"), segment_selector_size);
7246   printf (_("  Offset entries:  %u\n"), *offset_count);
7247 
7248   if (segment_selector_size != 0)
7249     {
7250       warn (_("The %s section contains an "
7251                 "unsupported segment selector size: %d.\n"),
7252               section->name, segment_selector_size);
7253       return (uint64_t)-1;
7254     }
7255 
7256   if ( *offset_count)
7257     {
7258       printf (_("\n   Offset Entries starting at %#tx:\n"),
7259                 start - section->start);
7260 
7261       for (i = 0; i < *offset_count; i++)
7262           {
7263             uint64_t entry;
7264 
7265             SAFE_BYTE_GET_AND_INC (entry, start, is_64bit ? 8 : 4, end);
7266             printf (_("    [%6u] %#" PRIx64 "\n"), i, entry);
7267           }
7268     }
7269 
7270   putchar ('\n');
7271   *loclists_start = start;
7272 
7273   /* The length field doesn't include the length field itself.  */
7274   return header_offset + length + (is_64bit ? 12 : 4);
7275 }
7276 
7277 static int
display_debug_loc(struct dwarf_section * section,void * file)7278 display_debug_loc (struct dwarf_section *section, void *file)
7279 {
7280   unsigned char *start = section->start, *vstart = NULL;
7281   uint64_t bytes;
7282   unsigned char *section_begin = start;
7283   unsigned int num_loc_list = 0;
7284   uint64_t last_offset = 0;
7285   uint64_t last_view = 0;
7286   unsigned int first = 0;
7287   unsigned int i;
7288   unsigned int j;
7289   int seen_first_offset = 0;
7290   int locs_sorted = 1;
7291   unsigned char *next = start, *vnext = vstart;
7292   unsigned int *array = NULL;
7293   const char *suffix = strrchr (section->name, '.');
7294   bool is_dwo = false;
7295   int is_loclists = strstr (section->name, "debug_loclists") != NULL;
7296   uint64_t next_header_offset = 0;
7297 
7298   if (suffix && strcmp (suffix, ".dwo") == 0)
7299     is_dwo = true;
7300 
7301   bytes = section->size;
7302 
7303   if (bytes == 0)
7304     {
7305       printf (_("\nThe %s section is empty.\n"), section->name);
7306       return 0;
7307     }
7308 
7309   if (is_loclists)
7310     {
7311       unsigned char *hdrptr = section_begin;
7312       uint64_t ll_length;
7313       unsigned short ll_version;
7314       unsigned char *end = section_begin + section->size;
7315       unsigned char address_size, segment_selector_size;
7316       uint32_t offset_entry_count;
7317 
7318       SAFE_BYTE_GET_AND_INC (ll_length, hdrptr, 4, end);
7319       if (ll_length == 0xffffffff)
7320           SAFE_BYTE_GET_AND_INC (ll_length, hdrptr, 8, end);
7321 
7322       SAFE_BYTE_GET_AND_INC (ll_version, hdrptr, 2, end);
7323       if (ll_version != 5)
7324           {
7325             warn (_("The %s section contains corrupt or "
7326                       "unsupported version number: %d.\n"),
7327                     section->name, ll_version);
7328             return 0;
7329           }
7330 
7331       SAFE_BYTE_GET_AND_INC (address_size, hdrptr, 1, end);
7332 
7333       SAFE_BYTE_GET_AND_INC (segment_selector_size, hdrptr, 1, end);
7334       if (segment_selector_size != 0)
7335           {
7336             warn (_("The %s section contains "
7337                       "unsupported segment selector size: %d.\n"),
7338                     section->name, segment_selector_size);
7339             return 0;
7340           }
7341 
7342       SAFE_BYTE_GET_AND_INC (offset_entry_count, hdrptr, 4, end);
7343 
7344       /*if (offset_entry_count != 0)
7345           return display_offset_entry_loclists (section);*/
7346 
7347       //header_size = hdrptr - section_begin;
7348     }
7349 
7350   if (load_debug_info (file) == 0)
7351     {
7352       warn (_("Unable to load/parse the .debug_info section, so cannot interpret the %s section.\n"),
7353               section->name);
7354       return 0;
7355     }
7356 
7357   /* Check the order of location list in .debug_info section. If
7358      offsets of location lists are in the ascending order, we can
7359      use `debug_information' directly.  */
7360   for (i = 0; i < num_debug_info_entries; i++)
7361     {
7362       unsigned int num;
7363 
7364       num = debug_information [i].num_loc_offsets;
7365       if (num > num_loc_list)
7366           num_loc_list = num;
7367 
7368       /* Check if we can use `debug_information' directly.  */
7369       if (locs_sorted && num != 0)
7370           {
7371             if (!seen_first_offset)
7372               {
7373                 /* This is the first location list.  */
7374                 last_offset = debug_information [i].loc_offsets [0];
7375                 last_view = debug_information [i].loc_views [0];
7376                 first = i;
7377                 seen_first_offset = 1;
7378                 j = 1;
7379               }
7380             else
7381               j = 0;
7382 
7383             for (; j < num; j++)
7384               {
7385                 if (last_offset >
7386                       debug_information [i].loc_offsets [j]
7387                       || (last_offset == debug_information [i].loc_offsets [j]
7388                           && last_view > debug_information [i].loc_views [j]))
7389                     {
7390                       locs_sorted = 0;
7391                       break;
7392                     }
7393                 last_offset = debug_information [i].loc_offsets [j];
7394                 last_view = debug_information [i].loc_views [j];
7395               }
7396           }
7397     }
7398 
7399   if (!seen_first_offset)
7400     error (_("No location lists in .debug_info section!\n"));
7401 
7402   if (!locs_sorted)
7403     array = (unsigned int *) xcmalloc (num_loc_list, sizeof (unsigned int));
7404 
7405   introduce (section, false);
7406 
7407   if (reloc_at (section, 0))
7408     printf (_(" Warning: This section has relocations - addresses seen here may not be accurate.\n\n"));
7409 
7410   if (!is_loclists)
7411     printf (_("    Offset   Begin            End              Expression\n"));
7412 
7413   for (i = first; i < num_debug_info_entries; i++)
7414     {
7415       uint64_t offset = 0, voffset = 0;
7416       uint64_t base_address;
7417       unsigned int k;
7418       int has_frame_base;
7419           debug_info *debug_info_p = debug_information + i;
7420           uint32_t offset_count;
7421 
7422 
7423       if (!locs_sorted)
7424           {
7425             for (k = 0; k < debug_info_p->num_loc_offsets; k++)
7426               array[k] = k;
7427             loc_offsets = debug_info_p->loc_offsets;
7428             loc_views = debug_info_p->loc_views;
7429             qsort (array, debug_info_p->num_loc_offsets,
7430                      sizeof (*array), loc_offsets_compar);
7431           }
7432 
7433       /* .debug_loclists has a per-unit header.
7434            Update start if we are detecting it.  */
7435       if (debug_info_p->dwarf_version == 5)
7436           {
7437             j = locs_sorted ? 0 : array [0];
7438 
7439             if (debug_info_p->num_loc_offsets)
7440               offset = debug_info_p->loc_offsets [j];
7441 
7442             if (debug_info_p->num_loc_views)
7443               voffset = debug_info_p->loc_views [j];
7444 
7445             /* Parse and dump unit headers in loclists.
7446                This will misbehave if the order of CUs in debug_info
7447                doesn't match the one in loclists.  */
7448             if (next_header_offset < offset)
7449               {
7450                 while (next_header_offset < offset)
7451                     {
7452                       next_header_offset = display_loclists_unit_header
7453                         (section, next_header_offset, &offset_count, &start);
7454 
7455                       if (next_header_offset == (uint64_t)-1)
7456                         /* Header parsing error.  */
7457                         return 0;
7458                     }
7459 
7460                 printf (_("\
7461     Offset   Begin            End              Expression\n"));
7462               }
7463           }
7464 
7465       int adjacent_view_loclists = 1;
7466 
7467       for (k = 0; k < debug_info_p->num_loc_offsets; k++)
7468           {
7469             j = locs_sorted ? k : array[k];
7470             if (k
7471                 && (debug_info_p->loc_offsets [locs_sorted
7472                                                                 ? k - 1 : array [k - 1]]
7473                       == debug_info_p->loc_offsets [j])
7474                 && (debug_info_p->loc_views [locs_sorted
7475                                                                ? k - 1 : array [k - 1]]
7476                       == debug_info_p->loc_views [j]))
7477               continue;
7478             has_frame_base = debug_info_p->have_frame_base [j];
7479             offset = debug_info_p->loc_offsets [j];
7480             next = section_begin + offset;
7481             voffset = debug_info_p->loc_views [j];
7482             if (voffset != (uint64_t) -1)
7483               vnext = section_begin + voffset;
7484             else
7485               vnext = NULL;
7486             base_address = debug_info_p->base_address;
7487 
7488             if (vnext && vnext < next)
7489               {
7490                 vstart = vnext;
7491                 display_view_pair_list (section, &vstart, i, next);
7492                 if (start == vnext)
7493                     start = vstart;
7494               }
7495 
7496             if (start < next)
7497               {
7498                 if (vnext && vnext < next)
7499                     warn (_("There is a hole [%#tx - %#" PRIx64 "]"
7500                               " in %s section.\n"),
7501                           start - section_begin, voffset, section->name);
7502                 else
7503                     warn (_("There is a hole [%#tx - %#" PRIx64 "]"
7504                               " in %s section.\n"),
7505                           start - section_begin, offset, section->name);
7506               }
7507             else if (start > next)
7508               warn (_("There is an overlap [%#tx - %#" PRIx64 "]"
7509                         " in %s section.\n"),
7510                       start - section_begin, offset, section->name);
7511             start = next;
7512             vstart = vnext;
7513 
7514             if (offset >= bytes)
7515               {
7516                 warn (_("Offset %#" PRIx64 " is bigger than %s section size.\n"),
7517                         offset, section->name);
7518                 continue;
7519               }
7520 
7521             if (vnext && voffset >= bytes)
7522               {
7523                 warn (_("View Offset %#" PRIx64 " is bigger than %s section size.\n"),
7524                         voffset, section->name);
7525                 continue;
7526               }
7527 
7528             if (!is_loclists)
7529               {
7530                 if (is_dwo)
7531                     display_loc_list_dwo (section, &start, i, offset,
7532                                               &vstart, has_frame_base);
7533                 else
7534                     display_loc_list (section, &start, i, offset, base_address,
7535                                           &vstart, has_frame_base);
7536               }
7537             else
7538               {
7539                 if (is_dwo)
7540                     warn (_("DWO is not yet supported.\n"));
7541                 else
7542                     display_loclists_list (section, &start, debug_info_p, offset,
7543                                                base_address, &vstart, has_frame_base);
7544               }
7545 
7546             /* FIXME: this arrangement is quite simplistic.  Nothing
7547                requires locview lists to be adjacent to corresponding
7548                loclists, and a single loclist could be augmented by
7549                different locview lists, and vice-versa, unlikely as it
7550                is that it would make sense to do so.  Hopefully we'll
7551                have view pair support built into loclists before we ever
7552                need to address all these possibilities.  */
7553             if (adjacent_view_loclists && vnext
7554                 && vnext != start && vstart != next)
7555               {
7556                 adjacent_view_loclists = 0;
7557                 warn (_("Hole and overlap detection requires adjacent view lists and loclists.\n"));
7558               }
7559 
7560             if (vnext && vnext == start)
7561               display_view_pair_list (section, &start, i, vstart);
7562           }
7563     }
7564 
7565   if (start < section->start + section->size)
7566     warn (ngettext ("There is %ld unused byte at the end of section %s\n",
7567                         "There are %ld unused bytes at the end of section %s\n",
7568                         (long) (section->start + section->size - start)),
7569             (long) (section->start + section->size - start), section->name);
7570   putchar ('\n');
7571   free (array);
7572   return 1;
7573 }
7574 
7575 static int
display_debug_str(struct dwarf_section * section,void * file ATTRIBUTE_UNUSED)7576 display_debug_str (struct dwarf_section *section,
7577                        void *file ATTRIBUTE_UNUSED)
7578 {
7579   unsigned char *start = section->start;
7580   uint64_t bytes = section->size;
7581   uint64_t addr = section->address;
7582 
7583   if (bytes == 0)
7584     {
7585       printf (_("\nThe %s section is empty.\n"), section->name);
7586       return 0;
7587     }
7588 
7589   introduce (section, false);
7590 
7591   while (bytes)
7592     {
7593       int j;
7594       int k;
7595       int lbytes;
7596 
7597       lbytes = (bytes > 16 ? 16 : bytes);
7598 
7599       printf ("  0x%8.8" PRIx64 " ", addr);
7600 
7601       for (j = 0; j < 16; j++)
7602           {
7603             if (j < lbytes)
7604               printf ("%2.2x", start[j]);
7605             else
7606               printf ("  ");
7607 
7608             if ((j & 3) == 3)
7609               printf (" ");
7610           }
7611 
7612       for (j = 0; j < lbytes; j++)
7613           {
7614             k = start[j];
7615             if (k >= ' ' && k < 0x80)
7616               printf ("%c", k);
7617             else
7618               printf (".");
7619           }
7620 
7621       putchar ('\n');
7622 
7623       start += lbytes;
7624       addr  += lbytes;
7625       bytes -= lbytes;
7626     }
7627 
7628   putchar ('\n');
7629 
7630   return 1;
7631 }
7632 
7633 static int
display_debug_info(struct dwarf_section * section,void * file)7634 display_debug_info (struct dwarf_section *section, void *file)
7635 {
7636   return process_debug_info (section, file, section->abbrev_sec, false, false);
7637 }
7638 
7639 static int
display_debug_types(struct dwarf_section * section,void * file)7640 display_debug_types (struct dwarf_section *section, void *file)
7641 {
7642   return process_debug_info (section, file, section->abbrev_sec, false, true);
7643 }
7644 
7645 static int
display_trace_info(struct dwarf_section * section,void * file)7646 display_trace_info (struct dwarf_section *section, void *file)
7647 {
7648   return process_debug_info (section, file, section->abbrev_sec, false, true);
7649 }
7650 
7651 static int
display_debug_aranges(struct dwarf_section * section,void * file ATTRIBUTE_UNUSED)7652 display_debug_aranges (struct dwarf_section *section,
7653                            void *file ATTRIBUTE_UNUSED)
7654 {
7655   unsigned char *start = section->start;
7656   unsigned char *end = start + section->size;
7657 
7658   introduce (section, false);
7659 
7660   /* It does not matter if this load fails,
7661      we test for that later on.  */
7662   load_debug_info (file);
7663 
7664   while (start < end)
7665     {
7666       unsigned char *hdrptr;
7667       DWARF2_Internal_ARange arange;
7668       unsigned char *addr_ranges;
7669       uint64_t length;
7670       uint64_t address;
7671       uint64_t sec_off;
7672       unsigned char address_size;
7673       unsigned int offset_size;
7674       unsigned char *end_ranges;
7675 
7676       hdrptr = start;
7677       sec_off = hdrptr - section->start;
7678 
7679       SAFE_BYTE_GET_AND_INC (arange.ar_length, hdrptr, 4, end);
7680       if (arange.ar_length == 0xffffffff)
7681           {
7682             SAFE_BYTE_GET_AND_INC (arange.ar_length, hdrptr, 8, end);
7683             offset_size = 8;
7684           }
7685       else
7686           offset_size = 4;
7687 
7688       if (arange.ar_length > (size_t) (end - hdrptr))
7689           {
7690             warn (_("Debug info is corrupted, %s header at %#" PRIx64
7691                       " has length %#" PRIx64 "\n"),
7692                     section->name, sec_off, arange.ar_length);
7693             break;
7694           }
7695       end_ranges = hdrptr + arange.ar_length;
7696 
7697       SAFE_BYTE_GET_AND_INC (arange.ar_version, hdrptr, 2, end_ranges);
7698       SAFE_BYTE_GET_AND_INC (arange.ar_info_offset, hdrptr, offset_size,
7699                                    end_ranges);
7700 
7701       if (num_debug_info_entries != DEBUG_INFO_UNAVAILABLE
7702             && num_debug_info_entries > 0
7703             && find_debug_info_for_offset (arange.ar_info_offset) == NULL)
7704           warn (_(".debug_info offset of %#" PRIx64
7705                     " in %s section does not point to a CU header.\n"),
7706                 arange.ar_info_offset, section->name);
7707 
7708       SAFE_BYTE_GET_AND_INC (arange.ar_pointer_size, hdrptr, 1, end_ranges);
7709       SAFE_BYTE_GET_AND_INC (arange.ar_segment_size, hdrptr, 1, end_ranges);
7710 
7711       if (arange.ar_version != 2 && arange.ar_version != 3)
7712           {
7713             /* PR 19872: A version number of 0 probably means that there is
7714                padding at the end of the .debug_aranges section.  Gold puts
7715                it there when performing an incremental link, for example.
7716                So do not generate a warning in this case.  */
7717             if (arange.ar_version)
7718               warn (_("Only DWARF 2 and 3 aranges are currently supported.\n"));
7719             break;
7720           }
7721 
7722       printf (_("  Length:                   %" PRId64 "\n"), arange.ar_length);
7723       printf (_("  Version:                  %d\n"), arange.ar_version);
7724       printf (_("  Offset into .debug_info:  %#" PRIx64 "\n"),
7725                 arange.ar_info_offset);
7726       printf (_("  Pointer Size:             %d\n"), arange.ar_pointer_size);
7727       printf (_("  Segment Size:             %d\n"), arange.ar_segment_size);
7728 
7729       address_size = arange.ar_pointer_size + arange.ar_segment_size;
7730 
7731       /* PR 17512: file: 001-108546-0.001:0.1.  */
7732       if (address_size == 0 || address_size > 8)
7733           {
7734             error (_("Invalid address size in %s section!\n"),
7735                      section->name);
7736             break;
7737           }
7738 
7739       /* The DWARF spec does not require that the address size be a power
7740            of two, but we do.  This will have to change if we ever encounter
7741            an uneven architecture.  */
7742       if ((address_size & (address_size - 1)) != 0)
7743           {
7744             warn (_("Pointer size + Segment size is not a power of two.\n"));
7745             break;
7746           }
7747 
7748       if (address_size > 4)
7749           printf (_("\n    Address            Length\n"));
7750       else
7751           printf (_("\n    Address    Length\n"));
7752 
7753       addr_ranges = hdrptr;
7754 
7755       /* Must pad to an alignment boundary that is twice the address size.  */
7756       addr_ranges += (2 * address_size - 1
7757                           - (hdrptr - start - 1) % (2 * address_size));
7758 
7759       while (2 * address_size <= end_ranges - addr_ranges)
7760           {
7761             SAFE_BYTE_GET_AND_INC (address, addr_ranges, address_size,
7762                                          end_ranges);
7763             SAFE_BYTE_GET_AND_INC (length, addr_ranges, address_size,
7764                                          end_ranges);
7765             printf ("    ");
7766             print_hex (address, address_size);
7767             print_hex_ns (length, address_size);
7768             putchar ('\n');
7769           }
7770 
7771       start = end_ranges;
7772     }
7773 
7774   printf ("\n");
7775 
7776   return 1;
7777 }
7778 
7779 /* Comparison function for qsort.  */
7780 static int
comp_addr_base(const void * v0,const void * v1)7781 comp_addr_base (const void * v0, const void * v1)
7782 {
7783   debug_info *info0 = *(debug_info **) v0;
7784   debug_info *info1 = *(debug_info **) v1;
7785   return info0->addr_base - info1->addr_base;
7786 }
7787 
7788 /* Display the debug_addr section.  */
7789 static int
display_debug_addr(struct dwarf_section * section,void * file)7790 display_debug_addr (struct dwarf_section *section,
7791                         void *file)
7792 {
7793   debug_info **debug_addr_info;
7794   unsigned char *entry;
7795   unsigned char *end;
7796   unsigned int i;
7797   unsigned int count;
7798   unsigned char * header;
7799 
7800   if (section->size == 0)
7801     {
7802       printf (_("\nThe %s section is empty.\n"), section->name);
7803       return 0;
7804     }
7805 
7806   if (load_debug_info (file) == 0)
7807     {
7808       warn (_("Unable to load/parse the .debug_info section, so cannot interpret the %s section.\n"),
7809               section->name);
7810       return 0;
7811     }
7812 
7813   introduce (section, false);
7814 
7815   /* PR  17531: file: cf38d01b.
7816      We use xcalloc because a corrupt file may not have initialised all of the
7817      fields in the debug_info structure, which means that the sort below might
7818      try to move uninitialised data.  */
7819   debug_addr_info = (debug_info **) xcalloc ((num_debug_info_entries + 1),
7820                                                        sizeof (debug_info *));
7821 
7822   count = 0;
7823   for (i = 0; i < num_debug_info_entries; i++)
7824     if (debug_information [i].addr_base != DEBUG_INFO_UNAVAILABLE)
7825       {
7826           /* PR 17531: file: cf38d01b.  */
7827           if (debug_information[i].addr_base >= section->size)
7828             warn (_("Corrupt address base (%#" PRIx64 ")"
7829                       " found in debug section %u\n"),
7830                     debug_information[i].addr_base, i);
7831           else
7832             debug_addr_info [count++] = debug_information + i;
7833       }
7834 
7835   /* Add a sentinel to make iteration convenient.  */
7836   debug_addr_info [count] = (debug_info *) xmalloc (sizeof (debug_info));
7837   debug_addr_info [count]->addr_base = section->size;
7838   qsort (debug_addr_info, count, sizeof (debug_info *), comp_addr_base);
7839 
7840   header = section->start;
7841   for (i = 0; i < count; i++)
7842     {
7843       unsigned int idx;
7844       unsigned int address_size = debug_addr_info [i]->pointer_size;
7845 
7846       printf (_("  For compilation unit at offset %#" PRIx64 ":\n"),
7847                 debug_addr_info [i]->cu_offset);
7848 
7849       printf (_("\tIndex\tAddress\n"));
7850       entry = section->start + debug_addr_info [i]->addr_base;
7851       if (debug_addr_info [i]->dwarf_version >= 5)
7852           {
7853             size_t header_size = entry - header;
7854             unsigned char *curr_header = header;
7855             uint64_t length;
7856             int version;
7857             int segment_selector_size;
7858 
7859             if (header_size != 8 && header_size != 16)
7860               {
7861                 warn (_("Corrupt %s section: expecting header size of 8 or 16, but found %zd instead"),
7862                         section->name, header_size);
7863                 break;
7864               }
7865 
7866             SAFE_BYTE_GET_AND_INC (length, curr_header, 4, entry);
7867             if (length == 0xffffffff)
7868               SAFE_BYTE_GET_AND_INC (length, curr_header, 8, entry);
7869             if (length > (size_t) (section->start + section->size - curr_header)
7870                 || length < (size_t) (entry - curr_header))
7871               {
7872                 warn (_("Corrupt %s section: unit_length field of %#" PRIx64
7873                           " is invalid"), section->name, length);
7874                 break;
7875               }
7876             end = curr_header + length;
7877             SAFE_BYTE_GET_AND_INC (version, curr_header, 2, entry);
7878             if (version != 5)
7879               warn (_("Corrupt %s section: expecting version number 5 in header but found %d instead\n"),
7880                       section->name, version);
7881 
7882             SAFE_BYTE_GET_AND_INC (address_size, curr_header, 1, entry);
7883             SAFE_BYTE_GET_AND_INC (segment_selector_size, curr_header, 1, entry);
7884             address_size += segment_selector_size;
7885           }
7886       else
7887           end = section->start + debug_addr_info [i + 1]->addr_base;
7888 
7889       header = end;
7890       idx = 0;
7891 
7892       if (address_size < 1 || address_size > sizeof (uint64_t))
7893           {
7894             warn (_("Corrupt %s section: address size (%x) is wrong"),
7895                     section->name, address_size);
7896             break;
7897           }
7898 
7899       while ((size_t) (end - entry) >= address_size)
7900           {
7901             uint64_t base = byte_get (entry, address_size);
7902             printf (_("\t%d:\t"), idx);
7903             print_hex_ns (base, address_size);
7904             printf ("\n");
7905             entry += address_size;
7906             idx++;
7907           }
7908     }
7909   printf ("\n");
7910 
7911   free (debug_addr_info[count]);
7912   free (debug_addr_info);
7913   return i == count;
7914 }
7915 
7916 /* Display the .debug_str_offsets and .debug_str_offsets.dwo sections.  */
7917 
7918 static int
display_debug_str_offsets(struct dwarf_section * section,void * file ATTRIBUTE_UNUSED)7919 display_debug_str_offsets (struct dwarf_section *section,
7920                                  void *file ATTRIBUTE_UNUSED)
7921 {
7922   unsigned long idx;
7923 
7924   if (section->size == 0)
7925     {
7926       printf (_("\nThe %s section is empty.\n"), section->name);
7927       return 0;
7928     }
7929 
7930   unsigned char *start = section->start;
7931   unsigned char *end = start + section->size;
7932   unsigned char *curr = start;
7933   uint64_t debug_str_offsets_hdr_len;
7934 
7935   const char *suffix = strrchr (section->name, '.');
7936   bool dwo = suffix && strcmp (suffix, ".dwo") == 0;
7937 
7938   if (dwo)
7939     load_debug_section_with_follow (str_dwo, file);
7940   else
7941     load_debug_section_with_follow (str, file);
7942 
7943   introduce (section, false);
7944 
7945   while (curr < end)
7946     {
7947       uint64_t length;
7948       uint64_t entry_length;
7949 
7950       SAFE_BYTE_GET_AND_INC (length, curr, 4, end);
7951       /* FIXME: We assume that this means 64-bit DWARF is being used.  */
7952       if (length == 0xffffffff)
7953           {
7954             SAFE_BYTE_GET_AND_INC (length, curr, 8, end);
7955             entry_length = 8;
7956             debug_str_offsets_hdr_len = 16;
7957           }
7958       else
7959           {
7960             entry_length = 4;
7961             debug_str_offsets_hdr_len = 8;
7962           }
7963 
7964       unsigned char *entries_end;
7965       if (length == 0)
7966           {
7967             /* This is probably an old style .debug_str_offset section which
7968                just contains offsets and no header (and the first offset is 0).  */
7969             length = section->size;
7970             curr   = section->start;
7971             entries_end = end;
7972 
7973             printf (_("    Length: %#" PRIx64 "\n"), length);
7974             printf (_("       Index   Offset [String]\n"));
7975           }
7976       else
7977           {
7978             if (length <= (size_t) (end - curr))
7979               entries_end = curr + length;
7980             else
7981               {
7982                 warn (_("Section %s is too small %#" PRIx64 "\n"),
7983                         section->name, section->size);
7984                 entries_end = end;
7985               }
7986 
7987             int version;
7988             SAFE_BYTE_GET_AND_INC (version, curr, 2, entries_end);
7989             if (version != 5)
7990               warn (_("Unexpected version number in str_offset header: %#x\n"), version);
7991 
7992             int padding;
7993             SAFE_BYTE_GET_AND_INC (padding, curr, 2, entries_end);
7994             if (padding != 0)
7995               warn (_("Unexpected value in str_offset header's padding field: %#x\n"), padding);
7996 
7997             printf (_("    Length: %#" PRIx64 "\n"), length);
7998             printf (_("    Version: %#x\n"), version);
7999             printf (_("       Index   Offset [String]\n"));
8000           }
8001 
8002       for (idx = 0; curr < entries_end; idx++)
8003           {
8004             uint64_t offset;
8005             const unsigned char * string;
8006 
8007             if ((size_t) (entries_end - curr) < entry_length)
8008               /* Not enough space to read one entry_length, give up.  */
8009               return 0;
8010 
8011             SAFE_BYTE_GET_AND_INC (offset, curr, entry_length, entries_end);
8012             if (dwo)
8013               string = (const unsigned char *)
8014                 fetch_indexed_string (idx, NULL, entry_length, dwo, debug_str_offsets_hdr_len);
8015             else
8016               string = fetch_indirect_string (offset);
8017 
8018             printf ("    %8lu ", idx);
8019             print_hex (offset, entry_length);
8020             printf (" %s\n", string);
8021           }
8022     }
8023 
8024   return 1;
8025 }
8026 
8027 /* Each debug_information[x].range_lists[y] gets this representation for
8028    sorting purposes.  */
8029 
8030 struct range_entry
8031 {
8032   /* The debug_information[x].range_lists[y] value.  */
8033   uint64_t ranges_offset;
8034 
8035   /* Original debug_information to find parameters of the data.  */
8036   debug_info *debug_info_p;
8037 };
8038 
8039 /* Sort struct range_entry in ascending order of its RANGES_OFFSET.  */
8040 
8041 static int
range_entry_compar(const void * ap,const void * bp)8042 range_entry_compar (const void *ap, const void *bp)
8043 {
8044   const struct range_entry *a_re = (const struct range_entry *) ap;
8045   const struct range_entry *b_re = (const struct range_entry *) bp;
8046   const uint64_t a = a_re->ranges_offset;
8047   const uint64_t b = b_re->ranges_offset;
8048 
8049   return (a > b) - (b > a);
8050 }
8051 
8052 static void
display_debug_ranges_list(unsigned char * start,unsigned char * finish,unsigned int pointer_size,uint64_t offset,uint64_t base_address)8053 display_debug_ranges_list (unsigned char *  start,
8054                                  unsigned char *  finish,
8055                                  unsigned int     pointer_size,
8056                                  uint64_t         offset,
8057                                  uint64_t         base_address)
8058 {
8059   while (start < finish)
8060     {
8061       uint64_t begin;
8062       uint64_t end;
8063 
8064       SAFE_BYTE_GET_AND_INC (begin, start, pointer_size, finish);
8065       if (start >= finish)
8066           break;
8067       SAFE_SIGNED_BYTE_GET_AND_INC (end, start, pointer_size, finish);
8068 
8069       printf ("    ");
8070       print_hex (offset, 4);
8071 
8072       if (begin == 0 && end == 0)
8073           {
8074             printf (_("<End of list>\n"));
8075             break;
8076           }
8077 
8078       /* Check base address specifiers.  */
8079       if (is_max_address (begin, pointer_size)
8080             && !is_max_address (end, pointer_size))
8081           {
8082             base_address = end;
8083             print_hex (begin, pointer_size);
8084             print_hex (end, pointer_size);
8085             printf ("(base address)\n");
8086             continue;
8087           }
8088 
8089       print_hex (begin + base_address, pointer_size);
8090       print_hex_ns (end + base_address, pointer_size);
8091 
8092       if (begin == end)
8093           fputs (_(" (start == end)"), stdout);
8094       else if (begin > end)
8095           fputs (_(" (start > end)"), stdout);
8096 
8097       putchar ('\n');
8098     }
8099 }
8100 
8101 static unsigned char *
display_debug_rnglists_list(unsigned char * start,unsigned char * finish,unsigned int pointer_size,uint64_t offset,uint64_t base_address,uint64_t addr_base)8102 display_debug_rnglists_list (unsigned char * start,
8103                                    unsigned char * finish,
8104                                    unsigned int    pointer_size,
8105                                    uint64_t        offset,
8106                                    uint64_t        base_address,
8107                                    uint64_t        addr_base)
8108 {
8109   unsigned char *next = start;
8110 
8111   while (1)
8112     {
8113       uint64_t off = offset + (start - next);
8114       enum dwarf_range_list_entry rlet;
8115       /* Initialize it due to a false compiler warning.  */
8116       uint64_t begin = -1, length, end = -1;
8117 
8118       if (start >= finish)
8119           {
8120             warn (_("Range list starting at offset %#" PRIx64
8121                       " is not terminated.\n"), offset);
8122             break;
8123           }
8124 
8125       printf ("    ");
8126       print_hex (off, 4);
8127 
8128       SAFE_BYTE_GET_AND_INC (rlet, start, 1, finish);
8129 
8130       switch (rlet)
8131           {
8132           case DW_RLE_end_of_list:
8133             printf (_("<End of list>\n"));
8134             break;
8135           case DW_RLE_base_addressx:
8136             READ_ULEB (base_address, start, finish);
8137             print_hex (base_address, pointer_size);
8138             printf (_("(base address index) "));
8139             base_address = fetch_indexed_addr ((base_address * pointer_size) + addr_base,
8140                                                        pointer_size);
8141             print_hex (base_address, pointer_size);
8142             printf (_("(base address)\n"));
8143             break;
8144           case DW_RLE_startx_endx:
8145             READ_ULEB (begin, start, finish);
8146             READ_ULEB (end, start, finish);
8147             begin = fetch_indexed_addr ((begin * pointer_size) + addr_base,
8148                                               pointer_size);
8149             end   = fetch_indexed_addr ((begin * pointer_size) + addr_base,
8150                                               pointer_size);
8151             break;
8152           case DW_RLE_startx_length:
8153             READ_ULEB (begin, start, finish);
8154             READ_ULEB (length, start, finish);
8155             begin = fetch_indexed_addr ((begin * pointer_size) + addr_base,
8156                                               pointer_size);
8157             end = begin + length;
8158             break;
8159           case DW_RLE_offset_pair:
8160             READ_ULEB (begin, start, finish);
8161             READ_ULEB (end, start, finish);
8162             break;
8163           case DW_RLE_base_address:
8164             SAFE_BYTE_GET_AND_INC (base_address, start, pointer_size, finish);
8165             print_hex (base_address, pointer_size);
8166             printf (_("(base address)\n"));
8167             break;
8168           case DW_RLE_start_end:
8169             SAFE_BYTE_GET_AND_INC (begin, start, pointer_size, finish);
8170             SAFE_BYTE_GET_AND_INC (end, start, pointer_size, finish);
8171             break;
8172           case DW_RLE_start_length:
8173             SAFE_BYTE_GET_AND_INC (begin, start, pointer_size, finish);
8174             READ_ULEB (length, start, finish);
8175             end = begin + length;
8176             break;
8177           default:
8178             error (_("Invalid range list entry type %d\n"), rlet);
8179             rlet = DW_RLE_end_of_list;
8180             break;
8181           }
8182 
8183       if (rlet == DW_RLE_end_of_list)
8184           break;
8185       if (rlet == DW_RLE_base_address || rlet == DW_RLE_base_addressx)
8186           continue;
8187 
8188       /* Only a DW_RLE_offset_pair needs the base address added.  */
8189       if (rlet == DW_RLE_offset_pair)
8190           {
8191             begin += base_address;
8192             end += base_address;
8193           }
8194 
8195       print_hex (begin, pointer_size);
8196       print_hex (end, pointer_size);
8197 
8198       if (begin == end)
8199           fputs (_(" (start == end)"), stdout);
8200       else if (begin > end)
8201           fputs (_(" (start > end)"), stdout);
8202 
8203       putchar ('\n');
8204     }
8205 
8206   return start;
8207 }
8208 
8209 static int
display_debug_rnglists_unit_header(struct dwarf_section * section,uint64_t * unit_offset,unsigned char * poffset_size)8210 display_debug_rnglists_unit_header (struct dwarf_section *  section,
8211                                             uint64_t *              unit_offset,
8212                                             unsigned char *         poffset_size)
8213 {
8214   uint64_t        start_offset = *unit_offset;
8215   unsigned char * p = section->start + start_offset;
8216   unsigned char * finish = section->start + section->size;
8217   uint64_t        initial_length;
8218   unsigned char   segment_selector_size;
8219   unsigned int    offset_entry_count;
8220   unsigned int    i;
8221   unsigned short  version;
8222   unsigned char   address_size = 0;
8223   unsigned char   offset_size;
8224 
8225   /* Get and check the length of the block.  */
8226   SAFE_BYTE_GET_AND_INC (initial_length, p, 4, finish);
8227 
8228   if (initial_length == 0xffffffff)
8229     {
8230       /* This section is 64-bit DWARF 3.  */
8231       SAFE_BYTE_GET_AND_INC (initial_length, p, 8, finish);
8232       *poffset_size = offset_size = 8;
8233     }
8234   else
8235     *poffset_size = offset_size = 4;
8236 
8237   if (initial_length > (size_t) (finish - p))
8238     {
8239       /* If the length field has a relocation against it, then we should
8240            not complain if it is inaccurate (and probably negative).
8241            It is copied from .debug_line handling code.  */
8242       if (reloc_at (section, (p - section->start) - offset_size))
8243           initial_length = finish - p;
8244       else
8245           {
8246             warn (_("The length field (%#" PRIx64
8247                       ") in the debug_rnglists header is wrong"
8248                       " - the section is too small\n"),
8249                     initial_length);
8250             return 0;
8251           }
8252     }
8253 
8254   /* Report the next unit offset to the caller.  */
8255   *unit_offset = (p - section->start) + initial_length;
8256 
8257   /* Get the other fields in the header.  */
8258   SAFE_BYTE_GET_AND_INC (version, p, 2, finish);
8259   SAFE_BYTE_GET_AND_INC (address_size, p, 1, finish);
8260   SAFE_BYTE_GET_AND_INC (segment_selector_size, p, 1, finish);
8261   SAFE_BYTE_GET_AND_INC (offset_entry_count, p, 4, finish);
8262 
8263   printf (_(" Table at Offset: %#" PRIx64 ":\n"), start_offset);
8264   printf (_("  Length:          %#" PRIx64 "\n"), initial_length);
8265   printf (_("  DWARF version:   %u\n"), version);
8266   printf (_("  Address size:    %u\n"), address_size);
8267   printf (_("  Segment size:    %u\n"), segment_selector_size);
8268   printf (_("  Offset entries:  %u\n"), offset_entry_count);
8269 
8270   /* Check the fields.  */
8271   if (segment_selector_size != 0)
8272     {
8273       warn (_("The %s section contains "
8274                 "unsupported segment selector size: %d.\n"),
8275               section->name, segment_selector_size);
8276       return 0;
8277     }
8278 
8279   if (version < 5)
8280     {
8281       warn (_("Only DWARF version 5+ debug_rnglists info "
8282                 "is currently supported.\n"));
8283       return 0;
8284     }
8285 
8286   if (offset_entry_count != 0)
8287     {
8288       printf (_("\n   Offsets starting at %#tx:\n"), p - section->start);
8289 
8290       for (i = 0; i < offset_entry_count; i++)
8291           {
8292             uint64_t entry;
8293 
8294             SAFE_BYTE_GET_AND_INC (entry, p, offset_size, finish);
8295             printf (_("    [%6u] %#" PRIx64 "\n"), i, entry);
8296           }
8297     }
8298 
8299   return 1;
8300 }
8301 
8302 static bool
is_range_list_for_this_section(bool is_rnglists,unsigned int version)8303 is_range_list_for_this_section (bool is_rnglists, unsigned int version)
8304 {
8305   if (is_rnglists && version > 4)
8306     return true;
8307 
8308   if (! is_rnglists && version < 5)
8309     return true;
8310 
8311   return false;
8312 }
8313 
8314 static int
display_debug_ranges(struct dwarf_section * section,void * file ATTRIBUTE_UNUSED)8315 display_debug_ranges (struct dwarf_section *section,
8316                           void *file ATTRIBUTE_UNUSED)
8317 {
8318   unsigned char *start = section->start;
8319   unsigned char *last_start = start;
8320   uint64_t bytes = section->size;
8321   unsigned char *section_begin = start;
8322   unsigned char *finish = start + bytes;
8323   unsigned int num_range_list, i;
8324   struct range_entry *range_entries;
8325   struct range_entry *range_entry_fill;
8326   bool is_rnglists = strstr (section->name, "debug_rnglists") != NULL;
8327   uint64_t last_offset = 0;
8328   uint64_t next_rnglists_cu_offset = 0;
8329   unsigned char offset_size;
8330 
8331   if (bytes == 0)
8332     {
8333       printf (_("\nThe %s section is empty.\n"), section->name);
8334       return 0;
8335     }
8336 
8337   introduce (section, false);
8338 
8339   if (load_debug_info (file) == 0)
8340     {
8341       warn (_("Unable to load/parse the .debug_info section, so cannot interpret the %s section.\n"),
8342               section->name);
8343       return 0;
8344     }
8345 
8346   num_range_list = 0;
8347   for (i = 0; i < num_debug_info_entries; i++)
8348     if (is_range_list_for_this_section (is_rnglists, debug_information [i].dwarf_version))
8349       num_range_list += debug_information [i].num_range_lists;
8350 
8351   if (num_range_list == 0)
8352     {
8353       /* This can happen when the file was compiled with -gsplit-debug
8354            which removes references to range lists from the primary .o file.  */
8355       printf (_("No range lists referenced by .debug_info section.\n"));
8356       return 1;
8357     }
8358 
8359   range_entry_fill = range_entries = XNEWVEC (struct range_entry, num_range_list);
8360 
8361   for (i = 0; i < num_debug_info_entries; i++)
8362     {
8363       debug_info *debug_info_p = &debug_information[i];
8364       unsigned int j;
8365 
8366       for (j = 0; j < debug_info_p->num_range_lists; j++)
8367           {
8368             if (is_range_list_for_this_section (is_rnglists, debug_info_p->dwarf_version))
8369               {
8370                 range_entry_fill->ranges_offset = debug_info_p->range_lists[j];
8371                 range_entry_fill->debug_info_p = debug_info_p;
8372                 range_entry_fill++;
8373               }
8374           }
8375     }
8376 
8377   assert (range_entry_fill >= range_entries);
8378   assert (num_range_list >= (unsigned int)(range_entry_fill - range_entries));
8379   num_range_list = range_entry_fill - range_entries;
8380   qsort (range_entries, num_range_list, sizeof (*range_entries),
8381            range_entry_compar);
8382 
8383   if (dwarf_check != 0 && range_entries[0].ranges_offset != 0)
8384     warn (_("Range lists in %s section start at %#" PRIx64 "\n"),
8385             section->name, range_entries[0].ranges_offset);
8386 
8387   putchar ('\n');
8388   if (!is_rnglists)
8389     printf (_("    Offset   Begin    End\n"));
8390 
8391   for (i = 0; i < num_range_list; i++)
8392     {
8393       struct range_entry *range_entry = &range_entries[i];
8394       debug_info *debug_info_p = range_entry->debug_info_p;
8395       unsigned int pointer_size;
8396       uint64_t offset;
8397       unsigned char *next;
8398       uint64_t base_address;
8399 
8400       pointer_size = debug_info_p->pointer_size;
8401       offset = range_entry->ranges_offset;
8402       base_address = debug_info_p->base_address;
8403 
8404       /* PR 17512: file: 001-101485-0.001:0.1.  */
8405       if (pointer_size < 2 || pointer_size > 8)
8406           {
8407             warn (_("Corrupt pointer size (%d) in debug entry at offset %#" PRIx64 "\n"),
8408                     pointer_size, offset);
8409             continue;
8410           }
8411 
8412       if (offset > (size_t) (finish - section_begin))
8413           {
8414             warn (_("Corrupt offset (%#" PRIx64 ") in range entry %u\n"),
8415                     offset, i);
8416             continue;
8417           }
8418 
8419       /* If we've moved on to the next compile unit in the rnglists section - dump the unit header(s).  */
8420       if (is_rnglists && next_rnglists_cu_offset < offset)
8421           {
8422             while (next_rnglists_cu_offset < offset)
8423               display_debug_rnglists_unit_header (section, &next_rnglists_cu_offset, &offset_size);
8424             printf (_("    Offset   Begin    End\n"));
8425           }
8426 
8427       next = section_begin + offset; /* Offset is from the section start, the base has already been added.  */
8428 
8429       /* If multiple DWARF entities reference the same range then we will
8430            have multiple entries in the `range_entries' list for the same
8431            offset.  Thanks to the sort above these will all be consecutive in
8432            the `range_entries' list, so we can easily ignore duplicates
8433            here.  */
8434       if (i > 0 && last_offset == offset)
8435           continue;
8436       last_offset = offset;
8437 
8438       if (dwarf_check != 0 && i > 0)
8439           {
8440             if (start < next)
8441               warn (_("There is a hole [%#tx - %#tx] in %s section.\n"),
8442                       start - section_begin, next - section_begin, section->name);
8443             else if (start > next)
8444               {
8445                 if (next == last_start)
8446                     continue;
8447                 warn (_("There is an overlap [%#tx - %#tx] in %s section.\n"),
8448                         start - section_begin, next - section_begin, section->name);
8449               }
8450           }
8451 
8452       start = next;
8453       last_start = next;
8454 
8455       if (is_rnglists)
8456         display_debug_rnglists_list
8457             (start, finish, pointer_size, offset, base_address, debug_info_p->addr_base);
8458       else
8459         display_debug_ranges_list
8460             (start, finish, pointer_size, offset, base_address);
8461     }
8462 
8463   /* Display trailing empty (or unreferenced) compile units, if any.  */
8464   if (is_rnglists)
8465     while (next_rnglists_cu_offset < section->size)
8466       display_debug_rnglists_unit_header (section, &next_rnglists_cu_offset, &offset_size);
8467 
8468   putchar ('\n');
8469 
8470   free (range_entries);
8471 
8472   return 1;
8473 }
8474 
8475 typedef struct Frame_Chunk
8476 {
8477   struct Frame_Chunk *next;
8478   unsigned char *chunk_start;
8479   unsigned int ncols;
8480   /* DW_CFA_{undefined,same_value,offset,register,unreferenced}  */
8481   short int *col_type;
8482   int64_t *col_offset;
8483   char *augmentation;
8484   unsigned int code_factor;
8485   int data_factor;
8486   uint64_t pc_begin;
8487   uint64_t pc_range;
8488   unsigned int cfa_reg;
8489   uint64_t cfa_offset;
8490   unsigned int ra;
8491   unsigned char fde_encoding;
8492   unsigned char cfa_exp;
8493   unsigned char ptr_size;
8494   unsigned char segment_size;
8495 }
8496 Frame_Chunk;
8497 
8498 typedef const char *(*dwarf_regname_lookup_ftype) (unsigned int);
8499 static dwarf_regname_lookup_ftype dwarf_regnames_lookup_func;
8500 static const char *const *dwarf_regnames;
8501 static unsigned int dwarf_regnames_count;
8502 static bool is_aarch64;
8503 
8504 /* A marker for a col_type that means this column was never referenced
8505    in the frame info.  */
8506 #define DW_CFA_unreferenced (-1)
8507 
8508 /* Return 0 if no more space is needed, 1 if more space is needed,
8509    -1 for invalid reg.  */
8510 
8511 static int
frame_need_space(Frame_Chunk * fc,unsigned int reg)8512 frame_need_space (Frame_Chunk *fc, unsigned int reg)
8513 {
8514   unsigned int prev = fc->ncols;
8515 
8516   if (reg < (unsigned int) fc->ncols)
8517     return 0;
8518 
8519   if (dwarf_regnames_count > 0
8520       && reg > dwarf_regnames_count)
8521     return -1;
8522 
8523   fc->ncols = reg + 1;
8524   /* PR 17512: file: 10450-2643-0.004.
8525      If reg == -1 then this can happen...  */
8526   if (fc->ncols == 0)
8527     return -1;
8528 
8529   /* PR 17512: file: 2844a11d.  */
8530   if (fc->ncols > 1024 && dwarf_regnames_count == 0)
8531     {
8532       error (_("Unfeasibly large register number: %u\n"), reg);
8533       fc->ncols = 0;
8534       /* FIXME: 1024 is an arbitrary limit.  Increase it if
8535            we ever encounter a valid binary that exceeds it.  */
8536       return -1;
8537     }
8538 
8539   fc->col_type = xcrealloc (fc->col_type, fc->ncols,
8540                                   sizeof (*fc->col_type));
8541   fc->col_offset = xcrealloc (fc->col_offset, fc->ncols,
8542                                     sizeof (*fc->col_offset));
8543   /* PR 17512: file:002-10025-0.005.  */
8544   if (fc->col_type == NULL || fc->col_offset == NULL)
8545     {
8546       error (_("Out of memory allocating %u columns in dwarf frame arrays\n"),
8547                fc->ncols);
8548       fc->ncols = 0;
8549       return -1;
8550     }
8551 
8552   while (prev < fc->ncols)
8553     {
8554       fc->col_type[prev] = DW_CFA_unreferenced;
8555       fc->col_offset[prev] = 0;
8556       prev++;
8557     }
8558   return 1;
8559 }
8560 
8561 static const char *const dwarf_regnames_i386[] =
8562 {
8563   "eax", "ecx", "edx", "ebx",                       /* 0 - 3  */
8564   "esp", "ebp", "esi", "edi",                       /* 4 - 7  */
8565   "eip", "eflags", NULL,                            /* 8 - 10  */
8566   "st0", "st1", "st2", "st3",                       /* 11 - 14  */
8567   "st4", "st5", "st6", "st7",                       /* 15 - 18  */
8568   NULL, NULL,                                                 /* 19 - 20  */
8569   "xmm0", "xmm1", "xmm2", "xmm3",                   /* 21 - 24  */
8570   "xmm4", "xmm5", "xmm6", "xmm7",                   /* 25 - 28  */
8571   "mm0", "mm1", "mm2", "mm3",                       /* 29 - 32  */
8572   "mm4", "mm5", "mm6", "mm7",                       /* 33 - 36  */
8573   "fcw", "fsw", "mxcsr",                            /* 37 - 39  */
8574   "es", "cs", "ss", "ds", "fs", "gs", NULL, NULL, /* 40 - 47  */
8575   "tr", "ldtr",                                               /* 48 - 49  */
8576   NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 50 - 57  */
8577   NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 58 - 65  */
8578   NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 66 - 73  */
8579   NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 74 - 81  */
8580   NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 82 - 89  */
8581   NULL, NULL, NULL,                                 /* 90 - 92  */
8582   "k0", "k1", "k2", "k3", "k4", "k5", "k6", "k7"  /* 93 - 100  */
8583 };
8584 
8585 static const char *const dwarf_regnames_iamcu[] =
8586 {
8587   "eax", "ecx", "edx", "ebx",                       /* 0 - 3  */
8588   "esp", "ebp", "esi", "edi",                       /* 4 - 7  */
8589   "eip", "eflags", NULL,                            /* 8 - 10  */
8590   NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 11 - 18  */
8591   NULL, NULL,                                                 /* 19 - 20  */
8592   NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 21 - 28  */
8593   NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 29 - 36  */
8594   NULL, NULL, NULL,                                 /* 37 - 39  */
8595   "es", "cs", "ss", "ds", "fs", "gs", NULL, NULL, /* 40 - 47  */
8596   "tr", "ldtr",                                               /* 48 - 49  */
8597   NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 50 - 57  */
8598   NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 58 - 65  */
8599   NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 66 - 73  */
8600   NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 74 - 81  */
8601   NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 82 - 89  */
8602   NULL, NULL, NULL,                                 /* 90 - 92  */
8603   NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL  /* 93 - 100  */
8604 };
8605 
8606 static void
init_dwarf_regnames_i386(void)8607 init_dwarf_regnames_i386 (void)
8608 {
8609   dwarf_regnames = dwarf_regnames_i386;
8610   dwarf_regnames_count = ARRAY_SIZE (dwarf_regnames_i386);
8611   dwarf_regnames_lookup_func = regname_internal_by_table_only;
8612 }
8613 
8614 static void
init_dwarf_regnames_iamcu(void)8615 init_dwarf_regnames_iamcu (void)
8616 {
8617   dwarf_regnames = dwarf_regnames_iamcu;
8618   dwarf_regnames_count = ARRAY_SIZE (dwarf_regnames_iamcu);
8619   dwarf_regnames_lookup_func = regname_internal_by_table_only;
8620 }
8621 
8622 static const char *const DW_CFA_GNU_window_save_name[] =
8623 {
8624   "DW_CFA_GNU_window_save",
8625   "DW_CFA_AARCH64_negate_ra_state"
8626 };
8627 
8628 static const char *const dwarf_regnames_x86_64[] =
8629 {
8630   "rax", "rdx", "rcx", "rbx",
8631   "rsi", "rdi", "rbp", "rsp",
8632   "r8",  "r9",  "r10", "r11",
8633   "r12", "r13", "r14", "r15",
8634   "rip",
8635   "xmm0",  "xmm1",  "xmm2",  "xmm3",
8636   "xmm4",  "xmm5",  "xmm6",  "xmm7",
8637   "xmm8",  "xmm9",  "xmm10", "xmm11",
8638   "xmm12", "xmm13", "xmm14", "xmm15",
8639   "st0", "st1", "st2", "st3",
8640   "st4", "st5", "st6", "st7",
8641   "mm0", "mm1", "mm2", "mm3",
8642   "mm4", "mm5", "mm6", "mm7",
8643   "rflags",
8644   "es", "cs", "ss", "ds", "fs", "gs", NULL, NULL,
8645   "fs.base", "gs.base", NULL, NULL,
8646   "tr", "ldtr",
8647   "mxcsr", "fcw", "fsw",
8648   "xmm16",  "xmm17",  "xmm18",  "xmm19",
8649   "xmm20",  "xmm21",  "xmm22",  "xmm23",
8650   "xmm24",  "xmm25",  "xmm26",  "xmm27",
8651   "xmm28",  "xmm29",  "xmm30",  "xmm31",
8652   NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 83 - 90  */
8653   NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 91 - 98  */
8654   NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 99 - 106  */
8655   NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 107 - 114  */
8656   NULL, NULL, NULL,                                 /* 115 - 117  */
8657   "k0", "k1", "k2", "k3", "k4", "k5", "k6", "k7"
8658 };
8659 
8660 static void
init_dwarf_regnames_x86_64(void)8661 init_dwarf_regnames_x86_64 (void)
8662 {
8663   dwarf_regnames = dwarf_regnames_x86_64;
8664   dwarf_regnames_count = ARRAY_SIZE (dwarf_regnames_x86_64);
8665   dwarf_regnames_lookup_func = regname_internal_by_table_only;
8666 }
8667 
8668 static const char *const dwarf_regnames_aarch64[] =
8669 {
8670    "x0",  "x1",  "x2",  "x3",  "x4",  "x5",  "x6",  "x7",
8671    "x8",  "x9", "x10", "x11", "x12", "x13", "x14", "x15",
8672   "x16", "x17", "x18", "x19", "x20", "x21", "x22", "x23",
8673   "x24", "x25", "x26", "x27", "x28", "x29", "x30", "sp",
8674    NULL, "elr",  NULL,  NULL,  NULL,  NULL,  NULL,  NULL,
8675    NULL,  NULL,  NULL,  NULL,  NULL,  NULL,  "vg", "ffr",
8676    "p0",  "p1",  "p2",  "p3",  "p4",  "p5",  "p6",  "p7",
8677    "p8",  "p9", "p10", "p11", "p12", "p13", "p14", "p15",
8678    "v0",  "v1",  "v2",  "v3",  "v4",  "v5",  "v6",  "v7",
8679    "v8",  "v9", "v10", "v11", "v12", "v13", "v14", "v15",
8680   "v16", "v17", "v18", "v19", "v20", "v21", "v22", "v23",
8681   "v24", "v25", "v26", "v27", "v28", "v29", "v30", "v31",
8682    "z0",  "z1",  "z2",  "z3",  "z4",  "z5",  "z6",  "z7",
8683    "z8",  "z9", "z10", "z11", "z12", "z13", "z14", "z15",
8684   "z16", "z17", "z18", "z19", "z20", "z21", "z22", "z23",
8685   "z24", "z25", "z26", "z27", "z28", "z29", "z30", "z31",
8686 };
8687 
8688 static void
init_dwarf_regnames_aarch64(void)8689 init_dwarf_regnames_aarch64 (void)
8690 {
8691   dwarf_regnames = dwarf_regnames_aarch64;
8692   dwarf_regnames_count = ARRAY_SIZE (dwarf_regnames_aarch64);
8693   dwarf_regnames_lookup_func = regname_internal_by_table_only;
8694   is_aarch64 = true;
8695 }
8696 
8697 static const char *const dwarf_regnames_s390[] =
8698 {
8699   /* Avoid saying "r5 (r5)", so omit the names of r0-r15.  */
8700   NULL,  NULL,  NULL,  NULL,  NULL,  NULL,  NULL,  NULL,
8701   NULL,  NULL,  NULL,  NULL,  NULL,  NULL,  NULL,  NULL,
8702   "f0",  "f2",  "f4",  "f6",  "f1",  "f3",  "f5",  "f7",
8703   "f8",  "f10", "f12", "f14", "f9",  "f11", "f13", "f15",
8704   "cr0", "cr1", "cr2", "cr3", "cr4", "cr5", "cr6", "cr7",
8705   "cr8", "cr9", "cr10", "cr11", "cr12", "cr13", "cr14", "cr15",
8706   "a0",  "a1",  "a2",  "a3",  "a4",  "a5",  "a6",  "a7",
8707   "a8",  "a9",  "a10", "a11", "a12", "a13", "a14", "a15",
8708   "pswm", "pswa",
8709   NULL, NULL,
8710   "v16", "v18", "v20", "v22", "v17", "v19", "v21", "v23",
8711   "v24", "v26", "v28", "v30", "v25", "v27", "v29", "v31",
8712 };
8713 
8714 static void
init_dwarf_regnames_s390(void)8715 init_dwarf_regnames_s390 (void)
8716 {
8717   dwarf_regnames = dwarf_regnames_s390;
8718   dwarf_regnames_count = ARRAY_SIZE (dwarf_regnames_s390);
8719   dwarf_regnames_lookup_func = regname_internal_by_table_only;
8720 }
8721 
8722 static const char *const dwarf_regnames_riscv[] =
8723 {
8724  "zero", "ra",   "sp",   "gp",  "tp",  "t0",  "t1",  "t2",  /*   0 -   7 */
8725  "s0",   "s1",   "a0",   "a1",  "a2",  "a3",  "a4",  "a5",  /*   8 -  15 */
8726  "a6",   "a7",   "s2",   "s3",  "s4",  "s5",  "s6",  "s7",  /*  16 -  23 */
8727  "s8",   "s9",   "s10",  "s11", "t3",  "t4",  "t5",  "t6",  /*  24 -  31 */
8728  "ft0",  "ft1",  "ft2",  "ft3", "ft4", "ft5", "ft6", "ft7", /*  32 -  39 */
8729  "fs0",  "fs1",                                             /*  40 -  41 */
8730  "fa0",  "fa1",  "fa2",  "fa3", "fa4", "fa5", "fa6", "fa7", /*  42 -  49 */
8731  "fs2",  "fs3",  "fs4",  "fs5", "fs6", "fs7", "fs8", "fs9", /*  50 -  57 */
8732  "fs10", "fs11",                                            /*  58 -  59 */
8733  "ft8",  "ft9",  "ft10", "ft11",                            /*  60 -  63 */
8734  NULL,  NULL,  NULL,  NULL,  NULL,  NULL,  NULL,  NULL,     /*  64 -  71 */
8735  NULL,  NULL,  NULL,  NULL,  NULL,  NULL,  NULL,  NULL,     /*  72 -  79 */
8736  NULL,  NULL,  NULL,  NULL,  NULL,  NULL,  NULL,  NULL,     /*  80 -  87 */
8737  NULL,  NULL,  NULL,  NULL,  NULL,  NULL,  NULL,  NULL,     /*  88 -  95 */
8738  "v0",  "v1",  "v2",  "v3",  "v4",  "v5",  "v6",  "v7",     /*  96 - 103 */
8739  "v8",  "v9",  "v10", "v11", "v12", "v13", "v14", "v15",    /* 104 - 111 */
8740  "v16", "v17", "v18", "v19", "v20", "v21", "v22", "v23",    /* 112 - 119 */
8741  "v24", "v25", "v26", "v27", "v28", "v29", "v30", "v31",    /* 120 - 127 */
8742 };
8743 
8744 /* A RISC-V replacement for REGNAME_INTERNAL_BY_TABLE_ONLY which handles
8745    the large number of CSRs.  */
8746 
8747 static const char *
regname_internal_riscv(unsigned int regno)8748 regname_internal_riscv (unsigned int regno)
8749 {
8750   const char *name = NULL;
8751 
8752   /* Lookup in the table first, this covers GPR and FPR.  */
8753   if (regno < ARRAY_SIZE (dwarf_regnames_riscv))
8754     name = dwarf_regnames_riscv [regno];
8755   else if (regno >= 4096 && regno <= 8191)
8756     {
8757       /* This might be a CSR, these live in a sparse number space from 4096
8758            to 8191  These numbers are defined in the RISC-V ELF ABI
8759            document.  */
8760       switch (regno)
8761           {
8762 #define DECLARE_CSR(NAME,VALUE,CLASS,DEFINE_VER,ABORT_VER) \
8763   case VALUE + 4096: name = #NAME; break;
8764 #include "opcode/riscv-opc.h"
8765 #undef DECLARE_CSR
8766 
8767           default:
8768             {
8769               static char csr_name[10];
8770               snprintf (csr_name, sizeof (csr_name), "csr%d", (regno - 4096));
8771               name = csr_name;
8772             }
8773             break;
8774           }
8775     }
8776 
8777   return name;
8778 }
8779 
8780 static void
init_dwarf_regnames_riscv(void)8781 init_dwarf_regnames_riscv (void)
8782 {
8783   dwarf_regnames = NULL;
8784   dwarf_regnames_count = 8192;
8785   dwarf_regnames_lookup_func = regname_internal_riscv;
8786 }
8787 
8788 void
init_dwarf_regnames_by_elf_machine_code(unsigned int e_machine)8789 init_dwarf_regnames_by_elf_machine_code (unsigned int e_machine)
8790 {
8791   dwarf_regnames_lookup_func = NULL;
8792   is_aarch64 = false;
8793 
8794   switch (e_machine)
8795     {
8796     case EM_386:
8797       init_dwarf_regnames_i386 ();
8798       break;
8799 
8800     case EM_IAMCU:
8801       init_dwarf_regnames_iamcu ();
8802       break;
8803 
8804     case EM_X86_64:
8805     case EM_L1OM:
8806     case EM_K1OM:
8807       init_dwarf_regnames_x86_64 ();
8808       break;
8809 
8810     case EM_AARCH64:
8811       init_dwarf_regnames_aarch64 ();
8812       break;
8813 
8814     case EM_S390:
8815       init_dwarf_regnames_s390 ();
8816       break;
8817 
8818     case EM_RISCV:
8819       init_dwarf_regnames_riscv ();
8820       break;
8821 
8822     default:
8823       break;
8824     }
8825 }
8826 
8827 /* Initialize the DWARF register name lookup state based on the
8828    architecture and specific machine type of a BFD.  */
8829 
8830 void
init_dwarf_regnames_by_bfd_arch_and_mach(enum bfd_architecture arch,unsigned long mach)8831 init_dwarf_regnames_by_bfd_arch_and_mach (enum bfd_architecture arch,
8832                                                     unsigned long mach)
8833 {
8834   dwarf_regnames_lookup_func = NULL;
8835   is_aarch64 = false;
8836 
8837   switch (arch)
8838     {
8839     case bfd_arch_i386:
8840       switch (mach)
8841           {
8842           case bfd_mach_x86_64:
8843           case bfd_mach_x86_64_intel_syntax:
8844           case bfd_mach_x64_32:
8845           case bfd_mach_x64_32_intel_syntax:
8846             init_dwarf_regnames_x86_64 ();
8847             break;
8848 
8849           default:
8850             init_dwarf_regnames_i386 ();
8851             break;
8852           }
8853       break;
8854 
8855     case bfd_arch_iamcu:
8856       init_dwarf_regnames_iamcu ();
8857       break;
8858 
8859     case bfd_arch_aarch64:
8860       init_dwarf_regnames_aarch64();
8861       break;
8862 
8863     case bfd_arch_s390:
8864       init_dwarf_regnames_s390 ();
8865       break;
8866 
8867     case bfd_arch_riscv:
8868       init_dwarf_regnames_riscv ();
8869       break;
8870 
8871     default:
8872       break;
8873     }
8874 }
8875 
8876 static const char *
regname_internal_by_table_only(unsigned int regno)8877 regname_internal_by_table_only (unsigned int regno)
8878 {
8879   if (dwarf_regnames != NULL
8880       && regno < dwarf_regnames_count
8881       && dwarf_regnames [regno] != NULL)
8882     return dwarf_regnames [regno];
8883 
8884   return NULL;
8885 }
8886 
8887 static const char *
regname(unsigned int regno,int name_only_p)8888 regname (unsigned int regno, int name_only_p)
8889 {
8890   static char reg[64];
8891 
8892   const char *name = NULL;
8893 
8894   if (dwarf_regnames_lookup_func != NULL)
8895     name = dwarf_regnames_lookup_func (regno);
8896 
8897   if (name != NULL)
8898     {
8899       if (name_only_p)
8900           return name;
8901       snprintf (reg, sizeof (reg), "r%d (%s)", regno, name);
8902     }
8903   else
8904     snprintf (reg, sizeof (reg), "r%d", regno);
8905   return reg;
8906 }
8907 
8908 static void
frame_display_row(Frame_Chunk * fc,int * need_col_headers,unsigned int * max_regs)8909 frame_display_row (Frame_Chunk *fc, int *need_col_headers, unsigned int *max_regs)
8910 {
8911   unsigned int r;
8912   char tmp[100];
8913 
8914   if (*max_regs != fc->ncols)
8915     *max_regs = fc->ncols;
8916 
8917   if (*need_col_headers)
8918     {
8919       *need_col_headers = 0;
8920 
8921       printf ("%-*s CFA      ", eh_addr_size * 2, "   LOC");
8922 
8923       for (r = 0; r < *max_regs; r++)
8924           if (fc->col_type[r] != DW_CFA_unreferenced)
8925             {
8926               if (r == fc->ra)
8927                 printf ("ra    ");
8928               else
8929                 printf ("%-5s ", regname (r, 1));
8930             }
8931 
8932       printf ("\n");
8933     }
8934 
8935   print_hex (fc->pc_begin, eh_addr_size);
8936   if (fc->cfa_exp)
8937     strcpy (tmp, "exp");
8938   else
8939     sprintf (tmp, "%s%+d", regname (fc->cfa_reg, 1), (int) fc->cfa_offset);
8940   printf ("%-8s ", tmp);
8941 
8942   for (r = 0; r < fc->ncols; r++)
8943     {
8944       if (fc->col_type[r] != DW_CFA_unreferenced)
8945           {
8946             switch (fc->col_type[r])
8947               {
8948               case DW_CFA_undefined:
8949                 strcpy (tmp, "u");
8950                 break;
8951               case DW_CFA_same_value:
8952                 strcpy (tmp, "s");
8953                 break;
8954               case DW_CFA_offset:
8955                 sprintf (tmp, "c%+" PRId64, fc->col_offset[r]);
8956                 break;
8957               case DW_CFA_val_offset:
8958                 sprintf (tmp, "v%+" PRId64, fc->col_offset[r]);
8959                 break;
8960               case DW_CFA_register:
8961                 sprintf (tmp, "%s", regname (fc->col_offset[r], 0));
8962                 break;
8963               case DW_CFA_expression:
8964                 strcpy (tmp, "exp");
8965                 break;
8966               case DW_CFA_val_expression:
8967                 strcpy (tmp, "vexp");
8968                 break;
8969               default:
8970                 strcpy (tmp, "n/a");
8971                 break;
8972               }
8973             printf ("%-5s ", tmp);
8974           }
8975     }
8976   printf ("\n");
8977 }
8978 
8979 #define GET(VAR, N) SAFE_BYTE_GET_AND_INC (VAR, start, N, end)
8980 
8981 static unsigned char *
read_cie(unsigned char * start,unsigned char * end,Frame_Chunk ** p_cie,int * p_version,uint64_t * p_aug_len,unsigned char ** p_aug)8982 read_cie (unsigned char *start, unsigned char *end,
8983             Frame_Chunk **p_cie, int *p_version,
8984             uint64_t *p_aug_len, unsigned char **p_aug)
8985 {
8986   int version;
8987   Frame_Chunk *fc;
8988   unsigned char *augmentation_data = NULL;
8989   uint64_t augmentation_data_len = 0;
8990 
8991   * p_cie = NULL;
8992   /* PR 17512: file: 001-228113-0.004.  */
8993   if (start >= end)
8994     return end;
8995 
8996   fc = (Frame_Chunk *) xmalloc (sizeof (Frame_Chunk));
8997   memset (fc, 0, sizeof (Frame_Chunk));
8998 
8999   fc->col_type = xmalloc (sizeof (*fc->col_type));
9000   fc->col_offset = xmalloc (sizeof (*fc->col_offset));
9001 
9002   version = *start++;
9003 
9004   fc->augmentation = (char *) start;
9005   /* PR 17512: file: 001-228113-0.004.
9006      Skip past augmentation name, but avoid running off the end of the data.  */
9007   while (start < end)
9008     if (* start ++ == '\0')
9009       break;
9010   if (start == end)
9011     {
9012       warn (_("No terminator for augmentation name\n"));
9013       goto fail;
9014     }
9015 
9016   if (strcmp (fc->augmentation, "eh") == 0)
9017     {
9018       if (eh_addr_size > (size_t) (end - start))
9019           goto fail;
9020       start += eh_addr_size;
9021     }
9022 
9023   if (version >= 4)
9024     {
9025       if (2 > (size_t) (end - start))
9026           goto fail;
9027       GET (fc->ptr_size, 1);
9028       if (fc->ptr_size < 1 || fc->ptr_size > 8)
9029           {
9030             warn (_("Invalid pointer size (%d) in CIE data\n"), fc->ptr_size);
9031             goto fail;
9032           }
9033 
9034       GET (fc->segment_size, 1);
9035       /* PR 17512: file: e99d2804.  */
9036       if (fc->segment_size > 8 || fc->segment_size + fc->ptr_size > 8)
9037           {
9038             warn (_("Invalid segment size (%d) in CIE data\n"), fc->segment_size);
9039             goto fail;
9040           }
9041 
9042       eh_addr_size = fc->ptr_size;
9043     }
9044   else
9045     {
9046       fc->ptr_size = eh_addr_size;
9047       fc->segment_size = 0;
9048     }
9049 
9050   READ_ULEB (fc->code_factor, start, end);
9051   READ_SLEB (fc->data_factor, start, end);
9052 
9053   if (start >= end)
9054     goto fail;
9055 
9056   if (version == 1)
9057     {
9058       GET (fc->ra, 1);
9059     }
9060   else
9061     {
9062       READ_ULEB (fc->ra, start, end);
9063     }
9064 
9065   if (fc->augmentation[0] == 'z')
9066     {
9067       if (start >= end)
9068           goto fail;
9069       READ_ULEB (augmentation_data_len, start, end);
9070       augmentation_data = start;
9071       /* PR 17512: file: 11042-2589-0.004.  */
9072       if (augmentation_data_len > (size_t) (end - start))
9073           {
9074             warn (_("Augmentation data too long: %#" PRIx64
9075                       ", expected at most %#tx\n"),
9076                     augmentation_data_len, end - start);
9077             goto fail;
9078           }
9079       start += augmentation_data_len;
9080     }
9081 
9082   if (augmentation_data_len)
9083     {
9084       unsigned char *p;
9085       unsigned char *q;
9086       unsigned char *qend;
9087 
9088       p = (unsigned char *) fc->augmentation + 1;
9089       q = augmentation_data;
9090       qend = q + augmentation_data_len;
9091 
9092       while (p < end && q < qend)
9093           {
9094             if (*p == 'L')
9095               q++;
9096             else if (*p == 'P')
9097               q += 1 + size_of_encoded_value (*q);
9098             else if (*p == 'R')
9099               fc->fde_encoding = *q++;
9100             else if (*p == 'S')
9101               ;
9102             else if (*p == 'B')
9103               ;
9104             else
9105               break;
9106             p++;
9107           }
9108       /* Note - it is OK if this loop terminates with q < qend.
9109            Padding may have been inserted to align the end of the CIE.  */
9110     }
9111 
9112   *p_cie = fc;
9113   if (p_version)
9114     *p_version = version;
9115   if (p_aug_len)
9116     {
9117       *p_aug_len = augmentation_data_len;
9118       *p_aug = augmentation_data;
9119     }
9120   return start;
9121 
9122  fail:
9123   free (fc->col_offset);
9124   free (fc->col_type);
9125   free (fc);
9126   return end;
9127 }
9128 
9129 /* Prints out the contents on the DATA array formatted as unsigned bytes.
9130    If do_wide is not enabled, then formats the output to fit into 80 columns.
9131    PRINTED contains the number of characters already written to the current
9132    output line.  */
9133 
9134 static void
display_data(size_t printed,const unsigned char * data,size_t len)9135 display_data (size_t printed, const unsigned char *data, size_t len)
9136 {
9137   if (do_wide || len < ((80 - printed) / 3))
9138     for (printed = 0; printed < len; ++printed)
9139       printf (" %02x", data[printed]);
9140   else
9141     {
9142       for (printed = 0; printed < len; ++printed)
9143           {
9144             if (printed % (80 / 3) == 0)
9145               putchar ('\n');
9146             printf (" %02x", data[printed]);
9147           }
9148     }
9149 }
9150 
9151 /* Prints out the contents on the augmentation data array.
9152    If do_wide is not enabled, then formats the output to fit into 80 columns.  */
9153 
9154 static void
display_augmentation_data(const unsigned char * data,uint64_t len)9155 display_augmentation_data (const unsigned char * data, uint64_t len)
9156 {
9157   size_t i;
9158 
9159   i = printf (_("  Augmentation data:    "));
9160   display_data (i, data, len);
9161 }
9162 
9163 static int
display_debug_frames(struct dwarf_section * section,void * file ATTRIBUTE_UNUSED)9164 display_debug_frames (struct dwarf_section *section,
9165                           void *file ATTRIBUTE_UNUSED)
9166 {
9167   unsigned char *start = section->start;
9168   unsigned char *end = start + section->size;
9169   unsigned char *section_start = start;
9170   Frame_Chunk *chunks = NULL, *forward_refs = NULL;
9171   Frame_Chunk *remembered_state = NULL;
9172   Frame_Chunk *rs;
9173   bool is_eh = strcmp (section->name, ".eh_frame") == 0;
9174   unsigned int max_regs = 0;
9175   const char *bad_reg = _("bad register: ");
9176   unsigned int saved_eh_addr_size = eh_addr_size;
9177 
9178   introduce (section, false);
9179 
9180   while (start < end)
9181     {
9182       unsigned char *saved_start;
9183       unsigned char *block_end;
9184       uint64_t length;
9185       uint64_t cie_id;
9186       Frame_Chunk *fc;
9187       Frame_Chunk *cie;
9188       int need_col_headers = 1;
9189       unsigned char *augmentation_data = NULL;
9190       uint64_t augmentation_data_len = 0;
9191       unsigned int encoded_ptr_size = saved_eh_addr_size;
9192       unsigned int offset_size;
9193       bool all_nops;
9194       static Frame_Chunk fde_fc;
9195 
9196       saved_start = start;
9197 
9198       SAFE_BYTE_GET_AND_INC (length, start, 4, end);
9199 
9200       if (length == 0)
9201           {
9202             printf ("\n%08tx ZERO terminator\n\n",
9203                         saved_start - section_start);
9204             /* Skip any zero terminators that directly follow.
9205                A corrupt section size could have loaded a whole
9206                slew of zero filled memory bytes.  eg
9207                PR 17512: file: 070-19381-0.004.  */
9208             while (start < end && * start == 0)
9209               ++ start;
9210             continue;
9211           }
9212 
9213       if (length == 0xffffffff)
9214           {
9215             SAFE_BYTE_GET_AND_INC (length, start, 8, end);
9216             offset_size = 8;
9217           }
9218       else
9219           offset_size = 4;
9220 
9221       if (length > (size_t) (end - start))
9222           {
9223             warn ("Invalid length %#" PRIx64 " in FDE at %#tx\n",
9224                     length, saved_start - section_start);
9225             block_end = end;
9226           }
9227       else
9228           block_end = start + length;
9229 
9230       SAFE_BYTE_GET_AND_INC (cie_id, start, offset_size, block_end);
9231 
9232       if (is_eh ? (cie_id == 0) : ((offset_size == 4 && cie_id == DW_CIE_ID)
9233                                            || (offset_size == 8 && cie_id == DW64_CIE_ID)))
9234           {
9235             int version;
9236             unsigned int mreg;
9237 
9238             start = read_cie (start, block_end, &cie, &version,
9239                                   &augmentation_data_len, &augmentation_data);
9240             /* PR 17512: file: 027-135133-0.005.  */
9241             if (cie == NULL)
9242               break;
9243 
9244             fc = cie;
9245             fc->next = chunks;
9246             chunks = fc;
9247             fc->chunk_start = saved_start;
9248             mreg = max_regs > 0 ? max_regs - 1 : 0;
9249             if (mreg < fc->ra)
9250               mreg = fc->ra;
9251             if (frame_need_space (fc, mreg) < 0)
9252               break;
9253             if (fc->fde_encoding)
9254               encoded_ptr_size = size_of_encoded_value (fc->fde_encoding);
9255 
9256             printf ("\n%08tx ", saved_start - section_start);
9257             print_hex (length, fc->ptr_size);
9258             print_hex (cie_id, offset_size);
9259 
9260             if (do_debug_frames_interp)
9261               {
9262                 printf ("CIE \"%s\" cf=%d df=%d ra=%d\n", fc->augmentation,
9263                           fc->code_factor, fc->data_factor, fc->ra);
9264               }
9265             else
9266               {
9267                 printf ("CIE\n");
9268                 printf ("  Version:               %d\n", version);
9269                 printf ("  Augmentation:          \"%s\"\n", fc->augmentation);
9270                 if (version >= 4)
9271                     {
9272                       printf ("  Pointer Size:          %u\n", fc->ptr_size);
9273                       printf ("  Segment Size:          %u\n", fc->segment_size);
9274                     }
9275                 printf ("  Code alignment factor: %u\n", fc->code_factor);
9276                 printf ("  Data alignment factor: %d\n", fc->data_factor);
9277                 printf ("  Return address column: %d\n", fc->ra);
9278 
9279                 if (augmentation_data_len)
9280                     display_augmentation_data (augmentation_data, augmentation_data_len);
9281 
9282                 putchar ('\n');
9283               }
9284           }
9285       else
9286           {
9287             unsigned char *look_for;
9288             unsigned long segment_selector;
9289             uint64_t cie_off;
9290 
9291             cie_off = cie_id;
9292             if (is_eh)
9293               {
9294                 uint64_t sign = (uint64_t) 1 << (offset_size * 8 - 1);
9295                 cie_off = (cie_off ^ sign) - sign;
9296                 cie_off = start - 4 - section_start - cie_off;
9297               }
9298 
9299             look_for = section_start + cie_off;
9300             if (cie_off <= (size_t) (saved_start - section_start))
9301               {
9302                 for (cie = chunks; cie ; cie = cie->next)
9303                     if (cie->chunk_start == look_for)
9304                       break;
9305               }
9306             else if (cie_off >= section->size)
9307               cie = NULL;
9308             else
9309               {
9310                 for (cie = forward_refs; cie ; cie = cie->next)
9311                     if (cie->chunk_start == look_for)
9312                       break;
9313                 if (!cie)
9314                     {
9315                       unsigned int off_size;
9316                       unsigned char *cie_scan;
9317 
9318                       cie_scan = look_for;
9319                       off_size = 4;
9320                       SAFE_BYTE_GET_AND_INC (length, cie_scan, 4, end);
9321                       if (length == 0xffffffff)
9322                         {
9323                           SAFE_BYTE_GET_AND_INC (length, cie_scan, 8, end);
9324                           off_size = 8;
9325                         }
9326                       if (length != 0 && length <= (size_t) (end - cie_scan))
9327                         {
9328                           uint64_t c_id;
9329                           unsigned char *cie_end = cie_scan + length;
9330 
9331                           SAFE_BYTE_GET_AND_INC (c_id, cie_scan, off_size,
9332                                                        cie_end);
9333                           if (is_eh
9334                                 ? c_id == 0
9335                                 : ((off_size == 4 && c_id == DW_CIE_ID)
9336                                    || (off_size == 8 && c_id == DW64_CIE_ID)))
9337                               {
9338                                 int version;
9339                                 unsigned int mreg;
9340 
9341                                 read_cie (cie_scan, cie_end, &cie, &version,
9342                                             &augmentation_data_len, &augmentation_data);
9343                                 /* PR 17512: file: 3450-2098-0.004.  */
9344                                 if (cie == NULL)
9345                                   {
9346                                     warn (_("Failed to read CIE information\n"));
9347                                     break;
9348                                   }
9349                                 cie->next = forward_refs;
9350                                 forward_refs = cie;
9351                                 cie->chunk_start = look_for;
9352                                 mreg = max_regs > 0 ? max_regs - 1 : 0;
9353                                 if (mreg < cie->ra)
9354                                   mreg = cie->ra;
9355                                 if (frame_need_space (cie, mreg) < 0)
9356                                   {
9357                                     warn (_("Invalid max register\n"));
9358                                     break;
9359                                   }
9360                                 if (cie->fde_encoding)
9361                                   encoded_ptr_size
9362                                     = size_of_encoded_value (cie->fde_encoding);
9363                               }
9364                         }
9365                     }
9366               }
9367 
9368             fc = &fde_fc;
9369             memset (fc, 0, sizeof (Frame_Chunk));
9370 
9371             if (!cie)
9372               {
9373                 fc->ncols = 0;
9374                 fc->col_type = xmalloc (sizeof (*fc->col_type));
9375                 fc->col_offset = xmalloc (sizeof (*fc->col_offset));
9376                 if (frame_need_space (fc, max_regs > 0 ? max_regs - 1 : 0) < 0)
9377                     {
9378                       warn (_("Invalid max register\n"));
9379                       break;
9380                     }
9381                 cie = fc;
9382                 fc->augmentation = "";
9383                 fc->fde_encoding = 0;
9384                 fc->ptr_size = eh_addr_size;
9385                 fc->segment_size = 0;
9386               }
9387             else
9388               {
9389                 fc->ncols = cie->ncols;
9390                 fc->col_type = xcmalloc (fc->ncols, sizeof (*fc->col_type));
9391                 fc->col_offset =  xcmalloc (fc->ncols, sizeof (*fc->col_offset));
9392                 memcpy (fc->col_type, cie->col_type,
9393                           fc->ncols * sizeof (*fc->col_type));
9394                 memcpy (fc->col_offset, cie->col_offset,
9395                           fc->ncols * sizeof (*fc->col_offset));
9396                 fc->augmentation = cie->augmentation;
9397                 fc->ptr_size = cie->ptr_size;
9398                 eh_addr_size = cie->ptr_size;
9399                 fc->segment_size = cie->segment_size;
9400                 fc->code_factor = cie->code_factor;
9401                 fc->data_factor = cie->data_factor;
9402                 fc->cfa_reg = cie->cfa_reg;
9403                 fc->cfa_offset = cie->cfa_offset;
9404                 fc->ra = cie->ra;
9405                 if (frame_need_space (fc, max_regs > 0 ? max_regs - 1: 0) < 0)
9406                     {
9407                       warn (_("Invalid max register\n"));
9408                       break;
9409                     }
9410                 fc->fde_encoding = cie->fde_encoding;
9411               }
9412 
9413             if (fc->fde_encoding)
9414               encoded_ptr_size = size_of_encoded_value (fc->fde_encoding);
9415 
9416             segment_selector = 0;
9417             if (fc->segment_size)
9418               {
9419                 if (fc->segment_size > sizeof (segment_selector))
9420                     {
9421                       /* PR 17512: file: 9e196b3e.  */
9422                       warn (_("Probably corrupt segment size: %d - using 4 instead\n"), fc->segment_size);
9423                       fc->segment_size = 4;
9424                     }
9425                 SAFE_BYTE_GET_AND_INC (segment_selector, start,
9426                                              fc->segment_size, block_end);
9427               }
9428 
9429             fc->pc_begin = get_encoded_value (&start, fc->fde_encoding, section,
9430                                                       block_end);
9431 
9432             /* FIXME: It appears that sometimes the final pc_range value is
9433                encoded in less than encoded_ptr_size bytes.  See the x86_64
9434                run of the "objcopy on compressed debug sections" test for an
9435                example of this.  */
9436             SAFE_BYTE_GET_AND_INC (fc->pc_range, start, encoded_ptr_size,
9437                                          block_end);
9438 
9439             if (cie->augmentation[0] == 'z')
9440               {
9441                 READ_ULEB (augmentation_data_len, start, block_end);
9442                 augmentation_data = start;
9443                 /* PR 17512 file: 722-8446-0.004 and PR 22386.  */
9444                 if (augmentation_data_len > (size_t) (block_end - start))
9445                     {
9446                       warn (_("Augmentation data too long: %#" PRIx64 ", "
9447                                 "expected at most %#tx\n"),
9448                               augmentation_data_len, block_end - start);
9449                       start = block_end;
9450                       augmentation_data = NULL;
9451                       augmentation_data_len = 0;
9452                     }
9453                 start += augmentation_data_len;
9454               }
9455 
9456             printf ("\n%08tx ", saved_start - section_start);
9457             print_hex (length, fc->ptr_size);
9458             print_hex (cie_id, offset_size);
9459             printf ("FDE ");
9460 
9461             if (cie->chunk_start)
9462               printf ("cie=%08tx", cie->chunk_start - section_start);
9463             else
9464               /* Ideally translate "invalid " to 8 chars, trailing space
9465                  is optional.  */
9466               printf (_("cie=invalid "));
9467 
9468             printf (" pc=");
9469             if (fc->segment_size)
9470               printf ("%04lx:", segment_selector);
9471 
9472             print_hex_ns (fc->pc_begin, fc->ptr_size);
9473             printf ("..");
9474             print_hex_ns (fc->pc_begin + fc->pc_range, fc->ptr_size);
9475             printf ("\n");
9476 
9477             if (! do_debug_frames_interp && augmentation_data_len)
9478               {
9479                 display_augmentation_data (augmentation_data, augmentation_data_len);
9480                 putchar ('\n');
9481               }
9482           }
9483 
9484       /* At this point, fc is the current chunk, cie (if any) is set, and
9485            we're about to interpret instructions for the chunk.  */
9486       /* ??? At present we need to do this always, since this sizes the
9487            fc->col_type and fc->col_offset arrays, which we write into always.
9488            We should probably split the interpreted and non-interpreted bits
9489            into two different routines, since there's so much that doesn't
9490            really overlap between them.  */
9491       if (1 || do_debug_frames_interp)
9492           {
9493             /* Start by making a pass over the chunk, allocating storage
9494                and taking note of what registers are used.  */
9495             unsigned char *tmp = start;
9496 
9497             while (start < block_end)
9498               {
9499                 unsigned int reg, op, opa;
9500                 unsigned long temp;
9501 
9502                 op = *start++;
9503                 opa = op & 0x3f;
9504                 if (op & 0xc0)
9505                     op &= 0xc0;
9506 
9507                 /* Warning: if you add any more cases to this switch, be
9508                      sure to add them to the corresponding switch below.  */
9509                 reg = -1u;
9510                 switch (op)
9511                     {
9512                     case DW_CFA_advance_loc:
9513                       break;
9514                     case DW_CFA_offset:
9515                       SKIP_ULEB (start, block_end);
9516                       reg = opa;
9517                       break;
9518                     case DW_CFA_restore:
9519                       reg = opa;
9520                       break;
9521                     case DW_CFA_set_loc:
9522                       if ((size_t) (block_end - start) < encoded_ptr_size)
9523                         start = block_end;
9524                       else
9525                         start += encoded_ptr_size;
9526                       break;
9527                     case DW_CFA_advance_loc1:
9528                       if ((size_t) (block_end - start) < 1)
9529                         start = block_end;
9530                       else
9531                         start += 1;
9532                       break;
9533                     case DW_CFA_advance_loc2:
9534                       if ((size_t) (block_end - start) < 2)
9535                         start = block_end;
9536                       else
9537                         start += 2;
9538                       break;
9539                     case DW_CFA_advance_loc4:
9540                       if ((size_t) (block_end - start) < 4)
9541                         start = block_end;
9542                       else
9543                         start += 4;
9544                       break;
9545                     case DW_CFA_offset_extended:
9546                     case DW_CFA_val_offset:
9547                       READ_ULEB (reg, start, block_end);
9548                       SKIP_ULEB (start, block_end);
9549                       break;
9550                     case DW_CFA_restore_extended:
9551                       READ_ULEB (reg, start, block_end);
9552                       break;
9553                     case DW_CFA_undefined:
9554                       READ_ULEB (reg, start, block_end);
9555                       break;
9556                     case DW_CFA_same_value:
9557                       READ_ULEB (reg, start, block_end);
9558                       break;
9559                     case DW_CFA_register:
9560                       READ_ULEB (reg, start, block_end);
9561                       SKIP_ULEB (start, block_end);
9562                       break;
9563                     case DW_CFA_def_cfa:
9564                       SKIP_ULEB (start, block_end);
9565                       SKIP_ULEB (start, block_end);
9566                       break;
9567                     case DW_CFA_def_cfa_register:
9568                       SKIP_ULEB (start, block_end);
9569                       break;
9570                     case DW_CFA_def_cfa_offset:
9571                       SKIP_ULEB (start, block_end);
9572                       break;
9573                     case DW_CFA_def_cfa_expression:
9574                       READ_ULEB (temp, start, block_end);
9575                       if ((size_t) (block_end - start) < temp)
9576                         start = block_end;
9577                       else
9578                         start += temp;
9579                       break;
9580                     case DW_CFA_expression:
9581                     case DW_CFA_val_expression:
9582                       READ_ULEB (reg, start, block_end);
9583                       READ_ULEB (temp, start, block_end);
9584                       if ((size_t) (block_end - start) < temp)
9585                         start = block_end;
9586                       else
9587                         start += temp;
9588                       break;
9589                     case DW_CFA_offset_extended_sf:
9590                     case DW_CFA_val_offset_sf:
9591                       READ_ULEB (reg, start, block_end);
9592                       SKIP_SLEB (start, block_end);
9593                       break;
9594                     case DW_CFA_def_cfa_sf:
9595                       SKIP_ULEB (start, block_end);
9596                       SKIP_SLEB (start, block_end);
9597                       break;
9598                     case DW_CFA_def_cfa_offset_sf:
9599                       SKIP_SLEB (start, block_end);
9600                       break;
9601                     case DW_CFA_MIPS_advance_loc8:
9602                       if ((size_t) (block_end - start) < 8)
9603                         start = block_end;
9604                       else
9605                         start += 8;
9606                       break;
9607                     case DW_CFA_GNU_args_size:
9608                       SKIP_ULEB (start, block_end);
9609                       break;
9610                     case DW_CFA_GNU_negative_offset_extended:
9611                       READ_ULEB (reg, start, block_end);
9612                       SKIP_ULEB (start, block_end);
9613                       break;
9614                     default:
9615                       break;
9616                     }
9617                 if (reg != -1u && frame_need_space (fc, reg) >= 0)
9618                     {
9619                       /* Don't leave any reg as DW_CFA_unreferenced so
9620                          that frame_display_row prints name of regs in
9621                          header, and all referenced regs in each line.  */
9622                       if (reg >= cie->ncols
9623                           || cie->col_type[reg] == DW_CFA_unreferenced)
9624                         fc->col_type[reg] = DW_CFA_undefined;
9625                       else
9626                         fc->col_type[reg] = cie->col_type[reg];
9627                     }
9628               }
9629             start = tmp;
9630           }
9631 
9632       all_nops = true;
9633 
9634       /* Now we know what registers are used, make a second pass over
9635            the chunk, this time actually printing out the info.  */
9636 
9637       while (start < block_end)
9638           {
9639             unsigned op, opa;
9640             /* Note: It is tempting to use an unsigned long for 'reg' but there
9641                are various functions, notably frame_space_needed() that assume that
9642                reg is an unsigned int.  */
9643             unsigned int reg;
9644             int64_t sofs;
9645             uint64_t ofs;
9646             const char *reg_prefix = "";
9647 
9648             op = *start++;
9649             opa = op & 0x3f;
9650             if (op & 0xc0)
9651               op &= 0xc0;
9652 
9653             /* Make a note if something other than DW_CFA_nop happens.  */
9654             if (op != DW_CFA_nop)
9655               all_nops = false;
9656 
9657             /* Warning: if you add any more cases to this switch, be
9658                sure to add them to the corresponding switch above.  */
9659             switch (op)
9660               {
9661               case DW_CFA_advance_loc:
9662                 opa *= fc->code_factor;
9663                 if (do_debug_frames_interp)
9664                     frame_display_row (fc, &need_col_headers, &max_regs);
9665                 else
9666                     {
9667                       printf ("  DW_CFA_advance_loc: %d to ", opa);
9668                       print_hex_ns (fc->pc_begin + opa, fc->ptr_size);
9669                       printf ("\n");
9670                     }
9671                 fc->pc_begin += opa;
9672                 break;
9673 
9674               case DW_CFA_offset:
9675                 READ_ULEB (ofs, start, block_end);
9676                 ofs *= fc->data_factor;
9677                 if (opa >= fc->ncols)
9678                     reg_prefix = bad_reg;
9679                 if (! do_debug_frames_interp || *reg_prefix != '\0')
9680                     printf ("  DW_CFA_offset: %s%s at cfa%+" PRId64 "\n",
9681                               reg_prefix, regname (opa, 0), ofs);
9682                 if (*reg_prefix == '\0')
9683                     {
9684                       fc->col_type[opa] = DW_CFA_offset;
9685                       fc->col_offset[opa] = ofs;
9686                     }
9687                 break;
9688 
9689               case DW_CFA_restore:
9690                 if (opa >= fc->ncols)
9691                     reg_prefix = bad_reg;
9692                 if (! do_debug_frames_interp || *reg_prefix != '\0')
9693                     printf ("  DW_CFA_restore: %s%s\n",
9694                               reg_prefix, regname (opa, 0));
9695                 if (*reg_prefix != '\0')
9696                     break;
9697 
9698                 if (opa >= cie->ncols
9699                       || cie->col_type[opa] == DW_CFA_unreferenced)
9700                     {
9701                       fc->col_type[opa] = DW_CFA_undefined;
9702                       fc->col_offset[opa] = 0;
9703                     }
9704                 else
9705                     {
9706                       fc->col_type[opa] = cie->col_type[opa];
9707                       fc->col_offset[opa] = cie->col_offset[opa];
9708                     }
9709                 break;
9710 
9711               case DW_CFA_set_loc:
9712                 ofs = get_encoded_value (&start, fc->fde_encoding, section,
9713                                                block_end);
9714                 if (do_debug_frames_interp)
9715                     frame_display_row (fc, &need_col_headers, &max_regs);
9716                 else
9717                     {
9718                       printf ("  DW_CFA_set_loc: ");
9719                       print_hex_ns (ofs, fc->ptr_size);
9720                       printf ("\n");
9721                     }
9722                 fc->pc_begin = ofs;
9723                 break;
9724 
9725               case DW_CFA_advance_loc1:
9726                 SAFE_BYTE_GET_AND_INC (ofs, start, 1, block_end);
9727                 ofs *= fc->code_factor;
9728                 if (do_debug_frames_interp)
9729                     frame_display_row (fc, &need_col_headers, &max_regs);
9730                 else
9731                     {
9732                       printf ("  DW_CFA_advance_loc1: %" PRId64 " to ", ofs);
9733                       print_hex_ns (fc->pc_begin + ofs, fc->ptr_size);
9734                       printf ("\n");
9735                     }
9736                 fc->pc_begin += ofs;
9737                 break;
9738 
9739               case DW_CFA_advance_loc2:
9740                 SAFE_BYTE_GET_AND_INC (ofs, start, 2, block_end);
9741                 ofs *= fc->code_factor;
9742                 if (do_debug_frames_interp)
9743                     frame_display_row (fc, &need_col_headers, &max_regs);
9744                 else
9745                     {
9746                       printf ("  DW_CFA_advance_loc2: %" PRId64 " to ", ofs);
9747                       print_hex_ns (fc->pc_begin + ofs, fc->ptr_size);
9748                       printf ("\n");
9749                     }
9750                 fc->pc_begin += ofs;
9751                 break;
9752 
9753               case DW_CFA_advance_loc4:
9754                 SAFE_BYTE_GET_AND_INC (ofs, start, 4, block_end);
9755                 ofs *= fc->code_factor;
9756                 if (do_debug_frames_interp)
9757                     frame_display_row (fc, &need_col_headers, &max_regs);
9758                 else
9759                     {
9760                       printf ("  DW_CFA_advance_loc4: %" PRId64 " to ", ofs);
9761                       print_hex_ns (fc->pc_begin + ofs, fc->ptr_size);
9762                       printf ("\n");
9763                     }
9764                 fc->pc_begin += ofs;
9765                 break;
9766 
9767               case DW_CFA_offset_extended:
9768                 READ_ULEB (reg, start, block_end);
9769                 READ_ULEB (ofs, start, block_end);
9770                 ofs *= fc->data_factor;
9771                 if (reg >= fc->ncols)
9772                     reg_prefix = bad_reg;
9773                 if (! do_debug_frames_interp || *reg_prefix != '\0')
9774                     printf ("  DW_CFA_offset_extended: %s%s at cfa%+" PRId64 "\n",
9775                               reg_prefix, regname (reg, 0), ofs);
9776                 if (*reg_prefix == '\0')
9777                     {
9778                       fc->col_type[reg] = DW_CFA_offset;
9779                       fc->col_offset[reg] = ofs;
9780                     }
9781                 break;
9782 
9783               case DW_CFA_val_offset:
9784                 READ_ULEB (reg, start, block_end);
9785                 READ_ULEB (ofs, start, block_end);
9786                 ofs *= fc->data_factor;
9787                 if (reg >= fc->ncols)
9788                     reg_prefix = bad_reg;
9789                 if (! do_debug_frames_interp || *reg_prefix != '\0')
9790                     printf ("  DW_CFA_val_offset: %s%s is cfa%+" PRId64 "\n",
9791                               reg_prefix, regname (reg, 0), ofs);
9792                 if (*reg_prefix == '\0')
9793                     {
9794                       fc->col_type[reg] = DW_CFA_val_offset;
9795                       fc->col_offset[reg] = ofs;
9796                     }
9797                 break;
9798 
9799               case DW_CFA_restore_extended:
9800                 READ_ULEB (reg, start, block_end);
9801                 if (reg >= fc->ncols)
9802                     reg_prefix = bad_reg;
9803                 if (! do_debug_frames_interp || *reg_prefix != '\0')
9804                     printf ("  DW_CFA_restore_extended: %s%s\n",
9805                               reg_prefix, regname (reg, 0));
9806                 if (*reg_prefix != '\0')
9807                     break;
9808 
9809                 if (reg >= cie->ncols
9810                       || cie->col_type[reg] == DW_CFA_unreferenced)
9811                     {
9812                       fc->col_type[reg] = DW_CFA_undefined;
9813                       fc->col_offset[reg] = 0;
9814                     }
9815                 else
9816                     {
9817                       fc->col_type[reg] = cie->col_type[reg];
9818                       fc->col_offset[reg] = cie->col_offset[reg];
9819                     }
9820                 break;
9821 
9822               case DW_CFA_undefined:
9823                 READ_ULEB (reg, start, block_end);
9824                 if (reg >= fc->ncols)
9825                     reg_prefix = bad_reg;
9826                 if (! do_debug_frames_interp || *reg_prefix != '\0')
9827                     printf ("  DW_CFA_undefined: %s%s\n",
9828                               reg_prefix, regname (reg, 0));
9829                 if (*reg_prefix == '\0')
9830                     {
9831                       fc->col_type[reg] = DW_CFA_undefined;
9832                       fc->col_offset[reg] = 0;
9833                     }
9834                 break;
9835 
9836               case DW_CFA_same_value:
9837                 READ_ULEB (reg, start, block_end);
9838                 if (reg >= fc->ncols)
9839                     reg_prefix = bad_reg;
9840                 if (! do_debug_frames_interp || *reg_prefix != '\0')
9841                     printf ("  DW_CFA_same_value: %s%s\n",
9842                               reg_prefix, regname (reg, 0));
9843                 if (*reg_prefix == '\0')
9844                     {
9845                       fc->col_type[reg] = DW_CFA_same_value;
9846                       fc->col_offset[reg] = 0;
9847                     }
9848                 break;
9849 
9850               case DW_CFA_register:
9851                 READ_ULEB (reg, start, block_end);
9852                 READ_ULEB (ofs, start, block_end);
9853                 if (reg >= fc->ncols)
9854                     reg_prefix = bad_reg;
9855                 if (! do_debug_frames_interp || *reg_prefix != '\0')
9856                     {
9857                       printf ("  DW_CFA_register: %s%s in ",
9858                                 reg_prefix, regname (reg, 0));
9859                       puts (regname (ofs, 0));
9860                     }
9861                 if (*reg_prefix == '\0')
9862                     {
9863                       fc->col_type[reg] = DW_CFA_register;
9864                       fc->col_offset[reg] = ofs;
9865                     }
9866                 break;
9867 
9868               case DW_CFA_remember_state:
9869                 if (! do_debug_frames_interp)
9870                     printf ("  DW_CFA_remember_state\n");
9871                 rs = (Frame_Chunk *) xmalloc (sizeof (Frame_Chunk));
9872                 rs->cfa_offset = fc->cfa_offset;
9873                 rs->cfa_reg = fc->cfa_reg;
9874                 rs->ra = fc->ra;
9875                 rs->cfa_exp = fc->cfa_exp;
9876                 rs->ncols = fc->ncols;
9877                 rs->col_type = xcmalloc (rs->ncols, sizeof (*rs->col_type));
9878                 rs->col_offset = xcmalloc (rs->ncols, sizeof (*rs->col_offset));
9879                 memcpy (rs->col_type, fc->col_type,
9880                           rs->ncols * sizeof (*fc->col_type));
9881                 memcpy (rs->col_offset, fc->col_offset,
9882                           rs->ncols * sizeof (*fc->col_offset));
9883                 rs->next = remembered_state;
9884                 remembered_state = rs;
9885                 break;
9886 
9887               case DW_CFA_restore_state:
9888                 if (! do_debug_frames_interp)
9889                     printf ("  DW_CFA_restore_state\n");
9890                 rs = remembered_state;
9891                 if (rs)
9892                     {
9893                       remembered_state = rs->next;
9894                       fc->cfa_offset = rs->cfa_offset;
9895                       fc->cfa_reg = rs->cfa_reg;
9896                       fc->ra = rs->ra;
9897                       fc->cfa_exp = rs->cfa_exp;
9898                       if (frame_need_space (fc, rs->ncols - 1) < 0)
9899                         {
9900                           warn (_("Invalid column number in saved frame state\n"));
9901                           fc->ncols = 0;
9902                         }
9903                       else
9904                         {
9905                           memcpy (fc->col_type, rs->col_type,
9906                                     rs->ncols * sizeof (*rs->col_type));
9907                           memcpy (fc->col_offset, rs->col_offset,
9908                                     rs->ncols * sizeof (*rs->col_offset));
9909                         }
9910                       free (rs->col_type);
9911                       free (rs->col_offset);
9912                       free (rs);
9913                     }
9914                 else if (do_debug_frames_interp)
9915                     printf ("Mismatched DW_CFA_restore_state\n");
9916                 break;
9917 
9918               case DW_CFA_def_cfa:
9919                 READ_ULEB (fc->cfa_reg, start, block_end);
9920                 READ_ULEB (fc->cfa_offset, start, block_end);
9921                 fc->cfa_exp = 0;
9922                 if (! do_debug_frames_interp)
9923                     printf ("  DW_CFA_def_cfa: %s ofs %d\n",
9924                               regname (fc->cfa_reg, 0), (int) fc->cfa_offset);
9925                 break;
9926 
9927               case DW_CFA_def_cfa_register:
9928                 READ_ULEB (fc->cfa_reg, start, block_end);
9929                 fc->cfa_exp = 0;
9930                 if (! do_debug_frames_interp)
9931                     printf ("  DW_CFA_def_cfa_register: %s\n",
9932                               regname (fc->cfa_reg, 0));
9933                 break;
9934 
9935               case DW_CFA_def_cfa_offset:
9936                 READ_ULEB (fc->cfa_offset, start, block_end);
9937                 if (! do_debug_frames_interp)
9938                     printf ("  DW_CFA_def_cfa_offset: %d\n", (int) fc->cfa_offset);
9939                 break;
9940 
9941               case DW_CFA_nop:
9942                 if (! do_debug_frames_interp)
9943                     printf ("  DW_CFA_nop\n");
9944                 break;
9945 
9946               case DW_CFA_def_cfa_expression:
9947                 READ_ULEB (ofs, start, block_end);
9948                 if (ofs > (size_t) (block_end - start))
9949                     {
9950                       printf (_("  %s: <corrupt len %" PRIu64 ">\n"),
9951                                 "DW_CFA_def_cfa_expression", ofs);
9952                       break;
9953                     }
9954                 if (! do_debug_frames_interp)
9955                     {
9956                       printf ("  DW_CFA_def_cfa_expression (");
9957                       decode_location_expression (start, eh_addr_size, 0, -1,
9958                                                         ofs, 0, section);
9959                       printf (")\n");
9960                     }
9961                 fc->cfa_exp = 1;
9962                 start += ofs;
9963                 break;
9964 
9965               case DW_CFA_expression:
9966                 READ_ULEB (reg, start, block_end);
9967                 READ_ULEB (ofs, start, block_end);
9968                 if (reg >= fc->ncols)
9969                     reg_prefix = bad_reg;
9970                 /* PR 17512: file: 069-133014-0.006.  */
9971                 /* PR 17512: file: 98c02eb4.  */
9972                 if (ofs > (size_t) (block_end - start))
9973                     {
9974                       printf (_("  %s: <corrupt len %" PRIu64 ">\n"),
9975                                 "DW_CFA_expression", ofs);
9976                       break;
9977                     }
9978                 if (! do_debug_frames_interp || *reg_prefix != '\0')
9979                     {
9980                       printf ("  DW_CFA_expression: %s%s (",
9981                                 reg_prefix, regname (reg, 0));
9982                       decode_location_expression (start, eh_addr_size, 0, -1,
9983                                                         ofs, 0, section);
9984                       printf (")\n");
9985                     }
9986                 if (*reg_prefix == '\0')
9987                     fc->col_type[reg] = DW_CFA_expression;
9988                 start += ofs;
9989                 break;
9990 
9991               case DW_CFA_val_expression:
9992                 READ_ULEB (reg, start, block_end);
9993                 READ_ULEB (ofs, start, block_end);
9994                 if (reg >= fc->ncols)
9995                     reg_prefix = bad_reg;
9996                 if (ofs > (size_t) (block_end - start))
9997                     {
9998                       printf ("  %s: <corrupt len %" PRIu64 ">\n",
9999                                 "DW_CFA_val_expression", ofs);
10000                       break;
10001                     }
10002                 if (! do_debug_frames_interp || *reg_prefix != '\0')
10003                     {
10004                       printf ("  DW_CFA_val_expression: %s%s (",
10005                                 reg_prefix, regname (reg, 0));
10006                       decode_location_expression (start, eh_addr_size, 0, -1,
10007                                                         ofs, 0, section);
10008                       printf (")\n");
10009                     }
10010                 if (*reg_prefix == '\0')
10011                     fc->col_type[reg] = DW_CFA_val_expression;
10012                 start += ofs;
10013                 break;
10014 
10015               case DW_CFA_offset_extended_sf:
10016                 READ_ULEB (reg, start, block_end);
10017                 READ_SLEB (sofs, start, block_end);
10018                 /* data_factor multiplicaton done here as unsigned to
10019                      avoid integer overflow warnings from asan on fuzzed
10020                      objects.  */
10021                 ofs = sofs;
10022                 ofs *= fc->data_factor;
10023                 if (reg >= fc->ncols)
10024                     reg_prefix = bad_reg;
10025                 if (! do_debug_frames_interp || *reg_prefix != '\0')
10026                     printf ("  DW_CFA_offset_extended_sf: %s%s at cfa%+" PRId64 "\n",
10027                               reg_prefix, regname (reg, 0), ofs);
10028                 if (*reg_prefix == '\0')
10029                     {
10030                       fc->col_type[reg] = DW_CFA_offset;
10031                       fc->col_offset[reg] = ofs;
10032                     }
10033                 break;
10034 
10035               case DW_CFA_val_offset_sf:
10036                 READ_ULEB (reg, start, block_end);
10037                 READ_SLEB (sofs, start, block_end);
10038                 ofs = sofs;
10039                 ofs *= fc->data_factor;
10040                 if (reg >= fc->ncols)
10041                     reg_prefix = bad_reg;
10042                 if (! do_debug_frames_interp || *reg_prefix != '\0')
10043                     printf ("  DW_CFA_val_offset_sf: %s%s is cfa%+" PRId64 "\n",
10044                               reg_prefix, regname (reg, 0), ofs);
10045                 if (*reg_prefix == '\0')
10046                     {
10047                       fc->col_type[reg] = DW_CFA_val_offset;
10048                       fc->col_offset[reg] = ofs;
10049                     }
10050                 break;
10051 
10052               case DW_CFA_def_cfa_sf:
10053                 READ_ULEB (fc->cfa_reg, start, block_end);
10054                 READ_SLEB (sofs, start, block_end);
10055                 ofs = sofs;
10056                 ofs *= fc->data_factor;
10057                 fc->cfa_offset = ofs;
10058                 fc->cfa_exp = 0;
10059                 if (! do_debug_frames_interp)
10060                     printf ("  DW_CFA_def_cfa_sf: %s ofs %" PRId64 "\n",
10061                               regname (fc->cfa_reg, 0), ofs);
10062                 break;
10063 
10064               case DW_CFA_def_cfa_offset_sf:
10065                 READ_SLEB (sofs, start, block_end);
10066                 ofs = sofs;
10067                 ofs *= fc->data_factor;
10068                 fc->cfa_offset = ofs;
10069                 if (! do_debug_frames_interp)
10070                     printf ("  DW_CFA_def_cfa_offset_sf: %" PRId64 "\n", ofs);
10071                 break;
10072 
10073               case DW_CFA_MIPS_advance_loc8:
10074                 SAFE_BYTE_GET_AND_INC (ofs, start, 8, block_end);
10075                 ofs *= fc->code_factor;
10076                 if (do_debug_frames_interp)
10077                     frame_display_row (fc, &need_col_headers, &max_regs);
10078                 else
10079                     {
10080                       printf ("  DW_CFA_MIPS_advance_loc8: %" PRId64 " to ", ofs);
10081                       print_hex_ns (fc->pc_begin + ofs, fc->ptr_size);
10082                       printf ("\n");
10083                     }
10084                 fc->pc_begin += ofs;
10085                 break;
10086 
10087               case DW_CFA_GNU_window_save:
10088                 if (! do_debug_frames_interp)
10089                     printf ("  %s\n", DW_CFA_GNU_window_save_name[is_aarch64]);
10090                 break;
10091 
10092               case DW_CFA_GNU_args_size:
10093                 READ_ULEB (ofs, start, block_end);
10094                 if (! do_debug_frames_interp)
10095                     printf ("  DW_CFA_GNU_args_size: %" PRIu64 "\n", ofs);
10096                 break;
10097 
10098               case DW_CFA_GNU_negative_offset_extended:
10099                 READ_ULEB (reg, start, block_end);
10100                 READ_SLEB (sofs, start, block_end);
10101                 ofs = sofs;
10102                 ofs = -ofs * fc->data_factor;
10103                 if (reg >= fc->ncols)
10104                     reg_prefix = bad_reg;
10105                 if (! do_debug_frames_interp || *reg_prefix != '\0')
10106                     printf ("  DW_CFA_GNU_negative_offset_extended: %s%s "
10107                               "at cfa%+" PRId64 "\n",
10108                               reg_prefix, regname (reg, 0), ofs);
10109                 if (*reg_prefix == '\0')
10110                     {
10111                       fc->col_type[reg] = DW_CFA_offset;
10112                       fc->col_offset[reg] = ofs;
10113                     }
10114                 break;
10115 
10116               default:
10117                 if (op >= DW_CFA_lo_user && op <= DW_CFA_hi_user)
10118                     printf (_("  DW_CFA_??? (User defined call frame op: %#x)\n"), op);
10119                 else
10120                     warn (_("Unsupported or unknown Dwarf Call Frame Instruction number: %#x\n"), op);
10121                 start = block_end;
10122               }
10123           }
10124 
10125       /* Interpret the CFA - as long as it is not completely full of NOPs.  */
10126       if (do_debug_frames_interp && ! all_nops)
10127           frame_display_row (fc, &need_col_headers, &max_regs);
10128 
10129       if (fde_fc.col_type != NULL)
10130           {
10131             free (fde_fc.col_type);
10132             fde_fc.col_type = NULL;
10133           }
10134       if (fde_fc.col_offset != NULL)
10135           {
10136             free (fde_fc.col_offset);
10137             fde_fc.col_offset = NULL;
10138           }
10139 
10140       start = block_end;
10141       eh_addr_size = saved_eh_addr_size;
10142     }
10143 
10144   printf ("\n");
10145 
10146   while (remembered_state != NULL)
10147     {
10148       rs = remembered_state;
10149       remembered_state = rs->next;
10150       free (rs->col_type);
10151       free (rs->col_offset);
10152       rs->next = NULL; /* Paranoia.  */
10153       free (rs);
10154     }
10155 
10156   while (chunks != NULL)
10157     {
10158       rs = chunks;
10159       chunks = rs->next;
10160       free (rs->col_type);
10161       free (rs->col_offset);
10162       rs->next = NULL; /* Paranoia.  */
10163       free (rs);
10164     }
10165 
10166   while (forward_refs != NULL)
10167     {
10168       rs = forward_refs;
10169       forward_refs = rs->next;
10170       free (rs->col_type);
10171       free (rs->col_offset);
10172       rs->next = NULL; /* Paranoia.  */
10173       free (rs);
10174     }
10175 
10176   return 1;
10177 }
10178 
10179 #undef GET
10180 
10181 static int
display_debug_names(struct dwarf_section * section,void * file)10182 display_debug_names (struct dwarf_section *section, void *file)
10183 {
10184   unsigned char *hdrptr = section->start;
10185   uint64_t unit_length;
10186   unsigned char *unit_start;
10187   const unsigned char *const section_end = section->start + section->size;
10188   unsigned char *unit_end;
10189 
10190   introduce (section, false);
10191 
10192   load_debug_section_with_follow (str, file);
10193 
10194   for (; hdrptr < section_end; hdrptr = unit_end)
10195     {
10196       unsigned int offset_size;
10197       uint16_t dwarf_version, padding;
10198       uint32_t comp_unit_count, local_type_unit_count, foreign_type_unit_count;
10199       uint64_t bucket_count, name_count, abbrev_table_size;
10200       uint32_t augmentation_string_size;
10201       unsigned int i;
10202       bool augmentation_printable;
10203       const char *augmentation_string;
10204       size_t total;
10205 
10206       unit_start = hdrptr;
10207 
10208       /* Get and check the length of the block.  */
10209       SAFE_BYTE_GET_AND_INC (unit_length, hdrptr, 4, section_end);
10210 
10211       if (unit_length == 0xffffffff)
10212           {
10213             /* This section is 64-bit DWARF.  */
10214             SAFE_BYTE_GET_AND_INC (unit_length, hdrptr, 8, section_end);
10215             offset_size = 8;
10216           }
10217       else
10218           offset_size = 4;
10219 
10220       if (unit_length > (size_t) (section_end - hdrptr)
10221             || unit_length < 2 + 2 + 4 * 7)
10222           {
10223           too_short:
10224             warn (_("Debug info is corrupted, %s header at %#tx"
10225                       " has length %#" PRIx64 "\n"),
10226                     section->name, unit_start - section->start, unit_length);
10227             return 0;
10228           }
10229       unit_end = hdrptr + unit_length;
10230 
10231       /* Get and check the version number.  */
10232       SAFE_BYTE_GET_AND_INC (dwarf_version, hdrptr, 2, unit_end);
10233       printf (_("Version %d\n"), (int) dwarf_version);
10234 
10235       /* Prior versions did not exist, and future versions may not be
10236            backwards compatible.  */
10237       if (dwarf_version != 5)
10238           {
10239             warn (_("Only DWARF version 5 .debug_names "
10240                       "is currently supported.\n"));
10241             return 0;
10242           }
10243 
10244       SAFE_BYTE_GET_AND_INC (padding, hdrptr, 2, unit_end);
10245       if (padding != 0)
10246           warn (_("Padding field of .debug_names must be 0 (found 0x%x)\n"),
10247                 padding);
10248 
10249       SAFE_BYTE_GET_AND_INC (comp_unit_count, hdrptr, 4, unit_end);
10250       if (comp_unit_count == 0)
10251           warn (_("Compilation unit count must be >= 1 in .debug_names\n"));
10252 
10253       SAFE_BYTE_GET_AND_INC (local_type_unit_count, hdrptr, 4, unit_end);
10254       SAFE_BYTE_GET_AND_INC (foreign_type_unit_count, hdrptr, 4, unit_end);
10255       SAFE_BYTE_GET_AND_INC (bucket_count, hdrptr, 4, unit_end);
10256       SAFE_BYTE_GET_AND_INC (name_count, hdrptr, 4, unit_end);
10257       SAFE_BYTE_GET_AND_INC (abbrev_table_size, hdrptr, 4, unit_end);
10258 
10259       SAFE_BYTE_GET_AND_INC (augmentation_string_size, hdrptr, 4, unit_end);
10260       if (augmentation_string_size % 4 != 0)
10261           {
10262             warn (_("Augmentation string length %u must be rounded up "
10263                       "to a multiple of 4 in .debug_names.\n"),
10264                     augmentation_string_size);
10265             augmentation_string_size += (-augmentation_string_size) & 3;
10266           }
10267       if (augmentation_string_size > (size_t) (unit_end - hdrptr))
10268           goto too_short;
10269 
10270       printf (_("Augmentation string:"));
10271 
10272       augmentation_printable = true;
10273       augmentation_string = (const char *) hdrptr;
10274 
10275       for (i = 0; i < augmentation_string_size; i++)
10276           {
10277             unsigned char uc;
10278 
10279             SAFE_BYTE_GET_AND_INC (uc, hdrptr, 1, unit_end);
10280             printf (" %02x", uc);
10281 
10282             if (uc != 0 && !ISPRINT (uc))
10283               augmentation_printable = false;
10284           }
10285 
10286       if (augmentation_printable)
10287           {
10288             printf ("  (\"");
10289             for (i = 0;
10290                  i < augmentation_string_size && augmentation_string[i];
10291                  ++i)
10292               putchar (augmentation_string[i]);
10293             printf ("\")");
10294           }
10295       putchar ('\n');
10296 
10297       printf (_("CU table:\n"));
10298       if (_mul_overflow (comp_unit_count, offset_size, &total)
10299             || total > (size_t) (unit_end - hdrptr))
10300           goto too_short;
10301       for (i = 0; i < comp_unit_count; i++)
10302           {
10303             uint64_t cu_offset;
10304 
10305             SAFE_BYTE_GET_AND_INC (cu_offset, hdrptr, offset_size, unit_end);
10306             printf ("[%3u] %#" PRIx64 "\n", i, cu_offset);
10307           }
10308       putchar ('\n');
10309 
10310       printf (_("TU table:\n"));
10311       if (_mul_overflow (local_type_unit_count, offset_size, &total)
10312             || total > (size_t) (unit_end - hdrptr))
10313           goto too_short;
10314       for (i = 0; i < local_type_unit_count; i++)
10315           {
10316             uint64_t tu_offset;
10317 
10318             SAFE_BYTE_GET_AND_INC (tu_offset, hdrptr, offset_size, unit_end);
10319             printf ("[%3u] %#" PRIx64 "\n", i, tu_offset);
10320           }
10321       putchar ('\n');
10322 
10323       printf (_("Foreign TU table:\n"));
10324       if (_mul_overflow (foreign_type_unit_count, 8, &total)
10325             || total > (size_t) (unit_end - hdrptr))
10326           goto too_short;
10327       for (i = 0; i < foreign_type_unit_count; i++)
10328           {
10329             uint64_t signature;
10330 
10331             SAFE_BYTE_GET_AND_INC (signature, hdrptr, 8, unit_end);
10332             printf (_("[%3u] "), i);
10333             print_hex_ns (signature, 8);
10334             putchar ('\n');
10335           }
10336       putchar ('\n');
10337 
10338       uint64_t xtra = (bucket_count * sizeof (uint32_t)
10339                            + name_count * (sizeof (uint32_t) + 2 * offset_size)
10340                            + abbrev_table_size);
10341       if (xtra > (size_t) (unit_end - hdrptr))
10342           {
10343             warn (_("Entry pool offset (%#" PRIx64 ") exceeds unit size %#tx "
10344                       "for unit %#tx in the debug_names\n"),
10345                     xtra, unit_end - unit_start, unit_start - section->start);
10346             return 0;
10347           }
10348       const uint32_t *const hash_table_buckets = (uint32_t *) hdrptr;
10349       hdrptr += bucket_count * sizeof (uint32_t);
10350       const uint32_t *const hash_table_hashes = (uint32_t *) hdrptr;
10351       if (bucket_count != 0)
10352           hdrptr += name_count * sizeof (uint32_t);
10353       unsigned char *const name_table_string_offsets = hdrptr;
10354       hdrptr += name_count * offset_size;
10355       unsigned char *const name_table_entry_offsets = hdrptr;
10356       hdrptr += name_count * offset_size;
10357       unsigned char *const abbrev_table = hdrptr;
10358       hdrptr += abbrev_table_size;
10359       const unsigned char *const abbrev_table_end = hdrptr;
10360       unsigned char *const entry_pool = hdrptr;
10361 
10362       size_t buckets_filled = 0;
10363       size_t bucketi;
10364       for (bucketi = 0; bucketi < bucket_count; bucketi++)
10365           {
10366             const uint32_t bucket = hash_table_buckets[bucketi];
10367 
10368             if (bucket != 0)
10369               ++buckets_filled;
10370           }
10371       printf (ngettext ("Used %zu of %lu bucket.\n",
10372                               "Used %zu of %lu buckets.\n",
10373                               (unsigned long) bucket_count),
10374                 buckets_filled, (unsigned long) bucket_count);
10375 
10376       if (bucket_count != 0)
10377           {
10378             uint32_t hash_prev = 0;
10379             size_t hash_clash_count = 0;
10380             size_t longest_clash = 0;
10381             size_t this_length = 0;
10382             size_t hashi;
10383             for (hashi = 0; hashi < name_count; hashi++)
10384               {
10385                 const uint32_t hash_this = hash_table_hashes[hashi];
10386 
10387                 if (hashi > 0)
10388                     {
10389                       if (hash_prev % bucket_count == hash_this % bucket_count)
10390                         {
10391                           ++hash_clash_count;
10392                           ++this_length;
10393                           longest_clash = MAX (longest_clash, this_length);
10394                         }
10395                       else
10396                         this_length = 0;
10397                     }
10398                 hash_prev = hash_this;
10399               }
10400             printf (_("Out of %" PRIu64 " items there are %zu bucket clashes"
10401                         " (longest of %zu entries).\n"),
10402                       name_count, hash_clash_count, longest_clash);
10403 
10404             if (name_count != buckets_filled + hash_clash_count)
10405               warn (_("The name_count (%" PRIu64 ")"
10406                         " is not the same as the used bucket_count"
10407                         " (%zu) + the hash clash count (%zu)"),
10408                       name_count, buckets_filled, hash_clash_count);
10409           }
10410 
10411       struct abbrev_lookup_entry
10412       {
10413           uint64_t abbrev_tag;
10414           unsigned char *abbrev_lookup_ptr;
10415       };
10416       struct abbrev_lookup_entry *abbrev_lookup = NULL;
10417       size_t abbrev_lookup_used = 0;
10418       size_t abbrev_lookup_allocated = 0;
10419 
10420       unsigned char *abbrevptr = abbrev_table;
10421       for (;;)
10422           {
10423             uint64_t abbrev_tag;
10424 
10425             READ_ULEB (abbrev_tag, abbrevptr, abbrev_table_end);
10426             if (abbrev_tag == 0)
10427               break;
10428             if (abbrev_lookup_used == abbrev_lookup_allocated)
10429               {
10430                 abbrev_lookup_allocated = MAX (0x100,
10431                                                        abbrev_lookup_allocated * 2);
10432                 abbrev_lookup = xrealloc (abbrev_lookup,
10433                                                   (abbrev_lookup_allocated
10434                                                    * sizeof (*abbrev_lookup)));
10435               }
10436             assert (abbrev_lookup_used < abbrev_lookup_allocated);
10437             struct abbrev_lookup_entry *entry;
10438             for (entry = abbrev_lookup;
10439                  entry < abbrev_lookup + abbrev_lookup_used;
10440                  entry++)
10441               if (entry->abbrev_tag == abbrev_tag)
10442                 {
10443                     warn (_("Duplicate abbreviation tag %" PRIu64
10444                               " in unit %#tx in the debug_names section\n"),
10445                           abbrev_tag, unit_start - section->start);
10446                     break;
10447                 }
10448             entry = &abbrev_lookup[abbrev_lookup_used++];
10449             entry->abbrev_tag = abbrev_tag;
10450             entry->abbrev_lookup_ptr = abbrevptr;
10451 
10452             /* Skip DWARF tag.  */
10453             SKIP_ULEB (abbrevptr, abbrev_table_end);
10454             for (;;)
10455               {
10456                 uint64_t xindex, form;
10457 
10458                 READ_ULEB (xindex, abbrevptr, abbrev_table_end);
10459                 READ_ULEB (form, abbrevptr, abbrev_table_end);
10460                 if (xindex == 0 && form == 0)
10461                     break;
10462               }
10463           }
10464 
10465       printf (_("\nSymbol table:\n"));
10466       uint32_t namei;
10467       for (namei = 0; namei < name_count; ++namei)
10468           {
10469             uint64_t string_offset, entry_offset;
10470             unsigned char *p;
10471 
10472             p = name_table_string_offsets + namei * offset_size;
10473             SAFE_BYTE_GET (string_offset, p, offset_size, unit_end);
10474             p = name_table_entry_offsets + namei * offset_size;
10475             SAFE_BYTE_GET (entry_offset, p, offset_size, unit_end);
10476 
10477             /* The name table is indexed starting at 1 according to
10478                DWARF, so be sure to use the DWARF numbering here.  */
10479             printf ("[%3u] ", namei + 1);
10480             if (bucket_count != 0)
10481               printf ("#%08x ", hash_table_hashes[namei]);
10482             printf ("%s:", fetch_indirect_string (string_offset));
10483 
10484             unsigned char *entryptr = entry_pool + entry_offset;
10485 
10486             /* We need to scan first whether there is a single or multiple
10487                entries.  TAGNO is -2 for the first entry, it is -1 for the
10488                initial tag read of the second entry, then it becomes 0 for the
10489                first entry for real printing etc.  */
10490             int tagno = -2;
10491             /* Initialize it due to a false compiler warning.  */
10492             uint64_t second_abbrev_tag = -1;
10493             for (;;)
10494               {
10495                 uint64_t abbrev_tag;
10496                 uint64_t dwarf_tag;
10497                 const struct abbrev_lookup_entry *entry;
10498 
10499                 READ_ULEB (abbrev_tag, entryptr, unit_end);
10500                 if (tagno == -1)
10501                     {
10502                       second_abbrev_tag = abbrev_tag;
10503                       tagno = 0;
10504                       entryptr = entry_pool + entry_offset;
10505                       continue;
10506                     }
10507                 if (abbrev_tag == 0)
10508                     break;
10509                 if (tagno >= 0)
10510                     printf ("%s<%" PRIu64 ">",
10511                               (tagno == 0 && second_abbrev_tag == 0 ? " " : "\n\t"),
10512                               abbrev_tag);
10513 
10514                 for (entry = abbrev_lookup;
10515                        entry < abbrev_lookup + abbrev_lookup_used;
10516                        entry++)
10517                     if (entry->abbrev_tag == abbrev_tag)
10518                       break;
10519                 if (entry >= abbrev_lookup + abbrev_lookup_used)
10520                     {
10521                       warn (_("Undefined abbreviation tag %" PRId64
10522                                 " in unit %#tx in the debug_names section\n"),
10523                               abbrev_tag,
10524                               unit_start - section->start);
10525                       break;
10526                     }
10527                 abbrevptr = entry->abbrev_lookup_ptr;
10528                 READ_ULEB (dwarf_tag, abbrevptr, abbrev_table_end);
10529                 if (tagno >= 0)
10530                     printf (" %s", get_TAG_name (dwarf_tag));
10531                 for (;;)
10532                     {
10533                       uint64_t xindex, form;
10534 
10535                       READ_ULEB (xindex, abbrevptr, abbrev_table_end);
10536                       READ_ULEB (form, abbrevptr, abbrev_table_end);
10537                       if (xindex == 0 && form == 0)
10538                         break;
10539 
10540                       if (tagno >= 0)
10541                         printf (" %s", get_IDX_name (xindex));
10542                       entryptr = read_and_display_attr_value (0, form, 0,
10543                                                                         unit_start, entryptr, unit_end,
10544                                                                         0, 0, offset_size,
10545                                                                         dwarf_version, NULL,
10546                                                                         (tagno < 0), section,
10547                                                                         NULL, '=', -1);
10548                     }
10549                 ++tagno;
10550               }
10551             if (tagno <= 0)
10552               printf (_(" <no entries>"));
10553             putchar ('\n');
10554           }
10555 
10556       free (abbrev_lookup);
10557     }
10558 
10559   return 1;
10560 }
10561 
10562 static int
display_debug_links(struct dwarf_section * section,void * file ATTRIBUTE_UNUSED)10563 display_debug_links (struct dwarf_section *  section,
10564                          void *                  file ATTRIBUTE_UNUSED)
10565 {
10566   const unsigned char * filename;
10567   unsigned int          filelen;
10568 
10569   introduce (section, false);
10570 
10571   /* The .gnu_debuglink section is formatted as:
10572       (c-string)  Filename.
10573       (padding)   If needed to reach a 4 byte boundary.
10574       (uint32_t)  CRC32 value.
10575 
10576     The .gun_debugaltlink section is formatted as:
10577       (c-string)  Filename.
10578       (binary)    Build-ID.  */
10579 
10580   filename =  section->start;
10581   filelen = strnlen ((const char *) filename, section->size);
10582   if (filelen == section->size)
10583     {
10584       warn (_("The debuglink filename is corrupt/missing\n"));
10585       return 0;
10586     }
10587 
10588   printf (_("  Separate debug info file: %s\n"), filename);
10589 
10590   if (startswith (section->name, ".gnu_debuglink"))
10591     {
10592       unsigned int          crc32;
10593       unsigned int          crc_offset;
10594 
10595       crc_offset = filelen + 1;
10596       crc_offset = (crc_offset + 3) & ~3;
10597       if (crc_offset + 4 > section->size)
10598           {
10599             warn (_("CRC offset missing/truncated\n"));
10600             return 0;
10601           }
10602 
10603       crc32 = byte_get (filename + crc_offset, 4);
10604 
10605       printf (_("  CRC value: %#x\n"), crc32);
10606 
10607       if (crc_offset + 4 < section->size)
10608           {
10609             warn (_("There are %#" PRIx64
10610                       " extraneous bytes at the end of the section\n"),
10611                     section->size - (crc_offset + 4));
10612             return 0;
10613           }
10614     }
10615   else /* startswith (section->name, ".gnu_debugaltlink") */
10616     {
10617       const unsigned char *build_id = section->start + filelen + 1;
10618       size_t build_id_len = section->size - (filelen + 1);
10619       size_t printed;
10620 
10621       /* FIXME: Should we support smaller build-id notes ?  */
10622       if (build_id_len < 0x14)
10623           {
10624             warn (_("Build-ID is too short (%#zx bytes)\n"), build_id_len);
10625             return 0;
10626           }
10627 
10628       printed = printf (_("  Build-ID (%#zx bytes):"), build_id_len);
10629       display_data (printed, build_id, build_id_len);
10630       putchar ('\n');
10631     }
10632 
10633   putchar ('\n');
10634   return 1;
10635 }
10636 
10637 static int
display_gdb_index(struct dwarf_section * section,void * file ATTRIBUTE_UNUSED)10638 display_gdb_index (struct dwarf_section *section,
10639                        void *file ATTRIBUTE_UNUSED)
10640 {
10641   unsigned char *start = section->start;
10642   uint32_t version;
10643   uint32_t cu_list_offset, tu_list_offset;
10644   uint32_t address_table_offset, symbol_table_offset, constant_pool_offset,
10645     shortcut_table_offset;
10646   unsigned int cu_list_elements, tu_list_elements;
10647   unsigned int address_table_elements, symbol_table_slots;
10648   unsigned char *cu_list, *tu_list;
10649   unsigned char *address_table, *symbol_table, *shortcut_table, *constant_pool;
10650   unsigned int i;
10651 
10652   /* The documentation for the format of this file is in gdb/dwarf2read.c.  */
10653 
10654   introduce (section, false);
10655 
10656   version = section->size < 4 ? 0 : byte_get_little_endian (start, 4);
10657   size_t header_size = (version < 9 ? 6 : 7) * sizeof (uint32_t);
10658   if (section->size < header_size)
10659     {
10660       warn (_("Truncated header in the %s section.\n"), section->name);
10661       return 0;
10662     }
10663 
10664   printf (_("Version %lu\n"), (unsigned long) version);
10665 
10666   /* Prior versions are obsolete, and future versions may not be
10667      backwards compatible.  */
10668   if (version < 3 || version > 9)
10669     {
10670       warn (_("Unsupported version %lu.\n"), (unsigned long) version);
10671       return 0;
10672     }
10673   if (version < 4)
10674     warn (_("The address table data in version 3 may be wrong.\n"));
10675   if (version < 5)
10676     warn (_("Version 4 does not support case insensitive lookups.\n"));
10677   if (version < 6)
10678     warn (_("Version 5 does not include inlined functions.\n"));
10679   if (version < 7)
10680       warn (_("Version 6 does not include symbol attributes.\n"));
10681   /* Version 7 indices generated by Gold have bad type unit references,
10682      PR binutils/15021.  But we don't know if the index was generated by
10683      Gold or not, so to avoid worrying users with gdb-generated indices
10684      we say nothing for version 7 here.  */
10685 
10686   cu_list_offset = byte_get_little_endian (start + 4, 4);
10687   tu_list_offset = byte_get_little_endian (start + 8, 4);
10688   address_table_offset = byte_get_little_endian (start + 12, 4);
10689   symbol_table_offset = byte_get_little_endian (start + 16, 4);
10690   shortcut_table_offset = byte_get_little_endian (start + 20, 4);
10691   if (version < 9)
10692     constant_pool_offset = shortcut_table_offset;
10693   else
10694     constant_pool_offset = byte_get_little_endian (start + 24, 4);
10695 
10696   if (cu_list_offset > section->size
10697       || tu_list_offset > section->size
10698       || address_table_offset > section->size
10699       || symbol_table_offset > section->size
10700       || shortcut_table_offset > section->size
10701       || constant_pool_offset > section->size
10702       || tu_list_offset < cu_list_offset
10703       || address_table_offset < tu_list_offset
10704       || symbol_table_offset < address_table_offset
10705       || shortcut_table_offset < symbol_table_offset
10706       || constant_pool_offset < shortcut_table_offset)
10707     {
10708       warn (_("Corrupt header in the %s section.\n"), section->name);
10709       return 0;
10710     }
10711 
10712   cu_list_elements = (tu_list_offset - cu_list_offset) / 16;
10713   tu_list_elements = (address_table_offset - tu_list_offset) / 24;
10714   address_table_elements = (symbol_table_offset - address_table_offset) / 20;
10715   symbol_table_slots = (shortcut_table_offset - symbol_table_offset) / 8;
10716 
10717   cu_list = start + cu_list_offset;
10718   tu_list = start + tu_list_offset;
10719   address_table = start + address_table_offset;
10720   symbol_table = start + symbol_table_offset;
10721   shortcut_table = start + shortcut_table_offset;
10722   constant_pool = start + constant_pool_offset;
10723 
10724   printf (_("\nCU table:\n"));
10725   for (i = 0; i < cu_list_elements; i++)
10726     {
10727       uint64_t cu_offset = byte_get_little_endian (cu_list + i * 16, 8);
10728       uint64_t cu_length = byte_get_little_endian (cu_list + i * 16 + 8, 8);
10729 
10730       printf ("[%3u] %#" PRIx64 " - %#" PRIx64 "\n",
10731                 i, cu_offset, cu_offset + cu_length - 1);
10732     }
10733 
10734   printf (_("\nTU table:\n"));
10735   for (i = 0; i < tu_list_elements; i++)
10736     {
10737       uint64_t tu_offset = byte_get_little_endian (tu_list + i * 24, 8);
10738       uint64_t type_offset = byte_get_little_endian (tu_list + i * 24 + 8, 8);
10739       uint64_t signature = byte_get_little_endian (tu_list + i * 24 + 16, 8);
10740 
10741       printf ("[%3u] %#" PRIx64 " %#" PRIx64 " ",
10742                 i, tu_offset, type_offset);
10743       print_hex_ns (signature, 8);
10744       printf ("\n");
10745     }
10746 
10747   printf (_("\nAddress table:\n"));
10748   for (i = 0; i < address_table_elements; i++)
10749     {
10750       uint64_t low = byte_get_little_endian (address_table + i * 20, 8);
10751       uint64_t high = byte_get_little_endian (address_table + i * 20 + 8, 8);
10752       uint32_t cu_index = byte_get_little_endian (address_table + i * 20 + 16, 4);
10753 
10754       print_hex (low, 8);
10755       print_hex (high, 8);
10756       printf ("%" PRIu32 "\n", cu_index);
10757     }
10758 
10759   printf (_("\nSymbol table:\n"));
10760   for (i = 0; i < symbol_table_slots; ++i)
10761     {
10762       uint32_t name_offset = byte_get_little_endian (symbol_table + i * 8, 4);
10763       uint32_t cu_vector_offset = byte_get_little_endian (symbol_table + i * 8 + 4, 4);
10764       uint32_t num_cus, cu;
10765 
10766       if (name_offset != 0
10767             || cu_vector_offset != 0)
10768           {
10769             unsigned int j;
10770 
10771             /* PR 17531: file: 5b7b07ad.  */
10772             if (name_offset >= section->size - constant_pool_offset)
10773               {
10774                 printf (_("[%3u] <corrupt offset: %x>"), i, name_offset);
10775                 warn (_("Corrupt name offset of 0x%x found for symbol table slot %d\n"),
10776                         name_offset, i);
10777               }
10778             else
10779               printf ("[%3u] %.*s:", i,
10780                         (int) (section->size - (constant_pool_offset + name_offset)),
10781                         constant_pool + name_offset);
10782 
10783             if (section->size - constant_pool_offset < 4
10784                 || cu_vector_offset > section->size - constant_pool_offset - 4)
10785               {
10786                 printf (_("<invalid CU vector offset: %x>\n"), cu_vector_offset);
10787                 warn (_("Corrupt CU vector offset of 0x%x found for symbol table slot %d\n"),
10788                         cu_vector_offset, i);
10789                 continue;
10790               }
10791 
10792             num_cus = byte_get_little_endian (constant_pool + cu_vector_offset, 4);
10793 
10794             if ((uint64_t) num_cus * 4 > section->size - (constant_pool_offset
10795                                                                       + cu_vector_offset + 4))
10796               {
10797                 printf ("<invalid number of CUs: %d>\n", num_cus);
10798                 warn (_("Invalid number of CUs (0x%x) for symbol table slot %d\n"),
10799                         num_cus, i);
10800                 continue;
10801               }
10802 
10803             if (num_cus > 1)
10804               printf ("\n");
10805 
10806             for (j = 0; j < num_cus; ++j)
10807               {
10808                 int is_static;
10809                 gdb_index_symbol_kind kind;
10810 
10811                 cu = byte_get_little_endian (constant_pool + cu_vector_offset + 4 + j * 4, 4);
10812                 is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu);
10813                 kind = GDB_INDEX_SYMBOL_KIND_VALUE (cu);
10814                 cu = GDB_INDEX_CU_VALUE (cu);
10815                 /* Convert to TU number if it's for a type unit.  */
10816                 if (cu >= cu_list_elements)
10817                     printf ("%cT%lu", num_cus > 1 ? '\t' : ' ',
10818                               (unsigned long) cu - cu_list_elements);
10819                 else
10820                     printf ("%c%lu", num_cus > 1 ? '\t' : ' ', (unsigned long) cu);
10821 
10822                 printf (" [%s, %s]",
10823                           is_static ? _("static") : _("global"),
10824                           get_gdb_index_symbol_kind_name (kind));
10825                 if (num_cus > 1)
10826                     printf ("\n");
10827               }
10828             if (num_cus <= 1)
10829               printf ("\n");
10830           }
10831     }
10832 
10833   if (version >= 9)
10834     {
10835       printf (_("\nShortcut table:\n"));
10836 
10837       if (shortcut_table_offset + 8 > constant_pool_offset)
10838           {
10839             warn (_("Corrupt shortcut table in the %s section.\n"), section->name);
10840             return 0;
10841           }
10842 
10843       uint32_t lang = byte_get_little_endian (shortcut_table, 4);
10844       printf (_("Language of main: "));
10845       display_lang (lang);
10846       printf ("\n");
10847 
10848       printf (_("Name of main: "));
10849       if (lang == 0)
10850           printf (_("<unknown>\n"));
10851       else
10852           {
10853             uint32_t name_offset = byte_get_little_endian (shortcut_table + 4, 4);
10854             if (name_offset >= section->size - constant_pool_offset)
10855               {
10856                 printf (_("<corrupt offset: %x>\n"), name_offset);
10857                 warn (_("Corrupt name offset of 0x%x found for name of main\n"),
10858                         name_offset);
10859               }
10860             else
10861               printf ("%s\n", constant_pool + name_offset);
10862           }
10863     }
10864 
10865   return 1;
10866 }
10867 
10868 /* Pre-allocate enough space for the CU/TU sets needed.  */
10869 
10870 static void
prealloc_cu_tu_list(unsigned int nshndx)10871 prealloc_cu_tu_list (unsigned int nshndx)
10872 {
10873   if (nshndx == 0)
10874     /* Always allocate at least one entry for the end-marker.  */
10875     nshndx = 1;
10876 
10877   if (shndx_pool == NULL)
10878     {
10879       shndx_pool_size = nshndx;
10880       shndx_pool_used = 0;
10881       shndx_pool = (unsigned int *) xcmalloc (shndx_pool_size,
10882                                                         sizeof (unsigned int));
10883     }
10884   else
10885     {
10886       shndx_pool_size = shndx_pool_used + nshndx;
10887       shndx_pool = (unsigned int *) xcrealloc (shndx_pool, shndx_pool_size,
10888                                                          sizeof (unsigned int));
10889     }
10890 }
10891 
10892 static void
add_shndx_to_cu_tu_entry(unsigned int shndx)10893 add_shndx_to_cu_tu_entry (unsigned int shndx)
10894 {
10895   shndx_pool [shndx_pool_used++] = shndx;
10896 }
10897 
10898 static void
end_cu_tu_entry(void)10899 end_cu_tu_entry (void)
10900 {
10901   shndx_pool [shndx_pool_used++] = 0;
10902 }
10903 
10904 /* Return the short name of a DWARF section given by a DW_SECT enumerator.  */
10905 
10906 static const char *
get_DW_SECT_short_name(unsigned int dw_sect)10907 get_DW_SECT_short_name (unsigned int dw_sect)
10908 {
10909   static char buf[16];
10910 
10911   switch (dw_sect)
10912     {
10913       case DW_SECT_INFO:
10914           return "info";
10915       case DW_SECT_TYPES:
10916           return "types";
10917       case DW_SECT_ABBREV:
10918           return "abbrev";
10919       case DW_SECT_LINE:
10920           return "line";
10921       case DW_SECT_LOC:
10922           return "loc";
10923       case DW_SECT_STR_OFFSETS:
10924           return "str_off";
10925       case DW_SECT_MACINFO:
10926           return "macinfo";
10927       case DW_SECT_MACRO:
10928           return "macro";
10929       default:
10930           break;
10931     }
10932 
10933   snprintf (buf, sizeof (buf), "%d", dw_sect);
10934   return buf;
10935 }
10936 
10937 /* Process a CU or TU index.  If DO_DISPLAY is true, print the contents.
10938    These sections are extensions for Fission.
10939    See http://gcc.gnu.org/wiki/DebugFissionDWP.  */
10940 
10941 static bool
process_cu_tu_index(struct dwarf_section * section,int do_display)10942 process_cu_tu_index (struct dwarf_section *section, int do_display)
10943 {
10944   unsigned char *phdr = section->start;
10945   unsigned char *limit = phdr + section->size;
10946   unsigned char *phash;
10947   unsigned char *pindex;
10948   unsigned char *ppool;
10949   unsigned int version;
10950   unsigned int ncols = 0;
10951   unsigned int nused;
10952   unsigned int nslots;
10953   unsigned int i;
10954   unsigned int j;
10955   uint64_t signature;
10956   size_t total;
10957 
10958   /* PR 17512: file: 002-168123-0.004.  */
10959   if (phdr == NULL)
10960     {
10961       warn (_("Section %s is empty\n"), section->name);
10962       return false;
10963     }
10964   /* PR 17512: file: 002-376-0.004.  */
10965   if (section->size < 24)
10966     {
10967       warn (_("Section %s is too small to contain a CU/TU header\n"),
10968               section->name);
10969       return false;
10970     }
10971 
10972   phash = phdr;
10973   SAFE_BYTE_GET_AND_INC (version, phash, 4, limit);
10974   if (version >= 2)
10975     SAFE_BYTE_GET_AND_INC (ncols, phash, 4, limit);
10976   SAFE_BYTE_GET_AND_INC (nused, phash, 4, limit);
10977   SAFE_BYTE_GET_AND_INC (nslots, phash, 4, limit);
10978 
10979   pindex = phash + (size_t) nslots * 8;
10980   ppool = pindex + (size_t) nslots * 4;
10981 
10982   if (do_display)
10983     {
10984       introduce (section, false);
10985 
10986       printf (_("  Version:                 %u\n"), version);
10987       if (version >= 2)
10988           printf (_("  Number of columns:       %u\n"), ncols);
10989       printf (_("  Number of used entries:  %u\n"), nused);
10990       printf (_("  Number of slots:         %u\n\n"), nslots);
10991     }
10992 
10993   /* PR 17531: file: 45d69832.  */
10994   if (_mul_overflow ((size_t) nslots, 12, &total)
10995       || total > (size_t) (limit - phash))
10996     {
10997       warn (ngettext ("Section %s is too small for %u slot\n",
10998                           "Section %s is too small for %u slots\n",
10999                           nslots),
11000               section->name, nslots);
11001       return false;
11002     }
11003 
11004   if (version == 1)
11005     {
11006       unsigned char *shndx_list;
11007       unsigned int shndx;
11008 
11009       if (!do_display)
11010           {
11011             prealloc_cu_tu_list ((limit - ppool) / 4);
11012             for (shndx_list = ppool + 4; shndx_list <= limit - 4; shndx_list += 4)
11013               {
11014                 shndx = byte_get (shndx_list, 4);
11015                 add_shndx_to_cu_tu_entry (shndx);
11016               }
11017             end_cu_tu_entry ();
11018           }
11019       else
11020           for (i = 0; i < nslots; i++)
11021             {
11022               SAFE_BYTE_GET (signature, phash, 8, limit);
11023               if (signature != 0)
11024                 {
11025                     SAFE_BYTE_GET (j, pindex, 4, limit);
11026                     shndx_list = ppool + j * 4;
11027                     /* PR 17531: file: 705e010d.  */
11028                     if (shndx_list < ppool)
11029                       {
11030                         warn (_("Section index pool located before start of section\n"));
11031                         return false;
11032                       }
11033 
11034                     printf (_("  [%3d] Signature:  %#" PRIx64 "  Sections: "),
11035                               i, signature);
11036                     for (;;)
11037                       {
11038                         if (shndx_list >= limit)
11039                           {
11040                               warn (_("Section %s too small for shndx pool\n"),
11041                                     section->name);
11042                               return false;
11043                           }
11044                         SAFE_BYTE_GET (shndx, shndx_list, 4, limit);
11045                         if (shndx == 0)
11046                           break;
11047                         printf (" %d", shndx);
11048                         shndx_list += 4;
11049                       }
11050                     printf ("\n");
11051                 }
11052               phash += 8;
11053               pindex += 4;
11054             }
11055     }
11056   else if (version == 2)
11057     {
11058       unsigned int val;
11059       unsigned int dw_sect;
11060       unsigned char *ph = phash;
11061       unsigned char *pi = pindex;
11062       unsigned char *poffsets = ppool + (size_t) ncols * 4;
11063       unsigned char *psizes = poffsets + (size_t) nused * ncols * 4;
11064       bool is_tu_index;
11065       struct cu_tu_set *this_set = NULL;
11066       unsigned int row;
11067       unsigned char *prow;
11068       size_t temp;
11069 
11070       is_tu_index = strcmp (section->name, ".debug_tu_index") == 0;
11071 
11072       /* PR 17531: file: 0dd159bf.
11073            Check for integer overflow (can occur when size_t is 32-bit)
11074            with overlarge ncols or nused values.  */
11075       if (nused == -1u
11076             || _mul_overflow ((size_t) ncols, 4, &temp)
11077             || _mul_overflow ((size_t) nused + 1, temp, &total)
11078             || total > (size_t) (limit - ppool)
11079             /* PR 30227: ncols could be 0.  */
11080             || _mul_overflow ((size_t) nused + 1, 4, &total)
11081             || total > (size_t) (limit - ppool))
11082           {
11083             warn (_("Section %s too small for offset and size tables\n"),
11084                     section->name);
11085             return false;
11086           }
11087 
11088       if (do_display)
11089           {
11090             printf (_("  Offset table\n"));
11091             printf ("  slot  %-16s  ",
11092                      is_tu_index ? _("signature") : _("dwo_id"));
11093           }
11094       else
11095           {
11096             if (is_tu_index)
11097               {
11098                 tu_count = nused;
11099                 tu_sets = xcalloc2 (nused, sizeof (struct cu_tu_set));
11100                 this_set = tu_sets;
11101               }
11102             else
11103               {
11104                 cu_count = nused;
11105                 cu_sets = xcalloc2 (nused, sizeof (struct cu_tu_set));
11106                 this_set = cu_sets;
11107               }
11108           }
11109 
11110       if (do_display)
11111           {
11112             for (j = 0; j < ncols; j++)
11113               {
11114                 unsigned char *p = ppool + j * 4;
11115                 SAFE_BYTE_GET (dw_sect, p, 4, limit);
11116                 printf (" %8s", get_DW_SECT_short_name (dw_sect));
11117               }
11118             printf ("\n");
11119           }
11120 
11121       for (i = 0; i < nslots; i++)
11122           {
11123             SAFE_BYTE_GET (signature, ph, 8, limit);
11124 
11125             SAFE_BYTE_GET (row, pi, 4, limit);
11126             if (row != 0)
11127               {
11128                 /* PR 17531: file: a05f6ab3.  */
11129                 if (row > nused)
11130                     {
11131                       warn (_("Row index (%u) is larger than number of used entries (%u)\n"),
11132                               row, nused);
11133                       return false;
11134                     }
11135 
11136                 if (!do_display)
11137                     {
11138                       size_t num_copy = sizeof (uint64_t);
11139 
11140                       memcpy (&this_set[row - 1].signature, ph, num_copy);
11141                     }
11142 
11143                 prow = poffsets + (row - 1) * ncols * 4;
11144                 if (do_display)
11145                     printf ("  [%3d] %#" PRIx64, i, signature);
11146                 for (j = 0; j < ncols; j++)
11147                     {
11148                       unsigned char *p = prow + j * 4;
11149                       SAFE_BYTE_GET (val, p, 4, limit);
11150                       if (do_display)
11151                         printf (" %8d", val);
11152                       else
11153                         {
11154                           p = ppool + j * 4;
11155                           SAFE_BYTE_GET (dw_sect, p, 4, limit);
11156 
11157                           /* PR 17531: file: 10796eb3.  */
11158                           if (dw_sect >= DW_SECT_MAX)
11159                               warn (_("Overlarge Dwarf section index detected: %u\n"), dw_sect);
11160                           else
11161                               this_set [row - 1].section_offsets [dw_sect] = val;
11162                         }
11163                     }
11164 
11165                 if (do_display)
11166                     printf ("\n");
11167               }
11168             ph += 8;
11169             pi += 4;
11170           }
11171 
11172       ph = phash;
11173       pi = pindex;
11174       if (do_display)
11175           {
11176             printf ("\n");
11177             printf (_("  Size table\n"));
11178             printf ("  slot  %-16s  ",
11179                      is_tu_index ? _("signature") : _("dwo_id"));
11180           }
11181 
11182       for (j = 0; j < ncols; j++)
11183           {
11184             unsigned char *p = ppool + j * 4;
11185             SAFE_BYTE_GET (val, p, 4, limit);
11186             if (do_display)
11187               printf (" %8s", get_DW_SECT_short_name (val));
11188           }
11189 
11190       if (do_display)
11191           printf ("\n");
11192 
11193       for (i = 0; i < nslots; i++)
11194           {
11195             SAFE_BYTE_GET (signature, ph, 8, limit);
11196 
11197             SAFE_BYTE_GET (row, pi, 4, limit);
11198             if (row != 0)
11199               {
11200                 prow = psizes + (row - 1) * ncols * 4;
11201 
11202                 if (do_display)
11203                     printf ("  [%3d] %#" PRIx64, i, signature);
11204 
11205                 for (j = 0; j < ncols; j++)
11206                     {
11207                       unsigned char *p = prow + j * 4;
11208 
11209                       /* PR 28645: Check for overflow.  Since we do not know how
11210                          many populated rows there will be, we cannot just
11211                          perform a single check at the start of this function.  */
11212                       if (p > (limit - 4))
11213                         {
11214                           if (do_display)
11215                               printf ("\n");
11216                           warn (_("Too many rows/columns in DWARF index section %s\n"),
11217                                   section->name);
11218                           return false;
11219                         }
11220 
11221                       SAFE_BYTE_GET (val, p, 4, limit);
11222 
11223                       if (do_display)
11224                         printf (" %8d", val);
11225                       else
11226                         {
11227                           p = ppool + j * 4;
11228                           SAFE_BYTE_GET (dw_sect, p, 4, limit);
11229                           if (dw_sect >= DW_SECT_MAX)
11230                               warn (_("Overlarge Dwarf section index detected: %u\n"), dw_sect);
11231                           else
11232                           this_set [row - 1].section_sizes [dw_sect] = val;
11233                         }
11234                     }
11235 
11236                 if (do_display)
11237                     printf ("\n");
11238               }
11239 
11240             ph += 8;
11241             pi += 4;
11242           }
11243     }
11244   else if (do_display)
11245     printf (_("  Unsupported version (%d)\n"), version);
11246 
11247   if (do_display)
11248       printf ("\n");
11249 
11250   return true;
11251 }
11252 
11253 static int cu_tu_indexes_read = -1; /* Tri-state variable.  */
11254 
11255 /* Load the CU and TU indexes if present.  This will build a list of
11256    section sets that we can use to associate a .debug_info.dwo section
11257    with its associated .debug_abbrev.dwo section in a .dwp file.  */
11258 
11259 static bool
load_cu_tu_indexes(void * file)11260 load_cu_tu_indexes (void *file)
11261 {
11262   /* If we have already loaded (or tried to load) the CU and TU indexes
11263      then do not bother to repeat the task.  */
11264   if (cu_tu_indexes_read == -1)
11265     {
11266       cu_tu_indexes_read = true;
11267 
11268       if (load_debug_section_with_follow (dwp_cu_index, file))
11269           if (! process_cu_tu_index (&debug_displays [dwp_cu_index].section, 0))
11270             cu_tu_indexes_read = false;
11271 
11272       if (load_debug_section_with_follow (dwp_tu_index, file))
11273           if (! process_cu_tu_index (&debug_displays [dwp_tu_index].section, 0))
11274             cu_tu_indexes_read = false;
11275     }
11276 
11277   return (bool) cu_tu_indexes_read;
11278 }
11279 
11280 /* Find the set of sections that includes section SHNDX.  */
11281 
11282 unsigned int *
find_cu_tu_set(void * file,unsigned int shndx)11283 find_cu_tu_set (void *file, unsigned int shndx)
11284 {
11285   unsigned int i;
11286 
11287   if (! load_cu_tu_indexes (file))
11288     return NULL;
11289 
11290   /* Find SHNDX in the shndx pool.  */
11291   for (i = 0; i < shndx_pool_used; i++)
11292     if (shndx_pool [i] == shndx)
11293       break;
11294 
11295   if (i >= shndx_pool_used)
11296     return NULL;
11297 
11298   /* Now backup to find the first entry in the set.  */
11299   while (i > 0 && shndx_pool [i - 1] != 0)
11300     i--;
11301 
11302   return shndx_pool + i;
11303 }
11304 
11305 /* Display a .debug_cu_index or .debug_tu_index section.  */
11306 
11307 static int
display_cu_index(struct dwarf_section * section,void * file ATTRIBUTE_UNUSED)11308 display_cu_index (struct dwarf_section *section, void *file ATTRIBUTE_UNUSED)
11309 {
11310   return process_cu_tu_index (section, 1);
11311 }
11312 
11313 static int
display_debug_not_supported(struct dwarf_section * section,void * file ATTRIBUTE_UNUSED)11314 display_debug_not_supported (struct dwarf_section *section,
11315                                    void *file ATTRIBUTE_UNUSED)
11316 {
11317   printf (_("Displaying the debug contents of section %s is not yet supported.\n"),
11318               section->name);
11319 
11320   return 1;
11321 }
11322 
11323 /* Like malloc, but takes two parameters like calloc.
11324    Verifies that the first parameter is not too large.
11325    Note: does *not* initialise the allocated memory to zero.  */
11326 
11327 void *
cmalloc(uint64_t nmemb,size_t size)11328 cmalloc (uint64_t nmemb, size_t size)
11329 {
11330   /* Check for overflow.  */
11331   if (nmemb >= ~(size_t) 0 / size)
11332     return NULL;
11333 
11334   return xmalloc (nmemb * size);
11335 }
11336 
11337 /* Like xmalloc, but takes two parameters like calloc.
11338    Verifies that the first parameter is not too large.
11339    Note: does *not* initialise the allocated memory to zero.  */
11340 
11341 void *
xcmalloc(uint64_t nmemb,size_t size)11342 xcmalloc (uint64_t nmemb, size_t size)
11343 {
11344   /* Check for overflow.  */
11345   if (nmemb >= ~(size_t) 0 / size)
11346     {
11347       fprintf (stderr,
11348                  _("Attempt to allocate an array with an excessive number of elements: %#" PRIx64 "\n"),
11349                  nmemb);
11350       xexit (1);
11351     }
11352 
11353   return xmalloc (nmemb * size);
11354 }
11355 
11356 /* Like xrealloc, but takes three parameters.
11357    Verifies that the second parameter is not too large.
11358    Note: does *not* initialise any new memory to zero.  */
11359 
11360 void *
xcrealloc(void * ptr,uint64_t nmemb,size_t size)11361 xcrealloc (void *ptr, uint64_t nmemb, size_t size)
11362 {
11363   /* Check for overflow.  */
11364   if (nmemb >= ~(size_t) 0 / size)
11365     {
11366       error (_("Attempt to re-allocate an array with an excessive number of elements: %#" PRIx64 "\n"),
11367                nmemb);
11368       xexit (1);
11369     }
11370 
11371   return xrealloc (ptr, nmemb * size);
11372 }
11373 
11374 /* Like xcalloc, but verifies that the first parameter is not too large.  */
11375 
11376 void *
xcalloc2(uint64_t nmemb,size_t size)11377 xcalloc2 (uint64_t nmemb, size_t size)
11378 {
11379   /* Check for overflow.  */
11380   if (nmemb >= ~(size_t) 0 / size)
11381     {
11382       error (_("Attempt to allocate a zero'ed array with an excessive number of elements: %#" PRIx64 "\n"),
11383                nmemb);
11384       xexit (1);
11385     }
11386 
11387   return xcalloc (nmemb, size);
11388 }
11389 
11390 static unsigned long
calc_gnu_debuglink_crc32(unsigned long crc,const unsigned char * buf,size_t len)11391 calc_gnu_debuglink_crc32 (unsigned long crc,
11392                                 const unsigned char *buf,
11393                                 size_t len)
11394 {
11395   static const unsigned long crc32_table[256] =
11396     {
11397       0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419,
11398       0x706af48f, 0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4,
11399       0xe0d5e91e, 0x97d2d988, 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07,
11400       0x90bf1d91, 0x1db71064, 0x6ab020f2, 0xf3b97148, 0x84be41de,
11401       0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7, 0x136c9856,
11402       0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9,
11403       0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4,
11404       0xa2677172, 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b,
11405       0x35b5a8fa, 0x42b2986c, 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3,
11406       0x45df5c75, 0xdcd60dcf, 0xabd13d59, 0x26d930ac, 0x51de003a,
11407       0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423, 0xcfba9599,
11408       0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924,
11409       0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190,
11410       0x01db7106, 0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f,
11411       0x9fbfe4a5, 0xe8b8d433, 0x7807c9a2, 0x0f00f934, 0x9609a88e,
11412       0xe10e9818, 0x7f6a0dbb, 0x086d3d2d, 0x91646c97, 0xe6635c01,
11413       0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e, 0x6c0695ed,
11414       0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950,
11415       0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3,
11416       0xfbd44c65, 0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2,
11417       0x4adfa541, 0x3dd895d7, 0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a,
11418       0x346ed9fc, 0xad678846, 0xda60b8d0, 0x44042d73, 0x33031de5,
11419       0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa, 0xbe0b1010,
11420       0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f,
11421       0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17,
11422       0x2eb40d81, 0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6,
11423       0x03b6e20c, 0x74b1d29a, 0xead54739, 0x9dd277af, 0x04db2615,
11424       0x73dc1683, 0xe3630b12, 0x94643b84, 0x0d6d6a3e, 0x7a6a5aa8,
11425       0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1, 0xf00f9344,
11426       0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb,
11427       0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a,
11428       0x67dd4acc, 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5,
11429       0xd6d6a3e8, 0xa1d1937e, 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1,
11430       0xa6bc5767, 0x3fb506dd, 0x48b2364b, 0xd80d2bda, 0xaf0a1b4c,
11431       0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55, 0x316e8eef,
11432       0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236,
11433       0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe,
11434       0xb2bd0b28, 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31,
11435       0x2cd99e8b, 0x5bdeae1d, 0x9b64c2b0, 0xec63f226, 0x756aa39c,
11436       0x026d930a, 0x9c0906a9, 0xeb0e363f, 0x72076785, 0x05005713,
11437       0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38, 0x92d28e9b,
11438       0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242,
11439       0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1,
11440       0x18b74777, 0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c,
11441       0x8f659eff, 0xf862ae69, 0x616bffd3, 0x166ccf45, 0xa00ae278,
11442       0xd70dd2ee, 0x4e048354, 0x3903b3c2, 0xa7672661, 0xd06016f7,
11443       0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc, 0x40df0b66,
11444       0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9,
11445       0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605,
11446       0xcdd70693, 0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8,
11447       0x5d681b02, 0x2a6f2b94, 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b,
11448       0x2d02ef8d
11449     };
11450   const unsigned char *end;
11451 
11452   crc = ~crc & 0xffffffff;
11453   for (end = buf + len; buf < end; ++ buf)
11454     crc = crc32_table[(crc ^ *buf) & 0xff] ^ (crc >> 8);
11455   return ~crc & 0xffffffff;
11456 }
11457 
11458 typedef bool (*check_func_type) (const char *, void *);
11459 typedef const char *(* parse_func_type) (struct dwarf_section *, void *);
11460 
11461 static bool
check_gnu_debuglink(const char * pathname,void * crc_pointer)11462 check_gnu_debuglink (const char * pathname, void * crc_pointer)
11463 {
11464   static unsigned char buffer[8 * 1024];
11465   FILE *f;
11466   size_t count;
11467   unsigned long crc = 0;
11468   void *sep_data;
11469 
11470   sep_data = open_debug_file (pathname);
11471   if (sep_data == NULL)
11472     return false;
11473 
11474   /* Yes - we are opening the file twice...  */
11475   f = fopen (pathname, "rb");
11476   if (f == NULL)
11477     {
11478       /* Paranoia: This should never happen.  */
11479       close_debug_file (sep_data);
11480       warn (_("Unable to reopen separate debug info file: %s\n"), pathname);
11481       return false;
11482     }
11483 
11484   while ((count = fread (buffer, 1, sizeof (buffer), f)) > 0)
11485     crc = calc_gnu_debuglink_crc32 (crc, buffer, count);
11486 
11487   fclose (f);
11488 
11489   if (crc != * (unsigned long *) crc_pointer)
11490     {
11491       close_debug_file (sep_data);
11492       warn (_("Separate debug info file %s found, but CRC does not match - ignoring\n"),
11493               pathname);
11494       return false;
11495     }
11496 
11497   return true;
11498 }
11499 
11500 static const char *
parse_gnu_debuglink(struct dwarf_section * section,void * data)11501 parse_gnu_debuglink (struct dwarf_section * section, void * data)
11502 {
11503   const char *     name;
11504   unsigned int     crc_offset;
11505   unsigned long *  crc32 = (unsigned long *) data;
11506 
11507   /* The name is first.
11508      The CRC value is stored after the filename, aligned up to 4 bytes.  */
11509   name = (const char *) section->start;
11510 
11511   crc_offset = strnlen (name, section->size) + 1;
11512   if (crc_offset == 1)
11513     return NULL;
11514   crc_offset = (crc_offset + 3) & ~3;
11515   if (crc_offset + 4 > section->size)
11516     return NULL;
11517 
11518   * crc32 = byte_get (section->start + crc_offset, 4);
11519   return name;
11520 }
11521 
11522 static bool
check_gnu_debugaltlink(const char * filename,void * data ATTRIBUTE_UNUSED)11523 check_gnu_debugaltlink (const char * filename, void * data ATTRIBUTE_UNUSED)
11524 {
11525   void * sep_data = open_debug_file (filename);
11526 
11527   if (sep_data == NULL)
11528     return false;
11529 
11530   /* FIXME: We should now extract the build-id in the separate file
11531      and check it...  */
11532 
11533   return true;
11534 }
11535 
11536 typedef struct build_id_data
11537 {
11538   size_t len;
11539   const unsigned char *data;
11540 } Build_id_data;
11541 
11542 static const char *
parse_gnu_debugaltlink(struct dwarf_section * section,void * data)11543 parse_gnu_debugaltlink (struct dwarf_section * section, void * data)
11544 {
11545   const char *name;
11546   size_t namelen;
11547   size_t id_len;
11548   Build_id_data *build_id_data;
11549 
11550   /* The name is first.
11551      The build-id follows immediately, with no padding, up to the section's end.  */
11552 
11553   name = (const char *) section->start;
11554   namelen = strnlen (name, section->size) + 1;
11555   if (namelen == 1)
11556     return NULL;
11557   if (namelen >= section->size)
11558     return NULL;
11559 
11560   id_len = section->size - namelen;
11561   if (id_len < 0x14)
11562     return NULL;
11563 
11564   build_id_data = (Build_id_data *) data;
11565   build_id_data->len = id_len;
11566   build_id_data->data = section->start + namelen;
11567 
11568   return name;
11569 }
11570 
11571 static void
add_separate_debug_file(const char * filename,void * handle)11572 add_separate_debug_file (const char * filename, void * handle)
11573 {
11574   separate_info * i = xmalloc (sizeof * i);
11575 
11576   i->filename = filename;
11577   i->handle   = handle;
11578   i->next     = first_separate_info;
11579   first_separate_info = i;
11580 }
11581 
11582 #if HAVE_LIBDEBUGINFOD
11583 /* Query debuginfod servers for the target debuglink or debugaltlink
11584    file. If successful, store the path of the file in filename and
11585    return TRUE, otherwise return FALSE.  */
11586 
11587 static bool
debuginfod_fetch_separate_debug_info(struct dwarf_section * section,char ** filename,void * file)11588 debuginfod_fetch_separate_debug_info (struct dwarf_section * section,
11589                                               char ** filename,
11590                                               void * file)
11591 {
11592   size_t build_id_len;
11593   unsigned char * build_id;
11594 
11595   if (strcmp (section->uncompressed_name, ".gnu_debuglink") == 0)
11596     {
11597       /* Get the build-id of file.  */
11598       build_id = get_build_id (file);
11599       build_id_len = 0;
11600     }
11601   else if (strcmp (section->uncompressed_name, ".gnu_debugaltlink") == 0)
11602     {
11603       /* Get the build-id of the debugaltlink file.  */
11604       unsigned int filelen;
11605 
11606       filelen = strnlen ((const char *)section->start, section->size);
11607       if (filelen == section->size)
11608           /* Corrupt debugaltlink.  */
11609           return false;
11610 
11611       build_id = section->start + filelen + 1;
11612       build_id_len = section->size - (filelen + 1);
11613 
11614       if (build_id_len == 0)
11615           return false;
11616     }
11617   else
11618     return false;
11619 
11620   if (build_id)
11621     {
11622       int fd;
11623       debuginfod_client * client;
11624 
11625       client = debuginfod_begin ();
11626       if (client == NULL)
11627           return false;
11628 
11629       /* Query debuginfod servers for the target file. If found its path
11630            will be stored in filename.  */
11631       fd = debuginfod_find_debuginfo (client, build_id, build_id_len, filename);
11632       debuginfod_end (client);
11633 
11634       /* Only free build_id if we allocated space for a hex string
11635            in get_build_id ().  */
11636       if (build_id_len == 0)
11637           free (build_id);
11638 
11639       if (fd >= 0)
11640           {
11641             /* File successfully retrieved. Close fd since we want to
11642                use open_debug_file () on filename instead.  */
11643             close (fd);
11644             return true;
11645           }
11646     }
11647 
11648   return false;
11649 }
11650 #endif /* HAVE_LIBDEBUGINFOD  */
11651 
11652 static void *
load_separate_debug_info(const char * main_filename,struct dwarf_section * xlink,parse_func_type parse_func,check_func_type check_func,void * func_data,void * file ATTRIBUTE_UNUSED)11653 load_separate_debug_info (const char *            main_filename,
11654                                 struct dwarf_section *  xlink,
11655                                 parse_func_type         parse_func,
11656                                 check_func_type         check_func,
11657                                 void *                  func_data,
11658                                 void *                  file ATTRIBUTE_UNUSED)
11659 {
11660   const char *   separate_filename;
11661   char *         debug_filename;
11662   char *         canon_dir;
11663   size_t         canon_dirlen;
11664   size_t         dirlen;
11665   char *         canon_filename;
11666   char *         canon_debug_filename;
11667   bool               self;
11668 
11669   if ((separate_filename = parse_func (xlink, func_data)) == NULL)
11670     {
11671       warn (_("Corrupt debuglink section: %s\n"),
11672               xlink->name ? xlink->name : xlink->uncompressed_name);
11673       return NULL;
11674     }
11675 
11676   /* Attempt to locate the separate file.
11677      This should duplicate the logic in bfd/opncls.c:find_separate_debug_file().  */
11678 
11679   canon_filename = lrealpath (main_filename);
11680   canon_dir = xstrdup (canon_filename);
11681 
11682   for (canon_dirlen = strlen (canon_dir); canon_dirlen > 0; canon_dirlen--)
11683     if (IS_DIR_SEPARATOR (canon_dir[canon_dirlen - 1]))
11684       break;
11685   canon_dir[canon_dirlen] = '\0';
11686 
11687 #ifndef DEBUGDIR
11688 #define DEBUGDIR "/lib/debug"
11689 #endif
11690 #ifndef EXTRA_DEBUG_ROOT1
11691 #define EXTRA_DEBUG_ROOT1 "/usr/lib/debug"
11692 #endif
11693 #ifndef EXTRA_DEBUG_ROOT2
11694 #define EXTRA_DEBUG_ROOT2 "/usr/lib/debug/usr"
11695 #endif
11696 
11697   debug_filename = (char *) malloc (strlen (DEBUGDIR) + 1
11698                                             + canon_dirlen
11699                                             + strlen (".debug/")
11700 #ifdef EXTRA_DEBUG_ROOT1
11701                                             + strlen (EXTRA_DEBUG_ROOT1)
11702 #endif
11703 #ifdef EXTRA_DEBUG_ROOT2
11704                                             + strlen (EXTRA_DEBUG_ROOT2)
11705 #endif
11706                                             + strlen (separate_filename)
11707                                             + 1);
11708   if (debug_filename == NULL)
11709     {
11710       warn (_("Out of memory"));
11711       free (canon_dir);
11712       free (canon_filename);
11713       return NULL;
11714     }
11715 
11716   /* First try in the current directory.  */
11717   sprintf (debug_filename, "%s", separate_filename);
11718   if (check_func (debug_filename, func_data))
11719     goto found;
11720 
11721   /* Then try in a subdirectory called .debug.  */
11722   sprintf (debug_filename, ".debug/%s", separate_filename);
11723   if (check_func (debug_filename, func_data))
11724     goto found;
11725 
11726   /* Then try in the same directory as the original file.  */
11727   sprintf (debug_filename, "%s%s", canon_dir, separate_filename);
11728   if (check_func (debug_filename, func_data))
11729     goto found;
11730 
11731   /* And the .debug subdirectory of that directory.  */
11732   sprintf (debug_filename, "%s.debug/%s", canon_dir, separate_filename);
11733   if (check_func (debug_filename, func_data))
11734     goto found;
11735 
11736 #ifdef EXTRA_DEBUG_ROOT1
11737   /* Try the first extra debug file root.  */
11738   sprintf (debug_filename, "%s/%s", EXTRA_DEBUG_ROOT1, separate_filename);
11739   if (check_func (debug_filename, func_data))
11740     goto found;
11741 
11742   /* Try the first extra debug file root.  */
11743   sprintf (debug_filename, "%s/%s/%s", EXTRA_DEBUG_ROOT1, canon_dir, separate_filename);
11744   if (check_func (debug_filename, func_data))
11745     goto found;
11746 #endif
11747 
11748 #ifdef EXTRA_DEBUG_ROOT2
11749   /* Try the second extra debug file root.  */
11750   sprintf (debug_filename, "%s/%s", EXTRA_DEBUG_ROOT2, separate_filename);
11751   if (check_func (debug_filename, func_data))
11752     goto found;
11753 #endif
11754 
11755   /* Then try in the global debug_filename directory.  */
11756   strcpy (debug_filename, DEBUGDIR);
11757   dirlen = strlen (DEBUGDIR) - 1;
11758   if (dirlen > 0 && DEBUGDIR[dirlen] != '/')
11759     strcat (debug_filename, "/");
11760   strcat (debug_filename, (const char *) separate_filename);
11761 
11762   if (check_func (debug_filename, func_data))
11763     goto found;
11764 
11765 #if HAVE_LIBDEBUGINFOD
11766   {
11767     char * tmp_filename;
11768 
11769     if (use_debuginfod
11770           && debuginfod_fetch_separate_debug_info (xlink,
11771                                                              & tmp_filename,
11772                                                              file))
11773       {
11774           /* File successfully downloaded from server, replace
11775              debug_filename with the file's path.  */
11776           free (debug_filename);
11777           debug_filename = tmp_filename;
11778           goto found;
11779       }
11780   }
11781 #endif
11782 
11783   if (do_debug_links)
11784     {
11785       /* Failed to find the file.  */
11786       warn (_("could not find separate debug file '%s'\n"),
11787               separate_filename);
11788       warn (_("tried: %s\n"), debug_filename);
11789 
11790 #ifdef EXTRA_DEBUG_ROOT2
11791       sprintf (debug_filename, "%s/%s", EXTRA_DEBUG_ROOT2,
11792                  separate_filename);
11793       warn (_("tried: %s\n"), debug_filename);
11794 #endif
11795 
11796 #ifdef EXTRA_DEBUG_ROOT1
11797       sprintf (debug_filename, "%s/%s/%s", EXTRA_DEBUG_ROOT1,
11798                  canon_dir, separate_filename);
11799       warn (_("tried: %s\n"), debug_filename);
11800 
11801       sprintf (debug_filename, "%s/%s", EXTRA_DEBUG_ROOT1,
11802                  separate_filename);
11803       warn (_("tried: %s\n"), debug_filename);
11804 #endif
11805 
11806       sprintf (debug_filename, "%s.debug/%s", canon_dir,
11807                  separate_filename);
11808       warn (_("tried: %s\n"), debug_filename);
11809 
11810       sprintf (debug_filename, "%s%s", canon_dir, separate_filename);
11811       warn (_("tried: %s\n"), debug_filename);
11812 
11813       sprintf (debug_filename, ".debug/%s", separate_filename);
11814       warn (_("tried: %s\n"), debug_filename);
11815 
11816       sprintf (debug_filename, "%s", separate_filename);
11817       warn (_("tried: %s\n"), debug_filename);
11818 
11819 #if HAVE_LIBDEBUGINFOD
11820       if (use_debuginfod)
11821           {
11822             char *urls = getenv (DEBUGINFOD_URLS_ENV_VAR);
11823 
11824             if (urls == NULL)
11825               urls = "";
11826 
11827             warn (_("tried: DEBUGINFOD_URLS=%s\n"), urls);
11828           }
11829 #endif
11830     }
11831 
11832   free (canon_dir);
11833   free (debug_filename);
11834   free (canon_filename);
11835   return NULL;
11836 
11837  found:
11838   free (canon_dir);
11839 
11840   canon_debug_filename = lrealpath (debug_filename);
11841   self = strcmp (canon_debug_filename, canon_filename) == 0;
11842   free (canon_filename);
11843   free (canon_debug_filename);
11844   if (self)
11845     {
11846       free (debug_filename);
11847       return NULL;
11848     }
11849 
11850   void * debug_handle;
11851 
11852   /* Now open the file.... */
11853   if ((debug_handle = open_debug_file (debug_filename)) == NULL)
11854     {
11855       warn (_("failed to open separate debug file: %s\n"), debug_filename);
11856       free (debug_filename);
11857       return NULL;
11858     }
11859 
11860   /* FIXME: We do not check to see if there are any other separate debug info
11861      files that would also match.  */
11862 
11863   if (do_debug_links)
11864     printf (_("\n%s: Found separate debug info file: %s\n"), main_filename, debug_filename);
11865   add_separate_debug_file (debug_filename, debug_handle);
11866 
11867   /* Do not free debug_filename - it might be referenced inside
11868      the structure returned by open_debug_file().  */
11869   return debug_handle;
11870 }
11871 
11872 /* Attempt to load a separate dwarf object file.  */
11873 
11874 static void *
load_dwo_file(const char * main_filename,const char * name,const char * dir,const char * id ATTRIBUTE_UNUSED)11875 load_dwo_file (const char * main_filename, const char * name, const char * dir, const char * id ATTRIBUTE_UNUSED)
11876 {
11877   char * separate_filename;
11878   void * separate_handle;
11879 
11880   if (IS_ABSOLUTE_PATH (name))
11881     separate_filename = strdup (name);
11882   else
11883     /* FIXME: Skip adding / if dwo_dir ends in /.  */
11884     separate_filename = concat (dir, "/", name, NULL);
11885   if (separate_filename == NULL)
11886     {
11887       warn (_("Out of memory allocating dwo filename\n"));
11888       return NULL;
11889     }
11890 
11891   if ((separate_handle = open_debug_file (separate_filename)) == NULL)
11892     {
11893       warn (_("Unable to load dwo file: %s\n"), separate_filename);
11894       free (separate_filename);
11895       return NULL;
11896     }
11897 
11898   /* FIXME: We should check the dwo_id.  */
11899 
11900   printf (_("%s: Found separate debug object file: %s\n\n"), main_filename, separate_filename);
11901 
11902   add_separate_debug_file (separate_filename, separate_handle);
11903   /* Note - separate_filename will be freed in free_debug_memory().  */
11904   return separate_handle;
11905 }
11906 
11907 static void *
try_build_id_prefix(const char * prefix,char * filename,const unsigned char * data,unsigned long id_len)11908 try_build_id_prefix (const char * prefix, char * filename, const unsigned char * data, unsigned long id_len)
11909 {
11910   char * f = filename;
11911 
11912   f += sprintf (f, "%s.build-id/%02x/", prefix, (unsigned) *data++);
11913   id_len --;
11914   while (id_len --)
11915     f += sprintf (f, "%02x", (unsigned) *data++);
11916   strcpy (f, ".debug");
11917 
11918   return open_debug_file (filename);
11919 }
11920 
11921 /* Try to load a debug file based upon the build-id held in the .note.gnu.build-id section.  */
11922 
11923 static void
load_build_id_debug_file(const char * main_filename ATTRIBUTE_UNUSED,void * main_file)11924 load_build_id_debug_file (const char * main_filename ATTRIBUTE_UNUSED, void * main_file)
11925 {
11926   if (! load_debug_section (note_gnu_build_id, main_file))
11927     return; /* No .note.gnu.build-id section.  */
11928 
11929   struct dwarf_section * section = & debug_displays [note_gnu_build_id].section;
11930   if (section == NULL)
11931     {
11932       warn (_("Unable to load the .note.gnu.build-id section\n"));
11933       return;
11934     }
11935 
11936   if (section->start == NULL || section->size < 0x18)
11937     {
11938       warn (_(".note.gnu.build-id section is corrupt/empty\n"));
11939       return;
11940     }
11941 
11942   /* In theory we should extract the contents of the section into
11943      a note structure and then check the fields.  For now though
11944      just use hard coded offsets instead:
11945 
11946        Field  Bytes    Contents
11947           NSize  0...3   4
11948           DSize  4...7   8+
11949           Type   8..11   3  (NT_GNU_BUILD_ID)
11950           Name   12.15   GNU\0
11951           Data   16....   */
11952 
11953   /* FIXME: Check the name size, name and type fields.  */
11954 
11955   unsigned long build_id_size;
11956   build_id_size = byte_get (section->start + 4, 4);
11957   if (build_id_size < 8)
11958     {
11959       warn (_(".note.gnu.build-id data size is too small\n"));
11960       return;
11961     }
11962 
11963   if (build_id_size > (section->size - 16))
11964     {
11965       warn (_(".note.gnu.build-id data size is too big\n"));
11966       return;
11967     }
11968 
11969   char * filename;
11970   filename = xmalloc (strlen (".build-id/")
11971                           + build_id_size * 2 + 2
11972                           + strlen (".debug")
11973                           /* The next string should be the same as the longest
11974                                name found in the prefixes[] array below.  */
11975                           + strlen ("/usrlib64/debug/usr")
11976                           + 1);
11977   void * handle;
11978 
11979   static const char * prefixes[] =
11980     {
11981       "",
11982       ".debug/",
11983       "/usr/lib/debug/",
11984       "/usr/lib/debug/usr/",
11985       "/usr/lib64/debug/",
11986       "/usr/lib64/debug/usr"
11987     };
11988   long unsigned int i;
11989 
11990   for (i = 0; i < ARRAY_SIZE (prefixes); i++)
11991     {
11992       handle = try_build_id_prefix (prefixes[i], filename,
11993                                             section->start + 16, build_id_size);
11994       if (handle != NULL)
11995           break;
11996     }
11997   /* FIXME: TYhe BFD library also tries a global debugfile directory prefix.  */
11998   if (handle == NULL)
11999     {
12000       /* Failed to find a debug file associated with the build-id.
12001            This is not an error however, rather it just means that
12002            the debug info has probably not been loaded on the system,
12003            or that another method is being used to link to the debug
12004            info.  */
12005       free (filename);
12006       return;
12007     }
12008 
12009   add_separate_debug_file (filename, handle);
12010 }
12011 
12012 /* Try to load a debug file pointed to by the .debug_sup section.  */
12013 
12014 static void
load_debug_sup_file(const char * main_filename,void * file)12015 load_debug_sup_file (const char * main_filename, void * file)
12016 {
12017   if (! load_debug_section (debug_sup, file))
12018     return; /* No .debug_sup section.  */
12019 
12020   struct dwarf_section * section;
12021   section = & debug_displays [debug_sup].section;
12022   assert (section != NULL);
12023 
12024   if (section->start == NULL || section->size < 5)
12025     {
12026       warn (_(".debug_sup section is corrupt/empty\n"));
12027       return;
12028     }
12029 
12030   if (section->start[2] != 0)
12031     return; /* This is a supplementary file.  */
12032 
12033   const char * filename = (const char *) section->start + 3;
12034   if (strnlen (filename, section->size - 3) == section->size - 3)
12035     {
12036       warn (_("filename in .debug_sup section is corrupt\n"));
12037       return;
12038     }
12039 
12040   if (filename[0] != '/' && strchr (main_filename, '/'))
12041     {
12042       char * new_name;
12043       int new_len;
12044 
12045       new_len = asprintf (& new_name, "%.*s/%s",
12046                                 (int) (strrchr (main_filename, '/') - main_filename),
12047                                 main_filename,
12048                                 filename);
12049       if (new_len < 3)
12050           {
12051             warn (_("unable to construct path for supplementary debug file"));
12052             if (new_len > -1)
12053               free (new_name);
12054             return;
12055           }
12056       filename = new_name;
12057     }
12058   else
12059     {
12060       /* PR 27796: Make sure that we pass a filename that can be free'd to
12061            add_separate_debug_file().  */
12062       filename = strdup (filename);
12063       if (filename == NULL)
12064           {
12065             warn (_("out of memory constructing filename for .debug_sup link\n"));
12066             return;
12067           }
12068     }
12069 
12070   void * handle = open_debug_file (filename);
12071   if (handle == NULL)
12072     {
12073       warn (_("unable to open file '%s' referenced from .debug_sup section\n"), filename);
12074       free ((void *) filename);
12075       return;
12076     }
12077 
12078   printf (_("%s: Found supplementary debug file: %s\n\n"), main_filename, filename);
12079 
12080   /* FIXME: Compare the checksums, if present.  */
12081   add_separate_debug_file (filename, handle);
12082 }
12083 
12084 /* Load a debuglink section and/or a debugaltlink section, if either are present.
12085    Recursively check the loaded files for more of these sections.
12086    Also follow any links in .debug_sup sections.
12087    FIXME: Should also check for DWO_* entries in the newly loaded files.  */
12088 
12089 static void
check_for_and_load_links(void * file,const char * filename)12090 check_for_and_load_links (void * file, const char * filename)
12091 {
12092   void * handle = NULL;
12093 
12094   if (load_debug_section (gnu_debugaltlink, file))
12095     {
12096       Build_id_data build_id_data;
12097 
12098       handle = load_separate_debug_info (filename,
12099                                                    & debug_displays[gnu_debugaltlink].section,
12100                                                    parse_gnu_debugaltlink,
12101                                                    check_gnu_debugaltlink,
12102                                                    & build_id_data,
12103                                                    file);
12104       if (handle)
12105           {
12106             assert (handle == first_separate_info->handle);
12107             check_for_and_load_links (first_separate_info->handle,
12108                                             first_separate_info->filename);
12109           }
12110     }
12111 
12112   if (load_debug_section (gnu_debuglink, file))
12113     {
12114       unsigned long crc32;
12115 
12116       handle = load_separate_debug_info (filename,
12117                                                    & debug_displays[gnu_debuglink].section,
12118                                                    parse_gnu_debuglink,
12119                                                    check_gnu_debuglink,
12120                                                    & crc32,
12121                                                    file);
12122       if (handle)
12123           {
12124             assert (handle == first_separate_info->handle);
12125             check_for_and_load_links (first_separate_info->handle,
12126                                             first_separate_info->filename);
12127           }
12128     }
12129 
12130   load_debug_sup_file (filename, file);
12131 
12132   load_build_id_debug_file (filename, file);
12133 }
12134 
12135 /* Load the separate debug info file(s) attached to FILE, if any exist.
12136    Returns TRUE if any were found, FALSE otherwise.
12137    If TRUE is returned then the linked list starting at first_separate_info
12138    will be populated with open file handles.  */
12139 
12140 bool
load_separate_debug_files(void * file,const char * filename)12141 load_separate_debug_files (void * file, const char * filename)
12142 {
12143   /* Skip this operation if we are not interested in debug links.  */
12144   if (! do_follow_links && ! do_debug_links)
12145     return false;
12146 
12147   /* See if there are any dwo links.  */
12148   if (load_debug_section (str, file)
12149       && load_debug_section (abbrev, file)
12150       && load_debug_section (info, file))
12151     {
12152       /* Load the .debug_addr section, if it exists.  */
12153       load_debug_section (debug_addr, file);
12154       /* Load the .debug_str_offsets section, if it exists.  */
12155       load_debug_section (str_index, file);
12156       /* Load the .debug_loclists section, if it exists.  */
12157       load_debug_section (loclists, file);
12158       /* Load the .debug_rnglists section, if it exists.  */
12159       load_debug_section (rnglists, file);
12160 
12161       free_dwo_info ();
12162 
12163       if (process_debug_info (& debug_displays[info].section, file, abbrev,
12164                                     true, false))
12165           {
12166             bool introduced = false;
12167             dwo_info *dwinfo;
12168             const char *dir = NULL;
12169             const char *id = NULL;
12170             const char *name = NULL;
12171 
12172             for (dwinfo = first_dwo_info; dwinfo != NULL; dwinfo = dwinfo->next)
12173               {
12174                 /* Accumulate NAME, DIR and ID fields.  */
12175                 switch (dwinfo->type)
12176                     {
12177                     case DWO_NAME:
12178                       if (name != NULL)
12179                         warn (_("Multiple DWO_NAMEs encountered for the same CU\n"));
12180                       name = dwinfo->value;
12181                       break;
12182 
12183                     case DWO_DIR:
12184                       /* There can be multiple DW_AT_comp_dir entries in a CU,
12185                          so do not complain.  */
12186                       dir = dwinfo->value;
12187                       break;
12188 
12189                     case DWO_ID:
12190                       if (id != NULL)
12191                         warn (_("multiple DWO_IDs encountered for the same CU\n"));
12192                       id = dwinfo->value;
12193                       break;
12194 
12195                     default:
12196                       error (_("Unexpected DWO INFO type"));
12197                       break;
12198                     }
12199 
12200                 /* If we have reached the end of our list, or we are changing
12201                      CUs, then display the information that we have accumulated
12202                      so far.  */
12203                 if (name != NULL
12204                       && (dwinfo->next == NULL
12205                           || dwinfo->next->cu_offset != dwinfo->cu_offset))
12206                     {
12207                       if (do_debug_links)
12208                         {
12209                           if (! introduced)
12210                               {
12211                                 printf (_("The %s section contains link(s) to dwo file(s):\n\n"),
12212                                           debug_displays [info].section.uncompressed_name);
12213                                 introduced = true;
12214                               }
12215 
12216                           printf (_("  Name:      %s\n"), name);
12217                           printf (_("  Directory: %s\n"), dir ? dir : _("<not-found>"));
12218                           if (id != NULL)
12219                               display_data (printf (_("  ID:       ")), (unsigned char *) id, 8);
12220                           else if (debug_information[0].dwarf_version != 5)
12221                               printf (_("  ID:        <not specified>\n"));
12222                           printf ("\n\n");
12223                         }
12224 
12225                       if (do_follow_links)
12226                         load_dwo_file (filename, name, dir, id);
12227 
12228                       name = dir = id = NULL;
12229                     }
12230               }
12231           }
12232     }
12233 
12234   if (! do_follow_links)
12235     /* The other debug links will be displayed by display_debug_links()
12236        so we do not need to do any further processing here.  */
12237     return false;
12238 
12239   /* FIXME: We do not check for the presence of both link sections in the same file.  */
12240   /* FIXME: We do not check for the presence of multiple, same-name debuglink sections.  */
12241   /* FIXME: We do not check for the presence of a dwo link as well as a debuglink.  */
12242 
12243   check_for_and_load_links (file, filename);
12244   if (first_separate_info != NULL)
12245     return true;
12246 
12247   do_follow_links = 0;
12248   return false;
12249 }
12250 
12251 void
free_debug_memory(void)12252 free_debug_memory (void)
12253 {
12254   unsigned int i;
12255 
12256   free_all_abbrevs ();
12257 
12258   free (shndx_pool);
12259   shndx_pool = NULL;
12260   shndx_pool_size = 0;
12261   shndx_pool_used = 0;
12262   free (cu_sets);
12263   cu_sets = NULL;
12264   cu_count = 0;
12265   free (tu_sets);
12266   tu_sets = NULL;
12267   tu_count = 0;
12268 
12269   memset (level_type_signed, 0, sizeof level_type_signed);
12270   cu_tu_indexes_read = -1;
12271 
12272   for (i = 0; i < max; i++)
12273     free_debug_section ((enum dwarf_section_display_enum) i);
12274 
12275   if (debug_information != NULL)
12276     {
12277       for (i = 0; i < alloc_num_debug_info_entries; i++)
12278           free_debug_information (&debug_information[i]);
12279       free (debug_information);
12280       debug_information = NULL;
12281       alloc_num_debug_info_entries = num_debug_info_entries = 0;
12282     }
12283 
12284   separate_info * d;
12285   separate_info * next;
12286 
12287   for (d = first_separate_info; d != NULL; d = next)
12288     {
12289       close_debug_file (d->handle);
12290       free ((void *) d->filename);
12291       next = d->next;
12292       free ((void *) d);
12293     }
12294   first_separate_info = NULL;
12295 
12296   free_dwo_info ();
12297 }
12298 
12299 typedef struct
12300 {
12301   const char letter;
12302   const char *option;
12303   int *variable;
12304   int val;
12305 } debug_dump_long_opts;
12306 
12307 static const debug_dump_long_opts debug_option_table[] =
12308 {
12309   { 'A', "addr", &do_debug_addr, 1 },
12310   { 'a', "abbrev", &do_debug_abbrevs, 1 },
12311   { 'c', "cu_index", &do_debug_cu_index, 1 },
12312 #ifdef HAVE_LIBDEBUGINFOD
12313   { 'D', "use-debuginfod", &use_debuginfod, 1 },
12314   { 'E', "do-not-use-debuginfod", &use_debuginfod, 0 },
12315 #endif
12316   { 'F', "frames-interp", &do_debug_frames_interp, 1 },
12317   { 'f', "frames", &do_debug_frames, 1 },
12318   { 'g', "gdb_index", &do_gdb_index, 1 },
12319   { 'i', "info", &do_debug_info, 1 },
12320   { 'K', "follow-links", &do_follow_links, 1 },
12321   { 'k', "links", &do_debug_links, 1 },
12322   { 'L', "decodedline", &do_debug_lines, FLAG_DEBUG_LINES_DECODED },
12323   { 'l', "rawline", &do_debug_lines, FLAG_DEBUG_LINES_RAW },
12324   /* For compatibility with earlier versions of readelf.  */
12325   { 'l', "line", &do_debug_lines, FLAG_DEBUG_LINES_RAW },
12326   { 'm', "macro", &do_debug_macinfo, 1 },
12327   { 'N', "no-follow-links", &do_follow_links, 0 },
12328   { 'O', "str-offsets", &do_debug_str_offsets, 1 },
12329   { 'o', "loc", &do_debug_loc, 1 },
12330   { 'p', "pubnames", &do_debug_pubnames, 1 },
12331   { 'R', "Ranges", &do_debug_ranges, 1 },
12332   { 'r', "aranges", &do_debug_aranges, 1 },
12333   /* For compatibility with earlier versions of readelf.  */
12334   { 'r', "ranges", &do_debug_aranges, 1 },
12335   { 's', "str", &do_debug_str, 1 },
12336   { 'T', "trace_aranges", &do_trace_aranges, 1 },
12337   { 't', "pubtypes", &do_debug_pubtypes, 1 },
12338   { 'U', "trace_info", &do_trace_info, 1 },
12339   { 'u', "trace_abbrev", &do_trace_abbrevs, 1 },
12340   { 0, NULL, NULL, 0 }
12341 };
12342 
12343 /* Enable display of specific DWARF sections as determined by the comma
12344    separated strings in NAMES.  Returns non-zero if any displaying was
12345    enabled.  */
12346 
12347 int
dwarf_select_sections_by_names(const char * names)12348 dwarf_select_sections_by_names (const char *names)
12349 {
12350   const char *p;
12351   int result = 0;
12352 
12353   p = names;
12354   while (*p)
12355     {
12356       const debug_dump_long_opts *entry;
12357 
12358       for (entry = debug_option_table; entry->option; entry++)
12359           {
12360             size_t len = strlen (entry->option);
12361 
12362             if (strncmp (p, entry->option, len) == 0
12363                 && (p[len] == ',' || p[len] == '\0'))
12364               {
12365                 if (entry->val == 0)
12366                     * entry->variable = 0;
12367                 else
12368                     * entry->variable = entry->val;
12369                 result |= entry->val;
12370 
12371                 p += len;
12372                 break;
12373               }
12374           }
12375 
12376       if (entry->option == NULL)
12377           {
12378             warn (_("Unrecognized debug option '%s'\n"), p);
12379             p = strchr (p, ',');
12380             if (p == NULL)
12381               break;
12382           }
12383 
12384       if (*p == ',')
12385           p++;
12386     }
12387 
12388   /* The --debug-dump=frames-interp option also enables the
12389      --debug-dump=frames option.  */
12390   if (do_debug_frames_interp)
12391     do_debug_frames = 1;
12392 
12393   return result;
12394 }
12395 
12396 /* Enable display of specific DWARF sections as determined by the characters
12397    in LETTERS.  Returns non-zero if any displaying was enabled.  */
12398 
12399 int
dwarf_select_sections_by_letters(const char * letters)12400 dwarf_select_sections_by_letters (const char *letters)
12401 {
12402   int result = 0;
12403 
12404   while (* letters)
12405     {
12406       const debug_dump_long_opts *entry;
12407 
12408       for (entry = debug_option_table; entry->letter; entry++)
12409           {
12410             if (entry->letter == * letters)
12411               {
12412                 if (entry->val == 0)
12413                     * entry->variable = 0;
12414                 else
12415                     * entry->variable |= entry->val;
12416                 result |= entry->val;
12417                 break;
12418               }
12419           }
12420 
12421       if (entry->letter == 0)
12422           warn (_("Unrecognized debug letter option '%c'\n"), * letters);
12423 
12424       letters ++;
12425     }
12426 
12427   /* The --debug-dump=frames-interp option also enables the
12428      --debug-dump=frames option.  */
12429   if (do_debug_frames_interp)
12430     do_debug_frames = 1;
12431 
12432   return result;
12433 }
12434 
12435 void
dwarf_select_sections_all(void)12436 dwarf_select_sections_all (void)
12437 {
12438   do_debug_info = 1;
12439   do_debug_abbrevs = 1;
12440   do_debug_lines = FLAG_DEBUG_LINES_RAW;
12441   do_debug_pubnames = 1;
12442   do_debug_pubtypes = 1;
12443   do_debug_aranges = 1;
12444   do_debug_ranges = 1;
12445   do_debug_frames = 1;
12446   do_debug_macinfo = 1;
12447   do_debug_str = 1;
12448   do_debug_loc = 1;
12449   do_gdb_index = 1;
12450   do_trace_info = 1;
12451   do_trace_abbrevs = 1;
12452   do_trace_aranges = 1;
12453   do_debug_addr = 1;
12454   do_debug_cu_index = 1;
12455   do_follow_links = 1;
12456   do_debug_links = 1;
12457   do_debug_str_offsets = 1;
12458 }
12459 
12460 #define NO_ABBREVS   NULL, NULL, NULL, 0, 0, 0, NULL, 0
12461 #define ABBREV(N)    NULL, NULL, NULL, 0, 0, N, NULL, 0
12462 
12463 /* N.B. The order here must match the order in section_display_enum.  */
12464 
12465 struct dwarf_section_display debug_displays[] =
12466 {
12467   { { ".debug_abbrev",            ".zdebug_abbrev",              ".dwabrev", NO_ABBREVS },              display_debug_abbrev,   &do_debug_abbrevs,    false },
12468   { { ".debug_aranges",           ".zdebug_aranges",             ".dwarnge", NO_ABBREVS },              display_debug_aranges,  &do_debug_aranges,    true },
12469   { { ".debug_frame",             ".zdebug_frame",               ".dwframe", NO_ABBREVS },              display_debug_frames,   &do_debug_frames,     true },
12470   { { ".debug_info",              ".zdebug_info",      ".dwinfo",      ABBREV (abbrev)},  display_debug_info,     &do_debug_info, true },
12471   { { ".debug_line",              ".zdebug_line",      ".dwline",      NO_ABBREVS },          display_debug_lines,    &do_debug_lines,      true },
12472   { { ".debug_pubnames",    ".zdebug_pubnames",        ".dwpbnms", NO_ABBREVS },              display_debug_pubnames, &do_debug_pubnames, false },
12473   { { ".debug_gnu_pubnames", ".zdebug_gnu_pubnames", "",     NO_ABBREVS },          display_debug_gnu_pubnames, &do_debug_pubnames, false },
12474   { { ".eh_frame",      "",                            "",   NO_ABBREVS },          display_debug_frames,   &do_debug_frames,     true },
12475   { { ".debug_macinfo",           ".zdebug_macinfo",             "",   NO_ABBREVS },          display_debug_macinfo,  &do_debug_macinfo,    false },
12476   { { ".debug_macro",             ".zdebug_macro",               ".dwmac",       NO_ABBREVS },          display_debug_macro,    &do_debug_macinfo,    true },
12477   { { ".debug_str",     ".zdebug_str",       ".dwstr",       NO_ABBREVS },          display_debug_str,            &do_debug_str,  false },
12478   { { ".debug_line_str",    ".zdebug_line_str",        "",   NO_ABBREVS },          display_debug_str,            &do_debug_str,  false },
12479   { { ".debug_loc",     ".zdebug_loc",       ".dwloc",       NO_ABBREVS },          display_debug_loc,            &do_debug_loc,  true },
12480   { { ".debug_loclists",    ".zdebug_loclists",        "",   NO_ABBREVS },          display_debug_loc,            &do_debug_loc,  true },
12481   { { ".debug_loclists.dwo", ".zdebug_loclists.dwo", "",         NO_ABBREVS },      display_debug_loc,      &do_debug_loc,      true },
12482   { { ".debug_pubtypes",    ".zdebug_pubtypes",        ".dwpbtyp", NO_ABBREVS },              display_debug_pubnames, &do_debug_pubtypes, false },
12483   { { ".debug_gnu_pubtypes", ".zdebug_gnu_pubtypes", "",     NO_ABBREVS },          display_debug_gnu_pubnames, &do_debug_pubtypes, false },
12484   { { ".debug_ranges",            ".zdebug_ranges",              ".dwrnges", NO_ABBREVS },              display_debug_ranges,   &do_debug_ranges,     true },
12485   { { ".debug_rnglists",    ".zdebug_rnglists",        "",   NO_ABBREVS },          display_debug_ranges,   &do_debug_ranges,     true },
12486   { { ".debug_rnglists.dwo", ".zdebug_rnglists.dwo", "",         NO_ABBREVS },      display_debug_ranges,   &do_debug_ranges,   true },
12487   { { ".debug_static_func", ".zdebug_static_func",   "",     NO_ABBREVS },          display_debug_not_supported, NULL,            false },
12488   { { ".debug_static_vars", ".zdebug_static_vars",   "",     NO_ABBREVS },          display_debug_not_supported, NULL,            false },
12489   { { ".debug_types",             ".zdebug_types",               "",   ABBREV (abbrev) }, display_debug_types,    &do_debug_info, true },
12490   { { ".debug_weaknames",   ".zdebug_weaknames",     "",     NO_ABBREVS },          display_debug_not_supported, NULL,            false },
12491   { { ".gdb_index",     "",                            "",   NO_ABBREVS },          display_gdb_index,            &do_gdb_index,  false },
12492   { { ".debug_names",             "",                            "",   NO_ABBREVS },          display_debug_names,    &do_gdb_index,        false },
12493   { { ".trace_info",              "",                            "",   ABBREV (trace_abbrev) }, display_trace_info, &do_trace_info,         true },
12494   { { ".trace_abbrev",            "",                            "",   NO_ABBREVS },          display_debug_abbrev,   &do_trace_abbrevs,    false },
12495   { { ".trace_aranges",           "",                            "",   NO_ABBREVS },          display_debug_aranges,  &do_trace_aranges,    false },
12496   { { ".debug_info.dwo",    ".zdebug_info.dwo",        "",   ABBREV (abbrev_dwo) }, display_debug_info, &do_debug_info, true },
12497   { { ".debug_abbrev.dwo",  ".zdebug_abbrev.dwo",    "",     NO_ABBREVS },        display_debug_abbrev,           &do_debug_abbrevs,        false },
12498   { { ".debug_types.dwo",   ".zdebug_types.dwo",     "",     ABBREV (abbrev_dwo) }, display_debug_types, &do_debug_info,          true },
12499   { { ".debug_line.dwo",    ".zdebug_line.dwo",        "",   NO_ABBREVS },          display_debug_lines,    &do_debug_lines,      true },
12500   { { ".debug_loc.dwo",           ".zdebug_loc.dwo",             "",   NO_ABBREVS },          display_debug_loc,            &do_debug_loc,  true },
12501   { { ".debug_macro.dwo",   ".zdebug_macro.dwo",     "",     NO_ABBREVS },          display_debug_macro,    &do_debug_macinfo,    true },
12502   { { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo",   "",     NO_ABBREVS },          display_debug_macinfo,  &do_debug_macinfo,    false },
12503   { { ".debug_str.dwo",           ".zdebug_str.dwo",             "",   NO_ABBREVS },          display_debug_str,            &do_debug_str,  true },
12504   { { ".debug_str_offsets", ".zdebug_str_offsets",   "",     NO_ABBREVS },          display_debug_str_offsets, &do_debug_str_offsets, true },
12505   { { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo", "",         NO_ABBREVS },          display_debug_str_offsets, &do_debug_str_offsets, true },
12506   { { ".debug_addr",              ".zdebug_addr",      "",   NO_ABBREVS },          display_debug_addr,           &do_debug_addr, true },
12507   { { ".debug_cu_index",    "",                                  "",   NO_ABBREVS },          display_cu_index,             &do_debug_cu_index, false },
12508   { { ".debug_tu_index",    "",                                  "",   NO_ABBREVS },          display_cu_index,             &do_debug_cu_index, false },
12509   { { ".gnu_debuglink",           "",                            "",   NO_ABBREVS },          display_debug_links,    &do_debug_links,      false },
12510   { { ".gnu_debugaltlink",  "",                                  "",   NO_ABBREVS },          display_debug_links,    &do_debug_links,      false },
12511   { { ".debug_sup",     "",                            "",   NO_ABBREVS },          display_debug_sup,            &do_debug_links,          false },
12512   /* Separate debug info files can containt their own .debug_str section,
12513      and this might be in *addition* to a .debug_str section already present
12514      in the main file.        Hence we need to have two entries for .debug_str.  */
12515   { { ".debug_str",     ".zdebug_str",       "",   NO_ABBREVS },          display_debug_str,            &do_debug_str,  false },
12516   { { ".note.gnu.build-id", "",                      "",     NO_ABBREVS },          display_debug_not_supported, NULL,            false },
12517 };
12518 
12519 /* A static assertion.  */
12520 extern int debug_displays_assert[ARRAY_SIZE (debug_displays) == max ? 1 : -1];
12521