llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT--> @llvm/pr-subscribers-clang @llvm/pr-subscribers-clang-codegen Author: Brad Smith (brad0) <details> <summary>Changes</summary> When a library function is redeclared with an asm label, Clang used the label for direct calls but not for calls it emits itself: - calls through the matching __builtin_* alias (e.g. __builtin_stpcpy, __builtin_abort), which getBuiltinLibFunction() emitted using the plain library name, since only the __builtin_ decl was checked for an asm label. - the abort() call in the terminate landing pad for EH cleanups in C, which getTerminateFn() always emitted as plain abort. Look up the library function in the translation unit and use the mangled name of a declaration with an asm label (or #pragma redefine_extname), matching GCC. glibc relies on this to keep internal calls off the PLT; a Clang-built libc.so had PLT calls to stpcpy and abort, failing elf/check-localplt. Fixes #<!-- -->244638 --- Full diff: https://github.com/llvm/llvm-project/pull/244853.diff 4 Files Affected: - (modified) clang/lib/CodeGen/CGBuiltin.cpp (+17-2) - (modified) clang/lib/CodeGen/CGException.cpp (+1-1) - (modified) clang/lib/CodeGen/CodeGenModule.h (+5) - (modified) clang/test/CodeGen/asm-label.c (+39) ``````````diff diff --git a/clang/lib/CodeGen/CGBuiltin.cpp b/clang/lib/CodeGen/CGBuiltin.cpp index bdcd9ee43ce1c..c5f201fd057db 100644 --- a/clang/lib/CodeGen/CGBuiltin.cpp +++ b/clang/lib/CodeGen/CGBuiltin.cpp @@ -190,17 +190,22 @@ llvm::Constant *CodeGenModule::getBuiltinLibFunction(const FunctionDecl *FD, llvm::SmallString<64> Name; GlobalDecl D(FD); + std::string LibName = Context.BuiltinInfo.getName(BuiltinID).substr(10); + // If the builtin has been declared explicitly with an assembler label, // use the mangled name. This differs from the plain label on platforms - // that prefix labels. + // that prefix labels. Likewise, if the library function the builtin maps to + // has been declared with an assembler label, use that, as GCC does. if (FD->hasAttr<AsmLabelAttr>()) Name = getMangledName(D); + else if (std::optional<StringRef> Label = getAsmLabelForLibFunction(LibName)) + Name = *Label; else if (StringRef TargetName = CodeGenUtils::getTargetBuiltinLibFunctionName( BuiltinID, getTarget()); !TargetName.empty()) Name = TargetName; else - Name = Context.BuiltinInfo.getName(BuiltinID).substr(10); + Name = LibName; llvm::FunctionType *Ty = cast<llvm::FunctionType>(getTypes().ConvertType(FD->getType())); @@ -208,6 +213,16 @@ llvm::Constant *CodeGenModule::getBuiltinLibFunction(const FunctionDecl *FD, return GetOrCreateLLVMFunction(Name, Ty, D, /*ForVTable=*/false); } +std::optional<StringRef> +CodeGenModule::getAsmLabelForLibFunction(StringRef Name) { + for (const NamedDecl *ND : + Context.getTranslationUnitDecl()->lookup(&Context.Idents.get(Name))) + if (const auto *FD = dyn_cast<FunctionDecl>(ND)) + if (FD->isExternC() && FD->hasAttr<AsmLabelAttr>()) + return getMangledName(GlobalDecl(FD)); + return std::nullopt; +} + void appendDefaultIntrinsicArgs(SmallVectorImpl<llvm::Value *> &Args, llvm::Function *F) { llvm::FunctionType *FTy = F->getFunctionType(); diff --git a/clang/lib/CodeGen/CGException.cpp b/clang/lib/CodeGen/CGException.cpp index 75d8a91e03436..ea58d3bb2c80c 100644 --- a/clang/lib/CodeGen/CGException.cpp +++ b/clang/lib/CodeGen/CGException.cpp @@ -70,7 +70,7 @@ llvm::FunctionCallee CodeGenModule::getTerminateFn() { getLangOpts().ObjCRuntime.hasTerminate()) name = "objc_terminate"; else - name = "abort"; + name = getAsmLabelForLibFunction("abort").value_or("abort"); return CreateRuntimeFunction(FTy, name); } diff --git a/clang/lib/CodeGen/CodeGenModule.h b/clang/lib/CodeGen/CodeGenModule.h index c6b13f5881784..d28e26bd97dc8 100644 --- a/clang/lib/CodeGen/CodeGenModule.h +++ b/clang/lib/CodeGen/CodeGenModule.h @@ -1338,6 +1338,11 @@ class CodeGenModule : public CodeGenTypeCache { llvm::Constant *getBuiltinLibFunction(const FunctionDecl *FD, unsigned BuiltinID); + /// If the C library function \p Name has been declared with an asm label + /// (or \#pragma redefine_extname), return its mangled name so that calls + /// the compiler emits itself use the same symbol as direct calls. + std::optional<StringRef> getAsmLabelForLibFunction(StringRef Name); + llvm::Function *getIntrinsic(unsigned IID, ArrayRef<llvm::Type *> Tys = {}); void AddCXXGlobalInit(llvm::Function *F) { CXXGlobalInits.push_back(F); } diff --git a/clang/test/CodeGen/asm-label.c b/clang/test/CodeGen/asm-label.c index c901223db990d..f9c9b4c724dd7 100644 --- a/clang/test/CodeGen/asm-label.c +++ b/clang/test/CodeGen/asm-label.c @@ -1,5 +1,6 @@ // RUN: %clang_cc1 -triple=i686-pc-linux-gnu -emit-llvm %s -o - | FileCheck %s --check-prefixes=CHECK,LINUX // RUN: %clang_cc1 -triple=i686-apple-darwin9 -emit-llvm %s -o - | FileCheck %s --check-prefixes=CHECK,DARWIN +// RUN: %clang_cc1 -triple=i686-pc-linux-gnu -fexceptions -emit-llvm %s -o - | FileCheck %s --check-prefix=EH char *strerror(int) asm("alias"); int x __asm("foo"); @@ -53,3 +54,41 @@ double sin(double x) asm("real_sin"); double test_sin(double d) { return __builtin_sin(d); } // CHECK-LABEL: @test_sin( // LINUX: call double @llvm.sin.f64( + +/// A call through the __builtin_ alias of a library function uses the asm +/// label of the library function, as a direct call does. +extern char *stpcpy(char *__restrict, const char *__restrict); +extern __typeof(stpcpy) stpcpy asm("__GI_stpcpy"); +char *test_builtin_stpcpy(char *d, const char *s) { + return __builtin_stpcpy(d, s); +} +// CHECK-LABEL: @test_builtin_stpcpy( +// LINUX: call ptr @__GI_stpcpy( +// DARWIN: call ptr @"\01__GI_stpcpy"( + +extern void abort(void) __attribute__((noreturn)); +extern __typeof(abort) abort asm("__GI_abort"); +void test_builtin_abort(int x) { + if (x) + __builtin_abort(); +} +// CHECK-LABEL: @test_builtin_abort( +// LINUX: call void @__GI_abort( +// DARWIN: call void @"\01__GI_abort"( + +/// The abort call in the terminate landing pad for an EH cleanup also uses +/// the asm label. +void release(int *); +void use(int *); +void test_cleanup_terminate(void) { + int v __attribute__((cleanup(release))) = 0; + use(&v); +} +// EH-LABEL: @test_cleanup_terminate( +// EH: invoke void @release( +// EH-NEXT: to label %{{.*}} unwind label %[[TERM:.*]] +// EH: [[TERM]]: +// EH-NEXT: landingpad { ptr, i32 } +// EH-NEXT: catch ptr null +// EH-NEXT: call void @__GI_abort() +// EH-NOT: call void @abort() `````````` </details> https://github.com/llvm/llvm-project/pull/244853 _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
