diff options
author | Marek Polacek <polacek@redhat.com> | 2020-03-05 14:07:25 -0500 |
---|---|---|
committer | Marek Polacek <polacek@redhat.com> | 2020-03-09 09:24:55 -0400 |
commit | 8475f2902a2e2ca5f7ace8bc5265bd1a815dda20 (patch) | |
tree | 616f67587661f17ab966cc152b5fd8cd25917fb0 | |
parent | 157e23d8803dda38d7d38404bcc4284ce2dce3b5 (diff) | |
download | gcc-8475f2902a2e2ca5f7ace8bc5265bd1a815dda20.zip gcc-8475f2902a2e2ca5f7ace8bc5265bd1a815dda20.tar.gz gcc-8475f2902a2e2ca5f7ace8bc5265bd1a815dda20.tar.bz2 |
c++: Fix ABI issue with alignas on armv7hl [PR94050]
The static_assert in the following test was failing on armv7hl because
we were disregarding the alignas specifier on Cell. BaseShape's data
takes up 20B on 32-bit architectures, but we failed to round up its
TYPE_SIZE. This happens since the
<https://gcc.gnu.org/ml/gcc-patches/2019-06/msg01189.html>
patch: here, in layout_class_type for TenuredCell, we see that the size
of TenuredCell and its CLASSTYPE_AS_BASE match, so we set
CLASSTYPE_AS_BASE (t) = t;
While TYPE_USER_ALIGN of TenuredCell was 0, because finalize_type_size
called from finish_record_layout reset it, TYPE_USER_ALIGN of its
CLASSTYPE_AS_BASE still remained 1. After we replace it, it's no longer
1. Then we perform layout_empty_base_or_field for TenuredCell and since
TYPE_USER_ALIGN of its CLASSTYPE_AS_BASE is now 0, we don't do this
adjustment:
if (CLASSTYPE_USER_ALIGN (type))
{
rli->record_align = MAX (rli->record_align, CLASSTYPE_ALIGN (type));
if (warn_packed)
rli->unpacked_align = MAX (rli->unpacked_align, CLASSTYPE_ALIGN (type));
TYPE_USER_ALIGN (rli->t) = 1;
}
where rli->t is BaseShape. Then finalize_record_size won't use the
correct rli->record_align and therefore
/* Round the size up to be a multiple of the required alignment. */
TYPE_SIZE (rli->t) = round_up (unpadded_size, TYPE_ALIGN (rli->t));
after this we end up with the wrong size.
Since the original fix was to avoid creating extra copies for LTO
purposes, I think the following fix should be acceptable.
PR c++/94050 - ABI issue with alignas on armv7hl.
* class.c (layout_class_type): Don't replace a class's
CLASSTYPE_AS_BASE if their TYPE_USER_ALIGN don't match.
* g++.dg/abi/align3.C: New test.
-rw-r--r-- | gcc/cp/ChangeLog | 6 | ||||
-rw-r--r-- | gcc/cp/class.c | 4 | ||||
-rw-r--r-- | gcc/testsuite/ChangeLog | 5 | ||||
-rw-r--r-- | gcc/testsuite/g++.dg/abi/align3.C | 12 |
4 files changed, 27 insertions, 0 deletions
diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog index 128880b..5fc3e7d 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -1,3 +1,9 @@ +2020-03-09 Marek Polacek <polacek@redhat.com> + + PR c++/94050 - ABI issue with alignas on armv7hl. + * class.c (layout_class_type): Don't replace a class's + CLASSTYPE_AS_BASE if their TYPE_USER_ALIGN don't match. + 2020-03-09 Bin Cheng <bin.cheng@linux.alibaba.com> * coroutines.cc (build_actor_fn): Factor out code inserting the diff --git a/gcc/cp/class.c b/gcc/cp/class.c index b3787f7..5340799 100644 --- a/gcc/cp/class.c +++ b/gcc/cp/class.c @@ -6705,6 +6705,10 @@ layout_class_type (tree t, tree *virtuals_p) /* If we didn't end up needing an as-base type, don't use it. */ if (CLASSTYPE_AS_BASE (t) != t + /* If T's CLASSTYPE_AS_BASE is TYPE_USER_ALIGN, but T is not, + replacing the as-base type would change CLASSTYPE_USER_ALIGN, + causing us to lose the user-specified alignment as in PR94050. */ + && TYPE_USER_ALIGN (t) == TYPE_USER_ALIGN (CLASSTYPE_AS_BASE (t)) && tree_int_cst_equal (TYPE_SIZE (t), TYPE_SIZE (CLASSTYPE_AS_BASE (t)))) CLASSTYPE_AS_BASE (t) = t; diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index d8719a4..6c0fab6 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,8 @@ +2020-03-09 Marek Polacek <polacek@redhat.com> + + PR c++/94050 - ABI issue with alignas on armv7hl. + * g++.dg/abi/align3.C: New test. + 2020-03-09 Christophe Lyon <christophe.lyon@linaro.org> * gcc.target/arm/fuse-caller-save.c: Fix DejaGnu typo. diff --git a/gcc/testsuite/g++.dg/abi/align3.C b/gcc/testsuite/g++.dg/abi/align3.C new file mode 100644 index 0000000..a56693a --- /dev/null +++ b/gcc/testsuite/g++.dg/abi/align3.C @@ -0,0 +1,12 @@ +// PR c++/94050 - ABI issue with alignas on armv7hl. +// { dg-do compile { target c++11 } } + +struct alignas(8) Cell {}; +struct TenuredCell : public Cell {}; +struct BaseShape : public TenuredCell { + void *p; + unsigned q, r; + void *s; + __UINTPTR_TYPE__ t; +}; +static_assert (sizeof (BaseShape) % 8 == 0, ""); |