Idea for LD_PRELOAD (was Re: glibc developers refuse to support user land fake FS syscalls)

Adam J. Richter (adam@yggdrasil.com)
Sun, 4 Jul 1999 09:56:39 -0700


LD_PRELOAD in its current implementation is imperfect, but
is still amazingly useful. At Yggdrasil the "logwrites" facility
uses LD_PRELOAD to trap open, symlink, mkdir, etc. to track file
installations by arbitrary software packages (static programs that
modify files are adjusted to support this facility too). It has
also been invaluable to enable the Electric Fence to debug
malloc problems, and made possible zlib, an LD_PRELOAD library
that does transparent decompression.

The problem with LD_PRELOAD is that it can only replace function
definitions, rather than "wrap" additional work around functions, which
is what it is almost always used for. So, the LD_PRELOAD library usually
has to carefully duplicate the implementation details of the functions that
it replaces. I believe I have a solution, which would be downward
compatible with existing LD_PRELOAD libraries, which would make LD_PRELOAD
much more reliable, and which would make them LD_PRELOAD libraries
"stackable" too (so you could do transparent decompression and
log file operations, for example).

I would appreciate any comments on whether this idea is feasible
with the ELF binary format and current implementation of binutils and
ld-linux.so.2.

There are two steps to what I want to do:
(1) Add "ld --wrap" functionality that is currently available
for static libraries to dynamic libraries by storing a list
of "wrapped" symbols in the shared library.

(2) Make "ld --wrap" stackable, both for the shared and static
cases.

#2 needs a little more explanation about how this is possible,
so I'll explain. Currently, if I say "ld --wrap foo", the undefined
references to "foo" will be resolved to "__wrap_foo", and undefined
references to "__real_foo" will be resolved to "foo". Here it is
in chart form:

SYMBOL RESOLVES TO
foo __wrap_foo
__real_foo foo

The extension that I want to add is that if __wrap_foo is
defined again, then the following resolution will be done on
subsequent .o files on the command line (or shared libraries):

SYMBOL RESOLVES TO
previous __real_foo __wrap_foo
__real_foo previous __wrap_foo

Using this functionality it would be possible to write more
portable and stackable "wrappers" for the file system calls, malloc
debuggers and so on. There would be little or no need for these
wrappers to replicate the particular impelementation details of the
functions that they are replicating or to call internal glibc functions.

I don't know exactly if or when I expect to have the time to
implement this, but I thought I'd mention the idea to see if any binutils
or ELF experts out there have any reactions to the idea.

Adam J. Richter __ ______________ 4880 Stevens Creek Blvd, Suite 104
adam@yggdrasil.com \ / San Jose, California 95129-1034
+1 408 261-6630 | g g d r a s i l United States of America
fax +1 408 261-6631 "Free Software For The Rest Of Us."

-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.rutgers.edu
Please read the FAQ at http://www.tux.org/lkml/