2

I know we can use cmake or make to control CFLAGS or CXXFLAGS for building release version manually. release version to me means -O2 or -O3 is given at least, it doesn't matter whether -g or strip is performed, whereas debug is given -O0.

However sometimes I want use scripts to decide whether an elf binary was built with optimizations, so I can decide what to do next. I tried objdump or file or readelf, but found no answers. Are there any alternatives that I can do this ?

Lewis Chan
  • 695
  • 6
  • 18
  • https://ieeexplore.ieee.org/stamp/stamp.jsp?arnumber=9638449 It's hard according to this paper. No off-the-shelf method at present. – Lewis Chan Feb 26 '23 at 13:05

1 Answers1

3

However sometimes I want use scripts to decide whether an elf binary was built with optimizations, so I can decide what to do next.

The problem with your question is that your binary is very likely to contain some code built with optimizations (e.g. crt0.o -- part of GLIBC).

Your final binary is composed of a bunch of object files, and these files do not have to have consistent optimization flags.

Most likely you only care about the code you wrote (as opposed to code linked from other libraries). You could use -frecord-gcc-switches for that. See this answer.

Employed Russian
  • 199,314
  • 34
  • 295
  • 362