不使用PIC建立在Linux .so文件(位置无关code)(x86 32位系统) [英] Create .SO files on Linux without using PIC (position independent code) (x86 32bit)

查看:203
本文介绍了不使用PIC建立在Linux .so文件(位置无关code)(x86 32位系统)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,x86汇编code是非常多的由寄存器的数量有限的制约。

当我得知在Linux上,创建一个.so文件,就必须指定-fPIC命令行参数,以创建与位置无关code,我简直不敢相信,首先它与gcc。

据我所知,ELF文件格式支持搬迁,就像 - 在我眼里好得多 - Windows动态链接库系统的工作原理:在Windows链接器重定位中的DLL所有的偏移,如果这是必要的

我认为需要的时间为缺乏一个整体的注册所有的时间来加载一个SO-文件或DLL文件,并且还使用的内存,以保持不同的搬迁。所以,文件的数量是左右不是那么糟糕,它指向GOT,并且具有所有这间接跳转。

我也不在乎在所有的ALSR等,为我心目中的应用程序是我只在乎有一个库code进行优化,尽可能多地。

1)为什么Linux的不支持更多的动态库加载,如Windows应能提供很多很多更好的性能code?

到目前为止,我已经找到了它没有真正的解释。喜欢它会如此非常糟糕,缓慢搬迁code(当然,对于台式机上装载一个文字处理器只是有些事情,它的问题有多快它负载,我完全接受,但对于计算密集型服务器进程(不处理从互联网上恶意数据),我想有所有性能和寄存器,我可以得到!

2)是否有可能对我来说,在Linux上创建不-fPIC编译SO-文件?可我只是离开-fPIC走呢?有哪些适用于这个话题,并有可能不浪费一整个注册,仍然加载动态库?任何HOWTO,手动或项目

如果我编译一个.so文件时,只下降了-fPIC会发生什么?


解决方案

  

如果我刚落,会发生什么的 -fPIC 编译时一个的.so -file?


生成的共享对象ELF文件会(很可能)动态加载的半随机(即未predictable)页地址(例如,因为 MMAP 系统调用会遇到 ASLR )。

和链接器会产生一个巨大的很多拆迁业务。相比,动态接头( ld.so )将要慢慢的过程的一次大lot搬迁的,所以你的文字部分将不得不重写(并不会下,高效读取 - 只使用相同的的.so 文件)其他进程共享。

因此​​,在实践中不忘 -fPIC 共享对象(即动态链接库)是最常见的一个坏主意,即使是可能的。

Drepper的方法文档做动态共享库纸和惠勒的程序库HOWTO

BTW,位置无关code 是更加昂贵在x86(32位)比x86-64的。但它是值得的(可能,PIC code是最多在x86 32位5〜10%,比非PIC慢)。

As far as I know, x86 assembly code is very much constrained by the limited amount of registers.

When I learnt that on Linux, to create a .so file, one has to specify the -fPIC command line argument to gcc in order to create position independent code, I couldn't believe it first.

As far as I know, the elf file format supports relocations, just like the - in my eyes much better - Windows DLL system works: On Windows the linker relocates all the offsets in the DLLs, if this is necessary.

I think that the time needed to load a SO-file or DLL-file, and also the amount of memory used to keep differently relocated .so-files around is not as bad as the lack of a whole register all the time which points to the GOT and having all this indirect jumps.

I also don't care at all for ALSR etc. for the applications I have in mind were I only care about having code in a library to be optimized as much as possible.

1) Why does Linux not support more dynamic library loading like Windows which should produce much much more performant code?

So far I have found no real explanation for it. Just some things like that it would be so very bad and slow to relocate code (Of course, for loading a word processor on a desktop machine, it matters how fast it loads, I fully accept that. But for a computationally intensive server process (not processing malicious data from the internet), I'd like to have all the performance and registers I can get!

2) Is it possible for me to create NOT -fPIC compiled SO-files on Linux? Can I just leave the -fPIC away? Is there any howto, manual or project which works on this topic and makes it possible to not waste a whole register and still load libraries dynamically?

What happens if I just drop the -fPIC when compiling a .so-file?

解决方案

What happens if I just drop the -fPIC when compiling a .so-file?

The resulting shared object ELF file would (very probably) be dynamically loaded at semi-random (i.e. unpredictable) page addresses (e.g. because the mmap syscall will encounter ASLR).

And the linker would produce a huge lot of relocation operations. So the dynamic linker (ld.so) would have to slowly process a big lot of relocations, so your text segment would have to be rewritten (and won't be efficiently read-only shared with other processes using the same .so file).

So in practice forgetting the -fPIC on a shared object (i.e. dynamically linked library) is most often a bad idea, even if it is possible.

Read Drepper's HowTo do Dynamic Shared Libraries paper and Wheeler's Program Library Howto

BTW, position independent code is much more costly on x86 (32 bits) than on x86-64. But it is worth the effort (probably, PIC code is at most 5 to 10% slower than non-PIC on x86 32 bits).

这篇关于不使用PIC建立在Linux .so文件(位置无关code)(x86 32位系统)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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