How To Write Shared Libraries(20)

2 Optimizations for DSOs(1)

In this section we describe various optimizations based on C or C++ variables or functions. The choice of variable or function, unless explicitly said, is made deliber- ately since many of the implementations apply to the one or the other. But there are some architectures where functions are handled like variables.
本节分析c/c++变量和函数的优化。除非明确说明,都有有意选取的实现内容。但有一些架构中,函数像变量一样处理。
This is mainly the case for embedded RISC architectures like SH-3 and SH-4 which have limitations in the addressing modes they provide which make it impossible to implement the function handling as for other architectures.
这主要适用于像SH-3和SH-4这样的嵌入式RISC体系结构,它们提供的寻址模式有局限性,这使得它们不可能像其他体系结构那样实现函数处理。(有道翻译)
In most cases it is no problem to apply the optimizations for variables and functions at the same time. This is what in fact should be done all the time to achieve best performance across all architectures.一般情况下使用优化处理变量和函数没有问题。这就是所有需要完成的提高性能的地方。

The most important recommendation is to always use -fpic or -fPIC when generating code which ends up in DSOs. This applies to data as well as code. Code which is not compiled this way almost certainly will contain text relocations. For these there is no excuse. Text relocations requires extra work to apply in the dynamic linker. And argumentation saying that the code is not shared because no other process uses the DSO is invalid. In this case it is not useful to use a DSO in the first place; the code should just be added to the application code.
最主要的推荐是生成DSO的文件添加编译参数-fpic或者-fPIC。数据和代码都使用。没有使用该参数的代码总是饱含重定位代码。这是必须的内容。代码重定位需要链接器额外的工作。这样代码是不共享的,因为没有其他过程使用。这种情境下在DSO中重定位没用。代码只要加载到应用代码中就OK了。

Some people try to argue that the use of -fpic/-fPIC on some architectures has too many disadvantages. This is mainly brought forward in argumentations about IA- 32.
一些人会认为使用-fpic/-fPIC在一些架构上有许多缺点。这主要是IA-32.
Here the use of %ebx as the PIC register deprives the compiler of one of the precious registers it could use for optimization. But this is really not that much of a problem. First, not having %ebx available was never a big penalty. Second, in modern compilers (e.g., gcc after release 3.1) the handling of the PIC register is much more flexible.
这里使用%ebx作为PIC寄存器剥夺了编译器可以用于优化的一个宝贵寄存器。但这并不是什么大问题。首先,没有%ebx从来都不是什么大问题。其次,在现代编译器中(例如3.1版之后的gcc), PIC寄存器的处理要灵活得多。(有道翻译)
It is not always necessary to use %ebx which can help eliminating unnecessary copy operations. And third, by providing the compiler with more information as explained later in this section a lot of the overhead in PIC can be removed. This all combined will lead to overhead which is in most situations not noticeable.
并不总是需要使用%ebx,它可以帮助消除不必要的复制操作。第三,通过向编译器提供更多的信息,就像本节后面解释的那样,可以删除PIC中的大量开销。所有这些结合在一起将导致开销,在大多数情况下是不明显的。(有道翻译)

When gcc is used, the options -fpic/-fPIC also tell the compiler that a number of optimizations which are pos- sible for the executable cannot be performed. This has to do with symbol lookups and cutting it short. Since the compiler can assume the executable to be the first object in the lookup scope it knows that all references of global symbols known to be defined in the executable are re- solved locally. Access to locally defined variable could be done directly, without using indirect access through the GOT. This is not true for DSOs: the DSOs can be later in the lookup scope and earlier objects might be in- terposed. It is therefore mandatory to compile all code which can potentially end up in a DSO with -fpic/-fPIC since otherwise the DSO might not work correctly. There is no compiler option to separate this optimization from the generation of position-independent code.
当使用gcc时,选项-fpic/- fpic也告诉编译器许多可执行文件可能的优化不能执行。这与查找符号和缩短它有关。由于编译器可以假定可执行文件是查找范围中的第一个对象,因此它知道在可执行文件中定义的所有全局符号引用都是本地解析的。可以直接访问本地定义的变量,而不需要通过GOT进行间接访问。这对于DSOs来说不是这样的:DSOs可以在查找范围的后面,而较早的对象可能被插入。因此,必须编译所有可能以-fpic/ -fpic结尾的DSO代码,否则DSO可能无法正常工作。没有编译器选项可以将此优化与位置无关代码的生成分离。(有道翻译)

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,287评论 6 498
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,346评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,277评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,132评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,147评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,106评论 1 295
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,019评论 3 417
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,862评论 0 274
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,301评论 1 310
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,521评论 2 332
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,682评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,405评论 5 343
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,996评论 3 325
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,651评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,803评论 1 268
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,674评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,563评论 2 352

推荐阅读更多精彩内容