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