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