本文介绍了GCC -fPIC选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我阅读了有关 GCC的代码生成约定的选项,但无法理解生成位置无关代码(PIC)是什么。

I have read about GCC's Options for Code Generation Conventions, but could not understand what "Generate position-independent code (PIC)" does. Please give an example to explain me what does it mean.

推荐答案

位置独立代码意味着生成的机器代码不依赖于位于特定地址以便工作。

Position Independent Code means that the generated machine code is not dependent on being located at a specific address in order to work.

例如

伪汇编:

PIC:这将工作是否代码位于地址100或1000.

PIC: This would work whether the code was at address 100 or 1000

100: COMPARE REG1, REG2
101: JUMP_IF_EQUAL CURRENT+10
...
111: NOP

代码位于地址100

100: COMPARE REG1, REG2
101: JUMP_IF_EQUAL 111
...
111: NOP

编辑:回应评论。

如果您的代码是使用-fPIC编译的,它适合包含在库中 - 库必须能够从其内存中的首选位置重新定位到另一个地址,可能有另一个已经加载图书馆在您的图书馆喜欢的地址。

If your code is compiled with -fPIC, it's suitable for inclusion in a library - the library must be able to be relocated from its preferred location in memory to another address, there could be another already loaded library at the address your library prefers.

这篇关于GCC -fPIC选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 19:45