From 3dbc6ac67a0efac9eaabb5ea4623247fa717d189 Mon Sep 17 00:00:00 2001
From: Moritz Angermann <moritz.angermann@gmail.com>
Date: Thu, 20 Aug 2020 02:13:02 +0000
Subject: [PATCH] Trying to get PIC right.

---
 compiler/GHC/Cmm/CLabel.hs   |  4 ++++
 compiler/GHC/CmmToAsm/PIC.hs | 19 +++++++++++++------
 2 files changed, 17 insertions(+), 6 deletions(-)

diff --git a/compiler/GHC/Cmm/CLabel.hs b/compiler/GHC/Cmm/CLabel.hs
index 4f06f83c5f5f..76c699e98f34 100644
--- a/compiler/GHC/Cmm/CLabel.hs
+++ b/compiler/GHC/Cmm/CLabel.hs
@@ -1436,6 +1436,10 @@ pprDynamicLinkerAsmLabel platform dllInfo lbl =
           SymbolPtr -> text ".LC_" <> ppr lbl
           _         -> panic "pprDynamicLinkerAsmLabel"
 
+      | platformArch platform == ArchAArch64
+      = ppr lbl
+
+
       | platformArch platform == ArchX86_64
       = case dllInfo of
           CodeStub        -> ppr lbl <> text "@plt"
diff --git a/compiler/GHC/CmmToAsm/PIC.hs b/compiler/GHC/CmmToAsm/PIC.hs
index 0cc54cea9103..753244625bfe 100644
--- a/compiler/GHC/CmmToAsm/PIC.hs
+++ b/compiler/GHC/CmmToAsm/PIC.hs
@@ -133,6 +133,14 @@ cmmMakeDynamicReference config referenceKind lbl
               addImport stub
               return $ CmmLit $ CmmLabel stub
 
+        -- GOT relative loads work differently on AArch64.  We don't
+        -- the got symbol is loaded directly, and not through an additional
+        -- load.
+        AccessViaSymbolPtr | ArchAArch64 <- platformArch platform -> do
+              let symbolPtr = mkDynamicLinkerLabel SymbolPtr lbl
+              addImport symbolPtr
+              return $ cmmMakePicReference config symbolPtr
+
         AccessViaSymbolPtr -> do
               let symbolPtr = mkDynamicLinkerLabel SymbolPtr lbl
               addImport symbolPtr
@@ -146,7 +154,6 @@ cmmMakeDynamicReference config referenceKind lbl
                 -- so just jump there if it's a call or a jump
               _ -> return $ CmmLit $ CmmLabel lbl
 
-
 -- -----------------------------------------------------------------------------
 -- Create a position independent reference to a label.
 -- (but do not bother with dynamic linking).
@@ -261,11 +268,11 @@ howToAccessLabel config _ OSMinGW32 this_mod _ lbl
 -- is enough for ~64MB of range. Anything else will need to go through a veneer,
 -- which is the job of the linker to build.  We might only want to lookup
 -- Data References through the GOT.
-howToAccessLabel _config ArchAArch64 _os _this_mod kind _lbl
-        = case kind of
-            DataReference -> AccessDirectly -- AccessViaSymbolPtr
-            CallReference -> AccessDirectly
-            JumpReference -> AccessDirectly
+-- howToAccessLabel _config ArchAArch64 _os _this_mod kind _lbl
+--         = case kind of
+--             DataReference -> AccessDirectly -- AccessViaSymbolPtr
+--             CallReference -> AccessDirectly
+--             JumpReference -> AccessDirectly
 
 
 -- Mach-O (Darwin, Mac OS X)
-- 
GitLab