How to get normal program startup while linked to TSan runtime library?

I do not want to use TSan. I want to use parts of TSan runtime library in my own project.

Here are my criteria:

  1. I want to link to TSan static libray.
  2. I do not want my code to be instrumented by TSan.
  3. I want my executable to have a normal program startup, i.e. no modification by TSan.

I have though of two ways to do this:

  1. I know that passing -fsanitize=thread -Xclang -disable-llvm-passes -Xclang -disable-O0-optnone to clang links my executable to TSan static library while not instrumenting my code (satisfying my first two conditions), but it causes TSan to modify my executable startup, which I suppose is caused by some TSan interceptors.

  2. I have also tried to directly link my executable to TSan static library instead but I get a segmentation fault. Besides, it seems this approach causes TSan to modify my executable startup too.

Here is my simple test.cpp:

     `int main(void) {return 0;}`

And here is my compilation command to directly link to libclang_rt.tsan.a:

     `clang++ -g -o test test.cpp /pathto/libclang_rt.tsan.a`

But I get a Segmentation fault (core dumped). Here is the stack trace:

Program received signal SIGSEGV, Segmentation fault.
0x0000000000000000 in ?? ()
(gdb) bt
#0  0x0000000000000000 in ?? ()
#1  0x000055555556850b in __interceptor___cxa_atexit (f=0x7ffff7cac0f0, arg=0x7ffff7e26290, dso=0x7ffff7e260c0)
    at /home/farzam/git/llvm_11_8_2022/llvm-project/compiler-rt/lib/tsan/rtl/tsan_interceptors_posix.cpp:427
#2  0x00007ffff7caa8e2 in ?? () from /lib/x86_64-linux-gnu/libstdc++.so.6
#3  0x00007ffff7fc947e in call_init (l=<optimized out>, argc=argc@entry=1, argv=argv@entry=0x7fffffffe088, env=env@entry=0x7fffffffe098) at ./elf/dl-init.c:70
#4  0x00007ffff7fc9568 in call_init (env=0x7fffffffe098, argv=0x7fffffffe088, argc=1, l=<optimized out>) at ./elf/dl-init.c:33
#5  _dl_init (main_map=0x7ffff7ffe2e0, argc=1, argv=0x7fffffffe088, env=0x7fffffffe098) at ./elf/dl-init.c:117
#6  0x00007ffff7fe32ea in _dl_start_user () from /lib64/ld-linux-x86-64.so.2
#7  0x0000000000000001 in ?? ()
#8  0x00007fffffffe39b in ?? ()
#9  0x0000000000000000 in ?? ()

It seems that TSan intercepts cxa_atexit and that is somehow causing an error here, which also shows that this approach modifies program startup too.

So my main question is: When linking to TSan static library, is there a way to force the generated executable to have a normal program startup? That is, to disable ALL TSan interceptors and other possible stuff that can modify program startup. Because as I have understood it is not limited to intercepting cxa_atexit. For instance, TSan causes the executable to run a number of its internal functions before getting to main (like Initialize), which I suppose setup some TSan internal objects.

I am using LLVM 16 which I have built from source. Any suggestions are greatly appreciated.