gcc 和 ld 中与位置无关的可执行文件的 -fPIE 选项是什么? [英] What is the -fPIE option for position-independent executables in gcc and ld?

查看:29
本文介绍了gcc 和 ld 中与位置无关的可执行文件的 -fPIE 选项是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

它将如何更改代码,例如函数调用?

How will it change the code, e.g. function calls?

推荐答案

PIE 是支持地址空间布局随机化(ASLR) 在可执行文件中.

PIE is to support address space layout randomization (ASLR) in executable files.

在创建 PIE 模式之前,程序的可执行文件无法放置在内存中的随机地址,只能将位置无关代码 (PIC) 动态库重新定位到随机偏移量.它的工作方式与 PIC 为动态库所做的工作非常相似,不同之处在于没有创建过程链接表 (PLT),而是使用了与 PC 相关的重定位.

Before the PIE mode was created, the program's executable could not be placed at a random address in memory, only position independent code (PIC) dynamic libraries could be relocated to a random offset. It works very much like what PIC does for dynamic libraries, the difference is that a Procedure Linkage Table (PLT) is not created, instead PC-relative relocation is used.

在 gcc/linkers 中启用 PIE 支持后,程序主体被编译和链接为与位置无关的代码.动态链接器对程序模块进行完全重定位处理,就像动态库一样.任何对全局数据的使用都将转换为通过全局偏移表 (GOT) 进行访问,并添加 GOT 重定位.

After enabling PIE support in gcc/linkers, the body of program is compiled and linked as position-independent code. A dynamic linker does full relocation processing on the program module, just like dynamic libraries. Any usage of global data is converted to access via the Global Offsets Table (GOT) and GOT relocations are added.

PIE 在这个 OpenBSD PIE 演示文稿中有很好的描述.

PIE is well described in this OpenBSD PIE presentation.

功能的变化显示在在这张幻灯片中(PIE vs PIC).

Changes to functions are shown in this slide (PIE vs PIC).

x86 图片与饼图

局部全局变量和函数在pie中进行了优化

Local global variables and functions are optimized in pie

外部全局变量和函数同pic

External global variables and functions are same as pic

以及这张幻灯片(PIE 与旧式链接)

and in this slide (PIE vs old-style linking)

x86 pie vs no-flags(固定)

x86 pie vs no-flags (fixed)

局部全局变量和函数类似于fixed

Local global variables and functions are similar to fixed

外部全局变量和函数同pic

External global variables and functions are same as pic

请注意,PIE 可能与 -static

Note, that PIE may be incompatible with -static

这篇关于gcc 和 ld 中与位置无关的可执行文件的 -fPIE 选项是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆