flat assembler
Message board for the users of flat assembler.

Index > Windows > Struct padding question (sorry!)

Author
Thread Post new topic Reply to topic
tripledot



Joined: 06 Jan 2009
Posts: 49
tripledot 08 Dec 2011, 12:16
Sorry if this has been done to death already; I have searched, promise!

In 64 bit Windows, I understand that struct members must be aligned to their natural boundary. I also get that the end of a struct should be padded so that the struct's total size is a multiple of the size of its largest member. What I'm not sure about is when a struct member is another struct (as opposed to a primitive datatype)... should I pad it so it aligns on a boundary the size of the inner struct, like this?

Code:
struct  SID_INFO            ; align 8
    pSid            dq  ?   ; 0x00
    pwzCommonName   dq  ?   ; 0x08
    pwzClass        dq  ?   ; 0x10
    pwzUPN          dq  ?   ; 0x18
ends                        ; 0x20

struct  SID_INFO_LIST                   ; align 32
    cItems      dd          ?           ; 0x00
                rd          7           ; 0x04
    aSidInfo    SID_INFO                ; 0x20
ends                                    ; 0x40    


The same question applies to end padding... when a struct member is another struct, do I pad the 'container' struct to a multiple of the size of the largest 'inner' struct, or just its largest member?

Hope this makes sense... thanks for looking! Confused
Post 08 Dec 2011, 12:16
View user's profile Send private message Reply with quote
tripledot



Joined: 06 Jan 2009
Posts: 49
tripledot 08 Dec 2011, 13:24
Quote:
Controlling Structure Packing

Projects should be compiled to use the default structure packing, which is currently 8 bytes because the largest integral type is 8 bytes. Doing so ensures that all structure types within the header files are compiled into the application with the same alignment the Windows API expects. It also ensures that structures with 8-byte values are properly aligned and will not cause alignment faults on processors that enforce data alignment.


http://msdn.microsoft.com/en-us/library/71kf49f1.aspx

Hmmm... after a bit more reading it seems like the Windows SDK wants to be compiled with "8-byte struct packing". I take this to mean that struct members should all be padded to 8 bytes in length. Why then, is this completely ignored in FASM's Windows includes? What am I missing?
Post 08 Dec 2011, 13:24
View user's profile Send private message Reply with quote
AsmGuru62



Joined: 28 Jan 2004
Posts: 1638
Location: Toronto, Canada
AsmGuru62 08 Dec 2011, 15:21
Assume you have 8 consequtive fields in a structure - every field is a BYTE (like in LOGFONT structure) - do every BYTE field is padded to 8 bytes, making every field of 8 bytes in length? Probably not.
Post 08 Dec 2011, 15:21
View user's profile Send private message Send e-mail Reply with quote
tripledot



Joined: 06 Jan 2009
Posts: 49
tripledot 08 Dec 2011, 15:29
No, I thought it was odd. But there is a lot of conflicting documentation out there, and it's quite hard to track down any "#pragma pack"s in the Windows headers. I wasn't sure whether some of the more common structs (such as those in the FASM includes) were more tightly packed, but that others might have to be heavily padded...

I'm glad somebody replied. I knew it was a dumb-arsed question, but there's no need to be condescending about it. The quote above (from MSDN regarding 64-bit code) is a little misleading, no?

You didn't really give me any useful information though... am I to assume that all Windows struct members are to be padded to their natural boundary? (e.g. mod2 for word, mod4 for dword, mod8 for qword, etc.)?
Post 08 Dec 2011, 15:29
View user's profile Send private message Reply with quote
AsmGuru62



Joined: 28 Jan 2004
Posts: 1638
Location: Toronto, Canada
AsmGuru62 08 Dec 2011, 17:33
I actually did not code for x64 (yet), so I really can't say... my apologies.
If I really needed that information - I would have built the x64 application and looked at these Win64 structures (like WNDCLASS, PAINTSTRUCT, etc.) in debugger and then field offsets would have revealed themselves.
Post 08 Dec 2011, 17:33
View user's profile Send private message Send e-mail Reply with quote
tripledot



Joined: 06 Jan 2009
Posts: 49
tripledot 09 Dec 2011, 09:41
Thanks - and sorry I was crabby.

Yes, that would be a good approach! As it is I'm really struggling with MSVC - I'm learning assembly because it's so much simpler than the bastard syntax of C/++/#!!! Just getting a project to build in MSVC is a nightmare.

I did find this last night though, in case it helps anyone else:

http://www.godevtool.com/GoasmHelp/64bits.htm#structs

It would be awesome if a macro magician could extend the struct macro to do this (hint) Wink
Post 09 Dec 2011, 09:41
View user's profile Send private message Reply with quote
AsmGuru62



Joined: 28 Jan 2004
Posts: 1638
Location: Toronto, Canada
AsmGuru62 09 Dec 2011, 11:44
Great link! Thanks.

So, not every type extended to 64-bits, but only handles and pointers.
Looks like a RECT structure (containing 4 UINTs) is the same size as in 32-bits.
Post 09 Dec 2011, 11:44
View user's profile Send private message Send e-mail Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.