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