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