summaryrefslogtreecommitdiffstats
path: root/llvm/lib/Support/Unix/Path.inc
diff options
context:
space:
mode:
authorDavid Chisnall <csdavec@swan.ac.uk>2019-04-29 09:24:51 +0000
committerDavid Chisnall <csdavec@swan.ac.uk>2019-04-29 09:24:51 +0000
commit714a4425de37c9e5d592ba29b5aad1b55761051d (patch)
treedd230e8bfcd46a21c734cf280bcb5de34ee2ffb5 /llvm/lib/Support/Unix/Path.inc
parent055aee1d8a75e9414d0662c4d9f4b8f0393f6100 (diff)
downloadbcm5719-llvm-714a4425de37c9e5d592ba29b5aad1b55761051d.tar.gz
bcm5719-llvm-714a4425de37c9e5d592ba29b5aad1b55761051d.zip
Try to use /proc on FreeBSD for getExecutablePath
Currently, clang's libTooling passes this function a fake argv0, which means that no libTooling tools can find the standard headers on FreeBSD. With this change, these will now work on any FreeBSD systems that have procfs mounted. This isn't the right fix for the libTooling issue, but it does bring the FreeBSD implementation of getExecutablePath closer to the Linux and macOS implementations. llvm-svn: 359427
Diffstat (limited to 'llvm/lib/Support/Unix/Path.inc')
-rw-r--r--llvm/lib/Support/Unix/Path.inc15
1 files changed, 14 insertions, 1 deletions
diff --git a/llvm/lib/Support/Unix/Path.inc b/llvm/lib/Support/Unix/Path.inc
index 0e150d1ac26..fa5115bfd65 100644
--- a/llvm/lib/Support/Unix/Path.inc
+++ b/llvm/lib/Support/Unix/Path.inc
@@ -185,8 +185,21 @@ std::string getMainExecutable(const char *argv0, void *MainAddr) {
#elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || \
defined(__minix) || defined(__DragonFly__) || \
defined(__FreeBSD_kernel__) || defined(_AIX)
+ StringRef curproc("/proc/curproc/file");
char exe_path[PATH_MAX];
-
+ // /proc is not mounted by default under FreeBSD, but gives more accurate
+ // information than argv[0] when it is.
+ if (sys::fs::exists(curproc)) {
+ ssize_t len = readlink(curproc.str().c_str(), exe_path, sizeof(exe_path));
+ if (len > 0) {
+ // Null terminate the string for realpath. readlink never null
+ // terminates its output.
+ len = std::min(len, ssize_t(sizeof(exe_path) - 1));
+ exe_path[len] = '\0';
+ return exe_path;
+ }
+ }
+ // If we don't have procfs mounted, fall back to argv[0]
if (getprogpath(exe_path, argv0) != NULL)
return exe_path;
#elif defined(__linux__) || defined(__CYGWIN__)
OpenPOWER on IntegriCloud