[dyndbg][LLD][ELF] Add dynamic debugging support for `--wrap=<symbol>` #222990
<!--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:
````````````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>
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.
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.
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.
@smithp35, would you be OK to approve this now? Thanks.
Marked approved on my side. Ping to @MaskRay to see if he has any further comments or objections.
Ping @MaskRay, are you happy for this change to be submitted?
Ping @MaskRay.
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.
Approved these changes.
Test --gc-sections?
smithp35
nga888
Forward the
--wrap=<symbol>arguments to the "inner" relocatable link.In the "outer" main link, set the
isDynDbgRefflag appropriately for the symbols involved.