]> 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 adf2a919990ce75521775d2edcc2d1995ba949bb..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];
@@ -3276,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:
@@ -5157,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)
@@ -5189,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;
@@ -5270,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
@@ -5357,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)
 {
@@ -5801,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)
@@ -11586,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;
@@ -11882,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
     {
@@ -11956,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;
@@ -12012,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;
@@ -12022,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];
@@ -12046,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;
 }