]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blobdiff - binutils/readelf.c
Harden gdb.base/bp-permanent.exp
[thirdparty/binutils-gdb.git] / binutils / readelf.c
index b3f4733ed2e3b6ae5ea04329c67e3daeff7d2983..ca251360d00f76cbd608bf3ab298c91c453ee424 100644 (file)
@@ -43,9 +43,7 @@
 #include "sysdep.h"
 #include <assert.h>
 #include <time.h>
-#ifdef HAVE_ZLIB_H
 #include <zlib.h>
-#endif
 #ifdef HAVE_WCHAR_H
 #include <wchar.h>
 #endif
@@ -552,7 +550,7 @@ print_symbol (int width, const char *symbol)
    to print multibyte characters, it just interprets them as hex values.  */
 
 static const char *
-printable_section_name (Elf_Internal_Shdr * sec)
+printable_section_name (const Elf_Internal_Shdr * sec)
 {
 #define MAX_PRINT_SEC_NAME_LEN 128
   static char  sec_name_buf [MAX_PRINT_SEC_NAME_LEN + 1];
@@ -2280,12 +2278,6 @@ decode_ARM_machine_flags (unsigned e_flags, char buf[])
       e_flags &= ~ EF_ARM_RELEXEC;
     }
 
-  if (e_flags & EF_ARM_HASENTRY)
-    {
-      strcat (buf, ", has entry point");
-      e_flags &= ~ EF_ARM_HASENTRY;
-    }
-
   /* Now handle EABI specific flags.  */
   switch (eabi)
     {
@@ -2952,12 +2944,8 @@ get_machine_flags (unsigned e_flags, unsigned e_machine)
                {
                case EF_RH850_FPU_DOUBLE: strcat (buf, ", double precision FPU"); break;
                case EF_RH850_FPU_SINGLE: strcat (buf, ", single precision FPU"); break;
-               case EF_RH850_SIMD: strcat (buf, ", SIMD"); break;
-               case EF_RH850_CACHE: strcat (buf, ", CACHE"); break;
-               case EF_RH850_MMU: strcat (buf, ", MMU"); break;
                case EF_RH850_REGMODE22: strcat (buf, ", regmode:22"); break;
                case EF_RH850_REGMODE32: strcat (buf, ", regmode:23"); break;
-               case EF_RH850_DATA_ALIGN8: strcat (buf, ", 8-byte alignment"); break;
                case EF_RH850_GP_FIX: strcat (buf, ", r4 fixed"); break;
                case EF_RH850_GP_NOFIX: strcat (buf, ", r4 free"); break;
                case EF_RH850_EP_FIX: strcat (buf, ", r30 fixed"); break;
@@ -3266,8 +3254,13 @@ get_machine_flags (unsigned e_flags, unsigned e_machine)
          break;
 
        case EM_RL78:
-         if (e_flags & E_FLAG_RL78_G10)
-           strcat (buf, ", G10");
+         switch (e_flags & E_FLAG_RL78_CPU_MASK)
+           {
+           case E_FLAG_RL78_ANY_CPU: break;
+           case E_FLAG_RL78_G10: strcat (buf, ", G10"); break;
+           case E_FLAG_RL78_G13: strcat (buf, ", G13"); break;
+           case E_FLAG_RL78_G14: strcat (buf, ", G14"); break;
+           }
          if (e_flags & E_FLAG_RL78_64BIT_DOUBLES)
            strcat (buf, ", 64-bit doubles");
          break;
@@ -3281,6 +3274,9 @@ get_machine_flags (unsigned e_flags, unsigned e_machine)
            strcat (buf, ", pid");
          if (e_flags & E_FLAG_RX_ABI)
            strcat (buf, ", RX ABI");
+         if (e_flags & E_FLAG_RX_SINSNS_SET)
+           strcat (buf, e_flags & E_FLAG_RX_SINSNS_YES
+                   ? ", uses String instructions" : ", bans String instructions");
          break;
 
        case EM_S390:
@@ -3758,6 +3754,20 @@ get_msp430x_section_type_name (unsigned int sh_type)
     }
 }
 
+static const char *
+get_v850_section_type_name (unsigned int sh_type)
+{
+  switch (sh_type)
+    {
+    case SHT_V850_SCOMMON: return "V850 Small Common";
+    case SHT_V850_TCOMMON: return "V850 Tiny Common";
+    case SHT_V850_ZCOMMON: return "V850 Zero Common";
+    case SHT_RENESAS_IOP:  return "RENESAS IOP";
+    case SHT_RENESAS_INFO: return "RENESAS INFO";
+    default: return NULL;
+    }
+}
+
 static const char *
 get_section_type_name (unsigned int sh_type)
 {
@@ -3826,6 +3836,11 @@ get_section_type_name (unsigned int sh_type)
            case EM_MSP430:
              result = get_msp430x_section_type_name (sh_type);
              break;
+           case EM_V800:
+           case EM_V850:
+           case EM_CYGNUS_V850:
+             result = get_v850_section_type_name (sh_type);
+             break;
            default:
              result = NULL;
              break;
@@ -3856,7 +3871,19 @@ get_section_type_name (unsigned int sh_type)
          sprintf (buff, "LOOS+%x", sh_type - SHT_LOOS);
        }
       else if ((sh_type >= SHT_LOUSER) && (sh_type <= SHT_HIUSER))
-       sprintf (buff, "LOUSER+%x", sh_type - SHT_LOUSER);
+       {
+         switch (elf_header.e_machine)
+           {
+           case EM_V800:
+           case EM_V850:
+           case EM_CYGNUS_V850:
+             return get_v850_section_type_name (sh_type);
+           default:
+             break;
+           }
+
+         sprintf (buff, "LOUSER+%x", sh_type - SHT_LOUSER);
+       }
       else
        /* This message is probably going to be displayed in a 15
           character wide field, so put the hex value first.  */
@@ -4211,11 +4238,6 @@ parse_args (int argc, char ** argv)
       && !do_section_groups && !do_archive_index
       && !do_dyn_syms)
     usage (stderr);
-  else if (argc < 3)
-    {
-      warn (_("Nothing to do.\n"));
-      usage (stderr);
-    }
 }
 
 static const char *
@@ -5136,7 +5158,8 @@ get_elf_section_flags (bfd_vma sh_flags)
       /* Generic.  */
       /* 18 */ { STRING_COMMA_LEN ("EXCLUDE") },
       /* SPARC specific.  */
-      /* 19 */ { STRING_COMMA_LEN ("ORDERED") }
+      /* 19 */ { STRING_COMMA_LEN ("ORDERED") },
+      /* 20 */ { STRING_COMMA_LEN ("COMPRESSED") }
     };
 
   if (do_section_details)
@@ -5168,6 +5191,7 @@ get_elf_section_flags (bfd_vma sh_flags)
            case SHF_GROUP:             sindex = 8; break;
            case SHF_TLS:               sindex = 9; break;
            case SHF_EXCLUDE:           sindex = 18; break;
+           case SHF_COMPRESSED:        sindex = 20; break;
 
            default:
              sindex = -1;
@@ -5249,6 +5273,7 @@ get_elf_section_flags (bfd_vma sh_flags)
            case SHF_GROUP:             *p = 'G'; break;
            case SHF_TLS:               *p = 'T'; break;
            case SHF_EXCLUDE:           *p = 'E'; break;
+           case SHF_COMPRESSED:        *p = 'C'; break;
 
            default:
              if ((elf_header.e_machine == EM_X86_64
@@ -5336,6 +5361,27 @@ get_elf_section_flags (bfd_vma sh_flags)
   return buff;
 }
 
+static unsigned int
+get_compression_header (Elf_Internal_Chdr *chdr, unsigned char *buf)
+{
+  if (is_32bit_elf)
+    {
+      Elf32_External_Chdr *echdr = (Elf32_External_Chdr *) buf;
+      chdr->ch_type = BYTE_GET (echdr->ch_type);
+      chdr->ch_size = BYTE_GET (echdr->ch_size);
+      chdr->ch_addralign = BYTE_GET (echdr->ch_addralign);
+      return sizeof (*echdr);
+    }
+  else
+    {
+      Elf64_External_Chdr *echdr = (Elf64_External_Chdr *) buf;
+      chdr->ch_type = BYTE_GET (echdr->ch_type);
+      chdr->ch_size = BYTE_GET (echdr->ch_size);
+      chdr->ch_addralign = BYTE_GET (echdr->ch_addralign);
+      return sizeof (*echdr);
+    }
+}
+
 static int
 process_section_headers (FILE * file)
 {
@@ -5780,7 +5826,29 @@ process_section_headers (FILE * file)
        }
 
       if (do_section_details)
-       printf ("       %s\n", get_elf_section_flags (section->sh_flags));
+       {
+         printf ("       %s\n", get_elf_section_flags (section->sh_flags));
+         if ((section->sh_flags & SHF_COMPRESSED) != 0)
+           {
+             /* Minimum section size is 12 bytes for 32-bit compression
+                header + 12 bytes for compressed data header.  */
+             unsigned char buf[24];
+             assert (sizeof (buf) >= sizeof (Elf64_External_Chdr));
+             if (get_data (&buf, (FILE *) file, section->sh_offset, 1,
+                           sizeof (buf), _("compression header")))
+               {
+                 Elf_Internal_Chdr chdr;
+                 get_compression_header (&chdr, buf);
+                 if (chdr.ch_type == ELFCOMPRESS_ZLIB)
+                   printf ("       ZLIB, ");
+                 else
+                   printf (_("       [<unknown>: 0x%x], "),
+                           chdr.ch_type);
+                 print_vma (chdr.ch_size, LONG_HEX);
+                 printf (", %lu\n", (unsigned long) chdr.ch_addralign);
+               }
+           }
+       }
     }
 
   if (!do_section_details)
@@ -11565,11 +11633,11 @@ is_none_reloc (unsigned int reloc_type)
 
 static void
 apply_relocations (void * file,
-                  Elf_Internal_Shdr * section,
-                  unsigned char * start)
+                  const Elf_Internal_Shdr * section,
+                  unsigned char * start, bfd_size_type size)
 {
   Elf_Internal_Shdr * relsec;
-  unsigned char * end = start + section->sh_size;
+  unsigned char * end = start + size;
 
   if (elf_header.e_type != ET_REL)
     return;
@@ -11861,7 +11929,7 @@ dump_section_as_bytes (Elf_Internal_Shdr * section,
 
   if (relocate)
     {
-      apply_relocations (file, section, start);
+      apply_relocations (file, section, start, section->sh_size);
     }
   else
     {
@@ -11935,12 +12003,9 @@ dump_section_as_bytes (Elf_Internal_Shdr * section,
 /* Uncompresses a section that was compressed using zlib, in place.  */
 
 static int
-uncompress_section_contents (unsigned char **buffer ATTRIBUTE_UNUSED,
-                            dwarf_size_type *size ATTRIBUTE_UNUSED)
+uncompress_section_contents (unsigned char **buffer,
+                            dwarf_size_type *size)
 {
-#ifndef HAVE_ZLIB_H
-  return FALSE;
-#else
   dwarf_size_type compressed_size = *size;
   unsigned char * compressed_buffer = *buffer;
   dwarf_size_type uncompressed_size;
@@ -11991,7 +12056,6 @@ uncompress_section_contents (unsigned char **buffer ATTRIBUTE_UNUSED,
       || strm.avail_out != 0)
     goto fail;
 
-  free (compressed_buffer);
   *buffer = uncompressed_buffer;
   *size = uncompressed_size;
   return 1;
@@ -12001,12 +12065,11 @@ uncompress_section_contents (unsigned char **buffer ATTRIBUTE_UNUSED,
   /* Indicate decompression failure.  */
   *buffer = NULL;
   return 0;
-#endif  /* HAVE_ZLIB_H */
 }
 
 static int
 load_specific_debug_section (enum dwarf_section_display_enum debug,
-                            Elf_Internal_Shdr * sec, void * file)
+                            const Elf_Internal_Shdr * sec, void * file)
 {
   struct dwarf_section * section = &debug_displays [debug].section;
   char buf [64];
@@ -12025,16 +12088,36 @@ load_specific_debug_section (enum dwarf_section_display_enum debug,
     section->size = 0;
   else
     {
-      section->size = sec->sh_size;
-      if (uncompress_section_contents (&section->start, &section->size))
-       sec->sh_size = section->size;
+      unsigned char *start = section->start;
+      dwarf_size_type size = sec->sh_size;
+
+      if ((sec->sh_flags & SHF_COMPRESSED) != 0)
+       {
+         Elf_Internal_Chdr chdr;
+         unsigned int compression_header_size
+           = get_compression_header (&chdr, start);
+         if (chdr.ch_type != ELFCOMPRESS_ZLIB
+             || chdr.ch_addralign != sec->sh_addralign)
+           return 0;
+         start += compression_header_size;
+         size -= compression_header_size;
+       }
+
+      if (uncompress_section_contents (&start, &size))
+       {
+         /* Free the compressed buffer, update the section buffer
+            and the section size if uncompress is successful.  */
+         free (section->start);
+         section->start = start;
+       }
+      section->size = size;
     }
 
   if (section->start == NULL)
     return 0;
 
   if (debug_displays [debug].relocate)
-    apply_relocations ((FILE *) file, sec, section->start);
+    apply_relocations ((FILE *) file, sec, section->start, section->size);
 
   return 1;
 }
@@ -12349,7 +12432,7 @@ static const char * arm_attr_tag_ABI_FP_number_model[] =
 static const char * arm_attr_tag_ABI_enum_size[] =
   {"Unused", "small", "int", "forced to int"};
 static const char * arm_attr_tag_ABI_HardFP_use[] =
-  {"As Tag_FP_arch", "SP only", "DP only", "SP and DP"};
+  {"As Tag_FP_arch", "SP only", "Reserved", "Deprecated"};
 static const char * arm_attr_tag_ABI_VFP_args[] =
   {"AAPCS", "VFP registers", "custom", "compatible"};
 static const char * arm_attr_tag_ABI_WMMX_args[] =
@@ -14807,6 +14890,85 @@ print_gnu_note (Elf_Internal_Note *pnote)
   return 1;
 }
 
+static const char *
+get_v850_elf_note_type (enum v850_notes n_type)
+{
+  static char buff[64];
+
+  switch (n_type)
+    {
+    case V850_NOTE_ALIGNMENT:  return _("Alignment of 8-byte objects");
+    case V850_NOTE_DATA_SIZE:  return _("Sizeof double and long double");
+    case V850_NOTE_FPU_INFO:   return _("Type of FPU support needed");
+    case V850_NOTE_SIMD_INFO:  return _("Use of SIMD instructions");
+    case V850_NOTE_CACHE_INFO: return _("Use of cache");
+    case V850_NOTE_MMU_INFO:   return _("Use of MMU");
+    default:
+      snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), n_type);
+      return buff;
+    }
+}
+
+static int
+print_v850_note (Elf_Internal_Note * pnote)
+{
+  unsigned int val;
+
+  if (pnote->descsz != 4)
+    return 0;
+  val = byte_get ((unsigned char *) pnote->descdata, pnote->descsz);
+
+  if (val == 0)
+    {
+      printf (_("not set\n"));
+      return 1;
+    }
+
+  switch (pnote->type)
+    {
+    case V850_NOTE_ALIGNMENT:
+      switch (val)
+       {
+       case EF_RH850_DATA_ALIGN4: printf (_("4-byte\n")); return 1;
+       case EF_RH850_DATA_ALIGN8: printf (_("8-byte\n")); return 1;
+       }
+      break;
+       
+    case V850_NOTE_DATA_SIZE:
+      switch (val)
+       {
+       case EF_RH850_DOUBLE32: printf (_("4-bytes\n")); return 1;
+       case EF_RH850_DOUBLE64: printf (_("8-bytes\n")); return 1;
+       }
+      break;
+       
+    case V850_NOTE_FPU_INFO:
+      switch (val)
+       {
+       case EF_RH850_FPU20: printf (_("FPU-2.0\n")); return 1;
+       case EF_RH850_FPU30: printf (_("FPU-3.0\n")); return 1;
+       }
+      break;
+       
+    case V850_NOTE_MMU_INFO:
+    case V850_NOTE_CACHE_INFO:
+    case V850_NOTE_SIMD_INFO:
+      if (val == EF_RH850_SIMD)
+       {
+         printf (_("yes\n"));
+         return 1;
+       }
+      break;
+
+    default:
+      /* An 'unknown note type' message will already have been displayed.  */
+      break;
+    }
+
+  printf (_("unknown value: %x\n"), val);
+  return 0;
+}
+
 static const char *
 get_netbsd_elfcore_note_type (unsigned e_type)
 {
@@ -15099,6 +15261,7 @@ process_corefile_note_segment (FILE * file, bfd_vma offset, bfd_vma length)
 {
   Elf_External_Note * pnotes;
   Elf_External_Note * external;
+  char * end;
   int res = 1;
 
   if (length <= 0)
@@ -15115,13 +15278,14 @@ process_corefile_note_segment (FILE * file, bfd_vma offset, bfd_vma length)
          (unsigned long) offset, (unsigned long) length);
   printf (_("  %-20s %10s\tDescription\n"), _("Owner"), _("Data size"));
 
-  while ((char *) external < (char *) pnotes + length)
+  end = (char *) pnotes + length;
+  while ((char *) external < end)
     {
       Elf_Internal_Note inote;
       size_t min_notesz;
       char *next;
       char * temp = NULL;
-      size_t data_remaining = ((char *) pnotes + length) - (char *) external;
+      size_t data_remaining = end - (char *) external;
 
       if (!is_ia64_vms ())
        {
@@ -15140,12 +15304,13 @@ process_corefile_note_segment (FILE * file, bfd_vma offset, bfd_vma length)
          inote.descsz   = BYTE_GET (external->descsz);
          inote.descdata = inote.namedata + align_power (inote.namesz, 2);
          /* PR 17531: file: 3443835e.  */
-         if (inote.descdata < (char *) pnotes)
+         if (inote.descdata < (char *) pnotes || inote.descdata > end)
            {
              warn (_("Corrupt note: name size is too big: %lx\n"), inote.namesz);
              inote.descdata = inote.namedata;
              inote.namesz   = 0;
            }
          inote.descpos  = offset + (inote.descdata - (char *) pnotes);
          next = inote.descdata + align_power (inote.descsz, 2);
        }
@@ -15247,6 +15412,87 @@ process_corefile_note_segments (FILE * file)
   return res;
 }
 
+static int
+process_v850_notes (FILE * file, bfd_vma offset, bfd_vma length)
+{
+  Elf_External_Note * pnotes;
+  Elf_External_Note * external;
+  char * end;
+  int res = 1;
+
+  if (length <= 0)
+    return 0;
+
+  pnotes = (Elf_External_Note *) get_data (NULL, file, offset, 1, length,
+                                           _("v850 notes"));
+  if (pnotes == NULL)
+    return 0;
+
+  external = pnotes;
+  end = (char*) pnotes + length;
+
+  printf (_("\nDisplaying contents of Renesas V850 notes section at offset 0x%lx with length 0x%lx:\n"),
+         (unsigned long) offset, (unsigned long) length);
+
+  while ((char *) external + sizeof (Elf_External_Note) < end)
+    {
+      Elf_External_Note * next;
+      Elf_Internal_Note inote;
+
+      inote.type     = BYTE_GET (external->type);
+      inote.namesz   = BYTE_GET (external->namesz);
+      inote.namedata = external->name;
+      inote.descsz   = BYTE_GET (external->descsz);
+      inote.descdata = inote.namedata + align_power (inote.namesz, 2);
+      inote.descpos  = offset + (inote.descdata - (char *) pnotes);
+
+      if (inote.descdata < (char *) pnotes || inote.descdata >= end)
+       {
+         warn (_("Corrupt note: name size is too big: %lx\n"), inote.namesz);
+         inote.descdata = inote.namedata;
+         inote.namesz   = 0;
+       }
+
+      next = (Elf_External_Note *) (inote.descdata + align_power (inote.descsz, 2));
+
+      if (   ((char *) next > end)
+         || ((char *) next <  (char *) pnotes))
+       {
+         warn (_("corrupt descsz found in note at offset 0x%lx\n"),
+               (unsigned long) ((char *) external - (char *) pnotes));
+         warn (_(" type: 0x%lx, namesize: 0x%lx, descsize: 0x%lx\n"),
+               inote.type, inote.namesz, inote.descsz);
+         break;
+       }
+
+      external = next;
+
+      /* Prevent out-of-bounds indexing.  */
+      if (   inote.namedata + inote.namesz > end
+         || inote.namedata + inote.namesz < inote.namedata)
+        {
+          warn (_("corrupt namesz found in note at offset 0x%lx\n"),
+                (unsigned long) ((char *) external - (char *) pnotes));
+          warn (_(" type: 0x%lx, namesize: 0x%lx, descsize: 0x%lx\n"),
+                inote.type, inote.namesz, inote.descsz);
+          break;
+        }
+
+      printf ("  %s: ", get_v850_elf_note_type (inote.type));
+
+      if (! print_v850_note (& inote))
+       {
+         res = 0;
+         printf ("<corrupt sizes: namesz: %lx, descsz: %lx>\n",
+                 inote.namesz, inote.descsz);
+       }
+    }
+
+  free (pnotes);
+
+  return res;
+}
+
 static int
 process_note_sections (FILE * file)
 {
@@ -15258,13 +15504,26 @@ process_note_sections (FILE * file)
   for (i = 0, section = section_headers;
        i < elf_header.e_shnum && section != NULL;
        i++, section++)
-    if (section->sh_type == SHT_NOTE)
-      {
-       res &= process_corefile_note_segment (file,
-                                             (bfd_vma) section->sh_offset,
-                                             (bfd_vma) section->sh_size);
-       n++;
-      }
+    {
+      if (section->sh_type == SHT_NOTE)
+       {
+         res &= process_corefile_note_segment (file,
+                                               (bfd_vma) section->sh_offset,
+                                               (bfd_vma) section->sh_size);
+         n++;
+       }
+
+      if ((   elf_header.e_machine == EM_V800
+          || elf_header.e_machine == EM_V850
+          || elf_header.e_machine == EM_CYGNUS_V850)
+         && section->sh_type == SHT_RENESAS_INFO)
+       {
+         res &= process_v850_notes (file,
+                                    (bfd_vma) section->sh_offset,
+                                    (bfd_vma) section->sh_size);
+         n++;
+       }
+    }
 
   if (n == 0)
     /* Try processing NOTE segments instead.  */
@@ -15934,6 +16193,11 @@ main (int argc, char ** argv)
 
   if (optind < (argc - 1))
     show_name = 1;
+  else if (optind >= argc)
+    {
+      warn (_("Nothing to do.\n"));
+      usage (stderr);
+    }
 
   err = 0;
   while (optind < argc)