How to achieve 32 pack alignment in VS compiler

Hi Experts,

I am working to develop an Windows driver for 64bit architecture.
Am trying to fetch the data from the hardware address by using a structure.
But due to misalignment of the structure,am not able to fetch the data as expected.

In GCC Compiler,we achieved the alignment by using __attribute((aligned(32), packed)).
But in windows Visual Studio Compiler,how can I achieve as it does not have 32 pack.

It will be very helpful if you people have few minutes for it to answer.

Thanks in Advance.

You are going to have to do it the old fashion way, of putting in the
padding elements yourself.

Don Burn
Windows Driver Consulting
Website: http://www.windrvr.com

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of xxxxx@gmail.com
Sent: Thursday, April 28, 2016 7:45 AM
To: Windows System Software Devs Interest List
Subject: [ntdev] How to achieve 32 pack alignment in VS compiler

Hi Experts,

I am working to develop an Windows driver for 64bit architecture.
Am trying to fetch the data from the hardware address by using a structure.
But due to misalignment of the structure,am not able to fetch the data as
expected.

In GCC Compiler,we achieved the alignment by using
__attribute((aligned(32), packed)).
But in windows Visual Studio Compiler,how can I achieve as it does not have
32 pack.

It will be very helpful if you people have few minutes for it to answer.

Thanks in Advance.


NTDEV is sponsored by OSR

Visit the list online at:
http:

MONTHLY seminars on crash dump analysis, WDF, Windows internals and software
drivers!
Details at http:

To unsubscribe, visit the List Server section of OSR Online at
http:</http:></http:></http:>

Sorry… I am new to this. Can you please elaborate it. How to achieve it exactly.

Basically you need to look at the types of the elements and add padding
fields after each element to force a 32 byte alignment. For example:

#pragma pack
__declspec(align(32)) struct _MY_REGISTERS
{
ULONG Reg1;
ULONG Pad1[7];
ULONG Reg2;

};

You could use #pragma pack(16) but you still will need padding, and my
preference is for explicit padding at that point. Also the
“__declspec(align(32))” is only there in case you declare a structure in
memory to ensure the correct alignment.

Don Burn
Windows Driver Consulting
Website: http://www.windrvr.com

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of xxxxx@gmail.com
Sent: Thursday, April 28, 2016 8:46 AM
To: Windows System Software Devs Interest List
Subject: RE:[ntdev] How to achieve 32 pack alignment in VS compiler

Sorry… I am new to this. Can you please elaborate it. How to achieve it
exactly.


NTDEV is sponsored by OSR

Visit the list online at:
http:

MONTHLY seminars on crash dump analysis, WDF, Windows internals and software
drivers!
Details at http:

To unsubscribe, visit the List Server section of OSR Online at
http:</http:></http:></http:>

Actually My structure looks like
struct My_Structure {
u64 lvl : 3;
u64 access : 1;
enum pze size :4;
enum trans type :3;
u64 th : 1;
u64 tabptr : 40;
u64 or : 1;
u64 tph : 2;
u64 nd : 1;
u64 sp : 8;
}; /* struct My_Structure [8 byte] */

In GCC compiler,I aligned the above struture by using __((aligned(8),packed)). So that the sizeof the above structure is 8 bytes. But even after applying hat you said, am not able to get the size as of 8bytes. Is there any method to achieve such alignment.

> In GCC Compiler,we achieved the alignment by using __attribute((aligned(32), packed)).

But in windows Visual Studio Compiler,how can I achieve as it does not have 32 pack.

#pragma pack() is supported by both.


Maxim S. Shatskih
Microsoft MVP on File System And Storage
xxxxx@storagecraft.com
http://www.storagecraft.com

  1. #pragma pack specifies the upper boundary for alignment.
  2. With MSC, you can use __declspec(align(n)) on a structure declaration, structure member declaration, variable declaration to force alignment to a specific boundary.
  3. If you want alignment of 8 for a structure which contains 64-bit items (like your example), you don’t need to do anything. 8 bytes is its natural alignment. Since the sum of the bitfields is 64 bit, its size should be 8 bytes by itself, without any special declarations.

You didn’t say what sizeof you’re getting.
Your problem must be in something else. For example, you may be using wrong function to read the device registers.

xxxxx@gmail.com wrote:

Actually My structure looks like
struct My_Structure {
u64 lvl : 3;
u64 access : 1;
enum pze size :4;
enum trans type :3;
u64 th : 1;
u64 tabptr : 40;
u64 or : 1;
u64 tph : 2;
u64 nd : 1;
u64 sp : 8;
}; /* struct My_Structure [8 byte] */

In GCC compiler,I aligned the above struture by using __((aligned(8),packed)). So that the sizeof the above structure is 8 bytes. But even after applying hat you said, am not able to get the size as of 8bytes. Is there any method to achieve such alignment.

It’s the enums that are causing the problem. Is this a C file, or a C++
file? In C, bitfields have to be built-in integral types. You aren’t
allowed to use an enum, so the compiler can do what it wants. In this
case, it’s adding padding to align “size” on an 8-byte boundary,. “lvl”
and “access” are in the first qword, “size” and “type” are in the second
qword, and the other fields become a third qword, so the structure is 24
bytes.

Further, the exact layout of bitfields within a structure has always
been implementation-dependent. For example, you probably think that
“lvl” should be the least significant bits of that structure, but in
fact neither the C nor the C++ standard specifies that, and there ARE
compilers that assign the fields in the opposite order. The compiler
can do what it wants.

In the short term, you can do what you want by changing the enums to
“u64”. In the long term, you’re probably better off with macros to
shift-and-mask.


Tim Roberts, xxxxx@probo.com
Providenza & Boekelheide, Inc.

> Further, the exact layout of bitfields within a structure has always

been implementation-dependent.

Exactly, that’s why avoidance of structures and using the stuff like like:

Val = ( READ_REGISTER_ULONG(x) >> MY_BIT_FIELD_SHIFT) & MY_BIT_FIELD_MASK;

is better. This even provides you with control over what PCI byte enables will be issued.

You can wrap it around by inlines.


Maxim S. Shatskih
Microsoft MVP on File System And Storage
xxxxx@storagecraft.com
http://www.storagecraft.com