← Back to Pull requests

[dyndbg][LLD][ELF] Add dynamic debugging support for `--wrap=<symbol>` #222990

Merged
nga888 merged 4 files (176 lines) on Oct 13, 2026 17:45 UTC
View on GitHub
nga888 nga888 commented on Sep 11, 2026 17:03 UTC
Author

Forward the --wrap=<symbol> arguments to the "inner" relocatable link.
In the "outer" main link, set the isDynDbgRef flag appropriately for the symbols involved.

Diff: 176 lines (+171 / -5) across 4 files Reviewer: smithp35 Review duration: 46121m
All Vouch review confidence checks passed
Residual risk 0.05 = Change Risk (0.38) × (1 − Review Confidence (0.87)). Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.38 · 176 lines diff across 4 files
0.38
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.82 · 5 substantive comment(s)
0.82
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 50%
86%
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.310
Real PR Conversation Timeline
12 items
llvmorg-github-actions[bot] llvmorg-github-actions[bot] commented on Sep 11, 2026 17:04 UTC
Bot

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

@llvm/pr-subscribers-lld

Author: Andrew Ng (nga888)

<details>
<summary>Changes</summary>

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

4 Files Affected:

  • (modified) lld/ELF/Driver.cpp (+7-3)

  • (modified) lld/ELF/SymbolTable.cpp (+9)

  • (added) lld/test/ELF/wrap-dynamic-debug.test (+135)

  • (modified) llvm/docs/DynamicDebugging.md (-2)
  • ``````

    diff --git a/lld/ELF/Driver.cpp b/lld/ELF/Driver.cpp
    index 788e58a9b93d3..7253ec03dedaa 100644
    --- a/lld/ELF/Driver.cpp
    +++ b/lld/ELF/Driver.cpp
    @@ -3252,7 +3252,8 @@ static void postParseObjectFile(ELFFileBase *file) {
     // extract these embedded ELF files to make a consolidated unoptimized
     // relocatable ELF file to embed in an ELF Dynamic Debugging section in the
     // output. See llvm/docs/DynamicDebugging.md for more details.
    -template <class ELFT> static void linkDynamicDebug(Ctx &ctx) {
    +template <class ELFT>
    +static void linkDynamicDebug(Ctx &ctx, ArrayRef<WrappedSymbol> wrapped) {
       Ctx dctx;
       LinkerScript script(dctx);
       dctx.e.initialize(ctx.e.outs(), ctx.e.errs(), ctx.e.exitEarly,
    @@ -3273,11 +3274,14 @@ template <class ELFT> static void linkDynamicDebug(Ctx &ctx) {
       if (errCount(ctx))
         return;
     
    +  auto saveArg = [&](const Twine &arg) { return dctx.saver.save(arg).data(); };
       std::vector<const char *> args{
           dctx.arg.progName.data(), "-r", "-o", "-",
    -      dctx.saver.save(Twine("-O") + Twine(ctx.arg.optimize)).data()};
    +      saveArg(Twine("-O") + Twine(ctx.arg.optimize))};
       if (ctx.arg.resolveGroups)
         args.push_back("--force-group-allocation");
    +  for (const auto &ws : wrapped)
    +    args.push_back(saveArg(Twine("--wrap=") + ws.sym->getName()));
       dctx.driver.linkerMain(args);
       if (errCount(dctx) > 0 || !dctx.dynDbgOutput) {
         Err(ctx) << "failed to create relocatable dynamic debug object";
    @@ -3590,7 +3594,7 @@ template <class ELFT> void LinkerDriver::link(opt::InputArgList &args) {
     
       if (ctx.hasDynDbg) {
         llvm::TimeTraceScope timeScope("Link dynamic debugging");
    -    linkDynamicDebug<ELFT>(ctx);
    +    linkDynamicDebug<ELFT>(ctx, wrapped);
         if (errCount(ctx))
           return;
       }
    diff --git a/lld/ELF/SymbolTable.cpp b/lld/ELF/SymbolTable.cpp
    index da0293e9ec83f..abe595d9fe258 100644
    --- a/lld/ELF/SymbolTable.cpp
    +++ b/lld/ELF/SymbolTable.cpp
    @@ -48,6 +48,15 @@ void SymbolTable::wrap(Symbol *sym, Symbol *real, Symbol *wrap) {
         // keep sym if it was defined, otherwise it's unused and can be dropped.
         sym->isUsedInRegularObj = false;
     
    +  if (ctx.hasDynDbg) {
    +    if (sym->isDynDbgRef)
    +      wrap->isDynDbgRef = true;
    +    if (real->isDynDbgRef)
    +      sym->isDynDbgRef = true;
    +    else if (!sym->isDefined())
    +      sym->isDynDbgRef = false;
    +  }
    +
       // Now renaming is complete, and no one refers to real. We drop real from
       // .symtab and .dynsym. If real is undefined, it is important that we don't
       // leave it in .dynsym, because otherwise it might lead to an undefined symbol
    diff --git a/lld/test/ELF/wrap-dynamic-debug.test b/lld/test/ELF/wrap-dynamic-debug.test
    new file mode 100644
    index 0000000000000..f0d80d22e80fb
    --- /dev/null
    +++ b/lld/test/ELF/wrap-dynamic-debug.test
    @@ -0,0 +1,135 @@
    +# REQUIRES: x86
    +
    +## This test simulates the input from dynamic debugging objects to test ELF LLD's handling
    +## of option `--wrap=<symbol>`.
    +
    +# RUN: rm -rf %t && split-file %s %t && cd %t
    +
    +## Create object without dynamic debugging.
    +# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux test.s -o test.o
    +
    +## Create the "outer" optimized objects.
    +# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux foo.s -o foo.o
    +
    +## Create and embed the "inner" unoptimized objects to simulate dynamic debugging objects.
    +## Note that the embedded `.debug_llvm_dyndbg` section should be aligned to 8.
    +# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux foo-unopt.s -o foo-unopt.o
    +# RUN: llvm-objcopy --add-section=.debug_llvm_dyndbg=foo-unopt.o foo.o
    +# RUN: llvm-objcopy --set-section-type=.debug_llvm_dyndbg=0x6fff4c10 \
    +# RUN:              --set-section-alignment=.debug_llvm_dyndbg=8 foo.o
    +
    +## Link with wrapping.
    +# RUN: ld.lld --wrap=f --wrap=g --wrap=h -o out test.o foo.o
    +# RUN: llvm-readelf -s out | FileCheck %s
    +
    +# CHECK: Symbol table '.symtab' contains 8 entries:
    +# CHECK:      {{.*}} 0 FUNC GLOBAL DEFAULT [[#]] _start
    +# CHECK-NEXT: {{.*}} 0 FUNC GLOBAL DEFAULT [[#]] g
    +# CHECK-NEXT: {{.*}} 0 FUNC GLOBAL DEFAULT [[#]] h
    +# CHECK-NEXT: {{.*}} 0 FUNC GLOBAL DEFAULT [[#]] foo
    +# CHECK-NEXT: {{.*}} 0 FUNC GLOBAL DEFAULT [[#]] __wrap_f
    +# CHECK-NEXT: {{.*}} 0 FUNC GLOBAL DEFAULT [[#]] __wrap_g
    +# CHECK-NEXT: {{.*}} 0 FUNC GLOBAL DEFAULT [[#]] __wrap_h
    +
    +## Extract and check "inner" ELF.
    +# RUN: llvm-objcopy --dump-section=.debug_llvm_dyndbg=in-dd.o out /dev/null
    +# RUN: llvm-readelf -rs in-dd.o | FileCheck --check-prefix=IN %s
    +
    +# IN: Relocation section '.rela.text' at offset {{.*}} contains 5 entries:
    +# IN:      R_X86_64_PLT32 0000000000000000 __wrap_f - 4
    +# IN-NEXT: R_X86_64_PLT32 0000000000000000 __wrap_g - 4
    +# IN-NEXT: R_X86_64_PLT32 0000000000000000 __wrap_h - 4
    +# IN-NEXT: R_X86_64_PLT32 0000000000000000 g - 4
    +# IN-NEXT: R_X86_64_PLT32 0000000000000000 h - 4
    +
    +# IN: Symbol table '.symtab' contains 11 entries:
    +# IN:      {{.*}} 0 FUNC    GLOBAL HIDDEN  [[#]] __dyndbg.foo
    +# IN-NEXT: {{.*}} 0 NOTYPE  GLOBAL DEFAULT UND   g
    +# IN-NEXT: {{.*}} 0 NOTYPE  GLOBAL DEFAULT UND   h
    +# IN-NEXT: {{.*}} 0 FUNC    GLOBAL HIDDEN  [[#]] __dyndbg.__wrap_f
    +# IN-NEXT: {{.*}} 0 FUNC    GLOBAL HIDDEN  [[#]] __dyndbg.__wrap_g
    +# IN-NEXT: {{.*}} 0 FUNC    GLOBAL HIDDEN  [[#]] __dyndbg.__wrap_h
    +# IN-NEXT: {{.*}} 0 NOTYPE  GLOBAL DEFAULT UND   __wrap_f
    +# IN-NEXT: {{.*}} 0 NOTYPE  GLOBAL DEFAULT UND   __wrap_g
    +# IN-NEXT: {{.*}} 0 NOTYPE  GLOBAL DEFAULT UND   __wrap_h
    +
    +#--- test.s
    +.global _start
    +.type _start,@function
    +_start:
    +  ret
    +
    +.global g
    +.type g,@function
    +g:
    +  ret
    +
    +.global h
    +.type h,@function
    +h:
    +  ret
    +
    +#--- foo.s
    +## Optimized version of "foo"
    +
    +.global foo
    +.type foo,@function
    +foo:
    +  call f
    +  call g
    +  ret
    +
    +## Wrapped but does not call "real".
    +.global __wrap_f
    +.type __wrap_f,@function
    +__wrap_f:
    +  ret
    +
    +## Wrapped that calls "real".
    +.global __wrap_g
    +.type __wrap_g,@function
    +__wrap_g:
    +  call __real_g
    +  ret
    +
    +## Wrapped that calls "real" but only used in "inner" unopt.
    +.global __wrap_h
    +.type __wrap_h,@function
    +__wrap_h:
    +  call __real_h
    +  ret
    +
    +#--- foo-unopt.s
    +## Unoptimized version of "foo"
    +
    +.global __dyndbg.foo
    +.hidden __dyndbg.foo
    +.type __dyndbg.foo,@function
    +__dyndbg.foo:
    +  call f
    +  call g
    +  call h
    +  ret
    +
    +## Wrapped but does not call "real".
    +.global __dyndbg.__wrap_f
    +.hidden __dyndbg.__wrap_f
    +.type __dyndbg.__wrap_f,@function
    +__dyndbg.__wrap_f:
    +  ret
    +
    +## Wrapped that calls "real".
    +.global __dyndbg.__wrap_g
    +.hidden __dyndbg.__wrap_g
    +.type __dyndbg.__wrap_g,@function
    +__dyndbg.__wrap_g:
    +  call __real_g
    +  ret
    +
    +## Wrapped that calls "real" but only used in "inner" unopt.
    +.global __dyndbg.__wrap_h
    +.hidden __dyndbg.__wrap_h
    +.type __dyndbg.__wrap_h,@function
    +__dyndbg.__wrap_h:
    +  call __real_h
    +  ret
    diff --git a/llvm/docs/DynamicDebugging.md b/llvm/docs/DynamicDebugging.md
    index ce38063d13946..d82931e848494 100644
    --- a/llvm/docs/DynamicDebugging.md
    +++ b/llvm/docs/DynamicDebugging.md
    @@ -18,7 +18,6 @@ Important info:
     
     * Currently the feature is only supported with the ELF file format. Non-x86 targets are not supported yet; although they should work the feature has only been tested on x86 platforms.
     * LLDB does not support Dynamic Debugging.
    -* LLD does not support `--wrap=<symbol>` with Dynamic Debugging.
     * Dynamic Debugging with LTO is not supported.
     
     When adding support for additional targets, `prepareForDynamicDebugging` needs to be taught the correct `tail-pad-to-size` and `tail-pad-value` attribute values, which would otherwise be omitted.
    @@ -98,6 +97,5 @@ Linking of output with dynamic debugging has a significant impact on memory usag
     # Future work
     
     * LLD support for `--gc-sections` is limited and therefore has reduced output size savings.
    -* LLD support for `--wrap=<symbol>`.
     * LTO isn't supported with the feature.
     * An inline instance of a function may have its instructions completely optimized away which also removes the debug metadata specifying that there's an inline scope. In this situation a debugger wouldn't be able to patch the parent function when a breakpoint is set on the inlinee. It could be possible for LLVM to preserve the inline scope metadata in order to emit an empty inline scope in DWARF (inline subroutine without any PC ranges), which would be a useful signal to the debugger that this has happened.
    
    
    ``````

    </details>

    smithp35 smithp35 commented on Sep 15, 2026 14:52 UTC
    Reviewer

    This looks OK to me. Would you be able to add a short description for the commit message. Something like:

    * The --wrap=<symbol> arguments are forwarded to the relocatable link.
    * In the main link set is isDynDbgRef flag appropriately for the symbols involved.
    

    smithp35 smithp35 commented on Sep 15, 2026 16:03 UTC
    Reviewer

    LGTM. I'll leave some time for MaskRay to comment. If it is quiet for a while give me a ping and I'll approve.

    nga888 nga888 commented on Sep 15, 2026 16:31 UTC
    Author

    LGTM. I'll leave some time for MaskRay to comment. If it is quiet for a while give me a ping and I'll approve.

    Thanks.

    nga888 nga888 commented on Sep 21, 2026 18:18 UTC
    Author

    @smithp35, would you be OK to approve this now? Thanks.

    smithp35 smithp35 approved these changes on Sep 22, 2026 14:49 UTC
    Approved Reviewer

    Marked approved on my side. Ping to @MaskRay to see if he has any further comments or objections.

    nga888 nga888 commented on Sep 29, 2026 10:15 UTC
    Author

    Ping @MaskRay, are you happy for this change to be submitted?

    nga888 nga888 commented on Oct 06, 2026 18:53 UTC
    Author

    Ping @MaskRay.

    MaskRay MaskRay commented on Oct 07, 2026 05:22 UTC
    Contributor

    Sorry for missing your previous mentions. LGTM.

    There are many scenarios on which of x, __wrap_x, __real_x is present. We had a lot of issues on handing them. This is a good start. I assume that the availability of x, __wrap_x, __real_x restricts how the inner object makes them available, so you are not dealing with every edge case.

    MaskRay MaskRay approved these changes on Oct 07, 2026 05:22 UTC
    Approved Reviewer

    Approved these changes.

    MaskRay MaskRay commented on Oct 07, 2026 05:24 UTC
    Contributor

    Test --gc-sections?

    Reviewers
    Assignees
    nga888 nga888
    Labels
    risk: low confidence: 86.6% 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: 38.4% (MEDIUM) · Higher = larger blast radius
    38.4%
    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: 86.6% · Lower = rushed or shallow review
    86.6%
    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: 5.1% · Safe to merge
    5.1%