public inbox for u-boot@lists.denx.de
 help / color / mirror / Atom feed
From: Detlev Zundel <dzu@denx.de>
To: u-boot@lists.denx.de
Subject: [U-Boot] packed attribute problem
Date: Thu, 07 Oct 2010 11:59:07 +0200	[thread overview]
Message-ID: <m2k4lujpck.fsf@ohwell.denx.de> (raw)
In-Reply-To: <20101005130359.432f7038@udp111988uds.am.freescale.net> (Scott Wood's message of "Tue, 5 Oct 2010 13:03:59 -0500")

Hi Scott,

> On Tue, 5 Oct 2010 13:43:01 +0200
> Detlev Zundel <dzu@denx.de> wrote:
>
>> Hi Reinhard,
>> 
>> [...]
>> 
>> >> 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!
>> 
>> I cannot follow why you think this is sick - actually I think this is
>> clever.  GCC "knows" that abc points to a packed struct and it has no
>> clue on actual values, so it _has to assume_ it is not aligned.
>
> So instead of the code breaking in the obvious situation where the
> unaligned address is assigned to a pointer type that implies alignment,
> it breaks when sufficient barriers to optimization are added that the
> compiler can no longer keep track of where the value came from?

This is getting somewhat theoretical here, but I do not believe that "a
pointer type implies alignment" in C.  I may be wrong here, but I cannot
think of any statement in the standards in this regard - it's only data
types that imply alignments.  So if this is true, then for pointers gcc
will always have to reason from actual values assigned to them.

> Seems like the right thing is for GCC to warn about the assignment to a
> plain "u32 *", and allow a warningless assignment to something like
> "u32 __attribute__((packed)) *".

Somehow I still feel that gcc should _always_ generate 32-bit accesses
when I dereference a u32 pointer in code.  Silently substituting 4 byte
accesses is simply not the right thing to do (maybe with some
-i-know-what-i-am-doing option, but certainly not by default).  

If such an access traps the machine, then so be it.  Then the code has
to be changed to reflect this in the source code level which will _make
programmers ware of it_.  Doing such a "silent fixup" which extends to
other situations thereby changing general semantics of the C language is
not a good thing IMHO.

Cheers
  Detlev

-- 
Modern technique has made it possible for leisure, within limits, to be not
the prerogative of small privileged classes, but a right evenly distributed
throughout the community.  The morality of  work is the morality of slaves,
and the modern world has no need of slavery.            -- Bertrand Russell
--
DENX Software Engineering GmbH,      MD: Wolfgang Denk & Detlev Zundel
HRB 165235 Munich,  Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-40 Fax: (+49)-8142-66989-80 Email: dzu at denx.de

  reply	other threads:[~2010-10-07  9:59 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
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 [this message]
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=m2k4lujpck.fsf@ohwell.denx.de \
    --to=dzu@denx.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