From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 53708 invoked by alias); 19 Aug 2019 12:46:14 -0000 Mailing-List: contact gcc-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-owner@gcc.gnu.org Received: (qmail 53695 invoked by uid 89); 19 Aug 2019 12:46:14 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-5.8 required=5.0 tests=AWL,BAYES_00,RCVD_IN_DNSWL_NONE,XPRIO autolearn=no version=3.3.1 spammy=74, upgraded, accesses, asking X-HELO: mout.kundenserver.de Received: from mout.kundenserver.de (HELO mout.kundenserver.de) (212.227.126.130) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Mon, 19 Aug 2019 12:46:13 +0000 Received: from [217.89.113.18] ([217.89.113.18]) by web-mail.kundenserver.de (3c-app-1and1-bs05.server.lan [172.19.170.162]) (via HTTP); Mon, 19 Aug 2019 14:46:00 +0200 MIME-Version: 1.0 Message-ID: From: =?UTF-8?Q?=22Markus_Fr=C3=B6schle=22?= To: gcc@gcc.gnu.org, gnu-gcc-bug@gnu.org Subject: asking for __attribute__((aligned()) clarification Content-Type: text/plain; charset=UTF-8 Date: Mon, 19 Aug 2019 12:46:00 -0000 X-IsSubscribed: yes X-SW-Source: 2019-08/txt/msg00133.txt.bz2 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 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