[2/3] Consistently use BFD's time

Message ID 20200114210956.25115-3-tromey@adacore.com
State New
Headers show
Series
  • Fix gdb's BFD cache
Related show

Commit Message

Tom Tromey Jan. 14, 2020, 9:09 p.m.
gdb uses the gnulib stat, while BFD does not.  This can lead to
inconsistencies between the two, because the gnulib stat adjusts for
timezones.

This patch changes gdb to use bfd_stat when examining a BFD's time.
This avoids the problem; and also opens the door to caching target
BFDs as well.

One possible downside here is that gdb must now create a BFD before
checking the cache.

gdb/ChangeLog
2020-01-14  Tom Tromey  <tromey@adacore.com>

	* gdb_bfd.c (gdb_bfd_open): Use bfd_stat.
	* symfile.c (reread_symbols): Use bfd_stat.

Change-Id: Ie937630a221cbae15809c99327b47c1cbce141c0
---
 gdb/ChangeLog |  5 +++++
 gdb/gdb_bfd.c | 49 +++++++++++++++++++++++++------------------------
 gdb/symfile.c |  5 +----
 3 files changed, 31 insertions(+), 28 deletions(-)

-- 
2.21.1

Comments

Victor Collod via Gdb-patches Jan. 14, 2020, 10:26 p.m. | #1
On Tue, Jan 14, 2020 at 4:10 PM Tom Tromey <tromey@adacore.com> wrote:
>

> gdb uses the gnulib stat, while BFD does not.  This can lead to

> inconsistencies between the two, because the gnulib stat adjusts for

> timezones.

>

> This patch changes gdb to use bfd_stat when examining a BFD's time.

> This avoids the problem; and also opens the door to caching target

> BFDs as well.

>

> One possible downside here is that gdb must now create a BFD before

> checking the cache.

>

> gdb/ChangeLog

> 2020-01-14  Tom Tromey  <tromey@adacore.com>

>

>         * gdb_bfd.c (gdb_bfd_open): Use bfd_stat.

>         * symfile.c (reread_symbols): Use bfd_stat.

>

> Change-Id: Ie937630a221cbae15809c99327b47c1cbce141c0

> ---

>  gdb/ChangeLog |  5 +++++

>  gdb/gdb_bfd.c | 49 +++++++++++++++++++++++++------------------------

>  gdb/symfile.c |  5 +----

>  3 files changed, 31 insertions(+), 28 deletions(-)

>

> diff --git a/gdb/gdb_bfd.c b/gdb/gdb_bfd.c

> index a1ee7814f32..26968396a15 100644

> --- a/gdb/gdb_bfd.c

> +++ b/gdb/gdb_bfd.c

> @@ -442,8 +442,15 @@ gdb_bfd_open (const char *name, const char *target, int fd)

>         }

>      }

>

> +  /* We open the BFD here so that we can use BFD to get the mtime.

> +     This is important because gdb uses the gnulib stat, but BFD does

> +     not, and this can lead to differences on some systems.  */


Maybe mention that mingw/windows is one of those?

Christian
Eli Zaretskii Jan. 15, 2020, 4:07 p.m. | #2
> From: Tom Tromey <tromey@adacore.com>

> Cc: Tom Tromey <tromey@adacore.com>

> Date: Tue, 14 Jan 2020 14:09:55 -0700

> 

> gdb uses the gnulib stat, while BFD does not.  This can lead to

> inconsistencies between the two, because the gnulib stat adjusts for

> timezones.


There's one more potential issue with Gnulib's replacement of 'fstat':
it also replaces the definition of 'struct stat', and it does that in
a way that might yield incompatibility between the definition on
<sys/stat.h> the system header and Gnulib's sys/stat.h replacement.
If gdb_bfd.c uses the Gnulib definition of 'struct stat' (as I think
we do everywhere in gdb/), then this replacement might create problems
on MinGW similar to those I reported to the Gnulib list (see the URL I
cited in an earlier message), because bfd_stat uses an incompatible
definition of 'struct stat'.

Of course, given that the Gnulib developers rejected my request not to
override the system definition of 'struct stat', GDB could also ignore
those problems, accepting their judgment.
Pedro Alves Jan. 16, 2020, 8:37 p.m. | #3
On 1/15/20 4:07 PM, Eli Zaretskii wrote:
>> From: Tom Tromey <tromey@adacore.com>

>> Cc: Tom Tromey <tromey@adacore.com>

>> Date: Tue, 14 Jan 2020 14:09:55 -0700

>>

>> gdb uses the gnulib stat, while BFD does not.  This can lead to

>> inconsistencies between the two, because the gnulib stat adjusts for

>> timezones.

> 

> There's one more potential issue with Gnulib's replacement of 'fstat':

> it also replaces the definition of 'struct stat', and it does that in

> a way that might yield incompatibility between the definition on

> <sys/stat.h> the system header and Gnulib's sys/stat.h replacement.

> If gdb_bfd.c uses the Gnulib definition of 'struct stat' (as I think

> we do everywhere in gdb/), then this replacement might create problems

> on MinGW similar to those I reported to the Gnulib list (see the URL I

> cited in an earlier message), because bfd_stat uses an incompatible

> definition of 'struct stat'.

> 

> Of course, given that the Gnulib developers rejected my request not to

> override the system definition of 'struct stat', GDB could also ignore

> those problems, accepting their judgment.


I think that we need to:

- #undef stat before including bfd headers.
- Redefine it afterwards back to rpl_stat (*)
- add some kind of wrapper around bfd_stat (like maybe called gdb_bfd_stat)
  that handles the mismatch.  Something like:

int
gdb_bfd_stat (bfd *abfd, struct stat *statbuf)
{
#undef stat
  struct stat st;

  int res = bfd_stat (abfd, &st);
  if (res != 0)
    return res;

#define COPY(FIELD) statbuf->FIELD = st.FIELD
  COPY (st_dev);
  // ... copy over all relevant fields ...
#undef COPY

#define stat rpl_stat
}

(*) there's probably some '#ifdef _GL...' we can check to know
whether we need to do this.

Thanks,
Pedro Alves
Victor Collod via Gdb-patches Jan. 16, 2020, 8:46 p.m. | #4
On Thu, Jan 16, 2020 at 3:37 PM Pedro Alves <palves@redhat.com> wrote:
>

> On 1/15/20 4:07 PM, Eli Zaretskii wrote:

> >> From: Tom Tromey <tromey@adacore.com>

> >> Cc: Tom Tromey <tromey@adacore.com>

> >> Date: Tue, 14 Jan 2020 14:09:55 -0700

> >>

> >> gdb uses the gnulib stat, while BFD does not.  This can lead to

> >> inconsistencies between the two, because the gnulib stat adjusts for

> >> timezones.

> >

> > There's one more potential issue with Gnulib's replacement of 'fstat':

> > it also replaces the definition of 'struct stat', and it does that in

> > a way that might yield incompatibility between the definition on

> > <sys/stat.h> the system header and Gnulib's sys/stat.h replacement.

> > If gdb_bfd.c uses the Gnulib definition of 'struct stat' (as I think

> > we do everywhere in gdb/), then this replacement might create problems

> > on MinGW similar to those I reported to the Gnulib list (see the URL I

> > cited in an earlier message), because bfd_stat uses an incompatible

> > definition of 'struct stat'.

> >

> > Of course, given that the Gnulib developers rejected my request not to

> > override the system definition of 'struct stat', GDB could also ignore

> > those problems, accepting their judgment.

>

> I think that we need to:

>

> - #undef stat before including bfd headers.

> - Redefine it afterwards back to rpl_stat (*)

> - add some kind of wrapper around bfd_stat (like maybe called gdb_bfd_stat)


Wouldn't it be easier to #define GNULIB_NAMESPACE in this file? That
way, the gnulib stuff stays in gnulib:: (or gdb::, or whatever), and
the global ::stat is the system one.

>   that handles the mismatch.  Something like:

>

> int

> gdb_bfd_stat (bfd *abfd, struct stat *statbuf)

> {

> #undef stat

>   struct stat st;

>

>   int res = bfd_stat (abfd, &st);

>   if (res != 0)

>     return res;

>

> #define COPY(FIELD) statbuf->FIELD = st.FIELD

>   COPY (st_dev);

>   // ... copy over all relevant fields ...

> #undef COPY

>

> #define stat rpl_stat

> }

>

> (*) there's probably some '#ifdef _GL...' we can check to know

> whether we need to do this.

>

> Thanks,

> Pedro Alves

>
Pedro Alves Jan. 16, 2020, 9:58 p.m. | #5
On 1/16/20 8:46 PM, Christian Biesinger via gdb-patches wrote:
> On Thu, Jan 16, 2020 at 3:37 PM Pedro Alves <palves@redhat.com> wrote:


>> I think that we need to:

>>

>> - #undef stat before including bfd headers.

>> - Redefine it afterwards back to rpl_stat (*)

>> - add some kind of wrapper around bfd_stat (like maybe called gdb_bfd_stat)

> 

> Wouldn't it be easier to #define GNULIB_NAMESPACE in this file? That

> way, the gnulib stuff stays in gnulib:: (or gdb::, or whatever), and

> the global ::stat is the system one.


Good idea.  I suspect it's better to do it in an isolated file than
in the whole symfile.c of whatever the problem was found, so that we
don't have to sprinkle the namespace prefix around.  I.e., in a file
that only implements gdb_bfd_stat.  At least, until have use a namespace
everywhere.  We could also poison bfd_stat so that we're sure nothing
in gdb uses it other than the wrapper.

Thanks,
Pedro Alves
Eli Zaretskii Jan. 17, 2020, 7:52 a.m. | #6
> Cc: gdb-patches@sourceware.org

> From: Pedro Alves <palves@redhat.com>

> Date: Thu, 16 Jan 2020 20:37:28 +0000

> 

> #define COPY(FIELD) statbuf->FIELD = st.FIELD

>   COPY (st_dev);

>   // ... copy over all relevant fields ...

> #undef COPY


Copying fields will work, but it would need some care.  For example,
the Gnulib replacement of 'struct stat' redefines st_size and st_mtime
to wider data types, so copying from the Gnulib definition to the
system definition might overflow.

Thanks.
Eli Zaretskii Jan. 17, 2020, 7:57 a.m. | #7
> From: Christian Biesinger <cbiesinger@google.com>

> Date: Thu, 16 Jan 2020 15:46:52 -0500

> Cc: Eli Zaretskii <eliz@gnu.org>, Tom Tromey <tromey@adacore.com>, 

> 	gdb-patches <gdb-patches@sourceware.org>

> 

> > - #undef stat before including bfd headers.

> > - Redefine it afterwards back to rpl_stat (*)

> > - add some kind of wrapper around bfd_stat (like maybe called gdb_bfd_stat)

> 

> Wouldn't it be easier to #define GNULIB_NAMESPACE in this file? That

> way, the gnulib stuff stays in gnulib:: (or gdb::, or whatever), and

> the global ::stat is the system one.


That would work, of course, but is there a chance we store some fields
of 'struct stat' in other data structures or objects, and then use
them elsewhere, for example for comparison with values received from
the Gnulib's 'stat' or 'fstat'?

And in any case, we will have to have a prominent commentary
explaining why we do such strange things.

I wonder whether a better way is not to import the Gnulib 'stat' and
'fstat' modules at all.  Are they required by other Gnulib modules,
and if so, by which ones?

Thanks.
Tom Tromey Jan. 17, 2020, 8:55 p.m. | #8
>>>>> "Tom" == Tom Tromey <tromey@adacore.com> writes:


>>>>> "Eli" == Eli Zaretskii <eliz@gnu.org> writes:

Eli> I wonder whether a better way is not to import the Gnulib 'stat' and
Eli> 'fstat' modules at all.  Are they required by other Gnulib modules,
Eli> and if so, by which ones?

Tom> I am wondering this as well.  While I think we can track down the bad
Tom> spots -- either calling the "wrong" stat or incorrectly comparing values
Tom> from the different stats (the patches I sent probably accomplish the
Tom> latter already) -- it seems fragile to rely on this.

It came in originally in a patch I sent and one that Yao sent:

https://sourceware.org/ml/gdb-patches/2013-11/msg00502.html
https://sourceware.org/ml/gdb-patches/2014-11/msg00654.html

I thought maybe removing these workarounds would be ok, but it seems
like it can't be done: when I remove stat and lstat from
update-gnulib.sh, they still appear.

Maybe --avoid=stat will work.

Tom
Eli Zaretskii Jan. 18, 2020, 7:58 a.m. | #9
> From: Tom Tromey <tromey@adacore.com>

> Cc: Eli Zaretskii <eliz@gnu.org>,  Christian Biesinger <cbiesinger@google.com>,  palves@redhat.com,  gdb-patches@sourceware.org

> Date: Fri, 17 Jan 2020 13:55:57 -0700

> 

> >>>>> "Tom" == Tom Tromey <tromey@adacore.com> writes:

> 

> >>>>> "Eli" == Eli Zaretskii <eliz@gnu.org> writes:

> Eli> I wonder whether a better way is not to import the Gnulib 'stat' and

> Eli> 'fstat' modules at all.  Are they required by other Gnulib modules,

> Eli> and if so, by which ones?

> 

> Tom> I am wondering this as well.  While I think we can track down the bad

> Tom> spots -- either calling the "wrong" stat or incorrectly comparing values

> Tom> from the different stats (the patches I sent probably accomplish the

> Tom> latter already) -- it seems fragile to rely on this.

> 

> It came in originally in a patch I sent and one that Yao sent:

> 

> https://sourceware.org/ml/gdb-patches/2013-11/msg00502.html


This one removed gdb_stat.h, which had replacements for the S_IS*
macros, something that should be easy to bring back, and doesn't
really justify replacing the functions and the struct's themselves.

> https://sourceware.org/ml/gdb-patches/2014-11/msg00654.html


This seems to be about using 'lstat' freely.  But I see only one call
to 'lstat' in our sources -- in symfile.c.  So maybe having our own
replacement, or even calling 'lstat' conditioned by an #ifdef, would
be a good-enough solution.

> I thought maybe removing these workarounds would be ok, but it seems

> like it can't be done: when I remove stat and lstat from

> update-gnulib.sh, they still appear.

> 

> Maybe --avoid=stat will work.


I guess this means some other Gnulib module pulls in 'stat', in which
case --avoid=stat is the way to try to avoid it, yes.  (My guess is
that the 'largefile' module causes 'stat' to be pulled in.)
Pedro Alves Jan. 20, 2020, 3:48 p.m. | #10
On 1/18/20 7:58 AM, Eli Zaretskii wrote:
>> From: Tom Tromey <tromey@adacore.com>

>> Cc: Eli Zaretskii <eliz@gnu.org>,  Christian Biesinger <cbiesinger@google.com>,  palves@redhat.com,  gdb-patches@sourceware.org

>> Date: Fri, 17 Jan 2020 13:55:57 -0700

>>

>>>>>>> "Tom" == Tom Tromey <tromey@adacore.com> writes:

>>

>>>>>>> "Eli" == Eli Zaretskii <eliz@gnu.org> writes:

>> Eli> I wonder whether a better way is not to import the Gnulib 'stat' and

>> Eli> 'fstat' modules at all.  Are they required by other Gnulib modules,

>> Eli> and if so, by which ones?

>>

>> Tom> I am wondering this as well.  While I think we can track down the bad

>> Tom> spots -- either calling the "wrong" stat or incorrectly comparing values

>> Tom> from the different stats (the patches I sent probably accomplish the

>> Tom> latter already) -- it seems fragile to rely on this.

>>

>> It came in originally in a patch I sent and one that Yao sent:

>>

>> https://sourceware.org/ml/gdb-patches/2013-11/msg00502.html

> 

> This one removed gdb_stat.h, which had replacements for the S_IS*

> macros, something that should be easy to bring back, and doesn't

> really justify replacing the functions and the struct's themselves.

> 

>> https://sourceware.org/ml/gdb-patches/2014-11/msg00654.html

> 

> This seems to be about using 'lstat' freely.  But I see only one call

> to 'lstat' in our sources -- in symfile.c.  So maybe having our own

> replacement, or even calling 'lstat' conditioned by an #ifdef, would

> be a good-enough solution.

> 

>> I thought maybe removing these workarounds would be ok, but it seems

>> like it can't be done: when I remove stat and lstat from

>> update-gnulib.sh, they still appear.

>>

>> Maybe --avoid=stat will work.

> 

> I guess this means some other Gnulib module pulls in 'stat', in which

> case --avoid=stat is the way to try to avoid it, yes.  (My guess is

> that the 'largefile' module causes 'stat' to be pulled in.)


I'm not sure about that solution -- won't --avoid=stat mean that
we disable any stat gnulib fix for all platforms, instead of just
for Windows?  We only have one lstat call, but we also use fstat, for
example, and I assume that these *stat modules in gnulib are all
intertwined.  Also, we may only have one lstat call nowadays, but
who knows about the future.

I played with a workaround along the lines of what I was suggesting
earlier, and I couldn't make it work in the forms discussed previously.

I did come up with a workaround though, it's just different.

I noticed that gnulib's sys/stat.h replacement starts with a way to
bypass it:

 #if defined __need_system_sys_stat_h
 /* Special invocation convention.  */

 #include_next <sys/stat.h>

 #else
 /* Normal invocation convention.  */

 #ifndef _GL_SYS_STAT_H

So I think we can take advantage of that to be able to make sure that
when we include "bfd.h", its functions are declared using the system's
stat, which is the same version that bfd is built against.
See prototype patch below, particularly common-types.h, which the
place where we include bfd.h for the first time.

It builds with my mingw cross compiler, the remaining issue would be
looking more in detail to the to_sys_stat conversion function.

I've also attached a gnulib fix for an issue I ran into, which will
need to go upstream.

From 3666298dcbdb817dbd5603dd2073e5788c67cac1 Mon Sep 17 00:00:00 2001
From: Pedro Alves <palves@redhat.com>

Date: Mon, 20 Jan 2020 15:40:54 +0000
Subject: [PATCH 1/2] Handle different "struct stat" between GDB and BFD

---
 gdb/defs.h                |  1 -
 gdb/gdb_bfd.c             | 45 +++++++++++++++++++++++++++++++++++++++++----
 gdb/gdb_bfd.h             |  2 +-
 gdb/jit.c                 |  4 ++--
 gdb/symfile.c             |  2 +-
 gdbsupport/common-types.h | 13 +++++++++++++
 6 files changed, 58 insertions(+), 9 deletions(-)

diff --git a/gdb/defs.h b/gdb/defs.h
index 1ad52feb1f8..f38b9dc6ff5 100644
--- a/gdb/defs.h
+++ b/gdb/defs.h
@@ -34,7 +34,6 @@
 #undef PACKAGE_TARNAME
 
 #include <config.h>
-#include "bfd.h"
 
 #include <sys/types.h>
 #include <limits.h>
diff --git a/gdb/gdb_bfd.c b/gdb/gdb_bfd.c
index 5a6dee2d51a..82b6a6bbaa2 100644
--- a/gdb/gdb_bfd.c
+++ b/gdb/gdb_bfd.c
@@ -66,7 +66,7 @@ struct gdb_bfd_data
       needs_relocations (0),
       crc_computed (0)
   {
-    struct stat buf;
+    sys_stat buf;
 
     if (bfd_stat (abfd, &buf) == 0)
       {
@@ -355,24 +355,61 @@ gdb_bfd_iovec_fileio_close (struct bfd *abfd, void *stream)
   return 0;
 }
 
+/* Convert between a struct stat (potentially a gnulib replacement)
+   and a sys_stat (the system's struct stat).  */
+
+static sys_stat
+to_sys_stat (struct stat *st)
+{
+  sys_stat sst {};
+
+#define COPY(FIELD) \
+  sst.FIELD = st->FIELD
+
+  COPY (st_dev);
+  COPY (st_ino);
+  COPY (st_mode);
+  COPY (st_nlink);
+  COPY (st_uid);
+  COPY (st_gid);
+  COPY (st_rdev);
+
+  /* Check for overflow?  */
+  COPY (st_size);
+
+  // Should probably check _GL_WINDOWS_STAT_TIMESPEC and refer to
+  // st_atim, etc. instead.
+  COPY (st_atime);
+  COPY (st_mtime);
+  COPY (st_ctime);
+
+#undef COPY
+
+  return sst;
+}
+
 /* Wrapper for target_fileio_fstat suitable for passing as the
    STAT_FUNC argument to gdb_bfd_openr_iovec.  */
 
 static int
 gdb_bfd_iovec_fileio_fstat (struct bfd *abfd, void *stream,
-			    struct stat *sb)
+			    sys_stat *sb)
 {
   int fd = *(int *) stream;
   int target_errno;
   int result;
 
-  result = target_fileio_fstat (fd, sb, &target_errno);
+  struct stat st;
+
+  result = target_fileio_fstat (fd, &st, &target_errno);
   if (result == -1)
     {
       errno = fileio_errno_to_host (target_errno);
       bfd_set_error (bfd_error_system_call);
     }
 
+  *sb = to_sys_stat (&st);
+
   return result;
 }
 
@@ -818,7 +855,7 @@ gdb_bfd_openr_iovec (const char *filename, const char *target,
 					void *stream),
 		     int (*stat_func) (struct bfd *abfd,
 				       void *stream,
-				       struct stat *sb))
+				       sys_stat *sb))
 {
   bfd *result = bfd_openr_iovec (filename, target,
 				 open_func, open_closure,
diff --git a/gdb/gdb_bfd.h b/gdb/gdb_bfd.h
index 9b1e292bf18..f0ad4814a80 100644
--- a/gdb/gdb_bfd.h
+++ b/gdb/gdb_bfd.h
@@ -154,7 +154,7 @@ gdb_bfd_ref_ptr gdb_bfd_openr_iovec (const char *filename, const char *target,
 							void *stream),
 				     int (*stat_func) (struct bfd *abfd,
 						       void *stream,
-						       struct stat *sb));
+						       sys_stat *sb));
 
 /* A wrapper for bfd_openr_next_archived_file that initializes the
    gdb-specific reference count.  */
diff --git a/gdb/jit.c b/gdb/jit.c
index eeaab70bfe0..976f8555250 100644
--- a/gdb/jit.c
+++ b/gdb/jit.c
@@ -124,11 +124,11 @@ mem_bfd_iovec_pread (struct bfd *abfd, void *stream, void *buf,
 /* For statting the file, we only support the st_size attribute.  */
 
 static int
-mem_bfd_iovec_stat (struct bfd *abfd, void *stream, struct stat *sb)
+mem_bfd_iovec_stat (struct bfd *abfd, void *stream, sys_stat *sb)
 {
   struct target_buffer *buffer = (struct target_buffer*) stream;
 
-  memset (sb, 0, sizeof (struct stat));
+  memset (sb, 0, sizeof (sys_stat));
   sb->st_size = buffer->size;
   return 0;
 }
diff --git a/gdb/symfile.c b/gdb/symfile.c
index f7bada75f35..65d342a53dd 100644
--- a/gdb/symfile.c
+++ b/gdb/symfile.c
@@ -1260,7 +1260,7 @@ separate_debug_file_exists (const std::string &name, unsigned long crc,
 {
   unsigned long file_crc;
   int file_crc_p;
-  struct stat parent_stat, abfd_stat;
+  sys_stat parent_stat, abfd_stat;
   int verified_as_different;
 
   /* Find a separate debug info file as if symbols would be present in
diff --git a/gdbsupport/common-types.h b/gdbsupport/common-types.h
index 61099b4e25d..8c136212c80 100644
--- a/gdbsupport/common-types.h
+++ b/gdbsupport/common-types.h
@@ -32,8 +32,21 @@ typedef unsigned long long ULONGEST;
 
 #else /* GDBSERVER */
 
+/* Gnulib may replace struct stat with its own version.  Bfd does not
+   use gnulib, so when we call into bfd, we must use the system struct
+   stat.  */
+#define __need_system_sys_stat_h 1
+
+#include <sys/stat.h>
+
 #include "bfd.h"
 
+typedef struct stat sys_stat;
+
+#undef __need_system_sys_stat_h
+
+#include <sys/stat.h>
+
 /* * A byte from the program being debugged.  */
 typedef bfd_byte gdb_byte;
 

base-commit: 26916852e189323593102561f5e3e2137b892dec
-- 
2.14.5
From 224fa934579ca3a1292c2e6a0377aaa9bfecc645 Mon Sep 17 00:00:00 2001
From: Pedro Alves <palves@redhat.com>

Date: Mon, 20 Jan 2020 15:40:55 +0000
Subject: [PATCH 2/2] Fix gnulib's lstat replacement in C++ namespace mode

Fixes:

 unittests/string_view-selftests.c: In member function 'gnulib::_gl_lstat_wrapper::operator gnulib::_gl_lstat_wrapper::type() const':
 unittests/string_view-selftests.c:11432:22: error: expected primary-expression before ';' token
      return ::rpl_stat;
		       ^

The problem is that the lstat replacement depends on the stat
(function) declaration, which is only declared afterwards.  The fix is
simply to declare lstat after stat.
---
 gnulib/import/sys_stat.in.h | 66 ++++++++++++++++++++++-----------------------
 1 file changed, 32 insertions(+), 34 deletions(-)

diff --git a/gnulib/import/sys_stat.in.h b/gnulib/import/sys_stat.in.h
index 9ddd1a8d004..537917b6a51 100644
--- a/gnulib/import/sys_stat.in.h
+++ b/gnulib/import/sys_stat.in.h
@@ -536,40 +536,6 @@ _GL_WARN_ON_USE (lchmod, "lchmod is unportable - "
 #endif
 
 
-#if @GNULIB_LSTAT@
-# if ! @HAVE_LSTAT@
-/* mingw does not support symlinks, therefore it does not have lstat.  But
-   without links, stat does just fine.  */
-#  if !(defined __cplusplus && defined GNULIB_NAMESPACE)
-#   define lstat stat
-#  endif
-_GL_CXXALIAS_RPL_1 (lstat, stat, int, (const char *name, struct stat *buf));
-# elif @REPLACE_LSTAT@
-#  if !(defined __cplusplus && defined GNULIB_NAMESPACE)
-#   undef lstat
-#   define lstat rpl_lstat
-#  endif
-_GL_FUNCDECL_RPL (lstat, int, (const char *name, struct stat *buf)
-                              _GL_ARG_NONNULL ((1, 2)));
-_GL_CXXALIAS_RPL (lstat, int, (const char *name, struct stat *buf));
-# else
-_GL_CXXALIAS_SYS (lstat, int, (const char *name, struct stat *buf));
-# endif
-# if @HAVE_LSTAT@
-_GL_CXXALIASWARN (lstat);
-# endif
-#elif @GNULIB_OVERRIDES_STRUCT_STAT@
-# undef lstat
-# define lstat lstat_used_without_requesting_gnulib_module_lstat
-#elif defined GNULIB_POSIXCHECK
-# undef lstat
-# if HAVE_RAW_DECL_LSTAT
-_GL_WARN_ON_USE (lstat, "lstat is unportable - "
-                 "use gnulib module lstat for portability");
-# endif
-#endif
-
-
 #if @REPLACE_MKDIR@
 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
 #  undef mkdir
@@ -781,6 +747,38 @@ _GL_WARN_ON_USE (stat, "stat is unportable - "
 # endif
 #endif
 
+#if @GNULIB_LSTAT@
+# if ! @HAVE_LSTAT@
+/* mingw does not support symlinks, therefore it does not have lstat.  But
+   without links, stat does just fine.  */
+#  if !(defined __cplusplus && defined GNULIB_NAMESPACE)
+#   define lstat stat
+#  endif
+_GL_CXXALIAS_RPL_1 (lstat, stat, int, (const char *name, struct stat *buf));
+# elif @REPLACE_LSTAT@
+#  if !(defined __cplusplus && defined GNULIB_NAMESPACE)
+#   undef lstat
+#   define lstat rpl_lstat
+#  endif
+_GL_FUNCDECL_RPL (lstat, int, (const char *name, struct stat *buf)
+                              _GL_ARG_NONNULL ((1, 2)));
+_GL_CXXALIAS_RPL (lstat, int, (const char *name, struct stat *buf));
+# else
+_GL_CXXALIAS_SYS (lstat, int, (const char *name, struct stat *buf));
+# endif
+# if @HAVE_LSTAT@
+_GL_CXXALIASWARN (lstat);
+# endif
+#elif @GNULIB_OVERRIDES_STRUCT_STAT@
+# undef lstat
+# define lstat lstat_used_without_requesting_gnulib_module_lstat
+#elif defined GNULIB_POSIXCHECK
+# undef lstat
+# if HAVE_RAW_DECL_LSTAT
+_GL_WARN_ON_USE (lstat, "lstat is unportable - "
+                 "use gnulib module lstat for portability");
+# endif
+#endif
 
 #if @GNULIB_UTIMENSAT@
 /* Use the rpl_ prefix also on Solaris <= 9, because on Solaris 9 our utimensat
-- 
2.14.5
Pedro Alves Jan. 20, 2020, 3:52 p.m. | #11
On 1/20/20 3:48 PM, Pedro Alves wrote:

> So I think we can take advantage of that to be able to make sure that

> when we include "bfd.h", its functions are declared using the system's

> stat, which is the same version that bfd is built against.

> See prototype patch below, particularly common-types.h, which the

> place where we include bfd.h for the first time.

> 

> It builds with my mingw cross compiler, the remaining issue would be

> looking more in detail to the to_sys_stat conversion function.

> 

> I've also attached a gnulib fix for an issue I ran into, which will

> need to go upstream.


Forgot to say that I put this on the users/palves/stat branch
if you'd like to try it & poke at it.

Thanks,
Pedro Alves
Eli Zaretskii Jan. 20, 2020, 5:28 p.m. | #12
> Cc: cbiesinger@google.com, gdb-patches@sourceware.org

> From: Pedro Alves <palves@redhat.com>

> Date: Mon, 20 Jan 2020 15:48:22 +0000

> 

> > I guess this means some other Gnulib module pulls in 'stat', in which

> > case --avoid=stat is the way to try to avoid it, yes.  (My guess is

> > that the 'largefile' module causes 'stat' to be pulled in.)

> 

> I'm not sure about that solution -- won't --avoid=stat mean that

> we disable any stat gnulib fix for all platforms, instead of just

> for Windows?


It would, but do we have any known problems with stat and fstat on
other platforms?

> We only have one lstat call, but we also use fstat, for example, and

> I assume that these *stat modules in gnulib are all intertwined.

> Also, we may only have one lstat call nowadays, but who knows about

> the future.


Even having a gdb_lstat for that purpose will be simpler and more
future-proof than the whole Gnulib stat module, I think.

> I did come up with a workaround though, it's just different.

> 

> I noticed that gnulib's sys/stat.h replacement starts with a way to

> bypass it:

> 

>  #if defined __need_system_sys_stat_h

>  /* Special invocation convention.  */

> 

>  #include_next <sys/stat.h>

> 

>  #else

>  /* Normal invocation convention.  */

> 

>  #ifndef _GL_SYS_STAT_H

> 

> So I think we can take advantage of that to be able to make sure that

> when we include "bfd.h", its functions are declared using the system's

> stat, which is the same version that bfd is built against.


But stat/fstat the functions will still shadow the system ones, would
they not?  And if they would, doesn't it mean subtle bugs where, e.g.,
the Gnulib replacement implementations rely on wide-enough st_size,
for example, or st_mtime?

Also, aren't some of the problems on platforms other than MinGW
resolved by the Gnulib sys/stat.h header, as opposed to the
replacement implementation of the functions themselves?

Thanks.
Pedro Alves Jan. 20, 2020, 8:50 p.m. | #13
On 1/20/20 5:28 PM, Eli Zaretskii wrote:
>> Cc: cbiesinger@google.com, gdb-patches@sourceware.org

>> From: Pedro Alves <palves@redhat.com>

>> Date: Mon, 20 Jan 2020 15:48:22 +0000

>>

>>> I guess this means some other Gnulib module pulls in 'stat', in which

>>> case --avoid=stat is the way to try to avoid it, yes.  (My guess is

>>> that the 'largefile' module causes 'stat' to be pulled in.)

>>

>> I'm not sure about that solution -- won't --avoid=stat mean that

>> we disable any stat gnulib fix for all platforms, instead of just

>> for Windows?

> 

> It would, but do we have any known problems with stat and fstat on

> other platforms?


There's the list of known problems in the gnulib pages:

 https://www.gnu.org/software/gnulib/manual/html_node/fstat.html
 https://www.gnu.org/software/gnulib/manual/html_node/stat.html
 https://www.gnu.org/software/gnulib/manual/html_node/lstat.html

> 

>> We only have one lstat call, but we also use fstat, for example, and

>> I assume that these *stat modules in gnulib are all intertwined.

>> Also, we may only have one lstat call nowadays, but who knows about

>> the future.

> 

> Even having a gdb_lstat for that purpose will be simpler and more

> future-proof than the whole Gnulib stat module, I think.


I think one could use that argument for any piece of gnulib in
isolation.  But fighting against use of some particular modules
ends up creating a larger burden over time IMO.  I'd rather avoid
doing that if possible.

> 

>> I did come up with a workaround though, it's just different.

>>

>> I noticed that gnulib's sys/stat.h replacement starts with a way to

>> bypass it:

>>

>>  #if defined __need_system_sys_stat_h

>>  /* Special invocation convention.  */

>>

>>  #include_next <sys/stat.h>

>>

>>  #else

>>  /* Normal invocation convention.  */

>>

>>  #ifndef _GL_SYS_STAT_H

>>

>> So I think we can take advantage of that to be able to make sure that

>> when we include "bfd.h", its functions are declared using the system's

>> stat, which is the same version that bfd is built against.

> 

> But stat/fstat the functions will still shadow the system ones, would

> they not?  


Let's look at the patch:

 --- c/gdbsupport/common-types.h
 +++ w/gdbsupport/common-types.h
 @@ -32,8 +32,21 @@ typedef unsigned long long ULONGEST;
  
  #else /* GDBSERVER */
  
 +/* Gnulib may replace struct stat with its own version.  Bfd does not
 +   use gnulib, so when we call into bfd, we must use the system struct
 +   stat.  */
 +#define __need_system_sys_stat_h 1
 +
 +#include <sys/stat.h>
 +
  #include "bfd.h"
  
 +typedef struct stat sys_stat;
 +
 +#undef __need_system_sys_stat_h
 +
 +#include <sys/stat.h>

Currently, without that patch, because of the gnulib struct stat
replacement, when we include bfd.h, we end up with the following
function declared, if you expand the preprocessor macros:

  extern int bfd_stat (bfd *, struct rpl_stat *);

This is incorrect, because that bfd function was not defined
that way.  It is instead written as:

  extern int bfd_stat (bfd *, struct stat *);

Given the stat replacement, we pass it a rpl_stat pointer, when it
is in reality expecting a "struct stat" one (or whatever that expands
in the system headers).  So we get undefined behavior at run time.

By defining __need_system_sys_stat_h just before bfd.h is included,
we're sure that bfd's bfd_stat is declared with the system's
stat, just like when bfd itself was compiled.

  extern int bfd_stat (bfd *, struct stat *);

We undef  __need_system_sys_stat_h again just after including
"bfd.h", so that the gdb uses the gnulib struct stat.  But we
also create the sys_stat typedef so that we can refer to the
system's stat type after __need_system_sys_stat_h is undefined
and gnulib's stat replacement is visible.

So the *stat functions will be shadowed by the gnulib ones
within gdb, yes.  But we also get a compile error if we
call bfd_stat with a masked struct stat:

 binutils-gdb/src/gdb/gdb_bfd.c: In constructor 'gdb_bfd_data::gdb_bfd_data(bfd*)':
 binutils-gdb/src/gdb/gdb_bfd.c:72:29: error: cannot convert 'rpl_stat*' to '_stat64*' for argument '2' to 'int bfd_stat(bfd*, _stat64*)'
      if (bfd_stat (abfd, &buf) == 0)
                              ^

> And if they would, doesn't it mean subtle bugs where, e.g.,

> the Gnulib replacement implementations rely on wide-enough st_size,

> for example, or st_mtime?


I'm not sure what you mean here.  When the replacement implementations
are called, they're passed a replaced struct stat pointer too.  It's
only while the bfd.h header is being compiled that the gnulib replacements
aren't visible.

> Also, aren't some of the problems on platforms other than MinGW

> resolved by the Gnulib sys/stat.h header, as opposed to the

> replacement implementation of the functions themselves?


Some yes, but not all.  But it's the sys/stat.h header replacement
that redefines struct stat, so I don't see the point you're making.

Now, the solution I came up with is reusable for any other library we
may end up depending on that is built with a different struct stat
and requires passing a struct stat in one of its entry pointers.

However, with all that said, bfd is always built along with gdb, so
we have a higher degree of control.  Maybe a better solution for
this is really to make sure that bfd is compiled with largefile
support, as suggested in the bug-gnulib discussion.

So far, I was under the impression that you're reaching the
GNULIB_defined_struct_stat code, where gnulib defines its own
struct stat.  But reading your description of the bug in gnulib
again, I see you're actually getting stat defined to _stati64.

So perhaps what we need is to enable largefile support by
default on bfd for mingw, to force use of the 64-bit stat?
Does the original issue go away if you configure
with --enable-largefile?  Maybe we need a mingw-specific
tweak in gdb's src/config/largefile.m4?

Looking my mingw-w64's stat.h, I see:

 #if defined(_FILE_OFFSET_BITS) && (_FILE_OFFSET_BITS == 64)
 #ifdef _USE_32BIT_TIME_T
 #define stat _stat32i64
 #define fstat _fstat32i64
 #else
 #define stat _stat64
 #define fstat _fstat64
 #endif
 #endif

So if BFD is compiled with _FILE_OFFSET_BITS == 64 and
_USE_32BIT_TIME_T is not defined, the mismatch ends up going
away?  Is there a reason we _wouldn't_ want to enable largefile
support in bfd?

Thanks,
Pedro Alves
Pedro Alves Jan. 21, 2020, 1:47 p.m. | #14
On 1/20/20 8:50 PM, Pedro Alves wrote:

> So if BFD is compiled with _FILE_OFFSET_BITS == 64 and

> _USE_32BIT_TIME_T is not defined, the mismatch ends up going

> away?  Is there a reason we _wouldn't_ want to enable largefile

> support in bfd?


I'm looking at this some more, and am trying to understand
what is really going on.  I can't seem to reproduce your original
issue, I think because I'm using (32-bit) mingw-w64, while the issue
with 32-bit size_t happen on (32-bit) mingw.org instead.  Correct?
But re-reading your description of the problem on bug-gnulib,
I think I get it.

BFD already uses AC_SYS_LARGEFILE, wrapped in ACX_LARGEFILE
(config/largefile.m4) due to a Solaris issue.  Actually,
the whole tree uses that -- ld, binutils, bfd, gdb, etc.,
even our toplevel gnulib/ directory's configure.ac calls it.
But then, the gnulib/import/

So maybe the best to do here is to import gnulib with 
--avoid=largefile, and let the ACX_LARGEFILE in gnulib/'s
top configure handle the enabling largefile support in sync
with all other top level dirs.  I tried that,
and confirmed that _FILE_OFFSET_BITS=64 still ends up in
gnulib's config.h.  The build then fails inside gnulib
for me on 32-bit mingw-w64, maybe there's a bug that needs
to be fixed, but I'd think this _should_ work.

See the users/palves/gnulib-largefile branch.

Thanks,
Pedro Alves
Eli Zaretskii Jan. 21, 2020, 5:34 p.m. | #15
> Cc: tromey@adacore.com, cbiesinger@google.com, gdb-patches@sourceware.org

> From: Pedro Alves <palves@redhat.com>

> Date: Mon, 20 Jan 2020 20:50:17 +0000

> 

> >> I'm not sure about that solution -- won't --avoid=stat mean that

> >> we disable any stat gnulib fix for all platforms, instead of just

> >> for Windows?

> > 

> > It would, but do we have any known problems with stat and fstat on

> > other platforms?

> 

> There's the list of known problems in the gnulib pages:

> 

>  https://www.gnu.org/software/gnulib/manual/html_node/fstat.html


This one is only about Solaris problems with st_mtime etc.  (MSVC
problems are of no concern to us.)

>  https://www.gnu.org/software/gnulib/manual/html_node/stat.html


Is the trailing slash issue important for GDB?

>  https://www.gnu.org/software/gnulib/manual/html_node/lstat.html


Likewise.

All in all, the gain sounds small to me, if at all.

> >> We only have one lstat call, but we also use fstat, for example, and

> >> I assume that these *stat modules in gnulib are all intertwined.

> >> Also, we may only have one lstat call nowadays, but who knows about

> >> the future.

> > 

> > Even having a gdb_lstat for that purpose will be simpler and more

> > future-proof than the whole Gnulib stat module, I think.

> 

> I think one could use that argument for any piece of gnulib in

> isolation.


I wouldn't say "any piece", there are some functions there with very
non-trivial guts.  But yes, the impact of Gnulib IME is mostly
important where it provides missing functions, not where it replaces
existing ones.

> But fighting against use of some particular modules ends up creating

> a larger burden over time IMO.  I'd rather avoid doing that if

> possible.


Sure, this goes without saying.  I was just considering this as one
alternative, since we don't seem to have a much better one.  Or do we?

>   extern int bfd_stat (bfd *, struct rpl_stat *);

> 

> This is incorrect, because that bfd function was not defined

> that way.  It is instead written as:

> 

>   extern int bfd_stat (bfd *, struct stat *);

> 

> Given the stat replacement, we pass it a rpl_stat pointer, when it

> is in reality expecting a "struct stat" one (or whatever that expands

> in the system headers).  So we get undefined behavior at run time.

> 

> By defining __need_system_sys_stat_h just before bfd.h is included,

> we're sure that bfd's bfd_stat is declared with the system's

> stat, just like when bfd itself was compiled.

> 

>   extern int bfd_stat (bfd *, struct stat *);

> 

> We undef  __need_system_sys_stat_h again just after including

> "bfd.h", so that the gdb uses the gnulib struct stat.  But we

> also create the sys_stat typedef so that we can refer to the

> system's stat type after __need_system_sys_stat_h is undefined

> and gnulib's stat replacement is visible.

> 

> So the *stat functions will be shadowed by the gnulib ones

> within gdb, yes.  But we also get a compile error if we

> call bfd_stat with a masked struct stat:


OK, I see.  This would work, of course, but IME solutions based on
specific order of inclusion of header files are fragile, and break
easily, because header files tend to include other header files and
break the order you carefully observed.  But if there's no better
alternative, perhaps this is the way to go.

> > Also, aren't some of the problems on platforms other than MinGW

> > resolved by the Gnulib sys/stat.h header, as opposed to the

> > replacement implementation of the functions themselves?

> 

> Some yes, but not all.  But it's the sys/stat.h header replacement

> that redefines struct stat, so I don't see the point you're making.


And you are sure that including first the system's stat.h and then the
Gnulib's version of it will never cause any compilation problems?

Also, if GDB uses values based on what bfd_stat retrieves, then these
values might be different from what the Gnulib replacement stat
retrieves in GDB's own code for the same file (due to size of the
fields), right?  Are we sure we never compare those expecting them to
match for the same file?

> So perhaps what we need is to enable largefile support by

> default on bfd for mingw, to force use of the 64-bit stat?

> Does the original issue go away if you configure

> with --enable-largefile?  Maybe we need a mingw-specific

> tweak in gdb's src/config/largefile.m4?

> 

> Looking my mingw-w64's stat.h, I see:

> 

>  #if defined(_FILE_OFFSET_BITS) && (_FILE_OFFSET_BITS == 64)

>  #ifdef _USE_32BIT_TIME_T

>  #define stat _stat32i64

>  #define fstat _fstat32i64

>  #else

>  #define stat _stat64

>  #define fstat _fstat64

>  #endif

>  #endif

> 

> So if BFD is compiled with _FILE_OFFSET_BITS == 64 and

> _USE_32BIT_TIME_T is not defined, the mismatch ends up going

> away?  Is there a reason we _wouldn't_ want to enable largefile

> support in bfd?


That's a non-starter for me, as I will explain in response to your
further message.
Eli Zaretskii Jan. 21, 2020, 5:56 p.m. | #16
> Cc: tromey@adacore.com, cbiesinger@google.com, gdb-patches@sourceware.org

> From: Pedro Alves <palves@redhat.com>

> Date: Tue, 21 Jan 2020 13:47:28 +0000

> 

> On 1/20/20 8:50 PM, Pedro Alves wrote:

> 

> > So if BFD is compiled with _FILE_OFFSET_BITS == 64 and

> > _USE_32BIT_TIME_T is not defined, the mismatch ends up going

> > away?  Is there a reason we _wouldn't_ want to enable largefile

> > support in bfd?

> 

> I'm looking at this some more, and am trying to understand

> what is really going on.  I can't seem to reproduce your original

> issue, I think because I'm using (32-bit) mingw-w64, while the issue

> with 32-bit size_t happen on (32-bit) mingw.org instead.  Correct?


It isn't only st_size, it's also the width of the st_?time fields.

> So maybe the best to do here is to import gnulib with 

> --avoid=largefile, and let the ACX_LARGEFILE in gnulib/'s

> top configure handle the enabling largefile support in sync

> with all other top level dirs.  I tried that,

> and confirmed that _FILE_OFFSET_BITS=64 still ends up in

> gnulib's config.h.  The build then fails inside gnulib

> for me on 32-bit mingw-w64, maybe there's a bug that needs

> to be fixed, but I'd think this _should_ work.


AFAIU, largefile is intentionally requested in all MinGW builds, the
Gnulib developers explicitly said that was their intent.  And
largefile then causes the replacement of st_size etc. in struct stat,
for consistency.  MinGW64 doesn't need all that stuff at all, because
their defaults are already set to use 64-bit st_size and 64-bit time_t
fields.  That's because MinGW64 tossed support for Windows version
before Vista long ago, and therefore the incompatible changes
Microsoft introduced into MSVCRT.DLL from Vista onwards are of no
importance for MinGW64 (and the fragment from their stat.h you've
shown is by now just useless ballast that is AFAIU never used in
MinGW64).

But mingw.org's MinGW still supports the older Windows versions, and
the only sane way of doing that is to use 32-bit time_t, which
basically means one needs to use the regular implementation of stat,
not _stati64 or any other variety.  Moreover, using _stati64 instead
of stat, as MinGW64 does by default (and Gnulib forces the same on any
other MinGW build), is a non-starter for me, because _stati64 didn't
exist before Vista.  So a GDB built that way will simply refuse to run
on any older system, even if you build it on a Windows box that does
have _stati64.

Having said all that, let me explicitly say that I don't want to put a
drag on GDB development, and therefore will not insist that solutions
for this kind of problems must compile cleanly with my MinGW
toolchain.  That is why I never bothered to say anything here about
the struct stat problem, and only posted that to the Gnulib list.  I
know how to hack the build to make it DTRT for mingw.org's MinGW; I
did that with the pretest, as soon as I found out the reason for the
breakage (in a nutshell, remote debugging with gdbserver was
completely broken: GDB would crash as soon as you run the remote
target).  As long as the problem was limited to mingw.org's MinGW, I
kept silence here, and only broke that silence because Tom uncovered
what seems to be a similar problem, but one that affects MinGW64 as
well.

Bottom line: if you have a satisfactory solution for MinGW64, go for
it regardless of what it will mean for the MinGW I use; I don't want
to complicate this stuff any more than it already is, given that the
Gnulib developers rejected what I consider the simplest and the most
reliable solution (which would have seamlessly satisfied both
varieties of MinGW).  Now that I'm aware of the problems with the
Gnulib stat replacements, I can work around that very easily.

Thanks.
Tom Tromey Jan. 23, 2020, 10:03 p.m. | #17
>>>>> "Pedro" == Pedro Alves <palves@redhat.com> writes:


Pedro> I'm not sure about that solution -- won't --avoid=stat mean that
Pedro> we disable any stat gnulib fix for all platforms, instead of just
Pedro> for Windows?

Yeah.

Actually, there is one more option for us, which is to patch gnulib
in-tree.  We already have the machinery to do this.

Pedro> So I think we can take advantage of that to be able to make sure that
Pedro> when we include "bfd.h", its functions are declared using the system's
Pedro> stat, which is the same version that bfd is built against.
Pedro> See prototype patch below, particularly common-types.h, which the
Pedro> place where we include bfd.h for the first time.

Pedro> It builds with my mingw cross compiler, the remaining issue would be
Pedro> looking more in detail to the to_sys_stat conversion function.

This looks reasonably promising to me.

Tom
Tom Tromey April 1, 2020, 8:20 p.m. | #18
>>>>> "Pedro" == Pedro Alves <palves@redhat.com> writes:


[ system stat -vs- gnulib stat ]

Pedro> I think that we need to:

Pedro> - #undef stat before including bfd headers.
Pedro> - Redefine it afterwards back to rpl_stat (*)
Pedro> - add some kind of wrapper around bfd_stat (like maybe called gdb_bfd_stat)
Pedro>   that handles the mismatch.  Something like:

I read the thread again but I found it hard to figure out which
alternative of the ones presented is preferred.

Could you tell me which one it is?
I'd like to update these patches and fix the caching bug as well.

thanks,
Tom

Patch

diff --git a/gdb/gdb_bfd.c b/gdb/gdb_bfd.c
index a1ee7814f32..26968396a15 100644
--- a/gdb/gdb_bfd.c
+++ b/gdb/gdb_bfd.c
@@ -442,8 +442,15 @@  gdb_bfd_open (const char *name, const char *target, int fd)
 	}
     }
 
+  /* We open the BFD here so that we can use BFD to get the mtime.
+     This is important because gdb uses the gnulib stat, but BFD does
+     not, and this can lead to differences on some systems.  */
+  abfd = bfd_fopen (name, target, FOPEN_RB, fd);
+  if (abfd == NULL)
+    return NULL;
+
   search.filename = name;
-  if (fstat (fd, &st) < 0)
+  if (bfd_stat (abfd, &st) < 0)
     {
       /* Weird situation here.  */
       search.mtime = 0;
@@ -461,38 +468,32 @@  gdb_bfd_open (const char *name, const char *target, int fd)
 
   /* Note that this must compute the same result as hash_bfd.  */
   hash = htab_hash_string (name);
-  /* Note that we cannot use htab_find_slot_with_hash here, because
-     opening the BFD may fail; and this would violate hashtab
-     invariants.  */
-  abfd = (struct bfd *) htab_find_with_hash (gdb_bfd_cache, &search, hash);
-  if (bfd_sharing && abfd != NULL)
+
+  if (bfd_sharing)
     {
-      if (debug_bfd_cache)
-	fprintf_unfiltered (gdb_stdlog,
-			    "Reusing cached bfd %s for %s\n",
-			    host_address_to_string (abfd),
-			    bfd_get_filename (abfd));
-      close (fd);
-      return gdb_bfd_ref_ptr::new_reference (abfd);
+      slot = htab_find_slot_with_hash (gdb_bfd_cache, &search, hash, INSERT);
+      if (*slot != nullptr)
+	{
+	  bfd_close (abfd);
+	  abfd = (bfd *) *slot;
+	  if (debug_bfd_cache)
+	    fprintf_unfiltered (gdb_stdlog,
+				"Reusing cached bfd %s for %s\n",
+				host_address_to_string (abfd),
+				bfd_get_filename (abfd));
+	  close (fd);
+	  return gdb_bfd_ref_ptr::new_reference (abfd);
+	}
+      else
+	*slot = abfd;
     }
 
-  abfd = bfd_fopen (name, target, FOPEN_RB, fd);
-  if (abfd == NULL)
-    return NULL;
-
   if (debug_bfd_cache)
     fprintf_unfiltered (gdb_stdlog,
 			"Creating new bfd %s for %s\n",
 			host_address_to_string (abfd),
 			bfd_get_filename (abfd));
 
-  if (bfd_sharing)
-    {
-      slot = htab_find_slot_with_hash (gdb_bfd_cache, &search, hash, INSERT);
-      gdb_assert (!*slot);
-      *slot = abfd;
-    }
-
   /* It's important to pass the already-computed mtime here, rather
      than, say, calling gdb_bfd_ref_ptr::new_reference here.  BFD by
      default will "stat" the file each time bfd_get_mtime is called --
diff --git a/gdb/symfile.c b/gdb/symfile.c
index f7bada75f35..18995351ed3 100644
--- a/gdb/symfile.c
+++ b/gdb/symfile.c
@@ -2456,10 +2456,7 @@  reread_symbols (void)
 	 `ar', often called a `static library' on most systems, though
 	 a `shared library' on AIX is also an archive), then you should
 	 stat on the archive name, not member name.  */
-      if (objfile->obfd->my_archive)
-	res = stat (objfile->obfd->my_archive->filename, &new_statbuf);
-      else
-	res = stat (objfile_name (objfile), &new_statbuf);
+      res = bfd_stat (objfile->obfd, &new_statbuf);
       if (res != 0)
 	{
 	  /* FIXME, should use print_sys_errmsg but it's not filtered.  */