Thread overview
Dynamically calling shared objects from statically build executable allowed
Feb 10, 2020
Andre Pany
Feb 10, 2020
Andre Pany
Feb 11, 2020
Andre Pany
February 10, 2020
Hi,

while dynamically calling a shared object from a statically
build executable on linux, both LDC and DMD fails with a
non-zero code 139.

In my productive system the SO is written in C++,
for reproduction I wrote a sample SO in D.

You can find the example here
https://github.com/ldc-developers/ldc/issues/3310


Is this a bug with LDC and DMD, or is it not allowed
to dynamically call a SO from a statically build executable on linux?

Background: The SO file I get from third party, while the executable
I develop. I need to run the executable on a distroless docker image (SCRATCH),
therefore I try to reduce system dependencies on my side by building a static executable.

PS. DMD even fails without the static switch with:
Aborting from src/rt/sections_elf_shared.d(480) DSO being unregistered isn't current last one.

Kind regards
André


February 10, 2020
On Monday, 10 February 2020 at 04:41:31 UTC, Andre Pany wrote:
> Is this a bug with LDC and DMD, or is it not allowed
> to dynamically call a SO from a statically build executable on linux

On Unix systems it is not possible to dynamically load a library shared by a static executable, I don't know if it works differently for Windows.

This is because it is absolutely necessary to link libdl dynamically, you cannot link static.

For GNU/Linux systems there is this alternative to dlopen/dlsym that does not require dynamic link: https://www.gnu.org/software/libtool/manual/html_node/Dlpreopening.html
February 10, 2020
On Monday, 10 February 2020 at 13:14:50 UTC, Ernesto Castellotti wrote:
> On Monday, 10 February 2020 at 04:41:31 UTC, Andre Pany wrote:
>> Is this a bug with LDC and DMD, or is it not allowed
>> to dynamically call a SO from a statically build executable on linux
>
> On Unix systems it is not possible to dynamically load a library shared by a static executable, I don't know if it works differently for Windows.
>
> This is because it is absolutely necessary to link libdl dynamically, you cannot link static.
>
> For GNU/Linux systems there is this alternative to dlopen/dlsym that does not require dynamic link: https://www.gnu.org/software/libtool/manual/html_node/Dlpreopening.html

This is a topic I almost have no knowledge, therefore a question.

If I understand this StackOverflow question correctly, it is possible to call dynamically a shared object from a static library (with dlopen):

https://stackoverflow.com/questions/17862272/dlopen-a-dynamic-library-from-a-static-library-linux-c

But I understand from you it is not possible dynamically call a shared object from a static executable.

Therefore it works for static libraries but not for statically executables?

Kind regards
Andre
February 10, 2020
On Monday, 10 February 2020 at 19:00:36 UTC, Andre Pany wrote:
> On Monday, 10 February 2020 at 13:14:50 UTC, Ernesto Castellotti wrote:
>> On Monday, 10 February 2020 at 04:41:31 UTC, Andre Pany wrote:
>>> Is this a bug with LDC and DMD, or is it not allowed
>>> to dynamically call a SO from a statically build executable on linux
>>
>> On Unix systems it is not possible to dynamically load a library shared by a static executable, I don't know if it works differently for Windows.
>>
>> This is because it is absolutely necessary to link libdl dynamically, you cannot link static.
>>
>> For GNU/Linux systems there is this alternative to dlopen/dlsym that does not require dynamic link: https://www.gnu.org/software/libtool/manual/html_node/Dlpreopening.html
>
> This is a topic I almost have no knowledge, therefore a question.
>
> If I understand this StackOverflow question correctly, it is possible to call dynamically a shared object from a static library (with dlopen):
>
> https://stackoverflow.com/questions/17862272/dlopen-a-dynamic-library-from-a-static-library-linux-c
>
> But I understand from you it is not possible dynamically call a shared object from a static executable.
>
> Therefore it works for static libraries but not for statically executables?
>
> Kind regards
> Andre

Static libraries are simple collections of object files, there is no difference between linking a static library or several object files

If you notice when going to compile the executable linka libdl and the static library, then the executable will be linked to dynamic library and will not be a static executable.

February 11, 2020
On Monday, 10 February 2020 at 19:39:04 UTC, Ernesto Castellotti wrote:
> On Monday, 10 February 2020 at 19:00:36 UTC, Andre Pany wrote:
>> [...]
>
> Static libraries are simple collections of object files, there is no difference between linking a static library or several object files
>
> If you notice when going to compile the executable linka libdl and the static library, then the executable will be linked to dynamic library and will not be a static executable.

Thanks for the explanation.

Kind regards
André