Automatic date update in version.in
[binutils-gdb.git] / bfd / peXXigen.c
index 5f977ea32dce48843604e8d789ac1de72e380679..50e4face50c8f5aa9c00ed7c35e937696d605ec2 100644 (file)
@@ -1,5 +1,5 @@
 /* Support for the generic parts of PE/PEI; the common executable parts.
-   Copyright (C) 1995-2021 Free Software Foundation, Inc.
+   Copyright (C) 1995-2022 Free Software Foundation, Inc.
    Written by Cygnus Solutions.
 
    This file is part of BFD, the Binary File Descriptor library.
@@ -2937,6 +2937,7 @@ bool
 _bfd_XX_bfd_copy_private_bfd_data_common (bfd * ibfd, bfd * obfd)
 {
   pe_data_type *ipe, *ope;
+  bfd_size_type size;
 
   /* One day we may try to grok other private data.  */
   if (ibfd->xvec->flavour != bfd_target_coff_flavour
@@ -2971,7 +2972,8 @@ _bfd_XX_bfd_copy_private_bfd_data_common (bfd * ibfd, bfd * obfd)
   memcpy (ope->dos_message, ipe->dos_message, sizeof (ope->dos_message));
 
   /* The file offsets contained in the debug directory need rewriting.  */
-  if (ope->pe_opthdr.DataDirectory[PE_DEBUG_DATA].Size != 0)
+  size = ope->pe_opthdr.DataDirectory[PE_DEBUG_DATA].Size;
+  if (size != 0)
     {
       bfd_vma addr = ope->pe_opthdr.DataDirectory[PE_DEBUG_DATA].VirtualAddress
        + ope->pe_opthdr.ImageBase;
@@ -2980,62 +2982,74 @@ _bfd_XX_bfd_copy_private_bfd_data_common (bfd * ibfd, bfd * obfd)
         representing s_size, not virt_size).  Therefore don't look for the
         section containing the first byte, but for that covering the last
         one.  */
-      bfd_vma last = addr + ope->pe_opthdr.DataDirectory[PE_DEBUG_DATA].Size - 1;
+      bfd_vma last = addr + size - 1;
       asection *section = find_section_by_vma (obfd, last);
-      bfd_byte *data;
 
-      /* PR 17512: file: 0f15796a.  */
-      if (section && addr < section->vma)
+      if (section != NULL)
        {
-         /* xgettext:c-format */
-         _bfd_error_handler
-           (_("%pB: Data Directory (%lx bytes at %" PRIx64 ") "
-              "extends across section boundary at %" PRIx64),
-            obfd, ope->pe_opthdr.DataDirectory[PE_DEBUG_DATA].Size,
-            (uint64_t) addr, (uint64_t) section->vma);
-         return false;
-       }
+         bfd_byte *data;
+         bfd_vma dataoff = addr - section->vma;
 
-      if (section && bfd_malloc_and_get_section (obfd, section, &data))
-       {
-         unsigned int i;
-         struct external_IMAGE_DEBUG_DIRECTORY *dd =
-           (struct external_IMAGE_DEBUG_DIRECTORY *)(data + (addr - section->vma));
+         /* PR 17512: file: 0f15796a.  */
+         if (addr < section->vma
+             || section->size < dataoff
+             || section->size - dataoff < size)
+           {
+             /* xgettext:c-format */
+             _bfd_error_handler
+               (_("%pB: Data Directory (%lx bytes at %" PRIx64 ") "
+                  "extends across section boundary at %" PRIx64),
+                obfd, ope->pe_opthdr.DataDirectory[PE_DEBUG_DATA].Size,
+                (uint64_t) addr, (uint64_t) section->vma);
+             return false;
+           }
 
-         for (i = 0; i < ope->pe_opthdr.DataDirectory[PE_DEBUG_DATA].Size
-                / sizeof (struct external_IMAGE_DEBUG_DIRECTORY); i++)
+         if (bfd_malloc_and_get_section (obfd, section, &data))
            {
-             asection *ddsection;
-             struct external_IMAGE_DEBUG_DIRECTORY *edd = &(dd[i]);
-             struct internal_IMAGE_DEBUG_DIRECTORY idd;
+             unsigned int i;
+             struct external_IMAGE_DEBUG_DIRECTORY *dd =
+               (struct external_IMAGE_DEBUG_DIRECTORY *)(data + dataoff);
+
+             for (i = 0; i < ope->pe_opthdr.DataDirectory[PE_DEBUG_DATA].Size
+                    / sizeof (struct external_IMAGE_DEBUG_DIRECTORY); i++)
+               {
+                 asection *ddsection;
+                 struct external_IMAGE_DEBUG_DIRECTORY *edd = &(dd[i]);
+                 struct internal_IMAGE_DEBUG_DIRECTORY idd;
+                 bfd_vma idd_vma;
 
-             _bfd_XXi_swap_debugdir_in (obfd, edd, &idd);
+                 _bfd_XXi_swap_debugdir_in (obfd, edd, &idd);
 
-             if (idd.AddressOfRawData == 0)
-               continue; /* RVA 0 means only offset is valid, not handled yet.  */
+                 /* RVA 0 means only offset is valid, not handled yet.  */
+                 if (idd.AddressOfRawData == 0)
+                   continue;
 
-             ddsection = find_section_by_vma (obfd, idd.AddressOfRawData + ope->pe_opthdr.ImageBase);
-             if (!ddsection)
-               continue; /* Not in a section! */
+                 idd_vma = idd.AddressOfRawData + ope->pe_opthdr.ImageBase;
+                 ddsection = find_section_by_vma (obfd, idd_vma);
+                 if (!ddsection)
+                   continue; /* Not in a section! */
 
-             idd.PointerToRawData = ddsection->filepos + (idd.AddressOfRawData
-                                                          + ope->pe_opthdr.ImageBase) - ddsection->vma;
+                 idd.PointerToRawData
+                   = ddsection->filepos + idd_vma - ddsection->vma;
+                 _bfd_XXi_swap_debugdir_out (obfd, &idd, edd);
+               }
 
-             _bfd_XXi_swap_debugdir_out (obfd, &idd, edd);
+             if (!bfd_set_section_contents (obfd, section, data, 0,
+                                            section->size))
+               {
+                 _bfd_error_handler (_("failed to update file offsets"
+                                       " in debug directory"));
+                 free (data);
+                 return false;
+               }
+             free (data);
            }
-
-         if (!bfd_set_section_contents (obfd, section, data, 0, section->size))
+         else
            {
-             _bfd_error_handler (_("failed to update file offsets in debug directory"));
-             free (data);
+             _bfd_error_handler (_("%pB: failed to read "
+                                   "debug data section"), obfd);
              return false;
            }
-         free (data);
-       }
-      else if (section)
-       {
-         _bfd_error_handler (_("%pB: failed to read debug data section"), obfd);
-         return false;
        }
     }
 
@@ -3088,7 +3102,7 @@ _bfd_XX_get_symbol_info (bfd * abfd, asymbol *symbol, symbol_info *ret)
   coff_get_symbol_info (abfd, symbol, ret);
 }
 
-#if !defined(COFF_WITH_pep) && defined(COFF_WITH_pex64) && defined(COFF_WITH_peAArch64)
+#if !defined(COFF_WITH_pep) && (defined(COFF_WITH_pex64) || defined(COFF_WITH_peAArch64))
 static int
 sort_x64_pdata (const void *l, const void *r)
 {
@@ -4516,7 +4530,7 @@ _bfd_XXi_final_link_postscript (bfd * abfd, struct coff_final_link_info *pfinfo)
 
 /* If there is a .pdata section and we have linked pdata finally, we
      need to sort the entries ascending.  */
-#if !defined(COFF_WITH_pep) && defined(COFF_WITH_pex64) && defined(COFF_WITH_peAArch64)
+#if !defined(COFF_WITH_pep) && (defined(COFF_WITH_pex64) || defined(COFF_WITH_peAArch64))
   {
     asection *sec = bfd_get_section_by_name (abfd, ".pdata");