With gcc (GCC) 7.2.1 20170915 (Red Hat 7.2.1-2) (fedora 27) int main(int argc, char **argv) { int k = 0x7fffffff; k += argc; return 0; } $ clang -static -fsanitize=undefined test.c vs $ gcc -static -fsanitize=undefined test.c /usr/lib/gcc/x86_64-redhat-linux/7/libubsan.a(sanitizer_linux_libcdep.o): In function `__sanitizer::GetThreadStackTopAndBottom(bool, unsigned long*, unsigned long*)': (.text+0x551): undefined reference to `pthread_attr_init' (.text+0x556): undefined reference to `pthread_self' (.text+0x561): undefined reference to `pthread_getattr_np' (.text+0x599): undefined reference to `pthread_attr_destroy' /usr/lib/gcc/x86_64-redhat-linux/7/libubsan.a(sanitizer_linux_libcdep.o): In function `__sanitizer::SetEnv(char const*, char const*)': (.text+0x70b): undefined reference to `dlsym' /usr/lib/gcc/x86_64-redhat-linux/7/libubsan.a(sanitizer_linux_libcdep.o): In function `__sanitizer::InitTlsSize()': (.text+0x843): undefined reference to `dlsym' /usr/lib/gcc/x86_64-redhat-linux/7/libubsan.a(sanitizer_posix_libcdep.o): In function `__sanitizer::GetNamedMappingFd(char const*, unsigned long)': (.text+0x747): undefined reference to `shm_open' (.text+0x763): undefined reference to `shm_unlink' /usr/lib/gcc/x86_64-redhat-linux/7/libubsan.a(sanitizer_posix_libcdep.o): In function `__sanitizer::AdjustStackSize(void*)': (.text+0xa8f): undefined reference to `pthread_attr_setstacksize' /usr/lib/gcc/x86_64-redhat-linux/7/libubsan.a(sanitizer_posix_libcdep.o): In function `__sanitizer::GetThreadSelf()': (.text+0x1d1): undefined reference to `pthread_self' /usr/lib/gcc/x86_64-redhat-linux/7/libubsan.a(sanitizer_posix_libcdep.o): In function `__sanitizer::my_pthread_attr_getstack(void*, void**, unsigned long*)': (.text+0x9b1): undefined reference to `pthread_attr_getstack' /usr/lib/gcc/x86_64-redhat-linux/7/libubsan.a(sanitizer_symbolizer_posix_libcdep.o): In function `__sanitizer::Symbolizer::LateInitialize()': (.text+0x7e3): undefined reference to `dlsym' collect2: error: ld returned 1 exit status
For that we have: -static-libubsan When the -fsanitize=undefined option is used to link a program, the GCC driver automatically links against libubsan. If libubsan is available as a shared library, and the -static option is not used, then this links against the shared version of libubsan. The -static-libubsan option directs the GCC driver to link libubsan statically, without necessarily linking other libraries statically.
Actually, I believe we should treat -static like -static-lib*san for the purpose of STATIC_LIB*SAN_LIBS.
Created attachment 43371 [details] gcc8-pr84285.patch Untested fix.
(In reply to Jakub Jelinek from comment #3) > Created attachment 43371 [details] > gcc8-pr84285.patch > > Untested fix. Thanks patch texted successfully.
Author: jakub Date: Fri Feb 9 06:44:06 2018 New Revision: 257515 URL: https://gcc.gnu.org/viewcvs?rev=257515&root=gcc&view=rev Log: PR sanitizer/84285 * gcc.c (STATIC_LIBASAN_LIBS, STATIC_LIBTSAN_LIBS, STATIC_LIBLSAN_LIBS, STATIC_LIBUBSAN_LIBS): Handle -static like -static-lib*san. Modified: trunk/gcc/ChangeLog trunk/gcc/gcc.c
Fixed on the trunk so far.
Author: jakub Date: Sat Mar 3 13:28:15 2018 New Revision: 258187 URL: https://gcc.gnu.org/viewcvs?rev=258187&root=gcc&view=rev Log: Backported from mainline 2018-02-09 Jakub Jelinek <jakub@redhat.com> PR sanitizer/84285 * gcc.c (STATIC_LIBASAN_LIBS, STATIC_LIBTSAN_LIBS, STATIC_LIBLSAN_LIBS, STATIC_LIBUBSAN_LIBS): Handle -static like -static-lib*san. Modified: branches/gcc-7-branch/gcc/ChangeLog branches/gcc-7-branch/gcc/gcc.c
Fixed also for 7.4+.