Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.
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?

share|improve this question
add comment

3 Answers 3

up vote 1 down vote accepted

Check out the binutils package.

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

Then try running objdump -d xyz.o.

share|improve this answer
1  
It works GREAT !!! –  Sam Sep 17 '12 at 21:17
add comment

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.

share|improve this answer
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. –  Colin Ian King Sep 17 '12 at 22:16
    
-O0 is (usually) the default optimization level so i felt safe in leaving it out. –  Alex L. Sep 18 '12 at 14:28
add comment

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

share|improve this answer
    
But how to read symbols ? It shows somwthing like this: ^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^[^@^@^@^D^@^@^@^@^@^@^ –  Sam Sep 17 '12 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 '12 at 20:39
    
@SAM I missed some essential part of your question. See my updated answer –  gertvdijk Sep 17 '12 at 20:51
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.