Use SCOPE_EXIT in gdbarch-selftest.c
[deliverable/binutils-gdb.git] / binutils / dwarf.c
index e6f6a43ef6bda2117bbb7698c337bd2bcbd79880..60e0ef8935575a8f4aa7f04aad9700dcacc801ea 100644 (file)
@@ -1,5 +1,5 @@
 /* dwarf.c -- display DWARF contents of a BFD binary file
-   Copyright (C) 2005-2017 Free Software Foundation, Inc.
+   Copyright (C) 2005-2019 Free Software Foundation, Inc.
 
    This file is part of GNU Binutils.
 
@@ -48,6 +48,12 @@ static debug_info *debug_information = NULL;
    that the .debug_info section could not be loaded/parsed.  */
 #define DEBUG_INFO_UNAVAILABLE  (unsigned int) -1
 
+static const char *           dwo_name;
+static const char *           dwo_dir;
+static const unsigned char *  dwo_id;
+static bfd_size_type          dwo_id_len;
+static bfd_boolean            need_dwo_info;
+
 unsigned int eh_addr_size;
 
 int do_debug_info;
@@ -381,6 +387,9 @@ read_uleb128 (unsigned char * data,
     }                                                          \
   while (0)
 
+/* Read AMOUNT bytes from PTR and store them in VAL as an unsigned value.
+   Checks to make sure that the read will not reach or pass END
+   and that VAL is big enough to hold AMOUNT bytes.  */
 #define SAFE_BYTE_GET(VAL, PTR, AMOUNT, END)   \
   do                                           \
     {                                          \
@@ -409,6 +418,7 @@ read_uleb128 (unsigned char * data,
     }                                          \
   while (0)
 
+/* Like SAFE_BYTE_GET, but also increments PTR by AMOUNT.  */
 #define SAFE_BYTE_GET_AND_INC(VAL, PTR, AMOUNT, END)   \
   do                                                   \
     {                                                  \
@@ -417,6 +427,7 @@ read_uleb128 (unsigned char * data,
     }                                                  \
   while (0)
 
+/* Like SAFE_BYTE_GET, but reads a signed value.  */
 #define SAFE_SIGNED_BYTE_GET(VAL, PTR, AMOUNT, END)    \
   do                                                   \
     {                                                  \
@@ -435,6 +446,7 @@ read_uleb128 (unsigned char * data,
     }                                                  \
   while (0)
 
+/* Like SAFE_SIGNED_BYTE_GET, but also increments PTR by AMOUNT.  */
 #define SAFE_SIGNED_BYTE_GET_AND_INC(VAL, PTR, AMOUNT, END)    \
   do                                                           \
     {                                                          \
@@ -731,7 +743,7 @@ fetch_indirect_line_string (dwarf_vma offset)
 
 static const char *
 fetch_indexed_string (dwarf_vma idx, struct cu_tu_set *this_set,
-                     dwarf_vma offset_size, int dwo)
+                     dwarf_vma offset_size, bfd_boolean dwo)
 {
   enum dwarf_section_display_enum str_sec_idx = dwo ? str_dwo : str;
   enum dwarf_section_display_enum idx_sec_idx = dwo ? str_index_dwo : str_index;
@@ -1369,21 +1381,21 @@ decode_location_expression (unsigned char * data,
          printf ("DW_OP_push_object_address");
          break;
        case DW_OP_call2:
-         /* XXX: Strictly speaking for 64-bit DWARF3 files
+         /* FIXME: Strictly speaking for 64-bit DWARF3 files
             this ought to be an 8-byte wide computation.  */
          SAFE_SIGNED_BYTE_GET_AND_INC (svalue, data, 2, end);
          printf ("DW_OP_call2: <0x%s>",
                  dwarf_vmatoa ("x", svalue + cu_offset));
          break;
        case DW_OP_call4:
-         /* XXX: Strictly speaking for 64-bit DWARF3 files
+         /* FIXME: Strictly speaking for 64-bit DWARF3 files
             this ought to be an 8-byte wide computation.  */
          SAFE_SIGNED_BYTE_GET_AND_INC (svalue, data, 4, end);
          printf ("DW_OP_call4: <0x%s>",
                  dwarf_vmatoa ("x", svalue + cu_offset));
          break;
        case DW_OP_call_ref:
-         /* XXX: Strictly speaking for 64-bit DWARF3 files
+         /* FIXME: Strictly speaking for 64-bit DWARF3 files
             this ought to be an 8-byte wide computation.  */
          if (dwarf_version == -1)
            {
@@ -1452,7 +1464,7 @@ decode_location_expression (unsigned char * data,
          break;
        case DW_OP_implicit_pointer:
        case DW_OP_GNU_implicit_pointer:
-         /* XXX: Strictly speaking for 64-bit DWARF3 files
+         /* FIXME: Strictly speaking for 64-bit DWARF3 files
             this ought to be an 8-byte wide computation.  */
          if (dwarf_version == -1)
            {
@@ -1563,6 +1575,25 @@ decode_location_expression (unsigned char * data,
          data += bytes_read;
          printf ("DW_OP_GNU_const_index <0x%s>", dwarf_vmatoa ("x", uvalue));
          break;
+       case DW_OP_GNU_variable_value:
+         /* FIXME: Strictly speaking for 64-bit DWARF3 files
+            this ought to be an 8-byte wide computation.  */
+         if (dwarf_version == -1)
+           {
+             printf (_("(DW_OP_GNU_variable_value in frame info)"));
+             /* No way to tell where the next op is, so just bail.  */
+             return need_frame_base;
+           }
+         if (dwarf_version == 2)
+           {
+             SAFE_BYTE_GET_AND_INC (uvalue, data, pointer_size, end);
+           }
+         else
+           {
+             SAFE_BYTE_GET_AND_INC (uvalue, data, offset_size, end);
+           }
+         printf ("DW_OP_GNU_variable_value: <0x%s>", dwarf_vmatoa ("x", uvalue));
+         break;
 
          /* HP extensions.  */
        case DW_OP_HP_is_value:
@@ -1713,6 +1744,32 @@ fetch_alt_indirect_string (dwarf_vma offset)
   return ret;
 }
        
+static const char *
+get_AT_name (unsigned long attribute)
+{
+  const char *name;
+
+  if (attribute == 0)
+    return "DW_AT value: 0";
+
+  /* One value is shared by the MIPS and HP extensions:  */
+  if (attribute == DW_AT_MIPS_fde)
+    return "DW_AT_MIPS_fde or DW_AT_HP_unmodifiable";
+
+  name = get_DW_AT_name (attribute);
+
+  if (name == NULL)
+    {
+      static char buffer[100];
+
+      snprintf (buffer, sizeof (buffer), _("Unknown AT value: %lx"),
+               attribute);
+      return buffer;
+    }
+
+  return name;
+}
+
 static unsigned char *
 read_and_display_attr_value (unsigned long           attribute,
                             unsigned long           form,
@@ -2036,8 +2093,8 @@ read_and_display_attr_value (unsigned long           attribute,
     case DW_FORM_GNU_str_index:
       if (!do_loc)
        {
-         const char *suffix = strrchr (section->name, '.');
-         int dwo = (suffix && strcmp (suffix, ".dwo") == 0) ? 1 : 0;
+         const char * suffix = strrchr (section->name, '.');
+         bfd_boolean  dwo = (suffix && strcmp (suffix, ".dwo") == 0) ? TRUE : FALSE;
 
          printf (_("%c(indexed string: 0x%s): %s"), delimiter,
                  dwarf_vmatoa ("x", uvalue),
@@ -2136,19 +2193,28 @@ read_and_display_attr_value (unsigned long           attribute,
              debug_info_p->have_frame_base [num] = have_frame_base;
              if (attribute != DW_AT_GNU_locviews)
                {
-                 debug_info_p->loc_offsets [num] = uvalue;
-                 debug_info_p->num_loc_offsets++;
-                 assert (debug_info_p->num_loc_offsets
-                         - debug_info_p->num_loc_views <= 1);
+                 /* Corrupt DWARF info can produce more offsets than views.
+                    See PR 23062 for an example.  */
+                 if (debug_info_p->num_loc_offsets
+                     > debug_info_p->num_loc_views)
+                   warn (_("More location offset attributes than DW_AT_GNU_locview attributes\n"));
+                 else
+                   {
+                     debug_info_p->loc_offsets [num] = uvalue;
+                     debug_info_p->num_loc_offsets++;
+                   }
                }
              else
                {
                  assert (debug_info_p->num_loc_views <= num);
                  num = debug_info_p->num_loc_views;
-                 debug_info_p->loc_views [num] = uvalue;
-                 debug_info_p->num_loc_views++;
-                 assert (debug_info_p->num_loc_views
-                         - debug_info_p->num_loc_offsets <= 1);
+                 if (num > debug_info_p->num_loc_offsets)
+                   warn (_("More DW_AT_GNU_locview attributes than location offset attributes\n"));
+                 else
+                   {
+                     debug_info_p->loc_views [num] = uvalue;
+                     debug_info_p->num_loc_views++;
+                   }
                }
            }
          break;
@@ -2188,6 +2254,69 @@ read_and_display_attr_value (unsigned long           attribute,
            }
          break;
 
+       case DW_AT_GNU_dwo_name:
+       case DW_AT_dwo_name:
+         if (need_dwo_info)
+           switch (form)
+             {
+             case DW_FORM_strp:
+               dwo_name = (const char *) fetch_indirect_string (uvalue);
+               break;
+             case DW_FORM_GNU_str_index:
+               dwo_name = fetch_indexed_string (uvalue, this_set, offset_size, FALSE);
+               break;
+             case DW_FORM_string:
+               dwo_name = (const char *) orig_data;
+               break;
+             default:
+               warn (_("Unsupported form (%s) for attribute %s\n"),
+                     get_FORM_name (form), get_AT_name (attribute));
+               dwo_name = _("<unknown>");
+               break;
+             }
+         break;
+             
+       case DW_AT_comp_dir:
+         /* FIXME: Also extract a build-id in a CU/TU.  */
+         if (need_dwo_info)
+           switch (form)
+             {
+             case DW_FORM_strp:
+               dwo_dir = (const char *) fetch_indirect_string (uvalue);
+               break;
+             case DW_FORM_line_strp:
+               dwo_dir = (const char *) fetch_indirect_line_string (uvalue);
+               break;
+             case DW_FORM_GNU_str_index:
+               dwo_dir = fetch_indexed_string (uvalue, this_set, offset_size, FALSE);
+               break;
+             case DW_FORM_string:
+               dwo_dir = (const char *) orig_data;
+               break;
+             default:
+               warn (_("Unsupported form (%s) for attribute %s\n"),
+                     get_FORM_name (form), get_AT_name (attribute));
+               dwo_dir = _("<unknown>");
+               break;
+             }
+         break;
+             
+       case DW_AT_GNU_dwo_id:
+         if (need_dwo_info)
+           switch (form)
+             {
+             case DW_FORM_data8:
+               dwo_id = data - 8;
+               dwo_id_len = 8;
+               break;
+             default:
+               warn (_("Unsupported form (%s) for attribute %s\n"),
+                     get_FORM_name (form), get_AT_name (attribute));
+               dwo_id = NULL;
+               break;
+             }
+         break;
+             
        default:
          break;
        }
@@ -2251,12 +2380,22 @@ read_and_display_attr_value (unsigned long           attribute,
          /* DWARF 4 values.  */
        case DW_LANG_Python:            printf ("(Python)"); break;
          /* DWARF 5 values.  */
+       case DW_LANG_OpenCL:            printf ("(OpenCL)"); break;
        case DW_LANG_Go:                printf ("(Go)"); break;
+       case DW_LANG_Modula3:           printf ("(Modula 3)"); break;
+       case DW_LANG_Haskell:           printf ("(Haskell)"); break;
+       case DW_LANG_C_plus_plus_03:    printf ("(C++03)"); break;
        case DW_LANG_C_plus_plus_11:    printf ("(C++11)"); break;
+       case DW_LANG_OCaml:             printf ("(OCaml)"); break;
+       case DW_LANG_Rust:              printf ("(Rust)"); break;
        case DW_LANG_C11:               printf ("(C11)"); break;
+       case DW_LANG_Swift:             printf ("(Swift)"); break;
+       case DW_LANG_Julia:             printf ("(Julia)"); break;
+       case DW_LANG_Dylan:             printf ("(Dylan)"); break;
        case DW_LANG_C_plus_plus_14:    printf ("(C++14)"); break;
        case DW_LANG_Fortran03:         printf ("(Fortran 03)"); break;
        case DW_LANG_Fortran08:         printf ("(Fortran 08)"); break;
+       case DW_LANG_RenderScript:      printf ("(RenderScript)"); break;
          /* MIPS extension.  */
        case DW_LANG_Mips_Assembler:    printf ("(MIPS assembler)"); break;
          /* UPC extension.  */
@@ -2493,6 +2632,22 @@ read_and_display_attr_value (unsigned long           attribute,
        }
       break;
 
+    case DW_AT_data_bit_offset:
+    case DW_AT_byte_size:
+    case DW_AT_bit_size:
+    case DW_AT_string_length_byte_size:
+    case DW_AT_string_length_bit_size:
+    case DW_AT_bit_stride:
+      if (form == DW_FORM_exprloc)
+       {
+         printf ("\t(");
+         (void) decode_location_expression (block_start, pointer_size,
+                                            offset_size, dwarf_version,
+                                            uvalue, cu_offset, section);
+         printf (")");
+       }
+      break;
+
     case DW_AT_import:
       {
        if (form == DW_FORM_ref_sig8
@@ -2540,32 +2695,6 @@ read_and_display_attr_value (unsigned long           attribute,
   return data;
 }
 
-static const char *
-get_AT_name (unsigned long attribute)
-{
-  const char *name;
-
-  if (attribute == 0)
-    return "DW_AT value: 0";
-
-  /* One value is shared by the MIPS and HP extensions:  */
-  if (attribute == DW_AT_MIPS_fde)
-    return "DW_AT_MIPS_fde or DW_AT_HP_unmodifiable";
-
-  name = get_DW_AT_name (attribute);
-
-  if (name == NULL)
-    {
-      static char buffer[100];
-
-      snprintf (buffer, sizeof (buffer), _("Unknown AT value: %lx"),
-               attribute);
-      return buffer;
-    }
-
-  return name;
-}
-
 static unsigned char *
 read_and_display_attr (unsigned long           attribute,
                       unsigned long           form,
@@ -2642,17 +2771,22 @@ introduce (struct dwarf_section * section, bfd_boolean raw)
     }
 }
   
-/* Process the contents of a .debug_info section.  If do_loc is non-zero
-   then we are scanning for location lists and we do not want to display
-   anything to the user.  If do_types is non-zero, we are processing
-   a .debug_types section instead of a .debug_info section.  */
+/* Process the contents of a .debug_info section.
+   If do_loc is TRUE then we are scanning for location lists and dwo tags
+   and we do not want to display anything to the user.
+   If do_types is TRUE, we are processing a .debug_types section instead of
+   a .debug_info section.
+   The information displayed is restricted by the values in DWARF_START_DIE
+   and DWARF_CUTOFF_LEVEL.
+   Returns TRUE upon success.  Otherwise an error or warning message is
+   printed and FALSE is returned.  */
 
-static int
-process_debug_info (struct dwarf_section *section,
-                   void *file,
-                   enum dwarf_section_display_enum abbrev_sec,
-                   int do_loc,
-                   int do_types)
+static bfd_boolean
+process_debug_info (struct dwarf_section *           section,
+                   void *                           file,
+                   enum dwarf_section_display_enum  abbrev_sec,
+                   bfd_boolean                      do_loc,
+                   bfd_boolean                      do_types)
 {
   unsigned char *start = section->start;
   unsigned char *end = start + section->size;
@@ -2684,7 +2818,7 @@ process_debug_info (struct dwarf_section *section,
            {
              warn (_("Reserved length value (0x%s) found in section %s\n"),
                    dwarf_vmatoa ("x", length), section->name);
-             return 0;
+             return FALSE;
            }
          else
            section_begin += length + 4;
@@ -2696,14 +2830,14 @@ process_debug_info (struct dwarf_section *section,
            {
              warn (_("Corrupt unit length (0x%s) found in section %s\n"),
                    dwarf_vmatoa ("x", length), section->name);
-             return 0;
+             return FALSE;
            }
        }
 
       if (num_units == 0)
        {
          error (_("No comp units in %s section ?\n"), section->name);
-         return 0;
+         return FALSE;
        }
 
       /* Then allocate an array to hold the information.  */
@@ -2714,8 +2848,9 @@ process_debug_info (struct dwarf_section *section,
          error (_("Not enough memory for a debug info array of %u entries\n"),
                 num_units);
          alloc_num_debug_info_entries = num_debug_info_entries = 0;
-         return 0;
+         return FALSE;
        }
+
       /* PR 17531: file: 92ca3797.
         We cannot rely upon the debug_information array being initialised
         before it is used.  A corrupt file could easily contain references
@@ -2741,7 +2876,7 @@ process_debug_info (struct dwarf_section *section,
     {
       warn (_("Unable to locate %s section!\n"),
            debug_displays [abbrev_sec].section.uncompressed_name);
-      return 0;
+      return FALSE;
     }
 
   if (!do_loc && dwarf_start_die == 0)
@@ -3008,7 +3143,7 @@ process_debug_info (struct dwarf_section *section,
                    }
                }
              if (dwarf_start_die != 0 && level < saved_level)
-               return 1;
+               return TRUE;
              continue;
            }
 
@@ -3048,7 +3183,7 @@ process_debug_info (struct dwarf_section *section,
                }
              warn (_("DIE at offset 0x%lx refers to abbreviation number %lu which does not exist\n"),
                    die_offset, abbrev_number);
-             return 0;
+             return FALSE;
            }
 
          if (!do_loc && do_printing)
@@ -3060,7 +3195,8 @@ process_debug_info (struct dwarf_section *section,
              need_base_address = 0;
              break;
            case DW_TAG_compile_unit:
-             need_base_address = 1;
+             need_base_address = 1;    
+             need_dwo_info = do_loc;
              break;
            case DW_TAG_entry_point:
            case DW_TAG_subprogram:
@@ -3146,7 +3282,7 @@ process_debug_info (struct dwarf_section *section,
   if (!do_loc)
     printf ("\n");
 
-  return 1;
+  return TRUE;
 }
 
 /* Locate and scan the .debug_info section in the file and record the pointer
@@ -3171,12 +3307,12 @@ load_debug_info (void * file)
   (void) load_cu_tu_indexes (file);
 
   if (load_debug_section_with_follow (info, file)
-      && process_debug_info (&debug_displays [info].section, file, abbrev, 1, 0))
+      && process_debug_info (&debug_displays [info].section, file, abbrev, TRUE, FALSE))
     return num_debug_info_entries;
 
   if (load_debug_section_with_follow (info_dwo, file)
       && process_debug_info (&debug_displays [info_dwo].section, file,
-                            abbrev_dwo, 1, 0))
+                            abbrev_dwo, TRUE, FALSE))
     return num_debug_info_entries;
 
   num_debug_info_entries = DEBUG_INFO_UNAVAILABLE;
@@ -3307,7 +3443,7 @@ display_formatted_table (unsigned char *                   data,
                         unsigned char *                   end,
                         const DWARF2_Internal_LineInfo *  linfo,
                         struct dwarf_section *            section,
-                        const char *                      what)
+                        bfd_boolean                       is_dir)
 {
   unsigned char *format_start, format_count, *format, formati;
   dwarf_vma data_count, datai;
@@ -3323,7 +3459,10 @@ display_formatted_table (unsigned char *                   data,
       data += bytes_read;
       if (data == end)
        {
-         warn (_("Corrupt %s format table entry\n"), what);
+         if (is_dir)
+           warn (_("Corrupt directory format table entry\n"));
+         else
+           warn (_("Corrupt file name format table entry\n"));
          return data;
        }
     }
@@ -3332,18 +3471,28 @@ display_formatted_table (unsigned char *                   data,
   data += bytes_read;
   if (data == end)
     {
-      warn (_("Corrupt %s list\n"), what);
+      if (is_dir)
+       warn (_("Corrupt directory list\n"));
+      else
+       warn (_("Corrupt file name list\n"));
       return data;
     }
 
   if (data_count == 0)
     {
-      printf (_("\n The %s Table is empty.\n"), what);
+      if (is_dir)
+       printf (_("\n The Directory Table is empty.\n"));
+      else
+       printf (_("\n The File Name Table is empty.\n"));
       return data;
     }
 
-  printf (_("\n The %s Table (offset 0x%lx):\n"), what,
-         (long)(data - start));
+  if (is_dir)
+    printf (_("\n The Directory Table (offset 0x%lx):\n"),
+           (long) (data - start));
+  else
+    printf (_("\n The File Name Table (offset 0x%lx):\n"),
+           (long) (data - start));
 
   printf (_("  Entry"));
   /* Delay displaying name as the last entry for better screen layout.  */ 
@@ -3411,7 +3560,10 @@ display_formatted_table (unsigned char *                   data,
        }
       if (data == end)
        {
-         warn (_("Corrupt %s entries list\n"), what);
+         if (is_dir)
+           warn (_("Corrupt directory entries list\n"));
+         else
+           warn (_("Corrupt file name entries list\n"));
          return data;
        }
       putchar ('\n');
@@ -3519,9 +3671,9 @@ display_debug_lines_raw (struct dwarf_section *  section,
              load_debug_section_with_follow (line_str, file);
 
              data = display_formatted_table (data, start, end, &linfo, section,
-                                             _("Directory"));
+                                             TRUE);
              data = display_formatted_table (data, start, end, &linfo, section,
-                                             _("File name"));
+                                             FALSE);
            }
          else
            {
@@ -4205,7 +4357,7 @@ display_debug_lines_decoded (struct dwarf_section *  section,
                printf ("%s:\n", file_table[0].name);
            }
 
-         printf (_("File name                            Line number    Starting address    View\n"));
+         printf (_("File name                            Line number    Starting address    View    Stmt\n"));
          saved_linfo = linfo;
        }
 
@@ -4543,9 +4695,14 @@ display_debug_lines_decoded (struct dwarf_section *  section,
                }
 
              if (state_machine_regs.view)
-               printf ("  %6u\n", state_machine_regs.view);
+               printf ("  %6u", state_machine_regs.view);
              else
-               putchar ('\n');
+               printf ("        ");
+
+             if (state_machine_regs.is_stmt)
+               printf ("       x");
+
+             putchar ('\n');
              state_machine_regs.view++;
 
              if (xop == -DW_LNE_end_sequence)
@@ -6161,19 +6318,19 @@ display_debug_str (struct dwarf_section *section,
 static int
 display_debug_info (struct dwarf_section *section, void *file)
 {
-  return process_debug_info (section, file, section->abbrev_sec, 0, 0);
+  return process_debug_info (section, file, section->abbrev_sec, FALSE, FALSE);
 }
 
 static int
 display_debug_types (struct dwarf_section *section, void *file)
 {
-  return process_debug_info (section, file, section->abbrev_sec, 0, 1);
+  return process_debug_info (section, file, section->abbrev_sec, FALSE, TRUE);
 }
 
 static int
 display_trace_info (struct dwarf_section *section, void *file)
 {
-  return process_debug_info (section, file, section->abbrev_sec, 0, 0);
+  return process_debug_info (section, file, section->abbrev_sec, FALSE, TRUE);
 }
 
 static int
@@ -6451,6 +6608,7 @@ display_debug_ranges_list (unsigned char *start, unsigned char *finish,
        break;
       SAFE_SIGNED_BYTE_GET_AND_INC (end, start, pointer_size, finish);
 
+      
       printf ("    %8.8lx ", offset);
 
       if (begin == 0 && end == 0)
@@ -6718,6 +6876,13 @@ display_debug_ranges (struct dwarf_section *section,
          continue;
        }
 
+      if (next < section_begin || next >= finish)
+       {
+         warn (_("Corrupt offset (%#8.8lx) in range entry %u\n"),
+               (unsigned long) offset, i);
+         continue;
+       }
+
       if (dwarf_check != 0 && i > 0)
        {
          if (start < next)
@@ -6733,6 +6898,7 @@ display_debug_ranges (struct dwarf_section *section,
                    (unsigned long) (next - section_begin), section->name);
            }
        }
+
       start = next;
       last_start = next;
 
@@ -6973,6 +7139,27 @@ init_dwarf_regnames_s390 (void)
   dwarf_regnames_count = ARRAY_SIZE (dwarf_regnames_s390);
 }
 
+static const char *const dwarf_regnames_riscv[] =
+{
+ "zero", "ra",   "sp",   "gp",  "tp",  "t0",  "t1",  "t2",  /* 0  - 7 */
+ "s0",   "s1",   "a0",   "a1",  "a2",  "a3",  "a4",  "a5",  /* 8  - 15 */
+ "a6",   "a7",   "s2",   "s3",  "s4",  "s5",  "s6",  "s7",  /* 16 - 23 */
+ "s8",   "s9",   "s10",  "s11", "t3",  "t4",  "t5",  "t6",  /* 24 - 31 */
+ "ft0",  "ft1",  "ft2",  "ft3", "ft4", "ft5", "ft6", "ft7", /* 32 - 39 */
+ "fs0",  "fs1",                                             /* 40 - 41 */
+ "fa0",  "fa1",  "fa2",  "fa3", "fa4", "fa5", "fa6", "fa7", /* 42 - 49 */
+ "fs2",  "fs3",  "fs4",  "fs5", "fs6", "fs7", "fs8", "fs9", /* 50 - 57 */
+ "fs10", "fs11",                                            /* 58 - 59 */
+ "ft8",  "ft9",  "ft10", "ft11"                             /* 60 - 63 */
+};
+
+void
+init_dwarf_regnames_riscv (void)
+{
+  dwarf_regnames = dwarf_regnames_riscv;
+  dwarf_regnames_count = ARRAY_SIZE (dwarf_regnames_riscv);
+}
+
 void
 init_dwarf_regnames (unsigned int e_machine)
 {
@@ -7000,6 +7187,10 @@ init_dwarf_regnames (unsigned int e_machine)
       init_dwarf_regnames_s390 ();
       break;
 
+    case EM_RISCV:
+      init_dwarf_regnames_riscv ();
+      break;
+
     default:
       break;
     }
@@ -7132,7 +7323,7 @@ read_cie (unsigned char *start, unsigned char *end,
   if (start == end)
     {
       warn (_("No terminator for augmentation name\n"));
-      return start;
+      goto fail;
     }
 
   if (strcmp (fc->augmentation, "eh") == 0)
@@ -7144,7 +7335,7 @@ read_cie (unsigned char *start, unsigned char *end,
       if (fc->ptr_size < 1 || fc->ptr_size > 8)
        {
          warn (_("Invalid pointer size (%d) in CIE data\n"), fc->ptr_size);
-         return end;
+         goto fail;
        }
 
       GET (fc->segment_size, 1);
@@ -7152,7 +7343,7 @@ read_cie (unsigned char *start, unsigned char *end,
       if (fc->segment_size > 8 || fc->segment_size + fc->ptr_size > 8)
        {
          warn (_("Invalid segment size (%d) in CIE data\n"), fc->segment_size);
-         return end;
+         goto fail;
        }
 
       eh_addr_size = fc->ptr_size;
@@ -7162,8 +7353,10 @@ read_cie (unsigned char *start, unsigned char *end,
       fc->ptr_size = eh_addr_size;
       fc->segment_size = 0;
     }
+
   READ_ULEB (fc->code_factor);
   READ_SLEB (fc->data_factor);
+
   if (version == 1)
     {
       GET (fc->ra, 1);
@@ -7183,7 +7376,7 @@ read_cie (unsigned char *start, unsigned char *end,
          warn (_("Augmentation data too long: 0x%s, expected at most %#lx\n"),
                dwarf_vmatoa ("x", augmentation_data_len),
                (unsigned long) (end - start));
-         return end;
+         goto fail;
        }
       start += augmentation_data_len;
     }
@@ -7208,6 +7401,8 @@ read_cie (unsigned char *start, unsigned char *end,
            fc->fde_encoding = *q++;
          else if (*p == 'S')
            ;
+         else if (*p == 'B')
+           ;
          else
            break;
          p++;
@@ -7225,31 +7420,48 @@ read_cie (unsigned char *start, unsigned char *end,
       *p_aug = augmentation_data;
     }
   return start;
+
+ fail:
+  free (fc->col_offset);
+  free (fc->col_type);
+  free (fc);
+  return end;
 }
 
-/* Prints out the contents on the augmentation data array.
-   If do_wide is not enabled, then formats the output to fit into 80 columns.  */
+/* Prints out the contents on the DATA array formatted as unsigned bytes.
+   If do_wide is not enabled, then formats the output to fit into 80 columns.
+   PRINTED contains the number of characters already written to the current
+   output line.  */
 
 static void
-display_augmentation_data (const unsigned char * data, const bfd_size_type len)
+display_data (bfd_size_type          printed,
+             const unsigned char *  data,
+             const bfd_size_type    len)
 {
-  bfd_size_type i;
-
-  i = printf (_("  Augmentation data:    "));
-
-  if (do_wide || len < ((80 - i) / 3))
-    for (i = 0; i < len; ++i)
-      printf (" %02x", data[i]);
+  if (do_wide || len < ((80 - printed) / 3))
+    for (printed = 0; printed < len; ++printed)
+      printf (" %02x", data[printed]);
   else
     {
-      for (i = 0; i < len; ++i)
+      for (printed = 0; printed < len; ++printed)
        {
-         if (i % (80 / 3) == 0)
+         if (printed % (80 / 3) == 0)
            putchar ('\n');
-         printf (" %02x", data[i]);
+         printf (" %02x", data[printed]);
        }
     }
-  putchar ('\n');
+}
+
+/* Prints out the contents on the augmentation data array.
+   If do_wide is not enabled, then formats the output to fit into 80 columns.  */
+
+static void
+display_augmentation_data (const unsigned char * data, const bfd_size_type len)
+{
+  bfd_size_type i;
+
+  i = printf (_("  Augmentation data:    "));
+  display_data (i, data, len);
 }
 
 static int
@@ -7259,10 +7471,10 @@ display_debug_frames (struct dwarf_section *section,
   unsigned char *start = section->start;
   unsigned char *end = start + section->size;
   unsigned char *section_start = start;
-  Frame_Chunk *chunks = 0, *forward_refs = 0;
-  Frame_Chunk *remembered_state = 0;
+  Frame_Chunk *chunks = NULL, *forward_refs = NULL;
+  Frame_Chunk *remembered_state = NULL;
   Frame_Chunk *rs;
-  int is_eh = strcmp (section->name, ".eh_frame") == 0;
+  bfd_boolean is_eh = strcmp (section->name, ".eh_frame") == 0;
   unsigned int length_return;
   unsigned int max_regs = 0;
   const char *bad_reg = _("bad register: ");
@@ -8195,6 +8407,36 @@ display_debug_frames (struct dwarf_section *section,
 
   printf ("\n");
 
+  while (remembered_state != NULL)
+    {
+      rs = remembered_state;
+      remembered_state = rs->next;
+      free (rs->col_type);
+      free (rs->col_offset);
+      rs->next = NULL; /* Paranoia.  */
+      free (rs);
+    }
+
+  while (chunks != NULL)
+    {
+      rs = chunks;
+      chunks = rs->next;
+      free (rs->col_type);
+      free (rs->col_offset);
+      rs->next = NULL; /* Paranoia.  */
+      free (rs);
+    }
+
+  while (forward_refs != NULL)
+    {
+      rs = forward_refs;
+      forward_refs = rs->next;
+      free (rs->col_type);
+      free (rs->col_offset);
+      rs->next = NULL; /* Paranoia.  */
+      free (rs);
+    }
+
   return 1;
 }
 
@@ -8550,8 +8792,8 @@ display_debug_names (struct dwarf_section *section, void *file)
 }
 
 static int
-display_debug_links (struct dwarf_section * section,
-                    void * file ATTRIBUTE_UNUSED)
+display_debug_links (struct dwarf_section *  section,
+                    void *                  file ATTRIBUTE_UNUSED)
 {
   const unsigned char * filename;
   unsigned int          filelen;
@@ -8615,19 +8857,7 @@ display_debug_links (struct dwarf_section * section,
        }
 
       printed = printf (_("  Build-ID (%#lx bytes):"), (long) build_id_len);
-
-      if (do_wide || build_id_len < ((80 - printed) / 3))
-       for (printed = 0; printed < build_id_len; ++printed)
-         printf (" %02x", build_id[printed]);
-      else
-       {
-         for (printed = 0; printed < build_id_len; ++printed)
-           {
-             if (printed % (80 / 3) == 0)
-               putchar ('\n');
-             printf (" %02x", build_id[printed]);
-           }
-       }
+      display_data (printed, build_id, build_id_len);
       putchar ('\n');
     }
 
@@ -9146,7 +9376,18 @@ process_cu_tu_index (struct dwarf_section *section, int do_display)
                }
 
              if (!do_display)
-               memcpy (&this_set[row - 1].signature, ph, sizeof (uint64_t));
+               {
+                 size_t num_copy = sizeof (uint64_t);
+
+                 /* PR 23064: Beware of buffer overflow.  */
+                 if (ph + num_copy < limit)
+                   memcpy (&this_set[row - 1].signature, ph, num_copy);
+                 else
+                   {
+                     warn (_("Signature (%p) extends beyond end of space in section\n"), ph);
+                     return 0;
+                   }
+               }
 
              prow = poffsets + (row - 1) * ncols * 4;
              /* PR 17531: file: b8ce60a8.  */
@@ -9572,7 +9813,7 @@ parse_gnu_debugaltlink (struct dwarf_section * section, void * data)
 
 static void *
 load_separate_debug_info (const char *            main_filename,
-                         struct dwarf_section *  link,
+                         struct dwarf_section *  xlink,
                          parse_func_type         parse_func,
                          check_func_type         check_func,
                          void *                  func_data)
@@ -9583,10 +9824,10 @@ load_separate_debug_info (const char *            main_filename,
   size_t         canon_dirlen;
   size_t         dirlen;
 
-  if ((separate_filename = parse_func (link, func_data)) == NULL)
+  if ((separate_filename = parse_func (xlink, func_data)) == NULL)
     {
       warn (_("Corrupt debuglink section: %s\n"),
-           link->name ? link->name : link->uncompressed_name);
+           xlink->name ? xlink->name : xlink->uncompressed_name);
       return FALSE;
     }
     
@@ -9624,6 +9865,7 @@ load_separate_debug_info (const char *            main_filename,
   if (debugfile == NULL)
     {
       warn (_("Out of memory"));
+      free (canon_dir);
       return NULL;
     }
 
@@ -9723,21 +9965,90 @@ load_separate_debug_info (const char *            main_filename,
   return separate_debug_file;
 }
 
+/* Attempt to load a separate dwarf object file.  */
+
+static void *
+load_dwo_file (const char * main_filename)
+{
+  char * filename;
+
+  /* FIXME: Skip adding / if dwo_dir ends in /.  */
+  filename = concat (dwo_dir, "/", dwo_name, NULL);
+  if (filename == NULL)
+    {
+      warn (_("Out of memory allocating dwo filename\n"));
+      return NULL;
+    }
+
+  if ((separate_debug_file = open_debug_file (filename)) == NULL)
+    {
+      warn (_("Unable to load dwo file: %s\n"), filename);
+      free (filename);
+      return NULL;
+    }
+
+  /* FIXME: We should check the dwo_id.  */
+
+  printf (_("%s: Found separate debug object file: %s\n\n"), main_filename, filename);
+  separate_debug_filename = filename;
+  return separate_debug_file;
+}
+
 /* Load a separate debug info file, if it exists.
    Returns the data pointer that is the result of calling open_debug_file
-   on the separate debug info file.  */
+   on the separate debug info file, or NULL if there were problems or there
+   is no such file.  */
 
 void *
 load_separate_debug_file (void * file, const char * filename)
 {
+  /* Skip this operation if we are not interested in debug links.  */
+  if (! do_follow_links && ! do_debug_links)
+    return NULL;
+
+  /* See if there is a dwo link.  */
+  if (load_debug_section (str, file)
+      && load_debug_section (abbrev, file)
+      && load_debug_section (info, file))
+    {
+      dwo_name = dwo_dir = NULL;
+      dwo_id = NULL;
+      dwo_id_len = 0;
+
+      if (process_debug_info (& debug_displays[info].section, file, abbrev, TRUE, FALSE))
+       {
+         if (dwo_name != NULL)
+           {
+             if (do_debug_links)
+               {
+                 printf (_("The %s section contains a link to a dwo file:\n"),
+                         debug_displays [info].section.uncompressed_name);
+                 printf (_("  Name:      %s\n"), dwo_name);
+                 printf (_("  Directory: %s\n"), dwo_dir ? dwo_dir : _("<not-found>"));
+                 if (dwo_id != NULL)
+                   display_data (printf (_("  ID:       ")), dwo_id, dwo_id_len);
+                 else
+                   printf (_("  ID: <unknown>\n"));
+                 printf ("\n\n");
+               }
+
+             /* FIXME: We do not check to see if there are any more dwo links in the file...  */
+             if (do_follow_links)
+               return load_dwo_file (filename);
+           }
+       }
+    }
+
   if (! do_follow_links)
+    /* The other debug links will be displayed by display_debug_links()
+       so we do not need to do any further processing here.  */
     return NULL;
 
   /* FIXME: We do not check for the presence of both link sections in the same file.  */
   /* FIXME: We do not check the separate debug info file to see if it too contains debuglinks.  */
   /* FIXME: We do not check for the presence of multiple, same-name debuglink sections.  */
+  /* FIXME: We do not check for the presence of a dwo link as well as a debuglink.  */
 
-  /* We try the alt version first as that is blessed by the DWARF5 standard.  */
   if (load_debug_section (gnu_debugaltlink, file))
     {
       Build_id_data * build_id_data;
This page took 0.037911 seconds and 4 git commands to generate.