Description: <short summary of the patch>
 TODO: Put a short summary on the line above and replace this paragraph
 with a longer explanation of this change. Complete the meta-information
 with other relevant fields (see below for details). To make it easier, the
 information below has been extracted from the changelog. Adjust it or drop
 it.
 .
 gcl (2.6.10-46) unstable; urgency=medium
 .
   * aarch64 st8 reloc updates
Author: Camm Maguire <camm@debian.org>

---
The information above should follow the Patch Tagging Guidelines, please
checkout http://dep.debian.net/deps/dep3/ to learn about the format. Here
are templates for supplementary fields that you might want to add:

Origin: <vendor|upstream|other>, <url of original patch>
Bug: <url in upstream bugtracker>
Bug-Debian: http://bugs.debian.org/<bugnumber>
Bug-Ubuntu: https://launchpad.net/bugs/<bugnumber>
Forwarded: <no|not-needed|url proving that it has been forwarded>
Reviewed-By: <name and email of someone who approved the patch>
Last-Update: <YYYY-MM-DD>

diff --git a/gcl/h/aarch64-linux.h b/gcl/h/aarch64-linux.h
index 921677d..d851524 100644
--- gcl-2.6.10.orig/h/aarch64-linux.h
+++ gcl-2.6.10/h/aarch64-linux.h
@@ -1,9 +1,17 @@
 #include "linux.h"
 
-//#define SGC
-//#define NEED_STACK_CHK_GUARD
-//C_GC_OFFSET
-//PTR_ALIGN
+#ifdef IN_GBC
+#undef MPROTECT_ACTION_FLAGS
+#define MPROTECT_ACTION_FLAGS SA_RESTART|SA_SIGINFO
+#define GET_FAULT_ADDR(sig,code,sv,a) \
+ ((siginfo_t *)code)->si_addr
+/*  #define GET_FAULT_ADDR(sig,code,sv,a) \ */
+/*      ((void *)(*((char ***)(&code)))[44]) */
+#endif
+
+//#define SGC  Not working as of 8/14/14 CM
 
 #define RELOC_H "elf64_aarch64_reloc.h"
 #define SPECIAL_RELOC_H "elf64_aarch64_reloc_special.h"
+
+#define NEED_STACK_CHK_GUARD
diff --git a/gcl/h/elf64_aarch64_reloc.h b/gcl/h/elf64_aarch64_reloc.h
index 753ff28..f03d449 100644
--- gcl-2.6.10.orig/h/elf64_aarch64_reloc.h
+++ gcl-2.6.10/h/elf64_aarch64_reloc.h
@@ -8,13 +8,13 @@
     case R_AARCH64_CALL26: /* BL:     ((S+A-P) >> 2) & 0x3ffffff.  */
       {
 	long x=((long)(s+a-p))/4;
-	if (abs(x)&(~MASK(26))) {
+	if (abs(x)&(~MASK(25))) {
 	  got+=gotp;
 	  *got++=s+a;
 	  gotp++;
 	  memcpy(got,tramp,sizeof(tramp));
 	  gotp+=sizeof(tramp)/sizeof(*got);
-	  x=((long)(got+1))/4;
+	  x=((long)got-p)/4;
 	}
 	store_vals(where,MASK(26),x);
       }
@@ -32,7 +32,7 @@
     case R_AARCH64_LDST8_ABS_LO12_NC: /* LD/ST8: (S+A) & 0xfff */
       store_val(where,MASK(12) << 10,((s+a) & 0xfff) << 10);
       break;
-    case R_AARCH64_LDST16_ABS_LO12_NC: /* LD/ST16: (S+A) & 0xffc */
+    case R_AARCH64_LDST16_ABS_LO12_NC: /* LD/ST16: (S+A) & 0xffe */
       store_val(where,MASK(12) << 10,((s+a) & 0xffe) << 9);
       break;
     case R_AARCH64_LDST32_ABS_LO12_NC: /* LD/ST32: (S+A) & 0xffc */
@@ -41,6 +41,6 @@
     case R_AARCH64_LDST64_ABS_LO12_NC: /* LD/ST64: (S+A) & 0xff8 */
       store_val(where,MASK(12) << 10,((s+a) & 0xff8) << 7);
       break;
-    case R_AARCH64_LDST128_ABS_LO12_NC: /* LD/ST128: (S+A) & 0xff8 */
+    case R_AARCH64_LDST128_ABS_LO12_NC: /* LD/ST128: (S+A) & 0xff0 */
       store_val(where,MASK(12) << 10,((s+a) & 0xff0) << 6);
       break;
diff --git a/gcl/o/main.c b/gcl/o/main.c
index 7136285..f5ecaaa 100755
--- gcl-2.6.10.orig/o/main.c
+++ gcl-2.6.10/o/main.c
@@ -309,11 +309,40 @@ DEFUN_NEW("SET-LOG-MAXPAGE-BOUND",object,fSset_log_maxpage_bound,SI,1,1,NONE,II,
 
 }
 
+#ifdef NEED_STACK_CHK_GUARD
+
+unsigned long __stack_chk_guard=0;
+
+static unsigned long
+random_ulong() {
+ 
+  object y;
+  
+  if (raw_image) return 0;
+
+  vs_top=vs_base;
+  vs_push(Ct);
+  Lmake_random_state();
+  y=vs_pop;
+  vs_push(number_negate(find_symbol(make_simple_string("MOST-NEGATIVE-FIXNUM"),system_package)->s.s_dbind));
+  vs_push(y);
+  Lrandom();
+
+  return fixint(vs_pop);
+
+}
+#endif
+
+
 int
 main(int argc, char **argv, char **envp) {
 
   gcl_init_alloc(&argv);
 
+#ifdef NEED_STACK_CHK_GUARD
+  __stack_chk_guard=random_ulong();/*Cannot be safely set inside a function which returns*/
+#endif
+
 #ifdef CAN_UNRANDOMIZE_SBRK
 #include <stdio.h>
 #include <stdlib.h>
diff --git a/gcl/o/prelink.c b/gcl/o/prelink.c
index b0a3775..abc3b61 100644
--- gcl-2.6.10.orig/o/prelink.c
+++ gcl-2.6.10/o/prelink.c
@@ -2,10 +2,6 @@
 
 #include "include.h"
 
-#ifdef NEED_STACK_CHK_GUARD
-unsigned long __stack_chk_guard=0;
-#endif
-
 void
 prelink_init(void) {
   
@@ -19,19 +15,5 @@ prelink_init(void) {
   my_rl_line_buffer=rl_line_buffer;
 #endif
 
-#ifdef NEED_STACK_CHK_GUARD
-  if (!raw_image) {
-    object y;
-    vs_top=vs_base;
-    vs_push(Ct);
-    Lmake_random_state();
-    y=vs_pop;
-    vs_push(number_negate(find_symbol(make_simple_string("MOST-NEGATIVE-FIXNUM"),system_package)->s.s_dbind));
-    vs_push(y);
-    Lrandom();
-    __stack_chk_guard=fixint(vs_pop);
-  }
-#endif
-
 }
 
