Layout of compiled objects
Asked Answered
C

7

21

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.

Caligula answered 26/1, 2010 at 10:55 Comment(2)
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.Bats
You can use g++ -S file.cpp to get assembler output in file.s. Is this what you want?Wanderlust
D
33

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)

Disenchant answered 25/5, 2010 at 5:11 Comment(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.Heiress
D
7

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.

Dished answered 26/1, 2010 at 17:17 Comment(3)
the pahole link is deadHumiliation
@Humiliation I fixed up the link.Dished
related Find holes in C structs due to alignmentLevesque
S
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.

Symonds answered 26/1, 2010 at 10:59 Comment(0)
A
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.

Actinism answered 26/1, 2010 at 15:49 Comment(0)
E
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.

Electroacoustics answered 26/1, 2010 at 16:28 Comment(2)
>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!Caligula
I finally managed to dig up those switches: /d1reportSingleClassLayoutXXX and /d1reportAllClassLayout.Disenchant
M
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).

Medarda answered 1/2, 2010 at 21:43 Comment(0)
P
0

If you are asking about seeing the memory layout of an object's field (as other answers seems to hint) then there is a visual studio extension that does just that: Struct Layout by Ramon Viladomat (also available via Github)

This will add a contextual item when you right click on a struct definition, that will open a memory layout visualizer window.

The display looks like this:

enter image description here

It has it's quirks, and I've been struggling to set it up correctly on some large projects, but when it works it's extremely useful.

There is also an open feature request ticket on the MS developer community forums to add the feature to the editor by default. Please upvote it if you want that to get traction.

Paragraphia answered 13/6, 2023 at 18:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.