← Back to Pull requests

[LLD] Fix incorrect behavior when using the "PROVIDE" directive #232318

Open
bai-tim opened this pull request with 45 lines diff across 4 files
View on GitHub
bai-tim bai-tim commented on Oct 15, 2026 13:15 UTC
Author

Symbols defined with --defsym are now considered defined before linker script commands are processed, so a PROVIDE directive can no longer override their value (GNU ld compatible behavior).

Diff: 45 lines (+45 / -0) across 4 files Reviewer: smithp35 Review duration: 71684m
All Vouch review confidence checks passed
Residual risk 0.04 = Change Risk (0.37) × (1 − Review Confidence (0.89)). Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.37 · 45 lines diff across 4 files
0.37
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.93 · 4 substantive comment(s)
0.93
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
88%
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.229
Real PR Conversation Timeline
6 items
llvmorg-github-actions[bot] llvmorg-github-actions[bot] commented on Oct 15, 2026 13:16 UTC
Bot

<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-lld-elf

@llvm/pr-subscribers-lld

Author: Timur Baidusenov (bai-tim)

<details>
<summary>Changes</summary>

Symbols defined with --defsym are now considered defined before linker script commands are processed, so a PROVIDE directive can no longer override their value (GNU ld compatible behavior), fixing incorrect section sizes when -T and --defsym are used together.

---
Full diff: https://github.com/llvm/llvm-project/pull/232318.diff

4 Files Affected:

  • (modified) lld/ELF/LinkerScript.cpp (+5)

  • (modified) lld/ELF/LinkerScript.h (+6)

  • (modified) lld/ELF/ScriptParser.cpp (+8)

  • (added) lld/test/ELF/provide-defsym.s (+39)
  • ``````

    diff --git a/lld/ELF/LinkerScript.cpp b/lld/ELF/LinkerScript.cpp
    index c69b0616a23678..43f5cc4cd3c021 100644
    --- a/lld/ELF/LinkerScript.cpp
    +++ b/lld/ELF/LinkerScript.cpp
    @@ -1973,6 +1973,11 @@ bool LinkerScript::shouldAddProvideSym(StringRef symName) {
       // undefined references from the RHS, the result of this function for a
       // symbol must be the same for each call. We use unusedProvideSyms to not
       // change the return value of a demoted symbol.
    +  // Symbols defined with --defsym are considered defined before linker script
    +  // commands are processed (GNU ld behavior), so a PROVIDE directive never
    +  // defines such a symbol.
    +  if (defsymSymbols.contains(symName))
    +    return false;
       Symbol *sym = ctx.symtab->find(symName);
       if (!sym)
         return false;
    diff --git a/lld/ELF/LinkerScript.h b/lld/ELF/LinkerScript.h
    index 80c4f564afabc1..cda021a4cee19f 100644
    --- a/lld/ELF/LinkerScript.h
    +++ b/lld/ELF/LinkerScript.h
    @@ -19,6 +19,7 @@
     #include "llvm/ADT/MapVector.h"
     #include "llvm/ADT/SmallVector.h"
     #include "llvm/ADT/StringRef.h"
    +#include "llvm/ADT/StringSet.h"
     #include "llvm/Support/Compiler.h"
     #include <cstddef>
     #include <cstdint>
    @@ -433,6 +434,11 @@ class LinkerScript final {
       // A list of symbols referenced by the script.
       SmallVector<llvm::StringRef, 0> referencedSymbols;
     
    +  // Symbols defined with --defsym. GNU ld treats them as defined before linker
    +  // script commands are processed, so PROVIDE directives cannot override them.
    +  // StringSet owns the stored strings, so they stay valid for the whole link.
    +  llvm::StringSet<> defsymSymbols;
    +
       // Used to implement INSERT [AFTER|BEFORE]. Contains output sections that need
       // to be reordered.
       SmallVector<InsertCommand, 0> insertCommands;
    diff --git a/lld/ELF/ScriptParser.cpp b/lld/ELF/ScriptParser.cpp
    index 0c54fde4882926..400e0c64a03384 100644
    --- a/lld/ELF/ScriptParser.cpp
    +++ b/lld/ELF/ScriptParser.cpp
    @@ -300,6 +300,14 @@ void ScriptParser::readDefsym() {
       Expr e = readExpr();
       if (!atEOF())
         setError("EOF expected, but got " + next());
    +  // Symbols defined with --defsym are treated as defined before linker script
    +  // commands are processed, so that e.g. they take precedence over a PROVIDE
    +  // directive (GNU ld compatible). The assignment itself is kept at its
    +  // command-line position, and script assignments are still evaluated in the
    +  // order they appear on the command line. defsymSymbols is a StringSet and
    +  // stores an owned copy of the name, since the source string may come from
    +  // the command line and does not necessarily outlive the link.
    +  ctx.script->defsymSymbols.insert(name);
       auto *cmd = make<SymbolAssignment>(name, e, 0, curBuf.filename.str());
       ctx.script->sectionCommands.push_back(cmd);
     }
    diff --git a/lld/test/ELF/provide-defsym.s b/lld/test/ELF/provide-defsym.s
    new file mode 100644
    index 00000000000000..069cf72b90fcb9
    --- /dev/null
    +++ b/lld/test/ELF/provide-defsym.s
    @@ -0,0 +1,39 @@
    +# REQUIRES: x86
    +
    +## A symbol defined by --defsym takes precedence over a PROVIDE directive and
    +## its value is used in linker script expressions, regardless of whether -T
    +## or --defsym comes first on the command line (GNU ld compatible).
    +
    +# RUN: rm -rf %t && split-file %s %t && cd %t
    +# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux a.s -o a.o
    +# RUN: ld.lld -o t1 a.o -T script.ld --defsym=_a=20
    +# RUN: ld.lld -o t2 a.o --defsym=_a=20 -T script.ld
    +# RUN: ld.lld -o t3 a.o -T script.ld
    +# RUN: llvm-readelf -s t1 | FileCheck %s --check-prefix=DEFSYM
    +# RUN: llvm-readelf -s t2 | FileCheck %s --check-prefix=DEFSYM
    +# RUN: llvm-readelf -W -S t1 | FileCheck %s --check-prefix=DEFSYM-SIZE
    +# RUN: llvm-readelf -W -S t2 | FileCheck %s --check-prefix=DEFSYM-SIZE
    +# RUN: llvm-readelf -W -S t3 | FileCheck %s --check-prefix=PROVIDE-SIZE
    +
    +# DEFSYM: 0000000000000014     0 NOTYPE  GLOBAL DEFAULT   ABS _a
    +
    +## _a * 1024 = 20 * 1024 = 0x5000.
    +# DEFSYM-SIZE: .test {{.*}} 005000
    +## _a * 1024 = 10 * 1024 = 0x2800.
    +# PROVIDE-SIZE: .test {{.*}} 002800
    +
    +#--- a.s
    +.text
    +.globl _start
    +_start:
    +  ret
    +
    +#--- script.ld
    +PROVIDE(_a = 10);
    +SECTIONS
    +{
    +  .test :
    +  {
    +    . += _a * 1024;
    +  }
    +}
    
    
    ``````

    </details>

    bai-tim bai-tim commented on Oct 15, 2026 13:27 UTC
    Author

    Could you please review @MaskRay @nga888 @keith @smithp35

    smithp35 smithp35 commented on code on Oct 15, 2026 16:04 UTC
    Code Review
    lld/ELF/LinkerScript.cpp:1
    @@ -1973,6 +1973,11 @@ bool LinkerScript::shouldAddProvideSym(StringRef symName) {
       // undefined references from the RHS, the result of this function for a
       // symbol must be the same for each call. We use unusedProvideSyms to not
       // change the return value of a demoted symbol.
    +  // Symbols defined with --defsym are considered defined before linker script

    I don't think this is the case https://sourceware.org/binutils/docs/ld/Options.html#index-symbols_002c-from-command-line

    The linker processes ‘--defsym’ arguments and ‘-T’ arguments in order, placing ‘--defsym’ before ‘-T’ will define the symbol before the linker script from ‘-T’ is processed, while placing ‘--defsym’ after ‘-T’ will define the symbol after the linker script has been processed. This difference has consequences for expressions within the linker script that use the ‘--defsym’ symbols, which order is correct will depend on what you are trying to achieve.

    As I understand it with --defsym before -T we have an equivalent linker script of

    _a = 20;
    PROVIDE(_a = 10);
    SECTIONS
    {
      .test :
      {
        . += _a * 1024;
      }
    }
    

    And with --defsym after -T we have an equivalent linker script of
    PROVIDE(_a = 10);
    SECTIONS
    {
      .text :
      {
        . += _a * 1024;
      }
    }
    _a = 20;
    

    It seems like GNU ld and lld process these differently. With GNU ld and _a = 20; at the end the symbol _a is set to 10, but _a = 20 is not a redefinition, it just updates the value. For GNU ld when the script is evaluated the second time the PROVIDE never executes because _a is defined so in effect _a is 20.

    I'm not sure that behaviour from GNU ld is actually desired. It definitely behaves differently to a script of

    _a = 10;
    SECTIONS
    {
      .text :
      {
        . += _a * 1024;
      }
    }
    _a = 20;
    

    I think it would be worth checking with binutils what the intended behaviour is?

    smithp35 smithp35 commented on code on Oct 15, 2026 16:08 UTC
    Code Review
    lld/ELF/LinkerScript.h:1
    @@ -433,6 +434,10 @@ class LinkerScript final {
       // A list of symbols referenced by the script.
       SmallVector<llvm::StringRef, 0> referencedSymbols;
     
    +  // Symbols defined with --defsym. GNU ld treats them as defined before linker

    I don't think there is a need to repeat the same comment 3 times (modulo small wording changes).

    smithp35 smithp35 commented on Oct 15, 2026 16:22 UTC
    Reviewer

    From a quick example I think lld may be correct and ld.bfd could be wrong (using its documentation rather than its implementation). I've left a more detailed comment in the source.

    From what I can tell both GNU ld and lld implement the ordering of --defsym correctly according to the ld.bfd documentation. In GNU ld's case it looks like a PROVIDE is only evaluated once, whereas in lld the PROVIDE assignment is evaluated each time.

    I think if this were to be "fixed" to behave like GNU ld, then we should fix the linker script differences, which should cause the defsym ordering difference to disappear.

    However I think LLD's behaviour may be closer to the intended behaviour than GNU ld, so I think we would want a consesnsus from LLD users/developers that this were the right thing to do first.

    In your description:

    fixing incorrect section sizes when -T and --defsym are used together.

    I think that is specific to your test case, so I think that should be removed.

    Reviewers
    Assignees
    bai-tim bai-tim
    Labels
    risk: low confidence: 88.7% 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: 37.3% (MEDIUM) · Higher = larger blast radius
    37.3%
    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: 88.7% · Lower = rushed or shallow review
    88.7%
    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: 4.2% · Safe to merge
    4.2%