https://github.com/brad0 created 
https://github.com/llvm/llvm-project/pull/244853

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

>From c9adcb281f5233733392fea623ac62c350c44035 Mon Sep 17 00:00:00 2001
From: Brad Smith <[email protected]>
Date: Sat, 21 Nov 2026 03:45:44 -0500
Subject: [PATCH] [Clang] Honor asm labels on library functions for
 compiler-emitted calls

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

Co-Authored-By: Claude Opus 5.5 <[email protected]>
---
 clang/lib/CodeGen/CGBuiltin.cpp   | 19 +++++++++++++--
 clang/lib/CodeGen/CGException.cpp |  2 +-
 clang/lib/CodeGen/CodeGenModule.h |  5 ++++
 clang/test/CodeGen/asm-label.c    | 39 +++++++++++++++++++++++++++++++
 4 files changed, 62 insertions(+), 3 deletions(-)

diff --git a/clang/lib/CodeGen/CGBuiltin.cpp b/clang/lib/CodeGen/CGBuiltin.cpp
index bdcd9ee43ce1c7..c5f201fd057db5 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 75d8a91e034360..ea58d3bb2c80c4 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 c6b13f58817843..d28e26bd97dc86 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 c901223db990d0..f9c9b4c724dd7e 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()

_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to