PE dos_message

Message ID ZOs7cbn1QMhdUKLi@squeak.grove.modra.org
State New
Headers
Series PE dos_message |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_binutils_build--master-aarch64 warning Patch is already merged
linaro-tcwg-bot/tcwg_binutils_build--master-arm warning Patch is already merged
linaro-tcwg-bot/tcwg_binutils_check--master-aarch64 warning Patch is already merged
linaro-tcwg-bot/tcwg_binutils_check--master-arm warning Patch is already merged

Commit Message

Alan Modra Aug. 27, 2023, 12:02 p.m. UTC
  I was looking at dos_message and wondering why we have H_PUT_32
in _bfd_XXi_only_swap_filehdr_out but no H_GET_32 in pe_bfd_object_p.
On a big-endian machine this would result in scrambling the code and
strings contained in dos_message.  Rather than fix the lack of
H_GET_32 in pe_bfd_object_p, I decided it doesn't make sense to store
dos_message internally as an array of ints.

include/
	* coff/internal.h (struct internal_extra_pe_filehdr): Make
	dos_message a char array.
	* coff/msdos.h (struct external_DOS_hdr): Flatten dos_message.
	* coff/pe.h (struct external_PEI_filehdr): Likewise.
bfd/
	* libcoff-in.h (struct pe_tdata): Make dos_message a char array.
	* libcoff.h: Regenerate.
	* peXXigen.c (_bfd_XXi_only_swap_filehdr_out): memcpy dos_message
	to output.
	* peicode.h (pe_mkobject): Don't memset already zeroed pe_opthdr.
	Tidy allocation of tdata.pe_obj_data.  Set up dos_message from..
	(default_dos_message): ..this.  New static array.
  

Patch

diff --git a/bfd/libcoff-in.h b/bfd/libcoff-in.h
index eacfcb3ec39..b70710895f1 100644
--- a/bfd/libcoff-in.h
+++ b/bfd/libcoff-in.h
@@ -147,7 +147,7 @@  typedef struct pe_tdata
   int dll;
   int has_reloc_section;
   int dont_strip_reloc;
-  int dos_message[16];
+  char dos_message[64];
   /* The timestamp to insert into the output file.
      If the timestamp is -1 then the current time is used.  */
   int timestamp;
diff --git a/bfd/libcoff.h b/bfd/libcoff.h
index ad6138e6e3c..dfe3800ee68 100644
--- a/bfd/libcoff.h
+++ b/bfd/libcoff.h
@@ -151,7 +151,7 @@  typedef struct pe_tdata
   int dll;
   int has_reloc_section;
   int dont_strip_reloc;
-  int dos_message[16];
+  char dos_message[64];
   /* The timestamp to insert into the output file.
      If the timestamp is -1 then the current time is used.  */
   int timestamp;
diff --git a/bfd/peXXigen.c b/bfd/peXXigen.c
index 32433935038..2f2968d48e8 100644
--- a/bfd/peXXigen.c
+++ b/bfd/peXXigen.c
@@ -890,9 +890,8 @@  _bfd_XXi_only_swap_filehdr_out (bfd * abfd, void * in, void * out)
 
   H_PUT_32 (abfd, filehdr_in->pe.e_lfanew, filehdr_out->e_lfanew);
 
-  for (idx = 0; idx < 16; idx++)
-    H_PUT_32 (abfd, filehdr_in->pe.dos_message[idx],
-	      filehdr_out->dos_message[idx]);
+  memcpy (filehdr_out->dos_message, filehdr_in->pe.dos_message,
+	  sizeof (filehdr_out->dos_message));
 
   /* Also put in the NT signature.  */
   H_PUT_32 (abfd, filehdr_in->pe.nt_signature, filehdr_out->nt_signature);
diff --git a/bfd/peicode.h b/bfd/peicode.h
index 1ff13b0313d..72adce1a068 100644
--- a/bfd/peicode.h
+++ b/bfd/peicode.h
@@ -258,40 +258,28 @@  coff_swap_scnhdr_in (bfd * abfd, void * ext, void * in)
 static bool
 pe_mkobject (bfd * abfd)
 {
-  pe_data_type *pe;
-  size_t amt = sizeof (pe_data_type);
-
-  abfd->tdata.pe_obj_data = (struct pe_tdata *) bfd_zalloc (abfd, amt);
-
-  if (abfd->tdata.pe_obj_data == 0)
+  /* Some x86 code followed by an ascii string.  */
+  static const char default_dos_message[64] = {
+    0x0e, 0x1f, 0xba, 0x0e, 0x00, 0xb4, 0x09, 0xcd,
+    0x21, 0xb8, 0x01, 0x4c, 0xcd, 0x21, 0x54, 0x68,
+    0x69, 0x73, 0x20, 0x70, 0x72, 0x6f, 0x67, 0x72,
+    0x61, 0x6d, 0x20, 0x63, 0x61, 0x6e, 0x6e, 0x6f,
+    0x74, 0x20, 0x62, 0x65, 0x20, 0x72, 0x75, 0x6e,
+    0x20, 0x69, 0x6e, 0x20, 0x44, 0x4f, 0x53, 0x20,
+    0x6d, 0x6f, 0x64, 0x65, 0x2e, 0x0d, 0x0d, 0x0a,
+    0x24, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
+
+  pe_data_type *pe = bfd_zalloc (abfd, sizeof (*pe));
+  abfd->tdata.pe_obj_data = pe;
+  if (pe == NULL)
     return false;
 
-  pe = pe_data (abfd);
-
   pe->coff.pe = 1;
 
   /* in_reloc_p is architecture dependent.  */
   pe->in_reloc_p = in_reloc_p;
 
-  /* Default DOS message string.  */
-  pe->dos_message[0]  = 0x0eba1f0e;
-  pe->dos_message[1]  = 0xcd09b400;
-  pe->dos_message[2]  = 0x4c01b821;
-  pe->dos_message[3]  = 0x685421cd;
-  pe->dos_message[4]  = 0x70207369;
-  pe->dos_message[5]  = 0x72676f72;
-  pe->dos_message[6]  = 0x63206d61;
-  pe->dos_message[7]  = 0x6f6e6e61;
-  pe->dos_message[8]  = 0x65622074;
-  pe->dos_message[9]  = 0x6e757220;
-  pe->dos_message[10] = 0x206e6920;
-  pe->dos_message[11] = 0x20534f44;
-  pe->dos_message[12] = 0x65646f6d;
-  pe->dos_message[13] = 0x0a0d0d2e;
-  pe->dos_message[14] = 0x24;
-  pe->dos_message[15] = 0x0;
-
-  memset (& pe->pe_opthdr, 0, sizeof pe->pe_opthdr);
+  memcpy (pe->dos_message, default_dos_message, sizeof (pe->dos_message));
 
   bfd_coff_long_section_names (abfd)
     = coff_backend_info (abfd)->_bfd_coff_long_section_names;
diff --git a/include/coff/internal.h b/include/coff/internal.h
index 7a566caa057..f33817e7788 100644
--- a/include/coff/internal.h
+++ b/include/coff/internal.h
@@ -54,7 +54,7 @@  struct internal_extra_pe_filehdr
   unsigned short e_oeminfo;	/* OEM information; e_oemid specific, 0x0 */
   unsigned short e_res2[10];	/* Reserved words, all 0x0 */
   bfd_vma  e_lfanew;		/* File address of new exe header, 0x80 */
-  unsigned int dos_message[16]; /* Text which always follows DOS header.  */
+  char dos_message[64];		/* Text which always follows DOS header.  */
   bfd_vma  nt_signature;   	/* required NT signature, 0x4550 */
 };
 
diff --git a/include/coff/msdos.h b/include/coff/msdos.h
index b879e45db92..5f09c76cc2f 100644
--- a/include/coff/msdos.h
+++ b/include/coff/msdos.h
@@ -48,7 +48,7 @@  struct external_DOS_hdr
   char e_oeminfo[2];		/* OEM information.  */
   char e_res2[10][2];		/* Reserved words, all 0x0.  */
   char e_lfanew[4];		/* File address of new exe header, usually 0x80.  */
-  char dos_message[16][4];	/* Other stuff, always follow DOS header.  */
+  char dos_message[64];		/* Other stuff, always follow DOS header.  */
 };
 
 /* The actual DOS header only includes up to the e_ovno field.  */
diff --git a/include/coff/pe.h b/include/coff/pe.h
index 6b26d533218..7e69e6f77ba 100644
--- a/include/coff/pe.h
+++ b/include/coff/pe.h
@@ -218,7 +218,7 @@  struct external_PEI_filehdr
   char e_oeminfo[2];		/* OEM information; e_oemid specific, 0x0.  */
   char e_res2[10][2];		/* Reserved words, all 0x0.  */
   char e_lfanew[4];		/* File address of new exe header, usually 0x80.  */
-  char dos_message[16][4];	/* Other stuff, always follow DOS header.  */
+  char dos_message[64];		/* Other stuff, always follow DOS header.  */
 
   /* Note: additional bytes may be inserted before the signature.  Use
    the e_lfanew field to find the actual location of the NT signature.  */