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

    <tr>
        <th>Summary</th>
        <td>
            llvm-bolt registers .eh_frames which may refer to unmapped sections
        </td>
    </tr>

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

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

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

<pre>
    BOLT currently [calls](https://github.com/llvm/llvm-project/blob/0d191b7553e7efbf7ce57c77274b83c15b681933/bolt/lib/Rewrite/RewriteInstance.cpp#L3154) `RTDyld->finalizeWithMemoryManagerLocking();`, which is [documented](https://github.com/llvm/llvm-project/blob/0d191b7553e7efbf7ce57c77274b83c15b681933/llvm/include/llvm/ExecutionEngine/RuntimeDyld.h#L271-L273) to do three things:
* apply relocations;
* register `.eh_frame`s;
* update memory permissions.

The second step will ultimately call `__register_frame` if its present (e.g. [here](https://github.com/llvm/llvm-project/blob/ede600377cb6df1bef71f070130d8cfe734cc5b7/llvm/lib/ExecutionEngine/RuntimeDyld/RTDyldMemoryManager.cpp#L83)).

In PIC builds the personality function is [encoded](https://github.com/llvm/llvm-project/blob/ede600377cb6df1bef71f070130d8cfe734cc5b7/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp#L149) with indirection (`DW_EH_PE_indirect`) via a `DW.ref.__gxx_personality_v0` symbol [located](https://github.com/llvm/llvm-project/blob/41fba3c107a5bc99065f3bf8b9f5b9d52eab2d98/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp#L399) in the `.data` section, which is not mapped to the `RuntimeDyld`.

For example, for the following `main.cc`:
```c++
int Foo() {
  throw 42;
}

int main() {
  try {
    return Foo();
  } catch (...) {
    return 0;
  }
}
```
compiled with `clang++ main.cc -o main -fpic -Wl,-q` and processed with BOLT using `llvm-bolt main -o main.bolted -keep-tmp` one can see that the intermediary output object file doesn't contain the `.data` section.

Unfortunately this means that `__register_frame` can read unmapped data, e.g. LLVM's libunwind will do so when trying to [read](https://github.com/llvm/llvm-project/blob/41fba3c107a5bc99065f3bf8b9f5b9d52eab2d98/libunwind/src/DwarfParser.hpp#L380) the address of the personality routine and trying to [handle](https://github.com/llvm/llvm-project/blob/41fba3c107a5bc99065f3bf8b9f5b9d52eab2d98/libunwind/src/AddressSpace.hpp#L353) the `DW_EH_PE_indirect` case there.

I think that `llvm-bolt` doesn't need `.eh_frame`s to be registered in process and probably doesn't need to update memory permissions either (as it won't execute anything from the intermediary output object file in process), so probably the call to `RTDyld->finalizeWithMemoryManagerLocking()` should be changed to a call to `RTDyld->resolveRelocations()` - but I might be wrong and this may need a more careful approach, so filing this as a bug instead of just sending a patch for review.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJy9VtuO2zYQ_Rr5hbCgi2VZD37I3toFNkiQbptHg6JGFhOKVElqHffrO0PL9nqbLdosEECwRUkznDlz5nBq0-zXVx8eHpkYrQXt1Z5FxZXgSrmouImyVef94KL8XZTd4bWVvhvrWJgeF0o9Hf_mgzVfQHhc1srU-Jc0aZXWZVHkUEJbt6WAohRlmZWLepWLtKiXq7TKc7IwigyVJLtPsLPSw_nuXjvPtYBYDEOU5Q95WiyirGLRMvn0eLNXzTzKb1upuZJ_wWeM7z30xu7fc823YB-M-Cr1FhNBmyi_Qqsou2a7ToqOSUfJNkaMPaYOzU_LePIjtVBjA-cHt99AjF4afau3UgcQRu1lD5Rn3FH6WZnO8ScnCLxhjWG-swD4i2mGsJObKMHg3zE-DFhOC8oITk4d5X96a2ErnQdLQMbQbVrLe8D7y6_GoeEeWB8wZQPYXjpHvuLpm_D72AFzIIxuGLoc2E4qxUaFkaMxxkB8on02m-Oup-2YbJn0jg0WHBaBYQEg3sZUmA4svK0k0MAySfKyFPWyadMa2jJtkzJJ86RZiRbKfCFEUZfP_AQS_msdaBWId0G0Iz1XVBm8LvC51-zj_TWrR6kah6UCQt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