forked from OSchip/llvm-project
[SanitizerCoverage] Make __start_/__stop_ symbols extern_weak
On ELF, we place the metadata sections (`__sancov_guards`, `__sancov_cntrs`, `__sancov_bools`, `__sancov_pcs` in section groups (either `comdat any` or `comdat noduplicates`). With `--gc-sections`, LLD since D96753 and GNU ld `-z start-stop-gc` may garbage collect such sections. If all `__sancov_bools` are discarded, LLD will error `error: undefined hidden symbol: __start___sancov_cntrs` (other sections are similar). ``` % cat a.c void discarded() {} % clang -fsanitize-coverage=func,trace-pc-guard -fpic -fvisibility=hidden a.c -shared -fuse-ld=lld -Wl,--gc-sections ... ld.lld: error: undefined hidden symbol: __start___sancov_guards >>> referenced by a.c >>> /tmp/a-456662.o:(sancov.module_ctor_trace_pc_guard) ``` Use the `extern_weak` linkage (lowered to undefined weak symbols) to avoid the undefined error. Differential Revision: https://reviews.llvm.org/D98903
This commit is contained in:
parent
c9861f722e
commit
9558456b53
|
@ -328,13 +328,15 @@ PreservedAnalyses ModuleSanitizerCoveragePass::run(Module &M,
|
|||
std::pair<Value *, Value *>
|
||||
ModuleSanitizerCoverage::CreateSecStartEnd(Module &M, const char *Section,
|
||||
Type *Ty) {
|
||||
// Use ExternalWeak so that if all sections are discarded due to section
|
||||
// garbage collection, the linker will not report undefined symbol errors.
|
||||
GlobalVariable *SecStart = new GlobalVariable(
|
||||
M, Ty->getPointerElementType(), false, GlobalVariable::ExternalLinkage,
|
||||
nullptr, getSectionStart(Section));
|
||||
M, Ty->getPointerElementType(), false,
|
||||
GlobalVariable::ExternalWeakLinkage, nullptr, getSectionStart(Section));
|
||||
SecStart->setVisibility(GlobalValue::HiddenVisibility);
|
||||
GlobalVariable *SecEnd = new GlobalVariable(
|
||||
M, Ty->getPointerElementType(), false, GlobalVariable::ExternalLinkage,
|
||||
nullptr, getSectionEnd(Section));
|
||||
M, Ty->getPointerElementType(), false,
|
||||
GlobalVariable::ExternalWeakLinkage, nullptr, getSectionEnd(Section));
|
||||
SecEnd->setVisibility(GlobalValue::HiddenVisibility);
|
||||
IRBuilder<> IRB(M.getContext());
|
||||
if (!TargetTriple.isOSBinFormatCOFF())
|
||||
|
|
|
@ -2,11 +2,14 @@
|
|||
; RUN: opt < %s -sancov -sanitizer-coverage-level=1 -sanitizer-coverage-inline-8bit-counters=1 -S -enable-new-pm=0 | FileCheck %s
|
||||
; RUN: opt < %s -passes='module(sancov-module)' -sanitizer-coverage-level=1 -sanitizer-coverage-inline-8bit-counters=1 -S | FileCheck %s
|
||||
|
||||
; CHECK: @__sancov_gen_ = private global [1 x i8] zeroinitializer, section "__sancov_cntrs", comdat($foo), align 1
|
||||
; CHECK: @__start___sancov_cntrs = extern_weak hidden global i8
|
||||
; CHECK-NEXT: @__stop___sancov_cntrs = extern_weak hidden global i8
|
||||
|
||||
target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64"
|
||||
target triple = "x86_64-unknown-linux-gnu"
|
||||
define void @foo() {
|
||||
entry:
|
||||
; CHECK: section "__sancov_cntrs", comdat($foo), align 1
|
||||
; CHECK: %0 = load i8, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @__sancov_gen_, i64 0, i64 0), align 1, !nosanitize
|
||||
; CHECK: %1 = add i8 %0, 1
|
||||
; CHECK: store i8 %1, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @__sancov_gen_, i64 0, i64 0), align 1, !nosanitize
|
||||
|
|
|
@ -4,6 +4,8 @@
|
|||
|
||||
; CHECK: $foo = comdat noduplicates
|
||||
; CHECK: @__sancov_gen_ = private global [1 x i1] zeroinitializer, section "__sancov_bools", comdat($foo), align 1{{$}}
|
||||
; CHECK: @__start___sancov_bools = extern_weak hidden global i1
|
||||
; CHECK-NEXT: @__stop___sancov_bools = extern_weak hidden global i1
|
||||
; CHECK-NOT: @llvm.used =
|
||||
; CHECK: @llvm.compiler.used = appending global [1 x i8*] [i8* bitcast ([1 x i1]* @__sancov_gen_ to i8*)], section "llvm.metadata"
|
||||
|
||||
|
|
|
@ -22,6 +22,8 @@ entry:
|
|||
}
|
||||
|
||||
; CHECK: private constant [6 x i64*] [{{.*}}@foo{{.*}}blockaddress{{.*}}blockaddress{{.*}}], section "__sancov_pcs", comdat($foo), align 8
|
||||
; CHECK: @__start___sancov_pcs = extern_weak hidden global i64
|
||||
; CHECK-NEXT: @__stop___sancov_pcs = extern_weak hidden global i64
|
||||
; CHECK: define internal void @sancov.module_ctor
|
||||
; CHECK: call void @__sanitizer_cov
|
||||
; CHECK: call void @__sanitizer_cov_pcs_init
|
||||
|
|
Loading…
Reference in New Issue