<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/74641>74641</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            Fix ORC runtime atexit implementation on Linux. 
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            orcjit
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          lhames
      </td>
    </tr>
</table>

<pre>
    The current Linux atexit implementation in the ORC runtime (as of 7b83f69db4a) is:

```c++
int __orc_rt_elfnix_atexit(void (*func)(void *)) {
 auto &PlatformRTState = ELFNixPlatformRuntimeState::get();
  return ELFNixPlatformRuntimeState::get().registerAtExit(
      func, NULL, PlatformRTState.getPlatformJDDSOHandle());
}
```

But there's no reason to assume that `func` is in the `Platform` JITDylib, the common case is that it will not be (now that both `LLJIT` and the `llvm-jitlink` tool place the ORC runtime code in a separate JITDylib from the "main" code by default).

A better implementation would be:

```c++
int __orc_rt_elfnix_atexit(void (*func)(void *)) {
  auto &PlatformRTState = ELFNixPlatformRuntimeState::get();
  Dl_info Info;
  if (!dladdr(func, &Info))
    return -1; // Could not identify address. Error out.
 return ELFNixPlatformRuntimeState::get().registerAtExit(
      func, NULL, Info.dli_fbase);
}
```

but we'll need a JIT `dladdr` implementation before we can do that.

</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJy8VcFu4zYQ_Rr6MohBUbZkHXRw4hhNYOwWu-nZIMWRxZQiDXK0dv6-oGTtZtMe9tDWIEx4hnzzOPNmLGM0J4dYs_U9W-8WcqDOh9p2sse4UF6_1S8dQjOEgI7gYNxwBUl4NQSmP1vs0ZEk4x0YB9QhfP7yAGFwZHoEJjYygm-hVJu8LSqtVpKJCkxk-ZbxHePzd8Gn1TBxn9ZoNY7gePShOQY6om2duR6n2ExsvnmjUwAmtu3gGiaqH8bt-KsCVt6QQA7kgYnidyup9aH_8vKVJCGwfAePh_0nc_3umbiP7sQy356QxjgVy2c4CEhDcL9-dRnwZCJh2NLjxH9GSp_pAQ_w6Y_DIe0fWC5PSLPpebf7-vk36bTFG6l3vFi5-5DP90m-HyhVKCATZQTnIaCM3gF5kDEOPQJ1koAVfORTcDBxrior-MwgOZ6fXnZv1qhENrkb3_feQSMjpksjjiG4GGvBeQI1asH5y-RSnroEeTg8P70kPOn0HMbab_3dqyFr3J_JRd5bOFvZ4N_k1XiNiaCEiGcZUj1nYtAG30-QQvTSOCbEdF69gcZWDpZSWd7nZwsKiTB8FPbFD1aDwv9bs_-6aHf2aFzr4cm1_p3ZtBOnTFupdWBiM-uRiWI8O6nsu2Bv4r_LWH4PTOyZ2MPDmKRUa6PRkWnfIIFhjEt4DMEH8APd0v0fd0-ivNTWHFslI_56f6iB4JKaI4kWUYNMckqavCUmdcTP0lDY-oBwQWikA-1Hed9eudB1rqu8kguss5Jnm82m4HzR1bzhyIUoG9koXK3LKheNFLrKpSyVytYLUwsu8kzwItusOefLXBerVVaUmW6bFUfFVhx7aewyNcvSh9PCxDhgXa6KVbawUqGN40QXwofmNWVMpOke6rG71HCKbMWtiRR_QJAhi_XeXH_qsX-e9d5NfwVLWAzB1h3ReRzpoxZOhrpBLRvfM7FP8Lft7hz8KzbExH6kG5nYj4z_CgAA___hXgM_">