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