New option in GCC 5.3: -fno-semantic-interposition

懵懂的女人 提交于 2019-11-27 22:18:07

问题


GCC 5.3 has added a new option: -fno-semantic-interposition

A new -fno-semantic-interposition option can be used to improve code quality of shared libraries where interposition of exported symbols is not allowed.

This sounds like this is something useful for C++ projects where interposition can't be used for whatever reason, but where latency is a concern.

However, the description is fairly vague. Is anyone able to clarify how this option works exactly?


回答1:


-fno-semantic-interposition can significantly improve performance of code in shared libraries but may change semantics in some cases.

By default GCC respects the ELF symbol interposition semantics. In short, any exported library function (i.e. any library function if compiled with default compiler flags) can be replaced at runtime via LD_PRELOAD or simply function in shared library which happens to be loaded earlier by dynamic linker. This prevents compiler from doing a lot of useful analyses and optimizations (most notably inlining and cloning) because they may break interposition.

-fno-semantic-interposition gives compiler permission to ignore potential interposition and optimize much more aggressively.

As I said, there are some caveats in using -fno-semantic-interposition:

  • it might change behavior of your program (when it was actually relying on interposition, sometimes without you realizing this)
  • it can only benefit shared libraries
  • it's much less useful if you already do proper optimization of your libraries (i.e. compile with -fvisibility=hidden and explicitly annotate all exported symbols with __attribute__((visibility("default"))))

The first item prevents wide deployment of fno-semantic-interposition. E.g. to my knowledge no Linux distro uses it at wide scale (it would be a great project btw).

BTW note that Clang compiler has -fno-semantic-interposition enabled by default, presumably for the sake of performance. They have an inverse -fsemantic-interposition flag to enable ELF interposition semantics.



来源:https://stackoverflow.com/questions/35745543/new-option-in-gcc-5-3-fno-semantic-interposition

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!