https://gcc.gnu.org/bugzilla/show_bug.cgi?id=91927
Bug ID: 91927 Summary: -mstrict-align doesn't prevent unaligned accesses at -O2 and -O3 on AARCH64 targets Product: gcc Version: 8.3.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: target Assignee: unassigned at gcc dot gnu.org Reporter: gr.audio at gmail dot com Target Milestone: --- Target: aarch64-elf Hello, I'm writing code for an AARCH64 target, and ran into the following issue. First off, at the stage my code runs, the MMU is not enabled, so the CPU doesn't allow unaligned accesses. I thus enabled the "-mstrict-align" option. The GCC (8.3.0) binaries I'm using are from ARM: https://developer.arm.com/tools-and-software/open-source-software/developer-tools/gnu-toolchain/gnu-a/downloads but I built GCC 9.2.0 for aarch64-elf and got the same issue. I get an exception (unaligned access) when my code runs, and investigated. Here is how it happens. I'm accessing successively two successive uint32_t members of a packed struct: eg: --------------------------------------------------------------- typedef struct __attribute__((__packed__)) { ... uint32_t nWidth; uint32_t nHeight; ... } MyStruct_t; ... MyStruct_t sThisStruct; ... // My code accesses them just like so: ... = sThisStruct.nWidth; ... = sThisStruct.nHeight; --------------------------------------------------------------- At -O2 and -O3, despite the "-mstrict-align" option, GCC emits code that creates an unaligned access: ldr d0, [sp, 52] (SP + 52 is 4-byte aligned obviously, but not 8-byte aligned, and thus, this 64-bit access yields an exception.) GCC optimizes the two 32-bit accesses in one 64-bit access, but it shouldn't in this case, with the "-mstrict-align" option. At -O1 and below, GCC correctly emits: ldr w0, [sp, 52] ... ldr w0, [sp, 56] instead, which are correctly aligned accesses. This issue looks like a cousin to the following bug: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=71727 It looks like the "-mstrict-align" option is not correctly enforced at certain optimization levels, for different optimizations (but with the same overall problem of potentially unaligned accesses). --------------------------------------------------------------- GCC version: Target: aarch64-elf Configured with: /tmp/dgboter/bbs/dsggnu-vm-1-x86_64--mingw32-i686/buildbot/mingw32-i686--aarch64-elf/build/src/gcc/configure --target=aarch64-elf --prefix=/tmp/dgboter/bbs/dsggnu-vm-1-x86_64--mingw32-i686/buildbot/mingw32-i686--aarch64-elf/build/bui ld-mingw-aarch64-elf/install// --with-gmp=/tmp/dgboter/bbs/dsggnu-vm-1-x86_64--mingw32-i686/buildbot/mingw32-i686--aarch64-el f/build/build-mingw-aarch64-elf/host-tools --with-mpfr=/tmp/dgboter/bbs/dsggnu-vm-1-x86_64--mingw32-i686/buildbot/mingw32-i68 6--aarch64-elf/build/build-mingw-aarch64-elf/host-tools --with-mpc=/tmp/dgboter/bbs/dsggnu-vm-1-x86_64--mingw32-i686/buildbot /mingw32-i686--aarch64-elf/build/build-mingw-aarch64-elf/host-tools --with-isl=/tmp/dgboter/bbs/dsggnu-vm-1-x86_64--mingw32-i 686/buildbot/mingw32-i686--aarch64-elf/build/build-mingw-aarch64-elf/host-tools --disable-shared --disable-nls --disable-thre ads --disable-tls --enable-checking=release --enable-languages=c,c++,fortran --with-newlib --with-libiconv-prefix=/tmp/dgbote r/bbs/dsggnu-vm-1-x86_64--mingw32-i686/buildbot/mingw32-i686--aarch64-elf/build/build-mingw-aarch64-elf/host-tools --host=i68 6-w64-mingw32 --with-pkgversion='GNU Toolchain for the A-profile Architecture 8.3-2019.03 (arm-rel-8.36)' --with-bugurl=https ://bugs.linaro.org/ Thread model: single gcc version 8.3.0 (GNU Toolchain for the A-profile Architecture 8.3-2019.03 (arm-rel-8.36)) --------------------------------------------------------------- Also happens with aarch-64-elf-gcc 9.2.0 (custom built).