This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
Go patch committed: Fix handling of zero sized struct
- From: Ian Lance Taylor <iant at google dot com>
- To: gcc-patches at gcc dot gnu dot org, gofrontend-dev at googlegroups dot com
- Date: Wed, 24 Jul 2013 11:25:52 -0700
- Subject: Go patch committed: Fix handling of zero sized struct
When a Go global variable has zero size, we fudge it to make it larger
to avoid problems with the linker. This turned out to cause a bug with
a struct with a zero-sized field, since code still expected to be able
to access the field. This patch changes the zero size handling to
recreate such structs, making sure that the first field has non-zero
size. Bootstrapped and ran Go testsuite on x86_64-unknown-linux-gnu.
Committed to mainline and 4.8 branch.
Ian
2013-07-24 Ian Lance Taylor <iant@google.com>
* go-gcc.cc (Gcc_backend::non_zero_size_type): If a struct has a
fields, recreate those fields with the first one with a non-zero
size.
Index: go-gcc.cc
===================================================================
--- go-gcc.cc (revision 201178)
+++ go-gcc.cc (working copy)
@@ -1242,20 +1242,41 @@ Gcc_backend::non_zero_size_type(tree typ
switch (TREE_CODE(type))
{
case RECORD_TYPE:
- {
- if (go_non_zero_struct == NULL_TREE)
- {
- type = make_node(RECORD_TYPE);
- tree field = build_decl(UNKNOWN_LOCATION, FIELD_DECL,
- get_identifier("dummy"),
- boolean_type_node);
- DECL_CONTEXT(field) = type;
- TYPE_FIELDS(type) = field;
- layout_type(type);
- go_non_zero_struct = type;
- }
- return go_non_zero_struct;
- }
+ if (TYPE_FIELDS(type) != NULL_TREE)
+ {
+ tree ns = make_node(RECORD_TYPE);
+ tree field_trees = NULL_TREE;
+ tree *pp = &field_trees;
+ for (tree field = TYPE_FIELDS(type);
+ field != NULL_TREE;
+ field = DECL_CHAIN(field))
+ {
+ tree ft = TREE_TYPE(field);
+ if (field == TYPE_FIELDS(type))
+ ft = non_zero_size_type(ft);
+ tree f = build_decl(DECL_SOURCE_LOCATION(field), FIELD_DECL,
+ DECL_NAME(field), ft);
+ DECL_CONTEXT(f) = ns;
+ *pp = f;
+ pp = &DECL_CHAIN(f);
+ }
+ TYPE_FIELDS(ns) = field_trees;
+ layout_type(ns);
+ return ns;
+ }
+
+ if (go_non_zero_struct == NULL_TREE)
+ {
+ type = make_node(RECORD_TYPE);
+ tree field = build_decl(UNKNOWN_LOCATION, FIELD_DECL,
+ get_identifier("dummy"),
+ boolean_type_node);
+ DECL_CONTEXT(field) = type;
+ TYPE_FIELDS(type) = field;
+ layout_type(type);
+ go_non_zero_struct = type;
+ }
+ return go_non_zero_struct;
case ARRAY_TYPE:
{