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