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