Re: [PATCH 2/3] Consistently use BFD's time

Pedro Alves
2020-01-20T15:48:56Z

[-- Attachment #1: Type: text/plain, Size: 8320 bytes --]

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

[-- Attachment #2: 0002-Fix-gnulib-s-lstat-replacement-in-C-namespace-mode.patch --]
[-- Type: text/x-patch, Size: 3622 bytes --]

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