This is sources Bugzilla
Bugzilla Version 2.17.5
Bugzilla Bug 3276
  Alignment error with static const variable in inline function Last modified: 2006-09-29 03:37
     Query page      Enter new bug
Bug#: 3276   Hardware:   Reporter: Jesper de Jong <jespdj@hotmail.com>
Host: Target: Build:
Product:     Add CC:
Component:   Version:   CC:
Remove selected CCs
Status: RESOLVED   Priority:  
Resolution: FIXED   Severity:  
Assigned To: Danny Smith <dannysmith@users.sourceforge.net>   Target Milestone:  
Summary:
Keywords:

Attachment Description Type Created Actions
tst.s Assember code produced by g++ text/plain 2006-09-28 07:45 Edit None
Create a New Attachment (proposed patch, testcase, etc.) View All

Bug 3276 depends on: Show dependency tree
Show dependency graph
Bug 3276 blocks:

Additional Comments:


Leave as RESOLVED FIXED
Reopen bug
Mark bug as VERIFIED

View Bug Activity   |   Format For Printing


Description:   Last confirmed: 0000-00-00 00:00 Opened: 2006-09-28 07:42
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.

------- Additional Comment #1 From Jesper de Jong 2006-09-28 07:45 -------
Created an attachment (id=1333)
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??

------- Additional Comment #2 From Jesper de Jong 2006-09-28 07:48 -------
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

------- Additional Comment #3 From Jesper de Jong 2006-09-28 08:06 -------
$ 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.

------- Additional Comment #4 From Jesper de Jong 2006-09-28 08:07 -------
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.

------- Additional Comment #5 From wilson@specifix.com 2006-09-28 21:30 -------
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.

------- Additional Comment #6 From Danny Smith 2006-09-28 23:15 -------
Patch here:
http://sourceware.org/ml/binutils/2006-09/msg00377.html
Danny

------- Additional Comment #7 From Danny Smith 2006-09-29 03:35 -------
Assigning to self...

------- Additional Comment #8 From Danny Smith 2006-09-29 03:37 -------
Fixed

     Query page      Enter new bug
Actions: New | Query | bug # | Reports | Requests   New Account | Log In