Discussion:
[9fans] am I nuts? does 8c support packed structs?
(too old to reply)
Ronald G. Minnich
2004-10-22 15:27:03 UTC
Permalink
consider this:

struct a {
ulong x;
long y;
short z;
};

in GCC, you can add attribute packed and that struct will be 10 bytes. I'm
getting some static from the Xen guys because IIRC 8c does not support
packed structs -- that struct on 8c is 12 bytes.

Am I nuts? Is there some 8c pragma that can force this to be packed?

ron
C H Forsyth
2004-10-22 15:43:52 UTC
Permalink
hj were only dicks on the mips it seems, but it's easy to add it to 8c.

diff /n/dump/2004/1022/sys/src/cmd/8c/swt.c /sys/src/cmd/8c/swt.c
589a590,591
if(hjdickflg)
w = hjdickflg;
597a600,601
if(hjdickflg)
w = hjdickflg;
t.c:

#include <u.h>
#pragma hjdicks dick
struct a {
ulong x;
long y;
short z;
};
#pragma hjdicks off
static int xxx = sizeof(struct a);
static struct a xx[2];
static int xxsize = sizeof(xx);

8c -S t.c

DATA xxx<>+0(SB)/4,$10
DATA xxsize<>+0(SB)/4,$20
GLOBL xxsize<>+0(SB),$4
GLOBL xx<>+0(SB),$20
GLOBL xxx<>+0(SB),$4

perhaps i should add "xen" as a synonym for "dick"
"on" will work too, if you'd like to be less rude.

also,
#pragma hjdicks xN
for some integer N sets the padding/rounding to N
Ronald G. Minnich
2004-10-22 16:38:04 UTC
Permalink
Post by C H Forsyth
hj were only dicks on the mips it seems, but it's easy to add it to 8c.
but it's not there now, right?

and I have to know, what's the whole hj thing?

Is the name hjdicks a pejorative way of making a comment about packed
structs :-)

ron
C H Forsyth
2004-10-22 17:21:56 UTC
Permalink
Post by Ronald G. Minnich
but it's not there now, right?
if you apply the diffs i gave in the email to the version of /sys/src/cmd/8c/swt.c on sources, against
which i had diffed, and recompile your copy, you will (should) then have it.
Post by Ronald G. Minnich
and I have to know, what's the whole hj thing?
it's somewhere in the 9fans archive, but H&J (Harris & Jeffries i think) did protocol implementations
that relied on that effect extensively. oh here it is:

...
From: "Bruce Ellis" <***@chunder.com>
Date: Wed, 7 May 2003 22:47:40 +1000

pragmatism and a silly discussion over dinner.
hj were dicks for assuming that they could
lay out structs in an architecture independant way.
we had to live with it.
Bruce Ellis
2004-10-22 21:06:40 UTC
Permalink
hj weren't the most egregious dicks, but yeah it was a hack
(that saved months of work). just don't tell jon stewart
about them! i was particularly impressed by some protocol
code which not only used packed structs with glee but managed
to byte swap headers twice, as it had been endian ported
twice by different monkeys. babel-fish this!

brucee
Charles Forsyth
2004-10-22 21:08:43 UTC
Permalink
Post by Bruce Ellis
hj weren't the most egregious dicks, but yeah it was a hack
(that saved months of work).
it's actually a reasonable example of the convenience of having fairly
complete control of the tools!
Douglas A. Gwyn
2004-10-25 10:25:59 UTC
Permalink
Post by Charles Forsyth
it's actually a reasonable example of the convenience of having fairly
complete control of the tools!
One needs to watch out, though. Usually structures are
padded a particular way for a reason (due to machine
architecture), and merely making the alignment less
strict without doing anything else can have adverse
consequences ranging from generating some incorrect
code (misaligned access) to "merely" slowing down
nearly every use of structures (not aligned with cache
lines).

You should beware of using code written by people who
are oblivious to the structure member padding issue.
Odds are that they have made other mistakes as well,
and in Internet server code that means opportunities
for hackers to attack your system.

Ronald G. Minnich
2004-10-22 17:21:08 UTC
Permalink
Post by C H Forsyth
hj were only dicks on the mips it seems, but it's easy to add it to 8c.
Here's a test of dicks on 8c today:


#include <u.h>
#include <libc.h>

#pragma hjdicks on
struct a {unsigned long x; char c; };
#pragma hjdicks off

struct a a;

main(){
print("size %d\n", sizeof(a));
}


This yields 8, with dicks on or off.

In gcc, for this:

struct a {unsigned long x; char c; } __attribute__ ((packed));

struct a *a = 0;

main(){
printf("size %d, addr %p\n", sizeof(*a), &a->c);
}

you get a size of 5.


"How do you create TCP headers without packed structs?" is one question I
got. I guess Plan 9 is quite possibly the "here's how" except of course
everyone did this before packed structs (or even gcc, or even before C
compilers that aligned structs and struct members for that matter -- was
there a C compiler before gcc? one wonders. The mind boggles).

Unless there's another 8c pragma I have not heard of, I'll stuck to
marshalling data by hand.

ron
j***@plan9.bell-labs.com
2004-10-22 17:29:53 UTC
Permalink
Post by Ronald G. Minnich
...
"How do you create TCP headers without packed structs?" is one question I
got. I guess Plan 9 is quite possibly the "here's how" except of course
everyone did this before packed structs (or even gcc, or even before C
compilers that aligned structs and struct members for that matter -- was
there a C compiler before gcc? one wonders. The mind boggles).
Unless there's another 8c pragma I have not heard of, I'll stuck to
marshalling data by hand.
ron
Next you'll tell me they use bitfields in packed structs.
Ronald G. Minnich
2004-10-22 17:31:52 UTC
Permalink
Post by j***@plan9.bell-labs.com
Next you'll tell me they use bitfields in packed structs.
hey man, I'll keep you posted.

ron
Brantley Coile
2004-10-22 17:52:23 UTC
Permalink
At a prior job I was in charge of the embedded kernel for a piece of
telco gear. Another group was writing the application and doing so on
Sun Motorola boxes. I warned them that my kernel would be aligning
structures to 32 bits, not the 16 bits that Sun OS was forced to use
because of all the SunOS structures that assumed they overlayed
protocol headers. My warning had the effect of causing the
application developers to write a bunch of code to marshal things into
and out of their inter-machine messages. But somehow they still
managed to screw it up and had to spend two weeks fixing their code
that some how still depended on 16=bit structure alignment.

I assume Sun fixed their code when the Sparc arrived.

Brantley
C H Forsyth
2004-10-22 18:27:05 UTC
Permalink
Post by Ronald G. Minnich
Unless there's another 8c pragma I have not heard of, I'll stuck to
marshalling data by hand.
perhaps the pickling code might help, though if it's only a little
i shouldn't bother.
Charles Forsyth
2004-10-22 18:22:25 UTC
Permalink
Post by Ronald G. Minnich
This yields 8, with dicks on or off.
i get 5 with the changes i posted (which should be the same
as russ's updates, except for the change of name).
Douglas A. Gwyn
2004-10-25 10:25:47 UTC
Permalink
Post by Ronald G. Minnich
Unless there's another 8c pragma I have not heard of, I'll stuck to
marshalling data by hand.
That "marshalling" is of course the correct approach
all along.
boyd, rounin
2004-10-22 19:30:09 UTC
Permalink
Post by Ronald G. Minnich
"How do you create TCP headers without packed structs?" is one question I
got.
iirc tcp is pretty much 16 bit aligned. failing that you can do 32 bit tricks.
Ronald G. Minnich
2004-10-22 19:35:59 UTC
Permalink
Post by boyd, rounin
iirc tcp is pretty much 16 bit aligned. failing that you can do 32 bit tricks.
yeah but the point the person was trying to make, which surprised me, was
that in this New Era it is seen to be impossible to create correct tcp
headers without __packed__.

ron
boyd, rounin
2004-10-22 19:42:48 UTC
Permalink
Post by Ronald G. Minnich
yeah but the point the person was trying to make, which surprised me, was
that in this New Era it is seen to be impossible to create correct tcp
headers without __packed__.
who cares? we have 64 bit processors now :(

idiots.
Ronald G. Minnich
2004-10-22 17:28:03 UTC
Permalink
#pragma pack on
no change in that program I sent. Still prints 8 for the size.
Interesting, maybe the feature rotted away due to it being unloved :-)

No big deal, I'm going to ignore it. I come down on the side of not using
it anyway, and not worrying about what gcc and linux guys thing.

ron
Ronald G. Minnich
2004-10-22 17:31:14 UTC
Permalink
you should do this anyway.
agreed. Now if I could convince the xen guys, but that's another story.

ron
Loading...