public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
From: Alan Modra <amodra@gmail.com>
To: binutils@sourceware.org
Subject: COFF build-id writes uninitialised data to file
Date: Fri, 23 Dec 2022 10:34:53 +1030	[thread overview]
Message-ID: <Y6TwpegMSaNdNc48@squeak.grove.modra.org> (raw)

1) The first write in write_build_id wrote rubbish past the struct
external_IMAGE_DEBUG_DIRECTORY, which was later overwritten with
correct data.  No user visible problem there, except that tools like
valgrind complain.
2) The size for the pdb name was incorrectly calculated.

	* emultempl/pe.em (write_build_id): Write the debug directory,
	not the entire section contents.
	(setup_build_id): Add size for the base name of pdb_name, not
	the full path.
	* emultempl/pep.em: Likewise.
	* testsuite/ld-pe/pdb2-section-contrib.d: Update.

diff --git a/ld/emultempl/pe.em b/ld/emultempl/pe.em
index 907259535b7..1e0e23df043 100644
--- a/ld/emultempl/pe.em
+++ b/ld/emultempl/pe.em
@@ -1285,7 +1285,6 @@ write_build_id (bfd *abfd)
   asection *asec;
   struct bfd_link_order *link_order = NULL;
   unsigned char *contents;
-  bfd_size_type size;
   bfd_size_type build_id_size;
   unsigned char *build_id;
   const char *pdb_base_name = NULL;
@@ -1320,7 +1319,6 @@ write_build_id (bfd *abfd)
   if (t->build_id.sec->contents == NULL)
     t->build_id.sec->contents = (unsigned char *) xmalloc (t->build_id.sec->size);
   contents = t->build_id.sec->contents;
-  size = t->build_id.sec->size;
 
   build_id_size = compute_build_id_size (t->build_id.style);
   build_id = xmalloc (build_id_size);
@@ -1351,7 +1349,7 @@ write_build_id (bfd *abfd)
   if (bfd_seek (abfd, asec->filepos + link_order->offset, SEEK_SET) != 0)
     return 0;
 
-  if (bfd_bwrite (contents, size, abfd) != size)
+  if (bfd_bwrite (contents, sizeof (*ext), abfd) != sizeof (*ext))
     return 0;
 
   if (pdb)
@@ -1417,7 +1415,7 @@ setup_build_id (bfd *ibfd)
 	+ sizeof (CV_INFO_PDB70) + 1;
 
       if (pdb_name)
-	s->size += strlen (pdb_name);
+	s->size += strlen (lbasename (pdb_name));
 
       return true;
     }
diff --git a/ld/emultempl/pep.em b/ld/emultempl/pep.em
index e2c538e6d99..e61414893ad 100644
--- a/ld/emultempl/pep.em
+++ b/ld/emultempl/pep.em
@@ -1277,7 +1277,6 @@ write_build_id (bfd *abfd)
   asection *asec;
   struct bfd_link_order *link_order = NULL;
   unsigned char *contents;
-  bfd_size_type size;
   bfd_size_type build_id_size;
   unsigned char *build_id;
   const char *pdb_base_name = NULL;
@@ -1312,7 +1311,6 @@ write_build_id (bfd *abfd)
   if (t->build_id.sec->contents == NULL)
     t->build_id.sec->contents = (unsigned char *) xmalloc (t->build_id.sec->size);
   contents = t->build_id.sec->contents;
-  size = t->build_id.sec->size;
 
   build_id_size = compute_build_id_size (t->build_id.style);
   build_id = xmalloc (build_id_size);
@@ -1343,7 +1341,7 @@ write_build_id (bfd *abfd)
   if (bfd_seek (abfd, asec->filepos + link_order->offset, SEEK_SET) != 0)
     return 0;
 
-  if (bfd_bwrite (contents, size, abfd) != size)
+  if (bfd_bwrite (contents, sizeof (*ext), abfd) != sizeof (*ext))
     return 0;
 
   if (pdb)
@@ -1409,7 +1407,7 @@ setup_build_id (bfd *ibfd)
 	+ sizeof (CV_INFO_PDB70) + 1;
 
       if (pdb_name)
-	s->size += strlen (pdb_name);
+	s->size += strlen (lbasename (pdb_name));
 
       return true;
     }
diff --git a/ld/testsuite/ld-pe/pdb2-section-contrib.d b/ld/testsuite/ld-pe/pdb2-section-contrib.d
index 3afeb149e4b..dd9437214bb 100644
--- a/ld/testsuite/ld-pe/pdb2-section-contrib.d
+++ b/ld/testsuite/ld-pe/pdb2-section-contrib.d
@@ -4,7 +4,7 @@ tmpdir/pdb2-sc:     file format binary
 Contents of section .data:
  0000 2dba2ef1 01000000 00000000 10000000  -...............
  0010 20000060 00000000 00000000 00000000   ..`............
- 0020 02000000 00000000 44000000 40000040  ........D...@..@
+ 0020 02000000 00000000 3d000000 40000040  ........=...@..@
  0030 00000000 00000000 00000000 01000000  ................
  0040 10000000 10000000 20000060 01000000  ........ ..`....
  0050 00000000 00000000 04000000 00000000  ................

-- 
Alan Modra
Australia Development Lab, IBM

                 reply	other threads:[~2022-12-23  0:04 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=Y6TwpegMSaNdNc48@squeak.grove.modra.org \
    --to=amodra@gmail.com \
    --cc=binutils@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).