Search code examples
global-variableself

Why aren't external variables added to .dynsym table?


From my understanding of .dynsym, it should contain the symbols that should be visible from an external program. I tried a simple program that contain this line:

extern int global = 1000;

I thought the executable would contain the global symbol in its .dynsym, but it is not the case. Is my understanding of this section wrong?


Solution

  • I thought the executable would contain the global symbol in its .dynsym

    Why did you think that?

    By default, only symbols referenced by some other shared library participating in the link are exported in the dynamic symbol table from the executable.

    You can force all symbols to be exported in the dynamic symbol table using --export-dynamic linker flag:

    $ cat foo.c
    int global = 1000;
    int main() { return 0; }
    
    $ gcc foo.c && nm -D a.out | grep global
    # no output
    
    $ gcc foo.c -Wl,--export-dynamic && nm -D a.out | grep global
    0000000000004028 D global