<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/77551>77551</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            [clang][compiler-rt] Duplicate symbol link errors when compiling/linking with `-fsanitize=<sanitizer>` and `-fxray-instrument`
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            clang
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          nmosier
      </td>
    </tr>
</table>

<pre>
    I observed that compiling and linking with the flags `-fsanitize=<sanitizer>` and `-fxray-instrument` produces a huge list of duplicate symbol linker errors.
For example, the compile command `clang -fsanitize=address -fxray-instrument test.c` outputs the following:
```
ld.lld: error: duplicate symbol: __sanitizer::internal_allocator()
>>> defined at sanitizer_allocator.cpp:38 (/tmp/llvm-20231128-6780-l667df/llvm-project-17.0.6.src/compiler-rt/lib/sanitizer_common/sanitizer_allocator.cpp:38)
>>> sanitizer_allocator.cpp.o:(__sanitizer::internal_allocator()) in archive /home/linuxbrew/.linuxbrew/Cellar/llvm/17.0.6/lib/clang/17/lib/linux/libclang_rt.asan-x86_64.a
>>> defined at sanitizer_allocator.cpp:38 (/tmp/llvm-20231128-6780-l667df/llvm-project-17.0.6.src/compiler-rt/lib/sanitizer_common/sanitizer_allocator.cpp:38)
>>> sanitizer_allocator.cpp.o:(.text._ZN11__sanitizer18internal_allocatorEv+0x0) in archive /home/linuxbrew/.linuxbrew/Cellar/llvm/17.0.6/lib/clang/17/lib/linux/libclang_rt.xray-x86_64.a

ld.lld: error: duplicate symbol: __sanitizer::InternalAlloc(unsigned long, __sanitizer::SizeClassAllocator32LocalCache<__sanitizer::SizeClassAllocator32<__sanitizer::AP32>>*, unsigned long)
>>> defined at sanitizer_allocator.cpp:86 (/tmp/llvm-20231128-6780-l667df/llvm-project-17.0.6.src/compiler-rt/lib/sanitizer_common/sanitizer_allocator.cpp:86)
>>> sanitizer_allocator.cpp.o:(__sanitizer::InternalAlloc(unsigned long, __sanitizer::SizeClassAllocator32LocalCache<__sanitizer::SizeClassAllocator32<__sanitizer::AP32>>*, unsigned long)) in archive /home/linuxbrew/.linuxbrew/Cellar/llvm/17.0.6/lib/clang/17/lib/linux/libclang_rt.asan-x86_64.a
>>> defined at sanitizer_allocator.cpp:86 (/tmp/llvm-20231128-6780-l667df/llvm-project-17.0.6.src/compiler-rt/lib/sanitizer_common/sanitizer_allocator.cpp:86)
>>> sanitizer_allocator.cpp.o:(.text._ZN11__sanitizer13InternalAllocEmPNS_30SizeClassAllocator32LocalCacheINS_20SizeClassAllocator32INS_4AP32EEEEEm+0x0) in archive /home/linuxbrew/.linuxbrew/Cellar/llvm/17.0.6/lib/clang/17/lib/linux/libclang_rt.xray-x86_64.a

ld.lld: error: duplicate symbol: __sanitizer::errno_EOWNERDEAD
>>> defined at sanitizer_errno.cpp
>>> sanitizer_errno.cpp.o:(__sanitizer::errno_EOWNERDEAD) in archive /home/linuxbrew/.linuxbrew/Cellar/llvm/17.0.6/lib/clang/17/lib/linux/libclang_rt.asan-x86_64.a
>>> defined at sanitizer_errno.cpp
>>> sanitizer_errno.cpp.o:(.rodata._ZN11__sanitizer16errno_EOWNERDEADE+0x0) in archive /home/linuxbrew/.linuxbrew/Cellar/llvm/17.0.6/lib/clang/17/lib/linux/libclang_rt.xray-x86_64.a

ld.lld: error: duplicate symbol: __sanitizer::InternalRealloc(void*, unsigned long, __sanitizer::SizeClassAllocator32LocalCache<__sanitizer::SizeClassAllocator32<__sanitizer::AP32>>*)
>>> defined at sanitizer_allocator.cpp:93 (/tmp/llvm-20231128-6780-l667df/llvm-project-17.0.6.src/compiler-rt/lib/sanitizer_common/sanitizer_allocator.cpp:93)
>>> sanitizer_allocator.cpp.o:(__sanitizer::InternalRealloc(void*, unsigned long, __sanitizer::SizeClassAllocator32LocalCache<__sanitizer::SizeClassAllocator32<__sanitizer::AP32>>*)) in archive /home/linuxbrew/.linuxbrew/Cellar/llvm/17.0.6/lib/clang/17/lib/linux/libclang_rt.asan-x86_64.a
>>> defined at sanitizer_allocator.cpp:93 (/tmp/llvm-20231128-6780-l667df/llvm-project-17.0.6.src/compiler-rt/lib/sanitizer_common/sanitizer_allocator.cpp:93)
>>> sanitizer_allocator.cpp.o:(.text._ZN11__sanitizer15InternalReallocEPvmPNS_30SizeClassAllocator32LocalCacheINS_20SizeClassAllocator32INS_4AP32EEEEE+0x0) in archive /home/linuxbrew/.linuxbrew/Cellar/llvm/17.0.6/lib/clang/17/lib/linux/libclang_rt.xray-x86_64.a

ld.lld: error: duplicate symbol: __sanitizer::InternalReallocArray(void*, unsigned long, unsigned long, __sanitizer::SizeClassAllocator32LocalCache<__sanitizer::SizeClassAllocator32<__sanitizer::AP32>>*)
>>> defined at sanitizer_allocator.cpp:101 (/tmp/llvm-20231128-6780-l667df/llvm-project-17.0.6.src/compiler-rt/lib/sanitizer_common/sanitizer_allocator.cpp:101)
>>> sanitizer_allocator.cpp.o:(__sanitizer::InternalReallocArray(void*, unsigned long, unsigned long, __sanitizer::SizeClassAllocator32LocalCache<__sanitizer::SizeClassAllocator32<__sanitizer::AP32>>*)) in archive /home/linuxbrew/.linuxbrew/Cellar/llvm/17.0.6/lib/clang/17/lib/linux/libclang_rt.asan-x86_64.a
>>> defined at sanitizer_allocator.cpp:101 (/tmp/llvm-20231128-6780-l667df/llvm-project-17.0.6.src/compiler-rt/lib/sanitizer_common/sanitizer_allocator.cpp:101)
>>> sanitizer_allocator.cpp.o:(.text._ZN11__sanitizer20InternalReallocArrayEPvmmPNS_30SizeClassAllocator32LocalCacheINS_20SizeClassAllocator32INS_4AP32EEEEE+0x0) in archive /home/linuxbrew/.linuxbrew/Cellar/llvm/17.0.6/lib/clang/17/lib/linux/libclang_rt.xray-x86_64.a

ld.lld: error: duplicate symbol: __sanitizer::InternalCalloc(unsigned long, unsigned long, __sanitizer::SizeClassAllocator32LocalCache<__sanitizer::SizeClassAllocator32<__sanitizer::AP32>>*)
>>> defined at sanitizer_allocator.cpp:112 (/tmp/llvm-20231128-6780-l667df/llvm-project-17.0.6.src/compiler-rt/lib/sanitizer_common/sanitizer_allocator.cpp:112)
...
```

I assume this is not the desired behavior. If `-fsanitize=address` and `-fxray-instrument` are mutually exclusive, then `clang` should refuse to compile the program, rather than produce a linker error.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzsWUtv6jgU_jVmY2E5NglhwSIXiFRp1KluFyPNBpn4QDzjxJHtUHp__Sjm0QcwrXorXehMFZX4cXJe3_ks-Qjn1KoGGKP4G4qnPdH60thxXRmnwPYWRj6Ob7BZOLBrkNiXwuPCVI3Sql5hUUusVf139_6gfIl9CXipxcphlND-0olaefUDEJ8iPtmPLOIzlNAgHbZtrHjsq9p521ZQ-26tsUa2BTgscNmuAGvlPDZLLNtGq0J4wO6xWhgd1IPFYK2xjiA6RTTLjcWwEVWjAbFJMGprc_itdnoLLeoVfmGkkNKCc_jIJOzBeVJ0lpnWN613W1eN1uZB1SvEs61qlNDdE4ZaEq0l4tnWwO7ltQfd3Hz-LDYZ4pmqPdha6LnQ2hTCG4tYithop4TPtg-WsFQ1SCw8PnzhSYYUTYN4xlMcpHNfNYjlWq-rPqOMRxFL-8kwpX2dJEO53K811vwFhe9HQ0JJQpwtEMt3AbR967t9aoFY_qSyC6upX0wdW3HC_jPbienCwNL3x4WNsKqxsEWp1oARy0tTQTC0bjcLCw-I5eT5YAJaC7tzGbF86-zBtQCOMH2YCtLbUVidW0-EE3V_kybzZEDEfyw5xMPGk_mft1H0LE9Repyj2Rqxb3RDf1WWQjG_ytJPlefNzsWs8xCxtK0Di0qsTWfP5FjiXv2AiRbOZfugcPabKYSeiKIExCfvkji1L7vr5kPOWNbpfmXMxzgjTS4BlmnyCZxxXcm6WiK7XsScITL-Ajiz6u72fs7pv2Pj5vZ-zk7u6VYGXfpn3V_1xQgRrK3NfPb7H7ez79NZNn0XgoJQyNvZfB22nK3uI81XU0Af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