public inbox for u-boot@lists.denx.de
 help / color / mirror / Atom feed
From: Reinhard Meyer <u-boot@emk-elektronik.de>
To: u-boot@lists.denx.de
Subject: [U-Boot] packed attribute problem
Date: Mon, 04 Oct 2010 14:43:48 +0200	[thread overview]
Message-ID: <4CA9CC04.5020800@emk-elektronik.de> (raw)
In-Reply-To: <20101004122900.A090B1539A0@gemini.denx.de>

Dear Wolfgang Denk,

> Dear Reinhard Meyer,
> 
> In message <4CA9BE94.6000401@emk-elektronik.de> you wrote:
>> Do you imply that the code is really different when the pointer gets
>> its value by assigning it NOT to a packed entity? Hard to believe.
> 
> This is a special "feature" of GCC on ARM.
> 
> -> cat foo.c
> #define writel(v,a)              (*(volatile unsigned int *)(a) = (v))
> 
> struct p {
>         int n;
> } __attribute__ ((packed));
> 
> struct q {
>         int n;
> };
> 
> void foo()
> {
>         struct p *pp = (struct p *)0x1000;
> 
>         pp->n = 5;
> }
> 
> void bar()
> {
>         struct q *qq = (struct q *)0x1000;
> 
>         qq->n = 5;
> }
> -> arm-linux-gcc -O -S foo.c
> -> cat foo.s
>         .file   "foo.c"
>         .text
>         .align  2
>         .global foo
>         .type   foo, %function
> foo:
>         @ Function supports interworking.
>         @ args = 0, pretend = 0, frame = 0
>         @ frame_needed = 0, uses_anonymous_args = 0
>         @ link register save eliminated.
>         @ lr needed for prologue
>         mov     r3, #4096
>         mov     r2, #0
>         orr     r1, r2, #5
>         strb    r1, [r3, #0]
>         strb    r2, [r3, #1]
>         strb    r2, [r3, #2]
>         strb    r2, [r3, #3]
>         bx      lr
>         .size   foo, .-foo
>         .align  2
>         .global bar
>         .type   bar, %function

In a non-packed struct an int will never be unaligned
(unless you use an unaligned pointer to the whole struct)

In a packed struct an int might be unaligned, so it
_might_ make sense for the compiler to handle that
differently on ARM. Assume you overlay (bad idea anyway)
a packed structure over some communication data stream
thats is byte oriented. On most architectures that would
work (besides obvious endianess issues) but on ARM it would
(without raising an exception) malfunction.
[remember the "display_buffer issue"]

> bar:
>         @ Function supports interworking.
>         @ args = 0, pretend = 0, frame = 0
>         @ frame_needed = 0, uses_anonymous_args = 0
>         @ link register save eliminated.
>         @ lr needed for prologue
>         mov     r2, #5
>         mov     r3, #4096
>         str     r2, [r3, #0]
>         bx      lr
>         .size   bar, .-bar
>         .ident  "GCC: (GNU) 4.2.2"
> 
> 
> Note that this is with GCC 4.2.2. Even GCC 4.0.0 behaves the same, so
> this is *not* an issue with very recent tool chains.

OK, for directly adressing elements inside a packed struct;
but the original post said:

"struct xyz {
	int	x;
	int	y;
	int	z[CONST];
} __attribute__ ((packed));

struct xyz *abc;
u32 * status_reg = (u32 *)&abc->z[0];

writel(status, status_reg);"

So the "status_reg" pointer is in a completely unrelated (to the packed struct)
"u32 *" and still the access is done like it was packed. If the
compiler silently drags that attribute along into the "u32 *"
THAT is really sick!

Reinhard

  reply	other threads:[~2010-10-04 12:43 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-10-04  9:38 [U-Boot] packed attribute problem Vipin Kumar
2010-10-04 10:17 ` Wolfgang Denk
2010-10-04 10:30   ` Vipin Kumar
2010-10-04 10:44     ` Wolfgang Denk
2010-10-04 10:56       ` Reinhard Meyer
2010-10-04 11:01         ` Vipin Kumar
2010-10-04 11:46           ` Reinhard Meyer
2010-10-04 12:29             ` Wolfgang Denk
2010-10-04 12:43               ` Reinhard Meyer [this message]
2010-10-04 12:50                 ` Balau
2010-10-04 12:58                   ` Wolfgang Denk
2010-10-04 13:02                     ` [U-Boot] DDR SPD table sywang
2010-10-04 15:56                       ` Mike Frysinger
2010-10-04 12:56                 ` [U-Boot] packed attribute problem Wolfgang Denk
2010-10-05  3:45                 ` Vipin Kumar
2010-10-05 11:43                 ` Detlev Zundel
2010-10-05 18:03                   ` Scott Wood
2010-10-07  9:59                     ` Detlev Zundel
2010-10-07 16:46                       ` Scott Wood
2010-10-07 17:57                         ` Wolfgang Denk
2010-10-07 18:52                           ` Scott Wood
2010-10-07 19:31                             ` Wolfgang Denk
2010-10-07 20:15                               ` Scott Wood
2010-10-11 11:32                                 ` Detlev Zundel
2010-10-11 13:22                                   ` Balau
2010-10-04 10:57       ` Vipin Kumar
2010-10-04 12:04         ` Wolfgang Denk

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=4CA9CC04.5020800@emk-elektronik.de \
    --to=u-boot@emk-elektronik.de \
    --cc=u-boot@lists.denx.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