Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to know the optimzation options used to build a shared library in C++

I have a very simple question but I haven't been able to find the answer yet so here I go: I am using a shared library and I'd like to know if it had been compiled with an optimization flag such as -O3 or not.

Is there a way to find that information ?

Thank you very much

like image 302
wizmer Avatar asked Jan 28 '15 09:01

wizmer


People also ask

How many optimization levels are there in gcc?

6.4 Optimization levels. In order to control compilation-time and compiler memory usage, and the trade-offs between speed and space for the resulting executable, GCC provides a range of general optimization levels, numbered from 0--3, as well as individual options for specific types of optimization.

What is default gcc optimization level?

GCC has a range of optimization levels, plus individual options to enable or disable particular optimizations. The overall compiler optimization level is controlled by the command line option -On, where n is the required optimization level, as follows: -O0 . (default).

What is compiler optimization in C?

Compiler optimization is generally implemented using a sequence of optimizing transformations, algorithms which take a program and transform it to produce a semantically equivalent output program that uses fewer resources or executes faster.

What optimization does gcc do?

The compiler optimizes to reduce the size of the binary instead of execution speed. If you do not specify an optimization option, gcc attempts to reduce the compilation time and to make debugging always yield the result expected from reading the source code.


2 Answers

If you are using gcc 4.3 or later, check out -frecord-gcc-switches. After you build the binary file use readelf -n to read the notes section. More info can be found here Detect GCC compile-time flags of a binary

like image 183
e271p314 Avatar answered Sep 22 '22 14:09

e271p314


Unless whoever compiled the library in the first place used a compiler that saves these flags to the binary somehow (I think only recent GCC allows that, and probably clang), there's inherently no way to know exactly what flags have been used. You can, of course, if you have had a lot of experience looking at assembly, deduct a lot (for example "this looks like an automatically unrolled loop", "This looks like someone optimized for a processor where A xor A is faster than A := 0x0", etc).

Generally, there's always different source code that can end up as the same compiled code, so there's no way to tell wether what has been compiled was optimized "by hand" in the first place or has seen compiler optimization in many cases.

Also, there are a lot of C++ compilers out there, a lot of versions of these and even more flags...

Now, your question comes out of somewhere; I'm guessing you're asking this because either

  1. you want to know if there's debugging symbols in there, or
  2. you want to make sure something isn't crashing because of incorrect optimization, or
  3. you want to know whether there's potential for optimization.

Now, 1. is really rather independent of the level of optimization; of course, the more you optimize, the less your bytecode corresponds to "lines of source code", but you can still have debugging symbols.

The second point: I've learned the hard way that unless I've successfully excluded every other alternative, I'm the one to blame for bugs (and not my compiler).

The third point: There's always room for optimization, but that won't help you unless you're in a position to recompile the library yourself. If you recompile, you'll set the flags, so no need to find out if they were set in the first place. If you're not able to recompile: Knowing there is room won't help you. If you're just getting your library out of a complex build process: Most build systems leave you with a log that will include things like compiler flags.

like image 36
Marcus Müller Avatar answered Sep 24 '22 14:09

Marcus Müller