public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Alan Modra <amodra@gmail.com>
To: Tom Tromey <tom@tromey.com>
Cc: Aditya Kamath1 via Gdb-patches <gdb-patches@sourceware.org>,
	Ulrich Weigand <Ulrich.Weigand@de.ibm.com>,
	Aditya Kamath1 <Aditya.Kamath1@ibm.com>,
	Alan Modra <amodra@au1.ibm.com>,
	Sangamesh Mallayya <sangamesh.swamy@in.ibm.com>
Subject: Re: [Patch] Fix AIX shared library load broken during fork ().
Date: Tue, 11 Jul 2023 08:47:35 +0930	[thread overview]
Message-ID: <ZKyRj5KH5dW3HeOl@squeak.grove.modra.org> (raw)
In-Reply-To: <87zg43rc2f.fsf@tromey.com>

On Mon, Jul 10, 2023 at 10:00:40AM -0600, Tom Tromey wrote:
> >>>>> Aditya Kamath1 via Gdb-patches <gdb-patches@sourceware.org> writes:
> 
> > +std::vector<gdb_bfd_ref_ptr> object_bfd_vector;
> 
> I think it's a mistake to add a new global like this.
> It seems to me that this would interact poorly with multi-inferior or
> multi-target debugging.
> 
> Also, since nothing ever clears the vector, these BFDs will never be
> closed.

The proper place to fix this of course is in bfd.  Aditya, I think the
following ought to cure the problem you're seeing.  Did you open a PR?

	* coff-rs6000.c (add_range): Revise comment, noting possible fail.
	(_bfd_xcoff_openr_next_archived_file): Start with clean ranges.

diff --git a/bfd/coff-rs6000.c b/bfd/coff-rs6000.c
index 271a24fff69..59b9743356f 100644
--- a/bfd/coff-rs6000.c
+++ b/bfd/coff-rs6000.c
@@ -1598,14 +1598,21 @@ _bfd_xcoff_archive_p (bfd *abfd)
 
 /* Track file ranges occupied by elements.  Add [START,END) to the
    list of ranges and return TRUE if there is no overlap between the
-   new and any other element or the archive file header.  Note that
-   this would seem to preclude calling _bfd_get_elt_at_filepos twice
-   for the same element, but we won't get to _bfd_xcoff_read_ar_hdr if
-   an element is read more than once.  See _bfd_get_elt_at_filepos use
-   of _bfd_look_for_bfd_in_cache.  Also, the xcoff archive code
+   new and any other element or the archive file header.  This relies
+   on _bfd_xcoff_read_ar_hdr not being called more than once for the
+   same element, but that should be true (*).  The xcoff archive code
    doesn't call _bfd_read_ar_hdr when reading the armap, nor does it
    need to use extended name tables.  So those other routines in
-   archive.c that call _bfd_read_ar_hdr are unused.  */
+   archive.c that call _bfd_read_ar_hdr are unused.
+
+   *) There is one case where this might fail, but I think it is
+   sufficently unusual that it doesn't seem worth fixing:  When
+   scanning over archive elements using openr_next_archived_file, if
+   openr_next_archived_file is called twice with the same arguments
+   *and* the element returned is bfd_close'd between those calls then
+   we'll return false here.  The _bfd_look_for_bfd_in_cache use in
+   _bfd_get_elt_at_filepos stops this happening in the case where an
+   element is not closed.  */
 
 static bool
 add_range (bfd *abfd, ufile_ptr start, ufile_ptr end)
@@ -1770,12 +1777,14 @@ _bfd_xcoff_openr_next_archived_file (bfd *archive, bfd *last_file)
     {
       if (last_file == NULL)
 	{
+	  /* If we are scanning over elements twice in an open archive,
+	     which can happen in gdb after a fork, ensure we start the
+	     second scan with clean ranges.  */
+	  x_artdata (archive)->ranges.start = 0;
+	  x_artdata (archive)->ranges.end = SIZEOF_AR_FILE_HDR;
+	  x_artdata (archive)->ranges.next = NULL;
+	  x_artdata (archive)->ar_hdr_size = SIZEOF_AR_HDR;
 	  filestart = bfd_ardata (archive)->first_file_filepos;
-	  if (x_artdata (archive)->ar_hdr_size == 0)
-	    {
-	      x_artdata (archive)->ranges.end = SIZEOF_AR_FILE_HDR;
-	      x_artdata (archive)->ar_hdr_size = SIZEOF_AR_HDR;
-	    }
 	}
       else
 	GET_VALUE_IN_FIELD (filestart, arch_xhdr (last_file)->nextoff, 10);
@@ -1794,12 +1803,11 @@ _bfd_xcoff_openr_next_archived_file (bfd *archive, bfd *last_file)
     {
       if (last_file == NULL)
 	{
+	  x_artdata (archive)->ranges.start = 0;
+	  x_artdata (archive)->ranges.end = SIZEOF_AR_FILE_HDR_BIG;
+	  x_artdata (archive)->ranges.next = NULL;
+	  x_artdata (archive)->ar_hdr_size = SIZEOF_AR_HDR_BIG;
 	  filestart = bfd_ardata (archive)->first_file_filepos;
-	  if (x_artdata (archive)->ar_hdr_size == 0)
-	    {
-	      x_artdata (archive)->ranges.end = SIZEOF_AR_FILE_HDR_BIG;
-	      x_artdata (archive)->ar_hdr_size = SIZEOF_AR_HDR_BIG;
-	    }
 	}
       else
 	GET_VALUE_IN_FIELD (filestart, arch_xhdr_big (last_file)->nextoff, 10);

-- 
Alan Modra
Australia Development Lab, IBM

  reply	other threads:[~2023-07-10 23:17 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-07-10 11:35 Aditya Kamath1
2023-07-10 14:11 ` Alan Modra
2023-07-10 14:29   ` Aditya Kamath1
2023-07-10 15:04     ` Alan Modra
2023-07-10 16:00 ` Tom Tromey
2023-07-10 23:17   ` Alan Modra [this message]
2023-07-11  5:08     ` Aditya Kamath1

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=ZKyRj5KH5dW3HeOl@squeak.grove.modra.org \
    --to=amodra@gmail.com \
    --cc=Aditya.Kamath1@ibm.com \
    --cc=Ulrich.Weigand@de.ibm.com \
    --cc=amodra@au1.ibm.com \
    --cc=gdb-patches@sourceware.org \
    --cc=sangamesh.swamy@in.ibm.com \
    --cc=tom@tromey.com \
    /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).