开发者

Is calling convention dealt by compiler or linker?

When we put _stdcall(the caller doesn't need to clear 开发者_StackOverflow社区the stack) in the function prototype,is it taken care of by the compiler or linker?


Calling convention is how to call the function (specific machine code for parameters preparation and cleaning up the ctack) and linker deals with which function (function at which address) to call.

So usually the compiler generates the right code and leaves "placeholders" for the exact function addresses and the linker then puts actual addresses there. That said, there's so-called link-time code generation where both is done by the linker.


Calling convention influences code generation and so it is the compiler that deals with it. The linker doesn't need to know about that.


On some systems, an object file is simply a bunch of data, symbols, and patch-points. The object file will tell the linker that that byte 574 of the linked code should be patched to contain the MSB of the difference between symbols Foo and Bar, and the linker will do that, but the linker has no concept of meaning beyond the list of computations to be performed.

On some other systems, the linker may be much more involved with code generation. For example, some ARM processors can run code which is encoded using a 16-bit or 32-bit instruction set. Some types of code can only run effectively using the 32-bit instruction set; some will run adequately, but be more compact, using the 16-bit set. The ARM linker knows which routines are written using each instruction set, and if code written using one instruction set tries to call a function written using the other, the linker will generate a wrapper method called a "veneer" and have the first function call that. The veneer will then perform the operations necessary to call the other method. Even though the veneer is "code", it's generated entirely by the linker.

There's really no fixed division of labor between compilers and linkers. I've seen some systems where the "compiler" translates a program into an "intermediate" form, and the linker does all the real code generation and makes optimizations based upon where things get located in memory. As noted, there are others where the linker is nothing but a "patch-up processor". Probably every imaginable division of labor between those extremes probably exists in some system somewhere.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜