]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
[Darwin, machopic] Preparatory patches.
authorIain Sandoe <iain@sandoe.co.uk>
Tue, 29 Oct 2019 20:13:06 +0000 (20:13 +0000)
committerIain Sandoe <iains@gcc.gnu.org>
Tue, 29 Oct 2019 20:13:06 +0000 (20:13 +0000)
This is a combined set of 5 patches that allows us to fix PRs 65342
and 67183.

276924 Make machopic_finish() static.

276767 Set a SYMBOL flag for indirections.

We are able to treat these specially where needed in legitimate address
tests (specifically, they are guaranteed to be pointer-aligned).

276708 Compute and cache indirection rules.

This caches a check for the requirement to indirect a symbol in the Darwin
ABI, and uses it where needed. We also ensure that we place the indirection
pointers into the non-lazy symbol pointers section. Other placements have
occurred with various platform toolchains - but these seem to have been
unintentional so we match current platform toolchains.

276675 Consider visibility in indirections.

For weak, hidden vars the indirection should just be as normal, that
is that the indirections for such symbols should appear in the non-lazy
symbol pointers table, not in the .data section.

276674 Initial tidy of Mach-O symbol handling.

We want to improve the detection and caching of symbol-properties
so that (a) we can make the compiler's output match the platform
norms (b) we can improve efficiency by checking flags instead of
inspecting strings. (c) The fix for PR71767 was a largish hammer
and we want to reduce the number of symbols that are made linker-
visible.

This first patch is largely typographical changes with no functional
difference intended:

- Tries to ensure that there's no overlap between the symbols used in
the Mach-O case and those declared in the i386 or rs6000 port trees.

- Some improvement to comments.

- Makes the naming of the symbol flags consistent with other uses.

- Provides a predicate macro for each use.

2019-10-29  Iain Sandoe  <iain@sandoe.co.uk>

Backport from mainline
2019-10-12  Iain Sandoe  <iain@sandoe.co.uk>

* config/darwin-protos.h (machopic_finish): Delete.
* config/darwin.c (machopic_finish): Make static.

Backport from mainline
2019-10-09  Iain Sandoe  <iain@sandoe.co.uk>

* config/darwin.c (machopic_indirect_data_reference): Set flag to
indicate that the new symbol is an indirection.
(machopic_indirect_call_target): Likewise.
* config/darwin.h (MACHO_SYMBOL_FLAG_INDIRECTION): New.
(MACHO_SYMBOL_INDIRECTION_P): New.
(MACHO_SYMBOL_FLAG_STATIC): Adjust bit number.

Backport from mainline
2019-10-08  Iain Sandoe  <iain@sandoe.co.uk>

* config/darwin.c (machopic_indirect_data_reference): Check for
required indirections before making direct access to defined
values.
(machopic_output_indirection): Place the indirected pointes for
required indirections into the non-lazy symbol pointers section.
(darwin_encode_section_info):
* config/darwin.h (MACHO_SYMBOL_FLAG_MUST_INDIRECT): New.
(MACHO_SYMBOL_MUST_INDIRECT_P): New.

Backport from mainline
2019-10-07  Iain Sandoe  <iain@sandoe.co.uk>

* config/darwin.c (machopic_output_indirection): Don't put
hidden symbol indirections into the .data section, use the
non-lazy symbol pointers section as normal.
(darwin_encode_section_info): Record if a symbol is hidden.
* config/darwin.h (MACHO_SYMBOL_FLAG_HIDDEN_VIS): New.
(MACHO_SYMBOL_HIDDEN_VIS_P): New.

Backport from mainline
2019-10-07  Iain Sandoe  <iain@sandoe.co.uk>

* config/darwin.c (machopic_symbol_defined_p): Use symbol flag
predicates instead of accessing bits directly.
(machopic_indirect_call_target): Likewise.
(machopic_output_indirection): Likewise.
(darwin_encode_section_info): Improve description.  Use renamed
symbol flags.  Use predicate macros for variables and functions.
* config/darwin.h:
Rename MACHO_SYMBOL_VARIABLE to MACHO_SYMBOL_FLAG_VARIABLE.
Rename MACHO_SYMBOL_DEFINED to MACHO_SYMBOL_FLAG_DEFINED.
Rename MACHO_SYMBOL_STATIC to MACHO_SYMBOL_FLAG_STATIC.
(MACHO_SYMBOL_VARIABLE_P): New.
(MACHO_SYMBOL_DEFINED_P):New.
(MACHO_SYMBOL_STATIC_P): New.
* config/i386/darwin.h (MACHO_SYMBOL_FLAG_VARIABLE): Delete.
(SYMBOL_FLAG_SUBT_DEP): New.
* config/rs6000/darwin.h (SYMBOL_FLAG_SUBT_DEP): New.

From-SVN: r277586

gcc/ChangeLog
gcc/config/darwin-protos.h
gcc/config/darwin.c
gcc/config/darwin.h
gcc/config/i386/darwin.h
gcc/config/rs6000/darwin.h

index 86aef482247ad2e812d6c90f9418abad4f62e7f2..899f4dbda6332b394bfce5981e88575e0dd8f4ac 100644 (file)
@@ -1,3 +1,63 @@
+2019-10-29  Iain Sandoe  <iain@sandoe.co.uk>
+
+       Backport from mainline
+       2019-10-12  Iain Sandoe  <iain@sandoe.co.uk>
+
+       * config/darwin-protos.h (machopic_finish): Delete.
+       * config/darwin.c (machopic_finish): Make static.
+
+       Backport from mainline
+       2019-10-09  Iain Sandoe  <iain@sandoe.co.uk>
+
+       * config/darwin.c (machopic_indirect_data_reference): Set flag to
+       indicate that the new symbol is an indirection.
+       (machopic_indirect_call_target): Likewise.
+       * config/darwin.h (MACHO_SYMBOL_FLAG_INDIRECTION): New.
+       (MACHO_SYMBOL_INDIRECTION_P): New.
+       (MACHO_SYMBOL_FLAG_STATIC): Adjust bit number.
+
+       Backport from mainline
+       2019-10-08  Iain Sandoe  <iain@sandoe.co.uk>
+
+       * config/darwin.c (machopic_indirect_data_reference): Check for
+       required indirections before making direct access to defined
+       values.
+       (machopic_output_indirection): Place the indirected pointes for
+       required indirections into the non-lazy symbol pointers section.
+       (darwin_encode_section_info):
+       * config/darwin.h (MACHO_SYMBOL_FLAG_MUST_INDIRECT): New.
+       (MACHO_SYMBOL_MUST_INDIRECT_P): New.
+
+       Backport from mainline
+       2019-10-07  Iain Sandoe  <iain@sandoe.co.uk>
+
+       * config/darwin.c (machopic_output_indirection): Don't put
+       hidden symbol indirections into the .data section, use the
+       non-lazy symbol pointers section as normal.
+       (darwin_encode_section_info): Record if a symbol is hidden.
+       * config/darwin.h (MACHO_SYMBOL_FLAG_HIDDEN_VIS): New.
+       (MACHO_SYMBOL_HIDDEN_VIS_P): New.
+
+       Backport from mainline
+       2019-10-07  Iain Sandoe  <iain@sandoe.co.uk>
+
+       * config/darwin.c (machopic_symbol_defined_p): Use symbol flag
+       predicates instead of accessing bits directly.
+       (machopic_indirect_call_target): Likewise.
+       (machopic_output_indirection): Likewise.
+       (darwin_encode_section_info): Improve description.  Use renamed
+       symbol flags.  Use predicate macros for variables and functions.
+       * config/darwin.h:
+       Rename MACHO_SYMBOL_VARIABLE to MACHO_SYMBOL_FLAG_VARIABLE.
+       Rename MACHO_SYMBOL_DEFINED to MACHO_SYMBOL_FLAG_DEFINED.
+       Rename MACHO_SYMBOL_STATIC to MACHO_SYMBOL_FLAG_STATIC.
+       (MACHO_SYMBOL_VARIABLE_P): New.
+       (MACHO_SYMBOL_DEFINED_P):New.
+       (MACHO_SYMBOL_STATIC_P): New.
+       * config/i386/darwin.h (MACHO_SYMBOL_FLAG_VARIABLE): Delete.
+       (SYMBOL_FLAG_SUBT_DEP): New.
+       * config/rs6000/darwin.h (SYMBOL_FLAG_SUBT_DEP): New.
+
 2019-10-29  Iain Sandoe  <iain@sandoe.co.uk>
 
        Backport from mainline
index 22e62be3592b47c366fb8dbc4207fc8152f75e19..47ce2ee78fea9606764d14a6719d849aa8f5ea71 100644 (file)
@@ -53,8 +53,6 @@ extern void darwin_set_default_type_attributes (tree);
 
 #endif /* TREE_CODE */
 
-extern void machopic_finish (FILE *);
-
 extern int machopic_reloc_rw_mask (void);
 extern section *machopic_select_section (tree, int, unsigned HOST_WIDE_INT);
 
index 7d89fd8bac1dd2ed6a7df9c8bf3e8e6879d8b0f9..e6a424449a62d3d3ad6326c960ee50aade204b04 100644 (file)
@@ -71,7 +71,7 @@ along with GCC; see the file COPYING3.  If not see
    setting the second word in the .non_lazy_symbol_pointer data
    structure to symbol.  See indirect_data for the code that handles
    the extra indirection, and machopic_output_indirection and its use
-   of MACHO_SYMBOL_STATIC for the code that handles @code{static}
+   of MACHO_SYMBOL_FLAG_STATIC for the code that handles @code{static}
    symbol indirection.  */
 
 /* For darwin >= 9  (OSX 10.5) the linker is capable of making the necessary
@@ -246,7 +246,7 @@ name_needs_quotes (const char *name)
 int
 machopic_symbol_defined_p (rtx sym_ref)
 {
-  if (SYMBOL_REF_FLAGS (sym_ref) & MACHO_SYMBOL_FLAG_DEFINED)
+  if (MACHO_SYMBOL_DEFINED_P (sym_ref))
     return true;
 
   /* If a symbol references local and is not an extern to this
@@ -255,7 +255,7 @@ machopic_symbol_defined_p (rtx sym_ref)
     {
       /* If the symbol references a variable and the variable is a
         common symbol, then this symbol is not defined.  */
-      if (SYMBOL_REF_FLAGS (sym_ref) & MACHO_SYMBOL_FLAG_VARIABLE)
+      if (MACHO_SYMBOL_VARIABLE_P (sym_ref))
        {
          tree decl = SYMBOL_REF_DECL (sym_ref);
          if (!decl)
@@ -662,7 +662,7 @@ machopic_indirect_data_reference (rtx orig, rtx reg)
           /* some other cpu -- writeme!  */
           gcc_unreachable ();
        }
-      else if (defined)
+      else if (defined && ! MACHO_SYMBOL_MUST_INDIRECT_P (orig))
        {
          rtx offset = NULL;
          if (DARWIN_PPC || HAVE_lo_sum)
@@ -704,6 +704,7 @@ machopic_indirect_data_reference (rtx orig, rtx reg)
                  machopic_indirection_name (orig, /*stub_p=*/false)));
 
       SYMBOL_REF_DATA (ptr_ref) = SYMBOL_REF_DATA (orig);
+      SYMBOL_REF_FLAGS (ptr_ref) |= MACHO_SYMBOL_FLAG_INDIRECTION;
 
       ptr_ref = gen_const_mem (Pmode, ptr_ref);
       machopic_define_symbol (ptr_ref);
@@ -794,8 +795,7 @@ machopic_indirect_call_target (rtx target)
 
   if (MACHOPIC_INDIRECT
       && GET_CODE (XEXP (target, 0)) == SYMBOL_REF
-      && !(SYMBOL_REF_FLAGS (XEXP (target, 0))
-          & MACHO_SYMBOL_FLAG_DEFINED))
+      && ! MACHO_SYMBOL_DEFINED_P (XEXP (target, 0)))
     {
       rtx sym_ref = XEXP (target, 0);
       const char *stub_name = machopic_indirection_name (sym_ref,
@@ -804,6 +804,7 @@ machopic_indirect_call_target (rtx target)
 
       XEXP (target, 0) = gen_rtx_SYMBOL_REF (mode, stub_name);
       SYMBOL_REF_DATA (XEXP (target, 0)) = SYMBOL_REF_DATA (sym_ref);
+      SYMBOL_REF_FLAGS (XEXP (target, 0)) |= MACHO_SYMBOL_FLAG_INDIRECTION;
       MEM_READONLY_P (target) = 1;
       MEM_NOTRAP_P (target) = 1;
     }
@@ -1125,6 +1126,8 @@ machopic_output_indirection (machopic_indirection **slot, FILE *asm_out_file)
       machopic_output_stub (asm_out_file, sym, stub);
     }
   else if (! indirect_data (symbol)
+          && ! MACHO_SYMBOL_MUST_INDIRECT_P (symbol)
+          && ! MACHO_SYMBOL_HIDDEN_VIS_P (symbol)
           && (machopic_symbol_defined_p (symbol)
               || SYMBOL_REF_LOCAL_P (symbol)))
     {
@@ -1171,14 +1174,14 @@ machopic_output_indirection (machopic_indirection **slot, FILE *asm_out_file)
       assemble_name (asm_out_file, sym_name);
       fprintf (asm_out_file, "\n");
 
-      /* Variables that are marked with MACHO_SYMBOL_STATIC need to
+      /* Variables that are marked with MACHO_SYMBOL_FLAG_STATIC need to
         have their symbol name instead of 0 in the second entry of
         the non-lazy symbol pointer data structure when they are
         defined.  This allows the runtime to rebind newer instances
         of the translation unit with the original instance of the
         symbol.  */
 
-      if ((SYMBOL_REF_FLAGS (symbol) & MACHO_SYMBOL_STATIC)
+      if (MACHO_SYMBOL_STATIC_P (symbol)
          && machopic_symbol_defined_p (symbol))
        init = gen_rtx_SYMBOL_REF (Pmode, sym_name);
 
@@ -1189,7 +1192,7 @@ machopic_output_indirection (machopic_indirection **slot, FILE *asm_out_file)
   return 1;
 }
 
-void
+static void
 machopic_finish (FILE *asm_out_file)
 {
   if (machopic_indirections)
@@ -1209,25 +1212,51 @@ machopic_operand_p (rtx op)
            && XINT (XEXP (op, 0), 1) == UNSPEC_MACHOPIC_OFFSET);
 }
 
-/* This function records whether a given name corresponds to a defined
-   or undefined function or variable, for machopic_classify_ident to
-   use later.  */
+/* This function:
+   computes and caches a series of flags that characterise the symbol's
+   properties that affect Mach-O code gen (including accidental cases
+   from older toolchains).
+
+   TODO:
+   Here we also need to do enough analysis to determine if a symbol's
+   name needs to be made linker-visible.  This is more tricky - since
+   it depends on whether we've previously seen a global weak definition
+   in the same section.
+   */
 
 void
-darwin_encode_section_info (tree decl, rtx rtl, int first ATTRIBUTE_UNUSED)
+darwin_encode_section_info (tree decl, rtx rtl, int first)
 {
-  rtx sym_ref;
+  /* Careful not to prod global register variables.  */
+  if (!MEM_P (rtl))
+    return;
 
-  /* Do the standard encoding things first.  */
+  /* Do the standard encoding things first; this sets:
+     SYMBOL_FLAG_FUNCTION,
+     SYMBOL_FLAG_LOCAL, (binds_local_p)
+     TLS_MODEL, SYMBOL_FLAG_SMALL
+     SYMBOL_FLAG_EXTERNAL.  */
   default_encode_section_info (decl, rtl, first);
 
-  if (TREE_CODE (decl) != FUNCTION_DECL && TREE_CODE (decl) != VAR_DECL)
+  if (! VAR_OR_FUNCTION_DECL_P (decl))
     return;
 
-  sym_ref = XEXP (rtl, 0);
-  if (TREE_CODE (decl) == VAR_DECL)
+  rtx sym_ref = XEXP (rtl, 0);
+  if (VAR_P (decl))
     SYMBOL_REF_FLAGS (sym_ref) |= MACHO_SYMBOL_FLAG_VARIABLE;
 
+  /* Only really common if there's no initialiser.  */
+  bool really_common_p = (DECL_COMMON (decl)
+                         && (DECL_INITIAL (decl) == NULL
+                             || (!in_lto_p
+                                 && DECL_INITIAL (decl) == error_mark_node)));
+
+  /* For Darwin, if we have specified visibility and it's not the default
+     that's counted 'hidden'.  */
+  if (DECL_VISIBILITY_SPECIFIED (decl)
+      && DECL_VISIBILITY (decl) != VISIBILITY_DEFAULT)
+    SYMBOL_REF_FLAGS (sym_ref) |= MACHO_SYMBOL_FLAG_HIDDEN_VIS;
+
   if (!DECL_EXTERNAL (decl)
       && (!TREE_PUBLIC (decl) || !DECL_WEAK (decl))
       && ! lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))
@@ -1238,7 +1267,13 @@ darwin_encode_section_info (tree decl, rtx rtl, int first ATTRIBUTE_UNUSED)
     SYMBOL_REF_FLAGS (sym_ref) |= MACHO_SYMBOL_FLAG_DEFINED;
 
   if (! TREE_PUBLIC (decl))
-    SYMBOL_REF_FLAGS (sym_ref) |= MACHO_SYMBOL_STATIC;
+    SYMBOL_REF_FLAGS (sym_ref) |= MACHO_SYMBOL_FLAG_STATIC;
+
+  /* Short cut check for Darwin 'must indirect' rules.  */
+  if (really_common_p
+      || (DECL_WEAK (decl) && ! MACHO_SYMBOL_HIDDEN_VIS_P (sym_ref))
+      || lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)))
+     SYMBOL_REF_FLAGS (sym_ref) |= MACHO_SYMBOL_FLAG_MUST_INDIRECT;
 }
 
 void
index 1af74af342e20736df495e62d204c92f27e6e811..5b2eb97d432ff896f5e3c753fe7d0574d1d32330 100644 (file)
@@ -778,21 +778,52 @@ extern GTY(()) section * darwin_sections[NUM_DARWIN_SECTIONS];
 #undef TARGET_ASM_MARK_DECL_PRESERVED
 #define TARGET_ASM_MARK_DECL_PRESERVED darwin_mark_decl_preserved
 
-/* Set on a symbol with SYMBOL_FLAG_FUNCTION or
-   MACHO_SYMBOL_FLAG_VARIABLE to indicate that the function or
-   variable has been defined in this translation unit.
-   When porting Mach-O to new architectures you need to make
-   sure these aren't clobbered by the backend.  */
+/* Any port using this header needs to define the first available
+   subtarget symbol bit: SYMBOL_FLAG_SUBT_DEP.  */
 
-#define MACHO_SYMBOL_FLAG_VARIABLE (SYMBOL_FLAG_MACH_DEP)
-#define MACHO_SYMBOL_FLAG_DEFINED ((SYMBOL_FLAG_MACH_DEP) << 1)
+/* Is a variable. */
+#define MACHO_SYMBOL_FLAG_VARIABLE (SYMBOL_FLAG_SUBT_DEP)
+#define MACHO_SYMBOL_VARIABLE_P(RTX) \
+  ((SYMBOL_REF_FLAGS (RTX) & MACHO_SYMBOL_FLAG_VARIABLE) != 0)
+
+/* Set on a symbol that must be indirected, even when there is a
+   definition in the TU.  The ABI mandates that common symbols are so
+   indirected, as are weak.  If 'fix-and-continue' is operational then
+   data symbols might also be.  */
+
+#define MACHO_SYMBOL_FLAG_MUST_INDIRECT ((SYMBOL_FLAG_SUBT_DEP) << 1)
+#define MACHO_SYMBOL_MUST_INDIRECT_P(RTX) \
+  ((SYMBOL_REF_FLAGS (RTX) & MACHO_SYMBOL_FLAG_MUST_INDIRECT) != 0)
+
+/* Set on a symbol with SYMBOL_FLAG_FUNCTION or MACHO_SYMBOL_FLAG_VARIABLE
+   to indicate that the function or variable is considered defined in this
+   translation unit.  */
+
+#define MACHO_SYMBOL_FLAG_DEFINED ((SYMBOL_FLAG_SUBT_DEP) << 2)
+#define MACHO_SYMBOL_DEFINED_P(RTX) \
+  ((SYMBOL_REF_FLAGS (RTX) & MACHO_SYMBOL_FLAG_DEFINED) != 0)
+
+/* Set on a symbol that has specified non-default visibility.  */
+
+#define MACHO_SYMBOL_FLAG_HIDDEN_VIS ((SYMBOL_FLAG_SUBT_DEP) << 3)
+#define MACHO_SYMBOL_HIDDEN_VIS_P(RTX) \
+  ((SYMBOL_REF_FLAGS (RTX) & MACHO_SYMBOL_FLAG_HIDDEN_VIS) != 0)
+
+/* Set on a symbol that is a pic stub or symbol indirection (i.e. the
+   L_xxxxx${stub,non_lazy_ptr,lazy_ptr}.  */
+
+#define MACHO_SYMBOL_FLAG_INDIRECTION ((SYMBOL_FLAG_SUBT_DEP) << 5)
+#define MACHO_SYMBOL_INDIRECTION_P(RTX) \
+  ((SYMBOL_REF_FLAGS (RTX) & MACHO_SYMBOL_FLAG_INDIRECTION) != 0)
 
 /* Set on a symbol to indicate when fix-and-continue style code
    generation is being used and the symbol refers to a static symbol
    that should be rebound from new instances of a translation unit to
    the original instance of the data.  */
 
-#define MACHO_SYMBOL_STATIC ((SYMBOL_FLAG_MACH_DEP) << 2)
+#define MACHO_SYMBOL_FLAG_STATIC ((SYMBOL_FLAG_SUBT_DEP) << 6)
+#define MACHO_SYMBOL_STATIC_P(RTX) \
+  ((SYMBOL_REF_FLAGS (RTX) & MACHO_SYMBOL_FLAG_STATIC) != 0)
 
 /* Symbolic names for various things we might know about a symbol.  */
 
index 58d059823b8402ec60fc9d178d6515f5741688f8..f0d3cd1d3389ed3e6e6ceed9df9ec5f2e0a3aa8c 100644 (file)
@@ -323,10 +323,8 @@ do {                                                                       \
         }                                                                       \
     }
 
-/* This needs to move since i386 uses the first flag and other flags are
-   used in Mach-O.  */
-#undef MACHO_SYMBOL_FLAG_VARIABLE
-#define MACHO_SYMBOL_FLAG_VARIABLE ((SYMBOL_FLAG_MACH_DEP) << 3)
+/* First available SYMBOL flag bit for use by subtargets.  */
+#define SYMBOL_FLAG_SUBT_DEP (SYMBOL_FLAG_MACH_DEP << 5)
 
 #undef MACHOPIC_NL_SYMBOL_PTR_SECTION
 #define MACHOPIC_NL_SYMBOL_PTR_SECTION \
index f8cc225c4402102f4ddbb2c7f85fe2bbdd8e8e32..0c81730bfa35aa0b2bdfdd2fc1d10e0561e86ac8 100644 (file)
@@ -430,6 +430,9 @@ do {                                                                        \
    this will need to be modified similar to the x86 case.  */
 #define TARGET_FOLD_BUILTIN SUBTARGET_FOLD_BUILTIN
 
+/* First available SYMBOL flag bit for use by subtargets.  */
+#define SYMBOL_FLAG_SUBT_DEP (SYMBOL_FLAG_MACH_DEP)
+
 /* Use standard DWARF numbering for DWARF debugging information.  */
 #define RS6000_USE_DWARF_NUMBERING