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