public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
* [PATCH 2/3] Add some new PE_IMAGE_DEBUG_TYPE values
  2020-01-21 15:53 [PATCH 0/3] Identify reproducible build PE files in 'objdump -p' Jon Turney
@ 2020-01-21 15:53 ` Jon Turney
  2020-01-28 22:12   ` Alan Modra
  2020-01-21 15:53 ` [PATCH 3/3] Identify reproducible builds in 'objdump -p' output for PE files Jon Turney
  2020-01-21 15:53 ` [PATCH 1/3] Bugfixes for pe_print_debugdata() Jon Turney
  2 siblings, 1 reply; 8+ messages in thread
From: Jon Turney @ 2020-01-21 15:53 UTC (permalink / raw)
  To: binutils; +Cc: Jon Turney

IMAGE_DEBUG_TYPE_REPRO is defined in the latest version of the PE
specification [1]. The others are defined in Windows SDK headers and/or
reported by DUMPBIN.

[1] https://docs.microsoft.com/en-us/windows/win32/debug/pe-format

bfd/ChangeLog:

2020-01-16  Jon Turney  <jon.turney@dronecode.org.uk>

	* peXXigen.c (debug_type_names): Add names for new debug data type
	values.

include/ChangeLog:

2020-01-16  Jon Turney  <jon.turney@dronecode.org.uk>

	* coff/internal.h (PE_IMAGE_DEBUG_TYPE_VC_FEATURE)
	(PE_IMAGE_DEBUG_TYPE_POGO, PE_IMAGE_DEBUG_TYPE_ILTCG)
	(PE_IMAGE_DEBUG_TYPE_MPX, PE_IMAGE_DEBUG_TYPE_REPRO): Add.
---
 bfd/ChangeLog           | 5 +++++
 bfd/peXXigen.c          | 7 ++++++-
 include/ChangeLog       | 6 ++++++
 include/coff/internal.h | 5 +++++
 4 files changed, 22 insertions(+), 1 deletion(-)

diff --git a/bfd/peXXigen.c b/bfd/peXXigen.c
index ac0cf17464..dc7951f8d9 100644
--- a/bfd/peXXigen.c
+++ b/bfd/peXXigen.c
@@ -2603,7 +2603,7 @@ rsrc_print_section (bfd * abfd, void * vfile)
   return TRUE;
 }
 
-#define IMAGE_NUMBEROF_DEBUG_TYPES 12
+#define IMAGE_NUMBEROF_DEBUG_TYPES 17
 
 static char * debug_type_names[IMAGE_NUMBEROF_DEBUG_TYPES] =
 {
@@ -2619,6 +2619,11 @@ static char * debug_type_names[IMAGE_NUMBEROF_DEBUG_TYPES] =
   "Borland",
   "Reserved",
   "CLSID",
+  "Feature",
+  "CoffGrp",
+  "ILTCG",
+  "MPX",
+  "Repro",
 };
 
 static bfd_boolean
diff --git a/include/coff/internal.h b/include/coff/internal.h
index 24ac1dc75c..cbeb0160f6 100644
--- a/include/coff/internal.h
+++ b/include/coff/internal.h
@@ -157,6 +157,11 @@ struct internal_IMAGE_DEBUG_DIRECTORY
 #define PE_IMAGE_DEBUG_TYPE_BORLAND          9
 #define PE_IMAGE_DEBUG_TYPE_RESERVED10       10
 #define PE_IMAGE_DEBUG_TYPE_CLSID            11
+#define PE_IMAGE_DEBUG_TYPE_VC_FEATURE       12
+#define PE_IMAGE_DEBUG_TYPE_POGO             13
+#define PE_IMAGE_DEBUG_TYPE_ILTCG            14
+#define PE_IMAGE_DEBUG_TYPE_MPX              15
+#define PE_IMAGE_DEBUG_TYPE_REPRO            16
 
 /* Extra structure for a codeview debug record */
 #define CV_INFO_SIGNATURE_LENGTH 16
-- 
2.21.0

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH 0/3] Identify reproducible build PE files in 'objdump -p'
@ 2020-01-21 15:53 Jon Turney
  2020-01-21 15:53 ` [PATCH 2/3] Add some new PE_IMAGE_DEBUG_TYPE values Jon Turney
                   ` (2 more replies)
  0 siblings, 3 replies; 8+ messages in thread
From: Jon Turney @ 2020-01-21 15:53 UTC (permalink / raw)
  To: binutils; +Cc: Jon Turney

Identify reproducible build PE files in 'objdump -p'

Future work: It would be nice to improve 'ld --no-insert-timestamp' to 
produce build marked as reproducible in this way, but that's more involved.

Jon Turney (3):
  Bugfixes for pe_print_debugdata()
  Add some new PE_IMAGE_DEBUG_TYPE values
  Identify reproducible builds in 'objdump -p' output for PE files

 bfd/ChangeLog           |  16 +++++++
 bfd/peXXigen.c          | 100 ++++++++++++++++++++++++++++++++++++----
 include/ChangeLog       |   6 +++
 include/coff/internal.h |   5 ++
 4 files changed, 118 insertions(+), 9 deletions(-)

-- 
2.21.0

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH 1/3] Bugfixes for pe_print_debugdata()
  2020-01-21 15:53 [PATCH 0/3] Identify reproducible build PE files in 'objdump -p' Jon Turney
  2020-01-21 15:53 ` [PATCH 2/3] Add some new PE_IMAGE_DEBUG_TYPE values Jon Turney
  2020-01-21 15:53 ` [PATCH 3/3] Identify reproducible builds in 'objdump -p' output for PE files Jon Turney
@ 2020-01-21 15:53 ` Jon Turney
  2020-01-28 22:11   ` Alan Modra
  2 siblings, 1 reply; 8+ messages in thread
From: Jon Turney @ 2020-01-21 15:53 UTC (permalink / raw)
  To: binutils; +Cc: Jon Turney

Use a separate iteration variable for inner loop (:blush:).  This
generally prevented any debug directory entries after a
IMAGE_DEBUG_TYPE_CODEVIEW entry from being reported.

Don't leak the memory allocated for the section containing the debug
directory.

bfd/ChangeLog:

2020-01-16  Jon Turney  <jon.turney@dronecode.org.uk>

	* peXXigen.c (pe_print_debugdata): Fix the iteration variable for
	inner loop.  Fix a memory leak.
---
 bfd/ChangeLog  | 5 +++++
 bfd/peXXigen.c | 8 +++++---
 2 files changed, 10 insertions(+), 3 deletions(-)

diff --git a/bfd/peXXigen.c b/bfd/peXXigen.c
index c5082a7dee..ac0cf17464 100644
--- a/bfd/peXXigen.c
+++ b/bfd/peXXigen.c
@@ -2630,7 +2630,7 @@ pe_print_debugdata (bfd * abfd, void * vfile)
   asection *section;
   bfd_byte *data = 0;
   bfd_size_type dataoff;
-  unsigned int i;
+  unsigned int i, j;
 
   bfd_vma addr = extra->DataDirectory[PE_DEBUG_DATA].VirtualAddress;
   bfd_size_type size = extra->DataDirectory[PE_DEBUG_DATA].Size;
@@ -2722,8 +2722,8 @@ pe_print_debugdata (bfd * abfd, void * vfile)
 					       idd.SizeOfData, cvinfo))
 	    continue;
 
-	  for (i = 0; i < cvinfo->SignatureLength; i++)
-	    sprintf (&signature[i*2], "%02x", cvinfo->Signature[i] & 0xff);
+	  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"),
@@ -2732,6 +2732,8 @@ pe_print_debugdata (bfd * abfd, void * vfile)
 	}
     }
 
+  free(data);
+
   if (size % sizeof (struct external_IMAGE_DEBUG_DIRECTORY) != 0)
     fprintf (file,
 	    _("The debug directory size is not a multiple of the debug directory entry size\n"));
-- 
2.21.0

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH 3/3] Identify reproducible builds in 'objdump -p' output for PE files
  2020-01-21 15:53 [PATCH 0/3] Identify reproducible build PE files in 'objdump -p' Jon Turney
  2020-01-21 15:53 ` [PATCH 2/3] Add some new PE_IMAGE_DEBUG_TYPE values Jon Turney
@ 2020-01-21 15:53 ` Jon Turney
  2020-01-28 22:18   ` Alan Modra
  2020-01-21 15:53 ` [PATCH 1/3] Bugfixes for pe_print_debugdata() Jon Turney
  2 siblings, 1 reply; 8+ messages in thread
From: Jon Turney @ 2020-01-21 15:53 UTC (permalink / raw)
  To: binutils; +Cc: Jon Turney

These are produced by MSVC when the '/Brepro' flag is used.

To quote from the PE specification [1]:

"The presence of an entry of type IMAGE_DEBUG_TYPE_REPRO indicates the
PE file is built in a way to achieve determinism or reproducibility. If
the input does not change, the output PE file is guaranteed to be
bit-for-bit identical no matter when or where the PE is produced.
Various date/time stamp fields in the PE file are filled with part or
all the bits from a calculated hash value that uses PE file content as
input, and therefore no longer represent the actual date and time when a
PE file or related specific data within the PE is produced. The raw data
of this debug entry may be empty, or may contain a calculated hash value
preceded by a four-byte value that represents the hash value length."

[1] https://docs.microsoft.com/en-us/windows/win32/debug/pe-format

bfd/ChangeLog:

2020-01-16  Jon Turney  <jon.turney@dronecode.org.uk>

	* peXXigen.c (pe_is_repro): New function.
	(_bfd_XX_print_private_bfd_data_common): Note timestamp is
	actually a build hash if PE_IMAGE_DEBUG_TYPE_REPRO is present.
---
 bfd/ChangeLog  |  6 ++++
 bfd/peXXigen.c | 85 +++++++++++++++++++++++++++++++++++++++++++++++---
 2 files changed, 86 insertions(+), 5 deletions(-)

diff --git a/bfd/peXXigen.c b/bfd/peXXigen.c
index dc7951f8d9..f742375525 100644
--- a/bfd/peXXigen.c
+++ b/bfd/peXXigen.c
@@ -2746,6 +2746,71 @@ pe_print_debugdata (bfd * abfd, void * vfile)
   return TRUE;
 }
 
+static bfd_boolean
+pe_is_repro (bfd * abfd)
+{
+  pe_data_type *pe = pe_data (abfd);
+  struct internal_extra_pe_aouthdr *extra = &pe->pe_opthdr;
+  asection *section;
+  bfd_byte *data = 0;
+  bfd_size_type dataoff;
+  unsigned int i;
+  bfd_boolean res = FALSE;
+
+  bfd_vma addr = extra->DataDirectory[PE_DEBUG_DATA].VirtualAddress;
+  bfd_size_type size = extra->DataDirectory[PE_DEBUG_DATA].Size;
+
+  if (size == 0)
+    return FALSE;
+
+  addr += extra->ImageBase;
+  for (section = abfd->sections; section != NULL; section = section->next)
+    {
+      if ((addr >= section->vma) && (addr < (section->vma + section->size)))
+	break;
+    }
+
+  if ((section == NULL) ||
+      (!(section->flags & SEC_HAS_CONTENTS)) ||
+      (section->size < size))
+    {
+      return FALSE;
+    }
+
+  dataoff = addr - section->vma;
+
+  if (size > (section->size - dataoff))
+    {
+      return FALSE;
+    }
+
+  if (!bfd_malloc_and_get_section (abfd, section, &data))
+    {
+      if (data != NULL)
+	free (data);
+      return FALSE;
+    }
+
+  for (i = 0; i < size / sizeof (struct external_IMAGE_DEBUG_DIRECTORY); i++)
+    {
+      struct external_IMAGE_DEBUG_DIRECTORY *ext
+	= &((struct external_IMAGE_DEBUG_DIRECTORY *)(data + dataoff))[i];
+      struct internal_IMAGE_DEBUG_DIRECTORY idd;
+
+      _bfd_XXi_swap_debugdir_in (abfd, ext, &idd);
+
+      if (idd.Type == PE_IMAGE_DEBUG_TYPE_REPRO)
+        {
+          res = TRUE;
+          break;
+        }
+    }
+
+  free(data);
+
+  return res;
+}
+
 /* Print out the program headers.  */
 
 bfd_boolean
@@ -2777,11 +2842,21 @@ _bfd_XX_print_private_bfd_data_common (bfd * abfd, void * vfile)
   PF (IMAGE_FILE_BYTES_REVERSED_HI, "big endian");
 #undef PF
 
-  /* ctime implies '\n'.  */
-  {
-    time_t t = pe->coff.timestamp;
-    fprintf (file, "\nTime/Date\t\t%s", ctime (&t));
-  }
+  /*
+    If a PE_IMAGE_DEBUG_TYPE_REPRO entry is present in the debug directory, the
+    timestamp is to be interpreted as the hash of a reproducible build.
+  */
+  if (pe_is_repro (abfd))
+    {
+      fprintf (file, "\nTime/Date\t\t%08lx", pe->coff.timestamp);
+      fprintf (file, "\t(This is a reproducible build file hash, not a timestamp)\n");
+    }
+  else
+    {
+      /* ctime implies '\n'.  */
+      time_t t = pe->coff.timestamp;
+      fprintf (file, "\nTime/Date\t\t%s", ctime (&t));
+    }
 
 #ifndef IMAGE_NT_OPTIONAL_HDR_MAGIC
 # define IMAGE_NT_OPTIONAL_HDR_MAGIC 0x10b
-- 
2.21.0

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH 1/3] Bugfixes for pe_print_debugdata()
  2020-01-21 15:53 ` [PATCH 1/3] Bugfixes for pe_print_debugdata() Jon Turney
@ 2020-01-28 22:11   ` Alan Modra
  0 siblings, 0 replies; 8+ messages in thread
From: Alan Modra @ 2020-01-28 22:11 UTC (permalink / raw)
  To: Jon Turney; +Cc: binutils

On Tue, Jan 21, 2020 at 03:52:10PM +0000, Jon Turney wrote:
> 	* peXXigen.c (pe_print_debugdata): Fix the iteration variable for
> 	inner loop.  Fix a memory leak.

OK.

-- 
Alan Modra
Australia Development Lab, IBM

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH 2/3] Add some new PE_IMAGE_DEBUG_TYPE values
  2020-01-21 15:53 ` [PATCH 2/3] Add some new PE_IMAGE_DEBUG_TYPE values Jon Turney
@ 2020-01-28 22:12   ` Alan Modra
  0 siblings, 0 replies; 8+ messages in thread
From: Alan Modra @ 2020-01-28 22:12 UTC (permalink / raw)
  To: Jon Turney; +Cc: binutils

On Tue, Jan 21, 2020 at 03:52:11PM +0000, Jon Turney wrote:
> IMAGE_DEBUG_TYPE_REPRO is defined in the latest version of the PE
> specification [1]. The others are defined in Windows SDK headers and/or
> reported by DUMPBIN.
> 
> [1] https://docs.microsoft.com/en-us/windows/win32/debug/pe-format
> 
> bfd/ChangeLog:
> 
> 2020-01-16  Jon Turney  <jon.turney@dronecode.org.uk>
> 
> 	* peXXigen.c (debug_type_names): Add names for new debug data type
> 	values.
> 
> include/ChangeLog:
> 
> 2020-01-16  Jon Turney  <jon.turney@dronecode.org.uk>
> 
> 	* coff/internal.h (PE_IMAGE_DEBUG_TYPE_VC_FEATURE)
> 	(PE_IMAGE_DEBUG_TYPE_POGO, PE_IMAGE_DEBUG_TYPE_ILTCG)
> 	(PE_IMAGE_DEBUG_TYPE_MPX, PE_IMAGE_DEBUG_TYPE_REPRO): Add.

OK.

-- 
Alan Modra
Australia Development Lab, IBM

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH 3/3] Identify reproducible builds in 'objdump -p' output for PE files
  2020-01-21 15:53 ` [PATCH 3/3] Identify reproducible builds in 'objdump -p' output for PE files Jon Turney
@ 2020-01-28 22:18   ` Alan Modra
  2020-01-30 13:08     ` Jon Turney
  0 siblings, 1 reply; 8+ messages in thread
From: Alan Modra @ 2020-01-28 22:18 UTC (permalink / raw)
  To: Jon Turney; +Cc: binutils

On Tue, Jan 21, 2020 at 03:52:12PM +0000, Jon Turney wrote:
> 	* peXXigen.c (pe_is_repro): New function.
> 	(_bfd_XX_print_private_bfd_data_common): Note timestamp is
> 	actually a build hash if PE_IMAGE_DEBUG_TYPE_REPRO is present.

OK, except

> +  if ((section == NULL) ||
> +      (!(section->flags & SEC_HAS_CONTENTS)) ||
> +      (section->size < size))

Please fix the trailing operators.

  if (section == NULL
      || !(section->flags & SEC_HAS_CONTENTS)
      || section->size < size)

-- 
Alan Modra
Australia Development Lab, IBM

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH 3/3] Identify reproducible builds in 'objdump -p' output for PE files
  2020-01-28 22:18   ` Alan Modra
@ 2020-01-30 13:08     ` Jon Turney
  0 siblings, 0 replies; 8+ messages in thread
From: Jon Turney @ 2020-01-30 13:08 UTC (permalink / raw)
  To: binutils

On 28/01/2020 22:18, Alan Modra wrote:
> On Tue, Jan 21, 2020 at 03:52:12PM +0000, Jon Turney wrote:
>> 	* peXXigen.c (pe_is_repro): New function.
>> 	(_bfd_XX_print_private_bfd_data_common): Note timestamp is
>> 	actually a build hash if PE_IMAGE_DEBUG_TYPE_REPRO is present.
> 
> OK, except
> 
>> +  if ((section == NULL) ||
>> +      (!(section->flags & SEC_HAS_CONTENTS)) ||
>> +      (section->size < size))
> 
> Please fix the trailing operators.
> 
>    if (section == NULL
>        || !(section->flags & SEC_HAS_CONTENTS)
>        || section->size < size)
> 

Thanks.

Pushed with that correction.

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2020-01-30 13:08 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-01-21 15:53 [PATCH 0/3] Identify reproducible build PE files in 'objdump -p' Jon Turney
2020-01-21 15:53 ` [PATCH 2/3] Add some new PE_IMAGE_DEBUG_TYPE values Jon Turney
2020-01-28 22:12   ` Alan Modra
2020-01-21 15:53 ` [PATCH 3/3] Identify reproducible builds in 'objdump -p' output for PE files Jon Turney
2020-01-28 22:18   ` Alan Modra
2020-01-30 13:08     ` Jon Turney
2020-01-21 15:53 ` [PATCH 1/3] Bugfixes for pe_print_debugdata() Jon Turney
2020-01-28 22:11   ` Alan Modra

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).