https://gcc.gnu.org/bugzilla/show_bug.cgi?id=91306

            Bug ID: 91306
           Summary: [MSP430] libgcc/crtstuff.c: Alignment of frame_dummy
                    .init_array entry is too big
           Product: gcc
           Version: 9.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libgcc
          Assignee: unassigned at gcc dot gnu.org
          Reporter: jozefl.gcc at gmail dot com
  Target Milestone: ---

In libgcc/crtstuff.c, an alignment of "sizeof (void *)" (i.e. pointer size)
is enforced for the frame_dummy() init_array entry. For msp430-elf in the
large memory model (-mlarge), pointer size is 4 bytes, but the expected
alignment of a pointer is 2 bytes.

So when .init_array is constructed, there could be padding added at the
beginning or between entries, since the structure itself is only aligned to
2 bytes but this entry is aligned to 4 bytes.
The padding causes incorrect execution since the code to run through
.init_array does not expect any gaps between entries since pointers only need
to be 2 byte aligned.

I see this alignment of "sizeof (void *)" was added to fix a mips64 bootstrap
failure in r182066 (https://gcc.gnu.org/ml/gcc-patches/2011-12/msg00393.html),
but this alignment isn't appropriate for msp430.

In crtstuff.c, the type of the init_array entry for frame_dummy is specified 
to be "void *", so why must the alignment also be specified?
I don't have access to a mips platform, but shouldn't the entry have the
correct alignment for the type?

Was the addition of the aligned attribute in the 2011 commit a workaround for a
GCC bug? Can it be removed now?

For the following code, GCC generates the the correct alignment without using
the "aligned" attribute on x86, x86_64 and msp430-elf/-mlarge.

static void * entry[]
__attribute__ ((used, section(".init_array"))) = { foo };

x86_64-pc-linux-gnu -m32:
>       .section        .init_array,"aw"
>       .align 4
>       .type   entry, @object
>       .size   entry, 4
>entry:
>       .long   foo

x86_64-pc-linux-gnu -m64:
>       .section        .init_array,"aw"
>       .align 8
>       .type   entry, @object
>       .size   entry, 8
>entry:
>       .quad   foo

msp430-elf -mlarge:
>       .section        .init_array,"aw"
>       .balign 2
>       .type   entry, @object
>       .size   entry, 4
>entry:
>       .long   foo

Reply via email to