Thread: Dynamic vs Static Linking regarding addresses

  1. #1
    Registered User
    Join Date
    Dec 2014
    Posts
    3

    Dynamic vs Static Linking regarding addresses

    Hello everybody

    I'm trying to understand the process from "compiling - linking - executing" on a low level view.

    Regarding static linking:

    Is every address for function calls, global variables etc. known and set after the linking? I mean while link time, so before run time?

    I know that there is a dynamic Linker/Loader, which is responsible for relocations. He works on run time, so when we actually want to start the executable and he will determine the missing addresses, because before runtime we could not know where the libraries are in the virtual address space.

    But I thought the dynamic Linker (ld.so) is only needed when we used dynamic linking. But is he also used for static linking? Or are every addresses set after static linking?

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    It might be worth noting that my response is all specific to Linux -- I suspect that's what you're asking about, but if you're on some other *nix system, some of this might not apply. Also, there's tons of info already out there on the web. At the bottom are some (hopefully) helpful links. Not all are related to your specific question, but they're worth reading if you want to get deeper into all of this.
    Quote Originally Posted by ANSI-C View Post
    Regarding static linking:

    Is every address for function calls, global variables etc. known and set after the linking? I mean while link time, so before run time?
    Yes, that's the gist of it. When the linker is done, the addresses of all the symbols are fixed, but only within the context of the executable file. ld.so does not come into play when running a purely statically linked binary. It's the kernel that reads the executable file into memory and turns the file-based address into addresses that correspond to memory1.

    Quote Originally Posted by ANSI-C View Post
    But I thought the dynamic Linker (ld.so) is only needed when we used dynamic linking. But is he also used for static linking? Or are every addresses set after static linking?
    ld.so is not used for static linking, only dynamic. It is itself something of a shared library that is bootstrapped and helps load other shared libraries.

    Some helpful/informative links:
    How statically linked programs run on Linux - Eli Bendersky's website
    http://www.linuxjournal.com/article/6463
    A Whirlwind Tutorial on Creating Really Teensy ELF Executables for Linux
    Anatomy of a Program in Memory - Gustavo Duarte

    1 Actually, the kernel maps it to virtual addresses, which it manages and translates to physical memory as appropriate. That's a topic worth it's own thread/question though.

  3. #3
    Make Fortran great again
    Join Date
    Sep 2009
    Posts
    1,413

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Great links so far! What I'm going to say is all Linux specific, but here we go. Object files (.o) created by the compiler, executable files created by the linker, and shared libraries (.so) are all ELF files. They all have sections that can be loaded into memory, with different permissions (usually read/write and read/execute), and may contain unresolved relocations and unresolved symbols ("please put the address of this here once you know it"). These are values which must be filled in somehow before the code is actually executable. For instance, a hello world program might have an unresolved external reference to the function "printf" or "puts" (try "nm program" to see). One object file might reference a global variable declared in another file, whose address is not known. Actually, object files have a lot of addresses which are not known because they all have a base address of zero. The linker's job is to put them all together into one program, such that there aren't any overlapping regions. Only at this point is the actual address of a function decided upon and thus any relocations that refer to the function may be resolved.

    There's a lot of symmetry between these systems. The linker takes object files and resolves many (but not all) relocations to produce executables and libraries. The loader chooses an actual base address for a library (which can otherwise be loaded anywhere) and resolves all remaining relocations. So a statically linked executable, with all of its code inside the same ELF and all of its relocations already known, does not need a loader at all. But a normal executable does in order to handle its shared libraries. [It is also possible to create a position-independent executable, which acts much like a library in that its selection of a base address can be deferred until runtime. Like I said, there's a lot of symmetry here.]

    The best part? The loader itself is an ELF file, statically linked of course, which simply knows how to open other ELF files and fix their relocations.

    tl;dr the linker resolves relocations at link-time, and unless you made a static executable, there will be some relocations left which are resolved by the loader (or "dynamic linker") at runtime.

    [edit] If you want to see more, try running "readelf -Wa program". [/edit]
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. GCC Linking against static library
    By bobthebullet990 in forum C Programming
    Replies: 2
    Last Post: 08-27-2015, 06:27 PM
  2. SFML - Static Linking
    By c_lover in forum C++ Programming
    Replies: 1
    Last Post: 08-07-2015, 02:59 PM
  3. Linking C++ Static Libs
    By phylene in forum C++ Programming
    Replies: 15
    Last Post: 03-19-2012, 08:08 PM
  4. static linking with Dev-c++/gcc
    By silk.odyssey in forum C Programming
    Replies: 6
    Last Post: 12-14-2003, 01:28 PM
  5. dynamic Ip addresses
    By lambs4 in forum Tech Board
    Replies: 5
    Last Post: 01-20-2003, 08:48 AM

Tags for this Thread