17

Is there a way—much like viewing the result of preprocessing with gcc -E—to see what my objects look like once compiled into object files?

I am talking about GCC, but a solution including MSVC would be fine.

user257620
  • 193
  • 1
  • 5
  • Good point John. The constructor will be code, and like all code belongs to the class, not individual objects. Hence you won't find it in the object layout. – MSalters Jan 26 '10 at 11:20
  • 1
    You can use `g++ -S file.cpp` to get assembler output in `file.s`. Is this what you want? – clstrfsck Jan 26 '10 at 11:23

6 Answers6

29

For Visual C++:

I finally managed to dig up the (well-hidden!) undocumented compiler flags that MSVC++ supports using information from here and here. Here they are:

/d1reportSingleClassLayoutXXX
/d1reportAllClassLayout

(replace XXX with the class name)

Sahil Singh
  • 2,875
  • 36
  • 53
j_random_hacker
  • 49,279
  • 10
  • 101
  • 162
  • 1
    Note for others who may land into this quest: This switch will output directly to build output window. So add it in .cpp properties 'Command Line > Additional options'. If your desired class/struct did not show up in the list (despite it is declared in header file), I declared a new class in cpp and make it inherit from that class, that did the job for me. – Wappenull Sep 27 '19 at 10:03
6

For GCC compiled executables, checkout Pahole. It will show you how the compiler laid out your structs/classes and whether or not they have "holes" in them. Holes are padding due to memory alignment rules.

paxos1977
  • 144,851
  • 26
  • 89
  • 126
0

Object files contain binary data - the only higher level that most compilers can output is assembler, so if you can't read that you are out of luck. However, take a look at this question for more info in this area.

Community
  • 1
  • 1
0

You can inspect the layout of binaries and their contents using map files. Use /MAP for VC and -Map or --print-map for gcc.

Georg Fritzsche
  • 95,426
  • 26
  • 188
  • 233
0

Your question is a little confusing.

If you want to see the result of preprocessing with MSVC, you can use /E, /P/, or /EP.

There's an undocumented option in MSVC to show the data layout of structures and classes. I'm having trouble finding it right now.

Adrian McCarthy
  • 43,194
  • 15
  • 118
  • 167
  • >There's an undocumented option in MSVC to show the data layout of structures and classes. I'm having trouble finding it right now. this is exactly what i am looking for! – user257620 Jan 27 '10 at 16:39
  • 2
    I finally managed to dig up those switches: `/d1reportSingleClassLayoutXXX` and `/d1reportAllClassLayout`. – j_random_hacker May 25 '10 at 05:12
0

A constructor is just another function (unless it's in-lined). Object files contain a lot of info for the linker; so you should be able to find the function in the .a file (the function names will be mangled though).

Hassan Syed
  • 19,697
  • 11
  • 84
  • 166