Fix build breakage from last commit (window-nat.c:windows_create_inferior)
[external/binutils.git] / binutils / dwarf.c
1 /* dwarf.c -- display DWARF contents of a BFD binary file
2    Copyright (C) 2005-2017 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 "bfd_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
32 static const char *regname (unsigned int regno, int row);
33
34 static int have_frame_base;
35 static int need_base_address;
36
37 static unsigned int last_pointer_size = 0;
38 static int warned_about_missing_comp_units = FALSE;
39
40 static unsigned int num_debug_info_entries = 0;
41 static unsigned int alloc_num_debug_info_entries = 0;
42 static debug_info *debug_information = NULL;
43 /* Special value for num_debug_info_entries to indicate
44    that the .debug_info section could not be loaded/parsed.  */
45 #define DEBUG_INFO_UNAVAILABLE  (unsigned int) -1
46
47 unsigned int eh_addr_size;
48
49 int do_debug_info;
50 int do_debug_abbrevs;
51 int do_debug_lines;
52 int do_debug_pubnames;
53 int do_debug_pubtypes;
54 int do_debug_aranges;
55 int do_debug_ranges;
56 int do_debug_frames;
57 int do_debug_frames_interp;
58 int do_debug_macinfo;
59 int do_debug_str;
60 int do_debug_loc;
61 int do_gdb_index;
62 int do_trace_info;
63 int do_trace_abbrevs;
64 int do_trace_aranges;
65 int do_debug_addr;
66 int do_debug_cu_index;
67 int do_wide;
68
69 int dwarf_cutoff_level = -1;
70 unsigned long dwarf_start_die;
71
72 int dwarf_check = 0;
73
74 /* Collection of CU/TU section sets from .debug_cu_index and .debug_tu_index
75    sections.  For version 1 package files, each set is stored in SHNDX_POOL
76    as a zero-terminated list of section indexes comprising one set of debug
77    sections from a .dwo file.  */
78
79 static unsigned int *shndx_pool = NULL;
80 static unsigned int shndx_pool_size = 0;
81 static unsigned int shndx_pool_used = 0;
82
83 /* For version 2 package files, each set contains an array of section offsets
84    and an array of section sizes, giving the offset and size of the
85    contribution from a CU or TU within one of the debug sections.
86    When displaying debug info from a package file, we need to use these
87    tables to locate the corresponding contributions to each section.  */
88
89 struct cu_tu_set
90 {
91   uint64_t signature;
92   dwarf_vma section_offsets[DW_SECT_MAX];
93   size_t section_sizes[DW_SECT_MAX];
94 };
95
96 static int cu_count = 0;
97 static int tu_count = 0;
98 static struct cu_tu_set *cu_sets = NULL;
99 static struct cu_tu_set *tu_sets = NULL;
100
101 static bfd_boolean load_cu_tu_indexes (void *);
102
103 /* Values for do_debug_lines.  */
104 #define FLAG_DEBUG_LINES_RAW     1
105 #define FLAG_DEBUG_LINES_DECODED 2
106
107 static unsigned int
108 size_of_encoded_value (int encoding)
109 {
110   switch (encoding & 0x7)
111     {
112     default:    /* ??? */
113     case 0:     return eh_addr_size;
114     case 2:     return 2;
115     case 3:     return 4;
116     case 4:     return 8;
117     }
118 }
119
120 static dwarf_vma
121 get_encoded_value (unsigned char **pdata,
122                    int encoding,
123                    struct dwarf_section *section,
124                    unsigned char * end)
125 {
126   unsigned char * data = * pdata;
127   unsigned int size = size_of_encoded_value (encoding);
128   dwarf_vma val;
129
130   if (data + size >= end)
131     {
132       warn (_("Encoded value extends past end of section\n"));
133       * pdata = end;
134       return 0;
135     }
136
137   /* PR 17512: file: 002-829853-0.004.  */
138   if (size > 8)
139     {
140       warn (_("Encoded size of %d is too large to read\n"), size);
141       * pdata = end;
142       return 0;
143     }
144
145   /* PR 17512: file: 1085-5603-0.004.  */
146   if (size == 0)
147     {
148       warn (_("Encoded size of 0 is too small to read\n"));
149       * pdata = end;
150       return 0;
151     }
152
153   if (encoding & DW_EH_PE_signed)
154     val = byte_get_signed (data, size);
155   else
156     val = byte_get (data, size);
157
158   if ((encoding & 0x70) == DW_EH_PE_pcrel)
159     val += section->address + (data - section->start);
160
161   * pdata = data + size;
162   return val;
163 }
164
165 #if defined HAVE_LONG_LONG && SIZEOF_LONG_LONG > SIZEOF_LONG
166 # ifndef __MINGW32__
167 #  define DWARF_VMA_FMT         "ll"
168 #  define DWARF_VMA_FMT_LONG    "%16.16llx"
169 # else
170 #  define DWARF_VMA_FMT         "I64"
171 #  define DWARF_VMA_FMT_LONG    "%016I64x"
172 # endif
173 #else
174 # define DWARF_VMA_FMT          "l"
175 # define DWARF_VMA_FMT_LONG     "%16.16lx"
176 #endif
177
178 /* Convert a dwarf vma value into a string.  Returns a pointer to a static
179    buffer containing the converted VALUE.  The value is converted according
180    to the printf formating character FMTCH.  If NUM_BYTES is non-zero then
181    it specifies the maximum number of bytes to be displayed in the converted
182    value and FMTCH is ignored - hex is always used.  */
183
184 static const char *
185 dwarf_vmatoa_1 (const char *fmtch, dwarf_vma value, unsigned num_bytes)
186 {
187   /* As dwarf_vmatoa is used more then once in a printf call
188      for output, we are cycling through an fixed array of pointers
189      for return address.  */
190   static int buf_pos = 0;
191   static struct dwarf_vmatoa_buf
192   {
193     char place[64];
194   } buf[16];
195   char *ret;
196
197   ret = buf[buf_pos++].place;
198   buf_pos %= ARRAY_SIZE (buf);
199
200   if (num_bytes)
201     {
202       /* Printf does not have a way of specifying a maximum field width for an
203          integer value, so we print the full value into a buffer and then select
204          the precision we need.  */
205       snprintf (ret, sizeof (buf[0].place), DWARF_VMA_FMT_LONG, value);
206       if (num_bytes > 8)
207         num_bytes = 8;
208       return ret + (16 - 2 * num_bytes);
209     }
210   else
211     {
212       char fmt[32];
213
214       sprintf (fmt, "%%%s%s", DWARF_VMA_FMT, fmtch);
215       snprintf (ret, sizeof (buf[0].place), fmt, value);
216       return ret;
217     }
218 }
219
220 static inline const char *
221 dwarf_vmatoa (const char * fmtch, dwarf_vma value)
222 {
223   return dwarf_vmatoa_1 (fmtch, value, 0);
224 }
225
226 /* Print a dwarf_vma value (typically an address, offset or length) in
227    hexadecimal format, followed by a space.  The length of the VALUE (and
228    hence the precision displayed) is determined by the NUM_BYTES parameter.  */
229
230 static void
231 print_dwarf_vma (dwarf_vma value, unsigned num_bytes)
232 {
233   printf ("%s ", dwarf_vmatoa_1 (NULL, value, num_bytes));
234 }
235
236 /* Format a 64-bit value, given as two 32-bit values, in hex.
237    For reentrancy, this uses a buffer provided by the caller.  */
238
239 static const char *
240 dwarf_vmatoa64 (dwarf_vma hvalue, dwarf_vma lvalue, char *buf,
241                 unsigned int buf_len)
242 {
243   int len = 0;
244
245   if (hvalue == 0)
246     snprintf (buf, buf_len, "%" DWARF_VMA_FMT "x", lvalue);
247   else
248     {
249       len = snprintf (buf, buf_len, "%" DWARF_VMA_FMT "x", hvalue);
250       snprintf (buf + len, buf_len - len,
251                 "%08" DWARF_VMA_FMT "x", lvalue);
252     }
253
254   return buf;
255 }
256
257 /* Read in a LEB128 encoded value starting at address DATA.
258    If SIGN is true, return a signed LEB128 value.
259    If LENGTH_RETURN is not NULL, return in it the number of bytes read.
260    No bytes will be read at address END or beyond.  */
261
262 dwarf_vma
263 read_leb128 (unsigned char *data,
264              unsigned int *length_return,
265              bfd_boolean sign,
266              const unsigned char * const end)
267 {
268   dwarf_vma result = 0;
269   unsigned int num_read = 0;
270   unsigned int shift = 0;
271   unsigned char byte = 0;
272
273   while (data < end)
274     {
275       byte = *data++;
276       num_read++;
277
278       result |= ((dwarf_vma) (byte & 0x7f)) << shift;
279
280       shift += 7;
281       if ((byte & 0x80) == 0)
282         break;
283
284       /* PR 17512: file: 0ca183b8.
285          FIXME: Should we signal this error somehow ?  */
286       if (shift >= sizeof (result) * 8)
287         break;
288     }
289
290   if (length_return != NULL)
291     *length_return = num_read;
292
293   if (sign && (shift < 8 * sizeof (result)) && (byte & 0x40))
294     result |= -((dwarf_vma) 1 << shift);
295
296   return result;
297 }
298
299 /* Create a signed version to avoid painful typecasts.  */
300 static inline dwarf_signed_vma
301 read_sleb128 (unsigned char * data,
302               unsigned int *  length_return,
303               const unsigned char * const end)
304 {
305   return (dwarf_signed_vma) read_leb128 (data, length_return, TRUE, end);
306 }
307
308 static inline dwarf_vma
309 read_uleb128 (unsigned char * data,
310               unsigned int *  length_return,
311               const unsigned char * const end)
312 {
313   return read_leb128 (data, length_return, FALSE, end);
314 }
315
316 #define SAFE_BYTE_GET(VAL, PTR, AMOUNT, END)    \
317   do                                            \
318     {                                           \
319       unsigned int amount = (AMOUNT);           \
320       if (sizeof (VAL) < amount)                \
321         {                                       \
322           error (_("internal error: attempt to read %d bytes of data in to %d sized variable"),\
323                  amount, (int) sizeof (VAL));   \
324           amount = sizeof (VAL);                \
325         }                                       \
326       if (((PTR) + amount) >= (END))            \
327         {                                       \
328           if ((PTR) < (END))                    \
329             amount = (END) - (PTR);             \
330           else                                  \
331             amount = 0;                         \
332         }                                       \
333       if (amount == 0 || amount > 8)            \
334         VAL = 0;                                \
335       else                                      \
336         VAL = byte_get ((PTR), amount);         \
337     }                                           \
338   while (0)
339
340 #define SAFE_BYTE_GET_AND_INC(VAL, PTR, AMOUNT, END)    \
341   do                                                    \
342     {                                                   \
343       SAFE_BYTE_GET (VAL, PTR, AMOUNT, END);            \
344       PTR += AMOUNT;                                    \
345     }                                                   \
346   while (0)
347
348 #define SAFE_SIGNED_BYTE_GET(VAL, PTR, AMOUNT, END)     \
349   do                                                    \
350     {                                                   \
351       unsigned int amount = (AMOUNT);                   \
352       if (((PTR) + amount) >= (END))                    \
353         {                                               \
354           if ((PTR) < (END))                            \
355             amount = (END) - (PTR);                     \
356           else                                          \
357             amount = 0;                                 \
358         }                                               \
359       if (amount)                                       \
360         VAL = byte_get_signed ((PTR), amount);          \
361       else                                              \
362         VAL = 0;                                        \
363     }                                                   \
364   while (0)
365
366 #define SAFE_SIGNED_BYTE_GET_AND_INC(VAL, PTR, AMOUNT, END)     \
367   do                                                            \
368     {                                                           \
369       SAFE_SIGNED_BYTE_GET (VAL, PTR, AMOUNT, END);             \
370       PTR += AMOUNT;                                            \
371     }                                                           \
372   while (0)
373
374 #define SAFE_BYTE_GET64(PTR, HIGH, LOW, END)            \
375   do                                                    \
376     {                                                   \
377       if (((PTR) + 8) <= (END))                         \
378         {                                               \
379           byte_get_64 ((PTR), (HIGH), (LOW));           \
380         }                                               \
381       else                                              \
382         {                                               \
383           * (LOW) = * (HIGH) = 0;                       \
384         }                                               \
385     }                                                   \
386   while (0)
387
388 typedef struct State_Machine_Registers
389 {
390   dwarf_vma address;
391   unsigned int file;
392   unsigned int line;
393   unsigned int column;
394   int is_stmt;
395   int basic_block;
396   unsigned char op_index;
397   unsigned char end_sequence;
398 /* This variable hold the number of the last entry seen
399    in the File Table.  */
400   unsigned int last_file_entry;
401 } SMR;
402
403 static SMR state_machine_regs;
404
405 static void
406 reset_state_machine (int is_stmt)
407 {
408   state_machine_regs.address = 0;
409   state_machine_regs.op_index = 0;
410   state_machine_regs.file = 1;
411   state_machine_regs.line = 1;
412   state_machine_regs.column = 0;
413   state_machine_regs.is_stmt = is_stmt;
414   state_machine_regs.basic_block = 0;
415   state_machine_regs.end_sequence = 0;
416   state_machine_regs.last_file_entry = 0;
417 }
418
419 /* Handled an extend line op.
420    Returns the number of bytes read.  */
421
422 static int
423 process_extended_line_op (unsigned char * data,
424                           int is_stmt,
425                           unsigned char * end)
426 {
427   unsigned char op_code;
428   unsigned int bytes_read;
429   unsigned int len;
430   unsigned char *name;
431   unsigned char *orig_data = data;
432   dwarf_vma adr;
433
434   len = read_uleb128 (data, & bytes_read, end);
435   data += bytes_read;
436
437   if (len == 0 || data == end || len > (uintptr_t) (end - data))
438     {
439       warn (_("Badly formed extended line op encountered!\n"));
440       return bytes_read;
441     }
442
443   len += bytes_read;
444   op_code = *data++;
445
446   printf (_("  Extended opcode %d: "), op_code);
447
448   switch (op_code)
449     {
450     case DW_LNE_end_sequence:
451       printf (_("End of Sequence\n\n"));
452       reset_state_machine (is_stmt);
453       break;
454
455     case DW_LNE_set_address:
456       /* PR 17512: file: 002-100480-0.004.  */
457       if (len - bytes_read - 1 > 8)
458         {
459           warn (_("Length (%d) of DW_LNE_set_address op is too long\n"),
460                 len - bytes_read - 1);
461           adr = 0;
462         }
463       else
464         SAFE_BYTE_GET (adr, data, len - bytes_read - 1, end);
465       printf (_("set Address to 0x%s\n"), dwarf_vmatoa ("x", adr));
466       state_machine_regs.address = adr;
467       state_machine_regs.op_index = 0;
468       break;
469
470     case DW_LNE_define_file:
471       printf (_("define new File Table entry\n"));
472       printf (_("  Entry\tDir\tTime\tSize\tName\n"));
473       printf ("   %d\t", ++state_machine_regs.last_file_entry);
474
475       name = data;
476       data += strnlen ((char *) data, end - data) + 1;
477       printf ("%s\t", dwarf_vmatoa ("u", read_uleb128 (data, & bytes_read, end)));
478       data += bytes_read;
479       printf ("%s\t", dwarf_vmatoa ("u", read_uleb128 (data, & bytes_read, end)));
480       data += bytes_read;
481       printf ("%s\t", dwarf_vmatoa ("u", read_uleb128 (data, & bytes_read, end)));
482       data += bytes_read;
483       printf ("%s\n\n", name);
484
485       if (((unsigned int) (data - orig_data) != len) || data == end)
486         warn (_("DW_LNE_define_file: Bad opcode length\n"));
487       break;
488
489     case DW_LNE_set_discriminator:
490       printf (_("set Discriminator to %s\n"),
491               dwarf_vmatoa ("u", read_uleb128 (data, & bytes_read, end)));
492       break;
493
494     /* HP extensions.  */
495     case DW_LNE_HP_negate_is_UV_update:
496       printf ("DW_LNE_HP_negate_is_UV_update\n");
497       break;
498     case DW_LNE_HP_push_context:
499       printf ("DW_LNE_HP_push_context\n");
500       break;
501     case DW_LNE_HP_pop_context:
502       printf ("DW_LNE_HP_pop_context\n");
503       break;
504     case DW_LNE_HP_set_file_line_column:
505       printf ("DW_LNE_HP_set_file_line_column\n");
506       break;
507     case DW_LNE_HP_set_routine_name:
508       printf ("DW_LNE_HP_set_routine_name\n");
509       break;
510     case DW_LNE_HP_set_sequence:
511       printf ("DW_LNE_HP_set_sequence\n");
512       break;
513     case DW_LNE_HP_negate_post_semantics:
514       printf ("DW_LNE_HP_negate_post_semantics\n");
515       break;
516     case DW_LNE_HP_negate_function_exit:
517       printf ("DW_LNE_HP_negate_function_exit\n");
518       break;
519     case DW_LNE_HP_negate_front_end_logical:
520       printf ("DW_LNE_HP_negate_front_end_logical\n");
521       break;
522     case DW_LNE_HP_define_proc:
523       printf ("DW_LNE_HP_define_proc\n");
524       break;
525     case DW_LNE_HP_source_file_correlation:
526       {
527         unsigned char *edata = data + len - bytes_read - 1;
528
529         printf ("DW_LNE_HP_source_file_correlation\n");
530
531         while (data < edata)
532           {
533             unsigned int opc;
534
535             opc = read_uleb128 (data, & bytes_read, edata);
536             data += bytes_read;
537
538             switch (opc)
539               {
540               case DW_LNE_HP_SFC_formfeed:
541                 printf ("    DW_LNE_HP_SFC_formfeed\n");
542                 break;
543               case DW_LNE_HP_SFC_set_listing_line:
544                 printf ("    DW_LNE_HP_SFC_set_listing_line (%s)\n",
545                         dwarf_vmatoa ("u",
546                                       read_uleb128 (data, & bytes_read, edata)));
547                 data += bytes_read;
548                 break;
549               case DW_LNE_HP_SFC_associate:
550                 printf ("    DW_LNE_HP_SFC_associate ");
551                 printf ("(%s",
552                         dwarf_vmatoa ("u",
553                                       read_uleb128 (data, & bytes_read, edata)));
554                 data += bytes_read;
555                 printf (",%s",
556                         dwarf_vmatoa ("u",
557                                       read_uleb128 (data, & bytes_read, edata)));
558                 data += bytes_read;
559                 printf (",%s)\n",
560                         dwarf_vmatoa ("u",
561                                       read_uleb128 (data, & bytes_read, edata)));
562                 data += bytes_read;
563                 break;
564               default:
565                 printf (_("    UNKNOWN DW_LNE_HP_SFC opcode (%u)\n"), opc);
566                 data = edata;
567                 break;
568               }
569           }
570       }
571       break;
572
573     default:
574       {
575         unsigned int rlen = len - bytes_read - 1;
576
577         if (op_code >= DW_LNE_lo_user
578             /* The test against DW_LNW_hi_user is redundant due to
579                the limited range of the unsigned char data type used
580                for op_code.  */
581             /*&& op_code <= DW_LNE_hi_user*/)
582           printf (_("user defined: "));
583         else
584           printf (_("UNKNOWN: "));
585         printf (_("length %d ["), rlen);
586         for (; rlen; rlen--)
587           printf (" %02x", *data++);
588         printf ("]\n");
589       }
590       break;
591     }
592
593   return len;
594 }
595
596 static const unsigned char *
597 fetch_indirect_string (dwarf_vma offset)
598 {
599   struct dwarf_section *section = &debug_displays [str].section;
600
601   if (section->start == NULL)
602     return (const unsigned char *) _("<no .debug_str section>");
603
604   if (offset > section->size)
605     {
606       warn (_("DW_FORM_strp offset too big: %s\n"),
607             dwarf_vmatoa ("x", offset));
608       return (const unsigned char *) _("<offset is too big>");
609     }
610
611   return (const unsigned char *) section->start + offset;
612 }
613
614 static const unsigned char *
615 fetch_indirect_line_string (dwarf_vma offset)
616 {
617   struct dwarf_section *section = &debug_displays [line_str].section;
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: %s\n"),
625             dwarf_vmatoa ("x", offset));
626       return (const unsigned char *) _("<offset is too big>");
627     }
628
629   return (const unsigned char *) section->start + offset;
630 }
631
632 static const char *
633 fetch_indexed_string (dwarf_vma idx, struct cu_tu_set *this_set,
634                       dwarf_vma offset_size, int dwo)
635 {
636   enum dwarf_section_display_enum str_sec_idx = dwo ? str_dwo : str;
637   enum dwarf_section_display_enum idx_sec_idx = dwo ? str_index_dwo : str_index;
638   struct dwarf_section *index_section = &debug_displays [idx_sec_idx].section;
639   struct dwarf_section *str_section = &debug_displays [str_sec_idx].section;
640   dwarf_vma index_offset = idx * offset_size;
641   dwarf_vma str_offset;
642
643   if (index_section->start == NULL)
644     return (dwo ? _("<no .debug_str_offsets.dwo section>")
645                 : _("<no .debug_str_offsets section>"));
646
647   if (this_set != NULL)
648     index_offset += this_set->section_offsets [DW_SECT_STR_OFFSETS];
649   if (index_offset > index_section->size)
650     {
651       warn (_("DW_FORM_GNU_str_index offset too big: %s\n"),
652             dwarf_vmatoa ("x", index_offset));
653       return _("<index offset is too big>");
654     }
655
656   if (str_section->start == NULL)
657     return (dwo ? _("<no .debug_str.dwo section>")
658                 : _("<no .debug_str section>"));
659
660   str_offset = byte_get (index_section->start + index_offset, offset_size);
661   str_offset -= str_section->address;
662   if (str_offset > str_section->size)
663     {
664       warn (_("DW_FORM_GNU_str_index indirect offset too big: %s\n"),
665             dwarf_vmatoa ("x", str_offset));
666       return _("<indirect index offset is too big>");
667     }
668
669   return (const char *) str_section->start + str_offset;
670 }
671
672 static const char *
673 fetch_indexed_value (dwarf_vma offset, dwarf_vma bytes)
674 {
675   struct dwarf_section *section = &debug_displays [debug_addr].section;
676
677   if (section->start == NULL)
678     return (_("<no .debug_addr section>"));
679
680   if (offset + bytes > section->size)
681     {
682       warn (_("Offset into section %s too big: %s\n"),
683             section->name, dwarf_vmatoa ("x", offset));
684       return "<offset too big>";
685     }
686
687   return dwarf_vmatoa ("x", byte_get (section->start + offset, bytes));
688 }
689
690
691 /* FIXME:  There are better and more efficient ways to handle
692    these structures.  For now though, I just want something that
693    is simple to implement.  */
694 typedef struct abbrev_attr
695 {
696   unsigned long attribute;
697   unsigned long form;
698   bfd_signed_vma implicit_const;
699   struct abbrev_attr *next;
700 }
701 abbrev_attr;
702
703 typedef struct abbrev_entry
704 {
705   unsigned long entry;
706   unsigned long tag;
707   int children;
708   struct abbrev_attr *first_attr;
709   struct abbrev_attr *last_attr;
710   struct abbrev_entry *next;
711 }
712 abbrev_entry;
713
714 static abbrev_entry *first_abbrev = NULL;
715 static abbrev_entry *last_abbrev = NULL;
716
717 static void
718 free_abbrevs (void)
719 {
720   abbrev_entry *abbrv;
721
722   for (abbrv = first_abbrev; abbrv;)
723     {
724       abbrev_entry *next_abbrev = abbrv->next;
725       abbrev_attr *attr;
726
727       for (attr = abbrv->first_attr; attr;)
728         {
729           abbrev_attr *next_attr = attr->next;
730
731           free (attr);
732           attr = next_attr;
733         }
734
735       free (abbrv);
736       abbrv = next_abbrev;
737     }
738
739   last_abbrev = first_abbrev = NULL;
740 }
741
742 static void
743 add_abbrev (unsigned long number, unsigned long tag, int children)
744 {
745   abbrev_entry *entry;
746
747   entry = (abbrev_entry *) malloc (sizeof (*entry));
748   if (entry == NULL)
749     /* ugg */
750     return;
751
752   entry->entry      = number;
753   entry->tag        = tag;
754   entry->children   = children;
755   entry->first_attr = NULL;
756   entry->last_attr  = NULL;
757   entry->next       = NULL;
758
759   if (first_abbrev == NULL)
760     first_abbrev = entry;
761   else
762     last_abbrev->next = entry;
763
764   last_abbrev = entry;
765 }
766
767 static void
768 add_abbrev_attr (unsigned long attribute, unsigned long form,
769                  bfd_signed_vma implicit_const)
770 {
771   abbrev_attr *attr;
772
773   attr = (abbrev_attr *) malloc (sizeof (*attr));
774   if (attr == NULL)
775     /* ugg */
776     return;
777
778   attr->attribute = attribute;
779   attr->form      = form;
780   attr->implicit_const = implicit_const;
781   attr->next      = NULL;
782
783   if (last_abbrev->first_attr == NULL)
784     last_abbrev->first_attr = attr;
785   else
786     last_abbrev->last_attr->next = attr;
787
788   last_abbrev->last_attr = attr;
789 }
790
791 /* Processes the (partial) contents of a .debug_abbrev section.
792    Returns NULL if the end of the section was encountered.
793    Returns the address after the last byte read if the end of
794    an abbreviation set was found.  */
795
796 static unsigned char *
797 process_abbrev_section (unsigned char *start, unsigned char *end)
798 {
799   if (first_abbrev != NULL)
800     return NULL;
801
802   while (start < end)
803     {
804       unsigned int bytes_read;
805       unsigned long entry;
806       unsigned long tag;
807       unsigned long attribute;
808       int children;
809
810       entry = read_uleb128 (start, & bytes_read, end);
811       start += bytes_read;
812
813       /* A single zero is supposed to end the section according
814          to the standard.  If there's more, then signal that to
815          the caller.  */
816       if (start == end)
817         return NULL;
818       if (entry == 0)
819         return start;
820
821       tag = read_uleb128 (start, & bytes_read, end);
822       start += bytes_read;
823       if (start == end)
824         return NULL;
825
826       children = *start++;
827
828       add_abbrev (entry, tag, children);
829
830       do
831         {
832           unsigned long form;
833           /* Initialize it due to a false compiler warning.  */
834           bfd_signed_vma implicit_const = -1;
835
836           attribute = read_uleb128 (start, & bytes_read, end);
837           start += bytes_read;
838           if (start == end)
839             break;
840
841           form = read_uleb128 (start, & bytes_read, end);
842           start += bytes_read;
843           if (start == end)
844             break;
845
846           if (form == DW_FORM_implicit_const)
847             {
848               implicit_const = read_sleb128 (start, & bytes_read, end);
849               start += bytes_read;
850               if (start == end)
851                 break;
852             }
853
854           add_abbrev_attr (attribute, form, implicit_const);
855         }
856       while (attribute != 0);
857     }
858
859   /* Report the missing single zero which ends the section.  */
860   error (_(".debug_abbrev section not zero terminated\n"));
861
862   return NULL;
863 }
864
865 static const char *
866 get_TAG_name (unsigned long tag)
867 {
868   const char *name = get_DW_TAG_name ((unsigned int)tag);
869
870   if (name == NULL)
871     {
872       static char buffer[100];
873
874       snprintf (buffer, sizeof (buffer), _("Unknown TAG value: %lx"), tag);
875       return buffer;
876     }
877
878   return name;
879 }
880
881 static const char *
882 get_FORM_name (unsigned long form)
883 {
884   const char *name;
885
886   if (form == 0)
887     return "DW_FORM value: 0";
888
889   name = get_DW_FORM_name (form);
890   if (name == NULL)
891     {
892       static char buffer[100];
893
894       snprintf (buffer, sizeof (buffer), _("Unknown FORM value: %lx"), form);
895       return buffer;
896     }
897
898   return name;
899 }
900
901 static unsigned char *
902 display_block (unsigned char *data,
903                dwarf_vma length,
904                const unsigned char * const end, char delimiter)
905 {
906   dwarf_vma maxlen;
907
908   printf (_("%c%s byte block: "), delimiter, dwarf_vmatoa ("u", length));
909   if (data > end)
910     return (unsigned char *) end;
911
912   maxlen = (dwarf_vma) (end - data);
913   length = length > maxlen ? maxlen : length;
914
915   while (length --)
916     printf ("%lx ", (unsigned long) byte_get (data++, 1));
917
918   return data;
919 }
920
921 static int
922 decode_location_expression (unsigned char * data,
923                             unsigned int pointer_size,
924                             unsigned int offset_size,
925                             int dwarf_version,
926                             dwarf_vma length,
927                             dwarf_vma cu_offset,
928                             struct dwarf_section * section)
929 {
930   unsigned op;
931   unsigned int bytes_read;
932   dwarf_vma uvalue;
933   dwarf_signed_vma svalue;
934   unsigned char *end = data + length;
935   int need_frame_base = 0;
936
937   while (data < end)
938     {
939       op = *data++;
940
941       switch (op)
942         {
943         case DW_OP_addr:
944           SAFE_BYTE_GET_AND_INC (uvalue, data, pointer_size, end);
945           printf ("DW_OP_addr: %s", dwarf_vmatoa ("x", uvalue));
946           break;
947         case DW_OP_deref:
948           printf ("DW_OP_deref");
949           break;
950         case DW_OP_const1u:
951           SAFE_BYTE_GET_AND_INC (uvalue, data, 1, end);
952           printf ("DW_OP_const1u: %lu", (unsigned long) uvalue);
953           break;
954         case DW_OP_const1s:
955           SAFE_SIGNED_BYTE_GET_AND_INC (svalue, data, 1, end);
956           printf ("DW_OP_const1s: %ld", (long) svalue);
957           break;
958         case DW_OP_const2u:
959           SAFE_BYTE_GET_AND_INC (uvalue, data, 2, end);
960           printf ("DW_OP_const2u: %lu", (unsigned long) uvalue);
961           break;
962         case DW_OP_const2s:
963           SAFE_SIGNED_BYTE_GET_AND_INC (svalue, data, 2, end);
964           printf ("DW_OP_const2s: %ld", (long) svalue);
965           break;
966         case DW_OP_const4u:
967           SAFE_BYTE_GET_AND_INC (uvalue, data, 4, end);
968           printf ("DW_OP_const4u: %lu", (unsigned long) uvalue);
969           break;
970         case DW_OP_const4s:
971           SAFE_SIGNED_BYTE_GET_AND_INC (svalue, data, 4, end);
972           printf ("DW_OP_const4s: %ld", (long) svalue);
973           break;
974         case DW_OP_const8u:
975           SAFE_BYTE_GET_AND_INC (uvalue, data, 4, end);
976           printf ("DW_OP_const8u: %lu ", (unsigned long) uvalue);
977           SAFE_BYTE_GET_AND_INC (uvalue, data, 4, end);
978           printf ("%lu", (unsigned long) uvalue);
979           break;
980         case DW_OP_const8s:
981           SAFE_SIGNED_BYTE_GET_AND_INC (svalue, data, 4, end);
982           printf ("DW_OP_const8s: %ld ", (long) svalue);
983           SAFE_SIGNED_BYTE_GET_AND_INC (svalue, data, 4, end);
984           printf ("%ld", (long) svalue);
985           break;
986         case DW_OP_constu:
987           printf ("DW_OP_constu: %s",
988                   dwarf_vmatoa ("u", read_uleb128 (data, &bytes_read, end)));
989           data += bytes_read;
990           break;
991         case DW_OP_consts:
992           printf ("DW_OP_consts: %s",
993                   dwarf_vmatoa ("d", read_sleb128 (data, &bytes_read, end)));
994           data += bytes_read;
995           break;
996         case DW_OP_dup:
997           printf ("DW_OP_dup");
998           break;
999         case DW_OP_drop:
1000           printf ("DW_OP_drop");
1001           break;
1002         case DW_OP_over:
1003           printf ("DW_OP_over");
1004           break;
1005         case DW_OP_pick:
1006           SAFE_BYTE_GET_AND_INC (uvalue, data, 1, end);
1007           printf ("DW_OP_pick: %ld", (unsigned long) uvalue);
1008           break;
1009         case DW_OP_swap:
1010           printf ("DW_OP_swap");
1011           break;
1012         case DW_OP_rot:
1013           printf ("DW_OP_rot");
1014           break;
1015         case DW_OP_xderef:
1016           printf ("DW_OP_xderef");
1017           break;
1018         case DW_OP_abs:
1019           printf ("DW_OP_abs");
1020           break;
1021         case DW_OP_and:
1022           printf ("DW_OP_and");
1023           break;
1024         case DW_OP_div:
1025           printf ("DW_OP_div");
1026           break;
1027         case DW_OP_minus:
1028           printf ("DW_OP_minus");
1029           break;
1030         case DW_OP_mod:
1031           printf ("DW_OP_mod");
1032           break;
1033         case DW_OP_mul:
1034           printf ("DW_OP_mul");
1035           break;
1036         case DW_OP_neg:
1037           printf ("DW_OP_neg");
1038           break;
1039         case DW_OP_not:
1040           printf ("DW_OP_not");
1041           break;
1042         case DW_OP_or:
1043           printf ("DW_OP_or");
1044           break;
1045         case DW_OP_plus:
1046           printf ("DW_OP_plus");
1047           break;
1048         case DW_OP_plus_uconst:
1049           printf ("DW_OP_plus_uconst: %s",
1050                   dwarf_vmatoa ("u", read_uleb128 (data, &bytes_read, end)));
1051           data += bytes_read;
1052           break;
1053         case DW_OP_shl:
1054           printf ("DW_OP_shl");
1055           break;
1056         case DW_OP_shr:
1057           printf ("DW_OP_shr");
1058           break;
1059         case DW_OP_shra:
1060           printf ("DW_OP_shra");
1061           break;
1062         case DW_OP_xor:
1063           printf ("DW_OP_xor");
1064           break;
1065         case DW_OP_bra:
1066           SAFE_SIGNED_BYTE_GET_AND_INC (svalue, data, 2, end);
1067           printf ("DW_OP_bra: %ld", (long) svalue);
1068           break;
1069         case DW_OP_eq:
1070           printf ("DW_OP_eq");
1071           break;
1072         case DW_OP_ge:
1073           printf ("DW_OP_ge");
1074           break;
1075         case DW_OP_gt:
1076           printf ("DW_OP_gt");
1077           break;
1078         case DW_OP_le:
1079           printf ("DW_OP_le");
1080           break;
1081         case DW_OP_lt:
1082           printf ("DW_OP_lt");
1083           break;
1084         case DW_OP_ne:
1085           printf ("DW_OP_ne");
1086           break;
1087         case DW_OP_skip:
1088           SAFE_SIGNED_BYTE_GET_AND_INC (svalue, data, 2, end);
1089           printf ("DW_OP_skip: %ld", (long) svalue);
1090           break;
1091
1092         case DW_OP_lit0:
1093         case DW_OP_lit1:
1094         case DW_OP_lit2:
1095         case DW_OP_lit3:
1096         case DW_OP_lit4:
1097         case DW_OP_lit5:
1098         case DW_OP_lit6:
1099         case DW_OP_lit7:
1100         case DW_OP_lit8:
1101         case DW_OP_lit9:
1102         case DW_OP_lit10:
1103         case DW_OP_lit11:
1104         case DW_OP_lit12:
1105         case DW_OP_lit13:
1106         case DW_OP_lit14:
1107         case DW_OP_lit15:
1108         case DW_OP_lit16:
1109         case DW_OP_lit17:
1110         case DW_OP_lit18:
1111         case DW_OP_lit19:
1112         case DW_OP_lit20:
1113         case DW_OP_lit21:
1114         case DW_OP_lit22:
1115         case DW_OP_lit23:
1116         case DW_OP_lit24:
1117         case DW_OP_lit25:
1118         case DW_OP_lit26:
1119         case DW_OP_lit27:
1120         case DW_OP_lit28:
1121         case DW_OP_lit29:
1122         case DW_OP_lit30:
1123         case DW_OP_lit31:
1124           printf ("DW_OP_lit%d", op - DW_OP_lit0);
1125           break;
1126
1127         case DW_OP_reg0:
1128         case DW_OP_reg1:
1129         case DW_OP_reg2:
1130         case DW_OP_reg3:
1131         case DW_OP_reg4:
1132         case DW_OP_reg5:
1133         case DW_OP_reg6:
1134         case DW_OP_reg7:
1135         case DW_OP_reg8:
1136         case DW_OP_reg9:
1137         case DW_OP_reg10:
1138         case DW_OP_reg11:
1139         case DW_OP_reg12:
1140         case DW_OP_reg13:
1141         case DW_OP_reg14:
1142         case DW_OP_reg15:
1143         case DW_OP_reg16:
1144         case DW_OP_reg17:
1145         case DW_OP_reg18:
1146         case DW_OP_reg19:
1147         case DW_OP_reg20:
1148         case DW_OP_reg21:
1149         case DW_OP_reg22:
1150         case DW_OP_reg23:
1151         case DW_OP_reg24:
1152         case DW_OP_reg25:
1153         case DW_OP_reg26:
1154         case DW_OP_reg27:
1155         case DW_OP_reg28:
1156         case DW_OP_reg29:
1157         case DW_OP_reg30:
1158         case DW_OP_reg31:
1159           printf ("DW_OP_reg%d (%s)", op - DW_OP_reg0,
1160                   regname (op - DW_OP_reg0, 1));
1161           break;
1162
1163         case DW_OP_breg0:
1164         case DW_OP_breg1:
1165         case DW_OP_breg2:
1166         case DW_OP_breg3:
1167         case DW_OP_breg4:
1168         case DW_OP_breg5:
1169         case DW_OP_breg6:
1170         case DW_OP_breg7:
1171         case DW_OP_breg8:
1172         case DW_OP_breg9:
1173         case DW_OP_breg10:
1174         case DW_OP_breg11:
1175         case DW_OP_breg12:
1176         case DW_OP_breg13:
1177         case DW_OP_breg14:
1178         case DW_OP_breg15:
1179         case DW_OP_breg16:
1180         case DW_OP_breg17:
1181         case DW_OP_breg18:
1182         case DW_OP_breg19:
1183         case DW_OP_breg20:
1184         case DW_OP_breg21:
1185         case DW_OP_breg22:
1186         case DW_OP_breg23:
1187         case DW_OP_breg24:
1188         case DW_OP_breg25:
1189         case DW_OP_breg26:
1190         case DW_OP_breg27:
1191         case DW_OP_breg28:
1192         case DW_OP_breg29:
1193         case DW_OP_breg30:
1194         case DW_OP_breg31:
1195           printf ("DW_OP_breg%d (%s): %s",
1196                   op - DW_OP_breg0,
1197                   regname (op - DW_OP_breg0, 1),
1198                   dwarf_vmatoa ("d", read_sleb128 (data, &bytes_read, end)));
1199           data += bytes_read;
1200           break;
1201
1202         case DW_OP_regx:
1203           uvalue = read_uleb128 (data, &bytes_read, end);
1204           data += bytes_read;
1205           printf ("DW_OP_regx: %s (%s)",
1206                   dwarf_vmatoa ("u", uvalue), regname (uvalue, 1));
1207           break;
1208         case DW_OP_fbreg:
1209           need_frame_base = 1;
1210           printf ("DW_OP_fbreg: %s",
1211                   dwarf_vmatoa ("d", read_sleb128 (data, &bytes_read, end)));
1212           data += bytes_read;
1213           break;
1214         case DW_OP_bregx:
1215           uvalue = read_uleb128 (data, &bytes_read, end);
1216           data += bytes_read;
1217           printf ("DW_OP_bregx: %s (%s) %s",
1218                   dwarf_vmatoa ("u", uvalue), regname (uvalue, 1),
1219                   dwarf_vmatoa ("d", read_sleb128 (data, &bytes_read, end)));
1220           data += bytes_read;
1221           break;
1222         case DW_OP_piece:
1223           printf ("DW_OP_piece: %s",
1224                   dwarf_vmatoa ("u", read_uleb128 (data, &bytes_read, end)));
1225           data += bytes_read;
1226           break;
1227         case DW_OP_deref_size:
1228           SAFE_BYTE_GET_AND_INC (uvalue, data, 1, end);
1229           printf ("DW_OP_deref_size: %ld", (long) uvalue);
1230           break;
1231         case DW_OP_xderef_size:
1232           SAFE_BYTE_GET_AND_INC (uvalue, data, 1, end);
1233           printf ("DW_OP_xderef_size: %ld", (long) uvalue);
1234           break;
1235         case DW_OP_nop:
1236           printf ("DW_OP_nop");
1237           break;
1238
1239           /* DWARF 3 extensions.  */
1240         case DW_OP_push_object_address:
1241           printf ("DW_OP_push_object_address");
1242           break;
1243         case DW_OP_call2:
1244           /* XXX: Strictly speaking for 64-bit DWARF3 files
1245              this ought to be an 8-byte wide computation.  */
1246           SAFE_SIGNED_BYTE_GET_AND_INC (svalue, data, 2, end);
1247           printf ("DW_OP_call2: <0x%s>",
1248                   dwarf_vmatoa ("x", svalue + cu_offset));
1249           break;
1250         case DW_OP_call4:
1251           /* XXX: Strictly speaking for 64-bit DWARF3 files
1252              this ought to be an 8-byte wide computation.  */
1253           SAFE_SIGNED_BYTE_GET_AND_INC (svalue, data, 4, end);
1254           printf ("DW_OP_call4: <0x%s>",
1255                   dwarf_vmatoa ("x", svalue + cu_offset));
1256           break;
1257         case DW_OP_call_ref:
1258           /* XXX: Strictly speaking for 64-bit DWARF3 files
1259              this ought to be an 8-byte wide computation.  */
1260           if (dwarf_version == -1)
1261             {
1262               printf (_("(DW_OP_call_ref in frame info)"));
1263               /* No way to tell where the next op is, so just bail.  */
1264               return need_frame_base;
1265             }
1266           if (dwarf_version == 2)
1267             {
1268               SAFE_BYTE_GET_AND_INC (uvalue, data, pointer_size, end);
1269             }
1270           else
1271             {
1272               SAFE_BYTE_GET_AND_INC (uvalue, data, offset_size, end);
1273             }
1274           printf ("DW_OP_call_ref: <0x%s>", dwarf_vmatoa ("x", uvalue));
1275           break;
1276         case DW_OP_form_tls_address:
1277           printf ("DW_OP_form_tls_address");
1278           break;
1279         case DW_OP_call_frame_cfa:
1280           printf ("DW_OP_call_frame_cfa");
1281           break;
1282         case DW_OP_bit_piece:
1283           printf ("DW_OP_bit_piece: ");
1284           printf (_("size: %s "),
1285                   dwarf_vmatoa ("u", read_uleb128 (data, &bytes_read, end)));
1286           data += bytes_read;
1287           printf (_("offset: %s "),
1288                   dwarf_vmatoa ("u", read_uleb128 (data, &bytes_read, end)));
1289           data += bytes_read;
1290           break;
1291
1292           /* DWARF 4 extensions.  */
1293         case DW_OP_stack_value:
1294           printf ("DW_OP_stack_value");
1295           break;
1296
1297         case DW_OP_implicit_value:
1298           printf ("DW_OP_implicit_value");
1299           uvalue = read_uleb128 (data, &bytes_read, end);
1300           data += bytes_read;
1301           data = display_block (data, uvalue, end, ' ');
1302           break;
1303
1304           /* GNU extensions.  */
1305         case DW_OP_GNU_push_tls_address:
1306           printf (_("DW_OP_GNU_push_tls_address or DW_OP_HP_unknown"));
1307           break;
1308         case DW_OP_GNU_uninit:
1309           printf ("DW_OP_GNU_uninit");
1310           /* FIXME: Is there data associated with this OP ?  */
1311           break;
1312         case DW_OP_GNU_encoded_addr:
1313           {
1314             int encoding = 0;
1315             dwarf_vma addr;
1316
1317             if (data < end)
1318               encoding = *data++;
1319             addr = get_encoded_value (&data, encoding, section, end);
1320
1321             printf ("DW_OP_GNU_encoded_addr: fmt:%02x addr:", encoding);
1322             print_dwarf_vma (addr, pointer_size);
1323           }
1324           break;
1325         case DW_OP_implicit_pointer:
1326         case DW_OP_GNU_implicit_pointer:
1327           /* XXX: Strictly speaking for 64-bit DWARF3 files
1328              this ought to be an 8-byte wide computation.  */
1329           if (dwarf_version == -1)
1330             {
1331               printf (_("(%s in frame info)"),
1332                       (op == DW_OP_implicit_pointer
1333                        ? "DW_OP_implicit_pointer"
1334                        : "DW_OP_GNU_implicit_pointer"));
1335               /* No way to tell where the next op is, so just bail.  */
1336               return need_frame_base;
1337             }
1338           if (dwarf_version == 2)
1339             {
1340               SAFE_BYTE_GET_AND_INC (uvalue, data, pointer_size, end);
1341             }
1342           else
1343             {
1344               SAFE_BYTE_GET_AND_INC (uvalue, data, offset_size, end);
1345             }
1346           printf ("%s: <0x%s> %s",
1347                   (op == DW_OP_implicit_pointer
1348                    ? "DW_OP_implicit_pointer" : "DW_OP_GNU_implicit_pointer"),
1349                   dwarf_vmatoa ("x", uvalue),
1350                   dwarf_vmatoa ("d", read_sleb128 (data,
1351                                                    &bytes_read, end)));
1352           data += bytes_read;
1353           break;
1354         case DW_OP_entry_value:
1355         case DW_OP_GNU_entry_value:
1356           uvalue = read_uleb128 (data, &bytes_read, end);
1357           data += bytes_read;
1358           /* PR 17531: file: 0cc9cd00.  */
1359           if (uvalue > (dwarf_vma) (end - data))
1360             uvalue = end - data;
1361           printf ("%s: (", (op == DW_OP_entry_value ? "DW_OP_entry_value"
1362                                                     : "DW_OP_GNU_entry_value"));
1363           if (decode_location_expression (data, pointer_size, offset_size,
1364                                           dwarf_version, uvalue,
1365                                           cu_offset, section))
1366             need_frame_base = 1;
1367           putchar (')');
1368           data += uvalue;
1369           if (data > end)
1370             data = end;
1371           break;
1372         case DW_OP_const_type:
1373         case DW_OP_GNU_const_type:
1374           uvalue = read_uleb128 (data, &bytes_read, end);
1375           data += bytes_read;
1376           printf ("%s: <0x%s> ",
1377                   (op == DW_OP_const_type ? "DW_OP_const_type"
1378                                           : "DW_OP_GNU_const_type"),
1379                   dwarf_vmatoa ("x", cu_offset + uvalue));
1380           SAFE_BYTE_GET_AND_INC (uvalue, data, 1, end);
1381           data = display_block (data, uvalue, end, ' ');
1382           break;
1383         case DW_OP_regval_type:
1384         case DW_OP_GNU_regval_type:
1385           uvalue = read_uleb128 (data, &bytes_read, end);
1386           data += bytes_read;
1387           printf ("%s: %s (%s)",
1388                   (op == DW_OP_regval_type ? "DW_OP_regval_type"
1389                                            : "DW_OP_GNU_regval_type"),
1390                   dwarf_vmatoa ("u", uvalue), regname (uvalue, 1));
1391           uvalue = read_uleb128 (data, &bytes_read, end);
1392           data += bytes_read;
1393           printf (" <0x%s>", dwarf_vmatoa ("x", cu_offset + uvalue));
1394           break;
1395         case DW_OP_deref_type:
1396         case DW_OP_GNU_deref_type:
1397           SAFE_BYTE_GET_AND_INC (uvalue, data, 1, end);
1398           printf ("%s: %ld",
1399                   (op == DW_OP_deref_type ? "DW_OP_deref_type"
1400                                           : "DW_OP_GNU_deref_type"),
1401                   (long) uvalue);
1402           uvalue = read_uleb128 (data, &bytes_read, end);
1403           data += bytes_read;
1404           printf (" <0x%s>", dwarf_vmatoa ("x", cu_offset + uvalue));
1405           break;
1406         case DW_OP_convert:
1407         case DW_OP_GNU_convert:
1408           uvalue = read_uleb128 (data, &bytes_read, end);
1409           data += bytes_read;
1410           printf ("%s <0x%s>",
1411                   (op == DW_OP_convert ? "DW_OP_convert" : "DW_OP_GNU_convert"),
1412                   dwarf_vmatoa ("x", uvalue ? cu_offset + uvalue : 0));
1413           break;
1414         case DW_OP_reinterpret:
1415         case DW_OP_GNU_reinterpret:
1416           uvalue = read_uleb128 (data, &bytes_read, end);
1417           data += bytes_read;
1418           printf ("%s <0x%s>",
1419                   (op == DW_OP_reinterpret ? "DW_OP_reinterpret"
1420                                            : "DW_OP_GNU_reinterpret"),
1421                   dwarf_vmatoa ("x", uvalue ? cu_offset + uvalue : 0));
1422           break;
1423         case DW_OP_GNU_parameter_ref:
1424           SAFE_BYTE_GET_AND_INC (uvalue, data, 4, end);
1425           printf ("DW_OP_GNU_parameter_ref: <0x%s>",
1426                   dwarf_vmatoa ("x", cu_offset + uvalue));
1427           break;
1428         case DW_OP_GNU_addr_index:
1429           uvalue = read_uleb128 (data, &bytes_read, end);
1430           data += bytes_read;
1431           printf ("DW_OP_GNU_addr_index <0x%s>", dwarf_vmatoa ("x", uvalue));
1432           break;
1433         case DW_OP_GNU_const_index:
1434           uvalue = read_uleb128 (data, &bytes_read, end);
1435           data += bytes_read;
1436           printf ("DW_OP_GNU_const_index <0x%s>", dwarf_vmatoa ("x", uvalue));
1437           break;
1438
1439           /* HP extensions.  */
1440         case DW_OP_HP_is_value:
1441           printf ("DW_OP_HP_is_value");
1442           /* FIXME: Is there data associated with this OP ?  */
1443           break;
1444         case DW_OP_HP_fltconst4:
1445           printf ("DW_OP_HP_fltconst4");
1446           /* FIXME: Is there data associated with this OP ?  */
1447           break;
1448         case DW_OP_HP_fltconst8:
1449           printf ("DW_OP_HP_fltconst8");
1450           /* FIXME: Is there data associated with this OP ?  */
1451           break;
1452         case DW_OP_HP_mod_range:
1453           printf ("DW_OP_HP_mod_range");
1454           /* FIXME: Is there data associated with this OP ?  */
1455           break;
1456         case DW_OP_HP_unmod_range:
1457           printf ("DW_OP_HP_unmod_range");
1458           /* FIXME: Is there data associated with this OP ?  */
1459           break;
1460         case DW_OP_HP_tls:
1461           printf ("DW_OP_HP_tls");
1462           /* FIXME: Is there data associated with this OP ?  */
1463           break;
1464
1465           /* PGI (STMicroelectronics) extensions.  */
1466         case DW_OP_PGI_omp_thread_num:
1467           /* Pushes the thread number for the current thread as it would be
1468              returned by the standard OpenMP library function:
1469              omp_get_thread_num().  The "current thread" is the thread for
1470              which the expression is being evaluated.  */
1471           printf ("DW_OP_PGI_omp_thread_num");
1472           break;
1473
1474         default:
1475           if (op >= DW_OP_lo_user
1476               && op <= DW_OP_hi_user)
1477             printf (_("(User defined location op 0x%x)"), op);
1478           else
1479             printf (_("(Unknown location op 0x%x)"), op);
1480           /* No way to tell where the next op is, so just bail.  */
1481           return need_frame_base;
1482         }
1483
1484       /* Separate the ops.  */
1485       if (data < end)
1486         printf ("; ");
1487     }
1488
1489   return need_frame_base;
1490 }
1491
1492 /* Find the CU or TU set corresponding to the given CU_OFFSET.
1493    This is used for DWARF package files.  */
1494
1495 static struct cu_tu_set *
1496 find_cu_tu_set_v2 (dwarf_vma cu_offset, int do_types)
1497 {
1498   struct cu_tu_set *p;
1499   unsigned int nsets;
1500   unsigned int dw_sect;
1501
1502   if (do_types)
1503     {
1504       p = tu_sets;
1505       nsets = tu_count;
1506       dw_sect = DW_SECT_TYPES;
1507     }
1508   else
1509     {
1510       p = cu_sets;
1511       nsets = cu_count;
1512       dw_sect = DW_SECT_INFO;
1513     }
1514   while (nsets > 0)
1515     {
1516       if (p->section_offsets [dw_sect] == cu_offset)
1517         return p;
1518       p++;
1519       nsets--;
1520     }
1521   return NULL;
1522 }
1523
1524 /* Add INC to HIGH_BITS:LOW_BITS.  */
1525 static void
1526 add64 (dwarf_vma * high_bits, dwarf_vma * low_bits, dwarf_vma inc)
1527 {
1528   dwarf_vma tmp = * low_bits;
1529
1530   tmp += inc;
1531
1532   /* FIXME: There is probably a better way of handling this:
1533
1534      We need to cope with dwarf_vma being a 32-bit or 64-bit
1535      type.  Plus regardless of its size LOW_BITS is meant to
1536      only hold 32-bits, so if there is overflow or wrap around
1537      we must propagate into HIGH_BITS.  */
1538   if (tmp < * low_bits)
1539     {
1540       ++ * high_bits;
1541     }
1542   else if (sizeof (tmp) > 8
1543            && (tmp >> 31) > 1)
1544     {
1545       ++ * high_bits;
1546       tmp &= 0xFFFFFFFF;
1547     }
1548
1549   * low_bits = tmp;
1550 }
1551
1552 static unsigned char *
1553 read_and_display_attr_value (unsigned long attribute,
1554                              unsigned long form,
1555                              dwarf_signed_vma implicit_const,
1556                              unsigned char * data,
1557                              unsigned char * end,
1558                              dwarf_vma cu_offset,
1559                              dwarf_vma pointer_size,
1560                              dwarf_vma offset_size,
1561                              int dwarf_version,
1562                              debug_info * debug_info_p,
1563                              int do_loc,
1564                              struct dwarf_section * section,
1565                              struct cu_tu_set * this_set, char delimiter)
1566 {
1567   dwarf_vma uvalue = 0;
1568   unsigned char *block_start = NULL;
1569   unsigned char * orig_data = data;
1570   unsigned int bytes_read;
1571
1572   if (data > end || (data == end && form != DW_FORM_flag_present))
1573     {
1574       warn (_("Corrupt attribute\n"));
1575       return data;
1576     }
1577
1578   switch (form)
1579     {
1580     default:
1581       break;
1582
1583     case DW_FORM_ref_addr:
1584       if (dwarf_version == 2)
1585         SAFE_BYTE_GET_AND_INC (uvalue, data, pointer_size, end);
1586       else if (dwarf_version == 3 || dwarf_version == 4)
1587         SAFE_BYTE_GET_AND_INC (uvalue, data, offset_size, end);
1588       else
1589         error (_("Internal error: DWARF version is not 2, 3 or 4.\n"));
1590
1591       break;
1592
1593     case DW_FORM_addr:
1594       SAFE_BYTE_GET_AND_INC (uvalue, data, pointer_size, end);
1595       break;
1596
1597     case DW_FORM_strp:
1598     case DW_FORM_line_strp:
1599     case DW_FORM_sec_offset:
1600     case DW_FORM_GNU_ref_alt:
1601     case DW_FORM_GNU_strp_alt:
1602       SAFE_BYTE_GET_AND_INC (uvalue, data, offset_size, end);
1603       break;
1604
1605     case DW_FORM_flag_present:
1606       uvalue = 1;
1607       break;
1608
1609     case DW_FORM_ref1:
1610     case DW_FORM_flag:
1611     case DW_FORM_data1:
1612       SAFE_BYTE_GET_AND_INC (uvalue, data, 1, end);
1613       break;
1614
1615     case DW_FORM_ref2:
1616     case DW_FORM_data2:
1617       SAFE_BYTE_GET_AND_INC (uvalue, data, 2, end);
1618       break;
1619
1620     case DW_FORM_ref4:
1621     case DW_FORM_data4:
1622       SAFE_BYTE_GET_AND_INC (uvalue, data, 4, end);
1623       break;
1624
1625     case DW_FORM_sdata:
1626       uvalue = read_sleb128 (data, & bytes_read, end);
1627       data += bytes_read;
1628       break;
1629
1630     case DW_FORM_GNU_str_index:
1631       uvalue = read_uleb128 (data, & bytes_read, end);
1632       data += bytes_read;
1633       break;
1634
1635     case DW_FORM_ref_udata:
1636     case DW_FORM_udata:
1637       uvalue = read_uleb128 (data, & bytes_read, end);
1638       data += bytes_read;
1639       break;
1640
1641     case DW_FORM_indirect:
1642       form = read_uleb128 (data, & bytes_read, end);
1643       data += bytes_read;
1644       if (!do_loc)
1645         printf ("%c%s", delimiter, get_FORM_name (form));
1646       if (form == DW_FORM_implicit_const)
1647         {
1648           implicit_const = read_sleb128 (data, & bytes_read, end);
1649           data += bytes_read;
1650         }
1651       return read_and_display_attr_value (attribute, form, implicit_const, data,
1652                                           end, cu_offset, pointer_size,
1653                                           offset_size, dwarf_version,
1654                                           debug_info_p, do_loc,
1655                                           section, this_set, delimiter);
1656     case DW_FORM_GNU_addr_index:
1657       uvalue = read_uleb128 (data, & bytes_read, end);
1658       data += bytes_read;
1659       break;
1660     }
1661
1662   switch (form)
1663     {
1664     case DW_FORM_ref_addr:
1665       if (!do_loc)
1666         printf ("%c<0x%s>", delimiter, dwarf_vmatoa ("x",uvalue));
1667       break;
1668
1669     case DW_FORM_GNU_ref_alt:
1670       if (!do_loc)
1671         printf ("%c<alt 0x%s>", delimiter, dwarf_vmatoa ("x",uvalue));
1672       break;
1673
1674     case DW_FORM_ref1:
1675     case DW_FORM_ref2:
1676     case DW_FORM_ref4:
1677     case DW_FORM_ref_udata:
1678       if (!do_loc)
1679         printf ("%c<0x%s>", delimiter, dwarf_vmatoa ("x", uvalue + cu_offset));
1680       break;
1681
1682     case DW_FORM_data4:
1683     case DW_FORM_addr:
1684     case DW_FORM_sec_offset:
1685       if (!do_loc)
1686         printf ("%c0x%s", delimiter, dwarf_vmatoa ("x", uvalue));
1687       break;
1688
1689     case DW_FORM_flag_present:
1690     case DW_FORM_flag:
1691     case DW_FORM_data1:
1692     case DW_FORM_data2:
1693     case DW_FORM_sdata:
1694     case DW_FORM_udata:
1695       if (!do_loc)
1696         printf ("%c%s", delimiter, dwarf_vmatoa ("d", uvalue));
1697       break;
1698
1699     case DW_FORM_implicit_const:
1700       if (!do_loc)
1701         printf ("%c%s", delimiter, dwarf_vmatoa ("d", implicit_const));
1702       break;
1703
1704     case DW_FORM_ref8:
1705     case DW_FORM_data8:
1706       if (!do_loc)
1707         {
1708           dwarf_vma high_bits;
1709           dwarf_vma utmp;
1710           char buf[64];
1711
1712           SAFE_BYTE_GET64 (data, &high_bits, &uvalue, end);
1713           utmp = uvalue;
1714           if (form == DW_FORM_ref8)
1715             add64 (& high_bits, & utmp, cu_offset);
1716           printf ("%c0x%s", delimiter,
1717                   dwarf_vmatoa64 (high_bits, utmp, buf, sizeof (buf)));
1718         }
1719
1720       if ((do_loc || do_debug_loc || do_debug_ranges)
1721           && num_debug_info_entries == 0)
1722         {
1723           if (sizeof (uvalue) == 8)
1724             SAFE_BYTE_GET (uvalue, data, 8, end);
1725           else
1726             error (_("DW_FORM_data8 is unsupported when sizeof (dwarf_vma) != 8\n"));
1727         }
1728
1729       data += 8;
1730       break;
1731
1732     case DW_FORM_data16:
1733       if (!do_loc)
1734         {
1735           dwarf_vma left_high_bits, left_low_bits;
1736           dwarf_vma right_high_bits, right_low_bits;
1737
1738           SAFE_BYTE_GET64 (data, &left_high_bits, &left_low_bits, end);
1739           SAFE_BYTE_GET64 (data + 8, &right_high_bits, &right_low_bits, end);
1740           if (byte_get == byte_get_little_endian)
1741             {
1742               /* Swap them.  */
1743               left_high_bits ^= right_high_bits;
1744               right_high_bits ^= left_high_bits;
1745               left_high_bits ^= right_high_bits;
1746               left_low_bits ^= right_low_bits;
1747               right_low_bits ^= left_low_bits;
1748               left_low_bits ^= right_low_bits;
1749             }
1750           printf (" 0x%08" DWARF_VMA_FMT "x%08" DWARF_VMA_FMT "x"
1751                   "%08" DWARF_VMA_FMT "x%08" DWARF_VMA_FMT "x",
1752                   left_high_bits, left_low_bits, right_high_bits,
1753                   right_low_bits);
1754         }
1755       data += 16;
1756       break;
1757
1758     case DW_FORM_string:
1759       if (!do_loc)
1760         printf ("%c%.*s", delimiter, (int) (end - data), data);
1761       data += strnlen ((char *) data, end - data) + 1;
1762       break;
1763
1764     case DW_FORM_block:
1765     case DW_FORM_exprloc:
1766       uvalue = read_uleb128 (data, & bytes_read, end);
1767       block_start = data + bytes_read;
1768       if (block_start >= end)
1769         {
1770           warn (_("Block ends prematurely\n"));
1771           uvalue = 0;
1772           block_start = end;
1773         }
1774       /* FIXME: Testing "(block_start + uvalue) < block_start" miscompiles with
1775          gcc 4.8.3 running on an x86_64 host in 32-bit mode.  So we pre-compute
1776          block_start + uvalue here.  */
1777       data = block_start + uvalue;
1778       /* PR 17512: file: 008-103549-0.001:0.1.  */
1779       if (block_start + uvalue > end || data < block_start)
1780         {
1781           warn (_("Corrupt attribute block length: %lx\n"), (long) uvalue);
1782           uvalue = end - block_start;
1783         }
1784       if (do_loc)
1785         data = block_start + uvalue;
1786       else
1787         data = display_block (block_start, uvalue, end, delimiter);
1788       break;
1789
1790     case DW_FORM_block1:
1791       SAFE_BYTE_GET (uvalue, data, 1, end);
1792       block_start = data + 1;
1793       if (block_start >= end)
1794         {
1795           warn (_("Block ends prematurely\n"));
1796           uvalue = 0;
1797           block_start = end;
1798         }
1799       data = block_start + uvalue;
1800       if (block_start + uvalue > end || data < block_start)
1801         {
1802           warn (_("Corrupt attribute block length: %lx\n"), (long) uvalue);
1803           uvalue = end - block_start;
1804         }
1805       if (do_loc)
1806         data = block_start + uvalue;
1807       else
1808         data = display_block (block_start, uvalue, end, delimiter);
1809       break;
1810
1811     case DW_FORM_block2:
1812       SAFE_BYTE_GET (uvalue, data, 2, end);
1813       block_start = data + 2;
1814       if (block_start >= end)
1815         {
1816           warn (_("Block ends prematurely\n"));
1817           uvalue = 0;
1818           block_start = end;
1819         }
1820       data = block_start + uvalue;
1821       if (block_start + uvalue > end || data < block_start)
1822         {
1823           warn (_("Corrupt attribute block length: %lx\n"), (long) uvalue);
1824           uvalue = end - block_start;
1825         }
1826       if (do_loc)
1827         data = block_start + uvalue;
1828       else
1829         data = display_block (block_start, uvalue, end, delimiter);
1830       break;
1831
1832     case DW_FORM_block4:
1833       SAFE_BYTE_GET (uvalue, data, 4, end);
1834       block_start = data + 4;
1835       /* PR 17512: file: 3371-3907-0.004.  */
1836       if (block_start >= end)
1837         {
1838           warn (_("Block ends prematurely\n"));
1839           uvalue = 0;
1840           block_start = end;
1841         }
1842       data = block_start + uvalue;
1843       if (block_start + uvalue > end
1844           /* PR 17531: file: 5b5f0592.  */
1845           || data < block_start)
1846         {
1847           warn (_("Corrupt attribute block length: %lx\n"), (long) uvalue);
1848           uvalue = end - block_start;
1849         }
1850       if (do_loc)
1851         data = block_start + uvalue;
1852       else
1853         data = display_block (block_start, uvalue, end, delimiter);
1854       break;
1855
1856     case DW_FORM_strp:
1857       if (!do_loc)
1858         printf (_("%c(indirect string, offset: 0x%s): %s"), delimiter,
1859                 dwarf_vmatoa ("x", uvalue),
1860                 fetch_indirect_string (uvalue));
1861       break;
1862
1863     case DW_FORM_line_strp:
1864       if (!do_loc)
1865         printf (_("%c(indirect line string, offset: 0x%s): %s"), delimiter,
1866                 dwarf_vmatoa ("x", uvalue),
1867                 fetch_indirect_line_string (uvalue));
1868       break;
1869
1870     case DW_FORM_GNU_str_index:
1871       if (!do_loc)
1872         {
1873           const char *suffix = strrchr (section->name, '.');
1874           int dwo = (suffix && strcmp (suffix, ".dwo") == 0) ? 1 : 0;
1875
1876           printf (_("%c(indexed string: 0x%s): %s"), delimiter,
1877                   dwarf_vmatoa ("x", uvalue),
1878                   fetch_indexed_string (uvalue, this_set, offset_size, dwo));
1879         }
1880       break;
1881
1882     case DW_FORM_GNU_strp_alt:
1883       if (!do_loc)
1884         printf (_("%c(alt indirect string, offset: 0x%s)"), delimiter,
1885                 dwarf_vmatoa ("x", uvalue));
1886       break;
1887
1888     case DW_FORM_indirect:
1889       /* Handled above.  */
1890       break;
1891
1892     case DW_FORM_ref_sig8:
1893       if (!do_loc)
1894         {
1895           dwarf_vma high_bits;
1896           char buf[64];
1897
1898           SAFE_BYTE_GET64 (data, &high_bits, &uvalue, end);
1899           printf ("%csignature: 0x%s", delimiter,
1900                   dwarf_vmatoa64 (high_bits, uvalue, buf, sizeof (buf)));
1901         }
1902       data += 8;
1903       break;
1904
1905     case DW_FORM_GNU_addr_index:
1906       if (!do_loc)
1907         printf (_("%c(addr_index: 0x%s): %s"), delimiter,
1908                 dwarf_vmatoa ("x", uvalue),
1909                 fetch_indexed_value (uvalue * pointer_size, pointer_size));
1910       break;
1911
1912     default:
1913       warn (_("Unrecognized form: %lu\n"), form);
1914       break;
1915     }
1916
1917   if ((do_loc || do_debug_loc || do_debug_ranges)
1918       && num_debug_info_entries == 0
1919       && debug_info_p != NULL)
1920     {
1921       switch (attribute)
1922         {
1923         case DW_AT_frame_base:
1924           have_frame_base = 1;
1925           /* Fall through.  */
1926         case DW_AT_location:
1927         case DW_AT_string_length:
1928         case DW_AT_return_addr:
1929         case DW_AT_data_member_location:
1930         case DW_AT_vtable_elem_location:
1931         case DW_AT_segment:
1932         case DW_AT_static_link:
1933         case DW_AT_use_location:
1934         case DW_AT_call_value:
1935         case DW_AT_GNU_call_site_value:
1936         case DW_AT_call_data_value:
1937         case DW_AT_GNU_call_site_data_value:
1938         case DW_AT_call_target:
1939         case DW_AT_GNU_call_site_target:
1940         case DW_AT_call_target_clobbered:
1941         case DW_AT_GNU_call_site_target_clobbered:
1942           if ((dwarf_version < 4
1943                && (form == DW_FORM_data4 || form == DW_FORM_data8))
1944               || form == DW_FORM_sec_offset)
1945             {
1946               /* Process location list.  */
1947               unsigned int lmax = debug_info_p->max_loc_offsets;
1948               unsigned int num = debug_info_p->num_loc_offsets;
1949
1950               if (lmax == 0 || num >= lmax)
1951                 {
1952                   lmax += 1024;
1953                   debug_info_p->loc_offsets = (dwarf_vma *)
1954                     xcrealloc (debug_info_p->loc_offsets,
1955                                lmax, sizeof (*debug_info_p->loc_offsets));
1956                   debug_info_p->have_frame_base = (int *)
1957                     xcrealloc (debug_info_p->have_frame_base,
1958                                lmax, sizeof (*debug_info_p->have_frame_base));
1959                   debug_info_p->max_loc_offsets = lmax;
1960                 }
1961               if (this_set != NULL)
1962                 uvalue += this_set->section_offsets [DW_SECT_LOC];
1963               debug_info_p->loc_offsets [num] = uvalue;
1964               debug_info_p->have_frame_base [num] = have_frame_base;
1965               debug_info_p->num_loc_offsets++;
1966             }
1967           break;
1968
1969         case DW_AT_low_pc:
1970           if (need_base_address)
1971             debug_info_p->base_address = uvalue;
1972           break;
1973
1974         case DW_AT_GNU_addr_base:
1975           debug_info_p->addr_base = uvalue;
1976           break;
1977
1978         case DW_AT_GNU_ranges_base:
1979           debug_info_p->ranges_base = uvalue;
1980           break;
1981
1982         case DW_AT_ranges:
1983           if ((dwarf_version < 4
1984                && (form == DW_FORM_data4 || form == DW_FORM_data8))
1985               || form == DW_FORM_sec_offset)
1986             {
1987               /* Process range list.  */
1988               unsigned int lmax = debug_info_p->max_range_lists;
1989               unsigned int num = debug_info_p->num_range_lists;
1990
1991               if (lmax == 0 || num >= lmax)
1992                 {
1993                   lmax += 1024;
1994                   debug_info_p->range_lists = (dwarf_vma *)
1995                     xcrealloc (debug_info_p->range_lists,
1996                                lmax, sizeof (*debug_info_p->range_lists));
1997                   debug_info_p->max_range_lists = lmax;
1998                 }
1999               debug_info_p->range_lists [num] = uvalue;
2000               debug_info_p->num_range_lists++;
2001             }
2002           break;
2003
2004         default:
2005           break;
2006         }
2007     }
2008
2009   if (do_loc || attribute == 0)
2010     return data;
2011
2012   /* For some attributes we can display further information.  */
2013   switch (attribute)
2014     {
2015     case DW_AT_inline:
2016       printf ("\t");
2017       switch (uvalue)
2018         {
2019         case DW_INL_not_inlined:
2020           printf (_("(not inlined)"));
2021           break;
2022         case DW_INL_inlined:
2023           printf (_("(inlined)"));
2024           break;
2025         case DW_INL_declared_not_inlined:
2026           printf (_("(declared as inline but ignored)"));
2027           break;
2028         case DW_INL_declared_inlined:
2029           printf (_("(declared as inline and inlined)"));
2030           break;
2031         default:
2032           printf (_("  (Unknown inline attribute value: %s)"),
2033                   dwarf_vmatoa ("x", uvalue));
2034           break;
2035         }
2036       break;
2037
2038     case DW_AT_language:
2039       printf ("\t");
2040       switch (uvalue)
2041         {
2042           /* Ordered by the numeric value of these constants.  */
2043         case DW_LANG_C89:               printf ("(ANSI C)"); break;
2044         case DW_LANG_C:                 printf ("(non-ANSI C)"); break;
2045         case DW_LANG_Ada83:             printf ("(Ada)"); break;
2046         case DW_LANG_C_plus_plus:       printf ("(C++)"); break;
2047         case DW_LANG_Cobol74:           printf ("(Cobol 74)"); break;
2048         case DW_LANG_Cobol85:           printf ("(Cobol 85)"); break;
2049         case DW_LANG_Fortran77:         printf ("(FORTRAN 77)"); break;
2050         case DW_LANG_Fortran90:         printf ("(Fortran 90)"); break;
2051         case DW_LANG_Pascal83:          printf ("(ANSI Pascal)"); break;
2052         case DW_LANG_Modula2:           printf ("(Modula 2)"); break;
2053           /* DWARF 2.1 values.  */
2054         case DW_LANG_Java:              printf ("(Java)"); break;
2055         case DW_LANG_C99:               printf ("(ANSI C99)"); break;
2056         case DW_LANG_Ada95:             printf ("(ADA 95)"); break;
2057         case DW_LANG_Fortran95:         printf ("(Fortran 95)"); break;
2058           /* DWARF 3 values.  */
2059         case DW_LANG_PLI:               printf ("(PLI)"); break;
2060         case DW_LANG_ObjC:              printf ("(Objective C)"); break;
2061         case DW_LANG_ObjC_plus_plus:    printf ("(Objective C++)"); break;
2062         case DW_LANG_UPC:               printf ("(Unified Parallel C)"); break;
2063         case DW_LANG_D:                 printf ("(D)"); break;
2064           /* DWARF 4 values.  */
2065         case DW_LANG_Python:            printf ("(Python)"); break;
2066           /* DWARF 5 values.  */
2067         case DW_LANG_Go:                printf ("(Go)"); break;
2068         case DW_LANG_C_plus_plus_11:    printf ("(C++11)"); break;
2069         case DW_LANG_C11:               printf ("(C11)"); break;
2070         case DW_LANG_C_plus_plus_14:    printf ("(C++14)"); break;
2071         case DW_LANG_Fortran03:         printf ("(Fortran 03)"); break;
2072         case DW_LANG_Fortran08:         printf ("(Fortran 08)"); break;
2073           /* MIPS extension.  */
2074         case DW_LANG_Mips_Assembler:    printf ("(MIPS assembler)"); break;
2075           /* UPC extension.  */
2076         case DW_LANG_Upc:               printf ("(Unified Parallel C)"); break;
2077         default:
2078           if (uvalue >= DW_LANG_lo_user && uvalue <= DW_LANG_hi_user)
2079             printf (_("(implementation defined: %s)"),
2080                     dwarf_vmatoa ("x", uvalue));
2081           else
2082             printf (_("(Unknown: %s)"), dwarf_vmatoa ("x", uvalue));
2083           break;
2084         }
2085       break;
2086
2087     case DW_AT_encoding:
2088       printf ("\t");
2089       switch (uvalue)
2090         {
2091         case DW_ATE_void:               printf ("(void)"); break;
2092         case DW_ATE_address:            printf ("(machine address)"); break;
2093         case DW_ATE_boolean:            printf ("(boolean)"); break;
2094         case DW_ATE_complex_float:      printf ("(complex float)"); break;
2095         case DW_ATE_float:              printf ("(float)"); break;
2096         case DW_ATE_signed:             printf ("(signed)"); break;
2097         case DW_ATE_signed_char:        printf ("(signed char)"); break;
2098         case DW_ATE_unsigned:           printf ("(unsigned)"); break;
2099         case DW_ATE_unsigned_char:      printf ("(unsigned char)"); break;
2100           /* DWARF 2.1 values:  */
2101         case DW_ATE_imaginary_float:    printf ("(imaginary float)"); break;
2102         case DW_ATE_decimal_float:      printf ("(decimal float)"); break;
2103           /* DWARF 3 values:  */
2104         case DW_ATE_packed_decimal:     printf ("(packed_decimal)"); break;
2105         case DW_ATE_numeric_string:     printf ("(numeric_string)"); break;
2106         case DW_ATE_edited:             printf ("(edited)"); break;
2107         case DW_ATE_signed_fixed:       printf ("(signed_fixed)"); break;
2108         case DW_ATE_unsigned_fixed:     printf ("(unsigned_fixed)"); break;
2109           /* HP extensions:  */
2110         case DW_ATE_HP_float80:         printf ("(HP_float80)"); break;
2111         case DW_ATE_HP_complex_float80: printf ("(HP_complex_float80)"); break;
2112         case DW_ATE_HP_float128:        printf ("(HP_float128)"); break;
2113         case DW_ATE_HP_complex_float128:printf ("(HP_complex_float128)"); break;
2114         case DW_ATE_HP_floathpintel:    printf ("(HP_floathpintel)"); break;
2115         case DW_ATE_HP_imaginary_float80:       printf ("(HP_imaginary_float80)"); break;
2116         case DW_ATE_HP_imaginary_float128:      printf ("(HP_imaginary_float128)"); break;
2117           /* DWARF 4 values:  */
2118         case DW_ATE_UTF:                printf ("(unicode string)"); break;
2119
2120         default:
2121           if (uvalue >= DW_ATE_lo_user
2122               && uvalue <= DW_ATE_hi_user)
2123             printf (_("(user defined type)"));
2124           else
2125             printf (_("(unknown type)"));
2126           break;
2127         }
2128       break;
2129
2130     case DW_AT_accessibility:
2131       printf ("\t");
2132       switch (uvalue)
2133         {
2134         case DW_ACCESS_public:          printf ("(public)"); break;
2135         case DW_ACCESS_protected:       printf ("(protected)"); break;
2136         case DW_ACCESS_private:         printf ("(private)"); break;
2137         default:
2138           printf (_("(unknown accessibility)"));
2139           break;
2140         }
2141       break;
2142
2143     case DW_AT_visibility:
2144       printf ("\t");
2145       switch (uvalue)
2146         {
2147         case DW_VIS_local:              printf ("(local)"); break;
2148         case DW_VIS_exported:           printf ("(exported)"); break;
2149         case DW_VIS_qualified:          printf ("(qualified)"); break;
2150         default:                        printf (_("(unknown visibility)")); break;
2151         }
2152       break;
2153
2154     case DW_AT_virtuality:
2155       printf ("\t");
2156       switch (uvalue)
2157         {
2158         case DW_VIRTUALITY_none:        printf ("(none)"); break;
2159         case DW_VIRTUALITY_virtual:     printf ("(virtual)"); break;
2160         case DW_VIRTUALITY_pure_virtual:printf ("(pure_virtual)"); break;
2161         default:                        printf (_("(unknown virtuality)")); break;
2162         }
2163       break;
2164
2165     case DW_AT_identifier_case:
2166       printf ("\t");
2167       switch (uvalue)
2168         {
2169         case DW_ID_case_sensitive:      printf ("(case_sensitive)"); break;
2170         case DW_ID_up_case:             printf ("(up_case)"); break;
2171         case DW_ID_down_case:           printf ("(down_case)"); break;
2172         case DW_ID_case_insensitive:    printf ("(case_insensitive)"); break;
2173         default:                        printf (_("(unknown case)")); break;
2174         }
2175       break;
2176
2177     case DW_AT_calling_convention:
2178       printf ("\t");
2179       switch (uvalue)
2180         {
2181         case DW_CC_normal:      printf ("(normal)"); break;
2182         case DW_CC_program:     printf ("(program)"); break;
2183         case DW_CC_nocall:      printf ("(nocall)"); break;
2184         default:
2185           if (uvalue >= DW_CC_lo_user
2186               && uvalue <= DW_CC_hi_user)
2187             printf (_("(user defined)"));
2188           else
2189             printf (_("(unknown convention)"));
2190         }
2191       break;
2192
2193     case DW_AT_ordering:
2194       printf ("\t");
2195       switch (uvalue)
2196         {
2197         case -1: printf (_("(undefined)")); break;
2198         case 0:  printf ("(row major)"); break;
2199         case 1:  printf ("(column major)"); break;
2200         }
2201       break;
2202
2203     case DW_AT_frame_base:
2204       have_frame_base = 1;
2205       /* Fall through.  */
2206     case DW_AT_location:
2207     case DW_AT_string_length:
2208     case DW_AT_return_addr:
2209     case DW_AT_data_member_location:
2210     case DW_AT_vtable_elem_location:
2211     case DW_AT_segment:
2212     case DW_AT_static_link:
2213     case DW_AT_use_location:
2214     case DW_AT_call_value:
2215     case DW_AT_GNU_call_site_value:
2216     case DW_AT_call_data_value:
2217     case DW_AT_GNU_call_site_data_value:
2218     case DW_AT_call_target:
2219     case DW_AT_GNU_call_site_target:
2220     case DW_AT_call_target_clobbered:
2221     case DW_AT_GNU_call_site_target_clobbered:
2222       if ((dwarf_version < 4
2223            && (form == DW_FORM_data4 || form == DW_FORM_data8))
2224           || form == DW_FORM_sec_offset)
2225         printf (_(" (location list)"));
2226       /* Fall through.  */
2227     case DW_AT_allocated:
2228     case DW_AT_associated:
2229     case DW_AT_data_location:
2230     case DW_AT_stride:
2231     case DW_AT_upper_bound:
2232     case DW_AT_lower_bound:
2233       if (block_start)
2234         {
2235           int need_frame_base;
2236
2237           printf ("\t(");
2238           need_frame_base = decode_location_expression (block_start,
2239                                                         pointer_size,
2240                                                         offset_size,
2241                                                         dwarf_version,
2242                                                         uvalue,
2243                                                         cu_offset, section);
2244           printf (")");
2245           if (need_frame_base && !have_frame_base)
2246             printf (_(" [without DW_AT_frame_base]"));
2247         }
2248       break;
2249
2250     case DW_AT_import:
2251       {
2252         if (form == DW_FORM_ref_sig8
2253             || form == DW_FORM_GNU_ref_alt)
2254           break;
2255
2256         if (form == DW_FORM_ref1
2257             || form == DW_FORM_ref2
2258             || form == DW_FORM_ref4
2259             || form == DW_FORM_ref_udata)
2260           uvalue += cu_offset;
2261
2262         if (uvalue >= section->size)
2263           warn (_("Offset %s used as value for DW_AT_import attribute of DIE at offset 0x%lx is too big.\n"),
2264                 dwarf_vmatoa ("x", uvalue),
2265                 (unsigned long) (orig_data - section->start));
2266         else
2267           {
2268             unsigned long abbrev_number;
2269             abbrev_entry * entry;
2270
2271             abbrev_number = read_uleb128 (section->start + uvalue, NULL, end);
2272
2273             printf (_("\t[Abbrev Number: %ld"), abbrev_number);
2274             /* Don't look up abbrev for DW_FORM_ref_addr, as it very often will
2275                use different abbrev table, and we don't track .debug_info chunks
2276                yet.  */
2277             if (form != DW_FORM_ref_addr)
2278               {
2279                 for (entry = first_abbrev; entry != NULL; entry = entry->next)
2280                   if (entry->entry == abbrev_number)
2281                     break;
2282                 if (entry != NULL)
2283                   printf (" (%s)", get_TAG_name (entry->tag));
2284               }
2285             printf ("]");
2286           }
2287       }
2288       break;
2289
2290     default:
2291       break;
2292     }
2293
2294   return data;
2295 }
2296
2297 static const char *
2298 get_AT_name (unsigned long attribute)
2299 {
2300   const char *name;
2301
2302   if (attribute == 0)
2303     return "DW_AT value: 0";
2304
2305   /* One value is shared by the MIPS and HP extensions:  */
2306   if (attribute == DW_AT_MIPS_fde)
2307     return "DW_AT_MIPS_fde or DW_AT_HP_unmodifiable";
2308
2309   name = get_DW_AT_name (attribute);
2310
2311   if (name == NULL)
2312     {
2313       static char buffer[100];
2314
2315       snprintf (buffer, sizeof (buffer), _("Unknown AT value: %lx"),
2316                 attribute);
2317       return buffer;
2318     }
2319
2320   return name;
2321 }
2322
2323 static unsigned char *
2324 read_and_display_attr (unsigned long attribute,
2325                        unsigned long form,
2326                        dwarf_signed_vma implicit_const,
2327                        unsigned char * data,
2328                        unsigned char * end,
2329                        dwarf_vma cu_offset,
2330                        dwarf_vma pointer_size,
2331                        dwarf_vma offset_size,
2332                        int dwarf_version,
2333                        debug_info * debug_info_p,
2334                        int do_loc,
2335                        struct dwarf_section * section,
2336                        struct cu_tu_set * this_set)
2337 {
2338   if (!do_loc)
2339     printf ("   %-18s:", get_AT_name (attribute));
2340   data = read_and_display_attr_value (attribute, form, implicit_const, data, end,
2341                                       cu_offset, pointer_size, offset_size,
2342                                       dwarf_version, debug_info_p,
2343                                       do_loc, section, this_set, ' ');
2344   if (!do_loc)
2345     printf ("\n");
2346   return data;
2347 }
2348
2349 /* Process the contents of a .debug_info section.  If do_loc is non-zero
2350    then we are scanning for location lists and we do not want to display
2351    anything to the user.  If do_types is non-zero, we are processing
2352    a .debug_types section instead of a .debug_info section.  */
2353
2354 static int
2355 process_debug_info (struct dwarf_section *section,
2356                     void *file,
2357                     enum dwarf_section_display_enum abbrev_sec,
2358                     int do_loc,
2359                     int do_types)
2360 {
2361   unsigned char *start = section->start;
2362   unsigned char *end = start + section->size;
2363   unsigned char *section_begin;
2364   unsigned int unit;
2365   unsigned int num_units = 0;
2366
2367   if ((do_loc || do_debug_loc || do_debug_ranges)
2368       && num_debug_info_entries == 0
2369       && ! do_types)
2370     {
2371       dwarf_vma length;
2372
2373       /* First scan the section to get the number of comp units.  */
2374       for (section_begin = start, num_units = 0; section_begin < end;
2375            num_units ++)
2376         {
2377           /* Read the first 4 bytes.  For a 32-bit DWARF section, this
2378              will be the length.  For a 64-bit DWARF section, it'll be
2379              the escape code 0xffffffff followed by an 8 byte length.  */
2380           SAFE_BYTE_GET (length, section_begin, 4, end);
2381
2382           if (length == 0xffffffff)
2383             {
2384               SAFE_BYTE_GET (length, section_begin + 4, 8, end);
2385               section_begin += length + 12;
2386             }
2387           else if (length >= 0xfffffff0 && length < 0xffffffff)
2388             {
2389               warn (_("Reserved length value (0x%s) found in section %s\n"),
2390                     dwarf_vmatoa ("x", length), section->name);
2391               return 0;
2392             }
2393           else
2394             section_begin += length + 4;
2395
2396           /* Negative values are illegal, they may even cause infinite
2397              looping.  This can happen if we can't accurately apply
2398              relocations to an object file, or if the file is corrupt.  */
2399           if ((signed long) length <= 0 || section_begin < start)
2400             {
2401               warn (_("Corrupt unit length (0x%s) found in section %s\n"),
2402                     dwarf_vmatoa ("x", length), section->name);
2403               return 0;
2404             }
2405         }
2406
2407       if (num_units == 0)
2408         {
2409           error (_("No comp units in %s section ?\n"), section->name);
2410           return 0;
2411         }
2412
2413       /* Then allocate an array to hold the information.  */
2414       debug_information = (debug_info *) cmalloc (num_units,
2415                                                   sizeof (* debug_information));
2416       if (debug_information == NULL)
2417         {
2418           error (_("Not enough memory for a debug info array of %u entries\n"),
2419                  num_units);
2420           alloc_num_debug_info_entries = num_debug_info_entries = 0;
2421           return 0;
2422         }
2423       /* PR 17531: file: 92ca3797.
2424          We cannot rely upon the debug_information array being initialised
2425          before it is used.  A corrupt file could easily contain references
2426          to a unit for which information has not been made available.  So
2427          we ensure that the array is zeroed here.  */
2428       memset (debug_information, 0, num_units * sizeof (*debug_information));
2429
2430       alloc_num_debug_info_entries = num_units;
2431     }
2432
2433   if (!do_loc)
2434     {
2435       if (dwarf_start_die == 0)
2436         printf (_("Contents of the %s section:\n\n"), section->name);
2437
2438       load_debug_section (str, file);
2439       load_debug_section (line_str, file);
2440       load_debug_section (str_dwo, file);
2441       load_debug_section (str_index, file);
2442       load_debug_section (str_index_dwo, file);
2443       load_debug_section (debug_addr, file);
2444     }
2445
2446   load_debug_section (abbrev_sec, file);
2447   if (debug_displays [abbrev_sec].section.start == NULL)
2448     {
2449       warn (_("Unable to locate %s section!\n"),
2450             debug_displays [abbrev_sec].section.name);
2451       return 0;
2452     }
2453
2454   for (section_begin = start, unit = 0; start < end; unit++)
2455     {
2456       DWARF2_Internal_CompUnit compunit;
2457       unsigned char *hdrptr;
2458       unsigned char *tags;
2459       int level, last_level, saved_level;
2460       dwarf_vma cu_offset;
2461       unsigned int offset_size;
2462       int initial_length_size;
2463       dwarf_vma signature_high = 0;
2464       dwarf_vma signature_low = 0;
2465       dwarf_vma type_offset = 0;
2466       struct cu_tu_set *this_set;
2467       dwarf_vma abbrev_base;
2468       size_t abbrev_size;
2469
2470       hdrptr = start;
2471
2472       SAFE_BYTE_GET_AND_INC (compunit.cu_length, hdrptr, 4, end);
2473
2474       if (compunit.cu_length == 0xffffffff)
2475         {
2476           SAFE_BYTE_GET_AND_INC (compunit.cu_length, hdrptr, 8, end);
2477           offset_size = 8;
2478           initial_length_size = 12;
2479         }
2480       else
2481         {
2482           offset_size = 4;
2483           initial_length_size = 4;
2484         }
2485
2486       SAFE_BYTE_GET_AND_INC (compunit.cu_version, hdrptr, 2, end);
2487
2488       cu_offset = start - section_begin;
2489
2490       this_set = find_cu_tu_set_v2 (cu_offset, do_types);
2491
2492       if (compunit.cu_version < 5)
2493         {
2494           compunit.cu_unit_type = DW_UT_compile;
2495           /* Initialize it due to a false compiler warning.  */
2496           compunit.cu_pointer_size = -1;
2497         }
2498       else
2499         {
2500           SAFE_BYTE_GET_AND_INC (compunit.cu_unit_type, hdrptr, 1, end);
2501           do_types = (compunit.cu_unit_type == DW_UT_type);
2502
2503           SAFE_BYTE_GET_AND_INC (compunit.cu_pointer_size, hdrptr, 1, end);
2504         }
2505
2506       SAFE_BYTE_GET_AND_INC (compunit.cu_abbrev_offset, hdrptr, offset_size, end);
2507
2508       if (this_set == NULL)
2509         {
2510           abbrev_base = 0;
2511           abbrev_size = debug_displays [abbrev_sec].section.size;
2512         }
2513       else
2514         {
2515           abbrev_base = this_set->section_offsets [DW_SECT_ABBREV];
2516           abbrev_size = this_set->section_sizes [DW_SECT_ABBREV];
2517         }
2518
2519       if (compunit.cu_version < 5)
2520         SAFE_BYTE_GET_AND_INC (compunit.cu_pointer_size, hdrptr, 1, end);
2521
2522       /* PR 17512: file: 001-108546-0.001:0.1.  */
2523       if (compunit.cu_pointer_size < 2 || compunit.cu_pointer_size > 8)
2524         {
2525           warn (_("Invalid pointer size (%d) in compunit header, using %d instead\n"),
2526                 compunit.cu_pointer_size, offset_size);
2527           compunit.cu_pointer_size = offset_size;
2528         }
2529
2530       if (do_types)
2531         {
2532           SAFE_BYTE_GET64 (hdrptr, &signature_high, &signature_low, end);
2533           hdrptr += 8;
2534           SAFE_BYTE_GET_AND_INC (type_offset, hdrptr, offset_size, end);
2535         }
2536
2537       if ((do_loc || do_debug_loc || do_debug_ranges)
2538           && num_debug_info_entries == 0
2539           && ! do_types)
2540         {
2541           debug_information [unit].cu_offset = cu_offset;
2542           debug_information [unit].pointer_size
2543             = compunit.cu_pointer_size;
2544           debug_information [unit].offset_size = offset_size;
2545           debug_information [unit].dwarf_version = compunit.cu_version;
2546           debug_information [unit].base_address = 0;
2547           debug_information [unit].addr_base = DEBUG_INFO_UNAVAILABLE;
2548           debug_information [unit].ranges_base = DEBUG_INFO_UNAVAILABLE;
2549           debug_information [unit].loc_offsets = NULL;
2550           debug_information [unit].have_frame_base = NULL;
2551           debug_information [unit].max_loc_offsets = 0;
2552           debug_information [unit].num_loc_offsets = 0;
2553           debug_information [unit].range_lists = NULL;
2554           debug_information [unit].max_range_lists= 0;
2555           debug_information [unit].num_range_lists = 0;
2556         }
2557
2558       if (!do_loc && dwarf_start_die == 0)
2559         {
2560           printf (_("  Compilation Unit @ offset 0x%s:\n"),
2561                   dwarf_vmatoa ("x", cu_offset));
2562           printf (_("   Length:        0x%s (%s)\n"),
2563                   dwarf_vmatoa ("x", compunit.cu_length),
2564                   offset_size == 8 ? "64-bit" : "32-bit");
2565           printf (_("   Version:       %d\n"), compunit.cu_version);
2566           printf (_("   Abbrev Offset: 0x%s\n"),
2567                   dwarf_vmatoa ("x", compunit.cu_abbrev_offset));
2568           printf (_("   Pointer Size:  %d\n"), compunit.cu_pointer_size);
2569           if (do_types)
2570             {
2571               char buf[64];
2572
2573               printf (_("   Signature:     0x%s\n"),
2574                       dwarf_vmatoa64 (signature_high, signature_low,
2575                                       buf, sizeof (buf)));
2576               printf (_("   Type Offset:   0x%s\n"),
2577                       dwarf_vmatoa ("x", type_offset));
2578             }
2579           if (this_set != NULL)
2580             {
2581               dwarf_vma *offsets = this_set->section_offsets;
2582               size_t *sizes = this_set->section_sizes;
2583
2584               printf (_("   Section contributions:\n"));
2585               printf (_("    .debug_abbrev.dwo:       0x%s  0x%s\n"),
2586                       dwarf_vmatoa ("x", offsets [DW_SECT_ABBREV]),
2587                       dwarf_vmatoa ("x", sizes [DW_SECT_ABBREV]));
2588               printf (_("    .debug_line.dwo:         0x%s  0x%s\n"),
2589                       dwarf_vmatoa ("x", offsets [DW_SECT_LINE]),
2590                       dwarf_vmatoa ("x", sizes [DW_SECT_LINE]));
2591               printf (_("    .debug_loc.dwo:          0x%s  0x%s\n"),
2592                       dwarf_vmatoa ("x", offsets [DW_SECT_LOC]),
2593                       dwarf_vmatoa ("x", sizes [DW_SECT_LOC]));
2594               printf (_("    .debug_str_offsets.dwo:  0x%s  0x%s\n"),
2595                       dwarf_vmatoa ("x", offsets [DW_SECT_STR_OFFSETS]),
2596                       dwarf_vmatoa ("x", sizes [DW_SECT_STR_OFFSETS]));
2597             }
2598         }
2599
2600       if (cu_offset + compunit.cu_length + initial_length_size
2601           > section->size)
2602         {
2603           warn (_("Debug info is corrupted, length of CU at %s"
2604                   " extends beyond end of section (length = %s)\n"),
2605                 dwarf_vmatoa ("x", cu_offset),
2606                 dwarf_vmatoa ("x", compunit.cu_length));
2607           num_units = unit;
2608           break;
2609         }
2610       tags = hdrptr;
2611       start += compunit.cu_length + initial_length_size;
2612
2613       if (start > end)
2614         {
2615           warn (_("Debug info is corrupt.  CU at %s extends beyond end of section"),
2616                 dwarf_vmatoa ("x", cu_offset));
2617           start = end;
2618         }
2619
2620       if (compunit.cu_version < 2 || compunit.cu_version > 5)
2621         {
2622           warn (_("CU at offset %s contains corrupt or "
2623                   "unsupported version number: %d.\n"),
2624                 dwarf_vmatoa ("x", cu_offset), compunit.cu_version);
2625           continue;
2626         }
2627
2628       if (compunit.cu_unit_type != DW_UT_compile
2629           && compunit.cu_unit_type != DW_UT_type)
2630         {
2631           warn (_("CU at offset %s contains corrupt or "
2632                   "unsupported unit type: %d.\n"),
2633                 dwarf_vmatoa ("x", cu_offset), compunit.cu_unit_type);
2634           continue;
2635         }
2636
2637       free_abbrevs ();
2638
2639       /* Process the abbrevs used by this compilation unit.  */
2640       if (compunit.cu_abbrev_offset >= abbrev_size)
2641         warn (_("Debug info is corrupted, abbrev offset (%lx) is larger than abbrev section size (%lx)\n"),
2642               (unsigned long) compunit.cu_abbrev_offset,
2643               (unsigned long) abbrev_size);
2644       /* PR 17531: file:4bcd9ce9.  */
2645       else if ((abbrev_base + abbrev_size)
2646                > debug_displays [abbrev_sec].section.size)
2647         warn (_("Debug info is corrupted, abbrev size (%lx) is larger than abbrev section size (%lx)\n"),
2648               (unsigned long) abbrev_base + abbrev_size,
2649               (unsigned long) debug_displays [abbrev_sec].section.size);
2650       else
2651         process_abbrev_section
2652           (((unsigned char *) debug_displays [abbrev_sec].section.start
2653             + abbrev_base + compunit.cu_abbrev_offset),
2654            ((unsigned char *) debug_displays [abbrev_sec].section.start
2655             + abbrev_base + abbrev_size));
2656
2657       level = 0;
2658       last_level = level;
2659       saved_level = -1;
2660       while (tags < start)
2661         {
2662           unsigned int bytes_read;
2663           unsigned long abbrev_number;
2664           unsigned long die_offset;
2665           abbrev_entry *entry;
2666           abbrev_attr *attr;
2667           int do_printing = 1;
2668
2669           die_offset = tags - section_begin;
2670
2671           abbrev_number = read_uleb128 (tags, & bytes_read, start);
2672           tags += bytes_read;
2673
2674           /* A null DIE marks the end of a list of siblings or it may also be
2675              a section padding.  */
2676           if (abbrev_number == 0)
2677             {
2678               /* Check if it can be a section padding for the last CU.  */
2679               if (level == 0 && start == end)
2680                 {
2681                   unsigned char *chk;
2682
2683                   for (chk = tags; chk < start; chk++)
2684                     if (*chk != 0)
2685                       break;
2686                   if (chk == start)
2687                     break;
2688                 }
2689
2690               if (!do_loc && die_offset >= dwarf_start_die
2691                   && (dwarf_cutoff_level == -1
2692                       || level < dwarf_cutoff_level))
2693                 printf (_(" <%d><%lx>: Abbrev Number: 0\n"),
2694                         level, die_offset);
2695
2696               --level;
2697               if (level < 0)
2698                 {
2699                   static unsigned num_bogus_warns = 0;
2700
2701                   if (num_bogus_warns < 3)
2702                     {
2703                       warn (_("Bogus end-of-siblings marker detected at offset %lx in %s section\n"),
2704                             die_offset, section->name);
2705                       num_bogus_warns ++;
2706                       if (num_bogus_warns == 3)
2707                         warn (_("Further warnings about bogus end-of-sibling markers suppressed\n"));
2708                     }
2709                 }
2710               if (dwarf_start_die != 0 && level < saved_level)
2711                 return 1;
2712               continue;
2713             }
2714
2715           if (!do_loc)
2716             {
2717               if (dwarf_start_die != 0 && die_offset < dwarf_start_die)
2718                 do_printing = 0;
2719               else
2720                 {
2721                   if (dwarf_start_die != 0 && die_offset == dwarf_start_die)
2722                     saved_level = level;
2723                   do_printing = (dwarf_cutoff_level == -1
2724                                  || level < dwarf_cutoff_level);
2725                   if (do_printing)
2726                     printf (_(" <%d><%lx>: Abbrev Number: %lu"),
2727                             level, die_offset, abbrev_number);
2728                   else if (dwarf_cutoff_level == -1
2729                            || last_level < dwarf_cutoff_level)
2730                     printf (_(" <%d><%lx>: ...\n"), level, die_offset);
2731                   last_level = level;
2732                 }
2733             }
2734
2735           /* Scan through the abbreviation list until we reach the
2736              correct entry.  */
2737           for (entry = first_abbrev;
2738                entry && entry->entry != abbrev_number;
2739                entry = entry->next)
2740             continue;
2741
2742           if (entry == NULL)
2743             {
2744               if (!do_loc && do_printing)
2745                 {
2746                   printf ("\n");
2747                   fflush (stdout);
2748                 }
2749               warn (_("DIE at offset 0x%lx refers to abbreviation number %lu which does not exist\n"),
2750                     die_offset, abbrev_number);
2751               return 0;
2752             }
2753
2754           if (!do_loc && do_printing)
2755             printf (" (%s)\n", get_TAG_name (entry->tag));
2756
2757           switch (entry->tag)
2758             {
2759             default:
2760               need_base_address = 0;
2761               break;
2762             case DW_TAG_compile_unit:
2763               need_base_address = 1;
2764               break;
2765             case DW_TAG_entry_point:
2766             case DW_TAG_subprogram:
2767               need_base_address = 0;
2768               /* Assuming that there is no DW_AT_frame_base.  */
2769               have_frame_base = 0;
2770               break;
2771             }
2772
2773           for (attr = entry->first_attr;
2774                attr && attr->attribute;
2775                attr = attr->next)
2776             {
2777               debug_info *arg;
2778
2779               if (! do_loc && do_printing)
2780                 /* Show the offset from where the tag was extracted.  */
2781                 printf ("    <%lx>", (unsigned long)(tags - section_begin));
2782
2783               if (debug_information && unit < alloc_num_debug_info_entries)
2784                 arg = debug_information + unit;
2785               else
2786                 arg = NULL;
2787
2788               tags = read_and_display_attr (attr->attribute,
2789                                             attr->form,
2790                                             attr->implicit_const,
2791                                             tags,
2792                                             end,
2793                                             cu_offset,
2794                                             compunit.cu_pointer_size,
2795                                             offset_size,
2796                                             compunit.cu_version,
2797                                             arg,
2798                                             do_loc || ! do_printing,
2799                                             section,
2800                                             this_set);
2801             }
2802
2803           if (entry->children)
2804             ++level;
2805         }
2806     }
2807
2808   /* Set num_debug_info_entries here so that it can be used to check if
2809      we need to process .debug_loc and .debug_ranges sections.  */
2810   if ((do_loc || do_debug_loc || do_debug_ranges)
2811       && num_debug_info_entries == 0
2812       && ! do_types)
2813     {
2814       if (num_units > alloc_num_debug_info_entries)
2815         num_debug_info_entries = alloc_num_debug_info_entries;
2816       else
2817         num_debug_info_entries = num_units;
2818     }
2819
2820   if (!do_loc)
2821     printf ("\n");
2822
2823   return 1;
2824 }
2825
2826 /* Locate and scan the .debug_info section in the file and record the pointer
2827    sizes and offsets for the compilation units in it.  Usually an executable
2828    will have just one pointer size, but this is not guaranteed, and so we try
2829    not to make any assumptions.  Returns zero upon failure, or the number of
2830    compilation units upon success.  */
2831
2832 static unsigned int
2833 load_debug_info (void * file)
2834 {
2835   /* Reset the last pointer size so that we can issue correct error
2836      messages if we are displaying the contents of more than one section.  */
2837   last_pointer_size = 0;
2838   warned_about_missing_comp_units = FALSE;
2839
2840   /* If we have already tried and failed to load the .debug_info
2841      section then do not bother to repeat the task.  */
2842   if (num_debug_info_entries == DEBUG_INFO_UNAVAILABLE)
2843     return 0;
2844
2845   /* If we already have the information there is nothing else to do.  */
2846   if (num_debug_info_entries > 0)
2847     return num_debug_info_entries;
2848
2849   /* If this is a DWARF package file, load the CU and TU indexes.  */
2850   (void) load_cu_tu_indexes (file);
2851
2852   if (load_debug_section (info, file)
2853       && process_debug_info (&debug_displays [info].section, file, abbrev, 1, 0))
2854     return num_debug_info_entries;
2855
2856   if (load_debug_section (info_dwo, file)
2857       && process_debug_info (&debug_displays [info_dwo].section, file,
2858                              abbrev_dwo, 1, 0))
2859     return num_debug_info_entries;
2860
2861   num_debug_info_entries = DEBUG_INFO_UNAVAILABLE;
2862   return 0;
2863 }
2864
2865 /* Read a DWARF .debug_line section header starting at DATA.
2866    Upon success returns an updated DATA pointer and the LINFO
2867    structure and the END_OF_SEQUENCE pointer will be filled in.
2868    Otherwise returns NULL.  */
2869
2870 static unsigned char *
2871 read_debug_line_header (struct dwarf_section * section,
2872                         unsigned char * data,
2873                         unsigned char * end,
2874                         DWARF2_Internal_LineInfo * linfo,
2875                         unsigned char ** end_of_sequence)
2876 {
2877   unsigned char *hdrptr;
2878   unsigned int initial_length_size;
2879   unsigned char address_size, segment_selector_size;
2880
2881   /* Extract information from the Line Number Program Header.
2882      (section 6.2.4 in the Dwarf3 doc).  */
2883   hdrptr = data;
2884
2885   /* Get and check the length of the block.  */
2886   SAFE_BYTE_GET_AND_INC (linfo->li_length, hdrptr, 4, end);
2887
2888   if (linfo->li_length == 0xffffffff)
2889     {
2890       /* This section is 64-bit DWARF 3.  */
2891       SAFE_BYTE_GET_AND_INC (linfo->li_length, hdrptr, 8, end);
2892       linfo->li_offset_size = 8;
2893       initial_length_size = 12;
2894     }
2895   else
2896     {
2897       linfo->li_offset_size = 4;
2898       initial_length_size = 4;
2899     }
2900
2901   if (linfo->li_length + initial_length_size > section->size)
2902     {
2903       /* If the length field has a relocation against it, then we should
2904          not complain if it is inaccurate (and probably negative).  This
2905          happens in object files when the .debug_line section is actually
2906          comprised of several different .debug_line.* sections, (some of
2907          which may be removed by linker garbage collection), and a relocation
2908          is used to compute the correct length once that is done.  */
2909       if (reloc_at (section, (hdrptr - section->start) - linfo->li_offset_size))
2910         {
2911           linfo->li_length = (end - data) - initial_length_size;
2912         }
2913       else
2914         {
2915           warn (_("The length field (0x%lx) in the debug_line header is wrong - the section is too small\n"),
2916                 (long) linfo->li_length);
2917           return NULL;
2918         }
2919     }
2920
2921   /* Get and check the version number.  */
2922   SAFE_BYTE_GET_AND_INC (linfo->li_version, hdrptr, 2, end);
2923
2924   if (linfo->li_version != 2
2925       && linfo->li_version != 3
2926       && linfo->li_version != 4
2927       && linfo->li_version != 5)
2928     {
2929       warn (_("Only DWARF version 2, 3, 4 and 5 line info "
2930               "is currently supported.\n"));
2931       return NULL;
2932     }
2933
2934   if (linfo->li_version >= 5)
2935     {
2936       SAFE_BYTE_GET_AND_INC (address_size, hdrptr, 1, end);
2937
2938       SAFE_BYTE_GET_AND_INC (segment_selector_size, hdrptr, 1, end);
2939       if (segment_selector_size != 0)
2940         {
2941           warn (_("The %s section contains "
2942                   "unsupported segment selector size: %d.\n"),
2943                 section->name, segment_selector_size);
2944           return 0;
2945         }
2946     }
2947
2948   SAFE_BYTE_GET_AND_INC (linfo->li_prologue_length, hdrptr,
2949                          linfo->li_offset_size, end);
2950   SAFE_BYTE_GET_AND_INC (linfo->li_min_insn_length, hdrptr, 1, end);
2951
2952   if (linfo->li_version >= 4)
2953     {
2954       SAFE_BYTE_GET_AND_INC (linfo->li_max_ops_per_insn, hdrptr, 1, end);
2955
2956       if (linfo->li_max_ops_per_insn == 0)
2957         {
2958           warn (_("Invalid maximum operations per insn.\n"));
2959           return NULL;
2960         }
2961     }
2962   else
2963     linfo->li_max_ops_per_insn = 1;
2964
2965   SAFE_BYTE_GET_AND_INC (linfo->li_default_is_stmt, hdrptr, 1, end);
2966   SAFE_SIGNED_BYTE_GET_AND_INC (linfo->li_line_base, hdrptr, 1, end);
2967   SAFE_BYTE_GET_AND_INC (linfo->li_line_range, hdrptr, 1, end);
2968   SAFE_BYTE_GET_AND_INC (linfo->li_opcode_base, hdrptr, 1, end);
2969
2970   * end_of_sequence = data + linfo->li_length + initial_length_size;
2971   /* PR 17512: file:002-117414-0.004.  */
2972   if (* end_of_sequence > end)
2973     {
2974       warn (_("Line length %s extends beyond end of section\n"),
2975             dwarf_vmatoa ("u", linfo->li_length));
2976       * end_of_sequence = end;
2977       return NULL;
2978     }
2979
2980   return hdrptr;
2981 }
2982
2983 static unsigned char *
2984 display_formatted_table (unsigned char *data,
2985                          unsigned char *start, unsigned char *end,
2986                          const DWARF2_Internal_LineInfo *linfo,
2987                          struct dwarf_section *section, const char *what)
2988 {
2989   unsigned char *format_start, format_count, *format, formati;
2990   dwarf_vma data_count, datai;
2991   unsigned int bytes_read, namepass, last_entry = 0;
2992
2993   SAFE_BYTE_GET_AND_INC (format_count, data, 1, end);
2994   format_start = data;
2995   for (formati = 0; formati < format_count; formati++)
2996     {
2997       read_uleb128 (data, & bytes_read, end);
2998       data += bytes_read;
2999       read_uleb128 (data, & bytes_read, end);
3000       data += bytes_read;
3001       if (data == end)
3002         {
3003           warn (_("Corrupt %s entry format table entry\n"), what);
3004           return data;
3005         }
3006     }
3007
3008   data_count = read_uleb128 (data, & bytes_read, end);
3009   data += bytes_read;
3010   if (data == end)
3011     {
3012       warn (_("Corrupt %s list\n"), what);
3013       return data;
3014     }
3015
3016   if (data_count == 0)
3017     {
3018       printf (_("\n The %s Table is empty.\n"), what);
3019       return data;
3020     }
3021
3022   printf (_("\n The %s Table (offset 0x%lx):\n"), what,
3023           (long)(data - start));
3024
3025   printf (_("  Entry"));
3026   /* Delay displaying name as the last entry for better screen layout.  */ 
3027   for (namepass = 0; namepass < 2; namepass++)
3028     {
3029       format = format_start;
3030       for (formati = 0; formati < format_count; formati++)
3031         {
3032           dwarf_vma content_type;
3033
3034           content_type = read_uleb128 (format, & bytes_read, end);
3035           format += bytes_read;
3036           if ((content_type == DW_LNCT_path) == (namepass == 1))
3037             switch (content_type)
3038               {
3039               case DW_LNCT_path:
3040                 printf (_("\tName"));
3041                 break;
3042               case DW_LNCT_directory_index:
3043                 printf (_("\tDir"));
3044                 break;
3045               case DW_LNCT_timestamp:
3046                 printf (_("\tTime"));
3047                 break;
3048               case DW_LNCT_size:
3049                 printf (_("\tSize"));
3050                 break;
3051               case DW_LNCT_MD5:
3052                 printf (_("\tMD5"));
3053                 break;
3054               default:
3055                 printf (_("\t(Unknown format content type %s)"),
3056                         dwarf_vmatoa ("u", content_type));
3057               }
3058           read_uleb128 (format, & bytes_read, end);
3059           format += bytes_read;
3060         }
3061     }
3062   putchar ('\n');
3063
3064   for (datai = 0; datai < data_count; datai++)
3065     {
3066       unsigned char *datapass = data;
3067
3068       printf ("  %d", last_entry++);
3069       /* Delay displaying name as the last entry for better screen layout.  */ 
3070       for (namepass = 0; namepass < 2; namepass++)
3071         {
3072           format = format_start;
3073           data = datapass;
3074           for (formati = 0; formati < format_count; formati++)
3075             {
3076               dwarf_vma content_type, form;
3077
3078               content_type = read_uleb128 (format, & bytes_read, end);
3079               format += bytes_read;
3080               form = read_uleb128 (format, & bytes_read, end);
3081               format += bytes_read;
3082               data = read_and_display_attr_value (0, form, 0, data, end, 0, 0,
3083                                                   linfo->li_offset_size,
3084                                                   linfo->li_version, NULL,
3085                             ((content_type == DW_LNCT_path) != (namepass == 1)),
3086                                                   section, NULL, '\t');
3087             }
3088         }
3089       if (data == end)
3090         {
3091           warn (_("Corrupt %s entries list\n"), what);
3092           return data;
3093         }
3094       putchar ('\n');
3095     }
3096   return data;
3097 }
3098
3099 static int
3100 display_debug_lines_raw (struct dwarf_section *section,
3101                          unsigned char *data,
3102                          unsigned char *end, void *file)
3103 {
3104   unsigned char *start = section->start;
3105
3106   printf (_("Raw dump of debug contents of section %s:\n\n"),
3107           section->name);
3108
3109   while (data < end)
3110     {
3111       static DWARF2_Internal_LineInfo saved_linfo;
3112       DWARF2_Internal_LineInfo linfo;
3113       unsigned char *standard_opcodes;
3114       unsigned char *end_of_sequence;
3115       int i;
3116
3117       if (const_strneq (section->name, ".debug_line.")
3118           /* Note: the following does not apply to .debug_line.dwo sections.
3119              These are full debug_line sections.  */
3120           && strcmp (section->name, ".debug_line.dwo") != 0)
3121         {
3122           /* Sections named .debug_line.<foo> are fragments of a .debug_line
3123              section containing just the Line Number Statements.  They are
3124              created by the assembler and intended to be used alongside gcc's
3125              -ffunction-sections command line option.  When the linker's
3126              garbage collection decides to discard a .text.<foo> section it
3127              can then also discard the line number information in .debug_line.<foo>.
3128
3129              Since the section is a fragment it does not have the details
3130              needed to fill out a LineInfo structure, so instead we use the
3131              details from the last full debug_line section that we processed.  */
3132           end_of_sequence = end;
3133           standard_opcodes = NULL;
3134           linfo = saved_linfo;
3135           /* PR 17531: file: 0522b371.  */
3136           if (linfo.li_line_range == 0)
3137             {
3138               warn (_("Partial .debug_line. section encountered without a prior full .debug_line section\n"));
3139               return 0;
3140             }
3141           reset_state_machine (linfo.li_default_is_stmt);
3142         }
3143       else
3144         {
3145           unsigned char * hdrptr;
3146
3147           if ((hdrptr = read_debug_line_header (section, data, end, & linfo,
3148                                                 & end_of_sequence)) == NULL)
3149             return 0;
3150
3151           printf (_("  Offset:                      0x%lx\n"), (long)(data - start));
3152           printf (_("  Length:                      %ld\n"), (long) linfo.li_length);
3153           printf (_("  DWARF Version:               %d\n"), linfo.li_version);
3154           printf (_("  Prologue Length:             %d\n"), (int) linfo.li_prologue_length);
3155           printf (_("  Minimum Instruction Length:  %d\n"), linfo.li_min_insn_length);
3156           if (linfo.li_version >= 4)
3157             printf (_("  Maximum Ops per Instruction: %d\n"), linfo.li_max_ops_per_insn);
3158           printf (_("  Initial value of 'is_stmt':  %d\n"), linfo.li_default_is_stmt);
3159           printf (_("  Line Base:                   %d\n"), linfo.li_line_base);
3160           printf (_("  Line Range:                  %d\n"), linfo.li_line_range);
3161           printf (_("  Opcode Base:                 %d\n"), linfo.li_opcode_base);
3162
3163           /* PR 17512: file: 1665-6428-0.004.  */
3164           if (linfo.li_line_range == 0)
3165             {
3166               warn (_("Line range of 0 is invalid, using 1 instead\n"));
3167               linfo.li_line_range = 1;
3168             }
3169
3170           reset_state_machine (linfo.li_default_is_stmt);
3171
3172           /* Display the contents of the Opcodes table.  */
3173           standard_opcodes = hdrptr;
3174
3175           /* PR 17512: file: 002-417945-0.004.  */
3176           if (standard_opcodes + linfo.li_opcode_base >= end)
3177             {
3178               warn (_("Line Base extends beyond end of section\n"));
3179               return 0;
3180             }
3181
3182           printf (_("\n Opcodes:\n"));
3183
3184           for (i = 1; i < linfo.li_opcode_base; i++)
3185             printf (_("  Opcode %d has %d args\n"), i, standard_opcodes[i - 1]);
3186
3187           /* Display the contents of the Directory table.  */
3188           data = standard_opcodes + linfo.li_opcode_base - 1;
3189
3190           if (linfo.li_version >= 5)
3191             {
3192               load_debug_section (line_str, file);
3193
3194               data = display_formatted_table (data, start, end, &linfo, section,
3195                                               _("Directory"));
3196               data = display_formatted_table (data, start, end, &linfo, section,
3197                                               _("File name"));
3198             }
3199           else
3200             {
3201               if (*data == 0)
3202                 printf (_("\n The Directory Table is empty.\n"));
3203               else
3204                 {
3205                   unsigned int last_dir_entry = 0;
3206
3207                   printf (_("\n The Directory Table (offset 0x%lx):\n"),
3208                           (long)(data - start));
3209
3210                   while (data < end && *data != 0)
3211                     {
3212                       printf ("  %d\t%.*s\n", ++last_dir_entry, (int) (end - data), data);
3213
3214                       data += strnlen ((char *) data, end - data) + 1;
3215                     }
3216
3217                   /* PR 17512: file: 002-132094-0.004.  */
3218                   if (data >= end - 1)
3219                     break;
3220                 }
3221
3222               /* Skip the NUL at the end of the table.  */
3223               data++;
3224
3225               /* Display the contents of the File Name table.  */
3226               if (*data == 0)
3227                 printf (_("\n The File Name Table is empty.\n"));
3228               else
3229                 {
3230                   printf (_("\n The File Name Table (offset 0x%lx):\n"),
3231                           (long)(data - start));
3232                   printf (_("  Entry\tDir\tTime\tSize\tName\n"));
3233
3234                   while (data < end && *data != 0)
3235                     {
3236                       unsigned char *name;
3237                       unsigned int bytes_read;
3238
3239                       printf ("  %d\t", ++state_machine_regs.last_file_entry);
3240                       name = data;
3241                       data += strnlen ((char *) data, end - data) + 1;
3242
3243                       printf ("%s\t",
3244                               dwarf_vmatoa ("u", read_uleb128 (data, & bytes_read, end)));
3245                       data += bytes_read;
3246                       printf ("%s\t",
3247                               dwarf_vmatoa ("u", read_uleb128 (data, & bytes_read, end)));
3248                       data += bytes_read;
3249                       printf ("%s\t",
3250                               dwarf_vmatoa ("u", read_uleb128 (data, & bytes_read, end)));
3251                       data += bytes_read;
3252                       printf ("%.*s\n", (int)(end - name), name);
3253
3254                       if (data == end)
3255                         {
3256                           warn (_("Corrupt file name table entry\n"));
3257                           break;
3258                         }
3259                     }
3260                 }
3261
3262               /* Skip the NUL at the end of the table.  */
3263               data++;
3264             }
3265
3266           putchar ('\n');
3267           saved_linfo = linfo;
3268         }
3269
3270       /* Now display the statements.  */
3271       if (data >= end_of_sequence)
3272         printf (_(" No Line Number Statements.\n"));
3273       else
3274         {
3275           printf (_(" Line Number Statements:\n"));
3276
3277           while (data < end_of_sequence)
3278             {
3279               unsigned char op_code;
3280               dwarf_signed_vma adv;
3281               dwarf_vma uladv;
3282               unsigned int bytes_read;
3283
3284               printf ("  [0x%08lx]", (long)(data - start));
3285
3286               op_code = *data++;
3287
3288               if (op_code >= linfo.li_opcode_base)
3289                 {
3290                   op_code -= linfo.li_opcode_base;
3291                   uladv = (op_code / linfo.li_line_range);
3292                   if (linfo.li_max_ops_per_insn == 1)
3293                     {
3294                       uladv *= linfo.li_min_insn_length;
3295                       state_machine_regs.address += uladv;
3296                       printf (_("  Special opcode %d: "
3297                                 "advance Address by %s to 0x%s"),
3298                               op_code, dwarf_vmatoa ("u", uladv),
3299                               dwarf_vmatoa ("x", state_machine_regs.address));
3300                     }
3301                   else
3302                     {
3303                       state_machine_regs.address
3304                         += ((state_machine_regs.op_index + uladv)
3305                             / linfo.li_max_ops_per_insn)
3306                         * linfo.li_min_insn_length;
3307                       state_machine_regs.op_index
3308                         = (state_machine_regs.op_index + uladv)
3309                         % linfo.li_max_ops_per_insn;
3310                       printf (_("  Special opcode %d: "
3311                                 "advance Address by %s to 0x%s[%d]"),
3312                               op_code, dwarf_vmatoa ("u", uladv),
3313                               dwarf_vmatoa ("x", state_machine_regs.address),
3314                               state_machine_regs.op_index);
3315                     }
3316                   adv = (op_code % linfo.li_line_range) + linfo.li_line_base;
3317                   state_machine_regs.line += adv;
3318                   printf (_(" and Line by %s to %d\n"),
3319                           dwarf_vmatoa ("d", adv), state_machine_regs.line);
3320                 }
3321               else switch (op_code)
3322                      {
3323                      case DW_LNS_extended_op:
3324                        data += process_extended_line_op (data, linfo.li_default_is_stmt, end);
3325                        break;
3326
3327                      case DW_LNS_copy:
3328                        printf (_("  Copy\n"));
3329                        break;
3330
3331                      case DW_LNS_advance_pc:
3332                        uladv = read_uleb128 (data, & bytes_read, end);
3333                        data += bytes_read;
3334                        if (linfo.li_max_ops_per_insn == 1)
3335                          {
3336                            uladv *= linfo.li_min_insn_length;
3337                            state_machine_regs.address += uladv;
3338                            printf (_("  Advance PC by %s to 0x%s\n"),
3339                                    dwarf_vmatoa ("u", uladv),
3340                                    dwarf_vmatoa ("x", state_machine_regs.address));
3341                          }
3342                        else
3343                          {
3344                            state_machine_regs.address
3345                              += ((state_machine_regs.op_index + uladv)
3346                                  / linfo.li_max_ops_per_insn)
3347                              * linfo.li_min_insn_length;
3348                            state_machine_regs.op_index
3349                              = (state_machine_regs.op_index + uladv)
3350                              % linfo.li_max_ops_per_insn;
3351                            printf (_("  Advance PC by %s to 0x%s[%d]\n"),
3352                                    dwarf_vmatoa ("u", uladv),
3353                                    dwarf_vmatoa ("x", state_machine_regs.address),
3354                                    state_machine_regs.op_index);
3355                          }
3356                        break;
3357
3358                      case DW_LNS_advance_line:
3359                        adv = read_sleb128 (data, & bytes_read, end);
3360                        data += bytes_read;
3361                        state_machine_regs.line += adv;
3362                        printf (_("  Advance Line by %s to %d\n"),
3363                                dwarf_vmatoa ("d", adv),
3364                                state_machine_regs.line);
3365                        break;
3366
3367                      case DW_LNS_set_file:
3368                        adv = read_uleb128 (data, & bytes_read, end);
3369                        data += bytes_read;
3370                        printf (_("  Set File Name to entry %s in the File Name Table\n"),
3371                                dwarf_vmatoa ("d", adv));
3372                        state_machine_regs.file = adv;
3373                        break;
3374
3375                      case DW_LNS_set_column:
3376                        uladv = read_uleb128 (data, & bytes_read, end);
3377                        data += bytes_read;
3378                        printf (_("  Set column to %s\n"),
3379                                dwarf_vmatoa ("u", uladv));
3380                        state_machine_regs.column = uladv;
3381                        break;
3382
3383                      case DW_LNS_negate_stmt:
3384                        adv = state_machine_regs.is_stmt;
3385                        adv = ! adv;
3386                        printf (_("  Set is_stmt to %s\n"), dwarf_vmatoa ("d", adv));
3387                        state_machine_regs.is_stmt = adv;
3388                        break;
3389
3390                      case DW_LNS_set_basic_block:
3391                        printf (_("  Set basic block\n"));
3392                        state_machine_regs.basic_block = 1;
3393                        break;
3394
3395                      case DW_LNS_const_add_pc:
3396                        uladv = ((255 - linfo.li_opcode_base) / linfo.li_line_range);
3397                        if (linfo.li_max_ops_per_insn)
3398                          {
3399                            uladv *= linfo.li_min_insn_length;
3400                            state_machine_regs.address += uladv;
3401                            printf (_("  Advance PC by constant %s to 0x%s\n"),
3402                                    dwarf_vmatoa ("u", uladv),
3403                                    dwarf_vmatoa ("x", state_machine_regs.address));
3404                          }
3405                        else
3406                          {
3407                            state_machine_regs.address
3408                              += ((state_machine_regs.op_index + uladv)
3409                                  / linfo.li_max_ops_per_insn)
3410                              * linfo.li_min_insn_length;
3411                            state_machine_regs.op_index
3412                              = (state_machine_regs.op_index + uladv)
3413                              % linfo.li_max_ops_per_insn;
3414                            printf (_("  Advance PC by constant %s to 0x%s[%d]\n"),
3415                                    dwarf_vmatoa ("u", uladv),
3416                                    dwarf_vmatoa ("x", state_machine_regs.address),
3417                                    state_machine_regs.op_index);
3418                          }
3419                        break;
3420
3421                      case DW_LNS_fixed_advance_pc:
3422                        SAFE_BYTE_GET_AND_INC (uladv, data, 2, end);
3423                        state_machine_regs.address += uladv;
3424                        state_machine_regs.op_index = 0;
3425                        printf (_("  Advance PC by fixed size amount %s to 0x%s\n"),
3426                                dwarf_vmatoa ("u", uladv),
3427                                dwarf_vmatoa ("x", state_machine_regs.address));
3428                        break;
3429
3430                      case DW_LNS_set_prologue_end:
3431                        printf (_("  Set prologue_end to true\n"));
3432                        break;
3433
3434                      case DW_LNS_set_epilogue_begin:
3435                        printf (_("  Set epilogue_begin to true\n"));
3436                        break;
3437
3438                      case DW_LNS_set_isa:
3439                        uladv = read_uleb128 (data, & bytes_read, end);
3440                        data += bytes_read;
3441                        printf (_("  Set ISA to %s\n"), dwarf_vmatoa ("u", uladv));
3442                        break;
3443
3444                      default:
3445                        printf (_("  Unknown opcode %d with operands: "), op_code);
3446
3447                        if (standard_opcodes != NULL)
3448                          for (i = standard_opcodes[op_code - 1]; i > 0 ; --i)
3449                            {
3450                              printf ("0x%s%s", dwarf_vmatoa ("x", read_uleb128 (data,
3451                                                                                 &bytes_read, end)),
3452                                      i == 1 ? "" : ", ");
3453                              data += bytes_read;
3454                            }
3455                        putchar ('\n');
3456                        break;
3457                      }
3458             }
3459           putchar ('\n');
3460         }
3461     }
3462
3463   return 1;
3464 }
3465
3466 typedef struct
3467 {
3468   unsigned char *name;
3469   unsigned int directory_index;
3470   unsigned int modification_date;
3471   unsigned int length;
3472 } File_Entry;
3473
3474 /* Output a decoded representation of the .debug_line section.  */
3475
3476 static int
3477 display_debug_lines_decoded (struct dwarf_section *section,
3478                              unsigned char *data,
3479                              unsigned char *end, void *fileptr)
3480 {
3481   static DWARF2_Internal_LineInfo saved_linfo;
3482
3483   printf (_("Decoded dump of debug contents of section %s:\n\n"),
3484           section->name);
3485
3486   while (data < end)
3487     {
3488       /* This loop amounts to one iteration per compilation unit.  */
3489       DWARF2_Internal_LineInfo linfo;
3490       unsigned char *standard_opcodes;
3491       unsigned char *end_of_sequence;
3492       int i;
3493       File_Entry *file_table = NULL;
3494       unsigned int n_files = 0;
3495       unsigned char **directory_table = NULL;
3496       dwarf_vma n_directories = 0;
3497
3498       if (const_strneq (section->name, ".debug_line.")
3499           /* Note: the following does not apply to .debug_line.dwo sections.
3500              These are full debug_line sections.  */
3501           && strcmp (section->name, ".debug_line.dwo") != 0)
3502         {
3503           /* See comment in display_debug_lines_raw().  */
3504           end_of_sequence = end;
3505           standard_opcodes = NULL;
3506           linfo = saved_linfo;
3507           /* PR 17531: file: 0522b371.  */
3508           if (linfo.li_line_range == 0)
3509             {
3510               warn (_("Partial .debug_line. section encountered without a prior full .debug_line section\n"));
3511               return 0;
3512             }
3513           reset_state_machine (linfo.li_default_is_stmt);
3514         }
3515       else
3516         {
3517           unsigned char *hdrptr;
3518
3519           if ((hdrptr = read_debug_line_header (section, data, end, & linfo,
3520                                                 & end_of_sequence)) == NULL)
3521               return 0;
3522
3523           /* PR 17531: file: 0522b371.  */
3524           if (linfo.li_line_range == 0)
3525             {
3526               warn (_("Line range of 0 is invalid, using 1 instead\n"));
3527               linfo.li_line_range = 1;
3528             }
3529           reset_state_machine (linfo.li_default_is_stmt);
3530
3531           /* Save a pointer to the contents of the Opcodes table.  */
3532           standard_opcodes = hdrptr;
3533
3534           /* Traverse the Directory table just to count entries.  */
3535           data = standard_opcodes + linfo.li_opcode_base - 1;
3536           /* PR 20440 */
3537           if (data >= end)
3538             {
3539               warn (_("opcode base of %d extends beyond end of section\n"),
3540                     linfo.li_opcode_base);
3541               return 0;
3542             }
3543
3544           if (linfo.li_version >= 5)
3545             {
3546               unsigned char *format_start, format_count, *format;
3547               dwarf_vma formati, entryi;
3548               unsigned int bytes_read;
3549
3550               load_debug_section (line_str, fileptr);
3551
3552               /* Skip directories format.  */
3553               SAFE_BYTE_GET_AND_INC (format_count, data, 1, end);
3554               format_start = data;
3555               for (formati = 0; formati < format_count; formati++)
3556                 {
3557                   read_uleb128 (data, & bytes_read, end);
3558                   data += bytes_read;
3559                   read_uleb128 (data, & bytes_read, end);
3560                   data += bytes_read;
3561                 }
3562
3563               n_directories = read_uleb128 (data, & bytes_read, end);
3564               data += bytes_read;
3565               if (data == end)
3566                 {
3567                   warn (_("Corrupt directories list\n"));
3568                   break;
3569                 }
3570
3571               directory_table = (unsigned char **)
3572                 xmalloc (n_directories * sizeof (unsigned char *));
3573
3574               for (entryi = 0; entryi < n_directories; entryi++)
3575                 {
3576                   unsigned char **pathp = &directory_table[entryi];
3577
3578                   format = format_start;
3579                   for (formati = 0; formati < format_count; formati++)
3580                     {
3581                       dwarf_vma content_type, form;
3582                       dwarf_vma uvalue;
3583
3584                       content_type = read_uleb128 (format, & bytes_read, end);
3585                       format += bytes_read;
3586                       form = read_uleb128 (format, & bytes_read, end);
3587                       format += bytes_read;
3588                       if (data == end)
3589                         {
3590                           warn (_("Corrupt directories list\n"));
3591                           break;
3592                         }
3593                       switch (content_type)
3594                         {
3595                         case DW_LNCT_path:
3596                           switch (form)
3597                             {
3598                             case DW_FORM_string:
3599                               *pathp = data;
3600                               break;
3601                             case DW_FORM_line_strp:
3602                               SAFE_BYTE_GET (uvalue, data, linfo.li_offset_size,
3603                                              end);
3604                               /* Remove const by the cast.  */
3605                               *pathp = (unsigned char *)
3606                                        fetch_indirect_line_string (uvalue);
3607                               break;
3608                             }
3609                           break;
3610                         }
3611                       data = read_and_display_attr_value (0, form, 0, data, end,
3612                                                           0, 0,
3613                                                           linfo.li_offset_size,
3614                                                           linfo.li_version,
3615                                                           NULL, 1, section,
3616                                                           NULL, '\t');
3617                     }
3618                   if (data == end)
3619                     {
3620                       warn (_("Corrupt directories list\n"));
3621                       break;
3622                     }
3623                 }
3624
3625               /* Skip files format.  */
3626               SAFE_BYTE_GET_AND_INC (format_count, data, 1, end);
3627               format_start = data;
3628               for (formati = 0; formati < format_count; formati++)
3629                 {
3630                   read_uleb128 (data, & bytes_read, end);
3631                   data += bytes_read;
3632                   read_uleb128 (data, & bytes_read, end);
3633                   data += bytes_read;
3634                 }
3635
3636               n_files = read_uleb128 (data, & bytes_read, end);
3637               data += bytes_read;
3638               if (data == end)
3639                 {
3640                   warn (_("Corrupt file name list\n"));
3641                   break;
3642                 }
3643
3644               file_table = (File_Entry *) xcalloc (1, n_files
3645                                                       * sizeof (File_Entry));
3646
3647               for (entryi = 0; entryi < n_files; entryi++)
3648                 {
3649                   File_Entry *file = &file_table[entryi];
3650
3651                   format = format_start;
3652                   for (formati = 0; formati < format_count; formati++)
3653                     {
3654                       dwarf_vma content_type, form;
3655                       dwarf_vma uvalue;
3656
3657                       content_type = read_uleb128 (format, & bytes_read, end);
3658                       format += bytes_read;
3659                       form = read_uleb128 (format, & bytes_read, end);
3660                       format += bytes_read;
3661                       if (data == end)
3662                         {
3663                           warn (_("Corrupt file name list\n"));
3664                           break;
3665                         }
3666                       switch (content_type)
3667                         {
3668                         case DW_LNCT_path:
3669                           switch (form)
3670                             {
3671                             case DW_FORM_string:
3672                               file->name = data;
3673                               break;
3674                             case DW_FORM_line_strp:
3675                               SAFE_BYTE_GET (uvalue, data, linfo.li_offset_size,
3676                                              end);
3677                               /* Remove const by the cast.  */
3678                               file->name = (unsigned char *)
3679                                            fetch_indirect_line_string (uvalue);
3680                               break;
3681                             }
3682                           break;
3683                         case DW_LNCT_directory_index:
3684                           switch (form)
3685                             {
3686                             case DW_FORM_data1:
3687                               SAFE_BYTE_GET (file->directory_index, data, 1,
3688                                              end);
3689                               break;
3690                             case DW_FORM_data2:
3691                               SAFE_BYTE_GET (file->directory_index, data, 2,
3692                                              end);
3693                               break;
3694                             case DW_FORM_udata:
3695                               file->directory_index = read_uleb128 (data, NULL,
3696                                                                     end);
3697                               break;
3698                             }
3699                           break;
3700                         }
3701                       data = read_and_display_attr_value (0, form, 0, data, end,
3702                                                           0, 0,
3703                                                           linfo.li_offset_size,
3704                                                           linfo.li_version,
3705                                                           NULL, 1, section,
3706                                                           NULL, '\t');
3707                     }
3708                   if (data == end)
3709                     {
3710                       warn (_("Corrupt file name list\n"));
3711                       break;
3712                     }
3713                 }
3714             }
3715           else
3716             {
3717               if (*data != 0)
3718                 {
3719                   unsigned char *ptr_directory_table = data;
3720
3721                   while (data < end && *data != 0)
3722                     {
3723                       data += strnlen ((char *) data, end - data) + 1;
3724                       n_directories++;
3725                     }
3726
3727                   /* PR 20440 */
3728                   if (data >= end)
3729                     {
3730                       warn (_("directory table ends unexpectedly\n"));
3731                       n_directories = 0;
3732                       break;
3733                     }
3734
3735                   /* Go through the directory table again to save the directories.  */
3736                   directory_table = (unsigned char **)
3737                     xmalloc (n_directories * sizeof (unsigned char *));
3738
3739                   i = 0;
3740                   while (*ptr_directory_table != 0)
3741                     {
3742                       directory_table[i] = ptr_directory_table;
3743                       ptr_directory_table += strnlen ((char *) ptr_directory_table,
3744                                                       ptr_directory_table - end) + 1;
3745                       i++;
3746                     }
3747                 }
3748               /* Skip the NUL at the end of the table.  */
3749               data++;
3750
3751               /* Traverse the File Name table just to count the entries.  */
3752               if (data < end && *data != 0)
3753                 {
3754                   unsigned char *ptr_file_name_table = data;
3755
3756                   while (data < end && *data != 0)
3757                     {
3758                       unsigned int bytes_read;
3759
3760                       /* Skip Name, directory index, last modification time and length
3761                          of file.  */
3762                       data += strnlen ((char *) data, end - data) + 1;
3763                       read_uleb128 (data, & bytes_read, end);
3764                       data += bytes_read;
3765                       read_uleb128 (data, & bytes_read, end);
3766                       data += bytes_read;
3767                       read_uleb128 (data, & bytes_read, end);
3768                       data += bytes_read;
3769
3770                       n_files++;
3771                     }
3772
3773                   if (data >= end)
3774                     {
3775                       warn (_("file table ends unexpectedly\n"));
3776                       n_files = 0;
3777                       break;
3778                     }
3779
3780                   /* Go through the file table again to save the strings.  */
3781                   file_table = (File_Entry *) xmalloc (n_files * sizeof (File_Entry));
3782
3783                   i = 0;
3784                   while (*ptr_file_name_table != 0)
3785                     {
3786                       unsigned int bytes_read;
3787
3788                       file_table[i].name = ptr_file_name_table;
3789                       ptr_file_name_table += strnlen ((char *) ptr_file_name_table,
3790                                                       end - ptr_file_name_table) + 1;
3791
3792                       /* We are not interested in directory, time or size.  */
3793                       file_table[i].directory_index = read_uleb128 (ptr_file_name_table,
3794                                                                     & bytes_read, end);
3795                       ptr_file_name_table += bytes_read;
3796                       file_table[i].modification_date = read_uleb128 (ptr_file_name_table,
3797                                                                       & bytes_read, end);
3798                       ptr_file_name_table += bytes_read;
3799                       file_table[i].length = read_uleb128 (ptr_file_name_table, & bytes_read, end);
3800                       ptr_file_name_table += bytes_read;
3801                       i++;
3802                     }
3803                   i = 0;
3804                 }
3805
3806               /* Skip the NUL at the end of the table.  */
3807               data++;
3808             }
3809
3810           /* Print the Compilation Unit's name and a header.  */
3811           if (directory_table == NULL)
3812             {
3813               printf (_("CU: %s:\n"), file_table[0].name);
3814               printf (_("File name                            Line number    Starting address\n"));
3815             }
3816           else
3817             {
3818               unsigned int ix = file_table[0].directory_index;
3819               const char *directory;
3820
3821               if (ix == 0)
3822                 directory = ".";
3823               /* PR 20439 */
3824               else if (n_directories == 0)
3825                 directory = _("<unknown>");
3826               else if (ix > n_directories)
3827                 {
3828                   warn (_("directory index %u > number of directories %s\n"),
3829                         ix, dwarf_vmatoa ("u", n_directories));
3830                   directory = _("<corrupt>");
3831                 }
3832               else
3833                 directory = (char *) directory_table[ix - 1];
3834
3835               if (do_wide || strlen (directory) < 76)
3836                 printf (_("CU: %s/%s:\n"), directory, file_table[0].name);
3837               else
3838                 printf ("%s:\n", file_table[0].name);
3839
3840               printf (_("File name                            Line number    Starting address\n"));
3841             }
3842
3843           saved_linfo = linfo;
3844         }
3845
3846       /* This loop iterates through the Dwarf Line Number Program.  */
3847       while (data < end_of_sequence)
3848         {
3849           unsigned char op_code;
3850           int adv;
3851           unsigned long int uladv;
3852           unsigned int bytes_read;
3853           int is_special_opcode = 0;
3854
3855           op_code = *data++;
3856
3857           if (op_code >= linfo.li_opcode_base)
3858             {
3859               op_code -= linfo.li_opcode_base;
3860               uladv = (op_code / linfo.li_line_range);
3861               if (linfo.li_max_ops_per_insn == 1)
3862                 {
3863                   uladv *= linfo.li_min_insn_length;
3864                   state_machine_regs.address += uladv;
3865                 }
3866               else
3867                 {
3868                   state_machine_regs.address
3869                     += ((state_machine_regs.op_index + uladv)
3870                         / linfo.li_max_ops_per_insn)
3871                     * linfo.li_min_insn_length;
3872                   state_machine_regs.op_index
3873                     = (state_machine_regs.op_index + uladv)
3874                     % linfo.li_max_ops_per_insn;
3875                 }
3876
3877               adv = (op_code % linfo.li_line_range) + linfo.li_line_base;
3878               state_machine_regs.line += adv;
3879               is_special_opcode = 1;
3880             }
3881           else switch (op_code)
3882                  {
3883                  case DW_LNS_extended_op:
3884                    {
3885                      unsigned int ext_op_code_len;
3886                      unsigned char ext_op_code;
3887                      unsigned char *op_code_data = data;
3888
3889                      ext_op_code_len = read_uleb128 (op_code_data, &bytes_read,
3890                                                      end_of_sequence);
3891                      op_code_data += bytes_read;
3892
3893                      if (ext_op_code_len == 0)
3894                        {
3895                          warn (_("Badly formed extended line op encountered!\n"));
3896                          break;
3897                        }
3898                      ext_op_code_len += bytes_read;
3899                      ext_op_code = *op_code_data++;
3900
3901                      switch (ext_op_code)
3902                        {
3903                        case DW_LNE_end_sequence:
3904                          reset_state_machine (linfo.li_default_is_stmt);
3905                          break;
3906                        case DW_LNE_set_address:
3907                          SAFE_BYTE_GET_AND_INC (state_machine_regs.address,
3908                                                 op_code_data,
3909                                                 ext_op_code_len - bytes_read - 1,
3910                                                 end);
3911                          state_machine_regs.op_index = 0;
3912                          break;
3913                        case DW_LNE_define_file:
3914                          {
3915                            file_table = (File_Entry *) xrealloc
3916                              (file_table, (n_files + 1) * sizeof (File_Entry));
3917
3918                            ++state_machine_regs.last_file_entry;
3919                            /* Source file name.  */
3920                            file_table[n_files].name = op_code_data;
3921                            op_code_data += strlen ((char *) op_code_data) + 1;
3922                            /* Directory index.  */
3923                            file_table[n_files].directory_index =
3924                              read_uleb128 (op_code_data, & bytes_read,
3925                                            end_of_sequence);
3926                            op_code_data += bytes_read;
3927                            /* Last modification time.  */
3928                            file_table[n_files].modification_date =
3929                              read_uleb128 (op_code_data, & bytes_read,
3930                                            end_of_sequence);
3931                            op_code_data += bytes_read;
3932                            /* File length.  */
3933                            file_table[n_files].length =
3934                              read_uleb128 (op_code_data, & bytes_read,
3935                                            end_of_sequence);
3936
3937                            n_files++;
3938                            break;
3939                          }
3940                        case DW_LNE_set_discriminator:
3941                        case DW_LNE_HP_set_sequence:
3942                          /* Simply ignored.  */
3943                          break;
3944
3945                        default:
3946                          printf (_("UNKNOWN (%u): length %d\n"),
3947                                  ext_op_code, ext_op_code_len - bytes_read);
3948                          break;
3949                        }
3950                      data += ext_op_code_len;
3951                      break;
3952                    }
3953                  case DW_LNS_copy:
3954                    break;
3955
3956                  case DW_LNS_advance_pc:
3957                    uladv = read_uleb128 (data, & bytes_read, end);
3958                    data += bytes_read;
3959                    if (linfo.li_max_ops_per_insn == 1)
3960                      {
3961                        uladv *= linfo.li_min_insn_length;
3962                        state_machine_regs.address += uladv;
3963                      }
3964                    else
3965                      {
3966                        state_machine_regs.address
3967                          += ((state_machine_regs.op_index + uladv)
3968                              / linfo.li_max_ops_per_insn)
3969                          * linfo.li_min_insn_length;
3970                        state_machine_regs.op_index
3971                          = (state_machine_regs.op_index + uladv)
3972                          % linfo.li_max_ops_per_insn;
3973                      }
3974                    break;
3975
3976                  case DW_LNS_advance_line:
3977                    adv = read_sleb128 (data, & bytes_read, end);
3978                    data += bytes_read;
3979                    state_machine_regs.line += adv;
3980                    break;
3981
3982                  case DW_LNS_set_file:
3983                    adv = read_uleb128 (data, & bytes_read, end);
3984                    data += bytes_read;
3985                    state_machine_regs.file = adv;
3986
3987                    {
3988                      unsigned file = state_machine_regs.file - 1;
3989                      unsigned dir;
3990
3991                      if (file_table == NULL || n_files == 0)
3992                        printf (_("\n [Use file table entry %d]\n"), file);
3993                      /* PR 20439 */
3994                      else if (file >= n_files)
3995                        {
3996                          warn (_("file index %u > number of files %u\n"), file + 1, n_files);
3997                          printf (_("\n <over large file table index %u>"), file);
3998                        }
3999                      else if ((dir = file_table[file].directory_index) == 0)
4000                        /* If directory index is 0, that means current directory.  */
4001                        printf ("\n./%s:[++]\n", file_table[file].name);
4002                      else if (directory_table == NULL || n_directories == 0)
4003                        printf (_("\n [Use file %s in directory table entry %d]\n"),
4004                                file_table[file].name, dir);
4005                      /* PR 20439 */
4006                      else if (dir > n_directories)
4007                        {
4008                          warn (_("directory index %u > number of directories %s\n"),
4009                                dir, dwarf_vmatoa ("u", n_directories));
4010                          printf (_("\n <over large directory table entry %u>\n"), dir);
4011                        }
4012                      else
4013                        printf ("\n%s/%s:\n",
4014                                /* The directory index starts counting at 1.  */
4015                                directory_table[dir - 1], file_table[file].name);
4016                    }
4017                    break;
4018
4019                  case DW_LNS_set_column:
4020                    uladv = read_uleb128 (data, & bytes_read, end);
4021                    data += bytes_read;
4022                    state_machine_regs.column = uladv;
4023                    break;
4024
4025                  case DW_LNS_negate_stmt:
4026                    adv = state_machine_regs.is_stmt;
4027                    adv = ! adv;
4028                    state_machine_regs.is_stmt = adv;
4029                    break;
4030
4031                  case DW_LNS_set_basic_block:
4032                    state_machine_regs.basic_block = 1;
4033                    break;
4034
4035                  case DW_LNS_const_add_pc:
4036                    uladv = ((255 - linfo.li_opcode_base) / linfo.li_line_range);
4037                    if (linfo.li_max_ops_per_insn == 1)
4038                      {
4039                        uladv *= linfo.li_min_insn_length;
4040                        state_machine_regs.address += uladv;
4041                      }
4042                    else
4043                      {
4044                        state_machine_regs.address
4045                          += ((state_machine_regs.op_index + uladv)
4046                              / linfo.li_max_ops_per_insn)
4047                          * linfo.li_min_insn_length;
4048                        state_machine_regs.op_index
4049                          = (state_machine_regs.op_index + uladv)
4050                          % linfo.li_max_ops_per_insn;
4051                      }
4052                    break;
4053
4054                  case DW_LNS_fixed_advance_pc:
4055                    SAFE_BYTE_GET_AND_INC (uladv, data, 2, end);
4056                    state_machine_regs.address += uladv;
4057                    state_machine_regs.op_index = 0;
4058                    break;
4059
4060                  case DW_LNS_set_prologue_end:
4061                    break;
4062
4063                  case DW_LNS_set_epilogue_begin:
4064                    break;
4065
4066                  case DW_LNS_set_isa:
4067                    uladv = read_uleb128 (data, & bytes_read, end);
4068                    data += bytes_read;
4069                    printf (_("  Set ISA to %lu\n"), uladv);
4070                    break;
4071
4072                  default:
4073                    printf (_("  Unknown opcode %d with operands: "), op_code);
4074
4075                    if (standard_opcodes != NULL)
4076                      for (i = standard_opcodes[op_code - 1]; i > 0 ; --i)
4077                        {
4078                          printf ("0x%s%s", dwarf_vmatoa ("x", read_uleb128 (data,
4079                                                                             &bytes_read, end)),
4080                                  i == 1 ? "" : ", ");
4081                          data += bytes_read;
4082                        }
4083                    putchar ('\n');
4084                    break;
4085                  }
4086
4087           /* Only Special opcodes, DW_LNS_copy and DW_LNE_end_sequence adds a row
4088              to the DWARF address/line matrix.  */
4089           if ((is_special_opcode) || (op_code == DW_LNE_end_sequence)
4090               || (op_code == DW_LNS_copy))
4091             {
4092               const unsigned int MAX_FILENAME_LENGTH = 35;
4093               char *fileName;
4094               char *newFileName = NULL;
4095               size_t fileNameLength;
4096
4097               if (file_table)
4098                 {
4099                   unsigned indx = state_machine_regs.file - 1;
4100                   /* PR 20439  */
4101                   if (indx >= n_files)
4102                     {
4103                       warn (_("corrupt file index %u encountered\n"), indx);
4104                       fileName = _("<corrupt>");
4105                     }
4106                   else
4107                     fileName = (char *) file_table[indx].name;
4108                 }
4109               else
4110                 fileName = _("<unknown>");
4111
4112               fileNameLength = strlen (fileName);
4113
4114               if ((fileNameLength > MAX_FILENAME_LENGTH) && (!do_wide))
4115                 {
4116                   newFileName = (char *) xmalloc (MAX_FILENAME_LENGTH + 1);
4117                   /* Truncate file name */
4118                   strncpy (newFileName,
4119                            fileName + fileNameLength - MAX_FILENAME_LENGTH,
4120                            MAX_FILENAME_LENGTH + 1);
4121                 }
4122               else
4123                 {
4124                   newFileName = (char *) xmalloc (fileNameLength + 1);
4125                   strncpy (newFileName, fileName, fileNameLength + 1);
4126                 }
4127
4128               if (!do_wide || (fileNameLength <= MAX_FILENAME_LENGTH))
4129                 {
4130                   if (linfo.li_max_ops_per_insn == 1)
4131                     printf ("%-35s  %11d  %#18" DWARF_VMA_FMT "x\n",
4132                             newFileName, state_machine_regs.line,
4133                             state_machine_regs.address);
4134                   else
4135                     printf ("%-35s  %11d  %#18" DWARF_VMA_FMT "x[%d]\n",
4136                             newFileName, state_machine_regs.line,
4137                             state_machine_regs.address,
4138                             state_machine_regs.op_index);
4139                 }
4140               else
4141                 {
4142                   if (linfo.li_max_ops_per_insn == 1)
4143                     printf ("%s  %11d  %#18" DWARF_VMA_FMT "x\n",
4144                             newFileName, state_machine_regs.line,
4145                             state_machine_regs.address);
4146                   else
4147                     printf ("%s  %11d  %#18" DWARF_VMA_FMT "x[%d]\n",
4148                             newFileName, state_machine_regs.line,
4149                             state_machine_regs.address,
4150                             state_machine_regs.op_index);
4151                 }
4152
4153               if (op_code == DW_LNE_end_sequence)
4154                 printf ("\n");
4155
4156               free (newFileName);
4157             }
4158         }
4159
4160       if (file_table)
4161         {
4162           free (file_table);
4163           file_table = NULL;
4164           n_files = 0;
4165         }
4166
4167       if (directory_table)
4168         {
4169           free (directory_table);
4170           directory_table = NULL;
4171           n_directories = 0;
4172         }
4173
4174       putchar ('\n');
4175     }
4176
4177   return 1;
4178 }
4179
4180 static int
4181 display_debug_lines (struct dwarf_section *section, void *file)
4182 {
4183   unsigned char *data = section->start;
4184   unsigned char *end = data + section->size;
4185   int retValRaw = 1;
4186   int retValDecoded = 1;
4187
4188   if (do_debug_lines == 0)
4189     do_debug_lines |= FLAG_DEBUG_LINES_RAW;
4190
4191   if (do_debug_lines & FLAG_DEBUG_LINES_RAW)
4192     retValRaw = display_debug_lines_raw (section, data, end, file);
4193
4194   if (do_debug_lines & FLAG_DEBUG_LINES_DECODED)
4195     retValDecoded = display_debug_lines_decoded (section, data, end, file);
4196
4197   if (!retValRaw || !retValDecoded)
4198     return 0;
4199
4200   return 1;
4201 }
4202
4203 static debug_info *
4204 find_debug_info_for_offset (unsigned long offset)
4205 {
4206   unsigned int i;
4207
4208   if (num_debug_info_entries == DEBUG_INFO_UNAVAILABLE)
4209     return NULL;
4210
4211   for (i = 0; i < num_debug_info_entries; i++)
4212     if (debug_information[i].cu_offset == offset)
4213       return debug_information + i;
4214
4215   return NULL;
4216 }
4217
4218 static const char *
4219 get_gdb_index_symbol_kind_name (gdb_index_symbol_kind kind)
4220 {
4221   /* See gdb/gdb-index.h.  */
4222   static const char * const kinds[] =
4223   {
4224     N_ ("no info"),
4225     N_ ("type"),
4226     N_ ("variable"),
4227     N_ ("function"),
4228     N_ ("other"),
4229     N_ ("unused5"),
4230     N_ ("unused6"),
4231     N_ ("unused7")
4232   };
4233
4234   return _ (kinds[kind]);
4235 }
4236
4237 static int
4238 display_debug_pubnames_worker (struct dwarf_section *section,
4239                                void *file ATTRIBUTE_UNUSED,
4240                                int is_gnu)
4241 {
4242   DWARF2_Internal_PubNames names;
4243   unsigned char *start = section->start;
4244   unsigned char *end = start + section->size;
4245
4246   /* It does not matter if this load fails,
4247      we test for that later on.  */
4248   load_debug_info (file);
4249
4250   printf (_("Contents of the %s section:\n\n"), section->name);
4251
4252   while (start < end)
4253     {
4254       unsigned char *data;
4255       unsigned char *adr;
4256       dwarf_vma offset;
4257       unsigned int offset_size, initial_length_size;
4258
4259       data = start;
4260
4261       SAFE_BYTE_GET_AND_INC (names.pn_length, data, 4, end);
4262       if (names.pn_length == 0xffffffff)
4263         {
4264           SAFE_BYTE_GET_AND_INC (names.pn_length, data, 8, end);
4265           offset_size = 8;
4266           initial_length_size = 12;
4267         }
4268       else
4269         {
4270           offset_size = 4;
4271           initial_length_size = 4;
4272         }
4273
4274       SAFE_BYTE_GET_AND_INC (names.pn_version, data, 2, end);
4275       SAFE_BYTE_GET_AND_INC (names.pn_offset, data, offset_size, end);
4276
4277       if (num_debug_info_entries != DEBUG_INFO_UNAVAILABLE
4278           && num_debug_info_entries > 0
4279           && find_debug_info_for_offset (names.pn_offset) == NULL)
4280         warn (_(".debug_info offset of 0x%lx in %s section does not point to a CU header.\n"),
4281               (unsigned long) names.pn_offset, section->name);
4282
4283       SAFE_BYTE_GET_AND_INC (names.pn_size, data, offset_size, end);
4284
4285       adr = start + names.pn_length + initial_length_size;
4286       /* PR 17531: file: 7615b6b2.  */
4287       if ((dwarf_signed_vma) names.pn_length < 0
4288           /* PR 17531: file: a5dbeaa7. */
4289           || adr < start)
4290         {
4291           warn (_("Negative length for public name: 0x%lx\n"), (long) names.pn_length);
4292           start = end;
4293         }
4294       else
4295         start = adr;
4296
4297       printf (_("  Length:                              %ld\n"),
4298               (long) names.pn_length);
4299       printf (_("  Version:                             %d\n"),
4300               names.pn_version);
4301       printf (_("  Offset into .debug_info section:     0x%lx\n"),
4302               (unsigned long) names.pn_offset);
4303       printf (_("  Size of area in .debug_info section: %ld\n"),
4304               (long) names.pn_size);
4305
4306       if (names.pn_version != 2 && names.pn_version != 3)
4307         {
4308           static int warned = 0;
4309
4310           if (! warned)
4311             {
4312               warn (_("Only DWARF 2 and 3 pubnames are currently supported\n"));
4313               warned = 1;
4314             }
4315
4316           continue;
4317         }
4318
4319       if (is_gnu)
4320         printf (_("\n    Offset  Kind          Name\n"));
4321       else
4322         printf (_("\n    Offset\tName\n"));
4323
4324       do
4325         {
4326           bfd_size_type maxprint;
4327
4328           SAFE_BYTE_GET (offset, data, offset_size, end);
4329
4330           if (offset != 0)
4331             {
4332               data += offset_size;
4333               if (data >= end)
4334                 break;
4335               maxprint = (end - data) - 1;
4336
4337               if (is_gnu)
4338                 {
4339                   unsigned int kind_data;
4340                   gdb_index_symbol_kind kind;
4341                   const char *kind_name;
4342                   int is_static;
4343
4344                   SAFE_BYTE_GET (kind_data, data, 1, end);
4345                   data++;
4346                   maxprint --;
4347                   /* GCC computes the kind as the upper byte in the CU index
4348                      word, and then right shifts it by the CU index size.
4349                      Left shift KIND to where the gdb-index.h accessor macros
4350                      can use it.  */
4351                   kind_data <<= GDB_INDEX_CU_BITSIZE;
4352                   kind = GDB_INDEX_SYMBOL_KIND_VALUE (kind_data);
4353                   kind_name = get_gdb_index_symbol_kind_name (kind);
4354                   is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (kind_data);
4355                   printf ("    %-6lx  %s,%-10s  %.*s\n",
4356                           (unsigned long) offset, is_static ? _("s") : _("g"),
4357                           kind_name, (int) maxprint, data);
4358                 }
4359               else
4360                 printf ("    %-6lx\t%.*s\n",
4361                         (unsigned long) offset, (int) maxprint, data);
4362
4363               data += strnlen ((char *) data, maxprint) + 1;
4364               if (data >= end)
4365                 break;
4366             }
4367         }
4368       while (offset != 0);
4369     }
4370
4371   printf ("\n");
4372   return 1;
4373 }
4374
4375 static int
4376 display_debug_pubnames (struct dwarf_section *section, void *file)
4377 {
4378   return display_debug_pubnames_worker (section, file, 0);
4379 }
4380
4381 static int
4382 display_debug_gnu_pubnames (struct dwarf_section *section, void *file)
4383 {
4384   return display_debug_pubnames_worker (section, file, 1);
4385 }
4386
4387 static int
4388 display_debug_macinfo (struct dwarf_section *section,
4389                        void *file ATTRIBUTE_UNUSED)
4390 {
4391   unsigned char *start = section->start;
4392   unsigned char *end = start + section->size;
4393   unsigned char *curr = start;
4394   unsigned int bytes_read;
4395   enum dwarf_macinfo_record_type op;
4396
4397   printf (_("Contents of the %s section:\n\n"), section->name);
4398
4399   while (curr < end)
4400     {
4401       unsigned int lineno;
4402       const unsigned char *string;
4403
4404       op = (enum dwarf_macinfo_record_type) *curr;
4405       curr++;
4406
4407       switch (op)
4408         {
4409         case DW_MACINFO_start_file:
4410           {
4411             unsigned int filenum;
4412
4413             lineno = read_uleb128 (curr, & bytes_read, end);
4414             curr += bytes_read;
4415             filenum = read_uleb128 (curr, & bytes_read, end);
4416             curr += bytes_read;
4417
4418             printf (_(" DW_MACINFO_start_file - lineno: %d filenum: %d\n"),
4419                     lineno, filenum);
4420           }
4421           break;
4422
4423         case DW_MACINFO_end_file:
4424           printf (_(" DW_MACINFO_end_file\n"));
4425           break;
4426
4427         case DW_MACINFO_define:
4428           lineno = read_uleb128 (curr, & bytes_read, end);
4429           curr += bytes_read;
4430           string = curr;
4431           curr += strnlen ((char *) string, end - string) + 1;
4432           printf (_(" DW_MACINFO_define - lineno : %d macro : %s\n"),
4433                   lineno, string);
4434           break;
4435
4436         case DW_MACINFO_undef:
4437           lineno = read_uleb128 (curr, & bytes_read, end);
4438           curr += bytes_read;
4439           string = curr;
4440           curr += strnlen ((char *) string, end - string) + 1;
4441           printf (_(" DW_MACINFO_undef - lineno : %d macro : %s\n"),
4442                   lineno, string);
4443           break;
4444
4445         case DW_MACINFO_vendor_ext:
4446           {
4447             unsigned int constant;
4448
4449             constant = read_uleb128 (curr, & bytes_read, end);
4450             curr += bytes_read;
4451             string = curr;
4452             curr += strnlen ((char *) string, end - string) + 1;
4453             printf (_(" DW_MACINFO_vendor_ext - constant : %d string : %s\n"),
4454                     constant, string);
4455           }
4456           break;
4457         }
4458     }
4459
4460   return 1;
4461 }
4462
4463 /* Given LINE_OFFSET into the .debug_line section, attempt to return
4464    filename and dirname corresponding to file name table entry with index
4465    FILEIDX.  Return NULL on failure.  */
4466
4467 static unsigned char *
4468 get_line_filename_and_dirname (dwarf_vma line_offset,
4469                                dwarf_vma fileidx,
4470                                unsigned char **dir_name)
4471 {
4472   struct dwarf_section *section = &debug_displays [line].section;
4473   unsigned char *hdrptr, *dirtable, *file_name;
4474   unsigned int offset_size, initial_length_size;
4475   unsigned int version, opcode_base, bytes_read;
4476   dwarf_vma length, diridx;
4477   const unsigned char * end;
4478
4479   *dir_name = NULL;
4480   if (section->start == NULL
4481       || line_offset >= section->size
4482       || fileidx == 0)
4483     return NULL;
4484
4485   hdrptr = section->start + line_offset;
4486   end = section->start + section->size;
4487
4488   SAFE_BYTE_GET_AND_INC (length, hdrptr, 4, end);
4489   if (length == 0xffffffff)
4490     {
4491       /* This section is 64-bit DWARF 3.  */
4492       SAFE_BYTE_GET_AND_INC (length, hdrptr, 8, end);
4493       offset_size = 8;
4494       initial_length_size = 12;
4495     }
4496   else
4497     {
4498       offset_size = 4;
4499       initial_length_size = 4;
4500     }
4501   if (length + initial_length_size > section->size)
4502     return NULL;
4503
4504   SAFE_BYTE_GET_AND_INC (version, hdrptr, 2, end);
4505   if (version != 2 && version != 3 && version != 4)
4506     return NULL;
4507   hdrptr += offset_size + 1;/* Skip prologue_length and min_insn_length.  */
4508   if (version >= 4)
4509     hdrptr++;               /* Skip max_ops_per_insn.  */
4510   hdrptr += 3;              /* Skip default_is_stmt, line_base, line_range.  */
4511
4512   SAFE_BYTE_GET_AND_INC (opcode_base, hdrptr, 1, end);
4513   if (opcode_base == 0)
4514     return NULL;
4515
4516   hdrptr += opcode_base - 1;
4517   dirtable = hdrptr;
4518   /* Skip over dirname table.  */
4519   while (*hdrptr != '\0')
4520     hdrptr += strnlen ((char *) hdrptr, end - hdrptr) + 1;
4521   hdrptr++;                 /* Skip the NUL at the end of the table.  */
4522   /* Now skip over preceding filename table entries.  */
4523   for (; *hdrptr != '\0' && fileidx > 1; fileidx--)
4524     {
4525       hdrptr += strnlen ((char *) hdrptr, end - hdrptr) + 1;
4526       read_uleb128 (hdrptr, &bytes_read, end);
4527       hdrptr += bytes_read;
4528       read_uleb128 (hdrptr, &bytes_read, end);
4529       hdrptr += bytes_read;
4530       read_uleb128 (hdrptr, &bytes_read, end);
4531       hdrptr += bytes_read;
4532     }
4533   if (hdrptr == end || *hdrptr == '\0')
4534     return NULL;
4535   file_name = hdrptr;
4536   hdrptr += strnlen ((char *) hdrptr, end - hdrptr) + 1;
4537   diridx = read_uleb128 (hdrptr, &bytes_read, end);
4538   if (diridx == 0)
4539     return file_name;
4540   for (; *dirtable != '\0' && diridx > 1; diridx--)
4541     dirtable += strnlen ((char *) dirtable, end - dirtable) + 1;
4542   if (*dirtable == '\0')
4543     return NULL;
4544   *dir_name = dirtable;
4545   return file_name;
4546 }
4547
4548 static int
4549 display_debug_macro (struct dwarf_section *section,
4550                      void *file)
4551 {
4552   unsigned char *start = section->start;
4553   unsigned char *end = start + section->size;
4554   unsigned char *curr = start;
4555   unsigned char *extended_op_buf[256];
4556   unsigned int bytes_read;
4557
4558   load_debug_section (str, file);
4559   load_debug_section (line, file);
4560
4561   printf (_("Contents of the %s section:\n\n"), section->name);
4562
4563   while (curr < end)
4564     {
4565       unsigned int lineno, version, flags;
4566       unsigned int offset_size = 4;
4567       const unsigned char *string;
4568       dwarf_vma line_offset = 0, sec_offset = curr - start, offset;
4569       unsigned char **extended_ops = NULL;
4570
4571       SAFE_BYTE_GET_AND_INC (version, curr, 2, end);
4572       if (version != 4 && version != 5)
4573         {
4574           error (_("Only GNU extension to DWARF 4 or 5 of %s is currently supported.\n"),
4575                  section->name);
4576           return 0;
4577         }
4578
4579       SAFE_BYTE_GET_AND_INC (flags, curr, 1, end);
4580       if (flags & 1)
4581         offset_size = 8;
4582       printf (_("  Offset:                      0x%lx\n"),
4583               (unsigned long) sec_offset);
4584       printf (_("  Version:                     %d\n"), version);
4585       printf (_("  Offset size:                 %d\n"), offset_size);
4586       if (flags & 2)
4587         {
4588           SAFE_BYTE_GET_AND_INC (line_offset, curr, offset_size, end);
4589           printf (_("  Offset into .debug_line:     0x%lx\n"),
4590                   (unsigned long) line_offset);
4591         }
4592       if (flags & 4)
4593         {
4594           unsigned int i, count, op;
4595           dwarf_vma nargs, n;
4596
4597           SAFE_BYTE_GET_AND_INC (count, curr, 1, end);
4598
4599           memset (extended_op_buf, 0, sizeof (extended_op_buf));
4600           extended_ops = extended_op_buf;
4601           if (count)
4602             {
4603               printf (_("  Extension opcode arguments:\n"));
4604               for (i = 0; i < count; i++)
4605                 {
4606                   SAFE_BYTE_GET_AND_INC (op, curr, 1, end);
4607                   extended_ops[op] = curr;
4608                   nargs = read_uleb128 (curr, &bytes_read, end);
4609                   curr += bytes_read;
4610                   if (nargs == 0)
4611                     printf (_("    DW_MACRO_%02x has no arguments\n"), op);
4612                   else
4613                     {
4614                       printf (_("    DW_MACRO_%02x arguments: "), op);
4615                       for (n = 0; n < nargs; n++)
4616                         {
4617                           unsigned int form;
4618
4619                           SAFE_BYTE_GET_AND_INC (form, curr, 1, end);
4620                           printf ("%s%s", get_FORM_name (form),
4621                                   n == nargs - 1 ? "\n" : ", ");
4622                           switch (form)
4623                             {
4624                             case DW_FORM_data1:
4625                             case DW_FORM_data2:
4626                             case DW_FORM_data4:
4627                             case DW_FORM_data8:
4628                             case DW_FORM_sdata:
4629                             case DW_FORM_udata:
4630                             case DW_FORM_block:
4631                             case DW_FORM_block1:
4632                             case DW_FORM_block2:
4633                             case DW_FORM_block4:
4634                             case DW_FORM_flag:
4635                             case DW_FORM_string:
4636                             case DW_FORM_strp:
4637                             case DW_FORM_sec_offset:
4638                               break;
4639                             default:
4640                               error (_("Invalid extension opcode form %s\n"),
4641                                      get_FORM_name (form));
4642                               return 0;
4643                             }
4644                         }
4645                     }
4646                 }
4647             }
4648         }
4649       printf ("\n");
4650
4651       while (1)
4652         {
4653           unsigned int op;
4654
4655           if (curr >= end)
4656             {
4657               error (_(".debug_macro section not zero terminated\n"));
4658               return 0;
4659             }
4660
4661           SAFE_BYTE_GET_AND_INC (op, curr, 1, end);
4662           if (op == 0)
4663             break;
4664
4665           switch (op)
4666             {
4667             case DW_MACRO_start_file:
4668               {
4669                 unsigned int filenum;
4670                 unsigned char *file_name = NULL, *dir_name = NULL;
4671
4672                 lineno = read_uleb128 (curr, &bytes_read, end);
4673                 curr += bytes_read;
4674                 filenum = read_uleb128 (curr, &bytes_read, end);
4675                 curr += bytes_read;
4676
4677                 if ((flags & 2) == 0)
4678                   error (_("DW_MACRO_start_file used, but no .debug_line offset provided.\n"));
4679                 else
4680                   file_name
4681                     = get_line_filename_and_dirname (line_offset, filenum,
4682                                                      &dir_name);
4683                 if (file_name == NULL)
4684                   printf (_(" DW_MACRO_start_file - lineno: %d filenum: %d\n"),
4685                           lineno, filenum);
4686                 else
4687                   printf (_(" DW_MACRO_start_file - lineno: %d filenum: %d filename: %s%s%s\n"),
4688                           lineno, filenum,
4689                           dir_name != NULL ? (const char *) dir_name : "",
4690                           dir_name != NULL ? "/" : "", file_name);
4691               }
4692               break;
4693
4694             case DW_MACRO_end_file:
4695               printf (_(" DW_MACRO_end_file\n"));
4696               break;
4697
4698             case DW_MACRO_define:
4699               lineno = read_uleb128 (curr, &bytes_read, end);
4700               curr += bytes_read;
4701               string = curr;
4702               curr += strnlen ((char *) string, end - string) + 1;
4703               printf (_(" DW_MACRO_define - lineno : %d macro : %s\n"),
4704                       lineno, string);
4705               break;
4706
4707             case DW_MACRO_undef:
4708               lineno = read_uleb128 (curr, &bytes_read, end);
4709               curr += bytes_read;
4710               string = curr;
4711               curr += strnlen ((char *) string, end - string) + 1;
4712               printf (_(" DW_MACRO_undef - lineno : %d macro : %s\n"),
4713                       lineno, string);
4714               break;
4715
4716             case DW_MACRO_define_strp:
4717               lineno = read_uleb128 (curr, &bytes_read, end);
4718               curr += bytes_read;
4719               SAFE_BYTE_GET_AND_INC (offset, curr, offset_size, end);
4720               string = fetch_indirect_string (offset);
4721               printf (_(" DW_MACRO_define_strp - lineno : %d macro : %s\n"),
4722                       lineno, string);
4723               break;
4724
4725             case DW_MACRO_undef_strp:
4726               lineno = read_uleb128 (curr, &bytes_read, end);
4727               curr += bytes_read;
4728               SAFE_BYTE_GET_AND_INC (offset, curr, offset_size, end);
4729               string = fetch_indirect_string (offset);
4730               printf (_(" DW_MACRO_undef_strp - lineno : %d macro : %s\n"),
4731                       lineno, string);
4732               break;
4733
4734             case DW_MACRO_import:
4735               SAFE_BYTE_GET_AND_INC (offset, curr, offset_size, end);
4736               printf (_(" DW_MACRO_import - offset : 0x%lx\n"),
4737                       (unsigned long) offset);
4738               break;
4739
4740             case DW_MACRO_define_sup:
4741               lineno = read_uleb128 (curr, &bytes_read, end);
4742               curr += bytes_read;
4743               SAFE_BYTE_GET_AND_INC (offset, curr, offset_size, end);
4744               printf (_(" DW_MACRO_define_sup - lineno : %d macro offset : 0x%lx\n"),
4745                       lineno, (unsigned long) offset);
4746               break;
4747
4748             case DW_MACRO_undef_sup:
4749               lineno = read_uleb128 (curr, &bytes_read, end);
4750               curr += bytes_read;
4751               SAFE_BYTE_GET_AND_INC (offset, curr, offset_size, end);
4752               printf (_(" DW_MACRO_undef_sup - lineno : %d macro offset : 0x%lx\n"),
4753                       lineno, (unsigned long) offset);
4754               break;
4755
4756             case DW_MACRO_import_sup:
4757               SAFE_BYTE_GET_AND_INC (offset, curr, offset_size, end);
4758               printf (_(" DW_MACRO_import_sup - offset : 0x%lx\n"),
4759                       (unsigned long) offset);
4760               break;
4761
4762             default:
4763               if (extended_ops == NULL || extended_ops[op] == NULL)
4764                 {
4765                   error (_(" Unknown macro opcode %02x seen\n"), op);
4766                   return 0;
4767                 }
4768               else
4769                 {
4770                   /* Skip over unhandled opcodes.  */
4771                   dwarf_vma nargs, n;
4772                   unsigned char *desc = extended_ops[op];
4773                   nargs = read_uleb128 (desc, &bytes_read, end);
4774                   desc += bytes_read;
4775                   if (nargs == 0)
4776                     {
4777                       printf (_(" DW_MACRO_%02x\n"), op);
4778                       break;
4779                     }
4780                   printf (_(" DW_MACRO_%02x -"), op);
4781                   for (n = 0; n < nargs; n++)
4782                     {
4783                       int val;
4784
4785                       /* DW_FORM_implicit_const is not expected here.  */
4786                       SAFE_BYTE_GET_AND_INC (val, desc, 1, end);
4787                       curr
4788                         = read_and_display_attr_value (0, val, 0,
4789                                                        curr, end, 0, 0, offset_size,
4790                                                        version, NULL, 0, NULL,
4791                                                        NULL, ' ');
4792                       if (n != nargs - 1)
4793                         printf (",");
4794                     }
4795                   printf ("\n");
4796                 }
4797               break;
4798             }
4799         }
4800
4801       printf ("\n");
4802     }
4803
4804   return 1;
4805 }
4806
4807 static int
4808 display_debug_abbrev (struct dwarf_section *section,
4809                       void *file ATTRIBUTE_UNUSED)
4810 {
4811   abbrev_entry *entry;
4812   unsigned char *start = section->start;
4813   unsigned char *end = start + section->size;
4814
4815   printf (_("Contents of the %s section:\n\n"), section->name);
4816
4817   do
4818     {
4819       unsigned char *last;
4820
4821       free_abbrevs ();
4822
4823       last = start;
4824       start = process_abbrev_section (start, end);
4825
4826       if (first_abbrev == NULL)
4827         continue;
4828
4829       printf (_("  Number TAG (0x%lx)\n"), (long) (last - section->start));
4830
4831       for (entry = first_abbrev; entry; entry = entry->next)
4832         {
4833           abbrev_attr *attr;
4834
4835           printf ("   %ld      %s    [%s]\n",
4836                   entry->entry,
4837                   get_TAG_name (entry->tag),
4838                   entry->children ? _("has children") : _("no children"));
4839
4840           for (attr = entry->first_attr; attr; attr = attr->next)
4841             {
4842               printf ("    %-18s %s",
4843                       get_AT_name (attr->attribute),
4844                       get_FORM_name (attr->form));
4845               if (attr->form == DW_FORM_implicit_const)
4846                 printf (": %" BFD_VMA_FMT "d", attr->implicit_const);
4847               putchar ('\n');
4848             }
4849         }
4850     }
4851   while (start);
4852
4853   printf ("\n");
4854
4855   return 1;
4856 }
4857
4858 /* Return true when ADDR is the maximum address, when addresses are
4859    POINTER_SIZE bytes long.  */
4860
4861 static bfd_boolean
4862 is_max_address (dwarf_vma addr, unsigned int pointer_size)
4863 {
4864   dwarf_vma mask = ~(~(dwarf_vma) 1 << (pointer_size * 8 - 1));
4865   return ((addr & mask) == mask);
4866 }
4867
4868 /* Display a location list from a normal (ie, non-dwo) .debug_loc section.  */
4869
4870 static void
4871 display_loc_list (struct dwarf_section *section,
4872                   unsigned char **start_ptr,
4873                   unsigned int debug_info_entry,
4874                   dwarf_vma offset,
4875                   dwarf_vma base_address,
4876                   int has_frame_base)
4877 {
4878   unsigned char *start = *start_ptr;
4879   unsigned char *section_end = section->start + section->size;
4880   unsigned long cu_offset;
4881   unsigned int pointer_size;
4882   unsigned int offset_size;
4883   int dwarf_version;
4884
4885   dwarf_vma begin;
4886   dwarf_vma end;
4887   unsigned short length;
4888   int need_frame_base;
4889
4890   if (debug_info_entry >= num_debug_info_entries)
4891     {
4892       warn (_("No debug information available for loc lists of entry: %u\n"),
4893             debug_info_entry);
4894       return;
4895     }
4896
4897   cu_offset = debug_information [debug_info_entry].cu_offset;
4898   pointer_size = debug_information [debug_info_entry].pointer_size;
4899   offset_size = debug_information [debug_info_entry].offset_size;
4900   dwarf_version = debug_information [debug_info_entry].dwarf_version;
4901
4902   if (pointer_size < 2 || pointer_size > 8)
4903     {
4904       warn (_("Invalid pointer size (%d) in debug info for entry %d\n"),
4905             pointer_size, debug_info_entry);
4906       return;
4907     }
4908
4909   while (1)
4910     {
4911       dwarf_vma off = offset + (start - *start_ptr);
4912
4913       if (start + 2 * pointer_size > section_end)
4914         {
4915           warn (_("Location list starting at offset 0x%lx is not terminated.\n"),
4916                 (unsigned long) offset);
4917           break;
4918         }
4919
4920       printf ("    %8.8lx ", (unsigned long) off);
4921
4922       SAFE_BYTE_GET_AND_INC (begin, start, pointer_size, section_end);
4923       SAFE_BYTE_GET_AND_INC (end, start, pointer_size, section_end);
4924
4925       if (begin == 0 && end == 0)
4926         {
4927           /* PR 18374: In a object file we can have a location list that
4928              starts with a begin and end of 0 because there are relocations
4929              that need to be applied to the addresses.  Actually applying
4930              the relocations now does not help as they will probably resolve
4931              to 0, since the object file has not been fully linked.  Real
4932              end of list markers will not have any relocations against them.  */
4933           if (! reloc_at (section, off)
4934               && ! reloc_at (section, off + pointer_size))
4935             {
4936               printf (_("<End of list>\n"));
4937               break;
4938             }
4939         }
4940
4941       /* Check base address specifiers.  */
4942       if (is_max_address (begin, pointer_size)
4943           && !is_max_address (end, pointer_size))
4944         {
4945           base_address = end;
4946           print_dwarf_vma (begin, pointer_size);
4947           print_dwarf_vma (end, pointer_size);
4948           printf (_("(base address)\n"));
4949           continue;
4950         }
4951
4952       if (start + 2 > section_end)
4953         {
4954           warn (_("Location list starting at offset 0x%lx is not terminated.\n"),
4955                 (unsigned long) offset);
4956           break;
4957         }
4958
4959       SAFE_BYTE_GET_AND_INC (length, start, 2, section_end);
4960
4961       if (start + length > section_end)
4962         {
4963           warn (_("Location list starting at offset 0x%lx is not terminated.\n"),
4964                 (unsigned long) offset);
4965           break;
4966         }
4967
4968       print_dwarf_vma (begin + base_address, pointer_size);
4969       print_dwarf_vma (end + base_address, pointer_size);
4970
4971       putchar ('(');
4972       need_frame_base = decode_location_expression (start,
4973                                                     pointer_size,
4974                                                     offset_size,
4975                                                     dwarf_version,
4976                                                     length,
4977                                                     cu_offset, section);
4978       putchar (')');
4979
4980       if (need_frame_base && !has_frame_base)
4981         printf (_(" [without DW_AT_frame_base]"));
4982
4983       if (begin == end)
4984         fputs (_(" (start == end)"), stdout);
4985       else if (begin > end)
4986         fputs (_(" (start > end)"), stdout);
4987
4988       putchar ('\n');
4989
4990       start += length;
4991     }
4992
4993   *start_ptr = start;
4994 }
4995
4996 /* Display a location list from a normal (ie, non-dwo) .debug_loclists section.  */
4997
4998 static void
4999 display_loclists_list (struct dwarf_section *section,
5000                        unsigned char **start_ptr,
5001                        unsigned int debug_info_entry,
5002                        dwarf_vma offset,
5003                        dwarf_vma base_address,
5004                        int has_frame_base)
5005 {
5006   unsigned char *start = *start_ptr;
5007   unsigned char *section_end = section->start + section->size;
5008   unsigned long cu_offset;
5009   unsigned int pointer_size;
5010   unsigned int offset_size;
5011   int dwarf_version;
5012   unsigned int bytes_read;
5013
5014   /* Initialize it due to a false compiler warning.  */
5015   dwarf_vma begin = -1;
5016   dwarf_vma end = -1;
5017   dwarf_vma length;
5018   int need_frame_base;
5019
5020   if (debug_info_entry >= num_debug_info_entries)
5021     {
5022       warn (_("No debug information available for "
5023               "loclists lists of entry: %u\n"),
5024             debug_info_entry);
5025       return;
5026     }
5027
5028   cu_offset = debug_information [debug_info_entry].cu_offset;
5029   pointer_size = debug_information [debug_info_entry].pointer_size;
5030   offset_size = debug_information [debug_info_entry].offset_size;
5031   dwarf_version = debug_information [debug_info_entry].dwarf_version;
5032
5033   if (pointer_size < 2 || pointer_size > 8)
5034     {
5035       warn (_("Invalid pointer size (%d) in debug info for entry %d\n"),
5036             pointer_size, debug_info_entry);
5037       return;
5038     }
5039
5040   while (1)
5041     {
5042       dwarf_vma off = offset + (start - *start_ptr);
5043       enum dwarf_location_list_entry_type llet;
5044
5045       if (start + 1 > section_end)
5046         {
5047           warn (_("Location list starting at offset 0x%lx is not terminated.\n"),
5048                 (unsigned long) offset);
5049           break;
5050         }
5051
5052       printf ("    %8.8lx ", (unsigned long) off);
5053
5054       SAFE_BYTE_GET_AND_INC (llet, start, 1, section_end);
5055
5056       switch (llet)
5057         {
5058         case DW_LLE_end_of_list:
5059           printf (_("<End of list>\n"));
5060           break;
5061         case DW_LLE_offset_pair:
5062           begin = read_uleb128 (start, &bytes_read, section_end);
5063           start += bytes_read;
5064           end = read_uleb128 (start, &bytes_read, section_end);
5065           start += bytes_read;
5066           break;
5067         case DW_LLE_base_address:
5068           SAFE_BYTE_GET_AND_INC (base_address, start, pointer_size,
5069                                  section_end);
5070           print_dwarf_vma (base_address, pointer_size);
5071           printf (_("(base address)\n"));
5072           break;
5073         default:
5074           error (_("Invalid location list entry type %d\n"), llet);
5075           return;
5076         }
5077       if (llet == DW_LLE_end_of_list)
5078         break;
5079       if (llet != DW_LLE_offset_pair)
5080         continue;
5081
5082       if (start + 2 > section_end)
5083         {
5084           warn (_("Location list starting at offset 0x%lx is not terminated.\n"),
5085                 (unsigned long) offset);
5086           break;
5087         }
5088
5089       length = read_uleb128 (start, &bytes_read, section_end);
5090       start += bytes_read;
5091
5092       print_dwarf_vma (begin + base_address, pointer_size);
5093       print_dwarf_vma (end + base_address, pointer_size);
5094
5095       putchar ('(');
5096       need_frame_base = decode_location_expression (start,
5097                                                     pointer_size,
5098                                                     offset_size,
5099                                                     dwarf_version,
5100                                                     length,
5101                                                     cu_offset, section);
5102       putchar (')');
5103
5104       if (need_frame_base && !has_frame_base)
5105         printf (_(" [without DW_AT_frame_base]"));
5106
5107       if (begin == end)
5108         fputs (_(" (start == end)"), stdout);
5109       else if (begin > end)
5110         fputs (_(" (start > end)"), stdout);
5111
5112       putchar ('\n');
5113
5114       start += length;
5115     }
5116
5117   *start_ptr = start;
5118 }
5119
5120 /* Print a .debug_addr table index in decimal, surrounded by square brackets,
5121    right-adjusted in a field of length LEN, and followed by a space.  */
5122
5123 static void
5124 print_addr_index (unsigned int idx, unsigned int len)
5125 {
5126   static char buf[15];
5127   snprintf (buf, sizeof (buf), "[%d]", idx);
5128   printf ("%*s ", len, buf);
5129 }
5130
5131 /* Display a location list from a .dwo section. It uses address indexes rather
5132    than embedded addresses.  This code closely follows display_loc_list, but the
5133    two are sufficiently different that combining things is very ugly.  */
5134
5135 static void
5136 display_loc_list_dwo (struct dwarf_section *section,
5137                       unsigned char **start_ptr,
5138                       unsigned int debug_info_entry,
5139                       dwarf_vma offset,
5140                       int has_frame_base)
5141 {
5142   unsigned char *start = *start_ptr;
5143   unsigned char *section_end = section->start + section->size;
5144   unsigned long cu_offset;
5145   unsigned int pointer_size;
5146   unsigned int offset_size;
5147   int dwarf_version;
5148   int entry_type;
5149   unsigned short length;
5150   int need_frame_base;
5151   unsigned int idx;
5152   unsigned int bytes_read;
5153
5154   if (debug_info_entry >= num_debug_info_entries)
5155     {
5156       warn (_("No debug information for loc lists of entry: %u\n"),
5157             debug_info_entry);
5158       return;
5159     }
5160
5161   cu_offset = debug_information [debug_info_entry].cu_offset;
5162   pointer_size = debug_information [debug_info_entry].pointer_size;
5163   offset_size = debug_information [debug_info_entry].offset_size;
5164   dwarf_version = debug_information [debug_info_entry].dwarf_version;
5165
5166   if (pointer_size < 2 || pointer_size > 8)
5167     {
5168       warn (_("Invalid pointer size (%d) in debug info for entry %d\n"),
5169             pointer_size, debug_info_entry);
5170       return;
5171     }
5172
5173   while (1)
5174     {
5175       printf ("    %8.8lx ", (unsigned long) (offset + (start - *start_ptr)));
5176
5177       if (start >= section_end)
5178         {
5179           warn (_("Location list starting at offset 0x%lx is not terminated.\n"),
5180                 (unsigned long) offset);
5181           break;
5182         }
5183
5184       SAFE_BYTE_GET_AND_INC (entry_type, start, 1, section_end);
5185       switch (entry_type)
5186         {
5187         case 0: /* A terminating entry.  */
5188           *start_ptr = start;
5189           printf (_("<End of list>\n"));
5190           return;
5191         case 1: /* A base-address entry.  */
5192           idx = read_uleb128 (start, &bytes_read, section_end);
5193           start += bytes_read;
5194           print_addr_index (idx, 8);
5195           printf ("         ");
5196           printf (_("(base address selection entry)\n"));
5197           continue;
5198         case 2: /* A start/end entry.  */
5199           idx = read_uleb128 (start, &bytes_read, section_end);
5200           start += bytes_read;
5201           print_addr_index (idx, 8);
5202           idx = read_uleb128 (start, &bytes_read, section_end);
5203           start += bytes_read;
5204           print_addr_index (idx, 8);
5205           break;
5206         case 3: /* A start/length entry.  */
5207           idx = read_uleb128 (start, &bytes_read, section_end);
5208           start += bytes_read;
5209           print_addr_index (idx, 8);
5210           SAFE_BYTE_GET_AND_INC (idx, start, 4, section_end);
5211           printf ("%08x ", idx);
5212           break;
5213         case 4: /* An offset pair entry.  */
5214           SAFE_BYTE_GET_AND_INC (idx, start, 4, section_end);
5215           printf ("%08x ", idx);
5216           SAFE_BYTE_GET_AND_INC (idx, start, 4, section_end);
5217           printf ("%08x ", idx);
5218           break;
5219         default:
5220           warn (_("Unknown location list entry type 0x%x.\n"), entry_type);
5221           *start_ptr = start;
5222           return;
5223         }
5224
5225       if (start + 2 > section_end)
5226         {
5227           warn (_("Location list starting at offset 0x%lx is not terminated.\n"),
5228                 (unsigned long) offset);
5229           break;
5230         }
5231
5232       SAFE_BYTE_GET_AND_INC (length, start, 2, section_end);
5233       if (start + length > section_end)
5234         {
5235           warn (_("Location list starting at offset 0x%lx is not terminated.\n"),
5236                 (unsigned long) offset);
5237           break;
5238         }
5239
5240       putchar ('(');
5241       need_frame_base = decode_location_expression (start,
5242                                                     pointer_size,
5243                                                     offset_size,
5244                                                     dwarf_version,
5245                                                     length,
5246                                                     cu_offset, section);
5247       putchar (')');
5248
5249       if (need_frame_base && !has_frame_base)
5250         printf (_(" [without DW_AT_frame_base]"));
5251
5252       putchar ('\n');
5253
5254       start += length;
5255     }
5256
5257   *start_ptr = start;
5258 }
5259
5260 /* Sort array of indexes in ascending order of loc_offsets[idx].  */
5261
5262 static dwarf_vma *loc_offsets;
5263
5264 static int
5265 loc_offsets_compar (const void *ap, const void *bp)
5266 {
5267   dwarf_vma a = loc_offsets[*(const unsigned int *) ap];
5268   dwarf_vma b = loc_offsets[*(const unsigned int *) bp];
5269
5270   return (a > b) - (b > a);
5271 }
5272
5273 static int
5274 display_debug_loc (struct dwarf_section *section, void *file)
5275 {
5276   unsigned char *start = section->start;
5277   unsigned long bytes;
5278   unsigned char *section_begin = start;
5279   unsigned int num_loc_list = 0;
5280   unsigned long last_offset = 0;
5281   unsigned int first = 0;
5282   unsigned int i;
5283   unsigned int j;
5284   int seen_first_offset = 0;
5285   int locs_sorted = 1;
5286   unsigned char *next;
5287   unsigned int *array = NULL;
5288   const char *suffix = strrchr (section->name, '.');
5289   int is_dwo = 0;
5290   int is_loclists = strstr (section->name, "debug_loclists") != NULL;
5291   dwarf_vma expected_start = 0;
5292
5293   if (suffix && strcmp (suffix, ".dwo") == 0)
5294     is_dwo = 1;
5295
5296   bytes = section->size;
5297
5298   if (bytes == 0)
5299     {
5300       printf (_("\nThe %s section is empty.\n"), section->name);
5301       return 0;
5302     }
5303
5304   if (is_loclists)
5305     {
5306       unsigned char *hdrptr = section_begin;
5307       dwarf_vma ll_length;
5308       unsigned short ll_version;
5309       unsigned char *end = section_begin + section->size;
5310       unsigned char address_size, segment_selector_size;
5311       uint32_t offset_entry_count;
5312
5313       SAFE_BYTE_GET_AND_INC (ll_length, hdrptr, 4, end);
5314       if (ll_length == 0xffffffff)
5315         SAFE_BYTE_GET_AND_INC (ll_length, hdrptr, 8, end);
5316
5317       SAFE_BYTE_GET_AND_INC (ll_version, hdrptr, 2, end);
5318       if (ll_version != 5)
5319         {
5320           warn (_("The %s section contains corrupt or "
5321                   "unsupported version number: %d.\n"),
5322                 section->name, ll_version);
5323           return 0;
5324         }
5325
5326       SAFE_BYTE_GET_AND_INC (address_size, hdrptr, 1, end);
5327
5328       SAFE_BYTE_GET_AND_INC (segment_selector_size, hdrptr, 1, end);
5329       if (segment_selector_size != 0)
5330         {
5331           warn (_("The %s section contains "
5332                   "unsupported segment selector size: %d.\n"),
5333                 section->name, segment_selector_size);
5334           return 0;
5335         }
5336
5337       SAFE_BYTE_GET_AND_INC (offset_entry_count, hdrptr, 4, end);
5338       if (offset_entry_count != 0)
5339         {
5340           warn (_("The %s section contains "
5341                   "unsupported offset entry count: %d.\n"),
5342                 section->name, offset_entry_count);
5343           return 0;
5344         }
5345
5346       expected_start = hdrptr - section_begin;
5347     }
5348
5349   if (load_debug_info (file) == 0)
5350     {
5351       warn (_("Unable to load/parse the .debug_info section, so cannot interpret the %s section.\n"),
5352             section->name);
5353       return 0;
5354     }
5355
5356   /* Check the order of location list in .debug_info section. If
5357      offsets of location lists are in the ascending order, we can
5358      use `debug_information' directly.  */
5359   for (i = 0; i < num_debug_info_entries; i++)
5360     {
5361       unsigned int num;
5362
5363       num = debug_information [i].num_loc_offsets;
5364       if (num > num_loc_list)
5365         num_loc_list = num;
5366
5367       /* Check if we can use `debug_information' directly.  */
5368       if (locs_sorted && num != 0)
5369         {
5370           if (!seen_first_offset)
5371             {
5372               /* This is the first location list.  */
5373               last_offset = debug_information [i].loc_offsets [0];
5374               first = i;
5375               seen_first_offset = 1;
5376               j = 1;
5377             }
5378           else
5379             j = 0;
5380
5381           for (; j < num; j++)
5382             {
5383               if (last_offset >
5384                   debug_information [i].loc_offsets [j])
5385                 {
5386                   locs_sorted = 0;
5387                   break;
5388                 }
5389               last_offset = debug_information [i].loc_offsets [j];
5390             }
5391         }
5392     }
5393
5394   if (!seen_first_offset)
5395     error (_("No location lists in .debug_info section!\n"));
5396
5397   if (debug_information [first].num_loc_offsets > 0
5398       && debug_information [first].loc_offsets [0] != expected_start)
5399     warn (_("Location lists in %s section start at 0x%s\n"),
5400           section->name,
5401           dwarf_vmatoa ("x", debug_information [first].loc_offsets [0]));
5402
5403   if (!locs_sorted)
5404     array = (unsigned int *) xcmalloc (num_loc_list, sizeof (unsigned int));
5405   printf (_("Contents of the %s section:\n\n"), section->name);
5406   if (reloc_at (section, 0))
5407     printf (_(" Warning: This section has relocations - addresses seen here may not be accurate.\n\n"));
5408   printf (_("    Offset   Begin            End              Expression\n"));
5409
5410   seen_first_offset = 0;
5411   for (i = first; i < num_debug_info_entries; i++)
5412     {
5413       dwarf_vma offset;
5414       dwarf_vma base_address;
5415       unsigned int k;
5416       int has_frame_base;
5417
5418       if (!locs_sorted)
5419         {
5420           for (k = 0; k < debug_information [i].num_loc_offsets; k++)
5421             array[k] = k;
5422           loc_offsets = debug_information [i].loc_offsets;
5423           qsort (array, debug_information [i].num_loc_offsets,
5424                  sizeof (*array), loc_offsets_compar);
5425         }
5426
5427       for (k = 0; k < debug_information [i].num_loc_offsets; k++)
5428         {
5429           j = locs_sorted ? k : array[k];
5430           if (k
5431               && debug_information [i].loc_offsets [locs_sorted
5432                                                     ? k - 1 : array [k - 1]]
5433                  == debug_information [i].loc_offsets [j])
5434             continue;
5435           has_frame_base = debug_information [i].have_frame_base [j];
5436           offset = debug_information [i].loc_offsets [j];
5437           next = section_begin + offset;
5438           base_address = debug_information [i].base_address;
5439
5440           if (!seen_first_offset)
5441             seen_first_offset = 1;
5442           else
5443             {
5444               if (start < next)
5445                 warn (_("There is a hole [0x%lx - 0x%lx] in .debug_loc section.\n"),
5446                       (unsigned long) (start - section_begin),
5447                       (unsigned long) offset);
5448               else if (start > next)
5449                 warn (_("There is an overlap [0x%lx - 0x%lx] in .debug_loc section.\n"),
5450                       (unsigned long) (start - section_begin),
5451                       (unsigned long) offset);
5452             }
5453           start = next;
5454
5455           if (offset >= bytes)
5456             {
5457               warn (_("Offset 0x%lx is bigger than .debug_loc section size.\n"),
5458                     (unsigned long) offset);
5459               continue;
5460             }
5461
5462           if (!is_loclists)
5463             {
5464               if (is_dwo)
5465                 display_loc_list_dwo (section, &start, i, offset,
5466                                       has_frame_base);
5467               else
5468                 display_loc_list (section, &start, i, offset, base_address,
5469                                   has_frame_base);
5470             }
5471           else
5472             {
5473               if (is_dwo)
5474                 warn (_("DWO is not yet supported.\n"));
5475               else
5476                 display_loclists_list (section, &start, i, offset, base_address,
5477                                        has_frame_base);
5478             }
5479         }
5480     }
5481
5482   if (start < section->start + section->size)
5483     warn (_("There are %ld unused bytes at the end of section %s\n"),
5484           (long) (section->start + section->size - start), section->name);
5485   putchar ('\n');
5486   free (array);
5487   return 1;
5488 }
5489
5490 static int
5491 display_debug_str (struct dwarf_section *section,
5492                    void *file ATTRIBUTE_UNUSED)
5493 {
5494   unsigned char *start = section->start;
5495   unsigned long bytes = section->size;
5496   dwarf_vma addr = section->address;
5497
5498   if (bytes == 0)
5499     {
5500       printf (_("\nThe %s section is empty.\n"), section->name);
5501       return 0;
5502     }
5503
5504   printf (_("Contents of the %s section:\n\n"), section->name);
5505
5506   while (bytes)
5507     {
5508       int j;
5509       int k;
5510       int lbytes;
5511
5512       lbytes = (bytes > 16 ? 16 : bytes);
5513
5514       printf ("  0x%8.8lx ", (unsigned long) addr);
5515
5516       for (j = 0; j < 16; j++)
5517         {
5518           if (j < lbytes)
5519             printf ("%2.2x", start[j]);
5520           else
5521             printf ("  ");
5522
5523           if ((j & 3) == 3)
5524             printf (" ");
5525         }
5526
5527       for (j = 0; j < lbytes; j++)
5528         {
5529           k = start[j];
5530           if (k >= ' ' && k < 0x80)
5531             printf ("%c", k);
5532           else
5533             printf (".");
5534         }
5535
5536       putchar ('\n');
5537
5538       start += lbytes;
5539       addr  += lbytes;
5540       bytes -= lbytes;
5541     }
5542
5543   putchar ('\n');
5544
5545   return 1;
5546 }
5547
5548 static int
5549 display_debug_info (struct dwarf_section *section, void *file)
5550 {
5551   return process_debug_info (section, file, section->abbrev_sec, 0, 0);
5552 }
5553
5554 static int
5555 display_debug_types (struct dwarf_section *section, void *file)
5556 {
5557   return process_debug_info (section, file, section->abbrev_sec, 0, 1);
5558 }
5559
5560 static int
5561 display_trace_info (struct dwarf_section *section, void *file)
5562 {
5563   return process_debug_info (section, file, section->abbrev_sec, 0, 0);
5564 }
5565
5566 static int
5567 display_debug_aranges (struct dwarf_section *section,
5568                        void *file ATTRIBUTE_UNUSED)
5569 {
5570   unsigned char *start = section->start;
5571   unsigned char *end = start + section->size;
5572
5573   printf (_("Contents of the %s section:\n\n"), section->name);
5574
5575   /* It does not matter if this load fails,
5576      we test for that later on.  */
5577   load_debug_info (file);
5578
5579   while (start < end)
5580     {
5581       unsigned char *hdrptr;
5582       DWARF2_Internal_ARange arange;
5583       unsigned char *addr_ranges;
5584       dwarf_vma length;
5585       dwarf_vma address;
5586       unsigned char address_size;
5587       int excess;
5588       unsigned int offset_size;
5589       unsigned int initial_length_size;
5590
5591       hdrptr = start;
5592
5593       SAFE_BYTE_GET_AND_INC (arange.ar_length, hdrptr, 4, end);
5594       if (arange.ar_length == 0xffffffff)
5595         {
5596           SAFE_BYTE_GET_AND_INC (arange.ar_length, hdrptr, 8, end);
5597           offset_size = 8;
5598           initial_length_size = 12;
5599         }
5600       else
5601         {
5602           offset_size = 4;
5603           initial_length_size = 4;
5604         }
5605
5606       SAFE_BYTE_GET_AND_INC (arange.ar_version, hdrptr, 2, end);
5607       SAFE_BYTE_GET_AND_INC (arange.ar_info_offset, hdrptr, offset_size, end);
5608
5609       if (num_debug_info_entries != DEBUG_INFO_UNAVAILABLE
5610           && num_debug_info_entries > 0
5611           && find_debug_info_for_offset (arange.ar_info_offset) == NULL)
5612         warn (_(".debug_info offset of 0x%lx in %s section does not point to a CU header.\n"),
5613               (unsigned long) arange.ar_info_offset, section->name);
5614
5615       SAFE_BYTE_GET_AND_INC (arange.ar_pointer_size, hdrptr, 1, end);
5616       SAFE_BYTE_GET_AND_INC (arange.ar_segment_size, hdrptr, 1, end);
5617
5618       if (arange.ar_version != 2 && arange.ar_version != 3)
5619         {
5620           /* PR 19872: A version number of 0 probably means that there is
5621              padding at the end of the .debug_aranges section.  Gold puts
5622              it there when performing an incremental link, for example.
5623              So do not generate a warning in this case.  */
5624           if (arange.ar_version)
5625             warn (_("Only DWARF 2 and 3 aranges are currently supported.\n"));
5626           break;
5627         }
5628
5629       printf (_("  Length:                   %ld\n"),
5630               (long) arange.ar_length);
5631       printf (_("  Version:                  %d\n"), arange.ar_version);
5632       printf (_("  Offset into .debug_info:  0x%lx\n"),
5633               (unsigned long) arange.ar_info_offset);
5634       printf (_("  Pointer Size:             %d\n"), arange.ar_pointer_size);
5635       printf (_("  Segment Size:             %d\n"), arange.ar_segment_size);
5636
5637       address_size = arange.ar_pointer_size + arange.ar_segment_size;
5638
5639       /* PR 17512: file: 001-108546-0.001:0.1.  */
5640       if (address_size == 0 || address_size > 8)
5641         {
5642           error (_("Invalid address size in %s section!\n"),
5643                  section->name);
5644           break;
5645         }
5646
5647       /* The DWARF spec does not require that the address size be a power
5648          of two, but we do.  This will have to change if we ever encounter
5649          an uneven architecture.  */
5650       if ((address_size & (address_size - 1)) != 0)
5651         {
5652           warn (_("Pointer size + Segment size is not a power of two.\n"));
5653           break;
5654         }
5655
5656       if (address_size > 4)
5657         printf (_("\n    Address            Length\n"));
5658       else
5659         printf (_("\n    Address    Length\n"));
5660
5661       addr_ranges = hdrptr;
5662
5663       /* Must pad to an alignment boundary that is twice the address size.  */
5664       excess = (hdrptr - start) % (2 * address_size);
5665       if (excess)
5666         addr_ranges += (2 * address_size) - excess;
5667
5668       hdrptr = start + arange.ar_length + initial_length_size;
5669       if (hdrptr < start || hdrptr > end)
5670         {
5671           error (_("Excessive header length: %lx\n"), (long) arange.ar_length);
5672           break;
5673         }
5674       start = hdrptr;
5675
5676       while (addr_ranges + 2 * address_size <= start)
5677         {
5678           SAFE_BYTE_GET_AND_INC (address, addr_ranges, address_size, end);
5679           SAFE_BYTE_GET_AND_INC (length, addr_ranges, address_size, end);
5680
5681           printf ("    ");
5682           print_dwarf_vma (address, address_size);
5683           print_dwarf_vma (length, address_size);
5684           putchar ('\n');
5685         }
5686     }
5687
5688   printf ("\n");
5689
5690   return 1;
5691 }
5692
5693 /* Comparison function for qsort.  */
5694 static int
5695 comp_addr_base (const void * v0, const void * v1)
5696 {
5697   debug_info * info0 = (debug_info *) v0;
5698   debug_info * info1 = (debug_info *) v1;
5699   return info0->addr_base - info1->addr_base;
5700 }
5701
5702 /* Display the debug_addr section.  */
5703 static int
5704 display_debug_addr (struct dwarf_section *section,
5705                     void *file)
5706 {
5707   debug_info **debug_addr_info;
5708   unsigned char *entry;
5709   unsigned char *end;
5710   unsigned int i;
5711   unsigned int count;
5712
5713   if (section->size == 0)
5714     {
5715       printf (_("\nThe %s section is empty.\n"), section->name);
5716       return 0;
5717     }
5718
5719   if (load_debug_info (file) == 0)
5720     {
5721       warn (_("Unable to load/parse the .debug_info section, so cannot interpret the %s section.\n"),
5722             section->name);
5723       return 0;
5724     }
5725
5726   printf (_("Contents of the %s section:\n\n"), section->name);
5727
5728   /* PR  17531: file: cf38d01b.
5729      We use xcalloc because a corrupt file may not have initialised all of the
5730      fields in the debug_info structure, which means that the sort below might
5731      try to move uninitialised data.  */
5732   debug_addr_info = (debug_info **) xcalloc ((num_debug_info_entries + 1),
5733                                              sizeof (debug_info *));
5734
5735   count = 0;
5736   for (i = 0; i < num_debug_info_entries; i++)
5737     if (debug_information [i].addr_base != DEBUG_INFO_UNAVAILABLE)
5738       {
5739         /* PR 17531: file: cf38d01b.  */
5740         if (debug_information[i].addr_base >= section->size)
5741           warn (_("Corrupt address base (%lx) found in debug section %u\n"),
5742                 (unsigned long) debug_information[i].addr_base, i);
5743         else
5744           debug_addr_info [count++] = debug_information + i;
5745       }
5746
5747   /* Add a sentinel to make iteration convenient.  */
5748   debug_addr_info [count] = (debug_info *) xmalloc (sizeof (debug_info));
5749   debug_addr_info [count]->addr_base = section->size;
5750   qsort (debug_addr_info, count, sizeof (debug_info *), comp_addr_base);
5751
5752   for (i = 0; i < count; i++)
5753     {
5754       unsigned int idx;
5755       unsigned int address_size = debug_addr_info [i]->pointer_size;
5756
5757       printf (_("  For compilation unit at offset 0x%s:\n"),
5758               dwarf_vmatoa ("x", debug_addr_info [i]->cu_offset));
5759
5760       printf (_("\tIndex\tAddress\n"));
5761       entry = section->start + debug_addr_info [i]->addr_base;
5762       end = section->start + debug_addr_info [i + 1]->addr_base;
5763       idx = 0;
5764       while (entry < end)
5765         {
5766           dwarf_vma base = byte_get (entry, address_size);
5767           printf (_("\t%d:\t"), idx);
5768           print_dwarf_vma (base, address_size);
5769           printf ("\n");
5770           entry += address_size;
5771           idx++;
5772         }
5773     }
5774   printf ("\n");
5775
5776   free (debug_addr_info);
5777   return 1;
5778 }
5779
5780 /* Display the .debug_str_offsets and .debug_str_offsets.dwo sections.  */
5781 static int
5782 display_debug_str_offsets (struct dwarf_section *section,
5783                            void *file ATTRIBUTE_UNUSED)
5784 {
5785   if (section->size == 0)
5786     {
5787       printf (_("\nThe %s section is empty.\n"), section->name);
5788       return 0;
5789     }
5790   /* TODO: Dump the contents.  This is made somewhat difficult by not knowing
5791      what the offset size is for this section.  */
5792   return 1;
5793 }
5794
5795 /* Each debug_information[x].range_lists[y] gets this representation for
5796    sorting purposes.  */
5797
5798 struct range_entry
5799 {
5800   /* The debug_information[x].range_lists[y] value.  */
5801   dwarf_vma ranges_offset;
5802
5803   /* Original debug_information to find parameters of the data.  */
5804   debug_info *debug_info_p;
5805 };
5806
5807 /* Sort struct range_entry in ascending order of its RANGES_OFFSET.  */
5808
5809 static int
5810 range_entry_compar (const void *ap, const void *bp)
5811 {
5812   const struct range_entry *a_re = (const struct range_entry *) ap;
5813   const struct range_entry *b_re = (const struct range_entry *) bp;
5814   const dwarf_vma a = a_re->ranges_offset;
5815   const dwarf_vma b = b_re->ranges_offset;
5816
5817   return (a > b) - (b > a);
5818 }
5819
5820 static void
5821 display_debug_ranges_list (unsigned char *start, unsigned char *finish,
5822                            unsigned int pointer_size, unsigned long offset,
5823                            unsigned long base_address)
5824 {
5825   while (start < finish)
5826     {
5827       dwarf_vma begin;
5828       dwarf_vma end;
5829
5830       SAFE_BYTE_GET_AND_INC (begin, start, pointer_size, finish);
5831       if (start >= finish)
5832         break;
5833       SAFE_SIGNED_BYTE_GET_AND_INC (end, start, pointer_size, finish);
5834
5835       printf ("    %8.8lx ", offset);
5836
5837       if (begin == 0 && end == 0)
5838         {
5839           printf (_("<End of list>\n"));
5840           break;
5841         }
5842
5843       /* Check base address specifiers.  */
5844       if (is_max_address (begin, pointer_size)
5845           && !is_max_address (end, pointer_size))
5846         {
5847           base_address = end;
5848           print_dwarf_vma (begin, pointer_size);
5849           print_dwarf_vma (end, pointer_size);
5850           printf ("(base address)\n");
5851           continue;
5852         }
5853
5854       print_dwarf_vma (begin + base_address, pointer_size);
5855       print_dwarf_vma (end + base_address, pointer_size);
5856
5857       if (begin == end)
5858         fputs (_("(start == end)"), stdout);
5859       else if (begin > end)
5860         fputs (_("(start > end)"), stdout);
5861
5862       putchar ('\n');
5863     }
5864 }
5865
5866 static void
5867 display_debug_rnglists_list (unsigned char *start, unsigned char *finish,
5868                              unsigned int pointer_size, unsigned long offset,
5869                              unsigned long base_address)
5870 {
5871   unsigned char *next = start;
5872
5873   while (1)
5874     {
5875       unsigned long off = offset + (start - next);
5876       enum dwarf_range_list_entry rlet;
5877       /* Initialize it due to a false compiler warning.  */
5878       dwarf_vma begin = -1, length, end = -1;
5879       unsigned int bytes_read;
5880
5881       if (start + 1 > finish)
5882         {
5883           warn (_("Range list starting at offset 0x%lx is not terminated.\n"),
5884                 offset);
5885           break;
5886         }
5887
5888       printf ("    %8.8lx ", off);
5889
5890       SAFE_BYTE_GET_AND_INC (rlet, start, 1, finish);
5891
5892       switch (rlet)
5893         {
5894         case DW_RLE_end_of_list:
5895           printf (_("<End of list>\n"));
5896           break;
5897         case DW_RLE_base_address:
5898           SAFE_BYTE_GET_AND_INC (base_address, start, pointer_size, finish);
5899           print_dwarf_vma (base_address, pointer_size);
5900           printf (_("(base address)\n"));
5901           break;
5902         case DW_RLE_start_length:
5903           SAFE_BYTE_GET_AND_INC (begin, start, pointer_size, finish);
5904           length = read_uleb128 (start, &bytes_read, finish);
5905           start += bytes_read;
5906           end = begin + length;
5907           break;
5908         case DW_RLE_offset_pair:
5909           begin = read_uleb128 (start, &bytes_read, finish);
5910           start += bytes_read;
5911           end = read_uleb128 (start, &bytes_read, finish);
5912           start += bytes_read;
5913           break;
5914         case DW_RLE_start_end:
5915           SAFE_BYTE_GET_AND_INC (begin, start, pointer_size, finish);
5916           SAFE_BYTE_GET_AND_INC (end, start, pointer_size, finish);
5917           break;
5918         default:
5919           error (_("Invalid range list entry type %d\n"), rlet);
5920           rlet = DW_RLE_end_of_list;
5921           break;
5922         }
5923       if (rlet == DW_RLE_end_of_list)
5924         break;
5925       if (rlet == DW_RLE_base_address)
5926         continue;
5927
5928       print_dwarf_vma (begin + base_address, pointer_size);
5929       print_dwarf_vma (end + base_address, pointer_size);
5930
5931       if (begin == end)
5932         fputs (_("(start == end)"), stdout);
5933       else if (begin > end)
5934         fputs (_("(start > end)"), stdout);
5935
5936       putchar ('\n');
5937     }
5938 }
5939
5940 static int
5941 display_debug_ranges (struct dwarf_section *section,
5942                       void *file ATTRIBUTE_UNUSED)
5943 {
5944   unsigned char *start = section->start;
5945   unsigned char *last_start = start;
5946   unsigned long bytes = section->size;
5947   unsigned char *section_begin = start;
5948   unsigned char *finish = start + bytes;
5949   unsigned int num_range_list, i;
5950   struct range_entry *range_entries, *range_entry_fill;
5951   int is_rnglists = strstr (section->name, "debug_rnglists") != NULL;
5952   /* Initialize it due to a false compiler warning.  */
5953   unsigned char address_size = 0;
5954
5955   if (bytes == 0)
5956     {
5957       printf (_("\nThe %s section is empty.\n"), section->name);
5958       return 0;
5959     }
5960
5961   if (is_rnglists)
5962     {
5963       dwarf_vma initial_length;
5964       unsigned int initial_length_size;
5965       unsigned char segment_selector_size;
5966       unsigned int offset_size, offset_entry_count;
5967       unsigned short version;
5968
5969       /* Get and check the length of the block.  */
5970       SAFE_BYTE_GET_AND_INC (initial_length, start, 4, finish);
5971
5972       if (initial_length == 0xffffffff)
5973         {
5974           /* This section is 64-bit DWARF 3.  */
5975           SAFE_BYTE_GET_AND_INC (initial_length, start, 8, finish);
5976           offset_size = 8;
5977           initial_length_size = 12;
5978         }
5979       else
5980         {
5981           offset_size = 4;
5982           initial_length_size = 4;
5983         }
5984
5985       if (initial_length + initial_length_size > section->size)
5986         {
5987           /* If the length field has a relocation against it, then we should
5988              not complain if it is inaccurate (and probably negative).
5989              It is copied from .debug_line handling code.  */
5990           if (reloc_at (section, (start - section->start) - offset_size))
5991             {
5992               initial_length = (finish - start) - initial_length_size;
5993             }
5994           else
5995             {
5996               warn (_("The length field (0x%lx) in the debug_rnglists header is wrong - the section is too small\n"),
5997                     (long) initial_length);
5998               return 0;
5999             }
6000         }
6001
6002       /* Get and check the version number.  */
6003       SAFE_BYTE_GET_AND_INC (version, start, 2, finish);
6004
6005       if (version != 5)
6006         {
6007           warn (_("Only DWARF version 5 debug_rnglists info "
6008                   "is currently supported.\n"));
6009           return 0;
6010         }
6011
6012       SAFE_BYTE_GET_AND_INC (address_size, start, 1, finish);
6013
6014       SAFE_BYTE_GET_AND_INC (segment_selector_size, start, 1, finish);
6015       if (segment_selector_size != 0)
6016         {
6017           warn (_("The %s section contains "
6018                   "unsupported segment selector size: %d.\n"),
6019                 section->name, segment_selector_size);
6020           return 0;
6021         }
6022
6023       SAFE_BYTE_GET_AND_INC (offset_entry_count, start, 4, finish);
6024       if (offset_entry_count != 0)
6025         {
6026           warn (_("The %s section contains "
6027                   "unsupported offset entry count: %u.\n"),
6028                 section->name, offset_entry_count);
6029           return 0;
6030         }
6031     }
6032
6033   if (load_debug_info (file) == 0)
6034     {
6035       warn (_("Unable to load/parse the .debug_info section, so cannot interpret the %s section.\n"),
6036             section->name);
6037       return 0;
6038     }
6039
6040   num_range_list = 0;
6041   for (i = 0; i < num_debug_info_entries; i++)
6042     num_range_list += debug_information [i].num_range_lists;
6043
6044   if (num_range_list == 0)
6045     {
6046       /* This can happen when the file was compiled with -gsplit-debug
6047          which removes references to range lists from the primary .o file.  */
6048       printf (_("No range lists in .debug_info section.\n"));
6049       return 1;
6050     }
6051
6052   range_entries = (struct range_entry *)
6053       xmalloc (sizeof (*range_entries) * num_range_list);
6054   range_entry_fill = range_entries;
6055
6056   for (i = 0; i < num_debug_info_entries; i++)
6057     {
6058       debug_info *debug_info_p = &debug_information[i];
6059       unsigned int j;
6060
6061       for (j = 0; j < debug_info_p->num_range_lists; j++)
6062         {
6063           range_entry_fill->ranges_offset = debug_info_p->range_lists[j];
6064           range_entry_fill->debug_info_p = debug_info_p;
6065           range_entry_fill++;
6066         }
6067     }
6068
6069   qsort (range_entries, num_range_list, sizeof (*range_entries),
6070          range_entry_compar);
6071
6072   if (dwarf_check != 0 && range_entries[0].ranges_offset != 0)
6073     warn (_("Range lists in %s section start at 0x%lx\n"),
6074           section->name, (unsigned long) range_entries[0].ranges_offset);
6075
6076   printf (_("Contents of the %s section:\n\n"), section->name);
6077   printf (_("    Offset   Begin    End\n"));
6078
6079   for (i = 0; i < num_range_list; i++)
6080     {
6081       struct range_entry *range_entry = &range_entries[i];
6082       debug_info *debug_info_p = range_entry->debug_info_p;
6083       unsigned int pointer_size;
6084       dwarf_vma offset;
6085       unsigned char *next;
6086       dwarf_vma base_address;
6087
6088       pointer_size = (is_rnglists ? address_size : debug_info_p->pointer_size);
6089       offset = range_entry->ranges_offset;
6090       next = section_begin + offset;
6091       base_address = debug_info_p->base_address;
6092
6093       /* PR 17512: file: 001-101485-0.001:0.1.  */
6094       if (pointer_size < 2 || pointer_size > 8)
6095         {
6096           warn (_("Corrupt pointer size (%d) in debug entry at offset %8.8lx\n"),
6097                 pointer_size, (unsigned long) offset);
6098           continue;
6099         }
6100
6101       if (dwarf_check != 0 && i > 0)
6102         {
6103           if (start < next)
6104             warn (_("There is a hole [0x%lx - 0x%lx] in %s section.\n"),
6105                   (unsigned long) (start - section_begin),
6106                   (unsigned long) (next - section_begin), section->name);
6107           else if (start > next)
6108             {
6109               if (next == last_start)
6110                 continue;
6111               warn (_("There is an overlap [0x%lx - 0x%lx] in %s section.\n"),
6112                     (unsigned long) (start - section_begin),
6113                     (unsigned long) (next - section_begin), section->name);
6114             }
6115         }
6116       start = next;
6117       last_start = next;
6118
6119       (is_rnglists ? display_debug_rnglists_list : display_debug_ranges_list)
6120         (start, finish, pointer_size, offset, base_address);
6121     }
6122   putchar ('\n');
6123
6124   free (range_entries);
6125
6126   return 1;
6127 }
6128
6129 typedef struct Frame_Chunk
6130 {
6131   struct Frame_Chunk *next;
6132   unsigned char *chunk_start;
6133   unsigned int ncols;
6134   /* DW_CFA_{undefined,same_value,offset,register,unreferenced}  */
6135   short int *col_type;
6136   int *col_offset;
6137   char *augmentation;
6138   unsigned int code_factor;
6139   int data_factor;
6140   dwarf_vma pc_begin;
6141   dwarf_vma pc_range;
6142   int cfa_reg;
6143   dwarf_vma cfa_offset;
6144   unsigned int ra;
6145   unsigned char fde_encoding;
6146   unsigned char cfa_exp;
6147   unsigned char ptr_size;
6148   unsigned char segment_size;
6149 }
6150 Frame_Chunk;
6151
6152 static const char *const *dwarf_regnames;
6153 static unsigned int dwarf_regnames_count;
6154
6155 /* A marker for a col_type that means this column was never referenced
6156    in the frame info.  */
6157 #define DW_CFA_unreferenced (-1)
6158
6159 /* Return 0 if no more space is needed, 1 if more space is needed,
6160    -1 for invalid reg.  */
6161
6162 static int
6163 frame_need_space (Frame_Chunk *fc, unsigned int reg)
6164 {
6165   unsigned int prev = fc->ncols;
6166
6167   if (reg < (unsigned int) fc->ncols)
6168     return 0;
6169
6170   if (dwarf_regnames_count
6171       && reg > dwarf_regnames_count)
6172     return -1;
6173
6174   fc->ncols = reg + 1;
6175   /* PR 17512: file: 10450-2643-0.004.
6176      If reg == -1 then this can happen...  */
6177   if (fc->ncols == 0)
6178     return -1;
6179
6180   /* PR 17512: file: 2844a11d.  */
6181   if (fc->ncols > 1024)
6182     {
6183       error (_("Unfeasibly large register number: %u\n"), reg);
6184       fc->ncols = 0;
6185       /* FIXME: 1024 is an arbitrary limit.  Increase it if
6186          we ever encounter a valid binary that exceeds it.  */
6187       return -1;
6188     }
6189
6190   fc->col_type = (short int *) xcrealloc (fc->col_type, fc->ncols,
6191                                           sizeof (short int));
6192   fc->col_offset = (int *) xcrealloc (fc->col_offset, fc->ncols, sizeof (int));
6193   /* PR 17512: file:002-10025-0.005.  */
6194   if (fc->col_type == NULL || fc->col_offset == NULL)
6195     {
6196       error (_("Out of memory allocating %u columns in dwarf frame arrays\n"),
6197              fc->ncols);
6198       fc->ncols = 0;
6199       return -1;
6200     }
6201
6202   while (prev < fc->ncols)
6203     {
6204       fc->col_type[prev] = DW_CFA_unreferenced;
6205       fc->col_offset[prev] = 0;
6206       prev++;
6207     }
6208   return 1;
6209 }
6210
6211 static const char *const dwarf_regnames_i386[] =
6212 {
6213   "eax", "ecx", "edx", "ebx",                     /* 0 - 3  */
6214   "esp", "ebp", "esi", "edi",                     /* 4 - 7  */
6215   "eip", "eflags", NULL,                          /* 8 - 10  */
6216   "st0", "st1", "st2", "st3",                     /* 11 - 14  */
6217   "st4", "st5", "st6", "st7",                     /* 15 - 18  */
6218   NULL, NULL,                                     /* 19 - 20  */
6219   "xmm0", "xmm1", "xmm2", "xmm3",                 /* 21 - 24  */
6220   "xmm4", "xmm5", "xmm6", "xmm7",                 /* 25 - 28  */
6221   "mm0", "mm1", "mm2", "mm3",                     /* 29 - 32  */
6222   "mm4", "mm5", "mm6", "mm7",                     /* 33 - 36  */
6223   "fcw", "fsw", "mxcsr",                          /* 37 - 39  */
6224   "es", "cs", "ss", "ds", "fs", "gs", NULL, NULL, /* 40 - 47  */
6225   "tr", "ldtr",                                   /* 48 - 49  */
6226   NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 50 - 57  */
6227   NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 58 - 65  */
6228   NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 66 - 73  */
6229   NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 74 - 81  */
6230   NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 82 - 89  */
6231   NULL, NULL, NULL,                               /* 90 - 92  */
6232   "k0", "k1", "k2", "k3", "k4", "k5", "k6", "k7"  /* 93 - 100  */
6233 };
6234
6235 static const char *const dwarf_regnames_iamcu[] =
6236 {
6237   "eax", "ecx", "edx", "ebx",                     /* 0 - 3  */
6238   "esp", "ebp", "esi", "edi",                     /* 4 - 7  */
6239   "eip", "eflags", NULL,                          /* 8 - 10  */
6240   NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 11 - 18  */
6241   NULL, NULL,                                     /* 19 - 20  */
6242   NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 21 - 28  */
6243   NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 29 - 36  */
6244   NULL, NULL, NULL,                               /* 37 - 39  */
6245   "es", "cs", "ss", "ds", "fs", "gs", NULL, NULL, /* 40 - 47  */
6246   "tr", "ldtr",                                   /* 48 - 49  */
6247   NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 50 - 57  */
6248   NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 58 - 65  */
6249   NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 66 - 73  */
6250   NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 74 - 81  */
6251   NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 82 - 89  */
6252   NULL, NULL, NULL,                               /* 90 - 92  */
6253   NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL  /* 93 - 100  */
6254 };
6255
6256 void
6257 init_dwarf_regnames_i386 (void)
6258 {
6259   dwarf_regnames = dwarf_regnames_i386;
6260   dwarf_regnames_count = ARRAY_SIZE (dwarf_regnames_i386);
6261 }
6262
6263 void
6264 init_dwarf_regnames_iamcu (void)
6265 {
6266   dwarf_regnames = dwarf_regnames_iamcu;
6267   dwarf_regnames_count = ARRAY_SIZE (dwarf_regnames_iamcu);
6268 }
6269
6270 static const char *const dwarf_regnames_x86_64[] =
6271 {
6272   "rax", "rdx", "rcx", "rbx",
6273   "rsi", "rdi", "rbp", "rsp",
6274   "r8",  "r9",  "r10", "r11",
6275   "r12", "r13", "r14", "r15",
6276   "rip",
6277   "xmm0",  "xmm1",  "xmm2",  "xmm3",
6278   "xmm4",  "xmm5",  "xmm6",  "xmm7",
6279   "xmm8",  "xmm9",  "xmm10", "xmm11",
6280   "xmm12", "xmm13", "xmm14", "xmm15",
6281   "st0", "st1", "st2", "st3",
6282   "st4", "st5", "st6", "st7",
6283   "mm0", "mm1", "mm2", "mm3",
6284   "mm4", "mm5", "mm6", "mm7",
6285   "rflags",
6286   "es", "cs", "ss", "ds", "fs", "gs", NULL, NULL,
6287   "fs.base", "gs.base", NULL, NULL,
6288   "tr", "ldtr",
6289   "mxcsr", "fcw", "fsw",
6290   "xmm16",  "xmm17",  "xmm18",  "xmm19",
6291   "xmm20",  "xmm21",  "xmm22",  "xmm23",
6292   "xmm24",  "xmm25",  "xmm26",  "xmm27",
6293   "xmm28",  "xmm29",  "xmm30",  "xmm31",
6294   NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 83 - 90  */
6295   NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 91 - 98  */
6296   NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 99 - 106  */
6297   NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 107 - 114  */
6298   NULL, NULL, NULL,                               /* 115 - 117  */
6299   "k0", "k1", "k2", "k3", "k4", "k5", "k6", "k7"
6300 };
6301
6302 void
6303 init_dwarf_regnames_x86_64 (void)
6304 {
6305   dwarf_regnames = dwarf_regnames_x86_64;
6306   dwarf_regnames_count = ARRAY_SIZE (dwarf_regnames_x86_64);
6307 }
6308
6309 static const char *const dwarf_regnames_aarch64[] =
6310 {
6311    "x0",  "x1",  "x2",  "x3",  "x4",  "x5",  "x6",  "x7",
6312    "x8",  "x9", "x10", "x11", "x12", "x13", "x14", "x15",
6313   "x16", "x17", "x18", "x19", "x20", "x21", "x22", "x23",
6314   "x24", "x25", "x26", "x27", "x28", "x29", "x30", "sp",
6315    NULL, "elr",  NULL,  NULL,  NULL,  NULL,  NULL,  NULL,
6316    NULL,  NULL,  NULL,  NULL,  NULL,  NULL,  NULL,  NULL,
6317    NULL,  NULL,  NULL,  NULL,  NULL,  NULL,  NULL,  NULL,
6318    NULL,  NULL,  NULL,  NULL,  NULL,  NULL,  NULL,  NULL,
6319    "v0",  "v1",  "v2",  "v3",  "v4",  "v5",  "v6",  "v7",
6320    "v8",  "v9", "v10", "v11", "v12", "v13", "v14", "v15",
6321   "v16", "v17", "v18", "v19", "v20", "v21", "v22", "v23",
6322   "v24", "v25", "v26", "v27", "v28", "v29", "v30", "v31",
6323 };
6324
6325 void
6326 init_dwarf_regnames_aarch64 (void)
6327 {
6328   dwarf_regnames = dwarf_regnames_aarch64;
6329   dwarf_regnames_count = ARRAY_SIZE (dwarf_regnames_aarch64);
6330 }
6331
6332 static const char *const dwarf_regnames_s390[] =
6333 {
6334   /* Avoid saying "r5 (r5)", so omit the names of r0-r15.  */
6335   NULL,  NULL,  NULL,  NULL,  NULL,  NULL,  NULL,  NULL,
6336   NULL,  NULL,  NULL,  NULL,  NULL,  NULL,  NULL,  NULL,
6337   "f0",  "f2",  "f4",  "f6",  "f1",  "f3",  "f5",  "f7",
6338   "f8",  "f10", "f12", "f14", "f9",  "f11", "f13", "f15",
6339   "cr0", "cr1", "cr2", "cr3", "cr4", "cr5", "cr6", "cr7",
6340   "cr8", "cr9", "cr10", "cr11", "cr12", "cr13", "cr14", "cr15",
6341   "a0",  "a1",  "a2",  "a3",  "a4",  "a5",  "a6",  "a7",
6342   "a8",  "a9",  "a10", "a11", "a12", "a13", "a14", "a15",
6343   "pswm", "pswa",
6344   NULL, NULL,
6345   "v16", "v18", "v20", "v22", "v17", "v19", "v21", "v23",
6346   "v24", "v26", "v28", "v30", "v25", "v27", "v29", "v31",
6347 };
6348
6349 void
6350 init_dwarf_regnames_s390 (void)
6351 {
6352   dwarf_regnames = dwarf_regnames_s390;
6353   dwarf_regnames_count = ARRAY_SIZE (dwarf_regnames_s390);
6354 }
6355
6356 void
6357 init_dwarf_regnames (unsigned int e_machine)
6358 {
6359   switch (e_machine)
6360     {
6361     case EM_386:
6362       init_dwarf_regnames_i386 ();
6363       break;
6364
6365     case EM_IAMCU:
6366       init_dwarf_regnames_iamcu ();
6367       break;
6368
6369     case EM_X86_64:
6370     case EM_L1OM:
6371     case EM_K1OM:
6372       init_dwarf_regnames_x86_64 ();
6373       break;
6374
6375     case EM_AARCH64:
6376       init_dwarf_regnames_aarch64 ();
6377       break;
6378
6379     case EM_S390:
6380       init_dwarf_regnames_s390 ();
6381       break;
6382
6383     default:
6384       break;
6385     }
6386 }
6387
6388 static const char *
6389 regname (unsigned int regno, int row)
6390 {
6391   static char reg[64];
6392   if (dwarf_regnames
6393       && regno < dwarf_regnames_count
6394       && dwarf_regnames [regno] != NULL)
6395     {
6396       if (row)
6397         return dwarf_regnames [regno];
6398       snprintf (reg, sizeof (reg), "r%d (%s)", regno,
6399                 dwarf_regnames [regno]);
6400     }
6401   else
6402     snprintf (reg, sizeof (reg), "r%d", regno);
6403   return reg;
6404 }
6405
6406 static void
6407 frame_display_row (Frame_Chunk *fc, int *need_col_headers, unsigned int *max_regs)
6408 {
6409   unsigned int r;
6410   char tmp[100];
6411
6412   if (*max_regs != fc->ncols)
6413     *max_regs = fc->ncols;
6414
6415   if (*need_col_headers)
6416     {
6417       static const char *sloc = "   LOC";
6418
6419       *need_col_headers = 0;
6420
6421       printf ("%-*s CFA      ", eh_addr_size * 2, sloc);
6422
6423       for (r = 0; r < *max_regs; r++)
6424         if (fc->col_type[r] != DW_CFA_unreferenced)
6425           {
6426             if (r == fc->ra)
6427               printf ("ra    ");
6428             else
6429               printf ("%-5s ", regname (r, 1));
6430           }
6431
6432       printf ("\n");
6433     }
6434
6435   print_dwarf_vma (fc->pc_begin, eh_addr_size);
6436   if (fc->cfa_exp)
6437     strcpy (tmp, "exp");
6438   else
6439     sprintf (tmp, "%s%+d", regname (fc->cfa_reg, 1), (int) fc->cfa_offset);
6440   printf ("%-8s ", tmp);
6441
6442   for (r = 0; r < fc->ncols; r++)
6443     {
6444       if (fc->col_type[r] != DW_CFA_unreferenced)
6445         {
6446           switch (fc->col_type[r])
6447             {
6448             case DW_CFA_undefined:
6449               strcpy (tmp, "u");
6450               break;
6451             case DW_CFA_same_value:
6452               strcpy (tmp, "s");
6453               break;
6454             case DW_CFA_offset:
6455               sprintf (tmp, "c%+d", fc->col_offset[r]);
6456               break;
6457             case DW_CFA_val_offset:
6458               sprintf (tmp, "v%+d", fc->col_offset[r]);
6459               break;
6460             case DW_CFA_register:
6461               sprintf (tmp, "%s", regname (fc->col_offset[r], 0));
6462               break;
6463             case DW_CFA_expression:
6464               strcpy (tmp, "exp");
6465               break;
6466             case DW_CFA_val_expression:
6467               strcpy (tmp, "vexp");
6468               break;
6469             default:
6470               strcpy (tmp, "n/a");
6471               break;
6472             }
6473           printf ("%-5s ", tmp);
6474         }
6475     }
6476   printf ("\n");
6477 }
6478
6479 #define GET(VAR, N)     SAFE_BYTE_GET_AND_INC (VAR, start, N, end)
6480 #define LEB()   read_uleb128 (start, & length_return, end); start += length_return
6481 #define SLEB()  read_sleb128 (start, & length_return, end); start += length_return
6482
6483 static unsigned char *
6484 read_cie (unsigned char *start, unsigned char *end,
6485           Frame_Chunk **p_cie, int *p_version,
6486           unsigned long *p_aug_len, unsigned char **p_aug)
6487 {
6488   int version;
6489   Frame_Chunk *fc;
6490   unsigned int length_return;
6491   unsigned char *augmentation_data = NULL;
6492   unsigned long augmentation_data_len = 0;
6493
6494   * p_cie = NULL;
6495   /* PR 17512: file: 001-228113-0.004.  */
6496   if (start >= end)
6497     return end;
6498
6499   fc = (Frame_Chunk *) xmalloc (sizeof (Frame_Chunk));
6500   memset (fc, 0, sizeof (Frame_Chunk));
6501
6502   fc->col_type = (short int *) xmalloc (sizeof (short int));
6503   fc->col_offset = (int *) xmalloc (sizeof (int));
6504
6505   version = *start++;
6506
6507   fc->augmentation = (char *) start;
6508   /* PR 17512: file: 001-228113-0.004.
6509      Skip past augmentation name, but avoid running off the end of the data.  */
6510   while (start < end)
6511     if (* start ++ == '\0')
6512       break;
6513   if (start == end)
6514     {
6515       warn (_("No terminator for augmentation name\n"));
6516       return start;
6517     }
6518
6519   if (strcmp (fc->augmentation, "eh") == 0)
6520     start += eh_addr_size;
6521
6522   if (version >= 4)
6523     {
6524       GET (fc->ptr_size, 1);
6525       if (fc->ptr_size < 1 || fc->ptr_size > 8)
6526         {
6527           warn (_("Invalid pointer size (%d) in CIE data\n"), fc->ptr_size);
6528           return end;
6529         }
6530
6531       GET (fc->segment_size, 1);
6532       /* PR 17512: file: e99d2804.  */
6533       if (fc->segment_size > 8 || fc->segment_size + fc->ptr_size > 8)
6534         {
6535           warn (_("Invalid segment size (%d) in CIE data\n"), fc->segment_size);
6536           return end;
6537         }
6538
6539       eh_addr_size = fc->ptr_size;
6540     }
6541   else
6542     {
6543       fc->ptr_size = eh_addr_size;
6544       fc->segment_size = 0;
6545     }
6546   fc->code_factor = LEB ();
6547   fc->data_factor = SLEB ();
6548   if (version == 1)
6549     {
6550       GET (fc->ra, 1);
6551     }
6552   else
6553     {
6554       fc->ra = LEB ();
6555     }
6556
6557   if (fc->augmentation[0] == 'z')
6558     {
6559       augmentation_data_len = LEB ();
6560       augmentation_data = start;
6561       start += augmentation_data_len;
6562       /* PR 17512: file: 11042-2589-0.004.  */
6563       if (start > end)
6564         {
6565           warn (_("Augmentation data too long: 0x%lx\n"), augmentation_data_len);
6566           return end;
6567         }
6568     }
6569
6570   if (augmentation_data_len)
6571     {
6572       unsigned char *p;
6573       unsigned char *q;
6574       unsigned char *qend;
6575
6576       p = (unsigned char *) fc->augmentation + 1;
6577       q = augmentation_data;
6578       qend = q + augmentation_data_len;
6579
6580       /* PR 17531: file: 015adfaa.  */
6581       if (qend < q)
6582         {
6583           warn (_("Negative augmentation data length: 0x%lx"), augmentation_data_len);
6584           augmentation_data_len = 0;
6585         }
6586
6587       while (p < end && q < augmentation_data + augmentation_data_len)
6588         {
6589           if (*p == 'L')
6590             q++;
6591           else if (*p == 'P')
6592             q += 1 + size_of_encoded_value (*q);
6593           else if (*p == 'R')
6594             fc->fde_encoding = *q++;
6595           else if (*p == 'S')
6596             ;
6597           else
6598             break;
6599           p++;
6600         }
6601       /* Note - it is OK if this loop terminates with q < qend.
6602          Padding may have been inserted to align the end of the CIE.  */
6603     }
6604
6605   *p_cie = fc;
6606   if (p_version)
6607     *p_version = version;
6608   if (p_aug_len)
6609     {
6610       *p_aug_len = augmentation_data_len;
6611       *p_aug = augmentation_data;
6612     }
6613   return start;
6614 }
6615
6616 static int
6617 display_debug_frames (struct dwarf_section *section,
6618                       void *file ATTRIBUTE_UNUSED)
6619 {
6620   unsigned char *start = section->start;
6621   unsigned char *end = start + section->size;
6622   unsigned char *section_start = start;
6623   Frame_Chunk *chunks = 0, *forward_refs = 0;
6624   Frame_Chunk *remembered_state = 0;
6625   Frame_Chunk *rs;
6626   int is_eh = strcmp (section->name, ".eh_frame") == 0;
6627   unsigned int length_return;
6628   unsigned int max_regs = 0;
6629   const char *bad_reg = _("bad register: ");
6630   unsigned int saved_eh_addr_size = eh_addr_size;
6631
6632   printf (_("Contents of the %s section:\n"), section->name);
6633
6634   while (start < end)
6635     {
6636       unsigned char *saved_start;
6637       unsigned char *block_end;
6638       dwarf_vma length;
6639       dwarf_vma cie_id;
6640       Frame_Chunk *fc;
6641       Frame_Chunk *cie;
6642       int need_col_headers = 1;
6643       unsigned char *augmentation_data = NULL;
6644       unsigned long augmentation_data_len = 0;
6645       unsigned int encoded_ptr_size = saved_eh_addr_size;
6646       unsigned int offset_size;
6647       unsigned int initial_length_size;
6648       bfd_boolean all_nops;
6649
6650       saved_start = start;
6651
6652       SAFE_BYTE_GET_AND_INC (length, start, 4, end);
6653
6654       if (length == 0)
6655         {
6656           printf ("\n%08lx ZERO terminator\n\n",
6657                     (unsigned long)(saved_start - section_start));
6658           /* Skip any zero terminators that directly follow.
6659              A corrupt section size could have loaded a whole
6660              slew of zero filled memory bytes.  eg
6661              PR 17512: file: 070-19381-0.004.  */
6662           while (start < end && * start == 0)
6663             ++ start;
6664           continue;
6665         }
6666
6667       if (length == 0xffffffff)
6668         {
6669           SAFE_BYTE_GET_AND_INC (length, start, 8, end);
6670           offset_size = 8;
6671           initial_length_size = 12;
6672         }
6673       else
6674         {
6675           offset_size = 4;
6676           initial_length_size = 4;
6677         }
6678
6679       block_end = saved_start + length + initial_length_size;
6680       if (block_end > end || block_end < start)
6681         {
6682           warn ("Invalid length 0x%s in FDE at %#08lx\n",
6683                 dwarf_vmatoa_1 (NULL, length, offset_size),
6684                 (unsigned long) (saved_start - section_start));
6685           block_end = end;
6686         }
6687
6688       SAFE_BYTE_GET_AND_INC (cie_id, start, offset_size, end);
6689
6690       if (is_eh ? (cie_id == 0) : ((offset_size == 4 && cie_id == DW_CIE_ID)
6691                                    || (offset_size == 8 && cie_id == DW64_CIE_ID)))
6692         {
6693           int version;
6694           unsigned int mreg;
6695
6696           start = read_cie (start, end, &cie, &version,
6697                             &augmentation_data_len, &augmentation_data);
6698           /* PR 17512: file: 027-135133-0.005.  */
6699           if (cie == NULL)
6700             break;
6701
6702           fc = cie;
6703           fc->next = chunks;
6704           chunks = fc;
6705           fc->chunk_start = saved_start;
6706           mreg = max_regs > 0 ? max_regs - 1 : 0;
6707           if (mreg < fc->ra)
6708             mreg = fc->ra;
6709           if (frame_need_space (fc, mreg) < 0)
6710             break;
6711           if (fc->fde_encoding)
6712             encoded_ptr_size = size_of_encoded_value (fc->fde_encoding);
6713
6714           printf ("\n%08lx ", (unsigned long) (saved_start - section_start));
6715           print_dwarf_vma (length, fc->ptr_size);
6716           print_dwarf_vma (cie_id, offset_size);
6717
6718           if (do_debug_frames_interp)
6719             {
6720               printf ("CIE \"%s\" cf=%d df=%d ra=%d\n", fc->augmentation,
6721                       fc->code_factor, fc->data_factor, fc->ra);
6722             }
6723           else
6724             {
6725               printf ("CIE\n");
6726               printf ("  Version:               %d\n", version);
6727               printf ("  Augmentation:          \"%s\"\n", fc->augmentation);
6728               if (version >= 4)
6729                 {
6730                   printf ("  Pointer Size:          %u\n", fc->ptr_size);
6731                   printf ("  Segment Size:          %u\n", fc->segment_size);
6732                 }
6733               printf ("  Code alignment factor: %u\n", fc->code_factor);
6734               printf ("  Data alignment factor: %d\n", fc->data_factor);
6735               printf ("  Return address column: %d\n", fc->ra);
6736
6737               if (augmentation_data_len)
6738                 {
6739                   unsigned long i;
6740
6741                   printf ("  Augmentation data:    ");
6742                   for (i = 0; i < augmentation_data_len; ++i)
6743                     /* FIXME: If do_wide is FALSE, then we should
6744                        add carriage returns at 80 columns...  */
6745                     printf (" %02x", augmentation_data[i]);
6746                   putchar ('\n');
6747                 }
6748               putchar ('\n');
6749             }
6750         }
6751       else
6752         {
6753           unsigned char *look_for;
6754           static Frame_Chunk fde_fc;
6755           unsigned long segment_selector;
6756
6757           if (is_eh)
6758             {
6759               dwarf_vma sign = (dwarf_vma) 1 << (offset_size * 8 - 1);
6760               look_for = start - 4 - ((cie_id ^ sign) - sign);
6761             }
6762           else
6763             look_for = section_start + cie_id;
6764
6765           if (look_for <= saved_start)
6766             {
6767               for (cie = chunks; cie ; cie = cie->next)
6768                 if (cie->chunk_start == look_for)
6769                   break;
6770             }
6771           else
6772             {
6773               for (cie = forward_refs; cie ; cie = cie->next)
6774                 if (cie->chunk_start == look_for)
6775                   break;
6776               if (!cie)
6777                 {
6778                   unsigned int off_size;
6779                   unsigned char *cie_scan;
6780
6781                   cie_scan = look_for;
6782                   off_size = 4;
6783                   SAFE_BYTE_GET_AND_INC (length, cie_scan, 4, end);
6784                   if (length == 0xffffffff)
6785                     {
6786                       SAFE_BYTE_GET_AND_INC (length, cie_scan, 8, end);
6787                       off_size = 8;
6788                     }
6789                   if (length != 0)
6790                     {
6791                       dwarf_vma c_id;
6792
6793                       SAFE_BYTE_GET_AND_INC (c_id, cie_scan, off_size, end);
6794                       if (is_eh
6795                           ? c_id == 0
6796                           : ((off_size == 4 && c_id == DW_CIE_ID)
6797                              || (off_size == 8 && c_id == DW64_CIE_ID)))
6798                         {
6799                           int version;
6800                           unsigned int mreg;
6801
6802                           read_cie (cie_scan, end, &cie, &version,
6803                                     &augmentation_data_len, &augmentation_data);
6804                           /* PR 17512: file: 3450-2098-0.004.  */
6805                           if (cie == NULL)
6806                             {
6807                               warn (_("Failed to read CIE information\n"));
6808                               break;
6809                             }
6810                           cie->next = forward_refs;
6811                           forward_refs = cie;
6812                           cie->chunk_start = look_for;
6813                           mreg = max_regs > 0 ? max_regs - 1 : 0;
6814                           if (mreg < cie->ra)
6815                             mreg = cie->ra;
6816                           if (frame_need_space (cie, mreg) < 0)
6817                             {
6818                               warn (_("Invalid max register\n"));
6819                               break;
6820                             }
6821                           if (cie->fde_encoding)
6822                             encoded_ptr_size
6823                               = size_of_encoded_value (cie->fde_encoding);
6824                         }
6825                     }
6826                 }
6827             }
6828
6829           fc = &fde_fc;
6830           memset (fc, 0, sizeof (Frame_Chunk));
6831
6832           if (!cie)
6833             {
6834               warn ("Invalid CIE pointer 0x%s in FDE at %#08lx\n",
6835                     dwarf_vmatoa_1 (NULL, cie_id, offset_size),
6836                     (unsigned long) (saved_start - section_start));
6837               fc->ncols = 0;
6838               fc->col_type = (short int *) xmalloc (sizeof (short int));
6839               fc->col_offset = (int *) xmalloc (sizeof (int));
6840               if (frame_need_space (fc, max_regs > 0 ? max_regs - 1 : 0) < 0)
6841                 {
6842                   warn (_("Invalid max register\n"));
6843                   break;
6844                 }
6845               cie = fc;
6846               fc->augmentation = "";
6847               fc->fde_encoding = 0;
6848               fc->ptr_size = eh_addr_size;
6849               fc->segment_size = 0;
6850             }
6851           else
6852             {
6853               fc->ncols = cie->ncols;
6854               fc->col_type = (short int *) xcmalloc (fc->ncols, sizeof (short int));
6855               fc->col_offset =  (int *) xcmalloc (fc->ncols, sizeof (int));
6856               memcpy (fc->col_type, cie->col_type, fc->ncols * sizeof (short int));
6857               memcpy (fc->col_offset, cie->col_offset, fc->ncols * sizeof (int));
6858               fc->augmentation = cie->augmentation;
6859               fc->ptr_size = cie->ptr_size;
6860               eh_addr_size = cie->ptr_size;
6861               fc->segment_size = cie->segment_size;
6862               fc->code_factor = cie->code_factor;
6863               fc->data_factor = cie->data_factor;
6864               fc->cfa_reg = cie->cfa_reg;
6865               fc->cfa_offset = cie->cfa_offset;
6866               fc->ra = cie->ra;
6867               if (frame_need_space (fc, max_regs > 0 ? max_regs - 1: 0) < 0)
6868                 {
6869                   warn (_("Invalid max register\n"));
6870                   break;
6871                 }
6872               fc->fde_encoding = cie->fde_encoding;
6873             }
6874
6875           if (fc->fde_encoding)
6876             encoded_ptr_size = size_of_encoded_value (fc->fde_encoding);
6877
6878           segment_selector = 0;
6879           if (fc->segment_size)
6880             {
6881               if (fc->segment_size > sizeof (segment_selector))
6882                 {
6883                   /* PR 17512: file: 9e196b3e.  */
6884                   warn (_("Probably corrupt segment size: %d - using 4 instead\n"), fc->segment_size);
6885                   fc->segment_size = 4;
6886                 }
6887               SAFE_BYTE_GET_AND_INC (segment_selector, start, fc->segment_size, end);
6888             }
6889
6890           fc->pc_begin = get_encoded_value (&start, fc->fde_encoding, section, end);
6891
6892           /* FIXME: It appears that sometimes the final pc_range value is
6893              encoded in less than encoded_ptr_size bytes.  See the x86_64
6894              run of the "objcopy on compressed debug sections" test for an
6895              example of this.  */
6896           SAFE_BYTE_GET_AND_INC (fc->pc_range, start, encoded_ptr_size, end);
6897
6898           if (cie->augmentation[0] == 'z')
6899             {
6900               augmentation_data_len = LEB ();
6901               augmentation_data = start;
6902               start += augmentation_data_len;
6903               /* PR 17512: file: 722-8446-0.004.  */
6904               if (start >= end || ((signed long) augmentation_data_len) < 0)
6905                 {
6906                   warn (_("Corrupt augmentation data length: %lx\n"),
6907                         augmentation_data_len);
6908                   start = end;
6909                   augmentation_data = NULL;
6910                   augmentation_data_len = 0;
6911                 }
6912             }
6913
6914           printf ("\n%08lx %s %s FDE cie=%08lx pc=",
6915                   (unsigned long)(saved_start - section_start),
6916                   dwarf_vmatoa_1 (NULL, length, fc->ptr_size),
6917                   dwarf_vmatoa_1 (NULL, cie_id, offset_size),
6918                   (unsigned long)(cie->chunk_start - section_start));
6919
6920           if (fc->segment_size)
6921             printf ("%04lx:", segment_selector);
6922
6923           printf ("%s..%s\n",
6924                   dwarf_vmatoa_1 (NULL, fc->pc_begin, fc->ptr_size),
6925                   dwarf_vmatoa_1 (NULL, fc->pc_begin + fc->pc_range, fc->ptr_size));
6926
6927           if (! do_debug_frames_interp && augmentation_data_len)
6928             {
6929               unsigned long i;
6930
6931               printf ("  Augmentation data:    ");
6932               for (i = 0; i < augmentation_data_len; ++i)
6933                 printf (" %02x", augmentation_data[i]);
6934               putchar ('\n');
6935               putchar ('\n');
6936             }
6937         }
6938
6939       /* At this point, fc is the current chunk, cie (if any) is set, and
6940          we're about to interpret instructions for the chunk.  */
6941       /* ??? At present we need to do this always, since this sizes the
6942          fc->col_type and fc->col_offset arrays, which we write into always.
6943          We should probably split the interpreted and non-interpreted bits
6944          into two different routines, since there's so much that doesn't
6945          really overlap between them.  */
6946       if (1 || do_debug_frames_interp)
6947         {
6948           /* Start by making a pass over the chunk, allocating storage
6949              and taking note of what registers are used.  */
6950           unsigned char *tmp = start;
6951
6952           while (start < block_end)
6953             {
6954               unsigned int reg, op, opa;
6955               unsigned long temp;
6956               unsigned char * new_start;
6957
6958               op = *start++;
6959               opa = op & 0x3f;
6960               if (op & 0xc0)
6961                 op &= 0xc0;
6962
6963               /* Warning: if you add any more cases to this switch, be
6964                  sure to add them to the corresponding switch below.  */
6965               switch (op)
6966                 {
6967                 case DW_CFA_advance_loc:
6968                   break;
6969                 case DW_CFA_offset:
6970                   LEB ();
6971                   if (frame_need_space (fc, opa) >= 0)
6972                     fc->col_type[opa] = DW_CFA_undefined;
6973                   break;
6974                 case DW_CFA_restore:
6975                   if (frame_need_space (fc, opa) >= 0)
6976                     fc->col_type[opa] = DW_CFA_undefined;
6977                   break;
6978                 case DW_CFA_set_loc:
6979                   start += encoded_ptr_size;
6980                   break;
6981                 case DW_CFA_advance_loc1:
6982                   start += 1;
6983                   break;
6984                 case DW_CFA_advance_loc2:
6985                   start += 2;
6986                   break;
6987                 case DW_CFA_advance_loc4:
6988                   start += 4;
6989                   break;
6990                 case DW_CFA_offset_extended:
6991                 case DW_CFA_val_offset:
6992                   reg = LEB (); LEB ();
6993                   if (frame_need_space (fc, reg) >= 0)
6994                     fc->col_type[reg] = DW_CFA_undefined;
6995                   break;
6996                 case DW_CFA_restore_extended:
6997                   reg = LEB ();
6998                   if (frame_need_space (fc, reg) >= 0)
6999                     fc->col_type[reg] = DW_CFA_undefined;
7000                   break;
7001                 case DW_CFA_undefined:
7002                   reg = LEB ();
7003                   if (frame_need_space (fc, reg) >= 0)
7004                     fc->col_type[reg] = DW_CFA_undefined;
7005                   break;
7006                 case DW_CFA_same_value:
7007                   reg = LEB ();
7008                   if (frame_need_space (fc, reg) >= 0)
7009                     fc->col_type[reg] = DW_CFA_undefined;
7010                   break;
7011                 case DW_CFA_register:
7012                   reg = LEB (); LEB ();
7013                   if (frame_need_space (fc, reg) >= 0)
7014                     fc->col_type[reg] = DW_CFA_undefined;
7015                   break;
7016                 case DW_CFA_def_cfa:
7017                   LEB (); LEB ();
7018                   break;
7019                 case DW_CFA_def_cfa_register:
7020                   LEB ();
7021                   break;
7022                 case DW_CFA_def_cfa_offset:
7023                   LEB ();
7024                   break;
7025                 case DW_CFA_def_cfa_expression:
7026                   temp = LEB ();
7027                   new_start = start + temp;
7028                   if (new_start < start)
7029                     {
7030                       warn (_("Corrupt CFA_def expression value: %lu\n"), temp);
7031                       start = block_end;
7032                     }
7033                   else
7034                     start = new_start;
7035                   break;
7036                 case DW_CFA_expression:
7037                 case DW_CFA_val_expression:
7038                   reg = LEB ();
7039                   temp = LEB ();
7040                   new_start = start + temp;
7041                   if (new_start < start)
7042                     {
7043                       /* PR 17512: file:306-192417-0.005.  */
7044                       warn (_("Corrupt CFA expression value: %lu\n"), temp);
7045                       start = block_end;
7046                     }
7047                   else
7048                     start = new_start;
7049                   if (frame_need_space (fc, reg) >= 0)
7050                     fc->col_type[reg] = DW_CFA_undefined;
7051                   break;
7052                 case DW_CFA_offset_extended_sf:
7053                 case DW_CFA_val_offset_sf:
7054                   reg = LEB (); SLEB ();
7055                   if (frame_need_space (fc, reg) >= 0)
7056                     fc->col_type[reg] = DW_CFA_undefined;
7057                   break;
7058                 case DW_CFA_def_cfa_sf:
7059                   LEB (); SLEB ();
7060                   break;
7061                 case DW_CFA_def_cfa_offset_sf:
7062                   SLEB ();
7063                   break;
7064                 case DW_CFA_MIPS_advance_loc8:
7065                   start += 8;
7066                   break;
7067                 case DW_CFA_GNU_args_size:
7068                   LEB ();
7069                   break;
7070                 case DW_CFA_GNU_negative_offset_extended:
7071                   reg = LEB (); LEB ();
7072                   if (frame_need_space (fc, reg) >= 0)
7073                     fc->col_type[reg] = DW_CFA_undefined;
7074                   break;
7075                 default:
7076                   break;
7077                 }
7078             }
7079           start = tmp;
7080         }
7081
7082       all_nops = TRUE;
7083
7084       /* Now we know what registers are used, make a second pass over
7085          the chunk, this time actually printing out the info.  */
7086
7087       while (start < block_end)
7088         {
7089           unsigned char * tmp;
7090           unsigned op, opa;
7091           unsigned long ul, reg, roffs;
7092           dwarf_vma l;
7093           dwarf_vma ofs;
7094           dwarf_vma vma;
7095           const char *reg_prefix = "";
7096
7097           op = *start++;
7098           opa = op & 0x3f;
7099           if (op & 0xc0)
7100             op &= 0xc0;
7101
7102           /* Make a note if something other than DW_CFA_nop happens.  */
7103           if (op != DW_CFA_nop)
7104             all_nops = FALSE;
7105
7106           /* Warning: if you add any more cases to this switch, be
7107              sure to add them to the corresponding switch above.  */
7108           switch (op)
7109             {
7110             case DW_CFA_advance_loc:
7111               if (do_debug_frames_interp)
7112                 frame_display_row (fc, &need_col_headers, &max_regs);
7113               else
7114                 printf ("  DW_CFA_advance_loc: %d to %s\n",
7115                         opa * fc->code_factor,
7116                         dwarf_vmatoa_1 (NULL,
7117                                         fc->pc_begin + opa * fc->code_factor,
7118                                         fc->ptr_size));
7119               fc->pc_begin += opa * fc->code_factor;
7120               break;
7121
7122             case DW_CFA_offset:
7123               roffs = LEB ();
7124               if (opa >= (unsigned int) fc->ncols)
7125                 reg_prefix = bad_reg;
7126               if (! do_debug_frames_interp || *reg_prefix != '\0')
7127                 printf ("  DW_CFA_offset: %s%s at cfa%+ld\n",
7128                         reg_prefix, regname (opa, 0),
7129                         roffs * fc->data_factor);
7130               if (*reg_prefix == '\0')
7131                 {
7132                   fc->col_type[opa] = DW_CFA_offset;
7133                   fc->col_offset[opa] = roffs * fc->data_factor;
7134                 }
7135               break;
7136
7137             case DW_CFA_restore:
7138               if (opa >= (unsigned int) fc->ncols)
7139                 reg_prefix = bad_reg;
7140               if (! do_debug_frames_interp || *reg_prefix != '\0')
7141                 printf ("  DW_CFA_restore: %s%s\n",
7142                         reg_prefix, regname (opa, 0));
7143               if (*reg_prefix != '\0')
7144                 break;
7145
7146               if (opa >= (unsigned int) cie->ncols
7147                   || (do_debug_frames_interp
7148                       && cie->col_type[opa] == DW_CFA_unreferenced))
7149                 {
7150                   fc->col_type[opa] = DW_CFA_undefined;
7151                   fc->col_offset[opa] = 0;
7152                 }
7153               else
7154                 {
7155                   fc->col_type[opa] = cie->col_type[opa];
7156                   fc->col_offset[opa] = cie->col_offset[opa];
7157                 }
7158               break;
7159
7160             case DW_CFA_set_loc:
7161               vma = get_encoded_value (&start, fc->fde_encoding, section, block_end);
7162               if (do_debug_frames_interp)
7163                 frame_display_row (fc, &need_col_headers, &max_regs);
7164               else
7165                 printf ("  DW_CFA_set_loc: %s\n",
7166                         dwarf_vmatoa_1 (NULL, vma, fc->ptr_size));
7167               fc->pc_begin = vma;
7168               break;
7169
7170             case DW_CFA_advance_loc1:
7171               SAFE_BYTE_GET_AND_INC (ofs, start, 1, end);
7172               if (do_debug_frames_interp)
7173                 frame_display_row (fc, &need_col_headers, &max_regs);
7174               else
7175                 printf ("  DW_CFA_advance_loc1: %ld to %s\n",
7176                         (unsigned long) (ofs * fc->code_factor),
7177                         dwarf_vmatoa_1 (NULL,
7178                                         fc->pc_begin + ofs * fc->code_factor,
7179                                         fc->ptr_size));
7180               fc->pc_begin += ofs * fc->code_factor;
7181               break;
7182
7183             case DW_CFA_advance_loc2:
7184               SAFE_BYTE_GET_AND_INC (ofs, start, 2, block_end);
7185               if (do_debug_frames_interp)
7186                 frame_display_row (fc, &need_col_headers, &max_regs);
7187               else
7188                 printf ("  DW_CFA_advance_loc2: %ld to %s\n",
7189                         (unsigned long) (ofs * fc->code_factor),
7190                         dwarf_vmatoa_1 (NULL,
7191                                         fc->pc_begin + ofs * fc->code_factor,
7192                                         fc->ptr_size));
7193               fc->pc_begin += ofs * fc->code_factor;
7194               break;
7195
7196             case DW_CFA_advance_loc4:
7197               SAFE_BYTE_GET_AND_INC (ofs, start, 4, block_end);
7198               if (do_debug_frames_interp)
7199                 frame_display_row (fc, &need_col_headers, &max_regs);
7200               else
7201                 printf ("  DW_CFA_advance_loc4: %ld to %s\n",
7202                         (unsigned long) (ofs * fc->code_factor),
7203                         dwarf_vmatoa_1 (NULL,
7204                                         fc->pc_begin + ofs * fc->code_factor,
7205                                         fc->ptr_size));
7206               fc->pc_begin += ofs * fc->code_factor;
7207               break;
7208
7209             case DW_CFA_offset_extended:
7210               reg = LEB ();
7211               roffs = LEB ();
7212               if (reg >= (unsigned int) fc->ncols)
7213                 reg_prefix = bad_reg;
7214               if (! do_debug_frames_interp || *reg_prefix != '\0')
7215                 printf ("  DW_CFA_offset_extended: %s%s at cfa%+ld\n",
7216                         reg_prefix, regname (reg, 0),
7217                         roffs * fc->data_factor);
7218               if (*reg_prefix == '\0')
7219                 {
7220                   fc->col_type[reg] = DW_CFA_offset;
7221                   fc->col_offset[reg] = roffs * fc->data_factor;
7222                 }
7223               break;
7224
7225             case DW_CFA_val_offset:
7226               reg = LEB ();
7227               roffs = LEB ();
7228               if (reg >= (unsigned int) fc->ncols)
7229                 reg_prefix = bad_reg;
7230               if (! do_debug_frames_interp || *reg_prefix != '\0')
7231                 printf ("  DW_CFA_val_offset: %s%s is cfa%+ld\n",
7232                         reg_prefix, regname (reg, 0),
7233                         roffs * fc->data_factor);
7234               if (*reg_prefix == '\0')
7235                 {
7236                   fc->col_type[reg] = DW_CFA_val_offset;
7237                   fc->col_offset[reg] = roffs * fc->data_factor;
7238                 }
7239               break;
7240
7241             case DW_CFA_restore_extended:
7242               reg = LEB ();
7243               if (reg >= (unsigned int) fc->ncols)
7244                 reg_prefix = bad_reg;
7245               if (! do_debug_frames_interp || *reg_prefix != '\0')
7246                 printf ("  DW_CFA_restore_extended: %s%s\n",
7247                         reg_prefix, regname (reg, 0));
7248               if (*reg_prefix != '\0')
7249                 break;
7250
7251               if (reg >= (unsigned int) cie->ncols)
7252                 {
7253                   fc->col_type[reg] = DW_CFA_undefined;
7254                   fc->col_offset[reg] = 0;
7255                 }
7256               else
7257                 {
7258                   fc->col_type[reg] = cie->col_type[reg];
7259                   fc->col_offset[reg] = cie->col_offset[reg];
7260                 }
7261               break;
7262
7263             case DW_CFA_undefined:
7264               reg = LEB ();
7265               if (reg >= (unsigned int) fc->ncols)
7266                 reg_prefix = bad_reg;
7267               if (! do_debug_frames_interp || *reg_prefix != '\0')
7268                 printf ("  DW_CFA_undefined: %s%s\n",
7269                         reg_prefix, regname (reg, 0));
7270               if (*reg_prefix == '\0')
7271                 {
7272                   fc->col_type[reg] = DW_CFA_undefined;
7273                   fc->col_offset[reg] = 0;
7274                 }
7275               break;
7276
7277             case DW_CFA_same_value:
7278               reg = LEB ();
7279               if (reg >= (unsigned int) fc->ncols)
7280                 reg_prefix = bad_reg;
7281               if (! do_debug_frames_interp || *reg_prefix != '\0')
7282                 printf ("  DW_CFA_same_value: %s%s\n",
7283                         reg_prefix, regname (reg, 0));
7284               if (*reg_prefix == '\0')
7285                 {
7286                   fc->col_type[reg] = DW_CFA_same_value;
7287                   fc->col_offset[reg] = 0;
7288                 }
7289               break;
7290
7291             case DW_CFA_register:
7292               reg = LEB ();
7293               roffs = LEB ();
7294               if (reg >= (unsigned int) fc->ncols)
7295                 reg_prefix = bad_reg;
7296               if (! do_debug_frames_interp || *reg_prefix != '\0')
7297                 {
7298                   printf ("  DW_CFA_register: %s%s in ",
7299                           reg_prefix, regname (reg, 0));
7300                   puts (regname (roffs, 0));
7301                 }
7302               if (*reg_prefix == '\0')
7303                 {
7304                   fc->col_type[reg] = DW_CFA_register;
7305                   fc->col_offset[reg] = roffs;
7306                 }
7307               break;
7308
7309             case DW_CFA_remember_state:
7310               if (! do_debug_frames_interp)
7311                 printf ("  DW_CFA_remember_state\n");
7312               rs = (Frame_Chunk *) xmalloc (sizeof (Frame_Chunk));
7313               rs->cfa_offset = fc->cfa_offset;
7314               rs->cfa_reg = fc->cfa_reg;
7315               rs->ra = fc->ra;
7316               rs->cfa_exp = fc->cfa_exp;
7317               rs->ncols = fc->ncols;
7318               rs->col_type = (short int *) xcmalloc (rs->ncols,
7319                                                      sizeof (* rs->col_type));
7320               rs->col_offset = (int *) xcmalloc (rs->ncols, sizeof (* rs->col_offset));
7321               memcpy (rs->col_type, fc->col_type, rs->ncols * sizeof (* fc->col_type));
7322               memcpy (rs->col_offset, fc->col_offset, rs->ncols * sizeof (* fc->col_offset));
7323               rs->next = remembered_state;
7324               remembered_state = rs;
7325               break;
7326
7327             case DW_CFA_restore_state:
7328               if (! do_debug_frames_interp)
7329                 printf ("  DW_CFA_restore_state\n");
7330               rs = remembered_state;
7331               if (rs)
7332                 {
7333                   remembered_state = rs->next;
7334                   fc->cfa_offset = rs->cfa_offset;
7335                   fc->cfa_reg = rs->cfa_reg;
7336                   fc->ra = rs->ra;
7337                   fc->cfa_exp = rs->cfa_exp;
7338                   if (frame_need_space (fc, rs->ncols - 1) < 0)
7339                     {
7340                       warn (_("Invalid column number in saved frame state\n"));
7341                       fc->ncols = 0;
7342                       break;
7343                     }
7344                   memcpy (fc->col_type, rs->col_type, rs->ncols * sizeof (* rs->col_type));
7345                   memcpy (fc->col_offset, rs->col_offset,
7346                           rs->ncols * sizeof (* rs->col_offset));
7347                   free (rs->col_type);
7348                   free (rs->col_offset);
7349                   free (rs);
7350                 }
7351               else if (do_debug_frames_interp)
7352                 printf ("Mismatched DW_CFA_restore_state\n");
7353               break;
7354
7355             case DW_CFA_def_cfa:
7356               fc->cfa_reg = LEB ();
7357               fc->cfa_offset = LEB ();
7358               fc->cfa_exp = 0;
7359               if (! do_debug_frames_interp)
7360                 printf ("  DW_CFA_def_cfa: %s ofs %d\n",
7361                         regname (fc->cfa_reg, 0), (int) fc->cfa_offset);
7362               break;
7363
7364             case DW_CFA_def_cfa_register:
7365               fc->cfa_reg = LEB ();
7366               fc->cfa_exp = 0;
7367               if (! do_debug_frames_interp)
7368                 printf ("  DW_CFA_def_cfa_register: %s\n",
7369                         regname (fc->cfa_reg, 0));
7370               break;
7371
7372             case DW_CFA_def_cfa_offset:
7373               fc->cfa_offset = LEB ();
7374               if (! do_debug_frames_interp)
7375                 printf ("  DW_CFA_def_cfa_offset: %d\n", (int) fc->cfa_offset);
7376               break;
7377
7378             case DW_CFA_nop:
7379               if (! do_debug_frames_interp)
7380                 printf ("  DW_CFA_nop\n");
7381               break;
7382
7383             case DW_CFA_def_cfa_expression:
7384               ul = LEB ();
7385               if (start >= block_end || ul > (unsigned long) (block_end - start))
7386                 {
7387                   printf (_("  DW_CFA_def_cfa_expression: <corrupt len %lu>\n"), ul);
7388                   break;
7389                 }
7390               if (! do_debug_frames_interp)
7391                 {
7392                   printf ("  DW_CFA_def_cfa_expression (");
7393                   decode_location_expression (start, eh_addr_size, 0, -1,
7394                                               ul, 0, section);
7395                   printf (")\n");
7396                 }
7397               fc->cfa_exp = 1;
7398               start += ul;
7399               break;
7400
7401             case DW_CFA_expression:
7402               reg = LEB ();
7403               ul = LEB ();
7404               if (reg >= (unsigned int) fc->ncols)
7405                 reg_prefix = bad_reg;
7406               /* PR 17512: file: 069-133014-0.006.  */
7407               /* PR 17512: file: 98c02eb4.  */
7408               tmp = start + ul;
7409               if (start >= block_end || tmp > block_end || tmp < start)
7410                 {
7411                   printf (_("  DW_CFA_expression: <corrupt len %lu>\n"), ul);
7412                   break;
7413                 }
7414               if (! do_debug_frames_interp || *reg_prefix != '\0')
7415                 {
7416                   printf ("  DW_CFA_expression: %s%s (",
7417                           reg_prefix, regname (reg, 0));
7418                   decode_location_expression (start, eh_addr_size, 0, -1,
7419                                               ul, 0, section);
7420                   printf (")\n");
7421                 }
7422               if (*reg_prefix == '\0')
7423                 fc->col_type[reg] = DW_CFA_expression;
7424               start = tmp;
7425               break;
7426
7427             case DW_CFA_val_expression:
7428               reg = LEB ();
7429               ul = LEB ();
7430               if (reg >= (unsigned int) fc->ncols)
7431                 reg_prefix = bad_reg;
7432               tmp = start + ul;
7433               if (start >= block_end || tmp > block_end || tmp < start)
7434                 {
7435                   printf ("  DW_CFA_val_expression: <corrupt len %lu>\n", ul);
7436                   break;
7437                 }
7438               if (! do_debug_frames_interp || *reg_prefix != '\0')
7439                 {
7440                   printf ("  DW_CFA_val_expression: %s%s (",
7441                           reg_prefix, regname (reg, 0));
7442                   decode_location_expression (start, eh_addr_size, 0, -1,
7443                                               ul, 0, section);
7444                   printf (")\n");
7445                 }
7446               if (*reg_prefix == '\0')
7447                 fc->col_type[reg] = DW_CFA_val_expression;
7448               start = tmp;
7449               break;
7450
7451             case DW_CFA_offset_extended_sf:
7452               reg = LEB ();
7453               l = SLEB ();
7454               if (frame_need_space (fc, reg) < 0)
7455                 reg_prefix = bad_reg;
7456               if (! do_debug_frames_interp || *reg_prefix != '\0')
7457                 printf ("  DW_CFA_offset_extended_sf: %s%s at cfa%+ld\n",
7458                         reg_prefix, regname (reg, 0),
7459                         (long)(l * fc->data_factor));
7460               if (*reg_prefix == '\0')
7461                 {
7462                   fc->col_type[reg] = DW_CFA_offset;
7463                   fc->col_offset[reg] = l * fc->data_factor;
7464                 }
7465               break;
7466
7467             case DW_CFA_val_offset_sf:
7468               reg = LEB ();
7469               l = SLEB ();
7470               if (frame_need_space (fc, reg) < 0)
7471                 reg_prefix = bad_reg;
7472               if (! do_debug_frames_interp || *reg_prefix != '\0')
7473                 printf ("  DW_CFA_val_offset_sf: %s%s is cfa%+ld\n",
7474                         reg_prefix, regname (reg, 0),
7475                         (long)(l * fc->data_factor));
7476               if (*reg_prefix == '\0')
7477                 {
7478                   fc->col_type[reg] = DW_CFA_val_offset;
7479                   fc->col_offset[reg] = l * fc->data_factor;
7480                 }
7481               break;
7482
7483             case DW_CFA_def_cfa_sf:
7484               fc->cfa_reg = LEB ();
7485               fc->cfa_offset = SLEB ();
7486               fc->cfa_offset = fc->cfa_offset * fc->data_factor;
7487               fc->cfa_exp = 0;
7488               if (! do_debug_frames_interp)
7489                 printf ("  DW_CFA_def_cfa_sf: %s ofs %d\n",
7490                         regname (fc->cfa_reg, 0), (int) fc->cfa_offset);
7491               break;
7492
7493             case DW_CFA_def_cfa_offset_sf:
7494               fc->cfa_offset = SLEB ();
7495               fc->cfa_offset *= fc->data_factor;
7496               if (! do_debug_frames_interp)
7497                 printf ("  DW_CFA_def_cfa_offset_sf: %d\n", (int) fc->cfa_offset);
7498               break;
7499
7500             case DW_CFA_MIPS_advance_loc8:
7501               SAFE_BYTE_GET_AND_INC (ofs, start, 8, block_end);
7502               if (do_debug_frames_interp)
7503                 frame_display_row (fc, &need_col_headers, &max_regs);
7504               else
7505                 printf ("  DW_CFA_MIPS_advance_loc8: %ld to %s\n",
7506                         (unsigned long) (ofs * fc->code_factor),
7507                         dwarf_vmatoa_1 (NULL,
7508                                         fc->pc_begin + ofs * fc->code_factor,
7509                                         fc->ptr_size));
7510               fc->pc_begin += ofs * fc->code_factor;
7511               break;
7512
7513             case DW_CFA_GNU_window_save:
7514               if (! do_debug_frames_interp)
7515                 printf ("  DW_CFA_GNU_window_save\n");
7516               break;
7517
7518             case DW_CFA_GNU_args_size:
7519               ul = LEB ();
7520               if (! do_debug_frames_interp)
7521                 printf ("  DW_CFA_GNU_args_size: %ld\n", ul);
7522               break;
7523
7524             case DW_CFA_GNU_negative_offset_extended:
7525               reg = LEB ();
7526               l = - LEB ();
7527               if (frame_need_space (fc, reg) < 0)
7528                 reg_prefix = bad_reg;
7529               if (! do_debug_frames_interp || *reg_prefix != '\0')
7530                 printf ("  DW_CFA_GNU_negative_offset_extended: %s%s at cfa%+ld\n",
7531                         reg_prefix, regname (reg, 0),
7532                         (long)(l * fc->data_factor));
7533               if (*reg_prefix == '\0')
7534                 {
7535                   fc->col_type[reg] = DW_CFA_offset;
7536                   fc->col_offset[reg] = l * fc->data_factor;
7537                 }
7538               break;
7539
7540             default:
7541               if (op >= DW_CFA_lo_user && op <= DW_CFA_hi_user)
7542                 printf (_("  DW_CFA_??? (User defined call frame op: %#x)\n"), op);
7543               else
7544                 warn (_("Unsupported or unknown Dwarf Call Frame Instruction number: %#x\n"), op);
7545               start = block_end;
7546             }
7547         }
7548
7549       /* Interpret the CFA - as long as it is not completely full of NOPs.  */
7550       if (do_debug_frames_interp && ! all_nops)
7551         frame_display_row (fc, &need_col_headers, &max_regs);
7552
7553       start = block_end;
7554       eh_addr_size = saved_eh_addr_size;
7555     }
7556
7557   printf ("\n");
7558
7559   return 1;
7560 }
7561
7562 #undef GET
7563 #undef LEB
7564 #undef SLEB
7565
7566 static int
7567 display_gdb_index (struct dwarf_section *section,
7568                    void *file ATTRIBUTE_UNUSED)
7569 {
7570   unsigned char *start = section->start;
7571   uint32_t version;
7572   uint32_t cu_list_offset, tu_list_offset;
7573   uint32_t address_table_offset, symbol_table_offset, constant_pool_offset;
7574   unsigned int cu_list_elements, tu_list_elements;
7575   unsigned int address_table_size, symbol_table_slots;
7576   unsigned char *cu_list, *tu_list;
7577   unsigned char *address_table, *symbol_table, *constant_pool;
7578   unsigned int i;
7579
7580   /* The documentation for the format of this file is in gdb/dwarf2read.c.  */
7581
7582   printf (_("Contents of the %s section:\n"), section->name);
7583
7584   if (section->size < 6 * sizeof (uint32_t))
7585     {
7586       warn (_("Truncated header in the %s section.\n"), section->name);
7587       return 0;
7588     }
7589
7590   version = byte_get_little_endian (start, 4);
7591   printf (_("Version %ld\n"), (long) version);
7592
7593   /* Prior versions are obsolete, and future versions may not be
7594      backwards compatible.  */
7595   if (version < 3 || version > 8)
7596     {
7597       warn (_("Unsupported version %lu.\n"), (unsigned long) version);
7598       return 0;
7599     }
7600   if (version < 4)
7601     warn (_("The address table data in version 3 may be wrong.\n"));
7602   if (version < 5)
7603     warn (_("Version 4 does not support case insensitive lookups.\n"));
7604   if (version < 6)
7605     warn (_("Version 5 does not include inlined functions.\n"));
7606   if (version < 7)
7607       warn (_("Version 6 does not include symbol attributes.\n"));
7608   /* Version 7 indices generated by Gold have bad type unit references,
7609      PR binutils/15021.  But we don't know if the index was generated by
7610      Gold or not, so to avoid worrying users with gdb-generated indices
7611      we say nothing for version 7 here.  */
7612
7613   cu_list_offset = byte_get_little_endian (start + 4, 4);
7614   tu_list_offset = byte_get_little_endian (start + 8, 4);
7615   address_table_offset = byte_get_little_endian (start + 12, 4);
7616   symbol_table_offset = byte_get_little_endian (start + 16, 4);
7617   constant_pool_offset = byte_get_little_endian (start + 20, 4);
7618
7619   if (cu_list_offset > section->size
7620       || tu_list_offset > section->size
7621       || address_table_offset > section->size
7622       || symbol_table_offset > section->size
7623       || constant_pool_offset > section->size)
7624     {
7625       warn (_("Corrupt header in the %s section.\n"), section->name);
7626       return 0;
7627     }
7628
7629   /* PR 17531: file: 418d0a8a.  */
7630   if (tu_list_offset < cu_list_offset)
7631     {
7632       warn (_("TU offset (%x) is less than CU offset (%x)\n"),
7633             tu_list_offset, cu_list_offset);
7634       return 0;
7635     }
7636
7637   cu_list_elements = (tu_list_offset - cu_list_offset) / 8;
7638
7639   if (address_table_offset < tu_list_offset)
7640     {
7641       warn (_("Address table offset (%x) is less than TU offset (%x)\n"),
7642             address_table_offset, tu_list_offset);
7643       return 0;
7644     }
7645
7646   tu_list_elements = (address_table_offset - tu_list_offset) / 8;
7647
7648   /* PR 17531: file: 18a47d3d.  */
7649   if (symbol_table_offset < address_table_offset)
7650     {
7651       warn (_("Symbol table offset (%xl) is less then Address table offset (%x)\n"),
7652             symbol_table_offset, address_table_offset);
7653       return 0;
7654     }
7655
7656   address_table_size = symbol_table_offset - address_table_offset;
7657
7658   if (constant_pool_offset < symbol_table_offset)
7659     {
7660       warn (_("Constant pool offset (%x) is less than symbol table offset (%x)\n"),
7661             constant_pool_offset, symbol_table_offset);
7662       return 0;
7663     }
7664
7665   symbol_table_slots = (constant_pool_offset - symbol_table_offset) / 8;
7666
7667   cu_list = start + cu_list_offset;
7668   tu_list = start + tu_list_offset;
7669   address_table = start + address_table_offset;
7670   symbol_table = start + symbol_table_offset;
7671   constant_pool = start + constant_pool_offset;
7672
7673   if (address_table + address_table_size > section->start + section->size)
7674     {
7675       warn (_("Address table extends beyond end of section.\n"));
7676       return 0;
7677     }
7678
7679   printf (_("\nCU table:\n"));
7680   for (i = 0; i < cu_list_elements; i += 2)
7681     {
7682       uint64_t cu_offset = byte_get_little_endian (cu_list + i * 8, 8);
7683       uint64_t cu_length = byte_get_little_endian (cu_list + i * 8 + 8, 8);
7684
7685       printf (_("[%3u] 0x%lx - 0x%lx\n"), i / 2,
7686               (unsigned long) cu_offset,
7687               (unsigned long) (cu_offset + cu_length - 1));
7688     }
7689
7690   printf (_("\nTU table:\n"));
7691   for (i = 0; i < tu_list_elements; i += 3)
7692     {
7693       uint64_t tu_offset = byte_get_little_endian (tu_list + i * 8, 8);
7694       uint64_t type_offset = byte_get_little_endian (tu_list + i * 8 + 8, 8);
7695       uint64_t signature = byte_get_little_endian (tu_list + i * 8 + 16, 8);
7696
7697       printf (_("[%3u] 0x%lx 0x%lx "), i / 3,
7698               (unsigned long) tu_offset,
7699               (unsigned long) type_offset);
7700       print_dwarf_vma (signature, 8);
7701       printf ("\n");
7702     }
7703
7704   printf (_("\nAddress table:\n"));
7705   for (i = 0; i < address_table_size && i <= address_table_size - (2 * 8 + 4);
7706        i += 2 * 8 + 4)
7707     {
7708       uint64_t low = byte_get_little_endian (address_table + i, 8);
7709       uint64_t high = byte_get_little_endian (address_table + i + 8, 8);
7710       uint32_t cu_index = byte_get_little_endian (address_table + i + 16, 4);
7711
7712       print_dwarf_vma (low, 8);
7713       print_dwarf_vma (high, 8);
7714       printf (_("%lu\n"), (unsigned long) cu_index);
7715     }
7716
7717   printf (_("\nSymbol table:\n"));
7718   for (i = 0; i < symbol_table_slots; ++i)
7719     {
7720       uint32_t name_offset = byte_get_little_endian (symbol_table + i * 8, 4);
7721       uint32_t cu_vector_offset = byte_get_little_endian (symbol_table + i * 8 + 4, 4);
7722       uint32_t num_cus, cu;
7723
7724       if (name_offset != 0
7725           || cu_vector_offset != 0)
7726         {
7727           unsigned int j;
7728           unsigned char * adr;
7729
7730           adr = constant_pool + name_offset;
7731           /* PR 17531: file: 5b7b07ad.  */
7732           if (adr < constant_pool || adr >= section->start + section->size)
7733             {
7734               printf (_("[%3u] <corrupt offset: %x>"), i, name_offset);
7735               warn (_("Corrupt name offset of 0x%x found for symbol table slot %d\n"),
7736                     name_offset, i);
7737             }
7738           else
7739             printf ("[%3u] %.*s:", i,
7740                     (int) (section->size - (constant_pool_offset + name_offset)),
7741                     constant_pool + name_offset);
7742
7743           adr = constant_pool + cu_vector_offset;
7744           if (adr < constant_pool || adr >= section->start + section->size - 3)
7745             {
7746               printf (_("<invalid CU vector offset: %x>\n"), cu_vector_offset);
7747               warn (_("Corrupt CU vector offset of 0x%x found for symbol table slot %d\n"),
7748                     cu_vector_offset, i);
7749               continue;
7750             }
7751
7752           num_cus = byte_get_little_endian (adr, 4);
7753
7754           adr = constant_pool + cu_vector_offset + 4 + num_cus * 4;
7755           if (num_cus * 4 < num_cus
7756               || adr >= section->start + section->size
7757               || adr < constant_pool)
7758             {
7759               printf ("<invalid number of CUs: %d>\n", num_cus);
7760               warn (_("Invalid number of CUs (0x%x) for symbol table slot %d\n"),
7761                     num_cus, i);
7762               continue;
7763             }
7764
7765           if (num_cus > 1)
7766             printf ("\n");
7767
7768           for (j = 0; j < num_cus; ++j)
7769             {
7770               int is_static;
7771               gdb_index_symbol_kind kind;
7772
7773               cu = byte_get_little_endian (constant_pool + cu_vector_offset + 4 + j * 4, 4);
7774               is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu);
7775               kind = GDB_INDEX_SYMBOL_KIND_VALUE (cu);
7776               cu = GDB_INDEX_CU_VALUE (cu);
7777               /* Convert to TU number if it's for a type unit.  */
7778               if (cu >= cu_list_elements / 2)
7779                 printf ("%cT%lu", num_cus > 1 ? '\t' : ' ',
7780                         (unsigned long) (cu - cu_list_elements / 2));
7781               else
7782                 printf ("%c%lu", num_cus > 1 ? '\t' : ' ', (unsigned long) cu);
7783
7784               printf (" [%s, %s]",
7785                       is_static ? _("static") : _("global"),
7786                       get_gdb_index_symbol_kind_name (kind));
7787               if (num_cus > 1)
7788                 printf ("\n");
7789             }
7790           if (num_cus <= 1)
7791             printf ("\n");
7792         }
7793     }
7794
7795   return 1;
7796 }
7797
7798 /* Pre-allocate enough space for the CU/TU sets needed.  */
7799
7800 static void
7801 prealloc_cu_tu_list (unsigned int nshndx)
7802 {
7803   if (shndx_pool == NULL)
7804     {
7805       shndx_pool_size = nshndx;
7806       shndx_pool_used = 0;
7807       shndx_pool = (unsigned int *) xcmalloc (shndx_pool_size,
7808                                               sizeof (unsigned int));
7809     }
7810   else
7811     {
7812       shndx_pool_size = shndx_pool_used + nshndx;
7813       shndx_pool = (unsigned int *) xcrealloc (shndx_pool, shndx_pool_size,
7814                                                sizeof (unsigned int));
7815     }
7816 }
7817
7818 static void
7819 add_shndx_to_cu_tu_entry (unsigned int shndx)
7820 {
7821   if (shndx_pool_used >= shndx_pool_size)
7822     {
7823       error (_("Internal error: out of space in the shndx pool.\n"));
7824       return;
7825     }
7826   shndx_pool [shndx_pool_used++] = shndx;
7827 }
7828
7829 static void
7830 end_cu_tu_entry (void)
7831 {
7832   if (shndx_pool_used >= shndx_pool_size)
7833     {
7834       error (_("Internal error: out of space in the shndx pool.\n"));
7835       return;
7836     }
7837   shndx_pool [shndx_pool_used++] = 0;
7838 }
7839
7840 /* Return the short name of a DWARF section given by a DW_SECT enumerator.  */
7841
7842 static const char *
7843 get_DW_SECT_short_name (unsigned int dw_sect)
7844 {
7845   static char buf[16];
7846
7847   switch (dw_sect)
7848     {
7849       case DW_SECT_INFO:
7850         return "info";
7851       case DW_SECT_TYPES:
7852         return "types";
7853       case DW_SECT_ABBREV:
7854         return "abbrev";
7855       case DW_SECT_LINE:
7856         return "line";
7857       case DW_SECT_LOC:
7858         return "loc";
7859       case DW_SECT_STR_OFFSETS:
7860         return "str_off";
7861       case DW_SECT_MACINFO:
7862         return "macinfo";
7863       case DW_SECT_MACRO:
7864         return "macro";
7865       default:
7866         break;
7867     }
7868
7869   snprintf (buf, sizeof (buf), "%d", dw_sect);
7870   return buf;
7871 }
7872
7873 /* Process a CU or TU index.  If DO_DISPLAY is true, print the contents.
7874    These sections are extensions for Fission.
7875    See http://gcc.gnu.org/wiki/DebugFissionDWP.  */
7876
7877 static int
7878 process_cu_tu_index (struct dwarf_section *section, int do_display)
7879 {
7880   unsigned char *phdr = section->start;
7881   unsigned char *limit = phdr + section->size;
7882   unsigned char *phash;
7883   unsigned char *pindex;
7884   unsigned char *ppool;
7885   unsigned int version;
7886   unsigned int ncols = 0;
7887   unsigned int nused;
7888   unsigned int nslots;
7889   unsigned int i;
7890   unsigned int j;
7891   dwarf_vma signature_high;
7892   dwarf_vma signature_low;
7893   char buf[64];
7894
7895   /* PR 17512: file: 002-168123-0.004.  */
7896   if (phdr == NULL)
7897     {
7898       warn (_("Section %s is empty\n"), section->name);
7899       return 0;
7900     }
7901   /* PR 17512: file: 002-376-0.004.  */
7902   if (section->size < 24)
7903     {
7904       warn (_("Section %s is too small to contain a CU/TU header\n"),
7905             section->name);
7906       return 0;
7907     }
7908
7909   SAFE_BYTE_GET (version, phdr, 4, limit);
7910   if (version >= 2)
7911     SAFE_BYTE_GET (ncols, phdr + 4, 4, limit);
7912   SAFE_BYTE_GET (nused, phdr + 8, 4, limit);
7913   SAFE_BYTE_GET (nslots, phdr + 12, 4, limit);
7914
7915   phash = phdr + 16;
7916   pindex = phash + nslots * 8;
7917   ppool = pindex + nslots * 4;
7918
7919   /* PR 17531: file: 45d69832.  */
7920   if (pindex < phash || ppool < phdr || (pindex == phash && nslots != 0))
7921     {
7922       warn (_("Section %s is too small for %d slots\n"),
7923             section->name, nslots);
7924       return 0;
7925     }
7926
7927   if (do_display)
7928     {
7929       printf (_("Contents of the %s section:\n\n"), section->name);
7930       printf (_("  Version:                 %d\n"), version);
7931       if (version >= 2)
7932         printf (_("  Number of columns:       %d\n"), ncols);
7933       printf (_("  Number of used entries:  %d\n"), nused);
7934       printf (_("  Number of slots:         %d\n\n"), nslots);
7935     }
7936
7937   if (ppool > limit || ppool < phdr)
7938     {
7939       warn (_("Section %s too small for %d hash table entries\n"),
7940             section->name, nslots);
7941       return 0;
7942     }
7943
7944   if (version == 1)
7945     {
7946       if (!do_display)
7947         prealloc_cu_tu_list ((limit - ppool) / 4);
7948       for (i = 0; i < nslots; i++)
7949         {
7950           unsigned char *shndx_list;
7951           unsigned int shndx;
7952
7953           SAFE_BYTE_GET64 (phash, &signature_high, &signature_low, limit);
7954           if (signature_high != 0 || signature_low != 0)
7955             {
7956               SAFE_BYTE_GET (j, pindex, 4, limit);
7957               shndx_list = ppool + j * 4;
7958               /* PR 17531: file: 705e010d.  */
7959               if (shndx_list < ppool)
7960                 {
7961                   warn (_("Section index pool located before start of section\n"));
7962                   return 0;
7963                 }
7964
7965               if (do_display)
7966                 printf (_("  [%3d] Signature:  0x%s  Sections: "),
7967                         i, dwarf_vmatoa64 (signature_high, signature_low,
7968                                            buf, sizeof (buf)));
7969               for (;;)
7970                 {
7971                   if (shndx_list >= limit)
7972                     {
7973                       warn (_("Section %s too small for shndx pool\n"),
7974                             section->name);
7975                       return 0;
7976                     }
7977                   SAFE_BYTE_GET (shndx, shndx_list, 4, limit);
7978                   if (shndx == 0)
7979                     break;
7980                   if (do_display)
7981                     printf (" %d", shndx);
7982                   else
7983                     add_shndx_to_cu_tu_entry (shndx);
7984                   shndx_list += 4;
7985                 }
7986               if (do_display)
7987                 printf ("\n");
7988               else
7989                 end_cu_tu_entry ();
7990             }
7991           phash += 8;
7992           pindex += 4;
7993         }
7994     }
7995   else if (version == 2)
7996     {
7997       unsigned int val;
7998       unsigned int dw_sect;
7999       unsigned char *ph = phash;
8000       unsigned char *pi = pindex;
8001       unsigned char *poffsets = ppool + ncols * 4;
8002       unsigned char *psizes = poffsets + nused * ncols * 4;
8003       unsigned char *pend = psizes + nused * ncols * 4;
8004       bfd_boolean is_tu_index;
8005       struct cu_tu_set *this_set = NULL;
8006       unsigned int row;
8007       unsigned char *prow;
8008
8009       is_tu_index = strcmp (section->name, ".debug_tu_index") == 0;
8010
8011       /* PR 17531: file: 0dd159bf.
8012          Check for wraparound with an overlarge ncols value.  */
8013       if (poffsets < ppool || (unsigned int) ((poffsets - ppool) / 4) != ncols)
8014         {
8015           warn (_("Overlarge number of columns: %x\n"), ncols);
8016           return 0;
8017         }
8018
8019       if (pend > limit)
8020         {
8021           warn (_("Section %s too small for offset and size tables\n"),
8022                 section->name);
8023           return 0;
8024         }
8025
8026       if (do_display)
8027         {
8028           printf (_("  Offset table\n"));
8029           printf ("  slot  %-16s  ",
8030                  is_tu_index ? _("signature") : _("dwo_id"));
8031         }
8032       else
8033         {
8034           if (is_tu_index)
8035             {
8036               tu_count = nused;
8037               tu_sets = xcalloc2 (nused, sizeof (struct cu_tu_set));
8038               this_set = tu_sets;
8039             }
8040           else
8041             {
8042               cu_count = nused;
8043               cu_sets = xcalloc2 (nused, sizeof (struct cu_tu_set));
8044               this_set = cu_sets;
8045             }
8046         }
8047
8048       if (do_display)
8049         {
8050           for (j = 0; j < ncols; j++)
8051             {
8052               SAFE_BYTE_GET (dw_sect, ppool + j * 4, 4, limit);
8053               printf (" %8s", get_DW_SECT_short_name (dw_sect));
8054             }
8055           printf ("\n");
8056         }
8057
8058       for (i = 0; i < nslots; i++)
8059         {
8060           SAFE_BYTE_GET64 (ph, &signature_high, &signature_low, limit);
8061
8062           SAFE_BYTE_GET (row, pi, 4, limit);
8063           if (row != 0)
8064             {
8065               /* PR 17531: file: a05f6ab3.  */
8066               if (row > nused)
8067                 {
8068                   warn (_("Row index (%u) is larger than number of used entries (%u)\n"),
8069                         row, nused);
8070                   return 0;
8071                 }
8072
8073               if (!do_display)
8074                 memcpy (&this_set[row - 1].signature, ph, sizeof (uint64_t));
8075
8076               prow = poffsets + (row - 1) * ncols * 4;
8077               /* PR 17531: file: b8ce60a8.  */
8078               if (prow < poffsets || prow > limit)
8079                 {
8080                   warn (_("Row index (%u) * num columns (%u) > space remaining in section\n"),
8081                         row, ncols);
8082                   return 0;
8083                 }
8084
8085               if (do_display)
8086                 printf (_("  [%3d] 0x%s"),
8087                         i, dwarf_vmatoa64 (signature_high, signature_low,
8088                                            buf, sizeof (buf)));
8089               for (j = 0; j < ncols; j++)
8090                 {
8091                   SAFE_BYTE_GET (val, prow + j * 4, 4, limit);
8092                   if (do_display)
8093                     printf (" %8d", val);
8094                   else
8095                     {
8096                       SAFE_BYTE_GET (dw_sect, ppool + j * 4, 4, limit);
8097
8098                       /* PR 17531: file: 10796eb3.  */
8099                       if (dw_sect >= DW_SECT_MAX)
8100                         warn (_("Overlarge Dwarf section index detected: %u\n"), dw_sect);
8101                       else
8102                         this_set [row - 1].section_offsets [dw_sect] = val;
8103                     }
8104                 }
8105
8106               if (do_display)
8107                 printf ("\n");
8108             }
8109           ph += 8;
8110           pi += 4;
8111         }
8112
8113       ph = phash;
8114       pi = pindex;
8115       if (do_display)
8116         {
8117           printf ("\n");
8118           printf (_("  Size table\n"));
8119           printf ("  slot  %-16s  ",
8120                  is_tu_index ? _("signature") : _("dwo_id"));
8121         }
8122
8123       for (j = 0; j < ncols; j++)
8124         {
8125           SAFE_BYTE_GET (val, ppool + j * 4, 4, limit);
8126           if (do_display)
8127             printf (" %8s", get_DW_SECT_short_name (val));
8128         }
8129
8130       if (do_display)
8131         printf ("\n");
8132
8133       for (i = 0; i < nslots; i++)
8134         {
8135           SAFE_BYTE_GET64 (ph, &signature_high, &signature_low, limit);
8136
8137           SAFE_BYTE_GET (row, pi, 4, limit);
8138           if (row != 0)
8139             {
8140               prow = psizes + (row - 1) * ncols * 4;
8141
8142               if (do_display)
8143                 printf (_("  [%3d] 0x%s"),
8144                         i, dwarf_vmatoa64 (signature_high, signature_low,
8145                                            buf, sizeof (buf)));
8146
8147               for (j = 0; j < ncols; j++)
8148                 {
8149                   SAFE_BYTE_GET (val, prow + j * 4, 4, limit);
8150                   if (do_display)
8151                     printf (" %8d", val);
8152                   else
8153                     {
8154                       SAFE_BYTE_GET (dw_sect, ppool + j * 4, 4, limit);
8155                       if (dw_sect >= DW_SECT_MAX)
8156                         warn (_("Overlarge Dwarf section index detected: %u\n"), dw_sect);
8157                       else
8158                       this_set [row - 1].section_sizes [dw_sect] = val;
8159                     }
8160                 }
8161
8162               if (do_display)
8163                 printf ("\n");
8164             }
8165
8166           ph += 8;
8167           pi += 4;
8168         }
8169     }
8170   else if (do_display)
8171     printf (_("  Unsupported version (%d)\n"), version);
8172
8173   if (do_display)
8174       printf ("\n");
8175
8176   return 1;
8177 }
8178
8179 /* Load the CU and TU indexes if present.  This will build a list of
8180    section sets that we can use to associate a .debug_info.dwo section
8181    with its associated .debug_abbrev.dwo section in a .dwp file.  */
8182
8183 static bfd_boolean
8184 load_cu_tu_indexes (void *file)
8185 {
8186   static int cu_tu_indexes_read = -1; /* Tri-state variable.  */
8187
8188   /* If we have already loaded (or tried to load) the CU and TU indexes
8189      then do not bother to repeat the task.  */
8190   if (cu_tu_indexes_read == -1)
8191     {
8192       cu_tu_indexes_read = TRUE;
8193   
8194       if (load_debug_section (dwp_cu_index, file))
8195         if (! process_cu_tu_index (&debug_displays [dwp_cu_index].section, 0))
8196           cu_tu_indexes_read = FALSE;
8197
8198       if (load_debug_section (dwp_tu_index, file))
8199         if (! process_cu_tu_index (&debug_displays [dwp_tu_index].section, 0))
8200           cu_tu_indexes_read = FALSE;
8201     }
8202
8203   return (bfd_boolean) cu_tu_indexes_read;
8204 }
8205
8206 /* Find the set of sections that includes section SHNDX.  */
8207
8208 unsigned int *
8209 find_cu_tu_set (void *file, unsigned int shndx)
8210 {
8211   unsigned int i;
8212
8213   if (! load_cu_tu_indexes (file))
8214     return NULL;
8215
8216   /* Find SHNDX in the shndx pool.  */
8217   for (i = 0; i < shndx_pool_used; i++)
8218     if (shndx_pool [i] == shndx)
8219       break;
8220
8221   if (i >= shndx_pool_used)
8222     return NULL;
8223
8224   /* Now backup to find the first entry in the set.  */
8225   while (i > 0 && shndx_pool [i - 1] != 0)
8226     i--;
8227
8228   return shndx_pool + i;
8229 }
8230
8231 /* Display a .debug_cu_index or .debug_tu_index section.  */
8232
8233 static int
8234 display_cu_index (struct dwarf_section *section, void *file ATTRIBUTE_UNUSED)
8235 {
8236   return process_cu_tu_index (section, 1);
8237 }
8238
8239 static int
8240 display_debug_not_supported (struct dwarf_section *section,
8241                              void *file ATTRIBUTE_UNUSED)
8242 {
8243   printf (_("Displaying the debug contents of section %s is not yet supported.\n"),
8244             section->name);
8245
8246   return 1;
8247 }
8248
8249 /* Like malloc, but takes two parameters like calloc.
8250    Verifies that the first parameter is not too large.
8251    Note: does *not* initialise the allocated memory to zero.  */
8252 void *
8253 cmalloc (size_t nmemb, size_t size)
8254 {
8255   /* Check for overflow.  */
8256   if (nmemb >= ~(size_t) 0 / size)
8257     return NULL;
8258
8259   return xmalloc (nmemb * size);
8260 }
8261
8262 /* Like xmalloc, but takes two parameters like calloc.
8263    Verifies that the first parameter is not too large.
8264    Note: does *not* initialise the allocated memory to zero.  */
8265 void *
8266 xcmalloc (size_t nmemb, size_t size)
8267 {
8268   /* Check for overflow.  */
8269   if (nmemb >= ~(size_t) 0 / size)
8270     {
8271       fprintf (stderr,
8272                _("Attempt to allocate an array with an excessive number of elements: 0x%lx\n"),
8273                (long) nmemb);
8274       xexit (1);
8275     }
8276
8277   return xmalloc (nmemb * size);
8278 }
8279
8280 /* Like xrealloc, but takes three parameters.
8281    Verifies that the second parameter is not too large.
8282    Note: does *not* initialise any new memory to zero.  */
8283 void *
8284 xcrealloc (void *ptr, size_t nmemb, size_t size)
8285 {
8286   /* Check for overflow.  */
8287   if (nmemb >= ~(size_t) 0 / size)
8288     {
8289       fprintf (stderr,
8290                _("Attempt to re-allocate an array with an excessive number of elements: 0x%lx\n"),
8291                (long) nmemb);
8292       xexit (1);
8293     }
8294
8295   return xrealloc (ptr, nmemb * size);
8296 }
8297
8298 /* Like xcalloc, but verifies that the first parameter is not too large.  */
8299 void *
8300 xcalloc2 (size_t nmemb, size_t size)
8301 {
8302   /* Check for overflow.  */
8303   if (nmemb >= ~(size_t) 0 / size)
8304     {
8305       fprintf (stderr,
8306                _("Attempt to allocate a zero'ed array with an excessive number of elements: 0x%lx\n"),
8307                (long) nmemb);
8308       xexit (1);
8309     }
8310
8311   return xcalloc (nmemb, size);
8312 }
8313
8314 void
8315 free_debug_memory (void)
8316 {
8317   unsigned int i;
8318
8319   free_abbrevs ();
8320
8321   for (i = 0; i < max; i++)
8322     free_debug_section ((enum dwarf_section_display_enum) i);
8323
8324   if (debug_information != NULL)
8325     {
8326       if (num_debug_info_entries != DEBUG_INFO_UNAVAILABLE)
8327         {
8328           for (i = 0; i < num_debug_info_entries; i++)
8329             {
8330               if (!debug_information [i].max_loc_offsets)
8331                 {
8332                   free (debug_information [i].loc_offsets);
8333                   free (debug_information [i].have_frame_base);
8334                 }
8335               if (!debug_information [i].max_range_lists)
8336                 free (debug_information [i].range_lists);
8337             }
8338         }
8339       free (debug_information);
8340       debug_information = NULL;
8341       alloc_num_debug_info_entries = num_debug_info_entries = 0;
8342     }
8343 }
8344
8345 void
8346 dwarf_select_sections_by_names (const char *names)
8347 {
8348   typedef struct
8349   {
8350     const char * option;
8351     int *        variable;
8352     int          val;
8353   }
8354   debug_dump_long_opts;
8355
8356   static const debug_dump_long_opts opts_table [] =
8357     {
8358       /* Please keep this table alpha- sorted.  */
8359       { "Ranges", & do_debug_ranges, 1 },
8360       { "abbrev", & do_debug_abbrevs, 1 },
8361       { "addr", & do_debug_addr, 1 },
8362       { "aranges", & do_debug_aranges, 1 },
8363       { "cu_index", & do_debug_cu_index, 1 },
8364       { "decodedline", & do_debug_lines, FLAG_DEBUG_LINES_DECODED },
8365       { "frames", & do_debug_frames, 1 },
8366       { "frames-interp", & do_debug_frames_interp, 1 },
8367       /* The special .gdb_index section.  */
8368       { "gdb_index", & do_gdb_index, 1 },
8369       { "info", & do_debug_info, 1 },
8370       { "line", & do_debug_lines, FLAG_DEBUG_LINES_RAW }, /* For backwards compatibility.  */
8371       { "loc",  & do_debug_loc, 1 },
8372       { "macro", & do_debug_macinfo, 1 },
8373       { "pubnames", & do_debug_pubnames, 1 },
8374       { "pubtypes", & do_debug_pubtypes, 1 },
8375       /* This entry is for compatibility
8376          with earlier versions of readelf.  */
8377       { "ranges", & do_debug_aranges, 1 },
8378       { "rawline", & do_debug_lines, FLAG_DEBUG_LINES_RAW },
8379       { "str", & do_debug_str, 1 },
8380       /* These trace_* sections are used by Itanium VMS.  */
8381       { "trace_abbrev", & do_trace_abbrevs, 1 },
8382       { "trace_aranges", & do_trace_aranges, 1 },
8383       { "trace_info", & do_trace_info, 1 },
8384       { NULL, NULL, 0 }
8385     };
8386
8387   const char *p;
8388
8389   p = names;
8390   while (*p)
8391     {
8392       const debug_dump_long_opts * entry;
8393
8394       for (entry = opts_table; entry->option; entry++)
8395         {
8396           size_t len = strlen (entry->option);
8397
8398           if (strncmp (p, entry->option, len) == 0
8399               && (p[len] == ',' || p[len] == '\0'))
8400             {
8401               * entry->variable |= entry->val;
8402
8403               /* The --debug-dump=frames-interp option also
8404                  enables the --debug-dump=frames option.  */
8405               if (do_debug_frames_interp)
8406                 do_debug_frames = 1;
8407
8408               p += len;
8409               break;
8410             }
8411         }
8412
8413       if (entry->option == NULL)
8414         {
8415           warn (_("Unrecognized debug option '%s'\n"), p);
8416           p = strchr (p, ',');
8417           if (p == NULL)
8418             break;
8419         }
8420
8421       if (*p == ',')
8422         p++;
8423     }
8424 }
8425
8426 void
8427 dwarf_select_sections_by_letters (const char *letters)
8428 {
8429   unsigned int lindex = 0;
8430
8431   while (letters[lindex])
8432     switch (letters[lindex++])
8433       {
8434       case 'i':
8435         do_debug_info = 1;
8436         break;
8437
8438       case 'a':
8439         do_debug_abbrevs = 1;
8440         break;
8441
8442       case 'l':
8443         do_debug_lines |= FLAG_DEBUG_LINES_RAW;
8444         break;
8445
8446       case 'L':
8447         do_debug_lines |= FLAG_DEBUG_LINES_DECODED;
8448         break;
8449
8450       case 'p':
8451         do_debug_pubnames = 1;
8452         break;
8453
8454       case 't':
8455         do_debug_pubtypes = 1;
8456         break;
8457
8458       case 'r':
8459         do_debug_aranges = 1;
8460         break;
8461
8462       case 'R':
8463         do_debug_ranges = 1;
8464         break;
8465
8466       case 'F':
8467         do_debug_frames_interp = 1;
8468         /* Fall through.  */
8469       case 'f':
8470         do_debug_frames = 1;
8471         break;
8472
8473       case 'm':
8474         do_debug_macinfo = 1;
8475         break;
8476
8477       case 's':
8478         do_debug_str = 1;
8479         break;
8480
8481       case 'o':
8482         do_debug_loc = 1;
8483         break;
8484
8485       default:
8486         warn (_("Unrecognized debug option '%s'\n"), letters);
8487         break;
8488       }
8489 }
8490
8491 void
8492 dwarf_select_sections_all (void)
8493 {
8494   do_debug_info = 1;
8495   do_debug_abbrevs = 1;
8496   do_debug_lines = FLAG_DEBUG_LINES_RAW;
8497   do_debug_pubnames = 1;
8498   do_debug_pubtypes = 1;
8499   do_debug_aranges = 1;
8500   do_debug_ranges = 1;
8501   do_debug_frames = 1;
8502   do_debug_macinfo = 1;
8503   do_debug_str = 1;
8504   do_debug_loc = 1;
8505   do_gdb_index = 1;
8506   do_trace_info = 1;
8507   do_trace_abbrevs = 1;
8508   do_trace_aranges = 1;
8509   do_debug_addr = 1;
8510   do_debug_cu_index = 1;
8511 }
8512
8513 struct dwarf_section_display debug_displays[] =
8514 {
8515   { { ".debug_abbrev",      ".zdebug_abbrev",   NULL, NULL, 0, 0, 0, NULL, 0, NULL },
8516     display_debug_abbrev,   &do_debug_abbrevs,  FALSE },
8517   { { ".debug_aranges",     ".zdebug_aranges",  NULL, NULL, 0, 0, 0, NULL, 0, NULL },
8518     display_debug_aranges,  &do_debug_aranges,  TRUE },
8519   { { ".debug_frame",       ".zdebug_frame",    NULL, NULL, 0, 0, 0, NULL, 0, NULL },
8520     display_debug_frames,   &do_debug_frames,   TRUE },
8521   { { ".debug_info",        ".zdebug_info",     NULL, NULL, 0, 0, abbrev, NULL, 0, NULL },
8522     display_debug_info,     &do_debug_info,     TRUE },
8523   { { ".debug_line",        ".zdebug_line",     NULL, NULL, 0, 0, 0, NULL, 0, NULL },
8524     display_debug_lines,    &do_debug_lines,    TRUE },
8525   { { ".debug_pubnames",    ".zdebug_pubnames", NULL, NULL, 0, 0, 0, NULL, 0, NULL },
8526     display_debug_pubnames, &do_debug_pubnames, FALSE },
8527   { { ".debug_gnu_pubnames", ".zdebug_gnu_pubnames", NULL, NULL, 0, 0, 0, NULL, 0, NULL },
8528     display_debug_gnu_pubnames, &do_debug_pubnames, FALSE },
8529   { { ".eh_frame",          "",                 NULL, NULL, 0, 0, 0, NULL, 0, NULL },
8530     display_debug_frames,   &do_debug_frames,   TRUE },
8531   { { ".debug_macinfo",     ".zdebug_macinfo",  NULL, NULL, 0, 0, 0, NULL, 0, NULL },
8532     display_debug_macinfo,  &do_debug_macinfo,  FALSE },
8533   { { ".debug_macro",       ".zdebug_macro",    NULL, NULL, 0, 0, 0, NULL, 0, NULL },
8534     display_debug_macro,    &do_debug_macinfo,  TRUE },
8535   { { ".debug_str",         ".zdebug_str",      NULL, NULL, 0, 0, 0, NULL, 0, NULL },
8536     display_debug_str,      &do_debug_str,      FALSE },
8537   { { ".debug_line_str",    ".zdebug_line_str", NULL, NULL, 0, 0, 0, NULL, 0, NULL },
8538     display_debug_str,      &do_debug_str,      FALSE },
8539   { { ".debug_loc",         ".zdebug_loc",      NULL, NULL, 0, 0, 0, NULL, 0, NULL },
8540     display_debug_loc,      &do_debug_loc,      TRUE },
8541   { { ".debug_loclists",    ".zdebug_loclists", NULL, NULL, 0, 0, 0, NULL, 0, NULL },
8542     display_debug_loc,      &do_debug_loc,      TRUE },
8543   { { ".debug_pubtypes",    ".zdebug_pubtypes", NULL, NULL, 0, 0, 0, NULL, 0, NULL },
8544     display_debug_pubnames, &do_debug_pubtypes, FALSE },
8545   { { ".debug_gnu_pubtypes", ".zdebug_gnu_pubtypes", NULL, NULL, 0, 0, 0, NULL, 0, NULL },
8546     display_debug_gnu_pubnames, &do_debug_pubtypes, FALSE },
8547   { { ".debug_ranges",      ".zdebug_ranges",   NULL, NULL, 0, 0, 0, NULL, 0, NULL },
8548     display_debug_ranges,   &do_debug_ranges,   TRUE },
8549   { { ".debug_rnglists",    ".zdebug_rnglists", NULL, NULL, 0, 0, 0, NULL, 0, NULL },
8550     display_debug_ranges,   &do_debug_ranges,   TRUE },
8551   { { ".debug_static_func", ".zdebug_static_func", NULL, NULL, 0, 0, 0, NULL, 0, NULL },
8552     display_debug_not_supported, NULL,          FALSE },
8553   { { ".debug_static_vars", ".zdebug_static_vars", NULL, NULL, 0, 0, 0, NULL, 0, NULL },
8554     display_debug_not_supported, NULL,          FALSE },
8555   { { ".debug_types",       ".zdebug_types",    NULL, NULL, 0, 0, abbrev, NULL, 0, NULL },
8556     display_debug_types,    &do_debug_info,     TRUE },
8557   { { ".debug_weaknames",   ".zdebug_weaknames", NULL, NULL, 0, 0, 0, NULL, 0, NULL },
8558     display_debug_not_supported, NULL,          FALSE },
8559   { { ".gdb_index",         "",                 NULL, NULL, 0, 0, 0, NULL, 0, NULL },
8560     display_gdb_index,      &do_gdb_index,      FALSE },
8561   { { ".trace_info",        "",                 NULL, NULL, 0, 0, trace_abbrev, NULL, 0, NULL },
8562     display_trace_info,     &do_trace_info,     TRUE },
8563   { { ".trace_abbrev",      "",                 NULL, NULL, 0, 0, 0, NULL, 0, NULL },
8564     display_debug_abbrev,   &do_trace_abbrevs,  FALSE },
8565   { { ".trace_aranges",     "",                 NULL, NULL, 0, 0, 0, NULL, 0, NULL },
8566     display_debug_aranges,  &do_trace_aranges,  FALSE },
8567   { { ".debug_info.dwo",    ".zdebug_info.dwo", NULL, NULL, 0, 0, abbrev_dwo, NULL, 0, NULL },
8568     display_debug_info,     &do_debug_info,     TRUE },
8569   { { ".debug_abbrev.dwo",  ".zdebug_abbrev.dwo", NULL, NULL, 0, 0, 0, NULL, 0, NULL },
8570     display_debug_abbrev,   &do_debug_abbrevs,  FALSE },
8571   { { ".debug_types.dwo",   ".zdebug_types.dwo", NULL, NULL, 0, 0, abbrev_dwo, NULL, 0, NULL },
8572     display_debug_types,    &do_debug_info,     TRUE },
8573   { { ".debug_line.dwo",    ".zdebug_line.dwo", NULL, NULL, 0, 0, 0, NULL, 0, NULL },
8574     display_debug_lines,    &do_debug_lines,    TRUE },
8575   { { ".debug_loc.dwo",     ".zdebug_loc.dwo",  NULL, NULL, 0, 0, 0, NULL, 0, NULL },
8576     display_debug_loc,      &do_debug_loc,      TRUE },
8577   { { ".debug_macro.dwo",   ".zdebug_macro.dwo", NULL, NULL, 0, 0, 0, NULL, 0, NULL },
8578     display_debug_macro,    &do_debug_macinfo,  TRUE },
8579   { { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo", NULL, NULL, 0, 0, 0, NULL, 0, NULL },
8580     display_debug_macinfo,  &do_debug_macinfo,  FALSE },
8581   { { ".debug_str.dwo",     ".zdebug_str.dwo",  NULL, NULL, 0, 0, 0, NULL, 0, NULL },
8582     display_debug_str,      &do_debug_str,      TRUE },
8583   { { ".debug_str_offsets", ".zdebug_str_offsets", NULL, NULL, 0, 0, 0, NULL, 0, NULL },
8584     display_debug_str_offsets, NULL,            FALSE },
8585   { { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo", NULL, NULL, 0, 0, 0, NULL, 0, NULL },
8586     display_debug_str_offsets, NULL,            FALSE },
8587   { { ".debug_addr",        ".zdebug_addr",     NULL, NULL, 0, 0, 0, NULL, 0, NULL },
8588     display_debug_addr,     &do_debug_addr,     TRUE },
8589   { { ".debug_cu_index",    "",                 NULL, NULL, 0, 0, 0, NULL, 0, NULL },
8590     display_cu_index,       &do_debug_cu_index, FALSE },
8591   { { ".debug_tu_index",    "",                 NULL, NULL, 0, 0, 0, NULL, 0, NULL },
8592     display_cu_index,       &do_debug_cu_index, FALSE },
8593 };
8594
8595 /* A static assertion.  */
8596 extern int debug_displays_assert[ARRAY_SIZE (debug_displays) == max ? 1 : -1];