← Back to Pull requests

[Clang] Honor asm labels on library functions for compiler-emitted calls #244853

Open
brad0 opened this pull request with 65 lines diff across 4 files
View on GitHub
brad0 brad0 commented on Nov 21, 2026 08:59 UTC
Author

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

    Diff: 65 lines (+62 / -3) across 4 files Reviewer: efriedma-quic Review duration: 13719m
    All Vouch review confidence checks passed
    Residual risk 0.03 = Change Risk (0.32) × (1 − Review Confidence (0.92)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.32 · 65 lines diff across 4 files
    0.32
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.93 · 5 substantive comment(s)
    0.93
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 50%
    92%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.251
    Real PR Conversation Timeline
    6 items
    llvmorg-github-actions[bot] llvmorg-github-actions[bot] commented on Nov 21, 2026 09:00 UTC
    Bot

    <!--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 --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>

    shalseth shalseth commented on Nov 21, 2026 13:46 UTC
    Contributor

    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.

    efriedma-quic efriedma-quic commented on code on Nov 23, 2026 23:54 UTC
    Code Review
    clang/test/CodeGen/asm-label.c:1
    @@ -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");

    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");
    

    brad0 brad0 commented on code on Nov 24, 2026 00:07 UTC
    Code Review
    clang/test/CodeGen/asm-label.c:1
    @@ -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");

    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.

    brad0 brad0 commented on Nov 30, 2026 21:35 UTC
    Author

    @efriedma-quic

    Reviewers
    Assignees
    brad0 brad0
    Labels
    risk: low confidence: 92% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 32.5% (LOW) · Higher = larger blast radius
    32.5%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 92% · Lower = rushed or shallow review
    92%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 2.6% · Safe to merge
    2.6%