Issue 175509
Summary [ORC JIT] ELFNixPlatform does not execute .fini_array (destructors) when loading object files
Labels new issue
Assignees
Reporter cyx-6
    ## Summary

ELFNixPlatform fails to execute .fini_array (destructor) functions during `LLJIT::deinitialize()` when code is loaded from native object files via `addObjectFile()`. The same code works correctly when loaded from LLVM IR via `addIRModule()`.

## Reproduction

Full reproducer with build scripts: https://github.com/cyx-6/llvm-orc-jit-dtor-issue

```bash
git clone https://github.com/cyx-6/llvm-orc-jit-dtor-issue.git
cd llvm-orc-jit-dtor-issue
bash prepare.sh
```

This passes (both ctor and dtor run):

```bash
bash runner_from_ll.sh
```

This fails with "Deinitializers did not run":

```bash
bash runner_from_obj.sh
```

# Behavior

| Input Format                         | Constructor (.init_array) | Destructor (.fini_array) |
|--------------------------------------|---------------------------|--------------------------|
| LLVM IR (.ll) via addIRModule()      | Runs                      | Runs                     |
| Object file (.o) via addObjectFile() | Runs                      | Does NOT run             |

# Root Cause Analysis

The issue stems from asymmetry between init and fini handling in ELFNixPlatform:

1. `isELFInitializerSection()` recognizes .init_array, .init, .ctors — but there is no corresponding `isELFFinalizerSection()` for .fini_array, .fini, .dtors
2. `ELFNixPlatformPlugin::registerInitSections()` registers .init_array sections with the ORC runtime — but there is no `registerFiniSections()` equivalent
3. `PerJITDylibState` in the ORC runtime has RecordedInits tracker — but no RecordedFinis tracker
4. `runInits()` is called during initialization — but there is no runFinis() called during deinitialization

In contrast, `GenericLLVMIRPlatformSupport` (used for IR modules) correctly handles both llvm.global_ctors and llvm.global_dtors metadata, which is why loading from .ll files works.

# Environment

- LLVM version: 18+ (also reproduced on trunk)
- Platform: Linux (x86_64, aarch64)
- Using ExecutorNativePlatform with liborc_rt

# Related Code

- llvm/lib/ExecutionEngine/Orc/ELFNixPlatform.cpp
- llvm/lib/ExecutionEngine/Orc/Shared/ObjectFormats.cpp
- compiler-rt/lib/orc/elfnix_platform.cpp

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

Reply via email to