[Clang] Honor asm labels on library functions for compiler-emitted calls #244853
<!--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:
__builtin_abort), which getBuiltinLibFunction() emitted using the
plain library name, since only the __builtin_ decl was checked for
an asm label.
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:
````````````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>
Tested on a SPARC M7 (Linux sparc64), on top of main 1ed0efd70af1 together with #244836, #244838, #244842 and #244854. clang/test/CodeGen/asm-label.c and asm-label-inline-builtins.c pass.
__builtin_stpcpy, __builtin_abort and the terminate landing pad now call __GI_stpcpy and __GI_abort, on sparc64 and in the x86_64 IR.
In a clang-built glibc, stpcpy and abort are gone from the elf/check-localplt failures.
It looks like gcc delays finalizing the name until the end of the translation unit; do we need to allow that?
extern char *stpcpy(char *__restrict, const char *__restrict);
void f(char*x,char*y) { stpcpy(x,y); }
extern __typeof(stpcpy) stpcpy asm("__GI_stpcpy");
The following calls __GI_strcpy, do we need to match that?
extern char *stpcpy(char *__restrict, const char *__restrict);
extern char *strcpy(char *__restrict, const char *__restrict);
void f(char*x,char*y) { __builtin_stpcpy(x,y); }
extern __typeof(strcpy) strcpy asm("__GI_strcpy");
1. Clang already rejects the direct call case with "cannot apply asm label to function after its first use". This patch doesn't change that. The label has to come before the use, so I don't think we need to allow it here.
2. GCC calls __GI_strcpy here because it turns __builtin_stpcpy(x, y) into strcpy(x, y) when the result is not used, even at -O0, and the call then uses the asm label on strcpy. In Clang that is done in LLVM (SimplifyLibCalls), which doesn't know about asm labels. It is the same problem as memcpy calls from the backend, so I think it is out of scope. If stpcpy is labeled too, we call __GI_stpcpy, so there is no PLT call.
@efriedma-quic
efriedma-quic
brad0
When a library function is redeclared with an asm label, Clang used the
label for direct calls but not for calls it emits itself:
__builtin_abort), which getBuiltinLibFunction() emitted using the
plain library name, since only the __builtin_ decl was checked for
an asm label.
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