9

What is the delay load equivalent in unix based system.

I have a code foo.cpp, While compiling with gcc I link it to shared objects(totally three .so files are there.). Each of the .so file for different option.

./foo -v needs libversion.so ./foo -update needs libupdate.so

I need the symbol for those libraries should be resolved only at the run time.

./foo -v should not break even if libupdate.so library is not there.

It is working in windows using the delay load option(in properties of dll). What is its equivalent in unix systems.

Will -lazy option does the same in UNIX? If so, where to include this option: in makefile or with linker ld?

yugr
  • 19,769
  • 3
  • 51
  • 96
saran
  • 179
  • 2
  • 4

2 Answers2

3

See the reference on your system for dlopen(). You can manually open libraries and resolve external symbols at runtime rather than at link time.

Dug out an example:

int main(int argc, char **argv) {                 
    void *handle=NULL;                                 
    double (*myfunc)(double);                     
    char *err=NULL;                                  

    handle = dlopen ("/lib/libm.so.1", RTLD_LAZY);
    if (!handle) {                                
        err=dlerror();
        perror(err);
        exit(1);                                  
    }                                             

    myfunc = dlsym(handle, "sin");                
    if ((err = dlerror()) != NULL)  {           
        perror(err);
        exit(1);                                  
    }                                             

    printf("sin of 1 is:%f\n", (*myfunc)(1.));              
    dlclose(handle);            
    return 0;                  
}                                                 
jim mcnamara
  • 16,005
  • 2
  • 34
  • 51
  • 1
    Thanks. But it would be better if I can specify the Delay load equivalent option in linker instead of using dlopen. Is there not any option in GCC to lazy load the libxxx.so file?.Can we not use gcc -WI to pass the lazy load options to set the linker for lazy load?. will the linker set default to the lazy load?. Please help me.Thanks in advance – saran Jun 03 '10 at 09:34
  • 1
    Nope, using dlopen()/dlsym() is the portable way on unix. Some system's custom linkers can handle this (ex: http://docs.sun.com/app/docs/doc/817-1983/6mhm6r4er?l=en&a=view), but GNU ld has no such option. – pra Jun 03 '10 at 21:22
  • @pra If you dug up some documentation supporting your statement and posted that as an answer, I'd upvote it. – Brian Vandenberg Jul 27 '15 at 20:14
  • It's possible to automate generation of `dlopen`/`dlsym` boilerplate (see the other answer). – yugr Feb 16 '18 at 09:25
2

I know it has been 8 years but still...

Delay load is not supported out out the box on GNU systems but you can mimic it yourself by creating a small static stub which provides all necessary symbols and dlopens real implementation on first call (or even at program startup). Such stub can be written by hand, generated by project-specific script or via Implib.so tool:

# Replace
$ gcc -o foo foo.c -lversion
# with
$ implib-gen.py libversion.so
$ gcc -o foo foo.c libversion.tramp.S libversion.init.c
yugr
  • 19,769
  • 3
  • 51
  • 96