You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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):
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:
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:
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:
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:
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:
Finally, we get Clang to generate proper code that outputs the following: Float number: 2.500000
Summary:
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.;
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;
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
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.
The text was updated successfully, but these errors were encountered:
UPDATE: In addition to Clang-14 in the original post, I also got to test this under Clang-18 (Ubuntu 22.04); Clang-18 is actually more broken in this regard.
For the third bug, you are able to use -flto to bypass the -msoft-float vs. -mfpxx option clash in Clang-14; however, you cannnot do the same in Clang-18, because it always errors out with error: test.lto.o: floating point ABI '-msoft-float' is incompatible with target floating point ABI '-mfpxx'.
Other than this extra bug in Clang-18, the four other bugs replicate the same.
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):
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:
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 withmips-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:
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
inmuslsf
, 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: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: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 simplesudo touch /usr/mips-linux-gnu/include/gnu/stubs-o32_soft.h
satisfies this "requirement," making the compilation (finally) successful:Finally, we get Clang to generate proper code that outputs the following:
Float number: 2.500000
Summary:
/lib/ld-musl-mips.so.1
instead of/lib/ld-musl-mips-sf.so.1
for soft-float MIPS targets.;-msoft-float
flag, Clang generates hard-float instructions, resulting in an "illegal instruction" error on CPUs without an FPU;-flto
,lld
automatically applies the-mfpxx
option, which conflicts with the soft-float ABI and causes a compilation failure; and/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.The text was updated successfully, but these errors were encountered: