Date: Tue, 28 Aug 2012 18:10:31 +0400 From: Andrey Zonov <zont@FreeBSD.org> To: Konstantin Belousov <kostikbel@gmail.com> Cc: freebsd-arch@freebsd.org Subject: Re: warning: cast increases required alignment of target type Message-ID: <503CD157.50306@FreeBSD.org> In-Reply-To: <20120828140646.GP33100@deviant.kiev.zoral.com.ua> References: <503C8809.3050507@FreeBSD.org> <CDF0AE44-1CB2-42F7-BE60-DD2E4697A374@bsdimp.com> <503CCF6E.7080208@FreeBSD.org> <20120828140646.GP33100@deviant.kiev.zoral.com.ua>
index | next in thread | previous in thread | raw e-mail
[-- Attachment #1 --] On 8/28/12 6:06 PM, Konstantin Belousov wrote: > On Tue, Aug 28, 2012 at 06:02:22PM +0400, Andrey Zonov wrote: >> On 8/28/12 5:12 PM, Warner Losh wrote: >>> >>> On Aug 28, 2012, at 2:57 AM, Andrey Zonov wrote: >>> >>>> Hi, >>>> >>>> Does anyone know how to correctly fix this warning for >>>> arm/ia64/mips/sparc64? >>>> >>>> usr.bin/elf2aout/elf2aout.c: In function 'main': >>>> usr.bin/elf2aout/elf2aout.c:129: warning: cast increases required >>>> alignment of target type >>>> >>>> I found this explanation from bde, but still don't understand how to >>>> correctly fix this issue. >>>> >>>> ------------------------------------------------------------------------ >>>> r99799 | bde | 2002-07-11 22:06:09 +0400 (Thu, 11 Jul 2002) | 10 lines >>>> >>>> Set NO_WERROR to ignore the following warning which is emitted on >>>> alphas: >>>> .../elf2aout.c:130: warning: cast increases required alignment of >>>> target type >>>> The warning is about casting ((char *)e + phoff) to a struct pointer, >>>> where e is aligned but phoff might be garbage, so I think the warning >>>> should be emitted on most machines (even on i386's, alignment checking >>>> might be on) and the correct fix would involve validation phoff before >>>> using it. >>>> >>>> Is this fix correct? >>> >>> No. You need to tell the compiler that e has the alignment you think it has so that it can check to make sure that you are actually right. Just casting like this defeats the purpose of the check and will break on other architectures. >>> >>> Warner >>> >> >> What do you say about this one? >> >> Index: usr.bin/elf2aout/elf2aout.c >> =================================================================== >> --- usr.bin/elf2aout/elf2aout.c (revision 239611) >> +++ usr.bin/elf2aout/elf2aout.c (working copy) >> @@ -126,7 +126,7 @@ >> entry = xe64toh(e->e_entry); >> phoff = xe64toh(e->e_phoff); >> phnum = xe16toh(e->e_phnum); >> - p = (Elf64_Phdr *)((char *)e + phoff); >> + p = (Elf64_Phdr *)(e + phoff); > This is plain wrong. Pointer arithmetic steps over the object's > count, not the char count. You end up incrementing the pointer by > sizeof(Elf64_Ehdr) * phoff. > Ooops, you are totally right. -- Andrey Zonov [-- Attachment #2 --] -----BEGIN PGP SIGNATURE----- Version: GnuPG/MacGPG2 v2.0.18 (Darwin) Comment: GPGTools - http://gpgtools.org iQEcBAEBAgAGBQJQPNFXAAoJEBWLemxX/CvTyNMH/07DQY9SEqLzWCicIpF5l8Lo 5S97NSBUee97EMaBeI+vs3oWNp2tpl8klcil6GKg5731rhfWG7uwMToV8wBZL1jW fo2xdyUZjV2WgEw0s88uBjXLGhjmYtGu5aMBqTDjeszF+at47IbdGjoAO754GYfe 90gNj8NZyJz9o2xBeyeh7jr8bLylx7OlW9FNLRp7cap1kE3kVe3SG4Uo3CPxdn0I AfZ2bBNhHq0IKBfF++YKMtqCQWTrFAxumUiBy/eNcgGo5LlYDX3g9Oqivm7oDiy8 KY4VIF0Fc/uNeojaKwLJOp2nRy157q9jIRJfYDcmv3oam3gJfEwFok15gFyStW4= =k3PF -----END PGP SIGNATURE-----home | help
Want to link to this message? Use this
URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?503CD157.50306>
