how do I tell gcc not to assume an alignment?

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

 



Hi,

In https://github.com/GNS3/dynamips-community/issues/9 I found that
the reason for a crash was the compiler assuming that the pointer was
4-byte aligned in one of the O3 optimizations.
Using gcc-4.4.7-3.el6.x86_64 in a Cent6.4-x86_64 box.

I don't understand why it made that assumption, so I'd like to know
how to tell the compiler not to assume an alignment.



In detail:

/* Byte-swap a memory block */
void mem_bswap32(void *ptr,size_t len)
{
   m_uint32_t *p = ptr;
   size_t count = len >> 2;
   int i;

   for(i=0;i<count;i++,p++)
      *p = swap32(*p);
}

gcc turned the original loop into 3 loops:
* one loop for the initial 0-3 swaps (before we reach a 16-byte aligned address)
* one loop that processes 4 swaps at a time using MOVDQA, which needs
addresses to be 16-byte aligned or it triggers a protection fault
* one loop for the remaining 0-3 swaps

Now here's the catch... it assumes that the address is already 4-byte
aligned, so it discards the 2 lower bits when checking how many to run
in the first loop:
   0x0000000000406725 <+21>: 49 c1 ea 02 shr $0x2,%r10

unfortunately our buffer is 2-byte aligned:
   ptr = 0x7fffe81b3ff2

and it crashes when it tries to copy 16 bytes with MOVDQA from the
start of the buffer (not 16-byte aligned):
=> 0x00000000004067d0 <+192>: 66 0f 6f 0c 17 movdqa (%rdi,%rdx,1),%xmm1




[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