Re: union issue

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Wednesday 30 May 2007 13:00, Andrew Haley wrote:
> Mihai Don?u writes:
>  > typedef struct _type2_t {
>  >     union {
>  >         uint32_t field1:31;
>  >         uint32_t field2:16;
>  >     };
>  >     uint32_t field3:1;
>  > } __PACKED type2_t;
> 
> I've read this three times now and I still don't understand your
> question.  You declare a packed struct, but you don't want it to be
> packed?  So don't declare it packed, then.  But that can't be what you
> really meant.
> 
> Andrew.
> 

Ok, I'm sorry if I wasn't clear.

Let me begin with the problem that I'm facing: I have an array of dword-s
where each dword has the following "bit-fields":
  * ordinal_or_name (bit 31, i.e. 0x80000000 )
  * ordinal (bits 0-15, i.e. 0x0000ffff)
  * name_rva (bits 0-30, i.e. 0x7fffffff)

(this is actually an entry in the ImportLookupTable of the .idata section of
a PE image (Windows .dll)).

If 'ordinal_or_name' is 0 then 'name_rva' contains a valid address (RVA). If
'ordinal_or_name' is 1 then 'ordinal' contains a valid number (import ordinal).

Instead of doing some "ugly" macros I wanted to do a dword-sized structure to
help me read the bit-fields more easily:

typedef struct _type2_t {
     union {
         uint32_t field1:31; /* bits 0-30 */
         uint32_t field2:16; /* bits 0-15 */
     };
     uint32_t field3:1;      /* bit 31 (or so I want) */
} __PACKED type2_t;

My problem is that gcc does not make the union *exactly* 31 bits, but rounds it
to a dword and as such, 'field3' maps to the first bit in the 2nd dword of a quad
(while I wanted it to map to the last bit in the 1st dword).

Btw, I'm talking x86(_x64) only here.

I hope this clears things up :)

-- 
Mihai Donțu

[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux