fix #14972; make `searchForLibUsingGcc` verify it returns a file, if gcc claims it found one.
Needs RevisionPublic

Authored by angerman on Apr 1 2018, 2:39 AM.

Details

Reviewers
ezyang
bgamari
hvr
Trac Issues
#14972
angerman created this revision.Apr 1 2018, 2:39 AM

@bgamari we do probably want to backport this into 8.4 as well.

ezyang added a comment.Apr 1 2018, 9:59 AM

It might be clever to have a -v3/-v4 log message when the file doesn't exist. Otherwise LGTM.

angerman requested review of this revision.Apr 2 2018, 11:53 PM
Phyx added a subscriber: Phyx.EditedApr 3 2018, 3:00 PM

I don't really understand this fix, under what circumstances would GCC return a file that doesn't exist?

The whole point of --print-file-name is to print what would be used for linking. The driver code in GCC already attempt to open the file for reading and would only then return it if the file exists and has the right permissions (so it's a stronger check than what is being done here).

https://github.com/gcc-mirror/gcc/blob/331e56ed8e4ac1adca1e1ecfcd1220f9aaf8673a/gcc/gcc.c#L2758

It would a bit pointless for it to return something the linker couldn't use as that would just error out.

So I'm trying to understand how GCC can find a file for reading yet ghci cant?

In D4553#126335, @Phyx wrote:

I don't really understand this fix, under what circumstances would GCC return a file that doesn't exist?

The whole point of --print-file-name is to print what would be used for linking. The driver code in GCC already attempt to open the file for reading and would only then return it if the file exists and has the right permissions (so it's a stronger check than what is being done here).

https://github.com/gcc-mirror/gcc/blob/331e56ed8e4ac1adca1e1ecfcd1220f9aaf8673a/gcc/gcc.c#L2758

It would a bit pointless for it to return something the linker couldn't use as that would just error out.

So I'm trying to understand how GCC can find a file for reading yet ghci cant?

Let me indulge you in some fun :-)

Setting the stage:

$ tree
.
└── gmp

1 directory, 0 files

And torturing gcc

$ gcc -B$PWD --print-file-name gmp 
/Users/angerman/tmpl/gmp

But... the question we all have been asking:

$ gcc --version
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 9.0.0 (clang-900.0.39.2)
Target: x86_64-apple-darwin16.7.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin

It's gcc in disguise! ;-) But is it clang that's at fault here?

I've only got a cross gcc at hand:

$ /nix/store/casycyxaf99445k2xmkpi3xcn2k5rfvb-gcc-7.3.0-x86_64-pc-mingw32-stage-static/bin/x86_64-pc-mingw32-gcc --version                  
x86_64-pc-mingw32-gcc (GCC) 7.3.0
Copyright (C) 2017 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Let's see what it will report...

$ /nix/store/casycyxaf99445k2xmkpi3xcn2k5rfvb-gcc-7.3.0-x86_64-pc-mingw32-stage-static/bin/x86_64-pc-mingw32-gcc -B$PWD --print-file-name gmp
/Users/angerman/tmpl/gmp

😱 Oh no!


Note: this is all on macOS.

Phyx added a comment.Apr 6 2018, 6:01 AM

Right, the problem is that gcc is referring to the posix definition of a file. Where directories are just special files. And the driver later filters them out.

So only on platforms that are posix complaint will directories be returned.

It sucks having to make askGCC even more expensive, but luckily it's only used as a last resort nowadays.

bgamari requested changes to this revision.Apr 19 2018, 11:05 AM
bgamari added inline comments.
compiler/ghci/Linker.hs
1480

This really needs a comment, if not a note.

This revision now requires changes to proceed.Apr 19 2018, 11:05 AM