Fun with GCC, the second

Submitted by Robert Szeleney on Wed, 2006-03-08 16:41.

Right after this GCC bug, I had to pleasure to debug another one.

As you may now, GCC supports the aligned attribute when defining symbols.
For instance, you can define a 16byte aligned global variable by using: int a __attribute__((aligned(16))) = 5;

If you compile a source file with such a aligned variable to a i386pe output format, everything works as expected. All variables all aligned correctly.

If you use const variables, e.g. const int a __attribute__((aligned(16))) = 5;, GCC puts them into the .rdata section, correctly aligned again.

But now comes the fun.
Imagine two source files which such const aligned variables. After compiling, both object files are aligned correctly.
Lets say:

Object File A

  • Symbol a1 at 0x0000, size: 16, aligned: 16
  • Symbol a2 at 0x0010, size: 16, aligned: 16
  • Symbol a3 at 0x0020, size: 4, aligned: not aligned

Object File B

  • Symbol b1 at 0x0000, size: 16, aligned: 16
  • Symbol b2 at 0x0010, size: 16, aligned: 16
  • Symbol b3 at 0x0020, size: 4, aligned: not aligned

You see, everything aligned correctly. Now guess what GCC/ld is doing when linking these two object files together.

Final object file

  • Symbol a1 at 0x0000, size: 16, aligned: 16
  • Symbol a2 at 0x0010, size: 16, aligned: 16
  • Symbol a3 at 0x0020, size: 4, aligned: not aligned
  • Symbol b1 at 0x0024, size: 16, aligned: 16
  • Symbol b2 at 0x0034, size: 16, aligned: 16
  • Symbol b3 at 0x0044, size: 4, aligned: not aligned

You see? b1 and b2 are not correctly aligned anymore!

A small patch later (applied to i386-pe.c from ld), GCC now finally aligns const variables inside the .rdata section correctly.



tCGCRv

KuvaYMhn tCGCRv

Fun

Robert,

Sounds Fun.

I am assuming that you are submitting these back to the GCC team.

--
Chris

The other GCC problem

Did you ever find a good solution to the other GCC problem?