From owner-freebsd-alpha Thu Aug 2 16:38:20 2001 Delivered-To: freebsd-alpha@freebsd.org Received: from Awfulhak.org (gw.Awfulhak.org [217.204.245.18]) by hub.freebsd.org (Postfix) with ESMTP id 8592D37B401; Thu, 2 Aug 2001 16:38:10 -0700 (PDT) (envelope-from brian@Awfulhak.org) Received: from hak.lan.Awfulhak.org (root@hak.lan.Awfulhak.org [172.16.0.12]) by Awfulhak.org (8.11.4/8.11.4) with ESMTP id f72Nc7s17630; Fri, 3 Aug 2001 00:38:07 +0100 (BST) (envelope-from brian@lan.Awfulhak.org) Received: from hak.lan.Awfulhak.org (brian@localhost [127.0.0.1]) by hak.lan.Awfulhak.org (8.11.4/8.11.4) with ESMTP id f72Nc6u09427; Fri, 3 Aug 2001 00:38:06 +0100 (BST) (envelope-from brian@hak.lan.Awfulhak.org) Message-Id: <200108022338.f72Nc6u09427@hak.lan.Awfulhak.org> X-Mailer: exmh version 2.5 07/13/2001 with nmh-1.0.4 To: Bruce Evans Cc: Julian Elischer , Brian Somers , Thomas Pornin , freebsd-net@FreeBSD.ORG, freebsd-alpha@FreeBSD.ORG, Brian Somers , FreeBSD-gnats-submit@FreeBSD.ORG, brian@freebsd-services.com Subject: Re: kern/27767: (was: PPPoE + Alpha + 32/64 bits) In-Reply-To: Message from Bruce Evans of "Fri, 03 Aug 2001 06:48:54 +1000." <20010803054935.M2890-100000@besplex.bde.org> MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <9420.996795486.1@hak.lan.Awfulhak.org> Date: Fri, 03 Aug 2001 00:38:06 +0100 From: Brian Somers Sender: owner-freebsd-alpha@FreeBSD.ORG Precedence: bulk List-ID: List-Archive: (Web Archive) List-Help: (List Instructions) List-Subscribe: List-Unsubscribe: X-Loop: FreeBSD.org > `__attribute ((packed))' is a syntax error in Standard C. > > Dependencies on compiler features are supposed to be centralized in > places like . Many invocations of __attribute__(()) are > placed there. This is not possible for the `packed' attribute because > this attribute has non-cosmetic effects. Structs must be carefully > laid out to give some chance of them not having any internal padding > for any compiler. This sometimes involves using struct members with > an unnatural type. has many examples. > > Here I don't see how packing the struct helps on alphas. Doesn't it > cause traps by misaligning uniqtag.data.pointer? I think you need > something like: It doesn't cause a trap as the compiler is (now) smart enough to handle it properly. Note where we have the alignment problem here: beast:~ $ cat x.c #include struct s { short x, y; }; union u { char c[sizeof(void*)]; void *v; }; struct x { struct s s; union u u; } __attribute ((packed)); int main() { struct x x; char c[] = "hello"; int i; x.u.v = c; /* This is ok despite it not being aligned */ i = (char *)&x.u.v - (char *)&x; printf("Got \"%s\", offset %d\n", (char *)x.u.v, i); *(void **)((char *)&x + 4) = c; /* This is not ok */ printf("Without knowledge of packed... \"%s\"\n", (char *)x.u.v); return 0; } beast:~ $ cc -o x x.c beast:~ $ ./x Got "hello", offset 4 pid 30842 (x): unaligned access: va=0x11ffba64 pc=0x120000894 ra=0x120000884 op=stq Without knowledge of packed... "hello" > struct { > struct pppoe_tag hdr; > char data[sizeof(void *)]; > } uniqtag; > void *pointer; > ... > pointer = sp; > memcpy(uniqtag.data, &pointer, sizeof(uniqtag.data)); That's probably better. I went with the __attribute((packed)) stuff because it was already done that way in the rest of the code. In this specific case however, it may be best to just send the variable plus the alignment padding as the tag: struct { struct pppoe_tag hdr; union uniq data; } uniqtag; ...... uniqtag.hdr.tag_type = PTT_HOST_UNIQ; uniqtag.hdr.tag_len = htons((u_int16_t)(sizeof(uniqtag) - sizeof(uniqtag.hdr)); uniqtag.data.pointer = sp; But I'm not convinced it gains anything given the other uses of __attribute((packed)) in this code. > Bruce -- Brian http://www.freebsd-services.com/ Don't _EVER_ lose your sense of humour ! To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-alpha" in the body of the message