c++, abi: Fix up class layout with bitfields [PR109039]
authorJakub Jelinek <jakub@redhat.com>
Fri, 10 Mar 2023 19:36:33 +0000 (20:36 +0100)
committerJakub Jelinek <jakub@redhat.com>
Fri, 10 Mar 2023 19:36:33 +0000 (20:36 +0100)
commit991f9eb2da3a268b7b08346761fa0078ab55f506
tree49caa3188f03f05545b27178a8304180115f8cc3
parent20d790aa3ea5b0d240032cab997b8e0938cac62c
c++, abi: Fix up class layout with bitfields [PR109039]

The following testcase FAILs, because starting with r12-6028
the S class has only 2 bytes, not enough to hold one 7-bit bitfield, one 8-bit
bitfield and one 8-bit char field.

The reason is that when end_of_class attempts to compute dsize, it simply
adds byte_position of the field and DECL_SIZE_UNIT (and uses maximum from
those offsets).
The problematic bit-field in question has bit_position 7, byte_position 0,
DECL_SIZE 8 and DECL_SIZE_UNIT 1.  So, byte_position + DECL_SIZE_UNIT is
1, even when the bitfield only has a single bit in the first byte and 7
further bits in the second byte, so per the Itanium ABI it should be 2:
"In either case, update dsize(C) to include the last byte
containing (part of) the bit-field, and update sizeof(C) to
max(sizeof(C),dsize(C))."

The following patch fixes it by computing bitsize of the end and using
CEIL_DIV_EXPR division to round it to next byte boundary and convert
from bits to bytes.

While this is an ABI change, classes with such incorrect layout couldn't
have worked properly, so I doubt anybody is actually running it often
in the wild.  Thus I think adding some ABI warning for it is unnecessary.

2023-03-10  Jakub Jelinek  <jakub@redhat.com>

PR c++/109039
* class.cc (end_of_class): For bit-fields, instead of computing
offset as sum of byte_position (field) and DECL_SIZE_UNIT (field),
compute it as sum of bit_position (field) and DECL_SIZE (field)
divided by BITS_PER_UNIT rounded up.

* g++.dg/abi/no_unique_address7.C: New test.
gcc/cp/class.cc
gcc/testsuite/g++.dg/abi/no_unique_address7.C [new file with mode: 0644]