在 UNIX 上,我可以使用如下命令行:
cc -o executable *.o -zlazyload -lsomelib
with the result that the libraries listed to the right of -zlazyload
are marked with the LAZYLOAD ELF tag in the binary. This may be verified by calling dump -Lv executable
and the result contains e.g.:
**** DYNAMIC SECTION INFORMATION ****
.dynamic:
[INDEX] Tag Value
[1] POSFLAG_1 LAZYLOAD
[2] NEEDED libsecdb.so.1
In this case, libsecdb
is not loaded at the startup time of the executable, but delayed to the time, when the first function from libsecdb
is called.
This trick may be used to keep the incore representation of the executable smaller if it does not use all features.
Is there a way to achive the same on Linux? The GNU linker seems to have a flag -zlazy
, but my experience is that this is without effect.
The background for this question is that on Solaris, it is simple to link the current Bourne Shell (bosh) using lazy linking and this results in a shell that is nearly as small as dash
when executing shell scripts, but that is still faster than dash
. The shared libraries for the interactive history editor are only linked if the shell is used in interactive mode.
答案1
I may be wrong, but I think that there are two things.
- loading (that is, mmap'ing the shared library into memory) and
- binding (that is, resolving symbols)
On Linux, -zlazy
turns on lazy binding (and it is the default, so adding it changes nothing). I'm not aware of any lazy loading on Linux.