Merge branch 'vendor/XZ'
[dragonfly.git] / contrib / binutils-2.25 / binutils / dwarf.c
1 /* dwarf.c -- display DWARF contents of a BFD binary file
2    Copyright (C) 2005-2014 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
32 static const char *regname (unsigned int regno, int row);
33
34 static int have_frame_base;
35 static int need_base_address;
36
37 static unsigned int last_pointer_size = 0;
38 static int warned_about_missing_comp_units = FALSE;
39
40 static unsigned int num_debug_info_entries = 0;
41 static debug_info *debug_information = NULL;
42 /* Special value for num_debug_info_entries to indicate
43    that the .debug_info section could not be loaded/parsed.  */
44 #define DEBUG_INFO_UNAVAILABLE  (unsigned int) -1
45
46 int eh_addr_size;
47
48 int do_debug_info;
49 int do_debug_abbrevs;
50 int do_debug_lines;
51 int do_debug_pubnames;
52 int do_debug_pubtypes;
53 int do_debug_aranges;
54 int do_debug_ranges;
55 int do_debug_frames;
56 int do_debug_frames_interp;
57 int do_debug_macinfo;
58 int do_debug_str;
59 int do_debug_loc;
60 int do_gdb_index;
61 int do_trace_info;
62 int do_trace_abbrevs;
63 int do_trace_aranges;
64 int do_debug_addr;
65 int do_debug_cu_index;
66 int do_wide;
67
68 int dwarf_cutoff_level = -1;
69 unsigned long dwarf_start_die;
70
71 int dwarf_check = 0;
72
73 /* Collection of CU/TU section sets from .debug_cu_index and .debug_tu_index
74    sections.  For version 1 package files, each set is stored in SHNDX_POOL
75    as a zero-terminated list of section indexes comprising one set of debug
76    sections from a .dwo file.  */
77
78 static int cu_tu_indexes_read = 0;
79 static unsigned int *shndx_pool = NULL;
80 static unsigned int shndx_pool_size = 0;
81 static unsigned int shndx_pool_used = 0;
82
83 /* For version 2 package files, each set contains an array of section offsets
84    and an array of section sizes, giving the offset and size of the
85    contribution from a CU or TU within one of the debug sections.
86    When displaying debug info from a package file, we need to use these
87    tables to locate the corresponding contributions to each section.  */
88
89 struct cu_tu_set
90 {
91   uint64_t signature;
92   dwarf_vma section_offsets[DW_SECT_MAX];
93   size_t section_sizes[DW_SECT_MAX];
94 };
95
96 static int cu_count = 0;
97 static int tu_count = 0;
98 static struct cu_tu_set *cu_sets = NULL;
99 static struct cu_tu_set *tu_sets = NULL;
100
101 static void load_cu_tu_indexes (void *file);
102
103 /* Values for do_debug_lines.  */
104 #define FLAG_DEBUG_LINES_RAW     1
105 #define FLAG_DEBUG_LINES_DECODED 2
106
107 static int
108 size_of_encoded_value (int encoding)
109 {
110   switch (encoding & 0x7)
111     {
112     default:    /* ??? */
113     case 0:     return eh_addr_size;
114     case 2:     return 2;
115     case 3:     return 4;
116     case 4:     return 8;
117     }
118 }
119
120 static dwarf_vma
121 get_encoded_value (unsigned char **pdata,
122                    int encoding,
123                    struct dwarf_section *section,
124                    unsigned char * end)
125 {
126   unsigned char * data = * pdata;
127   int size = size_of_encoded_value (encoding);
128   dwarf_vma val;
129
130   if (data + size >= end)
131     {
132       warn (_("Encoded value extends past end of section\n"));
133       * pdata = end;
134       return 0;
135     }
136
137   if (encoding & DW_EH_PE_signed)
138     val = byte_get_signed (data, size);
139   else
140     val = byte_get (data, size);
141
142   if ((encoding & 0x70) == DW_EH_PE_pcrel)
143     val += section->address + (data - section->start);
144
145   * pdata = data + size;
146   return val;
147 }
148
149 #if __STDC_VERSION__ >= 199901L || (defined(__GNUC__) && __GNUC__ >= 2)
150 #ifndef __MINGW32__
151 #define  DWARF_VMA_FMT       "ll"
152 #define  DWARF_VMA_FMT_LONG  "%16.16llx"
153 #else
154 #define  DWARF_VMA_FMT       "I64"
155 #define  DWARF_VMA_FMT_LONG  "%016I64x"
156 #endif
157 #else
158 #define  DWARF_VMA_FMT       "l"
159 #define  DWARF_VMA_FMT_LONG  "%16.16lx"
160 #endif
161
162 /* Convert a dwarf vma value into a string.  Returns a pointer to a static
163    buffer containing the converted VALUE.  The value is converted according
164    to the printf formating character FMTCH.  If NUM_BYTES is non-zero then
165    it specifies the maximum number of bytes to be displayed in the converted
166    value and FMTCH is ignored - hex is always used.  */
167
168 static const char *
169 dwarf_vmatoa_1 (const char *fmtch, dwarf_vma value, unsigned num_bytes)
170 {
171   /* As dwarf_vmatoa is used more then once in a printf call
172      for output, we are cycling through an fixed array of pointers
173      for return address.  */
174   static int buf_pos = 0;
175   static struct dwarf_vmatoa_buf
176   {
177     char place[64];
178   } buf[16];
179   char *ret;
180
181   ret = buf[buf_pos++].place;
182   buf_pos %= ARRAY_SIZE (buf);
183
184   if (num_bytes)
185     {
186       /* Printf does not have a way of specifiying a maximum field width for an
187          integer value, so we print the full value into a buffer and then select
188          the precision we need.  */
189       snprintf (ret, sizeof (buf[0].place), DWARF_VMA_FMT_LONG, value);
190       if (num_bytes > 8)
191         num_bytes = 8;
192       return ret + (16 - 2 * num_bytes);
193     }
194   else
195     {
196       char fmt[32];
197
198       sprintf (fmt, "%%%s%s", DWARF_VMA_FMT, fmtch);
199       snprintf (ret, sizeof (buf[0].place), fmt, value);
200       return ret;
201     }
202 }
203
204 static inline const char *
205 dwarf_vmatoa (const char * fmtch, dwarf_vma value)
206 {
207   return dwarf_vmatoa_1 (fmtch, value, 0);
208 }
209
210 /* Print a dwarf_vma value (typically an address, offset or length) in
211    hexadecimal format, followed by a space.  The length of the VALUE (and
212    hence the precision displayed) is determined by the NUM_BYTES parameter.  */
213
214 static void
215 print_dwarf_vma (dwarf_vma value, unsigned num_bytes)
216 {
217   printf ("%s ", dwarf_vmatoa_1 (NULL, value, num_bytes));
218 }
219
220 /* Format a 64-bit value, given as two 32-bit values, in hex.
221    For reentrancy, this uses a buffer provided by the caller.  */
222
223 static const char *
224 dwarf_vmatoa64 (dwarf_vma hvalue, dwarf_vma lvalue, char *buf,
225                 unsigned int buf_len)
226 {
227   int len = 0;
228
229   if (hvalue == 0)
230     snprintf (buf, buf_len, "%" DWARF_VMA_FMT "x", lvalue);
231   else
232     {
233       len = snprintf (buf, buf_len, "%" DWARF_VMA_FMT "x", hvalue);
234       snprintf (buf + len, buf_len - len,
235                 "%08" DWARF_VMA_FMT "x", lvalue);
236     }
237
238   return buf;
239 }
240
241 /* Read in a LEB128 encoded value starting at address DATA.
242    If SIGN is true, return a signed LEB128 value.
243    If LENGTH_RETURN is not NULL, return in it the number of bytes read.
244    No bytes will be read at address END or beyond.  */
245
246 dwarf_vma
247 read_leb128 (unsigned char *data,
248              unsigned int *length_return,
249              bfd_boolean sign,
250              const unsigned char * const end)
251 {
252   dwarf_vma result = 0;
253   unsigned int num_read = 0;
254   unsigned int shift = 0;
255   unsigned char byte = 0;
256
257   while (data < end)
258     {
259       byte = *data++;
260       num_read++;
261
262       result |= ((dwarf_vma) (byte & 0x7f)) << shift;
263
264       shift += 7;
265       if ((byte & 0x80) == 0)
266         break;
267     }
268
269   if (length_return != NULL)
270     *length_return = num_read;
271
272   if (sign && (shift < 8 * sizeof (result)) && (byte & 0x40))
273     result |= (dwarf_vma) -1 << shift;
274
275   return result;
276 }
277
278 /* Create a signed version to avoid painful typecasts.  */
279 static inline dwarf_signed_vma
280 read_sleb128 (unsigned char * data,
281               unsigned int *  length_return,
282               const unsigned char * const end)
283 {
284   return (dwarf_signed_vma) read_leb128 (data, length_return, TRUE, end);
285 }
286
287 static inline dwarf_vma
288 read_uleb128 (unsigned char * data,
289               unsigned int *  length_return,
290               const unsigned char * const end)
291 {
292   return read_leb128 (data, length_return, FALSE, end);
293 }
294
295 #define SAFE_BYTE_GET(VAL, PTR, AMOUNT, END)    \
296   do                                            \
297     {                                           \
298       int dummy [sizeof (VAL) < (AMOUNT) ? -1 : 1] ATTRIBUTE_UNUSED ; \
299       unsigned int amount = (AMOUNT);           \
300       if (((PTR) + amount) >= (END))            \
301         {                                       \
302           if ((PTR) < (END))                    \
303             amount = (END) - (PTR);             \
304           else                                  \
305             amount = 0;                         \
306         }                                       \
307       if (amount)                               \
308         VAL = byte_get ((PTR), amount);         \
309       else                                      \
310         VAL = 0;                                \
311     }                                           \
312   while (0)
313
314 #define SAFE_BYTE_GET_AND_INC(VAL, PTR, AMOUNT, END)    \
315   do                                                    \
316     {                                                   \
317       SAFE_BYTE_GET (VAL, PTR, AMOUNT, END);            \
318       PTR += AMOUNT;                                    \
319     }                                                   \
320   while (0)
321
322 #define SAFE_SIGNED_BYTE_GET(VAL, PTR, AMOUNT, END)     \
323   do                                                    \
324     {                                                   \
325       unsigned int amount = (AMOUNT);                   \
326       if (((PTR) + amount) >= (END))                    \
327         {                                               \
328           if ((PTR) < (END))                            \
329             amount = (END) - (PTR);                     \
330           else                                          \
331             amount = 0;                                 \
332         }                                               \
333       if (amount)                                       \
334         VAL = byte_get_signed ((PTR), amount);          \
335       else                                              \
336         VAL = 0;                                        \
337     }                                                   \
338   while (0)
339
340 #define SAFE_SIGNED_BYTE_GET_AND_INC(VAL, PTR, AMOUNT, END)     \
341   do                                                            \
342     {                                                           \
343       SAFE_SIGNED_BYTE_GET (VAL, PTR, AMOUNT, END);             \
344       PTR += AMOUNT;                                            \
345     }                                                           \
346   while (0)
347
348 #define SAFE_BYTE_GET64(PTR, HIGH, LOW, END)            \
349   do                                                    \
350     {                                                   \
351       if (((PTR) + 8) <= (END))                         \
352         {                                               \
353           byte_get_64 ((PTR), (HIGH), (LOW));           \
354         }                                               \
355       else                                              \
356         {                                               \
357           * (LOW) = * (HIGH) = 0;                       \
358         }                                               \
359     }                                                   \
360   while (0)
361
362 typedef struct State_Machine_Registers
363 {
364   dwarf_vma address;
365   unsigned int file;
366   unsigned int line;
367   unsigned int column;
368   int is_stmt;
369   int basic_block;
370   unsigned char op_index;
371   unsigned char end_sequence;
372 /* This variable hold the number of the last entry seen
373    in the File Table.  */
374   unsigned int last_file_entry;
375 } SMR;
376
377 static SMR state_machine_regs;
378
379 static void
380 reset_state_machine (int is_stmt)
381 {
382   state_machine_regs.address = 0;
383   state_machine_regs.op_index = 0;
384   state_machine_regs.file = 1;
385   state_machine_regs.line = 1;
386   state_machine_regs.column = 0;
387   state_machine_regs.is_stmt = is_stmt;
388   state_machine_regs.basic_block = 0;
389   state_machine_regs.end_sequence = 0;
390   state_machine_regs.last_file_entry = 0;
391 }
392
393 /* Handled an extend line op.
394    Returns the number of bytes read.  */
395
396 static int
397 process_extended_line_op (unsigned char * data,
398                           int is_stmt,
399                           unsigned char * end)
400 {
401   unsigned char op_code;
402   unsigned int bytes_read;
403   unsigned int len;
404   unsigned char *name;
405   unsigned char *orig_data = data;
406   dwarf_vma adr;
407
408   len = read_uleb128 (data, & bytes_read, end);
409   data += bytes_read;
410
411   if (len == 0 || data == end)
412     {
413       warn (_("Badly formed extended line op encountered!\n"));
414       return bytes_read;
415     }
416
417   len += bytes_read;
418   op_code = *data++;
419
420   printf (_("  Extended opcode %d: "), op_code);
421
422   switch (op_code)
423     {
424     case DW_LNE_end_sequence:
425       printf (_("End of Sequence\n\n"));
426       reset_state_machine (is_stmt);
427       break;
428
429     case DW_LNE_set_address:
430       SAFE_BYTE_GET (adr, data, len - bytes_read - 1, end);
431       printf (_("set Address to 0x%s\n"), dwarf_vmatoa ("x", adr));
432       state_machine_regs.address = adr;
433       state_machine_regs.op_index = 0;
434       break;
435
436     case DW_LNE_define_file:
437       printf (_("define new File Table entry\n"));
438       printf (_("  Entry\tDir\tTime\tSize\tName\n"));
439       printf ("   %d\t", ++state_machine_regs.last_file_entry);
440
441       name = data;
442       data += strnlen ((char *) data, end - data) + 1;
443       printf ("%s\t", dwarf_vmatoa ("u", read_uleb128 (data, & bytes_read, end)));
444       data += bytes_read;
445       printf ("%s\t", dwarf_vmatoa ("u", read_uleb128 (data, & bytes_read, end)));
446       data += bytes_read;
447       printf ("%s\t", dwarf_vmatoa ("u", read_uleb128 (data, & bytes_read, end)));
448       data += bytes_read;
449       printf ("%s\n\n", name);
450
451       if (((unsigned int) (data - orig_data) != len) || data == end)
452         warn (_("DW_LNE_define_file: Bad opcode length\n"));
453       break;
454
455     case DW_LNE_set_discriminator:
456       printf (_("set Discriminator to %s\n"),
457               dwarf_vmatoa ("u", read_uleb128 (data, & bytes_read, end)));
458       break;
459
460     /* HP extensions.  */
461     case DW_LNE_HP_negate_is_UV_update:
462       printf ("DW_LNE_HP_negate_is_UV_update\n");
463       break;
464     case DW_LNE_HP_push_context:
465       printf ("DW_LNE_HP_push_context\n");
466       break;
467     case DW_LNE_HP_pop_context:
468       printf ("DW_LNE_HP_pop_context\n");
469       break;
470     case DW_LNE_HP_set_file_line_column:
471       printf ("DW_LNE_HP_set_file_line_column\n");
472       break;
473     case DW_LNE_HP_set_routine_name:
474       printf ("DW_LNE_HP_set_routine_name\n");
475       break;
476     case DW_LNE_HP_set_sequence:
477       printf ("DW_LNE_HP_set_sequence\n");
478       break;
479     case DW_LNE_HP_negate_post_semantics:
480       printf ("DW_LNE_HP_negate_post_semantics\n");
481       break;
482     case DW_LNE_HP_negate_function_exit:
483       printf ("DW_LNE_HP_negate_function_exit\n");
484       break;
485     case DW_LNE_HP_negate_front_end_logical:
486       printf ("DW_LNE_HP_negate_front_end_logical\n");
487       break;
488     case DW_LNE_HP_define_proc:
489       printf ("DW_LNE_HP_define_proc\n");
490       break;
491     case DW_LNE_HP_source_file_correlation:
492       {
493         unsigned char *edata = data + len - bytes_read - 1;
494
495         printf ("DW_LNE_HP_source_file_correlation\n");
496
497         while (data < edata)
498           {
499             unsigned int opc;
500
501             opc = read_uleb128 (data, & bytes_read, edata);
502             data += bytes_read;
503
504             switch (opc)
505               {
506               case DW_LNE_HP_SFC_formfeed:
507                 printf ("    DW_LNE_HP_SFC_formfeed\n");
508                 break;
509               case DW_LNE_HP_SFC_set_listing_line:
510                 printf ("    DW_LNE_HP_SFC_set_listing_line (%s)\n",
511                         dwarf_vmatoa ("u",
512                                       read_uleb128 (data, & bytes_read, edata)));
513                 data += bytes_read;
514                 break;
515               case DW_LNE_HP_SFC_associate:
516                 printf ("    DW_LNE_HP_SFC_associate ");
517                 printf ("(%s",
518                         dwarf_vmatoa ("u",
519                                       read_uleb128 (data, & bytes_read, edata)));
520                 data += bytes_read;
521                 printf (",%s",
522                         dwarf_vmatoa ("u",
523                                       read_uleb128 (data, & bytes_read, edata)));
524                 data += bytes_read;
525                 printf (",%s)\n",
526                         dwarf_vmatoa ("u",
527                                       read_uleb128 (data, & bytes_read, edata)));
528                 data += bytes_read;
529                 break;
530               default:
531                 printf (_("    UNKNOWN DW_LNE_HP_SFC opcode (%u)\n"), opc);
532                 data = edata;
533                 break;
534               }
535           }
536       }
537       break;
538
539     default:
540       {
541         unsigned int rlen = len - bytes_read - 1;
542
543         if (op_code >= DW_LNE_lo_user
544             /* The test against DW_LNW_hi_user is redundant due to
545                the limited range of the unsigned char data type used
546                for op_code.  */
547             /*&& op_code <= DW_LNE_hi_user*/)
548           printf (_("user defined: "));
549         else
550           printf (_("UNKNOWN: "));
551         printf (_("length %d ["), rlen);
552         for (; rlen; rlen--)
553           printf (" %02x", *data++);
554         printf ("]\n");
555       }
556       break;
557     }
558
559   return len;
560 }
561
562 static const unsigned char *
563 fetch_indirect_string (dwarf_vma offset)
564 {
565   struct dwarf_section *section = &debug_displays [str].section;
566
567   if (section->start == NULL)
568     return (const unsigned char *) _("<no .debug_str section>");
569
570   if (offset > section->size)
571     {
572       warn (_("DW_FORM_strp offset too big: %s\n"),
573             dwarf_vmatoa ("x", offset));
574       return (const unsigned char *) _("<offset is too big>");
575     }
576
577   return (const unsigned char *) section->start + offset;
578 }
579
580 static const char *
581 fetch_indexed_string (dwarf_vma idx, struct cu_tu_set *this_set,
582                       dwarf_vma offset_size, int dwo)
583 {
584   enum dwarf_section_display_enum str_sec_idx = dwo ? str_dwo : str;
585   enum dwarf_section_display_enum idx_sec_idx = dwo ? str_index_dwo : str_index;
586   struct dwarf_section *index_section = &debug_displays [idx_sec_idx].section;
587   struct dwarf_section *str_section = &debug_displays [str_sec_idx].section;
588   dwarf_vma index_offset = idx * offset_size;
589   dwarf_vma str_offset;
590
591   if (index_section->start == NULL)
592     return (dwo ? _("<no .debug_str_offsets.dwo section>")
593                 : _("<no .debug_str_offsets section>"));
594
595   if (this_set != NULL)
596     index_offset += this_set->section_offsets [DW_SECT_STR_OFFSETS];
597   if (index_offset > index_section->size)
598     {
599       warn (_("DW_FORM_GNU_str_index offset too big: %s\n"),
600             dwarf_vmatoa ("x", index_offset));
601       return _("<index offset is too big>");
602     }
603
604   if (str_section->start == NULL)
605     return (dwo ? _("<no .debug_str.dwo section>")
606                 : _("<no .debug_str section>"));
607
608   str_offset = byte_get (index_section->start + index_offset, offset_size);
609   str_offset -= str_section->address;
610   if (str_offset > str_section->size)
611     {
612       warn (_("DW_FORM_GNU_str_index indirect offset too big: %s\n"),
613             dwarf_vmatoa ("x", str_offset));
614       return _("<indirect index offset is too big>");
615     }
616
617   return (const char *) str_section->start + str_offset;
618 }
619
620 static const char *
621 fetch_indexed_value (dwarf_vma offset, dwarf_vma bytes)
622 {
623   struct dwarf_section *section = &debug_displays [debug_addr].section;
624
625   if (section->start == NULL)
626     return (_("<no .debug_addr section>"));
627
628   if (offset + bytes > section->size)
629     {
630       warn (_("Offset into section %s too big: %s\n"),
631             section->name, dwarf_vmatoa ("x", offset));
632       return "<offset too big>";
633     }
634
635   return dwarf_vmatoa ("x", byte_get (section->start + offset, bytes));
636 }
637
638
639 /* FIXME:  There are better and more efficient ways to handle
640    these structures.  For now though, I just want something that
641    is simple to implement.  */
642 typedef struct abbrev_attr
643 {
644   unsigned long attribute;
645   unsigned long form;
646   struct abbrev_attr *next;
647 }
648 abbrev_attr;
649
650 typedef struct abbrev_entry
651 {
652   unsigned long entry;
653   unsigned long tag;
654   int children;
655   struct abbrev_attr *first_attr;
656   struct abbrev_attr *last_attr;
657   struct abbrev_entry *next;
658 }
659 abbrev_entry;
660
661 static abbrev_entry *first_abbrev = NULL;
662 static abbrev_entry *last_abbrev = NULL;
663
664 static void
665 free_abbrevs (void)
666 {
667   abbrev_entry *abbrv;
668
669   for (abbrv = first_abbrev; abbrv;)
670     {
671       abbrev_entry *next_abbrev = abbrv->next;
672       abbrev_attr *attr;
673
674       for (attr = abbrv->first_attr; attr;)
675         {
676           abbrev_attr *next_attr = attr->next;
677
678           free (attr);
679           attr = next_attr;
680         }
681
682       free (abbrv);
683       abbrv = next_abbrev;
684     }
685
686   last_abbrev = first_abbrev = NULL;
687 }
688
689 static void
690 add_abbrev (unsigned long number, unsigned long tag, int children)
691 {
692   abbrev_entry *entry;
693
694   entry = (abbrev_entry *) malloc (sizeof (*entry));
695   if (entry == NULL)
696     /* ugg */
697     return;
698
699   entry->entry      = number;
700   entry->tag        = tag;
701   entry->children   = children;
702   entry->first_attr = NULL;
703   entry->last_attr  = NULL;
704   entry->next       = NULL;
705
706   if (first_abbrev == NULL)
707     first_abbrev = entry;
708   else
709     last_abbrev->next = entry;
710
711   last_abbrev = entry;
712 }
713
714 static void
715 add_abbrev_attr (unsigned long attribute, unsigned long form)
716 {
717   abbrev_attr *attr;
718
719   attr = (abbrev_attr *) malloc (sizeof (*attr));
720   if (attr == NULL)
721     /* ugg */
722     return;
723
724   attr->attribute = attribute;
725   attr->form      = form;
726   attr->next      = NULL;
727
728   if (last_abbrev->first_attr == NULL)
729     last_abbrev->first_attr = attr;
730   else
731     last_abbrev->last_attr->next = attr;
732
733   last_abbrev->last_attr = attr;
734 }
735
736 /* Processes the (partial) contents of a .debug_abbrev section.
737    Returns NULL if the end of the section was encountered.
738    Returns the address after the last byte read if the end of
739    an abbreviation set was found.  */
740
741 static unsigned char *
742 process_abbrev_section (unsigned char *start, unsigned char *end)
743 {
744   if (first_abbrev != NULL)
745     return NULL;
746
747   while (start < end)
748     {
749       unsigned int bytes_read;
750       unsigned long entry;
751       unsigned long tag;
752       unsigned long attribute;
753       int children;
754
755       entry = read_uleb128 (start, & bytes_read, end);
756       start += bytes_read;
757
758       /* A single zero is supposed to end the section according
759          to the standard.  If there's more, then signal that to
760          the caller.  */
761       if (start == end)
762         return NULL;
763       if (entry == 0)
764         return start;
765
766       tag = read_uleb128 (start, & bytes_read, end);
767       start += bytes_read;
768       if (start == end)
769         return NULL;
770
771       children = *start++;
772
773       add_abbrev (entry, tag, children);
774
775       do
776         {
777           unsigned long form;
778
779           attribute = read_uleb128 (start, & bytes_read, end);
780           start += bytes_read;
781           if (start == end)
782             break;
783
784           form = read_uleb128 (start, & bytes_read, end);
785           start += bytes_read;
786           if (start == end)
787             break;
788
789           add_abbrev_attr (attribute, form);
790         }
791       while (attribute != 0);
792     }
793
794   /* Report the missing single zero which ends the section.  */
795   error (_(".debug_abbrev section not zero terminated\n"));
796
797   return NULL;
798 }
799
800 static const char *
801 get_TAG_name (unsigned long tag)
802 {
803   const char *name = get_DW_TAG_name ((unsigned int)tag);
804
805   if (name == NULL)
806     {
807       static char buffer[100];
808
809       snprintf (buffer, sizeof (buffer), _("Unknown TAG value: %lx"), tag);
810       return buffer;
811     }
812
813   return name;
814 }
815
816 static const char *
817 get_FORM_name (unsigned long form)
818 {
819   const char *name;
820
821   if (form == 0)
822     return "DW_FORM value: 0";
823
824   name = get_DW_FORM_name (form);
825   if (name == NULL)
826     {
827       static char buffer[100];
828
829       snprintf (buffer, sizeof (buffer), _("Unknown FORM value: %lx"), form);
830       return buffer;
831     }
832
833   return name;
834 }
835
836 static unsigned char *
837 display_block (unsigned char *data,
838                dwarf_vma length,
839                const unsigned char * const end)
840 {
841   dwarf_vma maxlen;
842
843   printf (_(" %s byte block: "), dwarf_vmatoa ("u", length));
844
845   maxlen = (dwarf_vma) (end - data);
846   length = length > maxlen ? maxlen : length;
847
848   while (length --)
849     printf ("%lx ", (unsigned long) byte_get (data++, 1));
850
851   return data;
852 }
853
854 static int
855 decode_location_expression (unsigned char * data,
856                             unsigned int pointer_size,
857                             unsigned int offset_size,
858                             int dwarf_version,
859                             dwarf_vma length,
860                             dwarf_vma cu_offset,
861                             struct dwarf_section * section)
862 {
863   unsigned op;
864   unsigned int bytes_read;
865   dwarf_vma uvalue;
866   dwarf_signed_vma svalue;
867   unsigned char *end = data + length;
868   int need_frame_base = 0;
869
870   while (data < end)
871     {
872       op = *data++;
873
874       switch (op)
875         {
876         case DW_OP_addr:
877           SAFE_BYTE_GET_AND_INC (uvalue, data, pointer_size, end);
878           printf ("DW_OP_addr: %s", dwarf_vmatoa ("x", uvalue));
879           break;
880         case DW_OP_deref:
881           printf ("DW_OP_deref");
882           break;
883         case DW_OP_const1u:
884           SAFE_BYTE_GET_AND_INC (uvalue, data, 1, end);
885           printf ("DW_OP_const1u: %lu", (unsigned long) uvalue);
886           break;
887         case DW_OP_const1s:
888           SAFE_SIGNED_BYTE_GET_AND_INC (svalue, data, 1, end);
889           printf ("DW_OP_const1s: %ld", (long) svalue);
890           break;
891         case DW_OP_const2u:
892           SAFE_BYTE_GET_AND_INC (uvalue, data, 2, end);
893           printf ("DW_OP_const2u: %lu", (unsigned long) uvalue);
894           break;
895         case DW_OP_const2s:
896           SAFE_SIGNED_BYTE_GET_AND_INC (svalue, data, 2, end);
897           printf ("DW_OP_const2s: %ld", (long) svalue);
898           break;
899         case DW_OP_const4u:
900           SAFE_BYTE_GET_AND_INC (uvalue, data, 4, end);
901           printf ("DW_OP_const4u: %lu", (unsigned long) uvalue);
902           break;
903         case DW_OP_const4s:
904           SAFE_SIGNED_BYTE_GET_AND_INC (svalue, data, 4, end);
905           printf ("DW_OP_const4s: %ld", (long) svalue);
906           break;
907         case DW_OP_const8u:
908           SAFE_BYTE_GET_AND_INC (uvalue, data, 4, end);
909           printf ("DW_OP_const8u: %lu ", (unsigned long) uvalue);
910           SAFE_BYTE_GET_AND_INC (uvalue, data, 4, end);
911           printf ("%lu", (unsigned long) uvalue);
912           break;
913         case DW_OP_const8s:
914           SAFE_SIGNED_BYTE_GET_AND_INC (svalue, data, 4, end);
915           printf ("DW_OP_const8s: %ld ", (long) svalue);
916           SAFE_SIGNED_BYTE_GET_AND_INC (svalue, data, 4, end);
917           printf ("%ld", (long) svalue);
918           break;
919         case DW_OP_constu:
920           printf ("DW_OP_constu: %s",
921                   dwarf_vmatoa ("u", read_uleb128 (data, &bytes_read, end)));
922           data += bytes_read;
923           break;
924         case DW_OP_consts:
925           printf ("DW_OP_consts: %s",
926                   dwarf_vmatoa ("d", read_sleb128 (data, &bytes_read, end)));
927           data += bytes_read;
928           break;
929         case DW_OP_dup:
930           printf ("DW_OP_dup");
931           break;
932         case DW_OP_drop:
933           printf ("DW_OP_drop");
934           break;
935         case DW_OP_over:
936           printf ("DW_OP_over");
937           break;
938         case DW_OP_pick:
939           SAFE_BYTE_GET_AND_INC (uvalue, data, 1, end);
940           printf ("DW_OP_pick: %ld", (unsigned long) uvalue);
941           break;
942         case DW_OP_swap:
943           printf ("DW_OP_swap");
944           break;
945         case DW_OP_rot:
946           printf ("DW_OP_rot");
947           break;
948         case DW_OP_xderef:
949           printf ("DW_OP_xderef");
950           break;
951         case DW_OP_abs:
952           printf ("DW_OP_abs");
953           break;
954         case DW_OP_and:
955           printf ("DW_OP_and");
956           break;
957         case DW_OP_div:
958           printf ("DW_OP_div");
959           break;
960         case DW_OP_minus:
961           printf ("DW_OP_minus");
962           break;
963         case DW_OP_mod:
964           printf ("DW_OP_mod");
965           break;
966         case DW_OP_mul:
967           printf ("DW_OP_mul");
968           break;
969         case DW_OP_neg:
970           printf ("DW_OP_neg");
971           break;
972         case DW_OP_not:
973           printf ("DW_OP_not");
974           break;
975         case DW_OP_or:
976           printf ("DW_OP_or");
977           break;
978         case DW_OP_plus:
979           printf ("DW_OP_plus");
980           break;
981         case DW_OP_plus_uconst:
982           printf ("DW_OP_plus_uconst: %s",
983                   dwarf_vmatoa ("u", read_uleb128 (data, &bytes_read, end)));
984           data += bytes_read;
985           break;
986         case DW_OP_shl:
987           printf ("DW_OP_shl");
988           break;
989         case DW_OP_shr:
990           printf ("DW_OP_shr");
991           break;
992         case DW_OP_shra:
993           printf ("DW_OP_shra");
994           break;
995         case DW_OP_xor:
996           printf ("DW_OP_xor");
997           break;
998         case DW_OP_bra:
999           SAFE_SIGNED_BYTE_GET_AND_INC (svalue, data, 2, end);
1000           printf ("DW_OP_bra: %ld", (long) svalue);
1001           break;
1002         case DW_OP_eq:
1003           printf ("DW_OP_eq");
1004           break;
1005         case DW_OP_ge:
1006           printf ("DW_OP_ge");
1007           break;
1008         case DW_OP_gt:
1009           printf ("DW_OP_gt");
1010           break;
1011         case DW_OP_le:
1012           printf ("DW_OP_le");
1013           break;
1014         case DW_OP_lt:
1015           printf ("DW_OP_lt");
1016           break;
1017         case DW_OP_ne:
1018           printf ("DW_OP_ne");
1019           break;
1020         case DW_OP_skip:
1021           SAFE_SIGNED_BYTE_GET_AND_INC (svalue, data, 2, end);
1022           printf ("DW_OP_skip: %ld", (long) svalue);
1023           break;
1024
1025         case DW_OP_lit0:
1026         case DW_OP_lit1:
1027         case DW_OP_lit2:
1028         case DW_OP_lit3:
1029         case DW_OP_lit4:
1030         case DW_OP_lit5:
1031         case DW_OP_lit6:
1032         case DW_OP_lit7:
1033         case DW_OP_lit8:
1034         case DW_OP_lit9:
1035         case DW_OP_lit10:
1036         case DW_OP_lit11:
1037         case DW_OP_lit12:
1038         case DW_OP_lit13:
1039         case DW_OP_lit14:
1040         case DW_OP_lit15:
1041         case DW_OP_lit16:
1042         case DW_OP_lit17:
1043         case DW_OP_lit18:
1044         case DW_OP_lit19:
1045         case DW_OP_lit20:
1046         case DW_OP_lit21:
1047         case DW_OP_lit22:
1048         case DW_OP_lit23:
1049         case DW_OP_lit24:
1050         case DW_OP_lit25:
1051         case DW_OP_lit26:
1052         case DW_OP_lit27:
1053         case DW_OP_lit28:
1054         case DW_OP_lit29:
1055         case DW_OP_lit30:
1056         case DW_OP_lit31:
1057           printf ("DW_OP_lit%d", op - DW_OP_lit0);
1058           break;
1059
1060         case DW_OP_reg0:
1061         case DW_OP_reg1:
1062         case DW_OP_reg2:
1063         case DW_OP_reg3:
1064         case DW_OP_reg4:
1065         case DW_OP_reg5:
1066         case DW_OP_reg6:
1067         case DW_OP_reg7:
1068         case DW_OP_reg8:
1069         case DW_OP_reg9:
1070         case DW_OP_reg10:
1071         case DW_OP_reg11:
1072         case DW_OP_reg12:
1073         case DW_OP_reg13:
1074         case DW_OP_reg14:
1075         case DW_OP_reg15:
1076         case DW_OP_reg16:
1077         case DW_OP_reg17:
1078         case DW_OP_reg18:
1079         case DW_OP_reg19:
1080         case DW_OP_reg20:
1081         case DW_OP_reg21:
1082         case DW_OP_reg22:
1083         case DW_OP_reg23:
1084         case DW_OP_reg24:
1085         case DW_OP_reg25:
1086         case DW_OP_reg26:
1087         case DW_OP_reg27:
1088         case DW_OP_reg28:
1089         case DW_OP_reg29:
1090         case DW_OP_reg30:
1091         case DW_OP_reg31:
1092           printf ("DW_OP_reg%d (%s)", op - DW_OP_reg0,
1093                   regname (op - DW_OP_reg0, 1));
1094           break;
1095
1096         case DW_OP_breg0:
1097         case DW_OP_breg1:
1098         case DW_OP_breg2:
1099         case DW_OP_breg3:
1100         case DW_OP_breg4:
1101         case DW_OP_breg5:
1102         case DW_OP_breg6:
1103         case DW_OP_breg7:
1104         case DW_OP_breg8:
1105         case DW_OP_breg9:
1106         case DW_OP_breg10:
1107         case DW_OP_breg11:
1108         case DW_OP_breg12:
1109         case DW_OP_breg13:
1110         case DW_OP_breg14:
1111         case DW_OP_breg15:
1112         case DW_OP_breg16:
1113         case DW_OP_breg17:
1114         case DW_OP_breg18:
1115         case DW_OP_breg19:
1116         case DW_OP_breg20:
1117         case DW_OP_breg21:
1118         case DW_OP_breg22:
1119         case DW_OP_breg23:
1120         case DW_OP_breg24:
1121         case DW_OP_breg25:
1122         case DW_OP_breg26:
1123         case DW_OP_breg27:
1124         case DW_OP_breg28:
1125         case DW_OP_breg29:
1126         case DW_OP_breg30:
1127         case DW_OP_breg31:
1128           printf ("DW_OP_breg%d (%s): %s",
1129                   op - DW_OP_breg0,
1130                   regname (op - DW_OP_breg0, 1),
1131                   dwarf_vmatoa ("d", read_sleb128 (data, &bytes_read, end)));
1132           data += bytes_read;
1133           break;
1134
1135         case DW_OP_regx:
1136           uvalue = read_uleb128 (data, &bytes_read, end);
1137           data += bytes_read;
1138           printf ("DW_OP_regx: %s (%s)",
1139                   dwarf_vmatoa ("u", uvalue), regname (uvalue, 1));
1140           break;
1141         case DW_OP_fbreg:
1142           need_frame_base = 1;
1143           printf ("DW_OP_fbreg: %s",
1144                   dwarf_vmatoa ("d", read_sleb128 (data, &bytes_read, end)));
1145           data += bytes_read;
1146           break;
1147         case DW_OP_bregx:
1148           uvalue = read_uleb128 (data, &bytes_read, end);
1149           data += bytes_read;
1150           printf ("DW_OP_bregx: %s (%s) %s",
1151                   dwarf_vmatoa ("u", uvalue), regname (uvalue, 1),
1152                   dwarf_vmatoa ("d", read_sleb128 (data, &bytes_read, end)));
1153           data += bytes_read;
1154           break;
1155         case DW_OP_piece:
1156           printf ("DW_OP_piece: %s",
1157                   dwarf_vmatoa ("u", read_uleb128 (data, &bytes_read, end)));
1158           data += bytes_read;
1159           break;
1160         case DW_OP_deref_size:
1161           SAFE_BYTE_GET_AND_INC (uvalue, data, 1, end);
1162           printf ("DW_OP_deref_size: %ld", (long) uvalue);
1163           break;
1164         case DW_OP_xderef_size:
1165           SAFE_BYTE_GET_AND_INC (uvalue, data, 1, end);
1166           printf ("DW_OP_xderef_size: %ld", (long) uvalue);
1167           break;
1168         case DW_OP_nop:
1169           printf ("DW_OP_nop");
1170           break;
1171
1172           /* DWARF 3 extensions.  */
1173         case DW_OP_push_object_address:
1174           printf ("DW_OP_push_object_address");
1175           break;
1176         case DW_OP_call2:
1177           /* XXX: Strictly speaking for 64-bit DWARF3 files
1178              this ought to be an 8-byte wide computation.  */
1179           SAFE_SIGNED_BYTE_GET_AND_INC (svalue, data, 2, end);
1180           printf ("DW_OP_call2: <0x%s>",
1181                   dwarf_vmatoa ("x", svalue + cu_offset));
1182           break;
1183         case DW_OP_call4:
1184           /* XXX: Strictly speaking for 64-bit DWARF3 files
1185              this ought to be an 8-byte wide computation.  */
1186           SAFE_SIGNED_BYTE_GET_AND_INC (svalue, data, 4, end);
1187           printf ("DW_OP_call4: <0x%s>",
1188                   dwarf_vmatoa ("x", svalue + cu_offset));
1189           break;
1190         case DW_OP_call_ref:
1191           /* XXX: Strictly speaking for 64-bit DWARF3 files
1192              this ought to be an 8-byte wide computation.  */
1193           if (dwarf_version == -1)
1194             {
1195               printf (_("(DW_OP_call_ref in frame info)"));
1196               /* No way to tell where the next op is, so just bail.  */
1197               return need_frame_base;
1198             }
1199           if (dwarf_version == 2)
1200             {
1201               SAFE_BYTE_GET_AND_INC (uvalue, data, pointer_size, end);
1202             }
1203           else
1204             {
1205               SAFE_BYTE_GET_AND_INC (uvalue, data, offset_size, end);
1206             }
1207           printf ("DW_OP_call_ref: <0x%s>", dwarf_vmatoa ("x", uvalue));
1208           break;
1209         case DW_OP_form_tls_address:
1210           printf ("DW_OP_form_tls_address");
1211           break;
1212         case DW_OP_call_frame_cfa:
1213           printf ("DW_OP_call_frame_cfa");
1214           break;
1215         case DW_OP_bit_piece:
1216           printf ("DW_OP_bit_piece: ");
1217           printf (_("size: %s "),
1218                   dwarf_vmatoa ("u", read_uleb128 (data, &bytes_read, end)));
1219           data += bytes_read;
1220           printf (_("offset: %s "),
1221                   dwarf_vmatoa ("u", read_uleb128 (data, &bytes_read, end)));
1222           data += bytes_read;
1223           break;
1224
1225           /* DWARF 4 extensions.  */
1226         case DW_OP_stack_value:
1227           printf ("DW_OP_stack_value");
1228           break;
1229
1230         case DW_OP_implicit_value:
1231           printf ("DW_OP_implicit_value");
1232           uvalue = read_uleb128 (data, &bytes_read, end);
1233           data += bytes_read;
1234           display_block (data, uvalue, end);
1235           data += uvalue;
1236           break;
1237
1238           /* GNU extensions.  */
1239         case DW_OP_GNU_push_tls_address:
1240           printf (_("DW_OP_GNU_push_tls_address or DW_OP_HP_unknown"));
1241           break;
1242         case DW_OP_GNU_uninit:
1243           printf ("DW_OP_GNU_uninit");
1244           /* FIXME: Is there data associated with this OP ?  */
1245           break;
1246         case DW_OP_GNU_encoded_addr:
1247           {
1248             int encoding;
1249             dwarf_vma addr;
1250
1251             encoding = *data++;
1252             addr = get_encoded_value (&data, encoding, section, end);
1253
1254             printf ("DW_OP_GNU_encoded_addr: fmt:%02x addr:", encoding);
1255             print_dwarf_vma (addr, pointer_size);
1256           }
1257           break;
1258         case DW_OP_GNU_implicit_pointer:
1259           /* XXX: Strictly speaking for 64-bit DWARF3 files
1260              this ought to be an 8-byte wide computation.  */
1261           if (dwarf_version == -1)
1262             {
1263               printf (_("(DW_OP_GNU_implicit_pointer in frame info)"));
1264               /* No way to tell where the next op is, so just bail.  */
1265               return need_frame_base;
1266             }
1267           if (dwarf_version == 2)
1268             {
1269               SAFE_BYTE_GET_AND_INC (uvalue, data, pointer_size, end);
1270             }
1271           else
1272             {
1273               SAFE_BYTE_GET_AND_INC (uvalue, data, offset_size, end);
1274             }
1275           printf ("DW_OP_GNU_implicit_pointer: <0x%s> %s",
1276                   dwarf_vmatoa ("x", uvalue),
1277                   dwarf_vmatoa ("d", read_sleb128 (data,
1278                                                    &bytes_read, end)));
1279           data += bytes_read;
1280           break;
1281         case DW_OP_GNU_entry_value:
1282           uvalue = read_uleb128 (data, &bytes_read, end);
1283           data += bytes_read;
1284           printf ("DW_OP_GNU_entry_value: (");
1285           if (decode_location_expression (data, pointer_size, offset_size,
1286                                           dwarf_version, uvalue,
1287                                           cu_offset, section))
1288             need_frame_base = 1;
1289           putchar (')');
1290           data += uvalue;
1291           break;
1292         case DW_OP_GNU_const_type:
1293           uvalue = read_uleb128 (data, &bytes_read, end);
1294           data += bytes_read;
1295           printf ("DW_OP_GNU_const_type: <0x%s> ",
1296                   dwarf_vmatoa ("x", cu_offset + uvalue));
1297           SAFE_BYTE_GET_AND_INC (uvalue, data, 1, end);
1298           display_block (data, uvalue, end);
1299           data += uvalue;
1300           break;
1301         case DW_OP_GNU_regval_type:
1302           uvalue = read_uleb128 (data, &bytes_read, end);
1303           data += bytes_read;
1304           printf ("DW_OP_GNU_regval_type: %s (%s)",
1305                   dwarf_vmatoa ("u", uvalue), regname (uvalue, 1));
1306           uvalue = read_uleb128 (data, &bytes_read, end);
1307           data += bytes_read;
1308           printf (" <0x%s>", dwarf_vmatoa ("x", cu_offset + uvalue));
1309           break;
1310         case DW_OP_GNU_deref_type:
1311           SAFE_BYTE_GET_AND_INC (uvalue, data, 1, end);
1312           printf ("DW_OP_GNU_deref_type: %ld", (long) uvalue);
1313           uvalue = read_uleb128 (data, &bytes_read, end);
1314           data += bytes_read;
1315           printf (" <0x%s>", dwarf_vmatoa ("x", cu_offset + uvalue));
1316           break;
1317         case DW_OP_GNU_convert:
1318           uvalue = read_uleb128 (data, &bytes_read, end);
1319           data += bytes_read;
1320           printf ("DW_OP_GNU_convert <0x%s>",
1321                   dwarf_vmatoa ("x", uvalue ? cu_offset + uvalue : 0));
1322           break;
1323         case DW_OP_GNU_reinterpret:
1324           uvalue = read_uleb128 (data, &bytes_read, end);
1325           data += bytes_read;
1326           printf ("DW_OP_GNU_reinterpret <0x%s>",
1327                   dwarf_vmatoa ("x", uvalue ? cu_offset + uvalue : 0));
1328           break;
1329         case DW_OP_GNU_parameter_ref:
1330           SAFE_BYTE_GET_AND_INC (uvalue, data, 4, end);
1331           printf ("DW_OP_GNU_parameter_ref: <0x%s>",
1332                   dwarf_vmatoa ("x", cu_offset + uvalue));
1333           break;
1334         case DW_OP_GNU_addr_index:
1335           uvalue = read_uleb128 (data, &bytes_read, end);
1336           data += bytes_read;
1337           printf ("DW_OP_GNU_addr_index <0x%s>", dwarf_vmatoa ("x", uvalue));
1338           break;
1339         case DW_OP_GNU_const_index:
1340           uvalue = read_uleb128 (data, &bytes_read, end);
1341           data += bytes_read;
1342           printf ("DW_OP_GNU_const_index <0x%s>", dwarf_vmatoa ("x", uvalue));
1343           break;
1344
1345           /* HP extensions.  */
1346         case DW_OP_HP_is_value:
1347           printf ("DW_OP_HP_is_value");
1348           /* FIXME: Is there data associated with this OP ?  */
1349           break;
1350         case DW_OP_HP_fltconst4:
1351           printf ("DW_OP_HP_fltconst4");
1352           /* FIXME: Is there data associated with this OP ?  */
1353           break;
1354         case DW_OP_HP_fltconst8:
1355           printf ("DW_OP_HP_fltconst8");
1356           /* FIXME: Is there data associated with this OP ?  */
1357           break;
1358         case DW_OP_HP_mod_range:
1359           printf ("DW_OP_HP_mod_range");
1360           /* FIXME: Is there data associated with this OP ?  */
1361           break;
1362         case DW_OP_HP_unmod_range:
1363           printf ("DW_OP_HP_unmod_range");
1364           /* FIXME: Is there data associated with this OP ?  */
1365           break;
1366         case DW_OP_HP_tls:
1367           printf ("DW_OP_HP_tls");
1368           /* FIXME: Is there data associated with this OP ?  */
1369           break;
1370
1371           /* PGI (STMicroelectronics) extensions.  */
1372         case DW_OP_PGI_omp_thread_num:
1373           /* Pushes the thread number for the current thread as it would be
1374              returned by the standard OpenMP library function:
1375              omp_get_thread_num().  The "current thread" is the thread for
1376              which the expression is being evaluated.  */
1377           printf ("DW_OP_PGI_omp_thread_num");
1378           break;
1379
1380         default:
1381           if (op >= DW_OP_lo_user
1382               && op <= DW_OP_hi_user)
1383             printf (_("(User defined location op)"));
1384           else
1385             printf (_("(Unknown location op)"));
1386           /* No way to tell where the next op is, so just bail.  */
1387           return need_frame_base;
1388         }
1389
1390       /* Separate the ops.  */
1391       if (data < end)
1392         printf ("; ");
1393     }
1394
1395   return need_frame_base;
1396 }
1397
1398 /* Find the CU or TU set corresponding to the given CU_OFFSET.
1399    This is used for DWARF package files.  */
1400
1401 static struct cu_tu_set *
1402 find_cu_tu_set_v2 (dwarf_vma cu_offset, int do_types)
1403 {
1404   struct cu_tu_set *p;
1405   unsigned int nsets;
1406   unsigned int dw_sect;
1407
1408   if (do_types)
1409     {
1410       p = tu_sets;
1411       nsets = tu_count;
1412       dw_sect = DW_SECT_TYPES;
1413     }
1414   else
1415     {
1416       p = cu_sets;
1417       nsets = cu_count;
1418       dw_sect = DW_SECT_INFO;
1419     }
1420   while (nsets > 0)
1421     {
1422       if (p->section_offsets [dw_sect] == cu_offset)
1423         return p;
1424       p++;
1425       nsets--;
1426     }
1427   return NULL;
1428 }
1429
1430 /* Add INC to HIGH_BITS:LOW_BITS.  */
1431 static void
1432 add64 (dwarf_vma * high_bits, dwarf_vma * low_bits, dwarf_vma inc)
1433 {
1434   dwarf_vma tmp = * low_bits;
1435
1436   tmp += inc;
1437
1438   /* FIXME: There is probably a better way of handling this:
1439
1440      We need to cope with dwarf_vma being a 32-bit or 64-bit
1441      type.  Plus regardless of its size LOW_BITS is meant to
1442      only hold 32-bits, so if there is overflow or wrap around
1443      we must propagate into HIGH_BITS.  */
1444   if (tmp < * low_bits)
1445     {
1446       ++ * high_bits;
1447     }
1448   else if (sizeof (tmp) > 8
1449            && (tmp >> 31) > 1)
1450     {
1451       ++ * high_bits;
1452       tmp &= 0xFFFFFFFF;
1453     }
1454
1455   * low_bits = tmp;
1456 }
1457
1458 static unsigned char *
1459 read_and_display_attr_value (unsigned long attribute,
1460                              unsigned long form,
1461                              unsigned char * data,
1462                              unsigned char * end,
1463                              dwarf_vma cu_offset,
1464                              dwarf_vma pointer_size,
1465                              dwarf_vma offset_size,
1466                              int dwarf_version,
1467                              debug_info * debug_info_p,
1468                              int do_loc,
1469                              struct dwarf_section * section,
1470                              struct cu_tu_set * this_set)
1471 {
1472   dwarf_vma uvalue = 0;
1473   unsigned char *block_start = NULL;
1474   unsigned char * orig_data = data;
1475   unsigned int bytes_read;
1476
1477   if (data > end || (data == end && form != DW_FORM_flag_present))
1478     {
1479       warn (_("Corrupt attribute\n"));
1480       return data;
1481     }
1482
1483   switch (form)
1484     {
1485     default:
1486       break;
1487
1488     case DW_FORM_ref_addr:
1489       if (dwarf_version == 2)
1490         SAFE_BYTE_GET_AND_INC (uvalue, data, pointer_size, end);
1491       else if (dwarf_version == 3 || dwarf_version == 4)
1492         SAFE_BYTE_GET_AND_INC (uvalue, data, offset_size, end);
1493       else
1494         error (_("Internal error: DWARF version is not 2, 3 or 4.\n"));
1495
1496       break;
1497
1498     case DW_FORM_addr:
1499       SAFE_BYTE_GET_AND_INC (uvalue, data, pointer_size, end);
1500       break;
1501
1502     case DW_FORM_strp:
1503     case DW_FORM_sec_offset:
1504     case DW_FORM_GNU_ref_alt:
1505     case DW_FORM_GNU_strp_alt:
1506       SAFE_BYTE_GET_AND_INC (uvalue, data, offset_size, end);
1507       break;
1508
1509     case DW_FORM_flag_present:
1510       uvalue = 1;
1511       break;
1512
1513     case DW_FORM_ref1:
1514     case DW_FORM_flag:
1515     case DW_FORM_data1:
1516       SAFE_BYTE_GET_AND_INC (uvalue, data, 1, end);
1517       break;
1518
1519     case DW_FORM_ref2:
1520     case DW_FORM_data2:
1521       SAFE_BYTE_GET_AND_INC (uvalue, data, 2, end);
1522       break;
1523
1524     case DW_FORM_ref4:
1525     case DW_FORM_data4:
1526       SAFE_BYTE_GET_AND_INC (uvalue, data, 4, end);
1527       break;
1528
1529     case DW_FORM_sdata:
1530       uvalue = read_sleb128 (data, & bytes_read, end);
1531       data += bytes_read;
1532       break;
1533
1534     case DW_FORM_GNU_str_index:
1535       uvalue = read_uleb128 (data, & bytes_read, end);
1536       data += bytes_read;
1537       break;
1538
1539     case DW_FORM_ref_udata:
1540     case DW_FORM_udata:
1541       uvalue = read_uleb128 (data, & bytes_read, end);
1542       data += bytes_read;
1543       break;
1544
1545     case DW_FORM_indirect:
1546       form = read_uleb128 (data, & bytes_read, end);
1547       data += bytes_read;
1548       if (!do_loc)
1549         printf (" %s", get_FORM_name (form));
1550       return read_and_display_attr_value (attribute, form, data, end,
1551                                           cu_offset, pointer_size,
1552                                           offset_size, dwarf_version,
1553                                           debug_info_p, do_loc,
1554                                           section, this_set);
1555     case DW_FORM_GNU_addr_index:
1556       uvalue = read_uleb128 (data, & bytes_read, end);
1557       data += bytes_read;
1558       break;
1559     }
1560
1561   switch (form)
1562     {
1563     case DW_FORM_ref_addr:
1564       if (!do_loc)
1565         printf (" <0x%s>", dwarf_vmatoa ("x",uvalue));
1566       break;
1567
1568     case DW_FORM_GNU_ref_alt:
1569       if (!do_loc)
1570         printf (" <alt 0x%s>", dwarf_vmatoa ("x",uvalue));
1571       break;
1572
1573     case DW_FORM_ref1:
1574     case DW_FORM_ref2:
1575     case DW_FORM_ref4:
1576     case DW_FORM_ref_udata:
1577       if (!do_loc)
1578         printf (" <0x%s>", dwarf_vmatoa ("x", uvalue + cu_offset));
1579       break;
1580
1581     case DW_FORM_data4:
1582     case DW_FORM_addr:
1583     case DW_FORM_sec_offset:
1584       if (!do_loc)
1585         printf (" 0x%s", dwarf_vmatoa ("x", uvalue));
1586       break;
1587
1588     case DW_FORM_flag_present:
1589     case DW_FORM_flag:
1590     case DW_FORM_data1:
1591     case DW_FORM_data2:
1592     case DW_FORM_sdata:
1593     case DW_FORM_udata:
1594       if (!do_loc)
1595         printf (" %s", dwarf_vmatoa ("d", uvalue));
1596       break;
1597
1598     case DW_FORM_ref8:
1599     case DW_FORM_data8:
1600       if (!do_loc)
1601         {
1602           dwarf_vma high_bits;
1603           dwarf_vma utmp;
1604           char buf[64];
1605
1606           SAFE_BYTE_GET64 (data, &high_bits, &uvalue, end);
1607           utmp = uvalue;
1608           if (form == DW_FORM_ref8)
1609             add64 (& high_bits, & utmp, cu_offset);
1610           printf (" 0x%s",
1611                   dwarf_vmatoa64 (high_bits, utmp, buf, sizeof (buf)));
1612         }
1613
1614       if ((do_loc || do_debug_loc || do_debug_ranges)
1615           && num_debug_info_entries == 0)
1616         {
1617           if (sizeof (uvalue) == 8)
1618             SAFE_BYTE_GET (uvalue, data, 8, end);
1619           else
1620             error (_("DW_FORM_data8 is unsupported when sizeof (dwarf_vma) != 8\n"));
1621         }
1622
1623       data += 8;
1624       break;
1625
1626     case DW_FORM_string:
1627       if (!do_loc)
1628         printf (" %.*s", (int) (end - data), data);
1629       data += strnlen ((char *) data, end - data) + 1;
1630       break;
1631
1632     case DW_FORM_block:
1633     case DW_FORM_exprloc:
1634       uvalue = read_uleb128 (data, & bytes_read, end);
1635       block_start = data + bytes_read;
1636       /* PR 17512: file: 008-103549-0.001:0.1.  */
1637       if (block_start + uvalue > end)
1638         {
1639           warn (_("Corrupt attribute block length: %lx\n"), (long) uvalue);
1640           uvalue = end - block_start;
1641         }
1642       if (do_loc)
1643         data = block_start + uvalue;
1644       else
1645         data = display_block (block_start, uvalue, end);
1646       break;
1647
1648     case DW_FORM_block1:
1649       SAFE_BYTE_GET (uvalue, data, 1, end);
1650       block_start = data + 1;
1651       if (block_start + uvalue > end)
1652         {
1653           warn (_("Corrupt attribute block length: %lx\n"), (long) uvalue);
1654           uvalue = end - block_start;
1655         }
1656       if (do_loc)
1657         data = block_start + uvalue;
1658       else
1659         data = display_block (block_start, uvalue, end);
1660       break;
1661
1662     case DW_FORM_block2:
1663       SAFE_BYTE_GET (uvalue, data, 2, end);
1664       block_start = data + 2;
1665       if (block_start + uvalue > end)
1666         {
1667           warn (_("Corrupt attribute block length: %lx\n"), (long) uvalue);
1668           uvalue = end - block_start;
1669         }
1670       if (do_loc)
1671         data = block_start + uvalue;
1672       else
1673         data = display_block (block_start, uvalue, end);
1674       break;
1675
1676     case DW_FORM_block4:
1677       SAFE_BYTE_GET (uvalue, data, 4, end);
1678       block_start = data + 4;
1679       if (block_start + uvalue > end)
1680         {
1681           warn (_("Corrupt attribute block length: %lx\n"), (long) uvalue);
1682           uvalue = end - block_start;
1683         }
1684       if (do_loc)
1685         data = block_start + uvalue;
1686       else
1687         data = display_block (block_start, uvalue, end);
1688       break;
1689
1690     case DW_FORM_strp:
1691       if (!do_loc)
1692         printf (_(" (indirect string, offset: 0x%s): %s"),
1693                 dwarf_vmatoa ("x", uvalue),
1694                 fetch_indirect_string (uvalue));
1695       break;
1696
1697     case DW_FORM_GNU_str_index:
1698       if (!do_loc)
1699         {
1700           const char *suffix = strrchr (section->name, '.');
1701           int dwo = (suffix && strcmp (suffix, ".dwo") == 0) ? 1 : 0;
1702
1703           printf (_(" (indexed string: 0x%s): %s"),
1704                   dwarf_vmatoa ("x", uvalue),
1705                   fetch_indexed_string (uvalue, this_set, offset_size, dwo));
1706         }
1707       break;
1708
1709     case DW_FORM_GNU_strp_alt:
1710       if (!do_loc)
1711         printf (_(" (alt indirect string, offset: 0x%s)"),
1712                 dwarf_vmatoa ("x", uvalue));
1713       break;
1714
1715     case DW_FORM_indirect:
1716       /* Handled above.  */
1717       break;
1718
1719     case DW_FORM_ref_sig8:
1720       if (!do_loc)
1721         {
1722           dwarf_vma high_bits;
1723           char buf[64];
1724
1725           SAFE_BYTE_GET64 (data, &high_bits, &uvalue, end);
1726           printf (" signature: 0x%s",
1727                   dwarf_vmatoa64 (high_bits, uvalue, buf, sizeof (buf)));
1728         }
1729       data += 8;
1730       break;
1731
1732     case DW_FORM_GNU_addr_index:
1733       if (!do_loc)
1734         printf (_(" (addr_index: 0x%s): %s"),
1735                 dwarf_vmatoa ("x", uvalue),
1736                 fetch_indexed_value (uvalue * pointer_size, pointer_size));
1737       break;
1738
1739     default:
1740       warn (_("Unrecognized form: %lu\n"), form);
1741       break;
1742     }
1743
1744   if ((do_loc || do_debug_loc || do_debug_ranges)
1745       && num_debug_info_entries == 0
1746       && debug_info_p != NULL)
1747     {
1748       switch (attribute)
1749         {
1750         case DW_AT_frame_base:
1751           have_frame_base = 1;
1752         case DW_AT_location:
1753         case DW_AT_string_length:
1754         case DW_AT_return_addr:
1755         case DW_AT_data_member_location:
1756         case DW_AT_vtable_elem_location:
1757         case DW_AT_segment:
1758         case DW_AT_static_link:
1759         case DW_AT_use_location:
1760         case DW_AT_GNU_call_site_value:
1761         case DW_AT_GNU_call_site_data_value:
1762         case DW_AT_GNU_call_site_target:
1763         case DW_AT_GNU_call_site_target_clobbered:
1764           if ((dwarf_version < 4
1765                && (form == DW_FORM_data4 || form == DW_FORM_data8))
1766               || form == DW_FORM_sec_offset)
1767             {
1768               /* Process location list.  */
1769               unsigned int lmax = debug_info_p->max_loc_offsets;
1770               unsigned int num = debug_info_p->num_loc_offsets;
1771
1772               if (lmax == 0 || num >= lmax)
1773                 {
1774                   lmax += 1024;
1775                   debug_info_p->loc_offsets = (dwarf_vma *)
1776                       xcrealloc (debug_info_p->loc_offsets,
1777                                  lmax, sizeof (*debug_info_p->loc_offsets));
1778                   debug_info_p->have_frame_base = (int *)
1779                       xcrealloc (debug_info_p->have_frame_base,
1780                                  lmax, sizeof (*debug_info_p->have_frame_base));
1781                   debug_info_p->max_loc_offsets = lmax;
1782                 }
1783               if (this_set != NULL)
1784                 uvalue += this_set->section_offsets [DW_SECT_LOC];
1785               debug_info_p->loc_offsets [num] = uvalue;
1786               debug_info_p->have_frame_base [num] = have_frame_base;
1787               debug_info_p->num_loc_offsets++;
1788             }
1789           break;
1790
1791         case DW_AT_low_pc:
1792           if (need_base_address)
1793             debug_info_p->base_address = uvalue;
1794           break;
1795
1796         case DW_AT_GNU_addr_base:
1797           debug_info_p->addr_base = uvalue;
1798           break;
1799
1800         case DW_AT_GNU_ranges_base:
1801           debug_info_p->ranges_base = uvalue;
1802           break;
1803
1804         case DW_AT_ranges:
1805           if ((dwarf_version < 4
1806                && (form == DW_FORM_data4 || form == DW_FORM_data8))
1807               || form == DW_FORM_sec_offset)
1808             {
1809               /* Process range list.  */
1810               unsigned int lmax = debug_info_p->max_range_lists;
1811               unsigned int num = debug_info_p->num_range_lists;
1812
1813               if (lmax == 0 || num >= lmax)
1814                 {
1815                   lmax += 1024;
1816                   debug_info_p->range_lists = (dwarf_vma *)
1817                       xcrealloc (debug_info_p->range_lists,
1818                                  lmax, sizeof (*debug_info_p->range_lists));
1819                   debug_info_p->max_range_lists = lmax;
1820                 }
1821               debug_info_p->range_lists [num] = uvalue;
1822               debug_info_p->num_range_lists++;
1823             }
1824           break;
1825
1826         default:
1827           break;
1828         }
1829     }
1830
1831   if (do_loc || attribute == 0)
1832     return data;
1833
1834   /* For some attributes we can display further information.  */
1835   switch (attribute)
1836     {
1837     case DW_AT_inline:
1838       printf ("\t");
1839       switch (uvalue)
1840         {
1841         case DW_INL_not_inlined:
1842           printf (_("(not inlined)"));
1843           break;
1844         case DW_INL_inlined:
1845           printf (_("(inlined)"));
1846           break;
1847         case DW_INL_declared_not_inlined:
1848           printf (_("(declared as inline but ignored)"));
1849           break;
1850         case DW_INL_declared_inlined:
1851           printf (_("(declared as inline and inlined)"));
1852           break;
1853         default:
1854           printf (_("  (Unknown inline attribute value: %s)"),
1855                   dwarf_vmatoa ("x", uvalue));
1856           break;
1857         }
1858       break;
1859
1860     case DW_AT_language:
1861       printf ("\t");
1862       switch (uvalue)
1863         {
1864           /* Ordered by the numeric value of these constants.  */
1865         case DW_LANG_C89:               printf ("(ANSI C)"); break;
1866         case DW_LANG_C:                 printf ("(non-ANSI C)"); break;
1867         case DW_LANG_Ada83:             printf ("(Ada)"); break;
1868         case DW_LANG_C_plus_plus:       printf ("(C++)"); break;
1869         case DW_LANG_Cobol74:           printf ("(Cobol 74)"); break;
1870         case DW_LANG_Cobol85:           printf ("(Cobol 85)"); break;
1871         case DW_LANG_Fortran77:         printf ("(FORTRAN 77)"); break;
1872         case DW_LANG_Fortran90:         printf ("(Fortran 90)"); break;
1873         case DW_LANG_Pascal83:          printf ("(ANSI Pascal)"); break;
1874         case DW_LANG_Modula2:           printf ("(Modula 2)"); break;
1875           /* DWARF 2.1 values.  */
1876         case DW_LANG_Java:              printf ("(Java)"); break;
1877         case DW_LANG_C99:               printf ("(ANSI C99)"); break;
1878         case DW_LANG_Ada95:             printf ("(ADA 95)"); break;
1879         case DW_LANG_Fortran95:         printf ("(Fortran 95)"); break;
1880           /* DWARF 3 values.  */
1881         case DW_LANG_PLI:               printf ("(PLI)"); break;
1882         case DW_LANG_ObjC:              printf ("(Objective C)"); break;
1883         case DW_LANG_ObjC_plus_plus:    printf ("(Objective C++)"); break;
1884         case DW_LANG_UPC:               printf ("(Unified Parallel C)"); break;
1885         case DW_LANG_D:                 printf ("(D)"); break;
1886           /* DWARF 4 values.  */
1887         case DW_LANG_Python:            printf ("(Python)"); break;
1888           /* DWARF 5 values.  */
1889         case DW_LANG_Go:                printf ("(Go)"); break;
1890           /* MIPS extension.  */
1891         case DW_LANG_Mips_Assembler:    printf ("(MIPS assembler)"); break;
1892           /* UPC extension.  */
1893         case DW_LANG_Upc:               printf ("(Unified Parallel C)"); break;
1894         default:
1895           if (uvalue >= DW_LANG_lo_user && uvalue <= DW_LANG_hi_user)
1896             printf (_("(implementation defined: %s)"),
1897                     dwarf_vmatoa ("x", uvalue));
1898           else
1899             printf (_("(Unknown: %s)"), dwarf_vmatoa ("x", uvalue));
1900           break;
1901         }
1902       break;
1903
1904     case DW_AT_encoding:
1905       printf ("\t");
1906       switch (uvalue)
1907         {
1908         case DW_ATE_void:               printf ("(void)"); break;
1909         case DW_ATE_address:            printf ("(machine address)"); break;
1910         case DW_ATE_boolean:            printf ("(boolean)"); break;
1911         case DW_ATE_complex_float:      printf ("(complex float)"); break;
1912         case DW_ATE_float:              printf ("(float)"); break;
1913         case DW_ATE_signed:             printf ("(signed)"); break;
1914         case DW_ATE_signed_char:        printf ("(signed char)"); break;
1915         case DW_ATE_unsigned:           printf ("(unsigned)"); break;
1916         case DW_ATE_unsigned_char:      printf ("(unsigned char)"); break;
1917           /* DWARF 2.1 values:  */
1918         case DW_ATE_imaginary_float:    printf ("(imaginary float)"); break;
1919         case DW_ATE_decimal_float:      printf ("(decimal float)"); break;
1920           /* DWARF 3 values:  */
1921         case DW_ATE_packed_decimal:     printf ("(packed_decimal)"); break;
1922         case DW_ATE_numeric_string:     printf ("(numeric_string)"); break;
1923         case DW_ATE_edited:             printf ("(edited)"); break;
1924         case DW_ATE_signed_fixed:       printf ("(signed_fixed)"); break;
1925         case DW_ATE_unsigned_fixed:     printf ("(unsigned_fixed)"); break;
1926           /* HP extensions:  */
1927         case DW_ATE_HP_float80:         printf ("(HP_float80)"); break;
1928         case DW_ATE_HP_complex_float80: printf ("(HP_complex_float80)"); break;
1929         case DW_ATE_HP_float128:        printf ("(HP_float128)"); break;
1930         case DW_ATE_HP_complex_float128:printf ("(HP_complex_float128)"); break;
1931         case DW_ATE_HP_floathpintel:    printf ("(HP_floathpintel)"); break;
1932         case DW_ATE_HP_imaginary_float80:       printf ("(HP_imaginary_float80)"); break;
1933         case DW_ATE_HP_imaginary_float128:      printf ("(HP_imaginary_float128)"); break;
1934
1935         default:
1936           if (uvalue >= DW_ATE_lo_user
1937               && uvalue <= DW_ATE_hi_user)
1938             printf (_("(user defined type)"));
1939           else
1940             printf (_("(unknown type)"));
1941           break;
1942         }
1943       break;
1944
1945     case DW_AT_accessibility:
1946       printf ("\t");
1947       switch (uvalue)
1948         {
1949         case DW_ACCESS_public:          printf ("(public)"); break;
1950         case DW_ACCESS_protected:       printf ("(protected)"); break;
1951         case DW_ACCESS_private:         printf ("(private)"); break;
1952         default:
1953           printf (_("(unknown accessibility)"));
1954           break;
1955         }
1956       break;
1957
1958     case DW_AT_visibility:
1959       printf ("\t");
1960       switch (uvalue)
1961         {
1962         case DW_VIS_local:              printf ("(local)"); break;
1963         case DW_VIS_exported:           printf ("(exported)"); break;
1964         case DW_VIS_qualified:          printf ("(qualified)"); break;
1965         default:                        printf (_("(unknown visibility)")); break;
1966         }
1967       break;
1968
1969     case DW_AT_virtuality:
1970       printf ("\t");
1971       switch (uvalue)
1972         {
1973         case DW_VIRTUALITY_none:        printf ("(none)"); break;
1974         case DW_VIRTUALITY_virtual:     printf ("(virtual)"); break;
1975         case DW_VIRTUALITY_pure_virtual:printf ("(pure_virtual)"); break;
1976         default:                        printf (_("(unknown virtuality)")); break;
1977         }
1978       break;
1979
1980     case DW_AT_identifier_case:
1981       printf ("\t");
1982       switch (uvalue)
1983         {
1984         case DW_ID_case_sensitive:      printf ("(case_sensitive)"); break;
1985         case DW_ID_up_case:             printf ("(up_case)"); break;
1986         case DW_ID_down_case:           printf ("(down_case)"); break;
1987         case DW_ID_case_insensitive:    printf ("(case_insensitive)"); break;
1988         default:                        printf (_("(unknown case)")); break;
1989         }
1990       break;
1991
1992     case DW_AT_calling_convention:
1993       printf ("\t");
1994       switch (uvalue)
1995         {
1996         case DW_CC_normal:      printf ("(normal)"); break;
1997         case DW_CC_program:     printf ("(program)"); break;
1998         case DW_CC_nocall:      printf ("(nocall)"); break;
1999         default:
2000           if (uvalue >= DW_CC_lo_user
2001               && uvalue <= DW_CC_hi_user)
2002             printf (_("(user defined)"));
2003           else
2004             printf (_("(unknown convention)"));
2005         }
2006       break;
2007
2008     case DW_AT_ordering:
2009       printf ("\t");
2010       switch (uvalue)
2011         {
2012         case -1: printf (_("(undefined)")); break;
2013         case 0:  printf ("(row major)"); break;
2014         case 1:  printf ("(column major)"); break;
2015         }
2016       break;
2017
2018     case DW_AT_frame_base:
2019       have_frame_base = 1;
2020     case DW_AT_location:
2021     case DW_AT_string_length:
2022     case DW_AT_return_addr:
2023     case DW_AT_data_member_location:
2024     case DW_AT_vtable_elem_location:
2025     case DW_AT_segment:
2026     case DW_AT_static_link:
2027     case DW_AT_use_location:
2028     case DW_AT_GNU_call_site_value:
2029     case DW_AT_GNU_call_site_data_value:
2030     case DW_AT_GNU_call_site_target:
2031     case DW_AT_GNU_call_site_target_clobbered:
2032       if ((dwarf_version < 4
2033            && (form == DW_FORM_data4 || form == DW_FORM_data8))
2034           || form == DW_FORM_sec_offset)
2035         printf (_(" (location list)"));
2036       /* Fall through.  */
2037     case DW_AT_allocated:
2038     case DW_AT_associated:
2039     case DW_AT_data_location:
2040     case DW_AT_stride:
2041     case DW_AT_upper_bound:
2042     case DW_AT_lower_bound:
2043       if (block_start)
2044         {
2045           int need_frame_base;
2046
2047           printf ("\t(");
2048           need_frame_base = decode_location_expression (block_start,
2049                                                         pointer_size,
2050                                                         offset_size,
2051                                                         dwarf_version,
2052                                                         uvalue,
2053                                                         cu_offset, section);
2054           printf (")");
2055           if (need_frame_base && !have_frame_base)
2056             printf (_(" [without DW_AT_frame_base]"));
2057         }
2058       break;
2059
2060     case DW_AT_import:
2061       {
2062         if (form == DW_FORM_ref_sig8
2063             || form == DW_FORM_GNU_ref_alt)
2064           break;
2065
2066         if (form == DW_FORM_ref1
2067             || form == DW_FORM_ref2
2068             || form == DW_FORM_ref4
2069             || form == DW_FORM_ref_udata)
2070           uvalue += cu_offset;
2071
2072         if (uvalue >= section->size)
2073           warn (_("Offset %s used as value for DW_AT_import attribute of DIE at offset %lx is too big.\n"),
2074                 dwarf_vmatoa ("x", uvalue),
2075                 (unsigned long) (orig_data - section->start));
2076         else
2077           {
2078             unsigned long abbrev_number;
2079             abbrev_entry * entry;
2080
2081             abbrev_number = read_uleb128 (section->start + uvalue, NULL, end);
2082
2083             printf (_("\t[Abbrev Number: %ld"), abbrev_number);
2084             /* Don't look up abbrev for DW_FORM_ref_addr, as it very often will
2085                use different abbrev table, and we don't track .debug_info chunks
2086                yet.  */
2087             if (form != DW_FORM_ref_addr)
2088               {
2089                 for (entry = first_abbrev; entry != NULL; entry = entry->next)
2090                   if (entry->entry == abbrev_number)
2091                     break;
2092                 if (entry != NULL)
2093                   printf (" (%s)", get_TAG_name (entry->tag));
2094               }
2095             printf ("]");
2096           }
2097       }
2098       break;
2099
2100     default:
2101       break;
2102     }
2103
2104   return data;
2105 }
2106
2107 static const char *
2108 get_AT_name (unsigned long attribute)
2109 {
2110   const char *name;
2111
2112   if (attribute == 0)
2113     return "DW_AT value: 0";
2114
2115   /* One value is shared by the MIPS and HP extensions:  */
2116   if (attribute == DW_AT_MIPS_fde)
2117     return "DW_AT_MIPS_fde or DW_AT_HP_unmodifiable";
2118
2119   name = get_DW_AT_name (attribute);
2120
2121   if (name == NULL)
2122     {
2123       static char buffer[100];
2124
2125       snprintf (buffer, sizeof (buffer), _("Unknown AT value: %lx"),
2126                 attribute);
2127       return buffer;
2128     }
2129
2130   return name;
2131 }
2132
2133 static unsigned char *
2134 read_and_display_attr (unsigned long attribute,
2135                        unsigned long form,
2136                        unsigned char * data,
2137                        unsigned char * end,
2138                        dwarf_vma cu_offset,
2139                        dwarf_vma pointer_size,
2140                        dwarf_vma offset_size,
2141                        int dwarf_version,
2142                        debug_info * debug_info_p,
2143                        int do_loc,
2144                        struct dwarf_section * section,
2145                        struct cu_tu_set * this_set)
2146 {
2147   if (!do_loc)
2148     printf ("   %-18s:", get_AT_name (attribute));
2149   data = read_and_display_attr_value (attribute, form, data, end,
2150                                       cu_offset, pointer_size, offset_size,
2151                                       dwarf_version, debug_info_p,
2152                                       do_loc, section, this_set);
2153   if (!do_loc)
2154     printf ("\n");
2155   return data;
2156 }
2157
2158 /* Process the contents of a .debug_info section.  If do_loc is non-zero
2159    then we are scanning for location lists and we do not want to display
2160    anything to the user.  If do_types is non-zero, we are processing
2161    a .debug_types section instead of a .debug_info section.  */
2162
2163 static int
2164 process_debug_info (struct dwarf_section *section,
2165                     void *file,
2166                     enum dwarf_section_display_enum abbrev_sec,
2167                     int do_loc,
2168                     int do_types)
2169 {
2170   unsigned char *start = section->start;
2171   unsigned char *end = start + section->size;
2172   unsigned char *section_begin;
2173   unsigned int unit;
2174   unsigned int num_units = 0;
2175
2176   if ((do_loc || do_debug_loc || do_debug_ranges)
2177       && num_debug_info_entries == 0
2178       && ! do_types)
2179     {
2180       dwarf_vma length;
2181
2182       /* First scan the section to get the number of comp units.  */
2183       for (section_begin = start, num_units = 0; section_begin < end;
2184            num_units ++)
2185         {
2186           /* Read the first 4 bytes.  For a 32-bit DWARF section, this
2187              will be the length.  For a 64-bit DWARF section, it'll be
2188              the escape code 0xffffffff followed by an 8 byte length.  */
2189           SAFE_BYTE_GET (length, section_begin, 4, end);
2190
2191           if (length == 0xffffffff)
2192             {
2193               SAFE_BYTE_GET (length, section_begin + 4, 8, end);
2194               section_begin += length + 12;
2195             }
2196           else if (length >= 0xfffffff0 && length < 0xffffffff)
2197             {
2198               warn (_("Reserved length value (0x%s) found in section %s\n"),
2199                     dwarf_vmatoa ("x", length), section->name);
2200               return 0;
2201             }
2202           else
2203             section_begin += length + 4;
2204
2205           /* Negative values are illegal, they may even cause infinite
2206              looping.  This can happen if we can't accurately apply
2207              relocations to an object file.  */
2208           if ((signed long) length <= 0)
2209             {
2210               warn (_("Corrupt unit length (0x%s) found in section %s\n"),
2211                     dwarf_vmatoa ("x", length), section->name);
2212               return 0;
2213             }
2214         }
2215
2216       if (num_units == 0)
2217         {
2218           error (_("No comp units in %s section ?\n"), section->name);
2219           return 0;
2220         }
2221
2222       /* Then allocate an array to hold the information.  */
2223       debug_information = (debug_info *) cmalloc (num_units,
2224                                                   sizeof (* debug_information));
2225       if (debug_information == NULL)
2226         {
2227           error (_("Not enough memory for a debug info array of %u entries\n"),
2228                  num_units);
2229           return 0;
2230         }
2231     }
2232
2233   if (!do_loc)
2234     {
2235       if (dwarf_start_die == 0)
2236         printf (_("Contents of the %s section:\n\n"), section->name);
2237
2238       load_debug_section (str, file);
2239       load_debug_section (str_dwo, file);
2240       load_debug_section (str_index, file);
2241       load_debug_section (str_index_dwo, file);
2242       load_debug_section (debug_addr, file);
2243     }
2244
2245   load_debug_section (abbrev_sec, file);
2246   if (debug_displays [abbrev_sec].section.start == NULL)
2247     {
2248       warn (_("Unable to locate %s section!\n"),
2249             debug_displays [abbrev_sec].section.name);
2250       return 0;
2251     }
2252
2253   for (section_begin = start, unit = 0; start < end; unit++)
2254     {
2255       DWARF2_Internal_CompUnit compunit;
2256       unsigned char *hdrptr;
2257       unsigned char *tags;
2258       int level, last_level, saved_level;
2259       dwarf_vma cu_offset;
2260       unsigned int offset_size;
2261       int initial_length_size;
2262       dwarf_vma signature_high = 0;
2263       dwarf_vma signature_low = 0;
2264       dwarf_vma type_offset = 0;
2265       struct cu_tu_set *this_set;
2266       dwarf_vma abbrev_base;
2267       size_t abbrev_size;
2268
2269       hdrptr = start;
2270
2271       SAFE_BYTE_GET_AND_INC (compunit.cu_length, hdrptr, 4, end);
2272
2273       if (compunit.cu_length == 0xffffffff)
2274         {
2275           SAFE_BYTE_GET_AND_INC (compunit.cu_length, hdrptr, 8, end);
2276           offset_size = 8;
2277           initial_length_size = 12;
2278         }
2279       else
2280         {
2281           offset_size = 4;
2282           initial_length_size = 4;
2283         }
2284
2285       SAFE_BYTE_GET_AND_INC (compunit.cu_version, hdrptr, 2, end);
2286
2287       cu_offset = start - section_begin;
2288
2289       this_set = find_cu_tu_set_v2 (cu_offset, do_types);
2290
2291       SAFE_BYTE_GET_AND_INC (compunit.cu_abbrev_offset, hdrptr, offset_size, end);
2292
2293       if (this_set == NULL)
2294         {
2295           abbrev_base = 0;
2296           abbrev_size = debug_displays [abbrev_sec].section.size;
2297         }
2298       else
2299         {
2300           abbrev_base = this_set->section_offsets [DW_SECT_ABBREV];
2301           abbrev_size = this_set->section_sizes [DW_SECT_ABBREV];
2302         }
2303
2304       SAFE_BYTE_GET_AND_INC (compunit.cu_pointer_size, hdrptr, 1, end);
2305       /* PR 17512: file: 001-108546-0.001:0.1.  */
2306       if (compunit.cu_pointer_size < 2 || compunit.cu_pointer_size > 8)
2307         {
2308           warn (_("Invalid pointer size (%d) in compunit header, using %d instead\n"),
2309                 compunit.cu_pointer_size, offset_size);
2310           compunit.cu_pointer_size = offset_size;
2311         }
2312
2313       if (do_types)
2314         {
2315           SAFE_BYTE_GET64 (hdrptr, &signature_high, &signature_low, end);
2316           hdrptr += 8;
2317           SAFE_BYTE_GET_AND_INC (type_offset, hdrptr, offset_size, end);
2318         }
2319
2320       if ((do_loc || do_debug_loc || do_debug_ranges)
2321           && num_debug_info_entries == 0
2322           && ! do_types)
2323         {
2324           debug_information [unit].cu_offset = cu_offset;
2325           debug_information [unit].pointer_size
2326             = compunit.cu_pointer_size;
2327           debug_information [unit].offset_size = offset_size;
2328           debug_information [unit].dwarf_version = compunit.cu_version;
2329           debug_information [unit].base_address = 0;
2330           debug_information [unit].addr_base = DEBUG_INFO_UNAVAILABLE;
2331           debug_information [unit].ranges_base = DEBUG_INFO_UNAVAILABLE;
2332           debug_information [unit].loc_offsets = NULL;
2333           debug_information [unit].have_frame_base = NULL;
2334           debug_information [unit].max_loc_offsets = 0;
2335           debug_information [unit].num_loc_offsets = 0;
2336           debug_information [unit].range_lists = NULL;
2337           debug_information [unit].max_range_lists= 0;
2338           debug_information [unit].num_range_lists = 0;
2339         }
2340
2341       if (!do_loc && dwarf_start_die == 0)
2342         {
2343           printf (_("  Compilation Unit @ offset 0x%s:\n"),
2344                   dwarf_vmatoa ("x", cu_offset));
2345           printf (_("   Length:        0x%s (%s)\n"),
2346                   dwarf_vmatoa ("x", compunit.cu_length),
2347                   offset_size == 8 ? "64-bit" : "32-bit");
2348           printf (_("   Version:       %d\n"), compunit.cu_version);
2349           printf (_("   Abbrev Offset: 0x%s\n"),
2350                   dwarf_vmatoa ("x", compunit.cu_abbrev_offset));
2351           printf (_("   Pointer Size:  %d\n"), compunit.cu_pointer_size);
2352           if (do_types)
2353             {
2354               char buf[64];
2355
2356               printf (_("   Signature:     0x%s\n"),
2357                       dwarf_vmatoa64 (signature_high, signature_low,
2358                                       buf, sizeof (buf)));
2359               printf (_("   Type Offset:   0x%s\n"),
2360                       dwarf_vmatoa ("x", type_offset));
2361             }
2362           if (this_set != NULL)
2363             {
2364               dwarf_vma *offsets = this_set->section_offsets;
2365               size_t *sizes = this_set->section_sizes;
2366
2367               printf (_("   Section contributions:\n"));
2368               printf (_("    .debug_abbrev.dwo:       0x%s  0x%s\n"),
2369                       dwarf_vmatoa ("x", offsets [DW_SECT_ABBREV]),
2370                       dwarf_vmatoa ("x", sizes [DW_SECT_ABBREV]));
2371               printf (_("    .debug_line.dwo:         0x%s  0x%s\n"),
2372                       dwarf_vmatoa ("x", offsets [DW_SECT_LINE]),
2373                       dwarf_vmatoa ("x", sizes [DW_SECT_LINE]));
2374               printf (_("    .debug_loc.dwo:          0x%s  0x%s\n"),
2375                       dwarf_vmatoa ("x", offsets [DW_SECT_LOC]),
2376                       dwarf_vmatoa ("x", sizes [DW_SECT_LOC]));
2377               printf (_("    .debug_str_offsets.dwo:  0x%s  0x%s\n"),
2378                       dwarf_vmatoa ("x", offsets [DW_SECT_STR_OFFSETS]),
2379                       dwarf_vmatoa ("x", sizes [DW_SECT_STR_OFFSETS]));
2380             }
2381         }
2382
2383       if (cu_offset + compunit.cu_length + initial_length_size
2384           > section->size)
2385         {
2386           warn (_("Debug info is corrupted, length of CU at %s"
2387                   " extends beyond end of section (length = %s)\n"),
2388                 dwarf_vmatoa ("x", cu_offset),
2389                 dwarf_vmatoa ("x", compunit.cu_length));
2390           break;
2391         }
2392       tags = hdrptr;
2393       start += compunit.cu_length + initial_length_size;
2394
2395       if (compunit.cu_version != 2
2396           && compunit.cu_version != 3
2397           && compunit.cu_version != 4)
2398         {
2399           warn (_("CU at offset %s contains corrupt or "
2400                   "unsupported version number: %d.\n"),
2401                 dwarf_vmatoa ("x", cu_offset), compunit.cu_version);
2402           continue;
2403         }
2404
2405       free_abbrevs ();
2406
2407       /* Process the abbrevs used by this compilation unit.  */
2408       if (compunit.cu_abbrev_offset >= abbrev_size)
2409         warn (_("Debug info is corrupted, abbrev offset (%lx) is larger than abbrev section size (%lx)\n"),
2410               (unsigned long) compunit.cu_abbrev_offset,
2411               (unsigned long) abbrev_size);
2412       else
2413         process_abbrev_section
2414           (((unsigned char *) debug_displays [abbrev_sec].section.start
2415             + abbrev_base + compunit.cu_abbrev_offset),
2416            ((unsigned char *) debug_displays [abbrev_sec].section.start
2417             + abbrev_base + abbrev_size));
2418
2419       level = 0;
2420       last_level = level;
2421       saved_level = -1;
2422       while (tags < start)
2423         {
2424           unsigned int bytes_read;
2425           unsigned long abbrev_number;
2426           unsigned long die_offset;
2427           abbrev_entry *entry;
2428           abbrev_attr *attr;
2429           int do_printing = 1;
2430
2431           die_offset = tags - section_begin;
2432
2433           abbrev_number = read_uleb128 (tags, & bytes_read, start);
2434           tags += bytes_read;
2435
2436           /* A null DIE marks the end of a list of siblings or it may also be
2437              a section padding.  */
2438           if (abbrev_number == 0)
2439             {
2440               /* Check if it can be a section padding for the last CU.  */
2441               if (level == 0 && start == end)
2442                 {
2443                   unsigned char *chk;
2444
2445                   for (chk = tags; chk < start; chk++)
2446                     if (*chk != 0)
2447                       break;
2448                   if (chk == start)
2449                     break;
2450                 }
2451
2452               if (!do_loc && die_offset >= dwarf_start_die
2453                   && (dwarf_cutoff_level == -1
2454                       || level < dwarf_cutoff_level))
2455                 printf (_(" <%d><%lx>: Abbrev Number: 0\n"),
2456                         level, die_offset);
2457
2458               --level;
2459               if (level < 0)
2460                 {
2461                   static unsigned num_bogus_warns = 0;
2462
2463                   if (num_bogus_warns < 3)
2464                     {
2465                       warn (_("Bogus end-of-siblings marker detected at offset %lx in %s section\n"),
2466                             die_offset, section->name);
2467                       num_bogus_warns ++;
2468                       if (num_bogus_warns == 3)
2469                         warn (_("Further warnings about bogus end-of-sibling markers suppressed\n"));
2470                     }
2471                 }
2472               if (dwarf_start_die != 0 && level < saved_level)
2473                 return 1;
2474               continue;
2475             }
2476
2477           if (!do_loc)
2478             {
2479               if (dwarf_start_die != 0 && die_offset < dwarf_start_die)
2480                 do_printing = 0;
2481               else
2482                 {
2483                   if (dwarf_start_die != 0 && die_offset == dwarf_start_die)
2484                     saved_level = level;
2485                   do_printing = (dwarf_cutoff_level == -1
2486                                  || level < dwarf_cutoff_level);
2487                   if (do_printing)
2488                     printf (_(" <%d><%lx>: Abbrev Number: %lu"),
2489                             level, die_offset, abbrev_number);
2490                   else if (dwarf_cutoff_level == -1
2491                            || last_level < dwarf_cutoff_level)
2492                     printf (_(" <%d><%lx>: ...\n"), level, die_offset);
2493                   last_level = level;
2494                 }
2495             }
2496
2497           /* Scan through the abbreviation list until we reach the
2498              correct entry.  */
2499           for (entry = first_abbrev;
2500                entry && entry->entry != abbrev_number;
2501                entry = entry->next)
2502             continue;
2503
2504           if (entry == NULL)
2505             {
2506               if (!do_loc && do_printing)
2507                 {
2508                   printf ("\n");
2509                   fflush (stdout);
2510                 }
2511               warn (_("DIE at offset %lx refers to abbreviation number %lu which does not exist\n"),
2512                     die_offset, abbrev_number);
2513               return 0;
2514             }
2515
2516           if (!do_loc && do_printing)
2517             printf (" (%s)\n", get_TAG_name (entry->tag));
2518
2519           switch (entry->tag)
2520             {
2521             default:
2522               need_base_address = 0;
2523               break;
2524             case DW_TAG_compile_unit:
2525               need_base_address = 1;
2526               break;
2527             case DW_TAG_entry_point:
2528             case DW_TAG_subprogram:
2529               need_base_address = 0;
2530               /* Assuming that there is no DW_AT_frame_base.  */
2531               have_frame_base = 0;
2532               break;
2533             }
2534
2535           for (attr = entry->first_attr;
2536                attr && attr->attribute;
2537                attr = attr->next)
2538             {
2539               debug_info *arg;
2540
2541               if (! do_loc && do_printing)
2542                 /* Show the offset from where the tag was extracted.  */
2543                 printf ("    <%lx>", (unsigned long)(tags - section_begin));
2544
2545               arg = debug_information;
2546               if (debug_information)
2547                 arg += unit;
2548
2549               tags = read_and_display_attr (attr->attribute,
2550                                             attr->form,
2551                                             tags,
2552                                             end,
2553                                             cu_offset,
2554                                             compunit.cu_pointer_size,
2555                                             offset_size,
2556                                             compunit.cu_version,
2557                                             arg,
2558                                             do_loc || ! do_printing,
2559                                             section,
2560                                             this_set);
2561             }
2562
2563           if (entry->children)
2564             ++level;
2565         }
2566     }
2567
2568   /* Set num_debug_info_entries here so that it can be used to check if
2569      we need to process .debug_loc and .debug_ranges sections.  */
2570   if ((do_loc || do_debug_loc || do_debug_ranges)
2571       && num_debug_info_entries == 0
2572       && ! do_types)
2573     num_debug_info_entries = num_units;
2574
2575   if (!do_loc)
2576     printf ("\n");
2577
2578   return 1;
2579 }
2580
2581 /* Locate and scan the .debug_info section in the file and record the pointer
2582    sizes and offsets for the compilation units in it.  Usually an executable
2583    will have just one pointer size, but this is not guaranteed, and so we try
2584    not to make any assumptions.  Returns zero upon failure, or the number of
2585    compilation units upon success.  */
2586
2587 static unsigned int
2588 load_debug_info (void * file)
2589 {
2590   /* Reset the last pointer size so that we can issue correct error
2591      messages if we are displaying the contents of more than one section.  */
2592   last_pointer_size = 0;
2593   warned_about_missing_comp_units = FALSE;
2594
2595   /* If we have already tried and failed to load the .debug_info
2596      section then do not bother to repeat the task.  */
2597   if (num_debug_info_entries == DEBUG_INFO_UNAVAILABLE)
2598     return 0;
2599
2600   /* If we already have the information there is nothing else to do.  */
2601   if (num_debug_info_entries > 0)
2602     return num_debug_info_entries;
2603
2604   /* If this is a DWARF package file, load the CU and TU indexes.  */
2605   load_cu_tu_indexes (file);
2606
2607   if (load_debug_section (info, file)
2608       && process_debug_info (&debug_displays [info].section, file, abbrev, 1, 0))
2609     return num_debug_info_entries;
2610   else if (load_debug_section (info_dwo, file)
2611            && process_debug_info (&debug_displays [info_dwo].section, file,
2612                                   abbrev_dwo, 1, 0))
2613     return num_debug_info_entries;
2614
2615   num_debug_info_entries = DEBUG_INFO_UNAVAILABLE;
2616   return 0;
2617 }
2618
2619 /* Read a DWARF .debug_line section header starting at DATA.
2620    Upon success returns an updated DATA pointer and the LINFO
2621    structure and the END_OF_SEQUENCE pointer will be filled in.
2622    Otherwise returns NULL.  */
2623
2624 static unsigned char *
2625 read_debug_line_header (struct dwarf_section * section,
2626                         unsigned char * data,
2627                         unsigned char * end,
2628                         DWARF2_Internal_LineInfo * linfo,
2629                         unsigned char ** end_of_sequence)
2630 {
2631   unsigned char *hdrptr;
2632   unsigned int offset_size;
2633   unsigned int initial_length_size;
2634
2635   /* Extract information from the Line Number Program Header.
2636      (section 6.2.4 in the Dwarf3 doc).  */
2637       hdrptr = data;
2638
2639   /* Get and check the length of the block.  */
2640   SAFE_BYTE_GET_AND_INC (linfo->li_length, hdrptr, 4, end);
2641
2642   if (linfo->li_length == 0xffffffff)
2643     {
2644       /* This section is 64-bit DWARF 3.  */
2645       SAFE_BYTE_GET_AND_INC (linfo->li_length, hdrptr, 8, end);
2646       offset_size = 8;
2647       initial_length_size = 12;
2648     }
2649   else
2650     {
2651       offset_size = 4;
2652       initial_length_size = 4;
2653     }
2654
2655   if (linfo->li_length + initial_length_size > section->size)
2656     {
2657       /* If the length is just a bias against the initial_length_size then
2658          this means that the field has a relocation against it which has not
2659          been applied.  (Ie we are dealing with an object file, not a linked
2660          binary).  Do not complain but instead assume that the rest of the
2661          section applies to this particular header.  */
2662       if (linfo->li_length == - initial_length_size)
2663         {
2664           linfo->li_length = section->size - initial_length_size;
2665         }
2666       else
2667         {
2668           warn (_("The line info appears to be corrupt - the section is too small\n"));
2669           return NULL;
2670         }
2671     }
2672
2673   /* Get and check the version number.  */
2674   SAFE_BYTE_GET_AND_INC (linfo->li_version, hdrptr, 2, end);
2675
2676   if (linfo->li_version != 2
2677       && linfo->li_version != 3
2678       && linfo->li_version != 4)
2679     {
2680       warn (_("Only DWARF version 2, 3 and 4 line info is currently supported.\n"));
2681       return NULL;
2682     }
2683
2684   SAFE_BYTE_GET_AND_INC (linfo->li_prologue_length, hdrptr, offset_size, end);
2685   SAFE_BYTE_GET_AND_INC (linfo->li_min_insn_length, hdrptr, 1, end);
2686
2687   if (linfo->li_version >= 4)
2688     {
2689       SAFE_BYTE_GET_AND_INC (linfo->li_max_ops_per_insn, hdrptr, 1, end);
2690
2691       if (linfo->li_max_ops_per_insn == 0)
2692         {
2693           warn (_("Invalid maximum operations per insn.\n"));
2694           return NULL;
2695         }
2696     }
2697   else
2698     linfo->li_max_ops_per_insn = 1;
2699
2700   SAFE_BYTE_GET_AND_INC (linfo->li_default_is_stmt, hdrptr, 1, end);
2701   SAFE_SIGNED_BYTE_GET_AND_INC (linfo->li_line_base, hdrptr, 1, end);
2702   SAFE_BYTE_GET_AND_INC (linfo->li_line_range, hdrptr, 1, end);
2703   SAFE_BYTE_GET_AND_INC (linfo->li_opcode_base, hdrptr, 1, end);
2704
2705   * end_of_sequence = data + linfo->li_length + initial_length_size;
2706   return hdrptr;
2707 }
2708
2709 static int
2710 display_debug_lines_raw (struct dwarf_section *section,
2711                          unsigned char *data,
2712                          unsigned char *end)
2713 {
2714   unsigned char *start = section->start;
2715
2716   printf (_("Raw dump of debug contents of section %s:\n\n"),
2717           section->name);
2718
2719   while (data < end)
2720     {
2721       static DWARF2_Internal_LineInfo saved_linfo;
2722       DWARF2_Internal_LineInfo linfo;
2723       unsigned char *standard_opcodes;
2724       unsigned char *end_of_sequence;
2725       unsigned int last_dir_entry = 0;
2726       int i;
2727
2728       if (const_strneq (section->name, ".debug_line.")
2729           /* Note: the following does not apply to .debug_line.dwo sections.
2730              These are full debug_line sections.  */
2731           && strcmp (section->name, ".debug_line.dwo") != 0)
2732         {
2733           /* Sections named .debug_line.<foo> are fragments of a .debug_line
2734              section containing just the Line Number Statements.  They are
2735              created by the assembler and intended to be used alongside gcc's
2736              -ffunction-sections command line option.  When the linker's
2737              garbage collection decides to discard a .text.<foo> section it
2738              can then also discard the line number information in .debug_line.<foo>.
2739
2740              Since the section is a fragment it does not have the details
2741              needed to fill out a LineInfo structure, so instead we use the
2742              details from the last full debug_line section that we processed.  */
2743           end_of_sequence = end;
2744           standard_opcodes = NULL;
2745           linfo = saved_linfo;
2746           reset_state_machine (linfo.li_default_is_stmt);
2747         }
2748       else
2749         {
2750           unsigned char * hdrptr;
2751
2752           if ((hdrptr = read_debug_line_header (section, data, end, & linfo,
2753                                                 & end_of_sequence)) == NULL)
2754             return 0;
2755
2756           printf (_("  Offset:                      0x%lx\n"), (long)(data - start));
2757           printf (_("  Length:                      %ld\n"), (long) linfo.li_length);
2758           printf (_("  DWARF Version:               %d\n"), linfo.li_version);
2759           printf (_("  Prologue Length:             %d\n"), linfo.li_prologue_length);
2760           printf (_("  Minimum Instruction Length:  %d\n"), linfo.li_min_insn_length);
2761           if (linfo.li_version >= 4)
2762             printf (_("  Maximum Ops per Instruction: %d\n"), linfo.li_max_ops_per_insn);
2763           printf (_("  Initial value of 'is_stmt':  %d\n"), linfo.li_default_is_stmt);
2764           printf (_("  Line Base:                   %d\n"), linfo.li_line_base);
2765           printf (_("  Line Range:                  %d\n"), linfo.li_line_range);
2766           printf (_("  Opcode Base:                 %d\n"), linfo.li_opcode_base);
2767
2768           reset_state_machine (linfo.li_default_is_stmt);
2769
2770           /* Display the contents of the Opcodes table.  */
2771           standard_opcodes = hdrptr;
2772
2773           printf (_("\n Opcodes:\n"));
2774
2775           for (i = 1; i < linfo.li_opcode_base; i++)
2776             printf (_("  Opcode %d has %d args\n"), i, standard_opcodes[i - 1]);
2777
2778           /* Display the contents of the Directory table.  */
2779           data = standard_opcodes + linfo.li_opcode_base - 1;
2780
2781           if (*data == 0)
2782             printf (_("\n The Directory Table is empty.\n"));
2783           else
2784             {
2785               printf (_("\n The Directory Table (offset 0x%lx):\n"),
2786                       (long)(data - start));
2787
2788               while (*data != 0)
2789                 {
2790                   printf ("  %d\t%s\n", ++last_dir_entry, data);
2791
2792                   data += strnlen ((char *) data, end - data) + 1;
2793                 }
2794             }
2795
2796           /* Skip the NUL at the end of the table.  */
2797           data++;
2798
2799           /* Display the contents of the File Name table.  */
2800           if (*data == 0)
2801             printf (_("\n The File Name Table is empty.\n"));
2802           else
2803             {
2804               printf (_("\n The File Name Table (offset 0x%lx):\n"),
2805                       (long)(data - start));
2806               printf (_("  Entry\tDir\tTime\tSize\tName\n"));
2807
2808               while (*data != 0)
2809                 {
2810                   unsigned char *name;
2811                   unsigned int bytes_read;
2812
2813                   printf ("  %d\t", ++state_machine_regs.last_file_entry);
2814                   name = data;
2815                   data += strnlen ((char *) data, end - data) + 1;
2816
2817                   printf ("%s\t",
2818                           dwarf_vmatoa ("u", read_uleb128 (data, & bytes_read, end)));
2819                   data += bytes_read;
2820                   printf ("%s\t",
2821                           dwarf_vmatoa ("u", read_uleb128 (data, & bytes_read, end)));
2822                   data += bytes_read;
2823                   printf ("%s\t",
2824                           dwarf_vmatoa ("u", read_uleb128 (data, & bytes_read, end)));
2825                   data += bytes_read;
2826                   printf ("%s\n", name);
2827
2828                   if (data == end)
2829                     {
2830                       warn (_("Corrupt file name table entry\n"));
2831                       break;
2832                     }
2833                 }
2834             }
2835
2836           /* Skip the NUL at the end of the table.  */
2837           data++;
2838           putchar ('\n');
2839           saved_linfo = linfo;
2840         }
2841
2842       /* Now display the statements.  */
2843       if (data >= end_of_sequence)
2844         printf (_(" No Line Number Statements.\n"));
2845       else
2846         {
2847           printf (_(" Line Number Statements:\n"));
2848
2849           while (data < end_of_sequence)
2850             {
2851               unsigned char op_code;
2852               dwarf_signed_vma adv;
2853               dwarf_vma uladv;
2854               unsigned int bytes_read;
2855
2856               printf ("  [0x%08lx]", (long)(data - start));
2857
2858               op_code = *data++;
2859
2860               if (op_code >= linfo.li_opcode_base)
2861                 {
2862                   op_code -= linfo.li_opcode_base;
2863                   uladv = (op_code / linfo.li_line_range);
2864                   if (linfo.li_max_ops_per_insn == 1)
2865                     {
2866                       uladv *= linfo.li_min_insn_length;
2867                       state_machine_regs.address += uladv;
2868                       printf (_("  Special opcode %d: "
2869                                 "advance Address by %s to 0x%s"),
2870                               op_code, dwarf_vmatoa ("u", uladv),
2871                               dwarf_vmatoa ("x", state_machine_regs.address));
2872                     }
2873                   else
2874                     {
2875                       state_machine_regs.address
2876                         += ((state_machine_regs.op_index + uladv)
2877                             / linfo.li_max_ops_per_insn)
2878                         * linfo.li_min_insn_length;
2879                       state_machine_regs.op_index
2880                         = (state_machine_regs.op_index + uladv)
2881                         % linfo.li_max_ops_per_insn;
2882                       printf (_("  Special opcode %d: "
2883                                 "advance Address by %s to 0x%s[%d]"),
2884                               op_code, dwarf_vmatoa ("u", uladv),
2885                               dwarf_vmatoa ("x", state_machine_regs.address),
2886                               state_machine_regs.op_index);
2887                     }
2888                   adv = (op_code % linfo.li_line_range) + linfo.li_line_base;
2889                   state_machine_regs.line += adv;
2890                   printf (_(" and Line by %s to %d\n"),
2891                           dwarf_vmatoa ("d", adv), state_machine_regs.line);
2892                 }
2893               else switch (op_code)
2894                      {
2895                      case DW_LNS_extended_op:
2896                        data += process_extended_line_op (data, linfo.li_default_is_stmt, end);
2897                        break;
2898
2899                      case DW_LNS_copy:
2900                        printf (_("  Copy\n"));
2901                        break;
2902
2903                      case DW_LNS_advance_pc:
2904                        uladv = read_uleb128 (data, & bytes_read, end);
2905                        data += bytes_read;
2906                        if (linfo.li_max_ops_per_insn == 1)
2907                          {
2908                            uladv *= linfo.li_min_insn_length;
2909                            state_machine_regs.address += uladv;
2910                            printf (_("  Advance PC by %s to 0x%s\n"),
2911                                    dwarf_vmatoa ("u", uladv),
2912                                    dwarf_vmatoa ("x", state_machine_regs.address));
2913                          }
2914                        else
2915                          {
2916                            state_machine_regs.address
2917                              += ((state_machine_regs.op_index + uladv)
2918                                  / linfo.li_max_ops_per_insn)
2919                              * linfo.li_min_insn_length;
2920                            state_machine_regs.op_index
2921                              = (state_machine_regs.op_index + uladv)
2922                              % linfo.li_max_ops_per_insn;
2923                            printf (_("  Advance PC by %s to 0x%s[%d]\n"),
2924                                    dwarf_vmatoa ("u", uladv),
2925                                    dwarf_vmatoa ("x", state_machine_regs.address),
2926                                    state_machine_regs.op_index);
2927                          }
2928                        break;
2929
2930                      case DW_LNS_advance_line:
2931                        adv = read_sleb128 (data, & bytes_read, end);
2932                        data += bytes_read;
2933                        state_machine_regs.line += adv;
2934                        printf (_("  Advance Line by %s to %d\n"),
2935                                dwarf_vmatoa ("d", adv),
2936                                state_machine_regs.line);
2937                        break;
2938
2939                      case DW_LNS_set_file:
2940                        adv = read_uleb128 (data, & bytes_read, end);
2941                        data += bytes_read;
2942                        printf (_("  Set File Name to entry %s in the File Name Table\n"),
2943                                dwarf_vmatoa ("d", adv));
2944                        state_machine_regs.file = adv;
2945                        break;
2946
2947                      case DW_LNS_set_column:
2948                        uladv = read_uleb128 (data, & bytes_read, end);
2949                        data += bytes_read;
2950                        printf (_("  Set column to %s\n"),
2951                                dwarf_vmatoa ("u", uladv));
2952                        state_machine_regs.column = uladv;
2953                        break;
2954
2955                      case DW_LNS_negate_stmt:
2956                        adv = state_machine_regs.is_stmt;
2957                        adv = ! adv;
2958                        printf (_("  Set is_stmt to %s\n"), dwarf_vmatoa ("d", adv));
2959                        state_machine_regs.is_stmt = adv;
2960                        break;
2961
2962                      case DW_LNS_set_basic_block:
2963                        printf (_("  Set basic block\n"));
2964                        state_machine_regs.basic_block = 1;
2965                        break;
2966
2967                      case DW_LNS_const_add_pc:
2968                        uladv = ((255 - linfo.li_opcode_base) / linfo.li_line_range);
2969                        if (linfo.li_max_ops_per_insn)
2970                          {
2971                            uladv *= linfo.li_min_insn_length;
2972                            state_machine_regs.address += uladv;
2973                            printf (_("  Advance PC by constant %s to 0x%s\n"),
2974                                    dwarf_vmatoa ("u", uladv),
2975                                    dwarf_vmatoa ("x", state_machine_regs.address));
2976                          }
2977                        else
2978                          {
2979                            state_machine_regs.address
2980                              += ((state_machine_regs.op_index + uladv)
2981                                  / linfo.li_max_ops_per_insn)
2982                              * linfo.li_min_insn_length;
2983                            state_machine_regs.op_index
2984                              = (state_machine_regs.op_index + uladv)
2985                              % linfo.li_max_ops_per_insn;
2986                            printf (_("  Advance PC by constant %s to 0x%s[%d]\n"),
2987                                    dwarf_vmatoa ("u", uladv),
2988                                    dwarf_vmatoa ("x", state_machine_regs.address),
2989                                    state_machine_regs.op_index);
2990                          }
2991                        break;
2992
2993                      case DW_LNS_fixed_advance_pc:
2994                        SAFE_BYTE_GET_AND_INC (uladv, data, 2, end);
2995                        state_machine_regs.address += uladv;
2996                        state_machine_regs.op_index = 0;
2997                        printf (_("  Advance PC by fixed size amount %s to 0x%s\n"),
2998                                dwarf_vmatoa ("u", uladv),
2999                                dwarf_vmatoa ("x", state_machine_regs.address));
3000                        break;
3001
3002                      case DW_LNS_set_prologue_end:
3003                        printf (_("  Set prologue_end to true\n"));
3004                        break;
3005
3006                      case DW_LNS_set_epilogue_begin:
3007                        printf (_("  Set epilogue_begin to true\n"));
3008                        break;
3009
3010                      case DW_LNS_set_isa:
3011                        uladv = read_uleb128 (data, & bytes_read, end);
3012                        data += bytes_read;
3013                        printf (_("  Set ISA to %s\n"), dwarf_vmatoa ("u", uladv));
3014                        break;
3015
3016                      default:
3017                        printf (_("  Unknown opcode %d with operands: "), op_code);
3018
3019                        if (standard_opcodes != NULL)
3020                          for (i = standard_opcodes[op_code - 1]; i > 0 ; --i)
3021                            {
3022                              printf ("0x%s%s", dwarf_vmatoa ("x", read_uleb128 (data,
3023                                                                                 &bytes_read, end)),
3024                                      i == 1 ? "" : ", ");
3025                              data += bytes_read;
3026                            }
3027                        putchar ('\n');
3028                        break;
3029                      }
3030             }
3031           putchar ('\n');
3032         }
3033     }
3034
3035   return 1;
3036 }
3037
3038 typedef struct
3039 {
3040   unsigned char *name;
3041   unsigned int directory_index;
3042   unsigned int modification_date;
3043   unsigned int length;
3044 } File_Entry;
3045
3046 /* Output a decoded representation of the .debug_line section.  */
3047
3048 static int
3049 display_debug_lines_decoded (struct dwarf_section *section,
3050                              unsigned char *data,
3051                              unsigned char *end)
3052 {
3053   static DWARF2_Internal_LineInfo saved_linfo;
3054
3055   printf (_("Decoded dump of debug contents of section %s:\n\n"),
3056           section->name);
3057
3058   while (data < end)
3059     {
3060       /* This loop amounts to one iteration per compilation unit.  */
3061       DWARF2_Internal_LineInfo linfo;
3062       unsigned char *standard_opcodes;
3063       unsigned char *end_of_sequence;
3064       int i;
3065       File_Entry *file_table = NULL;
3066       unsigned int n_files = 0;
3067       unsigned char **directory_table = NULL;
3068       unsigned int n_directories = 0;
3069
3070       if (const_strneq (section->name, ".debug_line.")
3071           /* Note: the following does not apply to .debug_line.dwo sections.
3072              These are full debug_line sections.  */
3073           && strcmp (section->name, ".debug_line.dwo") != 0)
3074         {
3075           /* See comment in display_debug_lines_raw().  */
3076           end_of_sequence = end;
3077           standard_opcodes = NULL;
3078           linfo = saved_linfo;
3079           reset_state_machine (linfo.li_default_is_stmt);
3080         }
3081       else
3082         {
3083           unsigned char *hdrptr;
3084
3085           if ((hdrptr = read_debug_line_header (section, data, end, & linfo,
3086                                                 & end_of_sequence)) == NULL)
3087               return 0;
3088
3089           reset_state_machine (linfo.li_default_is_stmt);
3090
3091           /* Save a pointer to the contents of the Opcodes table.  */
3092           standard_opcodes = hdrptr;
3093
3094           /* Traverse the Directory table just to count entries.  */
3095           data = standard_opcodes + linfo.li_opcode_base - 1;
3096           if (*data != 0)
3097             {
3098               unsigned char *ptr_directory_table = data;
3099
3100               while (*data != 0)
3101                 {
3102                   data += strnlen ((char *) data, end - data) + 1;
3103                   n_directories++;
3104                 }
3105
3106               /* Go through the directory table again to save the directories.  */
3107               directory_table = (unsigned char **)
3108                 xmalloc (n_directories * sizeof (unsigned char *));
3109
3110               i = 0;
3111               while (*ptr_directory_table != 0)
3112                 {
3113                   directory_table[i] = ptr_directory_table;
3114                   ptr_directory_table += strnlen ((char *) ptr_directory_table,
3115                                                   ptr_directory_table - end) + 1;
3116                   i++;
3117                 }
3118             }
3119           /* Skip the NUL at the end of the table.  */
3120           data++;
3121
3122           /* Traverse the File Name table just to count the entries.  */
3123           if (*data != 0)
3124             {
3125               unsigned char *ptr_file_name_table = data;
3126
3127               while (*data != 0)
3128                 {
3129                   unsigned int bytes_read;
3130
3131                   /* Skip Name, directory index, last modification time and length
3132                      of file.  */
3133                   data += strnlen ((char *) data, end - data) + 1;
3134                   read_uleb128 (data, & bytes_read, end);
3135                   data += bytes_read;
3136                   read_uleb128 (data, & bytes_read, end);
3137                   data += bytes_read;
3138                   read_uleb128 (data, & bytes_read, end);
3139                   data += bytes_read;
3140
3141                   n_files++;
3142                 }
3143
3144               /* Go through the file table again to save the strings.  */
3145               file_table = (File_Entry *) xmalloc (n_files * sizeof (File_Entry));
3146
3147               i = 0;
3148               while (*ptr_file_name_table != 0)
3149                 {
3150                   unsigned int bytes_read;
3151
3152                   file_table[i].name = ptr_file_name_table;
3153                   ptr_file_name_table += strnlen ((char *) ptr_file_name_table,
3154                                                   end - ptr_file_name_table) + 1;
3155
3156                   /* We are not interested in directory, time or size.  */
3157                   file_table[i].directory_index = read_uleb128 (ptr_file_name_table,
3158                                                                 & bytes_read, end);
3159                   ptr_file_name_table += bytes_read;
3160                   file_table[i].modification_date = read_uleb128 (ptr_file_name_table,
3161                                                                   & bytes_read, end);
3162                   ptr_file_name_table += bytes_read;
3163                   file_table[i].length = read_uleb128 (ptr_file_name_table, & bytes_read, end);
3164                   ptr_file_name_table += bytes_read;
3165                   i++;
3166                 }
3167               i = 0;
3168
3169               /* Print the Compilation Unit's name and a header.  */
3170               if (directory_table == NULL)
3171                 {
3172                   printf (_("CU: %s:\n"), file_table[0].name);
3173                   printf (_("File name                            Line number    Starting address\n"));
3174                 }
3175               else
3176                 {
3177                   unsigned int ix = file_table[0].directory_index;
3178                   const char *directory = ix ? (char *)directory_table[ix - 1] : ".";
3179
3180                   if (do_wide || strlen (directory) < 76)
3181                     printf (_("CU: %s/%s:\n"), directory, file_table[0].name);
3182                   else
3183                     printf ("%s:\n", file_table[0].name);
3184
3185                   printf (_("File name                            Line number    Starting address\n"));
3186                 }
3187             }
3188
3189           /* Skip the NUL at the end of the table.  */
3190           data++;
3191
3192           saved_linfo = linfo;
3193         }
3194
3195       /* This loop iterates through the Dwarf Line Number Program.  */
3196       while (data < end_of_sequence)
3197         {
3198           unsigned char op_code;
3199           int adv;
3200           unsigned long int uladv;
3201           unsigned int bytes_read;
3202           int is_special_opcode = 0;
3203
3204           op_code = *data++;
3205
3206           if (op_code >= linfo.li_opcode_base)
3207             {
3208               op_code -= linfo.li_opcode_base;
3209               uladv = (op_code / linfo.li_line_range);
3210               if (linfo.li_max_ops_per_insn == 1)
3211                 {
3212                   uladv *= linfo.li_min_insn_length;
3213                   state_machine_regs.address += uladv;
3214                 }
3215               else
3216                 {
3217                   state_machine_regs.address
3218                     += ((state_machine_regs.op_index + uladv)
3219                         / linfo.li_max_ops_per_insn)
3220                     * linfo.li_min_insn_length;
3221                   state_machine_regs.op_index
3222                     = (state_machine_regs.op_index + uladv)
3223                     % linfo.li_max_ops_per_insn;
3224                 }
3225
3226               adv = (op_code % linfo.li_line_range) + linfo.li_line_base;
3227               state_machine_regs.line += adv;
3228               is_special_opcode = 1;
3229             }
3230           else switch (op_code)
3231                  {
3232                  case DW_LNS_extended_op:
3233                    {
3234                      unsigned int ext_op_code_len;
3235                      unsigned char ext_op_code;
3236                      unsigned char *op_code_data = data;
3237
3238                      ext_op_code_len = read_uleb128 (op_code_data, &bytes_read,
3239                                                      end_of_sequence);
3240                      op_code_data += bytes_read;
3241
3242                      if (ext_op_code_len == 0)
3243                        {
3244                          warn (_("Badly formed extended line op encountered!\n"));
3245                          break;
3246                        }
3247                      ext_op_code_len += bytes_read;
3248                      ext_op_code = *op_code_data++;
3249
3250                      switch (ext_op_code)
3251                        {
3252                        case DW_LNE_end_sequence:
3253                          reset_state_machine (linfo.li_default_is_stmt);
3254                          break;
3255                        case DW_LNE_set_address:
3256                          SAFE_BYTE_GET_AND_INC (state_machine_regs.address,
3257                                                 op_code_data,
3258                                                 ext_op_code_len - bytes_read - 1,
3259                                                 end);
3260                          state_machine_regs.op_index = 0;
3261                          break;
3262                        case DW_LNE_define_file:
3263                          {
3264                            file_table = (File_Entry *) xrealloc
3265                              (file_table, (n_files + 1) * sizeof (File_Entry));
3266
3267                            ++state_machine_regs.last_file_entry;
3268                            /* Source file name.  */
3269                            file_table[n_files].name = op_code_data;
3270                            op_code_data += strlen ((char *) op_code_data) + 1;
3271                            /* Directory index.  */
3272                            file_table[n_files].directory_index =
3273                              read_uleb128 (op_code_data, & bytes_read,
3274                                            end_of_sequence);
3275                            op_code_data += bytes_read;
3276                            /* Last modification time.  */
3277                            file_table[n_files].modification_date =
3278                              read_uleb128 (op_code_data, & bytes_read,
3279                                            end_of_sequence);
3280                            op_code_data += bytes_read;
3281                            /* File length.  */
3282                            file_table[n_files].length =
3283                              read_uleb128 (op_code_data, & bytes_read,
3284                                            end_of_sequence);
3285
3286                            n_files++;
3287                            break;
3288                          }
3289                        case DW_LNE_set_discriminator:
3290                        case DW_LNE_HP_set_sequence:
3291                          /* Simply ignored.  */
3292                          break;
3293
3294                        default:
3295                          printf (_("UNKNOWN (%u): length %d\n"),
3296                                  ext_op_code, ext_op_code_len - bytes_read);
3297                          break;
3298                        }
3299                      data += ext_op_code_len;
3300                      break;
3301                    }
3302                  case DW_LNS_copy:
3303                    break;
3304
3305                  case DW_LNS_advance_pc:
3306                    uladv = read_uleb128 (data, & bytes_read, end);
3307                    data += bytes_read;
3308                    if (linfo.li_max_ops_per_insn == 1)
3309                      {
3310                        uladv *= linfo.li_min_insn_length;
3311                        state_machine_regs.address += uladv;
3312                      }
3313                    else
3314                      {
3315                        state_machine_regs.address
3316                          += ((state_machine_regs.op_index + uladv)
3317                              / linfo.li_max_ops_per_insn)
3318                          * linfo.li_min_insn_length;
3319                        state_machine_regs.op_index
3320                          = (state_machine_regs.op_index + uladv)
3321                          % linfo.li_max_ops_per_insn;
3322                      }
3323                    break;
3324
3325                  case DW_LNS_advance_line:
3326                    adv = read_sleb128 (data, & bytes_read, end);
3327                    data += bytes_read;
3328                    state_machine_regs.line += adv;
3329                    break;
3330
3331                  case DW_LNS_set_file:
3332                    adv = read_uleb128 (data, & bytes_read, end);
3333                    data += bytes_read;
3334                    state_machine_regs.file = adv;
3335
3336                    if (file_table == NULL)
3337                      printf (_("\n [Use file table entry %d]\n"), state_machine_regs.file - 1);
3338                    else if (file_table[state_machine_regs.file - 1].directory_index == 0)
3339                      /* If directory index is 0, that means current directory.  */
3340                      printf ("\n./%s:[++]\n",
3341                              file_table[state_machine_regs.file - 1].name);
3342                    else if (directory_table == NULL)
3343                      printf (_("\n [Use directory table entry %d]\n"),
3344                              file_table[state_machine_regs.file - 1].directory_index - 1);
3345                    else
3346                      /* The directory index starts counting at 1.  */
3347                      printf ("\n%s/%s:\n",
3348                              directory_table[file_table[state_machine_regs.file - 1].directory_index - 1],
3349                              file_table[state_machine_regs.file - 1].name);
3350                    break;
3351
3352                  case DW_LNS_set_column:
3353                    uladv = read_uleb128 (data, & bytes_read, end);
3354                    data += bytes_read;
3355                    state_machine_regs.column = uladv;
3356                    break;
3357
3358                  case DW_LNS_negate_stmt:
3359                    adv = state_machine_regs.is_stmt;
3360                    adv = ! adv;
3361                    state_machine_regs.is_stmt = adv;
3362                    break;
3363
3364                  case DW_LNS_set_basic_block:
3365                    state_machine_regs.basic_block = 1;
3366                    break;
3367
3368                  case DW_LNS_const_add_pc:
3369                    uladv = ((255 - linfo.li_opcode_base) / linfo.li_line_range);
3370                    if (linfo.li_max_ops_per_insn == 1)
3371                      {
3372                        uladv *= linfo.li_min_insn_length;
3373                        state_machine_regs.address += uladv;
3374                      }
3375                    else
3376                      {
3377                        state_machine_regs.address
3378                          += ((state_machine_regs.op_index + uladv)
3379                              / linfo.li_max_ops_per_insn)
3380                          * linfo.li_min_insn_length;
3381                        state_machine_regs.op_index
3382                          = (state_machine_regs.op_index + uladv)
3383                          % linfo.li_max_ops_per_insn;
3384                      }
3385                    break;
3386
3387                  case DW_LNS_fixed_advance_pc:
3388                    SAFE_BYTE_GET_AND_INC (uladv, data, 2, end);
3389                    state_machine_regs.address += uladv;
3390                    state_machine_regs.op_index = 0;
3391                    break;
3392
3393                  case DW_LNS_set_prologue_end:
3394                    break;
3395
3396                  case DW_LNS_set_epilogue_begin:
3397                    break;
3398
3399                  case DW_LNS_set_isa:
3400                    uladv = read_uleb128 (data, & bytes_read, end);
3401                    data += bytes_read;
3402                    printf (_("  Set ISA to %lu\n"), uladv);
3403                    break;
3404
3405                  default:
3406                    printf (_("  Unknown opcode %d with operands: "), op_code);
3407
3408                    if (standard_opcodes != NULL)
3409                      for (i = standard_opcodes[op_code - 1]; i > 0 ; --i)
3410                        {
3411                          printf ("0x%s%s", dwarf_vmatoa ("x", read_uleb128 (data,
3412                                                                             &bytes_read, end)),
3413                                  i == 1 ? "" : ", ");
3414                          data += bytes_read;
3415                        }
3416                    putchar ('\n');
3417                    break;
3418                  }
3419
3420           /* Only Special opcodes, DW_LNS_copy and DW_LNE_end_sequence adds a row
3421              to the DWARF address/line matrix.  */
3422           if ((is_special_opcode) || (op_code == DW_LNE_end_sequence)
3423               || (op_code == DW_LNS_copy))
3424             {
3425               const unsigned int MAX_FILENAME_LENGTH = 35;
3426               char *fileName;
3427               char *newFileName = NULL;
3428               size_t fileNameLength;
3429
3430               if (file_table)
3431                 fileName = (char *) file_table[state_machine_regs.file - 1].name;
3432               else
3433                 fileName = "<unknown>";
3434
3435               fileNameLength = strlen (fileName);
3436
3437               if ((fileNameLength > MAX_FILENAME_LENGTH) && (!do_wide))
3438                 {
3439                   newFileName = (char *) xmalloc (MAX_FILENAME_LENGTH + 1);
3440                   /* Truncate file name */
3441                   strncpy (newFileName,
3442                            fileName + fileNameLength - MAX_FILENAME_LENGTH,
3443                            MAX_FILENAME_LENGTH + 1);
3444                 }
3445               else
3446                 {
3447                   newFileName = (char *) xmalloc (fileNameLength + 1);
3448                   strncpy (newFileName, fileName, fileNameLength + 1);
3449                 }
3450
3451               if (!do_wide || (fileNameLength <= MAX_FILENAME_LENGTH))
3452                 {
3453                   if (linfo.li_max_ops_per_insn == 1)
3454                     printf ("%-35s  %11d  %#18" DWARF_VMA_FMT "x\n",
3455                             newFileName, state_machine_regs.line,
3456                             state_machine_regs.address);
3457                   else
3458                     printf ("%-35s  %11d  %#18" DWARF_VMA_FMT "x[%d]\n",
3459                             newFileName, state_machine_regs.line,
3460                             state_machine_regs.address,
3461                             state_machine_regs.op_index);
3462                 }
3463               else
3464                 {
3465                   if (linfo.li_max_ops_per_insn == 1)
3466                     printf ("%s  %11d  %#18" DWARF_VMA_FMT "x\n",
3467                             newFileName, state_machine_regs.line,
3468                             state_machine_regs.address);
3469                   else
3470                     printf ("%s  %11d  %#18" DWARF_VMA_FMT "x[%d]\n",
3471                             newFileName, state_machine_regs.line,
3472                             state_machine_regs.address,
3473                             state_machine_regs.op_index);
3474                 }
3475
3476               if (op_code == DW_LNE_end_sequence)
3477                 printf ("\n");
3478
3479               free (newFileName);
3480             }
3481         }
3482
3483       if (file_table)
3484         {
3485           free (file_table);
3486           file_table = NULL;
3487           n_files = 0;
3488         }
3489
3490       if (directory_table)
3491         {
3492           free (directory_table);
3493           directory_table = NULL;
3494           n_directories = 0;
3495         }
3496
3497       putchar ('\n');
3498     }
3499
3500   return 1;
3501 }
3502
3503 static int
3504 display_debug_lines (struct dwarf_section *section, void *file ATTRIBUTE_UNUSED)
3505 {
3506   unsigned char *data = section->start;
3507   unsigned char *end = data + section->size;
3508   int retValRaw = 1;
3509   int retValDecoded = 1;
3510
3511   if (do_debug_lines == 0)
3512     do_debug_lines |= FLAG_DEBUG_LINES_RAW;
3513
3514   if (do_debug_lines & FLAG_DEBUG_LINES_RAW)
3515     retValRaw = display_debug_lines_raw (section, data, end);
3516
3517   if (do_debug_lines & FLAG_DEBUG_LINES_DECODED)
3518     retValDecoded = display_debug_lines_decoded (section, data, end);
3519
3520   if (!retValRaw || !retValDecoded)
3521     return 0;
3522
3523   return 1;
3524 }
3525
3526 static debug_info *
3527 find_debug_info_for_offset (unsigned long offset)
3528 {
3529   unsigned int i;
3530
3531   if (num_debug_info_entries == DEBUG_INFO_UNAVAILABLE)
3532     return NULL;
3533
3534   for (i = 0; i < num_debug_info_entries; i++)
3535     if (debug_information[i].cu_offset == offset)
3536       return debug_information + i;
3537
3538   return NULL;
3539 }
3540
3541 static const char *
3542 get_gdb_index_symbol_kind_name (gdb_index_symbol_kind kind)
3543 {
3544   /* See gdb/gdb-index.h.  */
3545   static const char * const kinds[] =
3546   {
3547     N_ ("no info"),
3548     N_ ("type"),
3549     N_ ("variable"),
3550     N_ ("function"),
3551     N_ ("other"),
3552     N_ ("unused5"),
3553     N_ ("unused6"),
3554     N_ ("unused7")
3555   };
3556
3557   return _ (kinds[kind]);
3558 }
3559
3560 static int
3561 display_debug_pubnames_worker (struct dwarf_section *section,
3562                                void *file ATTRIBUTE_UNUSED,
3563                                int is_gnu)
3564 {
3565   DWARF2_Internal_PubNames names;
3566   unsigned char *start = section->start;
3567   unsigned char *end = start + section->size;
3568
3569   /* It does not matter if this load fails,
3570      we test for that later on.  */
3571   load_debug_info (file);
3572
3573   printf (_("Contents of the %s section:\n\n"), section->name);
3574
3575   while (start < end)
3576     {
3577       unsigned char *data;
3578       unsigned long offset;
3579       unsigned int offset_size, initial_length_size;
3580
3581       data = start;
3582
3583       SAFE_BYTE_GET_AND_INC (names.pn_length, data, 4, end);
3584       if (names.pn_length == 0xffffffff)
3585         {
3586           SAFE_BYTE_GET_AND_INC (names.pn_length, data, 8, end);
3587           offset_size = 8;
3588           initial_length_size = 12;
3589         }
3590       else
3591         {
3592           offset_size = 4;
3593           initial_length_size = 4;
3594         }
3595
3596       SAFE_BYTE_GET_AND_INC (names.pn_version, data, 2, end);
3597       SAFE_BYTE_GET_AND_INC (names.pn_offset, data, offset_size, end);
3598
3599       if (num_debug_info_entries != DEBUG_INFO_UNAVAILABLE
3600           && num_debug_info_entries > 0
3601           && find_debug_info_for_offset (names.pn_offset) == NULL)
3602         warn (_(".debug_info offset of 0x%lx in %s section does not point to a CU header.\n"),
3603               (unsigned long) names.pn_offset, section->name);
3604
3605       SAFE_BYTE_GET_AND_INC (names.pn_size, data, offset_size, end);
3606
3607       start += names.pn_length + initial_length_size;
3608
3609       if (names.pn_version != 2 && names.pn_version != 3)
3610         {
3611           static int warned = 0;
3612
3613           if (! warned)
3614             {
3615               warn (_("Only DWARF 2 and 3 pubnames are currently supported\n"));
3616               warned = 1;
3617             }
3618
3619           continue;
3620         }
3621
3622       printf (_("  Length:                              %ld\n"),
3623               (long) names.pn_length);
3624       printf (_("  Version:                             %d\n"),
3625               names.pn_version);
3626       printf (_("  Offset into .debug_info section:     0x%lx\n"),
3627               (unsigned long) names.pn_offset);
3628       printf (_("  Size of area in .debug_info section: %ld\n"),
3629               (long) names.pn_size);
3630
3631       if (is_gnu)
3632         printf (_("\n    Offset  Kind          Name\n"));
3633       else
3634         printf (_("\n    Offset\tName\n"));
3635
3636       do
3637         {
3638           bfd_size_type maxprint;
3639
3640           SAFE_BYTE_GET (offset, data, offset_size, end);
3641
3642           if (offset != 0)
3643             {
3644               data += offset_size;
3645               if (data >= end)
3646                 break;
3647               maxprint = (end - data) - 1;
3648               
3649               if (is_gnu)
3650                 {
3651                   unsigned int kind_data;
3652                   gdb_index_symbol_kind kind;
3653                   const char *kind_name;
3654                   int is_static;
3655
3656                   SAFE_BYTE_GET (kind_data, data, 1, end);
3657                   data++;
3658                   maxprint --;
3659                   /* GCC computes the kind as the upper byte in the CU index
3660                      word, and then right shifts it by the CU index size.
3661                      Left shift KIND to where the gdb-index.h accessor macros
3662                      can use it.  */
3663                   kind_data <<= GDB_INDEX_CU_BITSIZE;
3664                   kind = GDB_INDEX_SYMBOL_KIND_VALUE (kind_data);
3665                   kind_name = get_gdb_index_symbol_kind_name (kind);
3666                   is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (kind_data);
3667                   printf ("    %-6lx  %s,%-10s  %.*s\n",
3668                           offset, is_static ? _("s") : _("g"),
3669                           kind_name, (int) maxprint, data);
3670                 }
3671               else
3672                 printf ("    %-6lx\t%.*s\n", offset, (int) maxprint, data);
3673
3674               data += strnlen ((char *) data, maxprint) + 1;
3675               if (data >= end)
3676                 break;
3677             }
3678         }
3679       while (offset != 0);
3680     }
3681
3682   printf ("\n");
3683   return 1;
3684 }
3685
3686 static int
3687 display_debug_pubnames (struct dwarf_section *section, void *file)
3688 {
3689   return display_debug_pubnames_worker (section, file, 0);
3690 }
3691
3692 static int
3693 display_debug_gnu_pubnames (struct dwarf_section *section, void *file)
3694 {
3695   return display_debug_pubnames_worker (section, file, 1);
3696 }
3697
3698 static int
3699 display_debug_macinfo (struct dwarf_section *section,
3700                        void *file ATTRIBUTE_UNUSED)
3701 {
3702   unsigned char *start = section->start;
3703   unsigned char *end = start + section->size;
3704   unsigned char *curr = start;
3705   unsigned int bytes_read;
3706   enum dwarf_macinfo_record_type op;
3707
3708   printf (_("Contents of the %s section:\n\n"), section->name);
3709
3710   while (curr < end)
3711     {
3712       unsigned int lineno;
3713       const unsigned char *string;
3714
3715       op = (enum dwarf_macinfo_record_type) *curr;
3716       curr++;
3717
3718       switch (op)
3719         {
3720         case DW_MACINFO_start_file:
3721           {
3722             unsigned int filenum;
3723
3724             lineno = read_uleb128 (curr, & bytes_read, end);
3725             curr += bytes_read;
3726             filenum = read_uleb128 (curr, & bytes_read, end);
3727             curr += bytes_read;
3728
3729             printf (_(" DW_MACINFO_start_file - lineno: %d filenum: %d\n"),
3730                     lineno, filenum);
3731           }
3732           break;
3733
3734         case DW_MACINFO_end_file:
3735           printf (_(" DW_MACINFO_end_file\n"));
3736           break;
3737
3738         case DW_MACINFO_define:
3739           lineno = read_uleb128 (curr, & bytes_read, end);
3740           curr += bytes_read;
3741           string = curr;
3742           curr += strnlen ((char *) string, end - string) + 1;
3743           printf (_(" DW_MACINFO_define - lineno : %d macro : %s\n"),
3744                   lineno, string);
3745           break;
3746
3747         case DW_MACINFO_undef:
3748           lineno = read_uleb128 (curr, & bytes_read, end);
3749           curr += bytes_read;
3750           string = curr;
3751           curr += strnlen ((char *) string, end - string) + 1;
3752           printf (_(" DW_MACINFO_undef - lineno : %d macro : %s\n"),
3753                   lineno, string);
3754           break;
3755
3756         case DW_MACINFO_vendor_ext:
3757           {
3758             unsigned int constant;
3759
3760             constant = read_uleb128 (curr, & bytes_read, end);
3761             curr += bytes_read;
3762             string = curr;
3763             curr += strnlen ((char *) string, end - string) + 1;
3764             printf (_(" DW_MACINFO_vendor_ext - constant : %d string : %s\n"),
3765                     constant, string);
3766           }
3767           break;
3768         }
3769     }
3770
3771   return 1;
3772 }
3773
3774 /* Given LINE_OFFSET into the .debug_line section, attempt to return
3775    filename and dirname corresponding to file name table entry with index
3776    FILEIDX.  Return NULL on failure.  */
3777
3778 static unsigned char *
3779 get_line_filename_and_dirname (dwarf_vma line_offset,
3780                                dwarf_vma fileidx,
3781                                unsigned char **dir_name)
3782 {
3783   struct dwarf_section *section = &debug_displays [line].section;
3784   unsigned char *hdrptr, *dirtable, *file_name;
3785   unsigned int offset_size, initial_length_size;
3786   unsigned int version, opcode_base, bytes_read;
3787   dwarf_vma length, diridx;
3788   const unsigned char * end;
3789
3790   *dir_name = NULL;
3791   if (section->start == NULL
3792       || line_offset >= section->size
3793       || fileidx == 0)
3794     return NULL;
3795
3796   hdrptr = section->start + line_offset;
3797   end = section->start + section->size;
3798
3799   SAFE_BYTE_GET_AND_INC (length, hdrptr, 4, end);
3800   if (length == 0xffffffff)
3801     {
3802       /* This section is 64-bit DWARF 3.  */
3803       SAFE_BYTE_GET_AND_INC (length, hdrptr, 8, end);
3804       offset_size = 8;
3805       initial_length_size = 12;
3806     }
3807   else
3808     {
3809       offset_size = 4;
3810       initial_length_size = 4;
3811     }
3812   if (length + initial_length_size > section->size)
3813     return NULL;
3814
3815   SAFE_BYTE_GET_AND_INC (version, hdrptr, 2, end);
3816   if (version != 2 && version != 3 && version != 4)
3817     return NULL;
3818   hdrptr += offset_size + 1;/* Skip prologue_length and min_insn_length.  */
3819   if (version >= 4)
3820     hdrptr++;               /* Skip max_ops_per_insn.  */
3821   hdrptr += 3;              /* Skip default_is_stmt, line_base, line_range.  */
3822
3823   SAFE_BYTE_GET_AND_INC (opcode_base, hdrptr, 1, end);
3824   if (opcode_base == 0)
3825     return NULL;
3826
3827   hdrptr += opcode_base - 1;
3828   dirtable = hdrptr;
3829   /* Skip over dirname table.  */
3830   while (*hdrptr != '\0')
3831     hdrptr += strnlen ((char *) hdrptr, end - hdrptr) + 1;
3832   hdrptr++;                 /* Skip the NUL at the end of the table.  */
3833   /* Now skip over preceding filename table entries.  */
3834   for (; *hdrptr != '\0' && fileidx > 1; fileidx--)
3835     {
3836       hdrptr += strnlen ((char *) hdrptr, end - hdrptr) + 1;
3837       read_uleb128 (hdrptr, &bytes_read, end);
3838       hdrptr += bytes_read;
3839       read_uleb128 (hdrptr, &bytes_read, end);
3840       hdrptr += bytes_read;
3841       read_uleb128 (hdrptr, &bytes_read, end);
3842       hdrptr += bytes_read;
3843     }
3844   if (hdrptr == end || *hdrptr == '\0')
3845     return NULL;
3846   file_name = hdrptr;
3847   hdrptr += strnlen ((char *) hdrptr, end - hdrptr) + 1;
3848   diridx = read_uleb128 (hdrptr, &bytes_read, end);
3849   if (diridx == 0)
3850     return file_name;
3851   for (; *dirtable != '\0' && diridx > 1; diridx--)
3852     dirtable += strnlen ((char *) dirtable, end - dirtable) + 1;
3853   if (*dirtable == '\0')
3854     return NULL;
3855   *dir_name = dirtable;
3856   return file_name;
3857 }
3858
3859 static int
3860 display_debug_macro (struct dwarf_section *section,
3861                      void *file)
3862 {
3863   unsigned char *start = section->start;
3864   unsigned char *end = start + section->size;
3865   unsigned char *curr = start;
3866   unsigned char *extended_op_buf[256];
3867   unsigned int bytes_read;
3868
3869   load_debug_section (str, file);
3870   load_debug_section (line, file);
3871
3872   printf (_("Contents of the %s section:\n\n"), section->name);
3873
3874   while (curr < end)
3875     {
3876       unsigned int lineno, version, flags;
3877       unsigned int offset_size = 4;
3878       const unsigned char *string;
3879       dwarf_vma line_offset = 0, sec_offset = curr - start, offset;
3880       unsigned char **extended_ops = NULL;
3881
3882       SAFE_BYTE_GET_AND_INC (version, curr, 2, end);
3883       if (version != 4)
3884         {
3885           error (_("Only GNU extension to DWARF 4 of %s is currently supported.\n"),
3886                  section->name);
3887           return 0;
3888         }
3889
3890       SAFE_BYTE_GET_AND_INC (flags, curr, 1, end);
3891       if (flags & 1)
3892         offset_size = 8;
3893       printf (_("  Offset:                      0x%lx\n"),
3894               (unsigned long) sec_offset);
3895       printf (_("  Version:                     %d\n"), version);
3896       printf (_("  Offset size:                 %d\n"), offset_size);
3897       if (flags & 2)
3898         {
3899           SAFE_BYTE_GET_AND_INC (line_offset, curr, offset_size, end);
3900           printf (_("  Offset into .debug_line:     0x%lx\n"),
3901                   (unsigned long) line_offset);
3902         }
3903       if (flags & 4)
3904         {
3905           unsigned int i, count, op;
3906           dwarf_vma nargs, n;
3907
3908           SAFE_BYTE_GET_AND_INC (count, curr, 1, end);
3909
3910           memset (extended_op_buf, 0, sizeof (extended_op_buf));
3911           extended_ops = extended_op_buf;
3912           if (count)
3913             {
3914               printf (_("  Extension opcode arguments:\n"));
3915               for (i = 0; i < count; i++)
3916                 {
3917                   SAFE_BYTE_GET_AND_INC (op, curr, 1, end);
3918                   extended_ops[op] = curr;
3919                   nargs = read_uleb128 (curr, &bytes_read, end);
3920                   curr += bytes_read;
3921                   if (nargs == 0)
3922                     printf (_("    DW_MACRO_GNU_%02x has no arguments\n"), op);
3923                   else
3924                     {
3925                       printf (_("    DW_MACRO_GNU_%02x arguments: "), op);
3926                       for (n = 0; n < nargs; n++)
3927                         {
3928                           unsigned int form;
3929
3930                           SAFE_BYTE_GET_AND_INC (form, curr, 1, end);
3931                           printf ("%s%s", get_FORM_name (form),
3932                                   n == nargs - 1 ? "\n" : ", ");
3933                           switch (form)
3934                             {
3935                             case DW_FORM_data1:
3936                             case DW_FORM_data2:
3937                             case DW_FORM_data4:
3938                             case DW_FORM_data8:
3939                             case DW_FORM_sdata:
3940                             case DW_FORM_udata:
3941                             case DW_FORM_block:
3942                             case DW_FORM_block1:
3943                             case DW_FORM_block2:
3944                             case DW_FORM_block4:
3945                             case DW_FORM_flag:
3946                             case DW_FORM_string:
3947                             case DW_FORM_strp:
3948                             case DW_FORM_sec_offset:
3949                               break;
3950                             default:
3951                               error (_("Invalid extension opcode form %s\n"),
3952                                      get_FORM_name (form));
3953                               return 0;
3954                             }
3955                         }
3956                     }
3957                 }
3958             }
3959         }
3960       printf ("\n");
3961
3962       while (1)
3963         {
3964           unsigned int op;
3965
3966           if (curr >= end)
3967             {
3968               error (_(".debug_macro section not zero terminated\n"));
3969               return 0;
3970             }
3971
3972           SAFE_BYTE_GET_AND_INC (op, curr, 1, end);
3973           if (op == 0)
3974             break;
3975
3976           switch (op)
3977             {
3978             case DW_MACRO_GNU_start_file:
3979               {
3980                 unsigned int filenum;
3981                 unsigned char *file_name = NULL, *dir_name = NULL;
3982
3983                 lineno = read_uleb128 (curr, &bytes_read, end);
3984                 curr += bytes_read;
3985                 filenum = read_uleb128 (curr, &bytes_read, end);
3986                 curr += bytes_read;
3987
3988                 if ((flags & 2) == 0)
3989                   error (_("DW_MACRO_GNU_start_file used, but no .debug_line offset provided.\n"));
3990                 else
3991                   file_name
3992                     = get_line_filename_and_dirname (line_offset, filenum,
3993                                                      &dir_name);
3994                 if (file_name == NULL)
3995                   printf (_(" DW_MACRO_GNU_start_file - lineno: %d filenum: %d\n"),
3996                           lineno, filenum);
3997                 else
3998                   printf (_(" DW_MACRO_GNU_start_file - lineno: %d filenum: %d filename: %s%s%s\n"),
3999                           lineno, filenum,
4000                           dir_name != NULL ? (const char *) dir_name : "",
4001                           dir_name != NULL ? "/" : "", file_name);
4002               }
4003               break;
4004
4005             case DW_MACRO_GNU_end_file:
4006               printf (_(" DW_MACRO_GNU_end_file\n"));
4007               break;
4008
4009             case DW_MACRO_GNU_define:
4010               lineno = read_uleb128 (curr, &bytes_read, end);
4011               curr += bytes_read;
4012               string = curr;
4013               curr += strnlen ((char *) string, end - string) + 1;
4014               printf (_(" DW_MACRO_GNU_define - lineno : %d macro : %s\n"),
4015                       lineno, string);
4016               break;
4017
4018             case DW_MACRO_GNU_undef:
4019               lineno = read_uleb128 (curr, &bytes_read, end);
4020               curr += bytes_read;
4021               string = curr;
4022               curr += strnlen ((char *) string, end - string) + 1;
4023               printf (_(" DW_MACRO_GNU_undef - lineno : %d macro : %s\n"),
4024                       lineno, string);
4025               break;
4026
4027             case DW_MACRO_GNU_define_indirect:
4028               lineno = read_uleb128 (curr, &bytes_read, end);
4029               curr += bytes_read;
4030               SAFE_BYTE_GET_AND_INC (offset, curr, offset_size, end);
4031               string = fetch_indirect_string (offset);
4032               printf (_(" DW_MACRO_GNU_define_indirect - lineno : %d macro : %s\n"),
4033                       lineno, string);
4034               break;
4035
4036             case DW_MACRO_GNU_undef_indirect:
4037               lineno = read_uleb128 (curr, &bytes_read, end);
4038               curr += bytes_read;
4039               SAFE_BYTE_GET_AND_INC (offset, curr, offset_size, end);
4040               string = fetch_indirect_string (offset);
4041               printf (_(" DW_MACRO_GNU_undef_indirect - lineno : %d macro : %s\n"),
4042                       lineno, string);
4043               break;
4044
4045             case DW_MACRO_GNU_transparent_include:
4046               SAFE_BYTE_GET_AND_INC (offset, curr, offset_size, end);
4047               printf (_(" DW_MACRO_GNU_transparent_include - offset : 0x%lx\n"),
4048                       (unsigned long) offset);
4049               break;
4050
4051             case DW_MACRO_GNU_define_indirect_alt:
4052               lineno = read_uleb128 (curr, &bytes_read, end);
4053               curr += bytes_read;
4054               SAFE_BYTE_GET_AND_INC (offset, curr, offset_size, end);
4055               printf (_(" DW_MACRO_GNU_define_indirect_alt - lineno : %d macro offset : 0x%lx\n"),
4056                       lineno, (unsigned long) offset);
4057               break;
4058
4059             case DW_MACRO_GNU_undef_indirect_alt:
4060               lineno = read_uleb128 (curr, &bytes_read, end);
4061               curr += bytes_read;
4062               SAFE_BYTE_GET_AND_INC (offset, curr, offset_size, end);
4063               printf (_(" DW_MACRO_GNU_undef_indirect_alt - lineno : %d macro offset : 0x%lx\n"),
4064                       lineno, (unsigned long) offset);
4065               break;
4066
4067             case DW_MACRO_GNU_transparent_include_alt:
4068               SAFE_BYTE_GET_AND_INC (offset, curr, offset_size, end);
4069               printf (_(" DW_MACRO_GNU_transparent_include_alt - offset : 0x%lx\n"),
4070                       (unsigned long) offset);
4071               break;
4072
4073             default:
4074               if (extended_ops == NULL || extended_ops[op] == NULL)
4075                 {
4076                   error (_(" Unknown macro opcode %02x seen\n"), op);
4077                   return 0;
4078                 }
4079               else
4080                 {
4081                   /* Skip over unhandled opcodes.  */
4082                   dwarf_vma nargs, n;
4083                   unsigned char *desc = extended_ops[op];
4084                   nargs = read_uleb128 (desc, &bytes_read, end);
4085                   desc += bytes_read;
4086                   if (nargs == 0)
4087                     {
4088                       printf (_(" DW_MACRO_GNU_%02x\n"), op);
4089                       break;
4090                     }
4091                   printf (_(" DW_MACRO_GNU_%02x -"), op);
4092                   for (n = 0; n < nargs; n++)
4093                     {
4094                       int val;
4095
4096                       SAFE_BYTE_GET_AND_INC (val, desc, 1, end);
4097                       curr
4098                         = read_and_display_attr_value (0, val,
4099                                                        curr, end, 0, 0, offset_size,
4100                                                        version, NULL, 0, NULL,
4101                                                        NULL);
4102                       if (n != nargs - 1)
4103                         printf (",");
4104                     }
4105                   printf ("\n");
4106                 }
4107               break;
4108             }
4109         }
4110
4111       printf ("\n");
4112     }   
4113
4114   return 1;
4115 }
4116
4117 static int
4118 display_debug_abbrev (struct dwarf_section *section,
4119                       void *file ATTRIBUTE_UNUSED)
4120 {
4121   abbrev_entry *entry;
4122   unsigned char *start = section->start;
4123   unsigned char *end = start + section->size;
4124
4125   printf (_("Contents of the %s section:\n\n"), section->name);
4126
4127   do
4128     {
4129       unsigned char *last;
4130
4131       free_abbrevs ();
4132
4133       last = start;
4134       start = process_abbrev_section (start, end);
4135
4136       if (first_abbrev == NULL)
4137         continue;
4138
4139       printf (_("  Number TAG (0x%lx)\n"), (long) (last - section->start));
4140
4141       for (entry = first_abbrev; entry; entry = entry->next)
4142         {
4143           abbrev_attr *attr;
4144
4145           printf ("   %ld      %s    [%s]\n",
4146                   entry->entry,
4147                   get_TAG_name (entry->tag),
4148                   entry->children ? _("has children") : _("no children"));
4149
4150           for (attr = entry->first_attr; attr; attr = attr->next)
4151             printf ("    %-18s %s\n",
4152                     get_AT_name (attr->attribute),
4153                     get_FORM_name (attr->form));
4154         }
4155     }
4156   while (start);
4157
4158   printf ("\n");
4159
4160   return 1;
4161 }
4162
4163 /* Display a location list from a normal (ie, non-dwo) .debug_loc section.  */
4164
4165 static void
4166 display_loc_list (struct dwarf_section *section,
4167                   unsigned char **start_ptr,
4168                   int debug_info_entry,
4169                   unsigned long offset,
4170                   unsigned long base_address,
4171                   int has_frame_base)
4172 {
4173   unsigned char *start = *start_ptr;
4174   unsigned char *section_end = section->start + section->size;
4175   unsigned long cu_offset = debug_information [debug_info_entry].cu_offset;
4176   unsigned int pointer_size = debug_information [debug_info_entry].pointer_size;
4177   unsigned int offset_size = debug_information [debug_info_entry].offset_size;
4178   int dwarf_version = debug_information [debug_info_entry].dwarf_version;
4179
4180   dwarf_vma begin;
4181   dwarf_vma end;
4182   unsigned short length;
4183   int need_frame_base;
4184
4185   if (pointer_size < 2 || pointer_size > 8)
4186     {
4187       warn (_("Invalid pointer size (%d) in debug info for entry %d\n"),
4188             pointer_size, debug_info_entry);
4189       return;
4190     }
4191
4192   while (1)
4193     {
4194       if (start + 2 * pointer_size > section_end)
4195         {
4196           warn (_("Location list starting at offset 0x%lx is not terminated.\n"),
4197                 offset);
4198           break;
4199         }
4200
4201       printf ("    %8.8lx ", offset + (start - *start_ptr));
4202
4203       /* Note: we use sign extension here in order to be sure that we can detect
4204          the -1 escape value.  Sign extension into the top 32 bits of a 32-bit
4205          address will not affect the values that we display since we always show
4206          hex values, and always the bottom 32-bits.  */
4207       SAFE_BYTE_GET_AND_INC (begin, start, pointer_size, section_end);
4208       SAFE_BYTE_GET_AND_INC (end, start, pointer_size, section_end);
4209
4210       if (begin == 0 && end == 0)
4211         {
4212           printf (_("<End of list>\n"));
4213           break;
4214         }
4215
4216       /* Check base address specifiers.  */
4217       if (begin == (dwarf_vma) -1 && end != (dwarf_vma) -1)
4218         {
4219           base_address = end;
4220           print_dwarf_vma (begin, pointer_size);
4221           print_dwarf_vma (end, pointer_size);
4222           printf (_("(base address)\n"));
4223           continue;
4224         }
4225
4226       if (start + 2 > section_end)
4227         {
4228           warn (_("Location list starting at offset 0x%lx is not terminated.\n"),
4229                 offset);
4230           break;
4231         }
4232
4233       SAFE_BYTE_GET_AND_INC (length, start, 2, section_end);
4234
4235       if (start + length > section_end)
4236         {
4237           warn (_("Location list starting at offset 0x%lx is not terminated.\n"),
4238                 offset);
4239           break;
4240         }
4241
4242       print_dwarf_vma (begin + base_address, pointer_size);
4243       print_dwarf_vma (end + base_address, pointer_size);
4244
4245       putchar ('(');
4246       need_frame_base = decode_location_expression (start,
4247                                                     pointer_size,
4248                                                     offset_size,
4249                                                     dwarf_version,
4250                                                     length,
4251                                                     cu_offset, section);
4252       putchar (')');
4253
4254       if (need_frame_base && !has_frame_base)
4255         printf (_(" [without DW_AT_frame_base]"));
4256
4257       if (begin == end)
4258         fputs (_(" (start == end)"), stdout);
4259       else if (begin > end)
4260         fputs (_(" (start > end)"), stdout);
4261
4262       putchar ('\n');
4263
4264       start += length;
4265     }
4266
4267   *start_ptr = start;
4268 }
4269
4270 /* Print a .debug_addr table index in decimal, surrounded by square brackets,
4271    right-adjusted in a field of length LEN, and followed by a space.  */
4272
4273 static void
4274 print_addr_index (unsigned int idx, unsigned int len)
4275 {
4276   static char buf[15];
4277   snprintf (buf, sizeof (buf), "[%d]", idx);
4278   printf ("%*s ", len, buf);
4279 }
4280
4281 /* Display a location list from a .dwo section. It uses address indexes rather
4282    than embedded addresses.  This code closely follows display_loc_list, but the
4283    two are sufficiently different that combining things is very ugly.  */
4284
4285 static void
4286 display_loc_list_dwo (struct dwarf_section *section,
4287                       unsigned char **start_ptr,
4288                       int debug_info_entry,
4289                       unsigned long offset,
4290                       int has_frame_base)
4291 {
4292   unsigned char *start = *start_ptr;
4293   unsigned char *section_end = section->start + section->size;
4294   unsigned long cu_offset = debug_information [debug_info_entry].cu_offset;
4295   unsigned int pointer_size = debug_information [debug_info_entry].pointer_size;
4296   unsigned int offset_size = debug_information [debug_info_entry].offset_size;
4297   int dwarf_version = debug_information [debug_info_entry].dwarf_version;
4298   int entry_type;
4299   unsigned short length;
4300   int need_frame_base;
4301   unsigned int idx;
4302   unsigned int bytes_read;
4303
4304   if (pointer_size < 2 || pointer_size > 8)
4305     {
4306       warn (_("Invalid pointer size (%d) in debug info for entry %d\n"),
4307             pointer_size, debug_info_entry);
4308       return;
4309     }
4310
4311   while (1)
4312     {
4313       printf ("    %8.8lx ", offset + (start - *start_ptr));
4314
4315       if (start >= section_end)
4316         {
4317           warn (_("Location list starting at offset 0x%lx is not terminated.\n"),
4318                 offset);
4319           break;
4320         }
4321
4322       SAFE_BYTE_GET_AND_INC (entry_type, start, 1, section_end);
4323       switch (entry_type)
4324         {
4325           case 0: /* A terminating entry.  */
4326             *start_ptr = start;
4327             printf (_("<End of list>\n"));
4328             return;
4329           case 1: /* A base-address entry.  */
4330             idx = read_uleb128 (start, &bytes_read, section_end);
4331             start += bytes_read;
4332             print_addr_index (idx, 8);
4333             printf ("         ");
4334             printf (_("(base address selection entry)\n"));
4335             continue;
4336           case 2: /* A start/end entry.  */
4337             idx = read_uleb128 (start, &bytes_read, section_end);
4338             start += bytes_read;
4339             print_addr_index (idx, 8);
4340             idx = read_uleb128 (start, &bytes_read, section_end);
4341             start += bytes_read;
4342             print_addr_index (idx, 8);
4343             break;
4344           case 3: /* A start/length entry.  */
4345             idx = read_uleb128 (start, &bytes_read, section_end);
4346             start += bytes_read;
4347             print_addr_index (idx, 8);
4348             SAFE_BYTE_GET_AND_INC (idx, start, 4, section_end);
4349             printf ("%08x ", idx);
4350             break;
4351           case 4: /* An offset pair entry.  */
4352             SAFE_BYTE_GET_AND_INC (idx, start, 4, section_end);
4353             printf ("%08x ", idx);
4354             SAFE_BYTE_GET_AND_INC (idx, start, 4, section_end);
4355             printf ("%08x ", idx);
4356             break;
4357           default:
4358             warn (_("Unknown location list entry type 0x%x.\n"), entry_type);
4359             *start_ptr = start;
4360             return;
4361         }
4362
4363       if (start + 2 > section_end)
4364         {
4365           warn (_("Location list starting at offset 0x%lx is not terminated.\n"),
4366                 offset);
4367           break;
4368         }
4369
4370       SAFE_BYTE_GET_AND_INC (length, start, 2, section_end);
4371       if (start + length > section_end)
4372         {
4373           warn (_("Location list starting at offset 0x%lx is not terminated.\n"),
4374                 offset);
4375           break;
4376         }
4377
4378       putchar ('(');
4379       need_frame_base = decode_location_expression (start,
4380                                                     pointer_size,
4381                                                     offset_size,
4382                                                     dwarf_version,
4383                                                     length,
4384                                                     cu_offset, section);
4385       putchar (')');
4386
4387       if (need_frame_base && !has_frame_base)
4388         printf (_(" [without DW_AT_frame_base]"));
4389
4390       putchar ('\n');
4391
4392       start += length;
4393     }
4394
4395   *start_ptr = start;
4396 }
4397
4398 /* Sort array of indexes in ascending order of loc_offsets[idx].  */
4399
4400 static dwarf_vma *loc_offsets;
4401
4402 static int
4403 loc_offsets_compar (const void *ap, const void *bp)
4404 {
4405   dwarf_vma a = loc_offsets[*(const unsigned int *) ap];
4406   dwarf_vma b = loc_offsets[*(const unsigned int *) bp];
4407
4408   return (a > b) - (b > a);
4409 }
4410
4411 static int
4412 display_debug_loc (struct dwarf_section *section, void *file)
4413 {
4414   unsigned char *start = section->start;
4415   unsigned long bytes;
4416   unsigned char *section_begin = start;
4417   unsigned int num_loc_list = 0;
4418   unsigned long last_offset = 0;
4419   unsigned int first = 0;
4420   unsigned int i;
4421   unsigned int j;
4422   unsigned int k;
4423   int seen_first_offset = 0;
4424   int locs_sorted = 1;
4425   unsigned char *next;
4426   unsigned int *array = NULL;
4427   const char *suffix = strrchr (section->name, '.');
4428   int is_dwo = 0;
4429
4430   if (suffix && strcmp (suffix, ".dwo") == 0)
4431     is_dwo = 1;
4432
4433   bytes = section->size;
4434
4435   if (bytes == 0)
4436     {
4437       printf (_("\nThe %s section is empty.\n"), section->name);
4438       return 0;
4439     }
4440
4441   if (load_debug_info (file) == 0)
4442     {
4443       warn (_("Unable to load/parse the .debug_info section, so cannot interpret the %s section.\n"),
4444             section->name);
4445       return 0;
4446     }
4447
4448   /* Check the order of location list in .debug_info section. If
4449      offsets of location lists are in the ascending order, we can
4450      use `debug_information' directly.  */
4451   for (i = 0; i < num_debug_info_entries; i++)
4452     {
4453       unsigned int num;
4454
4455       num = debug_information [i].num_loc_offsets;
4456       if (num > num_loc_list)
4457         num_loc_list = num;
4458
4459       /* Check if we can use `debug_information' directly.  */
4460       if (locs_sorted && num != 0)
4461         {
4462           if (!seen_first_offset)
4463             {
4464               /* This is the first location list.  */
4465               last_offset = debug_information [i].loc_offsets [0];
4466               first = i;
4467               seen_first_offset = 1;
4468               j = 1;
4469             }
4470           else
4471             j = 0;
4472
4473           for (; j < num; j++)
4474             {
4475               if (last_offset >
4476                   debug_information [i].loc_offsets [j])
4477                 {
4478                   locs_sorted = 0;
4479                   break;
4480                 }
4481               last_offset = debug_information [i].loc_offsets [j];
4482             }
4483         }
4484     }
4485
4486   if (!seen_first_offset)
4487     error (_("No location lists in .debug_info section!\n"));
4488
4489   if (debug_information [first].num_loc_offsets > 0
4490       && debug_information [first].loc_offsets [0] != 0)
4491     warn (_("Location lists in %s section start at 0x%s\n"),
4492           section->name,
4493           dwarf_vmatoa ("x", debug_information [first].loc_offsets [0]));
4494
4495   if (!locs_sorted)
4496     array = (unsigned int *) xcmalloc (num_loc_list, sizeof (unsigned int));
4497   printf (_("Contents of the %s section:\n\n"), section->name);
4498   printf (_("    Offset   Begin    End      Expression\n"));
4499
4500   seen_first_offset = 0;
4501   for (i = first; i < num_debug_info_entries; i++)
4502     {
4503       unsigned long offset;
4504       unsigned long base_address;
4505       int has_frame_base;
4506
4507       if (!locs_sorted)
4508         {
4509           for (k = 0; k < debug_information [i].num_loc_offsets; k++)
4510             array[k] = k;
4511           loc_offsets = debug_information [i].loc_offsets;
4512           qsort (array, debug_information [i].num_loc_offsets,
4513                  sizeof (*array), loc_offsets_compar);
4514         }
4515
4516       for (k = 0; k < debug_information [i].num_loc_offsets; k++)
4517         {
4518           j = locs_sorted ? k : array[k];
4519           if (k
4520               && debug_information [i].loc_offsets [locs_sorted
4521                                                     ? k - 1 : array [k - 1]]
4522                  == debug_information [i].loc_offsets [j])
4523             continue;
4524           has_frame_base = debug_information [i].have_frame_base [j];
4525           offset = debug_information [i].loc_offsets [j];
4526           next = section_begin + offset;
4527           base_address = debug_information [i].base_address;
4528
4529           if (!seen_first_offset)
4530             seen_first_offset = 1;
4531           else
4532             {
4533               if (start < next)
4534                 warn (_("There is a hole [0x%lx - 0x%lx] in .debug_loc section.\n"),
4535                       (unsigned long) (start - section_begin),
4536                       (unsigned long) (next - section_begin));
4537               else if (start > next)
4538                 warn (_("There is an overlap [0x%lx - 0x%lx] in .debug_loc section.\n"),
4539                       (unsigned long) (start - section_begin),
4540                       (unsigned long) (next - section_begin));
4541             }
4542           start = next;
4543
4544           if (offset >= bytes)
4545             {
4546               warn (_("Offset 0x%lx is bigger than .debug_loc section size.\n"),
4547                     offset);
4548               continue;
4549             }
4550
4551           if (is_dwo)
4552             display_loc_list_dwo (section, &start, i, offset, has_frame_base);
4553           else
4554             display_loc_list (section, &start, i, offset, base_address,
4555                               has_frame_base);
4556         }
4557     }
4558
4559   if (start < section->start + section->size)
4560     warn (_("There are %ld unused bytes at the end of section %s\n"),
4561           (long) (section->start + section->size - start), section->name);
4562   putchar ('\n');
4563   free (array);
4564   return 1;
4565 }
4566
4567 static int
4568 display_debug_str (struct dwarf_section *section,
4569                    void *file ATTRIBUTE_UNUSED)
4570 {
4571   unsigned char *start = section->start;
4572   unsigned long bytes = section->size;
4573   dwarf_vma addr = section->address;
4574
4575   if (bytes == 0)
4576     {
4577       printf (_("\nThe %s section is empty.\n"), section->name);
4578       return 0;
4579     }
4580
4581   printf (_("Contents of the %s section:\n\n"), section->name);
4582
4583   while (bytes)
4584     {
4585       int j;
4586       int k;
4587       int lbytes;
4588
4589       lbytes = (bytes > 16 ? 16 : bytes);
4590
4591       printf ("  0x%8.8lx ", (unsigned long) addr);
4592
4593       for (j = 0; j < 16; j++)
4594         {
4595           if (j < lbytes)
4596             printf ("%2.2x", start[j]);
4597           else
4598             printf ("  ");
4599
4600           if ((j & 3) == 3)
4601             printf (" ");
4602         }
4603
4604       for (j = 0; j < lbytes; j++)
4605         {
4606           k = start[j];
4607           if (k >= ' ' && k < 0x80)
4608             printf ("%c", k);
4609           else
4610             printf (".");
4611         }
4612
4613       putchar ('\n');
4614
4615       start += lbytes;
4616       addr  += lbytes;
4617       bytes -= lbytes;
4618     }
4619
4620   putchar ('\n');
4621
4622   return 1;
4623 }
4624
4625 static int
4626 display_debug_info (struct dwarf_section *section, void *file)
4627 {
4628   return process_debug_info (section, file, section->abbrev_sec, 0, 0);
4629 }
4630
4631 static int
4632 display_debug_types (struct dwarf_section *section, void *file)
4633 {
4634   return process_debug_info (section, file, section->abbrev_sec, 0, 1);
4635 }
4636
4637 static int
4638 display_trace_info (struct dwarf_section *section, void *file)
4639 {
4640   return process_debug_info (section, file, section->abbrev_sec, 0, 0);
4641 }
4642
4643 static int
4644 display_debug_aranges (struct dwarf_section *section,
4645                        void *file ATTRIBUTE_UNUSED)
4646 {
4647   unsigned char *start = section->start;
4648   unsigned char *end = start + section->size;
4649
4650   printf (_("Contents of the %s section:\n\n"), section->name);
4651
4652   /* It does not matter if this load fails,
4653      we test for that later on.  */
4654   load_debug_info (file);
4655
4656   while (start < end)
4657     {
4658       unsigned char *hdrptr;
4659       DWARF2_Internal_ARange arange;
4660       unsigned char *addr_ranges;
4661       dwarf_vma length;
4662       dwarf_vma address;
4663       unsigned char address_size;
4664       int excess;
4665       unsigned int offset_size;
4666       unsigned int initial_length_size;
4667
4668       hdrptr = start;
4669
4670       SAFE_BYTE_GET_AND_INC (arange.ar_length, hdrptr, 4, end);
4671       if (arange.ar_length == 0xffffffff)
4672         {
4673           SAFE_BYTE_GET_AND_INC (arange.ar_length, hdrptr, 8, end);
4674           offset_size = 8;
4675           initial_length_size = 12;
4676         }
4677       else
4678         {
4679           offset_size = 4;
4680           initial_length_size = 4;
4681         }
4682
4683       SAFE_BYTE_GET_AND_INC (arange.ar_version, hdrptr, 2, end);
4684       SAFE_BYTE_GET_AND_INC (arange.ar_info_offset, hdrptr, offset_size, end);
4685
4686       if (num_debug_info_entries != DEBUG_INFO_UNAVAILABLE
4687           && num_debug_info_entries > 0
4688           && find_debug_info_for_offset (arange.ar_info_offset) == NULL)
4689         warn (_(".debug_info offset of 0x%lx in %s section does not point to a CU header.\n"),
4690               (unsigned long) arange.ar_info_offset, section->name);
4691
4692       SAFE_BYTE_GET_AND_INC (arange.ar_pointer_size, hdrptr, 1, end);
4693       SAFE_BYTE_GET_AND_INC (arange.ar_segment_size, hdrptr, 1, end);
4694
4695       if (arange.ar_version != 2 && arange.ar_version != 3)
4696         {
4697           warn (_("Only DWARF 2 and 3 aranges are currently supported.\n"));
4698           break;
4699         }
4700
4701       printf (_("  Length:                   %ld\n"),
4702               (long) arange.ar_length);
4703       printf (_("  Version:                  %d\n"), arange.ar_version);
4704       printf (_("  Offset into .debug_info:  0x%lx\n"),
4705               (unsigned long) arange.ar_info_offset);
4706       printf (_("  Pointer Size:             %d\n"), arange.ar_pointer_size);
4707       printf (_("  Segment Size:             %d\n"), arange.ar_segment_size);
4708
4709       address_size = arange.ar_pointer_size + arange.ar_segment_size;
4710
4711       /* PR 17512: file: 001-108546-0.001:0.1.  */
4712       if (address_size == 0 || address_size > 8)
4713         {
4714           error (_("Invalid address size in %s section!\n"),
4715                  section->name);
4716           break;
4717         }
4718
4719       /* The DWARF spec does not require that the address size be a power
4720          of two, but we do.  This will have to change if we ever encounter
4721          an uneven architecture.  */
4722       if ((address_size & (address_size - 1)) != 0)
4723         {
4724           warn (_("Pointer size + Segment size is not a power of two.\n"));
4725           break;
4726         }
4727
4728       if (address_size > 4)
4729         printf (_("\n    Address            Length\n"));
4730       else
4731         printf (_("\n    Address    Length\n"));
4732
4733       addr_ranges = hdrptr;
4734
4735       /* Must pad to an alignment boundary that is twice the address size.  */
4736       excess = (hdrptr - start) % (2 * address_size);
4737       if (excess)
4738         addr_ranges += (2 * address_size) - excess;
4739
4740       start += arange.ar_length + initial_length_size;
4741
4742       while (addr_ranges + 2 * address_size <= start)
4743         {
4744           SAFE_BYTE_GET_AND_INC (address, addr_ranges, address_size, end);
4745           SAFE_BYTE_GET_AND_INC (length, addr_ranges, address_size, end);
4746
4747           printf ("    ");
4748           print_dwarf_vma (address, address_size);
4749           print_dwarf_vma (length, address_size);
4750           putchar ('\n');
4751         }
4752     }
4753
4754   printf ("\n");
4755
4756   return 1;
4757 }
4758
4759 /* Comparison function for qsort.  */
4760 static int
4761 comp_addr_base (const void * v0, const void * v1)
4762 {
4763   debug_info * info0 = (debug_info *) v0;
4764   debug_info * info1 = (debug_info *) v1;
4765   return info0->addr_base - info1->addr_base;
4766 }
4767
4768 /* Display the debug_addr section.  */
4769 static int
4770 display_debug_addr (struct dwarf_section *section,
4771                     void *file)
4772 {
4773   debug_info **debug_addr_info;
4774   unsigned char *entry;
4775   unsigned char *end;
4776   unsigned int i;
4777   unsigned int count;
4778
4779   if (section->size == 0)
4780     {
4781       printf (_("\nThe %s section is empty.\n"), section->name);
4782       return 0;
4783     }
4784
4785   if (load_debug_info (file) == 0)
4786     {
4787       warn (_("Unable to load/parse the .debug_info section, so cannot interpret the %s section.\n"),
4788             section->name);
4789       return 0;
4790     }
4791
4792   printf (_("Contents of the %s section:\n\n"), section->name);
4793
4794   debug_addr_info = (debug_info **) xmalloc ((num_debug_info_entries + 1)
4795                                              * sizeof (debug_info *));
4796
4797   count = 0;
4798   for (i = 0; i < num_debug_info_entries; i++)
4799     {
4800       if (debug_information [i].addr_base != DEBUG_INFO_UNAVAILABLE)
4801         debug_addr_info [count++] = &debug_information [i];
4802     }
4803
4804   /* Add a sentinel to make iteration convenient.  */
4805   debug_addr_info [count] = (debug_info *) xmalloc (sizeof (debug_info));
4806   debug_addr_info [count]->addr_base = section->size;
4807
4808   qsort (debug_addr_info, count, sizeof (debug_info *), comp_addr_base);
4809   for (i = 0; i < count; i++)
4810     {
4811       unsigned int idx;
4812       unsigned int address_size = debug_addr_info [i]->pointer_size;
4813
4814       printf (_("  For compilation unit at offset 0x%s:\n"),
4815               dwarf_vmatoa ("x", debug_addr_info [i]->cu_offset));
4816
4817       printf (_("\tIndex\tAddress\n"));
4818       entry = section->start + debug_addr_info [i]->addr_base;
4819       end = section->start + debug_addr_info [i + 1]->addr_base;
4820       idx = 0;
4821       while (entry < end)
4822         {
4823           dwarf_vma base = byte_get (entry, address_size);
4824           printf (_("\t%d:\t"), idx);
4825           print_dwarf_vma (base, address_size);
4826           printf ("\n");
4827           entry += address_size;
4828           idx++;
4829         }
4830     }
4831   printf ("\n");
4832
4833   free (debug_addr_info);
4834   return 1;
4835 }
4836
4837 /* Display the .debug_str_offsets and .debug_str_offsets.dwo sections.  */
4838 static int
4839 display_debug_str_offsets (struct dwarf_section *section,
4840                            void *file ATTRIBUTE_UNUSED)
4841 {
4842   if (section->size == 0)
4843     {
4844       printf (_("\nThe %s section is empty.\n"), section->name);
4845       return 0;
4846     }
4847   /* TODO: Dump the contents.  This is made somewhat difficult by not knowing
4848      what the offset size is for this section.  */
4849   return 1;
4850 }
4851
4852 /* Each debug_information[x].range_lists[y] gets this representation for
4853    sorting purposes.  */
4854
4855 struct range_entry
4856 {
4857   /* The debug_information[x].range_lists[y] value.  */
4858   unsigned long ranges_offset;
4859
4860   /* Original debug_information to find parameters of the data.  */
4861   debug_info *debug_info_p;
4862 };
4863
4864 /* Sort struct range_entry in ascending order of its RANGES_OFFSET.  */
4865
4866 static int
4867 range_entry_compar (const void *ap, const void *bp)
4868 {
4869   const struct range_entry *a_re = (const struct range_entry *) ap;
4870   const struct range_entry *b_re = (const struct range_entry *) bp;
4871   const unsigned long a = a_re->ranges_offset;
4872   const unsigned long b = b_re->ranges_offset;
4873
4874   return (a > b) - (b > a);
4875 }
4876
4877 static int
4878 display_debug_ranges (struct dwarf_section *section,
4879                       void *file ATTRIBUTE_UNUSED)
4880 {
4881   unsigned char *start = section->start;
4882   unsigned char *last_start = start;
4883   unsigned long bytes = section->size;
4884   unsigned char *section_begin = start;
4885   unsigned char *finish = start + bytes;
4886   unsigned int num_range_list, i;
4887   struct range_entry *range_entries, *range_entry_fill;
4888
4889   if (bytes == 0)
4890     {
4891       printf (_("\nThe %s section is empty.\n"), section->name);
4892       return 0;
4893     }
4894
4895   if (load_debug_info (file) == 0)
4896     {
4897       warn (_("Unable to load/parse the .debug_info section, so cannot interpret the %s section.\n"),
4898             section->name);
4899       return 0;
4900     }
4901
4902   num_range_list = 0;
4903   for (i = 0; i < num_debug_info_entries; i++)
4904     num_range_list += debug_information [i].num_range_lists;
4905
4906   if (num_range_list == 0)
4907     {
4908       /* This can happen when the file was compiled with -gsplit-debug
4909          which removes references to range lists from the primary .o file.  */
4910       printf (_("No range lists in .debug_info section.\n"));
4911       return 1;
4912     }
4913
4914   range_entries = (struct range_entry *)
4915       xmalloc (sizeof (*range_entries) * num_range_list);
4916   range_entry_fill = range_entries;
4917
4918   for (i = 0; i < num_debug_info_entries; i++)
4919     {
4920       debug_info *debug_info_p = &debug_information[i];
4921       unsigned int j;
4922
4923       for (j = 0; j < debug_info_p->num_range_lists; j++)
4924         {
4925           range_entry_fill->ranges_offset = debug_info_p->range_lists[j];
4926           range_entry_fill->debug_info_p = debug_info_p;
4927           range_entry_fill++;
4928         }
4929     }
4930
4931   qsort (range_entries, num_range_list, sizeof (*range_entries),
4932          range_entry_compar);
4933
4934   if (dwarf_check != 0 && range_entries[0].ranges_offset != 0)
4935     warn (_("Range lists in %s section start at 0x%lx\n"),
4936           section->name, range_entries[0].ranges_offset);
4937
4938   printf (_("Contents of the %s section:\n\n"), section->name);
4939   printf (_("    Offset   Begin    End\n"));
4940
4941   for (i = 0; i < num_range_list; i++)
4942     {
4943       struct range_entry *range_entry = &range_entries[i];
4944       debug_info *debug_info_p = range_entry->debug_info_p;
4945       unsigned int pointer_size;
4946       unsigned long offset;
4947       unsigned char *next;
4948       unsigned long base_address;
4949
4950       pointer_size = debug_info_p->pointer_size;
4951       offset = range_entry->ranges_offset;
4952       next = section_begin + offset;
4953       base_address = debug_info_p->base_address;
4954
4955       /* PR 17512: file: 001-101485-0.001:0.1.  */
4956       if (pointer_size < 2 || pointer_size > 8)
4957         {
4958           warn (_("Corrupt pointer size (%d) in debug entry at offset %8.8lx\n"),
4959                 pointer_size, offset);
4960           continue;
4961         }
4962       
4963       if (dwarf_check != 0 && i > 0)
4964         {
4965           if (start < next)
4966             warn (_("There is a hole [0x%lx - 0x%lx] in %s section.\n"),
4967                   (unsigned long) (start - section_begin),
4968                   (unsigned long) (next - section_begin), section->name);
4969           else if (start > next)
4970             {
4971               if (next == last_start)
4972                 continue;
4973               warn (_("There is an overlap [0x%lx - 0x%lx] in %s section.\n"),
4974                     (unsigned long) (start - section_begin),
4975                     (unsigned long) (next - section_begin), section->name);
4976             }
4977         }
4978       start = next;
4979       last_start = next;
4980
4981       while (start < finish)
4982         {
4983           dwarf_vma begin;
4984           dwarf_vma end;
4985
4986           /* Note: we use sign extension here in order to be sure that
4987              we can detect the -1 escape value.  Sign extension into the
4988              top 32 bits of a 32-bit address will not affect the values
4989              that we display since we always show hex values, and always
4990              the bottom 32-bits.  */
4991           SAFE_BYTE_GET_AND_INC (begin, start, pointer_size, finish);
4992           if (start >= finish)
4993             break;
4994           SAFE_SIGNED_BYTE_GET_AND_INC (end, start, pointer_size, finish);
4995
4996           printf ("    %8.8lx ", offset);
4997
4998           if (begin == 0 && end == 0)
4999             {
5000               printf (_("<End of list>\n"));
5001               break;
5002             }
5003
5004           /* Check base address specifiers.  */
5005           if (begin == (dwarf_vma) -1 && end != (dwarf_vma) -1)
5006             {
5007               base_address = end;
5008               print_dwarf_vma (begin, pointer_size);
5009               print_dwarf_vma (end, pointer_size);
5010               printf ("(base address)\n");
5011               continue;
5012             }
5013
5014           print_dwarf_vma (begin + base_address, pointer_size);
5015           print_dwarf_vma (end + base_address, pointer_size);
5016
5017           if (begin == end)
5018             fputs (_("(start == end)"), stdout);
5019           else if (begin > end)
5020             fputs (_("(start > end)"), stdout);
5021
5022           putchar ('\n');
5023         }
5024     }
5025   putchar ('\n');
5026
5027   free (range_entries);
5028
5029   return 1;
5030 }
5031
5032 typedef struct Frame_Chunk
5033 {
5034   struct Frame_Chunk *next;
5035   unsigned char *chunk_start;
5036   int ncols;
5037   /* DW_CFA_{undefined,same_value,offset,register,unreferenced}  */
5038   short int *col_type;
5039   int *col_offset;
5040   char *augmentation;
5041   unsigned int code_factor;
5042   int data_factor;
5043   dwarf_vma pc_begin;
5044   dwarf_vma pc_range;
5045   int cfa_reg;
5046   int cfa_offset;
5047   int ra;
5048   unsigned char fde_encoding;
5049   unsigned char cfa_exp;
5050   unsigned char ptr_size;
5051   unsigned char segment_size;
5052 }
5053 Frame_Chunk;
5054
5055 static const char *const *dwarf_regnames;
5056 static unsigned int dwarf_regnames_count;
5057
5058 /* A marker for a col_type that means this column was never referenced
5059    in the frame info.  */
5060 #define DW_CFA_unreferenced (-1)
5061
5062 /* Return 0 if not more space is needed, 1 if more space is needed,
5063    -1 for invalid reg.  */
5064
5065 static int
5066 frame_need_space (Frame_Chunk *fc, unsigned int reg)
5067 {
5068   int prev = fc->ncols;
5069
5070   if (reg < (unsigned int) fc->ncols)
5071     return 0;
5072
5073   if (dwarf_regnames_count
5074       && reg > dwarf_regnames_count)
5075     return -1;
5076
5077   fc->ncols = reg + 1;
5078   fc->col_type = (short int *) xcrealloc (fc->col_type, fc->ncols,
5079                                           sizeof (short int));
5080   fc->col_offset = (int *) xcrealloc (fc->col_offset, fc->ncols, sizeof (int));
5081   /* PR 17512: file:002-10025-0.005.  */ 
5082   if (fc->col_type == NULL || fc->col_offset == NULL)
5083     {
5084       error (_("Out of memory allocating %u columns in dwarf frame arrays\n"),
5085              fc->ncols);
5086       fc->ncols = 0;
5087       return -1;
5088     }
5089
5090   while (prev < fc->ncols)
5091     {
5092       fc->col_type[prev] = DW_CFA_unreferenced;
5093       fc->col_offset[prev] = 0;
5094       prev++;
5095     }
5096   return 1;
5097 }
5098
5099 static const char *const dwarf_regnames_i386[] =
5100 {
5101   "eax", "ecx", "edx", "ebx",                     /* 0 - 3  */
5102   "esp", "ebp", "esi", "edi",                     /* 4 - 7  */
5103   "eip", "eflags", NULL,                          /* 8 - 10  */
5104   "st0", "st1", "st2", "st3",                     /* 11 - 14  */
5105   "st4", "st5", "st6", "st7",                     /* 15 - 18  */
5106   NULL, NULL,                                     /* 19 - 20  */
5107   "xmm0", "xmm1", "xmm2", "xmm3",                 /* 21 - 24  */
5108   "xmm4", "xmm5", "xmm6", "xmm7",                 /* 25 - 28  */
5109   "mm0", "mm1", "mm2", "mm3",                     /* 29 - 32  */
5110   "mm4", "mm5", "mm6", "mm7",                     /* 33 - 36  */
5111   "fcw", "fsw", "mxcsr",                          /* 37 - 39  */
5112   "es", "cs", "ss", "ds", "fs", "gs", NULL, NULL, /* 40 - 47  */
5113   "tr", "ldtr",                                   /* 48 - 49  */
5114   NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 50 - 57  */
5115   NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 58 - 65  */
5116   NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 66 - 73  */
5117   NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 74 - 81  */
5118   NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 82 - 89  */
5119   NULL, NULL, NULL,                               /* 90 - 92  */
5120   "k0", "k1", "k2", "k3", "k4", "k5", "k6", "k7"  /* 93 - 100  */
5121 };
5122
5123 void
5124 init_dwarf_regnames_i386 (void)
5125 {
5126   dwarf_regnames = dwarf_regnames_i386;
5127   dwarf_regnames_count = ARRAY_SIZE (dwarf_regnames_i386);
5128 }
5129
5130 static const char *const dwarf_regnames_x86_64[] =
5131 {
5132   "rax", "rdx", "rcx", "rbx",
5133   "rsi", "rdi", "rbp", "rsp",
5134   "r8",  "r9",  "r10", "r11",
5135   "r12", "r13", "r14", "r15",
5136   "rip",
5137   "xmm0",  "xmm1",  "xmm2",  "xmm3",
5138   "xmm4",  "xmm5",  "xmm6",  "xmm7",
5139   "xmm8",  "xmm9",  "xmm10", "xmm11",
5140   "xmm12", "xmm13", "xmm14", "xmm15",
5141   "st0", "st1", "st2", "st3",
5142   "st4", "st5", "st6", "st7",
5143   "mm0", "mm1", "mm2", "mm3",
5144   "mm4", "mm5", "mm6", "mm7",
5145   "rflags",
5146   "es", "cs", "ss", "ds", "fs", "gs", NULL, NULL,
5147   "fs.base", "gs.base", NULL, NULL,
5148   "tr", "ldtr",
5149   "mxcsr", "fcw", "fsw",
5150   "xmm16",  "xmm17",  "xmm18",  "xmm19",
5151   "xmm20",  "xmm21",  "xmm22",  "xmm23",
5152   "xmm24",  "xmm25",  "xmm26",  "xmm27",
5153   "xmm28",  "xmm29",  "xmm30",  "xmm31",
5154   NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 83 - 90  */
5155   NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 91 - 98  */
5156   NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 99 - 106  */
5157   NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 107 - 114  */
5158   NULL, NULL, NULL,                               /* 115 - 117  */
5159   "k0", "k1", "k2", "k3", "k4", "k5", "k6", "k7"
5160 };
5161
5162 void
5163 init_dwarf_regnames_x86_64 (void)
5164 {
5165   dwarf_regnames = dwarf_regnames_x86_64;
5166   dwarf_regnames_count = ARRAY_SIZE (dwarf_regnames_x86_64);
5167 }
5168
5169 static const char *const dwarf_regnames_aarch64[] =
5170 {
5171    "x0",  "x1",  "x2",  "x3",  "x4",  "x5",  "x6",  "x7", 
5172    "x8",  "x9", "x10", "x11", "x12", "x13", "x14", "x15", 
5173   "x16", "x17", "x18", "x19", "x20", "x21", "x22", "x23",
5174   "x24", "x25", "x26", "x27", "x28", "x29", "x30", "sp",
5175    NULL, "elr",  NULL,  NULL,  NULL,  NULL,  NULL,  NULL,
5176    NULL,  NULL,  NULL,  NULL,  NULL,  NULL,  NULL,  NULL,
5177    NULL,  NULL,  NULL,  NULL,  NULL,  NULL,  NULL,  NULL,
5178    NULL,  NULL,  NULL,  NULL,  NULL,  NULL,  NULL,  NULL,
5179    "v0",  "v1",  "v2",  "v3",  "v4",  "v5",  "v6",  "v7", 
5180    "v8",  "v9", "v10", "v11", "v12", "v13", "v14", "v15", 
5181   "v16", "v17", "v18", "v19", "v20", "v21", "v22", "v23",
5182   "v24", "v25", "v26", "v27", "v28", "v29", "v30", "v31",
5183 };
5184
5185 void
5186 init_dwarf_regnames_aarch64 (void)
5187 {
5188   dwarf_regnames = dwarf_regnames_aarch64;
5189   dwarf_regnames_count = ARRAY_SIZE (dwarf_regnames_aarch64);
5190 }
5191
5192 void
5193 init_dwarf_regnames (unsigned int e_machine)
5194 {
5195   switch (e_machine)
5196     {
5197     case EM_386:
5198     case EM_486:
5199       init_dwarf_regnames_i386 ();
5200       break;
5201
5202     case EM_X86_64:
5203     case EM_L1OM:
5204     case EM_K1OM:
5205       init_dwarf_regnames_x86_64 ();
5206       break;
5207
5208     case EM_AARCH64:
5209       init_dwarf_regnames_aarch64 ();
5210       break;
5211
5212     default:
5213       break;
5214     }
5215 }
5216
5217 static const char *
5218 regname (unsigned int regno, int row)
5219 {
5220   static char reg[64];
5221   if (dwarf_regnames
5222       && regno < dwarf_regnames_count
5223       && dwarf_regnames [regno] != NULL)
5224     {
5225       if (row)
5226         return dwarf_regnames [regno];
5227       snprintf (reg, sizeof (reg), "r%d (%s)", regno,
5228                 dwarf_regnames [regno]);
5229     }
5230   else
5231     snprintf (reg, sizeof (reg), "r%d", regno);
5232   return reg;
5233 }
5234
5235 static void
5236 frame_display_row (Frame_Chunk *fc, int *need_col_headers, int *max_regs)
5237 {
5238   int r;
5239   char tmp[100];
5240
5241   if (*max_regs < fc->ncols)
5242     *max_regs = fc->ncols;
5243
5244   if (*need_col_headers)
5245     {
5246       static const char *sloc = "   LOC";
5247
5248       *need_col_headers = 0;
5249
5250       printf ("%-*s CFA      ", eh_addr_size * 2, sloc);
5251
5252       for (r = 0; r < *max_regs; r++)
5253         if (fc->col_type[r] != DW_CFA_unreferenced)
5254           {
5255             if (r == fc->ra)
5256               printf ("ra      ");
5257             else
5258               printf ("%-5s ", regname (r, 1));
5259           }
5260
5261       printf ("\n");
5262     }
5263
5264   print_dwarf_vma (fc->pc_begin, eh_addr_size);
5265   if (fc->cfa_exp)
5266     strcpy (tmp, "exp");
5267   else
5268     sprintf (tmp, "%s%+d", regname (fc->cfa_reg, 1), fc->cfa_offset);
5269   printf ("%-8s ", tmp);
5270
5271   for (r = 0; r < fc->ncols; r++)
5272     {
5273       if (fc->col_type[r] != DW_CFA_unreferenced)
5274         {
5275           switch (fc->col_type[r])
5276             {
5277             case DW_CFA_undefined:
5278               strcpy (tmp, "u");
5279               break;
5280             case DW_CFA_same_value:
5281               strcpy (tmp, "s");
5282               break;
5283             case DW_CFA_offset:
5284               sprintf (tmp, "c%+d", fc->col_offset[r]);
5285               break;
5286             case DW_CFA_val_offset:
5287               sprintf (tmp, "v%+d", fc->col_offset[r]);
5288               break;
5289             case DW_CFA_register:
5290               sprintf (tmp, "%s", regname (fc->col_offset[r], 0));
5291               break;
5292             case DW_CFA_expression:
5293               strcpy (tmp, "exp");
5294               break;
5295             case DW_CFA_val_expression:
5296               strcpy (tmp, "vexp");
5297               break;
5298             default:
5299               strcpy (tmp, "n/a");
5300               break;
5301             }
5302           printf ("%-5s ", tmp);
5303         }
5304     }
5305   printf ("\n");
5306 }
5307
5308 #define GET(VAR, N)     SAFE_BYTE_GET_AND_INC (VAR, start, N, end)
5309 #define LEB()   read_uleb128 (start, & length_return, end); start += length_return
5310 #define SLEB()  read_sleb128 (start, & length_return, end); start += length_return
5311
5312 static unsigned char *
5313 read_cie (unsigned char *start, unsigned char *end,
5314           Frame_Chunk **p_cie, int *p_version,
5315           unsigned long *p_aug_len, unsigned char **p_aug)
5316 {
5317   int version;
5318   Frame_Chunk *fc;
5319   unsigned int length_return;
5320   unsigned char *augmentation_data = NULL;
5321   unsigned long augmentation_data_len = 0;
5322
5323   * p_cie = NULL;
5324   /* PR 17512: file: 001-228113-0.004.  */
5325   if (start >= end)
5326     return end;
5327
5328   fc = (Frame_Chunk *) xmalloc (sizeof (Frame_Chunk));
5329   memset (fc, 0, sizeof (Frame_Chunk));
5330
5331   fc->col_type = (short int *) xmalloc (sizeof (short int));
5332   fc->col_offset = (int *) xmalloc (sizeof (int));
5333
5334   version = *start++;
5335
5336   fc->augmentation = (char *) start;
5337   /* PR 17512: file: 001-228113-0.004.
5338      Skip past augmentation name, but avoid running off the end of the data.  */
5339   while (start < end)
5340     if (* start ++ == '\0')
5341       break;
5342   if (start == end)
5343     {
5344       warn (_("No terminator for augmentation name\n"));
5345       return start;
5346     }
5347
5348   if (strcmp (fc->augmentation, "eh") == 0)
5349     start += eh_addr_size;
5350
5351   if (version >= 4)
5352     {
5353       GET (fc->ptr_size, 1);
5354       GET (fc->segment_size, 1);
5355       eh_addr_size = fc->ptr_size;
5356     }
5357   else
5358     {
5359       fc->ptr_size = eh_addr_size;
5360       fc->segment_size = 0;
5361     }
5362   fc->code_factor = LEB ();
5363   fc->data_factor = SLEB ();
5364   if (version == 1)
5365     {
5366       GET (fc->ra, 1);
5367     }
5368   else
5369     {
5370       fc->ra = LEB ();
5371     }
5372
5373   if (fc->augmentation[0] == 'z')
5374     {
5375       augmentation_data_len = LEB ();
5376       augmentation_data = start;
5377       start += augmentation_data_len;
5378     }
5379
5380   if (augmentation_data_len)
5381     {
5382       unsigned char *p, *q;
5383       p = (unsigned char *) fc->augmentation + 1;
5384       q = augmentation_data;
5385
5386       while (1)
5387         {
5388           if (*p == 'L')
5389             q++;
5390           else if (*p == 'P')
5391             q += 1 + size_of_encoded_value (*q);
5392           else if (*p == 'R')
5393             fc->fde_encoding = *q++;
5394           else if (*p == 'S')
5395             ;
5396           else
5397             break;
5398           p++;
5399         }
5400     }
5401
5402   *p_cie = fc;
5403   if (p_version)
5404     *p_version = version;
5405   if (p_aug_len)
5406     {
5407       *p_aug_len = augmentation_data_len;
5408       *p_aug = augmentation_data;
5409     }
5410   return start;
5411 }
5412
5413 static int
5414 display_debug_frames (struct dwarf_section *section,
5415                       void *file ATTRIBUTE_UNUSED)
5416 {
5417   unsigned char *start = section->start;
5418   unsigned char *end = start + section->size;
5419   unsigned char *section_start = start;
5420   Frame_Chunk *chunks = 0, *forward_refs = 0;
5421   Frame_Chunk *remembered_state = 0;
5422   Frame_Chunk *rs;
5423   int is_eh = strcmp (section->name, ".eh_frame") == 0;
5424   unsigned int length_return;
5425   int max_regs = 0;
5426   const char *bad_reg = _("bad register: ");
5427   int saved_eh_addr_size = eh_addr_size;
5428
5429   printf (_("Contents of the %s section:\n"), section->name);
5430
5431   while (start < end)
5432     {
5433       unsigned char *saved_start;
5434       unsigned char *block_end;
5435       dwarf_vma length;
5436       dwarf_vma cie_id;
5437       Frame_Chunk *fc;
5438       Frame_Chunk *cie;
5439       int need_col_headers = 1;
5440       unsigned char *augmentation_data = NULL;
5441       unsigned long augmentation_data_len = 0;
5442       unsigned int encoded_ptr_size = saved_eh_addr_size;
5443       unsigned int offset_size;
5444       unsigned int initial_length_size;
5445
5446       saved_start = start;
5447
5448       SAFE_BYTE_GET_AND_INC (length, start, 4, end);
5449
5450       if (length == 0)
5451         {
5452           printf ("\n%08lx ZERO terminator\n\n",
5453                     (unsigned long)(saved_start - section_start));
5454           continue;
5455         }
5456
5457       if (length == 0xffffffff)
5458         {
5459           SAFE_BYTE_GET_AND_INC (length, start, 8, end);
5460           offset_size = 8;
5461           initial_length_size = 12;
5462         }
5463       else
5464         {
5465           offset_size = 4;
5466           initial_length_size = 4;
5467         }
5468
5469       block_end = saved_start + length + initial_length_size;
5470       if (block_end > end || block_end < start)
5471         {
5472           warn ("Invalid length 0x%s in FDE at %#08lx\n",
5473                 dwarf_vmatoa_1 (NULL, length, offset_size),
5474                 (unsigned long) (saved_start - section_start));
5475           block_end = end;
5476         }
5477
5478       SAFE_BYTE_GET_AND_INC (cie_id, start, offset_size, end);
5479
5480       if (is_eh ? (cie_id == 0) : ((offset_size == 4 && cie_id == DW_CIE_ID)
5481                                    || (offset_size == 8 && cie_id == DW64_CIE_ID)))
5482         {
5483           int version;
5484           int mreg;
5485
5486           start = read_cie (start, end, &cie, &version,
5487                             &augmentation_data_len, &augmentation_data);
5488           /* PR 17512: file: 027-135133-0.005.  */
5489           if (cie == NULL)
5490             break;
5491           fc = cie;
5492           fc->next = chunks;
5493           chunks = fc;
5494           fc->chunk_start = saved_start;
5495           mreg = max_regs - 1;
5496           if (mreg < fc->ra)
5497             mreg = fc->ra;
5498           frame_need_space (fc, mreg);
5499           if (fc->fde_encoding)
5500             encoded_ptr_size = size_of_encoded_value (fc->fde_encoding);
5501
5502           printf ("\n%08lx ", (unsigned long) (saved_start - section_start));
5503           print_dwarf_vma (length, fc->ptr_size);
5504           print_dwarf_vma (cie_id, offset_size);
5505
5506           if (do_debug_frames_interp)
5507             {
5508               printf ("CIE \"%s\" cf=%d df=%d ra=%d\n", fc->augmentation,
5509                       fc->code_factor, fc->data_factor, fc->ra);
5510             }
5511           else
5512             {
5513               printf ("CIE\n");
5514               printf ("  Version:               %d\n", version);
5515               printf ("  Augmentation:          \"%s\"\n", fc->augmentation);
5516               if (version >= 4)
5517                 {
5518                   printf ("  Pointer Size:          %u\n", fc->ptr_size);
5519                   printf ("  Segment Size:          %u\n", fc->segment_size);
5520                 }
5521               printf ("  Code alignment factor: %u\n", fc->code_factor);
5522               printf ("  Data alignment factor: %d\n", fc->data_factor);
5523               printf ("  Return address column: %d\n", fc->ra);
5524
5525               if (augmentation_data_len)
5526                 {
5527                   unsigned long i;
5528                   printf ("  Augmentation data:    ");
5529                   for (i = 0; i < augmentation_data_len; ++i)
5530                     printf (" %02x", augmentation_data[i]);
5531                   putchar ('\n');
5532                 }
5533               putchar ('\n');
5534             }
5535         }
5536       else
5537         {
5538           unsigned char *look_for;
5539           static Frame_Chunk fde_fc;
5540           unsigned long segment_selector;
5541
5542           if (is_eh)
5543             {
5544               dwarf_vma sign = (dwarf_vma) 1 << (offset_size * 8 - 1);
5545               look_for = start - 4 - ((cie_id ^ sign) - sign);
5546             }
5547           else
5548             look_for = section_start + cie_id;
5549
5550           if (look_for <= saved_start)
5551             {
5552               for (cie = chunks; cie ; cie = cie->next)
5553                 if (cie->chunk_start == look_for)
5554                   break;
5555             }
5556           else
5557             {
5558               for (cie = forward_refs; cie ; cie = cie->next)
5559                 if (cie->chunk_start == look_for)
5560                   break;
5561               if (!cie)
5562                 {
5563                   unsigned int off_size;
5564                   unsigned char *cie_scan;
5565
5566                   cie_scan = look_for;
5567                   off_size = 4;
5568                   SAFE_BYTE_GET_AND_INC (length, cie_scan, 4, end);
5569                   if (length == 0xffffffff)
5570                     {
5571                       SAFE_BYTE_GET_AND_INC (length, cie_scan, 8, end);
5572                       off_size = 8;
5573                     }
5574                   if (length != 0)
5575                     {
5576                       dwarf_vma c_id;
5577
5578                       SAFE_BYTE_GET_AND_INC (c_id, cie_scan, off_size, end);
5579                       if (is_eh
5580                           ? c_id == 0
5581                           : ((off_size == 4 && c_id == DW_CIE_ID)
5582                              || (off_size == 8 && c_id == DW64_CIE_ID)))
5583                         {
5584                           int version;
5585                           int mreg;
5586
5587                           read_cie (cie_scan, end, &cie, &version,
5588                                     &augmentation_data_len, &augmentation_data);
5589                           cie->next = forward_refs;
5590                           forward_refs = cie;
5591                           cie->chunk_start = look_for;
5592                           mreg = max_regs - 1;
5593                           if (mreg < cie->ra)
5594                             mreg = cie->ra;
5595                           frame_need_space (cie, mreg);
5596                           if (cie->fde_encoding)
5597                             encoded_ptr_size
5598                               = size_of_encoded_value (cie->fde_encoding);
5599                         }
5600                     }
5601                 }
5602             }
5603
5604           fc = &fde_fc;
5605           memset (fc, 0, sizeof (Frame_Chunk));
5606
5607           if (!cie)
5608             {
5609               warn ("Invalid CIE pointer 0x%s in FDE at %#08lx\n",
5610                     dwarf_vmatoa_1 (NULL, cie_id, offset_size),
5611                     (unsigned long) (saved_start - section_start));
5612               fc->ncols = 0;
5613               fc->col_type = (short int *) xmalloc (sizeof (short int));
5614               fc->col_offset = (int *) xmalloc (sizeof (int));
5615               frame_need_space (fc, max_regs - 1);
5616               cie = fc;
5617               fc->augmentation = "";
5618               fc->fde_encoding = 0;
5619               fc->ptr_size = eh_addr_size;
5620               fc->segment_size = 0;
5621             }
5622           else
5623             {
5624               fc->ncols = cie->ncols;
5625               fc->col_type = (short int *) xcmalloc (fc->ncols, sizeof (short int));
5626               fc->col_offset =  (int *) xcmalloc (fc->ncols, sizeof (int));
5627               memcpy (fc->col_type, cie->col_type, fc->ncols * sizeof (short int));
5628               memcpy (fc->col_offset, cie->col_offset, fc->ncols * sizeof (int));
5629               fc->augmentation = cie->augmentation;
5630               fc->ptr_size = cie->ptr_size;
5631               eh_addr_size = cie->ptr_size;
5632               fc->segment_size = cie->segment_size;
5633               fc->code_factor = cie->code_factor;
5634               fc->data_factor = cie->data_factor;
5635               fc->cfa_reg = cie->cfa_reg;
5636               fc->cfa_offset = cie->cfa_offset;
5637               fc->ra = cie->ra;
5638               frame_need_space (fc, max_regs - 1);
5639               fc->fde_encoding = cie->fde_encoding;
5640             }
5641
5642           if (fc->fde_encoding)
5643             encoded_ptr_size = size_of_encoded_value (fc->fde_encoding);
5644
5645           segment_selector = 0;
5646           if (fc->segment_size)
5647             SAFE_BYTE_GET_AND_INC (segment_selector, start, fc->segment_size, end);
5648
5649           fc->pc_begin = get_encoded_value (&start, fc->fde_encoding, section, end);
5650
5651           /* FIXME: It appears that sometimes the final pc_range value is
5652              encoded in less than encoded_ptr_size bytes.  See the x86_64
5653              run of the "objcopy on compressed debug sections" test for an
5654              example of this.  */
5655           SAFE_BYTE_GET_AND_INC (fc->pc_range, start, encoded_ptr_size, end);
5656
5657           if (cie->augmentation[0] == 'z')
5658             {
5659               augmentation_data_len = LEB ();
5660               augmentation_data = start;
5661               start += augmentation_data_len;
5662             }
5663
5664           printf ("\n%08lx %s %s FDE cie=%08lx pc=",
5665                   (unsigned long)(saved_start - section_start),
5666                   dwarf_vmatoa_1 (NULL, length, fc->ptr_size),
5667                   dwarf_vmatoa_1 (NULL, cie_id, offset_size),
5668                   (unsigned long)(cie->chunk_start - section_start));
5669
5670           if (fc->segment_size)
5671             printf ("%04lx:", segment_selector);
5672
5673           printf ("%s..%s\n",
5674                   dwarf_vmatoa_1 (NULL, fc->pc_begin, fc->ptr_size),
5675                   dwarf_vmatoa_1 (NULL, fc->pc_begin + fc->pc_range, fc->ptr_size));
5676
5677           if (! do_debug_frames_interp && augmentation_data_len)
5678             {
5679               unsigned long i;
5680
5681               printf ("  Augmentation data:    ");
5682               for (i = 0; i < augmentation_data_len; ++i)
5683                 printf (" %02x", augmentation_data[i]);
5684               putchar ('\n');
5685               putchar ('\n');
5686             }
5687         }
5688
5689       /* At this point, fc is the current chunk, cie (if any) is set, and
5690          we're about to interpret instructions for the chunk.  */
5691       /* ??? At present we need to do this always, since this sizes the
5692          fc->col_type and fc->col_offset arrays, which we write into always.
5693          We should probably split the interpreted and non-interpreted bits
5694          into two different routines, since there's so much that doesn't
5695          really overlap between them.  */
5696       if (1 || do_debug_frames_interp)
5697         {
5698           /* Start by making a pass over the chunk, allocating storage
5699              and taking note of what registers are used.  */
5700           unsigned char *tmp = start;
5701
5702           while (start < block_end)
5703             {
5704               unsigned int reg, op, opa;
5705               unsigned long temp;
5706
5707               op = *start++;
5708               opa = op & 0x3f;
5709               if (op & 0xc0)
5710                 op &= 0xc0;
5711
5712               /* Warning: if you add any more cases to this switch, be
5713                  sure to add them to the corresponding switch below.  */
5714               switch (op)
5715                 {
5716                 case DW_CFA_advance_loc:
5717                   break;
5718                 case DW_CFA_offset:
5719                   LEB ();
5720                   if (frame_need_space (fc, opa) >= 0)
5721                     fc->col_type[opa] = DW_CFA_undefined;
5722                   break;
5723                 case DW_CFA_restore:
5724                   if (frame_need_space (fc, opa) >= 0)
5725                     fc->col_type[opa] = DW_CFA_undefined;
5726                   break;
5727                 case DW_CFA_set_loc:
5728                   start += encoded_ptr_size;
5729                   break;
5730                 case DW_CFA_advance_loc1:
5731                   start += 1;
5732                   break;
5733                 case DW_CFA_advance_loc2:
5734                   start += 2;
5735                   break;
5736                 case DW_CFA_advance_loc4:
5737                   start += 4;
5738                   break;
5739                 case DW_CFA_offset_extended:
5740                 case DW_CFA_val_offset:
5741                   reg = LEB (); LEB ();
5742                   if (frame_need_space (fc, reg) >= 0)
5743                     fc->col_type[reg] = DW_CFA_undefined;
5744                   break;
5745                 case DW_CFA_restore_extended:
5746                   reg = LEB ();
5747                   frame_need_space (fc, reg);
5748                   if (frame_need_space (fc, reg) >= 0)
5749                     fc->col_type[reg] = DW_CFA_undefined;
5750                   break;
5751                 case DW_CFA_undefined:
5752                   reg = LEB ();
5753                   if (frame_need_space (fc, reg) >= 0)
5754                     fc->col_type[reg] = DW_CFA_undefined;
5755                   break;
5756                 case DW_CFA_same_value:
5757                   reg = LEB ();
5758                   if (frame_need_space (fc, reg) >= 0)
5759                     fc->col_type[reg] = DW_CFA_undefined;
5760                   break;
5761                 case DW_CFA_register:
5762                   reg = LEB (); LEB ();
5763                   if (frame_need_space (fc, reg) >= 0)
5764                     fc->col_type[reg] = DW_CFA_undefined;
5765                   break;
5766                 case DW_CFA_def_cfa:
5767                   LEB (); LEB ();
5768                   break;
5769                 case DW_CFA_def_cfa_register:
5770                   LEB ();
5771                   break;
5772                 case DW_CFA_def_cfa_offset:
5773                   LEB ();
5774                   break;
5775                 case DW_CFA_def_cfa_expression:
5776                   temp = LEB ();
5777                   if (start + temp < start)
5778                     {
5779                       warn (_("Corrupt CFA_def expression value: %lu\n"), temp);
5780                       start = block_end;
5781                     }
5782                   else
5783                     start += temp;
5784                   break;
5785                 case DW_CFA_expression:
5786                 case DW_CFA_val_expression:
5787                   reg = LEB ();
5788                   temp = LEB ();
5789                   if (start + temp < start)
5790                     {
5791                       /* PR 17512: file:306-192417-0.005.  */ 
5792                       warn (_("Corrupt CFA expression value: %lu\n"), temp);
5793                       start = block_end;
5794                     }
5795                   else
5796                     start += temp;
5797                   if (frame_need_space (fc, reg) >= 0)
5798                     fc->col_type[reg] = DW_CFA_undefined;
5799                   break;
5800                 case DW_CFA_offset_extended_sf:
5801                 case DW_CFA_val_offset_sf:
5802                   reg = LEB (); SLEB ();
5803                   if (frame_need_space (fc, reg) >= 0)
5804                     fc->col_type[reg] = DW_CFA_undefined;
5805                   break;
5806                 case DW_CFA_def_cfa_sf:
5807                   LEB (); SLEB ();
5808                   break;
5809                 case DW_CFA_def_cfa_offset_sf:
5810                   SLEB ();
5811                   break;
5812                 case DW_CFA_MIPS_advance_loc8:
5813                   start += 8;
5814                   break;
5815                 case DW_CFA_GNU_args_size:
5816                   LEB ();
5817                   break;
5818                 case DW_CFA_GNU_negative_offset_extended:
5819                   reg = LEB (); LEB ();
5820                   if (frame_need_space (fc, reg) >= 0)
5821                     fc->col_type[reg] = DW_CFA_undefined;
5822                   break;
5823                 default:
5824                   break;
5825                 }
5826             }
5827           start = tmp;
5828         }
5829
5830       /* Now we know what registers are used, make a second pass over
5831          the chunk, this time actually printing out the info.  */
5832
5833       while (start < block_end)
5834         {
5835           unsigned op, opa;
5836           unsigned long ul, reg, roffs;
5837           long l;
5838           dwarf_vma ofs;
5839           dwarf_vma vma;
5840           const char *reg_prefix = "";
5841
5842           op = *start++;
5843           opa = op & 0x3f;
5844           if (op & 0xc0)
5845             op &= 0xc0;
5846
5847           /* Warning: if you add any more cases to this switch, be
5848              sure to add them to the corresponding switch above.  */
5849           switch (op)
5850             {
5851             case DW_CFA_advance_loc:
5852               if (do_debug_frames_interp)
5853                 frame_display_row (fc, &need_col_headers, &max_regs);
5854               else
5855                 printf ("  DW_CFA_advance_loc: %d to %s\n",
5856                         opa * fc->code_factor,
5857                         dwarf_vmatoa_1 (NULL, 
5858                                         fc->pc_begin + opa * fc->code_factor,
5859                                         fc->ptr_size));
5860               fc->pc_begin += opa * fc->code_factor;
5861               break;
5862
5863             case DW_CFA_offset:
5864               roffs = LEB ();
5865               if (opa >= (unsigned int) fc->ncols)
5866                 reg_prefix = bad_reg;
5867               if (! do_debug_frames_interp || *reg_prefix != '\0')
5868                 printf ("  DW_CFA_offset: %s%s at cfa%+ld\n",
5869                         reg_prefix, regname (opa, 0),
5870                         roffs * fc->data_factor);
5871               if (*reg_prefix == '\0')
5872                 {
5873                   fc->col_type[opa] = DW_CFA_offset;
5874                   fc->col_offset[opa] = roffs * fc->data_factor;
5875                 }
5876               break;
5877
5878             case DW_CFA_restore:
5879               if (opa >= (unsigned int) cie->ncols
5880                   || opa >= (unsigned int) fc->ncols)
5881                 reg_prefix = bad_reg;
5882               if (! do_debug_frames_interp || *reg_prefix != '\0')
5883                 printf ("  DW_CFA_restore: %s%s\n",
5884                         reg_prefix, regname (opa, 0));
5885               if (*reg_prefix == '\0')
5886                 {
5887                   fc->col_type[opa] = cie->col_type[opa];
5888                   fc->col_offset[opa] = cie->col_offset[opa];
5889                   if (do_debug_frames_interp
5890                       && fc->col_type[opa] == DW_CFA_unreferenced)
5891                     fc->col_type[opa] = DW_CFA_undefined;
5892                 }
5893               break;
5894
5895             case DW_CFA_set_loc:
5896               vma = get_encoded_value (&start, fc->fde_encoding, section, end);
5897               if (do_debug_frames_interp)
5898                 frame_display_row (fc, &need_col_headers, &max_regs);
5899               else
5900                 printf ("  DW_CFA_set_loc: %s\n",
5901                         dwarf_vmatoa_1 (NULL, vma, fc->ptr_size));
5902               fc->pc_begin = vma;
5903               break;
5904
5905             case DW_CFA_advance_loc1:
5906               SAFE_BYTE_GET_AND_INC (ofs, start, 1, end);
5907               if (do_debug_frames_interp)
5908                 frame_display_row (fc, &need_col_headers, &max_regs);
5909               else
5910                 printf ("  DW_CFA_advance_loc1: %ld to %s\n",
5911                         (unsigned long) (ofs * fc->code_factor),
5912                         dwarf_vmatoa_1 (NULL,
5913                                         fc->pc_begin + ofs * fc->code_factor,
5914                                         fc->ptr_size));
5915               fc->pc_begin += ofs * fc->code_factor;
5916               break;
5917
5918             case DW_CFA_advance_loc2:
5919               SAFE_BYTE_GET_AND_INC (ofs, start, 2, end);
5920               if (do_debug_frames_interp)
5921                 frame_display_row (fc, &need_col_headers, &max_regs);
5922               else
5923                 printf ("  DW_CFA_advance_loc2: %ld to %s\n",
5924                         (unsigned long) (ofs * fc->code_factor),
5925                         dwarf_vmatoa_1 (NULL,
5926                                         fc->pc_begin + ofs * fc->code_factor,
5927                                         fc->ptr_size));
5928               fc->pc_begin += ofs * fc->code_factor;
5929               break;
5930
5931             case DW_CFA_advance_loc4:
5932               SAFE_BYTE_GET_AND_INC (ofs, start, 4, end);
5933               if (do_debug_frames_interp)
5934                 frame_display_row (fc, &need_col_headers, &max_regs);
5935               else
5936                 printf ("  DW_CFA_advance_loc4: %ld to %s\n",
5937                         (unsigned long) (ofs * fc->code_factor),
5938                         dwarf_vmatoa_1 (NULL,
5939                                         fc->pc_begin + ofs * fc->code_factor,
5940                                         fc->ptr_size));
5941               fc->pc_begin += ofs * fc->code_factor;
5942               break;
5943
5944             case DW_CFA_offset_extended:
5945               reg = LEB ();
5946               roffs = LEB ();
5947               if (reg >= (unsigned int) fc->ncols)
5948                 reg_prefix = bad_reg;
5949               if (! do_debug_frames_interp || *reg_prefix != '\0')
5950                 printf ("  DW_CFA_offset_extended: %s%s at cfa%+ld\n",
5951                         reg_prefix, regname (reg, 0),
5952                         roffs * fc->data_factor);
5953               if (*reg_prefix == '\0')
5954                 {
5955                   fc->col_type[reg] = DW_CFA_offset;
5956                   fc->col_offset[reg] = roffs * fc->data_factor;
5957                 }
5958               break;
5959
5960             case DW_CFA_val_offset:
5961               reg = LEB ();
5962               roffs = LEB ();
5963               if (reg >= (unsigned int) fc->ncols)
5964                 reg_prefix = bad_reg;
5965               if (! do_debug_frames_interp || *reg_prefix != '\0')
5966                 printf ("  DW_CFA_val_offset: %s%s at cfa%+ld\n",
5967                         reg_prefix, regname (reg, 0),
5968                         roffs * fc->data_factor);
5969               if (*reg_prefix == '\0')
5970                 {
5971                   fc->col_type[reg] = DW_CFA_val_offset;
5972                   fc->col_offset[reg] = roffs * fc->data_factor;
5973                 }
5974               break;
5975
5976             case DW_CFA_restore_extended:
5977               reg = LEB ();
5978               if (reg >= (unsigned int) cie->ncols
5979                   || reg >= (unsigned int) fc->ncols)
5980                 reg_prefix = bad_reg;
5981               if (! do_debug_frames_interp || *reg_prefix != '\0')
5982                 printf ("  DW_CFA_restore_extended: %s%s\n",
5983                         reg_prefix, regname (reg, 0));
5984               if (*reg_prefix == '\0')
5985                 {
5986                   fc->col_type[reg] = cie->col_type[reg];
5987                   fc->col_offset[reg] = cie->col_offset[reg];
5988                 }
5989               break;
5990
5991             case DW_CFA_undefined:
5992               reg = LEB ();
5993               if (reg >= (unsigned int) fc->ncols)
5994                 reg_prefix = bad_reg;
5995               if (! do_debug_frames_interp || *reg_prefix != '\0')
5996                 printf ("  DW_CFA_undefined: %s%s\n",
5997                         reg_prefix, regname (reg, 0));
5998               if (*reg_prefix == '\0')
5999                 {
6000                   fc->col_type[reg] = DW_CFA_undefined;
6001                   fc->col_offset[reg] = 0;
6002                 }
6003               break;
6004
6005             case DW_CFA_same_value:
6006               reg = LEB ();
6007               if (reg >= (unsigned int) fc->ncols)
6008                 reg_prefix = bad_reg;
6009               if (! do_debug_frames_interp || *reg_prefix != '\0')
6010                 printf ("  DW_CFA_same_value: %s%s\n",
6011                         reg_prefix, regname (reg, 0));
6012               if (*reg_prefix == '\0')
6013                 {
6014                   fc->col_type[reg] = DW_CFA_same_value;
6015                   fc->col_offset[reg] = 0;
6016                 }
6017               break;
6018
6019             case DW_CFA_register:
6020               reg = LEB ();
6021               roffs = LEB ();
6022               if (reg >= (unsigned int) fc->ncols)
6023                 reg_prefix = bad_reg;
6024               if (! do_debug_frames_interp || *reg_prefix != '\0')
6025                 {
6026                   printf ("  DW_CFA_register: %s%s in ",
6027                           reg_prefix, regname (reg, 0));
6028                   puts (regname (roffs, 0));
6029                 }
6030               if (*reg_prefix == '\0')
6031                 {
6032                   fc->col_type[reg] = DW_CFA_register;
6033                   fc->col_offset[reg] = roffs;
6034                 }
6035               break;
6036
6037             case DW_CFA_remember_state:
6038               if (! do_debug_frames_interp)
6039                 printf ("  DW_CFA_remember_state\n");
6040               rs = (Frame_Chunk *) xmalloc (sizeof (Frame_Chunk));
6041               rs->cfa_offset = fc->cfa_offset;
6042               rs->cfa_reg = fc->cfa_reg;
6043               rs->ra = fc->ra;
6044               rs->cfa_exp = fc->cfa_exp;
6045               rs->ncols = fc->ncols;
6046               rs->col_type = (short int *) xcmalloc (rs->ncols,
6047                                                      sizeof (* rs->col_type));
6048               rs->col_offset = (int *) xcmalloc (rs->ncols, sizeof (* rs->col_offset));
6049               memcpy (rs->col_type, fc->col_type, rs->ncols * sizeof (* fc->col_type));
6050               memcpy (rs->col_offset, fc->col_offset, rs->ncols * sizeof (* fc->col_offset));
6051               rs->next = remembered_state;
6052               remembered_state = rs;
6053               break;
6054
6055             case DW_CFA_restore_state:
6056               if (! do_debug_frames_interp)
6057                 printf ("  DW_CFA_restore_state\n");
6058               rs = remembered_state;
6059               if (rs)
6060                 {
6061                   remembered_state = rs->next;
6062                   fc->cfa_offset = rs->cfa_offset;
6063                   fc->cfa_reg = rs->cfa_reg;
6064                   fc->ra = rs->ra;
6065                   fc->cfa_exp = rs->cfa_exp;
6066                   frame_need_space (fc, rs->ncols - 1);
6067                   memcpy (fc->col_type, rs->col_type, rs->ncols * sizeof (* rs->col_type));
6068                   memcpy (fc->col_offset, rs->col_offset,
6069                           rs->ncols * sizeof (* rs->col_offset));
6070                   free (rs->col_type);
6071                   free (rs->col_offset);
6072                   free (rs);
6073                 }
6074               else if (do_debug_frames_interp)
6075                 printf ("Mismatched DW_CFA_restore_state\n");
6076               break;
6077
6078             case DW_CFA_def_cfa:
6079               fc->cfa_reg = LEB ();
6080               fc->cfa_offset = LEB ();
6081               fc->cfa_exp = 0;
6082               if (! do_debug_frames_interp)
6083                 printf ("  DW_CFA_def_cfa: %s ofs %d\n",
6084                         regname (fc->cfa_reg, 0), fc->cfa_offset);
6085               break;
6086
6087             case DW_CFA_def_cfa_register:
6088               fc->cfa_reg = LEB ();
6089               fc->cfa_exp = 0;
6090               if (! do_debug_frames_interp)
6091                 printf ("  DW_CFA_def_cfa_register: %s\n",
6092                         regname (fc->cfa_reg, 0));
6093               break;
6094
6095             case DW_CFA_def_cfa_offset:
6096               fc->cfa_offset = LEB ();
6097               if (! do_debug_frames_interp)
6098                 printf ("  DW_CFA_def_cfa_offset: %d\n", fc->cfa_offset);
6099               break;
6100
6101             case DW_CFA_nop:
6102               if (! do_debug_frames_interp)
6103                 printf ("  DW_CFA_nop\n");
6104               break;
6105
6106             case DW_CFA_def_cfa_expression:
6107               ul = LEB ();
6108               if (! do_debug_frames_interp)
6109                 {
6110                   printf ("  DW_CFA_def_cfa_expression (");
6111                   decode_location_expression (start, eh_addr_size, 0, -1,
6112                                               ul, 0, section);
6113                   printf (")\n");
6114                 }
6115               fc->cfa_exp = 1;
6116               start += ul;
6117               break;
6118
6119             case DW_CFA_expression:
6120               reg = LEB ();
6121               ul = LEB ();
6122               if (reg >= (unsigned int) fc->ncols)
6123                 reg_prefix = bad_reg;
6124               if (! do_debug_frames_interp || *reg_prefix != '\0')
6125                 {
6126                   printf ("  DW_CFA_expression: %s%s (",
6127                           reg_prefix, regname (reg, 0));
6128                   decode_location_expression (start, eh_addr_size, 0, -1,
6129                                               ul, 0, section);
6130                   printf (")\n");
6131                 }
6132               if (*reg_prefix == '\0')
6133                 fc->col_type[reg] = DW_CFA_expression;
6134               start += ul;
6135               break;
6136
6137             case DW_CFA_val_expression:
6138               reg = LEB ();
6139               ul = LEB ();
6140               if (reg >= (unsigned int) fc->ncols)
6141                 reg_prefix = bad_reg;
6142               if (! do_debug_frames_interp || *reg_prefix != '\0')
6143                 {
6144                   printf ("  DW_CFA_val_expression: %s%s (",
6145                           reg_prefix, regname (reg, 0));
6146                   decode_location_expression (start, eh_addr_size, 0, -1,
6147                                               ul, 0, section);
6148                   printf (")\n");
6149                 }
6150               if (*reg_prefix == '\0')
6151                 fc->col_type[reg] = DW_CFA_val_expression;
6152               start += ul;
6153               break;
6154
6155             case DW_CFA_offset_extended_sf:
6156               reg = LEB ();
6157               l = SLEB ();
6158               if (frame_need_space (fc, reg) < 0)
6159                 reg_prefix = bad_reg;
6160               if (! do_debug_frames_interp || *reg_prefix != '\0')
6161                 printf ("  DW_CFA_offset_extended_sf: %s%s at cfa%+ld\n",
6162                         reg_prefix, regname (reg, 0),
6163                         l * fc->data_factor);
6164               if (*reg_prefix == '\0')
6165                 {
6166                   fc->col_type[reg] = DW_CFA_offset;
6167                   fc->col_offset[reg] = l * fc->data_factor;
6168                 }
6169               break;
6170
6171             case DW_CFA_val_offset_sf:
6172               reg = LEB ();
6173               l = SLEB ();
6174               if (frame_need_space (fc, reg) < 0)
6175                 reg_prefix = bad_reg;
6176               if (! do_debug_frames_interp || *reg_prefix != '\0')
6177                 printf ("  DW_CFA_val_offset_sf: %s%s at cfa%+ld\n",
6178                         reg_prefix, regname (reg, 0),
6179                         l * fc->data_factor);
6180               if (*reg_prefix == '\0')
6181                 {
6182                   fc->col_type[reg] = DW_CFA_val_offset;
6183                   fc->col_offset[reg] = l * fc->data_factor;
6184                 }
6185               break;
6186
6187             case DW_CFA_def_cfa_sf:
6188               fc->cfa_reg = LEB ();
6189               fc->cfa_offset = SLEB ();
6190               fc->cfa_offset = fc->cfa_offset * fc->data_factor;
6191               fc->cfa_exp = 0;
6192               if (! do_debug_frames_interp)
6193                 printf ("  DW_CFA_def_cfa_sf: %s ofs %d\n",
6194                         regname (fc->cfa_reg, 0), fc->cfa_offset);
6195               break;
6196
6197             case DW_CFA_def_cfa_offset_sf:
6198               fc->cfa_offset = SLEB ();
6199               fc->cfa_offset = fc->cfa_offset * fc->data_factor;
6200               if (! do_debug_frames_interp)
6201                 printf ("  DW_CFA_def_cfa_offset_sf: %d\n", fc->cfa_offset);
6202               break;
6203
6204             case DW_CFA_MIPS_advance_loc8:
6205               SAFE_BYTE_GET_AND_INC (ofs, start, 8, end);
6206               if (do_debug_frames_interp)
6207                 frame_display_row (fc, &need_col_headers, &max_regs);
6208               else
6209                 printf ("  DW_CFA_MIPS_advance_loc8: %ld to %s\n",
6210                         (unsigned long) (ofs * fc->code_factor),
6211                         dwarf_vmatoa_1 (NULL,
6212                                         fc->pc_begin + ofs * fc->code_factor,
6213                                         fc->ptr_size));
6214               fc->pc_begin += ofs * fc->code_factor;
6215               break;
6216
6217             case DW_CFA_GNU_window_save:
6218               if (! do_debug_frames_interp)
6219                 printf ("  DW_CFA_GNU_window_save\n");
6220               break;
6221
6222             case DW_CFA_GNU_args_size:
6223               ul = LEB ();
6224               if (! do_debug_frames_interp)
6225                 printf ("  DW_CFA_GNU_args_size: %ld\n", ul);
6226               break;
6227
6228             case DW_CFA_GNU_negative_offset_extended:
6229               reg = LEB ();
6230               l = - LEB ();
6231               if (frame_need_space (fc, reg) < 0)
6232                 reg_prefix = bad_reg;
6233               if (! do_debug_frames_interp || *reg_prefix != '\0')
6234                 printf ("  DW_CFA_GNU_negative_offset_extended: %s%s at cfa%+ld\n",
6235                         reg_prefix, regname (reg, 0),
6236                         l * fc->data_factor);
6237               if (*reg_prefix == '\0')
6238                 {
6239                   fc->col_type[reg] = DW_CFA_offset;
6240                   fc->col_offset[reg] = l * fc->data_factor;
6241                 }
6242               break;
6243
6244             default:
6245               if (op >= DW_CFA_lo_user && op <= DW_CFA_hi_user)
6246                 printf (_("  DW_CFA_??? (User defined call frame op: %#x)\n"), op);
6247               else
6248                 warn (_("Unsupported or unknown Dwarf Call Frame Instruction number: %#x\n"), op);
6249               start = block_end;
6250             }
6251         }
6252
6253       if (do_debug_frames_interp)
6254         frame_display_row (fc, &need_col_headers, &max_regs);
6255
6256       start = block_end;
6257       eh_addr_size = saved_eh_addr_size;
6258     }
6259
6260   printf ("\n");
6261
6262   return 1;
6263 }
6264
6265 #undef GET
6266 #undef LEB
6267 #undef SLEB
6268
6269 static int
6270 display_gdb_index (struct dwarf_section *section,
6271                    void *file ATTRIBUTE_UNUSED)
6272 {
6273   unsigned char *start = section->start;
6274   uint32_t version;
6275   uint32_t cu_list_offset, tu_list_offset;
6276   uint32_t address_table_offset, symbol_table_offset, constant_pool_offset;
6277   unsigned int cu_list_elements, tu_list_elements;
6278   unsigned int address_table_size, symbol_table_slots;
6279   unsigned char *cu_list, *tu_list;
6280   unsigned char *address_table, *symbol_table, *constant_pool;
6281   unsigned int i;
6282
6283   /* The documentation for the format of this file is in gdb/dwarf2read.c.  */
6284
6285   printf (_("Contents of the %s section:\n"), section->name);
6286
6287   if (section->size < 6 * sizeof (uint32_t))
6288     {
6289       warn (_("Truncated header in the %s section.\n"), section->name);
6290       return 0;
6291     }
6292
6293   version = byte_get_little_endian (start, 4);
6294   printf (_("Version %ld\n"), (long) version);
6295
6296   /* Prior versions are obsolete, and future versions may not be
6297      backwards compatible.  */
6298   if (version < 3 || version > 8)
6299     {
6300       warn (_("Unsupported version %lu.\n"), (unsigned long) version);
6301       return 0;
6302     }
6303   if (version < 4)
6304     warn (_("The address table data in version 3 may be wrong.\n"));
6305   if (version < 5)
6306     warn (_("Version 4 does not support case insensitive lookups.\n"));
6307   if (version < 6)
6308     warn (_("Version 5 does not include inlined functions.\n"));
6309   if (version < 7)
6310       warn (_("Version 6 does not include symbol attributes.\n"));
6311   /* Version 7 indices generated by Gold have bad type unit references,
6312      PR binutils/15021.  But we don't know if the index was generated by
6313      Gold or not, so to avoid worrying users with gdb-generated indices
6314      we say nothing for version 7 here.  */
6315
6316   cu_list_offset = byte_get_little_endian (start + 4, 4);
6317   tu_list_offset = byte_get_little_endian (start + 8, 4);
6318   address_table_offset = byte_get_little_endian (start + 12, 4);
6319   symbol_table_offset = byte_get_little_endian (start + 16, 4);
6320   constant_pool_offset = byte_get_little_endian (start + 20, 4);
6321
6322   if (cu_list_offset > section->size
6323       || tu_list_offset > section->size
6324       || address_table_offset > section->size
6325       || symbol_table_offset > section->size
6326       || constant_pool_offset > section->size)
6327     {
6328       warn (_("Corrupt header in the %s section.\n"), section->name);
6329       return 0;
6330     }
6331
6332   cu_list_elements = (tu_list_offset - cu_list_offset) / 8;
6333   tu_list_elements = (address_table_offset - tu_list_offset) / 8;
6334   address_table_size = symbol_table_offset - address_table_offset;
6335   symbol_table_slots = (constant_pool_offset - symbol_table_offset) / 8;
6336
6337   cu_list = start + cu_list_offset;
6338   tu_list = start + tu_list_offset;
6339   address_table = start + address_table_offset;
6340   symbol_table = start + symbol_table_offset;
6341   constant_pool = start + constant_pool_offset;
6342
6343   printf (_("\nCU table:\n"));
6344   for (i = 0; i < cu_list_elements; i += 2)
6345     {
6346       uint64_t cu_offset = byte_get_little_endian (cu_list + i * 8, 8);
6347       uint64_t cu_length = byte_get_little_endian (cu_list + i * 8 + 8, 8);
6348
6349       printf (_("[%3u] 0x%lx - 0x%lx\n"), i / 2,
6350               (unsigned long) cu_offset,
6351               (unsigned long) (cu_offset + cu_length - 1));
6352     }
6353
6354   printf (_("\nTU table:\n"));
6355   for (i = 0; i < tu_list_elements; i += 3)
6356     {
6357       uint64_t tu_offset = byte_get_little_endian (tu_list + i * 8, 8);
6358       uint64_t type_offset = byte_get_little_endian (tu_list + i * 8 + 8, 8);
6359       uint64_t signature = byte_get_little_endian (tu_list + i * 8 + 16, 8);
6360
6361       printf (_("[%3u] 0x%lx 0x%lx "), i / 3,
6362               (unsigned long) tu_offset,
6363               (unsigned long) type_offset);
6364       print_dwarf_vma (signature, 8);
6365       printf ("\n");
6366     }
6367
6368   printf (_("\nAddress table:\n"));
6369   for (i = 0; i < address_table_size; i += 2 * 8 + 4)
6370     {
6371       uint64_t low = byte_get_little_endian (address_table + i, 8);
6372       uint64_t high = byte_get_little_endian (address_table + i + 8, 8);
6373       uint32_t cu_index = byte_get_little_endian (address_table + i + 16, 4);
6374
6375       print_dwarf_vma (low, 8);
6376       print_dwarf_vma (high, 8);
6377       printf (_("%lu\n"), (unsigned long) cu_index);
6378     }
6379
6380   printf (_("\nSymbol table:\n"));
6381   for (i = 0; i < symbol_table_slots; ++i)
6382     {
6383       uint32_t name_offset = byte_get_little_endian (symbol_table + i * 8, 4);
6384       uint32_t cu_vector_offset = byte_get_little_endian (symbol_table + i * 8 + 4, 4);
6385       uint32_t num_cus, cu;
6386
6387       if (name_offset != 0
6388           || cu_vector_offset != 0)
6389         {
6390           unsigned int j;
6391
6392           printf ("[%3u] %s:", i, constant_pool + name_offset);
6393           num_cus = byte_get_little_endian (constant_pool + cu_vector_offset, 4);
6394           if (num_cus > 1)
6395             printf ("\n");
6396           for (j = 0; j < num_cus; ++j)
6397             {
6398               int is_static;
6399               gdb_index_symbol_kind kind;
6400
6401               cu = byte_get_little_endian (constant_pool + cu_vector_offset + 4 + j * 4, 4);
6402               is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu);
6403               kind = GDB_INDEX_SYMBOL_KIND_VALUE (cu);
6404               cu = GDB_INDEX_CU_VALUE (cu);
6405               /* Convert to TU number if it's for a type unit.  */
6406               if (cu >= cu_list_elements / 2)
6407                 printf ("%cT%lu", num_cus > 1 ? '\t' : ' ',
6408                         (unsigned long) (cu - cu_list_elements / 2));
6409               else
6410                 printf ("%c%lu", num_cus > 1 ? '\t' : ' ', (unsigned long) cu);
6411
6412               printf (" [%s, %s]",
6413                       is_static ? _("static") : _("global"),
6414                       get_gdb_index_symbol_kind_name (kind));
6415               if (num_cus > 1)
6416                 printf ("\n");
6417             }
6418           if (num_cus <= 1)
6419             printf ("\n");
6420         }
6421     }
6422
6423   return 1;
6424 }
6425
6426 /* Pre-allocate enough space for the CU/TU sets needed.  */
6427
6428 static void
6429 prealloc_cu_tu_list (unsigned int nshndx)
6430 {
6431   if (shndx_pool == NULL)
6432     {
6433       shndx_pool_size = nshndx;
6434       shndx_pool_used = 0;
6435       shndx_pool = (unsigned int *) xcmalloc (shndx_pool_size,
6436                                               sizeof (unsigned int));
6437     }
6438   else
6439     {
6440       shndx_pool_size = shndx_pool_used + nshndx;
6441       shndx_pool = (unsigned int *) xcrealloc (shndx_pool, shndx_pool_size,
6442                                                sizeof (unsigned int));
6443     }
6444 }
6445
6446 static void
6447 add_shndx_to_cu_tu_entry (unsigned int shndx)
6448 {
6449   if (shndx_pool_used >= shndx_pool_size)
6450     {
6451       error (_("Internal error: out of space in the shndx pool.\n"));
6452       return;
6453     }
6454   shndx_pool [shndx_pool_used++] = shndx;
6455 }
6456
6457 static void
6458 end_cu_tu_entry (void)
6459 {
6460   if (shndx_pool_used >= shndx_pool_size)
6461     {
6462       error (_("Internal error: out of space in the shndx pool.\n"));
6463       return;
6464     }
6465   shndx_pool [shndx_pool_used++] = 0;
6466 }
6467
6468 /* Return the short name of a DWARF section given by a DW_SECT enumerator.  */
6469
6470 static const char *
6471 get_DW_SECT_short_name (unsigned int dw_sect)
6472 {
6473   static char buf[16];
6474
6475   switch (dw_sect)
6476     {
6477       case DW_SECT_INFO:
6478         return "info";
6479       case DW_SECT_TYPES:
6480         return "types";
6481       case DW_SECT_ABBREV:
6482         return "abbrev";
6483       case DW_SECT_LINE:
6484         return "line";
6485       case DW_SECT_LOC:
6486         return "loc";
6487       case DW_SECT_STR_OFFSETS:
6488         return "str_off";
6489       case DW_SECT_MACINFO:
6490         return "macinfo";
6491       case DW_SECT_MACRO:
6492         return "macro";
6493       default:
6494         break;
6495     }
6496
6497   snprintf (buf, sizeof (buf), "%d", dw_sect);
6498   return buf;
6499 }
6500
6501 /* Process a CU or TU index.  If DO_DISPLAY is true, print the contents.
6502    These sections are extensions for Fission.
6503    See http://gcc.gnu.org/wiki/DebugFissionDWP.  */
6504
6505 static int
6506 process_cu_tu_index (struct dwarf_section *section, int do_display)
6507 {
6508   unsigned char *phdr = section->start;
6509   unsigned char *limit = phdr + section->size;
6510   unsigned char *phash;
6511   unsigned char *pindex;
6512   unsigned char *ppool;
6513   unsigned int version;
6514   unsigned int ncols = 0;
6515   unsigned int nused;
6516   unsigned int nslots;
6517   unsigned int i;
6518   unsigned int j;
6519   dwarf_vma signature_high;
6520   dwarf_vma signature_low;
6521   char buf[64];
6522
6523   version = byte_get (phdr, 4);
6524   if (version >= 2)
6525     ncols = byte_get (phdr + 4, 4);
6526   nused = byte_get (phdr + 8, 4);
6527   nslots = byte_get (phdr + 12, 4);
6528   phash = phdr + 16;
6529   pindex = phash + nslots * 8;
6530   ppool = pindex + nslots * 4;
6531
6532   if (do_display)
6533     {
6534       printf (_("Contents of the %s section:\n\n"), section->name);
6535       printf (_("  Version:                 %d\n"), version);
6536       if (version >= 2)
6537         printf (_("  Number of columns:       %d\n"), ncols);
6538       printf (_("  Number of used entries:  %d\n"), nused);
6539       printf (_("  Number of slots:         %d\n\n"), nslots);
6540     }
6541
6542   if (ppool > limit)
6543     {
6544       warn (_("Section %s too small for %d hash table entries\n"),
6545             section->name, nslots);
6546       return 0;
6547     }
6548
6549   if (version == 1)
6550     {
6551       if (!do_display)
6552         prealloc_cu_tu_list ((limit - ppool) / 4);
6553       for (i = 0; i < nslots; i++)
6554         {
6555           unsigned char *shndx_list;
6556           unsigned int shndx;
6557
6558           byte_get_64 (phash, &signature_high, &signature_low);
6559           if (signature_high != 0 || signature_low != 0)
6560             {
6561               j = byte_get (pindex, 4);
6562               shndx_list = ppool + j * 4;
6563               if (do_display)
6564                 printf (_("  [%3d] Signature:  0x%s  Sections: "),
6565                         i, dwarf_vmatoa64 (signature_high, signature_low,
6566                                            buf, sizeof (buf)));
6567               for (;;)
6568                 {
6569                   if (shndx_list >= limit)
6570                     {
6571                       warn (_("Section %s too small for shndx pool\n"),
6572                             section->name);
6573                       return 0;
6574                     }
6575                   shndx = byte_get (shndx_list, 4);
6576                   if (shndx == 0)
6577                     break;
6578                   if (do_display)
6579                     printf (" %d", shndx);
6580                   else
6581                     add_shndx_to_cu_tu_entry (shndx);
6582                   shndx_list += 4;
6583                 }
6584               if (do_display)
6585                 printf ("\n");
6586               else
6587                 end_cu_tu_entry ();
6588             }
6589           phash += 8;
6590           pindex += 4;
6591         }
6592     }
6593   else if (version == 2)
6594     {
6595       unsigned int val;
6596       unsigned int dw_sect;
6597       unsigned char *ph = phash;
6598       unsigned char *pi = pindex;
6599       unsigned char *poffsets = ppool + ncols * 4;
6600       unsigned char *psizes = poffsets + nused * ncols * 4;
6601       unsigned char *pend = psizes + nused * ncols * 4;
6602       bfd_boolean is_tu_index;
6603       struct cu_tu_set *this_set = NULL;
6604       unsigned int row;
6605       unsigned char *prow;
6606
6607       is_tu_index = strcmp (section->name, ".debug_tu_index") == 0;
6608
6609       if (pend > limit)
6610         {
6611           warn (_("Section %s too small for offset and size tables\n"),
6612                 section->name);
6613           return 0;
6614         }
6615
6616       if (do_display)
6617         {
6618           printf (_("  Offset table\n"));
6619           printf ("  slot  %-16s  ",
6620                  is_tu_index ? _("signature") : _("dwo_id"));
6621         }
6622       else
6623         {
6624           if (is_tu_index)
6625             {
6626               tu_count = nused;
6627               tu_sets = xcmalloc (nused, sizeof (struct cu_tu_set));
6628               this_set = tu_sets;
6629             }
6630           else
6631             {
6632               cu_count = nused;
6633               cu_sets = xcmalloc (nused, sizeof (struct cu_tu_set));
6634               this_set = cu_sets;
6635             }
6636         }
6637       if (do_display)
6638         {
6639           for (j = 0; j < ncols; j++)
6640             {
6641               dw_sect = byte_get (ppool + j * 4, 4);
6642               printf (" %8s", get_DW_SECT_short_name (dw_sect));
6643             }
6644           printf ("\n");
6645         }
6646       for (i = 0; i < nslots; i++)
6647         {
6648           byte_get_64 (ph, &signature_high, &signature_low);
6649           row = byte_get (pi, 4);
6650           if (row != 0)
6651             {
6652               if (!do_display)
6653                 memcpy (&this_set[row - 1].signature, ph, sizeof (uint64_t));
6654               prow = poffsets + (row - 1) * ncols * 4;
6655               if (do_display)
6656                 printf (_("  [%3d] 0x%s"),
6657                         i, dwarf_vmatoa64 (signature_high, signature_low,
6658                                            buf, sizeof (buf)));
6659               for (j = 0; j < ncols; j++)
6660                 {
6661                   val = byte_get (prow + j * 4, 4);
6662                   if (do_display)
6663                     printf (" %8d", val);
6664                   else
6665                     {
6666                       dw_sect = byte_get (ppool + j * 4, 4);
6667                       this_set [row - 1].section_offsets [dw_sect] = val;
6668                     }
6669                 }
6670               if (do_display)
6671                 printf ("\n");
6672             }
6673           ph += 8;
6674           pi += 4;
6675         }
6676
6677       ph = phash;
6678       pi = pindex;
6679       if (do_display)
6680         {
6681           printf ("\n");
6682           printf (_("  Size table\n"));
6683           printf ("  slot  %-16s  ",
6684                  is_tu_index ? _("signature") : _("dwo_id"));
6685         }
6686       for (j = 0; j < ncols; j++)
6687         {
6688           val = byte_get (ppool + j * 4, 4);
6689           if (do_display)
6690             printf (" %8s", get_DW_SECT_short_name (val));
6691         }
6692       if (do_display)
6693         printf ("\n");
6694       for (i = 0; i < nslots; i++)
6695         {
6696           byte_get_64 (ph, &signature_high, &signature_low);
6697           row = byte_get (pi, 4);
6698           if (row != 0)
6699             {
6700               prow = psizes + (row - 1) * ncols * 4;
6701               if (do_display)
6702                 printf (_("  [%3d] 0x%s"),
6703                         i, dwarf_vmatoa64 (signature_high, signature_low,
6704                                            buf, sizeof (buf)));
6705               for (j = 0; j < ncols; j++)
6706                 {
6707                   val = byte_get (prow + j * 4, 4);
6708                   if (do_display)
6709                     printf (" %8d", val);
6710                   else
6711                     {
6712                       dw_sect = byte_get (ppool + j * 4, 4);
6713                       this_set [row - 1].section_sizes [dw_sect] = val;
6714                     }
6715                 }
6716               if (do_display)
6717                 printf ("\n");
6718             }
6719           ph += 8;
6720           pi += 4;
6721         }
6722     }
6723   else if (do_display)
6724     printf (_("  Unsupported version\n"));
6725
6726   if (do_display)
6727       printf ("\n");
6728
6729   return 1;
6730 }
6731
6732 /* Load the CU and TU indexes if present.  This will build a list of
6733    section sets that we can use to associate a .debug_info.dwo section
6734    with its associated .debug_abbrev.dwo section in a .dwp file.  */
6735
6736 static void
6737 load_cu_tu_indexes (void *file)
6738 {
6739   /* If we have already loaded (or tried to load) the CU and TU indexes
6740      then do not bother to repeat the task.  */
6741   if (cu_tu_indexes_read)
6742     return;
6743
6744   if (load_debug_section (dwp_cu_index, file))
6745     process_cu_tu_index (&debug_displays [dwp_cu_index].section, 0);
6746
6747   if (load_debug_section (dwp_tu_index, file))
6748     process_cu_tu_index (&debug_displays [dwp_tu_index].section, 0);
6749
6750   cu_tu_indexes_read = 1;
6751 }
6752
6753 /* Find the set of sections that includes section SHNDX.  */
6754
6755 unsigned int *
6756 find_cu_tu_set (void *file, unsigned int shndx)
6757 {
6758   unsigned int i;
6759
6760   load_cu_tu_indexes (file);
6761
6762   /* Find SHNDX in the shndx pool.  */
6763   for (i = 0; i < shndx_pool_used; i++)
6764     if (shndx_pool [i] == shndx)
6765       break;
6766
6767   if (i >= shndx_pool_used)
6768     return NULL;
6769
6770   /* Now backup to find the first entry in the set.  */
6771   while (i > 0 && shndx_pool [i - 1] != 0)
6772     i--;
6773
6774   return shndx_pool + i;
6775 }
6776
6777 /* Display a .debug_cu_index or .debug_tu_index section.  */
6778
6779 static int
6780 display_cu_index (struct dwarf_section *section, void *file ATTRIBUTE_UNUSED)
6781 {
6782   return process_cu_tu_index (section, 1);
6783 }
6784
6785 static int
6786 display_debug_not_supported (struct dwarf_section *section,
6787                              void *file ATTRIBUTE_UNUSED)
6788 {
6789   printf (_("Displaying the debug contents of section %s is not yet supported.\n"),
6790             section->name);
6791
6792   return 1;
6793 }
6794
6795 void *
6796 cmalloc (size_t nmemb, size_t size)
6797 {
6798   /* Check for overflow.  */
6799   if (nmemb >= ~(size_t) 0 / size)
6800     return NULL;
6801   else
6802     return malloc (nmemb * size);
6803 }
6804
6805 void *
6806 xcmalloc (size_t nmemb, size_t size)
6807 {
6808   /* Check for overflow.  */
6809   if (nmemb >= ~(size_t) 0 / size)
6810     return NULL;
6811   else
6812     return xmalloc (nmemb * size);
6813 }
6814
6815 void *
6816 xcrealloc (void *ptr, size_t nmemb, size_t size)
6817 {
6818   /* Check for overflow.  */
6819   if (nmemb >= ~(size_t) 0 / size)
6820     return NULL;
6821   else
6822     return xrealloc (ptr, nmemb * size);
6823 }
6824
6825 void
6826 free_debug_memory (void)
6827 {
6828   unsigned int i;
6829
6830   free_abbrevs ();
6831
6832   for (i = 0; i < max; i++)
6833     free_debug_section ((enum dwarf_section_display_enum) i);
6834
6835   if (debug_information != NULL)
6836     {
6837       if (num_debug_info_entries != DEBUG_INFO_UNAVAILABLE)
6838         {
6839           for (i = 0; i < num_debug_info_entries; i++)
6840             {
6841               if (!debug_information [i].max_loc_offsets)
6842                 {
6843                   free (debug_information [i].loc_offsets);
6844                   free (debug_information [i].have_frame_base);
6845                 }
6846               if (!debug_information [i].max_range_lists)
6847                 free (debug_information [i].range_lists);
6848             }
6849         }
6850
6851       free (debug_information);
6852       debug_information = NULL;
6853       num_debug_info_entries = 0;
6854     }
6855 }
6856
6857 void
6858 dwarf_select_sections_by_names (const char *names)
6859 {
6860   typedef struct
6861   {
6862     const char * option;
6863     int *        variable;
6864     int          val;
6865   }
6866   debug_dump_long_opts;
6867
6868   static const debug_dump_long_opts opts_table [] =
6869     {
6870       /* Please keep this table alpha- sorted.  */
6871       { "Ranges", & do_debug_ranges, 1 },
6872       { "abbrev", & do_debug_abbrevs, 1 },
6873       { "addr", & do_debug_addr, 1 },
6874       { "aranges", & do_debug_aranges, 1 },
6875       { "cu_index", & do_debug_cu_index, 1 },
6876       { "decodedline", & do_debug_lines, FLAG_DEBUG_LINES_DECODED },
6877       { "frames", & do_debug_frames, 1 },
6878       { "frames-interp", & do_debug_frames_interp, 1 },
6879       /* The special .gdb_index section.  */
6880       { "gdb_index", & do_gdb_index, 1 },
6881       { "info", & do_debug_info, 1 },
6882       { "line", & do_debug_lines, FLAG_DEBUG_LINES_RAW }, /* For backwards compatibility.  */
6883       { "loc",  & do_debug_loc, 1 },
6884       { "macro", & do_debug_macinfo, 1 },
6885       { "pubnames", & do_debug_pubnames, 1 },
6886       { "pubtypes", & do_debug_pubtypes, 1 },
6887       /* This entry is for compatability
6888          with earlier versions of readelf.  */
6889       { "ranges", & do_debug_aranges, 1 },
6890       { "rawline", & do_debug_lines, FLAG_DEBUG_LINES_RAW },
6891       { "str", & do_debug_str, 1 },
6892       /* These trace_* sections are used by Itanium VMS.  */
6893       { "trace_abbrev", & do_trace_abbrevs, 1 },
6894       { "trace_aranges", & do_trace_aranges, 1 },
6895       { "trace_info", & do_trace_info, 1 },
6896       { NULL, NULL, 0 }
6897     };
6898
6899   const char *p;
6900
6901   p = names;
6902   while (*p)
6903     {
6904       const debug_dump_long_opts * entry;
6905
6906       for (entry = opts_table; entry->option; entry++)
6907         {
6908           size_t len = strlen (entry->option);
6909
6910           if (strncmp (p, entry->option, len) == 0
6911               && (p[len] == ',' || p[len] == '\0'))
6912             {
6913               * entry->variable |= entry->val;
6914
6915               /* The --debug-dump=frames-interp option also
6916                  enables the --debug-dump=frames option.  */
6917               if (do_debug_frames_interp)
6918                 do_debug_frames = 1;
6919
6920               p += len;
6921               break;
6922             }
6923         }
6924
6925       if (entry->option == NULL)
6926         {
6927           warn (_("Unrecognized debug option '%s'\n"), p);
6928           p = strchr (p, ',');
6929           if (p == NULL)
6930             break;
6931         }
6932
6933       if (*p == ',')
6934         p++;
6935     }
6936 }
6937
6938 void
6939 dwarf_select_sections_by_letters (const char *letters)
6940 {
6941   unsigned int lindex = 0;
6942
6943   while (letters[lindex])
6944     switch (letters[lindex++])
6945       {
6946       case 'i':
6947         do_debug_info = 1;
6948         break;
6949
6950       case 'a':
6951         do_debug_abbrevs = 1;
6952         break;
6953
6954       case 'l':
6955         do_debug_lines |= FLAG_DEBUG_LINES_RAW;
6956         break;
6957
6958       case 'L':
6959         do_debug_lines |= FLAG_DEBUG_LINES_DECODED;
6960         break;
6961
6962       case 'p':
6963         do_debug_pubnames = 1;
6964         break;
6965
6966       case 't':
6967         do_debug_pubtypes = 1;
6968         break;
6969
6970       case 'r':
6971         do_debug_aranges = 1;
6972         break;
6973
6974       case 'R':
6975         do_debug_ranges = 1;
6976         break;
6977
6978       case 'F':
6979         do_debug_frames_interp = 1;
6980       case 'f':
6981         do_debug_frames = 1;
6982         break;
6983
6984       case 'm':
6985         do_debug_macinfo = 1;
6986         break;
6987
6988       case 's':
6989         do_debug_str = 1;
6990         break;
6991
6992       case 'o':
6993         do_debug_loc = 1;
6994         break;
6995
6996       default:
6997         warn (_("Unrecognized debug option '%s'\n"), optarg);
6998         break;
6999       }
7000 }
7001
7002 void
7003 dwarf_select_sections_all (void)
7004 {
7005   do_debug_info = 1;
7006   do_debug_abbrevs = 1;
7007   do_debug_lines = FLAG_DEBUG_LINES_RAW;
7008   do_debug_pubnames = 1;
7009   do_debug_pubtypes = 1;
7010   do_debug_aranges = 1;
7011   do_debug_ranges = 1;
7012   do_debug_frames = 1;
7013   do_debug_macinfo = 1;
7014   do_debug_str = 1;
7015   do_debug_loc = 1;
7016   do_gdb_index = 1;
7017   do_trace_info = 1;
7018   do_trace_abbrevs = 1;
7019   do_trace_aranges = 1;
7020   do_debug_addr = 1;
7021   do_debug_cu_index = 1;
7022 }
7023
7024 struct dwarf_section_display debug_displays[] =
7025 {
7026   { { ".debug_abbrev",      ".zdebug_abbrev",   NULL, NULL, 0, 0, 0 },
7027     display_debug_abbrev,   &do_debug_abbrevs,  0 },
7028   { { ".debug_aranges",     ".zdebug_aranges",  NULL, NULL, 0, 0, 0 },
7029     display_debug_aranges,  &do_debug_aranges,  1 },
7030   { { ".debug_frame",       ".zdebug_frame",    NULL, NULL, 0, 0, 0 },
7031     display_debug_frames,   &do_debug_frames,   1 },
7032   { { ".debug_info",        ".zdebug_info",     NULL, NULL, 0, 0, abbrev },
7033     display_debug_info,     &do_debug_info,     1 },
7034   { { ".debug_line",        ".zdebug_line",     NULL, NULL, 0, 0, 0 },
7035     display_debug_lines,    &do_debug_lines,    1 },
7036   { { ".debug_pubnames",    ".zdebug_pubnames", NULL, NULL, 0, 0, 0 },
7037     display_debug_pubnames, &do_debug_pubnames, 0 },
7038   { { ".debug_gnu_pubnames", ".zdebug_gnu_pubnames", NULL, NULL, 0, 0, 0 },
7039     display_debug_gnu_pubnames, &do_debug_pubnames, 0 },
7040   { { ".eh_frame",          "",                 NULL, NULL, 0, 0, 0 },
7041     display_debug_frames,   &do_debug_frames,   1 },
7042   { { ".debug_macinfo",     ".zdebug_macinfo",  NULL, NULL, 0, 0, 0 },
7043     display_debug_macinfo,  &do_debug_macinfo,  0 },
7044   { { ".debug_macro",       ".zdebug_macro",    NULL, NULL, 0, 0, 0 },
7045     display_debug_macro,    &do_debug_macinfo,  1 },
7046   { { ".debug_str",         ".zdebug_str",      NULL, NULL, 0, 0, 0 },
7047     display_debug_str,      &do_debug_str,      0 },
7048   { { ".debug_loc",         ".zdebug_loc",      NULL, NULL, 0, 0, 0 },
7049     display_debug_loc,      &do_debug_loc,      1 },
7050   { { ".debug_pubtypes",    ".zdebug_pubtypes", NULL, NULL, 0, 0, 0 },
7051     display_debug_pubnames, &do_debug_pubtypes, 0 },
7052   { { ".debug_gnu_pubtypes", ".zdebug_gnu_pubtypes", NULL, NULL, 0, 0, 0 },
7053     display_debug_gnu_pubnames, &do_debug_pubtypes, 0 },
7054   { { ".debug_ranges",      ".zdebug_ranges",   NULL, NULL, 0, 0, 0 },
7055     display_debug_ranges,   &do_debug_ranges,   1 },
7056   { { ".debug_static_func", ".zdebug_static_func", NULL, NULL, 0, 0, 0 },
7057     display_debug_not_supported, NULL,          0 },
7058   { { ".debug_static_vars", ".zdebug_static_vars", NULL, NULL, 0, 0, 0 },
7059     display_debug_not_supported, NULL,          0 },
7060   { { ".debug_types",       ".zdebug_types",    NULL, NULL, 0, 0, abbrev },
7061     display_debug_types,    &do_debug_info,     1 },
7062   { { ".debug_weaknames",   ".zdebug_weaknames", NULL, NULL, 0, 0, 0 },
7063     display_debug_not_supported, NULL,          0 },
7064   { { ".gdb_index",         "",                 NULL, NULL, 0, 0, 0 },
7065     display_gdb_index,      &do_gdb_index,      0 },
7066   { { ".trace_info",        "",                 NULL, NULL, 0, 0, trace_abbrev },
7067     display_trace_info,     &do_trace_info,     1 },
7068   { { ".trace_abbrev",      "",                 NULL, NULL, 0, 0, 0 },
7069     display_debug_abbrev,   &do_trace_abbrevs,  0 },
7070   { { ".trace_aranges",     "",                 NULL, NULL, 0, 0, 0 },
7071     display_debug_aranges,  &do_trace_aranges,  0 },
7072   { { ".debug_info.dwo",    ".zdebug_info.dwo", NULL, NULL, 0, 0, abbrev_dwo },
7073     display_debug_info,     &do_debug_info,     1 },
7074   { { ".debug_abbrev.dwo",  ".zdebug_abbrev.dwo", NULL, NULL, 0, 0, 0 },
7075     display_debug_abbrev,   &do_debug_abbrevs,  0 },
7076   { { ".debug_types.dwo",   ".zdebug_types.dwo", NULL, NULL, 0, 0, abbrev_dwo },
7077     display_debug_types,    &do_debug_info,     1 },
7078   { { ".debug_line.dwo",    ".zdebug_line.dwo", NULL, NULL, 0, 0, 0 },
7079     display_debug_lines,    &do_debug_lines,    1 },
7080   { { ".debug_loc.dwo",     ".zdebug_loc.dwo",  NULL, NULL, 0, 0, 0 },
7081     display_debug_loc,      &do_debug_loc,      1 },
7082   { { ".debug_macro.dwo",   ".zdebug_macro.dwo", NULL, NULL, 0, 0, 0 },
7083     display_debug_macro,    &do_debug_macinfo,  1 },
7084   { { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo", NULL, NULL, 0, 0, 0 },
7085     display_debug_macinfo,  &do_debug_macinfo,  0 },
7086   { { ".debug_str.dwo",     ".zdebug_str.dwo",  NULL, NULL, 0, 0, 0 },
7087     display_debug_str,      &do_debug_str,      1 },
7088   { { ".debug_str_offsets", ".zdebug_str_offsets", NULL, NULL, 0, 0, 0 },
7089     display_debug_str_offsets, NULL,            0 },
7090   { { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo", NULL, NULL, 0, 0, 0 },
7091     display_debug_str_offsets, NULL,            0 },
7092   { { ".debug_addr",        ".zdebug_addr",     NULL, NULL, 0, 0, 0 },
7093     display_debug_addr,     &do_debug_addr,     1 },
7094   { { ".debug_cu_index",    "",                 NULL, NULL, 0, 0, 0 },
7095     display_cu_index,       &do_debug_cu_index, 0 },
7096   { { ".debug_tu_index",    "",                 NULL, NULL, 0, 0, 0 },
7097     display_cu_index,       &do_debug_cu_index, 0 },
7098 };