From: Sam Ravnborg <sam(a)ravnborg.org>
Date: Mon, 25 Apr 2016 22:48:45 +0200
A simpler model would be
#define LOADER(TYPE, addr) \
({ \
TYPE ret; \
memcpy(&ret, (void *)addr, sizeof(TYPE)); \
ret; \
})
I would expect the compiler to optimize this away for archs
that do not require aligned access.
The compiler is allowed to "look through" void pointer casts and
use the alignment guaranteed by the original types.
That is why memcpy()'s aren't used for unaligned access handling.
Take a look at include/linux/unaligned/packed_struct.h in the kernel
sources, which is in my opinion the most portable and efficient way to
deal with this problem.
That code does the right thing on both architectures where alignment
matters, and where it does not.