CS:APP--Chapter07 : Linking(part 1)
标签(空格分隔): CS:APP
目录prologue
So far what I have learnt is to comprehend the relationship between my code/program and hardward for example how to translate my code into assembly code. From this point forward, We can shift to another perspective to inspect our program, that's the relationship to operation system. Undoubtedly, OS is a significant interface between user and computer resource. But the structure of computer shown in chapter 4 simply enable us to have a closer look at how way complicate it is, OS can simplify the operation , which forces programmer who want make effective use of computer to have a better understand the mechansim under the hood when our program is being executed from source code to executable file.
one blank left for overview of this chapter
1. compiler drivers
As we said before, our source code such as hello.c is executed only when it is converted from source file to executable code which is loaded into memory and then executed by processor.
one diagram :
We perform compile driver in order to invoke pre-processor, compiler, assembler, linker to generate executable file which eventually is loaded into memory by loader and executed by CPU.
Now we move on the command line to process our code.
Note: Because my own computer only has windows OS, many demonstrations on the book cannot be implemented now but poosible later and otherwise I just look up for another solution to accomplish these concepts.
GCC provides some options:
- v Display the programs invoked by the compiler.
- E Preprocess only; do not compile, assemble or link.
- S Compile only; do not assemble or link.
- c Compile and assemble, but do not link.
- o < file > Place the output into < file >.
computer is always performing works until there is transferring control over and interruped.
2. static linking
From a big point of view, dynamic linking seems more advanced and prevailing than static linking in terms of processing and simplicity. static linking is a kind of method we can come up with in a short period.
It just merges a collection of relocalable object files and command-line arguments and then generates a full linked executable file.
2.1 a glance at relocatable object file
relocatable object file comprises of instructions and data segments, which are a contigous sequence of bytes. According to the features of C, these bytes are functions and global and locak variables. What's more, these functios are stored in one block in memory, initialied global variables are in another section and uninitialized variables are in yet another section.
In this case, there are two steps need to be done to get executable file by linker which performs linking:
- symbol resolution
several relocatable object files defines and references variables within their text such as function, global variables and static variables[1].
The relocatable onject file has organized these variable into different sections and thus linker should re-organize these sections.
- relocation
Because these relocatable files start from address 0, the location of most instructions and data must be modified after linking. In this case, linker should relocate these sections by associating a new memory location with each definition and then modifying all references to these symbols.?????
2.2 object files
there are three types of object files:
name | description |
---|---|
relocatable object file | before linking |
executable object file | after linking |
shared object file | often used in dynamic linking at load or run time |
the standard of each file varies from system to system, just taking Linux for example, we focus on Executable and Linkable Format.
2.3 relocatable object file
ELF header has 16 bytes where store the word size and ordering of the system, followed by several sections
one variable is declared with static, which is invisable from external modules and object files. ↩︎