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

2019-01-25 12:10发布

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?

标签: c++ gcc gcc5
1条回答
Juvenile、少年°
2楼-- · 2019-01-25 13:10

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

By default compiler respects ELF function interposition semantics. In short, any exported function (i.e. any function if compiled with default compiler flags) can be replaced at runtime via LD_PRELOAD or simply by 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).

查看更多
登录 后发表回答