On 04/04/2017 06:56 AM, Florian Weimer wrote:
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.
There are target-independent tests in the test suite that exercise this support so I'd say the answer is yes. For example gcc.c-torture/execute/strct-pack-1.c (there are others as well). Martin
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