4

I need to write a little library, to be loaded by setting LD_PRELOAD, and which will override some functions in the standard C library but also call those functions in the standard library. Specifically, I want my shiny new library to let users provide their own resolv.conf so they can, eg, specify their default domain, or set aliases in it for long hostnames. I'm going to need a good understanding of how the linker works if I'm to do this, so can anyone point me at some good documentation, that doesn't assume that I know it all already and just need a reference, but also doesn't assume that I'm a complete idiot?

2
  • When you say you want to know how "the linker" works, what linker are you talking about? The GNU linker?
    – bta
    Feb 19, 2010 at 0:25
  • the GNU linker would be a good start. Once I have something that works with that, I should have all the keywords I need to google for the differences for the Sun/SGI/whatever tools.
    – DrHyde
    Feb 19, 2010 at 17:45

3 Answers 3

3

If you are using the GNU linker, one thing you need to know about is RTLD_NEXT. This allows you to "find the next occurrence of a function in the search order after the current library".

This way, you can override a standard function and still be able to call the original version.

I used this function in a version of malloc that tracks allocation stats. I needed to override malloc to collect the stats but I still wanted the original version of malloc to do the allocation:

static void *(*next_malloc)(size_t) = NULL;

__attribute__ ((constructor))
static void
bootstrap() 
{
    next_malloc = dlsym(RTLD_NEXT, "malloc");
}

void *
malloc(size_t size)
{
    void *ptr = next_malloc(size);

    // collect some stats

    return ptr;
}
0
1

Several good answers, but no one's mentioned the one resource that led me through this: Building library interposers for fun and profit.

1

It sounds like your interested in dynamic loading rather than linking in general. That's how ODBC finds and uses a particular driver at run time, like what you want to do with your resolver.

Rather than google you up a random link, I'll give you some keywords on dynamic linking:

Concepts: "dynamic loading" "Position Independent Code" soname

Tools: LD_DEBUG ldconfig ldd nm objdump

The C API for dynamic loading: dlfcn.h dlopen dlsym dlerror dlclose

I did find one good tutorial, which I remember being helpful when I was working with ODBC: http://www.ibm.com/developerworks/library/l-shobj/

However, it recommends calling ld directly, which is a no-no with gcc. Instead use the -shared option:

g++ -shared -fPIC foo.c -o libfoo.so.1.0 -Wl,soname,libfoo.so.1

Also: You should look for solutions that do not involve LD_PRELOAD or LD_LIBRARY_PATH, which are basically debug tools, and libraries that require them in production are flawed: Why LD_LIBRARY_PATH is bad LD_LIBRARY_PATH Is Not The Answer LD_LIBRARY_PATH - just say no

ODBC avoids it with an odbc.ini config file where you specify the full path to the .so to use.

3
  • Doesn't this assume that I have some control over the innards of the applications in use, so that they can search for and dynamically load the library? While I could patch all the apps, and rebuild them, and keep them in my home directory, I'd rather not do that. Yeah, it's a nasty hack, but I think that using LD_PRELOAD to shim something in between, for example, Firefox and the resolver is the right tool in this case.
    – DrHyde
    Feb 19, 2010 at 17:50
  • Nope. You can, and should, use ldconfig to define the .so search path in production systems. For one thing, it does caching. The paths in LD_PRELOAD must be searched at runtime... for EVERY dynamically linked library. Check out 'man ldconfig'; it's an enlightening look at how Linux is glued together.
    – olooney
    Feb 19, 2010 at 18:40
  • ldconfig only looks useful when run by root, because otherwise it can't update the cache. And I'm not root in all the places I want to use this.
    – DrHyde
    Feb 19, 2010 at 23:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.