Thread overview
[Issue 23061] Exceptions cannot be caught by catch statement in static build
Apr 28, 2022
Tomoya Tanjo
May 22, 2022
Tomoya Tanjo
May 24, 2022
Tomoya Tanjo
April 28, 2022
https://issues.dlang.org/show_bug.cgi?id=23061

Tomoya Tanjo <ttanjo@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Severity|normal                      |major

--- Comment #1 from Tomoya Tanjo <ttanjo@gmail.com> ---
I changed the severity to `major` because it prevents from using exception, that is a language feature of D.

--
May 22, 2022
https://issues.dlang.org/show_bug.cgi?id=23061

--- Comment #2 from Tomoya Tanjo <ttanjo@gmail.com> ---
I found that this issue only happens when dmd is built with `DRuntime_Use_Libunwind`.

I built dmd with `DRuntime_Use_Libunwind` on Alpine Linux edge and obtained the following results.

When `app.d` is built without `-lunwind`, it works as intended as follows:
```console
$ ./dmd/generated/linux/release/64/dmd -c app.d
$ cc app.o -o app -m64 -static -Xlinker --export-dynamic
-L./dmd/generated/linux/release/64/../../../../../phobos/generated/linux/release/64
-Xlinker -Bstatic -lphobos2 -lpthread -lm -lrt -ldl
$ ./app
$ echo $?
0
```

On the other hand, when it is built with `-lunwind`, it does not work as
reported.
```console
$ ./dmd/generated/linux/release/64/dmd -c app.d
$ cc app.o -o app -m64 -static -Xlinker --export-dynamic
-L./dmd/generated/linux/release/64/../../../../../phobos/generated/linux/release/64
-Xlinker -Bstatic -lphobos2 -lpthread -lm -lrt -ldl -lunwind
$ ./app
src/rt/dwarfeh.d:330: uncaught exception reached top of stack
This might happen if you're missing a top level catch in your fiber or signal
handler
object.Exception@app.d(5): aa
Aborted
$ echo $?
134
```

--
May 24, 2022
https://issues.dlang.org/show_bug.cgi?id=23061

Tomoya Tanjo <ttanjo@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|---                         |INVALID

--- Comment #3 from Tomoya Tanjo <ttanjo@gmail.com> ---
I close this issue because it is a matter of linker options rather than
druntime itself.
It only happens when using `gcc` on Alpine Linux.
It does not happen when using `clang`.

When we use `gcc` as a linker, it needs `-Xlinker --eh-frame-hdr`.
```console
$ dmd -c app.d
$ gcc app.o -o app -m64 -static -Xlinker --eh-frame-hdr -Xlinker
--export-dynamic -L/usr/lib/ -Xlinker -Bstatic -lphobos2 -lpthread -lm -lrt
-ldl -lunwind
$ echo $?
0
```

When we use `clang` as a linker, it works without `-Xlinker --eh-frame-hdr`
because `clang` implicitly passes them to `ld`.
```console
$ dmd -c app.d
$ clang app.o -o app -m64 -static -Xlinker --export-dynamic -L/usr/lib/
-Xlinker -Bstatic -lphobos2 -lpthread -lm -lrt -ldl -lunwind
$ ./app
$ echo $?
0
```

--