GCC -fPIC option(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 选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:GCC -fPIC 选项
基础教程推荐
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01
- 管理共享内存应该分配多少内存?(助推) 2022-12-07
- 从 std::cin 读取密码 2021-01-01
- 使用从字符串中提取的参数调用函数 2022-01-01
- 为什么语句不能出现在命名空间范围内? 2021-01-01
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01
- Windows Media Foundation 录制音频 2021-01-01
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01