<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/64927>64927</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            [llvm-rc] Wrong Clang executable picked after commit 8c6a0c8
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          kleisauke
      </td>
    </tr>
</table>

<pre>
    After commit 8c6a0c8bf50bca6c3a0c4de1b84f21466ee31655 (cherry-picked in LLVM 17.0.0-rc3) the wrong Clang executable is selected in llvm-rc when using this directory format:
```console
$ cat /data/mxe/usr/bin/x86_64-w64-mingw32-windres
#!/bin/sh
exec "/data/mxe/usr/x86_64-w64-mingw32/bin/x86_64-w64-mingw32-windres" "$@"
$ tree /data/mxe/usr -L 3 --dirsfirst
├── bin
│   ├── x86_64-w64-mingw32-clang -> /data/mxe/usr/x86_64-w64-mingw32/bin/clang-target-wrapper.sh
│   └── x86_64-w64-mingw32-windres
├── x86_64-w64-mingw32
│   └── bin
│        ├── clang-target-wrapper.sh
│        └── x86_64-w64-mingw32-windres -> /data/mxe/usr/x86_64-pc-linux-gnu/bin/llvm-windres
└── x86_64-pc-linux-gnu
    └── bin
        ├── clang -> clang-17
        ├── clang-17
        ├── llvm-rc
        └── llvm-windres -> llvm-rc

```

With LLVM 17.0.0-rc3 it picks the non-sysroot aware `/data/mxe/usr/x86_64-pc-linux-gnu/bin/clang-17` instead of `/data/mxe/usr/bin/x86_64-w64-mingw32-clang`, which is unexpected.
```
access("/proc/self/exe", F_OK)          = 0
readlink("/proc/self/exe", "/data/mxe/usr/x86_64-pc-linux-gn"..., 4096) = 45
lstat("/data", {st_mode=S_IFDIR|0755, st_size=12288, ...}) = 0
lstat("/data/mxe", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0
lstat("/data/mxe/usr", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0
lstat("/data/mxe/usr/x86_64-pc-linux-gnu", {st_mode=S_IFDIR|0755, st_size=99, ...}) = 0
lstat("/data/mxe/usr/x86_64-pc-linux-gnu/bin", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0
lstat("/data/mxe/usr/x86_64-pc-linux-gnu/bin/llvm-rc", {st_mode=S_IFREG|0755, st_size=759536, ...}) = 0
access("/data/mxe/usr/x86_64-pc-linux-gnu/bin/x86_64-w64-mingw32-clang", R_OK|X_OK) = -1 ENOENT (No such file or directory)
access("/data/mxe/usr/x86_64-pc-linux-gnu/bin/clang-17", R_OK|X_OK) = 0
stat("/data/mxe/usr/x86_64-pc-linux-gnu/bin/clang-17", {st_mode=S_IFREG|0755, st_size=128354312, ...}) = 0
access("/data/mxe/usr/x86_64-pc-linux-gnu/bin/clang-17", F_OK) = 0
```

/cc @mstorsjo
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzEV01v4zYQ_TX0ZSCBGn0ffNiNo2LR7S6QFt3eAooaW9zIokFSddJfX1ByvElWTmx02xo-CJTmvTcznCdRWKs2PdGSpe9ZulqIwbXaLO86UlYMd7SodfOwfLd2ZEDq7VY5KGQmuCzqdcprKTIZCy6ThqK6SNYYJVlGFEdZmgLDQrZkzEOwU_KOGlA9fPz4-y8Q5SEPeWBkzLAE1xLsje43cNWJfgN0T3Jwou4IlAVLHUk3BXfdn9vASNi31MNgVb8B1yoLjTIknTYPsNZmKxyL3zG-Yvwdy_j0l7q3uqPDKiYghQOGVSOcYFht74lhNVjDsKpVz7C6L7LbLAn2WRJsVb_ZxxjsVd8Yso8YMcPo-Lhtp2WvHRjiPPT3oOfwIU6ICUu4vzjm4AzRXBIQfIQYgqBRxq6Vse4Qco2sTFh5dbgo-PECvIinDxUIAK8FzOiVY_cCFl-fqOwr6Y-xgRNmQy7YG7HbkQkfizqnKTlP0_OeXZTPZdyzBRx_r7GenfYzpMtyP6MjOxl0qh_ug00_HFsy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