Why is fPIC absolutely necessary on 64 and not on 32bit platforms?

£可爱£侵袭症+ 提交于 2019-11-29 21:07:41

As I understand it the problem is x86-64 seems to introduce a new, faster way of referencing data relative to the instruction pointer, which did not exist for x86-32.

This article has a nice in-depth analysis of it, and gives the following executive summary:

The ability of x86-64 to use instruction-pointer relative offsetting to data addresses is a nice optimisation, but in a shared-library situation assumptions about the relative location of data are invalid and can not be used. In this case, access to global data (i.e. anything that might be changed around on you) must go through a layer of abstraction, namely the global offset table.

I.e. -fPIC addressing adds an extra layer of abstraction to addressing, to make what was previously possible (and a desirable feature) in the usual addressing style still work with the newer architecture.

But I don't find this quite adequate. If it is the case that relocations spoil the concept of shared libraries, why can it be done on 32bit platforms?

It can be done, it just isn't particularly efficient... computing the relocations has runtime costs, the relocated executables take additional memory, and the mechanism introduces a lot of complexity into the executable loader. Also, Linux distros really want to encourage all code to be compiled with -fPIC because changing the base address of an executable is a mitigation strategy which makes writing exploits for security vulnerabilities more difficult.

It's also worth mentioning that -fPIC isn't generally a significant performance cost, especially if you use -fvisibility=hidden or equivalent.

why were not all fields increased in size to accommodate?

The "field" in question is the immediate field of x86-64 addressing modes, which is isn't under the control of the ELF developers.

You can use -mcmodel=large option to build shared libraries without -fpic on x86_64

Reference : http://eli.thegreenplace.net/2012/01/03/understanding-the-x64-code-models/

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!