问题描述
我已阅读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 意味着生成的机器代码不依赖于位于特定地址才能工作.
Position Independent Code means that the generated machine code is not dependent on being located at a specific address in order to work.
例如跳转将作为相对而不是绝对生成.
E.g. jumps would be generated as relative rather than absolute.
伪组装:
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
Non-PIC:仅当代码位于地址 100 时才有效
Non-PIC: This will only work if the code is at address 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 选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!