public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* asking for __attribute__((aligned()) clarification
@ 2019-08-19 12:46 "Markus Fröschle"
  2019-08-19 13:57 ` Paul Koning
  0 siblings, 1 reply; 11+ messages in thread
From: "Markus Fröschle" @ 2019-08-19 12:46 UTC (permalink / raw)
  To: gcc, gnu-gcc-bug

All,

this is my first post on these lists, so please bear with me.

My question is about gcc's __attribute__((aligned()). Please consider the following code:

#include <stdint.h>
    
typedef uint32_t uuint32_t __attribute__((aligned(1)));

uint32_t getuuint32(uint8_t p[]) {
    return *(uuint32_t*)p;
}

This is meant to prevent gcc to produce hard faults/address errors on architectures that do not support unaligned access to shorts/ints (e.g some ARMs, some m68k). On these architectures, gcc is supposed to replace the 32 bit access with a series of 8 or 16 bit accesses.

I originally came from gcc 4.6.4 (yes, pretty old) where this did not work and gcc does not respect the aligned(1) attribute for its code generation (i.e. it generates a 'normal' pointer dereference, consequently crashing when the code executes). To be fair, it is my understanding that the gcc manuals never promised this *would* work.

As - at least as far as I can tell - documentation didn't really change regarding lowering alignment for variables and does not appear to say anything specific regarding pointer dereference on single, misaligned variables, I was pretty astonished to see this working on newer gcc versions (tried 6.2 and 7.4), however. gcc appears to even know the differences within an architecture (68000 generates a bytewise copy while ColdFire - that supports unaligned access - copies a 32 bit value).

My question: is this now intended behaviour we can rely on? If yes, can we have documentation upgraded to clearly state that this use case is valid?

Thank you.
Markus

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

end of thread, other threads:[~2019-08-21 17:35 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-08-19 12:46 asking for __attribute__((aligned()) clarification "Markus Fröschle"
2019-08-19 13:57 ` Paul Koning
2019-08-19 14:01   ` Richard Earnshaw (lists)
2019-08-19 14:08     ` Alexander Monakov
2019-08-19 14:12       ` Paul Koning
2019-08-20  5:46       ` Aw: " "Markus Fröschle"
2019-08-21 14:29         ` Alexander Monakov
2019-08-21 14:32           ` Paul Koning
2019-08-21 14:58             ` Alexander Monakov
2019-08-21 16:50               ` Paul Koning
2019-08-21 17:35                 ` 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).