public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* which gcc options can control layout of bit fields
@ 2021-10-26 23:23 Andrew Makhorin
  2021-10-26 23:39 ` Jonathan Wakely
  0 siblings, 1 reply; 2+ messages in thread
From: Andrew Makhorin @ 2021-10-26 23:23 UTC (permalink / raw)
  To: gcc-help

Hi,

Could anyone tell me which gcc options can control layout of bit
fields?

The problem I encountered is that gcc for Cygwin doesn't follow
System V ABI for i386. Namely, ABI says: 

   A bit-field must entirely reside in a storage unit appropriate for 
   its declared type. Thus a bit-field never crosses its unit boundary.

But in the code generated by gcc under Cygwin bit fields are allocated
contiguously and may cross the unit boundary. On the other hand, gcc
under Linux follows the ABI conventions.

Example:

struct { int a; char b; int c:14, d:14; int e; }
   s = {0xAAAAAAAA, 0xBB, 0xCCC, 0xDDD, 0xEEEEEEEE};

static int *p = (int *)&s;

int main(void)
{
      printf("0x%08X 0x%08X 0x%08X 0x%08X\n", p[0], p[1], p[2], p[3]);

      return 0;
}

Under Cygwin s.d is splitted between p[1] and p[2]:

0xAAAAAAAA 0x774CCCBB 0x00000003 0xEEEEEEEE

Under Linux all is okay:

0xAAAAAAAA 0x000CCCBB 0x00000DDD 0xEEEEEEEE


Thank you,

Andrew Makhorin

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: which gcc options can control layout of bit fields
  2021-10-26 23:23 which gcc options can control layout of bit fields Andrew Makhorin
@ 2021-10-26 23:39 ` Jonathan Wakely
  0 siblings, 0 replies; 2+ messages in thread
From: Jonathan Wakely @ 2021-10-26 23:39 UTC (permalink / raw)
  To: Andrew Makhorin; +Cc: gcc-help

On Wed, 27 Oct 2021, 00:24 Andrew Makhorin via Gcc-help, <
gcc-help@gcc.gnu.org> wrote:

> Hi,
>
> Could anyone tell me which gcc options can control layout of bit
> fields?
>
> The problem I encountered is that gcc for Cygwin doesn't follow
> System V ABI for i386.



I think that's intentional. Have you tried -mabi=sysv ?

https://gcc.gnu.org/onlinedocs/gcc/x86-Options.html


Namely, ABI says:
>
>    A bit-field must entirely reside in a storage unit appropriate for
>    its declared type. Thus a bit-field never crosses its unit boundary.
>
> But in the code generated by gcc under Cygwin bit fields are allocated
> contiguously and may cross the unit boundary. On the other hand, gcc
> under Linux follows the ABI conventions.
>
> Example:
>
> struct { int a; char b; int c:14, d:14; int e; }
>    s = {0xAAAAAAAA, 0xBB, 0xCCC, 0xDDD, 0xEEEEEEEE};
>
> static int *p = (int *)&s;
>
> int main(void)
> {
>       printf("0x%08X 0x%08X 0x%08X 0x%08X\n", p[0], p[1], p[2], p[3]);
>
>       return 0;
> }
>
> Under Cygwin s.d is splitted between p[1] and p[2]:
>
> 0xAAAAAAAA 0x774CCCBB 0x00000003 0xEEEEEEEE
>
> Under Linux all is okay:
>
> 0xAAAAAAAA 0x000CCCBB 0x00000DDD 0xEEEEEEEE
>
>
> Thank you,
>
> Andrew Makhorin
>

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2021-10-26 23:40 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-10-26 23:23 which gcc options can control layout of bit fields Andrew Makhorin
2021-10-26 23:39 ` Jonathan Wakely

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).