Agner`s CPU blog

Software optimization resources | E-mail subscription to this blog |

Author: Agner Date: 2010-10-30 01:35
You want to use
call foo
foo: pop edx
for getting a reference point for position-independent code.

This method is actually used sometimes, but this a very bad idea because it interferes with the return prediction method used by modern microprocessors. The next return instruction will be falsely predicted as going to foo rather than to the place where the function was called from. And subsequent returns will be mispredicted as well. Mispredictions are quite costly and should be avoided if possible. See the explanation of return stack buffer in my microarchitecture manual.

The most efficient solution is to not use position-independent code at all. Current linkers will usually accept position-dependent code, even in shared objects (.so) in Linux, BSD and Mac, as far as I know. (Correct me if I'm wrong). Position-independent code is never needed under Windows.

thread Relocations new - mirabilos - 2010-10-28
last reply Relocations - Agner - 2010-10-30