I recently read the CSAPP and had some doubts about the compilation system part of it.
Now we have a sample using HelloWorld.c(just print hello world). The book said in Pre-processor phase, they replace the "#include " line with the content of this header file. But when I open the stdio.h, I find that there is only a declaration for printf() and there is no concrete implementation. So in the compilation system, when will the specific implementation of printf() be introduced?
And the book also said, in linking phase, the linker(ld) linked helloworld.o and printf.o . Why the linker knows to link my object file to printf.o? In a compilation system, why does it declare this function in the first step(Pre-processor phase) and link the concrete implementation in the last step(linking phase)?
Practically, over-simplified:
.a
or .so
file on unix).libc.so
has printf
function that starts at character number 0xaabbccdd
in the library file libc.so
.printf
takes. Does it take int
? Does it take char *
? Does it take uint_least64_t
? It's in the header file - int printf(const char *, ...);
. The header tells the compiler how to call the function (what parameters does the function take and what type it returns). Note that each .c
file is compiled separately. printf
) and compiled function body. The header has int printf(const char *, ...);
without function body.push pointer to "%d\n" on the stack; push some int on the stack; call printf; pop from the stack the returned "int"; rest of the instructions;
.call printf
. It then says: "Och, there is no printf
body in your code". So then it searches printf
in the libraries, to see where it is. The linker goes through all the libraries you link your program with and it finds printf
in the standard library - it's in libc.so
at address 0xaabbccdd
. So linker substitutes call printf
for goto libs.so file to address 0xaabbccdd
kind-of instruction.call printf
will jump into the file libc.so
at specified location.What I have written above is only for illustration purposes.