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