[committed] Darwin: Handle NULL DECL_SIZE_TYPE in machopic_select_section (PR94237).

Message ID 2D2D234B-6BB3-452A-8628-94800172638C@sandoe.co.uk
State New
Headers show
Series
  • [committed] Darwin: Handle NULL DECL_SIZE_TYPE in machopic_select_section (PR94237).
Related show

Commit Message

Iain Sandoe March 21, 2020, 7:17 p.m.
Hi,

A recent change in the LTO streaming arrangement means that it is
now possible for machopic_select_section () to be called with a NULL
value for DECL_SIZE_TYPE - corresponding to an incomplete or not-yet-
laid out type.

When section anchors are present, and we are generating assembler, we
normally need to know the object size when choosing the section, since
zero-sized objects must be placed in sections that forbid section
anchors.

In the current circumstance, the objective of the earlier streaming of
this data is to allow nm to determine BSS c.f. Data symbols (when used
with the LTO plugin).

We now detect when the size is unknown and return the 'generic' section
for the DECL kind, which will still be correct in determining the BSS 
c.f. Data case.

Since Darwin does not yet make use of the LTO plugin this is a bit of
future-proofing (but needed to fix the PR too). 

tested across the darwin range,
applied to master,
thanks
Iain

gcc/ChangeLog:

2020-03-21  Iain Sandoe  <iain@sandoe.co.uk>

	PR lto/94237
	* config/darwin.c (darwin_mergeable_constant_section): Collect
	section anchor checks into the caller.
	(machopic_select_section): Collect section anchor checks into
	the determination of 'effective zero-size' objects.  When the
	size is unknown, assume it is non-zero, and thus return the
	'generic' section for the DECL.

Patch

diff --git a/gcc/config/darwin.c b/gcc/config/darwin.c
index 8131361715b..d3c0af8a4b6 100644
--- a/gcc/config/darwin.c
+++ b/gcc/config/darwin.c
@@ -1354,9 +1354,7 @@  darwin_mergeable_constant_section (tree exp,
   machine_mode mode = DECL_MODE (exp);
   unsigned int modesize = GET_MODE_BITSIZE (mode);
 
-  if (DARWIN_SECTION_ANCHORS
-      && flag_section_anchors
-      && zsize)
+  if (zsize)
     return darwin_sections[zobj_const_section];
 
   if (flag_merge_constants
@@ -1586,8 +1584,23 @@  machopic_select_section (tree decl,
 	  && DECL_WEAK (decl)
 	  && !lookup_attribute ("weak_import", DECL_ATTRIBUTES (decl)));
 
-  zsize = (DECL_P (decl)
+  /* Darwin pads zero-sized objects with at least one byte, so that the ld64
+     atom model is preserved (objects must have distinct regions starting with
+     a unique linker-visible symbol).
+     In order to support section anchors, we need to move objects with zero
+     size into sections which are marked as "no section anchors"; the padded
+     objects, obviously, have real sizes that differ from their DECL sizes.  */
+  zsize = DARWIN_SECTION_ANCHORS && flag_section_anchors;
+
+  /* In the streaming of LTO symbol data, we might have a situation where the
+     var is incomplete or layout not finished (DECL_SIZE_UNIT is NULL_TREE).
+     We cannot tell if it is zero-sized then, but we can get the section
+     category correct so that nm reports the right kind of section
+     (e.g. BSS c.f. data).  */
+  zsize = (zsize
+	   && DECL_P (decl)
 	   && (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == CONST_DECL)
+	   && DECL_SIZE_UNIT (decl)
 	   && tree_to_uhwi (DECL_SIZE_UNIT (decl)) == 0);
 
   one = DECL_P (decl)
@@ -1635,15 +1648,11 @@  machopic_select_section (tree decl,
 	  else
 	    base_section = darwin_sections[data_coal_section];
 	}
-      else if (DARWIN_SECTION_ANCHORS
-	       && flag_section_anchors
-	       && zsize)
+      else if (zsize)
 	{
 	  /* If we're doing section anchors, then punt zero-sized objects into
 	     their own sections so that they don't interfere with offset
-	     computation for the remaining vars.  This does not need to be done
-	     for stuff in mergeable sections, since these are ineligible for
-	     anchors.  */
+	     computation for the remaining vars.  */
 	  if (ro)
 	    base_section = darwin_sections[zobj_const_data_section];
 	  else