[Bug c/51628] __attribute__((packed)) is unsafe in some cases

2018-04-12 Thread dingcurie at icloud dot com
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=51628

W.H. Ding  changed:

   What|Removed |Added

 CC||dingcurie at icloud dot com

--- Comment #47 from W.H. Ding  ---
Hi, everyone

I wonder if this issue has to do with the bug-like problem I encountered when
accessing an unaligned stand-alone global variable (rather than a member of a
packed struct). A test case is as follows:

char g_c = 'x';
int g_d __attribute__((aligned(1))) = 13;

int main(void)
{
g_c = 'z';
//
g_d = 33;// Crash on, in my case, ARM Cortex-M0
//
return 0;
}

Due to the presence of g_c, g_d is at an odd address, as the map file
indicates:

 .data.g_c  0x20200x1 ./src/test.o
0x2020g_c
 .data.g_d  0x20210x4 ./src/test.o
0x2021g_d

The generated assembly, however, accesses g_d as if it were properly aligned:

   8:../src/test.c  //
   9:../src/test.c  g_d = 33;
  52.loc 1 9 0
  53 000a 044B  ldr r3, .L2+4
  54 000c 2122  movsr2, #33
  55 000e 1A60  str r2, [r3]
  10:../src/test.c  //

BTW: I'm using GNU ARM Embedded Toolchain 7-2017-q4-major and on Windows 7.

Any ideas?

[Bug c/51628] __attribute__((packed)) is unsafe in some cases

2018-04-13 Thread dingcurie at icloud dot com
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=51628

--- Comment #51 from W.H. Ding  ---
(In reply to Sven from comment #49)

> This doesn't work. The aligned attribute is for providing additional
> alignment hints. The GCC documentation clearly states, that aligned can
> increase the alignment. So g_d is still 4 byte aligned, and correctly so.

It's really confusing about using 'aligned' attributes in various situations.

Under the title "Specifying Attributes of Variables", the doc says "When used
as part of a typedef, the aligned attribute can both increase and decrease
alignment" without an example. While under the title "Specifying Attributes of
Types", the doc gives an example of typedef but states at the end "The aligned
attribute can only increase alignment."

So perhaps, my test case should have been:

typedef int unaligned_int __attribute__((aligned(1)));

char g_c = 'x';
unaligned_int g_d = 13;

int main(void)
{
g_c = 'z';
//
g_d = 33;// Crash on, in my case, ARM Cortex-M0
//
return 0;
}

But the result is the same. After all, the 'aligned' attribute took effect in
either case since g_d is at an odd address as a consequence.

[Bug c/51628] __attribute__((packed)) is unsafe in some cases

2018-04-15 Thread dingcurie at icloud dot com
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=51628

--- Comment #52 from W.H. Ding  ---
(In reply to rguent...@suse.de from comment #48)

So, is there an old bug that covers my problem, or should I file a new one?
Thank you.