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