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

    <tr>
        <th>Summary</th>
        <td>
            Error in pointer calculation for ARM EHABI bare-metal in libcxxabi/src/cxx_personality.cpp?
        </td>
    </tr>

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

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

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

<pre>
    Please note that despite the comment (which appears to be accurate in our ARM EHABI bare-metal implementation of exception handling), the "#if defined(LIBCXXABI_BAREMETAL)" case **adds** "offset" (which is the value of ptr, an absolute relocation) back to ptr itself, thereby generating an incorrect address.  I believe this is a bug?  In our implementation, this generated an illegal memory address, and the address of offset/ptr was correct by itself.  The term offset is confusing in this case, as it is a direct address and not an offset.

 libcxxabi/src/cxx_personality.cpp (lines 342-360):

```
#if defined(_LIBCXXABI_ARM_EHABI)
static const void* read_target2_value(const void* ptr)
{
    uintptr_t offset = *reinterpret_cast<const uintptr_t*>(ptr);
    if (!offset)
        return 0;
    // "ARM EABI provides a TARGET2 relocation to describe these typeinfo
    // pointers. The reason being it allows their precise semantics to be
    // deferred to the linker. For bare-metal they turn into absolute
    // relocations. For linux they turn into GOT-REL relocations."
    // https://gcc.gnu.org/ml/gcc-patches/2009-08/msg00264.html
#if defined(LIBCXXABI_BAREMETAL)
    return reinterpret_cast<const void*>(reinterpret_cast<uintptr_t>(ptr) +
                                         offset);
#else
    return *reinterpret_cast<const void **>(reinterpret_cast<uintptr_t>(ptr) +
                                            offset);
#endif
}
```

</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJy1VU1z4jgQ_TXmooIyMjhw8AESMjtVmdqtVA5zo2S5DdoRlkuSk_Dv90k2AfIxu5d1uQxyt55ed79ul6Y6Fn9pEo5YYzwxvxeeVeRaFRfEpDkcqPEs4YuXvZJ7Jt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