Blame SOURCES/0012-AArch64-GOT-fixed.patch

8e013d
From c428a156b8e659a884d4867e52c49534125dc72f Mon Sep 17 00:00:00 2001
8e013d
From: Mark Shinwell <mshinwell@gmail.com>
8e013d
Date: Wed, 13 Sep 2017 10:23:16 +0100
8e013d
Subject: [PATCH 12/12] AArch64 GOT fixed
8e013d
8e013d
---
8e013d
 Changes                    |  4 ++++
8e013d
 asmcomp/arm64/emit.mlp     | 15 +++++++++++++--
8e013d
 asmcomp/arm64/selection.ml |  4 ++--
8e013d
 3 files changed, 19 insertions(+), 4 deletions(-)
8e013d
8e013d
diff --git a/Changes b/Changes
8e013d
index e8dbd42e2..b84a1f30e 100644
8e013d
--- a/Changes
8e013d
+++ b/Changes
8e013d
@@ -150,6 +150,10 @@ OCaml 4.05.0 (13 Jul 2017):
8e013d
   (Hannes Mehnert, Guillaume Bury,
8e013d
    review by Daniel Bünzli, Gabriel Scherer, Damien Doligez)
8e013d
 
8e013d
+- GPR#1330: when generating dynamically-linkable code on AArch64, always
8e013d
+  reference symbols (even locally-defined ones) through the GOT.
8e013d
+  (Mark Shinwell, review by Xavier Leroy)
8e013d
+
8e013d
 ### Standard library:
8e013d
 
8e013d
 - MPR#6975, GPR#902: Truncate function added to stdlib Buffer module
8e013d
diff --git a/asmcomp/arm64/emit.mlp b/asmcomp/arm64/emit.mlp
8e013d
index f75646e12..729096c57 100644
8e013d
--- a/asmcomp/arm64/emit.mlp
8e013d
+++ b/asmcomp/arm64/emit.mlp
8e013d
@@ -114,6 +114,7 @@ let emit_addressing addr r =
8e013d
   | Iindexed ofs ->
8e013d
       `[{emit_reg r}, #{emit_int ofs}]`
8e013d
   | Ibased(s, ofs) ->
8e013d
+      assert (not !Clflags.dlcode);  (* see selection.ml *)
8e013d
       `[{emit_reg r}, #:lo12:{emit_symbol_offset s ofs}]`
8e013d
 
8e013d
 (* Record live pointers at call points *)
8e013d
@@ -323,7 +324,7 @@ let emit_literals() =
8e013d
 (* Emit code to load the address of a symbol *)
8e013d
 
8e013d
 let emit_load_symbol_addr dst s =
8e013d
-  if (not !Clflags.dlcode) || Compilenv.symbol_in_current_unit s then begin
8e013d
+  if not !Clflags.dlcode then begin
8e013d
     `	adrp	{emit_reg dst}, {emit_symbol s}\n`;
8e013d
     `	add	{emit_reg dst}, {emit_reg dst}, #:lo12:{emit_symbol s}\n`
8e013d
   end else begin
8e013d
@@ -609,6 +610,7 @@ let emit_instr i =
8e013d
           match addr with
8e013d
           | Iindexed _ -> i.arg.(0)
8e013d
           | Ibased(s, ofs) ->
8e013d
+              assert (not !Clflags.dlcode);  (* see selection.ml *)
8e013d
               `	adrp	{emit_reg reg_tmp1}, {emit_symbol_offset s ofs}\n`;
8e013d
               reg_tmp1 in
8e013d
         begin match size with
8e013d
@@ -636,6 +638,7 @@ let emit_instr i =
8e013d
           match addr with
8e013d
           | Iindexed _ -> i.arg.(1)
8e013d
           | Ibased(s, ofs) ->
8e013d
+              assert (not !Clflags.dlcode);
8e013d
               `	adrp	{emit_reg reg_tmp1}, {emit_symbol_offset s ofs}\n`;
8e013d
               reg_tmp1 in
8e013d
         begin match size with
8e013d
@@ -924,7 +927,15 @@ let fundecl fundecl =
8e013d
 
8e013d
 let emit_item = function
8e013d
   | Cglobal_symbol s -> `	.globl	{emit_symbol s}\n`;
8e013d
-  | Cdefine_symbol s -> `{emit_symbol s}:\n`
8e013d
+  | Cdefine_symbol s ->
8e013d
+    if !Clflags.dlcode then begin
8e013d
+      (* GOT relocations against non-global symbols don't seem to work
8e013d
+         properly: GOT entries are not created for the symbols and the
8e013d
+         relocations evaluate to random other GOT entries.  For the moment
8e013d
+         force all symbols to be global. *)
8e013d
+      `	.globl	{emit_symbol s}\n`;
8e013d
+    end;
8e013d
+    `{emit_symbol s}:\n`
8e013d
   | Cint8 n -> `	.byte	{emit_int n}\n`
8e013d
   | Cint16 n -> `	.short	{emit_int n}\n`
8e013d
   | Cint32 n -> `	.long	{emit_nativeint n}\n`
8e013d
diff --git a/asmcomp/arm64/selection.ml b/asmcomp/arm64/selection.ml
8e013d
index d8ea7f83b..b714d0032 100644
8e013d
--- a/asmcomp/arm64/selection.ml
8e013d
+++ b/asmcomp/arm64/selection.ml
8e013d
@@ -82,8 +82,8 @@ let inline_ops =
8e013d
   [ "sqrt"; "caml_bswap16_direct"; "caml_int32_direct_bswap";
8e013d
     "caml_int64_direct_bswap"; "caml_nativeint_direct_bswap" ]
8e013d
 
8e013d
-let use_direct_addressing symb =
8e013d
-  (not !Clflags.dlcode) || Compilenv.symbol_in_current_unit symb
8e013d
+let use_direct_addressing _symb =
8e013d
+  not !Clflags.dlcode
8e013d
 
8e013d
 (* Instruction selection *)
8e013d
 
8e013d
-- 
8e013d
2.13.2
8e013d