public inbox for crossgcc@sourceware.org
 help / color / mirror / Atom feed
* ARM and padding
@ 2000-07-18  1:01 jens.ohlund
  2000-07-18  1:36 ` Philip Blundell
  2000-07-18  2:38 ` Luke Diamand
  0 siblings, 2 replies; 6+ messages in thread
From: jens.ohlund @ 2000-07-18  1:01 UTC (permalink / raw)
  To: crossgcc

Hi !

My problem is the following:
I have an ARM Evaluation Board-1. Rev B. I receive data from a serial line.
this data is placed in a linear buffer, and then i typecast to a struct of some kind.

This data which i receive I cannot control, and have therefore to work with it as is.

The structs contains other structs and so on and in the end it's only 16 or 8 bits values a extract.
BUT these values are not aligned, and when I typecast I get the wrong values, since the buffer is "packed".

How do I tell the compiler that the structs are to be packed ?

I'm developing on NT4, using cygwin and gcc-2.95.2 (arm-elf-gcc).

yours
Jens Ohlund


------
Want more information?  See the CrossGCC FAQ, http://www.objsw.com/CrossGCC/
Want to unsubscribe? Send a note to crossgcc-unsubscribe@sourceware.cygnus.com

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

* Re: ARM and padding
  2000-07-18  1:01 ARM and padding jens.ohlund
@ 2000-07-18  1:36 ` Philip Blundell
  2000-07-18  2:38 ` Luke Diamand
  1 sibling, 0 replies; 6+ messages in thread
From: Philip Blundell @ 2000-07-18  1:36 UTC (permalink / raw)
  To: jens.ohlund; +Cc: crossgcc

>How do I tell the compiler that the structs are to be packed ?

__attribute__ (( packed )).  See the gcc manual.

p.



------
Want more information?  See the CrossGCC FAQ, http://www.objsw.com/CrossGCC/
Want to unsubscribe? Send a note to crossgcc-unsubscribe@sourceware.cygnus.com

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

* Re: ARM and padding
  2000-07-18  1:01 ARM and padding jens.ohlund
  2000-07-18  1:36 ` Philip Blundell
@ 2000-07-18  2:38 ` Luke Diamand
  1 sibling, 0 replies; 6+ messages in thread
From: Luke Diamand @ 2000-07-18  2:38 UTC (permalink / raw)
  To: jens.ohlund; +Cc: crossgcc

Looking at your structure, and noting that the data is not word aligned
in the first place, I suspect you may struggle to get this approach to
work - won't the ARM end up totally garbling the word access to the int
field three quarters of the time?

You might prefer to use direct byte reads and manipulations - e.g.

inline int getRemainingBytes( unsigned char *bytes ) {
    unsigned char *ptr = bytes+251;
    return ptr[0]+(ptr[1]<<8)+(ptr[2]<<16)+(ptr[3]<<24);  // or
something like that
}

This also has the virtue that you can cope better if you get endian
blues.

Just my 2c.


jens.ohlund@secrc.abb.se wrote:
> 
> Hi !
> 
> My problem is the following:
> I have an ARM Evaluation Board-1. Rev B. I receive data from a serial line.
> this data is placed in a linear buffer, and then i typecast to a struct of some kind.
> 
> This data which i receive I cannot control, and have therefore to work with it as is.
> 
> The structs contains other structs and so on and in the end it's only 16 or 8 bits values a extract.
> BUT these values are not aligned, and when I typecast I get the wrong values, since the buffer is "packed".
> 
> How do I tell the compiler that the structs are to be packed ?
> 
> I'm developing on NT4, using cygwin and gcc-2.95.2 (arm-elf-gcc).
> 
> yours
> Jens Ohlund
> 
> ------
> Want more information?  See the CrossGCC FAQ, http://www.objsw.com/CrossGCC/
> Want to unsubscribe? Send a note to crossgcc-unsubscribe@sourceware.cygnus.com

-- 
Virata http://www.virata.com
Cambridge
Tel: +44 1223 566919      Fax: +44 1223 566915

------
Want more information?  See the CrossGCC FAQ, http://www.objsw.com/CrossGCC/
Want to unsubscribe? Send a note to crossgcc-unsubscribe@sourceware.cygnus.com

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

* RE: ARM and padding
@ 2000-07-19  2:12 Ilya Vershkov
  0 siblings, 0 replies; 6+ messages in thread
From: Ilya Vershkov @ 2000-07-19  2:12 UTC (permalink / raw)
  To: 'jens.ohlund@secrc.abb.se', crossgcc

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 2309 bytes --]

Title: RE: ARM and padding





Do you use C or C++?
We had some problems with structure alignment in C++ code


 ## -----Original Message-----
 ## From: jens.ohlund@secrc.abb.se [ mailto:jens.ohlund@secrc.abb.se ]
 ## Sent: 18 July 2000 11:14
 ## To: crossgcc@sources.redhat.com
 ## Subject: Re: ARM and padding
 ## 
 ## 
 ## Hi !
 ## 
 ## It doesn't help. I've made a struct like this:
 ## struct
 ## {
 ##   unsigned char buf[250] __attribute__ ((packed));
 ##   unsigned char currPos __attribute__ ((packed));
 ##   unsigned int  remainingBytes __attribute__ ((packed));
 ##   unsigned char state __attribute__ ((packed));
 ## } __attribute__ ((packed)) lowRx;
 ## 
 ## And still, it's on the adresses:
 ## &buf[] = 0x241E0
 ## &currPos = 0x242DA
 ## &remainingBytes = 0x242DC
 ## &state = 0x242DF
 ## 
 ## Which makes the sizes:
 ## buf -> currPos =  250
 ## currPos -> remainingBytes = 2 !!!!!
 ## remainingBytes -> state = 4
 ## 
 ## currPos -> remainingBytes should only be 1.
 ## 
 ## This is just one struct out of many, with the same problem. 
 ## I can change Some of them, as this example. But not many compared to
 ## amount of troublesome structs.
 ## 
 ## yours
 ## Jens
 ## 
 ## 
 ## 
 ## 
 ## crossgcc-owner@sources.redhat.com
 ## 2000-07-18 10:38
 ## 
 ## 
 ## To:   jens.ohlund@secrc.abb.se
 ## cc:   crossgcc@sourceware.cygnus.com
 ## Subject:  Re: ARM and padding
 ## 
 ## Security Level:?         Internal
 ## 
 ## 
 ## >How do I tell the compiler that the structs are to be packed ?
 ## 
 ## __attribute__ (( packed )).  See the gcc manual.
 ## 
 ## p.
 ## 
 ## 
 ## 
 ## ------
 ## Want more information?  See the CrossGCC FAQ, 
 ## http://www.objsw.com/CrossGCC/
 ## Want to unsubscribe? Send a note to 
 ## crossgcc-unsubscribe@sourceware.cygnus.com
 ## 
 ## 
 ## 
 ## 
 ## 
 ## 
 ## ------
 ## Want more information?  See the CrossGCC FAQ, 
 ## http://www.objsw.com/CrossGCC/
 ## Want to unsubscribe? Send a note to 
 ## crossgcc-unsubscribe@sourceware.cygnus.com
 ## 




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

* Re: ARM and padding
  2000-07-18  2:14 jens.ohlund
@ 2000-07-18  2:28 ` Ola Liljedahl
  0 siblings, 0 replies; 6+ messages in thread
From: Ola Liljedahl @ 2000-07-18  2:28 UTC (permalink / raw)
  To: jens.ohlund; +Cc: crossgcc

You are transferring a binary structure from one architecture to another
using the compiler's struct layout as the format of the packet?!?

Don't expect this to work more than in some rare instance (depending on
the involved processor architectures and compilers).

You need some more protocol layer to get this communication reliable. Or
some packet format which is inherently more portable.
--
	Ola Liljedahl

jens.ohlund@secrc.abb.se wrote:
> 
> Hi !
> 
> It doesn't help. I've made a struct like this:
> struct
> {
>   unsigned char buf[250] __attribute__ ((packed));
>   unsigned char currPos __attribute__ ((packed));
>   unsigned int  remainingBytes __attribute__ ((packed));
>   unsigned char state __attribute__ ((packed));
> } __attribute__ ((packed)) lowRx;
> 
> And still, it's on the adresses:
> &buf[] = 0x241E0
> &currPos = 0x242DA
> &remainingBytes = 0x242DC
> &state = 0x242DF
> 
> Which makes the sizes:
> buf -> currPos =  250
> currPos -> remainingBytes = 2 !!!!!
> remainingBytes -> state = 4
> 
> currPos -> remainingBytes should only be 1.
> 
> This is just one struct out of many, with the same problem. I can change Some of them, as this example. But not many compared to
> amount of troublesome structs.
> 
> yours
> Jens
> 
> crossgcc-owner@sources.redhat.com
> 2000-07-18 10:38
> 
> To:   jens.ohlund@secrc.abb.se
> cc:   crossgcc@sourceware.cygnus.com
> Subject:  Re: ARM and padding
> 
> Security Level:?         Internal
> 
> >How do I tell the compiler that the structs are to be packed ?
> 
> __attribute__ (( packed )).  See the gcc manual.
> 
> p.
> 
> ------
> Want more information?  See the CrossGCC FAQ, http://www.objsw.com/CrossGCC/
> Want to unsubscribe? Send a note to crossgcc-unsubscribe@sourceware.cygnus.com
> 
> ------
> Want more information?  See the CrossGCC FAQ, http://www.objsw.com/CrossGCC/
> Want to unsubscribe? Send a note to crossgcc-unsubscribe@sourceware.cygnus.com

------
Want more information?  See the CrossGCC FAQ, http://www.objsw.com/CrossGCC/
Want to unsubscribe? Send a note to crossgcc-unsubscribe@sourceware.cygnus.com

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

* Re: ARM and padding
@ 2000-07-18  2:14 jens.ohlund
  2000-07-18  2:28 ` Ola Liljedahl
  0 siblings, 1 reply; 6+ messages in thread
From: jens.ohlund @ 2000-07-18  2:14 UTC (permalink / raw)
  To: crossgcc

Hi !

It doesn't help. I've made a struct like this:
struct
{
  unsigned char buf[250] __attribute__ ((packed));
  unsigned char currPos __attribute__ ((packed));
  unsigned int  remainingBytes __attribute__ ((packed));
  unsigned char state __attribute__ ((packed));
} __attribute__ ((packed)) lowRx;

And still, it's on the adresses:
&buf[] = 0x241E0
&currPos = 0x242DA
&remainingBytes = 0x242DC
&state = 0x242DF

Which makes the sizes:
buf -> currPos =  250
currPos -> remainingBytes = 2 !!!!!
remainingBytes -> state = 4

currPos -> remainingBytes should only be 1.

This is just one struct out of many, with the same problem. I can change Some of them, as this example. But not many compared to
amount of troublesome structs.

yours
Jens




crossgcc-owner@sources.redhat.com
2000-07-18 10:38


To:   jens.ohlund@secrc.abb.se
cc:   crossgcc@sourceware.cygnus.com
Subject:  Re: ARM and padding

Security Level:?         Internal


>How do I tell the compiler that the structs are to be packed ?

__attribute__ (( packed )).  See the gcc manual.

p.



------
Want more information?  See the CrossGCC FAQ, http://www.objsw.com/CrossGCC/
Want to unsubscribe? Send a note to crossgcc-unsubscribe@sourceware.cygnus.com






------
Want more information?  See the CrossGCC FAQ, http://www.objsw.com/CrossGCC/
Want to unsubscribe? Send a note to crossgcc-unsubscribe@sourceware.cygnus.com

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

end of thread, other threads:[~2000-07-19  2:12 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2000-07-18  1:01 ARM and padding jens.ohlund
2000-07-18  1:36 ` Philip Blundell
2000-07-18  2:38 ` Luke Diamand
2000-07-18  2:14 jens.ohlund
2000-07-18  2:28 ` Ola Liljedahl
2000-07-19  2:12 Ilya Vershkov

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).