From 44dbbe61060acac4d0991a15decac4c909e26844 Mon Sep 17 00:00:00 2001 From: "Yaxun (Sam) Liu" Date: Mon, 19 Jul 2021 17:15:38 -0400 Subject: [HIP] Preserve ASAN bitcode library functions Address sanitizer passes may generate call of ASAN bitcode library functions after bitcode linking in lld, therefore lld cannot add those symbols since it does not know they will be used later. To solve this issue, clang emits a reference to a bicode library function which calls all ASAN functions which need to be preserved. This basically force all ASAN functions to be linked in. Reviewed by: Artem Belevich Differential Revision: https://reviews.llvm.org/D106315 --- clang/lib/CodeGen/CodeGenModule.cpp | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'clang/lib/CodeGen/CodeGenModule.cpp') diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp index fc1d8d924349..9b40b88ea3c9 100644 --- a/clang/lib/CodeGen/CodeGenModule.cpp +++ b/clang/lib/CodeGen/CodeGenModule.cpp @@ -523,6 +523,22 @@ void CodeGenModule::Release() { !Context.getTargetInfo().getTriple().isOSEmscripten()) { EmitMainVoidAlias(); } + + // Emit reference of __amdgpu_device_library_preserve_asan_functions to + // preserve ASAN functions in bitcode libraries. + if (LangOpts.Sanitize.has(SanitizerKind::Address) && getTriple().isAMDGPU()) { + auto *FT = llvm::FunctionType::get(VoidTy, {}); + auto *F = llvm::Function::Create( + FT, llvm::GlobalValue::ExternalLinkage, + "__amdgpu_device_library_preserve_asan_functions", &getModule()); + auto *Var = new llvm::GlobalVariable( + getModule(), FT->getPointerTo(), + /*isConstant=*/true, llvm::GlobalValue::WeakAnyLinkage, F, + "__amdgpu_device_library_preserve_asan_functions_ptr", nullptr, + llvm::GlobalVariable::NotThreadLocal); + addCompilerUsedGlobal(Var); + } + emitLLVMUsed(); if (SanStats) SanStats->finish(); -- cgit v1.2.3