从可执行文件中找出编译优化标志
这里我有一个可执行文件,但不知道它的构建环境,假设使用 gcc/g++。 有没有办法找出编译过程中使用的优化标志(例如O0,O2,...)?
所有方法都受到欢迎,无论是通过分析二进制文件还是通过 gdb 进行一些调试测试(如果我们假设 -g 标志在编译期间可用)。
Here I have an executable without knowing its build environment, with the assumption of gcc/g++ being used.
Is there a way to find out the optimization flag used during compilation (like O0, O2, ...)?
All means are welcomed, no matter it's by analyzing the binary or some debug test via gdb (if we assume that -g flag is available during compilation).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果幸运的话,命令行会出现在可执行文件本身中,具体取决于所使用的操作系统和文件格式。如果它是 Elf 文件,请尝试使用 中的
objdump
转储内容GNU binutilsIf you are lucky, the command-line is present in the executable file itself, depending on the operating system and file format used. If it is an Elf-file, try to dump the content using the
objdump
from GNU binutils我真的不知道这是否有帮助,但是在反汇编(objdump -d)中检查 O0 非常容易,因为生成的代码根本没有优化,并且添加了一些指令来简化调试。
通常在 x86 上,函数的序言包括保存堆栈指针(我认为是为了回溯)。因此,例如,如果您找到 main 函数,您应该看到类似以下内容:
... main:
...推%rbp
... mov %rsp,%rbp
您应该在几乎每个函数的开头看到这个“模式”。
对于其他目标(我不知道你的目标平台是什么),你应该在序言中或函数调用之前看到或多或少相似的汇编序列。
对于其他优化级别,事情要棘手得多。
抱歉,我仍然含糊其辞,没有回答整个问题......只是希望它会有所帮助。
I really don't know if this can help, but checking O0 is quite easy in the disassembly (objdump -d), since the generated code has no optimization at all and adds some few instructions to simplify debugging.
Typically on an x86, the prologue of a function includes saving the stack pointer (for the backtrace, I presume). So if you locate, for example, the main function, you should see something like:
... main:
... push %rbp
... mov %rsp,%rbp
And you should see this "pattern" at almost every beginning of the functions.
For other targets (I dunno what your target platform is), you should see more or less similar assembly sequences in the prologues or before the function calls.
For other optimization levels, things are way way trickier.
Sorry for remaining vague and not answering the entire question... Just hoping it will help.