【问题标题】:How to check if a macro exists in an object file in C?如何检查C中的目标文件中是否存在宏?
【发布时间】:2012-04-11 08:59:16
【问题描述】:

比如我定义了一个宏:

#ifdef VERSION
 //.... do something
#endif

如何检查我的目标文件中是否存在VERSION?我试图用objdump 反汇编它,但没有发现我的宏VERSION 的实际值。 VERSION 在 Makefile 中定义。

【问题讨论】:

    标签: c object-files


    【解决方案1】:

    尝试在gcc 中使用-g3 选项进行编译。它也将宏信息存储在生成的 ELF 文件中。

    在此之后,如果您在输出可执行文件或目标文件中为它定义了一个宏 MACRO_NAME,只需 grep。例如,

    $ grep MACRO_NAME a.out # any object file will do instead of a.out
    Binary file a.out matches
    

    或者你甚至可以尝试,

    $ strings -a -n 1 a.out | grep MACRO_NAME
    
     -a Do not scan only the initialized and loaded sections of object files; 
        scan the whole files.
    
     -n min-len Print sequences of characters that are at least min-len characters long,
        instead of the default 4.
    

    【讨论】:

    • +1 for -g3 ... - 然后在二进制文件中找到宏,我更喜欢strings <binary> | grep <macro>
    • @alk。我尝试了字符串版本,但 MACRO_NAME 嵌入到目标文件中,strings 没有将其视为字符串并且 grep 失败。
    • 过失,我刚刚测试了这个,看来你是对的。需要对此进行调查... 挖掘
    • 好的,明白了...... - 使用:strings -a <binary> | grep <macro> 并且它有效。请参阅 man strings 了解正在发生的事情。
    • @ouah 不知何故,我昏昏欲睡的头脑无法发现错字。帮助:)
    【解决方案2】:

    以下命令显示.debug_macro DW​​ARF 部分的内容:

    $ readelf --debug-dump=macro path/to/binary
    

    $ objdump --dwarf=macro path/to/binary
    

    您也可以使用dwarfdump path/to/binary,但在输出中只保留.debug_macro 部分是not easy

    【讨论】:

      猜你喜欢
      • 2011-07-01
      • 1970-01-01
      • 2017-02-21
      • 1970-01-01
      • 2017-04-24
      • 2011-04-07
      • 1970-01-01
      • 2013-10-28
      • 2015-02-20
      相关资源
      最近更新 更多