I apologize if my question is not precise because I don't have a lot of Linux related experience. I'm currently building a Linux from scratch (mostly following the guide at linuxfromscratch.org version 7.3). I ran into the following problem: when I build an executable it gets a hardcoded path to something called ELF interpreter.
readelf -l program
shows something like
[Requesting program interpreter: /lib/ld-linux.so.2]
I traced this library ld-linux-so.2 to be part of glibc. I am not very happy with this behaviour because it makes the binary very unportable - if I change the location of /lib/ld-linux.so.2 the executable no longer works and the only "fix" I found is to use the patchelf utility from NixOS to change the hardcoded path to another hardcoded path. For this reason I would like to link against a static version of the ld library but such is not produced. And so this is my question, could you please explain how could I build glibc so that it will produce a static version of ld-linux.so.2 which I could later link to my executables. I don't fully understand what this ld library does, but I assume this is the part that loads other dynamic libraries (or at least glibc.so). I would like to link my executables dynamically, but I would like the dynamic linker itself to be statically built into them, so they would not depend on hardcoded paths. Or alternatively I would like to be able to set the path to the interpreter with environment variable similar to LD_LIBRARY_PATH, maybe LD_INTERPRETER_PATH. The goal is to be able to produce portable binaries, that would run on any platform with the same ABI no matter what the directory structure is.
Some background that may be relevant: I'm using Slackware 14 x86 to build i686 compiler toolchain, so overall it is all x86 host and target. I am using glibc 2.17 and gcc 4.7.x.
/lib/ld-linux.so.2
is statically built (but still a shared library, not using any external library except the kernel provided VDSO). – Basile Starynkevitch