Bug 3276 - Alignment error with static const variable in inline function
Summary: Alignment error with static const variable in inline function
Status: RESOLVED FIXED
Alias: None
Product: binutils
Classification: Unclassified
Component: binutils (show other bugs)
Version: 2.17
: P2 normal
Target Milestone: ---
Assignee: Danny Smith
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2006-09-28 07:42 UTC by Jesper de Jong
Modified: 2006-09-29 03:37 UTC (History)
1 user (show)

See Also:
Host:
Target: i686-pc-cygwin
Build:
Last reconfirmed:


Attachments
Assember code produced by g++ (528 bytes, text/plain)
2006-09-28 07:45 UTC, Jesper de Jong
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Jesper de Jong 2006-09-28 07:42:44 UTC
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.
Comment 1 Jesper de Jong 2006-09-28 07:45:21 UTC
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??
Comment 2 Jesper de Jong 2006-09-28 07:48:41 UTC
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
Comment 3 Jesper de Jong 2006-09-28 08:06:16 UTC
$ 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.
Comment 4 Jesper de Jong 2006-09-28 08:07:27 UTC
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.
Comment 5 wilson@specifix.com 2006-09-28 21:30:37 UTC
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.
Comment 6 Danny Smith 2006-09-28 23:15:10 UTC
Patch here:
http://sourceware.org/ml/binutils/2006-09/msg00377.html
Danny
Comment 7 Danny Smith 2006-09-29 03:35:36 UTC
Assigning to self...
Comment 8 Danny Smith 2006-09-29 03:37:07 UTC
Fixed