-
Notifications
You must be signed in to change notification settings - Fork 12.9k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
[Clang] --target=mips-linux-muslsf Generates Broken Code by Default #102259
Comments
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 Other than this extra bug in Clang-18, the four other bugs replicate the same. |
FWIW, note that prior to #107664, |
I just hit this as well trying to compile code for a MediaTek MT7621A using LLVM 20. |
Could you please try 20 release candidate or |
I'm on the latest snapshot build from https://apt.llvm.org:
|
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: