From: Andrew Burgess <andrew.burgess@embecosm.com>
To: gdb-patches@sourceware.org
Cc: Jim Wilson <jimw@sifive.com>, John Baldwin <jhb@freebsd.org>,
Palmer Dabbelt <palmer@sifive.com>, Tom Tromey <tom@tromey.com>,
Andrew Burgess <andrew.burgess@embecosm.com>
Subject: [PATCH 1/2] gdb: Fix alignment computation for structs with only static fields
Date: Sun, 07 Apr 2019 21:38:00 -0000 [thread overview]
Message-ID: <fc5e30e2581ca0ae46f1b51ad01e9fb2c5ae6cae.1554672870.git.andrew.burgess@embecosm.com> (raw)
In-Reply-To: <cover.1554672869.git.andrew.burgess@embecosm.com>
In-Reply-To: <cover.1554672869.git.andrew.burgess@embecosm.com>
The current code in gdbtypes.c:type_align incorrectly returns 0 as the
alignment for a structure containing only static fields. After this
patch the correct value of 1 is returned. The gdb.base/align.exp test
is extended to cover this case.
gdb/ChangeLog:
* gdbtypes.c (type_align): A struct with no non-static fields also
has alignment of 1.
gdb/testsuite/ChangeLog:
* gdb.base/align.exp: Extend test to cover structures containing
only static fields.
---
gdb/ChangeLog | 5 +++++
gdb/gdbtypes.c | 12 ++++++------
gdb/testsuite/ChangeLog | 5 +++++
gdb/testsuite/gdb.base/align.exp | 24 ++++++++++++++++++------
4 files changed, 34 insertions(+), 12 deletions(-)
diff --git a/gdb/gdbtypes.c b/gdb/gdbtypes.c
index b1a51374d96..1d7ff145b98 100644
--- a/gdb/gdbtypes.c
+++ b/gdb/gdbtypes.c
@@ -3015,16 +3015,12 @@ type_align (struct type *type)
case TYPE_CODE_STRUCT:
case TYPE_CODE_UNION:
{
- if (TYPE_NFIELDS (type) == 0)
- {
- /* An empty struct has alignment 1. */
- align = 1;
- break;
- }
+ int number_of_non_static_fields = 0;
for (unsigned i = 0; i < TYPE_NFIELDS (type); ++i)
{
if (!field_is_static (&TYPE_FIELD (type, i)))
{
+ number_of_non_static_fields++;
ULONGEST f_align = type_align (TYPE_FIELD_TYPE (type, i));
if (f_align == 0)
{
@@ -3036,6 +3032,10 @@ type_align (struct type *type)
align = f_align;
}
}
+ /* A struct with no fields, or with only static fields has an
+ alignment of 1. */
+ if (number_of_non_static_fields == 0)
+ align = 1;
}
break;
diff --git a/gdb/testsuite/gdb.base/align.exp b/gdb/testsuite/gdb.base/align.exp
index 558625d1b3e..e3ca0470e94 100644
--- a/gdb/testsuite/gdb.base/align.exp
+++ b/gdb/testsuite/gdb.base/align.exp
@@ -67,8 +67,10 @@ proc prepare_test_source_file { lang } {
puts -nonewline $outfile "#define DEF(T,U) struct align_pair_ ## T ## _x_ ## U "
puts $outfile "{ T one; U two; }"
if { $lang == "c++" } {
- puts -nonewline $outfile "#define DEF_WITH_STATIC(T,U) struct align_pair_static_ ## T ## _x_ ## U "
+ puts -nonewline $outfile "#define DEF_WITH_1_STATIC(T,U) struct align_pair_static_ ## T ## _x_ ## U "
puts $outfile "{ static T one; U two; }"
+ puts -nonewline $outfile "#define DEF_WITH_2_STATIC(T,U) struct align_pair_static_ ## T ## _x_static_ ## U "
+ puts $outfile "{ static T one; static U two; }"
}
if { $lang == "c" } {
puts $outfile "unsigned a_void = ${align_func} (void);"
@@ -99,11 +101,17 @@ proc prepare_test_source_file { lang } {
puts $outfile " = ${align_func} (struct align_pair_${joined});"
if { $lang == "c++" } {
- puts $outfile "DEF_WITH_STATIC ($utype, $uinner);"
- set joined "${utype}_x_${uinner}"
- puts $outfile "struct align_pair_static_$joined item_static_${joined};"
- puts $outfile "unsigned a_static_${joined}"
- puts $outfile " = ${align_func} (struct align_pair_static_${joined});"
+ puts $outfile "DEF_WITH_1_STATIC ($utype, $uinner);"
+ set joined "static_${utype}_x_${uinner}"
+ puts $outfile "struct align_pair_$joined item_${joined};"
+ puts $outfile "unsigned a_${joined}"
+ puts $outfile " = ${align_func} (struct align_pair_${joined});"
+
+ puts $outfile "DEF_WITH_2_STATIC ($utype, $uinner);"
+ set joined "static_${utype}_x_static_${uinner}"
+ puts $outfile "struct align_pair_$joined item_${joined};"
+ puts $outfile "unsigned a_${joined}"
+ puts $outfile " = ${align_func} (struct align_pair_${joined});"
}
}
}
@@ -160,6 +168,10 @@ proc run_alignment_test { lang } {
set expected [get_integer_valueof a_static_${utype}_x_${uinner} 0]
gdb_test "print ${align_func}(struct align_pair_static_${utype}_x_${uinner})" \
" = $expected"
+
+ set expected [get_integer_valueof a_static_${utype}_x_static_${uinner} 0]
+ gdb_test "print ${align_func}(struct align_pair_static_${utype}_x_static_${uinner})" \
+ " = $expected"
}
}
}
--
2.14.5
next prev parent reply other threads:[~2019-04-07 21:38 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-04-07 21:38 [PATCH 0/2] More type_align fixes Andrew Burgess
2019-04-07 21:38 ` Andrew Burgess [this message]
2019-04-07 22:36 ` [PATCH 1/2] gdb: Fix alignment computation for structs with only static fields Tom Tromey
2019-04-07 21:38 ` [PATCH 2/2] gdb/riscv: Remove riscv_type_alignment function Andrew Burgess
2019-04-11 22:42 ` [PATCH 0/2] More type_align fixes Andrew Burgess
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=fc5e30e2581ca0ae46f1b51ad01e9fb2c5ae6cae.1554672870.git.andrew.burgess@embecosm.com \
--to=andrew.burgess@embecosm.com \
--cc=gdb-patches@sourceware.org \
--cc=jhb@freebsd.org \
--cc=jimw@sifive.com \
--cc=palmer@sifive.com \
--cc=tom@tromey.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox