From: Chen Gang <gang.chen.5i5j@gmail.com>
To: James Hogan <james.hogan@imgtec.com>
Cc: linux-metag@vger.kernel.org,
"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>
Subject: Re: [Suggest] arch: metag: compiler: Are they compiler's issues?
Date: Sat, 11 Jan 2014 00:20:14 +0800 [thread overview]
Message-ID: <52D01DBE.6010205@gmail.com> (raw)
In-Reply-To: <52D019A2.9000705@imgtec.com>
On 01/11/2014 12:02 AM, James Hogan wrote:
> On 10/01/14 15:57, Chen Gang wrote:
>> On 01/08/2014 11:01 PM, Chen Gang wrote:
>>> On 01/06/2014 06:31 PM, James Hogan wrote:
>>>> I suspect this is due to bad assumptions in the code. The metag ABI is
>>>> unusual in padding the size of structs to a 32bit boundary even if all
>>>> members are <32bit. This is actually permitted by the C standard but
>>>> it's a bit of a pain. e.g.
>>>>
>>>> struct s {
>>>> short x
>>>> struct {
>>>> short x[3];
>>>> } y;
>>>> short z;
>>>> };
>>>>
>>>> on x86
>>>> alignof(s::y) == 2
>>>> s::y at offset 2
>>>> sizeof(s::y) == 6
>>>> s::z at offset 6+2 = 8
>>>> sizeof(struct s) == 10
>>>>
>>>> but on metag
>>>> alignof(s::y) == 4
>>>> s::y at offset 4
>>>> sizeof(s::y) == 8 (padding, this is what catches people out)
>>>> s::z at offset 4+8 = 12
>>>> sizeof(struct s) == 16 (and here too)
>>>>
>>>> Adding packed attribute on outer struct reduces sizeof(struct s) to 12
>>>> on metag:
>>>> alignof(s::y) == 4
>>>> s::y at offset 2 (packed)
>>>> sizeof(s::y) == 8 (still padded)
>>>
>>> In my memory, when packed(2), it breaks the C standard (although I am
>>> not quit sure).
>>>
>>> And I guess, all C programmers will assume it will be 6 when within
>>> pack(2) or pack(1).
>>>
>>>> s::z at offset 2+8 = 10
>>>> sizeof(struct s) == 12 (packed)
>>>>
>>>> Also reduced to 12 if only inner struct is marked packed:
>>>> alignof(s::y) == 2
>>>> s::y at offset 2
>>>> sizeof(s::y) == 6 (packed)
>>>> s::z at offset 2+6 = 8
>>>> sizeof(struct s) == 12 (still padded)
>>>>
>>>> Adding packed attribute on both outer and inner struct reduces
>>>> sizeof(struct s) to 10 to match x86.
>>>>
>>>> Unfortunately it's years too late to change this ABI, so we're stuck
>>>> with it.
>>>>
>>>
>>> Unfortunately too, most using cases are related with API (the related
>>> structure definition must be the same in binary data).
>>>
>>> I am sure there are still another ways to bypass this issue, but that
>>> will make the code looks very strange (especially they are API).
>>>
>>> :-(
>>>
>>
>> I guess most C programmers will use this way to describe protocol/data
>> format, and keep compatible for it (since it is API).
>>
>> So even if it really does not break C standard, I still recommend our
>> compiler to improve itself to support this features.
>
> The compiler cannot change this without breaking the ABI.
>
> If the structure describes a set-in-stone data layout (which it sounds
> like it does since it asserts the size of it) then the correct fix is to
> pack the structures in such a way as to guarantee the correct offsets
> and sizes on all compliant compilers. Otherwise if it's just an internal
> programming API it shouldn't be using compile time asserts to enforce
> things that vary between ABIs.
>
OK, thanks, I guess your meaning is:
struct s {
short x;
struct {
short x[3];
} y __attribute__ ((packed));
short z;
} __attribute__ ((packed));
That will satisfy all of compilers (include metag), is it correct?
Thanks.
--
Chen Gang
Open, share and attitude like air, water and life which God blessed
next prev parent reply other threads:[~2014-01-10 16:20 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2013-12-24 2:36 [Suggest] arch: metag: compiler: Are they compiler's issues? Chen Gang
2013-12-24 3:25 ` Chen Gang
2013-12-24 3:26 ` Chen Gang
2014-01-06 10:31 ` James Hogan
2014-01-08 15:01 ` Chen Gang
2014-01-10 15:57 ` Chen Gang
2014-01-10 16:02 ` James Hogan
2014-01-10 16:20 ` Chen Gang [this message]
2014-01-10 16:30 ` James Hogan
2014-01-10 16:38 ` Chen Gang
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=52D01DBE.6010205@gmail.com \
--to=gang.chen.5i5j@gmail.com \
--cc=james.hogan@imgtec.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-metag@vger.kernel.org \
/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