Related Projects > NASMX

Win64 Structure Alignment (Win API)

<< < (2/6) > >>

Bryant Keller:

--- Quote from: encryptor256 on January 20, 2014, 05:27:05 AM ---Solution to structure alignment.
I think Win64 is built on C, so we have to learn, how C aligns structures (according to BITS),
then implement new nasm.exe command switch, something like: "-cstruct", which means,
when ever compiler finds structure, which starts with "struct" definition, then it aligns
the structure according to C rules, + according to __BITS__ too, so,
"-cstruct" + BITS 32 : defines structures and aligns them on 32 bit boundaries.
"-cstruct" + BITS 64 : defines structures and aligns them on 64 bit boundaries.

Q: Why do we have to support C structures in NASM?
A: Why not! NASM is built on that C\C++ thingy too, so, why couldn't we! :D

--- End quote ---

The development of NASMX is largely independent of the Netwide Assembler. Early on, a design choice was made to not break backward compatibility with old NASM versions whenever possible, this is because distributions like Debian will only allow new versions of software into their repositories after the software has been stable for some time.

Another thing to keep in mind is that NASM doesn't technically support structures.  There are a lot of features that NASM has that aren't actually hard-coded in. If you look at the source code, you'll notice that there is a bunch of macros in a file called standard.mac, one of which is the implementation of STRUC and ENDSTRUC. These are loaded into the nasm.exe file at compile time so they are always available to you. As you can tell by that file, structure support is just some fancy macro wrapper for the absolute directive. This makes since because structures are just made up of a bunch of label offsets to its members.

encryptor256:

--- Quote from: encryptor256 on January 20, 2014, 05:27:05 AM ---Let's cut old cpu support and old Ms Windows base and go with acceleration!
--- End quote ---


--- Quote from: Rob Neff on January 20, 2014, 06:08:16 PM ---there is a huge segment of the population out there still running Linux and WinXP on 32-bit machines.  We can't just suddenly force them all to upgrade
--- End quote ---


--- Quote from: Bryant Keller on January 21, 2014, 12:48:27 AM ---a design choice was made to not break backward compatibility
--- End quote ---

Okay, okay, i agree, i wasn't thinking straight on that, sorry for that,
i blame early morning, when i posted that hostile idea, to cut backward compatibility. :D


--- Quote from: Bryant Keller on January 21, 2014, 12:48:27 AM ---Another thing to keep in mind is that NASM doesn't technically support structures.  There are a lot of features that NASM has that aren't actually hard-coded in. If you look at the source code, you'll notice that there is a bunch of macros in a file called standard.mac, one of which is the implementation of STRUC and ENDSTRUC.

--- End quote ---

This is something new, i was thinking otherwise, but it seems, that it's now other-otherwise, was thinking, that "struct" is not a macro, but it is. :D

Well, it seems, you both, already have some bright ideas, about, how to solve structure alignment, at least that part where NASMX supported Win64, maybe.

Rob Neff:
Ok.  I'm 99% finished with part 1 - correcting the struc alignment issue.
I'm still working on the union stuff which is rather broken at the moment but struc's are fine now I believe.
I'll work on part 2 - pack()'ing - later on.

Can you download a snapshot and test please?

Confirmation is appreciated.

Thanks.

encryptor256:

--- Quote from: Rob Neff on January 23, 2014, 02:47:52 AM ---Ok.  I'm 99% finished with part 1 - correcting the struc alignment issue.
I'm still working on the union stuff which is rather broken at the moment but struc's are fine now I believe.
I'll work on part 2 - pack()'ing - later on.

Can you download a snapshot and test please?

Confirmation is appreciated.

Thanks.

--- End quote ---

Confirm!

Tested some winAPI and custom made structures.
Verified by brains and compared against Win64 Pelles C.
Result:
Structure Alignment now is correct!
* Well, at least, didn't managed to break it by finding any miss alignments.

Structure size's and member offset's reported, are correct.
By correct, i mean, they were equal.

Added attachment: "win64_demo1.asm", a file, where i did some testing, this file was derived from NASMX/WIN64/DEMO1.

Here is Win64 Pelles C, structure size + alignment + offset tester code, can be used for other structures too:

--- Code: ---        #define ins(x) wsprintf(buffer,"%s\r\n%s: %d",buffer,#x,(int)&m.##x - (int)&m)
#define sz(x) wsprintf(buffer,"%s\r\n%s: %d",buffer,#x,sizeof(x))
#define initial(x) wsprintf(buffer,"\r\nSize: %d\r\nAligned: %d\r\n",sizeof(x),__alignof(x))

char buffer[4096];

        // TEST: MSG
// -------------------------------------------
MSG m;

initial(MSG);

ins(hwnd);
        ins(message);
        ins(wParam);
        ins(lParam);
        ins(time);
        ins(pt.x);
        ins(pt.y);
MessageBox(0,buffer,0,0);

--- End code ---

Structures tested and can be found in attachment or elsewhere: CREATESTRUCT, STARTUPINFO, WNDCLASSEX, MyStructureD, MyStructureC, MyStructureB, MyStructureA, PAINTSTRUCT, LOGFONT, MSG.

Bye.

Rob Neff:
Thank you for your efforts, encryptor256.  8)

Now, can I ask another favor?  I'd like you to write a console app, using NASMX only, where the program tests the generated struc size and offsets against known values.  It needs to assemble in both 32 and 64-bit mode.  The values to compare against must be defined such that, for example, MSG.wParam offset will change depending on BITS.  The program should printf() a message if it finds an error and return a positive value, otherwise printf() a success message and return zero (0).

I would like to see quite a few self-made strucs that test for the various permutations - kind of like your MyStructureX - but be ruthless in definitions trying to break it.  If possible, only %include 'nasmx.inc' and %include 'test.inc'.

I would like to include this work into NASMX as a way to automate testing and to prevent any regressions of future changes.  Please ensure NASMX copyright with yourself as the author for the source code.  Thanks again for your hard work.

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version