]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blobdiff - bfd/peXXigen.c
2.41 Release sources
[thirdparty/binutils-gdb.git] / bfd / peXXigen.c
index 5ab09387e72a357a6aee7fa5a4ef335c931a9d0c..da53f349dd03792978e15a99393f62b9874d806e 100644 (file)
@@ -1,5 +1,5 @@
 /* Support for the generic parts of PE/PEI; the common executable parts.
-   Copyright (C) 1995-2022 Free Software Foundation, Inc.
+   Copyright (C) 1995-2023 Free Software Foundation, Inc.
    Written by Cygnus Solutions.
 
    This file is part of BFD, the Binary File Descriptor library.
@@ -60,9 +60,9 @@
    on this code has a chance of getting something accomplished without
    wasting too much time.  */
 
-/* This expands into COFF_WITH_pe, COFF_WITH_pep, COFF_WITH_pex64 or
-   COFF_WITH_peAArch64 depending on whether we're compiling for straight
-   PE or PE+.  */
+/* This expands into COFF_WITH_pe, COFF_WITH_pep, COFF_WITH_pex64,
+   COFF_WITH_peAArch64 or COFF_WITH_peLoongArch64 depending on whether we're
+   compiling for straight PE or PE+.  */
 #define COFF_WITH_XX
 
 #include "sysdep.h"
@@ -86,6 +86,8 @@
 # include "coff/ia64.h"
 #elif defined COFF_WITH_peAArch64
 # include "coff/aarch64.h"
+#elif defined COFF_WITH_peLoongArch64
+# include "coff/loongarch64.h"
 #else
 # include "coff/i386.h"
 #endif
@@ -95,7 +97,7 @@
 #include "libpei.h"
 #include "safe-ctype.h"
 
-#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 || defined COFF_WITH_peLoongArch64
 # undef AOUTSZ
 # define AOUTSZ                PEPAOUTSZ
 # define PEAOUTHDR     PEPAOUTHDR
@@ -187,7 +189,8 @@ _bfd_XXi_swap_sym_in (bfd * abfd, void * ext1, void * in1)
            }
          memcpy (sec_name, name, name_len);
 
-         flags = SEC_HAS_CONTENTS | SEC_ALLOC | SEC_DATA | SEC_LOAD;
+         flags = (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_DATA | SEC_LOAD
+                  | SEC_LINKER_CREATED);
          sec = bfd_make_section_anyway_with_flags (abfd, sec_name, flags);
          if (sec == NULL)
            {
@@ -196,18 +199,7 @@ _bfd_XXi_swap_sym_in (bfd * abfd, void * ext1, void * in1)
              return;
            }
 
-         sec->vma = 0;
-         sec->lma = 0;
-         sec->size = 0;
-         sec->filepos = 0;
-         sec->rel_filepos = 0;
-         sec->reloc_count = 0;
-         sec->line_filepos = 0;
-         sec->lineno_count = 0;
-         sec->userdata = NULL;
-         sec->next = NULL;
          sec->alignment_power = 2;
-
          sec->target_index = unused_section_number;
 
          in->n_scnum = unused_section_number;
@@ -324,14 +316,14 @@ _bfd_XXi_swap_aux_in (bfd *       abfd,
       break;
     }
 
-  in->x_sym.x_tagndx.l = H_GET_32 (abfd, ext->x_sym.x_tagndx);
+  in->x_sym.x_tagndx.u32 = H_GET_32 (abfd, ext->x_sym.x_tagndx);
   in->x_sym.x_tvndx = H_GET_16 (abfd, ext->x_sym.x_tvndx);
 
   if (in_class == C_BLOCK || in_class == C_FCN || ISFCN (type)
       || ISTAG (in_class))
     {
       in->x_sym.x_fcnary.x_fcn.x_lnnoptr = GET_FCN_LNNOPTR (abfd, ext);
-      in->x_sym.x_fcnary.x_fcn.x_endndx.l = GET_FCN_ENDNDX (abfd, ext);
+      in->x_sym.x_fcnary.x_fcn.x_endndx.u32 = GET_FCN_ENDNDX (abfd, ext);
     }
   else
     {
@@ -399,14 +391,14 @@ _bfd_XXi_swap_aux_out (bfd *  abfd,
       break;
     }
 
-  H_PUT_32 (abfd, in->x_sym.x_tagndx.l, ext->x_sym.x_tagndx);
+  H_PUT_32 (abfd, in->x_sym.x_tagndx.u32, ext->x_sym.x_tagndx);
   H_PUT_16 (abfd, in->x_sym.x_tvndx, ext->x_sym.x_tvndx);
 
   if (in_class == C_BLOCK || in_class == C_FCN || ISFCN (type)
       || ISTAG (in_class))
     {
       PUT_FCN_LNNOPTR (abfd, in->x_sym.x_fcnary.x_fcn.x_lnnoptr,  ext);
-      PUT_FCN_ENDNDX  (abfd, in->x_sym.x_fcnary.x_fcn.x_endndx.l, ext);
+      PUT_FCN_ENDNDX  (abfd, in->x_sym.x_fcnary.x_fcn.x_endndx.u32, ext);
     }
   else
     {
@@ -472,7 +464,7 @@ _bfd_XXi_swap_aouthdr_in (bfd * abfd,
   aouthdr_int->text_start =
     GET_AOUTHDR_TEXT_START (abfd, aouthdr_ext->text_start);
 
-#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) && !defined(COFF_WITH_peLoongArch64)
   /* PE32+ does not have data_start member!  */
   aouthdr_int->data_start =
     GET_AOUTHDR_DATA_START (abfd, aouthdr_ext->data_start);
@@ -515,50 +507,31 @@ _bfd_XXi_swap_aouthdr_in (bfd * abfd,
   a->LoaderFlags = H_GET_32 (abfd, src->LoaderFlags);
   a->NumberOfRvaAndSizes = H_GET_32 (abfd, src->NumberOfRvaAndSizes);
 
-  {
-    unsigned idx;
-
-    /* PR 17512: Corrupt PE binaries can cause seg-faults.  */
-    if (a->NumberOfRvaAndSizes > IMAGE_NUMBEROF_DIRECTORY_ENTRIES)
-      {
-       /* xgettext:c-format */
-       _bfd_error_handler
-         (_("%pB: aout header specifies an invalid number of"
-            " data-directory entries: %u"), abfd, a->NumberOfRvaAndSizes);
-       bfd_set_error (bfd_error_bad_value);
-
-       /* Paranoia: If the number is corrupt, then assume that the
-          actual entries themselves might be corrupt as well.  */
-       a->NumberOfRvaAndSizes = 0;
-      }
-
-    for (idx = 0; idx < a->NumberOfRvaAndSizes; idx++)
-      {
-       /* If data directory is empty, rva also should be 0.  */
-       int size =
-         H_GET_32 (abfd, src->DataDirectory[idx][1]);
-
-       a->DataDirectory[idx].Size = size;
+  /* PR 17512: Don't blindly trust NumberOfRvaAndSizes.  */
+  unsigned idx;
+  for (idx = 0;
+       idx < a->NumberOfRvaAndSizes && idx < IMAGE_NUMBEROF_DIRECTORY_ENTRIES;
+       idx++)
+    {
+      /* If data directory is empty, rva also should be 0.  */
+      int size = H_GET_32 (abfd, src->DataDirectory[idx][1]);
+      int vma = size ? H_GET_32 (abfd, src->DataDirectory[idx][0]) : 0;
 
-       if (size)
-         a->DataDirectory[idx].VirtualAddress =
-           H_GET_32 (abfd, src->DataDirectory[idx][0]);
-       else
-         a->DataDirectory[idx].VirtualAddress = 0;
-      }
+      a->DataDirectory[idx].Size = size;
+      a->DataDirectory[idx].VirtualAddress = vma;
+    }
 
-    while (idx < IMAGE_NUMBEROF_DIRECTORY_ENTRIES)
-      {
-       a->DataDirectory[idx].Size = 0;
-       a->DataDirectory[idx].VirtualAddress = 0;
-       idx ++;
-      }
-  }
+  while (idx < IMAGE_NUMBEROF_DIRECTORY_ENTRIES)
+    {
+      a->DataDirectory[idx].Size = 0;
+      a->DataDirectory[idx].VirtualAddress = 0;
+      idx++;
+    }
 
   if (aouthdr_int->entry)
     {
       aouthdr_int->entry += a->ImageBase;
-#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) && !defined(COFF_WITH_peLoongArch64)
       aouthdr_int->entry &= 0xffffffff;
 #endif
     }
@@ -566,12 +539,12 @@ _bfd_XXi_swap_aouthdr_in (bfd * abfd,
   if (aouthdr_int->tsize)
     {
       aouthdr_int->text_start += a->ImageBase;
-#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) && !defined(COFF_WITH_peLoongArch64)
       aouthdr_int->text_start &= 0xffffffff;
 #endif
     }
 
-#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) && !defined(COFF_WITH_peLoongArch64)
   /* PE32+ does not have data_start member!  */
   if (aouthdr_int->dsize)
     {
@@ -631,7 +604,7 @@ _bfd_XXi_swap_aouthdr_out (bfd * abfd, void * in, void * out)
   if (aouthdr_in->tsize)
     {
       aouthdr_in->text_start -= ib;
-#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) && !defined(COFF_WITH_peLoongArch64)
       aouthdr_in->text_start &= 0xffffffff;
 #endif
     }
@@ -639,7 +612,7 @@ _bfd_XXi_swap_aouthdr_out (bfd * abfd, void * in, void * out)
   if (aouthdr_in->dsize)
     {
       aouthdr_in->data_start -= ib;
-#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) && !defined(COFF_WITH_peLoongArch64)
       aouthdr_in->data_start &= 0xffffffff;
 #endif
     }
@@ -647,7 +620,7 @@ _bfd_XXi_swap_aouthdr_out (bfd * abfd, void * in, void * out)
   if (aouthdr_in->entry)
     {
       aouthdr_in->entry -= ib;
-#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) && !defined(COFF_WITH_peLoongArch64)
       aouthdr_in->entry &= 0xffffffff;
 #endif
     }
@@ -761,7 +734,7 @@ _bfd_XXi_swap_aouthdr_out (bfd * abfd, void * in, void * out)
   PUT_AOUTHDR_TEXT_START (abfd, aouthdr_in->text_start,
                          aouthdr_out->standard.text_start);
 
-#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) && !defined(COFF_WITH_peLoongArch64)
   /* PE32+ does not have data_start member!  */
   PUT_AOUTHDR_DATA_START (abfd, aouthdr_in->data_start,
                          aouthdr_out->standard.data_start);
@@ -946,9 +919,14 @@ _bfd_XXi_swap_scnhdr_out (bfd * abfd, void * in, void * out)
   if (scnhdr_int->s_vaddr < pe_data (abfd)->pe_opthdr.ImageBase)
     _bfd_error_handler (_("%pB:%.8s: section below image base"),
                         abfd, scnhdr_int->s_name);
+  /* Do not compare lower 32-bits for 64-bit vma.  */
+#if !defined(COFF_WITH_pex64) && !defined(COFF_WITH_peAArch64) && !defined(COFF_WITH_peLoongArch64)
   else if(ss != (ss & 0xffffffff))
     _bfd_error_handler (_("%pB:%.8s: RVA truncated"), abfd, scnhdr_int->s_name);
   PUT_SCNHDR_VADDR (abfd, ss & 0xffffffff, scnhdr_ext->s_vaddr);
+#else
+  PUT_SCNHDR_VADDR (abfd, ss, scnhdr_ext->s_vaddr);
+#endif
 
   /* NT wants the size data to be rounded up to the next
      NT_FILE_ALIGNMENT, but zero if it has no content (as in .bss,
@@ -1021,7 +999,7 @@ _bfd_XXi_swap_scnhdr_out (bfd * abfd, void * in, void * out)
        { ".pdata", IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_INITIALIZED_DATA },
        { ".rdata", IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_INITIALIZED_DATA },
        { ".reloc", IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_DISCARDABLE },
-       { ".rsrc",  IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_WRITE },
+       { ".rsrc",  IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_INITIALIZED_DATA },
        { ".text" , IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_CODE | IMAGE_SCN_MEM_EXECUTE },
        { ".tls",   IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_WRITE },
        { ".xdata", IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_INITIALIZED_DATA },
@@ -1134,7 +1112,8 @@ _bfd_XXi_swap_debugdir_out (bfd * abfd, void * inp, void * extp)
 }
 
 CODEVIEW_INFO *
-_bfd_XXi_slurp_codeview_record (bfd * abfd, file_ptr where, unsigned long length, CODEVIEW_INFO *cvinfo)
+_bfd_XXi_slurp_codeview_record (bfd * abfd, file_ptr where, unsigned long length, CODEVIEW_INFO *cvinfo,
+                               char **pdb)
 {
   char buffer[256+1];
   bfd_size_type nread;
@@ -1174,6 +1153,9 @@ _bfd_XXi_slurp_codeview_record (bfd * abfd, file_ptr where, unsigned long length
       cvinfo->SignatureLength = CV_INFO_SIGNATURE_LENGTH;
       /* cvinfo->PdbFileName = cvinfo70->PdbFileName;  */
 
+      if (pdb)
+       *pdb = xstrdup (cvinfo70->PdbFileName);
+
       return cvinfo;
     }
   else if ((cvinfo->CVSignature == CVINFO_PDB20_CVSIGNATURE)
@@ -1185,6 +1167,9 @@ _bfd_XXi_slurp_codeview_record (bfd * abfd, file_ptr where, unsigned long length
       cvinfo->SignatureLength = 4;
       /* cvinfo->PdbFileName = cvinfo20->PdbFileName;  */
 
+      if (pdb)
+       *pdb = xstrdup (cvinfo20->PdbFileName);
+
       return cvinfo;
     }
 
@@ -1192,9 +1177,11 @@ _bfd_XXi_slurp_codeview_record (bfd * abfd, file_ptr where, unsigned long length
 }
 
 unsigned int
-_bfd_XXi_write_codeview_record (bfd * abfd, file_ptr where, CODEVIEW_INFO *cvinfo)
+_bfd_XXi_write_codeview_record (bfd * abfd, file_ptr where, CODEVIEW_INFO *cvinfo,
+                               const char *pdb)
 {
-  const bfd_size_type size = sizeof (CV_INFO_PDB70) + 1;
+  size_t pdb_len = pdb ? strlen (pdb) : 0;
+  const bfd_size_type size = sizeof (CV_INFO_PDB70) + pdb_len + 1;
   bfd_size_type written;
   CV_INFO_PDB70 *cvinfo70;
   char * buffer;
@@ -1217,7 +1204,11 @@ _bfd_XXi_write_codeview_record (bfd * abfd, file_ptr where, CODEVIEW_INFO *cvinf
   memcpy (&(cvinfo70->Signature[8]), &(cvinfo->Signature[8]), 8);
 
   H_PUT_32 (abfd, cvinfo->Age, cvinfo70->Age);
-  cvinfo70->PdbFileName[0] = '\0';
+
+  if (pdb == NULL)
+    cvinfo70->PdbFileName[0] = '\0';
+  else
+    memcpy (cvinfo70->PdbFileName, pdb, pdb_len + 1);
 
   written = bfd_bwrite (buffer, size, abfd);
 
@@ -1246,6 +1237,24 @@ static char * dir_names[IMAGE_NUMBEROF_DIRECTORY_ENTRIES] =
   N_("Reserved")
 };
 
+static bool
+get_contents_sanity_check (bfd *abfd, asection *section,
+                          bfd_size_type dataoff, bfd_size_type datasize)
+{
+  if ((section->flags & SEC_HAS_CONTENTS) == 0)
+    return false;
+  if (dataoff > section->size
+      || datasize > section->size - dataoff)
+    return false;
+  ufile_ptr filesize = bfd_get_file_size (abfd);
+  if (filesize != 0
+      && ((ufile_ptr) section->filepos > filesize
+         || dataoff > filesize - section->filepos
+         || datasize > filesize - section->filepos - dataoff))
+    return false;
+  return true;
+}
+
 static bool
 pe_print_idata (bfd * abfd, void * vfile)
 {
@@ -1269,7 +1278,7 @@ pe_print_idata (bfd * abfd, void * vfile)
     {
       /* Maybe the extra header isn't there.  Look for the section.  */
       section = bfd_get_section_by_name (abfd, ".idata");
-      if (section == NULL)
+      if (section == NULL || (section->flags & SEC_HAS_CONTENTS) == 0)
        return true;
 
       addr = section->vma;
@@ -1412,6 +1421,9 @@ pe_print_idata (bfd * abfd, void * vfile)
                {
                  ft_idx = first_thunk - (ft_section->vma - extra->ImageBase);
                  ft_datasize = ft_section->size - ft_idx;
+                 if (!get_contents_sanity_check (abfd, ft_section,
+                                                 ft_idx, ft_datasize))
+                   continue;
                  ft_data = (bfd_byte *) bfd_malloc (ft_datasize);
                  if (ft_data == NULL)
                    continue;
@@ -1581,24 +1593,9 @@ pe_print_edata (bfd * abfd, void * vfile)
                   _("\nThere is an export table, but the section containing it could not be found\n"));
          return true;
        }
-      else if (!(section->flags & SEC_HAS_CONTENTS))
-       {
-         fprintf (file,
-                  _("\nThere is an export table in %s, but that section has no contents\n"),
-                  section->name);
-         return true;
-       }
 
       dataoff = addr - section->vma;
       datasize = extra->DataDirectory[PE_EXPORT_TABLE].Size;
-      if (dataoff > section->size
-         || datasize > section->size - dataoff)
-       {
-         fprintf (file,
-                  _("\nThere is an export table in %s, but it does not fit into that section\n"),
-                  section->name);
-         return true;
-       }
     }
 
   /* PR 17512: Handle corrupt PE binaries.  */
@@ -1611,6 +1608,14 @@ pe_print_edata (bfd * abfd, void * vfile)
       return true;
     }
 
+  if (!get_contents_sanity_check (abfd, section, dataoff, datasize))
+    {
+      fprintf (file,
+              _("\nThere is an export table in %s, but contents cannot be read\n"),
+              section->name);
+      return true;
+    }
+
   /* xgettext:c-format */
   fprintf (file, _("\nThere is an export table in %s at 0x%lx\n"),
           section->name, (unsigned long) addr);
@@ -1621,7 +1626,10 @@ pe_print_edata (bfd * abfd, void * vfile)
 
   if (! bfd_get_section_contents (abfd, section, data,
                                  (file_ptr) dataoff, datasize))
-    return false;
+    {
+      free (data);
+      return false;
+    }
 
   /* Go get Export Directory Table.  */
   edt.export_flags   = bfd_get_32 (abfd, data +         0);
@@ -1813,7 +1821,7 @@ pe_print_edata (bfd * abfd, void * vfile)
 static bool
 pe_print_pdata (bfd * abfd, void * vfile)
 {
-#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) && !defined(COFF_WITH_peLoongArch64)
 # define PDATA_ROW_SIZE        (3 * 8)
 #else
 # define PDATA_ROW_SIZE        (5 * 4)
@@ -1827,6 +1835,7 @@ pe_print_pdata (bfd * abfd, void * vfile)
   int onaline = PDATA_ROW_SIZE;
 
   if (section == NULL
+      || (section->flags & SEC_HAS_CONTENTS) == 0
       || coff_section_data (abfd, section) == NULL
       || pei_section_data (abfd, section) == NULL)
     return true;
@@ -1840,7 +1849,7 @@ pe_print_pdata (bfd * abfd, void * vfile)
 
   fprintf (file,
           _("\nThe Function Table (interpreted .pdata section contents)\n"));
-#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) && !defined(COFF_WITH_peLoongArch64)
   fprintf (file,
           _(" vma:\t\t\tBegin Address    End Address      Unwind Info\n"));
 #else
@@ -1877,7 +1886,7 @@ pe_print_pdata (bfd * abfd, void * vfile)
       bfd_vma eh_handler;
       bfd_vma eh_data;
       bfd_vma prolog_end_addr;
-#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) || defined(COFF_WITH_peLoongArch64)
       int em_data;
 #endif
 
@@ -1895,7 +1904,7 @@ pe_print_pdata (bfd * abfd, void * vfile)
        /* We are probably into the padding of the section now.  */
        break;
 
-#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) || defined(COFF_WITH_peLoongArch64)
       em_data = ((eh_handler & 0x1) << 2) | (prolog_end_addr & 0x3);
 #endif
       eh_handler &= ~(bfd_vma) 0x3;
@@ -1906,7 +1915,7 @@ pe_print_pdata (bfd * abfd, void * vfile)
       bfd_fprintf_vma (abfd, file, begin_addr); fputc (' ', file);
       bfd_fprintf_vma (abfd, file, end_addr); fputc (' ', file);
       bfd_fprintf_vma (abfd, file, eh_handler);
-#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) || defined(COFF_WITH_peLoongArch64)
       fputc (' ', file);
       bfd_fprintf_vma (abfd, file, eh_data); fputc (' ', file);
       bfd_fprintf_vma (abfd, file, prolog_end_addr);
@@ -1996,6 +2005,7 @@ _bfd_XX_print_ce_compressed_pdata (bfd * abfd, void * vfile)
   struct sym_cache cache = {0, 0} ;
 
   if (section == NULL
+      || (section->flags & SEC_HAS_CONTENTS) == 0
       || coff_section_data (abfd, section) == NULL
       || pei_section_data (abfd, section) == NULL)
     return true;
@@ -2025,6 +2035,8 @@ _bfd_XX_print_ce_compressed_pdata (bfd * abfd, void * vfile)
     }
 
   start = 0;
+  if (stop > datasize)
+    stop = datasize;
 
   for (i = start; i < stop; i += onaline)
     {
@@ -2127,7 +2139,9 @@ pe_print_reloc (bfd * abfd, void * vfile)
   asection *section = bfd_get_section_by_name (abfd, ".reloc");
   bfd_byte *p, *end;
 
-  if (section == NULL || section->size == 0 || !(section->flags & SEC_HAS_CONTENTS))
+  if (section == NULL
+      || section->size == 0
+      || (section->flags & SEC_HAS_CONTENTS) == 0)
     return true;
 
   fprintf (file,
@@ -2615,22 +2629,25 @@ pe_print_debugdata (bfd * abfd, void * vfile)
             We need to use a 32-bit aligned buffer
             to safely read in a codeview record.  */
          char buffer[256 + 1] ATTRIBUTE_ALIGNED_ALIGNOF (CODEVIEW_INFO);
+         char *pdb;
 
          CODEVIEW_INFO *cvinfo = (CODEVIEW_INFO *) buffer;
 
          /* The debug entry doesn't have to have to be in a section,
             in which case AddressOfRawData is 0, so always use PointerToRawData.  */
          if (!_bfd_XXi_slurp_codeview_record (abfd, (file_ptr) idd.PointerToRawData,
-                                              idd.SizeOfData, cvinfo))
+                                              idd.SizeOfData, cvinfo, &pdb))
            continue;
 
          for (j = 0; j < cvinfo->SignatureLength; j++)
            sprintf (&signature[j*2], "%02x", cvinfo->Signature[j] & 0xff);
 
          /* xgettext:c-format */
-         fprintf (file, _("(format %c%c%c%c signature %s age %ld)\n"),
+         fprintf (file, _("(format %c%c%c%c signature %s age %ld pdb %s)\n"),
                   buffer[0], buffer[1], buffer[2], buffer[3],
-                  signature, cvinfo->Age);
+                  signature, cvinfo->Age, pdb[0] ? pdb : "(none)");
+
+         free (pdb);
        }
     }
 
@@ -2797,7 +2814,7 @@ _bfd_XX_print_private_bfd_data_common (bfd * abfd, void * vfile)
   bfd_fprintf_vma (abfd, file, i->AddressOfEntryPoint);
   fprintf (file, "\nBaseOfCode\t\t");
   bfd_fprintf_vma (abfd, file, i->BaseOfCode);
-#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) && !defined(COFF_WITH_peLoongArch64)
   /* PE32+ does not have BaseOfData member!  */
   fprintf (file, "\nBaseOfData\t\t");
   bfd_fprintf_vma (abfd, file, i->BaseOfData);
@@ -3014,7 +3031,8 @@ _bfd_XX_bfd_copy_private_bfd_data_common (bfd * ibfd, bfd * obfd)
              return false;
            }
 
-         if (bfd_malloc_and_get_section (obfd, section, &data))
+         if ((section->flags & SEC_HAS_CONTENTS) != 0
+             && bfd_malloc_and_get_section (obfd, section, &data))
            {
              unsigned int i;
              struct external_IMAGE_DEBUG_DIRECTORY *dd =
@@ -3112,7 +3130,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) || defined(COFF_WITH_peLoongArch64))
 static int
 sort_x64_pdata (const void *l, const void *r)
 {
@@ -3327,7 +3345,7 @@ rsrc_parse_entry (bfd *abfd,
   if (HighBitSet (val))
     {
       entry->is_dir = true;
-      entry->value.directory = bfd_malloc (sizeof * entry->value.directory);
+      entry->value.directory = bfd_malloc (sizeof (*entry->value.directory));
       if (entry->value.directory == NULL)
        return dataend;
 
@@ -3338,12 +3356,12 @@ rsrc_parse_entry (bfd *abfd,
     }
 
   entry->is_dir = false;
-  entry->value.leaf = bfd_malloc (sizeof * entry->value.leaf);
+  entry->value.leaf = bfd_malloc (sizeof (*entry->value.leaf));
   if (entry->value.leaf == NULL)
     return dataend;
 
   data = datastart + val;
-  if (data < datastart || data >= dataend)
+  if (data < datastart || data + 12 > dataend)
     return dataend;
 
   addr = bfd_get_32 (abfd, data);
@@ -3351,6 +3369,8 @@ rsrc_parse_entry (bfd *abfd,
   entry->value.leaf->codepage = bfd_get_32 (abfd, data + 8);
   /* FIXME: We assume that the reserved field (data + 12) is OK.  */
 
+  if (size > dataend - datastart - (addr - rva_bias))
+    return dataend;
   entry->value.leaf->data = bfd_malloc (size);
   if (entry->value.leaf->data == NULL)
     return dataend;
@@ -3379,7 +3399,7 @@ rsrc_parse_entries (bfd *abfd,
       return highest_data;
     }
 
-  entry = bfd_malloc (sizeof * entry);
+  entry = bfd_malloc (sizeof (*entry));
   if (entry == NULL)
     return dataend;
 
@@ -3398,7 +3418,7 @@ rsrc_parse_entries (bfd *abfd,
 
       if (i)
        {
-         entry->next_entry = bfd_malloc (sizeof * entry);
+         entry->next_entry = bfd_malloc (sizeof (*entry));
          entry = entry->next_entry;
          if (entry == NULL)
            return dataend;
@@ -4186,13 +4206,7 @@ rsrc_process_section (bfd * abfd,
 
   rva_bias = sec->vma - pe->pe_opthdr.ImageBase;
 
-  data = bfd_malloc (size);
-  if (data == NULL)
-    return;
-
-  datastart = data;
-
-  if (! bfd_get_section_contents (abfd, sec, data, 0, size))
+  if (! bfd_malloc_and_get_section (abfd, sec, &datastart))
     goto end;
 
   /* Step zero: Scan the input bfds looking for .rsrc sections and record
@@ -4204,7 +4218,8 @@ rsrc_process_section (bfd * abfd,
      at the end of a variable amount.  (It does not appear to be based upon
      the section alignment or the file alignment).  We need to skip any
      padding bytes when parsing the input .rsrc sections.  */
-  rsrc_sizes = bfd_malloc (max_num_input_rsrc * sizeof * rsrc_sizes);
+  data = datastart;
+  rsrc_sizes = bfd_malloc (max_num_input_rsrc * sizeof (*rsrc_sizes));
   if (rsrc_sizes == NULL)
     goto end;
 
@@ -4221,7 +4236,7 @@ rsrc_process_section (bfd * abfd,
            {
              max_num_input_rsrc += 10;
              rsrc_sizes = bfd_realloc (rsrc_sizes, max_num_input_rsrc
-                                       * sizeof * rsrc_sizes);
+                                       * sizeof (*rsrc_sizes));
              if (rsrc_sizes == NULL)
                goto end;
            }
@@ -4274,7 +4289,7 @@ rsrc_process_section (bfd * abfd,
   data = datastart;
   rva_bias = sec->vma - pe->pe_opthdr.ImageBase;
 
-  type_tables = bfd_malloc (num_resource_sets * sizeof * type_tables);
+  type_tables = bfd_malloc (num_resource_sets * sizeof (*type_tables));
   if (type_tables == NULL)
     goto end;
 
@@ -4531,7 +4546,7 @@ _bfd_XXi_final_link_postscript (bfd * abfd, struct coff_final_link_info *pfinfo)
        the TLS data directory consists of 4 pointers, followed
        by two 4-byte integer. This implies that the total size
        is different for 32-bit and 64-bit executables.  */
-#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) && !defined(COFF_WITH_peLoongArch64)
       pe_data (abfd)->pe_opthdr.DataDirectory[PE_TLS_TABLE].Size = 0x18;
 #else
       pe_data (abfd)->pe_opthdr.DataDirectory[PE_TLS_TABLE].Size = 0x28;
@@ -4540,28 +4555,22 @@ _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) || defined(COFF_WITH_peLoongArch64))
   {
     asection *sec = bfd_get_section_by_name (abfd, ".pdata");
 
     if (sec)
       {
        bfd_size_type x = sec->rawsize;
-       bfd_byte *tmp_data = NULL;
-
-       if (x)
-         tmp_data = bfd_malloc (x);
+       bfd_byte *tmp_data;
 
-       if (tmp_data != NULL)
+       if (bfd_malloc_and_get_section (abfd, sec, &tmp_data))
          {
-           if (bfd_get_section_contents (abfd, sec, tmp_data, 0, x))
-             {
-               qsort (tmp_data,
-                      (size_t) (x / 12),
-                      12, sort_x64_pdata);
-               bfd_set_section_contents (pfinfo->output_bfd, sec,
-                                         tmp_data, 0, x);
-             }
+           qsort (tmp_data,
+                  (size_t) (x / 12),
+                  12, sort_x64_pdata);
+           bfd_set_section_contents (pfinfo->output_bfd, sec,
+                                     tmp_data, 0, x);
            free (tmp_data);
          }
        else