Page 2 of 2

Posted: Sat Jun 30, 2007 4:10 pm
by Kevin McGuire
JJeronimo wrote:
frank wrote:Try changing it to

Code: Select all

#pragma pack( push )
#pragma pack( 1 )
struct gdt_entry
{
   <snip>
}; 
#pragma pack( pop )
The align problem is more likely to be in the gp structure, cause the limit, which comes first, is shorter than the address...
JJ
It is. I double checked his structure earlier on, but never thought about his pointer being padded.

Code: Select all

struct gdt_entry{unsigned short limit_low;unsigned short base_low;unsigned char base_middle;unsigned char access;unsigned char granularity;unsigned char base_high;}; 
struct __attribute__ ((__packed__)) gdt_entry_nopad{unsigned short limit_low;unsigned short base_low;unsigned char base_middle;unsigned char access;unsigned char granularity;unsigned char base_high;}; 
struct gdt_ptr{unsigned short limit;unsigned int base;};
struct __attribute__ ((__packed__)) gdt_ptr_nopad{unsigned short limit;unsigned int base;};
printf("%u - %u\n", sizeof(struct gdt_entry), sizeof(struct gdt_entry_nopad));
printf("%u - %u\n", sizeof(struct gdt_ptr), sizeof(struct gdt_ptr_nopad));
If anyone declares and uses a Global Descriptor Pointer, then they need to make sure it is not padded by the compiler by using the compiler attribute for GCC, or MSVC (next line):

GCC:struct __attribute__ ((__packed__)) gdt_ptr;

MSVC:#pragma pack( push )
MSVC:#pragma pack( 1 )
MSVC:struct gdt_entry{ unsigned short limit; unsigned int base;};
MSVC:#pragma pack( pop )

Posted: Sat Jun 30, 2007 4:54 pm
by LaurensR1983
Btw... do i have to do the pragma stuff for each seperate struct like:

Code: Select all

#pragma pack( push ) 
#pragma pack( 1 ) 

struct gdt_entry
{
    unsigned short limit_low;
    unsigned short base_low;
    unsigned char base_middle;
    unsigned char access;
    unsigned char granularity;
    unsigned char base_high;
};
#pragma pack( pop )

#pragma pack( push ) 
#pragma pack( 1 ) 

struct gdt_ptr
{
    unsigned short limit;
    unsigned int base;
};

#pragma pack( pop )

Or can I do multiple at once, like this:

Code: Select all

#pragma pack( push ) 
#pragma pack( 1 ) 

struct gdt_entry
{
    unsigned short limit_low;
    unsigned short base_low;
    unsigned char base_middle;
    unsigned char access;
    unsigned char granularity;
    unsigned char base_high;
};

struct gdt_ptr
{
    unsigned short limit;
    unsigned int base;
};

#pragma pack( pop )


Posted: Sat Jun 30, 2007 5:12 pm
by Kevin McGuire
It will work for both of them since the push saves the current padding specifier, then you actually set a new one, and finally pop the old padding specifier to return the default behavior.

Posted: Sat Jun 30, 2007 5:20 pm
by LaurensR1983
Thanks for your reply!

I prefer the second way, because the code gets a bit more readable