Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

gcc

gcc5

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?

like image 445
DevShark Avatar asked Mar 02 '16 11:03

DevShark


People also ask

What is the newest version of GCC?

The current version is 11.

Does GCC support C++ 11?

GCC provides experimental support for the 2011 ISO C++ standard. This support can be enabled with the -std=c++11 or -std=gnu++11 compiler options; the former disables GNU extensions. As of GCC 4.8. 1, GCC's C++11 mode implements all of the major features of the C++11 standard produced by the ISO C++ committee.

Does GCC 5.4 support C++ 17?

GCC has had complete support for C++17 language features since version 8. Clang 5 and later supports all C++17 language features.

Does GCC 4.9 support C ++ 14?

GCC 4.9. 0 is now available, with further improved C++11 and C++14 conformance.


1 Answers

-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 by function with the same name in another 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's only relevant for shared libraries (not executables)
  • 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 GCC-compatible ELF interposition semantics.

like image 74
yugr Avatar answered Sep 21 '22 04:09

yugr