]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
[Darwin, machopic] Preparatory patches.
authorIain Sandoe <iain@sandoe.co.uk>
Sun, 27 Oct 2019 21:17:33 +0000 (21:17 +0000)
committerIain Sandoe <iains@gcc.gnu.org>
Sun, 27 Oct 2019 21:17:33 +0000 (21:17 +0000)
This is a combined set of 5 patches that allows us to fix
PR 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-27  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: r277493

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 22157fe4704907a2f53bb713c2532218efadd1cb..fb91f3e7114cd225047b161294c75194dab6e68e 100644 (file)
@@ -1,3 +1,63 @@
+2019-10-27  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-25  Jan Hubicka  <hubicka@ucw.cz>
 
        Backport from mainline
 
 2019-10-24  Iain Sandoe  <iain@sandoe.co.uk>
 
+       Backport from mainline
+       2019-10-23  Iain Sandoe  <iain@sandoe.co.uk>
        * config/rs6000/darwin.h (ASM_OUTPUT_MAX_SKIP_ALIGN): Guard
        against out of range max skip or log values.
 
index e5614b627d71aa28e0b34f6a5941021772433445..afeca81f807fac06c5b0b44eb09d2d89c6132403 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 55759df79c8af37b76d0eae0eddb3e87e72107d0..d83047b23eb8a09ec4b1529754805e50f861cffe 100644 (file)
@@ -75,7 +75,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
@@ -250,7 +250,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
@@ -259,7 +259,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)
@@ -666,7 +666,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)
@@ -708,6 +708,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);
@@ -798,8 +799,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,
@@ -808,6 +808,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;
     }
@@ -1122,6 +1123,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)))
     {
@@ -1168,14 +1171,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);
 
@@ -1186,7 +1189,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)
@@ -1206,25 +1209,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))
@@ -1235,7 +1264,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 e8fe90728e89a04ad42ad93e01818aa0b839fe06..4f1268494b163557f8729617f9d59c2cced2635b 100644 (file)
@@ -785,21 +785,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 1e96350fa718bf26418affef0e0e69f98bd744d3..3284af2fb204e4e02533ef3476e930496bca88a7 100644 (file)
@@ -326,10 +326,8 @@ extern int darwin_emit_branch_islands;
         }                                                              \
     }
 
-/* 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 6f750c69e0e7b06c523ac1af7f18a4673a64faf1..9fab84fd82e189f133e543dd1634618cf6aba47e 100644 (file)
@@ -468,6 +468,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