官术网_书友最值得收藏!

Introducing Linking

The compiler outputs an object file for each compilation unit. In the previous example, we had three .cpp files and the compiler produced three object files. The task of the linker is to combine these object files together into a single object file. Combining files together results in relative address changes; for example, if the linker puts the rect.o file after main.o, the starting address of rect.o becomes 0x04 instead of the previous value of 0x00:

code: 
0x00 main
0x01 Rect r;
0x02 _Rect_init_(&r, 3.1, 4.05);
0x03 printf("%d\n", _Rect_get_area(&r));
0x04 _Rect_init_
0x05 side1_ = s1
0x06 side2_ = s2
0x07 return
0x08 _Rect_get_area_
0x09 register = side1_
0x0A reg_multiply side2_
0x0B return
information (symbol table):
main: 0x00
_Rect_init_: 0x04
printf: ????
_Rect_get_area_: 0x08

_Rect_init_: 0x04
_Rect_get_area_: 0x08

The linker correspondingly updates the symbol table addresses (the information: section in our example). As mentioned previously, each object file has its symbol table, which maps the string name of the symbol to its relative location (address) in the file. The next step of linking is to resolve all the unresolved symbols in the object file.

Now that the linker has combined main.o and rect.o together, it knows the relative location of unresolved symbols because they are now located in the same file. The printf symbol will be resolved the same way, except this time it will link the object files with the standard library. After all the object files are combined together (we omitted the linking of square.o for brevity), all addresses are updated, and all the symbols are resolved, the linker outputs the one final object file that can be executed by the operating system. As discussed earlier in the chapter, the OS uses a tool called the loader to load the contents of the executable file into the memory.

主站蜘蛛池模板: 和田县| 新密市| 长泰县| 鄂托克前旗| 盘山县| 湘西| 资兴市| 米林县| 镶黄旗| 娄烦县| 望奎县| 正阳县| 景洪市| 略阳县| 磴口县| 汤阴县| 甘孜县| 通山县| 平顶山市| 慈利县| 蕲春县| 泾源县| 德昌县| 青阳县| 博野县| 丰宁| 双流县| 井冈山市| 灌云县| 西安市| 鲁山县| 博野县| 柳江县| 龙岩市| 镇巴县| 鹤峰县| 平湖市| 缙云县| 巴中市| 新兴县| 横山县|