6
g++ -c xyz.cpp

creates an object file xyz.o

But when I open it in vim editor it shows some crazy text...

How can I see the machine code?

What package can use to open and see that machine code?

3 Answers 3

4

Check out the binutils package.

In it you'll find objdump - display information from object files.

Then try running objdump -d xyz.o.

1
  • 1
    It works GREAT !!!
    – Sam
    Sep 17, 2012 at 21:17
2

Machine code generated by your compiler is not supposed to be human readable, but rather completely optimized to run as fast as possible on your processor.

However, you can read symbols from the binary files like this:

$ nm main.o | c++filt         
                 U _Unwind_Resume                                                                                                    
                 U FactoryImpl::FactoryImpl()                                                                                        
                 U UI::UI(Subject*)                                                                                                  
                 U operator delete(void*)                                                                                            
                 U operator new(unsigned long)                                                                                       
                 U __gxx_personality_v0                                                                                              
0000000000000000 T main

Notes:

  • nm and c++filt are from the binutils package you probably have already when developing.
  • you can only view the symbols if they're not removed by compiler options.

I just realized my answer wasn't really answering your question about reading the actual machine code. However, I think it provides some useful information instead. For reading the assembly-level operations for your processor, use objdump -d as proposed by @hnasarat

3
  • But how to read symbols ? It shows somwthing like this: ^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^[^@^@^@^D^@^@^@^@^@^@^
    – Sam
    Sep 17, 2012 at 20:38
  • @SAM What exactly don't you understand about the example in my answer? It shows symbols from my main.o file with the given command. Try to run the same on your .o file.
    – gertvdijk
    Sep 17, 2012 at 20:39
  • @SAM I missed some essential part of your question. See my updated answer
    – gertvdijk
    Sep 17, 2012 at 20:51
1

You should use the -S flag to make the compiler output assembly code.

So for your example the code would be g++ -S xyz.cpp and the assembly would be printed in xyz.s.

2
  • 2
    If you also select the lowest optimisation level -O0 you may find the object code more understandable because less optimisation transformations have been applied to the intermediate code. Sep 17, 2012 at 22:16
  • -O0 is (usually) the default optimization level so i felt safe in leaving it out.
    – Alex L.
    Sep 18, 2012 at 14:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .