From patchwork Sun Apr 7 21:37:54 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Burgess X-Patchwork-Id: 32192 Received: (qmail 106693 invoked by alias); 7 Apr 2019 21:38:07 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Delivered-To: mailing list gdb-patches@sourceware.org Received: (qmail 106630 invoked by uid 89); 7 Apr 2019 21:38:06 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-19.4 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_NONE, SPF_PASS autolearn=ham version=3.3.1 spammy=tu, TU, HX-Languages-Length:3831, 6710 X-HELO: mail-wr1-f66.google.com Received: from mail-wr1-f66.google.com (HELO mail-wr1-f66.google.com) (209.85.221.66) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Sun, 07 Apr 2019 21:38:04 +0000 Received: by mail-wr1-f66.google.com with SMTP id y13so14014456wrd.3 for ; Sun, 07 Apr 2019 14:38:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=embecosm.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :in-reply-to:references; bh=OmDu20w4yqtLGkhRPInPGEmIWvBDwsjxCG4/Oo7ib2I=; b=RBVmjEh5RotCbDsfv3Aku5PGCf6/uyPDTKQXxUc0786wJmCu6v7/1egcV0AmGCZug1 mkBnyGDhg5RgynLFx6cs+CJ75dYE6Fb6P/2YzP0R8ayguFzIKWBnhLIiOmNL4oqh9HpN 5p5M0e287COvX3hK4Rel+jf8RGz23O+3T6JbInYueHi92bzNCylDsjQsIH6DBYdgYvyA c3wwxe5CS2eYLXBDLhlSDOVzLc5eQ41dxJG/3H4ITgY16fo6uf2IbUBfT9jCIb4ibzG/ 428H9aZYFneJyjgERvfzyvtN7i07Ji3rmKnZsZq+2wdnXFGpX+rnt7hEkr43zyvxZ6wm t+nQ== Return-Path: Received: from localhost (host86-185-77-247.range86-185.btcentralplus.com. [86.185.77.247]) by smtp.gmail.com with ESMTPSA id t76sm14186956wmt.8.2019.04.07.14.38.01 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Sun, 07 Apr 2019 14:38:01 -0700 (PDT) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: Jim Wilson , John Baldwin , Palmer Dabbelt , Tom Tromey , Andrew Burgess Subject: [PATCH 1/2] gdb: Fix alignment computation for structs with only static fields Date: Sun, 7 Apr 2019 22:37:54 +0100 Message-Id: In-Reply-To: References: In-Reply-To: References: X-IsSubscribed: yes 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" } } }