Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the compiler options from a compiled executable?

Tags:

c++

c

linux

It there a way to see what compiler and flags were used to create an executable file in *nix? I have an old version of my code compiled and I would like to see whether it was compiled with or without optimization. Google was not too helpful, but I'm not sure I am using the correct keywords.

like image 711
Mosby Avatar asked Aug 24 '12 15:08

Mosby


People also ask

What is compiler option?

Compilers options (− x on Linux, and /Qx on Microsoft Windows) control which instructions the compiler uses within a function, while the processor(…) clause controls creation of non-standard functions using wider registers (YMM or ZMM) for passing SIMD data for parameters and results.

What is CL compiler?

cl.exe is a tool that controls the Microsoft C++ (MSVC) C and C++ compilers and linker. cl.exe can be run only on operating systems that support Microsoft Visual Studio for Windows. You can start this tool only from a Visual Studio developer command prompt.

Which GCC option is used for compilation?

GCC stands for GNU Compiler Collections which is used to compile mainly C and C++ language. It can also be used to compile Objective C and Objective C++.


1 Answers

gcc has a -frecord-gcc-switches option for that:

   -frecord-gcc-switches        This switch causes the command line that was used to invoke the compiler to        be recorded into the object file that is being created.  This switch is only        implemented on some targets and the exact format of the recording is target        and binary file format dependent, but it usually takes the form of a section        containing ASCII text. 

Afterwards, the ELF executables will contain .GCC.command.line section with that information.

$ gcc -O2 -frecord-gcc-switches a.c $ readelf -p .GCC.command.line a.out   String dump of section '.GCC.command.line':   [     0]  a.c   [     4]  -mtune=generic   [    13]  -march=x86-64   [    21]  -O2   [    25]  -frecord-gcc-switches 

Of course, it won't work for executables compiled without that option.


For the simple case of optimizations, you could try using a debugger if the file was compiled with debug info. If you step through it a little, you may notice that some variables were 'optimized out'. That suggests that optimization took place.

like image 89
Michał Górny Avatar answered Oct 01 '22 14:10

Michał Górny