Target support for packed/aligned (1) structs

Florian Weimer fweimer@redhat.com
Tue Apr 4 12:56:00 GMT 2017


For the usual GNU/Linux targets, does GCC support unaligned loads 
through pointers to packed structs?  That is, something like that:

struct __attribute__ ((may_alias, packed, aligned (1))) header
{
   uint16_t id;
   uint16_t flags;
   uint16_t qdcount;
   uint16_t ancount;
   uint16_t nscount;
   uint16_t adcount;
};

uint16_t
get_question_count (const void *buf)
{
   const struct header *header = buf;
   return ntohs (header->qdcount);
}

And this should work even if buf is not properly aligned.

I know there are problems if I take the address of a member in struct 
header and use that pointer, but I hope that direct use of the member is 
okay.

This would allow us to simplify some code in glibc and avoid defensive 
copies to a properly aligned struct.

Thanks,
Florian



More information about the Gcc-help mailing list