I initially entered this bug in the GCC bug database, but the bug seems not to be in GCC, but probably in binutils. See GCC bug 29249: http://gcc.gnu.org/bugzilla/show_bug.cgi?id=29249 The problem is this: I have a small C++ program that requires an int to be aligned on a 16 byte boundary. When I compile and run this program with g++ version 4.1.1 and binutils version 2.17.50 on Cygwin, I get an alignment error (the int is not aligned on a 16 byte boundary). g++ outputs the correct assembler code, so the error must be in as or ld. This is the source code: // ----- begin source code #include <stdio.h> typedef int __attribute__ ((aligned (16))) aint; inline void function() { static const aint x = 123; printf("sizeof(x) = %d, __alignof__(x) = %d\n", sizeof(x), __alignof__(x)); unsigned long a = (unsigned long) &x; printf("Address of x: 0x%lX - %s\n", a, a & 15L ? "ALIGNMENT ERROR" : "ok"); } int main() { function(); return 0; } // ----- end source code I compile this using the following command: g++ tst.cpp -o tst The output when running the program is: Address of x: 0x40E3E8 - ALIGNMENT ERROR I will attach the assembler code produced by GCC.
Created attachment 1333 [details] Assember code produced by g++ This is the assembler code produced by g++ when compiling the source with: g++ tst.cpp -S Note that g++ correctly outputs a ".align 16" directive in the assembler source. However, the as and/or ld seems to ignore it??
Actually the output of running the above program is slightly different (the address is different), but still wrong: $ ./tst sizeof(x) = 4, __alignof__(x) = 16 Address of x: 0x40F418 - ALIGNMENT ERROR
$ as --version GNU assembler 2.17.50 20060817 Copyright 2005 Free Software Foundation, Inc. This program is free software; you may redistribute it under the terms of the GNU General Public License. This program has absolutely no warranty. This assembler was configured for a target of `i686-pc-cygwin'. $ ln --version ln (GNU coreutils) 5.97 Copyright (C) 2006 Free Software Foundation, Inc. This is free software. You may redistribute copies of it under the terms of the GNU General Public License <http://www.gnu.org/licenses/gpl.html>. There is NO WARRANTY, to the extent permitted by law. Written by Mike Parker and David MacKenzie.
Sorry, meant to include the version info of ld instead of ln: $ ld --version GNU ld version 2.17.50 20060817 Copyright 2005 Free Software Foundation, Inc. This program is free software; you may redistribute it under the terms of the GNU General Public License. This program has absolutely no warranty.
Subject: Re: New: Alignment error with static const variable in inline function On Thu, 2006-09-28 at 07:42 +0000, jespdj at hotmail dot com wrote: > g++ outputs the correct assembler code, so the error must be in as or ld. Or, more likely, in the OS. Try running objdump -x on your executable. If the .rdata section has alignment 2**4, then the linker is OK. It is your OS (i.e. the loader) that isn't respecting the alignment. I tried this on an i386-cygwin system, and it looked OK to me. The rdata section has the right alignment, and the program worked. I didn't do this with binutils-2.17 though. I used the one I already had.
Patch here: http://sourceware.org/ml/binutils/2006-09/msg00377.html Danny
Assigning to self...
Fixed