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