[llvm] 6ce8e71 - [SanitizerBinaryMetadata] Make module_[cd]tor external
Fangrui Song via llvm-commits
llvm-commits at lists.llvm.org
Wed Feb 8 00:58:43 PST 2023
Author: Fangrui Song
Date: 2023-02-08T00:58:39-08:00
New Revision: 6ce8e716bfab4a1d2d8777f9868af5759a515391
URL: https://github.com/llvm/llvm-project/commit/6ce8e716bfab4a1d2d8777f9868af5759a515391
DIFF: https://github.com/llvm/llvm-project/commit/6ce8e716bfab4a1d2d8777f9868af5759a515391.diff
LOG: [SanitizerBinaryMetadata] Make module_[cd]tor external
If a COMDAT key has a local linkage, it behaves as `comdat nodeduplicate` and
llvm/lib/Linker/LinkModules.cpp does not deduplicate its members.
This is not intended. Switch to an external linkage to allow deduplication.
See also https://maskray.me/blog/2021-07-25-comdat-and-section-group#grp_comdat
Reviewed By: melver
Differential Revision: https://reviews.llvm.org/D143530
Added:
llvm/test/Instrumentation/SanitizerBinaryMetadata/ctor.ll
Modified:
llvm/lib/Transforms/Instrumentation/SanitizerBinaryMetadata.cpp
Removed:
################################################################################
diff --git a/llvm/lib/Transforms/Instrumentation/SanitizerBinaryMetadata.cpp b/llvm/lib/Transforms/Instrumentation/SanitizerBinaryMetadata.cpp
index f3ff9fe8bac05..e990ea204d422 100644
--- a/llvm/lib/Transforms/Instrumentation/SanitizerBinaryMetadata.cpp
+++ b/llvm/lib/Transforms/Instrumentation/SanitizerBinaryMetadata.cpp
@@ -215,9 +215,12 @@ bool SanitizerBinaryMetadata::run() {
Constant *CtorData = nullptr;
Constant *DtorData = nullptr;
if (TargetTriple.supportsCOMDAT()) {
- // Use COMDAT to deduplicate constructor/destructor function.
+ // Use COMDAT to deduplicate constructor/destructor function. The COMDAT
+ // key needs to be a non-local linkage.
Ctor->setComdat(Mod.getOrInsertComdat(Ctor->getName()));
Dtor->setComdat(Mod.getOrInsertComdat(Dtor->getName()));
+ Ctor->setLinkage(GlobalValue::ExternalLinkage);
+ Dtor->setLinkage(GlobalValue::ExternalLinkage);
CtorData = Ctor;
DtorData = Dtor;
}
diff --git a/llvm/test/Instrumentation/SanitizerBinaryMetadata/ctor.ll b/llvm/test/Instrumentation/SanitizerBinaryMetadata/ctor.ll
new file mode 100644
index 0000000000000..9e26c7525c5a6
--- /dev/null
+++ b/llvm/test/Instrumentation/SanitizerBinaryMetadata/ctor.ll
@@ -0,0 +1,22 @@
+; RUN: opt < %s -passes='module(sanmd-module)' -sanitizer-metadata-atomics -S | FileCheck %s
+
+; CHECK: $__sanitizer_metadata_atomics.module_ctor = comdat any
+; CHECK: $__sanitizer_metadata_atomics.module_dtor = comdat any
+; CHECK: $__sanitizer_metadata_covered.module_ctor = comdat any
+; CHECK: $__sanitizer_metadata_covered.module_dtor = comdat any
+
+; CHECK: @llvm.used = appending global [4 x ptr] [ptr @__sanitizer_metadata_atomics.module_ctor, ptr @__sanitizer_metadata_atomics.module_dtor, ptr @__sanitizer_metadata_covered.module_ctor, ptr @__sanitizer_metadata_covered.module_dtor], section "llvm.metadata"
+; CHECK: @llvm.global_ctors = appending global [2 x { i32, ptr, ptr }] [{ i32, ptr, ptr } { i32 2, ptr @__sanitizer_metadata_atomics.module_ctor, ptr @__sanitizer_metadata_atomics.module_ctor }, { i32, ptr, ptr } { i32 2, ptr @__sanitizer_metadata_covered.module_ctor, ptr @__sanitizer_metadata_covered.module_ctor }]
+; CHECK: @llvm.global_dtors = appending global [2 x { i32, ptr, ptr }] [{ i32, ptr, ptr } { i32 2, ptr @__sanitizer_metadata_atomics.module_dtor, ptr @__sanitizer_metadata_atomics.module_dtor }, { i32, ptr, ptr } { i32 2, ptr @__sanitizer_metadata_covered.module_dtor, ptr @__sanitizer_metadata_covered.module_dtor }]
+
+; CHECK: define dso_local void @__sanitizer_metadata_covered.module_ctor() #1 comdat {
+; CHECK: define dso_local void @__sanitizer_metadata_covered.module_dtor() #1 comdat {
+
+target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
+target triple = "x86_64-unknown-linux-gnu"
+
+define i8 @foo(ptr %a) nounwind uwtable {
+entry:
+ %0 = load atomic i8, ptr %a unordered, align 1
+ ret i8 %0
+}
More information about the llvm-commits
mailing list