summaryrefslogtreecommitdiffstats
path: root/llvm/lib/Support/Unix/Path.inc
diff options
context:
space:
mode:
authorEd Maste <emaste@freebsd.org>2019-11-13 13:24:23 -0500
committerEd Maste <emaste@freebsd.org>2019-11-14 09:48:48 -0500
commita0a38b81ea911f1cd4e400f1ab54dd4930598a7c (patch)
tree4cc476d6293554ff2da8cc978a9eb78588be6e0b /llvm/lib/Support/Unix/Path.inc
parent232cdb3d3018b4bda60a2a62c3b071ad3af8bd35 (diff)
downloadbcm5719-llvm-a0a38b81ea911f1cd4e400f1ab54dd4930598a7c.tar.gz
bcm5719-llvm-a0a38b81ea911f1cd4e400f1ab54dd4930598a7c.zip
On FreeBSD use AT_EXECPATH from ELF auxiliary vectors for getExecutablePath
/proc/curproc/file and the KERN_PROC_PATHNAME sysctl may not return the desired path if there are multiple hardlinks to the file, or if the path has expired from the namecache. Reviewed By: theraven Differential Revision: https://reviews.llvm.org/D70198
Diffstat (limited to 'llvm/lib/Support/Unix/Path.inc')
-rw-r--r--llvm/lib/Support/Unix/Path.inc46
1 files changed, 41 insertions, 5 deletions
diff --git a/llvm/lib/Support/Unix/Path.inc b/llvm/lib/Support/Unix/Path.inc
index b86766d4bc4..51b564f9a18 100644
--- a/llvm/lib/Support/Unix/Path.inc
+++ b/llvm/lib/Support/Unix/Path.inc
@@ -38,6 +38,14 @@
#include <mach-o/dyld.h>
#include <sys/attr.h>
#include <copyfile.h>
+#elif defined(__FreeBSD__)
+#include <osreldate.h>
+#if __FreeBSD_version >= 1300057
+#include <sys/auxv.h>
+#else
+#include <machine/elf.h>
+extern char **environ;
+#endif
#elif defined(__DragonFly__)
#include <sys/mount.h>
#endif
@@ -183,13 +191,41 @@ std::string getMainExecutable(const char *argv0, void *MainAddr) {
if (realpath(exe_path, link_path))
return link_path;
}
-#elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || \
- defined(__minix) || defined(__DragonFly__) || \
- defined(__FreeBSD_kernel__) || defined(_AIX)
+#elif defined(__FreeBSD__)
+ // On FreeBSD if the exec path specified in ELF auxiliary vectors is
+ // preferred, if available. /proc/curproc/file and the KERN_PROC_PATHNAME
+ // sysctl may not return the desired path if there are multiple hardlinks
+ // to the file.
+ char exe_path[PATH_MAX];
+#if __FreeBSD_version >= 1300057
+ char exe_path[PATH_MAX];
+ if (elf_aux_info(AT_EXECPATH, exe_path, sizeof(exe_path)) == 0)
+ return exe_path;
+#else
+ // elf_aux_info(AT_EXECPATH, ... is not available in all supported versions,
+ // fall back to finding the ELF auxiliary vectors after the process's
+ // environment.
+ char **p = ::environ;
+ while (*p++ != 0)
+ ;
+ // Iterate through auxiliary vectors for AT_EXECPATH.
+ for (;;) {
+ switch (*(uintptr_t *)p++) {
+ case AT_EXECPATH:
+ return *p;
+ case AT_NULL:
+ break;
+ }
+ p++;
+ }
+#endif
+ // Fall back to argv[0] if auxiliary vectors are not available.
+ if (getprogpath(exe_path, argv0) != NULL)
+ return exe_path;
+#elif defined(__NetBSD__) || defined(__OpenBSD__) || defined(__minix) || \
+ defined(__DragonFly__) || defined(__FreeBSD_kernel__) || defined(_AIX)
const char *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, exe_path, sizeof(exe_path));
if (len > 0) {
OpenPOWER on IntegriCloud