Re: linker
George <George@discussions.microsoft.com> wrote:
The linker does not change the .obj files. They remain untouched when the
linker is through. However, as the linker combines .obj files and .lib
files to produce an .exe, some of the bytes that came from the object files
and libraries HAVE to change, because of relocations and fixups.
Your senses are great! I am wondering in your senses, what are the
differences between relocation and fixup?
In my mind, fixup means either during link or load time, resolve external
referred address (e.g. function called but implemented in another compile
unit or dllexported functions). But what do you mean relocation?
The compiler does not know at what address each function and data item will
be located. Take the following C source file, for example:
int globalVar = 17;
int func() {
return globalVar;
}
int another() {
return func();
}
The compiler has no idea what address globalVar, func, and another will
actually have in the final executable. There might be many other object
files that got loaded first. So, for each object file, the compiler starts
out counting from 0 in its code section and its data section. Look at the
generated code from that file:
....
PUBLIC _globalVar
_DATA SEGMENT
_globalVar DD 011H
_DATA ENDS
PUBLIC _func
....
_TEXT SEGMENT
_func PROC
00000 55 push ebp
00001 8b ec mov ebp, esp
00003 a1 00 00 00 00 mov eax, DWORD PTR _globalVar
00008 5d pop ebp
00009 c3 ret 0
_func ENDP
_TEXT ENDS
PUBLIC _another
_TEXT SEGMENT
_another PROC
00010 55 push ebp
00011 8b ec mov ebp, esp
00013 e8 00 00 00 00 call _func
00018 5d pop ebp
00019 c3 ret 0
_another ENDP
_TEXT ENDS
END
Notice the code in "func". The instruction at offset 00003 refers to
_globalVar using the address 0. The compiler also puts a table in the
object file telling the linker that, when it figures out where this chunk
of the _DATA segment actually begins, it should add that that offset to the
four bytes at offset 00004. That is "relocation".
The same thing happend in "another". The call instruction points to
address 0, which is the offset of "_func" in the _TEXT segment for this
object file. Again, the compiler tells the linker to add the actual
starting address of this _TEXT chunk to the four bytes at offset 00014.
--
Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.