public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
From: Andrew Haley <aph@redhat.com>
To: Lars Poeschel <poeschel@lemonage.de>, Oleg Endo <oleg.endo@t-online.de>
Cc: gcc-help@gcc.gnu.org
Subject: Re: Unexpected unaligned access on arm
Date: Tue, 9 Jun 2020 10:38:58 +0100	[thread overview]
Message-ID: <427c76eb-f080-601b-fbc9-66d9e8e47a67@redhat.com> (raw)
In-Reply-To: <20200609080509.dbgs7zizqw7cajvq@lem-wkst-02.lemonage>

On 09/06/2020 09:05, Lars Poeschel wrote:
> On Mon, Jun 08, 2020 at 10:49:22PM +0900, Oleg Endo wrote:
>> On Mon, 2020-06-08 at 15:34 +0200, Lars Poeschel wrote:
>>>
>>> What am I missing ? What am I doing wrong ?
>>>
>>
>> You're casting an address of some byte array to a point to struct,
>> which has an alignment > 1 byte.  Try adding a #pragma pack (1) or
>> respective attribute.
>
> Thank you! The pragma does indeed the right thing. gcc now produces
> code, that accesses the fields in question individually.
> But shouldn't the option
> -mno-unaligned-access
> I use for compiling also do the same ?

No.

'-munaligned-access'
'-mno-unaligned-access'
     Enables (or disables) reading and writing of 16- and 32- bit values
     from addresses that are not 16- or 32- bit aligned.  By default
     unaligned access is disabled for all pre-ARMv6, all ARMv6-M and for
     ARMv8-M Baseline architectures, and enabled for all other
     architectures.  If unaligned access is not enabled then words in

-mno-unaligned-access tells the compiler not to generate accesses to
unaligned words. In this test case, the compiler didn't generate the
unaligned access, *you* did. You did this by casting the address of an
unaligned byte array to the address of a struct.

If you take the address of a struct, cast it to a char*, then cast it
back to a pointer to the struct type, that'll work. Likewise if you
cast the result of malloc() to any struct pointer.

  If you lie to the compiler, it will get its revenge.
    - Henry Spencer

-- 
Andrew Haley  (he/him)
Java Platform Lead Engineer
Red Hat UK Ltd. <https://www.redhat.com>
https://keybase.io/andrewhaley
EAC8 43EB D3EF DB98 CC77 2FAD A5CD 6035 332F A671


      reply	other threads:[~2020-06-09  9:39 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-06-08 13:34 Lars Poeschel
2020-06-08 13:48 ` Alexander Monakov
2020-06-08 13:49 ` Oleg Endo
2020-06-09  8:05   ` Lars Poeschel
2020-06-09  9:38     ` Andrew Haley [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=427c76eb-f080-601b-fbc9-66d9e8e47a67@redhat.com \
    --to=aph@redhat.com \
    --cc=gcc-help@gcc.gnu.org \
    --cc=oleg.endo@t-online.de \
    --cc=poeschel@lemonage.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).