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