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