-fPIE(“位置无关的可执行文件”选项(gcc,ld) [英] -fPIE ("position-independent executable) option (gcc, ld)

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

问题描述

在gcc和ld中需要什么 -fPIE (-pie,位置无关的可执行文件)选项?

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

解决方案

PIE支持

在创建PIE模式之前,程序的可执行文件不能被放置在内存中的随机地址中,只有位置独立代码(PIC)动态库可以重新定位到随机偏移量。它与PIC为动态库所做的工作非常相似,区别在于不会创建过程链接表(PLT),而是使用PC相对重定位。



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



PIE在 OpenBSD PIE演示文稿



显示功能变更在幻灯片中(PIE vs PIC)。


x86 pic vs pie



局部全局变量和函数在饼图中优化


$ b

外部全局变量和函数与pic


以及本幻灯片(PIE vs旧式链接)


x86 pie vs no-flags(fixed)



局部全局变量和函数类似于固定的

外部glob al变量和函数与pic相同

请注意,PIE可能与 -static


What is the -fPIE (-pie, "position-independent executable") option needed for in gcc and ld?

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

解决方案

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

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.

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 is well described in this OpenBSD PIE presentation.

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

x86 pic vs pie

Local global variables and functions are optimized in pie

External global variables and functions are same as pic

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

x86 pie vs no-flags (fixed)

Local global variables and functions are similar to fixed

External global variables and functions are same as pic

Note, that PIE may be incompatible with -static

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

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