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