From owner-svn-src-head@freebsd.org Wed Nov 13 21:02:19 2019 Return-Path: Delivered-To: svn-src-head@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id 3ACE01BBC37; Wed, 13 Nov 2019 21:02:19 +0000 (UTC) (envelope-from emaste@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) server-signature RSA-PSS (4096 bits) client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 47CxrW0VTpz3L2K; Wed, 13 Nov 2019 21:02:19 +0000 (UTC) (envelope-from emaste@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id E36051DDEF; Wed, 13 Nov 2019 21:02:18 +0000 (UTC) (envelope-from emaste@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id xADL2Itl044750; Wed, 13 Nov 2019 21:02:18 GMT (envelope-from emaste@FreeBSD.org) Received: (from emaste@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id xADL2I2w044749; Wed, 13 Nov 2019 21:02:18 GMT (envelope-from emaste@FreeBSD.org) Message-Id: <201911132102.xADL2I2w044749@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: emaste set sender to emaste@FreeBSD.org using -f From: Ed Maste Date: Wed, 13 Nov 2019 21:02:18 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r354692 - head/contrib/llvm/lib/Support/Unix X-SVN-Group: head X-SVN-Commit-Author: emaste X-SVN-Commit-Paths: head/contrib/llvm/lib/Support/Unix X-SVN-Commit-Revision: 354692 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: SVN commit messages for the src tree for head/-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 13 Nov 2019 21:02:19 -0000 Author: emaste Date: Wed Nov 13 21:02:18 2019 New Revision: 354692 URL: https://svnweb.freebsd.org/changeset/base/354692 Log: llvm: 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. PR: 241932 Tested by: ler Sponsored by: The FreeBSD Foundation Modified: head/contrib/llvm/lib/Support/Unix/Path.inc Modified: head/contrib/llvm/lib/Support/Unix/Path.inc ============================================================================== --- head/contrib/llvm/lib/Support/Unix/Path.inc Wed Nov 13 20:32:23 2019 (r354691) +++ head/contrib/llvm/lib/Support/Unix/Path.inc Wed Nov 13 21:02:18 2019 (r354692) @@ -38,6 +38,9 @@ #include #include #include +#elif defined(__FreeBSD__) +#include +extern char **environ; #elif defined(__DragonFly__) #include #endif @@ -183,13 +186,32 @@ std::string getMainExecutable(const char *argv0, void 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]; + char **p = ::environ; + while (*p++ != 0) + ; + // ELF auxiliary vectors immediately follow the process's environment. + for (;;) { + switch (*(uintptr_t *)p++) { + case AT_EXECPATH: + return *p; + case AT_NULL: + break; + } + p++; + } + // 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) 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) {