public inbox for ecos-discuss@sourceware.org
 help / color / mirror / Atom feed
* [ECOS] How to stop gcc padding structs???
@ 2001-01-28 10:37 Grant Edwards
  2001-01-29  0:27 ` Andrew Lunn
                   ` (2 more replies)
  0 siblings, 3 replies; 7+ messages in thread
From: Grant Edwards @ 2001-01-28 10:37 UTC (permalink / raw)
  To: ecos-discuss

I'm again fighting with gcc trying (and failing) to get it to
stop putting padding bytes into structs.  Have any eCosians
figured out how to prevent gcc from padding structs?

I ran into this problem before and gave up, finally having to
use hand-calculated constants instead of "sizeof (struct foo)"
in numerous places.  For example, it's impossible to define an
Ethernet header structure that ends up having a size of 14
bytes!

In the following example, gcc insists that each of the "high"
structs occupies four bytes despite my putting a "packed"
attribute evryplace that doesn't generate a syntax warning.

typedef struct
{
  volatile unsigned char data __attribute__((packed));
  volatile unsigned char _xxx __attribute__((packed));
} high __attribute((packed));

typedef struct
{
  high  a __attribute__((packed));
  high  b __attribute__((packed));
  high  c __attribute__((packed));
} tDemo __attribute__((packed));

tDemo foo;

void *addr[] =
{
  &foo,
  &foo.a,
  &foo.b,
  &foo.c
};

testit:     file format elf32-bigarm
Disassembly of section .data:
00008000 <addr>:
    8000:	00008010 	andeq	r8, r0, r0, lsl r0
    8004:	00008010 	andeq	r8, r0, r0, lsl r0
    8008:	00008014 	andeq	r8, r0, r4, lsl r0
    800c:	00008018 	andeq	r8, r0, r8, lsl r0

-- 
Grant Edwards
grante@visi.com

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [ECOS] How to stop gcc padding structs???
  2001-01-28 10:37 [ECOS] How to stop gcc padding structs??? Grant Edwards
@ 2001-01-29  0:27 ` Andrew Lunn
  2001-01-29  7:24   ` Grant Edwards
  2001-01-29  9:49 ` Jonathan Larmour
  2001-01-29 14:15 ` Jesper Skov
  2 siblings, 1 reply; 7+ messages in thread
From: Andrew Lunn @ 2001-01-29  0:27 UTC (permalink / raw)
  To: Grant Edwards; +Cc: ecos-discuss

On Sun, Jan 28, 2001 at 12:40:59PM -0600, Grant Edwards wrote:
> 
> I'm again fighting with gcc trying (and failing) to get it to
> stop putting padding bytes into structs.  Have any eCosians
> figured out how to prevent gcc from padding structs?

Yes, but more by luck combined with trial and error than anything
else.  When you have this sort of structure in structre arrangement i
find it best to work upwards. Make sure sizeof(high) is realy 2. Then
make sure tDemo is 6 bytes. You may also have to play around with the
align attribute. I don't need it for my code, but you never know. 

Also i think someone once said this is target specific. It can be made
to work for the SA110, is all i can say.

        Andrew

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [ECOS] How to stop gcc padding structs???
  2001-01-29  0:27 ` Andrew Lunn
@ 2001-01-29  7:24   ` Grant Edwards
  0 siblings, 0 replies; 7+ messages in thread
From: Grant Edwards @ 2001-01-29  7:24 UTC (permalink / raw)
  To: ecos-discuss

On Mon, Jan 29, 2001 at 09:27:09AM +0100, Andrew Lunn wrote:
> On Sun, Jan 28, 2001 at 12:40:59PM -0600, Grant Edwards wrote:
> > 
> > I'm again fighting with gcc trying (and failing) to get it to
> > stop putting padding bytes into structs.  Have any eCosians
> > figured out how to prevent gcc from padding structs?
> 
> Yes, but more by luck combined with trial and error than anything
> else.  When you have this sort of structure in structre arrangement i
> find it best to work upwards. Make sure sizeof(high) is realy 2. 

It's 4, and nothing I try with __attribute__((packed)) will make it 2.
I'll mess with alignement attributes some.

-- 
Grant Edwards
grante@visi.com

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [ECOS] How to stop gcc padding structs???
  2001-01-28 10:37 [ECOS] How to stop gcc padding structs??? Grant Edwards
  2001-01-29  0:27 ` Andrew Lunn
@ 2001-01-29  9:49 ` Jonathan Larmour
  2001-01-29  9:53   ` Grant Edwards
  2001-01-29 14:15 ` Jesper Skov
  2 siblings, 1 reply; 7+ messages in thread
From: Jonathan Larmour @ 2001-01-29  9:49 UTC (permalink / raw)
  To: Grant Edwards; +Cc: ecos-discuss

Grant Edwards wrote:
> 
> I'm again fighting with gcc trying (and failing) to get it to
> stop putting padding bytes into structs.  Have any eCosians
> figured out how to prevent gcc from padding structs?
> 
> I ran into this problem before and gave up, finally having to
> use hand-calculated constants instead of "sizeof (struct foo)"
> in numerous places.  For example, it's impossible to define an
> Ethernet header structure that ends up having a size of 14
> bytes!
> 
> In the following example, gcc insists that each of the "high"
> structs occupies four bytes despite my putting a "packed"
> attribute evryplace that doesn't generate a syntax warning.

It's generally non-portable to rely on struct layout matching hardware, and
just because gcc can be coerced to do it doesn't make it wise, nor
guarantee it on different architectures (or different versions of gcc!). It
is better practice to use accessor macros instead.

And no, I know Red Hat don't always practice what we preach, but we
*should*.

Jifl
-- 
Red Hat, Rustat House, Clifton Road, Cambridge, UK. Tel: +44 (1223) 271062
Maybe this world is another planet's Hell -Aldous Huxley || Opinions==mine

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [ECOS] How to stop gcc padding structs???
  2001-01-29  9:49 ` Jonathan Larmour
@ 2001-01-29  9:53   ` Grant Edwards
  0 siblings, 0 replies; 7+ messages in thread
From: Grant Edwards @ 2001-01-29  9:53 UTC (permalink / raw)
  To: Jonathan Larmour; +Cc: ecos-discuss

On Mon, Jan 29, 2001 at 05:49:28PM +0000, Jonathan Larmour wrote:

> It's generally non-portable to rely on struct layout matching
> hardware, and just because gcc can be coerced to do it doesn't
> make it wise, nor guarantee it on different architectures (or
> different versions of gcc!). It is better practice to use
> accessor macros instead.
> 
> And no, I know Red Hat don't always practice what we preach,
> but we *should*.

I know, but debugging is a royal pain when GDB doesn't know
about the structure of the peripheral and you've got to work
off of hex memory dumps just like the bad old days.

-- 
Grant Edwards
grante@visi.com

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [ECOS] How to stop gcc padding structs???
  2001-01-28 10:37 [ECOS] How to stop gcc padding structs??? Grant Edwards
  2001-01-29  0:27 ` Andrew Lunn
  2001-01-29  9:49 ` Jonathan Larmour
@ 2001-01-29 14:15 ` Jesper Skov
  2 siblings, 0 replies; 7+ messages in thread
From: Jesper Skov @ 2001-01-29 14:15 UTC (permalink / raw)
  To: ecos-discuss


typedef struct
{
  volatile unsigned char data __attribute__((packed));
  volatile unsigned char _xxx __attribute__((packed));
} high __attribute((packed));

Replace with

typedef struct
{
  volatile unsigned char data;
  volatile unsigned char _xxx;
} __attribute__((packed)) high ;

and it should work.

Jesper

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [ECOS] How to stop gcc padding structs???
@ 2001-01-28 13:34 Lewin A.R.W. Edwards
  0 siblings, 0 replies; 7+ messages in thread
From: Lewin A.R.W. Edwards @ 2001-01-28 13:34 UTC (permalink / raw)
  To: ecos-discuss

>I'm again fighting with gcc trying (and failing) to get it to
>stop putting padding bytes into structs.  Have any eCosians
>figured out how to prevent gcc from padding structs?

I have had the same type of problem trying to get it working on a 
structure-by-structure basis and I am running with the entire OS compiled 
with packed structures.

Warning!!!! am not sure if it is caused by this act, but I have run into 
another problem since doing this, and it caused much head-scratching. It 
seems that if I pass a parameter by reference, and then pass that reference 
down into another function, the lower function gets a pointer to garbage 
rather than a pointer to the real value. So it can't properly work with or 
update the parameter.

The solution to this is to create a local variable in the upper function, 
and pass THAT down to the lower function, and copy the value across 
afterwards. But it's very annoying.

=== Lewin A.R.W. Edwards (Embedded Engineer)
Work: http://www.digi-frame.com/
Personal: http://www.zws.com/ and http://www.larwe.com/

"Und setzet ihr nicht das Leben ein,
Nie wird euch das Leben gewonnen sein."

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2001-01-29 14:15 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-01-28 10:37 [ECOS] How to stop gcc padding structs??? Grant Edwards
2001-01-29  0:27 ` Andrew Lunn
2001-01-29  7:24   ` Grant Edwards
2001-01-29  9:49 ` Jonathan Larmour
2001-01-29  9:53   ` Grant Edwards
2001-01-29 14:15 ` Jesper Skov
2001-01-28 13:34 Lewin A.R.W. Edwards

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).