From a2982a4d5d4df3543cdd2e32c6cf3aefa537a88a Mon Sep 17 00:00:00 2001
From: Iain Sandoe <iain@sandoe.co.uk>
Date: Sat, 12 Sep 2020 09:44:55 +0100
Subject: [PATCH] Darwin, Arm64 : Assign a register for the static chain.

At present, we do not have a workable solution for trampolines for
the Darwin Arm64 port.  Executable stack is not an option and the
advice from Arm is not to use any lower address bits to signal that
we have a descriptor.

It is possible that we might be able to load some remappable object
(e.g. a dylib) - but the details there still have to be resolved.

In any case, we need a usable static chain - and the port has that
assigned to R18 by default - which is the platform register in darwinpcs.

It's uncertain whether R16/R17 are actually used in the darwinpcs in
the role of support for branch islanding.  The AAPCS64 use as IP0/1
is conditional on ELF relocations, so perhaps irrelevant to Darwin.

Using R16 does not conflict with the existing (non-functional) trampoline
code.

lots of TODOs here.

(cherry picked from commit 69d64d3eef51272592611095f32af196433d1bd0)
Signed-off-by: Kirill A. Korinsky <kirill@korins.ky>
---
 gcc/config/aarch64/darwin.h | 5 +++++
 1 file changed, 5 insertions(+)

diff --git gcc/config/aarch64/darwin.h gcc/config/aarch64/darwin.h
index 25721630924..b605b94f97f 100644
--- gcc/config/aarch64/darwin.h
+++ gcc/config/aarch64/darwin.h
@@ -150,6 +150,11 @@ along with GCC; see the file COPYING3.  If not see
     1, 1			/* FFR and FFRT */	\
   }
 
+/* Although we cannot use executable stack, we still need to assign
+   a static chain regnum.  At the moment using R16 (IP0) is available.  */
+#undef STATIC_CHAIN_REGNUM
+#define STATIC_CHAIN_REGNUM	R16_REGNUM
+
 #define SUBTARGET_ENCODE_SECTION_INFO  darwin_encode_section_info
 
 #undef ASM_MAYBE_OUTPUT_ENCODED_ADDR_RTX
-- 
2.42.1

