1

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.

Quonux
  • 2,975
  • 1
  • 24
  • 32
Amumu
  • 17,924
  • 31
  • 84
  • 131

2 Answers2

6

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.
Pavan Manjunath
  • 27,404
  • 12
  • 99
  • 125
  • +1 for `-g3` ... - to then find the macro in the binary I'd prefer `strings | grep `. – alk Apr 11 '12 at 09:14
  • @alk. I tried the strings version but the `MACRO_NAME` is so embedded into the object file that `strings` is not treating it as a string and grep fails. – Pavan Manjunath Apr 11 '12 at 09:17
  • Mea culpa, I just tested this and it seems you are right. Need to investigate on this one ... *out digging* – alk Apr 11 '12 at 09:21
  • 1
    Ok, got it ... - use: `strings -a | grep ` and it works. See `man strings` for what's going on. – alk Apr 11 '12 at 09:25
  • @ouah somehow my drowsy mind is not able to spot the typo. Help :) – Pavan Manjunath Apr 11 '12 at 10:04
  • Ahh, idiot me! I switched to windows when I did the edit. So din verify. Thanks @ouah :) – Pavan Manjunath Apr 11 '12 at 10:07
  • @ouah Aaargh... Take this gun! Shoot my hellish brain. Edited! – Pavan Manjunath Apr 11 '12 at 10:24
  • Just for the record, there's also `dwarfdump`, it dumps DWARF debug information. You're most likely interested in `.debug_macro` sections. You can filter the others out like so, `dwarfdump path/to/binary | sed -En ':l1 /^\.debug_macro/ { $ { p; q }; h; :l2 n; $ { /^\./ { /^\.debug_macro/ { H } }; /^\./ ! H; x; p; q }; /^\./ ! { H; b l2 }; x; p; x; b l1 }'`. Not that it's not usable without `sed`, I just felt like doing some `brainfuck`'ing :) More on the script [here](https://stackoverflow.com/a/52239152/52499). – x-yuri Sep 08 '18 at 20:31
0

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.

x-yuri
  • 16,722
  • 15
  • 114
  • 161