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