Bug 84285 - Fail to statically link with -fsanitize=undefined
Summary: Fail to statically link with -fsanitize=undefined
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: sanitizer (show other bugs)
Version: 7.2.1
: P3 normal
Target Milestone: ---
Assignee: Jakub Jelinek
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2018-02-08 14:33 UTC by Marc-André Lureau
Modified: 2018-03-03 20:27 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2018-02-08 00:00:00


Attachments
gcc8-pr84285.patch (526 bytes, patch)
2018-02-08 15:00 UTC, Jakub Jelinek
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Marc-André Lureau 2018-02-08 14:33:34 UTC
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
Comment 1 Martin Liška 2018-02-08 14:49:18 UTC
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.
Comment 2 Jakub Jelinek 2018-02-08 14:59:01 UTC
Actually, I believe we should treat -static like -static-lib*san for the purpose of STATIC_LIB*SAN_LIBS.
Comment 3 Jakub Jelinek 2018-02-08 15:00:42 UTC
Created attachment 43371 [details]
gcc8-pr84285.patch

Untested fix.
Comment 4 Marc-André Lureau 2018-02-08 18:56:15 UTC
(In reply to Jakub Jelinek from comment #3)
> Created attachment 43371 [details]
> gcc8-pr84285.patch
> 
> Untested fix.

Thanks
patch texted successfully.
Comment 5 Jakub Jelinek 2018-02-09 06:44:38 UTC
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
Comment 6 Jakub Jelinek 2018-02-09 06:45:41 UTC
Fixed on the trunk so far.
Comment 7 Jakub Jelinek 2018-03-03 13:28:47 UTC
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
Comment 8 Jakub Jelinek 2018-03-03 20:27:18 UTC
Fixed also for 7.4+.