From 38725d77a9b8345d5ea952540a43a594d96f92d7 Mon Sep 17 00:00:00 2001
From: Iain Sandoe <iain@sandoe.co.uk>
Date: Mon, 3 Aug 2020 22:46:40 +0100
Subject: [PATCH] Darwin, Arm64 : Base build changes to Darwin common code.

It seems that the arm64 port will not make use of traditional mach-o
pic but instead use a GOT - as such, the target-dependent parts of
darwin.c are made NOP for this.  We still expect to apply other Mach-O
constraints.

Most of the change is about the support for giving the user an error
message when they try to invoke the compiler with multiple -arch
flags.

There is no provision in the initial port to handle the arm64_32 variant.
This is (understood to be) only used in watchOS which we are not
supporting initially.

+  No machopic indirections for Arm64 personality/LSDA entries.

As for x86-64 (64bit code), the personality and LSDA .cfi_xxx entries
do not require an indirection in the compiler code.

+ Accept arm64 in arch flags, for aarch64 compilers.

We were checking for this and complaining if it was present for an
X86 or PPC compiler, but we also need to accept (and ignore it) for
an aarch64 / Arm64 compiler.

(cherry picked from commit 03f7e422c3dfc7b2f286fdb644f572999d991def)
Signed-off-by: Kirill A. Korinsky <kirill@korins.ky>
---
 gcc/config/darwin-driver.c | 24 ++++++++++++++++++++++--
 gcc/config/darwin.c        | 11 ++++++++++-
 gcc/config/darwin.h        |  1 +
 3 files changed, 33 insertions(+), 3 deletions(-)

diff --git gcc/config/darwin-driver.c gcc/config/darwin-driver.c
index 9fc40f6680f..ff66865e4bd 100644
--- gcc/config/darwin-driver.c
+++ gcc/config/darwin-driver.c
@@ -268,10 +268,13 @@ darwin_driver_init (unsigned int *decoded_options_count,
   bool seenX86_64 = false;
   bool seenPPC = false;
   bool seenPPC64 = false;
+#if !DARWIN_ARM64
+  bool seenArm64 = false;
   bool seenM32 = false;
   bool seenM64 = false;
   bool appendM32 = false;
   bool appendM64 = false;
+#endif
   const char *vers_string = NULL;
   bool seen_version_min = false;
   bool seen_sysroot_p = false;
@@ -297,6 +300,12 @@ darwin_driver_init (unsigned int *decoded_options_count,
 	    seenPPC = true;
 	  else if (!strcmp ((*decoded_options)[i].arg, "ppc64"))
 	    seenPPC64 = true;
+	  else if (!strcmp ((*decoded_options)[i].arg, "arm64"))
+#if !DARWIN_ARM64
+	    seenArm64 = true;
+#else
+	    ; /* We accept the option, but don't need to act on it.  */
+#endif
 	  else
 	    error ("this compiler does not support %s",
 		   (*decoded_options)[i].arg);
@@ -310,7 +319,7 @@ darwin_driver_init (unsigned int *decoded_options_count,
 	  --i;
 	  --*decoded_options_count; 
 	  break;
-
+#if !DARWIN_ARM64
 	case OPT_m32:
 	  seenM32 = true;
 	  break;
@@ -318,7 +327,7 @@ darwin_driver_init (unsigned int *decoded_options_count,
 	case OPT_m64:
 	  seenM64 = true;
 	  break;
-
+#endif
 	case OPT_mmacosx_version_min_:
 	  seen_version_min = true;
 	  vers_string =
@@ -368,6 +377,8 @@ darwin_driver_init (unsigned int *decoded_options_count,
 #if DARWIN_X86
   if (seenPPC || seenPPC64)
     warning (0, "this compiler does not support PowerPC (arch flags ignored)");
+  else if (seenArm64)
+    warning (0, "this compiler does not support Arm64 (arch flags ignored)");
   if (seenX86)
     {
       if (seenX86_64 || seenM64)
@@ -387,6 +398,8 @@ darwin_driver_init (unsigned int *decoded_options_count,
 #elif DARWIN_PPC
   if (seenX86 || seenX86_64)
     warning (0, "this compiler does not support X86 (arch flags ignored)");
+  else if (seenArm64)
+    warning (0, "this compiler does not support Arm64 (arch flags ignored)");
   if (seenPPC)
     {
       if (seenPPC64 || seenM64)
@@ -403,12 +416,18 @@ darwin_driver_init (unsigned int *decoded_options_count,
       else if (! seenM64) /* Add -m64 if the User didn't. */
 	appendM64 = true;
     }
+#elif DARWIN_ARM64
+  if (seenPPC || seenPPC64)
+    warning (0, "this compiler does not support PowerPC (arch flags ignored)");
+  if (seenX86 || seenX86_64)
+    warning (0, "this compiler does not support X86 (arch flags ignored)");
 #endif
 
   /* If there is nothing else on the command line, do not add sysroot etc.  */
   if (*decoded_options_count <= 1)
     return;
 
+#if !DARWIN_ARM64
   if (appendM32 || appendM64)
     {
       ++*decoded_options_count;
@@ -418,6 +437,7 @@ darwin_driver_init (unsigned int *decoded_options_count,
       generate_option (appendM32 ? OPT_m32 : OPT_m64, NULL, 1, CL_DRIVER,
 		       &(*decoded_options)[*decoded_options_count - 1]);
     }
+#endif
 
   if (!seen_sysroot_p)
     {
diff --git gcc/config/darwin.c gcc/config/darwin.c
index 9005104fb42..d08a3eb360d 100644
--- gcc/config/darwin.c
+++ gcc/config/darwin.c
@@ -115,7 +115,7 @@ static bool ld_init_term_start_labels = false;
 section * darwin_sections[NUM_DARWIN_SECTIONS];
 
 /* While we transition to using in-tests instead of ifdef'd code.  */
-#if !HAVE_lo_sum
+#if !HAVE_lo_sum || DARWIN_ARM64
 #define gen_macho_high(m,a,b) (a)
 #define gen_macho_low(m,a,b,c) (a)
 #endif
@@ -1055,6 +1055,7 @@ machopic_legitimize_pic_address (rtx orig, machine_mode mode, rtx reg)
   return pic_ref;
 }
 
+#if !DARWIN_ARM64
 /* Callbacks to output the stub or non-lazy pointers.
    Each works on the item in *SLOT,if it has been used.
    DATA is the FILE* for assembly output.
@@ -1210,6 +1211,7 @@ machopic_finish (FILE *asm_out_file)
   machopic_indirections->traverse_noresize
     <FILE *, machopic_output_indirection> (asm_out_file);
 }
+#endif
 
 int
 machopic_operand_p (rtx op)
@@ -2244,6 +2246,8 @@ darwin_emit_except_table_label (FILE *file)
 rtx
 darwin_make_eh_symbol_indirect (rtx orig, bool ARG_UNUSED (pubvis))
 {
+  if (DARWIN_ARM64)
+    return orig;
   if (DARWIN_PPC == 0 && TARGET_64BIT)
     return orig;
 
@@ -3064,7 +3068,12 @@ darwin_file_end (void)
       fprintf (asm_out_file, "\t.long\t0\n\t.long\t%u\n", flags);
      }
 
+#if !DARWIN_ARM64
   machopic_finish (asm_out_file);
+#else
+  gcc_checking_assert (!machopic_indirections);
+#endif
+
   if (flag_apple_kext)
     {
       /* These sections are only used for kernel code.  */
diff --git gcc/config/darwin.h gcc/config/darwin.h
index 2602f5d49c9..1a1133db7de 100644
--- gcc/config/darwin.h
+++ gcc/config/darwin.h
@@ -42,6 +42,7 @@ see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
 
 #define DARWIN_X86 0
 #define DARWIN_PPC 0
+#define DARWIN_ARM64 0
 
 /* Suppress g++ attempt to link in the math library automatically. */
 #define MATH_LIBRARY ""
-- 
2.42.1

