I have a elf binary which has been statically linked to libc. I do not have access to its C code. I would like to use OpenOnload library, which has implementation of sockets in user-space and therefore provides lower latency compared to standard libc versions. OpenOnload implements standard socket api, and overrides libc version using LD_PRELOAD. But, since this elf binary is statically linked, it cannot use the OpenOnload version of the socket API.
I believe that converting this binary to dynamically link with OpenOnload is possible, with the following steps:
- Add new Program headers: PT_INTERP, PT_DYNAMIC and PT_LOAD.
- Add entries in PT_DYNAMIC to list dependency with libc.
- Add PLT stubs for required libc functions in the new PT_LOAD section.
- Modify existing binary code for libc functions to jump to corresponding PLT stubs.
As a first cut, I tried just adding 3 PT_LOAD segments. New segment headers were added after existing PT_LOAD segment headers. Also, vm_addr of existing segments was not modified. File offsets of existing segments were shifted below to next aligned address based on p_align. New PT_LOAD segments were added in the file at end of the file.
After re-writing the file, when I ran it, it was loaded properly by the kernel, but then it immediately seg-faulted.
My questions are:
- If I just shift the file-offsets in the elf binary, without modifying the vm_addresses, can it cause any error while running the binary?
- Is it possible to do what I am attempting? Has anybody attempted it?
malloc
) will crash unpredictably if that expectation is violated. You may be better off running the entire binary through a disassembler, manually pruning out all of libc, and then reassembling and relinking it. – zwol