Search code examples
cgccshared-librarieselfdynamic-loading

What exactly does `-rdynamic` do and when exactly is it needed?


What exactly does -rdynamic (or --export-dynamic at the linker level) do and how does it relate to symbol visibility as defined by the -fvisibility* flags or visibility pragmas and __attribute__s?

For --export-dynamic, ld(1) mentions:

... If you use "dlopen" to load a dynamic object which needs to refer back to the symbols defined by the program, rather than some other dynamic object, then you will probably need to use this option when linking the program itself. ...

I'm not sure I completely understand this. Could you please provide an example that doesn't work without -rdynamic but does with it?

Edit: I actually tried compiling a couple of dummy libraries (single file, multi-file, various -O levels, some inter-function calls, some hidden symbols, some visible), with and without -rdynamic, and so far I've been getting byte-identical outputs (when keeping all other flags constant of course), which is quite puzzling.


Solution

  • Here is a simple example project to illustrate the use of -rdynamic.

    bar.c

    extern void foo(void);
    
    void bar(void)
    {
        foo();
    }
    

    main.c

    #include <dlfcn.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    void foo(void)
    {
        puts("Hello world");
    }
    
    int main(void)
    {
        void * dlh = dlopen("./libbar.so", RTLD_NOW);
        if (!dlh) {
            fprintf(stderr, "%s\n", dlerror());
            exit(EXIT_FAILURE); 
        }
        void (*bar)(void) = dlsym(dlh,"bar");
        if (!bar) {
            fprintf(stderr, "%s\n", dlerror());
            exit(EXIT_FAILURE); 
        }
        bar();
        return 0;
    }
    

    Makefile

    .PHONY: all clean test
    
    LDEXTRAFLAGS ?=
    
    all: prog
    
    bar.o: bar.c
        gcc -c -Wall -fpic -o $@ $<
        
    libbar.so: bar.o
        gcc -shared -o $@ $<
        
    main.o: main.c
        gcc -c -Wall -o $@ $<
    
    prog: main.o | libbar.so
        gcc $(LDEXTRAFLAGS) -o $@ $< -ldl
        
    clean:
        rm -f *.o *.so prog
        
    test: prog
        ./$<
            
    

    Here, bar.c becomes a shared library libbar.so and main.c becomes a program that dlopens libbar and calls bar() from that library. bar() calls foo(), which is external in bar.c and defined in main.c.

    So, without -rdynamic:

    gcc -c -Wall -o main.o main.c
    gcc -c -Wall -fpic -o bar.o bar.c
    gcc -shared -o libbar.so bar.o
    gcc  -o prog main.o -ldl
    ./prog
    ./libbar.so: undefined symbol: foo
    make: *** [Makefile:23: test] Error 1
    

    And with -rdynamic:

    $ make test LDEXTRAFLAGS=-rdynamic
    gcc -c -Wall -o main.o main.c
    gcc -c -Wall -fpic -o bar.o bar.c
    gcc -shared -o libbar.so bar.o
    gcc -rdynamic -o prog main.o -ldl
    ./prog
    Hello world
    

    As you noted, the gcc option -rdynamic enables the linker option --export-dynamic. So it is a shorthand for passing -Wl,--export-dynamic to gcc.

    By default, when linking a program (as opposed to a shared library) the static linker does not propagate an external symbol foo that is statically defined by the program from its global symbol table to its dynamic symbol table unless it determines that foo is referenced by a shared library in the linkage - i.e. unless the linker can see that foo needs to be dynamically exported to resolve a reference in the linkage. If foo is not dynamically exported it will not be visible to the dynamic linker at runtime and will be unavailable to resolve undefined dynamic symbol references in any shared library loaded by the program - such as libbar.sos reference to foo. If foo is referenced only by a shared library that is dlopen-ed programatically, then the static linker cannot determine whether or not foo will be referenced (because it is unaware of dlopen-ed libraries) and will not propagate foo to the dynamic symbol table.

    The --export-dynamic option countermands this default behaviour, causing any external symbol that is statically defined by the program to be propagated to its dynamic symbol table.

    The effect of -rdynamic/--export-dynamic is manifest not only in the dynamic symbol table of a program that performs dynamic linkage programatically, via dlopen and friends, although the effect will not be useful when dlopen and friends are not in play. Here is an example of its effect in a program that performs dynamic linkage in the automated way, having it set up via the dynamic section that the static linker writes into the program.

    bar.c - no change.

    main.c

    #include <stdio.h>
    
    extern void bar();
    
    void boo(void)
    {
        puts("Goodbye world");
    }
    
    void foo(void)
    {
        puts("Hello world");
    }
    
    int main(void)
    {
        bar();
        return 0;
    }
    

    Makefile

    .PHONY: all clean test
    
    LDEXTRAFLAGS ?=
    
    all: prog
    
    bar.o: bar.c
        gcc -c -Wall -fpic -o $@ $<
        
    libbar.so: bar.o
        gcc -shared -o $@ $<
        
    main.o: main.c
        gcc -c -Wall -o $@ $<
    
    prog: main.o | libbar.so
        gcc $(LDEXTRAFLAGS) -o $@ $< -L. -lbar -Wl,-rpath='$$ORIGIN'
        
    clean:
        rm -f *.o *.so prog
        
    test: prog
        ./$<
        
    

    In this example, both boo and foo are defined but unreferenced in main.c, but foo remains referenced by libbar.so while boo is not referenced in the linkage at all.

    Without -Wl,--export-dynamic:

    $ make clean
    rm -f *.o *.so prog
    $ make test
    gcc -c -Wall -o main.o main.c
    gcc -c -Wall -fpic -o bar.o bar.c
    gcc -shared -o libbar.so bar.o
    gcc  -o prog main.o -L. -lbar -Wl,-rpath='$ORIGIN'
    ./prog
    Hello world
    

    We see that foo is propagated to the dynamic symbol table of prog, because it is referenced from libbar.so:

    $ readelf --dyn-syms --wide prog
    
    Symbol table '.dynsym' contains 9 entries:
       Num:    Value          Size Type    Bind   Vis      Ndx Name
         0: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT  UND 
         1: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND __libc_start_main@GLIBC_2.34 (2)
         2: 0000000000000000     0 NOTYPE  WEAK   DEFAULT  UND _ITM_deregisterTMCloneTable
         3: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND puts@GLIBC_2.2.5 (3)
         4: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND bar
         5: 0000000000000000     0 NOTYPE  WEAK   DEFAULT  UND __gmon_start__
         6: 0000000000000000     0 NOTYPE  WEAK   DEFAULT  UND _ITM_registerTMCloneTable
         7: 0000000000000000     0 FUNC    WEAK   DEFAULT  UND __cxa_finalize@GLIBC_2.2.5 (3)
         8: 0000000000001183    26 FUNC    GLOBAL DEFAULT   16 foo
         
    

    But boo is absent.

    With -Wl,--export-dynamic:

    $ make clean
    rm -f *.o *.so prog
    $ make LDEXTRAFLAGS=-Wl,--export-dynamic test
    gcc -c -Wall -o main.o main.c
    gcc -c -Wall -fpic -o bar.o bar.c
    gcc -shared -o libbar.so bar.o
    gcc -Wl,--export-dynamic -o prog main.o -L. -lbar -Wl,-rpath='$ORIGIN'
    ./prog
    Hello world
    $ readelf --dyn-syms --wide prog
    
    Symbol table '.dynsym' contains 18 entries:
       Num:    Value          Size Type    Bind   Vis      Ndx Name
         0: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT  UND 
         1: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND __libc_start_main@GLIBC_2.34 (2)
         2: 0000000000000000     0 NOTYPE  WEAK   DEFAULT  UND _ITM_deregisterTMCloneTable
         3: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND puts@GLIBC_2.2.5 (3)
         4: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND bar
         5: 0000000000000000     0 NOTYPE  WEAK   DEFAULT  UND __gmon_start__
         6: 0000000000000000     0 NOTYPE  WEAK   DEFAULT  UND _ITM_registerTMCloneTable
         7: 0000000000004010     0 NOTYPE  GLOBAL DEFAULT   25 _edata
         8: 0000000000004000     0 NOTYPE  GLOBAL DEFAULT   25 __data_start
         9: 0000000000001183    26 FUNC    GLOBAL DEFAULT   16 foo
        10: 0000000000004018     0 NOTYPE  GLOBAL DEFAULT   26 _end
        11: 0000000000001169    26 FUNC    GLOBAL DEFAULT   16 boo
        12: 0000000000000000     0 FUNC    WEAK   DEFAULT  UND __cxa_finalize@GLIBC_2.2.5 (3)
        13: 0000000000004000     0 NOTYPE  WEAK   DEFAULT   25 data_start
        14: 0000000000002000     4 OBJECT  GLOBAL DEFAULT   18 _IO_stdin_used
        15: 0000000000001080    38 FUNC    GLOBAL DEFAULT   16 _start
        16: 0000000000004010     0 NOTYPE  GLOBAL DEFAULT   26 __bss_start
        17: 000000000000119d    25 FUNC    GLOBAL DEFAULT   16 main
    

    boo is added to the dynamic symbol table of prog, as well as several other previously absent external defined symbols which are unreferenced in the program. All these extra dynamic symbols are idle in this particular program linkage.

    --export-dynamic is enabled unconditionally for the linkage of a shared library and disabled by default for the linkage of a program.