From owner-freebsd-arm@FreeBSD.ORG Tue Sep 3 12:51:29 2013 Return-Path: Delivered-To: freebsd-arm@FreeBSD.org Received: from mx1.freebsd.org (mx1.freebsd.org [8.8.178.115]) (using TLSv1 with cipher ADH-AES256-SHA (256/256 bits)) (No client certificate requested) by hub.freebsd.org (Postfix) with ESMTP id 577942F8; Tue, 3 Sep 2013 12:51:29 +0000 (UTC) (envelope-from ian@FreeBSD.org) Received: from mho-02-ewr.mailhop.org (mho-02-ewr.mailhop.org [204.13.248.72]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.freebsd.org (Postfix) with ESMTPS id 2D9352416; Tue, 3 Sep 2013 12:51:28 +0000 (UTC) Received: from c-24-8-230-52.hsd1.co.comcast.net ([24.8.230.52] helo=damnhippie.dyndns.org) by mho-02-ewr.mailhop.org with esmtpsa (TLSv1:AES256-SHA:256) (Exim 4.72) (envelope-from ) id 1VGq4o-000FCz-MW; Tue, 03 Sep 2013 12:51:22 +0000 Received: from [172.22.42.240] (revolution.hippie.lan [172.22.42.240]) by damnhippie.dyndns.org (8.14.3/8.14.3) with ESMTP id r83CpJxt068579; Tue, 3 Sep 2013 06:51:19 -0600 (MDT) (envelope-from ian@FreeBSD.org) X-Mail-Handler: Dyn Standard SMTP by Dyn X-Originating-IP: 24.8.230.52 X-Report-Abuse-To: abuse@dyndns.com (see http://www.dyndns.com/services/sendlabs/outbound_abuse.html for abuse reporting information) X-MHO-User: U2FsdGVkX1+ZckPwHiJFgEEoUFJwSyeo Subject: Re: A simple question about C... From: Ian Lepore To: Michael Tuexen In-Reply-To: References: Content-Type: text/plain; charset="us-ascii" Date: Tue, 03 Sep 2013 06:51:19 -0600 Message-ID: <1378212679.1111.366.camel@revolution.hippie.lan> Mime-Version: 1.0 X-Mailer: Evolution 2.32.1 FreeBSD GNOME Team Port Content-Transfer-Encoding: 7bit Cc: freebsd-arm@FreeBSD.org X-BeenThere: freebsd-arm@freebsd.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: Porting FreeBSD to the StrongARM Processor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 03 Sep 2013 12:51:29 -0000 On Tue, 2013-09-03 at 14:31 +0200, Michael Tuexen wrote: > Dear all, > > I'm trying to get a C program (packetdrill) running FreeBSD r254973 > on a Raspberry Pi. > Since tests are failing, I wrote the little C program: > > #include > #include > #include > > int > main(void) > { > uint8_t byte[] = {0x00, 0x01, 0x02, 0x03, > 0x04, 0x05, 0x06, 0x07, > 0x08, 0x09, 0x0a, 0x0b}; > uint32_t i, n, *p; > > for (i = 0; i <= 8; i++) { > p = (uint32_t *)(byte + i); > memcpy(&n, byte + i, sizeof(uint32_t)); > printf("p = %p, *p = %08x, n = %08x.\n", (void *)p, *p, n); > } > return (0); > } > > It produces the following output: > > p = 0xbfffec48, *p = 03020100, n = 03020100. > p = 0xbfffec49, *p = 00030201, n = 04030201. > p = 0xbfffec4a, *p = 01000302, n = 05040302. > p = 0xbfffec4b, *p = 02010003, n = 06050403. > p = 0xbfffec4c, *p = 07060504, n = 07060504. > p = 0xbfffec4d, *p = 04070605, n = 08070605. > p = 0xbfffec4e, *p = 05040706, n = 09080706. > p = 0xbfffec4f, *p = 06050407, n = 0a090807. > p = 0xbfffec50, *p = 0b0a0908, n = 0b0a0908. > > So everything is fine when reading the 4 byte long uint32_t on a 4-byte aligned > address. Results are strange (at least for me), when the address is not 4-byte > aligned. There is no difference between clang and gcc. Can I only dereference > properly aligned pointers? Is the above expected or is it a bug in the compiler? > > Best regards > Michael Yes, you can only safely dereference pointers aligned to the size of the dereference (2, 4, 8 bytes). On some ARM platforms a misaligned reference leads to alignment fault, on others the data is rotated in some predefined way, and on some it's just "undefined behavior." There are functions in endian.h to help with the alignment, in particular the leNNdec() and leNNenc() functions are designed to be alignment-agnostic. -- Ian