[Support] Fix getMainExecutable on FreeBSD when called via an absolute path
authorJessica Clarke <jrtc27@jrtc27.com>
Sat, 29 May 2021 13:59:46 +0000 (14:59 +0100)
committerJessica Clarke <jrtc27@jrtc27.com>
Sat, 29 May 2021 13:59:46 +0000 (14:59 +0100)
commit762f707c0072abba60b5b4bdbb64c2c69c6152b1
treef56eee1aec5a0b2bfa5be7906a4da675825a8891
parentaa9a30b83a06e3e5e68e32ea645ec2d9edc27efc
[Support] Fix getMainExecutable on FreeBSD when called via an absolute path

On FreeBSD, absolute paths are passed unmodified in AT_EXECPATH, but
relative paths are resolved to absolute paths, and any symlinks will be
followed in the process. This means that the resource dir calculation
will be wrong if Clang is invoked as an absolute path to a symlink, and
this currently causes clang/test/Driver/rocm-detect.hip to fail on
FreeBSD. Thus, make sure to call realpath on the result, just like is
done on macOS.

Whilst here, clean up the old fallback auxargs loop to use the actual
type for auxargs rather than using lots of hacky casts that rely on
addresses and pointers being the same (which is not the case on CHERI,
and thus Arm's prototype Morello, although for little-endian systems it
happens to work still as the word-sized integer will be padded to a full
pointer, and it's someone academic given dereferencing past the end of
environ will give a bounds fault, but CheriBSD is new enough that the
elf_aux_info path will be used). This also makes the code easier to
follow, and removes the confusing double-increment of p.

Reviewed By: dim, arichardson

Differential Revision: https://reviews.llvm.org/D103346
llvm/lib/Support/Unix/Path.inc