public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
From: David Edelsohn <dje.gcc@gmail.com>
To: Richard Purdie <richard.purdie@linuxfoundation.org>
Cc: GCC Development <gcc@gcc.gnu.org>, Khem Raj <raj.khem@gmail.com>
Subject: Re: Reproducible builds - supporting relative paths in *-prefix-map
Date: Mon, 15 Aug 2022 10:36:13 -0400	[thread overview]
Message-ID: <CAGWvnykFsQzgmUSiS4VyVjPf_dVUVOjCHEoapPDDmj-WXOjZsg@mail.gmail.com> (raw)
In-Reply-To: <dcd80163ee635803aaf0a0b4aaa86600e9b1591c.camel@linuxfoundation.org>

On Mon, Aug 15, 2022 at 10:28 AM Richard Purdie via Gcc <gcc@gcc.gnu.org> wrote:
>
> On Mon, 2022-08-15 at 12:13 +0100, Richard Purdie via Gcc wrote:
> > Hi,
> >
> > I'm wondering if we'd be able to improve path handling in the -f*-
> > prefix-map compiler options to cover relative paths?
> >
> > Currently it works well for absolute paths but if a file uses a
> > relative path or a path with a symlink in, or a non-absolute path, it
> > will miss those cases. For relative paths in particular it is
> > problematic as you can't easily construct a compiler commandline that
> > would cover all relative path options.
> >
> > At first glance this is relatively straight forward, for example:
> >
> > Index: gcc-12.1.0/gcc/file-prefix-map.cc
> > ===================================================================
> > --- gcc-12.1.0.orig/gcc/file-prefix-map.cc
> > +++ gcc-12.1.0/gcc/file-prefix-map.cc
> > @@ -70,19 +70,25 @@ remap_filename (file_prefix_map *maps, c
> >    file_prefix_map *map;
> >    char *s;
> >    const char *name;
> > +  char *realname;
> >    size_t name_len;
> >
> > +  realname = lrealpath (filename);
> > +
> >    for (map = maps; map; map = map->next)
> > -    if (filename_ncmp (filename, map->old_prefix, map->old_len) == 0)
> > +    if (filename_ncmp (realname, map->old_prefix, map->old_len) == 0)
> >        break;
> > -  if (!map)
> > +  if (!map) {
> > +    free (realname);
> >      return filename;
> > -  name = filename + map->old_len;
> > +  }
> > +  name = realname + map->old_len;
> >    name_len = strlen (name) + 1;
> >
> >    s = (char *) ggc_alloc_atomic (name_len + map->new_len);
> >    memcpy (s, map->new_prefix, map->new_len);
> >    memcpy (s + map->new_len, name, name_len);
> > +  free (realname);
> >    return s;
> >  }
> >
> > which address a realpath() call into the prefix mapping code. I did
> > experiment with this and found it breaks compiling ruby and xen-tools
> > which both have code which does:
> >
> > #include __FILE__
> >
> > It may be possible to make the remapping conditional of not being
> > directly in a #include statement but I didn't find the gcc code
> > responsible for that as yet. I also noticed some valgrind tests fails
> > after it, I've not looked into why that would be yet.
> >
> > I wanted to ask if there would be any interest in adding support for
> > something like this? I suspect the include/__FILE__ issue is probably a
> > latent bug anyway. If anyone has any pointers to the code I could
> > improve my patch with I'm also happy to have them!
>
> To answer my own question, something like:
>
> +Index: gcc-12.1.0/libcpp/macro.cc
> +===================================================================
> +--- gcc-12.1.0.orig/libcpp/macro.cc
> ++++ gcc-12.1.0/libcpp/macro.cc
> +@@ -563,7 +563,7 @@ _cpp_builtin_macro_text (cpp_reader *pfi
> +           if (!name)
> +             abort ();
> +         }
> +-      if (pfile->cb.remap_filename)
> ++      if (pfile->cb.remap_filename && !pfile->state.in_directive)
> +         name = pfile->cb.remap_filename (name);
> +       len = strlen (name);
> +       buf = _cpp_unaligned_alloc (pfile, len * 2 + 3);
>
> seems to do roughly what I was wondering about.
>
> I'd be interested to understand whether some patch along the lines I've
> mentioned here would stand a chance of being accepted or not.

Thanks for recognizing this issue and proposing a solution.  It's
probably more effective to submit this as an actual patch to
gcc-patches and cc David Malcolm, libcpp maintainer, than to ask
hypotheticals on the GCC mailing list.

Thanks, David

  reply	other threads:[~2022-08-15 14:37 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-08-15 11:13 Richard Purdie
2022-08-15 14:26 ` Richard Purdie
2022-08-15 14:36   ` David Edelsohn [this message]
2022-08-15 17:15 ` Joseph Myers
2022-08-15 19:51   ` Richard Purdie
2022-08-15 19:55 ` Mark Wielaard
2022-08-15 20:29   ` Richard Purdie
2022-08-17 11:23     ` Mark Wielaard
2022-08-17 18:06       ` Richard Purdie

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=CAGWvnykFsQzgmUSiS4VyVjPf_dVUVOjCHEoapPDDmj-WXOjZsg@mail.gmail.com \
    --to=dje.gcc@gmail.com \
    --cc=gcc@gcc.gnu.org \
    --cc=raj.khem@gmail.com \
    --cc=richard.purdie@linuxfoundation.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).