Issue 239792
Summary [BOLT] A tail call's return PC becomes 0 when SCTC folds it or BOLT removes the NOP after it
Labels BOLT
Assignees
Reporter not4juu
    ## Description

With `--update-debug-sections`, the return PC of a tail call site (`DW_AT_call_return_pc`) becomes `0` in two cases:

- BOLT removes a NOP after the tail-call branch,
- SCTC folds the tail-call `jmp` into the conditional branch before it.

GCC describes call sites by their return PC, so with a return PC of `0` the debugger cannot place the tail-call frame.

> [!NOTE]
> The same happens in DWARF 4, where the return PC is `DW_AT_low_pc` of `DW_TAG_GNU_call_site`, a GNU extension that clang also emits and BOLT already updates. The fix covers both.

**Environment:**

- **BOLT:** `llvm-bolt`, `llvm-dwarfdump` and `llvm-objdump` built from `72f18f704c03c8bdac29ba54d387e7d08fd1bad4`
- **Tools:** `gcc 14.2.0`, `aarch64-linux-gnu-g++ 14.1.1`, GNU `ld 2.46` (x86-64) and `2.41` (AArch64), `gdb 16.3`

## Reproducer

### AArch64: the NOP after a tail call

When branch targets are aligned, NOPs follow the tail-call `b`, and BOLT removes them:

```cpp
// lib.cpp
__attribute__((noinline)) int tail(int v) { return v * 7; }

__attribute__((noinline)) int f(int x, int *p) {
  if (x > 10)
    return tail(x);
  int s = 0;
  for (int i = 0; i < x; i++)
 s += p[i];
  return s;
}
```

```bash
# the a64fx tuning aligns branch targets, which puts NOPs after the tail-call b
aarch64-linux-gnu-g++ -O2 -mcpu=a64fx -gdwarf-5 -gz=none -fPIC -shared -nostdlib lib.cpp -o libt.so -Wl,--emit-relocs
mkdir -p bolt && llvm-bolt libt.so -o bolt/libt.so --update-debug-sections
llvm-objdump -d --no-show-raw-insn --disassemble-symbols=_Z1fiPi libt.so
llvm-objdump -d --no-show-raw-insn --disassemble-symbols=_Z1fiPi bolt/libt.so
llvm-dwarfdump --debug-info libt.so      | grep call_return_pc
llvm-dwarfdump --debug-info bolt/libt.so | grep call_return_pc

# libt.so                             # bolt/libt.so
2e8: b.gt 330                         400014: b.gt 400050
... ...
330: b    2b0 <_Z4taili@plt> 400050: b    2b0 <_Z4taili@plt>
334: nop 400054: movi v31.2d, #0
338: nop
33c: nop
340: movi v31.2d, #0
```

| | `libt.so` | `bolt/libt.so` | expected |
|---|---|---|---|
| `DW_AT_call_return_pc` | `0x334`: end of the `b`, start of the first `nop` | `0x0` | `0x400054`: end of the `b` |

### x86-64: the return PC after SCTC

GCC keeps the `jmp` for an ordinary condition, and SCTC folds it. Use `lib.cpp` from above, and:

```cpp
// main.cpp
int f(int x, int *p);
int a[16];
int main(int argc, char **) { return f(argc + 10, a) + 1; }
```

```bash
g++ -O2 -gdwarf-5 -gz=none -fPIC -shared lib.cpp -o libt.so -Wl,--emit-relocs   # and -gdwarf-4
g++ -O2 -g main.cpp -o main -L. -lt
mkdir -p bolt && llvm-bolt libt.so -o bolt/libt.so --update-debug-sections
```

```bash
llvm-objdump -d --no-show-raw-insn --disassemble-symbols=_Z1fiPi libt.so
llvm-objdump -d --no-show-raw-insn --disassemble-symbols=_Z1fiPi bolt/libt.so
llvm-dwarfdump --debug-info libt.so      | grep -A3 -E 'DW_TAG_(GNU_)?call_site$'
llvm-dwarfdump --debug-info bolt/libt.so | grep -A3 -E 'DW_TAG_(GNU_)?call_site$'
LD_LIBRARY_PATH=.    gdb -batch -ex 'set breakpoint pending on' -ex 'break tail' -ex run -ex bt ./main
LD_LIBRARY_PATH=bolt gdb -batch -ex 'set breakpoint pending on' -ex 'break tail' -ex run -ex bt ./main

# libt.so                             # bolt/libt.so
1123: jg    1150 <_Z1fiPi+0x30>       4000cd: jg    1030 <_Z4taili@plt>
1125: testl %edi, %edi                4000d3: testl %edi, %edi
...
1150: jmp   1030 <_Z4taili@plt>
1155: nopl  (%rax)
```

| | `libt.so` | `bolt/libt.so` | expected |
|---|---|---|---|
| `DW_AT_call_return_pc` (DWARF 4: `DW_AT_low_pc` of `DW_TAG_GNU_call_site`) | `0x1155`: end of the `jmp` | `0x0` | `0x4000d3`: end of the `jg` |
| gdb `bt` in `tail`, frame `#1` | `0x00007ffff7fba155 in f (x=<optimized out>, ...) at lib.cpp:6` | `0x00007ffff7a00000 in ?? ()` | `0x00007ffff7e000d3 in f (x=11, ...)` |

`0x00007ffff7a00000` is the load base of `bolt/libt.so`, i.e. a return PC of `0`.

Running BOLT on the same `libt.so` with `--simplify-conditional-tail-calls=0` disables SCTC: BOLT keeps the `jmp` but removes the `nopl` after it, so the return PC becomes `0` as on AArch64.

## Cause

A return PC goes through `translateInputToOutputAddress()` as the address it points to, not as the end of the call. When BOLT removes the NOP after the call, or SCTC removes the block with the `jmp`, that address is in no output block and has no address map entry, so it becomes `0`.

## Fix

A return PC that translates to `0` becomes the output end of the block that ends with the call (NOP case), or of the block that now holds the conditional branch (SCTC case; the address map has the branch at the input address of the `jmp`). This applies to `DW_AT_call_return_pc` and to `DW_AT_low_pc` of `DW_TAG_GNU_call_site`. The output code does not change; only the debug info does.

```diff
--- a/bolt/lib/Rewrite/DWARFRewriter.cpp
+++ b/bolt/lib/Rewrite/DWARFRewriter.cpp
@@ -1233,6 +1233,39 @@ void DWARFRewriter::updateUnitDebugInfo(
     }
   };
 
+  // Output return PC of a call that ends its block, for an input return PC
+  // with no output address: BOLT removed the code after the call (a NOP), or
+  // SCTC folded the call into the conditional branch before it.
+  auto getBlockEndReturnPC = [&](uint64_t ReturnPC) -> uint64_t {
+    if (!ReturnPC)
+      return 0;
+    const BinaryFunction *Function =
+ BC.getBinaryFunctionContainingAddress(ReturnPC - 1);
+    if (!Function)
+ return 0;
+    const BinaryBasicBlock *BB = Function->getBasicBlockContainingOffset(
+        ReturnPC - 1 - Function->getAddress());
+    if (!BB)
+      return 0;
+    if (BB->getOutputAddressRange().first)
+      return ReturnPC - Function->getAddress() == BB->getEndOffset()
+                 ? BB->getOutputAddressRange().second
+                 : 0;
+    // SCTC removed the block; the address map has the branch the call moved to.
+    if (!BC.hasIOAddressMap())
+      return 0;
+    std::optional<uint64_t> Branch =
+        BC.getIOAddressMap().lookup(Function->getAddress() + BB->getOffset());
+    if (!Branch)
+      return 0;
+    for (const BinaryBasicBlock &Other : Function->blocks()) {
+      const auto [Start, End] = Other.getOutputAddressRange();
+      if (Start <= *Branch && *Branch < End)
+        return End;
+    }
+    return 0;
+  };
+
   for (const std::unique_ptr<DIEBuilder::DIEInfo> &DI : DIs) {
     DIE *Die = DI->Die;
 switch (Die->getTag()) {
@@ -1394,6 +1427,9 @@ void DWARFRewriter::updateUnitDebugInfo(
         if (Function)
 UpdatedAddress =
 Function->translateInputToOutputAddress(UpdatedAddress);
+        if (!UpdatedAddress &&
+            AttrVal.getAttribute() == dwarf::DW_AT_call_return_pc)
+          UpdatedAddress = getBlockEndReturnPC(*Address);
 
         if (isIndexedAddressForm(AttrVal.getForm())) {
           const uint32_t Index =
@@ -1656,6 +1692,9 @@ void DWARFRewriter::updateUnitDebugInfo(
 << Die->getTag() << " to 0x"
                        << Twine::utohexstr(NewAddress) << '\n');
           }
+          // DWARF 4 GNU call site: DW_AT_low_pc is the return PC.
+          if (!NewAddress && Die->getTag() == dwarf::DW_TAG_GNU_call_site)
+            NewAddress = getBlockEndReturnPC(Address);
 
           dwarf::Form Form = LowPCAttrInfo.getForm();
           assert(Form != dwarf::DW_FORM_LLVM_addrx_offset &&
```

With the fix, all call sites get the values in the "expected" columns above. `check-bolt`, `check-large-bolt` and `check-large-bolt-arm` pass with the fix, with the same results as without it.

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

Reply via email to