Issue 237789
Summary [lld-macho] LTO link drops the FDEs of global functions: exceptions thrown through them terminate
Labels new issue
Assignees
Reporter psiha
    In an LTO link, ld64.lld drops the `__eh_frame` FDEs of functions that are **global** symbols defined in the LTO-generated object. Such a function then has no unwind info at all: its compact unwind encoding is `0x00000000` and it has no FDE. An exception thrown through it ends in `std::terminate` ("terminating due to uncaught exception").

Relinking the same LTO objects (`-object_path_lto`) as plain objects with the same ld64.lld, or with Apple's ld, keeps the FDEs.

### Reproducer (macOS arm64, Homebrew LLVM/lld 22.1.8 and 23.1.x)

`a.cpp`:
```cpp
#include <stdexcept>
[[noreturn, gnu::cold, clang::minsize]] __attribute__((noinline)) void throw_le() { throw std::length_error{"x"}; }
[[noreturn, gnu::cold, clang::minsize]] __attribute__((noinline)) void throw_oor(char const* m) { throw std::out_of_range{m}; }
```
`b.cpp`:
```cpp
#include <stdexcept>
#include <cstdio>
[[noreturn]] void throw_le(); [[noreturn]] void throw_oor(char const*);
__attribute__((noinline)) int f(int x) { if (x > 3) throw_le(); if (x > 2) throw_oor("y"); return x; }
int main(int argc, char**) { try { f(argc + 3); } catch (std::length_error const&) { std::puts("caught le"); }
  try { f(argc + 1); } catch (std::out_of_range const&) { std::puts("caught oor"); } }
```
```sh
v="-O3 -flto=thin -fomit-frame-pointer"
clang++ -std=c++2c $v -c a.cpp -o a.o
clang++ -std=c++2c $v -c b.cpp -o b.o
clang++ $v -fuse-ld=lld -Wl,-object_path_lto,$PWD/lto a.o b.o -o t
./t # terminates: uncaught exception
llvm-objdump --unwind-info t # e.g. main / throw_oor: encoding 0x00000000
llvm-dwarfdump --eh-frame t                # their FDEs are missing
clang++ -fuse-ld=lld lto/*.o -o t2 && ./t2 # relinked from the same LTO objects: FDEs present, both exceptions caught
```
In the LTO object itself (`lto/*.lto.o`) the affected functions have a "use DWARF" compact unwind entry (`0x03000000`) and a valid FDE.

Without LTO, or without `-fomit-frame-pointer` (most functions then get a compact frame entry and need no FDE), the program works. Functions that always need an FDE on Darwin, such as `[[clang::preserve_most]]` ones, are affected regardless of the frame pointer.

### Likely cause

- `SymbolTable::addDefined` deliberately keeps the bitcode file as the owner (`getFile()`) of a global symbol that the LTO-generated object redefines.
- `ObjFile::registerEhFrames` treats an FDE as dead when `funcSym->getFile() != this`.

So every FDE of a global function in an LTO object is discarded. Local symbols (and a function at section offset 0, which resolves to the local `ltmp0`) keep theirs, which matches what we see. `registerEhFrames` probably needs to accept a symbol whose owner is the bitcode file this LTO object was compiled from.

Possibly related: #58277.

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

Reply via email to