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