8

I have a compiled elf file and I would like to verify the optimisation level used. The question is how can I determine optimisation level from an object/elf?

I have tried with "file" command and "objdump" (most of the switches), but none of them revealed this. Is it possible at all?

tothphu
  • 899
  • 12
  • 21
  • 2
    Recent GCC accepts `#pragma GCC optimize` so the the optimisation level may change from on function to another. So your question even has no precise meaning. – Basile Starynkevitch Jan 04 '12 at 06:27
  • I see. What is the reason behind such optimisation? – tothphu Jan 05 '12 at 20:48
  • I don't understand your question. The `#pragma GCC optimize` can change the required optimization level. You could use it e.g. to request that some particular function is always compiled in `-O2`, even if the rest of the source file is compiled in `-O0 -g` – Basile Starynkevitch Jan 05 '12 at 20:59
  • And why do you ask? A compiler is expected to make optimizations "transparently" in the sense that they don't affect the program's meaning. – Basile Starynkevitch Jan 05 '12 at 21:01
  • I was just asking what's the real world usage of optimising specific functions, but not others. But I see from the debugging example you have provided, so during debugging you have some critical functions optimised. – tothphu Jan 06 '12 at 00:37
  • Basile Starynkevitch: his question is perfectly fine. I came with PRECISELY the same question to this thread via google right now. While your comment provides additional information, I am afraid the question has not been answered, so it is quite pointless to distract from the question itself. SO has been useful precisely because it encourages people to ANSWER a question, no matter how much YOU individually think it is an "invalid" question, which is completely subjective anyway. – shevy Jun 15 '20 at 09:31

2 Answers2

13

strings foo.o | grep -- -O might give you what you want.

nstCactus
  • 5,141
  • 2
  • 30
  • 41
Trent
  • 131
  • 1
  • 2
  • perfect solution! – kyb Mar 19 '19 at 16:10
  • 1
    Is this reliable? Or may there be cases where it does not work? At the least it is better than the two "answers" above. Would be nice to know how well the above situation works; and if not, why not. Edit: Just tried it. Unfortunately I do not get any results for -O in many of the binaries. For example after having used -O3 for "file", I see no result with the above. :( – shevy Jun 15 '20 at 09:33
  • 1
    A more reliable solution is `readelf --debug-dump=info foo.o | grep "DW_AT_producer"`. It should work if the object file was built with `-g` option. See this related answer https://stackoverflow.com/a/65507874/72178. – ks1322 Apr 21 '22 at 17:52
2

No, not in general. The compiler doesn't write its active "optimisation" setting to the object file, and it's not necessarily possible to determine from the object code itself.

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
  • Thanks. I was hoping that it might do that. I have to work around the problem then. – tothphu Jan 04 '12 at 03:30
  • 1
    Sometimes you can view the command line options for the compiler and linker in the .comments section. When using ARM RVCT tools, I think "fromelf -v" dumps the comments section which shows the command line, but I'm not sure if objdump/elfdump or other tools do so – NullPointer Jan 04 '12 at 11:04
  • This is quite unfortunate. I wanted to write a script that can easily tell me the optimization levels. To explain the why: I am contemplating right now which programs to compile via -O2 and which ones via -O3. But it is easy to lose track once you compiled a few hundred programs again ... – shevy Jun 15 '20 at 09:32