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