GCC -fPIC 选项

GCC -fPIC option(GCC -fPIC 选项)
本文介绍了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 意味着生成的机器代码不依赖于位于特定地址才能工作.

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 选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Prevent class inheritance in C++(防止 C++ 中的类继承)
Why should I declare a virtual destructor for an abstract class in C++?(为什么要在 C++ 中为抽象类声明虚拟析构函数?)
Why is Default constructor called in virtual inheritance?(为什么在虚拟继承中调用默认构造函数?)
C++ cast to derived class(C++ 转换为派生类)
C++ virtual function return type(C++虚函数返回类型)
Is there any real risk to deriving from the C++ STL containers?(从 C++ STL 容器派生是否有任何真正的风险?)