1 diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_file.cpp b/compiler-rt/lib/sanitizer_common/sanitizer_file.cpp
2 index 7ef499ce07b1..8fd682f943fe 100644
3 --- a/compiler-rt/lib/sanitizer_common/sanitizer_file.cpp
4 +++ b/compiler-rt/lib/sanitizer_common/sanitizer_file.cpp
6 #include "sanitizer_file.h"
7 # include "sanitizer_interface_internal.h"
10 +#include "sanitizer_posix.h"
13 namespace __sanitizer {
15 void CatastrophicErrorWrite(const char *buffer, uptr length) {
16 @@ -206,11 +210,35 @@ char *FindPathToBinary(const char *name) {
17 return internal_strdup(name);
20 + uptr name_len = internal_strlen(name);
21 + InternalMmapVector<char> buffer(kMaxPathLength);
24 + // If we cannot find the requested binary in PATH, we should try to locate
25 + // it next to the binary, in case it is shipped with the build itself
26 + // (e.g. llvm-symbolizer shipped with sanitizer build to symbolize on client.
27 + if (internal_readlink("/proc/self/exe", buffer.data(), kMaxPathLength) >= 0) {
28 + uptr buf_len = internal_strlen(buffer.data());
30 + /* Avoid using dirname() here */
31 + while (buf_len > 0) {
32 + if (buffer[buf_len - 1] == '/')
37 + if (buf_len && buf_len + name_len + 1 <= kMaxPathLength) {
38 + internal_memcpy(&buffer[buf_len], name, name_len);
39 + buffer[buf_len + name_len] = '\0';
40 + if (FileExists(buffer.data()))
41 + return internal_strdup(buffer.data());
46 const char *path = GetEnv("PATH");
49 - uptr name_len = internal_strlen(name);
50 - InternalMmapVector<char> buffer(kMaxPathLength);
51 const char *beg = path;
53 const char *end = internal_strchrnul(beg, kPathSeparator);