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