How to check if a macro exists in an object file in C?

有些话、适合烂在心里 提交于 2019-12-01 09:50:04

问题


For example, I define a macro:

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

How can I check if VERSION exist in my object file or not? I tried to disassemble it with objdump, but found no actual value of my macro VERSION. VERSION is defined in Makefile.


回答1:


Try compiling with -g3 option in gcc. It stores macro information too in the generated ELF file.

After this, if you've defined a macro MACRO_NAME just grep for it in the output executable or your object file. For example,

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

Or you can even try,

$ 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.



回答2:


The following command displays contents of .debug_macro DWARF section:

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

or

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

You can also use dwarfdump path/to/binary, but it's not easy to leave only .debug_macro section in the output.



来源:https://stackoverflow.com/questions/10102851/how-to-check-if-a-macro-exists-in-an-object-file-in-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!