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