为什么需要fPIE

为什么需要fPIEfPIE 是什么 以下内容来自 gcc 编译选项 fpic fPIC fpie fPIE 的说明 一点点 CSDN 博客 shared Create a shared library 创建共享库时需要添加的编译选项 因为共享库的代码都是位置无关的 所以通常还需要添加编译选项 fPIC fPIC Generate

大家好,我是讯享网,很高兴认识大家。

fPIE是什么

以下内容来自gcc编译选项fpic/fPIC, fpie/fPIE的说明_一点点-CSDN博客

-fPIC; Generate position-independent code if possible (large mode)
-fpic; Generate position-independent code if possible (small mode)
编译共享库时使用;

-fPIE; Generate position-independent code for executables if possible (large mode)
-fpie; Generate position-independent code for executables if possible (small mode)
编译可执行程序时使用
还需要在ld时增加-pie选项才能产生这种代码。即gcc -fpie -pie来编译程序。单独使用哪一个都无法达到效果。

-pie; Create a position independent executable

“mcmodel=” Use given x86-64 code model [仅适用于x86_64环境]
-mcmodel=small
Generate code for the small code model: the program and its symbols must be linked in the lower 2 GB of the address space. Pointers are 64 bits. Programs can be statically or dynamically linked. This is the default code model.
[默认值]程序和它的符号必须位于2GB以下的地址空间。指针仍然是64位。程序可以静态连接也可以动态连接。

-mcmodel=kernel
Generate code for the kernel code model. The kernel runs in the negative 2 GB of the address space. This model has to be used for Linux kernel code.
内核运行于2GB地址空间之外。在编译linux内核时必须使用该选项!


讯享网

-mcmodel=medium
Generate code for the medium model: The program is linked in the lower 2 GB of the address space. Small symbols are also placed there. Symbols with sizes larger than ‘-mlarge-data-threshold’ are put into large data or bss sections and can be located above 2GB. Programs can be statically or dynamically linked.
程序必须位于2GB以下的地址空间,但是它的符号可以位于任何地址空间。程序可以静态连接也可以动态连接。
注意:共享库不能使用这个选项编译!

-mcmodel=large
Generate code for the large model: This model makes no assumptions about addresses and sizes of sections.
对地址空间没有任何限制。 

为什么需要fPIE

以下内容来自GCC -fpie选项生成文件分析_softee的专栏-CSDN博客

Linux系统下,ELF格式的可执行文件的各个段都会被分配到不同的虚拟内存空间中。在操作系统实现地址随机化机制(Address Space Layout Randomization)之前,程序在任意一次执行下,所使用的虚拟空间的地址往往是相同的。这就给恶意攻击者的攻击行为提供了很大的便利(见Stack Smashing for Fun and Profit) 

不使用fPIE的时候:

使用fPIE的时候:

elf文件中的代码段被映射到了不同的虚拟内存地址上,这是因为编译器在该选项下可以生成位置无关的代码,从而在执行时,可以被装载到不同的地址空间。当代码、数据、stack和heap在每次执行时,都被随机化分配到不同的空间的时候,攻击者的难度无疑大大增加了。

小讯
上一篇 2025-02-06 13:34
下一篇 2025-03-24 17:49

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/93402.html