This is the mail archive of the gdb-cvs@sourceware.org mailing list for the GDB project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[binutils-gdb] gdb: Fix alignment computation for structs with only static fields


https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=41077b6625d16cc6c0c4b404a177a8850300b8a0

commit 41077b6625d16cc6c0c4b404a177a8850300b8a0
Author: Andrew Burgess <andrew.burgess@embecosm.com>
Date:   Sat Apr 6 11:02:04 2019 +0100

    gdb: Fix alignment computation for structs with only static fields
    
    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.

Diff:
---
 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/ChangeLog b/gdb/ChangeLog
index 0be7538..d50a034 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,5 +1,10 @@
 2019-04-11  Andrew Burgess  <andrew.burgess@embecosm.com>
 
+	* gdbtypes.c (type_align): A struct with no non-static fields also
+	has alignment of 1.
+
+2019-04-11  Andrew Burgess  <andrew.burgess@embecosm.com>
+
 	* riscv-tdep.c (riscv_call_arg_complex_float): Fix offset of first
 	component to 0.
 	(riscv_struct_info::riscv_struct_info): Initialise m_offsets
diff --git a/gdb/gdbtypes.c b/gdb/gdbtypes.c
index 919feac..683238d 100644
--- a/gdb/gdbtypes.c
+++ b/gdb/gdbtypes.c
@@ -3014,16 +3014,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)
 		  {
@@ -3035,6 +3031,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/ChangeLog b/gdb/testsuite/ChangeLog
index dfbee85..3b952b8 100644
--- a/gdb/testsuite/ChangeLog
+++ b/gdb/testsuite/ChangeLog
@@ -1,3 +1,8 @@
+2019-04-11  Andrew Burgess  <andrew.burgess@embecosm.com>
+
+	* gdb.base/align.exp: Extend test to cover structures containing
+	only static fields.
+
 2019-04-11  Tom de Vries  <tdevries@suse.de>
 
 	* boards/cc-with-dwz-m.exp: New file.
diff --git a/gdb/testsuite/gdb.base/align.exp b/gdb/testsuite/gdb.base/align.exp
index 558625d..e3ca047 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"
 	    }
 	}
     }


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]