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