Creating an executable shared library

On Linux, with a C program you can create an executable shared library: linux - Why and how are some shared libraries runnable, as though they are executables? - Unix & Linux Stack Exchange

Would this be possible with Rust? I'm looking to distribute an application that can be both linked (via LD_PRELOAD) and executed.

1 Like

I've tried this a number of times, but all I get are segmentation faults. Adding -Wl,-e,entry just doesn't seem to work, nor does any other similar permutation. I tried to add a #[start] annotation and use nightly, but the shared object still segfaults. I also tried using an executable as shared library, with and without the linker flags that would set it to a shared library, and it didn't work. You will probably just have to make them separate.

Half of this can be done using --crate-type cdylib -Clink-arg=-Wl,-e,_star -Clink-arg=/usr/lib/x86_64-linux-gnu/Scrt1.o, however I couldn't find a way to set the PT_INTERP entry in the program headers to point to the dynamic linker outside of rewriting the executable using custom code. ld ignores --dynamic-linker for dynamic libraries and patchelf refuses to set the PT_INTERP if there isn't one already. Without setting PT_INTERP dynamic libraries produced this way can only be run as executables by prefixing them with the path to the dynamic linker like /lib64/ ./

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.