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

    <tr>
        <th>Summary</th>
        <td>
            [clang] --gcc-toolchain=... and --gcc-install-dir do not work as expected with -m32
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            clang
      </td>
    </tr>

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

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

<pre>
    I noticed a change with using 32-bit `libstdc++` and `libgcc` with clang versions 14 and above (also, see note at the end of this report). Previously (e.g., clang 10), `--gcc-toolchain=...` was all that was needed to pick up the 32-bit gcc libraries. Now an additional `-L` option needs to be specified:

Consider this gcc installation:

```
/tools/compilers/gcc-12.2.0
  bin
 include
  lib        # 32-bit libstdc++ is here
  lib64      # 64-bit libstdc++ is here
  :
  : 
```

Previously, the following used to work and `libstdc++` would be picked up from `/tools/compilers/gcc-12.2.0/lib`:

```
clang++ -m32 --gcc-toolchain=/tools/compilers/gcc-12.2.0 ...
```

In newer versions of clang,`-L` option needs to be additionally specified:

```
clang++ -m32 --gcc-toolchain=/tools/compilers/gcc-12.2.0 ... -L /tools/compilers/gcc-12.2.0/lib
```

This issue occurs only with `-m32`. Somehow, without `-m32`, it picks up `libstdc++` from `/tools/compilers/gcc-12.2.0/lib64`.

The issue is there with `--gcc-install-dir=...` as well. `-gcc-install-dir=/tools/compilers/gcc-12.2.0/lib/gcc/x86_64-redhat-linux/12.2.0` doesn't work (both `ld` and `ld.lld` give error that `-lstdc++ -lgcc` cannot be found) unless `-L /tools/compilers/gcc-12.2.0/lib` is also specified. 

In 64-bit, the clang driver is passing the following options to the linker:

```
 -L/tools/compilers/gcc-12.2.0/lib/gcc/x86_64-redhat-linux/12.2.0 -L/tools/compilers/gcc-12.2.0/lib/gcc/x86_64-redhat-linux/12.2.0/../../../../lib64
```

In 32-bit, it is passing only this option:

```
-L/tools/compilers/gcc-12.2.0/lib/gcc/x86_64-redhat-linux/12.2.0/32
```

clang-10 seems to pass the following options:

```
-L/tools/compilers/gcc-12.2.0/lib/gcc/x86_64-redhat-linux/12.2.0 -L/tools/compilers/gcc-12.2.0/lib/gcc/x86_64-redhat-linux/12.2.0/../../..
```

*NOTE*: I don't know if clang-10 is the last version where this functioned as expected. I don't have builds of clang-11 through 13 available but the issue can be seen in clang-14 and above (including the HEAD).
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJy8Vs2O2zYQfhr6MpBAUVrZPviwycZogCAN2qDXgiLHErs0aZCUne3TF0PJ9WZ_nBRIujC0EjlDzs8334yM0fQOccNu3rCbu4Uc0-DD5jfzIP_-49Oi8_ph8x6cT0ahBglqkK5HOJk0wBiN66EWRWcSsJZb08WkFRNv6NdykE7P671StJDVlJWuhyOGaLyLUDVZTnb-iMDEStromXgLEZHuRZAJ0oCAToPfQRpMhIAHHxIT6xI-BTwaP0b7QMpY9iUpT3dUnIk1fbKWF0WvVJG8t2qQxrH6rizLbJOMIK2FNMiUPxyiRg3Jw8GoexgP-frZzV4psKYLMhiMJXz0J5AOpNYmGe-kzVd9oHP9gVbyaZEO6xDiAZXZGdSsvmX8jvH5-da7aDSGyTm6wriYpLWSjngizFo-_6ZPsSWnIhNb5fcHYzHQOzlbiVKUsxhAZ9z8apyyo8bzhjUdzH9M1GdHv0ommAgDhscqbXNRaZtvq_zrRX6Fl33Jz0tGKXUU-5231p8IbGOcMnPy4f4RvL6G3cmPVlO8KX-oKYO74Pck--1oia01HUlejXrG1-xosa8FPIfXN68CAuDrYXhP2DlhuFSK38F879trKLuA0T68Crmf5A8UH-B7Y_y665-pDEyMI4JXagwRvLMPE3uQ5_tasJaX8Lvf4-BPBBPa82N6tE2rJmUMRILAS0j5j7BoG7r2a0txNtREgmrAi5U5gnMhF9qEC-XICCe0tsxiz6W-N4B5iYntl1X7Z9sUAfUgU2GNG78wsZ1FWw7aY3RMLNNUNkysO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