Skip to content

Commit 5475834

Browse files
authored
[sancov] Use comdats when one already exists (#131929)
This code avoids adding comdat groups to interposable linkage types (weak, linkonce (non-ODR)) to avoid changing semantics, since comdat elimination happens before weak/strong prevailaing symbol resolution. However, if the function is already in a comdat, we can add to the group without changing the semantics of the linked program. Fixes an issue uncovered in PR #126240
1 parent 39ce995 commit 5475834

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

llvm/lib/Transforms/Instrumentation/SanitizerCoverage.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -745,7 +745,7 @@ GlobalVariable *ModuleSanitizerCoverage::CreateFunctionLocalArrayInSection(
745745
Constant::getNullValue(ArrayTy), "__sancov_gen_");
746746

747747
if (TargetTriple.supportsCOMDAT() &&
748-
(TargetTriple.isOSBinFormatELF() || !F.isInterposable()))
748+
(F.hasComdat() || TargetTriple.isOSBinFormatELF() || !F.isInterposable()))
749749
if (auto Comdat = getOrCreateFunctionComdat(F, TargetTriple))
750750
Array->setComdat(Comdat);
751751
Array->setSection(getSectionName(Section));

llvm/test/Instrumentation/SanitizerCoverage/interposable-symbol.ll

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
; RUN: opt < %s -passes='module(sancov-module)' -sanitizer-coverage-level=3 -sanitizer-coverage-trace-pc-guard -mtriple x86_64-linux-gnu -S | FileCheck %s --check-prefixes=CHECK,ELF
33
; RUN: opt < %s -passes='module(sancov-module)' -sanitizer-coverage-level=3 -sanitizer-coverage-trace-pc-guard -mtriple x86_64-windows-msvc -S | FileCheck %s --check-prefixes=CHECK,COFF
44

5+
$WeakComdat = comdat any
6+
57
define void @Vanilla() {
68
entry:
79
ret void
@@ -29,14 +31,22 @@ entry:
2931
ret void
3032
}
3133

34+
define weak void @WeakComdat() comdat {
35+
entry:
36+
ret void
37+
}
38+
39+
3240
; CHECK: $Vanilla = comdat nodeduplicate
3341
; ELF: $LinkOnceOdr = comdat nodeduplicate
3442
; COFF: $LinkOnceOdr = comdat any
43+
; CHECK: $WeakComdat = comdat any
3544
; CHECK: @__sancov_gen_ = private global [1 x i32] zeroinitializer, section {{.*}}, comdat($Vanilla), align 4{{$}}
3645
; CHECK-NEXT: @__sancov_gen_.1 = private global [1 x i32] zeroinitializer, section {{.*}}, align 4{{$}}
3746
; CHECK-NEXT: @__sancov_gen_.2 = private global [1 x i32] zeroinitializer, section {{.*}}, align 4{{$}}
3847
; CHECK-NEXT: @__sancov_gen_.3 = private global [1 x i32] zeroinitializer, section {{.*}}, comdat($LinkOnceOdr), align 4{{$}}
3948
; CHECK-NEXT: @__sancov_gen_.4 = private global [1 x i32] zeroinitializer, section {{.*}}, comdat($WeakOdr), align 4{{$}}
49+
; CHECK-NEXT: @__sancov_gen_.5 = private global [1 x i32] zeroinitializer, section {{.*}}, comdat($WeakComdat), align 4{{$}}
4050

4151
; CHECK: define void @Vanilla() comdat {
4252
; ELF: define linkonce void @LinkOnce() comdat {
@@ -46,3 +56,4 @@ entry:
4656
; CHECK: declare extern_weak void @ExternWeak()
4757
; CHECK: define linkonce_odr void @LinkOnceOdr() comdat {
4858
; CHECK: define weak_odr void @WeakOdr() comdat {
59+
; CHECK: define weak void @WeakComdat() comdat {

0 commit comments

Comments
 (0)