Deep Dive Into Static Linking
In a previous article, we gave an overview of what the linker does. In this article, we will dive deeper into how static linking works.
What does static linking mean?
Static linking takes in a set of relocatable object files and creates an executable object file which can be used to run your program. When a program is compiled, it compiles all the files separately and ignores undefined symbols (initialized but undefined functions or variables). The static linker is responsible for taking all the relocatable object files from the compiler output and links the undefined symbols to their definitions as well as relocating the sections in memory (we will talk about what this means below).
In the previous article, we saw that the ask_question
function signature was initialized, but not defined. The static linker to the relocatable object files of main.c
and helper.c
and resolves the symbols so that the program can be run. This is known as the symbol resolution step of the linker.
Relocating the sections in memory is the other job of the linker. The compiler assigns sections in memory for the sections in the object file assuming that it starts at the memory address 0. However, in a multi-file program, not all the sections loaded into memory can start at memory address 0. The linker has to relocate the…