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

    <tr>
        <th>Summary</th>
        <td>
            [Clang] --target=mips-linux-muslsf Generates Broken Code by Default
        </td>
    </tr>

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

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

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

<pre>
    These have been tested under Debian 12's default (i.e., Clang v14.0.6), but I do think that it continues to affect the latest version, too.
(It is worth mentioning that these bugs affect all variations of `mips[eb]-[vendor]-linux-musl[sf]` just the same.)

Before proceeding with the below steps as to replicate this bug, make sure that you install the following packages (commands are for Debian 12):

```
sudo apt install llvm clang
sudo apt install lld
sudo apt install libgcc1-mips-cross
sudo apt install gcc-mips-linux-gnu
```

I have went through all of these steps on an actual MIPS32r2 device (Qualcomm Atheros QCA9533), with an up-to-date musl libc ( 1.2.4-4) and Linux kernel (5.15.150)

---

Without a multitude of manual hacks to command-line switches, Clang will (by default) generate broken code for soft-float MIPS targets using musl libc:
---
```c
clang -xc - --target=mips-linux-muslsf  -fuse-ld=lld -o test <<< '#include <stdio.h>
int main() { printf("Float number: %f \n", (float)5/2); return 0; }'
```

**FIRST BUG:** After doing `chmod +x ./test` on the target machine, you will be encountered with a `./test: not found` error; `file ./test` indicates that the compiled program has been configured to use `/lib/ld-musl-mips.so.1` as its interpreter, even though it had been compiled with `mips-linux-muslsf` (i.e., `musl*sf*`) and *should* have instead been using `/lib/ld-musl-mips-sf.so.1`.
---
As a workaround, we could hardcode this path into the final binary:

```c
clang -xc - --target=mips-linux-muslsf -fuse-ld=lld  -o test \
-Wl,--dynamic-linker=/lib/ld-musl-mips-sf.so.1 <<< '#include <stdio.h>
int main() { printf("Float number: %f \n", (float)5/2); return 0; }'
```
**SECOND BUG:** However, a MIPS machine would still be unable to run our program; this time, the shell will throw a new error: `Illegal instruction`.
---
Despite the presence of `sf` in `muslsf`, Clang still seems to generate hard-float instructions for CPUs that otherwise lack an FPU, and this happens regardless of whether or not we specify an `-march` (e.g., `-march=mips32r2` results in the same broken code). Attempting to specify either one of `-msoft-float`, `-mfloat-abi=soft`, or `-mattr=+soft-float` results in compilation failure:

```c
clang -xc - --target=mips-linux-muslsf -fuse-ld=lld  -o test \
-Wl,--dynamic-linker=/lib/ld-musl-mips-sf.so.1 \
-msoft-float <<< '#include <stdio.h>
int main() { printf("Float number: %f \n", (float)5/2); return 0; }'

=================
ld.lld: error: /tmp/--471681.o: floating point ABI '-msoft-float' is incompatible with target floating point ABI '-mfpxx'
clang: error: linker command failed with exit code 1 (use -v to see invocation)
```

**THIRD BUG:** For some reason, `lld` is given the `-mfpxx` option by default, which conflicts with `-msoft-float`.  Fortunately, by manually supplying `-flto`, we can bypass this third issue:
---
```c
clang -xc - --target=mips-linux-muslsf -fuse-ld=lld  -o test \
-Wl,--dynamic-linker=/lib/ld-musl-mips-sf.so.1 \
-msoft-float -flto <<< '#include <stdio.h>
int main() { printf("Float number: %f \n", (float)5/2); return 0; }'
```

**FOURTH BUG:** Due to the presence of `#include <stdio.h>`, one final bug occurs at this point: `/usr/include/gnu/stubs.h:8:11: fatal error: '/usr/mips-linux-gnu/include/gnu/stubs-o32_soft.h' file not found`.  This file is neither existent nor needed and, in fact, `mips-linux-gnu-gcc` itself does not even include it in the first place.  Indeed, a simple `sudo touch /usr/mips-linux-gnu/include/gnu/stubs-o32_soft.h` satisfies this "requirement," making the compilation (finally) successful:
---

```c
sudo touch /usr/mips-linux-gnu/include/gnu/stubs-o32_soft.h && \
clang -xc - --target=mips-linux-muslsf -fuse-ld=lld  -o test \
-Wl,--dynamic-linker=/lib/ld-musl-mips-sf.so.1 \
-msoft-float -flto <<< '#include <stdio.h>
int main() { printf("Float number: %f \n", (float)5/2); return 0; }'
```
**Finally,** we get Clang to generate proper code that outputs the following: `Float number: 2.500000`
---

# Summary:

1. **Interpreter Path Bug:** Clang incorrectly configures the interpreter path to `/lib/ld-musl-mips.so.1` instead of `/lib/ld-musl-mips-sf.so.1` for soft-float MIPS targets.;
3. **Illegal FPU Instruction Bug:** In the spite of `-msoft-float` flag, Clang generates hard-float instructions, resulting in an "illegal instruction" error on CPUs without an FPU;
4. **Conflict with -mfpxx Option:** Without `-flto`, `lld` automatically applies the `-mfpxx` option, which conflicts with the soft-float ABI and causes a compilation failure; and
5. **Missing Header File Bug:** The `/usr/mips-linux-gnu/include/gnu/stubs-o32_soft.h` file does not exist and is not required, yet Clang ceases compilation as long as a dummy (empty) file has not been provided.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzsWVtv47gV_jXMy4EEm44d-yEPvqx3AnS7szsZ7GNBi0cWNxSp8mLH_744pORLmkyx7aKYAh0YzlimyHP7zvcdWXiv9gbxkU1XbLq5EzE01j0-N05gENaIu52Vp8fnBj1CIw4IO0QDAX1ACdFIdLDBnRIGxpzxBw8SaxF1AMbnqsSS8TWstTB7OIzvy1E5Y3xB13YxwBNIC6FR5gVCIwKoAJU1QZmIHoIFUddYBQgNghZ0JBzQeWUNbRCsLdlow0ZLxudPAZSHo3WhgRZNUNYos8-7hmT7Lu79sKHQGg7CKUHrPNga2GzUqs6z6Qp3bLop2HR1QCOtow9amfhatNFrNl35mk03bDaC36PPpnnRkpuL3pj0vsLaOoTO2QpRkilHFZq0fIfaHsEH7DyI5KbDTqtKBKRYeLKU_GvFC4KPDrMXJxtBGR_IdtqmtlrbI-3ciepF7NFTxCvbtsJID8LRkpvcLNhkeW0jm436V_roo7QgunA-RetDCxWl7sMF8qNv1G5fVeOCYlpUznr_wcJ9VeVFOcZ7E9-1Lb8_5QI8oqHAOxv3TUqlrfsc56BaA8KAqEIUGn56-vxlwh0HiQdVIcXolyg0xQmWoUFnPfyyXi6mk0lfmClRwkDsimALSWmh1JNPFd0O45KX98U94wsQRsJfyHJ4QWdQ0_fTckyv0ZuKKIri-uNv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