11

g++ is built using either the DWARF2, sjlj or seh exception model. MinGW-builds provide various builds of g++ that have different exception models. I would like to be able to determine from the gcc toolchain what exception model is being used. Is there a g++ argument that will dump the default exception model of the compiler?

Matt Clarkson
  • 14,106
  • 10
  • 57
  • 85
  • 2
    Looks like you can determine if gcc is using `sjlj` by checking the output of the assembly compilation looking for `_Unwind_SjLj_Resume` or `_Unwind_Resume` as that's what the [configuration script](http://gcc.gnu.org/git/?p=gcc.git;a=blob_plain;f=libstdc%2B%2B-v3/configure;hb=HEAD) for `libstdc++` does – Matt Clarkson Jul 31 '13 at 10:20
  • 1
    You can also check for `--enable-sjlj-exceptions` in the output of `gcc -v` – Matt Clarkson Jul 31 '13 at 10:31

3 Answers3

15

Edit: Originally, I was testing for the configuration flags that are described in g++ -v. As Jonathon Wakely points out in the comments, this is not a good thing to do.

An inspection way to do it is to compile to assembly:

struct S { ~S(); };
void bar();
void foo() {
  S s;
  bar();
}

The result of g++ -S <filename> -o output.s have the following exception references in them:

MinGW-4.8.1-x86-posix-sjlj:

.def    ___gxx_personality_sj0; .scl    2;  .type   32; .endef
.def    __Unwind_SjLj_Register; .scl    2;  .type   32; .endef
.def    __Unwind_SjLj_Unregister;   .scl    2;  .type   32; .endef
.def    __Unwind_SjLj_Resume;   .scl    2;  .type   32; .endef

MinGW-4.8.1-x86-posix-dwarf:

.def    ___gxx_personality_v0;  .scl    2;  .type   32; .endef
.def    __Unwind_Resume;    .scl    2;  .type   32; .endef

MinGW-4.8.1-x64-win32-sjlj:

.def    __gxx_personality_sj0;  .scl    2;  .type   32; .endef
.def    _Unwind_SjLj_Register;  .scl    2;  .type   32; .endef
.def    _Unwind_SjLj_Unregister;    .scl    2;  .type   32; .endef
.def    _Unwind_SjLj_Resume;    .scl    2;  .type   32; .endef

MinGW-4.8.1-x64-posix-seh:

.def    __gxx_personality_seh0; .scl    2;  .type   32; .endef
.def    _Unwind_Resume; .scl    2;  .type   32; .endef

MinGW-4.8.1-x64-posix-sjlj:

.def    __gxx_personality_sj0;  .scl    2;  .type   32; .endef
.def    _Unwind_SjLj_Register;  .scl    2;  .type   32; .endef
.def    _Unwind_SjLj_Unregister;    .scl    2;  .type   32; .endef
.def    _Unwind_SjLj_Resume;    .scl    2;  .type   32; .endef

FC17-g++-4.7.2-x64:

.cfi_personality 0x3,__gxx_personality_v0
.globl  __gxx_personality_v0
call    _Unwind_Resume

Looks like we should search for __gxx_personality_([a-z])(0-9]+) and then compare the first capture group to:

  • v = dwarf
  • seh = seh
  • sj = sjlj
Matt Clarkson
  • 14,106
  • 10
  • 57
  • 85
  • 1
    This is incorrect, as the [docs](http://gcc.gnu.org/install/configure.html) say, the default (i.e. what's used when you don't use any `--enable` or `--disable` options) depends on the platform. – Jonathan Wakely Jul 31 '13 at 11:19
  • Thanks, Jonathan. Still looking at working out the exception model via compiling code. – Matt Clarkson Jul 31 '13 at 12:10
  • 2
    @JonathanWakely, I updated the answer, taking out the configuration flag checking, thanks for helping out with that. I added some examples of the compilation of the exception handling snippet that I found in the `stdlibc++` configuration script and then looked at the assembly to determine the correct base personalities. I used the following as reference: http://www.hexblog.com/wp-content/uploads/2012/06/Recon-2012-Skochinsky-Compiler-Internals.pdf – Matt Clarkson Jul 31 '13 at 12:56
4

Just to complement the answers above, GCC has a predefined macro allowing to recognize at compile time whether SJLJ exception model is used:

__USING_SJLJ_EXCEPTIONS__

This macro is defined, with value 1, if the compiler uses the old mechanism based on setjmp and longjmp for exception handling.

See https://gcc.gnu.org/onlinedocs/cpp/Common-Predefined-Macros.html

According to documentation, it is available since at least version 3.1.1; I have just tested it on GCC 7.1 (under MinGW-w64).

Andrey Betenev
  • 339
  • 2
  • 4
0

For me, finding corresponding DLL for each exception model works:
libgcc_s_dw2-1.dll (DWARF)
libgcc_s_seh-1.dll (SEH)
libgcc_s_sjlj-1.dll (SJLJ)
Reference: https://stackoverflow.com/a/19791995/16550663

irous
  • 401
  • 3
  • 8
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 07 '22 at 12:03