Clang incorrectly optimizes a function call equivalent to memset to a runtime call to memset even when compiling with -nostdlib. While normally this would be a useful optimization, in this case it causes compilation to fail.
The bad optimization disappears when compiling without optimization. I have also been able to replicate the problem on clang 6.0.
Extended Description
Clang incorrectly optimizes a function call equivalent to memset to a runtime call to memset even when compiling with -nostdlib. While normally this would be a useful optimization, in this case it causes compilation to fail.
The bad optimization disappears when compiling without optimization. I have also been able to replicate the problem on clang 6.0.
See also the corrosponding GCC bug: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=90701.
Output of clang-7 -v -save-temps -nostdlib -O3 -Wall repro2.c: clang version 7.0.0-3~ubuntu0.18.04.1 (tags/RELEASE_700/final) Target: x86_64-pc-linux-gnu Thread model: posix InstalledDir: /usr/bin Found candidate GCC installation: /usr/bin/../lib/gcc/i686-linux-gnu/8 Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/7 Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/7.4.0 Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/8 Found candidate GCC installation: /usr/lib/gcc/i686-linux-gnu/8 Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/7 Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/7.4.0 Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/8 Selected GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/8 Candidate multilib: .;@m64 Candidate multilib: 32;@m32 Candidate multilib: x32;@mx32 Selected multilib: .;@m64 "/usr/lib/llvm-7/bin/clang" -cc1 -triple x86_64-pc-linux-gnu -E -save-temps=cwd -disable-free -disable-llvm-verifier -discard-value-names -main-file-name repro2.c -mrelocation-model static -mthread-model posix -fmath-errno -masm-verbose -mconstructor-aliases -munwind-tables -fuse-init-array -target-cpu x86-64 -dwarf-column-info -debugger-tuning=gdb -momit-leaf-frame-pointer -v -resource-dir /usr/lib/llvm-7/lib/clang/7.0.0 -internal-isystem /usr/local/include -internal-isystem /usr/lib/llvm-7/lib/clang/7.0.0/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -O3 -Wall -fdebug-compilation-dir /usr/local/src/proot-rs/src/kernel/execve/loader -ferror-limit 19 -fmessage-length 93 -fobjc-runtime=gcc -fdiagnostics-show-option -fcolor-diagnostics -vectorize-loops -vectorize-slp -o repro2.i -x c repro2.c -faddrsig clang -cc1 version 7.0.0 based upon LLVM 7.0.0 default target x86_64-pc-linux-gnu ignoring nonexistent directory "/include"
include "..." search starts here:
include <...> search starts here:
/usr/local/include /usr/lib/llvm-7/lib/clang/7.0.0/include /usr/include/x86_64-linux-gnu /usr/include End of search list. "/usr/lib/llvm-7/bin/clang" -cc1 -triple x86_64-pc-linux-gnu -emit-llvm-bc -emit-llvm-uselists -save-temps=cwd -disable-free -disable-llvm-verifier -discard-value-names -main-file-name repro2.c -mrelocation-model static -mthread-model posix -fmath-errno -masm-verbose -mconstructor-aliases -munwind-tables -fuse-init-array -target-cpu x86-64 -dwarf-column-info -debugger-tuning=gdb -momit-leaf-frame-pointer -v -resource-dir /usr/lib/llvm-7/lib/clang/7.0.0 -O3 -Wall -fdebug-compilation-dir /usr/local/src/proot-rs/src/kernel/execve/loader -ferror-limit 19 -fmessage-length 93 -fobjc-runtime=gcc -fdiagnostics-show-option -fcolor-diagnostics -vectorize-loops -vectorize-slp -disable-llvm-passes -o repro2.bc -x cpp-output repro2.i -faddrsig clang -cc1 version 7.0.0 based upon LLVM 7.0.0 default target x86_64-pc-linux-gnu
include "..." search starts here:
End of search list. "/usr/lib/llvm-7/bin/clang" -cc1 -triple x86_64-pc-linux-gnu -S -save-temps=cwd -disable-free -disable-llvm-verifier -discard-value-names -main-file-name repro2.c -mrelocation-model static -mthread-model posix -fmath-errno -masm-verbose -mconstructor-aliases -munwind-tables -fuse-init-array -target-cpu x86-64 -dwarf-column-info -debugger-tuning=gdb -momit-leaf-frame-pointer -v -resource-dir /usr/lib/llvm-7/lib/clang/7.0.0 -O3 -Wall -fdebug-compilation-dir /usr/local/src/proot-rs/src/kernel/execve/loader -ferror-limit 19 -fmessage-length 93 -fobjc-runtime=gcc -fdiagnostics-show-option -fcolor-diagnostics -vectorize-loops -vectorize-slp -o repro2.s -x ir repro2.bc -faddrsig clang -cc1 version 7.0.0 based upon LLVM 7.0.0 default target x86_64-pc-linux-gnu "/usr/lib/llvm-7/bin/clang" -cc1as -triple x86_64-pc-linux-gnu -filetype obj -main-file-name repro2.c -target-cpu x86-64 -dwarf-version=4 -mrelocation-model static -o repro2.o repro2.s "/usr/bin/ld" -z relro --hash-style=gnu --eh-frame-hdr -m elf_x86_64 -dynamic-linker /lib64/ld-linux-x86-64.so.2 -o a.out -L/usr/bin/../lib/gcc/x86_64-linux-gnu/8 -L/usr/bin/../lib/gcc/x86_64-linux-gnu/8/../../../x86_64-linux-gnu -L/lib/x86_64-linux-gnu -L/lib/../lib64 -L/usr/lib/x86_64-linux-gnu -L/usr/bin/../lib/gcc/x86_64-linux-gnu/8/../../.. -L/usr/lib/llvm-7/bin/../lib -L/lib -L/usr/lib repro2.o repro2.o: In function
_start': repro2.c:(.text+0x50): undefined reference to
memset' repro2.c:(.text+0x5d): undefined reference tomemset' repro2.c:(.text+0x7e): undefined reference to
memset' clang: error: linker command failed with exit code 1 (use -v to see invocation)