Skip to content

MSYS2: Export forwarders for __cxa_begin_catch, __cxa_end_catch, __cxa_rethrow and __gxx_personality_seh0 #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

qmfrederik
Copy link
Owner

libobjc2 uses native C++ exceptions on MinGW. The clang compiler will emit references to __cxa_begin_catch, __cxa_end_catch, __cxa_rethrow and __gxx_personality_seh0 for Objective C code which uses Objective C exceptions.

These symbols are defined in the C++ runtime, not in libobjc2. As a result, merely linking with libobjc2 is not sufficient. Objective C code such as GNUstep must be compiled with the LDFLAGS="-lgcc_s -lstdc++" or LDFLAGS="-lc++", depending on the environment.

This is tedious. Additionally, specifying -lc++ on the msys/clang64 environment causes linker errors:

 Linking library libgnustep-base ...
ld.lld: error: libc++.dll.a(libc++.dll): .idata$4 should not refer to special section 0

A similar error has been observed for other libraries

A solution for this is to define forwarding exports for __cxa_begin_catch, __cxa_end_catch, __cxa_rethrow and __gxx_personality_seh0. This is implemented by adding a eh_forwards.def file to the list of libobjc2 source files, which forwards the symbols to the actual C++ runtime. On MSYS2, the libstdc++ and libc++ runtimes are supported, which covers all MinGW environments: https://www.msys2.org/docs/environments/.

Forwarding exports are discussed here:

@qmfrederik qmfrederik force-pushed the mingw-export-cxx-symbols branch from d0c53a3 to 21e5314 Compare February 13, 2024 23:18
…`__cxa_rethrow` and `__gxx_personality_seh0`

libobjc2 uses native C++ exceptions on MinGW.  The clang compiler will emit references to `__cxa_begin_catch`, `__cxa_end_catch`, `__cxa_rethrow` and `__gxx_personality_seh0` for Objective C code which uses Objective C exceptions.

These symbols are defined in the C++ runtime, not in libobjc2.  As a result, merely linking with libobjc2 is not sufficient.  Objective C code such as GNUstep must be compiled with the `LDFLAGS="-lgcc_s -lstdc++"` or `LDFLAGS="-lc++"`, depending on the environment.

This is tedious.  Additionally, specifying `-lc++` on the msys/clang64 environment causes linker errors:

```
 Linking library libgnustep-base ...
ld.lld: error: libc++.dll.a(libc++.dll): .idata$4 should not refer to special section 0
```

A [similar error has been observed for other libraries](msys2/MINGW-packages#18589)

A solution for this is to define forwarding exports for `__cxa_begin_catch`, `__cxa_end_catch`, `__cxa_rethrow` and `__gxx_personality_seh0`.  This is implemented by adding a `eh_forwards.def` file to the list of libobjc2 source files, which forwards the symbols to the actual C++ runtime.  On MSYS2, the libstdc++ and libc++ runtimes are supported, which covers all MinGW environments: https://www.msys2.org/docs/environments/.

Forwarding exports are discussed here:
- https://learn.microsoft.com/en-us/cpp/build/reference/exports?view=msvc-170
- https://devblogs.microsoft.com/oldnewthing/20060719-24/?p=30473
- https://devblogs.microsoft.com/oldnewthing/20121116-00/?p=6073
@qmfrederik qmfrederik force-pushed the mingw-export-cxx-symbols branch from 21e5314 to 6ef33e5 Compare February 13, 2024 23:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant