Get the compiler options from a compiled executable?

C++CLinux

C++ Problem Overview


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.

C++ Solutions


Solution 1 - C++

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.

Solution 2 - C++

If you compile with the -frecord-gcc-switches flag, then the command line compiler options will be written in the binary in the note section. See also the docs.

Solution 3 - C++

Another option is -grecord-gcc-swtiches (note, not -f but -g). According to gcc docs it'll put flags into dwarf debug info. And looks like it's enabled by default since gcc 4.8.

I've found dwarfdump program to be useful to extract those cflags. Note, strings program does not see them. Looks like dwarf info is compressed.

Solution 4 - C++

As long as the executable was compiled by gcc with -g option, the following should do the trick:

readelf --debug-dump=info /path/to/executable | grep "DW_AT_producer"

For example:

% cat test.c
int main() {
    return 42;
}
% gcc -g test.c -o test
% readelf --debug-dump=info ./test | grep "DW_AT_producer"
    <c>   DW_AT_producer    : (indirect string, offset: 0x2a): GNU C17 10.2.0 -mtune=generic -march=x86-64 -g

Sadly, clang doesn't seem to record options in similar way, at least in version 10.

Of course, strings would turn this up too, but one has to have at least some idea of what to look for as inspecting all the strings in real-world binary with naked eyes is usually impractical. E.g. with the binary from above example:

% strings ./test | grep march
GNU C17 10.2.0 -mtune=generic -march=x86-64 -g -O3

Solution 5 - C++

This is something that would require compiler support. You don't mention what compiler you are using but since you tagged your question linux I will assume you are using gcc -- which does not default the feature you're asking about (but -frecord-gcc-switches is an option to perform this).

If you want to inspect your binary, the strings command will show you everything that appears to be a readable character string within the file.

Solution 6 - C++

If you still have the compiler (same version) you used, and it is only one flag you're unsure about, you can try compiling your code again, once with and once without the flag. Then you can compare the executables. Your old one should be identical, or very similar, to one of the new ones.

Solution 7 - C++

I highly doubt it is possible:

int main()
{
}

When compiled with:

gcc -O3 -ffast-math -g main.c -o main

None of the parameters can be found in the generated object:

strings main | grep -O3
(no output)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMosbyView Question on Stackoverflow
Solution 1 - C++Michał GórnyView Answer on Stackoverflow
Solution 2 - C++perrealView Answer on Stackoverflow
Solution 3 - C++Aliaksei KandratsenkaView Answer on Stackoverflow
Solution 4 - C++Ilya KurnosovView Answer on Stackoverflow
Solution 5 - C++mahView Answer on Stackoverflow
Solution 6 - C++Zyx 2000View Answer on Stackoverflow
Solution 7 - C++user1202136View Answer on Stackoverflow